]> code.delx.au - gnu-emacs/blob - src/xdisp.c
(init_iterator): Compare with Vminibuf_selected_window
[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 "charset.h"
180 #include "indent.h"
181 #include "commands.h"
182 #include "macros.h"
183 #include "disptab.h"
184 #include "termhooks.h"
185 #include "intervals.h"
186 #include "coding.h"
187 #include "process.h"
188 #include "region-cache.h"
189 #include "fontset.h"
190
191 #ifdef HAVE_X_WINDOWS
192 #include "xterm.h"
193 #endif
194 #ifdef WINDOWSNT
195 #include "w32term.h"
196 #endif
197 #ifdef macintosh
198 #include "macterm.h"
199 #endif
200
201 #define INFINITY 10000000
202
203 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
204 extern void set_frame_menubar P_ ((struct frame *f, int, int));
205 extern int pending_menu_activation;
206 #endif
207
208 extern int interrupt_input;
209 extern int command_loop_level;
210
211 extern int minibuffer_auto_raise;
212
213 extern Lisp_Object Qface;
214
215 extern Lisp_Object Voverriding_local_map;
216 extern Lisp_Object Voverriding_local_map_menu_flag;
217 extern Lisp_Object Qmenu_item;
218
219 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
220 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
221 Lisp_Object Qredisplay_end_trigger_functions;
222 Lisp_Object Qinhibit_point_motion_hooks;
223 Lisp_Object QCeval, Qwhen, QCfile, QCdata, QCpropertize;
224 Lisp_Object Qfontified;
225 Lisp_Object Qgrow_only;
226 Lisp_Object Qinhibit_eval_during_redisplay;
227 Lisp_Object Qbuffer_position, Qposition, Qobject;
228
229 /* Functions called to fontify regions of text. */
230
231 Lisp_Object Vfontification_functions;
232 Lisp_Object Qfontification_functions;
233
234 /* Non-zero means draw tool bar buttons raised when the mouse moves
235 over them. */
236
237 int auto_raise_tool_bar_buttons_p;
238
239 /* Margin around tool bar buttons in pixels. */
240
241 Lisp_Object Vtool_bar_button_margin;
242
243 /* Thickness of shadow to draw around tool bar buttons. */
244
245 int tool_bar_button_relief;
246
247 /* Non-zero means automatically resize tool-bars so that all tool-bar
248 items are visible, and no blank lines remain. */
249
250 int auto_resize_tool_bars_p;
251
252 /* Non-nil means don't actually do any redisplay. */
253
254 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
255
256 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
257
258 int inhibit_eval_during_redisplay;
259
260 /* Names of text properties relevant for redisplay. */
261
262 Lisp_Object Qdisplay, Qrelative_width, Qalign_to;
263 extern Lisp_Object Qface, Qinvisible, Qwidth;
264
265 /* Symbols used in text property values. */
266
267 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
268 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
269 Lisp_Object Qmargin;
270 extern Lisp_Object Qheight;
271
272 /* Non-nil means highlight trailing whitespace. */
273
274 Lisp_Object Vshow_trailing_whitespace;
275
276 /* Name of the face used to highlight trailing whitespace. */
277
278 Lisp_Object Qtrailing_whitespace;
279
280 /* The symbol `image' which is the car of the lists used to represent
281 images in Lisp. */
282
283 Lisp_Object Qimage;
284
285 /* Non-zero means print newline to stdout before next mini-buffer
286 message. */
287
288 int noninteractive_need_newline;
289
290 /* Non-zero means print newline to message log before next message. */
291
292 static int message_log_need_newline;
293
294 /* Three markers that message_dolog uses.
295 It could allocate them itself, but that causes trouble
296 in handling memory-full errors. */
297 static Lisp_Object message_dolog_marker1;
298 static Lisp_Object message_dolog_marker2;
299 static Lisp_Object message_dolog_marker3;
300 \f
301 /* The buffer position of the first character appearing entirely or
302 partially on the line of the selected window which contains the
303 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
304 redisplay optimization in redisplay_internal. */
305
306 static struct text_pos this_line_start_pos;
307
308 /* Number of characters past the end of the line above, including the
309 terminating newline. */
310
311 static struct text_pos this_line_end_pos;
312
313 /* The vertical positions and the height of this line. */
314
315 static int this_line_vpos;
316 static int this_line_y;
317 static int this_line_pixel_height;
318
319 /* X position at which this display line starts. Usually zero;
320 negative if first character is partially visible. */
321
322 static int this_line_start_x;
323
324 /* Buffer that this_line_.* variables are referring to. */
325
326 static struct buffer *this_line_buffer;
327
328 /* Nonzero means truncate lines in all windows less wide than the
329 frame. */
330
331 int truncate_partial_width_windows;
332
333 /* A flag to control how to display unibyte 8-bit character. */
334
335 int unibyte_display_via_language_environment;
336
337 /* Nonzero means we have more than one non-mini-buffer-only frame.
338 Not guaranteed to be accurate except while parsing
339 frame-title-format. */
340
341 int multiple_frames;
342
343 Lisp_Object Vglobal_mode_string;
344
345 /* Marker for where to display an arrow on top of the buffer text. */
346
347 Lisp_Object Voverlay_arrow_position;
348
349 /* String to display for the arrow. Only used on terminal frames. */
350
351 Lisp_Object Voverlay_arrow_string;
352
353 /* Values of those variables at last redisplay. However, if
354 Voverlay_arrow_position is a marker, last_arrow_position is its
355 numerical position. */
356
357 static Lisp_Object last_arrow_position, last_arrow_string;
358
359 /* Like mode-line-format, but for the title bar on a visible frame. */
360
361 Lisp_Object Vframe_title_format;
362
363 /* Like mode-line-format, but for the title bar on an iconified frame. */
364
365 Lisp_Object Vicon_title_format;
366
367 /* List of functions to call when a window's size changes. These
368 functions get one arg, a frame on which one or more windows' sizes
369 have changed. */
370
371 static Lisp_Object Vwindow_size_change_functions;
372
373 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
374
375 /* Nonzero if overlay arrow has been displayed once in this window. */
376
377 static int overlay_arrow_seen;
378
379 /* Nonzero means highlight the region even in nonselected windows. */
380
381 int highlight_nonselected_windows;
382
383 /* If cursor motion alone moves point off frame, try scrolling this
384 many lines up or down if that will bring it back. */
385
386 static int scroll_step;
387
388 /* Nonzero means scroll just far enough to bring point back on the
389 screen, when appropriate. */
390
391 static int scroll_conservatively;
392
393 /* Recenter the window whenever point gets within this many lines of
394 the top or bottom of the window. This value is translated into a
395 pixel value by multiplying it with CANON_Y_UNIT, which means that
396 there is really a fixed pixel height scroll margin. */
397
398 int scroll_margin;
399
400 /* Number of windows showing the buffer of the selected window (or
401 another buffer with the same base buffer). keyboard.c refers to
402 this. */
403
404 int buffer_shared;
405
406 /* Vector containing glyphs for an ellipsis `...'. */
407
408 static Lisp_Object default_invis_vector[3];
409
410 /* Zero means display the mode-line/header-line/menu-bar in the default face
411 (this slightly odd definition is for compatibility with previous versions
412 of emacs), non-zero means display them using their respective faces.
413
414 This variable is deprecated. */
415
416 int mode_line_inverse_video;
417
418 /* Prompt to display in front of the mini-buffer contents. */
419
420 Lisp_Object minibuf_prompt;
421
422 /* Width of current mini-buffer prompt. Only set after display_line
423 of the line that contains the prompt. */
424
425 int minibuf_prompt_width;
426 int minibuf_prompt_pixel_width;
427
428 /* This is the window where the echo area message was displayed. It
429 is always a mini-buffer window, but it may not be the same window
430 currently active as a mini-buffer. */
431
432 Lisp_Object echo_area_window;
433
434 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
435 pushes the current message and the value of
436 message_enable_multibyte on the stack, the function restore_message
437 pops the stack and displays MESSAGE again. */
438
439 Lisp_Object Vmessage_stack;
440
441 /* Nonzero means multibyte characters were enabled when the echo area
442 message was specified. */
443
444 int message_enable_multibyte;
445
446 /* Nonzero if we should redraw the mode lines on the next redisplay. */
447
448 int update_mode_lines;
449
450 /* Nonzero if window sizes or contents have changed since last
451 redisplay that finished. */
452
453 int windows_or_buffers_changed;
454
455 /* Nonzero after display_mode_line if %l was used and it displayed a
456 line number. */
457
458 int line_number_displayed;
459
460 /* Maximum buffer size for which to display line numbers. */
461
462 Lisp_Object Vline_number_display_limit;
463
464 /* Line width to consider when repositioning for line number display. */
465
466 static int line_number_display_limit_width;
467
468 /* Number of lines to keep in the message log buffer. t means
469 infinite. nil means don't log at all. */
470
471 Lisp_Object Vmessage_log_max;
472
473 /* The name of the *Messages* buffer, a string. */
474
475 static Lisp_Object Vmessages_buffer_name;
476
477 /* Current, index 0, and last displayed echo area message. Either
478 buffers from echo_buffers, or nil to indicate no message. */
479
480 Lisp_Object echo_area_buffer[2];
481
482 /* The buffers referenced from echo_area_buffer. */
483
484 static Lisp_Object echo_buffer[2];
485
486 /* A vector saved used in with_area_buffer to reduce consing. */
487
488 static Lisp_Object Vwith_echo_area_save_vector;
489
490 /* Non-zero means display_echo_area should display the last echo area
491 message again. Set by redisplay_preserve_echo_area. */
492
493 static int display_last_displayed_message_p;
494
495 /* Nonzero if echo area is being used by print; zero if being used by
496 message. */
497
498 int message_buf_print;
499
500 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
501
502 Lisp_Object Qinhibit_menubar_update;
503 int inhibit_menubar_update;
504
505 /* Maximum height for resizing mini-windows. Either a float
506 specifying a fraction of the available height, or an integer
507 specifying a number of lines. */
508
509 Lisp_Object Vmax_mini_window_height;
510
511 /* Non-zero means messages should be displayed with truncated
512 lines instead of being continued. */
513
514 int message_truncate_lines;
515 Lisp_Object Qmessage_truncate_lines;
516
517 /* Set to 1 in clear_message to make redisplay_internal aware
518 of an emptied echo area. */
519
520 static int message_cleared_p;
521
522 /* Non-zero means we want a hollow cursor in windows that are not
523 selected. Zero means there's no cursor in such windows. */
524
525 int cursor_in_non_selected_windows;
526 Lisp_Object Qcursor_in_non_selected_windows;
527
528 /* A scratch glyph row with contents used for generating truncation
529 glyphs. Also used in direct_output_for_insert. */
530
531 #define MAX_SCRATCH_GLYPHS 100
532 struct glyph_row scratch_glyph_row;
533 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
534
535 /* Ascent and height of the last line processed by move_it_to. */
536
537 static int last_max_ascent, last_height;
538
539 /* Non-zero if there's a help-echo in the echo area. */
540
541 int help_echo_showing_p;
542
543 /* If >= 0, computed, exact values of mode-line and header-line height
544 to use in the macros CURRENT_MODE_LINE_HEIGHT and
545 CURRENT_HEADER_LINE_HEIGHT. */
546
547 int current_mode_line_height, current_header_line_height;
548
549 /* The maximum distance to look ahead for text properties. Values
550 that are too small let us call compute_char_face and similar
551 functions too often which is expensive. Values that are too large
552 let us call compute_char_face and alike too often because we
553 might not be interested in text properties that far away. */
554
555 #define TEXT_PROP_DISTANCE_LIMIT 100
556
557 #if GLYPH_DEBUG
558
559 /* Variables to turn off display optimizations from Lisp. */
560
561 int inhibit_try_window_id, inhibit_try_window_reusing;
562 int inhibit_try_cursor_movement;
563
564 /* Non-zero means print traces of redisplay if compiled with
565 GLYPH_DEBUG != 0. */
566
567 int trace_redisplay_p;
568
569 #endif /* GLYPH_DEBUG */
570
571 #ifdef DEBUG_TRACE_MOVE
572 /* Non-zero means trace with TRACE_MOVE to stderr. */
573 int trace_move;
574
575 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
576 #else
577 #define TRACE_MOVE(x) (void) 0
578 #endif
579
580 /* Non-zero means automatically scroll windows horizontally to make
581 point visible. */
582
583 int automatic_hscrolling_p;
584
585 /* How close to the margin can point get before the window is scrolled
586 horizontally. */
587 int automatic_hscroll_margin;
588
589 /* How much to scroll horizontally when point is inside the above margin. */
590 Lisp_Object Vautomatic_hscroll_step;
591
592 /* A list of symbols, one for each supported image type. */
593
594 Lisp_Object Vimage_types;
595
596 /* The variable `resize-mini-windows'. If nil, don't resize
597 mini-windows. If t, always resize them to fit the text they
598 display. If `grow-only', let mini-windows grow only until they
599 become empty. */
600
601 Lisp_Object Vresize_mini_windows;
602
603 /* Buffer being redisplayed -- for redisplay_window_error. */
604
605 struct buffer *displayed_buffer;
606
607 /* Value returned from text property handlers (see below). */
608
609 enum prop_handled
610 {
611 HANDLED_NORMALLY,
612 HANDLED_RECOMPUTE_PROPS,
613 HANDLED_OVERLAY_STRING_CONSUMED,
614 HANDLED_RETURN
615 };
616
617 /* A description of text properties that redisplay is interested
618 in. */
619
620 struct props
621 {
622 /* The name of the property. */
623 Lisp_Object *name;
624
625 /* A unique index for the property. */
626 enum prop_idx idx;
627
628 /* A handler function called to set up iterator IT from the property
629 at IT's current position. Value is used to steer handle_stop. */
630 enum prop_handled (*handler) P_ ((struct it *it));
631 };
632
633 static enum prop_handled handle_face_prop P_ ((struct it *));
634 static enum prop_handled handle_invisible_prop P_ ((struct it *));
635 static enum prop_handled handle_display_prop P_ ((struct it *));
636 static enum prop_handled handle_composition_prop P_ ((struct it *));
637 static enum prop_handled handle_overlay_change P_ ((struct it *));
638 static enum prop_handled handle_fontified_prop P_ ((struct it *));
639
640 /* Properties handled by iterators. */
641
642 static struct props it_props[] =
643 {
644 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
645 /* Handle `face' before `display' because some sub-properties of
646 `display' need to know the face. */
647 {&Qface, FACE_PROP_IDX, handle_face_prop},
648 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
649 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
650 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
651 {NULL, 0, NULL}
652 };
653
654 /* Value is the position described by X. If X is a marker, value is
655 the marker_position of X. Otherwise, value is X. */
656
657 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
658
659 /* Enumeration returned by some move_it_.* functions internally. */
660
661 enum move_it_result
662 {
663 /* Not used. Undefined value. */
664 MOVE_UNDEFINED,
665
666 /* Move ended at the requested buffer position or ZV. */
667 MOVE_POS_MATCH_OR_ZV,
668
669 /* Move ended at the requested X pixel position. */
670 MOVE_X_REACHED,
671
672 /* Move within a line ended at the end of a line that must be
673 continued. */
674 MOVE_LINE_CONTINUED,
675
676 /* Move within a line ended at the end of a line that would
677 be displayed truncated. */
678 MOVE_LINE_TRUNCATED,
679
680 /* Move within a line ended at a line end. */
681 MOVE_NEWLINE_OR_CR
682 };
683
684
685 \f
686 /* Function prototypes. */
687
688 static void setup_for_ellipsis P_ ((struct it *));
689 static void mark_window_display_accurate_1 P_ ((struct window *, int));
690 static int single_display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
691 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
692 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
693 static int redisplay_mode_lines P_ ((Lisp_Object, int));
694 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
695
696 #if 0
697 static int invisible_text_between_p P_ ((struct it *, int, int));
698 #endif
699
700 static int next_element_from_ellipsis P_ ((struct it *));
701 static void pint2str P_ ((char *, int, int));
702 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
703 struct text_pos));
704 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
705 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
706 static void store_frame_title_char P_ ((char));
707 static int store_frame_title P_ ((unsigned char *, int, int));
708 static void x_consider_frame_title P_ ((Lisp_Object));
709 static void handle_stop P_ ((struct it *));
710 static int tool_bar_lines_needed P_ ((struct frame *));
711 static int single_display_prop_intangible_p P_ ((Lisp_Object));
712 static void ensure_echo_area_buffers P_ ((void));
713 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
714 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
715 static int with_echo_area_buffer P_ ((struct window *, int,
716 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
717 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
718 static void clear_garbaged_frames P_ ((void));
719 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
720 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
721 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
722 static int display_echo_area P_ ((struct window *));
723 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
724 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
725 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
726 static int string_char_and_length P_ ((unsigned char *, int, int *));
727 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
728 struct text_pos));
729 static int compute_window_start_on_continuation_line P_ ((struct window *));
730 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
731 static void insert_left_trunc_glyphs P_ ((struct it *));
732 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
733 static void extend_face_to_end_of_line P_ ((struct it *));
734 static int append_space P_ ((struct it *, int));
735 static int make_cursor_line_fully_visible P_ ((struct window *));
736 static int try_scrolling P_ ((Lisp_Object, int, int, int, int));
737 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
738 static int trailing_whitespace_p P_ ((int));
739 static int message_log_check_duplicate P_ ((int, int, int, int));
740 static void push_it P_ ((struct it *));
741 static void pop_it P_ ((struct it *));
742 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
743 static void redisplay_internal P_ ((int));
744 static int echo_area_display P_ ((int));
745 static void redisplay_windows P_ ((Lisp_Object));
746 static void redisplay_window P_ ((Lisp_Object, int));
747 static Lisp_Object redisplay_window_error ();
748 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
749 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
750 static void update_menu_bar P_ ((struct frame *, int));
751 static int try_window_reusing_current_matrix P_ ((struct window *));
752 static int try_window_id P_ ((struct window *));
753 static int display_line P_ ((struct it *));
754 static int display_mode_lines P_ ((struct window *));
755 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
756 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object));
757 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
758 static void display_menu_bar P_ ((struct window *));
759 static int display_count_lines P_ ((int, int, int, int, int *));
760 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
761 int, int, struct it *, int, int, int, int));
762 static void compute_line_metrics P_ ((struct it *));
763 static void run_redisplay_end_trigger_hook P_ ((struct it *));
764 static int get_overlay_strings P_ ((struct it *, int));
765 static void next_overlay_string P_ ((struct it *));
766 static void reseat P_ ((struct it *, struct text_pos, int));
767 static void reseat_1 P_ ((struct it *, struct text_pos, int));
768 static void back_to_previous_visible_line_start P_ ((struct it *));
769 static void reseat_at_previous_visible_line_start P_ ((struct it *));
770 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
771 static int next_element_from_display_vector P_ ((struct it *));
772 static int next_element_from_string P_ ((struct it *));
773 static int next_element_from_c_string P_ ((struct it *));
774 static int next_element_from_buffer P_ ((struct it *));
775 static int next_element_from_composition P_ ((struct it *));
776 static int next_element_from_image P_ ((struct it *));
777 static int next_element_from_stretch P_ ((struct it *));
778 static void load_overlay_strings P_ ((struct it *, int));
779 static int init_from_display_pos P_ ((struct it *, struct window *,
780 struct display_pos *));
781 static void reseat_to_string P_ ((struct it *, unsigned char *,
782 Lisp_Object, int, int, int, int));
783 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
784 int, int, int));
785 void move_it_vertically_backward P_ ((struct it *, int));
786 static void init_to_row_start P_ ((struct it *, struct window *,
787 struct glyph_row *));
788 static int init_to_row_end P_ ((struct it *, struct window *,
789 struct glyph_row *));
790 static void back_to_previous_line_start P_ ((struct it *));
791 static int forward_to_next_line_start P_ ((struct it *, int *));
792 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
793 Lisp_Object, int));
794 static struct text_pos string_pos P_ ((int, Lisp_Object));
795 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
796 static int number_of_chars P_ ((unsigned char *, int));
797 static void compute_stop_pos P_ ((struct it *));
798 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
799 Lisp_Object));
800 static int face_before_or_after_it_pos P_ ((struct it *, int));
801 static int next_overlay_change P_ ((int));
802 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
803 Lisp_Object, struct text_pos *,
804 int));
805 static int underlying_face_id P_ ((struct it *));
806 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
807 struct window *));
808
809 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
810 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
811
812 #ifdef HAVE_WINDOW_SYSTEM
813
814 static void update_tool_bar P_ ((struct frame *, int));
815 static void build_desired_tool_bar_string P_ ((struct frame *f));
816 static int redisplay_tool_bar P_ ((struct frame *));
817 static void display_tool_bar_line P_ ((struct it *));
818
819 #endif /* HAVE_WINDOW_SYSTEM */
820
821 \f
822 /***********************************************************************
823 Window display dimensions
824 ***********************************************************************/
825
826 /* Return the window-relative maximum y + 1 for glyph rows displaying
827 text in window W. This is the height of W minus the height of a
828 mode line, if any. */
829
830 INLINE int
831 window_text_bottom_y (w)
832 struct window *w;
833 {
834 struct frame *f = XFRAME (w->frame);
835 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
836
837 if (WINDOW_WANTS_MODELINE_P (w))
838 height -= CURRENT_MODE_LINE_HEIGHT (w);
839 return height;
840 }
841
842
843 /* Return the pixel width of display area AREA of window W. AREA < 0
844 means return the total width of W, not including fringes to
845 the left and right of the window. */
846
847 INLINE int
848 window_box_width (w, area)
849 struct window *w;
850 int area;
851 {
852 struct frame *f = XFRAME (w->frame);
853 int width = XFASTINT (w->width);
854
855 if (!w->pseudo_window_p)
856 {
857 width -= FRAME_SCROLL_BAR_WIDTH (f) + FRAME_FRINGE_COLS (f);
858
859 if (area == TEXT_AREA)
860 {
861 if (INTEGERP (w->left_margin_width))
862 width -= XFASTINT (w->left_margin_width);
863 if (INTEGERP (w->right_margin_width))
864 width -= XFASTINT (w->right_margin_width);
865 }
866 else if (area == LEFT_MARGIN_AREA)
867 width = (INTEGERP (w->left_margin_width)
868 ? XFASTINT (w->left_margin_width) : 0);
869 else if (area == RIGHT_MARGIN_AREA)
870 width = (INTEGERP (w->right_margin_width)
871 ? XFASTINT (w->right_margin_width) : 0);
872 }
873
874 return width * CANON_X_UNIT (f);
875 }
876
877
878 /* Return the pixel height of the display area of window W, not
879 including mode lines of W, if any. */
880
881 INLINE int
882 window_box_height (w)
883 struct window *w;
884 {
885 struct frame *f = XFRAME (w->frame);
886 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
887
888 xassert (height >= 0);
889
890 /* Note: the code below that determines the mode-line/header-line
891 height is essentially the same as that contained in the macro
892 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
893 the appropriate glyph row has its `mode_line_p' flag set,
894 and if it doesn't, uses estimate_mode_line_height instead. */
895
896 if (WINDOW_WANTS_MODELINE_P (w))
897 {
898 struct glyph_row *ml_row
899 = (w->current_matrix && w->current_matrix->rows
900 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
901 : 0);
902 if (ml_row && ml_row->mode_line_p)
903 height -= ml_row->height;
904 else
905 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
906 }
907
908 if (WINDOW_WANTS_HEADER_LINE_P (w))
909 {
910 struct glyph_row *hl_row
911 = (w->current_matrix && w->current_matrix->rows
912 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
913 : 0);
914 if (hl_row && hl_row->mode_line_p)
915 height -= hl_row->height;
916 else
917 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
918 }
919
920 /* With a very small font and a mode-line that's taller than
921 default, we might end up with a negative height. */
922 return max (0, height);
923 }
924
925
926 /* Return the frame-relative coordinate of the left edge of display
927 area AREA of window W. AREA < 0 means return the left edge of the
928 whole window, to the right of the left fringe of W. */
929
930 INLINE int
931 window_box_left (w, area)
932 struct window *w;
933 int area;
934 {
935 struct frame *f = XFRAME (w->frame);
936 int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
937
938 if (!w->pseudo_window_p)
939 {
940 x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
941 + FRAME_LEFT_FRINGE_WIDTH (f));
942
943 if (area == TEXT_AREA)
944 x += window_box_width (w, LEFT_MARGIN_AREA);
945 else if (area == RIGHT_MARGIN_AREA)
946 x += (window_box_width (w, LEFT_MARGIN_AREA)
947 + window_box_width (w, TEXT_AREA));
948 }
949
950 return x;
951 }
952
953
954 /* Return the frame-relative coordinate of the right edge of display
955 area AREA of window W. AREA < 0 means return the left edge of the
956 whole window, to the left of the right fringe of W. */
957
958 INLINE int
959 window_box_right (w, area)
960 struct window *w;
961 int area;
962 {
963 return window_box_left (w, area) + window_box_width (w, area);
964 }
965
966
967 /* Get the bounding box of the display area AREA of window W, without
968 mode lines, in frame-relative coordinates. AREA < 0 means the
969 whole window, not including the left and right fringes of
970 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
971 coordinates of the upper-left corner of the box. Return in
972 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
973
974 INLINE void
975 window_box (w, area, box_x, box_y, box_width, box_height)
976 struct window *w;
977 int area;
978 int *box_x, *box_y, *box_width, *box_height;
979 {
980 struct frame *f = XFRAME (w->frame);
981
982 *box_width = window_box_width (w, area);
983 *box_height = window_box_height (w);
984 *box_x = window_box_left (w, area);
985 *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
986 + XFASTINT (w->top) * CANON_Y_UNIT (f));
987 if (WINDOW_WANTS_HEADER_LINE_P (w))
988 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
989 }
990
991
992 /* Get the bounding box of the display area AREA of window W, without
993 mode lines. AREA < 0 means the whole window, not including the
994 left and right fringe of the window. Return in *TOP_LEFT_X
995 and TOP_LEFT_Y the frame-relative pixel coordinates of the
996 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
997 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
998 box. */
999
1000 INLINE void
1001 window_box_edges (w, area, top_left_x, top_left_y,
1002 bottom_right_x, bottom_right_y)
1003 struct window *w;
1004 int area;
1005 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1006 {
1007 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1008 bottom_right_y);
1009 *bottom_right_x += *top_left_x;
1010 *bottom_right_y += *top_left_y;
1011 }
1012
1013
1014 \f
1015 /***********************************************************************
1016 Utilities
1017 ***********************************************************************/
1018
1019 /* Return the bottom y-position of the line the iterator IT is in.
1020 This can modify IT's settings. */
1021
1022 int
1023 line_bottom_y (it)
1024 struct it *it;
1025 {
1026 int line_height = it->max_ascent + it->max_descent;
1027 int line_top_y = it->current_y;
1028
1029 if (line_height == 0)
1030 {
1031 if (last_height)
1032 line_height = last_height;
1033 else if (IT_CHARPOS (*it) < ZV)
1034 {
1035 move_it_by_lines (it, 1, 1);
1036 line_height = (it->max_ascent || it->max_descent
1037 ? it->max_ascent + it->max_descent
1038 : last_height);
1039 }
1040 else
1041 {
1042 struct glyph_row *row = it->glyph_row;
1043
1044 /* Use the default character height. */
1045 it->glyph_row = NULL;
1046 it->what = IT_CHARACTER;
1047 it->c = ' ';
1048 it->len = 1;
1049 PRODUCE_GLYPHS (it);
1050 line_height = it->ascent + it->descent;
1051 it->glyph_row = row;
1052 }
1053 }
1054
1055 return line_top_y + line_height;
1056 }
1057
1058
1059 /* Return 1 if position CHARPOS is visible in window W. Set *FULLY to
1060 1 if POS is visible and the line containing POS is fully visible.
1061 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1062 and header-lines heights. */
1063
1064 int
1065 pos_visible_p (w, charpos, fully, exact_mode_line_heights_p)
1066 struct window *w;
1067 int charpos, *fully, exact_mode_line_heights_p;
1068 {
1069 struct it it;
1070 struct text_pos top;
1071 int visible_p;
1072 struct buffer *old_buffer = NULL;
1073
1074 if (XBUFFER (w->buffer) != current_buffer)
1075 {
1076 old_buffer = current_buffer;
1077 set_buffer_internal_1 (XBUFFER (w->buffer));
1078 }
1079
1080 *fully = visible_p = 0;
1081 SET_TEXT_POS_FROM_MARKER (top, w->start);
1082
1083 /* Compute exact mode line heights, if requested. */
1084 if (exact_mode_line_heights_p)
1085 {
1086 if (WINDOW_WANTS_MODELINE_P (w))
1087 current_mode_line_height
1088 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1089 current_buffer->mode_line_format);
1090
1091 if (WINDOW_WANTS_HEADER_LINE_P (w))
1092 current_header_line_height
1093 = display_mode_line (w, HEADER_LINE_FACE_ID,
1094 current_buffer->header_line_format);
1095 }
1096
1097 start_display (&it, w, top);
1098 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
1099 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
1100
1101 /* Note that we may overshoot because of invisible text. */
1102 if (IT_CHARPOS (it) >= charpos)
1103 {
1104 int top_y = it.current_y;
1105 int bottom_y = line_bottom_y (&it);
1106 int window_top_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
1107
1108 if (top_y < window_top_y)
1109 visible_p = bottom_y > window_top_y;
1110 else if (top_y < it.last_visible_y)
1111 {
1112 visible_p = 1;
1113 *fully = bottom_y <= it.last_visible_y;
1114 }
1115 }
1116 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1117 {
1118 move_it_by_lines (&it, 1, 0);
1119 if (charpos < IT_CHARPOS (it))
1120 {
1121 visible_p = 1;
1122 *fully = 0;
1123 }
1124 }
1125
1126 if (old_buffer)
1127 set_buffer_internal_1 (old_buffer);
1128
1129 current_header_line_height = current_mode_line_height = -1;
1130 return visible_p;
1131 }
1132
1133
1134 /* Return the next character from STR which is MAXLEN bytes long.
1135 Return in *LEN the length of the character. This is like
1136 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1137 we find one, we return a `?', but with the length of the invalid
1138 character. */
1139
1140 static INLINE int
1141 string_char_and_length (str, maxlen, len)
1142 unsigned char *str;
1143 int maxlen, *len;
1144 {
1145 int c;
1146
1147 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1148 if (!CHAR_VALID_P (c, 1))
1149 /* We may not change the length here because other places in Emacs
1150 don't use this function, i.e. they silently accept invalid
1151 characters. */
1152 c = '?';
1153
1154 return c;
1155 }
1156
1157
1158
1159 /* Given a position POS containing a valid character and byte position
1160 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1161
1162 static struct text_pos
1163 string_pos_nchars_ahead (pos, string, nchars)
1164 struct text_pos pos;
1165 Lisp_Object string;
1166 int nchars;
1167 {
1168 xassert (STRINGP (string) && nchars >= 0);
1169
1170 if (STRING_MULTIBYTE (string))
1171 {
1172 int rest = STRING_BYTES (XSTRING (string)) - BYTEPOS (pos);
1173 unsigned char *p = XSTRING (string)->data + BYTEPOS (pos);
1174 int len;
1175
1176 while (nchars--)
1177 {
1178 string_char_and_length (p, rest, &len);
1179 p += len, rest -= len;
1180 xassert (rest >= 0);
1181 CHARPOS (pos) += 1;
1182 BYTEPOS (pos) += len;
1183 }
1184 }
1185 else
1186 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1187
1188 return pos;
1189 }
1190
1191
1192 /* Value is the text position, i.e. character and byte position,
1193 for character position CHARPOS in STRING. */
1194
1195 static INLINE struct text_pos
1196 string_pos (charpos, string)
1197 int charpos;
1198 Lisp_Object string;
1199 {
1200 struct text_pos pos;
1201 xassert (STRINGP (string));
1202 xassert (charpos >= 0);
1203 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1204 return pos;
1205 }
1206
1207
1208 /* Value is a text position, i.e. character and byte position, for
1209 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1210 means recognize multibyte characters. */
1211
1212 static struct text_pos
1213 c_string_pos (charpos, s, multibyte_p)
1214 int charpos;
1215 unsigned char *s;
1216 int multibyte_p;
1217 {
1218 struct text_pos pos;
1219
1220 xassert (s != NULL);
1221 xassert (charpos >= 0);
1222
1223 if (multibyte_p)
1224 {
1225 int rest = strlen (s), len;
1226
1227 SET_TEXT_POS (pos, 0, 0);
1228 while (charpos--)
1229 {
1230 string_char_and_length (s, rest, &len);
1231 s += len, rest -= len;
1232 xassert (rest >= 0);
1233 CHARPOS (pos) += 1;
1234 BYTEPOS (pos) += len;
1235 }
1236 }
1237 else
1238 SET_TEXT_POS (pos, charpos, charpos);
1239
1240 return pos;
1241 }
1242
1243
1244 /* Value is the number of characters in C string S. MULTIBYTE_P
1245 non-zero means recognize multibyte characters. */
1246
1247 static int
1248 number_of_chars (s, multibyte_p)
1249 unsigned char *s;
1250 int multibyte_p;
1251 {
1252 int nchars;
1253
1254 if (multibyte_p)
1255 {
1256 int rest = strlen (s), len;
1257 unsigned char *p = (unsigned char *) s;
1258
1259 for (nchars = 0; rest > 0; ++nchars)
1260 {
1261 string_char_and_length (p, rest, &len);
1262 rest -= len, p += len;
1263 }
1264 }
1265 else
1266 nchars = strlen (s);
1267
1268 return nchars;
1269 }
1270
1271
1272 /* Compute byte position NEWPOS->bytepos corresponding to
1273 NEWPOS->charpos. POS is a known position in string STRING.
1274 NEWPOS->charpos must be >= POS.charpos. */
1275
1276 static void
1277 compute_string_pos (newpos, pos, string)
1278 struct text_pos *newpos, pos;
1279 Lisp_Object string;
1280 {
1281 xassert (STRINGP (string));
1282 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1283
1284 if (STRING_MULTIBYTE (string))
1285 *newpos = string_pos_nchars_ahead (pos, string,
1286 CHARPOS (*newpos) - CHARPOS (pos));
1287 else
1288 BYTEPOS (*newpos) = CHARPOS (*newpos);
1289 }
1290
1291
1292 \f
1293 /***********************************************************************
1294 Lisp form evaluation
1295 ***********************************************************************/
1296
1297 /* Error handler for safe_eval and safe_call. */
1298
1299 static Lisp_Object
1300 safe_eval_handler (arg)
1301 Lisp_Object arg;
1302 {
1303 add_to_log ("Error during redisplay: %s", arg, Qnil);
1304 return Qnil;
1305 }
1306
1307
1308 /* Evaluate SEXPR and return the result, or nil if something went
1309 wrong. Prevent redisplay during the evaluation. */
1310
1311 Lisp_Object
1312 safe_eval (sexpr)
1313 Lisp_Object sexpr;
1314 {
1315 Lisp_Object val;
1316
1317 if (inhibit_eval_during_redisplay)
1318 val = Qnil;
1319 else
1320 {
1321 int count = BINDING_STACK_SIZE ();
1322 struct gcpro gcpro1;
1323
1324 GCPRO1 (sexpr);
1325 specbind (Qinhibit_redisplay, Qt);
1326 val = internal_condition_case_1 (Feval, sexpr, Qerror,
1327 safe_eval_handler);
1328 UNGCPRO;
1329 val = unbind_to (count, val);
1330 }
1331
1332 return val;
1333 }
1334
1335
1336 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1337 Return the result, or nil if something went wrong. Prevent
1338 redisplay during the evaluation. */
1339
1340 Lisp_Object
1341 safe_call (nargs, args)
1342 int nargs;
1343 Lisp_Object *args;
1344 {
1345 Lisp_Object val;
1346
1347 if (inhibit_eval_during_redisplay)
1348 val = Qnil;
1349 else
1350 {
1351 int count = BINDING_STACK_SIZE ();
1352 struct gcpro gcpro1;
1353
1354 GCPRO1 (args[0]);
1355 gcpro1.nvars = nargs;
1356 specbind (Qinhibit_redisplay, Qt);
1357 val = internal_condition_case_2 (Ffuncall, nargs, args, Qerror,
1358 safe_eval_handler);
1359 UNGCPRO;
1360 val = unbind_to (count, val);
1361 }
1362
1363 return val;
1364 }
1365
1366
1367 /* Call function FN with one argument ARG.
1368 Return the result, or nil if something went wrong. */
1369
1370 Lisp_Object
1371 safe_call1 (fn, arg)
1372 Lisp_Object fn, arg;
1373 {
1374 Lisp_Object args[2];
1375 args[0] = fn;
1376 args[1] = arg;
1377 return safe_call (2, args);
1378 }
1379
1380
1381 \f
1382 /***********************************************************************
1383 Debugging
1384 ***********************************************************************/
1385
1386 #if 0
1387
1388 /* Define CHECK_IT to perform sanity checks on iterators.
1389 This is for debugging. It is too slow to do unconditionally. */
1390
1391 static void
1392 check_it (it)
1393 struct it *it;
1394 {
1395 if (it->method == next_element_from_string)
1396 {
1397 xassert (STRINGP (it->string));
1398 xassert (IT_STRING_CHARPOS (*it) >= 0);
1399 }
1400 else if (it->method == next_element_from_buffer)
1401 {
1402 /* Check that character and byte positions agree. */
1403 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1404 }
1405
1406 if (it->dpvec)
1407 xassert (it->current.dpvec_index >= 0);
1408 else
1409 xassert (it->current.dpvec_index < 0);
1410 }
1411
1412 #define CHECK_IT(IT) check_it ((IT))
1413
1414 #else /* not 0 */
1415
1416 #define CHECK_IT(IT) (void) 0
1417
1418 #endif /* not 0 */
1419
1420
1421 #if GLYPH_DEBUG
1422
1423 /* Check that the window end of window W is what we expect it
1424 to be---the last row in the current matrix displaying text. */
1425
1426 static void
1427 check_window_end (w)
1428 struct window *w;
1429 {
1430 if (!MINI_WINDOW_P (w)
1431 && !NILP (w->window_end_valid))
1432 {
1433 struct glyph_row *row;
1434 xassert ((row = MATRIX_ROW (w->current_matrix,
1435 XFASTINT (w->window_end_vpos)),
1436 !row->enabled_p
1437 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1438 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1439 }
1440 }
1441
1442 #define CHECK_WINDOW_END(W) check_window_end ((W))
1443
1444 #else /* not GLYPH_DEBUG */
1445
1446 #define CHECK_WINDOW_END(W) (void) 0
1447
1448 #endif /* not GLYPH_DEBUG */
1449
1450
1451 \f
1452 /***********************************************************************
1453 Iterator initialization
1454 ***********************************************************************/
1455
1456 /* Initialize IT for displaying current_buffer in window W, starting
1457 at character position CHARPOS. CHARPOS < 0 means that no buffer
1458 position is specified which is useful when the iterator is assigned
1459 a position later. BYTEPOS is the byte position corresponding to
1460 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
1461
1462 If ROW is not null, calls to produce_glyphs with IT as parameter
1463 will produce glyphs in that row.
1464
1465 BASE_FACE_ID is the id of a base face to use. It must be one of
1466 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
1467 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
1468 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
1469
1470 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
1471 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
1472 will be initialized to use the corresponding mode line glyph row of
1473 the desired matrix of W. */
1474
1475 void
1476 init_iterator (it, w, charpos, bytepos, row, base_face_id)
1477 struct it *it;
1478 struct window *w;
1479 int charpos, bytepos;
1480 struct glyph_row *row;
1481 enum face_id base_face_id;
1482 {
1483 int highlight_region_p;
1484
1485 /* Some precondition checks. */
1486 xassert (w != NULL && it != NULL);
1487 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
1488 && charpos <= ZV));
1489
1490 /* If face attributes have been changed since the last redisplay,
1491 free realized faces now because they depend on face definitions
1492 that might have changed. */
1493 if (face_change_count)
1494 {
1495 face_change_count = 0;
1496 free_all_realized_faces (Qnil);
1497 }
1498
1499 /* Use one of the mode line rows of W's desired matrix if
1500 appropriate. */
1501 if (row == NULL)
1502 {
1503 if (base_face_id == MODE_LINE_FACE_ID
1504 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
1505 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
1506 else if (base_face_id == HEADER_LINE_FACE_ID)
1507 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
1508 }
1509
1510 /* Clear IT. */
1511 bzero (it, sizeof *it);
1512 it->current.overlay_string_index = -1;
1513 it->current.dpvec_index = -1;
1514 it->base_face_id = base_face_id;
1515
1516 /* The window in which we iterate over current_buffer: */
1517 XSETWINDOW (it->window, w);
1518 it->w = w;
1519 it->f = XFRAME (w->frame);
1520
1521 /* Extra space between lines (on window systems only). */
1522 if (base_face_id == DEFAULT_FACE_ID
1523 && FRAME_WINDOW_P (it->f))
1524 {
1525 if (NATNUMP (current_buffer->extra_line_spacing))
1526 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
1527 else if (it->f->extra_line_spacing > 0)
1528 it->extra_line_spacing = it->f->extra_line_spacing;
1529 }
1530
1531 /* If realized faces have been removed, e.g. because of face
1532 attribute changes of named faces, recompute them. When running
1533 in batch mode, the face cache of Vterminal_frame is null. If
1534 we happen to get called, make a dummy face cache. */
1535 if (
1536 #ifndef WINDOWSNT
1537 noninteractive &&
1538 #endif
1539 FRAME_FACE_CACHE (it->f) == NULL)
1540 init_frame_faces (it->f);
1541 if (FRAME_FACE_CACHE (it->f)->used == 0)
1542 recompute_basic_faces (it->f);
1543
1544 /* Current value of the `space-width', and 'height' properties. */
1545 it->space_width = Qnil;
1546 it->font_height = Qnil;
1547
1548 /* Are control characters displayed as `^C'? */
1549 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
1550
1551 /* -1 means everything between a CR and the following line end
1552 is invisible. >0 means lines indented more than this value are
1553 invisible. */
1554 it->selective = (INTEGERP (current_buffer->selective_display)
1555 ? XFASTINT (current_buffer->selective_display)
1556 : (!NILP (current_buffer->selective_display)
1557 ? -1 : 0));
1558 it->selective_display_ellipsis_p
1559 = !NILP (current_buffer->selective_display_ellipses);
1560
1561 /* Display table to use. */
1562 it->dp = window_display_table (w);
1563
1564 /* Are multibyte characters enabled in current_buffer? */
1565 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1566
1567 /* Non-zero if we should highlight the region. */
1568 highlight_region_p
1569 = (!NILP (Vtransient_mark_mode)
1570 && !NILP (current_buffer->mark_active)
1571 && XMARKER (current_buffer->mark)->buffer != 0);
1572
1573 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
1574 start and end of a visible region in window IT->w. Set both to
1575 -1 to indicate no region. */
1576 if (highlight_region_p
1577 /* Maybe highlight only in selected window. */
1578 && (/* Either show region everywhere. */
1579 highlight_nonselected_windows
1580 /* Or show region in the selected window. */
1581 || w == XWINDOW (selected_window)
1582 /* Or show the region if we are in the mini-buffer and W is
1583 the window the mini-buffer refers to. */
1584 || (MINI_WINDOW_P (XWINDOW (selected_window))
1585 && WINDOWP (Vminibuf_selected_window)
1586 && w == XWINDOW (Vminibuf_selected_window))))
1587 {
1588 int charpos = marker_position (current_buffer->mark);
1589 it->region_beg_charpos = min (PT, charpos);
1590 it->region_end_charpos = max (PT, charpos);
1591 }
1592 else
1593 it->region_beg_charpos = it->region_end_charpos = -1;
1594
1595 /* Get the position at which the redisplay_end_trigger hook should
1596 be run, if it is to be run at all. */
1597 if (MARKERP (w->redisplay_end_trigger)
1598 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
1599 it->redisplay_end_trigger_charpos
1600 = marker_position (w->redisplay_end_trigger);
1601 else if (INTEGERP (w->redisplay_end_trigger))
1602 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
1603
1604 /* Correct bogus values of tab_width. */
1605 it->tab_width = XINT (current_buffer->tab_width);
1606 if (it->tab_width <= 0 || it->tab_width > 1000)
1607 it->tab_width = 8;
1608
1609 /* Are lines in the display truncated? */
1610 it->truncate_lines_p
1611 = (base_face_id != DEFAULT_FACE_ID
1612 || XINT (it->w->hscroll)
1613 || (truncate_partial_width_windows
1614 && !WINDOW_FULL_WIDTH_P (it->w))
1615 || !NILP (current_buffer->truncate_lines));
1616
1617 /* Get dimensions of truncation and continuation glyphs. These are
1618 displayed as fringe bitmaps under X, so we don't need them for such
1619 frames. */
1620 if (!FRAME_WINDOW_P (it->f))
1621 {
1622 if (it->truncate_lines_p)
1623 {
1624 /* We will need the truncation glyph. */
1625 xassert (it->glyph_row == NULL);
1626 produce_special_glyphs (it, IT_TRUNCATION);
1627 it->truncation_pixel_width = it->pixel_width;
1628 }
1629 else
1630 {
1631 /* We will need the continuation glyph. */
1632 xassert (it->glyph_row == NULL);
1633 produce_special_glyphs (it, IT_CONTINUATION);
1634 it->continuation_pixel_width = it->pixel_width;
1635 }
1636
1637 /* Reset these values to zero because the produce_special_glyphs
1638 above has changed them. */
1639 it->pixel_width = it->ascent = it->descent = 0;
1640 it->phys_ascent = it->phys_descent = 0;
1641 }
1642
1643 /* Set this after getting the dimensions of truncation and
1644 continuation glyphs, so that we don't produce glyphs when calling
1645 produce_special_glyphs, above. */
1646 it->glyph_row = row;
1647 it->area = TEXT_AREA;
1648
1649 /* Get the dimensions of the display area. The display area
1650 consists of the visible window area plus a horizontally scrolled
1651 part to the left of the window. All x-values are relative to the
1652 start of this total display area. */
1653 if (base_face_id != DEFAULT_FACE_ID)
1654 {
1655 /* Mode lines, menu bar in terminal frames. */
1656 it->first_visible_x = 0;
1657 it->last_visible_x = XFASTINT (w->width) * CANON_X_UNIT (it->f);
1658 }
1659 else
1660 {
1661 it->first_visible_x
1662 = XFASTINT (it->w->hscroll) * CANON_X_UNIT (it->f);
1663 it->last_visible_x = (it->first_visible_x
1664 + window_box_width (w, TEXT_AREA));
1665
1666 /* If we truncate lines, leave room for the truncator glyph(s) at
1667 the right margin. Otherwise, leave room for the continuation
1668 glyph(s). Truncation and continuation glyphs are not inserted
1669 for window-based redisplay. */
1670 if (!FRAME_WINDOW_P (it->f))
1671 {
1672 if (it->truncate_lines_p)
1673 it->last_visible_x -= it->truncation_pixel_width;
1674 else
1675 it->last_visible_x -= it->continuation_pixel_width;
1676 }
1677
1678 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
1679 it->current_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w) + w->vscroll;
1680 }
1681
1682 /* Leave room for a border glyph. */
1683 if (!FRAME_WINDOW_P (it->f)
1684 && !WINDOW_RIGHTMOST_P (it->w))
1685 it->last_visible_x -= 1;
1686
1687 it->last_visible_y = window_text_bottom_y (w);
1688
1689 /* For mode lines and alike, arrange for the first glyph having a
1690 left box line if the face specifies a box. */
1691 if (base_face_id != DEFAULT_FACE_ID)
1692 {
1693 struct face *face;
1694
1695 it->face_id = base_face_id;
1696
1697 /* If we have a boxed mode line, make the first character appear
1698 with a left box line. */
1699 face = FACE_FROM_ID (it->f, base_face_id);
1700 if (face->box != FACE_NO_BOX)
1701 it->start_of_box_run_p = 1;
1702 }
1703
1704 /* If a buffer position was specified, set the iterator there,
1705 getting overlays and face properties from that position. */
1706 if (charpos >= BUF_BEG (current_buffer))
1707 {
1708 it->end_charpos = ZV;
1709 it->face_id = -1;
1710 IT_CHARPOS (*it) = charpos;
1711
1712 /* Compute byte position if not specified. */
1713 if (bytepos < charpos)
1714 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
1715 else
1716 IT_BYTEPOS (*it) = bytepos;
1717
1718 /* Compute faces etc. */
1719 reseat (it, it->current.pos, 1);
1720 }
1721
1722 CHECK_IT (it);
1723 }
1724
1725
1726 /* Initialize IT for the display of window W with window start POS. */
1727
1728 void
1729 start_display (it, w, pos)
1730 struct it *it;
1731 struct window *w;
1732 struct text_pos pos;
1733 {
1734 struct glyph_row *row;
1735 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
1736
1737 row = w->desired_matrix->rows + first_vpos;
1738 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
1739
1740 if (!it->truncate_lines_p)
1741 {
1742 int start_at_line_beg_p;
1743 int first_y = it->current_y;
1744
1745 /* If window start is not at a line start, skip forward to POS to
1746 get the correct continuation lines width. */
1747 start_at_line_beg_p = (CHARPOS (pos) == BEGV
1748 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
1749 if (!start_at_line_beg_p)
1750 {
1751 reseat_at_previous_visible_line_start (it);
1752 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
1753
1754 /* If lines are continued, this line may end in the middle
1755 of a multi-glyph character (e.g. a control character
1756 displayed as \003, or in the middle of an overlay
1757 string). In this case move_it_to above will not have
1758 taken us to the start of the continuation line but to the
1759 end of the continued line. */
1760 if (it->current_x > 0)
1761 {
1762 if (it->current.dpvec_index >= 0
1763 || it->current.overlay_string_index >= 0)
1764 {
1765 set_iterator_to_next (it, 1);
1766 move_it_in_display_line_to (it, -1, -1, 0);
1767 }
1768
1769 it->continuation_lines_width += it->current_x;
1770 }
1771
1772 /* We're starting a new display line, not affected by the
1773 height of the continued line, so clear the appropriate
1774 fields in the iterator structure. */
1775 it->max_ascent = it->max_descent = 0;
1776 it->max_phys_ascent = it->max_phys_descent = 0;
1777
1778 it->current_y = first_y;
1779 it->vpos = 0;
1780 it->current_x = it->hpos = 0;
1781 }
1782 }
1783
1784 #if 0 /* Don't assert the following because start_display is sometimes
1785 called intentionally with a window start that is not at a
1786 line start. Please leave this code in as a comment. */
1787
1788 /* Window start should be on a line start, now. */
1789 xassert (it->continuation_lines_width
1790 || IT_CHARPOS (it) == BEGV
1791 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
1792 #endif /* 0 */
1793 }
1794
1795
1796 /* Return 1 if POS is a position in ellipses displayed for invisible
1797 text. W is the window we display, for text property lookup. */
1798
1799 static int
1800 in_ellipses_for_invisible_text_p (pos, w)
1801 struct display_pos *pos;
1802 struct window *w;
1803 {
1804 Lisp_Object prop, window;
1805 int ellipses_p = 0;
1806 int charpos = CHARPOS (pos->pos);
1807
1808 /* If POS specifies a position in a display vector, this might
1809 be for an ellipsis displayed for invisible text. We won't
1810 get the iterator set up for delivering that ellipsis unless
1811 we make sure that it gets aware of the invisible text. */
1812 if (pos->dpvec_index >= 0
1813 && pos->overlay_string_index < 0
1814 && CHARPOS (pos->string_pos) < 0
1815 && charpos > BEGV
1816 && (XSETWINDOW (window, w),
1817 prop = Fget_char_property (make_number (charpos),
1818 Qinvisible, window),
1819 !TEXT_PROP_MEANS_INVISIBLE (prop)))
1820 {
1821 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
1822 window);
1823 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
1824 }
1825
1826 return ellipses_p;
1827 }
1828
1829
1830 /* Initialize IT for stepping through current_buffer in window W,
1831 starting at position POS that includes overlay string and display
1832 vector/ control character translation position information. Value
1833 is zero if there are overlay strings with newlines at POS. */
1834
1835 static int
1836 init_from_display_pos (it, w, pos)
1837 struct it *it;
1838 struct window *w;
1839 struct display_pos *pos;
1840 {
1841 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
1842 int i, overlay_strings_with_newlines = 0;
1843
1844 /* If POS specifies a position in a display vector, this might
1845 be for an ellipsis displayed for invisible text. We won't
1846 get the iterator set up for delivering that ellipsis unless
1847 we make sure that it gets aware of the invisible text. */
1848 if (in_ellipses_for_invisible_text_p (pos, w))
1849 {
1850 --charpos;
1851 bytepos = 0;
1852 }
1853
1854 /* Keep in mind: the call to reseat in init_iterator skips invisible
1855 text, so we might end up at a position different from POS. This
1856 is only a problem when POS is a row start after a newline and an
1857 overlay starts there with an after-string, and the overlay has an
1858 invisible property. Since we don't skip invisible text in
1859 display_line and elsewhere immediately after consuming the
1860 newline before the row start, such a POS will not be in a string,
1861 but the call to init_iterator below will move us to the
1862 after-string. */
1863 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
1864
1865 for (i = 0; i < it->n_overlay_strings; ++i)
1866 {
1867 char *s = XSTRING (it->overlay_strings[i])->data;
1868 char *e = s + STRING_BYTES (XSTRING (it->overlay_strings[i]));
1869
1870 while (s < e && *s != '\n')
1871 ++s;
1872
1873 if (s < e)
1874 {
1875 overlay_strings_with_newlines = 1;
1876 break;
1877 }
1878 }
1879
1880 /* If position is within an overlay string, set up IT to the right
1881 overlay string. */
1882 if (pos->overlay_string_index >= 0)
1883 {
1884 int relative_index;
1885
1886 /* If the first overlay string happens to have a `display'
1887 property for an image, the iterator will be set up for that
1888 image, and we have to undo that setup first before we can
1889 correct the overlay string index. */
1890 if (it->method == next_element_from_image)
1891 pop_it (it);
1892
1893 /* We already have the first chunk of overlay strings in
1894 IT->overlay_strings. Load more until the one for
1895 pos->overlay_string_index is in IT->overlay_strings. */
1896 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
1897 {
1898 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
1899 it->current.overlay_string_index = 0;
1900 while (n--)
1901 {
1902 load_overlay_strings (it, 0);
1903 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
1904 }
1905 }
1906
1907 it->current.overlay_string_index = pos->overlay_string_index;
1908 relative_index = (it->current.overlay_string_index
1909 % OVERLAY_STRING_CHUNK_SIZE);
1910 it->string = it->overlay_strings[relative_index];
1911 xassert (STRINGP (it->string));
1912 it->current.string_pos = pos->string_pos;
1913 it->method = next_element_from_string;
1914 }
1915
1916 #if 0 /* This is bogus because POS not having an overlay string
1917 position does not mean it's after the string. Example: A
1918 line starting with a before-string and initialization of IT
1919 to the previous row's end position. */
1920 else if (it->current.overlay_string_index >= 0)
1921 {
1922 /* If POS says we're already after an overlay string ending at
1923 POS, make sure to pop the iterator because it will be in
1924 front of that overlay string. When POS is ZV, we've thereby
1925 also ``processed'' overlay strings at ZV. */
1926 while (it->sp)
1927 pop_it (it);
1928 it->current.overlay_string_index = -1;
1929 it->method = next_element_from_buffer;
1930 if (CHARPOS (pos->pos) == ZV)
1931 it->overlay_strings_at_end_processed_p = 1;
1932 }
1933 #endif /* 0 */
1934
1935 if (CHARPOS (pos->string_pos) >= 0)
1936 {
1937 /* Recorded position is not in an overlay string, but in another
1938 string. This can only be a string from a `display' property.
1939 IT should already be filled with that string. */
1940 it->current.string_pos = pos->string_pos;
1941 xassert (STRINGP (it->string));
1942 }
1943
1944 /* Restore position in display vector translations, control
1945 character translations or ellipses. */
1946 if (pos->dpvec_index >= 0)
1947 {
1948 if (it->dpvec == NULL)
1949 get_next_display_element (it);
1950 xassert (it->dpvec && it->current.dpvec_index == 0);
1951 it->current.dpvec_index = pos->dpvec_index;
1952 }
1953
1954 CHECK_IT (it);
1955 return !overlay_strings_with_newlines;
1956 }
1957
1958
1959 /* Initialize IT for stepping through current_buffer in window W
1960 starting at ROW->start. */
1961
1962 static void
1963 init_to_row_start (it, w, row)
1964 struct it *it;
1965 struct window *w;
1966 struct glyph_row *row;
1967 {
1968 init_from_display_pos (it, w, &row->start);
1969 it->continuation_lines_width = row->continuation_lines_width;
1970 CHECK_IT (it);
1971 }
1972
1973
1974 /* Initialize IT for stepping through current_buffer in window W
1975 starting in the line following ROW, i.e. starting at ROW->end.
1976 Value is zero if there are overlay strings with newlines at ROW's
1977 end position. */
1978
1979 static int
1980 init_to_row_end (it, w, row)
1981 struct it *it;
1982 struct window *w;
1983 struct glyph_row *row;
1984 {
1985 int success = 0;
1986
1987 if (init_from_display_pos (it, w, &row->end))
1988 {
1989 if (row->continued_p)
1990 it->continuation_lines_width
1991 = row->continuation_lines_width + row->pixel_width;
1992 CHECK_IT (it);
1993 success = 1;
1994 }
1995
1996 return success;
1997 }
1998
1999
2000
2001 \f
2002 /***********************************************************************
2003 Text properties
2004 ***********************************************************************/
2005
2006 /* Called when IT reaches IT->stop_charpos. Handle text property and
2007 overlay changes. Set IT->stop_charpos to the next position where
2008 to stop. */
2009
2010 static void
2011 handle_stop (it)
2012 struct it *it;
2013 {
2014 enum prop_handled handled;
2015 int handle_overlay_change_p = 1;
2016 struct props *p;
2017
2018 it->dpvec = NULL;
2019 it->current.dpvec_index = -1;
2020
2021 do
2022 {
2023 handled = HANDLED_NORMALLY;
2024
2025 /* Call text property handlers. */
2026 for (p = it_props; p->handler; ++p)
2027 {
2028 handled = p->handler (it);
2029
2030 if (handled == HANDLED_RECOMPUTE_PROPS)
2031 break;
2032 else if (handled == HANDLED_RETURN)
2033 return;
2034 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2035 handle_overlay_change_p = 0;
2036 }
2037
2038 if (handled != HANDLED_RECOMPUTE_PROPS)
2039 {
2040 /* Don't check for overlay strings below when set to deliver
2041 characters from a display vector. */
2042 if (it->method == next_element_from_display_vector)
2043 handle_overlay_change_p = 0;
2044
2045 /* Handle overlay changes. */
2046 if (handle_overlay_change_p)
2047 handled = handle_overlay_change (it);
2048
2049 /* Determine where to stop next. */
2050 if (handled == HANDLED_NORMALLY)
2051 compute_stop_pos (it);
2052 }
2053 }
2054 while (handled == HANDLED_RECOMPUTE_PROPS);
2055 }
2056
2057
2058 /* Compute IT->stop_charpos from text property and overlay change
2059 information for IT's current position. */
2060
2061 static void
2062 compute_stop_pos (it)
2063 struct it *it;
2064 {
2065 register INTERVAL iv, next_iv;
2066 Lisp_Object object, limit, position;
2067
2068 /* If nowhere else, stop at the end. */
2069 it->stop_charpos = it->end_charpos;
2070
2071 if (STRINGP (it->string))
2072 {
2073 /* Strings are usually short, so don't limit the search for
2074 properties. */
2075 object = it->string;
2076 limit = Qnil;
2077 position = make_number (IT_STRING_CHARPOS (*it));
2078 }
2079 else
2080 {
2081 int charpos;
2082
2083 /* If next overlay change is in front of the current stop pos
2084 (which is IT->end_charpos), stop there. Note: value of
2085 next_overlay_change is point-max if no overlay change
2086 follows. */
2087 charpos = next_overlay_change (IT_CHARPOS (*it));
2088 if (charpos < it->stop_charpos)
2089 it->stop_charpos = charpos;
2090
2091 /* If showing the region, we have to stop at the region
2092 start or end because the face might change there. */
2093 if (it->region_beg_charpos > 0)
2094 {
2095 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2096 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2097 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2098 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2099 }
2100
2101 /* Set up variables for computing the stop position from text
2102 property changes. */
2103 XSETBUFFER (object, current_buffer);
2104 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2105 position = make_number (IT_CHARPOS (*it));
2106
2107 }
2108
2109 /* Get the interval containing IT's position. Value is a null
2110 interval if there isn't such an interval. */
2111 iv = validate_interval_range (object, &position, &position, 0);
2112 if (!NULL_INTERVAL_P (iv))
2113 {
2114 Lisp_Object values_here[LAST_PROP_IDX];
2115 struct props *p;
2116
2117 /* Get properties here. */
2118 for (p = it_props; p->handler; ++p)
2119 values_here[p->idx] = textget (iv->plist, *p->name);
2120
2121 /* Look for an interval following iv that has different
2122 properties. */
2123 for (next_iv = next_interval (iv);
2124 (!NULL_INTERVAL_P (next_iv)
2125 && (NILP (limit)
2126 || XFASTINT (limit) > next_iv->position));
2127 next_iv = next_interval (next_iv))
2128 {
2129 for (p = it_props; p->handler; ++p)
2130 {
2131 Lisp_Object new_value;
2132
2133 new_value = textget (next_iv->plist, *p->name);
2134 if (!EQ (values_here[p->idx], new_value))
2135 break;
2136 }
2137
2138 if (p->handler)
2139 break;
2140 }
2141
2142 if (!NULL_INTERVAL_P (next_iv))
2143 {
2144 if (INTEGERP (limit)
2145 && next_iv->position >= XFASTINT (limit))
2146 /* No text property change up to limit. */
2147 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2148 else
2149 /* Text properties change in next_iv. */
2150 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2151 }
2152 }
2153
2154 xassert (STRINGP (it->string)
2155 || (it->stop_charpos >= BEGV
2156 && it->stop_charpos >= IT_CHARPOS (*it)));
2157 }
2158
2159
2160 /* Return the position of the next overlay change after POS in
2161 current_buffer. Value is point-max if no overlay change
2162 follows. This is like `next-overlay-change' but doesn't use
2163 xmalloc. */
2164
2165 static int
2166 next_overlay_change (pos)
2167 int pos;
2168 {
2169 int noverlays;
2170 int endpos;
2171 Lisp_Object *overlays;
2172 int len;
2173 int i;
2174
2175 /* Get all overlays at the given position. */
2176 len = 10;
2177 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2178 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2179 if (noverlays > len)
2180 {
2181 len = noverlays;
2182 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2183 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2184 }
2185
2186 /* If any of these overlays ends before endpos,
2187 use its ending point instead. */
2188 for (i = 0; i < noverlays; ++i)
2189 {
2190 Lisp_Object oend;
2191 int oendpos;
2192
2193 oend = OVERLAY_END (overlays[i]);
2194 oendpos = OVERLAY_POSITION (oend);
2195 endpos = min (endpos, oendpos);
2196 }
2197
2198 return endpos;
2199 }
2200
2201
2202 \f
2203 /***********************************************************************
2204 Fontification
2205 ***********************************************************************/
2206
2207 /* Handle changes in the `fontified' property of the current buffer by
2208 calling hook functions from Qfontification_functions to fontify
2209 regions of text. */
2210
2211 static enum prop_handled
2212 handle_fontified_prop (it)
2213 struct it *it;
2214 {
2215 Lisp_Object prop, pos;
2216 enum prop_handled handled = HANDLED_NORMALLY;
2217
2218 /* Get the value of the `fontified' property at IT's current buffer
2219 position. (The `fontified' property doesn't have a special
2220 meaning in strings.) If the value is nil, call functions from
2221 Qfontification_functions. */
2222 if (!STRINGP (it->string)
2223 && it->s == NULL
2224 && !NILP (Vfontification_functions)
2225 && !NILP (Vrun_hooks)
2226 && (pos = make_number (IT_CHARPOS (*it)),
2227 prop = Fget_char_property (pos, Qfontified, Qnil),
2228 NILP (prop)))
2229 {
2230 int count = BINDING_STACK_SIZE ();
2231 Lisp_Object val;
2232
2233 val = Vfontification_functions;
2234 specbind (Qfontification_functions, Qnil);
2235
2236 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2237 safe_call1 (val, pos);
2238 else
2239 {
2240 Lisp_Object globals, fn;
2241 struct gcpro gcpro1, gcpro2;
2242
2243 globals = Qnil;
2244 GCPRO2 (val, globals);
2245
2246 for (; CONSP (val); val = XCDR (val))
2247 {
2248 fn = XCAR (val);
2249
2250 if (EQ (fn, Qt))
2251 {
2252 /* A value of t indicates this hook has a local
2253 binding; it means to run the global binding too.
2254 In a global value, t should not occur. If it
2255 does, we must ignore it to avoid an endless
2256 loop. */
2257 for (globals = Fdefault_value (Qfontification_functions);
2258 CONSP (globals);
2259 globals = XCDR (globals))
2260 {
2261 fn = XCAR (globals);
2262 if (!EQ (fn, Qt))
2263 safe_call1 (fn, pos);
2264 }
2265 }
2266 else
2267 safe_call1 (fn, pos);
2268 }
2269
2270 UNGCPRO;
2271 }
2272
2273 unbind_to (count, Qnil);
2274
2275 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2276 something. This avoids an endless loop if they failed to
2277 fontify the text for which reason ever. */
2278 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2279 handled = HANDLED_RECOMPUTE_PROPS;
2280 }
2281
2282 return handled;
2283 }
2284
2285
2286 \f
2287 /***********************************************************************
2288 Faces
2289 ***********************************************************************/
2290
2291 /* Set up iterator IT from face properties at its current position.
2292 Called from handle_stop. */
2293
2294 static enum prop_handled
2295 handle_face_prop (it)
2296 struct it *it;
2297 {
2298 int new_face_id, next_stop;
2299
2300 if (!STRINGP (it->string))
2301 {
2302 new_face_id
2303 = face_at_buffer_position (it->w,
2304 IT_CHARPOS (*it),
2305 it->region_beg_charpos,
2306 it->region_end_charpos,
2307 &next_stop,
2308 (IT_CHARPOS (*it)
2309 + TEXT_PROP_DISTANCE_LIMIT),
2310 0);
2311
2312 /* Is this a start of a run of characters with box face?
2313 Caveat: this can be called for a freshly initialized
2314 iterator; face_id is -1 in this case. We know that the new
2315 face will not change until limit, i.e. if the new face has a
2316 box, all characters up to limit will have one. But, as
2317 usual, we don't know whether limit is really the end. */
2318 if (new_face_id != it->face_id)
2319 {
2320 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2321
2322 /* If new face has a box but old face has not, this is
2323 the start of a run of characters with box, i.e. it has
2324 a shadow on the left side. The value of face_id of the
2325 iterator will be -1 if this is the initial call that gets
2326 the face. In this case, we have to look in front of IT's
2327 position and see whether there is a face != new_face_id. */
2328 it->start_of_box_run_p
2329 = (new_face->box != FACE_NO_BOX
2330 && (it->face_id >= 0
2331 || IT_CHARPOS (*it) == BEG
2332 || new_face_id != face_before_it_pos (it)));
2333 it->face_box_p = new_face->box != FACE_NO_BOX;
2334 }
2335 }
2336 else
2337 {
2338 int base_face_id, bufpos;
2339
2340 if (it->current.overlay_string_index >= 0)
2341 bufpos = IT_CHARPOS (*it);
2342 else
2343 bufpos = 0;
2344
2345 /* For strings from a buffer, i.e. overlay strings or strings
2346 from a `display' property, use the face at IT's current
2347 buffer position as the base face to merge with, so that
2348 overlay strings appear in the same face as surrounding
2349 text, unless they specify their own faces. */
2350 base_face_id = underlying_face_id (it);
2351
2352 new_face_id = face_at_string_position (it->w,
2353 it->string,
2354 IT_STRING_CHARPOS (*it),
2355 bufpos,
2356 it->region_beg_charpos,
2357 it->region_end_charpos,
2358 &next_stop,
2359 base_face_id, 0);
2360
2361 #if 0 /* This shouldn't be neccessary. Let's check it. */
2362 /* If IT is used to display a mode line we would really like to
2363 use the mode line face instead of the frame's default face. */
2364 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2365 && new_face_id == DEFAULT_FACE_ID)
2366 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
2367 #endif
2368
2369 /* Is this a start of a run of characters with box? Caveat:
2370 this can be called for a freshly allocated iterator; face_id
2371 is -1 is this case. We know that the new face will not
2372 change until the next check pos, i.e. if the new face has a
2373 box, all characters up to that position will have a
2374 box. But, as usual, we don't know whether that position
2375 is really the end. */
2376 if (new_face_id != it->face_id)
2377 {
2378 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2379 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2380
2381 /* If new face has a box but old face hasn't, this is the
2382 start of a run of characters with box, i.e. it has a
2383 shadow on the left side. */
2384 it->start_of_box_run_p
2385 = new_face->box && (old_face == NULL || !old_face->box);
2386 it->face_box_p = new_face->box != FACE_NO_BOX;
2387 }
2388 }
2389
2390 it->face_id = new_face_id;
2391 return HANDLED_NORMALLY;
2392 }
2393
2394
2395 /* Return the ID of the face ``underlying'' IT's current position,
2396 which is in a string. If the iterator is associated with a
2397 buffer, return the face at IT's current buffer position.
2398 Otherwise, use the iterator's base_face_id. */
2399
2400 static int
2401 underlying_face_id (it)
2402 struct it *it;
2403 {
2404 int face_id = it->base_face_id, i;
2405
2406 xassert (STRINGP (it->string));
2407
2408 for (i = it->sp - 1; i >= 0; --i)
2409 if (NILP (it->stack[i].string))
2410 face_id = it->stack[i].face_id;
2411
2412 return face_id;
2413 }
2414
2415
2416 /* Compute the face one character before or after the current position
2417 of IT. BEFORE_P non-zero means get the face in front of IT's
2418 position. Value is the id of the face. */
2419
2420 static int
2421 face_before_or_after_it_pos (it, before_p)
2422 struct it *it;
2423 int before_p;
2424 {
2425 int face_id, limit;
2426 int next_check_charpos;
2427 struct text_pos pos;
2428
2429 xassert (it->s == NULL);
2430
2431 if (STRINGP (it->string))
2432 {
2433 int bufpos, base_face_id;
2434
2435 /* No face change past the end of the string (for the case
2436 we are padding with spaces). No face change before the
2437 string start. */
2438 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size
2439 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2440 return it->face_id;
2441
2442 /* Set pos to the position before or after IT's current position. */
2443 if (before_p)
2444 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2445 else
2446 /* For composition, we must check the character after the
2447 composition. */
2448 pos = (it->what == IT_COMPOSITION
2449 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
2450 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
2451
2452 if (it->current.overlay_string_index >= 0)
2453 bufpos = IT_CHARPOS (*it);
2454 else
2455 bufpos = 0;
2456
2457 base_face_id = underlying_face_id (it);
2458
2459 /* Get the face for ASCII, or unibyte. */
2460 face_id = face_at_string_position (it->w,
2461 it->string,
2462 CHARPOS (pos),
2463 bufpos,
2464 it->region_beg_charpos,
2465 it->region_end_charpos,
2466 &next_check_charpos,
2467 base_face_id, 0);
2468
2469 /* Correct the face for charsets different from ASCII. Do it
2470 for the multibyte case only. The face returned above is
2471 suitable for unibyte text if IT->string is unibyte. */
2472 if (STRING_MULTIBYTE (it->string))
2473 {
2474 unsigned char *p = XSTRING (it->string)->data + BYTEPOS (pos);
2475 int rest = STRING_BYTES (XSTRING (it->string)) - BYTEPOS (pos);
2476 int c, len;
2477 struct face *face = FACE_FROM_ID (it->f, face_id);
2478
2479 c = string_char_and_length (p, rest, &len);
2480 face_id = FACE_FOR_CHAR (it->f, face, c);
2481 }
2482 }
2483 else
2484 {
2485 if ((IT_CHARPOS (*it) >= ZV && !before_p)
2486 || (IT_CHARPOS (*it) <= BEGV && before_p))
2487 return it->face_id;
2488
2489 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
2490 pos = it->current.pos;
2491
2492 if (before_p)
2493 DEC_TEXT_POS (pos, it->multibyte_p);
2494 else
2495 {
2496 if (it->what == IT_COMPOSITION)
2497 /* For composition, we must check the position after the
2498 composition. */
2499 pos.charpos += it->cmp_len, pos.bytepos += it->len;
2500 else
2501 INC_TEXT_POS (pos, it->multibyte_p);
2502 }
2503
2504 /* Determine face for CHARSET_ASCII, or unibyte. */
2505 face_id = face_at_buffer_position (it->w,
2506 CHARPOS (pos),
2507 it->region_beg_charpos,
2508 it->region_end_charpos,
2509 &next_check_charpos,
2510 limit, 0);
2511
2512 /* Correct the face for charsets different from ASCII. Do it
2513 for the multibyte case only. The face returned above is
2514 suitable for unibyte text if current_buffer is unibyte. */
2515 if (it->multibyte_p)
2516 {
2517 int c = FETCH_MULTIBYTE_CHAR (CHARPOS (pos));
2518 struct face *face = FACE_FROM_ID (it->f, face_id);
2519 face_id = FACE_FOR_CHAR (it->f, face, c);
2520 }
2521 }
2522
2523 return face_id;
2524 }
2525
2526
2527 \f
2528 /***********************************************************************
2529 Invisible text
2530 ***********************************************************************/
2531
2532 /* Set up iterator IT from invisible properties at its current
2533 position. Called from handle_stop. */
2534
2535 static enum prop_handled
2536 handle_invisible_prop (it)
2537 struct it *it;
2538 {
2539 enum prop_handled handled = HANDLED_NORMALLY;
2540
2541 if (STRINGP (it->string))
2542 {
2543 extern Lisp_Object Qinvisible;
2544 Lisp_Object prop, end_charpos, limit, charpos;
2545
2546 /* Get the value of the invisible text property at the
2547 current position. Value will be nil if there is no such
2548 property. */
2549 charpos = make_number (IT_STRING_CHARPOS (*it));
2550 prop = Fget_text_property (charpos, Qinvisible, it->string);
2551
2552 if (!NILP (prop)
2553 && IT_STRING_CHARPOS (*it) < it->end_charpos)
2554 {
2555 handled = HANDLED_RECOMPUTE_PROPS;
2556
2557 /* Get the position at which the next change of the
2558 invisible text property can be found in IT->string.
2559 Value will be nil if the property value is the same for
2560 all the rest of IT->string. */
2561 XSETINT (limit, XSTRING (it->string)->size);
2562 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2563 it->string, limit);
2564
2565 /* Text at current position is invisible. The next
2566 change in the property is at position end_charpos.
2567 Move IT's current position to that position. */
2568 if (INTEGERP (end_charpos)
2569 && XFASTINT (end_charpos) < XFASTINT (limit))
2570 {
2571 struct text_pos old;
2572 old = it->current.string_pos;
2573 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
2574 compute_string_pos (&it->current.string_pos, old, it->string);
2575 }
2576 else
2577 {
2578 /* The rest of the string is invisible. If this is an
2579 overlay string, proceed with the next overlay string
2580 or whatever comes and return a character from there. */
2581 if (it->current.overlay_string_index >= 0)
2582 {
2583 next_overlay_string (it);
2584 /* Don't check for overlay strings when we just
2585 finished processing them. */
2586 handled = HANDLED_OVERLAY_STRING_CONSUMED;
2587 }
2588 else
2589 {
2590 struct Lisp_String *s = XSTRING (it->string);
2591 IT_STRING_CHARPOS (*it) = s->size;
2592 IT_STRING_BYTEPOS (*it) = STRING_BYTES (s);
2593 }
2594 }
2595 }
2596 }
2597 else
2598 {
2599 int invis_p, newpos, next_stop, start_charpos;
2600 Lisp_Object pos, prop, overlay;
2601
2602 /* First of all, is there invisible text at this position? */
2603 start_charpos = IT_CHARPOS (*it);
2604 pos = make_number (IT_CHARPOS (*it));
2605 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
2606 &overlay);
2607 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
2608
2609 /* If we are on invisible text, skip over it. */
2610 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
2611 {
2612 /* Record whether we have to display an ellipsis for the
2613 invisible text. */
2614 int display_ellipsis_p = invis_p == 2;
2615
2616 handled = HANDLED_RECOMPUTE_PROPS;
2617
2618 /* Loop skipping over invisible text. The loop is left at
2619 ZV or with IT on the first char being visible again. */
2620 do
2621 {
2622 /* Try to skip some invisible text. Return value is the
2623 position reached which can be equal to IT's position
2624 if there is nothing invisible here. This skips both
2625 over invisible text properties and overlays with
2626 invisible property. */
2627 newpos = skip_invisible (IT_CHARPOS (*it),
2628 &next_stop, ZV, it->window);
2629
2630 /* If we skipped nothing at all we weren't at invisible
2631 text in the first place. If everything to the end of
2632 the buffer was skipped, end the loop. */
2633 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
2634 invis_p = 0;
2635 else
2636 {
2637 /* We skipped some characters but not necessarily
2638 all there are. Check if we ended up on visible
2639 text. Fget_char_property returns the property of
2640 the char before the given position, i.e. if we
2641 get invis_p = 0, this means that the char at
2642 newpos is visible. */
2643 pos = make_number (newpos);
2644 prop = Fget_char_property (pos, Qinvisible, it->window);
2645 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
2646 }
2647
2648 /* If we ended up on invisible text, proceed to
2649 skip starting with next_stop. */
2650 if (invis_p)
2651 IT_CHARPOS (*it) = next_stop;
2652 }
2653 while (invis_p);
2654
2655 /* The position newpos is now either ZV or on visible text. */
2656 IT_CHARPOS (*it) = newpos;
2657 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2658
2659 /* If there are before-strings at the start of invisible
2660 text, and the text is invisible because of a text
2661 property, arrange to show before-strings because 20.x did
2662 it that way. (If the text is invisible because of an
2663 overlay property instead of a text property, this is
2664 already handled in the overlay code.) */
2665 if (NILP (overlay)
2666 && get_overlay_strings (it, start_charpos))
2667 {
2668 handled = HANDLED_RECOMPUTE_PROPS;
2669 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
2670 }
2671 else if (display_ellipsis_p)
2672 setup_for_ellipsis (it);
2673 }
2674 }
2675
2676 return handled;
2677 }
2678
2679
2680 /* Make iterator IT return `...' next. */
2681
2682 static void
2683 setup_for_ellipsis (it)
2684 struct it *it;
2685 {
2686 if (it->dp
2687 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
2688 {
2689 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
2690 it->dpvec = v->contents;
2691 it->dpend = v->contents + v->size;
2692 }
2693 else
2694 {
2695 /* Default `...'. */
2696 it->dpvec = default_invis_vector;
2697 it->dpend = default_invis_vector + 3;
2698 }
2699
2700 /* The ellipsis display does not replace the display of the
2701 character at the new position. Indicate this by setting
2702 IT->dpvec_char_len to zero. */
2703 it->dpvec_char_len = 0;
2704
2705 it->current.dpvec_index = 0;
2706 it->method = next_element_from_display_vector;
2707 }
2708
2709
2710 \f
2711 /***********************************************************************
2712 'display' property
2713 ***********************************************************************/
2714
2715 /* Set up iterator IT from `display' property at its current position.
2716 Called from handle_stop. */
2717
2718 static enum prop_handled
2719 handle_display_prop (it)
2720 struct it *it;
2721 {
2722 Lisp_Object prop, object;
2723 struct text_pos *position;
2724 int display_replaced_p = 0;
2725
2726 if (STRINGP (it->string))
2727 {
2728 object = it->string;
2729 position = &it->current.string_pos;
2730 }
2731 else
2732 {
2733 object = it->w->buffer;
2734 position = &it->current.pos;
2735 }
2736
2737 /* Reset those iterator values set from display property values. */
2738 it->font_height = Qnil;
2739 it->space_width = Qnil;
2740 it->voffset = 0;
2741
2742 /* We don't support recursive `display' properties, i.e. string
2743 values that have a string `display' property, that have a string
2744 `display' property etc. */
2745 if (!it->string_from_display_prop_p)
2746 it->area = TEXT_AREA;
2747
2748 prop = Fget_char_property (make_number (position->charpos),
2749 Qdisplay, object);
2750 if (NILP (prop))
2751 return HANDLED_NORMALLY;
2752
2753 if (CONSP (prop)
2754 /* Simple properties. */
2755 && !EQ (XCAR (prop), Qimage)
2756 && !EQ (XCAR (prop), Qspace)
2757 && !EQ (XCAR (prop), Qwhen)
2758 && !EQ (XCAR (prop), Qspace_width)
2759 && !EQ (XCAR (prop), Qheight)
2760 && !EQ (XCAR (prop), Qraise)
2761 /* Marginal area specifications. */
2762 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
2763 && !NILP (XCAR (prop)))
2764 {
2765 for (; CONSP (prop); prop = XCDR (prop))
2766 {
2767 if (handle_single_display_prop (it, XCAR (prop), object,
2768 position, display_replaced_p))
2769 display_replaced_p = 1;
2770 }
2771 }
2772 else if (VECTORP (prop))
2773 {
2774 int i;
2775 for (i = 0; i < ASIZE (prop); ++i)
2776 if (handle_single_display_prop (it, AREF (prop, i), object,
2777 position, display_replaced_p))
2778 display_replaced_p = 1;
2779 }
2780 else
2781 {
2782 if (handle_single_display_prop (it, prop, object, position, 0))
2783 display_replaced_p = 1;
2784 }
2785
2786 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
2787 }
2788
2789
2790 /* Value is the position of the end of the `display' property starting
2791 at START_POS in OBJECT. */
2792
2793 static struct text_pos
2794 display_prop_end (it, object, start_pos)
2795 struct it *it;
2796 Lisp_Object object;
2797 struct text_pos start_pos;
2798 {
2799 Lisp_Object end;
2800 struct text_pos end_pos;
2801
2802 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
2803 Qdisplay, object, Qnil);
2804 CHARPOS (end_pos) = XFASTINT (end);
2805 if (STRINGP (object))
2806 compute_string_pos (&end_pos, start_pos, it->string);
2807 else
2808 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
2809
2810 return end_pos;
2811 }
2812
2813
2814 /* Set up IT from a single `display' sub-property value PROP. OBJECT
2815 is the object in which the `display' property was found. *POSITION
2816 is the position at which it was found. DISPLAY_REPLACED_P non-zero
2817 means that we previously saw a display sub-property which already
2818 replaced text display with something else, for example an image;
2819 ignore such properties after the first one has been processed.
2820
2821 If PROP is a `space' or `image' sub-property, set *POSITION to the
2822 end position of the `display' property.
2823
2824 Value is non-zero if something was found which replaces the display
2825 of buffer or string text. */
2826
2827 static int
2828 handle_single_display_prop (it, prop, object, position,
2829 display_replaced_before_p)
2830 struct it *it;
2831 Lisp_Object prop;
2832 Lisp_Object object;
2833 struct text_pos *position;
2834 int display_replaced_before_p;
2835 {
2836 Lisp_Object value;
2837 int replaces_text_display_p = 0;
2838 Lisp_Object form;
2839
2840 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
2841 evaluated. If the result is nil, VALUE is ignored. */
2842 form = Qt;
2843 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2844 {
2845 prop = XCDR (prop);
2846 if (!CONSP (prop))
2847 return 0;
2848 form = XCAR (prop);
2849 prop = XCDR (prop);
2850 }
2851
2852 if (!NILP (form) && !EQ (form, Qt))
2853 {
2854 int count = BINDING_STACK_SIZE ();
2855 struct gcpro gcpro1;
2856
2857 /* Bind `object' to the object having the `display' property, a
2858 buffer or string. Bind `position' to the position in the
2859 object where the property was found, and `buffer-position'
2860 to the current position in the buffer. */
2861 specbind (Qobject, object);
2862 specbind (Qposition, make_number (CHARPOS (*position)));
2863 specbind (Qbuffer_position,
2864 make_number (STRINGP (object)
2865 ? IT_CHARPOS (*it) : CHARPOS (*position)));
2866 GCPRO1 (form);
2867 form = safe_eval (form);
2868 UNGCPRO;
2869 unbind_to (count, Qnil);
2870 }
2871
2872 if (NILP (form))
2873 return 0;
2874
2875 if (CONSP (prop)
2876 && EQ (XCAR (prop), Qheight)
2877 && CONSP (XCDR (prop)))
2878 {
2879 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2880 return 0;
2881
2882 /* `(height HEIGHT)'. */
2883 it->font_height = XCAR (XCDR (prop));
2884 if (!NILP (it->font_height))
2885 {
2886 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2887 int new_height = -1;
2888
2889 if (CONSP (it->font_height)
2890 && (EQ (XCAR (it->font_height), Qplus)
2891 || EQ (XCAR (it->font_height), Qminus))
2892 && CONSP (XCDR (it->font_height))
2893 && INTEGERP (XCAR (XCDR (it->font_height))))
2894 {
2895 /* `(+ N)' or `(- N)' where N is an integer. */
2896 int steps = XINT (XCAR (XCDR (it->font_height)));
2897 if (EQ (XCAR (it->font_height), Qplus))
2898 steps = - steps;
2899 it->face_id = smaller_face (it->f, it->face_id, steps);
2900 }
2901 else if (FUNCTIONP (it->font_height))
2902 {
2903 /* Call function with current height as argument.
2904 Value is the new height. */
2905 Lisp_Object height;
2906 height = safe_call1 (it->font_height,
2907 face->lface[LFACE_HEIGHT_INDEX]);
2908 if (NUMBERP (height))
2909 new_height = XFLOATINT (height);
2910 }
2911 else if (NUMBERP (it->font_height))
2912 {
2913 /* Value is a multiple of the canonical char height. */
2914 struct face *face;
2915
2916 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
2917 new_height = (XFLOATINT (it->font_height)
2918 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
2919 }
2920 else
2921 {
2922 /* Evaluate IT->font_height with `height' bound to the
2923 current specified height to get the new height. */
2924 Lisp_Object value;
2925 int count = BINDING_STACK_SIZE ();
2926
2927 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
2928 value = safe_eval (it->font_height);
2929 unbind_to (count, Qnil);
2930
2931 if (NUMBERP (value))
2932 new_height = XFLOATINT (value);
2933 }
2934
2935 if (new_height > 0)
2936 it->face_id = face_with_height (it->f, it->face_id, new_height);
2937 }
2938 }
2939 else if (CONSP (prop)
2940 && EQ (XCAR (prop), Qspace_width)
2941 && CONSP (XCDR (prop)))
2942 {
2943 /* `(space_width WIDTH)'. */
2944 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2945 return 0;
2946
2947 value = XCAR (XCDR (prop));
2948 if (NUMBERP (value) && XFLOATINT (value) > 0)
2949 it->space_width = value;
2950 }
2951 else if (CONSP (prop)
2952 && EQ (XCAR (prop), Qraise)
2953 && CONSP (XCDR (prop)))
2954 {
2955 /* `(raise FACTOR)'. */
2956 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2957 return 0;
2958
2959 #ifdef HAVE_WINDOW_SYSTEM
2960 value = XCAR (XCDR (prop));
2961 if (NUMBERP (value))
2962 {
2963 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2964 it->voffset = - (XFLOATINT (value)
2965 * (FONT_HEIGHT (face->font)));
2966 }
2967 #endif /* HAVE_WINDOW_SYSTEM */
2968 }
2969 else if (!it->string_from_display_prop_p)
2970 {
2971 /* `((margin left-margin) VALUE)' or `((margin right-margin)
2972 VALUE) or `((margin nil) VALUE)' or VALUE. */
2973 Lisp_Object location, value;
2974 struct text_pos start_pos;
2975 int valid_p;
2976
2977 /* Characters having this form of property are not displayed, so
2978 we have to find the end of the property. */
2979 start_pos = *position;
2980 *position = display_prop_end (it, object, start_pos);
2981 value = Qnil;
2982
2983 /* Let's stop at the new position and assume that all
2984 text properties change there. */
2985 it->stop_charpos = position->charpos;
2986
2987 location = Qunbound;
2988 if (CONSP (prop) && CONSP (XCAR (prop)))
2989 {
2990 Lisp_Object tem;
2991
2992 value = XCDR (prop);
2993 if (CONSP (value))
2994 value = XCAR (value);
2995
2996 tem = XCAR (prop);
2997 if (EQ (XCAR (tem), Qmargin)
2998 && (tem = XCDR (tem),
2999 tem = CONSP (tem) ? XCAR (tem) : Qnil,
3000 (NILP (tem)
3001 || EQ (tem, Qleft_margin)
3002 || EQ (tem, Qright_margin))))
3003 location = tem;
3004 }
3005
3006 if (EQ (location, Qunbound))
3007 {
3008 location = Qnil;
3009 value = prop;
3010 }
3011
3012 #ifdef HAVE_WINDOW_SYSTEM
3013 if (FRAME_TERMCAP_P (it->f))
3014 valid_p = STRINGP (value);
3015 else
3016 valid_p = (STRINGP (value)
3017 || (CONSP (value) && EQ (XCAR (value), Qspace))
3018 || valid_image_p (value));
3019 #else /* not HAVE_WINDOW_SYSTEM */
3020 valid_p = STRINGP (value);
3021 #endif /* not HAVE_WINDOW_SYSTEM */
3022
3023 if ((EQ (location, Qleft_margin)
3024 || EQ (location, Qright_margin)
3025 || NILP (location))
3026 && valid_p
3027 && !display_replaced_before_p)
3028 {
3029 replaces_text_display_p = 1;
3030
3031 /* Save current settings of IT so that we can restore them
3032 when we are finished with the glyph property value. */
3033 push_it (it);
3034
3035 if (NILP (location))
3036 it->area = TEXT_AREA;
3037 else if (EQ (location, Qleft_margin))
3038 it->area = LEFT_MARGIN_AREA;
3039 else
3040 it->area = RIGHT_MARGIN_AREA;
3041
3042 if (STRINGP (value))
3043 {
3044 it->string = value;
3045 it->multibyte_p = STRING_MULTIBYTE (it->string);
3046 it->current.overlay_string_index = -1;
3047 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3048 it->end_charpos = it->string_nchars
3049 = XSTRING (it->string)->size;
3050 it->method = next_element_from_string;
3051 it->stop_charpos = 0;
3052 it->string_from_display_prop_p = 1;
3053 /* Say that we haven't consumed the characters with
3054 `display' property yet. The call to pop_it in
3055 set_iterator_to_next will clean this up. */
3056 *position = start_pos;
3057 }
3058 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3059 {
3060 it->method = next_element_from_stretch;
3061 it->object = value;
3062 it->current.pos = it->position = start_pos;
3063 }
3064 #ifdef HAVE_WINDOW_SYSTEM
3065 else
3066 {
3067 it->what = IT_IMAGE;
3068 it->image_id = lookup_image (it->f, value);
3069 it->position = start_pos;
3070 it->object = NILP (object) ? it->w->buffer : object;
3071 it->method = next_element_from_image;
3072
3073 /* Say that we haven't consumed the characters with
3074 `display' property yet. The call to pop_it in
3075 set_iterator_to_next will clean this up. */
3076 *position = start_pos;
3077 }
3078 #endif /* HAVE_WINDOW_SYSTEM */
3079 }
3080 else
3081 /* Invalid property or property not supported. Restore
3082 the position to what it was before. */
3083 *position = start_pos;
3084 }
3085
3086 return replaces_text_display_p;
3087 }
3088
3089
3090 /* Check if PROP is a display sub-property value whose text should be
3091 treated as intangible. */
3092
3093 static int
3094 single_display_prop_intangible_p (prop)
3095 Lisp_Object prop;
3096 {
3097 /* Skip over `when FORM'. */
3098 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3099 {
3100 prop = XCDR (prop);
3101 if (!CONSP (prop))
3102 return 0;
3103 prop = XCDR (prop);
3104 }
3105
3106 if (!CONSP (prop))
3107 return 0;
3108
3109 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3110 we don't need to treat text as intangible. */
3111 if (EQ (XCAR (prop), Qmargin))
3112 {
3113 prop = XCDR (prop);
3114 if (!CONSP (prop))
3115 return 0;
3116
3117 prop = XCDR (prop);
3118 if (!CONSP (prop)
3119 || EQ (XCAR (prop), Qleft_margin)
3120 || EQ (XCAR (prop), Qright_margin))
3121 return 0;
3122 }
3123
3124 return CONSP (prop) && EQ (XCAR (prop), Qimage);
3125 }
3126
3127
3128 /* Check if PROP is a display property value whose text should be
3129 treated as intangible. */
3130
3131 int
3132 display_prop_intangible_p (prop)
3133 Lisp_Object prop;
3134 {
3135 if (CONSP (prop)
3136 && CONSP (XCAR (prop))
3137 && !EQ (Qmargin, XCAR (XCAR (prop))))
3138 {
3139 /* A list of sub-properties. */
3140 while (CONSP (prop))
3141 {
3142 if (single_display_prop_intangible_p (XCAR (prop)))
3143 return 1;
3144 prop = XCDR (prop);
3145 }
3146 }
3147 else if (VECTORP (prop))
3148 {
3149 /* A vector of sub-properties. */
3150 int i;
3151 for (i = 0; i < ASIZE (prop); ++i)
3152 if (single_display_prop_intangible_p (AREF (prop, i)))
3153 return 1;
3154 }
3155 else
3156 return single_display_prop_intangible_p (prop);
3157
3158 return 0;
3159 }
3160
3161
3162 /* Return 1 if PROP is a display sub-property value containing STRING. */
3163
3164 static int
3165 single_display_prop_string_p (prop, string)
3166 Lisp_Object prop, string;
3167 {
3168 if (EQ (string, prop))
3169 return 1;
3170
3171 /* Skip over `when FORM'. */
3172 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3173 {
3174 prop = XCDR (prop);
3175 if (!CONSP (prop))
3176 return 0;
3177 prop = XCDR (prop);
3178 }
3179
3180 if (CONSP (prop))
3181 /* Skip over `margin LOCATION'. */
3182 if (EQ (XCAR (prop), Qmargin))
3183 {
3184 prop = XCDR (prop);
3185 if (!CONSP (prop))
3186 return 0;
3187
3188 prop = XCDR (prop);
3189 if (!CONSP (prop))
3190 return 0;
3191 }
3192
3193 return CONSP (prop) && EQ (XCAR (prop), string);
3194 }
3195
3196
3197 /* Return 1 if STRING appears in the `display' property PROP. */
3198
3199 static int
3200 display_prop_string_p (prop, string)
3201 Lisp_Object prop, string;
3202 {
3203 if (CONSP (prop)
3204 && CONSP (XCAR (prop))
3205 && !EQ (Qmargin, XCAR (XCAR (prop))))
3206 {
3207 /* A list of sub-properties. */
3208 while (CONSP (prop))
3209 {
3210 if (single_display_prop_string_p (XCAR (prop), string))
3211 return 1;
3212 prop = XCDR (prop);
3213 }
3214 }
3215 else if (VECTORP (prop))
3216 {
3217 /* A vector of sub-properties. */
3218 int i;
3219 for (i = 0; i < ASIZE (prop); ++i)
3220 if (single_display_prop_string_p (AREF (prop, i), string))
3221 return 1;
3222 }
3223 else
3224 return single_display_prop_string_p (prop, string);
3225
3226 return 0;
3227 }
3228
3229
3230 /* Determine from which buffer position in W's buffer STRING comes
3231 from. AROUND_CHARPOS is an approximate position where it could
3232 be from. Value is the buffer position or 0 if it couldn't be
3233 determined.
3234
3235 W's buffer must be current.
3236
3237 This function is necessary because we don't record buffer positions
3238 in glyphs generated from strings (to keep struct glyph small).
3239 This function may only use code that doesn't eval because it is
3240 called asynchronously from note_mouse_highlight. */
3241
3242 int
3243 string_buffer_position (w, string, around_charpos)
3244 struct window *w;
3245 Lisp_Object string;
3246 int around_charpos;
3247 {
3248 Lisp_Object limit, prop, pos;
3249 const int MAX_DISTANCE = 1000;
3250 int found = 0;
3251
3252 pos = make_number (around_charpos);
3253 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
3254 while (!found && !EQ (pos, limit))
3255 {
3256 prop = Fget_char_property (pos, Qdisplay, Qnil);
3257 if (!NILP (prop) && display_prop_string_p (prop, string))
3258 found = 1;
3259 else
3260 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
3261 }
3262
3263 if (!found)
3264 {
3265 pos = make_number (around_charpos);
3266 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
3267 while (!found && !EQ (pos, limit))
3268 {
3269 prop = Fget_char_property (pos, Qdisplay, Qnil);
3270 if (!NILP (prop) && display_prop_string_p (prop, string))
3271 found = 1;
3272 else
3273 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
3274 limit);
3275 }
3276 }
3277
3278 return found ? XINT (pos) : 0;
3279 }
3280
3281
3282 \f
3283 /***********************************************************************
3284 `composition' property
3285 ***********************************************************************/
3286
3287 /* Set up iterator IT from `composition' property at its current
3288 position. Called from handle_stop. */
3289
3290 static enum prop_handled
3291 handle_composition_prop (it)
3292 struct it *it;
3293 {
3294 Lisp_Object prop, string;
3295 int pos, pos_byte, end;
3296 enum prop_handled handled = HANDLED_NORMALLY;
3297
3298 if (STRINGP (it->string))
3299 {
3300 pos = IT_STRING_CHARPOS (*it);
3301 pos_byte = IT_STRING_BYTEPOS (*it);
3302 string = it->string;
3303 }
3304 else
3305 {
3306 pos = IT_CHARPOS (*it);
3307 pos_byte = IT_BYTEPOS (*it);
3308 string = Qnil;
3309 }
3310
3311 /* If there's a valid composition and point is not inside of the
3312 composition (in the case that the composition is from the current
3313 buffer), draw a glyph composed from the composition components. */
3314 if (find_composition (pos, -1, &pos, &end, &prop, string)
3315 && COMPOSITION_VALID_P (pos, end, prop)
3316 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
3317 {
3318 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
3319
3320 if (id >= 0)
3321 {
3322 it->method = next_element_from_composition;
3323 it->cmp_id = id;
3324 it->cmp_len = COMPOSITION_LENGTH (prop);
3325 /* For a terminal, draw only the first character of the
3326 components. */
3327 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
3328 it->len = (STRINGP (it->string)
3329 ? string_char_to_byte (it->string, end)
3330 : CHAR_TO_BYTE (end)) - pos_byte;
3331 it->stop_charpos = end;
3332 handled = HANDLED_RETURN;
3333 }
3334 }
3335
3336 return handled;
3337 }
3338
3339
3340 \f
3341 /***********************************************************************
3342 Overlay strings
3343 ***********************************************************************/
3344
3345 /* The following structure is used to record overlay strings for
3346 later sorting in load_overlay_strings. */
3347
3348 struct overlay_entry
3349 {
3350 Lisp_Object overlay;
3351 Lisp_Object string;
3352 int priority;
3353 int after_string_p;
3354 };
3355
3356
3357 /* Set up iterator IT from overlay strings at its current position.
3358 Called from handle_stop. */
3359
3360 static enum prop_handled
3361 handle_overlay_change (it)
3362 struct it *it;
3363 {
3364 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
3365 return HANDLED_RECOMPUTE_PROPS;
3366 else
3367 return HANDLED_NORMALLY;
3368 }
3369
3370
3371 /* Set up the next overlay string for delivery by IT, if there is an
3372 overlay string to deliver. Called by set_iterator_to_next when the
3373 end of the current overlay string is reached. If there are more
3374 overlay strings to display, IT->string and
3375 IT->current.overlay_string_index are set appropriately here.
3376 Otherwise IT->string is set to nil. */
3377
3378 static void
3379 next_overlay_string (it)
3380 struct it *it;
3381 {
3382 ++it->current.overlay_string_index;
3383 if (it->current.overlay_string_index == it->n_overlay_strings)
3384 {
3385 /* No more overlay strings. Restore IT's settings to what
3386 they were before overlay strings were processed, and
3387 continue to deliver from current_buffer. */
3388 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
3389
3390 pop_it (it);
3391 xassert (it->stop_charpos >= BEGV
3392 && it->stop_charpos <= it->end_charpos);
3393 it->string = Qnil;
3394 it->current.overlay_string_index = -1;
3395 SET_TEXT_POS (it->current.string_pos, -1, -1);
3396 it->n_overlay_strings = 0;
3397 it->method = next_element_from_buffer;
3398
3399 /* If we're at the end of the buffer, record that we have
3400 processed the overlay strings there already, so that
3401 next_element_from_buffer doesn't try it again. */
3402 if (IT_CHARPOS (*it) >= it->end_charpos)
3403 it->overlay_strings_at_end_processed_p = 1;
3404
3405 /* If we have to display `...' for invisible text, set
3406 the iterator up for that. */
3407 if (display_ellipsis_p)
3408 setup_for_ellipsis (it);
3409 }
3410 else
3411 {
3412 /* There are more overlay strings to process. If
3413 IT->current.overlay_string_index has advanced to a position
3414 where we must load IT->overlay_strings with more strings, do
3415 it. */
3416 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
3417
3418 if (it->current.overlay_string_index && i == 0)
3419 load_overlay_strings (it, 0);
3420
3421 /* Initialize IT to deliver display elements from the overlay
3422 string. */
3423 it->string = it->overlay_strings[i];
3424 it->multibyte_p = STRING_MULTIBYTE (it->string);
3425 SET_TEXT_POS (it->current.string_pos, 0, 0);
3426 it->method = next_element_from_string;
3427 it->stop_charpos = 0;
3428 }
3429
3430 CHECK_IT (it);
3431 }
3432
3433
3434 /* Compare two overlay_entry structures E1 and E2. Used as a
3435 comparison function for qsort in load_overlay_strings. Overlay
3436 strings for the same position are sorted so that
3437
3438 1. All after-strings come in front of before-strings, except
3439 when they come from the same overlay.
3440
3441 2. Within after-strings, strings are sorted so that overlay strings
3442 from overlays with higher priorities come first.
3443
3444 2. Within before-strings, strings are sorted so that overlay
3445 strings from overlays with higher priorities come last.
3446
3447 Value is analogous to strcmp. */
3448
3449
3450 static int
3451 compare_overlay_entries (e1, e2)
3452 void *e1, *e2;
3453 {
3454 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
3455 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
3456 int result;
3457
3458 if (entry1->after_string_p != entry2->after_string_p)
3459 {
3460 /* Let after-strings appear in front of before-strings if
3461 they come from different overlays. */
3462 if (EQ (entry1->overlay, entry2->overlay))
3463 result = entry1->after_string_p ? 1 : -1;
3464 else
3465 result = entry1->after_string_p ? -1 : 1;
3466 }
3467 else if (entry1->after_string_p)
3468 /* After-strings sorted in order of decreasing priority. */
3469 result = entry2->priority - entry1->priority;
3470 else
3471 /* Before-strings sorted in order of increasing priority. */
3472 result = entry1->priority - entry2->priority;
3473
3474 return result;
3475 }
3476
3477
3478 /* Load the vector IT->overlay_strings with overlay strings from IT's
3479 current buffer position, or from CHARPOS if that is > 0. Set
3480 IT->n_overlays to the total number of overlay strings found.
3481
3482 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
3483 a time. On entry into load_overlay_strings,
3484 IT->current.overlay_string_index gives the number of overlay
3485 strings that have already been loaded by previous calls to this
3486 function.
3487
3488 IT->add_overlay_start contains an additional overlay start
3489 position to consider for taking overlay strings from, if non-zero.
3490 This position comes into play when the overlay has an `invisible'
3491 property, and both before and after-strings. When we've skipped to
3492 the end of the overlay, because of its `invisible' property, we
3493 nevertheless want its before-string to appear.
3494 IT->add_overlay_start will contain the overlay start position
3495 in this case.
3496
3497 Overlay strings are sorted so that after-string strings come in
3498 front of before-string strings. Within before and after-strings,
3499 strings are sorted by overlay priority. See also function
3500 compare_overlay_entries. */
3501
3502 static void
3503 load_overlay_strings (it, charpos)
3504 struct it *it;
3505 int charpos;
3506 {
3507 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
3508 Lisp_Object ov, overlay, window, str, invisible;
3509 int start, end;
3510 int size = 20;
3511 int n = 0, i, j, invis_p;
3512 struct overlay_entry *entries
3513 = (struct overlay_entry *) alloca (size * sizeof *entries);
3514
3515 if (charpos <= 0)
3516 charpos = IT_CHARPOS (*it);
3517
3518 /* Append the overlay string STRING of overlay OVERLAY to vector
3519 `entries' which has size `size' and currently contains `n'
3520 elements. AFTER_P non-zero means STRING is an after-string of
3521 OVERLAY. */
3522 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
3523 do \
3524 { \
3525 Lisp_Object priority; \
3526 \
3527 if (n == size) \
3528 { \
3529 int new_size = 2 * size; \
3530 struct overlay_entry *old = entries; \
3531 entries = \
3532 (struct overlay_entry *) alloca (new_size \
3533 * sizeof *entries); \
3534 bcopy (old, entries, size * sizeof *entries); \
3535 size = new_size; \
3536 } \
3537 \
3538 entries[n].string = (STRING); \
3539 entries[n].overlay = (OVERLAY); \
3540 priority = Foverlay_get ((OVERLAY), Qpriority); \
3541 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
3542 entries[n].after_string_p = (AFTER_P); \
3543 ++n; \
3544 } \
3545 while (0)
3546
3547 /* Process overlay before the overlay center. */
3548 for (ov = current_buffer->overlays_before; CONSP (ov); ov = XCDR (ov))
3549 {
3550 overlay = XCAR (ov);
3551 xassert (OVERLAYP (overlay));
3552 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3553 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3554
3555 if (end < charpos)
3556 break;
3557
3558 /* Skip this overlay if it doesn't start or end at IT's current
3559 position. */
3560 if (end != charpos && start != charpos)
3561 continue;
3562
3563 /* Skip this overlay if it doesn't apply to IT->w. */
3564 window = Foverlay_get (overlay, Qwindow);
3565 if (WINDOWP (window) && XWINDOW (window) != it->w)
3566 continue;
3567
3568 /* If the text ``under'' the overlay is invisible, both before-
3569 and after-strings from this overlay are visible; start and
3570 end position are indistinguishable. */
3571 invisible = Foverlay_get (overlay, Qinvisible);
3572 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3573
3574 /* If overlay has a non-empty before-string, record it. */
3575 if ((start == charpos || (end == charpos && invis_p))
3576 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3577 && XSTRING (str)->size)
3578 RECORD_OVERLAY_STRING (overlay, str, 0);
3579
3580 /* If overlay has a non-empty after-string, record it. */
3581 if ((end == charpos || (start == charpos && invis_p))
3582 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3583 && XSTRING (str)->size)
3584 RECORD_OVERLAY_STRING (overlay, str, 1);
3585 }
3586
3587 /* Process overlays after the overlay center. */
3588 for (ov = current_buffer->overlays_after; CONSP (ov); ov = XCDR (ov))
3589 {
3590 overlay = XCAR (ov);
3591 xassert (OVERLAYP (overlay));
3592 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3593 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3594
3595 if (start > charpos)
3596 break;
3597
3598 /* Skip this overlay if it doesn't start or end at IT's current
3599 position. */
3600 if (end != charpos && start != charpos)
3601 continue;
3602
3603 /* Skip this overlay if it doesn't apply to IT->w. */
3604 window = Foverlay_get (overlay, Qwindow);
3605 if (WINDOWP (window) && XWINDOW (window) != it->w)
3606 continue;
3607
3608 /* If the text ``under'' the overlay is invisible, it has a zero
3609 dimension, and both before- and after-strings apply. */
3610 invisible = Foverlay_get (overlay, Qinvisible);
3611 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3612
3613 /* If overlay has a non-empty before-string, record it. */
3614 if ((start == charpos || (end == charpos && invis_p))
3615 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3616 && XSTRING (str)->size)
3617 RECORD_OVERLAY_STRING (overlay, str, 0);
3618
3619 /* If overlay has a non-empty after-string, record it. */
3620 if ((end == charpos || (start == charpos && invis_p))
3621 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3622 && XSTRING (str)->size)
3623 RECORD_OVERLAY_STRING (overlay, str, 1);
3624 }
3625
3626 #undef RECORD_OVERLAY_STRING
3627
3628 /* Sort entries. */
3629 if (n > 1)
3630 qsort (entries, n, sizeof *entries, compare_overlay_entries);
3631
3632 /* Record the total number of strings to process. */
3633 it->n_overlay_strings = n;
3634
3635 /* IT->current.overlay_string_index is the number of overlay strings
3636 that have already been consumed by IT. Copy some of the
3637 remaining overlay strings to IT->overlay_strings. */
3638 i = 0;
3639 j = it->current.overlay_string_index;
3640 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
3641 it->overlay_strings[i++] = entries[j++].string;
3642
3643 CHECK_IT (it);
3644 }
3645
3646
3647 /* Get the first chunk of overlay strings at IT's current buffer
3648 position, or at CHARPOS if that is > 0. Value is non-zero if at
3649 least one overlay string was found. */
3650
3651 static int
3652 get_overlay_strings (it, charpos)
3653 struct it *it;
3654 int charpos;
3655 {
3656 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
3657 process. This fills IT->overlay_strings with strings, and sets
3658 IT->n_overlay_strings to the total number of strings to process.
3659 IT->pos.overlay_string_index has to be set temporarily to zero
3660 because load_overlay_strings needs this; it must be set to -1
3661 when no overlay strings are found because a zero value would
3662 indicate a position in the first overlay string. */
3663 it->current.overlay_string_index = 0;
3664 load_overlay_strings (it, charpos);
3665
3666 /* If we found overlay strings, set up IT to deliver display
3667 elements from the first one. Otherwise set up IT to deliver
3668 from current_buffer. */
3669 if (it->n_overlay_strings)
3670 {
3671 /* Make sure we know settings in current_buffer, so that we can
3672 restore meaningful values when we're done with the overlay
3673 strings. */
3674 compute_stop_pos (it);
3675 xassert (it->face_id >= 0);
3676
3677 /* Save IT's settings. They are restored after all overlay
3678 strings have been processed. */
3679 xassert (it->sp == 0);
3680 push_it (it);
3681
3682 /* Set up IT to deliver display elements from the first overlay
3683 string. */
3684 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3685 it->string = it->overlay_strings[0];
3686 it->stop_charpos = 0;
3687 it->end_charpos = XSTRING (it->string)->size;
3688 it->multibyte_p = STRING_MULTIBYTE (it->string);
3689 xassert (STRINGP (it->string));
3690 it->method = next_element_from_string;
3691 }
3692 else
3693 {
3694 it->string = Qnil;
3695 it->current.overlay_string_index = -1;
3696 it->method = next_element_from_buffer;
3697 }
3698
3699 CHECK_IT (it);
3700
3701 /* Value is non-zero if we found at least one overlay string. */
3702 return STRINGP (it->string);
3703 }
3704
3705
3706 \f
3707 /***********************************************************************
3708 Saving and restoring state
3709 ***********************************************************************/
3710
3711 /* Save current settings of IT on IT->stack. Called, for example,
3712 before setting up IT for an overlay string, to be able to restore
3713 IT's settings to what they were after the overlay string has been
3714 processed. */
3715
3716 static void
3717 push_it (it)
3718 struct it *it;
3719 {
3720 struct iterator_stack_entry *p;
3721
3722 xassert (it->sp < 2);
3723 p = it->stack + it->sp;
3724
3725 p->stop_charpos = it->stop_charpos;
3726 xassert (it->face_id >= 0);
3727 p->face_id = it->face_id;
3728 p->string = it->string;
3729 p->pos = it->current;
3730 p->end_charpos = it->end_charpos;
3731 p->string_nchars = it->string_nchars;
3732 p->area = it->area;
3733 p->multibyte_p = it->multibyte_p;
3734 p->space_width = it->space_width;
3735 p->font_height = it->font_height;
3736 p->voffset = it->voffset;
3737 p->string_from_display_prop_p = it->string_from_display_prop_p;
3738 p->display_ellipsis_p = 0;
3739 ++it->sp;
3740 }
3741
3742
3743 /* Restore IT's settings from IT->stack. Called, for example, when no
3744 more overlay strings must be processed, and we return to delivering
3745 display elements from a buffer, or when the end of a string from a
3746 `display' property is reached and we return to delivering display
3747 elements from an overlay string, or from a buffer. */
3748
3749 static void
3750 pop_it (it)
3751 struct it *it;
3752 {
3753 struct iterator_stack_entry *p;
3754
3755 xassert (it->sp > 0);
3756 --it->sp;
3757 p = it->stack + it->sp;
3758 it->stop_charpos = p->stop_charpos;
3759 it->face_id = p->face_id;
3760 it->string = p->string;
3761 it->current = p->pos;
3762 it->end_charpos = p->end_charpos;
3763 it->string_nchars = p->string_nchars;
3764 it->area = p->area;
3765 it->multibyte_p = p->multibyte_p;
3766 it->space_width = p->space_width;
3767 it->font_height = p->font_height;
3768 it->voffset = p->voffset;
3769 it->string_from_display_prop_p = p->string_from_display_prop_p;
3770 }
3771
3772
3773 \f
3774 /***********************************************************************
3775 Moving over lines
3776 ***********************************************************************/
3777
3778 /* Set IT's current position to the previous line start. */
3779
3780 static void
3781 back_to_previous_line_start (it)
3782 struct it *it;
3783 {
3784 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
3785 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3786 }
3787
3788
3789 /* Move IT to the next line start.
3790
3791 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
3792 we skipped over part of the text (as opposed to moving the iterator
3793 continuously over the text). Otherwise, don't change the value
3794 of *SKIPPED_P.
3795
3796 Newlines may come from buffer text, overlay strings, or strings
3797 displayed via the `display' property. That's the reason we can't
3798 simply use find_next_newline_no_quit.
3799
3800 Note that this function may not skip over invisible text that is so
3801 because of text properties and immediately follows a newline. If
3802 it would, function reseat_at_next_visible_line_start, when called
3803 from set_iterator_to_next, would effectively make invisible
3804 characters following a newline part of the wrong glyph row, which
3805 leads to wrong cursor motion. */
3806
3807 static int
3808 forward_to_next_line_start (it, skipped_p)
3809 struct it *it;
3810 int *skipped_p;
3811 {
3812 int old_selective, newline_found_p, n;
3813 const int MAX_NEWLINE_DISTANCE = 500;
3814
3815 /* If already on a newline, just consume it to avoid unintended
3816 skipping over invisible text below. */
3817 if (it->what == IT_CHARACTER
3818 && it->c == '\n'
3819 && CHARPOS (it->position) == IT_CHARPOS (*it))
3820 {
3821 set_iterator_to_next (it, 0);
3822 it->c = 0;
3823 return 1;
3824 }
3825
3826 /* Don't handle selective display in the following. It's (a)
3827 unnecessary because it's done by the caller, and (b) leads to an
3828 infinite recursion because next_element_from_ellipsis indirectly
3829 calls this function. */
3830 old_selective = it->selective;
3831 it->selective = 0;
3832
3833 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
3834 from buffer text. */
3835 for (n = newline_found_p = 0;
3836 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
3837 n += STRINGP (it->string) ? 0 : 1)
3838 {
3839 if (!get_next_display_element (it))
3840 break;
3841 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
3842 set_iterator_to_next (it, 0);
3843 }
3844
3845 /* If we didn't find a newline near enough, see if we can use a
3846 short-cut. */
3847 if (n == MAX_NEWLINE_DISTANCE)
3848 {
3849 int start = IT_CHARPOS (*it);
3850 int limit = find_next_newline_no_quit (start, 1);
3851 Lisp_Object pos;
3852
3853 xassert (!STRINGP (it->string));
3854
3855 /* If there isn't any `display' property in sight, and no
3856 overlays, we can just use the position of the newline in
3857 buffer text. */
3858 if (it->stop_charpos >= limit
3859 || ((pos = Fnext_single_property_change (make_number (start),
3860 Qdisplay,
3861 Qnil, make_number (limit)),
3862 NILP (pos))
3863 && next_overlay_change (start) == ZV))
3864 {
3865 IT_CHARPOS (*it) = limit;
3866 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
3867 *skipped_p = newline_found_p = 1;
3868 }
3869 else
3870 {
3871 while (get_next_display_element (it)
3872 && !newline_found_p)
3873 {
3874 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
3875 set_iterator_to_next (it, 0);
3876 }
3877 }
3878 }
3879
3880 it->selective = old_selective;
3881 return newline_found_p;
3882 }
3883
3884
3885 /* Set IT's current position to the previous visible line start. Skip
3886 invisible text that is so either due to text properties or due to
3887 selective display. Caution: this does not change IT->current_x and
3888 IT->hpos. */
3889
3890 static void
3891 back_to_previous_visible_line_start (it)
3892 struct it *it;
3893 {
3894 int visible_p = 0;
3895
3896 /* Go back one newline if not on BEGV already. */
3897 if (IT_CHARPOS (*it) > BEGV)
3898 back_to_previous_line_start (it);
3899
3900 /* Move over lines that are invisible because of selective display
3901 or text properties. */
3902 while (IT_CHARPOS (*it) > BEGV
3903 && !visible_p)
3904 {
3905 visible_p = 1;
3906
3907 /* If selective > 0, then lines indented more than that values
3908 are invisible. */
3909 if (it->selective > 0
3910 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3911 it->selective))
3912 visible_p = 0;
3913 else
3914 {
3915 Lisp_Object prop;
3916
3917 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
3918 Qinvisible, it->window);
3919 if (TEXT_PROP_MEANS_INVISIBLE (prop))
3920 visible_p = 0;
3921 }
3922
3923 /* Back one more newline if the current one is invisible. */
3924 if (!visible_p)
3925 back_to_previous_line_start (it);
3926 }
3927
3928 xassert (IT_CHARPOS (*it) >= BEGV);
3929 xassert (IT_CHARPOS (*it) == BEGV
3930 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3931 CHECK_IT (it);
3932 }
3933
3934
3935 /* Reseat iterator IT at the previous visible line start. Skip
3936 invisible text that is so either due to text properties or due to
3937 selective display. At the end, update IT's overlay information,
3938 face information etc. */
3939
3940 static void
3941 reseat_at_previous_visible_line_start (it)
3942 struct it *it;
3943 {
3944 back_to_previous_visible_line_start (it);
3945 reseat (it, it->current.pos, 1);
3946 CHECK_IT (it);
3947 }
3948
3949
3950 /* Reseat iterator IT on the next visible line start in the current
3951 buffer. ON_NEWLINE_P non-zero means position IT on the newline
3952 preceding the line start. Skip over invisible text that is so
3953 because of selective display. Compute faces, overlays etc at the
3954 new position. Note that this function does not skip over text that
3955 is invisible because of text properties. */
3956
3957 static void
3958 reseat_at_next_visible_line_start (it, on_newline_p)
3959 struct it *it;
3960 int on_newline_p;
3961 {
3962 int newline_found_p, skipped_p = 0;
3963
3964 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3965
3966 /* Skip over lines that are invisible because they are indented
3967 more than the value of IT->selective. */
3968 if (it->selective > 0)
3969 while (IT_CHARPOS (*it) < ZV
3970 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3971 it->selective))
3972 {
3973 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3974 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3975 }
3976
3977 /* Position on the newline if that's what's requested. */
3978 if (on_newline_p && newline_found_p)
3979 {
3980 if (STRINGP (it->string))
3981 {
3982 if (IT_STRING_CHARPOS (*it) > 0)
3983 {
3984 --IT_STRING_CHARPOS (*it);
3985 --IT_STRING_BYTEPOS (*it);
3986 }
3987 }
3988 else if (IT_CHARPOS (*it) > BEGV)
3989 {
3990 --IT_CHARPOS (*it);
3991 --IT_BYTEPOS (*it);
3992 reseat (it, it->current.pos, 0);
3993 }
3994 }
3995 else if (skipped_p)
3996 reseat (it, it->current.pos, 0);
3997
3998 CHECK_IT (it);
3999 }
4000
4001
4002 \f
4003 /***********************************************************************
4004 Changing an iterator's position
4005 ***********************************************************************/
4006
4007 /* Change IT's current position to POS in current_buffer. If FORCE_P
4008 is non-zero, always check for text properties at the new position.
4009 Otherwise, text properties are only looked up if POS >=
4010 IT->check_charpos of a property. */
4011
4012 static void
4013 reseat (it, pos, force_p)
4014 struct it *it;
4015 struct text_pos pos;
4016 int force_p;
4017 {
4018 int original_pos = IT_CHARPOS (*it);
4019
4020 reseat_1 (it, pos, 0);
4021
4022 /* Determine where to check text properties. Avoid doing it
4023 where possible because text property lookup is very expensive. */
4024 if (force_p
4025 || CHARPOS (pos) > it->stop_charpos
4026 || CHARPOS (pos) < original_pos)
4027 handle_stop (it);
4028
4029 CHECK_IT (it);
4030 }
4031
4032
4033 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4034 IT->stop_pos to POS, also. */
4035
4036 static void
4037 reseat_1 (it, pos, set_stop_p)
4038 struct it *it;
4039 struct text_pos pos;
4040 int set_stop_p;
4041 {
4042 /* Don't call this function when scanning a C string. */
4043 xassert (it->s == NULL);
4044
4045 /* POS must be a reasonable value. */
4046 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4047
4048 it->current.pos = it->position = pos;
4049 XSETBUFFER (it->object, current_buffer);
4050 it->end_charpos = ZV;
4051 it->dpvec = NULL;
4052 it->current.dpvec_index = -1;
4053 it->current.overlay_string_index = -1;
4054 IT_STRING_CHARPOS (*it) = -1;
4055 IT_STRING_BYTEPOS (*it) = -1;
4056 it->string = Qnil;
4057 it->method = next_element_from_buffer;
4058 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
4059 it->sp = 0;
4060 it->face_before_selective_p = 0;
4061
4062 if (set_stop_p)
4063 it->stop_charpos = CHARPOS (pos);
4064 }
4065
4066
4067 /* Set up IT for displaying a string, starting at CHARPOS in window W.
4068 If S is non-null, it is a C string to iterate over. Otherwise,
4069 STRING gives a Lisp string to iterate over.
4070
4071 If PRECISION > 0, don't return more then PRECISION number of
4072 characters from the string.
4073
4074 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4075 characters have been returned. FIELD_WIDTH < 0 means an infinite
4076 field width.
4077
4078 MULTIBYTE = 0 means disable processing of multibyte characters,
4079 MULTIBYTE > 0 means enable it,
4080 MULTIBYTE < 0 means use IT->multibyte_p.
4081
4082 IT must be initialized via a prior call to init_iterator before
4083 calling this function. */
4084
4085 static void
4086 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4087 struct it *it;
4088 unsigned char *s;
4089 Lisp_Object string;
4090 int charpos;
4091 int precision, field_width, multibyte;
4092 {
4093 /* No region in strings. */
4094 it->region_beg_charpos = it->region_end_charpos = -1;
4095
4096 /* No text property checks performed by default, but see below. */
4097 it->stop_charpos = -1;
4098
4099 /* Set iterator position and end position. */
4100 bzero (&it->current, sizeof it->current);
4101 it->current.overlay_string_index = -1;
4102 it->current.dpvec_index = -1;
4103 xassert (charpos >= 0);
4104
4105 /* If STRING is specified, use its multibyteness, otherwise use the
4106 setting of MULTIBYTE, if specified. */
4107 if (multibyte >= 0)
4108 it->multibyte_p = multibyte > 0;
4109
4110 if (s == NULL)
4111 {
4112 xassert (STRINGP (string));
4113 it->string = string;
4114 it->s = NULL;
4115 it->end_charpos = it->string_nchars = XSTRING (string)->size;
4116 it->method = next_element_from_string;
4117 it->current.string_pos = string_pos (charpos, string);
4118 }
4119 else
4120 {
4121 it->s = s;
4122 it->string = Qnil;
4123
4124 /* Note that we use IT->current.pos, not it->current.string_pos,
4125 for displaying C strings. */
4126 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4127 if (it->multibyte_p)
4128 {
4129 it->current.pos = c_string_pos (charpos, s, 1);
4130 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4131 }
4132 else
4133 {
4134 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4135 it->end_charpos = it->string_nchars = strlen (s);
4136 }
4137
4138 it->method = next_element_from_c_string;
4139 }
4140
4141 /* PRECISION > 0 means don't return more than PRECISION characters
4142 from the string. */
4143 if (precision > 0 && it->end_charpos - charpos > precision)
4144 it->end_charpos = it->string_nchars = charpos + precision;
4145
4146 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4147 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4148 FIELD_WIDTH < 0 means infinite field width. This is useful for
4149 padding with `-' at the end of a mode line. */
4150 if (field_width < 0)
4151 field_width = INFINITY;
4152 if (field_width > it->end_charpos - charpos)
4153 it->end_charpos = charpos + field_width;
4154
4155 /* Use the standard display table for displaying strings. */
4156 if (DISP_TABLE_P (Vstandard_display_table))
4157 it->dp = XCHAR_TABLE (Vstandard_display_table);
4158
4159 it->stop_charpos = charpos;
4160 CHECK_IT (it);
4161 }
4162
4163
4164 \f
4165 /***********************************************************************
4166 Iteration
4167 ***********************************************************************/
4168
4169 /* Load IT's display element fields with information about the next
4170 display element from the current position of IT. Value is zero if
4171 end of buffer (or C string) is reached. */
4172
4173 int
4174 get_next_display_element (it)
4175 struct it *it;
4176 {
4177 /* Non-zero means that we found an display element. Zero means that
4178 we hit the end of what we iterate over. Performance note: the
4179 function pointer `method' used here turns out to be faster than
4180 using a sequence of if-statements. */
4181 int success_p = (*it->method) (it);
4182
4183 if (it->what == IT_CHARACTER)
4184 {
4185 /* Map via display table or translate control characters.
4186 IT->c, IT->len etc. have been set to the next character by
4187 the function call above. If we have a display table, and it
4188 contains an entry for IT->c, translate it. Don't do this if
4189 IT->c itself comes from a display table, otherwise we could
4190 end up in an infinite recursion. (An alternative could be to
4191 count the recursion depth of this function and signal an
4192 error when a certain maximum depth is reached.) Is it worth
4193 it? */
4194 if (success_p && it->dpvec == NULL)
4195 {
4196 Lisp_Object dv;
4197
4198 if (it->dp
4199 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
4200 VECTORP (dv)))
4201 {
4202 struct Lisp_Vector *v = XVECTOR (dv);
4203
4204 /* Return the first character from the display table
4205 entry, if not empty. If empty, don't display the
4206 current character. */
4207 if (v->size)
4208 {
4209 it->dpvec_char_len = it->len;
4210 it->dpvec = v->contents;
4211 it->dpend = v->contents + v->size;
4212 it->current.dpvec_index = 0;
4213 it->method = next_element_from_display_vector;
4214 success_p = get_next_display_element (it);
4215 }
4216 else
4217 {
4218 set_iterator_to_next (it, 0);
4219 success_p = get_next_display_element (it);
4220 }
4221 }
4222
4223 /* Translate control characters into `\003' or `^C' form.
4224 Control characters coming from a display table entry are
4225 currently not translated because we use IT->dpvec to hold
4226 the translation. This could easily be changed but I
4227 don't believe that it is worth doing.
4228
4229 Non-printable multibyte characters are also translated
4230 octal form. */
4231 else if ((it->c < ' '
4232 && (it->area != TEXT_AREA
4233 || (it->c != '\n' && it->c != '\t')))
4234 || (it->c >= 127
4235 && it->len == 1)
4236 || !CHAR_PRINTABLE_P (it->c))
4237 {
4238 /* IT->c is a control character which must be displayed
4239 either as '\003' or as `^C' where the '\\' and '^'
4240 can be defined in the display table. Fill
4241 IT->ctl_chars with glyphs for what we have to
4242 display. Then, set IT->dpvec to these glyphs. */
4243 GLYPH g;
4244
4245 if (it->c < 128 && it->ctl_arrow_p)
4246 {
4247 /* Set IT->ctl_chars[0] to the glyph for `^'. */
4248 if (it->dp
4249 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
4250 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
4251 g = XINT (DISP_CTRL_GLYPH (it->dp));
4252 else
4253 g = FAST_MAKE_GLYPH ('^', 0);
4254 XSETINT (it->ctl_chars[0], g);
4255
4256 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
4257 XSETINT (it->ctl_chars[1], g);
4258
4259 /* Set up IT->dpvec and return first character from it. */
4260 it->dpvec_char_len = it->len;
4261 it->dpvec = it->ctl_chars;
4262 it->dpend = it->dpvec + 2;
4263 it->current.dpvec_index = 0;
4264 it->method = next_element_from_display_vector;
4265 get_next_display_element (it);
4266 }
4267 else
4268 {
4269 unsigned char str[MAX_MULTIBYTE_LENGTH];
4270 int len;
4271 int i;
4272 GLYPH escape_glyph;
4273
4274 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
4275 if (it->dp
4276 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
4277 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
4278 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
4279 else
4280 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
4281
4282 if (SINGLE_BYTE_CHAR_P (it->c))
4283 str[0] = it->c, len = 1;
4284 else
4285 {
4286 len = CHAR_STRING_NO_SIGNAL (it->c, str);
4287 if (len < 0)
4288 {
4289 /* It's an invalid character, which
4290 shouldn't happen actually, but due to
4291 bugs it may happen. Let's print the char
4292 as is, there's not much meaningful we can
4293 do with it. */
4294 str[0] = it->c;
4295 str[1] = it->c >> 8;
4296 str[2] = it->c >> 16;
4297 str[3] = it->c >> 24;
4298 len = 4;
4299 }
4300 }
4301
4302 for (i = 0; i < len; i++)
4303 {
4304 XSETINT (it->ctl_chars[i * 4], escape_glyph);
4305 /* Insert three more glyphs into IT->ctl_chars for
4306 the octal display of the character. */
4307 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
4308 XSETINT (it->ctl_chars[i * 4 + 1], g);
4309 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
4310 XSETINT (it->ctl_chars[i * 4 + 2], g);
4311 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
4312 XSETINT (it->ctl_chars[i * 4 + 3], g);
4313 }
4314
4315 /* Set up IT->dpvec and return the first character
4316 from it. */
4317 it->dpvec_char_len = it->len;
4318 it->dpvec = it->ctl_chars;
4319 it->dpend = it->dpvec + len * 4;
4320 it->current.dpvec_index = 0;
4321 it->method = next_element_from_display_vector;
4322 get_next_display_element (it);
4323 }
4324 }
4325 }
4326
4327 /* Adjust face id for a multibyte character. There are no
4328 multibyte character in unibyte text. */
4329 if (it->multibyte_p
4330 && success_p
4331 && FRAME_WINDOW_P (it->f))
4332 {
4333 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4334 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
4335 }
4336 }
4337
4338 /* Is this character the last one of a run of characters with
4339 box? If yes, set IT->end_of_box_run_p to 1. */
4340 if (it->face_box_p
4341 && it->s == NULL)
4342 {
4343 int face_id;
4344 struct face *face;
4345
4346 it->end_of_box_run_p
4347 = ((face_id = face_after_it_pos (it),
4348 face_id != it->face_id)
4349 && (face = FACE_FROM_ID (it->f, face_id),
4350 face->box == FACE_NO_BOX));
4351 }
4352
4353 /* Value is 0 if end of buffer or string reached. */
4354 return success_p;
4355 }
4356
4357
4358 /* Move IT to the next display element.
4359
4360 RESEAT_P non-zero means if called on a newline in buffer text,
4361 skip to the next visible line start.
4362
4363 Functions get_next_display_element and set_iterator_to_next are
4364 separate because I find this arrangement easier to handle than a
4365 get_next_display_element function that also increments IT's
4366 position. The way it is we can first look at an iterator's current
4367 display element, decide whether it fits on a line, and if it does,
4368 increment the iterator position. The other way around we probably
4369 would either need a flag indicating whether the iterator has to be
4370 incremented the next time, or we would have to implement a
4371 decrement position function which would not be easy to write. */
4372
4373 void
4374 set_iterator_to_next (it, reseat_p)
4375 struct it *it;
4376 int reseat_p;
4377 {
4378 /* Reset flags indicating start and end of a sequence of characters
4379 with box. Reset them at the start of this function because
4380 moving the iterator to a new position might set them. */
4381 it->start_of_box_run_p = it->end_of_box_run_p = 0;
4382
4383 if (it->method == next_element_from_buffer)
4384 {
4385 /* The current display element of IT is a character from
4386 current_buffer. Advance in the buffer, and maybe skip over
4387 invisible lines that are so because of selective display. */
4388 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
4389 reseat_at_next_visible_line_start (it, 0);
4390 else
4391 {
4392 xassert (it->len != 0);
4393 IT_BYTEPOS (*it) += it->len;
4394 IT_CHARPOS (*it) += 1;
4395 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
4396 }
4397 }
4398 else if (it->method == next_element_from_composition)
4399 {
4400 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
4401 if (STRINGP (it->string))
4402 {
4403 IT_STRING_BYTEPOS (*it) += it->len;
4404 IT_STRING_CHARPOS (*it) += it->cmp_len;
4405 it->method = next_element_from_string;
4406 goto consider_string_end;
4407 }
4408 else
4409 {
4410 IT_BYTEPOS (*it) += it->len;
4411 IT_CHARPOS (*it) += it->cmp_len;
4412 it->method = next_element_from_buffer;
4413 }
4414 }
4415 else if (it->method == next_element_from_c_string)
4416 {
4417 /* Current display element of IT is from a C string. */
4418 IT_BYTEPOS (*it) += it->len;
4419 IT_CHARPOS (*it) += 1;
4420 }
4421 else if (it->method == next_element_from_display_vector)
4422 {
4423 /* Current display element of IT is from a display table entry.
4424 Advance in the display table definition. Reset it to null if
4425 end reached, and continue with characters from buffers/
4426 strings. */
4427 ++it->current.dpvec_index;
4428
4429 /* Restore face of the iterator to what they were before the
4430 display vector entry (these entries may contain faces). */
4431 it->face_id = it->saved_face_id;
4432
4433 if (it->dpvec + it->current.dpvec_index == it->dpend)
4434 {
4435 if (it->s)
4436 it->method = next_element_from_c_string;
4437 else if (STRINGP (it->string))
4438 it->method = next_element_from_string;
4439 else
4440 it->method = next_element_from_buffer;
4441
4442 it->dpvec = NULL;
4443 it->current.dpvec_index = -1;
4444
4445 /* Skip over characters which were displayed via IT->dpvec. */
4446 if (it->dpvec_char_len < 0)
4447 reseat_at_next_visible_line_start (it, 1);
4448 else if (it->dpvec_char_len > 0)
4449 {
4450 it->len = it->dpvec_char_len;
4451 set_iterator_to_next (it, reseat_p);
4452 }
4453 }
4454 }
4455 else if (it->method == next_element_from_string)
4456 {
4457 /* Current display element is a character from a Lisp string. */
4458 xassert (it->s == NULL && STRINGP (it->string));
4459 IT_STRING_BYTEPOS (*it) += it->len;
4460 IT_STRING_CHARPOS (*it) += 1;
4461
4462 consider_string_end:
4463
4464 if (it->current.overlay_string_index >= 0)
4465 {
4466 /* IT->string is an overlay string. Advance to the
4467 next, if there is one. */
4468 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
4469 next_overlay_string (it);
4470 }
4471 else
4472 {
4473 /* IT->string is not an overlay string. If we reached
4474 its end, and there is something on IT->stack, proceed
4475 with what is on the stack. This can be either another
4476 string, this time an overlay string, or a buffer. */
4477 if (IT_STRING_CHARPOS (*it) == XSTRING (it->string)->size
4478 && it->sp > 0)
4479 {
4480 pop_it (it);
4481 if (!STRINGP (it->string))
4482 it->method = next_element_from_buffer;
4483 else
4484 goto consider_string_end;
4485 }
4486 }
4487 }
4488 else if (it->method == next_element_from_image
4489 || it->method == next_element_from_stretch)
4490 {
4491 /* The position etc with which we have to proceed are on
4492 the stack. The position may be at the end of a string,
4493 if the `display' property takes up the whole string. */
4494 pop_it (it);
4495 it->image_id = 0;
4496 if (STRINGP (it->string))
4497 {
4498 it->method = next_element_from_string;
4499 goto consider_string_end;
4500 }
4501 else
4502 it->method = next_element_from_buffer;
4503 }
4504 else
4505 /* There are no other methods defined, so this should be a bug. */
4506 abort ();
4507
4508 xassert (it->method != next_element_from_string
4509 || (STRINGP (it->string)
4510 && IT_STRING_CHARPOS (*it) >= 0));
4511 }
4512
4513
4514 /* Load IT's display element fields with information about the next
4515 display element which comes from a display table entry or from the
4516 result of translating a control character to one of the forms `^C'
4517 or `\003'. IT->dpvec holds the glyphs to return as characters. */
4518
4519 static int
4520 next_element_from_display_vector (it)
4521 struct it *it;
4522 {
4523 /* Precondition. */
4524 xassert (it->dpvec && it->current.dpvec_index >= 0);
4525
4526 /* Remember the current face id in case glyphs specify faces.
4527 IT's face is restored in set_iterator_to_next. */
4528 it->saved_face_id = it->face_id;
4529
4530 if (INTEGERP (*it->dpvec)
4531 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
4532 {
4533 int lface_id;
4534 GLYPH g;
4535
4536 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
4537 it->c = FAST_GLYPH_CHAR (g);
4538 it->len = CHAR_BYTES (it->c);
4539
4540 /* The entry may contain a face id to use. Such a face id is
4541 the id of a Lisp face, not a realized face. A face id of
4542 zero means no face is specified. */
4543 lface_id = FAST_GLYPH_FACE (g);
4544 if (lface_id)
4545 {
4546 /* The function returns -1 if lface_id is invalid. */
4547 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
4548 if (face_id >= 0)
4549 it->face_id = face_id;
4550 }
4551 }
4552 else
4553 /* Display table entry is invalid. Return a space. */
4554 it->c = ' ', it->len = 1;
4555
4556 /* Don't change position and object of the iterator here. They are
4557 still the values of the character that had this display table
4558 entry or was translated, and that's what we want. */
4559 it->what = IT_CHARACTER;
4560 return 1;
4561 }
4562
4563
4564 /* Load IT with the next display element from Lisp string IT->string.
4565 IT->current.string_pos is the current position within the string.
4566 If IT->current.overlay_string_index >= 0, the Lisp string is an
4567 overlay string. */
4568
4569 static int
4570 next_element_from_string (it)
4571 struct it *it;
4572 {
4573 struct text_pos position;
4574
4575 xassert (STRINGP (it->string));
4576 xassert (IT_STRING_CHARPOS (*it) >= 0);
4577 position = it->current.string_pos;
4578
4579 /* Time to check for invisible text? */
4580 if (IT_STRING_CHARPOS (*it) < it->end_charpos
4581 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
4582 {
4583 handle_stop (it);
4584
4585 /* Since a handler may have changed IT->method, we must
4586 recurse here. */
4587 return get_next_display_element (it);
4588 }
4589
4590 if (it->current.overlay_string_index >= 0)
4591 {
4592 /* Get the next character from an overlay string. In overlay
4593 strings, There is no field width or padding with spaces to
4594 do. */
4595 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
4596 {
4597 it->what = IT_EOB;
4598 return 0;
4599 }
4600 else if (STRING_MULTIBYTE (it->string))
4601 {
4602 int remaining = (STRING_BYTES (XSTRING (it->string))
4603 - IT_STRING_BYTEPOS (*it));
4604 unsigned char *s = (XSTRING (it->string)->data
4605 + IT_STRING_BYTEPOS (*it));
4606 it->c = string_char_and_length (s, remaining, &it->len);
4607 }
4608 else
4609 {
4610 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
4611 it->len = 1;
4612 }
4613 }
4614 else
4615 {
4616 /* Get the next character from a Lisp string that is not an
4617 overlay string. Such strings come from the mode line, for
4618 example. We may have to pad with spaces, or truncate the
4619 string. See also next_element_from_c_string. */
4620 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
4621 {
4622 it->what = IT_EOB;
4623 return 0;
4624 }
4625 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
4626 {
4627 /* Pad with spaces. */
4628 it->c = ' ', it->len = 1;
4629 CHARPOS (position) = BYTEPOS (position) = -1;
4630 }
4631 else if (STRING_MULTIBYTE (it->string))
4632 {
4633 int maxlen = (STRING_BYTES (XSTRING (it->string))
4634 - IT_STRING_BYTEPOS (*it));
4635 unsigned char *s = (XSTRING (it->string)->data
4636 + IT_STRING_BYTEPOS (*it));
4637 it->c = string_char_and_length (s, maxlen, &it->len);
4638 }
4639 else
4640 {
4641 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
4642 it->len = 1;
4643 }
4644 }
4645
4646 /* Record what we have and where it came from. Note that we store a
4647 buffer position in IT->position although it could arguably be a
4648 string position. */
4649 it->what = IT_CHARACTER;
4650 it->object = it->string;
4651 it->position = position;
4652 return 1;
4653 }
4654
4655
4656 /* Load IT with next display element from C string IT->s.
4657 IT->string_nchars is the maximum number of characters to return
4658 from the string. IT->end_charpos may be greater than
4659 IT->string_nchars when this function is called, in which case we
4660 may have to return padding spaces. Value is zero if end of string
4661 reached, including padding spaces. */
4662
4663 static int
4664 next_element_from_c_string (it)
4665 struct it *it;
4666 {
4667 int success_p = 1;
4668
4669 xassert (it->s);
4670 it->what = IT_CHARACTER;
4671 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
4672 it->object = Qnil;
4673
4674 /* IT's position can be greater IT->string_nchars in case a field
4675 width or precision has been specified when the iterator was
4676 initialized. */
4677 if (IT_CHARPOS (*it) >= it->end_charpos)
4678 {
4679 /* End of the game. */
4680 it->what = IT_EOB;
4681 success_p = 0;
4682 }
4683 else if (IT_CHARPOS (*it) >= it->string_nchars)
4684 {
4685 /* Pad with spaces. */
4686 it->c = ' ', it->len = 1;
4687 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
4688 }
4689 else if (it->multibyte_p)
4690 {
4691 /* Implementation note: The calls to strlen apparently aren't a
4692 performance problem because there is no noticeable performance
4693 difference between Emacs running in unibyte or multibyte mode. */
4694 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
4695 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
4696 maxlen, &it->len);
4697 }
4698 else
4699 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
4700
4701 return success_p;
4702 }
4703
4704
4705 /* Set up IT to return characters from an ellipsis, if appropriate.
4706 The definition of the ellipsis glyphs may come from a display table
4707 entry. This function Fills IT with the first glyph from the
4708 ellipsis if an ellipsis is to be displayed. */
4709
4710 static int
4711 next_element_from_ellipsis (it)
4712 struct it *it;
4713 {
4714 if (it->selective_display_ellipsis_p)
4715 {
4716 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4717 {
4718 /* Use the display table definition for `...'. Invalid glyphs
4719 will be handled by the method returning elements from dpvec. */
4720 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4721 it->dpvec_char_len = it->len;
4722 it->dpvec = v->contents;
4723 it->dpend = v->contents + v->size;
4724 it->current.dpvec_index = 0;
4725 it->method = next_element_from_display_vector;
4726 }
4727 else
4728 {
4729 /* Use default `...' which is stored in default_invis_vector. */
4730 it->dpvec_char_len = it->len;
4731 it->dpvec = default_invis_vector;
4732 it->dpend = default_invis_vector + 3;
4733 it->current.dpvec_index = 0;
4734 it->method = next_element_from_display_vector;
4735 }
4736 }
4737 else
4738 {
4739 /* The face at the current position may be different from the
4740 face we find after the invisible text. Remember what it
4741 was in IT->saved_face_id, and signal that it's there by
4742 setting face_before_selective_p. */
4743 it->saved_face_id = it->face_id;
4744 it->method = next_element_from_buffer;
4745 reseat_at_next_visible_line_start (it, 1);
4746 it->face_before_selective_p = 1;
4747 }
4748
4749 return get_next_display_element (it);
4750 }
4751
4752
4753 /* Deliver an image display element. The iterator IT is already
4754 filled with image information (done in handle_display_prop). Value
4755 is always 1. */
4756
4757
4758 static int
4759 next_element_from_image (it)
4760 struct it *it;
4761 {
4762 it->what = IT_IMAGE;
4763 return 1;
4764 }
4765
4766
4767 /* Fill iterator IT with next display element from a stretch glyph
4768 property. IT->object is the value of the text property. Value is
4769 always 1. */
4770
4771 static int
4772 next_element_from_stretch (it)
4773 struct it *it;
4774 {
4775 it->what = IT_STRETCH;
4776 return 1;
4777 }
4778
4779
4780 /* Load IT with the next display element from current_buffer. Value
4781 is zero if end of buffer reached. IT->stop_charpos is the next
4782 position at which to stop and check for text properties or buffer
4783 end. */
4784
4785 static int
4786 next_element_from_buffer (it)
4787 struct it *it;
4788 {
4789 int success_p = 1;
4790
4791 /* Check this assumption, otherwise, we would never enter the
4792 if-statement, below. */
4793 xassert (IT_CHARPOS (*it) >= BEGV
4794 && IT_CHARPOS (*it) <= it->stop_charpos);
4795
4796 if (IT_CHARPOS (*it) >= it->stop_charpos)
4797 {
4798 if (IT_CHARPOS (*it) >= it->end_charpos)
4799 {
4800 int overlay_strings_follow_p;
4801
4802 /* End of the game, except when overlay strings follow that
4803 haven't been returned yet. */
4804 if (it->overlay_strings_at_end_processed_p)
4805 overlay_strings_follow_p = 0;
4806 else
4807 {
4808 it->overlay_strings_at_end_processed_p = 1;
4809 overlay_strings_follow_p = get_overlay_strings (it, 0);
4810 }
4811
4812 if (overlay_strings_follow_p)
4813 success_p = get_next_display_element (it);
4814 else
4815 {
4816 it->what = IT_EOB;
4817 it->position = it->current.pos;
4818 success_p = 0;
4819 }
4820 }
4821 else
4822 {
4823 handle_stop (it);
4824 return get_next_display_element (it);
4825 }
4826 }
4827 else
4828 {
4829 /* No face changes, overlays etc. in sight, so just return a
4830 character from current_buffer. */
4831 unsigned char *p;
4832
4833 /* Maybe run the redisplay end trigger hook. Performance note:
4834 This doesn't seem to cost measurable time. */
4835 if (it->redisplay_end_trigger_charpos
4836 && it->glyph_row
4837 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
4838 run_redisplay_end_trigger_hook (it);
4839
4840 /* Get the next character, maybe multibyte. */
4841 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
4842 if (it->multibyte_p && !ASCII_BYTE_P (*p))
4843 {
4844 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
4845 - IT_BYTEPOS (*it));
4846 it->c = string_char_and_length (p, maxlen, &it->len);
4847 }
4848 else
4849 it->c = *p, it->len = 1;
4850
4851 /* Record what we have and where it came from. */
4852 it->what = IT_CHARACTER;;
4853 it->object = it->w->buffer;
4854 it->position = it->current.pos;
4855
4856 /* Normally we return the character found above, except when we
4857 really want to return an ellipsis for selective display. */
4858 if (it->selective)
4859 {
4860 if (it->c == '\n')
4861 {
4862 /* A value of selective > 0 means hide lines indented more
4863 than that number of columns. */
4864 if (it->selective > 0
4865 && IT_CHARPOS (*it) + 1 < ZV
4866 && indented_beyond_p (IT_CHARPOS (*it) + 1,
4867 IT_BYTEPOS (*it) + 1,
4868 it->selective))
4869 {
4870 success_p = next_element_from_ellipsis (it);
4871 it->dpvec_char_len = -1;
4872 }
4873 }
4874 else if (it->c == '\r' && it->selective == -1)
4875 {
4876 /* A value of selective == -1 means that everything from the
4877 CR to the end of the line is invisible, with maybe an
4878 ellipsis displayed for it. */
4879 success_p = next_element_from_ellipsis (it);
4880 it->dpvec_char_len = -1;
4881 }
4882 }
4883 }
4884
4885 /* Value is zero if end of buffer reached. */
4886 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
4887 return success_p;
4888 }
4889
4890
4891 /* Run the redisplay end trigger hook for IT. */
4892
4893 static void
4894 run_redisplay_end_trigger_hook (it)
4895 struct it *it;
4896 {
4897 Lisp_Object args[3];
4898
4899 /* IT->glyph_row should be non-null, i.e. we should be actually
4900 displaying something, or otherwise we should not run the hook. */
4901 xassert (it->glyph_row);
4902
4903 /* Set up hook arguments. */
4904 args[0] = Qredisplay_end_trigger_functions;
4905 args[1] = it->window;
4906 XSETINT (args[2], it->redisplay_end_trigger_charpos);
4907 it->redisplay_end_trigger_charpos = 0;
4908
4909 /* Since we are *trying* to run these functions, don't try to run
4910 them again, even if they get an error. */
4911 it->w->redisplay_end_trigger = Qnil;
4912 Frun_hook_with_args (3, args);
4913
4914 /* Notice if it changed the face of the character we are on. */
4915 handle_face_prop (it);
4916 }
4917
4918
4919 /* Deliver a composition display element. The iterator IT is already
4920 filled with composition information (done in
4921 handle_composition_prop). Value is always 1. */
4922
4923 static int
4924 next_element_from_composition (it)
4925 struct it *it;
4926 {
4927 it->what = IT_COMPOSITION;
4928 it->position = (STRINGP (it->string)
4929 ? it->current.string_pos
4930 : it->current.pos);
4931 return 1;
4932 }
4933
4934
4935 \f
4936 /***********************************************************************
4937 Moving an iterator without producing glyphs
4938 ***********************************************************************/
4939
4940 /* Move iterator IT to a specified buffer or X position within one
4941 line on the display without producing glyphs.
4942
4943 Begin to skip at IT's current position. Skip to TO_CHARPOS or TO_X
4944 whichever is reached first.
4945
4946 TO_CHARPOS <= 0 means no TO_CHARPOS is specified.
4947
4948 TO_X < 0 means that no TO_X is specified. TO_X is normally a value
4949 0 <= TO_X <= IT->last_visible_x. This means in particular, that
4950 TO_X includes the amount by which a window is horizontally
4951 scrolled.
4952
4953 Value is
4954
4955 MOVE_POS_MATCH_OR_ZV
4956 - when TO_POS or ZV was reached.
4957
4958 MOVE_X_REACHED
4959 -when TO_X was reached before TO_POS or ZV were reached.
4960
4961 MOVE_LINE_CONTINUED
4962 - when we reached the end of the display area and the line must
4963 be continued.
4964
4965 MOVE_LINE_TRUNCATED
4966 - when we reached the end of the display area and the line is
4967 truncated.
4968
4969 MOVE_NEWLINE_OR_CR
4970 - when we stopped at a line end, i.e. a newline or a CR and selective
4971 display is on. */
4972
4973 static enum move_it_result
4974 move_it_in_display_line_to (it, to_charpos, to_x, op)
4975 struct it *it;
4976 int to_charpos, to_x, op;
4977 {
4978 enum move_it_result result = MOVE_UNDEFINED;
4979 struct glyph_row *saved_glyph_row;
4980
4981 /* Don't produce glyphs in produce_glyphs. */
4982 saved_glyph_row = it->glyph_row;
4983 it->glyph_row = NULL;
4984
4985 while (1)
4986 {
4987 int x, i, ascent = 0, descent = 0;
4988
4989 /* Stop when ZV or TO_CHARPOS reached. */
4990 if (!get_next_display_element (it)
4991 || ((op & MOVE_TO_POS) != 0
4992 && BUFFERP (it->object)
4993 && IT_CHARPOS (*it) >= to_charpos))
4994 {
4995 result = MOVE_POS_MATCH_OR_ZV;
4996 break;
4997 }
4998
4999 /* The call to produce_glyphs will get the metrics of the
5000 display element IT is loaded with. We record in x the
5001 x-position before this display element in case it does not
5002 fit on the line. */
5003 x = it->current_x;
5004
5005 /* Remember the line height so far in case the next element doesn't
5006 fit on the line. */
5007 if (!it->truncate_lines_p)
5008 {
5009 ascent = it->max_ascent;
5010 descent = it->max_descent;
5011 }
5012
5013 PRODUCE_GLYPHS (it);
5014
5015 if (it->area != TEXT_AREA)
5016 {
5017 set_iterator_to_next (it, 1);
5018 continue;
5019 }
5020
5021 /* The number of glyphs we get back in IT->nglyphs will normally
5022 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
5023 character on a terminal frame, or (iii) a line end. For the
5024 second case, IT->nglyphs - 1 padding glyphs will be present
5025 (on X frames, there is only one glyph produced for a
5026 composite character.
5027
5028 The behavior implemented below means, for continuation lines,
5029 that as many spaces of a TAB as fit on the current line are
5030 displayed there. For terminal frames, as many glyphs of a
5031 multi-glyph character are displayed in the current line, too.
5032 This is what the old redisplay code did, and we keep it that
5033 way. Under X, the whole shape of a complex character must
5034 fit on the line or it will be completely displayed in the
5035 next line.
5036
5037 Note that both for tabs and padding glyphs, all glyphs have
5038 the same width. */
5039 if (it->nglyphs)
5040 {
5041 /* More than one glyph or glyph doesn't fit on line. All
5042 glyphs have the same width. */
5043 int single_glyph_width = it->pixel_width / it->nglyphs;
5044 int new_x;
5045
5046 for (i = 0; i < it->nglyphs; ++i, x = new_x)
5047 {
5048 new_x = x + single_glyph_width;
5049
5050 /* We want to leave anything reaching TO_X to the caller. */
5051 if ((op & MOVE_TO_X) && new_x > to_x)
5052 {
5053 it->current_x = x;
5054 result = MOVE_X_REACHED;
5055 break;
5056 }
5057 else if (/* Lines are continued. */
5058 !it->truncate_lines_p
5059 && (/* And glyph doesn't fit on the line. */
5060 new_x > it->last_visible_x
5061 /* Or it fits exactly and we're on a window
5062 system frame. */
5063 || (new_x == it->last_visible_x
5064 && FRAME_WINDOW_P (it->f))))
5065 {
5066 if (/* IT->hpos == 0 means the very first glyph
5067 doesn't fit on the line, e.g. a wide image. */
5068 it->hpos == 0
5069 || (new_x == it->last_visible_x
5070 && FRAME_WINDOW_P (it->f)))
5071 {
5072 ++it->hpos;
5073 it->current_x = new_x;
5074 if (i == it->nglyphs - 1)
5075 set_iterator_to_next (it, 1);
5076 }
5077 else
5078 {
5079 it->current_x = x;
5080 it->max_ascent = ascent;
5081 it->max_descent = descent;
5082 }
5083
5084 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
5085 IT_CHARPOS (*it)));
5086 result = MOVE_LINE_CONTINUED;
5087 break;
5088 }
5089 else if (new_x > it->first_visible_x)
5090 {
5091 /* Glyph is visible. Increment number of glyphs that
5092 would be displayed. */
5093 ++it->hpos;
5094 }
5095 else
5096 {
5097 /* Glyph is completely off the left margin of the display
5098 area. Nothing to do. */
5099 }
5100 }
5101
5102 if (result != MOVE_UNDEFINED)
5103 break;
5104 }
5105 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
5106 {
5107 /* Stop when TO_X specified and reached. This check is
5108 necessary here because of lines consisting of a line end,
5109 only. The line end will not produce any glyphs and we
5110 would never get MOVE_X_REACHED. */
5111 xassert (it->nglyphs == 0);
5112 result = MOVE_X_REACHED;
5113 break;
5114 }
5115
5116 /* Is this a line end? If yes, we're done. */
5117 if (ITERATOR_AT_END_OF_LINE_P (it))
5118 {
5119 result = MOVE_NEWLINE_OR_CR;
5120 break;
5121 }
5122
5123 /* The current display element has been consumed. Advance
5124 to the next. */
5125 set_iterator_to_next (it, 1);
5126
5127 /* Stop if lines are truncated and IT's current x-position is
5128 past the right edge of the window now. */
5129 if (it->truncate_lines_p
5130 && it->current_x >= it->last_visible_x)
5131 {
5132 result = MOVE_LINE_TRUNCATED;
5133 break;
5134 }
5135 }
5136
5137 /* Restore the iterator settings altered at the beginning of this
5138 function. */
5139 it->glyph_row = saved_glyph_row;
5140 return result;
5141 }
5142
5143
5144 /* Move IT forward to a specified buffer position TO_CHARPOS, TO_X,
5145 TO_Y, TO_VPOS. OP is a bit-mask that specifies where to stop. See
5146 the description of enum move_operation_enum.
5147
5148 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
5149 screen line, this function will set IT to the next position >
5150 TO_CHARPOS. */
5151
5152 void
5153 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
5154 struct it *it;
5155 int to_charpos, to_x, to_y, to_vpos;
5156 int op;
5157 {
5158 enum move_it_result skip, skip2 = MOVE_X_REACHED;
5159 int line_height;
5160 int reached = 0;
5161
5162 for (;;)
5163 {
5164 if (op & MOVE_TO_VPOS)
5165 {
5166 /* If no TO_CHARPOS and no TO_X specified, stop at the
5167 start of the line TO_VPOS. */
5168 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
5169 {
5170 if (it->vpos == to_vpos)
5171 {
5172 reached = 1;
5173 break;
5174 }
5175 else
5176 skip = move_it_in_display_line_to (it, -1, -1, 0);
5177 }
5178 else
5179 {
5180 /* TO_VPOS >= 0 means stop at TO_X in the line at
5181 TO_VPOS, or at TO_POS, whichever comes first. */
5182 if (it->vpos == to_vpos)
5183 {
5184 reached = 2;
5185 break;
5186 }
5187
5188 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
5189
5190 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
5191 {
5192 reached = 3;
5193 break;
5194 }
5195 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
5196 {
5197 /* We have reached TO_X but not in the line we want. */
5198 skip = move_it_in_display_line_to (it, to_charpos,
5199 -1, MOVE_TO_POS);
5200 if (skip == MOVE_POS_MATCH_OR_ZV)
5201 {
5202 reached = 4;
5203 break;
5204 }
5205 }
5206 }
5207 }
5208 else if (op & MOVE_TO_Y)
5209 {
5210 struct it it_backup;
5211
5212 /* TO_Y specified means stop at TO_X in the line containing
5213 TO_Y---or at TO_CHARPOS if this is reached first. The
5214 problem is that we can't really tell whether the line
5215 contains TO_Y before we have completely scanned it, and
5216 this may skip past TO_X. What we do is to first scan to
5217 TO_X.
5218
5219 If TO_X is not specified, use a TO_X of zero. The reason
5220 is to make the outcome of this function more predictable.
5221 If we didn't use TO_X == 0, we would stop at the end of
5222 the line which is probably not what a caller would expect
5223 to happen. */
5224 skip = move_it_in_display_line_to (it, to_charpos,
5225 ((op & MOVE_TO_X)
5226 ? to_x : 0),
5227 (MOVE_TO_X
5228 | (op & MOVE_TO_POS)));
5229
5230 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
5231 if (skip == MOVE_POS_MATCH_OR_ZV)
5232 {
5233 reached = 5;
5234 break;
5235 }
5236
5237 /* If TO_X was reached, we would like to know whether TO_Y
5238 is in the line. This can only be said if we know the
5239 total line height which requires us to scan the rest of
5240 the line. */
5241 if (skip == MOVE_X_REACHED)
5242 {
5243 it_backup = *it;
5244 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
5245 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
5246 op & MOVE_TO_POS);
5247 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
5248 }
5249
5250 /* Now, decide whether TO_Y is in this line. */
5251 line_height = it->max_ascent + it->max_descent;
5252 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
5253
5254 if (to_y >= it->current_y
5255 && to_y < it->current_y + line_height)
5256 {
5257 if (skip == MOVE_X_REACHED)
5258 /* If TO_Y is in this line and TO_X was reached above,
5259 we scanned too far. We have to restore IT's settings
5260 to the ones before skipping. */
5261 *it = it_backup;
5262 reached = 6;
5263 }
5264 else if (skip == MOVE_X_REACHED)
5265 {
5266 skip = skip2;
5267 if (skip == MOVE_POS_MATCH_OR_ZV)
5268 reached = 7;
5269 }
5270
5271 if (reached)
5272 break;
5273 }
5274 else
5275 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
5276
5277 switch (skip)
5278 {
5279 case MOVE_POS_MATCH_OR_ZV:
5280 reached = 8;
5281 goto out;
5282
5283 case MOVE_NEWLINE_OR_CR:
5284 set_iterator_to_next (it, 1);
5285 it->continuation_lines_width = 0;
5286 break;
5287
5288 case MOVE_LINE_TRUNCATED:
5289 it->continuation_lines_width = 0;
5290 reseat_at_next_visible_line_start (it, 0);
5291 if ((op & MOVE_TO_POS) != 0
5292 && IT_CHARPOS (*it) > to_charpos)
5293 {
5294 reached = 9;
5295 goto out;
5296 }
5297 break;
5298
5299 case MOVE_LINE_CONTINUED:
5300 it->continuation_lines_width += it->current_x;
5301 break;
5302
5303 default:
5304 abort ();
5305 }
5306
5307 /* Reset/increment for the next run. */
5308 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
5309 it->current_x = it->hpos = 0;
5310 it->current_y += it->max_ascent + it->max_descent;
5311 ++it->vpos;
5312 last_height = it->max_ascent + it->max_descent;
5313 last_max_ascent = it->max_ascent;
5314 it->max_ascent = it->max_descent = 0;
5315 }
5316
5317 out:
5318
5319 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
5320 }
5321
5322
5323 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
5324
5325 If DY > 0, move IT backward at least that many pixels. DY = 0
5326 means move IT backward to the preceding line start or BEGV. This
5327 function may move over more than DY pixels if IT->current_y - DY
5328 ends up in the middle of a line; in this case IT->current_y will be
5329 set to the top of the line moved to. */
5330
5331 void
5332 move_it_vertically_backward (it, dy)
5333 struct it *it;
5334 int dy;
5335 {
5336 int nlines, h;
5337 struct it it2, it3;
5338 int start_pos = IT_CHARPOS (*it);
5339
5340 xassert (dy >= 0);
5341
5342 /* Estimate how many newlines we must move back. */
5343 nlines = max (1, dy / CANON_Y_UNIT (it->f));
5344
5345 /* Set the iterator's position that many lines back. */
5346 while (nlines-- && IT_CHARPOS (*it) > BEGV)
5347 back_to_previous_visible_line_start (it);
5348
5349 /* Reseat the iterator here. When moving backward, we don't want
5350 reseat to skip forward over invisible text, set up the iterator
5351 to deliver from overlay strings at the new position etc. So,
5352 use reseat_1 here. */
5353 reseat_1 (it, it->current.pos, 1);
5354
5355 /* We are now surely at a line start. */
5356 it->current_x = it->hpos = 0;
5357
5358 /* Move forward and see what y-distance we moved. First move to the
5359 start of the next line so that we get its height. We need this
5360 height to be able to tell whether we reached the specified
5361 y-distance. */
5362 it2 = *it;
5363 it2.max_ascent = it2.max_descent = 0;
5364 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
5365 MOVE_TO_POS | MOVE_TO_VPOS);
5366 xassert (IT_CHARPOS (*it) >= BEGV);
5367 it3 = it2;
5368
5369 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
5370 xassert (IT_CHARPOS (*it) >= BEGV);
5371 h = it2.current_y - it->current_y;
5372 nlines = it2.vpos - it->vpos;
5373
5374 /* Correct IT's y and vpos position. */
5375 it->vpos -= nlines;
5376 it->current_y -= h;
5377
5378 if (dy == 0)
5379 {
5380 /* DY == 0 means move to the start of the screen line. The
5381 value of nlines is > 0 if continuation lines were involved. */
5382 if (nlines > 0)
5383 move_it_by_lines (it, nlines, 1);
5384 xassert (IT_CHARPOS (*it) <= start_pos);
5385 }
5386 else if (nlines)
5387 {
5388 /* The y-position we try to reach. Note that h has been
5389 subtracted in front of the if-statement. */
5390 int target_y = it->current_y + h - dy;
5391 int y0 = it3.current_y;
5392 int y1 = line_bottom_y (&it3);
5393 int line_height = y1 - y0;
5394
5395 /* If we did not reach target_y, try to move further backward if
5396 we can. If we moved too far backward, try to move forward. */
5397 if (target_y < it->current_y
5398 /* This is heuristic. In a window that's 3 lines high, with
5399 a line height of 13 pixels each, recentering with point
5400 on the bottom line will try to move -39/2 = 19 pixels
5401 backward. Try to avoid moving into the first line. */
5402 && it->current_y - target_y > line_height / 3 * 2
5403 && IT_CHARPOS (*it) > BEGV)
5404 {
5405 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
5406 target_y - it->current_y));
5407 move_it_vertically (it, target_y - it->current_y);
5408 xassert (IT_CHARPOS (*it) >= BEGV);
5409 }
5410 else if (target_y >= it->current_y + line_height
5411 && IT_CHARPOS (*it) < ZV)
5412 {
5413 /* Should move forward by at least one line, maybe more. */
5414 do
5415 {
5416 move_it_by_lines (it, 1, 1);
5417 }
5418 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
5419
5420 xassert (IT_CHARPOS (*it) >= BEGV);
5421 }
5422 }
5423 }
5424
5425
5426 /* Move IT by a specified amount of pixel lines DY. DY negative means
5427 move backwards. DY = 0 means move to start of screen line. At the
5428 end, IT will be on the start of a screen line. */
5429
5430 void
5431 move_it_vertically (it, dy)
5432 struct it *it;
5433 int dy;
5434 {
5435 if (dy <= 0)
5436 move_it_vertically_backward (it, -dy);
5437 else if (dy > 0)
5438 {
5439 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
5440 move_it_to (it, ZV, -1, it->current_y + dy, -1,
5441 MOVE_TO_POS | MOVE_TO_Y);
5442 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
5443
5444 /* If buffer ends in ZV without a newline, move to the start of
5445 the line to satisfy the post-condition. */
5446 if (IT_CHARPOS (*it) == ZV
5447 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
5448 move_it_by_lines (it, 0, 0);
5449 }
5450 }
5451
5452
5453 /* Move iterator IT past the end of the text line it is in. */
5454
5455 void
5456 move_it_past_eol (it)
5457 struct it *it;
5458 {
5459 enum move_it_result rc;
5460
5461 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
5462 if (rc == MOVE_NEWLINE_OR_CR)
5463 set_iterator_to_next (it, 0);
5464 }
5465
5466
5467 #if 0 /* Currently not used. */
5468
5469 /* Return non-zero if some text between buffer positions START_CHARPOS
5470 and END_CHARPOS is invisible. IT->window is the window for text
5471 property lookup. */
5472
5473 static int
5474 invisible_text_between_p (it, start_charpos, end_charpos)
5475 struct it *it;
5476 int start_charpos, end_charpos;
5477 {
5478 Lisp_Object prop, limit;
5479 int invisible_found_p;
5480
5481 xassert (it != NULL && start_charpos <= end_charpos);
5482
5483 /* Is text at START invisible? */
5484 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
5485 it->window);
5486 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5487 invisible_found_p = 1;
5488 else
5489 {
5490 limit = Fnext_single_char_property_change (make_number (start_charpos),
5491 Qinvisible, Qnil,
5492 make_number (end_charpos));
5493 invisible_found_p = XFASTINT (limit) < end_charpos;
5494 }
5495
5496 return invisible_found_p;
5497 }
5498
5499 #endif /* 0 */
5500
5501
5502 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
5503 negative means move up. DVPOS == 0 means move to the start of the
5504 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
5505 NEED_Y_P is zero, IT->current_y will be left unchanged.
5506
5507 Further optimization ideas: If we would know that IT->f doesn't use
5508 a face with proportional font, we could be faster for
5509 truncate-lines nil. */
5510
5511 void
5512 move_it_by_lines (it, dvpos, need_y_p)
5513 struct it *it;
5514 int dvpos, need_y_p;
5515 {
5516 struct position pos;
5517
5518 if (!FRAME_WINDOW_P (it->f))
5519 {
5520 struct text_pos textpos;
5521
5522 /* We can use vmotion on frames without proportional fonts. */
5523 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
5524 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
5525 reseat (it, textpos, 1);
5526 it->vpos += pos.vpos;
5527 it->current_y += pos.vpos;
5528 }
5529 else if (dvpos == 0)
5530 {
5531 /* DVPOS == 0 means move to the start of the screen line. */
5532 move_it_vertically_backward (it, 0);
5533 xassert (it->current_x == 0 && it->hpos == 0);
5534 }
5535 else if (dvpos > 0)
5536 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
5537 else
5538 {
5539 struct it it2;
5540 int start_charpos, i;
5541
5542 /* Start at the beginning of the screen line containing IT's
5543 position. */
5544 move_it_vertically_backward (it, 0);
5545
5546 /* Go back -DVPOS visible lines and reseat the iterator there. */
5547 start_charpos = IT_CHARPOS (*it);
5548 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
5549 back_to_previous_visible_line_start (it);
5550 reseat (it, it->current.pos, 1);
5551 it->current_x = it->hpos = 0;
5552
5553 /* Above call may have moved too far if continuation lines
5554 are involved. Scan forward and see if it did. */
5555 it2 = *it;
5556 it2.vpos = it2.current_y = 0;
5557 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
5558 it->vpos -= it2.vpos;
5559 it->current_y -= it2.current_y;
5560 it->current_x = it->hpos = 0;
5561
5562 /* If we moved too far, move IT some lines forward. */
5563 if (it2.vpos > -dvpos)
5564 {
5565 int delta = it2.vpos + dvpos;
5566 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
5567 }
5568 }
5569 }
5570
5571
5572 \f
5573 /***********************************************************************
5574 Messages
5575 ***********************************************************************/
5576
5577
5578 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
5579 to *Messages*. */
5580
5581 void
5582 add_to_log (format, arg1, arg2)
5583 char *format;
5584 Lisp_Object arg1, arg2;
5585 {
5586 Lisp_Object args[3];
5587 Lisp_Object msg, fmt;
5588 char *buffer;
5589 int len;
5590 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5591
5592 /* Do nothing if called asynchronously. Inserting text into
5593 a buffer may call after-change-functions and alike and
5594 that would means running Lisp asynchronously. */
5595 if (handling_signal)
5596 return;
5597
5598 fmt = msg = Qnil;
5599 GCPRO4 (fmt, msg, arg1, arg2);
5600
5601 args[0] = fmt = build_string (format);
5602 args[1] = arg1;
5603 args[2] = arg2;
5604 msg = Fformat (3, args);
5605
5606 len = STRING_BYTES (XSTRING (msg)) + 1;
5607 buffer = (char *) alloca (len);
5608 bcopy (XSTRING (msg)->data, buffer, len);
5609
5610 message_dolog (buffer, len - 1, 1, 0);
5611 UNGCPRO;
5612 }
5613
5614
5615 /* Output a newline in the *Messages* buffer if "needs" one. */
5616
5617 void
5618 message_log_maybe_newline ()
5619 {
5620 if (message_log_need_newline)
5621 message_dolog ("", 0, 1, 0);
5622 }
5623
5624
5625 /* Add a string M of length NBYTES to the message log, optionally
5626 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
5627 nonzero, means interpret the contents of M as multibyte. This
5628 function calls low-level routines in order to bypass text property
5629 hooks, etc. which might not be safe to run. */
5630
5631 void
5632 message_dolog (m, nbytes, nlflag, multibyte)
5633 char *m;
5634 int nbytes, nlflag, multibyte;
5635 {
5636 if (!NILP (Vmessage_log_max))
5637 {
5638 struct buffer *oldbuf;
5639 Lisp_Object oldpoint, oldbegv, oldzv;
5640 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5641 int point_at_end = 0;
5642 int zv_at_end = 0;
5643 Lisp_Object old_deactivate_mark, tem;
5644 struct gcpro gcpro1;
5645
5646 old_deactivate_mark = Vdeactivate_mark;
5647 oldbuf = current_buffer;
5648 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
5649 current_buffer->undo_list = Qt;
5650
5651 oldpoint = message_dolog_marker1;
5652 set_marker_restricted (oldpoint, make_number (PT), Qnil);
5653 oldbegv = message_dolog_marker2;
5654 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
5655 oldzv = message_dolog_marker3;
5656 set_marker_restricted (oldzv, make_number (ZV), Qnil);
5657 GCPRO1 (old_deactivate_mark);
5658
5659 if (PT == Z)
5660 point_at_end = 1;
5661 if (ZV == Z)
5662 zv_at_end = 1;
5663
5664 BEGV = BEG;
5665 BEGV_BYTE = BEG_BYTE;
5666 ZV = Z;
5667 ZV_BYTE = Z_BYTE;
5668 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5669
5670 /* Insert the string--maybe converting multibyte to single byte
5671 or vice versa, so that all the text fits the buffer. */
5672 if (multibyte
5673 && NILP (current_buffer->enable_multibyte_characters))
5674 {
5675 int i, c, char_bytes;
5676 unsigned char work[1];
5677
5678 /* Convert a multibyte string to single-byte
5679 for the *Message* buffer. */
5680 for (i = 0; i < nbytes; i += nbytes)
5681 {
5682 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
5683 work[0] = (SINGLE_BYTE_CHAR_P (c)
5684 ? c
5685 : multibyte_char_to_unibyte (c, Qnil));
5686 insert_1_both (work, 1, 1, 1, 0, 0);
5687 }
5688 }
5689 else if (! multibyte
5690 && ! NILP (current_buffer->enable_multibyte_characters))
5691 {
5692 int i, c, char_bytes;
5693 unsigned char *msg = (unsigned char *) m;
5694 unsigned char str[MAX_MULTIBYTE_LENGTH];
5695 /* Convert a single-byte string to multibyte
5696 for the *Message* buffer. */
5697 for (i = 0; i < nbytes; i++)
5698 {
5699 c = unibyte_char_to_multibyte (msg[i]);
5700 char_bytes = CHAR_STRING (c, str);
5701 insert_1_both (str, 1, char_bytes, 1, 0, 0);
5702 }
5703 }
5704 else if (nbytes)
5705 insert_1 (m, nbytes, 1, 0, 0);
5706
5707 if (nlflag)
5708 {
5709 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
5710 insert_1 ("\n", 1, 1, 0, 0);
5711
5712 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
5713 this_bol = PT;
5714 this_bol_byte = PT_BYTE;
5715
5716 /* See if this line duplicates the previous one.
5717 If so, combine duplicates. */
5718 if (this_bol > BEG)
5719 {
5720 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
5721 prev_bol = PT;
5722 prev_bol_byte = PT_BYTE;
5723
5724 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
5725 this_bol, this_bol_byte);
5726 if (dup)
5727 {
5728 del_range_both (prev_bol, prev_bol_byte,
5729 this_bol, this_bol_byte, 0);
5730 if (dup > 1)
5731 {
5732 char dupstr[40];
5733 int duplen;
5734
5735 /* If you change this format, don't forget to also
5736 change message_log_check_duplicate. */
5737 sprintf (dupstr, " [%d times]", dup);
5738 duplen = strlen (dupstr);
5739 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
5740 insert_1 (dupstr, duplen, 1, 0, 1);
5741 }
5742 }
5743 }
5744
5745 /* If we have more than the desired maximum number of lines
5746 in the *Messages* buffer now, delete the oldest ones.
5747 This is safe because we don't have undo in this buffer. */
5748
5749 if (NATNUMP (Vmessage_log_max))
5750 {
5751 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
5752 -XFASTINT (Vmessage_log_max) - 1, 0);
5753 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
5754 }
5755 }
5756 BEGV = XMARKER (oldbegv)->charpos;
5757 BEGV_BYTE = marker_byte_position (oldbegv);
5758
5759 if (zv_at_end)
5760 {
5761 ZV = Z;
5762 ZV_BYTE = Z_BYTE;
5763 }
5764 else
5765 {
5766 ZV = XMARKER (oldzv)->charpos;
5767 ZV_BYTE = marker_byte_position (oldzv);
5768 }
5769
5770 if (point_at_end)
5771 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5772 else
5773 /* We can't do Fgoto_char (oldpoint) because it will run some
5774 Lisp code. */
5775 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
5776 XMARKER (oldpoint)->bytepos);
5777
5778 UNGCPRO;
5779 unchain_marker (oldpoint);
5780 unchain_marker (oldbegv);
5781 unchain_marker (oldzv);
5782
5783 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
5784 set_buffer_internal (oldbuf);
5785 if (NILP (tem))
5786 windows_or_buffers_changed = old_windows_or_buffers_changed;
5787 message_log_need_newline = !nlflag;
5788 Vdeactivate_mark = old_deactivate_mark;
5789 }
5790 }
5791
5792
5793 /* We are at the end of the buffer after just having inserted a newline.
5794 (Note: We depend on the fact we won't be crossing the gap.)
5795 Check to see if the most recent message looks a lot like the previous one.
5796 Return 0 if different, 1 if the new one should just replace it, or a
5797 value N > 1 if we should also append " [N times]". */
5798
5799 static int
5800 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
5801 int prev_bol, this_bol;
5802 int prev_bol_byte, this_bol_byte;
5803 {
5804 int i;
5805 int len = Z_BYTE - 1 - this_bol_byte;
5806 int seen_dots = 0;
5807 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
5808 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
5809
5810 for (i = 0; i < len; i++)
5811 {
5812 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
5813 seen_dots = 1;
5814 if (p1[i] != p2[i])
5815 return seen_dots;
5816 }
5817 p1 += len;
5818 if (*p1 == '\n')
5819 return 2;
5820 if (*p1++ == ' ' && *p1++ == '[')
5821 {
5822 int n = 0;
5823 while (*p1 >= '0' && *p1 <= '9')
5824 n = n * 10 + *p1++ - '0';
5825 if (strncmp (p1, " times]\n", 8) == 0)
5826 return n+1;
5827 }
5828 return 0;
5829 }
5830
5831
5832 /* Display an echo area message M with a specified length of NBYTES
5833 bytes. The string may include null characters. If M is 0, clear
5834 out any existing message, and let the mini-buffer text show
5835 through.
5836
5837 The buffer M must continue to exist until after the echo area gets
5838 cleared or some other message gets displayed there. This means do
5839 not pass text that is stored in a Lisp string; do not pass text in
5840 a buffer that was alloca'd. */
5841
5842 void
5843 message2 (m, nbytes, multibyte)
5844 char *m;
5845 int nbytes;
5846 int multibyte;
5847 {
5848 /* First flush out any partial line written with print. */
5849 message_log_maybe_newline ();
5850 if (m)
5851 message_dolog (m, nbytes, 1, multibyte);
5852 message2_nolog (m, nbytes, multibyte);
5853 }
5854
5855
5856 /* The non-logging counterpart of message2. */
5857
5858 void
5859 message2_nolog (m, nbytes, multibyte)
5860 char *m;
5861 int nbytes;
5862 {
5863 struct frame *sf = SELECTED_FRAME ();
5864 message_enable_multibyte = multibyte;
5865
5866 if (noninteractive)
5867 {
5868 if (noninteractive_need_newline)
5869 putc ('\n', stderr);
5870 noninteractive_need_newline = 0;
5871 if (m)
5872 fwrite (m, nbytes, 1, stderr);
5873 if (cursor_in_echo_area == 0)
5874 fprintf (stderr, "\n");
5875 fflush (stderr);
5876 }
5877 /* A null message buffer means that the frame hasn't really been
5878 initialized yet. Error messages get reported properly by
5879 cmd_error, so this must be just an informative message; toss it. */
5880 else if (INTERACTIVE
5881 && sf->glyphs_initialized_p
5882 && FRAME_MESSAGE_BUF (sf))
5883 {
5884 Lisp_Object mini_window;
5885 struct frame *f;
5886
5887 /* Get the frame containing the mini-buffer
5888 that the selected frame is using. */
5889 mini_window = FRAME_MINIBUF_WINDOW (sf);
5890 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5891
5892 FRAME_SAMPLE_VISIBILITY (f);
5893 if (FRAME_VISIBLE_P (sf)
5894 && ! FRAME_VISIBLE_P (f))
5895 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
5896
5897 if (m)
5898 {
5899 set_message (m, Qnil, nbytes, multibyte);
5900 if (minibuffer_auto_raise)
5901 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5902 }
5903 else
5904 clear_message (1, 1);
5905
5906 do_pending_window_change (0);
5907 echo_area_display (1);
5908 do_pending_window_change (0);
5909 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5910 (*frame_up_to_date_hook) (f);
5911 }
5912 }
5913
5914
5915 /* Display an echo area message M with a specified length of NBYTES
5916 bytes. The string may include null characters. If M is not a
5917 string, clear out any existing message, and let the mini-buffer
5918 text show through. */
5919
5920 void
5921 message3 (m, nbytes, multibyte)
5922 Lisp_Object m;
5923 int nbytes;
5924 int multibyte;
5925 {
5926 struct gcpro gcpro1;
5927
5928 GCPRO1 (m);
5929
5930 /* First flush out any partial line written with print. */
5931 message_log_maybe_newline ();
5932 if (STRINGP (m))
5933 message_dolog (XSTRING (m)->data, nbytes, 1, multibyte);
5934 message3_nolog (m, nbytes, multibyte);
5935
5936 UNGCPRO;
5937 }
5938
5939
5940 /* The non-logging version of message3. */
5941
5942 void
5943 message3_nolog (m, nbytes, multibyte)
5944 Lisp_Object m;
5945 int nbytes, multibyte;
5946 {
5947 struct frame *sf = SELECTED_FRAME ();
5948 message_enable_multibyte = multibyte;
5949
5950 if (noninteractive)
5951 {
5952 if (noninteractive_need_newline)
5953 putc ('\n', stderr);
5954 noninteractive_need_newline = 0;
5955 if (STRINGP (m))
5956 fwrite (XSTRING (m)->data, nbytes, 1, stderr);
5957 if (cursor_in_echo_area == 0)
5958 fprintf (stderr, "\n");
5959 fflush (stderr);
5960 }
5961 /* A null message buffer means that the frame hasn't really been
5962 initialized yet. Error messages get reported properly by
5963 cmd_error, so this must be just an informative message; toss it. */
5964 else if (INTERACTIVE
5965 && sf->glyphs_initialized_p
5966 && FRAME_MESSAGE_BUF (sf))
5967 {
5968 Lisp_Object mini_window;
5969 Lisp_Object frame;
5970 struct frame *f;
5971
5972 /* Get the frame containing the mini-buffer
5973 that the selected frame is using. */
5974 mini_window = FRAME_MINIBUF_WINDOW (sf);
5975 frame = XWINDOW (mini_window)->frame;
5976 f = XFRAME (frame);
5977
5978 FRAME_SAMPLE_VISIBILITY (f);
5979 if (FRAME_VISIBLE_P (sf)
5980 && !FRAME_VISIBLE_P (f))
5981 Fmake_frame_visible (frame);
5982
5983 if (STRINGP (m) && XSTRING (m)->size)
5984 {
5985 set_message (NULL, m, nbytes, multibyte);
5986 if (minibuffer_auto_raise)
5987 Fraise_frame (frame);
5988 }
5989 else
5990 clear_message (1, 1);
5991
5992 do_pending_window_change (0);
5993 echo_area_display (1);
5994 do_pending_window_change (0);
5995 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5996 (*frame_up_to_date_hook) (f);
5997 }
5998 }
5999
6000
6001 /* Display a null-terminated echo area message M. If M is 0, clear
6002 out any existing message, and let the mini-buffer text show through.
6003
6004 The buffer M must continue to exist until after the echo area gets
6005 cleared or some other message gets displayed there. Do not pass
6006 text that is stored in a Lisp string. Do not pass text in a buffer
6007 that was alloca'd. */
6008
6009 void
6010 message1 (m)
6011 char *m;
6012 {
6013 message2 (m, (m ? strlen (m) : 0), 0);
6014 }
6015
6016
6017 /* The non-logging counterpart of message1. */
6018
6019 void
6020 message1_nolog (m)
6021 char *m;
6022 {
6023 message2_nolog (m, (m ? strlen (m) : 0), 0);
6024 }
6025
6026 /* Display a message M which contains a single %s
6027 which gets replaced with STRING. */
6028
6029 void
6030 message_with_string (m, string, log)
6031 char *m;
6032 Lisp_Object string;
6033 int log;
6034 {
6035 if (noninteractive)
6036 {
6037 if (m)
6038 {
6039 if (noninteractive_need_newline)
6040 putc ('\n', stderr);
6041 noninteractive_need_newline = 0;
6042 fprintf (stderr, m, XSTRING (string)->data);
6043 if (cursor_in_echo_area == 0)
6044 fprintf (stderr, "\n");
6045 fflush (stderr);
6046 }
6047 }
6048 else if (INTERACTIVE)
6049 {
6050 /* The frame whose minibuffer we're going to display the message on.
6051 It may be larger than the selected frame, so we need
6052 to use its buffer, not the selected frame's buffer. */
6053 Lisp_Object mini_window;
6054 struct frame *f, *sf = SELECTED_FRAME ();
6055
6056 /* Get the frame containing the minibuffer
6057 that the selected frame is using. */
6058 mini_window = FRAME_MINIBUF_WINDOW (sf);
6059 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6060
6061 /* A null message buffer means that the frame hasn't really been
6062 initialized yet. Error messages get reported properly by
6063 cmd_error, so this must be just an informative message; toss it. */
6064 if (FRAME_MESSAGE_BUF (f))
6065 {
6066 int len;
6067 char *a[1];
6068 a[0] = (char *) XSTRING (string)->data;
6069
6070 len = doprnt (FRAME_MESSAGE_BUF (f),
6071 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6072
6073 if (log)
6074 message2 (FRAME_MESSAGE_BUF (f), len,
6075 STRING_MULTIBYTE (string));
6076 else
6077 message2_nolog (FRAME_MESSAGE_BUF (f), len,
6078 STRING_MULTIBYTE (string));
6079
6080 /* Print should start at the beginning of the message
6081 buffer next time. */
6082 message_buf_print = 0;
6083 }
6084 }
6085 }
6086
6087
6088 /* Dump an informative message to the minibuf. If M is 0, clear out
6089 any existing message, and let the mini-buffer text show through. */
6090
6091 /* VARARGS 1 */
6092 void
6093 message (m, a1, a2, a3)
6094 char *m;
6095 EMACS_INT a1, a2, a3;
6096 {
6097 if (noninteractive)
6098 {
6099 if (m)
6100 {
6101 if (noninteractive_need_newline)
6102 putc ('\n', stderr);
6103 noninteractive_need_newline = 0;
6104 fprintf (stderr, m, a1, a2, a3);
6105 if (cursor_in_echo_area == 0)
6106 fprintf (stderr, "\n");
6107 fflush (stderr);
6108 }
6109 }
6110 else if (INTERACTIVE)
6111 {
6112 /* The frame whose mini-buffer we're going to display the message
6113 on. It may be larger than the selected frame, so we need to
6114 use its buffer, not the selected frame's buffer. */
6115 Lisp_Object mini_window;
6116 struct frame *f, *sf = SELECTED_FRAME ();
6117
6118 /* Get the frame containing the mini-buffer
6119 that the selected frame is using. */
6120 mini_window = FRAME_MINIBUF_WINDOW (sf);
6121 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6122
6123 /* A null message buffer means that the frame hasn't really been
6124 initialized yet. Error messages get reported properly by
6125 cmd_error, so this must be just an informative message; toss
6126 it. */
6127 if (FRAME_MESSAGE_BUF (f))
6128 {
6129 if (m)
6130 {
6131 int len;
6132 #ifdef NO_ARG_ARRAY
6133 char *a[3];
6134 a[0] = (char *) a1;
6135 a[1] = (char *) a2;
6136 a[2] = (char *) a3;
6137
6138 len = doprnt (FRAME_MESSAGE_BUF (f),
6139 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6140 #else
6141 len = doprnt (FRAME_MESSAGE_BUF (f),
6142 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
6143 (char **) &a1);
6144 #endif /* NO_ARG_ARRAY */
6145
6146 message2 (FRAME_MESSAGE_BUF (f), len, 0);
6147 }
6148 else
6149 message1 (0);
6150
6151 /* Print should start at the beginning of the message
6152 buffer next time. */
6153 message_buf_print = 0;
6154 }
6155 }
6156 }
6157
6158
6159 /* The non-logging version of message. */
6160
6161 void
6162 message_nolog (m, a1, a2, a3)
6163 char *m;
6164 EMACS_INT a1, a2, a3;
6165 {
6166 Lisp_Object old_log_max;
6167 old_log_max = Vmessage_log_max;
6168 Vmessage_log_max = Qnil;
6169 message (m, a1, a2, a3);
6170 Vmessage_log_max = old_log_max;
6171 }
6172
6173
6174 /* Display the current message in the current mini-buffer. This is
6175 only called from error handlers in process.c, and is not time
6176 critical. */
6177
6178 void
6179 update_echo_area ()
6180 {
6181 if (!NILP (echo_area_buffer[0]))
6182 {
6183 Lisp_Object string;
6184 string = Fcurrent_message ();
6185 message3 (string, XSTRING (string)->size,
6186 !NILP (current_buffer->enable_multibyte_characters));
6187 }
6188 }
6189
6190
6191 /* Make sure echo area buffers in echo_buffers[] are life. If they
6192 aren't, make new ones. */
6193
6194 static void
6195 ensure_echo_area_buffers ()
6196 {
6197 int i;
6198
6199 for (i = 0; i < 2; ++i)
6200 if (!BUFFERP (echo_buffer[i])
6201 || NILP (XBUFFER (echo_buffer[i])->name))
6202 {
6203 char name[30];
6204 Lisp_Object old_buffer;
6205 int j;
6206
6207 old_buffer = echo_buffer[i];
6208 sprintf (name, " *Echo Area %d*", i);
6209 echo_buffer[i] = Fget_buffer_create (build_string (name));
6210 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
6211
6212 for (j = 0; j < 2; ++j)
6213 if (EQ (old_buffer, echo_area_buffer[j]))
6214 echo_area_buffer[j] = echo_buffer[i];
6215 }
6216 }
6217
6218
6219 /* Call FN with args A1..A4 with either the current or last displayed
6220 echo_area_buffer as current buffer.
6221
6222 WHICH zero means use the current message buffer
6223 echo_area_buffer[0]. If that is nil, choose a suitable buffer
6224 from echo_buffer[] and clear it.
6225
6226 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
6227 suitable buffer from echo_buffer[] and clear it.
6228
6229 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
6230 that the current message becomes the last displayed one, make
6231 choose a suitable buffer for echo_area_buffer[0], and clear it.
6232
6233 Value is what FN returns. */
6234
6235 static int
6236 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
6237 struct window *w;
6238 int which;
6239 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
6240 EMACS_INT a1;
6241 Lisp_Object a2;
6242 EMACS_INT a3, a4;
6243 {
6244 Lisp_Object buffer;
6245 int this_one, the_other, clear_buffer_p, rc;
6246 int count = BINDING_STACK_SIZE ();
6247
6248 /* If buffers aren't life, make new ones. */
6249 ensure_echo_area_buffers ();
6250
6251 clear_buffer_p = 0;
6252
6253 if (which == 0)
6254 this_one = 0, the_other = 1;
6255 else if (which > 0)
6256 this_one = 1, the_other = 0;
6257 else
6258 {
6259 this_one = 0, the_other = 1;
6260 clear_buffer_p = 1;
6261
6262 /* We need a fresh one in case the current echo buffer equals
6263 the one containing the last displayed echo area message. */
6264 if (!NILP (echo_area_buffer[this_one])
6265 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
6266 echo_area_buffer[this_one] = Qnil;
6267 }
6268
6269 /* Choose a suitable buffer from echo_buffer[] is we don't
6270 have one. */
6271 if (NILP (echo_area_buffer[this_one]))
6272 {
6273 echo_area_buffer[this_one]
6274 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
6275 ? echo_buffer[the_other]
6276 : echo_buffer[this_one]);
6277 clear_buffer_p = 1;
6278 }
6279
6280 buffer = echo_area_buffer[this_one];
6281
6282 /* Don't get confused by reusing the buffer used for echoing
6283 for a different purpose. */
6284 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
6285 cancel_echoing ();
6286
6287 record_unwind_protect (unwind_with_echo_area_buffer,
6288 with_echo_area_buffer_unwind_data (w));
6289
6290 /* Make the echo area buffer current. Note that for display
6291 purposes, it is not necessary that the displayed window's buffer
6292 == current_buffer, except for text property lookup. So, let's
6293 only set that buffer temporarily here without doing a full
6294 Fset_window_buffer. We must also change w->pointm, though,
6295 because otherwise an assertions in unshow_buffer fails, and Emacs
6296 aborts. */
6297 set_buffer_internal_1 (XBUFFER (buffer));
6298 if (w)
6299 {
6300 w->buffer = buffer;
6301 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
6302 }
6303
6304 current_buffer->undo_list = Qt;
6305 current_buffer->read_only = Qnil;
6306 specbind (Qinhibit_read_only, Qt);
6307 specbind (Qinhibit_modification_hooks, Qt);
6308
6309 if (clear_buffer_p && Z > BEG)
6310 del_range (BEG, Z);
6311
6312 xassert (BEGV >= BEG);
6313 xassert (ZV <= Z && ZV >= BEGV);
6314
6315 rc = fn (a1, a2, a3, a4);
6316
6317 xassert (BEGV >= BEG);
6318 xassert (ZV <= Z && ZV >= BEGV);
6319
6320 unbind_to (count, Qnil);
6321 return rc;
6322 }
6323
6324
6325 /* Save state that should be preserved around the call to the function
6326 FN called in with_echo_area_buffer. */
6327
6328 static Lisp_Object
6329 with_echo_area_buffer_unwind_data (w)
6330 struct window *w;
6331 {
6332 int i = 0;
6333 Lisp_Object vector;
6334
6335 /* Reduce consing by keeping one vector in
6336 Vwith_echo_area_save_vector. */
6337 vector = Vwith_echo_area_save_vector;
6338 Vwith_echo_area_save_vector = Qnil;
6339
6340 if (NILP (vector))
6341 vector = Fmake_vector (make_number (7), Qnil);
6342
6343 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
6344 AREF (vector, i) = Vdeactivate_mark, ++i;
6345 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
6346
6347 if (w)
6348 {
6349 XSETWINDOW (AREF (vector, i), w); ++i;
6350 AREF (vector, i) = w->buffer; ++i;
6351 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
6352 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
6353 }
6354 else
6355 {
6356 int end = i + 4;
6357 for (; i < end; ++i)
6358 AREF (vector, i) = Qnil;
6359 }
6360
6361 xassert (i == ASIZE (vector));
6362 return vector;
6363 }
6364
6365
6366 /* Restore global state from VECTOR which was created by
6367 with_echo_area_buffer_unwind_data. */
6368
6369 static Lisp_Object
6370 unwind_with_echo_area_buffer (vector)
6371 Lisp_Object vector;
6372 {
6373 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
6374 Vdeactivate_mark = AREF (vector, 1);
6375 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
6376
6377 if (WINDOWP (AREF (vector, 3)))
6378 {
6379 struct window *w;
6380 Lisp_Object buffer, charpos, bytepos;
6381
6382 w = XWINDOW (AREF (vector, 3));
6383 buffer = AREF (vector, 4);
6384 charpos = AREF (vector, 5);
6385 bytepos = AREF (vector, 6);
6386
6387 w->buffer = buffer;
6388 set_marker_both (w->pointm, buffer,
6389 XFASTINT (charpos), XFASTINT (bytepos));
6390 }
6391
6392 Vwith_echo_area_save_vector = vector;
6393 return Qnil;
6394 }
6395
6396
6397 /* Set up the echo area for use by print functions. MULTIBYTE_P
6398 non-zero means we will print multibyte. */
6399
6400 void
6401 setup_echo_area_for_printing (multibyte_p)
6402 int multibyte_p;
6403 {
6404 ensure_echo_area_buffers ();
6405
6406 if (!message_buf_print)
6407 {
6408 /* A message has been output since the last time we printed.
6409 Choose a fresh echo area buffer. */
6410 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6411 echo_area_buffer[0] = echo_buffer[1];
6412 else
6413 echo_area_buffer[0] = echo_buffer[0];
6414
6415 /* Switch to that buffer and clear it. */
6416 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6417 current_buffer->truncate_lines = Qnil;
6418
6419 if (Z > BEG)
6420 {
6421 int count = BINDING_STACK_SIZE ();
6422 specbind (Qinhibit_read_only, Qt);
6423 del_range (BEG, Z);
6424 unbind_to (count, Qnil);
6425 }
6426 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6427
6428 /* Set up the buffer for the multibyteness we need. */
6429 if (multibyte_p
6430 != !NILP (current_buffer->enable_multibyte_characters))
6431 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
6432
6433 /* Raise the frame containing the echo area. */
6434 if (minibuffer_auto_raise)
6435 {
6436 struct frame *sf = SELECTED_FRAME ();
6437 Lisp_Object mini_window;
6438 mini_window = FRAME_MINIBUF_WINDOW (sf);
6439 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6440 }
6441
6442 message_log_maybe_newline ();
6443 message_buf_print = 1;
6444 }
6445 else
6446 {
6447 if (NILP (echo_area_buffer[0]))
6448 {
6449 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6450 echo_area_buffer[0] = echo_buffer[1];
6451 else
6452 echo_area_buffer[0] = echo_buffer[0];
6453 }
6454
6455 if (current_buffer != XBUFFER (echo_area_buffer[0]))
6456 {
6457 /* Someone switched buffers between print requests. */
6458 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6459 current_buffer->truncate_lines = Qnil;
6460 }
6461 }
6462 }
6463
6464
6465 /* Display an echo area message in window W. Value is non-zero if W's
6466 height is changed. If display_last_displayed_message_p is
6467 non-zero, display the message that was last displayed, otherwise
6468 display the current message. */
6469
6470 static int
6471 display_echo_area (w)
6472 struct window *w;
6473 {
6474 int i, no_message_p, window_height_changed_p, count;
6475
6476 /* Temporarily disable garbage collections while displaying the echo
6477 area. This is done because a GC can print a message itself.
6478 That message would modify the echo area buffer's contents while a
6479 redisplay of the buffer is going on, and seriously confuse
6480 redisplay. */
6481 count = inhibit_garbage_collection ();
6482
6483 /* If there is no message, we must call display_echo_area_1
6484 nevertheless because it resizes the window. But we will have to
6485 reset the echo_area_buffer in question to nil at the end because
6486 with_echo_area_buffer will sets it to an empty buffer. */
6487 i = display_last_displayed_message_p ? 1 : 0;
6488 no_message_p = NILP (echo_area_buffer[i]);
6489
6490 window_height_changed_p
6491 = with_echo_area_buffer (w, display_last_displayed_message_p,
6492 display_echo_area_1,
6493 (EMACS_INT) w, Qnil, 0, 0);
6494
6495 if (no_message_p)
6496 echo_area_buffer[i] = Qnil;
6497
6498 unbind_to (count, Qnil);
6499 return window_height_changed_p;
6500 }
6501
6502
6503 /* Helper for display_echo_area. Display the current buffer which
6504 contains the current echo area message in window W, a mini-window,
6505 a pointer to which is passed in A1. A2..A4 are currently not used.
6506 Change the height of W so that all of the message is displayed.
6507 Value is non-zero if height of W was changed. */
6508
6509 static int
6510 display_echo_area_1 (a1, a2, a3, a4)
6511 EMACS_INT a1;
6512 Lisp_Object a2;
6513 EMACS_INT a3, a4;
6514 {
6515 struct window *w = (struct window *) a1;
6516 Lisp_Object window;
6517 struct text_pos start;
6518 int window_height_changed_p = 0;
6519
6520 /* Do this before displaying, so that we have a large enough glyph
6521 matrix for the display. */
6522 window_height_changed_p = resize_mini_window (w, 0);
6523
6524 /* Display. */
6525 clear_glyph_matrix (w->desired_matrix);
6526 XSETWINDOW (window, w);
6527 SET_TEXT_POS (start, BEG, BEG_BYTE);
6528 try_window (window, start);
6529
6530 return window_height_changed_p;
6531 }
6532
6533
6534 /* Resize the echo area window to exactly the size needed for the
6535 currently displayed message, if there is one. If a mini-buffer
6536 is active, don't shrink it. */
6537
6538 void
6539 resize_echo_area_exactly ()
6540 {
6541 if (BUFFERP (echo_area_buffer[0])
6542 && WINDOWP (echo_area_window))
6543 {
6544 struct window *w = XWINDOW (echo_area_window);
6545 int resized_p;
6546 Lisp_Object resize_exactly;
6547
6548 if (minibuf_level == 0)
6549 resize_exactly = Qt;
6550 else
6551 resize_exactly = Qnil;
6552
6553 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
6554 (EMACS_INT) w, resize_exactly, 0, 0);
6555 if (resized_p)
6556 {
6557 ++windows_or_buffers_changed;
6558 ++update_mode_lines;
6559 redisplay_internal (0);
6560 }
6561 }
6562 }
6563
6564
6565 /* Callback function for with_echo_area_buffer, when used from
6566 resize_echo_area_exactly. A1 contains a pointer to the window to
6567 resize, EXACTLY non-nil means resize the mini-window exactly to the
6568 size of the text displayed. A3 and A4 are not used. Value is what
6569 resize_mini_window returns. */
6570
6571 static int
6572 resize_mini_window_1 (a1, exactly, a3, a4)
6573 EMACS_INT a1;
6574 Lisp_Object exactly;
6575 EMACS_INT a3, a4;
6576 {
6577 return resize_mini_window ((struct window *) a1, !NILP (exactly));
6578 }
6579
6580
6581 /* Resize mini-window W to fit the size of its contents. EXACT:P
6582 means size the window exactly to the size needed. Otherwise, it's
6583 only enlarged until W's buffer is empty. Value is non-zero if
6584 the window height has been changed. */
6585
6586 int
6587 resize_mini_window (w, exact_p)
6588 struct window *w;
6589 int exact_p;
6590 {
6591 struct frame *f = XFRAME (w->frame);
6592 int window_height_changed_p = 0;
6593
6594 xassert (MINI_WINDOW_P (w));
6595
6596 /* Don't resize windows while redisplaying a window; it would
6597 confuse redisplay functions when the size of the window they are
6598 displaying changes from under them. Such a resizing can happen,
6599 for instance, when which-func prints a long message while
6600 we are running fontification-functions. We're running these
6601 functions with safe_call which binds inhibit-redisplay to t. */
6602 if (!NILP (Vinhibit_redisplay))
6603 return 0;
6604
6605 /* Nil means don't try to resize. */
6606 if (NILP (Vresize_mini_windows)
6607 || (FRAME_X_P (f) && f->output_data.x == NULL))
6608 return 0;
6609
6610 if (!FRAME_MINIBUF_ONLY_P (f))
6611 {
6612 struct it it;
6613 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
6614 int total_height = XFASTINT (root->height) + XFASTINT (w->height);
6615 int height, max_height;
6616 int unit = CANON_Y_UNIT (f);
6617 struct text_pos start;
6618 struct buffer *old_current_buffer = NULL;
6619
6620 if (current_buffer != XBUFFER (w->buffer))
6621 {
6622 old_current_buffer = current_buffer;
6623 set_buffer_internal (XBUFFER (w->buffer));
6624 }
6625
6626 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
6627
6628 /* Compute the max. number of lines specified by the user. */
6629 if (FLOATP (Vmax_mini_window_height))
6630 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_HEIGHT (f);
6631 else if (INTEGERP (Vmax_mini_window_height))
6632 max_height = XINT (Vmax_mini_window_height);
6633 else
6634 max_height = total_height / 4;
6635
6636 /* Correct that max. height if it's bogus. */
6637 max_height = max (1, max_height);
6638 max_height = min (total_height, max_height);
6639
6640 /* Find out the height of the text in the window. */
6641 if (it.truncate_lines_p)
6642 height = 1;
6643 else
6644 {
6645 last_height = 0;
6646 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
6647 if (it.max_ascent == 0 && it.max_descent == 0)
6648 height = it.current_y + last_height;
6649 else
6650 height = it.current_y + it.max_ascent + it.max_descent;
6651 height -= it.extra_line_spacing;
6652 height = (height + unit - 1) / unit;
6653 }
6654
6655 /* Compute a suitable window start. */
6656 if (height > max_height)
6657 {
6658 height = max_height;
6659 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
6660 move_it_vertically_backward (&it, (height - 1) * unit);
6661 start = it.current.pos;
6662 }
6663 else
6664 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
6665 SET_MARKER_FROM_TEXT_POS (w->start, start);
6666
6667 if (EQ (Vresize_mini_windows, Qgrow_only))
6668 {
6669 /* Let it grow only, until we display an empty message, in which
6670 case the window shrinks again. */
6671 if (height > XFASTINT (w->height))
6672 {
6673 int old_height = XFASTINT (w->height);
6674 freeze_window_starts (f, 1);
6675 grow_mini_window (w, height - XFASTINT (w->height));
6676 window_height_changed_p = XFASTINT (w->height) != old_height;
6677 }
6678 else if (height < XFASTINT (w->height)
6679 && (exact_p || BEGV == ZV))
6680 {
6681 int old_height = XFASTINT (w->height);
6682 freeze_window_starts (f, 0);
6683 shrink_mini_window (w);
6684 window_height_changed_p = XFASTINT (w->height) != old_height;
6685 }
6686 }
6687 else
6688 {
6689 /* Always resize to exact size needed. */
6690 if (height > XFASTINT (w->height))
6691 {
6692 int old_height = XFASTINT (w->height);
6693 freeze_window_starts (f, 1);
6694 grow_mini_window (w, height - XFASTINT (w->height));
6695 window_height_changed_p = XFASTINT (w->height) != old_height;
6696 }
6697 else if (height < XFASTINT (w->height))
6698 {
6699 int old_height = XFASTINT (w->height);
6700 freeze_window_starts (f, 0);
6701 shrink_mini_window (w);
6702
6703 if (height)
6704 {
6705 freeze_window_starts (f, 1);
6706 grow_mini_window (w, height - XFASTINT (w->height));
6707 }
6708
6709 window_height_changed_p = XFASTINT (w->height) != old_height;
6710 }
6711 }
6712
6713 if (old_current_buffer)
6714 set_buffer_internal (old_current_buffer);
6715 }
6716
6717 return window_height_changed_p;
6718 }
6719
6720
6721 /* Value is the current message, a string, or nil if there is no
6722 current message. */
6723
6724 Lisp_Object
6725 current_message ()
6726 {
6727 Lisp_Object msg;
6728
6729 if (NILP (echo_area_buffer[0]))
6730 msg = Qnil;
6731 else
6732 {
6733 with_echo_area_buffer (0, 0, current_message_1,
6734 (EMACS_INT) &msg, Qnil, 0, 0);
6735 if (NILP (msg))
6736 echo_area_buffer[0] = Qnil;
6737 }
6738
6739 return msg;
6740 }
6741
6742
6743 static int
6744 current_message_1 (a1, a2, a3, a4)
6745 EMACS_INT a1;
6746 Lisp_Object a2;
6747 EMACS_INT a3, a4;
6748 {
6749 Lisp_Object *msg = (Lisp_Object *) a1;
6750
6751 if (Z > BEG)
6752 *msg = make_buffer_string (BEG, Z, 1);
6753 else
6754 *msg = Qnil;
6755 return 0;
6756 }
6757
6758
6759 /* Push the current message on Vmessage_stack for later restauration
6760 by restore_message. Value is non-zero if the current message isn't
6761 empty. This is a relatively infrequent operation, so it's not
6762 worth optimizing. */
6763
6764 int
6765 push_message ()
6766 {
6767 Lisp_Object msg;
6768 msg = current_message ();
6769 Vmessage_stack = Fcons (msg, Vmessage_stack);
6770 return STRINGP (msg);
6771 }
6772
6773
6774 /* Handler for record_unwind_protect calling pop_message. */
6775
6776 Lisp_Object
6777 push_message_unwind (dummy)
6778 Lisp_Object dummy;
6779 {
6780 pop_message ();
6781 return Qnil;
6782 }
6783
6784
6785 /* Restore message display from the top of Vmessage_stack. */
6786
6787 void
6788 restore_message ()
6789 {
6790 Lisp_Object msg;
6791
6792 xassert (CONSP (Vmessage_stack));
6793 msg = XCAR (Vmessage_stack);
6794 if (STRINGP (msg))
6795 message3_nolog (msg, STRING_BYTES (XSTRING (msg)), STRING_MULTIBYTE (msg));
6796 else
6797 message3_nolog (msg, 0, 0);
6798 }
6799
6800
6801 /* Pop the top-most entry off Vmessage_stack. */
6802
6803 void
6804 pop_message ()
6805 {
6806 xassert (CONSP (Vmessage_stack));
6807 Vmessage_stack = XCDR (Vmessage_stack);
6808 }
6809
6810
6811 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
6812 exits. If the stack is not empty, we have a missing pop_message
6813 somewhere. */
6814
6815 void
6816 check_message_stack ()
6817 {
6818 if (!NILP (Vmessage_stack))
6819 abort ();
6820 }
6821
6822
6823 /* Truncate to NCHARS what will be displayed in the echo area the next
6824 time we display it---but don't redisplay it now. */
6825
6826 void
6827 truncate_echo_area (nchars)
6828 int nchars;
6829 {
6830 if (nchars == 0)
6831 echo_area_buffer[0] = Qnil;
6832 /* A null message buffer means that the frame hasn't really been
6833 initialized yet. Error messages get reported properly by
6834 cmd_error, so this must be just an informative message; toss it. */
6835 else if (!noninteractive
6836 && INTERACTIVE
6837 && !NILP (echo_area_buffer[0]))
6838 {
6839 struct frame *sf = SELECTED_FRAME ();
6840 if (FRAME_MESSAGE_BUF (sf))
6841 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
6842 }
6843 }
6844
6845
6846 /* Helper function for truncate_echo_area. Truncate the current
6847 message to at most NCHARS characters. */
6848
6849 static int
6850 truncate_message_1 (nchars, a2, a3, a4)
6851 EMACS_INT nchars;
6852 Lisp_Object a2;
6853 EMACS_INT a3, a4;
6854 {
6855 if (BEG + nchars < Z)
6856 del_range (BEG + nchars, Z);
6857 if (Z == BEG)
6858 echo_area_buffer[0] = Qnil;
6859 return 0;
6860 }
6861
6862
6863 /* Set the current message to a substring of S or STRING.
6864
6865 If STRING is a Lisp string, set the message to the first NBYTES
6866 bytes from STRING. NBYTES zero means use the whole string. If
6867 STRING is multibyte, the message will be displayed multibyte.
6868
6869 If S is not null, set the message to the first LEN bytes of S. LEN
6870 zero means use the whole string. MULTIBYTE_P non-zero means S is
6871 multibyte. Display the message multibyte in that case. */
6872
6873 void
6874 set_message (s, string, nbytes, multibyte_p)
6875 char *s;
6876 Lisp_Object string;
6877 int nbytes;
6878 {
6879 message_enable_multibyte
6880 = ((s && multibyte_p)
6881 || (STRINGP (string) && STRING_MULTIBYTE (string)));
6882
6883 with_echo_area_buffer (0, -1, set_message_1,
6884 (EMACS_INT) s, string, nbytes, multibyte_p);
6885 message_buf_print = 0;
6886 help_echo_showing_p = 0;
6887 }
6888
6889
6890 /* Helper function for set_message. Arguments have the same meaning
6891 as there, with A1 corresponding to S and A2 corresponding to STRING
6892 This function is called with the echo area buffer being
6893 current. */
6894
6895 static int
6896 set_message_1 (a1, a2, nbytes, multibyte_p)
6897 EMACS_INT a1;
6898 Lisp_Object a2;
6899 EMACS_INT nbytes, multibyte_p;
6900 {
6901 char *s = (char *) a1;
6902 Lisp_Object string = a2;
6903
6904 xassert (BEG == Z);
6905
6906 /* Change multibyteness of the echo buffer appropriately. */
6907 if (message_enable_multibyte
6908 != !NILP (current_buffer->enable_multibyte_characters))
6909 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
6910
6911 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
6912
6913 /* Insert new message at BEG. */
6914 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6915
6916 if (STRINGP (string))
6917 {
6918 int nchars;
6919
6920 if (nbytes == 0)
6921 nbytes = XSTRING (string)->size_byte;
6922 nchars = string_byte_to_char (string, nbytes);
6923
6924 /* This function takes care of single/multibyte conversion. We
6925 just have to ensure that the echo area buffer has the right
6926 setting of enable_multibyte_characters. */
6927 insert_from_string (string, 0, 0, nchars, nbytes, 1);
6928 }
6929 else if (s)
6930 {
6931 if (nbytes == 0)
6932 nbytes = strlen (s);
6933
6934 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
6935 {
6936 /* Convert from multi-byte to single-byte. */
6937 int i, c, n;
6938 unsigned char work[1];
6939
6940 /* Convert a multibyte string to single-byte. */
6941 for (i = 0; i < nbytes; i += n)
6942 {
6943 c = string_char_and_length (s + i, nbytes - i, &n);
6944 work[0] = (SINGLE_BYTE_CHAR_P (c)
6945 ? c
6946 : multibyte_char_to_unibyte (c, Qnil));
6947 insert_1_both (work, 1, 1, 1, 0, 0);
6948 }
6949 }
6950 else if (!multibyte_p
6951 && !NILP (current_buffer->enable_multibyte_characters))
6952 {
6953 /* Convert from single-byte to multi-byte. */
6954 int i, c, n;
6955 unsigned char *msg = (unsigned char *) s;
6956 unsigned char str[MAX_MULTIBYTE_LENGTH];
6957
6958 /* Convert a single-byte string to multibyte. */
6959 for (i = 0; i < nbytes; i++)
6960 {
6961 c = unibyte_char_to_multibyte (msg[i]);
6962 n = CHAR_STRING (c, str);
6963 insert_1_both (str, 1, n, 1, 0, 0);
6964 }
6965 }
6966 else
6967 insert_1 (s, nbytes, 1, 0, 0);
6968 }
6969
6970 return 0;
6971 }
6972
6973
6974 /* Clear messages. CURRENT_P non-zero means clear the current
6975 message. LAST_DISPLAYED_P non-zero means clear the message
6976 last displayed. */
6977
6978 void
6979 clear_message (current_p, last_displayed_p)
6980 int current_p, last_displayed_p;
6981 {
6982 if (current_p)
6983 {
6984 echo_area_buffer[0] = Qnil;
6985 message_cleared_p = 1;
6986 }
6987
6988 if (last_displayed_p)
6989 echo_area_buffer[1] = Qnil;
6990
6991 message_buf_print = 0;
6992 }
6993
6994 /* Clear garbaged frames.
6995
6996 This function is used where the old redisplay called
6997 redraw_garbaged_frames which in turn called redraw_frame which in
6998 turn called clear_frame. The call to clear_frame was a source of
6999 flickering. I believe a clear_frame is not necessary. It should
7000 suffice in the new redisplay to invalidate all current matrices,
7001 and ensure a complete redisplay of all windows. */
7002
7003 static void
7004 clear_garbaged_frames ()
7005 {
7006 if (frame_garbaged)
7007 {
7008 Lisp_Object tail, frame;
7009
7010 FOR_EACH_FRAME (tail, frame)
7011 {
7012 struct frame *f = XFRAME (frame);
7013
7014 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
7015 {
7016 if (f->resized_p)
7017 Fredraw_frame (frame);
7018 clear_current_matrices (f);
7019 f->garbaged = 0;
7020 f->resized_p = 0;
7021 }
7022 }
7023
7024 frame_garbaged = 0;
7025 ++windows_or_buffers_changed;
7026 }
7027 }
7028
7029
7030 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
7031 is non-zero update selected_frame. Value is non-zero if the
7032 mini-windows height has been changed. */
7033
7034 static int
7035 echo_area_display (update_frame_p)
7036 int update_frame_p;
7037 {
7038 Lisp_Object mini_window;
7039 struct window *w;
7040 struct frame *f;
7041 int window_height_changed_p = 0;
7042 struct frame *sf = SELECTED_FRAME ();
7043
7044 mini_window = FRAME_MINIBUF_WINDOW (sf);
7045 w = XWINDOW (mini_window);
7046 f = XFRAME (WINDOW_FRAME (w));
7047
7048 /* Don't display if frame is invisible or not yet initialized. */
7049 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
7050 return 0;
7051
7052 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
7053 #ifndef macintosh
7054 #ifdef HAVE_WINDOW_SYSTEM
7055 /* When Emacs starts, selected_frame may be a visible terminal
7056 frame, even if we run under a window system. If we let this
7057 through, a message would be displayed on the terminal. */
7058 if (EQ (selected_frame, Vterminal_frame)
7059 && !NILP (Vwindow_system))
7060 return 0;
7061 #endif /* HAVE_WINDOW_SYSTEM */
7062 #endif
7063
7064 /* Redraw garbaged frames. */
7065 if (frame_garbaged)
7066 clear_garbaged_frames ();
7067
7068 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
7069 {
7070 echo_area_window = mini_window;
7071 window_height_changed_p = display_echo_area (w);
7072 w->must_be_updated_p = 1;
7073
7074 /* Update the display, unless called from redisplay_internal.
7075 Also don't update the screen during redisplay itself. The
7076 update will happen at the end of redisplay, and an update
7077 here could cause confusion. */
7078 if (update_frame_p && !redisplaying_p)
7079 {
7080 int n = 0;
7081
7082 /* If the display update has been interrupted by pending
7083 input, update mode lines in the frame. Due to the
7084 pending input, it might have been that redisplay hasn't
7085 been called, so that mode lines above the echo area are
7086 garbaged. This looks odd, so we prevent it here. */
7087 if (!display_completed)
7088 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
7089
7090 if (window_height_changed_p
7091 /* Don't do this if Emacs is shutting down. Redisplay
7092 needs to run hooks. */
7093 && !NILP (Vrun_hooks))
7094 {
7095 /* Must update other windows. Likewise as in other
7096 cases, don't let this update be interrupted by
7097 pending input. */
7098 int count = BINDING_STACK_SIZE ();
7099 specbind (Qredisplay_dont_pause, Qt);
7100 windows_or_buffers_changed = 1;
7101 redisplay_internal (0);
7102 unbind_to (count, Qnil);
7103 }
7104 else if (FRAME_WINDOW_P (f) && n == 0)
7105 {
7106 /* Window configuration is the same as before.
7107 Can do with a display update of the echo area,
7108 unless we displayed some mode lines. */
7109 update_single_window (w, 1);
7110 rif->flush_display (f);
7111 }
7112 else
7113 update_frame (f, 1, 1);
7114
7115 /* If cursor is in the echo area, make sure that the next
7116 redisplay displays the minibuffer, so that the cursor will
7117 be replaced with what the minibuffer wants. */
7118 if (cursor_in_echo_area)
7119 ++windows_or_buffers_changed;
7120 }
7121 }
7122 else if (!EQ (mini_window, selected_window))
7123 windows_or_buffers_changed++;
7124
7125 /* Last displayed message is now the current message. */
7126 echo_area_buffer[1] = echo_area_buffer[0];
7127
7128 /* Prevent redisplay optimization in redisplay_internal by resetting
7129 this_line_start_pos. This is done because the mini-buffer now
7130 displays the message instead of its buffer text. */
7131 if (EQ (mini_window, selected_window))
7132 CHARPOS (this_line_start_pos) = 0;
7133
7134 return window_height_changed_p;
7135 }
7136
7137
7138 \f
7139 /***********************************************************************
7140 Frame Titles
7141 ***********************************************************************/
7142
7143
7144 #ifdef HAVE_WINDOW_SYSTEM
7145
7146 /* A buffer for constructing frame titles in it; allocated from the
7147 heap in init_xdisp and resized as needed in store_frame_title_char. */
7148
7149 static char *frame_title_buf;
7150
7151 /* The buffer's end, and a current output position in it. */
7152
7153 static char *frame_title_buf_end;
7154 static char *frame_title_ptr;
7155
7156
7157 /* Store a single character C for the frame title in frame_title_buf.
7158 Re-allocate frame_title_buf if necessary. */
7159
7160 static void
7161 store_frame_title_char (c)
7162 char c;
7163 {
7164 /* If output position has reached the end of the allocated buffer,
7165 double the buffer's size. */
7166 if (frame_title_ptr == frame_title_buf_end)
7167 {
7168 int len = frame_title_ptr - frame_title_buf;
7169 int new_size = 2 * len * sizeof *frame_title_buf;
7170 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
7171 frame_title_buf_end = frame_title_buf + new_size;
7172 frame_title_ptr = frame_title_buf + len;
7173 }
7174
7175 *frame_title_ptr++ = c;
7176 }
7177
7178
7179 /* Store part of a frame title in frame_title_buf, beginning at
7180 frame_title_ptr. STR is the string to store. Do not copy
7181 characters that yield more columns than PRECISION; PRECISION <= 0
7182 means copy the whole string. Pad with spaces until FIELD_WIDTH
7183 number of characters have been copied; FIELD_WIDTH <= 0 means don't
7184 pad. Called from display_mode_element when it is used to build a
7185 frame title. */
7186
7187 static int
7188 store_frame_title (str, field_width, precision)
7189 unsigned char *str;
7190 int field_width, precision;
7191 {
7192 int n = 0;
7193 int dummy, nbytes;
7194
7195 /* Copy at most PRECISION chars from STR. */
7196 nbytes = strlen (str);
7197 n+= c_string_width (str, nbytes, precision, &dummy, &nbytes);
7198 while (nbytes--)
7199 store_frame_title_char (*str++);
7200
7201 /* Fill up with spaces until FIELD_WIDTH reached. */
7202 while (field_width > 0
7203 && n < field_width)
7204 {
7205 store_frame_title_char (' ');
7206 ++n;
7207 }
7208
7209 return n;
7210 }
7211
7212
7213 /* Set the title of FRAME, if it has changed. The title format is
7214 Vicon_title_format if FRAME is iconified, otherwise it is
7215 frame_title_format. */
7216
7217 static void
7218 x_consider_frame_title (frame)
7219 Lisp_Object frame;
7220 {
7221 struct frame *f = XFRAME (frame);
7222
7223 if (FRAME_WINDOW_P (f)
7224 || FRAME_MINIBUF_ONLY_P (f)
7225 || f->explicit_name)
7226 {
7227 /* Do we have more than one visible frame on this X display? */
7228 Lisp_Object tail;
7229 Lisp_Object fmt;
7230 struct buffer *obuf;
7231 int len;
7232 struct it it;
7233
7234 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
7235 {
7236 Lisp_Object other_frame = XCAR (tail);
7237 struct frame *tf = XFRAME (other_frame);
7238
7239 if (tf != f
7240 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
7241 && !FRAME_MINIBUF_ONLY_P (tf)
7242 && !EQ (other_frame, tip_frame)
7243 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
7244 break;
7245 }
7246
7247 /* Set global variable indicating that multiple frames exist. */
7248 multiple_frames = CONSP (tail);
7249
7250 /* Switch to the buffer of selected window of the frame. Set up
7251 frame_title_ptr so that display_mode_element will output into it;
7252 then display the title. */
7253 obuf = current_buffer;
7254 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
7255 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
7256 frame_title_ptr = frame_title_buf;
7257 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
7258 NULL, DEFAULT_FACE_ID);
7259 display_mode_element (&it, 0, -1, -1, fmt, Qnil);
7260 len = frame_title_ptr - frame_title_buf;
7261 frame_title_ptr = NULL;
7262 set_buffer_internal_1 (obuf);
7263
7264 /* Set the title only if it's changed. This avoids consing in
7265 the common case where it hasn't. (If it turns out that we've
7266 already wasted too much time by walking through the list with
7267 display_mode_element, then we might need to optimize at a
7268 higher level than this.) */
7269 if (! STRINGP (f->name)
7270 || STRING_BYTES (XSTRING (f->name)) != len
7271 || bcmp (frame_title_buf, XSTRING (f->name)->data, len) != 0)
7272 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
7273 }
7274 }
7275
7276 #else /* not HAVE_WINDOW_SYSTEM */
7277
7278 #define frame_title_ptr ((char *)0)
7279 #define store_frame_title(str, mincol, maxcol) 0
7280
7281 #endif /* not HAVE_WINDOW_SYSTEM */
7282
7283
7284
7285 \f
7286 /***********************************************************************
7287 Menu Bars
7288 ***********************************************************************/
7289
7290
7291 /* Prepare for redisplay by updating menu-bar item lists when
7292 appropriate. This can call eval. */
7293
7294 void
7295 prepare_menu_bars ()
7296 {
7297 int all_windows;
7298 struct gcpro gcpro1, gcpro2;
7299 struct frame *f;
7300 Lisp_Object tooltip_frame;
7301
7302 #ifdef HAVE_WINDOW_SYSTEM
7303 tooltip_frame = tip_frame;
7304 #else
7305 tooltip_frame = Qnil;
7306 #endif
7307
7308 /* Update all frame titles based on their buffer names, etc. We do
7309 this before the menu bars so that the buffer-menu will show the
7310 up-to-date frame titles. */
7311 #ifdef HAVE_WINDOW_SYSTEM
7312 if (windows_or_buffers_changed || update_mode_lines)
7313 {
7314 Lisp_Object tail, frame;
7315
7316 FOR_EACH_FRAME (tail, frame)
7317 {
7318 f = XFRAME (frame);
7319 if (!EQ (frame, tooltip_frame)
7320 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
7321 x_consider_frame_title (frame);
7322 }
7323 }
7324 #endif /* HAVE_WINDOW_SYSTEM */
7325
7326 /* Update the menu bar item lists, if appropriate. This has to be
7327 done before any actual redisplay or generation of display lines. */
7328 all_windows = (update_mode_lines
7329 || buffer_shared > 1
7330 || windows_or_buffers_changed);
7331 if (all_windows)
7332 {
7333 Lisp_Object tail, frame;
7334 int count = BINDING_STACK_SIZE ();
7335
7336 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7337
7338 FOR_EACH_FRAME (tail, frame)
7339 {
7340 f = XFRAME (frame);
7341
7342 /* Ignore tooltip frame. */
7343 if (EQ (frame, tooltip_frame))
7344 continue;
7345
7346 /* If a window on this frame changed size, report that to
7347 the user and clear the size-change flag. */
7348 if (FRAME_WINDOW_SIZES_CHANGED (f))
7349 {
7350 Lisp_Object functions;
7351
7352 /* Clear flag first in case we get an error below. */
7353 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
7354 functions = Vwindow_size_change_functions;
7355 GCPRO2 (tail, functions);
7356
7357 while (CONSP (functions))
7358 {
7359 call1 (XCAR (functions), frame);
7360 functions = XCDR (functions);
7361 }
7362 UNGCPRO;
7363 }
7364
7365 GCPRO1 (tail);
7366 update_menu_bar (f, 0);
7367 #ifdef HAVE_WINDOW_SYSTEM
7368 update_tool_bar (f, 0);
7369 #endif
7370 UNGCPRO;
7371 }
7372
7373 unbind_to (count, Qnil);
7374 }
7375 else
7376 {
7377 struct frame *sf = SELECTED_FRAME ();
7378 update_menu_bar (sf, 1);
7379 #ifdef HAVE_WINDOW_SYSTEM
7380 update_tool_bar (sf, 1);
7381 #endif
7382 }
7383
7384 /* Motif needs this. See comment in xmenu.c. Turn it off when
7385 pending_menu_activation is not defined. */
7386 #ifdef USE_X_TOOLKIT
7387 pending_menu_activation = 0;
7388 #endif
7389 }
7390
7391
7392 /* Update the menu bar item list for frame F. This has to be done
7393 before we start to fill in any display lines, because it can call
7394 eval.
7395
7396 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
7397
7398 static void
7399 update_menu_bar (f, save_match_data)
7400 struct frame *f;
7401 int save_match_data;
7402 {
7403 Lisp_Object window;
7404 register struct window *w;
7405
7406 /* If called recursively during a menu update, do nothing. This can
7407 happen when, for instance, an activate-menubar-hook causes a
7408 redisplay. */
7409 if (inhibit_menubar_update)
7410 return;
7411
7412 window = FRAME_SELECTED_WINDOW (f);
7413 w = XWINDOW (window);
7414
7415 if (update_mode_lines)
7416 w->update_mode_line = Qt;
7417
7418 if (FRAME_WINDOW_P (f)
7419 ?
7420 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
7421 FRAME_EXTERNAL_MENU_BAR (f)
7422 #else
7423 FRAME_MENU_BAR_LINES (f) > 0
7424 #endif
7425 : FRAME_MENU_BAR_LINES (f) > 0)
7426 {
7427 /* If the user has switched buffers or windows, we need to
7428 recompute to reflect the new bindings. But we'll
7429 recompute when update_mode_lines is set too; that means
7430 that people can use force-mode-line-update to request
7431 that the menu bar be recomputed. The adverse effect on
7432 the rest of the redisplay algorithm is about the same as
7433 windows_or_buffers_changed anyway. */
7434 if (windows_or_buffers_changed
7435 || !NILP (w->update_mode_line)
7436 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7437 < BUF_MODIFF (XBUFFER (w->buffer)))
7438 != !NILP (w->last_had_star))
7439 || ((!NILP (Vtransient_mark_mode)
7440 && !NILP (XBUFFER (w->buffer)->mark_active))
7441 != !NILP (w->region_showing)))
7442 {
7443 struct buffer *prev = current_buffer;
7444 int count = BINDING_STACK_SIZE ();
7445
7446 specbind (Qinhibit_menubar_update, Qt);
7447
7448 set_buffer_internal_1 (XBUFFER (w->buffer));
7449 if (save_match_data)
7450 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7451 if (NILP (Voverriding_local_map_menu_flag))
7452 {
7453 specbind (Qoverriding_terminal_local_map, Qnil);
7454 specbind (Qoverriding_local_map, Qnil);
7455 }
7456
7457 /* Run the Lucid hook. */
7458 safe_run_hooks (Qactivate_menubar_hook);
7459
7460 /* If it has changed current-menubar from previous value,
7461 really recompute the menu-bar from the value. */
7462 if (! NILP (Vlucid_menu_bar_dirty_flag))
7463 call0 (Qrecompute_lucid_menubar);
7464
7465 safe_run_hooks (Qmenu_bar_update_hook);
7466 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
7467
7468 /* Redisplay the menu bar in case we changed it. */
7469 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
7470 if (FRAME_WINDOW_P (f)
7471 #if defined (macintosh)
7472 /* All frames on Mac OS share the same menubar. So only the
7473 selected frame should be allowed to set it. */
7474 && f == SELECTED_FRAME ()
7475 #endif
7476 )
7477 set_frame_menubar (f, 0, 0);
7478 else
7479 /* On a terminal screen, the menu bar is an ordinary screen
7480 line, and this makes it get updated. */
7481 w->update_mode_line = Qt;
7482 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7483 /* In the non-toolkit version, the menu bar is an ordinary screen
7484 line, and this makes it get updated. */
7485 w->update_mode_line = Qt;
7486 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7487
7488 unbind_to (count, Qnil);
7489 set_buffer_internal_1 (prev);
7490 }
7491 }
7492 }
7493
7494
7495 \f
7496 /***********************************************************************
7497 Tool-bars
7498 ***********************************************************************/
7499
7500 #ifdef HAVE_WINDOW_SYSTEM
7501
7502 /* Update the tool-bar item list for frame F. This has to be done
7503 before we start to fill in any display lines. Called from
7504 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
7505 and restore it here. */
7506
7507 static void
7508 update_tool_bar (f, save_match_data)
7509 struct frame *f;
7510 int save_match_data;
7511 {
7512 if (WINDOWP (f->tool_bar_window)
7513 && XFASTINT (XWINDOW (f->tool_bar_window)->height) > 0)
7514 {
7515 Lisp_Object window;
7516 struct window *w;
7517
7518 window = FRAME_SELECTED_WINDOW (f);
7519 w = XWINDOW (window);
7520
7521 /* If the user has switched buffers or windows, we need to
7522 recompute to reflect the new bindings. But we'll
7523 recompute when update_mode_lines is set too; that means
7524 that people can use force-mode-line-update to request
7525 that the menu bar be recomputed. The adverse effect on
7526 the rest of the redisplay algorithm is about the same as
7527 windows_or_buffers_changed anyway. */
7528 if (windows_or_buffers_changed
7529 || !NILP (w->update_mode_line)
7530 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7531 < BUF_MODIFF (XBUFFER (w->buffer)))
7532 != !NILP (w->last_had_star))
7533 || ((!NILP (Vtransient_mark_mode)
7534 && !NILP (XBUFFER (w->buffer)->mark_active))
7535 != !NILP (w->region_showing)))
7536 {
7537 struct buffer *prev = current_buffer;
7538 int count = BINDING_STACK_SIZE ();
7539
7540 /* Set current_buffer to the buffer of the selected
7541 window of the frame, so that we get the right local
7542 keymaps. */
7543 set_buffer_internal_1 (XBUFFER (w->buffer));
7544
7545 /* Save match data, if we must. */
7546 if (save_match_data)
7547 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7548
7549 /* Make sure that we don't accidentally use bogus keymaps. */
7550 if (NILP (Voverriding_local_map_menu_flag))
7551 {
7552 specbind (Qoverriding_terminal_local_map, Qnil);
7553 specbind (Qoverriding_local_map, Qnil);
7554 }
7555
7556 /* Build desired tool-bar items from keymaps. */
7557 f->tool_bar_items
7558 = tool_bar_items (f->tool_bar_items, &f->n_tool_bar_items);
7559
7560 /* Redisplay the tool-bar in case we changed it. */
7561 w->update_mode_line = Qt;
7562
7563 unbind_to (count, Qnil);
7564 set_buffer_internal_1 (prev);
7565 }
7566 }
7567 }
7568
7569
7570 /* Set F->desired_tool_bar_string to a Lisp string representing frame
7571 F's desired tool-bar contents. F->tool_bar_items must have
7572 been set up previously by calling prepare_menu_bars. */
7573
7574 static void
7575 build_desired_tool_bar_string (f)
7576 struct frame *f;
7577 {
7578 int i, size, size_needed;
7579 struct gcpro gcpro1, gcpro2, gcpro3;
7580 Lisp_Object image, plist, props;
7581
7582 image = plist = props = Qnil;
7583 GCPRO3 (image, plist, props);
7584
7585 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
7586 Otherwise, make a new string. */
7587
7588 /* The size of the string we might be able to reuse. */
7589 size = (STRINGP (f->desired_tool_bar_string)
7590 ? XSTRING (f->desired_tool_bar_string)->size
7591 : 0);
7592
7593 /* We need one space in the string for each image. */
7594 size_needed = f->n_tool_bar_items;
7595
7596 /* Reuse f->desired_tool_bar_string, if possible. */
7597 if (size < size_needed || NILP (f->desired_tool_bar_string))
7598 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
7599 make_number (' '));
7600 else
7601 {
7602 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
7603 Fremove_text_properties (make_number (0), make_number (size),
7604 props, f->desired_tool_bar_string);
7605 }
7606
7607 /* Put a `display' property on the string for the images to display,
7608 put a `menu_item' property on tool-bar items with a value that
7609 is the index of the item in F's tool-bar item vector. */
7610 for (i = 0; i < f->n_tool_bar_items; ++i)
7611 {
7612 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
7613
7614 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
7615 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
7616 int hmargin, vmargin, relief, idx, end;
7617 extern Lisp_Object QCrelief, QCmargin, QCconversion, Qimage;
7618
7619 /* If image is a vector, choose the image according to the
7620 button state. */
7621 image = PROP (TOOL_BAR_ITEM_IMAGES);
7622 if (VECTORP (image))
7623 {
7624 if (enabled_p)
7625 idx = (selected_p
7626 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
7627 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
7628 else
7629 idx = (selected_p
7630 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
7631 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
7632
7633 xassert (ASIZE (image) >= idx);
7634 image = AREF (image, idx);
7635 }
7636 else
7637 idx = -1;
7638
7639 /* Ignore invalid image specifications. */
7640 if (!valid_image_p (image))
7641 continue;
7642
7643 /* Display the tool-bar button pressed, or depressed. */
7644 plist = Fcopy_sequence (XCDR (image));
7645
7646 /* Compute margin and relief to draw. */
7647 relief = (tool_bar_button_relief >= 0
7648 ? tool_bar_button_relief
7649 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
7650 hmargin = vmargin = relief;
7651
7652 if (INTEGERP (Vtool_bar_button_margin)
7653 && XINT (Vtool_bar_button_margin) > 0)
7654 {
7655 hmargin += XFASTINT (Vtool_bar_button_margin);
7656 vmargin += XFASTINT (Vtool_bar_button_margin);
7657 }
7658 else if (CONSP (Vtool_bar_button_margin))
7659 {
7660 if (INTEGERP (XCAR (Vtool_bar_button_margin))
7661 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
7662 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
7663
7664 if (INTEGERP (XCDR (Vtool_bar_button_margin))
7665 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
7666 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
7667 }
7668
7669 if (auto_raise_tool_bar_buttons_p)
7670 {
7671 /* Add a `:relief' property to the image spec if the item is
7672 selected. */
7673 if (selected_p)
7674 {
7675 plist = Fplist_put (plist, QCrelief, make_number (-relief));
7676 hmargin -= relief;
7677 vmargin -= relief;
7678 }
7679 }
7680 else
7681 {
7682 /* If image is selected, display it pressed, i.e. with a
7683 negative relief. If it's not selected, display it with a
7684 raised relief. */
7685 plist = Fplist_put (plist, QCrelief,
7686 (selected_p
7687 ? make_number (-relief)
7688 : make_number (relief)));
7689 hmargin -= relief;
7690 vmargin -= relief;
7691 }
7692
7693 /* Put a margin around the image. */
7694 if (hmargin || vmargin)
7695 {
7696 if (hmargin == vmargin)
7697 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
7698 else
7699 plist = Fplist_put (plist, QCmargin,
7700 Fcons (make_number (hmargin),
7701 make_number (vmargin)));
7702 }
7703
7704 /* If button is not enabled, and we don't have special images
7705 for the disabled state, make the image appear disabled by
7706 applying an appropriate algorithm to it. */
7707 if (!enabled_p && idx < 0)
7708 plist = Fplist_put (plist, QCconversion, Qdisabled);
7709
7710 /* Put a `display' text property on the string for the image to
7711 display. Put a `menu-item' property on the string that gives
7712 the start of this item's properties in the tool-bar items
7713 vector. */
7714 image = Fcons (Qimage, plist);
7715 props = list4 (Qdisplay, image,
7716 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
7717
7718 /* Let the last image hide all remaining spaces in the tool bar
7719 string. The string can be longer than needed when we reuse a
7720 previous string. */
7721 if (i + 1 == f->n_tool_bar_items)
7722 end = XSTRING (f->desired_tool_bar_string)->size;
7723 else
7724 end = i + 1;
7725 Fadd_text_properties (make_number (i), make_number (end),
7726 props, f->desired_tool_bar_string);
7727 #undef PROP
7728 }
7729
7730 UNGCPRO;
7731 }
7732
7733
7734 /* Display one line of the tool-bar of frame IT->f. */
7735
7736 static void
7737 display_tool_bar_line (it)
7738 struct it *it;
7739 {
7740 struct glyph_row *row = it->glyph_row;
7741 int max_x = it->last_visible_x;
7742 struct glyph *last;
7743
7744 prepare_desired_row (row);
7745 row->y = it->current_y;
7746
7747 /* Note that this isn't made use of if the face hasn't a box,
7748 so there's no need to check the face here. */
7749 it->start_of_box_run_p = 1;
7750
7751 while (it->current_x < max_x)
7752 {
7753 int x_before, x, n_glyphs_before, i, nglyphs;
7754
7755 /* Get the next display element. */
7756 if (!get_next_display_element (it))
7757 break;
7758
7759 /* Produce glyphs. */
7760 x_before = it->current_x;
7761 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
7762 PRODUCE_GLYPHS (it);
7763
7764 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
7765 i = 0;
7766 x = x_before;
7767 while (i < nglyphs)
7768 {
7769 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
7770
7771 if (x + glyph->pixel_width > max_x)
7772 {
7773 /* Glyph doesn't fit on line. */
7774 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
7775 it->current_x = x;
7776 goto out;
7777 }
7778
7779 ++it->hpos;
7780 x += glyph->pixel_width;
7781 ++i;
7782 }
7783
7784 /* Stop at line ends. */
7785 if (ITERATOR_AT_END_OF_LINE_P (it))
7786 break;
7787
7788 set_iterator_to_next (it, 1);
7789 }
7790
7791 out:;
7792
7793 row->displays_text_p = row->used[TEXT_AREA] != 0;
7794 extend_face_to_end_of_line (it);
7795 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
7796 last->right_box_line_p = 1;
7797 if (last == row->glyphs[TEXT_AREA])
7798 last->left_box_line_p = 1;
7799 compute_line_metrics (it);
7800
7801 /* If line is empty, make it occupy the rest of the tool-bar. */
7802 if (!row->displays_text_p)
7803 {
7804 row->height = row->phys_height = it->last_visible_y - row->y;
7805 row->ascent = row->phys_ascent = 0;
7806 }
7807
7808 row->full_width_p = 1;
7809 row->continued_p = 0;
7810 row->truncated_on_left_p = 0;
7811 row->truncated_on_right_p = 0;
7812
7813 it->current_x = it->hpos = 0;
7814 it->current_y += row->height;
7815 ++it->vpos;
7816 ++it->glyph_row;
7817 }
7818
7819
7820 /* Value is the number of screen lines needed to make all tool-bar
7821 items of frame F visible. */
7822
7823 static int
7824 tool_bar_lines_needed (f)
7825 struct frame *f;
7826 {
7827 struct window *w = XWINDOW (f->tool_bar_window);
7828 struct it it;
7829
7830 /* Initialize an iterator for iteration over
7831 F->desired_tool_bar_string in the tool-bar window of frame F. */
7832 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7833 it.first_visible_x = 0;
7834 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7835 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7836
7837 while (!ITERATOR_AT_END_P (&it))
7838 {
7839 it.glyph_row = w->desired_matrix->rows;
7840 clear_glyph_row (it.glyph_row);
7841 display_tool_bar_line (&it);
7842 }
7843
7844 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
7845 }
7846
7847
7848 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
7849 0, 1, 0,
7850 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
7851 (frame)
7852 Lisp_Object frame;
7853 {
7854 struct frame *f;
7855 struct window *w;
7856 int nlines = 0;
7857
7858 if (NILP (frame))
7859 frame = selected_frame;
7860 else
7861 CHECK_FRAME (frame);
7862 f = XFRAME (frame);
7863
7864 if (WINDOWP (f->tool_bar_window)
7865 || (w = XWINDOW (f->tool_bar_window),
7866 XFASTINT (w->height) > 0))
7867 {
7868 update_tool_bar (f, 1);
7869 if (f->n_tool_bar_items)
7870 {
7871 build_desired_tool_bar_string (f);
7872 nlines = tool_bar_lines_needed (f);
7873 }
7874 }
7875
7876 return make_number (nlines);
7877 }
7878
7879
7880 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
7881 height should be changed. */
7882
7883 static int
7884 redisplay_tool_bar (f)
7885 struct frame *f;
7886 {
7887 struct window *w;
7888 struct it it;
7889 struct glyph_row *row;
7890 int change_height_p = 0;
7891
7892 /* If frame hasn't a tool-bar window or if it is zero-height, don't
7893 do anything. This means you must start with tool-bar-lines
7894 non-zero to get the auto-sizing effect. Or in other words, you
7895 can turn off tool-bars by specifying tool-bar-lines zero. */
7896 if (!WINDOWP (f->tool_bar_window)
7897 || (w = XWINDOW (f->tool_bar_window),
7898 XFASTINT (w->height) == 0))
7899 return 0;
7900
7901 /* Set up an iterator for the tool-bar window. */
7902 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7903 it.first_visible_x = 0;
7904 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7905 row = it.glyph_row;
7906
7907 /* Build a string that represents the contents of the tool-bar. */
7908 build_desired_tool_bar_string (f);
7909 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7910
7911 /* Display as many lines as needed to display all tool-bar items. */
7912 while (it.current_y < it.last_visible_y)
7913 display_tool_bar_line (&it);
7914
7915 /* It doesn't make much sense to try scrolling in the tool-bar
7916 window, so don't do it. */
7917 w->desired_matrix->no_scrolling_p = 1;
7918 w->must_be_updated_p = 1;
7919
7920 if (auto_resize_tool_bars_p)
7921 {
7922 int nlines;
7923
7924 /* If we couldn't display everything, change the tool-bar's
7925 height. */
7926 if (IT_STRING_CHARPOS (it) < it.end_charpos)
7927 change_height_p = 1;
7928
7929 /* If there are blank lines at the end, except for a partially
7930 visible blank line at the end that is smaller than
7931 CANON_Y_UNIT, change the tool-bar's height. */
7932 row = it.glyph_row - 1;
7933 if (!row->displays_text_p
7934 && row->height >= CANON_Y_UNIT (f))
7935 change_height_p = 1;
7936
7937 /* If row displays tool-bar items, but is partially visible,
7938 change the tool-bar's height. */
7939 if (row->displays_text_p
7940 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
7941 change_height_p = 1;
7942
7943 /* Resize windows as needed by changing the `tool-bar-lines'
7944 frame parameter. */
7945 if (change_height_p
7946 && (nlines = tool_bar_lines_needed (f),
7947 nlines != XFASTINT (w->height)))
7948 {
7949 extern Lisp_Object Qtool_bar_lines;
7950 Lisp_Object frame;
7951 int old_height = XFASTINT (w->height);
7952
7953 XSETFRAME (frame, f);
7954 clear_glyph_matrix (w->desired_matrix);
7955 Fmodify_frame_parameters (frame,
7956 Fcons (Fcons (Qtool_bar_lines,
7957 make_number (nlines)),
7958 Qnil));
7959 if (XFASTINT (w->height) != old_height)
7960 fonts_changed_p = 1;
7961 }
7962 }
7963
7964 return change_height_p;
7965 }
7966
7967
7968 /* Get information about the tool-bar item which is displayed in GLYPH
7969 on frame F. Return in *PROP_IDX the index where tool-bar item
7970 properties start in F->tool_bar_items. Value is zero if
7971 GLYPH doesn't display a tool-bar item. */
7972
7973 int
7974 tool_bar_item_info (f, glyph, prop_idx)
7975 struct frame *f;
7976 struct glyph *glyph;
7977 int *prop_idx;
7978 {
7979 Lisp_Object prop;
7980 int success_p;
7981 int charpos;
7982
7983 /* This function can be called asynchronously, which means we must
7984 exclude any possibility that Fget_text_property signals an
7985 error. */
7986 charpos = min (XSTRING (f->current_tool_bar_string)->size, glyph->charpos);
7987 charpos = max (0, charpos);
7988
7989 /* Get the text property `menu-item' at pos. The value of that
7990 property is the start index of this item's properties in
7991 F->tool_bar_items. */
7992 prop = Fget_text_property (make_number (charpos),
7993 Qmenu_item, f->current_tool_bar_string);
7994 if (INTEGERP (prop))
7995 {
7996 *prop_idx = XINT (prop);
7997 success_p = 1;
7998 }
7999 else
8000 success_p = 0;
8001
8002 return success_p;
8003 }
8004
8005 #endif /* HAVE_WINDOW_SYSTEM */
8006
8007
8008 \f
8009 /************************************************************************
8010 Horizontal scrolling
8011 ************************************************************************/
8012
8013 static int hscroll_window_tree P_ ((Lisp_Object));
8014 static int hscroll_windows P_ ((Lisp_Object));
8015
8016 /* For all leaf windows in the window tree rooted at WINDOW, set their
8017 hscroll value so that PT is (i) visible in the window, and (ii) so
8018 that it is not within a certain margin at the window's left and
8019 right border. Value is non-zero if any window's hscroll has been
8020 changed. */
8021
8022 static int
8023 hscroll_window_tree (window)
8024 Lisp_Object window;
8025 {
8026 int hscrolled_p = 0;
8027 int hscroll_relative_p = FLOATP (Vautomatic_hscroll_step);
8028 int hscroll_step_abs = 0;
8029 double hscroll_step_rel = 0;
8030
8031 if (hscroll_relative_p)
8032 {
8033 hscroll_step_rel = XFLOAT_DATA (Vautomatic_hscroll_step);
8034 if (hscroll_step_rel < 0)
8035 {
8036 hscroll_relative_p = 0;
8037 hscroll_step_abs = 0;
8038 }
8039 }
8040 else if (INTEGERP (Vautomatic_hscroll_step))
8041 {
8042 hscroll_step_abs = XINT (Vautomatic_hscroll_step);
8043 if (hscroll_step_abs < 0)
8044 hscroll_step_abs = 0;
8045 }
8046 else
8047 hscroll_step_abs = 0;
8048
8049 while (WINDOWP (window))
8050 {
8051 struct window *w = XWINDOW (window);
8052
8053 if (WINDOWP (w->hchild))
8054 hscrolled_p |= hscroll_window_tree (w->hchild);
8055 else if (WINDOWP (w->vchild))
8056 hscrolled_p |= hscroll_window_tree (w->vchild);
8057 else if (w->cursor.vpos >= 0)
8058 {
8059 int hscroll_margin, text_area_x, text_area_y;
8060 int text_area_width, text_area_height;
8061 struct glyph_row *current_cursor_row
8062 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
8063 struct glyph_row *desired_cursor_row
8064 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
8065 struct glyph_row *cursor_row
8066 = (desired_cursor_row->enabled_p
8067 ? desired_cursor_row
8068 : current_cursor_row);
8069
8070 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
8071 &text_area_width, &text_area_height);
8072
8073 /* Scroll when cursor is inside this scroll margin. */
8074 hscroll_margin
8075 = automatic_hscroll_margin * CANON_X_UNIT (XFRAME (w->frame));
8076
8077 if ((XFASTINT (w->hscroll)
8078 && w->cursor.x <= hscroll_margin)
8079 || (cursor_row->enabled_p
8080 && cursor_row->truncated_on_right_p
8081 && (w->cursor.x >= text_area_width - hscroll_margin)))
8082 {
8083 struct it it;
8084 int hscroll;
8085 struct buffer *saved_current_buffer;
8086 int pt;
8087 int wanted_x;
8088
8089 /* Find point in a display of infinite width. */
8090 saved_current_buffer = current_buffer;
8091 current_buffer = XBUFFER (w->buffer);
8092
8093 if (w == XWINDOW (selected_window))
8094 pt = BUF_PT (current_buffer);
8095 else
8096 {
8097 pt = marker_position (w->pointm);
8098 pt = max (BEGV, pt);
8099 pt = min (ZV, pt);
8100 }
8101
8102 /* Move iterator to pt starting at cursor_row->start in
8103 a line with infinite width. */
8104 init_to_row_start (&it, w, cursor_row);
8105 it.last_visible_x = INFINITY;
8106 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
8107 current_buffer = saved_current_buffer;
8108
8109 /* Position cursor in window. */
8110 if (!hscroll_relative_p && hscroll_step_abs == 0)
8111 hscroll = max (0, it.current_x - text_area_width / 2)
8112 / CANON_X_UNIT (it.f);
8113 else if (w->cursor.x >= text_area_width - hscroll_margin)
8114 {
8115 if (hscroll_relative_p)
8116 wanted_x = text_area_width * (1 - hscroll_step_rel)
8117 - hscroll_margin;
8118 else
8119 wanted_x = text_area_width
8120 - hscroll_step_abs * CANON_X_UNIT (it.f)
8121 - hscroll_margin;
8122 hscroll
8123 = max (0, it.current_x - wanted_x) / CANON_X_UNIT (it.f);
8124 }
8125 else
8126 {
8127 if (hscroll_relative_p)
8128 wanted_x = text_area_width * hscroll_step_rel
8129 + hscroll_margin;
8130 else
8131 wanted_x = hscroll_step_abs * CANON_X_UNIT (it.f)
8132 + hscroll_margin;
8133 hscroll
8134 = max (0, it.current_x - wanted_x) / CANON_X_UNIT (it.f);
8135 }
8136 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
8137
8138 /* Don't call Fset_window_hscroll if value hasn't
8139 changed because it will prevent redisplay
8140 optimizations. */
8141 if (XFASTINT (w->hscroll) != hscroll)
8142 {
8143 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
8144 w->hscroll = make_number (hscroll);
8145 hscrolled_p = 1;
8146 }
8147 }
8148 }
8149
8150 window = w->next;
8151 }
8152
8153 /* Value is non-zero if hscroll of any leaf window has been changed. */
8154 return hscrolled_p;
8155 }
8156
8157
8158 /* Set hscroll so that cursor is visible and not inside horizontal
8159 scroll margins for all windows in the tree rooted at WINDOW. See
8160 also hscroll_window_tree above. Value is non-zero if any window's
8161 hscroll has been changed. If it has, desired matrices on the frame
8162 of WINDOW are cleared. */
8163
8164 static int
8165 hscroll_windows (window)
8166 Lisp_Object window;
8167 {
8168 int hscrolled_p;
8169
8170 if (automatic_hscrolling_p)
8171 {
8172 hscrolled_p = hscroll_window_tree (window);
8173 if (hscrolled_p)
8174 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
8175 }
8176 else
8177 hscrolled_p = 0;
8178 return hscrolled_p;
8179 }
8180
8181
8182 \f
8183 /************************************************************************
8184 Redisplay
8185 ************************************************************************/
8186
8187 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
8188 to a non-zero value. This is sometimes handy to have in a debugger
8189 session. */
8190
8191 #if GLYPH_DEBUG
8192
8193 /* First and last unchanged row for try_window_id. */
8194
8195 int debug_first_unchanged_at_end_vpos;
8196 int debug_last_unchanged_at_beg_vpos;
8197
8198 /* Delta vpos and y. */
8199
8200 int debug_dvpos, debug_dy;
8201
8202 /* Delta in characters and bytes for try_window_id. */
8203
8204 int debug_delta, debug_delta_bytes;
8205
8206 /* Values of window_end_pos and window_end_vpos at the end of
8207 try_window_id. */
8208
8209 int debug_end_pos, debug_end_vpos;
8210
8211 /* Append a string to W->desired_matrix->method. FMT is a printf
8212 format string. A1...A9 are a supplement for a variable-length
8213 argument list. If trace_redisplay_p is non-zero also printf the
8214 resulting string to stderr. */
8215
8216 static void
8217 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
8218 struct window *w;
8219 char *fmt;
8220 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
8221 {
8222 char buffer[512];
8223 char *method = w->desired_matrix->method;
8224 int len = strlen (method);
8225 int size = sizeof w->desired_matrix->method;
8226 int remaining = size - len - 1;
8227
8228 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
8229 if (len && remaining)
8230 {
8231 method[len] = '|';
8232 --remaining, ++len;
8233 }
8234
8235 strncpy (method + len, buffer, remaining);
8236
8237 if (trace_redisplay_p)
8238 fprintf (stderr, "%p (%s): %s\n",
8239 w,
8240 ((BUFFERP (w->buffer)
8241 && STRINGP (XBUFFER (w->buffer)->name))
8242 ? (char *) XSTRING (XBUFFER (w->buffer)->name)->data
8243 : "no buffer"),
8244 buffer);
8245 }
8246
8247 #endif /* GLYPH_DEBUG */
8248
8249
8250 /* This counter is used to clear the face cache every once in a while
8251 in redisplay_internal. It is incremented for each redisplay.
8252 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
8253 cleared. */
8254
8255 #define CLEAR_FACE_CACHE_COUNT 500
8256 static int clear_face_cache_count;
8257
8258 /* Record the previous terminal frame we displayed. */
8259
8260 static struct frame *previous_terminal_frame;
8261
8262 /* Non-zero while redisplay_internal is in progress. */
8263
8264 int redisplaying_p;
8265
8266
8267 /* Value is non-zero if all changes in window W, which displays
8268 current_buffer, are in the text between START and END. START is a
8269 buffer position, END is given as a distance from Z. Used in
8270 redisplay_internal for display optimization. */
8271
8272 static INLINE int
8273 text_outside_line_unchanged_p (w, start, end)
8274 struct window *w;
8275 int start, end;
8276 {
8277 int unchanged_p = 1;
8278
8279 /* If text or overlays have changed, see where. */
8280 if (XFASTINT (w->last_modified) < MODIFF
8281 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8282 {
8283 /* Gap in the line? */
8284 if (GPT < start || Z - GPT < end)
8285 unchanged_p = 0;
8286
8287 /* Changes start in front of the line, or end after it? */
8288 if (unchanged_p
8289 && (BEG_UNCHANGED < start - 1
8290 || END_UNCHANGED < end))
8291 unchanged_p = 0;
8292
8293 /* If selective display, can't optimize if changes start at the
8294 beginning of the line. */
8295 if (unchanged_p
8296 && INTEGERP (current_buffer->selective_display)
8297 && XINT (current_buffer->selective_display) > 0
8298 && (BEG_UNCHANGED < start || GPT <= start))
8299 unchanged_p = 0;
8300
8301 /* If there are overlays at the start or end of the line, these
8302 may have overlay strings with newlines in them. A change at
8303 START, for instance, may actually concern the display of such
8304 overlay strings as well, and they are displayed on different
8305 lines. So, quickly rule out this case. (For the future, it
8306 might be desirable to implement something more telling than
8307 just BEG/END_UNCHANGED.) */
8308 if (unchanged_p)
8309 {
8310 if (BEG + BEG_UNCHANGED == start
8311 && overlay_touches_p (start))
8312 unchanged_p = 0;
8313 if (END_UNCHANGED == end
8314 && overlay_touches_p (Z - end))
8315 unchanged_p = 0;
8316 }
8317 }
8318
8319 return unchanged_p;
8320 }
8321
8322
8323 /* Do a frame update, taking possible shortcuts into account. This is
8324 the main external entry point for redisplay.
8325
8326 If the last redisplay displayed an echo area message and that message
8327 is no longer requested, we clear the echo area or bring back the
8328 mini-buffer if that is in use. */
8329
8330 void
8331 redisplay ()
8332 {
8333 redisplay_internal (0);
8334 }
8335
8336
8337 /* Return 1 if point moved out of or into a composition. Otherwise
8338 return 0. PREV_BUF and PREV_PT are the last point buffer and
8339 position. BUF and PT are the current point buffer and position. */
8340
8341 int
8342 check_point_in_composition (prev_buf, prev_pt, buf, pt)
8343 struct buffer *prev_buf, *buf;
8344 int prev_pt, pt;
8345 {
8346 int start, end;
8347 Lisp_Object prop;
8348 Lisp_Object buffer;
8349
8350 XSETBUFFER (buffer, buf);
8351 /* Check a composition at the last point if point moved within the
8352 same buffer. */
8353 if (prev_buf == buf)
8354 {
8355 if (prev_pt == pt)
8356 /* Point didn't move. */
8357 return 0;
8358
8359 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
8360 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
8361 && COMPOSITION_VALID_P (start, end, prop)
8362 && start < prev_pt && end > prev_pt)
8363 /* The last point was within the composition. Return 1 iff
8364 point moved out of the composition. */
8365 return (pt <= start || pt >= end);
8366 }
8367
8368 /* Check a composition at the current point. */
8369 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
8370 && find_composition (pt, -1, &start, &end, &prop, buffer)
8371 && COMPOSITION_VALID_P (start, end, prop)
8372 && start < pt && end > pt);
8373 }
8374
8375
8376 /* Reconsider the setting of B->clip_changed which is displayed
8377 in window W. */
8378
8379 static INLINE void
8380 reconsider_clip_changes (w, b)
8381 struct window *w;
8382 struct buffer *b;
8383 {
8384 if (b->prevent_redisplay_optimizations_p)
8385 b->clip_changed = 1;
8386 else if (b->clip_changed
8387 && !NILP (w->window_end_valid)
8388 && w->current_matrix->buffer == b
8389 && w->current_matrix->zv == BUF_ZV (b)
8390 && w->current_matrix->begv == BUF_BEGV (b))
8391 b->clip_changed = 0;
8392
8393 /* If display wasn't paused, and W is not a tool bar window, see if
8394 point has been moved into or out of a composition. In that case,
8395 we set b->clip_changed to 1 to force updating the screen. If
8396 b->clip_changed has already been set to 1, we can skip this
8397 check. */
8398 if (!b->clip_changed
8399 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
8400 {
8401 int pt;
8402
8403 if (w == XWINDOW (selected_window))
8404 pt = BUF_PT (current_buffer);
8405 else
8406 pt = marker_position (w->pointm);
8407
8408 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
8409 || pt != XINT (w->last_point))
8410 && check_point_in_composition (w->current_matrix->buffer,
8411 XINT (w->last_point),
8412 XBUFFER (w->buffer), pt))
8413 b->clip_changed = 1;
8414 }
8415 }
8416
8417
8418 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
8419 response to any user action; therefore, we should preserve the echo
8420 area. (Actually, our caller does that job.) Perhaps in the future
8421 avoid recentering windows if it is not necessary; currently that
8422 causes some problems. */
8423
8424 static void
8425 redisplay_internal (preserve_echo_area)
8426 int preserve_echo_area;
8427 {
8428 struct window *w = XWINDOW (selected_window);
8429 struct frame *f = XFRAME (w->frame);
8430 int pause;
8431 int must_finish = 0;
8432 struct text_pos tlbufpos, tlendpos;
8433 int number_of_visible_frames;
8434 int count;
8435 struct frame *sf = SELECTED_FRAME ();
8436
8437 /* Non-zero means redisplay has to consider all windows on all
8438 frames. Zero means, only selected_window is considered. */
8439 int consider_all_windows_p;
8440
8441 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
8442
8443 /* No redisplay if running in batch mode or frame is not yet fully
8444 initialized, or redisplay is explicitly turned off by setting
8445 Vinhibit_redisplay. */
8446 if (noninteractive
8447 || !NILP (Vinhibit_redisplay)
8448 || !f->glyphs_initialized_p)
8449 return;
8450
8451 /* The flag redisplay_performed_directly_p is set by
8452 direct_output_for_insert when it already did the whole screen
8453 update necessary. */
8454 if (redisplay_performed_directly_p)
8455 {
8456 redisplay_performed_directly_p = 0;
8457 if (!hscroll_windows (selected_window))
8458 return;
8459 }
8460
8461 #ifdef USE_X_TOOLKIT
8462 if (popup_activated ())
8463 return;
8464 #endif
8465
8466 /* I don't think this happens but let's be paranoid. */
8467 if (redisplaying_p)
8468 return;
8469
8470 /* Record a function that resets redisplaying_p to its old value
8471 when we leave this function. */
8472 count = BINDING_STACK_SIZE ();
8473 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
8474 ++redisplaying_p;
8475
8476 retry:
8477 pause = 0;
8478 reconsider_clip_changes (w, current_buffer);
8479
8480 /* If new fonts have been loaded that make a glyph matrix adjustment
8481 necessary, do it. */
8482 if (fonts_changed_p)
8483 {
8484 adjust_glyphs (NULL);
8485 ++windows_or_buffers_changed;
8486 fonts_changed_p = 0;
8487 }
8488
8489 /* If face_change_count is non-zero, init_iterator will free all
8490 realized faces, which includes the faces referenced from current
8491 matrices. So, we can't reuse current matrices in this case. */
8492 if (face_change_count)
8493 ++windows_or_buffers_changed;
8494
8495 if (! FRAME_WINDOW_P (sf)
8496 && previous_terminal_frame != sf)
8497 {
8498 /* Since frames on an ASCII terminal share the same display
8499 area, displaying a different frame means redisplay the whole
8500 thing. */
8501 windows_or_buffers_changed++;
8502 SET_FRAME_GARBAGED (sf);
8503 XSETFRAME (Vterminal_frame, sf);
8504 }
8505 previous_terminal_frame = sf;
8506
8507 /* Set the visible flags for all frames. Do this before checking
8508 for resized or garbaged frames; they want to know if their frames
8509 are visible. See the comment in frame.h for
8510 FRAME_SAMPLE_VISIBILITY. */
8511 {
8512 Lisp_Object tail, frame;
8513
8514 number_of_visible_frames = 0;
8515
8516 FOR_EACH_FRAME (tail, frame)
8517 {
8518 struct frame *f = XFRAME (frame);
8519
8520 FRAME_SAMPLE_VISIBILITY (f);
8521 if (FRAME_VISIBLE_P (f))
8522 ++number_of_visible_frames;
8523 clear_desired_matrices (f);
8524 }
8525 }
8526
8527 /* Notice any pending interrupt request to change frame size. */
8528 do_pending_window_change (1);
8529
8530 /* Clear frames marked as garbaged. */
8531 if (frame_garbaged)
8532 clear_garbaged_frames ();
8533
8534 /* Build menubar and tool-bar items. */
8535 prepare_menu_bars ();
8536
8537 if (windows_or_buffers_changed)
8538 update_mode_lines++;
8539
8540 /* Detect case that we need to write or remove a star in the mode line. */
8541 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
8542 {
8543 w->update_mode_line = Qt;
8544 if (buffer_shared > 1)
8545 update_mode_lines++;
8546 }
8547
8548 /* If %c is in the mode line, update it if needed. */
8549 if (!NILP (w->column_number_displayed)
8550 /* This alternative quickly identifies a common case
8551 where no change is needed. */
8552 && !(PT == XFASTINT (w->last_point)
8553 && XFASTINT (w->last_modified) >= MODIFF
8554 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
8555 && XFASTINT (w->column_number_displayed) != current_column ())
8556 w->update_mode_line = Qt;
8557
8558 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
8559
8560 /* The variable buffer_shared is set in redisplay_window and
8561 indicates that we redisplay a buffer in different windows. See
8562 there. */
8563 consider_all_windows_p = update_mode_lines || buffer_shared > 1;
8564
8565 /* If specs for an arrow have changed, do thorough redisplay
8566 to ensure we remove any arrow that should no longer exist. */
8567 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
8568 || ! EQ (Voverlay_arrow_string, last_arrow_string))
8569 consider_all_windows_p = windows_or_buffers_changed = 1;
8570
8571 /* Normally the message* functions will have already displayed and
8572 updated the echo area, but the frame may have been trashed, or
8573 the update may have been preempted, so display the echo area
8574 again here. Checking message_cleared_p captures the case that
8575 the echo area should be cleared. */
8576 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
8577 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
8578 || (message_cleared_p
8579 && minibuf_level == 0
8580 /* If the mini-window is currently selected, this means the
8581 echo-area doesn't show through. */
8582 && !MINI_WINDOW_P (XWINDOW (selected_window))))
8583 {
8584 int window_height_changed_p = echo_area_display (0);
8585 must_finish = 1;
8586
8587 /* If we don't display the current message, don't clear the
8588 message_cleared_p flag, because, if we did, we wouldn't clear
8589 the echo area in the next redisplay which doesn't preserve
8590 the echo area. */
8591 if (!display_last_displayed_message_p)
8592 message_cleared_p = 0;
8593
8594 if (fonts_changed_p)
8595 goto retry;
8596 else if (window_height_changed_p)
8597 {
8598 consider_all_windows_p = 1;
8599 ++update_mode_lines;
8600 ++windows_or_buffers_changed;
8601
8602 /* If window configuration was changed, frames may have been
8603 marked garbaged. Clear them or we will experience
8604 surprises wrt scrolling. */
8605 if (frame_garbaged)
8606 clear_garbaged_frames ();
8607 }
8608 }
8609 else if (EQ (selected_window, minibuf_window)
8610 && (current_buffer->clip_changed
8611 || XFASTINT (w->last_modified) < MODIFF
8612 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8613 && resize_mini_window (w, 0))
8614 {
8615 /* Resized active mini-window to fit the size of what it is
8616 showing if its contents might have changed. */
8617 must_finish = 1;
8618 consider_all_windows_p = 1;
8619 ++windows_or_buffers_changed;
8620 ++update_mode_lines;
8621
8622 /* If window configuration was changed, frames may have been
8623 marked garbaged. Clear them or we will experience
8624 surprises wrt scrolling. */
8625 if (frame_garbaged)
8626 clear_garbaged_frames ();
8627 }
8628
8629
8630 /* If showing the region, and mark has changed, we must redisplay
8631 the whole window. The assignment to this_line_start_pos prevents
8632 the optimization directly below this if-statement. */
8633 if (((!NILP (Vtransient_mark_mode)
8634 && !NILP (XBUFFER (w->buffer)->mark_active))
8635 != !NILP (w->region_showing))
8636 || (!NILP (w->region_showing)
8637 && !EQ (w->region_showing,
8638 Fmarker_position (XBUFFER (w->buffer)->mark))))
8639 CHARPOS (this_line_start_pos) = 0;
8640
8641 /* Optimize the case that only the line containing the cursor in the
8642 selected window has changed. Variables starting with this_ are
8643 set in display_line and record information about the line
8644 containing the cursor. */
8645 tlbufpos = this_line_start_pos;
8646 tlendpos = this_line_end_pos;
8647 if (!consider_all_windows_p
8648 && CHARPOS (tlbufpos) > 0
8649 && NILP (w->update_mode_line)
8650 && !current_buffer->clip_changed
8651 && FRAME_VISIBLE_P (XFRAME (w->frame))
8652 && !FRAME_OBSCURED_P (XFRAME (w->frame))
8653 /* Make sure recorded data applies to current buffer, etc. */
8654 && this_line_buffer == current_buffer
8655 && current_buffer == XBUFFER (w->buffer)
8656 && NILP (w->force_start)
8657 /* Point must be on the line that we have info recorded about. */
8658 && PT >= CHARPOS (tlbufpos)
8659 && PT <= Z - CHARPOS (tlendpos)
8660 /* All text outside that line, including its final newline,
8661 must be unchanged */
8662 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
8663 CHARPOS (tlendpos)))
8664 {
8665 if (CHARPOS (tlbufpos) > BEGV
8666 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
8667 && (CHARPOS (tlbufpos) == ZV
8668 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
8669 /* Former continuation line has disappeared by becoming empty */
8670 goto cancel;
8671 else if (XFASTINT (w->last_modified) < MODIFF
8672 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
8673 || MINI_WINDOW_P (w))
8674 {
8675 /* We have to handle the case of continuation around a
8676 wide-column character (See the comment in indent.c around
8677 line 885).
8678
8679 For instance, in the following case:
8680
8681 -------- Insert --------
8682 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
8683 J_I_ ==> J_I_ `^^' are cursors.
8684 ^^ ^^
8685 -------- --------
8686
8687 As we have to redraw the line above, we should goto cancel. */
8688
8689 struct it it;
8690 int line_height_before = this_line_pixel_height;
8691
8692 /* Note that start_display will handle the case that the
8693 line starting at tlbufpos is a continuation lines. */
8694 start_display (&it, w, tlbufpos);
8695
8696 /* Implementation note: It this still necessary? */
8697 if (it.current_x != this_line_start_x)
8698 goto cancel;
8699
8700 TRACE ((stderr, "trying display optimization 1\n"));
8701 w->cursor.vpos = -1;
8702 overlay_arrow_seen = 0;
8703 it.vpos = this_line_vpos;
8704 it.current_y = this_line_y;
8705 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
8706 display_line (&it);
8707
8708 /* If line contains point, is not continued,
8709 and ends at same distance from eob as before, we win */
8710 if (w->cursor.vpos >= 0
8711 /* Line is not continued, otherwise this_line_start_pos
8712 would have been set to 0 in display_line. */
8713 && CHARPOS (this_line_start_pos)
8714 /* Line ends as before. */
8715 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
8716 /* Line has same height as before. Otherwise other lines
8717 would have to be shifted up or down. */
8718 && this_line_pixel_height == line_height_before)
8719 {
8720 /* If this is not the window's last line, we must adjust
8721 the charstarts of the lines below. */
8722 if (it.current_y < it.last_visible_y)
8723 {
8724 struct glyph_row *row
8725 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
8726 int delta, delta_bytes;
8727
8728 if (Z - CHARPOS (tlendpos) == ZV)
8729 {
8730 /* This line ends at end of (accessible part of)
8731 buffer. There is no newline to count. */
8732 delta = (Z
8733 - CHARPOS (tlendpos)
8734 - MATRIX_ROW_START_CHARPOS (row));
8735 delta_bytes = (Z_BYTE
8736 - BYTEPOS (tlendpos)
8737 - MATRIX_ROW_START_BYTEPOS (row));
8738 }
8739 else
8740 {
8741 /* This line ends in a newline. Must take
8742 account of the newline and the rest of the
8743 text that follows. */
8744 delta = (Z
8745 - CHARPOS (tlendpos)
8746 - MATRIX_ROW_START_CHARPOS (row));
8747 delta_bytes = (Z_BYTE
8748 - BYTEPOS (tlendpos)
8749 - MATRIX_ROW_START_BYTEPOS (row));
8750 }
8751
8752 increment_matrix_positions (w->current_matrix,
8753 this_line_vpos + 1,
8754 w->current_matrix->nrows,
8755 delta, delta_bytes);
8756 }
8757
8758 /* If this row displays text now but previously didn't,
8759 or vice versa, w->window_end_vpos may have to be
8760 adjusted. */
8761 if ((it.glyph_row - 1)->displays_text_p)
8762 {
8763 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
8764 XSETINT (w->window_end_vpos, this_line_vpos);
8765 }
8766 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
8767 && this_line_vpos > 0)
8768 XSETINT (w->window_end_vpos, this_line_vpos - 1);
8769 w->window_end_valid = Qnil;
8770
8771 /* Update hint: No need to try to scroll in update_window. */
8772 w->desired_matrix->no_scrolling_p = 1;
8773
8774 #if GLYPH_DEBUG
8775 *w->desired_matrix->method = 0;
8776 debug_method_add (w, "optimization 1");
8777 #endif
8778 goto update;
8779 }
8780 else
8781 goto cancel;
8782 }
8783 else if (/* Cursor position hasn't changed. */
8784 PT == XFASTINT (w->last_point)
8785 /* Make sure the cursor was last displayed
8786 in this window. Otherwise we have to reposition it. */
8787 && 0 <= w->cursor.vpos
8788 && XINT (w->height) > w->cursor.vpos)
8789 {
8790 if (!must_finish)
8791 {
8792 do_pending_window_change (1);
8793
8794 /* We used to always goto end_of_redisplay here, but this
8795 isn't enough if we have a blinking cursor. */
8796 if (w->cursor_off_p == w->last_cursor_off_p)
8797 goto end_of_redisplay;
8798 }
8799 goto update;
8800 }
8801 /* If highlighting the region, or if the cursor is in the echo area,
8802 then we can't just move the cursor. */
8803 else if (! (!NILP (Vtransient_mark_mode)
8804 && !NILP (current_buffer->mark_active))
8805 && (EQ (selected_window, current_buffer->last_selected_window)
8806 || highlight_nonselected_windows)
8807 && NILP (w->region_showing)
8808 && NILP (Vshow_trailing_whitespace)
8809 && !cursor_in_echo_area)
8810 {
8811 struct it it;
8812 struct glyph_row *row;
8813
8814 /* Skip from tlbufpos to PT and see where it is. Note that
8815 PT may be in invisible text. If so, we will end at the
8816 next visible position. */
8817 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
8818 NULL, DEFAULT_FACE_ID);
8819 it.current_x = this_line_start_x;
8820 it.current_y = this_line_y;
8821 it.vpos = this_line_vpos;
8822
8823 /* The call to move_it_to stops in front of PT, but
8824 moves over before-strings. */
8825 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
8826
8827 if (it.vpos == this_line_vpos
8828 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
8829 row->enabled_p))
8830 {
8831 xassert (this_line_vpos == it.vpos);
8832 xassert (this_line_y == it.current_y);
8833 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
8834 #if GLYPH_DEBUG
8835 *w->desired_matrix->method = 0;
8836 debug_method_add (w, "optimization 3");
8837 #endif
8838 goto update;
8839 }
8840 else
8841 goto cancel;
8842 }
8843
8844 cancel:
8845 /* Text changed drastically or point moved off of line. */
8846 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
8847 }
8848
8849 CHARPOS (this_line_start_pos) = 0;
8850 consider_all_windows_p |= buffer_shared > 1;
8851 ++clear_face_cache_count;
8852
8853
8854 /* Build desired matrices, and update the display. If
8855 consider_all_windows_p is non-zero, do it for all windows on all
8856 frames. Otherwise do it for selected_window, only. */
8857
8858 if (consider_all_windows_p)
8859 {
8860 Lisp_Object tail, frame;
8861 int i, n = 0, size = 50;
8862 struct frame **updated
8863 = (struct frame **) alloca (size * sizeof *updated);
8864
8865 /* Clear the face cache eventually. */
8866 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
8867 {
8868 clear_face_cache (0);
8869 clear_face_cache_count = 0;
8870 }
8871
8872 /* Recompute # windows showing selected buffer. This will be
8873 incremented each time such a window is displayed. */
8874 buffer_shared = 0;
8875
8876 FOR_EACH_FRAME (tail, frame)
8877 {
8878 struct frame *f = XFRAME (frame);
8879
8880 if (FRAME_WINDOW_P (f) || f == sf)
8881 {
8882 if (clear_face_cache_count % 50 == 0
8883 && FRAME_WINDOW_P (f))
8884 clear_image_cache (f, 0);
8885
8886 /* Mark all the scroll bars to be removed; we'll redeem
8887 the ones we want when we redisplay their windows. */
8888 if (condemn_scroll_bars_hook)
8889 condemn_scroll_bars_hook (f);
8890
8891 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8892 redisplay_windows (FRAME_ROOT_WINDOW (f));
8893
8894 /* Any scroll bars which redisplay_windows should have
8895 nuked should now go away. */
8896 if (judge_scroll_bars_hook)
8897 judge_scroll_bars_hook (f);
8898
8899 /* If fonts changed, display again. */
8900 if (fonts_changed_p)
8901 goto retry;
8902
8903 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8904 {
8905 /* See if we have to hscroll. */
8906 if (hscroll_windows (f->root_window))
8907 goto retry;
8908
8909 /* Prevent various kinds of signals during display
8910 update. stdio is not robust about handling
8911 signals, which can cause an apparent I/O
8912 error. */
8913 if (interrupt_input)
8914 unrequest_sigio ();
8915 stop_polling ();
8916
8917 /* Update the display. */
8918 set_window_update_flags (XWINDOW (f->root_window), 1);
8919 pause |= update_frame (f, 0, 0);
8920 if (pause)
8921 break;
8922
8923 if (n == size)
8924 {
8925 int nbytes = size * sizeof *updated;
8926 struct frame **p = (struct frame **) alloca (2 * nbytes);
8927 bcopy (updated, p, nbytes);
8928 size *= 2;
8929 }
8930
8931 updated[n++] = f;
8932 }
8933 }
8934 }
8935
8936 /* Do the mark_window_display_accurate after all windows have
8937 been redisplayed because this call resets flags in buffers
8938 which are needed for proper redisplay. */
8939 for (i = 0; i < n; ++i)
8940 {
8941 struct frame *f = updated[i];
8942 mark_window_display_accurate (f->root_window, 1);
8943 if (frame_up_to_date_hook)
8944 frame_up_to_date_hook (f);
8945 }
8946 }
8947 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8948 {
8949 Lisp_Object mini_window;
8950 struct frame *mini_frame;
8951
8952 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
8953 internal_condition_case_1 (redisplay_window_1, selected_window, Qerror,
8954 redisplay_window_error);
8955
8956 /* Compare desired and current matrices, perform output. */
8957 update:
8958
8959 /* If fonts changed, display again. */
8960 if (fonts_changed_p)
8961 goto retry;
8962
8963 /* Prevent various kinds of signals during display update.
8964 stdio is not robust about handling signals,
8965 which can cause an apparent I/O error. */
8966 if (interrupt_input)
8967 unrequest_sigio ();
8968 stop_polling ();
8969
8970 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8971 {
8972 if (hscroll_windows (selected_window))
8973 goto retry;
8974
8975 XWINDOW (selected_window)->must_be_updated_p = 1;
8976 pause = update_frame (sf, 0, 0);
8977 }
8978
8979 /* We may have called echo_area_display at the top of this
8980 function. If the echo area is on another frame, that may
8981 have put text on a frame other than the selected one, so the
8982 above call to update_frame would not have caught it. Catch
8983 it here. */
8984 mini_window = FRAME_MINIBUF_WINDOW (sf);
8985 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8986
8987 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
8988 {
8989 XWINDOW (mini_window)->must_be_updated_p = 1;
8990 pause |= update_frame (mini_frame, 0, 0);
8991 if (!pause && hscroll_windows (mini_window))
8992 goto retry;
8993 }
8994 }
8995
8996 /* If display was paused because of pending input, make sure we do a
8997 thorough update the next time. */
8998 if (pause)
8999 {
9000 /* Prevent the optimization at the beginning of
9001 redisplay_internal that tries a single-line update of the
9002 line containing the cursor in the selected window. */
9003 CHARPOS (this_line_start_pos) = 0;
9004
9005 /* Let the overlay arrow be updated the next time. */
9006 if (!NILP (last_arrow_position))
9007 {
9008 last_arrow_position = Qt;
9009 last_arrow_string = Qt;
9010 }
9011
9012 /* If we pause after scrolling, some rows in the current
9013 matrices of some windows are not valid. */
9014 if (!WINDOW_FULL_WIDTH_P (w)
9015 && !FRAME_WINDOW_P (XFRAME (w->frame)))
9016 update_mode_lines = 1;
9017 }
9018 else
9019 {
9020 if (!consider_all_windows_p)
9021 {
9022 /* This has already been done above if
9023 consider_all_windows_p is set. */
9024 mark_window_display_accurate_1 (w, 1);
9025
9026 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
9027 last_arrow_string = Voverlay_arrow_string;
9028
9029 if (frame_up_to_date_hook != 0)
9030 frame_up_to_date_hook (sf);
9031 }
9032
9033 update_mode_lines = 0;
9034 windows_or_buffers_changed = 0;
9035 }
9036
9037 /* Start SIGIO interrupts coming again. Having them off during the
9038 code above makes it less likely one will discard output, but not
9039 impossible, since there might be stuff in the system buffer here.
9040 But it is much hairier to try to do anything about that. */
9041 if (interrupt_input)
9042 request_sigio ();
9043 start_polling ();
9044
9045 /* If a frame has become visible which was not before, redisplay
9046 again, so that we display it. Expose events for such a frame
9047 (which it gets when becoming visible) don't call the parts of
9048 redisplay constructing glyphs, so simply exposing a frame won't
9049 display anything in this case. So, we have to display these
9050 frames here explicitly. */
9051 if (!pause)
9052 {
9053 Lisp_Object tail, frame;
9054 int new_count = 0;
9055
9056 FOR_EACH_FRAME (tail, frame)
9057 {
9058 int this_is_visible = 0;
9059
9060 if (XFRAME (frame)->visible)
9061 this_is_visible = 1;
9062 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
9063 if (XFRAME (frame)->visible)
9064 this_is_visible = 1;
9065
9066 if (this_is_visible)
9067 new_count++;
9068 }
9069
9070 if (new_count != number_of_visible_frames)
9071 windows_or_buffers_changed++;
9072 }
9073
9074 /* Change frame size now if a change is pending. */
9075 do_pending_window_change (1);
9076
9077 /* If we just did a pending size change, or have additional
9078 visible frames, redisplay again. */
9079 if (windows_or_buffers_changed && !pause)
9080 goto retry;
9081
9082 end_of_redisplay:;
9083
9084 unbind_to (count, Qnil);
9085 }
9086
9087
9088 /* Redisplay, but leave alone any recent echo area message unless
9089 another message has been requested in its place.
9090
9091 This is useful in situations where you need to redisplay but no
9092 user action has occurred, making it inappropriate for the message
9093 area to be cleared. See tracking_off and
9094 wait_reading_process_input for examples of these situations.
9095
9096 FROM_WHERE is an integer saying from where this function was
9097 called. This is useful for debugging. */
9098
9099 void
9100 redisplay_preserve_echo_area (from_where)
9101 int from_where;
9102 {
9103 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
9104
9105 if (!NILP (echo_area_buffer[1]))
9106 {
9107 /* We have a previously displayed message, but no current
9108 message. Redisplay the previous message. */
9109 display_last_displayed_message_p = 1;
9110 redisplay_internal (1);
9111 display_last_displayed_message_p = 0;
9112 }
9113 else
9114 redisplay_internal (1);
9115 }
9116
9117
9118 /* Function registered with record_unwind_protect in
9119 redisplay_internal. Clears the flag indicating that a redisplay is
9120 in progress. */
9121
9122 static Lisp_Object
9123 unwind_redisplay (old_redisplaying_p)
9124 Lisp_Object old_redisplaying_p;
9125 {
9126 redisplaying_p = XFASTINT (old_redisplaying_p);
9127 return Qnil;
9128 }
9129
9130
9131 /* Mark the display of window W as accurate or inaccurate. If
9132 ACCURATE_P is non-zero mark display of W as accurate. If
9133 ACCURATE_P is zero, arrange for W to be redisplayed the next time
9134 redisplay_internal is called. */
9135
9136 static void
9137 mark_window_display_accurate_1 (w, accurate_p)
9138 struct window *w;
9139 int accurate_p;
9140 {
9141 if (BUFFERP (w->buffer))
9142 {
9143 struct buffer *b = XBUFFER (w->buffer);
9144
9145 w->last_modified
9146 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
9147 w->last_overlay_modified
9148 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
9149 w->last_had_star
9150 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
9151
9152 if (accurate_p)
9153 {
9154 b->clip_changed = 0;
9155 b->prevent_redisplay_optimizations_p = 0;
9156
9157 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
9158 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
9159 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
9160 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
9161
9162 w->current_matrix->buffer = b;
9163 w->current_matrix->begv = BUF_BEGV (b);
9164 w->current_matrix->zv = BUF_ZV (b);
9165
9166 w->last_cursor = w->cursor;
9167 w->last_cursor_off_p = w->cursor_off_p;
9168
9169 if (w == XWINDOW (selected_window))
9170 w->last_point = make_number (BUF_PT (b));
9171 else
9172 w->last_point = make_number (XMARKER (w->pointm)->charpos);
9173 }
9174 }
9175
9176 if (accurate_p)
9177 {
9178 w->window_end_valid = w->buffer;
9179 #if 0 /* This is incorrect with variable-height lines. */
9180 xassert (XINT (w->window_end_vpos)
9181 < (XINT (w->height)
9182 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
9183 #endif
9184 w->update_mode_line = Qnil;
9185 }
9186 }
9187
9188
9189 /* Mark the display of windows in the window tree rooted at WINDOW as
9190 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
9191 windows as accurate. If ACCURATE_P is zero, arrange for windows to
9192 be redisplayed the next time redisplay_internal is called. */
9193
9194 void
9195 mark_window_display_accurate (window, accurate_p)
9196 Lisp_Object window;
9197 int accurate_p;
9198 {
9199 struct window *w;
9200
9201 for (; !NILP (window); window = w->next)
9202 {
9203 w = XWINDOW (window);
9204 mark_window_display_accurate_1 (w, accurate_p);
9205
9206 if (!NILP (w->vchild))
9207 mark_window_display_accurate (w->vchild, accurate_p);
9208 if (!NILP (w->hchild))
9209 mark_window_display_accurate (w->hchild, accurate_p);
9210 }
9211
9212 if (accurate_p)
9213 {
9214 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
9215 last_arrow_string = Voverlay_arrow_string;
9216 }
9217 else
9218 {
9219 /* Force a thorough redisplay the next time by setting
9220 last_arrow_position and last_arrow_string to t, which is
9221 unequal to any useful value of Voverlay_arrow_... */
9222 last_arrow_position = Qt;
9223 last_arrow_string = Qt;
9224 }
9225 }
9226
9227
9228 /* Return value in display table DP (Lisp_Char_Table *) for character
9229 C. Since a display table doesn't have any parent, we don't have to
9230 follow parent. Do not call this function directly but use the
9231 macro DISP_CHAR_VECTOR. */
9232
9233 Lisp_Object
9234 disp_char_vector (dp, c)
9235 struct Lisp_Char_Table *dp;
9236 int c;
9237 {
9238 int code[4], i;
9239 Lisp_Object val;
9240
9241 if (SINGLE_BYTE_CHAR_P (c))
9242 return (dp->contents[c]);
9243
9244 SPLIT_CHAR (c, code[0], code[1], code[2]);
9245 if (code[1] < 32)
9246 code[1] = -1;
9247 else if (code[2] < 32)
9248 code[2] = -1;
9249
9250 /* Here, the possible range of code[0] (== charset ID) is
9251 128..max_charset. Since the top level char table contains data
9252 for multibyte characters after 256th element, we must increment
9253 code[0] by 128 to get a correct index. */
9254 code[0] += 128;
9255 code[3] = -1; /* anchor */
9256
9257 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
9258 {
9259 val = dp->contents[code[i]];
9260 if (!SUB_CHAR_TABLE_P (val))
9261 return (NILP (val) ? dp->defalt : val);
9262 }
9263
9264 /* Here, val is a sub char table. We return the default value of
9265 it. */
9266 return (dp->defalt);
9267 }
9268
9269
9270 \f
9271 /***********************************************************************
9272 Window Redisplay
9273 ***********************************************************************/
9274
9275 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
9276
9277 static void
9278 redisplay_windows (window)
9279 Lisp_Object window;
9280 {
9281 while (!NILP (window))
9282 {
9283 struct window *w = XWINDOW (window);
9284
9285 if (!NILP (w->hchild))
9286 redisplay_windows (w->hchild);
9287 else if (!NILP (w->vchild))
9288 redisplay_windows (w->vchild);
9289 else
9290 {
9291 displayed_buffer = XBUFFER (w->buffer);
9292 internal_condition_case_1 (redisplay_window_0, window, Qerror,
9293 redisplay_window_error);
9294 }
9295
9296 window = w->next;
9297 }
9298 }
9299
9300 static Lisp_Object
9301 redisplay_window_error ()
9302 {
9303 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
9304 return Qnil;
9305 }
9306
9307 static Lisp_Object
9308 redisplay_window_0 (window)
9309 Lisp_Object window;
9310 {
9311 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
9312 redisplay_window (window, 0);
9313 return Qnil;
9314 }
9315
9316 static Lisp_Object
9317 redisplay_window_1 (window)
9318 Lisp_Object window;
9319 {
9320 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
9321 redisplay_window (window, 1);
9322 return Qnil;
9323 }
9324 \f
9325 /* Set cursor position of W. PT is assumed to be displayed in ROW.
9326 DELTA is the number of bytes by which positions recorded in ROW
9327 differ from current buffer positions. */
9328
9329 void
9330 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
9331 struct window *w;
9332 struct glyph_row *row;
9333 struct glyph_matrix *matrix;
9334 int delta, delta_bytes, dy, dvpos;
9335 {
9336 struct glyph *glyph = row->glyphs[TEXT_AREA];
9337 struct glyph *end = glyph + row->used[TEXT_AREA];
9338 int x = row->x;
9339 int pt_old = PT - delta;
9340
9341 /* Skip over glyphs not having an object at the start of the row.
9342 These are special glyphs like truncation marks on terminal
9343 frames. */
9344 if (row->displays_text_p)
9345 while (glyph < end
9346 && INTEGERP (glyph->object)
9347 && glyph->charpos < 0)
9348 {
9349 x += glyph->pixel_width;
9350 ++glyph;
9351 }
9352
9353 while (glyph < end
9354 && !INTEGERP (glyph->object)
9355 && (!BUFFERP (glyph->object)
9356 || glyph->charpos < pt_old))
9357 {
9358 x += glyph->pixel_width;
9359 ++glyph;
9360 }
9361
9362 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
9363 w->cursor.x = x;
9364 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
9365 w->cursor.y = row->y + dy;
9366
9367 if (w == XWINDOW (selected_window))
9368 {
9369 if (!row->continued_p
9370 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
9371 && row->x == 0)
9372 {
9373 this_line_buffer = XBUFFER (w->buffer);
9374
9375 CHARPOS (this_line_start_pos)
9376 = MATRIX_ROW_START_CHARPOS (row) + delta;
9377 BYTEPOS (this_line_start_pos)
9378 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
9379
9380 CHARPOS (this_line_end_pos)
9381 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
9382 BYTEPOS (this_line_end_pos)
9383 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
9384
9385 this_line_y = w->cursor.y;
9386 this_line_pixel_height = row->height;
9387 this_line_vpos = w->cursor.vpos;
9388 this_line_start_x = row->x;
9389 }
9390 else
9391 CHARPOS (this_line_start_pos) = 0;
9392 }
9393 }
9394
9395
9396 /* Run window scroll functions, if any, for WINDOW with new window
9397 start STARTP. Sets the window start of WINDOW to that position.
9398
9399 We assume that the window's buffer is really current. */
9400
9401 static INLINE struct text_pos
9402 run_window_scroll_functions (window, startp)
9403 Lisp_Object window;
9404 struct text_pos startp;
9405 {
9406 struct window *w = XWINDOW (window);
9407 SET_MARKER_FROM_TEXT_POS (w->start, startp);
9408
9409 if (current_buffer != XBUFFER (w->buffer))
9410 abort ();
9411
9412 if (!NILP (Vwindow_scroll_functions))
9413 {
9414 run_hook_with_args_2 (Qwindow_scroll_functions, window,
9415 make_number (CHARPOS (startp)));
9416 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9417 /* In case the hook functions switch buffers. */
9418 if (current_buffer != XBUFFER (w->buffer))
9419 set_buffer_internal_1 (XBUFFER (w->buffer));
9420 }
9421
9422 return startp;
9423 }
9424
9425
9426 /* Modify the desired matrix of window W and W->vscroll so that the
9427 line containing the cursor is fully visible. If this requires
9428 larger matrices than are allocated, set fonts_changed_p and return
9429 0. */
9430
9431 static int
9432 make_cursor_line_fully_visible (w)
9433 struct window *w;
9434 {
9435 struct glyph_matrix *matrix;
9436 struct glyph_row *row;
9437 int window_height;
9438
9439 /* It's not always possible to find the cursor, e.g, when a window
9440 is full of overlay strings. Don't do anything in that case. */
9441 if (w->cursor.vpos < 0)
9442 return 1;
9443
9444 matrix = w->desired_matrix;
9445 row = MATRIX_ROW (matrix, w->cursor.vpos);
9446
9447 /* If the cursor row is not partially visible, there's nothing
9448 to do. */
9449 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
9450 return 1;
9451
9452 /* If the row the cursor is in is taller than the window's height,
9453 it's not clear what to do, so do nothing. */
9454 window_height = window_box_height (w);
9455 if (row->height >= window_height)
9456 return 1;
9457
9458 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
9459 {
9460 int dy = row->height - row->visible_height;
9461 w->vscroll = 0;
9462 w->cursor.y += dy;
9463 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9464 }
9465 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
9466 {
9467 int dy = - (row->height - row->visible_height);
9468 w->vscroll = dy;
9469 w->cursor.y += dy;
9470 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9471 }
9472
9473 /* When we change the cursor y-position of the selected window,
9474 change this_line_y as well so that the display optimization for
9475 the cursor line of the selected window in redisplay_internal uses
9476 the correct y-position. */
9477 if (w == XWINDOW (selected_window))
9478 this_line_y = w->cursor.y;
9479
9480 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
9481 redisplay with larger matrices. */
9482 if (matrix->nrows < required_matrix_height (w))
9483 {
9484 fonts_changed_p = 1;
9485 return 0;
9486 }
9487
9488 return 1;
9489 }
9490
9491
9492 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
9493 non-zero means only WINDOW is redisplayed in redisplay_internal.
9494 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
9495 in redisplay_window to bring a partially visible line into view in
9496 the case that only the cursor has moved.
9497
9498 Value is
9499
9500 1 if scrolling succeeded
9501
9502 0 if scrolling didn't find point.
9503
9504 -1 if new fonts have been loaded so that we must interrupt
9505 redisplay, adjust glyph matrices, and try again. */
9506
9507 enum
9508 {
9509 SCROLLING_SUCCESS,
9510 SCROLLING_FAILED,
9511 SCROLLING_NEED_LARGER_MATRICES
9512 };
9513
9514 static int
9515 try_scrolling (window, just_this_one_p, scroll_conservatively,
9516 scroll_step, temp_scroll_step)
9517 Lisp_Object window;
9518 int just_this_one_p;
9519 int scroll_conservatively, scroll_step;
9520 int temp_scroll_step;
9521 {
9522 struct window *w = XWINDOW (window);
9523 struct frame *f = XFRAME (w->frame);
9524 struct text_pos scroll_margin_pos;
9525 struct text_pos pos;
9526 struct text_pos startp;
9527 struct it it;
9528 Lisp_Object window_end;
9529 int this_scroll_margin;
9530 int dy = 0;
9531 int scroll_max;
9532 int rc;
9533 int amount_to_scroll = 0;
9534 Lisp_Object aggressive;
9535 int height;
9536
9537 #if GLYPH_DEBUG
9538 debug_method_add (w, "try_scrolling");
9539 #endif
9540
9541 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9542
9543 /* Compute scroll margin height in pixels. We scroll when point is
9544 within this distance from the top or bottom of the window. */
9545 if (scroll_margin > 0)
9546 {
9547 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
9548 this_scroll_margin *= CANON_Y_UNIT (f);
9549 }
9550 else
9551 this_scroll_margin = 0;
9552
9553 /* Compute how much we should try to scroll maximally to bring point
9554 into view. */
9555 if (scroll_step || scroll_conservatively || temp_scroll_step)
9556 scroll_max = max (scroll_step,
9557 max (scroll_conservatively, temp_scroll_step));
9558 else if (NUMBERP (current_buffer->scroll_down_aggressively)
9559 || NUMBERP (current_buffer->scroll_up_aggressively))
9560 /* We're trying to scroll because of aggressive scrolling
9561 but no scroll_step is set. Choose an arbitrary one. Maybe
9562 there should be a variable for this. */
9563 scroll_max = 10;
9564 else
9565 scroll_max = 0;
9566 scroll_max *= CANON_Y_UNIT (f);
9567
9568 /* Decide whether we have to scroll down. Start at the window end
9569 and move this_scroll_margin up to find the position of the scroll
9570 margin. */
9571 window_end = Fwindow_end (window, Qt);
9572 CHARPOS (scroll_margin_pos) = XINT (window_end);
9573 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
9574 if (this_scroll_margin)
9575 {
9576 start_display (&it, w, scroll_margin_pos);
9577 move_it_vertically (&it, - this_scroll_margin);
9578 scroll_margin_pos = it.current.pos;
9579 }
9580
9581 if (PT >= CHARPOS (scroll_margin_pos))
9582 {
9583 int y0;
9584
9585 /* Point is in the scroll margin at the bottom of the window, or
9586 below. Compute a new window start that makes point visible. */
9587
9588 /* Compute the distance from the scroll margin to PT.
9589 Give up if the distance is greater than scroll_max. */
9590 start_display (&it, w, scroll_margin_pos);
9591 y0 = it.current_y;
9592 move_it_to (&it, PT, 0, it.last_visible_y, -1,
9593 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9594
9595 /* To make point visible, we have to move the window start
9596 down so that the line the cursor is in is visible, which
9597 means we have to add in the height of the cursor line. */
9598 dy = line_bottom_y (&it) - y0;
9599
9600 if (dy > scroll_max)
9601 return SCROLLING_FAILED;
9602
9603 /* Move the window start down. If scrolling conservatively,
9604 move it just enough down to make point visible. If
9605 scroll_step is set, move it down by scroll_step. */
9606 start_display (&it, w, startp);
9607
9608 if (scroll_conservatively)
9609 amount_to_scroll
9610 = max (max (dy, CANON_Y_UNIT (f)),
9611 CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
9612 else if (scroll_step || temp_scroll_step)
9613 amount_to_scroll = scroll_max;
9614 else
9615 {
9616 aggressive = current_buffer->scroll_up_aggressively;
9617 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
9618 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
9619 if (NUMBERP (aggressive))
9620 amount_to_scroll = XFLOATINT (aggressive) * height;
9621 }
9622
9623 if (amount_to_scroll <= 0)
9624 return SCROLLING_FAILED;
9625
9626 move_it_vertically (&it, amount_to_scroll);
9627 startp = it.current.pos;
9628 }
9629 else
9630 {
9631 /* See if point is inside the scroll margin at the top of the
9632 window. */
9633 scroll_margin_pos = startp;
9634 if (this_scroll_margin)
9635 {
9636 start_display (&it, w, startp);
9637 move_it_vertically (&it, this_scroll_margin);
9638 scroll_margin_pos = it.current.pos;
9639 }
9640
9641 if (PT < CHARPOS (scroll_margin_pos))
9642 {
9643 /* Point is in the scroll margin at the top of the window or
9644 above what is displayed in the window. */
9645 int y0;
9646
9647 /* Compute the vertical distance from PT to the scroll
9648 margin position. Give up if distance is greater than
9649 scroll_max. */
9650 SET_TEXT_POS (pos, PT, PT_BYTE);
9651 start_display (&it, w, pos);
9652 y0 = it.current_y;
9653 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
9654 it.last_visible_y, -1,
9655 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9656 dy = it.current_y - y0;
9657 if (dy > scroll_max)
9658 return SCROLLING_FAILED;
9659
9660 /* Compute new window start. */
9661 start_display (&it, w, startp);
9662
9663 if (scroll_conservatively)
9664 amount_to_scroll =
9665 max (dy, CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
9666 else if (scroll_step || temp_scroll_step)
9667 amount_to_scroll = scroll_max;
9668 else
9669 {
9670 aggressive = current_buffer->scroll_down_aggressively;
9671 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
9672 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
9673 if (NUMBERP (aggressive))
9674 amount_to_scroll = XFLOATINT (aggressive) * height;
9675 }
9676
9677 if (amount_to_scroll <= 0)
9678 return SCROLLING_FAILED;
9679
9680 move_it_vertically (&it, - amount_to_scroll);
9681 startp = it.current.pos;
9682 }
9683 }
9684
9685 /* Run window scroll functions. */
9686 startp = run_window_scroll_functions (window, startp);
9687
9688 /* Display the window. Give up if new fonts are loaded, or if point
9689 doesn't appear. */
9690 if (!try_window (window, startp))
9691 rc = SCROLLING_NEED_LARGER_MATRICES;
9692 else if (w->cursor.vpos < 0)
9693 {
9694 clear_glyph_matrix (w->desired_matrix);
9695 rc = SCROLLING_FAILED;
9696 }
9697 else
9698 {
9699 /* Maybe forget recorded base line for line number display. */
9700 if (!just_this_one_p
9701 || current_buffer->clip_changed
9702 || BEG_UNCHANGED < CHARPOS (startp))
9703 w->base_line_number = Qnil;
9704
9705 /* If cursor ends up on a partially visible line, shift display
9706 lines up or down. If that fails because we need larger
9707 matrices, give up. */
9708 if (!make_cursor_line_fully_visible (w))
9709 rc = SCROLLING_NEED_LARGER_MATRICES;
9710 else
9711 rc = SCROLLING_SUCCESS;
9712 }
9713
9714 return rc;
9715 }
9716
9717
9718 /* Compute a suitable window start for window W if display of W starts
9719 on a continuation line. Value is non-zero if a new window start
9720 was computed.
9721
9722 The new window start will be computed, based on W's width, starting
9723 from the start of the continued line. It is the start of the
9724 screen line with the minimum distance from the old start W->start. */
9725
9726 static int
9727 compute_window_start_on_continuation_line (w)
9728 struct window *w;
9729 {
9730 struct text_pos pos, start_pos;
9731 int window_start_changed_p = 0;
9732
9733 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
9734
9735 /* If window start is on a continuation line... Window start may be
9736 < BEGV in case there's invisible text at the start of the
9737 buffer (M-x rmail, for example). */
9738 if (CHARPOS (start_pos) > BEGV
9739 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
9740 {
9741 struct it it;
9742 struct glyph_row *row;
9743
9744 /* Handle the case that the window start is out of range. */
9745 if (CHARPOS (start_pos) < BEGV)
9746 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
9747 else if (CHARPOS (start_pos) > ZV)
9748 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
9749
9750 /* Find the start of the continued line. This should be fast
9751 because scan_buffer is fast (newline cache). */
9752 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
9753 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
9754 row, DEFAULT_FACE_ID);
9755 reseat_at_previous_visible_line_start (&it);
9756
9757 /* If the line start is "too far" away from the window start,
9758 say it takes too much time to compute a new window start. */
9759 if (CHARPOS (start_pos) - IT_CHARPOS (it)
9760 < XFASTINT (w->height) * XFASTINT (w->width))
9761 {
9762 int min_distance, distance;
9763
9764 /* Move forward by display lines to find the new window
9765 start. If window width was enlarged, the new start can
9766 be expected to be > the old start. If window width was
9767 decreased, the new window start will be < the old start.
9768 So, we're looking for the display line start with the
9769 minimum distance from the old window start. */
9770 pos = it.current.pos;
9771 min_distance = INFINITY;
9772 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
9773 distance < min_distance)
9774 {
9775 min_distance = distance;
9776 pos = it.current.pos;
9777 move_it_by_lines (&it, 1, 0);
9778 }
9779
9780 /* Set the window start there. */
9781 SET_MARKER_FROM_TEXT_POS (w->start, pos);
9782 window_start_changed_p = 1;
9783 }
9784 }
9785
9786 return window_start_changed_p;
9787 }
9788
9789
9790 /* Try cursor movement in case text has not changes in window WINDOW,
9791 with window start STARTP. Value is
9792
9793 CURSOR_MOVEMENT_SUCCESS if successful
9794
9795 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
9796
9797 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
9798 display. *SCROLL_STEP is set to 1, under certain circumstances, if
9799 we want to scroll as if scroll-step were set to 1. See the code.
9800
9801 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
9802 which case we have to abort this redisplay, and adjust matrices
9803 first. */
9804
9805 enum
9806 {
9807 CURSOR_MOVEMENT_SUCCESS,
9808 CURSOR_MOVEMENT_CANNOT_BE_USED,
9809 CURSOR_MOVEMENT_MUST_SCROLL,
9810 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
9811 };
9812
9813 static int
9814 try_cursor_movement (window, startp, scroll_step)
9815 Lisp_Object window;
9816 struct text_pos startp;
9817 int *scroll_step;
9818 {
9819 struct window *w = XWINDOW (window);
9820 struct frame *f = XFRAME (w->frame);
9821 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
9822
9823 #if GLYPH_DEBUG
9824 if (inhibit_try_cursor_movement)
9825 return rc;
9826 #endif
9827
9828 /* Handle case where text has not changed, only point, and it has
9829 not moved off the frame. */
9830 if (/* Point may be in this window. */
9831 PT >= CHARPOS (startp)
9832 /* Selective display hasn't changed. */
9833 && !current_buffer->clip_changed
9834 /* Function force-mode-line-update is used to force a thorough
9835 redisplay. It sets either windows_or_buffers_changed or
9836 update_mode_lines. So don't take a shortcut here for these
9837 cases. */
9838 && !update_mode_lines
9839 && !windows_or_buffers_changed
9840 /* Can't use this case if highlighting a region. When a
9841 region exists, cursor movement has to do more than just
9842 set the cursor. */
9843 && !(!NILP (Vtransient_mark_mode)
9844 && !NILP (current_buffer->mark_active))
9845 && NILP (w->region_showing)
9846 && NILP (Vshow_trailing_whitespace)
9847 /* Right after splitting windows, last_point may be nil. */
9848 && INTEGERP (w->last_point)
9849 /* This code is not used for mini-buffer for the sake of the case
9850 of redisplaying to replace an echo area message; since in
9851 that case the mini-buffer contents per se are usually
9852 unchanged. This code is of no real use in the mini-buffer
9853 since the handling of this_line_start_pos, etc., in redisplay
9854 handles the same cases. */
9855 && !EQ (window, minibuf_window)
9856 /* When splitting windows or for new windows, it happens that
9857 redisplay is called with a nil window_end_vpos or one being
9858 larger than the window. This should really be fixed in
9859 window.c. I don't have this on my list, now, so we do
9860 approximately the same as the old redisplay code. --gerd. */
9861 && INTEGERP (w->window_end_vpos)
9862 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
9863 && (FRAME_WINDOW_P (f)
9864 || !MARKERP (Voverlay_arrow_position)
9865 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
9866 {
9867 int this_scroll_margin;
9868 struct glyph_row *row = NULL;
9869
9870 #if GLYPH_DEBUG
9871 debug_method_add (w, "cursor movement");
9872 #endif
9873
9874 /* Scroll if point within this distance from the top or bottom
9875 of the window. This is a pixel value. */
9876 this_scroll_margin = max (0, scroll_margin);
9877 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
9878 this_scroll_margin *= CANON_Y_UNIT (f);
9879
9880 /* Start with the row the cursor was displayed during the last
9881 not paused redisplay. Give up if that row is not valid. */
9882 if (w->last_cursor.vpos < 0
9883 || w->last_cursor.vpos >= w->current_matrix->nrows)
9884 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9885 else
9886 {
9887 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
9888 if (row->mode_line_p)
9889 ++row;
9890 if (!row->enabled_p)
9891 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9892 }
9893
9894 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
9895 {
9896 int scroll_p = 0;
9897 int last_y = window_text_bottom_y (w) - this_scroll_margin;
9898
9899 if (PT > XFASTINT (w->last_point))
9900 {
9901 /* Point has moved forward. */
9902 while (MATRIX_ROW_END_CHARPOS (row) < PT
9903 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
9904 {
9905 xassert (row->enabled_p);
9906 ++row;
9907 }
9908
9909 /* The end position of a row equals the start position
9910 of the next row. If PT is there, we would rather
9911 display it in the next line. */
9912 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9913 && MATRIX_ROW_END_CHARPOS (row) == PT
9914 && !cursor_row_p (w, row))
9915 ++row;
9916
9917 /* If within the scroll margin, scroll. Note that
9918 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
9919 the next line would be drawn, and that
9920 this_scroll_margin can be zero. */
9921 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
9922 || PT > MATRIX_ROW_END_CHARPOS (row)
9923 /* Line is completely visible last line in window
9924 and PT is to be set in the next line. */
9925 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
9926 && PT == MATRIX_ROW_END_CHARPOS (row)
9927 && !row->ends_at_zv_p
9928 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
9929 scroll_p = 1;
9930 }
9931 else if (PT < XFASTINT (w->last_point))
9932 {
9933 /* Cursor has to be moved backward. Note that PT >=
9934 CHARPOS (startp) because of the outer
9935 if-statement. */
9936 while (!row->mode_line_p
9937 && (MATRIX_ROW_START_CHARPOS (row) > PT
9938 || (MATRIX_ROW_START_CHARPOS (row) == PT
9939 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
9940 && (row->y > this_scroll_margin
9941 || CHARPOS (startp) == BEGV))
9942 {
9943 xassert (row->enabled_p);
9944 --row;
9945 }
9946
9947 /* Consider the following case: Window starts at BEGV,
9948 there is invisible, intangible text at BEGV, so that
9949 display starts at some point START > BEGV. It can
9950 happen that we are called with PT somewhere between
9951 BEGV and START. Try to handle that case. */
9952 if (row < w->current_matrix->rows
9953 || row->mode_line_p)
9954 {
9955 row = w->current_matrix->rows;
9956 if (row->mode_line_p)
9957 ++row;
9958 }
9959
9960 /* Due to newlines in overlay strings, we may have to
9961 skip forward over overlay strings. */
9962 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9963 && MATRIX_ROW_END_CHARPOS (row) == PT
9964 && !cursor_row_p (w, row))
9965 ++row;
9966
9967 /* If within the scroll margin, scroll. */
9968 if (row->y < this_scroll_margin
9969 && CHARPOS (startp) != BEGV)
9970 scroll_p = 1;
9971 }
9972
9973 if (PT < MATRIX_ROW_START_CHARPOS (row)
9974 || PT > MATRIX_ROW_END_CHARPOS (row))
9975 {
9976 /* if PT is not in the glyph row, give up. */
9977 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9978 }
9979 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
9980 {
9981 if (PT == MATRIX_ROW_END_CHARPOS (row)
9982 && !row->ends_at_zv_p
9983 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
9984 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9985 else if (row->height > window_box_height (w))
9986 {
9987 /* If we end up in a partially visible line, let's
9988 make it fully visible, except when it's taller
9989 than the window, in which case we can't do much
9990 about it. */
9991 *scroll_step = 1;
9992 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9993 }
9994 else
9995 {
9996 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9997 try_window (window, startp);
9998 if (!make_cursor_line_fully_visible (w))
9999 rc = CURSOR_MOVEMENT_NEED_LARGER_MATRICES;
10000 else
10001 rc = CURSOR_MOVEMENT_SUCCESS;
10002 }
10003 }
10004 else if (scroll_p)
10005 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10006 else
10007 {
10008 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10009 rc = CURSOR_MOVEMENT_SUCCESS;
10010 }
10011 }
10012 }
10013
10014 return rc;
10015 }
10016
10017
10018 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
10019 selected_window is redisplayed. */
10020
10021 static void
10022 redisplay_window (window, just_this_one_p)
10023 Lisp_Object window;
10024 int just_this_one_p;
10025 {
10026 struct window *w = XWINDOW (window);
10027 struct frame *f = XFRAME (w->frame);
10028 struct buffer *buffer = XBUFFER (w->buffer);
10029 struct buffer *old = current_buffer;
10030 struct text_pos lpoint, opoint, startp;
10031 int update_mode_line;
10032 int tem;
10033 struct it it;
10034 /* Record it now because it's overwritten. */
10035 int current_matrix_up_to_date_p = 0;
10036 int temp_scroll_step = 0;
10037 int count = BINDING_STACK_SIZE ();
10038 int rc;
10039
10040 SET_TEXT_POS (lpoint, PT, PT_BYTE);
10041 opoint = lpoint;
10042
10043 /* W must be a leaf window here. */
10044 xassert (!NILP (w->buffer));
10045 #if GLYPH_DEBUG
10046 *w->desired_matrix->method = 0;
10047 #endif
10048
10049 specbind (Qinhibit_point_motion_hooks, Qt);
10050
10051 reconsider_clip_changes (w, buffer);
10052
10053 /* Has the mode line to be updated? */
10054 update_mode_line = (!NILP (w->update_mode_line)
10055 || update_mode_lines
10056 || buffer->clip_changed);
10057
10058 if (MINI_WINDOW_P (w))
10059 {
10060 if (w == XWINDOW (echo_area_window)
10061 && !NILP (echo_area_buffer[0]))
10062 {
10063 if (update_mode_line)
10064 /* We may have to update a tty frame's menu bar or a
10065 tool-bar. Example `M-x C-h C-h C-g'. */
10066 goto finish_menu_bars;
10067 else
10068 /* We've already displayed the echo area glyphs in this window. */
10069 goto finish_scroll_bars;
10070 }
10071 else if (w != XWINDOW (minibuf_window))
10072 {
10073 /* W is a mini-buffer window, but it's not the currently
10074 active one, so clear it. */
10075 int yb = window_text_bottom_y (w);
10076 struct glyph_row *row;
10077 int y;
10078
10079 for (y = 0, row = w->desired_matrix->rows;
10080 y < yb;
10081 y += row->height, ++row)
10082 blank_row (w, row, y);
10083 goto finish_scroll_bars;
10084 }
10085
10086 clear_glyph_matrix (w->desired_matrix);
10087 }
10088
10089 /* Otherwise set up data on this window; select its buffer and point
10090 value. */
10091 /* Really select the buffer, for the sake of buffer-local
10092 variables. */
10093 set_buffer_internal_1 (XBUFFER (w->buffer));
10094 SET_TEXT_POS (opoint, PT, PT_BYTE);
10095
10096 current_matrix_up_to_date_p
10097 = (!NILP (w->window_end_valid)
10098 && !current_buffer->clip_changed
10099 && XFASTINT (w->last_modified) >= MODIFF
10100 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
10101
10102 /* When windows_or_buffers_changed is non-zero, we can't rely on
10103 the window end being valid, so set it to nil there. */
10104 if (windows_or_buffers_changed)
10105 {
10106 /* If window starts on a continuation line, maybe adjust the
10107 window start in case the window's width changed. */
10108 if (XMARKER (w->start)->buffer == current_buffer)
10109 compute_window_start_on_continuation_line (w);
10110
10111 w->window_end_valid = Qnil;
10112 }
10113
10114 /* Some sanity checks. */
10115 CHECK_WINDOW_END (w);
10116 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
10117 abort ();
10118 if (BYTEPOS (opoint) < CHARPOS (opoint))
10119 abort ();
10120
10121 /* If %c is in mode line, update it if needed. */
10122 if (!NILP (w->column_number_displayed)
10123 /* This alternative quickly identifies a common case
10124 where no change is needed. */
10125 && !(PT == XFASTINT (w->last_point)
10126 && XFASTINT (w->last_modified) >= MODIFF
10127 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
10128 && XFASTINT (w->column_number_displayed) != current_column ())
10129 update_mode_line = 1;
10130
10131 /* Count number of windows showing the selected buffer. An indirect
10132 buffer counts as its base buffer. */
10133 if (!just_this_one_p)
10134 {
10135 struct buffer *current_base, *window_base;
10136 current_base = current_buffer;
10137 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
10138 if (current_base->base_buffer)
10139 current_base = current_base->base_buffer;
10140 if (window_base->base_buffer)
10141 window_base = window_base->base_buffer;
10142 if (current_base == window_base)
10143 buffer_shared++;
10144 }
10145
10146 /* Point refers normally to the selected window. For any other
10147 window, set up appropriate value. */
10148 if (!EQ (window, selected_window))
10149 {
10150 int new_pt = XMARKER (w->pointm)->charpos;
10151 int new_pt_byte = marker_byte_position (w->pointm);
10152 if (new_pt < BEGV)
10153 {
10154 new_pt = BEGV;
10155 new_pt_byte = BEGV_BYTE;
10156 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
10157 }
10158 else if (new_pt > (ZV - 1))
10159 {
10160 new_pt = ZV;
10161 new_pt_byte = ZV_BYTE;
10162 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
10163 }
10164
10165 /* We don't use SET_PT so that the point-motion hooks don't run. */
10166 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
10167 }
10168
10169 /* If any of the character widths specified in the display table
10170 have changed, invalidate the width run cache. It's true that
10171 this may be a bit late to catch such changes, but the rest of
10172 redisplay goes (non-fatally) haywire when the display table is
10173 changed, so why should we worry about doing any better? */
10174 if (current_buffer->width_run_cache)
10175 {
10176 struct Lisp_Char_Table *disptab = buffer_display_table ();
10177
10178 if (! disptab_matches_widthtab (disptab,
10179 XVECTOR (current_buffer->width_table)))
10180 {
10181 invalidate_region_cache (current_buffer,
10182 current_buffer->width_run_cache,
10183 BEG, Z);
10184 recompute_width_table (current_buffer, disptab);
10185 }
10186 }
10187
10188 /* If window-start is screwed up, choose a new one. */
10189 if (XMARKER (w->start)->buffer != current_buffer)
10190 goto recenter;
10191
10192 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10193
10194 /* If someone specified a new starting point but did not insist,
10195 check whether it can be used. */
10196 if (!NILP (w->optional_new_start)
10197 && CHARPOS (startp) >= BEGV
10198 && CHARPOS (startp) <= ZV)
10199 {
10200 w->optional_new_start = Qnil;
10201 start_display (&it, w, startp);
10202 move_it_to (&it, PT, 0, it.last_visible_y, -1,
10203 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
10204 if (IT_CHARPOS (it) == PT)
10205 w->force_start = Qt;
10206 }
10207
10208 /* Handle case where place to start displaying has been specified,
10209 unless the specified location is outside the accessible range. */
10210 if (!NILP (w->force_start)
10211 || w->frozen_window_start_p)
10212 {
10213 w->force_start = Qnil;
10214 w->vscroll = 0;
10215 w->window_end_valid = Qnil;
10216
10217 /* Forget any recorded base line for line number display. */
10218 if (!current_matrix_up_to_date_p
10219 || current_buffer->clip_changed)
10220 w->base_line_number = Qnil;
10221
10222 /* Redisplay the mode line. Select the buffer properly for that.
10223 Also, run the hook window-scroll-functions
10224 because we have scrolled. */
10225 /* Note, we do this after clearing force_start because
10226 if there's an error, it is better to forget about force_start
10227 than to get into an infinite loop calling the hook functions
10228 and having them get more errors. */
10229 if (!update_mode_line
10230 || ! NILP (Vwindow_scroll_functions))
10231 {
10232 update_mode_line = 1;
10233 w->update_mode_line = Qt;
10234 startp = run_window_scroll_functions (window, startp);
10235 }
10236
10237 w->last_modified = make_number (0);
10238 w->last_overlay_modified = make_number (0);
10239 if (CHARPOS (startp) < BEGV)
10240 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
10241 else if (CHARPOS (startp) > ZV)
10242 SET_TEXT_POS (startp, ZV, ZV_BYTE);
10243
10244 /* Redisplay, then check if cursor has been set during the
10245 redisplay. Give up if new fonts were loaded. */
10246 if (!try_window (window, startp))
10247 {
10248 w->force_start = Qt;
10249 clear_glyph_matrix (w->desired_matrix);
10250 goto finish_scroll_bars;
10251 }
10252
10253 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
10254 {
10255 /* If point does not appear, try to move point so it does
10256 appear. The desired matrix has been built above, so we
10257 can use it here. */
10258 int window_height;
10259 struct glyph_row *row;
10260
10261 window_height = window_box_height (w) / 2;
10262 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
10263 while (MATRIX_ROW_BOTTOM_Y (row) < window_height)
10264 ++row;
10265
10266 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
10267 MATRIX_ROW_START_BYTEPOS (row));
10268
10269 if (w != XWINDOW (selected_window))
10270 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
10271 else if (current_buffer == old)
10272 SET_TEXT_POS (lpoint, PT, PT_BYTE);
10273
10274 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
10275
10276 /* If we are highlighting the region, then we just changed
10277 the region, so redisplay to show it. */
10278 if (!NILP (Vtransient_mark_mode)
10279 && !NILP (current_buffer->mark_active))
10280 {
10281 clear_glyph_matrix (w->desired_matrix);
10282 if (!try_window (window, startp))
10283 goto need_larger_matrices;
10284 }
10285 }
10286
10287 if (!make_cursor_line_fully_visible (w))
10288 goto need_larger_matrices;
10289 #if GLYPH_DEBUG
10290 debug_method_add (w, "forced window start");
10291 #endif
10292 goto done;
10293 }
10294
10295 /* Handle case where text has not changed, only point, and it has
10296 not moved off the frame. */
10297 if (current_matrix_up_to_date_p
10298 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
10299 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
10300 {
10301 switch (rc)
10302 {
10303 case CURSOR_MOVEMENT_SUCCESS:
10304 goto done;
10305
10306 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
10307 goto need_larger_matrices;
10308
10309 case CURSOR_MOVEMENT_MUST_SCROLL:
10310 goto try_to_scroll;
10311
10312 default:
10313 abort ();
10314 }
10315 }
10316 /* If current starting point was originally the beginning of a line
10317 but no longer is, find a new starting point. */
10318 else if (!NILP (w->start_at_line_beg)
10319 && !(CHARPOS (startp) <= BEGV
10320 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
10321 {
10322 #if GLYPH_DEBUG
10323 debug_method_add (w, "recenter 1");
10324 #endif
10325 goto recenter;
10326 }
10327
10328 /* Try scrolling with try_window_id. Value is > 0 if update has
10329 been done, it is -1 if we know that the same window start will
10330 not work. It is 0 if unsuccessful for some other reason. */
10331 else if ((tem = try_window_id (w)) != 0)
10332 {
10333 #if GLYPH_DEBUG
10334 debug_method_add (w, "try_window_id %d", tem);
10335 #endif
10336
10337 if (fonts_changed_p)
10338 goto need_larger_matrices;
10339 if (tem > 0)
10340 goto done;
10341
10342 /* Otherwise try_window_id has returned -1 which means that we
10343 don't want the alternative below this comment to execute. */
10344 }
10345 else if (CHARPOS (startp) >= BEGV
10346 && CHARPOS (startp) <= ZV
10347 && PT >= CHARPOS (startp)
10348 && (CHARPOS (startp) < ZV
10349 /* Avoid starting at end of buffer. */
10350 || CHARPOS (startp) == BEGV
10351 || (XFASTINT (w->last_modified) >= MODIFF
10352 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
10353 {
10354 #if GLYPH_DEBUG
10355 debug_method_add (w, "same window start");
10356 #endif
10357
10358 /* Try to redisplay starting at same place as before.
10359 If point has not moved off frame, accept the results. */
10360 if (!current_matrix_up_to_date_p
10361 /* Don't use try_window_reusing_current_matrix in this case
10362 because a window scroll function can have changed the
10363 buffer. */
10364 || !NILP (Vwindow_scroll_functions)
10365 || MINI_WINDOW_P (w)
10366 || !try_window_reusing_current_matrix (w))
10367 {
10368 IF_DEBUG (debug_method_add (w, "1"));
10369 try_window (window, startp);
10370 }
10371
10372 if (fonts_changed_p)
10373 goto need_larger_matrices;
10374
10375 if (w->cursor.vpos >= 0)
10376 {
10377 if (!just_this_one_p
10378 || current_buffer->clip_changed
10379 || BEG_UNCHANGED < CHARPOS (startp))
10380 /* Forget any recorded base line for line number display. */
10381 w->base_line_number = Qnil;
10382
10383 if (!make_cursor_line_fully_visible (w))
10384 goto need_larger_matrices;
10385 goto done;
10386 }
10387 else
10388 clear_glyph_matrix (w->desired_matrix);
10389 }
10390
10391 try_to_scroll:
10392
10393 w->last_modified = make_number (0);
10394 w->last_overlay_modified = make_number (0);
10395
10396 /* Redisplay the mode line. Select the buffer properly for that. */
10397 if (!update_mode_line)
10398 {
10399 update_mode_line = 1;
10400 w->update_mode_line = Qt;
10401 }
10402
10403 /* Try to scroll by specified few lines. */
10404 if ((scroll_conservatively
10405 || scroll_step
10406 || temp_scroll_step
10407 || NUMBERP (current_buffer->scroll_up_aggressively)
10408 || NUMBERP (current_buffer->scroll_down_aggressively))
10409 && !current_buffer->clip_changed
10410 && CHARPOS (startp) >= BEGV
10411 && CHARPOS (startp) <= ZV)
10412 {
10413 /* The function returns -1 if new fonts were loaded, 1 if
10414 successful, 0 if not successful. */
10415 int rc = try_scrolling (window, just_this_one_p,
10416 scroll_conservatively,
10417 scroll_step,
10418 temp_scroll_step);
10419 switch (rc)
10420 {
10421 case SCROLLING_SUCCESS:
10422 goto done;
10423
10424 case SCROLLING_NEED_LARGER_MATRICES:
10425 goto need_larger_matrices;
10426
10427 case SCROLLING_FAILED:
10428 break;
10429
10430 default:
10431 abort ();
10432 }
10433 }
10434
10435 /* Finally, just choose place to start which centers point */
10436
10437 recenter:
10438
10439 #if GLYPH_DEBUG
10440 debug_method_add (w, "recenter");
10441 #endif
10442
10443 /* w->vscroll = 0; */
10444
10445 /* Forget any previously recorded base line for line number display. */
10446 if (!current_matrix_up_to_date_p
10447 || current_buffer->clip_changed)
10448 w->base_line_number = Qnil;
10449
10450 /* Move backward half the height of the window. */
10451 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10452 it.current_y = it.last_visible_y;
10453 move_it_vertically_backward (&it, window_box_height (w) / 2);
10454 xassert (IT_CHARPOS (it) >= BEGV);
10455
10456 /* The function move_it_vertically_backward may move over more
10457 than the specified y-distance. If it->w is small, e.g. a
10458 mini-buffer window, we may end up in front of the window's
10459 display area. Start displaying at the start of the line
10460 containing PT in this case. */
10461 if (it.current_y <= 0)
10462 {
10463 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10464 move_it_vertically (&it, 0);
10465 xassert (IT_CHARPOS (it) <= PT);
10466 it.current_y = 0;
10467 }
10468
10469 it.current_x = it.hpos = 0;
10470
10471 /* Set startp here explicitly in case that helps avoid an infinite loop
10472 in case the window-scroll-functions functions get errors. */
10473 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
10474
10475 /* Run scroll hooks. */
10476 startp = run_window_scroll_functions (window, it.current.pos);
10477
10478 /* Redisplay the window. */
10479 if (!current_matrix_up_to_date_p
10480 || windows_or_buffers_changed
10481 /* Don't use try_window_reusing_current_matrix in this case
10482 because it can have changed the buffer. */
10483 || !NILP (Vwindow_scroll_functions)
10484 || !just_this_one_p
10485 || MINI_WINDOW_P (w)
10486 || !try_window_reusing_current_matrix (w))
10487 try_window (window, startp);
10488
10489 /* If new fonts have been loaded (due to fontsets), give up. We
10490 have to start a new redisplay since we need to re-adjust glyph
10491 matrices. */
10492 if (fonts_changed_p)
10493 goto need_larger_matrices;
10494
10495 /* If cursor did not appear assume that the middle of the window is
10496 in the first line of the window. Do it again with the next line.
10497 (Imagine a window of height 100, displaying two lines of height
10498 60. Moving back 50 from it->last_visible_y will end in the first
10499 line.) */
10500 if (w->cursor.vpos < 0)
10501 {
10502 if (!NILP (w->window_end_valid)
10503 && PT >= Z - XFASTINT (w->window_end_pos))
10504 {
10505 clear_glyph_matrix (w->desired_matrix);
10506 move_it_by_lines (&it, 1, 0);
10507 try_window (window, it.current.pos);
10508 }
10509 else if (PT < IT_CHARPOS (it))
10510 {
10511 clear_glyph_matrix (w->desired_matrix);
10512 move_it_by_lines (&it, -1, 0);
10513 try_window (window, it.current.pos);
10514 }
10515 else
10516 {
10517 /* Not much we can do about it. */
10518 }
10519 }
10520
10521 /* Consider the following case: Window starts at BEGV, there is
10522 invisible, intangible text at BEGV, so that display starts at
10523 some point START > BEGV. It can happen that we are called with
10524 PT somewhere between BEGV and START. Try to handle that case. */
10525 if (w->cursor.vpos < 0)
10526 {
10527 struct glyph_row *row = w->current_matrix->rows;
10528 if (row->mode_line_p)
10529 ++row;
10530 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10531 }
10532
10533 if (!make_cursor_line_fully_visible (w))
10534 goto need_larger_matrices;
10535
10536 done:
10537
10538 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10539 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
10540 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
10541 ? Qt : Qnil);
10542
10543 /* Display the mode line, if we must. */
10544 if ((update_mode_line
10545 /* If window not full width, must redo its mode line
10546 if (a) the window to its side is being redone and
10547 (b) we do a frame-based redisplay. This is a consequence
10548 of how inverted lines are drawn in frame-based redisplay. */
10549 || (!just_this_one_p
10550 && !FRAME_WINDOW_P (f)
10551 && !WINDOW_FULL_WIDTH_P (w))
10552 /* Line number to display. */
10553 || INTEGERP (w->base_line_pos)
10554 /* Column number is displayed and different from the one displayed. */
10555 || (!NILP (w->column_number_displayed)
10556 && XFASTINT (w->column_number_displayed) != current_column ()))
10557 /* This means that the window has a mode line. */
10558 && (WINDOW_WANTS_MODELINE_P (w)
10559 || WINDOW_WANTS_HEADER_LINE_P (w)))
10560 {
10561 display_mode_lines (w);
10562
10563 /* If mode line height has changed, arrange for a thorough
10564 immediate redisplay using the correct mode line height. */
10565 if (WINDOW_WANTS_MODELINE_P (w)
10566 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
10567 {
10568 fonts_changed_p = 1;
10569 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
10570 = DESIRED_MODE_LINE_HEIGHT (w);
10571 }
10572
10573 /* If top line height has changed, arrange for a thorough
10574 immediate redisplay using the correct mode line height. */
10575 if (WINDOW_WANTS_HEADER_LINE_P (w)
10576 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
10577 {
10578 fonts_changed_p = 1;
10579 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
10580 = DESIRED_HEADER_LINE_HEIGHT (w);
10581 }
10582
10583 if (fonts_changed_p)
10584 goto need_larger_matrices;
10585 }
10586
10587 if (!line_number_displayed
10588 && !BUFFERP (w->base_line_pos))
10589 {
10590 w->base_line_pos = Qnil;
10591 w->base_line_number = Qnil;
10592 }
10593
10594 finish_menu_bars:
10595
10596 /* When we reach a frame's selected window, redo the frame's menu bar. */
10597 if (update_mode_line
10598 && EQ (FRAME_SELECTED_WINDOW (f), window))
10599 {
10600 int redisplay_menu_p = 0;
10601
10602 if (FRAME_WINDOW_P (f))
10603 {
10604 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
10605 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
10606 #else
10607 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10608 #endif
10609 }
10610 else
10611 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10612
10613 if (redisplay_menu_p)
10614 display_menu_bar (w);
10615
10616 #ifdef HAVE_WINDOW_SYSTEM
10617 if (WINDOWP (f->tool_bar_window)
10618 && (FRAME_TOOL_BAR_LINES (f) > 0
10619 || auto_resize_tool_bars_p))
10620 redisplay_tool_bar (f);
10621 #endif
10622 }
10623
10624 need_larger_matrices:
10625 ;
10626 finish_scroll_bars:
10627
10628 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
10629 {
10630 int start, end, whole;
10631
10632 /* Calculate the start and end positions for the current window.
10633 At some point, it would be nice to choose between scrollbars
10634 which reflect the whole buffer size, with special markers
10635 indicating narrowing, and scrollbars which reflect only the
10636 visible region.
10637
10638 Note that mini-buffers sometimes aren't displaying any text. */
10639 if (!MINI_WINDOW_P (w)
10640 || (w == XWINDOW (minibuf_window)
10641 && NILP (echo_area_buffer[0])))
10642 {
10643 whole = ZV - BEGV;
10644 start = marker_position (w->start) - BEGV;
10645 /* I don't think this is guaranteed to be right. For the
10646 moment, we'll pretend it is. */
10647 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
10648
10649 if (end < start)
10650 end = start;
10651 if (whole < (end - start))
10652 whole = end - start;
10653 }
10654 else
10655 start = end = whole = 0;
10656
10657 /* Indicate what this scroll bar ought to be displaying now. */
10658 set_vertical_scroll_bar_hook (w, end - start, whole, start);
10659
10660 /* Note that we actually used the scroll bar attached to this
10661 window, so it shouldn't be deleted at the end of redisplay. */
10662 redeem_scroll_bar_hook (w);
10663 }
10664
10665 /* Restore current_buffer and value of point in it. */
10666 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
10667 set_buffer_internal_1 (old);
10668 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
10669
10670 unbind_to (count, Qnil);
10671 }
10672
10673
10674 /* Build the complete desired matrix of WINDOW with a window start
10675 buffer position POS. Value is non-zero if successful. It is zero
10676 if fonts were loaded during redisplay which makes re-adjusting
10677 glyph matrices necessary. */
10678
10679 int
10680 try_window (window, pos)
10681 Lisp_Object window;
10682 struct text_pos pos;
10683 {
10684 struct window *w = XWINDOW (window);
10685 struct it it;
10686 struct glyph_row *last_text_row = NULL;
10687
10688 /* Make POS the new window start. */
10689 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
10690
10691 /* Mark cursor position as unknown. No overlay arrow seen. */
10692 w->cursor.vpos = -1;
10693 overlay_arrow_seen = 0;
10694
10695 /* Initialize iterator and info to start at POS. */
10696 start_display (&it, w, pos);
10697
10698 /* Display all lines of W. */
10699 while (it.current_y < it.last_visible_y)
10700 {
10701 if (display_line (&it))
10702 last_text_row = it.glyph_row - 1;
10703 if (fonts_changed_p)
10704 return 0;
10705 }
10706
10707 /* If bottom moved off end of frame, change mode line percentage. */
10708 if (XFASTINT (w->window_end_pos) <= 0
10709 && Z != IT_CHARPOS (it))
10710 w->update_mode_line = Qt;
10711
10712 /* Set window_end_pos to the offset of the last character displayed
10713 on the window from the end of current_buffer. Set
10714 window_end_vpos to its row number. */
10715 if (last_text_row)
10716 {
10717 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
10718 w->window_end_bytepos
10719 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10720 w->window_end_pos
10721 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10722 w->window_end_vpos
10723 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10724 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
10725 ->displays_text_p);
10726 }
10727 else
10728 {
10729 w->window_end_bytepos = 0;
10730 w->window_end_pos = w->window_end_vpos = make_number (0);
10731 }
10732
10733 /* But that is not valid info until redisplay finishes. */
10734 w->window_end_valid = Qnil;
10735 return 1;
10736 }
10737
10738
10739 \f
10740 /************************************************************************
10741 Window redisplay reusing current matrix when buffer has not changed
10742 ************************************************************************/
10743
10744 /* Try redisplay of window W showing an unchanged buffer with a
10745 different window start than the last time it was displayed by
10746 reusing its current matrix. Value is non-zero if successful.
10747 W->start is the new window start. */
10748
10749 static int
10750 try_window_reusing_current_matrix (w)
10751 struct window *w;
10752 {
10753 struct frame *f = XFRAME (w->frame);
10754 struct glyph_row *row, *bottom_row;
10755 struct it it;
10756 struct run run;
10757 struct text_pos start, new_start;
10758 int nrows_scrolled, i;
10759 struct glyph_row *last_text_row;
10760 struct glyph_row *last_reused_text_row;
10761 struct glyph_row *start_row;
10762 int start_vpos, min_y, max_y;
10763
10764 #if GLYPH_DEBUG
10765 if (inhibit_try_window_reusing)
10766 return 0;
10767 #endif
10768
10769 if (/* This function doesn't handle terminal frames. */
10770 !FRAME_WINDOW_P (f)
10771 /* Don't try to reuse the display if windows have been split
10772 or such. */
10773 || windows_or_buffers_changed)
10774 return 0;
10775
10776 /* Can't do this if region may have changed. */
10777 if ((!NILP (Vtransient_mark_mode)
10778 && !NILP (current_buffer->mark_active))
10779 || !NILP (w->region_showing)
10780 || !NILP (Vshow_trailing_whitespace))
10781 return 0;
10782
10783 /* If top-line visibility has changed, give up. */
10784 if (WINDOW_WANTS_HEADER_LINE_P (w)
10785 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
10786 return 0;
10787
10788 /* Give up if old or new display is scrolled vertically. We could
10789 make this function handle this, but right now it doesn't. */
10790 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10791 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
10792 return 0;
10793
10794 /* The variable new_start now holds the new window start. The old
10795 start `start' can be determined from the current matrix. */
10796 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
10797 start = start_row->start.pos;
10798 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
10799
10800 /* Clear the desired matrix for the display below. */
10801 clear_glyph_matrix (w->desired_matrix);
10802
10803 if (CHARPOS (new_start) <= CHARPOS (start))
10804 {
10805 int first_row_y;
10806
10807 /* Don't use this method if the display starts with an ellipsis
10808 displayed for invisible text. It's not easy to handle that case
10809 below, and it's certainly not worth the effort since this is
10810 not a frequent case. */
10811 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
10812 return 0;
10813
10814 IF_DEBUG (debug_method_add (w, "twu1"));
10815
10816 /* Display up to a row that can be reused. The variable
10817 last_text_row is set to the last row displayed that displays
10818 text. Note that it.vpos == 0 if or if not there is a
10819 header-line; it's not the same as the MATRIX_ROW_VPOS! */
10820 start_display (&it, w, new_start);
10821 first_row_y = it.current_y;
10822 w->cursor.vpos = -1;
10823 last_text_row = last_reused_text_row = NULL;
10824
10825 while (it.current_y < it.last_visible_y
10826 && IT_CHARPOS (it) < CHARPOS (start)
10827 && !fonts_changed_p)
10828 if (display_line (&it))
10829 last_text_row = it.glyph_row - 1;
10830
10831 /* A value of current_y < last_visible_y means that we stopped
10832 at the previous window start, which in turn means that we
10833 have at least one reusable row. */
10834 if (it.current_y < it.last_visible_y)
10835 {
10836 /* IT.vpos always starts from 0; it counts text lines. */
10837 nrows_scrolled = it.vpos;
10838
10839 /* Find PT if not already found in the lines displayed. */
10840 if (w->cursor.vpos < 0)
10841 {
10842 int dy = it.current_y - first_row_y;
10843
10844 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10845 row = row_containing_pos (w, PT, row, NULL, dy);
10846 if (row)
10847 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
10848 dy, nrows_scrolled);
10849 else
10850 {
10851 clear_glyph_matrix (w->desired_matrix);
10852 return 0;
10853 }
10854 }
10855
10856 /* Scroll the display. Do it before the current matrix is
10857 changed. The problem here is that update has not yet
10858 run, i.e. part of the current matrix is not up to date.
10859 scroll_run_hook will clear the cursor, and use the
10860 current matrix to get the height of the row the cursor is
10861 in. */
10862 run.current_y = first_row_y;
10863 run.desired_y = it.current_y;
10864 run.height = it.last_visible_y - it.current_y;
10865
10866 if (run.height > 0 && run.current_y != run.desired_y)
10867 {
10868 update_begin (f);
10869 rif->update_window_begin_hook (w);
10870 rif->clear_mouse_face (w);
10871 rif->scroll_run_hook (w, &run);
10872 rif->update_window_end_hook (w, 0, 0);
10873 update_end (f);
10874 }
10875
10876 /* Shift current matrix down by nrows_scrolled lines. */
10877 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
10878 rotate_matrix (w->current_matrix,
10879 start_vpos,
10880 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10881 nrows_scrolled);
10882
10883 /* Disable lines that must be updated. */
10884 for (i = 0; i < it.vpos; ++i)
10885 (start_row + i)->enabled_p = 0;
10886
10887 /* Re-compute Y positions. */
10888 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10889 max_y = it.last_visible_y;
10890 for (row = start_row + nrows_scrolled;
10891 row < bottom_row;
10892 ++row)
10893 {
10894 row->y = it.current_y;
10895 row->visible_height = row->height;
10896
10897 if (row->y < min_y)
10898 row->visible_height -= min_y - row->y;
10899 if (row->y + row->height > max_y)
10900 row->visible_height -= row->y + row->height - max_y;
10901
10902 it.current_y += row->height;
10903
10904 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10905 last_reused_text_row = row;
10906 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
10907 break;
10908 }
10909
10910 /* Disable lines in the current matrix which are now
10911 below the window. */
10912 for (++row; row < bottom_row; ++row)
10913 row->enabled_p = 0;
10914 }
10915
10916 /* Update window_end_pos etc.; last_reused_text_row is the last
10917 reused row from the current matrix containing text, if any.
10918 The value of last_text_row is the last displayed line
10919 containing text. */
10920 if (last_reused_text_row)
10921 {
10922 w->window_end_bytepos
10923 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
10924 w->window_end_pos
10925 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
10926 w->window_end_vpos
10927 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
10928 w->current_matrix));
10929 }
10930 else if (last_text_row)
10931 {
10932 w->window_end_bytepos
10933 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10934 w->window_end_pos
10935 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10936 w->window_end_vpos
10937 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10938 }
10939 else
10940 {
10941 /* This window must be completely empty. */
10942 w->window_end_bytepos = 0;
10943 w->window_end_pos = w->window_end_vpos = make_number (0);
10944 }
10945 w->window_end_valid = Qnil;
10946
10947 /* Update hint: don't try scrolling again in update_window. */
10948 w->desired_matrix->no_scrolling_p = 1;
10949
10950 #if GLYPH_DEBUG
10951 debug_method_add (w, "try_window_reusing_current_matrix 1");
10952 #endif
10953 return 1;
10954 }
10955 else if (CHARPOS (new_start) > CHARPOS (start))
10956 {
10957 struct glyph_row *pt_row, *row;
10958 struct glyph_row *first_reusable_row;
10959 struct glyph_row *first_row_to_display;
10960 int dy;
10961 int yb = window_text_bottom_y (w);
10962
10963 /* Find the row starting at new_start, if there is one. Don't
10964 reuse a partially visible line at the end. */
10965 first_reusable_row = start_row;
10966 while (first_reusable_row->enabled_p
10967 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
10968 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
10969 < CHARPOS (new_start)))
10970 ++first_reusable_row;
10971
10972 /* Give up if there is no row to reuse. */
10973 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
10974 || !first_reusable_row->enabled_p
10975 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
10976 != CHARPOS (new_start)))
10977 return 0;
10978
10979 /* We can reuse fully visible rows beginning with
10980 first_reusable_row to the end of the window. Set
10981 first_row_to_display to the first row that cannot be reused.
10982 Set pt_row to the row containing point, if there is any. */
10983 pt_row = NULL;
10984 for (first_row_to_display = first_reusable_row;
10985 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
10986 ++first_row_to_display)
10987 {
10988 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
10989 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
10990 pt_row = first_row_to_display;
10991 }
10992
10993 /* Start displaying at the start of first_row_to_display. */
10994 xassert (first_row_to_display->y < yb);
10995 init_to_row_start (&it, w, first_row_to_display);
10996
10997 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
10998 - start_vpos);
10999 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
11000 - nrows_scrolled);
11001 it.current_y = (first_row_to_display->y - first_reusable_row->y
11002 + WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
11003
11004 /* Display lines beginning with first_row_to_display in the
11005 desired matrix. Set last_text_row to the last row displayed
11006 that displays text. */
11007 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
11008 if (pt_row == NULL)
11009 w->cursor.vpos = -1;
11010 last_text_row = NULL;
11011 while (it.current_y < it.last_visible_y && !fonts_changed_p)
11012 if (display_line (&it))
11013 last_text_row = it.glyph_row - 1;
11014
11015 /* Give up If point isn't in a row displayed or reused. */
11016 if (w->cursor.vpos < 0)
11017 {
11018 clear_glyph_matrix (w->desired_matrix);
11019 return 0;
11020 }
11021
11022 /* If point is in a reused row, adjust y and vpos of the cursor
11023 position. */
11024 if (pt_row)
11025 {
11026 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
11027 w->current_matrix);
11028 w->cursor.y -= first_reusable_row->y;
11029 }
11030
11031 /* Scroll the display. */
11032 run.current_y = first_reusable_row->y;
11033 run.desired_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
11034 run.height = it.last_visible_y - run.current_y;
11035 dy = run.current_y - run.desired_y;
11036
11037 if (run.height)
11038 {
11039 struct frame *f = XFRAME (WINDOW_FRAME (w));
11040 update_begin (f);
11041 rif->update_window_begin_hook (w);
11042 rif->clear_mouse_face (w);
11043 rif->scroll_run_hook (w, &run);
11044 rif->update_window_end_hook (w, 0, 0);
11045 update_end (f);
11046 }
11047
11048 /* Adjust Y positions of reused rows. */
11049 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
11050 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
11051 max_y = it.last_visible_y;
11052 for (row = first_reusable_row; row < first_row_to_display; ++row)
11053 {
11054 row->y -= dy;
11055 row->visible_height = row->height;
11056 if (row->y < min_y)
11057 row->visible_height -= min_y - row->y;
11058 if (row->y + row->height > max_y)
11059 row->visible_height -= row->y + row->height - max_y;
11060 }
11061
11062 /* Scroll the current matrix. */
11063 xassert (nrows_scrolled > 0);
11064 rotate_matrix (w->current_matrix,
11065 start_vpos,
11066 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
11067 -nrows_scrolled);
11068
11069 /* Disable rows not reused. */
11070 for (row -= nrows_scrolled; row < bottom_row; ++row)
11071 row->enabled_p = 0;
11072
11073 /* Adjust window end. A null value of last_text_row means that
11074 the window end is in reused rows which in turn means that
11075 only its vpos can have changed. */
11076 if (last_text_row)
11077 {
11078 w->window_end_bytepos
11079 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
11080 w->window_end_pos
11081 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
11082 w->window_end_vpos
11083 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
11084 }
11085 else
11086 {
11087 w->window_end_vpos
11088 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
11089 }
11090
11091 w->window_end_valid = Qnil;
11092 w->desired_matrix->no_scrolling_p = 1;
11093
11094 #if GLYPH_DEBUG
11095 debug_method_add (w, "try_window_reusing_current_matrix 2");
11096 #endif
11097 return 1;
11098 }
11099
11100 return 0;
11101 }
11102
11103
11104 \f
11105 /************************************************************************
11106 Window redisplay reusing current matrix when buffer has changed
11107 ************************************************************************/
11108
11109 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
11110 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
11111 int *, int *));
11112 static struct glyph_row *
11113 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
11114 struct glyph_row *));
11115
11116
11117 /* Return the last row in MATRIX displaying text. If row START is
11118 non-null, start searching with that row. IT gives the dimensions
11119 of the display. Value is null if matrix is empty; otherwise it is
11120 a pointer to the row found. */
11121
11122 static struct glyph_row *
11123 find_last_row_displaying_text (matrix, it, start)
11124 struct glyph_matrix *matrix;
11125 struct it *it;
11126 struct glyph_row *start;
11127 {
11128 struct glyph_row *row, *row_found;
11129
11130 /* Set row_found to the last row in IT->w's current matrix
11131 displaying text. The loop looks funny but think of partially
11132 visible lines. */
11133 row_found = NULL;
11134 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
11135 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
11136 {
11137 xassert (row->enabled_p);
11138 row_found = row;
11139 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
11140 break;
11141 ++row;
11142 }
11143
11144 return row_found;
11145 }
11146
11147
11148 /* Return the last row in the current matrix of W that is not affected
11149 by changes at the start of current_buffer that occurred since W's
11150 current matrix was built. Value is null if no such row exists.
11151
11152 BEG_UNCHANGED us the number of characters unchanged at the start of
11153 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
11154 first changed character in current_buffer. Characters at positions <
11155 BEG + BEG_UNCHANGED are at the same buffer positions as they were
11156 when the current matrix was built. */
11157
11158 static struct glyph_row *
11159 find_last_unchanged_at_beg_row (w)
11160 struct window *w;
11161 {
11162 int first_changed_pos = BEG + BEG_UNCHANGED;
11163 struct glyph_row *row;
11164 struct glyph_row *row_found = NULL;
11165 int yb = window_text_bottom_y (w);
11166
11167 /* Find the last row displaying unchanged text. */
11168 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11169 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
11170 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
11171 {
11172 if (/* If row ends before first_changed_pos, it is unchanged,
11173 except in some case. */
11174 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
11175 /* When row ends in ZV and we write at ZV it is not
11176 unchanged. */
11177 && !row->ends_at_zv_p
11178 /* When first_changed_pos is the end of a continued line,
11179 row is not unchanged because it may be no longer
11180 continued. */
11181 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
11182 && row->continued_p))
11183 row_found = row;
11184
11185 /* Stop if last visible row. */
11186 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
11187 break;
11188
11189 ++row;
11190 }
11191
11192 return row_found;
11193 }
11194
11195
11196 /* Find the first glyph row in the current matrix of W that is not
11197 affected by changes at the end of current_buffer since the
11198 time W's current matrix was built.
11199
11200 Return in *DELTA the number of chars by which buffer positions in
11201 unchanged text at the end of current_buffer must be adjusted.
11202
11203 Return in *DELTA_BYTES the corresponding number of bytes.
11204
11205 Value is null if no such row exists, i.e. all rows are affected by
11206 changes. */
11207
11208 static struct glyph_row *
11209 find_first_unchanged_at_end_row (w, delta, delta_bytes)
11210 struct window *w;
11211 int *delta, *delta_bytes;
11212 {
11213 struct glyph_row *row;
11214 struct glyph_row *row_found = NULL;
11215
11216 *delta = *delta_bytes = 0;
11217
11218 /* Display must not have been paused, otherwise the current matrix
11219 is not up to date. */
11220 if (NILP (w->window_end_valid))
11221 abort ();
11222
11223 /* A value of window_end_pos >= END_UNCHANGED means that the window
11224 end is in the range of changed text. If so, there is no
11225 unchanged row at the end of W's current matrix. */
11226 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
11227 return NULL;
11228
11229 /* Set row to the last row in W's current matrix displaying text. */
11230 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
11231
11232 /* If matrix is entirely empty, no unchanged row exists. */
11233 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
11234 {
11235 /* The value of row is the last glyph row in the matrix having a
11236 meaningful buffer position in it. The end position of row
11237 corresponds to window_end_pos. This allows us to translate
11238 buffer positions in the current matrix to current buffer
11239 positions for characters not in changed text. */
11240 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
11241 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
11242 int last_unchanged_pos, last_unchanged_pos_old;
11243 struct glyph_row *first_text_row
11244 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11245
11246 *delta = Z - Z_old;
11247 *delta_bytes = Z_BYTE - Z_BYTE_old;
11248
11249 /* Set last_unchanged_pos to the buffer position of the last
11250 character in the buffer that has not been changed. Z is the
11251 index + 1 of the last character in current_buffer, i.e. by
11252 subtracting END_UNCHANGED we get the index of the last
11253 unchanged character, and we have to add BEG to get its buffer
11254 position. */
11255 last_unchanged_pos = Z - END_UNCHANGED + BEG;
11256 last_unchanged_pos_old = last_unchanged_pos - *delta;
11257
11258 /* Search backward from ROW for a row displaying a line that
11259 starts at a minimum position >= last_unchanged_pos_old. */
11260 for (; row > first_text_row; --row)
11261 {
11262 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
11263 abort ();
11264
11265 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
11266 row_found = row;
11267 }
11268 }
11269
11270 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
11271 abort ();
11272
11273 return row_found;
11274 }
11275
11276
11277 /* Make sure that glyph rows in the current matrix of window W
11278 reference the same glyph memory as corresponding rows in the
11279 frame's frame matrix. This function is called after scrolling W's
11280 current matrix on a terminal frame in try_window_id and
11281 try_window_reusing_current_matrix. */
11282
11283 static void
11284 sync_frame_with_window_matrix_rows (w)
11285 struct window *w;
11286 {
11287 struct frame *f = XFRAME (w->frame);
11288 struct glyph_row *window_row, *window_row_end, *frame_row;
11289
11290 /* Preconditions: W must be a leaf window and full-width. Its frame
11291 must have a frame matrix. */
11292 xassert (NILP (w->hchild) && NILP (w->vchild));
11293 xassert (WINDOW_FULL_WIDTH_P (w));
11294 xassert (!FRAME_WINDOW_P (f));
11295
11296 /* If W is a full-width window, glyph pointers in W's current matrix
11297 have, by definition, to be the same as glyph pointers in the
11298 corresponding frame matrix. */
11299 window_row = w->current_matrix->rows;
11300 window_row_end = window_row + w->current_matrix->nrows;
11301 frame_row = f->current_matrix->rows + XFASTINT (w->top);
11302 while (window_row < window_row_end)
11303 {
11304 int area;
11305
11306 for (area = LEFT_MARGIN_AREA; area <= LAST_AREA; ++area)
11307 frame_row->glyphs[area] = window_row->glyphs[area];
11308
11309 /* Disable frame rows whose corresponding window rows have
11310 been disabled in try_window_id. */
11311 if (!window_row->enabled_p)
11312 frame_row->enabled_p = 0;
11313
11314 ++window_row, ++frame_row;
11315 }
11316 }
11317
11318
11319 /* Find the glyph row in window W containing CHARPOS. Consider all
11320 rows between START and END (not inclusive). END null means search
11321 all rows to the end of the display area of W. Value is the row
11322 containing CHARPOS or null. */
11323
11324 struct glyph_row *
11325 row_containing_pos (w, charpos, start, end, dy)
11326 struct window *w;
11327 int charpos;
11328 struct glyph_row *start, *end;
11329 int dy;
11330 {
11331 struct glyph_row *row = start;
11332 int last_y;
11333
11334 /* If we happen to start on a header-line, skip that. */
11335 if (row->mode_line_p)
11336 ++row;
11337
11338 if ((end && row >= end) || !row->enabled_p)
11339 return NULL;
11340
11341 last_y = window_text_bottom_y (w) - dy;
11342
11343 while ((end == NULL || row < end)
11344 && MATRIX_ROW_BOTTOM_Y (row) < last_y
11345 && (MATRIX_ROW_END_CHARPOS (row) < charpos
11346 || (MATRIX_ROW_END_CHARPOS (row) == charpos
11347 /* The end position of a row equals the start
11348 position of the next row. If CHARPOS is there, we
11349 would rather display it in the next line, except
11350 when this line ends in ZV. */
11351 && !row->ends_at_zv_p
11352 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))
11353 ++row;
11354
11355 /* Give up if CHARPOS not found. */
11356 if ((end && row >= end)
11357 || charpos < MATRIX_ROW_START_CHARPOS (row)
11358 || charpos > MATRIX_ROW_END_CHARPOS (row))
11359 row = NULL;
11360
11361 return row;
11362 }
11363
11364
11365 /* Try to redisplay window W by reusing its existing display. W's
11366 current matrix must be up to date when this function is called,
11367 i.e. window_end_valid must not be nil.
11368
11369 Value is
11370
11371 1 if display has been updated
11372 0 if otherwise unsuccessful
11373 -1 if redisplay with same window start is known not to succeed
11374
11375 The following steps are performed:
11376
11377 1. Find the last row in the current matrix of W that is not
11378 affected by changes at the start of current_buffer. If no such row
11379 is found, give up.
11380
11381 2. Find the first row in W's current matrix that is not affected by
11382 changes at the end of current_buffer. Maybe there is no such row.
11383
11384 3. Display lines beginning with the row + 1 found in step 1 to the
11385 row found in step 2 or, if step 2 didn't find a row, to the end of
11386 the window.
11387
11388 4. If cursor is not known to appear on the window, give up.
11389
11390 5. If display stopped at the row found in step 2, scroll the
11391 display and current matrix as needed.
11392
11393 6. Maybe display some lines at the end of W, if we must. This can
11394 happen under various circumstances, like a partially visible line
11395 becoming fully visible, or because newly displayed lines are displayed
11396 in smaller font sizes.
11397
11398 7. Update W's window end information. */
11399
11400 static int
11401 try_window_id (w)
11402 struct window *w;
11403 {
11404 struct frame *f = XFRAME (w->frame);
11405 struct glyph_matrix *current_matrix = w->current_matrix;
11406 struct glyph_matrix *desired_matrix = w->desired_matrix;
11407 struct glyph_row *last_unchanged_at_beg_row;
11408 struct glyph_row *first_unchanged_at_end_row;
11409 struct glyph_row *row;
11410 struct glyph_row *bottom_row;
11411 int bottom_vpos;
11412 struct it it;
11413 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
11414 struct text_pos start_pos;
11415 struct run run;
11416 int first_unchanged_at_end_vpos = 0;
11417 struct glyph_row *last_text_row, *last_text_row_at_end;
11418 struct text_pos start;
11419 int first_changed_charpos, last_changed_charpos;
11420
11421 #if GLYPH_DEBUG
11422 if (inhibit_try_window_id)
11423 return 0;
11424 #endif
11425
11426 /* This is handy for debugging. */
11427 #if 0
11428 #define GIVE_UP(X) \
11429 do { \
11430 fprintf (stderr, "try_window_id give up %d\n", (X)); \
11431 return 0; \
11432 } while (0)
11433 #else
11434 #define GIVE_UP(X) return 0
11435 #endif
11436
11437 SET_TEXT_POS_FROM_MARKER (start, w->start);
11438
11439 /* Don't use this for mini-windows because these can show
11440 messages and mini-buffers, and we don't handle that here. */
11441 if (MINI_WINDOW_P (w))
11442 GIVE_UP (1);
11443
11444 /* This flag is used to prevent redisplay optimizations. */
11445 if (windows_or_buffers_changed)
11446 GIVE_UP (2);
11447
11448 /* Verify that narrowing has not changed. This flag is also set to prevent
11449 redisplay optimizations. It would be nice to further
11450 reduce the number of cases where this prevents try_window_id. */
11451 if (current_buffer->clip_changed)
11452 GIVE_UP (3);
11453
11454 /* Window must either use window-based redisplay or be full width. */
11455 if (!FRAME_WINDOW_P (f)
11456 && (!line_ins_del_ok
11457 || !WINDOW_FULL_WIDTH_P (w)))
11458 GIVE_UP (4);
11459
11460 /* Give up if point is not known NOT to appear in W. */
11461 if (PT < CHARPOS (start))
11462 GIVE_UP (5);
11463
11464 /* Another way to prevent redisplay optimizations. */
11465 if (XFASTINT (w->last_modified) == 0)
11466 GIVE_UP (6);
11467
11468 /* Verify that window is not hscrolled. */
11469 if (XFASTINT (w->hscroll) != 0)
11470 GIVE_UP (7);
11471
11472 /* Verify that display wasn't paused. */
11473 if (NILP (w->window_end_valid))
11474 GIVE_UP (8);
11475
11476 /* Can't use this if highlighting a region because a cursor movement
11477 will do more than just set the cursor. */
11478 if (!NILP (Vtransient_mark_mode)
11479 && !NILP (current_buffer->mark_active))
11480 GIVE_UP (9);
11481
11482 /* Likewise if highlighting trailing whitespace. */
11483 if (!NILP (Vshow_trailing_whitespace))
11484 GIVE_UP (11);
11485
11486 /* Likewise if showing a region. */
11487 if (!NILP (w->region_showing))
11488 GIVE_UP (10);
11489
11490 /* Can use this if overlay arrow position and or string have changed. */
11491 if (!EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
11492 || !EQ (last_arrow_string, Voverlay_arrow_string))
11493 GIVE_UP (12);
11494
11495
11496 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
11497 only if buffer has really changed. The reason is that the gap is
11498 initially at Z for freshly visited files. The code below would
11499 set end_unchanged to 0 in that case. */
11500 if (MODIFF > SAVE_MODIFF
11501 /* This seems to happen sometimes after saving a buffer. */
11502 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
11503 {
11504 if (GPT - BEG < BEG_UNCHANGED)
11505 BEG_UNCHANGED = GPT - BEG;
11506 if (Z - GPT < END_UNCHANGED)
11507 END_UNCHANGED = Z - GPT;
11508 }
11509
11510 /* The position of the first and last character that has been changed. */
11511 first_changed_charpos = BEG + BEG_UNCHANGED;
11512 last_changed_charpos = Z - END_UNCHANGED;
11513
11514 /* If window starts after a line end, and the last change is in
11515 front of that newline, then changes don't affect the display.
11516 This case happens with stealth-fontification. Note that although
11517 the display is unchanged, glyph positions in the matrix have to
11518 be adjusted, of course. */
11519 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
11520 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
11521 && ((last_changed_charpos < CHARPOS (start)
11522 && CHARPOS (start) == BEGV)
11523 || (last_changed_charpos < CHARPOS (start) - 1
11524 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
11525 {
11526 int Z_old, delta, Z_BYTE_old, delta_bytes;
11527 struct glyph_row *r0;
11528
11529 /* Compute how many chars/bytes have been added to or removed
11530 from the buffer. */
11531 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
11532 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
11533 delta = Z - Z_old;
11534 delta_bytes = Z_BYTE - Z_BYTE_old;
11535
11536 /* Give up if PT is not in the window. Note that it already has
11537 been checked at the start of try_window_id that PT is not in
11538 front of the window start. */
11539 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
11540 GIVE_UP (13);
11541
11542 /* If window start is unchanged, we can reuse the whole matrix
11543 as is, after adjusting glyph positions. No need to compute
11544 the window end again, since its offset from Z hasn't changed. */
11545 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
11546 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
11547 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes)
11548 {
11549 /* Adjust positions in the glyph matrix. */
11550 if (delta || delta_bytes)
11551 {
11552 struct glyph_row *r1
11553 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11554 increment_matrix_positions (w->current_matrix,
11555 MATRIX_ROW_VPOS (r0, current_matrix),
11556 MATRIX_ROW_VPOS (r1, current_matrix),
11557 delta, delta_bytes);
11558 }
11559
11560 /* Set the cursor. */
11561 row = row_containing_pos (w, PT, r0, NULL, 0);
11562 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
11563 return 1;
11564 }
11565 }
11566
11567 /* Handle the case that changes are all below what is displayed in
11568 the window, and that PT is in the window. This shortcut cannot
11569 be taken if ZV is visible in the window, and text has been added
11570 there that is visible in the window. */
11571 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
11572 /* ZV is not visible in the window, or there are no
11573 changes at ZV, actually. */
11574 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
11575 || first_changed_charpos == last_changed_charpos))
11576 {
11577 struct glyph_row *r0;
11578
11579 /* Give up if PT is not in the window. Note that it already has
11580 been checked at the start of try_window_id that PT is not in
11581 front of the window start. */
11582 if (PT >= MATRIX_ROW_END_CHARPOS (row))
11583 GIVE_UP (14);
11584
11585 /* If window start is unchanged, we can reuse the whole matrix
11586 as is, without changing glyph positions since no text has
11587 been added/removed in front of the window end. */
11588 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
11589 if (TEXT_POS_EQUAL_P (start, r0->start.pos))
11590 {
11591 /* We have to compute the window end anew since text
11592 can have been added/removed after it. */
11593 w->window_end_pos
11594 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
11595 w->window_end_bytepos
11596 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11597
11598 /* Set the cursor. */
11599 row = row_containing_pos (w, PT, r0, NULL, 0);
11600 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
11601 return 2;
11602 }
11603 }
11604
11605 /* Give up if window start is in the changed area.
11606
11607 The condition used to read
11608
11609 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
11610
11611 but why that was tested escapes me at the moment. */
11612 if (CHARPOS (start) >= first_changed_charpos
11613 && CHARPOS (start) <= last_changed_charpos)
11614 GIVE_UP (15);
11615
11616 /* Check that window start agrees with the start of the first glyph
11617 row in its current matrix. Check this after we know the window
11618 start is not in changed text, otherwise positions would not be
11619 comparable. */
11620 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
11621 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
11622 GIVE_UP (16);
11623
11624 /* Give up if the window ends in strings. Overlay strings
11625 at the end are difficult to handle, so don't try. */
11626 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
11627 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
11628 GIVE_UP (20);
11629
11630 /* Compute the position at which we have to start displaying new
11631 lines. Some of the lines at the top of the window might be
11632 reusable because they are not displaying changed text. Find the
11633 last row in W's current matrix not affected by changes at the
11634 start of current_buffer. Value is null if changes start in the
11635 first line of window. */
11636 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
11637 if (last_unchanged_at_beg_row)
11638 {
11639 /* Avoid starting to display in the moddle of a character, a TAB
11640 for instance. This is easier than to set up the iterator
11641 exactly, and it's not a frequent case, so the additional
11642 effort wouldn't really pay off. */
11643 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
11644 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
11645 && last_unchanged_at_beg_row > w->current_matrix->rows)
11646 --last_unchanged_at_beg_row;
11647
11648 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
11649 GIVE_UP (17);
11650
11651 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
11652 GIVE_UP (18);
11653 start_pos = it.current.pos;
11654
11655 /* Start displaying new lines in the desired matrix at the same
11656 vpos we would use in the current matrix, i.e. below
11657 last_unchanged_at_beg_row. */
11658 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
11659 current_matrix);
11660 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11661 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
11662
11663 xassert (it.hpos == 0 && it.current_x == 0);
11664 }
11665 else
11666 {
11667 /* There are no reusable lines at the start of the window.
11668 Start displaying in the first line. */
11669 start_display (&it, w, start);
11670 start_pos = it.current.pos;
11671 }
11672
11673 /* Find the first row that is not affected by changes at the end of
11674 the buffer. Value will be null if there is no unchanged row, in
11675 which case we must redisplay to the end of the window. delta
11676 will be set to the value by which buffer positions beginning with
11677 first_unchanged_at_end_row have to be adjusted due to text
11678 changes. */
11679 first_unchanged_at_end_row
11680 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
11681 IF_DEBUG (debug_delta = delta);
11682 IF_DEBUG (debug_delta_bytes = delta_bytes);
11683
11684 /* Set stop_pos to the buffer position up to which we will have to
11685 display new lines. If first_unchanged_at_end_row != NULL, this
11686 is the buffer position of the start of the line displayed in that
11687 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
11688 that we don't stop at a buffer position. */
11689 stop_pos = 0;
11690 if (first_unchanged_at_end_row)
11691 {
11692 xassert (last_unchanged_at_beg_row == NULL
11693 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
11694
11695 /* If this is a continuation line, move forward to the next one
11696 that isn't. Changes in lines above affect this line.
11697 Caution: this may move first_unchanged_at_end_row to a row
11698 not displaying text. */
11699 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
11700 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11701 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11702 < it.last_visible_y))
11703 ++first_unchanged_at_end_row;
11704
11705 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11706 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11707 >= it.last_visible_y))
11708 first_unchanged_at_end_row = NULL;
11709 else
11710 {
11711 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
11712 + delta);
11713 first_unchanged_at_end_vpos
11714 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
11715 xassert (stop_pos >= Z - END_UNCHANGED);
11716 }
11717 }
11718 else if (last_unchanged_at_beg_row == NULL)
11719 GIVE_UP (19);
11720
11721
11722 #if GLYPH_DEBUG
11723
11724 /* Either there is no unchanged row at the end, or the one we have
11725 now displays text. This is a necessary condition for the window
11726 end pos calculation at the end of this function. */
11727 xassert (first_unchanged_at_end_row == NULL
11728 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
11729
11730 debug_last_unchanged_at_beg_vpos
11731 = (last_unchanged_at_beg_row
11732 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
11733 : -1);
11734 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
11735
11736 #endif /* GLYPH_DEBUG != 0 */
11737
11738
11739 /* Display new lines. Set last_text_row to the last new line
11740 displayed which has text on it, i.e. might end up as being the
11741 line where the window_end_vpos is. */
11742 w->cursor.vpos = -1;
11743 last_text_row = NULL;
11744 overlay_arrow_seen = 0;
11745 while (it.current_y < it.last_visible_y
11746 && !fonts_changed_p
11747 && (first_unchanged_at_end_row == NULL
11748 || IT_CHARPOS (it) < stop_pos))
11749 {
11750 if (display_line (&it))
11751 last_text_row = it.glyph_row - 1;
11752 }
11753
11754 if (fonts_changed_p)
11755 return -1;
11756
11757
11758 /* Compute differences in buffer positions, y-positions etc. for
11759 lines reused at the bottom of the window. Compute what we can
11760 scroll. */
11761 if (first_unchanged_at_end_row
11762 /* No lines reused because we displayed everything up to the
11763 bottom of the window. */
11764 && it.current_y < it.last_visible_y)
11765 {
11766 dvpos = (it.vpos
11767 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
11768 current_matrix));
11769 dy = it.current_y - first_unchanged_at_end_row->y;
11770 run.current_y = first_unchanged_at_end_row->y;
11771 run.desired_y = run.current_y + dy;
11772 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
11773 }
11774 else
11775 {
11776 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
11777 first_unchanged_at_end_row = NULL;
11778 }
11779 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
11780
11781
11782 /* Find the cursor if not already found. We have to decide whether
11783 PT will appear on this window (it sometimes doesn't, but this is
11784 not a very frequent case.) This decision has to be made before
11785 the current matrix is altered. A value of cursor.vpos < 0 means
11786 that PT is either in one of the lines beginning at
11787 first_unchanged_at_end_row or below the window. Don't care for
11788 lines that might be displayed later at the window end; as
11789 mentioned, this is not a frequent case. */
11790 if (w->cursor.vpos < 0)
11791 {
11792 /* Cursor in unchanged rows at the top? */
11793 if (PT < CHARPOS (start_pos)
11794 && last_unchanged_at_beg_row)
11795 {
11796 row = row_containing_pos (w, PT,
11797 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
11798 last_unchanged_at_beg_row + 1, 0);
11799 if (row)
11800 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11801 }
11802
11803 /* Start from first_unchanged_at_end_row looking for PT. */
11804 else if (first_unchanged_at_end_row)
11805 {
11806 row = row_containing_pos (w, PT - delta,
11807 first_unchanged_at_end_row, NULL, 0);
11808 if (row)
11809 set_cursor_from_row (w, row, w->current_matrix, delta,
11810 delta_bytes, dy, dvpos);
11811 }
11812
11813 /* Give up if cursor was not found. */
11814 if (w->cursor.vpos < 0)
11815 {
11816 clear_glyph_matrix (w->desired_matrix);
11817 return -1;
11818 }
11819 }
11820
11821 /* Don't let the cursor end in the scroll margins. */
11822 {
11823 int this_scroll_margin, cursor_height;
11824
11825 this_scroll_margin = max (0, scroll_margin);
11826 this_scroll_margin = min (this_scroll_margin,
11827 XFASTINT (w->height) / 4);
11828 this_scroll_margin *= CANON_Y_UNIT (it.f);
11829 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
11830
11831 if ((w->cursor.y < this_scroll_margin
11832 && CHARPOS (start) > BEGV)
11833 /* Don't take scroll margin into account at the bottom because
11834 old redisplay didn't do it either. */
11835 || w->cursor.y + cursor_height > it.last_visible_y)
11836 {
11837 w->cursor.vpos = -1;
11838 clear_glyph_matrix (w->desired_matrix);
11839 return -1;
11840 }
11841 }
11842
11843 /* Scroll the display. Do it before changing the current matrix so
11844 that xterm.c doesn't get confused about where the cursor glyph is
11845 found. */
11846 if (dy && run.height)
11847 {
11848 update_begin (f);
11849
11850 if (FRAME_WINDOW_P (f))
11851 {
11852 rif->update_window_begin_hook (w);
11853 rif->clear_mouse_face (w);
11854 rif->scroll_run_hook (w, &run);
11855 rif->update_window_end_hook (w, 0, 0);
11856 }
11857 else
11858 {
11859 /* Terminal frame. In this case, dvpos gives the number of
11860 lines to scroll by; dvpos < 0 means scroll up. */
11861 int first_unchanged_at_end_vpos
11862 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
11863 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
11864 int end = (XFASTINT (w->top)
11865 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
11866 + window_internal_height (w));
11867
11868 /* Perform the operation on the screen. */
11869 if (dvpos > 0)
11870 {
11871 /* Scroll last_unchanged_at_beg_row to the end of the
11872 window down dvpos lines. */
11873 set_terminal_window (end);
11874
11875 /* On dumb terminals delete dvpos lines at the end
11876 before inserting dvpos empty lines. */
11877 if (!scroll_region_ok)
11878 ins_del_lines (end - dvpos, -dvpos);
11879
11880 /* Insert dvpos empty lines in front of
11881 last_unchanged_at_beg_row. */
11882 ins_del_lines (from, dvpos);
11883 }
11884 else if (dvpos < 0)
11885 {
11886 /* Scroll up last_unchanged_at_beg_vpos to the end of
11887 the window to last_unchanged_at_beg_vpos - |dvpos|. */
11888 set_terminal_window (end);
11889
11890 /* Delete dvpos lines in front of
11891 last_unchanged_at_beg_vpos. ins_del_lines will set
11892 the cursor to the given vpos and emit |dvpos| delete
11893 line sequences. */
11894 ins_del_lines (from + dvpos, dvpos);
11895
11896 /* On a dumb terminal insert dvpos empty lines at the
11897 end. */
11898 if (!scroll_region_ok)
11899 ins_del_lines (end + dvpos, -dvpos);
11900 }
11901
11902 set_terminal_window (0);
11903 }
11904
11905 update_end (f);
11906 }
11907
11908 /* Shift reused rows of the current matrix to the right position.
11909 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
11910 text. */
11911 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11912 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
11913 if (dvpos < 0)
11914 {
11915 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
11916 bottom_vpos, dvpos);
11917 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
11918 bottom_vpos, 0);
11919 }
11920 else if (dvpos > 0)
11921 {
11922 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
11923 bottom_vpos, dvpos);
11924 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
11925 first_unchanged_at_end_vpos + dvpos, 0);
11926 }
11927
11928 /* For frame-based redisplay, make sure that current frame and window
11929 matrix are in sync with respect to glyph memory. */
11930 if (!FRAME_WINDOW_P (f))
11931 sync_frame_with_window_matrix_rows (w);
11932
11933 /* Adjust buffer positions in reused rows. */
11934 if (delta)
11935 increment_matrix_positions (current_matrix,
11936 first_unchanged_at_end_vpos + dvpos,
11937 bottom_vpos, delta, delta_bytes);
11938
11939 /* Adjust Y positions. */
11940 if (dy)
11941 shift_glyph_matrix (w, current_matrix,
11942 first_unchanged_at_end_vpos + dvpos,
11943 bottom_vpos, dy);
11944
11945 if (first_unchanged_at_end_row)
11946 first_unchanged_at_end_row += dvpos;
11947
11948 /* If scrolling up, there may be some lines to display at the end of
11949 the window. */
11950 last_text_row_at_end = NULL;
11951 if (dy < 0)
11952 {
11953 /* Scrolling up can leave for example a partially visible line
11954 at the end of the window to be redisplayed. */
11955 /* Set last_row to the glyph row in the current matrix where the
11956 window end line is found. It has been moved up or down in
11957 the matrix by dvpos. */
11958 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
11959 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
11960
11961 /* If last_row is the window end line, it should display text. */
11962 xassert (last_row->displays_text_p);
11963
11964 /* If window end line was partially visible before, begin
11965 displaying at that line. Otherwise begin displaying with the
11966 line following it. */
11967 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
11968 {
11969 init_to_row_start (&it, w, last_row);
11970 it.vpos = last_vpos;
11971 it.current_y = last_row->y;
11972 }
11973 else
11974 {
11975 init_to_row_end (&it, w, last_row);
11976 it.vpos = 1 + last_vpos;
11977 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
11978 ++last_row;
11979 }
11980
11981 /* We may start in a continuation line. If so, we have to
11982 get the right continuation_lines_width and current_x. */
11983 it.continuation_lines_width = last_row->continuation_lines_width;
11984 it.hpos = it.current_x = 0;
11985
11986 /* Display the rest of the lines at the window end. */
11987 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11988 while (it.current_y < it.last_visible_y
11989 && !fonts_changed_p)
11990 {
11991 /* Is it always sure that the display agrees with lines in
11992 the current matrix? I don't think so, so we mark rows
11993 displayed invalid in the current matrix by setting their
11994 enabled_p flag to zero. */
11995 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
11996 if (display_line (&it))
11997 last_text_row_at_end = it.glyph_row - 1;
11998 }
11999 }
12000
12001 /* Update window_end_pos and window_end_vpos. */
12002 if (first_unchanged_at_end_row
12003 && first_unchanged_at_end_row->y < it.last_visible_y
12004 && !last_text_row_at_end)
12005 {
12006 /* Window end line if one of the preserved rows from the current
12007 matrix. Set row to the last row displaying text in current
12008 matrix starting at first_unchanged_at_end_row, after
12009 scrolling. */
12010 xassert (first_unchanged_at_end_row->displays_text_p);
12011 row = find_last_row_displaying_text (w->current_matrix, &it,
12012 first_unchanged_at_end_row);
12013 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
12014
12015 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
12016 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
12017 w->window_end_vpos
12018 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
12019 xassert (w->window_end_bytepos >= 0);
12020 IF_DEBUG (debug_method_add (w, "A"));
12021 }
12022 else if (last_text_row_at_end)
12023 {
12024 w->window_end_pos
12025 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
12026 w->window_end_bytepos
12027 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
12028 w->window_end_vpos
12029 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
12030 xassert (w->window_end_bytepos >= 0);
12031 IF_DEBUG (debug_method_add (w, "B"));
12032 }
12033 else if (last_text_row)
12034 {
12035 /* We have displayed either to the end of the window or at the
12036 end of the window, i.e. the last row with text is to be found
12037 in the desired matrix. */
12038 w->window_end_pos
12039 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12040 w->window_end_bytepos
12041 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12042 w->window_end_vpos
12043 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
12044 xassert (w->window_end_bytepos >= 0);
12045 }
12046 else if (first_unchanged_at_end_row == NULL
12047 && last_text_row == NULL
12048 && last_text_row_at_end == NULL)
12049 {
12050 /* Displayed to end of window, but no line containing text was
12051 displayed. Lines were deleted at the end of the window. */
12052 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
12053 int vpos = XFASTINT (w->window_end_vpos);
12054 struct glyph_row *current_row = current_matrix->rows + vpos;
12055 struct glyph_row *desired_row = desired_matrix->rows + vpos;
12056
12057 for (row = NULL;
12058 row == NULL && vpos >= first_vpos;
12059 --vpos, --current_row, --desired_row)
12060 {
12061 if (desired_row->enabled_p)
12062 {
12063 if (desired_row->displays_text_p)
12064 row = desired_row;
12065 }
12066 else if (current_row->displays_text_p)
12067 row = current_row;
12068 }
12069
12070 xassert (row != NULL);
12071 w->window_end_vpos = make_number (vpos + 1);
12072 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
12073 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
12074 xassert (w->window_end_bytepos >= 0);
12075 IF_DEBUG (debug_method_add (w, "C"));
12076 }
12077 else
12078 abort ();
12079
12080 #if 0 /* This leads to problems, for instance when the cursor is
12081 at ZV, and the cursor line displays no text. */
12082 /* Disable rows below what's displayed in the window. This makes
12083 debugging easier. */
12084 enable_glyph_matrix_rows (current_matrix,
12085 XFASTINT (w->window_end_vpos) + 1,
12086 bottom_vpos, 0);
12087 #endif
12088
12089 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
12090 debug_end_vpos = XFASTINT (w->window_end_vpos));
12091
12092 /* Record that display has not been completed. */
12093 w->window_end_valid = Qnil;
12094 w->desired_matrix->no_scrolling_p = 1;
12095 return 3;
12096
12097 #undef GIVE_UP
12098 }
12099
12100
12101 \f
12102 /***********************************************************************
12103 More debugging support
12104 ***********************************************************************/
12105
12106 #if GLYPH_DEBUG
12107
12108 void dump_glyph_row P_ ((struct glyph_row *, int, int));
12109 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
12110 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
12111
12112
12113 /* Dump the contents of glyph matrix MATRIX on stderr.
12114
12115 GLYPHS 0 means don't show glyph contents.
12116 GLYPHS 1 means show glyphs in short form
12117 GLYPHS > 1 means show glyphs in long form. */
12118
12119 void
12120 dump_glyph_matrix (matrix, glyphs)
12121 struct glyph_matrix *matrix;
12122 int glyphs;
12123 {
12124 int i;
12125 for (i = 0; i < matrix->nrows; ++i)
12126 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
12127 }
12128
12129
12130 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
12131 the glyph row and area where the glyph comes from. */
12132
12133 void
12134 dump_glyph (row, glyph, area)
12135 struct glyph_row *row;
12136 struct glyph *glyph;
12137 int area;
12138 {
12139 if (glyph->type == CHAR_GLYPH)
12140 {
12141 fprintf (stderr,
12142 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12143 glyph - row->glyphs[TEXT_AREA],
12144 'C',
12145 glyph->charpos,
12146 (BUFFERP (glyph->object)
12147 ? 'B'
12148 : (STRINGP (glyph->object)
12149 ? 'S'
12150 : '-')),
12151 glyph->pixel_width,
12152 glyph->u.ch,
12153 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
12154 ? glyph->u.ch
12155 : '.'),
12156 glyph->face_id,
12157 glyph->left_box_line_p,
12158 glyph->right_box_line_p);
12159 }
12160 else if (glyph->type == STRETCH_GLYPH)
12161 {
12162 fprintf (stderr,
12163 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12164 glyph - row->glyphs[TEXT_AREA],
12165 'S',
12166 glyph->charpos,
12167 (BUFFERP (glyph->object)
12168 ? 'B'
12169 : (STRINGP (glyph->object)
12170 ? 'S'
12171 : '-')),
12172 glyph->pixel_width,
12173 0,
12174 '.',
12175 glyph->face_id,
12176 glyph->left_box_line_p,
12177 glyph->right_box_line_p);
12178 }
12179 else if (glyph->type == IMAGE_GLYPH)
12180 {
12181 fprintf (stderr,
12182 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12183 glyph - row->glyphs[TEXT_AREA],
12184 'I',
12185 glyph->charpos,
12186 (BUFFERP (glyph->object)
12187 ? 'B'
12188 : (STRINGP (glyph->object)
12189 ? 'S'
12190 : '-')),
12191 glyph->pixel_width,
12192 glyph->u.img_id,
12193 '.',
12194 glyph->face_id,
12195 glyph->left_box_line_p,
12196 glyph->right_box_line_p);
12197 }
12198 }
12199
12200
12201 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
12202 GLYPHS 0 means don't show glyph contents.
12203 GLYPHS 1 means show glyphs in short form
12204 GLYPHS > 1 means show glyphs in long form. */
12205
12206 void
12207 dump_glyph_row (row, vpos, glyphs)
12208 struct glyph_row *row;
12209 int vpos, glyphs;
12210 {
12211 if (glyphs != 1)
12212 {
12213 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
12214 fprintf (stderr, "=======================================================================\n");
12215
12216 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d\
12217 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
12218 vpos,
12219 MATRIX_ROW_START_CHARPOS (row),
12220 MATRIX_ROW_END_CHARPOS (row),
12221 row->used[TEXT_AREA],
12222 row->contains_overlapping_glyphs_p,
12223 row->enabled_p,
12224 row->truncated_on_left_p,
12225 row->truncated_on_right_p,
12226 row->overlay_arrow_p,
12227 row->continued_p,
12228 MATRIX_ROW_CONTINUATION_LINE_P (row),
12229 row->displays_text_p,
12230 row->ends_at_zv_p,
12231 row->fill_line_p,
12232 row->ends_in_middle_of_char_p,
12233 row->starts_in_middle_of_char_p,
12234 row->mouse_face_p,
12235 row->x,
12236 row->y,
12237 row->pixel_width,
12238 row->height,
12239 row->visible_height,
12240 row->ascent,
12241 row->phys_ascent);
12242 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
12243 row->end.overlay_string_index,
12244 row->continuation_lines_width);
12245 fprintf (stderr, "%9d %5d\n",
12246 CHARPOS (row->start.string_pos),
12247 CHARPOS (row->end.string_pos));
12248 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
12249 row->end.dpvec_index);
12250 }
12251
12252 if (glyphs > 1)
12253 {
12254 int area;
12255
12256 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12257 {
12258 struct glyph *glyph = row->glyphs[area];
12259 struct glyph *glyph_end = glyph + row->used[area];
12260
12261 /* Glyph for a line end in text. */
12262 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
12263 ++glyph_end;
12264
12265 if (glyph < glyph_end)
12266 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
12267
12268 for (; glyph < glyph_end; ++glyph)
12269 dump_glyph (row, glyph, area);
12270 }
12271 }
12272 else if (glyphs == 1)
12273 {
12274 int area;
12275
12276 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12277 {
12278 char *s = (char *) alloca (row->used[area] + 1);
12279 int i;
12280
12281 for (i = 0; i < row->used[area]; ++i)
12282 {
12283 struct glyph *glyph = row->glyphs[area] + i;
12284 if (glyph->type == CHAR_GLYPH
12285 && glyph->u.ch < 0x80
12286 && glyph->u.ch >= ' ')
12287 s[i] = glyph->u.ch;
12288 else
12289 s[i] = '.';
12290 }
12291
12292 s[i] = '\0';
12293 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
12294 }
12295 }
12296 }
12297
12298
12299 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
12300 Sdump_glyph_matrix, 0, 1, "p",
12301 doc: /* Dump the current matrix of the selected window to stderr.
12302 Shows contents of glyph row structures. With non-nil
12303 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
12304 glyphs in short form, otherwise show glyphs in long form. */)
12305 (glyphs)
12306 Lisp_Object glyphs;
12307 {
12308 struct window *w = XWINDOW (selected_window);
12309 struct buffer *buffer = XBUFFER (w->buffer);
12310
12311 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
12312 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
12313 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
12314 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
12315 fprintf (stderr, "=============================================\n");
12316 dump_glyph_matrix (w->current_matrix,
12317 NILP (glyphs) ? 0 : XINT (glyphs));
12318 return Qnil;
12319 }
12320
12321
12322 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
12323 doc: /* Dump glyph row ROW to stderr.
12324 GLYPH 0 means don't dump glyphs.
12325 GLYPH 1 means dump glyphs in short form.
12326 GLYPH > 1 or omitted means dump glyphs in long form. */)
12327 (row, glyphs)
12328 Lisp_Object row, glyphs;
12329 {
12330 struct glyph_matrix *matrix;
12331 int vpos;
12332
12333 CHECK_NUMBER (row);
12334 matrix = XWINDOW (selected_window)->current_matrix;
12335 vpos = XINT (row);
12336 if (vpos >= 0 && vpos < matrix->nrows)
12337 dump_glyph_row (MATRIX_ROW (matrix, vpos),
12338 vpos,
12339 INTEGERP (glyphs) ? XINT (glyphs) : 2);
12340 return Qnil;
12341 }
12342
12343
12344 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
12345 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
12346 GLYPH 0 means don't dump glyphs.
12347 GLYPH 1 means dump glyphs in short form.
12348 GLYPH > 1 or omitted means dump glyphs in long form. */)
12349 (row, glyphs)
12350 Lisp_Object row, glyphs;
12351 {
12352 struct frame *sf = SELECTED_FRAME ();
12353 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
12354 int vpos;
12355
12356 CHECK_NUMBER (row);
12357 vpos = XINT (row);
12358 if (vpos >= 0 && vpos < m->nrows)
12359 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
12360 INTEGERP (glyphs) ? XINT (glyphs) : 2);
12361 return Qnil;
12362 }
12363
12364
12365 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
12366 doc: /* Toggle tracing of redisplay.
12367 With ARG, turn tracing on if and only if ARG is positive. */)
12368 (arg)
12369 Lisp_Object arg;
12370 {
12371 if (NILP (arg))
12372 trace_redisplay_p = !trace_redisplay_p;
12373 else
12374 {
12375 arg = Fprefix_numeric_value (arg);
12376 trace_redisplay_p = XINT (arg) > 0;
12377 }
12378
12379 return Qnil;
12380 }
12381
12382
12383 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
12384 doc: /* Like `format', but print result to stderr. */)
12385 (nargs, args)
12386 int nargs;
12387 Lisp_Object *args;
12388 {
12389 Lisp_Object s = Fformat (nargs, args);
12390 fprintf (stderr, "%s", XSTRING (s)->data);
12391 return Qnil;
12392 }
12393
12394 #endif /* GLYPH_DEBUG */
12395
12396
12397 \f
12398 /***********************************************************************
12399 Building Desired Matrix Rows
12400 ***********************************************************************/
12401
12402 /* Return a temporary glyph row holding the glyphs of an overlay
12403 arrow. Only used for non-window-redisplay windows. */
12404
12405 static struct glyph_row *
12406 get_overlay_arrow_glyph_row (w)
12407 struct window *w;
12408 {
12409 struct frame *f = XFRAME (WINDOW_FRAME (w));
12410 struct buffer *buffer = XBUFFER (w->buffer);
12411 struct buffer *old = current_buffer;
12412 unsigned char *arrow_string = XSTRING (Voverlay_arrow_string)->data;
12413 int arrow_len = XSTRING (Voverlay_arrow_string)->size;
12414 unsigned char *arrow_end = arrow_string + arrow_len;
12415 unsigned char *p;
12416 struct it it;
12417 int multibyte_p;
12418 int n_glyphs_before;
12419
12420 set_buffer_temp (buffer);
12421 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
12422 it.glyph_row->used[TEXT_AREA] = 0;
12423 SET_TEXT_POS (it.position, 0, 0);
12424
12425 multibyte_p = !NILP (buffer->enable_multibyte_characters);
12426 p = arrow_string;
12427 while (p < arrow_end)
12428 {
12429 Lisp_Object face, ilisp;
12430
12431 /* Get the next character. */
12432 if (multibyte_p)
12433 it.c = string_char_and_length (p, arrow_len, &it.len);
12434 else
12435 it.c = *p, it.len = 1;
12436 p += it.len;
12437
12438 /* Get its face. */
12439 ilisp = make_number (p - arrow_string);
12440 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
12441 it.face_id = compute_char_face (f, it.c, face);
12442
12443 /* Compute its width, get its glyphs. */
12444 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
12445 SET_TEXT_POS (it.position, -1, -1);
12446 PRODUCE_GLYPHS (&it);
12447
12448 /* If this character doesn't fit any more in the line, we have
12449 to remove some glyphs. */
12450 if (it.current_x > it.last_visible_x)
12451 {
12452 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
12453 break;
12454 }
12455 }
12456
12457 set_buffer_temp (old);
12458 return it.glyph_row;
12459 }
12460
12461
12462 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
12463 glyphs are only inserted for terminal frames since we can't really
12464 win with truncation glyphs when partially visible glyphs are
12465 involved. Which glyphs to insert is determined by
12466 produce_special_glyphs. */
12467
12468 static void
12469 insert_left_trunc_glyphs (it)
12470 struct it *it;
12471 {
12472 struct it truncate_it;
12473 struct glyph *from, *end, *to, *toend;
12474
12475 xassert (!FRAME_WINDOW_P (it->f));
12476
12477 /* Get the truncation glyphs. */
12478 truncate_it = *it;
12479 truncate_it.current_x = 0;
12480 truncate_it.face_id = DEFAULT_FACE_ID;
12481 truncate_it.glyph_row = &scratch_glyph_row;
12482 truncate_it.glyph_row->used[TEXT_AREA] = 0;
12483 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
12484 truncate_it.object = make_number (0);
12485 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
12486
12487 /* Overwrite glyphs from IT with truncation glyphs. */
12488 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
12489 end = from + truncate_it.glyph_row->used[TEXT_AREA];
12490 to = it->glyph_row->glyphs[TEXT_AREA];
12491 toend = to + it->glyph_row->used[TEXT_AREA];
12492
12493 while (from < end)
12494 *to++ = *from++;
12495
12496 /* There may be padding glyphs left over. Overwrite them too. */
12497 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
12498 {
12499 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
12500 while (from < end)
12501 *to++ = *from++;
12502 }
12503
12504 if (to > toend)
12505 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
12506 }
12507
12508
12509 /* Compute the pixel height and width of IT->glyph_row.
12510
12511 Most of the time, ascent and height of a display line will be equal
12512 to the max_ascent and max_height values of the display iterator
12513 structure. This is not the case if
12514
12515 1. We hit ZV without displaying anything. In this case, max_ascent
12516 and max_height will be zero.
12517
12518 2. We have some glyphs that don't contribute to the line height.
12519 (The glyph row flag contributes_to_line_height_p is for future
12520 pixmap extensions).
12521
12522 The first case is easily covered by using default values because in
12523 these cases, the line height does not really matter, except that it
12524 must not be zero. */
12525
12526 static void
12527 compute_line_metrics (it)
12528 struct it *it;
12529 {
12530 struct glyph_row *row = it->glyph_row;
12531 int area, i;
12532
12533 if (FRAME_WINDOW_P (it->f))
12534 {
12535 int i, min_y, max_y;
12536
12537 /* The line may consist of one space only, that was added to
12538 place the cursor on it. If so, the row's height hasn't been
12539 computed yet. */
12540 if (row->height == 0)
12541 {
12542 if (it->max_ascent + it->max_descent == 0)
12543 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
12544 row->ascent = it->max_ascent;
12545 row->height = it->max_ascent + it->max_descent;
12546 row->phys_ascent = it->max_phys_ascent;
12547 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
12548 }
12549
12550 /* Compute the width of this line. */
12551 row->pixel_width = row->x;
12552 for (i = 0; i < row->used[TEXT_AREA]; ++i)
12553 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
12554
12555 xassert (row->pixel_width >= 0);
12556 xassert (row->ascent >= 0 && row->height > 0);
12557
12558 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
12559 || MATRIX_ROW_OVERLAPS_PRED_P (row));
12560
12561 /* If first line's physical ascent is larger than its logical
12562 ascent, use the physical ascent, and make the row taller.
12563 This makes accented characters fully visible. */
12564 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
12565 && row->phys_ascent > row->ascent)
12566 {
12567 row->height += row->phys_ascent - row->ascent;
12568 row->ascent = row->phys_ascent;
12569 }
12570
12571 /* Compute how much of the line is visible. */
12572 row->visible_height = row->height;
12573
12574 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (it->w);
12575 max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
12576
12577 if (row->y < min_y)
12578 row->visible_height -= min_y - row->y;
12579 if (row->y + row->height > max_y)
12580 row->visible_height -= row->y + row->height - max_y;
12581 }
12582 else
12583 {
12584 row->pixel_width = row->used[TEXT_AREA];
12585 if (row->continued_p)
12586 row->pixel_width -= it->continuation_pixel_width;
12587 else if (row->truncated_on_right_p)
12588 row->pixel_width -= it->truncation_pixel_width;
12589 row->ascent = row->phys_ascent = 0;
12590 row->height = row->phys_height = row->visible_height = 1;
12591 }
12592
12593 /* Compute a hash code for this row. */
12594 row->hash = 0;
12595 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12596 for (i = 0; i < row->used[area]; ++i)
12597 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
12598 + row->glyphs[area][i].u.val
12599 + row->glyphs[area][i].face_id
12600 + row->glyphs[area][i].padding_p
12601 + (row->glyphs[area][i].type << 2));
12602
12603 it->max_ascent = it->max_descent = 0;
12604 it->max_phys_ascent = it->max_phys_descent = 0;
12605 }
12606
12607
12608 /* Append one space to the glyph row of iterator IT if doing a
12609 window-based redisplay. DEFAULT_FACE_P non-zero means let the
12610 space have the default face, otherwise let it have the same face as
12611 IT->face_id. Value is non-zero if a space was added.
12612
12613 This function is called to make sure that there is always one glyph
12614 at the end of a glyph row that the cursor can be set on under
12615 window-systems. (If there weren't such a glyph we would not know
12616 how wide and tall a box cursor should be displayed).
12617
12618 At the same time this space let's a nicely handle clearing to the
12619 end of the line if the row ends in italic text. */
12620
12621 static int
12622 append_space (it, default_face_p)
12623 struct it *it;
12624 int default_face_p;
12625 {
12626 if (FRAME_WINDOW_P (it->f))
12627 {
12628 int n = it->glyph_row->used[TEXT_AREA];
12629
12630 if (it->glyph_row->glyphs[TEXT_AREA] + n
12631 < it->glyph_row->glyphs[1 + TEXT_AREA])
12632 {
12633 /* Save some values that must not be changed.
12634 Must save IT->c and IT->len because otherwise
12635 ITERATOR_AT_END_P wouldn't work anymore after
12636 append_space has been called. */
12637 enum display_element_type saved_what = it->what;
12638 int saved_c = it->c, saved_len = it->len;
12639 int saved_x = it->current_x;
12640 int saved_face_id = it->face_id;
12641 struct text_pos saved_pos;
12642 Lisp_Object saved_object;
12643 struct face *face;
12644
12645 saved_object = it->object;
12646 saved_pos = it->position;
12647
12648 it->what = IT_CHARACTER;
12649 bzero (&it->position, sizeof it->position);
12650 it->object = make_number (0);
12651 it->c = ' ';
12652 it->len = 1;
12653
12654 if (default_face_p)
12655 it->face_id = DEFAULT_FACE_ID;
12656 else if (it->face_before_selective_p)
12657 it->face_id = it->saved_face_id;
12658 face = FACE_FROM_ID (it->f, it->face_id);
12659 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
12660
12661 PRODUCE_GLYPHS (it);
12662
12663 it->current_x = saved_x;
12664 it->object = saved_object;
12665 it->position = saved_pos;
12666 it->what = saved_what;
12667 it->face_id = saved_face_id;
12668 it->len = saved_len;
12669 it->c = saved_c;
12670 return 1;
12671 }
12672 }
12673
12674 return 0;
12675 }
12676
12677
12678 /* Extend the face of the last glyph in the text area of IT->glyph_row
12679 to the end of the display line. Called from display_line.
12680 If the glyph row is empty, add a space glyph to it so that we
12681 know the face to draw. Set the glyph row flag fill_line_p. */
12682
12683 static void
12684 extend_face_to_end_of_line (it)
12685 struct it *it;
12686 {
12687 struct face *face;
12688 struct frame *f = it->f;
12689
12690 /* If line is already filled, do nothing. */
12691 if (it->current_x >= it->last_visible_x)
12692 return;
12693
12694 /* Face extension extends the background and box of IT->face_id
12695 to the end of the line. If the background equals the background
12696 of the frame, we don't have to do anything. */
12697 if (it->face_before_selective_p)
12698 face = FACE_FROM_ID (it->f, it->saved_face_id);
12699 else
12700 face = FACE_FROM_ID (f, it->face_id);
12701
12702 if (FRAME_WINDOW_P (f)
12703 && face->box == FACE_NO_BOX
12704 && face->background == FRAME_BACKGROUND_PIXEL (f)
12705 && !face->stipple)
12706 return;
12707
12708 /* Set the glyph row flag indicating that the face of the last glyph
12709 in the text area has to be drawn to the end of the text area. */
12710 it->glyph_row->fill_line_p = 1;
12711
12712 /* If current character of IT is not ASCII, make sure we have the
12713 ASCII face. This will be automatically undone the next time
12714 get_next_display_element returns a multibyte character. Note
12715 that the character will always be single byte in unibyte text. */
12716 if (!SINGLE_BYTE_CHAR_P (it->c))
12717 {
12718 it->face_id = FACE_FOR_CHAR (f, face, 0);
12719 }
12720
12721 if (FRAME_WINDOW_P (f))
12722 {
12723 /* If the row is empty, add a space with the current face of IT,
12724 so that we know which face to draw. */
12725 if (it->glyph_row->used[TEXT_AREA] == 0)
12726 {
12727 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
12728 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
12729 it->glyph_row->used[TEXT_AREA] = 1;
12730 }
12731 }
12732 else
12733 {
12734 /* Save some values that must not be changed. */
12735 int saved_x = it->current_x;
12736 struct text_pos saved_pos;
12737 Lisp_Object saved_object;
12738 enum display_element_type saved_what = it->what;
12739 int saved_face_id = it->face_id;
12740
12741 saved_object = it->object;
12742 saved_pos = it->position;
12743
12744 it->what = IT_CHARACTER;
12745 bzero (&it->position, sizeof it->position);
12746 it->object = make_number (0);
12747 it->c = ' ';
12748 it->len = 1;
12749 it->face_id = face->id;
12750
12751 PRODUCE_GLYPHS (it);
12752
12753 while (it->current_x <= it->last_visible_x)
12754 PRODUCE_GLYPHS (it);
12755
12756 /* Don't count these blanks really. It would let us insert a left
12757 truncation glyph below and make us set the cursor on them, maybe. */
12758 it->current_x = saved_x;
12759 it->object = saved_object;
12760 it->position = saved_pos;
12761 it->what = saved_what;
12762 it->face_id = saved_face_id;
12763 }
12764 }
12765
12766
12767 /* Value is non-zero if text starting at CHARPOS in current_buffer is
12768 trailing whitespace. */
12769
12770 static int
12771 trailing_whitespace_p (charpos)
12772 int charpos;
12773 {
12774 int bytepos = CHAR_TO_BYTE (charpos);
12775 int c = 0;
12776
12777 while (bytepos < ZV_BYTE
12778 && (c = FETCH_CHAR (bytepos),
12779 c == ' ' || c == '\t'))
12780 ++bytepos;
12781
12782 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
12783 {
12784 if (bytepos != PT_BYTE)
12785 return 1;
12786 }
12787 return 0;
12788 }
12789
12790
12791 /* Highlight trailing whitespace, if any, in ROW. */
12792
12793 void
12794 highlight_trailing_whitespace (f, row)
12795 struct frame *f;
12796 struct glyph_row *row;
12797 {
12798 int used = row->used[TEXT_AREA];
12799
12800 if (used)
12801 {
12802 struct glyph *start = row->glyphs[TEXT_AREA];
12803 struct glyph *glyph = start + used - 1;
12804
12805 /* Skip over glyphs inserted to display the cursor at the
12806 end of a line, for extending the face of the last glyph
12807 to the end of the line on terminals, and for truncation
12808 and continuation glyphs. */
12809 while (glyph >= start
12810 && glyph->type == CHAR_GLYPH
12811 && INTEGERP (glyph->object))
12812 --glyph;
12813
12814 /* If last glyph is a space or stretch, and it's trailing
12815 whitespace, set the face of all trailing whitespace glyphs in
12816 IT->glyph_row to `trailing-whitespace'. */
12817 if (glyph >= start
12818 && BUFFERP (glyph->object)
12819 && (glyph->type == STRETCH_GLYPH
12820 || (glyph->type == CHAR_GLYPH
12821 && glyph->u.ch == ' '))
12822 && trailing_whitespace_p (glyph->charpos))
12823 {
12824 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
12825
12826 while (glyph >= start
12827 && BUFFERP (glyph->object)
12828 && (glyph->type == STRETCH_GLYPH
12829 || (glyph->type == CHAR_GLYPH
12830 && glyph->u.ch == ' ')))
12831 (glyph--)->face_id = face_id;
12832 }
12833 }
12834 }
12835
12836
12837 /* Value is non-zero if glyph row ROW in window W should be
12838 used to hold the cursor. */
12839
12840 static int
12841 cursor_row_p (w, row)
12842 struct window *w;
12843 struct glyph_row *row;
12844 {
12845 int cursor_row_p = 1;
12846
12847 if (PT == MATRIX_ROW_END_CHARPOS (row))
12848 {
12849 /* If the row ends with a newline from a string, we don't want
12850 the cursor there (if the row is continued it doesn't end in a
12851 newline). */
12852 if (CHARPOS (row->end.string_pos) >= 0
12853 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12854 cursor_row_p = row->continued_p;
12855
12856 /* If the row ends at ZV, display the cursor at the end of that
12857 row instead of at the start of the row below. */
12858 else if (row->ends_at_zv_p)
12859 cursor_row_p = 1;
12860 else
12861 cursor_row_p = 0;
12862 }
12863
12864 return cursor_row_p;
12865 }
12866
12867
12868 /* Construct the glyph row IT->glyph_row in the desired matrix of
12869 IT->w from text at the current position of IT. See dispextern.h
12870 for an overview of struct it. Value is non-zero if
12871 IT->glyph_row displays text, as opposed to a line displaying ZV
12872 only. */
12873
12874 static int
12875 display_line (it)
12876 struct it *it;
12877 {
12878 struct glyph_row *row = it->glyph_row;
12879
12880 /* We always start displaying at hpos zero even if hscrolled. */
12881 xassert (it->hpos == 0 && it->current_x == 0);
12882
12883 /* We must not display in a row that's not a text row. */
12884 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
12885 < it->w->desired_matrix->nrows);
12886
12887 /* Is IT->w showing the region? */
12888 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
12889
12890 /* Clear the result glyph row and enable it. */
12891 prepare_desired_row (row);
12892
12893 row->y = it->current_y;
12894 row->start = it->current;
12895 row->continuation_lines_width = it->continuation_lines_width;
12896 row->displays_text_p = 1;
12897 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
12898 it->starts_in_middle_of_char_p = 0;
12899
12900 /* Arrange the overlays nicely for our purposes. Usually, we call
12901 display_line on only one line at a time, in which case this
12902 can't really hurt too much, or we call it on lines which appear
12903 one after another in the buffer, in which case all calls to
12904 recenter_overlay_lists but the first will be pretty cheap. */
12905 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
12906
12907 /* Move over display elements that are not visible because we are
12908 hscrolled. This may stop at an x-position < IT->first_visible_x
12909 if the first glyph is partially visible or if we hit a line end. */
12910 if (it->current_x < it->first_visible_x)
12911 move_it_in_display_line_to (it, ZV, it->first_visible_x,
12912 MOVE_TO_POS | MOVE_TO_X);
12913
12914 /* Get the initial row height. This is either the height of the
12915 text hscrolled, if there is any, or zero. */
12916 row->ascent = it->max_ascent;
12917 row->height = it->max_ascent + it->max_descent;
12918 row->phys_ascent = it->max_phys_ascent;
12919 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
12920
12921 /* Loop generating characters. The loop is left with IT on the next
12922 character to display. */
12923 while (1)
12924 {
12925 int n_glyphs_before, hpos_before, x_before;
12926 int x, i, nglyphs;
12927 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
12928
12929 /* Retrieve the next thing to display. Value is zero if end of
12930 buffer reached. */
12931 if (!get_next_display_element (it))
12932 {
12933 /* Maybe add a space at the end of this line that is used to
12934 display the cursor there under X. Set the charpos of the
12935 first glyph of blank lines not corresponding to any text
12936 to -1. */
12937 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
12938 || row->used[TEXT_AREA] == 0)
12939 {
12940 row->glyphs[TEXT_AREA]->charpos = -1;
12941 row->displays_text_p = 0;
12942
12943 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
12944 && (!MINI_WINDOW_P (it->w)
12945 || (minibuf_level && EQ (it->window, minibuf_window))))
12946 row->indicate_empty_line_p = 1;
12947 }
12948
12949 it->continuation_lines_width = 0;
12950 row->ends_at_zv_p = 1;
12951 break;
12952 }
12953
12954 /* Now, get the metrics of what we want to display. This also
12955 generates glyphs in `row' (which is IT->glyph_row). */
12956 n_glyphs_before = row->used[TEXT_AREA];
12957 x = it->current_x;
12958
12959 /* Remember the line height so far in case the next element doesn't
12960 fit on the line. */
12961 if (!it->truncate_lines_p)
12962 {
12963 ascent = it->max_ascent;
12964 descent = it->max_descent;
12965 phys_ascent = it->max_phys_ascent;
12966 phys_descent = it->max_phys_descent;
12967 }
12968
12969 PRODUCE_GLYPHS (it);
12970
12971 /* If this display element was in marginal areas, continue with
12972 the next one. */
12973 if (it->area != TEXT_AREA)
12974 {
12975 row->ascent = max (row->ascent, it->max_ascent);
12976 row->height = max (row->height, it->max_ascent + it->max_descent);
12977 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12978 row->phys_height = max (row->phys_height,
12979 it->max_phys_ascent + it->max_phys_descent);
12980 set_iterator_to_next (it, 1);
12981 continue;
12982 }
12983
12984 /* Does the display element fit on the line? If we truncate
12985 lines, we should draw past the right edge of the window. If
12986 we don't truncate, we want to stop so that we can display the
12987 continuation glyph before the right margin. If lines are
12988 continued, there are two possible strategies for characters
12989 resulting in more than 1 glyph (e.g. tabs): Display as many
12990 glyphs as possible in this line and leave the rest for the
12991 continuation line, or display the whole element in the next
12992 line. Original redisplay did the former, so we do it also. */
12993 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
12994 hpos_before = it->hpos;
12995 x_before = x;
12996
12997 if (/* Not a newline. */
12998 nglyphs > 0
12999 /* Glyphs produced fit entirely in the line. */
13000 && it->current_x < it->last_visible_x)
13001 {
13002 it->hpos += nglyphs;
13003 row->ascent = max (row->ascent, it->max_ascent);
13004 row->height = max (row->height, it->max_ascent + it->max_descent);
13005 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13006 row->phys_height = max (row->phys_height,
13007 it->max_phys_ascent + it->max_phys_descent);
13008 if (it->current_x - it->pixel_width < it->first_visible_x)
13009 row->x = x - it->first_visible_x;
13010 }
13011 else
13012 {
13013 int new_x;
13014 struct glyph *glyph;
13015
13016 for (i = 0; i < nglyphs; ++i, x = new_x)
13017 {
13018 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
13019 new_x = x + glyph->pixel_width;
13020
13021 if (/* Lines are continued. */
13022 !it->truncate_lines_p
13023 && (/* Glyph doesn't fit on the line. */
13024 new_x > it->last_visible_x
13025 /* Or it fits exactly on a window system frame. */
13026 || (new_x == it->last_visible_x
13027 && FRAME_WINDOW_P (it->f))))
13028 {
13029 /* End of a continued line. */
13030
13031 if (it->hpos == 0
13032 || (new_x == it->last_visible_x
13033 && FRAME_WINDOW_P (it->f)))
13034 {
13035 /* Current glyph is the only one on the line or
13036 fits exactly on the line. We must continue
13037 the line because we can't draw the cursor
13038 after the glyph. */
13039 row->continued_p = 1;
13040 it->current_x = new_x;
13041 it->continuation_lines_width += new_x;
13042 ++it->hpos;
13043 if (i == nglyphs - 1)
13044 set_iterator_to_next (it, 1);
13045 }
13046 else if (CHAR_GLYPH_PADDING_P (*glyph)
13047 && !FRAME_WINDOW_P (it->f))
13048 {
13049 /* A padding glyph that doesn't fit on this line.
13050 This means the whole character doesn't fit
13051 on the line. */
13052 row->used[TEXT_AREA] = n_glyphs_before;
13053
13054 /* Fill the rest of the row with continuation
13055 glyphs like in 20.x. */
13056 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
13057 < row->glyphs[1 + TEXT_AREA])
13058 produce_special_glyphs (it, IT_CONTINUATION);
13059
13060 row->continued_p = 1;
13061 it->current_x = x_before;
13062 it->continuation_lines_width += x_before;
13063
13064 /* Restore the height to what it was before the
13065 element not fitting on the line. */
13066 it->max_ascent = ascent;
13067 it->max_descent = descent;
13068 it->max_phys_ascent = phys_ascent;
13069 it->max_phys_descent = phys_descent;
13070 }
13071 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
13072 {
13073 /* A TAB that extends past the right edge of the
13074 window. This produces a single glyph on
13075 window system frames. We leave the glyph in
13076 this row and let it fill the row, but don't
13077 consume the TAB. */
13078 it->continuation_lines_width += it->last_visible_x;
13079 row->ends_in_middle_of_char_p = 1;
13080 row->continued_p = 1;
13081 glyph->pixel_width = it->last_visible_x - x;
13082 it->starts_in_middle_of_char_p = 1;
13083 }
13084 else
13085 {
13086 /* Something other than a TAB that draws past
13087 the right edge of the window. Restore
13088 positions to values before the element. */
13089 row->used[TEXT_AREA] = n_glyphs_before + i;
13090
13091 /* Display continuation glyphs. */
13092 if (!FRAME_WINDOW_P (it->f))
13093 produce_special_glyphs (it, IT_CONTINUATION);
13094 row->continued_p = 1;
13095
13096 it->continuation_lines_width += x;
13097
13098 if (nglyphs > 1 && i > 0)
13099 {
13100 row->ends_in_middle_of_char_p = 1;
13101 it->starts_in_middle_of_char_p = 1;
13102 }
13103
13104 /* Restore the height to what it was before the
13105 element not fitting on the line. */
13106 it->max_ascent = ascent;
13107 it->max_descent = descent;
13108 it->max_phys_ascent = phys_ascent;
13109 it->max_phys_descent = phys_descent;
13110 }
13111
13112 break;
13113 }
13114 else if (new_x > it->first_visible_x)
13115 {
13116 /* Increment number of glyphs actually displayed. */
13117 ++it->hpos;
13118
13119 if (x < it->first_visible_x)
13120 /* Glyph is partially visible, i.e. row starts at
13121 negative X position. */
13122 row->x = x - it->first_visible_x;
13123 }
13124 else
13125 {
13126 /* Glyph is completely off the left margin of the
13127 window. This should not happen because of the
13128 move_it_in_display_line at the start of
13129 this function. */
13130 abort ();
13131 }
13132 }
13133
13134 row->ascent = max (row->ascent, it->max_ascent);
13135 row->height = max (row->height, it->max_ascent + it->max_descent);
13136 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13137 row->phys_height = max (row->phys_height,
13138 it->max_phys_ascent + it->max_phys_descent);
13139
13140 /* End of this display line if row is continued. */
13141 if (row->continued_p)
13142 break;
13143 }
13144
13145 /* Is this a line end? If yes, we're also done, after making
13146 sure that a non-default face is extended up to the right
13147 margin of the window. */
13148 if (ITERATOR_AT_END_OF_LINE_P (it))
13149 {
13150 int used_before = row->used[TEXT_AREA];
13151
13152 row->ends_in_newline_from_string_p = STRINGP (it->object);
13153
13154 /* Add a space at the end of the line that is used to
13155 display the cursor there. */
13156 append_space (it, 0);
13157
13158 /* Extend the face to the end of the line. */
13159 extend_face_to_end_of_line (it);
13160
13161 /* Make sure we have the position. */
13162 if (used_before == 0)
13163 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
13164
13165 /* Consume the line end. This skips over invisible lines. */
13166 set_iterator_to_next (it, 1);
13167 it->continuation_lines_width = 0;
13168 break;
13169 }
13170
13171 /* Proceed with next display element. Note that this skips
13172 over lines invisible because of selective display. */
13173 set_iterator_to_next (it, 1);
13174
13175 /* If we truncate lines, we are done when the last displayed
13176 glyphs reach past the right margin of the window. */
13177 if (it->truncate_lines_p
13178 && (FRAME_WINDOW_P (it->f)
13179 ? (it->current_x >= it->last_visible_x)
13180 : (it->current_x > it->last_visible_x)))
13181 {
13182 /* Maybe add truncation glyphs. */
13183 if (!FRAME_WINDOW_P (it->f))
13184 {
13185 int i, n;
13186
13187 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
13188 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
13189 break;
13190
13191 for (n = row->used[TEXT_AREA]; i < n; ++i)
13192 {
13193 row->used[TEXT_AREA] = i;
13194 produce_special_glyphs (it, IT_TRUNCATION);
13195 }
13196 }
13197
13198 row->truncated_on_right_p = 1;
13199 it->continuation_lines_width = 0;
13200 reseat_at_next_visible_line_start (it, 0);
13201 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
13202 it->hpos = hpos_before;
13203 it->current_x = x_before;
13204 break;
13205 }
13206 }
13207
13208 /* If line is not empty and hscrolled, maybe insert truncation glyphs
13209 at the left window margin. */
13210 if (it->first_visible_x
13211 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
13212 {
13213 if (!FRAME_WINDOW_P (it->f))
13214 insert_left_trunc_glyphs (it);
13215 row->truncated_on_left_p = 1;
13216 }
13217
13218 /* If the start of this line is the overlay arrow-position, then
13219 mark this glyph row as the one containing the overlay arrow.
13220 This is clearly a mess with variable size fonts. It would be
13221 better to let it be displayed like cursors under X. */
13222 if (MARKERP (Voverlay_arrow_position)
13223 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
13224 && (MATRIX_ROW_START_CHARPOS (row)
13225 == marker_position (Voverlay_arrow_position))
13226 && STRINGP (Voverlay_arrow_string)
13227 && ! overlay_arrow_seen)
13228 {
13229 /* Overlay arrow in window redisplay is a fringe bitmap. */
13230 if (!FRAME_WINDOW_P (it->f))
13231 {
13232 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
13233 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
13234 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
13235 struct glyph *p = row->glyphs[TEXT_AREA];
13236 struct glyph *p2, *end;
13237
13238 /* Copy the arrow glyphs. */
13239 while (glyph < arrow_end)
13240 *p++ = *glyph++;
13241
13242 /* Throw away padding glyphs. */
13243 p2 = p;
13244 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
13245 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
13246 ++p2;
13247 if (p2 > p)
13248 {
13249 while (p2 < end)
13250 *p++ = *p2++;
13251 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
13252 }
13253 }
13254
13255 overlay_arrow_seen = 1;
13256 row->overlay_arrow_p = 1;
13257 }
13258
13259 /* Compute pixel dimensions of this line. */
13260 compute_line_metrics (it);
13261
13262 /* Remember the position at which this line ends. */
13263 row->end = it->current;
13264
13265 /* Maybe set the cursor. */
13266 if (it->w->cursor.vpos < 0
13267 && PT >= MATRIX_ROW_START_CHARPOS (row)
13268 && PT <= MATRIX_ROW_END_CHARPOS (row)
13269 && cursor_row_p (it->w, row))
13270 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
13271
13272 /* Highlight trailing whitespace. */
13273 if (!NILP (Vshow_trailing_whitespace))
13274 highlight_trailing_whitespace (it->f, it->glyph_row);
13275
13276 /* Prepare for the next line. This line starts horizontally at (X
13277 HPOS) = (0 0). Vertical positions are incremented. As a
13278 convenience for the caller, IT->glyph_row is set to the next
13279 row to be used. */
13280 it->current_x = it->hpos = 0;
13281 it->current_y += row->height;
13282 ++it->vpos;
13283 ++it->glyph_row;
13284 return row->displays_text_p;
13285 }
13286
13287
13288 \f
13289 /***********************************************************************
13290 Menu Bar
13291 ***********************************************************************/
13292
13293 /* Redisplay the menu bar in the frame for window W.
13294
13295 The menu bar of X frames that don't have X toolkit support is
13296 displayed in a special window W->frame->menu_bar_window.
13297
13298 The menu bar of terminal frames is treated specially as far as
13299 glyph matrices are concerned. Menu bar lines are not part of
13300 windows, so the update is done directly on the frame matrix rows
13301 for the menu bar. */
13302
13303 static void
13304 display_menu_bar (w)
13305 struct window *w;
13306 {
13307 struct frame *f = XFRAME (WINDOW_FRAME (w));
13308 struct it it;
13309 Lisp_Object items;
13310 int i;
13311
13312 /* Don't do all this for graphical frames. */
13313 #ifdef HAVE_NTGUI
13314 if (!NILP (Vwindow_system))
13315 return;
13316 #endif
13317 #ifdef USE_X_TOOLKIT
13318 if (FRAME_X_P (f))
13319 return;
13320 #endif
13321 #ifdef macintosh
13322 if (FRAME_MAC_P (f))
13323 return;
13324 #endif
13325
13326 #ifdef USE_X_TOOLKIT
13327 xassert (!FRAME_WINDOW_P (f));
13328 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
13329 it.first_visible_x = 0;
13330 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
13331 #else /* not USE_X_TOOLKIT */
13332 if (FRAME_WINDOW_P (f))
13333 {
13334 /* Menu bar lines are displayed in the desired matrix of the
13335 dummy window menu_bar_window. */
13336 struct window *menu_w;
13337 xassert (WINDOWP (f->menu_bar_window));
13338 menu_w = XWINDOW (f->menu_bar_window);
13339 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
13340 MENU_FACE_ID);
13341 it.first_visible_x = 0;
13342 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
13343 }
13344 else
13345 {
13346 /* This is a TTY frame, i.e. character hpos/vpos are used as
13347 pixel x/y. */
13348 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
13349 MENU_FACE_ID);
13350 it.first_visible_x = 0;
13351 it.last_visible_x = FRAME_WIDTH (f);
13352 }
13353 #endif /* not USE_X_TOOLKIT */
13354
13355 if (! mode_line_inverse_video)
13356 /* Force the menu-bar to be displayed in the default face. */
13357 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
13358
13359 /* Clear all rows of the menu bar. */
13360 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
13361 {
13362 struct glyph_row *row = it.glyph_row + i;
13363 clear_glyph_row (row);
13364 row->enabled_p = 1;
13365 row->full_width_p = 1;
13366 }
13367
13368 /* Display all items of the menu bar. */
13369 items = FRAME_MENU_BAR_ITEMS (it.f);
13370 for (i = 0; i < XVECTOR (items)->size; i += 4)
13371 {
13372 Lisp_Object string;
13373
13374 /* Stop at nil string. */
13375 string = AREF (items, i + 1);
13376 if (NILP (string))
13377 break;
13378
13379 /* Remember where item was displayed. */
13380 AREF (items, i + 3) = make_number (it.hpos);
13381
13382 /* Display the item, pad with one space. */
13383 if (it.current_x < it.last_visible_x)
13384 display_string (NULL, string, Qnil, 0, 0, &it,
13385 XSTRING (string)->size + 1, 0, 0, -1);
13386 }
13387
13388 /* Fill out the line with spaces. */
13389 if (it.current_x < it.last_visible_x)
13390 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
13391
13392 /* Compute the total height of the lines. */
13393 compute_line_metrics (&it);
13394 }
13395
13396
13397 \f
13398 /***********************************************************************
13399 Mode Line
13400 ***********************************************************************/
13401
13402 /* Redisplay mode lines in the window tree whose root is WINDOW. If
13403 FORCE is non-zero, redisplay mode lines unconditionally.
13404 Otherwise, redisplay only mode lines that are garbaged. Value is
13405 the number of windows whose mode lines were redisplayed. */
13406
13407 static int
13408 redisplay_mode_lines (window, force)
13409 Lisp_Object window;
13410 int force;
13411 {
13412 int nwindows = 0;
13413
13414 while (!NILP (window))
13415 {
13416 struct window *w = XWINDOW (window);
13417
13418 if (WINDOWP (w->hchild))
13419 nwindows += redisplay_mode_lines (w->hchild, force);
13420 else if (WINDOWP (w->vchild))
13421 nwindows += redisplay_mode_lines (w->vchild, force);
13422 else if (force
13423 || FRAME_GARBAGED_P (XFRAME (w->frame))
13424 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
13425 {
13426 struct text_pos lpoint;
13427 struct buffer *old = current_buffer;
13428
13429 /* Set the window's buffer for the mode line display. */
13430 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13431 set_buffer_internal_1 (XBUFFER (w->buffer));
13432
13433 /* Point refers normally to the selected window. For any
13434 other window, set up appropriate value. */
13435 if (!EQ (window, selected_window))
13436 {
13437 struct text_pos pt;
13438
13439 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
13440 if (CHARPOS (pt) < BEGV)
13441 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
13442 else if (CHARPOS (pt) > (ZV - 1))
13443 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
13444 else
13445 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
13446 }
13447
13448 /* Display mode lines. */
13449 clear_glyph_matrix (w->desired_matrix);
13450 if (display_mode_lines (w))
13451 {
13452 ++nwindows;
13453 w->must_be_updated_p = 1;
13454 }
13455
13456 /* Restore old settings. */
13457 set_buffer_internal_1 (old);
13458 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13459 }
13460
13461 window = w->next;
13462 }
13463
13464 return nwindows;
13465 }
13466
13467
13468 /* Display the mode and/or top line of window W. Value is the number
13469 of mode lines displayed. */
13470
13471 static int
13472 display_mode_lines (w)
13473 struct window *w;
13474 {
13475 Lisp_Object old_selected_window, old_selected_frame;
13476 int n = 0;
13477
13478 old_selected_frame = selected_frame;
13479 selected_frame = w->frame;
13480 old_selected_window = selected_window;
13481 XSETWINDOW (selected_window, w);
13482
13483 /* These will be set while the mode line specs are processed. */
13484 line_number_displayed = 0;
13485 w->column_number_displayed = Qnil;
13486
13487 if (WINDOW_WANTS_MODELINE_P (w))
13488 {
13489 struct window *sel_w = XWINDOW (old_selected_window);
13490
13491 /* Select mode line face based on the real selected window. */
13492 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
13493 current_buffer->mode_line_format);
13494 ++n;
13495 }
13496
13497 if (WINDOW_WANTS_HEADER_LINE_P (w))
13498 {
13499 display_mode_line (w, HEADER_LINE_FACE_ID,
13500 current_buffer->header_line_format);
13501 ++n;
13502 }
13503
13504 selected_frame = old_selected_frame;
13505 selected_window = old_selected_window;
13506 return n;
13507 }
13508
13509
13510 /* Display mode or top line of window W. FACE_ID specifies which line
13511 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
13512 FORMAT is the mode line format to display. Value is the pixel
13513 height of the mode line displayed. */
13514
13515 static int
13516 display_mode_line (w, face_id, format)
13517 struct window *w;
13518 enum face_id face_id;
13519 Lisp_Object format;
13520 {
13521 struct it it;
13522 struct face *face;
13523
13524 init_iterator (&it, w, -1, -1, NULL, face_id);
13525 prepare_desired_row (it.glyph_row);
13526
13527 if (! mode_line_inverse_video)
13528 /* Force the mode-line to be displayed in the default face. */
13529 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
13530
13531 /* Temporarily make frame's keyboard the current kboard so that
13532 kboard-local variables in the mode_line_format will get the right
13533 values. */
13534 push_frame_kboard (it.f);
13535 display_mode_element (&it, 0, 0, 0, format, Qnil);
13536 pop_frame_kboard ();
13537
13538 /* Fill up with spaces. */
13539 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
13540
13541 compute_line_metrics (&it);
13542 it.glyph_row->full_width_p = 1;
13543 it.glyph_row->mode_line_p = 1;
13544 it.glyph_row->continued_p = 0;
13545 it.glyph_row->truncated_on_left_p = 0;
13546 it.glyph_row->truncated_on_right_p = 0;
13547
13548 /* Make a 3D mode-line have a shadow at its right end. */
13549 face = FACE_FROM_ID (it.f, face_id);
13550 extend_face_to_end_of_line (&it);
13551 if (face->box != FACE_NO_BOX)
13552 {
13553 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
13554 + it.glyph_row->used[TEXT_AREA] - 1);
13555 last->right_box_line_p = 1;
13556 }
13557
13558 return it.glyph_row->height;
13559 }
13560
13561 /* Alist that caches the results of :propertize.
13562 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
13563 Lisp_Object mode_line_proptrans_alist;
13564
13565 /* Contribute ELT to the mode line for window IT->w. How it
13566 translates into text depends on its data type.
13567
13568 IT describes the display environment in which we display, as usual.
13569
13570 DEPTH is the depth in recursion. It is used to prevent
13571 infinite recursion here.
13572
13573 FIELD_WIDTH is the number of characters the display of ELT should
13574 occupy in the mode line, and PRECISION is the maximum number of
13575 characters to display from ELT's representation. See
13576 display_string for details.
13577
13578 Returns the hpos of the end of the text generated by ELT. */
13579
13580 static int
13581 display_mode_element (it, depth, field_width, precision, elt, props)
13582 struct it *it;
13583 int depth;
13584 int field_width, precision;
13585 Lisp_Object elt, props;
13586 {
13587 int n = 0, field, prec;
13588 int literal = 0;
13589
13590 tail_recurse:
13591 if (depth > 10)
13592 goto invalid;
13593
13594 depth++;
13595
13596 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
13597 {
13598 case Lisp_String:
13599 {
13600 /* A string: output it and check for %-constructs within it. */
13601 unsigned char c;
13602 unsigned char *this = XSTRING (elt)->data;
13603 unsigned char *lisp_string = this;
13604
13605 if (!NILP (props))
13606 {
13607 Lisp_Object oprops, aelt;
13608 oprops = Ftext_properties_at (make_number (0), elt);
13609 if (NILP (Fequal (props, oprops)))
13610 {
13611 aelt = Fassoc (elt, mode_line_proptrans_alist);
13612 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
13613 elt = XCAR (aelt);
13614 else
13615 {
13616 elt = Fcopy_sequence (elt);
13617 Fset_text_properties (0, Flength (elt), props, elt);
13618 mode_line_proptrans_alist
13619 = Fcons (Fcons (elt, props),
13620 mode_line_proptrans_alist);
13621 }
13622 }
13623 }
13624
13625 if (literal)
13626 {
13627 prec = precision - n;
13628 if (frame_title_ptr)
13629 n += store_frame_title (XSTRING (elt)->data, -1, prec);
13630 else
13631 n += display_string (NULL, elt, Qnil, 0, 0, it,
13632 0, prec, 0, STRING_MULTIBYTE (elt));
13633
13634 break;
13635 }
13636
13637 while ((precision <= 0 || n < precision)
13638 && *this
13639 && (frame_title_ptr
13640 || it->current_x < it->last_visible_x))
13641 {
13642 unsigned char *last = this;
13643
13644 /* Advance to end of string or next format specifier. */
13645 while ((c = *this++) != '\0' && c != '%')
13646 ;
13647
13648 if (this - 1 != last)
13649 {
13650 /* Output to end of string or up to '%'. Field width
13651 is length of string. Don't output more than
13652 PRECISION allows us. */
13653 --this;
13654
13655 prec = chars_in_text (last, this - last);
13656 if (precision > 0 && prec > precision - n)
13657 prec = precision - n;
13658
13659 if (frame_title_ptr)
13660 n += store_frame_title (last, 0, prec);
13661 else
13662 {
13663 int bytepos = last - lisp_string;
13664 int charpos = string_byte_to_char (elt, bytepos);
13665 n += display_string (NULL, elt, Qnil, 0, charpos,
13666 it, 0, prec, 0,
13667 STRING_MULTIBYTE (elt));
13668 }
13669 }
13670 else /* c == '%' */
13671 {
13672 unsigned char *percent_position = this;
13673
13674 /* Get the specified minimum width. Zero means
13675 don't pad. */
13676 field = 0;
13677 while ((c = *this++) >= '0' && c <= '9')
13678 field = field * 10 + c - '0';
13679
13680 /* Don't pad beyond the total padding allowed. */
13681 if (field_width - n > 0 && field > field_width - n)
13682 field = field_width - n;
13683
13684 /* Note that either PRECISION <= 0 or N < PRECISION. */
13685 prec = precision - n;
13686
13687 if (c == 'M')
13688 n += display_mode_element (it, depth, field, prec,
13689 Vglobal_mode_string, props);
13690 else if (c != 0)
13691 {
13692 int multibyte;
13693 unsigned char *spec
13694 = decode_mode_spec (it->w, c, field, prec, &multibyte);
13695
13696 if (frame_title_ptr)
13697 n += store_frame_title (spec, field, prec);
13698 else
13699 {
13700 int nglyphs_before, bytepos, charpos, nwritten;
13701
13702 nglyphs_before = it->glyph_row->used[TEXT_AREA];
13703 bytepos = percent_position - XSTRING (elt)->data;
13704 charpos = (STRING_MULTIBYTE (elt)
13705 ? string_byte_to_char (elt, bytepos)
13706 : bytepos);
13707 nwritten = display_string (spec, Qnil, elt,
13708 charpos, 0, it,
13709 field, prec, 0,
13710 multibyte);
13711
13712 /* Assign to the glyphs written above the
13713 string where the `%x' came from, position
13714 of the `%'. */
13715 if (nwritten > 0)
13716 {
13717 struct glyph *glyph
13718 = (it->glyph_row->glyphs[TEXT_AREA]
13719 + nglyphs_before);
13720 int i;
13721
13722 for (i = 0; i < nwritten; ++i)
13723 {
13724 glyph[i].object = elt;
13725 glyph[i].charpos = charpos;
13726 }
13727
13728 n += nwritten;
13729 }
13730 }
13731 }
13732 else /* c == 0 */
13733 break;
13734 }
13735 }
13736 }
13737 break;
13738
13739 case Lisp_Symbol:
13740 /* A symbol: process the value of the symbol recursively
13741 as if it appeared here directly. Avoid error if symbol void.
13742 Special case: if value of symbol is a string, output the string
13743 literally. */
13744 {
13745 register Lisp_Object tem;
13746 tem = Fboundp (elt);
13747 if (!NILP (tem))
13748 {
13749 tem = Fsymbol_value (elt);
13750 /* If value is a string, output that string literally:
13751 don't check for % within it. */
13752 if (STRINGP (tem))
13753 literal = 1;
13754
13755 if (!EQ (tem, elt))
13756 {
13757 /* Give up right away for nil or t. */
13758 elt = tem;
13759 goto tail_recurse;
13760 }
13761 }
13762 }
13763 break;
13764
13765 case Lisp_Cons:
13766 {
13767 register Lisp_Object car, tem;
13768
13769 /* A cons cell: five distinct cases.
13770 If first element is :eval or :propertize, do something special.
13771 If first element is a string or a cons, process all the elements
13772 and effectively concatenate them.
13773 If first element is a negative number, truncate displaying cdr to
13774 at most that many characters. If positive, pad (with spaces)
13775 to at least that many characters.
13776 If first element is a symbol, process the cadr or caddr recursively
13777 according to whether the symbol's value is non-nil or nil. */
13778 car = XCAR (elt);
13779 if (EQ (car, QCeval))
13780 {
13781 /* An element of the form (:eval FORM) means evaluate FORM
13782 and use the result as mode line elements. */
13783
13784 if (CONSP (XCDR (elt)))
13785 {
13786 Lisp_Object spec;
13787 spec = safe_eval (XCAR (XCDR (elt)));
13788 n += display_mode_element (it, depth, field_width - n,
13789 precision - n, spec, props);
13790 }
13791 }
13792 else if (EQ (car, QCpropertize))
13793 {
13794 if (CONSP (XCDR (elt)))
13795 {
13796 /* An element of the form (:propertize ELT PROPS...)
13797 means display ELT but applying properties PROPS. */
13798 n += display_mode_element (it, depth, field_width - n,
13799 precision - n, XCAR (XCDR (elt)),
13800 XCDR (XCDR (elt)));
13801 }
13802 }
13803 else if (SYMBOLP (car))
13804 {
13805 tem = Fboundp (car);
13806 elt = XCDR (elt);
13807 if (!CONSP (elt))
13808 goto invalid;
13809 /* elt is now the cdr, and we know it is a cons cell.
13810 Use its car if CAR has a non-nil value. */
13811 if (!NILP (tem))
13812 {
13813 tem = Fsymbol_value (car);
13814 if (!NILP (tem))
13815 {
13816 elt = XCAR (elt);
13817 goto tail_recurse;
13818 }
13819 }
13820 /* Symbol's value is nil (or symbol is unbound)
13821 Get the cddr of the original list
13822 and if possible find the caddr and use that. */
13823 elt = XCDR (elt);
13824 if (NILP (elt))
13825 break;
13826 else if (!CONSP (elt))
13827 goto invalid;
13828 elt = XCAR (elt);
13829 goto tail_recurse;
13830 }
13831 else if (INTEGERP (car))
13832 {
13833 register int lim = XINT (car);
13834 elt = XCDR (elt);
13835 if (lim < 0)
13836 {
13837 /* Negative int means reduce maximum width. */
13838 if (precision <= 0)
13839 precision = -lim;
13840 else
13841 precision = min (precision, -lim);
13842 }
13843 else if (lim > 0)
13844 {
13845 /* Padding specified. Don't let it be more than
13846 current maximum. */
13847 if (precision > 0)
13848 lim = min (precision, lim);
13849
13850 /* If that's more padding than already wanted, queue it.
13851 But don't reduce padding already specified even if
13852 that is beyond the current truncation point. */
13853 field_width = max (lim, field_width);
13854 }
13855 goto tail_recurse;
13856 }
13857 else if (STRINGP (car) || CONSP (car))
13858 {
13859 register int limit = 50;
13860 /* Limit is to protect against circular lists. */
13861 while (CONSP (elt)
13862 && --limit > 0
13863 && (precision <= 0 || n < precision))
13864 {
13865 n += display_mode_element (it, depth, field_width - n,
13866 precision - n, XCAR (elt), props);
13867 elt = XCDR (elt);
13868 }
13869 }
13870 }
13871 break;
13872
13873 default:
13874 invalid:
13875 if (frame_title_ptr)
13876 n += store_frame_title ("*invalid*", 0, precision - n);
13877 else
13878 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
13879 precision - n, 0, 0);
13880 return n;
13881 }
13882
13883 /* Pad to FIELD_WIDTH. */
13884 if (field_width > 0 && n < field_width)
13885 {
13886 if (frame_title_ptr)
13887 n += store_frame_title ("", field_width - n, 0);
13888 else
13889 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
13890 0, 0, 0);
13891 }
13892
13893 return n;
13894 }
13895
13896
13897 /* Write a null-terminated, right justified decimal representation of
13898 the positive integer D to BUF using a minimal field width WIDTH. */
13899
13900 static void
13901 pint2str (buf, width, d)
13902 register char *buf;
13903 register int width;
13904 register int d;
13905 {
13906 register char *p = buf;
13907
13908 if (d <= 0)
13909 *p++ = '0';
13910 else
13911 {
13912 while (d > 0)
13913 {
13914 *p++ = d % 10 + '0';
13915 d /= 10;
13916 }
13917 }
13918
13919 for (width -= (int) (p - buf); width > 0; --width)
13920 *p++ = ' ';
13921 *p-- = '\0';
13922 while (p > buf)
13923 {
13924 d = *buf;
13925 *buf++ = *p;
13926 *p-- = d;
13927 }
13928 }
13929
13930 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
13931 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
13932 type of CODING_SYSTEM. Return updated pointer into BUF. */
13933
13934 static unsigned char invalid_eol_type[] = "(*invalid*)";
13935
13936 static char *
13937 decode_mode_spec_coding (coding_system, buf, eol_flag)
13938 Lisp_Object coding_system;
13939 register char *buf;
13940 int eol_flag;
13941 {
13942 Lisp_Object val;
13943 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
13944 unsigned char *eol_str;
13945 int eol_str_len;
13946 /* The EOL conversion we are using. */
13947 Lisp_Object eoltype;
13948
13949 val = Fget (coding_system, Qcoding_system);
13950 eoltype = Qnil;
13951
13952 if (!VECTORP (val)) /* Not yet decided. */
13953 {
13954 if (multibyte)
13955 *buf++ = '-';
13956 if (eol_flag)
13957 eoltype = eol_mnemonic_undecided;
13958 /* Don't mention EOL conversion if it isn't decided. */
13959 }
13960 else
13961 {
13962 Lisp_Object eolvalue;
13963
13964 eolvalue = Fget (coding_system, Qeol_type);
13965
13966 if (multibyte)
13967 *buf++ = XFASTINT (AREF (val, 1));
13968
13969 if (eol_flag)
13970 {
13971 /* The EOL conversion that is normal on this system. */
13972
13973 if (NILP (eolvalue)) /* Not yet decided. */
13974 eoltype = eol_mnemonic_undecided;
13975 else if (VECTORP (eolvalue)) /* Not yet decided. */
13976 eoltype = eol_mnemonic_undecided;
13977 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
13978 eoltype = (XFASTINT (eolvalue) == 0
13979 ? eol_mnemonic_unix
13980 : (XFASTINT (eolvalue) == 1
13981 ? eol_mnemonic_dos : eol_mnemonic_mac));
13982 }
13983 }
13984
13985 if (eol_flag)
13986 {
13987 /* Mention the EOL conversion if it is not the usual one. */
13988 if (STRINGP (eoltype))
13989 {
13990 eol_str = XSTRING (eoltype)->data;
13991 eol_str_len = XSTRING (eoltype)->size;
13992 }
13993 else if (INTEGERP (eoltype)
13994 && CHAR_VALID_P (XINT (eoltype), 0))
13995 {
13996 eol_str = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
13997 eol_str_len = CHAR_STRING (XINT (eoltype), eol_str);
13998 }
13999 else
14000 {
14001 eol_str = invalid_eol_type;
14002 eol_str_len = sizeof (invalid_eol_type) - 1;
14003 }
14004 bcopy (eol_str, buf, eol_str_len);
14005 buf += eol_str_len;
14006 }
14007
14008 return buf;
14009 }
14010
14011 /* Return a string for the output of a mode line %-spec for window W,
14012 generated by character C. PRECISION >= 0 means don't return a
14013 string longer than that value. FIELD_WIDTH > 0 means pad the
14014 string returned with spaces to that value. Return 1 in *MULTIBYTE
14015 if the result is multibyte text. */
14016
14017 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
14018
14019 static char *
14020 decode_mode_spec (w, c, field_width, precision, multibyte)
14021 struct window *w;
14022 register int c;
14023 int field_width, precision;
14024 int *multibyte;
14025 {
14026 Lisp_Object obj;
14027 struct frame *f = XFRAME (WINDOW_FRAME (w));
14028 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
14029 struct buffer *b = XBUFFER (w->buffer);
14030
14031 obj = Qnil;
14032 *multibyte = 0;
14033
14034 switch (c)
14035 {
14036 case '*':
14037 if (!NILP (b->read_only))
14038 return "%";
14039 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
14040 return "*";
14041 return "-";
14042
14043 case '+':
14044 /* This differs from %* only for a modified read-only buffer. */
14045 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
14046 return "*";
14047 if (!NILP (b->read_only))
14048 return "%";
14049 return "-";
14050
14051 case '&':
14052 /* This differs from %* in ignoring read-only-ness. */
14053 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
14054 return "*";
14055 return "-";
14056
14057 case '%':
14058 return "%";
14059
14060 case '[':
14061 {
14062 int i;
14063 char *p;
14064
14065 if (command_loop_level > 5)
14066 return "[[[... ";
14067 p = decode_mode_spec_buf;
14068 for (i = 0; i < command_loop_level; i++)
14069 *p++ = '[';
14070 *p = 0;
14071 return decode_mode_spec_buf;
14072 }
14073
14074 case ']':
14075 {
14076 int i;
14077 char *p;
14078
14079 if (command_loop_level > 5)
14080 return " ...]]]";
14081 p = decode_mode_spec_buf;
14082 for (i = 0; i < command_loop_level; i++)
14083 *p++ = ']';
14084 *p = 0;
14085 return decode_mode_spec_buf;
14086 }
14087
14088 case '-':
14089 {
14090 register int i;
14091
14092 /* Let lots_of_dashes be a string of infinite length. */
14093 if (field_width <= 0
14094 || field_width > sizeof (lots_of_dashes))
14095 {
14096 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
14097 decode_mode_spec_buf[i] = '-';
14098 decode_mode_spec_buf[i] = '\0';
14099 return decode_mode_spec_buf;
14100 }
14101 else
14102 return lots_of_dashes;
14103 }
14104
14105 case 'b':
14106 obj = b->name;
14107 break;
14108
14109 case 'c':
14110 {
14111 int col = current_column ();
14112 w->column_number_displayed = make_number (col);
14113 pint2str (decode_mode_spec_buf, field_width, col);
14114 return decode_mode_spec_buf;
14115 }
14116
14117 case 'F':
14118 /* %F displays the frame name. */
14119 if (!NILP (f->title))
14120 return (char *) XSTRING (f->title)->data;
14121 if (f->explicit_name || ! FRAME_WINDOW_P (f))
14122 return (char *) XSTRING (f->name)->data;
14123 return "Emacs";
14124
14125 case 'f':
14126 obj = b->filename;
14127 break;
14128
14129 case 'l':
14130 {
14131 int startpos = XMARKER (w->start)->charpos;
14132 int startpos_byte = marker_byte_position (w->start);
14133 int line, linepos, linepos_byte, topline;
14134 int nlines, junk;
14135 int height = XFASTINT (w->height);
14136
14137 /* If we decided that this buffer isn't suitable for line numbers,
14138 don't forget that too fast. */
14139 if (EQ (w->base_line_pos, w->buffer))
14140 goto no_value;
14141 /* But do forget it, if the window shows a different buffer now. */
14142 else if (BUFFERP (w->base_line_pos))
14143 w->base_line_pos = Qnil;
14144
14145 /* If the buffer is very big, don't waste time. */
14146 if (INTEGERP (Vline_number_display_limit)
14147 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
14148 {
14149 w->base_line_pos = Qnil;
14150 w->base_line_number = Qnil;
14151 goto no_value;
14152 }
14153
14154 if (!NILP (w->base_line_number)
14155 && !NILP (w->base_line_pos)
14156 && XFASTINT (w->base_line_pos) <= startpos)
14157 {
14158 line = XFASTINT (w->base_line_number);
14159 linepos = XFASTINT (w->base_line_pos);
14160 linepos_byte = buf_charpos_to_bytepos (b, linepos);
14161 }
14162 else
14163 {
14164 line = 1;
14165 linepos = BUF_BEGV (b);
14166 linepos_byte = BUF_BEGV_BYTE (b);
14167 }
14168
14169 /* Count lines from base line to window start position. */
14170 nlines = display_count_lines (linepos, linepos_byte,
14171 startpos_byte,
14172 startpos, &junk);
14173
14174 topline = nlines + line;
14175
14176 /* Determine a new base line, if the old one is too close
14177 or too far away, or if we did not have one.
14178 "Too close" means it's plausible a scroll-down would
14179 go back past it. */
14180 if (startpos == BUF_BEGV (b))
14181 {
14182 w->base_line_number = make_number (topline);
14183 w->base_line_pos = make_number (BUF_BEGV (b));
14184 }
14185 else if (nlines < height + 25 || nlines > height * 3 + 50
14186 || linepos == BUF_BEGV (b))
14187 {
14188 int limit = BUF_BEGV (b);
14189 int limit_byte = BUF_BEGV_BYTE (b);
14190 int position;
14191 int distance = (height * 2 + 30) * line_number_display_limit_width;
14192
14193 if (startpos - distance > limit)
14194 {
14195 limit = startpos - distance;
14196 limit_byte = CHAR_TO_BYTE (limit);
14197 }
14198
14199 nlines = display_count_lines (startpos, startpos_byte,
14200 limit_byte,
14201 - (height * 2 + 30),
14202 &position);
14203 /* If we couldn't find the lines we wanted within
14204 line_number_display_limit_width chars per line,
14205 give up on line numbers for this window. */
14206 if (position == limit_byte && limit == startpos - distance)
14207 {
14208 w->base_line_pos = w->buffer;
14209 w->base_line_number = Qnil;
14210 goto no_value;
14211 }
14212
14213 w->base_line_number = make_number (topline - nlines);
14214 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
14215 }
14216
14217 /* Now count lines from the start pos to point. */
14218 nlines = display_count_lines (startpos, startpos_byte,
14219 PT_BYTE, PT, &junk);
14220
14221 /* Record that we did display the line number. */
14222 line_number_displayed = 1;
14223
14224 /* Make the string to show. */
14225 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
14226 return decode_mode_spec_buf;
14227 no_value:
14228 {
14229 char* p = decode_mode_spec_buf;
14230 int pad = field_width - 2;
14231 while (pad-- > 0)
14232 *p++ = ' ';
14233 *p++ = '?';
14234 *p++ = '?';
14235 *p = '\0';
14236 return decode_mode_spec_buf;
14237 }
14238 }
14239 break;
14240
14241 case 'm':
14242 obj = b->mode_name;
14243 break;
14244
14245 case 'n':
14246 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
14247 return " Narrow";
14248 break;
14249
14250 case 'p':
14251 {
14252 int pos = marker_position (w->start);
14253 int total = BUF_ZV (b) - BUF_BEGV (b);
14254
14255 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
14256 {
14257 if (pos <= BUF_BEGV (b))
14258 return "All";
14259 else
14260 return "Bottom";
14261 }
14262 else if (pos <= BUF_BEGV (b))
14263 return "Top";
14264 else
14265 {
14266 if (total > 1000000)
14267 /* Do it differently for a large value, to avoid overflow. */
14268 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
14269 else
14270 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
14271 /* We can't normally display a 3-digit number,
14272 so get us a 2-digit number that is close. */
14273 if (total == 100)
14274 total = 99;
14275 sprintf (decode_mode_spec_buf, "%2d%%", total);
14276 return decode_mode_spec_buf;
14277 }
14278 }
14279
14280 /* Display percentage of size above the bottom of the screen. */
14281 case 'P':
14282 {
14283 int toppos = marker_position (w->start);
14284 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
14285 int total = BUF_ZV (b) - BUF_BEGV (b);
14286
14287 if (botpos >= BUF_ZV (b))
14288 {
14289 if (toppos <= BUF_BEGV (b))
14290 return "All";
14291 else
14292 return "Bottom";
14293 }
14294 else
14295 {
14296 if (total > 1000000)
14297 /* Do it differently for a large value, to avoid overflow. */
14298 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
14299 else
14300 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
14301 /* We can't normally display a 3-digit number,
14302 so get us a 2-digit number that is close. */
14303 if (total == 100)
14304 total = 99;
14305 if (toppos <= BUF_BEGV (b))
14306 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
14307 else
14308 sprintf (decode_mode_spec_buf, "%2d%%", total);
14309 return decode_mode_spec_buf;
14310 }
14311 }
14312
14313 case 's':
14314 /* status of process */
14315 obj = Fget_buffer_process (w->buffer);
14316 if (NILP (obj))
14317 return "no process";
14318 #ifdef subprocesses
14319 obj = Fsymbol_name (Fprocess_status (obj));
14320 #endif
14321 break;
14322
14323 case 't': /* indicate TEXT or BINARY */
14324 #ifdef MODE_LINE_BINARY_TEXT
14325 return MODE_LINE_BINARY_TEXT (b);
14326 #else
14327 return "T";
14328 #endif
14329
14330 case 'z':
14331 /* coding-system (not including end-of-line format) */
14332 case 'Z':
14333 /* coding-system (including end-of-line type) */
14334 {
14335 int eol_flag = (c == 'Z');
14336 char *p = decode_mode_spec_buf;
14337
14338 if (! FRAME_WINDOW_P (f))
14339 {
14340 /* No need to mention EOL here--the terminal never needs
14341 to do EOL conversion. */
14342 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
14343 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
14344 }
14345 p = decode_mode_spec_coding (b->buffer_file_coding_system,
14346 p, eol_flag);
14347
14348 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
14349 #ifdef subprocesses
14350 obj = Fget_buffer_process (Fcurrent_buffer ());
14351 if (PROCESSP (obj))
14352 {
14353 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
14354 p, eol_flag);
14355 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
14356 p, eol_flag);
14357 }
14358 #endif /* subprocesses */
14359 #endif /* 0 */
14360 *p = 0;
14361 return decode_mode_spec_buf;
14362 }
14363 }
14364
14365 if (STRINGP (obj))
14366 {
14367 *multibyte = STRING_MULTIBYTE (obj);
14368 return (char *) XSTRING (obj)->data;
14369 }
14370 else
14371 return "";
14372 }
14373
14374
14375 /* Count up to COUNT lines starting from START / START_BYTE.
14376 But don't go beyond LIMIT_BYTE.
14377 Return the number of lines thus found (always nonnegative).
14378
14379 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
14380
14381 static int
14382 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
14383 int start, start_byte, limit_byte, count;
14384 int *byte_pos_ptr;
14385 {
14386 register unsigned char *cursor;
14387 unsigned char *base;
14388
14389 register int ceiling;
14390 register unsigned char *ceiling_addr;
14391 int orig_count = count;
14392
14393 /* If we are not in selective display mode,
14394 check only for newlines. */
14395 int selective_display = (!NILP (current_buffer->selective_display)
14396 && !INTEGERP (current_buffer->selective_display));
14397
14398 if (count > 0)
14399 {
14400 while (start_byte < limit_byte)
14401 {
14402 ceiling = BUFFER_CEILING_OF (start_byte);
14403 ceiling = min (limit_byte - 1, ceiling);
14404 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
14405 base = (cursor = BYTE_POS_ADDR (start_byte));
14406 while (1)
14407 {
14408 if (selective_display)
14409 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
14410 ;
14411 else
14412 while (*cursor != '\n' && ++cursor != ceiling_addr)
14413 ;
14414
14415 if (cursor != ceiling_addr)
14416 {
14417 if (--count == 0)
14418 {
14419 start_byte += cursor - base + 1;
14420 *byte_pos_ptr = start_byte;
14421 return orig_count;
14422 }
14423 else
14424 if (++cursor == ceiling_addr)
14425 break;
14426 }
14427 else
14428 break;
14429 }
14430 start_byte += cursor - base;
14431 }
14432 }
14433 else
14434 {
14435 while (start_byte > limit_byte)
14436 {
14437 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
14438 ceiling = max (limit_byte, ceiling);
14439 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
14440 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
14441 while (1)
14442 {
14443 if (selective_display)
14444 while (--cursor != ceiling_addr
14445 && *cursor != '\n' && *cursor != 015)
14446 ;
14447 else
14448 while (--cursor != ceiling_addr && *cursor != '\n')
14449 ;
14450
14451 if (cursor != ceiling_addr)
14452 {
14453 if (++count == 0)
14454 {
14455 start_byte += cursor - base + 1;
14456 *byte_pos_ptr = start_byte;
14457 /* When scanning backwards, we should
14458 not count the newline posterior to which we stop. */
14459 return - orig_count - 1;
14460 }
14461 }
14462 else
14463 break;
14464 }
14465 /* Here we add 1 to compensate for the last decrement
14466 of CURSOR, which took it past the valid range. */
14467 start_byte += cursor - base + 1;
14468 }
14469 }
14470
14471 *byte_pos_ptr = limit_byte;
14472
14473 if (count < 0)
14474 return - orig_count + count;
14475 return orig_count - count;
14476
14477 }
14478
14479
14480 \f
14481 /***********************************************************************
14482 Displaying strings
14483 ***********************************************************************/
14484
14485 /* Display a NUL-terminated string, starting with index START.
14486
14487 If STRING is non-null, display that C string. Otherwise, the Lisp
14488 string LISP_STRING is displayed.
14489
14490 If FACE_STRING is not nil, FACE_STRING_POS is a position in
14491 FACE_STRING. Display STRING or LISP_STRING with the face at
14492 FACE_STRING_POS in FACE_STRING:
14493
14494 Display the string in the environment given by IT, but use the
14495 standard display table, temporarily.
14496
14497 FIELD_WIDTH is the minimum number of output glyphs to produce.
14498 If STRING has fewer characters than FIELD_WIDTH, pad to the right
14499 with spaces. If STRING has more characters, more than FIELD_WIDTH
14500 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
14501
14502 PRECISION is the maximum number of characters to output from
14503 STRING. PRECISION < 0 means don't truncate the string.
14504
14505 This is roughly equivalent to printf format specifiers:
14506
14507 FIELD_WIDTH PRECISION PRINTF
14508 ----------------------------------------
14509 -1 -1 %s
14510 -1 10 %.10s
14511 10 -1 %10s
14512 20 10 %20.10s
14513
14514 MULTIBYTE zero means do not display multibyte chars, > 0 means do
14515 display them, and < 0 means obey the current buffer's value of
14516 enable_multibyte_characters.
14517
14518 Value is the number of glyphs produced. */
14519
14520 static int
14521 display_string (string, lisp_string, face_string, face_string_pos,
14522 start, it, field_width, precision, max_x, multibyte)
14523 unsigned char *string;
14524 Lisp_Object lisp_string;
14525 Lisp_Object face_string;
14526 int face_string_pos;
14527 int start;
14528 struct it *it;
14529 int field_width, precision, max_x;
14530 int multibyte;
14531 {
14532 int hpos_at_start = it->hpos;
14533 int saved_face_id = it->face_id;
14534 struct glyph_row *row = it->glyph_row;
14535
14536 /* Initialize the iterator IT for iteration over STRING beginning
14537 with index START. */
14538 reseat_to_string (it, string, lisp_string, start,
14539 precision, field_width, multibyte);
14540
14541 /* If displaying STRING, set up the face of the iterator
14542 from LISP_STRING, if that's given. */
14543 if (STRINGP (face_string))
14544 {
14545 int endptr;
14546 struct face *face;
14547
14548 it->face_id
14549 = face_at_string_position (it->w, face_string, face_string_pos,
14550 0, it->region_beg_charpos,
14551 it->region_end_charpos,
14552 &endptr, it->base_face_id, 0);
14553 face = FACE_FROM_ID (it->f, it->face_id);
14554 it->face_box_p = face->box != FACE_NO_BOX;
14555 }
14556
14557 /* Set max_x to the maximum allowed X position. Don't let it go
14558 beyond the right edge of the window. */
14559 if (max_x <= 0)
14560 max_x = it->last_visible_x;
14561 else
14562 max_x = min (max_x, it->last_visible_x);
14563
14564 /* Skip over display elements that are not visible. because IT->w is
14565 hscrolled. */
14566 if (it->current_x < it->first_visible_x)
14567 move_it_in_display_line_to (it, 100000, it->first_visible_x,
14568 MOVE_TO_POS | MOVE_TO_X);
14569
14570 row->ascent = it->max_ascent;
14571 row->height = it->max_ascent + it->max_descent;
14572 row->phys_ascent = it->max_phys_ascent;
14573 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14574
14575 /* This condition is for the case that we are called with current_x
14576 past last_visible_x. */
14577 while (it->current_x < max_x)
14578 {
14579 int x_before, x, n_glyphs_before, i, nglyphs;
14580
14581 /* Get the next display element. */
14582 if (!get_next_display_element (it))
14583 break;
14584
14585 /* Produce glyphs. */
14586 x_before = it->current_x;
14587 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
14588 PRODUCE_GLYPHS (it);
14589
14590 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
14591 i = 0;
14592 x = x_before;
14593 while (i < nglyphs)
14594 {
14595 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
14596
14597 if (!it->truncate_lines_p
14598 && x + glyph->pixel_width > max_x)
14599 {
14600 /* End of continued line or max_x reached. */
14601 if (CHAR_GLYPH_PADDING_P (*glyph))
14602 {
14603 /* A wide character is unbreakable. */
14604 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
14605 it->current_x = x_before;
14606 }
14607 else
14608 {
14609 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
14610 it->current_x = x;
14611 }
14612 break;
14613 }
14614 else if (x + glyph->pixel_width > it->first_visible_x)
14615 {
14616 /* Glyph is at least partially visible. */
14617 ++it->hpos;
14618 if (x < it->first_visible_x)
14619 it->glyph_row->x = x - it->first_visible_x;
14620 }
14621 else
14622 {
14623 /* Glyph is off the left margin of the display area.
14624 Should not happen. */
14625 abort ();
14626 }
14627
14628 row->ascent = max (row->ascent, it->max_ascent);
14629 row->height = max (row->height, it->max_ascent + it->max_descent);
14630 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14631 row->phys_height = max (row->phys_height,
14632 it->max_phys_ascent + it->max_phys_descent);
14633 x += glyph->pixel_width;
14634 ++i;
14635 }
14636
14637 /* Stop if max_x reached. */
14638 if (i < nglyphs)
14639 break;
14640
14641 /* Stop at line ends. */
14642 if (ITERATOR_AT_END_OF_LINE_P (it))
14643 {
14644 it->continuation_lines_width = 0;
14645 break;
14646 }
14647
14648 set_iterator_to_next (it, 1);
14649
14650 /* Stop if truncating at the right edge. */
14651 if (it->truncate_lines_p
14652 && it->current_x >= it->last_visible_x)
14653 {
14654 /* Add truncation mark, but don't do it if the line is
14655 truncated at a padding space. */
14656 if (IT_CHARPOS (*it) < it->string_nchars)
14657 {
14658 if (!FRAME_WINDOW_P (it->f))
14659 {
14660 int i, n;
14661
14662 if (it->current_x > it->last_visible_x)
14663 {
14664 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
14665 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
14666 break;
14667 for (n = row->used[TEXT_AREA]; i < n; ++i)
14668 {
14669 row->used[TEXT_AREA] = i;
14670 produce_special_glyphs (it, IT_TRUNCATION);
14671 }
14672 }
14673 produce_special_glyphs (it, IT_TRUNCATION);
14674 }
14675 it->glyph_row->truncated_on_right_p = 1;
14676 }
14677 break;
14678 }
14679 }
14680
14681 /* Maybe insert a truncation at the left. */
14682 if (it->first_visible_x
14683 && IT_CHARPOS (*it) > 0)
14684 {
14685 if (!FRAME_WINDOW_P (it->f))
14686 insert_left_trunc_glyphs (it);
14687 it->glyph_row->truncated_on_left_p = 1;
14688 }
14689
14690 it->face_id = saved_face_id;
14691
14692 /* Value is number of columns displayed. */
14693 return it->hpos - hpos_at_start;
14694 }
14695
14696
14697 \f
14698 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
14699 appears as an element of LIST or as the car of an element of LIST.
14700 If PROPVAL is a list, compare each element against LIST in that
14701 way, and return 1/2 if any element of PROPVAL is found in LIST.
14702 Otherwise return 0. This function cannot quit.
14703 The return value is 2 if the text is invisible but with an ellipsis
14704 and 1 if it's invisible and without an ellipsis. */
14705
14706 int
14707 invisible_p (propval, list)
14708 register Lisp_Object propval;
14709 Lisp_Object list;
14710 {
14711 register Lisp_Object tail, proptail;
14712
14713 for (tail = list; CONSP (tail); tail = XCDR (tail))
14714 {
14715 register Lisp_Object tem;
14716 tem = XCAR (tail);
14717 if (EQ (propval, tem))
14718 return 1;
14719 if (CONSP (tem) && EQ (propval, XCAR (tem)))
14720 return NILP (XCDR (tem)) ? 1 : 2;
14721 }
14722
14723 if (CONSP (propval))
14724 {
14725 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
14726 {
14727 Lisp_Object propelt;
14728 propelt = XCAR (proptail);
14729 for (tail = list; CONSP (tail); tail = XCDR (tail))
14730 {
14731 register Lisp_Object tem;
14732 tem = XCAR (tail);
14733 if (EQ (propelt, tem))
14734 return 1;
14735 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
14736 return NILP (XCDR (tem)) ? 1 : 2;
14737 }
14738 }
14739 }
14740
14741 return 0;
14742 }
14743
14744 \f
14745 /***********************************************************************
14746 Initialization
14747 ***********************************************************************/
14748
14749 void
14750 syms_of_xdisp ()
14751 {
14752 Vwith_echo_area_save_vector = Qnil;
14753 staticpro (&Vwith_echo_area_save_vector);
14754
14755 Vmessage_stack = Qnil;
14756 staticpro (&Vmessage_stack);
14757
14758 Qinhibit_redisplay = intern ("inhibit-redisplay");
14759 staticpro (&Qinhibit_redisplay);
14760
14761 message_dolog_marker1 = Fmake_marker ();
14762 staticpro (&message_dolog_marker1);
14763 message_dolog_marker2 = Fmake_marker ();
14764 staticpro (&message_dolog_marker2);
14765 message_dolog_marker3 = Fmake_marker ();
14766 staticpro (&message_dolog_marker3);
14767
14768 #if GLYPH_DEBUG
14769 defsubr (&Sdump_glyph_matrix);
14770 defsubr (&Sdump_glyph_row);
14771 defsubr (&Sdump_tool_bar_row);
14772 defsubr (&Strace_redisplay);
14773 defsubr (&Strace_to_stderr);
14774 #endif
14775 #ifdef HAVE_WINDOW_SYSTEM
14776 defsubr (&Stool_bar_lines_needed);
14777 #endif
14778
14779 staticpro (&Qmenu_bar_update_hook);
14780 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
14781
14782 staticpro (&Qoverriding_terminal_local_map);
14783 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
14784
14785 staticpro (&Qoverriding_local_map);
14786 Qoverriding_local_map = intern ("overriding-local-map");
14787
14788 staticpro (&Qwindow_scroll_functions);
14789 Qwindow_scroll_functions = intern ("window-scroll-functions");
14790
14791 staticpro (&Qredisplay_end_trigger_functions);
14792 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
14793
14794 staticpro (&Qinhibit_point_motion_hooks);
14795 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
14796
14797 QCdata = intern (":data");
14798 staticpro (&QCdata);
14799 Qdisplay = intern ("display");
14800 staticpro (&Qdisplay);
14801 Qspace_width = intern ("space-width");
14802 staticpro (&Qspace_width);
14803 Qraise = intern ("raise");
14804 staticpro (&Qraise);
14805 Qspace = intern ("space");
14806 staticpro (&Qspace);
14807 Qmargin = intern ("margin");
14808 staticpro (&Qmargin);
14809 Qleft_margin = intern ("left-margin");
14810 staticpro (&Qleft_margin);
14811 Qright_margin = intern ("right-margin");
14812 staticpro (&Qright_margin);
14813 Qalign_to = intern ("align-to");
14814 staticpro (&Qalign_to);
14815 QCalign_to = intern (":align-to");
14816 staticpro (&QCalign_to);
14817 Qrelative_width = intern ("relative-width");
14818 staticpro (&Qrelative_width);
14819 QCrelative_width = intern (":relative-width");
14820 staticpro (&QCrelative_width);
14821 QCrelative_height = intern (":relative-height");
14822 staticpro (&QCrelative_height);
14823 QCeval = intern (":eval");
14824 staticpro (&QCeval);
14825 QCpropertize = intern (":propertize");
14826 staticpro (&QCpropertize);
14827 Qwhen = intern ("when");
14828 staticpro (&Qwhen);
14829 QCfile = intern (":file");
14830 staticpro (&QCfile);
14831 Qfontified = intern ("fontified");
14832 staticpro (&Qfontified);
14833 Qfontification_functions = intern ("fontification-functions");
14834 staticpro (&Qfontification_functions);
14835 Qtrailing_whitespace = intern ("trailing-whitespace");
14836 staticpro (&Qtrailing_whitespace);
14837 Qimage = intern ("image");
14838 staticpro (&Qimage);
14839 Qmessage_truncate_lines = intern ("message-truncate-lines");
14840 staticpro (&Qmessage_truncate_lines);
14841 Qcursor_in_non_selected_windows = intern ("cursor-in-non-selected-windows");
14842 staticpro (&Qcursor_in_non_selected_windows);
14843 Qgrow_only = intern ("grow-only");
14844 staticpro (&Qgrow_only);
14845 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
14846 staticpro (&Qinhibit_menubar_update);
14847 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
14848 staticpro (&Qinhibit_eval_during_redisplay);
14849 Qposition = intern ("position");
14850 staticpro (&Qposition);
14851 Qbuffer_position = intern ("buffer-position");
14852 staticpro (&Qbuffer_position);
14853 Qobject = intern ("object");
14854 staticpro (&Qobject);
14855
14856 last_arrow_position = Qnil;
14857 last_arrow_string = Qnil;
14858 staticpro (&last_arrow_position);
14859 staticpro (&last_arrow_string);
14860
14861 echo_buffer[0] = echo_buffer[1] = Qnil;
14862 staticpro (&echo_buffer[0]);
14863 staticpro (&echo_buffer[1]);
14864
14865 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
14866 staticpro (&echo_area_buffer[0]);
14867 staticpro (&echo_area_buffer[1]);
14868
14869 Vmessages_buffer_name = build_string ("*Messages*");
14870 staticpro (&Vmessages_buffer_name);
14871
14872 mode_line_proptrans_alist = Qnil;
14873 staticpro (&mode_line_proptrans_alist);
14874
14875 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
14876 doc: /* Non-nil means highlight trailing whitespace.
14877 The face used for trailing whitespace is `trailing-whitespace'. */);
14878 Vshow_trailing_whitespace = Qnil;
14879
14880 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
14881 doc: /* Non-nil means don't actually do any redisplay.
14882 This is used for internal purposes. */);
14883 Vinhibit_redisplay = Qnil;
14884
14885 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
14886 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
14887 Vglobal_mode_string = Qnil;
14888
14889 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
14890 doc: /* Marker for where to display an arrow on top of the buffer text.
14891 This must be the beginning of a line in order to work.
14892 See also `overlay-arrow-string'. */);
14893 Voverlay_arrow_position = Qnil;
14894
14895 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
14896 doc: /* String to display as an arrow. See also `overlay-arrow-position'. */);
14897 Voverlay_arrow_string = Qnil;
14898
14899 DEFVAR_INT ("scroll-step", &scroll_step,
14900 doc: /* *The number of lines to try scrolling a window by when point moves out.
14901 If that fails to bring point back on frame, point is centered instead.
14902 If this is zero, point is always centered after it moves off frame.
14903 If you want scrolling to always be a line at a time, you should set
14904 `scroll-conservatively' to a large value rather than set this to 1. */);
14905
14906 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
14907 doc: /* *Scroll up to this many lines, to bring point back on screen.
14908 A value of zero means to scroll the text to center point vertically
14909 in the window. */);
14910 scroll_conservatively = 0;
14911
14912 DEFVAR_INT ("scroll-margin", &scroll_margin,
14913 doc: /* *Number of lines of margin at the top and bottom of a window.
14914 Recenter the window whenever point gets within this many lines
14915 of the top or bottom of the window. */);
14916 scroll_margin = 0;
14917
14918 #if GLYPH_DEBUG
14919 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
14920 #endif
14921
14922 DEFVAR_BOOL ("truncate-partial-width-windows",
14923 &truncate_partial_width_windows,
14924 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
14925 truncate_partial_width_windows = 1;
14926
14927 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
14928 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
14929 Any other value means to use the appropriate face, `mode-line',
14930 `header-line', or `menu' respectively.
14931
14932 This variable is deprecated; please change the above faces instead. */);
14933 mode_line_inverse_video = 1;
14934
14935 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
14936 doc: /* *Maximum buffer size for which line number should be displayed.
14937 If the buffer is bigger than this, the line number does not appear
14938 in the mode line. A value of nil means no limit. */);
14939 Vline_number_display_limit = Qnil;
14940
14941 DEFVAR_INT ("line-number-display-limit-width",
14942 &line_number_display_limit_width,
14943 doc: /* *Maximum line width (in characters) for line number display.
14944 If the average length of the lines near point is bigger than this, then the
14945 line number may be omitted from the mode line. */);
14946 line_number_display_limit_width = 200;
14947
14948 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
14949 doc: /* *Non-nil means highlight region even in nonselected windows. */);
14950 highlight_nonselected_windows = 0;
14951
14952 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
14953 doc: /* Non-nil if more than one frame is visible on this display.
14954 Minibuffer-only frames don't count, but iconified frames do.
14955 This variable is not guaranteed to be accurate except while processing
14956 `frame-title-format' and `icon-title-format'. */);
14957
14958 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
14959 doc: /* Template for displaying the title bar of visible frames.
14960 \(Assuming the window manager supports this feature.)
14961 This variable has the same structure as `mode-line-format' (which see),
14962 and is used only on frames for which no explicit name has been set
14963 \(see `modify-frame-parameters'). */);
14964 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
14965 doc: /* Template for displaying the title bar of an iconified frame.
14966 \(Assuming the window manager supports this feature.)
14967 This variable has the same structure as `mode-line-format' (which see),
14968 and is used only on frames for which no explicit name has been set
14969 \(see `modify-frame-parameters'). */);
14970 Vicon_title_format
14971 = Vframe_title_format
14972 = Fcons (intern ("multiple-frames"),
14973 Fcons (build_string ("%b"),
14974 Fcons (Fcons (empty_string,
14975 Fcons (intern ("invocation-name"),
14976 Fcons (build_string ("@"),
14977 Fcons (intern ("system-name"),
14978 Qnil)))),
14979 Qnil)));
14980
14981 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
14982 doc: /* Maximum number of lines to keep in the message log buffer.
14983 If nil, disable message logging. If t, log messages but don't truncate
14984 the buffer when it becomes large. */);
14985 Vmessage_log_max = make_number (50);
14986
14987 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
14988 doc: /* Functions called before redisplay, if window sizes have changed.
14989 The value should be a list of functions that take one argument.
14990 Just before redisplay, for each frame, if any of its windows have changed
14991 size since the last redisplay, or have been split or deleted,
14992 all the functions in the list are called, with the frame as argument. */);
14993 Vwindow_size_change_functions = Qnil;
14994
14995 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
14996 doc: /* List of Functions to call before redisplaying a window with scrolling.
14997 Each function is called with two arguments, the window
14998 and its new display-start position. Note that the value of `window-end'
14999 is not valid when these functions are called. */);
15000 Vwindow_scroll_functions = Qnil;
15001
15002 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
15003 doc: /* *Non-nil means automatically resize tool-bars.
15004 This increases a tool-bar's height if not all tool-bar items are visible.
15005 It decreases a tool-bar's height when it would display blank lines
15006 otherwise. */);
15007 auto_resize_tool_bars_p = 1;
15008
15009 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
15010 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
15011 auto_raise_tool_bar_buttons_p = 1;
15012
15013 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
15014 doc: /* *Margin around tool-bar buttons in pixels.
15015 If an integer, use that for both horizontal and vertical margins.
15016 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
15017 HORZ specifying the horizontal margin, and VERT specifying the
15018 vertical margin. */);
15019 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
15020
15021 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
15022 doc: /* *Relief thickness of tool-bar buttons. */);
15023 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
15024
15025 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
15026 doc: /* List of functions to call to fontify regions of text.
15027 Each function is called with one argument POS. Functions must
15028 fontify a region starting at POS in the current buffer, and give
15029 fontified regions the property `fontified'. */);
15030 Vfontification_functions = Qnil;
15031 Fmake_variable_buffer_local (Qfontification_functions);
15032
15033 DEFVAR_BOOL ("unibyte-display-via-language-environment",
15034 &unibyte_display_via_language_environment,
15035 doc: /* *Non-nil means display unibyte text according to language environment.
15036 Specifically this means that unibyte non-ASCII characters
15037 are displayed by converting them to the equivalent multibyte characters
15038 according to the current language environment. As a result, they are
15039 displayed according to the current fontset. */);
15040 unibyte_display_via_language_environment = 0;
15041
15042 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
15043 doc: /* *Maximum height for resizing mini-windows.
15044 If a float, it specifies a fraction of the mini-window frame's height.
15045 If an integer, it specifies a number of lines. */);
15046 Vmax_mini_window_height = make_float (0.25);
15047
15048 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
15049 doc: /* *How to resize mini-windows.
15050 A value of nil means don't automatically resize mini-windows.
15051 A value of t means resize them to fit the text displayed in them.
15052 A value of `grow-only', the default, means let mini-windows grow
15053 only, until their display becomes empty, at which point the windows
15054 go back to their normal size. */);
15055 Vresize_mini_windows = Qgrow_only;
15056
15057 DEFVAR_BOOL ("cursor-in-non-selected-windows",
15058 &cursor_in_non_selected_windows,
15059 doc: /* *Non-nil means display a hollow cursor in non-selected windows.
15060 nil means don't display a cursor there. */);
15061 cursor_in_non_selected_windows = 1;
15062
15063 DEFVAR_BOOL ("automatic-hscrolling", &automatic_hscrolling_p,
15064 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
15065 automatic_hscrolling_p = 1;
15066
15067 DEFVAR_INT ("automatic-hscroll-margin", &automatic_hscroll_margin,
15068 doc: /* *How many columns away from the window edge point is allowed to get
15069 before automatic hscrolling will horizontally scroll the window. */);
15070 automatic_hscroll_margin = 5;
15071
15072 DEFVAR_LISP ("automatic-hscroll-step", &Vautomatic_hscroll_step,
15073 doc: /* *How many columns to scroll the window when point gets too close to the edge.
15074 When point is less than `automatic-hscroll-margin' columns from the window
15075 edge, automatic hscrolling will scroll the window by the amount of columns
15076 determined by this variable. If its value is a positive integer, scroll that
15077 many columns. If it's a positive floating-point number, it specifies the
15078 fraction of the window's width to scroll. If it's nil or zero, point will be
15079 centered horizontally after the scroll. Any other value, including negative
15080 numbers, are treated as if the value were zero.
15081
15082 Automatic hscrolling always moves point outside the scroll margin, so if
15083 point was more than scroll step columns inside the margin, the window will
15084 scroll more than the value given by the scroll step.
15085
15086 Note that the lower bound for automatic hscrolling specified by `scroll-left'
15087 and `scroll-right' overrides this variable's effect. */);
15088 Vautomatic_hscroll_step = make_number (0);
15089
15090 DEFVAR_LISP ("image-types", &Vimage_types,
15091 doc: /* List of supported image types.
15092 Each element of the list is a symbol for a supported image type. */);
15093 Vimage_types = Qnil;
15094
15095 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
15096 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
15097 Bind this around calls to `message' to let it take effect. */);
15098 message_truncate_lines = 0;
15099
15100 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
15101 doc: /* Normal hook run for clicks on menu bar, before displaying a submenu.
15102 Can be used to update submenus whose contents should vary. */);
15103 Vmenu_bar_update_hook = Qnil;
15104
15105 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
15106 doc: /* Non-nil means don't update menu bars. Internal use only. */);
15107 inhibit_menubar_update = 0;
15108
15109 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
15110 doc: /* Non-nil means don't eval Lisp during redisplay. */);
15111 inhibit_eval_during_redisplay = 0;
15112
15113 #if GLYPH_DEBUG
15114 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
15115 doc: /* Inhibit try_window_id display optimization. */);
15116 inhibit_try_window_id = 0;
15117
15118 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
15119 doc: /* Inhibit try_window_reusing display optimization. */);
15120 inhibit_try_window_reusing = 0;
15121
15122 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
15123 doc: /* Inhibit try_cursor_movement display optimization. */);
15124 inhibit_try_cursor_movement = 0;
15125 #endif /* GLYPH_DEBUG */
15126 }
15127
15128
15129 /* Initialize this module when Emacs starts. */
15130
15131 void
15132 init_xdisp ()
15133 {
15134 Lisp_Object root_window;
15135 struct window *mini_w;
15136
15137 current_header_line_height = current_mode_line_height = -1;
15138
15139 CHARPOS (this_line_start_pos) = 0;
15140
15141 mini_w = XWINDOW (minibuf_window);
15142 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
15143
15144 if (!noninteractive)
15145 {
15146 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
15147 int i;
15148
15149 XWINDOW (root_window)->top = make_number (FRAME_TOP_MARGIN (f));
15150 set_window_height (root_window,
15151 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
15152 0);
15153 mini_w->top = make_number (FRAME_HEIGHT (f) - 1);
15154 set_window_height (minibuf_window, 1, 0);
15155
15156 XWINDOW (root_window)->width = make_number (FRAME_WIDTH (f));
15157 mini_w->width = make_number (FRAME_WIDTH (f));
15158
15159 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
15160 scratch_glyph_row.glyphs[TEXT_AREA + 1]
15161 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
15162
15163 /* The default ellipsis glyphs `...'. */
15164 for (i = 0; i < 3; ++i)
15165 default_invis_vector[i] = make_number ('.');
15166 }
15167
15168 #ifdef HAVE_WINDOW_SYSTEM
15169 {
15170 /* Allocate the buffer for frame titles. */
15171 int size = 100;
15172 frame_title_buf = (char *) xmalloc (size);
15173 frame_title_buf_end = frame_title_buf + size;
15174 frame_title_ptr = NULL;
15175 }
15176 #endif /* HAVE_WINDOW_SYSTEM */
15177
15178 help_echo_showing_p = 0;
15179 }
15180
15181