(tool_bar_item_info): Avoid calling Fget_text_property
[bpt/emacs.git] / 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 int charpos;
7939
7940 /* This function can be called asynchronously, which means we must
7941 exclude any possibility that Fget_text_property signals an
7942 error. */
7943 charpos = min (XSTRING (f->current_tool_bar_string)->size, glyph->charpos);
7944 charpos = max (0, charpos);
7945
7946 /* Get the text property `menu-item' at pos. The value of that
7947 property is the start index of this item's properties in
7948 F->tool_bar_items. */
7949 prop = Fget_text_property (make_number (charpos),
7950 Qmenu_item, f->current_tool_bar_string);
7951 if (INTEGERP (prop))
7952 {
7953 *prop_idx = XINT (prop);
7954 success_p = 1;
7955 }
7956 else
7957 success_p = 0;
7958
7959 return success_p;
7960 }
7961
7962 #endif /* HAVE_WINDOW_SYSTEM */
7963
7964
7965 \f
7966 /************************************************************************
7967 Horizontal scrolling
7968 ************************************************************************/
7969
7970 static int hscroll_window_tree P_ ((Lisp_Object));
7971 static int hscroll_windows P_ ((Lisp_Object));
7972
7973 /* For all leaf windows in the window tree rooted at WINDOW, set their
7974 hscroll value so that PT is (i) visible in the window, and (ii) so
7975 that it is not within a certain margin at the window's left and
7976 right border. Value is non-zero if any window's hscroll has been
7977 changed. */
7978
7979 static int
7980 hscroll_window_tree (window)
7981 Lisp_Object window;
7982 {
7983 int hscrolled_p = 0;
7984
7985 while (WINDOWP (window))
7986 {
7987 struct window *w = XWINDOW (window);
7988
7989 if (WINDOWP (w->hchild))
7990 hscrolled_p |= hscroll_window_tree (w->hchild);
7991 else if (WINDOWP (w->vchild))
7992 hscrolled_p |= hscroll_window_tree (w->vchild);
7993 else if (w->cursor.vpos >= 0)
7994 {
7995 int hscroll_margin, text_area_x, text_area_y;
7996 int text_area_width, text_area_height;
7997 struct glyph_row *current_cursor_row
7998 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
7999 struct glyph_row *desired_cursor_row
8000 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
8001 struct glyph_row *cursor_row
8002 = (desired_cursor_row->enabled_p
8003 ? desired_cursor_row
8004 : current_cursor_row);
8005
8006 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
8007 &text_area_width, &text_area_height);
8008
8009 /* Scroll when cursor is inside this scroll margin. */
8010 /* Shouldn't we export this `5' for customization ? -stef */
8011 hscroll_margin = 5 * CANON_X_UNIT (XFRAME (w->frame));
8012
8013 if ((XFASTINT (w->hscroll)
8014 && w->cursor.x < hscroll_margin)
8015 || (cursor_row->enabled_p
8016 && cursor_row->truncated_on_right_p
8017 && (w->cursor.x > text_area_width - hscroll_margin)))
8018 {
8019 struct it it;
8020 int hscroll;
8021 struct buffer *saved_current_buffer;
8022 int pt;
8023
8024 /* Find point in a display of infinite width. */
8025 saved_current_buffer = current_buffer;
8026 current_buffer = XBUFFER (w->buffer);
8027
8028 if (w == XWINDOW (selected_window))
8029 pt = BUF_PT (current_buffer);
8030 else
8031 {
8032 pt = marker_position (w->pointm);
8033 pt = max (BEGV, pt);
8034 pt = min (ZV, pt);
8035 }
8036
8037 /* Move iterator to pt starting at cursor_row->start in
8038 a line with infinite width. */
8039 init_to_row_start (&it, w, cursor_row);
8040 it.last_visible_x = INFINITY;
8041 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
8042 current_buffer = saved_current_buffer;
8043
8044 /* Center cursor in window. */
8045 hscroll = (max (0, it.current_x - text_area_width / 2)
8046 / CANON_X_UNIT (it.f));
8047 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
8048
8049 /* Don't call Fset_window_hscroll if value hasn't
8050 changed because it will prevent redisplay
8051 optimizations. */
8052 if (XFASTINT (w->hscroll) != hscroll)
8053 {
8054 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
8055 w->hscroll = make_number (hscroll);
8056 hscrolled_p = 1;
8057 }
8058 }
8059 }
8060
8061 window = w->next;
8062 }
8063
8064 /* Value is non-zero if hscroll of any leaf window has been changed. */
8065 return hscrolled_p;
8066 }
8067
8068
8069 /* Set hscroll so that cursor is visible and not inside horizontal
8070 scroll margins for all windows in the tree rooted at WINDOW. See
8071 also hscroll_window_tree above. Value is non-zero if any window's
8072 hscroll has been changed. If it has, desired matrices on the frame
8073 of WINDOW are cleared. */
8074
8075 static int
8076 hscroll_windows (window)
8077 Lisp_Object window;
8078 {
8079 int hscrolled_p;
8080
8081 if (automatic_hscrolling_p)
8082 {
8083 hscrolled_p = hscroll_window_tree (window);
8084 if (hscrolled_p)
8085 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
8086 }
8087 else
8088 hscrolled_p = 0;
8089 return hscrolled_p;
8090 }
8091
8092
8093 \f
8094 /************************************************************************
8095 Redisplay
8096 ************************************************************************/
8097
8098 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
8099 to a non-zero value. This is sometimes handy to have in a debugger
8100 session. */
8101
8102 #if GLYPH_DEBUG
8103
8104 /* First and last unchanged row for try_window_id. */
8105
8106 int debug_first_unchanged_at_end_vpos;
8107 int debug_last_unchanged_at_beg_vpos;
8108
8109 /* Delta vpos and y. */
8110
8111 int debug_dvpos, debug_dy;
8112
8113 /* Delta in characters and bytes for try_window_id. */
8114
8115 int debug_delta, debug_delta_bytes;
8116
8117 /* Values of window_end_pos and window_end_vpos at the end of
8118 try_window_id. */
8119
8120 int debug_end_pos, debug_end_vpos;
8121
8122 /* Append a string to W->desired_matrix->method. FMT is a printf
8123 format string. A1...A9 are a supplement for a variable-length
8124 argument list. If trace_redisplay_p is non-zero also printf the
8125 resulting string to stderr. */
8126
8127 static void
8128 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
8129 struct window *w;
8130 char *fmt;
8131 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
8132 {
8133 char buffer[512];
8134 char *method = w->desired_matrix->method;
8135 int len = strlen (method);
8136 int size = sizeof w->desired_matrix->method;
8137 int remaining = size - len - 1;
8138
8139 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
8140 if (len && remaining)
8141 {
8142 method[len] = '|';
8143 --remaining, ++len;
8144 }
8145
8146 strncpy (method + len, buffer, remaining);
8147
8148 if (trace_redisplay_p)
8149 fprintf (stderr, "%p (%s): %s\n",
8150 w,
8151 ((BUFFERP (w->buffer)
8152 && STRINGP (XBUFFER (w->buffer)->name))
8153 ? (char *) XSTRING (XBUFFER (w->buffer)->name)->data
8154 : "no buffer"),
8155 buffer);
8156 }
8157
8158 #endif /* GLYPH_DEBUG */
8159
8160
8161 /* This counter is used to clear the face cache every once in a while
8162 in redisplay_internal. It is incremented for each redisplay.
8163 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
8164 cleared. */
8165
8166 #define CLEAR_FACE_CACHE_COUNT 10000
8167 static int clear_face_cache_count;
8168
8169 /* Record the previous terminal frame we displayed. */
8170
8171 static struct frame *previous_terminal_frame;
8172
8173 /* Non-zero while redisplay_internal is in progress. */
8174
8175 int redisplaying_p;
8176
8177
8178 /* Value is non-zero if all changes in window W, which displays
8179 current_buffer, are in the text between START and END. START is a
8180 buffer position, END is given as a distance from Z. Used in
8181 redisplay_internal for display optimization. */
8182
8183 static INLINE int
8184 text_outside_line_unchanged_p (w, start, end)
8185 struct window *w;
8186 int start, end;
8187 {
8188 int unchanged_p = 1;
8189
8190 /* If text or overlays have changed, see where. */
8191 if (XFASTINT (w->last_modified) < MODIFF
8192 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8193 {
8194 /* Gap in the line? */
8195 if (GPT < start || Z - GPT < end)
8196 unchanged_p = 0;
8197
8198 /* Changes start in front of the line, or end after it? */
8199 if (unchanged_p
8200 && (BEG_UNCHANGED < start - 1
8201 || END_UNCHANGED < end))
8202 unchanged_p = 0;
8203
8204 /* If selective display, can't optimize if changes start at the
8205 beginning of the line. */
8206 if (unchanged_p
8207 && INTEGERP (current_buffer->selective_display)
8208 && XINT (current_buffer->selective_display) > 0
8209 && (BEG_UNCHANGED < start || GPT <= start))
8210 unchanged_p = 0;
8211
8212 /* If there are overlays at the start or end of the line, these
8213 may have overlay strings with newlines in them. A change at
8214 START, for instance, may actually concern the display of such
8215 overlay strings as well, and they are displayed on different
8216 lines. So, quickly rule out this case. (For the future, it
8217 might be desirable to implement something more telling than
8218 just BEG/END_UNCHANGED.) */
8219 if (unchanged_p)
8220 {
8221 if (BEG + BEG_UNCHANGED == start
8222 && overlay_touches_p (start))
8223 unchanged_p = 0;
8224 if (END_UNCHANGED == end
8225 && overlay_touches_p (Z - end))
8226 unchanged_p = 0;
8227 }
8228 }
8229
8230 return unchanged_p;
8231 }
8232
8233
8234 /* Do a frame update, taking possible shortcuts into account. This is
8235 the main external entry point for redisplay.
8236
8237 If the last redisplay displayed an echo area message and that message
8238 is no longer requested, we clear the echo area or bring back the
8239 mini-buffer if that is in use. */
8240
8241 void
8242 redisplay ()
8243 {
8244 redisplay_internal (0);
8245 }
8246
8247
8248 /* Return 1 if point moved out of or into a composition. Otherwise
8249 return 0. PREV_BUF and PREV_PT are the last point buffer and
8250 position. BUF and PT are the current point buffer and position. */
8251
8252 int
8253 check_point_in_composition (prev_buf, prev_pt, buf, pt)
8254 struct buffer *prev_buf, *buf;
8255 int prev_pt, pt;
8256 {
8257 int start, end;
8258 Lisp_Object prop;
8259 Lisp_Object buffer;
8260
8261 XSETBUFFER (buffer, buf);
8262 /* Check a composition at the last point if point moved within the
8263 same buffer. */
8264 if (prev_buf == buf)
8265 {
8266 if (prev_pt == pt)
8267 /* Point didn't move. */
8268 return 0;
8269
8270 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
8271 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
8272 && COMPOSITION_VALID_P (start, end, prop)
8273 && start < prev_pt && end > prev_pt)
8274 /* The last point was within the composition. Return 1 iff
8275 point moved out of the composition. */
8276 return (pt <= start || pt >= end);
8277 }
8278
8279 /* Check a composition at the current point. */
8280 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
8281 && find_composition (pt, -1, &start, &end, &prop, buffer)
8282 && COMPOSITION_VALID_P (start, end, prop)
8283 && start < pt && end > pt);
8284 }
8285
8286
8287 /* Reconsider the setting of B->clip_changed which is displayed
8288 in window W. */
8289
8290 static INLINE void
8291 reconsider_clip_changes (w, b)
8292 struct window *w;
8293 struct buffer *b;
8294 {
8295 if (b->prevent_redisplay_optimizations_p)
8296 b->clip_changed = 1;
8297 else if (b->clip_changed
8298 && !NILP (w->window_end_valid)
8299 && w->current_matrix->buffer == b
8300 && w->current_matrix->zv == BUF_ZV (b)
8301 && w->current_matrix->begv == BUF_BEGV (b))
8302 b->clip_changed = 0;
8303
8304 /* If display wasn't paused, and W is not a tool bar window, see if
8305 point has been moved into or out of a composition. In that case,
8306 we set b->clip_changed to 1 to force updating the screen. If
8307 b->clip_changed has already been set to 1, we can skip this
8308 check. */
8309 if (!b->clip_changed
8310 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
8311 {
8312 int pt;
8313
8314 if (w == XWINDOW (selected_window))
8315 pt = BUF_PT (current_buffer);
8316 else
8317 pt = marker_position (w->pointm);
8318
8319 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
8320 || pt != XINT (w->last_point))
8321 && check_point_in_composition (w->current_matrix->buffer,
8322 XINT (w->last_point),
8323 XBUFFER (w->buffer), pt))
8324 b->clip_changed = 1;
8325 }
8326 }
8327
8328
8329 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
8330 response to any user action; therefore, we should preserve the echo
8331 area. (Actually, our caller does that job.) Perhaps in the future
8332 avoid recentering windows if it is not necessary; currently that
8333 causes some problems. */
8334
8335 static void
8336 redisplay_internal (preserve_echo_area)
8337 int preserve_echo_area;
8338 {
8339 struct window *w = XWINDOW (selected_window);
8340 struct frame *f = XFRAME (w->frame);
8341 int pause;
8342 int must_finish = 0;
8343 struct text_pos tlbufpos, tlendpos;
8344 int number_of_visible_frames;
8345 int count;
8346 struct frame *sf = SELECTED_FRAME ();
8347
8348 /* Non-zero means redisplay has to consider all windows on all
8349 frames. Zero means, only selected_window is considered. */
8350 int consider_all_windows_p;
8351
8352 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
8353
8354 /* No redisplay if running in batch mode or frame is not yet fully
8355 initialized, or redisplay is explicitly turned off by setting
8356 Vinhibit_redisplay. */
8357 if (noninteractive
8358 || !NILP (Vinhibit_redisplay)
8359 || !f->glyphs_initialized_p)
8360 return;
8361
8362 /* The flag redisplay_performed_directly_p is set by
8363 direct_output_for_insert when it already did the whole screen
8364 update necessary. */
8365 if (redisplay_performed_directly_p)
8366 {
8367 redisplay_performed_directly_p = 0;
8368 if (!hscroll_windows (selected_window))
8369 return;
8370 }
8371
8372 #ifdef USE_X_TOOLKIT
8373 if (popup_activated ())
8374 return;
8375 #endif
8376
8377 /* I don't think this happens but let's be paranoid. */
8378 if (redisplaying_p)
8379 return;
8380
8381 /* Record a function that resets redisplaying_p to its old value
8382 when we leave this function. */
8383 count = BINDING_STACK_SIZE ();
8384 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
8385 ++redisplaying_p;
8386
8387 retry:
8388 pause = 0;
8389 reconsider_clip_changes (w, current_buffer);
8390
8391 /* If new fonts have been loaded that make a glyph matrix adjustment
8392 necessary, do it. */
8393 if (fonts_changed_p)
8394 {
8395 adjust_glyphs (NULL);
8396 ++windows_or_buffers_changed;
8397 fonts_changed_p = 0;
8398 }
8399
8400 /* If face_change_count is non-zero, init_iterator will free all
8401 realized faces, which includes the faces referenced from current
8402 matrices. So, we can't reuse current matrices in this case. */
8403 if (face_change_count)
8404 ++windows_or_buffers_changed;
8405
8406 if (! FRAME_WINDOW_P (sf)
8407 && previous_terminal_frame != sf)
8408 {
8409 /* Since frames on an ASCII terminal share the same display
8410 area, displaying a different frame means redisplay the whole
8411 thing. */
8412 windows_or_buffers_changed++;
8413 SET_FRAME_GARBAGED (sf);
8414 XSETFRAME (Vterminal_frame, sf);
8415 }
8416 previous_terminal_frame = sf;
8417
8418 /* Set the visible flags for all frames. Do this before checking
8419 for resized or garbaged frames; they want to know if their frames
8420 are visible. See the comment in frame.h for
8421 FRAME_SAMPLE_VISIBILITY. */
8422 {
8423 Lisp_Object tail, frame;
8424
8425 number_of_visible_frames = 0;
8426
8427 FOR_EACH_FRAME (tail, frame)
8428 {
8429 struct frame *f = XFRAME (frame);
8430
8431 FRAME_SAMPLE_VISIBILITY (f);
8432 if (FRAME_VISIBLE_P (f))
8433 ++number_of_visible_frames;
8434 clear_desired_matrices (f);
8435 }
8436 }
8437
8438 /* Notice any pending interrupt request to change frame size. */
8439 do_pending_window_change (1);
8440
8441 /* Clear frames marked as garbaged. */
8442 if (frame_garbaged)
8443 clear_garbaged_frames ();
8444
8445 /* Build menubar and tool-bar items. */
8446 prepare_menu_bars ();
8447
8448 if (windows_or_buffers_changed)
8449 update_mode_lines++;
8450
8451 /* Detect case that we need to write or remove a star in the mode line. */
8452 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
8453 {
8454 w->update_mode_line = Qt;
8455 if (buffer_shared > 1)
8456 update_mode_lines++;
8457 }
8458
8459 /* If %c is in the mode line, update it if needed. */
8460 if (!NILP (w->column_number_displayed)
8461 /* This alternative quickly identifies a common case
8462 where no change is needed. */
8463 && !(PT == XFASTINT (w->last_point)
8464 && XFASTINT (w->last_modified) >= MODIFF
8465 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
8466 && XFASTINT (w->column_number_displayed) != current_column ())
8467 w->update_mode_line = Qt;
8468
8469 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
8470
8471 /* The variable buffer_shared is set in redisplay_window and
8472 indicates that we redisplay a buffer in different windows. See
8473 there. */
8474 consider_all_windows_p = update_mode_lines || buffer_shared > 1;
8475
8476 /* If specs for an arrow have changed, do thorough redisplay
8477 to ensure we remove any arrow that should no longer exist. */
8478 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
8479 || ! EQ (Voverlay_arrow_string, last_arrow_string))
8480 consider_all_windows_p = windows_or_buffers_changed = 1;
8481
8482 /* Normally the message* functions will have already displayed and
8483 updated the echo area, but the frame may have been trashed, or
8484 the update may have been preempted, so display the echo area
8485 again here. Checking message_cleared_p captures the case that
8486 the echo area should be cleared. */
8487 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
8488 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
8489 || (message_cleared_p
8490 && minibuf_level == 0
8491 /* If the mini-window is currently selected, this means the
8492 echo-area doesn't show through. */
8493 && !MINI_WINDOW_P (XWINDOW (selected_window))))
8494 {
8495 int window_height_changed_p = echo_area_display (0);
8496 must_finish = 1;
8497
8498 /* If we don't display the current message, don't clear the
8499 message_cleared_p flag, because, if we did, we wouldn't clear
8500 the echo area in the next redisplay which doesn't preserve
8501 the echo area. */
8502 if (!display_last_displayed_message_p)
8503 message_cleared_p = 0;
8504
8505 if (fonts_changed_p)
8506 goto retry;
8507 else if (window_height_changed_p)
8508 {
8509 consider_all_windows_p = 1;
8510 ++update_mode_lines;
8511 ++windows_or_buffers_changed;
8512
8513 /* If window configuration was changed, frames may have been
8514 marked garbaged. Clear them or we will experience
8515 surprises wrt scrolling. */
8516 if (frame_garbaged)
8517 clear_garbaged_frames ();
8518 }
8519 }
8520 else if (EQ (selected_window, minibuf_window)
8521 && (current_buffer->clip_changed
8522 || XFASTINT (w->last_modified) < MODIFF
8523 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8524 && resize_mini_window (w, 0))
8525 {
8526 /* Resized active mini-window to fit the size of what it is
8527 showing if its contents might have changed. */
8528 must_finish = 1;
8529 consider_all_windows_p = 1;
8530 ++windows_or_buffers_changed;
8531 ++update_mode_lines;
8532
8533 /* If window configuration was changed, frames may have been
8534 marked garbaged. Clear them or we will experience
8535 surprises wrt scrolling. */
8536 if (frame_garbaged)
8537 clear_garbaged_frames ();
8538 }
8539
8540
8541 /* If showing the region, and mark has changed, we must redisplay
8542 the whole window. The assignment to this_line_start_pos prevents
8543 the optimization directly below this if-statement. */
8544 if (((!NILP (Vtransient_mark_mode)
8545 && !NILP (XBUFFER (w->buffer)->mark_active))
8546 != !NILP (w->region_showing))
8547 || (!NILP (w->region_showing)
8548 && !EQ (w->region_showing,
8549 Fmarker_position (XBUFFER (w->buffer)->mark))))
8550 CHARPOS (this_line_start_pos) = 0;
8551
8552 /* Optimize the case that only the line containing the cursor in the
8553 selected window has changed. Variables starting with this_ are
8554 set in display_line and record information about the line
8555 containing the cursor. */
8556 tlbufpos = this_line_start_pos;
8557 tlendpos = this_line_end_pos;
8558 if (!consider_all_windows_p
8559 && CHARPOS (tlbufpos) > 0
8560 && NILP (w->update_mode_line)
8561 && !current_buffer->clip_changed
8562 && FRAME_VISIBLE_P (XFRAME (w->frame))
8563 && !FRAME_OBSCURED_P (XFRAME (w->frame))
8564 /* Make sure recorded data applies to current buffer, etc. */
8565 && this_line_buffer == current_buffer
8566 && current_buffer == XBUFFER (w->buffer)
8567 && NILP (w->force_start)
8568 /* Point must be on the line that we have info recorded about. */
8569 && PT >= CHARPOS (tlbufpos)
8570 && PT <= Z - CHARPOS (tlendpos)
8571 /* All text outside that line, including its final newline,
8572 must be unchanged */
8573 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
8574 CHARPOS (tlendpos)))
8575 {
8576 if (CHARPOS (tlbufpos) > BEGV
8577 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
8578 && (CHARPOS (tlbufpos) == ZV
8579 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
8580 /* Former continuation line has disappeared by becoming empty */
8581 goto cancel;
8582 else if (XFASTINT (w->last_modified) < MODIFF
8583 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
8584 || MINI_WINDOW_P (w))
8585 {
8586 /* We have to handle the case of continuation around a
8587 wide-column character (See the comment in indent.c around
8588 line 885).
8589
8590 For instance, in the following case:
8591
8592 -------- Insert --------
8593 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
8594 J_I_ ==> J_I_ `^^' are cursors.
8595 ^^ ^^
8596 -------- --------
8597
8598 As we have to redraw the line above, we should goto cancel. */
8599
8600 struct it it;
8601 int line_height_before = this_line_pixel_height;
8602
8603 /* Note that start_display will handle the case that the
8604 line starting at tlbufpos is a continuation lines. */
8605 start_display (&it, w, tlbufpos);
8606
8607 /* Implementation note: It this still necessary? */
8608 if (it.current_x != this_line_start_x)
8609 goto cancel;
8610
8611 TRACE ((stderr, "trying display optimization 1\n"));
8612 w->cursor.vpos = -1;
8613 overlay_arrow_seen = 0;
8614 it.vpos = this_line_vpos;
8615 it.current_y = this_line_y;
8616 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
8617 display_line (&it);
8618
8619 /* If line contains point, is not continued,
8620 and ends at same distance from eob as before, we win */
8621 if (w->cursor.vpos >= 0
8622 /* Line is not continued, otherwise this_line_start_pos
8623 would have been set to 0 in display_line. */
8624 && CHARPOS (this_line_start_pos)
8625 /* Line ends as before. */
8626 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
8627 /* Line has same height as before. Otherwise other lines
8628 would have to be shifted up or down. */
8629 && this_line_pixel_height == line_height_before)
8630 {
8631 /* If this is not the window's last line, we must adjust
8632 the charstarts of the lines below. */
8633 if (it.current_y < it.last_visible_y)
8634 {
8635 struct glyph_row *row
8636 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
8637 int delta, delta_bytes;
8638
8639 if (Z - CHARPOS (tlendpos) == ZV)
8640 {
8641 /* This line ends at end of (accessible part of)
8642 buffer. There is no newline to count. */
8643 delta = (Z
8644 - CHARPOS (tlendpos)
8645 - MATRIX_ROW_START_CHARPOS (row));
8646 delta_bytes = (Z_BYTE
8647 - BYTEPOS (tlendpos)
8648 - MATRIX_ROW_START_BYTEPOS (row));
8649 }
8650 else
8651 {
8652 /* This line ends in a newline. Must take
8653 account of the newline and the rest of the
8654 text that follows. */
8655 delta = (Z
8656 - CHARPOS (tlendpos)
8657 - MATRIX_ROW_START_CHARPOS (row));
8658 delta_bytes = (Z_BYTE
8659 - BYTEPOS (tlendpos)
8660 - MATRIX_ROW_START_BYTEPOS (row));
8661 }
8662
8663 increment_matrix_positions (w->current_matrix,
8664 this_line_vpos + 1,
8665 w->current_matrix->nrows,
8666 delta, delta_bytes);
8667 }
8668
8669 /* If this row displays text now but previously didn't,
8670 or vice versa, w->window_end_vpos may have to be
8671 adjusted. */
8672 if ((it.glyph_row - 1)->displays_text_p)
8673 {
8674 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
8675 XSETINT (w->window_end_vpos, this_line_vpos);
8676 }
8677 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
8678 && this_line_vpos > 0)
8679 XSETINT (w->window_end_vpos, this_line_vpos - 1);
8680 w->window_end_valid = Qnil;
8681
8682 /* Update hint: No need to try to scroll in update_window. */
8683 w->desired_matrix->no_scrolling_p = 1;
8684
8685 #if GLYPH_DEBUG
8686 *w->desired_matrix->method = 0;
8687 debug_method_add (w, "optimization 1");
8688 #endif
8689 goto update;
8690 }
8691 else
8692 goto cancel;
8693 }
8694 else if (/* Cursor position hasn't changed. */
8695 PT == XFASTINT (w->last_point)
8696 /* Make sure the cursor was last displayed
8697 in this window. Otherwise we have to reposition it. */
8698 && 0 <= w->cursor.vpos
8699 && XINT (w->height) > w->cursor.vpos)
8700 {
8701 if (!must_finish)
8702 {
8703 do_pending_window_change (1);
8704
8705 /* We used to always goto end_of_redisplay here, but this
8706 isn't enough if we have a blinking cursor. */
8707 if (w->cursor_off_p == w->last_cursor_off_p)
8708 goto end_of_redisplay;
8709 }
8710 goto update;
8711 }
8712 /* If highlighting the region, or if the cursor is in the echo area,
8713 then we can't just move the cursor. */
8714 else if (! (!NILP (Vtransient_mark_mode)
8715 && !NILP (current_buffer->mark_active))
8716 && (EQ (selected_window, current_buffer->last_selected_window)
8717 || highlight_nonselected_windows)
8718 && NILP (w->region_showing)
8719 && NILP (Vshow_trailing_whitespace)
8720 && !cursor_in_echo_area)
8721 {
8722 struct it it;
8723 struct glyph_row *row;
8724
8725 /* Skip from tlbufpos to PT and see where it is. Note that
8726 PT may be in invisible text. If so, we will end at the
8727 next visible position. */
8728 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
8729 NULL, DEFAULT_FACE_ID);
8730 it.current_x = this_line_start_x;
8731 it.current_y = this_line_y;
8732 it.vpos = this_line_vpos;
8733
8734 /* The call to move_it_to stops in front of PT, but
8735 moves over before-strings. */
8736 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
8737
8738 if (it.vpos == this_line_vpos
8739 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
8740 row->enabled_p))
8741 {
8742 xassert (this_line_vpos == it.vpos);
8743 xassert (this_line_y == it.current_y);
8744 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
8745 #if GLYPH_DEBUG
8746 *w->desired_matrix->method = 0;
8747 debug_method_add (w, "optimization 3");
8748 #endif
8749 goto update;
8750 }
8751 else
8752 goto cancel;
8753 }
8754
8755 cancel:
8756 /* Text changed drastically or point moved off of line. */
8757 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
8758 }
8759
8760 CHARPOS (this_line_start_pos) = 0;
8761 consider_all_windows_p |= buffer_shared > 1;
8762 ++clear_face_cache_count;
8763
8764
8765 /* Build desired matrices, and update the display. If
8766 consider_all_windows_p is non-zero, do it for all windows on all
8767 frames. Otherwise do it for selected_window, only. */
8768
8769 if (consider_all_windows_p)
8770 {
8771 Lisp_Object tail, frame;
8772 int i, n = 0, size = 50;
8773 struct frame **updated
8774 = (struct frame **) alloca (size * sizeof *updated);
8775
8776 /* Clear the face cache eventually. */
8777 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
8778 {
8779 clear_face_cache (0);
8780 clear_face_cache_count = 0;
8781 }
8782
8783 /* Recompute # windows showing selected buffer. This will be
8784 incremented each time such a window is displayed. */
8785 buffer_shared = 0;
8786
8787 FOR_EACH_FRAME (tail, frame)
8788 {
8789 struct frame *f = XFRAME (frame);
8790
8791 if (FRAME_WINDOW_P (f) || f == sf)
8792 {
8793 /* Mark all the scroll bars to be removed; we'll redeem
8794 the ones we want when we redisplay their windows. */
8795 if (condemn_scroll_bars_hook)
8796 condemn_scroll_bars_hook (f);
8797
8798 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8799 redisplay_windows (FRAME_ROOT_WINDOW (f));
8800
8801 /* Any scroll bars which redisplay_windows should have
8802 nuked should now go away. */
8803 if (judge_scroll_bars_hook)
8804 judge_scroll_bars_hook (f);
8805
8806 /* If fonts changed, display again. */
8807 if (fonts_changed_p)
8808 goto retry;
8809
8810 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8811 {
8812 /* See if we have to hscroll. */
8813 if (hscroll_windows (f->root_window))
8814 goto retry;
8815
8816 /* Prevent various kinds of signals during display
8817 update. stdio is not robust about handling
8818 signals, which can cause an apparent I/O
8819 error. */
8820 if (interrupt_input)
8821 unrequest_sigio ();
8822 stop_polling ();
8823
8824 /* Update the display. */
8825 set_window_update_flags (XWINDOW (f->root_window), 1);
8826 pause |= update_frame (f, 0, 0);
8827 if (pause)
8828 break;
8829
8830 if (n == size)
8831 {
8832 int nbytes = size * sizeof *updated;
8833 struct frame **p = (struct frame **) alloca (2 * nbytes);
8834 bcopy (updated, p, nbytes);
8835 size *= 2;
8836 }
8837
8838 updated[n++] = f;
8839 }
8840 }
8841 }
8842
8843 /* Do the mark_window_display_accurate after all windows have
8844 been redisplayed because this call resets flags in buffers
8845 which are needed for proper redisplay. */
8846 for (i = 0; i < n; ++i)
8847 {
8848 struct frame *f = updated[i];
8849 mark_window_display_accurate (f->root_window, 1);
8850 if (frame_up_to_date_hook)
8851 frame_up_to_date_hook (f);
8852 }
8853 }
8854 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8855 {
8856 Lisp_Object mini_window;
8857 struct frame *mini_frame;
8858
8859 redisplay_window (selected_window, 1);
8860
8861 /* Compare desired and current matrices, perform output. */
8862 update:
8863
8864 /* If fonts changed, display again. */
8865 if (fonts_changed_p)
8866 goto retry;
8867
8868 /* Prevent various kinds of signals during display update.
8869 stdio is not robust about handling signals,
8870 which can cause an apparent I/O error. */
8871 if (interrupt_input)
8872 unrequest_sigio ();
8873 stop_polling ();
8874
8875 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8876 {
8877 if (hscroll_windows (selected_window))
8878 goto retry;
8879
8880 XWINDOW (selected_window)->must_be_updated_p = 1;
8881 pause = update_frame (sf, 0, 0);
8882 }
8883
8884 /* We may have called echo_area_display at the top of this
8885 function. If the echo area is on another frame, that may
8886 have put text on a frame other than the selected one, so the
8887 above call to update_frame would not have caught it. Catch
8888 it here. */
8889 mini_window = FRAME_MINIBUF_WINDOW (sf);
8890 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8891
8892 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
8893 {
8894 XWINDOW (mini_window)->must_be_updated_p = 1;
8895 pause |= update_frame (mini_frame, 0, 0);
8896 if (!pause && hscroll_windows (mini_window))
8897 goto retry;
8898 }
8899 }
8900
8901 /* If display was paused because of pending input, make sure we do a
8902 thorough update the next time. */
8903 if (pause)
8904 {
8905 /* Prevent the optimization at the beginning of
8906 redisplay_internal that tries a single-line update of the
8907 line containing the cursor in the selected window. */
8908 CHARPOS (this_line_start_pos) = 0;
8909
8910 /* Let the overlay arrow be updated the next time. */
8911 if (!NILP (last_arrow_position))
8912 {
8913 last_arrow_position = Qt;
8914 last_arrow_string = Qt;
8915 }
8916
8917 /* If we pause after scrolling, some rows in the current
8918 matrices of some windows are not valid. */
8919 if (!WINDOW_FULL_WIDTH_P (w)
8920 && !FRAME_WINDOW_P (XFRAME (w->frame)))
8921 update_mode_lines = 1;
8922 }
8923 else
8924 {
8925 if (!consider_all_windows_p)
8926 {
8927 /* This has already been done above if
8928 consider_all_windows_p is set. */
8929 mark_window_display_accurate_1 (w, 1);
8930
8931 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
8932 last_arrow_string = Voverlay_arrow_string;
8933
8934 if (frame_up_to_date_hook != 0)
8935 frame_up_to_date_hook (sf);
8936 }
8937
8938 update_mode_lines = 0;
8939 windows_or_buffers_changed = 0;
8940 }
8941
8942 /* Start SIGIO interrupts coming again. Having them off during the
8943 code above makes it less likely one will discard output, but not
8944 impossible, since there might be stuff in the system buffer here.
8945 But it is much hairier to try to do anything about that. */
8946 if (interrupt_input)
8947 request_sigio ();
8948 start_polling ();
8949
8950 /* If a frame has become visible which was not before, redisplay
8951 again, so that we display it. Expose events for such a frame
8952 (which it gets when becoming visible) don't call the parts of
8953 redisplay constructing glyphs, so simply exposing a frame won't
8954 display anything in this case. So, we have to display these
8955 frames here explicitly. */
8956 if (!pause)
8957 {
8958 Lisp_Object tail, frame;
8959 int new_count = 0;
8960
8961 FOR_EACH_FRAME (tail, frame)
8962 {
8963 int this_is_visible = 0;
8964
8965 if (XFRAME (frame)->visible)
8966 this_is_visible = 1;
8967 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
8968 if (XFRAME (frame)->visible)
8969 this_is_visible = 1;
8970
8971 if (this_is_visible)
8972 new_count++;
8973 }
8974
8975 if (new_count != number_of_visible_frames)
8976 windows_or_buffers_changed++;
8977 }
8978
8979 /* Change frame size now if a change is pending. */
8980 do_pending_window_change (1);
8981
8982 /* If we just did a pending size change, or have additional
8983 visible frames, redisplay again. */
8984 if (windows_or_buffers_changed && !pause)
8985 goto retry;
8986
8987 end_of_redisplay:;
8988
8989 unbind_to (count, Qnil);
8990 }
8991
8992
8993 /* Redisplay, but leave alone any recent echo area message unless
8994 another message has been requested in its place.
8995
8996 This is useful in situations where you need to redisplay but no
8997 user action has occurred, making it inappropriate for the message
8998 area to be cleared. See tracking_off and
8999 wait_reading_process_input for examples of these situations.
9000
9001 FROM_WHERE is an integer saying from where this function was
9002 called. This is useful for debugging. */
9003
9004 void
9005 redisplay_preserve_echo_area (from_where)
9006 int from_where;
9007 {
9008 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
9009
9010 if (!NILP (echo_area_buffer[1]))
9011 {
9012 /* We have a previously displayed message, but no current
9013 message. Redisplay the previous message. */
9014 display_last_displayed_message_p = 1;
9015 redisplay_internal (1);
9016 display_last_displayed_message_p = 0;
9017 }
9018 else
9019 redisplay_internal (1);
9020 }
9021
9022
9023 /* Function registered with record_unwind_protect in
9024 redisplay_internal. Clears the flag indicating that a redisplay is
9025 in progress. */
9026
9027 static Lisp_Object
9028 unwind_redisplay (old_redisplaying_p)
9029 Lisp_Object old_redisplaying_p;
9030 {
9031 redisplaying_p = XFASTINT (old_redisplaying_p);
9032 return Qnil;
9033 }
9034
9035
9036 /* Mark the display of window W as accurate or inaccurate. If
9037 ACCURATE_P is non-zero mark display of W as accurate. If
9038 ACCURATE_P is zero, arrange for W to be redisplayed the next time
9039 redisplay_internal is called. */
9040
9041 static void
9042 mark_window_display_accurate_1 (w, accurate_p)
9043 struct window *w;
9044 int accurate_p;
9045 {
9046 if (BUFFERP (w->buffer))
9047 {
9048 struct buffer *b = XBUFFER (w->buffer);
9049
9050 w->last_modified
9051 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
9052 w->last_overlay_modified
9053 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
9054 w->last_had_star
9055 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
9056
9057 if (accurate_p)
9058 {
9059 b->clip_changed = 0;
9060 b->prevent_redisplay_optimizations_p = 0;
9061
9062 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
9063 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
9064 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
9065 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
9066
9067 w->current_matrix->buffer = b;
9068 w->current_matrix->begv = BUF_BEGV (b);
9069 w->current_matrix->zv = BUF_ZV (b);
9070
9071 w->last_cursor = w->cursor;
9072 w->last_cursor_off_p = w->cursor_off_p;
9073
9074 if (w == XWINDOW (selected_window))
9075 w->last_point = make_number (BUF_PT (b));
9076 else
9077 w->last_point = make_number (XMARKER (w->pointm)->charpos);
9078 }
9079 }
9080
9081 if (accurate_p)
9082 {
9083 w->window_end_valid = w->buffer;
9084 #if 0 /* This is incorrect with variable-height lines. */
9085 xassert (XINT (w->window_end_vpos)
9086 < (XINT (w->height)
9087 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
9088 #endif
9089 w->update_mode_line = Qnil;
9090 }
9091 }
9092
9093
9094 /* Mark the display of windows in the window tree rooted at WINDOW as
9095 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
9096 windows as accurate. If ACCURATE_P is zero, arrange for windows to
9097 be redisplayed the next time redisplay_internal is called. */
9098
9099 void
9100 mark_window_display_accurate (window, accurate_p)
9101 Lisp_Object window;
9102 int accurate_p;
9103 {
9104 struct window *w;
9105
9106 for (; !NILP (window); window = w->next)
9107 {
9108 w = XWINDOW (window);
9109 mark_window_display_accurate_1 (w, accurate_p);
9110
9111 if (!NILP (w->vchild))
9112 mark_window_display_accurate (w->vchild, accurate_p);
9113 if (!NILP (w->hchild))
9114 mark_window_display_accurate (w->hchild, accurate_p);
9115 }
9116
9117 if (accurate_p)
9118 {
9119 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
9120 last_arrow_string = Voverlay_arrow_string;
9121 }
9122 else
9123 {
9124 /* Force a thorough redisplay the next time by setting
9125 last_arrow_position and last_arrow_string to t, which is
9126 unequal to any useful value of Voverlay_arrow_... */
9127 last_arrow_position = Qt;
9128 last_arrow_string = Qt;
9129 }
9130 }
9131
9132
9133 /* Return value in display table DP (Lisp_Char_Table *) for character
9134 C. Since a display table doesn't have any parent, we don't have to
9135 follow parent. Do not call this function directly but use the
9136 macro DISP_CHAR_VECTOR. */
9137
9138 Lisp_Object
9139 disp_char_vector (dp, c)
9140 struct Lisp_Char_Table *dp;
9141 int c;
9142 {
9143 int code[4], i;
9144 Lisp_Object val;
9145
9146 if (SINGLE_BYTE_CHAR_P (c))
9147 return (dp->contents[c]);
9148
9149 SPLIT_CHAR (c, code[0], code[1], code[2]);
9150 if (code[1] < 32)
9151 code[1] = -1;
9152 else if (code[2] < 32)
9153 code[2] = -1;
9154
9155 /* Here, the possible range of code[0] (== charset ID) is
9156 128..max_charset. Since the top level char table contains data
9157 for multibyte characters after 256th element, we must increment
9158 code[0] by 128 to get a correct index. */
9159 code[0] += 128;
9160 code[3] = -1; /* anchor */
9161
9162 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
9163 {
9164 val = dp->contents[code[i]];
9165 if (!SUB_CHAR_TABLE_P (val))
9166 return (NILP (val) ? dp->defalt : val);
9167 }
9168
9169 /* Here, val is a sub char table. We return the default value of
9170 it. */
9171 return (dp->defalt);
9172 }
9173
9174
9175 \f
9176 /***********************************************************************
9177 Window Redisplay
9178 ***********************************************************************/
9179
9180 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
9181
9182 static void
9183 redisplay_windows (window)
9184 Lisp_Object window;
9185 {
9186 while (!NILP (window))
9187 {
9188 struct window *w = XWINDOW (window);
9189
9190 if (!NILP (w->hchild))
9191 redisplay_windows (w->hchild);
9192 else if (!NILP (w->vchild))
9193 redisplay_windows (w->vchild);
9194 else
9195 redisplay_window (window, 0);
9196
9197 window = w->next;
9198 }
9199 }
9200
9201
9202 /* Set cursor position of W. PT is assumed to be displayed in ROW.
9203 DELTA is the number of bytes by which positions recorded in ROW
9204 differ from current buffer positions. */
9205
9206 void
9207 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
9208 struct window *w;
9209 struct glyph_row *row;
9210 struct glyph_matrix *matrix;
9211 int delta, delta_bytes, dy, dvpos;
9212 {
9213 struct glyph *glyph = row->glyphs[TEXT_AREA];
9214 struct glyph *end = glyph + row->used[TEXT_AREA];
9215 int x = row->x;
9216 int pt_old = PT - delta;
9217
9218 /* Skip over glyphs not having an object at the start of the row.
9219 These are special glyphs like truncation marks on terminal
9220 frames. */
9221 if (row->displays_text_p)
9222 while (glyph < end
9223 && INTEGERP (glyph->object)
9224 && glyph->charpos < 0)
9225 {
9226 x += glyph->pixel_width;
9227 ++glyph;
9228 }
9229
9230 while (glyph < end
9231 && !INTEGERP (glyph->object)
9232 && (!BUFFERP (glyph->object)
9233 || glyph->charpos < pt_old))
9234 {
9235 x += glyph->pixel_width;
9236 ++glyph;
9237 }
9238
9239 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
9240 w->cursor.x = x;
9241 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
9242 w->cursor.y = row->y + dy;
9243
9244 if (w == XWINDOW (selected_window))
9245 {
9246 if (!row->continued_p
9247 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
9248 && row->x == 0)
9249 {
9250 this_line_buffer = XBUFFER (w->buffer);
9251
9252 CHARPOS (this_line_start_pos)
9253 = MATRIX_ROW_START_CHARPOS (row) + delta;
9254 BYTEPOS (this_line_start_pos)
9255 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
9256
9257 CHARPOS (this_line_end_pos)
9258 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
9259 BYTEPOS (this_line_end_pos)
9260 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
9261
9262 this_line_y = w->cursor.y;
9263 this_line_pixel_height = row->height;
9264 this_line_vpos = w->cursor.vpos;
9265 this_line_start_x = row->x;
9266 }
9267 else
9268 CHARPOS (this_line_start_pos) = 0;
9269 }
9270 }
9271
9272
9273 /* Run window scroll functions, if any, for WINDOW with new window
9274 start STARTP. Sets the window start of WINDOW to that position.
9275
9276 We assume that the window's buffer is really current. */
9277
9278 static INLINE struct text_pos
9279 run_window_scroll_functions (window, startp)
9280 Lisp_Object window;
9281 struct text_pos startp;
9282 {
9283 struct window *w = XWINDOW (window);
9284 SET_MARKER_FROM_TEXT_POS (w->start, startp);
9285
9286 if (current_buffer != XBUFFER (w->buffer))
9287 abort ();
9288
9289 if (!NILP (Vwindow_scroll_functions))
9290 {
9291 run_hook_with_args_2 (Qwindow_scroll_functions, window,
9292 make_number (CHARPOS (startp)));
9293 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9294 /* In case the hook functions switch buffers. */
9295 if (current_buffer != XBUFFER (w->buffer))
9296 set_buffer_internal_1 (XBUFFER (w->buffer));
9297 }
9298
9299 return startp;
9300 }
9301
9302
9303 /* Modify the desired matrix of window W and W->vscroll so that the
9304 line containing the cursor is fully visible. If this requires
9305 larger matrices than are allocated, set fonts_changed_p and return
9306 0. */
9307
9308 static int
9309 make_cursor_line_fully_visible (w)
9310 struct window *w;
9311 {
9312 struct glyph_matrix *matrix;
9313 struct glyph_row *row;
9314 int window_height;
9315
9316 /* It's not always possible to find the cursor, e.g, when a window
9317 is full of overlay strings. Don't do anything in that case. */
9318 if (w->cursor.vpos < 0)
9319 return 1;
9320
9321 matrix = w->desired_matrix;
9322 row = MATRIX_ROW (matrix, w->cursor.vpos);
9323
9324 /* If the cursor row is not partially visible, there's nothing
9325 to do. */
9326 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
9327 return 1;
9328
9329 /* If the row the cursor is in is taller than the window's height,
9330 it's not clear what to do, so do nothing. */
9331 window_height = window_box_height (w);
9332 if (row->height >= window_height)
9333 return 1;
9334
9335 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
9336 {
9337 int dy = row->height - row->visible_height;
9338 w->vscroll = 0;
9339 w->cursor.y += dy;
9340 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9341 }
9342 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
9343 {
9344 int dy = - (row->height - row->visible_height);
9345 w->vscroll = dy;
9346 w->cursor.y += dy;
9347 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9348 }
9349
9350 /* When we change the cursor y-position of the selected window,
9351 change this_line_y as well so that the display optimization for
9352 the cursor line of the selected window in redisplay_internal uses
9353 the correct y-position. */
9354 if (w == XWINDOW (selected_window))
9355 this_line_y = w->cursor.y;
9356
9357 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
9358 redisplay with larger matrices. */
9359 if (matrix->nrows < required_matrix_height (w))
9360 {
9361 fonts_changed_p = 1;
9362 return 0;
9363 }
9364
9365 return 1;
9366 }
9367
9368
9369 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
9370 non-zero means only WINDOW is redisplayed in redisplay_internal.
9371 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
9372 in redisplay_window to bring a partially visible line into view in
9373 the case that only the cursor has moved.
9374
9375 Value is
9376
9377 1 if scrolling succeeded
9378
9379 0 if scrolling didn't find point.
9380
9381 -1 if new fonts have been loaded so that we must interrupt
9382 redisplay, adjust glyph matrices, and try again. */
9383
9384 enum
9385 {
9386 SCROLLING_SUCCESS,
9387 SCROLLING_FAILED,
9388 SCROLLING_NEED_LARGER_MATRICES
9389 };
9390
9391 static int
9392 try_scrolling (window, just_this_one_p, scroll_conservatively,
9393 scroll_step, temp_scroll_step)
9394 Lisp_Object window;
9395 int just_this_one_p;
9396 int scroll_conservatively, scroll_step;
9397 int temp_scroll_step;
9398 {
9399 struct window *w = XWINDOW (window);
9400 struct frame *f = XFRAME (w->frame);
9401 struct text_pos scroll_margin_pos;
9402 struct text_pos pos;
9403 struct text_pos startp;
9404 struct it it;
9405 Lisp_Object window_end;
9406 int this_scroll_margin;
9407 int dy = 0;
9408 int scroll_max;
9409 int rc;
9410 int amount_to_scroll = 0;
9411 Lisp_Object aggressive;
9412 int height;
9413
9414 #if GLYPH_DEBUG
9415 debug_method_add (w, "try_scrolling");
9416 #endif
9417
9418 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9419
9420 /* Compute scroll margin height in pixels. We scroll when point is
9421 within this distance from the top or bottom of the window. */
9422 if (scroll_margin > 0)
9423 {
9424 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
9425 this_scroll_margin *= CANON_Y_UNIT (f);
9426 }
9427 else
9428 this_scroll_margin = 0;
9429
9430 /* Compute how much we should try to scroll maximally to bring point
9431 into view. */
9432 if (scroll_step || scroll_conservatively || temp_scroll_step)
9433 scroll_max = max (scroll_step,
9434 max (scroll_conservatively, temp_scroll_step));
9435 else if (NUMBERP (current_buffer->scroll_down_aggressively)
9436 || NUMBERP (current_buffer->scroll_up_aggressively))
9437 /* We're trying to scroll because of aggressive scrolling
9438 but no scroll_step is set. Choose an arbitrary one. Maybe
9439 there should be a variable for this. */
9440 scroll_max = 10;
9441 else
9442 scroll_max = 0;
9443 scroll_max *= CANON_Y_UNIT (f);
9444
9445 /* Decide whether we have to scroll down. Start at the window end
9446 and move this_scroll_margin up to find the position of the scroll
9447 margin. */
9448 window_end = Fwindow_end (window, Qt);
9449 CHARPOS (scroll_margin_pos) = XINT (window_end);
9450 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
9451 if (this_scroll_margin)
9452 {
9453 start_display (&it, w, scroll_margin_pos);
9454 move_it_vertically (&it, - this_scroll_margin);
9455 scroll_margin_pos = it.current.pos;
9456 }
9457
9458 if (PT >= CHARPOS (scroll_margin_pos))
9459 {
9460 int y0;
9461
9462 /* Point is in the scroll margin at the bottom of the window, or
9463 below. Compute a new window start that makes point visible. */
9464
9465 /* Compute the distance from the scroll margin to PT.
9466 Give up if the distance is greater than scroll_max. */
9467 start_display (&it, w, scroll_margin_pos);
9468 y0 = it.current_y;
9469 move_it_to (&it, PT, 0, it.last_visible_y, -1,
9470 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9471
9472 /* With a scroll_margin of 0, scroll_margin_pos is at the window
9473 end, which is one line below the window. The iterator's
9474 current_y will be same as y0 in that case, but we have to
9475 scroll a line to make PT visible. That's the reason why 1 is
9476 added below. */
9477 dy = 1 + it.current_y - y0;
9478
9479 if (dy > scroll_max)
9480 return SCROLLING_FAILED;
9481
9482 /* Move the window start down. If scrolling conservatively,
9483 move it just enough down to make point visible. If
9484 scroll_step is set, move it down by scroll_step. */
9485 start_display (&it, w, startp);
9486
9487 if (scroll_conservatively)
9488 amount_to_scroll
9489 = max (max (dy, CANON_Y_UNIT (f)),
9490 CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
9491 else if (scroll_step || temp_scroll_step)
9492 amount_to_scroll = scroll_max;
9493 else
9494 {
9495 aggressive = current_buffer->scroll_down_aggressively;
9496 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
9497 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
9498 if (NUMBERP (aggressive))
9499 amount_to_scroll = XFLOATINT (aggressive) * height;
9500 }
9501
9502 if (amount_to_scroll <= 0)
9503 return SCROLLING_FAILED;
9504
9505 move_it_vertically (&it, amount_to_scroll);
9506 startp = it.current.pos;
9507 }
9508 else
9509 {
9510 /* See if point is inside the scroll margin at the top of the
9511 window. */
9512 scroll_margin_pos = startp;
9513 if (this_scroll_margin)
9514 {
9515 start_display (&it, w, startp);
9516 move_it_vertically (&it, this_scroll_margin);
9517 scroll_margin_pos = it.current.pos;
9518 }
9519
9520 if (PT < CHARPOS (scroll_margin_pos))
9521 {
9522 /* Point is in the scroll margin at the top of the window or
9523 above what is displayed in the window. */
9524 int y0;
9525
9526 /* Compute the vertical distance from PT to the scroll
9527 margin position. Give up if distance is greater than
9528 scroll_max. */
9529 SET_TEXT_POS (pos, PT, PT_BYTE);
9530 start_display (&it, w, pos);
9531 y0 = it.current_y;
9532 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
9533 it.last_visible_y, -1,
9534 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9535 dy = it.current_y - y0;
9536 if (dy > scroll_max)
9537 return SCROLLING_FAILED;
9538
9539 /* Compute new window start. */
9540 start_display (&it, w, startp);
9541
9542 if (scroll_conservatively)
9543 amount_to_scroll =
9544 max (dy, CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
9545 else if (scroll_step || temp_scroll_step)
9546 amount_to_scroll = scroll_max;
9547 else
9548 {
9549 aggressive = current_buffer->scroll_up_aggressively;
9550 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
9551 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
9552 if (NUMBERP (aggressive))
9553 amount_to_scroll = XFLOATINT (aggressive) * height;
9554 }
9555
9556 if (amount_to_scroll <= 0)
9557 return SCROLLING_FAILED;
9558
9559 move_it_vertically (&it, - amount_to_scroll);
9560 startp = it.current.pos;
9561 }
9562 }
9563
9564 /* Run window scroll functions. */
9565 startp = run_window_scroll_functions (window, startp);
9566
9567 /* Display the window. Give up if new fonts are loaded, or if point
9568 doesn't appear. */
9569 if (!try_window (window, startp))
9570 rc = SCROLLING_NEED_LARGER_MATRICES;
9571 else if (w->cursor.vpos < 0)
9572 {
9573 clear_glyph_matrix (w->desired_matrix);
9574 rc = SCROLLING_FAILED;
9575 }
9576 else
9577 {
9578 /* Maybe forget recorded base line for line number display. */
9579 if (!just_this_one_p
9580 || current_buffer->clip_changed
9581 || BEG_UNCHANGED < CHARPOS (startp))
9582 w->base_line_number = Qnil;
9583
9584 /* If cursor ends up on a partially visible line, shift display
9585 lines up or down. If that fails because we need larger
9586 matrices, give up. */
9587 if (!make_cursor_line_fully_visible (w))
9588 rc = SCROLLING_NEED_LARGER_MATRICES;
9589 else
9590 rc = SCROLLING_SUCCESS;
9591 }
9592
9593 return rc;
9594 }
9595
9596
9597 /* Compute a suitable window start for window W if display of W starts
9598 on a continuation line. Value is non-zero if a new window start
9599 was computed.
9600
9601 The new window start will be computed, based on W's width, starting
9602 from the start of the continued line. It is the start of the
9603 screen line with the minimum distance from the old start W->start. */
9604
9605 static int
9606 compute_window_start_on_continuation_line (w)
9607 struct window *w;
9608 {
9609 struct text_pos pos, start_pos;
9610 int window_start_changed_p = 0;
9611
9612 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
9613
9614 /* If window start is on a continuation line... Window start may be
9615 < BEGV in case there's invisible text at the start of the
9616 buffer (M-x rmail, for example). */
9617 if (CHARPOS (start_pos) > BEGV
9618 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
9619 {
9620 struct it it;
9621 struct glyph_row *row;
9622
9623 /* Handle the case that the window start is out of range. */
9624 if (CHARPOS (start_pos) < BEGV)
9625 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
9626 else if (CHARPOS (start_pos) > ZV)
9627 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
9628
9629 /* Find the start of the continued line. This should be fast
9630 because scan_buffer is fast (newline cache). */
9631 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
9632 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
9633 row, DEFAULT_FACE_ID);
9634 reseat_at_previous_visible_line_start (&it);
9635
9636 /* If the line start is "too far" away from the window start,
9637 say it takes too much time to compute a new window start. */
9638 if (CHARPOS (start_pos) - IT_CHARPOS (it)
9639 < XFASTINT (w->height) * XFASTINT (w->width))
9640 {
9641 int min_distance, distance;
9642
9643 /* Move forward by display lines to find the new window
9644 start. If window width was enlarged, the new start can
9645 be expected to be > the old start. If window width was
9646 decreased, the new window start will be < the old start.
9647 So, we're looking for the display line start with the
9648 minimum distance from the old window start. */
9649 pos = it.current.pos;
9650 min_distance = INFINITY;
9651 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
9652 distance < min_distance)
9653 {
9654 min_distance = distance;
9655 pos = it.current.pos;
9656 move_it_by_lines (&it, 1, 0);
9657 }
9658
9659 /* Set the window start there. */
9660 SET_MARKER_FROM_TEXT_POS (w->start, pos);
9661 window_start_changed_p = 1;
9662 }
9663 }
9664
9665 return window_start_changed_p;
9666 }
9667
9668
9669 /* Try cursor movement in case text has not changes in window WINDOW,
9670 with window start STARTP. Value is
9671
9672 CURSOR_MOVEMENT_SUCCESS if successful
9673
9674 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
9675
9676 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
9677 display. *SCROLL_STEP is set to 1, under certain circumstances, if
9678 we want to scroll as if scroll-step were set to 1. See the code.
9679
9680 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
9681 which case we have to abort this redisplay, and adjust matrices
9682 first. */
9683
9684 enum
9685 {
9686 CURSOR_MOVEMENT_SUCCESS,
9687 CURSOR_MOVEMENT_CANNOT_BE_USED,
9688 CURSOR_MOVEMENT_MUST_SCROLL,
9689 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
9690 };
9691
9692 static int
9693 try_cursor_movement (window, startp, scroll_step)
9694 Lisp_Object window;
9695 struct text_pos startp;
9696 int *scroll_step;
9697 {
9698 struct window *w = XWINDOW (window);
9699 struct frame *f = XFRAME (w->frame);
9700 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
9701
9702 #if GLYPH_DEBUG
9703 if (inhibit_try_cursor_movement)
9704 return rc;
9705 #endif
9706
9707 /* Handle case where text has not changed, only point, and it has
9708 not moved off the frame. */
9709 if (/* Point may be in this window. */
9710 PT >= CHARPOS (startp)
9711 /* Selective display hasn't changed. */
9712 && !current_buffer->clip_changed
9713 /* Function force-mode-line-update is used to force a thorough
9714 redisplay. It sets either windows_or_buffers_changed or
9715 update_mode_lines. So don't take a shortcut here for these
9716 cases. */
9717 && !update_mode_lines
9718 && !windows_or_buffers_changed
9719 /* Can't use this case if highlighting a region. When a
9720 region exists, cursor movement has to do more than just
9721 set the cursor. */
9722 && !(!NILP (Vtransient_mark_mode)
9723 && !NILP (current_buffer->mark_active))
9724 && NILP (w->region_showing)
9725 && NILP (Vshow_trailing_whitespace)
9726 /* Right after splitting windows, last_point may be nil. */
9727 && INTEGERP (w->last_point)
9728 /* This code is not used for mini-buffer for the sake of the case
9729 of redisplaying to replace an echo area message; since in
9730 that case the mini-buffer contents per se are usually
9731 unchanged. This code is of no real use in the mini-buffer
9732 since the handling of this_line_start_pos, etc., in redisplay
9733 handles the same cases. */
9734 && !EQ (window, minibuf_window)
9735 /* When splitting windows or for new windows, it happens that
9736 redisplay is called with a nil window_end_vpos or one being
9737 larger than the window. This should really be fixed in
9738 window.c. I don't have this on my list, now, so we do
9739 approximately the same as the old redisplay code. --gerd. */
9740 && INTEGERP (w->window_end_vpos)
9741 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
9742 && (FRAME_WINDOW_P (f)
9743 || !MARKERP (Voverlay_arrow_position)
9744 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
9745 {
9746 int this_scroll_margin;
9747 struct glyph_row *row = NULL;
9748
9749 #if GLYPH_DEBUG
9750 debug_method_add (w, "cursor movement");
9751 #endif
9752
9753 /* Scroll if point within this distance from the top or bottom
9754 of the window. This is a pixel value. */
9755 this_scroll_margin = max (0, scroll_margin);
9756 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
9757 this_scroll_margin *= CANON_Y_UNIT (f);
9758
9759 /* Start with the row the cursor was displayed during the last
9760 not paused redisplay. Give up if that row is not valid. */
9761 if (w->last_cursor.vpos < 0
9762 || w->last_cursor.vpos >= w->current_matrix->nrows)
9763 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9764 else
9765 {
9766 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
9767 if (row->mode_line_p)
9768 ++row;
9769 if (!row->enabled_p)
9770 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9771 }
9772
9773 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
9774 {
9775 int scroll_p = 0;
9776 int last_y = window_text_bottom_y (w) - this_scroll_margin;
9777
9778 if (PT > XFASTINT (w->last_point))
9779 {
9780 /* Point has moved forward. */
9781 while (MATRIX_ROW_END_CHARPOS (row) < PT
9782 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
9783 {
9784 xassert (row->enabled_p);
9785 ++row;
9786 }
9787
9788 /* The end position of a row equals the start position
9789 of the next row. If PT is there, we would rather
9790 display it in the next line. */
9791 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9792 && MATRIX_ROW_END_CHARPOS (row) == PT
9793 && !cursor_row_p (w, row))
9794 ++row;
9795
9796 /* If within the scroll margin, scroll. Note that
9797 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
9798 the next line would be drawn, and that
9799 this_scroll_margin can be zero. */
9800 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
9801 || PT > MATRIX_ROW_END_CHARPOS (row)
9802 /* Line is completely visible last line in window
9803 and PT is to be set in the next line. */
9804 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
9805 && PT == MATRIX_ROW_END_CHARPOS (row)
9806 && !row->ends_at_zv_p
9807 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
9808 scroll_p = 1;
9809 }
9810 else if (PT < XFASTINT (w->last_point))
9811 {
9812 /* Cursor has to be moved backward. Note that PT >=
9813 CHARPOS (startp) because of the outer
9814 if-statement. */
9815 while (!row->mode_line_p
9816 && (MATRIX_ROW_START_CHARPOS (row) > PT
9817 || (MATRIX_ROW_START_CHARPOS (row) == PT
9818 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
9819 && (row->y > this_scroll_margin
9820 || CHARPOS (startp) == BEGV))
9821 {
9822 xassert (row->enabled_p);
9823 --row;
9824 }
9825
9826 /* Consider the following case: Window starts at BEGV,
9827 there is invisible, intangible text at BEGV, so that
9828 display starts at some point START > BEGV. It can
9829 happen that we are called with PT somewhere between
9830 BEGV and START. Try to handle that case. */
9831 if (row < w->current_matrix->rows
9832 || row->mode_line_p)
9833 {
9834 row = w->current_matrix->rows;
9835 if (row->mode_line_p)
9836 ++row;
9837 }
9838
9839 /* Due to newlines in overlay strings, we may have to
9840 skip forward over overlay strings. */
9841 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9842 && MATRIX_ROW_END_CHARPOS (row) == PT
9843 && !cursor_row_p (w, row))
9844 ++row;
9845
9846 /* If within the scroll margin, scroll. */
9847 if (row->y < this_scroll_margin
9848 && CHARPOS (startp) != BEGV)
9849 scroll_p = 1;
9850 }
9851
9852 if (PT < MATRIX_ROW_START_CHARPOS (row)
9853 || PT > MATRIX_ROW_END_CHARPOS (row))
9854 {
9855 /* if PT is not in the glyph row, give up. */
9856 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9857 }
9858 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
9859 {
9860 if (PT == MATRIX_ROW_END_CHARPOS (row)
9861 && !row->ends_at_zv_p
9862 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
9863 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9864 else if (row->height > window_box_height (w))
9865 {
9866 /* If we end up in a partially visible line, let's
9867 make it fully visible, except when it's taller
9868 than the window, in which case we can't do much
9869 about it. */
9870 *scroll_step = 1;
9871 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9872 }
9873 else
9874 {
9875 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9876 try_window (window, startp);
9877 if (!make_cursor_line_fully_visible (w))
9878 rc = CURSOR_MOVEMENT_NEED_LARGER_MATRICES;
9879 else
9880 rc = CURSOR_MOVEMENT_SUCCESS;
9881 }
9882 }
9883 else if (scroll_p)
9884 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9885 else
9886 {
9887 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9888 rc = CURSOR_MOVEMENT_SUCCESS;
9889 }
9890 }
9891 }
9892
9893 return rc;
9894 }
9895
9896
9897 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
9898 selected_window is redisplayed. */
9899
9900 static void
9901 redisplay_window (window, just_this_one_p)
9902 Lisp_Object window;
9903 int just_this_one_p;
9904 {
9905 struct window *w = XWINDOW (window);
9906 struct frame *f = XFRAME (w->frame);
9907 struct buffer *buffer = XBUFFER (w->buffer);
9908 struct buffer *old = current_buffer;
9909 struct text_pos lpoint, opoint, startp;
9910 int update_mode_line;
9911 int tem;
9912 struct it it;
9913 /* Record it now because it's overwritten. */
9914 int current_matrix_up_to_date_p = 0;
9915 int temp_scroll_step = 0;
9916 int count = BINDING_STACK_SIZE ();
9917 int rc;
9918
9919 SET_TEXT_POS (lpoint, PT, PT_BYTE);
9920 opoint = lpoint;
9921
9922 /* W must be a leaf window here. */
9923 xassert (!NILP (w->buffer));
9924 #if GLYPH_DEBUG
9925 *w->desired_matrix->method = 0;
9926 #endif
9927
9928 specbind (Qinhibit_point_motion_hooks, Qt);
9929
9930 reconsider_clip_changes (w, buffer);
9931
9932 /* Has the mode line to be updated? */
9933 update_mode_line = (!NILP (w->update_mode_line)
9934 || update_mode_lines
9935 || buffer->clip_changed);
9936
9937 if (MINI_WINDOW_P (w))
9938 {
9939 if (w == XWINDOW (echo_area_window)
9940 && !NILP (echo_area_buffer[0]))
9941 {
9942 if (update_mode_line)
9943 /* We may have to update a tty frame's menu bar or a
9944 tool-bar. Example `M-x C-h C-h C-g'. */
9945 goto finish_menu_bars;
9946 else
9947 /* We've already displayed the echo area glyphs in this window. */
9948 goto finish_scroll_bars;
9949 }
9950 else if (w != XWINDOW (minibuf_window))
9951 {
9952 /* W is a mini-buffer window, but it's not the currently
9953 active one, so clear it. */
9954 int yb = window_text_bottom_y (w);
9955 struct glyph_row *row;
9956 int y;
9957
9958 for (y = 0, row = w->desired_matrix->rows;
9959 y < yb;
9960 y += row->height, ++row)
9961 blank_row (w, row, y);
9962 goto finish_scroll_bars;
9963 }
9964
9965 clear_glyph_matrix (w->desired_matrix);
9966 }
9967
9968 /* Otherwise set up data on this window; select its buffer and point
9969 value. */
9970 /* Really select the buffer, for the sake of buffer-local
9971 variables. */
9972 set_buffer_internal_1 (XBUFFER (w->buffer));
9973 SET_TEXT_POS (opoint, PT, PT_BYTE);
9974
9975 current_matrix_up_to_date_p
9976 = (!NILP (w->window_end_valid)
9977 && !current_buffer->clip_changed
9978 && XFASTINT (w->last_modified) >= MODIFF
9979 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
9980
9981 /* When windows_or_buffers_changed is non-zero, we can't rely on
9982 the window end being valid, so set it to nil there. */
9983 if (windows_or_buffers_changed)
9984 {
9985 /* If window starts on a continuation line, maybe adjust the
9986 window start in case the window's width changed. */
9987 if (XMARKER (w->start)->buffer == current_buffer)
9988 compute_window_start_on_continuation_line (w);
9989
9990 w->window_end_valid = Qnil;
9991 }
9992
9993 /* Some sanity checks. */
9994 CHECK_WINDOW_END (w);
9995 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
9996 abort ();
9997 if (BYTEPOS (opoint) < CHARPOS (opoint))
9998 abort ();
9999
10000 /* If %c is in mode line, update it if needed. */
10001 if (!NILP (w->column_number_displayed)
10002 /* This alternative quickly identifies a common case
10003 where no change is needed. */
10004 && !(PT == XFASTINT (w->last_point)
10005 && XFASTINT (w->last_modified) >= MODIFF
10006 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
10007 && XFASTINT (w->column_number_displayed) != current_column ())
10008 update_mode_line = 1;
10009
10010 /* Count number of windows showing the selected buffer. An indirect
10011 buffer counts as its base buffer. */
10012 if (!just_this_one_p)
10013 {
10014 struct buffer *current_base, *window_base;
10015 current_base = current_buffer;
10016 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
10017 if (current_base->base_buffer)
10018 current_base = current_base->base_buffer;
10019 if (window_base->base_buffer)
10020 window_base = window_base->base_buffer;
10021 if (current_base == window_base)
10022 buffer_shared++;
10023 }
10024
10025 /* Point refers normally to the selected window. For any other
10026 window, set up appropriate value. */
10027 if (!EQ (window, selected_window))
10028 {
10029 int new_pt = XMARKER (w->pointm)->charpos;
10030 int new_pt_byte = marker_byte_position (w->pointm);
10031 if (new_pt < BEGV)
10032 {
10033 new_pt = BEGV;
10034 new_pt_byte = BEGV_BYTE;
10035 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
10036 }
10037 else if (new_pt > (ZV - 1))
10038 {
10039 new_pt = ZV;
10040 new_pt_byte = ZV_BYTE;
10041 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
10042 }
10043
10044 /* We don't use SET_PT so that the point-motion hooks don't run. */
10045 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
10046 }
10047
10048 /* If any of the character widths specified in the display table
10049 have changed, invalidate the width run cache. It's true that
10050 this may be a bit late to catch such changes, but the rest of
10051 redisplay goes (non-fatally) haywire when the display table is
10052 changed, so why should we worry about doing any better? */
10053 if (current_buffer->width_run_cache)
10054 {
10055 struct Lisp_Char_Table *disptab = buffer_display_table ();
10056
10057 if (! disptab_matches_widthtab (disptab,
10058 XVECTOR (current_buffer->width_table)))
10059 {
10060 invalidate_region_cache (current_buffer,
10061 current_buffer->width_run_cache,
10062 BEG, Z);
10063 recompute_width_table (current_buffer, disptab);
10064 }
10065 }
10066
10067 /* If window-start is screwed up, choose a new one. */
10068 if (XMARKER (w->start)->buffer != current_buffer)
10069 goto recenter;
10070
10071 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10072
10073 /* If someone specified a new starting point but did not insist,
10074 check whether it can be used. */
10075 if (!NILP (w->optional_new_start)
10076 && CHARPOS (startp) >= BEGV
10077 && CHARPOS (startp) <= ZV)
10078 {
10079 w->optional_new_start = Qnil;
10080 start_display (&it, w, startp);
10081 move_it_to (&it, PT, 0, it.last_visible_y, -1,
10082 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
10083 if (IT_CHARPOS (it) == PT)
10084 w->force_start = Qt;
10085 }
10086
10087 /* Handle case where place to start displaying has been specified,
10088 unless the specified location is outside the accessible range. */
10089 if (!NILP (w->force_start)
10090 || w->frozen_window_start_p)
10091 {
10092 w->force_start = Qnil;
10093 w->vscroll = 0;
10094 w->window_end_valid = Qnil;
10095
10096 /* Forget any recorded base line for line number display. */
10097 if (!current_matrix_up_to_date_p
10098 || current_buffer->clip_changed)
10099 w->base_line_number = Qnil;
10100
10101 /* Redisplay the mode line. Select the buffer properly for that.
10102 Also, run the hook window-scroll-functions
10103 because we have scrolled. */
10104 /* Note, we do this after clearing force_start because
10105 if there's an error, it is better to forget about force_start
10106 than to get into an infinite loop calling the hook functions
10107 and having them get more errors. */
10108 if (!update_mode_line
10109 || ! NILP (Vwindow_scroll_functions))
10110 {
10111 update_mode_line = 1;
10112 w->update_mode_line = Qt;
10113 startp = run_window_scroll_functions (window, startp);
10114 }
10115
10116 w->last_modified = make_number (0);
10117 w->last_overlay_modified = make_number (0);
10118 if (CHARPOS (startp) < BEGV)
10119 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
10120 else if (CHARPOS (startp) > ZV)
10121 SET_TEXT_POS (startp, ZV, ZV_BYTE);
10122
10123 /* Redisplay, then check if cursor has been set during the
10124 redisplay. Give up if new fonts were loaded. */
10125 if (!try_window (window, startp))
10126 {
10127 w->force_start = Qt;
10128 clear_glyph_matrix (w->desired_matrix);
10129 goto finish_scroll_bars;
10130 }
10131
10132 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
10133 {
10134 /* If point does not appear, try to move point so it does
10135 appear. The desired matrix has been built above, so we
10136 can use it here. */
10137 int window_height;
10138 struct glyph_row *row;
10139
10140 window_height = window_box_height (w) / 2;
10141 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
10142 while (MATRIX_ROW_BOTTOM_Y (row) < window_height)
10143 ++row;
10144
10145 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
10146 MATRIX_ROW_START_BYTEPOS (row));
10147
10148 if (w != XWINDOW (selected_window))
10149 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
10150 else if (current_buffer == old)
10151 SET_TEXT_POS (lpoint, PT, PT_BYTE);
10152
10153 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
10154
10155 /* If we are highlighting the region, then we just changed
10156 the region, so redisplay to show it. */
10157 if (!NILP (Vtransient_mark_mode)
10158 && !NILP (current_buffer->mark_active))
10159 {
10160 clear_glyph_matrix (w->desired_matrix);
10161 if (!try_window (window, startp))
10162 goto need_larger_matrices;
10163 }
10164 }
10165
10166 if (!make_cursor_line_fully_visible (w))
10167 goto need_larger_matrices;
10168 #if GLYPH_DEBUG
10169 debug_method_add (w, "forced window start");
10170 #endif
10171 goto done;
10172 }
10173
10174 /* Handle case where text has not changed, only point, and it has
10175 not moved off the frame. */
10176 if (current_matrix_up_to_date_p
10177 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
10178 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
10179 {
10180 switch (rc)
10181 {
10182 case CURSOR_MOVEMENT_SUCCESS:
10183 goto done;
10184
10185 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
10186 goto need_larger_matrices;
10187
10188 case CURSOR_MOVEMENT_MUST_SCROLL:
10189 goto try_to_scroll;
10190
10191 default:
10192 abort ();
10193 }
10194 }
10195 /* If current starting point was originally the beginning of a line
10196 but no longer is, find a new starting point. */
10197 else if (!NILP (w->start_at_line_beg)
10198 && !(CHARPOS (startp) <= BEGV
10199 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
10200 {
10201 #if GLYPH_DEBUG
10202 debug_method_add (w, "recenter 1");
10203 #endif
10204 goto recenter;
10205 }
10206
10207 /* Try scrolling with try_window_id. Value is > 0 if update has
10208 been done, it is -1 if we know that the same window start will
10209 not work. It is 0 if unsuccessful for some other reason. */
10210 else if ((tem = try_window_id (w)) != 0)
10211 {
10212 #if GLYPH_DEBUG
10213 debug_method_add (w, "try_window_id %d", tem);
10214 #endif
10215
10216 if (fonts_changed_p)
10217 goto need_larger_matrices;
10218 if (tem > 0)
10219 goto done;
10220
10221 /* Otherwise try_window_id has returned -1 which means that we
10222 don't want the alternative below this comment to execute. */
10223 }
10224 else if (CHARPOS (startp) >= BEGV
10225 && CHARPOS (startp) <= ZV
10226 && PT >= CHARPOS (startp)
10227 && (CHARPOS (startp) < ZV
10228 /* Avoid starting at end of buffer. */
10229 || CHARPOS (startp) == BEGV
10230 || (XFASTINT (w->last_modified) >= MODIFF
10231 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
10232 {
10233 #if GLYPH_DEBUG
10234 debug_method_add (w, "same window start");
10235 #endif
10236
10237 /* Try to redisplay starting at same place as before.
10238 If point has not moved off frame, accept the results. */
10239 if (!current_matrix_up_to_date_p
10240 /* Don't use try_window_reusing_current_matrix in this case
10241 because a window scroll function can have changed the
10242 buffer. */
10243 || !NILP (Vwindow_scroll_functions)
10244 || MINI_WINDOW_P (w)
10245 || !try_window_reusing_current_matrix (w))
10246 {
10247 IF_DEBUG (debug_method_add (w, "1"));
10248 try_window (window, startp);
10249 }
10250
10251 if (fonts_changed_p)
10252 goto need_larger_matrices;
10253
10254 if (w->cursor.vpos >= 0)
10255 {
10256 if (!just_this_one_p
10257 || current_buffer->clip_changed
10258 || BEG_UNCHANGED < CHARPOS (startp))
10259 /* Forget any recorded base line for line number display. */
10260 w->base_line_number = Qnil;
10261
10262 if (!make_cursor_line_fully_visible (w))
10263 goto need_larger_matrices;
10264 goto done;
10265 }
10266 else
10267 clear_glyph_matrix (w->desired_matrix);
10268 }
10269
10270 try_to_scroll:
10271
10272 w->last_modified = make_number (0);
10273 w->last_overlay_modified = make_number (0);
10274
10275 /* Redisplay the mode line. Select the buffer properly for that. */
10276 if (!update_mode_line)
10277 {
10278 update_mode_line = 1;
10279 w->update_mode_line = Qt;
10280 }
10281
10282 /* Try to scroll by specified few lines. */
10283 if ((scroll_conservatively
10284 || scroll_step
10285 || temp_scroll_step
10286 || NUMBERP (current_buffer->scroll_up_aggressively)
10287 || NUMBERP (current_buffer->scroll_down_aggressively))
10288 && !current_buffer->clip_changed
10289 && CHARPOS (startp) >= BEGV
10290 && CHARPOS (startp) <= ZV)
10291 {
10292 /* The function returns -1 if new fonts were loaded, 1 if
10293 successful, 0 if not successful. */
10294 int rc = try_scrolling (window, just_this_one_p,
10295 scroll_conservatively,
10296 scroll_step,
10297 temp_scroll_step);
10298 switch (rc)
10299 {
10300 case SCROLLING_SUCCESS:
10301 goto done;
10302
10303 case SCROLLING_NEED_LARGER_MATRICES:
10304 goto need_larger_matrices;
10305
10306 case SCROLLING_FAILED:
10307 break;
10308
10309 default:
10310 abort ();
10311 }
10312 }
10313
10314 /* Finally, just choose place to start which centers point */
10315
10316 recenter:
10317
10318 #if GLYPH_DEBUG
10319 debug_method_add (w, "recenter");
10320 #endif
10321
10322 /* w->vscroll = 0; */
10323
10324 /* Forget any previously recorded base line for line number display. */
10325 if (!current_matrix_up_to_date_p
10326 || current_buffer->clip_changed)
10327 w->base_line_number = Qnil;
10328
10329 /* Move backward half the height of the window. */
10330 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10331 it.current_y = it.last_visible_y;
10332 move_it_vertically_backward (&it, window_box_height (w) / 2);
10333 xassert (IT_CHARPOS (it) >= BEGV);
10334
10335 /* The function move_it_vertically_backward may move over more
10336 than the specified y-distance. If it->w is small, e.g. a
10337 mini-buffer window, we may end up in front of the window's
10338 display area. Start displaying at the start of the line
10339 containing PT in this case. */
10340 if (it.current_y <= 0)
10341 {
10342 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10343 move_it_vertically (&it, 0);
10344 xassert (IT_CHARPOS (it) <= PT);
10345 it.current_y = 0;
10346 }
10347
10348 it.current_x = it.hpos = 0;
10349
10350 /* Set startp here explicitly in case that helps avoid an infinite loop
10351 in case the window-scroll-functions functions get errors. */
10352 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
10353
10354 /* Run scroll hooks. */
10355 startp = run_window_scroll_functions (window, it.current.pos);
10356
10357 /* Redisplay the window. */
10358 if (!current_matrix_up_to_date_p
10359 || windows_or_buffers_changed
10360 /* Don't use try_window_reusing_current_matrix in this case
10361 because it can have changed the buffer. */
10362 || !NILP (Vwindow_scroll_functions)
10363 || !just_this_one_p
10364 || MINI_WINDOW_P (w)
10365 || !try_window_reusing_current_matrix (w))
10366 try_window (window, startp);
10367
10368 /* If new fonts have been loaded (due to fontsets), give up. We
10369 have to start a new redisplay since we need to re-adjust glyph
10370 matrices. */
10371 if (fonts_changed_p)
10372 goto need_larger_matrices;
10373
10374 /* If cursor did not appear assume that the middle of the window is
10375 in the first line of the window. Do it again with the next line.
10376 (Imagine a window of height 100, displaying two lines of height
10377 60. Moving back 50 from it->last_visible_y will end in the first
10378 line.) */
10379 if (w->cursor.vpos < 0)
10380 {
10381 if (!NILP (w->window_end_valid)
10382 && PT >= Z - XFASTINT (w->window_end_pos))
10383 {
10384 clear_glyph_matrix (w->desired_matrix);
10385 move_it_by_lines (&it, 1, 0);
10386 try_window (window, it.current.pos);
10387 }
10388 else if (PT < IT_CHARPOS (it))
10389 {
10390 clear_glyph_matrix (w->desired_matrix);
10391 move_it_by_lines (&it, -1, 0);
10392 try_window (window, it.current.pos);
10393 }
10394 else
10395 {
10396 /* Not much we can do about it. */
10397 }
10398 }
10399
10400 /* Consider the following case: Window starts at BEGV, there is
10401 invisible, intangible text at BEGV, so that display starts at
10402 some point START > BEGV. It can happen that we are called with
10403 PT somewhere between BEGV and START. Try to handle that case. */
10404 if (w->cursor.vpos < 0)
10405 {
10406 struct glyph_row *row = w->current_matrix->rows;
10407 if (row->mode_line_p)
10408 ++row;
10409 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10410 }
10411
10412 if (!make_cursor_line_fully_visible (w))
10413 goto need_larger_matrices;
10414
10415 done:
10416
10417 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10418 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
10419 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
10420 ? Qt : Qnil);
10421
10422 /* Display the mode line, if we must. */
10423 if ((update_mode_line
10424 /* If window not full width, must redo its mode line
10425 if (a) the window to its side is being redone and
10426 (b) we do a frame-based redisplay. This is a consequence
10427 of how inverted lines are drawn in frame-based redisplay. */
10428 || (!just_this_one_p
10429 && !FRAME_WINDOW_P (f)
10430 && !WINDOW_FULL_WIDTH_P (w))
10431 /* Line number to display. */
10432 || INTEGERP (w->base_line_pos)
10433 /* Column number is displayed and different from the one displayed. */
10434 || (!NILP (w->column_number_displayed)
10435 && XFASTINT (w->column_number_displayed) != current_column ()))
10436 /* This means that the window has a mode line. */
10437 && (WINDOW_WANTS_MODELINE_P (w)
10438 || WINDOW_WANTS_HEADER_LINE_P (w)))
10439 {
10440 display_mode_lines (w);
10441
10442 /* If mode line height has changed, arrange for a thorough
10443 immediate redisplay using the correct mode line height. */
10444 if (WINDOW_WANTS_MODELINE_P (w)
10445 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
10446 {
10447 fonts_changed_p = 1;
10448 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
10449 = DESIRED_MODE_LINE_HEIGHT (w);
10450 }
10451
10452 /* If top line height has changed, arrange for a thorough
10453 immediate redisplay using the correct mode line height. */
10454 if (WINDOW_WANTS_HEADER_LINE_P (w)
10455 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
10456 {
10457 fonts_changed_p = 1;
10458 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
10459 = DESIRED_HEADER_LINE_HEIGHT (w);
10460 }
10461
10462 if (fonts_changed_p)
10463 goto need_larger_matrices;
10464 }
10465
10466 if (!line_number_displayed
10467 && !BUFFERP (w->base_line_pos))
10468 {
10469 w->base_line_pos = Qnil;
10470 w->base_line_number = Qnil;
10471 }
10472
10473 finish_menu_bars:
10474
10475 /* When we reach a frame's selected window, redo the frame's menu bar. */
10476 if (update_mode_line
10477 && EQ (FRAME_SELECTED_WINDOW (f), window))
10478 {
10479 int redisplay_menu_p = 0;
10480
10481 if (FRAME_WINDOW_P (f))
10482 {
10483 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
10484 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
10485 #else
10486 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10487 #endif
10488 }
10489 else
10490 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10491
10492 if (redisplay_menu_p)
10493 display_menu_bar (w);
10494
10495 #ifdef HAVE_WINDOW_SYSTEM
10496 if (WINDOWP (f->tool_bar_window)
10497 && (FRAME_TOOL_BAR_LINES (f) > 0
10498 || auto_resize_tool_bars_p))
10499 redisplay_tool_bar (f);
10500 #endif
10501 }
10502
10503 need_larger_matrices:
10504 ;
10505 finish_scroll_bars:
10506
10507 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
10508 {
10509 int start, end, whole;
10510
10511 /* Calculate the start and end positions for the current window.
10512 At some point, it would be nice to choose between scrollbars
10513 which reflect the whole buffer size, with special markers
10514 indicating narrowing, and scrollbars which reflect only the
10515 visible region.
10516
10517 Note that mini-buffers sometimes aren't displaying any text. */
10518 if (!MINI_WINDOW_P (w)
10519 || (w == XWINDOW (minibuf_window)
10520 && NILP (echo_area_buffer[0])))
10521 {
10522 whole = ZV - BEGV;
10523 start = marker_position (w->start) - BEGV;
10524 /* I don't think this is guaranteed to be right. For the
10525 moment, we'll pretend it is. */
10526 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
10527
10528 if (end < start)
10529 end = start;
10530 if (whole < (end - start))
10531 whole = end - start;
10532 }
10533 else
10534 start = end = whole = 0;
10535
10536 /* Indicate what this scroll bar ought to be displaying now. */
10537 set_vertical_scroll_bar_hook (w, end - start, whole, start);
10538
10539 /* Note that we actually used the scroll bar attached to this
10540 window, so it shouldn't be deleted at the end of redisplay. */
10541 redeem_scroll_bar_hook (w);
10542 }
10543
10544 /* Restore current_buffer and value of point in it. */
10545 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
10546 set_buffer_internal_1 (old);
10547 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
10548
10549 unbind_to (count, Qnil);
10550 }
10551
10552
10553 /* Build the complete desired matrix of WINDOW with a window start
10554 buffer position POS. Value is non-zero if successful. It is zero
10555 if fonts were loaded during redisplay which makes re-adjusting
10556 glyph matrices necessary. */
10557
10558 int
10559 try_window (window, pos)
10560 Lisp_Object window;
10561 struct text_pos pos;
10562 {
10563 struct window *w = XWINDOW (window);
10564 struct it it;
10565 struct glyph_row *last_text_row = NULL;
10566
10567 /* Make POS the new window start. */
10568 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
10569
10570 /* Mark cursor position as unknown. No overlay arrow seen. */
10571 w->cursor.vpos = -1;
10572 overlay_arrow_seen = 0;
10573
10574 /* Initialize iterator and info to start at POS. */
10575 start_display (&it, w, pos);
10576
10577 /* Display all lines of W. */
10578 while (it.current_y < it.last_visible_y)
10579 {
10580 if (display_line (&it))
10581 last_text_row = it.glyph_row - 1;
10582 if (fonts_changed_p)
10583 return 0;
10584 }
10585
10586 /* If bottom moved off end of frame, change mode line percentage. */
10587 if (XFASTINT (w->window_end_pos) <= 0
10588 && Z != IT_CHARPOS (it))
10589 w->update_mode_line = Qt;
10590
10591 /* Set window_end_pos to the offset of the last character displayed
10592 on the window from the end of current_buffer. Set
10593 window_end_vpos to its row number. */
10594 if (last_text_row)
10595 {
10596 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
10597 w->window_end_bytepos
10598 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10599 w->window_end_pos
10600 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10601 w->window_end_vpos
10602 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10603 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
10604 ->displays_text_p);
10605 }
10606 else
10607 {
10608 w->window_end_bytepos = 0;
10609 w->window_end_pos = w->window_end_vpos = make_number (0);
10610 }
10611
10612 /* But that is not valid info until redisplay finishes. */
10613 w->window_end_valid = Qnil;
10614 return 1;
10615 }
10616
10617
10618 \f
10619 /************************************************************************
10620 Window redisplay reusing current matrix when buffer has not changed
10621 ************************************************************************/
10622
10623 /* Try redisplay of window W showing an unchanged buffer with a
10624 different window start than the last time it was displayed by
10625 reusing its current matrix. Value is non-zero if successful.
10626 W->start is the new window start. */
10627
10628 static int
10629 try_window_reusing_current_matrix (w)
10630 struct window *w;
10631 {
10632 struct frame *f = XFRAME (w->frame);
10633 struct glyph_row *row, *bottom_row;
10634 struct it it;
10635 struct run run;
10636 struct text_pos start, new_start;
10637 int nrows_scrolled, i;
10638 struct glyph_row *last_text_row;
10639 struct glyph_row *last_reused_text_row;
10640 struct glyph_row *start_row;
10641 int start_vpos, min_y, max_y;
10642
10643 #if GLYPH_DEBUG
10644 if (inhibit_try_window_reusing)
10645 return 0;
10646 #endif
10647
10648 if (/* This function doesn't handle terminal frames. */
10649 !FRAME_WINDOW_P (f)
10650 /* Don't try to reuse the display if windows have been split
10651 or such. */
10652 || windows_or_buffers_changed)
10653 return 0;
10654
10655 /* Can't do this if region may have changed. */
10656 if ((!NILP (Vtransient_mark_mode)
10657 && !NILP (current_buffer->mark_active))
10658 || !NILP (w->region_showing)
10659 || !NILP (Vshow_trailing_whitespace))
10660 return 0;
10661
10662 /* If top-line visibility has changed, give up. */
10663 if (WINDOW_WANTS_HEADER_LINE_P (w)
10664 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
10665 return 0;
10666
10667 /* Give up if old or new display is scrolled vertically. We could
10668 make this function handle this, but right now it doesn't. */
10669 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10670 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
10671 return 0;
10672
10673 /* The variable new_start now holds the new window start. The old
10674 start `start' can be determined from the current matrix. */
10675 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
10676 start = start_row->start.pos;
10677 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
10678
10679 /* Clear the desired matrix for the display below. */
10680 clear_glyph_matrix (w->desired_matrix);
10681
10682 if (CHARPOS (new_start) <= CHARPOS (start))
10683 {
10684 int first_row_y;
10685
10686 /* Don't use this method if the display starts with an ellipsis
10687 displayed for invisible text. It's not easy to handle that case
10688 below, and it's certainly not worth the effort since this is
10689 not a frequent case. */
10690 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
10691 return 0;
10692
10693 IF_DEBUG (debug_method_add (w, "twu1"));
10694
10695 /* Display up to a row that can be reused. The variable
10696 last_text_row is set to the last row displayed that displays
10697 text. Note that it.vpos == 0 if or if not there is a
10698 header-line; it's not the same as the MATRIX_ROW_VPOS! */
10699 start_display (&it, w, new_start);
10700 first_row_y = it.current_y;
10701 w->cursor.vpos = -1;
10702 last_text_row = last_reused_text_row = NULL;
10703
10704 while (it.current_y < it.last_visible_y
10705 && IT_CHARPOS (it) < CHARPOS (start)
10706 && !fonts_changed_p)
10707 if (display_line (&it))
10708 last_text_row = it.glyph_row - 1;
10709
10710 /* A value of current_y < last_visible_y means that we stopped
10711 at the previous window start, which in turn means that we
10712 have at least one reusable row. */
10713 if (it.current_y < it.last_visible_y)
10714 {
10715 /* IT.vpos always starts from 0; it counts text lines. */
10716 nrows_scrolled = it.vpos;
10717
10718 /* Find PT if not already found in the lines displayed. */
10719 if (w->cursor.vpos < 0)
10720 {
10721 int dy = it.current_y - first_row_y;
10722
10723 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10724 row = row_containing_pos (w, PT, row, NULL, dy);
10725 if (row)
10726 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
10727 dy, nrows_scrolled);
10728 else
10729 {
10730 clear_glyph_matrix (w->desired_matrix);
10731 return 0;
10732 }
10733 }
10734
10735 /* Scroll the display. Do it before the current matrix is
10736 changed. The problem here is that update has not yet
10737 run, i.e. part of the current matrix is not up to date.
10738 scroll_run_hook will clear the cursor, and use the
10739 current matrix to get the height of the row the cursor is
10740 in. */
10741 run.current_y = first_row_y;
10742 run.desired_y = it.current_y;
10743 run.height = it.last_visible_y - it.current_y;
10744
10745 if (run.height > 0 && run.current_y != run.desired_y)
10746 {
10747 update_begin (f);
10748 rif->update_window_begin_hook (w);
10749 rif->clear_mouse_face (w);
10750 rif->scroll_run_hook (w, &run);
10751 rif->update_window_end_hook (w, 0, 0);
10752 update_end (f);
10753 }
10754
10755 /* Shift current matrix down by nrows_scrolled lines. */
10756 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
10757 rotate_matrix (w->current_matrix,
10758 start_vpos,
10759 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10760 nrows_scrolled);
10761
10762 /* Disable lines that must be updated. */
10763 for (i = 0; i < it.vpos; ++i)
10764 (start_row + i)->enabled_p = 0;
10765
10766 /* Re-compute Y positions. */
10767 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10768 max_y = it.last_visible_y;
10769 for (row = start_row + nrows_scrolled;
10770 row < bottom_row;
10771 ++row)
10772 {
10773 row->y = it.current_y;
10774 row->visible_height = row->height;
10775
10776 if (row->y < min_y)
10777 row->visible_height -= min_y - row->y;
10778 if (row->y + row->height > max_y)
10779 row->visible_height -= row->y + row->height - max_y;
10780
10781 it.current_y += row->height;
10782
10783 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10784 last_reused_text_row = row;
10785 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
10786 break;
10787 }
10788
10789 /* Disable lines in the current matrix which are now
10790 below the window. */
10791 for (++row; row < bottom_row; ++row)
10792 row->enabled_p = 0;
10793 }
10794
10795 /* Update window_end_pos etc.; last_reused_text_row is the last
10796 reused row from the current matrix containing text, if any.
10797 The value of last_text_row is the last displayed line
10798 containing text. */
10799 if (last_reused_text_row)
10800 {
10801 w->window_end_bytepos
10802 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
10803 w->window_end_pos
10804 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
10805 w->window_end_vpos
10806 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
10807 w->current_matrix));
10808 }
10809 else if (last_text_row)
10810 {
10811 w->window_end_bytepos
10812 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10813 w->window_end_pos
10814 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10815 w->window_end_vpos
10816 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10817 }
10818 else
10819 {
10820 /* This window must be completely empty. */
10821 w->window_end_bytepos = 0;
10822 w->window_end_pos = w->window_end_vpos = make_number (0);
10823 }
10824 w->window_end_valid = Qnil;
10825
10826 /* Update hint: don't try scrolling again in update_window. */
10827 w->desired_matrix->no_scrolling_p = 1;
10828
10829 #if GLYPH_DEBUG
10830 debug_method_add (w, "try_window_reusing_current_matrix 1");
10831 #endif
10832 return 1;
10833 }
10834 else if (CHARPOS (new_start) > CHARPOS (start))
10835 {
10836 struct glyph_row *pt_row, *row;
10837 struct glyph_row *first_reusable_row;
10838 struct glyph_row *first_row_to_display;
10839 int dy;
10840 int yb = window_text_bottom_y (w);
10841
10842 /* Find the row starting at new_start, if there is one. Don't
10843 reuse a partially visible line at the end. */
10844 first_reusable_row = start_row;
10845 while (first_reusable_row->enabled_p
10846 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
10847 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
10848 < CHARPOS (new_start)))
10849 ++first_reusable_row;
10850
10851 /* Give up if there is no row to reuse. */
10852 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
10853 || !first_reusable_row->enabled_p
10854 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
10855 != CHARPOS (new_start)))
10856 return 0;
10857
10858 /* We can reuse fully visible rows beginning with
10859 first_reusable_row to the end of the window. Set
10860 first_row_to_display to the first row that cannot be reused.
10861 Set pt_row to the row containing point, if there is any. */
10862 pt_row = NULL;
10863 for (first_row_to_display = first_reusable_row;
10864 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
10865 ++first_row_to_display)
10866 {
10867 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
10868 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
10869 pt_row = first_row_to_display;
10870 }
10871
10872 /* Start displaying at the start of first_row_to_display. */
10873 xassert (first_row_to_display->y < yb);
10874 init_to_row_start (&it, w, first_row_to_display);
10875
10876 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
10877 - start_vpos);
10878 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
10879 - nrows_scrolled);
10880 it.current_y = (first_row_to_display->y - first_reusable_row->y
10881 + WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
10882
10883 /* Display lines beginning with first_row_to_display in the
10884 desired matrix. Set last_text_row to the last row displayed
10885 that displays text. */
10886 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
10887 if (pt_row == NULL)
10888 w->cursor.vpos = -1;
10889 last_text_row = NULL;
10890 while (it.current_y < it.last_visible_y && !fonts_changed_p)
10891 if (display_line (&it))
10892 last_text_row = it.glyph_row - 1;
10893
10894 /* Give up If point isn't in a row displayed or reused. */
10895 if (w->cursor.vpos < 0)
10896 {
10897 clear_glyph_matrix (w->desired_matrix);
10898 return 0;
10899 }
10900
10901 /* If point is in a reused row, adjust y and vpos of the cursor
10902 position. */
10903 if (pt_row)
10904 {
10905 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
10906 w->current_matrix);
10907 w->cursor.y -= first_reusable_row->y;
10908 }
10909
10910 /* Scroll the display. */
10911 run.current_y = first_reusable_row->y;
10912 run.desired_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10913 run.height = it.last_visible_y - run.current_y;
10914 dy = run.current_y - run.desired_y;
10915
10916 if (run.height)
10917 {
10918 struct frame *f = XFRAME (WINDOW_FRAME (w));
10919 update_begin (f);
10920 rif->update_window_begin_hook (w);
10921 rif->clear_mouse_face (w);
10922 rif->scroll_run_hook (w, &run);
10923 rif->update_window_end_hook (w, 0, 0);
10924 update_end (f);
10925 }
10926
10927 /* Adjust Y positions of reused rows. */
10928 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
10929 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10930 max_y = it.last_visible_y;
10931 for (row = first_reusable_row; row < first_row_to_display; ++row)
10932 {
10933 row->y -= dy;
10934 row->visible_height = row->height;
10935 if (row->y < min_y)
10936 row->visible_height -= min_y - row->y;
10937 if (row->y + row->height > max_y)
10938 row->visible_height -= row->y + row->height - max_y;
10939 }
10940
10941 /* Scroll the current matrix. */
10942 xassert (nrows_scrolled > 0);
10943 rotate_matrix (w->current_matrix,
10944 start_vpos,
10945 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10946 -nrows_scrolled);
10947
10948 /* Disable rows not reused. */
10949 for (row -= nrows_scrolled; row < bottom_row; ++row)
10950 row->enabled_p = 0;
10951
10952 /* Adjust window end. A null value of last_text_row means that
10953 the window end is in reused rows which in turn means that
10954 only its vpos can have changed. */
10955 if (last_text_row)
10956 {
10957 w->window_end_bytepos
10958 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10959 w->window_end_pos
10960 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10961 w->window_end_vpos
10962 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10963 }
10964 else
10965 {
10966 w->window_end_vpos
10967 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
10968 }
10969
10970 w->window_end_valid = Qnil;
10971 w->desired_matrix->no_scrolling_p = 1;
10972
10973 #if GLYPH_DEBUG
10974 debug_method_add (w, "try_window_reusing_current_matrix 2");
10975 #endif
10976 return 1;
10977 }
10978
10979 return 0;
10980 }
10981
10982
10983 \f
10984 /************************************************************************
10985 Window redisplay reusing current matrix when buffer has changed
10986 ************************************************************************/
10987
10988 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
10989 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
10990 int *, int *));
10991 static struct glyph_row *
10992 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
10993 struct glyph_row *));
10994
10995
10996 /* Return the last row in MATRIX displaying text. If row START is
10997 non-null, start searching with that row. IT gives the dimensions
10998 of the display. Value is null if matrix is empty; otherwise it is
10999 a pointer to the row found. */
11000
11001 static struct glyph_row *
11002 find_last_row_displaying_text (matrix, it, start)
11003 struct glyph_matrix *matrix;
11004 struct it *it;
11005 struct glyph_row *start;
11006 {
11007 struct glyph_row *row, *row_found;
11008
11009 /* Set row_found to the last row in IT->w's current matrix
11010 displaying text. The loop looks funny but think of partially
11011 visible lines. */
11012 row_found = NULL;
11013 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
11014 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
11015 {
11016 xassert (row->enabled_p);
11017 row_found = row;
11018 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
11019 break;
11020 ++row;
11021 }
11022
11023 return row_found;
11024 }
11025
11026
11027 /* Return the last row in the current matrix of W that is not affected
11028 by changes at the start of current_buffer that occurred since W's
11029 current matrix was built. Value is null if no such row exists.
11030
11031 BEG_UNCHANGED us the number of characters unchanged at the start of
11032 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
11033 first changed character in current_buffer. Characters at positions <
11034 BEG + BEG_UNCHANGED are at the same buffer positions as they were
11035 when the current matrix was built. */
11036
11037 static struct glyph_row *
11038 find_last_unchanged_at_beg_row (w)
11039 struct window *w;
11040 {
11041 int first_changed_pos = BEG + BEG_UNCHANGED;
11042 struct glyph_row *row;
11043 struct glyph_row *row_found = NULL;
11044 int yb = window_text_bottom_y (w);
11045
11046 /* Find the last row displaying unchanged text. */
11047 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11048 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
11049 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
11050 {
11051 if (/* If row ends before first_changed_pos, it is unchanged,
11052 except in some case. */
11053 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
11054 /* When row ends in ZV and we write at ZV it is not
11055 unchanged. */
11056 && !row->ends_at_zv_p
11057 /* When first_changed_pos is the end of a continued line,
11058 row is not unchanged because it may be no longer
11059 continued. */
11060 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
11061 && row->continued_p))
11062 row_found = row;
11063
11064 /* Stop if last visible row. */
11065 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
11066 break;
11067
11068 ++row;
11069 }
11070
11071 return row_found;
11072 }
11073
11074
11075 /* Find the first glyph row in the current matrix of W that is not
11076 affected by changes at the end of current_buffer since the
11077 time W's current matrix was built.
11078
11079 Return in *DELTA the number of chars by which buffer positions in
11080 unchanged text at the end of current_buffer must be adjusted.
11081
11082 Return in *DELTA_BYTES the corresponding number of bytes.
11083
11084 Value is null if no such row exists, i.e. all rows are affected by
11085 changes. */
11086
11087 static struct glyph_row *
11088 find_first_unchanged_at_end_row (w, delta, delta_bytes)
11089 struct window *w;
11090 int *delta, *delta_bytes;
11091 {
11092 struct glyph_row *row;
11093 struct glyph_row *row_found = NULL;
11094
11095 *delta = *delta_bytes = 0;
11096
11097 /* Display must not have been paused, otherwise the current matrix
11098 is not up to date. */
11099 if (NILP (w->window_end_valid))
11100 abort ();
11101
11102 /* A value of window_end_pos >= END_UNCHANGED means that the window
11103 end is in the range of changed text. If so, there is no
11104 unchanged row at the end of W's current matrix. */
11105 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
11106 return NULL;
11107
11108 /* Set row to the last row in W's current matrix displaying text. */
11109 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
11110
11111 /* If matrix is entirely empty, no unchanged row exists. */
11112 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
11113 {
11114 /* The value of row is the last glyph row in the matrix having a
11115 meaningful buffer position in it. The end position of row
11116 corresponds to window_end_pos. This allows us to translate
11117 buffer positions in the current matrix to current buffer
11118 positions for characters not in changed text. */
11119 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
11120 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
11121 int last_unchanged_pos, last_unchanged_pos_old;
11122 struct glyph_row *first_text_row
11123 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11124
11125 *delta = Z - Z_old;
11126 *delta_bytes = Z_BYTE - Z_BYTE_old;
11127
11128 /* Set last_unchanged_pos to the buffer position of the last
11129 character in the buffer that has not been changed. Z is the
11130 index + 1 of the last character in current_buffer, i.e. by
11131 subtracting END_UNCHANGED we get the index of the last
11132 unchanged character, and we have to add BEG to get its buffer
11133 position. */
11134 last_unchanged_pos = Z - END_UNCHANGED + BEG;
11135 last_unchanged_pos_old = last_unchanged_pos - *delta;
11136
11137 /* Search backward from ROW for a row displaying a line that
11138 starts at a minimum position >= last_unchanged_pos_old. */
11139 for (; row > first_text_row; --row)
11140 {
11141 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
11142 abort ();
11143
11144 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
11145 row_found = row;
11146 }
11147 }
11148
11149 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
11150 abort ();
11151
11152 return row_found;
11153 }
11154
11155
11156 /* Make sure that glyph rows in the current matrix of window W
11157 reference the same glyph memory as corresponding rows in the
11158 frame's frame matrix. This function is called after scrolling W's
11159 current matrix on a terminal frame in try_window_id and
11160 try_window_reusing_current_matrix. */
11161
11162 static void
11163 sync_frame_with_window_matrix_rows (w)
11164 struct window *w;
11165 {
11166 struct frame *f = XFRAME (w->frame);
11167 struct glyph_row *window_row, *window_row_end, *frame_row;
11168
11169 /* Preconditions: W must be a leaf window and full-width. Its frame
11170 must have a frame matrix. */
11171 xassert (NILP (w->hchild) && NILP (w->vchild));
11172 xassert (WINDOW_FULL_WIDTH_P (w));
11173 xassert (!FRAME_WINDOW_P (f));
11174
11175 /* If W is a full-width window, glyph pointers in W's current matrix
11176 have, by definition, to be the same as glyph pointers in the
11177 corresponding frame matrix. */
11178 window_row = w->current_matrix->rows;
11179 window_row_end = window_row + w->current_matrix->nrows;
11180 frame_row = f->current_matrix->rows + XFASTINT (w->top);
11181 while (window_row < window_row_end)
11182 {
11183 int area;
11184
11185 for (area = LEFT_MARGIN_AREA; area <= LAST_AREA; ++area)
11186 frame_row->glyphs[area] = window_row->glyphs[area];
11187
11188 /* Disable frame rows whose corresponding window rows have
11189 been disabled in try_window_id. */
11190 if (!window_row->enabled_p)
11191 frame_row->enabled_p = 0;
11192
11193 ++window_row, ++frame_row;
11194 }
11195 }
11196
11197
11198 /* Find the glyph row in window W containing CHARPOS. Consider all
11199 rows between START and END (not inclusive). END null means search
11200 all rows to the end of the display area of W. Value is the row
11201 containing CHARPOS or null. */
11202
11203 struct glyph_row *
11204 row_containing_pos (w, charpos, start, end, dy)
11205 struct window *w;
11206 int charpos;
11207 struct glyph_row *start, *end;
11208 int dy;
11209 {
11210 struct glyph_row *row = start;
11211 int last_y;
11212
11213 /* If we happen to start on a header-line, skip that. */
11214 if (row->mode_line_p)
11215 ++row;
11216
11217 if ((end && row >= end) || !row->enabled_p)
11218 return NULL;
11219
11220 last_y = window_text_bottom_y (w) - dy;
11221
11222 while ((end == NULL || row < end)
11223 && MATRIX_ROW_BOTTOM_Y (row) < last_y
11224 && (MATRIX_ROW_END_CHARPOS (row) < charpos
11225 || (MATRIX_ROW_END_CHARPOS (row) == charpos
11226 /* The end position of a row equals the start
11227 position of the next row. If CHARPOS is there, we
11228 would rather display it in the next line, except
11229 when this line ends in ZV. */
11230 && !row->ends_at_zv_p
11231 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))
11232 ++row;
11233
11234 /* Give up if CHARPOS not found. */
11235 if ((end && row >= end)
11236 || charpos < MATRIX_ROW_START_CHARPOS (row)
11237 || charpos > MATRIX_ROW_END_CHARPOS (row))
11238 row = NULL;
11239
11240 return row;
11241 }
11242
11243
11244 /* Try to redisplay window W by reusing its existing display. W's
11245 current matrix must be up to date when this function is called,
11246 i.e. window_end_valid must not be nil.
11247
11248 Value is
11249
11250 1 if display has been updated
11251 0 if otherwise unsuccessful
11252 -1 if redisplay with same window start is known not to succeed
11253
11254 The following steps are performed:
11255
11256 1. Find the last row in the current matrix of W that is not
11257 affected by changes at the start of current_buffer. If no such row
11258 is found, give up.
11259
11260 2. Find the first row in W's current matrix that is not affected by
11261 changes at the end of current_buffer. Maybe there is no such row.
11262
11263 3. Display lines beginning with the row + 1 found in step 1 to the
11264 row found in step 2 or, if step 2 didn't find a row, to the end of
11265 the window.
11266
11267 4. If cursor is not known to appear on the window, give up.
11268
11269 5. If display stopped at the row found in step 2, scroll the
11270 display and current matrix as needed.
11271
11272 6. Maybe display some lines at the end of W, if we must. This can
11273 happen under various circumstances, like a partially visible line
11274 becoming fully visible, or because newly displayed lines are displayed
11275 in smaller font sizes.
11276
11277 7. Update W's window end information. */
11278
11279 static int
11280 try_window_id (w)
11281 struct window *w;
11282 {
11283 struct frame *f = XFRAME (w->frame);
11284 struct glyph_matrix *current_matrix = w->current_matrix;
11285 struct glyph_matrix *desired_matrix = w->desired_matrix;
11286 struct glyph_row *last_unchanged_at_beg_row;
11287 struct glyph_row *first_unchanged_at_end_row;
11288 struct glyph_row *row;
11289 struct glyph_row *bottom_row;
11290 int bottom_vpos;
11291 struct it it;
11292 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
11293 struct text_pos start_pos;
11294 struct run run;
11295 int first_unchanged_at_end_vpos = 0;
11296 struct glyph_row *last_text_row, *last_text_row_at_end;
11297 struct text_pos start;
11298 int first_changed_charpos, last_changed_charpos;
11299
11300 #if GLYPH_DEBUG
11301 if (inhibit_try_window_id)
11302 return 0;
11303 #endif
11304
11305 /* This is handy for debugging. */
11306 #if 0
11307 #define GIVE_UP(X) \
11308 do { \
11309 fprintf (stderr, "try_window_id give up %d\n", (X)); \
11310 return 0; \
11311 } while (0)
11312 #else
11313 #define GIVE_UP(X) return 0
11314 #endif
11315
11316 SET_TEXT_POS_FROM_MARKER (start, w->start);
11317
11318 /* Don't use this for mini-windows because these can show
11319 messages and mini-buffers, and we don't handle that here. */
11320 if (MINI_WINDOW_P (w))
11321 GIVE_UP (1);
11322
11323 /* This flag is used to prevent redisplay optimizations. */
11324 if (windows_or_buffers_changed)
11325 GIVE_UP (2);
11326
11327 /* Verify that narrowing has not changed. This flag is also set to prevent
11328 redisplay optimizations. It would be nice to further
11329 reduce the number of cases where this prevents try_window_id. */
11330 if (current_buffer->clip_changed)
11331 GIVE_UP (3);
11332
11333 /* Window must either use window-based redisplay or be full width. */
11334 if (!FRAME_WINDOW_P (f)
11335 && (!line_ins_del_ok
11336 || !WINDOW_FULL_WIDTH_P (w)))
11337 GIVE_UP (4);
11338
11339 /* Give up if point is not known NOT to appear in W. */
11340 if (PT < CHARPOS (start))
11341 GIVE_UP (5);
11342
11343 /* Another way to prevent redisplay optimizations. */
11344 if (XFASTINT (w->last_modified) == 0)
11345 GIVE_UP (6);
11346
11347 /* Verify that window is not hscrolled. */
11348 if (XFASTINT (w->hscroll) != 0)
11349 GIVE_UP (7);
11350
11351 /* Verify that display wasn't paused. */
11352 if (NILP (w->window_end_valid))
11353 GIVE_UP (8);
11354
11355 /* Can't use this if highlighting a region because a cursor movement
11356 will do more than just set the cursor. */
11357 if (!NILP (Vtransient_mark_mode)
11358 && !NILP (current_buffer->mark_active))
11359 GIVE_UP (9);
11360
11361 /* Likewise if highlighting trailing whitespace. */
11362 if (!NILP (Vshow_trailing_whitespace))
11363 GIVE_UP (11);
11364
11365 /* Likewise if showing a region. */
11366 if (!NILP (w->region_showing))
11367 GIVE_UP (10);
11368
11369 /* Can use this if overlay arrow position and or string have changed. */
11370 if (!EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
11371 || !EQ (last_arrow_string, Voverlay_arrow_string))
11372 GIVE_UP (12);
11373
11374
11375 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
11376 only if buffer has really changed. The reason is that the gap is
11377 initially at Z for freshly visited files. The code below would
11378 set end_unchanged to 0 in that case. */
11379 if (MODIFF > SAVE_MODIFF
11380 /* This seems to happen sometimes after saving a buffer. */
11381 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
11382 {
11383 if (GPT - BEG < BEG_UNCHANGED)
11384 BEG_UNCHANGED = GPT - BEG;
11385 if (Z - GPT < END_UNCHANGED)
11386 END_UNCHANGED = Z - GPT;
11387 }
11388
11389 /* The position of the first and last character that has been changed. */
11390 first_changed_charpos = BEG + BEG_UNCHANGED;
11391 last_changed_charpos = Z - END_UNCHANGED;
11392
11393 /* If window starts after a line end, and the last change is in
11394 front of that newline, then changes don't affect the display.
11395 This case happens with stealth-fontification. Note that although
11396 the display is unchanged, glyph positions in the matrix have to
11397 be adjusted, of course. */
11398 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
11399 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
11400 && ((last_changed_charpos < CHARPOS (start)
11401 && CHARPOS (start) == BEGV)
11402 || (last_changed_charpos < CHARPOS (start) - 1
11403 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
11404 {
11405 int Z_old, delta, Z_BYTE_old, delta_bytes;
11406 struct glyph_row *r0;
11407
11408 /* Compute how many chars/bytes have been added to or removed
11409 from the buffer. */
11410 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
11411 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
11412 delta = Z - Z_old;
11413 delta_bytes = Z_BYTE - Z_BYTE_old;
11414
11415 /* Give up if PT is not in the window. Note that it already has
11416 been checked at the start of try_window_id that PT is not in
11417 front of the window start. */
11418 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
11419 GIVE_UP (13);
11420
11421 /* If window start is unchanged, we can reuse the whole matrix
11422 as is, after adjusting glyph positions. No need to compute
11423 the window end again, since its offset from Z hasn't changed. */
11424 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
11425 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
11426 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes)
11427 {
11428 /* Adjust positions in the glyph matrix. */
11429 if (delta || delta_bytes)
11430 {
11431 struct glyph_row *r1
11432 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11433 increment_matrix_positions (w->current_matrix,
11434 MATRIX_ROW_VPOS (r0, current_matrix),
11435 MATRIX_ROW_VPOS (r1, current_matrix),
11436 delta, delta_bytes);
11437 }
11438
11439 /* Set the cursor. */
11440 row = row_containing_pos (w, PT, r0, NULL, 0);
11441 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
11442 return 1;
11443 }
11444 }
11445
11446 /* Handle the case that changes are all below what is displayed in
11447 the window, and that PT is in the window. This shortcut cannot
11448 be taken if ZV is visible in the window, and text has been added
11449 there that is visible in the window. */
11450 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
11451 /* ZV is not visible in the window, or there are no
11452 changes at ZV, actually. */
11453 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
11454 || first_changed_charpos == last_changed_charpos))
11455 {
11456 struct glyph_row *r0;
11457
11458 /* Give up if PT is not in the window. Note that it already has
11459 been checked at the start of try_window_id that PT is not in
11460 front of the window start. */
11461 if (PT >= MATRIX_ROW_END_CHARPOS (row))
11462 GIVE_UP (14);
11463
11464 /* If window start is unchanged, we can reuse the whole matrix
11465 as is, without changing glyph positions since no text has
11466 been added/removed in front of the window end. */
11467 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
11468 if (TEXT_POS_EQUAL_P (start, r0->start.pos))
11469 {
11470 /* We have to compute the window end anew since text
11471 can have been added/removed after it. */
11472 w->window_end_pos
11473 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
11474 w->window_end_bytepos
11475 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11476
11477 /* Set the cursor. */
11478 row = row_containing_pos (w, PT, r0, NULL, 0);
11479 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
11480 return 2;
11481 }
11482 }
11483
11484 /* Give up if window start is in the changed area.
11485
11486 The condition used to read
11487
11488 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
11489
11490 but why that was tested escapes me at the moment. */
11491 if (CHARPOS (start) >= first_changed_charpos
11492 && CHARPOS (start) <= last_changed_charpos)
11493 GIVE_UP (15);
11494
11495 /* Check that window start agrees with the start of the first glyph
11496 row in its current matrix. Check this after we know the window
11497 start is not in changed text, otherwise positions would not be
11498 comparable. */
11499 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
11500 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
11501 GIVE_UP (16);
11502
11503 /* Give up if the window ends in strings. Overlay strings
11504 at the end are difficult to handle, so don't try. */
11505 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
11506 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
11507 GIVE_UP (20);
11508
11509 /* Compute the position at which we have to start displaying new
11510 lines. Some of the lines at the top of the window might be
11511 reusable because they are not displaying changed text. Find the
11512 last row in W's current matrix not affected by changes at the
11513 start of current_buffer. Value is null if changes start in the
11514 first line of window. */
11515 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
11516 if (last_unchanged_at_beg_row)
11517 {
11518 /* Avoid starting to display in the moddle of a character, a TAB
11519 for instance. This is easier than to set up the iterator
11520 exactly, and it's not a frequent case, so the additional
11521 effort wouldn't really pay off. */
11522 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
11523 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
11524 && last_unchanged_at_beg_row > w->current_matrix->rows)
11525 --last_unchanged_at_beg_row;
11526
11527 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
11528 GIVE_UP (17);
11529
11530 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
11531 GIVE_UP (18);
11532 start_pos = it.current.pos;
11533
11534 /* Start displaying new lines in the desired matrix at the same
11535 vpos we would use in the current matrix, i.e. below
11536 last_unchanged_at_beg_row. */
11537 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
11538 current_matrix);
11539 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11540 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
11541
11542 xassert (it.hpos == 0 && it.current_x == 0);
11543 }
11544 else
11545 {
11546 /* There are no reusable lines at the start of the window.
11547 Start displaying in the first line. */
11548 start_display (&it, w, start);
11549 start_pos = it.current.pos;
11550 }
11551
11552 /* Find the first row that is not affected by changes at the end of
11553 the buffer. Value will be null if there is no unchanged row, in
11554 which case we must redisplay to the end of the window. delta
11555 will be set to the value by which buffer positions beginning with
11556 first_unchanged_at_end_row have to be adjusted due to text
11557 changes. */
11558 first_unchanged_at_end_row
11559 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
11560 IF_DEBUG (debug_delta = delta);
11561 IF_DEBUG (debug_delta_bytes = delta_bytes);
11562
11563 /* Set stop_pos to the buffer position up to which we will have to
11564 display new lines. If first_unchanged_at_end_row != NULL, this
11565 is the buffer position of the start of the line displayed in that
11566 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
11567 that we don't stop at a buffer position. */
11568 stop_pos = 0;
11569 if (first_unchanged_at_end_row)
11570 {
11571 xassert (last_unchanged_at_beg_row == NULL
11572 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
11573
11574 /* If this is a continuation line, move forward to the next one
11575 that isn't. Changes in lines above affect this line.
11576 Caution: this may move first_unchanged_at_end_row to a row
11577 not displaying text. */
11578 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
11579 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11580 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11581 < it.last_visible_y))
11582 ++first_unchanged_at_end_row;
11583
11584 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11585 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11586 >= it.last_visible_y))
11587 first_unchanged_at_end_row = NULL;
11588 else
11589 {
11590 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
11591 + delta);
11592 first_unchanged_at_end_vpos
11593 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
11594 xassert (stop_pos >= Z - END_UNCHANGED);
11595 }
11596 }
11597 else if (last_unchanged_at_beg_row == NULL)
11598 GIVE_UP (19);
11599
11600
11601 #if GLYPH_DEBUG
11602
11603 /* Either there is no unchanged row at the end, or the one we have
11604 now displays text. This is a necessary condition for the window
11605 end pos calculation at the end of this function. */
11606 xassert (first_unchanged_at_end_row == NULL
11607 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
11608
11609 debug_last_unchanged_at_beg_vpos
11610 = (last_unchanged_at_beg_row
11611 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
11612 : -1);
11613 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
11614
11615 #endif /* GLYPH_DEBUG != 0 */
11616
11617
11618 /* Display new lines. Set last_text_row to the last new line
11619 displayed which has text on it, i.e. might end up as being the
11620 line where the window_end_vpos is. */
11621 w->cursor.vpos = -1;
11622 last_text_row = NULL;
11623 overlay_arrow_seen = 0;
11624 while (it.current_y < it.last_visible_y
11625 && !fonts_changed_p
11626 && (first_unchanged_at_end_row == NULL
11627 || IT_CHARPOS (it) < stop_pos))
11628 {
11629 if (display_line (&it))
11630 last_text_row = it.glyph_row - 1;
11631 }
11632
11633 if (fonts_changed_p)
11634 return -1;
11635
11636
11637 /* Compute differences in buffer positions, y-positions etc. for
11638 lines reused at the bottom of the window. Compute what we can
11639 scroll. */
11640 if (first_unchanged_at_end_row
11641 /* No lines reused because we displayed everything up to the
11642 bottom of the window. */
11643 && it.current_y < it.last_visible_y)
11644 {
11645 dvpos = (it.vpos
11646 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
11647 current_matrix));
11648 dy = it.current_y - first_unchanged_at_end_row->y;
11649 run.current_y = first_unchanged_at_end_row->y;
11650 run.desired_y = run.current_y + dy;
11651 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
11652 }
11653 else
11654 {
11655 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
11656 first_unchanged_at_end_row = NULL;
11657 }
11658 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
11659
11660
11661 /* Find the cursor if not already found. We have to decide whether
11662 PT will appear on this window (it sometimes doesn't, but this is
11663 not a very frequent case.) This decision has to be made before
11664 the current matrix is altered. A value of cursor.vpos < 0 means
11665 that PT is either in one of the lines beginning at
11666 first_unchanged_at_end_row or below the window. Don't care for
11667 lines that might be displayed later at the window end; as
11668 mentioned, this is not a frequent case. */
11669 if (w->cursor.vpos < 0)
11670 {
11671 /* Cursor in unchanged rows at the top? */
11672 if (PT < CHARPOS (start_pos)
11673 && last_unchanged_at_beg_row)
11674 {
11675 row = row_containing_pos (w, PT,
11676 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
11677 last_unchanged_at_beg_row + 1, 0);
11678 if (row)
11679 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11680 }
11681
11682 /* Start from first_unchanged_at_end_row looking for PT. */
11683 else if (first_unchanged_at_end_row)
11684 {
11685 row = row_containing_pos (w, PT - delta,
11686 first_unchanged_at_end_row, NULL, 0);
11687 if (row)
11688 set_cursor_from_row (w, row, w->current_matrix, delta,
11689 delta_bytes, dy, dvpos);
11690 }
11691
11692 /* Give up if cursor was not found. */
11693 if (w->cursor.vpos < 0)
11694 {
11695 clear_glyph_matrix (w->desired_matrix);
11696 return -1;
11697 }
11698 }
11699
11700 /* Don't let the cursor end in the scroll margins. */
11701 {
11702 int this_scroll_margin, cursor_height;
11703
11704 this_scroll_margin = max (0, scroll_margin);
11705 this_scroll_margin = min (this_scroll_margin,
11706 XFASTINT (w->height) / 4);
11707 this_scroll_margin *= CANON_Y_UNIT (it.f);
11708 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
11709
11710 if ((w->cursor.y < this_scroll_margin
11711 && CHARPOS (start) > BEGV)
11712 /* Don't take scroll margin into account at the bottom because
11713 old redisplay didn't do it either. */
11714 || w->cursor.y + cursor_height > it.last_visible_y)
11715 {
11716 w->cursor.vpos = -1;
11717 clear_glyph_matrix (w->desired_matrix);
11718 return -1;
11719 }
11720 }
11721
11722 /* Scroll the display. Do it before changing the current matrix so
11723 that xterm.c doesn't get confused about where the cursor glyph is
11724 found. */
11725 if (dy && run.height)
11726 {
11727 update_begin (f);
11728
11729 if (FRAME_WINDOW_P (f))
11730 {
11731 rif->update_window_begin_hook (w);
11732 rif->clear_mouse_face (w);
11733 rif->scroll_run_hook (w, &run);
11734 rif->update_window_end_hook (w, 0, 0);
11735 }
11736 else
11737 {
11738 /* Terminal frame. In this case, dvpos gives the number of
11739 lines to scroll by; dvpos < 0 means scroll up. */
11740 int first_unchanged_at_end_vpos
11741 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
11742 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
11743 int end = (XFASTINT (w->top)
11744 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
11745 + window_internal_height (w));
11746
11747 /* Perform the operation on the screen. */
11748 if (dvpos > 0)
11749 {
11750 /* Scroll last_unchanged_at_beg_row to the end of the
11751 window down dvpos lines. */
11752 set_terminal_window (end);
11753
11754 /* On dumb terminals delete dvpos lines at the end
11755 before inserting dvpos empty lines. */
11756 if (!scroll_region_ok)
11757 ins_del_lines (end - dvpos, -dvpos);
11758
11759 /* Insert dvpos empty lines in front of
11760 last_unchanged_at_beg_row. */
11761 ins_del_lines (from, dvpos);
11762 }
11763 else if (dvpos < 0)
11764 {
11765 /* Scroll up last_unchanged_at_beg_vpos to the end of
11766 the window to last_unchanged_at_beg_vpos - |dvpos|. */
11767 set_terminal_window (end);
11768
11769 /* Delete dvpos lines in front of
11770 last_unchanged_at_beg_vpos. ins_del_lines will set
11771 the cursor to the given vpos and emit |dvpos| delete
11772 line sequences. */
11773 ins_del_lines (from + dvpos, dvpos);
11774
11775 /* On a dumb terminal insert dvpos empty lines at the
11776 end. */
11777 if (!scroll_region_ok)
11778 ins_del_lines (end + dvpos, -dvpos);
11779 }
11780
11781 set_terminal_window (0);
11782 }
11783
11784 update_end (f);
11785 }
11786
11787 /* Shift reused rows of the current matrix to the right position.
11788 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
11789 text. */
11790 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11791 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
11792 if (dvpos < 0)
11793 {
11794 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
11795 bottom_vpos, dvpos);
11796 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
11797 bottom_vpos, 0);
11798 }
11799 else if (dvpos > 0)
11800 {
11801 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
11802 bottom_vpos, dvpos);
11803 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
11804 first_unchanged_at_end_vpos + dvpos, 0);
11805 }
11806
11807 /* For frame-based redisplay, make sure that current frame and window
11808 matrix are in sync with respect to glyph memory. */
11809 if (!FRAME_WINDOW_P (f))
11810 sync_frame_with_window_matrix_rows (w);
11811
11812 /* Adjust buffer positions in reused rows. */
11813 if (delta)
11814 increment_matrix_positions (current_matrix,
11815 first_unchanged_at_end_vpos + dvpos,
11816 bottom_vpos, delta, delta_bytes);
11817
11818 /* Adjust Y positions. */
11819 if (dy)
11820 shift_glyph_matrix (w, current_matrix,
11821 first_unchanged_at_end_vpos + dvpos,
11822 bottom_vpos, dy);
11823
11824 if (first_unchanged_at_end_row)
11825 first_unchanged_at_end_row += dvpos;
11826
11827 /* If scrolling up, there may be some lines to display at the end of
11828 the window. */
11829 last_text_row_at_end = NULL;
11830 if (dy < 0)
11831 {
11832 /* Scrolling up can leave for example a partially visible line
11833 at the end of the window to be redisplayed. */
11834 /* Set last_row to the glyph row in the current matrix where the
11835 window end line is found. It has been moved up or down in
11836 the matrix by dvpos. */
11837 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
11838 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
11839
11840 /* If last_row is the window end line, it should display text. */
11841 xassert (last_row->displays_text_p);
11842
11843 /* If window end line was partially visible before, begin
11844 displaying at that line. Otherwise begin displaying with the
11845 line following it. */
11846 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
11847 {
11848 init_to_row_start (&it, w, last_row);
11849 it.vpos = last_vpos;
11850 it.current_y = last_row->y;
11851 }
11852 else
11853 {
11854 init_to_row_end (&it, w, last_row);
11855 it.vpos = 1 + last_vpos;
11856 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
11857 ++last_row;
11858 }
11859
11860 /* We may start in a continuation line. If so, we have to
11861 get the right continuation_lines_width and current_x. */
11862 it.continuation_lines_width = last_row->continuation_lines_width;
11863 it.hpos = it.current_x = 0;
11864
11865 /* Display the rest of the lines at the window end. */
11866 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11867 while (it.current_y < it.last_visible_y
11868 && !fonts_changed_p)
11869 {
11870 /* Is it always sure that the display agrees with lines in
11871 the current matrix? I don't think so, so we mark rows
11872 displayed invalid in the current matrix by setting their
11873 enabled_p flag to zero. */
11874 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
11875 if (display_line (&it))
11876 last_text_row_at_end = it.glyph_row - 1;
11877 }
11878 }
11879
11880 /* Update window_end_pos and window_end_vpos. */
11881 if (first_unchanged_at_end_row
11882 && first_unchanged_at_end_row->y < it.last_visible_y
11883 && !last_text_row_at_end)
11884 {
11885 /* Window end line if one of the preserved rows from the current
11886 matrix. Set row to the last row displaying text in current
11887 matrix starting at first_unchanged_at_end_row, after
11888 scrolling. */
11889 xassert (first_unchanged_at_end_row->displays_text_p);
11890 row = find_last_row_displaying_text (w->current_matrix, &it,
11891 first_unchanged_at_end_row);
11892 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
11893
11894 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
11895 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11896 w->window_end_vpos
11897 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
11898 xassert (w->window_end_bytepos >= 0);
11899 IF_DEBUG (debug_method_add (w, "A"));
11900 }
11901 else if (last_text_row_at_end)
11902 {
11903 w->window_end_pos
11904 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
11905 w->window_end_bytepos
11906 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
11907 w->window_end_vpos
11908 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
11909 xassert (w->window_end_bytepos >= 0);
11910 IF_DEBUG (debug_method_add (w, "B"));
11911 }
11912 else if (last_text_row)
11913 {
11914 /* We have displayed either to the end of the window or at the
11915 end of the window, i.e. the last row with text is to be found
11916 in the desired matrix. */
11917 w->window_end_pos
11918 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
11919 w->window_end_bytepos
11920 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
11921 w->window_end_vpos
11922 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
11923 xassert (w->window_end_bytepos >= 0);
11924 }
11925 else if (first_unchanged_at_end_row == NULL
11926 && last_text_row == NULL
11927 && last_text_row_at_end == NULL)
11928 {
11929 /* Displayed to end of window, but no line containing text was
11930 displayed. Lines were deleted at the end of the window. */
11931 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
11932 int vpos = XFASTINT (w->window_end_vpos);
11933 struct glyph_row *current_row = current_matrix->rows + vpos;
11934 struct glyph_row *desired_row = desired_matrix->rows + vpos;
11935
11936 for (row = NULL;
11937 row == NULL && vpos >= first_vpos;
11938 --vpos, --current_row, --desired_row)
11939 {
11940 if (desired_row->enabled_p)
11941 {
11942 if (desired_row->displays_text_p)
11943 row = desired_row;
11944 }
11945 else if (current_row->displays_text_p)
11946 row = current_row;
11947 }
11948
11949 xassert (row != NULL);
11950 w->window_end_vpos = make_number (vpos + 1);
11951 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
11952 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11953 xassert (w->window_end_bytepos >= 0);
11954 IF_DEBUG (debug_method_add (w, "C"));
11955 }
11956 else
11957 abort ();
11958
11959 #if 0 /* This leads to problems, for instance when the cursor is
11960 at ZV, and the cursor line displays no text. */
11961 /* Disable rows below what's displayed in the window. This makes
11962 debugging easier. */
11963 enable_glyph_matrix_rows (current_matrix,
11964 XFASTINT (w->window_end_vpos) + 1,
11965 bottom_vpos, 0);
11966 #endif
11967
11968 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
11969 debug_end_vpos = XFASTINT (w->window_end_vpos));
11970
11971 /* Record that display has not been completed. */
11972 w->window_end_valid = Qnil;
11973 w->desired_matrix->no_scrolling_p = 1;
11974 return 3;
11975
11976 #undef GIVE_UP
11977 }
11978
11979
11980 \f
11981 /***********************************************************************
11982 More debugging support
11983 ***********************************************************************/
11984
11985 #if GLYPH_DEBUG
11986
11987 void dump_glyph_row P_ ((struct glyph_row *, int, int));
11988 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
11989 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
11990
11991
11992 /* Dump the contents of glyph matrix MATRIX on stderr.
11993
11994 GLYPHS 0 means don't show glyph contents.
11995 GLYPHS 1 means show glyphs in short form
11996 GLYPHS > 1 means show glyphs in long form. */
11997
11998 void
11999 dump_glyph_matrix (matrix, glyphs)
12000 struct glyph_matrix *matrix;
12001 int glyphs;
12002 {
12003 int i;
12004 for (i = 0; i < matrix->nrows; ++i)
12005 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
12006 }
12007
12008
12009 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
12010 the glyph row and area where the glyph comes from. */
12011
12012 void
12013 dump_glyph (row, glyph, area)
12014 struct glyph_row *row;
12015 struct glyph *glyph;
12016 int area;
12017 {
12018 if (glyph->type == CHAR_GLYPH)
12019 {
12020 fprintf (stderr,
12021 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12022 glyph - row->glyphs[TEXT_AREA],
12023 'C',
12024 glyph->charpos,
12025 (BUFFERP (glyph->object)
12026 ? 'B'
12027 : (STRINGP (glyph->object)
12028 ? 'S'
12029 : '-')),
12030 glyph->pixel_width,
12031 glyph->u.ch,
12032 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
12033 ? glyph->u.ch
12034 : '.'),
12035 glyph->face_id,
12036 glyph->left_box_line_p,
12037 glyph->right_box_line_p);
12038 }
12039 else if (glyph->type == STRETCH_GLYPH)
12040 {
12041 fprintf (stderr,
12042 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12043 glyph - row->glyphs[TEXT_AREA],
12044 'S',
12045 glyph->charpos,
12046 (BUFFERP (glyph->object)
12047 ? 'B'
12048 : (STRINGP (glyph->object)
12049 ? 'S'
12050 : '-')),
12051 glyph->pixel_width,
12052 0,
12053 '.',
12054 glyph->face_id,
12055 glyph->left_box_line_p,
12056 glyph->right_box_line_p);
12057 }
12058 else if (glyph->type == IMAGE_GLYPH)
12059 {
12060 fprintf (stderr,
12061 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12062 glyph - row->glyphs[TEXT_AREA],
12063 'I',
12064 glyph->charpos,
12065 (BUFFERP (glyph->object)
12066 ? 'B'
12067 : (STRINGP (glyph->object)
12068 ? 'S'
12069 : '-')),
12070 glyph->pixel_width,
12071 glyph->u.img_id,
12072 '.',
12073 glyph->face_id,
12074 glyph->left_box_line_p,
12075 glyph->right_box_line_p);
12076 }
12077 }
12078
12079
12080 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
12081 GLYPHS 0 means don't show glyph contents.
12082 GLYPHS 1 means show glyphs in short form
12083 GLYPHS > 1 means show glyphs in long form. */
12084
12085 void
12086 dump_glyph_row (row, vpos, glyphs)
12087 struct glyph_row *row;
12088 int vpos, glyphs;
12089 {
12090 if (glyphs != 1)
12091 {
12092 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
12093 fprintf (stderr, "=======================================================================\n");
12094
12095 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d\
12096 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
12097 vpos,
12098 MATRIX_ROW_START_CHARPOS (row),
12099 MATRIX_ROW_END_CHARPOS (row),
12100 row->used[TEXT_AREA],
12101 row->contains_overlapping_glyphs_p,
12102 row->enabled_p,
12103 row->truncated_on_left_p,
12104 row->truncated_on_right_p,
12105 row->overlay_arrow_p,
12106 row->continued_p,
12107 MATRIX_ROW_CONTINUATION_LINE_P (row),
12108 row->displays_text_p,
12109 row->ends_at_zv_p,
12110 row->fill_line_p,
12111 row->ends_in_middle_of_char_p,
12112 row->starts_in_middle_of_char_p,
12113 row->mouse_face_p,
12114 row->x,
12115 row->y,
12116 row->pixel_width,
12117 row->height,
12118 row->visible_height,
12119 row->ascent,
12120 row->phys_ascent);
12121 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
12122 row->end.overlay_string_index,
12123 row->continuation_lines_width);
12124 fprintf (stderr, "%9d %5d\n",
12125 CHARPOS (row->start.string_pos),
12126 CHARPOS (row->end.string_pos));
12127 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
12128 row->end.dpvec_index);
12129 }
12130
12131 if (glyphs > 1)
12132 {
12133 int area;
12134
12135 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12136 {
12137 struct glyph *glyph = row->glyphs[area];
12138 struct glyph *glyph_end = glyph + row->used[area];
12139
12140 /* Glyph for a line end in text. */
12141 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
12142 ++glyph_end;
12143
12144 if (glyph < glyph_end)
12145 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
12146
12147 for (; glyph < glyph_end; ++glyph)
12148 dump_glyph (row, glyph, area);
12149 }
12150 }
12151 else if (glyphs == 1)
12152 {
12153 int area;
12154
12155 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12156 {
12157 char *s = (char *) alloca (row->used[area] + 1);
12158 int i;
12159
12160 for (i = 0; i < row->used[area]; ++i)
12161 {
12162 struct glyph *glyph = row->glyphs[area] + i;
12163 if (glyph->type == CHAR_GLYPH
12164 && glyph->u.ch < 0x80
12165 && glyph->u.ch >= ' ')
12166 s[i] = glyph->u.ch;
12167 else
12168 s[i] = '.';
12169 }
12170
12171 s[i] = '\0';
12172 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
12173 }
12174 }
12175 }
12176
12177
12178 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
12179 Sdump_glyph_matrix, 0, 1, "p",
12180 doc: /* Dump the current matrix of the selected window to stderr.
12181 Shows contents of glyph row structures. With non-nil
12182 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
12183 glyphs in short form, otherwise show glyphs in long form. */)
12184 (glyphs)
12185 Lisp_Object glyphs;
12186 {
12187 struct window *w = XWINDOW (selected_window);
12188 struct buffer *buffer = XBUFFER (w->buffer);
12189
12190 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
12191 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
12192 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
12193 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
12194 fprintf (stderr, "=============================================\n");
12195 dump_glyph_matrix (w->current_matrix,
12196 NILP (glyphs) ? 0 : XINT (glyphs));
12197 return Qnil;
12198 }
12199
12200
12201 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
12202 doc: /* Dump glyph row ROW to stderr.
12203 GLYPH 0 means don't dump glyphs.
12204 GLYPH 1 means dump glyphs in short form.
12205 GLYPH > 1 or omitted means dump glyphs in long form. */)
12206 (row, glyphs)
12207 Lisp_Object row, glyphs;
12208 {
12209 struct glyph_matrix *matrix;
12210 int vpos;
12211
12212 CHECK_NUMBER (row);
12213 matrix = XWINDOW (selected_window)->current_matrix;
12214 vpos = XINT (row);
12215 if (vpos >= 0 && vpos < matrix->nrows)
12216 dump_glyph_row (MATRIX_ROW (matrix, vpos),
12217 vpos,
12218 INTEGERP (glyphs) ? XINT (glyphs) : 2);
12219 return Qnil;
12220 }
12221
12222
12223 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
12224 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
12225 GLYPH 0 means don't dump glyphs.
12226 GLYPH 1 means dump glyphs in short form.
12227 GLYPH > 1 or omitted means dump glyphs in long form. */)
12228 (row, glyphs)
12229 Lisp_Object row, glyphs;
12230 {
12231 struct frame *sf = SELECTED_FRAME ();
12232 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
12233 int vpos;
12234
12235 CHECK_NUMBER (row);
12236 vpos = XINT (row);
12237 if (vpos >= 0 && vpos < m->nrows)
12238 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
12239 INTEGERP (glyphs) ? XINT (glyphs) : 2);
12240 return Qnil;
12241 }
12242
12243
12244 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
12245 doc: /* Toggle tracing of redisplay.
12246 With ARG, turn tracing on if and only if ARG is positive. */)
12247 (arg)
12248 Lisp_Object arg;
12249 {
12250 if (NILP (arg))
12251 trace_redisplay_p = !trace_redisplay_p;
12252 else
12253 {
12254 arg = Fprefix_numeric_value (arg);
12255 trace_redisplay_p = XINT (arg) > 0;
12256 }
12257
12258 return Qnil;
12259 }
12260
12261
12262 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
12263 doc: /* Like `format', but print result to stderr. */)
12264 (nargs, args)
12265 int nargs;
12266 Lisp_Object *args;
12267 {
12268 Lisp_Object s = Fformat (nargs, args);
12269 fprintf (stderr, "%s", XSTRING (s)->data);
12270 return Qnil;
12271 }
12272
12273 #endif /* GLYPH_DEBUG */
12274
12275
12276 \f
12277 /***********************************************************************
12278 Building Desired Matrix Rows
12279 ***********************************************************************/
12280
12281 /* Return a temporary glyph row holding the glyphs of an overlay
12282 arrow. Only used for non-window-redisplay windows. */
12283
12284 static struct glyph_row *
12285 get_overlay_arrow_glyph_row (w)
12286 struct window *w;
12287 {
12288 struct frame *f = XFRAME (WINDOW_FRAME (w));
12289 struct buffer *buffer = XBUFFER (w->buffer);
12290 struct buffer *old = current_buffer;
12291 unsigned char *arrow_string = XSTRING (Voverlay_arrow_string)->data;
12292 int arrow_len = XSTRING (Voverlay_arrow_string)->size;
12293 unsigned char *arrow_end = arrow_string + arrow_len;
12294 unsigned char *p;
12295 struct it it;
12296 int multibyte_p;
12297 int n_glyphs_before;
12298
12299 set_buffer_temp (buffer);
12300 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
12301 it.glyph_row->used[TEXT_AREA] = 0;
12302 SET_TEXT_POS (it.position, 0, 0);
12303
12304 multibyte_p = !NILP (buffer->enable_multibyte_characters);
12305 p = arrow_string;
12306 while (p < arrow_end)
12307 {
12308 Lisp_Object face, ilisp;
12309
12310 /* Get the next character. */
12311 if (multibyte_p)
12312 it.c = string_char_and_length (p, arrow_len, &it.len);
12313 else
12314 it.c = *p, it.len = 1;
12315 p += it.len;
12316
12317 /* Get its face. */
12318 ilisp = make_number (p - arrow_string);
12319 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
12320 it.face_id = compute_char_face (f, it.c, face);
12321
12322 /* Compute its width, get its glyphs. */
12323 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
12324 SET_TEXT_POS (it.position, -1, -1);
12325 PRODUCE_GLYPHS (&it);
12326
12327 /* If this character doesn't fit any more in the line, we have
12328 to remove some glyphs. */
12329 if (it.current_x > it.last_visible_x)
12330 {
12331 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
12332 break;
12333 }
12334 }
12335
12336 set_buffer_temp (old);
12337 return it.glyph_row;
12338 }
12339
12340
12341 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
12342 glyphs are only inserted for terminal frames since we can't really
12343 win with truncation glyphs when partially visible glyphs are
12344 involved. Which glyphs to insert is determined by
12345 produce_special_glyphs. */
12346
12347 static void
12348 insert_left_trunc_glyphs (it)
12349 struct it *it;
12350 {
12351 struct it truncate_it;
12352 struct glyph *from, *end, *to, *toend;
12353
12354 xassert (!FRAME_WINDOW_P (it->f));
12355
12356 /* Get the truncation glyphs. */
12357 truncate_it = *it;
12358 truncate_it.current_x = 0;
12359 truncate_it.face_id = DEFAULT_FACE_ID;
12360 truncate_it.glyph_row = &scratch_glyph_row;
12361 truncate_it.glyph_row->used[TEXT_AREA] = 0;
12362 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
12363 truncate_it.object = make_number (0);
12364 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
12365
12366 /* Overwrite glyphs from IT with truncation glyphs. */
12367 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
12368 end = from + truncate_it.glyph_row->used[TEXT_AREA];
12369 to = it->glyph_row->glyphs[TEXT_AREA];
12370 toend = to + it->glyph_row->used[TEXT_AREA];
12371
12372 while (from < end)
12373 *to++ = *from++;
12374
12375 /* There may be padding glyphs left over. Overwrite them too. */
12376 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
12377 {
12378 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
12379 while (from < end)
12380 *to++ = *from++;
12381 }
12382
12383 if (to > toend)
12384 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
12385 }
12386
12387
12388 /* Compute the pixel height and width of IT->glyph_row.
12389
12390 Most of the time, ascent and height of a display line will be equal
12391 to the max_ascent and max_height values of the display iterator
12392 structure. This is not the case if
12393
12394 1. We hit ZV without displaying anything. In this case, max_ascent
12395 and max_height will be zero.
12396
12397 2. We have some glyphs that don't contribute to the line height.
12398 (The glyph row flag contributes_to_line_height_p is for future
12399 pixmap extensions).
12400
12401 The first case is easily covered by using default values because in
12402 these cases, the line height does not really matter, except that it
12403 must not be zero. */
12404
12405 static void
12406 compute_line_metrics (it)
12407 struct it *it;
12408 {
12409 struct glyph_row *row = it->glyph_row;
12410 int area, i;
12411
12412 if (FRAME_WINDOW_P (it->f))
12413 {
12414 int i, min_y, max_y;
12415
12416 /* The line may consist of one space only, that was added to
12417 place the cursor on it. If so, the row's height hasn't been
12418 computed yet. */
12419 if (row->height == 0)
12420 {
12421 if (it->max_ascent + it->max_descent == 0)
12422 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
12423 row->ascent = it->max_ascent;
12424 row->height = it->max_ascent + it->max_descent;
12425 row->phys_ascent = it->max_phys_ascent;
12426 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
12427 }
12428
12429 /* Compute the width of this line. */
12430 row->pixel_width = row->x;
12431 for (i = 0; i < row->used[TEXT_AREA]; ++i)
12432 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
12433
12434 xassert (row->pixel_width >= 0);
12435 xassert (row->ascent >= 0 && row->height > 0);
12436
12437 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
12438 || MATRIX_ROW_OVERLAPS_PRED_P (row));
12439
12440 /* If first line's physical ascent is larger than its logical
12441 ascent, use the physical ascent, and make the row taller.
12442 This makes accented characters fully visible. */
12443 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
12444 && row->phys_ascent > row->ascent)
12445 {
12446 row->height += row->phys_ascent - row->ascent;
12447 row->ascent = row->phys_ascent;
12448 }
12449
12450 /* Compute how much of the line is visible. */
12451 row->visible_height = row->height;
12452
12453 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (it->w);
12454 max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
12455
12456 if (row->y < min_y)
12457 row->visible_height -= min_y - row->y;
12458 if (row->y + row->height > max_y)
12459 row->visible_height -= row->y + row->height - max_y;
12460 }
12461 else
12462 {
12463 row->pixel_width = row->used[TEXT_AREA];
12464 if (row->continued_p)
12465 row->pixel_width -= it->continuation_pixel_width;
12466 else if (row->truncated_on_right_p)
12467 row->pixel_width -= it->truncation_pixel_width;
12468 row->ascent = row->phys_ascent = 0;
12469 row->height = row->phys_height = row->visible_height = 1;
12470 }
12471
12472 /* Compute a hash code for this row. */
12473 row->hash = 0;
12474 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12475 for (i = 0; i < row->used[area]; ++i)
12476 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
12477 + row->glyphs[area][i].u.val
12478 + row->glyphs[area][i].face_id
12479 + row->glyphs[area][i].padding_p
12480 + (row->glyphs[area][i].type << 2));
12481
12482 it->max_ascent = it->max_descent = 0;
12483 it->max_phys_ascent = it->max_phys_descent = 0;
12484 }
12485
12486
12487 /* Append one space to the glyph row of iterator IT if doing a
12488 window-based redisplay. DEFAULT_FACE_P non-zero means let the
12489 space have the default face, otherwise let it have the same face as
12490 IT->face_id. Value is non-zero if a space was added.
12491
12492 This function is called to make sure that there is always one glyph
12493 at the end of a glyph row that the cursor can be set on under
12494 window-systems. (If there weren't such a glyph we would not know
12495 how wide and tall a box cursor should be displayed).
12496
12497 At the same time this space let's a nicely handle clearing to the
12498 end of the line if the row ends in italic text. */
12499
12500 static int
12501 append_space (it, default_face_p)
12502 struct it *it;
12503 int default_face_p;
12504 {
12505 if (FRAME_WINDOW_P (it->f))
12506 {
12507 int n = it->glyph_row->used[TEXT_AREA];
12508
12509 if (it->glyph_row->glyphs[TEXT_AREA] + n
12510 < it->glyph_row->glyphs[1 + TEXT_AREA])
12511 {
12512 /* Save some values that must not be changed.
12513 Must save IT->c and IT->len because otherwise
12514 ITERATOR_AT_END_P wouldn't work anymore after
12515 append_space has been called. */
12516 enum display_element_type saved_what = it->what;
12517 int saved_c = it->c, saved_len = it->len;
12518 int saved_x = it->current_x;
12519 int saved_face_id = it->face_id;
12520 struct text_pos saved_pos;
12521 Lisp_Object saved_object;
12522 struct face *face;
12523
12524 saved_object = it->object;
12525 saved_pos = it->position;
12526
12527 it->what = IT_CHARACTER;
12528 bzero (&it->position, sizeof it->position);
12529 it->object = make_number (0);
12530 it->c = ' ';
12531 it->len = 1;
12532
12533 if (default_face_p)
12534 it->face_id = DEFAULT_FACE_ID;
12535 else if (it->face_before_selective_p)
12536 it->face_id = it->saved_face_id;
12537 face = FACE_FROM_ID (it->f, it->face_id);
12538 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
12539
12540 PRODUCE_GLYPHS (it);
12541
12542 it->current_x = saved_x;
12543 it->object = saved_object;
12544 it->position = saved_pos;
12545 it->what = saved_what;
12546 it->face_id = saved_face_id;
12547 it->len = saved_len;
12548 it->c = saved_c;
12549 return 1;
12550 }
12551 }
12552
12553 return 0;
12554 }
12555
12556
12557 /* Extend the face of the last glyph in the text area of IT->glyph_row
12558 to the end of the display line. Called from display_line.
12559 If the glyph row is empty, add a space glyph to it so that we
12560 know the face to draw. Set the glyph row flag fill_line_p. */
12561
12562 static void
12563 extend_face_to_end_of_line (it)
12564 struct it *it;
12565 {
12566 struct face *face;
12567 struct frame *f = it->f;
12568
12569 /* If line is already filled, do nothing. */
12570 if (it->current_x >= it->last_visible_x)
12571 return;
12572
12573 /* Face extension extends the background and box of IT->face_id
12574 to the end of the line. If the background equals the background
12575 of the frame, we don't have to do anything. */
12576 if (it->face_before_selective_p)
12577 face = FACE_FROM_ID (it->f, it->saved_face_id);
12578 else
12579 face = FACE_FROM_ID (f, it->face_id);
12580
12581 if (FRAME_WINDOW_P (f)
12582 && face->box == FACE_NO_BOX
12583 && face->background == FRAME_BACKGROUND_PIXEL (f)
12584 && !face->stipple)
12585 return;
12586
12587 /* Set the glyph row flag indicating that the face of the last glyph
12588 in the text area has to be drawn to the end of the text area. */
12589 it->glyph_row->fill_line_p = 1;
12590
12591 /* If current character of IT is not ASCII, make sure we have the
12592 ASCII face. This will be automatically undone the next time
12593 get_next_display_element returns a multibyte character. Note
12594 that the character will always be single byte in unibyte text. */
12595 if (!SINGLE_BYTE_CHAR_P (it->c))
12596 {
12597 it->face_id = FACE_FOR_CHAR (f, face, 0);
12598 }
12599
12600 if (FRAME_WINDOW_P (f))
12601 {
12602 /* If the row is empty, add a space with the current face of IT,
12603 so that we know which face to draw. */
12604 if (it->glyph_row->used[TEXT_AREA] == 0)
12605 {
12606 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
12607 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
12608 it->glyph_row->used[TEXT_AREA] = 1;
12609 }
12610 }
12611 else
12612 {
12613 /* Save some values that must not be changed. */
12614 int saved_x = it->current_x;
12615 struct text_pos saved_pos;
12616 Lisp_Object saved_object;
12617 enum display_element_type saved_what = it->what;
12618 int saved_face_id = it->face_id;
12619
12620 saved_object = it->object;
12621 saved_pos = it->position;
12622
12623 it->what = IT_CHARACTER;
12624 bzero (&it->position, sizeof it->position);
12625 it->object = make_number (0);
12626 it->c = ' ';
12627 it->len = 1;
12628 it->face_id = face->id;
12629
12630 PRODUCE_GLYPHS (it);
12631
12632 while (it->current_x <= it->last_visible_x)
12633 PRODUCE_GLYPHS (it);
12634
12635 /* Don't count these blanks really. It would let us insert a left
12636 truncation glyph below and make us set the cursor on them, maybe. */
12637 it->current_x = saved_x;
12638 it->object = saved_object;
12639 it->position = saved_pos;
12640 it->what = saved_what;
12641 it->face_id = saved_face_id;
12642 }
12643 }
12644
12645
12646 /* Value is non-zero if text starting at CHARPOS in current_buffer is
12647 trailing whitespace. */
12648
12649 static int
12650 trailing_whitespace_p (charpos)
12651 int charpos;
12652 {
12653 int bytepos = CHAR_TO_BYTE (charpos);
12654 int c = 0;
12655
12656 while (bytepos < ZV_BYTE
12657 && (c = FETCH_CHAR (bytepos),
12658 c == ' ' || c == '\t'))
12659 ++bytepos;
12660
12661 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
12662 {
12663 if (bytepos != PT_BYTE)
12664 return 1;
12665 }
12666 return 0;
12667 }
12668
12669
12670 /* Highlight trailing whitespace, if any, in ROW. */
12671
12672 void
12673 highlight_trailing_whitespace (f, row)
12674 struct frame *f;
12675 struct glyph_row *row;
12676 {
12677 int used = row->used[TEXT_AREA];
12678
12679 if (used)
12680 {
12681 struct glyph *start = row->glyphs[TEXT_AREA];
12682 struct glyph *glyph = start + used - 1;
12683
12684 /* Skip over glyphs inserted to display the cursor at the
12685 end of a line, for extending the face of the last glyph
12686 to the end of the line on terminals, and for truncation
12687 and continuation glyphs. */
12688 while (glyph >= start
12689 && glyph->type == CHAR_GLYPH
12690 && INTEGERP (glyph->object))
12691 --glyph;
12692
12693 /* If last glyph is a space or stretch, and it's trailing
12694 whitespace, set the face of all trailing whitespace glyphs in
12695 IT->glyph_row to `trailing-whitespace'. */
12696 if (glyph >= start
12697 && BUFFERP (glyph->object)
12698 && (glyph->type == STRETCH_GLYPH
12699 || (glyph->type == CHAR_GLYPH
12700 && glyph->u.ch == ' '))
12701 && trailing_whitespace_p (glyph->charpos))
12702 {
12703 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
12704
12705 while (glyph >= start
12706 && BUFFERP (glyph->object)
12707 && (glyph->type == STRETCH_GLYPH
12708 || (glyph->type == CHAR_GLYPH
12709 && glyph->u.ch == ' ')))
12710 (glyph--)->face_id = face_id;
12711 }
12712 }
12713 }
12714
12715
12716 /* Value is non-zero if glyph row ROW in window W should be
12717 used to hold the cursor. */
12718
12719 static int
12720 cursor_row_p (w, row)
12721 struct window *w;
12722 struct glyph_row *row;
12723 {
12724 int cursor_row_p = 1;
12725
12726 if (PT == MATRIX_ROW_END_CHARPOS (row))
12727 {
12728 /* If the row ends with a newline from a string, we don't want
12729 the cursor there (if the row is continued it doesn't end in a
12730 newline). */
12731 if (CHARPOS (row->end.string_pos) >= 0
12732 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12733 cursor_row_p = row->continued_p;
12734
12735 /* If the row ends at ZV, display the cursor at the end of that
12736 row instead of at the start of the row below. */
12737 else if (row->ends_at_zv_p)
12738 cursor_row_p = 1;
12739 else
12740 cursor_row_p = 0;
12741 }
12742
12743 return cursor_row_p;
12744 }
12745
12746
12747 /* Construct the glyph row IT->glyph_row in the desired matrix of
12748 IT->w from text at the current position of IT. See dispextern.h
12749 for an overview of struct it. Value is non-zero if
12750 IT->glyph_row displays text, as opposed to a line displaying ZV
12751 only. */
12752
12753 static int
12754 display_line (it)
12755 struct it *it;
12756 {
12757 struct glyph_row *row = it->glyph_row;
12758
12759 /* We always start displaying at hpos zero even if hscrolled. */
12760 xassert (it->hpos == 0 && it->current_x == 0);
12761
12762 /* We must not display in a row that's not a text row. */
12763 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
12764 < it->w->desired_matrix->nrows);
12765
12766 /* Is IT->w showing the region? */
12767 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
12768
12769 /* Clear the result glyph row and enable it. */
12770 prepare_desired_row (row);
12771
12772 row->y = it->current_y;
12773 row->start = it->current;
12774 row->continuation_lines_width = it->continuation_lines_width;
12775 row->displays_text_p = 1;
12776 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
12777 it->starts_in_middle_of_char_p = 0;
12778
12779 /* Arrange the overlays nicely for our purposes. Usually, we call
12780 display_line on only one line at a time, in which case this
12781 can't really hurt too much, or we call it on lines which appear
12782 one after another in the buffer, in which case all calls to
12783 recenter_overlay_lists but the first will be pretty cheap. */
12784 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
12785
12786 /* Move over display elements that are not visible because we are
12787 hscrolled. This may stop at an x-position < IT->first_visible_x
12788 if the first glyph is partially visible or if we hit a line end. */
12789 if (it->current_x < it->first_visible_x)
12790 move_it_in_display_line_to (it, ZV, it->first_visible_x,
12791 MOVE_TO_POS | MOVE_TO_X);
12792
12793 /* Get the initial row height. This is either the height of the
12794 text hscrolled, if there is any, or zero. */
12795 row->ascent = it->max_ascent;
12796 row->height = it->max_ascent + it->max_descent;
12797 row->phys_ascent = it->max_phys_ascent;
12798 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
12799
12800 /* Loop generating characters. The loop is left with IT on the next
12801 character to display. */
12802 while (1)
12803 {
12804 int n_glyphs_before, hpos_before, x_before;
12805 int x, i, nglyphs;
12806 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
12807
12808 /* Retrieve the next thing to display. Value is zero if end of
12809 buffer reached. */
12810 if (!get_next_display_element (it))
12811 {
12812 /* Maybe add a space at the end of this line that is used to
12813 display the cursor there under X. Set the charpos of the
12814 first glyph of blank lines not corresponding to any text
12815 to -1. */
12816 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
12817 || row->used[TEXT_AREA] == 0)
12818 {
12819 row->glyphs[TEXT_AREA]->charpos = -1;
12820 row->displays_text_p = 0;
12821
12822 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
12823 && (!MINI_WINDOW_P (it->w)
12824 || (minibuf_level && EQ (it->window, minibuf_window))))
12825 row->indicate_empty_line_p = 1;
12826 }
12827
12828 it->continuation_lines_width = 0;
12829 row->ends_at_zv_p = 1;
12830 break;
12831 }
12832
12833 /* Now, get the metrics of what we want to display. This also
12834 generates glyphs in `row' (which is IT->glyph_row). */
12835 n_glyphs_before = row->used[TEXT_AREA];
12836 x = it->current_x;
12837
12838 /* Remember the line height so far in case the next element doesn't
12839 fit on the line. */
12840 if (!it->truncate_lines_p)
12841 {
12842 ascent = it->max_ascent;
12843 descent = it->max_descent;
12844 phys_ascent = it->max_phys_ascent;
12845 phys_descent = it->max_phys_descent;
12846 }
12847
12848 PRODUCE_GLYPHS (it);
12849
12850 /* If this display element was in marginal areas, continue with
12851 the next one. */
12852 if (it->area != TEXT_AREA)
12853 {
12854 row->ascent = max (row->ascent, it->max_ascent);
12855 row->height = max (row->height, it->max_ascent + it->max_descent);
12856 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12857 row->phys_height = max (row->phys_height,
12858 it->max_phys_ascent + it->max_phys_descent);
12859 set_iterator_to_next (it, 1);
12860 continue;
12861 }
12862
12863 /* Does the display element fit on the line? If we truncate
12864 lines, we should draw past the right edge of the window. If
12865 we don't truncate, we want to stop so that we can display the
12866 continuation glyph before the right margin. If lines are
12867 continued, there are two possible strategies for characters
12868 resulting in more than 1 glyph (e.g. tabs): Display as many
12869 glyphs as possible in this line and leave the rest for the
12870 continuation line, or display the whole element in the next
12871 line. Original redisplay did the former, so we do it also. */
12872 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
12873 hpos_before = it->hpos;
12874 x_before = x;
12875
12876 if (/* Not a newline. */
12877 nglyphs > 0
12878 /* Glyphs produced fit entirely in the line. */
12879 && it->current_x < it->last_visible_x)
12880 {
12881 it->hpos += nglyphs;
12882 row->ascent = max (row->ascent, it->max_ascent);
12883 row->height = max (row->height, it->max_ascent + it->max_descent);
12884 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12885 row->phys_height = max (row->phys_height,
12886 it->max_phys_ascent + it->max_phys_descent);
12887 if (it->current_x - it->pixel_width < it->first_visible_x)
12888 row->x = x - it->first_visible_x;
12889 }
12890 else
12891 {
12892 int new_x;
12893 struct glyph *glyph;
12894
12895 for (i = 0; i < nglyphs; ++i, x = new_x)
12896 {
12897 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12898 new_x = x + glyph->pixel_width;
12899
12900 if (/* Lines are continued. */
12901 !it->truncate_lines_p
12902 && (/* Glyph doesn't fit on the line. */
12903 new_x > it->last_visible_x
12904 /* Or it fits exactly on a window system frame. */
12905 || (new_x == it->last_visible_x
12906 && FRAME_WINDOW_P (it->f))))
12907 {
12908 /* End of a continued line. */
12909
12910 if (it->hpos == 0
12911 || (new_x == it->last_visible_x
12912 && FRAME_WINDOW_P (it->f)))
12913 {
12914 /* Current glyph is the only one on the line or
12915 fits exactly on the line. We must continue
12916 the line because we can't draw the cursor
12917 after the glyph. */
12918 row->continued_p = 1;
12919 it->current_x = new_x;
12920 it->continuation_lines_width += new_x;
12921 ++it->hpos;
12922 if (i == nglyphs - 1)
12923 set_iterator_to_next (it, 1);
12924 }
12925 else if (CHAR_GLYPH_PADDING_P (*glyph)
12926 && !FRAME_WINDOW_P (it->f))
12927 {
12928 /* A padding glyph that doesn't fit on this line.
12929 This means the whole character doesn't fit
12930 on the line. */
12931 row->used[TEXT_AREA] = n_glyphs_before;
12932
12933 /* Fill the rest of the row with continuation
12934 glyphs like in 20.x. */
12935 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
12936 < row->glyphs[1 + TEXT_AREA])
12937 produce_special_glyphs (it, IT_CONTINUATION);
12938
12939 row->continued_p = 1;
12940 it->current_x = x_before;
12941 it->continuation_lines_width += x_before;
12942
12943 /* Restore the height to what it was before the
12944 element not fitting on the line. */
12945 it->max_ascent = ascent;
12946 it->max_descent = descent;
12947 it->max_phys_ascent = phys_ascent;
12948 it->max_phys_descent = phys_descent;
12949 }
12950 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
12951 {
12952 /* A TAB that extends past the right edge of the
12953 window. This produces a single glyph on
12954 window system frames. We leave the glyph in
12955 this row and let it fill the row, but don't
12956 consume the TAB. */
12957 it->continuation_lines_width += it->last_visible_x;
12958 row->ends_in_middle_of_char_p = 1;
12959 row->continued_p = 1;
12960 glyph->pixel_width = it->last_visible_x - x;
12961 it->starts_in_middle_of_char_p = 1;
12962 }
12963 else
12964 {
12965 /* Something other than a TAB that draws past
12966 the right edge of the window. Restore
12967 positions to values before the element. */
12968 row->used[TEXT_AREA] = n_glyphs_before + i;
12969
12970 /* Display continuation glyphs. */
12971 if (!FRAME_WINDOW_P (it->f))
12972 produce_special_glyphs (it, IT_CONTINUATION);
12973 row->continued_p = 1;
12974
12975 it->continuation_lines_width += x;
12976
12977 if (nglyphs > 1 && i > 0)
12978 {
12979 row->ends_in_middle_of_char_p = 1;
12980 it->starts_in_middle_of_char_p = 1;
12981 }
12982
12983 /* Restore the height to what it was before the
12984 element not fitting on the line. */
12985 it->max_ascent = ascent;
12986 it->max_descent = descent;
12987 it->max_phys_ascent = phys_ascent;
12988 it->max_phys_descent = phys_descent;
12989 }
12990
12991 break;
12992 }
12993 else if (new_x > it->first_visible_x)
12994 {
12995 /* Increment number of glyphs actually displayed. */
12996 ++it->hpos;
12997
12998 if (x < it->first_visible_x)
12999 /* Glyph is partially visible, i.e. row starts at
13000 negative X position. */
13001 row->x = x - it->first_visible_x;
13002 }
13003 else
13004 {
13005 /* Glyph is completely off the left margin of the
13006 window. This should not happen because of the
13007 move_it_in_display_line at the start of
13008 this function. */
13009 abort ();
13010 }
13011 }
13012
13013 row->ascent = max (row->ascent, it->max_ascent);
13014 row->height = max (row->height, it->max_ascent + it->max_descent);
13015 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13016 row->phys_height = max (row->phys_height,
13017 it->max_phys_ascent + it->max_phys_descent);
13018
13019 /* End of this display line if row is continued. */
13020 if (row->continued_p)
13021 break;
13022 }
13023
13024 /* Is this a line end? If yes, we're also done, after making
13025 sure that a non-default face is extended up to the right
13026 margin of the window. */
13027 if (ITERATOR_AT_END_OF_LINE_P (it))
13028 {
13029 int used_before = row->used[TEXT_AREA];
13030
13031 row->ends_in_newline_from_string_p = STRINGP (it->object);
13032
13033 /* Add a space at the end of the line that is used to
13034 display the cursor there. */
13035 append_space (it, 0);
13036
13037 /* Extend the face to the end of the line. */
13038 extend_face_to_end_of_line (it);
13039
13040 /* Make sure we have the position. */
13041 if (used_before == 0)
13042 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
13043
13044 /* Consume the line end. This skips over invisible lines. */
13045 set_iterator_to_next (it, 1);
13046 it->continuation_lines_width = 0;
13047 break;
13048 }
13049
13050 /* Proceed with next display element. Note that this skips
13051 over lines invisible because of selective display. */
13052 set_iterator_to_next (it, 1);
13053
13054 /* If we truncate lines, we are done when the last displayed
13055 glyphs reach past the right margin of the window. */
13056 if (it->truncate_lines_p
13057 && (FRAME_WINDOW_P (it->f)
13058 ? (it->current_x >= it->last_visible_x)
13059 : (it->current_x > it->last_visible_x)))
13060 {
13061 /* Maybe add truncation glyphs. */
13062 if (!FRAME_WINDOW_P (it->f))
13063 {
13064 int i, n;
13065
13066 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
13067 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
13068 break;
13069
13070 for (n = row->used[TEXT_AREA]; i < n; ++i)
13071 {
13072 row->used[TEXT_AREA] = i;
13073 produce_special_glyphs (it, IT_TRUNCATION);
13074 }
13075 }
13076
13077 row->truncated_on_right_p = 1;
13078 it->continuation_lines_width = 0;
13079 reseat_at_next_visible_line_start (it, 0);
13080 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
13081 it->hpos = hpos_before;
13082 it->current_x = x_before;
13083 break;
13084 }
13085 }
13086
13087 /* If line is not empty and hscrolled, maybe insert truncation glyphs
13088 at the left window margin. */
13089 if (it->first_visible_x
13090 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
13091 {
13092 if (!FRAME_WINDOW_P (it->f))
13093 insert_left_trunc_glyphs (it);
13094 row->truncated_on_left_p = 1;
13095 }
13096
13097 /* If the start of this line is the overlay arrow-position, then
13098 mark this glyph row as the one containing the overlay arrow.
13099 This is clearly a mess with variable size fonts. It would be
13100 better to let it be displayed like cursors under X. */
13101 if (MARKERP (Voverlay_arrow_position)
13102 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
13103 && (MATRIX_ROW_START_CHARPOS (row)
13104 == marker_position (Voverlay_arrow_position))
13105 && STRINGP (Voverlay_arrow_string)
13106 && ! overlay_arrow_seen)
13107 {
13108 /* Overlay arrow in window redisplay is a fringe bitmap. */
13109 if (!FRAME_WINDOW_P (it->f))
13110 {
13111 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
13112 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
13113 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
13114 struct glyph *p = row->glyphs[TEXT_AREA];
13115 struct glyph *p2, *end;
13116
13117 /* Copy the arrow glyphs. */
13118 while (glyph < arrow_end)
13119 *p++ = *glyph++;
13120
13121 /* Throw away padding glyphs. */
13122 p2 = p;
13123 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
13124 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
13125 ++p2;
13126 if (p2 > p)
13127 {
13128 while (p2 < end)
13129 *p++ = *p2++;
13130 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
13131 }
13132 }
13133
13134 overlay_arrow_seen = 1;
13135 row->overlay_arrow_p = 1;
13136 }
13137
13138 /* Compute pixel dimensions of this line. */
13139 compute_line_metrics (it);
13140
13141 /* Remember the position at which this line ends. */
13142 row->end = it->current;
13143
13144 /* Maybe set the cursor. */
13145 if (it->w->cursor.vpos < 0
13146 && PT >= MATRIX_ROW_START_CHARPOS (row)
13147 && PT <= MATRIX_ROW_END_CHARPOS (row)
13148 && cursor_row_p (it->w, row))
13149 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
13150
13151 /* Highlight trailing whitespace. */
13152 if (!NILP (Vshow_trailing_whitespace))
13153 highlight_trailing_whitespace (it->f, it->glyph_row);
13154
13155 /* Prepare for the next line. This line starts horizontally at (X
13156 HPOS) = (0 0). Vertical positions are incremented. As a
13157 convenience for the caller, IT->glyph_row is set to the next
13158 row to be used. */
13159 it->current_x = it->hpos = 0;
13160 it->current_y += row->height;
13161 ++it->vpos;
13162 ++it->glyph_row;
13163 return row->displays_text_p;
13164 }
13165
13166
13167 \f
13168 /***********************************************************************
13169 Menu Bar
13170 ***********************************************************************/
13171
13172 /* Redisplay the menu bar in the frame for window W.
13173
13174 The menu bar of X frames that don't have X toolkit support is
13175 displayed in a special window W->frame->menu_bar_window.
13176
13177 The menu bar of terminal frames is treated specially as far as
13178 glyph matrices are concerned. Menu bar lines are not part of
13179 windows, so the update is done directly on the frame matrix rows
13180 for the menu bar. */
13181
13182 static void
13183 display_menu_bar (w)
13184 struct window *w;
13185 {
13186 struct frame *f = XFRAME (WINDOW_FRAME (w));
13187 struct it it;
13188 Lisp_Object items;
13189 int i;
13190
13191 /* Don't do all this for graphical frames. */
13192 #ifdef HAVE_NTGUI
13193 if (!NILP (Vwindow_system))
13194 return;
13195 #endif
13196 #ifdef USE_X_TOOLKIT
13197 if (FRAME_X_P (f))
13198 return;
13199 #endif
13200 #ifdef macintosh
13201 if (FRAME_MAC_P (f))
13202 return;
13203 #endif
13204
13205 #ifdef USE_X_TOOLKIT
13206 xassert (!FRAME_WINDOW_P (f));
13207 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
13208 it.first_visible_x = 0;
13209 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
13210 #else /* not USE_X_TOOLKIT */
13211 if (FRAME_WINDOW_P (f))
13212 {
13213 /* Menu bar lines are displayed in the desired matrix of the
13214 dummy window menu_bar_window. */
13215 struct window *menu_w;
13216 xassert (WINDOWP (f->menu_bar_window));
13217 menu_w = XWINDOW (f->menu_bar_window);
13218 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
13219 MENU_FACE_ID);
13220 it.first_visible_x = 0;
13221 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
13222 }
13223 else
13224 {
13225 /* This is a TTY frame, i.e. character hpos/vpos are used as
13226 pixel x/y. */
13227 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
13228 MENU_FACE_ID);
13229 it.first_visible_x = 0;
13230 it.last_visible_x = FRAME_WIDTH (f);
13231 }
13232 #endif /* not USE_X_TOOLKIT */
13233
13234 if (! mode_line_inverse_video)
13235 /* Force the menu-bar to be displayed in the default face. */
13236 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
13237
13238 /* Clear all rows of the menu bar. */
13239 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
13240 {
13241 struct glyph_row *row = it.glyph_row + i;
13242 clear_glyph_row (row);
13243 row->enabled_p = 1;
13244 row->full_width_p = 1;
13245 }
13246
13247 /* Display all items of the menu bar. */
13248 items = FRAME_MENU_BAR_ITEMS (it.f);
13249 for (i = 0; i < XVECTOR (items)->size; i += 4)
13250 {
13251 Lisp_Object string;
13252
13253 /* Stop at nil string. */
13254 string = AREF (items, i + 1);
13255 if (NILP (string))
13256 break;
13257
13258 /* Remember where item was displayed. */
13259 AREF (items, i + 3) = make_number (it.hpos);
13260
13261 /* Display the item, pad with one space. */
13262 if (it.current_x < it.last_visible_x)
13263 display_string (NULL, string, Qnil, 0, 0, &it,
13264 XSTRING (string)->size + 1, 0, 0, -1);
13265 }
13266
13267 /* Fill out the line with spaces. */
13268 if (it.current_x < it.last_visible_x)
13269 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
13270
13271 /* Compute the total height of the lines. */
13272 compute_line_metrics (&it);
13273 }
13274
13275
13276 \f
13277 /***********************************************************************
13278 Mode Line
13279 ***********************************************************************/
13280
13281 /* Redisplay mode lines in the window tree whose root is WINDOW. If
13282 FORCE is non-zero, redisplay mode lines unconditionally.
13283 Otherwise, redisplay only mode lines that are garbaged. Value is
13284 the number of windows whose mode lines were redisplayed. */
13285
13286 static int
13287 redisplay_mode_lines (window, force)
13288 Lisp_Object window;
13289 int force;
13290 {
13291 int nwindows = 0;
13292
13293 while (!NILP (window))
13294 {
13295 struct window *w = XWINDOW (window);
13296
13297 if (WINDOWP (w->hchild))
13298 nwindows += redisplay_mode_lines (w->hchild, force);
13299 else if (WINDOWP (w->vchild))
13300 nwindows += redisplay_mode_lines (w->vchild, force);
13301 else if (force
13302 || FRAME_GARBAGED_P (XFRAME (w->frame))
13303 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
13304 {
13305 struct text_pos lpoint;
13306 struct buffer *old = current_buffer;
13307
13308 /* Set the window's buffer for the mode line display. */
13309 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13310 set_buffer_internal_1 (XBUFFER (w->buffer));
13311
13312 /* Point refers normally to the selected window. For any
13313 other window, set up appropriate value. */
13314 if (!EQ (window, selected_window))
13315 {
13316 struct text_pos pt;
13317
13318 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
13319 if (CHARPOS (pt) < BEGV)
13320 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
13321 else if (CHARPOS (pt) > (ZV - 1))
13322 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
13323 else
13324 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
13325 }
13326
13327 /* Display mode lines. */
13328 clear_glyph_matrix (w->desired_matrix);
13329 if (display_mode_lines (w))
13330 {
13331 ++nwindows;
13332 w->must_be_updated_p = 1;
13333 }
13334
13335 /* Restore old settings. */
13336 set_buffer_internal_1 (old);
13337 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13338 }
13339
13340 window = w->next;
13341 }
13342
13343 return nwindows;
13344 }
13345
13346
13347 /* Display the mode and/or top line of window W. Value is the number
13348 of mode lines displayed. */
13349
13350 static int
13351 display_mode_lines (w)
13352 struct window *w;
13353 {
13354 Lisp_Object old_selected_window, old_selected_frame;
13355 int n = 0;
13356
13357 old_selected_frame = selected_frame;
13358 selected_frame = w->frame;
13359 old_selected_window = selected_window;
13360 XSETWINDOW (selected_window, w);
13361
13362 /* These will be set while the mode line specs are processed. */
13363 line_number_displayed = 0;
13364 w->column_number_displayed = Qnil;
13365
13366 if (WINDOW_WANTS_MODELINE_P (w))
13367 {
13368 display_mode_line (w, MODE_LINE_FACE_ID,
13369 current_buffer->mode_line_format);
13370 ++n;
13371 }
13372
13373 if (WINDOW_WANTS_HEADER_LINE_P (w))
13374 {
13375 display_mode_line (w, HEADER_LINE_FACE_ID,
13376 current_buffer->header_line_format);
13377 ++n;
13378 }
13379
13380 selected_frame = old_selected_frame;
13381 selected_window = old_selected_window;
13382 return n;
13383 }
13384
13385
13386 /* Display mode or top line of window W. FACE_ID specifies which line
13387 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
13388 FORMAT is the mode line format to display. Value is the pixel
13389 height of the mode line displayed. */
13390
13391 static int
13392 display_mode_line (w, face_id, format)
13393 struct window *w;
13394 enum face_id face_id;
13395 Lisp_Object format;
13396 {
13397 struct it it;
13398 struct face *face;
13399
13400 init_iterator (&it, w, -1, -1, NULL, face_id);
13401 prepare_desired_row (it.glyph_row);
13402
13403 if (! mode_line_inverse_video)
13404 /* Force the mode-line to be displayed in the default face. */
13405 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
13406
13407 /* Temporarily make frame's keyboard the current kboard so that
13408 kboard-local variables in the mode_line_format will get the right
13409 values. */
13410 push_frame_kboard (it.f);
13411 display_mode_element (&it, 0, 0, 0, format);
13412 pop_frame_kboard ();
13413
13414 /* Fill up with spaces. */
13415 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
13416
13417 compute_line_metrics (&it);
13418 it.glyph_row->full_width_p = 1;
13419 it.glyph_row->mode_line_p = 1;
13420 it.glyph_row->continued_p = 0;
13421 it.glyph_row->truncated_on_left_p = 0;
13422 it.glyph_row->truncated_on_right_p = 0;
13423
13424 /* Make a 3D mode-line have a shadow at its right end. */
13425 face = FACE_FROM_ID (it.f, face_id);
13426 extend_face_to_end_of_line (&it);
13427 if (face->box != FACE_NO_BOX)
13428 {
13429 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
13430 + it.glyph_row->used[TEXT_AREA] - 1);
13431 last->right_box_line_p = 1;
13432 }
13433
13434 return it.glyph_row->height;
13435 }
13436
13437
13438 /* Contribute ELT to the mode line for window IT->w. How it
13439 translates into text depends on its data type.
13440
13441 IT describes the display environment in which we display, as usual.
13442
13443 DEPTH is the depth in recursion. It is used to prevent
13444 infinite recursion here.
13445
13446 FIELD_WIDTH is the number of characters the display of ELT should
13447 occupy in the mode line, and PRECISION is the maximum number of
13448 characters to display from ELT's representation. See
13449 display_string for details. *
13450
13451 Returns the hpos of the end of the text generated by ELT. */
13452
13453 static int
13454 display_mode_element (it, depth, field_width, precision, elt)
13455 struct it *it;
13456 int depth;
13457 int field_width, precision;
13458 Lisp_Object elt;
13459 {
13460 int n = 0, field, prec;
13461
13462 tail_recurse:
13463 if (depth > 10)
13464 goto invalid;
13465
13466 depth++;
13467
13468 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
13469 {
13470 case Lisp_String:
13471 {
13472 /* A string: output it and check for %-constructs within it. */
13473 unsigned char c;
13474 unsigned char *this = XSTRING (elt)->data;
13475 unsigned char *lisp_string = this;
13476
13477 while ((precision <= 0 || n < precision)
13478 && *this
13479 && (frame_title_ptr
13480 || it->current_x < it->last_visible_x))
13481 {
13482 unsigned char *last = this;
13483
13484 /* Advance to end of string or next format specifier. */
13485 while ((c = *this++) != '\0' && c != '%')
13486 ;
13487
13488 if (this - 1 != last)
13489 {
13490 /* Output to end of string or up to '%'. Field width
13491 is length of string. Don't output more than
13492 PRECISION allows us. */
13493 --this;
13494
13495 prec = chars_in_text (last, this - last);
13496 if (precision > 0 && prec > precision - n)
13497 prec = precision - n;
13498
13499 if (frame_title_ptr)
13500 n += store_frame_title (last, 0, prec);
13501 else
13502 {
13503 int bytepos = last - lisp_string;
13504 int charpos = string_byte_to_char (elt, bytepos);
13505 n += display_string (NULL, elt, Qnil, 0, charpos,
13506 it, 0, prec, 0,
13507 STRING_MULTIBYTE (elt));
13508 }
13509 }
13510 else /* c == '%' */
13511 {
13512 unsigned char *percent_position = this;
13513
13514 /* Get the specified minimum width. Zero means
13515 don't pad. */
13516 field = 0;
13517 while ((c = *this++) >= '0' && c <= '9')
13518 field = field * 10 + c - '0';
13519
13520 /* Don't pad beyond the total padding allowed. */
13521 if (field_width - n > 0 && field > field_width - n)
13522 field = field_width - n;
13523
13524 /* Note that either PRECISION <= 0 or N < PRECISION. */
13525 prec = precision - n;
13526
13527 if (c == 'M')
13528 n += display_mode_element (it, depth, field, prec,
13529 Vglobal_mode_string);
13530 else if (c != 0)
13531 {
13532 int multibyte;
13533 unsigned char *spec
13534 = decode_mode_spec (it->w, c, field, prec, &multibyte);
13535
13536 if (frame_title_ptr)
13537 n += store_frame_title (spec, field, prec);
13538 else
13539 {
13540 int nglyphs_before, bytepos, charpos, nwritten;
13541
13542 nglyphs_before = it->glyph_row->used[TEXT_AREA];
13543 bytepos = percent_position - XSTRING (elt)->data;
13544 charpos = (multibyte
13545 ? string_byte_to_char (elt, bytepos)
13546 : bytepos);
13547 nwritten = display_string (spec, Qnil, elt,
13548 charpos, 0, it,
13549 field, prec, 0,
13550 multibyte);
13551
13552 /* Assign to the glyphs written above the
13553 string where the `%x' came from, position
13554 of the `%'. */
13555 if (nwritten > 0)
13556 {
13557 struct glyph *glyph
13558 = (it->glyph_row->glyphs[TEXT_AREA]
13559 + nglyphs_before);
13560 int i;
13561
13562 for (i = 0; i < nwritten; ++i)
13563 {
13564 glyph[i].object = elt;
13565 glyph[i].charpos = charpos;
13566 }
13567
13568 n += nwritten;
13569 }
13570 }
13571 }
13572 }
13573 }
13574 }
13575 break;
13576
13577 case Lisp_Symbol:
13578 /* A symbol: process the value of the symbol recursively
13579 as if it appeared here directly. Avoid error if symbol void.
13580 Special case: if value of symbol is a string, output the string
13581 literally. */
13582 {
13583 register Lisp_Object tem;
13584 tem = Fboundp (elt);
13585 if (!NILP (tem))
13586 {
13587 tem = Fsymbol_value (elt);
13588 /* If value is a string, output that string literally:
13589 don't check for % within it. */
13590 if (STRINGP (tem))
13591 {
13592 prec = precision - n;
13593 if (frame_title_ptr)
13594 n += store_frame_title (XSTRING (tem)->data, -1, prec);
13595 else
13596 n += display_string (NULL, tem, Qnil, 0, 0, it,
13597 0, prec, 0, STRING_MULTIBYTE (tem));
13598 }
13599 else if (!EQ (tem, elt))
13600 {
13601 /* Give up right away for nil or t. */
13602 elt = tem;
13603 goto tail_recurse;
13604 }
13605 }
13606 }
13607 break;
13608
13609 case Lisp_Cons:
13610 {
13611 register Lisp_Object car, tem;
13612
13613 /* A cons cell: three distinct cases.
13614 If first element is a string or a cons, process all the elements
13615 and effectively concatenate them.
13616 If first element is a negative number, truncate displaying cdr to
13617 at most that many characters. If positive, pad (with spaces)
13618 to at least that many characters.
13619 If first element is a symbol, process the cadr or caddr recursively
13620 according to whether the symbol's value is non-nil or nil. */
13621 car = XCAR (elt);
13622 if (EQ (car, QCeval) && CONSP (XCDR (elt)))
13623 {
13624 /* An element of the form (:eval FORM) means evaluate FORM
13625 and use the result as mode line elements. */
13626 struct gcpro gcpro1;
13627 Lisp_Object spec;
13628
13629 spec = safe_eval (XCAR (XCDR (elt)));
13630 GCPRO1 (spec);
13631 n += display_mode_element (it, depth, field_width - n,
13632 precision - n, spec);
13633 UNGCPRO;
13634 }
13635 else if (SYMBOLP (car))
13636 {
13637 tem = Fboundp (car);
13638 elt = XCDR (elt);
13639 if (!CONSP (elt))
13640 goto invalid;
13641 /* elt is now the cdr, and we know it is a cons cell.
13642 Use its car if CAR has a non-nil value. */
13643 if (!NILP (tem))
13644 {
13645 tem = Fsymbol_value (car);
13646 if (!NILP (tem))
13647 {
13648 elt = XCAR (elt);
13649 goto tail_recurse;
13650 }
13651 }
13652 /* Symbol's value is nil (or symbol is unbound)
13653 Get the cddr of the original list
13654 and if possible find the caddr and use that. */
13655 elt = XCDR (elt);
13656 if (NILP (elt))
13657 break;
13658 else if (!CONSP (elt))
13659 goto invalid;
13660 elt = XCAR (elt);
13661 goto tail_recurse;
13662 }
13663 else if (INTEGERP (car))
13664 {
13665 register int lim = XINT (car);
13666 elt = XCDR (elt);
13667 if (lim < 0)
13668 {
13669 /* Negative int means reduce maximum width. */
13670 if (precision <= 0)
13671 precision = -lim;
13672 else
13673 precision = min (precision, -lim);
13674 }
13675 else if (lim > 0)
13676 {
13677 /* Padding specified. Don't let it be more than
13678 current maximum. */
13679 if (precision > 0)
13680 lim = min (precision, lim);
13681
13682 /* If that's more padding than already wanted, queue it.
13683 But don't reduce padding already specified even if
13684 that is beyond the current truncation point. */
13685 field_width = max (lim, field_width);
13686 }
13687 goto tail_recurse;
13688 }
13689 else if (STRINGP (car) || CONSP (car))
13690 {
13691 register int limit = 50;
13692 /* Limit is to protect against circular lists. */
13693 while (CONSP (elt)
13694 && --limit > 0
13695 && (precision <= 0 || n < precision))
13696 {
13697 n += display_mode_element (it, depth, field_width - n,
13698 precision - n, XCAR (elt));
13699 elt = XCDR (elt);
13700 }
13701 }
13702 }
13703 break;
13704
13705 default:
13706 invalid:
13707 if (frame_title_ptr)
13708 n += store_frame_title ("*invalid*", 0, precision - n);
13709 else
13710 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
13711 precision - n, 0, 0);
13712 return n;
13713 }
13714
13715 /* Pad to FIELD_WIDTH. */
13716 if (field_width > 0 && n < field_width)
13717 {
13718 if (frame_title_ptr)
13719 n += store_frame_title ("", field_width - n, 0);
13720 else
13721 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
13722 0, 0, 0);
13723 }
13724
13725 return n;
13726 }
13727
13728
13729 /* Write a null-terminated, right justified decimal representation of
13730 the positive integer D to BUF using a minimal field width WIDTH. */
13731
13732 static void
13733 pint2str (buf, width, d)
13734 register char *buf;
13735 register int width;
13736 register int d;
13737 {
13738 register char *p = buf;
13739
13740 if (d <= 0)
13741 *p++ = '0';
13742 else
13743 {
13744 while (d > 0)
13745 {
13746 *p++ = d % 10 + '0';
13747 d /= 10;
13748 }
13749 }
13750
13751 for (width -= (int) (p - buf); width > 0; --width)
13752 *p++ = ' ';
13753 *p-- = '\0';
13754 while (p > buf)
13755 {
13756 d = *buf;
13757 *buf++ = *p;
13758 *p-- = d;
13759 }
13760 }
13761
13762 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
13763 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
13764 type of CODING_SYSTEM. Return updated pointer into BUF. */
13765
13766 static unsigned char invalid_eol_type[] = "(*invalid*)";
13767
13768 static char *
13769 decode_mode_spec_coding (coding_system, buf, eol_flag)
13770 Lisp_Object coding_system;
13771 register char *buf;
13772 int eol_flag;
13773 {
13774 Lisp_Object val;
13775 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
13776 unsigned char *eol_str;
13777 int eol_str_len;
13778 /* The EOL conversion we are using. */
13779 Lisp_Object eoltype;
13780
13781 val = Fget (coding_system, Qcoding_system);
13782 eoltype = Qnil;
13783
13784 if (!VECTORP (val)) /* Not yet decided. */
13785 {
13786 if (multibyte)
13787 *buf++ = '-';
13788 if (eol_flag)
13789 eoltype = eol_mnemonic_undecided;
13790 /* Don't mention EOL conversion if it isn't decided. */
13791 }
13792 else
13793 {
13794 Lisp_Object eolvalue;
13795
13796 eolvalue = Fget (coding_system, Qeol_type);
13797
13798 if (multibyte)
13799 *buf++ = XFASTINT (AREF (val, 1));
13800
13801 if (eol_flag)
13802 {
13803 /* The EOL conversion that is normal on this system. */
13804
13805 if (NILP (eolvalue)) /* Not yet decided. */
13806 eoltype = eol_mnemonic_undecided;
13807 else if (VECTORP (eolvalue)) /* Not yet decided. */
13808 eoltype = eol_mnemonic_undecided;
13809 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
13810 eoltype = (XFASTINT (eolvalue) == 0
13811 ? eol_mnemonic_unix
13812 : (XFASTINT (eolvalue) == 1
13813 ? eol_mnemonic_dos : eol_mnemonic_mac));
13814 }
13815 }
13816
13817 if (eol_flag)
13818 {
13819 /* Mention the EOL conversion if it is not the usual one. */
13820 if (STRINGP (eoltype))
13821 {
13822 eol_str = XSTRING (eoltype)->data;
13823 eol_str_len = XSTRING (eoltype)->size;
13824 }
13825 else if (INTEGERP (eoltype)
13826 && CHAR_VALID_P (XINT (eoltype), 0))
13827 {
13828 eol_str = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
13829 eol_str_len = CHAR_STRING (XINT (eoltype), eol_str);
13830 }
13831 else
13832 {
13833 eol_str = invalid_eol_type;
13834 eol_str_len = sizeof (invalid_eol_type) - 1;
13835 }
13836 bcopy (eol_str, buf, eol_str_len);
13837 buf += eol_str_len;
13838 }
13839
13840 return buf;
13841 }
13842
13843 /* Return a string for the output of a mode line %-spec for window W,
13844 generated by character C. PRECISION >= 0 means don't return a
13845 string longer than that value. FIELD_WIDTH > 0 means pad the
13846 string returned with spaces to that value. Return 1 in *MULTIBYTE
13847 if the result is multibyte text. */
13848
13849 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
13850
13851 static char *
13852 decode_mode_spec (w, c, field_width, precision, multibyte)
13853 struct window *w;
13854 register int c;
13855 int field_width, precision;
13856 int *multibyte;
13857 {
13858 Lisp_Object obj;
13859 struct frame *f = XFRAME (WINDOW_FRAME (w));
13860 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
13861 struct buffer *b = XBUFFER (w->buffer);
13862
13863 obj = Qnil;
13864 *multibyte = 0;
13865
13866 switch (c)
13867 {
13868 case '*':
13869 if (!NILP (b->read_only))
13870 return "%";
13871 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
13872 return "*";
13873 return "-";
13874
13875 case '+':
13876 /* This differs from %* only for a modified read-only buffer. */
13877 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
13878 return "*";
13879 if (!NILP (b->read_only))
13880 return "%";
13881 return "-";
13882
13883 case '&':
13884 /* This differs from %* in ignoring read-only-ness. */
13885 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
13886 return "*";
13887 return "-";
13888
13889 case '%':
13890 return "%";
13891
13892 case '[':
13893 {
13894 int i;
13895 char *p;
13896
13897 if (command_loop_level > 5)
13898 return "[[[... ";
13899 p = decode_mode_spec_buf;
13900 for (i = 0; i < command_loop_level; i++)
13901 *p++ = '[';
13902 *p = 0;
13903 return decode_mode_spec_buf;
13904 }
13905
13906 case ']':
13907 {
13908 int i;
13909 char *p;
13910
13911 if (command_loop_level > 5)
13912 return " ...]]]";
13913 p = decode_mode_spec_buf;
13914 for (i = 0; i < command_loop_level; i++)
13915 *p++ = ']';
13916 *p = 0;
13917 return decode_mode_spec_buf;
13918 }
13919
13920 case '-':
13921 {
13922 register int i;
13923
13924 /* Let lots_of_dashes be a string of infinite length. */
13925 if (field_width <= 0
13926 || field_width > sizeof (lots_of_dashes))
13927 {
13928 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
13929 decode_mode_spec_buf[i] = '-';
13930 decode_mode_spec_buf[i] = '\0';
13931 return decode_mode_spec_buf;
13932 }
13933 else
13934 return lots_of_dashes;
13935 }
13936
13937 case 'b':
13938 obj = b->name;
13939 break;
13940
13941 case 'c':
13942 {
13943 int col = current_column ();
13944 w->column_number_displayed = make_number (col);
13945 pint2str (decode_mode_spec_buf, field_width, col);
13946 return decode_mode_spec_buf;
13947 }
13948
13949 case 'F':
13950 /* %F displays the frame name. */
13951 if (!NILP (f->title))
13952 return (char *) XSTRING (f->title)->data;
13953 if (f->explicit_name || ! FRAME_WINDOW_P (f))
13954 return (char *) XSTRING (f->name)->data;
13955 return "Emacs";
13956
13957 case 'f':
13958 obj = b->filename;
13959 break;
13960
13961 case 'l':
13962 {
13963 int startpos = XMARKER (w->start)->charpos;
13964 int startpos_byte = marker_byte_position (w->start);
13965 int line, linepos, linepos_byte, topline;
13966 int nlines, junk;
13967 int height = XFASTINT (w->height);
13968
13969 /* If we decided that this buffer isn't suitable for line numbers,
13970 don't forget that too fast. */
13971 if (EQ (w->base_line_pos, w->buffer))
13972 goto no_value;
13973 /* But do forget it, if the window shows a different buffer now. */
13974 else if (BUFFERP (w->base_line_pos))
13975 w->base_line_pos = Qnil;
13976
13977 /* If the buffer is very big, don't waste time. */
13978 if (INTEGERP (Vline_number_display_limit)
13979 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
13980 {
13981 w->base_line_pos = Qnil;
13982 w->base_line_number = Qnil;
13983 goto no_value;
13984 }
13985
13986 if (!NILP (w->base_line_number)
13987 && !NILP (w->base_line_pos)
13988 && XFASTINT (w->base_line_pos) <= startpos)
13989 {
13990 line = XFASTINT (w->base_line_number);
13991 linepos = XFASTINT (w->base_line_pos);
13992 linepos_byte = buf_charpos_to_bytepos (b, linepos);
13993 }
13994 else
13995 {
13996 line = 1;
13997 linepos = BUF_BEGV (b);
13998 linepos_byte = BUF_BEGV_BYTE (b);
13999 }
14000
14001 /* Count lines from base line to window start position. */
14002 nlines = display_count_lines (linepos, linepos_byte,
14003 startpos_byte,
14004 startpos, &junk);
14005
14006 topline = nlines + line;
14007
14008 /* Determine a new base line, if the old one is too close
14009 or too far away, or if we did not have one.
14010 "Too close" means it's plausible a scroll-down would
14011 go back past it. */
14012 if (startpos == BUF_BEGV (b))
14013 {
14014 w->base_line_number = make_number (topline);
14015 w->base_line_pos = make_number (BUF_BEGV (b));
14016 }
14017 else if (nlines < height + 25 || nlines > height * 3 + 50
14018 || linepos == BUF_BEGV (b))
14019 {
14020 int limit = BUF_BEGV (b);
14021 int limit_byte = BUF_BEGV_BYTE (b);
14022 int position;
14023 int distance = (height * 2 + 30) * line_number_display_limit_width;
14024
14025 if (startpos - distance > limit)
14026 {
14027 limit = startpos - distance;
14028 limit_byte = CHAR_TO_BYTE (limit);
14029 }
14030
14031 nlines = display_count_lines (startpos, startpos_byte,
14032 limit_byte,
14033 - (height * 2 + 30),
14034 &position);
14035 /* If we couldn't find the lines we wanted within
14036 line_number_display_limit_width chars per line,
14037 give up on line numbers for this window. */
14038 if (position == limit_byte && limit == startpos - distance)
14039 {
14040 w->base_line_pos = w->buffer;
14041 w->base_line_number = Qnil;
14042 goto no_value;
14043 }
14044
14045 w->base_line_number = make_number (topline - nlines);
14046 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
14047 }
14048
14049 /* Now count lines from the start pos to point. */
14050 nlines = display_count_lines (startpos, startpos_byte,
14051 PT_BYTE, PT, &junk);
14052
14053 /* Record that we did display the line number. */
14054 line_number_displayed = 1;
14055
14056 /* Make the string to show. */
14057 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
14058 return decode_mode_spec_buf;
14059 no_value:
14060 {
14061 char* p = decode_mode_spec_buf;
14062 int pad = field_width - 2;
14063 while (pad-- > 0)
14064 *p++ = ' ';
14065 *p++ = '?';
14066 *p++ = '?';
14067 *p = '\0';
14068 return decode_mode_spec_buf;
14069 }
14070 }
14071 break;
14072
14073 case 'm':
14074 obj = b->mode_name;
14075 break;
14076
14077 case 'n':
14078 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
14079 return " Narrow";
14080 break;
14081
14082 case 'p':
14083 {
14084 int pos = marker_position (w->start);
14085 int total = BUF_ZV (b) - BUF_BEGV (b);
14086
14087 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
14088 {
14089 if (pos <= BUF_BEGV (b))
14090 return "All";
14091 else
14092 return "Bottom";
14093 }
14094 else if (pos <= BUF_BEGV (b))
14095 return "Top";
14096 else
14097 {
14098 if (total > 1000000)
14099 /* Do it differently for a large value, to avoid overflow. */
14100 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
14101 else
14102 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
14103 /* We can't normally display a 3-digit number,
14104 so get us a 2-digit number that is close. */
14105 if (total == 100)
14106 total = 99;
14107 sprintf (decode_mode_spec_buf, "%2d%%", total);
14108 return decode_mode_spec_buf;
14109 }
14110 }
14111
14112 /* Display percentage of size above the bottom of the screen. */
14113 case 'P':
14114 {
14115 int toppos = marker_position (w->start);
14116 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
14117 int total = BUF_ZV (b) - BUF_BEGV (b);
14118
14119 if (botpos >= BUF_ZV (b))
14120 {
14121 if (toppos <= BUF_BEGV (b))
14122 return "All";
14123 else
14124 return "Bottom";
14125 }
14126 else
14127 {
14128 if (total > 1000000)
14129 /* Do it differently for a large value, to avoid overflow. */
14130 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
14131 else
14132 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
14133 /* We can't normally display a 3-digit number,
14134 so get us a 2-digit number that is close. */
14135 if (total == 100)
14136 total = 99;
14137 if (toppos <= BUF_BEGV (b))
14138 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
14139 else
14140 sprintf (decode_mode_spec_buf, "%2d%%", total);
14141 return decode_mode_spec_buf;
14142 }
14143 }
14144
14145 case 's':
14146 /* status of process */
14147 obj = Fget_buffer_process (w->buffer);
14148 if (NILP (obj))
14149 return "no process";
14150 #ifdef subprocesses
14151 obj = Fsymbol_name (Fprocess_status (obj));
14152 #endif
14153 break;
14154
14155 case 't': /* indicate TEXT or BINARY */
14156 #ifdef MODE_LINE_BINARY_TEXT
14157 return MODE_LINE_BINARY_TEXT (b);
14158 #else
14159 return "T";
14160 #endif
14161
14162 case 'z':
14163 /* coding-system (not including end-of-line format) */
14164 case 'Z':
14165 /* coding-system (including end-of-line type) */
14166 {
14167 int eol_flag = (c == 'Z');
14168 char *p = decode_mode_spec_buf;
14169
14170 if (! FRAME_WINDOW_P (f))
14171 {
14172 /* No need to mention EOL here--the terminal never needs
14173 to do EOL conversion. */
14174 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
14175 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
14176 }
14177 p = decode_mode_spec_coding (b->buffer_file_coding_system,
14178 p, eol_flag);
14179
14180 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
14181 #ifdef subprocesses
14182 obj = Fget_buffer_process (Fcurrent_buffer ());
14183 if (PROCESSP (obj))
14184 {
14185 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
14186 p, eol_flag);
14187 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
14188 p, eol_flag);
14189 }
14190 #endif /* subprocesses */
14191 #endif /* 0 */
14192 *p = 0;
14193 return decode_mode_spec_buf;
14194 }
14195 }
14196
14197 if (STRINGP (obj))
14198 {
14199 *multibyte = STRING_MULTIBYTE (obj);
14200 return (char *) XSTRING (obj)->data;
14201 }
14202 else
14203 return "";
14204 }
14205
14206
14207 /* Count up to COUNT lines starting from START / START_BYTE.
14208 But don't go beyond LIMIT_BYTE.
14209 Return the number of lines thus found (always nonnegative).
14210
14211 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
14212
14213 static int
14214 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
14215 int start, start_byte, limit_byte, count;
14216 int *byte_pos_ptr;
14217 {
14218 register unsigned char *cursor;
14219 unsigned char *base;
14220
14221 register int ceiling;
14222 register unsigned char *ceiling_addr;
14223 int orig_count = count;
14224
14225 /* If we are not in selective display mode,
14226 check only for newlines. */
14227 int selective_display = (!NILP (current_buffer->selective_display)
14228 && !INTEGERP (current_buffer->selective_display));
14229
14230 if (count > 0)
14231 {
14232 while (start_byte < limit_byte)
14233 {
14234 ceiling = BUFFER_CEILING_OF (start_byte);
14235 ceiling = min (limit_byte - 1, ceiling);
14236 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
14237 base = (cursor = BYTE_POS_ADDR (start_byte));
14238 while (1)
14239 {
14240 if (selective_display)
14241 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
14242 ;
14243 else
14244 while (*cursor != '\n' && ++cursor != ceiling_addr)
14245 ;
14246
14247 if (cursor != ceiling_addr)
14248 {
14249 if (--count == 0)
14250 {
14251 start_byte += cursor - base + 1;
14252 *byte_pos_ptr = start_byte;
14253 return orig_count;
14254 }
14255 else
14256 if (++cursor == ceiling_addr)
14257 break;
14258 }
14259 else
14260 break;
14261 }
14262 start_byte += cursor - base;
14263 }
14264 }
14265 else
14266 {
14267 while (start_byte > limit_byte)
14268 {
14269 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
14270 ceiling = max (limit_byte, ceiling);
14271 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
14272 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
14273 while (1)
14274 {
14275 if (selective_display)
14276 while (--cursor != ceiling_addr
14277 && *cursor != '\n' && *cursor != 015)
14278 ;
14279 else
14280 while (--cursor != ceiling_addr && *cursor != '\n')
14281 ;
14282
14283 if (cursor != ceiling_addr)
14284 {
14285 if (++count == 0)
14286 {
14287 start_byte += cursor - base + 1;
14288 *byte_pos_ptr = start_byte;
14289 /* When scanning backwards, we should
14290 not count the newline posterior to which we stop. */
14291 return - orig_count - 1;
14292 }
14293 }
14294 else
14295 break;
14296 }
14297 /* Here we add 1 to compensate for the last decrement
14298 of CURSOR, which took it past the valid range. */
14299 start_byte += cursor - base + 1;
14300 }
14301 }
14302
14303 *byte_pos_ptr = limit_byte;
14304
14305 if (count < 0)
14306 return - orig_count + count;
14307 return orig_count - count;
14308
14309 }
14310
14311
14312 \f
14313 /***********************************************************************
14314 Displaying strings
14315 ***********************************************************************/
14316
14317 /* Display a NUL-terminated string, starting with index START.
14318
14319 If STRING is non-null, display that C string. Otherwise, the Lisp
14320 string LISP_STRING is displayed.
14321
14322 If FACE_STRING is not nil, FACE_STRING_POS is a position in
14323 FACE_STRING. Display STRING or LISP_STRING with the face at
14324 FACE_STRING_POS in FACE_STRING:
14325
14326 Display the string in the environment given by IT, but use the
14327 standard display table, temporarily.
14328
14329 FIELD_WIDTH is the minimum number of output glyphs to produce.
14330 If STRING has fewer characters than FIELD_WIDTH, pad to the right
14331 with spaces. If STRING has more characters, more than FIELD_WIDTH
14332 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
14333
14334 PRECISION is the maximum number of characters to output from
14335 STRING. PRECISION < 0 means don't truncate the string.
14336
14337 This is roughly equivalent to printf format specifiers:
14338
14339 FIELD_WIDTH PRECISION PRINTF
14340 ----------------------------------------
14341 -1 -1 %s
14342 -1 10 %.10s
14343 10 -1 %10s
14344 20 10 %20.10s
14345
14346 MULTIBYTE zero means do not display multibyte chars, > 0 means do
14347 display them, and < 0 means obey the current buffer's value of
14348 enable_multibyte_characters.
14349
14350 Value is the number of glyphs produced. */
14351
14352 static int
14353 display_string (string, lisp_string, face_string, face_string_pos,
14354 start, it, field_width, precision, max_x, multibyte)
14355 unsigned char *string;
14356 Lisp_Object lisp_string;
14357 Lisp_Object face_string;
14358 int face_string_pos;
14359 int start;
14360 struct it *it;
14361 int field_width, precision, max_x;
14362 int multibyte;
14363 {
14364 int hpos_at_start = it->hpos;
14365 int saved_face_id = it->face_id;
14366 struct glyph_row *row = it->glyph_row;
14367
14368 /* Initialize the iterator IT for iteration over STRING beginning
14369 with index START. */
14370 reseat_to_string (it, string, lisp_string, start,
14371 precision, field_width, multibyte);
14372
14373 /* If displaying STRING, set up the face of the iterator
14374 from LISP_STRING, if that's given. */
14375 if (STRINGP (face_string))
14376 {
14377 int endptr;
14378 struct face *face;
14379
14380 it->face_id
14381 = face_at_string_position (it->w, face_string, face_string_pos,
14382 0, it->region_beg_charpos,
14383 it->region_end_charpos,
14384 &endptr, it->base_face_id, 0);
14385 face = FACE_FROM_ID (it->f, it->face_id);
14386 it->face_box_p = face->box != FACE_NO_BOX;
14387 }
14388
14389 /* Set max_x to the maximum allowed X position. Don't let it go
14390 beyond the right edge of the window. */
14391 if (max_x <= 0)
14392 max_x = it->last_visible_x;
14393 else
14394 max_x = min (max_x, it->last_visible_x);
14395
14396 /* Skip over display elements that are not visible. because IT->w is
14397 hscrolled. */
14398 if (it->current_x < it->first_visible_x)
14399 move_it_in_display_line_to (it, 100000, it->first_visible_x,
14400 MOVE_TO_POS | MOVE_TO_X);
14401
14402 row->ascent = it->max_ascent;
14403 row->height = it->max_ascent + it->max_descent;
14404 row->phys_ascent = it->max_phys_ascent;
14405 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14406
14407 /* This condition is for the case that we are called with current_x
14408 past last_visible_x. */
14409 while (it->current_x < max_x)
14410 {
14411 int x_before, x, n_glyphs_before, i, nglyphs;
14412
14413 /* Get the next display element. */
14414 if (!get_next_display_element (it))
14415 break;
14416
14417 /* Produce glyphs. */
14418 x_before = it->current_x;
14419 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
14420 PRODUCE_GLYPHS (it);
14421
14422 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
14423 i = 0;
14424 x = x_before;
14425 while (i < nglyphs)
14426 {
14427 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
14428
14429 if (!it->truncate_lines_p
14430 && x + glyph->pixel_width > max_x)
14431 {
14432 /* End of continued line or max_x reached. */
14433 if (CHAR_GLYPH_PADDING_P (*glyph))
14434 {
14435 /* A wide character is unbreakable. */
14436 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
14437 it->current_x = x_before;
14438 }
14439 else
14440 {
14441 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
14442 it->current_x = x;
14443 }
14444 break;
14445 }
14446 else if (x + glyph->pixel_width > it->first_visible_x)
14447 {
14448 /* Glyph is at least partially visible. */
14449 ++it->hpos;
14450 if (x < it->first_visible_x)
14451 it->glyph_row->x = x - it->first_visible_x;
14452 }
14453 else
14454 {
14455 /* Glyph is off the left margin of the display area.
14456 Should not happen. */
14457 abort ();
14458 }
14459
14460 row->ascent = max (row->ascent, it->max_ascent);
14461 row->height = max (row->height, it->max_ascent + it->max_descent);
14462 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14463 row->phys_height = max (row->phys_height,
14464 it->max_phys_ascent + it->max_phys_descent);
14465 x += glyph->pixel_width;
14466 ++i;
14467 }
14468
14469 /* Stop if max_x reached. */
14470 if (i < nglyphs)
14471 break;
14472
14473 /* Stop at line ends. */
14474 if (ITERATOR_AT_END_OF_LINE_P (it))
14475 {
14476 it->continuation_lines_width = 0;
14477 break;
14478 }
14479
14480 set_iterator_to_next (it, 1);
14481
14482 /* Stop if truncating at the right edge. */
14483 if (it->truncate_lines_p
14484 && it->current_x >= it->last_visible_x)
14485 {
14486 /* Add truncation mark, but don't do it if the line is
14487 truncated at a padding space. */
14488 if (IT_CHARPOS (*it) < it->string_nchars)
14489 {
14490 if (!FRAME_WINDOW_P (it->f))
14491 {
14492 int i, n;
14493
14494 if (it->current_x > it->last_visible_x)
14495 {
14496 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
14497 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
14498 break;
14499 for (n = row->used[TEXT_AREA]; i < n; ++i)
14500 {
14501 row->used[TEXT_AREA] = i;
14502 produce_special_glyphs (it, IT_TRUNCATION);
14503 }
14504 }
14505 produce_special_glyphs (it, IT_TRUNCATION);
14506 }
14507 it->glyph_row->truncated_on_right_p = 1;
14508 }
14509 break;
14510 }
14511 }
14512
14513 /* Maybe insert a truncation at the left. */
14514 if (it->first_visible_x
14515 && IT_CHARPOS (*it) > 0)
14516 {
14517 if (!FRAME_WINDOW_P (it->f))
14518 insert_left_trunc_glyphs (it);
14519 it->glyph_row->truncated_on_left_p = 1;
14520 }
14521
14522 it->face_id = saved_face_id;
14523
14524 /* Value is number of columns displayed. */
14525 return it->hpos - hpos_at_start;
14526 }
14527
14528
14529 \f
14530 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
14531 appears as an element of LIST or as the car of an element of LIST.
14532 If PROPVAL is a list, compare each element against LIST in that
14533 way, and return 1/2 if any element of PROPVAL is found in LIST.
14534 Otherwise return 0. This function cannot quit.
14535 The return value is 2 if the text is invisible but with an ellipsis
14536 and 1 if it's invisible and without an ellipsis. */
14537
14538 int
14539 invisible_p (propval, list)
14540 register Lisp_Object propval;
14541 Lisp_Object list;
14542 {
14543 register Lisp_Object tail, proptail;
14544
14545 for (tail = list; CONSP (tail); tail = XCDR (tail))
14546 {
14547 register Lisp_Object tem;
14548 tem = XCAR (tail);
14549 if (EQ (propval, tem))
14550 return 1;
14551 if (CONSP (tem) && EQ (propval, XCAR (tem)))
14552 return NILP (XCDR (tem)) ? 1 : 2;
14553 }
14554
14555 if (CONSP (propval))
14556 {
14557 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
14558 {
14559 Lisp_Object propelt;
14560 propelt = XCAR (proptail);
14561 for (tail = list; CONSP (tail); tail = XCDR (tail))
14562 {
14563 register Lisp_Object tem;
14564 tem = XCAR (tail);
14565 if (EQ (propelt, tem))
14566 return 1;
14567 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
14568 return NILP (XCDR (tem)) ? 1 : 2;
14569 }
14570 }
14571 }
14572
14573 return 0;
14574 }
14575
14576 \f
14577 /***********************************************************************
14578 Initialization
14579 ***********************************************************************/
14580
14581 void
14582 syms_of_xdisp ()
14583 {
14584 Vwith_echo_area_save_vector = Qnil;
14585 staticpro (&Vwith_echo_area_save_vector);
14586
14587 Vmessage_stack = Qnil;
14588 staticpro (&Vmessage_stack);
14589
14590 Qinhibit_redisplay = intern ("inhibit-redisplay");
14591 staticpro (&Qinhibit_redisplay);
14592
14593 #if GLYPH_DEBUG
14594 defsubr (&Sdump_glyph_matrix);
14595 defsubr (&Sdump_glyph_row);
14596 defsubr (&Sdump_tool_bar_row);
14597 defsubr (&Strace_redisplay);
14598 defsubr (&Strace_to_stderr);
14599 #endif
14600 #ifdef HAVE_WINDOW_SYSTEM
14601 defsubr (&Stool_bar_lines_needed);
14602 #endif
14603
14604 staticpro (&Qmenu_bar_update_hook);
14605 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
14606
14607 staticpro (&Qoverriding_terminal_local_map);
14608 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
14609
14610 staticpro (&Qoverriding_local_map);
14611 Qoverriding_local_map = intern ("overriding-local-map");
14612
14613 staticpro (&Qwindow_scroll_functions);
14614 Qwindow_scroll_functions = intern ("window-scroll-functions");
14615
14616 staticpro (&Qredisplay_end_trigger_functions);
14617 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
14618
14619 staticpro (&Qinhibit_point_motion_hooks);
14620 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
14621
14622 QCdata = intern (":data");
14623 staticpro (&QCdata);
14624 Qdisplay = intern ("display");
14625 staticpro (&Qdisplay);
14626 Qspace_width = intern ("space-width");
14627 staticpro (&Qspace_width);
14628 Qraise = intern ("raise");
14629 staticpro (&Qraise);
14630 Qspace = intern ("space");
14631 staticpro (&Qspace);
14632 Qmargin = intern ("margin");
14633 staticpro (&Qmargin);
14634 Qleft_margin = intern ("left-margin");
14635 staticpro (&Qleft_margin);
14636 Qright_margin = intern ("right-margin");
14637 staticpro (&Qright_margin);
14638 Qalign_to = intern ("align-to");
14639 staticpro (&Qalign_to);
14640 QCalign_to = intern (":align-to");
14641 staticpro (&QCalign_to);
14642 Qrelative_width = intern ("relative-width");
14643 staticpro (&Qrelative_width);
14644 QCrelative_width = intern (":relative-width");
14645 staticpro (&QCrelative_width);
14646 QCrelative_height = intern (":relative-height");
14647 staticpro (&QCrelative_height);
14648 QCeval = intern (":eval");
14649 staticpro (&QCeval);
14650 Qwhen = intern ("when");
14651 staticpro (&Qwhen);
14652 QCfile = intern (":file");
14653 staticpro (&QCfile);
14654 Qfontified = intern ("fontified");
14655 staticpro (&Qfontified);
14656 Qfontification_functions = intern ("fontification-functions");
14657 staticpro (&Qfontification_functions);
14658 Qtrailing_whitespace = intern ("trailing-whitespace");
14659 staticpro (&Qtrailing_whitespace);
14660 Qimage = intern ("image");
14661 staticpro (&Qimage);
14662 Qmessage_truncate_lines = intern ("message-truncate-lines");
14663 staticpro (&Qmessage_truncate_lines);
14664 Qcursor_in_non_selected_windows = intern ("cursor-in-non-selected-windows");
14665 staticpro (&Qcursor_in_non_selected_windows);
14666 Qgrow_only = intern ("grow-only");
14667 staticpro (&Qgrow_only);
14668 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
14669 staticpro (&Qinhibit_menubar_update);
14670 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
14671 staticpro (&Qinhibit_eval_during_redisplay);
14672 Qposition = intern ("position");
14673 staticpro (&Qposition);
14674 Qbuffer_position = intern ("buffer-position");
14675 staticpro (&Qbuffer_position);
14676 Qobject = intern ("object");
14677 staticpro (&Qobject);
14678
14679 last_arrow_position = Qnil;
14680 last_arrow_string = Qnil;
14681 staticpro (&last_arrow_position);
14682 staticpro (&last_arrow_string);
14683
14684 echo_buffer[0] = echo_buffer[1] = Qnil;
14685 staticpro (&echo_buffer[0]);
14686 staticpro (&echo_buffer[1]);
14687
14688 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
14689 staticpro (&echo_area_buffer[0]);
14690 staticpro (&echo_area_buffer[1]);
14691
14692 Vmessages_buffer_name = build_string ("*Messages*");
14693 staticpro (&Vmessages_buffer_name);
14694
14695 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
14696 doc: /* Non-nil means highlight trailing whitespace.
14697 The face used for trailing whitespace is `trailing-whitespace'. */);
14698 Vshow_trailing_whitespace = Qnil;
14699
14700 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
14701 doc: /* Non-nil means don't actually do any redisplay.
14702 This is used for internal purposes. */);
14703 Vinhibit_redisplay = Qnil;
14704
14705 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
14706 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
14707 Vglobal_mode_string = Qnil;
14708
14709 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
14710 doc: /* Marker for where to display an arrow on top of the buffer text.
14711 This must be the beginning of a line in order to work.
14712 See also `overlay-arrow-string'. */);
14713 Voverlay_arrow_position = Qnil;
14714
14715 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
14716 doc: /* String to display as an arrow. See also `overlay-arrow-position'. */);
14717 Voverlay_arrow_string = Qnil;
14718
14719 DEFVAR_INT ("scroll-step", &scroll_step,
14720 doc: /* *The number of lines to try scrolling a window by when point moves out.
14721 If that fails to bring point back on frame, point is centered instead.
14722 If this is zero, point is always centered after it moves off frame.
14723 If you want scrolling to always be a line at a time, you should set
14724 `scroll-conservatively' to a large value rather than set this to 1. */);
14725
14726 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
14727 doc: /* *Scroll up to this many lines, to bring point back on screen.
14728 A value of zero means to scroll the text to center point vertically
14729 in the window. */);
14730 scroll_conservatively = 0;
14731
14732 DEFVAR_INT ("scroll-margin", &scroll_margin,
14733 doc: /* *Number of lines of margin at the top and bottom of a window.
14734 Recenter the window whenever point gets within this many lines
14735 of the top or bottom of the window. */);
14736 scroll_margin = 0;
14737
14738 #if GLYPH_DEBUG
14739 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
14740 #endif
14741
14742 DEFVAR_BOOL ("truncate-partial-width-windows",
14743 &truncate_partial_width_windows,
14744 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
14745 truncate_partial_width_windows = 1;
14746
14747 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
14748 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
14749 Any other value means to use the appropriate face, `mode-line',
14750 `header-line', or `menu' respectively.
14751
14752 This variable is deprecated; please change the above faces instead. */);
14753 mode_line_inverse_video = 1;
14754
14755 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
14756 doc: /* *Maximum buffer size for which line number should be displayed.
14757 If the buffer is bigger than this, the line number does not appear
14758 in the mode line. A value of nil means no limit. */);
14759 Vline_number_display_limit = Qnil;
14760
14761 DEFVAR_INT ("line-number-display-limit-width",
14762 &line_number_display_limit_width,
14763 doc: /* *Maximum line width (in characters) for line number display.
14764 If the average length of the lines near point is bigger than this, then the
14765 line number may be omitted from the mode line. */);
14766 line_number_display_limit_width = 200;
14767
14768 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
14769 doc: /* *Non-nil means highlight region even in nonselected windows. */);
14770 highlight_nonselected_windows = 0;
14771
14772 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
14773 doc: /* Non-nil if more than one frame is visible on this display.
14774 Minibuffer-only frames don't count, but iconified frames do.
14775 This variable is not guaranteed to be accurate except while processing
14776 `frame-title-format' and `icon-title-format'. */);
14777
14778 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
14779 doc: /* Template for displaying the title bar of visible frames.
14780 \(Assuming the window manager supports this feature.)
14781 This variable has the same structure as `mode-line-format' (which see),
14782 and is used only on frames for which no explicit name has been set
14783 \(see `modify-frame-parameters'). */);
14784 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
14785 doc: /* Template for displaying the title bar of an iconified frame.
14786 \(Assuming the window manager supports this feature.)
14787 This variable has the same structure as `mode-line-format' (which see),
14788 and is used only on frames for which no explicit name has been set
14789 \(see `modify-frame-parameters'). */);
14790 Vicon_title_format
14791 = Vframe_title_format
14792 = Fcons (intern ("multiple-frames"),
14793 Fcons (build_string ("%b"),
14794 Fcons (Fcons (empty_string,
14795 Fcons (intern ("invocation-name"),
14796 Fcons (build_string ("@"),
14797 Fcons (intern ("system-name"),
14798 Qnil)))),
14799 Qnil)));
14800
14801 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
14802 doc: /* Maximum number of lines to keep in the message log buffer.
14803 If nil, disable message logging. If t, log messages but don't truncate
14804 the buffer when it becomes large. */);
14805 Vmessage_log_max = make_number (50);
14806
14807 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
14808 doc: /* Functions called before redisplay, if window sizes have changed.
14809 The value should be a list of functions that take one argument.
14810 Just before redisplay, for each frame, if any of its windows have changed
14811 size since the last redisplay, or have been split or deleted,
14812 all the functions in the list are called, with the frame as argument. */);
14813 Vwindow_size_change_functions = Qnil;
14814
14815 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
14816 doc: /* List of Functions to call before redisplaying a window with scrolling.
14817 Each function is called with two arguments, the window
14818 and its new display-start position. Note that the value of `window-end'
14819 is not valid when these functions are called. */);
14820 Vwindow_scroll_functions = Qnil;
14821
14822 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
14823 doc: /* *Non-nil means automatically resize tool-bars.
14824 This increases a tool-bar's height if not all tool-bar items are visible.
14825 It decreases a tool-bar's height when it would display blank lines
14826 otherwise. */);
14827 auto_resize_tool_bars_p = 1;
14828
14829 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
14830 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
14831 auto_raise_tool_bar_buttons_p = 1;
14832
14833 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
14834 doc: /* *Margin around tool-bar buttons in pixels.
14835 If an integer, use that for both horizontal and vertical margins.
14836 Otherwise, value should be a pair of integers `(HORZ : VERT)' with
14837 HORZ specifying the horizontal margin, and VERT specifying the
14838 vertical margin. */);
14839 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
14840
14841 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
14842 doc: /* Relief thickness of tool-bar buttons. */);
14843 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
14844
14845 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
14846 doc: /* List of functions to call to fontify regions of text.
14847 Each function is called with one argument POS. Functions must
14848 fontify a region starting at POS in the current buffer, and give
14849 fontified regions the property `fontified'. */);
14850 Vfontification_functions = Qnil;
14851 Fmake_variable_buffer_local (Qfontification_functions);
14852
14853 DEFVAR_BOOL ("unibyte-display-via-language-environment",
14854 &unibyte_display_via_language_environment,
14855 doc: /* *Non-nil means display unibyte text according to language environment.
14856 Specifically this means that unibyte non-ASCII characters
14857 are displayed by converting them to the equivalent multibyte characters
14858 according to the current language environment. As a result, they are
14859 displayed according to the current fontset. */);
14860 unibyte_display_via_language_environment = 0;
14861
14862 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
14863 doc: /* *Maximum height for resizing mini-windows.
14864 If a float, it specifies a fraction of the mini-window frame's height.
14865 If an integer, it specifies a number of lines. */);
14866 Vmax_mini_window_height = make_float (0.25);
14867
14868 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
14869 doc: /* *How to resize mini-windows.
14870 A value of nil means don't automatically resize mini-windows.
14871 A value of t means resize them to fit the text displayed in them.
14872 A value of `grow-only', the default, means let mini-windows grow
14873 only, until their display becomes empty, at which point the windows
14874 go back to their normal size. */);
14875 Vresize_mini_windows = Qgrow_only;
14876
14877 DEFVAR_BOOL ("cursor-in-non-selected-windows",
14878 &cursor_in_non_selected_windows,
14879 doc: /* *Non-nil means display a hollow cursor in non-selected windows.
14880 Nil means don't display a cursor there. */);
14881 cursor_in_non_selected_windows = 1;
14882
14883 DEFVAR_BOOL ("automatic-hscrolling", &automatic_hscrolling_p,
14884 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
14885 automatic_hscrolling_p = 1;
14886
14887 DEFVAR_LISP ("image-types", &Vimage_types,
14888 doc: /* List of supported image types.
14889 Each element of the list is a symbol for a supported image type. */);
14890 Vimage_types = Qnil;
14891
14892 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
14893 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
14894 Bind this around calls to `message' to let it take effect. */);
14895 message_truncate_lines = 0;
14896
14897 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
14898 doc: /* Normal hook run for clicks on menu bar, before displaying a submenu.
14899 Can be used to update submenus whose contents should vary. */);
14900 Vmenu_bar_update_hook = Qnil;
14901
14902 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
14903 doc: /* Non-nil means don't update menu bars. Internal use only. */);
14904 inhibit_menubar_update = 0;
14905
14906 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
14907 doc: /* Non-nil means don't eval Lisp during redisplay. */);
14908 inhibit_eval_during_redisplay = 0;
14909
14910 #if GLYPH_DEBUG
14911 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
14912 doc: /* Inhibit try_window_id display optimization. */);
14913 inhibit_try_window_id = 0;
14914
14915 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
14916 doc: /* Inhibit try_window_reusing display optimization. */);
14917 inhibit_try_window_reusing = 0;
14918
14919 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
14920 doc: /* Inhibit try_cursor_movement display optimization. */);
14921 inhibit_try_cursor_movement = 0;
14922 #endif /* GLYPH_DEBUG */
14923 }
14924
14925
14926 /* Initialize this module when Emacs starts. */
14927
14928 void
14929 init_xdisp ()
14930 {
14931 Lisp_Object root_window;
14932 struct window *mini_w;
14933
14934 current_header_line_height = current_mode_line_height = -1;
14935
14936 CHARPOS (this_line_start_pos) = 0;
14937
14938 mini_w = XWINDOW (minibuf_window);
14939 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
14940
14941 if (!noninteractive)
14942 {
14943 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
14944 int i;
14945
14946 XWINDOW (root_window)->top = make_number (FRAME_TOP_MARGIN (f));
14947 set_window_height (root_window,
14948 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
14949 0);
14950 mini_w->top = make_number (FRAME_HEIGHT (f) - 1);
14951 set_window_height (minibuf_window, 1, 0);
14952
14953 XWINDOW (root_window)->width = make_number (FRAME_WIDTH (f));
14954 mini_w->width = make_number (FRAME_WIDTH (f));
14955
14956 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
14957 scratch_glyph_row.glyphs[TEXT_AREA + 1]
14958 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
14959
14960 /* The default ellipsis glyphs `...'. */
14961 for (i = 0; i < 3; ++i)
14962 default_invis_vector[i] = make_number ('.');
14963 }
14964
14965 #ifdef HAVE_WINDOW_SYSTEM
14966 {
14967 /* Allocate the buffer for frame titles. */
14968 int size = 100;
14969 frame_title_buf = (char *) xmalloc (size);
14970 frame_title_buf_end = frame_title_buf + size;
14971 frame_title_ptr = NULL;
14972 }
14973 #endif /* HAVE_WINDOW_SYSTEM */
14974
14975 help_echo_showing_p = 0;
14976 }
14977
14978