]> code.delx.au - gnu-emacs/blob - src/xdisp.c
*** empty log message ***
[gnu-emacs] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 86, 87, 88, 93, 94, 95, 97, 98, 99, 2000, 2001
3 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
23
24 Redisplay.
25
26 Emacs separates the task of updating the display from code
27 modifying global state, e.g. buffer text. This way functions
28 operating on buffers don't also have to be concerned with updating
29 the display.
30
31 Updating the display is triggered by the Lisp interpreter when it
32 decides it's time to do it. This is done either automatically for
33 you as part of the interpreter's command loop or as the result of
34 calling Lisp functions like `sit-for'. The C function `redisplay'
35 in xdisp.c is the only entry into the inner redisplay code. (Or,
36 let's say almost---see the description of direct update
37 operations, below.)
38
39 The following diagram shows how redisplay code is invoked. As you
40 can see, Lisp calls redisplay and vice versa. Under window systems
41 like X, some portions of the redisplay code are also called
42 asynchronously during mouse movement or expose events. It is very
43 important that these code parts do NOT use the C library (malloc,
44 free) because many C libraries under Unix are not reentrant. They
45 may also NOT call functions of the Lisp interpreter which could
46 change the interpreter's state. If you don't follow these rules,
47 you will encounter bugs which are very hard to explain.
48
49 (Direct functions, see below)
50 direct_output_for_insert,
51 direct_forward_char (dispnew.c)
52 +---------------------------------+
53 | |
54 | V
55 +--------------+ redisplay +----------------+
56 | Lisp machine |---------------->| Redisplay code |<--+
57 +--------------+ (xdisp.c) +----------------+ |
58 ^ | |
59 +----------------------------------+ |
60 Don't use this path when called |
61 asynchronously! |
62 |
63 expose_window (asynchronous) |
64 |
65 X expose events -----+
66
67 What does redisplay? Obviously, it has to figure out somehow what
68 has been changed since the last time the display has been updated,
69 and to make these changes visible. Preferably it would do that in
70 a moderately intelligent way, i.e. fast.
71
72 Changes in buffer text can be deduced from window and buffer
73 structures, and from some global variables like `beg_unchanged' and
74 `end_unchanged'. The contents of the display are additionally
75 recorded in a `glyph matrix', a two-dimensional matrix of glyph
76 structures. Each row in such a matrix corresponds to a line on the
77 display, and each glyph in a row corresponds to a column displaying
78 a character, an image, or what else. This matrix is called the
79 `current glyph matrix' or `current matrix' in redisplay
80 terminology.
81
82 For buffer parts that have been changed since the last update, a
83 second glyph matrix is constructed, the so called `desired glyph
84 matrix' or short `desired matrix'. Current and desired matrix are
85 then compared to find a cheap way to update the display, e.g. by
86 reusing part of the display by scrolling lines.
87
88
89 Direct operations.
90
91 You will find a lot of redisplay optimizations when you start
92 looking at the innards of redisplay. The overall goal of all these
93 optimizations is to make redisplay fast because it is done
94 frequently.
95
96 Two optimizations are not found in xdisp.c. These are the direct
97 operations mentioned above. As the name suggests they follow a
98 different principle than the rest of redisplay. Instead of
99 building a desired matrix and then comparing it with the current
100 display, they perform their actions directly on the display and on
101 the current matrix.
102
103 One direct operation updates the display after one character has
104 been entered. The other one moves the cursor by one position
105 forward or backward. You find these functions under the names
106 `direct_output_for_insert' and `direct_output_forward_char' in
107 dispnew.c.
108
109
110 Desired matrices.
111
112 Desired matrices are always built per Emacs window. The function
113 `display_line' is the central function to look at if you are
114 interested. It constructs one row in a desired matrix given an
115 iterator structure containing both a buffer position and a
116 description of the environment in which the text is to be
117 displayed. But this is too early, read on.
118
119 Characters and pixmaps displayed for a range of buffer text depend
120 on various settings of buffers and windows, on overlays and text
121 properties, on display tables, on selective display. The good news
122 is that all this hairy stuff is hidden behind a small set of
123 interface functions taking a iterator structure (struct it)
124 argument.
125
126 Iteration over things to be displayed is then simple. It is
127 started by initializing an iterator with a call to init_iterator.
128 Calls to get_next_display_element fill the iterator structure with
129 relevant information about the next thing to display. Calls to
130 set_iterator_to_next move the iterator to the next thing.
131
132 Besides this, an iterator also contains information about the
133 display environment in which glyphs for display elements are to be
134 produced. It has fields for the width and height of the display,
135 the information whether long lines are truncated or continued, a
136 current X and Y position, and lots of other stuff you can better
137 see in dispextern.h.
138
139 Glyphs in a desired matrix are normally constructed in a loop
140 calling get_next_display_element and then produce_glyphs. The call
141 to produce_glyphs will fill the iterator structure with pixel
142 information about the element being displayed and at the same time
143 produce glyphs for it. If the display element fits on the line
144 being displayed, set_iterator_to_next is called next, otherwise the
145 glyphs produced are discarded.
146
147
148 Frame matrices.
149
150 That just couldn't be all, could it? What about terminal types not
151 supporting operations on sub-windows of the screen? To update the
152 display on such a terminal, window-based glyph matrices are not
153 well suited. To be able to reuse part of the display (scrolling
154 lines up and down), we must instead have a view of the whole
155 screen. This is what `frame matrices' are for. They are a trick.
156
157 Frames on terminals like above have a glyph pool. Windows on such
158 a frame sub-allocate their glyph memory from their frame's glyph
159 pool. The frame itself is given its own glyph matrices. By
160 coincidence---or maybe something else---rows in window glyph
161 matrices are slices of corresponding rows in frame matrices. Thus
162 writing to window matrices implicitly updates a frame matrix which
163 provides us with the view of the whole screen that we originally
164 wanted to have without having to move many bytes around. To be
165 honest, there is a little bit more done, but not much more. If you
166 plan to extend that code, take a look at dispnew.c. The function
167 build_frame_matrix is a good starting point. */
168
169 #include <config.h>
170 #include <stdio.h>
171
172 #include "lisp.h"
173 #include "keyboard.h"
174 #include "frame.h"
175 #include "window.h"
176 #include "termchar.h"
177 #include "dispextern.h"
178 #include "buffer.h"
179 #include "character.h"
180 #include "charset.h"
181 #include "indent.h"
182 #include "commands.h"
183 #include "macros.h"
184 #include "disptab.h"
185 #include "termhooks.h"
186 #include "intervals.h"
187 #include "coding.h"
188 #include "process.h"
189 #include "region-cache.h"
190 #include "fontset.h"
191
192 #ifdef HAVE_X_WINDOWS
193 #include "xterm.h"
194 #endif
195 #ifdef WINDOWSNT
196 #include "w32term.h"
197 #endif
198 #ifdef macintosh
199 #include "macterm.h"
200 #endif
201
202 #define INFINITY 10000000
203
204 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
205 extern void set_frame_menubar P_ ((struct frame *f, int, int));
206 extern int pending_menu_activation;
207 #endif
208
209 extern int interrupt_input;
210 extern int command_loop_level;
211
212 extern int minibuffer_auto_raise;
213
214 extern Lisp_Object Qface;
215
216 extern Lisp_Object Voverriding_local_map;
217 extern Lisp_Object Voverriding_local_map_menu_flag;
218 extern Lisp_Object Qmenu_item;
219
220 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
221 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
222 Lisp_Object Qredisplay_end_trigger_functions;
223 Lisp_Object Qinhibit_point_motion_hooks;
224 Lisp_Object QCeval, Qwhen, QCfile, QCdata, QCpropertize;
225 Lisp_Object Qfontified;
226 Lisp_Object Qgrow_only;
227 Lisp_Object Qinhibit_eval_during_redisplay;
228 Lisp_Object Qbuffer_position, Qposition, Qobject;
229
230 /* Functions called to fontify regions of text. */
231
232 Lisp_Object Vfontification_functions;
233 Lisp_Object Qfontification_functions;
234
235 /* Non-zero means draw tool bar buttons raised when the mouse moves
236 over them. */
237
238 int auto_raise_tool_bar_buttons_p;
239
240 /* Margin around tool bar buttons in pixels. */
241
242 Lisp_Object Vtool_bar_button_margin;
243
244 /* Thickness of shadow to draw around tool bar buttons. */
245
246 int tool_bar_button_relief;
247
248 /* Non-zero means automatically resize tool-bars so that all tool-bar
249 items are visible, and no blank lines remain. */
250
251 int auto_resize_tool_bars_p;
252
253 /* Non-nil means don't actually do any redisplay. */
254
255 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
256
257 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
258
259 int inhibit_eval_during_redisplay;
260
261 /* Names of text properties relevant for redisplay. */
262
263 Lisp_Object Qdisplay, Qrelative_width, Qalign_to;
264 extern Lisp_Object Qface, Qinvisible, Qwidth;
265
266 /* Symbols used in text property values. */
267
268 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
269 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
270 Lisp_Object Qmargin;
271 extern Lisp_Object Qheight;
272
273 /* Non-nil means highlight trailing whitespace. */
274
275 Lisp_Object Vshow_trailing_whitespace;
276
277 /* Name of the face used to highlight trailing whitespace. */
278
279 Lisp_Object Qtrailing_whitespace;
280
281 /* The symbol `image' which is the car of the lists used to represent
282 images in Lisp. */
283
284 Lisp_Object Qimage;
285
286 /* Non-zero means print newline to stdout before next mini-buffer
287 message. */
288
289 int noninteractive_need_newline;
290
291 /* Non-zero means print newline to message log before next message. */
292
293 static int message_log_need_newline;
294
295 /* Three markers that message_dolog uses.
296 It could allocate them itself, but that causes trouble
297 in handling memory-full errors. */
298 static Lisp_Object message_dolog_marker1;
299 static Lisp_Object message_dolog_marker2;
300 static Lisp_Object message_dolog_marker3;
301 \f
302 /* The buffer position of the first character appearing entirely or
303 partially on the line of the selected window which contains the
304 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
305 redisplay optimization in redisplay_internal. */
306
307 static struct text_pos this_line_start_pos;
308
309 /* Number of characters past the end of the line above, including the
310 terminating newline. */
311
312 static struct text_pos this_line_end_pos;
313
314 /* The vertical positions and the height of this line. */
315
316 static int this_line_vpos;
317 static int this_line_y;
318 static int this_line_pixel_height;
319
320 /* X position at which this display line starts. Usually zero;
321 negative if first character is partially visible. */
322
323 static int this_line_start_x;
324
325 /* Buffer that this_line_.* variables are referring to. */
326
327 static struct buffer *this_line_buffer;
328
329 /* Nonzero means truncate lines in all windows less wide than the
330 frame. */
331
332 int truncate_partial_width_windows;
333
334 /* A flag to control how to display unibyte 8-bit character. */
335
336 int unibyte_display_via_language_environment;
337
338 /* Nonzero means we have more than one non-mini-buffer-only frame.
339 Not guaranteed to be accurate except while parsing
340 frame-title-format. */
341
342 int multiple_frames;
343
344 Lisp_Object Vglobal_mode_string;
345
346 /* Marker for where to display an arrow on top of the buffer text. */
347
348 Lisp_Object Voverlay_arrow_position;
349
350 /* String to display for the arrow. Only used on terminal frames. */
351
352 Lisp_Object Voverlay_arrow_string;
353
354 /* Values of those variables at last redisplay. However, if
355 Voverlay_arrow_position is a marker, last_arrow_position is its
356 numerical position. */
357
358 static Lisp_Object last_arrow_position, last_arrow_string;
359
360 /* Like mode-line-format, but for the title bar on a visible frame. */
361
362 Lisp_Object Vframe_title_format;
363
364 /* Like mode-line-format, but for the title bar on an iconified frame. */
365
366 Lisp_Object Vicon_title_format;
367
368 /* List of functions to call when a window's size changes. These
369 functions get one arg, a frame on which one or more windows' sizes
370 have changed. */
371
372 static Lisp_Object Vwindow_size_change_functions;
373
374 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
375
376 /* Nonzero if overlay arrow has been displayed once in this window. */
377
378 static int overlay_arrow_seen;
379
380 /* Nonzero means highlight the region even in nonselected windows. */
381
382 int highlight_nonselected_windows;
383
384 /* If cursor motion alone moves point off frame, try scrolling this
385 many lines up or down if that will bring it back. */
386
387 static int scroll_step;
388
389 /* Nonzero means scroll just far enough to bring point back on the
390 screen, when appropriate. */
391
392 static int scroll_conservatively;
393
394 /* Recenter the window whenever point gets within this many lines of
395 the top or bottom of the window. This value is translated into a
396 pixel value by multiplying it with CANON_Y_UNIT, which means that
397 there is really a fixed pixel height scroll margin. */
398
399 int scroll_margin;
400
401 /* Number of windows showing the buffer of the selected window (or
402 another buffer with the same base buffer). keyboard.c refers to
403 this. */
404
405 int buffer_shared;
406
407 /* Vector containing glyphs for an ellipsis `...'. */
408
409 static Lisp_Object default_invis_vector[3];
410
411 /* Zero means display the mode-line/header-line/menu-bar in the default face
412 (this slightly odd definition is for compatibility with previous versions
413 of emacs), non-zero means display them using their respective faces.
414
415 This variable is deprecated. */
416
417 int mode_line_inverse_video;
418
419 /* Prompt to display in front of the mini-buffer contents. */
420
421 Lisp_Object minibuf_prompt;
422
423 /* Width of current mini-buffer prompt. Only set after display_line
424 of the line that contains the prompt. */
425
426 int minibuf_prompt_width;
427 int minibuf_prompt_pixel_width;
428
429 /* This is the window where the echo area message was displayed. It
430 is always a mini-buffer window, but it may not be the same window
431 currently active as a mini-buffer. */
432
433 Lisp_Object echo_area_window;
434
435 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
436 pushes the current message and the value of
437 message_enable_multibyte on the stack, the function restore_message
438 pops the stack and displays MESSAGE again. */
439
440 Lisp_Object Vmessage_stack;
441
442 /* Nonzero means multibyte characters were enabled when the echo area
443 message was specified. */
444
445 int message_enable_multibyte;
446
447 /* Nonzero if we should redraw the mode lines on the next redisplay. */
448
449 int update_mode_lines;
450
451 /* Nonzero if window sizes or contents have changed since last
452 redisplay that finished. */
453
454 int windows_or_buffers_changed;
455
456 /* Nonzero after display_mode_line if %l was used and it displayed a
457 line number. */
458
459 int line_number_displayed;
460
461 /* Maximum buffer size for which to display line numbers. */
462
463 Lisp_Object Vline_number_display_limit;
464
465 /* Line width to consider when repositioning for line number display. */
466
467 static int line_number_display_limit_width;
468
469 /* Number of lines to keep in the message log buffer. t means
470 infinite. nil means don't log at all. */
471
472 Lisp_Object Vmessage_log_max;
473
474 /* The name of the *Messages* buffer, a string. */
475
476 static Lisp_Object Vmessages_buffer_name;
477
478 /* Current, index 0, and last displayed echo area message. Either
479 buffers from echo_buffers, or nil to indicate no message. */
480
481 Lisp_Object echo_area_buffer[2];
482
483 /* The buffers referenced from echo_area_buffer. */
484
485 static Lisp_Object echo_buffer[2];
486
487 /* A vector saved used in with_area_buffer to reduce consing. */
488
489 static Lisp_Object Vwith_echo_area_save_vector;
490
491 /* Non-zero means display_echo_area should display the last echo area
492 message again. Set by redisplay_preserve_echo_area. */
493
494 static int display_last_displayed_message_p;
495
496 /* Nonzero if echo area is being used by print; zero if being used by
497 message. */
498
499 int message_buf_print;
500
501 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
502
503 Lisp_Object Qinhibit_menubar_update;
504 int inhibit_menubar_update;
505
506 /* Maximum height for resizing mini-windows. Either a float
507 specifying a fraction of the available height, or an integer
508 specifying a number of lines. */
509
510 Lisp_Object Vmax_mini_window_height;
511
512 /* Non-zero means messages should be displayed with truncated
513 lines instead of being continued. */
514
515 int message_truncate_lines;
516 Lisp_Object Qmessage_truncate_lines;
517
518 /* Set to 1 in clear_message to make redisplay_internal aware
519 of an emptied echo area. */
520
521 static int message_cleared_p;
522
523 /* Non-zero means we want a hollow cursor in windows that are not
524 selected. Zero means there's no cursor in such windows. */
525
526 int cursor_in_non_selected_windows;
527 Lisp_Object Qcursor_in_non_selected_windows;
528
529 /* A scratch glyph row with contents used for generating truncation
530 glyphs. Also used in direct_output_for_insert. */
531
532 #define MAX_SCRATCH_GLYPHS 100
533 struct glyph_row scratch_glyph_row;
534 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
535
536 /* Ascent and height of the last line processed by move_it_to. */
537
538 static int last_max_ascent, last_height;
539
540 /* Non-zero if there's a help-echo in the echo area. */
541
542 int help_echo_showing_p;
543
544 /* If >= 0, computed, exact values of mode-line and header-line height
545 to use in the macros CURRENT_MODE_LINE_HEIGHT and
546 CURRENT_HEADER_LINE_HEIGHT. */
547
548 int current_mode_line_height, current_header_line_height;
549
550 /* The maximum distance to look ahead for text properties. Values
551 that are too small let us call compute_char_face and similar
552 functions too often which is expensive. Values that are too large
553 let us call compute_char_face and alike too often because we
554 might not be interested in text properties that far away. */
555
556 #define TEXT_PROP_DISTANCE_LIMIT 100
557
558 #if GLYPH_DEBUG
559
560 /* Variables to turn off display optimizations from Lisp. */
561
562 int inhibit_try_window_id, inhibit_try_window_reusing;
563 int inhibit_try_cursor_movement;
564
565 /* Non-zero means print traces of redisplay if compiled with
566 GLYPH_DEBUG != 0. */
567
568 int trace_redisplay_p;
569
570 #endif /* GLYPH_DEBUG */
571
572 #ifdef DEBUG_TRACE_MOVE
573 /* Non-zero means trace with TRACE_MOVE to stderr. */
574 int trace_move;
575
576 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
577 #else
578 #define TRACE_MOVE(x) (void) 0
579 #endif
580
581 /* Non-zero means automatically scroll windows horizontally to make
582 point visible. */
583
584 int automatic_hscrolling_p;
585
586 /* How close to the margin can point get before the window is scrolled
587 horizontally. */
588 int automatic_hscroll_margin;
589
590 /* How much to scroll horizontally when point is inside the above margin. */
591 Lisp_Object Vautomatic_hscroll_step;
592
593 /* A list of symbols, one for each supported image type. */
594
595 Lisp_Object Vimage_types;
596
597 /* The variable `resize-mini-windows'. If nil, don't resize
598 mini-windows. If t, always resize them to fit the text they
599 display. If `grow-only', let mini-windows grow only until they
600 become empty. */
601
602 Lisp_Object Vresize_mini_windows;
603
604 /* Buffer being redisplayed -- for redisplay_window_error. */
605
606 struct buffer *displayed_buffer;
607
608 /* Value returned from text property handlers (see below). */
609
610 enum prop_handled
611 {
612 HANDLED_NORMALLY,
613 HANDLED_RECOMPUTE_PROPS,
614 HANDLED_OVERLAY_STRING_CONSUMED,
615 HANDLED_RETURN
616 };
617
618 /* A description of text properties that redisplay is interested
619 in. */
620
621 struct props
622 {
623 /* The name of the property. */
624 Lisp_Object *name;
625
626 /* A unique index for the property. */
627 enum prop_idx idx;
628
629 /* A handler function called to set up iterator IT from the property
630 at IT's current position. Value is used to steer handle_stop. */
631 enum prop_handled (*handler) P_ ((struct it *it));
632 };
633
634 static enum prop_handled handle_face_prop P_ ((struct it *));
635 static enum prop_handled handle_invisible_prop P_ ((struct it *));
636 static enum prop_handled handle_display_prop P_ ((struct it *));
637 static enum prop_handled handle_composition_prop P_ ((struct it *));
638 static enum prop_handled handle_overlay_change P_ ((struct it *));
639 static enum prop_handled handle_fontified_prop P_ ((struct it *));
640 static enum prop_handled handle_auto_composed_prop P_ ((struct it *));
641
642 /* Properties handled by iterators. */
643
644 static struct props it_props[] =
645 {
646 {&Qauto_composed, AUTO_COMPOSED_PROP_IDX, handle_auto_composed_prop},
647 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
648 /* Handle `face' before `display' because some sub-properties of
649 `display' need to know the face. */
650 {&Qface, FACE_PROP_IDX, handle_face_prop},
651 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
652 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
653 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
654 {NULL, 0, NULL}
655 };
656
657 /* Value is the position described by X. If X is a marker, value is
658 the marker_position of X. Otherwise, value is X. */
659
660 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
661
662 /* Enumeration returned by some move_it_.* functions internally. */
663
664 enum move_it_result
665 {
666 /* Not used. Undefined value. */
667 MOVE_UNDEFINED,
668
669 /* Move ended at the requested buffer position or ZV. */
670 MOVE_POS_MATCH_OR_ZV,
671
672 /* Move ended at the requested X pixel position. */
673 MOVE_X_REACHED,
674
675 /* Move within a line ended at the end of a line that must be
676 continued. */
677 MOVE_LINE_CONTINUED,
678
679 /* Move within a line ended at the end of a line that would
680 be displayed truncated. */
681 MOVE_LINE_TRUNCATED,
682
683 /* Move within a line ended at a line end. */
684 MOVE_NEWLINE_OR_CR
685 };
686
687
688 \f
689 /* Function prototypes. */
690
691 static void setup_for_ellipsis P_ ((struct it *));
692 static void mark_window_display_accurate_1 P_ ((struct window *, int));
693 static int single_display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
694 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
695 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
696 static int redisplay_mode_lines P_ ((Lisp_Object, int));
697 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
698
699 #if 0
700 static int invisible_text_between_p P_ ((struct it *, int, int));
701 #endif
702
703 static int next_element_from_ellipsis P_ ((struct it *));
704 static void pint2str P_ ((char *, int, int));
705 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
706 struct text_pos));
707 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
708 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
709 static void store_frame_title_char P_ ((char));
710 static int store_frame_title P_ ((unsigned char *, int, int));
711 static void x_consider_frame_title P_ ((Lisp_Object));
712 static void handle_stop P_ ((struct it *));
713 static int tool_bar_lines_needed P_ ((struct frame *));
714 static int single_display_prop_intangible_p P_ ((Lisp_Object));
715 static void ensure_echo_area_buffers P_ ((void));
716 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
717 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
718 static int with_echo_area_buffer P_ ((struct window *, int,
719 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
720 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
721 static void clear_garbaged_frames P_ ((void));
722 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
723 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
724 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
725 static int display_echo_area P_ ((struct window *));
726 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
727 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
728 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
729 static int string_char_and_length P_ ((unsigned char *, int, int *));
730 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
731 struct text_pos));
732 static int compute_window_start_on_continuation_line P_ ((struct window *));
733 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
734 static void insert_left_trunc_glyphs P_ ((struct it *));
735 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
736 static void extend_face_to_end_of_line P_ ((struct it *));
737 static int append_space P_ ((struct it *, int));
738 static int make_cursor_line_fully_visible P_ ((struct window *));
739 static int try_scrolling P_ ((Lisp_Object, int, int, int, int));
740 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
741 static int trailing_whitespace_p P_ ((int));
742 static int message_log_check_duplicate P_ ((int, int, int, int));
743 static void push_it P_ ((struct it *));
744 static void pop_it P_ ((struct it *));
745 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
746 static void redisplay_internal P_ ((int));
747 static int echo_area_display P_ ((int));
748 static void redisplay_windows P_ ((Lisp_Object));
749 static void redisplay_window P_ ((Lisp_Object, int));
750 static Lisp_Object redisplay_window_error ();
751 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
752 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
753 static void update_menu_bar P_ ((struct frame *, int));
754 static int try_window_reusing_current_matrix P_ ((struct window *));
755 static int try_window_id P_ ((struct window *));
756 static int display_line P_ ((struct it *));
757 static int display_mode_lines P_ ((struct window *));
758 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
759 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object));
760 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
761 static void display_menu_bar P_ ((struct window *));
762 static int display_count_lines P_ ((int, int, int, int, int *));
763 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
764 int, int, struct it *, int, int, int, int));
765 static void compute_line_metrics P_ ((struct it *));
766 static void run_redisplay_end_trigger_hook P_ ((struct it *));
767 static int get_overlay_strings P_ ((struct it *, int));
768 static void next_overlay_string P_ ((struct it *));
769 static void reseat P_ ((struct it *, struct text_pos, int));
770 static void reseat_1 P_ ((struct it *, struct text_pos, int));
771 static void back_to_previous_visible_line_start P_ ((struct it *));
772 static void reseat_at_previous_visible_line_start P_ ((struct it *));
773 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
774 static int next_element_from_display_vector P_ ((struct it *));
775 static int next_element_from_string P_ ((struct it *));
776 static int next_element_from_c_string P_ ((struct it *));
777 static int next_element_from_buffer P_ ((struct it *));
778 static int next_element_from_composition P_ ((struct it *));
779 static int next_element_from_image P_ ((struct it *));
780 static int next_element_from_stretch P_ ((struct it *));
781 static void load_overlay_strings P_ ((struct it *, int));
782 static int init_from_display_pos P_ ((struct it *, struct window *,
783 struct display_pos *));
784 static void reseat_to_string P_ ((struct it *, unsigned char *,
785 Lisp_Object, int, int, int, int));
786 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
787 int, int, int));
788 void move_it_vertically_backward P_ ((struct it *, int));
789 static void init_to_row_start P_ ((struct it *, struct window *,
790 struct glyph_row *));
791 static int init_to_row_end P_ ((struct it *, struct window *,
792 struct glyph_row *));
793 static void back_to_previous_line_start P_ ((struct it *));
794 static int forward_to_next_line_start P_ ((struct it *, int *));
795 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
796 Lisp_Object, int));
797 static struct text_pos string_pos P_ ((int, Lisp_Object));
798 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
799 static int number_of_chars P_ ((unsigned char *, int));
800 static void compute_stop_pos P_ ((struct it *));
801 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
802 Lisp_Object));
803 static int face_before_or_after_it_pos P_ ((struct it *, int));
804 static int next_overlay_change P_ ((int));
805 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
806 Lisp_Object, struct text_pos *,
807 int));
808 static int underlying_face_id P_ ((struct it *));
809 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
810 struct window *));
811
812 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
813 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
814
815 #ifdef HAVE_WINDOW_SYSTEM
816
817 static void update_tool_bar P_ ((struct frame *, int));
818 static void build_desired_tool_bar_string P_ ((struct frame *f));
819 static int redisplay_tool_bar P_ ((struct frame *));
820 static void display_tool_bar_line P_ ((struct it *));
821
822 #endif /* HAVE_WINDOW_SYSTEM */
823
824 \f
825 /***********************************************************************
826 Window display dimensions
827 ***********************************************************************/
828
829 /* Return the window-relative maximum y + 1 for glyph rows displaying
830 text in window W. This is the height of W minus the height of a
831 mode line, if any. */
832
833 INLINE int
834 window_text_bottom_y (w)
835 struct window *w;
836 {
837 struct frame *f = XFRAME (w->frame);
838 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
839
840 if (WINDOW_WANTS_MODELINE_P (w))
841 height -= CURRENT_MODE_LINE_HEIGHT (w);
842 return height;
843 }
844
845
846 /* Return the pixel width of display area AREA of window W. AREA < 0
847 means return the total width of W, not including fringes to
848 the left and right of the window. */
849
850 INLINE int
851 window_box_width (w, area)
852 struct window *w;
853 int area;
854 {
855 struct frame *f = XFRAME (w->frame);
856 int width = XFASTINT (w->width);
857
858 if (!w->pseudo_window_p)
859 {
860 width -= FRAME_SCROLL_BAR_WIDTH (f) + FRAME_FRINGE_COLS (f);
861
862 if (area == TEXT_AREA)
863 {
864 if (INTEGERP (w->left_margin_width))
865 width -= XFASTINT (w->left_margin_width);
866 if (INTEGERP (w->right_margin_width))
867 width -= XFASTINT (w->right_margin_width);
868 }
869 else if (area == LEFT_MARGIN_AREA)
870 width = (INTEGERP (w->left_margin_width)
871 ? XFASTINT (w->left_margin_width) : 0);
872 else if (area == RIGHT_MARGIN_AREA)
873 width = (INTEGERP (w->right_margin_width)
874 ? XFASTINT (w->right_margin_width) : 0);
875 }
876
877 return width * CANON_X_UNIT (f);
878 }
879
880
881 /* Return the pixel height of the display area of window W, not
882 including mode lines of W, if any. */
883
884 INLINE int
885 window_box_height (w)
886 struct window *w;
887 {
888 struct frame *f = XFRAME (w->frame);
889 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
890
891 xassert (height >= 0);
892
893 /* Note: the code below that determines the mode-line/header-line
894 height is essentially the same as that contained in the macro
895 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
896 the appropriate glyph row has its `mode_line_p' flag set,
897 and if it doesn't, uses estimate_mode_line_height instead. */
898
899 if (WINDOW_WANTS_MODELINE_P (w))
900 {
901 struct glyph_row *ml_row
902 = (w->current_matrix && w->current_matrix->rows
903 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
904 : 0);
905 if (ml_row && ml_row->mode_line_p)
906 height -= ml_row->height;
907 else
908 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
909 }
910
911 if (WINDOW_WANTS_HEADER_LINE_P (w))
912 {
913 struct glyph_row *hl_row
914 = (w->current_matrix && w->current_matrix->rows
915 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
916 : 0);
917 if (hl_row && hl_row->mode_line_p)
918 height -= hl_row->height;
919 else
920 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
921 }
922
923 /* With a very small font and a mode-line that's taller than
924 default, we might end up with a negative height. */
925 return max (0, height);
926 }
927
928
929 /* Return the frame-relative coordinate of the left edge of display
930 area AREA of window W. AREA < 0 means return the left edge of the
931 whole window, to the right of the left fringe of W. */
932
933 INLINE int
934 window_box_left (w, area)
935 struct window *w;
936 int area;
937 {
938 struct frame *f = XFRAME (w->frame);
939 int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
940
941 if (!w->pseudo_window_p)
942 {
943 x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
944 + FRAME_LEFT_FRINGE_WIDTH (f));
945
946 if (area == TEXT_AREA)
947 x += window_box_width (w, LEFT_MARGIN_AREA);
948 else if (area == RIGHT_MARGIN_AREA)
949 x += (window_box_width (w, LEFT_MARGIN_AREA)
950 + window_box_width (w, TEXT_AREA));
951 }
952
953 return x;
954 }
955
956
957 /* Return the frame-relative coordinate of the right edge of display
958 area AREA of window W. AREA < 0 means return the left edge of the
959 whole window, to the left of the right fringe of W. */
960
961 INLINE int
962 window_box_right (w, area)
963 struct window *w;
964 int area;
965 {
966 return window_box_left (w, area) + window_box_width (w, area);
967 }
968
969
970 /* Get the bounding box of the display area AREA of window W, without
971 mode lines, in frame-relative coordinates. AREA < 0 means the
972 whole window, not including the left and right fringes of
973 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
974 coordinates of the upper-left corner of the box. Return in
975 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
976
977 INLINE void
978 window_box (w, area, box_x, box_y, box_width, box_height)
979 struct window *w;
980 int area;
981 int *box_x, *box_y, *box_width, *box_height;
982 {
983 struct frame *f = XFRAME (w->frame);
984
985 *box_width = window_box_width (w, area);
986 *box_height = window_box_height (w);
987 *box_x = window_box_left (w, area);
988 *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
989 + XFASTINT (w->top) * CANON_Y_UNIT (f));
990 if (WINDOW_WANTS_HEADER_LINE_P (w))
991 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
992 }
993
994
995 /* Get the bounding box of the display area AREA of window W, without
996 mode lines. AREA < 0 means the whole window, not including the
997 left and right fringe of the window. Return in *TOP_LEFT_X
998 and TOP_LEFT_Y the frame-relative pixel coordinates of the
999 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1000 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1001 box. */
1002
1003 INLINE void
1004 window_box_edges (w, area, top_left_x, top_left_y,
1005 bottom_right_x, bottom_right_y)
1006 struct window *w;
1007 int area;
1008 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1009 {
1010 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1011 bottom_right_y);
1012 *bottom_right_x += *top_left_x;
1013 *bottom_right_y += *top_left_y;
1014 }
1015
1016
1017 \f
1018 /***********************************************************************
1019 Utilities
1020 ***********************************************************************/
1021
1022 /* Return the bottom y-position of the line the iterator IT is in.
1023 This can modify IT's settings. */
1024
1025 int
1026 line_bottom_y (it)
1027 struct it *it;
1028 {
1029 int line_height = it->max_ascent + it->max_descent;
1030 int line_top_y = it->current_y;
1031
1032 if (line_height == 0)
1033 {
1034 if (last_height)
1035 line_height = last_height;
1036 else if (IT_CHARPOS (*it) < ZV)
1037 {
1038 move_it_by_lines (it, 1, 1);
1039 line_height = (it->max_ascent || it->max_descent
1040 ? it->max_ascent + it->max_descent
1041 : last_height);
1042 }
1043 else
1044 {
1045 struct glyph_row *row = it->glyph_row;
1046
1047 /* Use the default character height. */
1048 it->glyph_row = NULL;
1049 it->what = IT_CHARACTER;
1050 it->c = ' ';
1051 it->len = 1;
1052 PRODUCE_GLYPHS (it);
1053 line_height = it->ascent + it->descent;
1054 it->glyph_row = row;
1055 }
1056 }
1057
1058 return line_top_y + line_height;
1059 }
1060
1061
1062 /* Return 1 if position CHARPOS is visible in window W. Set *FULLY to
1063 1 if POS is visible and the line containing POS is fully visible.
1064 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1065 and header-lines heights. */
1066
1067 int
1068 pos_visible_p (w, charpos, fully, exact_mode_line_heights_p)
1069 struct window *w;
1070 int charpos, *fully, exact_mode_line_heights_p;
1071 {
1072 struct it it;
1073 struct text_pos top;
1074 int visible_p;
1075 struct buffer *old_buffer = NULL;
1076
1077 if (XBUFFER (w->buffer) != current_buffer)
1078 {
1079 old_buffer = current_buffer;
1080 set_buffer_internal_1 (XBUFFER (w->buffer));
1081 }
1082
1083 *fully = visible_p = 0;
1084 SET_TEXT_POS_FROM_MARKER (top, w->start);
1085
1086 /* Compute exact mode line heights, if requested. */
1087 if (exact_mode_line_heights_p)
1088 {
1089 if (WINDOW_WANTS_MODELINE_P (w))
1090 current_mode_line_height
1091 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1092 current_buffer->mode_line_format);
1093
1094 if (WINDOW_WANTS_HEADER_LINE_P (w))
1095 current_header_line_height
1096 = display_mode_line (w, HEADER_LINE_FACE_ID,
1097 current_buffer->header_line_format);
1098 }
1099
1100 start_display (&it, w, top);
1101 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
1102 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
1103
1104 /* Note that we may overshoot because of invisible text. */
1105 if (IT_CHARPOS (it) >= charpos)
1106 {
1107 int top_y = it.current_y;
1108 int bottom_y = line_bottom_y (&it);
1109 int window_top_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
1110
1111 if (top_y < window_top_y)
1112 visible_p = bottom_y > window_top_y;
1113 else if (top_y < it.last_visible_y)
1114 {
1115 visible_p = 1;
1116 *fully = bottom_y <= it.last_visible_y;
1117 }
1118 }
1119 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1120 {
1121 move_it_by_lines (&it, 1, 0);
1122 if (charpos < IT_CHARPOS (it))
1123 {
1124 visible_p = 1;
1125 *fully = 0;
1126 }
1127 }
1128
1129 if (old_buffer)
1130 set_buffer_internal_1 (old_buffer);
1131
1132 current_header_line_height = current_mode_line_height = -1;
1133 return visible_p;
1134 }
1135
1136
1137 /* Return the next character from STR which is MAXLEN bytes long.
1138 Return in *LEN the length of the character. This is like
1139 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1140 we find one, we return a `?', but with the length of the invalid
1141 character. */
1142
1143 static INLINE int
1144 string_char_and_length (str, maxlen, len)
1145 unsigned char *str;
1146 int maxlen, *len;
1147 {
1148 int c;
1149
1150 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1151 if (!CHAR_VALID_P (c, 1))
1152 /* We may not change the length here because other places in Emacs
1153 don't use this function, i.e. they silently accept invalid
1154 characters. */
1155 c = '?';
1156
1157 return c;
1158 }
1159
1160
1161
1162 /* Given a position POS containing a valid character and byte position
1163 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1164
1165 static struct text_pos
1166 string_pos_nchars_ahead (pos, string, nchars)
1167 struct text_pos pos;
1168 Lisp_Object string;
1169 int nchars;
1170 {
1171 xassert (STRINGP (string) && nchars >= 0);
1172
1173 if (STRING_MULTIBYTE (string))
1174 {
1175 int rest = STRING_BYTES (XSTRING (string)) - BYTEPOS (pos);
1176 unsigned char *p = XSTRING (string)->data + BYTEPOS (pos);
1177 int len;
1178
1179 while (nchars--)
1180 {
1181 string_char_and_length (p, rest, &len);
1182 p += len, rest -= len;
1183 xassert (rest >= 0);
1184 CHARPOS (pos) += 1;
1185 BYTEPOS (pos) += len;
1186 }
1187 }
1188 else
1189 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1190
1191 return pos;
1192 }
1193
1194
1195 /* Value is the text position, i.e. character and byte position,
1196 for character position CHARPOS in STRING. */
1197
1198 static INLINE struct text_pos
1199 string_pos (charpos, string)
1200 int charpos;
1201 Lisp_Object string;
1202 {
1203 struct text_pos pos;
1204 xassert (STRINGP (string));
1205 xassert (charpos >= 0);
1206 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1207 return pos;
1208 }
1209
1210
1211 /* Value is a text position, i.e. character and byte position, for
1212 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1213 means recognize multibyte characters. */
1214
1215 static struct text_pos
1216 c_string_pos (charpos, s, multibyte_p)
1217 int charpos;
1218 unsigned char *s;
1219 int multibyte_p;
1220 {
1221 struct text_pos pos;
1222
1223 xassert (s != NULL);
1224 xassert (charpos >= 0);
1225
1226 if (multibyte_p)
1227 {
1228 int rest = strlen (s), len;
1229
1230 SET_TEXT_POS (pos, 0, 0);
1231 while (charpos--)
1232 {
1233 string_char_and_length (s, rest, &len);
1234 s += len, rest -= len;
1235 xassert (rest >= 0);
1236 CHARPOS (pos) += 1;
1237 BYTEPOS (pos) += len;
1238 }
1239 }
1240 else
1241 SET_TEXT_POS (pos, charpos, charpos);
1242
1243 return pos;
1244 }
1245
1246
1247 /* Value is the number of characters in C string S. MULTIBYTE_P
1248 non-zero means recognize multibyte characters. */
1249
1250 static int
1251 number_of_chars (s, multibyte_p)
1252 unsigned char *s;
1253 int multibyte_p;
1254 {
1255 int nchars;
1256
1257 if (multibyte_p)
1258 {
1259 int rest = strlen (s), len;
1260 unsigned char *p = (unsigned char *) s;
1261
1262 for (nchars = 0; rest > 0; ++nchars)
1263 {
1264 string_char_and_length (p, rest, &len);
1265 rest -= len, p += len;
1266 }
1267 }
1268 else
1269 nchars = strlen (s);
1270
1271 return nchars;
1272 }
1273
1274
1275 /* Compute byte position NEWPOS->bytepos corresponding to
1276 NEWPOS->charpos. POS is a known position in string STRING.
1277 NEWPOS->charpos must be >= POS.charpos. */
1278
1279 static void
1280 compute_string_pos (newpos, pos, string)
1281 struct text_pos *newpos, pos;
1282 Lisp_Object string;
1283 {
1284 xassert (STRINGP (string));
1285 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1286
1287 if (STRING_MULTIBYTE (string))
1288 *newpos = string_pos_nchars_ahead (pos, string,
1289 CHARPOS (*newpos) - CHARPOS (pos));
1290 else
1291 BYTEPOS (*newpos) = CHARPOS (*newpos);
1292 }
1293
1294
1295 \f
1296 /***********************************************************************
1297 Lisp form evaluation
1298 ***********************************************************************/
1299
1300 /* Error handler for safe_eval and safe_call. */
1301
1302 static Lisp_Object
1303 safe_eval_handler (arg)
1304 Lisp_Object arg;
1305 {
1306 add_to_log ("Error during redisplay: %s", arg, Qnil);
1307 return Qnil;
1308 }
1309
1310
1311 /* Evaluate SEXPR and return the result, or nil if something went
1312 wrong. Prevent redisplay during the evaluation. */
1313
1314 Lisp_Object
1315 safe_eval (sexpr)
1316 Lisp_Object sexpr;
1317 {
1318 Lisp_Object val;
1319
1320 if (inhibit_eval_during_redisplay)
1321 val = Qnil;
1322 else
1323 {
1324 int count = BINDING_STACK_SIZE ();
1325 struct gcpro gcpro1;
1326
1327 GCPRO1 (sexpr);
1328 specbind (Qinhibit_redisplay, Qt);
1329 val = internal_condition_case_1 (Feval, sexpr, Qerror,
1330 safe_eval_handler);
1331 UNGCPRO;
1332 val = unbind_to (count, val);
1333 }
1334
1335 return val;
1336 }
1337
1338
1339 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1340 Return the result, or nil if something went wrong. Prevent
1341 redisplay during the evaluation. */
1342
1343 Lisp_Object
1344 safe_call (nargs, args)
1345 int nargs;
1346 Lisp_Object *args;
1347 {
1348 Lisp_Object val;
1349
1350 if (inhibit_eval_during_redisplay)
1351 val = Qnil;
1352 else
1353 {
1354 int count = BINDING_STACK_SIZE ();
1355 struct gcpro gcpro1;
1356
1357 GCPRO1 (args[0]);
1358 gcpro1.nvars = nargs;
1359 specbind (Qinhibit_redisplay, Qt);
1360 val = internal_condition_case_2 (Ffuncall, nargs, args, Qerror,
1361 safe_eval_handler);
1362 UNGCPRO;
1363 val = unbind_to (count, val);
1364 }
1365
1366 return val;
1367 }
1368
1369
1370 /* Call function FN with one argument ARG.
1371 Return the result, or nil if something went wrong. */
1372
1373 Lisp_Object
1374 safe_call1 (fn, arg)
1375 Lisp_Object fn, arg;
1376 {
1377 Lisp_Object args[2];
1378 args[0] = fn;
1379 args[1] = arg;
1380 return safe_call (2, args);
1381 }
1382
1383
1384 \f
1385 /***********************************************************************
1386 Debugging
1387 ***********************************************************************/
1388
1389 #if 0
1390
1391 /* Define CHECK_IT to perform sanity checks on iterators.
1392 This is for debugging. It is too slow to do unconditionally. */
1393
1394 static void
1395 check_it (it)
1396 struct it *it;
1397 {
1398 if (it->method == next_element_from_string)
1399 {
1400 xassert (STRINGP (it->string));
1401 xassert (IT_STRING_CHARPOS (*it) >= 0);
1402 }
1403 else if (it->method == next_element_from_buffer)
1404 {
1405 /* Check that character and byte positions agree. */
1406 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1407 }
1408
1409 if (it->dpvec)
1410 xassert (it->current.dpvec_index >= 0);
1411 else
1412 xassert (it->current.dpvec_index < 0);
1413 }
1414
1415 #define CHECK_IT(IT) check_it ((IT))
1416
1417 #else /* not 0 */
1418
1419 #define CHECK_IT(IT) (void) 0
1420
1421 #endif /* not 0 */
1422
1423
1424 #if GLYPH_DEBUG
1425
1426 /* Check that the window end of window W is what we expect it
1427 to be---the last row in the current matrix displaying text. */
1428
1429 static void
1430 check_window_end (w)
1431 struct window *w;
1432 {
1433 if (!MINI_WINDOW_P (w)
1434 && !NILP (w->window_end_valid))
1435 {
1436 struct glyph_row *row;
1437 xassert ((row = MATRIX_ROW (w->current_matrix,
1438 XFASTINT (w->window_end_vpos)),
1439 !row->enabled_p
1440 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1441 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1442 }
1443 }
1444
1445 #define CHECK_WINDOW_END(W) check_window_end ((W))
1446
1447 #else /* not GLYPH_DEBUG */
1448
1449 #define CHECK_WINDOW_END(W) (void) 0
1450
1451 #endif /* not GLYPH_DEBUG */
1452
1453
1454 \f
1455 /***********************************************************************
1456 Iterator initialization
1457 ***********************************************************************/
1458
1459 /* Initialize IT for displaying current_buffer in window W, starting
1460 at character position CHARPOS. CHARPOS < 0 means that no buffer
1461 position is specified which is useful when the iterator is assigned
1462 a position later. BYTEPOS is the byte position corresponding to
1463 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
1464
1465 If ROW is not null, calls to produce_glyphs with IT as parameter
1466 will produce glyphs in that row.
1467
1468 BASE_FACE_ID is the id of a base face to use. It must be one of
1469 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
1470 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
1471 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
1472
1473 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
1474 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
1475 will be initialized to use the corresponding mode line glyph row of
1476 the desired matrix of W. */
1477
1478 void
1479 init_iterator (it, w, charpos, bytepos, row, base_face_id)
1480 struct it *it;
1481 struct window *w;
1482 int charpos, bytepos;
1483 struct glyph_row *row;
1484 enum face_id base_face_id;
1485 {
1486 int highlight_region_p;
1487
1488 /* Some precondition checks. */
1489 xassert (w != NULL && it != NULL);
1490 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
1491 && charpos <= ZV));
1492
1493 /* If face attributes have been changed since the last redisplay,
1494 free realized faces now because they depend on face definitions
1495 that might have changed. */
1496 if (face_change_count)
1497 {
1498 face_change_count = 0;
1499 free_all_realized_faces (Qnil);
1500 }
1501
1502 /* Use one of the mode line rows of W's desired matrix if
1503 appropriate. */
1504 if (row == NULL)
1505 {
1506 if (base_face_id == MODE_LINE_FACE_ID
1507 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
1508 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
1509 else if (base_face_id == HEADER_LINE_FACE_ID)
1510 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
1511 }
1512
1513 /* Clear IT. */
1514 bzero (it, sizeof *it);
1515 it->current.overlay_string_index = -1;
1516 it->current.dpvec_index = -1;
1517 it->base_face_id = base_face_id;
1518
1519 /* The window in which we iterate over current_buffer: */
1520 XSETWINDOW (it->window, w);
1521 it->w = w;
1522 it->f = XFRAME (w->frame);
1523
1524 /* Extra space between lines (on window systems only). */
1525 if (base_face_id == DEFAULT_FACE_ID
1526 && FRAME_WINDOW_P (it->f))
1527 {
1528 if (NATNUMP (current_buffer->extra_line_spacing))
1529 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
1530 else if (it->f->extra_line_spacing > 0)
1531 it->extra_line_spacing = it->f->extra_line_spacing;
1532 }
1533
1534 /* If realized faces have been removed, e.g. because of face
1535 attribute changes of named faces, recompute them. When running
1536 in batch mode, the face cache of Vterminal_frame is null. If
1537 we happen to get called, make a dummy face cache. */
1538 if (
1539 #ifndef WINDOWSNT
1540 noninteractive &&
1541 #endif
1542 FRAME_FACE_CACHE (it->f) == NULL)
1543 init_frame_faces (it->f);
1544 if (FRAME_FACE_CACHE (it->f)->used == 0)
1545 recompute_basic_faces (it->f);
1546
1547 /* Current value of the `space-width', and 'height' properties. */
1548 it->space_width = Qnil;
1549 it->font_height = Qnil;
1550
1551 /* Are control characters displayed as `^C'? */
1552 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
1553
1554 /* -1 means everything between a CR and the following line end
1555 is invisible. >0 means lines indented more than this value are
1556 invisible. */
1557 it->selective = (INTEGERP (current_buffer->selective_display)
1558 ? XFASTINT (current_buffer->selective_display)
1559 : (!NILP (current_buffer->selective_display)
1560 ? -1 : 0));
1561 it->selective_display_ellipsis_p
1562 = !NILP (current_buffer->selective_display_ellipses);
1563
1564 /* Display table to use. */
1565 it->dp = window_display_table (w);
1566
1567 /* Are multibyte characters enabled in current_buffer? */
1568 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1569
1570 /* Non-zero if we should highlight the region. */
1571 highlight_region_p
1572 = (!NILP (Vtransient_mark_mode)
1573 && !NILP (current_buffer->mark_active)
1574 && XMARKER (current_buffer->mark)->buffer != 0);
1575
1576 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
1577 start and end of a visible region in window IT->w. Set both to
1578 -1 to indicate no region. */
1579 if (highlight_region_p
1580 /* Maybe highlight only in selected window. */
1581 && (/* Either show region everywhere. */
1582 highlight_nonselected_windows
1583 /* Or show region in the selected window. */
1584 || w == XWINDOW (selected_window)
1585 /* Or show the region if we are in the mini-buffer and W is
1586 the window the mini-buffer refers to. */
1587 || (MINI_WINDOW_P (XWINDOW (selected_window))
1588 && WINDOWP (Vminibuf_selected_window)
1589 && w == XWINDOW (Vminibuf_selected_window))))
1590 {
1591 int charpos = marker_position (current_buffer->mark);
1592 it->region_beg_charpos = min (PT, charpos);
1593 it->region_end_charpos = max (PT, charpos);
1594 }
1595 else
1596 it->region_beg_charpos = it->region_end_charpos = -1;
1597
1598 /* Get the position at which the redisplay_end_trigger hook should
1599 be run, if it is to be run at all. */
1600 if (MARKERP (w->redisplay_end_trigger)
1601 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
1602 it->redisplay_end_trigger_charpos
1603 = marker_position (w->redisplay_end_trigger);
1604 else if (INTEGERP (w->redisplay_end_trigger))
1605 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
1606
1607 /* Correct bogus values of tab_width. */
1608 it->tab_width = XINT (current_buffer->tab_width);
1609 if (it->tab_width <= 0 || it->tab_width > 1000)
1610 it->tab_width = 8;
1611
1612 /* Are lines in the display truncated? */
1613 it->truncate_lines_p
1614 = (base_face_id != DEFAULT_FACE_ID
1615 || XINT (it->w->hscroll)
1616 || (truncate_partial_width_windows
1617 && !WINDOW_FULL_WIDTH_P (it->w))
1618 || !NILP (current_buffer->truncate_lines));
1619
1620 /* Get dimensions of truncation and continuation glyphs. These are
1621 displayed as fringe bitmaps under X, so we don't need them for such
1622 frames. */
1623 if (!FRAME_WINDOW_P (it->f))
1624 {
1625 if (it->truncate_lines_p)
1626 {
1627 /* We will need the truncation glyph. */
1628 xassert (it->glyph_row == NULL);
1629 produce_special_glyphs (it, IT_TRUNCATION);
1630 it->truncation_pixel_width = it->pixel_width;
1631 }
1632 else
1633 {
1634 /* We will need the continuation glyph. */
1635 xassert (it->glyph_row == NULL);
1636 produce_special_glyphs (it, IT_CONTINUATION);
1637 it->continuation_pixel_width = it->pixel_width;
1638 }
1639
1640 /* Reset these values to zero because the produce_special_glyphs
1641 above has changed them. */
1642 it->pixel_width = it->ascent = it->descent = 0;
1643 it->phys_ascent = it->phys_descent = 0;
1644 }
1645
1646 /* Set this after getting the dimensions of truncation and
1647 continuation glyphs, so that we don't produce glyphs when calling
1648 produce_special_glyphs, above. */
1649 it->glyph_row = row;
1650 it->area = TEXT_AREA;
1651
1652 /* Get the dimensions of the display area. The display area
1653 consists of the visible window area plus a horizontally scrolled
1654 part to the left of the window. All x-values are relative to the
1655 start of this total display area. */
1656 if (base_face_id != DEFAULT_FACE_ID)
1657 {
1658 /* Mode lines, menu bar in terminal frames. */
1659 it->first_visible_x = 0;
1660 it->last_visible_x = XFASTINT (w->width) * CANON_X_UNIT (it->f);
1661 }
1662 else
1663 {
1664 it->first_visible_x
1665 = XFASTINT (it->w->hscroll) * CANON_X_UNIT (it->f);
1666 it->last_visible_x = (it->first_visible_x
1667 + window_box_width (w, TEXT_AREA));
1668
1669 /* If we truncate lines, leave room for the truncator glyph(s) at
1670 the right margin. Otherwise, leave room for the continuation
1671 glyph(s). Truncation and continuation glyphs are not inserted
1672 for window-based redisplay. */
1673 if (!FRAME_WINDOW_P (it->f))
1674 {
1675 if (it->truncate_lines_p)
1676 it->last_visible_x -= it->truncation_pixel_width;
1677 else
1678 it->last_visible_x -= it->continuation_pixel_width;
1679 }
1680
1681 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
1682 it->current_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w) + w->vscroll;
1683 }
1684
1685 /* Leave room for a border glyph. */
1686 if (!FRAME_WINDOW_P (it->f)
1687 && !WINDOW_RIGHTMOST_P (it->w))
1688 it->last_visible_x -= 1;
1689
1690 it->last_visible_y = window_text_bottom_y (w);
1691
1692 /* For mode lines and alike, arrange for the first glyph having a
1693 left box line if the face specifies a box. */
1694 if (base_face_id != DEFAULT_FACE_ID)
1695 {
1696 struct face *face;
1697
1698 it->face_id = base_face_id;
1699
1700 /* If we have a boxed mode line, make the first character appear
1701 with a left box line. */
1702 face = FACE_FROM_ID (it->f, base_face_id);
1703 if (face->box != FACE_NO_BOX)
1704 it->start_of_box_run_p = 1;
1705 }
1706
1707 /* If a buffer position was specified, set the iterator there,
1708 getting overlays and face properties from that position. */
1709 if (charpos >= BUF_BEG (current_buffer))
1710 {
1711 it->end_charpos = ZV;
1712 it->face_id = -1;
1713 IT_CHARPOS (*it) = charpos;
1714
1715 /* Compute byte position if not specified. */
1716 if (bytepos < charpos)
1717 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
1718 else
1719 IT_BYTEPOS (*it) = bytepos;
1720
1721 /* Compute faces etc. */
1722 reseat (it, it->current.pos, 1);
1723 }
1724
1725 CHECK_IT (it);
1726 }
1727
1728
1729 /* Initialize IT for the display of window W with window start POS. */
1730
1731 void
1732 start_display (it, w, pos)
1733 struct it *it;
1734 struct window *w;
1735 struct text_pos pos;
1736 {
1737 struct glyph_row *row;
1738 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
1739
1740 row = w->desired_matrix->rows + first_vpos;
1741 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
1742
1743 if (!it->truncate_lines_p)
1744 {
1745 int start_at_line_beg_p;
1746 int first_y = it->current_y;
1747
1748 /* If window start is not at a line start, skip forward to POS to
1749 get the correct continuation lines width. */
1750 start_at_line_beg_p = (CHARPOS (pos) == BEGV
1751 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
1752 if (!start_at_line_beg_p)
1753 {
1754 reseat_at_previous_visible_line_start (it);
1755 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
1756
1757 /* If lines are continued, this line may end in the middle
1758 of a multi-glyph character (e.g. a control character
1759 displayed as \003, or in the middle of an overlay
1760 string). In this case move_it_to above will not have
1761 taken us to the start of the continuation line but to the
1762 end of the continued line. */
1763 if (it->current_x > 0)
1764 {
1765 if (it->current.dpvec_index >= 0
1766 || it->current.overlay_string_index >= 0)
1767 {
1768 set_iterator_to_next (it, 1);
1769 move_it_in_display_line_to (it, -1, -1, 0);
1770 }
1771
1772 it->continuation_lines_width += it->current_x;
1773 }
1774
1775 /* We're starting a new display line, not affected by the
1776 height of the continued line, so clear the appropriate
1777 fields in the iterator structure. */
1778 it->max_ascent = it->max_descent = 0;
1779 it->max_phys_ascent = it->max_phys_descent = 0;
1780
1781 it->current_y = first_y;
1782 it->vpos = 0;
1783 it->current_x = it->hpos = 0;
1784 }
1785 }
1786
1787 #if 0 /* Don't assert the following because start_display is sometimes
1788 called intentionally with a window start that is not at a
1789 line start. Please leave this code in as a comment. */
1790
1791 /* Window start should be on a line start, now. */
1792 xassert (it->continuation_lines_width
1793 || IT_CHARPOS (it) == BEGV
1794 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
1795 #endif /* 0 */
1796 }
1797
1798
1799 /* Return 1 if POS is a position in ellipses displayed for invisible
1800 text. W is the window we display, for text property lookup. */
1801
1802 static int
1803 in_ellipses_for_invisible_text_p (pos, w)
1804 struct display_pos *pos;
1805 struct window *w;
1806 {
1807 Lisp_Object prop, window;
1808 int ellipses_p = 0;
1809 int charpos = CHARPOS (pos->pos);
1810
1811 /* If POS specifies a position in a display vector, this might
1812 be for an ellipsis displayed for invisible text. We won't
1813 get the iterator set up for delivering that ellipsis unless
1814 we make sure that it gets aware of the invisible text. */
1815 if (pos->dpvec_index >= 0
1816 && pos->overlay_string_index < 0
1817 && CHARPOS (pos->string_pos) < 0
1818 && charpos > BEGV
1819 && (XSETWINDOW (window, w),
1820 prop = Fget_char_property (make_number (charpos),
1821 Qinvisible, window),
1822 !TEXT_PROP_MEANS_INVISIBLE (prop)))
1823 {
1824 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
1825 window);
1826 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
1827 }
1828
1829 return ellipses_p;
1830 }
1831
1832
1833 /* Initialize IT for stepping through current_buffer in window W,
1834 starting at position POS that includes overlay string and display
1835 vector/ control character translation position information. Value
1836 is zero if there are overlay strings with newlines at POS. */
1837
1838 static int
1839 init_from_display_pos (it, w, pos)
1840 struct it *it;
1841 struct window *w;
1842 struct display_pos *pos;
1843 {
1844 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
1845 int i, overlay_strings_with_newlines = 0;
1846
1847 /* If POS specifies a position in a display vector, this might
1848 be for an ellipsis displayed for invisible text. We won't
1849 get the iterator set up for delivering that ellipsis unless
1850 we make sure that it gets aware of the invisible text. */
1851 if (in_ellipses_for_invisible_text_p (pos, w))
1852 {
1853 --charpos;
1854 bytepos = 0;
1855 }
1856
1857 /* Keep in mind: the call to reseat in init_iterator skips invisible
1858 text, so we might end up at a position different from POS. This
1859 is only a problem when POS is a row start after a newline and an
1860 overlay starts there with an after-string, and the overlay has an
1861 invisible property. Since we don't skip invisible text in
1862 display_line and elsewhere immediately after consuming the
1863 newline before the row start, such a POS will not be in a string,
1864 but the call to init_iterator below will move us to the
1865 after-string. */
1866 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
1867
1868 for (i = 0; i < it->n_overlay_strings; ++i)
1869 {
1870 char *s = XSTRING (it->overlay_strings[i])->data;
1871 char *e = s + STRING_BYTES (XSTRING (it->overlay_strings[i]));
1872
1873 while (s < e && *s != '\n')
1874 ++s;
1875
1876 if (s < e)
1877 {
1878 overlay_strings_with_newlines = 1;
1879 break;
1880 }
1881 }
1882
1883 /* If position is within an overlay string, set up IT to the right
1884 overlay string. */
1885 if (pos->overlay_string_index >= 0)
1886 {
1887 int relative_index;
1888
1889 /* If the first overlay string happens to have a `display'
1890 property for an image, the iterator will be set up for that
1891 image, and we have to undo that setup first before we can
1892 correct the overlay string index. */
1893 if (it->method == next_element_from_image)
1894 pop_it (it);
1895
1896 /* We already have the first chunk of overlay strings in
1897 IT->overlay_strings. Load more until the one for
1898 pos->overlay_string_index is in IT->overlay_strings. */
1899 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
1900 {
1901 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
1902 it->current.overlay_string_index = 0;
1903 while (n--)
1904 {
1905 load_overlay_strings (it, 0);
1906 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
1907 }
1908 }
1909
1910 it->current.overlay_string_index = pos->overlay_string_index;
1911 relative_index = (it->current.overlay_string_index
1912 % OVERLAY_STRING_CHUNK_SIZE);
1913 it->string = it->overlay_strings[relative_index];
1914 xassert (STRINGP (it->string));
1915 it->current.string_pos = pos->string_pos;
1916 it->method = next_element_from_string;
1917 }
1918
1919 #if 0 /* This is bogus because POS not having an overlay string
1920 position does not mean it's after the string. Example: A
1921 line starting with a before-string and initialization of IT
1922 to the previous row's end position. */
1923 else if (it->current.overlay_string_index >= 0)
1924 {
1925 /* If POS says we're already after an overlay string ending at
1926 POS, make sure to pop the iterator because it will be in
1927 front of that overlay string. When POS is ZV, we've thereby
1928 also ``processed'' overlay strings at ZV. */
1929 while (it->sp)
1930 pop_it (it);
1931 it->current.overlay_string_index = -1;
1932 it->method = next_element_from_buffer;
1933 if (CHARPOS (pos->pos) == ZV)
1934 it->overlay_strings_at_end_processed_p = 1;
1935 }
1936 #endif /* 0 */
1937
1938 if (CHARPOS (pos->string_pos) >= 0)
1939 {
1940 /* Recorded position is not in an overlay string, but in another
1941 string. This can only be a string from a `display' property.
1942 IT should already be filled with that string. */
1943 it->current.string_pos = pos->string_pos;
1944 xassert (STRINGP (it->string));
1945 }
1946
1947 /* Restore position in display vector translations, control
1948 character translations or ellipses. */
1949 if (pos->dpvec_index >= 0)
1950 {
1951 if (it->dpvec == NULL)
1952 get_next_display_element (it);
1953 xassert (it->dpvec && it->current.dpvec_index == 0);
1954 it->current.dpvec_index = pos->dpvec_index;
1955 }
1956
1957 CHECK_IT (it);
1958 return !overlay_strings_with_newlines;
1959 }
1960
1961
1962 /* Initialize IT for stepping through current_buffer in window W
1963 starting at ROW->start. */
1964
1965 static void
1966 init_to_row_start (it, w, row)
1967 struct it *it;
1968 struct window *w;
1969 struct glyph_row *row;
1970 {
1971 init_from_display_pos (it, w, &row->start);
1972 it->continuation_lines_width = row->continuation_lines_width;
1973 CHECK_IT (it);
1974 }
1975
1976
1977 /* Initialize IT for stepping through current_buffer in window W
1978 starting in the line following ROW, i.e. starting at ROW->end.
1979 Value is zero if there are overlay strings with newlines at ROW's
1980 end position. */
1981
1982 static int
1983 init_to_row_end (it, w, row)
1984 struct it *it;
1985 struct window *w;
1986 struct glyph_row *row;
1987 {
1988 int success = 0;
1989
1990 if (init_from_display_pos (it, w, &row->end))
1991 {
1992 if (row->continued_p)
1993 it->continuation_lines_width
1994 = row->continuation_lines_width + row->pixel_width;
1995 CHECK_IT (it);
1996 success = 1;
1997 }
1998
1999 return success;
2000 }
2001
2002
2003
2004 \f
2005 /***********************************************************************
2006 Text properties
2007 ***********************************************************************/
2008
2009 /* Called when IT reaches IT->stop_charpos. Handle text property and
2010 overlay changes. Set IT->stop_charpos to the next position where
2011 to stop. */
2012
2013 static void
2014 handle_stop (it)
2015 struct it *it;
2016 {
2017 enum prop_handled handled;
2018 int handle_overlay_change_p = 1;
2019 struct props *p;
2020
2021 it->dpvec = NULL;
2022 it->current.dpvec_index = -1;
2023
2024 do
2025 {
2026 handled = HANDLED_NORMALLY;
2027
2028 /* Call text property handlers. */
2029 for (p = it_props; p->handler; ++p)
2030 {
2031 handled = p->handler (it);
2032
2033 if (handled == HANDLED_RECOMPUTE_PROPS)
2034 break;
2035 else if (handled == HANDLED_RETURN)
2036 return;
2037 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2038 handle_overlay_change_p = 0;
2039 }
2040
2041 if (handled != HANDLED_RECOMPUTE_PROPS)
2042 {
2043 /* Don't check for overlay strings below when set to deliver
2044 characters from a display vector. */
2045 if (it->method == next_element_from_display_vector)
2046 handle_overlay_change_p = 0;
2047
2048 /* Handle overlay changes. */
2049 if (handle_overlay_change_p)
2050 handled = handle_overlay_change (it);
2051
2052 /* Determine where to stop next. */
2053 if (handled == HANDLED_NORMALLY)
2054 compute_stop_pos (it);
2055 }
2056 }
2057 while (handled == HANDLED_RECOMPUTE_PROPS);
2058 }
2059
2060
2061 /* Compute IT->stop_charpos from text property and overlay change
2062 information for IT's current position. */
2063
2064 static void
2065 compute_stop_pos (it)
2066 struct it *it;
2067 {
2068 register INTERVAL iv, next_iv;
2069 Lisp_Object object, limit, position;
2070
2071 /* If nowhere else, stop at the end. */
2072 it->stop_charpos = it->end_charpos;
2073
2074 if (STRINGP (it->string))
2075 {
2076 /* Strings are usually short, so don't limit the search for
2077 properties. */
2078 object = it->string;
2079 limit = Qnil;
2080 position = make_number (IT_STRING_CHARPOS (*it));
2081 }
2082 else
2083 {
2084 int charpos;
2085
2086 /* If next overlay change is in front of the current stop pos
2087 (which is IT->end_charpos), stop there. Note: value of
2088 next_overlay_change is point-max if no overlay change
2089 follows. */
2090 charpos = next_overlay_change (IT_CHARPOS (*it));
2091 if (charpos < it->stop_charpos)
2092 it->stop_charpos = charpos;
2093
2094 /* If showing the region, we have to stop at the region
2095 start or end because the face might change there. */
2096 if (it->region_beg_charpos > 0)
2097 {
2098 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2099 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2100 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2101 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2102 }
2103
2104 /* Set up variables for computing the stop position from text
2105 property changes. */
2106 XSETBUFFER (object, current_buffer);
2107 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2108 position = make_number (IT_CHARPOS (*it));
2109
2110 }
2111
2112 /* Get the interval containing IT's position. Value is a null
2113 interval if there isn't such an interval. */
2114 iv = validate_interval_range (object, &position, &position, 0);
2115 if (!NULL_INTERVAL_P (iv))
2116 {
2117 Lisp_Object values_here[LAST_PROP_IDX];
2118 struct props *p;
2119
2120 /* Get properties here. */
2121 for (p = it_props; p->handler; ++p)
2122 values_here[p->idx] = textget (iv->plist, *p->name);
2123
2124 /* Look for an interval following iv that has different
2125 properties. */
2126 for (next_iv = next_interval (iv);
2127 (!NULL_INTERVAL_P (next_iv)
2128 && (NILP (limit)
2129 || XFASTINT (limit) > next_iv->position));
2130 next_iv = next_interval (next_iv))
2131 {
2132 for (p = it_props; p->handler; ++p)
2133 {
2134 Lisp_Object new_value;
2135
2136 new_value = textget (next_iv->plist, *p->name);
2137 if (!EQ (values_here[p->idx], new_value))
2138 break;
2139 }
2140
2141 if (p->handler)
2142 break;
2143 }
2144
2145 if (!NULL_INTERVAL_P (next_iv))
2146 {
2147 if (INTEGERP (limit)
2148 && next_iv->position >= XFASTINT (limit))
2149 /* No text property change up to limit. */
2150 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2151 else
2152 /* Text properties change in next_iv. */
2153 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2154 }
2155 }
2156
2157 xassert (STRINGP (it->string)
2158 || (it->stop_charpos >= BEGV
2159 && it->stop_charpos >= IT_CHARPOS (*it)));
2160 }
2161
2162
2163 /* Return the position of the next overlay change after POS in
2164 current_buffer. Value is point-max if no overlay change
2165 follows. This is like `next-overlay-change' but doesn't use
2166 xmalloc. */
2167
2168 static int
2169 next_overlay_change (pos)
2170 int pos;
2171 {
2172 int noverlays;
2173 int endpos;
2174 Lisp_Object *overlays;
2175 int len;
2176 int i;
2177
2178 /* Get all overlays at the given position. */
2179 len = 10;
2180 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2181 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2182 if (noverlays > len)
2183 {
2184 len = noverlays;
2185 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2186 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2187 }
2188
2189 /* If any of these overlays ends before endpos,
2190 use its ending point instead. */
2191 for (i = 0; i < noverlays; ++i)
2192 {
2193 Lisp_Object oend;
2194 int oendpos;
2195
2196 oend = OVERLAY_END (overlays[i]);
2197 oendpos = OVERLAY_POSITION (oend);
2198 endpos = min (endpos, oendpos);
2199 }
2200
2201 return endpos;
2202 }
2203
2204
2205 \f
2206 /***********************************************************************
2207 Fontification
2208 ***********************************************************************/
2209
2210 /* Handle changes in the `fontified' property of the current buffer by
2211 calling hook functions from Qfontification_functions to fontify
2212 regions of text. */
2213
2214 static enum prop_handled
2215 handle_fontified_prop (it)
2216 struct it *it;
2217 {
2218 Lisp_Object prop, pos;
2219 enum prop_handled handled = HANDLED_NORMALLY;
2220
2221 /* Get the value of the `fontified' property at IT's current buffer
2222 position. (The `fontified' property doesn't have a special
2223 meaning in strings.) If the value is nil, call functions from
2224 Qfontification_functions. */
2225 if (!STRINGP (it->string)
2226 && it->s == NULL
2227 && !NILP (Vfontification_functions)
2228 && !NILP (Vrun_hooks)
2229 && (pos = make_number (IT_CHARPOS (*it)),
2230 prop = Fget_char_property (pos, Qfontified, Qnil),
2231 NILP (prop)))
2232 {
2233 int count = BINDING_STACK_SIZE ();
2234 Lisp_Object val;
2235
2236 val = Vfontification_functions;
2237 specbind (Qfontification_functions, Qnil);
2238
2239 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2240 safe_call1 (val, pos);
2241 else
2242 {
2243 Lisp_Object globals, fn;
2244 struct gcpro gcpro1, gcpro2;
2245
2246 globals = Qnil;
2247 GCPRO2 (val, globals);
2248
2249 for (; CONSP (val); val = XCDR (val))
2250 {
2251 fn = XCAR (val);
2252
2253 if (EQ (fn, Qt))
2254 {
2255 /* A value of t indicates this hook has a local
2256 binding; it means to run the global binding too.
2257 In a global value, t should not occur. If it
2258 does, we must ignore it to avoid an endless
2259 loop. */
2260 for (globals = Fdefault_value (Qfontification_functions);
2261 CONSP (globals);
2262 globals = XCDR (globals))
2263 {
2264 fn = XCAR (globals);
2265 if (!EQ (fn, Qt))
2266 safe_call1 (fn, pos);
2267 }
2268 }
2269 else
2270 safe_call1 (fn, pos);
2271 }
2272
2273 UNGCPRO;
2274 }
2275
2276 unbind_to (count, Qnil);
2277
2278 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2279 something. This avoids an endless loop if they failed to
2280 fontify the text for which reason ever. */
2281 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2282 handled = HANDLED_RECOMPUTE_PROPS;
2283 }
2284
2285 return handled;
2286 }
2287
2288
2289 \f
2290 /***********************************************************************
2291 Faces
2292 ***********************************************************************/
2293
2294 /* Set up iterator IT from face properties at its current position.
2295 Called from handle_stop. */
2296
2297 static enum prop_handled
2298 handle_face_prop (it)
2299 struct it *it;
2300 {
2301 int new_face_id, next_stop;
2302
2303 if (!STRINGP (it->string))
2304 {
2305 new_face_id
2306 = face_at_buffer_position (it->w,
2307 IT_CHARPOS (*it),
2308 it->region_beg_charpos,
2309 it->region_end_charpos,
2310 &next_stop,
2311 (IT_CHARPOS (*it)
2312 + TEXT_PROP_DISTANCE_LIMIT),
2313 0);
2314
2315 /* Is this a start of a run of characters with box face?
2316 Caveat: this can be called for a freshly initialized
2317 iterator; face_id is -1 in this case. We know that the new
2318 face will not change until limit, i.e. if the new face has a
2319 box, all characters up to limit will have one. But, as
2320 usual, we don't know whether limit is really the end. */
2321 if (new_face_id != it->face_id)
2322 {
2323 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2324
2325 /* If new face has a box but old face has not, this is
2326 the start of a run of characters with box, i.e. it has
2327 a shadow on the left side. The value of face_id of the
2328 iterator will be -1 if this is the initial call that gets
2329 the face. In this case, we have to look in front of IT's
2330 position and see whether there is a face != new_face_id. */
2331 it->start_of_box_run_p
2332 = (new_face->box != FACE_NO_BOX
2333 && (it->face_id >= 0
2334 || IT_CHARPOS (*it) == BEG
2335 || new_face_id != face_before_it_pos (it)));
2336 it->face_box_p = new_face->box != FACE_NO_BOX;
2337 }
2338 }
2339 else
2340 {
2341 int base_face_id, bufpos;
2342
2343 if (it->current.overlay_string_index >= 0)
2344 bufpos = IT_CHARPOS (*it);
2345 else
2346 bufpos = 0;
2347
2348 /* For strings from a buffer, i.e. overlay strings or strings
2349 from a `display' property, use the face at IT's current
2350 buffer position as the base face to merge with, so that
2351 overlay strings appear in the same face as surrounding
2352 text, unless they specify their own faces. */
2353 base_face_id = underlying_face_id (it);
2354
2355 new_face_id = face_at_string_position (it->w,
2356 it->string,
2357 IT_STRING_CHARPOS (*it),
2358 bufpos,
2359 it->region_beg_charpos,
2360 it->region_end_charpos,
2361 &next_stop,
2362 base_face_id, 0);
2363
2364 #if 0 /* This shouldn't be neccessary. Let's check it. */
2365 /* If IT is used to display a mode line we would really like to
2366 use the mode line face instead of the frame's default face. */
2367 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2368 && new_face_id == DEFAULT_FACE_ID)
2369 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
2370 #endif
2371
2372 /* Is this a start of a run of characters with box? Caveat:
2373 this can be called for a freshly allocated iterator; face_id
2374 is -1 is this case. We know that the new face will not
2375 change until the next check pos, i.e. if the new face has a
2376 box, all characters up to that position will have a
2377 box. But, as usual, we don't know whether that position
2378 is really the end. */
2379 if (new_face_id != it->face_id)
2380 {
2381 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2382 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2383
2384 /* If new face has a box but old face hasn't, this is the
2385 start of a run of characters with box, i.e. it has a
2386 shadow on the left side. */
2387 it->start_of_box_run_p
2388 = new_face->box && (old_face == NULL || !old_face->box);
2389 it->face_box_p = new_face->box != FACE_NO_BOX;
2390 }
2391 }
2392
2393 it->face_id = new_face_id;
2394 return HANDLED_NORMALLY;
2395 }
2396
2397
2398 /* Return the ID of the face ``underlying'' IT's current position,
2399 which is in a string. If the iterator is associated with a
2400 buffer, return the face at IT's current buffer position.
2401 Otherwise, use the iterator's base_face_id. */
2402
2403 static int
2404 underlying_face_id (it)
2405 struct it *it;
2406 {
2407 int face_id = it->base_face_id, i;
2408
2409 xassert (STRINGP (it->string));
2410
2411 for (i = it->sp - 1; i >= 0; --i)
2412 if (NILP (it->stack[i].string))
2413 face_id = it->stack[i].face_id;
2414
2415 return face_id;
2416 }
2417
2418
2419 /* Compute the face one character before or after the current position
2420 of IT. BEFORE_P non-zero means get the face in front of IT's
2421 position. Value is the id of the face. */
2422
2423 static int
2424 face_before_or_after_it_pos (it, before_p)
2425 struct it *it;
2426 int before_p;
2427 {
2428 int face_id, limit;
2429 int next_check_charpos;
2430 struct text_pos pos;
2431
2432 xassert (it->s == NULL);
2433
2434 if (STRINGP (it->string))
2435 {
2436 int bufpos, base_face_id;
2437
2438 /* No face change past the end of the string (for the case
2439 we are padding with spaces). No face change before the
2440 string start. */
2441 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size
2442 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2443 return it->face_id;
2444
2445 /* Set pos to the position before or after IT's current position. */
2446 if (before_p)
2447 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2448 else
2449 /* For composition, we must check the character after the
2450 composition. */
2451 pos = (it->what == IT_COMPOSITION
2452 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
2453 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
2454
2455 if (it->current.overlay_string_index >= 0)
2456 bufpos = IT_CHARPOS (*it);
2457 else
2458 bufpos = 0;
2459
2460 base_face_id = underlying_face_id (it);
2461
2462 /* Get the face for ASCII, or unibyte. */
2463 face_id = face_at_string_position (it->w,
2464 it->string,
2465 CHARPOS (pos),
2466 bufpos,
2467 it->region_beg_charpos,
2468 it->region_end_charpos,
2469 &next_check_charpos,
2470 base_face_id, 0);
2471
2472 /* Correct the face for charsets different from ASCII. Do it
2473 for the multibyte case only. The face returned above is
2474 suitable for unibyte text if IT->string is unibyte. */
2475 if (STRING_MULTIBYTE (it->string))
2476 {
2477 unsigned char *p = XSTRING (it->string)->data + BYTEPOS (pos);
2478 int rest = STRING_BYTES (XSTRING (it->string)) - BYTEPOS (pos);
2479 int c, len;
2480 struct face *face = FACE_FROM_ID (it->f, face_id);
2481
2482 c = string_char_and_length (p, rest, &len);
2483 face_id = FACE_FOR_CHAR (it->f, face, c);
2484 }
2485 }
2486 else
2487 {
2488 if ((IT_CHARPOS (*it) >= ZV && !before_p)
2489 || (IT_CHARPOS (*it) <= BEGV && before_p))
2490 return it->face_id;
2491
2492 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
2493 pos = it->current.pos;
2494
2495 if (before_p)
2496 DEC_TEXT_POS (pos, it->multibyte_p);
2497 else
2498 {
2499 if (it->what == IT_COMPOSITION)
2500 /* For composition, we must check the position after the
2501 composition. */
2502 pos.charpos += it->cmp_len, pos.bytepos += it->len;
2503 else
2504 INC_TEXT_POS (pos, it->multibyte_p);
2505 }
2506
2507 /* Determine face for CHARSET_ASCII, or unibyte. */
2508 face_id = face_at_buffer_position (it->w,
2509 CHARPOS (pos),
2510 it->region_beg_charpos,
2511 it->region_end_charpos,
2512 &next_check_charpos,
2513 limit, 0);
2514
2515 /* Correct the face for charsets different from ASCII. Do it
2516 for the multibyte case only. The face returned above is
2517 suitable for unibyte text if current_buffer is unibyte. */
2518 if (it->multibyte_p)
2519 {
2520 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
2521 struct face *face = FACE_FROM_ID (it->f, face_id);
2522 face_id = FACE_FOR_CHAR (it->f, face, c);
2523 }
2524 }
2525
2526 return face_id;
2527 }
2528
2529
2530 \f
2531 /***********************************************************************
2532 Invisible text
2533 ***********************************************************************/
2534
2535 /* Set up iterator IT from invisible properties at its current
2536 position. Called from handle_stop. */
2537
2538 static enum prop_handled
2539 handle_invisible_prop (it)
2540 struct it *it;
2541 {
2542 enum prop_handled handled = HANDLED_NORMALLY;
2543
2544 if (STRINGP (it->string))
2545 {
2546 extern Lisp_Object Qinvisible;
2547 Lisp_Object prop, end_charpos, limit, charpos;
2548
2549 /* Get the value of the invisible text property at the
2550 current position. Value will be nil if there is no such
2551 property. */
2552 charpos = make_number (IT_STRING_CHARPOS (*it));
2553 prop = Fget_text_property (charpos, Qinvisible, it->string);
2554
2555 if (!NILP (prop)
2556 && IT_STRING_CHARPOS (*it) < it->end_charpos)
2557 {
2558 handled = HANDLED_RECOMPUTE_PROPS;
2559
2560 /* Get the position at which the next change of the
2561 invisible text property can be found in IT->string.
2562 Value will be nil if the property value is the same for
2563 all the rest of IT->string. */
2564 XSETINT (limit, XSTRING (it->string)->size);
2565 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2566 it->string, limit);
2567
2568 /* Text at current position is invisible. The next
2569 change in the property is at position end_charpos.
2570 Move IT's current position to that position. */
2571 if (INTEGERP (end_charpos)
2572 && XFASTINT (end_charpos) < XFASTINT (limit))
2573 {
2574 struct text_pos old;
2575 old = it->current.string_pos;
2576 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
2577 compute_string_pos (&it->current.string_pos, old, it->string);
2578 }
2579 else
2580 {
2581 /* The rest of the string is invisible. If this is an
2582 overlay string, proceed with the next overlay string
2583 or whatever comes and return a character from there. */
2584 if (it->current.overlay_string_index >= 0)
2585 {
2586 next_overlay_string (it);
2587 /* Don't check for overlay strings when we just
2588 finished processing them. */
2589 handled = HANDLED_OVERLAY_STRING_CONSUMED;
2590 }
2591 else
2592 {
2593 struct Lisp_String *s = XSTRING (it->string);
2594 IT_STRING_CHARPOS (*it) = s->size;
2595 IT_STRING_BYTEPOS (*it) = STRING_BYTES (s);
2596 }
2597 }
2598 }
2599 }
2600 else
2601 {
2602 int invis_p, newpos, next_stop, start_charpos;
2603 Lisp_Object pos, prop, overlay;
2604
2605 /* First of all, is there invisible text at this position? */
2606 start_charpos = IT_CHARPOS (*it);
2607 pos = make_number (IT_CHARPOS (*it));
2608 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
2609 &overlay);
2610 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
2611
2612 /* If we are on invisible text, skip over it. */
2613 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
2614 {
2615 /* Record whether we have to display an ellipsis for the
2616 invisible text. */
2617 int display_ellipsis_p = invis_p == 2;
2618
2619 handled = HANDLED_RECOMPUTE_PROPS;
2620
2621 /* Loop skipping over invisible text. The loop is left at
2622 ZV or with IT on the first char being visible again. */
2623 do
2624 {
2625 /* Try to skip some invisible text. Return value is the
2626 position reached which can be equal to IT's position
2627 if there is nothing invisible here. This skips both
2628 over invisible text properties and overlays with
2629 invisible property. */
2630 newpos = skip_invisible (IT_CHARPOS (*it),
2631 &next_stop, ZV, it->window);
2632
2633 /* If we skipped nothing at all we weren't at invisible
2634 text in the first place. If everything to the end of
2635 the buffer was skipped, end the loop. */
2636 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
2637 invis_p = 0;
2638 else
2639 {
2640 /* We skipped some characters but not necessarily
2641 all there are. Check if we ended up on visible
2642 text. Fget_char_property returns the property of
2643 the char before the given position, i.e. if we
2644 get invis_p = 0, this means that the char at
2645 newpos is visible. */
2646 pos = make_number (newpos);
2647 prop = Fget_char_property (pos, Qinvisible, it->window);
2648 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
2649 }
2650
2651 /* If we ended up on invisible text, proceed to
2652 skip starting with next_stop. */
2653 if (invis_p)
2654 IT_CHARPOS (*it) = next_stop;
2655 }
2656 while (invis_p);
2657
2658 /* The position newpos is now either ZV or on visible text. */
2659 IT_CHARPOS (*it) = newpos;
2660 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2661
2662 /* If there are before-strings at the start of invisible
2663 text, and the text is invisible because of a text
2664 property, arrange to show before-strings because 20.x did
2665 it that way. (If the text is invisible because of an
2666 overlay property instead of a text property, this is
2667 already handled in the overlay code.) */
2668 if (NILP (overlay)
2669 && get_overlay_strings (it, start_charpos))
2670 {
2671 handled = HANDLED_RECOMPUTE_PROPS;
2672 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
2673 }
2674 else if (display_ellipsis_p)
2675 setup_for_ellipsis (it);
2676 }
2677 }
2678
2679 return handled;
2680 }
2681
2682
2683 /* Make iterator IT return `...' next. */
2684
2685 static void
2686 setup_for_ellipsis (it)
2687 struct it *it;
2688 {
2689 if (it->dp
2690 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
2691 {
2692 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
2693 it->dpvec = v->contents;
2694 it->dpend = v->contents + v->size;
2695 }
2696 else
2697 {
2698 /* Default `...'. */
2699 it->dpvec = default_invis_vector;
2700 it->dpend = default_invis_vector + 3;
2701 }
2702
2703 /* The ellipsis display does not replace the display of the
2704 character at the new position. Indicate this by setting
2705 IT->dpvec_char_len to zero. */
2706 it->dpvec_char_len = 0;
2707
2708 it->current.dpvec_index = 0;
2709 it->method = next_element_from_display_vector;
2710 }
2711
2712
2713 \f
2714 /***********************************************************************
2715 'display' property
2716 ***********************************************************************/
2717
2718 /* Set up iterator IT from `display' property at its current position.
2719 Called from handle_stop. */
2720
2721 static enum prop_handled
2722 handle_display_prop (it)
2723 struct it *it;
2724 {
2725 Lisp_Object prop, object;
2726 struct text_pos *position;
2727 int display_replaced_p = 0;
2728
2729 if (STRINGP (it->string))
2730 {
2731 object = it->string;
2732 position = &it->current.string_pos;
2733 }
2734 else
2735 {
2736 object = it->w->buffer;
2737 position = &it->current.pos;
2738 }
2739
2740 /* Reset those iterator values set from display property values. */
2741 it->font_height = Qnil;
2742 it->space_width = Qnil;
2743 it->voffset = 0;
2744
2745 /* We don't support recursive `display' properties, i.e. string
2746 values that have a string `display' property, that have a string
2747 `display' property etc. */
2748 if (!it->string_from_display_prop_p)
2749 it->area = TEXT_AREA;
2750
2751 prop = Fget_char_property (make_number (position->charpos),
2752 Qdisplay, object);
2753 if (NILP (prop))
2754 return HANDLED_NORMALLY;
2755
2756 if (CONSP (prop)
2757 /* Simple properties. */
2758 && !EQ (XCAR (prop), Qimage)
2759 && !EQ (XCAR (prop), Qspace)
2760 && !EQ (XCAR (prop), Qwhen)
2761 && !EQ (XCAR (prop), Qspace_width)
2762 && !EQ (XCAR (prop), Qheight)
2763 && !EQ (XCAR (prop), Qraise)
2764 /* Marginal area specifications. */
2765 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
2766 && !NILP (XCAR (prop)))
2767 {
2768 for (; CONSP (prop); prop = XCDR (prop))
2769 {
2770 if (handle_single_display_prop (it, XCAR (prop), object,
2771 position, display_replaced_p))
2772 display_replaced_p = 1;
2773 }
2774 }
2775 else if (VECTORP (prop))
2776 {
2777 int i;
2778 for (i = 0; i < ASIZE (prop); ++i)
2779 if (handle_single_display_prop (it, AREF (prop, i), object,
2780 position, display_replaced_p))
2781 display_replaced_p = 1;
2782 }
2783 else
2784 {
2785 if (handle_single_display_prop (it, prop, object, position, 0))
2786 display_replaced_p = 1;
2787 }
2788
2789 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
2790 }
2791
2792
2793 /* Value is the position of the end of the `display' property starting
2794 at START_POS in OBJECT. */
2795
2796 static struct text_pos
2797 display_prop_end (it, object, start_pos)
2798 struct it *it;
2799 Lisp_Object object;
2800 struct text_pos start_pos;
2801 {
2802 Lisp_Object end;
2803 struct text_pos end_pos;
2804
2805 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
2806 Qdisplay, object, Qnil);
2807 CHARPOS (end_pos) = XFASTINT (end);
2808 if (STRINGP (object))
2809 compute_string_pos (&end_pos, start_pos, it->string);
2810 else
2811 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
2812
2813 return end_pos;
2814 }
2815
2816
2817 /* Set up IT from a single `display' sub-property value PROP. OBJECT
2818 is the object in which the `display' property was found. *POSITION
2819 is the position at which it was found. DISPLAY_REPLACED_P non-zero
2820 means that we previously saw a display sub-property which already
2821 replaced text display with something else, for example an image;
2822 ignore such properties after the first one has been processed.
2823
2824 If PROP is a `space' or `image' sub-property, set *POSITION to the
2825 end position of the `display' property.
2826
2827 Value is non-zero if something was found which replaces the display
2828 of buffer or string text. */
2829
2830 static int
2831 handle_single_display_prop (it, prop, object, position,
2832 display_replaced_before_p)
2833 struct it *it;
2834 Lisp_Object prop;
2835 Lisp_Object object;
2836 struct text_pos *position;
2837 int display_replaced_before_p;
2838 {
2839 Lisp_Object value;
2840 int replaces_text_display_p = 0;
2841 Lisp_Object form;
2842
2843 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
2844 evaluated. If the result is nil, VALUE is ignored. */
2845 form = Qt;
2846 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2847 {
2848 prop = XCDR (prop);
2849 if (!CONSP (prop))
2850 return 0;
2851 form = XCAR (prop);
2852 prop = XCDR (prop);
2853 }
2854
2855 if (!NILP (form) && !EQ (form, Qt))
2856 {
2857 int count = BINDING_STACK_SIZE ();
2858 struct gcpro gcpro1;
2859
2860 /* Bind `object' to the object having the `display' property, a
2861 buffer or string. Bind `position' to the position in the
2862 object where the property was found, and `buffer-position'
2863 to the current position in the buffer. */
2864 specbind (Qobject, object);
2865 specbind (Qposition, make_number (CHARPOS (*position)));
2866 specbind (Qbuffer_position,
2867 make_number (STRINGP (object)
2868 ? IT_CHARPOS (*it) : CHARPOS (*position)));
2869 GCPRO1 (form);
2870 form = safe_eval (form);
2871 UNGCPRO;
2872 unbind_to (count, Qnil);
2873 }
2874
2875 if (NILP (form))
2876 return 0;
2877
2878 if (CONSP (prop)
2879 && EQ (XCAR (prop), Qheight)
2880 && CONSP (XCDR (prop)))
2881 {
2882 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2883 return 0;
2884
2885 /* `(height HEIGHT)'. */
2886 it->font_height = XCAR (XCDR (prop));
2887 if (!NILP (it->font_height))
2888 {
2889 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2890 int new_height = -1;
2891
2892 if (CONSP (it->font_height)
2893 && (EQ (XCAR (it->font_height), Qplus)
2894 || EQ (XCAR (it->font_height), Qminus))
2895 && CONSP (XCDR (it->font_height))
2896 && INTEGERP (XCAR (XCDR (it->font_height))))
2897 {
2898 /* `(+ N)' or `(- N)' where N is an integer. */
2899 int steps = XINT (XCAR (XCDR (it->font_height)));
2900 if (EQ (XCAR (it->font_height), Qplus))
2901 steps = - steps;
2902 it->face_id = smaller_face (it->f, it->face_id, steps);
2903 }
2904 else if (FUNCTIONP (it->font_height))
2905 {
2906 /* Call function with current height as argument.
2907 Value is the new height. */
2908 Lisp_Object height;
2909 height = safe_call1 (it->font_height,
2910 face->lface[LFACE_HEIGHT_INDEX]);
2911 if (NUMBERP (height))
2912 new_height = XFLOATINT (height);
2913 }
2914 else if (NUMBERP (it->font_height))
2915 {
2916 /* Value is a multiple of the canonical char height. */
2917 struct face *face;
2918
2919 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
2920 new_height = (XFLOATINT (it->font_height)
2921 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
2922 }
2923 else
2924 {
2925 /* Evaluate IT->font_height with `height' bound to the
2926 current specified height to get the new height. */
2927 Lisp_Object value;
2928 int count = BINDING_STACK_SIZE ();
2929
2930 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
2931 value = safe_eval (it->font_height);
2932 unbind_to (count, Qnil);
2933
2934 if (NUMBERP (value))
2935 new_height = XFLOATINT (value);
2936 }
2937
2938 if (new_height > 0)
2939 it->face_id = face_with_height (it->f, it->face_id, new_height);
2940 }
2941 }
2942 else if (CONSP (prop)
2943 && EQ (XCAR (prop), Qspace_width)
2944 && CONSP (XCDR (prop)))
2945 {
2946 /* `(space_width WIDTH)'. */
2947 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2948 return 0;
2949
2950 value = XCAR (XCDR (prop));
2951 if (NUMBERP (value) && XFLOATINT (value) > 0)
2952 it->space_width = value;
2953 }
2954 else if (CONSP (prop)
2955 && EQ (XCAR (prop), Qraise)
2956 && CONSP (XCDR (prop)))
2957 {
2958 /* `(raise FACTOR)'. */
2959 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2960 return 0;
2961
2962 #ifdef HAVE_WINDOW_SYSTEM
2963 value = XCAR (XCDR (prop));
2964 if (NUMBERP (value))
2965 {
2966 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2967 it->voffset = - (XFLOATINT (value)
2968 * (FONT_HEIGHT (face->font)));
2969 }
2970 #endif /* HAVE_WINDOW_SYSTEM */
2971 }
2972 else if (!it->string_from_display_prop_p)
2973 {
2974 /* `((margin left-margin) VALUE)' or `((margin right-margin)
2975 VALUE) or `((margin nil) VALUE)' or VALUE. */
2976 Lisp_Object location, value;
2977 struct text_pos start_pos;
2978 int valid_p;
2979
2980 /* Characters having this form of property are not displayed, so
2981 we have to find the end of the property. */
2982 start_pos = *position;
2983 *position = display_prop_end (it, object, start_pos);
2984 value = Qnil;
2985
2986 /* Let's stop at the new position and assume that all
2987 text properties change there. */
2988 it->stop_charpos = position->charpos;
2989
2990 location = Qunbound;
2991 if (CONSP (prop) && CONSP (XCAR (prop)))
2992 {
2993 Lisp_Object tem;
2994
2995 value = XCDR (prop);
2996 if (CONSP (value))
2997 value = XCAR (value);
2998
2999 tem = XCAR (prop);
3000 if (EQ (XCAR (tem), Qmargin)
3001 && (tem = XCDR (tem),
3002 tem = CONSP (tem) ? XCAR (tem) : Qnil,
3003 (NILP (tem)
3004 || EQ (tem, Qleft_margin)
3005 || EQ (tem, Qright_margin))))
3006 location = tem;
3007 }
3008
3009 if (EQ (location, Qunbound))
3010 {
3011 location = Qnil;
3012 value = prop;
3013 }
3014
3015 #ifdef HAVE_WINDOW_SYSTEM
3016 if (FRAME_TERMCAP_P (it->f))
3017 valid_p = STRINGP (value);
3018 else
3019 valid_p = (STRINGP (value)
3020 || (CONSP (value) && EQ (XCAR (value), Qspace))
3021 || valid_image_p (value));
3022 #else /* not HAVE_WINDOW_SYSTEM */
3023 valid_p = STRINGP (value);
3024 #endif /* not HAVE_WINDOW_SYSTEM */
3025
3026 if ((EQ (location, Qleft_margin)
3027 || EQ (location, Qright_margin)
3028 || NILP (location))
3029 && valid_p
3030 && !display_replaced_before_p)
3031 {
3032 replaces_text_display_p = 1;
3033
3034 /* Save current settings of IT so that we can restore them
3035 when we are finished with the glyph property value. */
3036 push_it (it);
3037
3038 if (NILP (location))
3039 it->area = TEXT_AREA;
3040 else if (EQ (location, Qleft_margin))
3041 it->area = LEFT_MARGIN_AREA;
3042 else
3043 it->area = RIGHT_MARGIN_AREA;
3044
3045 if (STRINGP (value))
3046 {
3047 it->string = value;
3048 it->multibyte_p = STRING_MULTIBYTE (it->string);
3049 it->current.overlay_string_index = -1;
3050 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3051 it->end_charpos = it->string_nchars
3052 = XSTRING (it->string)->size;
3053 it->method = next_element_from_string;
3054 it->stop_charpos = 0;
3055 it->string_from_display_prop_p = 1;
3056 /* Say that we haven't consumed the characters with
3057 `display' property yet. The call to pop_it in
3058 set_iterator_to_next will clean this up. */
3059 *position = start_pos;
3060 }
3061 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3062 {
3063 it->method = next_element_from_stretch;
3064 it->object = value;
3065 it->current.pos = it->position = start_pos;
3066 }
3067 #ifdef HAVE_WINDOW_SYSTEM
3068 else
3069 {
3070 it->what = IT_IMAGE;
3071 it->image_id = lookup_image (it->f, value);
3072 it->position = start_pos;
3073 it->object = NILP (object) ? it->w->buffer : object;
3074 it->method = next_element_from_image;
3075
3076 /* Say that we haven't consumed the characters with
3077 `display' property yet. The call to pop_it in
3078 set_iterator_to_next will clean this up. */
3079 *position = start_pos;
3080 }
3081 #endif /* HAVE_WINDOW_SYSTEM */
3082 }
3083 else
3084 /* Invalid property or property not supported. Restore
3085 the position to what it was before. */
3086 *position = start_pos;
3087 }
3088
3089 return replaces_text_display_p;
3090 }
3091
3092
3093 /* Check if PROP is a display sub-property value whose text should be
3094 treated as intangible. */
3095
3096 static int
3097 single_display_prop_intangible_p (prop)
3098 Lisp_Object prop;
3099 {
3100 /* Skip over `when FORM'. */
3101 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3102 {
3103 prop = XCDR (prop);
3104 if (!CONSP (prop))
3105 return 0;
3106 prop = XCDR (prop);
3107 }
3108
3109 if (!CONSP (prop))
3110 return 0;
3111
3112 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3113 we don't need to treat text as intangible. */
3114 if (EQ (XCAR (prop), Qmargin))
3115 {
3116 prop = XCDR (prop);
3117 if (!CONSP (prop))
3118 return 0;
3119
3120 prop = XCDR (prop);
3121 if (!CONSP (prop)
3122 || EQ (XCAR (prop), Qleft_margin)
3123 || EQ (XCAR (prop), Qright_margin))
3124 return 0;
3125 }
3126
3127 return CONSP (prop) && EQ (XCAR (prop), Qimage);
3128 }
3129
3130
3131 /* Check if PROP is a display property value whose text should be
3132 treated as intangible. */
3133
3134 int
3135 display_prop_intangible_p (prop)
3136 Lisp_Object prop;
3137 {
3138 if (CONSP (prop)
3139 && CONSP (XCAR (prop))
3140 && !EQ (Qmargin, XCAR (XCAR (prop))))
3141 {
3142 /* A list of sub-properties. */
3143 while (CONSP (prop))
3144 {
3145 if (single_display_prop_intangible_p (XCAR (prop)))
3146 return 1;
3147 prop = XCDR (prop);
3148 }
3149 }
3150 else if (VECTORP (prop))
3151 {
3152 /* A vector of sub-properties. */
3153 int i;
3154 for (i = 0; i < ASIZE (prop); ++i)
3155 if (single_display_prop_intangible_p (AREF (prop, i)))
3156 return 1;
3157 }
3158 else
3159 return single_display_prop_intangible_p (prop);
3160
3161 return 0;
3162 }
3163
3164
3165 /* Return 1 if PROP is a display sub-property value containing STRING. */
3166
3167 static int
3168 single_display_prop_string_p (prop, string)
3169 Lisp_Object prop, string;
3170 {
3171 if (EQ (string, prop))
3172 return 1;
3173
3174 /* Skip over `when FORM'. */
3175 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3176 {
3177 prop = XCDR (prop);
3178 if (!CONSP (prop))
3179 return 0;
3180 prop = XCDR (prop);
3181 }
3182
3183 if (CONSP (prop))
3184 /* Skip over `margin LOCATION'. */
3185 if (EQ (XCAR (prop), Qmargin))
3186 {
3187 prop = XCDR (prop);
3188 if (!CONSP (prop))
3189 return 0;
3190
3191 prop = XCDR (prop);
3192 if (!CONSP (prop))
3193 return 0;
3194 }
3195
3196 return CONSP (prop) && EQ (XCAR (prop), string);
3197 }
3198
3199
3200 /* Return 1 if STRING appears in the `display' property PROP. */
3201
3202 static int
3203 display_prop_string_p (prop, string)
3204 Lisp_Object prop, string;
3205 {
3206 if (CONSP (prop)
3207 && CONSP (XCAR (prop))
3208 && !EQ (Qmargin, XCAR (XCAR (prop))))
3209 {
3210 /* A list of sub-properties. */
3211 while (CONSP (prop))
3212 {
3213 if (single_display_prop_string_p (XCAR (prop), string))
3214 return 1;
3215 prop = XCDR (prop);
3216 }
3217 }
3218 else if (VECTORP (prop))
3219 {
3220 /* A vector of sub-properties. */
3221 int i;
3222 for (i = 0; i < ASIZE (prop); ++i)
3223 if (single_display_prop_string_p (AREF (prop, i), string))
3224 return 1;
3225 }
3226 else
3227 return single_display_prop_string_p (prop, string);
3228
3229 return 0;
3230 }
3231
3232
3233 /* Determine from which buffer position in W's buffer STRING comes
3234 from. AROUND_CHARPOS is an approximate position where it could
3235 be from. Value is the buffer position or 0 if it couldn't be
3236 determined.
3237
3238 W's buffer must be current.
3239
3240 This function is necessary because we don't record buffer positions
3241 in glyphs generated from strings (to keep struct glyph small).
3242 This function may only use code that doesn't eval because it is
3243 called asynchronously from note_mouse_highlight. */
3244
3245 int
3246 string_buffer_position (w, string, around_charpos)
3247 struct window *w;
3248 Lisp_Object string;
3249 int around_charpos;
3250 {
3251 Lisp_Object limit, prop, pos;
3252 const int MAX_DISTANCE = 1000;
3253 int found = 0;
3254
3255 pos = make_number (around_charpos);
3256 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
3257 while (!found && !EQ (pos, limit))
3258 {
3259 prop = Fget_char_property (pos, Qdisplay, Qnil);
3260 if (!NILP (prop) && display_prop_string_p (prop, string))
3261 found = 1;
3262 else
3263 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
3264 }
3265
3266 if (!found)
3267 {
3268 pos = make_number (around_charpos);
3269 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
3270 while (!found && !EQ (pos, limit))
3271 {
3272 prop = Fget_char_property (pos, Qdisplay, Qnil);
3273 if (!NILP (prop) && display_prop_string_p (prop, string))
3274 found = 1;
3275 else
3276 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
3277 limit);
3278 }
3279 }
3280
3281 return found ? XINT (pos) : 0;
3282 }
3283
3284
3285 \f
3286 /***********************************************************************
3287 `composition' property
3288 ***********************************************************************/
3289
3290 static enum prop_handled
3291 handle_auto_composed_prop (it)
3292 struct it *it;
3293 {
3294 enum prop_handled handled = HANDLED_NORMALLY;
3295
3296 if (! NILP (Vauto_composition_function))
3297 {
3298 Lisp_Object val;
3299 int pos;
3300
3301 if (STRINGP (it->string))
3302 pos = IT_STRING_CHARPOS (*it);
3303 else
3304 pos = IT_CHARPOS (*it);
3305
3306 val =Fget_char_property (make_number (pos), Qauto_composed, it->string);
3307 if (NILP (val))
3308 {
3309 int count = BINDING_STACK_SIZE ();
3310 Lisp_Object args[3];
3311
3312 args[0] = Vauto_composition_function;
3313 specbind (Qauto_composition_function, Qnil);
3314 args[1] = make_number (pos);
3315 args[2] = it->string;
3316 safe_call (3, args);
3317 unbind_to (count, Qnil);
3318
3319 val = Fget_char_property (args[1], Qauto_composed, it->string);
3320 if (! NILP (val))
3321 handled = HANDLED_RECOMPUTE_PROPS;
3322 }
3323 }
3324
3325 return handled;
3326 }
3327
3328 /* Set up iterator IT from `composition' property at its current
3329 position. Called from handle_stop. */
3330
3331 static enum prop_handled
3332 handle_composition_prop (it)
3333 struct it *it;
3334 {
3335 Lisp_Object prop, string;
3336 int pos, pos_byte, end;
3337 enum prop_handled handled = HANDLED_NORMALLY;
3338
3339 if (STRINGP (it->string))
3340 {
3341 pos = IT_STRING_CHARPOS (*it);
3342 pos_byte = IT_STRING_BYTEPOS (*it);
3343 string = it->string;
3344 }
3345 else
3346 {
3347 pos = IT_CHARPOS (*it);
3348 pos_byte = IT_BYTEPOS (*it);
3349 string = Qnil;
3350 }
3351
3352 /* If there's a valid composition and point is not inside of the
3353 composition (in the case that the composition is from the current
3354 buffer), draw a glyph composed from the composition components. */
3355 if (find_composition (pos, -1, &pos, &end, &prop, string)
3356 && COMPOSITION_VALID_P (pos, end, prop)
3357 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
3358 {
3359 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
3360
3361 if (id >= 0)
3362 {
3363 it->method = next_element_from_composition;
3364 it->cmp_id = id;
3365 it->cmp_len = COMPOSITION_LENGTH (prop);
3366 /* For a terminal, draw only the first character of the
3367 components. */
3368 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
3369 it->len = (STRINGP (it->string)
3370 ? string_char_to_byte (it->string, end)
3371 : CHAR_TO_BYTE (end)) - pos_byte;
3372 it->stop_charpos = end;
3373 handled = HANDLED_RETURN;
3374 }
3375 }
3376
3377 return handled;
3378 }
3379
3380
3381 \f
3382 /***********************************************************************
3383 Overlay strings
3384 ***********************************************************************/
3385
3386 /* The following structure is used to record overlay strings for
3387 later sorting in load_overlay_strings. */
3388
3389 struct overlay_entry
3390 {
3391 Lisp_Object overlay;
3392 Lisp_Object string;
3393 int priority;
3394 int after_string_p;
3395 };
3396
3397
3398 /* Set up iterator IT from overlay strings at its current position.
3399 Called from handle_stop. */
3400
3401 static enum prop_handled
3402 handle_overlay_change (it)
3403 struct it *it;
3404 {
3405 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
3406 return HANDLED_RECOMPUTE_PROPS;
3407 else
3408 return HANDLED_NORMALLY;
3409 }
3410
3411
3412 /* Set up the next overlay string for delivery by IT, if there is an
3413 overlay string to deliver. Called by set_iterator_to_next when the
3414 end of the current overlay string is reached. If there are more
3415 overlay strings to display, IT->string and
3416 IT->current.overlay_string_index are set appropriately here.
3417 Otherwise IT->string is set to nil. */
3418
3419 static void
3420 next_overlay_string (it)
3421 struct it *it;
3422 {
3423 ++it->current.overlay_string_index;
3424 if (it->current.overlay_string_index == it->n_overlay_strings)
3425 {
3426 /* No more overlay strings. Restore IT's settings to what
3427 they were before overlay strings were processed, and
3428 continue to deliver from current_buffer. */
3429 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
3430
3431 pop_it (it);
3432 xassert (it->stop_charpos >= BEGV
3433 && it->stop_charpos <= it->end_charpos);
3434 it->string = Qnil;
3435 it->current.overlay_string_index = -1;
3436 SET_TEXT_POS (it->current.string_pos, -1, -1);
3437 it->n_overlay_strings = 0;
3438 it->method = next_element_from_buffer;
3439
3440 /* If we're at the end of the buffer, record that we have
3441 processed the overlay strings there already, so that
3442 next_element_from_buffer doesn't try it again. */
3443 if (IT_CHARPOS (*it) >= it->end_charpos)
3444 it->overlay_strings_at_end_processed_p = 1;
3445
3446 /* If we have to display `...' for invisible text, set
3447 the iterator up for that. */
3448 if (display_ellipsis_p)
3449 setup_for_ellipsis (it);
3450 }
3451 else
3452 {
3453 /* There are more overlay strings to process. If
3454 IT->current.overlay_string_index has advanced to a position
3455 where we must load IT->overlay_strings with more strings, do
3456 it. */
3457 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
3458
3459 if (it->current.overlay_string_index && i == 0)
3460 load_overlay_strings (it, 0);
3461
3462 /* Initialize IT to deliver display elements from the overlay
3463 string. */
3464 it->string = it->overlay_strings[i];
3465 it->multibyte_p = STRING_MULTIBYTE (it->string);
3466 SET_TEXT_POS (it->current.string_pos, 0, 0);
3467 it->method = next_element_from_string;
3468 it->stop_charpos = 0;
3469 }
3470
3471 CHECK_IT (it);
3472 }
3473
3474
3475 /* Compare two overlay_entry structures E1 and E2. Used as a
3476 comparison function for qsort in load_overlay_strings. Overlay
3477 strings for the same position are sorted so that
3478
3479 1. All after-strings come in front of before-strings, except
3480 when they come from the same overlay.
3481
3482 2. Within after-strings, strings are sorted so that overlay strings
3483 from overlays with higher priorities come first.
3484
3485 2. Within before-strings, strings are sorted so that overlay
3486 strings from overlays with higher priorities come last.
3487
3488 Value is analogous to strcmp. */
3489
3490
3491 static int
3492 compare_overlay_entries (e1, e2)
3493 void *e1, *e2;
3494 {
3495 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
3496 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
3497 int result;
3498
3499 if (entry1->after_string_p != entry2->after_string_p)
3500 {
3501 /* Let after-strings appear in front of before-strings if
3502 they come from different overlays. */
3503 if (EQ (entry1->overlay, entry2->overlay))
3504 result = entry1->after_string_p ? 1 : -1;
3505 else
3506 result = entry1->after_string_p ? -1 : 1;
3507 }
3508 else if (entry1->after_string_p)
3509 /* After-strings sorted in order of decreasing priority. */
3510 result = entry2->priority - entry1->priority;
3511 else
3512 /* Before-strings sorted in order of increasing priority. */
3513 result = entry1->priority - entry2->priority;
3514
3515 return result;
3516 }
3517
3518
3519 /* Load the vector IT->overlay_strings with overlay strings from IT's
3520 current buffer position, or from CHARPOS if that is > 0. Set
3521 IT->n_overlays to the total number of overlay strings found.
3522
3523 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
3524 a time. On entry into load_overlay_strings,
3525 IT->current.overlay_string_index gives the number of overlay
3526 strings that have already been loaded by previous calls to this
3527 function.
3528
3529 IT->add_overlay_start contains an additional overlay start
3530 position to consider for taking overlay strings from, if non-zero.
3531 This position comes into play when the overlay has an `invisible'
3532 property, and both before and after-strings. When we've skipped to
3533 the end of the overlay, because of its `invisible' property, we
3534 nevertheless want its before-string to appear.
3535 IT->add_overlay_start will contain the overlay start position
3536 in this case.
3537
3538 Overlay strings are sorted so that after-string strings come in
3539 front of before-string strings. Within before and after-strings,
3540 strings are sorted by overlay priority. See also function
3541 compare_overlay_entries. */
3542
3543 static void
3544 load_overlay_strings (it, charpos)
3545 struct it *it;
3546 int charpos;
3547 {
3548 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
3549 Lisp_Object ov, overlay, window, str, invisible;
3550 int start, end;
3551 int size = 20;
3552 int n = 0, i, j, invis_p;
3553 struct overlay_entry *entries
3554 = (struct overlay_entry *) alloca (size * sizeof *entries);
3555
3556 if (charpos <= 0)
3557 charpos = IT_CHARPOS (*it);
3558
3559 /* Append the overlay string STRING of overlay OVERLAY to vector
3560 `entries' which has size `size' and currently contains `n'
3561 elements. AFTER_P non-zero means STRING is an after-string of
3562 OVERLAY. */
3563 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
3564 do \
3565 { \
3566 Lisp_Object priority; \
3567 \
3568 if (n == size) \
3569 { \
3570 int new_size = 2 * size; \
3571 struct overlay_entry *old = entries; \
3572 entries = \
3573 (struct overlay_entry *) alloca (new_size \
3574 * sizeof *entries); \
3575 bcopy (old, entries, size * sizeof *entries); \
3576 size = new_size; \
3577 } \
3578 \
3579 entries[n].string = (STRING); \
3580 entries[n].overlay = (OVERLAY); \
3581 priority = Foverlay_get ((OVERLAY), Qpriority); \
3582 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
3583 entries[n].after_string_p = (AFTER_P); \
3584 ++n; \
3585 } \
3586 while (0)
3587
3588 /* Process overlay before the overlay center. */
3589 for (ov = current_buffer->overlays_before; CONSP (ov); ov = XCDR (ov))
3590 {
3591 overlay = XCAR (ov);
3592 xassert (OVERLAYP (overlay));
3593 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3594 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3595
3596 if (end < charpos)
3597 break;
3598
3599 /* Skip this overlay if it doesn't start or end at IT's current
3600 position. */
3601 if (end != charpos && start != charpos)
3602 continue;
3603
3604 /* Skip this overlay if it doesn't apply to IT->w. */
3605 window = Foverlay_get (overlay, Qwindow);
3606 if (WINDOWP (window) && XWINDOW (window) != it->w)
3607 continue;
3608
3609 /* If the text ``under'' the overlay is invisible, both before-
3610 and after-strings from this overlay are visible; start and
3611 end position are indistinguishable. */
3612 invisible = Foverlay_get (overlay, Qinvisible);
3613 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3614
3615 /* If overlay has a non-empty before-string, record it. */
3616 if ((start == charpos || (end == charpos && invis_p))
3617 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3618 && XSTRING (str)->size)
3619 RECORD_OVERLAY_STRING (overlay, str, 0);
3620
3621 /* If overlay has a non-empty after-string, record it. */
3622 if ((end == charpos || (start == charpos && invis_p))
3623 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3624 && XSTRING (str)->size)
3625 RECORD_OVERLAY_STRING (overlay, str, 1);
3626 }
3627
3628 /* Process overlays after the overlay center. */
3629 for (ov = current_buffer->overlays_after; CONSP (ov); ov = XCDR (ov))
3630 {
3631 overlay = XCAR (ov);
3632 xassert (OVERLAYP (overlay));
3633 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3634 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3635
3636 if (start > charpos)
3637 break;
3638
3639 /* Skip this overlay if it doesn't start or end at IT's current
3640 position. */
3641 if (end != charpos && start != charpos)
3642 continue;
3643
3644 /* Skip this overlay if it doesn't apply to IT->w. */
3645 window = Foverlay_get (overlay, Qwindow);
3646 if (WINDOWP (window) && XWINDOW (window) != it->w)
3647 continue;
3648
3649 /* If the text ``under'' the overlay is invisible, it has a zero
3650 dimension, and both before- and after-strings apply. */
3651 invisible = Foverlay_get (overlay, Qinvisible);
3652 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3653
3654 /* If overlay has a non-empty before-string, record it. */
3655 if ((start == charpos || (end == charpos && invis_p))
3656 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3657 && XSTRING (str)->size)
3658 RECORD_OVERLAY_STRING (overlay, str, 0);
3659
3660 /* If overlay has a non-empty after-string, record it. */
3661 if ((end == charpos || (start == charpos && invis_p))
3662 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3663 && XSTRING (str)->size)
3664 RECORD_OVERLAY_STRING (overlay, str, 1);
3665 }
3666
3667 #undef RECORD_OVERLAY_STRING
3668
3669 /* Sort entries. */
3670 if (n > 1)
3671 qsort (entries, n, sizeof *entries, compare_overlay_entries);
3672
3673 /* Record the total number of strings to process. */
3674 it->n_overlay_strings = n;
3675
3676 /* IT->current.overlay_string_index is the number of overlay strings
3677 that have already been consumed by IT. Copy some of the
3678 remaining overlay strings to IT->overlay_strings. */
3679 i = 0;
3680 j = it->current.overlay_string_index;
3681 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
3682 it->overlay_strings[i++] = entries[j++].string;
3683
3684 CHECK_IT (it);
3685 }
3686
3687
3688 /* Get the first chunk of overlay strings at IT's current buffer
3689 position, or at CHARPOS if that is > 0. Value is non-zero if at
3690 least one overlay string was found. */
3691
3692 static int
3693 get_overlay_strings (it, charpos)
3694 struct it *it;
3695 int charpos;
3696 {
3697 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
3698 process. This fills IT->overlay_strings with strings, and sets
3699 IT->n_overlay_strings to the total number of strings to process.
3700 IT->pos.overlay_string_index has to be set temporarily to zero
3701 because load_overlay_strings needs this; it must be set to -1
3702 when no overlay strings are found because a zero value would
3703 indicate a position in the first overlay string. */
3704 it->current.overlay_string_index = 0;
3705 load_overlay_strings (it, charpos);
3706
3707 /* If we found overlay strings, set up IT to deliver display
3708 elements from the first one. Otherwise set up IT to deliver
3709 from current_buffer. */
3710 if (it->n_overlay_strings)
3711 {
3712 /* Make sure we know settings in current_buffer, so that we can
3713 restore meaningful values when we're done with the overlay
3714 strings. */
3715 compute_stop_pos (it);
3716 xassert (it->face_id >= 0);
3717
3718 /* Save IT's settings. They are restored after all overlay
3719 strings have been processed. */
3720 xassert (it->sp == 0);
3721 push_it (it);
3722
3723 /* Set up IT to deliver display elements from the first overlay
3724 string. */
3725 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3726 it->string = it->overlay_strings[0];
3727 it->stop_charpos = 0;
3728 it->end_charpos = XSTRING (it->string)->size;
3729 it->multibyte_p = STRING_MULTIBYTE (it->string);
3730 xassert (STRINGP (it->string));
3731 it->method = next_element_from_string;
3732 }
3733 else
3734 {
3735 it->string = Qnil;
3736 it->current.overlay_string_index = -1;
3737 it->method = next_element_from_buffer;
3738 }
3739
3740 CHECK_IT (it);
3741
3742 /* Value is non-zero if we found at least one overlay string. */
3743 return STRINGP (it->string);
3744 }
3745
3746
3747 \f
3748 /***********************************************************************
3749 Saving and restoring state
3750 ***********************************************************************/
3751
3752 /* Save current settings of IT on IT->stack. Called, for example,
3753 before setting up IT for an overlay string, to be able to restore
3754 IT's settings to what they were after the overlay string has been
3755 processed. */
3756
3757 static void
3758 push_it (it)
3759 struct it *it;
3760 {
3761 struct iterator_stack_entry *p;
3762
3763 xassert (it->sp < 2);
3764 p = it->stack + it->sp;
3765
3766 p->stop_charpos = it->stop_charpos;
3767 xassert (it->face_id >= 0);
3768 p->face_id = it->face_id;
3769 p->string = it->string;
3770 p->pos = it->current;
3771 p->end_charpos = it->end_charpos;
3772 p->string_nchars = it->string_nchars;
3773 p->area = it->area;
3774 p->multibyte_p = it->multibyte_p;
3775 p->space_width = it->space_width;
3776 p->font_height = it->font_height;
3777 p->voffset = it->voffset;
3778 p->string_from_display_prop_p = it->string_from_display_prop_p;
3779 p->display_ellipsis_p = 0;
3780 ++it->sp;
3781 }
3782
3783
3784 /* Restore IT's settings from IT->stack. Called, for example, when no
3785 more overlay strings must be processed, and we return to delivering
3786 display elements from a buffer, or when the end of a string from a
3787 `display' property is reached and we return to delivering display
3788 elements from an overlay string, or from a buffer. */
3789
3790 static void
3791 pop_it (it)
3792 struct it *it;
3793 {
3794 struct iterator_stack_entry *p;
3795
3796 xassert (it->sp > 0);
3797 --it->sp;
3798 p = it->stack + it->sp;
3799 it->stop_charpos = p->stop_charpos;
3800 it->face_id = p->face_id;
3801 it->string = p->string;
3802 it->current = p->pos;
3803 it->end_charpos = p->end_charpos;
3804 it->string_nchars = p->string_nchars;
3805 it->area = p->area;
3806 it->multibyte_p = p->multibyte_p;
3807 it->space_width = p->space_width;
3808 it->font_height = p->font_height;
3809 it->voffset = p->voffset;
3810 it->string_from_display_prop_p = p->string_from_display_prop_p;
3811 }
3812
3813
3814 \f
3815 /***********************************************************************
3816 Moving over lines
3817 ***********************************************************************/
3818
3819 /* Set IT's current position to the previous line start. */
3820
3821 static void
3822 back_to_previous_line_start (it)
3823 struct it *it;
3824 {
3825 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
3826 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3827 }
3828
3829
3830 /* Move IT to the next line start.
3831
3832 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
3833 we skipped over part of the text (as opposed to moving the iterator
3834 continuously over the text). Otherwise, don't change the value
3835 of *SKIPPED_P.
3836
3837 Newlines may come from buffer text, overlay strings, or strings
3838 displayed via the `display' property. That's the reason we can't
3839 simply use find_next_newline_no_quit.
3840
3841 Note that this function may not skip over invisible text that is so
3842 because of text properties and immediately follows a newline. If
3843 it would, function reseat_at_next_visible_line_start, when called
3844 from set_iterator_to_next, would effectively make invisible
3845 characters following a newline part of the wrong glyph row, which
3846 leads to wrong cursor motion. */
3847
3848 static int
3849 forward_to_next_line_start (it, skipped_p)
3850 struct it *it;
3851 int *skipped_p;
3852 {
3853 int old_selective, newline_found_p, n;
3854 const int MAX_NEWLINE_DISTANCE = 500;
3855
3856 /* If already on a newline, just consume it to avoid unintended
3857 skipping over invisible text below. */
3858 if (it->what == IT_CHARACTER
3859 && it->c == '\n'
3860 && CHARPOS (it->position) == IT_CHARPOS (*it))
3861 {
3862 set_iterator_to_next (it, 0);
3863 it->c = 0;
3864 return 1;
3865 }
3866
3867 /* Don't handle selective display in the following. It's (a)
3868 unnecessary because it's done by the caller, and (b) leads to an
3869 infinite recursion because next_element_from_ellipsis indirectly
3870 calls this function. */
3871 old_selective = it->selective;
3872 it->selective = 0;
3873
3874 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
3875 from buffer text. */
3876 for (n = newline_found_p = 0;
3877 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
3878 n += STRINGP (it->string) ? 0 : 1)
3879 {
3880 if (!get_next_display_element (it))
3881 break;
3882 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
3883 set_iterator_to_next (it, 0);
3884 }
3885
3886 /* If we didn't find a newline near enough, see if we can use a
3887 short-cut. */
3888 if (n == MAX_NEWLINE_DISTANCE)
3889 {
3890 int start = IT_CHARPOS (*it);
3891 int limit = find_next_newline_no_quit (start, 1);
3892 Lisp_Object pos;
3893
3894 xassert (!STRINGP (it->string));
3895
3896 /* If there isn't any `display' property in sight, and no
3897 overlays, we can just use the position of the newline in
3898 buffer text. */
3899 if (it->stop_charpos >= limit
3900 || ((pos = Fnext_single_property_change (make_number (start),
3901 Qdisplay,
3902 Qnil, make_number (limit)),
3903 NILP (pos))
3904 && next_overlay_change (start) == ZV))
3905 {
3906 IT_CHARPOS (*it) = limit;
3907 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
3908 *skipped_p = newline_found_p = 1;
3909 }
3910 else
3911 {
3912 while (get_next_display_element (it)
3913 && !newline_found_p)
3914 {
3915 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
3916 set_iterator_to_next (it, 0);
3917 }
3918 }
3919 }
3920
3921 it->selective = old_selective;
3922 return newline_found_p;
3923 }
3924
3925
3926 /* Set IT's current position to the previous visible line start. Skip
3927 invisible text that is so either due to text properties or due to
3928 selective display. Caution: this does not change IT->current_x and
3929 IT->hpos. */
3930
3931 static void
3932 back_to_previous_visible_line_start (it)
3933 struct it *it;
3934 {
3935 int visible_p = 0;
3936
3937 /* Go back one newline if not on BEGV already. */
3938 if (IT_CHARPOS (*it) > BEGV)
3939 back_to_previous_line_start (it);
3940
3941 /* Move over lines that are invisible because of selective display
3942 or text properties. */
3943 while (IT_CHARPOS (*it) > BEGV
3944 && !visible_p)
3945 {
3946 visible_p = 1;
3947
3948 /* If selective > 0, then lines indented more than that values
3949 are invisible. */
3950 if (it->selective > 0
3951 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3952 it->selective))
3953 visible_p = 0;
3954 else
3955 {
3956 Lisp_Object prop;
3957
3958 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
3959 Qinvisible, it->window);
3960 if (TEXT_PROP_MEANS_INVISIBLE (prop))
3961 visible_p = 0;
3962 }
3963
3964 /* Back one more newline if the current one is invisible. */
3965 if (!visible_p)
3966 back_to_previous_line_start (it);
3967 }
3968
3969 xassert (IT_CHARPOS (*it) >= BEGV);
3970 xassert (IT_CHARPOS (*it) == BEGV
3971 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3972 CHECK_IT (it);
3973 }
3974
3975
3976 /* Reseat iterator IT at the previous visible line start. Skip
3977 invisible text that is so either due to text properties or due to
3978 selective display. At the end, update IT's overlay information,
3979 face information etc. */
3980
3981 static void
3982 reseat_at_previous_visible_line_start (it)
3983 struct it *it;
3984 {
3985 back_to_previous_visible_line_start (it);
3986 reseat (it, it->current.pos, 1);
3987 CHECK_IT (it);
3988 }
3989
3990
3991 /* Reseat iterator IT on the next visible line start in the current
3992 buffer. ON_NEWLINE_P non-zero means position IT on the newline
3993 preceding the line start. Skip over invisible text that is so
3994 because of selective display. Compute faces, overlays etc at the
3995 new position. Note that this function does not skip over text that
3996 is invisible because of text properties. */
3997
3998 static void
3999 reseat_at_next_visible_line_start (it, on_newline_p)
4000 struct it *it;
4001 int on_newline_p;
4002 {
4003 int newline_found_p, skipped_p = 0;
4004
4005 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4006
4007 /* Skip over lines that are invisible because they are indented
4008 more than the value of IT->selective. */
4009 if (it->selective > 0)
4010 while (IT_CHARPOS (*it) < ZV
4011 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4012 it->selective))
4013 {
4014 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4015 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4016 }
4017
4018 /* Position on the newline if that's what's requested. */
4019 if (on_newline_p && newline_found_p)
4020 {
4021 if (STRINGP (it->string))
4022 {
4023 if (IT_STRING_CHARPOS (*it) > 0)
4024 {
4025 --IT_STRING_CHARPOS (*it);
4026 --IT_STRING_BYTEPOS (*it);
4027 }
4028 }
4029 else if (IT_CHARPOS (*it) > BEGV)
4030 {
4031 --IT_CHARPOS (*it);
4032 --IT_BYTEPOS (*it);
4033 reseat (it, it->current.pos, 0);
4034 }
4035 }
4036 else if (skipped_p)
4037 reseat (it, it->current.pos, 0);
4038
4039 CHECK_IT (it);
4040 }
4041
4042
4043 \f
4044 /***********************************************************************
4045 Changing an iterator's position
4046 ***********************************************************************/
4047
4048 /* Change IT's current position to POS in current_buffer. If FORCE_P
4049 is non-zero, always check for text properties at the new position.
4050 Otherwise, text properties are only looked up if POS >=
4051 IT->check_charpos of a property. */
4052
4053 static void
4054 reseat (it, pos, force_p)
4055 struct it *it;
4056 struct text_pos pos;
4057 int force_p;
4058 {
4059 int original_pos = IT_CHARPOS (*it);
4060
4061 reseat_1 (it, pos, 0);
4062
4063 /* Determine where to check text properties. Avoid doing it
4064 where possible because text property lookup is very expensive. */
4065 if (force_p
4066 || CHARPOS (pos) > it->stop_charpos
4067 || CHARPOS (pos) < original_pos)
4068 handle_stop (it);
4069
4070 CHECK_IT (it);
4071 }
4072
4073
4074 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4075 IT->stop_pos to POS, also. */
4076
4077 static void
4078 reseat_1 (it, pos, set_stop_p)
4079 struct it *it;
4080 struct text_pos pos;
4081 int set_stop_p;
4082 {
4083 /* Don't call this function when scanning a C string. */
4084 xassert (it->s == NULL);
4085
4086 /* POS must be a reasonable value. */
4087 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4088
4089 it->current.pos = it->position = pos;
4090 XSETBUFFER (it->object, current_buffer);
4091 it->end_charpos = ZV;
4092 it->dpvec = NULL;
4093 it->current.dpvec_index = -1;
4094 it->current.overlay_string_index = -1;
4095 IT_STRING_CHARPOS (*it) = -1;
4096 IT_STRING_BYTEPOS (*it) = -1;
4097 it->string = Qnil;
4098 it->method = next_element_from_buffer;
4099 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
4100 it->sp = 0;
4101 it->face_before_selective_p = 0;
4102
4103 if (set_stop_p)
4104 it->stop_charpos = CHARPOS (pos);
4105 }
4106
4107
4108 /* Set up IT for displaying a string, starting at CHARPOS in window W.
4109 If S is non-null, it is a C string to iterate over. Otherwise,
4110 STRING gives a Lisp string to iterate over.
4111
4112 If PRECISION > 0, don't return more then PRECISION number of
4113 characters from the string.
4114
4115 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4116 characters have been returned. FIELD_WIDTH < 0 means an infinite
4117 field width.
4118
4119 MULTIBYTE = 0 means disable processing of multibyte characters,
4120 MULTIBYTE > 0 means enable it,
4121 MULTIBYTE < 0 means use IT->multibyte_p.
4122
4123 IT must be initialized via a prior call to init_iterator before
4124 calling this function. */
4125
4126 static void
4127 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4128 struct it *it;
4129 unsigned char *s;
4130 Lisp_Object string;
4131 int charpos;
4132 int precision, field_width, multibyte;
4133 {
4134 /* No region in strings. */
4135 it->region_beg_charpos = it->region_end_charpos = -1;
4136
4137 /* No text property checks performed by default, but see below. */
4138 it->stop_charpos = -1;
4139
4140 /* Set iterator position and end position. */
4141 bzero (&it->current, sizeof it->current);
4142 it->current.overlay_string_index = -1;
4143 it->current.dpvec_index = -1;
4144 xassert (charpos >= 0);
4145
4146 /* If STRING is specified, use its multibyteness, otherwise use the
4147 setting of MULTIBYTE, if specified. */
4148 if (multibyte >= 0)
4149 it->multibyte_p = multibyte > 0;
4150
4151 if (s == NULL)
4152 {
4153 xassert (STRINGP (string));
4154 it->string = string;
4155 it->s = NULL;
4156 it->end_charpos = it->string_nchars = XSTRING (string)->size;
4157 it->method = next_element_from_string;
4158 it->current.string_pos = string_pos (charpos, string);
4159 }
4160 else
4161 {
4162 it->s = s;
4163 it->string = Qnil;
4164
4165 /* Note that we use IT->current.pos, not it->current.string_pos,
4166 for displaying C strings. */
4167 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4168 if (it->multibyte_p)
4169 {
4170 it->current.pos = c_string_pos (charpos, s, 1);
4171 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4172 }
4173 else
4174 {
4175 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4176 it->end_charpos = it->string_nchars = strlen (s);
4177 }
4178
4179 it->method = next_element_from_c_string;
4180 }
4181
4182 /* PRECISION > 0 means don't return more than PRECISION characters
4183 from the string. */
4184 if (precision > 0 && it->end_charpos - charpos > precision)
4185 it->end_charpos = it->string_nchars = charpos + precision;
4186
4187 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4188 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4189 FIELD_WIDTH < 0 means infinite field width. This is useful for
4190 padding with `-' at the end of a mode line. */
4191 if (field_width < 0)
4192 field_width = INFINITY;
4193 if (field_width > it->end_charpos - charpos)
4194 it->end_charpos = charpos + field_width;
4195
4196 /* Use the standard display table for displaying strings. */
4197 if (DISP_TABLE_P (Vstandard_display_table))
4198 it->dp = XCHAR_TABLE (Vstandard_display_table);
4199
4200 it->stop_charpos = charpos;
4201 CHECK_IT (it);
4202 }
4203
4204
4205 \f
4206 /***********************************************************************
4207 Iteration
4208 ***********************************************************************/
4209
4210 /* Load IT's display element fields with information about the next
4211 display element from the current position of IT. Value is zero if
4212 end of buffer (or C string) is reached. */
4213
4214 int
4215 get_next_display_element (it)
4216 struct it *it;
4217 {
4218 /* Non-zero means that we found an display element. Zero means that
4219 we hit the end of what we iterate over. Performance note: the
4220 function pointer `method' used here turns out to be faster than
4221 using a sequence of if-statements. */
4222 int success_p = (*it->method) (it);
4223
4224 if (it->what == IT_CHARACTER)
4225 {
4226 /* Map via display table or translate control characters.
4227 IT->c, IT->len etc. have been set to the next character by
4228 the function call above. If we have a display table, and it
4229 contains an entry for IT->c, translate it. Don't do this if
4230 IT->c itself comes from a display table, otherwise we could
4231 end up in an infinite recursion. (An alternative could be to
4232 count the recursion depth of this function and signal an
4233 error when a certain maximum depth is reached.) Is it worth
4234 it? */
4235 if (success_p && it->dpvec == NULL)
4236 {
4237 Lisp_Object dv;
4238
4239 if (it->dp
4240 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
4241 VECTORP (dv)))
4242 {
4243 struct Lisp_Vector *v = XVECTOR (dv);
4244
4245 /* Return the first character from the display table
4246 entry, if not empty. If empty, don't display the
4247 current character. */
4248 if (v->size)
4249 {
4250 it->dpvec_char_len = it->len;
4251 it->dpvec = v->contents;
4252 it->dpend = v->contents + v->size;
4253 it->current.dpvec_index = 0;
4254 it->method = next_element_from_display_vector;
4255 success_p = get_next_display_element (it);
4256 }
4257 else
4258 {
4259 set_iterator_to_next (it, 0);
4260 success_p = get_next_display_element (it);
4261 }
4262 }
4263
4264 /* Translate control characters into `\003' or `^C' form.
4265 Control characters coming from a display table entry are
4266 currently not translated because we use IT->dpvec to hold
4267 the translation. This could easily be changed but I
4268 don't believe that it is worth doing.
4269
4270 Non-printable multibyte characters are also translated
4271 octal form. */
4272 else if ((it->c < ' '
4273 && (it->area != TEXT_AREA
4274 || (it->c != '\n' && it->c != '\t')))
4275 || (it->c != '\n' && it->c != '\t'
4276 && (it->multibyte_p ? !CHAR_PRINTABLE_P (it->c)
4277 : it->c == 127)))
4278 {
4279 /* IT->c is a control character which must be displayed
4280 either as '\003' or as `^C' where the '\\' and '^'
4281 can be defined in the display table. Fill
4282 IT->ctl_chars with glyphs for what we have to
4283 display. Then, set IT->dpvec to these glyphs. */
4284 GLYPH g;
4285
4286 if (it->c < 128 && it->ctl_arrow_p)
4287 {
4288 /* Set IT->ctl_chars[0] to the glyph for `^'. */
4289 if (it->dp
4290 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
4291 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
4292 g = XINT (DISP_CTRL_GLYPH (it->dp));
4293 else
4294 g = FAST_MAKE_GLYPH ('^', 0);
4295 XSETINT (it->ctl_chars[0], g);
4296
4297 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
4298 XSETINT (it->ctl_chars[1], g);
4299
4300 /* Set up IT->dpvec and return first character from it. */
4301 it->dpvec_char_len = it->len;
4302 it->dpvec = it->ctl_chars;
4303 it->dpend = it->dpvec + 2;
4304 it->current.dpvec_index = 0;
4305 it->method = next_element_from_display_vector;
4306 get_next_display_element (it);
4307 }
4308 else
4309 {
4310 unsigned char str[MAX_MULTIBYTE_LENGTH];
4311 int len;
4312 int i;
4313 GLYPH escape_glyph;
4314
4315 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
4316 if (it->dp
4317 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
4318 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
4319 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
4320 else
4321 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
4322
4323 if (CHAR_BYTE8_P (it->c))
4324 {
4325 str[0] = CHAR_TO_BYTE8 (it->c);
4326 len = 1;
4327 }
4328 else if (it->c < 256)
4329 {
4330 str[0] = it->c;
4331 len = 1;
4332 }
4333 else
4334 {
4335 /* It's an invalid character, which
4336 shouldn't happen actually, but due to
4337 bugs it may happen. Let's print the char
4338 as is, there's not much meaningful we can
4339 do with it. */
4340 str[0] = it->c;
4341 str[1] = it->c >> 8;
4342 str[2] = it->c >> 16;
4343 str[3] = it->c >> 24;
4344 len = 4;
4345 }
4346
4347 for (i = 0; i < len; i++)
4348 {
4349 XSETINT (it->ctl_chars[i * 4], escape_glyph);
4350 /* Insert three more glyphs into IT->ctl_chars for
4351 the octal display of the character. */
4352 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
4353 XSETINT (it->ctl_chars[i * 4 + 1], g);
4354 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
4355 XSETINT (it->ctl_chars[i * 4 + 2], g);
4356 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
4357 XSETINT (it->ctl_chars[i * 4 + 3], g);
4358 }
4359
4360 /* Set up IT->dpvec and return the first character
4361 from it. */
4362 it->dpvec_char_len = it->len;
4363 it->dpvec = it->ctl_chars;
4364 it->dpend = it->dpvec + len * 4;
4365 it->current.dpvec_index = 0;
4366 it->method = next_element_from_display_vector;
4367 get_next_display_element (it);
4368 }
4369 }
4370 }
4371
4372 /* Adjust face id for a multibyte character. There are no
4373 multibyte character in unibyte text. */
4374 if (it->multibyte_p
4375 && success_p
4376 && FRAME_WINDOW_P (it->f))
4377 {
4378 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4379 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
4380 }
4381 }
4382
4383 /* Is this character the last one of a run of characters with
4384 box? If yes, set IT->end_of_box_run_p to 1. */
4385 if (it->face_box_p
4386 && it->s == NULL)
4387 {
4388 int face_id;
4389 struct face *face;
4390
4391 it->end_of_box_run_p
4392 = ((face_id = face_after_it_pos (it),
4393 face_id != it->face_id)
4394 && (face = FACE_FROM_ID (it->f, face_id),
4395 face->box == FACE_NO_BOX));
4396 }
4397
4398 /* Value is 0 if end of buffer or string reached. */
4399 return success_p;
4400 }
4401
4402
4403 /* Move IT to the next display element.
4404
4405 RESEAT_P non-zero means if called on a newline in buffer text,
4406 skip to the next visible line start.
4407
4408 Functions get_next_display_element and set_iterator_to_next are
4409 separate because I find this arrangement easier to handle than a
4410 get_next_display_element function that also increments IT's
4411 position. The way it is we can first look at an iterator's current
4412 display element, decide whether it fits on a line, and if it does,
4413 increment the iterator position. The other way around we probably
4414 would either need a flag indicating whether the iterator has to be
4415 incremented the next time, or we would have to implement a
4416 decrement position function which would not be easy to write. */
4417
4418 void
4419 set_iterator_to_next (it, reseat_p)
4420 struct it *it;
4421 int reseat_p;
4422 {
4423 /* Reset flags indicating start and end of a sequence of characters
4424 with box. Reset them at the start of this function because
4425 moving the iterator to a new position might set them. */
4426 it->start_of_box_run_p = it->end_of_box_run_p = 0;
4427
4428 if (it->method == next_element_from_buffer)
4429 {
4430 /* The current display element of IT is a character from
4431 current_buffer. Advance in the buffer, and maybe skip over
4432 invisible lines that are so because of selective display. */
4433 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
4434 reseat_at_next_visible_line_start (it, 0);
4435 else
4436 {
4437 xassert (it->len != 0);
4438 IT_BYTEPOS (*it) += it->len;
4439 IT_CHARPOS (*it) += 1;
4440 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
4441 }
4442 }
4443 else if (it->method == next_element_from_composition)
4444 {
4445 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
4446 if (STRINGP (it->string))
4447 {
4448 IT_STRING_BYTEPOS (*it) += it->len;
4449 IT_STRING_CHARPOS (*it) += it->cmp_len;
4450 it->method = next_element_from_string;
4451 goto consider_string_end;
4452 }
4453 else
4454 {
4455 IT_BYTEPOS (*it) += it->len;
4456 IT_CHARPOS (*it) += it->cmp_len;
4457 it->method = next_element_from_buffer;
4458 }
4459 }
4460 else if (it->method == next_element_from_c_string)
4461 {
4462 /* Current display element of IT is from a C string. */
4463 IT_BYTEPOS (*it) += it->len;
4464 IT_CHARPOS (*it) += 1;
4465 }
4466 else if (it->method == next_element_from_display_vector)
4467 {
4468 /* Current display element of IT is from a display table entry.
4469 Advance in the display table definition. Reset it to null if
4470 end reached, and continue with characters from buffers/
4471 strings. */
4472 ++it->current.dpvec_index;
4473
4474 /* Restore face of the iterator to what they were before the
4475 display vector entry (these entries may contain faces). */
4476 it->face_id = it->saved_face_id;
4477
4478 if (it->dpvec + it->current.dpvec_index == it->dpend)
4479 {
4480 if (it->s)
4481 it->method = next_element_from_c_string;
4482 else if (STRINGP (it->string))
4483 it->method = next_element_from_string;
4484 else
4485 it->method = next_element_from_buffer;
4486
4487 it->dpvec = NULL;
4488 it->current.dpvec_index = -1;
4489
4490 /* Skip over characters which were displayed via IT->dpvec. */
4491 if (it->dpvec_char_len < 0)
4492 reseat_at_next_visible_line_start (it, 1);
4493 else if (it->dpvec_char_len > 0)
4494 {
4495 it->len = it->dpvec_char_len;
4496 set_iterator_to_next (it, reseat_p);
4497 }
4498 }
4499 }
4500 else if (it->method == next_element_from_string)
4501 {
4502 /* Current display element is a character from a Lisp string. */
4503 xassert (it->s == NULL && STRINGP (it->string));
4504 IT_STRING_BYTEPOS (*it) += it->len;
4505 IT_STRING_CHARPOS (*it) += 1;
4506
4507 consider_string_end:
4508
4509 if (it->current.overlay_string_index >= 0)
4510 {
4511 /* IT->string is an overlay string. Advance to the
4512 next, if there is one. */
4513 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
4514 next_overlay_string (it);
4515 }
4516 else
4517 {
4518 /* IT->string is not an overlay string. If we reached
4519 its end, and there is something on IT->stack, proceed
4520 with what is on the stack. This can be either another
4521 string, this time an overlay string, or a buffer. */
4522 if (IT_STRING_CHARPOS (*it) == XSTRING (it->string)->size
4523 && it->sp > 0)
4524 {
4525 pop_it (it);
4526 if (!STRINGP (it->string))
4527 it->method = next_element_from_buffer;
4528 else
4529 goto consider_string_end;
4530 }
4531 }
4532 }
4533 else if (it->method == next_element_from_image
4534 || it->method == next_element_from_stretch)
4535 {
4536 /* The position etc with which we have to proceed are on
4537 the stack. The position may be at the end of a string,
4538 if the `display' property takes up the whole string. */
4539 pop_it (it);
4540 it->image_id = 0;
4541 if (STRINGP (it->string))
4542 {
4543 it->method = next_element_from_string;
4544 goto consider_string_end;
4545 }
4546 else
4547 it->method = next_element_from_buffer;
4548 }
4549 else
4550 /* There are no other methods defined, so this should be a bug. */
4551 abort ();
4552
4553 xassert (it->method != next_element_from_string
4554 || (STRINGP (it->string)
4555 && IT_STRING_CHARPOS (*it) >= 0));
4556 }
4557
4558
4559 /* Load IT's display element fields with information about the next
4560 display element which comes from a display table entry or from the
4561 result of translating a control character to one of the forms `^C'
4562 or `\003'. IT->dpvec holds the glyphs to return as characters. */
4563
4564 static int
4565 next_element_from_display_vector (it)
4566 struct it *it;
4567 {
4568 /* Precondition. */
4569 xassert (it->dpvec && it->current.dpvec_index >= 0);
4570
4571 /* Remember the current face id in case glyphs specify faces.
4572 IT's face is restored in set_iterator_to_next. */
4573 it->saved_face_id = it->face_id;
4574
4575 if (INTEGERP (*it->dpvec)
4576 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
4577 {
4578 int lface_id;
4579 GLYPH g;
4580
4581 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
4582 it->c = FAST_GLYPH_CHAR (g);
4583 it->len = CHAR_BYTES (it->c);
4584
4585 /* The entry may contain a face id to use. Such a face id is
4586 the id of a Lisp face, not a realized face. A face id of
4587 zero means no face is specified. */
4588 lface_id = FAST_GLYPH_FACE (g);
4589 if (lface_id)
4590 {
4591 /* The function returns -1 if lface_id is invalid. */
4592 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
4593 if (face_id >= 0)
4594 it->face_id = face_id;
4595 }
4596 }
4597 else
4598 /* Display table entry is invalid. Return a space. */
4599 it->c = ' ', it->len = 1;
4600
4601 /* Don't change position and object of the iterator here. They are
4602 still the values of the character that had this display table
4603 entry or was translated, and that's what we want. */
4604 it->what = IT_CHARACTER;
4605 return 1;
4606 }
4607
4608
4609 /* Load IT with the next display element from Lisp string IT->string.
4610 IT->current.string_pos is the current position within the string.
4611 If IT->current.overlay_string_index >= 0, the Lisp string is an
4612 overlay string. */
4613
4614 static int
4615 next_element_from_string (it)
4616 struct it *it;
4617 {
4618 struct text_pos position;
4619
4620 xassert (STRINGP (it->string));
4621 xassert (IT_STRING_CHARPOS (*it) >= 0);
4622 position = it->current.string_pos;
4623
4624 /* Time to check for invisible text? */
4625 if (IT_STRING_CHARPOS (*it) < it->end_charpos
4626 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
4627 {
4628 handle_stop (it);
4629
4630 /* Since a handler may have changed IT->method, we must
4631 recurse here. */
4632 return get_next_display_element (it);
4633 }
4634
4635 if (it->current.overlay_string_index >= 0)
4636 {
4637 /* Get the next character from an overlay string. In overlay
4638 strings, There is no field width or padding with spaces to
4639 do. */
4640 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
4641 {
4642 it->what = IT_EOB;
4643 return 0;
4644 }
4645 else if (STRING_MULTIBYTE (it->string))
4646 {
4647 int remaining = (STRING_BYTES (XSTRING (it->string))
4648 - IT_STRING_BYTEPOS (*it));
4649 unsigned char *s = (XSTRING (it->string)->data
4650 + IT_STRING_BYTEPOS (*it));
4651 it->c = string_char_and_length (s, remaining, &it->len);
4652 }
4653 else
4654 {
4655 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
4656 it->len = 1;
4657 }
4658 }
4659 else
4660 {
4661 /* Get the next character from a Lisp string that is not an
4662 overlay string. Such strings come from the mode line, for
4663 example. We may have to pad with spaces, or truncate the
4664 string. See also next_element_from_c_string. */
4665 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
4666 {
4667 it->what = IT_EOB;
4668 return 0;
4669 }
4670 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
4671 {
4672 /* Pad with spaces. */
4673 it->c = ' ', it->len = 1;
4674 CHARPOS (position) = BYTEPOS (position) = -1;
4675 }
4676 else if (STRING_MULTIBYTE (it->string))
4677 {
4678 int maxlen = (STRING_BYTES (XSTRING (it->string))
4679 - IT_STRING_BYTEPOS (*it));
4680 unsigned char *s = (XSTRING (it->string)->data
4681 + IT_STRING_BYTEPOS (*it));
4682 it->c = string_char_and_length (s, maxlen, &it->len);
4683 }
4684 else
4685 {
4686 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
4687 it->len = 1;
4688 }
4689 }
4690
4691 /* Record what we have and where it came from. Note that we store a
4692 buffer position in IT->position although it could arguably be a
4693 string position. */
4694 it->what = IT_CHARACTER;
4695 it->object = it->string;
4696 it->position = position;
4697 return 1;
4698 }
4699
4700
4701 /* Load IT with next display element from C string IT->s.
4702 IT->string_nchars is the maximum number of characters to return
4703 from the string. IT->end_charpos may be greater than
4704 IT->string_nchars when this function is called, in which case we
4705 may have to return padding spaces. Value is zero if end of string
4706 reached, including padding spaces. */
4707
4708 static int
4709 next_element_from_c_string (it)
4710 struct it *it;
4711 {
4712 int success_p = 1;
4713
4714 xassert (it->s);
4715 it->what = IT_CHARACTER;
4716 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
4717 it->object = Qnil;
4718
4719 /* IT's position can be greater IT->string_nchars in case a field
4720 width or precision has been specified when the iterator was
4721 initialized. */
4722 if (IT_CHARPOS (*it) >= it->end_charpos)
4723 {
4724 /* End of the game. */
4725 it->what = IT_EOB;
4726 success_p = 0;
4727 }
4728 else if (IT_CHARPOS (*it) >= it->string_nchars)
4729 {
4730 /* Pad with spaces. */
4731 it->c = ' ', it->len = 1;
4732 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
4733 }
4734 else if (it->multibyte_p)
4735 {
4736 /* Implementation note: The calls to strlen apparently aren't a
4737 performance problem because there is no noticeable performance
4738 difference between Emacs running in unibyte or multibyte mode. */
4739 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
4740 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
4741 maxlen, &it->len);
4742 }
4743 else
4744 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
4745
4746 return success_p;
4747 }
4748
4749
4750 /* Set up IT to return characters from an ellipsis, if appropriate.
4751 The definition of the ellipsis glyphs may come from a display table
4752 entry. This function Fills IT with the first glyph from the
4753 ellipsis if an ellipsis is to be displayed. */
4754
4755 static int
4756 next_element_from_ellipsis (it)
4757 struct it *it;
4758 {
4759 if (it->selective_display_ellipsis_p)
4760 {
4761 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4762 {
4763 /* Use the display table definition for `...'. Invalid glyphs
4764 will be handled by the method returning elements from dpvec. */
4765 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4766 it->dpvec_char_len = it->len;
4767 it->dpvec = v->contents;
4768 it->dpend = v->contents + v->size;
4769 it->current.dpvec_index = 0;
4770 it->method = next_element_from_display_vector;
4771 }
4772 else
4773 {
4774 /* Use default `...' which is stored in default_invis_vector. */
4775 it->dpvec_char_len = it->len;
4776 it->dpvec = default_invis_vector;
4777 it->dpend = default_invis_vector + 3;
4778 it->current.dpvec_index = 0;
4779 it->method = next_element_from_display_vector;
4780 }
4781 }
4782 else
4783 {
4784 /* The face at the current position may be different from the
4785 face we find after the invisible text. Remember what it
4786 was in IT->saved_face_id, and signal that it's there by
4787 setting face_before_selective_p. */
4788 it->saved_face_id = it->face_id;
4789 it->method = next_element_from_buffer;
4790 reseat_at_next_visible_line_start (it, 1);
4791 it->face_before_selective_p = 1;
4792 }
4793
4794 return get_next_display_element (it);
4795 }
4796
4797
4798 /* Deliver an image display element. The iterator IT is already
4799 filled with image information (done in handle_display_prop). Value
4800 is always 1. */
4801
4802
4803 static int
4804 next_element_from_image (it)
4805 struct it *it;
4806 {
4807 it->what = IT_IMAGE;
4808 return 1;
4809 }
4810
4811
4812 /* Fill iterator IT with next display element from a stretch glyph
4813 property. IT->object is the value of the text property. Value is
4814 always 1. */
4815
4816 static int
4817 next_element_from_stretch (it)
4818 struct it *it;
4819 {
4820 it->what = IT_STRETCH;
4821 return 1;
4822 }
4823
4824
4825 /* Load IT with the next display element from current_buffer. Value
4826 is zero if end of buffer reached. IT->stop_charpos is the next
4827 position at which to stop and check for text properties or buffer
4828 end. */
4829
4830 static int
4831 next_element_from_buffer (it)
4832 struct it *it;
4833 {
4834 int success_p = 1;
4835
4836 /* Check this assumption, otherwise, we would never enter the
4837 if-statement, below. */
4838 xassert (IT_CHARPOS (*it) >= BEGV
4839 && IT_CHARPOS (*it) <= it->stop_charpos);
4840
4841 if (IT_CHARPOS (*it) >= it->stop_charpos)
4842 {
4843 if (IT_CHARPOS (*it) >= it->end_charpos)
4844 {
4845 int overlay_strings_follow_p;
4846
4847 /* End of the game, except when overlay strings follow that
4848 haven't been returned yet. */
4849 if (it->overlay_strings_at_end_processed_p)
4850 overlay_strings_follow_p = 0;
4851 else
4852 {
4853 it->overlay_strings_at_end_processed_p = 1;
4854 overlay_strings_follow_p = get_overlay_strings (it, 0);
4855 }
4856
4857 if (overlay_strings_follow_p)
4858 success_p = get_next_display_element (it);
4859 else
4860 {
4861 it->what = IT_EOB;
4862 it->position = it->current.pos;
4863 success_p = 0;
4864 }
4865 }
4866 else
4867 {
4868 handle_stop (it);
4869 return get_next_display_element (it);
4870 }
4871 }
4872 else
4873 {
4874 /* No face changes, overlays etc. in sight, so just return a
4875 character from current_buffer. */
4876 unsigned char *p;
4877
4878 /* Maybe run the redisplay end trigger hook. Performance note:
4879 This doesn't seem to cost measurable time. */
4880 if (it->redisplay_end_trigger_charpos
4881 && it->glyph_row
4882 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
4883 run_redisplay_end_trigger_hook (it);
4884
4885 /* Get the next character, maybe multibyte. */
4886 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
4887 if (it->multibyte_p && !ASCII_BYTE_P (*p))
4888 {
4889 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
4890 - IT_BYTEPOS (*it));
4891 it->c = string_char_and_length (p, maxlen, &it->len);
4892 }
4893 else
4894 it->c = *p, it->len = 1;
4895
4896 /* Record what we have and where it came from. */
4897 it->what = IT_CHARACTER;;
4898 it->object = it->w->buffer;
4899 it->position = it->current.pos;
4900
4901 /* Normally we return the character found above, except when we
4902 really want to return an ellipsis for selective display. */
4903 if (it->selective)
4904 {
4905 if (it->c == '\n')
4906 {
4907 /* A value of selective > 0 means hide lines indented more
4908 than that number of columns. */
4909 if (it->selective > 0
4910 && IT_CHARPOS (*it) + 1 < ZV
4911 && indented_beyond_p (IT_CHARPOS (*it) + 1,
4912 IT_BYTEPOS (*it) + 1,
4913 it->selective))
4914 {
4915 success_p = next_element_from_ellipsis (it);
4916 it->dpvec_char_len = -1;
4917 }
4918 }
4919 else if (it->c == '\r' && it->selective == -1)
4920 {
4921 /* A value of selective == -1 means that everything from the
4922 CR to the end of the line is invisible, with maybe an
4923 ellipsis displayed for it. */
4924 success_p = next_element_from_ellipsis (it);
4925 it->dpvec_char_len = -1;
4926 }
4927 }
4928 }
4929
4930 /* Value is zero if end of buffer reached. */
4931 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
4932 return success_p;
4933 }
4934
4935
4936 /* Run the redisplay end trigger hook for IT. */
4937
4938 static void
4939 run_redisplay_end_trigger_hook (it)
4940 struct it *it;
4941 {
4942 Lisp_Object args[3];
4943
4944 /* IT->glyph_row should be non-null, i.e. we should be actually
4945 displaying something, or otherwise we should not run the hook. */
4946 xassert (it->glyph_row);
4947
4948 /* Set up hook arguments. */
4949 args[0] = Qredisplay_end_trigger_functions;
4950 args[1] = it->window;
4951 XSETINT (args[2], it->redisplay_end_trigger_charpos);
4952 it->redisplay_end_trigger_charpos = 0;
4953
4954 /* Since we are *trying* to run these functions, don't try to run
4955 them again, even if they get an error. */
4956 it->w->redisplay_end_trigger = Qnil;
4957 Frun_hook_with_args (3, args);
4958
4959 /* Notice if it changed the face of the character we are on. */
4960 handle_face_prop (it);
4961 }
4962
4963
4964 /* Deliver a composition display element. The iterator IT is already
4965 filled with composition information (done in
4966 handle_composition_prop). Value is always 1. */
4967
4968 static int
4969 next_element_from_composition (it)
4970 struct it *it;
4971 {
4972 it->what = IT_COMPOSITION;
4973 it->position = (STRINGP (it->string)
4974 ? it->current.string_pos
4975 : it->current.pos);
4976 return 1;
4977 }
4978
4979
4980 \f
4981 /***********************************************************************
4982 Moving an iterator without producing glyphs
4983 ***********************************************************************/
4984
4985 /* Move iterator IT to a specified buffer or X position within one
4986 line on the display without producing glyphs.
4987
4988 Begin to skip at IT's current position. Skip to TO_CHARPOS or TO_X
4989 whichever is reached first.
4990
4991 TO_CHARPOS <= 0 means no TO_CHARPOS is specified.
4992
4993 TO_X < 0 means that no TO_X is specified. TO_X is normally a value
4994 0 <= TO_X <= IT->last_visible_x. This means in particular, that
4995 TO_X includes the amount by which a window is horizontally
4996 scrolled.
4997
4998 Value is
4999
5000 MOVE_POS_MATCH_OR_ZV
5001 - when TO_POS or ZV was reached.
5002
5003 MOVE_X_REACHED
5004 -when TO_X was reached before TO_POS or ZV were reached.
5005
5006 MOVE_LINE_CONTINUED
5007 - when we reached the end of the display area and the line must
5008 be continued.
5009
5010 MOVE_LINE_TRUNCATED
5011 - when we reached the end of the display area and the line is
5012 truncated.
5013
5014 MOVE_NEWLINE_OR_CR
5015 - when we stopped at a line end, i.e. a newline or a CR and selective
5016 display is on. */
5017
5018 static enum move_it_result
5019 move_it_in_display_line_to (it, to_charpos, to_x, op)
5020 struct it *it;
5021 int to_charpos, to_x, op;
5022 {
5023 enum move_it_result result = MOVE_UNDEFINED;
5024 struct glyph_row *saved_glyph_row;
5025
5026 /* Don't produce glyphs in produce_glyphs. */
5027 saved_glyph_row = it->glyph_row;
5028 it->glyph_row = NULL;
5029
5030 while (1)
5031 {
5032 int x, i, ascent = 0, descent = 0;
5033
5034 /* Stop when ZV or TO_CHARPOS reached. */
5035 if (!get_next_display_element (it)
5036 || ((op & MOVE_TO_POS) != 0
5037 && BUFFERP (it->object)
5038 && IT_CHARPOS (*it) >= to_charpos))
5039 {
5040 result = MOVE_POS_MATCH_OR_ZV;
5041 break;
5042 }
5043
5044 /* The call to produce_glyphs will get the metrics of the
5045 display element IT is loaded with. We record in x the
5046 x-position before this display element in case it does not
5047 fit on the line. */
5048 x = it->current_x;
5049
5050 /* Remember the line height so far in case the next element doesn't
5051 fit on the line. */
5052 if (!it->truncate_lines_p)
5053 {
5054 ascent = it->max_ascent;
5055 descent = it->max_descent;
5056 }
5057
5058 PRODUCE_GLYPHS (it);
5059
5060 if (it->area != TEXT_AREA)
5061 {
5062 set_iterator_to_next (it, 1);
5063 continue;
5064 }
5065
5066 /* The number of glyphs we get back in IT->nglyphs will normally
5067 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
5068 character on a terminal frame, or (iii) a line end. For the
5069 second case, IT->nglyphs - 1 padding glyphs will be present
5070 (on X frames, there is only one glyph produced for a
5071 composite character.
5072
5073 The behavior implemented below means, for continuation lines,
5074 that as many spaces of a TAB as fit on the current line are
5075 displayed there. For terminal frames, as many glyphs of a
5076 multi-glyph character are displayed in the current line, too.
5077 This is what the old redisplay code did, and we keep it that
5078 way. Under X, the whole shape of a complex character must
5079 fit on the line or it will be completely displayed in the
5080 next line.
5081
5082 Note that both for tabs and padding glyphs, all glyphs have
5083 the same width. */
5084 if (it->nglyphs)
5085 {
5086 /* More than one glyph or glyph doesn't fit on line. All
5087 glyphs have the same width. */
5088 int single_glyph_width = it->pixel_width / it->nglyphs;
5089 int new_x;
5090
5091 for (i = 0; i < it->nglyphs; ++i, x = new_x)
5092 {
5093 new_x = x + single_glyph_width;
5094
5095 /* We want to leave anything reaching TO_X to the caller. */
5096 if ((op & MOVE_TO_X) && new_x > to_x)
5097 {
5098 it->current_x = x;
5099 result = MOVE_X_REACHED;
5100 break;
5101 }
5102 else if (/* Lines are continued. */
5103 !it->truncate_lines_p
5104 && (/* And glyph doesn't fit on the line. */
5105 new_x > it->last_visible_x
5106 /* Or it fits exactly and we're on a window
5107 system frame. */
5108 || (new_x == it->last_visible_x
5109 && FRAME_WINDOW_P (it->f))))
5110 {
5111 if (/* IT->hpos == 0 means the very first glyph
5112 doesn't fit on the line, e.g. a wide image. */
5113 it->hpos == 0
5114 || (new_x == it->last_visible_x
5115 && FRAME_WINDOW_P (it->f)))
5116 {
5117 ++it->hpos;
5118 it->current_x = new_x;
5119 if (i == it->nglyphs - 1)
5120 set_iterator_to_next (it, 1);
5121 }
5122 else
5123 {
5124 it->current_x = x;
5125 it->max_ascent = ascent;
5126 it->max_descent = descent;
5127 }
5128
5129 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
5130 IT_CHARPOS (*it)));
5131 result = MOVE_LINE_CONTINUED;
5132 break;
5133 }
5134 else if (new_x > it->first_visible_x)
5135 {
5136 /* Glyph is visible. Increment number of glyphs that
5137 would be displayed. */
5138 ++it->hpos;
5139 }
5140 else
5141 {
5142 /* Glyph is completely off the left margin of the display
5143 area. Nothing to do. */
5144 }
5145 }
5146
5147 if (result != MOVE_UNDEFINED)
5148 break;
5149 }
5150 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
5151 {
5152 /* Stop when TO_X specified and reached. This check is
5153 necessary here because of lines consisting of a line end,
5154 only. The line end will not produce any glyphs and we
5155 would never get MOVE_X_REACHED. */
5156 xassert (it->nglyphs == 0);
5157 result = MOVE_X_REACHED;
5158 break;
5159 }
5160
5161 /* Is this a line end? If yes, we're done. */
5162 if (ITERATOR_AT_END_OF_LINE_P (it))
5163 {
5164 result = MOVE_NEWLINE_OR_CR;
5165 break;
5166 }
5167
5168 /* The current display element has been consumed. Advance
5169 to the next. */
5170 set_iterator_to_next (it, 1);
5171
5172 /* Stop if lines are truncated and IT's current x-position is
5173 past the right edge of the window now. */
5174 if (it->truncate_lines_p
5175 && it->current_x >= it->last_visible_x)
5176 {
5177 result = MOVE_LINE_TRUNCATED;
5178 break;
5179 }
5180 }
5181
5182 /* Restore the iterator settings altered at the beginning of this
5183 function. */
5184 it->glyph_row = saved_glyph_row;
5185 return result;
5186 }
5187
5188
5189 /* Move IT forward to a specified buffer position TO_CHARPOS, TO_X,
5190 TO_Y, TO_VPOS. OP is a bit-mask that specifies where to stop. See
5191 the description of enum move_operation_enum.
5192
5193 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
5194 screen line, this function will set IT to the next position >
5195 TO_CHARPOS. */
5196
5197 void
5198 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
5199 struct it *it;
5200 int to_charpos, to_x, to_y, to_vpos;
5201 int op;
5202 {
5203 enum move_it_result skip, skip2 = MOVE_X_REACHED;
5204 int line_height;
5205 int reached = 0;
5206
5207 for (;;)
5208 {
5209 if (op & MOVE_TO_VPOS)
5210 {
5211 /* If no TO_CHARPOS and no TO_X specified, stop at the
5212 start of the line TO_VPOS. */
5213 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
5214 {
5215 if (it->vpos == to_vpos)
5216 {
5217 reached = 1;
5218 break;
5219 }
5220 else
5221 skip = move_it_in_display_line_to (it, -1, -1, 0);
5222 }
5223 else
5224 {
5225 /* TO_VPOS >= 0 means stop at TO_X in the line at
5226 TO_VPOS, or at TO_POS, whichever comes first. */
5227 if (it->vpos == to_vpos)
5228 {
5229 reached = 2;
5230 break;
5231 }
5232
5233 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
5234
5235 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
5236 {
5237 reached = 3;
5238 break;
5239 }
5240 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
5241 {
5242 /* We have reached TO_X but not in the line we want. */
5243 skip = move_it_in_display_line_to (it, to_charpos,
5244 -1, MOVE_TO_POS);
5245 if (skip == MOVE_POS_MATCH_OR_ZV)
5246 {
5247 reached = 4;
5248 break;
5249 }
5250 }
5251 }
5252 }
5253 else if (op & MOVE_TO_Y)
5254 {
5255 struct it it_backup;
5256
5257 /* TO_Y specified means stop at TO_X in the line containing
5258 TO_Y---or at TO_CHARPOS if this is reached first. The
5259 problem is that we can't really tell whether the line
5260 contains TO_Y before we have completely scanned it, and
5261 this may skip past TO_X. What we do is to first scan to
5262 TO_X.
5263
5264 If TO_X is not specified, use a TO_X of zero. The reason
5265 is to make the outcome of this function more predictable.
5266 If we didn't use TO_X == 0, we would stop at the end of
5267 the line which is probably not what a caller would expect
5268 to happen. */
5269 skip = move_it_in_display_line_to (it, to_charpos,
5270 ((op & MOVE_TO_X)
5271 ? to_x : 0),
5272 (MOVE_TO_X
5273 | (op & MOVE_TO_POS)));
5274
5275 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
5276 if (skip == MOVE_POS_MATCH_OR_ZV)
5277 {
5278 reached = 5;
5279 break;
5280 }
5281
5282 /* If TO_X was reached, we would like to know whether TO_Y
5283 is in the line. This can only be said if we know the
5284 total line height which requires us to scan the rest of
5285 the line. */
5286 if (skip == MOVE_X_REACHED)
5287 {
5288 it_backup = *it;
5289 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
5290 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
5291 op & MOVE_TO_POS);
5292 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
5293 }
5294
5295 /* Now, decide whether TO_Y is in this line. */
5296 line_height = it->max_ascent + it->max_descent;
5297 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
5298
5299 if (to_y >= it->current_y
5300 && to_y < it->current_y + line_height)
5301 {
5302 if (skip == MOVE_X_REACHED)
5303 /* If TO_Y is in this line and TO_X was reached above,
5304 we scanned too far. We have to restore IT's settings
5305 to the ones before skipping. */
5306 *it = it_backup;
5307 reached = 6;
5308 }
5309 else if (skip == MOVE_X_REACHED)
5310 {
5311 skip = skip2;
5312 if (skip == MOVE_POS_MATCH_OR_ZV)
5313 reached = 7;
5314 }
5315
5316 if (reached)
5317 break;
5318 }
5319 else
5320 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
5321
5322 switch (skip)
5323 {
5324 case MOVE_POS_MATCH_OR_ZV:
5325 reached = 8;
5326 goto out;
5327
5328 case MOVE_NEWLINE_OR_CR:
5329 set_iterator_to_next (it, 1);
5330 it->continuation_lines_width = 0;
5331 break;
5332
5333 case MOVE_LINE_TRUNCATED:
5334 it->continuation_lines_width = 0;
5335 reseat_at_next_visible_line_start (it, 0);
5336 if ((op & MOVE_TO_POS) != 0
5337 && IT_CHARPOS (*it) > to_charpos)
5338 {
5339 reached = 9;
5340 goto out;
5341 }
5342 break;
5343
5344 case MOVE_LINE_CONTINUED:
5345 it->continuation_lines_width += it->current_x;
5346 break;
5347
5348 default:
5349 abort ();
5350 }
5351
5352 /* Reset/increment for the next run. */
5353 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
5354 it->current_x = it->hpos = 0;
5355 it->current_y += it->max_ascent + it->max_descent;
5356 ++it->vpos;
5357 last_height = it->max_ascent + it->max_descent;
5358 last_max_ascent = it->max_ascent;
5359 it->max_ascent = it->max_descent = 0;
5360 }
5361
5362 out:
5363
5364 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
5365 }
5366
5367
5368 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
5369
5370 If DY > 0, move IT backward at least that many pixels. DY = 0
5371 means move IT backward to the preceding line start or BEGV. This
5372 function may move over more than DY pixels if IT->current_y - DY
5373 ends up in the middle of a line; in this case IT->current_y will be
5374 set to the top of the line moved to. */
5375
5376 void
5377 move_it_vertically_backward (it, dy)
5378 struct it *it;
5379 int dy;
5380 {
5381 int nlines, h;
5382 struct it it2, it3;
5383 int start_pos = IT_CHARPOS (*it);
5384
5385 xassert (dy >= 0);
5386
5387 /* Estimate how many newlines we must move back. */
5388 nlines = max (1, dy / CANON_Y_UNIT (it->f));
5389
5390 /* Set the iterator's position that many lines back. */
5391 while (nlines-- && IT_CHARPOS (*it) > BEGV)
5392 back_to_previous_visible_line_start (it);
5393
5394 /* Reseat the iterator here. When moving backward, we don't want
5395 reseat to skip forward over invisible text, set up the iterator
5396 to deliver from overlay strings at the new position etc. So,
5397 use reseat_1 here. */
5398 reseat_1 (it, it->current.pos, 1);
5399
5400 /* We are now surely at a line start. */
5401 it->current_x = it->hpos = 0;
5402
5403 /* Move forward and see what y-distance we moved. First move to the
5404 start of the next line so that we get its height. We need this
5405 height to be able to tell whether we reached the specified
5406 y-distance. */
5407 it2 = *it;
5408 it2.max_ascent = it2.max_descent = 0;
5409 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
5410 MOVE_TO_POS | MOVE_TO_VPOS);
5411 xassert (IT_CHARPOS (*it) >= BEGV);
5412 it3 = it2;
5413
5414 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
5415 xassert (IT_CHARPOS (*it) >= BEGV);
5416 h = it2.current_y - it->current_y;
5417 nlines = it2.vpos - it->vpos;
5418
5419 /* Correct IT's y and vpos position. */
5420 it->vpos -= nlines;
5421 it->current_y -= h;
5422
5423 if (dy == 0)
5424 {
5425 /* DY == 0 means move to the start of the screen line. The
5426 value of nlines is > 0 if continuation lines were involved. */
5427 if (nlines > 0)
5428 move_it_by_lines (it, nlines, 1);
5429 xassert (IT_CHARPOS (*it) <= start_pos);
5430 }
5431 else if (nlines)
5432 {
5433 /* The y-position we try to reach. Note that h has been
5434 subtracted in front of the if-statement. */
5435 int target_y = it->current_y + h - dy;
5436 int y0 = it3.current_y;
5437 int y1 = line_bottom_y (&it3);
5438 int line_height = y1 - y0;
5439
5440 /* If we did not reach target_y, try to move further backward if
5441 we can. If we moved too far backward, try to move forward. */
5442 if (target_y < it->current_y
5443 /* This is heuristic. In a window that's 3 lines high, with
5444 a line height of 13 pixels each, recentering with point
5445 on the bottom line will try to move -39/2 = 19 pixels
5446 backward. Try to avoid moving into the first line. */
5447 && it->current_y - target_y > line_height / 3 * 2
5448 && IT_CHARPOS (*it) > BEGV)
5449 {
5450 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
5451 target_y - it->current_y));
5452 move_it_vertically (it, target_y - it->current_y);
5453 xassert (IT_CHARPOS (*it) >= BEGV);
5454 }
5455 else if (target_y >= it->current_y + line_height
5456 && IT_CHARPOS (*it) < ZV)
5457 {
5458 /* Should move forward by at least one line, maybe more. */
5459 do
5460 {
5461 move_it_by_lines (it, 1, 1);
5462 }
5463 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
5464
5465 xassert (IT_CHARPOS (*it) >= BEGV);
5466 }
5467 }
5468 }
5469
5470
5471 /* Move IT by a specified amount of pixel lines DY. DY negative means
5472 move backwards. DY = 0 means move to start of screen line. At the
5473 end, IT will be on the start of a screen line. */
5474
5475 void
5476 move_it_vertically (it, dy)
5477 struct it *it;
5478 int dy;
5479 {
5480 if (dy <= 0)
5481 move_it_vertically_backward (it, -dy);
5482 else if (dy > 0)
5483 {
5484 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
5485 move_it_to (it, ZV, -1, it->current_y + dy, -1,
5486 MOVE_TO_POS | MOVE_TO_Y);
5487 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
5488
5489 /* If buffer ends in ZV without a newline, move to the start of
5490 the line to satisfy the post-condition. */
5491 if (IT_CHARPOS (*it) == ZV
5492 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
5493 move_it_by_lines (it, 0, 0);
5494 }
5495 }
5496
5497
5498 /* Move iterator IT past the end of the text line it is in. */
5499
5500 void
5501 move_it_past_eol (it)
5502 struct it *it;
5503 {
5504 enum move_it_result rc;
5505
5506 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
5507 if (rc == MOVE_NEWLINE_OR_CR)
5508 set_iterator_to_next (it, 0);
5509 }
5510
5511
5512 #if 0 /* Currently not used. */
5513
5514 /* Return non-zero if some text between buffer positions START_CHARPOS
5515 and END_CHARPOS is invisible. IT->window is the window for text
5516 property lookup. */
5517
5518 static int
5519 invisible_text_between_p (it, start_charpos, end_charpos)
5520 struct it *it;
5521 int start_charpos, end_charpos;
5522 {
5523 Lisp_Object prop, limit;
5524 int invisible_found_p;
5525
5526 xassert (it != NULL && start_charpos <= end_charpos);
5527
5528 /* Is text at START invisible? */
5529 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
5530 it->window);
5531 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5532 invisible_found_p = 1;
5533 else
5534 {
5535 limit = Fnext_single_char_property_change (make_number (start_charpos),
5536 Qinvisible, Qnil,
5537 make_number (end_charpos));
5538 invisible_found_p = XFASTINT (limit) < end_charpos;
5539 }
5540
5541 return invisible_found_p;
5542 }
5543
5544 #endif /* 0 */
5545
5546
5547 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
5548 negative means move up. DVPOS == 0 means move to the start of the
5549 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
5550 NEED_Y_P is zero, IT->current_y will be left unchanged.
5551
5552 Further optimization ideas: If we would know that IT->f doesn't use
5553 a face with proportional font, we could be faster for
5554 truncate-lines nil. */
5555
5556 void
5557 move_it_by_lines (it, dvpos, need_y_p)
5558 struct it *it;
5559 int dvpos, need_y_p;
5560 {
5561 struct position pos;
5562
5563 if (!FRAME_WINDOW_P (it->f))
5564 {
5565 struct text_pos textpos;
5566
5567 /* We can use vmotion on frames without proportional fonts. */
5568 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
5569 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
5570 reseat (it, textpos, 1);
5571 it->vpos += pos.vpos;
5572 it->current_y += pos.vpos;
5573 }
5574 else if (dvpos == 0)
5575 {
5576 /* DVPOS == 0 means move to the start of the screen line. */
5577 move_it_vertically_backward (it, 0);
5578 xassert (it->current_x == 0 && it->hpos == 0);
5579 }
5580 else if (dvpos > 0)
5581 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
5582 else
5583 {
5584 struct it it2;
5585 int start_charpos, i;
5586
5587 /* Start at the beginning of the screen line containing IT's
5588 position. */
5589 move_it_vertically_backward (it, 0);
5590
5591 /* Go back -DVPOS visible lines and reseat the iterator there. */
5592 start_charpos = IT_CHARPOS (*it);
5593 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
5594 back_to_previous_visible_line_start (it);
5595 reseat (it, it->current.pos, 1);
5596 it->current_x = it->hpos = 0;
5597
5598 /* Above call may have moved too far if continuation lines
5599 are involved. Scan forward and see if it did. */
5600 it2 = *it;
5601 it2.vpos = it2.current_y = 0;
5602 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
5603 it->vpos -= it2.vpos;
5604 it->current_y -= it2.current_y;
5605 it->current_x = it->hpos = 0;
5606
5607 /* If we moved too far, move IT some lines forward. */
5608 if (it2.vpos > -dvpos)
5609 {
5610 int delta = it2.vpos + dvpos;
5611 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
5612 }
5613 }
5614 }
5615
5616
5617 \f
5618 /***********************************************************************
5619 Messages
5620 ***********************************************************************/
5621
5622
5623 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
5624 to *Messages*. */
5625
5626 void
5627 add_to_log (format, arg1, arg2)
5628 char *format;
5629 Lisp_Object arg1, arg2;
5630 {
5631 Lisp_Object args[3];
5632 Lisp_Object msg, fmt;
5633 char *buffer;
5634 int len;
5635 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5636
5637 /* Do nothing if called asynchronously. Inserting text into
5638 a buffer may call after-change-functions and alike and
5639 that would means running Lisp asynchronously. */
5640 if (handling_signal)
5641 return;
5642
5643 fmt = msg = Qnil;
5644 GCPRO4 (fmt, msg, arg1, arg2);
5645
5646 args[0] = fmt = build_string (format);
5647 args[1] = arg1;
5648 args[2] = arg2;
5649 msg = Fformat (3, args);
5650
5651 len = STRING_BYTES (XSTRING (msg)) + 1;
5652 buffer = (char *) alloca (len);
5653 bcopy (XSTRING (msg)->data, buffer, len);
5654
5655 message_dolog (buffer, len - 1, 1, 0);
5656 UNGCPRO;
5657 }
5658
5659
5660 /* Output a newline in the *Messages* buffer if "needs" one. */
5661
5662 void
5663 message_log_maybe_newline ()
5664 {
5665 if (message_log_need_newline)
5666 message_dolog ("", 0, 1, 0);
5667 }
5668
5669
5670 /* Add a string M of length NBYTES to the message log, optionally
5671 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
5672 nonzero, means interpret the contents of M as multibyte. This
5673 function calls low-level routines in order to bypass text property
5674 hooks, etc. which might not be safe to run. */
5675
5676 void
5677 message_dolog (m, nbytes, nlflag, multibyte)
5678 char *m;
5679 int nbytes, nlflag, multibyte;
5680 {
5681 if (!NILP (Vmessage_log_max))
5682 {
5683 struct buffer *oldbuf;
5684 Lisp_Object oldpoint, oldbegv, oldzv;
5685 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5686 int point_at_end = 0;
5687 int zv_at_end = 0;
5688 Lisp_Object old_deactivate_mark, tem;
5689 struct gcpro gcpro1;
5690
5691 old_deactivate_mark = Vdeactivate_mark;
5692 oldbuf = current_buffer;
5693 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
5694 current_buffer->undo_list = Qt;
5695
5696 oldpoint = message_dolog_marker1;
5697 set_marker_restricted (oldpoint, make_number (PT), Qnil);
5698 oldbegv = message_dolog_marker2;
5699 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
5700 oldzv = message_dolog_marker3;
5701 set_marker_restricted (oldzv, make_number (ZV), Qnil);
5702 GCPRO1 (old_deactivate_mark);
5703
5704 if (PT == Z)
5705 point_at_end = 1;
5706 if (ZV == Z)
5707 zv_at_end = 1;
5708
5709 BEGV = BEG;
5710 BEGV_BYTE = BEG_BYTE;
5711 ZV = Z;
5712 ZV_BYTE = Z_BYTE;
5713 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5714
5715 /* Insert the string--maybe converting multibyte to single byte
5716 or vice versa, so that all the text fits the buffer. */
5717 if (multibyte
5718 && NILP (current_buffer->enable_multibyte_characters))
5719 {
5720 int i, c, char_bytes;
5721 unsigned char work[1];
5722
5723 /* Convert a multibyte string to single-byte
5724 for the *Message* buffer. */
5725 for (i = 0; i < nbytes; i += nbytes)
5726 {
5727 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
5728 work[0] = (ASCII_CHAR_P (c)
5729 ? c
5730 : multibyte_char_to_unibyte (c, Qnil));
5731 insert_1_both (work, 1, 1, 1, 0, 0);
5732 }
5733 }
5734 else if (! multibyte
5735 && ! NILP (current_buffer->enable_multibyte_characters))
5736 {
5737 int i, c, char_bytes;
5738 unsigned char *msg = (unsigned char *) m;
5739 unsigned char str[MAX_MULTIBYTE_LENGTH];
5740 /* Convert a single-byte string to multibyte
5741 for the *Message* buffer. */
5742 for (i = 0; i < nbytes; i++)
5743 {
5744 c = unibyte_char_to_multibyte (msg[i]);
5745 char_bytes = CHAR_STRING (c, str);
5746 insert_1_both (str, 1, char_bytes, 1, 0, 0);
5747 }
5748 }
5749 else if (nbytes)
5750 insert_1 (m, nbytes, 1, 0, 0);
5751
5752 if (nlflag)
5753 {
5754 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
5755 insert_1 ("\n", 1, 1, 0, 0);
5756
5757 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
5758 this_bol = PT;
5759 this_bol_byte = PT_BYTE;
5760
5761 /* See if this line duplicates the previous one.
5762 If so, combine duplicates. */
5763 if (this_bol > BEG)
5764 {
5765 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
5766 prev_bol = PT;
5767 prev_bol_byte = PT_BYTE;
5768
5769 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
5770 this_bol, this_bol_byte);
5771 if (dup)
5772 {
5773 del_range_both (prev_bol, prev_bol_byte,
5774 this_bol, this_bol_byte, 0);
5775 if (dup > 1)
5776 {
5777 char dupstr[40];
5778 int duplen;
5779
5780 /* If you change this format, don't forget to also
5781 change message_log_check_duplicate. */
5782 sprintf (dupstr, " [%d times]", dup);
5783 duplen = strlen (dupstr);
5784 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
5785 insert_1 (dupstr, duplen, 1, 0, 1);
5786 }
5787 }
5788 }
5789
5790 /* If we have more than the desired maximum number of lines
5791 in the *Messages* buffer now, delete the oldest ones.
5792 This is safe because we don't have undo in this buffer. */
5793
5794 if (NATNUMP (Vmessage_log_max))
5795 {
5796 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
5797 -XFASTINT (Vmessage_log_max) - 1, 0);
5798 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
5799 }
5800 }
5801 BEGV = XMARKER (oldbegv)->charpos;
5802 BEGV_BYTE = marker_byte_position (oldbegv);
5803
5804 if (zv_at_end)
5805 {
5806 ZV = Z;
5807 ZV_BYTE = Z_BYTE;
5808 }
5809 else
5810 {
5811 ZV = XMARKER (oldzv)->charpos;
5812 ZV_BYTE = marker_byte_position (oldzv);
5813 }
5814
5815 if (point_at_end)
5816 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5817 else
5818 /* We can't do Fgoto_char (oldpoint) because it will run some
5819 Lisp code. */
5820 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
5821 XMARKER (oldpoint)->bytepos);
5822
5823 UNGCPRO;
5824 unchain_marker (oldpoint);
5825 unchain_marker (oldbegv);
5826 unchain_marker (oldzv);
5827
5828 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
5829 set_buffer_internal (oldbuf);
5830 if (NILP (tem))
5831 windows_or_buffers_changed = old_windows_or_buffers_changed;
5832 message_log_need_newline = !nlflag;
5833 Vdeactivate_mark = old_deactivate_mark;
5834 }
5835 }
5836
5837
5838 /* We are at the end of the buffer after just having inserted a newline.
5839 (Note: We depend on the fact we won't be crossing the gap.)
5840 Check to see if the most recent message looks a lot like the previous one.
5841 Return 0 if different, 1 if the new one should just replace it, or a
5842 value N > 1 if we should also append " [N times]". */
5843
5844 static int
5845 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
5846 int prev_bol, this_bol;
5847 int prev_bol_byte, this_bol_byte;
5848 {
5849 int i;
5850 int len = Z_BYTE - 1 - this_bol_byte;
5851 int seen_dots = 0;
5852 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
5853 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
5854
5855 for (i = 0; i < len; i++)
5856 {
5857 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
5858 seen_dots = 1;
5859 if (p1[i] != p2[i])
5860 return seen_dots;
5861 }
5862 p1 += len;
5863 if (*p1 == '\n')
5864 return 2;
5865 if (*p1++ == ' ' && *p1++ == '[')
5866 {
5867 int n = 0;
5868 while (*p1 >= '0' && *p1 <= '9')
5869 n = n * 10 + *p1++ - '0';
5870 if (strncmp (p1, " times]\n", 8) == 0)
5871 return n+1;
5872 }
5873 return 0;
5874 }
5875
5876
5877 /* Display an echo area message M with a specified length of NBYTES
5878 bytes. The string may include null characters. If M is 0, clear
5879 out any existing message, and let the mini-buffer text show
5880 through.
5881
5882 The buffer M must continue to exist until after the echo area gets
5883 cleared or some other message gets displayed there. This means do
5884 not pass text that is stored in a Lisp string; do not pass text in
5885 a buffer that was alloca'd. */
5886
5887 void
5888 message2 (m, nbytes, multibyte)
5889 char *m;
5890 int nbytes;
5891 int multibyte;
5892 {
5893 /* First flush out any partial line written with print. */
5894 message_log_maybe_newline ();
5895 if (m)
5896 message_dolog (m, nbytes, 1, multibyte);
5897 message2_nolog (m, nbytes, multibyte);
5898 }
5899
5900
5901 /* The non-logging counterpart of message2. */
5902
5903 void
5904 message2_nolog (m, nbytes, multibyte)
5905 char *m;
5906 int nbytes, multibyte;
5907 {
5908 struct frame *sf = SELECTED_FRAME ();
5909 message_enable_multibyte = multibyte;
5910
5911 if (noninteractive)
5912 {
5913 if (noninteractive_need_newline)
5914 putc ('\n', stderr);
5915 noninteractive_need_newline = 0;
5916 if (m)
5917 fwrite (m, nbytes, 1, stderr);
5918 if (cursor_in_echo_area == 0)
5919 fprintf (stderr, "\n");
5920 fflush (stderr);
5921 }
5922 /* A null message buffer means that the frame hasn't really been
5923 initialized yet. Error messages get reported properly by
5924 cmd_error, so this must be just an informative message; toss it. */
5925 else if (INTERACTIVE
5926 && sf->glyphs_initialized_p
5927 && FRAME_MESSAGE_BUF (sf))
5928 {
5929 Lisp_Object mini_window;
5930 struct frame *f;
5931
5932 /* Get the frame containing the mini-buffer
5933 that the selected frame is using. */
5934 mini_window = FRAME_MINIBUF_WINDOW (sf);
5935 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5936
5937 FRAME_SAMPLE_VISIBILITY (f);
5938 if (FRAME_VISIBLE_P (sf)
5939 && ! FRAME_VISIBLE_P (f))
5940 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
5941
5942 if (m)
5943 {
5944 set_message (m, Qnil, nbytes, multibyte);
5945 if (minibuffer_auto_raise)
5946 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5947 }
5948 else
5949 clear_message (1, 1);
5950
5951 do_pending_window_change (0);
5952 echo_area_display (1);
5953 do_pending_window_change (0);
5954 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5955 (*frame_up_to_date_hook) (f);
5956 }
5957 }
5958
5959
5960 /* Display an echo area message M with a specified length of NBYTES
5961 bytes. The string may include null characters. If M is not a
5962 string, clear out any existing message, and let the mini-buffer
5963 text show through. */
5964
5965 void
5966 message3 (m, nbytes, multibyte)
5967 Lisp_Object m;
5968 int nbytes;
5969 int multibyte;
5970 {
5971 struct gcpro gcpro1;
5972
5973 GCPRO1 (m);
5974
5975 /* First flush out any partial line written with print. */
5976 message_log_maybe_newline ();
5977 if (STRINGP (m))
5978 message_dolog (XSTRING (m)->data, nbytes, 1, multibyte);
5979 message3_nolog (m, nbytes, multibyte);
5980
5981 UNGCPRO;
5982 }
5983
5984
5985 /* The non-logging version of message3. */
5986
5987 void
5988 message3_nolog (m, nbytes, multibyte)
5989 Lisp_Object m;
5990 int nbytes, multibyte;
5991 {
5992 struct frame *sf = SELECTED_FRAME ();
5993 message_enable_multibyte = multibyte;
5994
5995 if (noninteractive)
5996 {
5997 if (noninteractive_need_newline)
5998 putc ('\n', stderr);
5999 noninteractive_need_newline = 0;
6000 if (STRINGP (m))
6001 fwrite (XSTRING (m)->data, nbytes, 1, stderr);
6002 if (cursor_in_echo_area == 0)
6003 fprintf (stderr, "\n");
6004 fflush (stderr);
6005 }
6006 /* A null message buffer means that the frame hasn't really been
6007 initialized yet. Error messages get reported properly by
6008 cmd_error, so this must be just an informative message; toss it. */
6009 else if (INTERACTIVE
6010 && sf->glyphs_initialized_p
6011 && FRAME_MESSAGE_BUF (sf))
6012 {
6013 Lisp_Object mini_window;
6014 Lisp_Object frame;
6015 struct frame *f;
6016
6017 /* Get the frame containing the mini-buffer
6018 that the selected frame is using. */
6019 mini_window = FRAME_MINIBUF_WINDOW (sf);
6020 frame = XWINDOW (mini_window)->frame;
6021 f = XFRAME (frame);
6022
6023 FRAME_SAMPLE_VISIBILITY (f);
6024 if (FRAME_VISIBLE_P (sf)
6025 && !FRAME_VISIBLE_P (f))
6026 Fmake_frame_visible (frame);
6027
6028 if (STRINGP (m) && XSTRING (m)->size)
6029 {
6030 set_message (NULL, m, nbytes, multibyte);
6031 if (minibuffer_auto_raise)
6032 Fraise_frame (frame);
6033 }
6034 else
6035 clear_message (1, 1);
6036
6037 do_pending_window_change (0);
6038 echo_area_display (1);
6039 do_pending_window_change (0);
6040 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6041 (*frame_up_to_date_hook) (f);
6042 }
6043 }
6044
6045
6046 /* Display a null-terminated echo area message M. If M is 0, clear
6047 out any existing message, and let the mini-buffer text show through.
6048
6049 The buffer M must continue to exist until after the echo area gets
6050 cleared or some other message gets displayed there. Do not pass
6051 text that is stored in a Lisp string. Do not pass text in a buffer
6052 that was alloca'd. */
6053
6054 void
6055 message1 (m)
6056 char *m;
6057 {
6058 message2 (m, (m ? strlen (m) : 0), 0);
6059 }
6060
6061
6062 /* The non-logging counterpart of message1. */
6063
6064 void
6065 message1_nolog (m)
6066 char *m;
6067 {
6068 message2_nolog (m, (m ? strlen (m) : 0), 0);
6069 }
6070
6071 /* Display a message M which contains a single %s
6072 which gets replaced with STRING. */
6073
6074 void
6075 message_with_string (m, string, log)
6076 char *m;
6077 Lisp_Object string;
6078 int log;
6079 {
6080 if (noninteractive)
6081 {
6082 if (m)
6083 {
6084 if (noninteractive_need_newline)
6085 putc ('\n', stderr);
6086 noninteractive_need_newline = 0;
6087 fprintf (stderr, m, XSTRING (string)->data);
6088 if (cursor_in_echo_area == 0)
6089 fprintf (stderr, "\n");
6090 fflush (stderr);
6091 }
6092 }
6093 else if (INTERACTIVE)
6094 {
6095 /* The frame whose minibuffer we're going to display the message on.
6096 It may be larger than the selected frame, so we need
6097 to use its buffer, not the selected frame's buffer. */
6098 Lisp_Object mini_window;
6099 struct frame *f, *sf = SELECTED_FRAME ();
6100
6101 /* Get the frame containing the minibuffer
6102 that the selected frame is using. */
6103 mini_window = FRAME_MINIBUF_WINDOW (sf);
6104 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6105
6106 /* A null message buffer means that the frame hasn't really been
6107 initialized yet. Error messages get reported properly by
6108 cmd_error, so this must be just an informative message; toss it. */
6109 if (FRAME_MESSAGE_BUF (f))
6110 {
6111 int len;
6112 char *a[1];
6113 a[0] = (char *) XSTRING (string)->data;
6114
6115 len = doprnt (FRAME_MESSAGE_BUF (f),
6116 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6117
6118 if (log)
6119 message2 (FRAME_MESSAGE_BUF (f), len,
6120 STRING_MULTIBYTE (string));
6121 else
6122 message2_nolog (FRAME_MESSAGE_BUF (f), len,
6123 STRING_MULTIBYTE (string));
6124
6125 /* Print should start at the beginning of the message
6126 buffer next time. */
6127 message_buf_print = 0;
6128 }
6129 }
6130 }
6131
6132
6133 /* Dump an informative message to the minibuf. If M is 0, clear out
6134 any existing message, and let the mini-buffer text show through. */
6135
6136 /* VARARGS 1 */
6137 void
6138 message (m, a1, a2, a3)
6139 char *m;
6140 EMACS_INT a1, a2, a3;
6141 {
6142 if (noninteractive)
6143 {
6144 if (m)
6145 {
6146 if (noninteractive_need_newline)
6147 putc ('\n', stderr);
6148 noninteractive_need_newline = 0;
6149 fprintf (stderr, m, a1, a2, a3);
6150 if (cursor_in_echo_area == 0)
6151 fprintf (stderr, "\n");
6152 fflush (stderr);
6153 }
6154 }
6155 else if (INTERACTIVE)
6156 {
6157 /* The frame whose mini-buffer we're going to display the message
6158 on. It may be larger than the selected frame, so we need to
6159 use its buffer, not the selected frame's buffer. */
6160 Lisp_Object mini_window;
6161 struct frame *f, *sf = SELECTED_FRAME ();
6162
6163 /* Get the frame containing the mini-buffer
6164 that the selected frame is using. */
6165 mini_window = FRAME_MINIBUF_WINDOW (sf);
6166 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6167
6168 /* A null message buffer means that the frame hasn't really been
6169 initialized yet. Error messages get reported properly by
6170 cmd_error, so this must be just an informative message; toss
6171 it. */
6172 if (FRAME_MESSAGE_BUF (f))
6173 {
6174 if (m)
6175 {
6176 int len;
6177 #ifdef NO_ARG_ARRAY
6178 char *a[3];
6179 a[0] = (char *) a1;
6180 a[1] = (char *) a2;
6181 a[2] = (char *) a3;
6182
6183 len = doprnt (FRAME_MESSAGE_BUF (f),
6184 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6185 #else
6186 len = doprnt (FRAME_MESSAGE_BUF (f),
6187 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
6188 (char **) &a1);
6189 #endif /* NO_ARG_ARRAY */
6190
6191 message2 (FRAME_MESSAGE_BUF (f), len, 0);
6192 }
6193 else
6194 message1 (0);
6195
6196 /* Print should start at the beginning of the message
6197 buffer next time. */
6198 message_buf_print = 0;
6199 }
6200 }
6201 }
6202
6203
6204 /* The non-logging version of message. */
6205
6206 void
6207 message_nolog (m, a1, a2, a3)
6208 char *m;
6209 EMACS_INT a1, a2, a3;
6210 {
6211 Lisp_Object old_log_max;
6212 old_log_max = Vmessage_log_max;
6213 Vmessage_log_max = Qnil;
6214 message (m, a1, a2, a3);
6215 Vmessage_log_max = old_log_max;
6216 }
6217
6218
6219 /* Display the current message in the current mini-buffer. This is
6220 only called from error handlers in process.c, and is not time
6221 critical. */
6222
6223 void
6224 update_echo_area ()
6225 {
6226 if (!NILP (echo_area_buffer[0]))
6227 {
6228 Lisp_Object string;
6229 string = Fcurrent_message ();
6230 message3 (string, XSTRING (string)->size,
6231 !NILP (current_buffer->enable_multibyte_characters));
6232 }
6233 }
6234
6235
6236 /* Make sure echo area buffers in echo_buffers[] are life. If they
6237 aren't, make new ones. */
6238
6239 static void
6240 ensure_echo_area_buffers ()
6241 {
6242 int i;
6243
6244 for (i = 0; i < 2; ++i)
6245 if (!BUFFERP (echo_buffer[i])
6246 || NILP (XBUFFER (echo_buffer[i])->name))
6247 {
6248 char name[30];
6249 Lisp_Object old_buffer;
6250 int j;
6251
6252 old_buffer = echo_buffer[i];
6253 sprintf (name, " *Echo Area %d*", i);
6254 echo_buffer[i] = Fget_buffer_create (build_string (name));
6255 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
6256
6257 for (j = 0; j < 2; ++j)
6258 if (EQ (old_buffer, echo_area_buffer[j]))
6259 echo_area_buffer[j] = echo_buffer[i];
6260 }
6261 }
6262
6263
6264 /* Call FN with args A1..A4 with either the current or last displayed
6265 echo_area_buffer as current buffer.
6266
6267 WHICH zero means use the current message buffer
6268 echo_area_buffer[0]. If that is nil, choose a suitable buffer
6269 from echo_buffer[] and clear it.
6270
6271 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
6272 suitable buffer from echo_buffer[] and clear it.
6273
6274 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
6275 that the current message becomes the last displayed one, make
6276 choose a suitable buffer for echo_area_buffer[0], and clear it.
6277
6278 Value is what FN returns. */
6279
6280 static int
6281 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
6282 struct window *w;
6283 int which;
6284 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
6285 EMACS_INT a1;
6286 Lisp_Object a2;
6287 EMACS_INT a3, a4;
6288 {
6289 Lisp_Object buffer;
6290 int this_one, the_other, clear_buffer_p, rc;
6291 int count = BINDING_STACK_SIZE ();
6292
6293 /* If buffers aren't life, make new ones. */
6294 ensure_echo_area_buffers ();
6295
6296 clear_buffer_p = 0;
6297
6298 if (which == 0)
6299 this_one = 0, the_other = 1;
6300 else if (which > 0)
6301 this_one = 1, the_other = 0;
6302 else
6303 {
6304 this_one = 0, the_other = 1;
6305 clear_buffer_p = 1;
6306
6307 /* We need a fresh one in case the current echo buffer equals
6308 the one containing the last displayed echo area message. */
6309 if (!NILP (echo_area_buffer[this_one])
6310 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
6311 echo_area_buffer[this_one] = Qnil;
6312 }
6313
6314 /* Choose a suitable buffer from echo_buffer[] is we don't
6315 have one. */
6316 if (NILP (echo_area_buffer[this_one]))
6317 {
6318 echo_area_buffer[this_one]
6319 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
6320 ? echo_buffer[the_other]
6321 : echo_buffer[this_one]);
6322 clear_buffer_p = 1;
6323 }
6324
6325 buffer = echo_area_buffer[this_one];
6326
6327 /* Don't get confused by reusing the buffer used for echoing
6328 for a different purpose. */
6329 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
6330 cancel_echoing ();
6331
6332 record_unwind_protect (unwind_with_echo_area_buffer,
6333 with_echo_area_buffer_unwind_data (w));
6334
6335 /* Make the echo area buffer current. Note that for display
6336 purposes, it is not necessary that the displayed window's buffer
6337 == current_buffer, except for text property lookup. So, let's
6338 only set that buffer temporarily here without doing a full
6339 Fset_window_buffer. We must also change w->pointm, though,
6340 because otherwise an assertions in unshow_buffer fails, and Emacs
6341 aborts. */
6342 set_buffer_internal_1 (XBUFFER (buffer));
6343 if (w)
6344 {
6345 w->buffer = buffer;
6346 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
6347 }
6348
6349 current_buffer->undo_list = Qt;
6350 current_buffer->read_only = Qnil;
6351 specbind (Qinhibit_read_only, Qt);
6352 specbind (Qinhibit_modification_hooks, Qt);
6353
6354 if (clear_buffer_p && Z > BEG)
6355 del_range (BEG, Z);
6356
6357 xassert (BEGV >= BEG);
6358 xassert (ZV <= Z && ZV >= BEGV);
6359
6360 rc = fn (a1, a2, a3, a4);
6361
6362 xassert (BEGV >= BEG);
6363 xassert (ZV <= Z && ZV >= BEGV);
6364
6365 unbind_to (count, Qnil);
6366 return rc;
6367 }
6368
6369
6370 /* Save state that should be preserved around the call to the function
6371 FN called in with_echo_area_buffer. */
6372
6373 static Lisp_Object
6374 with_echo_area_buffer_unwind_data (w)
6375 struct window *w;
6376 {
6377 int i = 0;
6378 Lisp_Object vector;
6379
6380 /* Reduce consing by keeping one vector in
6381 Vwith_echo_area_save_vector. */
6382 vector = Vwith_echo_area_save_vector;
6383 Vwith_echo_area_save_vector = Qnil;
6384
6385 if (NILP (vector))
6386 vector = Fmake_vector (make_number (7), Qnil);
6387
6388 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
6389 AREF (vector, i) = Vdeactivate_mark, ++i;
6390 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
6391
6392 if (w)
6393 {
6394 XSETWINDOW (AREF (vector, i), w); ++i;
6395 AREF (vector, i) = w->buffer; ++i;
6396 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
6397 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
6398 }
6399 else
6400 {
6401 int end = i + 4;
6402 for (; i < end; ++i)
6403 AREF (vector, i) = Qnil;
6404 }
6405
6406 xassert (i == ASIZE (vector));
6407 return vector;
6408 }
6409
6410
6411 /* Restore global state from VECTOR which was created by
6412 with_echo_area_buffer_unwind_data. */
6413
6414 static Lisp_Object
6415 unwind_with_echo_area_buffer (vector)
6416 Lisp_Object vector;
6417 {
6418 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
6419 Vdeactivate_mark = AREF (vector, 1);
6420 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
6421
6422 if (WINDOWP (AREF (vector, 3)))
6423 {
6424 struct window *w;
6425 Lisp_Object buffer, charpos, bytepos;
6426
6427 w = XWINDOW (AREF (vector, 3));
6428 buffer = AREF (vector, 4);
6429 charpos = AREF (vector, 5);
6430 bytepos = AREF (vector, 6);
6431
6432 w->buffer = buffer;
6433 set_marker_both (w->pointm, buffer,
6434 XFASTINT (charpos), XFASTINT (bytepos));
6435 }
6436
6437 Vwith_echo_area_save_vector = vector;
6438 return Qnil;
6439 }
6440
6441
6442 /* Set up the echo area for use by print functions. MULTIBYTE_P
6443 non-zero means we will print multibyte. */
6444
6445 void
6446 setup_echo_area_for_printing (multibyte_p)
6447 int multibyte_p;
6448 {
6449 ensure_echo_area_buffers ();
6450
6451 if (!message_buf_print)
6452 {
6453 /* A message has been output since the last time we printed.
6454 Choose a fresh echo area buffer. */
6455 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6456 echo_area_buffer[0] = echo_buffer[1];
6457 else
6458 echo_area_buffer[0] = echo_buffer[0];
6459
6460 /* Switch to that buffer and clear it. */
6461 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6462 current_buffer->truncate_lines = Qnil;
6463
6464 if (Z > BEG)
6465 {
6466 int count = BINDING_STACK_SIZE ();
6467 specbind (Qinhibit_read_only, Qt);
6468 del_range (BEG, Z);
6469 unbind_to (count, Qnil);
6470 }
6471 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6472
6473 /* Set up the buffer for the multibyteness we need. */
6474 if (multibyte_p
6475 != !NILP (current_buffer->enable_multibyte_characters))
6476 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil, Qnil);
6477
6478 /* Raise the frame containing the echo area. */
6479 if (minibuffer_auto_raise)
6480 {
6481 struct frame *sf = SELECTED_FRAME ();
6482 Lisp_Object mini_window;
6483 mini_window = FRAME_MINIBUF_WINDOW (sf);
6484 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6485 }
6486
6487 message_log_maybe_newline ();
6488 message_buf_print = 1;
6489 }
6490 else
6491 {
6492 if (NILP (echo_area_buffer[0]))
6493 {
6494 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6495 echo_area_buffer[0] = echo_buffer[1];
6496 else
6497 echo_area_buffer[0] = echo_buffer[0];
6498 }
6499
6500 if (current_buffer != XBUFFER (echo_area_buffer[0]))
6501 {
6502 /* Someone switched buffers between print requests. */
6503 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6504 current_buffer->truncate_lines = Qnil;
6505 }
6506 }
6507 }
6508
6509
6510 /* Display an echo area message in window W. Value is non-zero if W's
6511 height is changed. If display_last_displayed_message_p is
6512 non-zero, display the message that was last displayed, otherwise
6513 display the current message. */
6514
6515 static int
6516 display_echo_area (w)
6517 struct window *w;
6518 {
6519 int i, no_message_p, window_height_changed_p, count;
6520
6521 /* Temporarily disable garbage collections while displaying the echo
6522 area. This is done because a GC can print a message itself.
6523 That message would modify the echo area buffer's contents while a
6524 redisplay of the buffer is going on, and seriously confuse
6525 redisplay. */
6526 count = inhibit_garbage_collection ();
6527
6528 /* If there is no message, we must call display_echo_area_1
6529 nevertheless because it resizes the window. But we will have to
6530 reset the echo_area_buffer in question to nil at the end because
6531 with_echo_area_buffer will sets it to an empty buffer. */
6532 i = display_last_displayed_message_p ? 1 : 0;
6533 no_message_p = NILP (echo_area_buffer[i]);
6534
6535 window_height_changed_p
6536 = with_echo_area_buffer (w, display_last_displayed_message_p,
6537 display_echo_area_1,
6538 (EMACS_INT) w, Qnil, 0, 0);
6539
6540 if (no_message_p)
6541 echo_area_buffer[i] = Qnil;
6542
6543 unbind_to (count, Qnil);
6544 return window_height_changed_p;
6545 }
6546
6547
6548 /* Helper for display_echo_area. Display the current buffer which
6549 contains the current echo area message in window W, a mini-window,
6550 a pointer to which is passed in A1. A2..A4 are currently not used.
6551 Change the height of W so that all of the message is displayed.
6552 Value is non-zero if height of W was changed. */
6553
6554 static int
6555 display_echo_area_1 (a1, a2, a3, a4)
6556 EMACS_INT a1;
6557 Lisp_Object a2;
6558 EMACS_INT a3, a4;
6559 {
6560 struct window *w = (struct window *) a1;
6561 Lisp_Object window;
6562 struct text_pos start;
6563 int window_height_changed_p = 0;
6564
6565 /* Do this before displaying, so that we have a large enough glyph
6566 matrix for the display. */
6567 window_height_changed_p = resize_mini_window (w, 0);
6568
6569 /* Display. */
6570 clear_glyph_matrix (w->desired_matrix);
6571 XSETWINDOW (window, w);
6572 SET_TEXT_POS (start, BEG, BEG_BYTE);
6573 try_window (window, start);
6574
6575 return window_height_changed_p;
6576 }
6577
6578
6579 /* Resize the echo area window to exactly the size needed for the
6580 currently displayed message, if there is one. If a mini-buffer
6581 is active, don't shrink it. */
6582
6583 void
6584 resize_echo_area_exactly ()
6585 {
6586 if (BUFFERP (echo_area_buffer[0])
6587 && WINDOWP (echo_area_window))
6588 {
6589 struct window *w = XWINDOW (echo_area_window);
6590 int resized_p;
6591 Lisp_Object resize_exactly;
6592
6593 if (minibuf_level == 0)
6594 resize_exactly = Qt;
6595 else
6596 resize_exactly = Qnil;
6597
6598 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
6599 (EMACS_INT) w, resize_exactly, 0, 0);
6600 if (resized_p)
6601 {
6602 ++windows_or_buffers_changed;
6603 ++update_mode_lines;
6604 redisplay_internal (0);
6605 }
6606 }
6607 }
6608
6609
6610 /* Callback function for with_echo_area_buffer, when used from
6611 resize_echo_area_exactly. A1 contains a pointer to the window to
6612 resize, EXACTLY non-nil means resize the mini-window exactly to the
6613 size of the text displayed. A3 and A4 are not used. Value is what
6614 resize_mini_window returns. */
6615
6616 static int
6617 resize_mini_window_1 (a1, exactly, a3, a4)
6618 EMACS_INT a1;
6619 Lisp_Object exactly;
6620 EMACS_INT a3, a4;
6621 {
6622 return resize_mini_window ((struct window *) a1, !NILP (exactly));
6623 }
6624
6625
6626 /* Resize mini-window W to fit the size of its contents. EXACT:P
6627 means size the window exactly to the size needed. Otherwise, it's
6628 only enlarged until W's buffer is empty. Value is non-zero if
6629 the window height has been changed. */
6630
6631 int
6632 resize_mini_window (w, exact_p)
6633 struct window *w;
6634 int exact_p;
6635 {
6636 struct frame *f = XFRAME (w->frame);
6637 int window_height_changed_p = 0;
6638
6639 xassert (MINI_WINDOW_P (w));
6640
6641 /* Don't resize windows while redisplaying a window; it would
6642 confuse redisplay functions when the size of the window they are
6643 displaying changes from under them. Such a resizing can happen,
6644 for instance, when which-func prints a long message while
6645 we are running fontification-functions. We're running these
6646 functions with safe_call which binds inhibit-redisplay to t. */
6647 if (!NILP (Vinhibit_redisplay))
6648 return 0;
6649
6650 /* Nil means don't try to resize. */
6651 if (NILP (Vresize_mini_windows)
6652 || (FRAME_X_P (f) && f->output_data.x == NULL))
6653 return 0;
6654
6655 if (!FRAME_MINIBUF_ONLY_P (f))
6656 {
6657 struct it it;
6658 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
6659 int total_height = XFASTINT (root->height) + XFASTINT (w->height);
6660 int height, max_height;
6661 int unit = CANON_Y_UNIT (f);
6662 struct text_pos start;
6663 struct buffer *old_current_buffer = NULL;
6664
6665 if (current_buffer != XBUFFER (w->buffer))
6666 {
6667 old_current_buffer = current_buffer;
6668 set_buffer_internal (XBUFFER (w->buffer));
6669 }
6670
6671 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
6672
6673 /* Compute the max. number of lines specified by the user. */
6674 if (FLOATP (Vmax_mini_window_height))
6675 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_HEIGHT (f);
6676 else if (INTEGERP (Vmax_mini_window_height))
6677 max_height = XINT (Vmax_mini_window_height);
6678 else
6679 max_height = total_height / 4;
6680
6681 /* Correct that max. height if it's bogus. */
6682 max_height = max (1, max_height);
6683 max_height = min (total_height, max_height);
6684
6685 /* Find out the height of the text in the window. */
6686 if (it.truncate_lines_p)
6687 height = 1;
6688 else
6689 {
6690 last_height = 0;
6691 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
6692 if (it.max_ascent == 0 && it.max_descent == 0)
6693 height = it.current_y + last_height;
6694 else
6695 height = it.current_y + it.max_ascent + it.max_descent;
6696 height -= it.extra_line_spacing;
6697 height = (height + unit - 1) / unit;
6698 }
6699
6700 /* Compute a suitable window start. */
6701 if (height > max_height)
6702 {
6703 height = max_height;
6704 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
6705 move_it_vertically_backward (&it, (height - 1) * unit);
6706 start = it.current.pos;
6707 }
6708 else
6709 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
6710 SET_MARKER_FROM_TEXT_POS (w->start, start);
6711
6712 if (EQ (Vresize_mini_windows, Qgrow_only))
6713 {
6714 /* Let it grow only, until we display an empty message, in which
6715 case the window shrinks again. */
6716 if (height > XFASTINT (w->height))
6717 {
6718 int old_height = XFASTINT (w->height);
6719 freeze_window_starts (f, 1);
6720 grow_mini_window (w, height - XFASTINT (w->height));
6721 window_height_changed_p = XFASTINT (w->height) != old_height;
6722 }
6723 else if (height < XFASTINT (w->height)
6724 && (exact_p || BEGV == ZV))
6725 {
6726 int old_height = XFASTINT (w->height);
6727 freeze_window_starts (f, 0);
6728 shrink_mini_window (w);
6729 window_height_changed_p = XFASTINT (w->height) != old_height;
6730 }
6731 }
6732 else
6733 {
6734 /* Always resize to exact size needed. */
6735 if (height > XFASTINT (w->height))
6736 {
6737 int old_height = XFASTINT (w->height);
6738 freeze_window_starts (f, 1);
6739 grow_mini_window (w, height - XFASTINT (w->height));
6740 window_height_changed_p = XFASTINT (w->height) != old_height;
6741 }
6742 else if (height < XFASTINT (w->height))
6743 {
6744 int old_height = XFASTINT (w->height);
6745 freeze_window_starts (f, 0);
6746 shrink_mini_window (w);
6747
6748 if (height)
6749 {
6750 freeze_window_starts (f, 1);
6751 grow_mini_window (w, height - XFASTINT (w->height));
6752 }
6753
6754 window_height_changed_p = XFASTINT (w->height) != old_height;
6755 }
6756 }
6757
6758 if (old_current_buffer)
6759 set_buffer_internal (old_current_buffer);
6760 }
6761
6762 return window_height_changed_p;
6763 }
6764
6765
6766 /* Value is the current message, a string, or nil if there is no
6767 current message. */
6768
6769 Lisp_Object
6770 current_message ()
6771 {
6772 Lisp_Object msg;
6773
6774 if (NILP (echo_area_buffer[0]))
6775 msg = Qnil;
6776 else
6777 {
6778 with_echo_area_buffer (0, 0, current_message_1,
6779 (EMACS_INT) &msg, Qnil, 0, 0);
6780 if (NILP (msg))
6781 echo_area_buffer[0] = Qnil;
6782 }
6783
6784 return msg;
6785 }
6786
6787
6788 static int
6789 current_message_1 (a1, a2, a3, a4)
6790 EMACS_INT a1;
6791 Lisp_Object a2;
6792 EMACS_INT a3, a4;
6793 {
6794 Lisp_Object *msg = (Lisp_Object *) a1;
6795
6796 if (Z > BEG)
6797 *msg = make_buffer_string (BEG, Z, 1);
6798 else
6799 *msg = Qnil;
6800 return 0;
6801 }
6802
6803
6804 /* Push the current message on Vmessage_stack for later restauration
6805 by restore_message. Value is non-zero if the current message isn't
6806 empty. This is a relatively infrequent operation, so it's not
6807 worth optimizing. */
6808
6809 int
6810 push_message ()
6811 {
6812 Lisp_Object msg;
6813 msg = current_message ();
6814 Vmessage_stack = Fcons (msg, Vmessage_stack);
6815 return STRINGP (msg);
6816 }
6817
6818
6819 /* Handler for record_unwind_protect calling pop_message. */
6820
6821 Lisp_Object
6822 push_message_unwind (dummy)
6823 Lisp_Object dummy;
6824 {
6825 pop_message ();
6826 return Qnil;
6827 }
6828
6829
6830 /* Restore message display from the top of Vmessage_stack. */
6831
6832 void
6833 restore_message ()
6834 {
6835 Lisp_Object msg;
6836
6837 xassert (CONSP (Vmessage_stack));
6838 msg = XCAR (Vmessage_stack);
6839 if (STRINGP (msg))
6840 message3_nolog (msg, STRING_BYTES (XSTRING (msg)), STRING_MULTIBYTE (msg));
6841 else
6842 message3_nolog (msg, 0, 0);
6843 }
6844
6845
6846 /* Pop the top-most entry off Vmessage_stack. */
6847
6848 void
6849 pop_message ()
6850 {
6851 xassert (CONSP (Vmessage_stack));
6852 Vmessage_stack = XCDR (Vmessage_stack);
6853 }
6854
6855
6856 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
6857 exits. If the stack is not empty, we have a missing pop_message
6858 somewhere. */
6859
6860 void
6861 check_message_stack ()
6862 {
6863 if (!NILP (Vmessage_stack))
6864 abort ();
6865 }
6866
6867
6868 /* Truncate to NCHARS what will be displayed in the echo area the next
6869 time we display it---but don't redisplay it now. */
6870
6871 void
6872 truncate_echo_area (nchars)
6873 int nchars;
6874 {
6875 if (nchars == 0)
6876 echo_area_buffer[0] = Qnil;
6877 /* A null message buffer means that the frame hasn't really been
6878 initialized yet. Error messages get reported properly by
6879 cmd_error, so this must be just an informative message; toss it. */
6880 else if (!noninteractive
6881 && INTERACTIVE
6882 && !NILP (echo_area_buffer[0]))
6883 {
6884 struct frame *sf = SELECTED_FRAME ();
6885 if (FRAME_MESSAGE_BUF (sf))
6886 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
6887 }
6888 }
6889
6890
6891 /* Helper function for truncate_echo_area. Truncate the current
6892 message to at most NCHARS characters. */
6893
6894 static int
6895 truncate_message_1 (nchars, a2, a3, a4)
6896 EMACS_INT nchars;
6897 Lisp_Object a2;
6898 EMACS_INT a3, a4;
6899 {
6900 if (BEG + nchars < Z)
6901 del_range (BEG + nchars, Z);
6902 if (Z == BEG)
6903 echo_area_buffer[0] = Qnil;
6904 return 0;
6905 }
6906
6907
6908 /* Set the current message to a substring of S or STRING.
6909
6910 If STRING is a Lisp string, set the message to the first NBYTES
6911 bytes from STRING. NBYTES zero means use the whole string. If
6912 STRING is multibyte, the message will be displayed multibyte.
6913
6914 If S is not null, set the message to the first LEN bytes of S. LEN
6915 zero means use the whole string. MULTIBYTE_P non-zero means S is
6916 multibyte. Display the message multibyte in that case. */
6917
6918 void
6919 set_message (s, string, nbytes, multibyte_p)
6920 char *s;
6921 Lisp_Object string;
6922 int nbytes, multibyte_p;
6923 {
6924 message_enable_multibyte
6925 = ((s && multibyte_p)
6926 || (STRINGP (string) && STRING_MULTIBYTE (string)));
6927
6928 with_echo_area_buffer (0, -1, set_message_1,
6929 (EMACS_INT) s, string, nbytes, multibyte_p);
6930 message_buf_print = 0;
6931 help_echo_showing_p = 0;
6932 }
6933
6934
6935 /* Helper function for set_message. Arguments have the same meaning
6936 as there, with A1 corresponding to S and A2 corresponding to STRING
6937 This function is called with the echo area buffer being
6938 current. */
6939
6940 static int
6941 set_message_1 (a1, a2, nbytes, multibyte_p)
6942 EMACS_INT a1;
6943 Lisp_Object a2;
6944 EMACS_INT nbytes, multibyte_p;
6945 {
6946 char *s = (char *) a1;
6947 Lisp_Object string = a2;
6948
6949 xassert (BEG == Z);
6950
6951 /* Change multibyteness of the echo buffer appropriately. */
6952 if (message_enable_multibyte
6953 != !NILP (current_buffer->enable_multibyte_characters))
6954 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil, Qnil);
6955
6956 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
6957
6958 /* Insert new message at BEG. */
6959 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6960
6961 if (STRINGP (string))
6962 {
6963 int nchars;
6964
6965 if (nbytes == 0)
6966 nbytes = XSTRING (string)->size_byte;
6967 nchars = string_byte_to_char (string, nbytes);
6968
6969 /* This function takes care of single/multibyte conversion. We
6970 just have to ensure that the echo area buffer has the right
6971 setting of enable_multibyte_characters. */
6972 insert_from_string (string, 0, 0, nchars, nbytes, 1);
6973 }
6974 else if (s)
6975 {
6976 if (nbytes == 0)
6977 nbytes = strlen (s);
6978
6979 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
6980 {
6981 /* Convert from multi-byte to single-byte. */
6982 int i, c, n;
6983 unsigned char work[1];
6984
6985 /* Convert a multibyte string to single-byte. */
6986 for (i = 0; i < nbytes; i += n)
6987 {
6988 c = string_char_and_length (s + i, nbytes - i, &n);
6989 work[0] = (ASCII_CHAR_P (c)
6990 ? c
6991 : multibyte_char_to_unibyte (c, Qnil));
6992 insert_1_both (work, 1, 1, 1, 0, 0);
6993 }
6994 }
6995 else if (!multibyte_p
6996 && !NILP (current_buffer->enable_multibyte_characters))
6997 {
6998 /* Convert from single-byte to multi-byte. */
6999 int i, c, n;
7000 unsigned char *msg = (unsigned char *) s;
7001 unsigned char str[MAX_MULTIBYTE_LENGTH];
7002
7003 /* Convert a single-byte string to multibyte. */
7004 for (i = 0; i < nbytes; i++)
7005 {
7006 c = unibyte_char_to_multibyte (msg[i]);
7007 n = CHAR_STRING (c, str);
7008 insert_1_both (str, 1, n, 1, 0, 0);
7009 }
7010 }
7011 else
7012 insert_1 (s, nbytes, 1, 0, 0);
7013 }
7014
7015 return 0;
7016 }
7017
7018
7019 /* Clear messages. CURRENT_P non-zero means clear the current
7020 message. LAST_DISPLAYED_P non-zero means clear the message
7021 last displayed. */
7022
7023 void
7024 clear_message (current_p, last_displayed_p)
7025 int current_p, last_displayed_p;
7026 {
7027 if (current_p)
7028 {
7029 echo_area_buffer[0] = Qnil;
7030 message_cleared_p = 1;
7031 }
7032
7033 if (last_displayed_p)
7034 echo_area_buffer[1] = Qnil;
7035
7036 message_buf_print = 0;
7037 }
7038
7039 /* Clear garbaged frames.
7040
7041 This function is used where the old redisplay called
7042 redraw_garbaged_frames which in turn called redraw_frame which in
7043 turn called clear_frame. The call to clear_frame was a source of
7044 flickering. I believe a clear_frame is not necessary. It should
7045 suffice in the new redisplay to invalidate all current matrices,
7046 and ensure a complete redisplay of all windows. */
7047
7048 static void
7049 clear_garbaged_frames ()
7050 {
7051 if (frame_garbaged)
7052 {
7053 Lisp_Object tail, frame;
7054
7055 FOR_EACH_FRAME (tail, frame)
7056 {
7057 struct frame *f = XFRAME (frame);
7058
7059 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
7060 {
7061 if (f->resized_p)
7062 Fredraw_frame (frame);
7063 clear_current_matrices (f);
7064 f->garbaged = 0;
7065 f->resized_p = 0;
7066 }
7067 }
7068
7069 frame_garbaged = 0;
7070 ++windows_or_buffers_changed;
7071 }
7072 }
7073
7074
7075 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
7076 is non-zero update selected_frame. Value is non-zero if the
7077 mini-windows height has been changed. */
7078
7079 static int
7080 echo_area_display (update_frame_p)
7081 int update_frame_p;
7082 {
7083 Lisp_Object mini_window;
7084 struct window *w;
7085 struct frame *f;
7086 int window_height_changed_p = 0;
7087 struct frame *sf = SELECTED_FRAME ();
7088
7089 mini_window = FRAME_MINIBUF_WINDOW (sf);
7090 w = XWINDOW (mini_window);
7091 f = XFRAME (WINDOW_FRAME (w));
7092
7093 /* Don't display if frame is invisible or not yet initialized. */
7094 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
7095 return 0;
7096
7097 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
7098 #ifndef macintosh
7099 #ifdef HAVE_WINDOW_SYSTEM
7100 /* When Emacs starts, selected_frame may be a visible terminal
7101 frame, even if we run under a window system. If we let this
7102 through, a message would be displayed on the terminal. */
7103 if (EQ (selected_frame, Vterminal_frame)
7104 && !NILP (Vwindow_system))
7105 return 0;
7106 #endif /* HAVE_WINDOW_SYSTEM */
7107 #endif
7108
7109 /* Redraw garbaged frames. */
7110 if (frame_garbaged)
7111 clear_garbaged_frames ();
7112
7113 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
7114 {
7115 echo_area_window = mini_window;
7116 window_height_changed_p = display_echo_area (w);
7117 w->must_be_updated_p = 1;
7118
7119 /* Update the display, unless called from redisplay_internal.
7120 Also don't update the screen during redisplay itself. The
7121 update will happen at the end of redisplay, and an update
7122 here could cause confusion. */
7123 if (update_frame_p && !redisplaying_p)
7124 {
7125 int n = 0;
7126
7127 /* If the display update has been interrupted by pending
7128 input, update mode lines in the frame. Due to the
7129 pending input, it might have been that redisplay hasn't
7130 been called, so that mode lines above the echo area are
7131 garbaged. This looks odd, so we prevent it here. */
7132 if (!display_completed)
7133 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
7134
7135 if (window_height_changed_p
7136 /* Don't do this if Emacs is shutting down. Redisplay
7137 needs to run hooks. */
7138 && !NILP (Vrun_hooks))
7139 {
7140 /* Must update other windows. Likewise as in other
7141 cases, don't let this update be interrupted by
7142 pending input. */
7143 int count = BINDING_STACK_SIZE ();
7144 specbind (Qredisplay_dont_pause, Qt);
7145 windows_or_buffers_changed = 1;
7146 redisplay_internal (0);
7147 unbind_to (count, Qnil);
7148 }
7149 else if (FRAME_WINDOW_P (f) && n == 0)
7150 {
7151 /* Window configuration is the same as before.
7152 Can do with a display update of the echo area,
7153 unless we displayed some mode lines. */
7154 update_single_window (w, 1);
7155 rif->flush_display (f);
7156 }
7157 else
7158 update_frame (f, 1, 1);
7159
7160 /* If cursor is in the echo area, make sure that the next
7161 redisplay displays the minibuffer, so that the cursor will
7162 be replaced with what the minibuffer wants. */
7163 if (cursor_in_echo_area)
7164 ++windows_or_buffers_changed;
7165 }
7166 }
7167 else if (!EQ (mini_window, selected_window))
7168 windows_or_buffers_changed++;
7169
7170 /* Last displayed message is now the current message. */
7171 echo_area_buffer[1] = echo_area_buffer[0];
7172
7173 /* Prevent redisplay optimization in redisplay_internal by resetting
7174 this_line_start_pos. This is done because the mini-buffer now
7175 displays the message instead of its buffer text. */
7176 if (EQ (mini_window, selected_window))
7177 CHARPOS (this_line_start_pos) = 0;
7178
7179 return window_height_changed_p;
7180 }
7181
7182
7183 \f
7184 /***********************************************************************
7185 Frame Titles
7186 ***********************************************************************/
7187
7188
7189 #ifdef HAVE_WINDOW_SYSTEM
7190
7191 /* A buffer for constructing frame titles in it; allocated from the
7192 heap in init_xdisp and resized as needed in store_frame_title_char. */
7193
7194 static char *frame_title_buf;
7195
7196 /* The buffer's end, and a current output position in it. */
7197
7198 static char *frame_title_buf_end;
7199 static char *frame_title_ptr;
7200
7201
7202 /* Store a single character C for the frame title in frame_title_buf.
7203 Re-allocate frame_title_buf if necessary. */
7204
7205 static void
7206 store_frame_title_char (c)
7207 char c;
7208 {
7209 /* If output position has reached the end of the allocated buffer,
7210 double the buffer's size. */
7211 if (frame_title_ptr == frame_title_buf_end)
7212 {
7213 int len = frame_title_ptr - frame_title_buf;
7214 int new_size = 2 * len * sizeof *frame_title_buf;
7215 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
7216 frame_title_buf_end = frame_title_buf + new_size;
7217 frame_title_ptr = frame_title_buf + len;
7218 }
7219
7220 *frame_title_ptr++ = c;
7221 }
7222
7223
7224 /* Store part of a frame title in frame_title_buf, beginning at
7225 frame_title_ptr. STR is the string to store. Do not copy
7226 characters that yield more columns than PRECISION; PRECISION <= 0
7227 means copy the whole string. Pad with spaces until FIELD_WIDTH
7228 number of characters have been copied; FIELD_WIDTH <= 0 means don't
7229 pad. Called from display_mode_element when it is used to build a
7230 frame title. */
7231
7232 static int
7233 store_frame_title (str, field_width, precision)
7234 unsigned char *str;
7235 int field_width, precision;
7236 {
7237 int n = 0;
7238 int dummy, nbytes;
7239
7240 /* Copy at most PRECISION chars from STR. */
7241 nbytes = strlen (str);
7242 n+= c_string_width (str, nbytes, precision, &dummy, &nbytes);
7243 while (nbytes--)
7244 store_frame_title_char (*str++);
7245
7246 /* Fill up with spaces until FIELD_WIDTH reached. */
7247 while (field_width > 0
7248 && n < field_width)
7249 {
7250 store_frame_title_char (' ');
7251 ++n;
7252 }
7253
7254 return n;
7255 }
7256
7257
7258 /* Set the title of FRAME, if it has changed. The title format is
7259 Vicon_title_format if FRAME is iconified, otherwise it is
7260 frame_title_format. */
7261
7262 static void
7263 x_consider_frame_title (frame)
7264 Lisp_Object frame;
7265 {
7266 struct frame *f = XFRAME (frame);
7267
7268 if (FRAME_WINDOW_P (f)
7269 || FRAME_MINIBUF_ONLY_P (f)
7270 || f->explicit_name)
7271 {
7272 /* Do we have more than one visible frame on this X display? */
7273 Lisp_Object tail;
7274 Lisp_Object fmt;
7275 struct buffer *obuf;
7276 int len;
7277 struct it it;
7278
7279 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
7280 {
7281 Lisp_Object other_frame = XCAR (tail);
7282 struct frame *tf = XFRAME (other_frame);
7283
7284 if (tf != f
7285 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
7286 && !FRAME_MINIBUF_ONLY_P (tf)
7287 && !EQ (other_frame, tip_frame)
7288 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
7289 break;
7290 }
7291
7292 /* Set global variable indicating that multiple frames exist. */
7293 multiple_frames = CONSP (tail);
7294
7295 /* Switch to the buffer of selected window of the frame. Set up
7296 frame_title_ptr so that display_mode_element will output into it;
7297 then display the title. */
7298 obuf = current_buffer;
7299 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
7300 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
7301 frame_title_ptr = frame_title_buf;
7302 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
7303 NULL, DEFAULT_FACE_ID);
7304 display_mode_element (&it, 0, -1, -1, fmt, Qnil);
7305 len = frame_title_ptr - frame_title_buf;
7306 frame_title_ptr = NULL;
7307 set_buffer_internal_1 (obuf);
7308
7309 /* Set the title only if it's changed. This avoids consing in
7310 the common case where it hasn't. (If it turns out that we've
7311 already wasted too much time by walking through the list with
7312 display_mode_element, then we might need to optimize at a
7313 higher level than this.) */
7314 if (! STRINGP (f->name)
7315 || STRING_BYTES (XSTRING (f->name)) != len
7316 || bcmp (frame_title_buf, XSTRING (f->name)->data, len) != 0)
7317 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
7318 }
7319 }
7320
7321 #else /* not HAVE_WINDOW_SYSTEM */
7322
7323 #define frame_title_ptr ((char *)0)
7324 #define store_frame_title(str, mincol, maxcol) 0
7325
7326 #endif /* not HAVE_WINDOW_SYSTEM */
7327
7328
7329
7330 \f
7331 /***********************************************************************
7332 Menu Bars
7333 ***********************************************************************/
7334
7335
7336 /* Prepare for redisplay by updating menu-bar item lists when
7337 appropriate. This can call eval. */
7338
7339 void
7340 prepare_menu_bars ()
7341 {
7342 int all_windows;
7343 struct gcpro gcpro1, gcpro2;
7344 struct frame *f;
7345 Lisp_Object tooltip_frame;
7346
7347 #ifdef HAVE_WINDOW_SYSTEM
7348 tooltip_frame = tip_frame;
7349 #else
7350 tooltip_frame = Qnil;
7351 #endif
7352
7353 /* Update all frame titles based on their buffer names, etc. We do
7354 this before the menu bars so that the buffer-menu will show the
7355 up-to-date frame titles. */
7356 #ifdef HAVE_WINDOW_SYSTEM
7357 if (windows_or_buffers_changed || update_mode_lines)
7358 {
7359 Lisp_Object tail, frame;
7360
7361 FOR_EACH_FRAME (tail, frame)
7362 {
7363 f = XFRAME (frame);
7364 if (!EQ (frame, tooltip_frame)
7365 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
7366 x_consider_frame_title (frame);
7367 }
7368 }
7369 #endif /* HAVE_WINDOW_SYSTEM */
7370
7371 /* Update the menu bar item lists, if appropriate. This has to be
7372 done before any actual redisplay or generation of display lines. */
7373 all_windows = (update_mode_lines
7374 || buffer_shared > 1
7375 || windows_or_buffers_changed);
7376 if (all_windows)
7377 {
7378 Lisp_Object tail, frame;
7379 int count = BINDING_STACK_SIZE ();
7380
7381 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7382
7383 FOR_EACH_FRAME (tail, frame)
7384 {
7385 f = XFRAME (frame);
7386
7387 /* Ignore tooltip frame. */
7388 if (EQ (frame, tooltip_frame))
7389 continue;
7390
7391 /* If a window on this frame changed size, report that to
7392 the user and clear the size-change flag. */
7393 if (FRAME_WINDOW_SIZES_CHANGED (f))
7394 {
7395 Lisp_Object functions;
7396
7397 /* Clear flag first in case we get an error below. */
7398 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
7399 functions = Vwindow_size_change_functions;
7400 GCPRO2 (tail, functions);
7401
7402 while (CONSP (functions))
7403 {
7404 call1 (XCAR (functions), frame);
7405 functions = XCDR (functions);
7406 }
7407 UNGCPRO;
7408 }
7409
7410 GCPRO1 (tail);
7411 update_menu_bar (f, 0);
7412 #ifdef HAVE_WINDOW_SYSTEM
7413 update_tool_bar (f, 0);
7414 #endif
7415 UNGCPRO;
7416 }
7417
7418 unbind_to (count, Qnil);
7419 }
7420 else
7421 {
7422 struct frame *sf = SELECTED_FRAME ();
7423 update_menu_bar (sf, 1);
7424 #ifdef HAVE_WINDOW_SYSTEM
7425 update_tool_bar (sf, 1);
7426 #endif
7427 }
7428
7429 /* Motif needs this. See comment in xmenu.c. Turn it off when
7430 pending_menu_activation is not defined. */
7431 #ifdef USE_X_TOOLKIT
7432 pending_menu_activation = 0;
7433 #endif
7434 }
7435
7436
7437 /* Update the menu bar item list for frame F. This has to be done
7438 before we start to fill in any display lines, because it can call
7439 eval.
7440
7441 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
7442
7443 static void
7444 update_menu_bar (f, save_match_data)
7445 struct frame *f;
7446 int save_match_data;
7447 {
7448 Lisp_Object window;
7449 register struct window *w;
7450
7451 /* If called recursively during a menu update, do nothing. This can
7452 happen when, for instance, an activate-menubar-hook causes a
7453 redisplay. */
7454 if (inhibit_menubar_update)
7455 return;
7456
7457 window = FRAME_SELECTED_WINDOW (f);
7458 w = XWINDOW (window);
7459
7460 if (update_mode_lines)
7461 w->update_mode_line = Qt;
7462
7463 if (FRAME_WINDOW_P (f)
7464 ?
7465 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
7466 FRAME_EXTERNAL_MENU_BAR (f)
7467 #else
7468 FRAME_MENU_BAR_LINES (f) > 0
7469 #endif
7470 : FRAME_MENU_BAR_LINES (f) > 0)
7471 {
7472 /* If the user has switched buffers or windows, we need to
7473 recompute to reflect the new bindings. But we'll
7474 recompute when update_mode_lines is set too; that means
7475 that people can use force-mode-line-update to request
7476 that the menu bar be recomputed. The adverse effect on
7477 the rest of the redisplay algorithm is about the same as
7478 windows_or_buffers_changed anyway. */
7479 if (windows_or_buffers_changed
7480 || !NILP (w->update_mode_line)
7481 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7482 < BUF_MODIFF (XBUFFER (w->buffer)))
7483 != !NILP (w->last_had_star))
7484 || ((!NILP (Vtransient_mark_mode)
7485 && !NILP (XBUFFER (w->buffer)->mark_active))
7486 != !NILP (w->region_showing)))
7487 {
7488 struct buffer *prev = current_buffer;
7489 int count = BINDING_STACK_SIZE ();
7490
7491 specbind (Qinhibit_menubar_update, Qt);
7492
7493 set_buffer_internal_1 (XBUFFER (w->buffer));
7494 if (save_match_data)
7495 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7496 if (NILP (Voverriding_local_map_menu_flag))
7497 {
7498 specbind (Qoverriding_terminal_local_map, Qnil);
7499 specbind (Qoverriding_local_map, Qnil);
7500 }
7501
7502 /* Run the Lucid hook. */
7503 safe_run_hooks (Qactivate_menubar_hook);
7504
7505 /* If it has changed current-menubar from previous value,
7506 really recompute the menu-bar from the value. */
7507 if (! NILP (Vlucid_menu_bar_dirty_flag))
7508 call0 (Qrecompute_lucid_menubar);
7509
7510 safe_run_hooks (Qmenu_bar_update_hook);
7511 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
7512
7513 /* Redisplay the menu bar in case we changed it. */
7514 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
7515 if (FRAME_WINDOW_P (f)
7516 #if defined (macintosh)
7517 /* All frames on Mac OS share the same menubar. So only the
7518 selected frame should be allowed to set it. */
7519 && f == SELECTED_FRAME ()
7520 #endif
7521 )
7522 set_frame_menubar (f, 0, 0);
7523 else
7524 /* On a terminal screen, the menu bar is an ordinary screen
7525 line, and this makes it get updated. */
7526 w->update_mode_line = Qt;
7527 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7528 /* In the non-toolkit version, the menu bar is an ordinary screen
7529 line, and this makes it get updated. */
7530 w->update_mode_line = Qt;
7531 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7532
7533 unbind_to (count, Qnil);
7534 set_buffer_internal_1 (prev);
7535 }
7536 }
7537 }
7538
7539
7540 \f
7541 /***********************************************************************
7542 Tool-bars
7543 ***********************************************************************/
7544
7545 #ifdef HAVE_WINDOW_SYSTEM
7546
7547 /* Update the tool-bar item list for frame F. This has to be done
7548 before we start to fill in any display lines. Called from
7549 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
7550 and restore it here. */
7551
7552 static void
7553 update_tool_bar (f, save_match_data)
7554 struct frame *f;
7555 int save_match_data;
7556 {
7557 if (WINDOWP (f->tool_bar_window)
7558 && XFASTINT (XWINDOW (f->tool_bar_window)->height) > 0)
7559 {
7560 Lisp_Object window;
7561 struct window *w;
7562
7563 window = FRAME_SELECTED_WINDOW (f);
7564 w = XWINDOW (window);
7565
7566 /* If the user has switched buffers or windows, we need to
7567 recompute to reflect the new bindings. But we'll
7568 recompute when update_mode_lines is set too; that means
7569 that people can use force-mode-line-update to request
7570 that the menu bar be recomputed. The adverse effect on
7571 the rest of the redisplay algorithm is about the same as
7572 windows_or_buffers_changed anyway. */
7573 if (windows_or_buffers_changed
7574 || !NILP (w->update_mode_line)
7575 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7576 < BUF_MODIFF (XBUFFER (w->buffer)))
7577 != !NILP (w->last_had_star))
7578 || ((!NILP (Vtransient_mark_mode)
7579 && !NILP (XBUFFER (w->buffer)->mark_active))
7580 != !NILP (w->region_showing)))
7581 {
7582 struct buffer *prev = current_buffer;
7583 int count = BINDING_STACK_SIZE ();
7584
7585 /* Set current_buffer to the buffer of the selected
7586 window of the frame, so that we get the right local
7587 keymaps. */
7588 set_buffer_internal_1 (XBUFFER (w->buffer));
7589
7590 /* Save match data, if we must. */
7591 if (save_match_data)
7592 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7593
7594 /* Make sure that we don't accidentally use bogus keymaps. */
7595 if (NILP (Voverriding_local_map_menu_flag))
7596 {
7597 specbind (Qoverriding_terminal_local_map, Qnil);
7598 specbind (Qoverriding_local_map, Qnil);
7599 }
7600
7601 /* Build desired tool-bar items from keymaps. */
7602 f->tool_bar_items
7603 = tool_bar_items (f->tool_bar_items, &f->n_tool_bar_items);
7604
7605 /* Redisplay the tool-bar in case we changed it. */
7606 w->update_mode_line = Qt;
7607
7608 unbind_to (count, Qnil);
7609 set_buffer_internal_1 (prev);
7610 }
7611 }
7612 }
7613
7614
7615 /* Set F->desired_tool_bar_string to a Lisp string representing frame
7616 F's desired tool-bar contents. F->tool_bar_items must have
7617 been set up previously by calling prepare_menu_bars. */
7618
7619 static void
7620 build_desired_tool_bar_string (f)
7621 struct frame *f;
7622 {
7623 int i, size, size_needed;
7624 struct gcpro gcpro1, gcpro2, gcpro3;
7625 Lisp_Object image, plist, props;
7626
7627 image = plist = props = Qnil;
7628 GCPRO3 (image, plist, props);
7629
7630 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
7631 Otherwise, make a new string. */
7632
7633 /* The size of the string we might be able to reuse. */
7634 size = (STRINGP (f->desired_tool_bar_string)
7635 ? XSTRING (f->desired_tool_bar_string)->size
7636 : 0);
7637
7638 /* We need one space in the string for each image. */
7639 size_needed = f->n_tool_bar_items;
7640
7641 /* Reuse f->desired_tool_bar_string, if possible. */
7642 if (size < size_needed || NILP (f->desired_tool_bar_string))
7643 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
7644 make_number (' '));
7645 else
7646 {
7647 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
7648 Fremove_text_properties (make_number (0), make_number (size),
7649 props, f->desired_tool_bar_string);
7650 }
7651
7652 /* Put a `display' property on the string for the images to display,
7653 put a `menu_item' property on tool-bar items with a value that
7654 is the index of the item in F's tool-bar item vector. */
7655 for (i = 0; i < f->n_tool_bar_items; ++i)
7656 {
7657 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
7658
7659 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
7660 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
7661 int hmargin, vmargin, relief, idx, end;
7662 extern Lisp_Object QCrelief, QCmargin, QCconversion, Qimage;
7663
7664 /* If image is a vector, choose the image according to the
7665 button state. */
7666 image = PROP (TOOL_BAR_ITEM_IMAGES);
7667 if (VECTORP (image))
7668 {
7669 if (enabled_p)
7670 idx = (selected_p
7671 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
7672 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
7673 else
7674 idx = (selected_p
7675 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
7676 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
7677
7678 xassert (ASIZE (image) >= idx);
7679 image = AREF (image, idx);
7680 }
7681 else
7682 idx = -1;
7683
7684 /* Ignore invalid image specifications. */
7685 if (!valid_image_p (image))
7686 continue;
7687
7688 /* Display the tool-bar button pressed, or depressed. */
7689 plist = Fcopy_sequence (XCDR (image));
7690
7691 /* Compute margin and relief to draw. */
7692 relief = (tool_bar_button_relief >= 0
7693 ? tool_bar_button_relief
7694 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
7695 hmargin = vmargin = relief;
7696
7697 if (INTEGERP (Vtool_bar_button_margin)
7698 && XINT (Vtool_bar_button_margin) > 0)
7699 {
7700 hmargin += XFASTINT (Vtool_bar_button_margin);
7701 vmargin += XFASTINT (Vtool_bar_button_margin);
7702 }
7703 else if (CONSP (Vtool_bar_button_margin))
7704 {
7705 if (INTEGERP (XCAR (Vtool_bar_button_margin))
7706 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
7707 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
7708
7709 if (INTEGERP (XCDR (Vtool_bar_button_margin))
7710 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
7711 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
7712 }
7713
7714 if (auto_raise_tool_bar_buttons_p)
7715 {
7716 /* Add a `:relief' property to the image spec if the item is
7717 selected. */
7718 if (selected_p)
7719 {
7720 plist = Fplist_put (plist, QCrelief, make_number (-relief));
7721 hmargin -= relief;
7722 vmargin -= relief;
7723 }
7724 }
7725 else
7726 {
7727 /* If image is selected, display it pressed, i.e. with a
7728 negative relief. If it's not selected, display it with a
7729 raised relief. */
7730 plist = Fplist_put (plist, QCrelief,
7731 (selected_p
7732 ? make_number (-relief)
7733 : make_number (relief)));
7734 hmargin -= relief;
7735 vmargin -= relief;
7736 }
7737
7738 /* Put a margin around the image. */
7739 if (hmargin || vmargin)
7740 {
7741 if (hmargin == vmargin)
7742 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
7743 else
7744 plist = Fplist_put (plist, QCmargin,
7745 Fcons (make_number (hmargin),
7746 make_number (vmargin)));
7747 }
7748
7749 /* If button is not enabled, and we don't have special images
7750 for the disabled state, make the image appear disabled by
7751 applying an appropriate algorithm to it. */
7752 if (!enabled_p && idx < 0)
7753 plist = Fplist_put (plist, QCconversion, Qdisabled);
7754
7755 /* Put a `display' text property on the string for the image to
7756 display. Put a `menu-item' property on the string that gives
7757 the start of this item's properties in the tool-bar items
7758 vector. */
7759 image = Fcons (Qimage, plist);
7760 props = list4 (Qdisplay, image,
7761 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
7762
7763 /* Let the last image hide all remaining spaces in the tool bar
7764 string. The string can be longer than needed when we reuse a
7765 previous string. */
7766 if (i + 1 == f->n_tool_bar_items)
7767 end = XSTRING (f->desired_tool_bar_string)->size;
7768 else
7769 end = i + 1;
7770 Fadd_text_properties (make_number (i), make_number (end),
7771 props, f->desired_tool_bar_string);
7772 #undef PROP
7773 }
7774
7775 UNGCPRO;
7776 }
7777
7778
7779 /* Display one line of the tool-bar of frame IT->f. */
7780
7781 static void
7782 display_tool_bar_line (it)
7783 struct it *it;
7784 {
7785 struct glyph_row *row = it->glyph_row;
7786 int max_x = it->last_visible_x;
7787 struct glyph *last;
7788
7789 prepare_desired_row (row);
7790 row->y = it->current_y;
7791
7792 /* Note that this isn't made use of if the face hasn't a box,
7793 so there's no need to check the face here. */
7794 it->start_of_box_run_p = 1;
7795
7796 while (it->current_x < max_x)
7797 {
7798 int x_before, x, n_glyphs_before, i, nglyphs;
7799
7800 /* Get the next display element. */
7801 if (!get_next_display_element (it))
7802 break;
7803
7804 /* Produce glyphs. */
7805 x_before = it->current_x;
7806 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
7807 PRODUCE_GLYPHS (it);
7808
7809 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
7810 i = 0;
7811 x = x_before;
7812 while (i < nglyphs)
7813 {
7814 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
7815
7816 if (x + glyph->pixel_width > max_x)
7817 {
7818 /* Glyph doesn't fit on line. */
7819 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
7820 it->current_x = x;
7821 goto out;
7822 }
7823
7824 ++it->hpos;
7825 x += glyph->pixel_width;
7826 ++i;
7827 }
7828
7829 /* Stop at line ends. */
7830 if (ITERATOR_AT_END_OF_LINE_P (it))
7831 break;
7832
7833 set_iterator_to_next (it, 1);
7834 }
7835
7836 out:;
7837
7838 row->displays_text_p = row->used[TEXT_AREA] != 0;
7839 extend_face_to_end_of_line (it);
7840 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
7841 last->right_box_line_p = 1;
7842 if (last == row->glyphs[TEXT_AREA])
7843 last->left_box_line_p = 1;
7844 compute_line_metrics (it);
7845
7846 /* If line is empty, make it occupy the rest of the tool-bar. */
7847 if (!row->displays_text_p)
7848 {
7849 row->height = row->phys_height = it->last_visible_y - row->y;
7850 row->ascent = row->phys_ascent = 0;
7851 }
7852
7853 row->full_width_p = 1;
7854 row->continued_p = 0;
7855 row->truncated_on_left_p = 0;
7856 row->truncated_on_right_p = 0;
7857
7858 it->current_x = it->hpos = 0;
7859 it->current_y += row->height;
7860 ++it->vpos;
7861 ++it->glyph_row;
7862 }
7863
7864
7865 /* Value is the number of screen lines needed to make all tool-bar
7866 items of frame F visible. */
7867
7868 static int
7869 tool_bar_lines_needed (f)
7870 struct frame *f;
7871 {
7872 struct window *w = XWINDOW (f->tool_bar_window);
7873 struct it it;
7874
7875 /* Initialize an iterator for iteration over
7876 F->desired_tool_bar_string in the tool-bar window of frame F. */
7877 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7878 it.first_visible_x = 0;
7879 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7880 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7881
7882 while (!ITERATOR_AT_END_P (&it))
7883 {
7884 it.glyph_row = w->desired_matrix->rows;
7885 clear_glyph_row (it.glyph_row);
7886 display_tool_bar_line (&it);
7887 }
7888
7889 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
7890 }
7891
7892
7893 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
7894 0, 1, 0,
7895 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
7896 (frame)
7897 Lisp_Object frame;
7898 {
7899 struct frame *f;
7900 struct window *w;
7901 int nlines = 0;
7902
7903 if (NILP (frame))
7904 frame = selected_frame;
7905 else
7906 CHECK_FRAME (frame);
7907 f = XFRAME (frame);
7908
7909 if (WINDOWP (f->tool_bar_window)
7910 || (w = XWINDOW (f->tool_bar_window),
7911 XFASTINT (w->height) > 0))
7912 {
7913 update_tool_bar (f, 1);
7914 if (f->n_tool_bar_items)
7915 {
7916 build_desired_tool_bar_string (f);
7917 nlines = tool_bar_lines_needed (f);
7918 }
7919 }
7920
7921 return make_number (nlines);
7922 }
7923
7924
7925 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
7926 height should be changed. */
7927
7928 static int
7929 redisplay_tool_bar (f)
7930 struct frame *f;
7931 {
7932 struct window *w;
7933 struct it it;
7934 struct glyph_row *row;
7935 int change_height_p = 0;
7936
7937 /* If frame hasn't a tool-bar window or if it is zero-height, don't
7938 do anything. This means you must start with tool-bar-lines
7939 non-zero to get the auto-sizing effect. Or in other words, you
7940 can turn off tool-bars by specifying tool-bar-lines zero. */
7941 if (!WINDOWP (f->tool_bar_window)
7942 || (w = XWINDOW (f->tool_bar_window),
7943 XFASTINT (w->height) == 0))
7944 return 0;
7945
7946 /* Set up an iterator for the tool-bar window. */
7947 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7948 it.first_visible_x = 0;
7949 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7950 row = it.glyph_row;
7951
7952 /* Build a string that represents the contents of the tool-bar. */
7953 build_desired_tool_bar_string (f);
7954 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7955
7956 /* Display as many lines as needed to display all tool-bar items. */
7957 while (it.current_y < it.last_visible_y)
7958 display_tool_bar_line (&it);
7959
7960 /* It doesn't make much sense to try scrolling in the tool-bar
7961 window, so don't do it. */
7962 w->desired_matrix->no_scrolling_p = 1;
7963 w->must_be_updated_p = 1;
7964
7965 if (auto_resize_tool_bars_p)
7966 {
7967 int nlines;
7968
7969 /* If we couldn't display everything, change the tool-bar's
7970 height. */
7971 if (IT_STRING_CHARPOS (it) < it.end_charpos)
7972 change_height_p = 1;
7973
7974 /* If there are blank lines at the end, except for a partially
7975 visible blank line at the end that is smaller than
7976 CANON_Y_UNIT, change the tool-bar's height. */
7977 row = it.glyph_row - 1;
7978 if (!row->displays_text_p
7979 && row->height >= CANON_Y_UNIT (f))
7980 change_height_p = 1;
7981
7982 /* If row displays tool-bar items, but is partially visible,
7983 change the tool-bar's height. */
7984 if (row->displays_text_p
7985 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
7986 change_height_p = 1;
7987
7988 /* Resize windows as needed by changing the `tool-bar-lines'
7989 frame parameter. */
7990 if (change_height_p
7991 && (nlines = tool_bar_lines_needed (f),
7992 nlines != XFASTINT (w->height)))
7993 {
7994 extern Lisp_Object Qtool_bar_lines;
7995 Lisp_Object frame;
7996 int old_height = XFASTINT (w->height);
7997
7998 XSETFRAME (frame, f);
7999 clear_glyph_matrix (w->desired_matrix);
8000 Fmodify_frame_parameters (frame,
8001 Fcons (Fcons (Qtool_bar_lines,
8002 make_number (nlines)),
8003 Qnil));
8004 if (XFASTINT (w->height) != old_height)
8005 fonts_changed_p = 1;
8006 }
8007 }
8008
8009 return change_height_p;
8010 }
8011
8012
8013 /* Get information about the tool-bar item which is displayed in GLYPH
8014 on frame F. Return in *PROP_IDX the index where tool-bar item
8015 properties start in F->tool_bar_items. Value is zero if
8016 GLYPH doesn't display a tool-bar item. */
8017
8018 int
8019 tool_bar_item_info (f, glyph, prop_idx)
8020 struct frame *f;
8021 struct glyph *glyph;
8022 int *prop_idx;
8023 {
8024 Lisp_Object prop;
8025 int success_p;
8026 int charpos;
8027
8028 /* This function can be called asynchronously, which means we must
8029 exclude any possibility that Fget_text_property signals an
8030 error. */
8031 charpos = min (XSTRING (f->current_tool_bar_string)->size, glyph->charpos);
8032 charpos = max (0, charpos);
8033
8034 /* Get the text property `menu-item' at pos. The value of that
8035 property is the start index of this item's properties in
8036 F->tool_bar_items. */
8037 prop = Fget_text_property (make_number (charpos),
8038 Qmenu_item, f->current_tool_bar_string);
8039 if (INTEGERP (prop))
8040 {
8041 *prop_idx = XINT (prop);
8042 success_p = 1;
8043 }
8044 else
8045 success_p = 0;
8046
8047 return success_p;
8048 }
8049
8050 #endif /* HAVE_WINDOW_SYSTEM */
8051
8052
8053 \f
8054 /************************************************************************
8055 Horizontal scrolling
8056 ************************************************************************/
8057
8058 static int hscroll_window_tree P_ ((Lisp_Object));
8059 static int hscroll_windows P_ ((Lisp_Object));
8060
8061 /* For all leaf windows in the window tree rooted at WINDOW, set their
8062 hscroll value so that PT is (i) visible in the window, and (ii) so
8063 that it is not within a certain margin at the window's left and
8064 right border. Value is non-zero if any window's hscroll has been
8065 changed. */
8066
8067 static int
8068 hscroll_window_tree (window)
8069 Lisp_Object window;
8070 {
8071 int hscrolled_p = 0;
8072 int hscroll_relative_p = FLOATP (Vautomatic_hscroll_step);
8073 int hscroll_step_abs = 0;
8074 double hscroll_step_rel = 0;
8075
8076 if (hscroll_relative_p)
8077 {
8078 hscroll_step_rel = XFLOAT_DATA (Vautomatic_hscroll_step);
8079 if (hscroll_step_rel < 0)
8080 {
8081 hscroll_relative_p = 0;
8082 hscroll_step_abs = 0;
8083 }
8084 }
8085 else if (INTEGERP (Vautomatic_hscroll_step))
8086 {
8087 hscroll_step_abs = XINT (Vautomatic_hscroll_step);
8088 if (hscroll_step_abs < 0)
8089 hscroll_step_abs = 0;
8090 }
8091 else
8092 hscroll_step_abs = 0;
8093
8094 while (WINDOWP (window))
8095 {
8096 struct window *w = XWINDOW (window);
8097
8098 if (WINDOWP (w->hchild))
8099 hscrolled_p |= hscroll_window_tree (w->hchild);
8100 else if (WINDOWP (w->vchild))
8101 hscrolled_p |= hscroll_window_tree (w->vchild);
8102 else if (w->cursor.vpos >= 0)
8103 {
8104 int hscroll_margin, text_area_x, text_area_y;
8105 int text_area_width, text_area_height;
8106 struct glyph_row *current_cursor_row
8107 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
8108 struct glyph_row *desired_cursor_row
8109 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
8110 struct glyph_row *cursor_row
8111 = (desired_cursor_row->enabled_p
8112 ? desired_cursor_row
8113 : current_cursor_row);
8114
8115 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
8116 &text_area_width, &text_area_height);
8117
8118 /* Scroll when cursor is inside this scroll margin. */
8119 hscroll_margin
8120 = automatic_hscroll_margin * CANON_X_UNIT (XFRAME (w->frame));
8121
8122 if ((XFASTINT (w->hscroll)
8123 && w->cursor.x <= hscroll_margin)
8124 || (cursor_row->enabled_p
8125 && cursor_row->truncated_on_right_p
8126 && (w->cursor.x >= text_area_width - hscroll_margin)))
8127 {
8128 struct it it;
8129 int hscroll;
8130 struct buffer *saved_current_buffer;
8131 int pt;
8132 int wanted_x;
8133
8134 /* Find point in a display of infinite width. */
8135 saved_current_buffer = current_buffer;
8136 current_buffer = XBUFFER (w->buffer);
8137
8138 if (w == XWINDOW (selected_window))
8139 pt = BUF_PT (current_buffer);
8140 else
8141 {
8142 pt = marker_position (w->pointm);
8143 pt = max (BEGV, pt);
8144 pt = min (ZV, pt);
8145 }
8146
8147 /* Move iterator to pt starting at cursor_row->start in
8148 a line with infinite width. */
8149 init_to_row_start (&it, w, cursor_row);
8150 it.last_visible_x = INFINITY;
8151 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
8152 current_buffer = saved_current_buffer;
8153
8154 /* Position cursor in window. */
8155 if (!hscroll_relative_p && hscroll_step_abs == 0)
8156 hscroll = max (0, it.current_x - text_area_width / 2)
8157 / CANON_X_UNIT (it.f);
8158 else if (w->cursor.x >= text_area_width - hscroll_margin)
8159 {
8160 if (hscroll_relative_p)
8161 wanted_x = text_area_width * (1 - hscroll_step_rel)
8162 - hscroll_margin;
8163 else
8164 wanted_x = text_area_width
8165 - hscroll_step_abs * CANON_X_UNIT (it.f)
8166 - hscroll_margin;
8167 hscroll
8168 = max (0, it.current_x - wanted_x) / CANON_X_UNIT (it.f);
8169 }
8170 else
8171 {
8172 if (hscroll_relative_p)
8173 wanted_x = text_area_width * hscroll_step_rel
8174 + hscroll_margin;
8175 else
8176 wanted_x = hscroll_step_abs * CANON_X_UNIT (it.f)
8177 + hscroll_margin;
8178 hscroll
8179 = max (0, it.current_x - wanted_x) / CANON_X_UNIT (it.f);
8180 }
8181 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
8182
8183 /* Don't call Fset_window_hscroll if value hasn't
8184 changed because it will prevent redisplay
8185 optimizations. */
8186 if (XFASTINT (w->hscroll) != hscroll)
8187 {
8188 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
8189 w->hscroll = make_number (hscroll);
8190 hscrolled_p = 1;
8191 }
8192 }
8193 }
8194
8195 window = w->next;
8196 }
8197
8198 /* Value is non-zero if hscroll of any leaf window has been changed. */
8199 return hscrolled_p;
8200 }
8201
8202
8203 /* Set hscroll so that cursor is visible and not inside horizontal
8204 scroll margins for all windows in the tree rooted at WINDOW. See
8205 also hscroll_window_tree above. Value is non-zero if any window's
8206 hscroll has been changed. If it has, desired matrices on the frame
8207 of WINDOW are cleared. */
8208
8209 static int
8210 hscroll_windows (window)
8211 Lisp_Object window;
8212 {
8213 int hscrolled_p;
8214
8215 if (automatic_hscrolling_p)
8216 {
8217 hscrolled_p = hscroll_window_tree (window);
8218 if (hscrolled_p)
8219 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
8220 }
8221 else
8222 hscrolled_p = 0;
8223 return hscrolled_p;
8224 }
8225
8226
8227 \f
8228 /************************************************************************
8229 Redisplay
8230 ************************************************************************/
8231
8232 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
8233 to a non-zero value. This is sometimes handy to have in a debugger
8234 session. */
8235
8236 #if GLYPH_DEBUG
8237
8238 /* First and last unchanged row for try_window_id. */
8239
8240 int debug_first_unchanged_at_end_vpos;
8241 int debug_last_unchanged_at_beg_vpos;
8242
8243 /* Delta vpos and y. */
8244
8245 int debug_dvpos, debug_dy;
8246
8247 /* Delta in characters and bytes for try_window_id. */
8248
8249 int debug_delta, debug_delta_bytes;
8250
8251 /* Values of window_end_pos and window_end_vpos at the end of
8252 try_window_id. */
8253
8254 int debug_end_pos, debug_end_vpos;
8255
8256 /* Append a string to W->desired_matrix->method. FMT is a printf
8257 format string. A1...A9 are a supplement for a variable-length
8258 argument list. If trace_redisplay_p is non-zero also printf the
8259 resulting string to stderr. */
8260
8261 static void
8262 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
8263 struct window *w;
8264 char *fmt;
8265 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
8266 {
8267 char buffer[512];
8268 char *method = w->desired_matrix->method;
8269 int len = strlen (method);
8270 int size = sizeof w->desired_matrix->method;
8271 int remaining = size - len - 1;
8272
8273 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
8274 if (len && remaining)
8275 {
8276 method[len] = '|';
8277 --remaining, ++len;
8278 }
8279
8280 strncpy (method + len, buffer, remaining);
8281
8282 if (trace_redisplay_p)
8283 fprintf (stderr, "%p (%s): %s\n",
8284 w,
8285 ((BUFFERP (w->buffer)
8286 && STRINGP (XBUFFER (w->buffer)->name))
8287 ? (char *) XSTRING (XBUFFER (w->buffer)->name)->data
8288 : "no buffer"),
8289 buffer);
8290 }
8291
8292 #endif /* GLYPH_DEBUG */
8293
8294
8295 /* This counter is used to clear the face cache every once in a while
8296 in redisplay_internal. It is incremented for each redisplay.
8297 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
8298 cleared. */
8299
8300 #define CLEAR_FACE_CACHE_COUNT 500
8301 static int clear_face_cache_count;
8302
8303 /* Record the previous terminal frame we displayed. */
8304
8305 static struct frame *previous_terminal_frame;
8306
8307 /* Non-zero while redisplay_internal is in progress. */
8308
8309 int redisplaying_p;
8310
8311
8312 /* Value is non-zero if all changes in window W, which displays
8313 current_buffer, are in the text between START and END. START is a
8314 buffer position, END is given as a distance from Z. Used in
8315 redisplay_internal for display optimization. */
8316
8317 static INLINE int
8318 text_outside_line_unchanged_p (w, start, end)
8319 struct window *w;
8320 int start, end;
8321 {
8322 int unchanged_p = 1;
8323
8324 /* If text or overlays have changed, see where. */
8325 if (XFASTINT (w->last_modified) < MODIFF
8326 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8327 {
8328 /* Gap in the line? */
8329 if (GPT < start || Z - GPT < end)
8330 unchanged_p = 0;
8331
8332 /* Changes start in front of the line, or end after it? */
8333 if (unchanged_p
8334 && (BEG_UNCHANGED < start - 1
8335 || END_UNCHANGED < end))
8336 unchanged_p = 0;
8337
8338 /* If selective display, can't optimize if changes start at the
8339 beginning of the line. */
8340 if (unchanged_p
8341 && INTEGERP (current_buffer->selective_display)
8342 && XINT (current_buffer->selective_display) > 0
8343 && (BEG_UNCHANGED < start || GPT <= start))
8344 unchanged_p = 0;
8345
8346 /* If there are overlays at the start or end of the line, these
8347 may have overlay strings with newlines in them. A change at
8348 START, for instance, may actually concern the display of such
8349 overlay strings as well, and they are displayed on different
8350 lines. So, quickly rule out this case. (For the future, it
8351 might be desirable to implement something more telling than
8352 just BEG/END_UNCHANGED.) */
8353 if (unchanged_p)
8354 {
8355 if (BEG + BEG_UNCHANGED == start
8356 && overlay_touches_p (start))
8357 unchanged_p = 0;
8358 if (END_UNCHANGED == end
8359 && overlay_touches_p (Z - end))
8360 unchanged_p = 0;
8361 }
8362 }
8363
8364 return unchanged_p;
8365 }
8366
8367
8368 /* Do a frame update, taking possible shortcuts into account. This is
8369 the main external entry point for redisplay.
8370
8371 If the last redisplay displayed an echo area message and that message
8372 is no longer requested, we clear the echo area or bring back the
8373 mini-buffer if that is in use. */
8374
8375 void
8376 redisplay ()
8377 {
8378 redisplay_internal (0);
8379 }
8380
8381
8382 /* Return 1 if point moved out of or into a composition. Otherwise
8383 return 0. PREV_BUF and PREV_PT are the last point buffer and
8384 position. BUF and PT are the current point buffer and position. */
8385
8386 int
8387 check_point_in_composition (prev_buf, prev_pt, buf, pt)
8388 struct buffer *prev_buf, *buf;
8389 int prev_pt, pt;
8390 {
8391 int start, end;
8392 Lisp_Object prop;
8393 Lisp_Object buffer;
8394
8395 XSETBUFFER (buffer, buf);
8396 /* Check a composition at the last point if point moved within the
8397 same buffer. */
8398 if (prev_buf == buf)
8399 {
8400 if (prev_pt == pt)
8401 /* Point didn't move. */
8402 return 0;
8403
8404 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
8405 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
8406 && COMPOSITION_VALID_P (start, end, prop)
8407 && start < prev_pt && end > prev_pt)
8408 /* The last point was within the composition. Return 1 iff
8409 point moved out of the composition. */
8410 return (pt <= start || pt >= end);
8411 }
8412
8413 /* Check a composition at the current point. */
8414 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
8415 && find_composition (pt, -1, &start, &end, &prop, buffer)
8416 && COMPOSITION_VALID_P (start, end, prop)
8417 && start < pt && end > pt);
8418 }
8419
8420
8421 /* Reconsider the setting of B->clip_changed which is displayed
8422 in window W. */
8423
8424 static INLINE void
8425 reconsider_clip_changes (w, b)
8426 struct window *w;
8427 struct buffer *b;
8428 {
8429 if (b->prevent_redisplay_optimizations_p)
8430 b->clip_changed = 1;
8431 else if (b->clip_changed
8432 && !NILP (w->window_end_valid)
8433 && w->current_matrix->buffer == b
8434 && w->current_matrix->zv == BUF_ZV (b)
8435 && w->current_matrix->begv == BUF_BEGV (b))
8436 b->clip_changed = 0;
8437
8438 /* If display wasn't paused, and W is not a tool bar window, see if
8439 point has been moved into or out of a composition. In that case,
8440 we set b->clip_changed to 1 to force updating the screen. If
8441 b->clip_changed has already been set to 1, we can skip this
8442 check. */
8443 if (!b->clip_changed
8444 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
8445 {
8446 int pt;
8447
8448 if (w == XWINDOW (selected_window))
8449 pt = BUF_PT (current_buffer);
8450 else
8451 pt = marker_position (w->pointm);
8452
8453 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
8454 || pt != XINT (w->last_point))
8455 && check_point_in_composition (w->current_matrix->buffer,
8456 XINT (w->last_point),
8457 XBUFFER (w->buffer), pt))
8458 b->clip_changed = 1;
8459 }
8460 }
8461
8462
8463 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
8464 response to any user action; therefore, we should preserve the echo
8465 area. (Actually, our caller does that job.) Perhaps in the future
8466 avoid recentering windows if it is not necessary; currently that
8467 causes some problems. */
8468
8469 static void
8470 redisplay_internal (preserve_echo_area)
8471 int preserve_echo_area;
8472 {
8473 struct window *w = XWINDOW (selected_window);
8474 struct frame *f = XFRAME (w->frame);
8475 int pause;
8476 int must_finish = 0;
8477 struct text_pos tlbufpos, tlendpos;
8478 int number_of_visible_frames;
8479 int count;
8480 struct frame *sf = SELECTED_FRAME ();
8481
8482 /* Non-zero means redisplay has to consider all windows on all
8483 frames. Zero means, only selected_window is considered. */
8484 int consider_all_windows_p;
8485
8486 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
8487
8488 /* No redisplay if running in batch mode or frame is not yet fully
8489 initialized, or redisplay is explicitly turned off by setting
8490 Vinhibit_redisplay. */
8491 if (noninteractive
8492 || !NILP (Vinhibit_redisplay)
8493 || !f->glyphs_initialized_p)
8494 return;
8495
8496 /* The flag redisplay_performed_directly_p is set by
8497 direct_output_for_insert when it already did the whole screen
8498 update necessary. */
8499 if (redisplay_performed_directly_p)
8500 {
8501 redisplay_performed_directly_p = 0;
8502 if (!hscroll_windows (selected_window))
8503 return;
8504 }
8505
8506 #ifdef USE_X_TOOLKIT
8507 if (popup_activated ())
8508 return;
8509 #endif
8510
8511 /* I don't think this happens but let's be paranoid. */
8512 if (redisplaying_p)
8513 return;
8514
8515 /* Record a function that resets redisplaying_p to its old value
8516 when we leave this function. */
8517 count = BINDING_STACK_SIZE ();
8518 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
8519 ++redisplaying_p;
8520
8521 retry:
8522 pause = 0;
8523 reconsider_clip_changes (w, current_buffer);
8524
8525 /* If new fonts have been loaded that make a glyph matrix adjustment
8526 necessary, do it. */
8527 if (fonts_changed_p)
8528 {
8529 adjust_glyphs (NULL);
8530 ++windows_or_buffers_changed;
8531 fonts_changed_p = 0;
8532 }
8533
8534 /* If face_change_count is non-zero, init_iterator will free all
8535 realized faces, which includes the faces referenced from current
8536 matrices. So, we can't reuse current matrices in this case. */
8537 if (face_change_count)
8538 ++windows_or_buffers_changed;
8539
8540 if (! FRAME_WINDOW_P (sf)
8541 && previous_terminal_frame != sf)
8542 {
8543 /* Since frames on an ASCII terminal share the same display
8544 area, displaying a different frame means redisplay the whole
8545 thing. */
8546 windows_or_buffers_changed++;
8547 SET_FRAME_GARBAGED (sf);
8548 XSETFRAME (Vterminal_frame, sf);
8549 }
8550 previous_terminal_frame = sf;
8551
8552 /* Set the visible flags for all frames. Do this before checking
8553 for resized or garbaged frames; they want to know if their frames
8554 are visible. See the comment in frame.h for
8555 FRAME_SAMPLE_VISIBILITY. */
8556 {
8557 Lisp_Object tail, frame;
8558
8559 number_of_visible_frames = 0;
8560
8561 FOR_EACH_FRAME (tail, frame)
8562 {
8563 struct frame *f = XFRAME (frame);
8564
8565 FRAME_SAMPLE_VISIBILITY (f);
8566 if (FRAME_VISIBLE_P (f))
8567 ++number_of_visible_frames;
8568 clear_desired_matrices (f);
8569 }
8570 }
8571
8572 /* Notice any pending interrupt request to change frame size. */
8573 do_pending_window_change (1);
8574
8575 /* Clear frames marked as garbaged. */
8576 if (frame_garbaged)
8577 clear_garbaged_frames ();
8578
8579 /* Build menubar and tool-bar items. */
8580 prepare_menu_bars ();
8581
8582 if (windows_or_buffers_changed)
8583 update_mode_lines++;
8584
8585 /* Detect case that we need to write or remove a star in the mode line. */
8586 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
8587 {
8588 w->update_mode_line = Qt;
8589 if (buffer_shared > 1)
8590 update_mode_lines++;
8591 }
8592
8593 /* If %c is in the mode line, update it if needed. */
8594 if (!NILP (w->column_number_displayed)
8595 /* This alternative quickly identifies a common case
8596 where no change is needed. */
8597 && !(PT == XFASTINT (w->last_point)
8598 && XFASTINT (w->last_modified) >= MODIFF
8599 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
8600 && XFASTINT (w->column_number_displayed) != current_column ())
8601 w->update_mode_line = Qt;
8602
8603 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
8604
8605 /* The variable buffer_shared is set in redisplay_window and
8606 indicates that we redisplay a buffer in different windows. See
8607 there. */
8608 consider_all_windows_p = update_mode_lines || buffer_shared > 1;
8609
8610 /* If specs for an arrow have changed, do thorough redisplay
8611 to ensure we remove any arrow that should no longer exist. */
8612 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
8613 || ! EQ (Voverlay_arrow_string, last_arrow_string))
8614 consider_all_windows_p = windows_or_buffers_changed = 1;
8615
8616 /* Normally the message* functions will have already displayed and
8617 updated the echo area, but the frame may have been trashed, or
8618 the update may have been preempted, so display the echo area
8619 again here. Checking message_cleared_p captures the case that
8620 the echo area should be cleared. */
8621 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
8622 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
8623 || (message_cleared_p
8624 && minibuf_level == 0
8625 /* If the mini-window is currently selected, this means the
8626 echo-area doesn't show through. */
8627 && !MINI_WINDOW_P (XWINDOW (selected_window))))
8628 {
8629 int window_height_changed_p = echo_area_display (0);
8630 must_finish = 1;
8631
8632 /* If we don't display the current message, don't clear the
8633 message_cleared_p flag, because, if we did, we wouldn't clear
8634 the echo area in the next redisplay which doesn't preserve
8635 the echo area. */
8636 if (!display_last_displayed_message_p)
8637 message_cleared_p = 0;
8638
8639 if (fonts_changed_p)
8640 goto retry;
8641 else if (window_height_changed_p)
8642 {
8643 consider_all_windows_p = 1;
8644 ++update_mode_lines;
8645 ++windows_or_buffers_changed;
8646
8647 /* If window configuration was changed, frames may have been
8648 marked garbaged. Clear them or we will experience
8649 surprises wrt scrolling. */
8650 if (frame_garbaged)
8651 clear_garbaged_frames ();
8652 }
8653 }
8654 else if (EQ (selected_window, minibuf_window)
8655 && (current_buffer->clip_changed
8656 || XFASTINT (w->last_modified) < MODIFF
8657 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8658 && resize_mini_window (w, 0))
8659 {
8660 /* Resized active mini-window to fit the size of what it is
8661 showing if its contents might have changed. */
8662 must_finish = 1;
8663 consider_all_windows_p = 1;
8664 ++windows_or_buffers_changed;
8665 ++update_mode_lines;
8666
8667 /* If window configuration was changed, frames may have been
8668 marked garbaged. Clear them or we will experience
8669 surprises wrt scrolling. */
8670 if (frame_garbaged)
8671 clear_garbaged_frames ();
8672 }
8673
8674
8675 /* If showing the region, and mark has changed, we must redisplay
8676 the whole window. The assignment to this_line_start_pos prevents
8677 the optimization directly below this if-statement. */
8678 if (((!NILP (Vtransient_mark_mode)
8679 && !NILP (XBUFFER (w->buffer)->mark_active))
8680 != !NILP (w->region_showing))
8681 || (!NILP (w->region_showing)
8682 && !EQ (w->region_showing,
8683 Fmarker_position (XBUFFER (w->buffer)->mark))))
8684 CHARPOS (this_line_start_pos) = 0;
8685
8686 /* Optimize the case that only the line containing the cursor in the
8687 selected window has changed. Variables starting with this_ are
8688 set in display_line and record information about the line
8689 containing the cursor. */
8690 tlbufpos = this_line_start_pos;
8691 tlendpos = this_line_end_pos;
8692 if (!consider_all_windows_p
8693 && CHARPOS (tlbufpos) > 0
8694 && NILP (w->update_mode_line)
8695 && !current_buffer->clip_changed
8696 && FRAME_VISIBLE_P (XFRAME (w->frame))
8697 && !FRAME_OBSCURED_P (XFRAME (w->frame))
8698 /* Make sure recorded data applies to current buffer, etc. */
8699 && this_line_buffer == current_buffer
8700 && current_buffer == XBUFFER (w->buffer)
8701 && NILP (w->force_start)
8702 /* Point must be on the line that we have info recorded about. */
8703 && PT >= CHARPOS (tlbufpos)
8704 && PT <= Z - CHARPOS (tlendpos)
8705 /* All text outside that line, including its final newline,
8706 must be unchanged */
8707 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
8708 CHARPOS (tlendpos)))
8709 {
8710 if (CHARPOS (tlbufpos) > BEGV
8711 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
8712 && (CHARPOS (tlbufpos) == ZV
8713 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
8714 /* Former continuation line has disappeared by becoming empty */
8715 goto cancel;
8716 else if (XFASTINT (w->last_modified) < MODIFF
8717 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
8718 || MINI_WINDOW_P (w))
8719 {
8720 /* We have to handle the case of continuation around a
8721 wide-column character (See the comment in indent.c around
8722 line 885).
8723
8724 For instance, in the following case:
8725
8726 -------- Insert --------
8727 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
8728 J_I_ ==> J_I_ `^^' are cursors.
8729 ^^ ^^
8730 -------- --------
8731
8732 As we have to redraw the line above, we should goto cancel. */
8733
8734 struct it it;
8735 int line_height_before = this_line_pixel_height;
8736
8737 /* Note that start_display will handle the case that the
8738 line starting at tlbufpos is a continuation lines. */
8739 start_display (&it, w, tlbufpos);
8740
8741 /* Implementation note: It this still necessary? */
8742 if (it.current_x != this_line_start_x)
8743 goto cancel;
8744
8745 TRACE ((stderr, "trying display optimization 1\n"));
8746 w->cursor.vpos = -1;
8747 overlay_arrow_seen = 0;
8748 it.vpos = this_line_vpos;
8749 it.current_y = this_line_y;
8750 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
8751 display_line (&it);
8752
8753 /* If line contains point, is not continued,
8754 and ends at same distance from eob as before, we win */
8755 if (w->cursor.vpos >= 0
8756 /* Line is not continued, otherwise this_line_start_pos
8757 would have been set to 0 in display_line. */
8758 && CHARPOS (this_line_start_pos)
8759 /* Line ends as before. */
8760 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
8761 /* Line has same height as before. Otherwise other lines
8762 would have to be shifted up or down. */
8763 && this_line_pixel_height == line_height_before)
8764 {
8765 /* If this is not the window's last line, we must adjust
8766 the charstarts of the lines below. */
8767 if (it.current_y < it.last_visible_y)
8768 {
8769 struct glyph_row *row
8770 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
8771 int delta, delta_bytes;
8772
8773 if (Z - CHARPOS (tlendpos) == ZV)
8774 {
8775 /* This line ends at end of (accessible part of)
8776 buffer. There is no newline to count. */
8777 delta = (Z
8778 - CHARPOS (tlendpos)
8779 - MATRIX_ROW_START_CHARPOS (row));
8780 delta_bytes = (Z_BYTE
8781 - BYTEPOS (tlendpos)
8782 - MATRIX_ROW_START_BYTEPOS (row));
8783 }
8784 else
8785 {
8786 /* This line ends in a newline. Must take
8787 account of the newline and the rest of the
8788 text that follows. */
8789 delta = (Z
8790 - CHARPOS (tlendpos)
8791 - MATRIX_ROW_START_CHARPOS (row));
8792 delta_bytes = (Z_BYTE
8793 - BYTEPOS (tlendpos)
8794 - MATRIX_ROW_START_BYTEPOS (row));
8795 }
8796
8797 increment_matrix_positions (w->current_matrix,
8798 this_line_vpos + 1,
8799 w->current_matrix->nrows,
8800 delta, delta_bytes);
8801 }
8802
8803 /* If this row displays text now but previously didn't,
8804 or vice versa, w->window_end_vpos may have to be
8805 adjusted. */
8806 if ((it.glyph_row - 1)->displays_text_p)
8807 {
8808 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
8809 XSETINT (w->window_end_vpos, this_line_vpos);
8810 }
8811 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
8812 && this_line_vpos > 0)
8813 XSETINT (w->window_end_vpos, this_line_vpos - 1);
8814 w->window_end_valid = Qnil;
8815
8816 /* Update hint: No need to try to scroll in update_window. */
8817 w->desired_matrix->no_scrolling_p = 1;
8818
8819 #if GLYPH_DEBUG
8820 *w->desired_matrix->method = 0;
8821 debug_method_add (w, "optimization 1");
8822 #endif
8823 goto update;
8824 }
8825 else
8826 goto cancel;
8827 }
8828 else if (/* Cursor position hasn't changed. */
8829 PT == XFASTINT (w->last_point)
8830 /* Make sure the cursor was last displayed
8831 in this window. Otherwise we have to reposition it. */
8832 && 0 <= w->cursor.vpos
8833 && XINT (w->height) > w->cursor.vpos)
8834 {
8835 if (!must_finish)
8836 {
8837 do_pending_window_change (1);
8838
8839 /* We used to always goto end_of_redisplay here, but this
8840 isn't enough if we have a blinking cursor. */
8841 if (w->cursor_off_p == w->last_cursor_off_p)
8842 goto end_of_redisplay;
8843 }
8844 goto update;
8845 }
8846 /* If highlighting the region, or if the cursor is in the echo area,
8847 then we can't just move the cursor. */
8848 else if (! (!NILP (Vtransient_mark_mode)
8849 && !NILP (current_buffer->mark_active))
8850 && (EQ (selected_window, current_buffer->last_selected_window)
8851 || highlight_nonselected_windows)
8852 && NILP (w->region_showing)
8853 && NILP (Vshow_trailing_whitespace)
8854 && !cursor_in_echo_area)
8855 {
8856 struct it it;
8857 struct glyph_row *row;
8858
8859 /* Skip from tlbufpos to PT and see where it is. Note that
8860 PT may be in invisible text. If so, we will end at the
8861 next visible position. */
8862 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
8863 NULL, DEFAULT_FACE_ID);
8864 it.current_x = this_line_start_x;
8865 it.current_y = this_line_y;
8866 it.vpos = this_line_vpos;
8867
8868 /* The call to move_it_to stops in front of PT, but
8869 moves over before-strings. */
8870 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
8871
8872 if (it.vpos == this_line_vpos
8873 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
8874 row->enabled_p))
8875 {
8876 xassert (this_line_vpos == it.vpos);
8877 xassert (this_line_y == it.current_y);
8878 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
8879 #if GLYPH_DEBUG
8880 *w->desired_matrix->method = 0;
8881 debug_method_add (w, "optimization 3");
8882 #endif
8883 goto update;
8884 }
8885 else
8886 goto cancel;
8887 }
8888
8889 cancel:
8890 /* Text changed drastically or point moved off of line. */
8891 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
8892 }
8893
8894 CHARPOS (this_line_start_pos) = 0;
8895 consider_all_windows_p |= buffer_shared > 1;
8896 ++clear_face_cache_count;
8897
8898
8899 /* Build desired matrices, and update the display. If
8900 consider_all_windows_p is non-zero, do it for all windows on all
8901 frames. Otherwise do it for selected_window, only. */
8902
8903 if (consider_all_windows_p)
8904 {
8905 Lisp_Object tail, frame;
8906 int i, n = 0, size = 50;
8907 struct frame **updated
8908 = (struct frame **) alloca (size * sizeof *updated);
8909
8910 /* Clear the face cache eventually. */
8911 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
8912 {
8913 clear_face_cache (0);
8914 clear_face_cache_count = 0;
8915 }
8916
8917 /* Recompute # windows showing selected buffer. This will be
8918 incremented each time such a window is displayed. */
8919 buffer_shared = 0;
8920
8921 FOR_EACH_FRAME (tail, frame)
8922 {
8923 struct frame *f = XFRAME (frame);
8924
8925 if (FRAME_WINDOW_P (f) || f == sf)
8926 {
8927 if (clear_face_cache_count % 50 == 0
8928 && FRAME_WINDOW_P (f))
8929 clear_image_cache (f, 0);
8930
8931 /* Mark all the scroll bars to be removed; we'll redeem
8932 the ones we want when we redisplay their windows. */
8933 if (condemn_scroll_bars_hook)
8934 condemn_scroll_bars_hook (f);
8935
8936 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8937 redisplay_windows (FRAME_ROOT_WINDOW (f));
8938
8939 /* Any scroll bars which redisplay_windows should have
8940 nuked should now go away. */
8941 if (judge_scroll_bars_hook)
8942 judge_scroll_bars_hook (f);
8943
8944 /* If fonts changed, display again. */
8945 if (fonts_changed_p)
8946 goto retry;
8947
8948 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8949 {
8950 /* See if we have to hscroll. */
8951 if (hscroll_windows (f->root_window))
8952 goto retry;
8953
8954 /* Prevent various kinds of signals during display
8955 update. stdio is not robust about handling
8956 signals, which can cause an apparent I/O
8957 error. */
8958 if (interrupt_input)
8959 unrequest_sigio ();
8960 stop_polling ();
8961
8962 /* Update the display. */
8963 set_window_update_flags (XWINDOW (f->root_window), 1);
8964 pause |= update_frame (f, 0, 0);
8965 if (pause)
8966 break;
8967
8968 if (n == size)
8969 {
8970 int nbytes = size * sizeof *updated;
8971 struct frame **p = (struct frame **) alloca (2 * nbytes);
8972 bcopy (updated, p, nbytes);
8973 size *= 2;
8974 }
8975
8976 updated[n++] = f;
8977 }
8978 }
8979 }
8980
8981 /* Do the mark_window_display_accurate after all windows have
8982 been redisplayed because this call resets flags in buffers
8983 which are needed for proper redisplay. */
8984 for (i = 0; i < n; ++i)
8985 {
8986 struct frame *f = updated[i];
8987 mark_window_display_accurate (f->root_window, 1);
8988 if (frame_up_to_date_hook)
8989 frame_up_to_date_hook (f);
8990 }
8991 }
8992 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8993 {
8994 Lisp_Object mini_window;
8995 struct frame *mini_frame;
8996
8997 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
8998 internal_condition_case_1 (redisplay_window_1, selected_window, Qerror,
8999 redisplay_window_error);
9000
9001 /* Compare desired and current matrices, perform output. */
9002 update:
9003
9004 /* If fonts changed, display again. */
9005 if (fonts_changed_p)
9006 goto retry;
9007
9008 /* Prevent various kinds of signals during display update.
9009 stdio is not robust about handling signals,
9010 which can cause an apparent I/O error. */
9011 if (interrupt_input)
9012 unrequest_sigio ();
9013 stop_polling ();
9014
9015 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
9016 {
9017 if (hscroll_windows (selected_window))
9018 goto retry;
9019
9020 XWINDOW (selected_window)->must_be_updated_p = 1;
9021 pause = update_frame (sf, 0, 0);
9022 }
9023
9024 /* We may have called echo_area_display at the top of this
9025 function. If the echo area is on another frame, that may
9026 have put text on a frame other than the selected one, so the
9027 above call to update_frame would not have caught it. Catch
9028 it here. */
9029 mini_window = FRAME_MINIBUF_WINDOW (sf);
9030 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9031
9032 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
9033 {
9034 XWINDOW (mini_window)->must_be_updated_p = 1;
9035 pause |= update_frame (mini_frame, 0, 0);
9036 if (!pause && hscroll_windows (mini_window))
9037 goto retry;
9038 }
9039 }
9040
9041 /* If display was paused because of pending input, make sure we do a
9042 thorough update the next time. */
9043 if (pause)
9044 {
9045 /* Prevent the optimization at the beginning of
9046 redisplay_internal that tries a single-line update of the
9047 line containing the cursor in the selected window. */
9048 CHARPOS (this_line_start_pos) = 0;
9049
9050 /* Let the overlay arrow be updated the next time. */
9051 if (!NILP (last_arrow_position))
9052 {
9053 last_arrow_position = Qt;
9054 last_arrow_string = Qt;
9055 }
9056
9057 /* If we pause after scrolling, some rows in the current
9058 matrices of some windows are not valid. */
9059 if (!WINDOW_FULL_WIDTH_P (w)
9060 && !FRAME_WINDOW_P (XFRAME (w->frame)))
9061 update_mode_lines = 1;
9062 }
9063 else
9064 {
9065 if (!consider_all_windows_p)
9066 {
9067 /* This has already been done above if
9068 consider_all_windows_p is set. */
9069 mark_window_display_accurate_1 (w, 1);
9070
9071 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
9072 last_arrow_string = Voverlay_arrow_string;
9073
9074 if (frame_up_to_date_hook != 0)
9075 frame_up_to_date_hook (sf);
9076 }
9077
9078 update_mode_lines = 0;
9079 windows_or_buffers_changed = 0;
9080 }
9081
9082 /* Start SIGIO interrupts coming again. Having them off during the
9083 code above makes it less likely one will discard output, but not
9084 impossible, since there might be stuff in the system buffer here.
9085 But it is much hairier to try to do anything about that. */
9086 if (interrupt_input)
9087 request_sigio ();
9088 start_polling ();
9089
9090 /* If a frame has become visible which was not before, redisplay
9091 again, so that we display it. Expose events for such a frame
9092 (which it gets when becoming visible) don't call the parts of
9093 redisplay constructing glyphs, so simply exposing a frame won't
9094 display anything in this case. So, we have to display these
9095 frames here explicitly. */
9096 if (!pause)
9097 {
9098 Lisp_Object tail, frame;
9099 int new_count = 0;
9100
9101 FOR_EACH_FRAME (tail, frame)
9102 {
9103 int this_is_visible = 0;
9104
9105 if (XFRAME (frame)->visible)
9106 this_is_visible = 1;
9107 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
9108 if (XFRAME (frame)->visible)
9109 this_is_visible = 1;
9110
9111 if (this_is_visible)
9112 new_count++;
9113 }
9114
9115 if (new_count != number_of_visible_frames)
9116 windows_or_buffers_changed++;
9117 }
9118
9119 /* Change frame size now if a change is pending. */
9120 do_pending_window_change (1);
9121
9122 /* If we just did a pending size change, or have additional
9123 visible frames, redisplay again. */
9124 if (windows_or_buffers_changed && !pause)
9125 goto retry;
9126
9127 end_of_redisplay:;
9128
9129 unbind_to (count, Qnil);
9130 }
9131
9132
9133 /* Redisplay, but leave alone any recent echo area message unless
9134 another message has been requested in its place.
9135
9136 This is useful in situations where you need to redisplay but no
9137 user action has occurred, making it inappropriate for the message
9138 area to be cleared. See tracking_off and
9139 wait_reading_process_input for examples of these situations.
9140
9141 FROM_WHERE is an integer saying from where this function was
9142 called. This is useful for debugging. */
9143
9144 void
9145 redisplay_preserve_echo_area (from_where)
9146 int from_where;
9147 {
9148 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
9149
9150 if (!NILP (echo_area_buffer[1]))
9151 {
9152 /* We have a previously displayed message, but no current
9153 message. Redisplay the previous message. */
9154 display_last_displayed_message_p = 1;
9155 redisplay_internal (1);
9156 display_last_displayed_message_p = 0;
9157 }
9158 else
9159 redisplay_internal (1);
9160 }
9161
9162
9163 /* Function registered with record_unwind_protect in
9164 redisplay_internal. Clears the flag indicating that a redisplay is
9165 in progress. */
9166
9167 static Lisp_Object
9168 unwind_redisplay (old_redisplaying_p)
9169 Lisp_Object old_redisplaying_p;
9170 {
9171 redisplaying_p = XFASTINT (old_redisplaying_p);
9172 return Qnil;
9173 }
9174
9175
9176 /* Mark the display of window W as accurate or inaccurate. If
9177 ACCURATE_P is non-zero mark display of W as accurate. If
9178 ACCURATE_P is zero, arrange for W to be redisplayed the next time
9179 redisplay_internal is called. */
9180
9181 static void
9182 mark_window_display_accurate_1 (w, accurate_p)
9183 struct window *w;
9184 int accurate_p;
9185 {
9186 if (BUFFERP (w->buffer))
9187 {
9188 struct buffer *b = XBUFFER (w->buffer);
9189
9190 w->last_modified
9191 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
9192 w->last_overlay_modified
9193 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
9194 w->last_had_star
9195 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
9196
9197 if (accurate_p)
9198 {
9199 b->clip_changed = 0;
9200 b->prevent_redisplay_optimizations_p = 0;
9201
9202 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
9203 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
9204 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
9205 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
9206
9207 w->current_matrix->buffer = b;
9208 w->current_matrix->begv = BUF_BEGV (b);
9209 w->current_matrix->zv = BUF_ZV (b);
9210
9211 w->last_cursor = w->cursor;
9212 w->last_cursor_off_p = w->cursor_off_p;
9213
9214 if (w == XWINDOW (selected_window))
9215 w->last_point = make_number (BUF_PT (b));
9216 else
9217 w->last_point = make_number (XMARKER (w->pointm)->charpos);
9218 }
9219 }
9220
9221 if (accurate_p)
9222 {
9223 w->window_end_valid = w->buffer;
9224 #if 0 /* This is incorrect with variable-height lines. */
9225 xassert (XINT (w->window_end_vpos)
9226 < (XINT (w->height)
9227 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
9228 #endif
9229 w->update_mode_line = Qnil;
9230 }
9231 }
9232
9233
9234 /* Mark the display of windows in the window tree rooted at WINDOW as
9235 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
9236 windows as accurate. If ACCURATE_P is zero, arrange for windows to
9237 be redisplayed the next time redisplay_internal is called. */
9238
9239 void
9240 mark_window_display_accurate (window, accurate_p)
9241 Lisp_Object window;
9242 int accurate_p;
9243 {
9244 struct window *w;
9245
9246 for (; !NILP (window); window = w->next)
9247 {
9248 w = XWINDOW (window);
9249 mark_window_display_accurate_1 (w, accurate_p);
9250
9251 if (!NILP (w->vchild))
9252 mark_window_display_accurate (w->vchild, accurate_p);
9253 if (!NILP (w->hchild))
9254 mark_window_display_accurate (w->hchild, accurate_p);
9255 }
9256
9257 if (accurate_p)
9258 {
9259 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
9260 last_arrow_string = Voverlay_arrow_string;
9261 }
9262 else
9263 {
9264 /* Force a thorough redisplay the next time by setting
9265 last_arrow_position and last_arrow_string to t, which is
9266 unequal to any useful value of Voverlay_arrow_... */
9267 last_arrow_position = Qt;
9268 last_arrow_string = Qt;
9269 }
9270 }
9271
9272
9273 /* Return value in display table DP (Lisp_Char_Table *) for character
9274 C. Since a display table doesn't have any parent, we don't have to
9275 follow parent. Do not call this function directly but use the
9276 macro DISP_CHAR_VECTOR. */
9277
9278 Lisp_Object
9279 disp_char_vector (dp, c)
9280 struct Lisp_Char_Table *dp;
9281 int c;
9282 {
9283 Lisp_Object val;
9284
9285 if (ASCII_CHAR_P (c))
9286 {
9287 val = dp->ascii;
9288 if (SUB_CHAR_TABLE_P (val))
9289 val = XSUB_CHAR_TABLE (val)->contents[c];
9290 }
9291 else
9292 {
9293 Lisp_Object table;
9294
9295 XSETCHAR_TABLE (table, dp);
9296 val = char_table_ref (table, c);
9297 }
9298 if (NILP (val))
9299 val = dp->defalt;
9300 return val;
9301 }
9302
9303
9304 \f
9305 /***********************************************************************
9306 Window Redisplay
9307 ***********************************************************************/
9308
9309 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
9310
9311 static void
9312 redisplay_windows (window)
9313 Lisp_Object window;
9314 {
9315 while (!NILP (window))
9316 {
9317 struct window *w = XWINDOW (window);
9318
9319 if (!NILP (w->hchild))
9320 redisplay_windows (w->hchild);
9321 else if (!NILP (w->vchild))
9322 redisplay_windows (w->vchild);
9323 else
9324 {
9325 displayed_buffer = XBUFFER (w->buffer);
9326 internal_condition_case_1 (redisplay_window_0, window, Qerror,
9327 redisplay_window_error);
9328 }
9329
9330 window = w->next;
9331 }
9332 }
9333
9334 static Lisp_Object
9335 redisplay_window_error ()
9336 {
9337 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
9338 return Qnil;
9339 }
9340
9341 static Lisp_Object
9342 redisplay_window_0 (window)
9343 Lisp_Object window;
9344 {
9345 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
9346 redisplay_window (window, 0);
9347 return Qnil;
9348 }
9349
9350 static Lisp_Object
9351 redisplay_window_1 (window)
9352 Lisp_Object window;
9353 {
9354 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
9355 redisplay_window (window, 1);
9356 return Qnil;
9357 }
9358 \f
9359
9360 /* Increment GLYPH until it reaches END or CONDITION fails while
9361 adding (GLYPH)->pixel_width to X. */
9362
9363 #define SKIP_GLYPHS(glyph, end, x, condition) \
9364 do \
9365 { \
9366 (x) += (glyph)->pixel_width; \
9367 ++(glyph); \
9368 } \
9369 while ((glyph) < (end) && (condition))
9370
9371
9372 /* Set cursor position of W. PT is assumed to be displayed in ROW.
9373 DELTA is the number of bytes by which positions recorded in ROW
9374 differ from current buffer positions. */
9375
9376 void
9377 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
9378 struct window *w;
9379 struct glyph_row *row;
9380 struct glyph_matrix *matrix;
9381 int delta, delta_bytes, dy, dvpos;
9382 {
9383 struct glyph *glyph = row->glyphs[TEXT_AREA];
9384 struct glyph *end = glyph + row->used[TEXT_AREA];
9385 /* The first glyph that starts a sequence of glyphs from string. */
9386 struct glyph *string_start;
9387 /* The X coordinate of string_start. */
9388 int string_start_x;
9389 /* The last known character position. */
9390 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
9391 /* The last known character position before string_start. */
9392 int string_before_pos;
9393 int x = row->x;
9394 int pt_old = PT - delta;
9395
9396 /* Skip over glyphs not having an object at the start of the row.
9397 These are special glyphs like truncation marks on terminal
9398 frames. */
9399 if (row->displays_text_p)
9400 while (glyph < end
9401 && INTEGERP (glyph->object)
9402 && glyph->charpos < 0)
9403 {
9404 x += glyph->pixel_width;
9405 ++glyph;
9406 }
9407
9408 string_start = NULL;
9409 while (glyph < end
9410 && !INTEGERP (glyph->object)
9411 && (!BUFFERP (glyph->object)
9412 || (last_pos = glyph->charpos) < pt_old))
9413 {
9414 if (! STRINGP (glyph->object))
9415 {
9416 string_start = NULL;
9417 x += glyph->pixel_width;
9418 ++glyph;
9419 }
9420 else
9421 {
9422 string_before_pos = last_pos;
9423 string_start = glyph;
9424 string_start_x = x;
9425 /* Skip all glyphs from string. */
9426 SKIP_GLYPHS (glyph, end, x, STRINGP (glyph->object));
9427 }
9428 }
9429
9430 if (string_start
9431 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
9432 {
9433 /* We may have skipped over point because the previous glyphs
9434 are from string. As there's no easy way to know the
9435 character position of the current glyph, find the correct
9436 glyph on point by scanning from string_start again. */
9437 Lisp_Object limit;
9438 Lisp_Object string;
9439 int pos;
9440
9441 limit = make_number (pt_old + 1);
9442 end = glyph;
9443 glyph = string_start;
9444 x = string_start_x;
9445 string = glyph->object;
9446 pos = string_buffer_position (w, string, string_before_pos);
9447 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
9448 because we always put cursor after overlay strings. */
9449 while (pos == 0 && glyph < end)
9450 {
9451 string = glyph->object;
9452 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
9453 if (glyph < end)
9454 pos = string_buffer_position (w, glyph->object, string_before_pos);
9455 }
9456
9457 while (glyph < end)
9458 {
9459 pos = XINT (Fnext_single_char_property_change
9460 (make_number (pos), Qdisplay, Qnil, limit));
9461 if (pos > pt_old)
9462 break;
9463 /* Skip glyphs from the same string. */
9464 string = glyph->object;
9465 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
9466 /* Skip glyphs from an overlay. */
9467 while (glyph < end
9468 && ! string_buffer_position (w, glyph->object, pos))
9469 {
9470 string = glyph->object;
9471 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
9472 }
9473 }
9474 }
9475
9476 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
9477 w->cursor.x = x;
9478 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
9479 w->cursor.y = row->y + dy;
9480
9481 if (w == XWINDOW (selected_window))
9482 {
9483 if (!row->continued_p
9484 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
9485 && row->x == 0)
9486 {
9487 this_line_buffer = XBUFFER (w->buffer);
9488
9489 CHARPOS (this_line_start_pos)
9490 = MATRIX_ROW_START_CHARPOS (row) + delta;
9491 BYTEPOS (this_line_start_pos)
9492 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
9493
9494 CHARPOS (this_line_end_pos)
9495 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
9496 BYTEPOS (this_line_end_pos)
9497 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
9498
9499 this_line_y = w->cursor.y;
9500 this_line_pixel_height = row->height;
9501 this_line_vpos = w->cursor.vpos;
9502 this_line_start_x = row->x;
9503 }
9504 else
9505 CHARPOS (this_line_start_pos) = 0;
9506 }
9507 }
9508
9509
9510 /* Run window scroll functions, if any, for WINDOW with new window
9511 start STARTP. Sets the window start of WINDOW to that position.
9512
9513 We assume that the window's buffer is really current. */
9514
9515 static INLINE struct text_pos
9516 run_window_scroll_functions (window, startp)
9517 Lisp_Object window;
9518 struct text_pos startp;
9519 {
9520 struct window *w = XWINDOW (window);
9521 SET_MARKER_FROM_TEXT_POS (w->start, startp);
9522
9523 if (current_buffer != XBUFFER (w->buffer))
9524 abort ();
9525
9526 if (!NILP (Vwindow_scroll_functions))
9527 {
9528 run_hook_with_args_2 (Qwindow_scroll_functions, window,
9529 make_number (CHARPOS (startp)));
9530 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9531 /* In case the hook functions switch buffers. */
9532 if (current_buffer != XBUFFER (w->buffer))
9533 set_buffer_internal_1 (XBUFFER (w->buffer));
9534 }
9535
9536 return startp;
9537 }
9538
9539
9540 /* Modify the desired matrix of window W and W->vscroll so that the
9541 line containing the cursor is fully visible. If this requires
9542 larger matrices than are allocated, set fonts_changed_p and return
9543 0. */
9544
9545 static int
9546 make_cursor_line_fully_visible (w)
9547 struct window *w;
9548 {
9549 struct glyph_matrix *matrix;
9550 struct glyph_row *row;
9551 int window_height;
9552
9553 /* It's not always possible to find the cursor, e.g, when a window
9554 is full of overlay strings. Don't do anything in that case. */
9555 if (w->cursor.vpos < 0)
9556 return 1;
9557
9558 matrix = w->desired_matrix;
9559 row = MATRIX_ROW (matrix, w->cursor.vpos);
9560
9561 /* If the cursor row is not partially visible, there's nothing
9562 to do. */
9563 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
9564 return 1;
9565
9566 /* If the row the cursor is in is taller than the window's height,
9567 it's not clear what to do, so do nothing. */
9568 window_height = window_box_height (w);
9569 if (row->height >= window_height)
9570 return 1;
9571
9572 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
9573 {
9574 int dy = row->height - row->visible_height;
9575 w->vscroll = 0;
9576 w->cursor.y += dy;
9577 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9578 }
9579 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
9580 {
9581 int dy = - (row->height - row->visible_height);
9582 w->vscroll = dy;
9583 w->cursor.y += dy;
9584 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9585 }
9586
9587 /* When we change the cursor y-position of the selected window,
9588 change this_line_y as well so that the display optimization for
9589 the cursor line of the selected window in redisplay_internal uses
9590 the correct y-position. */
9591 if (w == XWINDOW (selected_window))
9592 this_line_y = w->cursor.y;
9593
9594 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
9595 redisplay with larger matrices. */
9596 if (matrix->nrows < required_matrix_height (w))
9597 {
9598 fonts_changed_p = 1;
9599 return 0;
9600 }
9601
9602 return 1;
9603 }
9604
9605
9606 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
9607 non-zero means only WINDOW is redisplayed in redisplay_internal.
9608 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
9609 in redisplay_window to bring a partially visible line into view in
9610 the case that only the cursor has moved.
9611
9612 Value is
9613
9614 1 if scrolling succeeded
9615
9616 0 if scrolling didn't find point.
9617
9618 -1 if new fonts have been loaded so that we must interrupt
9619 redisplay, adjust glyph matrices, and try again. */
9620
9621 enum
9622 {
9623 SCROLLING_SUCCESS,
9624 SCROLLING_FAILED,
9625 SCROLLING_NEED_LARGER_MATRICES
9626 };
9627
9628 static int
9629 try_scrolling (window, just_this_one_p, scroll_conservatively,
9630 scroll_step, temp_scroll_step)
9631 Lisp_Object window;
9632 int just_this_one_p;
9633 int scroll_conservatively, scroll_step;
9634 int temp_scroll_step;
9635 {
9636 struct window *w = XWINDOW (window);
9637 struct frame *f = XFRAME (w->frame);
9638 struct text_pos scroll_margin_pos;
9639 struct text_pos pos;
9640 struct text_pos startp;
9641 struct it it;
9642 Lisp_Object window_end;
9643 int this_scroll_margin;
9644 int dy = 0;
9645 int scroll_max;
9646 int rc;
9647 int amount_to_scroll = 0;
9648 Lisp_Object aggressive;
9649 int height;
9650
9651 #if GLYPH_DEBUG
9652 debug_method_add (w, "try_scrolling");
9653 #endif
9654
9655 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9656
9657 /* Compute scroll margin height in pixels. We scroll when point is
9658 within this distance from the top or bottom of the window. */
9659 if (scroll_margin > 0)
9660 {
9661 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
9662 this_scroll_margin *= CANON_Y_UNIT (f);
9663 }
9664 else
9665 this_scroll_margin = 0;
9666
9667 /* Compute how much we should try to scroll maximally to bring point
9668 into view. */
9669 if (scroll_step || scroll_conservatively || temp_scroll_step)
9670 scroll_max = max (scroll_step,
9671 max (scroll_conservatively, temp_scroll_step));
9672 else if (NUMBERP (current_buffer->scroll_down_aggressively)
9673 || NUMBERP (current_buffer->scroll_up_aggressively))
9674 /* We're trying to scroll because of aggressive scrolling
9675 but no scroll_step is set. Choose an arbitrary one. Maybe
9676 there should be a variable for this. */
9677 scroll_max = 10;
9678 else
9679 scroll_max = 0;
9680 scroll_max *= CANON_Y_UNIT (f);
9681
9682 /* Decide whether we have to scroll down. Start at the window end
9683 and move this_scroll_margin up to find the position of the scroll
9684 margin. */
9685 window_end = Fwindow_end (window, Qt);
9686 CHARPOS (scroll_margin_pos) = XINT (window_end);
9687 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
9688 if (this_scroll_margin)
9689 {
9690 start_display (&it, w, scroll_margin_pos);
9691 move_it_vertically (&it, - this_scroll_margin);
9692 scroll_margin_pos = it.current.pos;
9693 }
9694
9695 if (PT >= CHARPOS (scroll_margin_pos))
9696 {
9697 int y0;
9698
9699 /* Point is in the scroll margin at the bottom of the window, or
9700 below. Compute a new window start that makes point visible. */
9701
9702 /* Compute the distance from the scroll margin to PT.
9703 Give up if the distance is greater than scroll_max. */
9704 start_display (&it, w, scroll_margin_pos);
9705 y0 = it.current_y;
9706 move_it_to (&it, PT, 0, it.last_visible_y, -1,
9707 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9708
9709 /* To make point visible, we have to move the window start
9710 down so that the line the cursor is in is visible, which
9711 means we have to add in the height of the cursor line. */
9712 dy = line_bottom_y (&it) - y0;
9713
9714 if (dy > scroll_max)
9715 return SCROLLING_FAILED;
9716
9717 /* Move the window start down. If scrolling conservatively,
9718 move it just enough down to make point visible. If
9719 scroll_step is set, move it down by scroll_step. */
9720 start_display (&it, w, startp);
9721
9722 if (scroll_conservatively)
9723 amount_to_scroll
9724 = max (max (dy, CANON_Y_UNIT (f)),
9725 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_up_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 else
9744 {
9745 /* See if point is inside the scroll margin at the top of the
9746 window. */
9747 scroll_margin_pos = startp;
9748 if (this_scroll_margin)
9749 {
9750 start_display (&it, w, startp);
9751 move_it_vertically (&it, this_scroll_margin);
9752 scroll_margin_pos = it.current.pos;
9753 }
9754
9755 if (PT < CHARPOS (scroll_margin_pos))
9756 {
9757 /* Point is in the scroll margin at the top of the window or
9758 above what is displayed in the window. */
9759 int y0;
9760
9761 /* Compute the vertical distance from PT to the scroll
9762 margin position. Give up if distance is greater than
9763 scroll_max. */
9764 SET_TEXT_POS (pos, PT, PT_BYTE);
9765 start_display (&it, w, pos);
9766 y0 = it.current_y;
9767 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
9768 it.last_visible_y, -1,
9769 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9770 dy = it.current_y - y0;
9771 if (dy > scroll_max)
9772 return SCROLLING_FAILED;
9773
9774 /* Compute new window start. */
9775 start_display (&it, w, startp);
9776
9777 if (scroll_conservatively)
9778 amount_to_scroll =
9779 max (dy, CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
9780 else if (scroll_step || temp_scroll_step)
9781 amount_to_scroll = scroll_max;
9782 else
9783 {
9784 aggressive = current_buffer->scroll_down_aggressively;
9785 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
9786 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
9787 if (NUMBERP (aggressive))
9788 amount_to_scroll = XFLOATINT (aggressive) * height;
9789 }
9790
9791 if (amount_to_scroll <= 0)
9792 return SCROLLING_FAILED;
9793
9794 move_it_vertically (&it, - amount_to_scroll);
9795 startp = it.current.pos;
9796 }
9797 }
9798
9799 /* Run window scroll functions. */
9800 startp = run_window_scroll_functions (window, startp);
9801
9802 /* Display the window. Give up if new fonts are loaded, or if point
9803 doesn't appear. */
9804 if (!try_window (window, startp))
9805 rc = SCROLLING_NEED_LARGER_MATRICES;
9806 else if (w->cursor.vpos < 0)
9807 {
9808 clear_glyph_matrix (w->desired_matrix);
9809 rc = SCROLLING_FAILED;
9810 }
9811 else
9812 {
9813 /* Maybe forget recorded base line for line number display. */
9814 if (!just_this_one_p
9815 || current_buffer->clip_changed
9816 || BEG_UNCHANGED < CHARPOS (startp))
9817 w->base_line_number = Qnil;
9818
9819 /* If cursor ends up on a partially visible line, shift display
9820 lines up or down. If that fails because we need larger
9821 matrices, give up. */
9822 if (!make_cursor_line_fully_visible (w))
9823 rc = SCROLLING_NEED_LARGER_MATRICES;
9824 else
9825 rc = SCROLLING_SUCCESS;
9826 }
9827
9828 return rc;
9829 }
9830
9831
9832 /* Compute a suitable window start for window W if display of W starts
9833 on a continuation line. Value is non-zero if a new window start
9834 was computed.
9835
9836 The new window start will be computed, based on W's width, starting
9837 from the start of the continued line. It is the start of the
9838 screen line with the minimum distance from the old start W->start. */
9839
9840 static int
9841 compute_window_start_on_continuation_line (w)
9842 struct window *w;
9843 {
9844 struct text_pos pos, start_pos;
9845 int window_start_changed_p = 0;
9846
9847 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
9848
9849 /* If window start is on a continuation line... Window start may be
9850 < BEGV in case there's invisible text at the start of the
9851 buffer (M-x rmail, for example). */
9852 if (CHARPOS (start_pos) > BEGV
9853 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
9854 {
9855 struct it it;
9856 struct glyph_row *row;
9857
9858 /* Handle the case that the window start is out of range. */
9859 if (CHARPOS (start_pos) < BEGV)
9860 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
9861 else if (CHARPOS (start_pos) > ZV)
9862 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
9863
9864 /* Find the start of the continued line. This should be fast
9865 because scan_buffer is fast (newline cache). */
9866 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
9867 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
9868 row, DEFAULT_FACE_ID);
9869 reseat_at_previous_visible_line_start (&it);
9870
9871 /* If the line start is "too far" away from the window start,
9872 say it takes too much time to compute a new window start. */
9873 if (CHARPOS (start_pos) - IT_CHARPOS (it)
9874 < XFASTINT (w->height) * XFASTINT (w->width))
9875 {
9876 int min_distance, distance;
9877
9878 /* Move forward by display lines to find the new window
9879 start. If window width was enlarged, the new start can
9880 be expected to be > the old start. If window width was
9881 decreased, the new window start will be < the old start.
9882 So, we're looking for the display line start with the
9883 minimum distance from the old window start. */
9884 pos = it.current.pos;
9885 min_distance = INFINITY;
9886 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
9887 distance < min_distance)
9888 {
9889 min_distance = distance;
9890 pos = it.current.pos;
9891 move_it_by_lines (&it, 1, 0);
9892 }
9893
9894 /* Set the window start there. */
9895 SET_MARKER_FROM_TEXT_POS (w->start, pos);
9896 window_start_changed_p = 1;
9897 }
9898 }
9899
9900 return window_start_changed_p;
9901 }
9902
9903
9904 /* Try cursor movement in case text has not changes in window WINDOW,
9905 with window start STARTP. Value is
9906
9907 CURSOR_MOVEMENT_SUCCESS if successful
9908
9909 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
9910
9911 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
9912 display. *SCROLL_STEP is set to 1, under certain circumstances, if
9913 we want to scroll as if scroll-step were set to 1. See the code.
9914
9915 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
9916 which case we have to abort this redisplay, and adjust matrices
9917 first. */
9918
9919 enum
9920 {
9921 CURSOR_MOVEMENT_SUCCESS,
9922 CURSOR_MOVEMENT_CANNOT_BE_USED,
9923 CURSOR_MOVEMENT_MUST_SCROLL,
9924 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
9925 };
9926
9927 static int
9928 try_cursor_movement (window, startp, scroll_step)
9929 Lisp_Object window;
9930 struct text_pos startp;
9931 int *scroll_step;
9932 {
9933 struct window *w = XWINDOW (window);
9934 struct frame *f = XFRAME (w->frame);
9935 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
9936
9937 #if GLYPH_DEBUG
9938 if (inhibit_try_cursor_movement)
9939 return rc;
9940 #endif
9941
9942 /* Handle case where text has not changed, only point, and it has
9943 not moved off the frame. */
9944 if (/* Point may be in this window. */
9945 PT >= CHARPOS (startp)
9946 /* Selective display hasn't changed. */
9947 && !current_buffer->clip_changed
9948 /* Function force-mode-line-update is used to force a thorough
9949 redisplay. It sets either windows_or_buffers_changed or
9950 update_mode_lines. So don't take a shortcut here for these
9951 cases. */
9952 && !update_mode_lines
9953 && !windows_or_buffers_changed
9954 /* Can't use this case if highlighting a region. When a
9955 region exists, cursor movement has to do more than just
9956 set the cursor. */
9957 && !(!NILP (Vtransient_mark_mode)
9958 && !NILP (current_buffer->mark_active))
9959 && NILP (w->region_showing)
9960 && NILP (Vshow_trailing_whitespace)
9961 /* Right after splitting windows, last_point may be nil. */
9962 && INTEGERP (w->last_point)
9963 /* This code is not used for mini-buffer for the sake of the case
9964 of redisplaying to replace an echo area message; since in
9965 that case the mini-buffer contents per se are usually
9966 unchanged. This code is of no real use in the mini-buffer
9967 since the handling of this_line_start_pos, etc., in redisplay
9968 handles the same cases. */
9969 && !EQ (window, minibuf_window)
9970 /* When splitting windows or for new windows, it happens that
9971 redisplay is called with a nil window_end_vpos or one being
9972 larger than the window. This should really be fixed in
9973 window.c. I don't have this on my list, now, so we do
9974 approximately the same as the old redisplay code. --gerd. */
9975 && INTEGERP (w->window_end_vpos)
9976 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
9977 && (FRAME_WINDOW_P (f)
9978 || !MARKERP (Voverlay_arrow_position)
9979 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
9980 {
9981 int this_scroll_margin;
9982 struct glyph_row *row = NULL;
9983
9984 #if GLYPH_DEBUG
9985 debug_method_add (w, "cursor movement");
9986 #endif
9987
9988 /* Scroll if point within this distance from the top or bottom
9989 of the window. This is a pixel value. */
9990 this_scroll_margin = max (0, scroll_margin);
9991 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
9992 this_scroll_margin *= CANON_Y_UNIT (f);
9993
9994 /* Start with the row the cursor was displayed during the last
9995 not paused redisplay. Give up if that row is not valid. */
9996 if (w->last_cursor.vpos < 0
9997 || w->last_cursor.vpos >= w->current_matrix->nrows)
9998 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9999 else
10000 {
10001 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
10002 if (row->mode_line_p)
10003 ++row;
10004 if (!row->enabled_p)
10005 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10006 }
10007
10008 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
10009 {
10010 int scroll_p = 0;
10011 int last_y = window_text_bottom_y (w) - this_scroll_margin;
10012
10013 if (PT > XFASTINT (w->last_point))
10014 {
10015 /* Point has moved forward. */
10016 while (MATRIX_ROW_END_CHARPOS (row) < PT
10017 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
10018 {
10019 xassert (row->enabled_p);
10020 ++row;
10021 }
10022
10023 /* The end position of a row equals the start position
10024 of the next row. If PT is there, we would rather
10025 display it in the next line. */
10026 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
10027 && MATRIX_ROW_END_CHARPOS (row) == PT
10028 && !cursor_row_p (w, row))
10029 ++row;
10030
10031 /* If within the scroll margin, scroll. Note that
10032 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
10033 the next line would be drawn, and that
10034 this_scroll_margin can be zero. */
10035 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
10036 || PT > MATRIX_ROW_END_CHARPOS (row)
10037 /* Line is completely visible last line in window
10038 and PT is to be set in the next line. */
10039 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
10040 && PT == MATRIX_ROW_END_CHARPOS (row)
10041 && !row->ends_at_zv_p
10042 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
10043 scroll_p = 1;
10044 }
10045 else if (PT < XFASTINT (w->last_point))
10046 {
10047 /* Cursor has to be moved backward. Note that PT >=
10048 CHARPOS (startp) because of the outer
10049 if-statement. */
10050 while (!row->mode_line_p
10051 && (MATRIX_ROW_START_CHARPOS (row) > PT
10052 || (MATRIX_ROW_START_CHARPOS (row) == PT
10053 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
10054 && (row->y > this_scroll_margin
10055 || CHARPOS (startp) == BEGV))
10056 {
10057 xassert (row->enabled_p);
10058 --row;
10059 }
10060
10061 /* Consider the following case: Window starts at BEGV,
10062 there is invisible, intangible text at BEGV, so that
10063 display starts at some point START > BEGV. It can
10064 happen that we are called with PT somewhere between
10065 BEGV and START. Try to handle that case. */
10066 if (row < w->current_matrix->rows
10067 || row->mode_line_p)
10068 {
10069 row = w->current_matrix->rows;
10070 if (row->mode_line_p)
10071 ++row;
10072 }
10073
10074 /* Due to newlines in overlay strings, we may have to
10075 skip forward over overlay strings. */
10076 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
10077 && MATRIX_ROW_END_CHARPOS (row) == PT
10078 && !cursor_row_p (w, row))
10079 ++row;
10080
10081 /* If within the scroll margin, scroll. */
10082 if (row->y < this_scroll_margin
10083 && CHARPOS (startp) != BEGV)
10084 scroll_p = 1;
10085 }
10086
10087 if (PT < MATRIX_ROW_START_CHARPOS (row)
10088 || PT > MATRIX_ROW_END_CHARPOS (row))
10089 {
10090 /* if PT is not in the glyph row, give up. */
10091 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10092 }
10093 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
10094 {
10095 if (PT == MATRIX_ROW_END_CHARPOS (row)
10096 && !row->ends_at_zv_p
10097 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
10098 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10099 else if (row->height > window_box_height (w))
10100 {
10101 /* If we end up in a partially visible line, let's
10102 make it fully visible, except when it's taller
10103 than the window, in which case we can't do much
10104 about it. */
10105 *scroll_step = 1;
10106 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10107 }
10108 else
10109 {
10110 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10111 try_window (window, startp);
10112 if (!make_cursor_line_fully_visible (w))
10113 rc = CURSOR_MOVEMENT_NEED_LARGER_MATRICES;
10114 else
10115 rc = CURSOR_MOVEMENT_SUCCESS;
10116 }
10117 }
10118 else if (scroll_p)
10119 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10120 else
10121 {
10122 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10123 rc = CURSOR_MOVEMENT_SUCCESS;
10124 }
10125 }
10126 }
10127
10128 return rc;
10129 }
10130
10131
10132 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
10133 selected_window is redisplayed. */
10134
10135 static void
10136 redisplay_window (window, just_this_one_p)
10137 Lisp_Object window;
10138 int just_this_one_p;
10139 {
10140 struct window *w = XWINDOW (window);
10141 struct frame *f = XFRAME (w->frame);
10142 struct buffer *buffer = XBUFFER (w->buffer);
10143 struct buffer *old = current_buffer;
10144 struct text_pos lpoint, opoint, startp;
10145 int update_mode_line;
10146 int tem;
10147 struct it it;
10148 /* Record it now because it's overwritten. */
10149 int current_matrix_up_to_date_p = 0;
10150 int temp_scroll_step = 0;
10151 int count = BINDING_STACK_SIZE ();
10152 int rc;
10153
10154 SET_TEXT_POS (lpoint, PT, PT_BYTE);
10155 opoint = lpoint;
10156
10157 /* W must be a leaf window here. */
10158 xassert (!NILP (w->buffer));
10159 #if GLYPH_DEBUG
10160 *w->desired_matrix->method = 0;
10161 #endif
10162
10163 specbind (Qinhibit_point_motion_hooks, Qt);
10164
10165 reconsider_clip_changes (w, buffer);
10166
10167 /* Has the mode line to be updated? */
10168 update_mode_line = (!NILP (w->update_mode_line)
10169 || update_mode_lines
10170 || buffer->clip_changed);
10171
10172 if (MINI_WINDOW_P (w))
10173 {
10174 if (w == XWINDOW (echo_area_window)
10175 && !NILP (echo_area_buffer[0]))
10176 {
10177 if (update_mode_line)
10178 /* We may have to update a tty frame's menu bar or a
10179 tool-bar. Example `M-x C-h C-h C-g'. */
10180 goto finish_menu_bars;
10181 else
10182 /* We've already displayed the echo area glyphs in this window. */
10183 goto finish_scroll_bars;
10184 }
10185 else if (w != XWINDOW (minibuf_window))
10186 {
10187 /* W is a mini-buffer window, but it's not the currently
10188 active one, so clear it. */
10189 int yb = window_text_bottom_y (w);
10190 struct glyph_row *row;
10191 int y;
10192
10193 for (y = 0, row = w->desired_matrix->rows;
10194 y < yb;
10195 y += row->height, ++row)
10196 blank_row (w, row, y);
10197 goto finish_scroll_bars;
10198 }
10199
10200 clear_glyph_matrix (w->desired_matrix);
10201 }
10202
10203 /* Otherwise set up data on this window; select its buffer and point
10204 value. */
10205 /* Really select the buffer, for the sake of buffer-local
10206 variables. */
10207 set_buffer_internal_1 (XBUFFER (w->buffer));
10208 SET_TEXT_POS (opoint, PT, PT_BYTE);
10209
10210 current_matrix_up_to_date_p
10211 = (!NILP (w->window_end_valid)
10212 && !current_buffer->clip_changed
10213 && XFASTINT (w->last_modified) >= MODIFF
10214 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
10215
10216 /* When windows_or_buffers_changed is non-zero, we can't rely on
10217 the window end being valid, so set it to nil there. */
10218 if (windows_or_buffers_changed)
10219 {
10220 /* If window starts on a continuation line, maybe adjust the
10221 window start in case the window's width changed. */
10222 if (XMARKER (w->start)->buffer == current_buffer)
10223 compute_window_start_on_continuation_line (w);
10224
10225 w->window_end_valid = Qnil;
10226 }
10227
10228 /* Some sanity checks. */
10229 CHECK_WINDOW_END (w);
10230 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
10231 abort ();
10232 if (BYTEPOS (opoint) < CHARPOS (opoint))
10233 abort ();
10234
10235 /* If %c is in mode line, update it if needed. */
10236 if (!NILP (w->column_number_displayed)
10237 /* This alternative quickly identifies a common case
10238 where no change is needed. */
10239 && !(PT == XFASTINT (w->last_point)
10240 && XFASTINT (w->last_modified) >= MODIFF
10241 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
10242 && XFASTINT (w->column_number_displayed) != current_column ())
10243 update_mode_line = 1;
10244
10245 /* Count number of windows showing the selected buffer. An indirect
10246 buffer counts as its base buffer. */
10247 if (!just_this_one_p)
10248 {
10249 struct buffer *current_base, *window_base;
10250 current_base = current_buffer;
10251 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
10252 if (current_base->base_buffer)
10253 current_base = current_base->base_buffer;
10254 if (window_base->base_buffer)
10255 window_base = window_base->base_buffer;
10256 if (current_base == window_base)
10257 buffer_shared++;
10258 }
10259
10260 /* Point refers normally to the selected window. For any other
10261 window, set up appropriate value. */
10262 if (!EQ (window, selected_window))
10263 {
10264 int new_pt = XMARKER (w->pointm)->charpos;
10265 int new_pt_byte = marker_byte_position (w->pointm);
10266 if (new_pt < BEGV)
10267 {
10268 new_pt = BEGV;
10269 new_pt_byte = BEGV_BYTE;
10270 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
10271 }
10272 else if (new_pt > (ZV - 1))
10273 {
10274 new_pt = ZV;
10275 new_pt_byte = ZV_BYTE;
10276 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
10277 }
10278
10279 /* We don't use SET_PT so that the point-motion hooks don't run. */
10280 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
10281 }
10282
10283 /* If any of the character widths specified in the display table
10284 have changed, invalidate the width run cache. It's true that
10285 this may be a bit late to catch such changes, but the rest of
10286 redisplay goes (non-fatally) haywire when the display table is
10287 changed, so why should we worry about doing any better? */
10288 if (current_buffer->width_run_cache)
10289 {
10290 struct Lisp_Char_Table *disptab = buffer_display_table ();
10291
10292 if (! disptab_matches_widthtab (disptab,
10293 XVECTOR (current_buffer->width_table)))
10294 {
10295 invalidate_region_cache (current_buffer,
10296 current_buffer->width_run_cache,
10297 BEG, Z);
10298 recompute_width_table (current_buffer, disptab);
10299 }
10300 }
10301
10302 /* If window-start is screwed up, choose a new one. */
10303 if (XMARKER (w->start)->buffer != current_buffer)
10304 goto recenter;
10305
10306 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10307
10308 /* If someone specified a new starting point but did not insist,
10309 check whether it can be used. */
10310 if (!NILP (w->optional_new_start)
10311 && CHARPOS (startp) >= BEGV
10312 && CHARPOS (startp) <= ZV)
10313 {
10314 w->optional_new_start = Qnil;
10315 start_display (&it, w, startp);
10316 move_it_to (&it, PT, 0, it.last_visible_y, -1,
10317 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
10318 if (IT_CHARPOS (it) == PT)
10319 w->force_start = Qt;
10320 }
10321
10322 /* Handle case where place to start displaying has been specified,
10323 unless the specified location is outside the accessible range. */
10324 if (!NILP (w->force_start)
10325 || w->frozen_window_start_p)
10326 {
10327 w->force_start = Qnil;
10328 w->vscroll = 0;
10329 w->window_end_valid = Qnil;
10330
10331 /* Forget any recorded base line for line number display. */
10332 if (!current_matrix_up_to_date_p
10333 || current_buffer->clip_changed)
10334 w->base_line_number = Qnil;
10335
10336 /* Redisplay the mode line. Select the buffer properly for that.
10337 Also, run the hook window-scroll-functions
10338 because we have scrolled. */
10339 /* Note, we do this after clearing force_start because
10340 if there's an error, it is better to forget about force_start
10341 than to get into an infinite loop calling the hook functions
10342 and having them get more errors. */
10343 if (!update_mode_line
10344 || ! NILP (Vwindow_scroll_functions))
10345 {
10346 update_mode_line = 1;
10347 w->update_mode_line = Qt;
10348 startp = run_window_scroll_functions (window, startp);
10349 }
10350
10351 w->last_modified = make_number (0);
10352 w->last_overlay_modified = make_number (0);
10353 if (CHARPOS (startp) < BEGV)
10354 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
10355 else if (CHARPOS (startp) > ZV)
10356 SET_TEXT_POS (startp, ZV, ZV_BYTE);
10357
10358 /* Redisplay, then check if cursor has been set during the
10359 redisplay. Give up if new fonts were loaded. */
10360 if (!try_window (window, startp))
10361 {
10362 w->force_start = Qt;
10363 clear_glyph_matrix (w->desired_matrix);
10364 goto finish_scroll_bars;
10365 }
10366
10367 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
10368 {
10369 /* If point does not appear, try to move point so it does
10370 appear. The desired matrix has been built above, so we
10371 can use it here. */
10372 int window_height;
10373 struct glyph_row *row;
10374
10375 window_height = window_box_height (w) / 2;
10376 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
10377 while (MATRIX_ROW_BOTTOM_Y (row) < window_height)
10378 ++row;
10379
10380 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
10381 MATRIX_ROW_START_BYTEPOS (row));
10382
10383 if (w != XWINDOW (selected_window))
10384 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
10385 else if (current_buffer == old)
10386 SET_TEXT_POS (lpoint, PT, PT_BYTE);
10387
10388 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
10389
10390 /* If we are highlighting the region, then we just changed
10391 the region, so redisplay to show it. */
10392 if (!NILP (Vtransient_mark_mode)
10393 && !NILP (current_buffer->mark_active))
10394 {
10395 clear_glyph_matrix (w->desired_matrix);
10396 if (!try_window (window, startp))
10397 goto need_larger_matrices;
10398 }
10399 }
10400
10401 if (!make_cursor_line_fully_visible (w))
10402 goto need_larger_matrices;
10403 #if GLYPH_DEBUG
10404 debug_method_add (w, "forced window start");
10405 #endif
10406 goto done;
10407 }
10408
10409 /* Handle case where text has not changed, only point, and it has
10410 not moved off the frame. */
10411 if (current_matrix_up_to_date_p
10412 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
10413 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
10414 {
10415 switch (rc)
10416 {
10417 case CURSOR_MOVEMENT_SUCCESS:
10418 goto done;
10419
10420 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
10421 goto need_larger_matrices;
10422
10423 case CURSOR_MOVEMENT_MUST_SCROLL:
10424 goto try_to_scroll;
10425
10426 default:
10427 abort ();
10428 }
10429 }
10430 /* If current starting point was originally the beginning of a line
10431 but no longer is, find a new starting point. */
10432 else if (!NILP (w->start_at_line_beg)
10433 && !(CHARPOS (startp) <= BEGV
10434 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
10435 {
10436 #if GLYPH_DEBUG
10437 debug_method_add (w, "recenter 1");
10438 #endif
10439 goto recenter;
10440 }
10441
10442 /* Try scrolling with try_window_id. Value is > 0 if update has
10443 been done, it is -1 if we know that the same window start will
10444 not work. It is 0 if unsuccessful for some other reason. */
10445 else if ((tem = try_window_id (w)) != 0)
10446 {
10447 #if GLYPH_DEBUG
10448 debug_method_add (w, "try_window_id %d", tem);
10449 #endif
10450
10451 if (fonts_changed_p)
10452 goto need_larger_matrices;
10453 if (tem > 0)
10454 goto done;
10455
10456 /* Otherwise try_window_id has returned -1 which means that we
10457 don't want the alternative below this comment to execute. */
10458 }
10459 else if (CHARPOS (startp) >= BEGV
10460 && CHARPOS (startp) <= ZV
10461 && PT >= CHARPOS (startp)
10462 && (CHARPOS (startp) < ZV
10463 /* Avoid starting at end of buffer. */
10464 || CHARPOS (startp) == BEGV
10465 || (XFASTINT (w->last_modified) >= MODIFF
10466 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
10467 {
10468 #if GLYPH_DEBUG
10469 debug_method_add (w, "same window start");
10470 #endif
10471
10472 /* Try to redisplay starting at same place as before.
10473 If point has not moved off frame, accept the results. */
10474 if (!current_matrix_up_to_date_p
10475 /* Don't use try_window_reusing_current_matrix in this case
10476 because a window scroll function can have changed the
10477 buffer. */
10478 || !NILP (Vwindow_scroll_functions)
10479 || MINI_WINDOW_P (w)
10480 || !try_window_reusing_current_matrix (w))
10481 {
10482 IF_DEBUG (debug_method_add (w, "1"));
10483 try_window (window, startp);
10484 }
10485
10486 if (fonts_changed_p)
10487 goto need_larger_matrices;
10488
10489 if (w->cursor.vpos >= 0)
10490 {
10491 if (!just_this_one_p
10492 || current_buffer->clip_changed
10493 || BEG_UNCHANGED < CHARPOS (startp))
10494 /* Forget any recorded base line for line number display. */
10495 w->base_line_number = Qnil;
10496
10497 if (!make_cursor_line_fully_visible (w))
10498 goto need_larger_matrices;
10499 goto done;
10500 }
10501 else
10502 clear_glyph_matrix (w->desired_matrix);
10503 }
10504
10505 try_to_scroll:
10506
10507 w->last_modified = make_number (0);
10508 w->last_overlay_modified = make_number (0);
10509
10510 /* Redisplay the mode line. Select the buffer properly for that. */
10511 if (!update_mode_line)
10512 {
10513 update_mode_line = 1;
10514 w->update_mode_line = Qt;
10515 }
10516
10517 /* Try to scroll by specified few lines. */
10518 if ((scroll_conservatively
10519 || scroll_step
10520 || temp_scroll_step
10521 || NUMBERP (current_buffer->scroll_up_aggressively)
10522 || NUMBERP (current_buffer->scroll_down_aggressively))
10523 && !current_buffer->clip_changed
10524 && CHARPOS (startp) >= BEGV
10525 && CHARPOS (startp) <= ZV)
10526 {
10527 /* The function returns -1 if new fonts were loaded, 1 if
10528 successful, 0 if not successful. */
10529 int rc = try_scrolling (window, just_this_one_p,
10530 scroll_conservatively,
10531 scroll_step,
10532 temp_scroll_step);
10533 switch (rc)
10534 {
10535 case SCROLLING_SUCCESS:
10536 goto done;
10537
10538 case SCROLLING_NEED_LARGER_MATRICES:
10539 goto need_larger_matrices;
10540
10541 case SCROLLING_FAILED:
10542 break;
10543
10544 default:
10545 abort ();
10546 }
10547 }
10548
10549 /* Finally, just choose place to start which centers point */
10550
10551 recenter:
10552
10553 #if GLYPH_DEBUG
10554 debug_method_add (w, "recenter");
10555 #endif
10556
10557 /* w->vscroll = 0; */
10558
10559 /* Forget any previously recorded base line for line number display. */
10560 if (!current_matrix_up_to_date_p
10561 || current_buffer->clip_changed)
10562 w->base_line_number = Qnil;
10563
10564 /* Move backward half the height of the window. */
10565 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10566 it.current_y = it.last_visible_y;
10567 move_it_vertically_backward (&it, window_box_height (w) / 2);
10568 xassert (IT_CHARPOS (it) >= BEGV);
10569
10570 /* The function move_it_vertically_backward may move over more
10571 than the specified y-distance. If it->w is small, e.g. a
10572 mini-buffer window, we may end up in front of the window's
10573 display area. Start displaying at the start of the line
10574 containing PT in this case. */
10575 if (it.current_y <= 0)
10576 {
10577 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10578 move_it_vertically (&it, 0);
10579 xassert (IT_CHARPOS (it) <= PT);
10580 it.current_y = 0;
10581 }
10582
10583 it.current_x = it.hpos = 0;
10584
10585 /* Set startp here explicitly in case that helps avoid an infinite loop
10586 in case the window-scroll-functions functions get errors. */
10587 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
10588
10589 /* Run scroll hooks. */
10590 startp = run_window_scroll_functions (window, it.current.pos);
10591
10592 /* Redisplay the window. */
10593 if (!current_matrix_up_to_date_p
10594 || windows_or_buffers_changed
10595 /* Don't use try_window_reusing_current_matrix in this case
10596 because it can have changed the buffer. */
10597 || !NILP (Vwindow_scroll_functions)
10598 || !just_this_one_p
10599 || MINI_WINDOW_P (w)
10600 || !try_window_reusing_current_matrix (w))
10601 try_window (window, startp);
10602
10603 /* If new fonts have been loaded (due to fontsets), give up. We
10604 have to start a new redisplay since we need to re-adjust glyph
10605 matrices. */
10606 if (fonts_changed_p)
10607 goto need_larger_matrices;
10608
10609 /* If cursor did not appear assume that the middle of the window is
10610 in the first line of the window. Do it again with the next line.
10611 (Imagine a window of height 100, displaying two lines of height
10612 60. Moving back 50 from it->last_visible_y will end in the first
10613 line.) */
10614 if (w->cursor.vpos < 0)
10615 {
10616 if (!NILP (w->window_end_valid)
10617 && PT >= Z - XFASTINT (w->window_end_pos))
10618 {
10619 clear_glyph_matrix (w->desired_matrix);
10620 move_it_by_lines (&it, 1, 0);
10621 try_window (window, it.current.pos);
10622 }
10623 else if (PT < IT_CHARPOS (it))
10624 {
10625 clear_glyph_matrix (w->desired_matrix);
10626 move_it_by_lines (&it, -1, 0);
10627 try_window (window, it.current.pos);
10628 }
10629 else
10630 {
10631 /* Not much we can do about it. */
10632 }
10633 }
10634
10635 /* Consider the following case: Window starts at BEGV, there is
10636 invisible, intangible text at BEGV, so that display starts at
10637 some point START > BEGV. It can happen that we are called with
10638 PT somewhere between BEGV and START. Try to handle that case. */
10639 if (w->cursor.vpos < 0)
10640 {
10641 struct glyph_row *row = w->current_matrix->rows;
10642 if (row->mode_line_p)
10643 ++row;
10644 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10645 }
10646
10647 if (!make_cursor_line_fully_visible (w))
10648 goto need_larger_matrices;
10649
10650 done:
10651
10652 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10653 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
10654 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
10655 ? Qt : Qnil);
10656
10657 /* Display the mode line, if we must. */
10658 if ((update_mode_line
10659 /* If window not full width, must redo its mode line
10660 if (a) the window to its side is being redone and
10661 (b) we do a frame-based redisplay. This is a consequence
10662 of how inverted lines are drawn in frame-based redisplay. */
10663 || (!just_this_one_p
10664 && !FRAME_WINDOW_P (f)
10665 && !WINDOW_FULL_WIDTH_P (w))
10666 /* Line number to display. */
10667 || INTEGERP (w->base_line_pos)
10668 /* Column number is displayed and different from the one displayed. */
10669 || (!NILP (w->column_number_displayed)
10670 && XFASTINT (w->column_number_displayed) != current_column ()))
10671 /* This means that the window has a mode line. */
10672 && (WINDOW_WANTS_MODELINE_P (w)
10673 || WINDOW_WANTS_HEADER_LINE_P (w)))
10674 {
10675 display_mode_lines (w);
10676
10677 /* If mode line height has changed, arrange for a thorough
10678 immediate redisplay using the correct mode line height. */
10679 if (WINDOW_WANTS_MODELINE_P (w)
10680 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
10681 {
10682 fonts_changed_p = 1;
10683 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
10684 = DESIRED_MODE_LINE_HEIGHT (w);
10685 }
10686
10687 /* If top line height has changed, arrange for a thorough
10688 immediate redisplay using the correct mode line height. */
10689 if (WINDOW_WANTS_HEADER_LINE_P (w)
10690 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
10691 {
10692 fonts_changed_p = 1;
10693 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
10694 = DESIRED_HEADER_LINE_HEIGHT (w);
10695 }
10696
10697 if (fonts_changed_p)
10698 goto need_larger_matrices;
10699 }
10700
10701 if (!line_number_displayed
10702 && !BUFFERP (w->base_line_pos))
10703 {
10704 w->base_line_pos = Qnil;
10705 w->base_line_number = Qnil;
10706 }
10707
10708 finish_menu_bars:
10709
10710 /* When we reach a frame's selected window, redo the frame's menu bar. */
10711 if (update_mode_line
10712 && EQ (FRAME_SELECTED_WINDOW (f), window))
10713 {
10714 int redisplay_menu_p = 0;
10715
10716 if (FRAME_WINDOW_P (f))
10717 {
10718 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
10719 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
10720 #else
10721 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10722 #endif
10723 }
10724 else
10725 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10726
10727 if (redisplay_menu_p)
10728 display_menu_bar (w);
10729
10730 #ifdef HAVE_WINDOW_SYSTEM
10731 if (WINDOWP (f->tool_bar_window)
10732 && (FRAME_TOOL_BAR_LINES (f) > 0
10733 || auto_resize_tool_bars_p))
10734 redisplay_tool_bar (f);
10735 #endif
10736 }
10737
10738 need_larger_matrices:
10739 ;
10740 finish_scroll_bars:
10741
10742 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
10743 {
10744 int start, end, whole;
10745
10746 /* Calculate the start and end positions for the current window.
10747 At some point, it would be nice to choose between scrollbars
10748 which reflect the whole buffer size, with special markers
10749 indicating narrowing, and scrollbars which reflect only the
10750 visible region.
10751
10752 Note that mini-buffers sometimes aren't displaying any text. */
10753 if (!MINI_WINDOW_P (w)
10754 || (w == XWINDOW (minibuf_window)
10755 && NILP (echo_area_buffer[0])))
10756 {
10757 whole = ZV - BEGV;
10758 start = marker_position (w->start) - BEGV;
10759 /* I don't think this is guaranteed to be right. For the
10760 moment, we'll pretend it is. */
10761 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
10762
10763 if (end < start)
10764 end = start;
10765 if (whole < (end - start))
10766 whole = end - start;
10767 }
10768 else
10769 start = end = whole = 0;
10770
10771 /* Indicate what this scroll bar ought to be displaying now. */
10772 set_vertical_scroll_bar_hook (w, end - start, whole, start);
10773
10774 /* Note that we actually used the scroll bar attached to this
10775 window, so it shouldn't be deleted at the end of redisplay. */
10776 redeem_scroll_bar_hook (w);
10777 }
10778
10779 /* Restore current_buffer and value of point in it. */
10780 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
10781 set_buffer_internal_1 (old);
10782 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
10783
10784 unbind_to (count, Qnil);
10785 }
10786
10787
10788 /* Build the complete desired matrix of WINDOW with a window start
10789 buffer position POS. Value is non-zero if successful. It is zero
10790 if fonts were loaded during redisplay which makes re-adjusting
10791 glyph matrices necessary. */
10792
10793 int
10794 try_window (window, pos)
10795 Lisp_Object window;
10796 struct text_pos pos;
10797 {
10798 struct window *w = XWINDOW (window);
10799 struct it it;
10800 struct glyph_row *last_text_row = NULL;
10801
10802 /* Make POS the new window start. */
10803 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
10804
10805 /* Mark cursor position as unknown. No overlay arrow seen. */
10806 w->cursor.vpos = -1;
10807 overlay_arrow_seen = 0;
10808
10809 /* Initialize iterator and info to start at POS. */
10810 start_display (&it, w, pos);
10811
10812 /* Display all lines of W. */
10813 while (it.current_y < it.last_visible_y)
10814 {
10815 if (display_line (&it))
10816 last_text_row = it.glyph_row - 1;
10817 if (fonts_changed_p)
10818 return 0;
10819 }
10820
10821 /* If bottom moved off end of frame, change mode line percentage. */
10822 if (XFASTINT (w->window_end_pos) <= 0
10823 && Z != IT_CHARPOS (it))
10824 w->update_mode_line = Qt;
10825
10826 /* Set window_end_pos to the offset of the last character displayed
10827 on the window from the end of current_buffer. Set
10828 window_end_vpos to its row number. */
10829 if (last_text_row)
10830 {
10831 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
10832 w->window_end_bytepos
10833 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10834 w->window_end_pos
10835 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10836 w->window_end_vpos
10837 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10838 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
10839 ->displays_text_p);
10840 }
10841 else
10842 {
10843 w->window_end_bytepos = 0;
10844 w->window_end_pos = w->window_end_vpos = make_number (0);
10845 }
10846
10847 /* But that is not valid info until redisplay finishes. */
10848 w->window_end_valid = Qnil;
10849 return 1;
10850 }
10851
10852
10853 \f
10854 /************************************************************************
10855 Window redisplay reusing current matrix when buffer has not changed
10856 ************************************************************************/
10857
10858 /* Try redisplay of window W showing an unchanged buffer with a
10859 different window start than the last time it was displayed by
10860 reusing its current matrix. Value is non-zero if successful.
10861 W->start is the new window start. */
10862
10863 static int
10864 try_window_reusing_current_matrix (w)
10865 struct window *w;
10866 {
10867 struct frame *f = XFRAME (w->frame);
10868 struct glyph_row *row, *bottom_row;
10869 struct it it;
10870 struct run run;
10871 struct text_pos start, new_start;
10872 int nrows_scrolled, i;
10873 struct glyph_row *last_text_row;
10874 struct glyph_row *last_reused_text_row;
10875 struct glyph_row *start_row;
10876 int start_vpos, min_y, max_y;
10877
10878 #if GLYPH_DEBUG
10879 if (inhibit_try_window_reusing)
10880 return 0;
10881 #endif
10882
10883 if (/* This function doesn't handle terminal frames. */
10884 !FRAME_WINDOW_P (f)
10885 /* Don't try to reuse the display if windows have been split
10886 or such. */
10887 || windows_or_buffers_changed)
10888 return 0;
10889
10890 /* Can't do this if region may have changed. */
10891 if ((!NILP (Vtransient_mark_mode)
10892 && !NILP (current_buffer->mark_active))
10893 || !NILP (w->region_showing)
10894 || !NILP (Vshow_trailing_whitespace))
10895 return 0;
10896
10897 /* If top-line visibility has changed, give up. */
10898 if (WINDOW_WANTS_HEADER_LINE_P (w)
10899 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
10900 return 0;
10901
10902 /* Give up if old or new display is scrolled vertically. We could
10903 make this function handle this, but right now it doesn't. */
10904 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10905 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
10906 return 0;
10907
10908 /* The variable new_start now holds the new window start. The old
10909 start `start' can be determined from the current matrix. */
10910 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
10911 start = start_row->start.pos;
10912 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
10913
10914 /* Clear the desired matrix for the display below. */
10915 clear_glyph_matrix (w->desired_matrix);
10916
10917 if (CHARPOS (new_start) <= CHARPOS (start))
10918 {
10919 int first_row_y;
10920
10921 /* Don't use this method if the display starts with an ellipsis
10922 displayed for invisible text. It's not easy to handle that case
10923 below, and it's certainly not worth the effort since this is
10924 not a frequent case. */
10925 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
10926 return 0;
10927
10928 IF_DEBUG (debug_method_add (w, "twu1"));
10929
10930 /* Display up to a row that can be reused. The variable
10931 last_text_row is set to the last row displayed that displays
10932 text. Note that it.vpos == 0 if or if not there is a
10933 header-line; it's not the same as the MATRIX_ROW_VPOS! */
10934 start_display (&it, w, new_start);
10935 first_row_y = it.current_y;
10936 w->cursor.vpos = -1;
10937 last_text_row = last_reused_text_row = NULL;
10938
10939 while (it.current_y < it.last_visible_y
10940 && IT_CHARPOS (it) < CHARPOS (start)
10941 && !fonts_changed_p)
10942 if (display_line (&it))
10943 last_text_row = it.glyph_row - 1;
10944
10945 /* A value of current_y < last_visible_y means that we stopped
10946 at the previous window start, which in turn means that we
10947 have at least one reusable row. */
10948 if (it.current_y < it.last_visible_y)
10949 {
10950 /* IT.vpos always starts from 0; it counts text lines. */
10951 nrows_scrolled = it.vpos;
10952
10953 /* Find PT if not already found in the lines displayed. */
10954 if (w->cursor.vpos < 0)
10955 {
10956 int dy = it.current_y - first_row_y;
10957
10958 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10959 row = row_containing_pos (w, PT, row, NULL, dy);
10960 if (row)
10961 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
10962 dy, nrows_scrolled);
10963 else
10964 {
10965 clear_glyph_matrix (w->desired_matrix);
10966 return 0;
10967 }
10968 }
10969
10970 /* Scroll the display. Do it before the current matrix is
10971 changed. The problem here is that update has not yet
10972 run, i.e. part of the current matrix is not up to date.
10973 scroll_run_hook will clear the cursor, and use the
10974 current matrix to get the height of the row the cursor is
10975 in. */
10976 run.current_y = first_row_y;
10977 run.desired_y = it.current_y;
10978 run.height = it.last_visible_y - it.current_y;
10979
10980 if (run.height > 0 && run.current_y != run.desired_y)
10981 {
10982 update_begin (f);
10983 rif->update_window_begin_hook (w);
10984 rif->clear_mouse_face (w);
10985 rif->scroll_run_hook (w, &run);
10986 rif->update_window_end_hook (w, 0, 0);
10987 update_end (f);
10988 }
10989
10990 /* Shift current matrix down by nrows_scrolled lines. */
10991 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
10992 rotate_matrix (w->current_matrix,
10993 start_vpos,
10994 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10995 nrows_scrolled);
10996
10997 /* Disable lines that must be updated. */
10998 for (i = 0; i < it.vpos; ++i)
10999 (start_row + i)->enabled_p = 0;
11000
11001 /* Re-compute Y positions. */
11002 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
11003 max_y = it.last_visible_y;
11004 for (row = start_row + nrows_scrolled;
11005 row < bottom_row;
11006 ++row)
11007 {
11008 row->y = it.current_y;
11009 row->visible_height = row->height;
11010
11011 if (row->y < min_y)
11012 row->visible_height -= min_y - row->y;
11013 if (row->y + row->height > max_y)
11014 row->visible_height -= row->y + row->height - max_y;
11015
11016 it.current_y += row->height;
11017
11018 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
11019 last_reused_text_row = row;
11020 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
11021 break;
11022 }
11023
11024 /* Disable lines in the current matrix which are now
11025 below the window. */
11026 for (++row; row < bottom_row; ++row)
11027 row->enabled_p = 0;
11028 }
11029
11030 /* Update window_end_pos etc.; last_reused_text_row is the last
11031 reused row from the current matrix containing text, if any.
11032 The value of last_text_row is the last displayed line
11033 containing text. */
11034 if (last_reused_text_row)
11035 {
11036 w->window_end_bytepos
11037 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
11038 w->window_end_pos
11039 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
11040 w->window_end_vpos
11041 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
11042 w->current_matrix));
11043 }
11044 else if (last_text_row)
11045 {
11046 w->window_end_bytepos
11047 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
11048 w->window_end_pos
11049 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
11050 w->window_end_vpos
11051 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
11052 }
11053 else
11054 {
11055 /* This window must be completely empty. */
11056 w->window_end_bytepos = 0;
11057 w->window_end_pos = w->window_end_vpos = make_number (0);
11058 }
11059 w->window_end_valid = Qnil;
11060
11061 /* Update hint: don't try scrolling again in update_window. */
11062 w->desired_matrix->no_scrolling_p = 1;
11063
11064 #if GLYPH_DEBUG
11065 debug_method_add (w, "try_window_reusing_current_matrix 1");
11066 #endif
11067 return 1;
11068 }
11069 else if (CHARPOS (new_start) > CHARPOS (start))
11070 {
11071 struct glyph_row *pt_row, *row;
11072 struct glyph_row *first_reusable_row;
11073 struct glyph_row *first_row_to_display;
11074 int dy;
11075 int yb = window_text_bottom_y (w);
11076
11077 /* Find the row starting at new_start, if there is one. Don't
11078 reuse a partially visible line at the end. */
11079 first_reusable_row = start_row;
11080 while (first_reusable_row->enabled_p
11081 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
11082 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
11083 < CHARPOS (new_start)))
11084 ++first_reusable_row;
11085
11086 /* Give up if there is no row to reuse. */
11087 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
11088 || !first_reusable_row->enabled_p
11089 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
11090 != CHARPOS (new_start)))
11091 return 0;
11092
11093 /* We can reuse fully visible rows beginning with
11094 first_reusable_row to the end of the window. Set
11095 first_row_to_display to the first row that cannot be reused.
11096 Set pt_row to the row containing point, if there is any. */
11097 pt_row = NULL;
11098 for (first_row_to_display = first_reusable_row;
11099 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
11100 ++first_row_to_display)
11101 {
11102 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
11103 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
11104 pt_row = first_row_to_display;
11105 }
11106
11107 /* Start displaying at the start of first_row_to_display. */
11108 xassert (first_row_to_display->y < yb);
11109 init_to_row_start (&it, w, first_row_to_display);
11110
11111 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
11112 - start_vpos);
11113 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
11114 - nrows_scrolled);
11115 it.current_y = (first_row_to_display->y - first_reusable_row->y
11116 + WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
11117
11118 /* Display lines beginning with first_row_to_display in the
11119 desired matrix. Set last_text_row to the last row displayed
11120 that displays text. */
11121 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
11122 if (pt_row == NULL)
11123 w->cursor.vpos = -1;
11124 last_text_row = NULL;
11125 while (it.current_y < it.last_visible_y && !fonts_changed_p)
11126 if (display_line (&it))
11127 last_text_row = it.glyph_row - 1;
11128
11129 /* Give up If point isn't in a row displayed or reused. */
11130 if (w->cursor.vpos < 0)
11131 {
11132 clear_glyph_matrix (w->desired_matrix);
11133 return 0;
11134 }
11135
11136 /* If point is in a reused row, adjust y and vpos of the cursor
11137 position. */
11138 if (pt_row)
11139 {
11140 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
11141 w->current_matrix);
11142 w->cursor.y -= first_reusable_row->y;
11143 }
11144
11145 /* Scroll the display. */
11146 run.current_y = first_reusable_row->y;
11147 run.desired_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
11148 run.height = it.last_visible_y - run.current_y;
11149 dy = run.current_y - run.desired_y;
11150
11151 if (run.height)
11152 {
11153 struct frame *f = XFRAME (WINDOW_FRAME (w));
11154 update_begin (f);
11155 rif->update_window_begin_hook (w);
11156 rif->clear_mouse_face (w);
11157 rif->scroll_run_hook (w, &run);
11158 rif->update_window_end_hook (w, 0, 0);
11159 update_end (f);
11160 }
11161
11162 /* Adjust Y positions of reused rows. */
11163 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
11164 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
11165 max_y = it.last_visible_y;
11166 for (row = first_reusable_row; row < first_row_to_display; ++row)
11167 {
11168 row->y -= dy;
11169 row->visible_height = row->height;
11170 if (row->y < min_y)
11171 row->visible_height -= min_y - row->y;
11172 if (row->y + row->height > max_y)
11173 row->visible_height -= row->y + row->height - max_y;
11174 }
11175
11176 /* Scroll the current matrix. */
11177 xassert (nrows_scrolled > 0);
11178 rotate_matrix (w->current_matrix,
11179 start_vpos,
11180 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
11181 -nrows_scrolled);
11182
11183 /* Disable rows not reused. */
11184 for (row -= nrows_scrolled; row < bottom_row; ++row)
11185 row->enabled_p = 0;
11186
11187 /* Adjust window end. A null value of last_text_row means that
11188 the window end is in reused rows which in turn means that
11189 only its vpos can have changed. */
11190 if (last_text_row)
11191 {
11192 w->window_end_bytepos
11193 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
11194 w->window_end_pos
11195 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
11196 w->window_end_vpos
11197 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
11198 }
11199 else
11200 {
11201 w->window_end_vpos
11202 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
11203 }
11204
11205 w->window_end_valid = Qnil;
11206 w->desired_matrix->no_scrolling_p = 1;
11207
11208 #if GLYPH_DEBUG
11209 debug_method_add (w, "try_window_reusing_current_matrix 2");
11210 #endif
11211 return 1;
11212 }
11213
11214 return 0;
11215 }
11216
11217
11218 \f
11219 /************************************************************************
11220 Window redisplay reusing current matrix when buffer has changed
11221 ************************************************************************/
11222
11223 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
11224 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
11225 int *, int *));
11226 static struct glyph_row *
11227 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
11228 struct glyph_row *));
11229
11230
11231 /* Return the last row in MATRIX displaying text. If row START is
11232 non-null, start searching with that row. IT gives the dimensions
11233 of the display. Value is null if matrix is empty; otherwise it is
11234 a pointer to the row found. */
11235
11236 static struct glyph_row *
11237 find_last_row_displaying_text (matrix, it, start)
11238 struct glyph_matrix *matrix;
11239 struct it *it;
11240 struct glyph_row *start;
11241 {
11242 struct glyph_row *row, *row_found;
11243
11244 /* Set row_found to the last row in IT->w's current matrix
11245 displaying text. The loop looks funny but think of partially
11246 visible lines. */
11247 row_found = NULL;
11248 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
11249 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
11250 {
11251 xassert (row->enabled_p);
11252 row_found = row;
11253 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
11254 break;
11255 ++row;
11256 }
11257
11258 return row_found;
11259 }
11260
11261
11262 /* Return the last row in the current matrix of W that is not affected
11263 by changes at the start of current_buffer that occurred since W's
11264 current matrix was built. Value is null if no such row exists.
11265
11266 BEG_UNCHANGED us the number of characters unchanged at the start of
11267 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
11268 first changed character in current_buffer. Characters at positions <
11269 BEG + BEG_UNCHANGED are at the same buffer positions as they were
11270 when the current matrix was built. */
11271
11272 static struct glyph_row *
11273 find_last_unchanged_at_beg_row (w)
11274 struct window *w;
11275 {
11276 int first_changed_pos = BEG + BEG_UNCHANGED;
11277 struct glyph_row *row;
11278 struct glyph_row *row_found = NULL;
11279 int yb = window_text_bottom_y (w);
11280
11281 /* Find the last row displaying unchanged text. */
11282 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11283 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
11284 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
11285 {
11286 if (/* If row ends before first_changed_pos, it is unchanged,
11287 except in some case. */
11288 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
11289 /* When row ends in ZV and we write at ZV it is not
11290 unchanged. */
11291 && !row->ends_at_zv_p
11292 /* When first_changed_pos is the end of a continued line,
11293 row is not unchanged because it may be no longer
11294 continued. */
11295 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
11296 && row->continued_p))
11297 row_found = row;
11298
11299 /* Stop if last visible row. */
11300 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
11301 break;
11302
11303 ++row;
11304 }
11305
11306 return row_found;
11307 }
11308
11309
11310 /* Find the first glyph row in the current matrix of W that is not
11311 affected by changes at the end of current_buffer since the
11312 time W's current matrix was built.
11313
11314 Return in *DELTA the number of chars by which buffer positions in
11315 unchanged text at the end of current_buffer must be adjusted.
11316
11317 Return in *DELTA_BYTES the corresponding number of bytes.
11318
11319 Value is null if no such row exists, i.e. all rows are affected by
11320 changes. */
11321
11322 static struct glyph_row *
11323 find_first_unchanged_at_end_row (w, delta, delta_bytes)
11324 struct window *w;
11325 int *delta, *delta_bytes;
11326 {
11327 struct glyph_row *row;
11328 struct glyph_row *row_found = NULL;
11329
11330 *delta = *delta_bytes = 0;
11331
11332 /* Display must not have been paused, otherwise the current matrix
11333 is not up to date. */
11334 if (NILP (w->window_end_valid))
11335 abort ();
11336
11337 /* A value of window_end_pos >= END_UNCHANGED means that the window
11338 end is in the range of changed text. If so, there is no
11339 unchanged row at the end of W's current matrix. */
11340 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
11341 return NULL;
11342
11343 /* Set row to the last row in W's current matrix displaying text. */
11344 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
11345
11346 /* If matrix is entirely empty, no unchanged row exists. */
11347 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
11348 {
11349 /* The value of row is the last glyph row in the matrix having a
11350 meaningful buffer position in it. The end position of row
11351 corresponds to window_end_pos. This allows us to translate
11352 buffer positions in the current matrix to current buffer
11353 positions for characters not in changed text. */
11354 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
11355 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
11356 int last_unchanged_pos, last_unchanged_pos_old;
11357 struct glyph_row *first_text_row
11358 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11359
11360 *delta = Z - Z_old;
11361 *delta_bytes = Z_BYTE - Z_BYTE_old;
11362
11363 /* Set last_unchanged_pos to the buffer position of the last
11364 character in the buffer that has not been changed. Z is the
11365 index + 1 of the last character in current_buffer, i.e. by
11366 subtracting END_UNCHANGED we get the index of the last
11367 unchanged character, and we have to add BEG to get its buffer
11368 position. */
11369 last_unchanged_pos = Z - END_UNCHANGED + BEG;
11370 last_unchanged_pos_old = last_unchanged_pos - *delta;
11371
11372 /* Search backward from ROW for a row displaying a line that
11373 starts at a minimum position >= last_unchanged_pos_old. */
11374 for (; row > first_text_row; --row)
11375 {
11376 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
11377 abort ();
11378
11379 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
11380 row_found = row;
11381 }
11382 }
11383
11384 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
11385 abort ();
11386
11387 return row_found;
11388 }
11389
11390
11391 /* Make sure that glyph rows in the current matrix of window W
11392 reference the same glyph memory as corresponding rows in the
11393 frame's frame matrix. This function is called after scrolling W's
11394 current matrix on a terminal frame in try_window_id and
11395 try_window_reusing_current_matrix. */
11396
11397 static void
11398 sync_frame_with_window_matrix_rows (w)
11399 struct window *w;
11400 {
11401 struct frame *f = XFRAME (w->frame);
11402 struct glyph_row *window_row, *window_row_end, *frame_row;
11403
11404 /* Preconditions: W must be a leaf window and full-width. Its frame
11405 must have a frame matrix. */
11406 xassert (NILP (w->hchild) && NILP (w->vchild));
11407 xassert (WINDOW_FULL_WIDTH_P (w));
11408 xassert (!FRAME_WINDOW_P (f));
11409
11410 /* If W is a full-width window, glyph pointers in W's current matrix
11411 have, by definition, to be the same as glyph pointers in the
11412 corresponding frame matrix. */
11413 window_row = w->current_matrix->rows;
11414 window_row_end = window_row + w->current_matrix->nrows;
11415 frame_row = f->current_matrix->rows + XFASTINT (w->top);
11416 while (window_row < window_row_end)
11417 {
11418 int area;
11419
11420 for (area = LEFT_MARGIN_AREA; area <= LAST_AREA; ++area)
11421 frame_row->glyphs[area] = window_row->glyphs[area];
11422
11423 /* Disable frame rows whose corresponding window rows have
11424 been disabled in try_window_id. */
11425 if (!window_row->enabled_p)
11426 frame_row->enabled_p = 0;
11427
11428 ++window_row, ++frame_row;
11429 }
11430 }
11431
11432
11433 /* Find the glyph row in window W containing CHARPOS. Consider all
11434 rows between START and END (not inclusive). END null means search
11435 all rows to the end of the display area of W. Value is the row
11436 containing CHARPOS or null. */
11437
11438 struct glyph_row *
11439 row_containing_pos (w, charpos, start, end, dy)
11440 struct window *w;
11441 int charpos;
11442 struct glyph_row *start, *end;
11443 int dy;
11444 {
11445 struct glyph_row *row = start;
11446 int last_y;
11447
11448 /* If we happen to start on a header-line, skip that. */
11449 if (row->mode_line_p)
11450 ++row;
11451
11452 if ((end && row >= end) || !row->enabled_p)
11453 return NULL;
11454
11455 last_y = window_text_bottom_y (w) - dy;
11456
11457 while ((end == NULL || row < end)
11458 && MATRIX_ROW_BOTTOM_Y (row) < last_y
11459 && (MATRIX_ROW_END_CHARPOS (row) < charpos
11460 || (MATRIX_ROW_END_CHARPOS (row) == charpos
11461 /* The end position of a row equals the start
11462 position of the next row. If CHARPOS is there, we
11463 would rather display it in the next line, except
11464 when this line ends in ZV. */
11465 && !row->ends_at_zv_p
11466 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))
11467 ++row;
11468
11469 /* Give up if CHARPOS not found. */
11470 if ((end && row >= end)
11471 || charpos < MATRIX_ROW_START_CHARPOS (row)
11472 || charpos > MATRIX_ROW_END_CHARPOS (row))
11473 row = NULL;
11474
11475 return row;
11476 }
11477
11478
11479 /* Try to redisplay window W by reusing its existing display. W's
11480 current matrix must be up to date when this function is called,
11481 i.e. window_end_valid must not be nil.
11482
11483 Value is
11484
11485 1 if display has been updated
11486 0 if otherwise unsuccessful
11487 -1 if redisplay with same window start is known not to succeed
11488
11489 The following steps are performed:
11490
11491 1. Find the last row in the current matrix of W that is not
11492 affected by changes at the start of current_buffer. If no such row
11493 is found, give up.
11494
11495 2. Find the first row in W's current matrix that is not affected by
11496 changes at the end of current_buffer. Maybe there is no such row.
11497
11498 3. Display lines beginning with the row + 1 found in step 1 to the
11499 row found in step 2 or, if step 2 didn't find a row, to the end of
11500 the window.
11501
11502 4. If cursor is not known to appear on the window, give up.
11503
11504 5. If display stopped at the row found in step 2, scroll the
11505 display and current matrix as needed.
11506
11507 6. Maybe display some lines at the end of W, if we must. This can
11508 happen under various circumstances, like a partially visible line
11509 becoming fully visible, or because newly displayed lines are displayed
11510 in smaller font sizes.
11511
11512 7. Update W's window end information. */
11513
11514 static int
11515 try_window_id (w)
11516 struct window *w;
11517 {
11518 struct frame *f = XFRAME (w->frame);
11519 struct glyph_matrix *current_matrix = w->current_matrix;
11520 struct glyph_matrix *desired_matrix = w->desired_matrix;
11521 struct glyph_row *last_unchanged_at_beg_row;
11522 struct glyph_row *first_unchanged_at_end_row;
11523 struct glyph_row *row;
11524 struct glyph_row *bottom_row;
11525 int bottom_vpos;
11526 struct it it;
11527 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
11528 struct text_pos start_pos;
11529 struct run run;
11530 int first_unchanged_at_end_vpos = 0;
11531 struct glyph_row *last_text_row, *last_text_row_at_end;
11532 struct text_pos start;
11533 int first_changed_charpos, last_changed_charpos;
11534
11535 #if GLYPH_DEBUG
11536 if (inhibit_try_window_id)
11537 return 0;
11538 #endif
11539
11540 /* This is handy for debugging. */
11541 #if 0
11542 #define GIVE_UP(X) \
11543 do { \
11544 fprintf (stderr, "try_window_id give up %d\n", (X)); \
11545 return 0; \
11546 } while (0)
11547 #else
11548 #define GIVE_UP(X) return 0
11549 #endif
11550
11551 SET_TEXT_POS_FROM_MARKER (start, w->start);
11552
11553 /* Don't use this for mini-windows because these can show
11554 messages and mini-buffers, and we don't handle that here. */
11555 if (MINI_WINDOW_P (w))
11556 GIVE_UP (1);
11557
11558 /* This flag is used to prevent redisplay optimizations. */
11559 if (windows_or_buffers_changed)
11560 GIVE_UP (2);
11561
11562 /* Verify that narrowing has not changed. This flag is also set to prevent
11563 redisplay optimizations. It would be nice to further
11564 reduce the number of cases where this prevents try_window_id. */
11565 if (current_buffer->clip_changed)
11566 GIVE_UP (3);
11567
11568 /* Window must either use window-based redisplay or be full width. */
11569 if (!FRAME_WINDOW_P (f)
11570 && (!line_ins_del_ok
11571 || !WINDOW_FULL_WIDTH_P (w)))
11572 GIVE_UP (4);
11573
11574 /* Give up if point is not known NOT to appear in W. */
11575 if (PT < CHARPOS (start))
11576 GIVE_UP (5);
11577
11578 /* Another way to prevent redisplay optimizations. */
11579 if (XFASTINT (w->last_modified) == 0)
11580 GIVE_UP (6);
11581
11582 /* Verify that window is not hscrolled. */
11583 if (XFASTINT (w->hscroll) != 0)
11584 GIVE_UP (7);
11585
11586 /* Verify that display wasn't paused. */
11587 if (NILP (w->window_end_valid))
11588 GIVE_UP (8);
11589
11590 /* Can't use this if highlighting a region because a cursor movement
11591 will do more than just set the cursor. */
11592 if (!NILP (Vtransient_mark_mode)
11593 && !NILP (current_buffer->mark_active))
11594 GIVE_UP (9);
11595
11596 /* Likewise if highlighting trailing whitespace. */
11597 if (!NILP (Vshow_trailing_whitespace))
11598 GIVE_UP (11);
11599
11600 /* Likewise if showing a region. */
11601 if (!NILP (w->region_showing))
11602 GIVE_UP (10);
11603
11604 /* Can use this if overlay arrow position and or string have changed. */
11605 if (!EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
11606 || !EQ (last_arrow_string, Voverlay_arrow_string))
11607 GIVE_UP (12);
11608
11609
11610 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
11611 only if buffer has really changed. The reason is that the gap is
11612 initially at Z for freshly visited files. The code below would
11613 set end_unchanged to 0 in that case. */
11614 if (MODIFF > SAVE_MODIFF
11615 /* This seems to happen sometimes after saving a buffer. */
11616 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
11617 {
11618 if (GPT - BEG < BEG_UNCHANGED)
11619 BEG_UNCHANGED = GPT - BEG;
11620 if (Z - GPT < END_UNCHANGED)
11621 END_UNCHANGED = Z - GPT;
11622 }
11623
11624 /* The position of the first and last character that has been changed. */
11625 first_changed_charpos = BEG + BEG_UNCHANGED;
11626 last_changed_charpos = Z - END_UNCHANGED;
11627
11628 /* If window starts after a line end, and the last change is in
11629 front of that newline, then changes don't affect the display.
11630 This case happens with stealth-fontification. Note that although
11631 the display is unchanged, glyph positions in the matrix have to
11632 be adjusted, of course. */
11633 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
11634 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
11635 && ((last_changed_charpos < CHARPOS (start)
11636 && CHARPOS (start) == BEGV)
11637 || (last_changed_charpos < CHARPOS (start) - 1
11638 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
11639 {
11640 int Z_old, delta, Z_BYTE_old, delta_bytes;
11641 struct glyph_row *r0;
11642
11643 /* Compute how many chars/bytes have been added to or removed
11644 from the buffer. */
11645 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
11646 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
11647 delta = Z - Z_old;
11648 delta_bytes = Z_BYTE - Z_BYTE_old;
11649
11650 /* Give up if PT is not in the window. Note that it already has
11651 been checked at the start of try_window_id that PT is not in
11652 front of the window start. */
11653 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
11654 GIVE_UP (13);
11655
11656 /* If window start is unchanged, we can reuse the whole matrix
11657 as is, after adjusting glyph positions. No need to compute
11658 the window end again, since its offset from Z hasn't changed. */
11659 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
11660 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
11661 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes)
11662 {
11663 /* Adjust positions in the glyph matrix. */
11664 if (delta || delta_bytes)
11665 {
11666 struct glyph_row *r1
11667 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11668 increment_matrix_positions (w->current_matrix,
11669 MATRIX_ROW_VPOS (r0, current_matrix),
11670 MATRIX_ROW_VPOS (r1, current_matrix),
11671 delta, delta_bytes);
11672 }
11673
11674 /* Set the cursor. */
11675 row = row_containing_pos (w, PT, r0, NULL, 0);
11676 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
11677 return 1;
11678 }
11679 }
11680
11681 /* Handle the case that changes are all below what is displayed in
11682 the window, and that PT is in the window. This shortcut cannot
11683 be taken if ZV is visible in the window, and text has been added
11684 there that is visible in the window. */
11685 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
11686 /* ZV is not visible in the window, or there are no
11687 changes at ZV, actually. */
11688 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
11689 || first_changed_charpos == last_changed_charpos))
11690 {
11691 struct glyph_row *r0;
11692
11693 /* Give up if PT is not in the window. Note that it already has
11694 been checked at the start of try_window_id that PT is not in
11695 front of the window start. */
11696 if (PT >= MATRIX_ROW_END_CHARPOS (row))
11697 GIVE_UP (14);
11698
11699 /* If window start is unchanged, we can reuse the whole matrix
11700 as is, without changing glyph positions since no text has
11701 been added/removed in front of the window end. */
11702 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
11703 if (TEXT_POS_EQUAL_P (start, r0->start.pos))
11704 {
11705 /* We have to compute the window end anew since text
11706 can have been added/removed after it. */
11707 w->window_end_pos
11708 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
11709 w->window_end_bytepos
11710 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11711
11712 /* Set the cursor. */
11713 row = row_containing_pos (w, PT, r0, NULL, 0);
11714 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
11715 return 2;
11716 }
11717 }
11718
11719 /* Give up if window start is in the changed area.
11720
11721 The condition used to read
11722
11723 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
11724
11725 but why that was tested escapes me at the moment. */
11726 if (CHARPOS (start) >= first_changed_charpos
11727 && CHARPOS (start) <= last_changed_charpos)
11728 GIVE_UP (15);
11729
11730 /* Check that window start agrees with the start of the first glyph
11731 row in its current matrix. Check this after we know the window
11732 start is not in changed text, otherwise positions would not be
11733 comparable. */
11734 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
11735 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
11736 GIVE_UP (16);
11737
11738 /* Give up if the window ends in strings. Overlay strings
11739 at the end are difficult to handle, so don't try. */
11740 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
11741 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
11742 GIVE_UP (20);
11743
11744 /* Compute the position at which we have to start displaying new
11745 lines. Some of the lines at the top of the window might be
11746 reusable because they are not displaying changed text. Find the
11747 last row in W's current matrix not affected by changes at the
11748 start of current_buffer. Value is null if changes start in the
11749 first line of window. */
11750 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
11751 if (last_unchanged_at_beg_row)
11752 {
11753 /* Avoid starting to display in the moddle of a character, a TAB
11754 for instance. This is easier than to set up the iterator
11755 exactly, and it's not a frequent case, so the additional
11756 effort wouldn't really pay off. */
11757 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
11758 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
11759 && last_unchanged_at_beg_row > w->current_matrix->rows)
11760 --last_unchanged_at_beg_row;
11761
11762 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
11763 GIVE_UP (17);
11764
11765 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
11766 GIVE_UP (18);
11767 start_pos = it.current.pos;
11768
11769 /* Start displaying new lines in the desired matrix at the same
11770 vpos we would use in the current matrix, i.e. below
11771 last_unchanged_at_beg_row. */
11772 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
11773 current_matrix);
11774 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11775 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
11776
11777 xassert (it.hpos == 0 && it.current_x == 0);
11778 }
11779 else
11780 {
11781 /* There are no reusable lines at the start of the window.
11782 Start displaying in the first line. */
11783 start_display (&it, w, start);
11784 start_pos = it.current.pos;
11785 }
11786
11787 /* Find the first row that is not affected by changes at the end of
11788 the buffer. Value will be null if there is no unchanged row, in
11789 which case we must redisplay to the end of the window. delta
11790 will be set to the value by which buffer positions beginning with
11791 first_unchanged_at_end_row have to be adjusted due to text
11792 changes. */
11793 first_unchanged_at_end_row
11794 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
11795 IF_DEBUG (debug_delta = delta);
11796 IF_DEBUG (debug_delta_bytes = delta_bytes);
11797
11798 /* Set stop_pos to the buffer position up to which we will have to
11799 display new lines. If first_unchanged_at_end_row != NULL, this
11800 is the buffer position of the start of the line displayed in that
11801 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
11802 that we don't stop at a buffer position. */
11803 stop_pos = 0;
11804 if (first_unchanged_at_end_row)
11805 {
11806 xassert (last_unchanged_at_beg_row == NULL
11807 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
11808
11809 /* If this is a continuation line, move forward to the next one
11810 that isn't. Changes in lines above affect this line.
11811 Caution: this may move first_unchanged_at_end_row to a row
11812 not displaying text. */
11813 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
11814 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11815 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11816 < it.last_visible_y))
11817 ++first_unchanged_at_end_row;
11818
11819 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11820 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11821 >= it.last_visible_y))
11822 first_unchanged_at_end_row = NULL;
11823 else
11824 {
11825 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
11826 + delta);
11827 first_unchanged_at_end_vpos
11828 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
11829 xassert (stop_pos >= Z - END_UNCHANGED);
11830 }
11831 }
11832 else if (last_unchanged_at_beg_row == NULL)
11833 GIVE_UP (19);
11834
11835
11836 #if GLYPH_DEBUG
11837
11838 /* Either there is no unchanged row at the end, or the one we have
11839 now displays text. This is a necessary condition for the window
11840 end pos calculation at the end of this function. */
11841 xassert (first_unchanged_at_end_row == NULL
11842 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
11843
11844 debug_last_unchanged_at_beg_vpos
11845 = (last_unchanged_at_beg_row
11846 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
11847 : -1);
11848 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
11849
11850 #endif /* GLYPH_DEBUG != 0 */
11851
11852
11853 /* Display new lines. Set last_text_row to the last new line
11854 displayed which has text on it, i.e. might end up as being the
11855 line where the window_end_vpos is. */
11856 w->cursor.vpos = -1;
11857 last_text_row = NULL;
11858 overlay_arrow_seen = 0;
11859 while (it.current_y < it.last_visible_y
11860 && !fonts_changed_p
11861 && (first_unchanged_at_end_row == NULL
11862 || IT_CHARPOS (it) < stop_pos))
11863 {
11864 if (display_line (&it))
11865 last_text_row = it.glyph_row - 1;
11866 }
11867
11868 if (fonts_changed_p)
11869 return -1;
11870
11871
11872 /* Compute differences in buffer positions, y-positions etc. for
11873 lines reused at the bottom of the window. Compute what we can
11874 scroll. */
11875 if (first_unchanged_at_end_row
11876 /* No lines reused because we displayed everything up to the
11877 bottom of the window. */
11878 && it.current_y < it.last_visible_y)
11879 {
11880 dvpos = (it.vpos
11881 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
11882 current_matrix));
11883 dy = it.current_y - first_unchanged_at_end_row->y;
11884 run.current_y = first_unchanged_at_end_row->y;
11885 run.desired_y = run.current_y + dy;
11886 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
11887 }
11888 else
11889 {
11890 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
11891 first_unchanged_at_end_row = NULL;
11892 }
11893 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
11894
11895
11896 /* Find the cursor if not already found. We have to decide whether
11897 PT will appear on this window (it sometimes doesn't, but this is
11898 not a very frequent case.) This decision has to be made before
11899 the current matrix is altered. A value of cursor.vpos < 0 means
11900 that PT is either in one of the lines beginning at
11901 first_unchanged_at_end_row or below the window. Don't care for
11902 lines that might be displayed later at the window end; as
11903 mentioned, this is not a frequent case. */
11904 if (w->cursor.vpos < 0)
11905 {
11906 /* Cursor in unchanged rows at the top? */
11907 if (PT < CHARPOS (start_pos)
11908 && last_unchanged_at_beg_row)
11909 {
11910 row = row_containing_pos (w, PT,
11911 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
11912 last_unchanged_at_beg_row + 1, 0);
11913 if (row)
11914 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11915 }
11916
11917 /* Start from first_unchanged_at_end_row looking for PT. */
11918 else if (first_unchanged_at_end_row)
11919 {
11920 row = row_containing_pos (w, PT - delta,
11921 first_unchanged_at_end_row, NULL, 0);
11922 if (row)
11923 set_cursor_from_row (w, row, w->current_matrix, delta,
11924 delta_bytes, dy, dvpos);
11925 }
11926
11927 /* Give up if cursor was not found. */
11928 if (w->cursor.vpos < 0)
11929 {
11930 clear_glyph_matrix (w->desired_matrix);
11931 return -1;
11932 }
11933 }
11934
11935 /* Don't let the cursor end in the scroll margins. */
11936 {
11937 int this_scroll_margin, cursor_height;
11938
11939 this_scroll_margin = max (0, scroll_margin);
11940 this_scroll_margin = min (this_scroll_margin,
11941 XFASTINT (w->height) / 4);
11942 this_scroll_margin *= CANON_Y_UNIT (it.f);
11943 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
11944
11945 if ((w->cursor.y < this_scroll_margin
11946 && CHARPOS (start) > BEGV)
11947 /* Don't take scroll margin into account at the bottom because
11948 old redisplay didn't do it either. */
11949 || w->cursor.y + cursor_height > it.last_visible_y)
11950 {
11951 w->cursor.vpos = -1;
11952 clear_glyph_matrix (w->desired_matrix);
11953 return -1;
11954 }
11955 }
11956
11957 /* Scroll the display. Do it before changing the current matrix so
11958 that xterm.c doesn't get confused about where the cursor glyph is
11959 found. */
11960 if (dy && run.height)
11961 {
11962 update_begin (f);
11963
11964 if (FRAME_WINDOW_P (f))
11965 {
11966 rif->update_window_begin_hook (w);
11967 rif->clear_mouse_face (w);
11968 rif->scroll_run_hook (w, &run);
11969 rif->update_window_end_hook (w, 0, 0);
11970 }
11971 else
11972 {
11973 /* Terminal frame. In this case, dvpos gives the number of
11974 lines to scroll by; dvpos < 0 means scroll up. */
11975 int first_unchanged_at_end_vpos
11976 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
11977 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
11978 int end = (XFASTINT (w->top)
11979 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
11980 + window_internal_height (w));
11981
11982 /* Perform the operation on the screen. */
11983 if (dvpos > 0)
11984 {
11985 /* Scroll last_unchanged_at_beg_row to the end of the
11986 window down dvpos lines. */
11987 set_terminal_window (end);
11988
11989 /* On dumb terminals delete dvpos lines at the end
11990 before inserting dvpos empty lines. */
11991 if (!scroll_region_ok)
11992 ins_del_lines (end - dvpos, -dvpos);
11993
11994 /* Insert dvpos empty lines in front of
11995 last_unchanged_at_beg_row. */
11996 ins_del_lines (from, dvpos);
11997 }
11998 else if (dvpos < 0)
11999 {
12000 /* Scroll up last_unchanged_at_beg_vpos to the end of
12001 the window to last_unchanged_at_beg_vpos - |dvpos|. */
12002 set_terminal_window (end);
12003
12004 /* Delete dvpos lines in front of
12005 last_unchanged_at_beg_vpos. ins_del_lines will set
12006 the cursor to the given vpos and emit |dvpos| delete
12007 line sequences. */
12008 ins_del_lines (from + dvpos, dvpos);
12009
12010 /* On a dumb terminal insert dvpos empty lines at the
12011 end. */
12012 if (!scroll_region_ok)
12013 ins_del_lines (end + dvpos, -dvpos);
12014 }
12015
12016 set_terminal_window (0);
12017 }
12018
12019 update_end (f);
12020 }
12021
12022 /* Shift reused rows of the current matrix to the right position.
12023 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
12024 text. */
12025 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
12026 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
12027 if (dvpos < 0)
12028 {
12029 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
12030 bottom_vpos, dvpos);
12031 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
12032 bottom_vpos, 0);
12033 }
12034 else if (dvpos > 0)
12035 {
12036 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
12037 bottom_vpos, dvpos);
12038 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
12039 first_unchanged_at_end_vpos + dvpos, 0);
12040 }
12041
12042 /* For frame-based redisplay, make sure that current frame and window
12043 matrix are in sync with respect to glyph memory. */
12044 if (!FRAME_WINDOW_P (f))
12045 sync_frame_with_window_matrix_rows (w);
12046
12047 /* Adjust buffer positions in reused rows. */
12048 if (delta)
12049 increment_matrix_positions (current_matrix,
12050 first_unchanged_at_end_vpos + dvpos,
12051 bottom_vpos, delta, delta_bytes);
12052
12053 /* Adjust Y positions. */
12054 if (dy)
12055 shift_glyph_matrix (w, current_matrix,
12056 first_unchanged_at_end_vpos + dvpos,
12057 bottom_vpos, dy);
12058
12059 if (first_unchanged_at_end_row)
12060 first_unchanged_at_end_row += dvpos;
12061
12062 /* If scrolling up, there may be some lines to display at the end of
12063 the window. */
12064 last_text_row_at_end = NULL;
12065 if (dy < 0)
12066 {
12067 /* Scrolling up can leave for example a partially visible line
12068 at the end of the window to be redisplayed. */
12069 /* Set last_row to the glyph row in the current matrix where the
12070 window end line is found. It has been moved up or down in
12071 the matrix by dvpos. */
12072 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
12073 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
12074
12075 /* If last_row is the window end line, it should display text. */
12076 xassert (last_row->displays_text_p);
12077
12078 /* If window end line was partially visible before, begin
12079 displaying at that line. Otherwise begin displaying with the
12080 line following it. */
12081 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
12082 {
12083 init_to_row_start (&it, w, last_row);
12084 it.vpos = last_vpos;
12085 it.current_y = last_row->y;
12086 }
12087 else
12088 {
12089 init_to_row_end (&it, w, last_row);
12090 it.vpos = 1 + last_vpos;
12091 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
12092 ++last_row;
12093 }
12094
12095 /* We may start in a continuation line. If so, we have to
12096 get the right continuation_lines_width and current_x. */
12097 it.continuation_lines_width = last_row->continuation_lines_width;
12098 it.hpos = it.current_x = 0;
12099
12100 /* Display the rest of the lines at the window end. */
12101 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
12102 while (it.current_y < it.last_visible_y
12103 && !fonts_changed_p)
12104 {
12105 /* Is it always sure that the display agrees with lines in
12106 the current matrix? I don't think so, so we mark rows
12107 displayed invalid in the current matrix by setting their
12108 enabled_p flag to zero. */
12109 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
12110 if (display_line (&it))
12111 last_text_row_at_end = it.glyph_row - 1;
12112 }
12113 }
12114
12115 /* Update window_end_pos and window_end_vpos. */
12116 if (first_unchanged_at_end_row
12117 && first_unchanged_at_end_row->y < it.last_visible_y
12118 && !last_text_row_at_end)
12119 {
12120 /* Window end line if one of the preserved rows from the current
12121 matrix. Set row to the last row displaying text in current
12122 matrix starting at first_unchanged_at_end_row, after
12123 scrolling. */
12124 xassert (first_unchanged_at_end_row->displays_text_p);
12125 row = find_last_row_displaying_text (w->current_matrix, &it,
12126 first_unchanged_at_end_row);
12127 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
12128
12129 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
12130 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
12131 w->window_end_vpos
12132 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
12133 xassert (w->window_end_bytepos >= 0);
12134 IF_DEBUG (debug_method_add (w, "A"));
12135 }
12136 else if (last_text_row_at_end)
12137 {
12138 w->window_end_pos
12139 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
12140 w->window_end_bytepos
12141 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
12142 w->window_end_vpos
12143 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
12144 xassert (w->window_end_bytepos >= 0);
12145 IF_DEBUG (debug_method_add (w, "B"));
12146 }
12147 else if (last_text_row)
12148 {
12149 /* We have displayed either to the end of the window or at the
12150 end of the window, i.e. the last row with text is to be found
12151 in the desired matrix. */
12152 w->window_end_pos
12153 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12154 w->window_end_bytepos
12155 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12156 w->window_end_vpos
12157 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
12158 xassert (w->window_end_bytepos >= 0);
12159 }
12160 else if (first_unchanged_at_end_row == NULL
12161 && last_text_row == NULL
12162 && last_text_row_at_end == NULL)
12163 {
12164 /* Displayed to end of window, but no line containing text was
12165 displayed. Lines were deleted at the end of the window. */
12166 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
12167 int vpos = XFASTINT (w->window_end_vpos);
12168 struct glyph_row *current_row = current_matrix->rows + vpos;
12169 struct glyph_row *desired_row = desired_matrix->rows + vpos;
12170
12171 for (row = NULL;
12172 row == NULL && vpos >= first_vpos;
12173 --vpos, --current_row, --desired_row)
12174 {
12175 if (desired_row->enabled_p)
12176 {
12177 if (desired_row->displays_text_p)
12178 row = desired_row;
12179 }
12180 else if (current_row->displays_text_p)
12181 row = current_row;
12182 }
12183
12184 xassert (row != NULL);
12185 w->window_end_vpos = make_number (vpos + 1);
12186 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
12187 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
12188 xassert (w->window_end_bytepos >= 0);
12189 IF_DEBUG (debug_method_add (w, "C"));
12190 }
12191 else
12192 abort ();
12193
12194 #if 0 /* This leads to problems, for instance when the cursor is
12195 at ZV, and the cursor line displays no text. */
12196 /* Disable rows below what's displayed in the window. This makes
12197 debugging easier. */
12198 enable_glyph_matrix_rows (current_matrix,
12199 XFASTINT (w->window_end_vpos) + 1,
12200 bottom_vpos, 0);
12201 #endif
12202
12203 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
12204 debug_end_vpos = XFASTINT (w->window_end_vpos));
12205
12206 /* Record that display has not been completed. */
12207 w->window_end_valid = Qnil;
12208 w->desired_matrix->no_scrolling_p = 1;
12209 return 3;
12210
12211 #undef GIVE_UP
12212 }
12213
12214
12215 \f
12216 /***********************************************************************
12217 More debugging support
12218 ***********************************************************************/
12219
12220 #if GLYPH_DEBUG
12221
12222 void dump_glyph_row P_ ((struct glyph_row *, int, int));
12223 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
12224 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
12225
12226
12227 /* Dump the contents of glyph matrix MATRIX on stderr.
12228
12229 GLYPHS 0 means don't show glyph contents.
12230 GLYPHS 1 means show glyphs in short form
12231 GLYPHS > 1 means show glyphs in long form. */
12232
12233 void
12234 dump_glyph_matrix (matrix, glyphs)
12235 struct glyph_matrix *matrix;
12236 int glyphs;
12237 {
12238 int i;
12239 for (i = 0; i < matrix->nrows; ++i)
12240 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
12241 }
12242
12243
12244 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
12245 the glyph row and area where the glyph comes from. */
12246
12247 void
12248 dump_glyph (row, glyph, area)
12249 struct glyph_row *row;
12250 struct glyph *glyph;
12251 int area;
12252 {
12253 if (glyph->type == CHAR_GLYPH)
12254 {
12255 fprintf (stderr,
12256 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12257 glyph - row->glyphs[TEXT_AREA],
12258 'C',
12259 glyph->charpos,
12260 (BUFFERP (glyph->object)
12261 ? 'B'
12262 : (STRINGP (glyph->object)
12263 ? 'S'
12264 : '-')),
12265 glyph->pixel_width,
12266 glyph->u.ch,
12267 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
12268 ? glyph->u.ch
12269 : '.'),
12270 glyph->face_id,
12271 glyph->left_box_line_p,
12272 glyph->right_box_line_p);
12273 }
12274 else if (glyph->type == STRETCH_GLYPH)
12275 {
12276 fprintf (stderr,
12277 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12278 glyph - row->glyphs[TEXT_AREA],
12279 'S',
12280 glyph->charpos,
12281 (BUFFERP (glyph->object)
12282 ? 'B'
12283 : (STRINGP (glyph->object)
12284 ? 'S'
12285 : '-')),
12286 glyph->pixel_width,
12287 0,
12288 '.',
12289 glyph->face_id,
12290 glyph->left_box_line_p,
12291 glyph->right_box_line_p);
12292 }
12293 else if (glyph->type == IMAGE_GLYPH)
12294 {
12295 fprintf (stderr,
12296 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12297 glyph - row->glyphs[TEXT_AREA],
12298 'I',
12299 glyph->charpos,
12300 (BUFFERP (glyph->object)
12301 ? 'B'
12302 : (STRINGP (glyph->object)
12303 ? 'S'
12304 : '-')),
12305 glyph->pixel_width,
12306 glyph->u.img_id,
12307 '.',
12308 glyph->face_id,
12309 glyph->left_box_line_p,
12310 glyph->right_box_line_p);
12311 }
12312 }
12313
12314
12315 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
12316 GLYPHS 0 means don't show glyph contents.
12317 GLYPHS 1 means show glyphs in short form
12318 GLYPHS > 1 means show glyphs in long form. */
12319
12320 void
12321 dump_glyph_row (row, vpos, glyphs)
12322 struct glyph_row *row;
12323 int vpos, glyphs;
12324 {
12325 if (glyphs != 1)
12326 {
12327 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
12328 fprintf (stderr, "=======================================================================\n");
12329
12330 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d\
12331 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
12332 vpos,
12333 MATRIX_ROW_START_CHARPOS (row),
12334 MATRIX_ROW_END_CHARPOS (row),
12335 row->used[TEXT_AREA],
12336 row->contains_overlapping_glyphs_p,
12337 row->enabled_p,
12338 row->truncated_on_left_p,
12339 row->truncated_on_right_p,
12340 row->overlay_arrow_p,
12341 row->continued_p,
12342 MATRIX_ROW_CONTINUATION_LINE_P (row),
12343 row->displays_text_p,
12344 row->ends_at_zv_p,
12345 row->fill_line_p,
12346 row->ends_in_middle_of_char_p,
12347 row->starts_in_middle_of_char_p,
12348 row->mouse_face_p,
12349 row->x,
12350 row->y,
12351 row->pixel_width,
12352 row->height,
12353 row->visible_height,
12354 row->ascent,
12355 row->phys_ascent);
12356 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
12357 row->end.overlay_string_index,
12358 row->continuation_lines_width);
12359 fprintf (stderr, "%9d %5d\n",
12360 CHARPOS (row->start.string_pos),
12361 CHARPOS (row->end.string_pos));
12362 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
12363 row->end.dpvec_index);
12364 }
12365
12366 if (glyphs > 1)
12367 {
12368 int area;
12369
12370 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12371 {
12372 struct glyph *glyph = row->glyphs[area];
12373 struct glyph *glyph_end = glyph + row->used[area];
12374
12375 /* Glyph for a line end in text. */
12376 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
12377 ++glyph_end;
12378
12379 if (glyph < glyph_end)
12380 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
12381
12382 for (; glyph < glyph_end; ++glyph)
12383 dump_glyph (row, glyph, area);
12384 }
12385 }
12386 else if (glyphs == 1)
12387 {
12388 int area;
12389
12390 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12391 {
12392 char *s = (char *) alloca (row->used[area] + 1);
12393 int i;
12394
12395 for (i = 0; i < row->used[area]; ++i)
12396 {
12397 struct glyph *glyph = row->glyphs[area] + i;
12398 if (glyph->type == CHAR_GLYPH
12399 && glyph->u.ch < 0x80
12400 && glyph->u.ch >= ' ')
12401 s[i] = glyph->u.ch;
12402 else
12403 s[i] = '.';
12404 }
12405
12406 s[i] = '\0';
12407 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
12408 }
12409 }
12410 }
12411
12412
12413 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
12414 Sdump_glyph_matrix, 0, 1, "p",
12415 doc: /* Dump the current matrix of the selected window to stderr.
12416 Shows contents of glyph row structures. With non-nil
12417 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
12418 glyphs in short form, otherwise show glyphs in long form. */)
12419 (glyphs)
12420 Lisp_Object glyphs;
12421 {
12422 struct window *w = XWINDOW (selected_window);
12423 struct buffer *buffer = XBUFFER (w->buffer);
12424
12425 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
12426 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
12427 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
12428 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
12429 fprintf (stderr, "=============================================\n");
12430 dump_glyph_matrix (w->current_matrix,
12431 NILP (glyphs) ? 0 : XINT (glyphs));
12432 return Qnil;
12433 }
12434
12435
12436 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
12437 doc: /* Dump glyph row ROW to stderr.
12438 GLYPH 0 means don't dump glyphs.
12439 GLYPH 1 means dump glyphs in short form.
12440 GLYPH > 1 or omitted means dump glyphs in long form. */)
12441 (row, glyphs)
12442 Lisp_Object row, glyphs;
12443 {
12444 struct glyph_matrix *matrix;
12445 int vpos;
12446
12447 CHECK_NUMBER (row);
12448 matrix = XWINDOW (selected_window)->current_matrix;
12449 vpos = XINT (row);
12450 if (vpos >= 0 && vpos < matrix->nrows)
12451 dump_glyph_row (MATRIX_ROW (matrix, vpos),
12452 vpos,
12453 INTEGERP (glyphs) ? XINT (glyphs) : 2);
12454 return Qnil;
12455 }
12456
12457
12458 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
12459 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
12460 GLYPH 0 means don't dump glyphs.
12461 GLYPH 1 means dump glyphs in short form.
12462 GLYPH > 1 or omitted means dump glyphs in long form. */)
12463 (row, glyphs)
12464 Lisp_Object row, glyphs;
12465 {
12466 struct frame *sf = SELECTED_FRAME ();
12467 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
12468 int vpos;
12469
12470 CHECK_NUMBER (row);
12471 vpos = XINT (row);
12472 if (vpos >= 0 && vpos < m->nrows)
12473 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
12474 INTEGERP (glyphs) ? XINT (glyphs) : 2);
12475 return Qnil;
12476 }
12477
12478
12479 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
12480 doc: /* Toggle tracing of redisplay.
12481 With ARG, turn tracing on if and only if ARG is positive. */)
12482 (arg)
12483 Lisp_Object arg;
12484 {
12485 if (NILP (arg))
12486 trace_redisplay_p = !trace_redisplay_p;
12487 else
12488 {
12489 arg = Fprefix_numeric_value (arg);
12490 trace_redisplay_p = XINT (arg) > 0;
12491 }
12492
12493 return Qnil;
12494 }
12495
12496
12497 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
12498 doc: /* Like `format', but print result to stderr. */)
12499 (nargs, args)
12500 int nargs;
12501 Lisp_Object *args;
12502 {
12503 Lisp_Object s = Fformat (nargs, args);
12504 fprintf (stderr, "%s", XSTRING (s)->data);
12505 return Qnil;
12506 }
12507
12508 #endif /* GLYPH_DEBUG */
12509
12510
12511 \f
12512 /***********************************************************************
12513 Building Desired Matrix Rows
12514 ***********************************************************************/
12515
12516 /* Return a temporary glyph row holding the glyphs of an overlay
12517 arrow. Only used for non-window-redisplay windows. */
12518
12519 static struct glyph_row *
12520 get_overlay_arrow_glyph_row (w)
12521 struct window *w;
12522 {
12523 struct frame *f = XFRAME (WINDOW_FRAME (w));
12524 struct buffer *buffer = XBUFFER (w->buffer);
12525 struct buffer *old = current_buffer;
12526 unsigned char *arrow_string = XSTRING (Voverlay_arrow_string)->data;
12527 int arrow_len = XSTRING (Voverlay_arrow_string)->size;
12528 unsigned char *arrow_end = arrow_string + arrow_len;
12529 unsigned char *p;
12530 struct it it;
12531 int multibyte_p;
12532 int n_glyphs_before;
12533
12534 set_buffer_temp (buffer);
12535 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
12536 it.glyph_row->used[TEXT_AREA] = 0;
12537 SET_TEXT_POS (it.position, 0, 0);
12538
12539 multibyte_p = !NILP (buffer->enable_multibyte_characters);
12540 p = arrow_string;
12541 while (p < arrow_end)
12542 {
12543 Lisp_Object face, ilisp;
12544
12545 /* Get the next character. */
12546 if (multibyte_p)
12547 it.c = string_char_and_length (p, arrow_len, &it.len);
12548 else
12549 it.c = *p, it.len = 1;
12550 p += it.len;
12551
12552 /* Get its face. */
12553 ilisp = make_number (p - arrow_string);
12554 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
12555 it.face_id = compute_char_face (f, it.c, face);
12556
12557 /* Compute its width, get its glyphs. */
12558 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
12559 SET_TEXT_POS (it.position, -1, -1);
12560 PRODUCE_GLYPHS (&it);
12561
12562 /* If this character doesn't fit any more in the line, we have
12563 to remove some glyphs. */
12564 if (it.current_x > it.last_visible_x)
12565 {
12566 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
12567 break;
12568 }
12569 }
12570
12571 set_buffer_temp (old);
12572 return it.glyph_row;
12573 }
12574
12575
12576 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
12577 glyphs are only inserted for terminal frames since we can't really
12578 win with truncation glyphs when partially visible glyphs are
12579 involved. Which glyphs to insert is determined by
12580 produce_special_glyphs. */
12581
12582 static void
12583 insert_left_trunc_glyphs (it)
12584 struct it *it;
12585 {
12586 struct it truncate_it;
12587 struct glyph *from, *end, *to, *toend;
12588
12589 xassert (!FRAME_WINDOW_P (it->f));
12590
12591 /* Get the truncation glyphs. */
12592 truncate_it = *it;
12593 truncate_it.current_x = 0;
12594 truncate_it.face_id = DEFAULT_FACE_ID;
12595 truncate_it.glyph_row = &scratch_glyph_row;
12596 truncate_it.glyph_row->used[TEXT_AREA] = 0;
12597 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
12598 truncate_it.object = make_number (0);
12599 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
12600
12601 /* Overwrite glyphs from IT with truncation glyphs. */
12602 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
12603 end = from + truncate_it.glyph_row->used[TEXT_AREA];
12604 to = it->glyph_row->glyphs[TEXT_AREA];
12605 toend = to + it->glyph_row->used[TEXT_AREA];
12606
12607 while (from < end)
12608 *to++ = *from++;
12609
12610 /* There may be padding glyphs left over. Overwrite them too. */
12611 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
12612 {
12613 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
12614 while (from < end)
12615 *to++ = *from++;
12616 }
12617
12618 if (to > toend)
12619 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
12620 }
12621
12622
12623 /* Compute the pixel height and width of IT->glyph_row.
12624
12625 Most of the time, ascent and height of a display line will be equal
12626 to the max_ascent and max_height values of the display iterator
12627 structure. This is not the case if
12628
12629 1. We hit ZV without displaying anything. In this case, max_ascent
12630 and max_height will be zero.
12631
12632 2. We have some glyphs that don't contribute to the line height.
12633 (The glyph row flag contributes_to_line_height_p is for future
12634 pixmap extensions).
12635
12636 The first case is easily covered by using default values because in
12637 these cases, the line height does not really matter, except that it
12638 must not be zero. */
12639
12640 static void
12641 compute_line_metrics (it)
12642 struct it *it;
12643 {
12644 struct glyph_row *row = it->glyph_row;
12645 int area, i;
12646
12647 if (FRAME_WINDOW_P (it->f))
12648 {
12649 int i, min_y, max_y;
12650
12651 /* The line may consist of one space only, that was added to
12652 place the cursor on it. If so, the row's height hasn't been
12653 computed yet. */
12654 if (row->height == 0)
12655 {
12656 if (it->max_ascent + it->max_descent == 0)
12657 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
12658 row->ascent = it->max_ascent;
12659 row->height = it->max_ascent + it->max_descent;
12660 row->phys_ascent = it->max_phys_ascent;
12661 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
12662 }
12663
12664 /* Compute the width of this line. */
12665 row->pixel_width = row->x;
12666 for (i = 0; i < row->used[TEXT_AREA]; ++i)
12667 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
12668
12669 xassert (row->pixel_width >= 0);
12670 xassert (row->ascent >= 0 && row->height > 0);
12671
12672 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
12673 || MATRIX_ROW_OVERLAPS_PRED_P (row));
12674
12675 /* If first line's physical ascent is larger than its logical
12676 ascent, use the physical ascent, and make the row taller.
12677 This makes accented characters fully visible. */
12678 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
12679 && row->phys_ascent > row->ascent)
12680 {
12681 row->height += row->phys_ascent - row->ascent;
12682 row->ascent = row->phys_ascent;
12683 }
12684
12685 /* Compute how much of the line is visible. */
12686 row->visible_height = row->height;
12687
12688 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (it->w);
12689 max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
12690
12691 if (row->y < min_y)
12692 row->visible_height -= min_y - row->y;
12693 if (row->y + row->height > max_y)
12694 row->visible_height -= row->y + row->height - max_y;
12695 }
12696 else
12697 {
12698 row->pixel_width = row->used[TEXT_AREA];
12699 if (row->continued_p)
12700 row->pixel_width -= it->continuation_pixel_width;
12701 else if (row->truncated_on_right_p)
12702 row->pixel_width -= it->truncation_pixel_width;
12703 row->ascent = row->phys_ascent = 0;
12704 row->height = row->phys_height = row->visible_height = 1;
12705 }
12706
12707 /* Compute a hash code for this row. */
12708 row->hash = 0;
12709 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12710 for (i = 0; i < row->used[area]; ++i)
12711 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
12712 + row->glyphs[area][i].u.val
12713 + row->glyphs[area][i].face_id
12714 + row->glyphs[area][i].padding_p
12715 + (row->glyphs[area][i].type << 2));
12716
12717 it->max_ascent = it->max_descent = 0;
12718 it->max_phys_ascent = it->max_phys_descent = 0;
12719 }
12720
12721
12722 /* Append one space to the glyph row of iterator IT if doing a
12723 window-based redisplay. DEFAULT_FACE_P non-zero means let the
12724 space have the default face, otherwise let it have the same face as
12725 IT->face_id. Value is non-zero if a space was added.
12726
12727 This function is called to make sure that there is always one glyph
12728 at the end of a glyph row that the cursor can be set on under
12729 window-systems. (If there weren't such a glyph we would not know
12730 how wide and tall a box cursor should be displayed).
12731
12732 At the same time this space let's a nicely handle clearing to the
12733 end of the line if the row ends in italic text. */
12734
12735 static int
12736 append_space (it, default_face_p)
12737 struct it *it;
12738 int default_face_p;
12739 {
12740 if (FRAME_WINDOW_P (it->f))
12741 {
12742 int n = it->glyph_row->used[TEXT_AREA];
12743
12744 if (it->glyph_row->glyphs[TEXT_AREA] + n
12745 < it->glyph_row->glyphs[1 + TEXT_AREA])
12746 {
12747 /* Save some values that must not be changed.
12748 Must save IT->c and IT->len because otherwise
12749 ITERATOR_AT_END_P wouldn't work anymore after
12750 append_space has been called. */
12751 enum display_element_type saved_what = it->what;
12752 int saved_c = it->c, saved_len = it->len;
12753 int saved_x = it->current_x;
12754 int saved_face_id = it->face_id;
12755 struct text_pos saved_pos;
12756 Lisp_Object saved_object;
12757 struct face *face;
12758
12759 saved_object = it->object;
12760 saved_pos = it->position;
12761
12762 it->what = IT_CHARACTER;
12763 bzero (&it->position, sizeof it->position);
12764 it->object = make_number (0);
12765 it->c = ' ';
12766 it->len = 1;
12767
12768 if (default_face_p)
12769 it->face_id = DEFAULT_FACE_ID;
12770 else if (it->face_before_selective_p)
12771 it->face_id = it->saved_face_id;
12772 face = FACE_FROM_ID (it->f, it->face_id);
12773 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
12774
12775 PRODUCE_GLYPHS (it);
12776
12777 it->current_x = saved_x;
12778 it->object = saved_object;
12779 it->position = saved_pos;
12780 it->what = saved_what;
12781 it->face_id = saved_face_id;
12782 it->len = saved_len;
12783 it->c = saved_c;
12784 return 1;
12785 }
12786 }
12787
12788 return 0;
12789 }
12790
12791
12792 /* Extend the face of the last glyph in the text area of IT->glyph_row
12793 to the end of the display line. Called from display_line.
12794 If the glyph row is empty, add a space glyph to it so that we
12795 know the face to draw. Set the glyph row flag fill_line_p. */
12796
12797 static void
12798 extend_face_to_end_of_line (it)
12799 struct it *it;
12800 {
12801 struct face *face;
12802 struct frame *f = it->f;
12803
12804 /* If line is already filled, do nothing. */
12805 if (it->current_x >= it->last_visible_x)
12806 return;
12807
12808 /* Face extension extends the background and box of IT->face_id
12809 to the end of the line. If the background equals the background
12810 of the frame, we don't have to do anything. */
12811 if (it->face_before_selective_p)
12812 face = FACE_FROM_ID (it->f, it->saved_face_id);
12813 else
12814 face = FACE_FROM_ID (f, it->face_id);
12815
12816 if (FRAME_WINDOW_P (f)
12817 && face->box == FACE_NO_BOX
12818 && face->background == FRAME_BACKGROUND_PIXEL (f)
12819 && !face->stipple)
12820 return;
12821
12822 /* Set the glyph row flag indicating that the face of the last glyph
12823 in the text area has to be drawn to the end of the text area. */
12824 it->glyph_row->fill_line_p = 1;
12825
12826 /* If current character of IT is not ASCII, make sure we have the
12827 ASCII face. This will be automatically undone the next time
12828 get_next_display_element returns a multibyte character. Note
12829 that the character will always be single byte in unibyte text. */
12830 if (!ASCII_CHAR_P (it->c))
12831 {
12832 it->face_id = FACE_FOR_CHAR (f, face, 0);
12833 }
12834
12835 if (FRAME_WINDOW_P (f))
12836 {
12837 /* If the row is empty, add a space with the current face of IT,
12838 so that we know which face to draw. */
12839 if (it->glyph_row->used[TEXT_AREA] == 0)
12840 {
12841 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
12842 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
12843 it->glyph_row->used[TEXT_AREA] = 1;
12844 }
12845 }
12846 else
12847 {
12848 /* Save some values that must not be changed. */
12849 int saved_x = it->current_x;
12850 struct text_pos saved_pos;
12851 Lisp_Object saved_object;
12852 enum display_element_type saved_what = it->what;
12853 int saved_face_id = it->face_id;
12854
12855 saved_object = it->object;
12856 saved_pos = it->position;
12857
12858 it->what = IT_CHARACTER;
12859 bzero (&it->position, sizeof it->position);
12860 it->object = make_number (0);
12861 it->c = ' ';
12862 it->len = 1;
12863 it->face_id = face->id;
12864
12865 PRODUCE_GLYPHS (it);
12866
12867 while (it->current_x <= it->last_visible_x)
12868 PRODUCE_GLYPHS (it);
12869
12870 /* Don't count these blanks really. It would let us insert a left
12871 truncation glyph below and make us set the cursor on them, maybe. */
12872 it->current_x = saved_x;
12873 it->object = saved_object;
12874 it->position = saved_pos;
12875 it->what = saved_what;
12876 it->face_id = saved_face_id;
12877 }
12878 }
12879
12880
12881 /* Value is non-zero if text starting at CHARPOS in current_buffer is
12882 trailing whitespace. */
12883
12884 static int
12885 trailing_whitespace_p (charpos)
12886 int charpos;
12887 {
12888 int bytepos = CHAR_TO_BYTE (charpos);
12889 int c = 0;
12890
12891 while (bytepos < ZV_BYTE
12892 && (c = FETCH_CHAR (bytepos),
12893 c == ' ' || c == '\t'))
12894 ++bytepos;
12895
12896 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
12897 {
12898 if (bytepos != PT_BYTE)
12899 return 1;
12900 }
12901 return 0;
12902 }
12903
12904
12905 /* Highlight trailing whitespace, if any, in ROW. */
12906
12907 void
12908 highlight_trailing_whitespace (f, row)
12909 struct frame *f;
12910 struct glyph_row *row;
12911 {
12912 int used = row->used[TEXT_AREA];
12913
12914 if (used)
12915 {
12916 struct glyph *start = row->glyphs[TEXT_AREA];
12917 struct glyph *glyph = start + used - 1;
12918
12919 /* Skip over glyphs inserted to display the cursor at the
12920 end of a line, for extending the face of the last glyph
12921 to the end of the line on terminals, and for truncation
12922 and continuation glyphs. */
12923 while (glyph >= start
12924 && glyph->type == CHAR_GLYPH
12925 && INTEGERP (glyph->object))
12926 --glyph;
12927
12928 /* If last glyph is a space or stretch, and it's trailing
12929 whitespace, set the face of all trailing whitespace glyphs in
12930 IT->glyph_row to `trailing-whitespace'. */
12931 if (glyph >= start
12932 && BUFFERP (glyph->object)
12933 && (glyph->type == STRETCH_GLYPH
12934 || (glyph->type == CHAR_GLYPH
12935 && glyph->u.ch == ' '))
12936 && trailing_whitespace_p (glyph->charpos))
12937 {
12938 int face_id = lookup_named_face (f, Qtrailing_whitespace);
12939
12940 while (glyph >= start
12941 && BUFFERP (glyph->object)
12942 && (glyph->type == STRETCH_GLYPH
12943 || (glyph->type == CHAR_GLYPH
12944 && glyph->u.ch == ' ')))
12945 (glyph--)->face_id = face_id;
12946 }
12947 }
12948 }
12949
12950
12951 /* Value is non-zero if glyph row ROW in window W should be
12952 used to hold the cursor. */
12953
12954 static int
12955 cursor_row_p (w, row)
12956 struct window *w;
12957 struct glyph_row *row;
12958 {
12959 int cursor_row_p = 1;
12960
12961 if (PT == MATRIX_ROW_END_CHARPOS (row))
12962 {
12963 /* If the row ends with a newline from a string, we don't want
12964 the cursor there (if the row is continued it doesn't end in a
12965 newline). */
12966 if (CHARPOS (row->end.string_pos) >= 0
12967 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12968 cursor_row_p = row->continued_p;
12969
12970 /* If the row ends at ZV, display the cursor at the end of that
12971 row instead of at the start of the row below. */
12972 else if (row->ends_at_zv_p)
12973 cursor_row_p = 1;
12974 else
12975 cursor_row_p = 0;
12976 }
12977
12978 return cursor_row_p;
12979 }
12980
12981
12982 /* Construct the glyph row IT->glyph_row in the desired matrix of
12983 IT->w from text at the current position of IT. See dispextern.h
12984 for an overview of struct it. Value is non-zero if
12985 IT->glyph_row displays text, as opposed to a line displaying ZV
12986 only. */
12987
12988 static int
12989 display_line (it)
12990 struct it *it;
12991 {
12992 struct glyph_row *row = it->glyph_row;
12993
12994 /* We always start displaying at hpos zero even if hscrolled. */
12995 xassert (it->hpos == 0 && it->current_x == 0);
12996
12997 /* We must not display in a row that's not a text row. */
12998 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
12999 < it->w->desired_matrix->nrows);
13000
13001 /* Is IT->w showing the region? */
13002 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
13003
13004 /* Clear the result glyph row and enable it. */
13005 prepare_desired_row (row);
13006
13007 row->y = it->current_y;
13008 row->start = it->current;
13009 row->continuation_lines_width = it->continuation_lines_width;
13010 row->displays_text_p = 1;
13011 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
13012 it->starts_in_middle_of_char_p = 0;
13013
13014 /* Arrange the overlays nicely for our purposes. Usually, we call
13015 display_line on only one line at a time, in which case this
13016 can't really hurt too much, or we call it on lines which appear
13017 one after another in the buffer, in which case all calls to
13018 recenter_overlay_lists but the first will be pretty cheap. */
13019 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
13020
13021 /* Move over display elements that are not visible because we are
13022 hscrolled. This may stop at an x-position < IT->first_visible_x
13023 if the first glyph is partially visible or if we hit a line end. */
13024 if (it->current_x < it->first_visible_x)
13025 move_it_in_display_line_to (it, ZV, it->first_visible_x,
13026 MOVE_TO_POS | MOVE_TO_X);
13027
13028 /* Get the initial row height. This is either the height of the
13029 text hscrolled, if there is any, or zero. */
13030 row->ascent = it->max_ascent;
13031 row->height = it->max_ascent + it->max_descent;
13032 row->phys_ascent = it->max_phys_ascent;
13033 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
13034
13035 /* Loop generating characters. The loop is left with IT on the next
13036 character to display. */
13037 while (1)
13038 {
13039 int n_glyphs_before, hpos_before, x_before;
13040 int x, i, nglyphs;
13041 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
13042
13043 /* Retrieve the next thing to display. Value is zero if end of
13044 buffer reached. */
13045 if (!get_next_display_element (it))
13046 {
13047 /* Maybe add a space at the end of this line that is used to
13048 display the cursor there under X. Set the charpos of the
13049 first glyph of blank lines not corresponding to any text
13050 to -1. */
13051 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
13052 || row->used[TEXT_AREA] == 0)
13053 {
13054 row->glyphs[TEXT_AREA]->charpos = -1;
13055 row->displays_text_p = 0;
13056
13057 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
13058 && (!MINI_WINDOW_P (it->w)
13059 || (minibuf_level && EQ (it->window, minibuf_window))))
13060 row->indicate_empty_line_p = 1;
13061 }
13062
13063 it->continuation_lines_width = 0;
13064 row->ends_at_zv_p = 1;
13065 break;
13066 }
13067
13068 /* Now, get the metrics of what we want to display. This also
13069 generates glyphs in `row' (which is IT->glyph_row). */
13070 n_glyphs_before = row->used[TEXT_AREA];
13071 x = it->current_x;
13072
13073 /* Remember the line height so far in case the next element doesn't
13074 fit on the line. */
13075 if (!it->truncate_lines_p)
13076 {
13077 ascent = it->max_ascent;
13078 descent = it->max_descent;
13079 phys_ascent = it->max_phys_ascent;
13080 phys_descent = it->max_phys_descent;
13081 }
13082
13083 PRODUCE_GLYPHS (it);
13084
13085 /* If this display element was in marginal areas, continue with
13086 the next one. */
13087 if (it->area != TEXT_AREA)
13088 {
13089 row->ascent = max (row->ascent, it->max_ascent);
13090 row->height = max (row->height, it->max_ascent + it->max_descent);
13091 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13092 row->phys_height = max (row->phys_height,
13093 it->max_phys_ascent + it->max_phys_descent);
13094 set_iterator_to_next (it, 1);
13095 continue;
13096 }
13097
13098 /* Does the display element fit on the line? If we truncate
13099 lines, we should draw past the right edge of the window. If
13100 we don't truncate, we want to stop so that we can display the
13101 continuation glyph before the right margin. If lines are
13102 continued, there are two possible strategies for characters
13103 resulting in more than 1 glyph (e.g. tabs): Display as many
13104 glyphs as possible in this line and leave the rest for the
13105 continuation line, or display the whole element in the next
13106 line. Original redisplay did the former, so we do it also. */
13107 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
13108 hpos_before = it->hpos;
13109 x_before = x;
13110
13111 if (/* Not a newline. */
13112 nglyphs > 0
13113 /* Glyphs produced fit entirely in the line. */
13114 && it->current_x < it->last_visible_x)
13115 {
13116 it->hpos += nglyphs;
13117 row->ascent = max (row->ascent, it->max_ascent);
13118 row->height = max (row->height, it->max_ascent + it->max_descent);
13119 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13120 row->phys_height = max (row->phys_height,
13121 it->max_phys_ascent + it->max_phys_descent);
13122 if (it->current_x - it->pixel_width < it->first_visible_x)
13123 row->x = x - it->first_visible_x;
13124 }
13125 else
13126 {
13127 int new_x;
13128 struct glyph *glyph;
13129
13130 for (i = 0; i < nglyphs; ++i, x = new_x)
13131 {
13132 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
13133 new_x = x + glyph->pixel_width;
13134
13135 if (/* Lines are continued. */
13136 !it->truncate_lines_p
13137 && (/* Glyph doesn't fit on the line. */
13138 new_x > it->last_visible_x
13139 /* Or it fits exactly on a window system frame. */
13140 || (new_x == it->last_visible_x
13141 && FRAME_WINDOW_P (it->f))))
13142 {
13143 /* End of a continued line. */
13144
13145 if (it->hpos == 0
13146 || (new_x == it->last_visible_x
13147 && FRAME_WINDOW_P (it->f)))
13148 {
13149 /* Current glyph is the only one on the line or
13150 fits exactly on the line. We must continue
13151 the line because we can't draw the cursor
13152 after the glyph. */
13153 row->continued_p = 1;
13154 it->current_x = new_x;
13155 it->continuation_lines_width += new_x;
13156 ++it->hpos;
13157 if (i == nglyphs - 1)
13158 set_iterator_to_next (it, 1);
13159 }
13160 else if (CHAR_GLYPH_PADDING_P (*glyph)
13161 && !FRAME_WINDOW_P (it->f))
13162 {
13163 /* A padding glyph that doesn't fit on this line.
13164 This means the whole character doesn't fit
13165 on the line. */
13166 row->used[TEXT_AREA] = n_glyphs_before;
13167
13168 /* Fill the rest of the row with continuation
13169 glyphs like in 20.x. */
13170 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
13171 < row->glyphs[1 + TEXT_AREA])
13172 produce_special_glyphs (it, IT_CONTINUATION);
13173
13174 row->continued_p = 1;
13175 it->current_x = x_before;
13176 it->continuation_lines_width += x_before;
13177
13178 /* Restore the height to what it was before the
13179 element not fitting on the line. */
13180 it->max_ascent = ascent;
13181 it->max_descent = descent;
13182 it->max_phys_ascent = phys_ascent;
13183 it->max_phys_descent = phys_descent;
13184 }
13185 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
13186 {
13187 /* A TAB that extends past the right edge of the
13188 window. This produces a single glyph on
13189 window system frames. We leave the glyph in
13190 this row and let it fill the row, but don't
13191 consume the TAB. */
13192 it->continuation_lines_width += it->last_visible_x;
13193 row->ends_in_middle_of_char_p = 1;
13194 row->continued_p = 1;
13195 glyph->pixel_width = it->last_visible_x - x;
13196 it->starts_in_middle_of_char_p = 1;
13197 }
13198 else
13199 {
13200 /* Something other than a TAB that draws past
13201 the right edge of the window. Restore
13202 positions to values before the element. */
13203 row->used[TEXT_AREA] = n_glyphs_before + i;
13204
13205 /* Display continuation glyphs. */
13206 if (!FRAME_WINDOW_P (it->f))
13207 produce_special_glyphs (it, IT_CONTINUATION);
13208 row->continued_p = 1;
13209
13210 it->continuation_lines_width += x;
13211
13212 if (nglyphs > 1 && i > 0)
13213 {
13214 row->ends_in_middle_of_char_p = 1;
13215 it->starts_in_middle_of_char_p = 1;
13216 }
13217
13218 /* Restore the height to what it was before the
13219 element not fitting on the line. */
13220 it->max_ascent = ascent;
13221 it->max_descent = descent;
13222 it->max_phys_ascent = phys_ascent;
13223 it->max_phys_descent = phys_descent;
13224 }
13225
13226 break;
13227 }
13228 else if (new_x > it->first_visible_x)
13229 {
13230 /* Increment number of glyphs actually displayed. */
13231 ++it->hpos;
13232
13233 if (x < it->first_visible_x)
13234 /* Glyph is partially visible, i.e. row starts at
13235 negative X position. */
13236 row->x = x - it->first_visible_x;
13237 }
13238 else
13239 {
13240 /* Glyph is completely off the left margin of the
13241 window. This should not happen because of the
13242 move_it_in_display_line at the start of
13243 this function. */
13244 abort ();
13245 }
13246 }
13247
13248 row->ascent = max (row->ascent, it->max_ascent);
13249 row->height = max (row->height, it->max_ascent + it->max_descent);
13250 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13251 row->phys_height = max (row->phys_height,
13252 it->max_phys_ascent + it->max_phys_descent);
13253
13254 /* End of this display line if row is continued. */
13255 if (row->continued_p)
13256 break;
13257 }
13258
13259 /* Is this a line end? If yes, we're also done, after making
13260 sure that a non-default face is extended up to the right
13261 margin of the window. */
13262 if (ITERATOR_AT_END_OF_LINE_P (it))
13263 {
13264 int used_before = row->used[TEXT_AREA];
13265
13266 row->ends_in_newline_from_string_p = STRINGP (it->object);
13267
13268 /* Add a space at the end of the line that is used to
13269 display the cursor there. */
13270 append_space (it, 0);
13271
13272 /* Extend the face to the end of the line. */
13273 extend_face_to_end_of_line (it);
13274
13275 /* Make sure we have the position. */
13276 if (used_before == 0)
13277 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
13278
13279 /* Consume the line end. This skips over invisible lines. */
13280 set_iterator_to_next (it, 1);
13281 it->continuation_lines_width = 0;
13282 break;
13283 }
13284
13285 /* Proceed with next display element. Note that this skips
13286 over lines invisible because of selective display. */
13287 set_iterator_to_next (it, 1);
13288
13289 /* If we truncate lines, we are done when the last displayed
13290 glyphs reach past the right margin of the window. */
13291 if (it->truncate_lines_p
13292 && (FRAME_WINDOW_P (it->f)
13293 ? (it->current_x >= it->last_visible_x)
13294 : (it->current_x > it->last_visible_x)))
13295 {
13296 /* Maybe add truncation glyphs. */
13297 if (!FRAME_WINDOW_P (it->f))
13298 {
13299 int i, n;
13300
13301 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
13302 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
13303 break;
13304
13305 for (n = row->used[TEXT_AREA]; i < n; ++i)
13306 {
13307 row->used[TEXT_AREA] = i;
13308 produce_special_glyphs (it, IT_TRUNCATION);
13309 }
13310 }
13311
13312 row->truncated_on_right_p = 1;
13313 it->continuation_lines_width = 0;
13314 reseat_at_next_visible_line_start (it, 0);
13315 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
13316 it->hpos = hpos_before;
13317 it->current_x = x_before;
13318 break;
13319 }
13320 }
13321
13322 /* If line is not empty and hscrolled, maybe insert truncation glyphs
13323 at the left window margin. */
13324 if (it->first_visible_x
13325 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
13326 {
13327 if (!FRAME_WINDOW_P (it->f))
13328 insert_left_trunc_glyphs (it);
13329 row->truncated_on_left_p = 1;
13330 }
13331
13332 /* If the start of this line is the overlay arrow-position, then
13333 mark this glyph row as the one containing the overlay arrow.
13334 This is clearly a mess with variable size fonts. It would be
13335 better to let it be displayed like cursors under X. */
13336 if (MARKERP (Voverlay_arrow_position)
13337 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
13338 && (MATRIX_ROW_START_CHARPOS (row)
13339 == marker_position (Voverlay_arrow_position))
13340 && STRINGP (Voverlay_arrow_string)
13341 && ! overlay_arrow_seen)
13342 {
13343 /* Overlay arrow in window redisplay is a fringe bitmap. */
13344 if (!FRAME_WINDOW_P (it->f))
13345 {
13346 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
13347 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
13348 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
13349 struct glyph *p = row->glyphs[TEXT_AREA];
13350 struct glyph *p2, *end;
13351
13352 /* Copy the arrow glyphs. */
13353 while (glyph < arrow_end)
13354 *p++ = *glyph++;
13355
13356 /* Throw away padding glyphs. */
13357 p2 = p;
13358 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
13359 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
13360 ++p2;
13361 if (p2 > p)
13362 {
13363 while (p2 < end)
13364 *p++ = *p2++;
13365 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
13366 }
13367 }
13368
13369 overlay_arrow_seen = 1;
13370 row->overlay_arrow_p = 1;
13371 }
13372
13373 /* Compute pixel dimensions of this line. */
13374 compute_line_metrics (it);
13375
13376 /* Remember the position at which this line ends. */
13377 row->end = it->current;
13378
13379 /* Maybe set the cursor. */
13380 if (it->w->cursor.vpos < 0
13381 && PT >= MATRIX_ROW_START_CHARPOS (row)
13382 && PT <= MATRIX_ROW_END_CHARPOS (row)
13383 && cursor_row_p (it->w, row))
13384 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
13385
13386 /* Highlight trailing whitespace. */
13387 if (!NILP (Vshow_trailing_whitespace))
13388 highlight_trailing_whitespace (it->f, it->glyph_row);
13389
13390 /* Prepare for the next line. This line starts horizontally at (X
13391 HPOS) = (0 0). Vertical positions are incremented. As a
13392 convenience for the caller, IT->glyph_row is set to the next
13393 row to be used. */
13394 it->current_x = it->hpos = 0;
13395 it->current_y += row->height;
13396 ++it->vpos;
13397 ++it->glyph_row;
13398 return row->displays_text_p;
13399 }
13400
13401
13402 \f
13403 /***********************************************************************
13404 Menu Bar
13405 ***********************************************************************/
13406
13407 /* Redisplay the menu bar in the frame for window W.
13408
13409 The menu bar of X frames that don't have X toolkit support is
13410 displayed in a special window W->frame->menu_bar_window.
13411
13412 The menu bar of terminal frames is treated specially as far as
13413 glyph matrices are concerned. Menu bar lines are not part of
13414 windows, so the update is done directly on the frame matrix rows
13415 for the menu bar. */
13416
13417 static void
13418 display_menu_bar (w)
13419 struct window *w;
13420 {
13421 struct frame *f = XFRAME (WINDOW_FRAME (w));
13422 struct it it;
13423 Lisp_Object items;
13424 int i;
13425
13426 /* Don't do all this for graphical frames. */
13427 #ifdef HAVE_NTGUI
13428 if (!NILP (Vwindow_system))
13429 return;
13430 #endif
13431 #ifdef USE_X_TOOLKIT
13432 if (FRAME_X_P (f))
13433 return;
13434 #endif
13435 #ifdef macintosh
13436 if (FRAME_MAC_P (f))
13437 return;
13438 #endif
13439
13440 #ifdef USE_X_TOOLKIT
13441 xassert (!FRAME_WINDOW_P (f));
13442 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
13443 it.first_visible_x = 0;
13444 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
13445 #else /* not USE_X_TOOLKIT */
13446 if (FRAME_WINDOW_P (f))
13447 {
13448 /* Menu bar lines are displayed in the desired matrix of the
13449 dummy window menu_bar_window. */
13450 struct window *menu_w;
13451 xassert (WINDOWP (f->menu_bar_window));
13452 menu_w = XWINDOW (f->menu_bar_window);
13453 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
13454 MENU_FACE_ID);
13455 it.first_visible_x = 0;
13456 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
13457 }
13458 else
13459 {
13460 /* This is a TTY frame, i.e. character hpos/vpos are used as
13461 pixel x/y. */
13462 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
13463 MENU_FACE_ID);
13464 it.first_visible_x = 0;
13465 it.last_visible_x = FRAME_WIDTH (f);
13466 }
13467 #endif /* not USE_X_TOOLKIT */
13468
13469 if (! mode_line_inverse_video)
13470 /* Force the menu-bar to be displayed in the default face. */
13471 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
13472
13473 /* Clear all rows of the menu bar. */
13474 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
13475 {
13476 struct glyph_row *row = it.glyph_row + i;
13477 clear_glyph_row (row);
13478 row->enabled_p = 1;
13479 row->full_width_p = 1;
13480 }
13481
13482 /* Display all items of the menu bar. */
13483 items = FRAME_MENU_BAR_ITEMS (it.f);
13484 for (i = 0; i < XVECTOR (items)->size; i += 4)
13485 {
13486 Lisp_Object string;
13487
13488 /* Stop at nil string. */
13489 string = AREF (items, i + 1);
13490 if (NILP (string))
13491 break;
13492
13493 /* Remember where item was displayed. */
13494 AREF (items, i + 3) = make_number (it.hpos);
13495
13496 /* Display the item, pad with one space. */
13497 if (it.current_x < it.last_visible_x)
13498 display_string (NULL, string, Qnil, 0, 0, &it,
13499 XSTRING (string)->size + 1, 0, 0, -1);
13500 }
13501
13502 /* Fill out the line with spaces. */
13503 if (it.current_x < it.last_visible_x)
13504 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
13505
13506 /* Compute the total height of the lines. */
13507 compute_line_metrics (&it);
13508 }
13509
13510
13511 \f
13512 /***********************************************************************
13513 Mode Line
13514 ***********************************************************************/
13515
13516 /* Redisplay mode lines in the window tree whose root is WINDOW. If
13517 FORCE is non-zero, redisplay mode lines unconditionally.
13518 Otherwise, redisplay only mode lines that are garbaged. Value is
13519 the number of windows whose mode lines were redisplayed. */
13520
13521 static int
13522 redisplay_mode_lines (window, force)
13523 Lisp_Object window;
13524 int force;
13525 {
13526 int nwindows = 0;
13527
13528 while (!NILP (window))
13529 {
13530 struct window *w = XWINDOW (window);
13531
13532 if (WINDOWP (w->hchild))
13533 nwindows += redisplay_mode_lines (w->hchild, force);
13534 else if (WINDOWP (w->vchild))
13535 nwindows += redisplay_mode_lines (w->vchild, force);
13536 else if (force
13537 || FRAME_GARBAGED_P (XFRAME (w->frame))
13538 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
13539 {
13540 struct text_pos lpoint;
13541 struct buffer *old = current_buffer;
13542
13543 /* Set the window's buffer for the mode line display. */
13544 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13545 set_buffer_internal_1 (XBUFFER (w->buffer));
13546
13547 /* Point refers normally to the selected window. For any
13548 other window, set up appropriate value. */
13549 if (!EQ (window, selected_window))
13550 {
13551 struct text_pos pt;
13552
13553 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
13554 if (CHARPOS (pt) < BEGV)
13555 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
13556 else if (CHARPOS (pt) > (ZV - 1))
13557 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
13558 else
13559 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
13560 }
13561
13562 /* Display mode lines. */
13563 clear_glyph_matrix (w->desired_matrix);
13564 if (display_mode_lines (w))
13565 {
13566 ++nwindows;
13567 w->must_be_updated_p = 1;
13568 }
13569
13570 /* Restore old settings. */
13571 set_buffer_internal_1 (old);
13572 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13573 }
13574
13575 window = w->next;
13576 }
13577
13578 return nwindows;
13579 }
13580
13581
13582 /* Display the mode and/or top line of window W. Value is the number
13583 of mode lines displayed. */
13584
13585 static int
13586 display_mode_lines (w)
13587 struct window *w;
13588 {
13589 Lisp_Object old_selected_window, old_selected_frame;
13590 int n = 0;
13591
13592 old_selected_frame = selected_frame;
13593 selected_frame = w->frame;
13594 old_selected_window = selected_window;
13595 XSETWINDOW (selected_window, w);
13596
13597 /* These will be set while the mode line specs are processed. */
13598 line_number_displayed = 0;
13599 w->column_number_displayed = Qnil;
13600
13601 if (WINDOW_WANTS_MODELINE_P (w))
13602 {
13603 struct window *sel_w = XWINDOW (old_selected_window);
13604
13605 /* Select mode line face based on the real selected window. */
13606 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
13607 current_buffer->mode_line_format);
13608 ++n;
13609 }
13610
13611 if (WINDOW_WANTS_HEADER_LINE_P (w))
13612 {
13613 display_mode_line (w, HEADER_LINE_FACE_ID,
13614 current_buffer->header_line_format);
13615 ++n;
13616 }
13617
13618 selected_frame = old_selected_frame;
13619 selected_window = old_selected_window;
13620 return n;
13621 }
13622
13623
13624 /* Display mode or top line of window W. FACE_ID specifies which line
13625 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
13626 FORMAT is the mode line format to display. Value is the pixel
13627 height of the mode line displayed. */
13628
13629 static int
13630 display_mode_line (w, face_id, format)
13631 struct window *w;
13632 enum face_id face_id;
13633 Lisp_Object format;
13634 {
13635 struct it it;
13636 struct face *face;
13637
13638 init_iterator (&it, w, -1, -1, NULL, face_id);
13639 prepare_desired_row (it.glyph_row);
13640
13641 if (! mode_line_inverse_video)
13642 /* Force the mode-line to be displayed in the default face. */
13643 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
13644
13645 /* Temporarily make frame's keyboard the current kboard so that
13646 kboard-local variables in the mode_line_format will get the right
13647 values. */
13648 push_frame_kboard (it.f);
13649 display_mode_element (&it, 0, 0, 0, format, Qnil);
13650 pop_frame_kboard ();
13651
13652 /* Fill up with spaces. */
13653 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
13654
13655 compute_line_metrics (&it);
13656 it.glyph_row->full_width_p = 1;
13657 it.glyph_row->mode_line_p = 1;
13658 it.glyph_row->continued_p = 0;
13659 it.glyph_row->truncated_on_left_p = 0;
13660 it.glyph_row->truncated_on_right_p = 0;
13661
13662 /* Make a 3D mode-line have a shadow at its right end. */
13663 face = FACE_FROM_ID (it.f, face_id);
13664 extend_face_to_end_of_line (&it);
13665 if (face->box != FACE_NO_BOX)
13666 {
13667 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
13668 + it.glyph_row->used[TEXT_AREA] - 1);
13669 last->right_box_line_p = 1;
13670 }
13671
13672 return it.glyph_row->height;
13673 }
13674
13675 /* Alist that caches the results of :propertize.
13676 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
13677 Lisp_Object mode_line_proptrans_alist;
13678
13679 /* Contribute ELT to the mode line for window IT->w. How it
13680 translates into text depends on its data type.
13681
13682 IT describes the display environment in which we display, as usual.
13683
13684 DEPTH is the depth in recursion. It is used to prevent
13685 infinite recursion here.
13686
13687 FIELD_WIDTH is the number of characters the display of ELT should
13688 occupy in the mode line, and PRECISION is the maximum number of
13689 characters to display from ELT's representation. See
13690 display_string for details.
13691
13692 Returns the hpos of the end of the text generated by ELT. */
13693
13694 static int
13695 display_mode_element (it, depth, field_width, precision, elt, props)
13696 struct it *it;
13697 int depth;
13698 int field_width, precision;
13699 Lisp_Object elt, props;
13700 {
13701 int n = 0, field, prec;
13702 int literal = 0;
13703
13704 tail_recurse:
13705 if (depth > 10)
13706 goto invalid;
13707
13708 depth++;
13709
13710 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
13711 {
13712 case Lisp_String:
13713 {
13714 /* A string: output it and check for %-constructs within it. */
13715 unsigned char c;
13716 unsigned char *this = XSTRING (elt)->data;
13717 unsigned char *lisp_string = this;
13718
13719 if (!NILP (props))
13720 {
13721 Lisp_Object oprops, aelt;
13722 oprops = Ftext_properties_at (make_number (0), elt);
13723 if (NILP (Fequal (props, oprops)))
13724 {
13725 aelt = Fassoc (elt, mode_line_proptrans_alist);
13726 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
13727 elt = XCAR (aelt);
13728 else
13729 {
13730 elt = Fcopy_sequence (elt);
13731 Fset_text_properties (make_number (0), Flength (elt),
13732 props, elt);
13733 mode_line_proptrans_alist
13734 = Fcons (Fcons (elt, props),
13735 mode_line_proptrans_alist);
13736 }
13737 }
13738 this = XSTRING (elt)->data;
13739 lisp_string = this;
13740 }
13741
13742 if (literal)
13743 {
13744 prec = precision - n;
13745 if (frame_title_ptr)
13746 n += store_frame_title (XSTRING (elt)->data, -1, prec);
13747 else
13748 n += display_string (NULL, elt, Qnil, 0, 0, it,
13749 0, prec, 0, STRING_MULTIBYTE (elt));
13750
13751 break;
13752 }
13753
13754 while ((precision <= 0 || n < precision)
13755 && *this
13756 && (frame_title_ptr
13757 || it->current_x < it->last_visible_x))
13758 {
13759 unsigned char *last = this;
13760
13761 /* Advance to end of string or next format specifier. */
13762 while ((c = *this++) != '\0' && c != '%')
13763 ;
13764
13765 if (this - 1 != last)
13766 {
13767 /* Output to end of string or up to '%'. Field width
13768 is length of string. Don't output more than
13769 PRECISION allows us. */
13770 --this;
13771
13772 prec = chars_in_text (last, this - last);
13773 if (precision > 0 && prec > precision - n)
13774 prec = precision - n;
13775
13776 if (frame_title_ptr)
13777 n += store_frame_title (last, 0, prec);
13778 else
13779 {
13780 int bytepos = last - lisp_string;
13781 int charpos = string_byte_to_char (elt, bytepos);
13782 n += display_string (NULL, elt, Qnil, 0, charpos,
13783 it, 0, prec, 0,
13784 STRING_MULTIBYTE (elt));
13785 }
13786 }
13787 else /* c == '%' */
13788 {
13789 unsigned char *percent_position = this;
13790
13791 /* Get the specified minimum width. Zero means
13792 don't pad. */
13793 field = 0;
13794 while ((c = *this++) >= '0' && c <= '9')
13795 field = field * 10 + c - '0';
13796
13797 /* Don't pad beyond the total padding allowed. */
13798 if (field_width - n > 0 && field > field_width - n)
13799 field = field_width - n;
13800
13801 /* Note that either PRECISION <= 0 or N < PRECISION. */
13802 prec = precision - n;
13803
13804 if (c == 'M')
13805 n += display_mode_element (it, depth, field, prec,
13806 Vglobal_mode_string, props);
13807 else if (c != 0)
13808 {
13809 int multibyte;
13810 unsigned char *spec
13811 = decode_mode_spec (it->w, c, field, prec, &multibyte);
13812
13813 if (frame_title_ptr)
13814 n += store_frame_title (spec, field, prec);
13815 else
13816 {
13817 int nglyphs_before, bytepos, charpos, nwritten;
13818
13819 nglyphs_before = it->glyph_row->used[TEXT_AREA];
13820 bytepos = percent_position - lisp_string;
13821 charpos = (STRING_MULTIBYTE (elt)
13822 ? string_byte_to_char (elt, bytepos)
13823 : bytepos);
13824 nwritten = display_string (spec, Qnil, elt,
13825 charpos, 0, it,
13826 field, prec, 0,
13827 multibyte);
13828
13829 /* Assign to the glyphs written above the
13830 string where the `%x' came from, position
13831 of the `%'. */
13832 if (nwritten > 0)
13833 {
13834 struct glyph *glyph
13835 = (it->glyph_row->glyphs[TEXT_AREA]
13836 + nglyphs_before);
13837 int i;
13838
13839 for (i = 0; i < nwritten; ++i)
13840 {
13841 glyph[i].object = elt;
13842 glyph[i].charpos = charpos;
13843 }
13844
13845 n += nwritten;
13846 }
13847 }
13848 }
13849 else /* c == 0 */
13850 break;
13851 }
13852 }
13853 }
13854 break;
13855
13856 case Lisp_Symbol:
13857 /* A symbol: process the value of the symbol recursively
13858 as if it appeared here directly. Avoid error if symbol void.
13859 Special case: if value of symbol is a string, output the string
13860 literally. */
13861 {
13862 register Lisp_Object tem;
13863 tem = Fboundp (elt);
13864 if (!NILP (tem))
13865 {
13866 tem = Fsymbol_value (elt);
13867 /* If value is a string, output that string literally:
13868 don't check for % within it. */
13869 if (STRINGP (tem))
13870 literal = 1;
13871
13872 if (!EQ (tem, elt))
13873 {
13874 /* Give up right away for nil or t. */
13875 elt = tem;
13876 goto tail_recurse;
13877 }
13878 }
13879 }
13880 break;
13881
13882 case Lisp_Cons:
13883 {
13884 register Lisp_Object car, tem;
13885
13886 /* A cons cell: five distinct cases.
13887 If first element is :eval or :propertize, do something special.
13888 If first element is a string or a cons, process all the elements
13889 and effectively concatenate them.
13890 If first element is a negative number, truncate displaying cdr to
13891 at most that many characters. If positive, pad (with spaces)
13892 to at least that many characters.
13893 If first element is a symbol, process the cadr or caddr recursively
13894 according to whether the symbol's value is non-nil or nil. */
13895 car = XCAR (elt);
13896 if (EQ (car, QCeval))
13897 {
13898 /* An element of the form (:eval FORM) means evaluate FORM
13899 and use the result as mode line elements. */
13900
13901 if (CONSP (XCDR (elt)))
13902 {
13903 Lisp_Object spec;
13904 spec = safe_eval (XCAR (XCDR (elt)));
13905 n += display_mode_element (it, depth, field_width - n,
13906 precision - n, spec, props);
13907 }
13908 }
13909 else if (EQ (car, QCpropertize))
13910 {
13911 if (CONSP (XCDR (elt)))
13912 {
13913 /* An element of the form (:propertize ELT PROPS...)
13914 means display ELT but applying properties PROPS. */
13915 n += display_mode_element (it, depth, field_width - n,
13916 precision - n, XCAR (XCDR (elt)),
13917 XCDR (XCDR (elt)));
13918 }
13919 }
13920 else if (SYMBOLP (car))
13921 {
13922 tem = Fboundp (car);
13923 elt = XCDR (elt);
13924 if (!CONSP (elt))
13925 goto invalid;
13926 /* elt is now the cdr, and we know it is a cons cell.
13927 Use its car if CAR has a non-nil value. */
13928 if (!NILP (tem))
13929 {
13930 tem = Fsymbol_value (car);
13931 if (!NILP (tem))
13932 {
13933 elt = XCAR (elt);
13934 goto tail_recurse;
13935 }
13936 }
13937 /* Symbol's value is nil (or symbol is unbound)
13938 Get the cddr of the original list
13939 and if possible find the caddr and use that. */
13940 elt = XCDR (elt);
13941 if (NILP (elt))
13942 break;
13943 else if (!CONSP (elt))
13944 goto invalid;
13945 elt = XCAR (elt);
13946 goto tail_recurse;
13947 }
13948 else if (INTEGERP (car))
13949 {
13950 register int lim = XINT (car);
13951 elt = XCDR (elt);
13952 if (lim < 0)
13953 {
13954 /* Negative int means reduce maximum width. */
13955 if (precision <= 0)
13956 precision = -lim;
13957 else
13958 precision = min (precision, -lim);
13959 }
13960 else if (lim > 0)
13961 {
13962 /* Padding specified. Don't let it be more than
13963 current maximum. */
13964 if (precision > 0)
13965 lim = min (precision, lim);
13966
13967 /* If that's more padding than already wanted, queue it.
13968 But don't reduce padding already specified even if
13969 that is beyond the current truncation point. */
13970 field_width = max (lim, field_width);
13971 }
13972 goto tail_recurse;
13973 }
13974 else if (STRINGP (car) || CONSP (car))
13975 {
13976 register int limit = 50;
13977 /* Limit is to protect against circular lists. */
13978 while (CONSP (elt)
13979 && --limit > 0
13980 && (precision <= 0 || n < precision))
13981 {
13982 n += display_mode_element (it, depth, field_width - n,
13983 precision - n, XCAR (elt), props);
13984 elt = XCDR (elt);
13985 }
13986 }
13987 }
13988 break;
13989
13990 default:
13991 invalid:
13992 if (frame_title_ptr)
13993 n += store_frame_title ("*invalid*", 0, precision - n);
13994 else
13995 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
13996 precision - n, 0, 0);
13997 return n;
13998 }
13999
14000 /* Pad to FIELD_WIDTH. */
14001 if (field_width > 0 && n < field_width)
14002 {
14003 if (frame_title_ptr)
14004 n += store_frame_title ("", field_width - n, 0);
14005 else
14006 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
14007 0, 0, 0);
14008 }
14009
14010 return n;
14011 }
14012
14013
14014 /* Write a null-terminated, right justified decimal representation of
14015 the positive integer D to BUF using a minimal field width WIDTH. */
14016
14017 static void
14018 pint2str (buf, width, d)
14019 register char *buf;
14020 register int width;
14021 register int d;
14022 {
14023 register char *p = buf;
14024
14025 if (d <= 0)
14026 *p++ = '0';
14027 else
14028 {
14029 while (d > 0)
14030 {
14031 *p++ = d % 10 + '0';
14032 d /= 10;
14033 }
14034 }
14035
14036 for (width -= (int) (p - buf); width > 0; --width)
14037 *p++ = ' ';
14038 *p-- = '\0';
14039 while (p > buf)
14040 {
14041 d = *buf;
14042 *buf++ = *p;
14043 *p-- = d;
14044 }
14045 }
14046
14047 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
14048 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
14049 type of CODING_SYSTEM. Return updated pointer into BUF. */
14050
14051 static unsigned char invalid_eol_type[] = "(*invalid*)";
14052
14053 static char *
14054 decode_mode_spec_coding (coding_system, buf, eol_flag)
14055 Lisp_Object coding_system;
14056 register char *buf;
14057 int eol_flag;
14058 {
14059 Lisp_Object val;
14060 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
14061 unsigned char *eol_str;
14062 int eol_str_len;
14063 /* The EOL conversion we are using. */
14064 Lisp_Object eoltype;
14065
14066 val = CODING_SYSTEM_SPEC (coding_system);
14067 eoltype = Qnil;
14068
14069 if (!VECTORP (val)) /* Not yet decided. */
14070 {
14071 if (multibyte)
14072 *buf++ = '-';
14073 if (eol_flag)
14074 eoltype = eol_mnemonic_undecided;
14075 /* Don't mention EOL conversion if it isn't decided. */
14076 }
14077 else
14078 {
14079 Lisp_Object attrs;
14080 Lisp_Object eolvalue;
14081
14082 attrs = AREF (val, 0);
14083 eolvalue = AREF (val, 2);
14084
14085 if (multibyte)
14086 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
14087
14088 if (eol_flag)
14089 {
14090 /* The EOL conversion that is normal on this system. */
14091
14092 if (NILP (eolvalue)) /* Not yet decided. */
14093 eoltype = eol_mnemonic_undecided;
14094 else if (VECTORP (eolvalue)) /* Not yet decided. */
14095 eoltype = eol_mnemonic_undecided;
14096 else /* eolvalue is Qunix, Qdos, or Qmac. */
14097 eoltype = (EQ (eolvalue, Qunix)
14098 ? eol_mnemonic_unix
14099 : (EQ (eolvalue, Qdos) == 1
14100 ? eol_mnemonic_dos : eol_mnemonic_mac));
14101 }
14102 }
14103
14104 if (eol_flag)
14105 {
14106 /* Mention the EOL conversion if it is not the usual one. */
14107 if (STRINGP (eoltype))
14108 {
14109 eol_str = XSTRING (eoltype)->data;
14110 eol_str_len = XSTRING (eoltype)->size;
14111 }
14112 else if (CHARACTERP (eoltype))
14113 {
14114 eol_str = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
14115 eol_str_len = CHAR_STRING (XINT (eoltype), eol_str);
14116 }
14117 else
14118 {
14119 eol_str = invalid_eol_type;
14120 eol_str_len = sizeof (invalid_eol_type) - 1;
14121 }
14122 bcopy (eol_str, buf, eol_str_len);
14123 buf += eol_str_len;
14124 }
14125
14126 return buf;
14127 }
14128
14129 /* Return a string for the output of a mode line %-spec for window W,
14130 generated by character C. PRECISION >= 0 means don't return a
14131 string longer than that value. FIELD_WIDTH > 0 means pad the
14132 string returned with spaces to that value. Return 1 in *MULTIBYTE
14133 if the result is multibyte text. */
14134
14135 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
14136
14137 static char *
14138 decode_mode_spec (w, c, field_width, precision, multibyte)
14139 struct window *w;
14140 register int c;
14141 int field_width, precision;
14142 int *multibyte;
14143 {
14144 Lisp_Object obj;
14145 struct frame *f = XFRAME (WINDOW_FRAME (w));
14146 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
14147 struct buffer *b = XBUFFER (w->buffer);
14148
14149 obj = Qnil;
14150 *multibyte = 0;
14151
14152 switch (c)
14153 {
14154 case '*':
14155 if (!NILP (b->read_only))
14156 return "%";
14157 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
14158 return "*";
14159 return "-";
14160
14161 case '+':
14162 /* This differs from %* only for a modified read-only buffer. */
14163 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
14164 return "*";
14165 if (!NILP (b->read_only))
14166 return "%";
14167 return "-";
14168
14169 case '&':
14170 /* This differs from %* in ignoring read-only-ness. */
14171 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
14172 return "*";
14173 return "-";
14174
14175 case '%':
14176 return "%";
14177
14178 case '[':
14179 {
14180 int i;
14181 char *p;
14182
14183 if (command_loop_level > 5)
14184 return "[[[... ";
14185 p = decode_mode_spec_buf;
14186 for (i = 0; i < command_loop_level; i++)
14187 *p++ = '[';
14188 *p = 0;
14189 return decode_mode_spec_buf;
14190 }
14191
14192 case ']':
14193 {
14194 int i;
14195 char *p;
14196
14197 if (command_loop_level > 5)
14198 return " ...]]]";
14199 p = decode_mode_spec_buf;
14200 for (i = 0; i < command_loop_level; i++)
14201 *p++ = ']';
14202 *p = 0;
14203 return decode_mode_spec_buf;
14204 }
14205
14206 case '-':
14207 {
14208 register int i;
14209
14210 /* Let lots_of_dashes be a string of infinite length. */
14211 if (field_width <= 0
14212 || field_width > sizeof (lots_of_dashes))
14213 {
14214 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
14215 decode_mode_spec_buf[i] = '-';
14216 decode_mode_spec_buf[i] = '\0';
14217 return decode_mode_spec_buf;
14218 }
14219 else
14220 return lots_of_dashes;
14221 }
14222
14223 case 'b':
14224 obj = b->name;
14225 break;
14226
14227 case 'c':
14228 {
14229 int col = current_column ();
14230 w->column_number_displayed = make_number (col);
14231 pint2str (decode_mode_spec_buf, field_width, col);
14232 return decode_mode_spec_buf;
14233 }
14234
14235 case 'F':
14236 /* %F displays the frame name. */
14237 if (!NILP (f->title))
14238 return (char *) XSTRING (f->title)->data;
14239 if (f->explicit_name || ! FRAME_WINDOW_P (f))
14240 return (char *) XSTRING (f->name)->data;
14241 return "Emacs";
14242
14243 case 'f':
14244 obj = b->filename;
14245 break;
14246
14247 case 'l':
14248 {
14249 int startpos = XMARKER (w->start)->charpos;
14250 int startpos_byte = marker_byte_position (w->start);
14251 int line, linepos, linepos_byte, topline;
14252 int nlines, junk;
14253 int height = XFASTINT (w->height);
14254
14255 /* If we decided that this buffer isn't suitable for line numbers,
14256 don't forget that too fast. */
14257 if (EQ (w->base_line_pos, w->buffer))
14258 goto no_value;
14259 /* But do forget it, if the window shows a different buffer now. */
14260 else if (BUFFERP (w->base_line_pos))
14261 w->base_line_pos = Qnil;
14262
14263 /* If the buffer is very big, don't waste time. */
14264 if (INTEGERP (Vline_number_display_limit)
14265 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
14266 {
14267 w->base_line_pos = Qnil;
14268 w->base_line_number = Qnil;
14269 goto no_value;
14270 }
14271
14272 if (!NILP (w->base_line_number)
14273 && !NILP (w->base_line_pos)
14274 && XFASTINT (w->base_line_pos) <= startpos)
14275 {
14276 line = XFASTINT (w->base_line_number);
14277 linepos = XFASTINT (w->base_line_pos);
14278 linepos_byte = buf_charpos_to_bytepos (b, linepos);
14279 }
14280 else
14281 {
14282 line = 1;
14283 linepos = BUF_BEGV (b);
14284 linepos_byte = BUF_BEGV_BYTE (b);
14285 }
14286
14287 /* Count lines from base line to window start position. */
14288 nlines = display_count_lines (linepos, linepos_byte,
14289 startpos_byte,
14290 startpos, &junk);
14291
14292 topline = nlines + line;
14293
14294 /* Determine a new base line, if the old one is too close
14295 or too far away, or if we did not have one.
14296 "Too close" means it's plausible a scroll-down would
14297 go back past it. */
14298 if (startpos == BUF_BEGV (b))
14299 {
14300 w->base_line_number = make_number (topline);
14301 w->base_line_pos = make_number (BUF_BEGV (b));
14302 }
14303 else if (nlines < height + 25 || nlines > height * 3 + 50
14304 || linepos == BUF_BEGV (b))
14305 {
14306 int limit = BUF_BEGV (b);
14307 int limit_byte = BUF_BEGV_BYTE (b);
14308 int position;
14309 int distance = (height * 2 + 30) * line_number_display_limit_width;
14310
14311 if (startpos - distance > limit)
14312 {
14313 limit = startpos - distance;
14314 limit_byte = CHAR_TO_BYTE (limit);
14315 }
14316
14317 nlines = display_count_lines (startpos, startpos_byte,
14318 limit_byte,
14319 - (height * 2 + 30),
14320 &position);
14321 /* If we couldn't find the lines we wanted within
14322 line_number_display_limit_width chars per line,
14323 give up on line numbers for this window. */
14324 if (position == limit_byte && limit == startpos - distance)
14325 {
14326 w->base_line_pos = w->buffer;
14327 w->base_line_number = Qnil;
14328 goto no_value;
14329 }
14330
14331 w->base_line_number = make_number (topline - nlines);
14332 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
14333 }
14334
14335 /* Now count lines from the start pos to point. */
14336 nlines = display_count_lines (startpos, startpos_byte,
14337 PT_BYTE, PT, &junk);
14338
14339 /* Record that we did display the line number. */
14340 line_number_displayed = 1;
14341
14342 /* Make the string to show. */
14343 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
14344 return decode_mode_spec_buf;
14345 no_value:
14346 {
14347 char* p = decode_mode_spec_buf;
14348 int pad = field_width - 2;
14349 while (pad-- > 0)
14350 *p++ = ' ';
14351 *p++ = '?';
14352 *p++ = '?';
14353 *p = '\0';
14354 return decode_mode_spec_buf;
14355 }
14356 }
14357 break;
14358
14359 case 'm':
14360 obj = b->mode_name;
14361 break;
14362
14363 case 'n':
14364 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
14365 return " Narrow";
14366 break;
14367
14368 case 'p':
14369 {
14370 int pos = marker_position (w->start);
14371 int total = BUF_ZV (b) - BUF_BEGV (b);
14372
14373 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
14374 {
14375 if (pos <= BUF_BEGV (b))
14376 return "All";
14377 else
14378 return "Bottom";
14379 }
14380 else if (pos <= BUF_BEGV (b))
14381 return "Top";
14382 else
14383 {
14384 if (total > 1000000)
14385 /* Do it differently for a large value, to avoid overflow. */
14386 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
14387 else
14388 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
14389 /* We can't normally display a 3-digit number,
14390 so get us a 2-digit number that is close. */
14391 if (total == 100)
14392 total = 99;
14393 sprintf (decode_mode_spec_buf, "%2d%%", total);
14394 return decode_mode_spec_buf;
14395 }
14396 }
14397
14398 /* Display percentage of size above the bottom of the screen. */
14399 case 'P':
14400 {
14401 int toppos = marker_position (w->start);
14402 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
14403 int total = BUF_ZV (b) - BUF_BEGV (b);
14404
14405 if (botpos >= BUF_ZV (b))
14406 {
14407 if (toppos <= BUF_BEGV (b))
14408 return "All";
14409 else
14410 return "Bottom";
14411 }
14412 else
14413 {
14414 if (total > 1000000)
14415 /* Do it differently for a large value, to avoid overflow. */
14416 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
14417 else
14418 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
14419 /* We can't normally display a 3-digit number,
14420 so get us a 2-digit number that is close. */
14421 if (total == 100)
14422 total = 99;
14423 if (toppos <= BUF_BEGV (b))
14424 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
14425 else
14426 sprintf (decode_mode_spec_buf, "%2d%%", total);
14427 return decode_mode_spec_buf;
14428 }
14429 }
14430
14431 case 's':
14432 /* status of process */
14433 obj = Fget_buffer_process (w->buffer);
14434 if (NILP (obj))
14435 return "no process";
14436 #ifdef subprocesses
14437 obj = Fsymbol_name (Fprocess_status (obj));
14438 #endif
14439 break;
14440
14441 case 't': /* indicate TEXT or BINARY */
14442 #ifdef MODE_LINE_BINARY_TEXT
14443 return MODE_LINE_BINARY_TEXT (b);
14444 #else
14445 return "T";
14446 #endif
14447
14448 case 'z':
14449 /* coding-system (not including end-of-line format) */
14450 case 'Z':
14451 /* coding-system (including end-of-line type) */
14452 {
14453 int eol_flag = (c == 'Z');
14454 char *p = decode_mode_spec_buf;
14455
14456 if (! FRAME_WINDOW_P (f))
14457 {
14458 /* No need to mention EOL here--the terminal never needs
14459 to do EOL conversion. */
14460 p = decode_mode_spec_coding (CODING_ID_NAME (keyboard_coding.id),
14461 p, 0);
14462 p = decode_mode_spec_coding (CODING_ID_NAME (terminal_coding.id),
14463 p, 0);
14464 }
14465 p = decode_mode_spec_coding (b->buffer_file_coding_system,
14466 p, eol_flag);
14467
14468 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
14469 #ifdef subprocesses
14470 obj = Fget_buffer_process (Fcurrent_buffer ());
14471 if (PROCESSP (obj))
14472 {
14473 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
14474 p, eol_flag);
14475 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
14476 p, eol_flag);
14477 }
14478 #endif /* subprocesses */
14479 #endif /* 0 */
14480 *p = 0;
14481 return decode_mode_spec_buf;
14482 }
14483 }
14484
14485 if (STRINGP (obj))
14486 {
14487 *multibyte = STRING_MULTIBYTE (obj);
14488 return (char *) XSTRING (obj)->data;
14489 }
14490 else
14491 return "";
14492 }
14493
14494
14495 /* Count up to COUNT lines starting from START / START_BYTE.
14496 But don't go beyond LIMIT_BYTE.
14497 Return the number of lines thus found (always nonnegative).
14498
14499 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
14500
14501 static int
14502 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
14503 int start, start_byte, limit_byte, count;
14504 int *byte_pos_ptr;
14505 {
14506 register unsigned char *cursor;
14507 unsigned char *base;
14508
14509 register int ceiling;
14510 register unsigned char *ceiling_addr;
14511 int orig_count = count;
14512
14513 /* If we are not in selective display mode,
14514 check only for newlines. */
14515 int selective_display = (!NILP (current_buffer->selective_display)
14516 && !INTEGERP (current_buffer->selective_display));
14517
14518 if (count > 0)
14519 {
14520 while (start_byte < limit_byte)
14521 {
14522 ceiling = BUFFER_CEILING_OF (start_byte);
14523 ceiling = min (limit_byte - 1, ceiling);
14524 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
14525 base = (cursor = BYTE_POS_ADDR (start_byte));
14526 while (1)
14527 {
14528 if (selective_display)
14529 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
14530 ;
14531 else
14532 while (*cursor != '\n' && ++cursor != ceiling_addr)
14533 ;
14534
14535 if (cursor != ceiling_addr)
14536 {
14537 if (--count == 0)
14538 {
14539 start_byte += cursor - base + 1;
14540 *byte_pos_ptr = start_byte;
14541 return orig_count;
14542 }
14543 else
14544 if (++cursor == ceiling_addr)
14545 break;
14546 }
14547 else
14548 break;
14549 }
14550 start_byte += cursor - base;
14551 }
14552 }
14553 else
14554 {
14555 while (start_byte > limit_byte)
14556 {
14557 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
14558 ceiling = max (limit_byte, ceiling);
14559 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
14560 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
14561 while (1)
14562 {
14563 if (selective_display)
14564 while (--cursor != ceiling_addr
14565 && *cursor != '\n' && *cursor != 015)
14566 ;
14567 else
14568 while (--cursor != ceiling_addr && *cursor != '\n')
14569 ;
14570
14571 if (cursor != ceiling_addr)
14572 {
14573 if (++count == 0)
14574 {
14575 start_byte += cursor - base + 1;
14576 *byte_pos_ptr = start_byte;
14577 /* When scanning backwards, we should
14578 not count the newline posterior to which we stop. */
14579 return - orig_count - 1;
14580 }
14581 }
14582 else
14583 break;
14584 }
14585 /* Here we add 1 to compensate for the last decrement
14586 of CURSOR, which took it past the valid range. */
14587 start_byte += cursor - base + 1;
14588 }
14589 }
14590
14591 *byte_pos_ptr = limit_byte;
14592
14593 if (count < 0)
14594 return - orig_count + count;
14595 return orig_count - count;
14596
14597 }
14598
14599
14600 \f
14601 /***********************************************************************
14602 Displaying strings
14603 ***********************************************************************/
14604
14605 /* Display a NUL-terminated string, starting with index START.
14606
14607 If STRING is non-null, display that C string. Otherwise, the Lisp
14608 string LISP_STRING is displayed.
14609
14610 If FACE_STRING is not nil, FACE_STRING_POS is a position in
14611 FACE_STRING. Display STRING or LISP_STRING with the face at
14612 FACE_STRING_POS in FACE_STRING:
14613
14614 Display the string in the environment given by IT, but use the
14615 standard display table, temporarily.
14616
14617 FIELD_WIDTH is the minimum number of output glyphs to produce.
14618 If STRING has fewer characters than FIELD_WIDTH, pad to the right
14619 with spaces. If STRING has more characters, more than FIELD_WIDTH
14620 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
14621
14622 PRECISION is the maximum number of characters to output from
14623 STRING. PRECISION < 0 means don't truncate the string.
14624
14625 This is roughly equivalent to printf format specifiers:
14626
14627 FIELD_WIDTH PRECISION PRINTF
14628 ----------------------------------------
14629 -1 -1 %s
14630 -1 10 %.10s
14631 10 -1 %10s
14632 20 10 %20.10s
14633
14634 MULTIBYTE zero means do not display multibyte chars, > 0 means do
14635 display them, and < 0 means obey the current buffer's value of
14636 enable_multibyte_characters.
14637
14638 Value is the number of glyphs produced. */
14639
14640 static int
14641 display_string (string, lisp_string, face_string, face_string_pos,
14642 start, it, field_width, precision, max_x, multibyte)
14643 unsigned char *string;
14644 Lisp_Object lisp_string;
14645 Lisp_Object face_string;
14646 int face_string_pos;
14647 int start;
14648 struct it *it;
14649 int field_width, precision, max_x;
14650 int multibyte;
14651 {
14652 int hpos_at_start = it->hpos;
14653 int saved_face_id = it->face_id;
14654 struct glyph_row *row = it->glyph_row;
14655
14656 /* Initialize the iterator IT for iteration over STRING beginning
14657 with index START. */
14658 reseat_to_string (it, string, lisp_string, start,
14659 precision, field_width, multibyte);
14660
14661 /* If displaying STRING, set up the face of the iterator
14662 from LISP_STRING, if that's given. */
14663 if (STRINGP (face_string))
14664 {
14665 int endptr;
14666 struct face *face;
14667
14668 it->face_id
14669 = face_at_string_position (it->w, face_string, face_string_pos,
14670 0, it->region_beg_charpos,
14671 it->region_end_charpos,
14672 &endptr, it->base_face_id, 0);
14673 face = FACE_FROM_ID (it->f, it->face_id);
14674 it->face_box_p = face->box != FACE_NO_BOX;
14675 }
14676
14677 /* Set max_x to the maximum allowed X position. Don't let it go
14678 beyond the right edge of the window. */
14679 if (max_x <= 0)
14680 max_x = it->last_visible_x;
14681 else
14682 max_x = min (max_x, it->last_visible_x);
14683
14684 /* Skip over display elements that are not visible. because IT->w is
14685 hscrolled. */
14686 if (it->current_x < it->first_visible_x)
14687 move_it_in_display_line_to (it, 100000, it->first_visible_x,
14688 MOVE_TO_POS | MOVE_TO_X);
14689
14690 row->ascent = it->max_ascent;
14691 row->height = it->max_ascent + it->max_descent;
14692 row->phys_ascent = it->max_phys_ascent;
14693 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14694
14695 /* This condition is for the case that we are called with current_x
14696 past last_visible_x. */
14697 while (it->current_x < max_x)
14698 {
14699 int x_before, x, n_glyphs_before, i, nglyphs;
14700
14701 /* Get the next display element. */
14702 if (!get_next_display_element (it))
14703 break;
14704
14705 /* Produce glyphs. */
14706 x_before = it->current_x;
14707 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
14708 PRODUCE_GLYPHS (it);
14709
14710 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
14711 i = 0;
14712 x = x_before;
14713 while (i < nglyphs)
14714 {
14715 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
14716
14717 if (!it->truncate_lines_p
14718 && x + glyph->pixel_width > max_x)
14719 {
14720 /* End of continued line or max_x reached. */
14721 if (CHAR_GLYPH_PADDING_P (*glyph))
14722 {
14723 /* A wide character is unbreakable. */
14724 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
14725 it->current_x = x_before;
14726 }
14727 else
14728 {
14729 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
14730 it->current_x = x;
14731 }
14732 break;
14733 }
14734 else if (x + glyph->pixel_width >= it->first_visible_x)
14735 {
14736 /* Glyph is at least partially visible. */
14737 ++it->hpos;
14738 if (x < it->first_visible_x)
14739 it->glyph_row->x = x - it->first_visible_x;
14740 }
14741 else
14742 {
14743 /* Glyph is off the left margin of the display area.
14744 Should not happen. */
14745 abort ();
14746 }
14747
14748 row->ascent = max (row->ascent, it->max_ascent);
14749 row->height = max (row->height, it->max_ascent + it->max_descent);
14750 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14751 row->phys_height = max (row->phys_height,
14752 it->max_phys_ascent + it->max_phys_descent);
14753 x += glyph->pixel_width;
14754 ++i;
14755 }
14756
14757 /* Stop if max_x reached. */
14758 if (i < nglyphs)
14759 break;
14760
14761 /* Stop at line ends. */
14762 if (ITERATOR_AT_END_OF_LINE_P (it))
14763 {
14764 it->continuation_lines_width = 0;
14765 break;
14766 }
14767
14768 set_iterator_to_next (it, 1);
14769
14770 /* Stop if truncating at the right edge. */
14771 if (it->truncate_lines_p
14772 && it->current_x >= it->last_visible_x)
14773 {
14774 /* Add truncation mark, but don't do it if the line is
14775 truncated at a padding space. */
14776 if (IT_CHARPOS (*it) < it->string_nchars)
14777 {
14778 if (!FRAME_WINDOW_P (it->f))
14779 {
14780 int i, n;
14781
14782 if (it->current_x > it->last_visible_x)
14783 {
14784 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
14785 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
14786 break;
14787 for (n = row->used[TEXT_AREA]; i < n; ++i)
14788 {
14789 row->used[TEXT_AREA] = i;
14790 produce_special_glyphs (it, IT_TRUNCATION);
14791 }
14792 }
14793 produce_special_glyphs (it, IT_TRUNCATION);
14794 }
14795 it->glyph_row->truncated_on_right_p = 1;
14796 }
14797 break;
14798 }
14799 }
14800
14801 /* Maybe insert a truncation at the left. */
14802 if (it->first_visible_x
14803 && IT_CHARPOS (*it) > 0)
14804 {
14805 if (!FRAME_WINDOW_P (it->f))
14806 insert_left_trunc_glyphs (it);
14807 it->glyph_row->truncated_on_left_p = 1;
14808 }
14809
14810 it->face_id = saved_face_id;
14811
14812 /* Value is number of columns displayed. */
14813 return it->hpos - hpos_at_start;
14814 }
14815
14816
14817 \f
14818 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
14819 appears as an element of LIST or as the car of an element of LIST.
14820 If PROPVAL is a list, compare each element against LIST in that
14821 way, and return 1/2 if any element of PROPVAL is found in LIST.
14822 Otherwise return 0. This function cannot quit.
14823 The return value is 2 if the text is invisible but with an ellipsis
14824 and 1 if it's invisible and without an ellipsis. */
14825
14826 int
14827 invisible_p (propval, list)
14828 register Lisp_Object propval;
14829 Lisp_Object list;
14830 {
14831 register Lisp_Object tail, proptail;
14832
14833 for (tail = list; CONSP (tail); tail = XCDR (tail))
14834 {
14835 register Lisp_Object tem;
14836 tem = XCAR (tail);
14837 if (EQ (propval, tem))
14838 return 1;
14839 if (CONSP (tem) && EQ (propval, XCAR (tem)))
14840 return NILP (XCDR (tem)) ? 1 : 2;
14841 }
14842
14843 if (CONSP (propval))
14844 {
14845 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
14846 {
14847 Lisp_Object propelt;
14848 propelt = XCAR (proptail);
14849 for (tail = list; CONSP (tail); tail = XCDR (tail))
14850 {
14851 register Lisp_Object tem;
14852 tem = XCAR (tail);
14853 if (EQ (propelt, tem))
14854 return 1;
14855 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
14856 return NILP (XCDR (tem)) ? 1 : 2;
14857 }
14858 }
14859 }
14860
14861 return 0;
14862 }
14863
14864 \f
14865 /***********************************************************************
14866 Initialization
14867 ***********************************************************************/
14868
14869 void
14870 syms_of_xdisp ()
14871 {
14872 Vwith_echo_area_save_vector = Qnil;
14873 staticpro (&Vwith_echo_area_save_vector);
14874
14875 Vmessage_stack = Qnil;
14876 staticpro (&Vmessage_stack);
14877
14878 Qinhibit_redisplay = intern ("inhibit-redisplay");
14879 staticpro (&Qinhibit_redisplay);
14880
14881 message_dolog_marker1 = Fmake_marker ();
14882 staticpro (&message_dolog_marker1);
14883 message_dolog_marker2 = Fmake_marker ();
14884 staticpro (&message_dolog_marker2);
14885 message_dolog_marker3 = Fmake_marker ();
14886 staticpro (&message_dolog_marker3);
14887
14888 #if GLYPH_DEBUG
14889 defsubr (&Sdump_glyph_matrix);
14890 defsubr (&Sdump_glyph_row);
14891 defsubr (&Sdump_tool_bar_row);
14892 defsubr (&Strace_redisplay);
14893 defsubr (&Strace_to_stderr);
14894 #endif
14895 #ifdef HAVE_WINDOW_SYSTEM
14896 defsubr (&Stool_bar_lines_needed);
14897 #endif
14898
14899 staticpro (&Qmenu_bar_update_hook);
14900 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
14901
14902 staticpro (&Qoverriding_terminal_local_map);
14903 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
14904
14905 staticpro (&Qoverriding_local_map);
14906 Qoverriding_local_map = intern ("overriding-local-map");
14907
14908 staticpro (&Qwindow_scroll_functions);
14909 Qwindow_scroll_functions = intern ("window-scroll-functions");
14910
14911 staticpro (&Qredisplay_end_trigger_functions);
14912 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
14913
14914 staticpro (&Qinhibit_point_motion_hooks);
14915 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
14916
14917 QCdata = intern (":data");
14918 staticpro (&QCdata);
14919 Qdisplay = intern ("display");
14920 staticpro (&Qdisplay);
14921 Qspace_width = intern ("space-width");
14922 staticpro (&Qspace_width);
14923 Qraise = intern ("raise");
14924 staticpro (&Qraise);
14925 Qspace = intern ("space");
14926 staticpro (&Qspace);
14927 Qmargin = intern ("margin");
14928 staticpro (&Qmargin);
14929 Qleft_margin = intern ("left-margin");
14930 staticpro (&Qleft_margin);
14931 Qright_margin = intern ("right-margin");
14932 staticpro (&Qright_margin);
14933 Qalign_to = intern ("align-to");
14934 staticpro (&Qalign_to);
14935 QCalign_to = intern (":align-to");
14936 staticpro (&QCalign_to);
14937 Qrelative_width = intern ("relative-width");
14938 staticpro (&Qrelative_width);
14939 QCrelative_width = intern (":relative-width");
14940 staticpro (&QCrelative_width);
14941 QCrelative_height = intern (":relative-height");
14942 staticpro (&QCrelative_height);
14943 QCeval = intern (":eval");
14944 staticpro (&QCeval);
14945 QCpropertize = intern (":propertize");
14946 staticpro (&QCpropertize);
14947 Qwhen = intern ("when");
14948 staticpro (&Qwhen);
14949 QCfile = intern (":file");
14950 staticpro (&QCfile);
14951 Qfontified = intern ("fontified");
14952 staticpro (&Qfontified);
14953 Qfontification_functions = intern ("fontification-functions");
14954 staticpro (&Qfontification_functions);
14955 Qtrailing_whitespace = intern ("trailing-whitespace");
14956 staticpro (&Qtrailing_whitespace);
14957 Qimage = intern ("image");
14958 staticpro (&Qimage);
14959 Qmessage_truncate_lines = intern ("message-truncate-lines");
14960 staticpro (&Qmessage_truncate_lines);
14961 Qcursor_in_non_selected_windows = intern ("cursor-in-non-selected-windows");
14962 staticpro (&Qcursor_in_non_selected_windows);
14963 Qgrow_only = intern ("grow-only");
14964 staticpro (&Qgrow_only);
14965 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
14966 staticpro (&Qinhibit_menubar_update);
14967 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
14968 staticpro (&Qinhibit_eval_during_redisplay);
14969 Qposition = intern ("position");
14970 staticpro (&Qposition);
14971 Qbuffer_position = intern ("buffer-position");
14972 staticpro (&Qbuffer_position);
14973 Qobject = intern ("object");
14974 staticpro (&Qobject);
14975
14976 last_arrow_position = Qnil;
14977 last_arrow_string = Qnil;
14978 staticpro (&last_arrow_position);
14979 staticpro (&last_arrow_string);
14980
14981 echo_buffer[0] = echo_buffer[1] = Qnil;
14982 staticpro (&echo_buffer[0]);
14983 staticpro (&echo_buffer[1]);
14984
14985 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
14986 staticpro (&echo_area_buffer[0]);
14987 staticpro (&echo_area_buffer[1]);
14988
14989 Vmessages_buffer_name = build_string ("*Messages*");
14990 staticpro (&Vmessages_buffer_name);
14991
14992 mode_line_proptrans_alist = Qnil;
14993 staticpro (&mode_line_proptrans_alist);
14994
14995 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
14996 doc: /* Non-nil means highlight trailing whitespace.
14997 The face used for trailing whitespace is `trailing-whitespace'. */);
14998 Vshow_trailing_whitespace = Qnil;
14999
15000 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
15001 doc: /* Non-nil means don't actually do any redisplay.
15002 This is used for internal purposes. */);
15003 Vinhibit_redisplay = Qnil;
15004
15005 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
15006 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
15007 Vglobal_mode_string = Qnil;
15008
15009 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
15010 doc: /* Marker for where to display an arrow on top of the buffer text.
15011 This must be the beginning of a line in order to work.
15012 See also `overlay-arrow-string'. */);
15013 Voverlay_arrow_position = Qnil;
15014
15015 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
15016 doc: /* String to display as an arrow. See also `overlay-arrow-position'. */);
15017 Voverlay_arrow_string = Qnil;
15018
15019 DEFVAR_INT ("scroll-step", &scroll_step,
15020 doc: /* *The number of lines to try scrolling a window by when point moves out.
15021 If that fails to bring point back on frame, point is centered instead.
15022 If this is zero, point is always centered after it moves off frame.
15023 If you want scrolling to always be a line at a time, you should set
15024 `scroll-conservatively' to a large value rather than set this to 1. */);
15025
15026 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
15027 doc: /* *Scroll up to this many lines, to bring point back on screen.
15028 A value of zero means to scroll the text to center point vertically
15029 in the window. */);
15030 scroll_conservatively = 0;
15031
15032 DEFVAR_INT ("scroll-margin", &scroll_margin,
15033 doc: /* *Number of lines of margin at the top and bottom of a window.
15034 Recenter the window whenever point gets within this many lines
15035 of the top or bottom of the window. */);
15036 scroll_margin = 0;
15037
15038 #if GLYPH_DEBUG
15039 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
15040 #endif
15041
15042 DEFVAR_BOOL ("truncate-partial-width-windows",
15043 &truncate_partial_width_windows,
15044 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
15045 truncate_partial_width_windows = 1;
15046
15047 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
15048 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
15049 Any other value means to use the appropriate face, `mode-line',
15050 `header-line', or `menu' respectively.
15051
15052 This variable is deprecated; please change the above faces instead. */);
15053 mode_line_inverse_video = 1;
15054
15055 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
15056 doc: /* *Maximum buffer size for which line number should be displayed.
15057 If the buffer is bigger than this, the line number does not appear
15058 in the mode line. A value of nil means no limit. */);
15059 Vline_number_display_limit = Qnil;
15060
15061 DEFVAR_INT ("line-number-display-limit-width",
15062 &line_number_display_limit_width,
15063 doc: /* *Maximum line width (in characters) for line number display.
15064 If the average length of the lines near point is bigger than this, then the
15065 line number may be omitted from the mode line. */);
15066 line_number_display_limit_width = 200;
15067
15068 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
15069 doc: /* *Non-nil means highlight region even in nonselected windows. */);
15070 highlight_nonselected_windows = 0;
15071
15072 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
15073 doc: /* Non-nil if more than one frame is visible on this display.
15074 Minibuffer-only frames don't count, but iconified frames do.
15075 This variable is not guaranteed to be accurate except while processing
15076 `frame-title-format' and `icon-title-format'. */);
15077
15078 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
15079 doc: /* Template for displaying the title bar of visible frames.
15080 \(Assuming the window manager supports this feature.)
15081 This variable has the same structure as `mode-line-format' (which see),
15082 and is used only on frames for which no explicit name has been set
15083 \(see `modify-frame-parameters'). */);
15084 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
15085 doc: /* Template for displaying the title bar of an iconified frame.
15086 \(Assuming the window manager supports this feature.)
15087 This variable has the same structure as `mode-line-format' (which see),
15088 and is used only on frames for which no explicit name has been set
15089 \(see `modify-frame-parameters'). */);
15090 Vicon_title_format
15091 = Vframe_title_format
15092 = Fcons (intern ("multiple-frames"),
15093 Fcons (build_string ("%b"),
15094 Fcons (Fcons (empty_string,
15095 Fcons (intern ("invocation-name"),
15096 Fcons (build_string ("@"),
15097 Fcons (intern ("system-name"),
15098 Qnil)))),
15099 Qnil)));
15100
15101 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
15102 doc: /* Maximum number of lines to keep in the message log buffer.
15103 If nil, disable message logging. If t, log messages but don't truncate
15104 the buffer when it becomes large. */);
15105 Vmessage_log_max = make_number (50);
15106
15107 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
15108 doc: /* Functions called before redisplay, if window sizes have changed.
15109 The value should be a list of functions that take one argument.
15110 Just before redisplay, for each frame, if any of its windows have changed
15111 size since the last redisplay, or have been split or deleted,
15112 all the functions in the list are called, with the frame as argument. */);
15113 Vwindow_size_change_functions = Qnil;
15114
15115 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
15116 doc: /* List of Functions to call before redisplaying a window with scrolling.
15117 Each function is called with two arguments, the window
15118 and its new display-start position. Note that the value of `window-end'
15119 is not valid when these functions are called. */);
15120 Vwindow_scroll_functions = Qnil;
15121
15122 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
15123 doc: /* *Non-nil means automatically resize tool-bars.
15124 This increases a tool-bar's height if not all tool-bar items are visible.
15125 It decreases a tool-bar's height when it would display blank lines
15126 otherwise. */);
15127 auto_resize_tool_bars_p = 1;
15128
15129 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
15130 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
15131 auto_raise_tool_bar_buttons_p = 1;
15132
15133 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
15134 doc: /* *Margin around tool-bar buttons in pixels.
15135 If an integer, use that for both horizontal and vertical margins.
15136 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
15137 HORZ specifying the horizontal margin, and VERT specifying the
15138 vertical margin. */);
15139 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
15140
15141 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
15142 doc: /* *Relief thickness of tool-bar buttons. */);
15143 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
15144
15145 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
15146 doc: /* List of functions to call to fontify regions of text.
15147 Each function is called with one argument POS. Functions must
15148 fontify a region starting at POS in the current buffer, and give
15149 fontified regions the property `fontified'. */);
15150 Vfontification_functions = Qnil;
15151 Fmake_variable_buffer_local (Qfontification_functions);
15152
15153 DEFVAR_BOOL ("unibyte-display-via-language-environment",
15154 &unibyte_display_via_language_environment,
15155 doc: /* *Non-nil means display unibyte text according to language environment.
15156 Specifically this means that unibyte non-ASCII characters
15157 are displayed by converting them to the equivalent multibyte characters
15158 according to the current language environment. As a result, they are
15159 displayed according to the current fontset. */);
15160 unibyte_display_via_language_environment = 0;
15161
15162 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
15163 doc: /* *Maximum height for resizing mini-windows.
15164 If a float, it specifies a fraction of the mini-window frame's height.
15165 If an integer, it specifies a number of lines. */);
15166 Vmax_mini_window_height = make_float (0.25);
15167
15168 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
15169 doc: /* *How to resize mini-windows.
15170 A value of nil means don't automatically resize mini-windows.
15171 A value of t means resize them to fit the text displayed in them.
15172 A value of `grow-only', the default, means let mini-windows grow
15173 only, until their display becomes empty, at which point the windows
15174 go back to their normal size. */);
15175 Vresize_mini_windows = Qgrow_only;
15176
15177 DEFVAR_BOOL ("cursor-in-non-selected-windows",
15178 &cursor_in_non_selected_windows,
15179 doc: /* *Non-nil means display a hollow cursor in non-selected windows.
15180 nil means don't display a cursor there. */);
15181 cursor_in_non_selected_windows = 1;
15182
15183 DEFVAR_BOOL ("automatic-hscrolling", &automatic_hscrolling_p,
15184 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
15185 automatic_hscrolling_p = 1;
15186
15187 DEFVAR_INT ("automatic-hscroll-margin", &automatic_hscroll_margin,
15188 doc: /* *How many columns away from the window edge point is allowed to get
15189 before automatic hscrolling will horizontally scroll the window. */);
15190 automatic_hscroll_margin = 5;
15191
15192 DEFVAR_LISP ("automatic-hscroll-step", &Vautomatic_hscroll_step,
15193 doc: /* *How many columns to scroll the window when point gets too close to the edge.
15194 When point is less than `automatic-hscroll-margin' columns from the window
15195 edge, automatic hscrolling will scroll the window by the amount of columns
15196 determined by this variable. If its value is a positive integer, scroll that
15197 many columns. If it's a positive floating-point number, it specifies the
15198 fraction of the window's width to scroll. If it's nil or zero, point will be
15199 centered horizontally after the scroll. Any other value, including negative
15200 numbers, are treated as if the value were zero.
15201
15202 Automatic hscrolling always moves point outside the scroll margin, so if
15203 point was more than scroll step columns inside the margin, the window will
15204 scroll more than the value given by the scroll step.
15205
15206 Note that the lower bound for automatic hscrolling specified by `scroll-left'
15207 and `scroll-right' overrides this variable's effect. */);
15208 Vautomatic_hscroll_step = make_number (0);
15209
15210 DEFVAR_LISP ("image-types", &Vimage_types,
15211 doc: /* List of supported image types.
15212 Each element of the list is a symbol for a supported image type. */);
15213 Vimage_types = Qnil;
15214
15215 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
15216 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
15217 Bind this around calls to `message' to let it take effect. */);
15218 message_truncate_lines = 0;
15219
15220 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
15221 doc: /* Normal hook run for clicks on menu bar, before displaying a submenu.
15222 Can be used to update submenus whose contents should vary. */);
15223 Vmenu_bar_update_hook = Qnil;
15224
15225 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
15226 doc: /* Non-nil means don't update menu bars. Internal use only. */);
15227 inhibit_menubar_update = 0;
15228
15229 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
15230 doc: /* Non-nil means don't eval Lisp during redisplay. */);
15231 inhibit_eval_during_redisplay = 0;
15232
15233 #if GLYPH_DEBUG
15234 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
15235 doc: /* Inhibit try_window_id display optimization. */);
15236 inhibit_try_window_id = 0;
15237
15238 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
15239 doc: /* Inhibit try_window_reusing display optimization. */);
15240 inhibit_try_window_reusing = 0;
15241
15242 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
15243 doc: /* Inhibit try_cursor_movement display optimization. */);
15244 inhibit_try_cursor_movement = 0;
15245 #endif /* GLYPH_DEBUG */
15246 }
15247
15248
15249 /* Initialize this module when Emacs starts. */
15250
15251 void
15252 init_xdisp ()
15253 {
15254 Lisp_Object root_window;
15255 struct window *mini_w;
15256
15257 current_header_line_height = current_mode_line_height = -1;
15258
15259 CHARPOS (this_line_start_pos) = 0;
15260
15261 mini_w = XWINDOW (minibuf_window);
15262 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
15263
15264 if (!noninteractive)
15265 {
15266 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
15267 int i;
15268
15269 XWINDOW (root_window)->top = make_number (FRAME_TOP_MARGIN (f));
15270 set_window_height (root_window,
15271 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
15272 0);
15273 mini_w->top = make_number (FRAME_HEIGHT (f) - 1);
15274 set_window_height (minibuf_window, 1, 0);
15275
15276 XWINDOW (root_window)->width = make_number (FRAME_WIDTH (f));
15277 mini_w->width = make_number (FRAME_WIDTH (f));
15278
15279 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
15280 scratch_glyph_row.glyphs[TEXT_AREA + 1]
15281 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
15282
15283 /* The default ellipsis glyphs `...'. */
15284 for (i = 0; i < 3; ++i)
15285 default_invis_vector[i] = make_number ('.');
15286 }
15287
15288 #ifdef HAVE_WINDOW_SYSTEM
15289 {
15290 /* Allocate the buffer for frame titles. */
15291 int size = 100;
15292 frame_title_buf = (char *) xmalloc (size);
15293 frame_title_buf_end = frame_title_buf + size;
15294 frame_title_ptr = NULL;
15295 }
15296 #endif /* HAVE_WINDOW_SYSTEM */
15297
15298 help_echo_showing_p = 0;
15299 }
15300
15301