]> code.delx.au - gnu-emacs/blob - src/xdisp.c
(command-line-processed): Doc fix.
[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;
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, Qimage, 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 \f
295 /* The buffer position of the first character appearing entirely or
296 partially on the line of the selected window which contains the
297 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
298 redisplay optimization in redisplay_internal. */
299
300 static struct text_pos this_line_start_pos;
301
302 /* Number of characters past the end of the line above, including the
303 terminating newline. */
304
305 static struct text_pos this_line_end_pos;
306
307 /* The vertical positions and the height of this line. */
308
309 static int this_line_vpos;
310 static int this_line_y;
311 static int this_line_pixel_height;
312
313 /* X position at which this display line starts. Usually zero;
314 negative if first character is partially visible. */
315
316 static int this_line_start_x;
317
318 /* Buffer that this_line_.* variables are referring to. */
319
320 static struct buffer *this_line_buffer;
321
322 /* Nonzero means truncate lines in all windows less wide than the
323 frame. */
324
325 int truncate_partial_width_windows;
326
327 /* A flag to control how to display unibyte 8-bit character. */
328
329 int unibyte_display_via_language_environment;
330
331 /* Nonzero means we have more than one non-mini-buffer-only frame.
332 Not guaranteed to be accurate except while parsing
333 frame-title-format. */
334
335 int multiple_frames;
336
337 Lisp_Object Vglobal_mode_string;
338
339 /* Marker for where to display an arrow on top of the buffer text. */
340
341 Lisp_Object Voverlay_arrow_position;
342
343 /* String to display for the arrow. Only used on terminal frames. */
344
345 Lisp_Object Voverlay_arrow_string;
346
347 /* Values of those variables at last redisplay. However, if
348 Voverlay_arrow_position is a marker, last_arrow_position is its
349 numerical position. */
350
351 static Lisp_Object last_arrow_position, last_arrow_string;
352
353 /* Like mode-line-format, but for the title bar on a visible frame. */
354
355 Lisp_Object Vframe_title_format;
356
357 /* Like mode-line-format, but for the title bar on an iconified frame. */
358
359 Lisp_Object Vicon_title_format;
360
361 /* List of functions to call when a window's size changes. These
362 functions get one arg, a frame on which one or more windows' sizes
363 have changed. */
364
365 static Lisp_Object Vwindow_size_change_functions;
366
367 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
368
369 /* Nonzero if overlay arrow has been displayed once in this window. */
370
371 static int overlay_arrow_seen;
372
373 /* Nonzero means highlight the region even in nonselected windows. */
374
375 int highlight_nonselected_windows;
376
377 /* If cursor motion alone moves point off frame, try scrolling this
378 many lines up or down if that will bring it back. */
379
380 static int scroll_step;
381
382 /* Non-0 means scroll just far enough to bring point back on the
383 screen, when appropriate. */
384
385 static int scroll_conservatively;
386
387 /* Recenter the window whenever point gets within this many lines of
388 the top or bottom of the window. This value is translated into a
389 pixel value by multiplying it with CANON_Y_UNIT, which means that
390 there is really a fixed pixel height scroll margin. */
391
392 int scroll_margin;
393
394 /* Number of windows showing the buffer of the selected window (or
395 another buffer with the same base buffer). keyboard.c refers to
396 this. */
397
398 int buffer_shared;
399
400 /* Vector containing glyphs for an ellipsis `...'. */
401
402 static Lisp_Object default_invis_vector[3];
403
404 /* Zero means display the mode-line/header-line/menu-bar in the default face
405 (this slightly odd definition is for compatibility with previous versions
406 of emacs), non-zero means display them using their respective faces.
407
408 This variable is deprecated. */
409
410 int mode_line_inverse_video;
411
412 /* Prompt to display in front of the mini-buffer contents. */
413
414 Lisp_Object minibuf_prompt;
415
416 /* Width of current mini-buffer prompt. Only set after display_line
417 of the line that contains the prompt. */
418
419 int minibuf_prompt_width;
420 int minibuf_prompt_pixel_width;
421
422 /* This is the window where the echo area message was displayed. It
423 is always a mini-buffer window, but it may not be the same window
424 currently active as a mini-buffer. */
425
426 Lisp_Object echo_area_window;
427
428 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
429 pushes the current message and the value of
430 message_enable_multibyte on the stack, the function restore_message
431 pops the stack and displays MESSAGE again. */
432
433 Lisp_Object Vmessage_stack;
434
435 /* Nonzero means multibyte characters were enabled when the echo area
436 message was specified. */
437
438 int message_enable_multibyte;
439
440 /* True if we should redraw the mode lines on the next redisplay. */
441
442 int update_mode_lines;
443
444 /* Nonzero if window sizes or contents have changed since last
445 redisplay that finished */
446
447 int windows_or_buffers_changed;
448
449 /* Nonzero after display_mode_line if %l was used and it displayed a
450 line number. */
451
452 int line_number_displayed;
453
454 /* Maximum buffer size for which to display line numbers. */
455
456 Lisp_Object Vline_number_display_limit;
457
458 /* line width to consider when repostioning for line number display */
459
460 static int line_number_display_limit_width;
461
462 /* Number of lines to keep in the message log buffer. t means
463 infinite. nil means don't log at all. */
464
465 Lisp_Object Vmessage_log_max;
466
467 /* The name of the *Messages* buffer, a string. */
468
469 static Lisp_Object Vmessages_buffer_name;
470
471 /* Current, index 0, and last displayed echo area message. Either
472 buffers from echo_buffers, or nil to indicate no message. */
473
474 Lisp_Object echo_area_buffer[2];
475
476 /* The buffers referenced from echo_area_buffer. */
477
478 static Lisp_Object echo_buffer[2];
479
480 /* A vector saved used in with_area_buffer to reduce consing. */
481
482 static Lisp_Object Vwith_echo_area_save_vector;
483
484 /* Non-zero means display_echo_area should display the last echo area
485 message again. Set by redisplay_preserve_echo_area. */
486
487 static int display_last_displayed_message_p;
488
489 /* Nonzero if echo area is being used by print; zero if being used by
490 message. */
491
492 int message_buf_print;
493
494 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
495
496 Lisp_Object Qinhibit_menubar_update;
497 int inhibit_menubar_update;
498
499 /* Maximum height for resizing mini-windows. Either a float
500 specifying a fraction of the available height, or an integer
501 specifying a number of lines. */
502
503 Lisp_Object Vmax_mini_window_height;
504
505 /* Non-zero means messages should be displayed with truncated
506 lines instead of being continued. */
507
508 int message_truncate_lines;
509 Lisp_Object Qmessage_truncate_lines;
510
511 /* Set to 1 in clear_message to make redisplay_internal aware
512 of an emptied echo area. */
513
514 static int message_cleared_p;
515
516 /* Non-zero means we want a hollow cursor in windows that are not
517 selected. Zero means there's no cursor in such windows. */
518
519 int cursor_in_non_selected_windows;
520 Lisp_Object Qcursor_in_non_selected_windows;
521
522 /* A scratch glyph row with contents used for generating truncation
523 glyphs. Also used in direct_output_for_insert. */
524
525 #define MAX_SCRATCH_GLYPHS 100
526 struct glyph_row scratch_glyph_row;
527 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
528
529 /* Ascent and height of the last line processed by move_it_to. */
530
531 static int last_max_ascent, last_height;
532
533 /* Non-zero if there's a help-echo in the echo area. */
534
535 int help_echo_showing_p;
536
537 /* If >= 0, computed, exact values of mode-line and header-line height
538 to use in the macros CURRENT_MODE_LINE_HEIGHT and
539 CURRENT_HEADER_LINE_HEIGHT. */
540
541 int current_mode_line_height, current_header_line_height;
542
543 /* The maximum distance to look ahead for text properties. Values
544 that are too small let us call compute_char_face and similar
545 functions too often which is expensive. Values that are too large
546 let us call compute_char_face and alike too often because we
547 might not be interested in text properties that far away. */
548
549 #define TEXT_PROP_DISTANCE_LIMIT 100
550
551 #if GLYPH_DEBUG
552
553 /* Variables to turn off display optimizations from Lisp. */
554
555 int inhibit_try_window_id, inhibit_try_window_reusing;
556 int inhibit_try_cursor_movement;
557
558 /* Non-zero means print traces of redisplay if compiled with
559 GLYPH_DEBUG != 0. */
560
561 int trace_redisplay_p;
562
563 #endif /* GLYPH_DEBUG */
564
565 #ifdef DEBUG_TRACE_MOVE
566 /* Non-zero means trace with TRACE_MOVE to stderr. */
567 int trace_move;
568
569 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
570 #else
571 #define TRACE_MOVE(x) (void) 0
572 #endif
573
574 /* Non-zero means automatically scroll windows horizontally to make
575 point visible. */
576
577 int automatic_hscrolling_p;
578
579 /* A list of symbols, one for each supported image type. */
580
581 Lisp_Object Vimage_types;
582
583 /* The variable `resize-mini-windows'. If nil, don't resize
584 mini-windows. If t, always resize them to fit the text they
585 display. If `grow-only', let mini-windows grow only until they
586 become empty. */
587
588 Lisp_Object Vresize_mini_windows;
589
590 /* Value returned from text property handlers (see below). */
591
592 enum prop_handled
593 {
594 HANDLED_NORMALLY,
595 HANDLED_RECOMPUTE_PROPS,
596 HANDLED_OVERLAY_STRING_CONSUMED,
597 HANDLED_RETURN
598 };
599
600 /* A description of text properties that redisplay is interested
601 in. */
602
603 struct props
604 {
605 /* The name of the property. */
606 Lisp_Object *name;
607
608 /* A unique index for the property. */
609 enum prop_idx idx;
610
611 /* A handler function called to set up iterator IT from the property
612 at IT's current position. Value is used to steer handle_stop. */
613 enum prop_handled (*handler) P_ ((struct it *it));
614 };
615
616 static enum prop_handled handle_face_prop P_ ((struct it *));
617 static enum prop_handled handle_invisible_prop P_ ((struct it *));
618 static enum prop_handled handle_display_prop P_ ((struct it *));
619 static enum prop_handled handle_composition_prop P_ ((struct it *));
620 static enum prop_handled handle_overlay_change P_ ((struct it *));
621 static enum prop_handled handle_fontified_prop P_ ((struct it *));
622
623 /* Properties handled by iterators. */
624
625 static struct props it_props[] =
626 {
627 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
628 /* Handle `face' before `display' because some sub-properties of
629 `display' need to know the face. */
630 {&Qface, FACE_PROP_IDX, handle_face_prop},
631 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
632 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
633 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
634 {NULL, 0, NULL}
635 };
636
637 /* Value is the position described by X. If X is a marker, value is
638 the marker_position of X. Otherwise, value is X. */
639
640 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
641
642 /* Enumeration returned by some move_it_.* functions internally. */
643
644 enum move_it_result
645 {
646 /* Not used. Undefined value. */
647 MOVE_UNDEFINED,
648
649 /* Move ended at the requested buffer position or ZV. */
650 MOVE_POS_MATCH_OR_ZV,
651
652 /* Move ended at the requested X pixel position. */
653 MOVE_X_REACHED,
654
655 /* Move within a line ended at the end of a line that must be
656 continued. */
657 MOVE_LINE_CONTINUED,
658
659 /* Move within a line ended at the end of a line that would
660 be displayed truncated. */
661 MOVE_LINE_TRUNCATED,
662
663 /* Move within a line ended at a line end. */
664 MOVE_NEWLINE_OR_CR
665 };
666
667
668 \f
669 /* Function prototypes. */
670
671 static void setup_for_ellipsis P_ ((struct it *));
672 static void mark_window_display_accurate_1 P_ ((struct window *, int));
673 static int single_display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
674 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
675 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
676 static int redisplay_mode_lines P_ ((Lisp_Object, int));
677 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
678
679 #if 0
680 static int invisible_text_between_p P_ ((struct it *, int, int));
681 #endif
682
683 static int next_element_from_ellipsis P_ ((struct it *));
684 static void pint2str P_ ((char *, int, int));
685 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
686 struct text_pos));
687 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
688 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
689 static void store_frame_title_char P_ ((char));
690 static int store_frame_title P_ ((unsigned char *, int, int));
691 static void x_consider_frame_title P_ ((Lisp_Object));
692 static void handle_stop P_ ((struct it *));
693 static int tool_bar_lines_needed P_ ((struct frame *));
694 static int single_display_prop_intangible_p P_ ((Lisp_Object));
695 static void ensure_echo_area_buffers P_ ((void));
696 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
697 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
698 static int with_echo_area_buffer P_ ((struct window *, int,
699 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
700 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
701 static void clear_garbaged_frames P_ ((void));
702 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
703 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
704 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
705 static int display_echo_area P_ ((struct window *));
706 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
707 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
708 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
709 static int string_char_and_length P_ ((unsigned char *, int, int *));
710 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
711 struct text_pos));
712 static int compute_window_start_on_continuation_line P_ ((struct window *));
713 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
714 static void insert_left_trunc_glyphs P_ ((struct it *));
715 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
716 static void extend_face_to_end_of_line P_ ((struct it *));
717 static int append_space P_ ((struct it *, int));
718 static int make_cursor_line_fully_visible P_ ((struct window *));
719 static int try_scrolling P_ ((Lisp_Object, int, int, int, int));
720 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
721 static int trailing_whitespace_p P_ ((int));
722 static int message_log_check_duplicate P_ ((int, int, int, int));
723 static void push_it P_ ((struct it *));
724 static void pop_it P_ ((struct it *));
725 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
726 static void redisplay_internal P_ ((int));
727 static int echo_area_display P_ ((int));
728 static void redisplay_windows P_ ((Lisp_Object));
729 static void redisplay_window P_ ((Lisp_Object, int));
730 static void update_menu_bar P_ ((struct frame *, int));
731 static int try_window_reusing_current_matrix P_ ((struct window *));
732 static int try_window_id P_ ((struct window *));
733 static int display_line P_ ((struct it *));
734 static int display_mode_lines P_ ((struct window *));
735 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
736 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object));
737 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
738 static void display_menu_bar P_ ((struct window *));
739 static int display_count_lines P_ ((int, int, int, int, int *));
740 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
741 int, int, struct it *, int, int, int, int));
742 static void compute_line_metrics P_ ((struct it *));
743 static void run_redisplay_end_trigger_hook P_ ((struct it *));
744 static int get_overlay_strings P_ ((struct it *, int));
745 static void next_overlay_string P_ ((struct it *));
746 static void reseat P_ ((struct it *, struct text_pos, int));
747 static void reseat_1 P_ ((struct it *, struct text_pos, int));
748 static void back_to_previous_visible_line_start P_ ((struct it *));
749 static void reseat_at_previous_visible_line_start P_ ((struct it *));
750 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
751 static int next_element_from_display_vector P_ ((struct it *));
752 static int next_element_from_string P_ ((struct it *));
753 static int next_element_from_c_string P_ ((struct it *));
754 static int next_element_from_buffer P_ ((struct it *));
755 static int next_element_from_composition P_ ((struct it *));
756 static int next_element_from_image P_ ((struct it *));
757 static int next_element_from_stretch P_ ((struct it *));
758 static void load_overlay_strings P_ ((struct it *, int));
759 static int init_from_display_pos P_ ((struct it *, struct window *,
760 struct display_pos *));
761 static void reseat_to_string P_ ((struct it *, unsigned char *,
762 Lisp_Object, int, int, int, int));
763 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
764 int, int, int));
765 void move_it_vertically_backward P_ ((struct it *, int));
766 static void init_to_row_start P_ ((struct it *, struct window *,
767 struct glyph_row *));
768 static int init_to_row_end P_ ((struct it *, struct window *,
769 struct glyph_row *));
770 static void back_to_previous_line_start P_ ((struct it *));
771 static int forward_to_next_line_start P_ ((struct it *, int *));
772 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
773 Lisp_Object, int));
774 static struct text_pos string_pos P_ ((int, Lisp_Object));
775 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
776 static int number_of_chars P_ ((unsigned char *, int));
777 static void compute_stop_pos P_ ((struct it *));
778 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
779 Lisp_Object));
780 static int face_before_or_after_it_pos P_ ((struct it *, int));
781 static int next_overlay_change P_ ((int));
782 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
783 Lisp_Object, struct text_pos *,
784 int));
785 static int underlying_face_id P_ ((struct it *));
786 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
787 struct window *));
788
789 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
790 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
791
792 #ifdef HAVE_WINDOW_SYSTEM
793
794 static void update_tool_bar P_ ((struct frame *, int));
795 static void build_desired_tool_bar_string P_ ((struct frame *f));
796 static int redisplay_tool_bar P_ ((struct frame *));
797 static void display_tool_bar_line P_ ((struct it *));
798
799 #endif /* HAVE_WINDOW_SYSTEM */
800
801 \f
802 /***********************************************************************
803 Window display dimensions
804 ***********************************************************************/
805
806 /* Return the window-relative maximum y + 1 for glyph rows displaying
807 text in window W. This is the height of W minus the height of a
808 mode line, if any. */
809
810 INLINE int
811 window_text_bottom_y (w)
812 struct window *w;
813 {
814 struct frame *f = XFRAME (w->frame);
815 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
816
817 if (WINDOW_WANTS_MODELINE_P (w))
818 height -= CURRENT_MODE_LINE_HEIGHT (w);
819 return height;
820 }
821
822
823 /* Return the pixel width of display area AREA of window W. AREA < 0
824 means return the total width of W, not including fringes to
825 the left and right of the window. */
826
827 INLINE int
828 window_box_width (w, area)
829 struct window *w;
830 int area;
831 {
832 struct frame *f = XFRAME (w->frame);
833 int width = XFASTINT (w->width);
834
835 if (!w->pseudo_window_p)
836 {
837 width -= FRAME_SCROLL_BAR_WIDTH (f) + FRAME_FRINGE_COLS (f);
838
839 if (area == TEXT_AREA)
840 {
841 if (INTEGERP (w->left_margin_width))
842 width -= XFASTINT (w->left_margin_width);
843 if (INTEGERP (w->right_margin_width))
844 width -= XFASTINT (w->right_margin_width);
845 }
846 else if (area == LEFT_MARGIN_AREA)
847 width = (INTEGERP (w->left_margin_width)
848 ? XFASTINT (w->left_margin_width) : 0);
849 else if (area == RIGHT_MARGIN_AREA)
850 width = (INTEGERP (w->right_margin_width)
851 ? XFASTINT (w->right_margin_width) : 0);
852 }
853
854 return width * CANON_X_UNIT (f);
855 }
856
857
858 /* Return the pixel height of the display area of window W, not
859 including mode lines of W, if any.. */
860
861 INLINE int
862 window_box_height (w)
863 struct window *w;
864 {
865 struct frame *f = XFRAME (w->frame);
866 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
867
868 xassert (height >= 0);
869
870 /* Note: the code below that determines the mode-line/header-line
871 height is essentially the same as that contained in the macro
872 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
873 the appropriate glyph row has its `mode_line_p' flag set,
874 and if it doesn't, uses estimate_mode_line_height instead. */
875
876 if (WINDOW_WANTS_MODELINE_P (w))
877 {
878 struct glyph_row *ml_row
879 = (w->current_matrix && w->current_matrix->rows
880 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
881 : 0);
882 if (ml_row && ml_row->mode_line_p)
883 height -= ml_row->height;
884 else
885 height -= estimate_mode_line_height (f, MODE_LINE_FACE_ID);
886 }
887
888 if (WINDOW_WANTS_HEADER_LINE_P (w))
889 {
890 struct glyph_row *hl_row
891 = (w->current_matrix && w->current_matrix->rows
892 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
893 : 0);
894 if (hl_row && hl_row->mode_line_p)
895 height -= hl_row->height;
896 else
897 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
898 }
899
900 return height;
901 }
902
903
904 /* Return the frame-relative coordinate of the left edge of display
905 area AREA of window W. AREA < 0 means return the left edge of the
906 whole window, to the right of the left fringe of W. */
907
908 INLINE int
909 window_box_left (w, area)
910 struct window *w;
911 int area;
912 {
913 struct frame *f = XFRAME (w->frame);
914 int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
915
916 if (!w->pseudo_window_p)
917 {
918 x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
919 + FRAME_LEFT_FRINGE_WIDTH (f));
920
921 if (area == TEXT_AREA)
922 x += window_box_width (w, LEFT_MARGIN_AREA);
923 else if (area == RIGHT_MARGIN_AREA)
924 x += (window_box_width (w, LEFT_MARGIN_AREA)
925 + window_box_width (w, TEXT_AREA));
926 }
927
928 return x;
929 }
930
931
932 /* Return the frame-relative coordinate of the right edge of display
933 area AREA of window W. AREA < 0 means return the left edge of the
934 whole window, to the left of the right fringe of W. */
935
936 INLINE int
937 window_box_right (w, area)
938 struct window *w;
939 int area;
940 {
941 return window_box_left (w, area) + window_box_width (w, area);
942 }
943
944
945 /* Get the bounding box of the display area AREA of window W, without
946 mode lines, in frame-relative coordinates. AREA < 0 means the
947 whole window, not including the left and right fringes of
948 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
949 coordinates of the upper-left corner of the box. Return in
950 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
951
952 INLINE void
953 window_box (w, area, box_x, box_y, box_width, box_height)
954 struct window *w;
955 int area;
956 int *box_x, *box_y, *box_width, *box_height;
957 {
958 struct frame *f = XFRAME (w->frame);
959
960 *box_width = window_box_width (w, area);
961 *box_height = window_box_height (w);
962 *box_x = window_box_left (w, area);
963 *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
964 + XFASTINT (w->top) * CANON_Y_UNIT (f));
965 if (WINDOW_WANTS_HEADER_LINE_P (w))
966 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
967 }
968
969
970 /* Get the bounding box of the display area AREA of window W, without
971 mode lines. AREA < 0 means the whole window, not including the
972 left and right fringe of the window. Return in *TOP_LEFT_X
973 and TOP_LEFT_Y the frame-relative pixel coordinates of the
974 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
975 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
976 box. */
977
978 INLINE void
979 window_box_edges (w, area, top_left_x, top_left_y,
980 bottom_right_x, bottom_right_y)
981 struct window *w;
982 int area;
983 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
984 {
985 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
986 bottom_right_y);
987 *bottom_right_x += *top_left_x;
988 *bottom_right_y += *top_left_y;
989 }
990
991
992 \f
993 /***********************************************************************
994 Utilities
995 ***********************************************************************/
996
997 /* Return the bottom y-position of the line the iterator IT is in.
998 This can modify IT's settings. */
999
1000 int
1001 line_bottom_y (it)
1002 struct it *it;
1003 {
1004 int line_height = it->max_ascent + it->max_descent;
1005 int line_top_y = it->current_y;
1006
1007 if (line_height == 0)
1008 {
1009 if (last_height)
1010 line_height = last_height;
1011 else if (IT_CHARPOS (*it) < ZV)
1012 {
1013 move_it_by_lines (it, 1, 1);
1014 line_height = (it->max_ascent || it->max_descent
1015 ? it->max_ascent + it->max_descent
1016 : last_height);
1017 }
1018 else
1019 {
1020 struct glyph_row *row = it->glyph_row;
1021
1022 /* Use the default character height. */
1023 it->glyph_row = NULL;
1024 it->what = IT_CHARACTER;
1025 it->c = ' ';
1026 it->len = 1;
1027 PRODUCE_GLYPHS (it);
1028 line_height = it->ascent + it->descent;
1029 it->glyph_row = row;
1030 }
1031 }
1032
1033 return line_top_y + line_height;
1034 }
1035
1036
1037 /* Return 1 if position CHARPOS is visible in window W. Set *FULLY to
1038 1 if POS is visible and the line containing POS is fully visible.
1039 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1040 and header-lines heights. */
1041
1042 int
1043 pos_visible_p (w, charpos, fully, exact_mode_line_heights_p)
1044 struct window *w;
1045 int charpos, *fully, exact_mode_line_heights_p;
1046 {
1047 struct it it;
1048 struct text_pos top;
1049 int visible_p;
1050 struct buffer *old_buffer = NULL;
1051
1052 if (XBUFFER (w->buffer) != current_buffer)
1053 {
1054 old_buffer = current_buffer;
1055 set_buffer_internal_1 (XBUFFER (w->buffer));
1056 }
1057
1058 *fully = visible_p = 0;
1059 SET_TEXT_POS_FROM_MARKER (top, w->start);
1060
1061 /* Compute exact mode line heights, if requested. */
1062 if (exact_mode_line_heights_p)
1063 {
1064 if (WINDOW_WANTS_MODELINE_P (w))
1065 current_mode_line_height
1066 = display_mode_line (w, MODE_LINE_FACE_ID,
1067 current_buffer->mode_line_format);
1068
1069 if (WINDOW_WANTS_HEADER_LINE_P (w))
1070 current_header_line_height
1071 = display_mode_line (w, HEADER_LINE_FACE_ID,
1072 current_buffer->header_line_format);
1073 }
1074
1075 start_display (&it, w, top);
1076 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
1077 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
1078
1079 /* Note that we may overshoot because of invisible text. */
1080 if (IT_CHARPOS (it) >= charpos)
1081 {
1082 int top_y = it.current_y;
1083 int bottom_y = line_bottom_y (&it);
1084 int window_top_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
1085
1086 if (top_y < window_top_y)
1087 visible_p = bottom_y > window_top_y;
1088 else if (top_y < it.last_visible_y)
1089 {
1090 visible_p = 1;
1091 *fully = bottom_y <= it.last_visible_y;
1092 }
1093 }
1094 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1095 {
1096 move_it_by_lines (&it, 1, 0);
1097 if (charpos < IT_CHARPOS (it))
1098 {
1099 visible_p = 1;
1100 *fully = 0;
1101 }
1102 }
1103
1104 if (old_buffer)
1105 set_buffer_internal_1 (old_buffer);
1106
1107 current_header_line_height = current_mode_line_height = -1;
1108 return visible_p;
1109 }
1110
1111
1112 /* Return the next character from STR which is MAXLEN bytes long.
1113 Return in *LEN the length of the character. This is like
1114 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1115 we find one, we return a `?', but with the length of the invalid
1116 character. */
1117
1118 static INLINE int
1119 string_char_and_length (str, maxlen, len)
1120 unsigned char *str;
1121 int maxlen, *len;
1122 {
1123 int c;
1124
1125 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1126 if (!CHAR_VALID_P (c, 1))
1127 /* We may not change the length here because other places in Emacs
1128 don't use this function, i.e. they silently accept invalid
1129 characters. */
1130 c = '?';
1131
1132 return c;
1133 }
1134
1135
1136
1137 /* Given a position POS containing a valid character and byte position
1138 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1139
1140 static struct text_pos
1141 string_pos_nchars_ahead (pos, string, nchars)
1142 struct text_pos pos;
1143 Lisp_Object string;
1144 int nchars;
1145 {
1146 xassert (STRINGP (string) && nchars >= 0);
1147
1148 if (STRING_MULTIBYTE (string))
1149 {
1150 int rest = STRING_BYTES (XSTRING (string)) - BYTEPOS (pos);
1151 unsigned char *p = XSTRING (string)->data + BYTEPOS (pos);
1152 int len;
1153
1154 while (nchars--)
1155 {
1156 string_char_and_length (p, rest, &len);
1157 p += len, rest -= len;
1158 xassert (rest >= 0);
1159 CHARPOS (pos) += 1;
1160 BYTEPOS (pos) += len;
1161 }
1162 }
1163 else
1164 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1165
1166 return pos;
1167 }
1168
1169
1170 /* Value is the text position, i.e. character and byte position,
1171 for character position CHARPOS in STRING. */
1172
1173 static INLINE struct text_pos
1174 string_pos (charpos, string)
1175 int charpos;
1176 Lisp_Object string;
1177 {
1178 struct text_pos pos;
1179 xassert (STRINGP (string));
1180 xassert (charpos >= 0);
1181 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1182 return pos;
1183 }
1184
1185
1186 /* Value is a text position, i.e. character and byte position, for
1187 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1188 means recognize multibyte characters. */
1189
1190 static struct text_pos
1191 c_string_pos (charpos, s, multibyte_p)
1192 int charpos;
1193 unsigned char *s;
1194 int multibyte_p;
1195 {
1196 struct text_pos pos;
1197
1198 xassert (s != NULL);
1199 xassert (charpos >= 0);
1200
1201 if (multibyte_p)
1202 {
1203 int rest = strlen (s), len;
1204
1205 SET_TEXT_POS (pos, 0, 0);
1206 while (charpos--)
1207 {
1208 string_char_and_length (s, rest, &len);
1209 s += len, rest -= len;
1210 xassert (rest >= 0);
1211 CHARPOS (pos) += 1;
1212 BYTEPOS (pos) += len;
1213 }
1214 }
1215 else
1216 SET_TEXT_POS (pos, charpos, charpos);
1217
1218 return pos;
1219 }
1220
1221
1222 /* Value is the number of characters in C string S. MULTIBYTE_P
1223 non-zero means recognize multibyte characters. */
1224
1225 static int
1226 number_of_chars (s, multibyte_p)
1227 unsigned char *s;
1228 int multibyte_p;
1229 {
1230 int nchars;
1231
1232 if (multibyte_p)
1233 {
1234 int rest = strlen (s), len;
1235 unsigned char *p = (unsigned char *) s;
1236
1237 for (nchars = 0; rest > 0; ++nchars)
1238 {
1239 string_char_and_length (p, rest, &len);
1240 rest -= len, p += len;
1241 }
1242 }
1243 else
1244 nchars = strlen (s);
1245
1246 return nchars;
1247 }
1248
1249
1250 /* Compute byte position NEWPOS->bytepos corresponding to
1251 NEWPOS->charpos. POS is a known position in string STRING.
1252 NEWPOS->charpos must be >= POS.charpos. */
1253
1254 static void
1255 compute_string_pos (newpos, pos, string)
1256 struct text_pos *newpos, pos;
1257 Lisp_Object string;
1258 {
1259 xassert (STRINGP (string));
1260 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1261
1262 if (STRING_MULTIBYTE (string))
1263 *newpos = string_pos_nchars_ahead (pos, string,
1264 CHARPOS (*newpos) - CHARPOS (pos));
1265 else
1266 BYTEPOS (*newpos) = CHARPOS (*newpos);
1267 }
1268
1269
1270 \f
1271 /***********************************************************************
1272 Lisp form evaluation
1273 ***********************************************************************/
1274
1275 /* Error handler for safe_eval and safe_call. */
1276
1277 static Lisp_Object
1278 safe_eval_handler (arg)
1279 Lisp_Object arg;
1280 {
1281 add_to_log ("Error during redisplay: %s", arg, Qnil);
1282 return Qnil;
1283 }
1284
1285
1286 /* Evaluate SEXPR and return the result, or nil if something went
1287 wrong. Prevent redisplay during the evaluation. */
1288
1289 Lisp_Object
1290 safe_eval (sexpr)
1291 Lisp_Object sexpr;
1292 {
1293 Lisp_Object val;
1294
1295 if (inhibit_eval_during_redisplay)
1296 val = Qnil;
1297 else
1298 {
1299 int count = BINDING_STACK_SIZE ();
1300 struct gcpro gcpro1;
1301
1302 GCPRO1 (sexpr);
1303 specbind (Qinhibit_redisplay, Qt);
1304 val = internal_condition_case_1 (Feval, sexpr, Qerror,
1305 safe_eval_handler);
1306 UNGCPRO;
1307 val = unbind_to (count, val);
1308 }
1309
1310 return val;
1311 }
1312
1313
1314 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1315 Return the result, or nil if something went wrong. Prevent
1316 redisplay during the evaluation. */
1317
1318 Lisp_Object
1319 safe_call (nargs, args)
1320 int nargs;
1321 Lisp_Object *args;
1322 {
1323 Lisp_Object val;
1324
1325 if (inhibit_eval_during_redisplay)
1326 val = Qnil;
1327 else
1328 {
1329 int count = BINDING_STACK_SIZE ();
1330 struct gcpro gcpro1;
1331
1332 GCPRO1 (args[0]);
1333 gcpro1.nvars = nargs;
1334 specbind (Qinhibit_redisplay, Qt);
1335 val = internal_condition_case_2 (Ffuncall, nargs, args, Qerror,
1336 safe_eval_handler);
1337 UNGCPRO;
1338 val = unbind_to (count, val);
1339 }
1340
1341 return val;
1342 }
1343
1344
1345 /* Call function FN with one argument ARG.
1346 Return the result, or nil if something went wrong. */
1347
1348 Lisp_Object
1349 safe_call1 (fn, arg)
1350 Lisp_Object fn, arg;
1351 {
1352 Lisp_Object args[2];
1353 args[0] = fn;
1354 args[1] = arg;
1355 return safe_call (2, args);
1356 }
1357
1358
1359 \f
1360 /***********************************************************************
1361 Debugging
1362 ***********************************************************************/
1363
1364 #if 0
1365
1366 /* Define CHECK_IT to perform sanity checks on iterators.
1367 This is for debugging. It is too slow to do unconditionally. */
1368
1369 static void
1370 check_it (it)
1371 struct it *it;
1372 {
1373 if (it->method == next_element_from_string)
1374 {
1375 xassert (STRINGP (it->string));
1376 xassert (IT_STRING_CHARPOS (*it) >= 0);
1377 }
1378 else if (it->method == next_element_from_buffer)
1379 {
1380 /* Check that character and byte positions agree. */
1381 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1382 }
1383
1384 if (it->dpvec)
1385 xassert (it->current.dpvec_index >= 0);
1386 else
1387 xassert (it->current.dpvec_index < 0);
1388 }
1389
1390 #define CHECK_IT(IT) check_it ((IT))
1391
1392 #else /* not 0 */
1393
1394 #define CHECK_IT(IT) (void) 0
1395
1396 #endif /* not 0 */
1397
1398
1399 #if GLYPH_DEBUG
1400
1401 /* Check that the window end of window W is what we expect it
1402 to be---the last row in the current matrix displaying text. */
1403
1404 static void
1405 check_window_end (w)
1406 struct window *w;
1407 {
1408 if (!MINI_WINDOW_P (w)
1409 && !NILP (w->window_end_valid))
1410 {
1411 struct glyph_row *row;
1412 xassert ((row = MATRIX_ROW (w->current_matrix,
1413 XFASTINT (w->window_end_vpos)),
1414 !row->enabled_p
1415 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1416 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1417 }
1418 }
1419
1420 #define CHECK_WINDOW_END(W) check_window_end ((W))
1421
1422 #else /* not GLYPH_DEBUG */
1423
1424 #define CHECK_WINDOW_END(W) (void) 0
1425
1426 #endif /* not GLYPH_DEBUG */
1427
1428
1429 \f
1430 /***********************************************************************
1431 Iterator initialization
1432 ***********************************************************************/
1433
1434 /* Initialize IT for displaying current_buffer in window W, starting
1435 at character position CHARPOS. CHARPOS < 0 means that no buffer
1436 position is specified which is useful when the iterator is assigned
1437 a position later. BYTEPOS is the byte position corresponding to
1438 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
1439
1440 If ROW is not null, calls to produce_glyphs with IT as parameter
1441 will produce glyphs in that row.
1442
1443 BASE_FACE_ID is the id of a base face to use. It must be one of
1444 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID or
1445 HEADER_LINE_FACE_ID for displaying mode lines, or TOOL_BAR_FACE_ID for
1446 displaying the tool-bar.
1447
1448 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID or
1449 HEADER_LINE_FACE_ID, the iterator will be initialized to use the
1450 corresponding mode line glyph row of the desired matrix of W. */
1451
1452 void
1453 init_iterator (it, w, charpos, bytepos, row, base_face_id)
1454 struct it *it;
1455 struct window *w;
1456 int charpos, bytepos;
1457 struct glyph_row *row;
1458 enum face_id base_face_id;
1459 {
1460 int highlight_region_p;
1461
1462 /* Some precondition checks. */
1463 xassert (w != NULL && it != NULL);
1464 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
1465 && charpos <= ZV));
1466
1467 /* If face attributes have been changed since the last redisplay,
1468 free realized faces now because they depend on face definitions
1469 that might have changed. */
1470 if (face_change_count)
1471 {
1472 face_change_count = 0;
1473 free_all_realized_faces (Qnil);
1474 }
1475
1476 /* Use one of the mode line rows of W's desired matrix if
1477 appropriate. */
1478 if (row == NULL)
1479 {
1480 if (base_face_id == MODE_LINE_FACE_ID)
1481 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
1482 else if (base_face_id == HEADER_LINE_FACE_ID)
1483 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
1484 }
1485
1486 /* Clear IT. */
1487 bzero (it, sizeof *it);
1488 it->current.overlay_string_index = -1;
1489 it->current.dpvec_index = -1;
1490 it->base_face_id = base_face_id;
1491
1492 /* The window in which we iterate over current_buffer: */
1493 XSETWINDOW (it->window, w);
1494 it->w = w;
1495 it->f = XFRAME (w->frame);
1496
1497 /* Extra space between lines (on window systems only). */
1498 if (base_face_id == DEFAULT_FACE_ID
1499 && FRAME_WINDOW_P (it->f))
1500 {
1501 if (NATNUMP (current_buffer->extra_line_spacing))
1502 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
1503 else if (it->f->extra_line_spacing > 0)
1504 it->extra_line_spacing = it->f->extra_line_spacing;
1505 }
1506
1507 /* If realized faces have been removed, e.g. because of face
1508 attribute changes of named faces, recompute them. When running
1509 in batch mode, the face cache of Vterminal_frame is null. If
1510 we happen to get called, make a dummy face cache. */
1511 if (
1512 #ifndef WINDOWSNT
1513 noninteractive &&
1514 #endif
1515 FRAME_FACE_CACHE (it->f) == NULL)
1516 init_frame_faces (it->f);
1517 if (FRAME_FACE_CACHE (it->f)->used == 0)
1518 recompute_basic_faces (it->f);
1519
1520 /* Current value of the `space-width', and 'height' properties. */
1521 it->space_width = Qnil;
1522 it->font_height = Qnil;
1523
1524 /* Are control characters displayed as `^C'? */
1525 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
1526
1527 /* -1 means everything between a CR and the following line end
1528 is invisible. >0 means lines indented more than this value are
1529 invisible. */
1530 it->selective = (INTEGERP (current_buffer->selective_display)
1531 ? XFASTINT (current_buffer->selective_display)
1532 : (!NILP (current_buffer->selective_display)
1533 ? -1 : 0));
1534 it->selective_display_ellipsis_p
1535 = !NILP (current_buffer->selective_display_ellipses);
1536
1537 /* Display table to use. */
1538 it->dp = window_display_table (w);
1539
1540 /* Are multibyte characters enabled in current_buffer? */
1541 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1542
1543 /* Non-zero if we should highlight the region. */
1544 highlight_region_p
1545 = (!NILP (Vtransient_mark_mode)
1546 && !NILP (current_buffer->mark_active)
1547 && XMARKER (current_buffer->mark)->buffer != 0);
1548
1549 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
1550 start and end of a visible region in window IT->w. Set both to
1551 -1 to indicate no region. */
1552 if (highlight_region_p
1553 /* Maybe highlight only in selected window. */
1554 && (/* Either show region everywhere. */
1555 highlight_nonselected_windows
1556 /* Or show region in the selected window. */
1557 || w == XWINDOW (selected_window)
1558 /* Or show the region if we are in the mini-buffer and W is
1559 the window the mini-buffer refers to. */
1560 || (MINI_WINDOW_P (XWINDOW (selected_window))
1561 && WINDOWP (Vminibuf_scroll_window)
1562 && w == XWINDOW (Vminibuf_scroll_window))))
1563 {
1564 int charpos = marker_position (current_buffer->mark);
1565 it->region_beg_charpos = min (PT, charpos);
1566 it->region_end_charpos = max (PT, charpos);
1567 }
1568 else
1569 it->region_beg_charpos = it->region_end_charpos = -1;
1570
1571 /* Get the position at which the redisplay_end_trigger hook should
1572 be run, if it is to be run at all. */
1573 if (MARKERP (w->redisplay_end_trigger)
1574 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
1575 it->redisplay_end_trigger_charpos
1576 = marker_position (w->redisplay_end_trigger);
1577 else if (INTEGERP (w->redisplay_end_trigger))
1578 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
1579
1580 /* Correct bogus values of tab_width. */
1581 it->tab_width = XINT (current_buffer->tab_width);
1582 if (it->tab_width <= 0 || it->tab_width > 1000)
1583 it->tab_width = 8;
1584
1585 /* Are lines in the display truncated? */
1586 it->truncate_lines_p
1587 = (base_face_id != DEFAULT_FACE_ID
1588 || XINT (it->w->hscroll)
1589 || (truncate_partial_width_windows
1590 && !WINDOW_FULL_WIDTH_P (it->w))
1591 || !NILP (current_buffer->truncate_lines));
1592
1593 /* Get dimensions of truncation and continuation glyphs. These are
1594 displayed as fringe bitmaps under X, so we don't need them for such
1595 frames. */
1596 if (!FRAME_WINDOW_P (it->f))
1597 {
1598 if (it->truncate_lines_p)
1599 {
1600 /* We will need the truncation glyph. */
1601 xassert (it->glyph_row == NULL);
1602 produce_special_glyphs (it, IT_TRUNCATION);
1603 it->truncation_pixel_width = it->pixel_width;
1604 }
1605 else
1606 {
1607 /* We will need the continuation glyph. */
1608 xassert (it->glyph_row == NULL);
1609 produce_special_glyphs (it, IT_CONTINUATION);
1610 it->continuation_pixel_width = it->pixel_width;
1611 }
1612
1613 /* Reset these values to zero becaue the produce_special_glyphs
1614 above has changed them. */
1615 it->pixel_width = it->ascent = it->descent = 0;
1616 it->phys_ascent = it->phys_descent = 0;
1617 }
1618
1619 /* Set this after getting the dimensions of truncation and
1620 continuation glyphs, so that we don't produce glyphs when calling
1621 produce_special_glyphs, above. */
1622 it->glyph_row = row;
1623 it->area = TEXT_AREA;
1624
1625 /* Get the dimensions of the display area. The display area
1626 consists of the visible window area plus a horizontally scrolled
1627 part to the left of the window. All x-values are relative to the
1628 start of this total display area. */
1629 if (base_face_id != DEFAULT_FACE_ID)
1630 {
1631 /* Mode lines, menu bar in terminal frames. */
1632 it->first_visible_x = 0;
1633 it->last_visible_x = XFASTINT (w->width) * CANON_X_UNIT (it->f);
1634 }
1635 else
1636 {
1637 it->first_visible_x
1638 = XFASTINT (it->w->hscroll) * CANON_X_UNIT (it->f);
1639 it->last_visible_x = (it->first_visible_x
1640 + window_box_width (w, TEXT_AREA));
1641
1642 /* If we truncate lines, leave room for the truncator glyph(s) at
1643 the right margin. Otherwise, leave room for the continuation
1644 glyph(s). Truncation and continuation glyphs are not inserted
1645 for window-based redisplay. */
1646 if (!FRAME_WINDOW_P (it->f))
1647 {
1648 if (it->truncate_lines_p)
1649 it->last_visible_x -= it->truncation_pixel_width;
1650 else
1651 it->last_visible_x -= it->continuation_pixel_width;
1652 }
1653
1654 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
1655 it->current_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w) + w->vscroll;
1656 }
1657
1658 /* Leave room for a border glyph. */
1659 if (!FRAME_WINDOW_P (it->f)
1660 && !WINDOW_RIGHTMOST_P (it->w))
1661 it->last_visible_x -= 1;
1662
1663 it->last_visible_y = window_text_bottom_y (w);
1664
1665 /* For mode lines and alike, arrange for the first glyph having a
1666 left box line if the face specifies a box. */
1667 if (base_face_id != DEFAULT_FACE_ID)
1668 {
1669 struct face *face;
1670
1671 it->face_id = base_face_id;
1672
1673 /* If we have a boxed mode line, make the first character appear
1674 with a left box line. */
1675 face = FACE_FROM_ID (it->f, base_face_id);
1676 if (face->box != FACE_NO_BOX)
1677 it->start_of_box_run_p = 1;
1678 }
1679
1680 /* If a buffer position was specified, set the iterator there,
1681 getting overlays and face properties from that position. */
1682 if (charpos >= BUF_BEG (current_buffer))
1683 {
1684 it->end_charpos = ZV;
1685 it->face_id = -1;
1686 IT_CHARPOS (*it) = charpos;
1687
1688 /* Compute byte position if not specified. */
1689 if (bytepos < charpos)
1690 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
1691 else
1692 IT_BYTEPOS (*it) = bytepos;
1693
1694 /* Compute faces etc. */
1695 reseat (it, it->current.pos, 1);
1696 }
1697
1698 CHECK_IT (it);
1699 }
1700
1701
1702 /* Initialize IT for the display of window W with window start POS. */
1703
1704 void
1705 start_display (it, w, pos)
1706 struct it *it;
1707 struct window *w;
1708 struct text_pos pos;
1709 {
1710 struct glyph_row *row;
1711 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
1712
1713 row = w->desired_matrix->rows + first_vpos;
1714 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
1715
1716 if (!it->truncate_lines_p)
1717 {
1718 int start_at_line_beg_p;
1719 int first_y = it->current_y;
1720
1721 /* If window start is not at a line start, skip forward to POS to
1722 get the correct continuation lines width. */
1723 start_at_line_beg_p = (CHARPOS (pos) == BEGV
1724 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
1725 if (!start_at_line_beg_p)
1726 {
1727 reseat_at_previous_visible_line_start (it);
1728 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
1729
1730 /* If lines are continued, this line may end in the middle
1731 of a multi-glyph character (e.g. a control character
1732 displayed as \003, or in the middle of an overlay
1733 string). In this case move_it_to above will not have
1734 taken us to the start of the continuation line but to the
1735 end of the continued line. */
1736 if (it->current_x > 0)
1737 {
1738 if (it->current.dpvec_index >= 0
1739 || it->current.overlay_string_index >= 0)
1740 {
1741 set_iterator_to_next (it, 1);
1742 move_it_in_display_line_to (it, -1, -1, 0);
1743 }
1744
1745 it->continuation_lines_width += it->current_x;
1746 }
1747
1748 /* We're starting a new display line, not affected by the
1749 height of the continued line, so clear the appropriate
1750 fields in the iterator structure. */
1751 it->max_ascent = it->max_descent = 0;
1752 it->max_phys_ascent = it->max_phys_descent = 0;
1753
1754 it->current_y = first_y;
1755 it->vpos = 0;
1756 it->current_x = it->hpos = 0;
1757 }
1758 }
1759
1760 #if 0 /* Don't assert the following because start_display is sometimes
1761 called intentionally with a window start that is not at a
1762 line start. Please leave this code in as a comment. */
1763
1764 /* Window start should be on a line start, now. */
1765 xassert (it->continuation_lines_width
1766 || IT_CHARPOS (it) == BEGV
1767 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
1768 #endif /* 0 */
1769 }
1770
1771
1772 /* Return 1 if POS is a position in ellipses displayed for invisible
1773 text. W is the window we display, for text property lookup. */
1774
1775 static int
1776 in_ellipses_for_invisible_text_p (pos, w)
1777 struct display_pos *pos;
1778 struct window *w;
1779 {
1780 Lisp_Object prop, window;
1781 int ellipses_p = 0;
1782 int charpos = CHARPOS (pos->pos);
1783
1784 /* If POS specifies a position in a display vector, this might
1785 be for an ellipsis displayed for invisible text. We won't
1786 get the iterator set up for delivering that ellipsis unless
1787 we make sure that it gets aware of the invisible text. */
1788 if (pos->dpvec_index >= 0
1789 && pos->overlay_string_index < 0
1790 && CHARPOS (pos->string_pos) < 0
1791 && charpos > BEGV
1792 && (XSETWINDOW (window, w),
1793 prop = Fget_char_property (make_number (charpos),
1794 Qinvisible, window),
1795 !TEXT_PROP_MEANS_INVISIBLE (prop)))
1796 {
1797 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
1798 window);
1799 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
1800 }
1801
1802 return ellipses_p;
1803 }
1804
1805
1806 /* Initialize IT for stepping through current_buffer in window W,
1807 starting at position POS that includes overlay string and display
1808 vector/ control character translation position information. Value
1809 is zero if there are overlay strings with newlines at POS. */
1810
1811 static int
1812 init_from_display_pos (it, w, pos)
1813 struct it *it;
1814 struct window *w;
1815 struct display_pos *pos;
1816 {
1817 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
1818 int i, overlay_strings_with_newlines = 0;
1819
1820 /* If POS specifies a position in a display vector, this might
1821 be for an ellipsis displayed for invisible text. We won't
1822 get the iterator set up for delivering that ellipsis unless
1823 we make sure that it gets aware of the invisible text. */
1824 if (in_ellipses_for_invisible_text_p (pos, w))
1825 {
1826 --charpos;
1827 bytepos = 0;
1828 }
1829
1830 /* Keep in mind: the call to reseat in init_iterator skips invisible
1831 text, so we might end up at a position different from POS. This
1832 is only a problem when POS is a row start after a newline and an
1833 overlay starts there with an after-string, and the overlay has an
1834 invisible property. Since we don't skip invisible text in
1835 display_line and elsewhere immediately after consuming the
1836 newline before the row start, such a POS will not be in a string,
1837 but the call to init_iterator below will move us to the
1838 after-string. */
1839 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
1840
1841 for (i = 0; i < it->n_overlay_strings; ++i)
1842 {
1843 char *s = XSTRING (it->overlay_strings[i])->data;
1844 char *e = s + STRING_BYTES (XSTRING (it->overlay_strings[i]));
1845
1846 while (s < e && *s != '\n')
1847 ++s;
1848
1849 if (s < e)
1850 {
1851 overlay_strings_with_newlines = 1;
1852 break;
1853 }
1854 }
1855
1856 /* If position is within an overlay string, set up IT to the right
1857 overlay string. */
1858 if (pos->overlay_string_index >= 0)
1859 {
1860 int relative_index;
1861
1862 /* If the first overlay string happens to have a `display'
1863 property for an image, the iterator will be set up for that
1864 image, and we have to undo that setup first before we can
1865 correct the overlay string index. */
1866 if (it->method == next_element_from_image)
1867 pop_it (it);
1868
1869 /* We already have the first chunk of overlay strings in
1870 IT->overlay_strings. Load more until the one for
1871 pos->overlay_string_index is in IT->overlay_strings. */
1872 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
1873 {
1874 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
1875 it->current.overlay_string_index = 0;
1876 while (n--)
1877 {
1878 load_overlay_strings (it, 0);
1879 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
1880 }
1881 }
1882
1883 it->current.overlay_string_index = pos->overlay_string_index;
1884 relative_index = (it->current.overlay_string_index
1885 % OVERLAY_STRING_CHUNK_SIZE);
1886 it->string = it->overlay_strings[relative_index];
1887 xassert (STRINGP (it->string));
1888 it->current.string_pos = pos->string_pos;
1889 it->method = next_element_from_string;
1890 }
1891
1892 #if 0 /* This is bogus because POS not having an overlay string
1893 position does not mean it's after the string. Example: A
1894 line starting with a before-string and initialization of IT
1895 to the previous row's end position. */
1896 else if (it->current.overlay_string_index >= 0)
1897 {
1898 /* If POS says we're already after an overlay string ending at
1899 POS, make sure to pop the iterator because it will be in
1900 front of that overlay string. When POS is ZV, we've thereby
1901 also ``processed'' overlay strings at ZV. */
1902 while (it->sp)
1903 pop_it (it);
1904 it->current.overlay_string_index = -1;
1905 it->method = next_element_from_buffer;
1906 if (CHARPOS (pos->pos) == ZV)
1907 it->overlay_strings_at_end_processed_p = 1;
1908 }
1909 #endif /* 0 */
1910
1911 if (CHARPOS (pos->string_pos) >= 0)
1912 {
1913 /* Recorded position is not in an overlay string, but in another
1914 string. This can only be a string from a `display' property.
1915 IT should already be filled with that string. */
1916 it->current.string_pos = pos->string_pos;
1917 xassert (STRINGP (it->string));
1918 }
1919
1920 /* Restore position in display vector translations, control
1921 character translations or ellipses. */
1922 if (pos->dpvec_index >= 0)
1923 {
1924 if (it->dpvec == NULL)
1925 get_next_display_element (it);
1926 xassert (it->dpvec && it->current.dpvec_index == 0);
1927 it->current.dpvec_index = pos->dpvec_index;
1928 }
1929
1930 CHECK_IT (it);
1931 return !overlay_strings_with_newlines;
1932 }
1933
1934
1935 /* Initialize IT for stepping through current_buffer in window W
1936 starting at ROW->start. */
1937
1938 static void
1939 init_to_row_start (it, w, row)
1940 struct it *it;
1941 struct window *w;
1942 struct glyph_row *row;
1943 {
1944 init_from_display_pos (it, w, &row->start);
1945 it->continuation_lines_width = row->continuation_lines_width;
1946 CHECK_IT (it);
1947 }
1948
1949
1950 /* Initialize IT for stepping through current_buffer in window W
1951 starting in the line following ROW, i.e. starting at ROW->end.
1952 Value is zero if there are overlay strings with newlines at ROW's
1953 end position. */
1954
1955 static int
1956 init_to_row_end (it, w, row)
1957 struct it *it;
1958 struct window *w;
1959 struct glyph_row *row;
1960 {
1961 int success = 0;
1962
1963 if (init_from_display_pos (it, w, &row->end))
1964 {
1965 if (row->continued_p)
1966 it->continuation_lines_width
1967 = row->continuation_lines_width + row->pixel_width;
1968 CHECK_IT (it);
1969 success = 1;
1970 }
1971
1972 return success;
1973 }
1974
1975
1976
1977 \f
1978 /***********************************************************************
1979 Text properties
1980 ***********************************************************************/
1981
1982 /* Called when IT reaches IT->stop_charpos. Handle text property and
1983 overlay changes. Set IT->stop_charpos to the next position where
1984 to stop. */
1985
1986 static void
1987 handle_stop (it)
1988 struct it *it;
1989 {
1990 enum prop_handled handled;
1991 int handle_overlay_change_p = 1;
1992 struct props *p;
1993
1994 it->dpvec = NULL;
1995 it->current.dpvec_index = -1;
1996
1997 do
1998 {
1999 handled = HANDLED_NORMALLY;
2000
2001 /* Call text property handlers. */
2002 for (p = it_props; p->handler; ++p)
2003 {
2004 handled = p->handler (it);
2005
2006 if (handled == HANDLED_RECOMPUTE_PROPS)
2007 break;
2008 else if (handled == HANDLED_RETURN)
2009 return;
2010 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2011 handle_overlay_change_p = 0;
2012 }
2013
2014 if (handled != HANDLED_RECOMPUTE_PROPS)
2015 {
2016 /* Don't check for overlay strings below when set to deliver
2017 characters from a display vector. */
2018 if (it->method == next_element_from_display_vector)
2019 handle_overlay_change_p = 0;
2020
2021 /* Handle overlay changes. */
2022 if (handle_overlay_change_p)
2023 handled = handle_overlay_change (it);
2024
2025 /* Determine where to stop next. */
2026 if (handled == HANDLED_NORMALLY)
2027 compute_stop_pos (it);
2028 }
2029 }
2030 while (handled == HANDLED_RECOMPUTE_PROPS);
2031 }
2032
2033
2034 /* Compute IT->stop_charpos from text property and overlay change
2035 information for IT's current position. */
2036
2037 static void
2038 compute_stop_pos (it)
2039 struct it *it;
2040 {
2041 register INTERVAL iv, next_iv;
2042 Lisp_Object object, limit, position;
2043
2044 /* If nowhere else, stop at the end. */
2045 it->stop_charpos = it->end_charpos;
2046
2047 if (STRINGP (it->string))
2048 {
2049 /* Strings are usually short, so don't limit the search for
2050 properties. */
2051 object = it->string;
2052 limit = Qnil;
2053 position = make_number (IT_STRING_CHARPOS (*it));
2054 }
2055 else
2056 {
2057 int charpos;
2058
2059 /* If next overlay change is in front of the current stop pos
2060 (which is IT->end_charpos), stop there. Note: value of
2061 next_overlay_change is point-max if no overlay change
2062 follows. */
2063 charpos = next_overlay_change (IT_CHARPOS (*it));
2064 if (charpos < it->stop_charpos)
2065 it->stop_charpos = charpos;
2066
2067 /* If showing the region, we have to stop at the region
2068 start or end because the face might change there. */
2069 if (it->region_beg_charpos > 0)
2070 {
2071 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2072 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2073 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2074 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2075 }
2076
2077 /* Set up variables for computing the stop position from text
2078 property changes. */
2079 XSETBUFFER (object, current_buffer);
2080 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2081 position = make_number (IT_CHARPOS (*it));
2082
2083 }
2084
2085 /* Get the interval containing IT's position. Value is a null
2086 interval if there isn't such an interval. */
2087 iv = validate_interval_range (object, &position, &position, 0);
2088 if (!NULL_INTERVAL_P (iv))
2089 {
2090 Lisp_Object values_here[LAST_PROP_IDX];
2091 struct props *p;
2092
2093 /* Get properties here. */
2094 for (p = it_props; p->handler; ++p)
2095 values_here[p->idx] = textget (iv->plist, *p->name);
2096
2097 /* Look for an interval following iv that has different
2098 properties. */
2099 for (next_iv = next_interval (iv);
2100 (!NULL_INTERVAL_P (next_iv)
2101 && (NILP (limit)
2102 || XFASTINT (limit) > next_iv->position));
2103 next_iv = next_interval (next_iv))
2104 {
2105 for (p = it_props; p->handler; ++p)
2106 {
2107 Lisp_Object new_value;
2108
2109 new_value = textget (next_iv->plist, *p->name);
2110 if (!EQ (values_here[p->idx], new_value))
2111 break;
2112 }
2113
2114 if (p->handler)
2115 break;
2116 }
2117
2118 if (!NULL_INTERVAL_P (next_iv))
2119 {
2120 if (INTEGERP (limit)
2121 && next_iv->position >= XFASTINT (limit))
2122 /* No text property change up to limit. */
2123 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2124 else
2125 /* Text properties change in next_iv. */
2126 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2127 }
2128 }
2129
2130 xassert (STRINGP (it->string)
2131 || (it->stop_charpos >= BEGV
2132 && it->stop_charpos >= IT_CHARPOS (*it)));
2133 }
2134
2135
2136 /* Return the position of the next overlay change after POS in
2137 current_buffer. Value is point-max if no overlay change
2138 follows. This is like `next-overlay-change' but doesn't use
2139 xmalloc. */
2140
2141 static int
2142 next_overlay_change (pos)
2143 int pos;
2144 {
2145 int noverlays;
2146 int endpos;
2147 Lisp_Object *overlays;
2148 int len;
2149 int i;
2150
2151 /* Get all overlays at the given position. */
2152 len = 10;
2153 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2154 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2155 if (noverlays > len)
2156 {
2157 len = noverlays;
2158 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2159 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2160 }
2161
2162 /* If any of these overlays ends before endpos,
2163 use its ending point instead. */
2164 for (i = 0; i < noverlays; ++i)
2165 {
2166 Lisp_Object oend;
2167 int oendpos;
2168
2169 oend = OVERLAY_END (overlays[i]);
2170 oendpos = OVERLAY_POSITION (oend);
2171 endpos = min (endpos, oendpos);
2172 }
2173
2174 return endpos;
2175 }
2176
2177
2178 \f
2179 /***********************************************************************
2180 Fontification
2181 ***********************************************************************/
2182
2183 /* Handle changes in the `fontified' property of the current buffer by
2184 calling hook functions from Qfontification_functions to fontify
2185 regions of text. */
2186
2187 static enum prop_handled
2188 handle_fontified_prop (it)
2189 struct it *it;
2190 {
2191 Lisp_Object prop, pos;
2192 enum prop_handled handled = HANDLED_NORMALLY;
2193
2194 /* Get the value of the `fontified' property at IT's current buffer
2195 position. (The `fontified' property doesn't have a special
2196 meaning in strings.) If the value is nil, call functions from
2197 Qfontification_functions. */
2198 if (!STRINGP (it->string)
2199 && it->s == NULL
2200 && !NILP (Vfontification_functions)
2201 && !NILP (Vrun_hooks)
2202 && (pos = make_number (IT_CHARPOS (*it)),
2203 prop = Fget_char_property (pos, Qfontified, Qnil),
2204 NILP (prop)))
2205 {
2206 int count = BINDING_STACK_SIZE ();
2207 Lisp_Object val;
2208
2209 val = Vfontification_functions;
2210 specbind (Qfontification_functions, Qnil);
2211
2212 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2213 safe_call1 (val, pos);
2214 else
2215 {
2216 Lisp_Object globals, fn;
2217 struct gcpro gcpro1, gcpro2;
2218
2219 globals = Qnil;
2220 GCPRO2 (val, globals);
2221
2222 for (; CONSP (val); val = XCDR (val))
2223 {
2224 fn = XCAR (val);
2225
2226 if (EQ (fn, Qt))
2227 {
2228 /* A value of t indicates this hook has a local
2229 binding; it means to run the global binding too.
2230 In a global value, t should not occur. If it
2231 does, we must ignore it to avoid an endless
2232 loop. */
2233 for (globals = Fdefault_value (Qfontification_functions);
2234 CONSP (globals);
2235 globals = XCDR (globals))
2236 {
2237 fn = XCAR (globals);
2238 if (!EQ (fn, Qt))
2239 safe_call1 (fn, pos);
2240 }
2241 }
2242 else
2243 safe_call1 (fn, pos);
2244 }
2245
2246 UNGCPRO;
2247 }
2248
2249 unbind_to (count, Qnil);
2250
2251 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2252 something. This avoids an endless loop if they failed to
2253 fontify the text for which reason ever. */
2254 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2255 handled = HANDLED_RECOMPUTE_PROPS;
2256 }
2257
2258 return handled;
2259 }
2260
2261
2262 \f
2263 /***********************************************************************
2264 Faces
2265 ***********************************************************************/
2266
2267 /* Set up iterator IT from face properties at its current position.
2268 Called from handle_stop. */
2269
2270 static enum prop_handled
2271 handle_face_prop (it)
2272 struct it *it;
2273 {
2274 int new_face_id, next_stop;
2275
2276 if (!STRINGP (it->string))
2277 {
2278 new_face_id
2279 = face_at_buffer_position (it->w,
2280 IT_CHARPOS (*it),
2281 it->region_beg_charpos,
2282 it->region_end_charpos,
2283 &next_stop,
2284 (IT_CHARPOS (*it)
2285 + TEXT_PROP_DISTANCE_LIMIT),
2286 0);
2287
2288 /* Is this a start of a run of characters with box face?
2289 Caveat: this can be called for a freshly initialized
2290 iterator; face_id is -1 is this case. We know that the new
2291 face will not change until limit, i.e. if the new face has a
2292 box, all characters up to limit will have one. But, as
2293 usual, we don't know whether limit is really the end. */
2294 if (new_face_id != it->face_id)
2295 {
2296 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2297
2298 /* If new face has a box but old face has not, this is
2299 the start of a run of characters with box, i.e. it has
2300 a shadow on the left side. The value of face_id of the
2301 iterator will be -1 if this is the initial call that gets
2302 the face. In this case, we have to look in front of IT's
2303 position and see whether there is a face != new_face_id. */
2304 it->start_of_box_run_p
2305 = (new_face->box != FACE_NO_BOX
2306 && (it->face_id >= 0
2307 || IT_CHARPOS (*it) == BEG
2308 || new_face_id != face_before_it_pos (it)));
2309 it->face_box_p = new_face->box != FACE_NO_BOX;
2310 }
2311 }
2312 else
2313 {
2314 int base_face_id, bufpos;
2315
2316 if (it->current.overlay_string_index >= 0)
2317 bufpos = IT_CHARPOS (*it);
2318 else
2319 bufpos = 0;
2320
2321 /* For strings from a buffer, i.e. overlay strings or strings
2322 from a `display' property, use the face at IT's current
2323 buffer position as the base face to merge with, so that
2324 overlay strings appear in the same face as surrounding
2325 text, unless they specify their own faces. */
2326 base_face_id = underlying_face_id (it);
2327
2328 new_face_id = face_at_string_position (it->w,
2329 it->string,
2330 IT_STRING_CHARPOS (*it),
2331 bufpos,
2332 it->region_beg_charpos,
2333 it->region_end_charpos,
2334 &next_stop,
2335 base_face_id, 0);
2336
2337 #if 0 /* This shouldn't be neccessary. Let's check it. */
2338 /* If IT is used to display a mode line we would really like to
2339 use the mode line face instead of the frame's default face. */
2340 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2341 && new_face_id == DEFAULT_FACE_ID)
2342 new_face_id = MODE_LINE_FACE_ID;
2343 #endif
2344
2345 /* Is this a start of a run of characters with box? Caveat:
2346 this can be called for a freshly allocated iterator; face_id
2347 is -1 is this case. We know that the new face will not
2348 change until the next check pos, i.e. if the new face has a
2349 box, all characters up to that position will have a
2350 box. But, as usual, we don't know whether that position
2351 is really the end. */
2352 if (new_face_id != it->face_id)
2353 {
2354 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2355 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2356
2357 /* If new face has a box but old face hasn't, this is the
2358 start of a run of characters with box, i.e. it has a
2359 shadow on the left side. */
2360 it->start_of_box_run_p
2361 = new_face->box && (old_face == NULL || !old_face->box);
2362 it->face_box_p = new_face->box != FACE_NO_BOX;
2363 }
2364 }
2365
2366 it->face_id = new_face_id;
2367 return HANDLED_NORMALLY;
2368 }
2369
2370
2371 /* Return the ID of the face ``underlying'' IT's current position,
2372 which is in a string. If the iterator is associated with a
2373 buffer, return the face at IT's current buffer position.
2374 Otherwise, use the iterator's base_face_id. */
2375
2376 static int
2377 underlying_face_id (it)
2378 struct it *it;
2379 {
2380 int face_id = it->base_face_id, i;
2381
2382 xassert (STRINGP (it->string));
2383
2384 for (i = it->sp - 1; i >= 0; --i)
2385 if (NILP (it->stack[i].string))
2386 face_id = it->stack[i].face_id;
2387
2388 return face_id;
2389 }
2390
2391
2392 /* Compute the face one character before or after the current position
2393 of IT. BEFORE_P non-zero means get the face in front of IT's
2394 position. Value is the id of the face. */
2395
2396 static int
2397 face_before_or_after_it_pos (it, before_p)
2398 struct it *it;
2399 int before_p;
2400 {
2401 int face_id, limit;
2402 int next_check_charpos;
2403 struct text_pos pos;
2404
2405 xassert (it->s == NULL);
2406
2407 if (STRINGP (it->string))
2408 {
2409 int bufpos, base_face_id;
2410
2411 /* No face change past the end of the string (for the case
2412 we are padding with spaces). No face change before the
2413 string start. */
2414 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size
2415 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2416 return it->face_id;
2417
2418 /* Set pos to the position before or after IT's current position. */
2419 if (before_p)
2420 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2421 else
2422 /* For composition, we must check the character after the
2423 composition. */
2424 pos = (it->what == IT_COMPOSITION
2425 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
2426 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
2427
2428 if (it->current.overlay_string_index >= 0)
2429 bufpos = IT_CHARPOS (*it);
2430 else
2431 bufpos = 0;
2432
2433 base_face_id = underlying_face_id (it);
2434
2435 /* Get the face for ASCII, or unibyte. */
2436 face_id = face_at_string_position (it->w,
2437 it->string,
2438 CHARPOS (pos),
2439 bufpos,
2440 it->region_beg_charpos,
2441 it->region_end_charpos,
2442 &next_check_charpos,
2443 base_face_id, 0);
2444
2445 /* Correct the face for charsets different from ASCII. Do it
2446 for the multibyte case only. The face returned above is
2447 suitable for unibyte text if IT->string is unibyte. */
2448 if (STRING_MULTIBYTE (it->string))
2449 {
2450 unsigned char *p = XSTRING (it->string)->data + BYTEPOS (pos);
2451 int rest = STRING_BYTES (XSTRING (it->string)) - BYTEPOS (pos);
2452 int c, len;
2453 struct face *face = FACE_FROM_ID (it->f, face_id);
2454
2455 c = string_char_and_length (p, rest, &len);
2456 face_id = FACE_FOR_CHAR (it->f, face, c);
2457 }
2458 }
2459 else
2460 {
2461 if ((IT_CHARPOS (*it) >= ZV && !before_p)
2462 || (IT_CHARPOS (*it) <= BEGV && before_p))
2463 return it->face_id;
2464
2465 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
2466 pos = it->current.pos;
2467
2468 if (before_p)
2469 DEC_TEXT_POS (pos, it->multibyte_p);
2470 else
2471 {
2472 if (it->what == IT_COMPOSITION)
2473 /* For composition, we must check the position after the
2474 composition. */
2475 pos.charpos += it->cmp_len, pos.bytepos += it->len;
2476 else
2477 INC_TEXT_POS (pos, it->multibyte_p);
2478 }
2479
2480 /* Determine face for CHARSET_ASCII, or unibyte. */
2481 face_id = face_at_buffer_position (it->w,
2482 CHARPOS (pos),
2483 it->region_beg_charpos,
2484 it->region_end_charpos,
2485 &next_check_charpos,
2486 limit, 0);
2487
2488 /* Correct the face for charsets different from ASCII. Do it
2489 for the multibyte case only. The face returned above is
2490 suitable for unibyte text if current_buffer is unibyte. */
2491 if (it->multibyte_p)
2492 {
2493 int c = FETCH_MULTIBYTE_CHAR (CHARPOS (pos));
2494 struct face *face = FACE_FROM_ID (it->f, face_id);
2495 face_id = FACE_FOR_CHAR (it->f, face, c);
2496 }
2497 }
2498
2499 return face_id;
2500 }
2501
2502
2503 \f
2504 /***********************************************************************
2505 Invisible text
2506 ***********************************************************************/
2507
2508 /* Set up iterator IT from invisible properties at its current
2509 position. Called from handle_stop. */
2510
2511 static enum prop_handled
2512 handle_invisible_prop (it)
2513 struct it *it;
2514 {
2515 enum prop_handled handled = HANDLED_NORMALLY;
2516
2517 if (STRINGP (it->string))
2518 {
2519 extern Lisp_Object Qinvisible;
2520 Lisp_Object prop, end_charpos, limit, charpos;
2521
2522 /* Get the value of the invisible text property at the
2523 current position. Value will be nil if there is no such
2524 property. */
2525 charpos = make_number (IT_STRING_CHARPOS (*it));
2526 prop = Fget_text_property (charpos, Qinvisible, it->string);
2527
2528 if (!NILP (prop)
2529 && IT_STRING_CHARPOS (*it) < it->end_charpos)
2530 {
2531 handled = HANDLED_RECOMPUTE_PROPS;
2532
2533 /* Get the position at which the next change of the
2534 invisible text property can be found in IT->string.
2535 Value will be nil if the property value is the same for
2536 all the rest of IT->string. */
2537 XSETINT (limit, XSTRING (it->string)->size);
2538 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2539 it->string, limit);
2540
2541 /* Text at current position is invisible. The next
2542 change in the property is at position end_charpos.
2543 Move IT's current position to that position. */
2544 if (INTEGERP (end_charpos)
2545 && XFASTINT (end_charpos) < XFASTINT (limit))
2546 {
2547 struct text_pos old;
2548 old = it->current.string_pos;
2549 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
2550 compute_string_pos (&it->current.string_pos, old, it->string);
2551 }
2552 else
2553 {
2554 /* The rest of the string is invisible. If this is an
2555 overlay string, proceed with the next overlay string
2556 or whatever comes and return a character from there. */
2557 if (it->current.overlay_string_index >= 0)
2558 {
2559 next_overlay_string (it);
2560 /* Don't check for overlay strings when we just
2561 finished processing them. */
2562 handled = HANDLED_OVERLAY_STRING_CONSUMED;
2563 }
2564 else
2565 {
2566 struct Lisp_String *s = XSTRING (it->string);
2567 IT_STRING_CHARPOS (*it) = s->size;
2568 IT_STRING_BYTEPOS (*it) = STRING_BYTES (s);
2569 }
2570 }
2571 }
2572 }
2573 else
2574 {
2575 int invis_p, newpos, next_stop, start_charpos;
2576 Lisp_Object pos, prop, overlay;
2577
2578 /* First of all, is there invisible text at this position? */
2579 start_charpos = IT_CHARPOS (*it);
2580 pos = make_number (IT_CHARPOS (*it));
2581 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
2582 &overlay);
2583 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
2584
2585 /* If we are on invisible text, skip over it. */
2586 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
2587 {
2588 /* Record whether we have to display an ellipsis for the
2589 invisible text. */
2590 int display_ellipsis_p = invis_p == 2;
2591
2592 handled = HANDLED_RECOMPUTE_PROPS;
2593
2594 /* Loop skipping over invisible text. The loop is left at
2595 ZV or with IT on the first char being visible again. */
2596 do
2597 {
2598 /* Try to skip some invisible text. Return value is the
2599 position reached which can be equal to IT's position
2600 if there is nothing invisible here. This skips both
2601 over invisible text properties and overlays with
2602 invisible property. */
2603 newpos = skip_invisible (IT_CHARPOS (*it),
2604 &next_stop, ZV, it->window);
2605
2606 /* If we skipped nothing at all we weren't at invisible
2607 text in the first place. If everything to the end of
2608 the buffer was skipped, end the loop. */
2609 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
2610 invis_p = 0;
2611 else
2612 {
2613 /* We skipped some characters but not necessarily
2614 all there are. Check if we ended up on visible
2615 text. Fget_char_property returns the property of
2616 the char before the given position, i.e. if we
2617 get invis_p = 0, this means that the char at
2618 newpos is visible. */
2619 pos = make_number (newpos);
2620 prop = Fget_char_property (pos, Qinvisible, it->window);
2621 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
2622 }
2623
2624 /* If we ended up on invisible text, proceed to
2625 skip starting with next_stop. */
2626 if (invis_p)
2627 IT_CHARPOS (*it) = next_stop;
2628 }
2629 while (invis_p);
2630
2631 /* The position newpos is now either ZV or on visible text. */
2632 IT_CHARPOS (*it) = newpos;
2633 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2634
2635 /* If there are before-strings at the start of invisible
2636 text, and the text is invisible because of a text
2637 property, arrange to show before-strings because 20.x did
2638 it that way. (If the text is invisible because of an
2639 overlay property instead of a text property, this is
2640 already handled in the overlay code.) */
2641 if (NILP (overlay)
2642 && get_overlay_strings (it, start_charpos))
2643 {
2644 handled = HANDLED_RECOMPUTE_PROPS;
2645 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
2646 }
2647 else if (display_ellipsis_p)
2648 setup_for_ellipsis (it);
2649 }
2650 }
2651
2652 return handled;
2653 }
2654
2655
2656 /* Make iterator IT return `...' next. */
2657
2658 static void
2659 setup_for_ellipsis (it)
2660 struct it *it;
2661 {
2662 if (it->dp
2663 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
2664 {
2665 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
2666 it->dpvec = v->contents;
2667 it->dpend = v->contents + v->size;
2668 }
2669 else
2670 {
2671 /* Default `...'. */
2672 it->dpvec = default_invis_vector;
2673 it->dpend = default_invis_vector + 3;
2674 }
2675
2676 /* The ellipsis display does not replace the display of the
2677 character at the new position. Indicate this by setting
2678 IT->dpvec_char_len to zero. */
2679 it->dpvec_char_len = 0;
2680
2681 it->current.dpvec_index = 0;
2682 it->method = next_element_from_display_vector;
2683 }
2684
2685
2686 \f
2687 /***********************************************************************
2688 'display' property
2689 ***********************************************************************/
2690
2691 /* Set up iterator IT from `display' property at its current position.
2692 Called from handle_stop. */
2693
2694 static enum prop_handled
2695 handle_display_prop (it)
2696 struct it *it;
2697 {
2698 Lisp_Object prop, object;
2699 struct text_pos *position;
2700 int display_replaced_p = 0;
2701
2702 if (STRINGP (it->string))
2703 {
2704 object = it->string;
2705 position = &it->current.string_pos;
2706 }
2707 else
2708 {
2709 object = it->w->buffer;
2710 position = &it->current.pos;
2711 }
2712
2713 /* Reset those iterator values set from display property values. */
2714 it->font_height = Qnil;
2715 it->space_width = Qnil;
2716 it->voffset = 0;
2717
2718 /* We don't support recursive `display' properties, i.e. string
2719 values that have a string `display' property, that have a string
2720 `display' property etc. */
2721 if (!it->string_from_display_prop_p)
2722 it->area = TEXT_AREA;
2723
2724 prop = Fget_char_property (make_number (position->charpos),
2725 Qdisplay, object);
2726 if (NILP (prop))
2727 return HANDLED_NORMALLY;
2728
2729 if (CONSP (prop)
2730 /* Simple properties. */
2731 && !EQ (XCAR (prop), Qimage)
2732 && !EQ (XCAR (prop), Qspace)
2733 && !EQ (XCAR (prop), Qwhen)
2734 && !EQ (XCAR (prop), Qspace_width)
2735 && !EQ (XCAR (prop), Qheight)
2736 && !EQ (XCAR (prop), Qraise)
2737 /* Marginal area specifications. */
2738 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
2739 && !NILP (XCAR (prop)))
2740 {
2741 for (; CONSP (prop); prop = XCDR (prop))
2742 {
2743 if (handle_single_display_prop (it, XCAR (prop), object,
2744 position, display_replaced_p))
2745 display_replaced_p = 1;
2746 }
2747 }
2748 else if (VECTORP (prop))
2749 {
2750 int i;
2751 for (i = 0; i < ASIZE (prop); ++i)
2752 if (handle_single_display_prop (it, AREF (prop, i), object,
2753 position, display_replaced_p))
2754 display_replaced_p = 1;
2755 }
2756 else
2757 {
2758 if (handle_single_display_prop (it, prop, object, position, 0))
2759 display_replaced_p = 1;
2760 }
2761
2762 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
2763 }
2764
2765
2766 /* Value is the position of the end of the `display' property starting
2767 at START_POS in OBJECT. */
2768
2769 static struct text_pos
2770 display_prop_end (it, object, start_pos)
2771 struct it *it;
2772 Lisp_Object object;
2773 struct text_pos start_pos;
2774 {
2775 Lisp_Object end;
2776 struct text_pos end_pos;
2777
2778 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
2779 Qdisplay, object, Qnil);
2780 CHARPOS (end_pos) = XFASTINT (end);
2781 if (STRINGP (object))
2782 compute_string_pos (&end_pos, start_pos, it->string);
2783 else
2784 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
2785
2786 return end_pos;
2787 }
2788
2789
2790 /* Set up IT from a single `display' sub-property value PROP. OBJECT
2791 is the object in which the `display' property was found. *POSITION
2792 is the position at which it was found. DISPLAY_REPLACED_P non-zero
2793 means that we previously saw a display sub-property which already
2794 replaced text display with something else, for example an image;
2795 ignore such properties after the first one has been processed.
2796
2797 If PROP is a `space' or `image' sub-property, set *POSITION to the
2798 end position of the `display' property.
2799
2800 Value is non-zero something was found which replaces the display
2801 of buffer or string text. */
2802
2803 static int
2804 handle_single_display_prop (it, prop, object, position,
2805 display_replaced_before_p)
2806 struct it *it;
2807 Lisp_Object prop;
2808 Lisp_Object object;
2809 struct text_pos *position;
2810 int display_replaced_before_p;
2811 {
2812 Lisp_Object value;
2813 int replaces_text_display_p = 0;
2814 Lisp_Object form;
2815
2816 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
2817 evaluated. If the result is nil, VALUE is ignored. */
2818 form = Qt;
2819 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2820 {
2821 prop = XCDR (prop);
2822 if (!CONSP (prop))
2823 return 0;
2824 form = XCAR (prop);
2825 prop = XCDR (prop);
2826 }
2827
2828 if (!NILP (form) && !EQ (form, Qt))
2829 {
2830 int count = BINDING_STACK_SIZE ();
2831 struct gcpro gcpro1;
2832
2833 /* Bind `object' to the object having the `display' property, a
2834 buffer or string. Bind `position' to the position in the
2835 object where the property was found, and `buffer-position'
2836 to the current position in the buffer. */
2837 specbind (Qobject, object);
2838 specbind (Qposition, make_number (CHARPOS (*position)));
2839 specbind (Qbuffer_position,
2840 make_number (STRINGP (object)
2841 ? IT_CHARPOS (*it) : CHARPOS (*position)));
2842 GCPRO1 (form);
2843 form = safe_eval (form);
2844 UNGCPRO;
2845 unbind_to (count, Qnil);
2846 }
2847
2848 if (NILP (form))
2849 return 0;
2850
2851 if (CONSP (prop)
2852 && EQ (XCAR (prop), Qheight)
2853 && CONSP (XCDR (prop)))
2854 {
2855 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2856 return 0;
2857
2858 /* `(height HEIGHT)'. */
2859 it->font_height = XCAR (XCDR (prop));
2860 if (!NILP (it->font_height))
2861 {
2862 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2863 int new_height = -1;
2864
2865 if (CONSP (it->font_height)
2866 && (EQ (XCAR (it->font_height), Qplus)
2867 || EQ (XCAR (it->font_height), Qminus))
2868 && CONSP (XCDR (it->font_height))
2869 && INTEGERP (XCAR (XCDR (it->font_height))))
2870 {
2871 /* `(+ N)' or `(- N)' where N is an integer. */
2872 int steps = XINT (XCAR (XCDR (it->font_height)));
2873 if (EQ (XCAR (it->font_height), Qplus))
2874 steps = - steps;
2875 it->face_id = smaller_face (it->f, it->face_id, steps);
2876 }
2877 else if (FUNCTIONP (it->font_height))
2878 {
2879 /* Call function with current height as argument.
2880 Value is the new height. */
2881 Lisp_Object height;
2882 height = safe_call1 (it->font_height,
2883 face->lface[LFACE_HEIGHT_INDEX]);
2884 if (NUMBERP (height))
2885 new_height = XFLOATINT (height);
2886 }
2887 else if (NUMBERP (it->font_height))
2888 {
2889 /* Value is a multiple of the canonical char height. */
2890 struct face *face;
2891
2892 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
2893 new_height = (XFLOATINT (it->font_height)
2894 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
2895 }
2896 else
2897 {
2898 /* Evaluate IT->font_height with `height' bound to the
2899 current specified height to get the new height. */
2900 Lisp_Object value;
2901 int count = BINDING_STACK_SIZE ();
2902
2903 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
2904 value = safe_eval (it->font_height);
2905 unbind_to (count, Qnil);
2906
2907 if (NUMBERP (value))
2908 new_height = XFLOATINT (value);
2909 }
2910
2911 if (new_height > 0)
2912 it->face_id = face_with_height (it->f, it->face_id, new_height);
2913 }
2914 }
2915 else if (CONSP (prop)
2916 && EQ (XCAR (prop), Qspace_width)
2917 && CONSP (XCDR (prop)))
2918 {
2919 /* `(space_width WIDTH)'. */
2920 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2921 return 0;
2922
2923 value = XCAR (XCDR (prop));
2924 if (NUMBERP (value) && XFLOATINT (value) > 0)
2925 it->space_width = value;
2926 }
2927 else if (CONSP (prop)
2928 && EQ (XCAR (prop), Qraise)
2929 && CONSP (XCDR (prop)))
2930 {
2931 /* `(raise FACTOR)'. */
2932 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2933 return 0;
2934
2935 #ifdef HAVE_WINDOW_SYSTEM
2936 value = XCAR (XCDR (prop));
2937 if (NUMBERP (value))
2938 {
2939 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2940 it->voffset = - (XFLOATINT (value)
2941 * (FONT_HEIGHT (face->font)));
2942 }
2943 #endif /* HAVE_WINDOW_SYSTEM */
2944 }
2945 else if (!it->string_from_display_prop_p)
2946 {
2947 /* `((margin left-margin) VALUE)' or `((margin right-margin)
2948 VALUE) or `((margin nil) VALUE)' or VALUE. */
2949 Lisp_Object location, value;
2950 struct text_pos start_pos;
2951 int valid_p;
2952
2953 /* Characters having this form of property are not displayed, so
2954 we have to find the end of the property. */
2955 start_pos = *position;
2956 *position = display_prop_end (it, object, start_pos);
2957 value = Qnil;
2958
2959 /* Let's stop at the new position and assume that all
2960 text properties change there. */
2961 it->stop_charpos = position->charpos;
2962
2963 location = Qunbound;
2964 if (CONSP (prop) && CONSP (XCAR (prop)))
2965 {
2966 Lisp_Object tem;
2967
2968 value = XCDR (prop);
2969 if (CONSP (value))
2970 value = XCAR (value);
2971
2972 tem = XCAR (prop);
2973 if (EQ (XCAR (tem), Qmargin)
2974 && (tem = XCDR (tem),
2975 tem = CONSP (tem) ? XCAR (tem) : Qnil,
2976 (NILP (tem)
2977 || EQ (tem, Qleft_margin)
2978 || EQ (tem, Qright_margin))))
2979 location = tem;
2980 }
2981
2982 if (EQ (location, Qunbound))
2983 {
2984 location = Qnil;
2985 value = prop;
2986 }
2987
2988 #ifdef HAVE_WINDOW_SYSTEM
2989 if (FRAME_TERMCAP_P (it->f))
2990 valid_p = STRINGP (value);
2991 else
2992 valid_p = (STRINGP (value)
2993 || (CONSP (value) && EQ (XCAR (value), Qspace))
2994 || valid_image_p (value));
2995 #else /* not HAVE_WINDOW_SYSTEM */
2996 valid_p = STRINGP (value);
2997 #endif /* not HAVE_WINDOW_SYSTEM */
2998
2999 if ((EQ (location, Qleft_margin)
3000 || EQ (location, Qright_margin)
3001 || NILP (location))
3002 && valid_p
3003 && !display_replaced_before_p)
3004 {
3005 replaces_text_display_p = 1;
3006
3007 /* Save current settings of IT so that we can restore them
3008 when we are finished with the glyph property value. */
3009 push_it (it);
3010
3011 if (NILP (location))
3012 it->area = TEXT_AREA;
3013 else if (EQ (location, Qleft_margin))
3014 it->area = LEFT_MARGIN_AREA;
3015 else
3016 it->area = RIGHT_MARGIN_AREA;
3017
3018 if (STRINGP (value))
3019 {
3020 it->string = value;
3021 it->multibyte_p = STRING_MULTIBYTE (it->string);
3022 it->current.overlay_string_index = -1;
3023 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3024 it->end_charpos = it->string_nchars
3025 = XSTRING (it->string)->size;
3026 it->method = next_element_from_string;
3027 it->stop_charpos = 0;
3028 it->string_from_display_prop_p = 1;
3029 /* Say that we haven't consumed the characters with
3030 `display' property yet. The call to pop_it in
3031 set_iterator_to_next will clean this up. */
3032 *position = start_pos;
3033 }
3034 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3035 {
3036 it->method = next_element_from_stretch;
3037 it->object = value;
3038 it->current.pos = it->position = start_pos;
3039 }
3040 #ifdef HAVE_WINDOW_SYSTEM
3041 else
3042 {
3043 it->what = IT_IMAGE;
3044 it->image_id = lookup_image (it->f, value);
3045 it->position = start_pos;
3046 it->object = NILP (object) ? it->w->buffer : object;
3047 it->method = next_element_from_image;
3048
3049 /* Say that we haven't consumed the characters with
3050 `display' property yet. The call to pop_it in
3051 set_iterator_to_next will clean this up. */
3052 *position = start_pos;
3053 }
3054 #endif /* HAVE_WINDOW_SYSTEM */
3055 }
3056 else
3057 /* Invalid property or property not supported. Restore
3058 the position to what it was before. */
3059 *position = start_pos;
3060 }
3061
3062 return replaces_text_display_p;
3063 }
3064
3065
3066 /* Check if PROP is a display sub-property value whose text should be
3067 treated as intangible. */
3068
3069 static int
3070 single_display_prop_intangible_p (prop)
3071 Lisp_Object prop;
3072 {
3073 /* Skip over `when FORM'. */
3074 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3075 {
3076 prop = XCDR (prop);
3077 if (!CONSP (prop))
3078 return 0;
3079 prop = XCDR (prop);
3080 }
3081
3082 if (!CONSP (prop))
3083 return 0;
3084
3085 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3086 we don't need to treat text as intangible. */
3087 if (EQ (XCAR (prop), Qmargin))
3088 {
3089 prop = XCDR (prop);
3090 if (!CONSP (prop))
3091 return 0;
3092
3093 prop = XCDR (prop);
3094 if (!CONSP (prop)
3095 || EQ (XCAR (prop), Qleft_margin)
3096 || EQ (XCAR (prop), Qright_margin))
3097 return 0;
3098 }
3099
3100 return CONSP (prop) && EQ (XCAR (prop), Qimage);
3101 }
3102
3103
3104 /* Check if PROP is a display property value whose text should be
3105 treated as intangible. */
3106
3107 int
3108 display_prop_intangible_p (prop)
3109 Lisp_Object prop;
3110 {
3111 if (CONSP (prop)
3112 && CONSP (XCAR (prop))
3113 && !EQ (Qmargin, XCAR (XCAR (prop))))
3114 {
3115 /* A list of sub-properties. */
3116 while (CONSP (prop))
3117 {
3118 if (single_display_prop_intangible_p (XCAR (prop)))
3119 return 1;
3120 prop = XCDR (prop);
3121 }
3122 }
3123 else if (VECTORP (prop))
3124 {
3125 /* A vector of sub-properties. */
3126 int i;
3127 for (i = 0; i < ASIZE (prop); ++i)
3128 if (single_display_prop_intangible_p (AREF (prop, i)))
3129 return 1;
3130 }
3131 else
3132 return single_display_prop_intangible_p (prop);
3133
3134 return 0;
3135 }
3136
3137
3138 /* Return 1 if PROP is a display sub-property value containing STRING. */
3139
3140 static int
3141 single_display_prop_string_p (prop, string)
3142 Lisp_Object prop, string;
3143 {
3144 if (EQ (string, prop))
3145 return 1;
3146
3147 /* Skip over `when FORM'. */
3148 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3149 {
3150 prop = XCDR (prop);
3151 if (!CONSP (prop))
3152 return 0;
3153 prop = XCDR (prop);
3154 }
3155
3156 if (CONSP (prop))
3157 /* Skip over `margin LOCATION'. */
3158 if (EQ (XCAR (prop), Qmargin))
3159 {
3160 prop = XCDR (prop);
3161 if (!CONSP (prop))
3162 return 0;
3163
3164 prop = XCDR (prop);
3165 if (!CONSP (prop))
3166 return 0;
3167 }
3168
3169 return CONSP (prop) && EQ (XCAR (prop), string);
3170 }
3171
3172
3173 /* Return 1 if STRING appears in the `display' property PROP. */
3174
3175 static int
3176 display_prop_string_p (prop, string)
3177 Lisp_Object prop, string;
3178 {
3179 if (CONSP (prop)
3180 && CONSP (XCAR (prop))
3181 && !EQ (Qmargin, XCAR (XCAR (prop))))
3182 {
3183 /* A list of sub-properties. */
3184 while (CONSP (prop))
3185 {
3186 if (single_display_prop_string_p (XCAR (prop), string))
3187 return 1;
3188 prop = XCDR (prop);
3189 }
3190 }
3191 else if (VECTORP (prop))
3192 {
3193 /* A vector of sub-properties. */
3194 int i;
3195 for (i = 0; i < ASIZE (prop); ++i)
3196 if (single_display_prop_string_p (AREF (prop, i), string))
3197 return 1;
3198 }
3199 else
3200 return single_display_prop_string_p (prop, string);
3201
3202 return 0;
3203 }
3204
3205
3206 /* Determine from which buffer position in W's buffer STRING comes
3207 from. AROUND_CHARPOS is an approximate position where it could
3208 be from. Value is the buffer position or 0 if it couldn't be
3209 determined.
3210
3211 W's buffer must be current.
3212
3213 This function is necessary because we don't record buffer positions
3214 in glyphs generated from strings (to keep struct glyph small).
3215 This function may only use code that doesn't eval because it is
3216 called asynchronously from note_mouse_highlight. */
3217
3218 int
3219 string_buffer_position (w, string, around_charpos)
3220 struct window *w;
3221 Lisp_Object string;
3222 int around_charpos;
3223 {
3224 Lisp_Object limit, prop, pos;
3225 const int MAX_DISTANCE = 1000;
3226 int found = 0;
3227
3228 pos = make_number (around_charpos);
3229 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
3230 while (!found && !EQ (pos, limit))
3231 {
3232 prop = Fget_char_property (pos, Qdisplay, Qnil);
3233 if (!NILP (prop) && display_prop_string_p (prop, string))
3234 found = 1;
3235 else
3236 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
3237 }
3238
3239 if (!found)
3240 {
3241 pos = make_number (around_charpos);
3242 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
3243 while (!found && !EQ (pos, limit))
3244 {
3245 prop = Fget_char_property (pos, Qdisplay, Qnil);
3246 if (!NILP (prop) && display_prop_string_p (prop, string))
3247 found = 1;
3248 else
3249 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
3250 limit);
3251 }
3252 }
3253
3254 return found ? XINT (pos) : 0;
3255 }
3256
3257
3258 \f
3259 /***********************************************************************
3260 `composition' property
3261 ***********************************************************************/
3262
3263 /* Set up iterator IT from `composition' property at its current
3264 position. Called from handle_stop. */
3265
3266 static enum prop_handled
3267 handle_composition_prop (it)
3268 struct it *it;
3269 {
3270 Lisp_Object prop, string;
3271 int pos, pos_byte, end;
3272 enum prop_handled handled = HANDLED_NORMALLY;
3273
3274 if (STRINGP (it->string))
3275 {
3276 pos = IT_STRING_CHARPOS (*it);
3277 pos_byte = IT_STRING_BYTEPOS (*it);
3278 string = it->string;
3279 }
3280 else
3281 {
3282 pos = IT_CHARPOS (*it);
3283 pos_byte = IT_BYTEPOS (*it);
3284 string = Qnil;
3285 }
3286
3287 /* If there's a valid composition and point is not inside of the
3288 composition (in the case that the composition is from the current
3289 buffer), draw a glyph composed from the composition components. */
3290 if (find_composition (pos, -1, &pos, &end, &prop, string)
3291 && COMPOSITION_VALID_P (pos, end, prop)
3292 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
3293 {
3294 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
3295
3296 if (id >= 0)
3297 {
3298 it->method = next_element_from_composition;
3299 it->cmp_id = id;
3300 it->cmp_len = COMPOSITION_LENGTH (prop);
3301 /* For a terminal, draw only the first character of the
3302 components. */
3303 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
3304 it->len = (STRINGP (it->string)
3305 ? string_char_to_byte (it->string, end)
3306 : CHAR_TO_BYTE (end)) - pos_byte;
3307 it->stop_charpos = end;
3308 handled = HANDLED_RETURN;
3309 }
3310 }
3311
3312 return handled;
3313 }
3314
3315
3316 \f
3317 /***********************************************************************
3318 Overlay strings
3319 ***********************************************************************/
3320
3321 /* The following structure is used to record overlay strings for
3322 later sorting in load_overlay_strings. */
3323
3324 struct overlay_entry
3325 {
3326 Lisp_Object overlay;
3327 Lisp_Object string;
3328 int priority;
3329 int after_string_p;
3330 };
3331
3332
3333 /* Set up iterator IT from overlay strings at its current position.
3334 Called from handle_stop. */
3335
3336 static enum prop_handled
3337 handle_overlay_change (it)
3338 struct it *it;
3339 {
3340 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
3341 return HANDLED_RECOMPUTE_PROPS;
3342 else
3343 return HANDLED_NORMALLY;
3344 }
3345
3346
3347 /* Set up the next overlay string for delivery by IT, if there is an
3348 overlay string to deliver. Called by set_iterator_to_next when the
3349 end of the current overlay string is reached. If there are more
3350 overlay strings to display, IT->string and
3351 IT->current.overlay_string_index are set appropriately here.
3352 Otherwise IT->string is set to nil. */
3353
3354 static void
3355 next_overlay_string (it)
3356 struct it *it;
3357 {
3358 ++it->current.overlay_string_index;
3359 if (it->current.overlay_string_index == it->n_overlay_strings)
3360 {
3361 /* No more overlay strings. Restore IT's settings to what
3362 they were before overlay strings were processed, and
3363 continue to deliver from current_buffer. */
3364 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
3365
3366 pop_it (it);
3367 xassert (it->stop_charpos >= BEGV
3368 && it->stop_charpos <= it->end_charpos);
3369 it->string = Qnil;
3370 it->current.overlay_string_index = -1;
3371 SET_TEXT_POS (it->current.string_pos, -1, -1);
3372 it->n_overlay_strings = 0;
3373 it->method = next_element_from_buffer;
3374
3375 /* If we're at the end of the buffer, record that we have
3376 processed the overlay strings there already, so that
3377 next_element_from_buffer doesn't try it again. */
3378 if (IT_CHARPOS (*it) >= it->end_charpos)
3379 it->overlay_strings_at_end_processed_p = 1;
3380
3381 /* If we have to display `...' for invisible text, set
3382 the iterator up for that. */
3383 if (display_ellipsis_p)
3384 setup_for_ellipsis (it);
3385 }
3386 else
3387 {
3388 /* There are more overlay strings to process. If
3389 IT->current.overlay_string_index has advanced to a position
3390 where we must load IT->overlay_strings with more strings, do
3391 it. */
3392 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
3393
3394 if (it->current.overlay_string_index && i == 0)
3395 load_overlay_strings (it, 0);
3396
3397 /* Initialize IT to deliver display elements from the overlay
3398 string. */
3399 it->string = it->overlay_strings[i];
3400 it->multibyte_p = STRING_MULTIBYTE (it->string);
3401 SET_TEXT_POS (it->current.string_pos, 0, 0);
3402 it->method = next_element_from_string;
3403 it->stop_charpos = 0;
3404 }
3405
3406 CHECK_IT (it);
3407 }
3408
3409
3410 /* Compare two overlay_entry structures E1 and E2. Used as a
3411 comparison function for qsort in load_overlay_strings. Overlay
3412 strings for the same position are sorted so that
3413
3414 1. All after-strings come in front of before-strings, except
3415 when they come from the same overlay.
3416
3417 2. Within after-strings, strings are sorted so that overlay strings
3418 from overlays with higher priorities come first.
3419
3420 2. Within before-strings, strings are sorted so that overlay
3421 strings from overlays with higher priorities come last.
3422
3423 Value is analogous to strcmp. */
3424
3425
3426 static int
3427 compare_overlay_entries (e1, e2)
3428 void *e1, *e2;
3429 {
3430 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
3431 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
3432 int result;
3433
3434 if (entry1->after_string_p != entry2->after_string_p)
3435 {
3436 /* Let after-strings appear in front of before-strings if
3437 they come from different overlays. */
3438 if (EQ (entry1->overlay, entry2->overlay))
3439 result = entry1->after_string_p ? 1 : -1;
3440 else
3441 result = entry1->after_string_p ? -1 : 1;
3442 }
3443 else if (entry1->after_string_p)
3444 /* After-strings sorted in order of decreasing priority. */
3445 result = entry2->priority - entry1->priority;
3446 else
3447 /* Before-strings sorted in order of increasing priority. */
3448 result = entry1->priority - entry2->priority;
3449
3450 return result;
3451 }
3452
3453
3454 /* Load the vector IT->overlay_strings with overlay strings from IT's
3455 current buffer position, or from CHARPOS if that is > 0. Set
3456 IT->n_overlays to the total number of overlay strings found.
3457
3458 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
3459 a time. On entry into load_overlay_strings,
3460 IT->current.overlay_string_index gives the number of overlay
3461 strings that have already been loaded by previous calls to this
3462 function.
3463
3464 IT->add_overlay_start contains an additional overlay start
3465 position to consider for taking overlay strings from, if non-zero.
3466 This position comes into play when the overlay has an `invisible'
3467 property, and both before and after-strings. When we've skipped to
3468 the end of the overlay, because of its `invisible' property, we
3469 nevertheless want its before-string to appear.
3470 IT->add_overlay_start will contain the overlay start position
3471 in this case.
3472
3473 Overlay strings are sorted so that after-string strings come in
3474 front of before-string strings. Within before and after-strings,
3475 strings are sorted by overlay priority. See also function
3476 compare_overlay_entries. */
3477
3478 static void
3479 load_overlay_strings (it, charpos)
3480 struct it *it;
3481 int charpos;
3482 {
3483 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
3484 Lisp_Object ov, overlay, window, str, invisible;
3485 int start, end;
3486 int size = 20;
3487 int n = 0, i, j, invis_p;
3488 struct overlay_entry *entries
3489 = (struct overlay_entry *) alloca (size * sizeof *entries);
3490
3491 if (charpos <= 0)
3492 charpos = IT_CHARPOS (*it);
3493
3494 /* Append the overlay string STRING of overlay OVERLAY to vector
3495 `entries' which has size `size' and currently contains `n'
3496 elements. AFTER_P non-zero means STRING is an after-string of
3497 OVERLAY. */
3498 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
3499 do \
3500 { \
3501 Lisp_Object priority; \
3502 \
3503 if (n == size) \
3504 { \
3505 int new_size = 2 * size; \
3506 struct overlay_entry *old = entries; \
3507 entries = \
3508 (struct overlay_entry *) alloca (new_size \
3509 * sizeof *entries); \
3510 bcopy (old, entries, size * sizeof *entries); \
3511 size = new_size; \
3512 } \
3513 \
3514 entries[n].string = (STRING); \
3515 entries[n].overlay = (OVERLAY); \
3516 priority = Foverlay_get ((OVERLAY), Qpriority); \
3517 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
3518 entries[n].after_string_p = (AFTER_P); \
3519 ++n; \
3520 } \
3521 while (0)
3522
3523 /* Process overlay before the overlay center. */
3524 for (ov = current_buffer->overlays_before; CONSP (ov); ov = XCDR (ov))
3525 {
3526 overlay = XCAR (ov);
3527 xassert (OVERLAYP (overlay));
3528 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3529 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3530
3531 if (end < charpos)
3532 break;
3533
3534 /* Skip this overlay if it doesn't start or end at IT's current
3535 position. */
3536 if (end != charpos && start != charpos)
3537 continue;
3538
3539 /* Skip this overlay if it doesn't apply to IT->w. */
3540 window = Foverlay_get (overlay, Qwindow);
3541 if (WINDOWP (window) && XWINDOW (window) != it->w)
3542 continue;
3543
3544 /* If the text ``under'' the overlay is invisible, both before-
3545 and after-strings from this overlay are visible; start and
3546 end position are indistinguishable. */
3547 invisible = Foverlay_get (overlay, Qinvisible);
3548 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3549
3550 /* If overlay has a non-empty before-string, record it. */
3551 if ((start == charpos || (end == charpos && invis_p))
3552 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3553 && XSTRING (str)->size)
3554 RECORD_OVERLAY_STRING (overlay, str, 0);
3555
3556 /* If overlay has a non-empty after-string, record it. */
3557 if ((end == charpos || (start == charpos && invis_p))
3558 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3559 && XSTRING (str)->size)
3560 RECORD_OVERLAY_STRING (overlay, str, 1);
3561 }
3562
3563 /* Process overlays after the overlay center. */
3564 for (ov = current_buffer->overlays_after; CONSP (ov); ov = XCDR (ov))
3565 {
3566 overlay = XCAR (ov);
3567 xassert (OVERLAYP (overlay));
3568 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3569 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3570
3571 if (start > charpos)
3572 break;
3573
3574 /* Skip this overlay if it doesn't start or end at IT's current
3575 position. */
3576 if (end != charpos && start != charpos)
3577 continue;
3578
3579 /* Skip this overlay if it doesn't apply to IT->w. */
3580 window = Foverlay_get (overlay, Qwindow);
3581 if (WINDOWP (window) && XWINDOW (window) != it->w)
3582 continue;
3583
3584 /* If the text ``under'' the overlay is invisible, it has a zero
3585 dimension, and both before- and after-strings apply. */
3586 invisible = Foverlay_get (overlay, Qinvisible);
3587 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3588
3589 /* If overlay has a non-empty before-string, record it. */
3590 if ((start == charpos || (end == charpos && invis_p))
3591 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3592 && XSTRING (str)->size)
3593 RECORD_OVERLAY_STRING (overlay, str, 0);
3594
3595 /* If overlay has a non-empty after-string, record it. */
3596 if ((end == charpos || (start == charpos && invis_p))
3597 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3598 && XSTRING (str)->size)
3599 RECORD_OVERLAY_STRING (overlay, str, 1);
3600 }
3601
3602 #undef RECORD_OVERLAY_STRING
3603
3604 /* Sort entries. */
3605 if (n > 1)
3606 qsort (entries, n, sizeof *entries, compare_overlay_entries);
3607
3608 /* Record the total number of strings to process. */
3609 it->n_overlay_strings = n;
3610
3611 /* IT->current.overlay_string_index is the number of overlay strings
3612 that have already been consumed by IT. Copy some of the
3613 remaining overlay strings to IT->overlay_strings. */
3614 i = 0;
3615 j = it->current.overlay_string_index;
3616 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
3617 it->overlay_strings[i++] = entries[j++].string;
3618
3619 CHECK_IT (it);
3620 }
3621
3622
3623 /* Get the first chunk of overlay strings at IT's current buffer
3624 position, or at CHARPOS if that is > 0. Value is non-zero if at
3625 least one overlay string was found. */
3626
3627 static int
3628 get_overlay_strings (it, charpos)
3629 struct it *it;
3630 int charpos;
3631 {
3632 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
3633 process. This fills IT->overlay_strings with strings, and sets
3634 IT->n_overlay_strings to the total number of strings to process.
3635 IT->pos.overlay_string_index has to be set temporarily to zero
3636 because load_overlay_strings needs this; it must be set to -1
3637 when no overlay strings are found because a zero value would
3638 indicate a position in the first overlay string. */
3639 it->current.overlay_string_index = 0;
3640 load_overlay_strings (it, charpos);
3641
3642 /* If we found overlay strings, set up IT to deliver display
3643 elements from the first one. Otherwise set up IT to deliver
3644 from current_buffer. */
3645 if (it->n_overlay_strings)
3646 {
3647 /* Make sure we know settings in current_buffer, so that we can
3648 restore meaningful values when we're done with the overlay
3649 strings. */
3650 compute_stop_pos (it);
3651 xassert (it->face_id >= 0);
3652
3653 /* Save IT's settings. They are restored after all overlay
3654 strings have been processed. */
3655 xassert (it->sp == 0);
3656 push_it (it);
3657
3658 /* Set up IT to deliver display elements from the first overlay
3659 string. */
3660 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3661 it->string = it->overlay_strings[0];
3662 it->stop_charpos = 0;
3663 it->end_charpos = XSTRING (it->string)->size;
3664 it->multibyte_p = STRING_MULTIBYTE (it->string);
3665 xassert (STRINGP (it->string));
3666 it->method = next_element_from_string;
3667 }
3668 else
3669 {
3670 it->string = Qnil;
3671 it->current.overlay_string_index = -1;
3672 it->method = next_element_from_buffer;
3673 }
3674
3675 CHECK_IT (it);
3676
3677 /* Value is non-zero if we found at least one overlay string. */
3678 return STRINGP (it->string);
3679 }
3680
3681
3682 \f
3683 /***********************************************************************
3684 Saving and restoring state
3685 ***********************************************************************/
3686
3687 /* Save current settings of IT on IT->stack. Called, for example,
3688 before setting up IT for an overlay string, to be able to restore
3689 IT's settings to what they were after the overlay string has been
3690 processed. */
3691
3692 static void
3693 push_it (it)
3694 struct it *it;
3695 {
3696 struct iterator_stack_entry *p;
3697
3698 xassert (it->sp < 2);
3699 p = it->stack + it->sp;
3700
3701 p->stop_charpos = it->stop_charpos;
3702 xassert (it->face_id >= 0);
3703 p->face_id = it->face_id;
3704 p->string = it->string;
3705 p->pos = it->current;
3706 p->end_charpos = it->end_charpos;
3707 p->string_nchars = it->string_nchars;
3708 p->area = it->area;
3709 p->multibyte_p = it->multibyte_p;
3710 p->space_width = it->space_width;
3711 p->font_height = it->font_height;
3712 p->voffset = it->voffset;
3713 p->string_from_display_prop_p = it->string_from_display_prop_p;
3714 p->display_ellipsis_p = 0;
3715 ++it->sp;
3716 }
3717
3718
3719 /* Restore IT's settings from IT->stack. Called, for example, when no
3720 more overlay strings must be processed, and we return to delivering
3721 display elements from a buffer, or when the end of a string from a
3722 `display' property is reached and we return to delivering display
3723 elements from an overlay string, or from a buffer. */
3724
3725 static void
3726 pop_it (it)
3727 struct it *it;
3728 {
3729 struct iterator_stack_entry *p;
3730
3731 xassert (it->sp > 0);
3732 --it->sp;
3733 p = it->stack + it->sp;
3734 it->stop_charpos = p->stop_charpos;
3735 it->face_id = p->face_id;
3736 it->string = p->string;
3737 it->current = p->pos;
3738 it->end_charpos = p->end_charpos;
3739 it->string_nchars = p->string_nchars;
3740 it->area = p->area;
3741 it->multibyte_p = p->multibyte_p;
3742 it->space_width = p->space_width;
3743 it->font_height = p->font_height;
3744 it->voffset = p->voffset;
3745 it->string_from_display_prop_p = p->string_from_display_prop_p;
3746 }
3747
3748
3749 \f
3750 /***********************************************************************
3751 Moving over lines
3752 ***********************************************************************/
3753
3754 /* Set IT's current position to the previous line start. */
3755
3756 static void
3757 back_to_previous_line_start (it)
3758 struct it *it;
3759 {
3760 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
3761 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3762 }
3763
3764
3765 /* Move IT to the next line start.
3766
3767 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
3768 we skipped over part of the text (as opposed to moving the iterator
3769 continuously over the text). Otherwise, don't change the value
3770 of *SKIPPED_P.
3771
3772 Newlines may come from buffer text, overlay strings, or strings
3773 displayed via the `display' property. That's the reason we can't
3774 simply use find_next_newline_no_quit.
3775
3776 Note that this function may not skip over invisible text that is so
3777 because of text properties and immediately follows a newline. If
3778 it would, function reseat_at_next_visible_line_start, when called
3779 from set_iterator_to_next, would effectively make invisible
3780 characters following a newline part of the wrong glyph row, which
3781 leads to wrong cursor motion. */
3782
3783 static int
3784 forward_to_next_line_start (it, skipped_p)
3785 struct it *it;
3786 int *skipped_p;
3787 {
3788 int old_selective, newline_found_p, n;
3789 const int MAX_NEWLINE_DISTANCE = 500;
3790
3791 /* If already on a newline, just consume it to avoid unintended
3792 skipping over invisible text below. */
3793 if (it->what == IT_CHARACTER
3794 && it->c == '\n'
3795 && CHARPOS (it->position) == IT_CHARPOS (*it))
3796 {
3797 set_iterator_to_next (it, 0);
3798 it->c = 0;
3799 return 1;
3800 }
3801
3802 /* Don't handle selective display in the following. It's (a)
3803 unnecessary because it's done by the caller, and (b) leads to an
3804 infinite recursion because next_element_from_ellipsis indirectly
3805 calls this function. */
3806 old_selective = it->selective;
3807 it->selective = 0;
3808
3809 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
3810 from buffer text. */
3811 for (n = newline_found_p = 0;
3812 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
3813 n += STRINGP (it->string) ? 0 : 1)
3814 {
3815 if (!get_next_display_element (it))
3816 break;
3817 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
3818 set_iterator_to_next (it, 0);
3819 }
3820
3821 /* If we didn't find a newline near enough, see if we can use a
3822 short-cut. */
3823 if (n == MAX_NEWLINE_DISTANCE)
3824 {
3825 int start = IT_CHARPOS (*it);
3826 int limit = find_next_newline_no_quit (start, 1);
3827 Lisp_Object pos;
3828
3829 xassert (!STRINGP (it->string));
3830
3831 /* If there isn't any `display' property in sight, and no
3832 overlays, we can just use the position of the newline in
3833 buffer text. */
3834 if (it->stop_charpos >= limit
3835 || ((pos = Fnext_single_property_change (make_number (start),
3836 Qdisplay,
3837 Qnil, make_number (limit)),
3838 NILP (pos))
3839 && next_overlay_change (start) == ZV))
3840 {
3841 IT_CHARPOS (*it) = limit;
3842 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
3843 *skipped_p = newline_found_p = 1;
3844 }
3845 else
3846 {
3847 while (get_next_display_element (it)
3848 && !newline_found_p)
3849 {
3850 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
3851 set_iterator_to_next (it, 0);
3852 }
3853 }
3854 }
3855
3856 it->selective = old_selective;
3857 return newline_found_p;
3858 }
3859
3860
3861 /* Set IT's current position to the previous visible line start. Skip
3862 invisible text that is so either due to text properties or due to
3863 selective display. Caution: this does not change IT->current_x and
3864 IT->hpos. */
3865
3866 static void
3867 back_to_previous_visible_line_start (it)
3868 struct it *it;
3869 {
3870 int visible_p = 0;
3871
3872 /* Go back one newline if not on BEGV already. */
3873 if (IT_CHARPOS (*it) > BEGV)
3874 back_to_previous_line_start (it);
3875
3876 /* Move over lines that are invisible because of selective display
3877 or text properties. */
3878 while (IT_CHARPOS (*it) > BEGV
3879 && !visible_p)
3880 {
3881 visible_p = 1;
3882
3883 /* If selective > 0, then lines indented more than that values
3884 are invisible. */
3885 if (it->selective > 0
3886 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3887 it->selective))
3888 visible_p = 0;
3889 else
3890 {
3891 Lisp_Object prop;
3892
3893 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
3894 Qinvisible, it->window);
3895 if (TEXT_PROP_MEANS_INVISIBLE (prop))
3896 visible_p = 0;
3897 }
3898
3899 /* Back one more newline if the current one is invisible. */
3900 if (!visible_p)
3901 back_to_previous_line_start (it);
3902 }
3903
3904 xassert (IT_CHARPOS (*it) >= BEGV);
3905 xassert (IT_CHARPOS (*it) == BEGV
3906 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3907 CHECK_IT (it);
3908 }
3909
3910
3911 /* Reseat iterator IT at the previous visible line start. Skip
3912 invisible text that is so either due to text properties or due to
3913 selective display. At the end, update IT's overlay information,
3914 face information etc. */
3915
3916 static void
3917 reseat_at_previous_visible_line_start (it)
3918 struct it *it;
3919 {
3920 back_to_previous_visible_line_start (it);
3921 reseat (it, it->current.pos, 1);
3922 CHECK_IT (it);
3923 }
3924
3925
3926 /* Reseat iterator IT on the next visible line start in the current
3927 buffer. ON_NEWLINE_P non-zero means position IT on the newline
3928 preceding the line start. Skip over invisible text that is so
3929 because of selective display. Compute faces, overlays etc at the
3930 new position. Note that this function does not skip over text that
3931 is invisible because of text properties. */
3932
3933 static void
3934 reseat_at_next_visible_line_start (it, on_newline_p)
3935 struct it *it;
3936 int on_newline_p;
3937 {
3938 int newline_found_p, skipped_p = 0;
3939
3940 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3941
3942 /* Skip over lines that are invisible because they are indented
3943 more than the value of IT->selective. */
3944 if (it->selective > 0)
3945 while (IT_CHARPOS (*it) < ZV
3946 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3947 it->selective))
3948 {
3949 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3950 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3951 }
3952
3953 /* Position on the newline if that's what's requested. */
3954 if (on_newline_p && newline_found_p)
3955 {
3956 if (STRINGP (it->string))
3957 {
3958 if (IT_STRING_CHARPOS (*it) > 0)
3959 {
3960 --IT_STRING_CHARPOS (*it);
3961 --IT_STRING_BYTEPOS (*it);
3962 }
3963 }
3964 else if (IT_CHARPOS (*it) > BEGV)
3965 {
3966 --IT_CHARPOS (*it);
3967 --IT_BYTEPOS (*it);
3968 reseat (it, it->current.pos, 0);
3969 }
3970 }
3971 else if (skipped_p)
3972 reseat (it, it->current.pos, 0);
3973
3974 CHECK_IT (it);
3975 }
3976
3977
3978 \f
3979 /***********************************************************************
3980 Changing an iterator's position
3981 ***********************************************************************/
3982
3983 /* Change IT's current position to POS in current_buffer. If FORCE_P
3984 is non-zero, always check for text properties at the new position.
3985 Otherwise, text properties are only looked up if POS >=
3986 IT->check_charpos of a property. */
3987
3988 static void
3989 reseat (it, pos, force_p)
3990 struct it *it;
3991 struct text_pos pos;
3992 int force_p;
3993 {
3994 int original_pos = IT_CHARPOS (*it);
3995
3996 reseat_1 (it, pos, 0);
3997
3998 /* Determine where to check text properties. Avoid doing it
3999 where possible because text property lookup is very expensive. */
4000 if (force_p
4001 || CHARPOS (pos) > it->stop_charpos
4002 || CHARPOS (pos) < original_pos)
4003 handle_stop (it);
4004
4005 CHECK_IT (it);
4006 }
4007
4008
4009 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4010 IT->stop_pos to POS, also. */
4011
4012 static void
4013 reseat_1 (it, pos, set_stop_p)
4014 struct it *it;
4015 struct text_pos pos;
4016 int set_stop_p;
4017 {
4018 /* Don't call this function when scanning a C string. */
4019 xassert (it->s == NULL);
4020
4021 /* POS must be a reasonable value. */
4022 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4023
4024 it->current.pos = it->position = pos;
4025 XSETBUFFER (it->object, current_buffer);
4026 it->end_charpos = ZV;
4027 it->dpvec = NULL;
4028 it->current.dpvec_index = -1;
4029 it->current.overlay_string_index = -1;
4030 IT_STRING_CHARPOS (*it) = -1;
4031 IT_STRING_BYTEPOS (*it) = -1;
4032 it->string = Qnil;
4033 it->method = next_element_from_buffer;
4034 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
4035 it->sp = 0;
4036 it->face_before_selective_p = 0;
4037
4038 if (set_stop_p)
4039 it->stop_charpos = CHARPOS (pos);
4040 }
4041
4042
4043 /* Set up IT for displaying a string, starting at CHARPOS in window W.
4044 If S is non-null, it is a C string to iterate over. Otherwise,
4045 STRING gives a Lisp string to iterate over.
4046
4047 If PRECISION > 0, don't return more then PRECISION number of
4048 characters from the string.
4049
4050 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4051 characters have been returned. FIELD_WIDTH < 0 means an infinite
4052 field width.
4053
4054 MULTIBYTE = 0 means disable processing of multibyte characters,
4055 MULTIBYTE > 0 means enable it,
4056 MULTIBYTE < 0 means use IT->multibyte_p.
4057
4058 IT must be initialized via a prior call to init_iterator before
4059 calling this function. */
4060
4061 static void
4062 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4063 struct it *it;
4064 unsigned char *s;
4065 Lisp_Object string;
4066 int charpos;
4067 int precision, field_width, multibyte;
4068 {
4069 /* No region in strings. */
4070 it->region_beg_charpos = it->region_end_charpos = -1;
4071
4072 /* No text property checks performed by default, but see below. */
4073 it->stop_charpos = -1;
4074
4075 /* Set iterator position and end position. */
4076 bzero (&it->current, sizeof it->current);
4077 it->current.overlay_string_index = -1;
4078 it->current.dpvec_index = -1;
4079 xassert (charpos >= 0);
4080
4081 /* If STRING is specified, use its multibyteness, otherwise use the
4082 setting of MULTIBYTE, if specified. */
4083 if (multibyte >= 0)
4084 it->multibyte_p = multibyte > 0;
4085
4086 if (s == NULL)
4087 {
4088 xassert (STRINGP (string));
4089 it->string = string;
4090 it->s = NULL;
4091 it->end_charpos = it->string_nchars = XSTRING (string)->size;
4092 it->method = next_element_from_string;
4093 it->current.string_pos = string_pos (charpos, string);
4094 }
4095 else
4096 {
4097 it->s = s;
4098 it->string = Qnil;
4099
4100 /* Note that we use IT->current.pos, not it->current.string_pos,
4101 for displaying C strings. */
4102 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4103 if (it->multibyte_p)
4104 {
4105 it->current.pos = c_string_pos (charpos, s, 1);
4106 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4107 }
4108 else
4109 {
4110 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4111 it->end_charpos = it->string_nchars = strlen (s);
4112 }
4113
4114 it->method = next_element_from_c_string;
4115 }
4116
4117 /* PRECISION > 0 means don't return more than PRECISION characters
4118 from the string. */
4119 if (precision > 0 && it->end_charpos - charpos > precision)
4120 it->end_charpos = it->string_nchars = charpos + precision;
4121
4122 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4123 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4124 FIELD_WIDTH < 0 means infinite field width. This is useful for
4125 padding with `-' at the end of a mode line. */
4126 if (field_width < 0)
4127 field_width = INFINITY;
4128 if (field_width > it->end_charpos - charpos)
4129 it->end_charpos = charpos + field_width;
4130
4131 /* Use the standard display table for displaying strings. */
4132 if (DISP_TABLE_P (Vstandard_display_table))
4133 it->dp = XCHAR_TABLE (Vstandard_display_table);
4134
4135 it->stop_charpos = charpos;
4136 CHECK_IT (it);
4137 }
4138
4139
4140 \f
4141 /***********************************************************************
4142 Iteration
4143 ***********************************************************************/
4144
4145 /* Load IT's display element fields with information about the next
4146 display element from the current position of IT. Value is zero if
4147 end of buffer (or C string) is reached. */
4148
4149 int
4150 get_next_display_element (it)
4151 struct it *it;
4152 {
4153 /* Non-zero means that we found an display element. Zero means that
4154 we hit the end of what we iterate over. Performance note: the
4155 function pointer `method' used here turns out to be faster than
4156 using a sequence of if-statements. */
4157 int success_p = (*it->method) (it);
4158
4159 if (it->what == IT_CHARACTER)
4160 {
4161 /* Map via display table or translate control characters.
4162 IT->c, IT->len etc. have been set to the next character by
4163 the function call above. If we have a display table, and it
4164 contains an entry for IT->c, translate it. Don't do this if
4165 IT->c itself comes from a display table, otherwise we could
4166 end up in an infinite recursion. (An alternative could be to
4167 count the recursion depth of this function and signal an
4168 error when a certain maximum depth is reached.) Is it worth
4169 it? */
4170 if (success_p && it->dpvec == NULL)
4171 {
4172 Lisp_Object dv;
4173
4174 if (it->dp
4175 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
4176 VECTORP (dv)))
4177 {
4178 struct Lisp_Vector *v = XVECTOR (dv);
4179
4180 /* Return the first character from the display table
4181 entry, if not empty. If empty, don't display the
4182 current character. */
4183 if (v->size)
4184 {
4185 it->dpvec_char_len = it->len;
4186 it->dpvec = v->contents;
4187 it->dpend = v->contents + v->size;
4188 it->current.dpvec_index = 0;
4189 it->method = next_element_from_display_vector;
4190 success_p = get_next_display_element (it);
4191 }
4192 else
4193 {
4194 set_iterator_to_next (it, 0);
4195 success_p = get_next_display_element (it);
4196 }
4197 }
4198
4199 /* Translate control characters into `\003' or `^C' form.
4200 Control characters coming from a display table entry are
4201 currently not translated because we use IT->dpvec to hold
4202 the translation. This could easily be changed but I
4203 don't believe that it is worth doing.
4204
4205 Non-printable multibyte characters are also translated
4206 octal form. */
4207 else if ((it->c < ' '
4208 && (it->area != TEXT_AREA
4209 || (it->c != '\n' && it->c != '\t')))
4210 || (it->c >= 127
4211 && it->len == 1)
4212 || !CHAR_PRINTABLE_P (it->c))
4213 {
4214 /* IT->c is a control character which must be displayed
4215 either as '\003' or as `^C' where the '\\' and '^'
4216 can be defined in the display table. Fill
4217 IT->ctl_chars with glyphs for what we have to
4218 display. Then, set IT->dpvec to these glyphs. */
4219 GLYPH g;
4220
4221 if (it->c < 128 && it->ctl_arrow_p)
4222 {
4223 /* Set IT->ctl_chars[0] to the glyph for `^'. */
4224 if (it->dp
4225 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
4226 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
4227 g = XINT (DISP_CTRL_GLYPH (it->dp));
4228 else
4229 g = FAST_MAKE_GLYPH ('^', 0);
4230 XSETINT (it->ctl_chars[0], g);
4231
4232 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
4233 XSETINT (it->ctl_chars[1], g);
4234
4235 /* Set up IT->dpvec and return first character from it. */
4236 it->dpvec_char_len = it->len;
4237 it->dpvec = it->ctl_chars;
4238 it->dpend = it->dpvec + 2;
4239 it->current.dpvec_index = 0;
4240 it->method = next_element_from_display_vector;
4241 get_next_display_element (it);
4242 }
4243 else
4244 {
4245 unsigned char str[MAX_MULTIBYTE_LENGTH];
4246 int len;
4247 int i;
4248 GLYPH escape_glyph;
4249
4250 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
4251 if (it->dp
4252 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
4253 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
4254 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
4255 else
4256 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
4257
4258 if (SINGLE_BYTE_CHAR_P (it->c))
4259 str[0] = it->c, len = 1;
4260 else
4261 {
4262 len = CHAR_STRING_NO_SIGNAL (it->c, str);
4263 if (len < 0)
4264 {
4265 /* It's an invalid character, which
4266 shouldn't happen actually, but due to
4267 bugs it may happen. Let's print the char
4268 as is, there's not much meaningful we can
4269 do with it. */
4270 str[0] = it->c;
4271 str[1] = it->c >> 8;
4272 str[2] = it->c >> 16;
4273 str[3] = it->c >> 24;
4274 len = 4;
4275 }
4276 }
4277
4278 for (i = 0; i < len; i++)
4279 {
4280 XSETINT (it->ctl_chars[i * 4], escape_glyph);
4281 /* Insert three more glyphs into IT->ctl_chars for
4282 the octal display of the character. */
4283 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
4284 XSETINT (it->ctl_chars[i * 4 + 1], g);
4285 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
4286 XSETINT (it->ctl_chars[i * 4 + 2], g);
4287 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
4288 XSETINT (it->ctl_chars[i * 4 + 3], g);
4289 }
4290
4291 /* Set up IT->dpvec and return the first character
4292 from it. */
4293 it->dpvec_char_len = it->len;
4294 it->dpvec = it->ctl_chars;
4295 it->dpend = it->dpvec + len * 4;
4296 it->current.dpvec_index = 0;
4297 it->method = next_element_from_display_vector;
4298 get_next_display_element (it);
4299 }
4300 }
4301 }
4302
4303 /* Adjust face id for a multibyte character. There are no
4304 multibyte character in unibyte text. */
4305 if (it->multibyte_p
4306 && success_p
4307 && FRAME_WINDOW_P (it->f))
4308 {
4309 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4310 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
4311 }
4312 }
4313
4314 /* Is this character the last one of a run of characters with
4315 box? If yes, set IT->end_of_box_run_p to 1. */
4316 if (it->face_box_p
4317 && it->s == NULL)
4318 {
4319 int face_id;
4320 struct face *face;
4321
4322 it->end_of_box_run_p
4323 = ((face_id = face_after_it_pos (it),
4324 face_id != it->face_id)
4325 && (face = FACE_FROM_ID (it->f, face_id),
4326 face->box == FACE_NO_BOX));
4327 }
4328
4329 /* Value is 0 if end of buffer or string reached. */
4330 return success_p;
4331 }
4332
4333
4334 /* Move IT to the next display element.
4335
4336 RESEAT_P non-zero means if called on a newline in buffer text,
4337 skip to the next visible line start.
4338
4339 Functions get_next_display_element and set_iterator_to_next are
4340 separate because I find this arrangement easier to handle than a
4341 get_next_display_element function that also increments IT's
4342 position. The way it is we can first look at an iterator's current
4343 display element, decide whether it fits on a line, and if it does,
4344 increment the iterator position. The other way around we probably
4345 would either need a flag indicating whether the iterator has to be
4346 incremented the next time, or we would have to implement a
4347 decrement position function which would not be easy to write. */
4348
4349 void
4350 set_iterator_to_next (it, reseat_p)
4351 struct it *it;
4352 int reseat_p;
4353 {
4354 /* Reset flags indicating start and end of a sequence of characters
4355 with box. Reset them at the start of this function because
4356 moving the iterator to a new position might set them. */
4357 it->start_of_box_run_p = it->end_of_box_run_p = 0;
4358
4359 if (it->method == next_element_from_buffer)
4360 {
4361 /* The current display element of IT is a character from
4362 current_buffer. Advance in the buffer, and maybe skip over
4363 invisible lines that are so because of selective display. */
4364 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
4365 reseat_at_next_visible_line_start (it, 0);
4366 else
4367 {
4368 xassert (it->len != 0);
4369 IT_BYTEPOS (*it) += it->len;
4370 IT_CHARPOS (*it) += 1;
4371 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
4372 }
4373 }
4374 else if (it->method == next_element_from_composition)
4375 {
4376 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
4377 if (STRINGP (it->string))
4378 {
4379 IT_STRING_BYTEPOS (*it) += it->len;
4380 IT_STRING_CHARPOS (*it) += it->cmp_len;
4381 it->method = next_element_from_string;
4382 goto consider_string_end;
4383 }
4384 else
4385 {
4386 IT_BYTEPOS (*it) += it->len;
4387 IT_CHARPOS (*it) += it->cmp_len;
4388 it->method = next_element_from_buffer;
4389 }
4390 }
4391 else if (it->method == next_element_from_c_string)
4392 {
4393 /* Current display element of IT is from a C string. */
4394 IT_BYTEPOS (*it) += it->len;
4395 IT_CHARPOS (*it) += 1;
4396 }
4397 else if (it->method == next_element_from_display_vector)
4398 {
4399 /* Current display element of IT is from a display table entry.
4400 Advance in the display table definition. Reset it to null if
4401 end reached, and continue with characters from buffers/
4402 strings. */
4403 ++it->current.dpvec_index;
4404
4405 /* Restore face of the iterator to what they were before the
4406 display vector entry (these entries may contain faces). */
4407 it->face_id = it->saved_face_id;
4408
4409 if (it->dpvec + it->current.dpvec_index == it->dpend)
4410 {
4411 if (it->s)
4412 it->method = next_element_from_c_string;
4413 else if (STRINGP (it->string))
4414 it->method = next_element_from_string;
4415 else
4416 it->method = next_element_from_buffer;
4417
4418 it->dpvec = NULL;
4419 it->current.dpvec_index = -1;
4420
4421 /* Skip over characters which were displayed via IT->dpvec. */
4422 if (it->dpvec_char_len < 0)
4423 reseat_at_next_visible_line_start (it, 1);
4424 else if (it->dpvec_char_len > 0)
4425 {
4426 it->len = it->dpvec_char_len;
4427 set_iterator_to_next (it, reseat_p);
4428 }
4429 }
4430 }
4431 else if (it->method == next_element_from_string)
4432 {
4433 /* Current display element is a character from a Lisp string. */
4434 xassert (it->s == NULL && STRINGP (it->string));
4435 IT_STRING_BYTEPOS (*it) += it->len;
4436 IT_STRING_CHARPOS (*it) += 1;
4437
4438 consider_string_end:
4439
4440 if (it->current.overlay_string_index >= 0)
4441 {
4442 /* IT->string is an overlay string. Advance to the
4443 next, if there is one. */
4444 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
4445 next_overlay_string (it);
4446 }
4447 else
4448 {
4449 /* IT->string is not an overlay string. If we reached
4450 its end, and there is something on IT->stack, proceed
4451 with what is on the stack. This can be either another
4452 string, this time an overlay string, or a buffer. */
4453 if (IT_STRING_CHARPOS (*it) == XSTRING (it->string)->size
4454 && it->sp > 0)
4455 {
4456 pop_it (it);
4457 if (!STRINGP (it->string))
4458 it->method = next_element_from_buffer;
4459 else
4460 goto consider_string_end;
4461 }
4462 }
4463 }
4464 else if (it->method == next_element_from_image
4465 || it->method == next_element_from_stretch)
4466 {
4467 /* The position etc with which we have to proceed are on
4468 the stack. The position may be at the end of a string,
4469 if the `display' property takes up the whole string. */
4470 pop_it (it);
4471 it->image_id = 0;
4472 if (STRINGP (it->string))
4473 {
4474 it->method = next_element_from_string;
4475 goto consider_string_end;
4476 }
4477 else
4478 it->method = next_element_from_buffer;
4479 }
4480 else
4481 /* There are no other methods defined, so this should be a bug. */
4482 abort ();
4483
4484 xassert (it->method != next_element_from_string
4485 || (STRINGP (it->string)
4486 && IT_STRING_CHARPOS (*it) >= 0));
4487 }
4488
4489
4490 /* Load IT's display element fields with information about the next
4491 display element which comes from a display table entry or from the
4492 result of translating a control character to one of the forms `^C'
4493 or `\003'. IT->dpvec holds the glyphs to return as characters. */
4494
4495 static int
4496 next_element_from_display_vector (it)
4497 struct it *it;
4498 {
4499 /* Precondition. */
4500 xassert (it->dpvec && it->current.dpvec_index >= 0);
4501
4502 /* Remember the current face id in case glyphs specify faces.
4503 IT's face is restored in set_iterator_to_next. */
4504 it->saved_face_id = it->face_id;
4505
4506 if (INTEGERP (*it->dpvec)
4507 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
4508 {
4509 int lface_id;
4510 GLYPH g;
4511
4512 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
4513 it->c = FAST_GLYPH_CHAR (g);
4514 it->len = CHAR_BYTES (it->c);
4515
4516 /* The entry may contain a face id to use. Such a face id is
4517 the id of a Lisp face, not a realized face. A face id of
4518 zero means no face is specified. */
4519 lface_id = FAST_GLYPH_FACE (g);
4520 if (lface_id)
4521 {
4522 /* The function returns -1 if lface_id is invalid. */
4523 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
4524 if (face_id >= 0)
4525 it->face_id = face_id;
4526 }
4527 }
4528 else
4529 /* Display table entry is invalid. Return a space. */
4530 it->c = ' ', it->len = 1;
4531
4532 /* Don't change position and object of the iterator here. They are
4533 still the values of the character that had this display table
4534 entry or was translated, and that's what we want. */
4535 it->what = IT_CHARACTER;
4536 return 1;
4537 }
4538
4539
4540 /* Load IT with the next display element from Lisp string IT->string.
4541 IT->current.string_pos is the current position within the string.
4542 If IT->current.overlay_string_index >= 0, the Lisp string is an
4543 overlay string. */
4544
4545 static int
4546 next_element_from_string (it)
4547 struct it *it;
4548 {
4549 struct text_pos position;
4550
4551 xassert (STRINGP (it->string));
4552 xassert (IT_STRING_CHARPOS (*it) >= 0);
4553 position = it->current.string_pos;
4554
4555 /* Time to check for invisible text? */
4556 if (IT_STRING_CHARPOS (*it) < it->end_charpos
4557 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
4558 {
4559 handle_stop (it);
4560
4561 /* Since a handler may have changed IT->method, we must
4562 recurse here. */
4563 return get_next_display_element (it);
4564 }
4565
4566 if (it->current.overlay_string_index >= 0)
4567 {
4568 /* Get the next character from an overlay string. In overlay
4569 strings, There is no field width or padding with spaces to
4570 do. */
4571 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
4572 {
4573 it->what = IT_EOB;
4574 return 0;
4575 }
4576 else if (STRING_MULTIBYTE (it->string))
4577 {
4578 int remaining = (STRING_BYTES (XSTRING (it->string))
4579 - IT_STRING_BYTEPOS (*it));
4580 unsigned char *s = (XSTRING (it->string)->data
4581 + IT_STRING_BYTEPOS (*it));
4582 it->c = string_char_and_length (s, remaining, &it->len);
4583 }
4584 else
4585 {
4586 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
4587 it->len = 1;
4588 }
4589 }
4590 else
4591 {
4592 /* Get the next character from a Lisp string that is not an
4593 overlay string. Such strings come from the mode line, for
4594 example. We may have to pad with spaces, or truncate the
4595 string. See also next_element_from_c_string. */
4596 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
4597 {
4598 it->what = IT_EOB;
4599 return 0;
4600 }
4601 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
4602 {
4603 /* Pad with spaces. */
4604 it->c = ' ', it->len = 1;
4605 CHARPOS (position) = BYTEPOS (position) = -1;
4606 }
4607 else if (STRING_MULTIBYTE (it->string))
4608 {
4609 int maxlen = (STRING_BYTES (XSTRING (it->string))
4610 - IT_STRING_BYTEPOS (*it));
4611 unsigned char *s = (XSTRING (it->string)->data
4612 + IT_STRING_BYTEPOS (*it));
4613 it->c = string_char_and_length (s, maxlen, &it->len);
4614 }
4615 else
4616 {
4617 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
4618 it->len = 1;
4619 }
4620 }
4621
4622 /* Record what we have and where it came from. Note that we store a
4623 buffer position in IT->position although it could arguably be a
4624 string position. */
4625 it->what = IT_CHARACTER;
4626 it->object = it->string;
4627 it->position = position;
4628 return 1;
4629 }
4630
4631
4632 /* Load IT with next display element from C string IT->s.
4633 IT->string_nchars is the maximum number of characters to return
4634 from the string. IT->end_charpos may be greater than
4635 IT->string_nchars when this function is called, in which case we
4636 may have to return padding spaces. Value is zero if end of string
4637 reached, including padding spaces. */
4638
4639 static int
4640 next_element_from_c_string (it)
4641 struct it *it;
4642 {
4643 int success_p = 1;
4644
4645 xassert (it->s);
4646 it->what = IT_CHARACTER;
4647 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
4648 it->object = Qnil;
4649
4650 /* IT's position can be greater IT->string_nchars in case a field
4651 width or precision has been specified when the iterator was
4652 initialized. */
4653 if (IT_CHARPOS (*it) >= it->end_charpos)
4654 {
4655 /* End of the game. */
4656 it->what = IT_EOB;
4657 success_p = 0;
4658 }
4659 else if (IT_CHARPOS (*it) >= it->string_nchars)
4660 {
4661 /* Pad with spaces. */
4662 it->c = ' ', it->len = 1;
4663 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
4664 }
4665 else if (it->multibyte_p)
4666 {
4667 /* Implementation note: The calls to strlen apparently aren't a
4668 performance problem because there is no noticeable performance
4669 difference between Emacs running in unibyte or multibyte mode. */
4670 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
4671 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
4672 maxlen, &it->len);
4673 }
4674 else
4675 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
4676
4677 return success_p;
4678 }
4679
4680
4681 /* Set up IT to return characters from an ellipsis, if appropriate.
4682 The definition of the ellipsis glyphs may come from a display table
4683 entry. This function Fills IT with the first glyph from the
4684 ellipsis if an ellipsis is to be displayed. */
4685
4686 static int
4687 next_element_from_ellipsis (it)
4688 struct it *it;
4689 {
4690 if (it->selective_display_ellipsis_p)
4691 {
4692 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4693 {
4694 /* Use the display table definition for `...'. Invalid glyphs
4695 will be handled by the method returning elements from dpvec. */
4696 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4697 it->dpvec_char_len = it->len;
4698 it->dpvec = v->contents;
4699 it->dpend = v->contents + v->size;
4700 it->current.dpvec_index = 0;
4701 it->method = next_element_from_display_vector;
4702 }
4703 else
4704 {
4705 /* Use default `...' which is stored in default_invis_vector. */
4706 it->dpvec_char_len = it->len;
4707 it->dpvec = default_invis_vector;
4708 it->dpend = default_invis_vector + 3;
4709 it->current.dpvec_index = 0;
4710 it->method = next_element_from_display_vector;
4711 }
4712 }
4713 else
4714 {
4715 /* The face at the current position may be different from the
4716 face we find after the invisible text. Remember what it
4717 was in IT->saved_face_id, and signal that it's there by
4718 setting face_before_selective_p. */
4719 it->saved_face_id = it->face_id;
4720 it->method = next_element_from_buffer;
4721 reseat_at_next_visible_line_start (it, 1);
4722 it->face_before_selective_p = 1;
4723 }
4724
4725 return get_next_display_element (it);
4726 }
4727
4728
4729 /* Deliver an image display element. The iterator IT is already
4730 filled with image information (done in handle_display_prop). Value
4731 is always 1. */
4732
4733
4734 static int
4735 next_element_from_image (it)
4736 struct it *it;
4737 {
4738 it->what = IT_IMAGE;
4739 return 1;
4740 }
4741
4742
4743 /* Fill iterator IT with next display element from a stretch glyph
4744 property. IT->object is the value of the text property. Value is
4745 always 1. */
4746
4747 static int
4748 next_element_from_stretch (it)
4749 struct it *it;
4750 {
4751 it->what = IT_STRETCH;
4752 return 1;
4753 }
4754
4755
4756 /* Load IT with the next display element from current_buffer. Value
4757 is zero if end of buffer reached. IT->stop_charpos is the next
4758 position at which to stop and check for text properties or buffer
4759 end. */
4760
4761 static int
4762 next_element_from_buffer (it)
4763 struct it *it;
4764 {
4765 int success_p = 1;
4766
4767 /* Check this assumption, otherwise, we would never enter the
4768 if-statement, below. */
4769 xassert (IT_CHARPOS (*it) >= BEGV
4770 && IT_CHARPOS (*it) <= it->stop_charpos);
4771
4772 if (IT_CHARPOS (*it) >= it->stop_charpos)
4773 {
4774 if (IT_CHARPOS (*it) >= it->end_charpos)
4775 {
4776 int overlay_strings_follow_p;
4777
4778 /* End of the game, except when overlay strings follow that
4779 haven't been returned yet. */
4780 if (it->overlay_strings_at_end_processed_p)
4781 overlay_strings_follow_p = 0;
4782 else
4783 {
4784 it->overlay_strings_at_end_processed_p = 1;
4785 overlay_strings_follow_p = get_overlay_strings (it, 0);
4786 }
4787
4788 if (overlay_strings_follow_p)
4789 success_p = get_next_display_element (it);
4790 else
4791 {
4792 it->what = IT_EOB;
4793 it->position = it->current.pos;
4794 success_p = 0;
4795 }
4796 }
4797 else
4798 {
4799 handle_stop (it);
4800 return get_next_display_element (it);
4801 }
4802 }
4803 else
4804 {
4805 /* No face changes, overlays etc. in sight, so just return a
4806 character from current_buffer. */
4807 unsigned char *p;
4808
4809 /* Maybe run the redisplay end trigger hook. Performance note:
4810 This doesn't seem to cost measurable time. */
4811 if (it->redisplay_end_trigger_charpos
4812 && it->glyph_row
4813 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
4814 run_redisplay_end_trigger_hook (it);
4815
4816 /* Get the next character, maybe multibyte. */
4817 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
4818 if (it->multibyte_p && !ASCII_BYTE_P (*p))
4819 {
4820 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
4821 - IT_BYTEPOS (*it));
4822 it->c = string_char_and_length (p, maxlen, &it->len);
4823 }
4824 else
4825 it->c = *p, it->len = 1;
4826
4827 /* Record what we have and where it came from. */
4828 it->what = IT_CHARACTER;;
4829 it->object = it->w->buffer;
4830 it->position = it->current.pos;
4831
4832 /* Normally we return the character found above, except when we
4833 really want to return an ellipsis for selective display. */
4834 if (it->selective)
4835 {
4836 if (it->c == '\n')
4837 {
4838 /* A value of selective > 0 means hide lines indented more
4839 than that number of columns. */
4840 if (it->selective > 0
4841 && IT_CHARPOS (*it) + 1 < ZV
4842 && indented_beyond_p (IT_CHARPOS (*it) + 1,
4843 IT_BYTEPOS (*it) + 1,
4844 it->selective))
4845 {
4846 success_p = next_element_from_ellipsis (it);
4847 it->dpvec_char_len = -1;
4848 }
4849 }
4850 else if (it->c == '\r' && it->selective == -1)
4851 {
4852 /* A value of selective == -1 means that everything from the
4853 CR to the end of the line is invisible, with maybe an
4854 ellipsis displayed for it. */
4855 success_p = next_element_from_ellipsis (it);
4856 it->dpvec_char_len = -1;
4857 }
4858 }
4859 }
4860
4861 /* Value is zero if end of buffer reached. */
4862 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
4863 return success_p;
4864 }
4865
4866
4867 /* Run the redisplay end trigger hook for IT. */
4868
4869 static void
4870 run_redisplay_end_trigger_hook (it)
4871 struct it *it;
4872 {
4873 Lisp_Object args[3];
4874
4875 /* IT->glyph_row should be non-null, i.e. we should be actually
4876 displaying something, or otherwise we should not run the hook. */
4877 xassert (it->glyph_row);
4878
4879 /* Set up hook arguments. */
4880 args[0] = Qredisplay_end_trigger_functions;
4881 args[1] = it->window;
4882 XSETINT (args[2], it->redisplay_end_trigger_charpos);
4883 it->redisplay_end_trigger_charpos = 0;
4884
4885 /* Since we are *trying* to run these functions, don't try to run
4886 them again, even if they get an error. */
4887 it->w->redisplay_end_trigger = Qnil;
4888 Frun_hook_with_args (3, args);
4889
4890 /* Notice if it changed the face of the character we are on. */
4891 handle_face_prop (it);
4892 }
4893
4894
4895 /* Deliver a composition display element. The iterator IT is already
4896 filled with composition information (done in
4897 handle_composition_prop). Value is always 1. */
4898
4899 static int
4900 next_element_from_composition (it)
4901 struct it *it;
4902 {
4903 it->what = IT_COMPOSITION;
4904 it->position = (STRINGP (it->string)
4905 ? it->current.string_pos
4906 : it->current.pos);
4907 return 1;
4908 }
4909
4910
4911 \f
4912 /***********************************************************************
4913 Moving an iterator without producing glyphs
4914 ***********************************************************************/
4915
4916 /* Move iterator IT to a specified buffer or X position within one
4917 line on the display without producing glyphs.
4918
4919 Begin to skip at IT's current position. Skip to TO_CHARPOS or TO_X
4920 whichever is reached first.
4921
4922 TO_CHARPOS <= 0 means no TO_CHARPOS is specified.
4923
4924 TO_X < 0 means that no TO_X is specified. TO_X is normally a value
4925 0 <= TO_X <= IT->last_visible_x. This means in particular, that
4926 TO_X includes the amount by which a window is horizontally
4927 scrolled.
4928
4929 Value is
4930
4931 MOVE_POS_MATCH_OR_ZV
4932 - when TO_POS or ZV was reached.
4933
4934 MOVE_X_REACHED
4935 -when TO_X was reached before TO_POS or ZV were reached.
4936
4937 MOVE_LINE_CONTINUED
4938 - when we reached the end of the display area and the line must
4939 be continued.
4940
4941 MOVE_LINE_TRUNCATED
4942 - when we reached the end of the display area and the line is
4943 truncated.
4944
4945 MOVE_NEWLINE_OR_CR
4946 - when we stopped at a line end, i.e. a newline or a CR and selective
4947 display is on. */
4948
4949 static enum move_it_result
4950 move_it_in_display_line_to (it, to_charpos, to_x, op)
4951 struct it *it;
4952 int to_charpos, to_x, op;
4953 {
4954 enum move_it_result result = MOVE_UNDEFINED;
4955 struct glyph_row *saved_glyph_row;
4956
4957 /* Don't produce glyphs in produce_glyphs. */
4958 saved_glyph_row = it->glyph_row;
4959 it->glyph_row = NULL;
4960
4961 while (1)
4962 {
4963 int x, i, ascent = 0, descent = 0;
4964
4965 /* Stop when ZV or TO_CHARPOS reached. */
4966 if (!get_next_display_element (it)
4967 || ((op & MOVE_TO_POS) != 0
4968 && BUFFERP (it->object)
4969 && IT_CHARPOS (*it) >= to_charpos))
4970 {
4971 result = MOVE_POS_MATCH_OR_ZV;
4972 break;
4973 }
4974
4975 /* The call to produce_glyphs will get the metrics of the
4976 display element IT is loaded with. We record in x the
4977 x-position before this display element in case it does not
4978 fit on the line. */
4979 x = it->current_x;
4980
4981 /* Remember the line height so far in case the next element doesn't
4982 fit on the line. */
4983 if (!it->truncate_lines_p)
4984 {
4985 ascent = it->max_ascent;
4986 descent = it->max_descent;
4987 }
4988
4989 PRODUCE_GLYPHS (it);
4990
4991 if (it->area != TEXT_AREA)
4992 {
4993 set_iterator_to_next (it, 1);
4994 continue;
4995 }
4996
4997 /* The number of glyphs we get back in IT->nglyphs will normally
4998 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
4999 character on a terminal frame, or (iii) a line end. For the
5000 second case, IT->nglyphs - 1 padding glyphs will be present
5001 (on X frames, there is only one glyph produced for a
5002 composite character.
5003
5004 The behavior implemented below means, for continuation lines,
5005 that as many spaces of a TAB as fit on the current line are
5006 displayed there. For terminal frames, as many glyphs of a
5007 multi-glyph character are displayed in the current line, too.
5008 This is what the old redisplay code did, and we keep it that
5009 way. Under X, the whole shape of a complex character must
5010 fit on the line or it will be completely displayed in the
5011 next line.
5012
5013 Note that both for tabs and padding glyphs, all glyphs have
5014 the same width. */
5015 if (it->nglyphs)
5016 {
5017 /* More than one glyph or glyph doesn't fit on line. All
5018 glyphs have the same width. */
5019 int single_glyph_width = it->pixel_width / it->nglyphs;
5020 int new_x;
5021
5022 for (i = 0; i < it->nglyphs; ++i, x = new_x)
5023 {
5024 new_x = x + single_glyph_width;
5025
5026 /* We want to leave anything reaching TO_X to the caller. */
5027 if ((op & MOVE_TO_X) && new_x > to_x)
5028 {
5029 it->current_x = x;
5030 result = MOVE_X_REACHED;
5031 break;
5032 }
5033 else if (/* Lines are continued. */
5034 !it->truncate_lines_p
5035 && (/* And glyph doesn't fit on the line. */
5036 new_x > it->last_visible_x
5037 /* Or it fits exactly and we're on a window
5038 system frame. */
5039 || (new_x == it->last_visible_x
5040 && FRAME_WINDOW_P (it->f))))
5041 {
5042 if (/* IT->hpos == 0 means the very first glyph
5043 doesn't fit on the line, e.g. a wide image. */
5044 it->hpos == 0
5045 || (new_x == it->last_visible_x
5046 && FRAME_WINDOW_P (it->f)))
5047 {
5048 ++it->hpos;
5049 it->current_x = new_x;
5050 if (i == it->nglyphs - 1)
5051 set_iterator_to_next (it, 1);
5052 }
5053 else
5054 {
5055 it->current_x = x;
5056 it->max_ascent = ascent;
5057 it->max_descent = descent;
5058 }
5059
5060 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
5061 IT_CHARPOS (*it)));
5062 result = MOVE_LINE_CONTINUED;
5063 break;
5064 }
5065 else if (new_x > it->first_visible_x)
5066 {
5067 /* Glyph is visible. Increment number of glyphs that
5068 would be displayed. */
5069 ++it->hpos;
5070 }
5071 else
5072 {
5073 /* Glyph is completely off the left margin of the display
5074 area. Nothing to do. */
5075 }
5076 }
5077
5078 if (result != MOVE_UNDEFINED)
5079 break;
5080 }
5081 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
5082 {
5083 /* Stop when TO_X specified and reached. This check is
5084 necessary here because of lines consisting of a line end,
5085 only. The line end will not produce any glyphs and we
5086 would never get MOVE_X_REACHED. */
5087 xassert (it->nglyphs == 0);
5088 result = MOVE_X_REACHED;
5089 break;
5090 }
5091
5092 /* Is this a line end? If yes, we're done. */
5093 if (ITERATOR_AT_END_OF_LINE_P (it))
5094 {
5095 result = MOVE_NEWLINE_OR_CR;
5096 break;
5097 }
5098
5099 /* The current display element has been consumed. Advance
5100 to the next. */
5101 set_iterator_to_next (it, 1);
5102
5103 /* Stop if lines are truncated and IT's current x-position is
5104 past the right edge of the window now. */
5105 if (it->truncate_lines_p
5106 && it->current_x >= it->last_visible_x)
5107 {
5108 result = MOVE_LINE_TRUNCATED;
5109 break;
5110 }
5111 }
5112
5113 /* Restore the iterator settings altered at the beginning of this
5114 function. */
5115 it->glyph_row = saved_glyph_row;
5116 return result;
5117 }
5118
5119
5120 /* Move IT forward to a specified buffer position TO_CHARPOS, TO_X,
5121 TO_Y, TO_VPOS. OP is a bit-mask that specifies where to stop. See
5122 the description of enum move_operation_enum.
5123
5124 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
5125 screen line, this function will set IT to the next position >
5126 TO_CHARPOS. */
5127
5128 void
5129 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
5130 struct it *it;
5131 int to_charpos, to_x, to_y, to_vpos;
5132 int op;
5133 {
5134 enum move_it_result skip, skip2 = MOVE_X_REACHED;
5135 int line_height;
5136 int reached = 0;
5137
5138 for (;;)
5139 {
5140 if (op & MOVE_TO_VPOS)
5141 {
5142 /* If no TO_CHARPOS and no TO_X specified, stop at the
5143 start of the line TO_VPOS. */
5144 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
5145 {
5146 if (it->vpos == to_vpos)
5147 {
5148 reached = 1;
5149 break;
5150 }
5151 else
5152 skip = move_it_in_display_line_to (it, -1, -1, 0);
5153 }
5154 else
5155 {
5156 /* TO_VPOS >= 0 means stop at TO_X in the line at
5157 TO_VPOS, or at TO_POS, whichever comes first. */
5158 if (it->vpos == to_vpos)
5159 {
5160 reached = 2;
5161 break;
5162 }
5163
5164 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
5165
5166 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
5167 {
5168 reached = 3;
5169 break;
5170 }
5171 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
5172 {
5173 /* We have reached TO_X but not in the line we want. */
5174 skip = move_it_in_display_line_to (it, to_charpos,
5175 -1, MOVE_TO_POS);
5176 if (skip == MOVE_POS_MATCH_OR_ZV)
5177 {
5178 reached = 4;
5179 break;
5180 }
5181 }
5182 }
5183 }
5184 else if (op & MOVE_TO_Y)
5185 {
5186 struct it it_backup;
5187
5188 /* TO_Y specified means stop at TO_X in the line containing
5189 TO_Y---or at TO_CHARPOS if this is reached first. The
5190 problem is that we can't really tell whether the line
5191 contains TO_Y before we have completely scanned it, and
5192 this may skip past TO_X. What we do is to first scan to
5193 TO_X.
5194
5195 If TO_X is not specified, use a TO_X of zero. The reason
5196 is to make the outcome of this function more predictable.
5197 If we didn't use TO_X == 0, we would stop at the end of
5198 the line which is probably not what a caller would expect
5199 to happen. */
5200 skip = move_it_in_display_line_to (it, to_charpos,
5201 ((op & MOVE_TO_X)
5202 ? to_x : 0),
5203 (MOVE_TO_X
5204 | (op & MOVE_TO_POS)));
5205
5206 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
5207 if (skip == MOVE_POS_MATCH_OR_ZV)
5208 {
5209 reached = 5;
5210 break;
5211 }
5212
5213 /* If TO_X was reached, we would like to know whether TO_Y
5214 is in the line. This can only be said if we know the
5215 total line height which requires us to scan the rest of
5216 the line. */
5217 if (skip == MOVE_X_REACHED)
5218 {
5219 it_backup = *it;
5220 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
5221 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
5222 op & MOVE_TO_POS);
5223 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
5224 }
5225
5226 /* Now, decide whether TO_Y is in this line. */
5227 line_height = it->max_ascent + it->max_descent;
5228 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
5229
5230 if (to_y >= it->current_y
5231 && to_y < it->current_y + line_height)
5232 {
5233 if (skip == MOVE_X_REACHED)
5234 /* If TO_Y is in this line and TO_X was reached above,
5235 we scanned too far. We have to restore IT's settings
5236 to the ones before skipping. */
5237 *it = it_backup;
5238 reached = 6;
5239 }
5240 else if (skip == MOVE_X_REACHED)
5241 {
5242 skip = skip2;
5243 if (skip == MOVE_POS_MATCH_OR_ZV)
5244 reached = 7;
5245 }
5246
5247 if (reached)
5248 break;
5249 }
5250 else
5251 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
5252
5253 switch (skip)
5254 {
5255 case MOVE_POS_MATCH_OR_ZV:
5256 reached = 8;
5257 goto out;
5258
5259 case MOVE_NEWLINE_OR_CR:
5260 set_iterator_to_next (it, 1);
5261 it->continuation_lines_width = 0;
5262 break;
5263
5264 case MOVE_LINE_TRUNCATED:
5265 it->continuation_lines_width = 0;
5266 reseat_at_next_visible_line_start (it, 0);
5267 if ((op & MOVE_TO_POS) != 0
5268 && IT_CHARPOS (*it) > to_charpos)
5269 {
5270 reached = 9;
5271 goto out;
5272 }
5273 break;
5274
5275 case MOVE_LINE_CONTINUED:
5276 it->continuation_lines_width += it->current_x;
5277 break;
5278
5279 default:
5280 abort ();
5281 }
5282
5283 /* Reset/increment for the next run. */
5284 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
5285 it->current_x = it->hpos = 0;
5286 it->current_y += it->max_ascent + it->max_descent;
5287 ++it->vpos;
5288 last_height = it->max_ascent + it->max_descent;
5289 last_max_ascent = it->max_ascent;
5290 it->max_ascent = it->max_descent = 0;
5291 }
5292
5293 out:
5294
5295 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
5296 }
5297
5298
5299 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
5300
5301 If DY > 0, move IT backward at least that many pixels. DY = 0
5302 means move IT backward to the preceding line start or BEGV. This
5303 function may move over more than DY pixels if IT->current_y - DY
5304 ends up in the middle of a line; in this case IT->current_y will be
5305 set to the top of the line moved to. */
5306
5307 void
5308 move_it_vertically_backward (it, dy)
5309 struct it *it;
5310 int dy;
5311 {
5312 int nlines, h;
5313 struct it it2, it3;
5314 int start_pos = IT_CHARPOS (*it);
5315
5316 xassert (dy >= 0);
5317
5318 /* Estimate how many newlines we must move back. */
5319 nlines = max (1, dy / CANON_Y_UNIT (it->f));
5320
5321 /* Set the iterator's position that many lines back. */
5322 while (nlines-- && IT_CHARPOS (*it) > BEGV)
5323 back_to_previous_visible_line_start (it);
5324
5325 /* Reseat the iterator here. When moving backward, we don't want
5326 reseat to skip forward over invisible text, set up the iterator
5327 to deliver from overlay strings at the new position etc. So,
5328 use reseat_1 here. */
5329 reseat_1 (it, it->current.pos, 1);
5330
5331 /* We are now surely at a line start. */
5332 it->current_x = it->hpos = 0;
5333
5334 /* Move forward and see what y-distance we moved. First move to the
5335 start of the next line so that we get its height. We need this
5336 height to be able to tell whether we reached the specified
5337 y-distance. */
5338 it2 = *it;
5339 it2.max_ascent = it2.max_descent = 0;
5340 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
5341 MOVE_TO_POS | MOVE_TO_VPOS);
5342 xassert (IT_CHARPOS (*it) >= BEGV);
5343 it3 = it2;
5344
5345 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
5346 xassert (IT_CHARPOS (*it) >= BEGV);
5347 h = it2.current_y - it->current_y;
5348 nlines = it2.vpos - it->vpos;
5349
5350 /* Correct IT's y and vpos position. */
5351 it->vpos -= nlines;
5352 it->current_y -= h;
5353
5354 if (dy == 0)
5355 {
5356 /* DY == 0 means move to the start of the screen line. The
5357 value of nlines is > 0 if continuation lines were involved. */
5358 if (nlines > 0)
5359 move_it_by_lines (it, nlines, 1);
5360 xassert (IT_CHARPOS (*it) <= start_pos);
5361 }
5362 else if (nlines)
5363 {
5364 /* The y-position we try to reach. Note that h has been
5365 subtracted in front of the if-statement. */
5366 int target_y = it->current_y + h - dy;
5367 int y0 = it3.current_y;
5368 int y1 = line_bottom_y (&it3);
5369 int line_height = y1 - y0;
5370
5371 /* If we did not reach target_y, try to move further backward if
5372 we can. If we moved too far backward, try to move forward. */
5373 if (target_y < it->current_y
5374 /* This is heuristic. In a window that's 3 lines high, with
5375 a line height of 13 pixels each, recentering with point
5376 on the bottom line will try to move -39/2 = 19 pixels
5377 backward. Try to avoid moving into the first line. */
5378 && it->current_y - target_y > line_height / 3 * 2
5379 && IT_CHARPOS (*it) > BEGV)
5380 {
5381 move_it_vertically (it, target_y - it->current_y);
5382 xassert (IT_CHARPOS (*it) >= BEGV);
5383 }
5384 else if (target_y >= it->current_y + line_height
5385 && IT_CHARPOS (*it) < ZV)
5386 {
5387 move_it_vertically (it, target_y - (it->current_y + line_height));
5388 xassert (IT_CHARPOS (*it) >= BEGV);
5389 }
5390 }
5391 }
5392
5393
5394 /* Move IT by a specified amount of pixel lines DY. DY negative means
5395 move backwards. DY = 0 means move to start of screen line. At the
5396 end, IT will be on the start of a screen line. */
5397
5398 void
5399 move_it_vertically (it, dy)
5400 struct it *it;
5401 int dy;
5402 {
5403 if (dy <= 0)
5404 move_it_vertically_backward (it, -dy);
5405 else if (dy > 0)
5406 {
5407 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
5408 move_it_to (it, ZV, -1, it->current_y + dy, -1,
5409 MOVE_TO_POS | MOVE_TO_Y);
5410 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
5411
5412 /* If buffer ends in ZV without a newline, move to the start of
5413 the line to satisfy the post-condition. */
5414 if (IT_CHARPOS (*it) == ZV
5415 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
5416 move_it_by_lines (it, 0, 0);
5417 }
5418 }
5419
5420
5421 /* Move iterator IT past the end of the text line it is in. */
5422
5423 void
5424 move_it_past_eol (it)
5425 struct it *it;
5426 {
5427 enum move_it_result rc;
5428
5429 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
5430 if (rc == MOVE_NEWLINE_OR_CR)
5431 set_iterator_to_next (it, 0);
5432 }
5433
5434
5435 #if 0 /* Currently not used. */
5436
5437 /* Return non-zero if some text between buffer positions START_CHARPOS
5438 and END_CHARPOS is invisible. IT->window is the window for text
5439 property lookup. */
5440
5441 static int
5442 invisible_text_between_p (it, start_charpos, end_charpos)
5443 struct it *it;
5444 int start_charpos, end_charpos;
5445 {
5446 Lisp_Object prop, limit;
5447 int invisible_found_p;
5448
5449 xassert (it != NULL && start_charpos <= end_charpos);
5450
5451 /* Is text at START invisible? */
5452 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
5453 it->window);
5454 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5455 invisible_found_p = 1;
5456 else
5457 {
5458 limit = Fnext_single_char_property_change (make_number (start_charpos),
5459 Qinvisible, Qnil,
5460 make_number (end_charpos));
5461 invisible_found_p = XFASTINT (limit) < end_charpos;
5462 }
5463
5464 return invisible_found_p;
5465 }
5466
5467 #endif /* 0 */
5468
5469
5470 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
5471 negative means move up. DVPOS == 0 means move to the start of the
5472 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
5473 NEED_Y_P is zero, IT->current_y will be left unchanged.
5474
5475 Further optimization ideas: If we would know that IT->f doesn't use
5476 a face with proportional font, we could be faster for
5477 truncate-lines nil. */
5478
5479 void
5480 move_it_by_lines (it, dvpos, need_y_p)
5481 struct it *it;
5482 int dvpos, need_y_p;
5483 {
5484 struct position pos;
5485
5486 if (!FRAME_WINDOW_P (it->f))
5487 {
5488 struct text_pos textpos;
5489
5490 /* We can use vmotion on frames without proportional fonts. */
5491 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
5492 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
5493 reseat (it, textpos, 1);
5494 it->vpos += pos.vpos;
5495 it->current_y += pos.vpos;
5496 }
5497 else if (dvpos == 0)
5498 {
5499 /* DVPOS == 0 means move to the start of the screen line. */
5500 move_it_vertically_backward (it, 0);
5501 xassert (it->current_x == 0 && it->hpos == 0);
5502 }
5503 else if (dvpos > 0)
5504 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
5505 else
5506 {
5507 struct it it2;
5508 int start_charpos, i;
5509
5510 /* Start at the beginning of the screen line containing IT's
5511 position. */
5512 move_it_vertically_backward (it, 0);
5513
5514 /* Go back -DVPOS visible lines and reseat the iterator there. */
5515 start_charpos = IT_CHARPOS (*it);
5516 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
5517 back_to_previous_visible_line_start (it);
5518 reseat (it, it->current.pos, 1);
5519 it->current_x = it->hpos = 0;
5520
5521 /* Above call may have moved too far if continuation lines
5522 are involved. Scan forward and see if it did. */
5523 it2 = *it;
5524 it2.vpos = it2.current_y = 0;
5525 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
5526 it->vpos -= it2.vpos;
5527 it->current_y -= it2.current_y;
5528 it->current_x = it->hpos = 0;
5529
5530 /* If we moved too far, move IT some lines forward. */
5531 if (it2.vpos > -dvpos)
5532 {
5533 int delta = it2.vpos + dvpos;
5534 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
5535 }
5536 }
5537 }
5538
5539
5540 \f
5541 /***********************************************************************
5542 Messages
5543 ***********************************************************************/
5544
5545
5546 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
5547 to *Messages*. */
5548
5549 void
5550 add_to_log (format, arg1, arg2)
5551 char *format;
5552 Lisp_Object arg1, arg2;
5553 {
5554 Lisp_Object args[3];
5555 Lisp_Object msg, fmt;
5556 char *buffer;
5557 int len;
5558 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5559
5560 /* Do nothing if called asynchronously. Inserting text into
5561 a buffer may call after-change-functions and alike and
5562 that would means running Lisp asynchronously. */
5563 if (handling_signal)
5564 return;
5565
5566 fmt = msg = Qnil;
5567 GCPRO4 (fmt, msg, arg1, arg2);
5568
5569 args[0] = fmt = build_string (format);
5570 args[1] = arg1;
5571 args[2] = arg2;
5572 msg = Fformat (3, args);
5573
5574 len = STRING_BYTES (XSTRING (msg)) + 1;
5575 buffer = (char *) alloca (len);
5576 bcopy (XSTRING (msg)->data, buffer, len);
5577
5578 message_dolog (buffer, len - 1, 1, 0);
5579 UNGCPRO;
5580 }
5581
5582
5583 /* Output a newline in the *Messages* buffer if "needs" one. */
5584
5585 void
5586 message_log_maybe_newline ()
5587 {
5588 if (message_log_need_newline)
5589 message_dolog ("", 0, 1, 0);
5590 }
5591
5592
5593 /* Add a string M of length NBYTES to the message log, optionally
5594 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
5595 nonzero, means interpret the contents of M as multibyte. This
5596 function calls low-level routines in order to bypass text property
5597 hooks, etc. which might not be safe to run. */
5598
5599 void
5600 message_dolog (m, nbytes, nlflag, multibyte)
5601 char *m;
5602 int nbytes, nlflag, multibyte;
5603 {
5604 if (!NILP (Vmessage_log_max))
5605 {
5606 struct buffer *oldbuf;
5607 Lisp_Object oldpoint, oldbegv, oldzv;
5608 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5609 int point_at_end = 0;
5610 int zv_at_end = 0;
5611 Lisp_Object old_deactivate_mark, tem;
5612 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5613
5614 old_deactivate_mark = Vdeactivate_mark;
5615 oldbuf = current_buffer;
5616 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
5617 current_buffer->undo_list = Qt;
5618
5619 oldpoint = Fpoint_marker ();
5620 oldbegv = Fpoint_min_marker ();
5621 oldzv = Fpoint_max_marker ();
5622 GCPRO4 (oldpoint, oldbegv, oldzv, old_deactivate_mark);
5623
5624 if (PT == Z)
5625 point_at_end = 1;
5626 if (ZV == Z)
5627 zv_at_end = 1;
5628
5629 BEGV = BEG;
5630 BEGV_BYTE = BEG_BYTE;
5631 ZV = Z;
5632 ZV_BYTE = Z_BYTE;
5633 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5634
5635 /* Insert the string--maybe converting multibyte to single byte
5636 or vice versa, so that all the text fits the buffer. */
5637 if (multibyte
5638 && NILP (current_buffer->enable_multibyte_characters))
5639 {
5640 int i, c, char_bytes;
5641 unsigned char work[1];
5642
5643 /* Convert a multibyte string to single-byte
5644 for the *Message* buffer. */
5645 for (i = 0; i < nbytes; i += nbytes)
5646 {
5647 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
5648 work[0] = (SINGLE_BYTE_CHAR_P (c)
5649 ? c
5650 : multibyte_char_to_unibyte (c, Qnil));
5651 insert_1_both (work, 1, 1, 1, 0, 0);
5652 }
5653 }
5654 else if (! multibyte
5655 && ! NILP (current_buffer->enable_multibyte_characters))
5656 {
5657 int i, c, char_bytes;
5658 unsigned char *msg = (unsigned char *) m;
5659 unsigned char str[MAX_MULTIBYTE_LENGTH];
5660 /* Convert a single-byte string to multibyte
5661 for the *Message* buffer. */
5662 for (i = 0; i < nbytes; i++)
5663 {
5664 c = unibyte_char_to_multibyte (msg[i]);
5665 char_bytes = CHAR_STRING (c, str);
5666 insert_1_both (str, 1, char_bytes, 1, 0, 0);
5667 }
5668 }
5669 else if (nbytes)
5670 insert_1 (m, nbytes, 1, 0, 0);
5671
5672 if (nlflag)
5673 {
5674 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
5675 insert_1 ("\n", 1, 1, 0, 0);
5676
5677 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
5678 this_bol = PT;
5679 this_bol_byte = PT_BYTE;
5680
5681 if (this_bol > BEG)
5682 {
5683 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
5684 prev_bol = PT;
5685 prev_bol_byte = PT_BYTE;
5686
5687 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
5688 this_bol, this_bol_byte);
5689 if (dup)
5690 {
5691 del_range_both (prev_bol, prev_bol_byte,
5692 this_bol, this_bol_byte, 0);
5693 if (dup > 1)
5694 {
5695 char dupstr[40];
5696 int duplen;
5697
5698 /* If you change this format, don't forget to also
5699 change message_log_check_duplicate. */
5700 sprintf (dupstr, " [%d times]", dup);
5701 duplen = strlen (dupstr);
5702 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
5703 insert_1 (dupstr, duplen, 1, 0, 1);
5704 }
5705 }
5706 }
5707
5708 if (NATNUMP (Vmessage_log_max))
5709 {
5710 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
5711 -XFASTINT (Vmessage_log_max) - 1, 0);
5712 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
5713 }
5714 }
5715 BEGV = XMARKER (oldbegv)->charpos;
5716 BEGV_BYTE = marker_byte_position (oldbegv);
5717
5718 if (zv_at_end)
5719 {
5720 ZV = Z;
5721 ZV_BYTE = Z_BYTE;
5722 }
5723 else
5724 {
5725 ZV = XMARKER (oldzv)->charpos;
5726 ZV_BYTE = marker_byte_position (oldzv);
5727 }
5728
5729 if (point_at_end)
5730 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5731 else
5732 /* We can't do Fgoto_char (oldpoint) because it will run some
5733 Lisp code. */
5734 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
5735 XMARKER (oldpoint)->bytepos);
5736
5737 UNGCPRO;
5738 free_marker (oldpoint);
5739 free_marker (oldbegv);
5740 free_marker (oldzv);
5741
5742 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
5743 set_buffer_internal (oldbuf);
5744 if (NILP (tem))
5745 windows_or_buffers_changed = old_windows_or_buffers_changed;
5746 message_log_need_newline = !nlflag;
5747 Vdeactivate_mark = old_deactivate_mark;
5748 }
5749 }
5750
5751
5752 /* We are at the end of the buffer after just having inserted a newline.
5753 (Note: We depend on the fact we won't be crossing the gap.)
5754 Check to see if the most recent message looks a lot like the previous one.
5755 Return 0 if different, 1 if the new one should just replace it, or a
5756 value N > 1 if we should also append " [N times]". */
5757
5758 static int
5759 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
5760 int prev_bol, this_bol;
5761 int prev_bol_byte, this_bol_byte;
5762 {
5763 int i;
5764 int len = Z_BYTE - 1 - this_bol_byte;
5765 int seen_dots = 0;
5766 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
5767 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
5768
5769 for (i = 0; i < len; i++)
5770 {
5771 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
5772 seen_dots = 1;
5773 if (p1[i] != p2[i])
5774 return seen_dots;
5775 }
5776 p1 += len;
5777 if (*p1 == '\n')
5778 return 2;
5779 if (*p1++ == ' ' && *p1++ == '[')
5780 {
5781 int n = 0;
5782 while (*p1 >= '0' && *p1 <= '9')
5783 n = n * 10 + *p1++ - '0';
5784 if (strncmp (p1, " times]\n", 8) == 0)
5785 return n+1;
5786 }
5787 return 0;
5788 }
5789
5790
5791 /* Display an echo area message M with a specified length of NBYTES
5792 bytes. The string may include null characters. If M is 0, clear
5793 out any existing message, and let the mini-buffer text show
5794 through.
5795
5796 The buffer M must continue to exist until after the echo area gets
5797 cleared or some other message gets displayed there. This means do
5798 not pass text that is stored in a Lisp string; do not pass text in
5799 a buffer that was alloca'd. */
5800
5801 void
5802 message2 (m, nbytes, multibyte)
5803 char *m;
5804 int nbytes;
5805 int multibyte;
5806 {
5807 /* First flush out any partial line written with print. */
5808 message_log_maybe_newline ();
5809 if (m)
5810 message_dolog (m, nbytes, 1, multibyte);
5811 message2_nolog (m, nbytes, multibyte);
5812 }
5813
5814
5815 /* The non-logging counterpart of message2. */
5816
5817 void
5818 message2_nolog (m, nbytes, multibyte)
5819 char *m;
5820 int nbytes;
5821 {
5822 struct frame *sf = SELECTED_FRAME ();
5823 message_enable_multibyte = multibyte;
5824
5825 if (noninteractive)
5826 {
5827 if (noninteractive_need_newline)
5828 putc ('\n', stderr);
5829 noninteractive_need_newline = 0;
5830 if (m)
5831 fwrite (m, nbytes, 1, stderr);
5832 if (cursor_in_echo_area == 0)
5833 fprintf (stderr, "\n");
5834 fflush (stderr);
5835 }
5836 /* A null message buffer means that the frame hasn't really been
5837 initialized yet. Error messages get reported properly by
5838 cmd_error, so this must be just an informative message; toss it. */
5839 else if (INTERACTIVE
5840 && sf->glyphs_initialized_p
5841 && FRAME_MESSAGE_BUF (sf))
5842 {
5843 Lisp_Object mini_window;
5844 struct frame *f;
5845
5846 /* Get the frame containing the mini-buffer
5847 that the selected frame is using. */
5848 mini_window = FRAME_MINIBUF_WINDOW (sf);
5849 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5850
5851 FRAME_SAMPLE_VISIBILITY (f);
5852 if (FRAME_VISIBLE_P (sf)
5853 && ! FRAME_VISIBLE_P (f))
5854 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
5855
5856 if (m)
5857 {
5858 set_message (m, Qnil, nbytes, multibyte);
5859 if (minibuffer_auto_raise)
5860 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5861 }
5862 else
5863 clear_message (1, 1);
5864
5865 do_pending_window_change (0);
5866 echo_area_display (1);
5867 do_pending_window_change (0);
5868 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5869 (*frame_up_to_date_hook) (f);
5870 }
5871 }
5872
5873
5874 /* Display an echo area message M with a specified length of NBYTES
5875 bytes. The string may include null characters. If M is not a
5876 string, clear out any existing message, and let the mini-buffer
5877 text show through. */
5878
5879 void
5880 message3 (m, nbytes, multibyte)
5881 Lisp_Object m;
5882 int nbytes;
5883 int multibyte;
5884 {
5885 struct gcpro gcpro1;
5886
5887 GCPRO1 (m);
5888
5889 /* First flush out any partial line written with print. */
5890 message_log_maybe_newline ();
5891 if (STRINGP (m))
5892 message_dolog (XSTRING (m)->data, nbytes, 1, multibyte);
5893 message3_nolog (m, nbytes, multibyte);
5894
5895 UNGCPRO;
5896 }
5897
5898
5899 /* The non-logging version of message3. */
5900
5901 void
5902 message3_nolog (m, nbytes, multibyte)
5903 Lisp_Object m;
5904 int nbytes, multibyte;
5905 {
5906 struct frame *sf = SELECTED_FRAME ();
5907 message_enable_multibyte = multibyte;
5908
5909 if (noninteractive)
5910 {
5911 if (noninteractive_need_newline)
5912 putc ('\n', stderr);
5913 noninteractive_need_newline = 0;
5914 if (STRINGP (m))
5915 fwrite (XSTRING (m)->data, nbytes, 1, stderr);
5916 if (cursor_in_echo_area == 0)
5917 fprintf (stderr, "\n");
5918 fflush (stderr);
5919 }
5920 /* A null message buffer means that the frame hasn't really been
5921 initialized yet. Error messages get reported properly by
5922 cmd_error, so this must be just an informative message; toss it. */
5923 else if (INTERACTIVE
5924 && sf->glyphs_initialized_p
5925 && FRAME_MESSAGE_BUF (sf))
5926 {
5927 Lisp_Object mini_window;
5928 Lisp_Object frame;
5929 struct frame *f;
5930
5931 /* Get the frame containing the mini-buffer
5932 that the selected frame is using. */
5933 mini_window = FRAME_MINIBUF_WINDOW (sf);
5934 frame = XWINDOW (mini_window)->frame;
5935 f = XFRAME (frame);
5936
5937 FRAME_SAMPLE_VISIBILITY (f);
5938 if (FRAME_VISIBLE_P (sf)
5939 && !FRAME_VISIBLE_P (f))
5940 Fmake_frame_visible (frame);
5941
5942 if (STRINGP (m) && XSTRING (m)->size)
5943 {
5944 set_message (NULL, m, nbytes, multibyte);
5945 if (minibuffer_auto_raise)
5946 Fraise_frame (frame);
5947 }
5948 else
5949 clear_message (1, 1);
5950
5951 do_pending_window_change (0);
5952 echo_area_display (1);
5953 do_pending_window_change (0);
5954 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5955 (*frame_up_to_date_hook) (f);
5956 }
5957 }
5958
5959
5960 /* Display a null-terminated echo area message M. If M is 0, clear
5961 out any existing message, and let the mini-buffer text show through.
5962
5963 The buffer M must continue to exist until after the echo area gets
5964 cleared or some other message gets displayed there. Do not pass
5965 text that is stored in a Lisp string. Do not pass text in a buffer
5966 that was alloca'd. */
5967
5968 void
5969 message1 (m)
5970 char *m;
5971 {
5972 message2 (m, (m ? strlen (m) : 0), 0);
5973 }
5974
5975
5976 /* The non-logging counterpart of message1. */
5977
5978 void
5979 message1_nolog (m)
5980 char *m;
5981 {
5982 message2_nolog (m, (m ? strlen (m) : 0), 0);
5983 }
5984
5985 /* Display a message M which contains a single %s
5986 which gets replaced with STRING. */
5987
5988 void
5989 message_with_string (m, string, log)
5990 char *m;
5991 Lisp_Object string;
5992 int log;
5993 {
5994 if (noninteractive)
5995 {
5996 if (m)
5997 {
5998 if (noninteractive_need_newline)
5999 putc ('\n', stderr);
6000 noninteractive_need_newline = 0;
6001 fprintf (stderr, m, XSTRING (string)->data);
6002 if (cursor_in_echo_area == 0)
6003 fprintf (stderr, "\n");
6004 fflush (stderr);
6005 }
6006 }
6007 else if (INTERACTIVE)
6008 {
6009 /* The frame whose minibuffer we're going to display the message on.
6010 It may be larger than the selected frame, so we need
6011 to use its buffer, not the selected frame's buffer. */
6012 Lisp_Object mini_window;
6013 struct frame *f, *sf = SELECTED_FRAME ();
6014
6015 /* Get the frame containing the minibuffer
6016 that the selected frame is using. */
6017 mini_window = FRAME_MINIBUF_WINDOW (sf);
6018 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6019
6020 /* A null message buffer means that the frame hasn't really been
6021 initialized yet. Error messages get reported properly by
6022 cmd_error, so this must be just an informative message; toss it. */
6023 if (FRAME_MESSAGE_BUF (f))
6024 {
6025 int len;
6026 char *a[1];
6027 a[0] = (char *) XSTRING (string)->data;
6028
6029 len = doprnt (FRAME_MESSAGE_BUF (f),
6030 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6031
6032 if (log)
6033 message2 (FRAME_MESSAGE_BUF (f), len,
6034 STRING_MULTIBYTE (string));
6035 else
6036 message2_nolog (FRAME_MESSAGE_BUF (f), len,
6037 STRING_MULTIBYTE (string));
6038
6039 /* Print should start at the beginning of the message
6040 buffer next time. */
6041 message_buf_print = 0;
6042 }
6043 }
6044 }
6045
6046
6047 /* Dump an informative message to the minibuf. If M is 0, clear out
6048 any existing message, and let the mini-buffer text show through. */
6049
6050 /* VARARGS 1 */
6051 void
6052 message (m, a1, a2, a3)
6053 char *m;
6054 EMACS_INT a1, a2, a3;
6055 {
6056 if (noninteractive)
6057 {
6058 if (m)
6059 {
6060 if (noninteractive_need_newline)
6061 putc ('\n', stderr);
6062 noninteractive_need_newline = 0;
6063 fprintf (stderr, m, a1, a2, a3);
6064 if (cursor_in_echo_area == 0)
6065 fprintf (stderr, "\n");
6066 fflush (stderr);
6067 }
6068 }
6069 else if (INTERACTIVE)
6070 {
6071 /* The frame whose mini-buffer we're going to display the message
6072 on. It may be larger than the selected frame, so we need to
6073 use its buffer, not the selected frame's buffer. */
6074 Lisp_Object mini_window;
6075 struct frame *f, *sf = SELECTED_FRAME ();
6076
6077 /* Get the frame containing the mini-buffer
6078 that the selected frame is using. */
6079 mini_window = FRAME_MINIBUF_WINDOW (sf);
6080 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6081
6082 /* A null message buffer means that the frame hasn't really been
6083 initialized yet. Error messages get reported properly by
6084 cmd_error, so this must be just an informative message; toss
6085 it. */
6086 if (FRAME_MESSAGE_BUF (f))
6087 {
6088 if (m)
6089 {
6090 int len;
6091 #ifdef NO_ARG_ARRAY
6092 char *a[3];
6093 a[0] = (char *) a1;
6094 a[1] = (char *) a2;
6095 a[2] = (char *) a3;
6096
6097 len = doprnt (FRAME_MESSAGE_BUF (f),
6098 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6099 #else
6100 len = doprnt (FRAME_MESSAGE_BUF (f),
6101 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
6102 (char **) &a1);
6103 #endif /* NO_ARG_ARRAY */
6104
6105 message2 (FRAME_MESSAGE_BUF (f), len, 0);
6106 }
6107 else
6108 message1 (0);
6109
6110 /* Print should start at the beginning of the message
6111 buffer next time. */
6112 message_buf_print = 0;
6113 }
6114 }
6115 }
6116
6117
6118 /* The non-logging version of message. */
6119
6120 void
6121 message_nolog (m, a1, a2, a3)
6122 char *m;
6123 EMACS_INT a1, a2, a3;
6124 {
6125 Lisp_Object old_log_max;
6126 old_log_max = Vmessage_log_max;
6127 Vmessage_log_max = Qnil;
6128 message (m, a1, a2, a3);
6129 Vmessage_log_max = old_log_max;
6130 }
6131
6132
6133 /* Display the current message in the current mini-buffer. This is
6134 only called from error handlers in process.c, and is not time
6135 critical. */
6136
6137 void
6138 update_echo_area ()
6139 {
6140 if (!NILP (echo_area_buffer[0]))
6141 {
6142 Lisp_Object string;
6143 string = Fcurrent_message ();
6144 message3 (string, XSTRING (string)->size,
6145 !NILP (current_buffer->enable_multibyte_characters));
6146 }
6147 }
6148
6149
6150 /* Make sure echo area buffers in echo_buffers[] are life. If they
6151 aren't, make new ones. */
6152
6153 static void
6154 ensure_echo_area_buffers ()
6155 {
6156 int i;
6157
6158 for (i = 0; i < 2; ++i)
6159 if (!BUFFERP (echo_buffer[i])
6160 || NILP (XBUFFER (echo_buffer[i])->name))
6161 {
6162 char name[30];
6163 Lisp_Object old_buffer;
6164 int j;
6165
6166 old_buffer = echo_buffer[i];
6167 sprintf (name, " *Echo Area %d*", i);
6168 echo_buffer[i] = Fget_buffer_create (build_string (name));
6169 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
6170
6171 for (j = 0; j < 2; ++j)
6172 if (EQ (old_buffer, echo_area_buffer[j]))
6173 echo_area_buffer[j] = echo_buffer[i];
6174 }
6175 }
6176
6177
6178 /* Call FN with args A1..A4 with either the current or last displayed
6179 echo_area_buffer as current buffer.
6180
6181 WHICH zero means use the current message buffer
6182 echo_area_buffer[0]. If that is nil, choose a suitable buffer
6183 from echo_buffer[] and clear it.
6184
6185 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
6186 suitable buffer from echo_buffer[] and clear it.
6187
6188 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
6189 that the current message becomes the last displayed one, make
6190 choose a suitable buffer for echo_area_buffer[0], and clear it.
6191
6192 Value is what FN returns. */
6193
6194 static int
6195 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
6196 struct window *w;
6197 int which;
6198 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
6199 EMACS_INT a1;
6200 Lisp_Object a2;
6201 EMACS_INT a3, a4;
6202 {
6203 Lisp_Object buffer;
6204 int this_one, the_other, clear_buffer_p, rc;
6205 int count = BINDING_STACK_SIZE ();
6206
6207 /* If buffers aren't life, make new ones. */
6208 ensure_echo_area_buffers ();
6209
6210 clear_buffer_p = 0;
6211
6212 if (which == 0)
6213 this_one = 0, the_other = 1;
6214 else if (which > 0)
6215 this_one = 1, the_other = 0;
6216 else
6217 {
6218 this_one = 0, the_other = 1;
6219 clear_buffer_p = 1;
6220
6221 /* We need a fresh one in case the current echo buffer equals
6222 the one containing the last displayed echo area message. */
6223 if (!NILP (echo_area_buffer[this_one])
6224 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
6225 echo_area_buffer[this_one] = Qnil;
6226 }
6227
6228 /* Choose a suitable buffer from echo_buffer[] is we don't
6229 have one. */
6230 if (NILP (echo_area_buffer[this_one]))
6231 {
6232 echo_area_buffer[this_one]
6233 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
6234 ? echo_buffer[the_other]
6235 : echo_buffer[this_one]);
6236 clear_buffer_p = 1;
6237 }
6238
6239 buffer = echo_area_buffer[this_one];
6240
6241 /* Don't get confused by reusing the buffer used for echoing
6242 for a different purpose. */
6243 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
6244 cancel_echoing ();
6245
6246 record_unwind_protect (unwind_with_echo_area_buffer,
6247 with_echo_area_buffer_unwind_data (w));
6248
6249 /* Make the echo area buffer current. Note that for display
6250 purposes, it is not necessary that the displayed window's buffer
6251 == current_buffer, except for text property lookup. So, let's
6252 only set that buffer temporarily here without doing a full
6253 Fset_window_buffer. We must also change w->pointm, though,
6254 because otherwise an assertions in unshow_buffer fails, and Emacs
6255 aborts. */
6256 set_buffer_internal_1 (XBUFFER (buffer));
6257 if (w)
6258 {
6259 w->buffer = buffer;
6260 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
6261 }
6262
6263 current_buffer->undo_list = Qt;
6264 current_buffer->read_only = Qnil;
6265 specbind (Qinhibit_read_only, Qt);
6266 specbind (Qinhibit_modification_hooks, Qt);
6267
6268 if (clear_buffer_p && Z > BEG)
6269 del_range (BEG, Z);
6270
6271 xassert (BEGV >= BEG);
6272 xassert (ZV <= Z && ZV >= BEGV);
6273
6274 rc = fn (a1, a2, a3, a4);
6275
6276 xassert (BEGV >= BEG);
6277 xassert (ZV <= Z && ZV >= BEGV);
6278
6279 unbind_to (count, Qnil);
6280 return rc;
6281 }
6282
6283
6284 /* Save state that should be preserved around the call to the function
6285 FN called in with_echo_area_buffer. */
6286
6287 static Lisp_Object
6288 with_echo_area_buffer_unwind_data (w)
6289 struct window *w;
6290 {
6291 int i = 0;
6292 Lisp_Object vector;
6293
6294 /* Reduce consing by keeping one vector in
6295 Vwith_echo_area_save_vector. */
6296 vector = Vwith_echo_area_save_vector;
6297 Vwith_echo_area_save_vector = Qnil;
6298
6299 if (NILP (vector))
6300 vector = Fmake_vector (make_number (7), Qnil);
6301
6302 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
6303 AREF (vector, i) = Vdeactivate_mark, ++i;
6304 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
6305
6306 if (w)
6307 {
6308 XSETWINDOW (AREF (vector, i), w); ++i;
6309 AREF (vector, i) = w->buffer; ++i;
6310 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
6311 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
6312 }
6313 else
6314 {
6315 int end = i + 4;
6316 for (; i < end; ++i)
6317 AREF (vector, i) = Qnil;
6318 }
6319
6320 xassert (i == ASIZE (vector));
6321 return vector;
6322 }
6323
6324
6325 /* Restore global state from VECTOR which was created by
6326 with_echo_area_buffer_unwind_data. */
6327
6328 static Lisp_Object
6329 unwind_with_echo_area_buffer (vector)
6330 Lisp_Object vector;
6331 {
6332 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
6333 Vdeactivate_mark = AREF (vector, 1);
6334 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
6335
6336 if (WINDOWP (AREF (vector, 3)))
6337 {
6338 struct window *w;
6339 Lisp_Object buffer, charpos, bytepos;
6340
6341 w = XWINDOW (AREF (vector, 3));
6342 buffer = AREF (vector, 4);
6343 charpos = AREF (vector, 5);
6344 bytepos = AREF (vector, 6);
6345
6346 w->buffer = buffer;
6347 set_marker_both (w->pointm, buffer,
6348 XFASTINT (charpos), XFASTINT (bytepos));
6349 }
6350
6351 Vwith_echo_area_save_vector = vector;
6352 return Qnil;
6353 }
6354
6355
6356 /* Set up the echo area for use by print functions. MULTIBYTE_P
6357 non-zero means we will print multibyte. */
6358
6359 void
6360 setup_echo_area_for_printing (multibyte_p)
6361 int multibyte_p;
6362 {
6363 ensure_echo_area_buffers ();
6364
6365 if (!message_buf_print)
6366 {
6367 /* A message has been output since the last time we printed.
6368 Choose a fresh echo area buffer. */
6369 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6370 echo_area_buffer[0] = echo_buffer[1];
6371 else
6372 echo_area_buffer[0] = echo_buffer[0];
6373
6374 /* Switch to that buffer and clear it. */
6375 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6376 current_buffer->truncate_lines = Qnil;
6377
6378 if (Z > BEG)
6379 {
6380 int count = BINDING_STACK_SIZE ();
6381 specbind (Qinhibit_read_only, Qt);
6382 del_range (BEG, Z);
6383 unbind_to (count, Qnil);
6384 }
6385 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6386
6387 /* Set up the buffer for the multibyteness we need. */
6388 if (multibyte_p
6389 != !NILP (current_buffer->enable_multibyte_characters))
6390 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
6391
6392 /* Raise the frame containing the echo area. */
6393 if (minibuffer_auto_raise)
6394 {
6395 struct frame *sf = SELECTED_FRAME ();
6396 Lisp_Object mini_window;
6397 mini_window = FRAME_MINIBUF_WINDOW (sf);
6398 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6399 }
6400
6401 message_log_maybe_newline ();
6402 message_buf_print = 1;
6403 }
6404 else
6405 {
6406 if (NILP (echo_area_buffer[0]))
6407 {
6408 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6409 echo_area_buffer[0] = echo_buffer[1];
6410 else
6411 echo_area_buffer[0] = echo_buffer[0];
6412 }
6413
6414 if (current_buffer != XBUFFER (echo_area_buffer[0]))
6415 {
6416 /* Someone switched buffers between print requests. */
6417 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6418 current_buffer->truncate_lines = Qnil;
6419 }
6420 }
6421 }
6422
6423
6424 /* Display an echo area message in window W. Value is non-zero if W's
6425 height is changed. If display_last_displayed_message_p is
6426 non-zero, display the message that was last displayed, otherwise
6427 display the current message. */
6428
6429 static int
6430 display_echo_area (w)
6431 struct window *w;
6432 {
6433 int i, no_message_p, window_height_changed_p, count;
6434
6435 /* Temporarily disable garbage collections while displaying the echo
6436 area. This is done because a GC can print a message itself.
6437 That message would modify the echo area buffer's contents while a
6438 redisplay of the buffer is going on, and seriously confuse
6439 redisplay. */
6440 count = inhibit_garbage_collection ();
6441
6442 /* If there is no message, we must call display_echo_area_1
6443 nevertheless because it resizes the window. But we will have to
6444 reset the echo_area_buffer in question to nil at the end because
6445 with_echo_area_buffer will sets it to an empty buffer. */
6446 i = display_last_displayed_message_p ? 1 : 0;
6447 no_message_p = NILP (echo_area_buffer[i]);
6448
6449 window_height_changed_p
6450 = with_echo_area_buffer (w, display_last_displayed_message_p,
6451 display_echo_area_1,
6452 (EMACS_INT) w, Qnil, 0, 0);
6453
6454 if (no_message_p)
6455 echo_area_buffer[i] = Qnil;
6456
6457 unbind_to (count, Qnil);
6458 return window_height_changed_p;
6459 }
6460
6461
6462 /* Helper for display_echo_area. Display the current buffer which
6463 contains the current echo area message in window W, a mini-window,
6464 a pointer to which is passed in A1. A2..A4 are currently not used.
6465 Change the height of W so that all of the message is displayed.
6466 Value is non-zero if height of W was changed. */
6467
6468 static int
6469 display_echo_area_1 (a1, a2, a3, a4)
6470 EMACS_INT a1;
6471 Lisp_Object a2;
6472 EMACS_INT a3, a4;
6473 {
6474 struct window *w = (struct window *) a1;
6475 Lisp_Object window;
6476 struct text_pos start;
6477 int window_height_changed_p = 0;
6478
6479 /* Do this before displaying, so that we have a large enough glyph
6480 matrix for the display. */
6481 window_height_changed_p = resize_mini_window (w, 0);
6482
6483 /* Display. */
6484 clear_glyph_matrix (w->desired_matrix);
6485 XSETWINDOW (window, w);
6486 SET_TEXT_POS (start, BEG, BEG_BYTE);
6487 try_window (window, start);
6488
6489 return window_height_changed_p;
6490 }
6491
6492
6493 /* Resize the echo area window to exactly the size needed for the
6494 currently displayed message, if there is one. If a mini-buffer
6495 is active, don't shrink it. */
6496
6497 void
6498 resize_echo_area_exactly ()
6499 {
6500 if (BUFFERP (echo_area_buffer[0])
6501 && WINDOWP (echo_area_window))
6502 {
6503 struct window *w = XWINDOW (echo_area_window);
6504 int resized_p;
6505 Lisp_Object resize_exactly;
6506
6507 if (minibuf_level == 0)
6508 resize_exactly = Qt;
6509 else
6510 resize_exactly = Qnil;
6511
6512 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
6513 (EMACS_INT) w, resize_exactly, 0, 0);
6514 if (resized_p)
6515 {
6516 ++windows_or_buffers_changed;
6517 ++update_mode_lines;
6518 redisplay_internal (0);
6519 }
6520 }
6521 }
6522
6523
6524 /* Callback function for with_echo_area_buffer, when used from
6525 resize_echo_area_exactly. A1 contains a pointer to the window to
6526 resize, EXACTLY non-nil means resize the mini-window exactly to the
6527 size of the text displayed. A3 and A4 are not used. Value is what
6528 resize_mini_window returns. */
6529
6530 static int
6531 resize_mini_window_1 (a1, exactly, a3, a4)
6532 EMACS_INT a1;
6533 Lisp_Object exactly;
6534 EMACS_INT a3, a4;
6535 {
6536 return resize_mini_window ((struct window *) a1, !NILP (exactly));
6537 }
6538
6539
6540 /* Resize mini-window W to fit the size of its contents. EXACT:P
6541 means size the window exactly to the size needed. Otherwise, it's
6542 only enlarged until W's buffer is empty. Value is non-zero if
6543 the window height has been changed. */
6544
6545 int
6546 resize_mini_window (w, exact_p)
6547 struct window *w;
6548 int exact_p;
6549 {
6550 struct frame *f = XFRAME (w->frame);
6551 int window_height_changed_p = 0;
6552
6553 xassert (MINI_WINDOW_P (w));
6554
6555 /* Don't resize windows while redisplaying a window; it would
6556 confuse redisplay functions when the size of the window they are
6557 displaying changes from under them. Such a resizing can happen,
6558 for instance, when which-func prints a long message while
6559 we are running fontification-functions. We're running these
6560 functions with safe_call which binds inhibit-redisplay to t. */
6561 if (!NILP (Vinhibit_redisplay))
6562 return 0;
6563
6564 /* Nil means don't try to resize. */
6565 if (NILP (Vresize_mini_windows)
6566 || (FRAME_X_P (f) && f->output_data.x == NULL))
6567 return 0;
6568
6569 if (!FRAME_MINIBUF_ONLY_P (f))
6570 {
6571 struct it it;
6572 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
6573 int total_height = XFASTINT (root->height) + XFASTINT (w->height);
6574 int height, max_height;
6575 int unit = CANON_Y_UNIT (f);
6576 struct text_pos start;
6577 struct buffer *old_current_buffer = NULL;
6578
6579 if (current_buffer != XBUFFER (w->buffer))
6580 {
6581 old_current_buffer = current_buffer;
6582 set_buffer_internal (XBUFFER (w->buffer));
6583 }
6584
6585 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
6586
6587 /* Compute the max. number of lines specified by the user. */
6588 if (FLOATP (Vmax_mini_window_height))
6589 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_HEIGHT (f);
6590 else if (INTEGERP (Vmax_mini_window_height))
6591 max_height = XINT (Vmax_mini_window_height);
6592 else
6593 max_height = total_height / 4;
6594
6595 /* Correct that max. height if it's bogus. */
6596 max_height = max (1, max_height);
6597 max_height = min (total_height, max_height);
6598
6599 /* Find out the height of the text in the window. */
6600 if (it.truncate_lines_p)
6601 height = 1;
6602 else
6603 {
6604 last_height = 0;
6605 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
6606 if (it.max_ascent == 0 && it.max_descent == 0)
6607 height = it.current_y + last_height;
6608 else
6609 height = it.current_y + it.max_ascent + it.max_descent;
6610 height -= it.extra_line_spacing;
6611 height = (height + unit - 1) / unit;
6612 }
6613
6614 /* Compute a suitable window start. */
6615 if (height > max_height)
6616 {
6617 height = max_height;
6618 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
6619 move_it_vertically_backward (&it, (height - 1) * unit);
6620 start = it.current.pos;
6621 }
6622 else
6623 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
6624 SET_MARKER_FROM_TEXT_POS (w->start, start);
6625
6626 if (EQ (Vresize_mini_windows, Qgrow_only))
6627 {
6628 /* Let it grow only, until we display an empty message, in which
6629 case the window shrinks again. */
6630 if (height > XFASTINT (w->height))
6631 {
6632 int old_height = XFASTINT (w->height);
6633 freeze_window_starts (f, 1);
6634 grow_mini_window (w, height - XFASTINT (w->height));
6635 window_height_changed_p = XFASTINT (w->height) != old_height;
6636 }
6637 else if (height < XFASTINT (w->height)
6638 && (exact_p || BEGV == ZV))
6639 {
6640 int old_height = XFASTINT (w->height);
6641 freeze_window_starts (f, 0);
6642 shrink_mini_window (w);
6643 window_height_changed_p = XFASTINT (w->height) != old_height;
6644 }
6645 }
6646 else
6647 {
6648 /* Always resize to exact size needed. */
6649 if (height > XFASTINT (w->height))
6650 {
6651 int old_height = XFASTINT (w->height);
6652 freeze_window_starts (f, 1);
6653 grow_mini_window (w, height - XFASTINT (w->height));
6654 window_height_changed_p = XFASTINT (w->height) != old_height;
6655 }
6656 else if (height < XFASTINT (w->height))
6657 {
6658 int old_height = XFASTINT (w->height);
6659 freeze_window_starts (f, 0);
6660 shrink_mini_window (w);
6661
6662 if (height)
6663 {
6664 freeze_window_starts (f, 1);
6665 grow_mini_window (w, height - XFASTINT (w->height));
6666 }
6667
6668 window_height_changed_p = XFASTINT (w->height) != old_height;
6669 }
6670 }
6671
6672 if (old_current_buffer)
6673 set_buffer_internal (old_current_buffer);
6674 }
6675
6676 return window_height_changed_p;
6677 }
6678
6679
6680 /* Value is the current message, a string, or nil if there is no
6681 current message. */
6682
6683 Lisp_Object
6684 current_message ()
6685 {
6686 Lisp_Object msg;
6687
6688 if (NILP (echo_area_buffer[0]))
6689 msg = Qnil;
6690 else
6691 {
6692 with_echo_area_buffer (0, 0, current_message_1,
6693 (EMACS_INT) &msg, Qnil, 0, 0);
6694 if (NILP (msg))
6695 echo_area_buffer[0] = Qnil;
6696 }
6697
6698 return msg;
6699 }
6700
6701
6702 static int
6703 current_message_1 (a1, a2, a3, a4)
6704 EMACS_INT a1;
6705 Lisp_Object a2;
6706 EMACS_INT a3, a4;
6707 {
6708 Lisp_Object *msg = (Lisp_Object *) a1;
6709
6710 if (Z > BEG)
6711 *msg = make_buffer_string (BEG, Z, 1);
6712 else
6713 *msg = Qnil;
6714 return 0;
6715 }
6716
6717
6718 /* Push the current message on Vmessage_stack for later restauration
6719 by restore_message. Value is non-zero if the current message isn't
6720 empty. This is a relatively infrequent operation, so it's not
6721 worth optimizing. */
6722
6723 int
6724 push_message ()
6725 {
6726 Lisp_Object msg;
6727 msg = current_message ();
6728 Vmessage_stack = Fcons (msg, Vmessage_stack);
6729 return STRINGP (msg);
6730 }
6731
6732
6733 /* Handler for record_unwind_protect calling pop_message. */
6734
6735 Lisp_Object
6736 push_message_unwind (dummy)
6737 Lisp_Object dummy;
6738 {
6739 pop_message ();
6740 return Qnil;
6741 }
6742
6743
6744 /* Restore message display from the top of Vmessage_stack. */
6745
6746 void
6747 restore_message ()
6748 {
6749 Lisp_Object msg;
6750
6751 xassert (CONSP (Vmessage_stack));
6752 msg = XCAR (Vmessage_stack);
6753 if (STRINGP (msg))
6754 message3_nolog (msg, STRING_BYTES (XSTRING (msg)), STRING_MULTIBYTE (msg));
6755 else
6756 message3_nolog (msg, 0, 0);
6757 }
6758
6759
6760 /* Pop the top-most entry off Vmessage_stack. */
6761
6762 void
6763 pop_message ()
6764 {
6765 xassert (CONSP (Vmessage_stack));
6766 Vmessage_stack = XCDR (Vmessage_stack);
6767 }
6768
6769
6770 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
6771 exits. If the stack is not empty, we have a missing pop_message
6772 somewhere. */
6773
6774 void
6775 check_message_stack ()
6776 {
6777 if (!NILP (Vmessage_stack))
6778 abort ();
6779 }
6780
6781
6782 /* Truncate to NCHARS what will be displayed in the echo area the next
6783 time we display it---but don't redisplay it now. */
6784
6785 void
6786 truncate_echo_area (nchars)
6787 int nchars;
6788 {
6789 if (nchars == 0)
6790 echo_area_buffer[0] = Qnil;
6791 /* A null message buffer means that the frame hasn't really been
6792 initialized yet. Error messages get reported properly by
6793 cmd_error, so this must be just an informative message; toss it. */
6794 else if (!noninteractive
6795 && INTERACTIVE
6796 && !NILP (echo_area_buffer[0]))
6797 {
6798 struct frame *sf = SELECTED_FRAME ();
6799 if (FRAME_MESSAGE_BUF (sf))
6800 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
6801 }
6802 }
6803
6804
6805 /* Helper function for truncate_echo_area. Truncate the current
6806 message to at most NCHARS characters. */
6807
6808 static int
6809 truncate_message_1 (nchars, a2, a3, a4)
6810 EMACS_INT nchars;
6811 Lisp_Object a2;
6812 EMACS_INT a3, a4;
6813 {
6814 if (BEG + nchars < Z)
6815 del_range (BEG + nchars, Z);
6816 if (Z == BEG)
6817 echo_area_buffer[0] = Qnil;
6818 return 0;
6819 }
6820
6821
6822 /* Set the current message to a substring of S or STRING.
6823
6824 If STRING is a Lisp string, set the message to the first NBYTES
6825 bytes from STRING. NBYTES zero means use the whole string. If
6826 STRING is multibyte, the message will be displayed multibyte.
6827
6828 If S is not null, set the message to the first LEN bytes of S. LEN
6829 zero means use the whole string. MULTIBYTE_P non-zero means S is
6830 multibyte. Display the message multibyte in that case. */
6831
6832 void
6833 set_message (s, string, nbytes, multibyte_p)
6834 char *s;
6835 Lisp_Object string;
6836 int nbytes;
6837 {
6838 message_enable_multibyte
6839 = ((s && multibyte_p)
6840 || (STRINGP (string) && STRING_MULTIBYTE (string)));
6841
6842 with_echo_area_buffer (0, -1, set_message_1,
6843 (EMACS_INT) s, string, nbytes, multibyte_p);
6844 message_buf_print = 0;
6845 help_echo_showing_p = 0;
6846 }
6847
6848
6849 /* Helper function for set_message. Arguments have the same meaning
6850 as there, with A1 corresponding to S and A2 corresponding to STRING
6851 This function is called with the echo area buffer being
6852 current. */
6853
6854 static int
6855 set_message_1 (a1, a2, nbytes, multibyte_p)
6856 EMACS_INT a1;
6857 Lisp_Object a2;
6858 EMACS_INT nbytes, multibyte_p;
6859 {
6860 char *s = (char *) a1;
6861 Lisp_Object string = a2;
6862
6863 xassert (BEG == Z);
6864
6865 /* Change multibyteness of the echo buffer appropriately. */
6866 if (message_enable_multibyte
6867 != !NILP (current_buffer->enable_multibyte_characters))
6868 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
6869
6870 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
6871
6872 /* Insert new message at BEG. */
6873 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6874
6875 if (STRINGP (string))
6876 {
6877 int nchars;
6878
6879 if (nbytes == 0)
6880 nbytes = XSTRING (string)->size_byte;
6881 nchars = string_byte_to_char (string, nbytes);
6882
6883 /* This function takes care of single/multibyte conversion. We
6884 just have to ensure that the echo area buffer has the right
6885 setting of enable_multibyte_characters. */
6886 insert_from_string (string, 0, 0, nchars, nbytes, 1);
6887 }
6888 else if (s)
6889 {
6890 if (nbytes == 0)
6891 nbytes = strlen (s);
6892
6893 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
6894 {
6895 /* Convert from multi-byte to single-byte. */
6896 int i, c, n;
6897 unsigned char work[1];
6898
6899 /* Convert a multibyte string to single-byte. */
6900 for (i = 0; i < nbytes; i += n)
6901 {
6902 c = string_char_and_length (s + i, nbytes - i, &n);
6903 work[0] = (SINGLE_BYTE_CHAR_P (c)
6904 ? c
6905 : multibyte_char_to_unibyte (c, Qnil));
6906 insert_1_both (work, 1, 1, 1, 0, 0);
6907 }
6908 }
6909 else if (!multibyte_p
6910 && !NILP (current_buffer->enable_multibyte_characters))
6911 {
6912 /* Convert from single-byte to multi-byte. */
6913 int i, c, n;
6914 unsigned char *msg = (unsigned char *) s;
6915 unsigned char str[MAX_MULTIBYTE_LENGTH];
6916
6917 /* Convert a single-byte string to multibyte. */
6918 for (i = 0; i < nbytes; i++)
6919 {
6920 c = unibyte_char_to_multibyte (msg[i]);
6921 n = CHAR_STRING (c, str);
6922 insert_1_both (str, 1, n, 1, 0, 0);
6923 }
6924 }
6925 else
6926 insert_1 (s, nbytes, 1, 0, 0);
6927 }
6928
6929 return 0;
6930 }
6931
6932
6933 /* Clear messages. CURRENT_P non-zero means clear the current
6934 message. LAST_DISPLAYED_P non-zero means clear the message
6935 last displayed. */
6936
6937 void
6938 clear_message (current_p, last_displayed_p)
6939 int current_p, last_displayed_p;
6940 {
6941 if (current_p)
6942 {
6943 echo_area_buffer[0] = Qnil;
6944 message_cleared_p = 1;
6945 }
6946
6947 if (last_displayed_p)
6948 echo_area_buffer[1] = Qnil;
6949
6950 message_buf_print = 0;
6951 }
6952
6953 /* Clear garbaged frames.
6954
6955 This function is used where the old redisplay called
6956 redraw_garbaged_frames which in turn called redraw_frame which in
6957 turn called clear_frame. The call to clear_frame was a source of
6958 flickering. I believe a clear_frame is not necessary. It should
6959 suffice in the new redisplay to invalidate all current matrices,
6960 and ensure a complete redisplay of all windows. */
6961
6962 static void
6963 clear_garbaged_frames ()
6964 {
6965 if (frame_garbaged)
6966 {
6967 Lisp_Object tail, frame;
6968
6969 FOR_EACH_FRAME (tail, frame)
6970 {
6971 struct frame *f = XFRAME (frame);
6972
6973 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
6974 {
6975 if (f->resized_p)
6976 Fredraw_frame (frame);
6977 clear_current_matrices (f);
6978 f->garbaged = 0;
6979 f->resized_p = 0;
6980 }
6981 }
6982
6983 frame_garbaged = 0;
6984 ++windows_or_buffers_changed;
6985 }
6986 }
6987
6988
6989 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
6990 is non-zero update selected_frame. Value is non-zero if the
6991 mini-windows height has been changed. */
6992
6993 static int
6994 echo_area_display (update_frame_p)
6995 int update_frame_p;
6996 {
6997 Lisp_Object mini_window;
6998 struct window *w;
6999 struct frame *f;
7000 int window_height_changed_p = 0;
7001 struct frame *sf = SELECTED_FRAME ();
7002
7003 mini_window = FRAME_MINIBUF_WINDOW (sf);
7004 w = XWINDOW (mini_window);
7005 f = XFRAME (WINDOW_FRAME (w));
7006
7007 /* Don't display if frame is invisible or not yet initialized. */
7008 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
7009 return 0;
7010
7011 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
7012 #ifndef macintosh
7013 #ifdef HAVE_WINDOW_SYSTEM
7014 /* When Emacs starts, selected_frame may be a visible terminal
7015 frame, even if we run under a window system. If we let this
7016 through, a message would be displayed on the terminal. */
7017 if (EQ (selected_frame, Vterminal_frame)
7018 && !NILP (Vwindow_system))
7019 return 0;
7020 #endif /* HAVE_WINDOW_SYSTEM */
7021 #endif
7022
7023 /* Redraw garbaged frames. */
7024 if (frame_garbaged)
7025 clear_garbaged_frames ();
7026
7027 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
7028 {
7029 echo_area_window = mini_window;
7030 window_height_changed_p = display_echo_area (w);
7031 w->must_be_updated_p = 1;
7032
7033 /* Update the display, unless called from redisplay_internal.
7034 Also don't update the screen during redisplay itself. The
7035 update will happen at the end of redisplay, and an update
7036 here could cause confusion. */
7037 if (update_frame_p && !redisplaying_p)
7038 {
7039 int n = 0;
7040
7041 /* If the display update has been interrupted by pending
7042 input, update mode lines in the frame. Due to the
7043 pending input, it might have been that redisplay hasn't
7044 been called, so that mode lines above the echo area are
7045 garbaged. This looks odd, so we prevent it here. */
7046 if (!display_completed)
7047 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
7048
7049 if (window_height_changed_p
7050 /* Don't do this if Emacs is shutting down. Redisplay
7051 needs to run hooks. */
7052 && !NILP (Vrun_hooks))
7053 {
7054 /* Must update other windows. Likewise as in other
7055 cases, don't let this update be interrupted by
7056 pending input. */
7057 int count = BINDING_STACK_SIZE ();
7058 specbind (Qredisplay_dont_pause, Qt);
7059 windows_or_buffers_changed = 1;
7060 redisplay_internal (0);
7061 unbind_to (count, Qnil);
7062 }
7063 else if (FRAME_WINDOW_P (f) && n == 0)
7064 {
7065 /* Window configuration is the same as before.
7066 Can do with a display update of the echo area,
7067 unless we displayed some mode lines. */
7068 update_single_window (w, 1);
7069 rif->flush_display (f);
7070 }
7071 else
7072 update_frame (f, 1, 1);
7073
7074 /* If cursor is in the echo area, make sure that the next
7075 redisplay displays the minibuffer, so that the cursor will
7076 be replaced with what the minibuffer wants. */
7077 if (cursor_in_echo_area)
7078 ++windows_or_buffers_changed;
7079 }
7080 }
7081 else if (!EQ (mini_window, selected_window))
7082 windows_or_buffers_changed++;
7083
7084 /* Last displayed message is now the current message. */
7085 echo_area_buffer[1] = echo_area_buffer[0];
7086
7087 /* Prevent redisplay optimization in redisplay_internal by resetting
7088 this_line_start_pos. This is done because the mini-buffer now
7089 displays the message instead of its buffer text. */
7090 if (EQ (mini_window, selected_window))
7091 CHARPOS (this_line_start_pos) = 0;
7092
7093 return window_height_changed_p;
7094 }
7095
7096
7097 \f
7098 /***********************************************************************
7099 Frame Titles
7100 ***********************************************************************/
7101
7102
7103 #ifdef HAVE_WINDOW_SYSTEM
7104
7105 /* A buffer for constructing frame titles in it; allocated from the
7106 heap in init_xdisp and resized as needed in store_frame_title_char. */
7107
7108 static char *frame_title_buf;
7109
7110 /* The buffer's end, and a current output position in it. */
7111
7112 static char *frame_title_buf_end;
7113 static char *frame_title_ptr;
7114
7115
7116 /* Store a single character C for the frame title in frame_title_buf.
7117 Re-allocate frame_title_buf if necessary. */
7118
7119 static void
7120 store_frame_title_char (c)
7121 char c;
7122 {
7123 /* If output position has reached the end of the allocated buffer,
7124 double the buffer's size. */
7125 if (frame_title_ptr == frame_title_buf_end)
7126 {
7127 int len = frame_title_ptr - frame_title_buf;
7128 int new_size = 2 * len * sizeof *frame_title_buf;
7129 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
7130 frame_title_buf_end = frame_title_buf + new_size;
7131 frame_title_ptr = frame_title_buf + len;
7132 }
7133
7134 *frame_title_ptr++ = c;
7135 }
7136
7137
7138 /* Store part of a frame title in frame_title_buf, beginning at
7139 frame_title_ptr. STR is the string to store. Do not copy
7140 characters that yield more columns than PRECISION; PRECISION <= 0
7141 means copy the whole string. Pad with spaces until FIELD_WIDTH
7142 number of characters have been copied; FIELD_WIDTH <= 0 means don't
7143 pad. Called from display_mode_element when it is used to build a
7144 frame title. */
7145
7146 static int
7147 store_frame_title (str, field_width, precision)
7148 unsigned char *str;
7149 int field_width, precision;
7150 {
7151 int n = 0;
7152 int dummy, nbytes;
7153
7154 /* Copy at most PRECISION chars from STR. */
7155 nbytes = strlen (str);
7156 n+= c_string_width (str, nbytes, precision, &dummy, &nbytes);
7157 while (nbytes--)
7158 store_frame_title_char (*str++);
7159
7160 /* Fill up with spaces until FIELD_WIDTH reached. */
7161 while (field_width > 0
7162 && n < field_width)
7163 {
7164 store_frame_title_char (' ');
7165 ++n;
7166 }
7167
7168 return n;
7169 }
7170
7171
7172 /* Set the title of FRAME, if it has changed. The title format is
7173 Vicon_title_format if FRAME is iconified, otherwise it is
7174 frame_title_format. */
7175
7176 static void
7177 x_consider_frame_title (frame)
7178 Lisp_Object frame;
7179 {
7180 struct frame *f = XFRAME (frame);
7181
7182 if (FRAME_WINDOW_P (f)
7183 || FRAME_MINIBUF_ONLY_P (f)
7184 || f->explicit_name)
7185 {
7186 /* Do we have more than one visible frame on this X display? */
7187 Lisp_Object tail;
7188 Lisp_Object fmt;
7189 struct buffer *obuf;
7190 int len;
7191 struct it it;
7192
7193 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
7194 {
7195 struct frame *tf = XFRAME (XCAR (tail));
7196
7197 if (tf != f
7198 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
7199 && !FRAME_MINIBUF_ONLY_P (tf)
7200 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
7201 break;
7202 }
7203
7204 /* Set global variable indicating that multiple frames exist. */
7205 multiple_frames = CONSP (tail);
7206
7207 /* Switch to the buffer of selected window of the frame. Set up
7208 frame_title_ptr so that display_mode_element will output into it;
7209 then display the title. */
7210 obuf = current_buffer;
7211 Fset_buffer (XWINDOW (f->selected_window)->buffer);
7212 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
7213 frame_title_ptr = frame_title_buf;
7214 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
7215 NULL, DEFAULT_FACE_ID);
7216 display_mode_element (&it, 0, -1, -1, fmt);
7217 len = frame_title_ptr - frame_title_buf;
7218 frame_title_ptr = NULL;
7219 set_buffer_internal (obuf);
7220
7221 /* Set the title only if it's changed. This avoids consing in
7222 the common case where it hasn't. (If it turns out that we've
7223 already wasted too much time by walking through the list with
7224 display_mode_element, then we might need to optimize at a
7225 higher level than this.) */
7226 if (! STRINGP (f->name)
7227 || STRING_BYTES (XSTRING (f->name)) != len
7228 || bcmp (frame_title_buf, XSTRING (f->name)->data, len) != 0)
7229 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
7230 }
7231 }
7232
7233 #else /* not HAVE_WINDOW_SYSTEM */
7234
7235 #define frame_title_ptr ((char *)0)
7236 #define store_frame_title(str, mincol, maxcol) 0
7237
7238 #endif /* not HAVE_WINDOW_SYSTEM */
7239
7240
7241
7242 \f
7243 /***********************************************************************
7244 Menu Bars
7245 ***********************************************************************/
7246
7247
7248 /* Prepare for redisplay by updating menu-bar item lists when
7249 appropriate. This can call eval. */
7250
7251 void
7252 prepare_menu_bars ()
7253 {
7254 int all_windows;
7255 struct gcpro gcpro1, gcpro2;
7256 struct frame *f;
7257 Lisp_Object tooltip_frame;
7258
7259 #ifdef HAVE_X_WINDOWS
7260 tooltip_frame = tip_frame;
7261 #else
7262 tooltip_frame = Qnil;
7263 #endif
7264
7265 /* Update all frame titles based on their buffer names, etc. We do
7266 this before the menu bars so that the buffer-menu will show the
7267 up-to-date frame titles. */
7268 #ifdef HAVE_WINDOW_SYSTEM
7269 if (windows_or_buffers_changed || update_mode_lines)
7270 {
7271 Lisp_Object tail, frame;
7272
7273 FOR_EACH_FRAME (tail, frame)
7274 {
7275 f = XFRAME (frame);
7276 if (!EQ (frame, tooltip_frame)
7277 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
7278 x_consider_frame_title (frame);
7279 }
7280 }
7281 #endif /* HAVE_WINDOW_SYSTEM */
7282
7283 /* Update the menu bar item lists, if appropriate. This has to be
7284 done before any actual redisplay or generation of display lines. */
7285 all_windows = (update_mode_lines
7286 || buffer_shared > 1
7287 || windows_or_buffers_changed);
7288 if (all_windows)
7289 {
7290 Lisp_Object tail, frame;
7291 int count = BINDING_STACK_SIZE ();
7292
7293 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7294
7295 FOR_EACH_FRAME (tail, frame)
7296 {
7297 f = XFRAME (frame);
7298
7299 /* Ignore tooltip frame. */
7300 if (EQ (frame, tooltip_frame))
7301 continue;
7302
7303 /* If a window on this frame changed size, report that to
7304 the user and clear the size-change flag. */
7305 if (FRAME_WINDOW_SIZES_CHANGED (f))
7306 {
7307 Lisp_Object functions;
7308
7309 /* Clear flag first in case we get an error below. */
7310 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
7311 functions = Vwindow_size_change_functions;
7312 GCPRO2 (tail, functions);
7313
7314 while (CONSP (functions))
7315 {
7316 call1 (XCAR (functions), frame);
7317 functions = XCDR (functions);
7318 }
7319 UNGCPRO;
7320 }
7321
7322 GCPRO1 (tail);
7323 update_menu_bar (f, 0);
7324 #ifdef HAVE_WINDOW_SYSTEM
7325 update_tool_bar (f, 0);
7326 #endif
7327 UNGCPRO;
7328 }
7329
7330 unbind_to (count, Qnil);
7331 }
7332 else
7333 {
7334 struct frame *sf = SELECTED_FRAME ();
7335 update_menu_bar (sf, 1);
7336 #ifdef HAVE_WINDOW_SYSTEM
7337 update_tool_bar (sf, 1);
7338 #endif
7339 }
7340
7341 /* Motif needs this. See comment in xmenu.c. Turn it off when
7342 pending_menu_activation is not defined. */
7343 #ifdef USE_X_TOOLKIT
7344 pending_menu_activation = 0;
7345 #endif
7346 }
7347
7348
7349 /* Update the menu bar item list for frame F. This has to be done
7350 before we start to fill in any display lines, because it can call
7351 eval.
7352
7353 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
7354
7355 static void
7356 update_menu_bar (f, save_match_data)
7357 struct frame *f;
7358 int save_match_data;
7359 {
7360 Lisp_Object window;
7361 register struct window *w;
7362
7363 /* If called recursively during a menu update, do nothing. This can
7364 happen when, for instance, an activate-menubar-hook causes a
7365 redisplay. */
7366 if (inhibit_menubar_update)
7367 return;
7368
7369 window = FRAME_SELECTED_WINDOW (f);
7370 w = XWINDOW (window);
7371
7372 if (update_mode_lines)
7373 w->update_mode_line = Qt;
7374
7375 if (FRAME_WINDOW_P (f)
7376 ?
7377 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
7378 FRAME_EXTERNAL_MENU_BAR (f)
7379 #else
7380 FRAME_MENU_BAR_LINES (f) > 0
7381 #endif
7382 : FRAME_MENU_BAR_LINES (f) > 0)
7383 {
7384 /* If the user has switched buffers or windows, we need to
7385 recompute to reflect the new bindings. But we'll
7386 recompute when update_mode_lines is set too; that means
7387 that people can use force-mode-line-update to request
7388 that the menu bar be recomputed. The adverse effect on
7389 the rest of the redisplay algorithm is about the same as
7390 windows_or_buffers_changed anyway. */
7391 if (windows_or_buffers_changed
7392 || !NILP (w->update_mode_line)
7393 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7394 < BUF_MODIFF (XBUFFER (w->buffer)))
7395 != !NILP (w->last_had_star))
7396 || ((!NILP (Vtransient_mark_mode)
7397 && !NILP (XBUFFER (w->buffer)->mark_active))
7398 != !NILP (w->region_showing)))
7399 {
7400 struct buffer *prev = current_buffer;
7401 int count = BINDING_STACK_SIZE ();
7402
7403 specbind (Qinhibit_menubar_update, Qt);
7404
7405 set_buffer_internal_1 (XBUFFER (w->buffer));
7406 if (save_match_data)
7407 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7408 if (NILP (Voverriding_local_map_menu_flag))
7409 {
7410 specbind (Qoverriding_terminal_local_map, Qnil);
7411 specbind (Qoverriding_local_map, Qnil);
7412 }
7413
7414 /* Run the Lucid hook. */
7415 safe_run_hooks (Qactivate_menubar_hook);
7416
7417 /* If it has changed current-menubar from previous value,
7418 really recompute the menu-bar from the value. */
7419 if (! NILP (Vlucid_menu_bar_dirty_flag))
7420 call0 (Qrecompute_lucid_menubar);
7421
7422 safe_run_hooks (Qmenu_bar_update_hook);
7423 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
7424
7425 /* Redisplay the menu bar in case we changed it. */
7426 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
7427 if (FRAME_WINDOW_P (f)
7428 #if defined (macintosh)
7429 /* All frames on Mac OS share the same menubar. So only the
7430 selected frame should be allowed to set it. */
7431 && f == SELECTED_FRAME ()
7432 #endif
7433 )
7434 set_frame_menubar (f, 0, 0);
7435 else
7436 /* On a terminal screen, the menu bar is an ordinary screen
7437 line, and this makes it get updated. */
7438 w->update_mode_line = Qt;
7439 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7440 /* In the non-toolkit version, the menu bar is an ordinary screen
7441 line, and this makes it get updated. */
7442 w->update_mode_line = Qt;
7443 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7444
7445 unbind_to (count, Qnil);
7446 set_buffer_internal_1 (prev);
7447 }
7448 }
7449 }
7450
7451
7452 \f
7453 /***********************************************************************
7454 Tool-bars
7455 ***********************************************************************/
7456
7457 #ifdef HAVE_WINDOW_SYSTEM
7458
7459 /* Update the tool-bar item list for frame F. This has to be done
7460 before we start to fill in any display lines. Called from
7461 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
7462 and restore it here. */
7463
7464 static void
7465 update_tool_bar (f, save_match_data)
7466 struct frame *f;
7467 int save_match_data;
7468 {
7469 if (WINDOWP (f->tool_bar_window)
7470 && XFASTINT (XWINDOW (f->tool_bar_window)->height) > 0)
7471 {
7472 Lisp_Object window;
7473 struct window *w;
7474
7475 window = FRAME_SELECTED_WINDOW (f);
7476 w = XWINDOW (window);
7477
7478 /* If the user has switched buffers or windows, we need to
7479 recompute to reflect the new bindings. But we'll
7480 recompute when update_mode_lines is set too; that means
7481 that people can use force-mode-line-update to request
7482 that the menu bar be recomputed. The adverse effect on
7483 the rest of the redisplay algorithm is about the same as
7484 windows_or_buffers_changed anyway. */
7485 if (windows_or_buffers_changed
7486 || !NILP (w->update_mode_line)
7487 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7488 < BUF_MODIFF (XBUFFER (w->buffer)))
7489 != !NILP (w->last_had_star))
7490 || ((!NILP (Vtransient_mark_mode)
7491 && !NILP (XBUFFER (w->buffer)->mark_active))
7492 != !NILP (w->region_showing)))
7493 {
7494 struct buffer *prev = current_buffer;
7495 int count = BINDING_STACK_SIZE ();
7496
7497 /* Set current_buffer to the buffer of the selected
7498 window of the frame, so that we get the right local
7499 keymaps. */
7500 set_buffer_internal_1 (XBUFFER (w->buffer));
7501
7502 /* Save match data, if we must. */
7503 if (save_match_data)
7504 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7505
7506 /* Make sure that we don't accidentally use bogus keymaps. */
7507 if (NILP (Voverriding_local_map_menu_flag))
7508 {
7509 specbind (Qoverriding_terminal_local_map, Qnil);
7510 specbind (Qoverriding_local_map, Qnil);
7511 }
7512
7513 /* Build desired tool-bar items from keymaps. */
7514 f->tool_bar_items
7515 = tool_bar_items (f->tool_bar_items, &f->n_tool_bar_items);
7516
7517 /* Redisplay the tool-bar in case we changed it. */
7518 w->update_mode_line = Qt;
7519
7520 unbind_to (count, Qnil);
7521 set_buffer_internal_1 (prev);
7522 }
7523 }
7524 }
7525
7526
7527 /* Set F->desired_tool_bar_string to a Lisp string representing frame
7528 F's desired tool-bar contents. F->tool_bar_items must have
7529 been set up previously by calling prepare_menu_bars. */
7530
7531 static void
7532 build_desired_tool_bar_string (f)
7533 struct frame *f;
7534 {
7535 int i, size, size_needed;
7536 struct gcpro gcpro1, gcpro2, gcpro3;
7537 Lisp_Object image, plist, props;
7538
7539 image = plist = props = Qnil;
7540 GCPRO3 (image, plist, props);
7541
7542 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
7543 Otherwise, make a new string. */
7544
7545 /* The size of the string we might be able to reuse. */
7546 size = (STRINGP (f->desired_tool_bar_string)
7547 ? XSTRING (f->desired_tool_bar_string)->size
7548 : 0);
7549
7550 /* We need one space in the string for each image. */
7551 size_needed = f->n_tool_bar_items;
7552
7553 /* Reuse f->desired_tool_bar_string, if possible. */
7554 if (size < size_needed || NILP (f->desired_tool_bar_string))
7555 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
7556 make_number (' '));
7557 else
7558 {
7559 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
7560 Fremove_text_properties (make_number (0), make_number (size),
7561 props, f->desired_tool_bar_string);
7562 }
7563
7564 /* Put a `display' property on the string for the images to display,
7565 put a `menu_item' property on tool-bar items with a value that
7566 is the index of the item in F's tool-bar item vector. */
7567 for (i = 0; i < f->n_tool_bar_items; ++i)
7568 {
7569 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
7570
7571 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
7572 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
7573 int hmargin, vmargin, relief, idx, end;
7574 extern Lisp_Object QCrelief, QCmargin, QCconversion, Qimage;
7575
7576 /* If image is a vector, choose the image according to the
7577 button state. */
7578 image = PROP (TOOL_BAR_ITEM_IMAGES);
7579 if (VECTORP (image))
7580 {
7581 if (enabled_p)
7582 idx = (selected_p
7583 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
7584 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
7585 else
7586 idx = (selected_p
7587 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
7588 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
7589
7590 xassert (ASIZE (image) >= idx);
7591 image = AREF (image, idx);
7592 }
7593 else
7594 idx = -1;
7595
7596 /* Ignore invalid image specifications. */
7597 if (!valid_image_p (image))
7598 continue;
7599
7600 /* Display the tool-bar button pressed, or depressed. */
7601 plist = Fcopy_sequence (XCDR (image));
7602
7603 /* Compute margin and relief to draw. */
7604 relief = (tool_bar_button_relief >= 0
7605 ? tool_bar_button_relief
7606 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
7607 hmargin = vmargin = relief;
7608
7609 if (INTEGERP (Vtool_bar_button_margin)
7610 && XINT (Vtool_bar_button_margin) > 0)
7611 {
7612 hmargin += XFASTINT (Vtool_bar_button_margin);
7613 vmargin += XFASTINT (Vtool_bar_button_margin);
7614 }
7615 else if (CONSP (Vtool_bar_button_margin))
7616 {
7617 if (INTEGERP (XCAR (Vtool_bar_button_margin))
7618 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
7619 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
7620
7621 if (INTEGERP (XCDR (Vtool_bar_button_margin))
7622 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
7623 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
7624 }
7625
7626 if (auto_raise_tool_bar_buttons_p)
7627 {
7628 /* Add a `:relief' property to the image spec if the item is
7629 selected. */
7630 if (selected_p)
7631 {
7632 plist = Fplist_put (plist, QCrelief, make_number (-relief));
7633 hmargin -= relief;
7634 vmargin -= relief;
7635 }
7636 }
7637 else
7638 {
7639 /* If image is selected, display it pressed, i.e. with a
7640 negative relief. If it's not selected, display it with a
7641 raised relief. */
7642 plist = Fplist_put (plist, QCrelief,
7643 (selected_p
7644 ? make_number (-relief)
7645 : make_number (relief)));
7646 hmargin -= relief;
7647 vmargin -= relief;
7648 }
7649
7650 /* Put a margin around the image. */
7651 if (hmargin || vmargin)
7652 {
7653 if (hmargin == vmargin)
7654 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
7655 else
7656 plist = Fplist_put (plist, QCmargin,
7657 Fcons (make_number (hmargin),
7658 make_number (vmargin)));
7659 }
7660
7661 /* If button is not enabled, and we don't have special images
7662 for the disabled state, make the image appear disabled by
7663 applying an appropriate algorithm to it. */
7664 if (!enabled_p && idx < 0)
7665 plist = Fplist_put (plist, QCconversion, Qdisabled);
7666
7667 /* Put a `display' text property on the string for the image to
7668 display. Put a `menu-item' property on the string that gives
7669 the start of this item's properties in the tool-bar items
7670 vector. */
7671 image = Fcons (Qimage, plist);
7672 props = list4 (Qdisplay, image,
7673 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
7674
7675 /* Let the last image hide all remaining spaces in the tool bar
7676 string. The string can be longer than needed when we reuse a
7677 previous string. */
7678 if (i + 1 == f->n_tool_bar_items)
7679 end = XSTRING (f->desired_tool_bar_string)->size;
7680 else
7681 end = i + 1;
7682 Fadd_text_properties (make_number (i), make_number (end),
7683 props, f->desired_tool_bar_string);
7684 #undef PROP
7685 }
7686
7687 UNGCPRO;
7688 }
7689
7690
7691 /* Display one line of the tool-bar of frame IT->f. */
7692
7693 static void
7694 display_tool_bar_line (it)
7695 struct it *it;
7696 {
7697 struct glyph_row *row = it->glyph_row;
7698 int max_x = it->last_visible_x;
7699 struct glyph *last;
7700
7701 prepare_desired_row (row);
7702 row->y = it->current_y;
7703
7704 /* Note that this isn't made use of if the face hasn't a box,
7705 so there's no need to check the face here. */
7706 it->start_of_box_run_p = 1;
7707
7708 while (it->current_x < max_x)
7709 {
7710 int x_before, x, n_glyphs_before, i, nglyphs;
7711
7712 /* Get the next display element. */
7713 if (!get_next_display_element (it))
7714 break;
7715
7716 /* Produce glyphs. */
7717 x_before = it->current_x;
7718 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
7719 PRODUCE_GLYPHS (it);
7720
7721 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
7722 i = 0;
7723 x = x_before;
7724 while (i < nglyphs)
7725 {
7726 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
7727
7728 if (x + glyph->pixel_width > max_x)
7729 {
7730 /* Glyph doesn't fit on line. */
7731 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
7732 it->current_x = x;
7733 goto out;
7734 }
7735
7736 ++it->hpos;
7737 x += glyph->pixel_width;
7738 ++i;
7739 }
7740
7741 /* Stop at line ends. */
7742 if (ITERATOR_AT_END_OF_LINE_P (it))
7743 break;
7744
7745 set_iterator_to_next (it, 1);
7746 }
7747
7748 out:;
7749
7750 row->displays_text_p = row->used[TEXT_AREA] != 0;
7751 extend_face_to_end_of_line (it);
7752 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
7753 last->right_box_line_p = 1;
7754 if (last == row->glyphs[TEXT_AREA])
7755 last->left_box_line_p = 1;
7756 compute_line_metrics (it);
7757
7758 /* If line is empty, make it occupy the rest of the tool-bar. */
7759 if (!row->displays_text_p)
7760 {
7761 row->height = row->phys_height = it->last_visible_y - row->y;
7762 row->ascent = row->phys_ascent = 0;
7763 }
7764
7765 row->full_width_p = 1;
7766 row->continued_p = 0;
7767 row->truncated_on_left_p = 0;
7768 row->truncated_on_right_p = 0;
7769
7770 it->current_x = it->hpos = 0;
7771 it->current_y += row->height;
7772 ++it->vpos;
7773 ++it->glyph_row;
7774 }
7775
7776
7777 /* Value is the number of screen lines needed to make all tool-bar
7778 items of frame F visible. */
7779
7780 static int
7781 tool_bar_lines_needed (f)
7782 struct frame *f;
7783 {
7784 struct window *w = XWINDOW (f->tool_bar_window);
7785 struct it it;
7786
7787 /* Initialize an iterator for iteration over
7788 F->desired_tool_bar_string in the tool-bar window of frame F. */
7789 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7790 it.first_visible_x = 0;
7791 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7792 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7793
7794 while (!ITERATOR_AT_END_P (&it))
7795 {
7796 it.glyph_row = w->desired_matrix->rows;
7797 clear_glyph_row (it.glyph_row);
7798 display_tool_bar_line (&it);
7799 }
7800
7801 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
7802 }
7803
7804
7805 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
7806 0, 1, 0,
7807 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
7808 (frame)
7809 Lisp_Object frame;
7810 {
7811 struct frame *f;
7812 struct window *w;
7813 int nlines = 0;
7814
7815 if (NILP (frame))
7816 frame = selected_frame;
7817 else
7818 CHECK_FRAME (frame);
7819 f = XFRAME (frame);
7820
7821 if (WINDOWP (f->tool_bar_window)
7822 || (w = XWINDOW (f->tool_bar_window),
7823 XFASTINT (w->height) > 0))
7824 {
7825 update_tool_bar (f, 1);
7826 if (f->n_tool_bar_items)
7827 {
7828 build_desired_tool_bar_string (f);
7829 nlines = tool_bar_lines_needed (f);
7830 }
7831 }
7832
7833 return make_number (nlines);
7834 }
7835
7836
7837 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
7838 height should be changed. */
7839
7840 static int
7841 redisplay_tool_bar (f)
7842 struct frame *f;
7843 {
7844 struct window *w;
7845 struct it it;
7846 struct glyph_row *row;
7847 int change_height_p = 0;
7848
7849 /* If frame hasn't a tool-bar window or if it is zero-height, don't
7850 do anything. This means you must start with tool-bar-lines
7851 non-zero to get the auto-sizing effect. Or in other words, you
7852 can turn off tool-bars by specifying tool-bar-lines zero. */
7853 if (!WINDOWP (f->tool_bar_window)
7854 || (w = XWINDOW (f->tool_bar_window),
7855 XFASTINT (w->height) == 0))
7856 return 0;
7857
7858 /* Set up an iterator for the tool-bar window. */
7859 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7860 it.first_visible_x = 0;
7861 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7862 row = it.glyph_row;
7863
7864 /* Build a string that represents the contents of the tool-bar. */
7865 build_desired_tool_bar_string (f);
7866 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7867
7868 /* Display as many lines as needed to display all tool-bar items. */
7869 while (it.current_y < it.last_visible_y)
7870 display_tool_bar_line (&it);
7871
7872 /* It doesn't make much sense to try scrolling in the tool-bar
7873 window, so don't do it. */
7874 w->desired_matrix->no_scrolling_p = 1;
7875 w->must_be_updated_p = 1;
7876
7877 if (auto_resize_tool_bars_p)
7878 {
7879 int nlines;
7880
7881 /* If we couldn't display everything, change the tool-bar's
7882 height. */
7883 if (IT_STRING_CHARPOS (it) < it.end_charpos)
7884 change_height_p = 1;
7885
7886 /* If there are blank lines at the end, except for a partially
7887 visible blank line at the end that is smaller than
7888 CANON_Y_UNIT, change the tool-bar's height. */
7889 row = it.glyph_row - 1;
7890 if (!row->displays_text_p
7891 && row->height >= CANON_Y_UNIT (f))
7892 change_height_p = 1;
7893
7894 /* If row displays tool-bar items, but is partially visible,
7895 change the tool-bar's height. */
7896 if (row->displays_text_p
7897 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
7898 change_height_p = 1;
7899
7900 /* Resize windows as needed by changing the `tool-bar-lines'
7901 frame parameter. */
7902 if (change_height_p
7903 && (nlines = tool_bar_lines_needed (f),
7904 nlines != XFASTINT (w->height)))
7905 {
7906 extern Lisp_Object Qtool_bar_lines;
7907 Lisp_Object frame;
7908 int old_height = XFASTINT (w->height);
7909
7910 XSETFRAME (frame, f);
7911 clear_glyph_matrix (w->desired_matrix);
7912 Fmodify_frame_parameters (frame,
7913 Fcons (Fcons (Qtool_bar_lines,
7914 make_number (nlines)),
7915 Qnil));
7916 if (XFASTINT (w->height) != old_height)
7917 fonts_changed_p = 1;
7918 }
7919 }
7920
7921 return change_height_p;
7922 }
7923
7924
7925 /* Get information about the tool-bar item which is displayed in GLYPH
7926 on frame F. Return in *PROP_IDX the index where tool-bar item
7927 properties start in F->tool_bar_items. Value is zero if
7928 GLYPH doesn't display a tool-bar item. */
7929
7930 int
7931 tool_bar_item_info (f, glyph, prop_idx)
7932 struct frame *f;
7933 struct glyph *glyph;
7934 int *prop_idx;
7935 {
7936 Lisp_Object prop;
7937 int success_p;
7938
7939 /* Get the text property `menu-item' at pos. The value of that
7940 property is the start index of this item's properties in
7941 F->tool_bar_items. */
7942 prop = Fget_text_property (make_number (glyph->charpos),
7943 Qmenu_item, f->current_tool_bar_string);
7944 if (INTEGERP (prop))
7945 {
7946 *prop_idx = XINT (prop);
7947 success_p = 1;
7948 }
7949 else
7950 success_p = 0;
7951
7952 return success_p;
7953 }
7954
7955 #endif /* HAVE_WINDOW_SYSTEM */
7956
7957
7958 \f
7959 /************************************************************************
7960 Horizontal scrolling
7961 ************************************************************************/
7962
7963 static int hscroll_window_tree P_ ((Lisp_Object));
7964 static int hscroll_windows P_ ((Lisp_Object));
7965
7966 /* For all leaf windows in the window tree rooted at WINDOW, set their
7967 hscroll value so that PT is (i) visible in the window, and (ii) so
7968 that it is not within a certain margin at the window's left and
7969 right border. Value is non-zero if any window's hscroll has been
7970 changed. */
7971
7972 static int
7973 hscroll_window_tree (window)
7974 Lisp_Object window;
7975 {
7976 int hscrolled_p = 0;
7977
7978 while (WINDOWP (window))
7979 {
7980 struct window *w = XWINDOW (window);
7981
7982 if (WINDOWP (w->hchild))
7983 hscrolled_p |= hscroll_window_tree (w->hchild);
7984 else if (WINDOWP (w->vchild))
7985 hscrolled_p |= hscroll_window_tree (w->vchild);
7986 else if (w->cursor.vpos >= 0)
7987 {
7988 int hscroll_margin, text_area_x, text_area_y;
7989 int text_area_width, text_area_height;
7990 struct glyph_row *current_cursor_row
7991 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
7992 struct glyph_row *desired_cursor_row
7993 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
7994 struct glyph_row *cursor_row
7995 = (desired_cursor_row->enabled_p
7996 ? desired_cursor_row
7997 : current_cursor_row);
7998
7999 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
8000 &text_area_width, &text_area_height);
8001
8002 /* Scroll when cursor is inside this scroll margin. */
8003 /* Shouldn't we export this `5' for customization ? -stef */
8004 hscroll_margin = 5 * CANON_X_UNIT (XFRAME (w->frame));
8005
8006 if ((XFASTINT (w->hscroll)
8007 && w->cursor.x < hscroll_margin)
8008 || (cursor_row->enabled_p
8009 && cursor_row->truncated_on_right_p
8010 && (w->cursor.x > text_area_width - hscroll_margin)))
8011 {
8012 struct it it;
8013 int hscroll;
8014 struct buffer *saved_current_buffer;
8015 int pt;
8016
8017 /* Find point in a display of infinite width. */
8018 saved_current_buffer = current_buffer;
8019 current_buffer = XBUFFER (w->buffer);
8020
8021 if (w == XWINDOW (selected_window))
8022 pt = BUF_PT (current_buffer);
8023 else
8024 {
8025 pt = marker_position (w->pointm);
8026 pt = max (BEGV, pt);
8027 pt = min (ZV, pt);
8028 }
8029
8030 /* Move iterator to pt starting at cursor_row->start in
8031 a line with infinite width. */
8032 init_to_row_start (&it, w, cursor_row);
8033 it.last_visible_x = INFINITY;
8034 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
8035 current_buffer = saved_current_buffer;
8036
8037 /* Center cursor in window. */
8038 hscroll = (max (0, it.current_x - text_area_width / 2)
8039 / CANON_X_UNIT (it.f));
8040 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
8041
8042 /* Don't call Fset_window_hscroll if value hasn't
8043 changed because it will prevent redisplay
8044 optimizations. */
8045 if (XFASTINT (w->hscroll) != hscroll)
8046 {
8047 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
8048 w->hscroll = make_number (hscroll);
8049 hscrolled_p = 1;
8050 }
8051 }
8052 }
8053
8054 window = w->next;
8055 }
8056
8057 /* Value is non-zero if hscroll of any leaf window has been changed. */
8058 return hscrolled_p;
8059 }
8060
8061
8062 /* Set hscroll so that cursor is visible and not inside horizontal
8063 scroll margins for all windows in the tree rooted at WINDOW. See
8064 also hscroll_window_tree above. Value is non-zero if any window's
8065 hscroll has been changed. If it has, desired matrices on the frame
8066 of WINDOW are cleared. */
8067
8068 static int
8069 hscroll_windows (window)
8070 Lisp_Object window;
8071 {
8072 int hscrolled_p;
8073
8074 if (automatic_hscrolling_p)
8075 {
8076 hscrolled_p = hscroll_window_tree (window);
8077 if (hscrolled_p)
8078 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
8079 }
8080 else
8081 hscrolled_p = 0;
8082 return hscrolled_p;
8083 }
8084
8085
8086 \f
8087 /************************************************************************
8088 Redisplay
8089 ************************************************************************/
8090
8091 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
8092 to a non-zero value. This is sometimes handy to have in a debugger
8093 session. */
8094
8095 #if GLYPH_DEBUG
8096
8097 /* First and last unchanged row for try_window_id. */
8098
8099 int debug_first_unchanged_at_end_vpos;
8100 int debug_last_unchanged_at_beg_vpos;
8101
8102 /* Delta vpos and y. */
8103
8104 int debug_dvpos, debug_dy;
8105
8106 /* Delta in characters and bytes for try_window_id. */
8107
8108 int debug_delta, debug_delta_bytes;
8109
8110 /* Values of window_end_pos and window_end_vpos at the end of
8111 try_window_id. */
8112
8113 int debug_end_pos, debug_end_vpos;
8114
8115 /* Append a string to W->desired_matrix->method. FMT is a printf
8116 format string. A1...A9 are a supplement for a variable-length
8117 argument list. If trace_redisplay_p is non-zero also printf the
8118 resulting string to stderr. */
8119
8120 static void
8121 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
8122 struct window *w;
8123 char *fmt;
8124 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
8125 {
8126 char buffer[512];
8127 char *method = w->desired_matrix->method;
8128 int len = strlen (method);
8129 int size = sizeof w->desired_matrix->method;
8130 int remaining = size - len - 1;
8131
8132 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
8133 if (len && remaining)
8134 {
8135 method[len] = '|';
8136 --remaining, ++len;
8137 }
8138
8139 strncpy (method + len, buffer, remaining);
8140
8141 if (trace_redisplay_p)
8142 fprintf (stderr, "%p (%s): %s\n",
8143 w,
8144 ((BUFFERP (w->buffer)
8145 && STRINGP (XBUFFER (w->buffer)->name))
8146 ? (char *) XSTRING (XBUFFER (w->buffer)->name)->data
8147 : "no buffer"),
8148 buffer);
8149 }
8150
8151 #endif /* GLYPH_DEBUG */
8152
8153
8154 /* This counter is used to clear the face cache every once in a while
8155 in redisplay_internal. It is incremented for each redisplay.
8156 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
8157 cleared. */
8158
8159 #define CLEAR_FACE_CACHE_COUNT 10000
8160 static int clear_face_cache_count;
8161
8162 /* Record the previous terminal frame we displayed. */
8163
8164 static struct frame *previous_terminal_frame;
8165
8166 /* Non-zero while redisplay_internal is in progress. */
8167
8168 int redisplaying_p;
8169
8170
8171 /* Value is non-zero if all changes in window W, which displays
8172 current_buffer, are in the text between START and END. START is a
8173 buffer position, END is given as a distance from Z. Used in
8174 redisplay_internal for display optimization. */
8175
8176 static INLINE int
8177 text_outside_line_unchanged_p (w, start, end)
8178 struct window *w;
8179 int start, end;
8180 {
8181 int unchanged_p = 1;
8182
8183 /* If text or overlays have changed, see where. */
8184 if (XFASTINT (w->last_modified) < MODIFF
8185 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8186 {
8187 /* Gap in the line? */
8188 if (GPT < start || Z - GPT < end)
8189 unchanged_p = 0;
8190
8191 /* Changes start in front of the line, or end after it? */
8192 if (unchanged_p
8193 && (BEG_UNCHANGED < start - 1
8194 || END_UNCHANGED < end))
8195 unchanged_p = 0;
8196
8197 /* If selective display, can't optimize if changes start at the
8198 beginning of the line. */
8199 if (unchanged_p
8200 && INTEGERP (current_buffer->selective_display)
8201 && XINT (current_buffer->selective_display) > 0
8202 && (BEG_UNCHANGED < start || GPT <= start))
8203 unchanged_p = 0;
8204
8205 /* If there are overlays at the start or end of the line, these
8206 may have overlay strings with newlines in them. A change at
8207 START, for instance, may actually concern the display of such
8208 overlay strings as well, and they are displayed on different
8209 lines. So, quickly rule out this case. (For the future, it
8210 might be desirable to implement something more telling than
8211 just BEG/END_UNCHANGED.) */
8212 if (unchanged_p)
8213 {
8214 if (BEG + BEG_UNCHANGED == start
8215 && overlay_touches_p (start))
8216 unchanged_p = 0;
8217 if (END_UNCHANGED == end
8218 && overlay_touches_p (Z - end))
8219 unchanged_p = 0;
8220 }
8221 }
8222
8223 return unchanged_p;
8224 }
8225
8226
8227 /* Do a frame update, taking possible shortcuts into account. This is
8228 the main external entry point for redisplay.
8229
8230 If the last redisplay displayed an echo area message and that message
8231 is no longer requested, we clear the echo area or bring back the
8232 mini-buffer if that is in use. */
8233
8234 void
8235 redisplay ()
8236 {
8237 redisplay_internal (0);
8238 }
8239
8240
8241 /* Return 1 if point moved out of or into a composition. Otherwise
8242 return 0. PREV_BUF and PREV_PT are the last point buffer and
8243 position. BUF and PT are the current point buffer and position. */
8244
8245 int
8246 check_point_in_composition (prev_buf, prev_pt, buf, pt)
8247 struct buffer *prev_buf, *buf;
8248 int prev_pt, pt;
8249 {
8250 int start, end;
8251 Lisp_Object prop;
8252 Lisp_Object buffer;
8253
8254 XSETBUFFER (buffer, buf);
8255 /* Check a composition at the last point if point moved within the
8256 same buffer. */
8257 if (prev_buf == buf)
8258 {
8259 if (prev_pt == pt)
8260 /* Point didn't move. */
8261 return 0;
8262
8263 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
8264 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
8265 && COMPOSITION_VALID_P (start, end, prop)
8266 && start < prev_pt && end > prev_pt)
8267 /* The last point was within the composition. Return 1 iff
8268 point moved out of the composition. */
8269 return (pt <= start || pt >= end);
8270 }
8271
8272 /* Check a composition at the current point. */
8273 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
8274 && find_composition (pt, -1, &start, &end, &prop, buffer)
8275 && COMPOSITION_VALID_P (start, end, prop)
8276 && start < pt && end > pt);
8277 }
8278
8279
8280 /* Reconsider the setting of B->clip_changed which is displayed
8281 in window W. */
8282
8283 static INLINE void
8284 reconsider_clip_changes (w, b)
8285 struct window *w;
8286 struct buffer *b;
8287 {
8288 if (b->prevent_redisplay_optimizations_p)
8289 b->clip_changed = 1;
8290 else if (b->clip_changed
8291 && !NILP (w->window_end_valid)
8292 && w->current_matrix->buffer == b
8293 && w->current_matrix->zv == BUF_ZV (b)
8294 && w->current_matrix->begv == BUF_BEGV (b))
8295 b->clip_changed = 0;
8296
8297 /* If display wasn't paused, and W is not a tool bar window, see if
8298 point has been moved into or out of a composition. In that case,
8299 we set b->clip_changed to 1 to force updating the screen. If
8300 b->clip_changed has already been set to 1, we can skip this
8301 check. */
8302 if (!b->clip_changed
8303 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
8304 {
8305 int pt;
8306
8307 if (w == XWINDOW (selected_window))
8308 pt = BUF_PT (current_buffer);
8309 else
8310 pt = marker_position (w->pointm);
8311
8312 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
8313 || pt != XINT (w->last_point))
8314 && check_point_in_composition (w->current_matrix->buffer,
8315 XINT (w->last_point),
8316 XBUFFER (w->buffer), pt))
8317 b->clip_changed = 1;
8318 }
8319 }
8320
8321
8322 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
8323 response to any user action; therefore, we should preserve the echo
8324 area. (Actually, our caller does that job.) Perhaps in the future
8325 avoid recentering windows if it is not necessary; currently that
8326 causes some problems. */
8327
8328 static void
8329 redisplay_internal (preserve_echo_area)
8330 int preserve_echo_area;
8331 {
8332 struct window *w = XWINDOW (selected_window);
8333 struct frame *f = XFRAME (w->frame);
8334 int pause;
8335 int must_finish = 0;
8336 struct text_pos tlbufpos, tlendpos;
8337 int number_of_visible_frames;
8338 int count;
8339 struct frame *sf = SELECTED_FRAME ();
8340
8341 /* Non-zero means redisplay has to consider all windows on all
8342 frames. Zero means, only selected_window is considered. */
8343 int consider_all_windows_p;
8344
8345 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
8346
8347 /* No redisplay if running in batch mode or frame is not yet fully
8348 initialized, or redisplay is explicitly turned off by setting
8349 Vinhibit_redisplay. */
8350 if (noninteractive
8351 || !NILP (Vinhibit_redisplay)
8352 || !f->glyphs_initialized_p)
8353 return;
8354
8355 /* The flag redisplay_performed_directly_p is set by
8356 direct_output_for_insert when it already did the whole screen
8357 update necessary. */
8358 if (redisplay_performed_directly_p)
8359 {
8360 redisplay_performed_directly_p = 0;
8361 if (!hscroll_windows (selected_window))
8362 return;
8363 }
8364
8365 #ifdef USE_X_TOOLKIT
8366 if (popup_activated ())
8367 return;
8368 #endif
8369
8370 /* I don't think this happens but let's be paranoid. */
8371 if (redisplaying_p)
8372 return;
8373
8374 /* Record a function that resets redisplaying_p to its old value
8375 when we leave this function. */
8376 count = BINDING_STACK_SIZE ();
8377 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
8378 ++redisplaying_p;
8379
8380 retry:
8381 pause = 0;
8382 reconsider_clip_changes (w, current_buffer);
8383
8384 /* If new fonts have been loaded that make a glyph matrix adjustment
8385 necessary, do it. */
8386 if (fonts_changed_p)
8387 {
8388 adjust_glyphs (NULL);
8389 ++windows_or_buffers_changed;
8390 fonts_changed_p = 0;
8391 }
8392
8393 /* If face_change_count is non-zero, init_iterator will free all
8394 realized faces, which includes the faces referenced from current
8395 matrices. So, we can't reuse current matrices in this case. */
8396 if (face_change_count)
8397 ++windows_or_buffers_changed;
8398
8399 if (! FRAME_WINDOW_P (sf)
8400 && previous_terminal_frame != sf)
8401 {
8402 /* Since frames on an ASCII terminal share the same display
8403 area, displaying a different frame means redisplay the whole
8404 thing. */
8405 windows_or_buffers_changed++;
8406 SET_FRAME_GARBAGED (sf);
8407 XSETFRAME (Vterminal_frame, sf);
8408 }
8409 previous_terminal_frame = sf;
8410
8411 /* Set the visible flags for all frames. Do this before checking
8412 for resized or garbaged frames; they want to know if their frames
8413 are visible. See the comment in frame.h for
8414 FRAME_SAMPLE_VISIBILITY. */
8415 {
8416 Lisp_Object tail, frame;
8417
8418 number_of_visible_frames = 0;
8419
8420 FOR_EACH_FRAME (tail, frame)
8421 {
8422 struct frame *f = XFRAME (frame);
8423
8424 FRAME_SAMPLE_VISIBILITY (f);
8425 if (FRAME_VISIBLE_P (f))
8426 ++number_of_visible_frames;
8427 clear_desired_matrices (f);
8428 }
8429 }
8430
8431 /* Notice any pending interrupt request to change frame size. */
8432 do_pending_window_change (1);
8433
8434 /* Clear frames marked as garbaged. */
8435 if (frame_garbaged)
8436 clear_garbaged_frames ();
8437
8438 /* Build menubar and tool-bar items. */
8439 prepare_menu_bars ();
8440
8441 if (windows_or_buffers_changed)
8442 update_mode_lines++;
8443
8444 /* Detect case that we need to write or remove a star in the mode line. */
8445 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
8446 {
8447 w->update_mode_line = Qt;
8448 if (buffer_shared > 1)
8449 update_mode_lines++;
8450 }
8451
8452 /* If %c is in the mode line, update it if needed. */
8453 if (!NILP (w->column_number_displayed)
8454 /* This alternative quickly identifies a common case
8455 where no change is needed. */
8456 && !(PT == XFASTINT (w->last_point)
8457 && XFASTINT (w->last_modified) >= MODIFF
8458 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
8459 && XFASTINT (w->column_number_displayed) != current_column ())
8460 w->update_mode_line = Qt;
8461
8462 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
8463
8464 /* The variable buffer_shared is set in redisplay_window and
8465 indicates that we redisplay a buffer in different windows. See
8466 there. */
8467 consider_all_windows_p = update_mode_lines || buffer_shared > 1;
8468
8469 /* If specs for an arrow have changed, do thorough redisplay
8470 to ensure we remove any arrow that should no longer exist. */
8471 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
8472 || ! EQ (Voverlay_arrow_string, last_arrow_string))
8473 consider_all_windows_p = windows_or_buffers_changed = 1;
8474
8475 /* Normally the message* functions will have already displayed and
8476 updated the echo area, but the frame may have been trashed, or
8477 the update may have been preempted, so display the echo area
8478 again here. Checking message_cleared_p captures the case that
8479 the echo area should be cleared. */
8480 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
8481 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
8482 || (message_cleared_p
8483 && minibuf_level == 0
8484 /* If the mini-window is currently selected, this means the
8485 echo-area doesn't show through. */
8486 && !MINI_WINDOW_P (XWINDOW (selected_window))))
8487 {
8488 int window_height_changed_p = echo_area_display (0);
8489 must_finish = 1;
8490
8491 /* If we don't display the current message, don't clear the
8492 message_cleared_p flag, because, if we did, we wouldn't clear
8493 the echo area in the next redisplay which doesn't preserve
8494 the echo area. */
8495 if (!display_last_displayed_message_p)
8496 message_cleared_p = 0;
8497
8498 if (fonts_changed_p)
8499 goto retry;
8500 else if (window_height_changed_p)
8501 {
8502 consider_all_windows_p = 1;
8503 ++update_mode_lines;
8504 ++windows_or_buffers_changed;
8505
8506 /* If window configuration was changed, frames may have been
8507 marked garbaged. Clear them or we will experience
8508 surprises wrt scrolling. */
8509 if (frame_garbaged)
8510 clear_garbaged_frames ();
8511 }
8512 }
8513 else if (EQ (selected_window, minibuf_window)
8514 && (current_buffer->clip_changed
8515 || XFASTINT (w->last_modified) < MODIFF
8516 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8517 && resize_mini_window (w, 0))
8518 {
8519 /* Resized active mini-window to fit the size of what it is
8520 showing if its contents might have changed. */
8521 must_finish = 1;
8522 consider_all_windows_p = 1;
8523 ++windows_or_buffers_changed;
8524 ++update_mode_lines;
8525
8526 /* If window configuration was changed, frames may have been
8527 marked garbaged. Clear them or we will experience
8528 surprises wrt scrolling. */
8529 if (frame_garbaged)
8530 clear_garbaged_frames ();
8531 }
8532
8533
8534 /* If showing the region, and mark has changed, we must redisplay
8535 the whole window. The assignment to this_line_start_pos prevents
8536 the optimization directly below this if-statement. */
8537 if (((!NILP (Vtransient_mark_mode)
8538 && !NILP (XBUFFER (w->buffer)->mark_active))
8539 != !NILP (w->region_showing))
8540 || (!NILP (w->region_showing)
8541 && !EQ (w->region_showing,
8542 Fmarker_position (XBUFFER (w->buffer)->mark))))
8543 CHARPOS (this_line_start_pos) = 0;
8544
8545 /* Optimize the case that only the line containing the cursor in the
8546 selected window has changed. Variables starting with this_ are
8547 set in display_line and record information about the line
8548 containing the cursor. */
8549 tlbufpos = this_line_start_pos;
8550 tlendpos = this_line_end_pos;
8551 if (!consider_all_windows_p
8552 && CHARPOS (tlbufpos) > 0
8553 && NILP (w->update_mode_line)
8554 && !current_buffer->clip_changed
8555 && FRAME_VISIBLE_P (XFRAME (w->frame))
8556 && !FRAME_OBSCURED_P (XFRAME (w->frame))
8557 /* Make sure recorded data applies to current buffer, etc. */
8558 && this_line_buffer == current_buffer
8559 && current_buffer == XBUFFER (w->buffer)
8560 && NILP (w->force_start)
8561 /* Point must be on the line that we have info recorded about. */
8562 && PT >= CHARPOS (tlbufpos)
8563 && PT <= Z - CHARPOS (tlendpos)
8564 /* All text outside that line, including its final newline,
8565 must be unchanged */
8566 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
8567 CHARPOS (tlendpos)))
8568 {
8569 if (CHARPOS (tlbufpos) > BEGV
8570 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
8571 && (CHARPOS (tlbufpos) == ZV
8572 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
8573 /* Former continuation line has disappeared by becoming empty */
8574 goto cancel;
8575 else if (XFASTINT (w->last_modified) < MODIFF
8576 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
8577 || MINI_WINDOW_P (w))
8578 {
8579 /* We have to handle the case of continuation around a
8580 wide-column character (See the comment in indent.c around
8581 line 885).
8582
8583 For instance, in the following case:
8584
8585 -------- Insert --------
8586 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
8587 J_I_ ==> J_I_ `^^' are cursors.
8588 ^^ ^^
8589 -------- --------
8590
8591 As we have to redraw the line above, we should goto cancel. */
8592
8593 struct it it;
8594 int line_height_before = this_line_pixel_height;
8595
8596 /* Note that start_display will handle the case that the
8597 line starting at tlbufpos is a continuation lines. */
8598 start_display (&it, w, tlbufpos);
8599
8600 /* Implementation note: It this still necessary? */
8601 if (it.current_x != this_line_start_x)
8602 goto cancel;
8603
8604 TRACE ((stderr, "trying display optimization 1\n"));
8605 w->cursor.vpos = -1;
8606 overlay_arrow_seen = 0;
8607 it.vpos = this_line_vpos;
8608 it.current_y = this_line_y;
8609 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
8610 display_line (&it);
8611
8612 /* If line contains point, is not continued,
8613 and ends at same distance from eob as before, we win */
8614 if (w->cursor.vpos >= 0
8615 /* Line is not continued, otherwise this_line_start_pos
8616 would have been set to 0 in display_line. */
8617 && CHARPOS (this_line_start_pos)
8618 /* Line ends as before. */
8619 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
8620 /* Line has same height as before. Otherwise other lines
8621 would have to be shifted up or down. */
8622 && this_line_pixel_height == line_height_before)
8623 {
8624 /* If this is not the window's last line, we must adjust
8625 the charstarts of the lines below. */
8626 if (it.current_y < it.last_visible_y)
8627 {
8628 struct glyph_row *row
8629 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
8630 int delta, delta_bytes;
8631
8632 if (Z - CHARPOS (tlendpos) == ZV)
8633 {
8634 /* This line ends at end of (accessible part of)
8635 buffer. There is no newline to count. */
8636 delta = (Z
8637 - CHARPOS (tlendpos)
8638 - MATRIX_ROW_START_CHARPOS (row));
8639 delta_bytes = (Z_BYTE
8640 - BYTEPOS (tlendpos)
8641 - MATRIX_ROW_START_BYTEPOS (row));
8642 }
8643 else
8644 {
8645 /* This line ends in a newline. Must take
8646 account of the newline and the rest of the
8647 text that follows. */
8648 delta = (Z
8649 - CHARPOS (tlendpos)
8650 - MATRIX_ROW_START_CHARPOS (row));
8651 delta_bytes = (Z_BYTE
8652 - BYTEPOS (tlendpos)
8653 - MATRIX_ROW_START_BYTEPOS (row));
8654 }
8655
8656 increment_matrix_positions (w->current_matrix,
8657 this_line_vpos + 1,
8658 w->current_matrix->nrows,
8659 delta, delta_bytes);
8660 }
8661
8662 /* If this row displays text now but previously didn't,
8663 or vice versa, w->window_end_vpos may have to be
8664 adjusted. */
8665 if ((it.glyph_row - 1)->displays_text_p)
8666 {
8667 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
8668 XSETINT (w->window_end_vpos, this_line_vpos);
8669 }
8670 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
8671 && this_line_vpos > 0)
8672 XSETINT (w->window_end_vpos, this_line_vpos - 1);
8673 w->window_end_valid = Qnil;
8674
8675 /* Update hint: No need to try to scroll in update_window. */
8676 w->desired_matrix->no_scrolling_p = 1;
8677
8678 #if GLYPH_DEBUG
8679 *w->desired_matrix->method = 0;
8680 debug_method_add (w, "optimization 1");
8681 #endif
8682 goto update;
8683 }
8684 else
8685 goto cancel;
8686 }
8687 else if (/* Cursor position hasn't changed. */
8688 PT == XFASTINT (w->last_point)
8689 /* Make sure the cursor was last displayed
8690 in this window. Otherwise we have to reposition it. */
8691 && 0 <= w->cursor.vpos
8692 && XINT (w->height) > w->cursor.vpos)
8693 {
8694 if (!must_finish)
8695 {
8696 do_pending_window_change (1);
8697
8698 /* We used to always goto end_of_redisplay here, but this
8699 isn't enough if we have a blinking cursor. */
8700 if (w->cursor_off_p == w->last_cursor_off_p)
8701 goto end_of_redisplay;
8702 }
8703 goto update;
8704 }
8705 /* If highlighting the region, or if the cursor is in the echo area,
8706 then we can't just move the cursor. */
8707 else if (! (!NILP (Vtransient_mark_mode)
8708 && !NILP (current_buffer->mark_active))
8709 && (EQ (selected_window, current_buffer->last_selected_window)
8710 || highlight_nonselected_windows)
8711 && NILP (w->region_showing)
8712 && NILP (Vshow_trailing_whitespace)
8713 && !cursor_in_echo_area)
8714 {
8715 struct it it;
8716 struct glyph_row *row;
8717
8718 /* Skip from tlbufpos to PT and see where it is. Note that
8719 PT may be in invisible text. If so, we will end at the
8720 next visible position. */
8721 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
8722 NULL, DEFAULT_FACE_ID);
8723 it.current_x = this_line_start_x;
8724 it.current_y = this_line_y;
8725 it.vpos = this_line_vpos;
8726
8727 /* The call to move_it_to stops in front of PT, but
8728 moves over before-strings. */
8729 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
8730
8731 if (it.vpos == this_line_vpos
8732 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
8733 row->enabled_p))
8734 {
8735 xassert (this_line_vpos == it.vpos);
8736 xassert (this_line_y == it.current_y);
8737 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
8738 #if GLYPH_DEBUG
8739 *w->desired_matrix->method = 0;
8740 debug_method_add (w, "optimization 3");
8741 #endif
8742 goto update;
8743 }
8744 else
8745 goto cancel;
8746 }
8747
8748 cancel:
8749 /* Text changed drastically or point moved off of line. */
8750 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
8751 }
8752
8753 CHARPOS (this_line_start_pos) = 0;
8754 consider_all_windows_p |= buffer_shared > 1;
8755 ++clear_face_cache_count;
8756
8757
8758 /* Build desired matrices, and update the display. If
8759 consider_all_windows_p is non-zero, do it for all windows on all
8760 frames. Otherwise do it for selected_window, only. */
8761
8762 if (consider_all_windows_p)
8763 {
8764 Lisp_Object tail, frame;
8765 int i, n = 0, size = 50;
8766 struct frame **updated
8767 = (struct frame **) alloca (size * sizeof *updated);
8768
8769 /* Clear the face cache eventually. */
8770 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
8771 {
8772 clear_face_cache (0);
8773 clear_face_cache_count = 0;
8774 }
8775
8776 /* Recompute # windows showing selected buffer. This will be
8777 incremented each time such a window is displayed. */
8778 buffer_shared = 0;
8779
8780 FOR_EACH_FRAME (tail, frame)
8781 {
8782 struct frame *f = XFRAME (frame);
8783
8784 if (FRAME_WINDOW_P (f) || f == sf)
8785 {
8786 /* Mark all the scroll bars to be removed; we'll redeem
8787 the ones we want when we redisplay their windows. */
8788 if (condemn_scroll_bars_hook)
8789 condemn_scroll_bars_hook (f);
8790
8791 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8792 redisplay_windows (FRAME_ROOT_WINDOW (f));
8793
8794 /* Any scroll bars which redisplay_windows should have
8795 nuked should now go away. */
8796 if (judge_scroll_bars_hook)
8797 judge_scroll_bars_hook (f);
8798
8799 /* If fonts changed, display again. */
8800 if (fonts_changed_p)
8801 goto retry;
8802
8803 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8804 {
8805 /* See if we have to hscroll. */
8806 if (hscroll_windows (f->root_window))
8807 goto retry;
8808
8809 /* Prevent various kinds of signals during display
8810 update. stdio is not robust about handling
8811 signals, which can cause an apparent I/O
8812 error. */
8813 if (interrupt_input)
8814 unrequest_sigio ();
8815 stop_polling ();
8816
8817 /* Update the display. */
8818 set_window_update_flags (XWINDOW (f->root_window), 1);
8819 pause |= update_frame (f, 0, 0);
8820 if (pause)
8821 break;
8822
8823 if (n == size)
8824 {
8825 int nbytes = size * sizeof *updated;
8826 struct frame **p = (struct frame **) alloca (2 * nbytes);
8827 bcopy (updated, p, nbytes);
8828 size *= 2;
8829 }
8830
8831 updated[n++] = f;
8832 }
8833 }
8834 }
8835
8836 /* Do the mark_window_display_accurate after all windows have
8837 been redisplayed because this call resets flags in buffers
8838 which are needed for proper redisplay. */
8839 for (i = 0; i < n; ++i)
8840 {
8841 struct frame *f = updated[i];
8842 mark_window_display_accurate (f->root_window, 1);
8843 if (frame_up_to_date_hook)
8844 frame_up_to_date_hook (f);
8845 }
8846 }
8847 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8848 {
8849 Lisp_Object mini_window;
8850 struct frame *mini_frame;
8851
8852 redisplay_window (selected_window, 1);
8853
8854 /* Compare desired and current matrices, perform output. */
8855 update:
8856
8857 /* If fonts changed, display again. */
8858 if (fonts_changed_p)
8859 goto retry;
8860
8861 /* Prevent various kinds of signals during display update.
8862 stdio is not robust about handling signals,
8863 which can cause an apparent I/O error. */
8864 if (interrupt_input)
8865 unrequest_sigio ();
8866 stop_polling ();
8867
8868 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8869 {
8870 if (hscroll_windows (selected_window))
8871 goto retry;
8872
8873 XWINDOW (selected_window)->must_be_updated_p = 1;
8874 pause = update_frame (sf, 0, 0);
8875 }
8876
8877 /* We may have called echo_area_display at the top of this
8878 function. If the echo area is on another frame, that may
8879 have put text on a frame other than the selected one, so the
8880 above call to update_frame would not have caught it. Catch
8881 it here. */
8882 mini_window = FRAME_MINIBUF_WINDOW (sf);
8883 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8884
8885 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
8886 {
8887 XWINDOW (mini_window)->must_be_updated_p = 1;
8888 pause |= update_frame (mini_frame, 0, 0);
8889 if (!pause && hscroll_windows (mini_window))
8890 goto retry;
8891 }
8892 }
8893
8894 /* If display was paused because of pending input, make sure we do a
8895 thorough update the next time. */
8896 if (pause)
8897 {
8898 /* Prevent the optimization at the beginning of
8899 redisplay_internal that tries a single-line update of the
8900 line containing the cursor in the selected window. */
8901 CHARPOS (this_line_start_pos) = 0;
8902
8903 /* Let the overlay arrow be updated the next time. */
8904 if (!NILP (last_arrow_position))
8905 {
8906 last_arrow_position = Qt;
8907 last_arrow_string = Qt;
8908 }
8909
8910 /* If we pause after scrolling, some rows in the current
8911 matrices of some windows are not valid. */
8912 if (!WINDOW_FULL_WIDTH_P (w)
8913 && !FRAME_WINDOW_P (XFRAME (w->frame)))
8914 update_mode_lines = 1;
8915 }
8916 else
8917 {
8918 if (!consider_all_windows_p)
8919 {
8920 /* This has already been done above if
8921 consider_all_windows_p is set. */
8922 mark_window_display_accurate_1 (w, 1);
8923
8924 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
8925 last_arrow_string = Voverlay_arrow_string;
8926
8927 if (frame_up_to_date_hook != 0)
8928 frame_up_to_date_hook (sf);
8929 }
8930
8931 update_mode_lines = 0;
8932 windows_or_buffers_changed = 0;
8933 }
8934
8935 /* Start SIGIO interrupts coming again. Having them off during the
8936 code above makes it less likely one will discard output, but not
8937 impossible, since there might be stuff in the system buffer here.
8938 But it is much hairier to try to do anything about that. */
8939 if (interrupt_input)
8940 request_sigio ();
8941 start_polling ();
8942
8943 /* If a frame has become visible which was not before, redisplay
8944 again, so that we display it. Expose events for such a frame
8945 (which it gets when becoming visible) don't call the parts of
8946 redisplay constructing glyphs, so simply exposing a frame won't
8947 display anything in this case. So, we have to display these
8948 frames here explicitly. */
8949 if (!pause)
8950 {
8951 Lisp_Object tail, frame;
8952 int new_count = 0;
8953
8954 FOR_EACH_FRAME (tail, frame)
8955 {
8956 int this_is_visible = 0;
8957
8958 if (XFRAME (frame)->visible)
8959 this_is_visible = 1;
8960 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
8961 if (XFRAME (frame)->visible)
8962 this_is_visible = 1;
8963
8964 if (this_is_visible)
8965 new_count++;
8966 }
8967
8968 if (new_count != number_of_visible_frames)
8969 windows_or_buffers_changed++;
8970 }
8971
8972 /* Change frame size now if a change is pending. */
8973 do_pending_window_change (1);
8974
8975 /* If we just did a pending size change, or have additional
8976 visible frames, redisplay again. */
8977 if (windows_or_buffers_changed && !pause)
8978 goto retry;
8979
8980 end_of_redisplay:;
8981
8982 unbind_to (count, Qnil);
8983 }
8984
8985
8986 /* Redisplay, but leave alone any recent echo area message unless
8987 another message has been requested in its place.
8988
8989 This is useful in situations where you need to redisplay but no
8990 user action has occurred, making it inappropriate for the message
8991 area to be cleared. See tracking_off and
8992 wait_reading_process_input for examples of these situations.
8993
8994 FROM_WHERE is an integer saying from where this function was
8995 called. This is useful for debugging. */
8996
8997 void
8998 redisplay_preserve_echo_area (from_where)
8999 int from_where;
9000 {
9001 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
9002
9003 if (!NILP (echo_area_buffer[1]))
9004 {
9005 /* We have a previously displayed message, but no current
9006 message. Redisplay the previous message. */
9007 display_last_displayed_message_p = 1;
9008 redisplay_internal (1);
9009 display_last_displayed_message_p = 0;
9010 }
9011 else
9012 redisplay_internal (1);
9013 }
9014
9015
9016 /* Function registered with record_unwind_protect in
9017 redisplay_internal. Clears the flag indicating that a redisplay is
9018 in progress. */
9019
9020 static Lisp_Object
9021 unwind_redisplay (old_redisplaying_p)
9022 Lisp_Object old_redisplaying_p;
9023 {
9024 redisplaying_p = XFASTINT (old_redisplaying_p);
9025 return Qnil;
9026 }
9027
9028
9029 /* Mark the display of window W as accurate or inaccurate. If
9030 ACCURATE_P is non-zero mark display of W as accurate. If
9031 ACCURATE_P is zero, arrange for W to be redisplayed the next time
9032 redisplay_internal is called. */
9033
9034 static void
9035 mark_window_display_accurate_1 (w, accurate_p)
9036 struct window *w;
9037 int accurate_p;
9038 {
9039 if (BUFFERP (w->buffer))
9040 {
9041 struct buffer *b = XBUFFER (w->buffer);
9042
9043 w->last_modified
9044 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
9045 w->last_overlay_modified
9046 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
9047 w->last_had_star
9048 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
9049
9050 if (accurate_p)
9051 {
9052 b->clip_changed = 0;
9053 b->prevent_redisplay_optimizations_p = 0;
9054
9055 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
9056 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
9057 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
9058 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
9059
9060 w->current_matrix->buffer = b;
9061 w->current_matrix->begv = BUF_BEGV (b);
9062 w->current_matrix->zv = BUF_ZV (b);
9063
9064 w->last_cursor = w->cursor;
9065 w->last_cursor_off_p = w->cursor_off_p;
9066
9067 if (w == XWINDOW (selected_window))
9068 w->last_point = make_number (BUF_PT (b));
9069 else
9070 w->last_point = make_number (XMARKER (w->pointm)->charpos);
9071 }
9072 }
9073
9074 if (accurate_p)
9075 {
9076 w->window_end_valid = w->buffer;
9077 #if 0 /* This is incorrect with variable-height lines. */
9078 xassert (XINT (w->window_end_vpos)
9079 < (XINT (w->height)
9080 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
9081 #endif
9082 w->update_mode_line = Qnil;
9083 }
9084 }
9085
9086
9087 /* Mark the display of windows in the window tree rooted at WINDOW as
9088 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
9089 windows as accurate. If ACCURATE_P is zero, arrange for windows to
9090 be redisplayed the next time redisplay_internal is called. */
9091
9092 void
9093 mark_window_display_accurate (window, accurate_p)
9094 Lisp_Object window;
9095 int accurate_p;
9096 {
9097 struct window *w;
9098
9099 for (; !NILP (window); window = w->next)
9100 {
9101 w = XWINDOW (window);
9102 mark_window_display_accurate_1 (w, accurate_p);
9103
9104 if (!NILP (w->vchild))
9105 mark_window_display_accurate (w->vchild, accurate_p);
9106 if (!NILP (w->hchild))
9107 mark_window_display_accurate (w->hchild, accurate_p);
9108 }
9109
9110 if (accurate_p)
9111 {
9112 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
9113 last_arrow_string = Voverlay_arrow_string;
9114 }
9115 else
9116 {
9117 /* Force a thorough redisplay the next time by setting
9118 last_arrow_position and last_arrow_string to t, which is
9119 unequal to any useful value of Voverlay_arrow_... */
9120 last_arrow_position = Qt;
9121 last_arrow_string = Qt;
9122 }
9123 }
9124
9125
9126 /* Return value in display table DP (Lisp_Char_Table *) for character
9127 C. Since a display table doesn't have any parent, we don't have to
9128 follow parent. Do not call this function directly but use the
9129 macro DISP_CHAR_VECTOR. */
9130
9131 Lisp_Object
9132 disp_char_vector (dp, c)
9133 struct Lisp_Char_Table *dp;
9134 int c;
9135 {
9136 int code[4], i;
9137 Lisp_Object val;
9138
9139 if (SINGLE_BYTE_CHAR_P (c))
9140 return (dp->contents[c]);
9141
9142 SPLIT_CHAR (c, code[0], code[1], code[2]);
9143 if (code[1] < 32)
9144 code[1] = -1;
9145 else if (code[2] < 32)
9146 code[2] = -1;
9147
9148 /* Here, the possible range of code[0] (== charset ID) is
9149 128..max_charset. Since the top level char table contains data
9150 for multibyte characters after 256th element, we must increment
9151 code[0] by 128 to get a correct index. */
9152 code[0] += 128;
9153 code[3] = -1; /* anchor */
9154
9155 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
9156 {
9157 val = dp->contents[code[i]];
9158 if (!SUB_CHAR_TABLE_P (val))
9159 return (NILP (val) ? dp->defalt : val);
9160 }
9161
9162 /* Here, val is a sub char table. We return the default value of
9163 it. */
9164 return (dp->defalt);
9165 }
9166
9167
9168 \f
9169 /***********************************************************************
9170 Window Redisplay
9171 ***********************************************************************/
9172
9173 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
9174
9175 static void
9176 redisplay_windows (window)
9177 Lisp_Object window;
9178 {
9179 while (!NILP (window))
9180 {
9181 struct window *w = XWINDOW (window);
9182
9183 if (!NILP (w->hchild))
9184 redisplay_windows (w->hchild);
9185 else if (!NILP (w->vchild))
9186 redisplay_windows (w->vchild);
9187 else
9188 redisplay_window (window, 0);
9189
9190 window = w->next;
9191 }
9192 }
9193
9194
9195 /* Set cursor position of W. PT is assumed to be displayed in ROW.
9196 DELTA is the number of bytes by which positions recorded in ROW
9197 differ from current buffer positions. */
9198
9199 void
9200 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
9201 struct window *w;
9202 struct glyph_row *row;
9203 struct glyph_matrix *matrix;
9204 int delta, delta_bytes, dy, dvpos;
9205 {
9206 struct glyph *glyph = row->glyphs[TEXT_AREA];
9207 struct glyph *end = glyph + row->used[TEXT_AREA];
9208 int x = row->x;
9209 int pt_old = PT - delta;
9210
9211 /* Skip over glyphs not having an object at the start of the row.
9212 These are special glyphs like truncation marks on terminal
9213 frames. */
9214 if (row->displays_text_p)
9215 while (glyph < end
9216 && INTEGERP (glyph->object)
9217 && glyph->charpos < 0)
9218 {
9219 x += glyph->pixel_width;
9220 ++glyph;
9221 }
9222
9223 while (glyph < end
9224 && !INTEGERP (glyph->object)
9225 && (!BUFFERP (glyph->object)
9226 || glyph->charpos < pt_old))
9227 {
9228 x += glyph->pixel_width;
9229 ++glyph;
9230 }
9231
9232 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
9233 w->cursor.x = x;
9234 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
9235 w->cursor.y = row->y + dy;
9236
9237 if (w == XWINDOW (selected_window))
9238 {
9239 if (!row->continued_p
9240 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
9241 && row->x == 0)
9242 {
9243 this_line_buffer = XBUFFER (w->buffer);
9244
9245 CHARPOS (this_line_start_pos)
9246 = MATRIX_ROW_START_CHARPOS (row) + delta;
9247 BYTEPOS (this_line_start_pos)
9248 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
9249
9250 CHARPOS (this_line_end_pos)
9251 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
9252 BYTEPOS (this_line_end_pos)
9253 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
9254
9255 this_line_y = w->cursor.y;
9256 this_line_pixel_height = row->height;
9257 this_line_vpos = w->cursor.vpos;
9258 this_line_start_x = row->x;
9259 }
9260 else
9261 CHARPOS (this_line_start_pos) = 0;
9262 }
9263 }
9264
9265
9266 /* Run window scroll functions, if any, for WINDOW with new window
9267 start STARTP. Sets the window start of WINDOW to that position.
9268
9269 We assume that the window's buffer is really current. */
9270
9271 static INLINE struct text_pos
9272 run_window_scroll_functions (window, startp)
9273 Lisp_Object window;
9274 struct text_pos startp;
9275 {
9276 struct window *w = XWINDOW (window);
9277 SET_MARKER_FROM_TEXT_POS (w->start, startp);
9278
9279 if (current_buffer != XBUFFER (w->buffer))
9280 abort ();
9281
9282 if (!NILP (Vwindow_scroll_functions))
9283 {
9284 run_hook_with_args_2 (Qwindow_scroll_functions, window,
9285 make_number (CHARPOS (startp)));
9286 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9287 /* In case the hook functions switch buffers. */
9288 if (current_buffer != XBUFFER (w->buffer))
9289 set_buffer_internal_1 (XBUFFER (w->buffer));
9290 }
9291
9292 return startp;
9293 }
9294
9295
9296 /* Modify the desired matrix of window W and W->vscroll so that the
9297 line containing the cursor is fully visible. If this requires
9298 larger matrices than are allocated, set fonts_changed_p and return
9299 0. */
9300
9301 static int
9302 make_cursor_line_fully_visible (w)
9303 struct window *w;
9304 {
9305 struct glyph_matrix *matrix;
9306 struct glyph_row *row;
9307 int window_height;
9308
9309 /* It's not always possible to find the cursor, e.g, when a window
9310 is full of overlay strings. Don't do anything in that case. */
9311 if (w->cursor.vpos < 0)
9312 return 1;
9313
9314 matrix = w->desired_matrix;
9315 row = MATRIX_ROW (matrix, w->cursor.vpos);
9316
9317 /* If the cursor row is not partially visible, there's nothing
9318 to do. */
9319 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
9320 return 1;
9321
9322 /* If the row the cursor is in is taller than the window's height,
9323 it's not clear what to do, so do nothing. */
9324 window_height = window_box_height (w);
9325 if (row->height >= window_height)
9326 return 1;
9327
9328 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
9329 {
9330 int dy = row->height - row->visible_height;
9331 w->vscroll = 0;
9332 w->cursor.y += dy;
9333 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9334 }
9335 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
9336 {
9337 int dy = - (row->height - row->visible_height);
9338 w->vscroll = dy;
9339 w->cursor.y += dy;
9340 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9341 }
9342
9343 /* When we change the cursor y-position of the selected window,
9344 change this_line_y as well so that the display optimization for
9345 the cursor line of the selected window in redisplay_internal uses
9346 the correct y-position. */
9347 if (w == XWINDOW (selected_window))
9348 this_line_y = w->cursor.y;
9349
9350 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
9351 redisplay with larger matrices. */
9352 if (matrix->nrows < required_matrix_height (w))
9353 {
9354 fonts_changed_p = 1;
9355 return 0;
9356 }
9357
9358 return 1;
9359 }
9360
9361
9362 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
9363 non-zero means only WINDOW is redisplayed in redisplay_internal.
9364 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
9365 in redisplay_window to bring a partially visible line into view in
9366 the case that only the cursor has moved.
9367
9368 Value is
9369
9370 1 if scrolling succeeded
9371
9372 0 if scrolling didn't find point.
9373
9374 -1 if new fonts have been loaded so that we must interrupt
9375 redisplay, adjust glyph matrices, and try again. */
9376
9377 enum
9378 {
9379 SCROLLING_SUCCESS,
9380 SCROLLING_FAILED,
9381 SCROLLING_NEED_LARGER_MATRICES
9382 };
9383
9384 static int
9385 try_scrolling (window, just_this_one_p, scroll_conservatively,
9386 scroll_step, temp_scroll_step)
9387 Lisp_Object window;
9388 int just_this_one_p;
9389 int scroll_conservatively, scroll_step;
9390 int temp_scroll_step;
9391 {
9392 struct window *w = XWINDOW (window);
9393 struct frame *f = XFRAME (w->frame);
9394 struct text_pos scroll_margin_pos;
9395 struct text_pos pos;
9396 struct text_pos startp;
9397 struct it it;
9398 Lisp_Object window_end;
9399 int this_scroll_margin;
9400 int dy = 0;
9401 int scroll_max;
9402 int rc;
9403 int amount_to_scroll = 0;
9404 Lisp_Object aggressive;
9405 int height;
9406
9407 #if GLYPH_DEBUG
9408 debug_method_add (w, "try_scrolling");
9409 #endif
9410
9411 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9412
9413 /* Compute scroll margin height in pixels. We scroll when point is
9414 within this distance from the top or bottom of the window. */
9415 if (scroll_margin > 0)
9416 {
9417 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
9418 this_scroll_margin *= CANON_Y_UNIT (f);
9419 }
9420 else
9421 this_scroll_margin = 0;
9422
9423 /* Compute how much we should try to scroll maximally to bring point
9424 into view. */
9425 if (scroll_step || scroll_conservatively || temp_scroll_step)
9426 scroll_max = max (scroll_step,
9427 max (scroll_conservatively, temp_scroll_step));
9428 else if (NUMBERP (current_buffer->scroll_down_aggressively)
9429 || NUMBERP (current_buffer->scroll_up_aggressively))
9430 /* We're trying to scroll because of aggressive scrolling
9431 but no scroll_step is set. Choose an arbitrary one. Maybe
9432 there should be a variable for this. */
9433 scroll_max = 10;
9434 else
9435 scroll_max = 0;
9436 scroll_max *= CANON_Y_UNIT (f);
9437
9438 /* Decide whether we have to scroll down. Start at the window end
9439 and move this_scroll_margin up to find the position of the scroll
9440 margin. */
9441 window_end = Fwindow_end (window, Qt);
9442 CHARPOS (scroll_margin_pos) = XINT (window_end);
9443 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
9444 if (this_scroll_margin)
9445 {
9446 start_display (&it, w, scroll_margin_pos);
9447 move_it_vertically (&it, - this_scroll_margin);
9448 scroll_margin_pos = it.current.pos;
9449 }
9450
9451 if (PT >= CHARPOS (scroll_margin_pos))
9452 {
9453 int y0;
9454
9455 /* Point is in the scroll margin at the bottom of the window, or
9456 below. Compute a new window start that makes point visible. */
9457
9458 /* Compute the distance from the scroll margin to PT.
9459 Give up if the distance is greater than scroll_max. */
9460 start_display (&it, w, scroll_margin_pos);
9461 y0 = it.current_y;
9462 move_it_to (&it, PT, 0, it.last_visible_y, -1,
9463 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9464
9465 /* With a scroll_margin of 0, scroll_margin_pos is at the window
9466 end, which is one line below the window. The iterator's
9467 current_y will be same as y0 in that case, but we have to
9468 scroll a line to make PT visible. That's the reason why 1 is
9469 added below. */
9470 dy = 1 + it.current_y - y0;
9471
9472 if (dy > scroll_max)
9473 return SCROLLING_FAILED;
9474
9475 /* Move the window start down. If scrolling conservatively,
9476 move it just enough down to make point visible. If
9477 scroll_step is set, move it down by scroll_step. */
9478 start_display (&it, w, startp);
9479
9480 if (scroll_conservatively)
9481 amount_to_scroll
9482 = max (max (dy, CANON_Y_UNIT (f)),
9483 CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
9484 else if (scroll_step || temp_scroll_step)
9485 amount_to_scroll = scroll_max;
9486 else
9487 {
9488 aggressive = current_buffer->scroll_down_aggressively;
9489 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
9490 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
9491 if (NUMBERP (aggressive))
9492 amount_to_scroll = XFLOATINT (aggressive) * height;
9493 }
9494
9495 if (amount_to_scroll <= 0)
9496 return SCROLLING_FAILED;
9497
9498 move_it_vertically (&it, amount_to_scroll);
9499 startp = it.current.pos;
9500 }
9501 else
9502 {
9503 /* See if point is inside the scroll margin at the top of the
9504 window. */
9505 scroll_margin_pos = startp;
9506 if (this_scroll_margin)
9507 {
9508 start_display (&it, w, startp);
9509 move_it_vertically (&it, this_scroll_margin);
9510 scroll_margin_pos = it.current.pos;
9511 }
9512
9513 if (PT < CHARPOS (scroll_margin_pos))
9514 {
9515 /* Point is in the scroll margin at the top of the window or
9516 above what is displayed in the window. */
9517 int y0;
9518
9519 /* Compute the vertical distance from PT to the scroll
9520 margin position. Give up if distance is greater than
9521 scroll_max. */
9522 SET_TEXT_POS (pos, PT, PT_BYTE);
9523 start_display (&it, w, pos);
9524 y0 = it.current_y;
9525 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
9526 it.last_visible_y, -1,
9527 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9528 dy = it.current_y - y0;
9529 if (dy > scroll_max)
9530 return SCROLLING_FAILED;
9531
9532 /* Compute new window start. */
9533 start_display (&it, w, startp);
9534
9535 if (scroll_conservatively)
9536 amount_to_scroll =
9537 max (dy, CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
9538 else if (scroll_step || temp_scroll_step)
9539 amount_to_scroll = scroll_max;
9540 else
9541 {
9542 aggressive = current_buffer->scroll_up_aggressively;
9543 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
9544 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
9545 if (NUMBERP (aggressive))
9546 amount_to_scroll = XFLOATINT (aggressive) * height;
9547 }
9548
9549 if (amount_to_scroll <= 0)
9550 return SCROLLING_FAILED;
9551
9552 move_it_vertically (&it, - amount_to_scroll);
9553 startp = it.current.pos;
9554 }
9555 }
9556
9557 /* Run window scroll functions. */
9558 startp = run_window_scroll_functions (window, startp);
9559
9560 /* Display the window. Give up if new fonts are loaded, or if point
9561 doesn't appear. */
9562 if (!try_window (window, startp))
9563 rc = SCROLLING_NEED_LARGER_MATRICES;
9564 else if (w->cursor.vpos < 0)
9565 {
9566 clear_glyph_matrix (w->desired_matrix);
9567 rc = SCROLLING_FAILED;
9568 }
9569 else
9570 {
9571 /* Maybe forget recorded base line for line number display. */
9572 if (!just_this_one_p
9573 || current_buffer->clip_changed
9574 || BEG_UNCHANGED < CHARPOS (startp))
9575 w->base_line_number = Qnil;
9576
9577 /* If cursor ends up on a partially visible line, shift display
9578 lines up or down. If that fails because we need larger
9579 matrices, give up. */
9580 if (!make_cursor_line_fully_visible (w))
9581 rc = SCROLLING_NEED_LARGER_MATRICES;
9582 else
9583 rc = SCROLLING_SUCCESS;
9584 }
9585
9586 return rc;
9587 }
9588
9589
9590 /* Compute a suitable window start for window W if display of W starts
9591 on a continuation line. Value is non-zero if a new window start
9592 was computed.
9593
9594 The new window start will be computed, based on W's width, starting
9595 from the start of the continued line. It is the start of the
9596 screen line with the minimum distance from the old start W->start. */
9597
9598 static int
9599 compute_window_start_on_continuation_line (w)
9600 struct window *w;
9601 {
9602 struct text_pos pos, start_pos;
9603 int window_start_changed_p = 0;
9604
9605 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
9606
9607 /* If window start is on a continuation line... Window start may be
9608 < BEGV in case there's invisible text at the start of the
9609 buffer (M-x rmail, for example). */
9610 if (CHARPOS (start_pos) > BEGV
9611 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
9612 {
9613 struct it it;
9614 struct glyph_row *row;
9615
9616 /* Handle the case that the window start is out of range. */
9617 if (CHARPOS (start_pos) < BEGV)
9618 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
9619 else if (CHARPOS (start_pos) > ZV)
9620 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
9621
9622 /* Find the start of the continued line. This should be fast
9623 because scan_buffer is fast (newline cache). */
9624 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
9625 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
9626 row, DEFAULT_FACE_ID);
9627 reseat_at_previous_visible_line_start (&it);
9628
9629 /* If the line start is "too far" away from the window start,
9630 say it takes too much time to compute a new window start. */
9631 if (CHARPOS (start_pos) - IT_CHARPOS (it)
9632 < XFASTINT (w->height) * XFASTINT (w->width))
9633 {
9634 int min_distance, distance;
9635
9636 /* Move forward by display lines to find the new window
9637 start. If window width was enlarged, the new start can
9638 be expected to be > the old start. If window width was
9639 decreased, the new window start will be < the old start.
9640 So, we're looking for the display line start with the
9641 minimum distance from the old window start. */
9642 pos = it.current.pos;
9643 min_distance = INFINITY;
9644 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
9645 distance < min_distance)
9646 {
9647 min_distance = distance;
9648 pos = it.current.pos;
9649 move_it_by_lines (&it, 1, 0);
9650 }
9651
9652 /* Set the window start there. */
9653 SET_MARKER_FROM_TEXT_POS (w->start, pos);
9654 window_start_changed_p = 1;
9655 }
9656 }
9657
9658 return window_start_changed_p;
9659 }
9660
9661
9662 /* Try cursor movement in case text has not changes in window WINDOW,
9663 with window start STARTP. Value is
9664
9665 CURSOR_MOVEMENT_SUCCESS if successful
9666
9667 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
9668
9669 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
9670 display. *SCROLL_STEP is set to 1, under certain circumstances, if
9671 we want to scroll as if scroll-step were set to 1. See the code.
9672
9673 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
9674 which case we have to abort this redisplay, and adjust matrices
9675 first. */
9676
9677 enum
9678 {
9679 CURSOR_MOVEMENT_SUCCESS,
9680 CURSOR_MOVEMENT_CANNOT_BE_USED,
9681 CURSOR_MOVEMENT_MUST_SCROLL,
9682 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
9683 };
9684
9685 static int
9686 try_cursor_movement (window, startp, scroll_step)
9687 Lisp_Object window;
9688 struct text_pos startp;
9689 int *scroll_step;
9690 {
9691 struct window *w = XWINDOW (window);
9692 struct frame *f = XFRAME (w->frame);
9693 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
9694
9695 #if GLYPH_DEBUG
9696 if (inhibit_try_cursor_movement)
9697 return rc;
9698 #endif
9699
9700 /* Handle case where text has not changed, only point, and it has
9701 not moved off the frame. */
9702 if (/* Point may be in this window. */
9703 PT >= CHARPOS (startp)
9704 /* Selective display hasn't changed. */
9705 && !current_buffer->clip_changed
9706 /* Function force-mode-line-update is used to force a thorough
9707 redisplay. It sets either windows_or_buffers_changed or
9708 update_mode_lines. So don't take a shortcut here for these
9709 cases. */
9710 && !update_mode_lines
9711 && !windows_or_buffers_changed
9712 /* Can't use this case if highlighting a region. When a
9713 region exists, cursor movement has to do more than just
9714 set the cursor. */
9715 && !(!NILP (Vtransient_mark_mode)
9716 && !NILP (current_buffer->mark_active))
9717 && NILP (w->region_showing)
9718 && NILP (Vshow_trailing_whitespace)
9719 /* Right after splitting windows, last_point may be nil. */
9720 && INTEGERP (w->last_point)
9721 /* This code is not used for mini-buffer for the sake of the case
9722 of redisplaying to replace an echo area message; since in
9723 that case the mini-buffer contents per se are usually
9724 unchanged. This code is of no real use in the mini-buffer
9725 since the handling of this_line_start_pos, etc., in redisplay
9726 handles the same cases. */
9727 && !EQ (window, minibuf_window)
9728 /* When splitting windows or for new windows, it happens that
9729 redisplay is called with a nil window_end_vpos or one being
9730 larger than the window. This should really be fixed in
9731 window.c. I don't have this on my list, now, so we do
9732 approximately the same as the old redisplay code. --gerd. */
9733 && INTEGERP (w->window_end_vpos)
9734 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
9735 && (FRAME_WINDOW_P (f)
9736 || !MARKERP (Voverlay_arrow_position)
9737 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
9738 {
9739 int this_scroll_margin;
9740 struct glyph_row *row = NULL;
9741
9742 #if GLYPH_DEBUG
9743 debug_method_add (w, "cursor movement");
9744 #endif
9745
9746 /* Scroll if point within this distance from the top or bottom
9747 of the window. This is a pixel value. */
9748 this_scroll_margin = max (0, scroll_margin);
9749 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
9750 this_scroll_margin *= CANON_Y_UNIT (f);
9751
9752 /* Start with the row the cursor was displayed during the last
9753 not paused redisplay. Give up if that row is not valid. */
9754 if (w->last_cursor.vpos < 0
9755 || w->last_cursor.vpos >= w->current_matrix->nrows)
9756 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9757 else
9758 {
9759 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
9760 if (row->mode_line_p)
9761 ++row;
9762 if (!row->enabled_p)
9763 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9764 }
9765
9766 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
9767 {
9768 int scroll_p = 0;
9769 int last_y = window_text_bottom_y (w) - this_scroll_margin;
9770
9771 if (PT > XFASTINT (w->last_point))
9772 {
9773 /* Point has moved forward. */
9774 while (MATRIX_ROW_END_CHARPOS (row) < PT
9775 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
9776 {
9777 xassert (row->enabled_p);
9778 ++row;
9779 }
9780
9781 /* The end position of a row equals the start position
9782 of the next row. If PT is there, we would rather
9783 display it in the next line. */
9784 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9785 && MATRIX_ROW_END_CHARPOS (row) == PT
9786 && !cursor_row_p (w, row))
9787 ++row;
9788
9789 /* If within the scroll margin, scroll. Note that
9790 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
9791 the next line would be drawn, and that
9792 this_scroll_margin can be zero. */
9793 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
9794 || PT > MATRIX_ROW_END_CHARPOS (row)
9795 /* Line is completely visible last line in window
9796 and PT is to be set in the next line. */
9797 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
9798 && PT == MATRIX_ROW_END_CHARPOS (row)
9799 && !row->ends_at_zv_p
9800 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
9801 scroll_p = 1;
9802 }
9803 else if (PT < XFASTINT (w->last_point))
9804 {
9805 /* Cursor has to be moved backward. Note that PT >=
9806 CHARPOS (startp) because of the outer
9807 if-statement. */
9808 while (!row->mode_line_p
9809 && (MATRIX_ROW_START_CHARPOS (row) > PT
9810 || (MATRIX_ROW_START_CHARPOS (row) == PT
9811 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
9812 && (row->y > this_scroll_margin
9813 || CHARPOS (startp) == BEGV))
9814 {
9815 xassert (row->enabled_p);
9816 --row;
9817 }
9818
9819 /* Consider the following case: Window starts at BEGV,
9820 there is invisible, intangible text at BEGV, so that
9821 display starts at some point START > BEGV. It can
9822 happen that we are called with PT somewhere between
9823 BEGV and START. Try to handle that case. */
9824 if (row < w->current_matrix->rows
9825 || row->mode_line_p)
9826 {
9827 row = w->current_matrix->rows;
9828 if (row->mode_line_p)
9829 ++row;
9830 }
9831
9832 /* Due to newlines in overlay strings, we may have to
9833 skip forward over overlay strings. */
9834 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9835 && MATRIX_ROW_END_CHARPOS (row) == PT
9836 && !cursor_row_p (w, row))
9837 ++row;
9838
9839 /* If within the scroll margin, scroll. */
9840 if (row->y < this_scroll_margin
9841 && CHARPOS (startp) != BEGV)
9842 scroll_p = 1;
9843 }
9844
9845 if (PT < MATRIX_ROW_START_CHARPOS (row)
9846 || PT > MATRIX_ROW_END_CHARPOS (row))
9847 {
9848 /* if PT is not in the glyph row, give up. */
9849 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9850 }
9851 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
9852 {
9853 if (PT == MATRIX_ROW_END_CHARPOS (row)
9854 && !row->ends_at_zv_p
9855 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
9856 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9857 else if (row->height > window_box_height (w))
9858 {
9859 /* If we end up in a partially visible line, let's
9860 make it fully visible, except when it's taller
9861 than the window, in which case we can't do much
9862 about it. */
9863 *scroll_step = 1;
9864 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9865 }
9866 else
9867 {
9868 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9869 try_window (window, startp);
9870 if (!make_cursor_line_fully_visible (w))
9871 rc = CURSOR_MOVEMENT_NEED_LARGER_MATRICES;
9872 else
9873 rc = CURSOR_MOVEMENT_SUCCESS;
9874 }
9875 }
9876 else if (scroll_p)
9877 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9878 else
9879 {
9880 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9881 rc = CURSOR_MOVEMENT_SUCCESS;
9882 }
9883 }
9884 }
9885
9886 return rc;
9887 }
9888
9889
9890 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
9891 selected_window is redisplayed. */
9892
9893 static void
9894 redisplay_window (window, just_this_one_p)
9895 Lisp_Object window;
9896 int just_this_one_p;
9897 {
9898 struct window *w = XWINDOW (window);
9899 struct frame *f = XFRAME (w->frame);
9900 struct buffer *buffer = XBUFFER (w->buffer);
9901 struct buffer *old = current_buffer;
9902 struct text_pos lpoint, opoint, startp;
9903 int update_mode_line;
9904 int tem;
9905 struct it it;
9906 /* Record it now because it's overwritten. */
9907 int current_matrix_up_to_date_p = 0;
9908 int temp_scroll_step = 0;
9909 int count = BINDING_STACK_SIZE ();
9910 int rc;
9911
9912 SET_TEXT_POS (lpoint, PT, PT_BYTE);
9913 opoint = lpoint;
9914
9915 /* W must be a leaf window here. */
9916 xassert (!NILP (w->buffer));
9917 #if GLYPH_DEBUG
9918 *w->desired_matrix->method = 0;
9919 #endif
9920
9921 specbind (Qinhibit_point_motion_hooks, Qt);
9922
9923 reconsider_clip_changes (w, buffer);
9924
9925 /* Has the mode line to be updated? */
9926 update_mode_line = (!NILP (w->update_mode_line)
9927 || update_mode_lines
9928 || buffer->clip_changed);
9929
9930 if (MINI_WINDOW_P (w))
9931 {
9932 if (w == XWINDOW (echo_area_window)
9933 && !NILP (echo_area_buffer[0]))
9934 {
9935 if (update_mode_line)
9936 /* We may have to update a tty frame's menu bar or a
9937 tool-bar. Example `M-x C-h C-h C-g'. */
9938 goto finish_menu_bars;
9939 else
9940 /* We've already displayed the echo area glyphs in this window. */
9941 goto finish_scroll_bars;
9942 }
9943 else if (w != XWINDOW (minibuf_window))
9944 {
9945 /* W is a mini-buffer window, but it's not the currently
9946 active one, so clear it. */
9947 int yb = window_text_bottom_y (w);
9948 struct glyph_row *row;
9949 int y;
9950
9951 for (y = 0, row = w->desired_matrix->rows;
9952 y < yb;
9953 y += row->height, ++row)
9954 blank_row (w, row, y);
9955 goto finish_scroll_bars;
9956 }
9957
9958 clear_glyph_matrix (w->desired_matrix);
9959 }
9960
9961 /* Otherwise set up data on this window; select its buffer and point
9962 value. */
9963 /* Really select the buffer, for the sake of buffer-local
9964 variables. */
9965 set_buffer_internal_1 (XBUFFER (w->buffer));
9966 SET_TEXT_POS (opoint, PT, PT_BYTE);
9967
9968 current_matrix_up_to_date_p
9969 = (!NILP (w->window_end_valid)
9970 && !current_buffer->clip_changed
9971 && XFASTINT (w->last_modified) >= MODIFF
9972 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
9973
9974 /* When windows_or_buffers_changed is non-zero, we can't rely on
9975 the window end being valid, so set it to nil there. */
9976 if (windows_or_buffers_changed)
9977 {
9978 /* If window starts on a continuation line, maybe adjust the
9979 window start in case the window's width changed. */
9980 if (XMARKER (w->start)->buffer == current_buffer)
9981 compute_window_start_on_continuation_line (w);
9982
9983 w->window_end_valid = Qnil;
9984 }
9985
9986 /* Some sanity checks. */
9987 CHECK_WINDOW_END (w);
9988 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
9989 abort ();
9990 if (BYTEPOS (opoint) < CHARPOS (opoint))
9991 abort ();
9992
9993 /* If %c is in mode line, update it if needed. */
9994 if (!NILP (w->column_number_displayed)
9995 /* This alternative quickly identifies a common case
9996 where no change is needed. */
9997 && !(PT == XFASTINT (w->last_point)
9998 && XFASTINT (w->last_modified) >= MODIFF
9999 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
10000 && XFASTINT (w->column_number_displayed) != current_column ())
10001 update_mode_line = 1;
10002
10003 /* Count number of windows showing the selected buffer. An indirect
10004 buffer counts as its base buffer. */
10005 if (!just_this_one_p)
10006 {
10007 struct buffer *current_base, *window_base;
10008 current_base = current_buffer;
10009 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
10010 if (current_base->base_buffer)
10011 current_base = current_base->base_buffer;
10012 if (window_base->base_buffer)
10013 window_base = window_base->base_buffer;
10014 if (current_base == window_base)
10015 buffer_shared++;
10016 }
10017
10018 /* Point refers normally to the selected window. For any other
10019 window, set up appropriate value. */
10020 if (!EQ (window, selected_window))
10021 {
10022 int new_pt = XMARKER (w->pointm)->charpos;
10023 int new_pt_byte = marker_byte_position (w->pointm);
10024 if (new_pt < BEGV)
10025 {
10026 new_pt = BEGV;
10027 new_pt_byte = BEGV_BYTE;
10028 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
10029 }
10030 else if (new_pt > (ZV - 1))
10031 {
10032 new_pt = ZV;
10033 new_pt_byte = ZV_BYTE;
10034 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
10035 }
10036
10037 /* We don't use SET_PT so that the point-motion hooks don't run. */
10038 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
10039 }
10040
10041 /* If any of the character widths specified in the display table
10042 have changed, invalidate the width run cache. It's true that
10043 this may be a bit late to catch such changes, but the rest of
10044 redisplay goes (non-fatally) haywire when the display table is
10045 changed, so why should we worry about doing any better? */
10046 if (current_buffer->width_run_cache)
10047 {
10048 struct Lisp_Char_Table *disptab = buffer_display_table ();
10049
10050 if (! disptab_matches_widthtab (disptab,
10051 XVECTOR (current_buffer->width_table)))
10052 {
10053 invalidate_region_cache (current_buffer,
10054 current_buffer->width_run_cache,
10055 BEG, Z);
10056 recompute_width_table (current_buffer, disptab);
10057 }
10058 }
10059
10060 /* If window-start is screwed up, choose a new one. */
10061 if (XMARKER (w->start)->buffer != current_buffer)
10062 goto recenter;
10063
10064 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10065
10066 /* If someone specified a new starting point but did not insist,
10067 check whether it can be used. */
10068 if (!NILP (w->optional_new_start)
10069 && CHARPOS (startp) >= BEGV
10070 && CHARPOS (startp) <= ZV)
10071 {
10072 w->optional_new_start = Qnil;
10073 start_display (&it, w, startp);
10074 move_it_to (&it, PT, 0, it.last_visible_y, -1,
10075 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
10076 if (IT_CHARPOS (it) == PT)
10077 w->force_start = Qt;
10078 }
10079
10080 /* Handle case where place to start displaying has been specified,
10081 unless the specified location is outside the accessible range. */
10082 if (!NILP (w->force_start)
10083 || w->frozen_window_start_p)
10084 {
10085 w->force_start = Qnil;
10086 w->vscroll = 0;
10087 w->window_end_valid = Qnil;
10088
10089 /* Forget any recorded base line for line number display. */
10090 if (!current_matrix_up_to_date_p
10091 || current_buffer->clip_changed)
10092 w->base_line_number = Qnil;
10093
10094 /* Redisplay the mode line. Select the buffer properly for that.
10095 Also, run the hook window-scroll-functions
10096 because we have scrolled. */
10097 /* Note, we do this after clearing force_start because
10098 if there's an error, it is better to forget about force_start
10099 than to get into an infinite loop calling the hook functions
10100 and having them get more errors. */
10101 if (!update_mode_line
10102 || ! NILP (Vwindow_scroll_functions))
10103 {
10104 update_mode_line = 1;
10105 w->update_mode_line = Qt;
10106 startp = run_window_scroll_functions (window, startp);
10107 }
10108
10109 w->last_modified = make_number (0);
10110 w->last_overlay_modified = make_number (0);
10111 if (CHARPOS (startp) < BEGV)
10112 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
10113 else if (CHARPOS (startp) > ZV)
10114 SET_TEXT_POS (startp, ZV, ZV_BYTE);
10115
10116 /* Redisplay, then check if cursor has been set during the
10117 redisplay. Give up if new fonts were loaded. */
10118 if (!try_window (window, startp))
10119 {
10120 w->force_start = Qt;
10121 clear_glyph_matrix (w->desired_matrix);
10122 goto finish_scroll_bars;
10123 }
10124
10125 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
10126 {
10127 /* If point does not appear, try to move point so it does
10128 appear. The desired matrix has been built above, so we
10129 can use it here. */
10130 int window_height;
10131 struct glyph_row *row;
10132
10133 window_height = window_box_height (w) / 2;
10134 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
10135 while (MATRIX_ROW_BOTTOM_Y (row) < window_height)
10136 ++row;
10137
10138 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
10139 MATRIX_ROW_START_BYTEPOS (row));
10140
10141 if (w != XWINDOW (selected_window))
10142 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
10143 else if (current_buffer == old)
10144 SET_TEXT_POS (lpoint, PT, PT_BYTE);
10145
10146 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
10147
10148 /* If we are highlighting the region, then we just changed
10149 the region, so redisplay to show it. */
10150 if (!NILP (Vtransient_mark_mode)
10151 && !NILP (current_buffer->mark_active))
10152 {
10153 clear_glyph_matrix (w->desired_matrix);
10154 if (!try_window (window, startp))
10155 goto need_larger_matrices;
10156 }
10157 }
10158
10159 if (!make_cursor_line_fully_visible (w))
10160 goto need_larger_matrices;
10161 #if GLYPH_DEBUG
10162 debug_method_add (w, "forced window start");
10163 #endif
10164 goto done;
10165 }
10166
10167 /* Handle case where text has not changed, only point, and it has
10168 not moved off the frame. */
10169 if (current_matrix_up_to_date_p
10170 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
10171 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
10172 {
10173 switch (rc)
10174 {
10175 case CURSOR_MOVEMENT_SUCCESS:
10176 goto done;
10177
10178 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
10179 goto need_larger_matrices;
10180
10181 case CURSOR_MOVEMENT_MUST_SCROLL:
10182 goto try_to_scroll;
10183
10184 default:
10185 abort ();
10186 }
10187 }
10188 /* If current starting point was originally the beginning of a line
10189 but no longer is, find a new starting point. */
10190 else if (!NILP (w->start_at_line_beg)
10191 && !(CHARPOS (startp) <= BEGV
10192 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
10193 {
10194 #if GLYPH_DEBUG
10195 debug_method_add (w, "recenter 1");
10196 #endif
10197 goto recenter;
10198 }
10199
10200 /* Try scrolling with try_window_id. Value is > 0 if update has
10201 been done, it is -1 if we know that the same window start will
10202 not work. It is 0 if unsuccessful for some other reason. */
10203 else if ((tem = try_window_id (w)) != 0)
10204 {
10205 #if GLYPH_DEBUG
10206 debug_method_add (w, "try_window_id %d", tem);
10207 #endif
10208
10209 if (fonts_changed_p)
10210 goto need_larger_matrices;
10211 if (tem > 0)
10212 goto done;
10213
10214 /* Otherwise try_window_id has returned -1 which means that we
10215 don't want the alternative below this comment to execute. */
10216 }
10217 else if (CHARPOS (startp) >= BEGV
10218 && CHARPOS (startp) <= ZV
10219 && PT >= CHARPOS (startp)
10220 && (CHARPOS (startp) < ZV
10221 /* Avoid starting at end of buffer. */
10222 || CHARPOS (startp) == BEGV
10223 || (XFASTINT (w->last_modified) >= MODIFF
10224 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
10225 {
10226 #if GLYPH_DEBUG
10227 debug_method_add (w, "same window start");
10228 #endif
10229
10230 /* Try to redisplay starting at same place as before.
10231 If point has not moved off frame, accept the results. */
10232 if (!current_matrix_up_to_date_p
10233 /* Don't use try_window_reusing_current_matrix in this case
10234 because a window scroll function can have changed the
10235 buffer. */
10236 || !NILP (Vwindow_scroll_functions)
10237 || MINI_WINDOW_P (w)
10238 || !try_window_reusing_current_matrix (w))
10239 {
10240 IF_DEBUG (debug_method_add (w, "1"));
10241 try_window (window, startp);
10242 }
10243
10244 if (fonts_changed_p)
10245 goto need_larger_matrices;
10246
10247 if (w->cursor.vpos >= 0)
10248 {
10249 if (!just_this_one_p
10250 || current_buffer->clip_changed
10251 || BEG_UNCHANGED < CHARPOS (startp))
10252 /* Forget any recorded base line for line number display. */
10253 w->base_line_number = Qnil;
10254
10255 if (!make_cursor_line_fully_visible (w))
10256 goto need_larger_matrices;
10257 goto done;
10258 }
10259 else
10260 clear_glyph_matrix (w->desired_matrix);
10261 }
10262
10263 try_to_scroll:
10264
10265 w->last_modified = make_number (0);
10266 w->last_overlay_modified = make_number (0);
10267
10268 /* Redisplay the mode line. Select the buffer properly for that. */
10269 if (!update_mode_line)
10270 {
10271 update_mode_line = 1;
10272 w->update_mode_line = Qt;
10273 }
10274
10275 /* Try to scroll by specified few lines. */
10276 if ((scroll_conservatively
10277 || scroll_step
10278 || temp_scroll_step
10279 || NUMBERP (current_buffer->scroll_up_aggressively)
10280 || NUMBERP (current_buffer->scroll_down_aggressively))
10281 && !current_buffer->clip_changed
10282 && CHARPOS (startp) >= BEGV
10283 && CHARPOS (startp) <= ZV)
10284 {
10285 /* The function returns -1 if new fonts were loaded, 1 if
10286 successful, 0 if not successful. */
10287 int rc = try_scrolling (window, just_this_one_p,
10288 scroll_conservatively,
10289 scroll_step,
10290 temp_scroll_step);
10291 switch (rc)
10292 {
10293 case SCROLLING_SUCCESS:
10294 goto done;
10295
10296 case SCROLLING_NEED_LARGER_MATRICES:
10297 goto need_larger_matrices;
10298
10299 case SCROLLING_FAILED:
10300 break;
10301
10302 default:
10303 abort ();
10304 }
10305 }
10306
10307 /* Finally, just choose place to start which centers point */
10308
10309 recenter:
10310
10311 #if GLYPH_DEBUG
10312 debug_method_add (w, "recenter");
10313 #endif
10314
10315 /* w->vscroll = 0; */
10316
10317 /* Forget any previously recorded base line for line number display. */
10318 if (!current_matrix_up_to_date_p
10319 || current_buffer->clip_changed)
10320 w->base_line_number = Qnil;
10321
10322 /* Move backward half the height of the window. */
10323 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10324 it.current_y = it.last_visible_y;
10325 move_it_vertically_backward (&it, window_box_height (w) / 2);
10326 xassert (IT_CHARPOS (it) >= BEGV);
10327
10328 /* The function move_it_vertically_backward may move over more
10329 than the specified y-distance. If it->w is small, e.g. a
10330 mini-buffer window, we may end up in front of the window's
10331 display area. Start displaying at the start of the line
10332 containing PT in this case. */
10333 if (it.current_y <= 0)
10334 {
10335 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10336 move_it_vertically (&it, 0);
10337 xassert (IT_CHARPOS (it) <= PT);
10338 it.current_y = 0;
10339 }
10340
10341 it.current_x = it.hpos = 0;
10342
10343 /* Set startp here explicitly in case that helps avoid an infinite loop
10344 in case the window-scroll-functions functions get errors. */
10345 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
10346
10347 /* Run scroll hooks. */
10348 startp = run_window_scroll_functions (window, it.current.pos);
10349
10350 /* Redisplay the window. */
10351 if (!current_matrix_up_to_date_p
10352 || windows_or_buffers_changed
10353 /* Don't use try_window_reusing_current_matrix in this case
10354 because it can have changed the buffer. */
10355 || !NILP (Vwindow_scroll_functions)
10356 || !just_this_one_p
10357 || MINI_WINDOW_P (w)
10358 || !try_window_reusing_current_matrix (w))
10359 try_window (window, startp);
10360
10361 /* If new fonts have been loaded (due to fontsets), give up. We
10362 have to start a new redisplay since we need to re-adjust glyph
10363 matrices. */
10364 if (fonts_changed_p)
10365 goto need_larger_matrices;
10366
10367 /* If cursor did not appear assume that the middle of the window is
10368 in the first line of the window. Do it again with the next line.
10369 (Imagine a window of height 100, displaying two lines of height
10370 60. Moving back 50 from it->last_visible_y will end in the first
10371 line.) */
10372 if (w->cursor.vpos < 0)
10373 {
10374 if (!NILP (w->window_end_valid)
10375 && PT >= Z - XFASTINT (w->window_end_pos))
10376 {
10377 clear_glyph_matrix (w->desired_matrix);
10378 move_it_by_lines (&it, 1, 0);
10379 try_window (window, it.current.pos);
10380 }
10381 else if (PT < IT_CHARPOS (it))
10382 {
10383 clear_glyph_matrix (w->desired_matrix);
10384 move_it_by_lines (&it, -1, 0);
10385 try_window (window, it.current.pos);
10386 }
10387 else
10388 {
10389 /* Not much we can do about it. */
10390 }
10391 }
10392
10393 /* Consider the following case: Window starts at BEGV, there is
10394 invisible, intangible text at BEGV, so that display starts at
10395 some point START > BEGV. It can happen that we are called with
10396 PT somewhere between BEGV and START. Try to handle that case. */
10397 if (w->cursor.vpos < 0)
10398 {
10399 struct glyph_row *row = w->current_matrix->rows;
10400 if (row->mode_line_p)
10401 ++row;
10402 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10403 }
10404
10405 if (!make_cursor_line_fully_visible (w))
10406 goto need_larger_matrices;
10407
10408 done:
10409
10410 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10411 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
10412 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
10413 ? Qt : Qnil);
10414
10415 /* Display the mode line, if we must. */
10416 if ((update_mode_line
10417 /* If window not full width, must redo its mode line
10418 if (a) the window to its side is being redone and
10419 (b) we do a frame-based redisplay. This is a consequence
10420 of how inverted lines are drawn in frame-based redisplay. */
10421 || (!just_this_one_p
10422 && !FRAME_WINDOW_P (f)
10423 && !WINDOW_FULL_WIDTH_P (w))
10424 /* Line number to display. */
10425 || INTEGERP (w->base_line_pos)
10426 /* Column number is displayed and different from the one displayed. */
10427 || (!NILP (w->column_number_displayed)
10428 && XFASTINT (w->column_number_displayed) != current_column ()))
10429 /* This means that the window has a mode line. */
10430 && (WINDOW_WANTS_MODELINE_P (w)
10431 || WINDOW_WANTS_HEADER_LINE_P (w)))
10432 {
10433 display_mode_lines (w);
10434
10435 /* If mode line height has changed, arrange for a thorough
10436 immediate redisplay using the correct mode line height. */
10437 if (WINDOW_WANTS_MODELINE_P (w)
10438 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
10439 {
10440 fonts_changed_p = 1;
10441 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
10442 = DESIRED_MODE_LINE_HEIGHT (w);
10443 }
10444
10445 /* If top line height has changed, arrange for a thorough
10446 immediate redisplay using the correct mode line height. */
10447 if (WINDOW_WANTS_HEADER_LINE_P (w)
10448 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
10449 {
10450 fonts_changed_p = 1;
10451 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
10452 = DESIRED_HEADER_LINE_HEIGHT (w);
10453 }
10454
10455 if (fonts_changed_p)
10456 goto need_larger_matrices;
10457 }
10458
10459 if (!line_number_displayed
10460 && !BUFFERP (w->base_line_pos))
10461 {
10462 w->base_line_pos = Qnil;
10463 w->base_line_number = Qnil;
10464 }
10465
10466 finish_menu_bars:
10467
10468 /* When we reach a frame's selected window, redo the frame's menu bar. */
10469 if (update_mode_line
10470 && EQ (FRAME_SELECTED_WINDOW (f), window))
10471 {
10472 int redisplay_menu_p = 0;
10473
10474 if (FRAME_WINDOW_P (f))
10475 {
10476 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
10477 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
10478 #else
10479 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10480 #endif
10481 }
10482 else
10483 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10484
10485 if (redisplay_menu_p)
10486 display_menu_bar (w);
10487
10488 #ifdef HAVE_WINDOW_SYSTEM
10489 if (WINDOWP (f->tool_bar_window)
10490 && (FRAME_TOOL_BAR_LINES (f) > 0
10491 || auto_resize_tool_bars_p))
10492 redisplay_tool_bar (f);
10493 #endif
10494 }
10495
10496 need_larger_matrices:
10497 ;
10498 finish_scroll_bars:
10499
10500 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
10501 {
10502 int start, end, whole;
10503
10504 /* Calculate the start and end positions for the current window.
10505 At some point, it would be nice to choose between scrollbars
10506 which reflect the whole buffer size, with special markers
10507 indicating narrowing, and scrollbars which reflect only the
10508 visible region.
10509
10510 Note that mini-buffers sometimes aren't displaying any text. */
10511 if (!MINI_WINDOW_P (w)
10512 || (w == XWINDOW (minibuf_window)
10513 && NILP (echo_area_buffer[0])))
10514 {
10515 whole = ZV - BEGV;
10516 start = marker_position (w->start) - BEGV;
10517 /* I don't think this is guaranteed to be right. For the
10518 moment, we'll pretend it is. */
10519 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
10520
10521 if (end < start)
10522 end = start;
10523 if (whole < (end - start))
10524 whole = end - start;
10525 }
10526 else
10527 start = end = whole = 0;
10528
10529 /* Indicate what this scroll bar ought to be displaying now. */
10530 set_vertical_scroll_bar_hook (w, end - start, whole, start);
10531
10532 /* Note that we actually used the scroll bar attached to this
10533 window, so it shouldn't be deleted at the end of redisplay. */
10534 redeem_scroll_bar_hook (w);
10535 }
10536
10537 /* Restore current_buffer and value of point in it. */
10538 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
10539 set_buffer_internal_1 (old);
10540 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
10541
10542 unbind_to (count, Qnil);
10543 }
10544
10545
10546 /* Build the complete desired matrix of WINDOW with a window start
10547 buffer position POS. Value is non-zero if successful. It is zero
10548 if fonts were loaded during redisplay which makes re-adjusting
10549 glyph matrices necessary. */
10550
10551 int
10552 try_window (window, pos)
10553 Lisp_Object window;
10554 struct text_pos pos;
10555 {
10556 struct window *w = XWINDOW (window);
10557 struct it it;
10558 struct glyph_row *last_text_row = NULL;
10559
10560 /* Make POS the new window start. */
10561 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
10562
10563 /* Mark cursor position as unknown. No overlay arrow seen. */
10564 w->cursor.vpos = -1;
10565 overlay_arrow_seen = 0;
10566
10567 /* Initialize iterator and info to start at POS. */
10568 start_display (&it, w, pos);
10569
10570 /* Display all lines of W. */
10571 while (it.current_y < it.last_visible_y)
10572 {
10573 if (display_line (&it))
10574 last_text_row = it.glyph_row - 1;
10575 if (fonts_changed_p)
10576 return 0;
10577 }
10578
10579 /* If bottom moved off end of frame, change mode line percentage. */
10580 if (XFASTINT (w->window_end_pos) <= 0
10581 && Z != IT_CHARPOS (it))
10582 w->update_mode_line = Qt;
10583
10584 /* Set window_end_pos to the offset of the last character displayed
10585 on the window from the end of current_buffer. Set
10586 window_end_vpos to its row number. */
10587 if (last_text_row)
10588 {
10589 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
10590 w->window_end_bytepos
10591 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10592 w->window_end_pos
10593 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10594 w->window_end_vpos
10595 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10596 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
10597 ->displays_text_p);
10598 }
10599 else
10600 {
10601 w->window_end_bytepos = 0;
10602 w->window_end_pos = w->window_end_vpos = make_number (0);
10603 }
10604
10605 /* But that is not valid info until redisplay finishes. */
10606 w->window_end_valid = Qnil;
10607 return 1;
10608 }
10609
10610
10611 \f
10612 /************************************************************************
10613 Window redisplay reusing current matrix when buffer has not changed
10614 ************************************************************************/
10615
10616 /* Try redisplay of window W showing an unchanged buffer with a
10617 different window start than the last time it was displayed by
10618 reusing its current matrix. Value is non-zero if successful.
10619 W->start is the new window start. */
10620
10621 static int
10622 try_window_reusing_current_matrix (w)
10623 struct window *w;
10624 {
10625 struct frame *f = XFRAME (w->frame);
10626 struct glyph_row *row, *bottom_row;
10627 struct it it;
10628 struct run run;
10629 struct text_pos start, new_start;
10630 int nrows_scrolled, i;
10631 struct glyph_row *last_text_row;
10632 struct glyph_row *last_reused_text_row;
10633 struct glyph_row *start_row;
10634 int start_vpos, min_y, max_y;
10635
10636 #if GLYPH_DEBUG
10637 if (inhibit_try_window_reusing)
10638 return 0;
10639 #endif
10640
10641 if (/* This function doesn't handle terminal frames. */
10642 !FRAME_WINDOW_P (f)
10643 /* Don't try to reuse the display if windows have been split
10644 or such. */
10645 || windows_or_buffers_changed)
10646 return 0;
10647
10648 /* Can't do this if region may have changed. */
10649 if ((!NILP (Vtransient_mark_mode)
10650 && !NILP (current_buffer->mark_active))
10651 || !NILP (w->region_showing)
10652 || !NILP (Vshow_trailing_whitespace))
10653 return 0;
10654
10655 /* If top-line visibility has changed, give up. */
10656 if (WINDOW_WANTS_HEADER_LINE_P (w)
10657 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
10658 return 0;
10659
10660 /* Give up if old or new display is scrolled vertically. We could
10661 make this function handle this, but right now it doesn't. */
10662 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10663 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
10664 return 0;
10665
10666 /* The variable new_start now holds the new window start. The old
10667 start `start' can be determined from the current matrix. */
10668 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
10669 start = start_row->start.pos;
10670 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
10671
10672 /* Clear the desired matrix for the display below. */
10673 clear_glyph_matrix (w->desired_matrix);
10674
10675 if (CHARPOS (new_start) <= CHARPOS (start))
10676 {
10677 int first_row_y;
10678
10679 /* Don't use this method if the display starts with an ellipsis
10680 displayed for invisible text. It's not easy to handle that case
10681 below, and it's certainly not worth the effort since this is
10682 not a frequent case. */
10683 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
10684 return 0;
10685
10686 IF_DEBUG (debug_method_add (w, "twu1"));
10687
10688 /* Display up to a row that can be reused. The variable
10689 last_text_row is set to the last row displayed that displays
10690 text. Note that it.vpos == 0 if or if not there is a
10691 header-line; it's not the same as the MATRIX_ROW_VPOS! */
10692 start_display (&it, w, new_start);
10693 first_row_y = it.current_y;
10694 w->cursor.vpos = -1;
10695 last_text_row = last_reused_text_row = NULL;
10696
10697 while (it.current_y < it.last_visible_y
10698 && IT_CHARPOS (it) < CHARPOS (start)
10699 && !fonts_changed_p)
10700 if (display_line (&it))
10701 last_text_row = it.glyph_row - 1;
10702
10703 /* A value of current_y < last_visible_y means that we stopped
10704 at the previous window start, which in turn means that we
10705 have at least one reusable row. */
10706 if (it.current_y < it.last_visible_y)
10707 {
10708 /* IT.vpos always starts from 0; it counts text lines. */
10709 nrows_scrolled = it.vpos;
10710
10711 /* Find PT if not already found in the lines displayed. */
10712 if (w->cursor.vpos < 0)
10713 {
10714 int dy = it.current_y - first_row_y;
10715
10716 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10717 row = row_containing_pos (w, PT, row, NULL, dy);
10718 if (row)
10719 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
10720 dy, nrows_scrolled);
10721 else
10722 {
10723 clear_glyph_matrix (w->desired_matrix);
10724 return 0;
10725 }
10726 }
10727
10728 /* Scroll the display. Do it before the current matrix is
10729 changed. The problem here is that update has not yet
10730 run, i.e. part of the current matrix is not up to date.
10731 scroll_run_hook will clear the cursor, and use the
10732 current matrix to get the height of the row the cursor is
10733 in. */
10734 run.current_y = first_row_y;
10735 run.desired_y = it.current_y;
10736 run.height = it.last_visible_y - it.current_y;
10737
10738 if (run.height > 0 && run.current_y != run.desired_y)
10739 {
10740 update_begin (f);
10741 rif->update_window_begin_hook (w);
10742 rif->clear_mouse_face (w);
10743 rif->scroll_run_hook (w, &run);
10744 rif->update_window_end_hook (w, 0, 0);
10745 update_end (f);
10746 }
10747
10748 /* Shift current matrix down by nrows_scrolled lines. */
10749 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
10750 rotate_matrix (w->current_matrix,
10751 start_vpos,
10752 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10753 nrows_scrolled);
10754
10755 /* Disable lines that must be updated. */
10756 for (i = 0; i < it.vpos; ++i)
10757 (start_row + i)->enabled_p = 0;
10758
10759 /* Re-compute Y positions. */
10760 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10761 max_y = it.last_visible_y;
10762 for (row = start_row + nrows_scrolled;
10763 row < bottom_row;
10764 ++row)
10765 {
10766 row->y = it.current_y;
10767 row->visible_height = row->height;
10768
10769 if (row->y < min_y)
10770 row->visible_height -= min_y - row->y;
10771 if (row->y + row->height > max_y)
10772 row->visible_height -= row->y + row->height - max_y;
10773
10774 it.current_y += row->height;
10775
10776 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10777 last_reused_text_row = row;
10778 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
10779 break;
10780 }
10781
10782 /* Disable lines in the current matrix which are now
10783 below the window. */
10784 for (++row; row < bottom_row; ++row)
10785 row->enabled_p = 0;
10786 }
10787
10788 /* Update window_end_pos etc.; last_reused_text_row is the last
10789 reused row from the current matrix containing text, if any.
10790 The value of last_text_row is the last displayed line
10791 containing text. */
10792 if (last_reused_text_row)
10793 {
10794 w->window_end_bytepos
10795 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
10796 w->window_end_pos
10797 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
10798 w->window_end_vpos
10799 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
10800 w->current_matrix));
10801 }
10802 else if (last_text_row)
10803 {
10804 w->window_end_bytepos
10805 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10806 w->window_end_pos
10807 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10808 w->window_end_vpos
10809 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10810 }
10811 else
10812 {
10813 /* This window must be completely empty. */
10814 w->window_end_bytepos = 0;
10815 w->window_end_pos = w->window_end_vpos = make_number (0);
10816 }
10817 w->window_end_valid = Qnil;
10818
10819 /* Update hint: don't try scrolling again in update_window. */
10820 w->desired_matrix->no_scrolling_p = 1;
10821
10822 #if GLYPH_DEBUG
10823 debug_method_add (w, "try_window_reusing_current_matrix 1");
10824 #endif
10825 return 1;
10826 }
10827 else if (CHARPOS (new_start) > CHARPOS (start))
10828 {
10829 struct glyph_row *pt_row, *row;
10830 struct glyph_row *first_reusable_row;
10831 struct glyph_row *first_row_to_display;
10832 int dy;
10833 int yb = window_text_bottom_y (w);
10834
10835 /* Find the row starting at new_start, if there is one. Don't
10836 reuse a partially visible line at the end. */
10837 first_reusable_row = start_row;
10838 while (first_reusable_row->enabled_p
10839 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
10840 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
10841 < CHARPOS (new_start)))
10842 ++first_reusable_row;
10843
10844 /* Give up if there is no row to reuse. */
10845 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
10846 || !first_reusable_row->enabled_p
10847 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
10848 != CHARPOS (new_start)))
10849 return 0;
10850
10851 /* We can reuse fully visible rows beginning with
10852 first_reusable_row to the end of the window. Set
10853 first_row_to_display to the first row that cannot be reused.
10854 Set pt_row to the row containing point, if there is any. */
10855 pt_row = NULL;
10856 for (first_row_to_display = first_reusable_row;
10857 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
10858 ++first_row_to_display)
10859 {
10860 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
10861 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
10862 pt_row = first_row_to_display;
10863 }
10864
10865 /* Start displaying at the start of first_row_to_display. */
10866 xassert (first_row_to_display->y < yb);
10867 init_to_row_start (&it, w, first_row_to_display);
10868
10869 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
10870 - start_vpos);
10871 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
10872 - nrows_scrolled);
10873 it.current_y = (first_row_to_display->y - first_reusable_row->y
10874 + WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
10875
10876 /* Display lines beginning with first_row_to_display in the
10877 desired matrix. Set last_text_row to the last row displayed
10878 that displays text. */
10879 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
10880 if (pt_row == NULL)
10881 w->cursor.vpos = -1;
10882 last_text_row = NULL;
10883 while (it.current_y < it.last_visible_y && !fonts_changed_p)
10884 if (display_line (&it))
10885 last_text_row = it.glyph_row - 1;
10886
10887 /* Give up If point isn't in a row displayed or reused. */
10888 if (w->cursor.vpos < 0)
10889 {
10890 clear_glyph_matrix (w->desired_matrix);
10891 return 0;
10892 }
10893
10894 /* If point is in a reused row, adjust y and vpos of the cursor
10895 position. */
10896 if (pt_row)
10897 {
10898 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
10899 w->current_matrix);
10900 w->cursor.y -= first_reusable_row->y;
10901 }
10902
10903 /* Scroll the display. */
10904 run.current_y = first_reusable_row->y;
10905 run.desired_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10906 run.height = it.last_visible_y - run.current_y;
10907 dy = run.current_y - run.desired_y;
10908
10909 if (run.height)
10910 {
10911 struct frame *f = XFRAME (WINDOW_FRAME (w));
10912 update_begin (f);
10913 rif->update_window_begin_hook (w);
10914 rif->clear_mouse_face (w);
10915 rif->scroll_run_hook (w, &run);
10916 rif->update_window_end_hook (w, 0, 0);
10917 update_end (f);
10918 }
10919
10920 /* Adjust Y positions of reused rows. */
10921 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
10922 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10923 max_y = it.last_visible_y;
10924 for (row = first_reusable_row; row < first_row_to_display; ++row)
10925 {
10926 row->y -= dy;
10927 row->visible_height = row->height;
10928 if (row->y < min_y)
10929 row->visible_height -= min_y - row->y;
10930 if (row->y + row->height > max_y)
10931 row->visible_height -= row->y + row->height - max_y;
10932 }
10933
10934 /* Scroll the current matrix. */
10935 xassert (nrows_scrolled > 0);
10936 rotate_matrix (w->current_matrix,
10937 start_vpos,
10938 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10939 -nrows_scrolled);
10940
10941 /* Disable rows not reused. */
10942 for (row -= nrows_scrolled; row < bottom_row; ++row)
10943 row->enabled_p = 0;
10944
10945 /* Adjust window end. A null value of last_text_row means that
10946 the window end is in reused rows which in turn means that
10947 only its vpos can have changed. */
10948 if (last_text_row)
10949 {
10950 w->window_end_bytepos
10951 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10952 w->window_end_pos
10953 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10954 w->window_end_vpos
10955 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10956 }
10957 else
10958 {
10959 w->window_end_vpos
10960 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
10961 }
10962
10963 w->window_end_valid = Qnil;
10964 w->desired_matrix->no_scrolling_p = 1;
10965
10966 #if GLYPH_DEBUG
10967 debug_method_add (w, "try_window_reusing_current_matrix 2");
10968 #endif
10969 return 1;
10970 }
10971
10972 return 0;
10973 }
10974
10975
10976 \f
10977 /************************************************************************
10978 Window redisplay reusing current matrix when buffer has changed
10979 ************************************************************************/
10980
10981 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
10982 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
10983 int *, int *));
10984 static struct glyph_row *
10985 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
10986 struct glyph_row *));
10987
10988
10989 /* Return the last row in MATRIX displaying text. If row START is
10990 non-null, start searching with that row. IT gives the dimensions
10991 of the display. Value is null if matrix is empty; otherwise it is
10992 a pointer to the row found. */
10993
10994 static struct glyph_row *
10995 find_last_row_displaying_text (matrix, it, start)
10996 struct glyph_matrix *matrix;
10997 struct it *it;
10998 struct glyph_row *start;
10999 {
11000 struct glyph_row *row, *row_found;
11001
11002 /* Set row_found to the last row in IT->w's current matrix
11003 displaying text. The loop looks funny but think of partially
11004 visible lines. */
11005 row_found = NULL;
11006 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
11007 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
11008 {
11009 xassert (row->enabled_p);
11010 row_found = row;
11011 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
11012 break;
11013 ++row;
11014 }
11015
11016 return row_found;
11017 }
11018
11019
11020 /* Return the last row in the current matrix of W that is not affected
11021 by changes at the start of current_buffer that occurred since W's
11022 current matrix was built. Value is null if no such row exists.
11023
11024 BEG_UNCHANGED us the number of characters unchanged at the start of
11025 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
11026 first changed character in current_buffer. Characters at positions <
11027 BEG + BEG_UNCHANGED are at the same buffer positions as they were
11028 when the current matrix was built. */
11029
11030 static struct glyph_row *
11031 find_last_unchanged_at_beg_row (w)
11032 struct window *w;
11033 {
11034 int first_changed_pos = BEG + BEG_UNCHANGED;
11035 struct glyph_row *row;
11036 struct glyph_row *row_found = NULL;
11037 int yb = window_text_bottom_y (w);
11038
11039 /* Find the last row displaying unchanged text. */
11040 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11041 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
11042 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
11043 {
11044 if (/* If row ends before first_changed_pos, it is unchanged,
11045 except in some case. */
11046 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
11047 /* When row ends in ZV and we write at ZV it is not
11048 unchanged. */
11049 && !row->ends_at_zv_p
11050 /* When first_changed_pos is the end of a continued line,
11051 row is not unchanged because it may be no longer
11052 continued. */
11053 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
11054 && row->continued_p))
11055 row_found = row;
11056
11057 /* Stop if last visible row. */
11058 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
11059 break;
11060
11061 ++row;
11062 }
11063
11064 return row_found;
11065 }
11066
11067
11068 /* Find the first glyph row in the current matrix of W that is not
11069 affected by changes at the end of current_buffer since the
11070 time W's current matrix was built.
11071
11072 Return in *DELTA the number of chars by which buffer positions in
11073 unchanged text at the end of current_buffer must be adjusted.
11074
11075 Return in *DELTA_BYTES the corresponding number of bytes.
11076
11077 Value is null if no such row exists, i.e. all rows are affected by
11078 changes. */
11079
11080 static struct glyph_row *
11081 find_first_unchanged_at_end_row (w, delta, delta_bytes)
11082 struct window *w;
11083 int *delta, *delta_bytes;
11084 {
11085 struct glyph_row *row;
11086 struct glyph_row *row_found = NULL;
11087
11088 *delta = *delta_bytes = 0;
11089
11090 /* Display must not have been paused, otherwise the current matrix
11091 is not up to date. */
11092 if (NILP (w->window_end_valid))
11093 abort ();
11094
11095 /* A value of window_end_pos >= END_UNCHANGED means that the window
11096 end is in the range of changed text. If so, there is no
11097 unchanged row at the end of W's current matrix. */
11098 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
11099 return NULL;
11100
11101 /* Set row to the last row in W's current matrix displaying text. */
11102 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
11103
11104 /* If matrix is entirely empty, no unchanged row exists. */
11105 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
11106 {
11107 /* The value of row is the last glyph row in the matrix having a
11108 meaningful buffer position in it. The end position of row
11109 corresponds to window_end_pos. This allows us to translate
11110 buffer positions in the current matrix to current buffer
11111 positions for characters not in changed text. */
11112 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
11113 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
11114 int last_unchanged_pos, last_unchanged_pos_old;
11115 struct glyph_row *first_text_row
11116 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11117
11118 *delta = Z - Z_old;
11119 *delta_bytes = Z_BYTE - Z_BYTE_old;
11120
11121 /* Set last_unchanged_pos to the buffer position of the last
11122 character in the buffer that has not been changed. Z is the
11123 index + 1 of the last character in current_buffer, i.e. by
11124 subtracting END_UNCHANGED we get the index of the last
11125 unchanged character, and we have to add BEG to get its buffer
11126 position. */
11127 last_unchanged_pos = Z - END_UNCHANGED + BEG;
11128 last_unchanged_pos_old = last_unchanged_pos - *delta;
11129
11130 /* Search backward from ROW for a row displaying a line that
11131 starts at a minimum position >= last_unchanged_pos_old. */
11132 for (; row > first_text_row; --row)
11133 {
11134 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
11135 abort ();
11136
11137 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
11138 row_found = row;
11139 }
11140 }
11141
11142 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
11143 abort ();
11144
11145 return row_found;
11146 }
11147
11148
11149 /* Make sure that glyph rows in the current matrix of window W
11150 reference the same glyph memory as corresponding rows in the
11151 frame's frame matrix. This function is called after scrolling W's
11152 current matrix on a terminal frame in try_window_id and
11153 try_window_reusing_current_matrix. */
11154
11155 static void
11156 sync_frame_with_window_matrix_rows (w)
11157 struct window *w;
11158 {
11159 struct frame *f = XFRAME (w->frame);
11160 struct glyph_row *window_row, *window_row_end, *frame_row;
11161
11162 /* Preconditions: W must be a leaf window and full-width. Its frame
11163 must have a frame matrix. */
11164 xassert (NILP (w->hchild) && NILP (w->vchild));
11165 xassert (WINDOW_FULL_WIDTH_P (w));
11166 xassert (!FRAME_WINDOW_P (f));
11167
11168 /* If W is a full-width window, glyph pointers in W's current matrix
11169 have, by definition, to be the same as glyph pointers in the
11170 corresponding frame matrix. */
11171 window_row = w->current_matrix->rows;
11172 window_row_end = window_row + w->current_matrix->nrows;
11173 frame_row = f->current_matrix->rows + XFASTINT (w->top);
11174 while (window_row < window_row_end)
11175 {
11176 int area;
11177
11178 for (area = LEFT_MARGIN_AREA; area <= LAST_AREA; ++area)
11179 frame_row->glyphs[area] = window_row->glyphs[area];
11180
11181 /* Disable frame rows whose corresponding window rows have
11182 been disabled in try_window_id. */
11183 if (!window_row->enabled_p)
11184 frame_row->enabled_p = 0;
11185
11186 ++window_row, ++frame_row;
11187 }
11188 }
11189
11190
11191 /* Find the glyph row in window W containing CHARPOS. Consider all
11192 rows between START and END (not inclusive). END null means search
11193 all rows to the end of the display area of W. Value is the row
11194 containing CHARPOS or null. */
11195
11196 struct glyph_row *
11197 row_containing_pos (w, charpos, start, end, dy)
11198 struct window *w;
11199 int charpos;
11200 struct glyph_row *start, *end;
11201 int dy;
11202 {
11203 struct glyph_row *row = start;
11204 int last_y;
11205
11206 /* If we happen to start on a header-line, skip that. */
11207 if (row->mode_line_p)
11208 ++row;
11209
11210 if ((end && row >= end) || !row->enabled_p)
11211 return NULL;
11212
11213 last_y = window_text_bottom_y (w) - dy;
11214
11215 while ((end == NULL || row < end)
11216 && MATRIX_ROW_BOTTOM_Y (row) < last_y
11217 && (MATRIX_ROW_END_CHARPOS (row) < charpos
11218 || (MATRIX_ROW_END_CHARPOS (row) == charpos
11219 /* The end position of a row equals the start
11220 position of the next row. If CHARPOS is there, we
11221 would rather display it in the next line, except
11222 when this line ends in ZV. */
11223 && !row->ends_at_zv_p
11224 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))
11225 ++row;
11226
11227 /* Give up if CHARPOS not found. */
11228 if ((end && row >= end)
11229 || charpos < MATRIX_ROW_START_CHARPOS (row)
11230 || charpos > MATRIX_ROW_END_CHARPOS (row))
11231 row = NULL;
11232
11233 return row;
11234 }
11235
11236
11237 /* Try to redisplay window W by reusing its existing display. W's
11238 current matrix must be up to date when this function is called,
11239 i.e. window_end_valid must not be nil.
11240
11241 Value is
11242
11243 1 if display has been updated
11244 0 if otherwise unsuccessful
11245 -1 if redisplay with same window start is known not to succeed
11246
11247 The following steps are performed:
11248
11249 1. Find the last row in the current matrix of W that is not
11250 affected by changes at the start of current_buffer. If no such row
11251 is found, give up.
11252
11253 2. Find the first row in W's current matrix that is not affected by
11254 changes at the end of current_buffer. Maybe there is no such row.
11255
11256 3. Display lines beginning with the row + 1 found in step 1 to the
11257 row found in step 2 or, if step 2 didn't find a row, to the end of
11258 the window.
11259
11260 4. If cursor is not known to appear on the window, give up.
11261
11262 5. If display stopped at the row found in step 2, scroll the
11263 display and current matrix as needed.
11264
11265 6. Maybe display some lines at the end of W, if we must. This can
11266 happen under various circumstances, like a partially visible line
11267 becoming fully visible, or because newly displayed lines are displayed
11268 in smaller font sizes.
11269
11270 7. Update W's window end information. */
11271
11272 static int
11273 try_window_id (w)
11274 struct window *w;
11275 {
11276 struct frame *f = XFRAME (w->frame);
11277 struct glyph_matrix *current_matrix = w->current_matrix;
11278 struct glyph_matrix *desired_matrix = w->desired_matrix;
11279 struct glyph_row *last_unchanged_at_beg_row;
11280 struct glyph_row *first_unchanged_at_end_row;
11281 struct glyph_row *row;
11282 struct glyph_row *bottom_row;
11283 int bottom_vpos;
11284 struct it it;
11285 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
11286 struct text_pos start_pos;
11287 struct run run;
11288 int first_unchanged_at_end_vpos = 0;
11289 struct glyph_row *last_text_row, *last_text_row_at_end;
11290 struct text_pos start;
11291 int first_changed_charpos, last_changed_charpos;
11292
11293 #if GLYPH_DEBUG
11294 if (inhibit_try_window_id)
11295 return 0;
11296 #endif
11297
11298 /* This is handy for debugging. */
11299 #if 0
11300 #define GIVE_UP(X) \
11301 do { \
11302 fprintf (stderr, "try_window_id give up %d\n", (X)); \
11303 return 0; \
11304 } while (0)
11305 #else
11306 #define GIVE_UP(X) return 0
11307 #endif
11308
11309 SET_TEXT_POS_FROM_MARKER (start, w->start);
11310
11311 /* Don't use this for mini-windows because these can show
11312 messages and mini-buffers, and we don't handle that here. */
11313 if (MINI_WINDOW_P (w))
11314 GIVE_UP (1);
11315
11316 /* This flag is used to prevent redisplay optimizations. */
11317 if (windows_or_buffers_changed)
11318 GIVE_UP (2);
11319
11320 /* Verify that narrowing has not changed. This flag is also set to prevent
11321 redisplay optimizations. It would be nice to further
11322 reduce the number of cases where this prevents try_window_id. */
11323 if (current_buffer->clip_changed)
11324 GIVE_UP (3);
11325
11326 /* Window must either use window-based redisplay or be full width. */
11327 if (!FRAME_WINDOW_P (f)
11328 && (!line_ins_del_ok
11329 || !WINDOW_FULL_WIDTH_P (w)))
11330 GIVE_UP (4);
11331
11332 /* Give up if point is not known NOT to appear in W. */
11333 if (PT < CHARPOS (start))
11334 GIVE_UP (5);
11335
11336 /* Another way to prevent redisplay optimizations. */
11337 if (XFASTINT (w->last_modified) == 0)
11338 GIVE_UP (6);
11339
11340 /* Verify that window is not hscrolled. */
11341 if (XFASTINT (w->hscroll) != 0)
11342 GIVE_UP (7);
11343
11344 /* Verify that display wasn't paused. */
11345 if (NILP (w->window_end_valid))
11346 GIVE_UP (8);
11347
11348 /* Can't use this if highlighting a region because a cursor movement
11349 will do more than just set the cursor. */
11350 if (!NILP (Vtransient_mark_mode)
11351 && !NILP (current_buffer->mark_active))
11352 GIVE_UP (9);
11353
11354 /* Likewise if highlighting trailing whitespace. */
11355 if (!NILP (Vshow_trailing_whitespace))
11356 GIVE_UP (11);
11357
11358 /* Likewise if showing a region. */
11359 if (!NILP (w->region_showing))
11360 GIVE_UP (10);
11361
11362 /* Can use this if overlay arrow position and or string have changed. */
11363 if (!EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
11364 || !EQ (last_arrow_string, Voverlay_arrow_string))
11365 GIVE_UP (12);
11366
11367
11368 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
11369 only if buffer has really changed. The reason is that the gap is
11370 initially at Z for freshly visited files. The code below would
11371 set end_unchanged to 0 in that case. */
11372 if (MODIFF > SAVE_MODIFF
11373 /* This seems to happen sometimes after saving a buffer. */
11374 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
11375 {
11376 if (GPT - BEG < BEG_UNCHANGED)
11377 BEG_UNCHANGED = GPT - BEG;
11378 if (Z - GPT < END_UNCHANGED)
11379 END_UNCHANGED = Z - GPT;
11380 }
11381
11382 /* The position of the first and last character that has been changed. */
11383 first_changed_charpos = BEG + BEG_UNCHANGED;
11384 last_changed_charpos = Z - END_UNCHANGED;
11385
11386 /* If window starts after a line end, and the last change is in
11387 front of that newline, then changes don't affect the display.
11388 This case happens with stealth-fontification. Note that although
11389 the display is unchanged, glyph positions in the matrix have to
11390 be adjusted, of course. */
11391 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
11392 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
11393 && ((last_changed_charpos < CHARPOS (start)
11394 && CHARPOS (start) == BEGV)
11395 || (last_changed_charpos < CHARPOS (start) - 1
11396 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
11397 {
11398 int Z_old, delta, Z_BYTE_old, delta_bytes;
11399 struct glyph_row *r0;
11400
11401 /* Compute how many chars/bytes have been added to or removed
11402 from the buffer. */
11403 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
11404 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
11405 delta = Z - Z_old;
11406 delta_bytes = Z_BYTE - Z_BYTE_old;
11407
11408 /* Give up if PT is not in the window. Note that it already has
11409 been checked at the start of try_window_id that PT is not in
11410 front of the window start. */
11411 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
11412 GIVE_UP (13);
11413
11414 /* If window start is unchanged, we can reuse the whole matrix
11415 as is, after adjusting glyph positions. No need to compute
11416 the window end again, since its offset from Z hasn't changed. */
11417 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
11418 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
11419 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes)
11420 {
11421 /* Adjust positions in the glyph matrix. */
11422 if (delta || delta_bytes)
11423 {
11424 struct glyph_row *r1
11425 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11426 increment_matrix_positions (w->current_matrix,
11427 MATRIX_ROW_VPOS (r0, current_matrix),
11428 MATRIX_ROW_VPOS (r1, current_matrix),
11429 delta, delta_bytes);
11430 }
11431
11432 /* Set the cursor. */
11433 row = row_containing_pos (w, PT, r0, NULL, 0);
11434 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
11435 return 1;
11436 }
11437 }
11438
11439 /* Handle the case that changes are all below what is displayed in
11440 the window, and that PT is in the window. This shortcut cannot
11441 be taken if ZV is visible in the window, and text has been added
11442 there that is visible in the window. */
11443 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
11444 /* ZV is not visible in the window, or there are no
11445 changes at ZV, actually. */
11446 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
11447 || first_changed_charpos == last_changed_charpos))
11448 {
11449 struct glyph_row *r0;
11450
11451 /* Give up if PT is not in the window. Note that it already has
11452 been checked at the start of try_window_id that PT is not in
11453 front of the window start. */
11454 if (PT >= MATRIX_ROW_END_CHARPOS (row))
11455 GIVE_UP (14);
11456
11457 /* If window start is unchanged, we can reuse the whole matrix
11458 as is, without changing glyph positions since no text has
11459 been added/removed in front of the window end. */
11460 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
11461 if (TEXT_POS_EQUAL_P (start, r0->start.pos))
11462 {
11463 /* We have to compute the window end anew since text
11464 can have been added/removed after it. */
11465 w->window_end_pos
11466 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
11467 w->window_end_bytepos
11468 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11469
11470 /* Set the cursor. */
11471 row = row_containing_pos (w, PT, r0, NULL, 0);
11472 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
11473 return 2;
11474 }
11475 }
11476
11477 /* Give up if window start is in the changed area.
11478
11479 The condition used to read
11480
11481 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
11482
11483 but why that was tested escapes me at the moment. */
11484 if (CHARPOS (start) >= first_changed_charpos
11485 && CHARPOS (start) <= last_changed_charpos)
11486 GIVE_UP (15);
11487
11488 /* Check that window start agrees with the start of the first glyph
11489 row in its current matrix. Check this after we know the window
11490 start is not in changed text, otherwise positions would not be
11491 comparable. */
11492 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
11493 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
11494 GIVE_UP (16);
11495
11496 /* Give up if the window ends in strings. Overlay strings
11497 at the end are difficult to handle, so don't try. */
11498 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
11499 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
11500 GIVE_UP (20);
11501
11502 /* Compute the position at which we have to start displaying new
11503 lines. Some of the lines at the top of the window might be
11504 reusable because they are not displaying changed text. Find the
11505 last row in W's current matrix not affected by changes at the
11506 start of current_buffer. Value is null if changes start in the
11507 first line of window. */
11508 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
11509 if (last_unchanged_at_beg_row)
11510 {
11511 /* Avoid starting to display in the moddle of a character, a TAB
11512 for instance. This is easier than to set up the iterator
11513 exactly, and it's not a frequent case, so the additional
11514 effort wouldn't really pay off. */
11515 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
11516 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
11517 && last_unchanged_at_beg_row > w->current_matrix->rows)
11518 --last_unchanged_at_beg_row;
11519
11520 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
11521 GIVE_UP (17);
11522
11523 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
11524 GIVE_UP (18);
11525 start_pos = it.current.pos;
11526
11527 /* Start displaying new lines in the desired matrix at the same
11528 vpos we would use in the current matrix, i.e. below
11529 last_unchanged_at_beg_row. */
11530 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
11531 current_matrix);
11532 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11533 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
11534
11535 xassert (it.hpos == 0 && it.current_x == 0);
11536 }
11537 else
11538 {
11539 /* There are no reusable lines at the start of the window.
11540 Start displaying in the first line. */
11541 start_display (&it, w, start);
11542 start_pos = it.current.pos;
11543 }
11544
11545 /* Find the first row that is not affected by changes at the end of
11546 the buffer. Value will be null if there is no unchanged row, in
11547 which case we must redisplay to the end of the window. delta
11548 will be set to the value by which buffer positions beginning with
11549 first_unchanged_at_end_row have to be adjusted due to text
11550 changes. */
11551 first_unchanged_at_end_row
11552 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
11553 IF_DEBUG (debug_delta = delta);
11554 IF_DEBUG (debug_delta_bytes = delta_bytes);
11555
11556 /* Set stop_pos to the buffer position up to which we will have to
11557 display new lines. If first_unchanged_at_end_row != NULL, this
11558 is the buffer position of the start of the line displayed in that
11559 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
11560 that we don't stop at a buffer position. */
11561 stop_pos = 0;
11562 if (first_unchanged_at_end_row)
11563 {
11564 xassert (last_unchanged_at_beg_row == NULL
11565 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
11566
11567 /* If this is a continuation line, move forward to the next one
11568 that isn't. Changes in lines above affect this line.
11569 Caution: this may move first_unchanged_at_end_row to a row
11570 not displaying text. */
11571 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
11572 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11573 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11574 < it.last_visible_y))
11575 ++first_unchanged_at_end_row;
11576
11577 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11578 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11579 >= it.last_visible_y))
11580 first_unchanged_at_end_row = NULL;
11581 else
11582 {
11583 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
11584 + delta);
11585 first_unchanged_at_end_vpos
11586 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
11587 xassert (stop_pos >= Z - END_UNCHANGED);
11588 }
11589 }
11590 else if (last_unchanged_at_beg_row == NULL)
11591 GIVE_UP (19);
11592
11593
11594 #if GLYPH_DEBUG
11595
11596 /* Either there is no unchanged row at the end, or the one we have
11597 now displays text. This is a necessary condition for the window
11598 end pos calculation at the end of this function. */
11599 xassert (first_unchanged_at_end_row == NULL
11600 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
11601
11602 debug_last_unchanged_at_beg_vpos
11603 = (last_unchanged_at_beg_row
11604 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
11605 : -1);
11606 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
11607
11608 #endif /* GLYPH_DEBUG != 0 */
11609
11610
11611 /* Display new lines. Set last_text_row to the last new line
11612 displayed which has text on it, i.e. might end up as being the
11613 line where the window_end_vpos is. */
11614 w->cursor.vpos = -1;
11615 last_text_row = NULL;
11616 overlay_arrow_seen = 0;
11617 while (it.current_y < it.last_visible_y
11618 && !fonts_changed_p
11619 && (first_unchanged_at_end_row == NULL
11620 || IT_CHARPOS (it) < stop_pos))
11621 {
11622 if (display_line (&it))
11623 last_text_row = it.glyph_row - 1;
11624 }
11625
11626 if (fonts_changed_p)
11627 return -1;
11628
11629
11630 /* Compute differences in buffer positions, y-positions etc. for
11631 lines reused at the bottom of the window. Compute what we can
11632 scroll. */
11633 if (first_unchanged_at_end_row
11634 /* No lines reused because we displayed everything up to the
11635 bottom of the window. */
11636 && it.current_y < it.last_visible_y)
11637 {
11638 dvpos = (it.vpos
11639 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
11640 current_matrix));
11641 dy = it.current_y - first_unchanged_at_end_row->y;
11642 run.current_y = first_unchanged_at_end_row->y;
11643 run.desired_y = run.current_y + dy;
11644 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
11645 }
11646 else
11647 {
11648 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
11649 first_unchanged_at_end_row = NULL;
11650 }
11651 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
11652
11653
11654 /* Find the cursor if not already found. We have to decide whether
11655 PT will appear on this window (it sometimes doesn't, but this is
11656 not a very frequent case.) This decision has to be made before
11657 the current matrix is altered. A value of cursor.vpos < 0 means
11658 that PT is either in one of the lines beginning at
11659 first_unchanged_at_end_row or below the window. Don't care for
11660 lines that might be displayed later at the window end; as
11661 mentioned, this is not a frequent case. */
11662 if (w->cursor.vpos < 0)
11663 {
11664 /* Cursor in unchanged rows at the top? */
11665 if (PT < CHARPOS (start_pos)
11666 && last_unchanged_at_beg_row)
11667 {
11668 row = row_containing_pos (w, PT,
11669 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
11670 last_unchanged_at_beg_row + 1, 0);
11671 if (row)
11672 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11673 }
11674
11675 /* Start from first_unchanged_at_end_row looking for PT. */
11676 else if (first_unchanged_at_end_row)
11677 {
11678 row = row_containing_pos (w, PT - delta,
11679 first_unchanged_at_end_row, NULL, 0);
11680 if (row)
11681 set_cursor_from_row (w, row, w->current_matrix, delta,
11682 delta_bytes, dy, dvpos);
11683 }
11684
11685 /* Give up if cursor was not found. */
11686 if (w->cursor.vpos < 0)
11687 {
11688 clear_glyph_matrix (w->desired_matrix);
11689 return -1;
11690 }
11691 }
11692
11693 /* Don't let the cursor end in the scroll margins. */
11694 {
11695 int this_scroll_margin, cursor_height;
11696
11697 this_scroll_margin = max (0, scroll_margin);
11698 this_scroll_margin = min (this_scroll_margin,
11699 XFASTINT (w->height) / 4);
11700 this_scroll_margin *= CANON_Y_UNIT (it.f);
11701 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
11702
11703 if ((w->cursor.y < this_scroll_margin
11704 && CHARPOS (start) > BEGV)
11705 /* Don't take scroll margin into account at the bottom because
11706 old redisplay didn't do it either. */
11707 || w->cursor.y + cursor_height > it.last_visible_y)
11708 {
11709 w->cursor.vpos = -1;
11710 clear_glyph_matrix (w->desired_matrix);
11711 return -1;
11712 }
11713 }
11714
11715 /* Scroll the display. Do it before changing the current matrix so
11716 that xterm.c doesn't get confused about where the cursor glyph is
11717 found. */
11718 if (dy && run.height)
11719 {
11720 update_begin (f);
11721
11722 if (FRAME_WINDOW_P (f))
11723 {
11724 rif->update_window_begin_hook (w);
11725 rif->clear_mouse_face (w);
11726 rif->scroll_run_hook (w, &run);
11727 rif->update_window_end_hook (w, 0, 0);
11728 }
11729 else
11730 {
11731 /* Terminal frame. In this case, dvpos gives the number of
11732 lines to scroll by; dvpos < 0 means scroll up. */
11733 int first_unchanged_at_end_vpos
11734 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
11735 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
11736 int end = (XFASTINT (w->top)
11737 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
11738 + window_internal_height (w));
11739
11740 /* Perform the operation on the screen. */
11741 if (dvpos > 0)
11742 {
11743 /* Scroll last_unchanged_at_beg_row to the end of the
11744 window down dvpos lines. */
11745 set_terminal_window (end);
11746
11747 /* On dumb terminals delete dvpos lines at the end
11748 before inserting dvpos empty lines. */
11749 if (!scroll_region_ok)
11750 ins_del_lines (end - dvpos, -dvpos);
11751
11752 /* Insert dvpos empty lines in front of
11753 last_unchanged_at_beg_row. */
11754 ins_del_lines (from, dvpos);
11755 }
11756 else if (dvpos < 0)
11757 {
11758 /* Scroll up last_unchanged_at_beg_vpos to the end of
11759 the window to last_unchanged_at_beg_vpos - |dvpos|. */
11760 set_terminal_window (end);
11761
11762 /* Delete dvpos lines in front of
11763 last_unchanged_at_beg_vpos. ins_del_lines will set
11764 the cursor to the given vpos and emit |dvpos| delete
11765 line sequences. */
11766 ins_del_lines (from + dvpos, dvpos);
11767
11768 /* On a dumb terminal insert dvpos empty lines at the
11769 end. */
11770 if (!scroll_region_ok)
11771 ins_del_lines (end + dvpos, -dvpos);
11772 }
11773
11774 set_terminal_window (0);
11775 }
11776
11777 update_end (f);
11778 }
11779
11780 /* Shift reused rows of the current matrix to the right position.
11781 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
11782 text. */
11783 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11784 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
11785 if (dvpos < 0)
11786 {
11787 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
11788 bottom_vpos, dvpos);
11789 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
11790 bottom_vpos, 0);
11791 }
11792 else if (dvpos > 0)
11793 {
11794 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
11795 bottom_vpos, dvpos);
11796 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
11797 first_unchanged_at_end_vpos + dvpos, 0);
11798 }
11799
11800 /* For frame-based redisplay, make sure that current frame and window
11801 matrix are in sync with respect to glyph memory. */
11802 if (!FRAME_WINDOW_P (f))
11803 sync_frame_with_window_matrix_rows (w);
11804
11805 /* Adjust buffer positions in reused rows. */
11806 if (delta)
11807 increment_matrix_positions (current_matrix,
11808 first_unchanged_at_end_vpos + dvpos,
11809 bottom_vpos, delta, delta_bytes);
11810
11811 /* Adjust Y positions. */
11812 if (dy)
11813 shift_glyph_matrix (w, current_matrix,
11814 first_unchanged_at_end_vpos + dvpos,
11815 bottom_vpos, dy);
11816
11817 if (first_unchanged_at_end_row)
11818 first_unchanged_at_end_row += dvpos;
11819
11820 /* If scrolling up, there may be some lines to display at the end of
11821 the window. */
11822 last_text_row_at_end = NULL;
11823 if (dy < 0)
11824 {
11825 /* Scrolling up can leave for example a partially visible line
11826 at the end of the window to be redisplayed. */
11827 /* Set last_row to the glyph row in the current matrix where the
11828 window end line is found. It has been moved up or down in
11829 the matrix by dvpos. */
11830 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
11831 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
11832
11833 /* If last_row is the window end line, it should display text. */
11834 xassert (last_row->displays_text_p);
11835
11836 /* If window end line was partially visible before, begin
11837 displaying at that line. Otherwise begin displaying with the
11838 line following it. */
11839 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
11840 {
11841 init_to_row_start (&it, w, last_row);
11842 it.vpos = last_vpos;
11843 it.current_y = last_row->y;
11844 }
11845 else
11846 {
11847 init_to_row_end (&it, w, last_row);
11848 it.vpos = 1 + last_vpos;
11849 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
11850 ++last_row;
11851 }
11852
11853 /* We may start in a continuation line. If so, we have to
11854 get the right continuation_lines_width and current_x. */
11855 it.continuation_lines_width = last_row->continuation_lines_width;
11856 it.hpos = it.current_x = 0;
11857
11858 /* Display the rest of the lines at the window end. */
11859 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11860 while (it.current_y < it.last_visible_y
11861 && !fonts_changed_p)
11862 {
11863 /* Is it always sure that the display agrees with lines in
11864 the current matrix? I don't think so, so we mark rows
11865 displayed invalid in the current matrix by setting their
11866 enabled_p flag to zero. */
11867 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
11868 if (display_line (&it))
11869 last_text_row_at_end = it.glyph_row - 1;
11870 }
11871 }
11872
11873 /* Update window_end_pos and window_end_vpos. */
11874 if (first_unchanged_at_end_row
11875 && first_unchanged_at_end_row->y < it.last_visible_y
11876 && !last_text_row_at_end)
11877 {
11878 /* Window end line if one of the preserved rows from the current
11879 matrix. Set row to the last row displaying text in current
11880 matrix starting at first_unchanged_at_end_row, after
11881 scrolling. */
11882 xassert (first_unchanged_at_end_row->displays_text_p);
11883 row = find_last_row_displaying_text (w->current_matrix, &it,
11884 first_unchanged_at_end_row);
11885 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
11886
11887 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
11888 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11889 w->window_end_vpos
11890 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
11891 xassert (w->window_end_bytepos >= 0);
11892 IF_DEBUG (debug_method_add (w, "A"));
11893 }
11894 else if (last_text_row_at_end)
11895 {
11896 w->window_end_pos
11897 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
11898 w->window_end_bytepos
11899 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
11900 w->window_end_vpos
11901 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
11902 xassert (w->window_end_bytepos >= 0);
11903 IF_DEBUG (debug_method_add (w, "B"));
11904 }
11905 else if (last_text_row)
11906 {
11907 /* We have displayed either to the end of the window or at the
11908 end of the window, i.e. the last row with text is to be found
11909 in the desired matrix. */
11910 w->window_end_pos
11911 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
11912 w->window_end_bytepos
11913 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
11914 w->window_end_vpos
11915 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
11916 xassert (w->window_end_bytepos >= 0);
11917 }
11918 else if (first_unchanged_at_end_row == NULL
11919 && last_text_row == NULL
11920 && last_text_row_at_end == NULL)
11921 {
11922 /* Displayed to end of window, but no line containing text was
11923 displayed. Lines were deleted at the end of the window. */
11924 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
11925 int vpos = XFASTINT (w->window_end_vpos);
11926 struct glyph_row *current_row = current_matrix->rows + vpos;
11927 struct glyph_row *desired_row = desired_matrix->rows + vpos;
11928
11929 for (row = NULL;
11930 row == NULL && vpos >= first_vpos;
11931 --vpos, --current_row, --desired_row)
11932 {
11933 if (desired_row->enabled_p)
11934 {
11935 if (desired_row->displays_text_p)
11936 row = desired_row;
11937 }
11938 else if (current_row->displays_text_p)
11939 row = current_row;
11940 }
11941
11942 xassert (row != NULL);
11943 w->window_end_vpos = make_number (vpos + 1);
11944 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
11945 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11946 xassert (w->window_end_bytepos >= 0);
11947 IF_DEBUG (debug_method_add (w, "C"));
11948 }
11949 else
11950 abort ();
11951
11952 #if 0 /* This leads to problems, for instance when the cursor is
11953 at ZV, and the cursor line displays no text. */
11954 /* Disable rows below what's displayed in the window. This makes
11955 debugging easier. */
11956 enable_glyph_matrix_rows (current_matrix,
11957 XFASTINT (w->window_end_vpos) + 1,
11958 bottom_vpos, 0);
11959 #endif
11960
11961 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
11962 debug_end_vpos = XFASTINT (w->window_end_vpos));
11963
11964 /* Record that display has not been completed. */
11965 w->window_end_valid = Qnil;
11966 w->desired_matrix->no_scrolling_p = 1;
11967 return 3;
11968
11969 #undef GIVE_UP
11970 }
11971
11972
11973 \f
11974 /***********************************************************************
11975 More debugging support
11976 ***********************************************************************/
11977
11978 #if GLYPH_DEBUG
11979
11980 void dump_glyph_row P_ ((struct glyph_row *, int, int));
11981 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
11982 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
11983
11984
11985 /* Dump the contents of glyph matrix MATRIX on stderr.
11986
11987 GLYPHS 0 means don't show glyph contents.
11988 GLYPHS 1 means show glyphs in short form
11989 GLYPHS > 1 means show glyphs in long form. */
11990
11991 void
11992 dump_glyph_matrix (matrix, glyphs)
11993 struct glyph_matrix *matrix;
11994 int glyphs;
11995 {
11996 int i;
11997 for (i = 0; i < matrix->nrows; ++i)
11998 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
11999 }
12000
12001
12002 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
12003 the glyph row and area where the glyph comes from. */
12004
12005 void
12006 dump_glyph (row, glyph, area)
12007 struct glyph_row *row;
12008 struct glyph *glyph;
12009 int area;
12010 {
12011 if (glyph->type == CHAR_GLYPH)
12012 {
12013 fprintf (stderr,
12014 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12015 glyph - row->glyphs[TEXT_AREA],
12016 'C',
12017 glyph->charpos,
12018 (BUFFERP (glyph->object)
12019 ? 'B'
12020 : (STRINGP (glyph->object)
12021 ? 'S'
12022 : '-')),
12023 glyph->pixel_width,
12024 glyph->u.ch,
12025 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
12026 ? glyph->u.ch
12027 : '.'),
12028 glyph->face_id,
12029 glyph->left_box_line_p,
12030 glyph->right_box_line_p);
12031 }
12032 else if (glyph->type == STRETCH_GLYPH)
12033 {
12034 fprintf (stderr,
12035 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12036 glyph - row->glyphs[TEXT_AREA],
12037 'S',
12038 glyph->charpos,
12039 (BUFFERP (glyph->object)
12040 ? 'B'
12041 : (STRINGP (glyph->object)
12042 ? 'S'
12043 : '-')),
12044 glyph->pixel_width,
12045 0,
12046 '.',
12047 glyph->face_id,
12048 glyph->left_box_line_p,
12049 glyph->right_box_line_p);
12050 }
12051 else if (glyph->type == IMAGE_GLYPH)
12052 {
12053 fprintf (stderr,
12054 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12055 glyph - row->glyphs[TEXT_AREA],
12056 'I',
12057 glyph->charpos,
12058 (BUFFERP (glyph->object)
12059 ? 'B'
12060 : (STRINGP (glyph->object)
12061 ? 'S'
12062 : '-')),
12063 glyph->pixel_width,
12064 glyph->u.img_id,
12065 '.',
12066 glyph->face_id,
12067 glyph->left_box_line_p,
12068 glyph->right_box_line_p);
12069 }
12070 }
12071
12072
12073 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
12074 GLYPHS 0 means don't show glyph contents.
12075 GLYPHS 1 means show glyphs in short form
12076 GLYPHS > 1 means show glyphs in long form. */
12077
12078 void
12079 dump_glyph_row (row, vpos, glyphs)
12080 struct glyph_row *row;
12081 int vpos, glyphs;
12082 {
12083 if (glyphs != 1)
12084 {
12085 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
12086 fprintf (stderr, "=======================================================================\n");
12087
12088 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d\
12089 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
12090 vpos,
12091 MATRIX_ROW_START_CHARPOS (row),
12092 MATRIX_ROW_END_CHARPOS (row),
12093 row->used[TEXT_AREA],
12094 row->contains_overlapping_glyphs_p,
12095 row->enabled_p,
12096 row->truncated_on_left_p,
12097 row->truncated_on_right_p,
12098 row->overlay_arrow_p,
12099 row->continued_p,
12100 MATRIX_ROW_CONTINUATION_LINE_P (row),
12101 row->displays_text_p,
12102 row->ends_at_zv_p,
12103 row->fill_line_p,
12104 row->ends_in_middle_of_char_p,
12105 row->starts_in_middle_of_char_p,
12106 row->mouse_face_p,
12107 row->x,
12108 row->y,
12109 row->pixel_width,
12110 row->height,
12111 row->visible_height,
12112 row->ascent,
12113 row->phys_ascent);
12114 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
12115 row->end.overlay_string_index,
12116 row->continuation_lines_width);
12117 fprintf (stderr, "%9d %5d\n",
12118 CHARPOS (row->start.string_pos),
12119 CHARPOS (row->end.string_pos));
12120 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
12121 row->end.dpvec_index);
12122 }
12123
12124 if (glyphs > 1)
12125 {
12126 int area;
12127
12128 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12129 {
12130 struct glyph *glyph = row->glyphs[area];
12131 struct glyph *glyph_end = glyph + row->used[area];
12132
12133 /* Glyph for a line end in text. */
12134 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
12135 ++glyph_end;
12136
12137 if (glyph < glyph_end)
12138 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
12139
12140 for (; glyph < glyph_end; ++glyph)
12141 dump_glyph (row, glyph, area);
12142 }
12143 }
12144 else if (glyphs == 1)
12145 {
12146 int area;
12147
12148 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12149 {
12150 char *s = (char *) alloca (row->used[area] + 1);
12151 int i;
12152
12153 for (i = 0; i < row->used[area]; ++i)
12154 {
12155 struct glyph *glyph = row->glyphs[area] + i;
12156 if (glyph->type == CHAR_GLYPH
12157 && glyph->u.ch < 0x80
12158 && glyph->u.ch >= ' ')
12159 s[i] = glyph->u.ch;
12160 else
12161 s[i] = '.';
12162 }
12163
12164 s[i] = '\0';
12165 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
12166 }
12167 }
12168 }
12169
12170
12171 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
12172 Sdump_glyph_matrix, 0, 1, "p",
12173 doc: /* Dump the current matrix of the selected window to stderr.
12174 Shows contents of glyph row structures. With non-nil
12175 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
12176 glyphs in short form, otherwise show glyphs in long form. */)
12177 (glyphs)
12178 Lisp_Object glyphs;
12179 {
12180 struct window *w = XWINDOW (selected_window);
12181 struct buffer *buffer = XBUFFER (w->buffer);
12182
12183 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
12184 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
12185 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
12186 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
12187 fprintf (stderr, "=============================================\n");
12188 dump_glyph_matrix (w->current_matrix,
12189 NILP (glyphs) ? 0 : XINT (glyphs));
12190 return Qnil;
12191 }
12192
12193
12194 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
12195 doc: /* Dump glyph row ROW to stderr.
12196 GLYPH 0 means don't dump glyphs.
12197 GLYPH 1 means dump glyphs in short form.
12198 GLYPH > 1 or omitted means dump glyphs in long form. */)
12199 (row, glyphs)
12200 Lisp_Object row, glyphs;
12201 {
12202 struct glyph_matrix *matrix;
12203 int vpos;
12204
12205 CHECK_NUMBER (row);
12206 matrix = XWINDOW (selected_window)->current_matrix;
12207 vpos = XINT (row);
12208 if (vpos >= 0 && vpos < matrix->nrows)
12209 dump_glyph_row (MATRIX_ROW (matrix, vpos),
12210 vpos,
12211 INTEGERP (glyphs) ? XINT (glyphs) : 2);
12212 return Qnil;
12213 }
12214
12215
12216 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
12217 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
12218 GLYPH 0 means don't dump glyphs.
12219 GLYPH 1 means dump glyphs in short form.
12220 GLYPH > 1 or omitted means dump glyphs in long form. */)
12221 (row, glyphs)
12222 Lisp_Object row, glyphs;
12223 {
12224 struct frame *sf = SELECTED_FRAME ();
12225 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
12226 int vpos;
12227
12228 CHECK_NUMBER (row);
12229 vpos = XINT (row);
12230 if (vpos >= 0 && vpos < m->nrows)
12231 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
12232 INTEGERP (glyphs) ? XINT (glyphs) : 2);
12233 return Qnil;
12234 }
12235
12236
12237 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
12238 doc: /* Toggle tracing of redisplay.
12239 With ARG, turn tracing on if and only if ARG is positive. */)
12240 (arg)
12241 Lisp_Object arg;
12242 {
12243 if (NILP (arg))
12244 trace_redisplay_p = !trace_redisplay_p;
12245 else
12246 {
12247 arg = Fprefix_numeric_value (arg);
12248 trace_redisplay_p = XINT (arg) > 0;
12249 }
12250
12251 return Qnil;
12252 }
12253
12254
12255 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
12256 doc: /* Like `format', but print result to stderr. */)
12257 (nargs, args)
12258 int nargs;
12259 Lisp_Object *args;
12260 {
12261 Lisp_Object s = Fformat (nargs, args);
12262 fprintf (stderr, "%s", XSTRING (s)->data);
12263 return Qnil;
12264 }
12265
12266 #endif /* GLYPH_DEBUG */
12267
12268
12269 \f
12270 /***********************************************************************
12271 Building Desired Matrix Rows
12272 ***********************************************************************/
12273
12274 /* Return a temporary glyph row holding the glyphs of an overlay
12275 arrow. Only used for non-window-redisplay windows. */
12276
12277 static struct glyph_row *
12278 get_overlay_arrow_glyph_row (w)
12279 struct window *w;
12280 {
12281 struct frame *f = XFRAME (WINDOW_FRAME (w));
12282 struct buffer *buffer = XBUFFER (w->buffer);
12283 struct buffer *old = current_buffer;
12284 unsigned char *arrow_string = XSTRING (Voverlay_arrow_string)->data;
12285 int arrow_len = XSTRING (Voverlay_arrow_string)->size;
12286 unsigned char *arrow_end = arrow_string + arrow_len;
12287 unsigned char *p;
12288 struct it it;
12289 int multibyte_p;
12290 int n_glyphs_before;
12291
12292 set_buffer_temp (buffer);
12293 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
12294 it.glyph_row->used[TEXT_AREA] = 0;
12295 SET_TEXT_POS (it.position, 0, 0);
12296
12297 multibyte_p = !NILP (buffer->enable_multibyte_characters);
12298 p = arrow_string;
12299 while (p < arrow_end)
12300 {
12301 Lisp_Object face, ilisp;
12302
12303 /* Get the next character. */
12304 if (multibyte_p)
12305 it.c = string_char_and_length (p, arrow_len, &it.len);
12306 else
12307 it.c = *p, it.len = 1;
12308 p += it.len;
12309
12310 /* Get its face. */
12311 ilisp = make_number (p - arrow_string);
12312 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
12313 it.face_id = compute_char_face (f, it.c, face);
12314
12315 /* Compute its width, get its glyphs. */
12316 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
12317 SET_TEXT_POS (it.position, -1, -1);
12318 PRODUCE_GLYPHS (&it);
12319
12320 /* If this character doesn't fit any more in the line, we have
12321 to remove some glyphs. */
12322 if (it.current_x > it.last_visible_x)
12323 {
12324 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
12325 break;
12326 }
12327 }
12328
12329 set_buffer_temp (old);
12330 return it.glyph_row;
12331 }
12332
12333
12334 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
12335 glyphs are only inserted for terminal frames since we can't really
12336 win with truncation glyphs when partially visible glyphs are
12337 involved. Which glyphs to insert is determined by
12338 produce_special_glyphs. */
12339
12340 static void
12341 insert_left_trunc_glyphs (it)
12342 struct it *it;
12343 {
12344 struct it truncate_it;
12345 struct glyph *from, *end, *to, *toend;
12346
12347 xassert (!FRAME_WINDOW_P (it->f));
12348
12349 /* Get the truncation glyphs. */
12350 truncate_it = *it;
12351 truncate_it.current_x = 0;
12352 truncate_it.face_id = DEFAULT_FACE_ID;
12353 truncate_it.glyph_row = &scratch_glyph_row;
12354 truncate_it.glyph_row->used[TEXT_AREA] = 0;
12355 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
12356 truncate_it.object = make_number (0);
12357 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
12358
12359 /* Overwrite glyphs from IT with truncation glyphs. */
12360 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
12361 end = from + truncate_it.glyph_row->used[TEXT_AREA];
12362 to = it->glyph_row->glyphs[TEXT_AREA];
12363 toend = to + it->glyph_row->used[TEXT_AREA];
12364
12365 while (from < end)
12366 *to++ = *from++;
12367
12368 /* There may be padding glyphs left over. Overwrite them too. */
12369 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
12370 {
12371 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
12372 while (from < end)
12373 *to++ = *from++;
12374 }
12375
12376 if (to > toend)
12377 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
12378 }
12379
12380
12381 /* Compute the pixel height and width of IT->glyph_row.
12382
12383 Most of the time, ascent and height of a display line will be equal
12384 to the max_ascent and max_height values of the display iterator
12385 structure. This is not the case if
12386
12387 1. We hit ZV without displaying anything. In this case, max_ascent
12388 and max_height will be zero.
12389
12390 2. We have some glyphs that don't contribute to the line height.
12391 (The glyph row flag contributes_to_line_height_p is for future
12392 pixmap extensions).
12393
12394 The first case is easily covered by using default values because in
12395 these cases, the line height does not really matter, except that it
12396 must not be zero. */
12397
12398 static void
12399 compute_line_metrics (it)
12400 struct it *it;
12401 {
12402 struct glyph_row *row = it->glyph_row;
12403 int area, i;
12404
12405 if (FRAME_WINDOW_P (it->f))
12406 {
12407 int i, min_y, max_y;
12408
12409 /* The line may consist of one space only, that was added to
12410 place the cursor on it. If so, the row's height hasn't been
12411 computed yet. */
12412 if (row->height == 0)
12413 {
12414 if (it->max_ascent + it->max_descent == 0)
12415 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
12416 row->ascent = it->max_ascent;
12417 row->height = it->max_ascent + it->max_descent;
12418 row->phys_ascent = it->max_phys_ascent;
12419 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
12420 }
12421
12422 /* Compute the width of this line. */
12423 row->pixel_width = row->x;
12424 for (i = 0; i < row->used[TEXT_AREA]; ++i)
12425 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
12426
12427 xassert (row->pixel_width >= 0);
12428 xassert (row->ascent >= 0 && row->height > 0);
12429
12430 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
12431 || MATRIX_ROW_OVERLAPS_PRED_P (row));
12432
12433 /* If first line's physical ascent is larger than its logical
12434 ascent, use the physical ascent, and make the row taller.
12435 This makes accented characters fully visible. */
12436 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
12437 && row->phys_ascent > row->ascent)
12438 {
12439 row->height += row->phys_ascent - row->ascent;
12440 row->ascent = row->phys_ascent;
12441 }
12442
12443 /* Compute how much of the line is visible. */
12444 row->visible_height = row->height;
12445
12446 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (it->w);
12447 max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
12448
12449 if (row->y < min_y)
12450 row->visible_height -= min_y - row->y;
12451 if (row->y + row->height > max_y)
12452 row->visible_height -= row->y + row->height - max_y;
12453 }
12454 else
12455 {
12456 row->pixel_width = row->used[TEXT_AREA];
12457 if (row->continued_p)
12458 row->pixel_width -= it->continuation_pixel_width;
12459 else if (row->truncated_on_right_p)
12460 row->pixel_width -= it->truncation_pixel_width;
12461 row->ascent = row->phys_ascent = 0;
12462 row->height = row->phys_height = row->visible_height = 1;
12463 }
12464
12465 /* Compute a hash code for this row. */
12466 row->hash = 0;
12467 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12468 for (i = 0; i < row->used[area]; ++i)
12469 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
12470 + row->glyphs[area][i].u.val
12471 + row->glyphs[area][i].face_id
12472 + row->glyphs[area][i].padding_p
12473 + (row->glyphs[area][i].type << 2));
12474
12475 it->max_ascent = it->max_descent = 0;
12476 it->max_phys_ascent = it->max_phys_descent = 0;
12477 }
12478
12479
12480 /* Append one space to the glyph row of iterator IT if doing a
12481 window-based redisplay. DEFAULT_FACE_P non-zero means let the
12482 space have the default face, otherwise let it have the same face as
12483 IT->face_id. Value is non-zero if a space was added.
12484
12485 This function is called to make sure that there is always one glyph
12486 at the end of a glyph row that the cursor can be set on under
12487 window-systems. (If there weren't such a glyph we would not know
12488 how wide and tall a box cursor should be displayed).
12489
12490 At the same time this space let's a nicely handle clearing to the
12491 end of the line if the row ends in italic text. */
12492
12493 static int
12494 append_space (it, default_face_p)
12495 struct it *it;
12496 int default_face_p;
12497 {
12498 if (FRAME_WINDOW_P (it->f))
12499 {
12500 int n = it->glyph_row->used[TEXT_AREA];
12501
12502 if (it->glyph_row->glyphs[TEXT_AREA] + n
12503 < it->glyph_row->glyphs[1 + TEXT_AREA])
12504 {
12505 /* Save some values that must not be changed.
12506 Must save IT->c and IT->len because otherwise
12507 ITERATOR_AT_END_P wouldn't work anymore after
12508 append_space has been called. */
12509 enum display_element_type saved_what = it->what;
12510 int saved_c = it->c, saved_len = it->len;
12511 int saved_x = it->current_x;
12512 int saved_face_id = it->face_id;
12513 struct text_pos saved_pos;
12514 Lisp_Object saved_object;
12515 struct face *face;
12516
12517 saved_object = it->object;
12518 saved_pos = it->position;
12519
12520 it->what = IT_CHARACTER;
12521 bzero (&it->position, sizeof it->position);
12522 it->object = make_number (0);
12523 it->c = ' ';
12524 it->len = 1;
12525
12526 if (default_face_p)
12527 it->face_id = DEFAULT_FACE_ID;
12528 else if (it->face_before_selective_p)
12529 it->face_id = it->saved_face_id;
12530 face = FACE_FROM_ID (it->f, it->face_id);
12531 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
12532
12533 PRODUCE_GLYPHS (it);
12534
12535 it->current_x = saved_x;
12536 it->object = saved_object;
12537 it->position = saved_pos;
12538 it->what = saved_what;
12539 it->face_id = saved_face_id;
12540 it->len = saved_len;
12541 it->c = saved_c;
12542 return 1;
12543 }
12544 }
12545
12546 return 0;
12547 }
12548
12549
12550 /* Extend the face of the last glyph in the text area of IT->glyph_row
12551 to the end of the display line. Called from display_line.
12552 If the glyph row is empty, add a space glyph to it so that we
12553 know the face to draw. Set the glyph row flag fill_line_p. */
12554
12555 static void
12556 extend_face_to_end_of_line (it)
12557 struct it *it;
12558 {
12559 struct face *face;
12560 struct frame *f = it->f;
12561
12562 /* If line is already filled, do nothing. */
12563 if (it->current_x >= it->last_visible_x)
12564 return;
12565
12566 /* Face extension extends the background and box of IT->face_id
12567 to the end of the line. If the background equals the background
12568 of the frame, we don't have to do anything. */
12569 if (it->face_before_selective_p)
12570 face = FACE_FROM_ID (it->f, it->saved_face_id);
12571 else
12572 face = FACE_FROM_ID (f, it->face_id);
12573
12574 if (FRAME_WINDOW_P (f)
12575 && face->box == FACE_NO_BOX
12576 && face->background == FRAME_BACKGROUND_PIXEL (f)
12577 && !face->stipple)
12578 return;
12579
12580 /* Set the glyph row flag indicating that the face of the last glyph
12581 in the text area has to be drawn to the end of the text area. */
12582 it->glyph_row->fill_line_p = 1;
12583
12584 /* If current character of IT is not ASCII, make sure we have the
12585 ASCII face. This will be automatically undone the next time
12586 get_next_display_element returns a multibyte character. Note
12587 that the character will always be single byte in unibyte text. */
12588 if (!SINGLE_BYTE_CHAR_P (it->c))
12589 {
12590 it->face_id = FACE_FOR_CHAR (f, face, 0);
12591 }
12592
12593 if (FRAME_WINDOW_P (f))
12594 {
12595 /* If the row is empty, add a space with the current face of IT,
12596 so that we know which face to draw. */
12597 if (it->glyph_row->used[TEXT_AREA] == 0)
12598 {
12599 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
12600 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
12601 it->glyph_row->used[TEXT_AREA] = 1;
12602 }
12603 }
12604 else
12605 {
12606 /* Save some values that must not be changed. */
12607 int saved_x = it->current_x;
12608 struct text_pos saved_pos;
12609 Lisp_Object saved_object;
12610 enum display_element_type saved_what = it->what;
12611 int saved_face_id = it->face_id;
12612
12613 saved_object = it->object;
12614 saved_pos = it->position;
12615
12616 it->what = IT_CHARACTER;
12617 bzero (&it->position, sizeof it->position);
12618 it->object = make_number (0);
12619 it->c = ' ';
12620 it->len = 1;
12621 it->face_id = face->id;
12622
12623 PRODUCE_GLYPHS (it);
12624
12625 while (it->current_x <= it->last_visible_x)
12626 PRODUCE_GLYPHS (it);
12627
12628 /* Don't count these blanks really. It would let us insert a left
12629 truncation glyph below and make us set the cursor on them, maybe. */
12630 it->current_x = saved_x;
12631 it->object = saved_object;
12632 it->position = saved_pos;
12633 it->what = saved_what;
12634 it->face_id = saved_face_id;
12635 }
12636 }
12637
12638
12639 /* Value is non-zero if text starting at CHARPOS in current_buffer is
12640 trailing whitespace. */
12641
12642 static int
12643 trailing_whitespace_p (charpos)
12644 int charpos;
12645 {
12646 int bytepos = CHAR_TO_BYTE (charpos);
12647 int c = 0;
12648
12649 while (bytepos < ZV_BYTE
12650 && (c = FETCH_CHAR (bytepos),
12651 c == ' ' || c == '\t'))
12652 ++bytepos;
12653
12654 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
12655 {
12656 if (bytepos != PT_BYTE)
12657 return 1;
12658 }
12659 return 0;
12660 }
12661
12662
12663 /* Highlight trailing whitespace, if any, in ROW. */
12664
12665 void
12666 highlight_trailing_whitespace (f, row)
12667 struct frame *f;
12668 struct glyph_row *row;
12669 {
12670 int used = row->used[TEXT_AREA];
12671
12672 if (used)
12673 {
12674 struct glyph *start = row->glyphs[TEXT_AREA];
12675 struct glyph *glyph = start + used - 1;
12676
12677 /* Skip over glyphs inserted to display the cursor at the
12678 end of a line, for extending the face of the last glyph
12679 to the end of the line on terminals, and for truncation
12680 and continuation glyphs. */
12681 while (glyph >= start
12682 && glyph->type == CHAR_GLYPH
12683 && INTEGERP (glyph->object))
12684 --glyph;
12685
12686 /* If last glyph is a space or stretch, and it's trailing
12687 whitespace, set the face of all trailing whitespace glyphs in
12688 IT->glyph_row to `trailing-whitespace'. */
12689 if (glyph >= start
12690 && BUFFERP (glyph->object)
12691 && (glyph->type == STRETCH_GLYPH
12692 || (glyph->type == CHAR_GLYPH
12693 && glyph->u.ch == ' '))
12694 && trailing_whitespace_p (glyph->charpos))
12695 {
12696 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
12697
12698 while (glyph >= start
12699 && BUFFERP (glyph->object)
12700 && (glyph->type == STRETCH_GLYPH
12701 || (glyph->type == CHAR_GLYPH
12702 && glyph->u.ch == ' ')))
12703 (glyph--)->face_id = face_id;
12704 }
12705 }
12706 }
12707
12708
12709 /* Value is non-zero if glyph row ROW in window W should be
12710 used to hold the cursor. */
12711
12712 static int
12713 cursor_row_p (w, row)
12714 struct window *w;
12715 struct glyph_row *row;
12716 {
12717 int cursor_row_p = 1;
12718
12719 if (PT == MATRIX_ROW_END_CHARPOS (row))
12720 {
12721 /* If the row ends with a newline from a string, we don't want
12722 the cursor there (if the row is continued it doesn't end in a
12723 newline). */
12724 if (CHARPOS (row->end.string_pos) >= 0
12725 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12726 cursor_row_p = row->continued_p;
12727
12728 /* If the row ends at ZV, display the cursor at the end of that
12729 row instead of at the start of the row below. */
12730 else if (row->ends_at_zv_p)
12731 cursor_row_p = 1;
12732 else
12733 cursor_row_p = 0;
12734 }
12735
12736 return cursor_row_p;
12737 }
12738
12739
12740 /* Construct the glyph row IT->glyph_row in the desired matrix of
12741 IT->w from text at the current position of IT. See dispextern.h
12742 for an overview of struct it. Value is non-zero if
12743 IT->glyph_row displays text, as opposed to a line displaying ZV
12744 only. */
12745
12746 static int
12747 display_line (it)
12748 struct it *it;
12749 {
12750 struct glyph_row *row = it->glyph_row;
12751
12752 /* We always start displaying at hpos zero even if hscrolled. */
12753 xassert (it->hpos == 0 && it->current_x == 0);
12754
12755 /* We must not display in a row that's not a text row. */
12756 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
12757 < it->w->desired_matrix->nrows);
12758
12759 /* Is IT->w showing the region? */
12760 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
12761
12762 /* Clear the result glyph row and enable it. */
12763 prepare_desired_row (row);
12764
12765 row->y = it->current_y;
12766 row->start = it->current;
12767 row->continuation_lines_width = it->continuation_lines_width;
12768 row->displays_text_p = 1;
12769 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
12770 it->starts_in_middle_of_char_p = 0;
12771
12772 /* Arrange the overlays nicely for our purposes. Usually, we call
12773 display_line on only one line at a time, in which case this
12774 can't really hurt too much, or we call it on lines which appear
12775 one after another in the buffer, in which case all calls to
12776 recenter_overlay_lists but the first will be pretty cheap. */
12777 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
12778
12779 /* Move over display elements that are not visible because we are
12780 hscrolled. This may stop at an x-position < IT->first_visible_x
12781 if the first glyph is partially visible or if we hit a line end. */
12782 if (it->current_x < it->first_visible_x)
12783 move_it_in_display_line_to (it, ZV, it->first_visible_x,
12784 MOVE_TO_POS | MOVE_TO_X);
12785
12786 /* Get the initial row height. This is either the height of the
12787 text hscrolled, if there is any, or zero. */
12788 row->ascent = it->max_ascent;
12789 row->height = it->max_ascent + it->max_descent;
12790 row->phys_ascent = it->max_phys_ascent;
12791 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
12792
12793 /* Loop generating characters. The loop is left with IT on the next
12794 character to display. */
12795 while (1)
12796 {
12797 int n_glyphs_before, hpos_before, x_before;
12798 int x, i, nglyphs;
12799 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
12800
12801 /* Retrieve the next thing to display. Value is zero if end of
12802 buffer reached. */
12803 if (!get_next_display_element (it))
12804 {
12805 /* Maybe add a space at the end of this line that is used to
12806 display the cursor there under X. Set the charpos of the
12807 first glyph of blank lines not corresponding to any text
12808 to -1. */
12809 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
12810 || row->used[TEXT_AREA] == 0)
12811 {
12812 row->glyphs[TEXT_AREA]->charpos = -1;
12813 row->displays_text_p = 0;
12814
12815 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
12816 && (!MINI_WINDOW_P (it->w)
12817 || (minibuf_level && EQ (it->window, minibuf_window))))
12818 row->indicate_empty_line_p = 1;
12819 }
12820
12821 it->continuation_lines_width = 0;
12822 row->ends_at_zv_p = 1;
12823 break;
12824 }
12825
12826 /* Now, get the metrics of what we want to display. This also
12827 generates glyphs in `row' (which is IT->glyph_row). */
12828 n_glyphs_before = row->used[TEXT_AREA];
12829 x = it->current_x;
12830
12831 /* Remember the line height so far in case the next element doesn't
12832 fit on the line. */
12833 if (!it->truncate_lines_p)
12834 {
12835 ascent = it->max_ascent;
12836 descent = it->max_descent;
12837 phys_ascent = it->max_phys_ascent;
12838 phys_descent = it->max_phys_descent;
12839 }
12840
12841 PRODUCE_GLYPHS (it);
12842
12843 /* If this display element was in marginal areas, continue with
12844 the next one. */
12845 if (it->area != TEXT_AREA)
12846 {
12847 row->ascent = max (row->ascent, it->max_ascent);
12848 row->height = max (row->height, it->max_ascent + it->max_descent);
12849 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12850 row->phys_height = max (row->phys_height,
12851 it->max_phys_ascent + it->max_phys_descent);
12852 set_iterator_to_next (it, 1);
12853 continue;
12854 }
12855
12856 /* Does the display element fit on the line? If we truncate
12857 lines, we should draw past the right edge of the window. If
12858 we don't truncate, we want to stop so that we can display the
12859 continuation glyph before the right margin. If lines are
12860 continued, there are two possible strategies for characters
12861 resulting in more than 1 glyph (e.g. tabs): Display as many
12862 glyphs as possible in this line and leave the rest for the
12863 continuation line, or display the whole element in the next
12864 line. Original redisplay did the former, so we do it also. */
12865 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
12866 hpos_before = it->hpos;
12867 x_before = x;
12868
12869 if (/* Not a newline. */
12870 nglyphs > 0
12871 /* Glyphs produced fit entirely in the line. */
12872 && it->current_x < it->last_visible_x)
12873 {
12874 it->hpos += nglyphs;
12875 row->ascent = max (row->ascent, it->max_ascent);
12876 row->height = max (row->height, it->max_ascent + it->max_descent);
12877 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12878 row->phys_height = max (row->phys_height,
12879 it->max_phys_ascent + it->max_phys_descent);
12880 if (it->current_x - it->pixel_width < it->first_visible_x)
12881 row->x = x - it->first_visible_x;
12882 }
12883 else
12884 {
12885 int new_x;
12886 struct glyph *glyph;
12887
12888 for (i = 0; i < nglyphs; ++i, x = new_x)
12889 {
12890 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12891 new_x = x + glyph->pixel_width;
12892
12893 if (/* Lines are continued. */
12894 !it->truncate_lines_p
12895 && (/* Glyph doesn't fit on the line. */
12896 new_x > it->last_visible_x
12897 /* Or it fits exactly on a window system frame. */
12898 || (new_x == it->last_visible_x
12899 && FRAME_WINDOW_P (it->f))))
12900 {
12901 /* End of a continued line. */
12902
12903 if (it->hpos == 0
12904 || (new_x == it->last_visible_x
12905 && FRAME_WINDOW_P (it->f)))
12906 {
12907 /* Current glyph is the only one on the line or
12908 fits exactly on the line. We must continue
12909 the line because we can't draw the cursor
12910 after the glyph. */
12911 row->continued_p = 1;
12912 it->current_x = new_x;
12913 it->continuation_lines_width += new_x;
12914 ++it->hpos;
12915 if (i == nglyphs - 1)
12916 set_iterator_to_next (it, 1);
12917 }
12918 else if (CHAR_GLYPH_PADDING_P (*glyph)
12919 && !FRAME_WINDOW_P (it->f))
12920 {
12921 /* A padding glyph that doesn't fit on this line.
12922 This means the whole character doesn't fit
12923 on the line. */
12924 row->used[TEXT_AREA] = n_glyphs_before;
12925
12926 /* Fill the rest of the row with continuation
12927 glyphs like in 20.x. */
12928 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
12929 < row->glyphs[1 + TEXT_AREA])
12930 produce_special_glyphs (it, IT_CONTINUATION);
12931
12932 row->continued_p = 1;
12933 it->current_x = x_before;
12934 it->continuation_lines_width += x_before;
12935
12936 /* Restore the height to what it was before the
12937 element not fitting on the line. */
12938 it->max_ascent = ascent;
12939 it->max_descent = descent;
12940 it->max_phys_ascent = phys_ascent;
12941 it->max_phys_descent = phys_descent;
12942 }
12943 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
12944 {
12945 /* A TAB that extends past the right edge of the
12946 window. This produces a single glyph on
12947 window system frames. We leave the glyph in
12948 this row and let it fill the row, but don't
12949 consume the TAB. */
12950 it->continuation_lines_width += it->last_visible_x;
12951 row->ends_in_middle_of_char_p = 1;
12952 row->continued_p = 1;
12953 glyph->pixel_width = it->last_visible_x - x;
12954 it->starts_in_middle_of_char_p = 1;
12955 }
12956 else
12957 {
12958 /* Something other than a TAB that draws past
12959 the right edge of the window. Restore
12960 positions to values before the element. */
12961 row->used[TEXT_AREA] = n_glyphs_before + i;
12962
12963 /* Display continuation glyphs. */
12964 if (!FRAME_WINDOW_P (it->f))
12965 produce_special_glyphs (it, IT_CONTINUATION);
12966 row->continued_p = 1;
12967
12968 it->continuation_lines_width += x;
12969
12970 if (nglyphs > 1 && i > 0)
12971 {
12972 row->ends_in_middle_of_char_p = 1;
12973 it->starts_in_middle_of_char_p = 1;
12974 }
12975
12976 /* Restore the height to what it was before the
12977 element not fitting on the line. */
12978 it->max_ascent = ascent;
12979 it->max_descent = descent;
12980 it->max_phys_ascent = phys_ascent;
12981 it->max_phys_descent = phys_descent;
12982 }
12983
12984 break;
12985 }
12986 else if (new_x > it->first_visible_x)
12987 {
12988 /* Increment number of glyphs actually displayed. */
12989 ++it->hpos;
12990
12991 if (x < it->first_visible_x)
12992 /* Glyph is partially visible, i.e. row starts at
12993 negative X position. */
12994 row->x = x - it->first_visible_x;
12995 }
12996 else
12997 {
12998 /* Glyph is completely off the left margin of the
12999 window. This should not happen because of the
13000 move_it_in_display_line at the start of
13001 this function. */
13002 abort ();
13003 }
13004 }
13005
13006 row->ascent = max (row->ascent, it->max_ascent);
13007 row->height = max (row->height, it->max_ascent + it->max_descent);
13008 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13009 row->phys_height = max (row->phys_height,
13010 it->max_phys_ascent + it->max_phys_descent);
13011
13012 /* End of this display line if row is continued. */
13013 if (row->continued_p)
13014 break;
13015 }
13016
13017 /* Is this a line end? If yes, we're also done, after making
13018 sure that a non-default face is extended up to the right
13019 margin of the window. */
13020 if (ITERATOR_AT_END_OF_LINE_P (it))
13021 {
13022 int used_before = row->used[TEXT_AREA];
13023
13024 row->ends_in_newline_from_string_p = STRINGP (it->object);
13025
13026 /* Add a space at the end of the line that is used to
13027 display the cursor there. */
13028 append_space (it, 0);
13029
13030 /* Extend the face to the end of the line. */
13031 extend_face_to_end_of_line (it);
13032
13033 /* Make sure we have the position. */
13034 if (used_before == 0)
13035 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
13036
13037 /* Consume the line end. This skips over invisible lines. */
13038 set_iterator_to_next (it, 1);
13039 it->continuation_lines_width = 0;
13040 break;
13041 }
13042
13043 /* Proceed with next display element. Note that this skips
13044 over lines invisible because of selective display. */
13045 set_iterator_to_next (it, 1);
13046
13047 /* If we truncate lines, we are done when the last displayed
13048 glyphs reach past the right margin of the window. */
13049 if (it->truncate_lines_p
13050 && (FRAME_WINDOW_P (it->f)
13051 ? (it->current_x >= it->last_visible_x)
13052 : (it->current_x > it->last_visible_x)))
13053 {
13054 /* Maybe add truncation glyphs. */
13055 if (!FRAME_WINDOW_P (it->f))
13056 {
13057 int i, n;
13058
13059 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
13060 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
13061 break;
13062
13063 for (n = row->used[TEXT_AREA]; i < n; ++i)
13064 {
13065 row->used[TEXT_AREA] = i;
13066 produce_special_glyphs (it, IT_TRUNCATION);
13067 }
13068 }
13069
13070 row->truncated_on_right_p = 1;
13071 it->continuation_lines_width = 0;
13072 reseat_at_next_visible_line_start (it, 0);
13073 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
13074 it->hpos = hpos_before;
13075 it->current_x = x_before;
13076 break;
13077 }
13078 }
13079
13080 /* If line is not empty and hscrolled, maybe insert truncation glyphs
13081 at the left window margin. */
13082 if (it->first_visible_x
13083 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
13084 {
13085 if (!FRAME_WINDOW_P (it->f))
13086 insert_left_trunc_glyphs (it);
13087 row->truncated_on_left_p = 1;
13088 }
13089
13090 /* If the start of this line is the overlay arrow-position, then
13091 mark this glyph row as the one containing the overlay arrow.
13092 This is clearly a mess with variable size fonts. It would be
13093 better to let it be displayed like cursors under X. */
13094 if (MARKERP (Voverlay_arrow_position)
13095 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
13096 && (MATRIX_ROW_START_CHARPOS (row)
13097 == marker_position (Voverlay_arrow_position))
13098 && STRINGP (Voverlay_arrow_string)
13099 && ! overlay_arrow_seen)
13100 {
13101 /* Overlay arrow in window redisplay is a fringe bitmap. */
13102 if (!FRAME_WINDOW_P (it->f))
13103 {
13104 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
13105 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
13106 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
13107 struct glyph *p = row->glyphs[TEXT_AREA];
13108 struct glyph *p2, *end;
13109
13110 /* Copy the arrow glyphs. */
13111 while (glyph < arrow_end)
13112 *p++ = *glyph++;
13113
13114 /* Throw away padding glyphs. */
13115 p2 = p;
13116 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
13117 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
13118 ++p2;
13119 if (p2 > p)
13120 {
13121 while (p2 < end)
13122 *p++ = *p2++;
13123 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
13124 }
13125 }
13126
13127 overlay_arrow_seen = 1;
13128 row->overlay_arrow_p = 1;
13129 }
13130
13131 /* Compute pixel dimensions of this line. */
13132 compute_line_metrics (it);
13133
13134 /* Remember the position at which this line ends. */
13135 row->end = it->current;
13136
13137 /* Maybe set the cursor. */
13138 if (it->w->cursor.vpos < 0
13139 && PT >= MATRIX_ROW_START_CHARPOS (row)
13140 && PT <= MATRIX_ROW_END_CHARPOS (row)
13141 && cursor_row_p (it->w, row))
13142 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
13143
13144 /* Highlight trailing whitespace. */
13145 if (!NILP (Vshow_trailing_whitespace))
13146 highlight_trailing_whitespace (it->f, it->glyph_row);
13147
13148 /* Prepare for the next line. This line starts horizontally at (X
13149 HPOS) = (0 0). Vertical positions are incremented. As a
13150 convenience for the caller, IT->glyph_row is set to the next
13151 row to be used. */
13152 it->current_x = it->hpos = 0;
13153 it->current_y += row->height;
13154 ++it->vpos;
13155 ++it->glyph_row;
13156 return row->displays_text_p;
13157 }
13158
13159
13160 \f
13161 /***********************************************************************
13162 Menu Bar
13163 ***********************************************************************/
13164
13165 /* Redisplay the menu bar in the frame for window W.
13166
13167 The menu bar of X frames that don't have X toolkit support is
13168 displayed in a special window W->frame->menu_bar_window.
13169
13170 The menu bar of terminal frames is treated specially as far as
13171 glyph matrices are concerned. Menu bar lines are not part of
13172 windows, so the update is done directly on the frame matrix rows
13173 for the menu bar. */
13174
13175 static void
13176 display_menu_bar (w)
13177 struct window *w;
13178 {
13179 struct frame *f = XFRAME (WINDOW_FRAME (w));
13180 struct it it;
13181 Lisp_Object items;
13182 int i;
13183
13184 /* Don't do all this for graphical frames. */
13185 #ifdef HAVE_NTGUI
13186 if (!NILP (Vwindow_system))
13187 return;
13188 #endif
13189 #ifdef USE_X_TOOLKIT
13190 if (FRAME_X_P (f))
13191 return;
13192 #endif
13193 #ifdef macintosh
13194 if (FRAME_MAC_P (f))
13195 return;
13196 #endif
13197
13198 #ifdef USE_X_TOOLKIT
13199 xassert (!FRAME_WINDOW_P (f));
13200 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
13201 it.first_visible_x = 0;
13202 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
13203 #else /* not USE_X_TOOLKIT */
13204 if (FRAME_WINDOW_P (f))
13205 {
13206 /* Menu bar lines are displayed in the desired matrix of the
13207 dummy window menu_bar_window. */
13208 struct window *menu_w;
13209 xassert (WINDOWP (f->menu_bar_window));
13210 menu_w = XWINDOW (f->menu_bar_window);
13211 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
13212 MENU_FACE_ID);
13213 it.first_visible_x = 0;
13214 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
13215 }
13216 else
13217 {
13218 /* This is a TTY frame, i.e. character hpos/vpos are used as
13219 pixel x/y. */
13220 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
13221 MENU_FACE_ID);
13222 it.first_visible_x = 0;
13223 it.last_visible_x = FRAME_WIDTH (f);
13224 }
13225 #endif /* not USE_X_TOOLKIT */
13226
13227 if (! mode_line_inverse_video)
13228 /* Force the menu-bar to be displayed in the default face. */
13229 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
13230
13231 /* Clear all rows of the menu bar. */
13232 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
13233 {
13234 struct glyph_row *row = it.glyph_row + i;
13235 clear_glyph_row (row);
13236 row->enabled_p = 1;
13237 row->full_width_p = 1;
13238 }
13239
13240 /* Display all items of the menu bar. */
13241 items = FRAME_MENU_BAR_ITEMS (it.f);
13242 for (i = 0; i < XVECTOR (items)->size; i += 4)
13243 {
13244 Lisp_Object string;
13245
13246 /* Stop at nil string. */
13247 string = AREF (items, i + 1);
13248 if (NILP (string))
13249 break;
13250
13251 /* Remember where item was displayed. */
13252 AREF (items, i + 3) = make_number (it.hpos);
13253
13254 /* Display the item, pad with one space. */
13255 if (it.current_x < it.last_visible_x)
13256 display_string (NULL, string, Qnil, 0, 0, &it,
13257 XSTRING (string)->size + 1, 0, 0, -1);
13258 }
13259
13260 /* Fill out the line with spaces. */
13261 if (it.current_x < it.last_visible_x)
13262 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
13263
13264 /* Compute the total height of the lines. */
13265 compute_line_metrics (&it);
13266 }
13267
13268
13269 \f
13270 /***********************************************************************
13271 Mode Line
13272 ***********************************************************************/
13273
13274 /* Redisplay mode lines in the window tree whose root is WINDOW. If
13275 FORCE is non-zero, redisplay mode lines unconditionally.
13276 Otherwise, redisplay only mode lines that are garbaged. Value is
13277 the number of windows whose mode lines were redisplayed. */
13278
13279 static int
13280 redisplay_mode_lines (window, force)
13281 Lisp_Object window;
13282 int force;
13283 {
13284 int nwindows = 0;
13285
13286 while (!NILP (window))
13287 {
13288 struct window *w = XWINDOW (window);
13289
13290 if (WINDOWP (w->hchild))
13291 nwindows += redisplay_mode_lines (w->hchild, force);
13292 else if (WINDOWP (w->vchild))
13293 nwindows += redisplay_mode_lines (w->vchild, force);
13294 else if (force
13295 || FRAME_GARBAGED_P (XFRAME (w->frame))
13296 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
13297 {
13298 struct text_pos lpoint;
13299 struct buffer *old = current_buffer;
13300
13301 /* Set the window's buffer for the mode line display. */
13302 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13303 set_buffer_internal_1 (XBUFFER (w->buffer));
13304
13305 /* Point refers normally to the selected window. For any
13306 other window, set up appropriate value. */
13307 if (!EQ (window, selected_window))
13308 {
13309 struct text_pos pt;
13310
13311 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
13312 if (CHARPOS (pt) < BEGV)
13313 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
13314 else if (CHARPOS (pt) > (ZV - 1))
13315 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
13316 else
13317 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
13318 }
13319
13320 /* Display mode lines. */
13321 clear_glyph_matrix (w->desired_matrix);
13322 if (display_mode_lines (w))
13323 {
13324 ++nwindows;
13325 w->must_be_updated_p = 1;
13326 }
13327
13328 /* Restore old settings. */
13329 set_buffer_internal_1 (old);
13330 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13331 }
13332
13333 window = w->next;
13334 }
13335
13336 return nwindows;
13337 }
13338
13339
13340 /* Display the mode and/or top line of window W. Value is the number
13341 of mode lines displayed. */
13342
13343 static int
13344 display_mode_lines (w)
13345 struct window *w;
13346 {
13347 Lisp_Object old_selected_window, old_selected_frame;
13348 int n = 0;
13349
13350 old_selected_frame = selected_frame;
13351 selected_frame = w->frame;
13352 old_selected_window = selected_window;
13353 XSETWINDOW (selected_window, w);
13354
13355 /* These will be set while the mode line specs are processed. */
13356 line_number_displayed = 0;
13357 w->column_number_displayed = Qnil;
13358
13359 if (WINDOW_WANTS_MODELINE_P (w))
13360 {
13361 display_mode_line (w, MODE_LINE_FACE_ID,
13362 current_buffer->mode_line_format);
13363 ++n;
13364 }
13365
13366 if (WINDOW_WANTS_HEADER_LINE_P (w))
13367 {
13368 display_mode_line (w, HEADER_LINE_FACE_ID,
13369 current_buffer->header_line_format);
13370 ++n;
13371 }
13372
13373 selected_frame = old_selected_frame;
13374 selected_window = old_selected_window;
13375 return n;
13376 }
13377
13378
13379 /* Display mode or top line of window W. FACE_ID specifies which line
13380 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
13381 FORMAT is the mode line format to display. Value is the pixel
13382 height of the mode line displayed. */
13383
13384 static int
13385 display_mode_line (w, face_id, format)
13386 struct window *w;
13387 enum face_id face_id;
13388 Lisp_Object format;
13389 {
13390 struct it it;
13391 struct face *face;
13392
13393 init_iterator (&it, w, -1, -1, NULL, face_id);
13394 prepare_desired_row (it.glyph_row);
13395
13396 if (! mode_line_inverse_video)
13397 /* Force the mode-line to be displayed in the default face. */
13398 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
13399
13400 /* Temporarily make frame's keyboard the current kboard so that
13401 kboard-local variables in the mode_line_format will get the right
13402 values. */
13403 push_frame_kboard (it.f);
13404 display_mode_element (&it, 0, 0, 0, format);
13405 pop_frame_kboard ();
13406
13407 /* Fill up with spaces. */
13408 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
13409
13410 compute_line_metrics (&it);
13411 it.glyph_row->full_width_p = 1;
13412 it.glyph_row->mode_line_p = 1;
13413 it.glyph_row->continued_p = 0;
13414 it.glyph_row->truncated_on_left_p = 0;
13415 it.glyph_row->truncated_on_right_p = 0;
13416
13417 /* Make a 3D mode-line have a shadow at its right end. */
13418 face = FACE_FROM_ID (it.f, face_id);
13419 extend_face_to_end_of_line (&it);
13420 if (face->box != FACE_NO_BOX)
13421 {
13422 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
13423 + it.glyph_row->used[TEXT_AREA] - 1);
13424 last->right_box_line_p = 1;
13425 }
13426
13427 return it.glyph_row->height;
13428 }
13429
13430
13431 /* Contribute ELT to the mode line for window IT->w. How it
13432 translates into text depends on its data type.
13433
13434 IT describes the display environment in which we display, as usual.
13435
13436 DEPTH is the depth in recursion. It is used to prevent
13437 infinite recursion here.
13438
13439 FIELD_WIDTH is the number of characters the display of ELT should
13440 occupy in the mode line, and PRECISION is the maximum number of
13441 characters to display from ELT's representation. See
13442 display_string for details. *
13443
13444 Returns the hpos of the end of the text generated by ELT. */
13445
13446 static int
13447 display_mode_element (it, depth, field_width, precision, elt)
13448 struct it *it;
13449 int depth;
13450 int field_width, precision;
13451 Lisp_Object elt;
13452 {
13453 int n = 0, field, prec;
13454
13455 tail_recurse:
13456 if (depth > 10)
13457 goto invalid;
13458
13459 depth++;
13460
13461 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
13462 {
13463 case Lisp_String:
13464 {
13465 /* A string: output it and check for %-constructs within it. */
13466 unsigned char c;
13467 unsigned char *this = XSTRING (elt)->data;
13468 unsigned char *lisp_string = this;
13469
13470 while ((precision <= 0 || n < precision)
13471 && *this
13472 && (frame_title_ptr
13473 || it->current_x < it->last_visible_x))
13474 {
13475 unsigned char *last = this;
13476
13477 /* Advance to end of string or next format specifier. */
13478 while ((c = *this++) != '\0' && c != '%')
13479 ;
13480
13481 if (this - 1 != last)
13482 {
13483 /* Output to end of string or up to '%'. Field width
13484 is length of string. Don't output more than
13485 PRECISION allows us. */
13486 --this;
13487
13488 prec = chars_in_text (last, this - last);
13489 if (precision > 0 && prec > precision - n)
13490 prec = precision - n;
13491
13492 if (frame_title_ptr)
13493 n += store_frame_title (last, 0, prec);
13494 else
13495 {
13496 int bytepos = last - lisp_string;
13497 int charpos = string_byte_to_char (elt, bytepos);
13498 n += display_string (NULL, elt, Qnil, 0, charpos,
13499 it, 0, prec, 0,
13500 STRING_MULTIBYTE (elt));
13501 }
13502 }
13503 else /* c == '%' */
13504 {
13505 unsigned char *percent_position = this;
13506
13507 /* Get the specified minimum width. Zero means
13508 don't pad. */
13509 field = 0;
13510 while ((c = *this++) >= '0' && c <= '9')
13511 field = field * 10 + c - '0';
13512
13513 /* Don't pad beyond the total padding allowed. */
13514 if (field_width - n > 0 && field > field_width - n)
13515 field = field_width - n;
13516
13517 /* Note that either PRECISION <= 0 or N < PRECISION. */
13518 prec = precision - n;
13519
13520 if (c == 'M')
13521 n += display_mode_element (it, depth, field, prec,
13522 Vglobal_mode_string);
13523 else if (c != 0)
13524 {
13525 int multibyte;
13526 unsigned char *spec
13527 = decode_mode_spec (it->w, c, field, prec, &multibyte);
13528
13529 if (frame_title_ptr)
13530 n += store_frame_title (spec, field, prec);
13531 else
13532 {
13533 int nglyphs_before, bytepos, charpos, nwritten;
13534
13535 nglyphs_before = it->glyph_row->used[TEXT_AREA];
13536 bytepos = percent_position - XSTRING (elt)->data;
13537 charpos = (multibyte
13538 ? string_byte_to_char (elt, bytepos)
13539 : bytepos);
13540 nwritten = display_string (spec, Qnil, elt,
13541 charpos, 0, it,
13542 field, prec, 0,
13543 multibyte);
13544
13545 /* Assign to the glyphs written above the
13546 string where the `%x' came from, position
13547 of the `%'. */
13548 if (nwritten > 0)
13549 {
13550 struct glyph *glyph
13551 = (it->glyph_row->glyphs[TEXT_AREA]
13552 + nglyphs_before);
13553 int i;
13554
13555 for (i = 0; i < nwritten; ++i)
13556 {
13557 glyph[i].object = elt;
13558 glyph[i].charpos = charpos;
13559 }
13560
13561 n += nwritten;
13562 }
13563 }
13564 }
13565 }
13566 }
13567 }
13568 break;
13569
13570 case Lisp_Symbol:
13571 /* A symbol: process the value of the symbol recursively
13572 as if it appeared here directly. Avoid error if symbol void.
13573 Special case: if value of symbol is a string, output the string
13574 literally. */
13575 {
13576 register Lisp_Object tem;
13577 tem = Fboundp (elt);
13578 if (!NILP (tem))
13579 {
13580 tem = Fsymbol_value (elt);
13581 /* If value is a string, output that string literally:
13582 don't check for % within it. */
13583 if (STRINGP (tem))
13584 {
13585 prec = precision - n;
13586 if (frame_title_ptr)
13587 n += store_frame_title (XSTRING (tem)->data, -1, prec);
13588 else
13589 n += display_string (NULL, tem, Qnil, 0, 0, it,
13590 0, prec, 0, STRING_MULTIBYTE (tem));
13591 }
13592 else if (!EQ (tem, elt))
13593 {
13594 /* Give up right away for nil or t. */
13595 elt = tem;
13596 goto tail_recurse;
13597 }
13598 }
13599 }
13600 break;
13601
13602 case Lisp_Cons:
13603 {
13604 register Lisp_Object car, tem;
13605
13606 /* A cons cell: three distinct cases.
13607 If first element is a string or a cons, process all the elements
13608 and effectively concatenate them.
13609 If first element is a negative number, truncate displaying cdr to
13610 at most that many characters. If positive, pad (with spaces)
13611 to at least that many characters.
13612 If first element is a symbol, process the cadr or caddr recursively
13613 according to whether the symbol's value is non-nil or nil. */
13614 car = XCAR (elt);
13615 if (EQ (car, QCeval) && CONSP (XCDR (elt)))
13616 {
13617 /* An element of the form (:eval FORM) means evaluate FORM
13618 and use the result as mode line elements. */
13619 struct gcpro gcpro1;
13620 Lisp_Object spec;
13621
13622 spec = safe_eval (XCAR (XCDR (elt)));
13623 GCPRO1 (spec);
13624 n += display_mode_element (it, depth, field_width - n,
13625 precision - n, spec);
13626 UNGCPRO;
13627 }
13628 else if (SYMBOLP (car))
13629 {
13630 tem = Fboundp (car);
13631 elt = XCDR (elt);
13632 if (!CONSP (elt))
13633 goto invalid;
13634 /* elt is now the cdr, and we know it is a cons cell.
13635 Use its car if CAR has a non-nil value. */
13636 if (!NILP (tem))
13637 {
13638 tem = Fsymbol_value (car);
13639 if (!NILP (tem))
13640 {
13641 elt = XCAR (elt);
13642 goto tail_recurse;
13643 }
13644 }
13645 /* Symbol's value is nil (or symbol is unbound)
13646 Get the cddr of the original list
13647 and if possible find the caddr and use that. */
13648 elt = XCDR (elt);
13649 if (NILP (elt))
13650 break;
13651 else if (!CONSP (elt))
13652 goto invalid;
13653 elt = XCAR (elt);
13654 goto tail_recurse;
13655 }
13656 else if (INTEGERP (car))
13657 {
13658 register int lim = XINT (car);
13659 elt = XCDR (elt);
13660 if (lim < 0)
13661 {
13662 /* Negative int means reduce maximum width. */
13663 if (precision <= 0)
13664 precision = -lim;
13665 else
13666 precision = min (precision, -lim);
13667 }
13668 else if (lim > 0)
13669 {
13670 /* Padding specified. Don't let it be more than
13671 current maximum. */
13672 if (precision > 0)
13673 lim = min (precision, lim);
13674
13675 /* If that's more padding than already wanted, queue it.
13676 But don't reduce padding already specified even if
13677 that is beyond the current truncation point. */
13678 field_width = max (lim, field_width);
13679 }
13680 goto tail_recurse;
13681 }
13682 else if (STRINGP (car) || CONSP (car))
13683 {
13684 register int limit = 50;
13685 /* Limit is to protect against circular lists. */
13686 while (CONSP (elt)
13687 && --limit > 0
13688 && (precision <= 0 || n < precision))
13689 {
13690 n += display_mode_element (it, depth, field_width - n,
13691 precision - n, XCAR (elt));
13692 elt = XCDR (elt);
13693 }
13694 }
13695 }
13696 break;
13697
13698 default:
13699 invalid:
13700 if (frame_title_ptr)
13701 n += store_frame_title ("*invalid*", 0, precision - n);
13702 else
13703 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
13704 precision - n, 0, 0);
13705 return n;
13706 }
13707
13708 /* Pad to FIELD_WIDTH. */
13709 if (field_width > 0 && n < field_width)
13710 {
13711 if (frame_title_ptr)
13712 n += store_frame_title ("", field_width - n, 0);
13713 else
13714 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
13715 0, 0, 0);
13716 }
13717
13718 return n;
13719 }
13720
13721
13722 /* Write a null-terminated, right justified decimal representation of
13723 the positive integer D to BUF using a minimal field width WIDTH. */
13724
13725 static void
13726 pint2str (buf, width, d)
13727 register char *buf;
13728 register int width;
13729 register int d;
13730 {
13731 register char *p = buf;
13732
13733 if (d <= 0)
13734 *p++ = '0';
13735 else
13736 {
13737 while (d > 0)
13738 {
13739 *p++ = d % 10 + '0';
13740 d /= 10;
13741 }
13742 }
13743
13744 for (width -= (int) (p - buf); width > 0; --width)
13745 *p++ = ' ';
13746 *p-- = '\0';
13747 while (p > buf)
13748 {
13749 d = *buf;
13750 *buf++ = *p;
13751 *p-- = d;
13752 }
13753 }
13754
13755 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
13756 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
13757 type of CODING_SYSTEM. Return updated pointer into BUF. */
13758
13759 static unsigned char invalid_eol_type[] = "(*invalid*)";
13760
13761 static char *
13762 decode_mode_spec_coding (coding_system, buf, eol_flag)
13763 Lisp_Object coding_system;
13764 register char *buf;
13765 int eol_flag;
13766 {
13767 Lisp_Object val;
13768 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
13769 unsigned char *eol_str;
13770 int eol_str_len;
13771 /* The EOL conversion we are using. */
13772 Lisp_Object eoltype;
13773
13774 val = Fget (coding_system, Qcoding_system);
13775 eoltype = Qnil;
13776
13777 if (!VECTORP (val)) /* Not yet decided. */
13778 {
13779 if (multibyte)
13780 *buf++ = '-';
13781 if (eol_flag)
13782 eoltype = eol_mnemonic_undecided;
13783 /* Don't mention EOL conversion if it isn't decided. */
13784 }
13785 else
13786 {
13787 Lisp_Object eolvalue;
13788
13789 eolvalue = Fget (coding_system, Qeol_type);
13790
13791 if (multibyte)
13792 *buf++ = XFASTINT (AREF (val, 1));
13793
13794 if (eol_flag)
13795 {
13796 /* The EOL conversion that is normal on this system. */
13797
13798 if (NILP (eolvalue)) /* Not yet decided. */
13799 eoltype = eol_mnemonic_undecided;
13800 else if (VECTORP (eolvalue)) /* Not yet decided. */
13801 eoltype = eol_mnemonic_undecided;
13802 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
13803 eoltype = (XFASTINT (eolvalue) == 0
13804 ? eol_mnemonic_unix
13805 : (XFASTINT (eolvalue) == 1
13806 ? eol_mnemonic_dos : eol_mnemonic_mac));
13807 }
13808 }
13809
13810 if (eol_flag)
13811 {
13812 /* Mention the EOL conversion if it is not the usual one. */
13813 if (STRINGP (eoltype))
13814 {
13815 eol_str = XSTRING (eoltype)->data;
13816 eol_str_len = XSTRING (eoltype)->size;
13817 }
13818 else if (INTEGERP (eoltype)
13819 && CHAR_VALID_P (XINT (eoltype), 0))
13820 {
13821 eol_str = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
13822 eol_str_len = CHAR_STRING (XINT (eoltype), eol_str);
13823 }
13824 else
13825 {
13826 eol_str = invalid_eol_type;
13827 eol_str_len = sizeof (invalid_eol_type) - 1;
13828 }
13829 bcopy (eol_str, buf, eol_str_len);
13830 buf += eol_str_len;
13831 }
13832
13833 return buf;
13834 }
13835
13836 /* Return a string for the output of a mode line %-spec for window W,
13837 generated by character C. PRECISION >= 0 means don't return a
13838 string longer than that value. FIELD_WIDTH > 0 means pad the
13839 string returned with spaces to that value. Return 1 in *MULTIBYTE
13840 if the result is multibyte text. */
13841
13842 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
13843
13844 static char *
13845 decode_mode_spec (w, c, field_width, precision, multibyte)
13846 struct window *w;
13847 register int c;
13848 int field_width, precision;
13849 int *multibyte;
13850 {
13851 Lisp_Object obj;
13852 struct frame *f = XFRAME (WINDOW_FRAME (w));
13853 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
13854 struct buffer *b = XBUFFER (w->buffer);
13855
13856 obj = Qnil;
13857 *multibyte = 0;
13858
13859 switch (c)
13860 {
13861 case '*':
13862 if (!NILP (b->read_only))
13863 return "%";
13864 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
13865 return "*";
13866 return "-";
13867
13868 case '+':
13869 /* This differs from %* only for a modified read-only buffer. */
13870 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
13871 return "*";
13872 if (!NILP (b->read_only))
13873 return "%";
13874 return "-";
13875
13876 case '&':
13877 /* This differs from %* in ignoring read-only-ness. */
13878 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
13879 return "*";
13880 return "-";
13881
13882 case '%':
13883 return "%";
13884
13885 case '[':
13886 {
13887 int i;
13888 char *p;
13889
13890 if (command_loop_level > 5)
13891 return "[[[... ";
13892 p = decode_mode_spec_buf;
13893 for (i = 0; i < command_loop_level; i++)
13894 *p++ = '[';
13895 *p = 0;
13896 return decode_mode_spec_buf;
13897 }
13898
13899 case ']':
13900 {
13901 int i;
13902 char *p;
13903
13904 if (command_loop_level > 5)
13905 return " ...]]]";
13906 p = decode_mode_spec_buf;
13907 for (i = 0; i < command_loop_level; i++)
13908 *p++ = ']';
13909 *p = 0;
13910 return decode_mode_spec_buf;
13911 }
13912
13913 case '-':
13914 {
13915 register int i;
13916
13917 /* Let lots_of_dashes be a string of infinite length. */
13918 if (field_width <= 0
13919 || field_width > sizeof (lots_of_dashes))
13920 {
13921 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
13922 decode_mode_spec_buf[i] = '-';
13923 decode_mode_spec_buf[i] = '\0';
13924 return decode_mode_spec_buf;
13925 }
13926 else
13927 return lots_of_dashes;
13928 }
13929
13930 case 'b':
13931 obj = b->name;
13932 break;
13933
13934 case 'c':
13935 {
13936 int col = current_column ();
13937 w->column_number_displayed = make_number (col);
13938 pint2str (decode_mode_spec_buf, field_width, col);
13939 return decode_mode_spec_buf;
13940 }
13941
13942 case 'F':
13943 /* %F displays the frame name. */
13944 if (!NILP (f->title))
13945 return (char *) XSTRING (f->title)->data;
13946 if (f->explicit_name || ! FRAME_WINDOW_P (f))
13947 return (char *) XSTRING (f->name)->data;
13948 return "Emacs";
13949
13950 case 'f':
13951 obj = b->filename;
13952 break;
13953
13954 case 'l':
13955 {
13956 int startpos = XMARKER (w->start)->charpos;
13957 int startpos_byte = marker_byte_position (w->start);
13958 int line, linepos, linepos_byte, topline;
13959 int nlines, junk;
13960 int height = XFASTINT (w->height);
13961
13962 /* If we decided that this buffer isn't suitable for line numbers,
13963 don't forget that too fast. */
13964 if (EQ (w->base_line_pos, w->buffer))
13965 goto no_value;
13966 /* But do forget it, if the window shows a different buffer now. */
13967 else if (BUFFERP (w->base_line_pos))
13968 w->base_line_pos = Qnil;
13969
13970 /* If the buffer is very big, don't waste time. */
13971 if (INTEGERP (Vline_number_display_limit)
13972 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
13973 {
13974 w->base_line_pos = Qnil;
13975 w->base_line_number = Qnil;
13976 goto no_value;
13977 }
13978
13979 if (!NILP (w->base_line_number)
13980 && !NILP (w->base_line_pos)
13981 && XFASTINT (w->base_line_pos) <= startpos)
13982 {
13983 line = XFASTINT (w->base_line_number);
13984 linepos = XFASTINT (w->base_line_pos);
13985 linepos_byte = buf_charpos_to_bytepos (b, linepos);
13986 }
13987 else
13988 {
13989 line = 1;
13990 linepos = BUF_BEGV (b);
13991 linepos_byte = BUF_BEGV_BYTE (b);
13992 }
13993
13994 /* Count lines from base line to window start position. */
13995 nlines = display_count_lines (linepos, linepos_byte,
13996 startpos_byte,
13997 startpos, &junk);
13998
13999 topline = nlines + line;
14000
14001 /* Determine a new base line, if the old one is too close
14002 or too far away, or if we did not have one.
14003 "Too close" means it's plausible a scroll-down would
14004 go back past it. */
14005 if (startpos == BUF_BEGV (b))
14006 {
14007 w->base_line_number = make_number (topline);
14008 w->base_line_pos = make_number (BUF_BEGV (b));
14009 }
14010 else if (nlines < height + 25 || nlines > height * 3 + 50
14011 || linepos == BUF_BEGV (b))
14012 {
14013 int limit = BUF_BEGV (b);
14014 int limit_byte = BUF_BEGV_BYTE (b);
14015 int position;
14016 int distance = (height * 2 + 30) * line_number_display_limit_width;
14017
14018 if (startpos - distance > limit)
14019 {
14020 limit = startpos - distance;
14021 limit_byte = CHAR_TO_BYTE (limit);
14022 }
14023
14024 nlines = display_count_lines (startpos, startpos_byte,
14025 limit_byte,
14026 - (height * 2 + 30),
14027 &position);
14028 /* If we couldn't find the lines we wanted within
14029 line_number_display_limit_width chars per line,
14030 give up on line numbers for this window. */
14031 if (position == limit_byte && limit == startpos - distance)
14032 {
14033 w->base_line_pos = w->buffer;
14034 w->base_line_number = Qnil;
14035 goto no_value;
14036 }
14037
14038 w->base_line_number = make_number (topline - nlines);
14039 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
14040 }
14041
14042 /* Now count lines from the start pos to point. */
14043 nlines = display_count_lines (startpos, startpos_byte,
14044 PT_BYTE, PT, &junk);
14045
14046 /* Record that we did display the line number. */
14047 line_number_displayed = 1;
14048
14049 /* Make the string to show. */
14050 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
14051 return decode_mode_spec_buf;
14052 no_value:
14053 {
14054 char* p = decode_mode_spec_buf;
14055 int pad = field_width - 2;
14056 while (pad-- > 0)
14057 *p++ = ' ';
14058 *p++ = '?';
14059 *p++ = '?';
14060 *p = '\0';
14061 return decode_mode_spec_buf;
14062 }
14063 }
14064 break;
14065
14066 case 'm':
14067 obj = b->mode_name;
14068 break;
14069
14070 case 'n':
14071 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
14072 return " Narrow";
14073 break;
14074
14075 case 'p':
14076 {
14077 int pos = marker_position (w->start);
14078 int total = BUF_ZV (b) - BUF_BEGV (b);
14079
14080 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
14081 {
14082 if (pos <= BUF_BEGV (b))
14083 return "All";
14084 else
14085 return "Bottom";
14086 }
14087 else if (pos <= BUF_BEGV (b))
14088 return "Top";
14089 else
14090 {
14091 if (total > 1000000)
14092 /* Do it differently for a large value, to avoid overflow. */
14093 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
14094 else
14095 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
14096 /* We can't normally display a 3-digit number,
14097 so get us a 2-digit number that is close. */
14098 if (total == 100)
14099 total = 99;
14100 sprintf (decode_mode_spec_buf, "%2d%%", total);
14101 return decode_mode_spec_buf;
14102 }
14103 }
14104
14105 /* Display percentage of size above the bottom of the screen. */
14106 case 'P':
14107 {
14108 int toppos = marker_position (w->start);
14109 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
14110 int total = BUF_ZV (b) - BUF_BEGV (b);
14111
14112 if (botpos >= BUF_ZV (b))
14113 {
14114 if (toppos <= BUF_BEGV (b))
14115 return "All";
14116 else
14117 return "Bottom";
14118 }
14119 else
14120 {
14121 if (total > 1000000)
14122 /* Do it differently for a large value, to avoid overflow. */
14123 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
14124 else
14125 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
14126 /* We can't normally display a 3-digit number,
14127 so get us a 2-digit number that is close. */
14128 if (total == 100)
14129 total = 99;
14130 if (toppos <= BUF_BEGV (b))
14131 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
14132 else
14133 sprintf (decode_mode_spec_buf, "%2d%%", total);
14134 return decode_mode_spec_buf;
14135 }
14136 }
14137
14138 case 's':
14139 /* status of process */
14140 obj = Fget_buffer_process (w->buffer);
14141 if (NILP (obj))
14142 return "no process";
14143 #ifdef subprocesses
14144 obj = Fsymbol_name (Fprocess_status (obj));
14145 #endif
14146 break;
14147
14148 case 't': /* indicate TEXT or BINARY */
14149 #ifdef MODE_LINE_BINARY_TEXT
14150 return MODE_LINE_BINARY_TEXT (b);
14151 #else
14152 return "T";
14153 #endif
14154
14155 case 'z':
14156 /* coding-system (not including end-of-line format) */
14157 case 'Z':
14158 /* coding-system (including end-of-line type) */
14159 {
14160 int eol_flag = (c == 'Z');
14161 char *p = decode_mode_spec_buf;
14162
14163 if (! FRAME_WINDOW_P (f))
14164 {
14165 /* No need to mention EOL here--the terminal never needs
14166 to do EOL conversion. */
14167 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
14168 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
14169 }
14170 p = decode_mode_spec_coding (b->buffer_file_coding_system,
14171 p, eol_flag);
14172
14173 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
14174 #ifdef subprocesses
14175 obj = Fget_buffer_process (Fcurrent_buffer ());
14176 if (PROCESSP (obj))
14177 {
14178 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
14179 p, eol_flag);
14180 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
14181 p, eol_flag);
14182 }
14183 #endif /* subprocesses */
14184 #endif /* 0 */
14185 *p = 0;
14186 return decode_mode_spec_buf;
14187 }
14188 }
14189
14190 if (STRINGP (obj))
14191 {
14192 *multibyte = STRING_MULTIBYTE (obj);
14193 return (char *) XSTRING (obj)->data;
14194 }
14195 else
14196 return "";
14197 }
14198
14199
14200 /* Count up to COUNT lines starting from START / START_BYTE.
14201 But don't go beyond LIMIT_BYTE.
14202 Return the number of lines thus found (always nonnegative).
14203
14204 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
14205
14206 static int
14207 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
14208 int start, start_byte, limit_byte, count;
14209 int *byte_pos_ptr;
14210 {
14211 register unsigned char *cursor;
14212 unsigned char *base;
14213
14214 register int ceiling;
14215 register unsigned char *ceiling_addr;
14216 int orig_count = count;
14217
14218 /* If we are not in selective display mode,
14219 check only for newlines. */
14220 int selective_display = (!NILP (current_buffer->selective_display)
14221 && !INTEGERP (current_buffer->selective_display));
14222
14223 if (count > 0)
14224 {
14225 while (start_byte < limit_byte)
14226 {
14227 ceiling = BUFFER_CEILING_OF (start_byte);
14228 ceiling = min (limit_byte - 1, ceiling);
14229 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
14230 base = (cursor = BYTE_POS_ADDR (start_byte));
14231 while (1)
14232 {
14233 if (selective_display)
14234 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
14235 ;
14236 else
14237 while (*cursor != '\n' && ++cursor != ceiling_addr)
14238 ;
14239
14240 if (cursor != ceiling_addr)
14241 {
14242 if (--count == 0)
14243 {
14244 start_byte += cursor - base + 1;
14245 *byte_pos_ptr = start_byte;
14246 return orig_count;
14247 }
14248 else
14249 if (++cursor == ceiling_addr)
14250 break;
14251 }
14252 else
14253 break;
14254 }
14255 start_byte += cursor - base;
14256 }
14257 }
14258 else
14259 {
14260 while (start_byte > limit_byte)
14261 {
14262 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
14263 ceiling = max (limit_byte, ceiling);
14264 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
14265 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
14266 while (1)
14267 {
14268 if (selective_display)
14269 while (--cursor != ceiling_addr
14270 && *cursor != '\n' && *cursor != 015)
14271 ;
14272 else
14273 while (--cursor != ceiling_addr && *cursor != '\n')
14274 ;
14275
14276 if (cursor != ceiling_addr)
14277 {
14278 if (++count == 0)
14279 {
14280 start_byte += cursor - base + 1;
14281 *byte_pos_ptr = start_byte;
14282 /* When scanning backwards, we should
14283 not count the newline posterior to which we stop. */
14284 return - orig_count - 1;
14285 }
14286 }
14287 else
14288 break;
14289 }
14290 /* Here we add 1 to compensate for the last decrement
14291 of CURSOR, which took it past the valid range. */
14292 start_byte += cursor - base + 1;
14293 }
14294 }
14295
14296 *byte_pos_ptr = limit_byte;
14297
14298 if (count < 0)
14299 return - orig_count + count;
14300 return orig_count - count;
14301
14302 }
14303
14304
14305 \f
14306 /***********************************************************************
14307 Displaying strings
14308 ***********************************************************************/
14309
14310 /* Display a NUL-terminated string, starting with index START.
14311
14312 If STRING is non-null, display that C string. Otherwise, the Lisp
14313 string LISP_STRING is displayed.
14314
14315 If FACE_STRING is not nil, FACE_STRING_POS is a position in
14316 FACE_STRING. Display STRING or LISP_STRING with the face at
14317 FACE_STRING_POS in FACE_STRING:
14318
14319 Display the string in the environment given by IT, but use the
14320 standard display table, temporarily.
14321
14322 FIELD_WIDTH is the minimum number of output glyphs to produce.
14323 If STRING has fewer characters than FIELD_WIDTH, pad to the right
14324 with spaces. If STRING has more characters, more than FIELD_WIDTH
14325 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
14326
14327 PRECISION is the maximum number of characters to output from
14328 STRING. PRECISION < 0 means don't truncate the string.
14329
14330 This is roughly equivalent to printf format specifiers:
14331
14332 FIELD_WIDTH PRECISION PRINTF
14333 ----------------------------------------
14334 -1 -1 %s
14335 -1 10 %.10s
14336 10 -1 %10s
14337 20 10 %20.10s
14338
14339 MULTIBYTE zero means do not display multibyte chars, > 0 means do
14340 display them, and < 0 means obey the current buffer's value of
14341 enable_multibyte_characters.
14342
14343 Value is the number of glyphs produced. */
14344
14345 static int
14346 display_string (string, lisp_string, face_string, face_string_pos,
14347 start, it, field_width, precision, max_x, multibyte)
14348 unsigned char *string;
14349 Lisp_Object lisp_string;
14350 Lisp_Object face_string;
14351 int face_string_pos;
14352 int start;
14353 struct it *it;
14354 int field_width, precision, max_x;
14355 int multibyte;
14356 {
14357 int hpos_at_start = it->hpos;
14358 int saved_face_id = it->face_id;
14359 struct glyph_row *row = it->glyph_row;
14360
14361 /* Initialize the iterator IT for iteration over STRING beginning
14362 with index START. */
14363 reseat_to_string (it, string, lisp_string, start,
14364 precision, field_width, multibyte);
14365
14366 /* If displaying STRING, set up the face of the iterator
14367 from LISP_STRING, if that's given. */
14368 if (STRINGP (face_string))
14369 {
14370 int endptr;
14371 struct face *face;
14372
14373 it->face_id
14374 = face_at_string_position (it->w, face_string, face_string_pos,
14375 0, it->region_beg_charpos,
14376 it->region_end_charpos,
14377 &endptr, it->base_face_id, 0);
14378 face = FACE_FROM_ID (it->f, it->face_id);
14379 it->face_box_p = face->box != FACE_NO_BOX;
14380 }
14381
14382 /* Set max_x to the maximum allowed X position. Don't let it go
14383 beyond the right edge of the window. */
14384 if (max_x <= 0)
14385 max_x = it->last_visible_x;
14386 else
14387 max_x = min (max_x, it->last_visible_x);
14388
14389 /* Skip over display elements that are not visible. because IT->w is
14390 hscrolled. */
14391 if (it->current_x < it->first_visible_x)
14392 move_it_in_display_line_to (it, 100000, it->first_visible_x,
14393 MOVE_TO_POS | MOVE_TO_X);
14394
14395 row->ascent = it->max_ascent;
14396 row->height = it->max_ascent + it->max_descent;
14397 row->phys_ascent = it->max_phys_ascent;
14398 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14399
14400 /* This condition is for the case that we are called with current_x
14401 past last_visible_x. */
14402 while (it->current_x < max_x)
14403 {
14404 int x_before, x, n_glyphs_before, i, nglyphs;
14405
14406 /* Get the next display element. */
14407 if (!get_next_display_element (it))
14408 break;
14409
14410 /* Produce glyphs. */
14411 x_before = it->current_x;
14412 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
14413 PRODUCE_GLYPHS (it);
14414
14415 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
14416 i = 0;
14417 x = x_before;
14418 while (i < nglyphs)
14419 {
14420 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
14421
14422 if (!it->truncate_lines_p
14423 && x + glyph->pixel_width > max_x)
14424 {
14425 /* End of continued line or max_x reached. */
14426 if (CHAR_GLYPH_PADDING_P (*glyph))
14427 {
14428 /* A wide character is unbreakable. */
14429 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
14430 it->current_x = x_before;
14431 }
14432 else
14433 {
14434 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
14435 it->current_x = x;
14436 }
14437 break;
14438 }
14439 else if (x + glyph->pixel_width > it->first_visible_x)
14440 {
14441 /* Glyph is at least partially visible. */
14442 ++it->hpos;
14443 if (x < it->first_visible_x)
14444 it->glyph_row->x = x - it->first_visible_x;
14445 }
14446 else
14447 {
14448 /* Glyph is off the left margin of the display area.
14449 Should not happen. */
14450 abort ();
14451 }
14452
14453 row->ascent = max (row->ascent, it->max_ascent);
14454 row->height = max (row->height, it->max_ascent + it->max_descent);
14455 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14456 row->phys_height = max (row->phys_height,
14457 it->max_phys_ascent + it->max_phys_descent);
14458 x += glyph->pixel_width;
14459 ++i;
14460 }
14461
14462 /* Stop if max_x reached. */
14463 if (i < nglyphs)
14464 break;
14465
14466 /* Stop at line ends. */
14467 if (ITERATOR_AT_END_OF_LINE_P (it))
14468 {
14469 it->continuation_lines_width = 0;
14470 break;
14471 }
14472
14473 set_iterator_to_next (it, 1);
14474
14475 /* Stop if truncating at the right edge. */
14476 if (it->truncate_lines_p
14477 && it->current_x >= it->last_visible_x)
14478 {
14479 /* Add truncation mark, but don't do it if the line is
14480 truncated at a padding space. */
14481 if (IT_CHARPOS (*it) < it->string_nchars)
14482 {
14483 if (!FRAME_WINDOW_P (it->f))
14484 {
14485 int i, n;
14486
14487 if (it->current_x > it->last_visible_x)
14488 {
14489 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
14490 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
14491 break;
14492 for (n = row->used[TEXT_AREA]; i < n; ++i)
14493 {
14494 row->used[TEXT_AREA] = i;
14495 produce_special_glyphs (it, IT_TRUNCATION);
14496 }
14497 }
14498 produce_special_glyphs (it, IT_TRUNCATION);
14499 }
14500 it->glyph_row->truncated_on_right_p = 1;
14501 }
14502 break;
14503 }
14504 }
14505
14506 /* Maybe insert a truncation at the left. */
14507 if (it->first_visible_x
14508 && IT_CHARPOS (*it) > 0)
14509 {
14510 if (!FRAME_WINDOW_P (it->f))
14511 insert_left_trunc_glyphs (it);
14512 it->glyph_row->truncated_on_left_p = 1;
14513 }
14514
14515 it->face_id = saved_face_id;
14516
14517 /* Value is number of columns displayed. */
14518 return it->hpos - hpos_at_start;
14519 }
14520
14521
14522 \f
14523 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
14524 appears as an element of LIST or as the car of an element of LIST.
14525 If PROPVAL is a list, compare each element against LIST in that
14526 way, and return 1/2 if any element of PROPVAL is found in LIST.
14527 Otherwise return 0. This function cannot quit.
14528 The return value is 2 if the text is invisible but with an ellipsis
14529 and 1 if it's invisible and without an ellipsis. */
14530
14531 int
14532 invisible_p (propval, list)
14533 register Lisp_Object propval;
14534 Lisp_Object list;
14535 {
14536 register Lisp_Object tail, proptail;
14537
14538 for (tail = list; CONSP (tail); tail = XCDR (tail))
14539 {
14540 register Lisp_Object tem;
14541 tem = XCAR (tail);
14542 if (EQ (propval, tem))
14543 return 1;
14544 if (CONSP (tem) && EQ (propval, XCAR (tem)))
14545 return NILP (XCDR (tem)) ? 1 : 2;
14546 }
14547
14548 if (CONSP (propval))
14549 {
14550 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
14551 {
14552 Lisp_Object propelt;
14553 propelt = XCAR (proptail);
14554 for (tail = list; CONSP (tail); tail = XCDR (tail))
14555 {
14556 register Lisp_Object tem;
14557 tem = XCAR (tail);
14558 if (EQ (propelt, tem))
14559 return 1;
14560 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
14561 return NILP (XCDR (tem)) ? 1 : 2;
14562 }
14563 }
14564 }
14565
14566 return 0;
14567 }
14568
14569 \f
14570 /***********************************************************************
14571 Initialization
14572 ***********************************************************************/
14573
14574 void
14575 syms_of_xdisp ()
14576 {
14577 Vwith_echo_area_save_vector = Qnil;
14578 staticpro (&Vwith_echo_area_save_vector);
14579
14580 Vmessage_stack = Qnil;
14581 staticpro (&Vmessage_stack);
14582
14583 Qinhibit_redisplay = intern ("inhibit-redisplay");
14584 staticpro (&Qinhibit_redisplay);
14585
14586 #if GLYPH_DEBUG
14587 defsubr (&Sdump_glyph_matrix);
14588 defsubr (&Sdump_glyph_row);
14589 defsubr (&Sdump_tool_bar_row);
14590 defsubr (&Strace_redisplay);
14591 defsubr (&Strace_to_stderr);
14592 #endif
14593 #ifdef HAVE_WINDOW_SYSTEM
14594 defsubr (&Stool_bar_lines_needed);
14595 #endif
14596
14597 staticpro (&Qmenu_bar_update_hook);
14598 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
14599
14600 staticpro (&Qoverriding_terminal_local_map);
14601 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
14602
14603 staticpro (&Qoverriding_local_map);
14604 Qoverriding_local_map = intern ("overriding-local-map");
14605
14606 staticpro (&Qwindow_scroll_functions);
14607 Qwindow_scroll_functions = intern ("window-scroll-functions");
14608
14609 staticpro (&Qredisplay_end_trigger_functions);
14610 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
14611
14612 staticpro (&Qinhibit_point_motion_hooks);
14613 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
14614
14615 QCdata = intern (":data");
14616 staticpro (&QCdata);
14617 Qdisplay = intern ("display");
14618 staticpro (&Qdisplay);
14619 Qspace_width = intern ("space-width");
14620 staticpro (&Qspace_width);
14621 Qraise = intern ("raise");
14622 staticpro (&Qraise);
14623 Qspace = intern ("space");
14624 staticpro (&Qspace);
14625 Qmargin = intern ("margin");
14626 staticpro (&Qmargin);
14627 Qleft_margin = intern ("left-margin");
14628 staticpro (&Qleft_margin);
14629 Qright_margin = intern ("right-margin");
14630 staticpro (&Qright_margin);
14631 Qalign_to = intern ("align-to");
14632 staticpro (&Qalign_to);
14633 QCalign_to = intern (":align-to");
14634 staticpro (&QCalign_to);
14635 Qrelative_width = intern ("relative-width");
14636 staticpro (&Qrelative_width);
14637 QCrelative_width = intern (":relative-width");
14638 staticpro (&QCrelative_width);
14639 QCrelative_height = intern (":relative-height");
14640 staticpro (&QCrelative_height);
14641 QCeval = intern (":eval");
14642 staticpro (&QCeval);
14643 Qwhen = intern ("when");
14644 staticpro (&Qwhen);
14645 QCfile = intern (":file");
14646 staticpro (&QCfile);
14647 Qfontified = intern ("fontified");
14648 staticpro (&Qfontified);
14649 Qfontification_functions = intern ("fontification-functions");
14650 staticpro (&Qfontification_functions);
14651 Qtrailing_whitespace = intern ("trailing-whitespace");
14652 staticpro (&Qtrailing_whitespace);
14653 Qimage = intern ("image");
14654 staticpro (&Qimage);
14655 Qmessage_truncate_lines = intern ("message-truncate-lines");
14656 staticpro (&Qmessage_truncate_lines);
14657 Qcursor_in_non_selected_windows = intern ("cursor-in-non-selected-windows");
14658 staticpro (&Qcursor_in_non_selected_windows);
14659 Qgrow_only = intern ("grow-only");
14660 staticpro (&Qgrow_only);
14661 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
14662 staticpro (&Qinhibit_menubar_update);
14663 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
14664 staticpro (&Qinhibit_eval_during_redisplay);
14665 Qposition = intern ("position");
14666 staticpro (&Qposition);
14667 Qbuffer_position = intern ("buffer-position");
14668 staticpro (&Qbuffer_position);
14669 Qobject = intern ("object");
14670 staticpro (&Qobject);
14671
14672 last_arrow_position = Qnil;
14673 last_arrow_string = Qnil;
14674 staticpro (&last_arrow_position);
14675 staticpro (&last_arrow_string);
14676
14677 echo_buffer[0] = echo_buffer[1] = Qnil;
14678 staticpro (&echo_buffer[0]);
14679 staticpro (&echo_buffer[1]);
14680
14681 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
14682 staticpro (&echo_area_buffer[0]);
14683 staticpro (&echo_area_buffer[1]);
14684
14685 Vmessages_buffer_name = build_string ("*Messages*");
14686 staticpro (&Vmessages_buffer_name);
14687
14688 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
14689 doc: /* Non-nil means highlight trailing whitespace.
14690 The face used for trailing whitespace is `trailing-whitespace'. */);
14691 Vshow_trailing_whitespace = Qnil;
14692
14693 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
14694 doc: /* Non-nil means don't actually do any redisplay.
14695 This is used for internal purposes. */);
14696 Vinhibit_redisplay = Qnil;
14697
14698 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
14699 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
14700 Vglobal_mode_string = Qnil;
14701
14702 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
14703 doc: /* Marker for where to display an arrow on top of the buffer text.
14704 This must be the beginning of a line in order to work.
14705 See also `overlay-arrow-string'. */);
14706 Voverlay_arrow_position = Qnil;
14707
14708 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
14709 doc: /* String to display as an arrow. See also `overlay-arrow-position'. */);
14710 Voverlay_arrow_string = Qnil;
14711
14712 DEFVAR_INT ("scroll-step", &scroll_step,
14713 doc: /* *The number of lines to try scrolling a window by when point moves out.
14714 If that fails to bring point back on frame, point is centered instead.
14715 If this is zero, point is always centered after it moves off frame.
14716 If you want scrolling to always be a line at a time, you should set
14717 `scroll-conservatively' to a large value rather than set this to 1. */);
14718
14719 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
14720 doc: /* *Scroll up to this many lines, to bring point back on screen.
14721 A value of zero means to scroll the text to center point vertically
14722 in the window. */);
14723 scroll_conservatively = 0;
14724
14725 DEFVAR_INT ("scroll-margin", &scroll_margin,
14726 doc: /* *Number of lines of margin at the top and bottom of a window.
14727 Recenter the window whenever point gets within this many lines
14728 of the top or bottom of the window. */);
14729 scroll_margin = 0;
14730
14731 #if GLYPH_DEBUG
14732 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
14733 #endif
14734
14735 DEFVAR_BOOL ("truncate-partial-width-windows",
14736 &truncate_partial_width_windows,
14737 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
14738 truncate_partial_width_windows = 1;
14739
14740 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
14741 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
14742 Any other value means to use the appropriate face, `mode-line',
14743 `header-line', or `menu' respectively.
14744
14745 This variable is deprecated; please change the above faces instead. */);
14746 mode_line_inverse_video = 1;
14747
14748 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
14749 doc: /* *Maximum buffer size for which line number should be displayed.
14750 If the buffer is bigger than this, the line number does not appear
14751 in the mode line. A value of nil means no limit. */);
14752 Vline_number_display_limit = Qnil;
14753
14754 DEFVAR_INT ("line-number-display-limit-width",
14755 &line_number_display_limit_width,
14756 doc: /* *Maximum line width (in characters) for line number display.
14757 If the average length of the lines near point is bigger than this, then the
14758 line number may be omitted from the mode line. */);
14759 line_number_display_limit_width = 200;
14760
14761 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
14762 doc: /* *Non-nil means highlight region even in nonselected windows. */);
14763 highlight_nonselected_windows = 0;
14764
14765 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
14766 doc: /* Non-nil if more than one frame is visible on this display.
14767 Minibuffer-only frames don't count, but iconified frames do.
14768 This variable is not guaranteed to be accurate except while processing
14769 `frame-title-format' and `icon-title-format'. */);
14770
14771 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
14772 doc: /* Template for displaying the title bar of visible frames.
14773 \(Assuming the window manager supports this feature.)
14774 This variable has the same structure as `mode-line-format' (which see),
14775 and is used only on frames for which no explicit name has been set
14776 \(see `modify-frame-parameters'). */);
14777 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
14778 doc: /* Template for displaying the title bar of an iconified frame.
14779 \(Assuming the window manager supports this feature.)
14780 This variable has the same structure as `mode-line-format' (which see),
14781 and is used only on frames for which no explicit name has been set
14782 \(see `modify-frame-parameters'). */);
14783 Vicon_title_format
14784 = Vframe_title_format
14785 = Fcons (intern ("multiple-frames"),
14786 Fcons (build_string ("%b"),
14787 Fcons (Fcons (empty_string,
14788 Fcons (intern ("invocation-name"),
14789 Fcons (build_string ("@"),
14790 Fcons (intern ("system-name"),
14791 Qnil)))),
14792 Qnil)));
14793
14794 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
14795 doc: /* Maximum number of lines to keep in the message log buffer.
14796 If nil, disable message logging. If t, log messages but don't truncate
14797 the buffer when it becomes large. */);
14798 Vmessage_log_max = make_number (50);
14799
14800 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
14801 doc: /* Functions called before redisplay, if window sizes have changed.
14802 The value should be a list of functions that take one argument.
14803 Just before redisplay, for each frame, if any of its windows have changed
14804 size since the last redisplay, or have been split or deleted,
14805 all the functions in the list are called, with the frame as argument. */);
14806 Vwindow_size_change_functions = Qnil;
14807
14808 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
14809 doc: /* List of Functions to call before redisplaying a window with scrolling.
14810 Each function is called with two arguments, the window
14811 and its new display-start position. Note that the value of `window-end'
14812 is not valid when these functions are called. */);
14813 Vwindow_scroll_functions = Qnil;
14814
14815 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
14816 doc: /* *Non-nil means automatically resize tool-bars.
14817 This increases a tool-bar's height if not all tool-bar items are visible.
14818 It decreases a tool-bar's height when it would display blank lines
14819 otherwise. */);
14820 auto_resize_tool_bars_p = 1;
14821
14822 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
14823 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
14824 auto_raise_tool_bar_buttons_p = 1;
14825
14826 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
14827 doc: /* *Margin around tool-bar buttons in pixels.
14828 If an integer, use that for both horizontal and vertical margins.
14829 Otherwise, value should be a pair of integers `(HORZ : VERT)' with
14830 HORZ specifying the horizontal margin, and VERT specifying the
14831 vertical margin. */);
14832 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
14833
14834 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
14835 doc: /* Relief thickness of tool-bar buttons. */);
14836 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
14837
14838 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
14839 doc: /* List of functions to call to fontify regions of text.
14840 Each function is called with one argument POS. Functions must
14841 fontify a region starting at POS in the current buffer, and give
14842 fontified regions the property `fontified'. */);
14843 Vfontification_functions = Qnil;
14844 Fmake_variable_buffer_local (Qfontification_functions);
14845
14846 DEFVAR_BOOL ("unibyte-display-via-language-environment",
14847 &unibyte_display_via_language_environment,
14848 doc: /* *Non-nil means display unibyte text according to language environment.
14849 Specifically this means that unibyte non-ASCII characters
14850 are displayed by converting them to the equivalent multibyte characters
14851 according to the current language environment. As a result, they are
14852 displayed according to the current fontset. */);
14853 unibyte_display_via_language_environment = 0;
14854
14855 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
14856 doc: /* *Maximum height for resizing mini-windows.
14857 If a float, it specifies a fraction of the mini-window frame's height.
14858 If an integer, it specifies a number of lines. */);
14859 Vmax_mini_window_height = make_float (0.25);
14860
14861 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
14862 doc: /* *How to resize mini-windows.
14863 A value of nil means don't automatically resize mini-windows.
14864 A value of t means resize them to fit the text displayed in them.
14865 A value of `grow-only', the default, means let mini-windows grow
14866 only, until their display becomes empty, at which point the windows
14867 go back to their normal size. */);
14868 Vresize_mini_windows = Qgrow_only;
14869
14870 DEFVAR_BOOL ("cursor-in-non-selected-windows",
14871 &cursor_in_non_selected_windows,
14872 doc: /* *Non-nil means display a hollow cursor in non-selected windows.
14873 Nil means don't display a cursor there. */);
14874 cursor_in_non_selected_windows = 1;
14875
14876 DEFVAR_BOOL ("automatic-hscrolling", &automatic_hscrolling_p,
14877 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
14878 automatic_hscrolling_p = 1;
14879
14880 DEFVAR_LISP ("image-types", &Vimage_types,
14881 doc: /* List of supported image types.
14882 Each element of the list is a symbol for a supported image type. */);
14883 Vimage_types = Qnil;
14884
14885 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
14886 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
14887 Bind this around calls to `message' to let it take effect. */);
14888 message_truncate_lines = 0;
14889
14890 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
14891 doc: /* Normal hook run for clicks on menu bar, before displaying a submenu.
14892 Can be used to update submenus whose contents should vary. */);
14893 Vmenu_bar_update_hook = Qnil;
14894
14895 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
14896 doc: /* Non-nil means don't update menu bars. Internal use only. */);
14897 inhibit_menubar_update = 0;
14898
14899 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
14900 doc: /* Non-nil means don't eval Lisp during redisplay. */);
14901 inhibit_eval_during_redisplay = 0;
14902
14903 #if GLYPH_DEBUG
14904 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
14905 doc: /* Inhibit try_window_id display optimization. */);
14906 inhibit_try_window_id = 0;
14907
14908 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
14909 doc: /* Inhibit try_window_reusing display optimization. */);
14910 inhibit_try_window_reusing = 0;
14911
14912 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
14913 doc: /* Inhibit try_cursor_movement display optimization. */);
14914 inhibit_try_cursor_movement = 0;
14915 #endif /* GLYPH_DEBUG */
14916 }
14917
14918
14919 /* Initialize this module when Emacs starts. */
14920
14921 void
14922 init_xdisp ()
14923 {
14924 Lisp_Object root_window;
14925 struct window *mini_w;
14926
14927 current_header_line_height = current_mode_line_height = -1;
14928
14929 CHARPOS (this_line_start_pos) = 0;
14930
14931 mini_w = XWINDOW (minibuf_window);
14932 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
14933
14934 if (!noninteractive)
14935 {
14936 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
14937 int i;
14938
14939 XWINDOW (root_window)->top = make_number (FRAME_TOP_MARGIN (f));
14940 set_window_height (root_window,
14941 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
14942 0);
14943 mini_w->top = make_number (FRAME_HEIGHT (f) - 1);
14944 set_window_height (minibuf_window, 1, 0);
14945
14946 XWINDOW (root_window)->width = make_number (FRAME_WIDTH (f));
14947 mini_w->width = make_number (FRAME_WIDTH (f));
14948
14949 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
14950 scratch_glyph_row.glyphs[TEXT_AREA + 1]
14951 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
14952
14953 /* The default ellipsis glyphs `...'. */
14954 for (i = 0; i < 3; ++i)
14955 default_invis_vector[i] = make_number ('.');
14956 }
14957
14958 #ifdef HAVE_WINDOW_SYSTEM
14959 {
14960 /* Allocate the buffer for frame titles. */
14961 int size = 100;
14962 frame_title_buf = (char *) xmalloc (size);
14963 frame_title_buf_end = frame_title_buf + size;
14964 frame_title_ptr = NULL;
14965 }
14966 #endif /* HAVE_WINDOW_SYSTEM */
14967
14968 help_echo_showing_p = 0;
14969 }
14970
14971