]> code.delx.au - gnu-emacs/blob - src/xdisp.c
(temp_echo_area_glyphs): String pointer args now
[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
694 \f
695 /* Function prototypes. */
696
697 static void setup_for_ellipsis P_ ((struct it *));
698 static void mark_window_display_accurate_1 P_ ((struct window *, int));
699 static int single_display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
700 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
701 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
702 static int redisplay_mode_lines P_ ((Lisp_Object, int));
703 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
704
705 #if 0
706 static int invisible_text_between_p P_ ((struct it *, int, int));
707 #endif
708
709 static int next_element_from_ellipsis P_ ((struct it *));
710 static void pint2str P_ ((char *, int, int));
711 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
712 struct text_pos));
713 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
714 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
715 static void store_frame_title_char P_ ((char));
716 static int store_frame_title P_ ((unsigned char *, int, int));
717 static void x_consider_frame_title P_ ((Lisp_Object));
718 static void handle_stop P_ ((struct it *));
719 static int tool_bar_lines_needed P_ ((struct frame *));
720 static int single_display_prop_intangible_p P_ ((Lisp_Object));
721 static void ensure_echo_area_buffers P_ ((void));
722 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
723 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
724 static int with_echo_area_buffer P_ ((struct window *, int,
725 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
726 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
727 static void clear_garbaged_frames P_ ((void));
728 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
729 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
730 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
731 static int display_echo_area P_ ((struct window *));
732 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
733 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
734 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
735 static int string_char_and_length P_ ((unsigned char *, int, int *));
736 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
737 struct text_pos));
738 static int compute_window_start_on_continuation_line P_ ((struct window *));
739 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
740 static void insert_left_trunc_glyphs P_ ((struct it *));
741 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
742 static void extend_face_to_end_of_line P_ ((struct it *));
743 static int append_space P_ ((struct it *, int));
744 static int make_cursor_line_fully_visible P_ ((struct window *));
745 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int));
746 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
747 static int trailing_whitespace_p P_ ((int));
748 static int message_log_check_duplicate P_ ((int, int, int, int));
749 static void push_it P_ ((struct it *));
750 static void pop_it P_ ((struct it *));
751 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
752 static void redisplay_internal P_ ((int));
753 static int echo_area_display P_ ((int));
754 static void redisplay_windows P_ ((Lisp_Object));
755 static void redisplay_window P_ ((Lisp_Object, int));
756 static Lisp_Object redisplay_window_error ();
757 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
758 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
759 static void update_menu_bar P_ ((struct frame *, int));
760 static int try_window_reusing_current_matrix P_ ((struct window *));
761 static int try_window_id P_ ((struct window *));
762 static int display_line P_ ((struct it *));
763 static int display_mode_lines P_ ((struct window *));
764 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
765 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
766 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
767 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
768 static void display_menu_bar P_ ((struct window *));
769 static int display_count_lines P_ ((int, int, int, int, int *));
770 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
771 int, int, struct it *, int, int, int, int));
772 static void compute_line_metrics P_ ((struct it *));
773 static void run_redisplay_end_trigger_hook P_ ((struct it *));
774 static int get_overlay_strings P_ ((struct it *, int));
775 static void next_overlay_string P_ ((struct it *));
776 static void reseat P_ ((struct it *, struct text_pos, int));
777 static void reseat_1 P_ ((struct it *, struct text_pos, int));
778 static void back_to_previous_visible_line_start P_ ((struct it *));
779 static void reseat_at_previous_visible_line_start P_ ((struct it *));
780 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
781 static int next_element_from_display_vector P_ ((struct it *));
782 static int next_element_from_string P_ ((struct it *));
783 static int next_element_from_c_string P_ ((struct it *));
784 static int next_element_from_buffer P_ ((struct it *));
785 static int next_element_from_composition P_ ((struct it *));
786 static int next_element_from_image P_ ((struct it *));
787 static int next_element_from_stretch P_ ((struct it *));
788 static void load_overlay_strings P_ ((struct it *, int));
789 static int init_from_display_pos P_ ((struct it *, struct window *,
790 struct display_pos *));
791 static void reseat_to_string P_ ((struct it *, unsigned char *,
792 Lisp_Object, int, int, int, int));
793 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
794 int, int, int));
795 void move_it_vertically_backward P_ ((struct it *, int));
796 static void init_to_row_start P_ ((struct it *, struct window *,
797 struct glyph_row *));
798 static int init_to_row_end P_ ((struct it *, struct window *,
799 struct glyph_row *));
800 static void back_to_previous_line_start P_ ((struct it *));
801 static int forward_to_next_line_start P_ ((struct it *, int *));
802 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
803 Lisp_Object, int));
804 static struct text_pos string_pos P_ ((int, Lisp_Object));
805 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
806 static int number_of_chars P_ ((unsigned char *, int));
807 static void compute_stop_pos P_ ((struct it *));
808 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
809 Lisp_Object));
810 static int face_before_or_after_it_pos P_ ((struct it *, int));
811 static int next_overlay_change P_ ((int));
812 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
813 Lisp_Object, struct text_pos *,
814 int));
815 static int underlying_face_id P_ ((struct it *));
816 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
817 struct window *));
818
819 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
820 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
821
822 #ifdef HAVE_WINDOW_SYSTEM
823
824 static void update_tool_bar P_ ((struct frame *, int));
825 static void build_desired_tool_bar_string P_ ((struct frame *f));
826 static int redisplay_tool_bar P_ ((struct frame *));
827 static void display_tool_bar_line P_ ((struct it *));
828
829 #endif /* HAVE_WINDOW_SYSTEM */
830
831 \f
832 /***********************************************************************
833 Window display dimensions
834 ***********************************************************************/
835
836 /* Return the window-relative maximum y + 1 for glyph rows displaying
837 text in window W. This is the height of W minus the height of a
838 mode line, if any. */
839
840 INLINE int
841 window_text_bottom_y (w)
842 struct window *w;
843 {
844 struct frame *f = XFRAME (w->frame);
845 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
846
847 if (WINDOW_WANTS_MODELINE_P (w))
848 height -= CURRENT_MODE_LINE_HEIGHT (w);
849 return height;
850 }
851
852
853 /* Return the pixel width of display area AREA of window W. AREA < 0
854 means return the total width of W, not including fringes to
855 the left and right of the window. */
856
857 INLINE int
858 window_box_width (w, area)
859 struct window *w;
860 int area;
861 {
862 struct frame *f = XFRAME (w->frame);
863 int width = XFASTINT (w->width);
864
865 if (!w->pseudo_window_p)
866 {
867 width -= FRAME_SCROLL_BAR_WIDTH (f) + FRAME_FRINGE_COLS (f);
868
869 if (area == TEXT_AREA)
870 {
871 if (INTEGERP (w->left_margin_width))
872 width -= XFASTINT (w->left_margin_width);
873 if (INTEGERP (w->right_margin_width))
874 width -= XFASTINT (w->right_margin_width);
875 }
876 else if (area == LEFT_MARGIN_AREA)
877 width = (INTEGERP (w->left_margin_width)
878 ? XFASTINT (w->left_margin_width) : 0);
879 else if (area == RIGHT_MARGIN_AREA)
880 width = (INTEGERP (w->right_margin_width)
881 ? XFASTINT (w->right_margin_width) : 0);
882 }
883
884 return width * CANON_X_UNIT (f);
885 }
886
887
888 /* Return the pixel height of the display area of window W, not
889 including mode lines of W, if any. */
890
891 INLINE int
892 window_box_height (w)
893 struct window *w;
894 {
895 struct frame *f = XFRAME (w->frame);
896 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
897
898 xassert (height >= 0);
899
900 /* Note: the code below that determines the mode-line/header-line
901 height is essentially the same as that contained in the macro
902 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
903 the appropriate glyph row has its `mode_line_p' flag set,
904 and if it doesn't, uses estimate_mode_line_height instead. */
905
906 if (WINDOW_WANTS_MODELINE_P (w))
907 {
908 struct glyph_row *ml_row
909 = (w->current_matrix && w->current_matrix->rows
910 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
911 : 0);
912 if (ml_row && ml_row->mode_line_p)
913 height -= ml_row->height;
914 else
915 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
916 }
917
918 if (WINDOW_WANTS_HEADER_LINE_P (w))
919 {
920 struct glyph_row *hl_row
921 = (w->current_matrix && w->current_matrix->rows
922 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
923 : 0);
924 if (hl_row && hl_row->mode_line_p)
925 height -= hl_row->height;
926 else
927 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
928 }
929
930 /* With a very small font and a mode-line that's taller than
931 default, we might end up with a negative height. */
932 return max (0, height);
933 }
934
935
936 /* Return the frame-relative coordinate of the left edge of display
937 area AREA of window W. AREA < 0 means return the left edge of the
938 whole window, to the right of the left fringe of W. */
939
940 INLINE int
941 window_box_left (w, area)
942 struct window *w;
943 int area;
944 {
945 struct frame *f = XFRAME (w->frame);
946 int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
947
948 if (!w->pseudo_window_p)
949 {
950 x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
951 + FRAME_LEFT_FRINGE_WIDTH (f));
952
953 if (area == TEXT_AREA)
954 x += window_box_width (w, LEFT_MARGIN_AREA);
955 else if (area == RIGHT_MARGIN_AREA)
956 x += (window_box_width (w, LEFT_MARGIN_AREA)
957 + window_box_width (w, TEXT_AREA));
958 }
959
960 return x;
961 }
962
963
964 /* Return the frame-relative coordinate of the right edge of display
965 area AREA of window W. AREA < 0 means return the left edge of the
966 whole window, to the left of the right fringe of W. */
967
968 INLINE int
969 window_box_right (w, area)
970 struct window *w;
971 int area;
972 {
973 return window_box_left (w, area) + window_box_width (w, area);
974 }
975
976
977 /* Get the bounding box of the display area AREA of window W, without
978 mode lines, in frame-relative coordinates. AREA < 0 means the
979 whole window, not including the left and right fringes of
980 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
981 coordinates of the upper-left corner of the box. Return in
982 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
983
984 INLINE void
985 window_box (w, area, box_x, box_y, box_width, box_height)
986 struct window *w;
987 int area;
988 int *box_x, *box_y, *box_width, *box_height;
989 {
990 struct frame *f = XFRAME (w->frame);
991
992 *box_width = window_box_width (w, area);
993 *box_height = window_box_height (w);
994 *box_x = window_box_left (w, area);
995 *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
996 + XFASTINT (w->top) * CANON_Y_UNIT (f));
997 if (WINDOW_WANTS_HEADER_LINE_P (w))
998 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
999 }
1000
1001
1002 /* Get the bounding box of the display area AREA of window W, without
1003 mode lines. AREA < 0 means the whole window, not including the
1004 left and right fringe of the window. Return in *TOP_LEFT_X
1005 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1006 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1007 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1008 box. */
1009
1010 INLINE void
1011 window_box_edges (w, area, top_left_x, top_left_y,
1012 bottom_right_x, bottom_right_y)
1013 struct window *w;
1014 int area;
1015 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1016 {
1017 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1018 bottom_right_y);
1019 *bottom_right_x += *top_left_x;
1020 *bottom_right_y += *top_left_y;
1021 }
1022
1023
1024 \f
1025 /***********************************************************************
1026 Utilities
1027 ***********************************************************************/
1028
1029 /* Return the bottom y-position of the line the iterator IT is in.
1030 This can modify IT's settings. */
1031
1032 int
1033 line_bottom_y (it)
1034 struct it *it;
1035 {
1036 int line_height = it->max_ascent + it->max_descent;
1037 int line_top_y = it->current_y;
1038
1039 if (line_height == 0)
1040 {
1041 if (last_height)
1042 line_height = last_height;
1043 else if (IT_CHARPOS (*it) < ZV)
1044 {
1045 move_it_by_lines (it, 1, 1);
1046 line_height = (it->max_ascent || it->max_descent
1047 ? it->max_ascent + it->max_descent
1048 : last_height);
1049 }
1050 else
1051 {
1052 struct glyph_row *row = it->glyph_row;
1053
1054 /* Use the default character height. */
1055 it->glyph_row = NULL;
1056 it->what = IT_CHARACTER;
1057 it->c = ' ';
1058 it->len = 1;
1059 PRODUCE_GLYPHS (it);
1060 line_height = it->ascent + it->descent;
1061 it->glyph_row = row;
1062 }
1063 }
1064
1065 return line_top_y + line_height;
1066 }
1067
1068
1069 /* Return 1 if position CHARPOS is visible in window W. Set *FULLY to
1070 1 if POS is visible and the line containing POS is fully visible.
1071 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1072 and header-lines heights. */
1073
1074 int
1075 pos_visible_p (w, charpos, fully, exact_mode_line_heights_p)
1076 struct window *w;
1077 int charpos, *fully, exact_mode_line_heights_p;
1078 {
1079 struct it it;
1080 struct text_pos top;
1081 int visible_p;
1082 struct buffer *old_buffer = NULL;
1083
1084 if (XBUFFER (w->buffer) != current_buffer)
1085 {
1086 old_buffer = current_buffer;
1087 set_buffer_internal_1 (XBUFFER (w->buffer));
1088 }
1089
1090 *fully = visible_p = 0;
1091 SET_TEXT_POS_FROM_MARKER (top, w->start);
1092
1093 /* Compute exact mode line heights, if requested. */
1094 if (exact_mode_line_heights_p)
1095 {
1096 if (WINDOW_WANTS_MODELINE_P (w))
1097 current_mode_line_height
1098 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1099 current_buffer->mode_line_format);
1100
1101 if (WINDOW_WANTS_HEADER_LINE_P (w))
1102 current_header_line_height
1103 = display_mode_line (w, HEADER_LINE_FACE_ID,
1104 current_buffer->header_line_format);
1105 }
1106
1107 start_display (&it, w, top);
1108 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
1109 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
1110
1111 /* Note that we may overshoot because of invisible text. */
1112 if (IT_CHARPOS (it) >= charpos)
1113 {
1114 int top_y = it.current_y;
1115 int bottom_y = line_bottom_y (&it);
1116 int window_top_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
1117
1118 if (top_y < window_top_y)
1119 visible_p = bottom_y > window_top_y;
1120 else if (top_y < it.last_visible_y)
1121 {
1122 visible_p = 1;
1123 *fully = bottom_y <= it.last_visible_y;
1124 }
1125 }
1126 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1127 {
1128 move_it_by_lines (&it, 1, 0);
1129 if (charpos < IT_CHARPOS (it))
1130 {
1131 visible_p = 1;
1132 *fully = 0;
1133 }
1134 }
1135
1136 if (old_buffer)
1137 set_buffer_internal_1 (old_buffer);
1138
1139 current_header_line_height = current_mode_line_height = -1;
1140 return visible_p;
1141 }
1142
1143
1144 /* Return the next character from STR which is MAXLEN bytes long.
1145 Return in *LEN the length of the character. This is like
1146 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1147 we find one, we return a `?', but with the length of the invalid
1148 character. */
1149
1150 static INLINE int
1151 string_char_and_length (str, maxlen, len)
1152 unsigned char *str;
1153 int maxlen, *len;
1154 {
1155 int c;
1156
1157 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1158 if (!CHAR_VALID_P (c, 1))
1159 /* We may not change the length here because other places in Emacs
1160 don't use this function, i.e. they silently accept invalid
1161 characters. */
1162 c = '?';
1163
1164 return c;
1165 }
1166
1167
1168
1169 /* Given a position POS containing a valid character and byte position
1170 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1171
1172 static struct text_pos
1173 string_pos_nchars_ahead (pos, string, nchars)
1174 struct text_pos pos;
1175 Lisp_Object string;
1176 int nchars;
1177 {
1178 xassert (STRINGP (string) && nchars >= 0);
1179
1180 if (STRING_MULTIBYTE (string))
1181 {
1182 int rest = SBYTES (string) - BYTEPOS (pos);
1183 unsigned char *p = SDATA (string) + BYTEPOS (pos);
1184 int len;
1185
1186 while (nchars--)
1187 {
1188 string_char_and_length (p, rest, &len);
1189 p += len, rest -= len;
1190 xassert (rest >= 0);
1191 CHARPOS (pos) += 1;
1192 BYTEPOS (pos) += len;
1193 }
1194 }
1195 else
1196 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1197
1198 return pos;
1199 }
1200
1201
1202 /* Value is the text position, i.e. character and byte position,
1203 for character position CHARPOS in STRING. */
1204
1205 static INLINE struct text_pos
1206 string_pos (charpos, string)
1207 int charpos;
1208 Lisp_Object string;
1209 {
1210 struct text_pos pos;
1211 xassert (STRINGP (string));
1212 xassert (charpos >= 0);
1213 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1214 return pos;
1215 }
1216
1217
1218 /* Value is a text position, i.e. character and byte position, for
1219 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1220 means recognize multibyte characters. */
1221
1222 static struct text_pos
1223 c_string_pos (charpos, s, multibyte_p)
1224 int charpos;
1225 unsigned char *s;
1226 int multibyte_p;
1227 {
1228 struct text_pos pos;
1229
1230 xassert (s != NULL);
1231 xassert (charpos >= 0);
1232
1233 if (multibyte_p)
1234 {
1235 int rest = strlen (s), len;
1236
1237 SET_TEXT_POS (pos, 0, 0);
1238 while (charpos--)
1239 {
1240 string_char_and_length (s, rest, &len);
1241 s += len, rest -= len;
1242 xassert (rest >= 0);
1243 CHARPOS (pos) += 1;
1244 BYTEPOS (pos) += len;
1245 }
1246 }
1247 else
1248 SET_TEXT_POS (pos, charpos, charpos);
1249
1250 return pos;
1251 }
1252
1253
1254 /* Value is the number of characters in C string S. MULTIBYTE_P
1255 non-zero means recognize multibyte characters. */
1256
1257 static int
1258 number_of_chars (s, multibyte_p)
1259 unsigned char *s;
1260 int multibyte_p;
1261 {
1262 int nchars;
1263
1264 if (multibyte_p)
1265 {
1266 int rest = strlen (s), len;
1267 unsigned char *p = (unsigned char *) s;
1268
1269 for (nchars = 0; rest > 0; ++nchars)
1270 {
1271 string_char_and_length (p, rest, &len);
1272 rest -= len, p += len;
1273 }
1274 }
1275 else
1276 nchars = strlen (s);
1277
1278 return nchars;
1279 }
1280
1281
1282 /* Compute byte position NEWPOS->bytepos corresponding to
1283 NEWPOS->charpos. POS is a known position in string STRING.
1284 NEWPOS->charpos must be >= POS.charpos. */
1285
1286 static void
1287 compute_string_pos (newpos, pos, string)
1288 struct text_pos *newpos, pos;
1289 Lisp_Object string;
1290 {
1291 xassert (STRINGP (string));
1292 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1293
1294 if (STRING_MULTIBYTE (string))
1295 *newpos = string_pos_nchars_ahead (pos, string,
1296 CHARPOS (*newpos) - CHARPOS (pos));
1297 else
1298 BYTEPOS (*newpos) = CHARPOS (*newpos);
1299 }
1300
1301
1302 \f
1303 /***********************************************************************
1304 Lisp form evaluation
1305 ***********************************************************************/
1306
1307 /* Error handler for safe_eval and safe_call. */
1308
1309 static Lisp_Object
1310 safe_eval_handler (arg)
1311 Lisp_Object arg;
1312 {
1313 add_to_log ("Error during redisplay: %s", arg, Qnil);
1314 return Qnil;
1315 }
1316
1317
1318 /* Evaluate SEXPR and return the result, or nil if something went
1319 wrong. Prevent redisplay during the evaluation. */
1320
1321 Lisp_Object
1322 safe_eval (sexpr)
1323 Lisp_Object sexpr;
1324 {
1325 Lisp_Object val;
1326
1327 if (inhibit_eval_during_redisplay)
1328 val = Qnil;
1329 else
1330 {
1331 int count = SPECPDL_INDEX ();
1332 struct gcpro gcpro1;
1333
1334 GCPRO1 (sexpr);
1335 specbind (Qinhibit_redisplay, Qt);
1336 /* Use Qt to ensure debugger does not run,
1337 so there is no possibility of wanting to redisplay. */
1338 val = internal_condition_case_1 (Feval, sexpr, Qt,
1339 safe_eval_handler);
1340 UNGCPRO;
1341 val = unbind_to (count, val);
1342 }
1343
1344 return val;
1345 }
1346
1347
1348 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1349 Return the result, or nil if something went wrong. Prevent
1350 redisplay during the evaluation. */
1351
1352 Lisp_Object
1353 safe_call (nargs, args)
1354 int nargs;
1355 Lisp_Object *args;
1356 {
1357 Lisp_Object val;
1358
1359 if (inhibit_eval_during_redisplay)
1360 val = Qnil;
1361 else
1362 {
1363 int count = SPECPDL_INDEX ();
1364 struct gcpro gcpro1;
1365
1366 GCPRO1 (args[0]);
1367 gcpro1.nvars = nargs;
1368 specbind (Qinhibit_redisplay, Qt);
1369 /* Use Qt to ensure debugger does not run,
1370 so there is no possibility of wanting to redisplay. */
1371 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
1372 safe_eval_handler);
1373 UNGCPRO;
1374 val = unbind_to (count, val);
1375 }
1376
1377 return val;
1378 }
1379
1380
1381 /* Call function FN with one argument ARG.
1382 Return the result, or nil if something went wrong. */
1383
1384 Lisp_Object
1385 safe_call1 (fn, arg)
1386 Lisp_Object fn, arg;
1387 {
1388 Lisp_Object args[2];
1389 args[0] = fn;
1390 args[1] = arg;
1391 return safe_call (2, args);
1392 }
1393
1394
1395 \f
1396 /***********************************************************************
1397 Debugging
1398 ***********************************************************************/
1399
1400 #if 0
1401
1402 /* Define CHECK_IT to perform sanity checks on iterators.
1403 This is for debugging. It is too slow to do unconditionally. */
1404
1405 static void
1406 check_it (it)
1407 struct it *it;
1408 {
1409 if (it->method == next_element_from_string)
1410 {
1411 xassert (STRINGP (it->string));
1412 xassert (IT_STRING_CHARPOS (*it) >= 0);
1413 }
1414 else if (it->method == next_element_from_buffer)
1415 {
1416 /* Check that character and byte positions agree. */
1417 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1418 }
1419
1420 if (it->dpvec)
1421 xassert (it->current.dpvec_index >= 0);
1422 else
1423 xassert (it->current.dpvec_index < 0);
1424 }
1425
1426 #define CHECK_IT(IT) check_it ((IT))
1427
1428 #else /* not 0 */
1429
1430 #define CHECK_IT(IT) (void) 0
1431
1432 #endif /* not 0 */
1433
1434
1435 #if GLYPH_DEBUG
1436
1437 /* Check that the window end of window W is what we expect it
1438 to be---the last row in the current matrix displaying text. */
1439
1440 static void
1441 check_window_end (w)
1442 struct window *w;
1443 {
1444 if (!MINI_WINDOW_P (w)
1445 && !NILP (w->window_end_valid))
1446 {
1447 struct glyph_row *row;
1448 xassert ((row = MATRIX_ROW (w->current_matrix,
1449 XFASTINT (w->window_end_vpos)),
1450 !row->enabled_p
1451 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1452 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1453 }
1454 }
1455
1456 #define CHECK_WINDOW_END(W) check_window_end ((W))
1457
1458 #else /* not GLYPH_DEBUG */
1459
1460 #define CHECK_WINDOW_END(W) (void) 0
1461
1462 #endif /* not GLYPH_DEBUG */
1463
1464
1465 \f
1466 /***********************************************************************
1467 Iterator initialization
1468 ***********************************************************************/
1469
1470 /* Initialize IT for displaying current_buffer in window W, starting
1471 at character position CHARPOS. CHARPOS < 0 means that no buffer
1472 position is specified which is useful when the iterator is assigned
1473 a position later. BYTEPOS is the byte position corresponding to
1474 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
1475
1476 If ROW is not null, calls to produce_glyphs with IT as parameter
1477 will produce glyphs in that row.
1478
1479 BASE_FACE_ID is the id of a base face to use. It must be one of
1480 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
1481 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
1482 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
1483
1484 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
1485 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
1486 will be initialized to use the corresponding mode line glyph row of
1487 the desired matrix of W. */
1488
1489 void
1490 init_iterator (it, w, charpos, bytepos, row, base_face_id)
1491 struct it *it;
1492 struct window *w;
1493 int charpos, bytepos;
1494 struct glyph_row *row;
1495 enum face_id base_face_id;
1496 {
1497 int highlight_region_p;
1498
1499 /* Some precondition checks. */
1500 xassert (w != NULL && it != NULL);
1501 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
1502 && charpos <= ZV));
1503
1504 /* If face attributes have been changed since the last redisplay,
1505 free realized faces now because they depend on face definitions
1506 that might have changed. */
1507 if (face_change_count)
1508 {
1509 face_change_count = 0;
1510 free_all_realized_faces (Qnil);
1511 }
1512
1513 /* Use one of the mode line rows of W's desired matrix if
1514 appropriate. */
1515 if (row == NULL)
1516 {
1517 if (base_face_id == MODE_LINE_FACE_ID
1518 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
1519 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
1520 else if (base_face_id == HEADER_LINE_FACE_ID)
1521 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
1522 }
1523
1524 /* Clear IT. */
1525 bzero (it, sizeof *it);
1526 it->current.overlay_string_index = -1;
1527 it->current.dpvec_index = -1;
1528 it->base_face_id = base_face_id;
1529
1530 /* The window in which we iterate over current_buffer: */
1531 XSETWINDOW (it->window, w);
1532 it->w = w;
1533 it->f = XFRAME (w->frame);
1534
1535 /* Extra space between lines (on window systems only). */
1536 if (base_face_id == DEFAULT_FACE_ID
1537 && FRAME_WINDOW_P (it->f))
1538 {
1539 if (NATNUMP (current_buffer->extra_line_spacing))
1540 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
1541 else if (it->f->extra_line_spacing > 0)
1542 it->extra_line_spacing = it->f->extra_line_spacing;
1543 }
1544
1545 /* If realized faces have been removed, e.g. because of face
1546 attribute changes of named faces, recompute them. When running
1547 in batch mode, the face cache of Vterminal_frame is null. If
1548 we happen to get called, make a dummy face cache. */
1549 if (
1550 #ifndef WINDOWSNT
1551 noninteractive &&
1552 #endif
1553 FRAME_FACE_CACHE (it->f) == NULL)
1554 init_frame_faces (it->f);
1555 if (FRAME_FACE_CACHE (it->f)->used == 0)
1556 recompute_basic_faces (it->f);
1557
1558 /* Current value of the `space-width', and 'height' properties. */
1559 it->space_width = Qnil;
1560 it->font_height = Qnil;
1561
1562 /* Are control characters displayed as `^C'? */
1563 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
1564
1565 /* -1 means everything between a CR and the following line end
1566 is invisible. >0 means lines indented more than this value are
1567 invisible. */
1568 it->selective = (INTEGERP (current_buffer->selective_display)
1569 ? XFASTINT (current_buffer->selective_display)
1570 : (!NILP (current_buffer->selective_display)
1571 ? -1 : 0));
1572 it->selective_display_ellipsis_p
1573 = !NILP (current_buffer->selective_display_ellipses);
1574
1575 /* Display table to use. */
1576 it->dp = window_display_table (w);
1577
1578 /* Are multibyte characters enabled in current_buffer? */
1579 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1580
1581 /* Non-zero if we should highlight the region. */
1582 highlight_region_p
1583 = (!NILP (Vtransient_mark_mode)
1584 && !NILP (current_buffer->mark_active)
1585 && XMARKER (current_buffer->mark)->buffer != 0);
1586
1587 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
1588 start and end of a visible region in window IT->w. Set both to
1589 -1 to indicate no region. */
1590 if (highlight_region_p
1591 /* Maybe highlight only in selected window. */
1592 && (/* Either show region everywhere. */
1593 highlight_nonselected_windows
1594 /* Or show region in the selected window. */
1595 || w == XWINDOW (selected_window)
1596 /* Or show the region if we are in the mini-buffer and W is
1597 the window the mini-buffer refers to. */
1598 || (MINI_WINDOW_P (XWINDOW (selected_window))
1599 && WINDOWP (minibuf_selected_window)
1600 && w == XWINDOW (minibuf_selected_window))))
1601 {
1602 int charpos = marker_position (current_buffer->mark);
1603 it->region_beg_charpos = min (PT, charpos);
1604 it->region_end_charpos = max (PT, charpos);
1605 }
1606 else
1607 it->region_beg_charpos = it->region_end_charpos = -1;
1608
1609 /* Get the position at which the redisplay_end_trigger hook should
1610 be run, if it is to be run at all. */
1611 if (MARKERP (w->redisplay_end_trigger)
1612 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
1613 it->redisplay_end_trigger_charpos
1614 = marker_position (w->redisplay_end_trigger);
1615 else if (INTEGERP (w->redisplay_end_trigger))
1616 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
1617
1618 /* Correct bogus values of tab_width. */
1619 it->tab_width = XINT (current_buffer->tab_width);
1620 if (it->tab_width <= 0 || it->tab_width > 1000)
1621 it->tab_width = 8;
1622
1623 /* Are lines in the display truncated? */
1624 it->truncate_lines_p
1625 = (base_face_id != DEFAULT_FACE_ID
1626 || XINT (it->w->hscroll)
1627 || (truncate_partial_width_windows
1628 && !WINDOW_FULL_WIDTH_P (it->w))
1629 || !NILP (current_buffer->truncate_lines));
1630
1631 /* Get dimensions of truncation and continuation glyphs. These are
1632 displayed as fringe bitmaps under X, so we don't need them for such
1633 frames. */
1634 if (!FRAME_WINDOW_P (it->f))
1635 {
1636 if (it->truncate_lines_p)
1637 {
1638 /* We will need the truncation glyph. */
1639 xassert (it->glyph_row == NULL);
1640 produce_special_glyphs (it, IT_TRUNCATION);
1641 it->truncation_pixel_width = it->pixel_width;
1642 }
1643 else
1644 {
1645 /* We will need the continuation glyph. */
1646 xassert (it->glyph_row == NULL);
1647 produce_special_glyphs (it, IT_CONTINUATION);
1648 it->continuation_pixel_width = it->pixel_width;
1649 }
1650
1651 /* Reset these values to zero because the produce_special_glyphs
1652 above has changed them. */
1653 it->pixel_width = it->ascent = it->descent = 0;
1654 it->phys_ascent = it->phys_descent = 0;
1655 }
1656
1657 /* Set this after getting the dimensions of truncation and
1658 continuation glyphs, so that we don't produce glyphs when calling
1659 produce_special_glyphs, above. */
1660 it->glyph_row = row;
1661 it->area = TEXT_AREA;
1662
1663 /* Get the dimensions of the display area. The display area
1664 consists of the visible window area plus a horizontally scrolled
1665 part to the left of the window. All x-values are relative to the
1666 start of this total display area. */
1667 if (base_face_id != DEFAULT_FACE_ID)
1668 {
1669 /* Mode lines, menu bar in terminal frames. */
1670 it->first_visible_x = 0;
1671 it->last_visible_x = XFASTINT (w->width) * CANON_X_UNIT (it->f);
1672 }
1673 else
1674 {
1675 it->first_visible_x
1676 = XFASTINT (it->w->hscroll) * CANON_X_UNIT (it->f);
1677 it->last_visible_x = (it->first_visible_x
1678 + window_box_width (w, TEXT_AREA));
1679
1680 /* If we truncate lines, leave room for the truncator glyph(s) at
1681 the right margin. Otherwise, leave room for the continuation
1682 glyph(s). Truncation and continuation glyphs are not inserted
1683 for window-based redisplay. */
1684 if (!FRAME_WINDOW_P (it->f))
1685 {
1686 if (it->truncate_lines_p)
1687 it->last_visible_x -= it->truncation_pixel_width;
1688 else
1689 it->last_visible_x -= it->continuation_pixel_width;
1690 }
1691
1692 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
1693 it->current_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w) + w->vscroll;
1694 }
1695
1696 /* Leave room for a border glyph. */
1697 if (!FRAME_WINDOW_P (it->f)
1698 && !WINDOW_RIGHTMOST_P (it->w))
1699 it->last_visible_x -= 1;
1700
1701 it->last_visible_y = window_text_bottom_y (w);
1702
1703 /* For mode lines and alike, arrange for the first glyph having a
1704 left box line if the face specifies a box. */
1705 if (base_face_id != DEFAULT_FACE_ID)
1706 {
1707 struct face *face;
1708
1709 it->face_id = base_face_id;
1710
1711 /* If we have a boxed mode line, make the first character appear
1712 with a left box line. */
1713 face = FACE_FROM_ID (it->f, base_face_id);
1714 if (face->box != FACE_NO_BOX)
1715 it->start_of_box_run_p = 1;
1716 }
1717
1718 /* If a buffer position was specified, set the iterator there,
1719 getting overlays and face properties from that position. */
1720 if (charpos >= BUF_BEG (current_buffer))
1721 {
1722 it->end_charpos = ZV;
1723 it->face_id = -1;
1724 IT_CHARPOS (*it) = charpos;
1725
1726 /* Compute byte position if not specified. */
1727 if (bytepos < charpos)
1728 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
1729 else
1730 IT_BYTEPOS (*it) = bytepos;
1731
1732 /* Compute faces etc. */
1733 reseat (it, it->current.pos, 1);
1734 }
1735
1736 CHECK_IT (it);
1737 }
1738
1739
1740 /* Initialize IT for the display of window W with window start POS. */
1741
1742 void
1743 start_display (it, w, pos)
1744 struct it *it;
1745 struct window *w;
1746 struct text_pos pos;
1747 {
1748 struct glyph_row *row;
1749 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
1750
1751 row = w->desired_matrix->rows + first_vpos;
1752 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
1753
1754 if (!it->truncate_lines_p)
1755 {
1756 int start_at_line_beg_p;
1757 int first_y = it->current_y;
1758
1759 /* If window start is not at a line start, skip forward to POS to
1760 get the correct continuation lines width. */
1761 start_at_line_beg_p = (CHARPOS (pos) == BEGV
1762 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
1763 if (!start_at_line_beg_p)
1764 {
1765 reseat_at_previous_visible_line_start (it);
1766 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
1767
1768 /* If lines are continued, this line may end in the middle
1769 of a multi-glyph character (e.g. a control character
1770 displayed as \003, or in the middle of an overlay
1771 string). In this case move_it_to above will not have
1772 taken us to the start of the continuation line but to the
1773 end of the continued line. */
1774 if (it->current_x > 0)
1775 {
1776 if (it->current.dpvec_index >= 0
1777 || it->current.overlay_string_index >= 0)
1778 {
1779 set_iterator_to_next (it, 1);
1780 move_it_in_display_line_to (it, -1, -1, 0);
1781 }
1782
1783 it->continuation_lines_width += it->current_x;
1784 }
1785
1786 /* We're starting a new display line, not affected by the
1787 height of the continued line, so clear the appropriate
1788 fields in the iterator structure. */
1789 it->max_ascent = it->max_descent = 0;
1790 it->max_phys_ascent = it->max_phys_descent = 0;
1791
1792 it->current_y = first_y;
1793 it->vpos = 0;
1794 it->current_x = it->hpos = 0;
1795 }
1796 }
1797
1798 #if 0 /* Don't assert the following because start_display is sometimes
1799 called intentionally with a window start that is not at a
1800 line start. Please leave this code in as a comment. */
1801
1802 /* Window start should be on a line start, now. */
1803 xassert (it->continuation_lines_width
1804 || IT_CHARPOS (it) == BEGV
1805 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
1806 #endif /* 0 */
1807 }
1808
1809
1810 /* Return 1 if POS is a position in ellipses displayed for invisible
1811 text. W is the window we display, for text property lookup. */
1812
1813 static int
1814 in_ellipses_for_invisible_text_p (pos, w)
1815 struct display_pos *pos;
1816 struct window *w;
1817 {
1818 Lisp_Object prop, window;
1819 int ellipses_p = 0;
1820 int charpos = CHARPOS (pos->pos);
1821
1822 /* If POS specifies a position in a display vector, this might
1823 be for an ellipsis displayed for invisible text. We won't
1824 get the iterator set up for delivering that ellipsis unless
1825 we make sure that it gets aware of the invisible text. */
1826 if (pos->dpvec_index >= 0
1827 && pos->overlay_string_index < 0
1828 && CHARPOS (pos->string_pos) < 0
1829 && charpos > BEGV
1830 && (XSETWINDOW (window, w),
1831 prop = Fget_char_property (make_number (charpos),
1832 Qinvisible, window),
1833 !TEXT_PROP_MEANS_INVISIBLE (prop)))
1834 {
1835 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
1836 window);
1837 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
1838 }
1839
1840 return ellipses_p;
1841 }
1842
1843
1844 /* Initialize IT for stepping through current_buffer in window W,
1845 starting at position POS that includes overlay string and display
1846 vector/ control character translation position information. Value
1847 is zero if there are overlay strings with newlines at POS. */
1848
1849 static int
1850 init_from_display_pos (it, w, pos)
1851 struct it *it;
1852 struct window *w;
1853 struct display_pos *pos;
1854 {
1855 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
1856 int i, overlay_strings_with_newlines = 0;
1857
1858 /* If POS specifies a position in a display vector, this might
1859 be for an ellipsis displayed for invisible text. We won't
1860 get the iterator set up for delivering that ellipsis unless
1861 we make sure that it gets aware of the invisible text. */
1862 if (in_ellipses_for_invisible_text_p (pos, w))
1863 {
1864 --charpos;
1865 bytepos = 0;
1866 }
1867
1868 /* Keep in mind: the call to reseat in init_iterator skips invisible
1869 text, so we might end up at a position different from POS. This
1870 is only a problem when POS is a row start after a newline and an
1871 overlay starts there with an after-string, and the overlay has an
1872 invisible property. Since we don't skip invisible text in
1873 display_line and elsewhere immediately after consuming the
1874 newline before the row start, such a POS will not be in a string,
1875 but the call to init_iterator below will move us to the
1876 after-string. */
1877 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
1878
1879 for (i = 0; i < it->n_overlay_strings; ++i)
1880 {
1881 char *s = SDATA (it->overlay_strings[i]);
1882 char *e = s + SBYTES (it->overlay_strings[i]);
1883
1884 while (s < e && *s != '\n')
1885 ++s;
1886
1887 if (s < e)
1888 {
1889 overlay_strings_with_newlines = 1;
1890 break;
1891 }
1892 }
1893
1894 /* If position is within an overlay string, set up IT to the right
1895 overlay string. */
1896 if (pos->overlay_string_index >= 0)
1897 {
1898 int relative_index;
1899
1900 /* If the first overlay string happens to have a `display'
1901 property for an image, the iterator will be set up for that
1902 image, and we have to undo that setup first before we can
1903 correct the overlay string index. */
1904 if (it->method == next_element_from_image)
1905 pop_it (it);
1906
1907 /* We already have the first chunk of overlay strings in
1908 IT->overlay_strings. Load more until the one for
1909 pos->overlay_string_index is in IT->overlay_strings. */
1910 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
1911 {
1912 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
1913 it->current.overlay_string_index = 0;
1914 while (n--)
1915 {
1916 load_overlay_strings (it, 0);
1917 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
1918 }
1919 }
1920
1921 it->current.overlay_string_index = pos->overlay_string_index;
1922 relative_index = (it->current.overlay_string_index
1923 % OVERLAY_STRING_CHUNK_SIZE);
1924 it->string = it->overlay_strings[relative_index];
1925 xassert (STRINGP (it->string));
1926 it->current.string_pos = pos->string_pos;
1927 it->method = next_element_from_string;
1928 }
1929
1930 #if 0 /* This is bogus because POS not having an overlay string
1931 position does not mean it's after the string. Example: A
1932 line starting with a before-string and initialization of IT
1933 to the previous row's end position. */
1934 else if (it->current.overlay_string_index >= 0)
1935 {
1936 /* If POS says we're already after an overlay string ending at
1937 POS, make sure to pop the iterator because it will be in
1938 front of that overlay string. When POS is ZV, we've thereby
1939 also ``processed'' overlay strings at ZV. */
1940 while (it->sp)
1941 pop_it (it);
1942 it->current.overlay_string_index = -1;
1943 it->method = next_element_from_buffer;
1944 if (CHARPOS (pos->pos) == ZV)
1945 it->overlay_strings_at_end_processed_p = 1;
1946 }
1947 #endif /* 0 */
1948
1949 if (CHARPOS (pos->string_pos) >= 0)
1950 {
1951 /* Recorded position is not in an overlay string, but in another
1952 string. This can only be a string from a `display' property.
1953 IT should already be filled with that string. */
1954 it->current.string_pos = pos->string_pos;
1955 xassert (STRINGP (it->string));
1956 }
1957
1958 /* Restore position in display vector translations, control
1959 character translations or ellipses. */
1960 if (pos->dpvec_index >= 0)
1961 {
1962 if (it->dpvec == NULL)
1963 get_next_display_element (it);
1964 xassert (it->dpvec && it->current.dpvec_index == 0);
1965 it->current.dpvec_index = pos->dpvec_index;
1966 }
1967
1968 CHECK_IT (it);
1969 return !overlay_strings_with_newlines;
1970 }
1971
1972
1973 /* Initialize IT for stepping through current_buffer in window W
1974 starting at ROW->start. */
1975
1976 static void
1977 init_to_row_start (it, w, row)
1978 struct it *it;
1979 struct window *w;
1980 struct glyph_row *row;
1981 {
1982 init_from_display_pos (it, w, &row->start);
1983 it->continuation_lines_width = row->continuation_lines_width;
1984 CHECK_IT (it);
1985 }
1986
1987
1988 /* Initialize IT for stepping through current_buffer in window W
1989 starting in the line following ROW, i.e. starting at ROW->end.
1990 Value is zero if there are overlay strings with newlines at ROW's
1991 end position. */
1992
1993 static int
1994 init_to_row_end (it, w, row)
1995 struct it *it;
1996 struct window *w;
1997 struct glyph_row *row;
1998 {
1999 int success = 0;
2000
2001 if (init_from_display_pos (it, w, &row->end))
2002 {
2003 if (row->continued_p)
2004 it->continuation_lines_width
2005 = row->continuation_lines_width + row->pixel_width;
2006 CHECK_IT (it);
2007 success = 1;
2008 }
2009
2010 return success;
2011 }
2012
2013
2014
2015 \f
2016 /***********************************************************************
2017 Text properties
2018 ***********************************************************************/
2019
2020 /* Called when IT reaches IT->stop_charpos. Handle text property and
2021 overlay changes. Set IT->stop_charpos to the next position where
2022 to stop. */
2023
2024 static void
2025 handle_stop (it)
2026 struct it *it;
2027 {
2028 enum prop_handled handled;
2029 int handle_overlay_change_p = 1;
2030 struct props *p;
2031
2032 it->dpvec = NULL;
2033 it->current.dpvec_index = -1;
2034
2035 do
2036 {
2037 handled = HANDLED_NORMALLY;
2038
2039 /* Call text property handlers. */
2040 for (p = it_props; p->handler; ++p)
2041 {
2042 handled = p->handler (it);
2043
2044 if (handled == HANDLED_RECOMPUTE_PROPS)
2045 break;
2046 else if (handled == HANDLED_RETURN)
2047 return;
2048 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2049 handle_overlay_change_p = 0;
2050 }
2051
2052 if (handled != HANDLED_RECOMPUTE_PROPS)
2053 {
2054 /* Don't check for overlay strings below when set to deliver
2055 characters from a display vector. */
2056 if (it->method == next_element_from_display_vector)
2057 handle_overlay_change_p = 0;
2058
2059 /* Handle overlay changes. */
2060 if (handle_overlay_change_p)
2061 handled = handle_overlay_change (it);
2062
2063 /* Determine where to stop next. */
2064 if (handled == HANDLED_NORMALLY)
2065 compute_stop_pos (it);
2066 }
2067 }
2068 while (handled == HANDLED_RECOMPUTE_PROPS);
2069 }
2070
2071
2072 /* Compute IT->stop_charpos from text property and overlay change
2073 information for IT's current position. */
2074
2075 static void
2076 compute_stop_pos (it)
2077 struct it *it;
2078 {
2079 register INTERVAL iv, next_iv;
2080 Lisp_Object object, limit, position;
2081
2082 /* If nowhere else, stop at the end. */
2083 it->stop_charpos = it->end_charpos;
2084
2085 if (STRINGP (it->string))
2086 {
2087 /* Strings are usually short, so don't limit the search for
2088 properties. */
2089 object = it->string;
2090 limit = Qnil;
2091 position = make_number (IT_STRING_CHARPOS (*it));
2092 }
2093 else
2094 {
2095 int charpos;
2096
2097 /* If next overlay change is in front of the current stop pos
2098 (which is IT->end_charpos), stop there. Note: value of
2099 next_overlay_change is point-max if no overlay change
2100 follows. */
2101 charpos = next_overlay_change (IT_CHARPOS (*it));
2102 if (charpos < it->stop_charpos)
2103 it->stop_charpos = charpos;
2104
2105 /* If showing the region, we have to stop at the region
2106 start or end because the face might change there. */
2107 if (it->region_beg_charpos > 0)
2108 {
2109 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2110 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2111 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2112 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2113 }
2114
2115 /* Set up variables for computing the stop position from text
2116 property changes. */
2117 XSETBUFFER (object, current_buffer);
2118 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2119 position = make_number (IT_CHARPOS (*it));
2120
2121 }
2122
2123 /* Get the interval containing IT's position. Value is a null
2124 interval if there isn't such an interval. */
2125 iv = validate_interval_range (object, &position, &position, 0);
2126 if (!NULL_INTERVAL_P (iv))
2127 {
2128 Lisp_Object values_here[LAST_PROP_IDX];
2129 struct props *p;
2130
2131 /* Get properties here. */
2132 for (p = it_props; p->handler; ++p)
2133 values_here[p->idx] = textget (iv->plist, *p->name);
2134
2135 /* Look for an interval following iv that has different
2136 properties. */
2137 for (next_iv = next_interval (iv);
2138 (!NULL_INTERVAL_P (next_iv)
2139 && (NILP (limit)
2140 || XFASTINT (limit) > next_iv->position));
2141 next_iv = next_interval (next_iv))
2142 {
2143 for (p = it_props; p->handler; ++p)
2144 {
2145 Lisp_Object new_value;
2146
2147 new_value = textget (next_iv->plist, *p->name);
2148 if (!EQ (values_here[p->idx], new_value))
2149 break;
2150 }
2151
2152 if (p->handler)
2153 break;
2154 }
2155
2156 if (!NULL_INTERVAL_P (next_iv))
2157 {
2158 if (INTEGERP (limit)
2159 && next_iv->position >= XFASTINT (limit))
2160 /* No text property change up to limit. */
2161 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2162 else
2163 /* Text properties change in next_iv. */
2164 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2165 }
2166 }
2167
2168 xassert (STRINGP (it->string)
2169 || (it->stop_charpos >= BEGV
2170 && it->stop_charpos >= IT_CHARPOS (*it)));
2171 }
2172
2173
2174 /* Return the position of the next overlay change after POS in
2175 current_buffer. Value is point-max if no overlay change
2176 follows. This is like `next-overlay-change' but doesn't use
2177 xmalloc. */
2178
2179 static int
2180 next_overlay_change (pos)
2181 int pos;
2182 {
2183 int noverlays;
2184 int endpos;
2185 Lisp_Object *overlays;
2186 int len;
2187 int i;
2188
2189 /* Get all overlays at the given position. */
2190 len = 10;
2191 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2192 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2193 if (noverlays > len)
2194 {
2195 len = noverlays;
2196 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2197 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2198 }
2199
2200 /* If any of these overlays ends before endpos,
2201 use its ending point instead. */
2202 for (i = 0; i < noverlays; ++i)
2203 {
2204 Lisp_Object oend;
2205 int oendpos;
2206
2207 oend = OVERLAY_END (overlays[i]);
2208 oendpos = OVERLAY_POSITION (oend);
2209 endpos = min (endpos, oendpos);
2210 }
2211
2212 return endpos;
2213 }
2214
2215
2216 \f
2217 /***********************************************************************
2218 Fontification
2219 ***********************************************************************/
2220
2221 /* Handle changes in the `fontified' property of the current buffer by
2222 calling hook functions from Qfontification_functions to fontify
2223 regions of text. */
2224
2225 static enum prop_handled
2226 handle_fontified_prop (it)
2227 struct it *it;
2228 {
2229 Lisp_Object prop, pos;
2230 enum prop_handled handled = HANDLED_NORMALLY;
2231
2232 /* Get the value of the `fontified' property at IT's current buffer
2233 position. (The `fontified' property doesn't have a special
2234 meaning in strings.) If the value is nil, call functions from
2235 Qfontification_functions. */
2236 if (!STRINGP (it->string)
2237 && it->s == NULL
2238 && !NILP (Vfontification_functions)
2239 && !NILP (Vrun_hooks)
2240 && (pos = make_number (IT_CHARPOS (*it)),
2241 prop = Fget_char_property (pos, Qfontified, Qnil),
2242 NILP (prop)))
2243 {
2244 int count = SPECPDL_INDEX ();
2245 Lisp_Object val;
2246
2247 val = Vfontification_functions;
2248 specbind (Qfontification_functions, Qnil);
2249
2250 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2251 safe_call1 (val, pos);
2252 else
2253 {
2254 Lisp_Object globals, fn;
2255 struct gcpro gcpro1, gcpro2;
2256
2257 globals = Qnil;
2258 GCPRO2 (val, globals);
2259
2260 for (; CONSP (val); val = XCDR (val))
2261 {
2262 fn = XCAR (val);
2263
2264 if (EQ (fn, Qt))
2265 {
2266 /* A value of t indicates this hook has a local
2267 binding; it means to run the global binding too.
2268 In a global value, t should not occur. If it
2269 does, we must ignore it to avoid an endless
2270 loop. */
2271 for (globals = Fdefault_value (Qfontification_functions);
2272 CONSP (globals);
2273 globals = XCDR (globals))
2274 {
2275 fn = XCAR (globals);
2276 if (!EQ (fn, Qt))
2277 safe_call1 (fn, pos);
2278 }
2279 }
2280 else
2281 safe_call1 (fn, pos);
2282 }
2283
2284 UNGCPRO;
2285 }
2286
2287 unbind_to (count, Qnil);
2288
2289 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2290 something. This avoids an endless loop if they failed to
2291 fontify the text for which reason ever. */
2292 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2293 handled = HANDLED_RECOMPUTE_PROPS;
2294 }
2295
2296 return handled;
2297 }
2298
2299
2300 \f
2301 /***********************************************************************
2302 Faces
2303 ***********************************************************************/
2304
2305 /* Set up iterator IT from face properties at its current position.
2306 Called from handle_stop. */
2307
2308 static enum prop_handled
2309 handle_face_prop (it)
2310 struct it *it;
2311 {
2312 int new_face_id, next_stop;
2313
2314 if (!STRINGP (it->string))
2315 {
2316 new_face_id
2317 = face_at_buffer_position (it->w,
2318 IT_CHARPOS (*it),
2319 it->region_beg_charpos,
2320 it->region_end_charpos,
2321 &next_stop,
2322 (IT_CHARPOS (*it)
2323 + TEXT_PROP_DISTANCE_LIMIT),
2324 0);
2325
2326 /* Is this a start of a run of characters with box face?
2327 Caveat: this can be called for a freshly initialized
2328 iterator; face_id is -1 in this case. We know that the new
2329 face will not change until limit, i.e. if the new face has a
2330 box, all characters up to limit will have one. But, as
2331 usual, we don't know whether limit is really the end. */
2332 if (new_face_id != it->face_id)
2333 {
2334 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2335
2336 /* If new face has a box but old face has not, this is
2337 the start of a run of characters with box, i.e. it has
2338 a shadow on the left side. The value of face_id of the
2339 iterator will be -1 if this is the initial call that gets
2340 the face. In this case, we have to look in front of IT's
2341 position and see whether there is a face != new_face_id. */
2342 it->start_of_box_run_p
2343 = (new_face->box != FACE_NO_BOX
2344 && (it->face_id >= 0
2345 || IT_CHARPOS (*it) == BEG
2346 || new_face_id != face_before_it_pos (it)));
2347 it->face_box_p = new_face->box != FACE_NO_BOX;
2348 }
2349 }
2350 else
2351 {
2352 int base_face_id, bufpos;
2353
2354 if (it->current.overlay_string_index >= 0)
2355 bufpos = IT_CHARPOS (*it);
2356 else
2357 bufpos = 0;
2358
2359 /* For strings from a buffer, i.e. overlay strings or strings
2360 from a `display' property, use the face at IT's current
2361 buffer position as the base face to merge with, so that
2362 overlay strings appear in the same face as surrounding
2363 text, unless they specify their own faces. */
2364 base_face_id = underlying_face_id (it);
2365
2366 new_face_id = face_at_string_position (it->w,
2367 it->string,
2368 IT_STRING_CHARPOS (*it),
2369 bufpos,
2370 it->region_beg_charpos,
2371 it->region_end_charpos,
2372 &next_stop,
2373 base_face_id, 0);
2374
2375 #if 0 /* This shouldn't be neccessary. Let's check it. */
2376 /* If IT is used to display a mode line we would really like to
2377 use the mode line face instead of the frame's default face. */
2378 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2379 && new_face_id == DEFAULT_FACE_ID)
2380 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
2381 #endif
2382
2383 /* Is this a start of a run of characters with box? Caveat:
2384 this can be called for a freshly allocated iterator; face_id
2385 is -1 is this case. We know that the new face will not
2386 change until the next check pos, i.e. if the new face has a
2387 box, all characters up to that position will have a
2388 box. But, as usual, we don't know whether that position
2389 is really the end. */
2390 if (new_face_id != it->face_id)
2391 {
2392 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2393 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2394
2395 /* If new face has a box but old face hasn't, this is the
2396 start of a run of characters with box, i.e. it has a
2397 shadow on the left side. */
2398 it->start_of_box_run_p
2399 = new_face->box && (old_face == NULL || !old_face->box);
2400 it->face_box_p = new_face->box != FACE_NO_BOX;
2401 }
2402 }
2403
2404 it->face_id = new_face_id;
2405 return HANDLED_NORMALLY;
2406 }
2407
2408
2409 /* Return the ID of the face ``underlying'' IT's current position,
2410 which is in a string. If the iterator is associated with a
2411 buffer, return the face at IT's current buffer position.
2412 Otherwise, use the iterator's base_face_id. */
2413
2414 static int
2415 underlying_face_id (it)
2416 struct it *it;
2417 {
2418 int face_id = it->base_face_id, i;
2419
2420 xassert (STRINGP (it->string));
2421
2422 for (i = it->sp - 1; i >= 0; --i)
2423 if (NILP (it->stack[i].string))
2424 face_id = it->stack[i].face_id;
2425
2426 return face_id;
2427 }
2428
2429
2430 /* Compute the face one character before or after the current position
2431 of IT. BEFORE_P non-zero means get the face in front of IT's
2432 position. Value is the id of the face. */
2433
2434 static int
2435 face_before_or_after_it_pos (it, before_p)
2436 struct it *it;
2437 int before_p;
2438 {
2439 int face_id, limit;
2440 int next_check_charpos;
2441 struct text_pos pos;
2442
2443 xassert (it->s == NULL);
2444
2445 if (STRINGP (it->string))
2446 {
2447 int bufpos, base_face_id;
2448
2449 /* No face change past the end of the string (for the case
2450 we are padding with spaces). No face change before the
2451 string start. */
2452 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
2453 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2454 return it->face_id;
2455
2456 /* Set pos to the position before or after IT's current position. */
2457 if (before_p)
2458 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2459 else
2460 /* For composition, we must check the character after the
2461 composition. */
2462 pos = (it->what == IT_COMPOSITION
2463 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
2464 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
2465
2466 if (it->current.overlay_string_index >= 0)
2467 bufpos = IT_CHARPOS (*it);
2468 else
2469 bufpos = 0;
2470
2471 base_face_id = underlying_face_id (it);
2472
2473 /* Get the face for ASCII, or unibyte. */
2474 face_id = face_at_string_position (it->w,
2475 it->string,
2476 CHARPOS (pos),
2477 bufpos,
2478 it->region_beg_charpos,
2479 it->region_end_charpos,
2480 &next_check_charpos,
2481 base_face_id, 0);
2482
2483 /* Correct the face for charsets different from ASCII. Do it
2484 for the multibyte case only. The face returned above is
2485 suitable for unibyte text if IT->string is unibyte. */
2486 if (STRING_MULTIBYTE (it->string))
2487 {
2488 unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
2489 int rest = SBYTES (it->string) - BYTEPOS (pos);
2490 int c, len;
2491 struct face *face = FACE_FROM_ID (it->f, face_id);
2492
2493 c = string_char_and_length (p, rest, &len);
2494 face_id = FACE_FOR_CHAR (it->f, face, c);
2495 }
2496 }
2497 else
2498 {
2499 if ((IT_CHARPOS (*it) >= ZV && !before_p)
2500 || (IT_CHARPOS (*it) <= BEGV && before_p))
2501 return it->face_id;
2502
2503 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
2504 pos = it->current.pos;
2505
2506 if (before_p)
2507 DEC_TEXT_POS (pos, it->multibyte_p);
2508 else
2509 {
2510 if (it->what == IT_COMPOSITION)
2511 /* For composition, we must check the position after the
2512 composition. */
2513 pos.charpos += it->cmp_len, pos.bytepos += it->len;
2514 else
2515 INC_TEXT_POS (pos, it->multibyte_p);
2516 }
2517
2518 /* Determine face for CHARSET_ASCII, or unibyte. */
2519 face_id = face_at_buffer_position (it->w,
2520 CHARPOS (pos),
2521 it->region_beg_charpos,
2522 it->region_end_charpos,
2523 &next_check_charpos,
2524 limit, 0);
2525
2526 /* Correct the face for charsets different from ASCII. Do it
2527 for the multibyte case only. The face returned above is
2528 suitable for unibyte text if current_buffer is unibyte. */
2529 if (it->multibyte_p)
2530 {
2531 int c = FETCH_MULTIBYTE_CHAR (CHARPOS (pos));
2532 struct face *face = FACE_FROM_ID (it->f, face_id);
2533 face_id = FACE_FOR_CHAR (it->f, face, c);
2534 }
2535 }
2536
2537 return face_id;
2538 }
2539
2540
2541 \f
2542 /***********************************************************************
2543 Invisible text
2544 ***********************************************************************/
2545
2546 /* Set up iterator IT from invisible properties at its current
2547 position. Called from handle_stop. */
2548
2549 static enum prop_handled
2550 handle_invisible_prop (it)
2551 struct it *it;
2552 {
2553 enum prop_handled handled = HANDLED_NORMALLY;
2554
2555 if (STRINGP (it->string))
2556 {
2557 extern Lisp_Object Qinvisible;
2558 Lisp_Object prop, end_charpos, limit, charpos;
2559
2560 /* Get the value of the invisible text property at the
2561 current position. Value will be nil if there is no such
2562 property. */
2563 charpos = make_number (IT_STRING_CHARPOS (*it));
2564 prop = Fget_text_property (charpos, Qinvisible, it->string);
2565
2566 if (!NILP (prop)
2567 && IT_STRING_CHARPOS (*it) < it->end_charpos)
2568 {
2569 handled = HANDLED_RECOMPUTE_PROPS;
2570
2571 /* Get the position at which the next change of the
2572 invisible text property can be found in IT->string.
2573 Value will be nil if the property value is the same for
2574 all the rest of IT->string. */
2575 XSETINT (limit, SCHARS (it->string));
2576 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2577 it->string, limit);
2578
2579 /* Text at current position is invisible. The next
2580 change in the property is at position end_charpos.
2581 Move IT's current position to that position. */
2582 if (INTEGERP (end_charpos)
2583 && XFASTINT (end_charpos) < XFASTINT (limit))
2584 {
2585 struct text_pos old;
2586 old = it->current.string_pos;
2587 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
2588 compute_string_pos (&it->current.string_pos, old, it->string);
2589 }
2590 else
2591 {
2592 /* The rest of the string is invisible. If this is an
2593 overlay string, proceed with the next overlay string
2594 or whatever comes and return a character from there. */
2595 if (it->current.overlay_string_index >= 0)
2596 {
2597 next_overlay_string (it);
2598 /* Don't check for overlay strings when we just
2599 finished processing them. */
2600 handled = HANDLED_OVERLAY_STRING_CONSUMED;
2601 }
2602 else
2603 {
2604 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
2605 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
2606 }
2607 }
2608 }
2609 }
2610 else
2611 {
2612 int invis_p, newpos, next_stop, start_charpos;
2613 Lisp_Object pos, prop, overlay;
2614
2615 /* First of all, is there invisible text at this position? */
2616 start_charpos = IT_CHARPOS (*it);
2617 pos = make_number (IT_CHARPOS (*it));
2618 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
2619 &overlay);
2620 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
2621
2622 /* If we are on invisible text, skip over it. */
2623 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
2624 {
2625 /* Record whether we have to display an ellipsis for the
2626 invisible text. */
2627 int display_ellipsis_p = invis_p == 2;
2628
2629 handled = HANDLED_RECOMPUTE_PROPS;
2630
2631 /* Loop skipping over invisible text. The loop is left at
2632 ZV or with IT on the first char being visible again. */
2633 do
2634 {
2635 /* Try to skip some invisible text. Return value is the
2636 position reached which can be equal to IT's position
2637 if there is nothing invisible here. This skips both
2638 over invisible text properties and overlays with
2639 invisible property. */
2640 newpos = skip_invisible (IT_CHARPOS (*it),
2641 &next_stop, ZV, it->window);
2642
2643 /* If we skipped nothing at all we weren't at invisible
2644 text in the first place. If everything to the end of
2645 the buffer was skipped, end the loop. */
2646 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
2647 invis_p = 0;
2648 else
2649 {
2650 /* We skipped some characters but not necessarily
2651 all there are. Check if we ended up on visible
2652 text. Fget_char_property returns the property of
2653 the char before the given position, i.e. if we
2654 get invis_p = 0, this means that the char at
2655 newpos is visible. */
2656 pos = make_number (newpos);
2657 prop = Fget_char_property (pos, Qinvisible, it->window);
2658 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
2659 }
2660
2661 /* If we ended up on invisible text, proceed to
2662 skip starting with next_stop. */
2663 if (invis_p)
2664 IT_CHARPOS (*it) = next_stop;
2665 }
2666 while (invis_p);
2667
2668 /* The position newpos is now either ZV or on visible text. */
2669 IT_CHARPOS (*it) = newpos;
2670 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2671
2672 /* If there are before-strings at the start of invisible
2673 text, and the text is invisible because of a text
2674 property, arrange to show before-strings because 20.x did
2675 it that way. (If the text is invisible because of an
2676 overlay property instead of a text property, this is
2677 already handled in the overlay code.) */
2678 if (NILP (overlay)
2679 && get_overlay_strings (it, start_charpos))
2680 {
2681 handled = HANDLED_RECOMPUTE_PROPS;
2682 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
2683 }
2684 else if (display_ellipsis_p)
2685 setup_for_ellipsis (it);
2686 }
2687 }
2688
2689 return handled;
2690 }
2691
2692
2693 /* Make iterator IT return `...' next. */
2694
2695 static void
2696 setup_for_ellipsis (it)
2697 struct it *it;
2698 {
2699 if (it->dp
2700 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
2701 {
2702 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
2703 it->dpvec = v->contents;
2704 it->dpend = v->contents + v->size;
2705 }
2706 else
2707 {
2708 /* Default `...'. */
2709 it->dpvec = default_invis_vector;
2710 it->dpend = default_invis_vector + 3;
2711 }
2712
2713 /* The ellipsis display does not replace the display of the
2714 character at the new position. Indicate this by setting
2715 IT->dpvec_char_len to zero. */
2716 it->dpvec_char_len = 0;
2717
2718 it->current.dpvec_index = 0;
2719 it->method = next_element_from_display_vector;
2720 }
2721
2722
2723 \f
2724 /***********************************************************************
2725 'display' property
2726 ***********************************************************************/
2727
2728 /* Set up iterator IT from `display' property at its current position.
2729 Called from handle_stop. */
2730
2731 static enum prop_handled
2732 handle_display_prop (it)
2733 struct it *it;
2734 {
2735 Lisp_Object prop, object;
2736 struct text_pos *position;
2737 int display_replaced_p = 0;
2738
2739 if (STRINGP (it->string))
2740 {
2741 object = it->string;
2742 position = &it->current.string_pos;
2743 }
2744 else
2745 {
2746 object = it->w->buffer;
2747 position = &it->current.pos;
2748 }
2749
2750 /* Reset those iterator values set from display property values. */
2751 it->font_height = Qnil;
2752 it->space_width = Qnil;
2753 it->voffset = 0;
2754
2755 /* We don't support recursive `display' properties, i.e. string
2756 values that have a string `display' property, that have a string
2757 `display' property etc. */
2758 if (!it->string_from_display_prop_p)
2759 it->area = TEXT_AREA;
2760
2761 prop = Fget_char_property (make_number (position->charpos),
2762 Qdisplay, object);
2763 if (NILP (prop))
2764 return HANDLED_NORMALLY;
2765
2766 if (CONSP (prop)
2767 /* Simple properties. */
2768 && !EQ (XCAR (prop), Qimage)
2769 && !EQ (XCAR (prop), Qspace)
2770 && !EQ (XCAR (prop), Qwhen)
2771 && !EQ (XCAR (prop), Qspace_width)
2772 && !EQ (XCAR (prop), Qheight)
2773 && !EQ (XCAR (prop), Qraise)
2774 /* Marginal area specifications. */
2775 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
2776 && !NILP (XCAR (prop)))
2777 {
2778 for (; CONSP (prop); prop = XCDR (prop))
2779 {
2780 if (handle_single_display_prop (it, XCAR (prop), object,
2781 position, display_replaced_p))
2782 display_replaced_p = 1;
2783 }
2784 }
2785 else if (VECTORP (prop))
2786 {
2787 int i;
2788 for (i = 0; i < ASIZE (prop); ++i)
2789 if (handle_single_display_prop (it, AREF (prop, i), object,
2790 position, display_replaced_p))
2791 display_replaced_p = 1;
2792 }
2793 else
2794 {
2795 if (handle_single_display_prop (it, prop, object, position, 0))
2796 display_replaced_p = 1;
2797 }
2798
2799 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
2800 }
2801
2802
2803 /* Value is the position of the end of the `display' property starting
2804 at START_POS in OBJECT. */
2805
2806 static struct text_pos
2807 display_prop_end (it, object, start_pos)
2808 struct it *it;
2809 Lisp_Object object;
2810 struct text_pos start_pos;
2811 {
2812 Lisp_Object end;
2813 struct text_pos end_pos;
2814
2815 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
2816 Qdisplay, object, Qnil);
2817 CHARPOS (end_pos) = XFASTINT (end);
2818 if (STRINGP (object))
2819 compute_string_pos (&end_pos, start_pos, it->string);
2820 else
2821 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
2822
2823 return end_pos;
2824 }
2825
2826
2827 /* Set up IT from a single `display' sub-property value PROP. OBJECT
2828 is the object in which the `display' property was found. *POSITION
2829 is the position at which it was found. DISPLAY_REPLACED_P non-zero
2830 means that we previously saw a display sub-property which already
2831 replaced text display with something else, for example an image;
2832 ignore such properties after the first one has been processed.
2833
2834 If PROP is a `space' or `image' sub-property, set *POSITION to the
2835 end position of the `display' property.
2836
2837 Value is non-zero if something was found which replaces the display
2838 of buffer or string text. */
2839
2840 static int
2841 handle_single_display_prop (it, prop, object, position,
2842 display_replaced_before_p)
2843 struct it *it;
2844 Lisp_Object prop;
2845 Lisp_Object object;
2846 struct text_pos *position;
2847 int display_replaced_before_p;
2848 {
2849 Lisp_Object value;
2850 int replaces_text_display_p = 0;
2851 Lisp_Object form;
2852
2853 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
2854 evaluated. If the result is nil, VALUE is ignored. */
2855 form = Qt;
2856 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2857 {
2858 prop = XCDR (prop);
2859 if (!CONSP (prop))
2860 return 0;
2861 form = XCAR (prop);
2862 prop = XCDR (prop);
2863 }
2864
2865 if (!NILP (form) && !EQ (form, Qt))
2866 {
2867 int count = SPECPDL_INDEX ();
2868 struct gcpro gcpro1;
2869
2870 /* Bind `object' to the object having the `display' property, a
2871 buffer or string. Bind `position' to the position in the
2872 object where the property was found, and `buffer-position'
2873 to the current position in the buffer. */
2874 specbind (Qobject, object);
2875 specbind (Qposition, make_number (CHARPOS (*position)));
2876 specbind (Qbuffer_position,
2877 make_number (STRINGP (object)
2878 ? IT_CHARPOS (*it) : CHARPOS (*position)));
2879 GCPRO1 (form);
2880 form = safe_eval (form);
2881 UNGCPRO;
2882 unbind_to (count, Qnil);
2883 }
2884
2885 if (NILP (form))
2886 return 0;
2887
2888 if (CONSP (prop)
2889 && EQ (XCAR (prop), Qheight)
2890 && CONSP (XCDR (prop)))
2891 {
2892 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2893 return 0;
2894
2895 /* `(height HEIGHT)'. */
2896 it->font_height = XCAR (XCDR (prop));
2897 if (!NILP (it->font_height))
2898 {
2899 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2900 int new_height = -1;
2901
2902 if (CONSP (it->font_height)
2903 && (EQ (XCAR (it->font_height), Qplus)
2904 || EQ (XCAR (it->font_height), Qminus))
2905 && CONSP (XCDR (it->font_height))
2906 && INTEGERP (XCAR (XCDR (it->font_height))))
2907 {
2908 /* `(+ N)' or `(- N)' where N is an integer. */
2909 int steps = XINT (XCAR (XCDR (it->font_height)));
2910 if (EQ (XCAR (it->font_height), Qplus))
2911 steps = - steps;
2912 it->face_id = smaller_face (it->f, it->face_id, steps);
2913 }
2914 else if (FUNCTIONP (it->font_height))
2915 {
2916 /* Call function with current height as argument.
2917 Value is the new height. */
2918 Lisp_Object height;
2919 height = safe_call1 (it->font_height,
2920 face->lface[LFACE_HEIGHT_INDEX]);
2921 if (NUMBERP (height))
2922 new_height = XFLOATINT (height);
2923 }
2924 else if (NUMBERP (it->font_height))
2925 {
2926 /* Value is a multiple of the canonical char height. */
2927 struct face *face;
2928
2929 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
2930 new_height = (XFLOATINT (it->font_height)
2931 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
2932 }
2933 else
2934 {
2935 /* Evaluate IT->font_height with `height' bound to the
2936 current specified height to get the new height. */
2937 Lisp_Object value;
2938 int count = SPECPDL_INDEX ();
2939
2940 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
2941 value = safe_eval (it->font_height);
2942 unbind_to (count, Qnil);
2943
2944 if (NUMBERP (value))
2945 new_height = XFLOATINT (value);
2946 }
2947
2948 if (new_height > 0)
2949 it->face_id = face_with_height (it->f, it->face_id, new_height);
2950 }
2951 }
2952 else if (CONSP (prop)
2953 && EQ (XCAR (prop), Qspace_width)
2954 && CONSP (XCDR (prop)))
2955 {
2956 /* `(space_width WIDTH)'. */
2957 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2958 return 0;
2959
2960 value = XCAR (XCDR (prop));
2961 if (NUMBERP (value) && XFLOATINT (value) > 0)
2962 it->space_width = value;
2963 }
2964 else if (CONSP (prop)
2965 && EQ (XCAR (prop), Qraise)
2966 && CONSP (XCDR (prop)))
2967 {
2968 /* `(raise FACTOR)'. */
2969 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2970 return 0;
2971
2972 #ifdef HAVE_WINDOW_SYSTEM
2973 value = XCAR (XCDR (prop));
2974 if (NUMBERP (value))
2975 {
2976 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2977 it->voffset = - (XFLOATINT (value)
2978 * (FONT_HEIGHT (face->font)));
2979 }
2980 #endif /* HAVE_WINDOW_SYSTEM */
2981 }
2982 else if (!it->string_from_display_prop_p)
2983 {
2984 /* `((margin left-margin) VALUE)' or `((margin right-margin)
2985 VALUE) or `((margin nil) VALUE)' or VALUE. */
2986 Lisp_Object location, value;
2987 struct text_pos start_pos;
2988 int valid_p;
2989
2990 /* Characters having this form of property are not displayed, so
2991 we have to find the end of the property. */
2992 start_pos = *position;
2993 *position = display_prop_end (it, object, start_pos);
2994 value = Qnil;
2995
2996 /* Let's stop at the new position and assume that all
2997 text properties change there. */
2998 it->stop_charpos = position->charpos;
2999
3000 location = Qunbound;
3001 if (CONSP (prop) && CONSP (XCAR (prop)))
3002 {
3003 Lisp_Object tem;
3004
3005 value = XCDR (prop);
3006 if (CONSP (value))
3007 value = XCAR (value);
3008
3009 tem = XCAR (prop);
3010 if (EQ (XCAR (tem), Qmargin)
3011 && (tem = XCDR (tem),
3012 tem = CONSP (tem) ? XCAR (tem) : Qnil,
3013 (NILP (tem)
3014 || EQ (tem, Qleft_margin)
3015 || EQ (tem, Qright_margin))))
3016 location = tem;
3017 }
3018
3019 if (EQ (location, Qunbound))
3020 {
3021 location = Qnil;
3022 value = prop;
3023 }
3024
3025 #ifdef HAVE_WINDOW_SYSTEM
3026 if (FRAME_TERMCAP_P (it->f))
3027 valid_p = STRINGP (value);
3028 else
3029 valid_p = (STRINGP (value)
3030 || (CONSP (value) && EQ (XCAR (value), Qspace))
3031 || valid_image_p (value));
3032 #else /* not HAVE_WINDOW_SYSTEM */
3033 valid_p = STRINGP (value);
3034 #endif /* not HAVE_WINDOW_SYSTEM */
3035
3036 if ((EQ (location, Qleft_margin)
3037 || EQ (location, Qright_margin)
3038 || NILP (location))
3039 && valid_p
3040 && !display_replaced_before_p)
3041 {
3042 replaces_text_display_p = 1;
3043
3044 /* Save current settings of IT so that we can restore them
3045 when we are finished with the glyph property value. */
3046 push_it (it);
3047
3048 if (NILP (location))
3049 it->area = TEXT_AREA;
3050 else if (EQ (location, Qleft_margin))
3051 it->area = LEFT_MARGIN_AREA;
3052 else
3053 it->area = RIGHT_MARGIN_AREA;
3054
3055 if (STRINGP (value))
3056 {
3057 it->string = value;
3058 it->multibyte_p = STRING_MULTIBYTE (it->string);
3059 it->current.overlay_string_index = -1;
3060 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3061 it->end_charpos = it->string_nchars = SCHARS (it->string);
3062 it->method = next_element_from_string;
3063 it->stop_charpos = 0;
3064 it->string_from_display_prop_p = 1;
3065 /* Say that we haven't consumed the characters with
3066 `display' property yet. The call to pop_it in
3067 set_iterator_to_next will clean this up. */
3068 *position = start_pos;
3069 }
3070 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3071 {
3072 it->method = next_element_from_stretch;
3073 it->object = value;
3074 it->current.pos = it->position = start_pos;
3075 }
3076 #ifdef HAVE_WINDOW_SYSTEM
3077 else
3078 {
3079 it->what = IT_IMAGE;
3080 it->image_id = lookup_image (it->f, value);
3081 it->position = start_pos;
3082 it->object = NILP (object) ? it->w->buffer : object;
3083 it->method = next_element_from_image;
3084
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 #endif /* HAVE_WINDOW_SYSTEM */
3091 }
3092 else
3093 /* Invalid property or property not supported. Restore
3094 the position to what it was before. */
3095 *position = start_pos;
3096 }
3097
3098 return replaces_text_display_p;
3099 }
3100
3101
3102 /* Check if PROP is a display sub-property value whose text should be
3103 treated as intangible. */
3104
3105 static int
3106 single_display_prop_intangible_p (prop)
3107 Lisp_Object prop;
3108 {
3109 /* Skip over `when FORM'. */
3110 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3111 {
3112 prop = XCDR (prop);
3113 if (!CONSP (prop))
3114 return 0;
3115 prop = XCDR (prop);
3116 }
3117
3118 if (!CONSP (prop))
3119 return 0;
3120
3121 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3122 we don't need to treat text as intangible. */
3123 if (EQ (XCAR (prop), Qmargin))
3124 {
3125 prop = XCDR (prop);
3126 if (!CONSP (prop))
3127 return 0;
3128
3129 prop = XCDR (prop);
3130 if (!CONSP (prop)
3131 || EQ (XCAR (prop), Qleft_margin)
3132 || EQ (XCAR (prop), Qright_margin))
3133 return 0;
3134 }
3135
3136 return CONSP (prop) && EQ (XCAR (prop), Qimage);
3137 }
3138
3139
3140 /* Check if PROP is a display property value whose text should be
3141 treated as intangible. */
3142
3143 int
3144 display_prop_intangible_p (prop)
3145 Lisp_Object prop;
3146 {
3147 if (CONSP (prop)
3148 && CONSP (XCAR (prop))
3149 && !EQ (Qmargin, XCAR (XCAR (prop))))
3150 {
3151 /* A list of sub-properties. */
3152 while (CONSP (prop))
3153 {
3154 if (single_display_prop_intangible_p (XCAR (prop)))
3155 return 1;
3156 prop = XCDR (prop);
3157 }
3158 }
3159 else if (VECTORP (prop))
3160 {
3161 /* A vector of sub-properties. */
3162 int i;
3163 for (i = 0; i < ASIZE (prop); ++i)
3164 if (single_display_prop_intangible_p (AREF (prop, i)))
3165 return 1;
3166 }
3167 else
3168 return single_display_prop_intangible_p (prop);
3169
3170 return 0;
3171 }
3172
3173
3174 /* Return 1 if PROP is a display sub-property value containing STRING. */
3175
3176 static int
3177 single_display_prop_string_p (prop, string)
3178 Lisp_Object prop, string;
3179 {
3180 if (EQ (string, prop))
3181 return 1;
3182
3183 /* Skip over `when FORM'. */
3184 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3185 {
3186 prop = XCDR (prop);
3187 if (!CONSP (prop))
3188 return 0;
3189 prop = XCDR (prop);
3190 }
3191
3192 if (CONSP (prop))
3193 /* Skip over `margin LOCATION'. */
3194 if (EQ (XCAR (prop), Qmargin))
3195 {
3196 prop = XCDR (prop);
3197 if (!CONSP (prop))
3198 return 0;
3199
3200 prop = XCDR (prop);
3201 if (!CONSP (prop))
3202 return 0;
3203 }
3204
3205 return CONSP (prop) && EQ (XCAR (prop), string);
3206 }
3207
3208
3209 /* Return 1 if STRING appears in the `display' property PROP. */
3210
3211 static int
3212 display_prop_string_p (prop, string)
3213 Lisp_Object prop, string;
3214 {
3215 if (CONSP (prop)
3216 && CONSP (XCAR (prop))
3217 && !EQ (Qmargin, XCAR (XCAR (prop))))
3218 {
3219 /* A list of sub-properties. */
3220 while (CONSP (prop))
3221 {
3222 if (single_display_prop_string_p (XCAR (prop), string))
3223 return 1;
3224 prop = XCDR (prop);
3225 }
3226 }
3227 else if (VECTORP (prop))
3228 {
3229 /* A vector of sub-properties. */
3230 int i;
3231 for (i = 0; i < ASIZE (prop); ++i)
3232 if (single_display_prop_string_p (AREF (prop, i), string))
3233 return 1;
3234 }
3235 else
3236 return single_display_prop_string_p (prop, string);
3237
3238 return 0;
3239 }
3240
3241
3242 /* Determine from which buffer position in W's buffer STRING comes
3243 from. AROUND_CHARPOS is an approximate position where it could
3244 be from. Value is the buffer position or 0 if it couldn't be
3245 determined.
3246
3247 W's buffer must be current.
3248
3249 This function is necessary because we don't record buffer positions
3250 in glyphs generated from strings (to keep struct glyph small).
3251 This function may only use code that doesn't eval because it is
3252 called asynchronously from note_mouse_highlight. */
3253
3254 int
3255 string_buffer_position (w, string, around_charpos)
3256 struct window *w;
3257 Lisp_Object string;
3258 int around_charpos;
3259 {
3260 Lisp_Object limit, prop, pos;
3261 const int MAX_DISTANCE = 1000;
3262 int found = 0;
3263
3264 pos = make_number (around_charpos);
3265 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
3266 while (!found && !EQ (pos, limit))
3267 {
3268 prop = Fget_char_property (pos, Qdisplay, Qnil);
3269 if (!NILP (prop) && display_prop_string_p (prop, string))
3270 found = 1;
3271 else
3272 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
3273 }
3274
3275 if (!found)
3276 {
3277 pos = make_number (around_charpos);
3278 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
3279 while (!found && !EQ (pos, limit))
3280 {
3281 prop = Fget_char_property (pos, Qdisplay, Qnil);
3282 if (!NILP (prop) && display_prop_string_p (prop, string))
3283 found = 1;
3284 else
3285 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
3286 limit);
3287 }
3288 }
3289
3290 return found ? XINT (pos) : 0;
3291 }
3292
3293
3294 \f
3295 /***********************************************************************
3296 `composition' property
3297 ***********************************************************************/
3298
3299 /* Set up iterator IT from `composition' property at its current
3300 position. Called from handle_stop. */
3301
3302 static enum prop_handled
3303 handle_composition_prop (it)
3304 struct it *it;
3305 {
3306 Lisp_Object prop, string;
3307 int pos, pos_byte, end;
3308 enum prop_handled handled = HANDLED_NORMALLY;
3309
3310 if (STRINGP (it->string))
3311 {
3312 pos = IT_STRING_CHARPOS (*it);
3313 pos_byte = IT_STRING_BYTEPOS (*it);
3314 string = it->string;
3315 }
3316 else
3317 {
3318 pos = IT_CHARPOS (*it);
3319 pos_byte = IT_BYTEPOS (*it);
3320 string = Qnil;
3321 }
3322
3323 /* If there's a valid composition and point is not inside of the
3324 composition (in the case that the composition is from the current
3325 buffer), draw a glyph composed from the composition components. */
3326 if (find_composition (pos, -1, &pos, &end, &prop, string)
3327 && COMPOSITION_VALID_P (pos, end, prop)
3328 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
3329 {
3330 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
3331
3332 if (id >= 0)
3333 {
3334 it->method = next_element_from_composition;
3335 it->cmp_id = id;
3336 it->cmp_len = COMPOSITION_LENGTH (prop);
3337 /* For a terminal, draw only the first character of the
3338 components. */
3339 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
3340 it->len = (STRINGP (it->string)
3341 ? string_char_to_byte (it->string, end)
3342 : CHAR_TO_BYTE (end)) - pos_byte;
3343 it->stop_charpos = end;
3344 handled = HANDLED_RETURN;
3345 }
3346 }
3347
3348 return handled;
3349 }
3350
3351
3352 \f
3353 /***********************************************************************
3354 Overlay strings
3355 ***********************************************************************/
3356
3357 /* The following structure is used to record overlay strings for
3358 later sorting in load_overlay_strings. */
3359
3360 struct overlay_entry
3361 {
3362 Lisp_Object overlay;
3363 Lisp_Object string;
3364 int priority;
3365 int after_string_p;
3366 };
3367
3368
3369 /* Set up iterator IT from overlay strings at its current position.
3370 Called from handle_stop. */
3371
3372 static enum prop_handled
3373 handle_overlay_change (it)
3374 struct it *it;
3375 {
3376 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
3377 return HANDLED_RECOMPUTE_PROPS;
3378 else
3379 return HANDLED_NORMALLY;
3380 }
3381
3382
3383 /* Set up the next overlay string for delivery by IT, if there is an
3384 overlay string to deliver. Called by set_iterator_to_next when the
3385 end of the current overlay string is reached. If there are more
3386 overlay strings to display, IT->string and
3387 IT->current.overlay_string_index are set appropriately here.
3388 Otherwise IT->string is set to nil. */
3389
3390 static void
3391 next_overlay_string (it)
3392 struct it *it;
3393 {
3394 ++it->current.overlay_string_index;
3395 if (it->current.overlay_string_index == it->n_overlay_strings)
3396 {
3397 /* No more overlay strings. Restore IT's settings to what
3398 they were before overlay strings were processed, and
3399 continue to deliver from current_buffer. */
3400 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
3401
3402 pop_it (it);
3403 xassert (it->stop_charpos >= BEGV
3404 && it->stop_charpos <= it->end_charpos);
3405 it->string = Qnil;
3406 it->current.overlay_string_index = -1;
3407 SET_TEXT_POS (it->current.string_pos, -1, -1);
3408 it->n_overlay_strings = 0;
3409 it->method = next_element_from_buffer;
3410
3411 /* If we're at the end of the buffer, record that we have
3412 processed the overlay strings there already, so that
3413 next_element_from_buffer doesn't try it again. */
3414 if (IT_CHARPOS (*it) >= it->end_charpos)
3415 it->overlay_strings_at_end_processed_p = 1;
3416
3417 /* If we have to display `...' for invisible text, set
3418 the iterator up for that. */
3419 if (display_ellipsis_p)
3420 setup_for_ellipsis (it);
3421 }
3422 else
3423 {
3424 /* There are more overlay strings to process. If
3425 IT->current.overlay_string_index has advanced to a position
3426 where we must load IT->overlay_strings with more strings, do
3427 it. */
3428 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
3429
3430 if (it->current.overlay_string_index && i == 0)
3431 load_overlay_strings (it, 0);
3432
3433 /* Initialize IT to deliver display elements from the overlay
3434 string. */
3435 it->string = it->overlay_strings[i];
3436 it->multibyte_p = STRING_MULTIBYTE (it->string);
3437 SET_TEXT_POS (it->current.string_pos, 0, 0);
3438 it->method = next_element_from_string;
3439 it->stop_charpos = 0;
3440 }
3441
3442 CHECK_IT (it);
3443 }
3444
3445
3446 /* Compare two overlay_entry structures E1 and E2. Used as a
3447 comparison function for qsort in load_overlay_strings. Overlay
3448 strings for the same position are sorted so that
3449
3450 1. All after-strings come in front of before-strings, except
3451 when they come from the same overlay.
3452
3453 2. Within after-strings, strings are sorted so that overlay strings
3454 from overlays with higher priorities come first.
3455
3456 2. Within before-strings, strings are sorted so that overlay
3457 strings from overlays with higher priorities come last.
3458
3459 Value is analogous to strcmp. */
3460
3461
3462 static int
3463 compare_overlay_entries (e1, e2)
3464 void *e1, *e2;
3465 {
3466 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
3467 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
3468 int result;
3469
3470 if (entry1->after_string_p != entry2->after_string_p)
3471 {
3472 /* Let after-strings appear in front of before-strings if
3473 they come from different overlays. */
3474 if (EQ (entry1->overlay, entry2->overlay))
3475 result = entry1->after_string_p ? 1 : -1;
3476 else
3477 result = entry1->after_string_p ? -1 : 1;
3478 }
3479 else if (entry1->after_string_p)
3480 /* After-strings sorted in order of decreasing priority. */
3481 result = entry2->priority - entry1->priority;
3482 else
3483 /* Before-strings sorted in order of increasing priority. */
3484 result = entry1->priority - entry2->priority;
3485
3486 return result;
3487 }
3488
3489
3490 /* Load the vector IT->overlay_strings with overlay strings from IT's
3491 current buffer position, or from CHARPOS if that is > 0. Set
3492 IT->n_overlays to the total number of overlay strings found.
3493
3494 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
3495 a time. On entry into load_overlay_strings,
3496 IT->current.overlay_string_index gives the number of overlay
3497 strings that have already been loaded by previous calls to this
3498 function.
3499
3500 IT->add_overlay_start contains an additional overlay start
3501 position to consider for taking overlay strings from, if non-zero.
3502 This position comes into play when the overlay has an `invisible'
3503 property, and both before and after-strings. When we've skipped to
3504 the end of the overlay, because of its `invisible' property, we
3505 nevertheless want its before-string to appear.
3506 IT->add_overlay_start will contain the overlay start position
3507 in this case.
3508
3509 Overlay strings are sorted so that after-string strings come in
3510 front of before-string strings. Within before and after-strings,
3511 strings are sorted by overlay priority. See also function
3512 compare_overlay_entries. */
3513
3514 static void
3515 load_overlay_strings (it, charpos)
3516 struct it *it;
3517 int charpos;
3518 {
3519 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
3520 Lisp_Object ov, overlay, window, str, invisible;
3521 int start, end;
3522 int size = 20;
3523 int n = 0, i, j, invis_p;
3524 struct overlay_entry *entries
3525 = (struct overlay_entry *) alloca (size * sizeof *entries);
3526
3527 if (charpos <= 0)
3528 charpos = IT_CHARPOS (*it);
3529
3530 /* Append the overlay string STRING of overlay OVERLAY to vector
3531 `entries' which has size `size' and currently contains `n'
3532 elements. AFTER_P non-zero means STRING is an after-string of
3533 OVERLAY. */
3534 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
3535 do \
3536 { \
3537 Lisp_Object priority; \
3538 \
3539 if (n == size) \
3540 { \
3541 int new_size = 2 * size; \
3542 struct overlay_entry *old = entries; \
3543 entries = \
3544 (struct overlay_entry *) alloca (new_size \
3545 * sizeof *entries); \
3546 bcopy (old, entries, size * sizeof *entries); \
3547 size = new_size; \
3548 } \
3549 \
3550 entries[n].string = (STRING); \
3551 entries[n].overlay = (OVERLAY); \
3552 priority = Foverlay_get ((OVERLAY), Qpriority); \
3553 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
3554 entries[n].after_string_p = (AFTER_P); \
3555 ++n; \
3556 } \
3557 while (0)
3558
3559 /* Process overlay before the overlay center. */
3560 for (ov = current_buffer->overlays_before; CONSP (ov); ov = XCDR (ov))
3561 {
3562 overlay = XCAR (ov);
3563 xassert (OVERLAYP (overlay));
3564 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3565 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3566
3567 if (end < charpos)
3568 break;
3569
3570 /* Skip this overlay if it doesn't start or end at IT's current
3571 position. */
3572 if (end != charpos && start != charpos)
3573 continue;
3574
3575 /* Skip this overlay if it doesn't apply to IT->w. */
3576 window = Foverlay_get (overlay, Qwindow);
3577 if (WINDOWP (window) && XWINDOW (window) != it->w)
3578 continue;
3579
3580 /* If the text ``under'' the overlay is invisible, both before-
3581 and after-strings from this overlay are visible; start and
3582 end position are indistinguishable. */
3583 invisible = Foverlay_get (overlay, Qinvisible);
3584 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3585
3586 /* If overlay has a non-empty before-string, record it. */
3587 if ((start == charpos || (end == charpos && invis_p))
3588 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3589 && SCHARS (str))
3590 RECORD_OVERLAY_STRING (overlay, str, 0);
3591
3592 /* If overlay has a non-empty after-string, record it. */
3593 if ((end == charpos || (start == charpos && invis_p))
3594 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3595 && SCHARS (str))
3596 RECORD_OVERLAY_STRING (overlay, str, 1);
3597 }
3598
3599 /* Process overlays after the overlay center. */
3600 for (ov = current_buffer->overlays_after; CONSP (ov); ov = XCDR (ov))
3601 {
3602 overlay = XCAR (ov);
3603 xassert (OVERLAYP (overlay));
3604 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3605 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3606
3607 if (start > charpos)
3608 break;
3609
3610 /* Skip this overlay if it doesn't start or end at IT's current
3611 position. */
3612 if (end != charpos && start != charpos)
3613 continue;
3614
3615 /* Skip this overlay if it doesn't apply to IT->w. */
3616 window = Foverlay_get (overlay, Qwindow);
3617 if (WINDOWP (window) && XWINDOW (window) != it->w)
3618 continue;
3619
3620 /* If the text ``under'' the overlay is invisible, it has a zero
3621 dimension, and both before- and after-strings apply. */
3622 invisible = Foverlay_get (overlay, Qinvisible);
3623 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3624
3625 /* If overlay has a non-empty before-string, record it. */
3626 if ((start == charpos || (end == charpos && invis_p))
3627 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3628 && SCHARS (str))
3629 RECORD_OVERLAY_STRING (overlay, str, 0);
3630
3631 /* If overlay has a non-empty after-string, record it. */
3632 if ((end == charpos || (start == charpos && invis_p))
3633 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3634 && SCHARS (str))
3635 RECORD_OVERLAY_STRING (overlay, str, 1);
3636 }
3637
3638 #undef RECORD_OVERLAY_STRING
3639
3640 /* Sort entries. */
3641 if (n > 1)
3642 qsort (entries, n, sizeof *entries, compare_overlay_entries);
3643
3644 /* Record the total number of strings to process. */
3645 it->n_overlay_strings = n;
3646
3647 /* IT->current.overlay_string_index is the number of overlay strings
3648 that have already been consumed by IT. Copy some of the
3649 remaining overlay strings to IT->overlay_strings. */
3650 i = 0;
3651 j = it->current.overlay_string_index;
3652 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
3653 it->overlay_strings[i++] = entries[j++].string;
3654
3655 CHECK_IT (it);
3656 }
3657
3658
3659 /* Get the first chunk of overlay strings at IT's current buffer
3660 position, or at CHARPOS if that is > 0. Value is non-zero if at
3661 least one overlay string was found. */
3662
3663 static int
3664 get_overlay_strings (it, charpos)
3665 struct it *it;
3666 int charpos;
3667 {
3668 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
3669 process. This fills IT->overlay_strings with strings, and sets
3670 IT->n_overlay_strings to the total number of strings to process.
3671 IT->pos.overlay_string_index has to be set temporarily to zero
3672 because load_overlay_strings needs this; it must be set to -1
3673 when no overlay strings are found because a zero value would
3674 indicate a position in the first overlay string. */
3675 it->current.overlay_string_index = 0;
3676 load_overlay_strings (it, charpos);
3677
3678 /* If we found overlay strings, set up IT to deliver display
3679 elements from the first one. Otherwise set up IT to deliver
3680 from current_buffer. */
3681 if (it->n_overlay_strings)
3682 {
3683 /* Make sure we know settings in current_buffer, so that we can
3684 restore meaningful values when we're done with the overlay
3685 strings. */
3686 compute_stop_pos (it);
3687 xassert (it->face_id >= 0);
3688
3689 /* Save IT's settings. They are restored after all overlay
3690 strings have been processed. */
3691 xassert (it->sp == 0);
3692 push_it (it);
3693
3694 /* Set up IT to deliver display elements from the first overlay
3695 string. */
3696 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3697 it->string = it->overlay_strings[0];
3698 it->stop_charpos = 0;
3699 xassert (STRINGP (it->string));
3700 it->end_charpos = SCHARS (it->string);
3701 it->multibyte_p = STRING_MULTIBYTE (it->string);
3702 it->method = next_element_from_string;
3703 }
3704 else
3705 {
3706 it->string = Qnil;
3707 it->current.overlay_string_index = -1;
3708 it->method = next_element_from_buffer;
3709 }
3710
3711 CHECK_IT (it);
3712
3713 /* Value is non-zero if we found at least one overlay string. */
3714 return STRINGP (it->string);
3715 }
3716
3717
3718 \f
3719 /***********************************************************************
3720 Saving and restoring state
3721 ***********************************************************************/
3722
3723 /* Save current settings of IT on IT->stack. Called, for example,
3724 before setting up IT for an overlay string, to be able to restore
3725 IT's settings to what they were after the overlay string has been
3726 processed. */
3727
3728 static void
3729 push_it (it)
3730 struct it *it;
3731 {
3732 struct iterator_stack_entry *p;
3733
3734 xassert (it->sp < 2);
3735 p = it->stack + it->sp;
3736
3737 p->stop_charpos = it->stop_charpos;
3738 xassert (it->face_id >= 0);
3739 p->face_id = it->face_id;
3740 p->string = it->string;
3741 p->pos = it->current;
3742 p->end_charpos = it->end_charpos;
3743 p->string_nchars = it->string_nchars;
3744 p->area = it->area;
3745 p->multibyte_p = it->multibyte_p;
3746 p->space_width = it->space_width;
3747 p->font_height = it->font_height;
3748 p->voffset = it->voffset;
3749 p->string_from_display_prop_p = it->string_from_display_prop_p;
3750 p->display_ellipsis_p = 0;
3751 ++it->sp;
3752 }
3753
3754
3755 /* Restore IT's settings from IT->stack. Called, for example, when no
3756 more overlay strings must be processed, and we return to delivering
3757 display elements from a buffer, or when the end of a string from a
3758 `display' property is reached and we return to delivering display
3759 elements from an overlay string, or from a buffer. */
3760
3761 static void
3762 pop_it (it)
3763 struct it *it;
3764 {
3765 struct iterator_stack_entry *p;
3766
3767 xassert (it->sp > 0);
3768 --it->sp;
3769 p = it->stack + it->sp;
3770 it->stop_charpos = p->stop_charpos;
3771 it->face_id = p->face_id;
3772 it->string = p->string;
3773 it->current = p->pos;
3774 it->end_charpos = p->end_charpos;
3775 it->string_nchars = p->string_nchars;
3776 it->area = p->area;
3777 it->multibyte_p = p->multibyte_p;
3778 it->space_width = p->space_width;
3779 it->font_height = p->font_height;
3780 it->voffset = p->voffset;
3781 it->string_from_display_prop_p = p->string_from_display_prop_p;
3782 }
3783
3784
3785 \f
3786 /***********************************************************************
3787 Moving over lines
3788 ***********************************************************************/
3789
3790 /* Set IT's current position to the previous line start. */
3791
3792 static void
3793 back_to_previous_line_start (it)
3794 struct it *it;
3795 {
3796 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
3797 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3798 }
3799
3800
3801 /* Move IT to the next line start.
3802
3803 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
3804 we skipped over part of the text (as opposed to moving the iterator
3805 continuously over the text). Otherwise, don't change the value
3806 of *SKIPPED_P.
3807
3808 Newlines may come from buffer text, overlay strings, or strings
3809 displayed via the `display' property. That's the reason we can't
3810 simply use find_next_newline_no_quit.
3811
3812 Note that this function may not skip over invisible text that is so
3813 because of text properties and immediately follows a newline. If
3814 it would, function reseat_at_next_visible_line_start, when called
3815 from set_iterator_to_next, would effectively make invisible
3816 characters following a newline part of the wrong glyph row, which
3817 leads to wrong cursor motion. */
3818
3819 static int
3820 forward_to_next_line_start (it, skipped_p)
3821 struct it *it;
3822 int *skipped_p;
3823 {
3824 int old_selective, newline_found_p, n;
3825 const int MAX_NEWLINE_DISTANCE = 500;
3826
3827 /* If already on a newline, just consume it to avoid unintended
3828 skipping over invisible text below. */
3829 if (it->what == IT_CHARACTER
3830 && it->c == '\n'
3831 && CHARPOS (it->position) == IT_CHARPOS (*it))
3832 {
3833 set_iterator_to_next (it, 0);
3834 it->c = 0;
3835 return 1;
3836 }
3837
3838 /* Don't handle selective display in the following. It's (a)
3839 unnecessary because it's done by the caller, and (b) leads to an
3840 infinite recursion because next_element_from_ellipsis indirectly
3841 calls this function. */
3842 old_selective = it->selective;
3843 it->selective = 0;
3844
3845 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
3846 from buffer text. */
3847 for (n = newline_found_p = 0;
3848 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
3849 n += STRINGP (it->string) ? 0 : 1)
3850 {
3851 if (!get_next_display_element (it))
3852 break;
3853 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
3854 set_iterator_to_next (it, 0);
3855 }
3856
3857 /* If we didn't find a newline near enough, see if we can use a
3858 short-cut. */
3859 if (n == MAX_NEWLINE_DISTANCE)
3860 {
3861 int start = IT_CHARPOS (*it);
3862 int limit = find_next_newline_no_quit (start, 1);
3863 Lisp_Object pos;
3864
3865 xassert (!STRINGP (it->string));
3866
3867 /* If there isn't any `display' property in sight, and no
3868 overlays, we can just use the position of the newline in
3869 buffer text. */
3870 if (it->stop_charpos >= limit
3871 || ((pos = Fnext_single_property_change (make_number (start),
3872 Qdisplay,
3873 Qnil, make_number (limit)),
3874 NILP (pos))
3875 && next_overlay_change (start) == ZV))
3876 {
3877 IT_CHARPOS (*it) = limit;
3878 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
3879 *skipped_p = newline_found_p = 1;
3880 }
3881 else
3882 {
3883 while (get_next_display_element (it)
3884 && !newline_found_p)
3885 {
3886 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
3887 set_iterator_to_next (it, 0);
3888 }
3889 }
3890 }
3891
3892 it->selective = old_selective;
3893 return newline_found_p;
3894 }
3895
3896
3897 /* Set IT's current position to the previous visible line start. Skip
3898 invisible text that is so either due to text properties or due to
3899 selective display. Caution: this does not change IT->current_x and
3900 IT->hpos. */
3901
3902 static void
3903 back_to_previous_visible_line_start (it)
3904 struct it *it;
3905 {
3906 int visible_p = 0;
3907
3908 /* Go back one newline if not on BEGV already. */
3909 if (IT_CHARPOS (*it) > BEGV)
3910 back_to_previous_line_start (it);
3911
3912 /* Move over lines that are invisible because of selective display
3913 or text properties. */
3914 while (IT_CHARPOS (*it) > BEGV
3915 && !visible_p)
3916 {
3917 visible_p = 1;
3918
3919 /* If selective > 0, then lines indented more than that values
3920 are invisible. */
3921 if (it->selective > 0
3922 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3923 (double) it->selective)) /* iftc */
3924 visible_p = 0;
3925 else
3926 {
3927 Lisp_Object prop;
3928
3929 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
3930 Qinvisible, it->window);
3931 if (TEXT_PROP_MEANS_INVISIBLE (prop))
3932 visible_p = 0;
3933 }
3934
3935 /* Back one more newline if the current one is invisible. */
3936 if (!visible_p)
3937 back_to_previous_line_start (it);
3938 }
3939
3940 xassert (IT_CHARPOS (*it) >= BEGV);
3941 xassert (IT_CHARPOS (*it) == BEGV
3942 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3943 CHECK_IT (it);
3944 }
3945
3946
3947 /* Reseat iterator IT at the previous visible line start. Skip
3948 invisible text that is so either due to text properties or due to
3949 selective display. At the end, update IT's overlay information,
3950 face information etc. */
3951
3952 static void
3953 reseat_at_previous_visible_line_start (it)
3954 struct it *it;
3955 {
3956 back_to_previous_visible_line_start (it);
3957 reseat (it, it->current.pos, 1);
3958 CHECK_IT (it);
3959 }
3960
3961
3962 /* Reseat iterator IT on the next visible line start in the current
3963 buffer. ON_NEWLINE_P non-zero means position IT on the newline
3964 preceding the line start. Skip over invisible text that is so
3965 because of selective display. Compute faces, overlays etc at the
3966 new position. Note that this function does not skip over text that
3967 is invisible because of text properties. */
3968
3969 static void
3970 reseat_at_next_visible_line_start (it, on_newline_p)
3971 struct it *it;
3972 int on_newline_p;
3973 {
3974 int newline_found_p, skipped_p = 0;
3975
3976 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3977
3978 /* Skip over lines that are invisible because they are indented
3979 more than the value of IT->selective. */
3980 if (it->selective > 0)
3981 while (IT_CHARPOS (*it) < ZV
3982 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3983 (double) it->selective)) /* iftc */
3984 {
3985 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3986 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3987 }
3988
3989 /* Position on the newline if that's what's requested. */
3990 if (on_newline_p && newline_found_p)
3991 {
3992 if (STRINGP (it->string))
3993 {
3994 if (IT_STRING_CHARPOS (*it) > 0)
3995 {
3996 --IT_STRING_CHARPOS (*it);
3997 --IT_STRING_BYTEPOS (*it);
3998 }
3999 }
4000 else if (IT_CHARPOS (*it) > BEGV)
4001 {
4002 --IT_CHARPOS (*it);
4003 --IT_BYTEPOS (*it);
4004 reseat (it, it->current.pos, 0);
4005 }
4006 }
4007 else if (skipped_p)
4008 reseat (it, it->current.pos, 0);
4009
4010 CHECK_IT (it);
4011 }
4012
4013
4014 \f
4015 /***********************************************************************
4016 Changing an iterator's position
4017 ***********************************************************************/
4018
4019 /* Change IT's current position to POS in current_buffer. If FORCE_P
4020 is non-zero, always check for text properties at the new position.
4021 Otherwise, text properties are only looked up if POS >=
4022 IT->check_charpos of a property. */
4023
4024 static void
4025 reseat (it, pos, force_p)
4026 struct it *it;
4027 struct text_pos pos;
4028 int force_p;
4029 {
4030 int original_pos = IT_CHARPOS (*it);
4031
4032 reseat_1 (it, pos, 0);
4033
4034 /* Determine where to check text properties. Avoid doing it
4035 where possible because text property lookup is very expensive. */
4036 if (force_p
4037 || CHARPOS (pos) > it->stop_charpos
4038 || CHARPOS (pos) < original_pos)
4039 handle_stop (it);
4040
4041 CHECK_IT (it);
4042 }
4043
4044
4045 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4046 IT->stop_pos to POS, also. */
4047
4048 static void
4049 reseat_1 (it, pos, set_stop_p)
4050 struct it *it;
4051 struct text_pos pos;
4052 int set_stop_p;
4053 {
4054 /* Don't call this function when scanning a C string. */
4055 xassert (it->s == NULL);
4056
4057 /* POS must be a reasonable value. */
4058 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4059
4060 it->current.pos = it->position = pos;
4061 XSETBUFFER (it->object, current_buffer);
4062 it->end_charpos = ZV;
4063 it->dpvec = NULL;
4064 it->current.dpvec_index = -1;
4065 it->current.overlay_string_index = -1;
4066 IT_STRING_CHARPOS (*it) = -1;
4067 IT_STRING_BYTEPOS (*it) = -1;
4068 it->string = Qnil;
4069 it->method = next_element_from_buffer;
4070 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
4071 it->sp = 0;
4072 it->face_before_selective_p = 0;
4073
4074 if (set_stop_p)
4075 it->stop_charpos = CHARPOS (pos);
4076 }
4077
4078
4079 /* Set up IT for displaying a string, starting at CHARPOS in window W.
4080 If S is non-null, it is a C string to iterate over. Otherwise,
4081 STRING gives a Lisp string to iterate over.
4082
4083 If PRECISION > 0, don't return more then PRECISION number of
4084 characters from the string.
4085
4086 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4087 characters have been returned. FIELD_WIDTH < 0 means an infinite
4088 field width.
4089
4090 MULTIBYTE = 0 means disable processing of multibyte characters,
4091 MULTIBYTE > 0 means enable it,
4092 MULTIBYTE < 0 means use IT->multibyte_p.
4093
4094 IT must be initialized via a prior call to init_iterator before
4095 calling this function. */
4096
4097 static void
4098 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4099 struct it *it;
4100 unsigned char *s;
4101 Lisp_Object string;
4102 int charpos;
4103 int precision, field_width, multibyte;
4104 {
4105 /* No region in strings. */
4106 it->region_beg_charpos = it->region_end_charpos = -1;
4107
4108 /* No text property checks performed by default, but see below. */
4109 it->stop_charpos = -1;
4110
4111 /* Set iterator position and end position. */
4112 bzero (&it->current, sizeof it->current);
4113 it->current.overlay_string_index = -1;
4114 it->current.dpvec_index = -1;
4115 xassert (charpos >= 0);
4116
4117 /* If STRING is specified, use its multibyteness, otherwise use the
4118 setting of MULTIBYTE, if specified. */
4119 if (multibyte >= 0)
4120 it->multibyte_p = multibyte > 0;
4121
4122 if (s == NULL)
4123 {
4124 xassert (STRINGP (string));
4125 it->string = string;
4126 it->s = NULL;
4127 it->end_charpos = it->string_nchars = SCHARS (string);
4128 it->method = next_element_from_string;
4129 it->current.string_pos = string_pos (charpos, string);
4130 }
4131 else
4132 {
4133 it->s = s;
4134 it->string = Qnil;
4135
4136 /* Note that we use IT->current.pos, not it->current.string_pos,
4137 for displaying C strings. */
4138 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4139 if (it->multibyte_p)
4140 {
4141 it->current.pos = c_string_pos (charpos, s, 1);
4142 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4143 }
4144 else
4145 {
4146 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4147 it->end_charpos = it->string_nchars = strlen (s);
4148 }
4149
4150 it->method = next_element_from_c_string;
4151 }
4152
4153 /* PRECISION > 0 means don't return more than PRECISION characters
4154 from the string. */
4155 if (precision > 0 && it->end_charpos - charpos > precision)
4156 it->end_charpos = it->string_nchars = charpos + precision;
4157
4158 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4159 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4160 FIELD_WIDTH < 0 means infinite field width. This is useful for
4161 padding with `-' at the end of a mode line. */
4162 if (field_width < 0)
4163 field_width = INFINITY;
4164 if (field_width > it->end_charpos - charpos)
4165 it->end_charpos = charpos + field_width;
4166
4167 /* Use the standard display table for displaying strings. */
4168 if (DISP_TABLE_P (Vstandard_display_table))
4169 it->dp = XCHAR_TABLE (Vstandard_display_table);
4170
4171 it->stop_charpos = charpos;
4172 CHECK_IT (it);
4173 }
4174
4175
4176 \f
4177 /***********************************************************************
4178 Iteration
4179 ***********************************************************************/
4180
4181 /* Load IT's display element fields with information about the next
4182 display element from the current position of IT. Value is zero if
4183 end of buffer (or C string) is reached. */
4184
4185 int
4186 get_next_display_element (it)
4187 struct it *it;
4188 {
4189 /* Non-zero means that we found an display element. Zero means that
4190 we hit the end of what we iterate over. Performance note: the
4191 function pointer `method' used here turns out to be faster than
4192 using a sequence of if-statements. */
4193 int success_p = (*it->method) (it);
4194
4195 if (it->what == IT_CHARACTER)
4196 {
4197 /* Map via display table or translate control characters.
4198 IT->c, IT->len etc. have been set to the next character by
4199 the function call above. If we have a display table, and it
4200 contains an entry for IT->c, translate it. Don't do this if
4201 IT->c itself comes from a display table, otherwise we could
4202 end up in an infinite recursion. (An alternative could be to
4203 count the recursion depth of this function and signal an
4204 error when a certain maximum depth is reached.) Is it worth
4205 it? */
4206 if (success_p && it->dpvec == NULL)
4207 {
4208 Lisp_Object dv;
4209
4210 if (it->dp
4211 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
4212 VECTORP (dv)))
4213 {
4214 struct Lisp_Vector *v = XVECTOR (dv);
4215
4216 /* Return the first character from the display table
4217 entry, if not empty. If empty, don't display the
4218 current character. */
4219 if (v->size)
4220 {
4221 it->dpvec_char_len = it->len;
4222 it->dpvec = v->contents;
4223 it->dpend = v->contents + v->size;
4224 it->current.dpvec_index = 0;
4225 it->method = next_element_from_display_vector;
4226 success_p = get_next_display_element (it);
4227 }
4228 else
4229 {
4230 set_iterator_to_next (it, 0);
4231 success_p = get_next_display_element (it);
4232 }
4233 }
4234
4235 /* Translate control characters into `\003' or `^C' form.
4236 Control characters coming from a display table entry are
4237 currently not translated because we use IT->dpvec to hold
4238 the translation. This could easily be changed but I
4239 don't believe that it is worth doing.
4240
4241 Non-printable multibyte characters are also translated
4242 octal form. */
4243 else if ((it->c < ' '
4244 && (it->area != TEXT_AREA
4245 || (it->c != '\n' && it->c != '\t')))
4246 || (it->c >= 127
4247 && it->len == 1)
4248 || !CHAR_PRINTABLE_P (it->c))
4249 {
4250 /* IT->c is a control character which must be displayed
4251 either as '\003' or as `^C' where the '\\' and '^'
4252 can be defined in the display table. Fill
4253 IT->ctl_chars with glyphs for what we have to
4254 display. Then, set IT->dpvec to these glyphs. */
4255 GLYPH g;
4256
4257 if (it->c < 128 && it->ctl_arrow_p)
4258 {
4259 /* Set IT->ctl_chars[0] to the glyph for `^'. */
4260 if (it->dp
4261 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
4262 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
4263 g = XINT (DISP_CTRL_GLYPH (it->dp));
4264 else
4265 g = FAST_MAKE_GLYPH ('^', 0);
4266 XSETINT (it->ctl_chars[0], g);
4267
4268 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
4269 XSETINT (it->ctl_chars[1], g);
4270
4271 /* Set up IT->dpvec and return first character from it. */
4272 it->dpvec_char_len = it->len;
4273 it->dpvec = it->ctl_chars;
4274 it->dpend = it->dpvec + 2;
4275 it->current.dpvec_index = 0;
4276 it->method = next_element_from_display_vector;
4277 get_next_display_element (it);
4278 }
4279 else
4280 {
4281 unsigned char str[MAX_MULTIBYTE_LENGTH];
4282 int len;
4283 int i;
4284 GLYPH escape_glyph;
4285
4286 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
4287 if (it->dp
4288 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
4289 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
4290 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
4291 else
4292 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
4293
4294 if (SINGLE_BYTE_CHAR_P (it->c))
4295 str[0] = it->c, len = 1;
4296 else
4297 {
4298 len = CHAR_STRING_NO_SIGNAL (it->c, str);
4299 if (len < 0)
4300 {
4301 /* It's an invalid character, which
4302 shouldn't happen actually, but due to
4303 bugs it may happen. Let's print the char
4304 as is, there's not much meaningful we can
4305 do with it. */
4306 str[0] = it->c;
4307 str[1] = it->c >> 8;
4308 str[2] = it->c >> 16;
4309 str[3] = it->c >> 24;
4310 len = 4;
4311 }
4312 }
4313
4314 for (i = 0; i < len; i++)
4315 {
4316 XSETINT (it->ctl_chars[i * 4], escape_glyph);
4317 /* Insert three more glyphs into IT->ctl_chars for
4318 the octal display of the character. */
4319 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
4320 XSETINT (it->ctl_chars[i * 4 + 1], g);
4321 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
4322 XSETINT (it->ctl_chars[i * 4 + 2], g);
4323 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
4324 XSETINT (it->ctl_chars[i * 4 + 3], g);
4325 }
4326
4327 /* Set up IT->dpvec and return the first character
4328 from it. */
4329 it->dpvec_char_len = it->len;
4330 it->dpvec = it->ctl_chars;
4331 it->dpend = it->dpvec + len * 4;
4332 it->current.dpvec_index = 0;
4333 it->method = next_element_from_display_vector;
4334 get_next_display_element (it);
4335 }
4336 }
4337 }
4338
4339 /* Adjust face id for a multibyte character. There are no
4340 multibyte character in unibyte text. */
4341 if (it->multibyte_p
4342 && success_p
4343 && FRAME_WINDOW_P (it->f))
4344 {
4345 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4346 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
4347 }
4348 }
4349
4350 /* Is this character the last one of a run of characters with
4351 box? If yes, set IT->end_of_box_run_p to 1. */
4352 if (it->face_box_p
4353 && it->s == NULL)
4354 {
4355 int face_id;
4356 struct face *face;
4357
4358 it->end_of_box_run_p
4359 = ((face_id = face_after_it_pos (it),
4360 face_id != it->face_id)
4361 && (face = FACE_FROM_ID (it->f, face_id),
4362 face->box == FACE_NO_BOX));
4363 }
4364
4365 /* Value is 0 if end of buffer or string reached. */
4366 return success_p;
4367 }
4368
4369
4370 /* Move IT to the next display element.
4371
4372 RESEAT_P non-zero means if called on a newline in buffer text,
4373 skip to the next visible line start.
4374
4375 Functions get_next_display_element and set_iterator_to_next are
4376 separate because I find this arrangement easier to handle than a
4377 get_next_display_element function that also increments IT's
4378 position. The way it is we can first look at an iterator's current
4379 display element, decide whether it fits on a line, and if it does,
4380 increment the iterator position. The other way around we probably
4381 would either need a flag indicating whether the iterator has to be
4382 incremented the next time, or we would have to implement a
4383 decrement position function which would not be easy to write. */
4384
4385 void
4386 set_iterator_to_next (it, reseat_p)
4387 struct it *it;
4388 int reseat_p;
4389 {
4390 /* Reset flags indicating start and end of a sequence of characters
4391 with box. Reset them at the start of this function because
4392 moving the iterator to a new position might set them. */
4393 it->start_of_box_run_p = it->end_of_box_run_p = 0;
4394
4395 if (it->method == next_element_from_buffer)
4396 {
4397 /* The current display element of IT is a character from
4398 current_buffer. Advance in the buffer, and maybe skip over
4399 invisible lines that are so because of selective display. */
4400 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
4401 reseat_at_next_visible_line_start (it, 0);
4402 else
4403 {
4404 xassert (it->len != 0);
4405 IT_BYTEPOS (*it) += it->len;
4406 IT_CHARPOS (*it) += 1;
4407 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
4408 }
4409 }
4410 else if (it->method == next_element_from_composition)
4411 {
4412 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
4413 if (STRINGP (it->string))
4414 {
4415 IT_STRING_BYTEPOS (*it) += it->len;
4416 IT_STRING_CHARPOS (*it) += it->cmp_len;
4417 it->method = next_element_from_string;
4418 goto consider_string_end;
4419 }
4420 else
4421 {
4422 IT_BYTEPOS (*it) += it->len;
4423 IT_CHARPOS (*it) += it->cmp_len;
4424 it->method = next_element_from_buffer;
4425 }
4426 }
4427 else if (it->method == next_element_from_c_string)
4428 {
4429 /* Current display element of IT is from a C string. */
4430 IT_BYTEPOS (*it) += it->len;
4431 IT_CHARPOS (*it) += 1;
4432 }
4433 else if (it->method == next_element_from_display_vector)
4434 {
4435 /* Current display element of IT is from a display table entry.
4436 Advance in the display table definition. Reset it to null if
4437 end reached, and continue with characters from buffers/
4438 strings. */
4439 ++it->current.dpvec_index;
4440
4441 /* Restore face of the iterator to what they were before the
4442 display vector entry (these entries may contain faces). */
4443 it->face_id = it->saved_face_id;
4444
4445 if (it->dpvec + it->current.dpvec_index == it->dpend)
4446 {
4447 if (it->s)
4448 it->method = next_element_from_c_string;
4449 else if (STRINGP (it->string))
4450 it->method = next_element_from_string;
4451 else
4452 it->method = next_element_from_buffer;
4453
4454 it->dpvec = NULL;
4455 it->current.dpvec_index = -1;
4456
4457 /* Skip over characters which were displayed via IT->dpvec. */
4458 if (it->dpvec_char_len < 0)
4459 reseat_at_next_visible_line_start (it, 1);
4460 else if (it->dpvec_char_len > 0)
4461 {
4462 it->len = it->dpvec_char_len;
4463 set_iterator_to_next (it, reseat_p);
4464 }
4465 }
4466 }
4467 else if (it->method == next_element_from_string)
4468 {
4469 /* Current display element is a character from a Lisp string. */
4470 xassert (it->s == NULL && STRINGP (it->string));
4471 IT_STRING_BYTEPOS (*it) += it->len;
4472 IT_STRING_CHARPOS (*it) += 1;
4473
4474 consider_string_end:
4475
4476 if (it->current.overlay_string_index >= 0)
4477 {
4478 /* IT->string is an overlay string. Advance to the
4479 next, if there is one. */
4480 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
4481 next_overlay_string (it);
4482 }
4483 else
4484 {
4485 /* IT->string is not an overlay string. If we reached
4486 its end, and there is something on IT->stack, proceed
4487 with what is on the stack. This can be either another
4488 string, this time an overlay string, or a buffer. */
4489 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
4490 && it->sp > 0)
4491 {
4492 pop_it (it);
4493 if (!STRINGP (it->string))
4494 it->method = next_element_from_buffer;
4495 else
4496 goto consider_string_end;
4497 }
4498 }
4499 }
4500 else if (it->method == next_element_from_image
4501 || it->method == next_element_from_stretch)
4502 {
4503 /* The position etc with which we have to proceed are on
4504 the stack. The position may be at the end of a string,
4505 if the `display' property takes up the whole string. */
4506 pop_it (it);
4507 it->image_id = 0;
4508 if (STRINGP (it->string))
4509 {
4510 it->method = next_element_from_string;
4511 goto consider_string_end;
4512 }
4513 else
4514 it->method = next_element_from_buffer;
4515 }
4516 else
4517 /* There are no other methods defined, so this should be a bug. */
4518 abort ();
4519
4520 xassert (it->method != next_element_from_string
4521 || (STRINGP (it->string)
4522 && IT_STRING_CHARPOS (*it) >= 0));
4523 }
4524
4525
4526 /* Load IT's display element fields with information about the next
4527 display element which comes from a display table entry or from the
4528 result of translating a control character to one of the forms `^C'
4529 or `\003'. IT->dpvec holds the glyphs to return as characters. */
4530
4531 static int
4532 next_element_from_display_vector (it)
4533 struct it *it;
4534 {
4535 /* Precondition. */
4536 xassert (it->dpvec && it->current.dpvec_index >= 0);
4537
4538 /* Remember the current face id in case glyphs specify faces.
4539 IT's face is restored in set_iterator_to_next. */
4540 it->saved_face_id = it->face_id;
4541
4542 if (INTEGERP (*it->dpvec)
4543 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
4544 {
4545 int lface_id;
4546 GLYPH g;
4547
4548 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
4549 it->c = FAST_GLYPH_CHAR (g);
4550 it->len = CHAR_BYTES (it->c);
4551
4552 /* The entry may contain a face id to use. Such a face id is
4553 the id of a Lisp face, not a realized face. A face id of
4554 zero means no face is specified. */
4555 lface_id = FAST_GLYPH_FACE (g);
4556 if (lface_id)
4557 {
4558 /* The function returns -1 if lface_id is invalid. */
4559 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
4560 if (face_id >= 0)
4561 it->face_id = face_id;
4562 }
4563 }
4564 else
4565 /* Display table entry is invalid. Return a space. */
4566 it->c = ' ', it->len = 1;
4567
4568 /* Don't change position and object of the iterator here. They are
4569 still the values of the character that had this display table
4570 entry or was translated, and that's what we want. */
4571 it->what = IT_CHARACTER;
4572 return 1;
4573 }
4574
4575
4576 /* Load IT with the next display element from Lisp string IT->string.
4577 IT->current.string_pos is the current position within the string.
4578 If IT->current.overlay_string_index >= 0, the Lisp string is an
4579 overlay string. */
4580
4581 static int
4582 next_element_from_string (it)
4583 struct it *it;
4584 {
4585 struct text_pos position;
4586
4587 xassert (STRINGP (it->string));
4588 xassert (IT_STRING_CHARPOS (*it) >= 0);
4589 position = it->current.string_pos;
4590
4591 /* Time to check for invisible text? */
4592 if (IT_STRING_CHARPOS (*it) < it->end_charpos
4593 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
4594 {
4595 handle_stop (it);
4596
4597 /* Since a handler may have changed IT->method, we must
4598 recurse here. */
4599 return get_next_display_element (it);
4600 }
4601
4602 if (it->current.overlay_string_index >= 0)
4603 {
4604 /* Get the next character from an overlay string. In overlay
4605 strings, There is no field width or padding with spaces to
4606 do. */
4607 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
4608 {
4609 it->what = IT_EOB;
4610 return 0;
4611 }
4612 else if (STRING_MULTIBYTE (it->string))
4613 {
4614 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
4615 unsigned char *s = SDATA (it->string) + IT_STRING_BYTEPOS (*it);
4616 it->c = string_char_and_length (s, remaining, &it->len);
4617 }
4618 else
4619 {
4620 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
4621 it->len = 1;
4622 }
4623 }
4624 else
4625 {
4626 /* Get the next character from a Lisp string that is not an
4627 overlay string. Such strings come from the mode line, for
4628 example. We may have to pad with spaces, or truncate the
4629 string. See also next_element_from_c_string. */
4630 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
4631 {
4632 it->what = IT_EOB;
4633 return 0;
4634 }
4635 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
4636 {
4637 /* Pad with spaces. */
4638 it->c = ' ', it->len = 1;
4639 CHARPOS (position) = BYTEPOS (position) = -1;
4640 }
4641 else if (STRING_MULTIBYTE (it->string))
4642 {
4643 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
4644 unsigned char *s = SDATA (it->string) + IT_STRING_BYTEPOS (*it);
4645 it->c = string_char_and_length (s, maxlen, &it->len);
4646 }
4647 else
4648 {
4649 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
4650 it->len = 1;
4651 }
4652 }
4653
4654 /* Record what we have and where it came from. Note that we store a
4655 buffer position in IT->position although it could arguably be a
4656 string position. */
4657 it->what = IT_CHARACTER;
4658 it->object = it->string;
4659 it->position = position;
4660 return 1;
4661 }
4662
4663
4664 /* Load IT with next display element from C string IT->s.
4665 IT->string_nchars is the maximum number of characters to return
4666 from the string. IT->end_charpos may be greater than
4667 IT->string_nchars when this function is called, in which case we
4668 may have to return padding spaces. Value is zero if end of string
4669 reached, including padding spaces. */
4670
4671 static int
4672 next_element_from_c_string (it)
4673 struct it *it;
4674 {
4675 int success_p = 1;
4676
4677 xassert (it->s);
4678 it->what = IT_CHARACTER;
4679 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
4680 it->object = Qnil;
4681
4682 /* IT's position can be greater IT->string_nchars in case a field
4683 width or precision has been specified when the iterator was
4684 initialized. */
4685 if (IT_CHARPOS (*it) >= it->end_charpos)
4686 {
4687 /* End of the game. */
4688 it->what = IT_EOB;
4689 success_p = 0;
4690 }
4691 else if (IT_CHARPOS (*it) >= it->string_nchars)
4692 {
4693 /* Pad with spaces. */
4694 it->c = ' ', it->len = 1;
4695 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
4696 }
4697 else if (it->multibyte_p)
4698 {
4699 /* Implementation note: The calls to strlen apparently aren't a
4700 performance problem because there is no noticeable performance
4701 difference between Emacs running in unibyte or multibyte mode. */
4702 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
4703 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
4704 maxlen, &it->len);
4705 }
4706 else
4707 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
4708
4709 return success_p;
4710 }
4711
4712
4713 /* Set up IT to return characters from an ellipsis, if appropriate.
4714 The definition of the ellipsis glyphs may come from a display table
4715 entry. This function Fills IT with the first glyph from the
4716 ellipsis if an ellipsis is to be displayed. */
4717
4718 static int
4719 next_element_from_ellipsis (it)
4720 struct it *it;
4721 {
4722 if (it->selective_display_ellipsis_p)
4723 {
4724 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4725 {
4726 /* Use the display table definition for `...'. Invalid glyphs
4727 will be handled by the method returning elements from dpvec. */
4728 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4729 it->dpvec_char_len = it->len;
4730 it->dpvec = v->contents;
4731 it->dpend = v->contents + v->size;
4732 it->current.dpvec_index = 0;
4733 it->method = next_element_from_display_vector;
4734 }
4735 else
4736 {
4737 /* Use default `...' which is stored in default_invis_vector. */
4738 it->dpvec_char_len = it->len;
4739 it->dpvec = default_invis_vector;
4740 it->dpend = default_invis_vector + 3;
4741 it->current.dpvec_index = 0;
4742 it->method = next_element_from_display_vector;
4743 }
4744 }
4745 else
4746 {
4747 /* The face at the current position may be different from the
4748 face we find after the invisible text. Remember what it
4749 was in IT->saved_face_id, and signal that it's there by
4750 setting face_before_selective_p. */
4751 it->saved_face_id = it->face_id;
4752 it->method = next_element_from_buffer;
4753 reseat_at_next_visible_line_start (it, 1);
4754 it->face_before_selective_p = 1;
4755 }
4756
4757 return get_next_display_element (it);
4758 }
4759
4760
4761 /* Deliver an image display element. The iterator IT is already
4762 filled with image information (done in handle_display_prop). Value
4763 is always 1. */
4764
4765
4766 static int
4767 next_element_from_image (it)
4768 struct it *it;
4769 {
4770 it->what = IT_IMAGE;
4771 return 1;
4772 }
4773
4774
4775 /* Fill iterator IT with next display element from a stretch glyph
4776 property. IT->object is the value of the text property. Value is
4777 always 1. */
4778
4779 static int
4780 next_element_from_stretch (it)
4781 struct it *it;
4782 {
4783 it->what = IT_STRETCH;
4784 return 1;
4785 }
4786
4787
4788 /* Load IT with the next display element from current_buffer. Value
4789 is zero if end of buffer reached. IT->stop_charpos is the next
4790 position at which to stop and check for text properties or buffer
4791 end. */
4792
4793 static int
4794 next_element_from_buffer (it)
4795 struct it *it;
4796 {
4797 int success_p = 1;
4798
4799 /* Check this assumption, otherwise, we would never enter the
4800 if-statement, below. */
4801 xassert (IT_CHARPOS (*it) >= BEGV
4802 && IT_CHARPOS (*it) <= it->stop_charpos);
4803
4804 if (IT_CHARPOS (*it) >= it->stop_charpos)
4805 {
4806 if (IT_CHARPOS (*it) >= it->end_charpos)
4807 {
4808 int overlay_strings_follow_p;
4809
4810 /* End of the game, except when overlay strings follow that
4811 haven't been returned yet. */
4812 if (it->overlay_strings_at_end_processed_p)
4813 overlay_strings_follow_p = 0;
4814 else
4815 {
4816 it->overlay_strings_at_end_processed_p = 1;
4817 overlay_strings_follow_p = get_overlay_strings (it, 0);
4818 }
4819
4820 if (overlay_strings_follow_p)
4821 success_p = get_next_display_element (it);
4822 else
4823 {
4824 it->what = IT_EOB;
4825 it->position = it->current.pos;
4826 success_p = 0;
4827 }
4828 }
4829 else
4830 {
4831 handle_stop (it);
4832 return get_next_display_element (it);
4833 }
4834 }
4835 else
4836 {
4837 /* No face changes, overlays etc. in sight, so just return a
4838 character from current_buffer. */
4839 unsigned char *p;
4840
4841 /* Maybe run the redisplay end trigger hook. Performance note:
4842 This doesn't seem to cost measurable time. */
4843 if (it->redisplay_end_trigger_charpos
4844 && it->glyph_row
4845 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
4846 run_redisplay_end_trigger_hook (it);
4847
4848 /* Get the next character, maybe multibyte. */
4849 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
4850 if (it->multibyte_p && !ASCII_BYTE_P (*p))
4851 {
4852 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
4853 - IT_BYTEPOS (*it));
4854 it->c = string_char_and_length (p, maxlen, &it->len);
4855 }
4856 else
4857 it->c = *p, it->len = 1;
4858
4859 /* Record what we have and where it came from. */
4860 it->what = IT_CHARACTER;;
4861 it->object = it->w->buffer;
4862 it->position = it->current.pos;
4863
4864 /* Normally we return the character found above, except when we
4865 really want to return an ellipsis for selective display. */
4866 if (it->selective)
4867 {
4868 if (it->c == '\n')
4869 {
4870 /* A value of selective > 0 means hide lines indented more
4871 than that number of columns. */
4872 if (it->selective > 0
4873 && IT_CHARPOS (*it) + 1 < ZV
4874 && indented_beyond_p (IT_CHARPOS (*it) + 1,
4875 IT_BYTEPOS (*it) + 1,
4876 (double) it->selective)) /* iftc */
4877 {
4878 success_p = next_element_from_ellipsis (it);
4879 it->dpvec_char_len = -1;
4880 }
4881 }
4882 else if (it->c == '\r' && it->selective == -1)
4883 {
4884 /* A value of selective == -1 means that everything from the
4885 CR to the end of the line is invisible, with maybe an
4886 ellipsis displayed for it. */
4887 success_p = next_element_from_ellipsis (it);
4888 it->dpvec_char_len = -1;
4889 }
4890 }
4891 }
4892
4893 /* Value is zero if end of buffer reached. */
4894 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
4895 return success_p;
4896 }
4897
4898
4899 /* Run the redisplay end trigger hook for IT. */
4900
4901 static void
4902 run_redisplay_end_trigger_hook (it)
4903 struct it *it;
4904 {
4905 Lisp_Object args[3];
4906
4907 /* IT->glyph_row should be non-null, i.e. we should be actually
4908 displaying something, or otherwise we should not run the hook. */
4909 xassert (it->glyph_row);
4910
4911 /* Set up hook arguments. */
4912 args[0] = Qredisplay_end_trigger_functions;
4913 args[1] = it->window;
4914 XSETINT (args[2], it->redisplay_end_trigger_charpos);
4915 it->redisplay_end_trigger_charpos = 0;
4916
4917 /* Since we are *trying* to run these functions, don't try to run
4918 them again, even if they get an error. */
4919 it->w->redisplay_end_trigger = Qnil;
4920 Frun_hook_with_args (3, args);
4921
4922 /* Notice if it changed the face of the character we are on. */
4923 handle_face_prop (it);
4924 }
4925
4926
4927 /* Deliver a composition display element. The iterator IT is already
4928 filled with composition information (done in
4929 handle_composition_prop). Value is always 1. */
4930
4931 static int
4932 next_element_from_composition (it)
4933 struct it *it;
4934 {
4935 it->what = IT_COMPOSITION;
4936 it->position = (STRINGP (it->string)
4937 ? it->current.string_pos
4938 : it->current.pos);
4939 return 1;
4940 }
4941
4942
4943 \f
4944 /***********************************************************************
4945 Moving an iterator without producing glyphs
4946 ***********************************************************************/
4947
4948 /* Move iterator IT to a specified buffer or X position within one
4949 line on the display without producing glyphs.
4950
4951 OP should be a bit mask including some or all of these bits:
4952 MOVE_TO_X: Stop on reaching x-position TO_X.
4953 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
4954 Regardless of OP's value, stop in reaching the end of the display line.
4955
4956 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
4957 This means, in particular, that TO_X includes window's horizontal
4958 scroll amount.
4959
4960 The return value has several possible values that
4961 say what condition caused the scan to stop:
4962
4963 MOVE_POS_MATCH_OR_ZV
4964 - when TO_POS or ZV was reached.
4965
4966 MOVE_X_REACHED
4967 -when TO_X was reached before TO_POS or ZV were reached.
4968
4969 MOVE_LINE_CONTINUED
4970 - when we reached the end of the display area and the line must
4971 be continued.
4972
4973 MOVE_LINE_TRUNCATED
4974 - when we reached the end of the display area and the line is
4975 truncated.
4976
4977 MOVE_NEWLINE_OR_CR
4978 - when we stopped at a line end, i.e. a newline or a CR and selective
4979 display is on. */
4980
4981 static enum move_it_result
4982 move_it_in_display_line_to (it, to_charpos, to_x, op)
4983 struct it *it;
4984 int to_charpos, to_x, op;
4985 {
4986 enum move_it_result result = MOVE_UNDEFINED;
4987 struct glyph_row *saved_glyph_row;
4988
4989 /* Don't produce glyphs in produce_glyphs. */
4990 saved_glyph_row = it->glyph_row;
4991 it->glyph_row = NULL;
4992
4993 while (1)
4994 {
4995 int x, i, ascent = 0, descent = 0;
4996
4997 /* Stop when ZV or TO_CHARPOS reached. */
4998 if (!get_next_display_element (it)
4999 || ((op & MOVE_TO_POS) != 0
5000 && BUFFERP (it->object)
5001 && IT_CHARPOS (*it) >= to_charpos))
5002 {
5003 result = MOVE_POS_MATCH_OR_ZV;
5004 break;
5005 }
5006
5007 /* The call to produce_glyphs will get the metrics of the
5008 display element IT is loaded with. We record in x the
5009 x-position before this display element in case it does not
5010 fit on the line. */
5011 x = it->current_x;
5012
5013 /* Remember the line height so far in case the next element doesn't
5014 fit on the line. */
5015 if (!it->truncate_lines_p)
5016 {
5017 ascent = it->max_ascent;
5018 descent = it->max_descent;
5019 }
5020
5021 PRODUCE_GLYPHS (it);
5022
5023 if (it->area != TEXT_AREA)
5024 {
5025 set_iterator_to_next (it, 1);
5026 continue;
5027 }
5028
5029 /* The number of glyphs we get back in IT->nglyphs will normally
5030 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
5031 character on a terminal frame, or (iii) a line end. For the
5032 second case, IT->nglyphs - 1 padding glyphs will be present
5033 (on X frames, there is only one glyph produced for a
5034 composite character.
5035
5036 The behavior implemented below means, for continuation lines,
5037 that as many spaces of a TAB as fit on the current line are
5038 displayed there. For terminal frames, as many glyphs of a
5039 multi-glyph character are displayed in the current line, too.
5040 This is what the old redisplay code did, and we keep it that
5041 way. Under X, the whole shape of a complex character must
5042 fit on the line or it will be completely displayed in the
5043 next line.
5044
5045 Note that both for tabs and padding glyphs, all glyphs have
5046 the same width. */
5047 if (it->nglyphs)
5048 {
5049 /* More than one glyph or glyph doesn't fit on line. All
5050 glyphs have the same width. */
5051 int single_glyph_width = it->pixel_width / it->nglyphs;
5052 int new_x;
5053
5054 for (i = 0; i < it->nglyphs; ++i, x = new_x)
5055 {
5056 new_x = x + single_glyph_width;
5057
5058 /* We want to leave anything reaching TO_X to the caller. */
5059 if ((op & MOVE_TO_X) && new_x > to_x)
5060 {
5061 it->current_x = x;
5062 result = MOVE_X_REACHED;
5063 break;
5064 }
5065 else if (/* Lines are continued. */
5066 !it->truncate_lines_p
5067 && (/* And glyph doesn't fit on the line. */
5068 new_x > it->last_visible_x
5069 /* Or it fits exactly and we're on a window
5070 system frame. */
5071 || (new_x == it->last_visible_x
5072 && FRAME_WINDOW_P (it->f))))
5073 {
5074 if (/* IT->hpos == 0 means the very first glyph
5075 doesn't fit on the line, e.g. a wide image. */
5076 it->hpos == 0
5077 || (new_x == it->last_visible_x
5078 && FRAME_WINDOW_P (it->f)))
5079 {
5080 ++it->hpos;
5081 it->current_x = new_x;
5082 if (i == it->nglyphs - 1)
5083 set_iterator_to_next (it, 1);
5084 }
5085 else
5086 {
5087 it->current_x = x;
5088 it->max_ascent = ascent;
5089 it->max_descent = descent;
5090 }
5091
5092 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
5093 IT_CHARPOS (*it)));
5094 result = MOVE_LINE_CONTINUED;
5095 break;
5096 }
5097 else if (new_x > it->first_visible_x)
5098 {
5099 /* Glyph is visible. Increment number of glyphs that
5100 would be displayed. */
5101 ++it->hpos;
5102 }
5103 else
5104 {
5105 /* Glyph is completely off the left margin of the display
5106 area. Nothing to do. */
5107 }
5108 }
5109
5110 if (result != MOVE_UNDEFINED)
5111 break;
5112 }
5113 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
5114 {
5115 /* Stop when TO_X specified and reached. This check is
5116 necessary here because of lines consisting of a line end,
5117 only. The line end will not produce any glyphs and we
5118 would never get MOVE_X_REACHED. */
5119 xassert (it->nglyphs == 0);
5120 result = MOVE_X_REACHED;
5121 break;
5122 }
5123
5124 /* Is this a line end? If yes, we're done. */
5125 if (ITERATOR_AT_END_OF_LINE_P (it))
5126 {
5127 result = MOVE_NEWLINE_OR_CR;
5128 break;
5129 }
5130
5131 /* The current display element has been consumed. Advance
5132 to the next. */
5133 set_iterator_to_next (it, 1);
5134
5135 /* Stop if lines are truncated and IT's current x-position is
5136 past the right edge of the window now. */
5137 if (it->truncate_lines_p
5138 && it->current_x >= it->last_visible_x)
5139 {
5140 result = MOVE_LINE_TRUNCATED;
5141 break;
5142 }
5143 }
5144
5145 /* Restore the iterator settings altered at the beginning of this
5146 function. */
5147 it->glyph_row = saved_glyph_row;
5148 return result;
5149 }
5150
5151
5152 /* Move IT forward until it satisfies one or more of the criteria in
5153 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
5154
5155 OP is a bit-mask that specifies where to stop, and in particular,
5156 which of those four position arguments makes a difference. See the
5157 description of enum move_operation_enum.
5158
5159 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
5160 screen line, this function will set IT to the next position >
5161 TO_CHARPOS. */
5162
5163 void
5164 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
5165 struct it *it;
5166 int to_charpos, to_x, to_y, to_vpos;
5167 int op;
5168 {
5169 enum move_it_result skip, skip2 = MOVE_X_REACHED;
5170 int line_height;
5171 int reached = 0;
5172
5173 for (;;)
5174 {
5175 if (op & MOVE_TO_VPOS)
5176 {
5177 /* If no TO_CHARPOS and no TO_X specified, stop at the
5178 start of the line TO_VPOS. */
5179 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
5180 {
5181 if (it->vpos == to_vpos)
5182 {
5183 reached = 1;
5184 break;
5185 }
5186 else
5187 skip = move_it_in_display_line_to (it, -1, -1, 0);
5188 }
5189 else
5190 {
5191 /* TO_VPOS >= 0 means stop at TO_X in the line at
5192 TO_VPOS, or at TO_POS, whichever comes first. */
5193 if (it->vpos == to_vpos)
5194 {
5195 reached = 2;
5196 break;
5197 }
5198
5199 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
5200
5201 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
5202 {
5203 reached = 3;
5204 break;
5205 }
5206 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
5207 {
5208 /* We have reached TO_X but not in the line we want. */
5209 skip = move_it_in_display_line_to (it, to_charpos,
5210 -1, MOVE_TO_POS);
5211 if (skip == MOVE_POS_MATCH_OR_ZV)
5212 {
5213 reached = 4;
5214 break;
5215 }
5216 }
5217 }
5218 }
5219 else if (op & MOVE_TO_Y)
5220 {
5221 struct it it_backup;
5222
5223 /* TO_Y specified means stop at TO_X in the line containing
5224 TO_Y---or at TO_CHARPOS if this is reached first. The
5225 problem is that we can't really tell whether the line
5226 contains TO_Y before we have completely scanned it, and
5227 this may skip past TO_X. What we do is to first scan to
5228 TO_X.
5229
5230 If TO_X is not specified, use a TO_X of zero. The reason
5231 is to make the outcome of this function more predictable.
5232 If we didn't use TO_X == 0, we would stop at the end of
5233 the line which is probably not what a caller would expect
5234 to happen. */
5235 skip = move_it_in_display_line_to (it, to_charpos,
5236 ((op & MOVE_TO_X)
5237 ? to_x : 0),
5238 (MOVE_TO_X
5239 | (op & MOVE_TO_POS)));
5240
5241 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
5242 if (skip == MOVE_POS_MATCH_OR_ZV)
5243 {
5244 reached = 5;
5245 break;
5246 }
5247
5248 /* If TO_X was reached, we would like to know whether TO_Y
5249 is in the line. This can only be said if we know the
5250 total line height which requires us to scan the rest of
5251 the line. */
5252 if (skip == MOVE_X_REACHED)
5253 {
5254 it_backup = *it;
5255 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
5256 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
5257 op & MOVE_TO_POS);
5258 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
5259 }
5260
5261 /* Now, decide whether TO_Y is in this line. */
5262 line_height = it->max_ascent + it->max_descent;
5263 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
5264
5265 if (to_y >= it->current_y
5266 && to_y < it->current_y + line_height)
5267 {
5268 if (skip == MOVE_X_REACHED)
5269 /* If TO_Y is in this line and TO_X was reached above,
5270 we scanned too far. We have to restore IT's settings
5271 to the ones before skipping. */
5272 *it = it_backup;
5273 reached = 6;
5274 }
5275 else if (skip == MOVE_X_REACHED)
5276 {
5277 skip = skip2;
5278 if (skip == MOVE_POS_MATCH_OR_ZV)
5279 reached = 7;
5280 }
5281
5282 if (reached)
5283 break;
5284 }
5285 else
5286 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
5287
5288 switch (skip)
5289 {
5290 case MOVE_POS_MATCH_OR_ZV:
5291 reached = 8;
5292 goto out;
5293
5294 case MOVE_NEWLINE_OR_CR:
5295 set_iterator_to_next (it, 1);
5296 it->continuation_lines_width = 0;
5297 break;
5298
5299 case MOVE_LINE_TRUNCATED:
5300 it->continuation_lines_width = 0;
5301 reseat_at_next_visible_line_start (it, 0);
5302 if ((op & MOVE_TO_POS) != 0
5303 && IT_CHARPOS (*it) > to_charpos)
5304 {
5305 reached = 9;
5306 goto out;
5307 }
5308 break;
5309
5310 case MOVE_LINE_CONTINUED:
5311 it->continuation_lines_width += it->current_x;
5312 break;
5313
5314 default:
5315 abort ();
5316 }
5317
5318 /* Reset/increment for the next run. */
5319 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
5320 it->current_x = it->hpos = 0;
5321 it->current_y += it->max_ascent + it->max_descent;
5322 ++it->vpos;
5323 last_height = it->max_ascent + it->max_descent;
5324 last_max_ascent = it->max_ascent;
5325 it->max_ascent = it->max_descent = 0;
5326 }
5327
5328 out:
5329
5330 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
5331 }
5332
5333
5334 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
5335
5336 If DY > 0, move IT backward at least that many pixels. DY = 0
5337 means move IT backward to the preceding line start or BEGV. This
5338 function may move over more than DY pixels if IT->current_y - DY
5339 ends up in the middle of a line; in this case IT->current_y will be
5340 set to the top of the line moved to. */
5341
5342 void
5343 move_it_vertically_backward (it, dy)
5344 struct it *it;
5345 int dy;
5346 {
5347 int nlines, h;
5348 struct it it2, it3;
5349 int start_pos = IT_CHARPOS (*it);
5350
5351 xassert (dy >= 0);
5352
5353 /* Estimate how many newlines we must move back. */
5354 nlines = max (1, dy / CANON_Y_UNIT (it->f));
5355
5356 /* Set the iterator's position that many lines back. */
5357 while (nlines-- && IT_CHARPOS (*it) > BEGV)
5358 back_to_previous_visible_line_start (it);
5359
5360 /* Reseat the iterator here. When moving backward, we don't want
5361 reseat to skip forward over invisible text, set up the iterator
5362 to deliver from overlay strings at the new position etc. So,
5363 use reseat_1 here. */
5364 reseat_1 (it, it->current.pos, 1);
5365
5366 /* We are now surely at a line start. */
5367 it->current_x = it->hpos = 0;
5368
5369 /* Move forward and see what y-distance we moved. First move to the
5370 start of the next line so that we get its height. We need this
5371 height to be able to tell whether we reached the specified
5372 y-distance. */
5373 it2 = *it;
5374 it2.max_ascent = it2.max_descent = 0;
5375 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
5376 MOVE_TO_POS | MOVE_TO_VPOS);
5377 xassert (IT_CHARPOS (*it) >= BEGV);
5378 it3 = it2;
5379
5380 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
5381 xassert (IT_CHARPOS (*it) >= BEGV);
5382 h = it2.current_y - it->current_y;
5383 nlines = it2.vpos - it->vpos;
5384
5385 /* Correct IT's y and vpos position. */
5386 it->vpos -= nlines;
5387 it->current_y -= h;
5388
5389 if (dy == 0)
5390 {
5391 /* DY == 0 means move to the start of the screen line. The
5392 value of nlines is > 0 if continuation lines were involved. */
5393 if (nlines > 0)
5394 move_it_by_lines (it, nlines, 1);
5395 xassert (IT_CHARPOS (*it) <= start_pos);
5396 }
5397 else if (nlines)
5398 {
5399 /* The y-position we try to reach. Note that h has been
5400 subtracted in front of the if-statement. */
5401 int target_y = it->current_y + h - dy;
5402 int y0 = it3.current_y;
5403 int y1 = line_bottom_y (&it3);
5404 int line_height = y1 - y0;
5405
5406 /* If we did not reach target_y, try to move further backward if
5407 we can. If we moved too far backward, try to move forward. */
5408 if (target_y < it->current_y
5409 /* This is heuristic. In a window that's 3 lines high, with
5410 a line height of 13 pixels each, recentering with point
5411 on the bottom line will try to move -39/2 = 19 pixels
5412 backward. Try to avoid moving into the first line. */
5413 && it->current_y - target_y > line_height / 3 * 2
5414 && IT_CHARPOS (*it) > BEGV)
5415 {
5416 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
5417 target_y - it->current_y));
5418 move_it_vertically (it, target_y - it->current_y);
5419 xassert (IT_CHARPOS (*it) >= BEGV);
5420 }
5421 else if (target_y >= it->current_y + line_height
5422 && IT_CHARPOS (*it) < ZV)
5423 {
5424 /* Should move forward by at least one line, maybe more.
5425
5426 Note: Calling move_it_by_lines can be expensive on
5427 terminal frames, where compute_motion is used (via
5428 vmotion) to do the job, when there are very long lines
5429 and truncate-lines is nil. That's the reason for
5430 treating terminal frames specially here. */
5431
5432 if (!FRAME_WINDOW_P (it->f))
5433 move_it_vertically (it, target_y - (it->current_y + line_height));
5434 else
5435 {
5436 do
5437 {
5438 move_it_by_lines (it, 1, 1);
5439 }
5440 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
5441 }
5442
5443 xassert (IT_CHARPOS (*it) >= BEGV);
5444 }
5445 }
5446 }
5447
5448
5449 /* Move IT by a specified amount of pixel lines DY. DY negative means
5450 move backwards. DY = 0 means move to start of screen line. At the
5451 end, IT will be on the start of a screen line. */
5452
5453 void
5454 move_it_vertically (it, dy)
5455 struct it *it;
5456 int dy;
5457 {
5458 if (dy <= 0)
5459 move_it_vertically_backward (it, -dy);
5460 else if (dy > 0)
5461 {
5462 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
5463 move_it_to (it, ZV, -1, it->current_y + dy, -1,
5464 MOVE_TO_POS | MOVE_TO_Y);
5465 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
5466
5467 /* If buffer ends in ZV without a newline, move to the start of
5468 the line to satisfy the post-condition. */
5469 if (IT_CHARPOS (*it) == ZV
5470 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
5471 move_it_by_lines (it, 0, 0);
5472 }
5473 }
5474
5475
5476 /* Move iterator IT past the end of the text line it is in. */
5477
5478 void
5479 move_it_past_eol (it)
5480 struct it *it;
5481 {
5482 enum move_it_result rc;
5483
5484 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
5485 if (rc == MOVE_NEWLINE_OR_CR)
5486 set_iterator_to_next (it, 0);
5487 }
5488
5489
5490 #if 0 /* Currently not used. */
5491
5492 /* Return non-zero if some text between buffer positions START_CHARPOS
5493 and END_CHARPOS is invisible. IT->window is the window for text
5494 property lookup. */
5495
5496 static int
5497 invisible_text_between_p (it, start_charpos, end_charpos)
5498 struct it *it;
5499 int start_charpos, end_charpos;
5500 {
5501 Lisp_Object prop, limit;
5502 int invisible_found_p;
5503
5504 xassert (it != NULL && start_charpos <= end_charpos);
5505
5506 /* Is text at START invisible? */
5507 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
5508 it->window);
5509 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5510 invisible_found_p = 1;
5511 else
5512 {
5513 limit = Fnext_single_char_property_change (make_number (start_charpos),
5514 Qinvisible, Qnil,
5515 make_number (end_charpos));
5516 invisible_found_p = XFASTINT (limit) < end_charpos;
5517 }
5518
5519 return invisible_found_p;
5520 }
5521
5522 #endif /* 0 */
5523
5524
5525 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
5526 negative means move up. DVPOS == 0 means move to the start of the
5527 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
5528 NEED_Y_P is zero, IT->current_y will be left unchanged.
5529
5530 Further optimization ideas: If we would know that IT->f doesn't use
5531 a face with proportional font, we could be faster for
5532 truncate-lines nil. */
5533
5534 void
5535 move_it_by_lines (it, dvpos, need_y_p)
5536 struct it *it;
5537 int dvpos, need_y_p;
5538 {
5539 struct position pos;
5540
5541 if (!FRAME_WINDOW_P (it->f))
5542 {
5543 struct text_pos textpos;
5544
5545 /* We can use vmotion on frames without proportional fonts. */
5546 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
5547 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
5548 reseat (it, textpos, 1);
5549 it->vpos += pos.vpos;
5550 it->current_y += pos.vpos;
5551 }
5552 else if (dvpos == 0)
5553 {
5554 /* DVPOS == 0 means move to the start of the screen line. */
5555 move_it_vertically_backward (it, 0);
5556 xassert (it->current_x == 0 && it->hpos == 0);
5557 }
5558 else if (dvpos > 0)
5559 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
5560 else
5561 {
5562 struct it it2;
5563 int start_charpos, i;
5564
5565 /* Start at the beginning of the screen line containing IT's
5566 position. */
5567 move_it_vertically_backward (it, 0);
5568
5569 /* Go back -DVPOS visible lines and reseat the iterator there. */
5570 start_charpos = IT_CHARPOS (*it);
5571 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
5572 back_to_previous_visible_line_start (it);
5573 reseat (it, it->current.pos, 1);
5574 it->current_x = it->hpos = 0;
5575
5576 /* Above call may have moved too far if continuation lines
5577 are involved. Scan forward and see if it did. */
5578 it2 = *it;
5579 it2.vpos = it2.current_y = 0;
5580 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
5581 it->vpos -= it2.vpos;
5582 it->current_y -= it2.current_y;
5583 it->current_x = it->hpos = 0;
5584
5585 /* If we moved too far, move IT some lines forward. */
5586 if (it2.vpos > -dvpos)
5587 {
5588 int delta = it2.vpos + dvpos;
5589 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
5590 }
5591 }
5592 }
5593
5594
5595 \f
5596 /***********************************************************************
5597 Messages
5598 ***********************************************************************/
5599
5600
5601 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
5602 to *Messages*. */
5603
5604 void
5605 add_to_log (format, arg1, arg2)
5606 char *format;
5607 Lisp_Object arg1, arg2;
5608 {
5609 Lisp_Object args[3];
5610 Lisp_Object msg, fmt;
5611 char *buffer;
5612 int len;
5613 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5614
5615 /* Do nothing if called asynchronously. Inserting text into
5616 a buffer may call after-change-functions and alike and
5617 that would means running Lisp asynchronously. */
5618 if (handling_signal)
5619 return;
5620
5621 fmt = msg = Qnil;
5622 GCPRO4 (fmt, msg, arg1, arg2);
5623
5624 args[0] = fmt = build_string (format);
5625 args[1] = arg1;
5626 args[2] = arg2;
5627 msg = Fformat (3, args);
5628
5629 len = SBYTES (msg) + 1;
5630 buffer = (char *) alloca (len);
5631 bcopy (SDATA (msg), buffer, len);
5632
5633 message_dolog (buffer, len - 1, 1, 0);
5634 UNGCPRO;
5635 }
5636
5637
5638 /* Output a newline in the *Messages* buffer if "needs" one. */
5639
5640 void
5641 message_log_maybe_newline ()
5642 {
5643 if (message_log_need_newline)
5644 message_dolog ("", 0, 1, 0);
5645 }
5646
5647
5648 /* Add a string M of length NBYTES to the message log, optionally
5649 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
5650 nonzero, means interpret the contents of M as multibyte. This
5651 function calls low-level routines in order to bypass text property
5652 hooks, etc. which might not be safe to run. */
5653
5654 void
5655 message_dolog (m, nbytes, nlflag, multibyte)
5656 char *m;
5657 int nbytes, nlflag, multibyte;
5658 {
5659 if (!NILP (Vmemory_full))
5660 return;
5661
5662 if (!NILP (Vmessage_log_max))
5663 {
5664 struct buffer *oldbuf;
5665 Lisp_Object oldpoint, oldbegv, oldzv;
5666 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5667 int point_at_end = 0;
5668 int zv_at_end = 0;
5669 Lisp_Object old_deactivate_mark, tem;
5670 struct gcpro gcpro1;
5671
5672 old_deactivate_mark = Vdeactivate_mark;
5673 oldbuf = current_buffer;
5674 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
5675 current_buffer->undo_list = Qt;
5676
5677 oldpoint = message_dolog_marker1;
5678 set_marker_restricted (oldpoint, make_number (PT), Qnil);
5679 oldbegv = message_dolog_marker2;
5680 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
5681 oldzv = message_dolog_marker3;
5682 set_marker_restricted (oldzv, make_number (ZV), Qnil);
5683 GCPRO1 (old_deactivate_mark);
5684
5685 if (PT == Z)
5686 point_at_end = 1;
5687 if (ZV == Z)
5688 zv_at_end = 1;
5689
5690 BEGV = BEG;
5691 BEGV_BYTE = BEG_BYTE;
5692 ZV = Z;
5693 ZV_BYTE = Z_BYTE;
5694 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5695
5696 /* Insert the string--maybe converting multibyte to single byte
5697 or vice versa, so that all the text fits the buffer. */
5698 if (multibyte
5699 && NILP (current_buffer->enable_multibyte_characters))
5700 {
5701 int i, c, char_bytes;
5702 unsigned char work[1];
5703
5704 /* Convert a multibyte string to single-byte
5705 for the *Message* buffer. */
5706 for (i = 0; i < nbytes; i += nbytes)
5707 {
5708 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
5709 work[0] = (SINGLE_BYTE_CHAR_P (c)
5710 ? c
5711 : multibyte_char_to_unibyte (c, Qnil));
5712 insert_1_both (work, 1, 1, 1, 0, 0);
5713 }
5714 }
5715 else if (! multibyte
5716 && ! NILP (current_buffer->enable_multibyte_characters))
5717 {
5718 int i, c, char_bytes;
5719 unsigned char *msg = (unsigned char *) m;
5720 unsigned char str[MAX_MULTIBYTE_LENGTH];
5721 /* Convert a single-byte string to multibyte
5722 for the *Message* buffer. */
5723 for (i = 0; i < nbytes; i++)
5724 {
5725 c = unibyte_char_to_multibyte (msg[i]);
5726 char_bytes = CHAR_STRING (c, str);
5727 insert_1_both (str, 1, char_bytes, 1, 0, 0);
5728 }
5729 }
5730 else if (nbytes)
5731 insert_1 (m, nbytes, 1, 0, 0);
5732
5733 if (nlflag)
5734 {
5735 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
5736 insert_1 ("\n", 1, 1, 0, 0);
5737
5738 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
5739 this_bol = PT;
5740 this_bol_byte = PT_BYTE;
5741
5742 /* See if this line duplicates the previous one.
5743 If so, combine duplicates. */
5744 if (this_bol > BEG)
5745 {
5746 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
5747 prev_bol = PT;
5748 prev_bol_byte = PT_BYTE;
5749
5750 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
5751 this_bol, this_bol_byte);
5752 if (dup)
5753 {
5754 del_range_both (prev_bol, prev_bol_byte,
5755 this_bol, this_bol_byte, 0);
5756 if (dup > 1)
5757 {
5758 char dupstr[40];
5759 int duplen;
5760
5761 /* If you change this format, don't forget to also
5762 change message_log_check_duplicate. */
5763 sprintf (dupstr, " [%d times]", dup);
5764 duplen = strlen (dupstr);
5765 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
5766 insert_1 (dupstr, duplen, 1, 0, 1);
5767 }
5768 }
5769 }
5770
5771 /* If we have more than the desired maximum number of lines
5772 in the *Messages* buffer now, delete the oldest ones.
5773 This is safe because we don't have undo in this buffer. */
5774
5775 if (NATNUMP (Vmessage_log_max))
5776 {
5777 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
5778 -XFASTINT (Vmessage_log_max) - 1, 0);
5779 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
5780 }
5781 }
5782 BEGV = XMARKER (oldbegv)->charpos;
5783 BEGV_BYTE = marker_byte_position (oldbegv);
5784
5785 if (zv_at_end)
5786 {
5787 ZV = Z;
5788 ZV_BYTE = Z_BYTE;
5789 }
5790 else
5791 {
5792 ZV = XMARKER (oldzv)->charpos;
5793 ZV_BYTE = marker_byte_position (oldzv);
5794 }
5795
5796 if (point_at_end)
5797 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5798 else
5799 /* We can't do Fgoto_char (oldpoint) because it will run some
5800 Lisp code. */
5801 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
5802 XMARKER (oldpoint)->bytepos);
5803
5804 UNGCPRO;
5805 unchain_marker (oldpoint);
5806 unchain_marker (oldbegv);
5807 unchain_marker (oldzv);
5808
5809 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
5810 set_buffer_internal (oldbuf);
5811 if (NILP (tem))
5812 windows_or_buffers_changed = old_windows_or_buffers_changed;
5813 message_log_need_newline = !nlflag;
5814 Vdeactivate_mark = old_deactivate_mark;
5815 }
5816 }
5817
5818
5819 /* We are at the end of the buffer after just having inserted a newline.
5820 (Note: We depend on the fact we won't be crossing the gap.)
5821 Check to see if the most recent message looks a lot like the previous one.
5822 Return 0 if different, 1 if the new one should just replace it, or a
5823 value N > 1 if we should also append " [N times]". */
5824
5825 static int
5826 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
5827 int prev_bol, this_bol;
5828 int prev_bol_byte, this_bol_byte;
5829 {
5830 int i;
5831 int len = Z_BYTE - 1 - this_bol_byte;
5832 int seen_dots = 0;
5833 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
5834 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
5835
5836 for (i = 0; i < len; i++)
5837 {
5838 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
5839 seen_dots = 1;
5840 if (p1[i] != p2[i])
5841 return seen_dots;
5842 }
5843 p1 += len;
5844 if (*p1 == '\n')
5845 return 2;
5846 if (*p1++ == ' ' && *p1++ == '[')
5847 {
5848 int n = 0;
5849 while (*p1 >= '0' && *p1 <= '9')
5850 n = n * 10 + *p1++ - '0';
5851 if (strncmp (p1, " times]\n", 8) == 0)
5852 return n+1;
5853 }
5854 return 0;
5855 }
5856
5857
5858 /* Display an echo area message M with a specified length of NBYTES
5859 bytes. The string may include null characters. If M is 0, clear
5860 out any existing message, and let the mini-buffer text show
5861 through.
5862
5863 The buffer M must continue to exist until after the echo area gets
5864 cleared or some other message gets displayed there. This means do
5865 not pass text that is stored in a Lisp string; do not pass text in
5866 a buffer that was alloca'd. */
5867
5868 void
5869 message2 (m, nbytes, multibyte)
5870 char *m;
5871 int nbytes;
5872 int multibyte;
5873 {
5874 /* First flush out any partial line written with print. */
5875 message_log_maybe_newline ();
5876 if (m)
5877 message_dolog (m, nbytes, 1, multibyte);
5878 message2_nolog (m, nbytes, multibyte);
5879 }
5880
5881
5882 /* The non-logging counterpart of message2. */
5883
5884 void
5885 message2_nolog (m, nbytes, multibyte)
5886 char *m;
5887 int nbytes;
5888 {
5889 struct frame *sf = SELECTED_FRAME ();
5890 message_enable_multibyte = multibyte;
5891
5892 if (noninteractive)
5893 {
5894 if (noninteractive_need_newline)
5895 putc ('\n', stderr);
5896 noninteractive_need_newline = 0;
5897 if (m)
5898 fwrite (m, nbytes, 1, stderr);
5899 if (cursor_in_echo_area == 0)
5900 fprintf (stderr, "\n");
5901 fflush (stderr);
5902 }
5903 /* A null message buffer means that the frame hasn't really been
5904 initialized yet. Error messages get reported properly by
5905 cmd_error, so this must be just an informative message; toss it. */
5906 else if (INTERACTIVE
5907 && sf->glyphs_initialized_p
5908 && FRAME_MESSAGE_BUF (sf))
5909 {
5910 Lisp_Object mini_window;
5911 struct frame *f;
5912
5913 /* Get the frame containing the mini-buffer
5914 that the selected frame is using. */
5915 mini_window = FRAME_MINIBUF_WINDOW (sf);
5916 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5917
5918 FRAME_SAMPLE_VISIBILITY (f);
5919 if (FRAME_VISIBLE_P (sf)
5920 && ! FRAME_VISIBLE_P (f))
5921 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
5922
5923 if (m)
5924 {
5925 set_message (m, Qnil, nbytes, multibyte);
5926 if (minibuffer_auto_raise)
5927 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5928 }
5929 else
5930 clear_message (1, 1);
5931
5932 do_pending_window_change (0);
5933 echo_area_display (1);
5934 do_pending_window_change (0);
5935 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5936 (*frame_up_to_date_hook) (f);
5937 }
5938 }
5939
5940
5941 /* Display an echo area message M with a specified length of NBYTES
5942 bytes. The string may include null characters. If M is not a
5943 string, clear out any existing message, and let the mini-buffer
5944 text show through. */
5945
5946 void
5947 message3 (m, nbytes, multibyte)
5948 Lisp_Object m;
5949 int nbytes;
5950 int multibyte;
5951 {
5952 struct gcpro gcpro1;
5953
5954 GCPRO1 (m);
5955
5956 /* First flush out any partial line written with print. */
5957 message_log_maybe_newline ();
5958 if (STRINGP (m))
5959 message_dolog (SDATA (m), nbytes, 1, multibyte);
5960 message3_nolog (m, nbytes, multibyte);
5961
5962 UNGCPRO;
5963 }
5964
5965
5966 /* The non-logging version of message3. */
5967
5968 void
5969 message3_nolog (m, nbytes, multibyte)
5970 Lisp_Object m;
5971 int nbytes, multibyte;
5972 {
5973 struct frame *sf = SELECTED_FRAME ();
5974 message_enable_multibyte = multibyte;
5975
5976 if (noninteractive)
5977 {
5978 if (noninteractive_need_newline)
5979 putc ('\n', stderr);
5980 noninteractive_need_newline = 0;
5981 if (STRINGP (m))
5982 fwrite (SDATA (m), nbytes, 1, stderr);
5983 if (cursor_in_echo_area == 0)
5984 fprintf (stderr, "\n");
5985 fflush (stderr);
5986 }
5987 /* A null message buffer means that the frame hasn't really been
5988 initialized yet. Error messages get reported properly by
5989 cmd_error, so this must be just an informative message; toss it. */
5990 else if (INTERACTIVE
5991 && sf->glyphs_initialized_p
5992 && FRAME_MESSAGE_BUF (sf))
5993 {
5994 Lisp_Object mini_window;
5995 Lisp_Object frame;
5996 struct frame *f;
5997
5998 /* Get the frame containing the mini-buffer
5999 that the selected frame is using. */
6000 mini_window = FRAME_MINIBUF_WINDOW (sf);
6001 frame = XWINDOW (mini_window)->frame;
6002 f = XFRAME (frame);
6003
6004 FRAME_SAMPLE_VISIBILITY (f);
6005 if (FRAME_VISIBLE_P (sf)
6006 && !FRAME_VISIBLE_P (f))
6007 Fmake_frame_visible (frame);
6008
6009 if (STRINGP (m) && SCHARS (m) > 0)
6010 {
6011 set_message (NULL, m, nbytes, multibyte);
6012 if (minibuffer_auto_raise)
6013 Fraise_frame (frame);
6014 }
6015 else
6016 clear_message (1, 1);
6017
6018 do_pending_window_change (0);
6019 echo_area_display (1);
6020 do_pending_window_change (0);
6021 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6022 (*frame_up_to_date_hook) (f);
6023 }
6024 }
6025
6026
6027 /* Display a null-terminated echo area message M. If M is 0, clear
6028 out any existing message, and let the mini-buffer text show through.
6029
6030 The buffer M must continue to exist until after the echo area gets
6031 cleared or some other message gets displayed there. Do not pass
6032 text that is stored in a Lisp string. Do not pass text in a buffer
6033 that was alloca'd. */
6034
6035 void
6036 message1 (m)
6037 char *m;
6038 {
6039 message2 (m, (m ? strlen (m) : 0), 0);
6040 }
6041
6042
6043 /* The non-logging counterpart of message1. */
6044
6045 void
6046 message1_nolog (m)
6047 char *m;
6048 {
6049 message2_nolog (m, (m ? strlen (m) : 0), 0);
6050 }
6051
6052 /* Display a message M which contains a single %s
6053 which gets replaced with STRING. */
6054
6055 void
6056 message_with_string (m, string, log)
6057 char *m;
6058 Lisp_Object string;
6059 int log;
6060 {
6061 CHECK_STRING (string);
6062
6063 if (noninteractive)
6064 {
6065 if (m)
6066 {
6067 if (noninteractive_need_newline)
6068 putc ('\n', stderr);
6069 noninteractive_need_newline = 0;
6070 fprintf (stderr, m, SDATA (string));
6071 if (cursor_in_echo_area == 0)
6072 fprintf (stderr, "\n");
6073 fflush (stderr);
6074 }
6075 }
6076 else if (INTERACTIVE)
6077 {
6078 /* The frame whose minibuffer we're going to display the message on.
6079 It may be larger than the selected frame, so we need
6080 to use its buffer, not the selected frame's buffer. */
6081 Lisp_Object mini_window;
6082 struct frame *f, *sf = SELECTED_FRAME ();
6083
6084 /* Get the frame containing the minibuffer
6085 that the selected frame is using. */
6086 mini_window = FRAME_MINIBUF_WINDOW (sf);
6087 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6088
6089 /* A null message buffer means that the frame hasn't really been
6090 initialized yet. Error messages get reported properly by
6091 cmd_error, so this must be just an informative message; toss it. */
6092 if (FRAME_MESSAGE_BUF (f))
6093 {
6094 Lisp_Object args[2], message;
6095 struct gcpro gcpro1, gcpro2;
6096
6097 args[0] = build_string (m);
6098 args[1] = message = string;
6099 GCPRO2 (args[0], message);
6100 gcpro1.nvars = 2;
6101
6102 message = Fformat (2, args);
6103
6104 if (log)
6105 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
6106 else
6107 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
6108
6109 UNGCPRO;
6110
6111 /* Print should start at the beginning of the message
6112 buffer next time. */
6113 message_buf_print = 0;
6114 }
6115 }
6116 }
6117
6118
6119 /* Dump an informative message to the minibuf. If M is 0, clear out
6120 any existing message, and let the mini-buffer text show through. */
6121
6122 /* VARARGS 1 */
6123 void
6124 message (m, a1, a2, a3)
6125 char *m;
6126 EMACS_INT a1, a2, a3;
6127 {
6128 if (noninteractive)
6129 {
6130 if (m)
6131 {
6132 if (noninteractive_need_newline)
6133 putc ('\n', stderr);
6134 noninteractive_need_newline = 0;
6135 fprintf (stderr, m, a1, a2, a3);
6136 if (cursor_in_echo_area == 0)
6137 fprintf (stderr, "\n");
6138 fflush (stderr);
6139 }
6140 }
6141 else if (INTERACTIVE)
6142 {
6143 /* The frame whose mini-buffer we're going to display the message
6144 on. It may be larger than the selected frame, so we need to
6145 use its buffer, not the selected frame's buffer. */
6146 Lisp_Object mini_window;
6147 struct frame *f, *sf = SELECTED_FRAME ();
6148
6149 /* Get the frame containing the mini-buffer
6150 that the selected frame is using. */
6151 mini_window = FRAME_MINIBUF_WINDOW (sf);
6152 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6153
6154 /* A null message buffer means that the frame hasn't really been
6155 initialized yet. Error messages get reported properly by
6156 cmd_error, so this must be just an informative message; toss
6157 it. */
6158 if (FRAME_MESSAGE_BUF (f))
6159 {
6160 if (m)
6161 {
6162 int len;
6163 #ifdef NO_ARG_ARRAY
6164 char *a[3];
6165 a[0] = (char *) a1;
6166 a[1] = (char *) a2;
6167 a[2] = (char *) a3;
6168
6169 len = doprnt (FRAME_MESSAGE_BUF (f),
6170 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6171 #else
6172 len = doprnt (FRAME_MESSAGE_BUF (f),
6173 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
6174 (char **) &a1);
6175 #endif /* NO_ARG_ARRAY */
6176
6177 message2 (FRAME_MESSAGE_BUF (f), len, 0);
6178 }
6179 else
6180 message1 (0);
6181
6182 /* Print should start at the beginning of the message
6183 buffer next time. */
6184 message_buf_print = 0;
6185 }
6186 }
6187 }
6188
6189
6190 /* The non-logging version of message. */
6191
6192 void
6193 message_nolog (m, a1, a2, a3)
6194 char *m;
6195 EMACS_INT a1, a2, a3;
6196 {
6197 Lisp_Object old_log_max;
6198 old_log_max = Vmessage_log_max;
6199 Vmessage_log_max = Qnil;
6200 message (m, a1, a2, a3);
6201 Vmessage_log_max = old_log_max;
6202 }
6203
6204
6205 /* Display the current message in the current mini-buffer. This is
6206 only called from error handlers in process.c, and is not time
6207 critical. */
6208
6209 void
6210 update_echo_area ()
6211 {
6212 if (!NILP (echo_area_buffer[0]))
6213 {
6214 Lisp_Object string;
6215 string = Fcurrent_message ();
6216 message3 (string, SBYTES (string),
6217 !NILP (current_buffer->enable_multibyte_characters));
6218 }
6219 }
6220
6221
6222 /* Make sure echo area buffers in `echo_buffers' are live.
6223 If they aren't, make new ones. */
6224
6225 static void
6226 ensure_echo_area_buffers ()
6227 {
6228 int i;
6229
6230 for (i = 0; i < 2; ++i)
6231 if (!BUFFERP (echo_buffer[i])
6232 || NILP (XBUFFER (echo_buffer[i])->name))
6233 {
6234 char name[30];
6235 Lisp_Object old_buffer;
6236 int j;
6237
6238 old_buffer = echo_buffer[i];
6239 sprintf (name, " *Echo Area %d*", i);
6240 echo_buffer[i] = Fget_buffer_create (build_string (name));
6241 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
6242
6243 for (j = 0; j < 2; ++j)
6244 if (EQ (old_buffer, echo_area_buffer[j]))
6245 echo_area_buffer[j] = echo_buffer[i];
6246 }
6247 }
6248
6249
6250 /* Call FN with args A1..A4 with either the current or last displayed
6251 echo_area_buffer as current buffer.
6252
6253 WHICH zero means use the current message buffer
6254 echo_area_buffer[0]. If that is nil, choose a suitable buffer
6255 from echo_buffer[] and clear it.
6256
6257 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
6258 suitable buffer from echo_buffer[] and clear it.
6259
6260 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
6261 that the current message becomes the last displayed one, make
6262 choose a suitable buffer for echo_area_buffer[0], and clear it.
6263
6264 Value is what FN returns. */
6265
6266 static int
6267 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
6268 struct window *w;
6269 int which;
6270 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
6271 EMACS_INT a1;
6272 Lisp_Object a2;
6273 EMACS_INT a3, a4;
6274 {
6275 Lisp_Object buffer;
6276 int this_one, the_other, clear_buffer_p, rc;
6277 int count = SPECPDL_INDEX ();
6278
6279 /* If buffers aren't live, make new ones. */
6280 ensure_echo_area_buffers ();
6281
6282 clear_buffer_p = 0;
6283
6284 if (which == 0)
6285 this_one = 0, the_other = 1;
6286 else if (which > 0)
6287 this_one = 1, the_other = 0;
6288 else
6289 {
6290 this_one = 0, the_other = 1;
6291 clear_buffer_p = 1;
6292
6293 /* We need a fresh one in case the current echo buffer equals
6294 the one containing the last displayed echo area message. */
6295 if (!NILP (echo_area_buffer[this_one])
6296 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
6297 echo_area_buffer[this_one] = Qnil;
6298 }
6299
6300 /* Choose a suitable buffer from echo_buffer[] is we don't
6301 have one. */
6302 if (NILP (echo_area_buffer[this_one]))
6303 {
6304 echo_area_buffer[this_one]
6305 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
6306 ? echo_buffer[the_other]
6307 : echo_buffer[this_one]);
6308 clear_buffer_p = 1;
6309 }
6310
6311 buffer = echo_area_buffer[this_one];
6312
6313 /* Don't get confused by reusing the buffer used for echoing
6314 for a different purpose. */
6315 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
6316 cancel_echoing ();
6317
6318 record_unwind_protect (unwind_with_echo_area_buffer,
6319 with_echo_area_buffer_unwind_data (w));
6320
6321 /* Make the echo area buffer current. Note that for display
6322 purposes, it is not necessary that the displayed window's buffer
6323 == current_buffer, except for text property lookup. So, let's
6324 only set that buffer temporarily here without doing a full
6325 Fset_window_buffer. We must also change w->pointm, though,
6326 because otherwise an assertions in unshow_buffer fails, and Emacs
6327 aborts. */
6328 set_buffer_internal_1 (XBUFFER (buffer));
6329 if (w)
6330 {
6331 w->buffer = buffer;
6332 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
6333 }
6334
6335 current_buffer->undo_list = Qt;
6336 current_buffer->read_only = Qnil;
6337 specbind (Qinhibit_read_only, Qt);
6338 specbind (Qinhibit_modification_hooks, Qt);
6339
6340 if (clear_buffer_p && Z > BEG)
6341 del_range (BEG, Z);
6342
6343 xassert (BEGV >= BEG);
6344 xassert (ZV <= Z && ZV >= BEGV);
6345
6346 rc = fn (a1, a2, a3, a4);
6347
6348 xassert (BEGV >= BEG);
6349 xassert (ZV <= Z && ZV >= BEGV);
6350
6351 unbind_to (count, Qnil);
6352 return rc;
6353 }
6354
6355
6356 /* Save state that should be preserved around the call to the function
6357 FN called in with_echo_area_buffer. */
6358
6359 static Lisp_Object
6360 with_echo_area_buffer_unwind_data (w)
6361 struct window *w;
6362 {
6363 int i = 0;
6364 Lisp_Object vector;
6365
6366 /* Reduce consing by keeping one vector in
6367 Vwith_echo_area_save_vector. */
6368 vector = Vwith_echo_area_save_vector;
6369 Vwith_echo_area_save_vector = Qnil;
6370
6371 if (NILP (vector))
6372 vector = Fmake_vector (make_number (7), Qnil);
6373
6374 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
6375 AREF (vector, i) = Vdeactivate_mark, ++i;
6376 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
6377
6378 if (w)
6379 {
6380 XSETWINDOW (AREF (vector, i), w); ++i;
6381 AREF (vector, i) = w->buffer; ++i;
6382 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
6383 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
6384 }
6385 else
6386 {
6387 int end = i + 4;
6388 for (; i < end; ++i)
6389 AREF (vector, i) = Qnil;
6390 }
6391
6392 xassert (i == ASIZE (vector));
6393 return vector;
6394 }
6395
6396
6397 /* Restore global state from VECTOR which was created by
6398 with_echo_area_buffer_unwind_data. */
6399
6400 static Lisp_Object
6401 unwind_with_echo_area_buffer (vector)
6402 Lisp_Object vector;
6403 {
6404 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
6405 Vdeactivate_mark = AREF (vector, 1);
6406 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
6407
6408 if (WINDOWP (AREF (vector, 3)))
6409 {
6410 struct window *w;
6411 Lisp_Object buffer, charpos, bytepos;
6412
6413 w = XWINDOW (AREF (vector, 3));
6414 buffer = AREF (vector, 4);
6415 charpos = AREF (vector, 5);
6416 bytepos = AREF (vector, 6);
6417
6418 w->buffer = buffer;
6419 set_marker_both (w->pointm, buffer,
6420 XFASTINT (charpos), XFASTINT (bytepos));
6421 }
6422
6423 Vwith_echo_area_save_vector = vector;
6424 return Qnil;
6425 }
6426
6427
6428 /* Set up the echo area for use by print functions. MULTIBYTE_P
6429 non-zero means we will print multibyte. */
6430
6431 void
6432 setup_echo_area_for_printing (multibyte_p)
6433 int multibyte_p;
6434 {
6435 ensure_echo_area_buffers ();
6436
6437 if (!message_buf_print)
6438 {
6439 /* A message has been output since the last time we printed.
6440 Choose a fresh echo area buffer. */
6441 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6442 echo_area_buffer[0] = echo_buffer[1];
6443 else
6444 echo_area_buffer[0] = echo_buffer[0];
6445
6446 /* Switch to that buffer and clear it. */
6447 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6448 current_buffer->truncate_lines = Qnil;
6449
6450 if (Z > BEG)
6451 {
6452 int count = SPECPDL_INDEX ();
6453 specbind (Qinhibit_read_only, Qt);
6454 /* Note that undo recording is always disabled. */
6455 del_range (BEG, Z);
6456 unbind_to (count, Qnil);
6457 }
6458 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6459
6460 /* Set up the buffer for the multibyteness we need. */
6461 if (multibyte_p
6462 != !NILP (current_buffer->enable_multibyte_characters))
6463 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
6464
6465 /* Raise the frame containing the echo area. */
6466 if (minibuffer_auto_raise)
6467 {
6468 struct frame *sf = SELECTED_FRAME ();
6469 Lisp_Object mini_window;
6470 mini_window = FRAME_MINIBUF_WINDOW (sf);
6471 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6472 }
6473
6474 message_log_maybe_newline ();
6475 message_buf_print = 1;
6476 }
6477 else
6478 {
6479 if (NILP (echo_area_buffer[0]))
6480 {
6481 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6482 echo_area_buffer[0] = echo_buffer[1];
6483 else
6484 echo_area_buffer[0] = echo_buffer[0];
6485 }
6486
6487 if (current_buffer != XBUFFER (echo_area_buffer[0]))
6488 {
6489 /* Someone switched buffers between print requests. */
6490 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6491 current_buffer->truncate_lines = Qnil;
6492 }
6493 }
6494 }
6495
6496
6497 /* Display an echo area message in window W. Value is non-zero if W's
6498 height is changed. If display_last_displayed_message_p is
6499 non-zero, display the message that was last displayed, otherwise
6500 display the current message. */
6501
6502 static int
6503 display_echo_area (w)
6504 struct window *w;
6505 {
6506 int i, no_message_p, window_height_changed_p, count;
6507
6508 /* Temporarily disable garbage collections while displaying the echo
6509 area. This is done because a GC can print a message itself.
6510 That message would modify the echo area buffer's contents while a
6511 redisplay of the buffer is going on, and seriously confuse
6512 redisplay. */
6513 count = inhibit_garbage_collection ();
6514
6515 /* If there is no message, we must call display_echo_area_1
6516 nevertheless because it resizes the window. But we will have to
6517 reset the echo_area_buffer in question to nil at the end because
6518 with_echo_area_buffer will sets it to an empty buffer. */
6519 i = display_last_displayed_message_p ? 1 : 0;
6520 no_message_p = NILP (echo_area_buffer[i]);
6521
6522 window_height_changed_p
6523 = with_echo_area_buffer (w, display_last_displayed_message_p,
6524 display_echo_area_1,
6525 (EMACS_INT) w, Qnil, 0, 0);
6526
6527 if (no_message_p)
6528 echo_area_buffer[i] = Qnil;
6529
6530 unbind_to (count, Qnil);
6531 return window_height_changed_p;
6532 }
6533
6534
6535 /* Helper for display_echo_area. Display the current buffer which
6536 contains the current echo area message in window W, a mini-window,
6537 a pointer to which is passed in A1. A2..A4 are currently not used.
6538 Change the height of W so that all of the message is displayed.
6539 Value is non-zero if height of W was changed. */
6540
6541 static int
6542 display_echo_area_1 (a1, a2, a3, a4)
6543 EMACS_INT a1;
6544 Lisp_Object a2;
6545 EMACS_INT a3, a4;
6546 {
6547 struct window *w = (struct window *) a1;
6548 Lisp_Object window;
6549 struct text_pos start;
6550 int window_height_changed_p = 0;
6551
6552 /* Do this before displaying, so that we have a large enough glyph
6553 matrix for the display. */
6554 window_height_changed_p = resize_mini_window (w, 0);
6555
6556 /* Display. */
6557 clear_glyph_matrix (w->desired_matrix);
6558 XSETWINDOW (window, w);
6559 SET_TEXT_POS (start, BEG, BEG_BYTE);
6560 try_window (window, start);
6561
6562 return window_height_changed_p;
6563 }
6564
6565
6566 /* Resize the echo area window to exactly the size needed for the
6567 currently displayed message, if there is one. If a mini-buffer
6568 is active, don't shrink it. */
6569
6570 void
6571 resize_echo_area_exactly ()
6572 {
6573 if (BUFFERP (echo_area_buffer[0])
6574 && WINDOWP (echo_area_window))
6575 {
6576 struct window *w = XWINDOW (echo_area_window);
6577 int resized_p;
6578 Lisp_Object resize_exactly;
6579
6580 if (minibuf_level == 0)
6581 resize_exactly = Qt;
6582 else
6583 resize_exactly = Qnil;
6584
6585 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
6586 (EMACS_INT) w, resize_exactly, 0, 0);
6587 if (resized_p)
6588 {
6589 ++windows_or_buffers_changed;
6590 ++update_mode_lines;
6591 redisplay_internal (0);
6592 }
6593 }
6594 }
6595
6596
6597 /* Callback function for with_echo_area_buffer, when used from
6598 resize_echo_area_exactly. A1 contains a pointer to the window to
6599 resize, EXACTLY non-nil means resize the mini-window exactly to the
6600 size of the text displayed. A3 and A4 are not used. Value is what
6601 resize_mini_window returns. */
6602
6603 static int
6604 resize_mini_window_1 (a1, exactly, a3, a4)
6605 EMACS_INT a1;
6606 Lisp_Object exactly;
6607 EMACS_INT a3, a4;
6608 {
6609 return resize_mini_window ((struct window *) a1, !NILP (exactly));
6610 }
6611
6612
6613 /* Resize mini-window W to fit the size of its contents. EXACT:P
6614 means size the window exactly to the size needed. Otherwise, it's
6615 only enlarged until W's buffer is empty. Value is non-zero if
6616 the window height has been changed. */
6617
6618 int
6619 resize_mini_window (w, exact_p)
6620 struct window *w;
6621 int exact_p;
6622 {
6623 struct frame *f = XFRAME (w->frame);
6624 int window_height_changed_p = 0;
6625
6626 xassert (MINI_WINDOW_P (w));
6627
6628 /* Don't resize windows while redisplaying a window; it would
6629 confuse redisplay functions when the size of the window they are
6630 displaying changes from under them. Such a resizing can happen,
6631 for instance, when which-func prints a long message while
6632 we are running fontification-functions. We're running these
6633 functions with safe_call which binds inhibit-redisplay to t. */
6634 if (!NILP (Vinhibit_redisplay))
6635 return 0;
6636
6637 /* Nil means don't try to resize. */
6638 if (NILP (Vresize_mini_windows)
6639 || (FRAME_X_P (f) && f->output_data.x == NULL))
6640 return 0;
6641
6642 if (!FRAME_MINIBUF_ONLY_P (f))
6643 {
6644 struct it it;
6645 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
6646 int total_height = XFASTINT (root->height) + XFASTINT (w->height);
6647 int height, max_height;
6648 int unit = CANON_Y_UNIT (f);
6649 struct text_pos start;
6650 struct buffer *old_current_buffer = NULL;
6651
6652 if (current_buffer != XBUFFER (w->buffer))
6653 {
6654 old_current_buffer = current_buffer;
6655 set_buffer_internal (XBUFFER (w->buffer));
6656 }
6657
6658 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
6659
6660 /* Compute the max. number of lines specified by the user. */
6661 if (FLOATP (Vmax_mini_window_height))
6662 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_HEIGHT (f);
6663 else if (INTEGERP (Vmax_mini_window_height))
6664 max_height = XINT (Vmax_mini_window_height);
6665 else
6666 max_height = total_height / 4;
6667
6668 /* Correct that max. height if it's bogus. */
6669 max_height = max (1, max_height);
6670 max_height = min (total_height, max_height);
6671
6672 /* Find out the height of the text in the window. */
6673 if (it.truncate_lines_p)
6674 height = 1;
6675 else
6676 {
6677 last_height = 0;
6678 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
6679 if (it.max_ascent == 0 && it.max_descent == 0)
6680 height = it.current_y + last_height;
6681 else
6682 height = it.current_y + it.max_ascent + it.max_descent;
6683 height -= it.extra_line_spacing;
6684 height = (height + unit - 1) / unit;
6685 }
6686
6687 /* Compute a suitable window start. */
6688 if (height > max_height)
6689 {
6690 height = max_height;
6691 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
6692 move_it_vertically_backward (&it, (height - 1) * unit);
6693 start = it.current.pos;
6694 }
6695 else
6696 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
6697 SET_MARKER_FROM_TEXT_POS (w->start, start);
6698
6699 if (EQ (Vresize_mini_windows, Qgrow_only))
6700 {
6701 /* Let it grow only, until we display an empty message, in which
6702 case the window shrinks again. */
6703 if (height > XFASTINT (w->height))
6704 {
6705 int old_height = XFASTINT (w->height);
6706 freeze_window_starts (f, 1);
6707 grow_mini_window (w, height - XFASTINT (w->height));
6708 window_height_changed_p = XFASTINT (w->height) != old_height;
6709 }
6710 else if (height < XFASTINT (w->height)
6711 && (exact_p || BEGV == ZV))
6712 {
6713 int old_height = XFASTINT (w->height);
6714 freeze_window_starts (f, 0);
6715 shrink_mini_window (w);
6716 window_height_changed_p = XFASTINT (w->height) != old_height;
6717 }
6718 }
6719 else
6720 {
6721 /* Always resize to exact size needed. */
6722 if (height > XFASTINT (w->height))
6723 {
6724 int old_height = XFASTINT (w->height);
6725 freeze_window_starts (f, 1);
6726 grow_mini_window (w, height - XFASTINT (w->height));
6727 window_height_changed_p = XFASTINT (w->height) != old_height;
6728 }
6729 else if (height < XFASTINT (w->height))
6730 {
6731 int old_height = XFASTINT (w->height);
6732 freeze_window_starts (f, 0);
6733 shrink_mini_window (w);
6734
6735 if (height)
6736 {
6737 freeze_window_starts (f, 1);
6738 grow_mini_window (w, height - XFASTINT (w->height));
6739 }
6740
6741 window_height_changed_p = XFASTINT (w->height) != old_height;
6742 }
6743 }
6744
6745 if (old_current_buffer)
6746 set_buffer_internal (old_current_buffer);
6747 }
6748
6749 return window_height_changed_p;
6750 }
6751
6752
6753 /* Value is the current message, a string, or nil if there is no
6754 current message. */
6755
6756 Lisp_Object
6757 current_message ()
6758 {
6759 Lisp_Object msg;
6760
6761 if (NILP (echo_area_buffer[0]))
6762 msg = Qnil;
6763 else
6764 {
6765 with_echo_area_buffer (0, 0, current_message_1,
6766 (EMACS_INT) &msg, Qnil, 0, 0);
6767 if (NILP (msg))
6768 echo_area_buffer[0] = Qnil;
6769 }
6770
6771 return msg;
6772 }
6773
6774
6775 static int
6776 current_message_1 (a1, a2, a3, a4)
6777 EMACS_INT a1;
6778 Lisp_Object a2;
6779 EMACS_INT a3, a4;
6780 {
6781 Lisp_Object *msg = (Lisp_Object *) a1;
6782
6783 if (Z > BEG)
6784 *msg = make_buffer_string (BEG, Z, 1);
6785 else
6786 *msg = Qnil;
6787 return 0;
6788 }
6789
6790
6791 /* Push the current message on Vmessage_stack for later restauration
6792 by restore_message. Value is non-zero if the current message isn't
6793 empty. This is a relatively infrequent operation, so it's not
6794 worth optimizing. */
6795
6796 int
6797 push_message ()
6798 {
6799 Lisp_Object msg;
6800 msg = current_message ();
6801 Vmessage_stack = Fcons (msg, Vmessage_stack);
6802 return STRINGP (msg);
6803 }
6804
6805
6806 /* Handler for record_unwind_protect calling pop_message. */
6807
6808 Lisp_Object
6809 push_message_unwind (dummy)
6810 Lisp_Object dummy;
6811 {
6812 pop_message ();
6813 return Qnil;
6814 }
6815
6816
6817 /* Restore message display from the top of Vmessage_stack. */
6818
6819 void
6820 restore_message ()
6821 {
6822 Lisp_Object msg;
6823
6824 xassert (CONSP (Vmessage_stack));
6825 msg = XCAR (Vmessage_stack);
6826 if (STRINGP (msg))
6827 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
6828 else
6829 message3_nolog (msg, 0, 0);
6830 }
6831
6832
6833 /* Pop the top-most entry off Vmessage_stack. */
6834
6835 void
6836 pop_message ()
6837 {
6838 xassert (CONSP (Vmessage_stack));
6839 Vmessage_stack = XCDR (Vmessage_stack);
6840 }
6841
6842
6843 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
6844 exits. If the stack is not empty, we have a missing pop_message
6845 somewhere. */
6846
6847 void
6848 check_message_stack ()
6849 {
6850 if (!NILP (Vmessage_stack))
6851 abort ();
6852 }
6853
6854
6855 /* Truncate to NCHARS what will be displayed in the echo area the next
6856 time we display it---but don't redisplay it now. */
6857
6858 void
6859 truncate_echo_area (nchars)
6860 int nchars;
6861 {
6862 if (nchars == 0)
6863 echo_area_buffer[0] = Qnil;
6864 /* A null message buffer means that the frame hasn't really been
6865 initialized yet. Error messages get reported properly by
6866 cmd_error, so this must be just an informative message; toss it. */
6867 else if (!noninteractive
6868 && INTERACTIVE
6869 && !NILP (echo_area_buffer[0]))
6870 {
6871 struct frame *sf = SELECTED_FRAME ();
6872 if (FRAME_MESSAGE_BUF (sf))
6873 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
6874 }
6875 }
6876
6877
6878 /* Helper function for truncate_echo_area. Truncate the current
6879 message to at most NCHARS characters. */
6880
6881 static int
6882 truncate_message_1 (nchars, a2, a3, a4)
6883 EMACS_INT nchars;
6884 Lisp_Object a2;
6885 EMACS_INT a3, a4;
6886 {
6887 if (BEG + nchars < Z)
6888 del_range (BEG + nchars, Z);
6889 if (Z == BEG)
6890 echo_area_buffer[0] = Qnil;
6891 return 0;
6892 }
6893
6894
6895 /* Set the current message to a substring of S or STRING.
6896
6897 If STRING is a Lisp string, set the message to the first NBYTES
6898 bytes from STRING. NBYTES zero means use the whole string. If
6899 STRING is multibyte, the message will be displayed multibyte.
6900
6901 If S is not null, set the message to the first LEN bytes of S. LEN
6902 zero means use the whole string. MULTIBYTE_P non-zero means S is
6903 multibyte. Display the message multibyte in that case. */
6904
6905 void
6906 set_message (s, string, nbytes, multibyte_p)
6907 char *s;
6908 Lisp_Object string;
6909 int nbytes;
6910 {
6911 message_enable_multibyte
6912 = ((s && multibyte_p)
6913 || (STRINGP (string) && STRING_MULTIBYTE (string)));
6914
6915 with_echo_area_buffer (0, -1, set_message_1,
6916 (EMACS_INT) s, string, nbytes, multibyte_p);
6917 message_buf_print = 0;
6918 help_echo_showing_p = 0;
6919 }
6920
6921
6922 /* Helper function for set_message. Arguments have the same meaning
6923 as there, with A1 corresponding to S and A2 corresponding to STRING
6924 This function is called with the echo area buffer being
6925 current. */
6926
6927 static int
6928 set_message_1 (a1, a2, nbytes, multibyte_p)
6929 EMACS_INT a1;
6930 Lisp_Object a2;
6931 EMACS_INT nbytes, multibyte_p;
6932 {
6933 char *s = (char *) a1;
6934 Lisp_Object string = a2;
6935
6936 xassert (BEG == Z);
6937
6938 /* Change multibyteness of the echo buffer appropriately. */
6939 if (message_enable_multibyte
6940 != !NILP (current_buffer->enable_multibyte_characters))
6941 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
6942
6943 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
6944
6945 /* Insert new message at BEG. */
6946 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6947
6948 if (STRINGP (string))
6949 {
6950 int nchars;
6951
6952 if (nbytes == 0)
6953 nbytes = SBYTES (string);
6954 nchars = string_byte_to_char (string, nbytes);
6955
6956 /* This function takes care of single/multibyte conversion. We
6957 just have to ensure that the echo area buffer has the right
6958 setting of enable_multibyte_characters. */
6959 insert_from_string (string, 0, 0, nchars, nbytes, 1);
6960 }
6961 else if (s)
6962 {
6963 if (nbytes == 0)
6964 nbytes = strlen (s);
6965
6966 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
6967 {
6968 /* Convert from multi-byte to single-byte. */
6969 int i, c, n;
6970 unsigned char work[1];
6971
6972 /* Convert a multibyte string to single-byte. */
6973 for (i = 0; i < nbytes; i += n)
6974 {
6975 c = string_char_and_length (s + i, nbytes - i, &n);
6976 work[0] = (SINGLE_BYTE_CHAR_P (c)
6977 ? c
6978 : multibyte_char_to_unibyte (c, Qnil));
6979 insert_1_both (work, 1, 1, 1, 0, 0);
6980 }
6981 }
6982 else if (!multibyte_p
6983 && !NILP (current_buffer->enable_multibyte_characters))
6984 {
6985 /* Convert from single-byte to multi-byte. */
6986 int i, c, n;
6987 unsigned char *msg = (unsigned char *) s;
6988 unsigned char str[MAX_MULTIBYTE_LENGTH];
6989
6990 /* Convert a single-byte string to multibyte. */
6991 for (i = 0; i < nbytes; i++)
6992 {
6993 c = unibyte_char_to_multibyte (msg[i]);
6994 n = CHAR_STRING (c, str);
6995 insert_1_both (str, 1, n, 1, 0, 0);
6996 }
6997 }
6998 else
6999 insert_1 (s, nbytes, 1, 0, 0);
7000 }
7001
7002 return 0;
7003 }
7004
7005
7006 /* Clear messages. CURRENT_P non-zero means clear the current
7007 message. LAST_DISPLAYED_P non-zero means clear the message
7008 last displayed. */
7009
7010 void
7011 clear_message (current_p, last_displayed_p)
7012 int current_p, last_displayed_p;
7013 {
7014 if (current_p)
7015 {
7016 echo_area_buffer[0] = Qnil;
7017 message_cleared_p = 1;
7018 }
7019
7020 if (last_displayed_p)
7021 echo_area_buffer[1] = Qnil;
7022
7023 message_buf_print = 0;
7024 }
7025
7026 /* Clear garbaged frames.
7027
7028 This function is used where the old redisplay called
7029 redraw_garbaged_frames which in turn called redraw_frame which in
7030 turn called clear_frame. The call to clear_frame was a source of
7031 flickering. I believe a clear_frame is not necessary. It should
7032 suffice in the new redisplay to invalidate all current matrices,
7033 and ensure a complete redisplay of all windows. */
7034
7035 static void
7036 clear_garbaged_frames ()
7037 {
7038 if (frame_garbaged)
7039 {
7040 Lisp_Object tail, frame;
7041 int changed_count = 0;
7042
7043 FOR_EACH_FRAME (tail, frame)
7044 {
7045 struct frame *f = XFRAME (frame);
7046
7047 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
7048 {
7049 if (f->resized_p)
7050 Fredraw_frame (frame);
7051 clear_current_matrices (f);
7052 changed_count++;
7053 f->garbaged = 0;
7054 f->resized_p = 0;
7055 }
7056 }
7057
7058 frame_garbaged = 0;
7059 if (changed_count)
7060 ++windows_or_buffers_changed;
7061 }
7062 }
7063
7064
7065 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
7066 is non-zero update selected_frame. Value is non-zero if the
7067 mini-windows height has been changed. */
7068
7069 static int
7070 echo_area_display (update_frame_p)
7071 int update_frame_p;
7072 {
7073 Lisp_Object mini_window;
7074 struct window *w;
7075 struct frame *f;
7076 int window_height_changed_p = 0;
7077 struct frame *sf = SELECTED_FRAME ();
7078
7079 mini_window = FRAME_MINIBUF_WINDOW (sf);
7080 w = XWINDOW (mini_window);
7081 f = XFRAME (WINDOW_FRAME (w));
7082
7083 /* Don't display if frame is invisible or not yet initialized. */
7084 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
7085 return 0;
7086
7087 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
7088 #ifndef MAC_OS8
7089 #ifdef HAVE_WINDOW_SYSTEM
7090 /* When Emacs starts, selected_frame may be a visible terminal
7091 frame, even if we run under a window system. If we let this
7092 through, a message would be displayed on the terminal. */
7093 if (EQ (selected_frame, Vterminal_frame)
7094 && !NILP (Vwindow_system))
7095 return 0;
7096 #endif /* HAVE_WINDOW_SYSTEM */
7097 #endif
7098
7099 /* Redraw garbaged frames. */
7100 if (frame_garbaged)
7101 clear_garbaged_frames ();
7102
7103 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
7104 {
7105 echo_area_window = mini_window;
7106 window_height_changed_p = display_echo_area (w);
7107 w->must_be_updated_p = 1;
7108
7109 /* Update the display, unless called from redisplay_internal.
7110 Also don't update the screen during redisplay itself. The
7111 update will happen at the end of redisplay, and an update
7112 here could cause confusion. */
7113 if (update_frame_p && !redisplaying_p)
7114 {
7115 int n = 0;
7116
7117 /* If the display update has been interrupted by pending
7118 input, update mode lines in the frame. Due to the
7119 pending input, it might have been that redisplay hasn't
7120 been called, so that mode lines above the echo area are
7121 garbaged. This looks odd, so we prevent it here. */
7122 if (!display_completed)
7123 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
7124
7125 if (window_height_changed_p
7126 /* Don't do this if Emacs is shutting down. Redisplay
7127 needs to run hooks. */
7128 && !NILP (Vrun_hooks))
7129 {
7130 /* Must update other windows. Likewise as in other
7131 cases, don't let this update be interrupted by
7132 pending input. */
7133 int count = SPECPDL_INDEX ();
7134 specbind (Qredisplay_dont_pause, Qt);
7135 windows_or_buffers_changed = 1;
7136 redisplay_internal (0);
7137 unbind_to (count, Qnil);
7138 }
7139 else if (FRAME_WINDOW_P (f) && n == 0)
7140 {
7141 /* Window configuration is the same as before.
7142 Can do with a display update of the echo area,
7143 unless we displayed some mode lines. */
7144 update_single_window (w, 1);
7145 rif->flush_display (f);
7146 }
7147 else
7148 update_frame (f, 1, 1);
7149
7150 /* If cursor is in the echo area, make sure that the next
7151 redisplay displays the minibuffer, so that the cursor will
7152 be replaced with what the minibuffer wants. */
7153 if (cursor_in_echo_area)
7154 ++windows_or_buffers_changed;
7155 }
7156 }
7157 else if (!EQ (mini_window, selected_window))
7158 windows_or_buffers_changed++;
7159
7160 /* Last displayed message is now the current message. */
7161 echo_area_buffer[1] = echo_area_buffer[0];
7162
7163 /* Prevent redisplay optimization in redisplay_internal by resetting
7164 this_line_start_pos. This is done because the mini-buffer now
7165 displays the message instead of its buffer text. */
7166 if (EQ (mini_window, selected_window))
7167 CHARPOS (this_line_start_pos) = 0;
7168
7169 return window_height_changed_p;
7170 }
7171
7172
7173 \f
7174 /***********************************************************************
7175 Frame Titles
7176 ***********************************************************************/
7177
7178
7179 /* The frame title buffering code is also used by Fformat_mode_line.
7180 So it is not conditioned by HAVE_WINDOW_SYSTEM. */
7181
7182 /* A buffer for constructing frame titles in it; allocated from the
7183 heap in init_xdisp and resized as needed in store_frame_title_char. */
7184
7185 static char *frame_title_buf;
7186
7187 /* The buffer's end, and a current output position in it. */
7188
7189 static char *frame_title_buf_end;
7190 static char *frame_title_ptr;
7191
7192
7193 /* Store a single character C for the frame title in frame_title_buf.
7194 Re-allocate frame_title_buf if necessary. */
7195
7196 static void
7197 store_frame_title_char (c)
7198 char c;
7199 {
7200 /* If output position has reached the end of the allocated buffer,
7201 double the buffer's size. */
7202 if (frame_title_ptr == frame_title_buf_end)
7203 {
7204 int len = frame_title_ptr - frame_title_buf;
7205 int new_size = 2 * len * sizeof *frame_title_buf;
7206 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
7207 frame_title_buf_end = frame_title_buf + new_size;
7208 frame_title_ptr = frame_title_buf + len;
7209 }
7210
7211 *frame_title_ptr++ = c;
7212 }
7213
7214
7215 /* Store part of a frame title in frame_title_buf, beginning at
7216 frame_title_ptr. STR is the string to store. Do not copy
7217 characters that yield more columns than PRECISION; PRECISION <= 0
7218 means copy the whole string. Pad with spaces until FIELD_WIDTH
7219 number of characters have been copied; FIELD_WIDTH <= 0 means don't
7220 pad. Called from display_mode_element when it is used to build a
7221 frame title. */
7222
7223 static int
7224 store_frame_title (str, field_width, precision)
7225 unsigned char *str;
7226 int field_width, precision;
7227 {
7228 int n = 0;
7229 int dummy, nbytes;
7230
7231 /* Copy at most PRECISION chars from STR. */
7232 nbytes = strlen (str);
7233 n+= c_string_width (str, nbytes, precision, &dummy, &nbytes);
7234 while (nbytes--)
7235 store_frame_title_char (*str++);
7236
7237 /* Fill up with spaces until FIELD_WIDTH reached. */
7238 while (field_width > 0
7239 && n < field_width)
7240 {
7241 store_frame_title_char (' ');
7242 ++n;
7243 }
7244
7245 return n;
7246 }
7247
7248 #ifdef HAVE_WINDOW_SYSTEM
7249
7250 /* Set the title of FRAME, if it has changed. The title format is
7251 Vicon_title_format if FRAME is iconified, otherwise it is
7252 frame_title_format. */
7253
7254 static void
7255 x_consider_frame_title (frame)
7256 Lisp_Object frame;
7257 {
7258 struct frame *f = XFRAME (frame);
7259
7260 if (FRAME_WINDOW_P (f)
7261 || FRAME_MINIBUF_ONLY_P (f)
7262 || f->explicit_name)
7263 {
7264 /* Do we have more than one visible frame on this X display? */
7265 Lisp_Object tail;
7266 Lisp_Object fmt;
7267 struct buffer *obuf;
7268 int len;
7269 struct it it;
7270
7271 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
7272 {
7273 Lisp_Object other_frame = XCAR (tail);
7274 struct frame *tf = XFRAME (other_frame);
7275
7276 if (tf != f
7277 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
7278 && !FRAME_MINIBUF_ONLY_P (tf)
7279 && !EQ (other_frame, tip_frame)
7280 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
7281 break;
7282 }
7283
7284 /* Set global variable indicating that multiple frames exist. */
7285 multiple_frames = CONSP (tail);
7286
7287 /* Switch to the buffer of selected window of the frame. Set up
7288 frame_title_ptr so that display_mode_element will output into it;
7289 then display the title. */
7290 obuf = current_buffer;
7291 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
7292 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
7293 frame_title_ptr = frame_title_buf;
7294 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
7295 NULL, DEFAULT_FACE_ID);
7296 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
7297 len = frame_title_ptr - frame_title_buf;
7298 frame_title_ptr = NULL;
7299 set_buffer_internal_1 (obuf);
7300
7301 /* Set the title only if it's changed. This avoids consing in
7302 the common case where it hasn't. (If it turns out that we've
7303 already wasted too much time by walking through the list with
7304 display_mode_element, then we might need to optimize at a
7305 higher level than this.) */
7306 if (! STRINGP (f->name)
7307 || SBYTES (f->name) != len
7308 || bcmp (frame_title_buf, SDATA (f->name), len) != 0)
7309 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
7310 }
7311 }
7312
7313 #endif /* not HAVE_WINDOW_SYSTEM */
7314
7315
7316
7317 \f
7318 /***********************************************************************
7319 Menu Bars
7320 ***********************************************************************/
7321
7322
7323 /* Prepare for redisplay by updating menu-bar item lists when
7324 appropriate. This can call eval. */
7325
7326 void
7327 prepare_menu_bars ()
7328 {
7329 int all_windows;
7330 struct gcpro gcpro1, gcpro2;
7331 struct frame *f;
7332 Lisp_Object tooltip_frame;
7333
7334 #ifdef HAVE_WINDOW_SYSTEM
7335 tooltip_frame = tip_frame;
7336 #else
7337 tooltip_frame = Qnil;
7338 #endif
7339
7340 /* Update all frame titles based on their buffer names, etc. We do
7341 this before the menu bars so that the buffer-menu will show the
7342 up-to-date frame titles. */
7343 #ifdef HAVE_WINDOW_SYSTEM
7344 if (windows_or_buffers_changed || update_mode_lines)
7345 {
7346 Lisp_Object tail, frame;
7347
7348 FOR_EACH_FRAME (tail, frame)
7349 {
7350 f = XFRAME (frame);
7351 if (!EQ (frame, tooltip_frame)
7352 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
7353 x_consider_frame_title (frame);
7354 }
7355 }
7356 #endif /* HAVE_WINDOW_SYSTEM */
7357
7358 /* Update the menu bar item lists, if appropriate. This has to be
7359 done before any actual redisplay or generation of display lines. */
7360 all_windows = (update_mode_lines
7361 || buffer_shared > 1
7362 || windows_or_buffers_changed);
7363 if (all_windows)
7364 {
7365 Lisp_Object tail, frame;
7366 int count = SPECPDL_INDEX ();
7367
7368 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7369
7370 FOR_EACH_FRAME (tail, frame)
7371 {
7372 f = XFRAME (frame);
7373
7374 /* Ignore tooltip frame. */
7375 if (EQ (frame, tooltip_frame))
7376 continue;
7377
7378 /* If a window on this frame changed size, report that to
7379 the user and clear the size-change flag. */
7380 if (FRAME_WINDOW_SIZES_CHANGED (f))
7381 {
7382 Lisp_Object functions;
7383
7384 /* Clear flag first in case we get an error below. */
7385 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
7386 functions = Vwindow_size_change_functions;
7387 GCPRO2 (tail, functions);
7388
7389 while (CONSP (functions))
7390 {
7391 call1 (XCAR (functions), frame);
7392 functions = XCDR (functions);
7393 }
7394 UNGCPRO;
7395 }
7396
7397 GCPRO1 (tail);
7398 update_menu_bar (f, 0);
7399 #ifdef HAVE_WINDOW_SYSTEM
7400 update_tool_bar (f, 0);
7401 #endif
7402 UNGCPRO;
7403 }
7404
7405 unbind_to (count, Qnil);
7406 }
7407 else
7408 {
7409 struct frame *sf = SELECTED_FRAME ();
7410 update_menu_bar (sf, 1);
7411 #ifdef HAVE_WINDOW_SYSTEM
7412 update_tool_bar (sf, 1);
7413 #endif
7414 }
7415
7416 /* Motif needs this. See comment in xmenu.c. Turn it off when
7417 pending_menu_activation is not defined. */
7418 #ifdef USE_X_TOOLKIT
7419 pending_menu_activation = 0;
7420 #endif
7421 }
7422
7423
7424 /* Update the menu bar item list for frame F. This has to be done
7425 before we start to fill in any display lines, because it can call
7426 eval.
7427
7428 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
7429
7430 static void
7431 update_menu_bar (f, save_match_data)
7432 struct frame *f;
7433 int save_match_data;
7434 {
7435 Lisp_Object window;
7436 register struct window *w;
7437
7438 /* If called recursively during a menu update, do nothing. This can
7439 happen when, for instance, an activate-menubar-hook causes a
7440 redisplay. */
7441 if (inhibit_menubar_update)
7442 return;
7443
7444 window = FRAME_SELECTED_WINDOW (f);
7445 w = XWINDOW (window);
7446
7447 #if 0 /* The if statement below this if statement used to include the
7448 condition !NILP (w->update_mode_line), rather than using
7449 update_mode_lines directly, and this if statement may have
7450 been added to make that condition work. Now the if
7451 statement below matches its comment, this isn't needed. */
7452 if (update_mode_lines)
7453 w->update_mode_line = Qt;
7454 #endif
7455
7456 if (FRAME_WINDOW_P (f)
7457 ?
7458 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS)
7459 FRAME_EXTERNAL_MENU_BAR (f)
7460 #else
7461 FRAME_MENU_BAR_LINES (f) > 0
7462 #endif
7463 : FRAME_MENU_BAR_LINES (f) > 0)
7464 {
7465 /* If the user has switched buffers or windows, we need to
7466 recompute to reflect the new bindings. But we'll
7467 recompute when update_mode_lines is set too; that means
7468 that people can use force-mode-line-update to request
7469 that the menu bar be recomputed. The adverse effect on
7470 the rest of the redisplay algorithm is about the same as
7471 windows_or_buffers_changed anyway. */
7472 if (windows_or_buffers_changed
7473 /* This used to test w->update_mode_line, but we believe
7474 there is no need to recompute the menu in that case. */
7475 || update_mode_lines
7476 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7477 < BUF_MODIFF (XBUFFER (w->buffer)))
7478 != !NILP (w->last_had_star))
7479 || ((!NILP (Vtransient_mark_mode)
7480 && !NILP (XBUFFER (w->buffer)->mark_active))
7481 != !NILP (w->region_showing)))
7482 {
7483 struct buffer *prev = current_buffer;
7484 int count = SPECPDL_INDEX ();
7485
7486 specbind (Qinhibit_menubar_update, Qt);
7487
7488 set_buffer_internal_1 (XBUFFER (w->buffer));
7489 if (save_match_data)
7490 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7491 if (NILP (Voverriding_local_map_menu_flag))
7492 {
7493 specbind (Qoverriding_terminal_local_map, Qnil);
7494 specbind (Qoverriding_local_map, Qnil);
7495 }
7496
7497 /* Run the Lucid hook. */
7498 safe_run_hooks (Qactivate_menubar_hook);
7499
7500 /* If it has changed current-menubar from previous value,
7501 really recompute the menu-bar from the value. */
7502 if (! NILP (Vlucid_menu_bar_dirty_flag))
7503 call0 (Qrecompute_lucid_menubar);
7504
7505 safe_run_hooks (Qmenu_bar_update_hook);
7506 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
7507
7508 /* Redisplay the menu bar in case we changed it. */
7509 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS)
7510 if (FRAME_WINDOW_P (f)
7511 #if defined (MAC_OS)
7512 /* All frames on Mac OS share the same menubar. So only the
7513 selected frame should be allowed to set it. */
7514 && f == SELECTED_FRAME ()
7515 #endif
7516 )
7517 set_frame_menubar (f, 0, 0);
7518 else
7519 /* On a terminal screen, the menu bar is an ordinary screen
7520 line, and this makes it get updated. */
7521 w->update_mode_line = Qt;
7522 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7523 /* In the non-toolkit version, the menu bar is an ordinary screen
7524 line, and this makes it get updated. */
7525 w->update_mode_line = Qt;
7526 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7527
7528 unbind_to (count, Qnil);
7529 set_buffer_internal_1 (prev);
7530 }
7531 }
7532 }
7533
7534
7535 \f
7536 /***********************************************************************
7537 Tool-bars
7538 ***********************************************************************/
7539
7540 #ifdef HAVE_WINDOW_SYSTEM
7541
7542 /* Update the tool-bar item list for frame F. This has to be done
7543 before we start to fill in any display lines. Called from
7544 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
7545 and restore it here. */
7546
7547 static void
7548 update_tool_bar (f, save_match_data)
7549 struct frame *f;
7550 int save_match_data;
7551 {
7552 if (WINDOWP (f->tool_bar_window)
7553 && XFASTINT (XWINDOW (f->tool_bar_window)->height) > 0)
7554 {
7555 Lisp_Object window;
7556 struct window *w;
7557
7558 window = FRAME_SELECTED_WINDOW (f);
7559 w = XWINDOW (window);
7560
7561 /* If the user has switched buffers or windows, we need to
7562 recompute to reflect the new bindings. But we'll
7563 recompute when update_mode_lines is set too; that means
7564 that people can use force-mode-line-update to request
7565 that the menu bar be recomputed. The adverse effect on
7566 the rest of the redisplay algorithm is about the same as
7567 windows_or_buffers_changed anyway. */
7568 if (windows_or_buffers_changed
7569 || !NILP (w->update_mode_line)
7570 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7571 < BUF_MODIFF (XBUFFER (w->buffer)))
7572 != !NILP (w->last_had_star))
7573 || ((!NILP (Vtransient_mark_mode)
7574 && !NILP (XBUFFER (w->buffer)->mark_active))
7575 != !NILP (w->region_showing)))
7576 {
7577 struct buffer *prev = current_buffer;
7578 int count = SPECPDL_INDEX ();
7579
7580 /* Set current_buffer to the buffer of the selected
7581 window of the frame, so that we get the right local
7582 keymaps. */
7583 set_buffer_internal_1 (XBUFFER (w->buffer));
7584
7585 /* Save match data, if we must. */
7586 if (save_match_data)
7587 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7588
7589 /* Make sure that we don't accidentally use bogus keymaps. */
7590 if (NILP (Voverriding_local_map_menu_flag))
7591 {
7592 specbind (Qoverriding_terminal_local_map, Qnil);
7593 specbind (Qoverriding_local_map, Qnil);
7594 }
7595
7596 /* Build desired tool-bar items from keymaps. */
7597 f->tool_bar_items
7598 = tool_bar_items (f->tool_bar_items, &f->n_tool_bar_items);
7599
7600 /* Redisplay the tool-bar in case we changed it. */
7601 w->update_mode_line = Qt;
7602
7603 unbind_to (count, Qnil);
7604 set_buffer_internal_1 (prev);
7605 }
7606 }
7607 }
7608
7609
7610 /* Set F->desired_tool_bar_string to a Lisp string representing frame
7611 F's desired tool-bar contents. F->tool_bar_items must have
7612 been set up previously by calling prepare_menu_bars. */
7613
7614 static void
7615 build_desired_tool_bar_string (f)
7616 struct frame *f;
7617 {
7618 int i, size, size_needed;
7619 struct gcpro gcpro1, gcpro2, gcpro3;
7620 Lisp_Object image, plist, props;
7621
7622 image = plist = props = Qnil;
7623 GCPRO3 (image, plist, props);
7624
7625 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
7626 Otherwise, make a new string. */
7627
7628 /* The size of the string we might be able to reuse. */
7629 size = (STRINGP (f->desired_tool_bar_string)
7630 ? SCHARS (f->desired_tool_bar_string)
7631 : 0);
7632
7633 /* We need one space in the string for each image. */
7634 size_needed = f->n_tool_bar_items;
7635
7636 /* Reuse f->desired_tool_bar_string, if possible. */
7637 if (size < size_needed || NILP (f->desired_tool_bar_string))
7638 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
7639 make_number (' '));
7640 else
7641 {
7642 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
7643 Fremove_text_properties (make_number (0), make_number (size),
7644 props, f->desired_tool_bar_string);
7645 }
7646
7647 /* Put a `display' property on the string for the images to display,
7648 put a `menu_item' property on tool-bar items with a value that
7649 is the index of the item in F's tool-bar item vector. */
7650 for (i = 0; i < f->n_tool_bar_items; ++i)
7651 {
7652 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
7653
7654 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
7655 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
7656 int hmargin, vmargin, relief, idx, end;
7657 extern Lisp_Object QCrelief, QCmargin, QCconversion, Qimage;
7658
7659 /* If image is a vector, choose the image according to the
7660 button state. */
7661 image = PROP (TOOL_BAR_ITEM_IMAGES);
7662 if (VECTORP (image))
7663 {
7664 if (enabled_p)
7665 idx = (selected_p
7666 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
7667 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
7668 else
7669 idx = (selected_p
7670 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
7671 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
7672
7673 xassert (ASIZE (image) >= idx);
7674 image = AREF (image, idx);
7675 }
7676 else
7677 idx = -1;
7678
7679 /* Ignore invalid image specifications. */
7680 if (!valid_image_p (image))
7681 continue;
7682
7683 /* Display the tool-bar button pressed, or depressed. */
7684 plist = Fcopy_sequence (XCDR (image));
7685
7686 /* Compute margin and relief to draw. */
7687 relief = (tool_bar_button_relief >= 0
7688 ? tool_bar_button_relief
7689 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
7690 hmargin = vmargin = relief;
7691
7692 if (INTEGERP (Vtool_bar_button_margin)
7693 && XINT (Vtool_bar_button_margin) > 0)
7694 {
7695 hmargin += XFASTINT (Vtool_bar_button_margin);
7696 vmargin += XFASTINT (Vtool_bar_button_margin);
7697 }
7698 else if (CONSP (Vtool_bar_button_margin))
7699 {
7700 if (INTEGERP (XCAR (Vtool_bar_button_margin))
7701 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
7702 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
7703
7704 if (INTEGERP (XCDR (Vtool_bar_button_margin))
7705 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
7706 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
7707 }
7708
7709 if (auto_raise_tool_bar_buttons_p)
7710 {
7711 /* Add a `:relief' property to the image spec if the item is
7712 selected. */
7713 if (selected_p)
7714 {
7715 plist = Fplist_put (plist, QCrelief, make_number (-relief));
7716 hmargin -= relief;
7717 vmargin -= relief;
7718 }
7719 }
7720 else
7721 {
7722 /* If image is selected, display it pressed, i.e. with a
7723 negative relief. If it's not selected, display it with a
7724 raised relief. */
7725 plist = Fplist_put (plist, QCrelief,
7726 (selected_p
7727 ? make_number (-relief)
7728 : make_number (relief)));
7729 hmargin -= relief;
7730 vmargin -= relief;
7731 }
7732
7733 /* Put a margin around the image. */
7734 if (hmargin || vmargin)
7735 {
7736 if (hmargin == vmargin)
7737 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
7738 else
7739 plist = Fplist_put (plist, QCmargin,
7740 Fcons (make_number (hmargin),
7741 make_number (vmargin)));
7742 }
7743
7744 /* If button is not enabled, and we don't have special images
7745 for the disabled state, make the image appear disabled by
7746 applying an appropriate algorithm to it. */
7747 if (!enabled_p && idx < 0)
7748 plist = Fplist_put (plist, QCconversion, Qdisabled);
7749
7750 /* Put a `display' text property on the string for the image to
7751 display. Put a `menu-item' property on the string that gives
7752 the start of this item's properties in the tool-bar items
7753 vector. */
7754 image = Fcons (Qimage, plist);
7755 props = list4 (Qdisplay, image,
7756 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
7757
7758 /* Let the last image hide all remaining spaces in the tool bar
7759 string. The string can be longer than needed when we reuse a
7760 previous string. */
7761 if (i + 1 == f->n_tool_bar_items)
7762 end = SCHARS (f->desired_tool_bar_string);
7763 else
7764 end = i + 1;
7765 Fadd_text_properties (make_number (i), make_number (end),
7766 props, f->desired_tool_bar_string);
7767 #undef PROP
7768 }
7769
7770 UNGCPRO;
7771 }
7772
7773
7774 /* Display one line of the tool-bar of frame IT->f. */
7775
7776 static void
7777 display_tool_bar_line (it)
7778 struct it *it;
7779 {
7780 struct glyph_row *row = it->glyph_row;
7781 int max_x = it->last_visible_x;
7782 struct glyph *last;
7783
7784 prepare_desired_row (row);
7785 row->y = it->current_y;
7786
7787 /* Note that this isn't made use of if the face hasn't a box,
7788 so there's no need to check the face here. */
7789 it->start_of_box_run_p = 1;
7790
7791 while (it->current_x < max_x)
7792 {
7793 int x_before, x, n_glyphs_before, i, nglyphs;
7794
7795 /* Get the next display element. */
7796 if (!get_next_display_element (it))
7797 break;
7798
7799 /* Produce glyphs. */
7800 x_before = it->current_x;
7801 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
7802 PRODUCE_GLYPHS (it);
7803
7804 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
7805 i = 0;
7806 x = x_before;
7807 while (i < nglyphs)
7808 {
7809 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
7810
7811 if (x + glyph->pixel_width > max_x)
7812 {
7813 /* Glyph doesn't fit on line. */
7814 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
7815 it->current_x = x;
7816 goto out;
7817 }
7818
7819 ++it->hpos;
7820 x += glyph->pixel_width;
7821 ++i;
7822 }
7823
7824 /* Stop at line ends. */
7825 if (ITERATOR_AT_END_OF_LINE_P (it))
7826 break;
7827
7828 set_iterator_to_next (it, 1);
7829 }
7830
7831 out:;
7832
7833 row->displays_text_p = row->used[TEXT_AREA] != 0;
7834 extend_face_to_end_of_line (it);
7835 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
7836 last->right_box_line_p = 1;
7837 if (last == row->glyphs[TEXT_AREA])
7838 last->left_box_line_p = 1;
7839 compute_line_metrics (it);
7840
7841 /* If line is empty, make it occupy the rest of the tool-bar. */
7842 if (!row->displays_text_p)
7843 {
7844 row->height = row->phys_height = it->last_visible_y - row->y;
7845 row->ascent = row->phys_ascent = 0;
7846 }
7847
7848 row->full_width_p = 1;
7849 row->continued_p = 0;
7850 row->truncated_on_left_p = 0;
7851 row->truncated_on_right_p = 0;
7852
7853 it->current_x = it->hpos = 0;
7854 it->current_y += row->height;
7855 ++it->vpos;
7856 ++it->glyph_row;
7857 }
7858
7859
7860 /* Value is the number of screen lines needed to make all tool-bar
7861 items of frame F visible. */
7862
7863 static int
7864 tool_bar_lines_needed (f)
7865 struct frame *f;
7866 {
7867 struct window *w = XWINDOW (f->tool_bar_window);
7868 struct it it;
7869
7870 /* Initialize an iterator for iteration over
7871 F->desired_tool_bar_string in the tool-bar window of frame F. */
7872 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7873 it.first_visible_x = 0;
7874 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7875 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7876
7877 while (!ITERATOR_AT_END_P (&it))
7878 {
7879 it.glyph_row = w->desired_matrix->rows;
7880 clear_glyph_row (it.glyph_row);
7881 display_tool_bar_line (&it);
7882 }
7883
7884 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
7885 }
7886
7887
7888 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
7889 0, 1, 0,
7890 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
7891 (frame)
7892 Lisp_Object frame;
7893 {
7894 struct frame *f;
7895 struct window *w;
7896 int nlines = 0;
7897
7898 if (NILP (frame))
7899 frame = selected_frame;
7900 else
7901 CHECK_FRAME (frame);
7902 f = XFRAME (frame);
7903
7904 if (WINDOWP (f->tool_bar_window)
7905 || (w = XWINDOW (f->tool_bar_window),
7906 XFASTINT (w->height) > 0))
7907 {
7908 update_tool_bar (f, 1);
7909 if (f->n_tool_bar_items)
7910 {
7911 build_desired_tool_bar_string (f);
7912 nlines = tool_bar_lines_needed (f);
7913 }
7914 }
7915
7916 return make_number (nlines);
7917 }
7918
7919
7920 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
7921 height should be changed. */
7922
7923 static int
7924 redisplay_tool_bar (f)
7925 struct frame *f;
7926 {
7927 struct window *w;
7928 struct it it;
7929 struct glyph_row *row;
7930 int change_height_p = 0;
7931
7932 /* If frame hasn't a tool-bar window or if it is zero-height, don't
7933 do anything. This means you must start with tool-bar-lines
7934 non-zero to get the auto-sizing effect. Or in other words, you
7935 can turn off tool-bars by specifying tool-bar-lines zero. */
7936 if (!WINDOWP (f->tool_bar_window)
7937 || (w = XWINDOW (f->tool_bar_window),
7938 XFASTINT (w->height) == 0))
7939 return 0;
7940
7941 /* Set up an iterator for the tool-bar window. */
7942 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7943 it.first_visible_x = 0;
7944 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7945 row = it.glyph_row;
7946
7947 /* Build a string that represents the contents of the tool-bar. */
7948 build_desired_tool_bar_string (f);
7949 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7950
7951 /* Display as many lines as needed to display all tool-bar items. */
7952 while (it.current_y < it.last_visible_y)
7953 display_tool_bar_line (&it);
7954
7955 /* It doesn't make much sense to try scrolling in the tool-bar
7956 window, so don't do it. */
7957 w->desired_matrix->no_scrolling_p = 1;
7958 w->must_be_updated_p = 1;
7959
7960 if (auto_resize_tool_bars_p)
7961 {
7962 int nlines;
7963
7964 /* If we couldn't display everything, change the tool-bar's
7965 height. */
7966 if (IT_STRING_CHARPOS (it) < it.end_charpos)
7967 change_height_p = 1;
7968
7969 /* If there are blank lines at the end, except for a partially
7970 visible blank line at the end that is smaller than
7971 CANON_Y_UNIT, change the tool-bar's height. */
7972 row = it.glyph_row - 1;
7973 if (!row->displays_text_p
7974 && row->height >= CANON_Y_UNIT (f))
7975 change_height_p = 1;
7976
7977 /* If row displays tool-bar items, but is partially visible,
7978 change the tool-bar's height. */
7979 if (row->displays_text_p
7980 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
7981 change_height_p = 1;
7982
7983 /* Resize windows as needed by changing the `tool-bar-lines'
7984 frame parameter. */
7985 if (change_height_p
7986 && (nlines = tool_bar_lines_needed (f),
7987 nlines != XFASTINT (w->height)))
7988 {
7989 extern Lisp_Object Qtool_bar_lines;
7990 Lisp_Object frame;
7991 int old_height = XFASTINT (w->height);
7992
7993 XSETFRAME (frame, f);
7994 clear_glyph_matrix (w->desired_matrix);
7995 Fmodify_frame_parameters (frame,
7996 Fcons (Fcons (Qtool_bar_lines,
7997 make_number (nlines)),
7998 Qnil));
7999 if (XFASTINT (w->height) != old_height)
8000 fonts_changed_p = 1;
8001 }
8002 }
8003
8004 return change_height_p;
8005 }
8006
8007
8008 /* Get information about the tool-bar item which is displayed in GLYPH
8009 on frame F. Return in *PROP_IDX the index where tool-bar item
8010 properties start in F->tool_bar_items. Value is zero if
8011 GLYPH doesn't display a tool-bar item. */
8012
8013 int
8014 tool_bar_item_info (f, glyph, prop_idx)
8015 struct frame *f;
8016 struct glyph *glyph;
8017 int *prop_idx;
8018 {
8019 Lisp_Object prop;
8020 int success_p;
8021 int charpos;
8022
8023 /* This function can be called asynchronously, which means we must
8024 exclude any possibility that Fget_text_property signals an
8025 error. */
8026 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
8027 charpos = max (0, charpos);
8028
8029 /* Get the text property `menu-item' at pos. The value of that
8030 property is the start index of this item's properties in
8031 F->tool_bar_items. */
8032 prop = Fget_text_property (make_number (charpos),
8033 Qmenu_item, f->current_tool_bar_string);
8034 if (INTEGERP (prop))
8035 {
8036 *prop_idx = XINT (prop);
8037 success_p = 1;
8038 }
8039 else
8040 success_p = 0;
8041
8042 return success_p;
8043 }
8044
8045 #endif /* HAVE_WINDOW_SYSTEM */
8046
8047
8048 \f
8049 /************************************************************************
8050 Horizontal scrolling
8051 ************************************************************************/
8052
8053 static int hscroll_window_tree P_ ((Lisp_Object));
8054 static int hscroll_windows P_ ((Lisp_Object));
8055
8056 /* For all leaf windows in the window tree rooted at WINDOW, set their
8057 hscroll value so that PT is (i) visible in the window, and (ii) so
8058 that it is not within a certain margin at the window's left and
8059 right border. Value is non-zero if any window's hscroll has been
8060 changed. */
8061
8062 static int
8063 hscroll_window_tree (window)
8064 Lisp_Object window;
8065 {
8066 int hscrolled_p = 0;
8067 int hscroll_relative_p = FLOATP (Vhscroll_step);
8068 int hscroll_step_abs = 0;
8069 double hscroll_step_rel = 0;
8070
8071 if (hscroll_relative_p)
8072 {
8073 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
8074 if (hscroll_step_rel < 0)
8075 {
8076 hscroll_relative_p = 0;
8077 hscroll_step_abs = 0;
8078 }
8079 }
8080 else if (INTEGERP (Vhscroll_step))
8081 {
8082 hscroll_step_abs = XINT (Vhscroll_step);
8083 if (hscroll_step_abs < 0)
8084 hscroll_step_abs = 0;
8085 }
8086 else
8087 hscroll_step_abs = 0;
8088
8089 while (WINDOWP (window))
8090 {
8091 struct window *w = XWINDOW (window);
8092
8093 if (WINDOWP (w->hchild))
8094 hscrolled_p |= hscroll_window_tree (w->hchild);
8095 else if (WINDOWP (w->vchild))
8096 hscrolled_p |= hscroll_window_tree (w->vchild);
8097 else if (w->cursor.vpos >= 0)
8098 {
8099 int h_margin, text_area_x, text_area_y;
8100 int text_area_width, text_area_height;
8101 struct glyph_row *current_cursor_row
8102 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
8103 struct glyph_row *desired_cursor_row
8104 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
8105 struct glyph_row *cursor_row
8106 = (desired_cursor_row->enabled_p
8107 ? desired_cursor_row
8108 : current_cursor_row);
8109
8110 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
8111 &text_area_width, &text_area_height);
8112
8113 /* Scroll when cursor is inside this scroll margin. */
8114 h_margin = hscroll_margin * CANON_X_UNIT (XFRAME (w->frame));
8115
8116 if ((XFASTINT (w->hscroll)
8117 && w->cursor.x <= h_margin)
8118 || (cursor_row->enabled_p
8119 && cursor_row->truncated_on_right_p
8120 && (w->cursor.x >= text_area_width - h_margin)))
8121 {
8122 struct it it;
8123 int hscroll;
8124 struct buffer *saved_current_buffer;
8125 int pt;
8126 int wanted_x;
8127
8128 /* Find point in a display of infinite width. */
8129 saved_current_buffer = current_buffer;
8130 current_buffer = XBUFFER (w->buffer);
8131
8132 if (w == XWINDOW (selected_window))
8133 pt = BUF_PT (current_buffer);
8134 else
8135 {
8136 pt = marker_position (w->pointm);
8137 pt = max (BEGV, pt);
8138 pt = min (ZV, pt);
8139 }
8140
8141 /* Move iterator to pt starting at cursor_row->start in
8142 a line with infinite width. */
8143 init_to_row_start (&it, w, cursor_row);
8144 it.last_visible_x = INFINITY;
8145 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
8146 current_buffer = saved_current_buffer;
8147
8148 /* Position cursor in window. */
8149 if (!hscroll_relative_p && hscroll_step_abs == 0)
8150 hscroll = max (0, it.current_x - text_area_width / 2)
8151 / CANON_X_UNIT (it.f);
8152 else if (w->cursor.x >= text_area_width - h_margin)
8153 {
8154 if (hscroll_relative_p)
8155 wanted_x = text_area_width * (1 - hscroll_step_rel)
8156 - h_margin;
8157 else
8158 wanted_x = text_area_width
8159 - hscroll_step_abs * CANON_X_UNIT (it.f)
8160 - h_margin;
8161 hscroll
8162 = max (0, it.current_x - wanted_x) / CANON_X_UNIT (it.f);
8163 }
8164 else
8165 {
8166 if (hscroll_relative_p)
8167 wanted_x = text_area_width * hscroll_step_rel
8168 + h_margin;
8169 else
8170 wanted_x = hscroll_step_abs * CANON_X_UNIT (it.f)
8171 + h_margin;
8172 hscroll
8173 = max (0, it.current_x - wanted_x) / CANON_X_UNIT (it.f);
8174 }
8175 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
8176
8177 /* Don't call Fset_window_hscroll if value hasn't
8178 changed because it will prevent redisplay
8179 optimizations. */
8180 if (XFASTINT (w->hscroll) != hscroll)
8181 {
8182 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
8183 w->hscroll = make_number (hscroll);
8184 hscrolled_p = 1;
8185 }
8186 }
8187 }
8188
8189 window = w->next;
8190 }
8191
8192 /* Value is non-zero if hscroll of any leaf window has been changed. */
8193 return hscrolled_p;
8194 }
8195
8196
8197 /* Set hscroll so that cursor is visible and not inside horizontal
8198 scroll margins for all windows in the tree rooted at WINDOW. See
8199 also hscroll_window_tree above. Value is non-zero if any window's
8200 hscroll has been changed. If it has, desired matrices on the frame
8201 of WINDOW are cleared. */
8202
8203 static int
8204 hscroll_windows (window)
8205 Lisp_Object window;
8206 {
8207 int hscrolled_p;
8208
8209 if (automatic_hscrolling_p)
8210 {
8211 hscrolled_p = hscroll_window_tree (window);
8212 if (hscrolled_p)
8213 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
8214 }
8215 else
8216 hscrolled_p = 0;
8217 return hscrolled_p;
8218 }
8219
8220
8221 \f
8222 /************************************************************************
8223 Redisplay
8224 ************************************************************************/
8225
8226 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
8227 to a non-zero value. This is sometimes handy to have in a debugger
8228 session. */
8229
8230 #if GLYPH_DEBUG
8231
8232 /* First and last unchanged row for try_window_id. */
8233
8234 int debug_first_unchanged_at_end_vpos;
8235 int debug_last_unchanged_at_beg_vpos;
8236
8237 /* Delta vpos and y. */
8238
8239 int debug_dvpos, debug_dy;
8240
8241 /* Delta in characters and bytes for try_window_id. */
8242
8243 int debug_delta, debug_delta_bytes;
8244
8245 /* Values of window_end_pos and window_end_vpos at the end of
8246 try_window_id. */
8247
8248 EMACS_INT debug_end_pos, debug_end_vpos;
8249
8250 /* Append a string to W->desired_matrix->method. FMT is a printf
8251 format string. A1...A9 are a supplement for a variable-length
8252 argument list. If trace_redisplay_p is non-zero also printf the
8253 resulting string to stderr. */
8254
8255 static void
8256 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
8257 struct window *w;
8258 char *fmt;
8259 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
8260 {
8261 char buffer[512];
8262 char *method = w->desired_matrix->method;
8263 int len = strlen (method);
8264 int size = sizeof w->desired_matrix->method;
8265 int remaining = size - len - 1;
8266
8267 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
8268 if (len && remaining)
8269 {
8270 method[len] = '|';
8271 --remaining, ++len;
8272 }
8273
8274 strncpy (method + len, buffer, remaining);
8275
8276 if (trace_redisplay_p)
8277 fprintf (stderr, "%p (%s): %s\n",
8278 w,
8279 ((BUFFERP (w->buffer)
8280 && STRINGP (XBUFFER (w->buffer)->name))
8281 ? (char *) SDATA (XBUFFER (w->buffer)->name)
8282 : "no buffer"),
8283 buffer);
8284 }
8285
8286 #endif /* GLYPH_DEBUG */
8287
8288
8289 /* This counter is used to clear the face cache every once in a while
8290 in redisplay_internal. It is incremented for each redisplay.
8291 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
8292 cleared. */
8293
8294 #define CLEAR_FACE_CACHE_COUNT 500
8295 static int clear_face_cache_count;
8296
8297 /* Record the previous terminal frame we displayed. */
8298
8299 static struct frame *previous_terminal_frame;
8300
8301 /* Non-zero while redisplay_internal is in progress. */
8302
8303 int redisplaying_p;
8304
8305
8306 /* Value is non-zero if all changes in window W, which displays
8307 current_buffer, are in the text between START and END. START is a
8308 buffer position, END is given as a distance from Z. Used in
8309 redisplay_internal for display optimization. */
8310
8311 static INLINE int
8312 text_outside_line_unchanged_p (w, start, end)
8313 struct window *w;
8314 int start, end;
8315 {
8316 int unchanged_p = 1;
8317
8318 /* If text or overlays have changed, see where. */
8319 if (XFASTINT (w->last_modified) < MODIFF
8320 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8321 {
8322 /* Gap in the line? */
8323 if (GPT < start || Z - GPT < end)
8324 unchanged_p = 0;
8325
8326 /* Changes start in front of the line, or end after it? */
8327 if (unchanged_p
8328 && (BEG_UNCHANGED < start - 1
8329 || END_UNCHANGED < end))
8330 unchanged_p = 0;
8331
8332 /* If selective display, can't optimize if changes start at the
8333 beginning of the line. */
8334 if (unchanged_p
8335 && INTEGERP (current_buffer->selective_display)
8336 && XINT (current_buffer->selective_display) > 0
8337 && (BEG_UNCHANGED < start || GPT <= start))
8338 unchanged_p = 0;
8339
8340 /* If there are overlays at the start or end of the line, these
8341 may have overlay strings with newlines in them. A change at
8342 START, for instance, may actually concern the display of such
8343 overlay strings as well, and they are displayed on different
8344 lines. So, quickly rule out this case. (For the future, it
8345 might be desirable to implement something more telling than
8346 just BEG/END_UNCHANGED.) */
8347 if (unchanged_p)
8348 {
8349 if (BEG + BEG_UNCHANGED == start
8350 && overlay_touches_p (start))
8351 unchanged_p = 0;
8352 if (END_UNCHANGED == end
8353 && overlay_touches_p (Z - end))
8354 unchanged_p = 0;
8355 }
8356 }
8357
8358 return unchanged_p;
8359 }
8360
8361
8362 /* Do a frame update, taking possible shortcuts into account. This is
8363 the main external entry point for redisplay.
8364
8365 If the last redisplay displayed an echo area message and that message
8366 is no longer requested, we clear the echo area or bring back the
8367 mini-buffer if that is in use. */
8368
8369 void
8370 redisplay ()
8371 {
8372 redisplay_internal (0);
8373 }
8374
8375
8376 /* Return 1 if point moved out of or into a composition. Otherwise
8377 return 0. PREV_BUF and PREV_PT are the last point buffer and
8378 position. BUF and PT are the current point buffer and position. */
8379
8380 int
8381 check_point_in_composition (prev_buf, prev_pt, buf, pt)
8382 struct buffer *prev_buf, *buf;
8383 int prev_pt, pt;
8384 {
8385 int start, end;
8386 Lisp_Object prop;
8387 Lisp_Object buffer;
8388
8389 XSETBUFFER (buffer, buf);
8390 /* Check a composition at the last point if point moved within the
8391 same buffer. */
8392 if (prev_buf == buf)
8393 {
8394 if (prev_pt == pt)
8395 /* Point didn't move. */
8396 return 0;
8397
8398 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
8399 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
8400 && COMPOSITION_VALID_P (start, end, prop)
8401 && start < prev_pt && end > prev_pt)
8402 /* The last point was within the composition. Return 1 iff
8403 point moved out of the composition. */
8404 return (pt <= start || pt >= end);
8405 }
8406
8407 /* Check a composition at the current point. */
8408 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
8409 && find_composition (pt, -1, &start, &end, &prop, buffer)
8410 && COMPOSITION_VALID_P (start, end, prop)
8411 && start < pt && end > pt);
8412 }
8413
8414
8415 /* Reconsider the setting of B->clip_changed which is displayed
8416 in window W. */
8417
8418 static INLINE void
8419 reconsider_clip_changes (w, b)
8420 struct window *w;
8421 struct buffer *b;
8422 {
8423 if (b->clip_changed
8424 && !NILP (w->window_end_valid)
8425 && w->current_matrix->buffer == b
8426 && w->current_matrix->zv == BUF_ZV (b)
8427 && w->current_matrix->begv == BUF_BEGV (b))
8428 b->clip_changed = 0;
8429
8430 /* If display wasn't paused, and W is not a tool bar window, see if
8431 point has been moved into or out of a composition. In that case,
8432 we set b->clip_changed to 1 to force updating the screen. If
8433 b->clip_changed has already been set to 1, we can skip this
8434 check. */
8435 if (!b->clip_changed
8436 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
8437 {
8438 int pt;
8439
8440 if (w == XWINDOW (selected_window))
8441 pt = BUF_PT (current_buffer);
8442 else
8443 pt = marker_position (w->pointm);
8444
8445 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
8446 || pt != XINT (w->last_point))
8447 && check_point_in_composition (w->current_matrix->buffer,
8448 XINT (w->last_point),
8449 XBUFFER (w->buffer), pt))
8450 b->clip_changed = 1;
8451 }
8452 }
8453
8454
8455 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
8456 response to any user action; therefore, we should preserve the echo
8457 area. (Actually, our caller does that job.) Perhaps in the future
8458 avoid recentering windows if it is not necessary; currently that
8459 causes some problems. */
8460
8461 static void
8462 redisplay_internal (preserve_echo_area)
8463 int preserve_echo_area;
8464 {
8465 struct window *w = XWINDOW (selected_window);
8466 struct frame *f = XFRAME (w->frame);
8467 int pause;
8468 int must_finish = 0;
8469 struct text_pos tlbufpos, tlendpos;
8470 int number_of_visible_frames;
8471 int count;
8472 struct frame *sf = SELECTED_FRAME ();
8473
8474 /* Non-zero means redisplay has to consider all windows on all
8475 frames. Zero means, only selected_window is considered. */
8476 int consider_all_windows_p;
8477
8478 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
8479
8480 /* No redisplay if running in batch mode or frame is not yet fully
8481 initialized, or redisplay is explicitly turned off by setting
8482 Vinhibit_redisplay. */
8483 if (noninteractive
8484 || !NILP (Vinhibit_redisplay)
8485 || !f->glyphs_initialized_p)
8486 return;
8487
8488 /* The flag redisplay_performed_directly_p is set by
8489 direct_output_for_insert when it already did the whole screen
8490 update necessary. */
8491 if (redisplay_performed_directly_p)
8492 {
8493 redisplay_performed_directly_p = 0;
8494 if (!hscroll_windows (selected_window))
8495 return;
8496 }
8497
8498 #ifdef USE_X_TOOLKIT
8499 if (popup_activated ())
8500 return;
8501 #endif
8502
8503 /* I don't think this happens but let's be paranoid. */
8504 if (redisplaying_p)
8505 return;
8506
8507 /* Record a function that resets redisplaying_p to its old value
8508 when we leave this function. */
8509 count = SPECPDL_INDEX ();
8510 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
8511 ++redisplaying_p;
8512
8513 retry:
8514 pause = 0;
8515 reconsider_clip_changes (w, current_buffer);
8516
8517 /* If new fonts have been loaded that make a glyph matrix adjustment
8518 necessary, do it. */
8519 if (fonts_changed_p)
8520 {
8521 adjust_glyphs (NULL);
8522 ++windows_or_buffers_changed;
8523 fonts_changed_p = 0;
8524 }
8525
8526 /* If face_change_count is non-zero, init_iterator will free all
8527 realized faces, which includes the faces referenced from current
8528 matrices. So, we can't reuse current matrices in this case. */
8529 if (face_change_count)
8530 ++windows_or_buffers_changed;
8531
8532 if (! FRAME_WINDOW_P (sf)
8533 && previous_terminal_frame != sf)
8534 {
8535 /* Since frames on an ASCII terminal share the same display
8536 area, displaying a different frame means redisplay the whole
8537 thing. */
8538 windows_or_buffers_changed++;
8539 SET_FRAME_GARBAGED (sf);
8540 XSETFRAME (Vterminal_frame, sf);
8541 }
8542 previous_terminal_frame = sf;
8543
8544 /* Set the visible flags for all frames. Do this before checking
8545 for resized or garbaged frames; they want to know if their frames
8546 are visible. See the comment in frame.h for
8547 FRAME_SAMPLE_VISIBILITY. */
8548 {
8549 Lisp_Object tail, frame;
8550
8551 number_of_visible_frames = 0;
8552
8553 FOR_EACH_FRAME (tail, frame)
8554 {
8555 struct frame *f = XFRAME (frame);
8556
8557 FRAME_SAMPLE_VISIBILITY (f);
8558 if (FRAME_VISIBLE_P (f))
8559 ++number_of_visible_frames;
8560 clear_desired_matrices (f);
8561 }
8562 }
8563
8564 /* Notice any pending interrupt request to change frame size. */
8565 do_pending_window_change (1);
8566
8567 /* Clear frames marked as garbaged. */
8568 if (frame_garbaged)
8569 clear_garbaged_frames ();
8570
8571 /* Build menubar and tool-bar items. */
8572 prepare_menu_bars ();
8573
8574 if (windows_or_buffers_changed)
8575 update_mode_lines++;
8576
8577 /* Detect case that we need to write or remove a star in the mode line. */
8578 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
8579 {
8580 w->update_mode_line = Qt;
8581 if (buffer_shared > 1)
8582 update_mode_lines++;
8583 }
8584
8585 /* If %c is in the mode line, update it if needed. */
8586 if (!NILP (w->column_number_displayed)
8587 /* This alternative quickly identifies a common case
8588 where no change is needed. */
8589 && !(PT == XFASTINT (w->last_point)
8590 && XFASTINT (w->last_modified) >= MODIFF
8591 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
8592 && (XFASTINT (w->column_number_displayed)
8593 != (int) current_column ())) /* iftc */
8594 w->update_mode_line = Qt;
8595
8596 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
8597
8598 /* The variable buffer_shared is set in redisplay_window and
8599 indicates that we redisplay a buffer in different windows. See
8600 there. */
8601 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
8602 || cursor_type_changed);
8603
8604 /* If specs for an arrow have changed, do thorough redisplay
8605 to ensure we remove any arrow that should no longer exist. */
8606 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
8607 || ! EQ (Voverlay_arrow_string, last_arrow_string))
8608 consider_all_windows_p = windows_or_buffers_changed = 1;
8609
8610 /* Normally the message* functions will have already displayed and
8611 updated the echo area, but the frame may have been trashed, or
8612 the update may have been preempted, so display the echo area
8613 again here. Checking message_cleared_p captures the case that
8614 the echo area should be cleared. */
8615 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
8616 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
8617 || (message_cleared_p
8618 && minibuf_level == 0
8619 /* If the mini-window is currently selected, this means the
8620 echo-area doesn't show through. */
8621 && !MINI_WINDOW_P (XWINDOW (selected_window))))
8622 {
8623 int window_height_changed_p = echo_area_display (0);
8624 must_finish = 1;
8625
8626 /* If we don't display the current message, don't clear the
8627 message_cleared_p flag, because, if we did, we wouldn't clear
8628 the echo area in the next redisplay which doesn't preserve
8629 the echo area. */
8630 if (!display_last_displayed_message_p)
8631 message_cleared_p = 0;
8632
8633 if (fonts_changed_p)
8634 goto retry;
8635 else if (window_height_changed_p)
8636 {
8637 consider_all_windows_p = 1;
8638 ++update_mode_lines;
8639 ++windows_or_buffers_changed;
8640
8641 /* If window configuration was changed, frames may have been
8642 marked garbaged. Clear them or we will experience
8643 surprises wrt scrolling. */
8644 if (frame_garbaged)
8645 clear_garbaged_frames ();
8646 }
8647 }
8648 else if (EQ (selected_window, minibuf_window)
8649 && (current_buffer->clip_changed
8650 || XFASTINT (w->last_modified) < MODIFF
8651 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8652 && resize_mini_window (w, 0))
8653 {
8654 /* Resized active mini-window to fit the size of what it is
8655 showing if its contents might have changed. */
8656 must_finish = 1;
8657 consider_all_windows_p = 1;
8658 ++windows_or_buffers_changed;
8659 ++update_mode_lines;
8660
8661 /* If window configuration was changed, frames may have been
8662 marked garbaged. Clear them or we will experience
8663 surprises wrt scrolling. */
8664 if (frame_garbaged)
8665 clear_garbaged_frames ();
8666 }
8667
8668
8669 /* If showing the region, and mark has changed, we must redisplay
8670 the whole window. The assignment to this_line_start_pos prevents
8671 the optimization directly below this if-statement. */
8672 if (((!NILP (Vtransient_mark_mode)
8673 && !NILP (XBUFFER (w->buffer)->mark_active))
8674 != !NILP (w->region_showing))
8675 || (!NILP (w->region_showing)
8676 && !EQ (w->region_showing,
8677 Fmarker_position (XBUFFER (w->buffer)->mark))))
8678 CHARPOS (this_line_start_pos) = 0;
8679
8680 /* Optimize the case that only the line containing the cursor in the
8681 selected window has changed. Variables starting with this_ are
8682 set in display_line and record information about the line
8683 containing the cursor. */
8684 tlbufpos = this_line_start_pos;
8685 tlendpos = this_line_end_pos;
8686 if (!consider_all_windows_p
8687 && CHARPOS (tlbufpos) > 0
8688 && NILP (w->update_mode_line)
8689 && !current_buffer->clip_changed
8690 && !current_buffer->prevent_redisplay_optimizations_p
8691 && FRAME_VISIBLE_P (XFRAME (w->frame))
8692 && !FRAME_OBSCURED_P (XFRAME (w->frame))
8693 /* Make sure recorded data applies to current buffer, etc. */
8694 && this_line_buffer == current_buffer
8695 && current_buffer == XBUFFER (w->buffer)
8696 && NILP (w->force_start)
8697 && NILP (w->optional_new_start)
8698 /* Point must be on the line that we have info recorded about. */
8699 && PT >= CHARPOS (tlbufpos)
8700 && PT <= Z - CHARPOS (tlendpos)
8701 /* All text outside that line, including its final newline,
8702 must be unchanged */
8703 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
8704 CHARPOS (tlendpos)))
8705 {
8706 if (CHARPOS (tlbufpos) > BEGV
8707 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
8708 && (CHARPOS (tlbufpos) == ZV
8709 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
8710 /* Former continuation line has disappeared by becoming empty */
8711 goto cancel;
8712 else if (XFASTINT (w->last_modified) < MODIFF
8713 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
8714 || MINI_WINDOW_P (w))
8715 {
8716 /* We have to handle the case of continuation around a
8717 wide-column character (See the comment in indent.c around
8718 line 885).
8719
8720 For instance, in the following case:
8721
8722 -------- Insert --------
8723 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
8724 J_I_ ==> J_I_ `^^' are cursors.
8725 ^^ ^^
8726 -------- --------
8727
8728 As we have to redraw the line above, we should goto cancel. */
8729
8730 struct it it;
8731 int line_height_before = this_line_pixel_height;
8732
8733 /* Note that start_display will handle the case that the
8734 line starting at tlbufpos is a continuation lines. */
8735 start_display (&it, w, tlbufpos);
8736
8737 /* Implementation note: It this still necessary? */
8738 if (it.current_x != this_line_start_x)
8739 goto cancel;
8740
8741 TRACE ((stderr, "trying display optimization 1\n"));
8742 w->cursor.vpos = -1;
8743 overlay_arrow_seen = 0;
8744 it.vpos = this_line_vpos;
8745 it.current_y = this_line_y;
8746 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
8747 display_line (&it);
8748
8749 /* If line contains point, is not continued,
8750 and ends at same distance from eob as before, we win */
8751 if (w->cursor.vpos >= 0
8752 /* Line is not continued, otherwise this_line_start_pos
8753 would have been set to 0 in display_line. */
8754 && CHARPOS (this_line_start_pos)
8755 /* Line ends as before. */
8756 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
8757 /* Line has same height as before. Otherwise other lines
8758 would have to be shifted up or down. */
8759 && this_line_pixel_height == line_height_before)
8760 {
8761 /* If this is not the window's last line, we must adjust
8762 the charstarts of the lines below. */
8763 if (it.current_y < it.last_visible_y)
8764 {
8765 struct glyph_row *row
8766 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
8767 int delta, delta_bytes;
8768
8769 if (Z - CHARPOS (tlendpos) == ZV)
8770 {
8771 /* This line ends at end of (accessible part of)
8772 buffer. There is no newline to count. */
8773 delta = (Z
8774 - CHARPOS (tlendpos)
8775 - MATRIX_ROW_START_CHARPOS (row));
8776 delta_bytes = (Z_BYTE
8777 - BYTEPOS (tlendpos)
8778 - MATRIX_ROW_START_BYTEPOS (row));
8779 }
8780 else
8781 {
8782 /* This line ends in a newline. Must take
8783 account of the newline and the rest of the
8784 text that follows. */
8785 delta = (Z
8786 - CHARPOS (tlendpos)
8787 - MATRIX_ROW_START_CHARPOS (row));
8788 delta_bytes = (Z_BYTE
8789 - BYTEPOS (tlendpos)
8790 - MATRIX_ROW_START_BYTEPOS (row));
8791 }
8792
8793 increment_matrix_positions (w->current_matrix,
8794 this_line_vpos + 1,
8795 w->current_matrix->nrows,
8796 delta, delta_bytes);
8797 }
8798
8799 /* If this row displays text now but previously didn't,
8800 or vice versa, w->window_end_vpos may have to be
8801 adjusted. */
8802 if ((it.glyph_row - 1)->displays_text_p)
8803 {
8804 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
8805 XSETINT (w->window_end_vpos, this_line_vpos);
8806 }
8807 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
8808 && this_line_vpos > 0)
8809 XSETINT (w->window_end_vpos, this_line_vpos - 1);
8810 w->window_end_valid = Qnil;
8811
8812 /* Update hint: No need to try to scroll in update_window. */
8813 w->desired_matrix->no_scrolling_p = 1;
8814
8815 #if GLYPH_DEBUG
8816 *w->desired_matrix->method = 0;
8817 debug_method_add (w, "optimization 1");
8818 #endif
8819 goto update;
8820 }
8821 else
8822 goto cancel;
8823 }
8824 else if (/* Cursor position hasn't changed. */
8825 PT == XFASTINT (w->last_point)
8826 /* Make sure the cursor was last displayed
8827 in this window. Otherwise we have to reposition it. */
8828 && 0 <= w->cursor.vpos
8829 && XINT (w->height) > w->cursor.vpos)
8830 {
8831 if (!must_finish)
8832 {
8833 do_pending_window_change (1);
8834
8835 /* We used to always goto end_of_redisplay here, but this
8836 isn't enough if we have a blinking cursor. */
8837 if (w->cursor_off_p == w->last_cursor_off_p)
8838 goto end_of_redisplay;
8839 }
8840 goto update;
8841 }
8842 /* If highlighting the region, or if the cursor is in the echo area,
8843 then we can't just move the cursor. */
8844 else if (! (!NILP (Vtransient_mark_mode)
8845 && !NILP (current_buffer->mark_active))
8846 && (EQ (selected_window, current_buffer->last_selected_window)
8847 || highlight_nonselected_windows)
8848 && NILP (w->region_showing)
8849 && NILP (Vshow_trailing_whitespace)
8850 && !cursor_in_echo_area)
8851 {
8852 struct it it;
8853 struct glyph_row *row;
8854
8855 /* Skip from tlbufpos to PT and see where it is. Note that
8856 PT may be in invisible text. If so, we will end at the
8857 next visible position. */
8858 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
8859 NULL, DEFAULT_FACE_ID);
8860 it.current_x = this_line_start_x;
8861 it.current_y = this_line_y;
8862 it.vpos = this_line_vpos;
8863
8864 /* The call to move_it_to stops in front of PT, but
8865 moves over before-strings. */
8866 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
8867
8868 if (it.vpos == this_line_vpos
8869 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
8870 row->enabled_p))
8871 {
8872 xassert (this_line_vpos == it.vpos);
8873 xassert (this_line_y == it.current_y);
8874 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
8875 #if GLYPH_DEBUG
8876 *w->desired_matrix->method = 0;
8877 debug_method_add (w, "optimization 3");
8878 #endif
8879 goto update;
8880 }
8881 else
8882 goto cancel;
8883 }
8884
8885 cancel:
8886 /* Text changed drastically or point moved off of line. */
8887 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
8888 }
8889
8890 CHARPOS (this_line_start_pos) = 0;
8891 consider_all_windows_p |= buffer_shared > 1;
8892 ++clear_face_cache_count;
8893
8894
8895 /* Build desired matrices, and update the display. If
8896 consider_all_windows_p is non-zero, do it for all windows on all
8897 frames. Otherwise do it for selected_window, only. */
8898
8899 if (consider_all_windows_p)
8900 {
8901 Lisp_Object tail, frame;
8902 int i, n = 0, size = 50;
8903 struct frame **updated
8904 = (struct frame **) alloca (size * sizeof *updated);
8905
8906 /* Clear the face cache eventually. */
8907 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
8908 {
8909 clear_face_cache (0);
8910 clear_face_cache_count = 0;
8911 }
8912
8913 /* Recompute # windows showing selected buffer. This will be
8914 incremented each time such a window is displayed. */
8915 buffer_shared = 0;
8916
8917 FOR_EACH_FRAME (tail, frame)
8918 {
8919 struct frame *f = XFRAME (frame);
8920
8921 if (FRAME_WINDOW_P (f) || f == sf)
8922 {
8923 #ifdef HAVE_WINDOW_SYSTEM
8924 if (clear_face_cache_count % 50 == 0
8925 && FRAME_WINDOW_P (f))
8926 clear_image_cache (f, 0);
8927 #endif /* HAVE_WINDOW_SYSTEM */
8928
8929 /* Mark all the scroll bars to be removed; we'll redeem
8930 the ones we want when we redisplay their windows. */
8931 if (condemn_scroll_bars_hook)
8932 condemn_scroll_bars_hook (f);
8933
8934 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8935 redisplay_windows (FRAME_ROOT_WINDOW (f));
8936
8937 /* Any scroll bars which redisplay_windows should have
8938 nuked should now go away. */
8939 if (judge_scroll_bars_hook)
8940 judge_scroll_bars_hook (f);
8941
8942 /* If fonts changed, display again. */
8943 if (fonts_changed_p)
8944 goto retry;
8945
8946 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8947 {
8948 /* See if we have to hscroll. */
8949 if (hscroll_windows (f->root_window))
8950 goto retry;
8951
8952 /* Prevent various kinds of signals during display
8953 update. stdio is not robust about handling
8954 signals, which can cause an apparent I/O
8955 error. */
8956 if (interrupt_input)
8957 unrequest_sigio ();
8958 stop_polling ();
8959
8960 /* Update the display. */
8961 set_window_update_flags (XWINDOW (f->root_window), 1);
8962 pause |= update_frame (f, 0, 0);
8963 if (pause)
8964 break;
8965
8966 if (n == size)
8967 {
8968 int nbytes = size * sizeof *updated;
8969 struct frame **p = (struct frame **) alloca (2 * nbytes);
8970 bcopy (updated, p, nbytes);
8971 size *= 2;
8972 }
8973
8974 updated[n++] = f;
8975 }
8976 }
8977 }
8978
8979 /* Do the mark_window_display_accurate after all windows have
8980 been redisplayed because this call resets flags in buffers
8981 which are needed for proper redisplay. */
8982 for (i = 0; i < n; ++i)
8983 {
8984 struct frame *f = updated[i];
8985 mark_window_display_accurate (f->root_window, 1);
8986 if (frame_up_to_date_hook)
8987 frame_up_to_date_hook (f);
8988 }
8989 }
8990 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8991 {
8992 Lisp_Object mini_window;
8993 struct frame *mini_frame;
8994
8995 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
8996 /* Use list_of_error, not Qerror, so that
8997 we catch only errors and don't run the debugger. */
8998 internal_condition_case_1 (redisplay_window_1, selected_window,
8999 list_of_error,
9000 redisplay_window_error);
9001
9002 /* Compare desired and current matrices, perform output. */
9003 update:
9004
9005 /* If fonts changed, display again. */
9006 if (fonts_changed_p)
9007 goto retry;
9008
9009 /* Prevent various kinds of signals during display update.
9010 stdio is not robust about handling signals,
9011 which can cause an apparent I/O error. */
9012 if (interrupt_input)
9013 unrequest_sigio ();
9014 stop_polling ();
9015
9016 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
9017 {
9018 if (hscroll_windows (selected_window))
9019 goto retry;
9020
9021 XWINDOW (selected_window)->must_be_updated_p = 1;
9022 pause = update_frame (sf, 0, 0);
9023 }
9024
9025 /* We may have called echo_area_display at the top of this
9026 function. If the echo area is on another frame, that may
9027 have put text on a frame other than the selected one, so the
9028 above call to update_frame would not have caught it. Catch
9029 it here. */
9030 mini_window = FRAME_MINIBUF_WINDOW (sf);
9031 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9032
9033 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
9034 {
9035 XWINDOW (mini_window)->must_be_updated_p = 1;
9036 pause |= update_frame (mini_frame, 0, 0);
9037 if (!pause && hscroll_windows (mini_window))
9038 goto retry;
9039 }
9040 }
9041
9042 /* If display was paused because of pending input, make sure we do a
9043 thorough update the next time. */
9044 if (pause)
9045 {
9046 /* Prevent the optimization at the beginning of
9047 redisplay_internal that tries a single-line update of the
9048 line containing the cursor in the selected window. */
9049 CHARPOS (this_line_start_pos) = 0;
9050
9051 /* Let the overlay arrow be updated the next time. */
9052 if (!NILP (last_arrow_position))
9053 {
9054 last_arrow_position = Qt;
9055 last_arrow_string = Qt;
9056 }
9057
9058 /* If we pause after scrolling, some rows in the current
9059 matrices of some windows are not valid. */
9060 if (!WINDOW_FULL_WIDTH_P (w)
9061 && !FRAME_WINDOW_P (XFRAME (w->frame)))
9062 update_mode_lines = 1;
9063 }
9064 else
9065 {
9066 if (!consider_all_windows_p)
9067 {
9068 /* This has already been done above if
9069 consider_all_windows_p is set. */
9070 mark_window_display_accurate_1 (w, 1);
9071
9072 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
9073 last_arrow_string = Voverlay_arrow_string;
9074
9075 if (frame_up_to_date_hook != 0)
9076 frame_up_to_date_hook (sf);
9077 }
9078
9079 update_mode_lines = 0;
9080 windows_or_buffers_changed = 0;
9081 cursor_type_changed = 0;
9082 }
9083
9084 /* Start SIGIO interrupts coming again. Having them off during the
9085 code above makes it less likely one will discard output, but not
9086 impossible, since there might be stuff in the system buffer here.
9087 But it is much hairier to try to do anything about that. */
9088 if (interrupt_input)
9089 request_sigio ();
9090 start_polling ();
9091
9092 /* If a frame has become visible which was not before, redisplay
9093 again, so that we display it. Expose events for such a frame
9094 (which it gets when becoming visible) don't call the parts of
9095 redisplay constructing glyphs, so simply exposing a frame won't
9096 display anything in this case. So, we have to display these
9097 frames here explicitly. */
9098 if (!pause)
9099 {
9100 Lisp_Object tail, frame;
9101 int new_count = 0;
9102
9103 FOR_EACH_FRAME (tail, frame)
9104 {
9105 int this_is_visible = 0;
9106
9107 if (XFRAME (frame)->visible)
9108 this_is_visible = 1;
9109 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
9110 if (XFRAME (frame)->visible)
9111 this_is_visible = 1;
9112
9113 if (this_is_visible)
9114 new_count++;
9115 }
9116
9117 if (new_count != number_of_visible_frames)
9118 windows_or_buffers_changed++;
9119 }
9120
9121 /* Change frame size now if a change is pending. */
9122 do_pending_window_change (1);
9123
9124 /* If we just did a pending size change, or have additional
9125 visible frames, redisplay again. */
9126 if (windows_or_buffers_changed && !pause)
9127 goto retry;
9128
9129 end_of_redisplay:;
9130
9131 unbind_to (count, Qnil);
9132 }
9133
9134
9135 /* Redisplay, but leave alone any recent echo area message unless
9136 another message has been requested in its place.
9137
9138 This is useful in situations where you need to redisplay but no
9139 user action has occurred, making it inappropriate for the message
9140 area to be cleared. See tracking_off and
9141 wait_reading_process_input for examples of these situations.
9142
9143 FROM_WHERE is an integer saying from where this function was
9144 called. This is useful for debugging. */
9145
9146 void
9147 redisplay_preserve_echo_area (from_where)
9148 int from_where;
9149 {
9150 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
9151
9152 if (!NILP (echo_area_buffer[1]))
9153 {
9154 /* We have a previously displayed message, but no current
9155 message. Redisplay the previous message. */
9156 display_last_displayed_message_p = 1;
9157 redisplay_internal (1);
9158 display_last_displayed_message_p = 0;
9159 }
9160 else
9161 redisplay_internal (1);
9162 }
9163
9164
9165 /* Function registered with record_unwind_protect in
9166 redisplay_internal. Clears the flag indicating that a redisplay is
9167 in progress. */
9168
9169 static Lisp_Object
9170 unwind_redisplay (old_redisplaying_p)
9171 Lisp_Object old_redisplaying_p;
9172 {
9173 redisplaying_p = XFASTINT (old_redisplaying_p);
9174 return Qnil;
9175 }
9176
9177
9178 /* Mark the display of window W as accurate or inaccurate. If
9179 ACCURATE_P is non-zero mark display of W as accurate. If
9180 ACCURATE_P is zero, arrange for W to be redisplayed the next time
9181 redisplay_internal is called. */
9182
9183 static void
9184 mark_window_display_accurate_1 (w, accurate_p)
9185 struct window *w;
9186 int accurate_p;
9187 {
9188 if (BUFFERP (w->buffer))
9189 {
9190 struct buffer *b = XBUFFER (w->buffer);
9191
9192 w->last_modified
9193 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
9194 w->last_overlay_modified
9195 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
9196 w->last_had_star
9197 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
9198
9199 if (accurate_p)
9200 {
9201 b->clip_changed = 0;
9202 b->prevent_redisplay_optimizations_p = 0;
9203
9204 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
9205 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
9206 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
9207 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
9208
9209 w->current_matrix->buffer = b;
9210 w->current_matrix->begv = BUF_BEGV (b);
9211 w->current_matrix->zv = BUF_ZV (b);
9212
9213 w->last_cursor = w->cursor;
9214 w->last_cursor_off_p = w->cursor_off_p;
9215
9216 if (w == XWINDOW (selected_window))
9217 w->last_point = make_number (BUF_PT (b));
9218 else
9219 w->last_point = make_number (XMARKER (w->pointm)->charpos);
9220 }
9221 }
9222
9223 if (accurate_p)
9224 {
9225 w->window_end_valid = w->buffer;
9226 #if 0 /* This is incorrect with variable-height lines. */
9227 xassert (XINT (w->window_end_vpos)
9228 < (XINT (w->height)
9229 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
9230 #endif
9231 w->update_mode_line = Qnil;
9232 }
9233 }
9234
9235
9236 /* Mark the display of windows in the window tree rooted at WINDOW as
9237 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
9238 windows as accurate. If ACCURATE_P is zero, arrange for windows to
9239 be redisplayed the next time redisplay_internal is called. */
9240
9241 void
9242 mark_window_display_accurate (window, accurate_p)
9243 Lisp_Object window;
9244 int accurate_p;
9245 {
9246 struct window *w;
9247
9248 for (; !NILP (window); window = w->next)
9249 {
9250 w = XWINDOW (window);
9251 mark_window_display_accurate_1 (w, accurate_p);
9252
9253 if (!NILP (w->vchild))
9254 mark_window_display_accurate (w->vchild, accurate_p);
9255 if (!NILP (w->hchild))
9256 mark_window_display_accurate (w->hchild, accurate_p);
9257 }
9258
9259 if (accurate_p)
9260 {
9261 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
9262 last_arrow_string = Voverlay_arrow_string;
9263 }
9264 else
9265 {
9266 /* Force a thorough redisplay the next time by setting
9267 last_arrow_position and last_arrow_string to t, which is
9268 unequal to any useful value of Voverlay_arrow_... */
9269 last_arrow_position = Qt;
9270 last_arrow_string = Qt;
9271 }
9272 }
9273
9274
9275 /* Return value in display table DP (Lisp_Char_Table *) for character
9276 C. Since a display table doesn't have any parent, we don't have to
9277 follow parent. Do not call this function directly but use the
9278 macro DISP_CHAR_VECTOR. */
9279
9280 Lisp_Object
9281 disp_char_vector (dp, c)
9282 struct Lisp_Char_Table *dp;
9283 int c;
9284 {
9285 int code[4], i;
9286 Lisp_Object val;
9287
9288 if (SINGLE_BYTE_CHAR_P (c))
9289 return (dp->contents[c]);
9290
9291 SPLIT_CHAR (c, code[0], code[1], code[2]);
9292 if (code[1] < 32)
9293 code[1] = -1;
9294 else if (code[2] < 32)
9295 code[2] = -1;
9296
9297 /* Here, the possible range of code[0] (== charset ID) is
9298 128..max_charset. Since the top level char table contains data
9299 for multibyte characters after 256th element, we must increment
9300 code[0] by 128 to get a correct index. */
9301 code[0] += 128;
9302 code[3] = -1; /* anchor */
9303
9304 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
9305 {
9306 val = dp->contents[code[i]];
9307 if (!SUB_CHAR_TABLE_P (val))
9308 return (NILP (val) ? dp->defalt : val);
9309 }
9310
9311 /* Here, val is a sub char table. We return the default value of
9312 it. */
9313 return (dp->defalt);
9314 }
9315
9316
9317 \f
9318 /***********************************************************************
9319 Window Redisplay
9320 ***********************************************************************/
9321
9322 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
9323
9324 static void
9325 redisplay_windows (window)
9326 Lisp_Object window;
9327 {
9328 while (!NILP (window))
9329 {
9330 struct window *w = XWINDOW (window);
9331
9332 if (!NILP (w->hchild))
9333 redisplay_windows (w->hchild);
9334 else if (!NILP (w->vchild))
9335 redisplay_windows (w->vchild);
9336 else
9337 {
9338 displayed_buffer = XBUFFER (w->buffer);
9339 /* Use list_of_error, not Qerror, so that
9340 we catch only errors and don't run the debugger. */
9341 internal_condition_case_1 (redisplay_window_0, window,
9342 list_of_error,
9343 redisplay_window_error);
9344 }
9345
9346 window = w->next;
9347 }
9348 }
9349
9350 static Lisp_Object
9351 redisplay_window_error ()
9352 {
9353 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
9354 return Qnil;
9355 }
9356
9357 static Lisp_Object
9358 redisplay_window_0 (window)
9359 Lisp_Object window;
9360 {
9361 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
9362 redisplay_window (window, 0);
9363 return Qnil;
9364 }
9365
9366 static Lisp_Object
9367 redisplay_window_1 (window)
9368 Lisp_Object window;
9369 {
9370 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
9371 redisplay_window (window, 1);
9372 return Qnil;
9373 }
9374 \f
9375 /* Set cursor position of W. PT is assumed to be displayed in ROW.
9376 DELTA is the number of bytes by which positions recorded in ROW
9377 differ from current buffer positions. */
9378
9379 void
9380 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
9381 struct window *w;
9382 struct glyph_row *row;
9383 struct glyph_matrix *matrix;
9384 int delta, delta_bytes, dy, dvpos;
9385 {
9386 struct glyph *glyph = row->glyphs[TEXT_AREA];
9387 struct glyph *end = glyph + row->used[TEXT_AREA];
9388 int x = row->x;
9389 int pt_old = PT - delta;
9390
9391 /* Skip over glyphs not having an object at the start of the row.
9392 These are special glyphs like truncation marks on terminal
9393 frames. */
9394 if (row->displays_text_p)
9395 while (glyph < end
9396 && INTEGERP (glyph->object)
9397 && glyph->charpos < 0)
9398 {
9399 x += glyph->pixel_width;
9400 ++glyph;
9401 }
9402
9403 while (glyph < end
9404 && !INTEGERP (glyph->object)
9405 && (!BUFFERP (glyph->object)
9406 || glyph->charpos < pt_old))
9407 {
9408 x += glyph->pixel_width;
9409 ++glyph;
9410 }
9411
9412 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
9413 w->cursor.x = x;
9414 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
9415 w->cursor.y = row->y + dy;
9416
9417 if (w == XWINDOW (selected_window))
9418 {
9419 if (!row->continued_p
9420 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
9421 && row->x == 0)
9422 {
9423 this_line_buffer = XBUFFER (w->buffer);
9424
9425 CHARPOS (this_line_start_pos)
9426 = MATRIX_ROW_START_CHARPOS (row) + delta;
9427 BYTEPOS (this_line_start_pos)
9428 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
9429
9430 CHARPOS (this_line_end_pos)
9431 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
9432 BYTEPOS (this_line_end_pos)
9433 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
9434
9435 this_line_y = w->cursor.y;
9436 this_line_pixel_height = row->height;
9437 this_line_vpos = w->cursor.vpos;
9438 this_line_start_x = row->x;
9439 }
9440 else
9441 CHARPOS (this_line_start_pos) = 0;
9442 }
9443 }
9444
9445
9446 /* Run window scroll functions, if any, for WINDOW with new window
9447 start STARTP. Sets the window start of WINDOW to that position.
9448
9449 We assume that the window's buffer is really current. */
9450
9451 static INLINE struct text_pos
9452 run_window_scroll_functions (window, startp)
9453 Lisp_Object window;
9454 struct text_pos startp;
9455 {
9456 struct window *w = XWINDOW (window);
9457 SET_MARKER_FROM_TEXT_POS (w->start, startp);
9458
9459 if (current_buffer != XBUFFER (w->buffer))
9460 abort ();
9461
9462 if (!NILP (Vwindow_scroll_functions))
9463 {
9464 run_hook_with_args_2 (Qwindow_scroll_functions, window,
9465 make_number (CHARPOS (startp)));
9466 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9467 /* In case the hook functions switch buffers. */
9468 if (current_buffer != XBUFFER (w->buffer))
9469 set_buffer_internal_1 (XBUFFER (w->buffer));
9470 }
9471
9472 return startp;
9473 }
9474
9475
9476 /* Make sure the line containing the cursor is fully visible.
9477 A value of 1 means there is nothing to be done.
9478 (Either the line is fully visible, or it cannot be made so,
9479 or we cannot tell.)
9480 A value of 0 means the caller should do scrolling
9481 as if point had gone off the screen. */
9482
9483 static int
9484 make_cursor_line_fully_visible (w)
9485 struct window *w;
9486 {
9487 struct glyph_matrix *matrix;
9488 struct glyph_row *row;
9489 int window_height;
9490
9491 /* It's not always possible to find the cursor, e.g, when a window
9492 is full of overlay strings. Don't do anything in that case. */
9493 if (w->cursor.vpos < 0)
9494 return 1;
9495
9496 matrix = w->desired_matrix;
9497 row = MATRIX_ROW (matrix, w->cursor.vpos);
9498
9499 /* If the cursor row is not partially visible, there's nothing to do. */
9500 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
9501 return 1;
9502
9503 /* If the row the cursor is in is taller than the window's height,
9504 it's not clear what to do, so do nothing. */
9505 window_height = window_box_height (w);
9506 if (row->height >= window_height)
9507 return 1;
9508
9509 return 0;
9510
9511 #if 0
9512 /* This code used to try to scroll the window just enough to make
9513 the line visible. It returned 0 to say that the caller should
9514 allocate larger glyph matrices. */
9515
9516 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
9517 {
9518 int dy = row->height - row->visible_height;
9519 w->vscroll = 0;
9520 w->cursor.y += dy;
9521 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9522 }
9523 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
9524 {
9525 int dy = - (row->height - row->visible_height);
9526 w->vscroll = dy;
9527 w->cursor.y += dy;
9528 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9529 }
9530
9531 /* When we change the cursor y-position of the selected window,
9532 change this_line_y as well so that the display optimization for
9533 the cursor line of the selected window in redisplay_internal uses
9534 the correct y-position. */
9535 if (w == XWINDOW (selected_window))
9536 this_line_y = w->cursor.y;
9537
9538 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
9539 redisplay with larger matrices. */
9540 if (matrix->nrows < required_matrix_height (w))
9541 {
9542 fonts_changed_p = 1;
9543 return 0;
9544 }
9545
9546 return 1;
9547 #endif /* 0 */
9548 }
9549
9550
9551 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
9552 non-zero means only WINDOW is redisplayed in redisplay_internal.
9553 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
9554 in redisplay_window to bring a partially visible line into view in
9555 the case that only the cursor has moved.
9556
9557 Value is
9558
9559 1 if scrolling succeeded
9560
9561 0 if scrolling didn't find point.
9562
9563 -1 if new fonts have been loaded so that we must interrupt
9564 redisplay, adjust glyph matrices, and try again. */
9565
9566 enum
9567 {
9568 SCROLLING_SUCCESS,
9569 SCROLLING_FAILED,
9570 SCROLLING_NEED_LARGER_MATRICES
9571 };
9572
9573 static int
9574 try_scrolling (window, just_this_one_p, scroll_conservatively,
9575 scroll_step, temp_scroll_step)
9576 Lisp_Object window;
9577 int just_this_one_p;
9578 EMACS_INT scroll_conservatively, scroll_step;
9579 int temp_scroll_step;
9580 {
9581 struct window *w = XWINDOW (window);
9582 struct frame *f = XFRAME (w->frame);
9583 struct text_pos scroll_margin_pos;
9584 struct text_pos pos;
9585 struct text_pos startp;
9586 struct it it;
9587 Lisp_Object window_end;
9588 int this_scroll_margin;
9589 int dy = 0;
9590 int scroll_max;
9591 int rc;
9592 int amount_to_scroll = 0;
9593 Lisp_Object aggressive;
9594 int height;
9595
9596 #if GLYPH_DEBUG
9597 debug_method_add (w, "try_scrolling");
9598 #endif
9599
9600 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9601
9602 /* Compute scroll margin height in pixels. We scroll when point is
9603 within this distance from the top or bottom of the window. */
9604 if (scroll_margin > 0)
9605 {
9606 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
9607 this_scroll_margin *= CANON_Y_UNIT (f);
9608 }
9609 else
9610 this_scroll_margin = 0;
9611
9612 /* Compute how much we should try to scroll maximally to bring point
9613 into view. */
9614 if (scroll_step || scroll_conservatively || temp_scroll_step)
9615 scroll_max = max (scroll_step,
9616 max (scroll_conservatively, temp_scroll_step));
9617 else if (NUMBERP (current_buffer->scroll_down_aggressively)
9618 || NUMBERP (current_buffer->scroll_up_aggressively))
9619 /* We're trying to scroll because of aggressive scrolling
9620 but no scroll_step is set. Choose an arbitrary one. Maybe
9621 there should be a variable for this. */
9622 scroll_max = 10;
9623 else
9624 scroll_max = 0;
9625 scroll_max *= CANON_Y_UNIT (f);
9626
9627 /* Decide whether we have to scroll down. Start at the window end
9628 and move this_scroll_margin up to find the position of the scroll
9629 margin. */
9630 window_end = Fwindow_end (window, Qt);
9631 CHARPOS (scroll_margin_pos) = XINT (window_end);
9632 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
9633 if (this_scroll_margin)
9634 {
9635 start_display (&it, w, scroll_margin_pos);
9636 move_it_vertically (&it, - this_scroll_margin);
9637 scroll_margin_pos = it.current.pos;
9638 }
9639
9640 if (PT >= CHARPOS (scroll_margin_pos))
9641 {
9642 int y0;
9643
9644 too_near_end:
9645 /* Point is in the scroll margin at the bottom of the window, or
9646 below. Compute a new window start that makes point visible. */
9647
9648 /* Compute the distance from the scroll margin to PT.
9649 Give up if the distance is greater than scroll_max. */
9650 start_display (&it, w, scroll_margin_pos);
9651 y0 = it.current_y;
9652 move_it_to (&it, PT, 0, it.last_visible_y, -1,
9653 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9654
9655 /* To make point visible, we have to move the window start
9656 down so that the line the cursor is in is visible, which
9657 means we have to add in the height of the cursor line. */
9658 dy = line_bottom_y (&it) - y0;
9659
9660 if (dy > scroll_max)
9661 return SCROLLING_FAILED;
9662
9663 /* Move the window start down. If scrolling conservatively,
9664 move it just enough down to make point visible. If
9665 scroll_step is set, move it down by scroll_step. */
9666 start_display (&it, w, startp);
9667
9668 if (scroll_conservatively)
9669 amount_to_scroll
9670 = max (max (dy, CANON_Y_UNIT (f)),
9671 CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
9672 else if (scroll_step || temp_scroll_step)
9673 amount_to_scroll = scroll_max;
9674 else
9675 {
9676 aggressive = current_buffer->scroll_up_aggressively;
9677 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
9678 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
9679 if (NUMBERP (aggressive))
9680 amount_to_scroll = XFLOATINT (aggressive) * height;
9681 }
9682
9683 if (amount_to_scroll <= 0)
9684 return SCROLLING_FAILED;
9685
9686 move_it_vertically (&it, amount_to_scroll);
9687 startp = it.current.pos;
9688 }
9689 else
9690 {
9691 /* See if point is inside the scroll margin at the top of the
9692 window. */
9693 scroll_margin_pos = startp;
9694 if (this_scroll_margin)
9695 {
9696 start_display (&it, w, startp);
9697 move_it_vertically (&it, this_scroll_margin);
9698 scroll_margin_pos = it.current.pos;
9699 }
9700
9701 if (PT < CHARPOS (scroll_margin_pos))
9702 {
9703 /* Point is in the scroll margin at the top of the window or
9704 above what is displayed in the window. */
9705 int y0;
9706
9707 /* Compute the vertical distance from PT to the scroll
9708 margin position. Give up if distance is greater than
9709 scroll_max. */
9710 SET_TEXT_POS (pos, PT, PT_BYTE);
9711 start_display (&it, w, pos);
9712 y0 = it.current_y;
9713 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
9714 it.last_visible_y, -1,
9715 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9716 dy = it.current_y - y0;
9717 if (dy > scroll_max)
9718 return SCROLLING_FAILED;
9719
9720 /* Compute new window start. */
9721 start_display (&it, w, startp);
9722
9723 if (scroll_conservatively)
9724 amount_to_scroll =
9725 max (dy, CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
9726 else if (scroll_step || temp_scroll_step)
9727 amount_to_scroll = scroll_max;
9728 else
9729 {
9730 aggressive = current_buffer->scroll_down_aggressively;
9731 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
9732 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
9733 if (NUMBERP (aggressive))
9734 amount_to_scroll = XFLOATINT (aggressive) * height;
9735 }
9736
9737 if (amount_to_scroll <= 0)
9738 return SCROLLING_FAILED;
9739
9740 move_it_vertically (&it, - amount_to_scroll);
9741 startp = it.current.pos;
9742 }
9743 }
9744
9745 /* Run window scroll functions. */
9746 startp = run_window_scroll_functions (window, startp);
9747
9748 /* Display the window. Give up if new fonts are loaded, or if point
9749 doesn't appear. */
9750 if (!try_window (window, startp))
9751 rc = SCROLLING_NEED_LARGER_MATRICES;
9752 else if (w->cursor.vpos < 0)
9753 {
9754 clear_glyph_matrix (w->desired_matrix);
9755 rc = SCROLLING_FAILED;
9756 }
9757 else
9758 {
9759 /* Maybe forget recorded base line for line number display. */
9760 if (!just_this_one_p
9761 || current_buffer->clip_changed
9762 || BEG_UNCHANGED < CHARPOS (startp))
9763 w->base_line_number = Qnil;
9764
9765 /* If cursor ends up on a partially visible line,
9766 treat that as being off the bottom of the screen. */
9767 if (! make_cursor_line_fully_visible (w))
9768 goto too_near_end;
9769 rc = SCROLLING_SUCCESS;
9770 }
9771
9772 return rc;
9773 }
9774
9775
9776 /* Compute a suitable window start for window W if display of W starts
9777 on a continuation line. Value is non-zero if a new window start
9778 was computed.
9779
9780 The new window start will be computed, based on W's width, starting
9781 from the start of the continued line. It is the start of the
9782 screen line with the minimum distance from the old start W->start. */
9783
9784 static int
9785 compute_window_start_on_continuation_line (w)
9786 struct window *w;
9787 {
9788 struct text_pos pos, start_pos;
9789 int window_start_changed_p = 0;
9790
9791 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
9792
9793 /* If window start is on a continuation line... Window start may be
9794 < BEGV in case there's invisible text at the start of the
9795 buffer (M-x rmail, for example). */
9796 if (CHARPOS (start_pos) > BEGV
9797 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
9798 {
9799 struct it it;
9800 struct glyph_row *row;
9801
9802 /* Handle the case that the window start is out of range. */
9803 if (CHARPOS (start_pos) < BEGV)
9804 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
9805 else if (CHARPOS (start_pos) > ZV)
9806 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
9807
9808 /* Find the start of the continued line. This should be fast
9809 because scan_buffer is fast (newline cache). */
9810 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
9811 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
9812 row, DEFAULT_FACE_ID);
9813 reseat_at_previous_visible_line_start (&it);
9814
9815 /* If the line start is "too far" away from the window start,
9816 say it takes too much time to compute a new window start. */
9817 if (CHARPOS (start_pos) - IT_CHARPOS (it)
9818 < XFASTINT (w->height) * XFASTINT (w->width))
9819 {
9820 int min_distance, distance;
9821
9822 /* Move forward by display lines to find the new window
9823 start. If window width was enlarged, the new start can
9824 be expected to be > the old start. If window width was
9825 decreased, the new window start will be < the old start.
9826 So, we're looking for the display line start with the
9827 minimum distance from the old window start. */
9828 pos = it.current.pos;
9829 min_distance = INFINITY;
9830 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
9831 distance < min_distance)
9832 {
9833 min_distance = distance;
9834 pos = it.current.pos;
9835 move_it_by_lines (&it, 1, 0);
9836 }
9837
9838 /* Set the window start there. */
9839 SET_MARKER_FROM_TEXT_POS (w->start, pos);
9840 window_start_changed_p = 1;
9841 }
9842 }
9843
9844 return window_start_changed_p;
9845 }
9846
9847
9848 /* Try cursor movement in case text has not changed in window WINDOW,
9849 with window start STARTP. Value is
9850
9851 CURSOR_MOVEMENT_SUCCESS if successful
9852
9853 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
9854
9855 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
9856 display. *SCROLL_STEP is set to 1, under certain circumstances, if
9857 we want to scroll as if scroll-step were set to 1. See the code.
9858
9859 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
9860 which case we have to abort this redisplay, and adjust matrices
9861 first. */
9862
9863 enum
9864 {
9865 CURSOR_MOVEMENT_SUCCESS,
9866 CURSOR_MOVEMENT_CANNOT_BE_USED,
9867 CURSOR_MOVEMENT_MUST_SCROLL,
9868 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
9869 };
9870
9871 static int
9872 try_cursor_movement (window, startp, scroll_step)
9873 Lisp_Object window;
9874 struct text_pos startp;
9875 int *scroll_step;
9876 {
9877 struct window *w = XWINDOW (window);
9878 struct frame *f = XFRAME (w->frame);
9879 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
9880
9881 #if GLYPH_DEBUG
9882 if (inhibit_try_cursor_movement)
9883 return rc;
9884 #endif
9885
9886 /* Handle case where text has not changed, only point, and it has
9887 not moved off the frame. */
9888 if (/* Point may be in this window. */
9889 PT >= CHARPOS (startp)
9890 /* Selective display hasn't changed. */
9891 && !current_buffer->clip_changed
9892 /* Function force-mode-line-update is used to force a thorough
9893 redisplay. It sets either windows_or_buffers_changed or
9894 update_mode_lines. So don't take a shortcut here for these
9895 cases. */
9896 && !update_mode_lines
9897 && !windows_or_buffers_changed
9898 && !cursor_type_changed
9899 /* Can't use this case if highlighting a region. When a
9900 region exists, cursor movement has to do more than just
9901 set the cursor. */
9902 && !(!NILP (Vtransient_mark_mode)
9903 && !NILP (current_buffer->mark_active))
9904 && NILP (w->region_showing)
9905 && NILP (Vshow_trailing_whitespace)
9906 /* Right after splitting windows, last_point may be nil. */
9907 && INTEGERP (w->last_point)
9908 /* This code is not used for mini-buffer for the sake of the case
9909 of redisplaying to replace an echo area message; since in
9910 that case the mini-buffer contents per se are usually
9911 unchanged. This code is of no real use in the mini-buffer
9912 since the handling of this_line_start_pos, etc., in redisplay
9913 handles the same cases. */
9914 && !EQ (window, minibuf_window)
9915 /* When splitting windows or for new windows, it happens that
9916 redisplay is called with a nil window_end_vpos or one being
9917 larger than the window. This should really be fixed in
9918 window.c. I don't have this on my list, now, so we do
9919 approximately the same as the old redisplay code. --gerd. */
9920 && INTEGERP (w->window_end_vpos)
9921 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
9922 && (FRAME_WINDOW_P (f)
9923 || !MARKERP (Voverlay_arrow_position)
9924 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
9925 {
9926 int this_scroll_margin;
9927 struct glyph_row *row = NULL;
9928
9929 #if GLYPH_DEBUG
9930 debug_method_add (w, "cursor movement");
9931 #endif
9932
9933 /* Scroll if point within this distance from the top or bottom
9934 of the window. This is a pixel value. */
9935 this_scroll_margin = max (0, scroll_margin);
9936 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
9937 this_scroll_margin *= CANON_Y_UNIT (f);
9938
9939 /* Start with the row the cursor was displayed during the last
9940 not paused redisplay. Give up if that row is not valid. */
9941 if (w->last_cursor.vpos < 0
9942 || w->last_cursor.vpos >= w->current_matrix->nrows)
9943 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9944 else
9945 {
9946 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
9947 if (row->mode_line_p)
9948 ++row;
9949 if (!row->enabled_p)
9950 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9951 }
9952
9953 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
9954 {
9955 int scroll_p = 0;
9956 int last_y = window_text_bottom_y (w) - this_scroll_margin;
9957
9958 if (PT > XFASTINT (w->last_point))
9959 {
9960 /* Point has moved forward. */
9961 while (MATRIX_ROW_END_CHARPOS (row) < PT
9962 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
9963 {
9964 xassert (row->enabled_p);
9965 ++row;
9966 }
9967
9968 /* The end position of a row equals the start position
9969 of the next row. If PT is there, we would rather
9970 display it in the next line. */
9971 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9972 && MATRIX_ROW_END_CHARPOS (row) == PT
9973 && !cursor_row_p (w, row))
9974 ++row;
9975
9976 /* If within the scroll margin, scroll. Note that
9977 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
9978 the next line would be drawn, and that
9979 this_scroll_margin can be zero. */
9980 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
9981 || PT > MATRIX_ROW_END_CHARPOS (row)
9982 /* Line is completely visible last line in window
9983 and PT is to be set in the next line. */
9984 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
9985 && PT == MATRIX_ROW_END_CHARPOS (row)
9986 && !row->ends_at_zv_p
9987 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
9988 scroll_p = 1;
9989 }
9990 else if (PT < XFASTINT (w->last_point))
9991 {
9992 /* Cursor has to be moved backward. Note that PT >=
9993 CHARPOS (startp) because of the outer
9994 if-statement. */
9995 while (!row->mode_line_p
9996 && (MATRIX_ROW_START_CHARPOS (row) > PT
9997 || (MATRIX_ROW_START_CHARPOS (row) == PT
9998 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
9999 && (row->y > this_scroll_margin
10000 || CHARPOS (startp) == BEGV))
10001 {
10002 xassert (row->enabled_p);
10003 --row;
10004 }
10005
10006 /* Consider the following case: Window starts at BEGV,
10007 there is invisible, intangible text at BEGV, so that
10008 display starts at some point START > BEGV. It can
10009 happen that we are called with PT somewhere between
10010 BEGV and START. Try to handle that case. */
10011 if (row < w->current_matrix->rows
10012 || row->mode_line_p)
10013 {
10014 row = w->current_matrix->rows;
10015 if (row->mode_line_p)
10016 ++row;
10017 }
10018
10019 /* Due to newlines in overlay strings, we may have to
10020 skip forward over overlay strings. */
10021 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
10022 && MATRIX_ROW_END_CHARPOS (row) == PT
10023 && !cursor_row_p (w, row))
10024 ++row;
10025
10026 /* If within the scroll margin, scroll. */
10027 if (row->y < this_scroll_margin
10028 && CHARPOS (startp) != BEGV)
10029 scroll_p = 1;
10030 }
10031
10032 if (PT < MATRIX_ROW_START_CHARPOS (row)
10033 || PT > MATRIX_ROW_END_CHARPOS (row))
10034 {
10035 /* if PT is not in the glyph row, give up. */
10036 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10037 }
10038 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
10039 {
10040 if (PT == MATRIX_ROW_END_CHARPOS (row)
10041 && !row->ends_at_zv_p
10042 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
10043 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10044 else if (row->height > window_box_height (w))
10045 {
10046 /* If we end up in a partially visible line, let's
10047 make it fully visible, except when it's taller
10048 than the window, in which case we can't do much
10049 about it. */
10050 *scroll_step = 1;
10051 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10052 }
10053 else
10054 {
10055 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10056 try_window (window, startp);
10057 if (!make_cursor_line_fully_visible (w))
10058 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10059 else
10060 rc = CURSOR_MOVEMENT_SUCCESS;
10061 }
10062 }
10063 else if (scroll_p)
10064 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10065 else
10066 {
10067 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10068 rc = CURSOR_MOVEMENT_SUCCESS;
10069 }
10070 }
10071 }
10072
10073 return rc;
10074 }
10075
10076
10077 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
10078 selected_window is redisplayed. */
10079
10080 static void
10081 redisplay_window (window, just_this_one_p)
10082 Lisp_Object window;
10083 int just_this_one_p;
10084 {
10085 struct window *w = XWINDOW (window);
10086 struct frame *f = XFRAME (w->frame);
10087 struct buffer *buffer = XBUFFER (w->buffer);
10088 struct buffer *old = current_buffer;
10089 struct text_pos lpoint, opoint, startp;
10090 int update_mode_line;
10091 int tem;
10092 struct it it;
10093 /* Record it now because it's overwritten. */
10094 int current_matrix_up_to_date_p = 0;
10095 /* This is less strict than current_matrix_up_to_date_p.
10096 It indictes that the buffer contents and narrowing are unchanged. */
10097 int buffer_unchanged_p = 0;
10098 int temp_scroll_step = 0;
10099 int count = SPECPDL_INDEX ();
10100 int rc;
10101 int centering_position;
10102
10103 SET_TEXT_POS (lpoint, PT, PT_BYTE);
10104 opoint = lpoint;
10105
10106 /* W must be a leaf window here. */
10107 xassert (!NILP (w->buffer));
10108 #if GLYPH_DEBUG
10109 *w->desired_matrix->method = 0;
10110 #endif
10111
10112 specbind (Qinhibit_point_motion_hooks, Qt);
10113
10114 reconsider_clip_changes (w, buffer);
10115
10116 /* Has the mode line to be updated? */
10117 update_mode_line = (!NILP (w->update_mode_line)
10118 || update_mode_lines
10119 || buffer->clip_changed
10120 || buffer->prevent_redisplay_optimizations_p);
10121
10122 if (MINI_WINDOW_P (w))
10123 {
10124 if (w == XWINDOW (echo_area_window)
10125 && !NILP (echo_area_buffer[0]))
10126 {
10127 if (update_mode_line)
10128 /* We may have to update a tty frame's menu bar or a
10129 tool-bar. Example `M-x C-h C-h C-g'. */
10130 goto finish_menu_bars;
10131 else
10132 /* We've already displayed the echo area glyphs in this window. */
10133 goto finish_scroll_bars;
10134 }
10135 else if (w != XWINDOW (minibuf_window))
10136 {
10137 /* W is a mini-buffer window, but it's not the currently
10138 active one, so clear it. */
10139 int yb = window_text_bottom_y (w);
10140 struct glyph_row *row;
10141 int y;
10142
10143 for (y = 0, row = w->desired_matrix->rows;
10144 y < yb;
10145 y += row->height, ++row)
10146 blank_row (w, row, y);
10147 goto finish_scroll_bars;
10148 }
10149
10150 clear_glyph_matrix (w->desired_matrix);
10151 }
10152
10153 /* Otherwise set up data on this window; select its buffer and point
10154 value. */
10155 /* Really select the buffer, for the sake of buffer-local
10156 variables. */
10157 set_buffer_internal_1 (XBUFFER (w->buffer));
10158 SET_TEXT_POS (opoint, PT, PT_BYTE);
10159
10160 current_matrix_up_to_date_p
10161 = (!NILP (w->window_end_valid)
10162 && !current_buffer->clip_changed
10163 && !current_buffer->prevent_redisplay_optimizations_p
10164 && XFASTINT (w->last_modified) >= MODIFF
10165 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
10166
10167 buffer_unchanged_p
10168 = (!NILP (w->window_end_valid)
10169 && !current_buffer->clip_changed
10170 && END_UNCHANGED + BEG_UNCHANGED >= Z - BEG
10171 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
10172
10173 /* When windows_or_buffers_changed is non-zero, we can't rely on
10174 the window end being valid, so set it to nil there. */
10175 if (windows_or_buffers_changed)
10176 {
10177 /* If window starts on a continuation line, maybe adjust the
10178 window start in case the window's width changed. */
10179 if (XMARKER (w->start)->buffer == current_buffer)
10180 compute_window_start_on_continuation_line (w);
10181
10182 w->window_end_valid = Qnil;
10183 }
10184
10185 /* Some sanity checks. */
10186 CHECK_WINDOW_END (w);
10187 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
10188 abort ();
10189 if (BYTEPOS (opoint) < CHARPOS (opoint))
10190 abort ();
10191
10192 /* If %c is in mode line, update it if needed. */
10193 if (!NILP (w->column_number_displayed)
10194 /* This alternative quickly identifies a common case
10195 where no change is needed. */
10196 && !(PT == XFASTINT (w->last_point)
10197 && XFASTINT (w->last_modified) >= MODIFF
10198 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
10199 && (XFASTINT (w->column_number_displayed)
10200 != (int) current_column ())) /* iftc */
10201 update_mode_line = 1;
10202
10203 /* Count number of windows showing the selected buffer. An indirect
10204 buffer counts as its base buffer. */
10205 if (!just_this_one_p)
10206 {
10207 struct buffer *current_base, *window_base;
10208 current_base = current_buffer;
10209 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
10210 if (current_base->base_buffer)
10211 current_base = current_base->base_buffer;
10212 if (window_base->base_buffer)
10213 window_base = window_base->base_buffer;
10214 if (current_base == window_base)
10215 buffer_shared++;
10216 }
10217
10218 /* Point refers normally to the selected window. For any other
10219 window, set up appropriate value. */
10220 if (!EQ (window, selected_window))
10221 {
10222 int new_pt = XMARKER (w->pointm)->charpos;
10223 int new_pt_byte = marker_byte_position (w->pointm);
10224 if (new_pt < BEGV)
10225 {
10226 new_pt = BEGV;
10227 new_pt_byte = BEGV_BYTE;
10228 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
10229 }
10230 else if (new_pt > (ZV - 1))
10231 {
10232 new_pt = ZV;
10233 new_pt_byte = ZV_BYTE;
10234 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
10235 }
10236
10237 /* We don't use SET_PT so that the point-motion hooks don't run. */
10238 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
10239 }
10240
10241 /* If any of the character widths specified in the display table
10242 have changed, invalidate the width run cache. It's true that
10243 this may be a bit late to catch such changes, but the rest of
10244 redisplay goes (non-fatally) haywire when the display table is
10245 changed, so why should we worry about doing any better? */
10246 if (current_buffer->width_run_cache)
10247 {
10248 struct Lisp_Char_Table *disptab = buffer_display_table ();
10249
10250 if (! disptab_matches_widthtab (disptab,
10251 XVECTOR (current_buffer->width_table)))
10252 {
10253 invalidate_region_cache (current_buffer,
10254 current_buffer->width_run_cache,
10255 BEG, Z);
10256 recompute_width_table (current_buffer, disptab);
10257 }
10258 }
10259
10260 /* If window-start is screwed up, choose a new one. */
10261 if (XMARKER (w->start)->buffer != current_buffer)
10262 goto recenter;
10263
10264 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10265
10266 /* If someone specified a new starting point but did not insist,
10267 check whether it can be used. */
10268 if (!NILP (w->optional_new_start)
10269 && CHARPOS (startp) >= BEGV
10270 && CHARPOS (startp) <= ZV)
10271 {
10272 w->optional_new_start = Qnil;
10273 start_display (&it, w, startp);
10274 move_it_to (&it, PT, 0, it.last_visible_y, -1,
10275 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
10276 if (IT_CHARPOS (it) == PT)
10277 w->force_start = Qt;
10278 }
10279
10280 /* Handle case where place to start displaying has been specified,
10281 unless the specified location is outside the accessible range. */
10282 if (!NILP (w->force_start)
10283 || w->frozen_window_start_p)
10284 {
10285 w->force_start = Qnil;
10286 w->vscroll = 0;
10287 w->window_end_valid = Qnil;
10288
10289 /* Forget any recorded base line for line number display. */
10290 if (!buffer_unchanged_p)
10291 w->base_line_number = Qnil;
10292
10293 /* Redisplay the mode line. Select the buffer properly for that.
10294 Also, run the hook window-scroll-functions
10295 because we have scrolled. */
10296 /* Note, we do this after clearing force_start because
10297 if there's an error, it is better to forget about force_start
10298 than to get into an infinite loop calling the hook functions
10299 and having them get more errors. */
10300 if (!update_mode_line
10301 || ! NILP (Vwindow_scroll_functions))
10302 {
10303 update_mode_line = 1;
10304 w->update_mode_line = Qt;
10305 startp = run_window_scroll_functions (window, startp);
10306 }
10307
10308 w->last_modified = make_number (0);
10309 w->last_overlay_modified = make_number (0);
10310 if (CHARPOS (startp) < BEGV)
10311 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
10312 else if (CHARPOS (startp) > ZV)
10313 SET_TEXT_POS (startp, ZV, ZV_BYTE);
10314
10315 /* Redisplay, then check if cursor has been set during the
10316 redisplay. Give up if new fonts were loaded. */
10317 if (!try_window (window, startp))
10318 {
10319 w->force_start = Qt;
10320 clear_glyph_matrix (w->desired_matrix);
10321 goto finish_scroll_bars;
10322 }
10323
10324 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
10325 {
10326 /* If point does not appear, try to move point so it does
10327 appear. The desired matrix has been built above, so we
10328 can use it here. */
10329 int window_height;
10330 struct glyph_row *row;
10331
10332 window_height = window_box_height (w) / 2;
10333 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
10334 while (MATRIX_ROW_BOTTOM_Y (row) < window_height)
10335 ++row;
10336
10337 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
10338 MATRIX_ROW_START_BYTEPOS (row));
10339
10340 if (w != XWINDOW (selected_window))
10341 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
10342 else if (current_buffer == old)
10343 SET_TEXT_POS (lpoint, PT, PT_BYTE);
10344
10345 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
10346
10347 /* If we are highlighting the region, then we just changed
10348 the region, so redisplay to show it. */
10349 if (!NILP (Vtransient_mark_mode)
10350 && !NILP (current_buffer->mark_active))
10351 {
10352 clear_glyph_matrix (w->desired_matrix);
10353 if (!try_window (window, startp))
10354 goto need_larger_matrices;
10355 }
10356 }
10357
10358 if (!make_cursor_line_fully_visible (w))
10359 goto try_to_scroll;
10360 #if GLYPH_DEBUG
10361 debug_method_add (w, "forced window start");
10362 #endif
10363 goto done;
10364 }
10365
10366 /* Handle case where text has not changed, only point, and it has
10367 not moved off the frame. */
10368 if (buffer_unchanged_p
10369 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
10370 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
10371 {
10372 switch (rc)
10373 {
10374 case CURSOR_MOVEMENT_SUCCESS:
10375 goto done;
10376
10377 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
10378 goto need_larger_matrices;
10379
10380 case CURSOR_MOVEMENT_MUST_SCROLL:
10381 goto try_to_scroll;
10382
10383 default:
10384 abort ();
10385 }
10386 }
10387 /* If current starting point was originally the beginning of a line
10388 but no longer is, find a new starting point. */
10389 else if (!NILP (w->start_at_line_beg)
10390 && !(CHARPOS (startp) <= BEGV
10391 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
10392 {
10393 #if GLYPH_DEBUG
10394 debug_method_add (w, "recenter 1");
10395 #endif
10396 goto recenter;
10397 }
10398
10399 /* Try scrolling with try_window_id. Value is > 0 if update has
10400 been done, it is -1 if we know that the same window start will
10401 not work. It is 0 if unsuccessful for some other reason. */
10402 else if ((tem = try_window_id (w)) != 0)
10403 {
10404 #if GLYPH_DEBUG
10405 debug_method_add (w, "try_window_id %d", tem);
10406 #endif
10407
10408 if (fonts_changed_p)
10409 goto need_larger_matrices;
10410 if (tem > 0)
10411 goto done;
10412
10413 /* Otherwise try_window_id has returned -1 which means that we
10414 don't want the alternative below this comment to execute. */
10415 }
10416 else if (CHARPOS (startp) >= BEGV
10417 && CHARPOS (startp) <= ZV
10418 && PT >= CHARPOS (startp)
10419 && (CHARPOS (startp) < ZV
10420 /* Avoid starting at end of buffer. */
10421 || CHARPOS (startp) == BEGV
10422 || (XFASTINT (w->last_modified) >= MODIFF
10423 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
10424 {
10425 #if GLYPH_DEBUG
10426 debug_method_add (w, "same window start");
10427 #endif
10428
10429 /* Try to redisplay starting at same place as before.
10430 If point has not moved off frame, accept the results. */
10431 if (!current_matrix_up_to_date_p
10432 /* Don't use try_window_reusing_current_matrix in this case
10433 because a window scroll function can have changed the
10434 buffer. */
10435 || !NILP (Vwindow_scroll_functions)
10436 || MINI_WINDOW_P (w)
10437 || !try_window_reusing_current_matrix (w))
10438 {
10439 IF_DEBUG (debug_method_add (w, "1"));
10440 try_window (window, startp);
10441 }
10442
10443 if (fonts_changed_p)
10444 goto need_larger_matrices;
10445
10446 if (w->cursor.vpos >= 0)
10447 {
10448 if (!just_this_one_p
10449 || current_buffer->clip_changed
10450 || BEG_UNCHANGED < CHARPOS (startp))
10451 /* Forget any recorded base line for line number display. */
10452 w->base_line_number = Qnil;
10453
10454 if (!make_cursor_line_fully_visible (w))
10455 /* Drop through and scroll. */
10456 ;
10457 goto done;
10458 }
10459 else
10460 clear_glyph_matrix (w->desired_matrix);
10461 }
10462
10463 try_to_scroll:
10464
10465 w->last_modified = make_number (0);
10466 w->last_overlay_modified = make_number (0);
10467
10468 /* Redisplay the mode line. Select the buffer properly for that. */
10469 if (!update_mode_line)
10470 {
10471 update_mode_line = 1;
10472 w->update_mode_line = Qt;
10473 }
10474
10475 /* Try to scroll by specified few lines. */
10476 if ((scroll_conservatively
10477 || scroll_step
10478 || temp_scroll_step
10479 || NUMBERP (current_buffer->scroll_up_aggressively)
10480 || NUMBERP (current_buffer->scroll_down_aggressively))
10481 && !current_buffer->clip_changed
10482 && CHARPOS (startp) >= BEGV
10483 && CHARPOS (startp) <= ZV)
10484 {
10485 /* The function returns -1 if new fonts were loaded, 1 if
10486 successful, 0 if not successful. */
10487 int rc = try_scrolling (window, just_this_one_p,
10488 scroll_conservatively,
10489 scroll_step,
10490 temp_scroll_step);
10491 switch (rc)
10492 {
10493 case SCROLLING_SUCCESS:
10494 goto done;
10495
10496 case SCROLLING_NEED_LARGER_MATRICES:
10497 goto need_larger_matrices;
10498
10499 case SCROLLING_FAILED:
10500 break;
10501
10502 default:
10503 abort ();
10504 }
10505 }
10506
10507 /* Finally, just choose place to start which centers point */
10508
10509 recenter:
10510 centering_position = window_box_height (w) / 2;
10511
10512 point_at_top:
10513 /* Jump here with centering_position already set to 0. */
10514
10515 #if GLYPH_DEBUG
10516 debug_method_add (w, "recenter");
10517 #endif
10518
10519 /* w->vscroll = 0; */
10520
10521 /* Forget any previously recorded base line for line number display. */
10522 if (!buffer_unchanged_p)
10523 w->base_line_number = Qnil;
10524
10525 /* Move backward half the height of the window. */
10526 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10527 it.current_y = it.last_visible_y;
10528 move_it_vertically_backward (&it, centering_position);
10529 xassert (IT_CHARPOS (it) >= BEGV);
10530
10531 /* The function move_it_vertically_backward may move over more
10532 than the specified y-distance. If it->w is small, e.g. a
10533 mini-buffer window, we may end up in front of the window's
10534 display area. Start displaying at the start of the line
10535 containing PT in this case. */
10536 if (it.current_y <= 0)
10537 {
10538 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10539 move_it_vertically (&it, 0);
10540 xassert (IT_CHARPOS (it) <= PT);
10541 it.current_y = 0;
10542 }
10543
10544 it.current_x = it.hpos = 0;
10545
10546 /* Set startp here explicitly in case that helps avoid an infinite loop
10547 in case the window-scroll-functions functions get errors. */
10548 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
10549
10550 /* Run scroll hooks. */
10551 startp = run_window_scroll_functions (window, it.current.pos);
10552
10553 /* Redisplay the window. */
10554 if (!current_matrix_up_to_date_p
10555 || windows_or_buffers_changed
10556 || cursor_type_changed
10557 /* Don't use try_window_reusing_current_matrix in this case
10558 because it can have changed the buffer. */
10559 || !NILP (Vwindow_scroll_functions)
10560 || !just_this_one_p
10561 || MINI_WINDOW_P (w)
10562 || !try_window_reusing_current_matrix (w))
10563 try_window (window, startp);
10564
10565 /* If new fonts have been loaded (due to fontsets), give up. We
10566 have to start a new redisplay since we need to re-adjust glyph
10567 matrices. */
10568 if (fonts_changed_p)
10569 goto need_larger_matrices;
10570
10571 /* If cursor did not appear assume that the middle of the window is
10572 in the first line of the window. Do it again with the next line.
10573 (Imagine a window of height 100, displaying two lines of height
10574 60. Moving back 50 from it->last_visible_y will end in the first
10575 line.) */
10576 if (w->cursor.vpos < 0)
10577 {
10578 if (!NILP (w->window_end_valid)
10579 && PT >= Z - XFASTINT (w->window_end_pos))
10580 {
10581 clear_glyph_matrix (w->desired_matrix);
10582 move_it_by_lines (&it, 1, 0);
10583 try_window (window, it.current.pos);
10584 }
10585 else if (PT < IT_CHARPOS (it))
10586 {
10587 clear_glyph_matrix (w->desired_matrix);
10588 move_it_by_lines (&it, -1, 0);
10589 try_window (window, it.current.pos);
10590 }
10591 else
10592 {
10593 /* Not much we can do about it. */
10594 }
10595 }
10596
10597 /* Consider the following case: Window starts at BEGV, there is
10598 invisible, intangible text at BEGV, so that display starts at
10599 some point START > BEGV. It can happen that we are called with
10600 PT somewhere between BEGV and START. Try to handle that case. */
10601 if (w->cursor.vpos < 0)
10602 {
10603 struct glyph_row *row = w->current_matrix->rows;
10604 if (row->mode_line_p)
10605 ++row;
10606 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10607 }
10608
10609 if (!make_cursor_line_fully_visible (w))
10610 {
10611 /* If centering point failed to make the whole line visible,
10612 put point at the top instead. That has to make the whole line
10613 visible, if it can be done. */
10614 centering_position = 0;
10615 goto point_at_top;
10616 }
10617
10618 done:
10619
10620 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10621 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
10622 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
10623 ? Qt : Qnil);
10624
10625 /* Display the mode line, if we must. */
10626 if ((update_mode_line
10627 /* If window not full width, must redo its mode line
10628 if (a) the window to its side is being redone and
10629 (b) we do a frame-based redisplay. This is a consequence
10630 of how inverted lines are drawn in frame-based redisplay. */
10631 || (!just_this_one_p
10632 && !FRAME_WINDOW_P (f)
10633 && !WINDOW_FULL_WIDTH_P (w))
10634 /* Line number to display. */
10635 || INTEGERP (w->base_line_pos)
10636 /* Column number is displayed and different from the one displayed. */
10637 || (!NILP (w->column_number_displayed)
10638 && (XFASTINT (w->column_number_displayed)
10639 != (int) current_column ()))) /* iftc */
10640 /* This means that the window has a mode line. */
10641 && (WINDOW_WANTS_MODELINE_P (w)
10642 || WINDOW_WANTS_HEADER_LINE_P (w)))
10643 {
10644 display_mode_lines (w);
10645
10646 /* If mode line height has changed, arrange for a thorough
10647 immediate redisplay using the correct mode line height. */
10648 if (WINDOW_WANTS_MODELINE_P (w)
10649 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
10650 {
10651 fonts_changed_p = 1;
10652 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
10653 = DESIRED_MODE_LINE_HEIGHT (w);
10654 }
10655
10656 /* If top line height has changed, arrange for a thorough
10657 immediate redisplay using the correct mode line height. */
10658 if (WINDOW_WANTS_HEADER_LINE_P (w)
10659 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
10660 {
10661 fonts_changed_p = 1;
10662 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
10663 = DESIRED_HEADER_LINE_HEIGHT (w);
10664 }
10665
10666 if (fonts_changed_p)
10667 goto need_larger_matrices;
10668 }
10669
10670 if (!line_number_displayed
10671 && !BUFFERP (w->base_line_pos))
10672 {
10673 w->base_line_pos = Qnil;
10674 w->base_line_number = Qnil;
10675 }
10676
10677 finish_menu_bars:
10678
10679 /* When we reach a frame's selected window, redo the frame's menu bar. */
10680 if (update_mode_line
10681 && EQ (FRAME_SELECTED_WINDOW (f), window))
10682 {
10683 int redisplay_menu_p = 0;
10684
10685 if (FRAME_WINDOW_P (f))
10686 {
10687 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS)
10688 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
10689 #else
10690 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10691 #endif
10692 }
10693 else
10694 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10695
10696 if (redisplay_menu_p)
10697 display_menu_bar (w);
10698
10699 #ifdef HAVE_WINDOW_SYSTEM
10700 if (WINDOWP (f->tool_bar_window)
10701 && (FRAME_TOOL_BAR_LINES (f) > 0
10702 || auto_resize_tool_bars_p))
10703 redisplay_tool_bar (f);
10704 #endif
10705 }
10706
10707 need_larger_matrices:
10708 ;
10709 finish_scroll_bars:
10710
10711 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
10712 {
10713 int start, end, whole;
10714
10715 /* Calculate the start and end positions for the current window.
10716 At some point, it would be nice to choose between scrollbars
10717 which reflect the whole buffer size, with special markers
10718 indicating narrowing, and scrollbars which reflect only the
10719 visible region.
10720
10721 Note that mini-buffers sometimes aren't displaying any text. */
10722 if (!MINI_WINDOW_P (w)
10723 || (w == XWINDOW (minibuf_window)
10724 && NILP (echo_area_buffer[0])))
10725 {
10726 whole = ZV - BEGV;
10727 start = marker_position (w->start) - BEGV;
10728 /* I don't think this is guaranteed to be right. For the
10729 moment, we'll pretend it is. */
10730 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
10731
10732 if (end < start)
10733 end = start;
10734 if (whole < (end - start))
10735 whole = end - start;
10736 }
10737 else
10738 start = end = whole = 0;
10739
10740 /* Indicate what this scroll bar ought to be displaying now. */
10741 set_vertical_scroll_bar_hook (w, end - start, whole, start);
10742
10743 /* Note that we actually used the scroll bar attached to this
10744 window, so it shouldn't be deleted at the end of redisplay. */
10745 redeem_scroll_bar_hook (w);
10746 }
10747
10748 /* Restore current_buffer and value of point in it. */
10749 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
10750 set_buffer_internal_1 (old);
10751 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
10752
10753 unbind_to (count, Qnil);
10754 }
10755
10756
10757 /* Build the complete desired matrix of WINDOW with a window start
10758 buffer position POS. Value is non-zero if successful. It is zero
10759 if fonts were loaded during redisplay which makes re-adjusting
10760 glyph matrices necessary. */
10761
10762 int
10763 try_window (window, pos)
10764 Lisp_Object window;
10765 struct text_pos pos;
10766 {
10767 struct window *w = XWINDOW (window);
10768 struct it it;
10769 struct glyph_row *last_text_row = NULL;
10770
10771 /* Make POS the new window start. */
10772 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
10773
10774 /* Mark cursor position as unknown. No overlay arrow seen. */
10775 w->cursor.vpos = -1;
10776 overlay_arrow_seen = 0;
10777
10778 /* Initialize iterator and info to start at POS. */
10779 start_display (&it, w, pos);
10780
10781 /* Display all lines of W. */
10782 while (it.current_y < it.last_visible_y)
10783 {
10784 if (display_line (&it))
10785 last_text_row = it.glyph_row - 1;
10786 if (fonts_changed_p)
10787 return 0;
10788 }
10789
10790 /* If bottom moved off end of frame, change mode line percentage. */
10791 if (XFASTINT (w->window_end_pos) <= 0
10792 && Z != IT_CHARPOS (it))
10793 w->update_mode_line = Qt;
10794
10795 /* Set window_end_pos to the offset of the last character displayed
10796 on the window from the end of current_buffer. Set
10797 window_end_vpos to its row number. */
10798 if (last_text_row)
10799 {
10800 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
10801 w->window_end_bytepos
10802 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10803 w->window_end_pos
10804 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10805 w->window_end_vpos
10806 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10807 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
10808 ->displays_text_p);
10809 }
10810 else
10811 {
10812 w->window_end_bytepos = 0;
10813 w->window_end_pos = w->window_end_vpos = make_number (0);
10814 }
10815
10816 /* But that is not valid info until redisplay finishes. */
10817 w->window_end_valid = Qnil;
10818 return 1;
10819 }
10820
10821
10822 \f
10823 /************************************************************************
10824 Window redisplay reusing current matrix when buffer has not changed
10825 ************************************************************************/
10826
10827 /* Try redisplay of window W showing an unchanged buffer with a
10828 different window start than the last time it was displayed by
10829 reusing its current matrix. Value is non-zero if successful.
10830 W->start is the new window start. */
10831
10832 static int
10833 try_window_reusing_current_matrix (w)
10834 struct window *w;
10835 {
10836 struct frame *f = XFRAME (w->frame);
10837 struct glyph_row *row, *bottom_row;
10838 struct it it;
10839 struct run run;
10840 struct text_pos start, new_start;
10841 int nrows_scrolled, i;
10842 struct glyph_row *last_text_row;
10843 struct glyph_row *last_reused_text_row;
10844 struct glyph_row *start_row;
10845 int start_vpos, min_y, max_y;
10846
10847 #if GLYPH_DEBUG
10848 if (inhibit_try_window_reusing)
10849 return 0;
10850 #endif
10851
10852 if (/* This function doesn't handle terminal frames. */
10853 !FRAME_WINDOW_P (f)
10854 /* Don't try to reuse the display if windows have been split
10855 or such. */
10856 || windows_or_buffers_changed
10857 || cursor_type_changed)
10858 return 0;
10859
10860 /* Can't do this if region may have changed. */
10861 if ((!NILP (Vtransient_mark_mode)
10862 && !NILP (current_buffer->mark_active))
10863 || !NILP (w->region_showing)
10864 || !NILP (Vshow_trailing_whitespace))
10865 return 0;
10866
10867 /* If top-line visibility has changed, give up. */
10868 if (WINDOW_WANTS_HEADER_LINE_P (w)
10869 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
10870 return 0;
10871
10872 /* Give up if old or new display is scrolled vertically. We could
10873 make this function handle this, but right now it doesn't. */
10874 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10875 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
10876 return 0;
10877
10878 /* The variable new_start now holds the new window start. The old
10879 start `start' can be determined from the current matrix. */
10880 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
10881 start = start_row->start.pos;
10882 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
10883
10884 /* Clear the desired matrix for the display below. */
10885 clear_glyph_matrix (w->desired_matrix);
10886
10887 if (CHARPOS (new_start) <= CHARPOS (start))
10888 {
10889 int first_row_y;
10890
10891 /* Don't use this method if the display starts with an ellipsis
10892 displayed for invisible text. It's not easy to handle that case
10893 below, and it's certainly not worth the effort since this is
10894 not a frequent case. */
10895 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
10896 return 0;
10897
10898 IF_DEBUG (debug_method_add (w, "twu1"));
10899
10900 /* Display up to a row that can be reused. The variable
10901 last_text_row is set to the last row displayed that displays
10902 text. Note that it.vpos == 0 if or if not there is a
10903 header-line; it's not the same as the MATRIX_ROW_VPOS! */
10904 start_display (&it, w, new_start);
10905 first_row_y = it.current_y;
10906 w->cursor.vpos = -1;
10907 last_text_row = last_reused_text_row = NULL;
10908
10909 while (it.current_y < it.last_visible_y
10910 && IT_CHARPOS (it) < CHARPOS (start)
10911 && !fonts_changed_p)
10912 if (display_line (&it))
10913 last_text_row = it.glyph_row - 1;
10914
10915 /* A value of current_y < last_visible_y means that we stopped
10916 at the previous window start, which in turn means that we
10917 have at least one reusable row. */
10918 if (it.current_y < it.last_visible_y)
10919 {
10920 /* IT.vpos always starts from 0; it counts text lines. */
10921 nrows_scrolled = it.vpos;
10922
10923 /* Find PT if not already found in the lines displayed. */
10924 if (w->cursor.vpos < 0)
10925 {
10926 int dy = it.current_y - first_row_y;
10927
10928 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10929 row = row_containing_pos (w, PT, row, NULL, dy);
10930 if (row)
10931 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
10932 dy, nrows_scrolled);
10933 else
10934 {
10935 clear_glyph_matrix (w->desired_matrix);
10936 return 0;
10937 }
10938 }
10939
10940 /* Scroll the display. Do it before the current matrix is
10941 changed. The problem here is that update has not yet
10942 run, i.e. part of the current matrix is not up to date.
10943 scroll_run_hook will clear the cursor, and use the
10944 current matrix to get the height of the row the cursor is
10945 in. */
10946 run.current_y = first_row_y;
10947 run.desired_y = it.current_y;
10948 run.height = it.last_visible_y - it.current_y;
10949
10950 if (run.height > 0 && run.current_y != run.desired_y)
10951 {
10952 update_begin (f);
10953 rif->update_window_begin_hook (w);
10954 rif->clear_mouse_face (w);
10955 rif->scroll_run_hook (w, &run);
10956 rif->update_window_end_hook (w, 0, 0);
10957 update_end (f);
10958 }
10959
10960 /* Shift current matrix down by nrows_scrolled lines. */
10961 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
10962 rotate_matrix (w->current_matrix,
10963 start_vpos,
10964 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10965 nrows_scrolled);
10966
10967 /* Disable lines that must be updated. */
10968 for (i = 0; i < it.vpos; ++i)
10969 (start_row + i)->enabled_p = 0;
10970
10971 /* Re-compute Y positions. */
10972 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10973 max_y = it.last_visible_y;
10974 for (row = start_row + nrows_scrolled;
10975 row < bottom_row;
10976 ++row)
10977 {
10978 row->y = it.current_y;
10979 row->visible_height = row->height;
10980
10981 if (row->y < min_y)
10982 row->visible_height -= min_y - row->y;
10983 if (row->y + row->height > max_y)
10984 row->visible_height -= row->y + row->height - max_y;
10985
10986 it.current_y += row->height;
10987
10988 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10989 last_reused_text_row = row;
10990 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
10991 break;
10992 }
10993
10994 /* Disable lines in the current matrix which are now
10995 below the window. */
10996 for (++row; row < bottom_row; ++row)
10997 row->enabled_p = 0;
10998 }
10999
11000 /* Update window_end_pos etc.; last_reused_text_row is the last
11001 reused row from the current matrix containing text, if any.
11002 The value of last_text_row is the last displayed line
11003 containing text. */
11004 if (last_reused_text_row)
11005 {
11006 w->window_end_bytepos
11007 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
11008 w->window_end_pos
11009 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
11010 w->window_end_vpos
11011 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
11012 w->current_matrix));
11013 }
11014 else if (last_text_row)
11015 {
11016 w->window_end_bytepos
11017 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
11018 w->window_end_pos
11019 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
11020 w->window_end_vpos
11021 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
11022 }
11023 else
11024 {
11025 /* This window must be completely empty. */
11026 w->window_end_bytepos = 0;
11027 w->window_end_pos = w->window_end_vpos = make_number (0);
11028 }
11029 w->window_end_valid = Qnil;
11030
11031 /* Update hint: don't try scrolling again in update_window. */
11032 w->desired_matrix->no_scrolling_p = 1;
11033
11034 #if GLYPH_DEBUG
11035 debug_method_add (w, "try_window_reusing_current_matrix 1");
11036 #endif
11037 return 1;
11038 }
11039 else if (CHARPOS (new_start) > CHARPOS (start))
11040 {
11041 struct glyph_row *pt_row, *row;
11042 struct glyph_row *first_reusable_row;
11043 struct glyph_row *first_row_to_display;
11044 int dy;
11045 int yb = window_text_bottom_y (w);
11046
11047 /* Find the row starting at new_start, if there is one. Don't
11048 reuse a partially visible line at the end. */
11049 first_reusable_row = start_row;
11050 while (first_reusable_row->enabled_p
11051 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
11052 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
11053 < CHARPOS (new_start)))
11054 ++first_reusable_row;
11055
11056 /* Give up if there is no row to reuse. */
11057 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
11058 || !first_reusable_row->enabled_p
11059 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
11060 != CHARPOS (new_start)))
11061 return 0;
11062
11063 /* We can reuse fully visible rows beginning with
11064 first_reusable_row to the end of the window. Set
11065 first_row_to_display to the first row that cannot be reused.
11066 Set pt_row to the row containing point, if there is any. */
11067 pt_row = NULL;
11068 for (first_row_to_display = first_reusable_row;
11069 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
11070 ++first_row_to_display)
11071 {
11072 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
11073 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
11074 pt_row = first_row_to_display;
11075 }
11076
11077 /* Start displaying at the start of first_row_to_display. */
11078 xassert (first_row_to_display->y < yb);
11079 init_to_row_start (&it, w, first_row_to_display);
11080
11081 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
11082 - start_vpos);
11083 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
11084 - nrows_scrolled);
11085 it.current_y = (first_row_to_display->y - first_reusable_row->y
11086 + WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
11087
11088 /* Display lines beginning with first_row_to_display in the
11089 desired matrix. Set last_text_row to the last row displayed
11090 that displays text. */
11091 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
11092 if (pt_row == NULL)
11093 w->cursor.vpos = -1;
11094 last_text_row = NULL;
11095 while (it.current_y < it.last_visible_y && !fonts_changed_p)
11096 if (display_line (&it))
11097 last_text_row = it.glyph_row - 1;
11098
11099 /* Give up If point isn't in a row displayed or reused. */
11100 if (w->cursor.vpos < 0)
11101 {
11102 clear_glyph_matrix (w->desired_matrix);
11103 return 0;
11104 }
11105
11106 /* If point is in a reused row, adjust y and vpos of the cursor
11107 position. */
11108 if (pt_row)
11109 {
11110 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
11111 w->current_matrix);
11112 w->cursor.y -= first_reusable_row->y;
11113 }
11114
11115 /* Scroll the display. */
11116 run.current_y = first_reusable_row->y;
11117 run.desired_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
11118 run.height = it.last_visible_y - run.current_y;
11119 dy = run.current_y - run.desired_y;
11120
11121 if (run.height)
11122 {
11123 struct frame *f = XFRAME (WINDOW_FRAME (w));
11124 update_begin (f);
11125 rif->update_window_begin_hook (w);
11126 rif->clear_mouse_face (w);
11127 rif->scroll_run_hook (w, &run);
11128 rif->update_window_end_hook (w, 0, 0);
11129 update_end (f);
11130 }
11131
11132 /* Adjust Y positions of reused rows. */
11133 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
11134 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
11135 max_y = it.last_visible_y;
11136 for (row = first_reusable_row; row < first_row_to_display; ++row)
11137 {
11138 row->y -= dy;
11139 row->visible_height = row->height;
11140 if (row->y < min_y)
11141 row->visible_height -= min_y - row->y;
11142 if (row->y + row->height > max_y)
11143 row->visible_height -= row->y + row->height - max_y;
11144 }
11145
11146 /* Scroll the current matrix. */
11147 xassert (nrows_scrolled > 0);
11148 rotate_matrix (w->current_matrix,
11149 start_vpos,
11150 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
11151 -nrows_scrolled);
11152
11153 /* Disable rows not reused. */
11154 for (row -= nrows_scrolled; row < bottom_row; ++row)
11155 row->enabled_p = 0;
11156
11157 /* Adjust window end. A null value of last_text_row means that
11158 the window end is in reused rows which in turn means that
11159 only its vpos can have changed. */
11160 if (last_text_row)
11161 {
11162 w->window_end_bytepos
11163 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
11164 w->window_end_pos
11165 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
11166 w->window_end_vpos
11167 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
11168 }
11169 else
11170 {
11171 w->window_end_vpos
11172 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
11173 }
11174
11175 w->window_end_valid = Qnil;
11176 w->desired_matrix->no_scrolling_p = 1;
11177
11178 #if GLYPH_DEBUG
11179 debug_method_add (w, "try_window_reusing_current_matrix 2");
11180 #endif
11181 return 1;
11182 }
11183
11184 return 0;
11185 }
11186
11187
11188 \f
11189 /************************************************************************
11190 Window redisplay reusing current matrix when buffer has changed
11191 ************************************************************************/
11192
11193 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
11194 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
11195 int *, int *));
11196 static struct glyph_row *
11197 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
11198 struct glyph_row *));
11199
11200
11201 /* Return the last row in MATRIX displaying text. If row START is
11202 non-null, start searching with that row. IT gives the dimensions
11203 of the display. Value is null if matrix is empty; otherwise it is
11204 a pointer to the row found. */
11205
11206 static struct glyph_row *
11207 find_last_row_displaying_text (matrix, it, start)
11208 struct glyph_matrix *matrix;
11209 struct it *it;
11210 struct glyph_row *start;
11211 {
11212 struct glyph_row *row, *row_found;
11213
11214 /* Set row_found to the last row in IT->w's current matrix
11215 displaying text. The loop looks funny but think of partially
11216 visible lines. */
11217 row_found = NULL;
11218 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
11219 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
11220 {
11221 xassert (row->enabled_p);
11222 row_found = row;
11223 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
11224 break;
11225 ++row;
11226 }
11227
11228 return row_found;
11229 }
11230
11231
11232 /* Return the last row in the current matrix of W that is not affected
11233 by changes at the start of current_buffer that occurred since W's
11234 current matrix was built. Value is null if no such row exists.
11235
11236 BEG_UNCHANGED us the number of characters unchanged at the start of
11237 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
11238 first changed character in current_buffer. Characters at positions <
11239 BEG + BEG_UNCHANGED are at the same buffer positions as they were
11240 when the current matrix was built. */
11241
11242 static struct glyph_row *
11243 find_last_unchanged_at_beg_row (w)
11244 struct window *w;
11245 {
11246 int first_changed_pos = BEG + BEG_UNCHANGED;
11247 struct glyph_row *row;
11248 struct glyph_row *row_found = NULL;
11249 int yb = window_text_bottom_y (w);
11250
11251 /* Find the last row displaying unchanged text. */
11252 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11253 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
11254 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
11255 {
11256 if (/* If row ends before first_changed_pos, it is unchanged,
11257 except in some case. */
11258 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
11259 /* When row ends in ZV and we write at ZV it is not
11260 unchanged. */
11261 && !row->ends_at_zv_p
11262 /* When first_changed_pos is the end of a continued line,
11263 row is not unchanged because it may be no longer
11264 continued. */
11265 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
11266 && row->continued_p))
11267 row_found = row;
11268
11269 /* Stop if last visible row. */
11270 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
11271 break;
11272
11273 ++row;
11274 }
11275
11276 return row_found;
11277 }
11278
11279
11280 /* Find the first glyph row in the current matrix of W that is not
11281 affected by changes at the end of current_buffer since the
11282 time W's current matrix was built.
11283
11284 Return in *DELTA the number of chars by which buffer positions in
11285 unchanged text at the end of current_buffer must be adjusted.
11286
11287 Return in *DELTA_BYTES the corresponding number of bytes.
11288
11289 Value is null if no such row exists, i.e. all rows are affected by
11290 changes. */
11291
11292 static struct glyph_row *
11293 find_first_unchanged_at_end_row (w, delta, delta_bytes)
11294 struct window *w;
11295 int *delta, *delta_bytes;
11296 {
11297 struct glyph_row *row;
11298 struct glyph_row *row_found = NULL;
11299
11300 *delta = *delta_bytes = 0;
11301
11302 /* Display must not have been paused, otherwise the current matrix
11303 is not up to date. */
11304 if (NILP (w->window_end_valid))
11305 abort ();
11306
11307 /* A value of window_end_pos >= END_UNCHANGED means that the window
11308 end is in the range of changed text. If so, there is no
11309 unchanged row at the end of W's current matrix. */
11310 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
11311 return NULL;
11312
11313 /* Set row to the last row in W's current matrix displaying text. */
11314 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
11315
11316 /* If matrix is entirely empty, no unchanged row exists. */
11317 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
11318 {
11319 /* The value of row is the last glyph row in the matrix having a
11320 meaningful buffer position in it. The end position of row
11321 corresponds to window_end_pos. This allows us to translate
11322 buffer positions in the current matrix to current buffer
11323 positions for characters not in changed text. */
11324 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
11325 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
11326 int last_unchanged_pos, last_unchanged_pos_old;
11327 struct glyph_row *first_text_row
11328 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11329
11330 *delta = Z - Z_old;
11331 *delta_bytes = Z_BYTE - Z_BYTE_old;
11332
11333 /* Set last_unchanged_pos to the buffer position of the last
11334 character in the buffer that has not been changed. Z is the
11335 index + 1 of the last character in current_buffer, i.e. by
11336 subtracting END_UNCHANGED we get the index of the last
11337 unchanged character, and we have to add BEG to get its buffer
11338 position. */
11339 last_unchanged_pos = Z - END_UNCHANGED + BEG;
11340 last_unchanged_pos_old = last_unchanged_pos - *delta;
11341
11342 /* Search backward from ROW for a row displaying a line that
11343 starts at a minimum position >= last_unchanged_pos_old. */
11344 for (; row > first_text_row; --row)
11345 {
11346 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
11347 abort ();
11348
11349 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
11350 row_found = row;
11351 }
11352 }
11353
11354 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
11355 abort ();
11356
11357 return row_found;
11358 }
11359
11360
11361 /* Make sure that glyph rows in the current matrix of window W
11362 reference the same glyph memory as corresponding rows in the
11363 frame's frame matrix. This function is called after scrolling W's
11364 current matrix on a terminal frame in try_window_id and
11365 try_window_reusing_current_matrix. */
11366
11367 static void
11368 sync_frame_with_window_matrix_rows (w)
11369 struct window *w;
11370 {
11371 struct frame *f = XFRAME (w->frame);
11372 struct glyph_row *window_row, *window_row_end, *frame_row;
11373
11374 /* Preconditions: W must be a leaf window and full-width. Its frame
11375 must have a frame matrix. */
11376 xassert (NILP (w->hchild) && NILP (w->vchild));
11377 xassert (WINDOW_FULL_WIDTH_P (w));
11378 xassert (!FRAME_WINDOW_P (f));
11379
11380 /* If W is a full-width window, glyph pointers in W's current matrix
11381 have, by definition, to be the same as glyph pointers in the
11382 corresponding frame matrix. Note that frame matrices have no
11383 marginal areas (see build_frame_matrix). */
11384 window_row = w->current_matrix->rows;
11385 window_row_end = window_row + w->current_matrix->nrows;
11386 frame_row = f->current_matrix->rows + XFASTINT (w->top);
11387 while (window_row < window_row_end)
11388 {
11389 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
11390 struct glyph *end = window_row->glyphs[LAST_AREA];
11391
11392 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
11393 frame_row->glyphs[TEXT_AREA] = start;
11394 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
11395 frame_row->glyphs[LAST_AREA] = end;
11396
11397 /* Disable frame rows whose corresponding window rows have
11398 been disabled in try_window_id. */
11399 if (!window_row->enabled_p)
11400 frame_row->enabled_p = 0;
11401
11402 ++window_row, ++frame_row;
11403 }
11404 }
11405
11406
11407 /* Find the glyph row in window W containing CHARPOS. Consider all
11408 rows between START and END (not inclusive). END null means search
11409 all rows to the end of the display area of W. Value is the row
11410 containing CHARPOS or null. */
11411
11412 struct glyph_row *
11413 row_containing_pos (w, charpos, start, end, dy)
11414 struct window *w;
11415 int charpos;
11416 struct glyph_row *start, *end;
11417 int dy;
11418 {
11419 struct glyph_row *row = start;
11420 int last_y;
11421
11422 /* If we happen to start on a header-line, skip that. */
11423 if (row->mode_line_p)
11424 ++row;
11425
11426 if ((end && row >= end) || !row->enabled_p)
11427 return NULL;
11428
11429 last_y = window_text_bottom_y (w) - dy;
11430
11431 while ((end == NULL || row < end)
11432 && MATRIX_ROW_BOTTOM_Y (row) < last_y
11433 && (MATRIX_ROW_END_CHARPOS (row) < charpos
11434 || (MATRIX_ROW_END_CHARPOS (row) == charpos
11435 /* The end position of a row equals the start
11436 position of the next row. If CHARPOS is there, we
11437 would rather display it in the next line, except
11438 when this line ends in ZV. */
11439 && !row->ends_at_zv_p
11440 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))
11441 ++row;
11442
11443 /* Give up if CHARPOS not found. */
11444 if ((end && row >= end)
11445 || charpos < MATRIX_ROW_START_CHARPOS (row)
11446 || charpos > MATRIX_ROW_END_CHARPOS (row))
11447 row = NULL;
11448
11449 return row;
11450 }
11451
11452
11453 /* Try to redisplay window W by reusing its existing display. W's
11454 current matrix must be up to date when this function is called,
11455 i.e. window_end_valid must not be nil.
11456
11457 Value is
11458
11459 1 if display has been updated
11460 0 if otherwise unsuccessful
11461 -1 if redisplay with same window start is known not to succeed
11462
11463 The following steps are performed:
11464
11465 1. Find the last row in the current matrix of W that is not
11466 affected by changes at the start of current_buffer. If no such row
11467 is found, give up.
11468
11469 2. Find the first row in W's current matrix that is not affected by
11470 changes at the end of current_buffer. Maybe there is no such row.
11471
11472 3. Display lines beginning with the row + 1 found in step 1 to the
11473 row found in step 2 or, if step 2 didn't find a row, to the end of
11474 the window.
11475
11476 4. If cursor is not known to appear on the window, give up.
11477
11478 5. If display stopped at the row found in step 2, scroll the
11479 display and current matrix as needed.
11480
11481 6. Maybe display some lines at the end of W, if we must. This can
11482 happen under various circumstances, like a partially visible line
11483 becoming fully visible, or because newly displayed lines are displayed
11484 in smaller font sizes.
11485
11486 7. Update W's window end information. */
11487
11488 static int
11489 try_window_id (w)
11490 struct window *w;
11491 {
11492 struct frame *f = XFRAME (w->frame);
11493 struct glyph_matrix *current_matrix = w->current_matrix;
11494 struct glyph_matrix *desired_matrix = w->desired_matrix;
11495 struct glyph_row *last_unchanged_at_beg_row;
11496 struct glyph_row *first_unchanged_at_end_row;
11497 struct glyph_row *row;
11498 struct glyph_row *bottom_row;
11499 int bottom_vpos;
11500 struct it it;
11501 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
11502 struct text_pos start_pos;
11503 struct run run;
11504 int first_unchanged_at_end_vpos = 0;
11505 struct glyph_row *last_text_row, *last_text_row_at_end;
11506 struct text_pos start;
11507 int first_changed_charpos, last_changed_charpos;
11508
11509 #if GLYPH_DEBUG
11510 if (inhibit_try_window_id)
11511 return 0;
11512 #endif
11513
11514 /* This is handy for debugging. */
11515 #if 0
11516 #define GIVE_UP(X) \
11517 do { \
11518 fprintf (stderr, "try_window_id give up %d\n", (X)); \
11519 return 0; \
11520 } while (0)
11521 #else
11522 #define GIVE_UP(X) return 0
11523 #endif
11524
11525 SET_TEXT_POS_FROM_MARKER (start, w->start);
11526
11527 /* Don't use this for mini-windows because these can show
11528 messages and mini-buffers, and we don't handle that here. */
11529 if (MINI_WINDOW_P (w))
11530 GIVE_UP (1);
11531
11532 /* This flag is used to prevent redisplay optimizations. */
11533 if (windows_or_buffers_changed || cursor_type_changed)
11534 GIVE_UP (2);
11535
11536 /* Verify that narrowing has not changed.
11537 Also verify that we were not told to prevent redisplay optimizations.
11538 It would be nice to further
11539 reduce the number of cases where this prevents try_window_id. */
11540 if (current_buffer->clip_changed
11541 || current_buffer->prevent_redisplay_optimizations_p)
11542 GIVE_UP (3);
11543
11544 /* Window must either use window-based redisplay or be full width. */
11545 if (!FRAME_WINDOW_P (f)
11546 && (!line_ins_del_ok
11547 || !WINDOW_FULL_WIDTH_P (w)))
11548 GIVE_UP (4);
11549
11550 /* Give up if point is not known NOT to appear in W. */
11551 if (PT < CHARPOS (start))
11552 GIVE_UP (5);
11553
11554 /* Another way to prevent redisplay optimizations. */
11555 if (XFASTINT (w->last_modified) == 0)
11556 GIVE_UP (6);
11557
11558 /* Verify that window is not hscrolled. */
11559 if (XFASTINT (w->hscroll) != 0)
11560 GIVE_UP (7);
11561
11562 /* Verify that display wasn't paused. */
11563 if (NILP (w->window_end_valid))
11564 GIVE_UP (8);
11565
11566 /* Can't use this if highlighting a region because a cursor movement
11567 will do more than just set the cursor. */
11568 if (!NILP (Vtransient_mark_mode)
11569 && !NILP (current_buffer->mark_active))
11570 GIVE_UP (9);
11571
11572 /* Likewise if highlighting trailing whitespace. */
11573 if (!NILP (Vshow_trailing_whitespace))
11574 GIVE_UP (11);
11575
11576 /* Likewise if showing a region. */
11577 if (!NILP (w->region_showing))
11578 GIVE_UP (10);
11579
11580 /* Can use this if overlay arrow position and or string have changed. */
11581 if (!EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
11582 || !EQ (last_arrow_string, Voverlay_arrow_string))
11583 GIVE_UP (12);
11584
11585
11586 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
11587 only if buffer has really changed. The reason is that the gap is
11588 initially at Z for freshly visited files. The code below would
11589 set end_unchanged to 0 in that case. */
11590 if (MODIFF > SAVE_MODIFF
11591 /* This seems to happen sometimes after saving a buffer. */
11592 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
11593 {
11594 if (GPT - BEG < BEG_UNCHANGED)
11595 BEG_UNCHANGED = GPT - BEG;
11596 if (Z - GPT < END_UNCHANGED)
11597 END_UNCHANGED = Z - GPT;
11598 }
11599
11600 /* The position of the first and last character that has been changed. */
11601 first_changed_charpos = BEG + BEG_UNCHANGED;
11602 last_changed_charpos = Z - END_UNCHANGED;
11603
11604 /* If window starts after a line end, and the last change is in
11605 front of that newline, then changes don't affect the display.
11606 This case happens with stealth-fontification. Note that although
11607 the display is unchanged, glyph positions in the matrix have to
11608 be adjusted, of course. */
11609 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
11610 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
11611 && ((last_changed_charpos < CHARPOS (start)
11612 && CHARPOS (start) == BEGV)
11613 || (last_changed_charpos < CHARPOS (start) - 1
11614 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
11615 {
11616 int Z_old, delta, Z_BYTE_old, delta_bytes;
11617 struct glyph_row *r0;
11618
11619 /* Compute how many chars/bytes have been added to or removed
11620 from the buffer. */
11621 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
11622 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
11623 delta = Z - Z_old;
11624 delta_bytes = Z_BYTE - Z_BYTE_old;
11625
11626 /* Give up if PT is not in the window. Note that it already has
11627 been checked at the start of try_window_id that PT is not in
11628 front of the window start. */
11629 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
11630 GIVE_UP (13);
11631
11632 /* If window start is unchanged, we can reuse the whole matrix
11633 as is, after adjusting glyph positions. No need to compute
11634 the window end again, since its offset from Z hasn't changed. */
11635 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
11636 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
11637 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes)
11638 {
11639 /* Adjust positions in the glyph matrix. */
11640 if (delta || delta_bytes)
11641 {
11642 struct glyph_row *r1
11643 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11644 increment_matrix_positions (w->current_matrix,
11645 MATRIX_ROW_VPOS (r0, current_matrix),
11646 MATRIX_ROW_VPOS (r1, current_matrix),
11647 delta, delta_bytes);
11648 }
11649
11650 /* Set the cursor. */
11651 row = row_containing_pos (w, PT, r0, NULL, 0);
11652 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
11653 return 1;
11654 }
11655 }
11656
11657 /* Handle the case that changes are all below what is displayed in
11658 the window, and that PT is in the window. This shortcut cannot
11659 be taken if ZV is visible in the window, and text has been added
11660 there that is visible in the window. */
11661 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
11662 /* ZV is not visible in the window, or there are no
11663 changes at ZV, actually. */
11664 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
11665 || first_changed_charpos == last_changed_charpos))
11666 {
11667 struct glyph_row *r0;
11668
11669 /* Give up if PT is not in the window. Note that it already has
11670 been checked at the start of try_window_id that PT is not in
11671 front of the window start. */
11672 if (PT >= MATRIX_ROW_END_CHARPOS (row))
11673 GIVE_UP (14);
11674
11675 /* If window start is unchanged, we can reuse the whole matrix
11676 as is, without changing glyph positions since no text has
11677 been added/removed in front of the window end. */
11678 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
11679 if (TEXT_POS_EQUAL_P (start, r0->start.pos))
11680 {
11681 /* We have to compute the window end anew since text
11682 can have been added/removed after it. */
11683 w->window_end_pos
11684 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
11685 w->window_end_bytepos
11686 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11687
11688 /* Set the cursor. */
11689 row = row_containing_pos (w, PT, r0, NULL, 0);
11690 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
11691 return 2;
11692 }
11693 }
11694
11695 /* Give up if window start is in the changed area.
11696
11697 The condition used to read
11698
11699 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
11700
11701 but why that was tested escapes me at the moment. */
11702 if (CHARPOS (start) >= first_changed_charpos
11703 && CHARPOS (start) <= last_changed_charpos)
11704 GIVE_UP (15);
11705
11706 /* Check that window start agrees with the start of the first glyph
11707 row in its current matrix. Check this after we know the window
11708 start is not in changed text, otherwise positions would not be
11709 comparable. */
11710 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
11711 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
11712 GIVE_UP (16);
11713
11714 /* Give up if the window ends in strings. Overlay strings
11715 at the end are difficult to handle, so don't try. */
11716 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
11717 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
11718 GIVE_UP (20);
11719
11720 /* Compute the position at which we have to start displaying new
11721 lines. Some of the lines at the top of the window might be
11722 reusable because they are not displaying changed text. Find the
11723 last row in W's current matrix not affected by changes at the
11724 start of current_buffer. Value is null if changes start in the
11725 first line of window. */
11726 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
11727 if (last_unchanged_at_beg_row)
11728 {
11729 /* Avoid starting to display in the moddle of a character, a TAB
11730 for instance. This is easier than to set up the iterator
11731 exactly, and it's not a frequent case, so the additional
11732 effort wouldn't really pay off. */
11733 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
11734 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
11735 && last_unchanged_at_beg_row > w->current_matrix->rows)
11736 --last_unchanged_at_beg_row;
11737
11738 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
11739 GIVE_UP (17);
11740
11741 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
11742 GIVE_UP (18);
11743 start_pos = it.current.pos;
11744
11745 /* Start displaying new lines in the desired matrix at the same
11746 vpos we would use in the current matrix, i.e. below
11747 last_unchanged_at_beg_row. */
11748 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
11749 current_matrix);
11750 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11751 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
11752
11753 xassert (it.hpos == 0 && it.current_x == 0);
11754 }
11755 else
11756 {
11757 /* There are no reusable lines at the start of the window.
11758 Start displaying in the first line. */
11759 start_display (&it, w, start);
11760 start_pos = it.current.pos;
11761 }
11762
11763 /* Find the first row that is not affected by changes at the end of
11764 the buffer. Value will be null if there is no unchanged row, in
11765 which case we must redisplay to the end of the window. delta
11766 will be set to the value by which buffer positions beginning with
11767 first_unchanged_at_end_row have to be adjusted due to text
11768 changes. */
11769 first_unchanged_at_end_row
11770 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
11771 IF_DEBUG (debug_delta = delta);
11772 IF_DEBUG (debug_delta_bytes = delta_bytes);
11773
11774 /* Set stop_pos to the buffer position up to which we will have to
11775 display new lines. If first_unchanged_at_end_row != NULL, this
11776 is the buffer position of the start of the line displayed in that
11777 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
11778 that we don't stop at a buffer position. */
11779 stop_pos = 0;
11780 if (first_unchanged_at_end_row)
11781 {
11782 xassert (last_unchanged_at_beg_row == NULL
11783 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
11784
11785 /* If this is a continuation line, move forward to the next one
11786 that isn't. Changes in lines above affect this line.
11787 Caution: this may move first_unchanged_at_end_row to a row
11788 not displaying text. */
11789 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
11790 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11791 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11792 < it.last_visible_y))
11793 ++first_unchanged_at_end_row;
11794
11795 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11796 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11797 >= it.last_visible_y))
11798 first_unchanged_at_end_row = NULL;
11799 else
11800 {
11801 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
11802 + delta);
11803 first_unchanged_at_end_vpos
11804 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
11805 xassert (stop_pos >= Z - END_UNCHANGED);
11806 }
11807 }
11808 else if (last_unchanged_at_beg_row == NULL)
11809 GIVE_UP (19);
11810
11811
11812 #if GLYPH_DEBUG
11813
11814 /* Either there is no unchanged row at the end, or the one we have
11815 now displays text. This is a necessary condition for the window
11816 end pos calculation at the end of this function. */
11817 xassert (first_unchanged_at_end_row == NULL
11818 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
11819
11820 debug_last_unchanged_at_beg_vpos
11821 = (last_unchanged_at_beg_row
11822 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
11823 : -1);
11824 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
11825
11826 #endif /* GLYPH_DEBUG != 0 */
11827
11828
11829 /* Display new lines. Set last_text_row to the last new line
11830 displayed which has text on it, i.e. might end up as being the
11831 line where the window_end_vpos is. */
11832 w->cursor.vpos = -1;
11833 last_text_row = NULL;
11834 overlay_arrow_seen = 0;
11835 while (it.current_y < it.last_visible_y
11836 && !fonts_changed_p
11837 && (first_unchanged_at_end_row == NULL
11838 || IT_CHARPOS (it) < stop_pos))
11839 {
11840 if (display_line (&it))
11841 last_text_row = it.glyph_row - 1;
11842 }
11843
11844 if (fonts_changed_p)
11845 return -1;
11846
11847
11848 /* Compute differences in buffer positions, y-positions etc. for
11849 lines reused at the bottom of the window. Compute what we can
11850 scroll. */
11851 if (first_unchanged_at_end_row
11852 /* No lines reused because we displayed everything up to the
11853 bottom of the window. */
11854 && it.current_y < it.last_visible_y)
11855 {
11856 dvpos = (it.vpos
11857 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
11858 current_matrix));
11859 dy = it.current_y - first_unchanged_at_end_row->y;
11860 run.current_y = first_unchanged_at_end_row->y;
11861 run.desired_y = run.current_y + dy;
11862 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
11863 }
11864 else
11865 {
11866 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
11867 first_unchanged_at_end_row = NULL;
11868 }
11869 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
11870
11871
11872 /* Find the cursor if not already found. We have to decide whether
11873 PT will appear on this window (it sometimes doesn't, but this is
11874 not a very frequent case.) This decision has to be made before
11875 the current matrix is altered. A value of cursor.vpos < 0 means
11876 that PT is either in one of the lines beginning at
11877 first_unchanged_at_end_row or below the window. Don't care for
11878 lines that might be displayed later at the window end; as
11879 mentioned, this is not a frequent case. */
11880 if (w->cursor.vpos < 0)
11881 {
11882 /* Cursor in unchanged rows at the top? */
11883 if (PT < CHARPOS (start_pos)
11884 && last_unchanged_at_beg_row)
11885 {
11886 row = row_containing_pos (w, PT,
11887 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
11888 last_unchanged_at_beg_row + 1, 0);
11889 if (row)
11890 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11891 }
11892
11893 /* Start from first_unchanged_at_end_row looking for PT. */
11894 else if (first_unchanged_at_end_row)
11895 {
11896 row = row_containing_pos (w, PT - delta,
11897 first_unchanged_at_end_row, NULL, 0);
11898 if (row)
11899 set_cursor_from_row (w, row, w->current_matrix, delta,
11900 delta_bytes, dy, dvpos);
11901 }
11902
11903 /* Give up if cursor was not found. */
11904 if (w->cursor.vpos < 0)
11905 {
11906 clear_glyph_matrix (w->desired_matrix);
11907 return -1;
11908 }
11909 }
11910
11911 /* Don't let the cursor end in the scroll margins. */
11912 {
11913 int this_scroll_margin, cursor_height;
11914
11915 this_scroll_margin = max (0, scroll_margin);
11916 this_scroll_margin = min (this_scroll_margin,
11917 XFASTINT (w->height) / 4);
11918 this_scroll_margin *= CANON_Y_UNIT (it.f);
11919 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
11920
11921 if ((w->cursor.y < this_scroll_margin
11922 && CHARPOS (start) > BEGV)
11923 /* Don't take scroll margin into account at the bottom because
11924 old redisplay didn't do it either. */
11925 || w->cursor.y + cursor_height > it.last_visible_y)
11926 {
11927 w->cursor.vpos = -1;
11928 clear_glyph_matrix (w->desired_matrix);
11929 return -1;
11930 }
11931 }
11932
11933 /* Scroll the display. Do it before changing the current matrix so
11934 that xterm.c doesn't get confused about where the cursor glyph is
11935 found. */
11936 if (dy && run.height)
11937 {
11938 update_begin (f);
11939
11940 if (FRAME_WINDOW_P (f))
11941 {
11942 rif->update_window_begin_hook (w);
11943 rif->clear_mouse_face (w);
11944 rif->scroll_run_hook (w, &run);
11945 rif->update_window_end_hook (w, 0, 0);
11946 }
11947 else
11948 {
11949 /* Terminal frame. In this case, dvpos gives the number of
11950 lines to scroll by; dvpos < 0 means scroll up. */
11951 int first_unchanged_at_end_vpos
11952 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
11953 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
11954 int end = (XFASTINT (w->top)
11955 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
11956 + window_internal_height (w));
11957
11958 /* Perform the operation on the screen. */
11959 if (dvpos > 0)
11960 {
11961 /* Scroll last_unchanged_at_beg_row to the end of the
11962 window down dvpos lines. */
11963 set_terminal_window (end);
11964
11965 /* On dumb terminals delete dvpos lines at the end
11966 before inserting dvpos empty lines. */
11967 if (!scroll_region_ok)
11968 ins_del_lines (end - dvpos, -dvpos);
11969
11970 /* Insert dvpos empty lines in front of
11971 last_unchanged_at_beg_row. */
11972 ins_del_lines (from, dvpos);
11973 }
11974 else if (dvpos < 0)
11975 {
11976 /* Scroll up last_unchanged_at_beg_vpos to the end of
11977 the window to last_unchanged_at_beg_vpos - |dvpos|. */
11978 set_terminal_window (end);
11979
11980 /* Delete dvpos lines in front of
11981 last_unchanged_at_beg_vpos. ins_del_lines will set
11982 the cursor to the given vpos and emit |dvpos| delete
11983 line sequences. */
11984 ins_del_lines (from + dvpos, dvpos);
11985
11986 /* On a dumb terminal insert dvpos empty lines at the
11987 end. */
11988 if (!scroll_region_ok)
11989 ins_del_lines (end + dvpos, -dvpos);
11990 }
11991
11992 set_terminal_window (0);
11993 }
11994
11995 update_end (f);
11996 }
11997
11998 /* Shift reused rows of the current matrix to the right position.
11999 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
12000 text. */
12001 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
12002 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
12003 if (dvpos < 0)
12004 {
12005 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
12006 bottom_vpos, dvpos);
12007 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
12008 bottom_vpos, 0);
12009 }
12010 else if (dvpos > 0)
12011 {
12012 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
12013 bottom_vpos, dvpos);
12014 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
12015 first_unchanged_at_end_vpos + dvpos, 0);
12016 }
12017
12018 /* For frame-based redisplay, make sure that current frame and window
12019 matrix are in sync with respect to glyph memory. */
12020 if (!FRAME_WINDOW_P (f))
12021 sync_frame_with_window_matrix_rows (w);
12022
12023 /* Adjust buffer positions in reused rows. */
12024 if (delta)
12025 increment_matrix_positions (current_matrix,
12026 first_unchanged_at_end_vpos + dvpos,
12027 bottom_vpos, delta, delta_bytes);
12028
12029 /* Adjust Y positions. */
12030 if (dy)
12031 shift_glyph_matrix (w, current_matrix,
12032 first_unchanged_at_end_vpos + dvpos,
12033 bottom_vpos, dy);
12034
12035 if (first_unchanged_at_end_row)
12036 first_unchanged_at_end_row += dvpos;
12037
12038 /* If scrolling up, there may be some lines to display at the end of
12039 the window. */
12040 last_text_row_at_end = NULL;
12041 if (dy < 0)
12042 {
12043 /* Scrolling up can leave for example a partially visible line
12044 at the end of the window to be redisplayed. */
12045 /* Set last_row to the glyph row in the current matrix where the
12046 window end line is found. It has been moved up or down in
12047 the matrix by dvpos. */
12048 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
12049 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
12050
12051 /* If last_row is the window end line, it should display text. */
12052 xassert (last_row->displays_text_p);
12053
12054 /* If window end line was partially visible before, begin
12055 displaying at that line. Otherwise begin displaying with the
12056 line following it. */
12057 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
12058 {
12059 init_to_row_start (&it, w, last_row);
12060 it.vpos = last_vpos;
12061 it.current_y = last_row->y;
12062 }
12063 else
12064 {
12065 init_to_row_end (&it, w, last_row);
12066 it.vpos = 1 + last_vpos;
12067 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
12068 ++last_row;
12069 }
12070
12071 /* We may start in a continuation line. If so, we have to
12072 get the right continuation_lines_width and current_x. */
12073 it.continuation_lines_width = last_row->continuation_lines_width;
12074 it.hpos = it.current_x = 0;
12075
12076 /* Display the rest of the lines at the window end. */
12077 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
12078 while (it.current_y < it.last_visible_y
12079 && !fonts_changed_p)
12080 {
12081 /* Is it always sure that the display agrees with lines in
12082 the current matrix? I don't think so, so we mark rows
12083 displayed invalid in the current matrix by setting their
12084 enabled_p flag to zero. */
12085 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
12086 if (display_line (&it))
12087 last_text_row_at_end = it.glyph_row - 1;
12088 }
12089 }
12090
12091 /* Update window_end_pos and window_end_vpos. */
12092 if (first_unchanged_at_end_row
12093 && first_unchanged_at_end_row->y < it.last_visible_y
12094 && !last_text_row_at_end)
12095 {
12096 /* Window end line if one of the preserved rows from the current
12097 matrix. Set row to the last row displaying text in current
12098 matrix starting at first_unchanged_at_end_row, after
12099 scrolling. */
12100 xassert (first_unchanged_at_end_row->displays_text_p);
12101 row = find_last_row_displaying_text (w->current_matrix, &it,
12102 first_unchanged_at_end_row);
12103 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
12104
12105 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
12106 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
12107 w->window_end_vpos
12108 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
12109 xassert (w->window_end_bytepos >= 0);
12110 IF_DEBUG (debug_method_add (w, "A"));
12111 }
12112 else if (last_text_row_at_end)
12113 {
12114 w->window_end_pos
12115 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
12116 w->window_end_bytepos
12117 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
12118 w->window_end_vpos
12119 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
12120 xassert (w->window_end_bytepos >= 0);
12121 IF_DEBUG (debug_method_add (w, "B"));
12122 }
12123 else if (last_text_row)
12124 {
12125 /* We have displayed either to the end of the window or at the
12126 end of the window, i.e. the last row with text is to be found
12127 in the desired matrix. */
12128 w->window_end_pos
12129 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12130 w->window_end_bytepos
12131 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12132 w->window_end_vpos
12133 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
12134 xassert (w->window_end_bytepos >= 0);
12135 }
12136 else if (first_unchanged_at_end_row == NULL
12137 && last_text_row == NULL
12138 && last_text_row_at_end == NULL)
12139 {
12140 /* Displayed to end of window, but no line containing text was
12141 displayed. Lines were deleted at the end of the window. */
12142 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
12143 int vpos = XFASTINT (w->window_end_vpos);
12144 struct glyph_row *current_row = current_matrix->rows + vpos;
12145 struct glyph_row *desired_row = desired_matrix->rows + vpos;
12146
12147 for (row = NULL;
12148 row == NULL && vpos >= first_vpos;
12149 --vpos, --current_row, --desired_row)
12150 {
12151 if (desired_row->enabled_p)
12152 {
12153 if (desired_row->displays_text_p)
12154 row = desired_row;
12155 }
12156 else if (current_row->displays_text_p)
12157 row = current_row;
12158 }
12159
12160 xassert (row != NULL);
12161 w->window_end_vpos = make_number (vpos + 1);
12162 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
12163 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
12164 xassert (w->window_end_bytepos >= 0);
12165 IF_DEBUG (debug_method_add (w, "C"));
12166 }
12167 else
12168 abort ();
12169
12170 #if 0 /* This leads to problems, for instance when the cursor is
12171 at ZV, and the cursor line displays no text. */
12172 /* Disable rows below what's displayed in the window. This makes
12173 debugging easier. */
12174 enable_glyph_matrix_rows (current_matrix,
12175 XFASTINT (w->window_end_vpos) + 1,
12176 bottom_vpos, 0);
12177 #endif
12178
12179 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
12180 debug_end_vpos = XFASTINT (w->window_end_vpos));
12181
12182 /* Record that display has not been completed. */
12183 w->window_end_valid = Qnil;
12184 w->desired_matrix->no_scrolling_p = 1;
12185 return 3;
12186
12187 #undef GIVE_UP
12188 }
12189
12190
12191 \f
12192 /***********************************************************************
12193 More debugging support
12194 ***********************************************************************/
12195
12196 #if GLYPH_DEBUG
12197
12198 void dump_glyph_row P_ ((struct glyph_row *, int, int));
12199 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
12200 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
12201
12202
12203 /* Dump the contents of glyph matrix MATRIX on stderr.
12204
12205 GLYPHS 0 means don't show glyph contents.
12206 GLYPHS 1 means show glyphs in short form
12207 GLYPHS > 1 means show glyphs in long form. */
12208
12209 void
12210 dump_glyph_matrix (matrix, glyphs)
12211 struct glyph_matrix *matrix;
12212 int glyphs;
12213 {
12214 int i;
12215 for (i = 0; i < matrix->nrows; ++i)
12216 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
12217 }
12218
12219
12220 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
12221 the glyph row and area where the glyph comes from. */
12222
12223 void
12224 dump_glyph (row, glyph, area)
12225 struct glyph_row *row;
12226 struct glyph *glyph;
12227 int area;
12228 {
12229 if (glyph->type == CHAR_GLYPH)
12230 {
12231 fprintf (stderr,
12232 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12233 glyph - row->glyphs[TEXT_AREA],
12234 'C',
12235 glyph->charpos,
12236 (BUFFERP (glyph->object)
12237 ? 'B'
12238 : (STRINGP (glyph->object)
12239 ? 'S'
12240 : '-')),
12241 glyph->pixel_width,
12242 glyph->u.ch,
12243 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
12244 ? glyph->u.ch
12245 : '.'),
12246 glyph->face_id,
12247 glyph->left_box_line_p,
12248 glyph->right_box_line_p);
12249 }
12250 else if (glyph->type == STRETCH_GLYPH)
12251 {
12252 fprintf (stderr,
12253 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12254 glyph - row->glyphs[TEXT_AREA],
12255 'S',
12256 glyph->charpos,
12257 (BUFFERP (glyph->object)
12258 ? 'B'
12259 : (STRINGP (glyph->object)
12260 ? 'S'
12261 : '-')),
12262 glyph->pixel_width,
12263 0,
12264 '.',
12265 glyph->face_id,
12266 glyph->left_box_line_p,
12267 glyph->right_box_line_p);
12268 }
12269 else if (glyph->type == IMAGE_GLYPH)
12270 {
12271 fprintf (stderr,
12272 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12273 glyph - row->glyphs[TEXT_AREA],
12274 'I',
12275 glyph->charpos,
12276 (BUFFERP (glyph->object)
12277 ? 'B'
12278 : (STRINGP (glyph->object)
12279 ? 'S'
12280 : '-')),
12281 glyph->pixel_width,
12282 glyph->u.img_id,
12283 '.',
12284 glyph->face_id,
12285 glyph->left_box_line_p,
12286 glyph->right_box_line_p);
12287 }
12288 }
12289
12290
12291 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
12292 GLYPHS 0 means don't show glyph contents.
12293 GLYPHS 1 means show glyphs in short form
12294 GLYPHS > 1 means show glyphs in long form. */
12295
12296 void
12297 dump_glyph_row (row, vpos, glyphs)
12298 struct glyph_row *row;
12299 int vpos, glyphs;
12300 {
12301 if (glyphs != 1)
12302 {
12303 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
12304 fprintf (stderr, "=======================================================================\n");
12305
12306 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d\
12307 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
12308 vpos,
12309 MATRIX_ROW_START_CHARPOS (row),
12310 MATRIX_ROW_END_CHARPOS (row),
12311 row->used[TEXT_AREA],
12312 row->contains_overlapping_glyphs_p,
12313 row->enabled_p,
12314 row->truncated_on_left_p,
12315 row->truncated_on_right_p,
12316 row->overlay_arrow_p,
12317 row->continued_p,
12318 MATRIX_ROW_CONTINUATION_LINE_P (row),
12319 row->displays_text_p,
12320 row->ends_at_zv_p,
12321 row->fill_line_p,
12322 row->ends_in_middle_of_char_p,
12323 row->starts_in_middle_of_char_p,
12324 row->mouse_face_p,
12325 row->x,
12326 row->y,
12327 row->pixel_width,
12328 row->height,
12329 row->visible_height,
12330 row->ascent,
12331 row->phys_ascent);
12332 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
12333 row->end.overlay_string_index,
12334 row->continuation_lines_width);
12335 fprintf (stderr, "%9d %5d\n",
12336 CHARPOS (row->start.string_pos),
12337 CHARPOS (row->end.string_pos));
12338 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
12339 row->end.dpvec_index);
12340 }
12341
12342 if (glyphs > 1)
12343 {
12344 int area;
12345
12346 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12347 {
12348 struct glyph *glyph = row->glyphs[area];
12349 struct glyph *glyph_end = glyph + row->used[area];
12350
12351 /* Glyph for a line end in text. */
12352 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
12353 ++glyph_end;
12354
12355 if (glyph < glyph_end)
12356 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
12357
12358 for (; glyph < glyph_end; ++glyph)
12359 dump_glyph (row, glyph, area);
12360 }
12361 }
12362 else if (glyphs == 1)
12363 {
12364 int area;
12365
12366 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12367 {
12368 char *s = (char *) alloca (row->used[area] + 1);
12369 int i;
12370
12371 for (i = 0; i < row->used[area]; ++i)
12372 {
12373 struct glyph *glyph = row->glyphs[area] + i;
12374 if (glyph->type == CHAR_GLYPH
12375 && glyph->u.ch < 0x80
12376 && glyph->u.ch >= ' ')
12377 s[i] = glyph->u.ch;
12378 else
12379 s[i] = '.';
12380 }
12381
12382 s[i] = '\0';
12383 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
12384 }
12385 }
12386 }
12387
12388
12389 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
12390 Sdump_glyph_matrix, 0, 1, "p",
12391 doc: /* Dump the current matrix of the selected window to stderr.
12392 Shows contents of glyph row structures. With non-nil
12393 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
12394 glyphs in short form, otherwise show glyphs in long form. */)
12395 (glyphs)
12396 Lisp_Object glyphs;
12397 {
12398 struct window *w = XWINDOW (selected_window);
12399 struct buffer *buffer = XBUFFER (w->buffer);
12400
12401 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
12402 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
12403 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
12404 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
12405 fprintf (stderr, "=============================================\n");
12406 dump_glyph_matrix (w->current_matrix,
12407 NILP (glyphs) ? 0 : XINT (glyphs));
12408 return Qnil;
12409 }
12410
12411
12412 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
12413 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
12414 ()
12415 {
12416 struct frame *f = XFRAME (selected_frame);
12417 dump_glyph_matrix (f->current_matrix, 1);
12418 return Qnil;
12419 }
12420
12421
12422 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
12423 doc: /* Dump glyph row ROW to stderr.
12424 GLYPH 0 means don't dump glyphs.
12425 GLYPH 1 means dump glyphs in short form.
12426 GLYPH > 1 or omitted means dump glyphs in long form. */)
12427 (row, glyphs)
12428 Lisp_Object row, glyphs;
12429 {
12430 struct glyph_matrix *matrix;
12431 int vpos;
12432
12433 CHECK_NUMBER (row);
12434 matrix = XWINDOW (selected_window)->current_matrix;
12435 vpos = XINT (row);
12436 if (vpos >= 0 && vpos < matrix->nrows)
12437 dump_glyph_row (MATRIX_ROW (matrix, vpos),
12438 vpos,
12439 INTEGERP (glyphs) ? XINT (glyphs) : 2);
12440 return Qnil;
12441 }
12442
12443
12444 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
12445 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
12446 GLYPH 0 means don't dump glyphs.
12447 GLYPH 1 means dump glyphs in short form.
12448 GLYPH > 1 or omitted means dump glyphs in long form. */)
12449 (row, glyphs)
12450 Lisp_Object row, glyphs;
12451 {
12452 struct frame *sf = SELECTED_FRAME ();
12453 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
12454 int vpos;
12455
12456 CHECK_NUMBER (row);
12457 vpos = XINT (row);
12458 if (vpos >= 0 && vpos < m->nrows)
12459 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
12460 INTEGERP (glyphs) ? XINT (glyphs) : 2);
12461 return Qnil;
12462 }
12463
12464
12465 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
12466 doc: /* Toggle tracing of redisplay.
12467 With ARG, turn tracing on if and only if ARG is positive. */)
12468 (arg)
12469 Lisp_Object arg;
12470 {
12471 if (NILP (arg))
12472 trace_redisplay_p = !trace_redisplay_p;
12473 else
12474 {
12475 arg = Fprefix_numeric_value (arg);
12476 trace_redisplay_p = XINT (arg) > 0;
12477 }
12478
12479 return Qnil;
12480 }
12481
12482
12483 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
12484 doc: /* Like `format', but print result to stderr.
12485 usage: (trace-to-stderr STRING &rest OBJECTS) */)
12486 (nargs, args)
12487 int nargs;
12488 Lisp_Object *args;
12489 {
12490 Lisp_Object s = Fformat (nargs, args);
12491 fprintf (stderr, "%s", SDATA (s));
12492 return Qnil;
12493 }
12494
12495 #endif /* GLYPH_DEBUG */
12496
12497
12498 \f
12499 /***********************************************************************
12500 Building Desired Matrix Rows
12501 ***********************************************************************/
12502
12503 /* Return a temporary glyph row holding the glyphs of an overlay
12504 arrow. Only used for non-window-redisplay windows. */
12505
12506 static struct glyph_row *
12507 get_overlay_arrow_glyph_row (w)
12508 struct window *w;
12509 {
12510 struct frame *f = XFRAME (WINDOW_FRAME (w));
12511 struct buffer *buffer = XBUFFER (w->buffer);
12512 struct buffer *old = current_buffer;
12513 unsigned char *arrow_string = SDATA (Voverlay_arrow_string);
12514 int arrow_len = SCHARS (Voverlay_arrow_string);
12515 unsigned char *arrow_end = arrow_string + arrow_len;
12516 unsigned char *p;
12517 struct it it;
12518 int multibyte_p;
12519 int n_glyphs_before;
12520
12521 set_buffer_temp (buffer);
12522 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
12523 it.glyph_row->used[TEXT_AREA] = 0;
12524 SET_TEXT_POS (it.position, 0, 0);
12525
12526 multibyte_p = !NILP (buffer->enable_multibyte_characters);
12527 p = arrow_string;
12528 while (p < arrow_end)
12529 {
12530 Lisp_Object face, ilisp;
12531
12532 /* Get the next character. */
12533 if (multibyte_p)
12534 it.c = string_char_and_length (p, arrow_len, &it.len);
12535 else
12536 it.c = *p, it.len = 1;
12537 p += it.len;
12538
12539 /* Get its face. */
12540 ilisp = make_number (p - arrow_string);
12541 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
12542 it.face_id = compute_char_face (f, it.c, face);
12543
12544 /* Compute its width, get its glyphs. */
12545 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
12546 SET_TEXT_POS (it.position, -1, -1);
12547 PRODUCE_GLYPHS (&it);
12548
12549 /* If this character doesn't fit any more in the line, we have
12550 to remove some glyphs. */
12551 if (it.current_x > it.last_visible_x)
12552 {
12553 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
12554 break;
12555 }
12556 }
12557
12558 set_buffer_temp (old);
12559 return it.glyph_row;
12560 }
12561
12562
12563 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
12564 glyphs are only inserted for terminal frames since we can't really
12565 win with truncation glyphs when partially visible glyphs are
12566 involved. Which glyphs to insert is determined by
12567 produce_special_glyphs. */
12568
12569 static void
12570 insert_left_trunc_glyphs (it)
12571 struct it *it;
12572 {
12573 struct it truncate_it;
12574 struct glyph *from, *end, *to, *toend;
12575
12576 xassert (!FRAME_WINDOW_P (it->f));
12577
12578 /* Get the truncation glyphs. */
12579 truncate_it = *it;
12580 truncate_it.current_x = 0;
12581 truncate_it.face_id = DEFAULT_FACE_ID;
12582 truncate_it.glyph_row = &scratch_glyph_row;
12583 truncate_it.glyph_row->used[TEXT_AREA] = 0;
12584 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
12585 truncate_it.object = make_number (0);
12586 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
12587
12588 /* Overwrite glyphs from IT with truncation glyphs. */
12589 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
12590 end = from + truncate_it.glyph_row->used[TEXT_AREA];
12591 to = it->glyph_row->glyphs[TEXT_AREA];
12592 toend = to + it->glyph_row->used[TEXT_AREA];
12593
12594 while (from < end)
12595 *to++ = *from++;
12596
12597 /* There may be padding glyphs left over. Overwrite them too. */
12598 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
12599 {
12600 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
12601 while (from < end)
12602 *to++ = *from++;
12603 }
12604
12605 if (to > toend)
12606 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
12607 }
12608
12609
12610 /* Compute the pixel height and width of IT->glyph_row.
12611
12612 Most of the time, ascent and height of a display line will be equal
12613 to the max_ascent and max_height values of the display iterator
12614 structure. This is not the case if
12615
12616 1. We hit ZV without displaying anything. In this case, max_ascent
12617 and max_height will be zero.
12618
12619 2. We have some glyphs that don't contribute to the line height.
12620 (The glyph row flag contributes_to_line_height_p is for future
12621 pixmap extensions).
12622
12623 The first case is easily covered by using default values because in
12624 these cases, the line height does not really matter, except that it
12625 must not be zero. */
12626
12627 static void
12628 compute_line_metrics (it)
12629 struct it *it;
12630 {
12631 struct glyph_row *row = it->glyph_row;
12632 int area, i;
12633
12634 if (FRAME_WINDOW_P (it->f))
12635 {
12636 int i, min_y, max_y;
12637
12638 /* The line may consist of one space only, that was added to
12639 place the cursor on it. If so, the row's height hasn't been
12640 computed yet. */
12641 if (row->height == 0)
12642 {
12643 if (it->max_ascent + it->max_descent == 0)
12644 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
12645 row->ascent = it->max_ascent;
12646 row->height = it->max_ascent + it->max_descent;
12647 row->phys_ascent = it->max_phys_ascent;
12648 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
12649 }
12650
12651 /* Compute the width of this line. */
12652 row->pixel_width = row->x;
12653 for (i = 0; i < row->used[TEXT_AREA]; ++i)
12654 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
12655
12656 xassert (row->pixel_width >= 0);
12657 xassert (row->ascent >= 0 && row->height > 0);
12658
12659 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
12660 || MATRIX_ROW_OVERLAPS_PRED_P (row));
12661
12662 /* If first line's physical ascent is larger than its logical
12663 ascent, use the physical ascent, and make the row taller.
12664 This makes accented characters fully visible. */
12665 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
12666 && row->phys_ascent > row->ascent)
12667 {
12668 row->height += row->phys_ascent - row->ascent;
12669 row->ascent = row->phys_ascent;
12670 }
12671
12672 /* Compute how much of the line is visible. */
12673 row->visible_height = row->height;
12674
12675 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (it->w);
12676 max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
12677
12678 if (row->y < min_y)
12679 row->visible_height -= min_y - row->y;
12680 if (row->y + row->height > max_y)
12681 row->visible_height -= row->y + row->height - max_y;
12682 }
12683 else
12684 {
12685 row->pixel_width = row->used[TEXT_AREA];
12686 if (row->continued_p)
12687 row->pixel_width -= it->continuation_pixel_width;
12688 else if (row->truncated_on_right_p)
12689 row->pixel_width -= it->truncation_pixel_width;
12690 row->ascent = row->phys_ascent = 0;
12691 row->height = row->phys_height = row->visible_height = 1;
12692 }
12693
12694 /* Compute a hash code for this row. */
12695 row->hash = 0;
12696 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12697 for (i = 0; i < row->used[area]; ++i)
12698 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
12699 + row->glyphs[area][i].u.val
12700 + row->glyphs[area][i].face_id
12701 + row->glyphs[area][i].padding_p
12702 + (row->glyphs[area][i].type << 2));
12703
12704 it->max_ascent = it->max_descent = 0;
12705 it->max_phys_ascent = it->max_phys_descent = 0;
12706 }
12707
12708
12709 /* Append one space to the glyph row of iterator IT if doing a
12710 window-based redisplay. DEFAULT_FACE_P non-zero means let the
12711 space have the default face, otherwise let it have the same face as
12712 IT->face_id. Value is non-zero if a space was added.
12713
12714 This function is called to make sure that there is always one glyph
12715 at the end of a glyph row that the cursor can be set on under
12716 window-systems. (If there weren't such a glyph we would not know
12717 how wide and tall a box cursor should be displayed).
12718
12719 At the same time this space let's a nicely handle clearing to the
12720 end of the line if the row ends in italic text. */
12721
12722 static int
12723 append_space (it, default_face_p)
12724 struct it *it;
12725 int default_face_p;
12726 {
12727 if (FRAME_WINDOW_P (it->f))
12728 {
12729 int n = it->glyph_row->used[TEXT_AREA];
12730
12731 if (it->glyph_row->glyphs[TEXT_AREA] + n
12732 < it->glyph_row->glyphs[1 + TEXT_AREA])
12733 {
12734 /* Save some values that must not be changed.
12735 Must save IT->c and IT->len because otherwise
12736 ITERATOR_AT_END_P wouldn't work anymore after
12737 append_space has been called. */
12738 enum display_element_type saved_what = it->what;
12739 int saved_c = it->c, saved_len = it->len;
12740 int saved_x = it->current_x;
12741 int saved_face_id = it->face_id;
12742 struct text_pos saved_pos;
12743 Lisp_Object saved_object;
12744 struct face *face;
12745
12746 saved_object = it->object;
12747 saved_pos = it->position;
12748
12749 it->what = IT_CHARACTER;
12750 bzero (&it->position, sizeof it->position);
12751 it->object = make_number (0);
12752 it->c = ' ';
12753 it->len = 1;
12754
12755 if (default_face_p)
12756 it->face_id = DEFAULT_FACE_ID;
12757 else if (it->face_before_selective_p)
12758 it->face_id = it->saved_face_id;
12759 face = FACE_FROM_ID (it->f, it->face_id);
12760 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
12761
12762 PRODUCE_GLYPHS (it);
12763
12764 it->current_x = saved_x;
12765 it->object = saved_object;
12766 it->position = saved_pos;
12767 it->what = saved_what;
12768 it->face_id = saved_face_id;
12769 it->len = saved_len;
12770 it->c = saved_c;
12771 return 1;
12772 }
12773 }
12774
12775 return 0;
12776 }
12777
12778
12779 /* Extend the face of the last glyph in the text area of IT->glyph_row
12780 to the end of the display line. Called from display_line.
12781 If the glyph row is empty, add a space glyph to it so that we
12782 know the face to draw. Set the glyph row flag fill_line_p. */
12783
12784 static void
12785 extend_face_to_end_of_line (it)
12786 struct it *it;
12787 {
12788 struct face *face;
12789 struct frame *f = it->f;
12790
12791 /* If line is already filled, do nothing. */
12792 if (it->current_x >= it->last_visible_x)
12793 return;
12794
12795 /* Face extension extends the background and box of IT->face_id
12796 to the end of the line. If the background equals the background
12797 of the frame, we don't have to do anything. */
12798 if (it->face_before_selective_p)
12799 face = FACE_FROM_ID (it->f, it->saved_face_id);
12800 else
12801 face = FACE_FROM_ID (f, it->face_id);
12802
12803 if (FRAME_WINDOW_P (f)
12804 && face->box == FACE_NO_BOX
12805 && face->background == FRAME_BACKGROUND_PIXEL (f)
12806 && !face->stipple)
12807 return;
12808
12809 /* Set the glyph row flag indicating that the face of the last glyph
12810 in the text area has to be drawn to the end of the text area. */
12811 it->glyph_row->fill_line_p = 1;
12812
12813 /* If current character of IT is not ASCII, make sure we have the
12814 ASCII face. This will be automatically undone the next time
12815 get_next_display_element returns a multibyte character. Note
12816 that the character will always be single byte in unibyte text. */
12817 if (!SINGLE_BYTE_CHAR_P (it->c))
12818 {
12819 it->face_id = FACE_FOR_CHAR (f, face, 0);
12820 }
12821
12822 if (FRAME_WINDOW_P (f))
12823 {
12824 /* If the row is empty, add a space with the current face of IT,
12825 so that we know which face to draw. */
12826 if (it->glyph_row->used[TEXT_AREA] == 0)
12827 {
12828 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
12829 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
12830 it->glyph_row->used[TEXT_AREA] = 1;
12831 }
12832 }
12833 else
12834 {
12835 /* Save some values that must not be changed. */
12836 int saved_x = it->current_x;
12837 struct text_pos saved_pos;
12838 Lisp_Object saved_object;
12839 enum display_element_type saved_what = it->what;
12840 int saved_face_id = it->face_id;
12841
12842 saved_object = it->object;
12843 saved_pos = it->position;
12844
12845 it->what = IT_CHARACTER;
12846 bzero (&it->position, sizeof it->position);
12847 it->object = make_number (0);
12848 it->c = ' ';
12849 it->len = 1;
12850 it->face_id = face->id;
12851
12852 PRODUCE_GLYPHS (it);
12853
12854 while (it->current_x <= it->last_visible_x)
12855 PRODUCE_GLYPHS (it);
12856
12857 /* Don't count these blanks really. It would let us insert a left
12858 truncation glyph below and make us set the cursor on them, maybe. */
12859 it->current_x = saved_x;
12860 it->object = saved_object;
12861 it->position = saved_pos;
12862 it->what = saved_what;
12863 it->face_id = saved_face_id;
12864 }
12865 }
12866
12867
12868 /* Value is non-zero if text starting at CHARPOS in current_buffer is
12869 trailing whitespace. */
12870
12871 static int
12872 trailing_whitespace_p (charpos)
12873 int charpos;
12874 {
12875 int bytepos = CHAR_TO_BYTE (charpos);
12876 int c = 0;
12877
12878 while (bytepos < ZV_BYTE
12879 && (c = FETCH_CHAR (bytepos),
12880 c == ' ' || c == '\t'))
12881 ++bytepos;
12882
12883 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
12884 {
12885 if (bytepos != PT_BYTE)
12886 return 1;
12887 }
12888 return 0;
12889 }
12890
12891
12892 /* Highlight trailing whitespace, if any, in ROW. */
12893
12894 void
12895 highlight_trailing_whitespace (f, row)
12896 struct frame *f;
12897 struct glyph_row *row;
12898 {
12899 int used = row->used[TEXT_AREA];
12900
12901 if (used)
12902 {
12903 struct glyph *start = row->glyphs[TEXT_AREA];
12904 struct glyph *glyph = start + used - 1;
12905
12906 /* Skip over glyphs inserted to display the cursor at the
12907 end of a line, for extending the face of the last glyph
12908 to the end of the line on terminals, and for truncation
12909 and continuation glyphs. */
12910 while (glyph >= start
12911 && glyph->type == CHAR_GLYPH
12912 && INTEGERP (glyph->object))
12913 --glyph;
12914
12915 /* If last glyph is a space or stretch, and it's trailing
12916 whitespace, set the face of all trailing whitespace glyphs in
12917 IT->glyph_row to `trailing-whitespace'. */
12918 if (glyph >= start
12919 && BUFFERP (glyph->object)
12920 && (glyph->type == STRETCH_GLYPH
12921 || (glyph->type == CHAR_GLYPH
12922 && glyph->u.ch == ' '))
12923 && trailing_whitespace_p (glyph->charpos))
12924 {
12925 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
12926
12927 while (glyph >= start
12928 && BUFFERP (glyph->object)
12929 && (glyph->type == STRETCH_GLYPH
12930 || (glyph->type == CHAR_GLYPH
12931 && glyph->u.ch == ' ')))
12932 (glyph--)->face_id = face_id;
12933 }
12934 }
12935 }
12936
12937
12938 /* Value is non-zero if glyph row ROW in window W should be
12939 used to hold the cursor. */
12940
12941 static int
12942 cursor_row_p (w, row)
12943 struct window *w;
12944 struct glyph_row *row;
12945 {
12946 int cursor_row_p = 1;
12947
12948 if (PT == MATRIX_ROW_END_CHARPOS (row))
12949 {
12950 /* If the row ends with a newline from a string, we don't want
12951 the cursor there (if the row is continued it doesn't end in a
12952 newline). */
12953 if (CHARPOS (row->end.string_pos) >= 0
12954 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12955 cursor_row_p = row->continued_p;
12956
12957 /* If the row ends at ZV, display the cursor at the end of that
12958 row instead of at the start of the row below. */
12959 else if (row->ends_at_zv_p)
12960 cursor_row_p = 1;
12961 else
12962 cursor_row_p = 0;
12963 }
12964
12965 return cursor_row_p;
12966 }
12967
12968
12969 /* Construct the glyph row IT->glyph_row in the desired matrix of
12970 IT->w from text at the current position of IT. See dispextern.h
12971 for an overview of struct it. Value is non-zero if
12972 IT->glyph_row displays text, as opposed to a line displaying ZV
12973 only. */
12974
12975 static int
12976 display_line (it)
12977 struct it *it;
12978 {
12979 struct glyph_row *row = it->glyph_row;
12980
12981 /* We always start displaying at hpos zero even if hscrolled. */
12982 xassert (it->hpos == 0 && it->current_x == 0);
12983
12984 /* We must not display in a row that's not a text row. */
12985 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
12986 < it->w->desired_matrix->nrows);
12987
12988 /* Is IT->w showing the region? */
12989 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
12990
12991 /* Clear the result glyph row and enable it. */
12992 prepare_desired_row (row);
12993
12994 row->y = it->current_y;
12995 row->start = it->current;
12996 row->continuation_lines_width = it->continuation_lines_width;
12997 row->displays_text_p = 1;
12998 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
12999 it->starts_in_middle_of_char_p = 0;
13000
13001 /* Arrange the overlays nicely for our purposes. Usually, we call
13002 display_line on only one line at a time, in which case this
13003 can't really hurt too much, or we call it on lines which appear
13004 one after another in the buffer, in which case all calls to
13005 recenter_overlay_lists but the first will be pretty cheap. */
13006 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
13007
13008 /* Move over display elements that are not visible because we are
13009 hscrolled. This may stop at an x-position < IT->first_visible_x
13010 if the first glyph is partially visible or if we hit a line end. */
13011 if (it->current_x < it->first_visible_x)
13012 move_it_in_display_line_to (it, ZV, it->first_visible_x,
13013 MOVE_TO_POS | MOVE_TO_X);
13014
13015 /* Get the initial row height. This is either the height of the
13016 text hscrolled, if there is any, or zero. */
13017 row->ascent = it->max_ascent;
13018 row->height = it->max_ascent + it->max_descent;
13019 row->phys_ascent = it->max_phys_ascent;
13020 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
13021
13022 /* Loop generating characters. The loop is left with IT on the next
13023 character to display. */
13024 while (1)
13025 {
13026 int n_glyphs_before, hpos_before, x_before;
13027 int x, i, nglyphs;
13028 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
13029
13030 /* Retrieve the next thing to display. Value is zero if end of
13031 buffer reached. */
13032 if (!get_next_display_element (it))
13033 {
13034 /* Maybe add a space at the end of this line that is used to
13035 display the cursor there under X. Set the charpos of the
13036 first glyph of blank lines not corresponding to any text
13037 to -1. */
13038 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
13039 || row->used[TEXT_AREA] == 0)
13040 {
13041 row->glyphs[TEXT_AREA]->charpos = -1;
13042 row->displays_text_p = 0;
13043
13044 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
13045 && (!MINI_WINDOW_P (it->w)
13046 || (minibuf_level && EQ (it->window, minibuf_window))))
13047 row->indicate_empty_line_p = 1;
13048 }
13049
13050 it->continuation_lines_width = 0;
13051 row->ends_at_zv_p = 1;
13052 break;
13053 }
13054
13055 /* Now, get the metrics of what we want to display. This also
13056 generates glyphs in `row' (which is IT->glyph_row). */
13057 n_glyphs_before = row->used[TEXT_AREA];
13058 x = it->current_x;
13059
13060 /* Remember the line height so far in case the next element doesn't
13061 fit on the line. */
13062 if (!it->truncate_lines_p)
13063 {
13064 ascent = it->max_ascent;
13065 descent = it->max_descent;
13066 phys_ascent = it->max_phys_ascent;
13067 phys_descent = it->max_phys_descent;
13068 }
13069
13070 PRODUCE_GLYPHS (it);
13071
13072 /* If this display element was in marginal areas, continue with
13073 the next one. */
13074 if (it->area != TEXT_AREA)
13075 {
13076 row->ascent = max (row->ascent, it->max_ascent);
13077 row->height = max (row->height, it->max_ascent + it->max_descent);
13078 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13079 row->phys_height = max (row->phys_height,
13080 it->max_phys_ascent + it->max_phys_descent);
13081 set_iterator_to_next (it, 1);
13082 continue;
13083 }
13084
13085 /* Does the display element fit on the line? If we truncate
13086 lines, we should draw past the right edge of the window. If
13087 we don't truncate, we want to stop so that we can display the
13088 continuation glyph before the right margin. If lines are
13089 continued, there are two possible strategies for characters
13090 resulting in more than 1 glyph (e.g. tabs): Display as many
13091 glyphs as possible in this line and leave the rest for the
13092 continuation line, or display the whole element in the next
13093 line. Original redisplay did the former, so we do it also. */
13094 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
13095 hpos_before = it->hpos;
13096 x_before = x;
13097
13098 if (/* Not a newline. */
13099 nglyphs > 0
13100 /* Glyphs produced fit entirely in the line. */
13101 && it->current_x < it->last_visible_x)
13102 {
13103 it->hpos += nglyphs;
13104 row->ascent = max (row->ascent, it->max_ascent);
13105 row->height = max (row->height, it->max_ascent + it->max_descent);
13106 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13107 row->phys_height = max (row->phys_height,
13108 it->max_phys_ascent + it->max_phys_descent);
13109 if (it->current_x - it->pixel_width < it->first_visible_x)
13110 row->x = x - it->first_visible_x;
13111 }
13112 else
13113 {
13114 int new_x;
13115 struct glyph *glyph;
13116
13117 for (i = 0; i < nglyphs; ++i, x = new_x)
13118 {
13119 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
13120 new_x = x + glyph->pixel_width;
13121
13122 if (/* Lines are continued. */
13123 !it->truncate_lines_p
13124 && (/* Glyph doesn't fit on the line. */
13125 new_x > it->last_visible_x
13126 /* Or it fits exactly on a window system frame. */
13127 || (new_x == it->last_visible_x
13128 && FRAME_WINDOW_P (it->f))))
13129 {
13130 /* End of a continued line. */
13131
13132 if (it->hpos == 0
13133 || (new_x == it->last_visible_x
13134 && FRAME_WINDOW_P (it->f)))
13135 {
13136 /* Current glyph is the only one on the line or
13137 fits exactly on the line. We must continue
13138 the line because we can't draw the cursor
13139 after the glyph. */
13140 row->continued_p = 1;
13141 it->current_x = new_x;
13142 it->continuation_lines_width += new_x;
13143 ++it->hpos;
13144 if (i == nglyphs - 1)
13145 set_iterator_to_next (it, 1);
13146 }
13147 else if (CHAR_GLYPH_PADDING_P (*glyph)
13148 && !FRAME_WINDOW_P (it->f))
13149 {
13150 /* A padding glyph that doesn't fit on this line.
13151 This means the whole character doesn't fit
13152 on the line. */
13153 row->used[TEXT_AREA] = n_glyphs_before;
13154
13155 /* Fill the rest of the row with continuation
13156 glyphs like in 20.x. */
13157 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
13158 < row->glyphs[1 + TEXT_AREA])
13159 produce_special_glyphs (it, IT_CONTINUATION);
13160
13161 row->continued_p = 1;
13162 it->current_x = x_before;
13163 it->continuation_lines_width += x_before;
13164
13165 /* Restore the height to what it was before the
13166 element not fitting on the line. */
13167 it->max_ascent = ascent;
13168 it->max_descent = descent;
13169 it->max_phys_ascent = phys_ascent;
13170 it->max_phys_descent = phys_descent;
13171 }
13172 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
13173 {
13174 /* A TAB that extends past the right edge of the
13175 window. This produces a single glyph on
13176 window system frames. We leave the glyph in
13177 this row and let it fill the row, but don't
13178 consume the TAB. */
13179 it->continuation_lines_width += it->last_visible_x;
13180 row->ends_in_middle_of_char_p = 1;
13181 row->continued_p = 1;
13182 glyph->pixel_width = it->last_visible_x - x;
13183 it->starts_in_middle_of_char_p = 1;
13184 }
13185 else
13186 {
13187 /* Something other than a TAB that draws past
13188 the right edge of the window. Restore
13189 positions to values before the element. */
13190 row->used[TEXT_AREA] = n_glyphs_before + i;
13191
13192 /* Display continuation glyphs. */
13193 if (!FRAME_WINDOW_P (it->f))
13194 produce_special_glyphs (it, IT_CONTINUATION);
13195 row->continued_p = 1;
13196
13197 it->continuation_lines_width += x;
13198
13199 if (nglyphs > 1 && i > 0)
13200 {
13201 row->ends_in_middle_of_char_p = 1;
13202 it->starts_in_middle_of_char_p = 1;
13203 }
13204
13205 /* Restore the height to what it was before the
13206 element not fitting on the line. */
13207 it->max_ascent = ascent;
13208 it->max_descent = descent;
13209 it->max_phys_ascent = phys_ascent;
13210 it->max_phys_descent = phys_descent;
13211 }
13212
13213 break;
13214 }
13215 else if (new_x > it->first_visible_x)
13216 {
13217 /* Increment number of glyphs actually displayed. */
13218 ++it->hpos;
13219
13220 if (x < it->first_visible_x)
13221 /* Glyph is partially visible, i.e. row starts at
13222 negative X position. */
13223 row->x = x - it->first_visible_x;
13224 }
13225 else
13226 {
13227 /* Glyph is completely off the left margin of the
13228 window. This should not happen because of the
13229 move_it_in_display_line at the start of
13230 this function. */
13231 abort ();
13232 }
13233 }
13234
13235 row->ascent = max (row->ascent, it->max_ascent);
13236 row->height = max (row->height, it->max_ascent + it->max_descent);
13237 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13238 row->phys_height = max (row->phys_height,
13239 it->max_phys_ascent + it->max_phys_descent);
13240
13241 /* End of this display line if row is continued. */
13242 if (row->continued_p)
13243 break;
13244 }
13245
13246 /* Is this a line end? If yes, we're also done, after making
13247 sure that a non-default face is extended up to the right
13248 margin of the window. */
13249 if (ITERATOR_AT_END_OF_LINE_P (it))
13250 {
13251 int used_before = row->used[TEXT_AREA];
13252
13253 row->ends_in_newline_from_string_p = STRINGP (it->object);
13254
13255 /* Add a space at the end of the line that is used to
13256 display the cursor there. */
13257 append_space (it, 0);
13258
13259 /* Extend the face to the end of the line. */
13260 extend_face_to_end_of_line (it);
13261
13262 /* Make sure we have the position. */
13263 if (used_before == 0)
13264 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
13265
13266 /* Consume the line end. This skips over invisible lines. */
13267 set_iterator_to_next (it, 1);
13268 it->continuation_lines_width = 0;
13269 break;
13270 }
13271
13272 /* Proceed with next display element. Note that this skips
13273 over lines invisible because of selective display. */
13274 set_iterator_to_next (it, 1);
13275
13276 /* If we truncate lines, we are done when the last displayed
13277 glyphs reach past the right margin of the window. */
13278 if (it->truncate_lines_p
13279 && (FRAME_WINDOW_P (it->f)
13280 ? (it->current_x >= it->last_visible_x)
13281 : (it->current_x > it->last_visible_x)))
13282 {
13283 /* Maybe add truncation glyphs. */
13284 if (!FRAME_WINDOW_P (it->f))
13285 {
13286 int i, n;
13287
13288 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
13289 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
13290 break;
13291
13292 for (n = row->used[TEXT_AREA]; i < n; ++i)
13293 {
13294 row->used[TEXT_AREA] = i;
13295 produce_special_glyphs (it, IT_TRUNCATION);
13296 }
13297 }
13298
13299 row->truncated_on_right_p = 1;
13300 it->continuation_lines_width = 0;
13301 reseat_at_next_visible_line_start (it, 0);
13302 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
13303 it->hpos = hpos_before;
13304 it->current_x = x_before;
13305 break;
13306 }
13307 }
13308
13309 /* If line is not empty and hscrolled, maybe insert truncation glyphs
13310 at the left window margin. */
13311 if (it->first_visible_x
13312 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
13313 {
13314 if (!FRAME_WINDOW_P (it->f))
13315 insert_left_trunc_glyphs (it);
13316 row->truncated_on_left_p = 1;
13317 }
13318
13319 /* If the start of this line is the overlay arrow-position, then
13320 mark this glyph row as the one containing the overlay arrow.
13321 This is clearly a mess with variable size fonts. It would be
13322 better to let it be displayed like cursors under X. */
13323 if (MARKERP (Voverlay_arrow_position)
13324 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
13325 && (MATRIX_ROW_START_CHARPOS (row)
13326 == marker_position (Voverlay_arrow_position))
13327 && STRINGP (Voverlay_arrow_string)
13328 && ! overlay_arrow_seen)
13329 {
13330 /* Overlay arrow in window redisplay is a fringe bitmap. */
13331 if (!FRAME_WINDOW_P (it->f))
13332 {
13333 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
13334 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
13335 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
13336 struct glyph *p = row->glyphs[TEXT_AREA];
13337 struct glyph *p2, *end;
13338
13339 /* Copy the arrow glyphs. */
13340 while (glyph < arrow_end)
13341 *p++ = *glyph++;
13342
13343 /* Throw away padding glyphs. */
13344 p2 = p;
13345 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
13346 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
13347 ++p2;
13348 if (p2 > p)
13349 {
13350 while (p2 < end)
13351 *p++ = *p2++;
13352 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
13353 }
13354 }
13355
13356 overlay_arrow_seen = 1;
13357 row->overlay_arrow_p = 1;
13358 }
13359
13360 /* Compute pixel dimensions of this line. */
13361 compute_line_metrics (it);
13362
13363 /* Remember the position at which this line ends. */
13364 row->end = it->current;
13365
13366 /* Maybe set the cursor. */
13367 if (it->w->cursor.vpos < 0
13368 && PT >= MATRIX_ROW_START_CHARPOS (row)
13369 && PT <= MATRIX_ROW_END_CHARPOS (row)
13370 && cursor_row_p (it->w, row))
13371 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
13372
13373 /* Highlight trailing whitespace. */
13374 if (!NILP (Vshow_trailing_whitespace))
13375 highlight_trailing_whitespace (it->f, it->glyph_row);
13376
13377 /* Prepare for the next line. This line starts horizontally at (X
13378 HPOS) = (0 0). Vertical positions are incremented. As a
13379 convenience for the caller, IT->glyph_row is set to the next
13380 row to be used. */
13381 it->current_x = it->hpos = 0;
13382 it->current_y += row->height;
13383 ++it->vpos;
13384 ++it->glyph_row;
13385 return row->displays_text_p;
13386 }
13387
13388
13389 \f
13390 /***********************************************************************
13391 Menu Bar
13392 ***********************************************************************/
13393
13394 /* Redisplay the menu bar in the frame for window W.
13395
13396 The menu bar of X frames that don't have X toolkit support is
13397 displayed in a special window W->frame->menu_bar_window.
13398
13399 The menu bar of terminal frames is treated specially as far as
13400 glyph matrices are concerned. Menu bar lines are not part of
13401 windows, so the update is done directly on the frame matrix rows
13402 for the menu bar. */
13403
13404 static void
13405 display_menu_bar (w)
13406 struct window *w;
13407 {
13408 struct frame *f = XFRAME (WINDOW_FRAME (w));
13409 struct it it;
13410 Lisp_Object items;
13411 int i;
13412
13413 /* Don't do all this for graphical frames. */
13414 #ifdef HAVE_NTGUI
13415 if (!NILP (Vwindow_system))
13416 return;
13417 #endif
13418 #ifdef USE_X_TOOLKIT
13419 if (FRAME_X_P (f))
13420 return;
13421 #endif
13422 #ifdef MAC_OS
13423 if (FRAME_MAC_P (f))
13424 return;
13425 #endif
13426
13427 #ifdef USE_X_TOOLKIT
13428 xassert (!FRAME_WINDOW_P (f));
13429 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
13430 it.first_visible_x = 0;
13431 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
13432 #else /* not USE_X_TOOLKIT */
13433 if (FRAME_WINDOW_P (f))
13434 {
13435 /* Menu bar lines are displayed in the desired matrix of the
13436 dummy window menu_bar_window. */
13437 struct window *menu_w;
13438 xassert (WINDOWP (f->menu_bar_window));
13439 menu_w = XWINDOW (f->menu_bar_window);
13440 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
13441 MENU_FACE_ID);
13442 it.first_visible_x = 0;
13443 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
13444 }
13445 else
13446 {
13447 /* This is a TTY frame, i.e. character hpos/vpos are used as
13448 pixel x/y. */
13449 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
13450 MENU_FACE_ID);
13451 it.first_visible_x = 0;
13452 it.last_visible_x = FRAME_WIDTH (f);
13453 }
13454 #endif /* not USE_X_TOOLKIT */
13455
13456 if (! mode_line_inverse_video)
13457 /* Force the menu-bar to be displayed in the default face. */
13458 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
13459
13460 /* Clear all rows of the menu bar. */
13461 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
13462 {
13463 struct glyph_row *row = it.glyph_row + i;
13464 clear_glyph_row (row);
13465 row->enabled_p = 1;
13466 row->full_width_p = 1;
13467 }
13468
13469 /* Display all items of the menu bar. */
13470 items = FRAME_MENU_BAR_ITEMS (it.f);
13471 for (i = 0; i < XVECTOR (items)->size; i += 4)
13472 {
13473 Lisp_Object string;
13474
13475 /* Stop at nil string. */
13476 string = AREF (items, i + 1);
13477 if (NILP (string))
13478 break;
13479
13480 /* Remember where item was displayed. */
13481 AREF (items, i + 3) = make_number (it.hpos);
13482
13483 /* Display the item, pad with one space. */
13484 if (it.current_x < it.last_visible_x)
13485 display_string (NULL, string, Qnil, 0, 0, &it,
13486 SCHARS (string) + 1, 0, 0, -1);
13487 }
13488
13489 /* Fill out the line with spaces. */
13490 if (it.current_x < it.last_visible_x)
13491 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
13492
13493 /* Compute the total height of the lines. */
13494 compute_line_metrics (&it);
13495 }
13496
13497
13498 \f
13499 /***********************************************************************
13500 Mode Line
13501 ***********************************************************************/
13502
13503 /* Redisplay mode lines in the window tree whose root is WINDOW. If
13504 FORCE is non-zero, redisplay mode lines unconditionally.
13505 Otherwise, redisplay only mode lines that are garbaged. Value is
13506 the number of windows whose mode lines were redisplayed. */
13507
13508 static int
13509 redisplay_mode_lines (window, force)
13510 Lisp_Object window;
13511 int force;
13512 {
13513 int nwindows = 0;
13514
13515 while (!NILP (window))
13516 {
13517 struct window *w = XWINDOW (window);
13518
13519 if (WINDOWP (w->hchild))
13520 nwindows += redisplay_mode_lines (w->hchild, force);
13521 else if (WINDOWP (w->vchild))
13522 nwindows += redisplay_mode_lines (w->vchild, force);
13523 else if (force
13524 || FRAME_GARBAGED_P (XFRAME (w->frame))
13525 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
13526 {
13527 struct text_pos lpoint;
13528 struct buffer *old = current_buffer;
13529
13530 /* Set the window's buffer for the mode line display. */
13531 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13532 set_buffer_internal_1 (XBUFFER (w->buffer));
13533
13534 /* Point refers normally to the selected window. For any
13535 other window, set up appropriate value. */
13536 if (!EQ (window, selected_window))
13537 {
13538 struct text_pos pt;
13539
13540 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
13541 if (CHARPOS (pt) < BEGV)
13542 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
13543 else if (CHARPOS (pt) > (ZV - 1))
13544 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
13545 else
13546 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
13547 }
13548
13549 /* Display mode lines. */
13550 clear_glyph_matrix (w->desired_matrix);
13551 if (display_mode_lines (w))
13552 {
13553 ++nwindows;
13554 w->must_be_updated_p = 1;
13555 }
13556
13557 /* Restore old settings. */
13558 set_buffer_internal_1 (old);
13559 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13560 }
13561
13562 window = w->next;
13563 }
13564
13565 return nwindows;
13566 }
13567
13568
13569 /* Display the mode and/or top line of window W. Value is the number
13570 of mode lines displayed. */
13571
13572 static int
13573 display_mode_lines (w)
13574 struct window *w;
13575 {
13576 Lisp_Object old_selected_window, old_selected_frame;
13577 int n = 0;
13578
13579 old_selected_frame = selected_frame;
13580 selected_frame = w->frame;
13581 old_selected_window = selected_window;
13582 XSETWINDOW (selected_window, w);
13583
13584 /* These will be set while the mode line specs are processed. */
13585 line_number_displayed = 0;
13586 w->column_number_displayed = Qnil;
13587
13588 if (WINDOW_WANTS_MODELINE_P (w))
13589 {
13590 struct window *sel_w = XWINDOW (old_selected_window);
13591
13592 /* Select mode line face based on the real selected window. */
13593 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
13594 current_buffer->mode_line_format);
13595 ++n;
13596 }
13597
13598 if (WINDOW_WANTS_HEADER_LINE_P (w))
13599 {
13600 display_mode_line (w, HEADER_LINE_FACE_ID,
13601 current_buffer->header_line_format);
13602 ++n;
13603 }
13604
13605 selected_frame = old_selected_frame;
13606 selected_window = old_selected_window;
13607 return n;
13608 }
13609
13610
13611 /* Display mode or top line of window W. FACE_ID specifies which line
13612 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
13613 FORMAT is the mode line format to display. Value is the pixel
13614 height of the mode line displayed. */
13615
13616 static int
13617 display_mode_line (w, face_id, format)
13618 struct window *w;
13619 enum face_id face_id;
13620 Lisp_Object format;
13621 {
13622 struct it it;
13623 struct face *face;
13624
13625 init_iterator (&it, w, -1, -1, NULL, face_id);
13626 prepare_desired_row (it.glyph_row);
13627
13628 if (! mode_line_inverse_video)
13629 /* Force the mode-line to be displayed in the default face. */
13630 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
13631
13632 /* Temporarily make frame's keyboard the current kboard so that
13633 kboard-local variables in the mode_line_format will get the right
13634 values. */
13635 push_frame_kboard (it.f);
13636 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
13637 pop_frame_kboard ();
13638
13639 /* Fill up with spaces. */
13640 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
13641
13642 compute_line_metrics (&it);
13643 it.glyph_row->full_width_p = 1;
13644 it.glyph_row->mode_line_p = 1;
13645 it.glyph_row->continued_p = 0;
13646 it.glyph_row->truncated_on_left_p = 0;
13647 it.glyph_row->truncated_on_right_p = 0;
13648
13649 /* Make a 3D mode-line have a shadow at its right end. */
13650 face = FACE_FROM_ID (it.f, face_id);
13651 extend_face_to_end_of_line (&it);
13652 if (face->box != FACE_NO_BOX)
13653 {
13654 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
13655 + it.glyph_row->used[TEXT_AREA] - 1);
13656 last->right_box_line_p = 1;
13657 }
13658
13659 return it.glyph_row->height;
13660 }
13661
13662 /* Alist that caches the results of :propertize.
13663 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
13664 Lisp_Object mode_line_proptrans_alist;
13665
13666 /* List of strings making up the mode-line. */
13667 Lisp_Object mode_line_string_list;
13668
13669 /* Base face property when building propertized mode line string. */
13670 static Lisp_Object mode_line_string_face;
13671 static Lisp_Object mode_line_string_face_prop;
13672
13673
13674 /* Contribute ELT to the mode line for window IT->w. How it
13675 translates into text depends on its data type.
13676
13677 IT describes the display environment in which we display, as usual.
13678
13679 DEPTH is the depth in recursion. It is used to prevent
13680 infinite recursion here.
13681
13682 FIELD_WIDTH is the number of characters the display of ELT should
13683 occupy in the mode line, and PRECISION is the maximum number of
13684 characters to display from ELT's representation. See
13685 display_string for details.
13686
13687 Returns the hpos of the end of the text generated by ELT.
13688
13689 PROPS is a property list to add to any string we encounter.
13690
13691 If RISKY is nonzero, remove (disregard) any properties in any string
13692 we encounter, and ignore :eval and :propertize.
13693
13694 If the global variable `frame_title_ptr' is non-NULL, then the output
13695 is passed to `store_frame_title' instead of `display_string'. */
13696
13697 static int
13698 display_mode_element (it, depth, field_width, precision, elt, props, risky)
13699 struct it *it;
13700 int depth;
13701 int field_width, precision;
13702 Lisp_Object elt, props;
13703 int risky;
13704 {
13705 int n = 0, field, prec;
13706 int literal = 0;
13707
13708 tail_recurse:
13709 if (depth > 10)
13710 goto invalid;
13711
13712 depth++;
13713
13714 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
13715 {
13716 case Lisp_String:
13717 {
13718 /* A string: output it and check for %-constructs within it. */
13719 unsigned char c;
13720 unsigned char *this, *lisp_string;
13721
13722 if (!NILP (props) || risky)
13723 {
13724 Lisp_Object oprops, aelt;
13725 oprops = Ftext_properties_at (make_number (0), elt);
13726
13727 if (NILP (Fequal (props, oprops)) || risky)
13728 {
13729 /* If the starting string has properties,
13730 merge the specified ones onto the existing ones. */
13731 if (! NILP (oprops) && !risky)
13732 {
13733 Lisp_Object tem;
13734
13735 oprops = Fcopy_sequence (oprops);
13736 tem = props;
13737 while (CONSP (tem))
13738 {
13739 oprops = Fplist_put (oprops, XCAR (tem),
13740 XCAR (XCDR (tem)));
13741 tem = XCDR (XCDR (tem));
13742 }
13743 props = oprops;
13744 }
13745
13746 aelt = Fassoc (elt, mode_line_proptrans_alist);
13747 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
13748 {
13749 mode_line_proptrans_alist
13750 = Fcons (aelt, Fdelq (aelt, mode_line_proptrans_alist));
13751 elt = XCAR (aelt);
13752 }
13753 else
13754 {
13755 Lisp_Object tem;
13756
13757 elt = Fcopy_sequence (elt);
13758 Fset_text_properties (make_number (0), Flength (elt),
13759 props, elt);
13760 /* Add this item to mode_line_proptrans_alist. */
13761 mode_line_proptrans_alist
13762 = Fcons (Fcons (elt, props),
13763 mode_line_proptrans_alist);
13764 /* Truncate mode_line_proptrans_alist
13765 to at most 50 elements. */
13766 tem = Fnthcdr (make_number (50),
13767 mode_line_proptrans_alist);
13768 if (! NILP (tem))
13769 XSETCDR (tem, Qnil);
13770 }
13771 }
13772 }
13773
13774 this = SDATA (elt);
13775 lisp_string = this;
13776
13777 if (literal)
13778 {
13779 prec = precision - n;
13780 if (frame_title_ptr)
13781 n += store_frame_title (SDATA (elt), -1, prec);
13782 else if (!NILP (mode_line_string_list))
13783 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
13784 else
13785 n += display_string (NULL, elt, Qnil, 0, 0, it,
13786 0, prec, 0, STRING_MULTIBYTE (elt));
13787
13788 break;
13789 }
13790
13791 while ((precision <= 0 || n < precision)
13792 && *this
13793 && (frame_title_ptr
13794 || !NILP (mode_line_string_list)
13795 || it->current_x < it->last_visible_x))
13796 {
13797 unsigned char *last = this;
13798
13799 /* Advance to end of string or next format specifier. */
13800 while ((c = *this++) != '\0' && c != '%')
13801 ;
13802
13803 if (this - 1 != last)
13804 {
13805 /* Output to end of string or up to '%'. Field width
13806 is length of string. Don't output more than
13807 PRECISION allows us. */
13808 --this;
13809
13810 prec = chars_in_text (last, this - last);
13811 if (precision > 0 && prec > precision - n)
13812 prec = precision - n;
13813
13814 if (frame_title_ptr)
13815 n += store_frame_title (last, 0, prec);
13816 else if (!NILP (mode_line_string_list))
13817 {
13818 int bytepos = last - lisp_string;
13819 int charpos = string_byte_to_char (elt, bytepos);
13820 n += store_mode_line_string (NULL,
13821 Fsubstring (elt, make_number (charpos),
13822 make_number (charpos + prec)),
13823 0, 0, 0, Qnil);
13824 }
13825 else
13826 {
13827 int bytepos = last - lisp_string;
13828 int charpos = string_byte_to_char (elt, bytepos);
13829 n += display_string (NULL, elt, Qnil, 0, charpos,
13830 it, 0, prec, 0,
13831 STRING_MULTIBYTE (elt));
13832 }
13833 }
13834 else /* c == '%' */
13835 {
13836 unsigned char *percent_position = this;
13837
13838 /* Get the specified minimum width. Zero means
13839 don't pad. */
13840 field = 0;
13841 while ((c = *this++) >= '0' && c <= '9')
13842 field = field * 10 + c - '0';
13843
13844 /* Don't pad beyond the total padding allowed. */
13845 if (field_width - n > 0 && field > field_width - n)
13846 field = field_width - n;
13847
13848 /* Note that either PRECISION <= 0 or N < PRECISION. */
13849 prec = precision - n;
13850
13851 if (c == 'M')
13852 n += display_mode_element (it, depth, field, prec,
13853 Vglobal_mode_string, props,
13854 risky);
13855 else if (c != 0)
13856 {
13857 int multibyte;
13858 int bytepos, charpos;
13859 unsigned char *spec;
13860
13861 bytepos = percent_position - lisp_string;
13862 charpos = (STRING_MULTIBYTE (elt)
13863 ? string_byte_to_char (elt, bytepos)
13864 : bytepos);
13865
13866 spec
13867 = decode_mode_spec (it->w, c, field, prec, &multibyte);
13868
13869 if (frame_title_ptr)
13870 n += store_frame_title (spec, field, prec);
13871 else if (!NILP (mode_line_string_list))
13872 {
13873 int len = strlen (spec);
13874 Lisp_Object tem = make_string (spec, len);
13875 props = Ftext_properties_at (make_number (charpos), elt);
13876 /* Should only keep face property in props */
13877 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
13878 }
13879 else
13880 {
13881 int nglyphs_before, nwritten;
13882
13883 nglyphs_before = it->glyph_row->used[TEXT_AREA];
13884 nwritten = display_string (spec, Qnil, elt,
13885 charpos, 0, it,
13886 field, prec, 0,
13887 multibyte);
13888
13889 /* Assign to the glyphs written above the
13890 string where the `%x' came from, position
13891 of the `%'. */
13892 if (nwritten > 0)
13893 {
13894 struct glyph *glyph
13895 = (it->glyph_row->glyphs[TEXT_AREA]
13896 + nglyphs_before);
13897 int i;
13898
13899 for (i = 0; i < nwritten; ++i)
13900 {
13901 glyph[i].object = elt;
13902 glyph[i].charpos = charpos;
13903 }
13904
13905 n += nwritten;
13906 }
13907 }
13908 }
13909 else /* c == 0 */
13910 break;
13911 }
13912 }
13913 }
13914 break;
13915
13916 case Lisp_Symbol:
13917 /* A symbol: process the value of the symbol recursively
13918 as if it appeared here directly. Avoid error if symbol void.
13919 Special case: if value of symbol is a string, output the string
13920 literally. */
13921 {
13922 register Lisp_Object tem;
13923
13924 /* If the variable is not marked as risky to set
13925 then its contents are risky to use. */
13926 if (NILP (Fget (elt, Qrisky_local_variable)))
13927 risky = 1;
13928
13929 tem = Fboundp (elt);
13930 if (!NILP (tem))
13931 {
13932 tem = Fsymbol_value (elt);
13933 /* If value is a string, output that string literally:
13934 don't check for % within it. */
13935 if (STRINGP (tem))
13936 literal = 1;
13937
13938 if (!EQ (tem, elt))
13939 {
13940 /* Give up right away for nil or t. */
13941 elt = tem;
13942 goto tail_recurse;
13943 }
13944 }
13945 }
13946 break;
13947
13948 case Lisp_Cons:
13949 {
13950 register Lisp_Object car, tem;
13951
13952 /* A cons cell: five distinct cases.
13953 If first element is :eval or :propertize, do something special.
13954 If first element is a string or a cons, process all the elements
13955 and effectively concatenate them.
13956 If first element is a negative number, truncate displaying cdr to
13957 at most that many characters. If positive, pad (with spaces)
13958 to at least that many characters.
13959 If first element is a symbol, process the cadr or caddr recursively
13960 according to whether the symbol's value is non-nil or nil. */
13961 car = XCAR (elt);
13962 if (EQ (car, QCeval))
13963 {
13964 /* An element of the form (:eval FORM) means evaluate FORM
13965 and use the result as mode line elements. */
13966
13967 if (risky)
13968 break;
13969
13970 if (CONSP (XCDR (elt)))
13971 {
13972 Lisp_Object spec;
13973 spec = safe_eval (XCAR (XCDR (elt)));
13974 n += display_mode_element (it, depth, field_width - n,
13975 precision - n, spec, props,
13976 risky);
13977 }
13978 }
13979 else if (EQ (car, QCpropertize))
13980 {
13981 /* An element of the form (:propertize ELT PROPS...)
13982 means display ELT but applying properties PROPS. */
13983
13984 if (risky)
13985 break;
13986
13987 if (CONSP (XCDR (elt)))
13988 n += display_mode_element (it, depth, field_width - n,
13989 precision - n, XCAR (XCDR (elt)),
13990 XCDR (XCDR (elt)), risky);
13991 }
13992 else if (SYMBOLP (car))
13993 {
13994 tem = Fboundp (car);
13995 elt = XCDR (elt);
13996 if (!CONSP (elt))
13997 goto invalid;
13998 /* elt is now the cdr, and we know it is a cons cell.
13999 Use its car if CAR has a non-nil value. */
14000 if (!NILP (tem))
14001 {
14002 tem = Fsymbol_value (car);
14003 if (!NILP (tem))
14004 {
14005 elt = XCAR (elt);
14006 goto tail_recurse;
14007 }
14008 }
14009 /* Symbol's value is nil (or symbol is unbound)
14010 Get the cddr of the original list
14011 and if possible find the caddr and use that. */
14012 elt = XCDR (elt);
14013 if (NILP (elt))
14014 break;
14015 else if (!CONSP (elt))
14016 goto invalid;
14017 elt = XCAR (elt);
14018 goto tail_recurse;
14019 }
14020 else if (INTEGERP (car))
14021 {
14022 register int lim = XINT (car);
14023 elt = XCDR (elt);
14024 if (lim < 0)
14025 {
14026 /* Negative int means reduce maximum width. */
14027 if (precision <= 0)
14028 precision = -lim;
14029 else
14030 precision = min (precision, -lim);
14031 }
14032 else if (lim > 0)
14033 {
14034 /* Padding specified. Don't let it be more than
14035 current maximum. */
14036 if (precision > 0)
14037 lim = min (precision, lim);
14038
14039 /* If that's more padding than already wanted, queue it.
14040 But don't reduce padding already specified even if
14041 that is beyond the current truncation point. */
14042 field_width = max (lim, field_width);
14043 }
14044 goto tail_recurse;
14045 }
14046 else if (STRINGP (car) || CONSP (car))
14047 {
14048 register int limit = 50;
14049 /* Limit is to protect against circular lists. */
14050 while (CONSP (elt)
14051 && --limit > 0
14052 && (precision <= 0 || n < precision))
14053 {
14054 n += display_mode_element (it, depth, field_width - n,
14055 precision - n, XCAR (elt),
14056 props, risky);
14057 elt = XCDR (elt);
14058 }
14059 }
14060 }
14061 break;
14062
14063 default:
14064 invalid:
14065 if (frame_title_ptr)
14066 n += store_frame_title ("*invalid*", 0, precision - n);
14067 else if (!NILP (mode_line_string_list))
14068 n += store_mode_line_string ("*invalid*", Qnil, 0, 0, precision - n, Qnil);
14069 else
14070 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
14071 precision - n, 0, 0);
14072 return n;
14073 }
14074
14075 /* Pad to FIELD_WIDTH. */
14076 if (field_width > 0 && n < field_width)
14077 {
14078 if (frame_title_ptr)
14079 n += store_frame_title ("", field_width - n, 0);
14080 else if (!NILP (mode_line_string_list))
14081 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
14082 else
14083 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
14084 0, 0, 0);
14085 }
14086
14087 return n;
14088 }
14089
14090 /* Store a mode-line string element in mode_line_string_list.
14091
14092 If STRING is non-null, display that C string. Otherwise, the Lisp
14093 string LISP_STRING is displayed.
14094
14095 FIELD_WIDTH is the minimum number of output glyphs to produce.
14096 If STRING has fewer characters than FIELD_WIDTH, pad to the right
14097 with spaces. FIELD_WIDTH <= 0 means don't pad.
14098
14099 PRECISION is the maximum number of characters to output from
14100 STRING. PRECISION <= 0 means don't truncate the string.
14101
14102 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
14103 properties to the string.
14104
14105 PROPS are the properties to add to the string.
14106 The mode_line_string_face face property is always added to the string.
14107 */
14108
14109 static int store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
14110 char *string;
14111 Lisp_Object lisp_string;
14112 int copy_string;
14113 int field_width;
14114 int precision;
14115 Lisp_Object props;
14116 {
14117 int len;
14118 int n = 0;
14119
14120 if (string != NULL)
14121 {
14122 len = strlen (string);
14123 if (precision > 0 && len > precision)
14124 len = precision;
14125 lisp_string = make_string (string, len);
14126 if (NILP (props))
14127 props = mode_line_string_face_prop;
14128 else if (!NILP (mode_line_string_face))
14129 {
14130 Lisp_Object face = Fplist_get (props, Qface);
14131 props = Fcopy_sequence (props);
14132 if (NILP (face))
14133 face = mode_line_string_face;
14134 else
14135 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
14136 props = Fplist_put (props, Qface, face);
14137 }
14138 Fadd_text_properties (make_number (0), make_number (len),
14139 props, lisp_string);
14140 }
14141 else
14142 {
14143 len = XFASTINT (Flength (lisp_string));
14144 if (precision > 0 && len > precision)
14145 {
14146 len = precision;
14147 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
14148 precision = -1;
14149 }
14150 if (!NILP (mode_line_string_face))
14151 {
14152 Lisp_Object face;
14153 if (NILP (props))
14154 props = Ftext_properties_at (make_number (0), lisp_string);
14155 face = Fplist_get (props, Qface);
14156 if (NILP (face))
14157 face = mode_line_string_face;
14158 else
14159 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
14160 props = Fcons (Qface, Fcons (face, Qnil));
14161 if (copy_string)
14162 lisp_string = Fcopy_sequence (lisp_string);
14163 }
14164 if (!NILP (props))
14165 Fadd_text_properties (make_number (0), make_number (len),
14166 props, lisp_string);
14167 }
14168
14169 if (len > 0)
14170 {
14171 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
14172 n += len;
14173 }
14174
14175 if (field_width > len)
14176 {
14177 field_width -= len;
14178 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
14179 if (!NILP (props))
14180 Fadd_text_properties (make_number (0), make_number (field_width),
14181 props, lisp_string);
14182 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
14183 n += field_width;
14184 }
14185
14186 return n;
14187 }
14188
14189
14190 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
14191 0, 3, 0,
14192 doc: /* Return the mode-line of selected window as a string.
14193 First optional arg FORMAT specifies a different format string (see
14194 `mode-line-format' for details) to use. If FORMAT is t, return
14195 the buffer's header-line. Second optional arg WINDOW specifies a
14196 different window to use as the context for the formatting.
14197 If third optional arg NO-PROPS is non-nil, string is not propertized. */)
14198 (format, window, no_props)
14199 Lisp_Object format, window, no_props;
14200 {
14201 struct it it;
14202 int len;
14203 struct window *w;
14204 struct buffer *old_buffer = NULL;
14205 enum face_id face_id = DEFAULT_FACE_ID;
14206
14207 if (NILP (window))
14208 window = selected_window;
14209 CHECK_WINDOW (window);
14210 w = XWINDOW (window);
14211 CHECK_BUFFER (w->buffer);
14212
14213 if (XBUFFER (w->buffer) != current_buffer)
14214 {
14215 old_buffer = current_buffer;
14216 set_buffer_internal_1 (XBUFFER (w->buffer));
14217 }
14218
14219 if (NILP (format) || EQ (format, Qt))
14220 {
14221 face_id = NILP (format)
14222 ? CURRENT_MODE_LINE_FACE_ID (w) :
14223 HEADER_LINE_FACE_ID;
14224 format = NILP (format)
14225 ? current_buffer->mode_line_format
14226 : current_buffer->header_line_format;
14227 }
14228
14229 init_iterator (&it, w, -1, -1, NULL, face_id);
14230
14231 if (NILP (no_props))
14232 {
14233 mode_line_string_face =
14234 (face_id == MODE_LINE_FACE_ID ? Qmode_line :
14235 face_id == MODE_LINE_INACTIVE_FACE_ID ? Qmode_line_inactive :
14236 face_id == HEADER_LINE_FACE_ID ? Qheader_line : Qnil);
14237
14238 mode_line_string_face_prop =
14239 NILP (mode_line_string_face) ? Qnil :
14240 Fcons (Qface, Fcons (mode_line_string_face, Qnil));
14241
14242 /* We need a dummy last element in mode_line_string_list to
14243 indicate we are building the propertized mode-line string.
14244 Using mode_line_string_face_prop here GC protects it. */
14245 mode_line_string_list =
14246 Fcons (mode_line_string_face_prop, Qnil);
14247 frame_title_ptr = NULL;
14248 }
14249 else
14250 {
14251 mode_line_string_face_prop = Qnil;
14252 mode_line_string_list = Qnil;
14253 frame_title_ptr = frame_title_buf;
14254 }
14255
14256 push_frame_kboard (it.f);
14257 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
14258 pop_frame_kboard ();
14259
14260 if (old_buffer)
14261 set_buffer_internal_1 (old_buffer);
14262
14263 if (NILP (no_props))
14264 {
14265 Lisp_Object str;
14266 mode_line_string_list = Fnreverse (mode_line_string_list);
14267 str = Fmapconcat (intern ("identity"), XCDR (mode_line_string_list),
14268 make_string ("", 0));
14269 mode_line_string_face_prop = Qnil;
14270 mode_line_string_list = Qnil;
14271 return str;
14272 }
14273
14274 len = frame_title_ptr - frame_title_buf;
14275 if (len > 0 && frame_title_ptr[-1] == '-')
14276 {
14277 /* Mode lines typically ends with numerous dashes; reduce to two dashes. */
14278 while (frame_title_ptr > frame_title_buf && *--frame_title_ptr == '-')
14279 ;
14280 frame_title_ptr += 3; /* restore last non-dash + two dashes */
14281 if (len > frame_title_ptr - frame_title_buf)
14282 len = frame_title_ptr - frame_title_buf;
14283 }
14284
14285 frame_title_ptr = NULL;
14286 return make_string (frame_title_buf, len);
14287 }
14288
14289 /* Write a null-terminated, right justified decimal representation of
14290 the positive integer D to BUF using a minimal field width WIDTH. */
14291
14292 static void
14293 pint2str (buf, width, d)
14294 register char *buf;
14295 register int width;
14296 register int d;
14297 {
14298 register char *p = buf;
14299
14300 if (d <= 0)
14301 *p++ = '0';
14302 else
14303 {
14304 while (d > 0)
14305 {
14306 *p++ = d % 10 + '0';
14307 d /= 10;
14308 }
14309 }
14310
14311 for (width -= (int) (p - buf); width > 0; --width)
14312 *p++ = ' ';
14313 *p-- = '\0';
14314 while (p > buf)
14315 {
14316 d = *buf;
14317 *buf++ = *p;
14318 *p-- = d;
14319 }
14320 }
14321
14322 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
14323 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
14324 type of CODING_SYSTEM. Return updated pointer into BUF. */
14325
14326 static unsigned char invalid_eol_type[] = "(*invalid*)";
14327
14328 static char *
14329 decode_mode_spec_coding (coding_system, buf, eol_flag)
14330 Lisp_Object coding_system;
14331 register char *buf;
14332 int eol_flag;
14333 {
14334 Lisp_Object val;
14335 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
14336 unsigned char *eol_str;
14337 int eol_str_len;
14338 /* The EOL conversion we are using. */
14339 Lisp_Object eoltype;
14340
14341 val = Fget (coding_system, Qcoding_system);
14342 eoltype = Qnil;
14343
14344 if (!VECTORP (val)) /* Not yet decided. */
14345 {
14346 if (multibyte)
14347 *buf++ = '-';
14348 if (eol_flag)
14349 eoltype = eol_mnemonic_undecided;
14350 /* Don't mention EOL conversion if it isn't decided. */
14351 }
14352 else
14353 {
14354 Lisp_Object eolvalue;
14355
14356 eolvalue = Fget (coding_system, Qeol_type);
14357
14358 if (multibyte)
14359 *buf++ = XFASTINT (AREF (val, 1));
14360
14361 if (eol_flag)
14362 {
14363 /* The EOL conversion that is normal on this system. */
14364
14365 if (NILP (eolvalue)) /* Not yet decided. */
14366 eoltype = eol_mnemonic_undecided;
14367 else if (VECTORP (eolvalue)) /* Not yet decided. */
14368 eoltype = eol_mnemonic_undecided;
14369 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
14370 eoltype = (XFASTINT (eolvalue) == 0
14371 ? eol_mnemonic_unix
14372 : (XFASTINT (eolvalue) == 1
14373 ? eol_mnemonic_dos : eol_mnemonic_mac));
14374 }
14375 }
14376
14377 if (eol_flag)
14378 {
14379 /* Mention the EOL conversion if it is not the usual one. */
14380 if (STRINGP (eoltype))
14381 {
14382 eol_str = SDATA (eoltype);
14383 eol_str_len = SBYTES (eoltype);
14384 }
14385 else if (INTEGERP (eoltype)
14386 && CHAR_VALID_P (XINT (eoltype), 0))
14387 {
14388 eol_str = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
14389 eol_str_len = CHAR_STRING (XINT (eoltype), eol_str);
14390 }
14391 else
14392 {
14393 eol_str = invalid_eol_type;
14394 eol_str_len = sizeof (invalid_eol_type) - 1;
14395 }
14396 bcopy (eol_str, buf, eol_str_len);
14397 buf += eol_str_len;
14398 }
14399
14400 return buf;
14401 }
14402
14403 /* Return a string for the output of a mode line %-spec for window W,
14404 generated by character C. PRECISION >= 0 means don't return a
14405 string longer than that value. FIELD_WIDTH > 0 means pad the
14406 string returned with spaces to that value. Return 1 in *MULTIBYTE
14407 if the result is multibyte text. */
14408
14409 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
14410
14411 static char *
14412 decode_mode_spec (w, c, field_width, precision, multibyte)
14413 struct window *w;
14414 register int c;
14415 int field_width, precision;
14416 int *multibyte;
14417 {
14418 Lisp_Object obj;
14419 struct frame *f = XFRAME (WINDOW_FRAME (w));
14420 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
14421 struct buffer *b = XBUFFER (w->buffer);
14422
14423 obj = Qnil;
14424 *multibyte = 0;
14425
14426 switch (c)
14427 {
14428 case '*':
14429 if (!NILP (b->read_only))
14430 return "%";
14431 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
14432 return "*";
14433 return "-";
14434
14435 case '+':
14436 /* This differs from %* only for a modified read-only buffer. */
14437 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
14438 return "*";
14439 if (!NILP (b->read_only))
14440 return "%";
14441 return "-";
14442
14443 case '&':
14444 /* This differs from %* in ignoring read-only-ness. */
14445 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
14446 return "*";
14447 return "-";
14448
14449 case '%':
14450 return "%";
14451
14452 case '[':
14453 {
14454 int i;
14455 char *p;
14456
14457 if (command_loop_level > 5)
14458 return "[[[... ";
14459 p = decode_mode_spec_buf;
14460 for (i = 0; i < command_loop_level; i++)
14461 *p++ = '[';
14462 *p = 0;
14463 return decode_mode_spec_buf;
14464 }
14465
14466 case ']':
14467 {
14468 int i;
14469 char *p;
14470
14471 if (command_loop_level > 5)
14472 return " ...]]]";
14473 p = decode_mode_spec_buf;
14474 for (i = 0; i < command_loop_level; i++)
14475 *p++ = ']';
14476 *p = 0;
14477 return decode_mode_spec_buf;
14478 }
14479
14480 case '-':
14481 {
14482 register int i;
14483
14484 /* Let lots_of_dashes be a string of infinite length. */
14485 if (!NILP (mode_line_string_list))
14486 return "--";
14487 if (field_width <= 0
14488 || field_width > sizeof (lots_of_dashes))
14489 {
14490 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
14491 decode_mode_spec_buf[i] = '-';
14492 decode_mode_spec_buf[i] = '\0';
14493 return decode_mode_spec_buf;
14494 }
14495 else
14496 return lots_of_dashes;
14497 }
14498
14499 case 'b':
14500 obj = b->name;
14501 break;
14502
14503 case 'c':
14504 {
14505 int col = (int) current_column (); /* iftc */
14506 w->column_number_displayed = make_number (col);
14507 pint2str (decode_mode_spec_buf, field_width, col);
14508 return decode_mode_spec_buf;
14509 }
14510
14511 case 'F':
14512 /* %F displays the frame name. */
14513 if (!NILP (f->title))
14514 return (char *) SDATA (f->title);
14515 if (f->explicit_name || ! FRAME_WINDOW_P (f))
14516 return (char *) SDATA (f->name);
14517 return "Emacs";
14518
14519 case 'f':
14520 obj = b->filename;
14521 break;
14522
14523 case 'l':
14524 {
14525 int startpos = XMARKER (w->start)->charpos;
14526 int startpos_byte = marker_byte_position (w->start);
14527 int line, linepos, linepos_byte, topline;
14528 int nlines, junk;
14529 int height = XFASTINT (w->height);
14530
14531 /* If we decided that this buffer isn't suitable for line numbers,
14532 don't forget that too fast. */
14533 if (EQ (w->base_line_pos, w->buffer))
14534 goto no_value;
14535 /* But do forget it, if the window shows a different buffer now. */
14536 else if (BUFFERP (w->base_line_pos))
14537 w->base_line_pos = Qnil;
14538
14539 /* If the buffer is very big, don't waste time. */
14540 if (INTEGERP (Vline_number_display_limit)
14541 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
14542 {
14543 w->base_line_pos = Qnil;
14544 w->base_line_number = Qnil;
14545 goto no_value;
14546 }
14547
14548 if (!NILP (w->base_line_number)
14549 && !NILP (w->base_line_pos)
14550 && XFASTINT (w->base_line_pos) <= startpos)
14551 {
14552 line = XFASTINT (w->base_line_number);
14553 linepos = XFASTINT (w->base_line_pos);
14554 linepos_byte = buf_charpos_to_bytepos (b, linepos);
14555 }
14556 else
14557 {
14558 line = 1;
14559 linepos = BUF_BEGV (b);
14560 linepos_byte = BUF_BEGV_BYTE (b);
14561 }
14562
14563 /* Count lines from base line to window start position. */
14564 nlines = display_count_lines (linepos, linepos_byte,
14565 startpos_byte,
14566 startpos, &junk);
14567
14568 topline = nlines + line;
14569
14570 /* Determine a new base line, if the old one is too close
14571 or too far away, or if we did not have one.
14572 "Too close" means it's plausible a scroll-down would
14573 go back past it. */
14574 if (startpos == BUF_BEGV (b))
14575 {
14576 w->base_line_number = make_number (topline);
14577 w->base_line_pos = make_number (BUF_BEGV (b));
14578 }
14579 else if (nlines < height + 25 || nlines > height * 3 + 50
14580 || linepos == BUF_BEGV (b))
14581 {
14582 int limit = BUF_BEGV (b);
14583 int limit_byte = BUF_BEGV_BYTE (b);
14584 int position;
14585 int distance = (height * 2 + 30) * line_number_display_limit_width;
14586
14587 if (startpos - distance > limit)
14588 {
14589 limit = startpos - distance;
14590 limit_byte = CHAR_TO_BYTE (limit);
14591 }
14592
14593 nlines = display_count_lines (startpos, startpos_byte,
14594 limit_byte,
14595 - (height * 2 + 30),
14596 &position);
14597 /* If we couldn't find the lines we wanted within
14598 line_number_display_limit_width chars per line,
14599 give up on line numbers for this window. */
14600 if (position == limit_byte && limit == startpos - distance)
14601 {
14602 w->base_line_pos = w->buffer;
14603 w->base_line_number = Qnil;
14604 goto no_value;
14605 }
14606
14607 w->base_line_number = make_number (topline - nlines);
14608 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
14609 }
14610
14611 /* Now count lines from the start pos to point. */
14612 nlines = display_count_lines (startpos, startpos_byte,
14613 PT_BYTE, PT, &junk);
14614
14615 /* Record that we did display the line number. */
14616 line_number_displayed = 1;
14617
14618 /* Make the string to show. */
14619 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
14620 return decode_mode_spec_buf;
14621 no_value:
14622 {
14623 char* p = decode_mode_spec_buf;
14624 int pad = field_width - 2;
14625 while (pad-- > 0)
14626 *p++ = ' ';
14627 *p++ = '?';
14628 *p++ = '?';
14629 *p = '\0';
14630 return decode_mode_spec_buf;
14631 }
14632 }
14633 break;
14634
14635 case 'm':
14636 obj = b->mode_name;
14637 break;
14638
14639 case 'n':
14640 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
14641 return " Narrow";
14642 break;
14643
14644 case 'p':
14645 {
14646 int pos = marker_position (w->start);
14647 int total = BUF_ZV (b) - BUF_BEGV (b);
14648
14649 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
14650 {
14651 if (pos <= BUF_BEGV (b))
14652 return "All";
14653 else
14654 return "Bottom";
14655 }
14656 else if (pos <= BUF_BEGV (b))
14657 return "Top";
14658 else
14659 {
14660 if (total > 1000000)
14661 /* Do it differently for a large value, to avoid overflow. */
14662 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
14663 else
14664 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
14665 /* We can't normally display a 3-digit number,
14666 so get us a 2-digit number that is close. */
14667 if (total == 100)
14668 total = 99;
14669 sprintf (decode_mode_spec_buf, "%2d%%", total);
14670 return decode_mode_spec_buf;
14671 }
14672 }
14673
14674 /* Display percentage of size above the bottom of the screen. */
14675 case 'P':
14676 {
14677 int toppos = marker_position (w->start);
14678 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
14679 int total = BUF_ZV (b) - BUF_BEGV (b);
14680
14681 if (botpos >= BUF_ZV (b))
14682 {
14683 if (toppos <= BUF_BEGV (b))
14684 return "All";
14685 else
14686 return "Bottom";
14687 }
14688 else
14689 {
14690 if (total > 1000000)
14691 /* Do it differently for a large value, to avoid overflow. */
14692 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
14693 else
14694 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
14695 /* We can't normally display a 3-digit number,
14696 so get us a 2-digit number that is close. */
14697 if (total == 100)
14698 total = 99;
14699 if (toppos <= BUF_BEGV (b))
14700 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
14701 else
14702 sprintf (decode_mode_spec_buf, "%2d%%", total);
14703 return decode_mode_spec_buf;
14704 }
14705 }
14706
14707 case 's':
14708 /* status of process */
14709 obj = Fget_buffer_process (w->buffer);
14710 if (NILP (obj))
14711 return "no process";
14712 #ifdef subprocesses
14713 obj = Fsymbol_name (Fprocess_status (obj));
14714 #endif
14715 break;
14716
14717 case 't': /* indicate TEXT or BINARY */
14718 #ifdef MODE_LINE_BINARY_TEXT
14719 return MODE_LINE_BINARY_TEXT (b);
14720 #else
14721 return "T";
14722 #endif
14723
14724 case 'z':
14725 /* coding-system (not including end-of-line format) */
14726 case 'Z':
14727 /* coding-system (including end-of-line type) */
14728 {
14729 int eol_flag = (c == 'Z');
14730 char *p = decode_mode_spec_buf;
14731
14732 if (! FRAME_WINDOW_P (f))
14733 {
14734 /* No need to mention EOL here--the terminal never needs
14735 to do EOL conversion. */
14736 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
14737 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
14738 }
14739 p = decode_mode_spec_coding (b->buffer_file_coding_system,
14740 p, eol_flag);
14741
14742 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
14743 #ifdef subprocesses
14744 obj = Fget_buffer_process (Fcurrent_buffer ());
14745 if (PROCESSP (obj))
14746 {
14747 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
14748 p, eol_flag);
14749 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
14750 p, eol_flag);
14751 }
14752 #endif /* subprocesses */
14753 #endif /* 0 */
14754 *p = 0;
14755 return decode_mode_spec_buf;
14756 }
14757 }
14758
14759 if (STRINGP (obj))
14760 {
14761 *multibyte = STRING_MULTIBYTE (obj);
14762 return (char *) SDATA (obj);
14763 }
14764 else
14765 return "";
14766 }
14767
14768
14769 /* Count up to COUNT lines starting from START / START_BYTE.
14770 But don't go beyond LIMIT_BYTE.
14771 Return the number of lines thus found (always nonnegative).
14772
14773 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
14774
14775 static int
14776 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
14777 int start, start_byte, limit_byte, count;
14778 int *byte_pos_ptr;
14779 {
14780 register unsigned char *cursor;
14781 unsigned char *base;
14782
14783 register int ceiling;
14784 register unsigned char *ceiling_addr;
14785 int orig_count = count;
14786
14787 /* If we are not in selective display mode,
14788 check only for newlines. */
14789 int selective_display = (!NILP (current_buffer->selective_display)
14790 && !INTEGERP (current_buffer->selective_display));
14791
14792 if (count > 0)
14793 {
14794 while (start_byte < limit_byte)
14795 {
14796 ceiling = BUFFER_CEILING_OF (start_byte);
14797 ceiling = min (limit_byte - 1, ceiling);
14798 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
14799 base = (cursor = BYTE_POS_ADDR (start_byte));
14800 while (1)
14801 {
14802 if (selective_display)
14803 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
14804 ;
14805 else
14806 while (*cursor != '\n' && ++cursor != ceiling_addr)
14807 ;
14808
14809 if (cursor != ceiling_addr)
14810 {
14811 if (--count == 0)
14812 {
14813 start_byte += cursor - base + 1;
14814 *byte_pos_ptr = start_byte;
14815 return orig_count;
14816 }
14817 else
14818 if (++cursor == ceiling_addr)
14819 break;
14820 }
14821 else
14822 break;
14823 }
14824 start_byte += cursor - base;
14825 }
14826 }
14827 else
14828 {
14829 while (start_byte > limit_byte)
14830 {
14831 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
14832 ceiling = max (limit_byte, ceiling);
14833 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
14834 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
14835 while (1)
14836 {
14837 if (selective_display)
14838 while (--cursor != ceiling_addr
14839 && *cursor != '\n' && *cursor != 015)
14840 ;
14841 else
14842 while (--cursor != ceiling_addr && *cursor != '\n')
14843 ;
14844
14845 if (cursor != ceiling_addr)
14846 {
14847 if (++count == 0)
14848 {
14849 start_byte += cursor - base + 1;
14850 *byte_pos_ptr = start_byte;
14851 /* When scanning backwards, we should
14852 not count the newline posterior to which we stop. */
14853 return - orig_count - 1;
14854 }
14855 }
14856 else
14857 break;
14858 }
14859 /* Here we add 1 to compensate for the last decrement
14860 of CURSOR, which took it past the valid range. */
14861 start_byte += cursor - base + 1;
14862 }
14863 }
14864
14865 *byte_pos_ptr = limit_byte;
14866
14867 if (count < 0)
14868 return - orig_count + count;
14869 return orig_count - count;
14870
14871 }
14872
14873
14874 \f
14875 /***********************************************************************
14876 Displaying strings
14877 ***********************************************************************/
14878
14879 /* Display a NUL-terminated string, starting with index START.
14880
14881 If STRING is non-null, display that C string. Otherwise, the Lisp
14882 string LISP_STRING is displayed.
14883
14884 If FACE_STRING is not nil, FACE_STRING_POS is a position in
14885 FACE_STRING. Display STRING or LISP_STRING with the face at
14886 FACE_STRING_POS in FACE_STRING:
14887
14888 Display the string in the environment given by IT, but use the
14889 standard display table, temporarily.
14890
14891 FIELD_WIDTH is the minimum number of output glyphs to produce.
14892 If STRING has fewer characters than FIELD_WIDTH, pad to the right
14893 with spaces. If STRING has more characters, more than FIELD_WIDTH
14894 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
14895
14896 PRECISION is the maximum number of characters to output from
14897 STRING. PRECISION < 0 means don't truncate the string.
14898
14899 This is roughly equivalent to printf format specifiers:
14900
14901 FIELD_WIDTH PRECISION PRINTF
14902 ----------------------------------------
14903 -1 -1 %s
14904 -1 10 %.10s
14905 10 -1 %10s
14906 20 10 %20.10s
14907
14908 MULTIBYTE zero means do not display multibyte chars, > 0 means do
14909 display them, and < 0 means obey the current buffer's value of
14910 enable_multibyte_characters.
14911
14912 Value is the number of glyphs produced. */
14913
14914 static int
14915 display_string (string, lisp_string, face_string, face_string_pos,
14916 start, it, field_width, precision, max_x, multibyte)
14917 unsigned char *string;
14918 Lisp_Object lisp_string;
14919 Lisp_Object face_string;
14920 int face_string_pos;
14921 int start;
14922 struct it *it;
14923 int field_width, precision, max_x;
14924 int multibyte;
14925 {
14926 int hpos_at_start = it->hpos;
14927 int saved_face_id = it->face_id;
14928 struct glyph_row *row = it->glyph_row;
14929
14930 /* Initialize the iterator IT for iteration over STRING beginning
14931 with index START. */
14932 reseat_to_string (it, string, lisp_string, start,
14933 precision, field_width, multibyte);
14934
14935 /* If displaying STRING, set up the face of the iterator
14936 from LISP_STRING, if that's given. */
14937 if (STRINGP (face_string))
14938 {
14939 int endptr;
14940 struct face *face;
14941
14942 it->face_id
14943 = face_at_string_position (it->w, face_string, face_string_pos,
14944 0, it->region_beg_charpos,
14945 it->region_end_charpos,
14946 &endptr, it->base_face_id, 0);
14947 face = FACE_FROM_ID (it->f, it->face_id);
14948 it->face_box_p = face->box != FACE_NO_BOX;
14949 }
14950
14951 /* Set max_x to the maximum allowed X position. Don't let it go
14952 beyond the right edge of the window. */
14953 if (max_x <= 0)
14954 max_x = it->last_visible_x;
14955 else
14956 max_x = min (max_x, it->last_visible_x);
14957
14958 /* Skip over display elements that are not visible. because IT->w is
14959 hscrolled. */
14960 if (it->current_x < it->first_visible_x)
14961 move_it_in_display_line_to (it, 100000, it->first_visible_x,
14962 MOVE_TO_POS | MOVE_TO_X);
14963
14964 row->ascent = it->max_ascent;
14965 row->height = it->max_ascent + it->max_descent;
14966 row->phys_ascent = it->max_phys_ascent;
14967 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14968
14969 /* This condition is for the case that we are called with current_x
14970 past last_visible_x. */
14971 while (it->current_x < max_x)
14972 {
14973 int x_before, x, n_glyphs_before, i, nglyphs;
14974
14975 /* Get the next display element. */
14976 if (!get_next_display_element (it))
14977 break;
14978
14979 /* Produce glyphs. */
14980 x_before = it->current_x;
14981 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
14982 PRODUCE_GLYPHS (it);
14983
14984 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
14985 i = 0;
14986 x = x_before;
14987 while (i < nglyphs)
14988 {
14989 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
14990
14991 if (!it->truncate_lines_p
14992 && x + glyph->pixel_width > max_x)
14993 {
14994 /* End of continued line or max_x reached. */
14995 if (CHAR_GLYPH_PADDING_P (*glyph))
14996 {
14997 /* A wide character is unbreakable. */
14998 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
14999 it->current_x = x_before;
15000 }
15001 else
15002 {
15003 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
15004 it->current_x = x;
15005 }
15006 break;
15007 }
15008 else if (x + glyph->pixel_width > it->first_visible_x)
15009 {
15010 /* Glyph is at least partially visible. */
15011 ++it->hpos;
15012 if (x < it->first_visible_x)
15013 it->glyph_row->x = x - it->first_visible_x;
15014 }
15015 else
15016 {
15017 /* Glyph is off the left margin of the display area.
15018 Should not happen. */
15019 abort ();
15020 }
15021
15022 row->ascent = max (row->ascent, it->max_ascent);
15023 row->height = max (row->height, it->max_ascent + it->max_descent);
15024 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15025 row->phys_height = max (row->phys_height,
15026 it->max_phys_ascent + it->max_phys_descent);
15027 x += glyph->pixel_width;
15028 ++i;
15029 }
15030
15031 /* Stop if max_x reached. */
15032 if (i < nglyphs)
15033 break;
15034
15035 /* Stop at line ends. */
15036 if (ITERATOR_AT_END_OF_LINE_P (it))
15037 {
15038 it->continuation_lines_width = 0;
15039 break;
15040 }
15041
15042 set_iterator_to_next (it, 1);
15043
15044 /* Stop if truncating at the right edge. */
15045 if (it->truncate_lines_p
15046 && it->current_x >= it->last_visible_x)
15047 {
15048 /* Add truncation mark, but don't do it if the line is
15049 truncated at a padding space. */
15050 if (IT_CHARPOS (*it) < it->string_nchars)
15051 {
15052 if (!FRAME_WINDOW_P (it->f))
15053 {
15054 int i, n;
15055
15056 if (it->current_x > it->last_visible_x)
15057 {
15058 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
15059 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
15060 break;
15061 for (n = row->used[TEXT_AREA]; i < n; ++i)
15062 {
15063 row->used[TEXT_AREA] = i;
15064 produce_special_glyphs (it, IT_TRUNCATION);
15065 }
15066 }
15067 produce_special_glyphs (it, IT_TRUNCATION);
15068 }
15069 it->glyph_row->truncated_on_right_p = 1;
15070 }
15071 break;
15072 }
15073 }
15074
15075 /* Maybe insert a truncation at the left. */
15076 if (it->first_visible_x
15077 && IT_CHARPOS (*it) > 0)
15078 {
15079 if (!FRAME_WINDOW_P (it->f))
15080 insert_left_trunc_glyphs (it);
15081 it->glyph_row->truncated_on_left_p = 1;
15082 }
15083
15084 it->face_id = saved_face_id;
15085
15086 /* Value is number of columns displayed. */
15087 return it->hpos - hpos_at_start;
15088 }
15089
15090
15091 \f
15092 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
15093 appears as an element of LIST or as the car of an element of LIST.
15094 If PROPVAL is a list, compare each element against LIST in that
15095 way, and return 1/2 if any element of PROPVAL is found in LIST.
15096 Otherwise return 0. This function cannot quit.
15097 The return value is 2 if the text is invisible but with an ellipsis
15098 and 1 if it's invisible and without an ellipsis. */
15099
15100 int
15101 invisible_p (propval, list)
15102 register Lisp_Object propval;
15103 Lisp_Object list;
15104 {
15105 register Lisp_Object tail, proptail;
15106
15107 for (tail = list; CONSP (tail); tail = XCDR (tail))
15108 {
15109 register Lisp_Object tem;
15110 tem = XCAR (tail);
15111 if (EQ (propval, tem))
15112 return 1;
15113 if (CONSP (tem) && EQ (propval, XCAR (tem)))
15114 return NILP (XCDR (tem)) ? 1 : 2;
15115 }
15116
15117 if (CONSP (propval))
15118 {
15119 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
15120 {
15121 Lisp_Object propelt;
15122 propelt = XCAR (proptail);
15123 for (tail = list; CONSP (tail); tail = XCDR (tail))
15124 {
15125 register Lisp_Object tem;
15126 tem = XCAR (tail);
15127 if (EQ (propelt, tem))
15128 return 1;
15129 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
15130 return NILP (XCDR (tem)) ? 1 : 2;
15131 }
15132 }
15133 }
15134
15135 return 0;
15136 }
15137
15138 \f
15139 /***********************************************************************
15140 Initialization
15141 ***********************************************************************/
15142
15143 void
15144 syms_of_xdisp ()
15145 {
15146 Vwith_echo_area_save_vector = Qnil;
15147 staticpro (&Vwith_echo_area_save_vector);
15148
15149 Vmessage_stack = Qnil;
15150 staticpro (&Vmessage_stack);
15151
15152 Qinhibit_redisplay = intern ("inhibit-redisplay");
15153 staticpro (&Qinhibit_redisplay);
15154
15155 message_dolog_marker1 = Fmake_marker ();
15156 staticpro (&message_dolog_marker1);
15157 message_dolog_marker2 = Fmake_marker ();
15158 staticpro (&message_dolog_marker2);
15159 message_dolog_marker3 = Fmake_marker ();
15160 staticpro (&message_dolog_marker3);
15161
15162 #if GLYPH_DEBUG
15163 defsubr (&Sdump_frame_glyph_matrix);
15164 defsubr (&Sdump_glyph_matrix);
15165 defsubr (&Sdump_glyph_row);
15166 defsubr (&Sdump_tool_bar_row);
15167 defsubr (&Strace_redisplay);
15168 defsubr (&Strace_to_stderr);
15169 #endif
15170 #ifdef HAVE_WINDOW_SYSTEM
15171 defsubr (&Stool_bar_lines_needed);
15172 #endif
15173 defsubr (&Sformat_mode_line);
15174
15175 staticpro (&Qmenu_bar_update_hook);
15176 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
15177
15178 staticpro (&Qoverriding_terminal_local_map);
15179 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
15180
15181 staticpro (&Qoverriding_local_map);
15182 Qoverriding_local_map = intern ("overriding-local-map");
15183
15184 staticpro (&Qwindow_scroll_functions);
15185 Qwindow_scroll_functions = intern ("window-scroll-functions");
15186
15187 staticpro (&Qredisplay_end_trigger_functions);
15188 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
15189
15190 staticpro (&Qinhibit_point_motion_hooks);
15191 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
15192
15193 QCdata = intern (":data");
15194 staticpro (&QCdata);
15195 Qdisplay = intern ("display");
15196 staticpro (&Qdisplay);
15197 Qspace_width = intern ("space-width");
15198 staticpro (&Qspace_width);
15199 Qraise = intern ("raise");
15200 staticpro (&Qraise);
15201 Qspace = intern ("space");
15202 staticpro (&Qspace);
15203 Qmargin = intern ("margin");
15204 staticpro (&Qmargin);
15205 Qleft_margin = intern ("left-margin");
15206 staticpro (&Qleft_margin);
15207 Qright_margin = intern ("right-margin");
15208 staticpro (&Qright_margin);
15209 Qalign_to = intern ("align-to");
15210 staticpro (&Qalign_to);
15211 QCalign_to = intern (":align-to");
15212 staticpro (&QCalign_to);
15213 Qrelative_width = intern ("relative-width");
15214 staticpro (&Qrelative_width);
15215 QCrelative_width = intern (":relative-width");
15216 staticpro (&QCrelative_width);
15217 QCrelative_height = intern (":relative-height");
15218 staticpro (&QCrelative_height);
15219 QCeval = intern (":eval");
15220 staticpro (&QCeval);
15221 QCpropertize = intern (":propertize");
15222 staticpro (&QCpropertize);
15223 Qwhen = intern ("when");
15224 staticpro (&Qwhen);
15225 QCfile = intern (":file");
15226 staticpro (&QCfile);
15227 Qfontified = intern ("fontified");
15228 staticpro (&Qfontified);
15229 Qfontification_functions = intern ("fontification-functions");
15230 staticpro (&Qfontification_functions);
15231 Qtrailing_whitespace = intern ("trailing-whitespace");
15232 staticpro (&Qtrailing_whitespace);
15233 Qimage = intern ("image");
15234 staticpro (&Qimage);
15235 Qmessage_truncate_lines = intern ("message-truncate-lines");
15236 staticpro (&Qmessage_truncate_lines);
15237 Qcursor_in_non_selected_windows = intern ("cursor-in-non-selected-windows");
15238 staticpro (&Qcursor_in_non_selected_windows);
15239 Qgrow_only = intern ("grow-only");
15240 staticpro (&Qgrow_only);
15241 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
15242 staticpro (&Qinhibit_menubar_update);
15243 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
15244 staticpro (&Qinhibit_eval_during_redisplay);
15245 Qposition = intern ("position");
15246 staticpro (&Qposition);
15247 Qbuffer_position = intern ("buffer-position");
15248 staticpro (&Qbuffer_position);
15249 Qobject = intern ("object");
15250 staticpro (&Qobject);
15251 Qrisky_local_variable = intern ("risky-local-variable");
15252 staticpro (&Qrisky_local_variable);
15253
15254 list_of_error = Fcons (intern ("error"), Qnil);
15255 staticpro (&list_of_error);
15256
15257 last_arrow_position = Qnil;
15258 last_arrow_string = Qnil;
15259 staticpro (&last_arrow_position);
15260 staticpro (&last_arrow_string);
15261
15262 echo_buffer[0] = echo_buffer[1] = Qnil;
15263 staticpro (&echo_buffer[0]);
15264 staticpro (&echo_buffer[1]);
15265
15266 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
15267 staticpro (&echo_area_buffer[0]);
15268 staticpro (&echo_area_buffer[1]);
15269
15270 Vmessages_buffer_name = build_string ("*Messages*");
15271 staticpro (&Vmessages_buffer_name);
15272
15273 mode_line_proptrans_alist = Qnil;
15274 staticpro (&mode_line_proptrans_alist);
15275
15276 mode_line_string_list = Qnil;
15277 staticpro (&mode_line_string_list);
15278
15279 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
15280 doc: /* Non-nil means highlight trailing whitespace.
15281 The face used for trailing whitespace is `trailing-whitespace'. */);
15282 Vshow_trailing_whitespace = Qnil;
15283
15284 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
15285 doc: /* Non-nil means don't actually do any redisplay.
15286 This is used for internal purposes. */);
15287 Vinhibit_redisplay = Qnil;
15288
15289 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
15290 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
15291 Vglobal_mode_string = Qnil;
15292
15293 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
15294 doc: /* Marker for where to display an arrow on top of the buffer text.
15295 This must be the beginning of a line in order to work.
15296 See also `overlay-arrow-string'. */);
15297 Voverlay_arrow_position = Qnil;
15298
15299 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
15300 doc: /* String to display as an arrow. See also `overlay-arrow-position'. */);
15301 Voverlay_arrow_string = Qnil;
15302
15303 DEFVAR_INT ("scroll-step", &scroll_step,
15304 doc: /* *The number of lines to try scrolling a window by when point moves out.
15305 If that fails to bring point back on frame, point is centered instead.
15306 If this is zero, point is always centered after it moves off frame.
15307 If you want scrolling to always be a line at a time, you should set
15308 `scroll-conservatively' to a large value rather than set this to 1. */);
15309
15310 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
15311 doc: /* *Scroll up to this many lines, to bring point back on screen.
15312 A value of zero means to scroll the text to center point vertically
15313 in the window. */);
15314 scroll_conservatively = 0;
15315
15316 DEFVAR_INT ("scroll-margin", &scroll_margin,
15317 doc: /* *Number of lines of margin at the top and bottom of a window.
15318 Recenter the window whenever point gets within this many lines
15319 of the top or bottom of the window. */);
15320 scroll_margin = 0;
15321
15322 #if GLYPH_DEBUG
15323 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
15324 #endif
15325
15326 DEFVAR_BOOL ("truncate-partial-width-windows",
15327 &truncate_partial_width_windows,
15328 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
15329 truncate_partial_width_windows = 1;
15330
15331 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
15332 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
15333 Any other value means to use the appropriate face, `mode-line',
15334 `header-line', or `menu' respectively.
15335
15336 This variable is deprecated; please change the above faces instead. */);
15337 mode_line_inverse_video = 1;
15338
15339 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
15340 doc: /* *Maximum buffer size for which line number should be displayed.
15341 If the buffer is bigger than this, the line number does not appear
15342 in the mode line. A value of nil means no limit. */);
15343 Vline_number_display_limit = Qnil;
15344
15345 DEFVAR_INT ("line-number-display-limit-width",
15346 &line_number_display_limit_width,
15347 doc: /* *Maximum line width (in characters) for line number display.
15348 If the average length of the lines near point is bigger than this, then the
15349 line number may be omitted from the mode line. */);
15350 line_number_display_limit_width = 200;
15351
15352 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
15353 doc: /* *Non-nil means highlight region even in nonselected windows. */);
15354 highlight_nonselected_windows = 0;
15355
15356 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
15357 doc: /* Non-nil if more than one frame is visible on this display.
15358 Minibuffer-only frames don't count, but iconified frames do.
15359 This variable is not guaranteed to be accurate except while processing
15360 `frame-title-format' and `icon-title-format'. */);
15361
15362 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
15363 doc: /* Template for displaying the title bar of visible frames.
15364 \(Assuming the window manager supports this feature.)
15365 This variable has the same structure as `mode-line-format' (which see),
15366 and is used only on frames for which no explicit name has been set
15367 \(see `modify-frame-parameters'). */);
15368 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
15369 doc: /* Template for displaying the title bar of an iconified frame.
15370 \(Assuming the window manager supports this feature.)
15371 This variable has the same structure as `mode-line-format' (which see),
15372 and is used only on frames for which no explicit name has been set
15373 \(see `modify-frame-parameters'). */);
15374 Vicon_title_format
15375 = Vframe_title_format
15376 = Fcons (intern ("multiple-frames"),
15377 Fcons (build_string ("%b"),
15378 Fcons (Fcons (empty_string,
15379 Fcons (intern ("invocation-name"),
15380 Fcons (build_string ("@"),
15381 Fcons (intern ("system-name"),
15382 Qnil)))),
15383 Qnil)));
15384
15385 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
15386 doc: /* Maximum number of lines to keep in the message log buffer.
15387 If nil, disable message logging. If t, log messages but don't truncate
15388 the buffer when it becomes large. */);
15389 Vmessage_log_max = make_number (50);
15390
15391 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
15392 doc: /* Functions called before redisplay, if window sizes have changed.
15393 The value should be a list of functions that take one argument.
15394 Just before redisplay, for each frame, if any of its windows have changed
15395 size since the last redisplay, or have been split or deleted,
15396 all the functions in the list are called, with the frame as argument. */);
15397 Vwindow_size_change_functions = Qnil;
15398
15399 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
15400 doc: /* List of Functions to call before redisplaying a window with scrolling.
15401 Each function is called with two arguments, the window
15402 and its new display-start position. Note that the value of `window-end'
15403 is not valid when these functions are called. */);
15404 Vwindow_scroll_functions = Qnil;
15405
15406 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
15407 doc: /* *Non-nil means automatically resize tool-bars.
15408 This increases a tool-bar's height if not all tool-bar items are visible.
15409 It decreases a tool-bar's height when it would display blank lines
15410 otherwise. */);
15411 auto_resize_tool_bars_p = 1;
15412
15413 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
15414 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
15415 auto_raise_tool_bar_buttons_p = 1;
15416
15417 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
15418 doc: /* *Margin around tool-bar buttons in pixels.
15419 If an integer, use that for both horizontal and vertical margins.
15420 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
15421 HORZ specifying the horizontal margin, and VERT specifying the
15422 vertical margin. */);
15423 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
15424
15425 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
15426 doc: /* *Relief thickness of tool-bar buttons. */);
15427 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
15428
15429 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
15430 doc: /* List of functions to call to fontify regions of text.
15431 Each function is called with one argument POS. Functions must
15432 fontify a region starting at POS in the current buffer, and give
15433 fontified regions the property `fontified'. */);
15434 Vfontification_functions = Qnil;
15435 Fmake_variable_buffer_local (Qfontification_functions);
15436
15437 DEFVAR_BOOL ("unibyte-display-via-language-environment",
15438 &unibyte_display_via_language_environment,
15439 doc: /* *Non-nil means display unibyte text according to language environment.
15440 Specifically this means that unibyte non-ASCII characters
15441 are displayed by converting them to the equivalent multibyte characters
15442 according to the current language environment. As a result, they are
15443 displayed according to the current fontset. */);
15444 unibyte_display_via_language_environment = 0;
15445
15446 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
15447 doc: /* *Maximum height for resizing mini-windows.
15448 If a float, it specifies a fraction of the mini-window frame's height.
15449 If an integer, it specifies a number of lines. */);
15450 Vmax_mini_window_height = make_float (0.25);
15451
15452 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
15453 doc: /* *How to resize mini-windows.
15454 A value of nil means don't automatically resize mini-windows.
15455 A value of t means resize them to fit the text displayed in them.
15456 A value of `grow-only', the default, means let mini-windows grow
15457 only, until their display becomes empty, at which point the windows
15458 go back to their normal size. */);
15459 Vresize_mini_windows = Qgrow_only;
15460
15461 DEFVAR_BOOL ("cursor-in-non-selected-windows",
15462 &cursor_in_non_selected_windows,
15463 doc: /* *Non-nil means display a hollow cursor in non-selected windows.
15464 nil means don't display a cursor there. */);
15465 cursor_in_non_selected_windows = 1;
15466
15467 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
15468 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
15469 automatic_hscrolling_p = 1;
15470
15471 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
15472 doc: /* *How many columns away from the window edge point is allowed to get
15473 before automatic hscrolling will horizontally scroll the window. */);
15474 hscroll_margin = 5;
15475
15476 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
15477 doc: /* *How many columns to scroll the window when point gets too close to the edge.
15478 When point is less than `automatic-hscroll-margin' columns from the window
15479 edge, automatic hscrolling will scroll the window by the amount of columns
15480 determined by this variable. If its value is a positive integer, scroll that
15481 many columns. If it's a positive floating-point number, it specifies the
15482 fraction of the window's width to scroll. If it's nil or zero, point will be
15483 centered horizontally after the scroll. Any other value, including negative
15484 numbers, are treated as if the value were zero.
15485
15486 Automatic hscrolling always moves point outside the scroll margin, so if
15487 point was more than scroll step columns inside the margin, the window will
15488 scroll more than the value given by the scroll step.
15489
15490 Note that the lower bound for automatic hscrolling specified by `scroll-left'
15491 and `scroll-right' overrides this variable's effect. */);
15492 Vhscroll_step = make_number (0);
15493
15494 DEFVAR_LISP ("image-types", &Vimage_types,
15495 doc: /* List of supported image types.
15496 Each element of the list is a symbol for a supported image type. */);
15497 Vimage_types = Qnil;
15498
15499 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
15500 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
15501 Bind this around calls to `message' to let it take effect. */);
15502 message_truncate_lines = 0;
15503
15504 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
15505 doc: /* Normal hook run for clicks on menu bar, before displaying a submenu.
15506 Can be used to update submenus whose contents should vary. */);
15507 Vmenu_bar_update_hook = Qnil;
15508
15509 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
15510 doc: /* Non-nil means don't update menu bars. Internal use only. */);
15511 inhibit_menubar_update = 0;
15512
15513 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
15514 doc: /* Non-nil means don't eval Lisp during redisplay. */);
15515 inhibit_eval_during_redisplay = 0;
15516
15517 #if GLYPH_DEBUG
15518 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
15519 doc: /* Inhibit try_window_id display optimization. */);
15520 inhibit_try_window_id = 0;
15521
15522 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
15523 doc: /* Inhibit try_window_reusing display optimization. */);
15524 inhibit_try_window_reusing = 0;
15525
15526 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
15527 doc: /* Inhibit try_cursor_movement display optimization. */);
15528 inhibit_try_cursor_movement = 0;
15529 #endif /* GLYPH_DEBUG */
15530 }
15531
15532
15533 /* Initialize this module when Emacs starts. */
15534
15535 void
15536 init_xdisp ()
15537 {
15538 Lisp_Object root_window;
15539 struct window *mini_w;
15540
15541 current_header_line_height = current_mode_line_height = -1;
15542
15543 CHARPOS (this_line_start_pos) = 0;
15544
15545 mini_w = XWINDOW (minibuf_window);
15546 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
15547
15548 if (!noninteractive)
15549 {
15550 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
15551 int i;
15552
15553 XWINDOW (root_window)->top = make_number (FRAME_TOP_MARGIN (f));
15554 set_window_height (root_window,
15555 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
15556 0);
15557 mini_w->top = make_number (FRAME_HEIGHT (f) - 1);
15558 set_window_height (minibuf_window, 1, 0);
15559
15560 XWINDOW (root_window)->width = make_number (FRAME_WIDTH (f));
15561 mini_w->width = make_number (FRAME_WIDTH (f));
15562
15563 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
15564 scratch_glyph_row.glyphs[TEXT_AREA + 1]
15565 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
15566
15567 /* The default ellipsis glyphs `...'. */
15568 for (i = 0; i < 3; ++i)
15569 default_invis_vector[i] = make_number ('.');
15570 }
15571
15572 {
15573 /* Allocate the buffer for frame titles.
15574 Also used for `format-mode-line'. */
15575 int size = 100;
15576 frame_title_buf = (char *) xmalloc (size);
15577 frame_title_buf_end = frame_title_buf + size;
15578 frame_title_ptr = NULL;
15579 }
15580
15581 help_echo_showing_p = 0;
15582 }
15583
15584