(reseat_to_string): Undo last change.
[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 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 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 be displayed is then simple. It is
127 started by initializing an iterator with a call to init_iterator
128 (or init_string_iterator for that matter). Calls to
129 get_next_display_element fill the iterator structure with relevant
130 information about the next thing to display. Calls to
131 set_iterator_to_next move the iterator to the next thing.
132
133 Besides this, an iterator also contains information about the
134 display environment in which glyphs for display elements are to be
135 produced. It has fields for the width and height of the display,
136 the information whether long lines are truncated or continued, a
137 current X and Y position, and lots of other stuff you can better
138 see in dispextern.h.
139
140 Glyphs in a desired matrix are normally constructed in a loop
141 calling get_next_display_element and then produce_glyphs. The call
142 to produce_glyphs will fill the iterator structure with pixel
143 information about the element being displayed and at the same time
144 produce glyphs for it. If the display element fits on the line
145 being displayed, set_iterator_to_next is called next, otherwise the
146 glyphs produced are discarded.
147
148
149 Frame matrices.
150
151 That just couldn't be all, could it? What about terminal types not
152 supporting operations on sub-windows of the screen? To update the
153 display on such a terminal, window-based glyph matrices are not
154 well suited. To be able to reuse part of the display (scrolling
155 lines up and down), we must instead have a view of the whole
156 screen. This is what `frame matrices' are for. They are a trick.
157
158 Frames on terminals like above have a glyph pool. Windows on such
159 a frame sub-allocate their glyph memory from their frame's glyph
160 pool. The frame itself is given its own glyph matrices. By
161 coincidence---or maybe something else---rows in window glyph
162 matrices are slices of corresponding rows in frame matrices. Thus
163 writing to window matrices implicitly updates a frame matrix which
164 provides us with the view of the whole screen that we originally
165 wanted to have without having to move many bytes around. To be
166 honest, there is a little bit more done, but not much more. If you
167 plan to extend that code, take a look at dispnew.c. The function
168 build_frame_matrix is a good starting point. */
169
170 #include <config.h>
171 #include <stdio.h>
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 min(a, b) ((a) < (b) ? (a) : (b))
202 #define max(a, b) ((a) > (b) ? (a) : (b))
203
204 #define INFINITY 10000000
205
206 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
207 extern void set_frame_menubar P_ ((struct frame *f, int, int));
208 extern int pending_menu_activation;
209 #endif
210
211 extern int interrupt_input;
212 extern int command_loop_level;
213
214 extern int minibuffer_auto_raise;
215
216 extern Lisp_Object Qface;
217
218 extern Lisp_Object Voverriding_local_map;
219 extern Lisp_Object Voverriding_local_map_menu_flag;
220 extern Lisp_Object Qmenu_item;
221
222 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
223 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
224 Lisp_Object Qredisplay_end_trigger_functions;
225 Lisp_Object Qinhibit_point_motion_hooks;
226 Lisp_Object QCeval, Qwhen, QCfile, QCdata;
227 Lisp_Object Qfontified;
228 Lisp_Object Qgrow_only;
229
230 /* Functions called to fontify regions of text. */
231
232 Lisp_Object Vfontification_functions;
233 Lisp_Object Qfontification_functions;
234
235 /* Non-zero means draw tool bar buttons raised when the mouse moves
236 over them. */
237
238 int auto_raise_tool_bar_buttons_p;
239
240 /* Margin around tool bar buttons in pixels. */
241
242 Lisp_Object Vtool_bar_button_margin;
243
244 /* Thickness of shadow to draw around tool bar buttons. */
245
246 int tool_bar_button_relief;
247
248 /* Non-zero means automatically resize tool-bars so that all tool-bar
249 items are visible, and no blank lines remain. */
250
251 int auto_resize_tool_bars_p;
252
253 /* Non-nil means don't actually do any redisplay. */
254
255 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
256
257 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
258
259 int inhibit_eval_during_redisplay, Qinhibit_eval_during_redisplay;
260
261 /* Names of text properties relevant for redisplay. */
262
263 Lisp_Object Qdisplay, Qrelative_width, Qalign_to;
264 extern Lisp_Object Qface, Qinvisible, Qimage, Qwidth;
265
266 /* Symbols used in text property values. */
267
268 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
269 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
270 Lisp_Object Qmargin;
271 extern Lisp_Object Qheight;
272
273 /* Non-nil means highlight trailing whitespace. */
274
275 Lisp_Object Vshow_trailing_whitespace;
276
277 /* Name of the face used to highlight trailing whitespace. */
278
279 Lisp_Object Qtrailing_whitespace;
280
281 /* The symbol `image' which is the car of the lists used to represent
282 images in Lisp. */
283
284 Lisp_Object Qimage;
285
286 /* Non-zero means print newline to stdout before next mini-buffer
287 message. */
288
289 int noninteractive_need_newline;
290
291 /* Non-zero means print newline to message log before next message. */
292
293 static int message_log_need_newline;
294
295 \f
296 /* The buffer position of the first character appearing entirely or
297 partially on the line of the selected window which contains the
298 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
299 redisplay optimization in redisplay_internal. */
300
301 static struct text_pos this_line_start_pos;
302
303 /* Number of characters past the end of the line above, including the
304 terminating newline. */
305
306 static struct text_pos this_line_end_pos;
307
308 /* The vertical positions and the height of this line. */
309
310 static int this_line_vpos;
311 static int this_line_y;
312 static int this_line_pixel_height;
313
314 /* X position at which this display line starts. Usually zero;
315 negative if first character is partially visible. */
316
317 static int this_line_start_x;
318
319 /* Buffer that this_line_.* variables are referring to. */
320
321 static struct buffer *this_line_buffer;
322
323 /* Nonzero means truncate lines in all windows less wide than the
324 frame. */
325
326 int truncate_partial_width_windows;
327
328 /* A flag to control how to display unibyte 8-bit character. */
329
330 int unibyte_display_via_language_environment;
331
332 /* Nonzero means we have more than one non-mini-buffer-only frame.
333 Not guaranteed to be accurate except while parsing
334 frame-title-format. */
335
336 int multiple_frames;
337
338 Lisp_Object Vglobal_mode_string;
339
340 /* Marker for where to display an arrow on top of the buffer text. */
341
342 Lisp_Object Voverlay_arrow_position;
343
344 /* String to display for the arrow. Only used on terminal frames. */
345
346 Lisp_Object Voverlay_arrow_string;
347
348 /* Values of those variables at last redisplay. However, if
349 Voverlay_arrow_position is a marker, last_arrow_position is its
350 numerical position. */
351
352 static Lisp_Object last_arrow_position, last_arrow_string;
353
354 /* Like mode-line-format, but for the title bar on a visible frame. */
355
356 Lisp_Object Vframe_title_format;
357
358 /* Like mode-line-format, but for the title bar on an iconified frame. */
359
360 Lisp_Object Vicon_title_format;
361
362 /* List of functions to call when a window's size changes. These
363 functions get one arg, a frame on which one or more windows' sizes
364 have changed. */
365
366 static Lisp_Object Vwindow_size_change_functions;
367
368 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
369
370 /* Nonzero if overlay arrow has been displayed once in this window. */
371
372 static int overlay_arrow_seen;
373
374 /* Nonzero means highlight the region even in nonselected windows. */
375
376 int highlight_nonselected_windows;
377
378 /* If cursor motion alone moves point off frame, try scrolling this
379 many lines up or down if that will bring it back. */
380
381 static int scroll_step;
382
383 /* Non-0 means scroll just far enough to bring point back on the
384 screen, when appropriate. */
385
386 static int scroll_conservatively;
387
388 /* Recenter the window whenever point gets within this many lines of
389 the top or bottom of the window. This value is translated into a
390 pixel value by multiplying it with CANON_Y_UNIT, which means that
391 there is really a fixed pixel height scroll margin. */
392
393 int scroll_margin;
394
395 /* Number of windows showing the buffer of the selected window (or
396 another buffer with the same base buffer). keyboard.c refers to
397 this. */
398
399 int buffer_shared;
400
401 /* Vector containing glyphs for an ellipsis `...'. */
402
403 static Lisp_Object default_invis_vector[3];
404
405 /* Zero means display the mode-line/header-line/menu-bar in the default face
406 (this slightly odd definition is for compatibility with previous versions
407 of emacs), non-zero means display them using their respective faces.
408
409 This variable is deprecated. */
410
411 int mode_line_inverse_video;
412
413 /* Prompt to display in front of the mini-buffer contents. */
414
415 Lisp_Object minibuf_prompt;
416
417 /* Width of current mini-buffer prompt. Only set after display_line
418 of the line that contains the prompt. */
419
420 int minibuf_prompt_width;
421 int minibuf_prompt_pixel_width;
422
423 /* This is the window where the echo area message was displayed. It
424 is always a mini-buffer window, but it may not be the same window
425 currently active as a mini-buffer. */
426
427 Lisp_Object echo_area_window;
428
429 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
430 pushes the current message and the value of
431 message_enable_multibyte on the stack, the function restore_message
432 pops the stack and displays MESSAGE again. */
433
434 Lisp_Object Vmessage_stack;
435
436 /* Nonzero means multibyte characters were enabled when the echo area
437 message was specified. */
438
439 int message_enable_multibyte;
440
441 /* True if we should redraw the mode lines on the next redisplay. */
442
443 int update_mode_lines;
444
445 /* Nonzero if window sizes or contents have changed since last
446 redisplay that finished */
447
448 int windows_or_buffers_changed;
449
450 /* Nonzero after display_mode_line if %l was used and it displayed a
451 line number. */
452
453 int line_number_displayed;
454
455 /* Maximum buffer size for which to display line numbers. */
456
457 Lisp_Object Vline_number_display_limit;
458
459 /* line width to consider when repostioning for line number display */
460
461 static int line_number_display_limit_width;
462
463 /* Number of lines to keep in the message log buffer. t means
464 infinite. nil means don't log at all. */
465
466 Lisp_Object Vmessage_log_max;
467
468 /* The name of the *Messages* buffer, a string. */
469
470 static Lisp_Object Vmessages_buffer_name;
471
472 /* Current, index 0, and last displayed echo area message. Either
473 buffers from echo_buffers, or nil to indicate no message. */
474
475 Lisp_Object echo_area_buffer[2];
476
477 /* The buffers referenced from echo_area_buffer. */
478
479 static Lisp_Object echo_buffer[2];
480
481 /* A vector saved used in with_area_buffer to reduce consing. */
482
483 static Lisp_Object Vwith_echo_area_save_vector;
484
485 /* Non-zero means display_echo_area should display the last echo area
486 message again. Set by redisplay_preserve_echo_area. */
487
488 static int display_last_displayed_message_p;
489
490 /* Nonzero if echo area is being used by print; zero if being used by
491 message. */
492
493 int message_buf_print;
494
495 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
496
497 Lisp_Object Qinhibit_menubar_update;
498 int inhibit_menubar_update;
499
500 /* Maximum height for resizing mini-windows. Either a float
501 specifying a fraction of the available height, or an integer
502 specifying a number of lines. */
503
504 Lisp_Object Vmax_mini_window_height;
505
506 /* Non-zero means messages should be displayed with truncated
507 lines instead of being continued. */
508
509 int message_truncate_lines;
510 Lisp_Object Qmessage_truncate_lines;
511
512 /* Non-zero means we want a hollow cursor in windows that are not
513 selected. Zero means there's no cursor in such windows. */
514
515 int cursor_in_non_selected_windows;
516
517 /* A scratch glyph row with contents used for generating truncation
518 glyphs. Also used in direct_output_for_insert. */
519
520 #define MAX_SCRATCH_GLYPHS 100
521 struct glyph_row scratch_glyph_row;
522 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
523
524 /* Ascent and height of the last line processed by move_it_to. */
525
526 static int last_max_ascent, last_height;
527
528 /* Non-zero if there's a help-echo in the echo area. */
529
530 int help_echo_showing_p;
531
532 /* If >= 0, computed, exact values of mode-line and header-line height
533 to use in the macros CURRENT_MODE_LINE_HEIGHT and
534 CURRENT_HEADER_LINE_HEIGHT. */
535
536 int current_mode_line_height, current_header_line_height;
537
538 /* The maximum distance to look ahead for text properties. Values
539 that are too small let us call compute_char_face and similar
540 functions too often which is expensive. Values that are too large
541 let us call compute_char_face and alike too often because we
542 might not be interested in text properties that far away. */
543
544 #define TEXT_PROP_DISTANCE_LIMIT 100
545
546 #if GLYPH_DEBUG
547
548 /* Non-zero means print traces of redisplay if compiled with
549 GLYPH_DEBUG != 0. */
550
551 int trace_redisplay_p;
552
553 #endif /* GLYPH_DEBUG */
554
555 #ifdef DEBUG_TRACE_MOVE
556 /* Non-zero means trace with TRACE_MOVE to stderr. */
557 int trace_move;
558
559 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
560 #else
561 #define TRACE_MOVE(x) (void) 0
562 #endif
563
564 /* Non-zero means automatically scroll windows horizontally to make
565 point visible. */
566
567 int automatic_hscrolling_p;
568
569 /* A list of symbols, one for each supported image type. */
570
571 Lisp_Object Vimage_types;
572
573 /* The variable `resize-mini-windows'. If nil, don't resize
574 mini-windows. If t, always resize them to fit the text they
575 display. If `grow-only', let mini-windows grow only until they
576 become empty. */
577
578 Lisp_Object Vresize_mini_windows;
579
580 /* Value returned from text property handlers (see below). */
581
582 enum prop_handled
583 {
584 HANDLED_NORMALLY,
585 HANDLED_RECOMPUTE_PROPS,
586 HANDLED_OVERLAY_STRING_CONSUMED,
587 HANDLED_RETURN
588 };
589
590 /* A description of text properties that redisplay is interested
591 in. */
592
593 struct props
594 {
595 /* The name of the property. */
596 Lisp_Object *name;
597
598 /* A unique index for the property. */
599 enum prop_idx idx;
600
601 /* A handler function called to set up iterator IT from the property
602 at IT's current position. Value is used to steer handle_stop. */
603 enum prop_handled (*handler) P_ ((struct it *it));
604 };
605
606 static enum prop_handled handle_face_prop P_ ((struct it *));
607 static enum prop_handled handle_invisible_prop P_ ((struct it *));
608 static enum prop_handled handle_display_prop P_ ((struct it *));
609 static enum prop_handled handle_composition_prop P_ ((struct it *));
610 static enum prop_handled handle_overlay_change P_ ((struct it *));
611 static enum prop_handled handle_fontified_prop P_ ((struct it *));
612
613 /* Properties handled by iterators. */
614
615 static struct props it_props[] =
616 {
617 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
618 /* Handle `face' before `display' because some sub-properties of
619 `display' need to know the face. */
620 {&Qface, FACE_PROP_IDX, handle_face_prop},
621 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
622 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
623 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
624 {NULL, 0, NULL}
625 };
626
627 /* Value is the position described by X. If X is a marker, value is
628 the marker_position of X. Otherwise, value is X. */
629
630 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
631
632 /* Enumeration returned by some move_it_.* functions internally. */
633
634 enum move_it_result
635 {
636 /* Not used. Undefined value. */
637 MOVE_UNDEFINED,
638
639 /* Move ended at the requested buffer position or ZV. */
640 MOVE_POS_MATCH_OR_ZV,
641
642 /* Move ended at the requested X pixel position. */
643 MOVE_X_REACHED,
644
645 /* Move within a line ended at the end of a line that must be
646 continued. */
647 MOVE_LINE_CONTINUED,
648
649 /* Move within a line ended at the end of a line that would
650 be displayed truncated. */
651 MOVE_LINE_TRUNCATED,
652
653 /* Move within a line ended at a line end. */
654 MOVE_NEWLINE_OR_CR
655 };
656
657
658 \f
659 /* Function prototypes. */
660
661 static void mark_window_display_accurate_1 P_ ((struct window *, int));
662 static int single_display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
663 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
664 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
665 static int redisplay_mode_lines P_ ((Lisp_Object, int));
666 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
667 static int invisible_text_between_p P_ ((struct it *, int, int));
668 static int next_element_from_ellipsis P_ ((struct it *));
669 static void pint2str P_ ((char *, int, int));
670 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
671 struct text_pos));
672 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
673 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
674 static void store_frame_title_char P_ ((char));
675 static int store_frame_title P_ ((unsigned char *, int, int));
676 static void x_consider_frame_title P_ ((Lisp_Object));
677 static void handle_stop P_ ((struct it *));
678 static int tool_bar_lines_needed P_ ((struct frame *));
679 static int single_display_prop_intangible_p P_ ((Lisp_Object));
680 static void ensure_echo_area_buffers P_ ((void));
681 static struct glyph_row *row_containing_pos P_ ((struct window *, int,
682 struct glyph_row *,
683 struct glyph_row *));
684 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
685 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
686 static int with_echo_area_buffer P_ ((struct window *, int,
687 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
688 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
689 static void clear_garbaged_frames P_ ((void));
690 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
691 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
692 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
693 static int display_echo_area P_ ((struct window *));
694 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
695 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
696 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
697 static int string_char_and_length P_ ((unsigned char *, int, int *));
698 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
699 struct text_pos));
700 static int compute_window_start_on_continuation_line P_ ((struct window *));
701 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
702 static void insert_left_trunc_glyphs P_ ((struct it *));
703 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
704 static void extend_face_to_end_of_line P_ ((struct it *));
705 static int append_space P_ ((struct it *, int));
706 static void make_cursor_line_fully_visible P_ ((struct window *));
707 static int try_scrolling P_ ((Lisp_Object, int, int, int, int));
708 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
709 static int trailing_whitespace_p P_ ((int));
710 static int message_log_check_duplicate P_ ((int, int, int, int));
711 int invisible_p P_ ((Lisp_Object, Lisp_Object));
712 int invisible_ellipsis_p P_ ((Lisp_Object, Lisp_Object));
713 static void push_it P_ ((struct it *));
714 static void pop_it P_ ((struct it *));
715 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
716 static void redisplay_internal P_ ((int));
717 static int echo_area_display P_ ((int));
718 static void redisplay_windows P_ ((Lisp_Object));
719 static void redisplay_window P_ ((Lisp_Object, int));
720 static void update_menu_bar P_ ((struct frame *, int));
721 static int try_window_reusing_current_matrix P_ ((struct window *));
722 static int try_window_id P_ ((struct window *));
723 static int display_line P_ ((struct it *));
724 static int display_mode_lines P_ ((struct window *));
725 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
726 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object));
727 static char *decode_mode_spec P_ ((struct window *, int, int, int));
728 static void display_menu_bar P_ ((struct window *));
729 static int display_count_lines P_ ((int, int, int, int, int *));
730 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
731 int, int, struct it *, int, int, int, int));
732 static void compute_line_metrics P_ ((struct it *));
733 static void run_redisplay_end_trigger_hook P_ ((struct it *));
734 static int get_overlay_strings P_ ((struct it *));
735 static void next_overlay_string P_ ((struct it *));
736 static void reseat P_ ((struct it *, struct text_pos, int));
737 static void reseat_1 P_ ((struct it *, struct text_pos, int));
738 static void back_to_previous_visible_line_start P_ ((struct it *));
739 static void reseat_at_previous_visible_line_start P_ ((struct it *));
740 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
741 static int next_element_from_display_vector P_ ((struct it *));
742 static int next_element_from_string P_ ((struct it *));
743 static int next_element_from_c_string P_ ((struct it *));
744 static int next_element_from_buffer P_ ((struct it *));
745 static int next_element_from_composition P_ ((struct it *));
746 static int next_element_from_image P_ ((struct it *));
747 static int next_element_from_stretch P_ ((struct it *));
748 static void load_overlay_strings P_ ((struct it *));
749 static void init_from_display_pos P_ ((struct it *, struct window *,
750 struct display_pos *));
751 static void reseat_to_string P_ ((struct it *, unsigned char *,
752 Lisp_Object, int, int, int, int));
753 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
754 int, int, int));
755 void move_it_vertically_backward P_ ((struct it *, int));
756 static void init_to_row_start P_ ((struct it *, struct window *,
757 struct glyph_row *));
758 static void init_to_row_end P_ ((struct it *, struct window *,
759 struct glyph_row *));
760 static void back_to_previous_line_start P_ ((struct it *));
761 static int forward_to_next_line_start P_ ((struct it *, int *));
762 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
763 Lisp_Object, int));
764 static struct text_pos string_pos P_ ((int, Lisp_Object));
765 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
766 static int number_of_chars P_ ((unsigned char *, int));
767 static void compute_stop_pos P_ ((struct it *));
768 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
769 Lisp_Object));
770 static int face_before_or_after_it_pos P_ ((struct it *, int));
771 static int next_overlay_change P_ ((int));
772 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
773 Lisp_Object, struct text_pos *,
774 int));
775 static int underlying_face_id P_ ((struct it *));
776 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
777 struct window *));
778
779 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
780 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
781
782 #ifdef HAVE_WINDOW_SYSTEM
783
784 static void update_tool_bar P_ ((struct frame *, int));
785 static void build_desired_tool_bar_string P_ ((struct frame *f));
786 static int redisplay_tool_bar P_ ((struct frame *));
787 static void display_tool_bar_line P_ ((struct it *));
788
789 #endif /* HAVE_WINDOW_SYSTEM */
790
791 \f
792 /***********************************************************************
793 Window display dimensions
794 ***********************************************************************/
795
796 /* Return the window-relative maximum y + 1 for glyph rows displaying
797 text in window W. This is the height of W minus the height of a
798 mode line, if any. */
799
800 INLINE int
801 window_text_bottom_y (w)
802 struct window *w;
803 {
804 struct frame *f = XFRAME (w->frame);
805 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
806
807 if (WINDOW_WANTS_MODELINE_P (w))
808 height -= CURRENT_MODE_LINE_HEIGHT (w);
809 return height;
810 }
811
812
813 /* Return the pixel width of display area AREA of window W. AREA < 0
814 means return the total width of W, not including bitmap areas to
815 the left and right of the window. */
816
817 INLINE int
818 window_box_width (w, area)
819 struct window *w;
820 int area;
821 {
822 struct frame *f = XFRAME (w->frame);
823 int width = XFASTINT (w->width);
824
825 if (!w->pseudo_window_p)
826 {
827 width -= FRAME_SCROLL_BAR_WIDTH (f) + FRAME_FLAGS_AREA_COLS (f);
828
829 if (area == TEXT_AREA)
830 {
831 if (INTEGERP (w->left_margin_width))
832 width -= XFASTINT (w->left_margin_width);
833 if (INTEGERP (w->right_margin_width))
834 width -= XFASTINT (w->right_margin_width);
835 }
836 else if (area == LEFT_MARGIN_AREA)
837 width = (INTEGERP (w->left_margin_width)
838 ? XFASTINT (w->left_margin_width) : 0);
839 else if (area == RIGHT_MARGIN_AREA)
840 width = (INTEGERP (w->right_margin_width)
841 ? XFASTINT (w->right_margin_width) : 0);
842 }
843
844 return width * CANON_X_UNIT (f);
845 }
846
847
848 /* Return the pixel height of the display area of window W, not
849 including mode lines of W, if any.. */
850
851 INLINE int
852 window_box_height (w)
853 struct window *w;
854 {
855 struct frame *f = XFRAME (w->frame);
856 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
857
858 xassert (height >= 0);
859
860 /* Note: the code below that determines the mode-line/header-line
861 height is essentially the same as that contained in the macro
862 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
863 the appropriate glyph row has its `mode_line_p' flag set,
864 and if it doesn't, uses estimate_mode_line_height instead. */
865
866 if (WINDOW_WANTS_MODELINE_P (w))
867 {
868 struct glyph_row *ml_row
869 = (w->current_matrix && w->current_matrix->rows
870 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
871 : 0);
872 if (ml_row && ml_row->mode_line_p)
873 height -= ml_row->height;
874 else
875 height -= estimate_mode_line_height (f, MODE_LINE_FACE_ID);
876 }
877
878 if (WINDOW_WANTS_HEADER_LINE_P (w))
879 {
880 struct glyph_row *hl_row
881 = (w->current_matrix && w->current_matrix->rows
882 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
883 : 0);
884 if (hl_row && hl_row->mode_line_p)
885 height -= hl_row->height;
886 else
887 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
888 }
889
890 return height;
891 }
892
893
894 /* Return the frame-relative coordinate of the left edge of display
895 area AREA of window W. AREA < 0 means return the left edge of the
896 whole window, to the right of any bitmap area at the left side of
897 W. */
898
899 INLINE int
900 window_box_left (w, area)
901 struct window *w;
902 int area;
903 {
904 struct frame *f = XFRAME (w->frame);
905 int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
906
907 if (!w->pseudo_window_p)
908 {
909 x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
910 + FRAME_LEFT_FLAGS_AREA_WIDTH (f));
911
912 if (area == TEXT_AREA)
913 x += window_box_width (w, LEFT_MARGIN_AREA);
914 else if (area == RIGHT_MARGIN_AREA)
915 x += (window_box_width (w, LEFT_MARGIN_AREA)
916 + window_box_width (w, TEXT_AREA));
917 }
918
919 return x;
920 }
921
922
923 /* Return the frame-relative coordinate of the right edge of display
924 area AREA of window W. AREA < 0 means return the left edge of the
925 whole window, to the left of any bitmap area at the right side of
926 W. */
927
928 INLINE int
929 window_box_right (w, area)
930 struct window *w;
931 int area;
932 {
933 return window_box_left (w, area) + window_box_width (w, area);
934 }
935
936
937 /* Get the bounding box of the display area AREA of window W, without
938 mode lines, in frame-relative coordinates. AREA < 0 means the
939 whole window, not including bitmap areas to the left and right of
940 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
941 coordinates of the upper-left corner of the box. Return in
942 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
943
944 INLINE void
945 window_box (w, area, box_x, box_y, box_width, box_height)
946 struct window *w;
947 int area;
948 int *box_x, *box_y, *box_width, *box_height;
949 {
950 struct frame *f = XFRAME (w->frame);
951
952 *box_width = window_box_width (w, area);
953 *box_height = window_box_height (w);
954 *box_x = window_box_left (w, area);
955 *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
956 + XFASTINT (w->top) * CANON_Y_UNIT (f));
957 if (WINDOW_WANTS_HEADER_LINE_P (w))
958 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
959 }
960
961
962 /* Get the bounding box of the display area AREA of window W, without
963 mode lines. AREA < 0 means the whole window, not including bitmap
964 areas to the left and right of the window. Return in *TOP_LEFT_X
965 and TOP_LEFT_Y the frame-relative pixel coordinates of the
966 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
967 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
968 box. */
969
970 INLINE void
971 window_box_edges (w, area, top_left_x, top_left_y,
972 bottom_right_x, bottom_right_y)
973 struct window *w;
974 int area;
975 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
976 {
977 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
978 bottom_right_y);
979 *bottom_right_x += *top_left_x;
980 *bottom_right_y += *top_left_y;
981 }
982
983
984 \f
985 /***********************************************************************
986 Utilities
987 ***********************************************************************/
988
989 /* Return the bottom y-position of the line the iterator IT is in.
990 This can modify IT's settings. */
991
992 int
993 line_bottom_y (it)
994 struct it *it;
995 {
996 int line_height = it->max_ascent + it->max_descent;
997 int line_top_y = it->current_y;
998
999 if (line_height == 0)
1000 {
1001 if (last_height)
1002 line_height = last_height;
1003 else if (IT_CHARPOS (*it) < ZV)
1004 {
1005 move_it_by_lines (it, 1, 1);
1006 line_height = (it->max_ascent || it->max_descent
1007 ? it->max_ascent + it->max_descent
1008 : last_height);
1009 }
1010 else
1011 {
1012 struct glyph_row *row = it->glyph_row;
1013
1014 /* Use the default character height. */
1015 it->glyph_row = NULL;
1016 it->what = IT_CHARACTER;
1017 it->c = ' ';
1018 it->len = 1;
1019 PRODUCE_GLYPHS (it);
1020 line_height = it->ascent + it->descent;
1021 it->glyph_row = row;
1022 }
1023 }
1024
1025 return line_top_y + line_height;
1026 }
1027
1028
1029 /* Return 1 if position CHARPOS is visible in window W. Set *FULLY to
1030 1 if POS is visible and the line containing POS is fully visible.
1031 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1032 and header-lines heights. */
1033
1034 int
1035 pos_visible_p (w, charpos, fully, exact_mode_line_heights_p)
1036 struct window *w;
1037 int charpos, *fully, exact_mode_line_heights_p;
1038 {
1039 struct it it;
1040 struct text_pos top;
1041 int visible_p;
1042 struct buffer *old_buffer = NULL;
1043
1044 if (XBUFFER (w->buffer) != current_buffer)
1045 {
1046 old_buffer = current_buffer;
1047 set_buffer_internal_1 (XBUFFER (w->buffer));
1048 }
1049
1050 *fully = visible_p = 0;
1051 SET_TEXT_POS_FROM_MARKER (top, w->start);
1052
1053 /* Compute exact mode line heights, if requested. */
1054 if (exact_mode_line_heights_p)
1055 {
1056 if (WINDOW_WANTS_MODELINE_P (w))
1057 current_mode_line_height
1058 = display_mode_line (w, MODE_LINE_FACE_ID,
1059 current_buffer->mode_line_format);
1060
1061 if (WINDOW_WANTS_HEADER_LINE_P (w))
1062 current_header_line_height
1063 = display_mode_line (w, HEADER_LINE_FACE_ID,
1064 current_buffer->header_line_format);
1065 }
1066
1067 start_display (&it, w, top);
1068 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
1069 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
1070
1071 /* Note that we may overshoot because of invisible text. */
1072 if (IT_CHARPOS (it) >= charpos)
1073 {
1074 int top_y = it.current_y;
1075 int bottom_y = line_bottom_y (&it);
1076 int window_top_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
1077
1078 if (top_y < window_top_y)
1079 visible_p = bottom_y > window_top_y;
1080 else if (top_y < it.last_visible_y)
1081 {
1082 visible_p = 1;
1083 *fully = bottom_y <= it.last_visible_y;
1084 }
1085 }
1086 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1087 {
1088 move_it_by_lines (&it, 1, 0);
1089 if (charpos < IT_CHARPOS (it))
1090 {
1091 visible_p = 1;
1092 *fully = 0;
1093 }
1094 }
1095
1096 if (old_buffer)
1097 set_buffer_internal_1 (old_buffer);
1098
1099 current_header_line_height = current_mode_line_height = -1;
1100 return visible_p;
1101 }
1102
1103
1104 /* Return the next character from STR which is MAXLEN bytes long.
1105 Return in *LEN the length of the character. This is like
1106 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1107 we find one, we return a `?', but with the length of the invalid
1108 character. */
1109
1110 static INLINE int
1111 string_char_and_length (str, maxlen, len)
1112 unsigned char *str;
1113 int maxlen, *len;
1114 {
1115 int c;
1116
1117 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1118 if (!CHAR_VALID_P (c, 1))
1119 /* We may not change the length here because other places in Emacs
1120 don't use this function, i.e. they silently accept invalid
1121 characters. */
1122 c = '?';
1123
1124 return c;
1125 }
1126
1127
1128
1129 /* Given a position POS containing a valid character and byte position
1130 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1131
1132 static struct text_pos
1133 string_pos_nchars_ahead (pos, string, nchars)
1134 struct text_pos pos;
1135 Lisp_Object string;
1136 int nchars;
1137 {
1138 xassert (STRINGP (string) && nchars >= 0);
1139
1140 if (STRING_MULTIBYTE (string))
1141 {
1142 int rest = STRING_BYTES (XSTRING (string)) - BYTEPOS (pos);
1143 unsigned char *p = XSTRING (string)->data + BYTEPOS (pos);
1144 int len;
1145
1146 while (nchars--)
1147 {
1148 string_char_and_length (p, rest, &len);
1149 p += len, rest -= len;
1150 xassert (rest >= 0);
1151 CHARPOS (pos) += 1;
1152 BYTEPOS (pos) += len;
1153 }
1154 }
1155 else
1156 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1157
1158 return pos;
1159 }
1160
1161
1162 /* Value is the text position, i.e. character and byte position,
1163 for character position CHARPOS in STRING. */
1164
1165 static INLINE struct text_pos
1166 string_pos (charpos, string)
1167 int charpos;
1168 Lisp_Object string;
1169 {
1170 struct text_pos pos;
1171 xassert (STRINGP (string));
1172 xassert (charpos >= 0);
1173 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1174 return pos;
1175 }
1176
1177
1178 /* Value is a text position, i.e. character and byte position, for
1179 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1180 means recognize multibyte characters. */
1181
1182 static struct text_pos
1183 c_string_pos (charpos, s, multibyte_p)
1184 int charpos;
1185 unsigned char *s;
1186 int multibyte_p;
1187 {
1188 struct text_pos pos;
1189
1190 xassert (s != NULL);
1191 xassert (charpos >= 0);
1192
1193 if (multibyte_p)
1194 {
1195 int rest = strlen (s), len;
1196
1197 SET_TEXT_POS (pos, 0, 0);
1198 while (charpos--)
1199 {
1200 string_char_and_length (s, rest, &len);
1201 s += len, rest -= len;
1202 xassert (rest >= 0);
1203 CHARPOS (pos) += 1;
1204 BYTEPOS (pos) += len;
1205 }
1206 }
1207 else
1208 SET_TEXT_POS (pos, charpos, charpos);
1209
1210 return pos;
1211 }
1212
1213
1214 /* Value is the number of characters in C string S. MULTIBYTE_P
1215 non-zero means recognize multibyte characters. */
1216
1217 static int
1218 number_of_chars (s, multibyte_p)
1219 unsigned char *s;
1220 int multibyte_p;
1221 {
1222 int nchars;
1223
1224 if (multibyte_p)
1225 {
1226 int rest = strlen (s), len;
1227 unsigned char *p = (unsigned char *) s;
1228
1229 for (nchars = 0; rest > 0; ++nchars)
1230 {
1231 string_char_and_length (p, rest, &len);
1232 rest -= len, p += len;
1233 }
1234 }
1235 else
1236 nchars = strlen (s);
1237
1238 return nchars;
1239 }
1240
1241
1242 /* Compute byte position NEWPOS->bytepos corresponding to
1243 NEWPOS->charpos. POS is a known position in string STRING.
1244 NEWPOS->charpos must be >= POS.charpos. */
1245
1246 static void
1247 compute_string_pos (newpos, pos, string)
1248 struct text_pos *newpos, pos;
1249 Lisp_Object string;
1250 {
1251 xassert (STRINGP (string));
1252 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1253
1254 if (STRING_MULTIBYTE (string))
1255 *newpos = string_pos_nchars_ahead (pos, string,
1256 CHARPOS (*newpos) - CHARPOS (pos));
1257 else
1258 BYTEPOS (*newpos) = CHARPOS (*newpos);
1259 }
1260
1261
1262 \f
1263 /***********************************************************************
1264 Lisp form evaluation
1265 ***********************************************************************/
1266
1267 /* Error handler for safe_eval and safe_call. */
1268
1269 static Lisp_Object
1270 safe_eval_handler (arg)
1271 Lisp_Object arg;
1272 {
1273 add_to_log ("Error during redisplay: %s", arg, Qnil);
1274 return Qnil;
1275 }
1276
1277
1278 /* Evaluate SEXPR and return the result, or nil if something went
1279 wrong. */
1280
1281 Lisp_Object
1282 safe_eval (sexpr)
1283 Lisp_Object sexpr;
1284 {
1285 Lisp_Object val;
1286
1287 if (inhibit_eval_during_redisplay)
1288 val = Qnil;
1289 else
1290 {
1291 int count = BINDING_STACK_SIZE ();
1292 struct gcpro gcpro1;
1293
1294 GCPRO1 (sexpr);
1295 specbind (Qinhibit_redisplay, Qt);
1296 val = internal_condition_case_1 (Feval, sexpr, Qerror,
1297 safe_eval_handler);
1298 UNGCPRO;
1299 val = unbind_to (count, val);
1300 }
1301
1302 return val;
1303 }
1304
1305
1306 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1307 Return the result, or nil if something went wrong. */
1308
1309 Lisp_Object
1310 safe_call (nargs, args)
1311 int nargs;
1312 Lisp_Object *args;
1313 {
1314 Lisp_Object val;
1315
1316 if (inhibit_eval_during_redisplay)
1317 val = Qnil;
1318 else
1319 {
1320 int count = BINDING_STACK_SIZE ();
1321 struct gcpro gcpro1;
1322
1323 GCPRO1 (args[0]);
1324 gcpro1.nvars = nargs;
1325 specbind (Qinhibit_redisplay, Qt);
1326 val = internal_condition_case_2 (Ffuncall, nargs, args, Qerror,
1327 safe_eval_handler);
1328 UNGCPRO;
1329 val = unbind_to (count, val);
1330 }
1331
1332 return val;
1333 }
1334
1335
1336 /* Call function FN with one argument ARG.
1337 Return the result, or nil if something went wrong. */
1338
1339 Lisp_Object
1340 safe_call1 (fn, arg)
1341 Lisp_Object fn, arg;
1342 {
1343 Lisp_Object args[2];
1344 args[0] = fn;
1345 args[1] = arg;
1346 return safe_call (2, args);
1347 }
1348
1349
1350 \f
1351 /***********************************************************************
1352 Debugging
1353 ***********************************************************************/
1354
1355 #if 0
1356
1357 /* Define CHECK_IT to perform sanity checks on iterators.
1358 This is for debugging. It is too slow to do unconditionally. */
1359
1360 static void
1361 check_it (it)
1362 struct it *it;
1363 {
1364 if (it->method == next_element_from_string)
1365 {
1366 xassert (STRINGP (it->string));
1367 xassert (IT_STRING_CHARPOS (*it) >= 0);
1368 }
1369 else if (it->method == next_element_from_buffer)
1370 {
1371 /* Check that character and byte positions agree. */
1372 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1373 }
1374
1375 if (it->dpvec)
1376 xassert (it->current.dpvec_index >= 0);
1377 else
1378 xassert (it->current.dpvec_index < 0);
1379 }
1380
1381 #define CHECK_IT(IT) check_it ((IT))
1382
1383 #else /* not 0 */
1384
1385 #define CHECK_IT(IT) (void) 0
1386
1387 #endif /* not 0 */
1388
1389
1390 #if GLYPH_DEBUG
1391
1392 /* Check that the window end of window W is what we expect it
1393 to be---the last row in the current matrix displaying text. */
1394
1395 static void
1396 check_window_end (w)
1397 struct window *w;
1398 {
1399 if (!MINI_WINDOW_P (w)
1400 && !NILP (w->window_end_valid))
1401 {
1402 struct glyph_row *row;
1403 xassert ((row = MATRIX_ROW (w->current_matrix,
1404 XFASTINT (w->window_end_vpos)),
1405 !row->enabled_p
1406 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1407 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1408 }
1409 }
1410
1411 #define CHECK_WINDOW_END(W) check_window_end ((W))
1412
1413 #else /* not GLYPH_DEBUG */
1414
1415 #define CHECK_WINDOW_END(W) (void) 0
1416
1417 #endif /* not GLYPH_DEBUG */
1418
1419
1420 \f
1421 /***********************************************************************
1422 Iterator initialization
1423 ***********************************************************************/
1424
1425 /* Initialize IT for displaying current_buffer in window W, starting
1426 at character position CHARPOS. CHARPOS < 0 means that no buffer
1427 position is specified which is useful when the iterator is assigned
1428 a position later. BYTEPOS is the byte position corresponding to
1429 CHARPOS. BYTEPOS <= 0 means compute it from CHARPOS.
1430
1431 If ROW is not null, calls to produce_glyphs with IT as parameter
1432 will produce glyphs in that row.
1433
1434 BASE_FACE_ID is the id of a base face to use. It must be one of
1435 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID or
1436 HEADER_LINE_FACE_ID for displaying mode lines, or TOOL_BAR_FACE_ID for
1437 displaying the tool-bar.
1438
1439 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID or
1440 HEADER_LINE_FACE_ID, the iterator will be initialized to use the
1441 corresponding mode line glyph row of the desired matrix of W. */
1442
1443 void
1444 init_iterator (it, w, charpos, bytepos, row, base_face_id)
1445 struct it *it;
1446 struct window *w;
1447 int charpos, bytepos;
1448 struct glyph_row *row;
1449 enum face_id base_face_id;
1450 {
1451 int highlight_region_p;
1452
1453 /* Some precondition checks. */
1454 xassert (w != NULL && it != NULL);
1455 xassert (charpos < 0 || (charpos > 0 && charpos <= ZV));
1456
1457 /* If face attributes have been changed since the last redisplay,
1458 free realized faces now because they depend on face definitions
1459 that might have changed. */
1460 if (face_change_count)
1461 {
1462 face_change_count = 0;
1463 free_all_realized_faces (Qnil);
1464 }
1465
1466 /* Use one of the mode line rows of W's desired matrix if
1467 appropriate. */
1468 if (row == NULL)
1469 {
1470 if (base_face_id == MODE_LINE_FACE_ID)
1471 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
1472 else if (base_face_id == HEADER_LINE_FACE_ID)
1473 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
1474 }
1475
1476 /* Clear IT. */
1477 bzero (it, sizeof *it);
1478 it->current.overlay_string_index = -1;
1479 it->current.dpvec_index = -1;
1480 it->base_face_id = base_face_id;
1481
1482 /* The window in which we iterate over current_buffer: */
1483 XSETWINDOW (it->window, w);
1484 it->w = w;
1485 it->f = XFRAME (w->frame);
1486
1487 /* Extra space between lines (on window systems only). */
1488 if (base_face_id == DEFAULT_FACE_ID
1489 && FRAME_WINDOW_P (it->f))
1490 {
1491 if (NATNUMP (current_buffer->extra_line_spacing))
1492 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
1493 else if (it->f->extra_line_spacing > 0)
1494 it->extra_line_spacing = it->f->extra_line_spacing;
1495 }
1496
1497 /* If realized faces have been removed, e.g. because of face
1498 attribute changes of named faces, recompute them. When running
1499 in batch mode, the face cache of Vterminal_frame is null. If
1500 we happen to get called, make a dummy face cache. */
1501 if (
1502 #ifndef WINDOWSNT
1503 noninteractive &&
1504 #endif
1505 FRAME_FACE_CACHE (it->f) == NULL)
1506 init_frame_faces (it->f);
1507 if (FRAME_FACE_CACHE (it->f)->used == 0)
1508 recompute_basic_faces (it->f);
1509
1510 /* Current value of the `space-width', and 'height' properties. */
1511 it->space_width = Qnil;
1512 it->font_height = Qnil;
1513
1514 /* Are control characters displayed as `^C'? */
1515 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
1516
1517 /* -1 means everything between a CR and the following line end
1518 is invisible. >0 means lines indented more than this value are
1519 invisible. */
1520 it->selective = (INTEGERP (current_buffer->selective_display)
1521 ? XFASTINT (current_buffer->selective_display)
1522 : (!NILP (current_buffer->selective_display)
1523 ? -1 : 0));
1524 it->selective_display_ellipsis_p
1525 = !NILP (current_buffer->selective_display_ellipses);
1526
1527 /* Display table to use. */
1528 it->dp = window_display_table (w);
1529
1530 /* Are multibyte characters enabled in current_buffer? */
1531 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1532
1533 /* Non-zero if we should highlight the region. */
1534 highlight_region_p
1535 = (!NILP (Vtransient_mark_mode)
1536 && !NILP (current_buffer->mark_active)
1537 && XMARKER (current_buffer->mark)->buffer != 0);
1538
1539 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
1540 start and end of a visible region in window IT->w. Set both to
1541 -1 to indicate no region. */
1542 if (highlight_region_p
1543 /* Maybe highlight only in selected window. */
1544 && (/* Either show region everywhere. */
1545 highlight_nonselected_windows
1546 /* Or show region in the selected window. */
1547 || w == XWINDOW (selected_window)
1548 /* Or show the region if we are in the mini-buffer and W is
1549 the window the mini-buffer refers to. */
1550 || (MINI_WINDOW_P (XWINDOW (selected_window))
1551 && WINDOWP (Vminibuf_scroll_window)
1552 && w == XWINDOW (Vminibuf_scroll_window))))
1553 {
1554 int charpos = marker_position (current_buffer->mark);
1555 it->region_beg_charpos = min (PT, charpos);
1556 it->region_end_charpos = max (PT, charpos);
1557 }
1558 else
1559 it->region_beg_charpos = it->region_end_charpos = -1;
1560
1561 /* Get the position at which the redisplay_end_trigger hook should
1562 be run, if it is to be run at all. */
1563 if (MARKERP (w->redisplay_end_trigger)
1564 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
1565 it->redisplay_end_trigger_charpos
1566 = marker_position (w->redisplay_end_trigger);
1567 else if (INTEGERP (w->redisplay_end_trigger))
1568 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
1569
1570 /* Correct bogus values of tab_width. */
1571 it->tab_width = XINT (current_buffer->tab_width);
1572 if (it->tab_width <= 0 || it->tab_width > 1000)
1573 it->tab_width = 8;
1574
1575 /* Are lines in the display truncated? */
1576 it->truncate_lines_p
1577 = (base_face_id != DEFAULT_FACE_ID
1578 || XINT (it->w->hscroll)
1579 || (truncate_partial_width_windows
1580 && !WINDOW_FULL_WIDTH_P (it->w))
1581 || !NILP (current_buffer->truncate_lines));
1582
1583 /* Get dimensions of truncation and continuation glyphs. These are
1584 displayed as bitmaps under X, so we don't need them for such
1585 frames. */
1586 if (!FRAME_WINDOW_P (it->f))
1587 {
1588 if (it->truncate_lines_p)
1589 {
1590 /* We will need the truncation glyph. */
1591 xassert (it->glyph_row == NULL);
1592 produce_special_glyphs (it, IT_TRUNCATION);
1593 it->truncation_pixel_width = it->pixel_width;
1594 }
1595 else
1596 {
1597 /* We will need the continuation glyph. */
1598 xassert (it->glyph_row == NULL);
1599 produce_special_glyphs (it, IT_CONTINUATION);
1600 it->continuation_pixel_width = it->pixel_width;
1601 }
1602
1603 /* Reset these values to zero becaue the produce_special_glyphs
1604 above has changed them. */
1605 it->pixel_width = it->ascent = it->descent = 0;
1606 it->phys_ascent = it->phys_descent = 0;
1607 }
1608
1609 /* Set this after getting the dimensions of truncation and
1610 continuation glyphs, so that we don't produce glyphs when calling
1611 produce_special_glyphs, above. */
1612 it->glyph_row = row;
1613 it->area = TEXT_AREA;
1614
1615 /* Get the dimensions of the display area. The display area
1616 consists of the visible window area plus a horizontally scrolled
1617 part to the left of the window. All x-values are relative to the
1618 start of this total display area. */
1619 if (base_face_id != DEFAULT_FACE_ID)
1620 {
1621 /* Mode lines, menu bar in terminal frames. */
1622 it->first_visible_x = 0;
1623 it->last_visible_x = XFASTINT (w->width) * CANON_X_UNIT (it->f);
1624 }
1625 else
1626 {
1627 it->first_visible_x
1628 = XFASTINT (it->w->hscroll) * CANON_X_UNIT (it->f);
1629 it->last_visible_x = (it->first_visible_x
1630 + window_box_width (w, TEXT_AREA));
1631
1632 /* If we truncate lines, leave room for the truncator glyph(s) at
1633 the right margin. Otherwise, leave room for the continuation
1634 glyph(s). Truncation and continuation glyphs are not inserted
1635 for window-based redisplay. */
1636 if (!FRAME_WINDOW_P (it->f))
1637 {
1638 if (it->truncate_lines_p)
1639 it->last_visible_x -= it->truncation_pixel_width;
1640 else
1641 it->last_visible_x -= it->continuation_pixel_width;
1642 }
1643
1644 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
1645 it->current_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w) + w->vscroll;
1646 }
1647
1648 /* Leave room for a border glyph. */
1649 if (!FRAME_WINDOW_P (it->f)
1650 && !WINDOW_RIGHTMOST_P (it->w))
1651 it->last_visible_x -= 1;
1652
1653 it->last_visible_y = window_text_bottom_y (w);
1654
1655 /* For mode lines and alike, arrange for the first glyph having a
1656 left box line if the face specifies a box. */
1657 if (base_face_id != DEFAULT_FACE_ID)
1658 {
1659 struct face *face;
1660
1661 it->face_id = base_face_id;
1662
1663 /* If we have a boxed mode line, make the first character appear
1664 with a left box line. */
1665 face = FACE_FROM_ID (it->f, base_face_id);
1666 if (face->box != FACE_NO_BOX)
1667 it->start_of_box_run_p = 1;
1668 }
1669
1670 /* If a buffer position was specified, set the iterator there,
1671 getting overlays and face properties from that position. */
1672 if (charpos > 0)
1673 {
1674 it->end_charpos = ZV;
1675 it->face_id = -1;
1676 IT_CHARPOS (*it) = charpos;
1677
1678 /* Compute byte position if not specified. */
1679 if (bytepos <= 0)
1680 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
1681 else
1682 IT_BYTEPOS (*it) = bytepos;
1683
1684 /* Compute faces etc. */
1685 reseat (it, it->current.pos, 1);
1686 }
1687
1688 CHECK_IT (it);
1689 }
1690
1691
1692 /* Initialize IT for the display of window W with window start POS. */
1693
1694 void
1695 start_display (it, w, pos)
1696 struct it *it;
1697 struct window *w;
1698 struct text_pos pos;
1699 {
1700 int start_at_line_beg_p;
1701 struct glyph_row *row;
1702 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
1703 int first_y;
1704
1705 row = w->desired_matrix->rows + first_vpos;
1706 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
1707 first_y = it->current_y;
1708
1709 /* If window start is not at a line start, move back to the line
1710 start. This makes sure that we take continuation lines into
1711 account. */
1712 start_at_line_beg_p = (CHARPOS (pos) == BEGV
1713 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
1714 if (!start_at_line_beg_p)
1715 reseat_at_previous_visible_line_start (it);
1716
1717 /* If window start is not at a line start, skip forward to POS to
1718 get the correct continuation_lines_width and current_x. */
1719 if (!start_at_line_beg_p)
1720 {
1721 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
1722
1723 /* If lines are continued, this line may end in the middle of a
1724 multi-glyph character (e.g. a control character displayed as
1725 \003, or in the middle of an overlay string). In this case
1726 move_it_to above will not have taken us to the start of
1727 the continuation line but to the end of the continued line. */
1728 if (!it->truncate_lines_p)
1729 {
1730 if (it->current_x > 0)
1731 {
1732 if (it->current.dpvec_index >= 0
1733 || it->current.overlay_string_index >= 0)
1734 {
1735 set_iterator_to_next (it, 1);
1736 move_it_in_display_line_to (it, -1, -1, 0);
1737 }
1738
1739 it->continuation_lines_width += it->current_x;
1740 }
1741
1742 /* We're starting a new display line, not affected by the
1743 height of the continued line, so clear the appropriate
1744 fields in the iterator structure. */
1745 it->max_ascent = it->max_descent = 0;
1746 it->max_phys_ascent = it->max_phys_descent = 0;
1747 }
1748
1749 it->current_y = first_y;
1750 it->vpos = 0;
1751 it->current_x = it->hpos = 0;
1752 }
1753
1754 #if 0 /* Don't assert the following because start_display is sometimes
1755 called intentionally with a window start that is not at a
1756 line start. Please leave this code in as a comment. */
1757
1758 /* Window start should be on a line start, now. */
1759 xassert (it->continuation_lines_width
1760 || IT_CHARPOS (it) == BEGV
1761 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
1762 #endif /* 0 */
1763 }
1764
1765
1766 /* Return 1 if POS is a position in ellipses displayed for invisible
1767 text. W is the window we display, for text property lookup. */
1768
1769 static int
1770 in_ellipses_for_invisible_text_p (pos, w)
1771 struct display_pos *pos;
1772 struct window *w;
1773 {
1774 Lisp_Object prop, window;
1775 int ellipses_p = 0;
1776 int charpos = CHARPOS (pos->pos);
1777
1778 /* If POS specifies a position in a display vector, this might
1779 be for an ellipsis displayed for invisible text. We won't
1780 get the iterator set up for delivering that ellipsis unless
1781 we make sure that it gets aware of the invisible text. */
1782 if (pos->dpvec_index >= 0
1783 && pos->overlay_string_index < 0
1784 && CHARPOS (pos->string_pos) < 0
1785 && charpos > BEGV
1786 && (XSETWINDOW (window, w),
1787 prop = Fget_char_property (make_number (charpos),
1788 Qinvisible, window),
1789 !TEXT_PROP_MEANS_INVISIBLE (prop)))
1790 {
1791 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
1792 window);
1793 if (TEXT_PROP_MEANS_INVISIBLE (prop)
1794 && TEXT_PROP_MEANS_INVISIBLE_WITH_ELLIPSIS (prop))
1795 ellipses_p = 1;
1796 }
1797
1798 return ellipses_p;
1799 }
1800
1801
1802 /* Initialize IT for stepping through current_buffer in window W,
1803 starting at position POS that includes overlay string and display
1804 vector/ control character translation position information. */
1805
1806 static void
1807 init_from_display_pos (it, w, pos)
1808 struct it *it;
1809 struct window *w;
1810 struct display_pos *pos;
1811 {
1812 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
1813
1814 /* If POS specifies a position in a display vector, this might
1815 be for an ellipsis displayed for invisible text. We won't
1816 get the iterator set up for delivering that ellipsis unless
1817 we make sure that it gets aware of the invisible text. */
1818 if (in_ellipses_for_invisible_text_p (pos, w))
1819 {
1820 --charpos;
1821 bytepos = 0;
1822 }
1823
1824 /* Keep in mind: the call to reseat in init_iterator skips invisible
1825 text, so we might end up at a position different from POS. This
1826 is only a problem when POS is a row start after a newline and an
1827 overlay starts there with an after-string, and the overlay has an
1828 invisible property. Since we don't skip invisible text in
1829 display_line and elsewhere immediately after consuming the
1830 newline before the row start, such a POS will not be in a string,
1831 but the call to init_iterator below will move us to the
1832 after-string. */
1833 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
1834
1835 /* If position is within an overlay string, set up IT to
1836 the right overlay string. */
1837 if (pos->overlay_string_index >= 0)
1838 {
1839 int relative_index;
1840
1841 /* We already have the first chunk of overlay strings in
1842 IT->overlay_strings. Load more until the one for
1843 pos->overlay_string_index is in IT->overlay_strings. */
1844 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
1845 {
1846 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
1847 it->current.overlay_string_index = 0;
1848 while (n--)
1849 {
1850 load_overlay_strings (it);
1851 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
1852 }
1853 }
1854
1855 it->current.overlay_string_index = pos->overlay_string_index;
1856 relative_index = (it->current.overlay_string_index
1857 % OVERLAY_STRING_CHUNK_SIZE);
1858 it->string = it->overlay_strings[relative_index];
1859 xassert (STRINGP (it->string));
1860 it->current.string_pos = pos->string_pos;
1861 it->method = next_element_from_string;
1862 }
1863 else if (it->current.overlay_string_index >= 0)
1864 {
1865 /* If POS says we're already after an overlay string ending at
1866 POS, make sure to pop the iterator because it will be in
1867 front of that overlay string. When POS is ZV, we've thereby
1868 also ``processed'' overlay strings at ZV. */
1869 while (it->sp)
1870 pop_it (it);
1871 it->current.overlay_string_index = -1;
1872 it->method = next_element_from_buffer;
1873 if (CHARPOS (pos->pos) == ZV)
1874 it->overlay_strings_at_end_processed_p = 1;
1875 }
1876
1877 if (CHARPOS (pos->string_pos) >= 0)
1878 {
1879 /* Recorded position is not in an overlay string, but in another
1880 string. This can only be a string from a `display' property.
1881 IT should already be filled with that string. */
1882 it->current.string_pos = pos->string_pos;
1883 xassert (STRINGP (it->string));
1884 }
1885
1886 /* Restore position in display vector translations, control
1887 character translations or ellipses. */
1888 if (pos->dpvec_index >= 0)
1889 {
1890 if (it->dpvec == NULL)
1891 get_next_display_element (it);
1892 xassert (it->dpvec && it->current.dpvec_index == 0);
1893 it->current.dpvec_index = pos->dpvec_index;
1894 }
1895
1896 CHECK_IT (it);
1897 }
1898
1899
1900 /* Initialize IT for stepping through current_buffer in window W
1901 starting at ROW->start. */
1902
1903 static void
1904 init_to_row_start (it, w, row)
1905 struct it *it;
1906 struct window *w;
1907 struct glyph_row *row;
1908 {
1909 init_from_display_pos (it, w, &row->start);
1910 it->continuation_lines_width = row->continuation_lines_width;
1911 CHECK_IT (it);
1912 }
1913
1914
1915 /* Initialize IT for stepping through current_buffer in window W
1916 starting in the line following ROW, i.e. starting at ROW->end. */
1917
1918 static void
1919 init_to_row_end (it, w, row)
1920 struct it *it;
1921 struct window *w;
1922 struct glyph_row *row;
1923 {
1924 init_from_display_pos (it, w, &row->end);
1925
1926 if (row->continued_p)
1927 it->continuation_lines_width = (row->continuation_lines_width
1928 + row->pixel_width);
1929 CHECK_IT (it);
1930 }
1931
1932
1933
1934 \f
1935 /***********************************************************************
1936 Text properties
1937 ***********************************************************************/
1938
1939 /* Called when IT reaches IT->stop_charpos. Handle text property and
1940 overlay changes. Set IT->stop_charpos to the next position where
1941 to stop. */
1942
1943 static void
1944 handle_stop (it)
1945 struct it *it;
1946 {
1947 enum prop_handled handled;
1948 int handle_overlay_change_p = 1;
1949 struct props *p;
1950
1951 it->dpvec = NULL;
1952 it->current.dpvec_index = -1;
1953
1954 do
1955 {
1956 handled = HANDLED_NORMALLY;
1957
1958 /* Call text property handlers. */
1959 for (p = it_props; p->handler; ++p)
1960 {
1961 handled = p->handler (it);
1962
1963 if (handled == HANDLED_RECOMPUTE_PROPS)
1964 break;
1965 else if (handled == HANDLED_RETURN)
1966 return;
1967 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
1968 handle_overlay_change_p = 0;
1969 }
1970
1971 if (handled != HANDLED_RECOMPUTE_PROPS)
1972 {
1973 /* Don't check for overlay strings below when set to deliver
1974 characters from a display vector. */
1975 if (it->method == next_element_from_display_vector)
1976 handle_overlay_change_p = 0;
1977
1978 /* Handle overlay changes. */
1979 if (handle_overlay_change_p)
1980 handled = handle_overlay_change (it);
1981
1982 /* Determine where to stop next. */
1983 if (handled == HANDLED_NORMALLY)
1984 compute_stop_pos (it);
1985 }
1986 }
1987 while (handled == HANDLED_RECOMPUTE_PROPS);
1988 }
1989
1990
1991 /* Compute IT->stop_charpos from text property and overlay change
1992 information for IT's current position. */
1993
1994 static void
1995 compute_stop_pos (it)
1996 struct it *it;
1997 {
1998 register INTERVAL iv, next_iv;
1999 Lisp_Object object, limit, position;
2000
2001 /* If nowhere else, stop at the end. */
2002 it->stop_charpos = it->end_charpos;
2003
2004 if (STRINGP (it->string))
2005 {
2006 /* Strings are usually short, so don't limit the search for
2007 properties. */
2008 object = it->string;
2009 limit = Qnil;
2010 position = make_number (IT_STRING_CHARPOS (*it));
2011 }
2012 else
2013 {
2014 int charpos;
2015
2016 /* If next overlay change is in front of the current stop pos
2017 (which is IT->end_charpos), stop there. Note: value of
2018 next_overlay_change is point-max if no overlay change
2019 follows. */
2020 charpos = next_overlay_change (IT_CHARPOS (*it));
2021 if (charpos < it->stop_charpos)
2022 it->stop_charpos = charpos;
2023
2024 /* If showing the region, we have to stop at the region
2025 start or end because the face might change there. */
2026 if (it->region_beg_charpos > 0)
2027 {
2028 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2029 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2030 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2031 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2032 }
2033
2034 /* Set up variables for computing the stop position from text
2035 property changes. */
2036 XSETBUFFER (object, current_buffer);
2037 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2038 position = make_number (IT_CHARPOS (*it));
2039
2040 }
2041
2042 /* Get the interval containing IT's position. Value is a null
2043 interval if there isn't such an interval. */
2044 iv = validate_interval_range (object, &position, &position, 0);
2045 if (!NULL_INTERVAL_P (iv))
2046 {
2047 Lisp_Object values_here[LAST_PROP_IDX];
2048 struct props *p;
2049
2050 /* Get properties here. */
2051 for (p = it_props; p->handler; ++p)
2052 values_here[p->idx] = textget (iv->plist, *p->name);
2053
2054 /* Look for an interval following iv that has different
2055 properties. */
2056 for (next_iv = next_interval (iv);
2057 (!NULL_INTERVAL_P (next_iv)
2058 && (NILP (limit)
2059 || XFASTINT (limit) > next_iv->position));
2060 next_iv = next_interval (next_iv))
2061 {
2062 for (p = it_props; p->handler; ++p)
2063 {
2064 Lisp_Object new_value;
2065
2066 new_value = textget (next_iv->plist, *p->name);
2067 if (!EQ (values_here[p->idx], new_value))
2068 break;
2069 }
2070
2071 if (p->handler)
2072 break;
2073 }
2074
2075 if (!NULL_INTERVAL_P (next_iv))
2076 {
2077 if (INTEGERP (limit)
2078 && next_iv->position >= XFASTINT (limit))
2079 /* No text property change up to limit. */
2080 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2081 else
2082 /* Text properties change in next_iv. */
2083 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2084 }
2085 }
2086
2087 xassert (STRINGP (it->string)
2088 || (it->stop_charpos >= BEGV
2089 && it->stop_charpos >= IT_CHARPOS (*it)));
2090 }
2091
2092
2093 /* Return the position of the next overlay change after POS in
2094 current_buffer. Value is point-max if no overlay change
2095 follows. This is like `next-overlay-change' but doesn't use
2096 xmalloc. */
2097
2098 static int
2099 next_overlay_change (pos)
2100 int pos;
2101 {
2102 int noverlays;
2103 int endpos;
2104 Lisp_Object *overlays;
2105 int len;
2106 int i;
2107
2108 /* Get all overlays at the given position. */
2109 len = 10;
2110 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2111 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2112 if (noverlays > len)
2113 {
2114 len = noverlays;
2115 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2116 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2117 }
2118
2119 /* If any of these overlays ends before endpos,
2120 use its ending point instead. */
2121 for (i = 0; i < noverlays; ++i)
2122 {
2123 Lisp_Object oend;
2124 int oendpos;
2125
2126 oend = OVERLAY_END (overlays[i]);
2127 oendpos = OVERLAY_POSITION (oend);
2128 endpos = min (endpos, oendpos);
2129 }
2130
2131 return endpos;
2132 }
2133
2134
2135 \f
2136 /***********************************************************************
2137 Fontification
2138 ***********************************************************************/
2139
2140 /* Handle changes in the `fontified' property of the current buffer by
2141 calling hook functions from Qfontification_functions to fontify
2142 regions of text. */
2143
2144 static enum prop_handled
2145 handle_fontified_prop (it)
2146 struct it *it;
2147 {
2148 Lisp_Object prop, pos;
2149 enum prop_handled handled = HANDLED_NORMALLY;
2150
2151 /* Get the value of the `fontified' property at IT's current buffer
2152 position. (The `fontified' property doesn't have a special
2153 meaning in strings.) If the value is nil, call functions from
2154 Qfontification_functions. */
2155 if (!STRINGP (it->string)
2156 && it->s == NULL
2157 && !NILP (Vfontification_functions)
2158 && !NILP (Vrun_hooks)
2159 && (pos = make_number (IT_CHARPOS (*it)),
2160 prop = Fget_char_property (pos, Qfontified, Qnil),
2161 NILP (prop)))
2162 {
2163 int count = BINDING_STACK_SIZE ();
2164 Lisp_Object val;
2165
2166 val = Vfontification_functions;
2167 specbind (Qfontification_functions, Qnil);
2168 specbind (Qafter_change_functions, Qnil);
2169
2170 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2171 safe_call1 (val, pos);
2172 else
2173 {
2174 Lisp_Object globals, fn;
2175 struct gcpro gcpro1, gcpro2;
2176
2177 globals = Qnil;
2178 GCPRO2 (val, globals);
2179
2180 for (; CONSP (val); val = XCDR (val))
2181 {
2182 fn = XCAR (val);
2183
2184 if (EQ (fn, Qt))
2185 {
2186 /* A value of t indicates this hook has a local
2187 binding; it means to run the global binding too.
2188 In a global value, t should not occur. If it
2189 does, we must ignore it to avoid an endless
2190 loop. */
2191 for (globals = Fdefault_value (Qfontification_functions);
2192 CONSP (globals);
2193 globals = XCDR (globals))
2194 {
2195 fn = XCAR (globals);
2196 if (!EQ (fn, Qt))
2197 safe_call1 (fn, pos);
2198 }
2199 }
2200 else
2201 safe_call1 (fn, pos);
2202 }
2203
2204 UNGCPRO;
2205 }
2206
2207 unbind_to (count, Qnil);
2208
2209 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2210 something. This avoids an endless loop if they failed to
2211 fontify the text for which reason ever. */
2212 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2213 handled = HANDLED_RECOMPUTE_PROPS;
2214 }
2215
2216 return handled;
2217 }
2218
2219
2220 \f
2221 /***********************************************************************
2222 Faces
2223 ***********************************************************************/
2224
2225 /* Set up iterator IT from face properties at its current position.
2226 Called from handle_stop. */
2227
2228 static enum prop_handled
2229 handle_face_prop (it)
2230 struct it *it;
2231 {
2232 int new_face_id, next_stop;
2233
2234 if (!STRINGP (it->string))
2235 {
2236 new_face_id
2237 = face_at_buffer_position (it->w,
2238 IT_CHARPOS (*it),
2239 it->region_beg_charpos,
2240 it->region_end_charpos,
2241 &next_stop,
2242 (IT_CHARPOS (*it)
2243 + TEXT_PROP_DISTANCE_LIMIT),
2244 0);
2245
2246 /* Is this a start of a run of characters with box face?
2247 Caveat: this can be called for a freshly initialized
2248 iterator; face_id is -1 is this case. We know that the new
2249 face will not change until limit, i.e. if the new face has a
2250 box, all characters up to limit will have one. But, as
2251 usual, we don't know whether limit is really the end. */
2252 if (new_face_id != it->face_id)
2253 {
2254 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2255
2256 /* If new face has a box but old face has not, this is
2257 the start of a run of characters with box, i.e. it has
2258 a shadow on the left side. The value of face_id of the
2259 iterator will be -1 if this is the initial call that gets
2260 the face. In this case, we have to look in front of IT's
2261 position and see whether there is a face != new_face_id. */
2262 it->start_of_box_run_p
2263 = (new_face->box != FACE_NO_BOX
2264 && (it->face_id >= 0
2265 || IT_CHARPOS (*it) == BEG
2266 || new_face_id != face_before_it_pos (it)));
2267 it->face_box_p = new_face->box != FACE_NO_BOX;
2268 }
2269 }
2270 else
2271 {
2272 int base_face_id, bufpos;
2273
2274 if (it->current.overlay_string_index >= 0)
2275 bufpos = IT_CHARPOS (*it);
2276 else
2277 bufpos = 0;
2278
2279 /* For strings from a buffer, i.e. overlay strings or strings
2280 from a `display' property, use the face at IT's current
2281 buffer position as the base face to merge with, so that
2282 overlay strings appear in the same face as surrounding
2283 text, unless they specify their own faces. */
2284 base_face_id = underlying_face_id (it);
2285
2286 new_face_id = face_at_string_position (it->w,
2287 it->string,
2288 IT_STRING_CHARPOS (*it),
2289 bufpos,
2290 it->region_beg_charpos,
2291 it->region_end_charpos,
2292 &next_stop,
2293 base_face_id, 0);
2294
2295 #if 0 /* This shouldn't be neccessary. Let's check it. */
2296 /* If IT is used to display a mode line we would really like to
2297 use the mode line face instead of the frame's default face. */
2298 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2299 && new_face_id == DEFAULT_FACE_ID)
2300 new_face_id = MODE_LINE_FACE_ID;
2301 #endif
2302
2303 /* Is this a start of a run of characters with box? Caveat:
2304 this can be called for a freshly allocated iterator; face_id
2305 is -1 is this case. We know that the new face will not
2306 change until the next check pos, i.e. if the new face has a
2307 box, all characters up to that position will have a
2308 box. But, as usual, we don't know whether that position
2309 is really the end. */
2310 if (new_face_id != it->face_id)
2311 {
2312 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2313 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2314
2315 /* If new face has a box but old face hasn't, this is the
2316 start of a run of characters with box, i.e. it has a
2317 shadow on the left side. */
2318 it->start_of_box_run_p
2319 = new_face->box && (old_face == NULL || !old_face->box);
2320 it->face_box_p = new_face->box != FACE_NO_BOX;
2321 }
2322 }
2323
2324 it->face_id = new_face_id;
2325 return HANDLED_NORMALLY;
2326 }
2327
2328
2329 /* Return the ID of the face ``underlying'' IT's current position,
2330 which is in a string. If the iterator is associated with a
2331 buffer, return the face at IT's current buffer position.
2332 Otherwise, use the iterator's base_face_id. */
2333
2334 static int
2335 underlying_face_id (it)
2336 struct it *it;
2337 {
2338 int face_id = it->base_face_id, i;
2339
2340 xassert (STRINGP (it->string));
2341
2342 for (i = it->sp - 1; i >= 0; --i)
2343 if (NILP (it->stack[i].string))
2344 face_id = it->stack[i].face_id;
2345
2346 return face_id;
2347 }
2348
2349
2350 /* Compute the face one character before or after the current position
2351 of IT. BEFORE_P non-zero means get the face in front of IT's
2352 position. Value is the id of the face. */
2353
2354 static int
2355 face_before_or_after_it_pos (it, before_p)
2356 struct it *it;
2357 int before_p;
2358 {
2359 int face_id, limit;
2360 int next_check_charpos;
2361 struct text_pos pos;
2362
2363 xassert (it->s == NULL);
2364
2365 if (STRINGP (it->string))
2366 {
2367 int bufpos, base_face_id;
2368
2369 /* No face change past the end of the string (for the case
2370 we are padding with spaces). No face change before the
2371 string start. */
2372 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size
2373 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2374 return it->face_id;
2375
2376 /* Set pos to the position before or after IT's current position. */
2377 if (before_p)
2378 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2379 else
2380 /* For composition, we must check the character after the
2381 composition. */
2382 pos = (it->what == IT_COMPOSITION
2383 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
2384 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
2385
2386 if (it->current.overlay_string_index >= 0)
2387 bufpos = IT_CHARPOS (*it);
2388 else
2389 bufpos = 0;
2390
2391 base_face_id = underlying_face_id (it);
2392
2393 /* Get the face for ASCII, or unibyte. */
2394 face_id = face_at_string_position (it->w,
2395 it->string,
2396 CHARPOS (pos),
2397 bufpos,
2398 it->region_beg_charpos,
2399 it->region_end_charpos,
2400 &next_check_charpos,
2401 base_face_id, 0);
2402
2403 /* Correct the face for charsets different from ASCII. Do it
2404 for the multibyte case only. The face returned above is
2405 suitable for unibyte text if IT->string is unibyte. */
2406 if (STRING_MULTIBYTE (it->string))
2407 {
2408 unsigned char *p = XSTRING (it->string)->data + BYTEPOS (pos);
2409 int rest = STRING_BYTES (XSTRING (it->string)) - BYTEPOS (pos);
2410 int c, len;
2411 struct face *face = FACE_FROM_ID (it->f, face_id);
2412
2413 c = string_char_and_length (p, rest, &len);
2414 face_id = FACE_FOR_CHAR (it->f, face, c);
2415 }
2416 }
2417 else
2418 {
2419 if ((IT_CHARPOS (*it) >= ZV && !before_p)
2420 || (IT_CHARPOS (*it) <= BEGV && before_p))
2421 return it->face_id;
2422
2423 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
2424 pos = it->current.pos;
2425
2426 if (before_p)
2427 DEC_TEXT_POS (pos, it->multibyte_p);
2428 else
2429 {
2430 if (it->what == IT_COMPOSITION)
2431 /* For composition, we must check the position after the
2432 composition. */
2433 pos.charpos += it->cmp_len, pos.bytepos += it->len;
2434 else
2435 INC_TEXT_POS (pos, it->multibyte_p);
2436 }
2437
2438 /* Determine face for CHARSET_ASCII, or unibyte. */
2439 face_id = face_at_buffer_position (it->w,
2440 CHARPOS (pos),
2441 it->region_beg_charpos,
2442 it->region_end_charpos,
2443 &next_check_charpos,
2444 limit, 0);
2445
2446 /* Correct the face for charsets different from ASCII. Do it
2447 for the multibyte case only. The face returned above is
2448 suitable for unibyte text if current_buffer is unibyte. */
2449 if (it->multibyte_p)
2450 {
2451 int c = FETCH_MULTIBYTE_CHAR (CHARPOS (pos));
2452 struct face *face = FACE_FROM_ID (it->f, face_id);
2453 face_id = FACE_FOR_CHAR (it->f, face, c);
2454 }
2455 }
2456
2457 return face_id;
2458 }
2459
2460
2461 \f
2462 /***********************************************************************
2463 Invisible text
2464 ***********************************************************************/
2465
2466 /* Set up iterator IT from invisible properties at its current
2467 position. Called from handle_stop. */
2468
2469 static enum prop_handled
2470 handle_invisible_prop (it)
2471 struct it *it;
2472 {
2473 enum prop_handled handled = HANDLED_NORMALLY;
2474
2475 if (STRINGP (it->string))
2476 {
2477 extern Lisp_Object Qinvisible;
2478 Lisp_Object prop, end_charpos, limit, charpos;
2479
2480 /* Get the value of the invisible text property at the
2481 current position. Value will be nil if there is no such
2482 property. */
2483 charpos = make_number (IT_STRING_CHARPOS (*it));
2484 prop = Fget_text_property (charpos, Qinvisible, it->string);
2485
2486 if (!NILP (prop)
2487 && IT_STRING_CHARPOS (*it) < it->end_charpos)
2488 {
2489 handled = HANDLED_RECOMPUTE_PROPS;
2490
2491 /* Get the position at which the next change of the
2492 invisible text property can be found in IT->string.
2493 Value will be nil if the property value is the same for
2494 all the rest of IT->string. */
2495 XSETINT (limit, XSTRING (it->string)->size);
2496 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2497 it->string, limit);
2498
2499 /* Text at current position is invisible. The next
2500 change in the property is at position end_charpos.
2501 Move IT's current position to that position. */
2502 if (INTEGERP (end_charpos)
2503 && XFASTINT (end_charpos) < XFASTINT (limit))
2504 {
2505 struct text_pos old;
2506 old = it->current.string_pos;
2507 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
2508 compute_string_pos (&it->current.string_pos, old, it->string);
2509 }
2510 else
2511 {
2512 /* The rest of the string is invisible. If this is an
2513 overlay string, proceed with the next overlay string
2514 or whatever comes and return a character from there. */
2515 if (it->current.overlay_string_index >= 0)
2516 {
2517 next_overlay_string (it);
2518 /* Don't check for overlay strings when we just
2519 finished processing them. */
2520 handled = HANDLED_OVERLAY_STRING_CONSUMED;
2521 }
2522 else
2523 {
2524 struct Lisp_String *s = XSTRING (it->string);
2525 IT_STRING_CHARPOS (*it) = s->size;
2526 IT_STRING_BYTEPOS (*it) = STRING_BYTES (s);
2527 }
2528 }
2529 }
2530 }
2531 else
2532 {
2533 int visible_p, newpos, next_stop;
2534 Lisp_Object pos, prop;
2535
2536 /* First of all, is there invisible text at this position? */
2537 pos = make_number (IT_CHARPOS (*it));
2538 prop = Fget_char_property (pos, Qinvisible, it->window);
2539
2540 /* If we are on invisible text, skip over it. */
2541 if (TEXT_PROP_MEANS_INVISIBLE (prop)
2542 && IT_CHARPOS (*it) < it->end_charpos)
2543 {
2544 /* Record whether we have to display an ellipsis for the
2545 invisible text. */
2546 int display_ellipsis_p
2547 = TEXT_PROP_MEANS_INVISIBLE_WITH_ELLIPSIS (prop);
2548
2549 handled = HANDLED_RECOMPUTE_PROPS;
2550
2551 /* Loop skipping over invisible text. The loop is left at
2552 ZV or with IT on the first char being visible again. */
2553 do
2554 {
2555 /* Try to skip some invisible text. Return value is the
2556 position reached which can be equal to IT's position
2557 if there is nothing invisible here. This skips both
2558 over invisible text properties and overlays with
2559 invisible property. */
2560 newpos = skip_invisible (IT_CHARPOS (*it),
2561 &next_stop, ZV, it->window);
2562
2563 /* If we skipped nothing at all we weren't at invisible
2564 text in the first place. If everything to the end of
2565 the buffer was skipped, end the loop. */
2566 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
2567 visible_p = 1;
2568 else
2569 {
2570 /* We skipped some characters but not necessarily
2571 all there are. Check if we ended up on visible
2572 text. Fget_char_property returns the property of
2573 the char before the given position, i.e. if we
2574 get visible_p = 1, this means that the char at
2575 newpos is visible. */
2576 pos = make_number (newpos);
2577 prop = Fget_char_property (pos, Qinvisible, it->window);
2578 visible_p = !TEXT_PROP_MEANS_INVISIBLE (prop);
2579 }
2580
2581 /* If we ended up on invisible text, proceed to
2582 skip starting with next_stop. */
2583 if (!visible_p)
2584 IT_CHARPOS (*it) = next_stop;
2585 }
2586 while (!visible_p);
2587
2588 /* The position newpos is now either ZV or on visible text. */
2589 IT_CHARPOS (*it) = newpos;
2590 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2591
2592 /* Maybe return `...' next for the end of the invisible text. */
2593 if (display_ellipsis_p)
2594 {
2595 if (it->dp
2596 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
2597 {
2598 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
2599 it->dpvec = v->contents;
2600 it->dpend = v->contents + v->size;
2601 }
2602 else
2603 {
2604 /* Default `...'. */
2605 it->dpvec = default_invis_vector;
2606 it->dpend = default_invis_vector + 3;
2607 }
2608
2609 /* The ellipsis display does not replace the display of
2610 the character at the new position. Indicate this by
2611 setting IT->dpvec_char_len to zero. */
2612 it->dpvec_char_len = 0;
2613
2614 it->current.dpvec_index = 0;
2615 it->method = next_element_from_display_vector;
2616 }
2617 }
2618 }
2619
2620 return handled;
2621 }
2622
2623
2624 \f
2625 /***********************************************************************
2626 'display' property
2627 ***********************************************************************/
2628
2629 /* Set up iterator IT from `display' property at its current position.
2630 Called from handle_stop. */
2631
2632 static enum prop_handled
2633 handle_display_prop (it)
2634 struct it *it;
2635 {
2636 Lisp_Object prop, object;
2637 struct text_pos *position;
2638 int display_replaced_p = 0;
2639
2640 if (STRINGP (it->string))
2641 {
2642 object = it->string;
2643 position = &it->current.string_pos;
2644 }
2645 else
2646 {
2647 object = it->w->buffer;
2648 position = &it->current.pos;
2649 }
2650
2651 /* Reset those iterator values set from display property values. */
2652 it->font_height = Qnil;
2653 it->space_width = Qnil;
2654 it->voffset = 0;
2655
2656 /* We don't support recursive `display' properties, i.e. string
2657 values that have a string `display' property, that have a string
2658 `display' property etc. */
2659 if (!it->string_from_display_prop_p)
2660 it->area = TEXT_AREA;
2661
2662 prop = Fget_char_property (make_number (position->charpos),
2663 Qdisplay, object);
2664 if (NILP (prop))
2665 return HANDLED_NORMALLY;
2666
2667 if (CONSP (prop)
2668 && CONSP (XCAR (prop))
2669 && !EQ (Qmargin, XCAR (XCAR (prop))))
2670 {
2671 /* A list of sub-properties. */
2672 for (; CONSP (prop); prop = XCDR (prop))
2673 {
2674 if (handle_single_display_prop (it, XCAR (prop), object,
2675 position, display_replaced_p))
2676 display_replaced_p = 1;
2677 }
2678 }
2679 else if (VECTORP (prop))
2680 {
2681 int i;
2682 for (i = 0; i < ASIZE (prop); ++i)
2683 if (handle_single_display_prop (it, AREF (prop, i), object,
2684 position, display_replaced_p))
2685 display_replaced_p = 1;
2686 }
2687 else
2688 {
2689 if (handle_single_display_prop (it, prop, object, position, 0))
2690 display_replaced_p = 1;
2691 }
2692
2693 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
2694 }
2695
2696
2697 /* Value is the position of the end of the `display' property starting
2698 at START_POS in OBJECT. */
2699
2700 static struct text_pos
2701 display_prop_end (it, object, start_pos)
2702 struct it *it;
2703 Lisp_Object object;
2704 struct text_pos start_pos;
2705 {
2706 Lisp_Object end;
2707 struct text_pos end_pos;
2708
2709 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
2710 Qdisplay, object, Qnil);
2711 CHARPOS (end_pos) = XFASTINT (end);
2712 if (STRINGP (object))
2713 compute_string_pos (&end_pos, start_pos, it->string);
2714 else
2715 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
2716
2717 return end_pos;
2718 }
2719
2720
2721 /* Set up IT from a single `display' sub-property value PROP. OBJECT
2722 is the object in which the `display' property was found. *POSITION
2723 is the position at which it was found. DISPLAY_REPLACED_P non-zero
2724 means that we previously saw a display sub-property which already
2725 replaced text display with something else, for example an image;
2726 ignore such properties after the first one has been processed.
2727
2728 If PROP is a `space' or `image' sub-property, set *POSITION to the
2729 end position of the `display' property.
2730
2731 Value is non-zero something was found which replaces the display
2732 of buffer or string text. */
2733
2734 static int
2735 handle_single_display_prop (it, prop, object, position,
2736 display_replaced_before_p)
2737 struct it *it;
2738 Lisp_Object prop;
2739 Lisp_Object object;
2740 struct text_pos *position;
2741 int display_replaced_before_p;
2742 {
2743 Lisp_Object value;
2744 int replaces_text_display_p = 0;
2745 Lisp_Object form;
2746
2747 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
2748 evaluated. If the result is nil, VALUE is ignored. */
2749 form = Qt;
2750 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2751 {
2752 prop = XCDR (prop);
2753 if (!CONSP (prop))
2754 return 0;
2755 form = XCAR (prop);
2756 prop = XCDR (prop);
2757 }
2758
2759 if (!NILP (form) && !EQ (form, Qt))
2760 {
2761 struct gcpro gcpro1;
2762 struct text_pos end_pos, pt;
2763
2764 GCPRO1 (form);
2765 end_pos = display_prop_end (it, object, *position);
2766
2767 /* Temporarily set point to the end position, and then evaluate
2768 the form. This makes `(eolp)' work as FORM. */
2769 if (BUFFERP (object))
2770 {
2771 CHARPOS (pt) = PT;
2772 BYTEPOS (pt) = PT_BYTE;
2773 TEMP_SET_PT_BOTH (CHARPOS (end_pos), BYTEPOS (end_pos));
2774 }
2775
2776 form = safe_eval (form);
2777
2778 if (BUFFERP (object))
2779 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
2780 UNGCPRO;
2781 }
2782
2783 if (NILP (form))
2784 return 0;
2785
2786 if (CONSP (prop)
2787 && EQ (XCAR (prop), Qheight)
2788 && CONSP (XCDR (prop)))
2789 {
2790 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2791 return 0;
2792
2793 /* `(height HEIGHT)'. */
2794 it->font_height = XCAR (XCDR (prop));
2795 if (!NILP (it->font_height))
2796 {
2797 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2798 int new_height = -1;
2799
2800 if (CONSP (it->font_height)
2801 && (EQ (XCAR (it->font_height), Qplus)
2802 || EQ (XCAR (it->font_height), Qminus))
2803 && CONSP (XCDR (it->font_height))
2804 && INTEGERP (XCAR (XCDR (it->font_height))))
2805 {
2806 /* `(+ N)' or `(- N)' where N is an integer. */
2807 int steps = XINT (XCAR (XCDR (it->font_height)));
2808 if (EQ (XCAR (it->font_height), Qplus))
2809 steps = - steps;
2810 it->face_id = smaller_face (it->f, it->face_id, steps);
2811 }
2812 else if (FUNCTIONP (it->font_height))
2813 {
2814 /* Call function with current height as argument.
2815 Value is the new height. */
2816 Lisp_Object height;
2817 height = safe_call1 (it->font_height,
2818 face->lface[LFACE_HEIGHT_INDEX]);
2819 if (NUMBERP (height))
2820 new_height = XFLOATINT (height);
2821 }
2822 else if (NUMBERP (it->font_height))
2823 {
2824 /* Value is a multiple of the canonical char height. */
2825 struct face *face;
2826
2827 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
2828 new_height = (XFLOATINT (it->font_height)
2829 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
2830 }
2831 else
2832 {
2833 /* Evaluate IT->font_height with `height' bound to the
2834 current specified height to get the new height. */
2835 Lisp_Object value;
2836 int count = BINDING_STACK_SIZE ();
2837
2838 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
2839 value = safe_eval (it->font_height);
2840 unbind_to (count, Qnil);
2841
2842 if (NUMBERP (value))
2843 new_height = XFLOATINT (value);
2844 }
2845
2846 if (new_height > 0)
2847 it->face_id = face_with_height (it->f, it->face_id, new_height);
2848 }
2849 }
2850 else if (CONSP (prop)
2851 && EQ (XCAR (prop), Qspace_width)
2852 && CONSP (XCDR (prop)))
2853 {
2854 /* `(space_width WIDTH)'. */
2855 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2856 return 0;
2857
2858 value = XCAR (XCDR (prop));
2859 if (NUMBERP (value) && XFLOATINT (value) > 0)
2860 it->space_width = value;
2861 }
2862 else if (CONSP (prop)
2863 && EQ (XCAR (prop), Qraise)
2864 && CONSP (XCDR (prop)))
2865 {
2866 /* `(raise FACTOR)'. */
2867 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2868 return 0;
2869
2870 #ifdef HAVE_WINDOW_SYSTEM
2871 value = XCAR (XCDR (prop));
2872 if (NUMBERP (value))
2873 {
2874 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2875 it->voffset = - (XFLOATINT (value)
2876 * (FONT_HEIGHT (face->font)));
2877 }
2878 #endif /* HAVE_WINDOW_SYSTEM */
2879 }
2880 else if (!it->string_from_display_prop_p)
2881 {
2882 /* `((margin left-margin) VALUE)' or `((margin right-margin)
2883 VALUE) or `((margin nil) VALUE)' or VALUE. */
2884 Lisp_Object location, value;
2885 struct text_pos start_pos;
2886 int valid_p;
2887
2888 /* Characters having this form of property are not displayed, so
2889 we have to find the end of the property. */
2890 start_pos = *position;
2891 *position = display_prop_end (it, object, start_pos);
2892 value = Qnil;
2893
2894 /* Let's stop at the new position and assume that all
2895 text properties change there. */
2896 it->stop_charpos = position->charpos;
2897
2898 location = Qunbound;
2899 if (CONSP (prop) && CONSP (XCAR (prop)))
2900 {
2901 Lisp_Object tem;
2902
2903 value = XCDR (prop);
2904 if (CONSP (value))
2905 value = XCAR (value);
2906
2907 tem = XCAR (prop);
2908 if (EQ (XCAR (tem), Qmargin)
2909 && (tem = XCDR (tem),
2910 tem = CONSP (tem) ? XCAR (tem) : Qnil,
2911 (NILP (tem)
2912 || EQ (tem, Qleft_margin)
2913 || EQ (tem, Qright_margin))))
2914 location = tem;
2915 }
2916
2917 if (EQ (location, Qunbound))
2918 {
2919 location = Qnil;
2920 value = prop;
2921 }
2922
2923 #ifdef HAVE_WINDOW_SYSTEM
2924 if (FRAME_TERMCAP_P (it->f))
2925 valid_p = STRINGP (value);
2926 else
2927 valid_p = (STRINGP (value)
2928 || (CONSP (value) && EQ (XCAR (value), Qspace))
2929 || valid_image_p (value));
2930 #else /* not HAVE_WINDOW_SYSTEM */
2931 valid_p = STRINGP (value);
2932 #endif /* not HAVE_WINDOW_SYSTEM */
2933
2934 if ((EQ (location, Qleft_margin)
2935 || EQ (location, Qright_margin)
2936 || NILP (location))
2937 && valid_p
2938 && !display_replaced_before_p)
2939 {
2940 replaces_text_display_p = 1;
2941
2942 /* Save current settings of IT so that we can restore them
2943 when we are finished with the glyph property value. */
2944 push_it (it);
2945
2946 if (NILP (location))
2947 it->area = TEXT_AREA;
2948 else if (EQ (location, Qleft_margin))
2949 it->area = LEFT_MARGIN_AREA;
2950 else
2951 it->area = RIGHT_MARGIN_AREA;
2952
2953 if (STRINGP (value))
2954 {
2955 it->string = value;
2956 it->multibyte_p = STRING_MULTIBYTE (it->string);
2957 it->current.overlay_string_index = -1;
2958 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
2959 it->end_charpos = it->string_nchars
2960 = XSTRING (it->string)->size;
2961 it->method = next_element_from_string;
2962 it->stop_charpos = 0;
2963 it->string_from_display_prop_p = 1;
2964 /* Say that we haven't consumed the characters with
2965 `display' property yet. The call to pop_it in
2966 set_iterator_to_next will clean this up. */
2967 *position = start_pos;
2968 }
2969 else if (CONSP (value) && EQ (XCAR (value), Qspace))
2970 {
2971 it->method = next_element_from_stretch;
2972 it->object = value;
2973 it->current.pos = it->position = start_pos;
2974 }
2975 #ifdef HAVE_WINDOW_SYSTEM
2976 else
2977 {
2978 it->what = IT_IMAGE;
2979 it->image_id = lookup_image (it->f, value);
2980 it->position = start_pos;
2981 it->object = NILP (object) ? it->w->buffer : object;
2982 it->method = next_element_from_image;
2983
2984 /* Say that we haven't consumed the characters with
2985 `display' property yet. The call to pop_it in
2986 set_iterator_to_next will clean this up. */
2987 *position = start_pos;
2988 }
2989 #endif /* HAVE_WINDOW_SYSTEM */
2990 }
2991 else
2992 /* Invalid property or property not supported. Restore
2993 the position to what it was before. */
2994 *position = start_pos;
2995 }
2996
2997 return replaces_text_display_p;
2998 }
2999
3000
3001 /* Check if PROP is a display sub-property value whose text should be
3002 treated as intangible. */
3003
3004 static int
3005 single_display_prop_intangible_p (prop)
3006 Lisp_Object prop;
3007 {
3008 /* Skip over `when FORM'. */
3009 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3010 {
3011 prop = XCDR (prop);
3012 if (!CONSP (prop))
3013 return 0;
3014 prop = XCDR (prop);
3015 }
3016
3017 if (!CONSP (prop))
3018 return 0;
3019
3020 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3021 we don't need to treat text as intangible. */
3022 if (EQ (XCAR (prop), Qmargin))
3023 {
3024 prop = XCDR (prop);
3025 if (!CONSP (prop))
3026 return 0;
3027
3028 prop = XCDR (prop);
3029 if (!CONSP (prop)
3030 || EQ (XCAR (prop), Qleft_margin)
3031 || EQ (XCAR (prop), Qright_margin))
3032 return 0;
3033 }
3034
3035 return CONSP (prop) && EQ (XCAR (prop), Qimage);
3036 }
3037
3038
3039 /* Check if PROP is a display property value whose text should be
3040 treated as intangible. */
3041
3042 int
3043 display_prop_intangible_p (prop)
3044 Lisp_Object prop;
3045 {
3046 if (CONSP (prop)
3047 && CONSP (XCAR (prop))
3048 && !EQ (Qmargin, XCAR (XCAR (prop))))
3049 {
3050 /* A list of sub-properties. */
3051 while (CONSP (prop))
3052 {
3053 if (single_display_prop_intangible_p (XCAR (prop)))
3054 return 1;
3055 prop = XCDR (prop);
3056 }
3057 }
3058 else if (VECTORP (prop))
3059 {
3060 /* A vector of sub-properties. */
3061 int i;
3062 for (i = 0; i < ASIZE (prop); ++i)
3063 if (single_display_prop_intangible_p (AREF (prop, i)))
3064 return 1;
3065 }
3066 else
3067 return single_display_prop_intangible_p (prop);
3068
3069 return 0;
3070 }
3071
3072
3073 /* Return 1 if PROP is a display sub-property value containing STRING. */
3074
3075 static int
3076 single_display_prop_string_p (prop, string)
3077 Lisp_Object prop, string;
3078 {
3079 extern Lisp_Object Qwhen, Qmargin;
3080
3081 if (EQ (string, prop))
3082 return 1;
3083
3084 /* Skip over `when FORM'. */
3085 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3086 {
3087 prop = XCDR (prop);
3088 if (!CONSP (prop))
3089 return 0;
3090 prop = XCDR (prop);
3091 }
3092
3093 if (CONSP (prop))
3094 /* Skip over `margin LOCATION'. */
3095 if (EQ (XCAR (prop), Qmargin))
3096 {
3097 prop = XCDR (prop);
3098 if (!CONSP (prop))
3099 return 0;
3100
3101 prop = XCDR (prop);
3102 if (!CONSP (prop))
3103 return 0;
3104 }
3105
3106 return CONSP (prop) && EQ (XCAR (prop), string);
3107 }
3108
3109
3110 /* Return 1 if STRING appears in the `display' property PROP. */
3111
3112 static int
3113 display_prop_string_p (prop, string)
3114 Lisp_Object prop, string;
3115 {
3116 extern Lisp_Object Qwhen, Qmargin;
3117
3118 if (CONSP (prop)
3119 && CONSP (XCAR (prop))
3120 && !EQ (Qmargin, XCAR (XCAR (prop))))
3121 {
3122 /* A list of sub-properties. */
3123 while (CONSP (prop))
3124 {
3125 if (single_display_prop_string_p (XCAR (prop), string))
3126 return 1;
3127 prop = XCDR (prop);
3128 }
3129 }
3130 else if (VECTORP (prop))
3131 {
3132 /* A vector of sub-properties. */
3133 int i;
3134 for (i = 0; i < ASIZE (prop); ++i)
3135 if (single_display_prop_string_p (AREF (prop, i), string))
3136 return 1;
3137 }
3138 else
3139 return single_display_prop_string_p (prop, string);
3140
3141 return 0;
3142 }
3143
3144
3145 /* Determine from which buffer position in W's buffer STRING comes
3146 from. AROUND_CHARPOS is an approximate position where it could
3147 be from. Value is the buffer position or 0 if it couldn't be
3148 determined.
3149
3150 W's buffer must be current.
3151
3152 This function is necessary because we don't record buffer positions
3153 in glyphs generated from strings (to keep struct glyph small).
3154 This function may only use code that doesn't eval because it is
3155 called asynchronously from note_mouse_highlight. */
3156
3157 int
3158 string_buffer_position (w, string, around_charpos)
3159 struct window *w;
3160 Lisp_Object string;
3161 int around_charpos;
3162 {
3163 Lisp_Object around = make_number (around_charpos);
3164 Lisp_Object limit, prop, pos;
3165 const int MAX_DISTANCE = 1000;
3166 int found = 0;
3167
3168 pos = make_number (around_charpos);
3169 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
3170 while (!found && !EQ (pos, limit))
3171 {
3172 prop = Fget_char_property (pos, Qdisplay, Qnil);
3173 if (!NILP (prop) && display_prop_string_p (prop, string))
3174 found = 1;
3175 else
3176 pos = Fnext_single_property_change (pos, Qdisplay, Qnil, limit);
3177 }
3178
3179 if (!found)
3180 {
3181 pos = make_number (around_charpos);
3182 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
3183 while (!found && !EQ (pos, limit))
3184 {
3185 prop = Fget_char_property (pos, Qdisplay, Qnil);
3186 if (!NILP (prop) && display_prop_string_p (prop, string))
3187 found = 1;
3188 else
3189 pos = Fprevious_single_property_change (pos, Qdisplay, Qnil,
3190 limit);
3191 }
3192 }
3193
3194 return found ? XINT (pos) : 0;
3195 }
3196
3197
3198 \f
3199 /***********************************************************************
3200 `composition' property
3201 ***********************************************************************/
3202
3203 /* Set up iterator IT from `composition' property at its current
3204 position. Called from handle_stop. */
3205
3206 static enum prop_handled
3207 handle_composition_prop (it)
3208 struct it *it;
3209 {
3210 Lisp_Object prop, string;
3211 int pos, pos_byte, end;
3212 enum prop_handled handled = HANDLED_NORMALLY;
3213
3214 if (STRINGP (it->string))
3215 {
3216 pos = IT_STRING_CHARPOS (*it);
3217 pos_byte = IT_STRING_BYTEPOS (*it);
3218 string = it->string;
3219 }
3220 else
3221 {
3222 pos = IT_CHARPOS (*it);
3223 pos_byte = IT_BYTEPOS (*it);
3224 string = Qnil;
3225 }
3226
3227 /* If there's a valid composition and point is not inside of the
3228 composition (in the case that the composition is from the current
3229 buffer), draw a glyph composed from the composition components. */
3230 if (find_composition (pos, -1, &pos, &end, &prop, string)
3231 && COMPOSITION_VALID_P (pos, end, prop)
3232 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
3233 {
3234 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
3235
3236 if (id >= 0)
3237 {
3238 it->method = next_element_from_composition;
3239 it->cmp_id = id;
3240 it->cmp_len = COMPOSITION_LENGTH (prop);
3241 /* For a terminal, draw only the first character of the
3242 components. */
3243 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
3244 it->len = (STRINGP (it->string)
3245 ? string_char_to_byte (it->string, end)
3246 : CHAR_TO_BYTE (end)) - pos_byte;
3247 it->stop_charpos = end;
3248 handled = HANDLED_RETURN;
3249 }
3250 }
3251
3252 return handled;
3253 }
3254
3255
3256 \f
3257 /***********************************************************************
3258 Overlay strings
3259 ***********************************************************************/
3260
3261 /* The following structure is used to record overlay strings for
3262 later sorting in load_overlay_strings. */
3263
3264 struct overlay_entry
3265 {
3266 Lisp_Object overlay;
3267 Lisp_Object string;
3268 int priority;
3269 int after_string_p;
3270 };
3271
3272
3273 /* Set up iterator IT from overlay strings at its current position.
3274 Called from handle_stop. */
3275
3276 static enum prop_handled
3277 handle_overlay_change (it)
3278 struct it *it;
3279 {
3280 if (!STRINGP (it->string) && get_overlay_strings (it))
3281 return HANDLED_RECOMPUTE_PROPS;
3282 else
3283 return HANDLED_NORMALLY;
3284 }
3285
3286
3287 /* Set up the next overlay string for delivery by IT, if there is an
3288 overlay string to deliver. Called by set_iterator_to_next when the
3289 end of the current overlay string is reached. If there are more
3290 overlay strings to display, IT->string and
3291 IT->current.overlay_string_index are set appropriately here.
3292 Otherwise IT->string is set to nil. */
3293
3294 static void
3295 next_overlay_string (it)
3296 struct it *it;
3297 {
3298 ++it->current.overlay_string_index;
3299 if (it->current.overlay_string_index == it->n_overlay_strings)
3300 {
3301 /* No more overlay strings. Restore IT's settings to what
3302 they were before overlay strings were processed, and
3303 continue to deliver from current_buffer. */
3304 pop_it (it);
3305 xassert (it->stop_charpos >= BEGV
3306 && it->stop_charpos <= it->end_charpos);
3307 it->string = Qnil;
3308 it->current.overlay_string_index = -1;
3309 SET_TEXT_POS (it->current.string_pos, -1, -1);
3310 it->n_overlay_strings = 0;
3311 it->method = next_element_from_buffer;
3312
3313 /* If we're at the end of the buffer, record that we have
3314 processed the overlay strings there already, so that
3315 next_element_from_buffer doesn't try it again. */
3316 if (IT_CHARPOS (*it) >= it->end_charpos)
3317 it->overlay_strings_at_end_processed_p = 1;
3318 }
3319 else
3320 {
3321 /* There are more overlay strings to process. If
3322 IT->current.overlay_string_index has advanced to a position
3323 where we must load IT->overlay_strings with more strings, do
3324 it. */
3325 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
3326
3327 if (it->current.overlay_string_index && i == 0)
3328 load_overlay_strings (it);
3329
3330 /* Initialize IT to deliver display elements from the overlay
3331 string. */
3332 it->string = it->overlay_strings[i];
3333 it->multibyte_p = STRING_MULTIBYTE (it->string);
3334 SET_TEXT_POS (it->current.string_pos, 0, 0);
3335 it->method = next_element_from_string;
3336 it->stop_charpos = 0;
3337 }
3338
3339 CHECK_IT (it);
3340 }
3341
3342
3343 /* Compare two overlay_entry structures E1 and E2. Used as a
3344 comparison function for qsort in load_overlay_strings. Overlay
3345 strings for the same position are sorted so that
3346
3347 1. All after-strings come in front of before-strings, except
3348 when they come from the same overlay.
3349
3350 2. Within after-strings, strings are sorted so that overlay strings
3351 from overlays with higher priorities come first.
3352
3353 2. Within before-strings, strings are sorted so that overlay
3354 strings from overlays with higher priorities come last.
3355
3356 Value is analogous to strcmp. */
3357
3358
3359 static int
3360 compare_overlay_entries (e1, e2)
3361 void *e1, *e2;
3362 {
3363 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
3364 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
3365 int result;
3366
3367 if (entry1->after_string_p != entry2->after_string_p)
3368 {
3369 /* Let after-strings appear in front of before-strings if
3370 they come from different overlays. */
3371 if (EQ (entry1->overlay, entry2->overlay))
3372 result = entry1->after_string_p ? 1 : -1;
3373 else
3374 result = entry1->after_string_p ? -1 : 1;
3375 }
3376 else if (entry1->after_string_p)
3377 /* After-strings sorted in order of decreasing priority. */
3378 result = entry2->priority - entry1->priority;
3379 else
3380 /* Before-strings sorted in order of increasing priority. */
3381 result = entry1->priority - entry2->priority;
3382
3383 return result;
3384 }
3385
3386
3387 /* Load the vector IT->overlay_strings with overlay strings from IT's
3388 current buffer position. Set IT->n_overlays to the total number of
3389 overlay strings found.
3390
3391 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
3392 a time. On entry into load_overlay_strings,
3393 IT->current.overlay_string_index gives the number of overlay
3394 strings that have already been loaded by previous calls to this
3395 function.
3396
3397 IT->add_overlay_start contains an additional overlay start
3398 position to consider for taking overlay strings from, if non-zero.
3399 This position comes into play when the overlay has an `invisible'
3400 property, and both before and after-strings. When we've skipped to
3401 the end of the overlay, because of its `invisible' property, we
3402 nevertheless want its before-string to appear.
3403 IT->add_overlay_start will contain the overlay start position
3404 in this case.
3405
3406 Overlay strings are sorted so that after-string strings come in
3407 front of before-string strings. Within before and after-strings,
3408 strings are sorted by overlay priority. See also function
3409 compare_overlay_entries. */
3410
3411 static void
3412 load_overlay_strings (it)
3413 struct it *it;
3414 {
3415 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
3416 Lisp_Object ov, overlay, window, str, invisible;
3417 int start, end;
3418 int size = 20;
3419 int n = 0, i, j, invis_p;
3420 struct overlay_entry *entries
3421 = (struct overlay_entry *) alloca (size * sizeof *entries);
3422 int charpos = IT_CHARPOS (*it);
3423
3424 /* Append the overlay string STRING of overlay OVERLAY to vector
3425 `entries' which has size `size' and currently contains `n'
3426 elements. AFTER_P non-zero means STRING is an after-string of
3427 OVERLAY. */
3428 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
3429 do \
3430 { \
3431 Lisp_Object priority; \
3432 \
3433 if (n == size) \
3434 { \
3435 int new_size = 2 * size; \
3436 struct overlay_entry *old = entries; \
3437 entries = \
3438 (struct overlay_entry *) alloca (new_size \
3439 * sizeof *entries); \
3440 bcopy (old, entries, size * sizeof *entries); \
3441 size = new_size; \
3442 } \
3443 \
3444 entries[n].string = (STRING); \
3445 entries[n].overlay = (OVERLAY); \
3446 priority = Foverlay_get ((OVERLAY), Qpriority); \
3447 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
3448 entries[n].after_string_p = (AFTER_P); \
3449 ++n; \
3450 } \
3451 while (0)
3452
3453 /* Process overlay before the overlay center. */
3454 for (ov = current_buffer->overlays_before; CONSP (ov); ov = XCDR (ov))
3455 {
3456 overlay = XCAR (ov);
3457 xassert (OVERLAYP (overlay));
3458 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3459 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3460
3461 if (end < charpos)
3462 break;
3463
3464 /* Skip this overlay if it doesn't start or end at IT's current
3465 position. */
3466 if (end != charpos && start != charpos)
3467 continue;
3468
3469 /* Skip this overlay if it doesn't apply to IT->w. */
3470 window = Foverlay_get (overlay, Qwindow);
3471 if (WINDOWP (window) && XWINDOW (window) != it->w)
3472 continue;
3473
3474 /* If the text ``under'' the overlay is invisible, both before-
3475 and after-strings from this overlay are visible; start and
3476 end position are indistinguishable. */
3477 invisible = Foverlay_get (overlay, Qinvisible);
3478 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3479
3480 /* If overlay has a non-empty before-string, record it. */
3481 if ((start == charpos || (end == charpos && invis_p))
3482 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3483 && XSTRING (str)->size)
3484 RECORD_OVERLAY_STRING (overlay, str, 0);
3485
3486 /* If overlay has a non-empty after-string, record it. */
3487 if ((end == charpos || (start == charpos && invis_p))
3488 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3489 && XSTRING (str)->size)
3490 RECORD_OVERLAY_STRING (overlay, str, 1);
3491 }
3492
3493 /* Process overlays after the overlay center. */
3494 for (ov = current_buffer->overlays_after; CONSP (ov); ov = XCDR (ov))
3495 {
3496 overlay = XCAR (ov);
3497 xassert (OVERLAYP (overlay));
3498 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3499 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3500
3501 if (start > charpos)
3502 break;
3503
3504 /* Skip this overlay if it doesn't start or end at IT's current
3505 position. */
3506 if (end != charpos && start != charpos)
3507 continue;
3508
3509 /* Skip this overlay if it doesn't apply to IT->w. */
3510 window = Foverlay_get (overlay, Qwindow);
3511 if (WINDOWP (window) && XWINDOW (window) != it->w)
3512 continue;
3513
3514 /* If the text ``under'' the overlay is invisible, it has a zero
3515 dimension, and both before- and after-strings apply. */
3516 invisible = Foverlay_get (overlay, Qinvisible);
3517 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3518
3519 /* If overlay has a non-empty before-string, record it. */
3520 if ((start == charpos || (end == charpos && invis_p))
3521 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3522 && XSTRING (str)->size)
3523 RECORD_OVERLAY_STRING (overlay, str, 0);
3524
3525 /* If overlay has a non-empty after-string, record it. */
3526 if ((end == charpos || (start == charpos && invis_p))
3527 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3528 && XSTRING (str)->size)
3529 RECORD_OVERLAY_STRING (overlay, str, 1);
3530 }
3531
3532 #undef RECORD_OVERLAY_STRING
3533
3534 /* Sort entries. */
3535 if (n > 1)
3536 qsort (entries, n, sizeof *entries, compare_overlay_entries);
3537
3538 /* Record the total number of strings to process. */
3539 it->n_overlay_strings = n;
3540
3541 /* IT->current.overlay_string_index is the number of overlay strings
3542 that have already been consumed by IT. Copy some of the
3543 remaining overlay strings to IT->overlay_strings. */
3544 i = 0;
3545 j = it->current.overlay_string_index;
3546 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
3547 it->overlay_strings[i++] = entries[j++].string;
3548
3549 CHECK_IT (it);
3550 }
3551
3552
3553 /* Get the first chunk of overlay strings at IT's current buffer
3554 position. Value is non-zero if at least one overlay string was
3555 found. */
3556
3557 static int
3558 get_overlay_strings (it)
3559 struct it *it;
3560 {
3561 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
3562 process. This fills IT->overlay_strings with strings, and sets
3563 IT->n_overlay_strings to the total number of strings to process.
3564 IT->pos.overlay_string_index has to be set temporarily to zero
3565 because load_overlay_strings needs this; it must be set to -1
3566 when no overlay strings are found because a zero value would
3567 indicate a position in the first overlay string. */
3568 it->current.overlay_string_index = 0;
3569 load_overlay_strings (it);
3570
3571 /* If we found overlay strings, set up IT to deliver display
3572 elements from the first one. Otherwise set up IT to deliver
3573 from current_buffer. */
3574 if (it->n_overlay_strings)
3575 {
3576 /* Make sure we know settings in current_buffer, so that we can
3577 restore meaningful values when we're done with the overlay
3578 strings. */
3579 compute_stop_pos (it);
3580 xassert (it->face_id >= 0);
3581
3582 /* Save IT's settings. They are restored after all overlay
3583 strings have been processed. */
3584 xassert (it->sp == 0);
3585 push_it (it);
3586
3587 /* Set up IT to deliver display elements from the first overlay
3588 string. */
3589 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3590 it->stop_charpos = 0;
3591 it->string = it->overlay_strings[0];
3592 it->multibyte_p = STRING_MULTIBYTE (it->string);
3593 xassert (STRINGP (it->string));
3594 it->method = next_element_from_string;
3595 }
3596 else
3597 {
3598 it->string = Qnil;
3599 it->current.overlay_string_index = -1;
3600 it->method = next_element_from_buffer;
3601 }
3602
3603 CHECK_IT (it);
3604
3605 /* Value is non-zero if we found at least one overlay string. */
3606 return STRINGP (it->string);
3607 }
3608
3609
3610 \f
3611 /***********************************************************************
3612 Saving and restoring state
3613 ***********************************************************************/
3614
3615 /* Save current settings of IT on IT->stack. Called, for example,
3616 before setting up IT for an overlay string, to be able to restore
3617 IT's settings to what they were after the overlay string has been
3618 processed. */
3619
3620 static void
3621 push_it (it)
3622 struct it *it;
3623 {
3624 struct iterator_stack_entry *p;
3625
3626 xassert (it->sp < 2);
3627 p = it->stack + it->sp;
3628
3629 p->stop_charpos = it->stop_charpos;
3630 xassert (it->face_id >= 0);
3631 p->face_id = it->face_id;
3632 p->string = it->string;
3633 p->pos = it->current;
3634 p->end_charpos = it->end_charpos;
3635 p->string_nchars = it->string_nchars;
3636 p->area = it->area;
3637 p->multibyte_p = it->multibyte_p;
3638 p->space_width = it->space_width;
3639 p->font_height = it->font_height;
3640 p->voffset = it->voffset;
3641 p->string_from_display_prop_p = it->string_from_display_prop_p;
3642 ++it->sp;
3643 }
3644
3645
3646 /* Restore IT's settings from IT->stack. Called, for example, when no
3647 more overlay strings must be processed, and we return to delivering
3648 display elements from a buffer, or when the end of a string from a
3649 `display' property is reached and we return to delivering display
3650 elements from an overlay string, or from a buffer. */
3651
3652 static void
3653 pop_it (it)
3654 struct it *it;
3655 {
3656 struct iterator_stack_entry *p;
3657
3658 xassert (it->sp > 0);
3659 --it->sp;
3660 p = it->stack + it->sp;
3661 it->stop_charpos = p->stop_charpos;
3662 it->face_id = p->face_id;
3663 it->string = p->string;
3664 it->current = p->pos;
3665 it->end_charpos = p->end_charpos;
3666 it->string_nchars = p->string_nchars;
3667 it->area = p->area;
3668 it->multibyte_p = p->multibyte_p;
3669 it->space_width = p->space_width;
3670 it->font_height = p->font_height;
3671 it->voffset = p->voffset;
3672 it->string_from_display_prop_p = p->string_from_display_prop_p;
3673 }
3674
3675
3676 \f
3677 /***********************************************************************
3678 Moving over lines
3679 ***********************************************************************/
3680
3681 /* Set IT's current position to the previous line start. */
3682
3683 static void
3684 back_to_previous_line_start (it)
3685 struct it *it;
3686 {
3687 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
3688 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3689 }
3690
3691
3692 /* Move IT to the next line start.
3693
3694 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
3695 we skipped over part of the text (as opposed to moving the iterator
3696 continuously over the text). Otherwise, don't change the value
3697 of *SKIPPED_P.
3698
3699 Newlines may come from buffer text, overlay strings, or strings
3700 displayed via the `display' property. That's the reason we can't
3701 simply use find_next_newline_no_quit.
3702
3703 Note that this function may not skip over invisible text that is so
3704 because of text properties and immediately follows a newline. If
3705 it would, function reseat_at_next_visible_line_start, when called
3706 from set_iterator_to_next, would effectively make invisible
3707 characters following a newline part of the wrong glyph row, which
3708 leads to wrong cursor motion. */
3709
3710 static int
3711 forward_to_next_line_start (it, skipped_p)
3712 struct it *it;
3713 int *skipped_p;
3714 {
3715 int old_selective, newline_found_p, n;
3716 const int MAX_NEWLINE_DISTANCE = 500;
3717
3718 /* If already on a newline, just consume it to avoid unintended
3719 skipping over invisible text below. */
3720 if (it->what == IT_CHARACTER
3721 && it->c == '\n'
3722 && CHARPOS (it->position) == IT_CHARPOS (*it))
3723 {
3724 set_iterator_to_next (it, 0);
3725 it->c = 0;
3726 return 1;
3727 }
3728
3729 /* Don't handle selective display in the following. It's (a)
3730 unnecessary because it's done by the caller, and (b) leads to an
3731 infinite recursion because next_element_from_ellipsis indirectly
3732 calls this function. */
3733 old_selective = it->selective;
3734 it->selective = 0;
3735
3736 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
3737 from buffer text. */
3738 for (n = newline_found_p = 0;
3739 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
3740 n += STRINGP (it->string) ? 0 : 1)
3741 {
3742 if (!get_next_display_element (it))
3743 break;
3744 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
3745 set_iterator_to_next (it, 0);
3746 }
3747
3748 /* If we didn't find a newline near enough, see if we can use a
3749 short-cut. */
3750 if (n == MAX_NEWLINE_DISTANCE)
3751 {
3752 int start = IT_CHARPOS (*it);
3753 int limit = find_next_newline_no_quit (start, 1);
3754 Lisp_Object pos;
3755
3756 xassert (!STRINGP (it->string));
3757
3758 /* If there isn't any `display' property in sight, and no
3759 overlays, we can just use the position of the newline in
3760 buffer text. */
3761 if (it->stop_charpos >= limit
3762 || ((pos = Fnext_single_property_change (make_number (start),
3763 Qdisplay,
3764 Qnil, make_number (limit)),
3765 NILP (pos))
3766 && next_overlay_change (start) == ZV))
3767 {
3768 IT_CHARPOS (*it) = limit;
3769 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
3770 *skipped_p = newline_found_p = 1;
3771 }
3772 else
3773 {
3774 while (get_next_display_element (it)
3775 && !newline_found_p)
3776 {
3777 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
3778 set_iterator_to_next (it, 0);
3779 }
3780 }
3781 }
3782
3783 it->selective = old_selective;
3784 return newline_found_p;
3785 }
3786
3787
3788 /* Set IT's current position to the previous visible line start. Skip
3789 invisible text that is so either due to text properties or due to
3790 selective display. Caution: this does not change IT->current_x and
3791 IT->hpos. */
3792
3793 static void
3794 back_to_previous_visible_line_start (it)
3795 struct it *it;
3796 {
3797 int visible_p = 0;
3798
3799 /* Go back one newline if not on BEGV already. */
3800 if (IT_CHARPOS (*it) > BEGV)
3801 back_to_previous_line_start (it);
3802
3803 /* Move over lines that are invisible because of selective display
3804 or text properties. */
3805 while (IT_CHARPOS (*it) > BEGV
3806 && !visible_p)
3807 {
3808 visible_p = 1;
3809
3810 /* If selective > 0, then lines indented more than that values
3811 are invisible. */
3812 if (it->selective > 0
3813 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3814 it->selective))
3815 visible_p = 0;
3816 else
3817 {
3818 Lisp_Object prop;
3819
3820 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
3821 Qinvisible, it->window);
3822 if (TEXT_PROP_MEANS_INVISIBLE (prop))
3823 visible_p = 0;
3824 }
3825
3826 /* Back one more newline if the current one is invisible. */
3827 if (!visible_p)
3828 back_to_previous_line_start (it);
3829 }
3830
3831 xassert (IT_CHARPOS (*it) >= BEGV);
3832 xassert (IT_CHARPOS (*it) == BEGV
3833 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3834 CHECK_IT (it);
3835 }
3836
3837
3838 /* Reseat iterator IT at the previous visible line start. Skip
3839 invisible text that is so either due to text properties or due to
3840 selective display. At the end, update IT's overlay information,
3841 face information etc. */
3842
3843 static void
3844 reseat_at_previous_visible_line_start (it)
3845 struct it *it;
3846 {
3847 back_to_previous_visible_line_start (it);
3848 reseat (it, it->current.pos, 1);
3849 CHECK_IT (it);
3850 }
3851
3852
3853 /* Reseat iterator IT on the next visible line start in the current
3854 buffer. ON_NEWLINE_P non-zero means position IT on the newline
3855 preceding the line start. Skip over invisible text that is so
3856 because of selective display. Compute faces, overlays etc at the
3857 new position. Note that this function does not skip over text that
3858 is invisible because of text properties. */
3859
3860 static void
3861 reseat_at_next_visible_line_start (it, on_newline_p)
3862 struct it *it;
3863 int on_newline_p;
3864 {
3865 int newline_found_p, skipped_p = 0;
3866
3867 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3868
3869 /* Skip over lines that are invisible because they are indented
3870 more than the value of IT->selective. */
3871 if (it->selective > 0)
3872 while (IT_CHARPOS (*it) < ZV
3873 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3874 it->selective))
3875 {
3876 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3877 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3878 }
3879
3880 /* Position on the newline if that's what's requested. */
3881 if (on_newline_p && newline_found_p)
3882 {
3883 if (STRINGP (it->string))
3884 {
3885 if (IT_STRING_CHARPOS (*it) > 0)
3886 {
3887 --IT_STRING_CHARPOS (*it);
3888 --IT_STRING_BYTEPOS (*it);
3889 }
3890 }
3891 else if (IT_CHARPOS (*it) > BEGV)
3892 {
3893 --IT_CHARPOS (*it);
3894 --IT_BYTEPOS (*it);
3895 reseat (it, it->current.pos, 0);
3896 }
3897 }
3898 else if (skipped_p)
3899 reseat (it, it->current.pos, 0);
3900
3901 CHECK_IT (it);
3902 }
3903
3904
3905 \f
3906 /***********************************************************************
3907 Changing an iterator's position
3908 ***********************************************************************/
3909
3910 /* Change IT's current position to POS in current_buffer. If FORCE_P
3911 is non-zero, always check for text properties at the new position.
3912 Otherwise, text properties are only looked up if POS >=
3913 IT->check_charpos of a property. */
3914
3915 static void
3916 reseat (it, pos, force_p)
3917 struct it *it;
3918 struct text_pos pos;
3919 int force_p;
3920 {
3921 int original_pos = IT_CHARPOS (*it);
3922
3923 reseat_1 (it, pos, 0);
3924
3925 /* Determine where to check text properties. Avoid doing it
3926 where possible because text property lookup is very expensive. */
3927 if (force_p
3928 || CHARPOS (pos) > it->stop_charpos
3929 || CHARPOS (pos) < original_pos)
3930 handle_stop (it);
3931
3932 CHECK_IT (it);
3933 }
3934
3935
3936 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
3937 IT->stop_pos to POS, also. */
3938
3939 static void
3940 reseat_1 (it, pos, set_stop_p)
3941 struct it *it;
3942 struct text_pos pos;
3943 int set_stop_p;
3944 {
3945 /* Don't call this function when scanning a C string. */
3946 xassert (it->s == NULL);
3947
3948 /* POS must be a reasonable value. */
3949 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
3950
3951 it->current.pos = it->position = pos;
3952 XSETBUFFER (it->object, current_buffer);
3953 it->end_charpos = ZV;
3954 it->dpvec = NULL;
3955 it->current.dpvec_index = -1;
3956 it->current.overlay_string_index = -1;
3957 IT_STRING_CHARPOS (*it) = -1;
3958 IT_STRING_BYTEPOS (*it) = -1;
3959 it->string = Qnil;
3960 it->method = next_element_from_buffer;
3961 it->sp = 0;
3962 it->face_before_selective_p = 0;
3963
3964 if (set_stop_p)
3965 it->stop_charpos = CHARPOS (pos);
3966 }
3967
3968
3969 /* Set up IT for displaying a string, starting at CHARPOS in window W.
3970 If S is non-null, it is a C string to iterate over. Otherwise,
3971 STRING gives a Lisp string to iterate over.
3972
3973 If PRECISION > 0, don't return more then PRECISION number of
3974 characters from the string.
3975
3976 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
3977 characters have been returned. FIELD_WIDTH < 0 means an infinite
3978 field width.
3979
3980 MULTIBYTE = 0 means disable processing of multibyte characters,
3981 MULTIBYTE > 0 means enable it,
3982 MULTIBYTE < 0 means use IT->multibyte_p.
3983
3984 IT must be initialized via a prior call to init_iterator before
3985 calling this function. */
3986
3987 static void
3988 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
3989 struct it *it;
3990 unsigned char *s;
3991 Lisp_Object string;
3992 int charpos;
3993 int precision, field_width, multibyte;
3994 {
3995 /* No region in strings. */
3996 it->region_beg_charpos = it->region_end_charpos = -1;
3997
3998 /* No text property checks performed by default, but see below. */
3999 it->stop_charpos = -1;
4000
4001 /* Set iterator position and end position. */
4002 bzero (&it->current, sizeof it->current);
4003 it->current.overlay_string_index = -1;
4004 it->current.dpvec_index = -1;
4005 xassert (charpos >= 0);
4006
4007 /* If STRING is specified, use its multibyteness, otherwise use the
4008 setting of MULTIBYTE, if specified. */
4009 if (multibyte >= 0)
4010 it->multibyte_p = multibyte > 0;
4011
4012 if (s == NULL)
4013 {
4014 xassert (STRINGP (string));
4015 it->string = string;
4016 it->s = NULL;
4017 it->end_charpos = it->string_nchars = XSTRING (string)->size;
4018 it->method = next_element_from_string;
4019 it->current.string_pos = string_pos (charpos, string);
4020 }
4021 else
4022 {
4023 it->s = s;
4024 it->string = Qnil;
4025
4026 /* Note that we use IT->current.pos, not it->current.string_pos,
4027 for displaying C strings. */
4028 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4029 if (it->multibyte_p)
4030 {
4031 it->current.pos = c_string_pos (charpos, s, 1);
4032 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4033 }
4034 else
4035 {
4036 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4037 it->end_charpos = it->string_nchars = strlen (s);
4038 }
4039
4040 it->method = next_element_from_c_string;
4041 }
4042
4043 /* PRECISION > 0 means don't return more than PRECISION characters
4044 from the string. */
4045 if (precision > 0 && it->end_charpos - charpos > precision)
4046 it->end_charpos = it->string_nchars = charpos + precision;
4047
4048 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4049 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4050 FIELD_WIDTH < 0 means infinite field width. This is useful for
4051 padding with `-' at the end of a mode line. */
4052 if (field_width < 0)
4053 field_width = INFINITY;
4054 if (field_width > it->end_charpos - charpos)
4055 it->end_charpos = charpos + field_width;
4056
4057 /* Use the standard display table for displaying strings. */
4058 if (DISP_TABLE_P (Vstandard_display_table))
4059 it->dp = XCHAR_TABLE (Vstandard_display_table);
4060
4061 it->stop_charpos = charpos;
4062 CHECK_IT (it);
4063 }
4064
4065
4066 \f
4067 /***********************************************************************
4068 Iteration
4069 ***********************************************************************/
4070
4071 /* Load IT's display element fields with information about the next
4072 display element from the current position of IT. Value is zero if
4073 end of buffer (or C string) is reached. */
4074
4075 int
4076 get_next_display_element (it)
4077 struct it *it;
4078 {
4079 /* Non-zero means that we found an display element. Zero means that
4080 we hit the end of what we iterate over. Performance note: the
4081 function pointer `method' used here turns out to be faster than
4082 using a sequence of if-statements. */
4083 int success_p = (*it->method) (it);
4084
4085 if (it->what == IT_CHARACTER)
4086 {
4087 /* Map via display table or translate control characters.
4088 IT->c, IT->len etc. have been set to the next character by
4089 the function call above. If we have a display table, and it
4090 contains an entry for IT->c, translate it. Don't do this if
4091 IT->c itself comes from a display table, otherwise we could
4092 end up in an infinite recursion. (An alternative could be to
4093 count the recursion depth of this function and signal an
4094 error when a certain maximum depth is reached.) Is it worth
4095 it? */
4096 if (success_p && it->dpvec == NULL)
4097 {
4098 Lisp_Object dv;
4099
4100 if (it->dp
4101 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
4102 VECTORP (dv)))
4103 {
4104 struct Lisp_Vector *v = XVECTOR (dv);
4105
4106 /* Return the first character from the display table
4107 entry, if not empty. If empty, don't display the
4108 current character. */
4109 if (v->size)
4110 {
4111 it->dpvec_char_len = it->len;
4112 it->dpvec = v->contents;
4113 it->dpend = v->contents + v->size;
4114 it->current.dpvec_index = 0;
4115 it->method = next_element_from_display_vector;
4116 success_p = get_next_display_element (it);
4117 }
4118 else
4119 {
4120 set_iterator_to_next (it, 0);
4121 success_p = get_next_display_element (it);
4122 }
4123 }
4124
4125 /* Translate control characters into `\003' or `^C' form.
4126 Control characters coming from a display table entry are
4127 currently not translated because we use IT->dpvec to hold
4128 the translation. This could easily be changed but I
4129 don't believe that it is worth doing.
4130
4131 Non-printable multibyte characters are also translated
4132 octal form. */
4133 else if ((it->c < ' '
4134 && (it->area != TEXT_AREA
4135 || (it->c != '\n' && it->c != '\t')))
4136 || (it->c >= 127
4137 && it->len == 1)
4138 || !CHAR_PRINTABLE_P (it->c))
4139 {
4140 /* IT->c is a control character which must be displayed
4141 either as '\003' or as `^C' where the '\\' and '^'
4142 can be defined in the display table. Fill
4143 IT->ctl_chars with glyphs for what we have to
4144 display. Then, set IT->dpvec to these glyphs. */
4145 GLYPH g;
4146
4147 if (it->c < 128 && it->ctl_arrow_p)
4148 {
4149 /* Set IT->ctl_chars[0] to the glyph for `^'. */
4150 if (it->dp
4151 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
4152 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
4153 g = XINT (DISP_CTRL_GLYPH (it->dp));
4154 else
4155 g = FAST_MAKE_GLYPH ('^', 0);
4156 XSETINT (it->ctl_chars[0], g);
4157
4158 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
4159 XSETINT (it->ctl_chars[1], g);
4160
4161 /* Set up IT->dpvec and return first character from it. */
4162 it->dpvec_char_len = it->len;
4163 it->dpvec = it->ctl_chars;
4164 it->dpend = it->dpvec + 2;
4165 it->current.dpvec_index = 0;
4166 it->method = next_element_from_display_vector;
4167 get_next_display_element (it);
4168 }
4169 else
4170 {
4171 unsigned char str[MAX_MULTIBYTE_LENGTH];
4172 int len;
4173 int i;
4174 GLYPH escape_glyph;
4175
4176 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
4177 if (it->dp
4178 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
4179 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
4180 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
4181 else
4182 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
4183
4184 if (SINGLE_BYTE_CHAR_P (it->c))
4185 str[0] = it->c, len = 1;
4186 else
4187 len = CHAR_STRING (it->c, str);
4188
4189 for (i = 0; i < len; i++)
4190 {
4191 XSETINT (it->ctl_chars[i * 4], escape_glyph);
4192 /* Insert three more glyphs into IT->ctl_chars for
4193 the octal display of the character. */
4194 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
4195 XSETINT (it->ctl_chars[i * 4 + 1], g);
4196 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
4197 XSETINT (it->ctl_chars[i * 4 + 2], g);
4198 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
4199 XSETINT (it->ctl_chars[i * 4 + 3], g);
4200 }
4201
4202 /* Set up IT->dpvec and return the first character
4203 from it. */
4204 it->dpvec_char_len = it->len;
4205 it->dpvec = it->ctl_chars;
4206 it->dpend = it->dpvec + len * 4;
4207 it->current.dpvec_index = 0;
4208 it->method = next_element_from_display_vector;
4209 get_next_display_element (it);
4210 }
4211 }
4212 }
4213
4214 /* Adjust face id for a multibyte character. There are no
4215 multibyte character in unibyte text. */
4216 if (it->multibyte_p
4217 && success_p
4218 && FRAME_WINDOW_P (it->f))
4219 {
4220 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4221 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
4222 }
4223 }
4224
4225 /* Is this character the last one of a run of characters with
4226 box? If yes, set IT->end_of_box_run_p to 1. */
4227 if (it->face_box_p
4228 && it->s == NULL)
4229 {
4230 int face_id;
4231 struct face *face;
4232
4233 it->end_of_box_run_p
4234 = ((face_id = face_after_it_pos (it),
4235 face_id != it->face_id)
4236 && (face = FACE_FROM_ID (it->f, face_id),
4237 face->box == FACE_NO_BOX));
4238 }
4239
4240 /* Value is 0 if end of buffer or string reached. */
4241 return success_p;
4242 }
4243
4244
4245 /* Move IT to the next display element.
4246
4247 RESEAT_P non-zero means if called on a newline in buffer text,
4248 skip to the next visible line start.
4249
4250 Functions get_next_display_element and set_iterator_to_next are
4251 separate because I find this arrangement easier to handle than a
4252 get_next_display_element function that also increments IT's
4253 position. The way it is we can first look at an iterator's current
4254 display element, decide whether it fits on a line, and if it does,
4255 increment the iterator position. The other way around we probably
4256 would either need a flag indicating whether the iterator has to be
4257 incremented the next time, or we would have to implement a
4258 decrement position function which would not be easy to write. */
4259
4260 void
4261 set_iterator_to_next (it, reseat_p)
4262 struct it *it;
4263 int reseat_p;
4264 {
4265 /* Reset flags indicating start and end of a sequence of characters
4266 with box. Reset them at the start of this function because
4267 moving the iterator to a new position might set them. */
4268 it->start_of_box_run_p = it->end_of_box_run_p = 0;
4269
4270 if (it->method == next_element_from_buffer)
4271 {
4272 /* The current display element of IT is a character from
4273 current_buffer. Advance in the buffer, and maybe skip over
4274 invisible lines that are so because of selective display. */
4275 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
4276 reseat_at_next_visible_line_start (it, 0);
4277 else
4278 {
4279 xassert (it->len != 0);
4280 IT_BYTEPOS (*it) += it->len;
4281 IT_CHARPOS (*it) += 1;
4282 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
4283 }
4284 }
4285 else if (it->method == next_element_from_composition)
4286 {
4287 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
4288 if (STRINGP (it->string))
4289 {
4290 IT_STRING_BYTEPOS (*it) += it->len;
4291 IT_STRING_CHARPOS (*it) += it->cmp_len;
4292 it->method = next_element_from_string;
4293 goto consider_string_end;
4294 }
4295 else
4296 {
4297 IT_BYTEPOS (*it) += it->len;
4298 IT_CHARPOS (*it) += it->cmp_len;
4299 it->method = next_element_from_buffer;
4300 }
4301 }
4302 else if (it->method == next_element_from_c_string)
4303 {
4304 /* Current display element of IT is from a C string. */
4305 IT_BYTEPOS (*it) += it->len;
4306 IT_CHARPOS (*it) += 1;
4307 }
4308 else if (it->method == next_element_from_display_vector)
4309 {
4310 /* Current display element of IT is from a display table entry.
4311 Advance in the display table definition. Reset it to null if
4312 end reached, and continue with characters from buffers/
4313 strings. */
4314 ++it->current.dpvec_index;
4315
4316 /* Restore face of the iterator to what they were before the
4317 display vector entry (these entries may contain faces). */
4318 it->face_id = it->saved_face_id;
4319
4320 if (it->dpvec + it->current.dpvec_index == it->dpend)
4321 {
4322 if (it->s)
4323 it->method = next_element_from_c_string;
4324 else if (STRINGP (it->string))
4325 it->method = next_element_from_string;
4326 else
4327 it->method = next_element_from_buffer;
4328
4329 it->dpvec = NULL;
4330 it->current.dpvec_index = -1;
4331
4332 /* Skip over characters which were displayed via IT->dpvec. */
4333 if (it->dpvec_char_len < 0)
4334 reseat_at_next_visible_line_start (it, 1);
4335 else if (it->dpvec_char_len > 0)
4336 {
4337 it->len = it->dpvec_char_len;
4338 set_iterator_to_next (it, reseat_p);
4339 }
4340 }
4341 }
4342 else if (it->method == next_element_from_string)
4343 {
4344 /* Current display element is a character from a Lisp string. */
4345 xassert (it->s == NULL && STRINGP (it->string));
4346 IT_STRING_BYTEPOS (*it) += it->len;
4347 IT_STRING_CHARPOS (*it) += 1;
4348
4349 consider_string_end:
4350
4351 if (it->current.overlay_string_index >= 0)
4352 {
4353 /* IT->string is an overlay string. Advance to the
4354 next, if there is one. */
4355 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
4356 next_overlay_string (it);
4357 }
4358 else
4359 {
4360 /* IT->string is not an overlay string. If we reached
4361 its end, and there is something on IT->stack, proceed
4362 with what is on the stack. This can be either another
4363 string, this time an overlay string, or a buffer. */
4364 if (IT_STRING_CHARPOS (*it) == XSTRING (it->string)->size
4365 && it->sp > 0)
4366 {
4367 pop_it (it);
4368 if (!STRINGP (it->string))
4369 it->method = next_element_from_buffer;
4370 }
4371 }
4372 }
4373 else if (it->method == next_element_from_image
4374 || it->method == next_element_from_stretch)
4375 {
4376 /* The position etc with which we have to proceed are on
4377 the stack. The position may be at the end of a string,
4378 if the `display' property takes up the whole string. */
4379 pop_it (it);
4380 it->image_id = 0;
4381 if (STRINGP (it->string))
4382 {
4383 it->method = next_element_from_string;
4384 goto consider_string_end;
4385 }
4386 else
4387 it->method = next_element_from_buffer;
4388 }
4389 else
4390 /* There are no other methods defined, so this should be a bug. */
4391 abort ();
4392
4393 xassert (it->method != next_element_from_string
4394 || (STRINGP (it->string)
4395 && IT_STRING_CHARPOS (*it) >= 0));
4396 }
4397
4398
4399 /* Load IT's display element fields with information about the next
4400 display element which comes from a display table entry or from the
4401 result of translating a control character to one of the forms `^C'
4402 or `\003'. IT->dpvec holds the glyphs to return as characters. */
4403
4404 static int
4405 next_element_from_display_vector (it)
4406 struct it *it;
4407 {
4408 /* Precondition. */
4409 xassert (it->dpvec && it->current.dpvec_index >= 0);
4410
4411 /* Remember the current face id in case glyphs specify faces.
4412 IT's face is restored in set_iterator_to_next. */
4413 it->saved_face_id = it->face_id;
4414
4415 if (INTEGERP (*it->dpvec)
4416 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
4417 {
4418 int lface_id;
4419 GLYPH g;
4420
4421 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
4422 it->c = FAST_GLYPH_CHAR (g);
4423 it->len = CHAR_BYTES (it->c);
4424
4425 /* The entry may contain a face id to use. Such a face id is
4426 the id of a Lisp face, not a realized face. A face id of
4427 zero means no face is specified. */
4428 lface_id = FAST_GLYPH_FACE (g);
4429 if (lface_id)
4430 {
4431 /* The function returns -1 if lface_id is invalid. */
4432 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
4433 if (face_id >= 0)
4434 it->face_id = face_id;
4435 }
4436 }
4437 else
4438 /* Display table entry is invalid. Return a space. */
4439 it->c = ' ', it->len = 1;
4440
4441 /* Don't change position and object of the iterator here. They are
4442 still the values of the character that had this display table
4443 entry or was translated, and that's what we want. */
4444 it->what = IT_CHARACTER;
4445 return 1;
4446 }
4447
4448
4449 /* Load IT with the next display element from Lisp string IT->string.
4450 IT->current.string_pos is the current position within the string.
4451 If IT->current.overlay_string_index >= 0, the Lisp string is an
4452 overlay string. */
4453
4454 static int
4455 next_element_from_string (it)
4456 struct it *it;
4457 {
4458 struct text_pos position;
4459
4460 xassert (STRINGP (it->string));
4461 xassert (IT_STRING_CHARPOS (*it) >= 0);
4462 position = it->current.string_pos;
4463
4464 /* Time to check for invisible text? */
4465 if (IT_STRING_CHARPOS (*it) < it->end_charpos
4466 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
4467 {
4468 handle_stop (it);
4469
4470 /* Since a handler may have changed IT->method, we must
4471 recurse here. */
4472 return get_next_display_element (it);
4473 }
4474
4475 if (it->current.overlay_string_index >= 0)
4476 {
4477 /* Get the next character from an overlay string. In overlay
4478 strings, There is no field width or padding with spaces to
4479 do. */
4480 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
4481 {
4482 it->what = IT_EOB;
4483 return 0;
4484 }
4485 else if (STRING_MULTIBYTE (it->string))
4486 {
4487 int remaining = (STRING_BYTES (XSTRING (it->string))
4488 - IT_STRING_BYTEPOS (*it));
4489 unsigned char *s = (XSTRING (it->string)->data
4490 + IT_STRING_BYTEPOS (*it));
4491 it->c = string_char_and_length (s, remaining, &it->len);
4492 }
4493 else
4494 {
4495 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
4496 it->len = 1;
4497 }
4498 }
4499 else
4500 {
4501 /* Get the next character from a Lisp string that is not an
4502 overlay string. Such strings come from the mode line, for
4503 example. We may have to pad with spaces, or truncate the
4504 string. See also next_element_from_c_string. */
4505 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
4506 {
4507 it->what = IT_EOB;
4508 return 0;
4509 }
4510 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
4511 {
4512 /* Pad with spaces. */
4513 it->c = ' ', it->len = 1;
4514 CHARPOS (position) = BYTEPOS (position) = -1;
4515 }
4516 else if (STRING_MULTIBYTE (it->string))
4517 {
4518 int maxlen = (STRING_BYTES (XSTRING (it->string))
4519 - IT_STRING_BYTEPOS (*it));
4520 unsigned char *s = (XSTRING (it->string)->data
4521 + IT_STRING_BYTEPOS (*it));
4522 it->c = string_char_and_length (s, maxlen, &it->len);
4523 }
4524 else
4525 {
4526 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
4527 it->len = 1;
4528 }
4529 }
4530
4531 /* Record what we have and where it came from. Note that we store a
4532 buffer position in IT->position although it could arguably be a
4533 string position. */
4534 it->what = IT_CHARACTER;
4535 it->object = it->string;
4536 it->position = position;
4537 return 1;
4538 }
4539
4540
4541 /* Load IT with next display element from C string IT->s.
4542 IT->string_nchars is the maximum number of characters to return
4543 from the string. IT->end_charpos may be greater than
4544 IT->string_nchars when this function is called, in which case we
4545 may have to return padding spaces. Value is zero if end of string
4546 reached, including padding spaces. */
4547
4548 static int
4549 next_element_from_c_string (it)
4550 struct it *it;
4551 {
4552 int success_p = 1;
4553
4554 xassert (it->s);
4555 it->what = IT_CHARACTER;
4556 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
4557 it->object = Qnil;
4558
4559 /* IT's position can be greater IT->string_nchars in case a field
4560 width or precision has been specified when the iterator was
4561 initialized. */
4562 if (IT_CHARPOS (*it) >= it->end_charpos)
4563 {
4564 /* End of the game. */
4565 it->what = IT_EOB;
4566 success_p = 0;
4567 }
4568 else if (IT_CHARPOS (*it) >= it->string_nchars)
4569 {
4570 /* Pad with spaces. */
4571 it->c = ' ', it->len = 1;
4572 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
4573 }
4574 else if (it->multibyte_p)
4575 {
4576 /* Implementation note: The calls to strlen apparently aren't a
4577 performance problem because there is no noticeable performance
4578 difference between Emacs running in unibyte or multibyte mode. */
4579 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
4580 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
4581 maxlen, &it->len);
4582 }
4583 else
4584 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
4585
4586 return success_p;
4587 }
4588
4589
4590 /* Set up IT to return characters from an ellipsis, if appropriate.
4591 The definition of the ellipsis glyphs may come from a display table
4592 entry. This function Fills IT with the first glyph from the
4593 ellipsis if an ellipsis is to be displayed. */
4594
4595 static int
4596 next_element_from_ellipsis (it)
4597 struct it *it;
4598 {
4599 if (it->selective_display_ellipsis_p)
4600 {
4601 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4602 {
4603 /* Use the display table definition for `...'. Invalid glyphs
4604 will be handled by the method returning elements from dpvec. */
4605 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4606 it->dpvec_char_len = it->len;
4607 it->dpvec = v->contents;
4608 it->dpend = v->contents + v->size;
4609 it->current.dpvec_index = 0;
4610 it->method = next_element_from_display_vector;
4611 }
4612 else
4613 {
4614 /* Use default `...' which is stored in default_invis_vector. */
4615 it->dpvec_char_len = it->len;
4616 it->dpvec = default_invis_vector;
4617 it->dpend = default_invis_vector + 3;
4618 it->current.dpvec_index = 0;
4619 it->method = next_element_from_display_vector;
4620 }
4621 }
4622 else
4623 {
4624 /* The face at the current position may be different from the
4625 face we find after the invisible text. Remember what it
4626 was in IT->saved_face_id, and signal that it's there by
4627 setting face_before_selective_p. */
4628 it->saved_face_id = it->face_id;
4629 it->method = next_element_from_buffer;
4630 reseat_at_next_visible_line_start (it, 1);
4631 it->face_before_selective_p = 1;
4632 }
4633
4634 return get_next_display_element (it);
4635 }
4636
4637
4638 /* Deliver an image display element. The iterator IT is already
4639 filled with image information (done in handle_display_prop). Value
4640 is always 1. */
4641
4642
4643 static int
4644 next_element_from_image (it)
4645 struct it *it;
4646 {
4647 it->what = IT_IMAGE;
4648 return 1;
4649 }
4650
4651
4652 /* Fill iterator IT with next display element from a stretch glyph
4653 property. IT->object is the value of the text property. Value is
4654 always 1. */
4655
4656 static int
4657 next_element_from_stretch (it)
4658 struct it *it;
4659 {
4660 it->what = IT_STRETCH;
4661 return 1;
4662 }
4663
4664
4665 /* Load IT with the next display element from current_buffer. Value
4666 is zero if end of buffer reached. IT->stop_charpos is the next
4667 position at which to stop and check for text properties or buffer
4668 end. */
4669
4670 static int
4671 next_element_from_buffer (it)
4672 struct it *it;
4673 {
4674 int success_p = 1;
4675
4676 /* Check this assumption, otherwise, we would never enter the
4677 if-statement, below. */
4678 xassert (IT_CHARPOS (*it) >= BEGV
4679 && IT_CHARPOS (*it) <= it->stop_charpos);
4680
4681 if (IT_CHARPOS (*it) >= it->stop_charpos)
4682 {
4683 if (IT_CHARPOS (*it) >= it->end_charpos)
4684 {
4685 int overlay_strings_follow_p;
4686
4687 /* End of the game, except when overlay strings follow that
4688 haven't been returned yet. */
4689 if (it->overlay_strings_at_end_processed_p)
4690 overlay_strings_follow_p = 0;
4691 else
4692 {
4693 it->overlay_strings_at_end_processed_p = 1;
4694 overlay_strings_follow_p = get_overlay_strings (it);
4695 }
4696
4697 if (overlay_strings_follow_p)
4698 success_p = get_next_display_element (it);
4699 else
4700 {
4701 it->what = IT_EOB;
4702 it->position = it->current.pos;
4703 success_p = 0;
4704 }
4705 }
4706 else
4707 {
4708 handle_stop (it);
4709 return get_next_display_element (it);
4710 }
4711 }
4712 else
4713 {
4714 /* No face changes, overlays etc. in sight, so just return a
4715 character from current_buffer. */
4716 unsigned char *p;
4717
4718 /* Maybe run the redisplay end trigger hook. Performance note:
4719 This doesn't seem to cost measurable time. */
4720 if (it->redisplay_end_trigger_charpos
4721 && it->glyph_row
4722 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
4723 run_redisplay_end_trigger_hook (it);
4724
4725 /* Get the next character, maybe multibyte. */
4726 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
4727 if (it->multibyte_p && !ASCII_BYTE_P (*p))
4728 {
4729 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
4730 - IT_BYTEPOS (*it));
4731 it->c = string_char_and_length (p, maxlen, &it->len);
4732 }
4733 else
4734 it->c = *p, it->len = 1;
4735
4736 /* Record what we have and where it came from. */
4737 it->what = IT_CHARACTER;;
4738 it->object = it->w->buffer;
4739 it->position = it->current.pos;
4740
4741 /* Normally we return the character found above, except when we
4742 really want to return an ellipsis for selective display. */
4743 if (it->selective)
4744 {
4745 if (it->c == '\n')
4746 {
4747 /* A value of selective > 0 means hide lines indented more
4748 than that number of columns. */
4749 if (it->selective > 0
4750 && IT_CHARPOS (*it) + 1 < ZV
4751 && indented_beyond_p (IT_CHARPOS (*it) + 1,
4752 IT_BYTEPOS (*it) + 1,
4753 it->selective))
4754 {
4755 success_p = next_element_from_ellipsis (it);
4756 it->dpvec_char_len = -1;
4757 }
4758 }
4759 else if (it->c == '\r' && it->selective == -1)
4760 {
4761 /* A value of selective == -1 means that everything from the
4762 CR to the end of the line is invisible, with maybe an
4763 ellipsis displayed for it. */
4764 success_p = next_element_from_ellipsis (it);
4765 it->dpvec_char_len = -1;
4766 }
4767 }
4768 }
4769
4770 /* Value is zero if end of buffer reached. */
4771 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
4772 return success_p;
4773 }
4774
4775
4776 /* Run the redisplay end trigger hook for IT. */
4777
4778 static void
4779 run_redisplay_end_trigger_hook (it)
4780 struct it *it;
4781 {
4782 Lisp_Object args[3];
4783
4784 /* IT->glyph_row should be non-null, i.e. we should be actually
4785 displaying something, or otherwise we should not run the hook. */
4786 xassert (it->glyph_row);
4787
4788 /* Set up hook arguments. */
4789 args[0] = Qredisplay_end_trigger_functions;
4790 args[1] = it->window;
4791 XSETINT (args[2], it->redisplay_end_trigger_charpos);
4792 it->redisplay_end_trigger_charpos = 0;
4793
4794 /* Since we are *trying* to run these functions, don't try to run
4795 them again, even if they get an error. */
4796 it->w->redisplay_end_trigger = Qnil;
4797 Frun_hook_with_args (3, args);
4798
4799 /* Notice if it changed the face of the character we are on. */
4800 handle_face_prop (it);
4801 }
4802
4803
4804 /* Deliver a composition display element. The iterator IT is already
4805 filled with composition information (done in
4806 handle_composition_prop). Value is always 1. */
4807
4808 static int
4809 next_element_from_composition (it)
4810 struct it *it;
4811 {
4812 it->what = IT_COMPOSITION;
4813 it->position = (STRINGP (it->string)
4814 ? it->current.string_pos
4815 : it->current.pos);
4816 return 1;
4817 }
4818
4819
4820 \f
4821 /***********************************************************************
4822 Moving an iterator without producing glyphs
4823 ***********************************************************************/
4824
4825 /* Move iterator IT to a specified buffer or X position within one
4826 line on the display without producing glyphs.
4827
4828 Begin to skip at IT's current position. Skip to TO_CHARPOS or TO_X
4829 whichever is reached first.
4830
4831 TO_CHARPOS <= 0 means no TO_CHARPOS is specified.
4832
4833 TO_X < 0 means that no TO_X is specified. TO_X is normally a value
4834 0 <= TO_X <= IT->last_visible_x. This means in particular, that
4835 TO_X includes the amount by which a window is horizontally
4836 scrolled.
4837
4838 Value is
4839
4840 MOVE_POS_MATCH_OR_ZV
4841 - when TO_POS or ZV was reached.
4842
4843 MOVE_X_REACHED
4844 -when TO_X was reached before TO_POS or ZV were reached.
4845
4846 MOVE_LINE_CONTINUED
4847 - when we reached the end of the display area and the line must
4848 be continued.
4849
4850 MOVE_LINE_TRUNCATED
4851 - when we reached the end of the display area and the line is
4852 truncated.
4853
4854 MOVE_NEWLINE_OR_CR
4855 - when we stopped at a line end, i.e. a newline or a CR and selective
4856 display is on. */
4857
4858 static enum move_it_result
4859 move_it_in_display_line_to (it, to_charpos, to_x, op)
4860 struct it *it;
4861 int to_charpos, to_x, op;
4862 {
4863 enum move_it_result result = MOVE_UNDEFINED;
4864 struct glyph_row *saved_glyph_row;
4865
4866 /* Don't produce glyphs in produce_glyphs. */
4867 saved_glyph_row = it->glyph_row;
4868 it->glyph_row = NULL;
4869
4870 while (1)
4871 {
4872 int x, i, ascent = 0, descent = 0;
4873
4874 /* Stop when ZV or TO_CHARPOS reached. */
4875 if (!get_next_display_element (it)
4876 || ((op & MOVE_TO_POS) != 0
4877 && BUFFERP (it->object)
4878 && IT_CHARPOS (*it) >= to_charpos))
4879 {
4880 result = MOVE_POS_MATCH_OR_ZV;
4881 break;
4882 }
4883
4884 /* The call to produce_glyphs will get the metrics of the
4885 display element IT is loaded with. We record in x the
4886 x-position before this display element in case it does not
4887 fit on the line. */
4888 x = it->current_x;
4889
4890 /* Remember the line height so far in case the next element doesn't
4891 fit on the line. */
4892 if (!it->truncate_lines_p)
4893 {
4894 ascent = it->max_ascent;
4895 descent = it->max_descent;
4896 }
4897
4898 PRODUCE_GLYPHS (it);
4899
4900 if (it->area != TEXT_AREA)
4901 {
4902 set_iterator_to_next (it, 1);
4903 continue;
4904 }
4905
4906 /* The number of glyphs we get back in IT->nglyphs will normally
4907 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
4908 character on a terminal frame, or (iii) a line end. For the
4909 second case, IT->nglyphs - 1 padding glyphs will be present
4910 (on X frames, there is only one glyph produced for a
4911 composite character.
4912
4913 The behavior implemented below means, for continuation lines,
4914 that as many spaces of a TAB as fit on the current line are
4915 displayed there. For terminal frames, as many glyphs of a
4916 multi-glyph character are displayed in the current line, too.
4917 This is what the old redisplay code did, and we keep it that
4918 way. Under X, the whole shape of a complex character must
4919 fit on the line or it will be completely displayed in the
4920 next line.
4921
4922 Note that both for tabs and padding glyphs, all glyphs have
4923 the same width. */
4924 if (it->nglyphs)
4925 {
4926 /* More than one glyph or glyph doesn't fit on line. All
4927 glyphs have the same width. */
4928 int single_glyph_width = it->pixel_width / it->nglyphs;
4929 int new_x;
4930
4931 for (i = 0; i < it->nglyphs; ++i, x = new_x)
4932 {
4933 new_x = x + single_glyph_width;
4934
4935 /* We want to leave anything reaching TO_X to the caller. */
4936 if ((op & MOVE_TO_X) && new_x > to_x)
4937 {
4938 it->current_x = x;
4939 result = MOVE_X_REACHED;
4940 break;
4941 }
4942 else if (/* Lines are continued. */
4943 !it->truncate_lines_p
4944 && (/* And glyph doesn't fit on the line. */
4945 new_x > it->last_visible_x
4946 /* Or it fits exactly and we're on a window
4947 system frame. */
4948 || (new_x == it->last_visible_x
4949 && FRAME_WINDOW_P (it->f))))
4950 {
4951 if (/* IT->hpos == 0 means the very first glyph
4952 doesn't fit on the line, e.g. a wide image. */
4953 it->hpos == 0
4954 || (new_x == it->last_visible_x
4955 && FRAME_WINDOW_P (it->f)))
4956 {
4957 ++it->hpos;
4958 it->current_x = new_x;
4959 if (i == it->nglyphs - 1)
4960 set_iterator_to_next (it, 1);
4961 }
4962 else
4963 {
4964 it->current_x = x;
4965 it->max_ascent = ascent;
4966 it->max_descent = descent;
4967 }
4968
4969 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
4970 IT_CHARPOS (*it)));
4971 result = MOVE_LINE_CONTINUED;
4972 break;
4973 }
4974 else if (new_x > it->first_visible_x)
4975 {
4976 /* Glyph is visible. Increment number of glyphs that
4977 would be displayed. */
4978 ++it->hpos;
4979 }
4980 else
4981 {
4982 /* Glyph is completely off the left margin of the display
4983 area. Nothing to do. */
4984 }
4985 }
4986
4987 if (result != MOVE_UNDEFINED)
4988 break;
4989 }
4990 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
4991 {
4992 /* Stop when TO_X specified and reached. This check is
4993 necessary here because of lines consisting of a line end,
4994 only. The line end will not produce any glyphs and we
4995 would never get MOVE_X_REACHED. */
4996 xassert (it->nglyphs == 0);
4997 result = MOVE_X_REACHED;
4998 break;
4999 }
5000
5001 /* Is this a line end? If yes, we're done. */
5002 if (ITERATOR_AT_END_OF_LINE_P (it))
5003 {
5004 result = MOVE_NEWLINE_OR_CR;
5005 break;
5006 }
5007
5008 /* The current display element has been consumed. Advance
5009 to the next. */
5010 set_iterator_to_next (it, 1);
5011
5012 /* Stop if lines are truncated and IT's current x-position is
5013 past the right edge of the window now. */
5014 if (it->truncate_lines_p
5015 && it->current_x >= it->last_visible_x)
5016 {
5017 result = MOVE_LINE_TRUNCATED;
5018 break;
5019 }
5020 }
5021
5022 /* Restore the iterator settings altered at the beginning of this
5023 function. */
5024 it->glyph_row = saved_glyph_row;
5025 return result;
5026 }
5027
5028
5029 /* Move IT forward to a specified buffer position TO_CHARPOS, TO_X,
5030 TO_Y, TO_VPOS. OP is a bit-mask that specifies where to stop. See
5031 the description of enum move_operation_enum.
5032
5033 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
5034 screen line, this function will set IT to the next position >
5035 TO_CHARPOS. */
5036
5037 void
5038 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
5039 struct it *it;
5040 int to_charpos, to_x, to_y, to_vpos;
5041 int op;
5042 {
5043 enum move_it_result skip, skip2 = MOVE_X_REACHED;
5044 int line_height;
5045 int reached = 0;
5046
5047 for (;;)
5048 {
5049 if (op & MOVE_TO_VPOS)
5050 {
5051 /* If no TO_CHARPOS and no TO_X specified, stop at the
5052 start of the line TO_VPOS. */
5053 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
5054 {
5055 if (it->vpos == to_vpos)
5056 {
5057 reached = 1;
5058 break;
5059 }
5060 else
5061 skip = move_it_in_display_line_to (it, -1, -1, 0);
5062 }
5063 else
5064 {
5065 /* TO_VPOS >= 0 means stop at TO_X in the line at
5066 TO_VPOS, or at TO_POS, whichever comes first. */
5067 if (it->vpos == to_vpos)
5068 {
5069 reached = 2;
5070 break;
5071 }
5072
5073 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
5074
5075 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
5076 {
5077 reached = 3;
5078 break;
5079 }
5080 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
5081 {
5082 /* We have reached TO_X but not in the line we want. */
5083 skip = move_it_in_display_line_to (it, to_charpos,
5084 -1, MOVE_TO_POS);
5085 if (skip == MOVE_POS_MATCH_OR_ZV)
5086 {
5087 reached = 4;
5088 break;
5089 }
5090 }
5091 }
5092 }
5093 else if (op & MOVE_TO_Y)
5094 {
5095 struct it it_backup;
5096
5097 /* TO_Y specified means stop at TO_X in the line containing
5098 TO_Y---or at TO_CHARPOS if this is reached first. The
5099 problem is that we can't really tell whether the line
5100 contains TO_Y before we have completely scanned it, and
5101 this may skip past TO_X. What we do is to first scan to
5102 TO_X.
5103
5104 If TO_X is not specified, use a TO_X of zero. The reason
5105 is to make the outcome of this function more predictable.
5106 If we didn't use TO_X == 0, we would stop at the end of
5107 the line which is probably not what a caller would expect
5108 to happen. */
5109 skip = move_it_in_display_line_to (it, to_charpos,
5110 ((op & MOVE_TO_X)
5111 ? to_x : 0),
5112 (MOVE_TO_X
5113 | (op & MOVE_TO_POS)));
5114
5115 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
5116 if (skip == MOVE_POS_MATCH_OR_ZV)
5117 {
5118 reached = 5;
5119 break;
5120 }
5121
5122 /* If TO_X was reached, we would like to know whether TO_Y
5123 is in the line. This can only be said if we know the
5124 total line height which requires us to scan the rest of
5125 the line. */
5126 if (skip == MOVE_X_REACHED)
5127 {
5128 it_backup = *it;
5129 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
5130 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
5131 op & MOVE_TO_POS);
5132 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
5133 }
5134
5135 /* Now, decide whether TO_Y is in this line. */
5136 line_height = it->max_ascent + it->max_descent;
5137 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
5138
5139 if (to_y >= it->current_y
5140 && to_y < it->current_y + line_height)
5141 {
5142 if (skip == MOVE_X_REACHED)
5143 /* If TO_Y is in this line and TO_X was reached above,
5144 we scanned too far. We have to restore IT's settings
5145 to the ones before skipping. */
5146 *it = it_backup;
5147 reached = 6;
5148 }
5149 else if (skip == MOVE_X_REACHED)
5150 {
5151 skip = skip2;
5152 if (skip == MOVE_POS_MATCH_OR_ZV)
5153 reached = 7;
5154 }
5155
5156 if (reached)
5157 break;
5158 }
5159 else
5160 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
5161
5162 switch (skip)
5163 {
5164 case MOVE_POS_MATCH_OR_ZV:
5165 reached = 8;
5166 goto out;
5167
5168 case MOVE_NEWLINE_OR_CR:
5169 set_iterator_to_next (it, 1);
5170 it->continuation_lines_width = 0;
5171 break;
5172
5173 case MOVE_LINE_TRUNCATED:
5174 it->continuation_lines_width = 0;
5175 reseat_at_next_visible_line_start (it, 0);
5176 if ((op & MOVE_TO_POS) != 0
5177 && IT_CHARPOS (*it) > to_charpos)
5178 {
5179 reached = 9;
5180 goto out;
5181 }
5182 break;
5183
5184 case MOVE_LINE_CONTINUED:
5185 it->continuation_lines_width += it->current_x;
5186 break;
5187
5188 default:
5189 abort ();
5190 }
5191
5192 /* Reset/increment for the next run. */
5193 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
5194 it->current_x = it->hpos = 0;
5195 it->current_y += it->max_ascent + it->max_descent;
5196 ++it->vpos;
5197 last_height = it->max_ascent + it->max_descent;
5198 last_max_ascent = it->max_ascent;
5199 it->max_ascent = it->max_descent = 0;
5200 }
5201
5202 out:
5203
5204 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
5205 }
5206
5207
5208 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
5209
5210 If DY > 0, move IT backward at least that many pixels. DY = 0
5211 means move IT backward to the preceding line start or BEGV. This
5212 function may move over more than DY pixels if IT->current_y - DY
5213 ends up in the middle of a line; in this case IT->current_y will be
5214 set to the top of the line moved to. */
5215
5216 void
5217 move_it_vertically_backward (it, dy)
5218 struct it *it;
5219 int dy;
5220 {
5221 int nlines, h, line_height;
5222 struct it it2;
5223 int start_pos = IT_CHARPOS (*it);
5224
5225 xassert (dy >= 0);
5226
5227 /* Estimate how many newlines we must move back. */
5228 nlines = max (1, dy / CANON_Y_UNIT (it->f));
5229
5230 /* Set the iterator's position that many lines back. */
5231 while (nlines-- && IT_CHARPOS (*it) > BEGV)
5232 back_to_previous_visible_line_start (it);
5233
5234 /* Reseat the iterator here. When moving backward, we don't want
5235 reseat to skip forward over invisible text, set up the iterator
5236 to deliver from overlay strings at the new position etc. So,
5237 use reseat_1 here. */
5238 reseat_1 (it, it->current.pos, 1);
5239
5240 /* We are now surely at a line start. */
5241 it->current_x = it->hpos = 0;
5242
5243 /* Move forward and see what y-distance we moved. First move to the
5244 start of the next line so that we get its height. We need this
5245 height to be able to tell whether we reached the specified
5246 y-distance. */
5247 it2 = *it;
5248 it2.max_ascent = it2.max_descent = 0;
5249 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
5250 MOVE_TO_POS | MOVE_TO_VPOS);
5251 xassert (IT_CHARPOS (*it) >= BEGV);
5252 line_height = it2.max_ascent + it2.max_descent;
5253
5254 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
5255 xassert (IT_CHARPOS (*it) >= BEGV);
5256 h = it2.current_y - it->current_y;
5257 nlines = it2.vpos - it->vpos;
5258
5259 /* Correct IT's y and vpos position. */
5260 it->vpos -= nlines;
5261 it->current_y -= h;
5262
5263 if (dy == 0)
5264 {
5265 /* DY == 0 means move to the start of the screen line. The
5266 value of nlines is > 0 if continuation lines were involved. */
5267 if (nlines > 0)
5268 move_it_by_lines (it, nlines, 1);
5269 xassert (IT_CHARPOS (*it) <= start_pos);
5270 }
5271 else if (nlines)
5272 {
5273 /* The y-position we try to reach. Note that h has been
5274 subtracted in front of the if-statement. */
5275 int target_y = it->current_y + h - dy;
5276
5277 /* If we did not reach target_y, try to move further backward if
5278 we can. If we moved too far backward, try to move forward. */
5279 if (target_y < it->current_y
5280 && IT_CHARPOS (*it) > BEGV)
5281 {
5282 move_it_vertically (it, target_y - it->current_y);
5283 xassert (IT_CHARPOS (*it) >= BEGV);
5284 }
5285 else if (target_y >= it->current_y + line_height
5286 && IT_CHARPOS (*it) < ZV)
5287 {
5288 move_it_vertically (it, target_y - (it->current_y + line_height));
5289 xassert (IT_CHARPOS (*it) >= BEGV);
5290 }
5291 }
5292 }
5293
5294
5295 /* Move IT by a specified amount of pixel lines DY. DY negative means
5296 move backwards. DY = 0 means move to start of screen line. At the
5297 end, IT will be on the start of a screen line. */
5298
5299 void
5300 move_it_vertically (it, dy)
5301 struct it *it;
5302 int dy;
5303 {
5304 if (dy <= 0)
5305 move_it_vertically_backward (it, -dy);
5306 else if (dy > 0)
5307 {
5308 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
5309 move_it_to (it, ZV, -1, it->current_y + dy, -1,
5310 MOVE_TO_POS | MOVE_TO_Y);
5311 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
5312
5313 /* If buffer ends in ZV without a newline, move to the start of
5314 the line to satisfy the post-condition. */
5315 if (IT_CHARPOS (*it) == ZV
5316 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
5317 move_it_by_lines (it, 0, 0);
5318 }
5319 }
5320
5321
5322 /* Move iterator IT past the end of the text line it is in. */
5323
5324 void
5325 move_it_past_eol (it)
5326 struct it *it;
5327 {
5328 enum move_it_result rc;
5329
5330 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
5331 if (rc == MOVE_NEWLINE_OR_CR)
5332 set_iterator_to_next (it, 0);
5333 }
5334
5335
5336 #if 0 /* Currently not used. */
5337
5338 /* Return non-zero if some text between buffer positions START_CHARPOS
5339 and END_CHARPOS is invisible. IT->window is the window for text
5340 property lookup. */
5341
5342 static int
5343 invisible_text_between_p (it, start_charpos, end_charpos)
5344 struct it *it;
5345 int start_charpos, end_charpos;
5346 {
5347 Lisp_Object prop, limit;
5348 int invisible_found_p;
5349
5350 xassert (it != NULL && start_charpos <= end_charpos);
5351
5352 /* Is text at START invisible? */
5353 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
5354 it->window);
5355 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5356 invisible_found_p = 1;
5357 else
5358 {
5359 limit = Fnext_single_char_property_change (make_number (start_charpos),
5360 Qinvisible, Qnil,
5361 make_number (end_charpos));
5362 invisible_found_p = XFASTINT (limit) < end_charpos;
5363 }
5364
5365 return invisible_found_p;
5366 }
5367
5368 #endif /* 0 */
5369
5370
5371 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
5372 negative means move up. DVPOS == 0 means move to the start of the
5373 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
5374 NEED_Y_P is zero, IT->current_y will be left unchanged.
5375
5376 Further optimization ideas: If we would know that IT->f doesn't use
5377 a face with proportional font, we could be faster for
5378 truncate-lines nil. */
5379
5380 void
5381 move_it_by_lines (it, dvpos, need_y_p)
5382 struct it *it;
5383 int dvpos, need_y_p;
5384 {
5385 struct position pos;
5386
5387 if (!FRAME_WINDOW_P (it->f))
5388 {
5389 struct text_pos textpos;
5390
5391 /* We can use vmotion on frames without proportional fonts. */
5392 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
5393 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
5394 reseat (it, textpos, 1);
5395 it->vpos += pos.vpos;
5396 it->current_y += pos.vpos;
5397 }
5398 else if (dvpos == 0)
5399 {
5400 /* DVPOS == 0 means move to the start of the screen line. */
5401 move_it_vertically_backward (it, 0);
5402 xassert (it->current_x == 0 && it->hpos == 0);
5403 }
5404 else if (dvpos > 0)
5405 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
5406 else
5407 {
5408 struct it it2;
5409 int start_charpos, i;
5410
5411 /* Go back -DVPOS visible lines and reseat the iterator there. */
5412 start_charpos = IT_CHARPOS (*it);
5413 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
5414 back_to_previous_visible_line_start (it);
5415 reseat (it, it->current.pos, 1);
5416 it->current_x = it->hpos = 0;
5417
5418 /* Above call may have moved too far if continuation lines
5419 are involved. Scan forward and see if it did. */
5420 it2 = *it;
5421 it2.vpos = it2.current_y = 0;
5422 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
5423 it->vpos -= it2.vpos;
5424 it->current_y -= it2.current_y;
5425 it->current_x = it->hpos = 0;
5426
5427 /* If we moved too far, move IT some lines forward. */
5428 if (it2.vpos > -dvpos)
5429 {
5430 int delta = it2.vpos + dvpos;
5431 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
5432 }
5433 }
5434 }
5435
5436
5437 \f
5438 /***********************************************************************
5439 Messages
5440 ***********************************************************************/
5441
5442
5443 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
5444 to *Messages*. */
5445
5446 void
5447 add_to_log (format, arg1, arg2)
5448 char *format;
5449 Lisp_Object arg1, arg2;
5450 {
5451 Lisp_Object args[3];
5452 Lisp_Object msg, fmt;
5453 char *buffer;
5454 int len;
5455 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5456
5457 fmt = msg = Qnil;
5458 GCPRO4 (fmt, msg, arg1, arg2);
5459
5460 args[0] = fmt = build_string (format);
5461 args[1] = arg1;
5462 args[2] = arg2;
5463 msg = Fformat (3, args);
5464
5465 len = STRING_BYTES (XSTRING (msg)) + 1;
5466 buffer = (char *) alloca (len);
5467 strcpy (buffer, XSTRING (msg)->data);
5468
5469 message_dolog (buffer, len - 1, 1, 0);
5470 UNGCPRO;
5471 }
5472
5473
5474 /* Output a newline in the *Messages* buffer if "needs" one. */
5475
5476 void
5477 message_log_maybe_newline ()
5478 {
5479 if (message_log_need_newline)
5480 message_dolog ("", 0, 1, 0);
5481 }
5482
5483
5484 /* Add a string M of length NBYTES to the message log, optionally
5485 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
5486 nonzero, means interpret the contents of M as multibyte. This
5487 function calls low-level routines in order to bypass text property
5488 hooks, etc. which might not be safe to run. */
5489
5490 void
5491 message_dolog (m, nbytes, nlflag, multibyte)
5492 char *m;
5493 int nbytes, nlflag, multibyte;
5494 {
5495 if (!NILP (Vmessage_log_max))
5496 {
5497 struct buffer *oldbuf;
5498 Lisp_Object oldpoint, oldbegv, oldzv;
5499 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5500 int point_at_end = 0;
5501 int zv_at_end = 0;
5502 Lisp_Object old_deactivate_mark, tem;
5503 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5504
5505 old_deactivate_mark = Vdeactivate_mark;
5506 oldbuf = current_buffer;
5507 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
5508 current_buffer->undo_list = Qt;
5509
5510 oldpoint = Fpoint_marker ();
5511 oldbegv = Fpoint_min_marker ();
5512 oldzv = Fpoint_max_marker ();
5513 GCPRO4 (oldpoint, oldbegv, oldzv, old_deactivate_mark);
5514
5515 if (PT == Z)
5516 point_at_end = 1;
5517 if (ZV == Z)
5518 zv_at_end = 1;
5519
5520 BEGV = BEG;
5521 BEGV_BYTE = BEG_BYTE;
5522 ZV = Z;
5523 ZV_BYTE = Z_BYTE;
5524 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5525
5526 /* Insert the string--maybe converting multibyte to single byte
5527 or vice versa, so that all the text fits the buffer. */
5528 if (multibyte
5529 && NILP (current_buffer->enable_multibyte_characters))
5530 {
5531 int i, c, char_bytes;
5532 unsigned char work[1];
5533
5534 /* Convert a multibyte string to single-byte
5535 for the *Message* buffer. */
5536 for (i = 0; i < nbytes; i += nbytes)
5537 {
5538 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
5539 work[0] = (SINGLE_BYTE_CHAR_P (c)
5540 ? c
5541 : multibyte_char_to_unibyte (c, Qnil));
5542 insert_1_both (work, 1, 1, 1, 0, 0);
5543 }
5544 }
5545 else if (! multibyte
5546 && ! NILP (current_buffer->enable_multibyte_characters))
5547 {
5548 int i, c, char_bytes;
5549 unsigned char *msg = (unsigned char *) m;
5550 unsigned char str[MAX_MULTIBYTE_LENGTH];
5551 /* Convert a single-byte string to multibyte
5552 for the *Message* buffer. */
5553 for (i = 0; i < nbytes; i++)
5554 {
5555 c = unibyte_char_to_multibyte (msg[i]);
5556 char_bytes = CHAR_STRING (c, str);
5557 insert_1_both (str, 1, char_bytes, 1, 0, 0);
5558 }
5559 }
5560 else if (nbytes)
5561 insert_1 (m, nbytes, 1, 0, 0);
5562
5563 if (nlflag)
5564 {
5565 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
5566 insert_1 ("\n", 1, 1, 0, 0);
5567
5568 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
5569 this_bol = PT;
5570 this_bol_byte = PT_BYTE;
5571
5572 if (this_bol > BEG)
5573 {
5574 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
5575 prev_bol = PT;
5576 prev_bol_byte = PT_BYTE;
5577
5578 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
5579 this_bol, this_bol_byte);
5580 if (dup)
5581 {
5582 del_range_both (prev_bol, prev_bol_byte,
5583 this_bol, this_bol_byte, 0);
5584 if (dup > 1)
5585 {
5586 char dupstr[40];
5587 int duplen;
5588
5589 /* If you change this format, don't forget to also
5590 change message_log_check_duplicate. */
5591 sprintf (dupstr, " [%d times]", dup);
5592 duplen = strlen (dupstr);
5593 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
5594 insert_1 (dupstr, duplen, 1, 0, 1);
5595 }
5596 }
5597 }
5598
5599 if (NATNUMP (Vmessage_log_max))
5600 {
5601 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
5602 -XFASTINT (Vmessage_log_max) - 1, 0);
5603 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
5604 }
5605 }
5606 BEGV = XMARKER (oldbegv)->charpos;
5607 BEGV_BYTE = marker_byte_position (oldbegv);
5608
5609 if (zv_at_end)
5610 {
5611 ZV = Z;
5612 ZV_BYTE = Z_BYTE;
5613 }
5614 else
5615 {
5616 ZV = XMARKER (oldzv)->charpos;
5617 ZV_BYTE = marker_byte_position (oldzv);
5618 }
5619
5620 if (point_at_end)
5621 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5622 else
5623 /* We can't do Fgoto_char (oldpoint) because it will run some
5624 Lisp code. */
5625 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
5626 XMARKER (oldpoint)->bytepos);
5627
5628 UNGCPRO;
5629 free_marker (oldpoint);
5630 free_marker (oldbegv);
5631 free_marker (oldzv);
5632
5633 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
5634 set_buffer_internal (oldbuf);
5635 if (NILP (tem))
5636 windows_or_buffers_changed = old_windows_or_buffers_changed;
5637 message_log_need_newline = !nlflag;
5638 Vdeactivate_mark = old_deactivate_mark;
5639 }
5640 }
5641
5642
5643 /* We are at the end of the buffer after just having inserted a newline.
5644 (Note: We depend on the fact we won't be crossing the gap.)
5645 Check to see if the most recent message looks a lot like the previous one.
5646 Return 0 if different, 1 if the new one should just replace it, or a
5647 value N > 1 if we should also append " [N times]". */
5648
5649 static int
5650 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
5651 int prev_bol, this_bol;
5652 int prev_bol_byte, this_bol_byte;
5653 {
5654 int i;
5655 int len = Z_BYTE - 1 - this_bol_byte;
5656 int seen_dots = 0;
5657 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
5658 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
5659
5660 for (i = 0; i < len; i++)
5661 {
5662 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
5663 seen_dots = 1;
5664 if (p1[i] != p2[i])
5665 return seen_dots;
5666 }
5667 p1 += len;
5668 if (*p1 == '\n')
5669 return 2;
5670 if (*p1++ == ' ' && *p1++ == '[')
5671 {
5672 int n = 0;
5673 while (*p1 >= '0' && *p1 <= '9')
5674 n = n * 10 + *p1++ - '0';
5675 if (strncmp (p1, " times]\n", 8) == 0)
5676 return n+1;
5677 }
5678 return 0;
5679 }
5680
5681
5682 /* Display an echo area message M with a specified length of NBYTES
5683 bytes. The string may include null characters. If M is 0, clear
5684 out any existing message, and let the mini-buffer text show
5685 through.
5686
5687 The buffer M must continue to exist until after the echo area gets
5688 cleared or some other message gets displayed there. This means do
5689 not pass text that is stored in a Lisp string; do not pass text in
5690 a buffer that was alloca'd. */
5691
5692 void
5693 message2 (m, nbytes, multibyte)
5694 char *m;
5695 int nbytes;
5696 int multibyte;
5697 {
5698 /* First flush out any partial line written with print. */
5699 message_log_maybe_newline ();
5700 if (m)
5701 message_dolog (m, nbytes, 1, multibyte);
5702 message2_nolog (m, nbytes, multibyte);
5703 }
5704
5705
5706 /* The non-logging counterpart of message2. */
5707
5708 void
5709 message2_nolog (m, nbytes, multibyte)
5710 char *m;
5711 int nbytes;
5712 {
5713 struct frame *sf = SELECTED_FRAME ();
5714 message_enable_multibyte = multibyte;
5715
5716 if (noninteractive)
5717 {
5718 if (noninteractive_need_newline)
5719 putc ('\n', stderr);
5720 noninteractive_need_newline = 0;
5721 if (m)
5722 fwrite (m, nbytes, 1, stderr);
5723 if (cursor_in_echo_area == 0)
5724 fprintf (stderr, "\n");
5725 fflush (stderr);
5726 }
5727 /* A null message buffer means that the frame hasn't really been
5728 initialized yet. Error messages get reported properly by
5729 cmd_error, so this must be just an informative message; toss it. */
5730 else if (INTERACTIVE
5731 && sf->glyphs_initialized_p
5732 && FRAME_MESSAGE_BUF (sf))
5733 {
5734 Lisp_Object mini_window;
5735 struct frame *f;
5736
5737 /* Get the frame containing the mini-buffer
5738 that the selected frame is using. */
5739 mini_window = FRAME_MINIBUF_WINDOW (sf);
5740 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5741
5742 FRAME_SAMPLE_VISIBILITY (f);
5743 if (FRAME_VISIBLE_P (sf)
5744 && ! FRAME_VISIBLE_P (f))
5745 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
5746
5747 if (m)
5748 {
5749 set_message (m, Qnil, nbytes, multibyte);
5750 if (minibuffer_auto_raise)
5751 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5752 }
5753 else
5754 clear_message (1, 1);
5755
5756 do_pending_window_change (0);
5757 echo_area_display (1);
5758 do_pending_window_change (0);
5759 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5760 (*frame_up_to_date_hook) (f);
5761 }
5762 }
5763
5764
5765 /* Display an echo area message M with a specified length of NBYTES
5766 bytes. The string may include null characters. If M is not a
5767 string, clear out any existing message, and let the mini-buffer
5768 text show through. */
5769
5770 void
5771 message3 (m, nbytes, multibyte)
5772 Lisp_Object m;
5773 int nbytes;
5774 int multibyte;
5775 {
5776 struct gcpro gcpro1;
5777
5778 GCPRO1 (m);
5779
5780 /* First flush out any partial line written with print. */
5781 message_log_maybe_newline ();
5782 if (STRINGP (m))
5783 message_dolog (XSTRING (m)->data, nbytes, 1, multibyte);
5784 message3_nolog (m, nbytes, multibyte);
5785
5786 UNGCPRO;
5787 }
5788
5789
5790 /* The non-logging version of message3. */
5791
5792 void
5793 message3_nolog (m, nbytes, multibyte)
5794 Lisp_Object m;
5795 int nbytes, multibyte;
5796 {
5797 struct frame *sf = SELECTED_FRAME ();
5798 message_enable_multibyte = multibyte;
5799
5800 if (noninteractive)
5801 {
5802 if (noninteractive_need_newline)
5803 putc ('\n', stderr);
5804 noninteractive_need_newline = 0;
5805 if (STRINGP (m))
5806 fwrite (XSTRING (m)->data, nbytes, 1, stderr);
5807 if (cursor_in_echo_area == 0)
5808 fprintf (stderr, "\n");
5809 fflush (stderr);
5810 }
5811 /* A null message buffer means that the frame hasn't really been
5812 initialized yet. Error messages get reported properly by
5813 cmd_error, so this must be just an informative message; toss it. */
5814 else if (INTERACTIVE
5815 && sf->glyphs_initialized_p
5816 && FRAME_MESSAGE_BUF (sf))
5817 {
5818 Lisp_Object mini_window;
5819 Lisp_Object frame;
5820 struct frame *f;
5821
5822 /* Get the frame containing the mini-buffer
5823 that the selected frame is using. */
5824 mini_window = FRAME_MINIBUF_WINDOW (sf);
5825 frame = XWINDOW (mini_window)->frame;
5826 f = XFRAME (frame);
5827
5828 FRAME_SAMPLE_VISIBILITY (f);
5829 if (FRAME_VISIBLE_P (sf)
5830 && !FRAME_VISIBLE_P (f))
5831 Fmake_frame_visible (frame);
5832
5833 if (STRINGP (m) && XSTRING (m)->size)
5834 {
5835 set_message (NULL, m, nbytes, multibyte);
5836 if (minibuffer_auto_raise)
5837 Fraise_frame (frame);
5838 }
5839 else
5840 clear_message (1, 1);
5841
5842 do_pending_window_change (0);
5843 echo_area_display (1);
5844 do_pending_window_change (0);
5845 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5846 (*frame_up_to_date_hook) (f);
5847 }
5848 }
5849
5850
5851 /* Display a null-terminated echo area message M. If M is 0, clear
5852 out any existing message, and let the mini-buffer text show through.
5853
5854 The buffer M must continue to exist until after the echo area gets
5855 cleared or some other message gets displayed there. Do not pass
5856 text that is stored in a Lisp string. Do not pass text in a buffer
5857 that was alloca'd. */
5858
5859 void
5860 message1 (m)
5861 char *m;
5862 {
5863 message2 (m, (m ? strlen (m) : 0), 0);
5864 }
5865
5866
5867 /* The non-logging counterpart of message1. */
5868
5869 void
5870 message1_nolog (m)
5871 char *m;
5872 {
5873 message2_nolog (m, (m ? strlen (m) : 0), 0);
5874 }
5875
5876 /* Display a message M which contains a single %s
5877 which gets replaced with STRING. */
5878
5879 void
5880 message_with_string (m, string, log)
5881 char *m;
5882 Lisp_Object string;
5883 int log;
5884 {
5885 if (noninteractive)
5886 {
5887 if (m)
5888 {
5889 if (noninteractive_need_newline)
5890 putc ('\n', stderr);
5891 noninteractive_need_newline = 0;
5892 fprintf (stderr, m, XSTRING (string)->data);
5893 if (cursor_in_echo_area == 0)
5894 fprintf (stderr, "\n");
5895 fflush (stderr);
5896 }
5897 }
5898 else if (INTERACTIVE)
5899 {
5900 /* The frame whose minibuffer we're going to display the message on.
5901 It may be larger than the selected frame, so we need
5902 to use its buffer, not the selected frame's buffer. */
5903 Lisp_Object mini_window;
5904 struct frame *f, *sf = SELECTED_FRAME ();
5905
5906 /* Get the frame containing the minibuffer
5907 that the selected frame is using. */
5908 mini_window = FRAME_MINIBUF_WINDOW (sf);
5909 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5910
5911 /* A null message buffer means that the frame hasn't really been
5912 initialized yet. Error messages get reported properly by
5913 cmd_error, so this must be just an informative message; toss it. */
5914 if (FRAME_MESSAGE_BUF (f))
5915 {
5916 int len;
5917 char *a[1];
5918 a[0] = (char *) XSTRING (string)->data;
5919
5920 len = doprnt (FRAME_MESSAGE_BUF (f),
5921 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5922
5923 if (log)
5924 message2 (FRAME_MESSAGE_BUF (f), len,
5925 STRING_MULTIBYTE (string));
5926 else
5927 message2_nolog (FRAME_MESSAGE_BUF (f), len,
5928 STRING_MULTIBYTE (string));
5929
5930 /* Print should start at the beginning of the message
5931 buffer next time. */
5932 message_buf_print = 0;
5933 }
5934 }
5935 }
5936
5937
5938 /* Dump an informative message to the minibuf. If M is 0, clear out
5939 any existing message, and let the mini-buffer text show through. */
5940
5941 /* VARARGS 1 */
5942 void
5943 message (m, a1, a2, a3)
5944 char *m;
5945 EMACS_INT a1, a2, a3;
5946 {
5947 if (noninteractive)
5948 {
5949 if (m)
5950 {
5951 if (noninteractive_need_newline)
5952 putc ('\n', stderr);
5953 noninteractive_need_newline = 0;
5954 fprintf (stderr, m, a1, a2, a3);
5955 if (cursor_in_echo_area == 0)
5956 fprintf (stderr, "\n");
5957 fflush (stderr);
5958 }
5959 }
5960 else if (INTERACTIVE)
5961 {
5962 /* The frame whose mini-buffer we're going to display the message
5963 on. It may be larger than the selected frame, so we need to
5964 use its buffer, not the selected frame's buffer. */
5965 Lisp_Object mini_window;
5966 struct frame *f, *sf = SELECTED_FRAME ();
5967
5968 /* Get the frame containing the mini-buffer
5969 that the selected frame is using. */
5970 mini_window = FRAME_MINIBUF_WINDOW (sf);
5971 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5972
5973 /* A null message buffer means that the frame hasn't really been
5974 initialized yet. Error messages get reported properly by
5975 cmd_error, so this must be just an informative message; toss
5976 it. */
5977 if (FRAME_MESSAGE_BUF (f))
5978 {
5979 if (m)
5980 {
5981 int len;
5982 #ifdef NO_ARG_ARRAY
5983 char *a[3];
5984 a[0] = (char *) a1;
5985 a[1] = (char *) a2;
5986 a[2] = (char *) a3;
5987
5988 len = doprnt (FRAME_MESSAGE_BUF (f),
5989 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5990 #else
5991 len = doprnt (FRAME_MESSAGE_BUF (f),
5992 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
5993 (char **) &a1);
5994 #endif /* NO_ARG_ARRAY */
5995
5996 message2 (FRAME_MESSAGE_BUF (f), len, 0);
5997 }
5998 else
5999 message1 (0);
6000
6001 /* Print should start at the beginning of the message
6002 buffer next time. */
6003 message_buf_print = 0;
6004 }
6005 }
6006 }
6007
6008
6009 /* The non-logging version of message. */
6010
6011 void
6012 message_nolog (m, a1, a2, a3)
6013 char *m;
6014 EMACS_INT a1, a2, a3;
6015 {
6016 Lisp_Object old_log_max;
6017 old_log_max = Vmessage_log_max;
6018 Vmessage_log_max = Qnil;
6019 message (m, a1, a2, a3);
6020 Vmessage_log_max = old_log_max;
6021 }
6022
6023
6024 /* Display the current message in the current mini-buffer. This is
6025 only called from error handlers in process.c, and is not time
6026 critical. */
6027
6028 void
6029 update_echo_area ()
6030 {
6031 if (!NILP (echo_area_buffer[0]))
6032 {
6033 Lisp_Object string;
6034 string = Fcurrent_message ();
6035 message3 (string, XSTRING (string)->size,
6036 !NILP (current_buffer->enable_multibyte_characters));
6037 }
6038 }
6039
6040
6041 /* Make sure echo area buffers in echo_buffers[] are life. If they
6042 aren't, make new ones. */
6043
6044 static void
6045 ensure_echo_area_buffers ()
6046 {
6047 int i;
6048
6049 for (i = 0; i < 2; ++i)
6050 if (!BUFFERP (echo_buffer[i])
6051 || NILP (XBUFFER (echo_buffer[i])->name))
6052 {
6053 char name[30];
6054 Lisp_Object old_buffer;
6055 int j;
6056
6057 old_buffer = echo_buffer[i];
6058 sprintf (name, " *Echo Area %d*", i);
6059 echo_buffer[i] = Fget_buffer_create (build_string (name));
6060 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
6061
6062 for (j = 0; j < 2; ++j)
6063 if (EQ (old_buffer, echo_area_buffer[j]))
6064 echo_area_buffer[j] = echo_buffer[i];
6065 }
6066 }
6067
6068
6069 /* Call FN with args A1..A4 with either the current or last displayed
6070 echo_area_buffer as current buffer.
6071
6072 WHICH zero means use the current message buffer
6073 echo_area_buffer[0]. If that is nil, choose a suitable buffer
6074 from echo_buffer[] and clear it.
6075
6076 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
6077 suitable buffer from echo_buffer[] and clear it.
6078
6079 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
6080 that the current message becomes the last displayed one, make
6081 choose a suitable buffer for echo_area_buffer[0], and clear it.
6082
6083 Value is what FN returns. */
6084
6085 static int
6086 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
6087 struct window *w;
6088 int which;
6089 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
6090 EMACS_INT a1;
6091 Lisp_Object a2;
6092 EMACS_INT a3, a4;
6093 {
6094 Lisp_Object buffer;
6095 int this_one, the_other, clear_buffer_p, rc;
6096 int count = BINDING_STACK_SIZE ();
6097
6098 /* If buffers aren't life, make new ones. */
6099 ensure_echo_area_buffers ();
6100
6101 clear_buffer_p = 0;
6102
6103 if (which == 0)
6104 this_one = 0, the_other = 1;
6105 else if (which > 0)
6106 this_one = 1, the_other = 0;
6107 else
6108 {
6109 this_one = 0, the_other = 1;
6110 clear_buffer_p = 1;
6111
6112 /* We need a fresh one in case the current echo buffer equals
6113 the one containing the last displayed echo area message. */
6114 if (!NILP (echo_area_buffer[this_one])
6115 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
6116 echo_area_buffer[this_one] = Qnil;
6117 }
6118
6119 /* Choose a suitable buffer from echo_buffer[] is we don't
6120 have one. */
6121 if (NILP (echo_area_buffer[this_one]))
6122 {
6123 echo_area_buffer[this_one]
6124 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
6125 ? echo_buffer[the_other]
6126 : echo_buffer[this_one]);
6127 clear_buffer_p = 1;
6128 }
6129
6130 buffer = echo_area_buffer[this_one];
6131
6132 record_unwind_protect (unwind_with_echo_area_buffer,
6133 with_echo_area_buffer_unwind_data (w));
6134
6135 /* Make the echo area buffer current. Note that for display
6136 purposes, it is not necessary that the displayed window's buffer
6137 == current_buffer, except for text property lookup. So, let's
6138 only set that buffer temporarily here without doing a full
6139 Fset_window_buffer. We must also change w->pointm, though,
6140 because otherwise an assertions in unshow_buffer fails, and Emacs
6141 aborts. */
6142 set_buffer_internal_1 (XBUFFER (buffer));
6143 if (w)
6144 {
6145 w->buffer = buffer;
6146 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
6147 }
6148
6149 current_buffer->undo_list = Qt;
6150 current_buffer->read_only = Qnil;
6151 specbind (Qinhibit_read_only, Qt);
6152
6153 if (clear_buffer_p && Z > BEG)
6154 del_range (BEG, Z);
6155
6156 xassert (BEGV >= BEG);
6157 xassert (ZV <= Z && ZV >= BEGV);
6158
6159 rc = fn (a1, a2, a3, a4);
6160
6161 xassert (BEGV >= BEG);
6162 xassert (ZV <= Z && ZV >= BEGV);
6163
6164 unbind_to (count, Qnil);
6165 return rc;
6166 }
6167
6168
6169 /* Save state that should be preserved around the call to the function
6170 FN called in with_echo_area_buffer. */
6171
6172 static Lisp_Object
6173 with_echo_area_buffer_unwind_data (w)
6174 struct window *w;
6175 {
6176 int i = 0;
6177 Lisp_Object vector;
6178
6179 /* Reduce consing by keeping one vector in
6180 Vwith_echo_area_save_vector. */
6181 vector = Vwith_echo_area_save_vector;
6182 Vwith_echo_area_save_vector = Qnil;
6183
6184 if (NILP (vector))
6185 vector = Fmake_vector (make_number (7), Qnil);
6186
6187 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
6188 AREF (vector, i) = Vdeactivate_mark, ++i;
6189 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
6190
6191 if (w)
6192 {
6193 XSETWINDOW (AREF (vector, i), w); ++i;
6194 AREF (vector, i) = w->buffer; ++i;
6195 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
6196 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
6197 }
6198 else
6199 {
6200 int end = i + 4;
6201 for (; i < end; ++i)
6202 AREF (vector, i) = Qnil;
6203 }
6204
6205 xassert (i == ASIZE (vector));
6206 return vector;
6207 }
6208
6209
6210 /* Restore global state from VECTOR which was created by
6211 with_echo_area_buffer_unwind_data. */
6212
6213 static Lisp_Object
6214 unwind_with_echo_area_buffer (vector)
6215 Lisp_Object vector;
6216 {
6217 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
6218 Vdeactivate_mark = AREF (vector, 1);
6219 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
6220
6221 if (WINDOWP (AREF (vector, 3)))
6222 {
6223 struct window *w;
6224 Lisp_Object buffer, charpos, bytepos;
6225
6226 w = XWINDOW (AREF (vector, 3));
6227 buffer = AREF (vector, 4);
6228 charpos = AREF (vector, 5);
6229 bytepos = AREF (vector, 6);
6230
6231 w->buffer = buffer;
6232 set_marker_both (w->pointm, buffer,
6233 XFASTINT (charpos), XFASTINT (bytepos));
6234 }
6235
6236 Vwith_echo_area_save_vector = vector;
6237 return Qnil;
6238 }
6239
6240
6241 /* Set up the echo area for use by print functions. MULTIBYTE_P
6242 non-zero means we will print multibyte. */
6243
6244 void
6245 setup_echo_area_for_printing (multibyte_p)
6246 int multibyte_p;
6247 {
6248 ensure_echo_area_buffers ();
6249
6250 if (!message_buf_print)
6251 {
6252 /* A message has been output since the last time we printed.
6253 Choose a fresh echo area buffer. */
6254 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6255 echo_area_buffer[0] = echo_buffer[1];
6256 else
6257 echo_area_buffer[0] = echo_buffer[0];
6258
6259 /* Switch to that buffer and clear it. */
6260 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6261 current_buffer->truncate_lines = Qnil;
6262
6263 if (Z > BEG)
6264 {
6265 int count = BINDING_STACK_SIZE ();
6266 specbind (Qinhibit_read_only, Qt);
6267 del_range (BEG, Z);
6268 unbind_to (count, Qnil);
6269 }
6270 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6271
6272 /* Set up the buffer for the multibyteness we need. */
6273 if (multibyte_p
6274 != !NILP (current_buffer->enable_multibyte_characters))
6275 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
6276
6277 /* Raise the frame containing the echo area. */
6278 if (minibuffer_auto_raise)
6279 {
6280 struct frame *sf = SELECTED_FRAME ();
6281 Lisp_Object mini_window;
6282 mini_window = FRAME_MINIBUF_WINDOW (sf);
6283 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6284 }
6285
6286 message_log_maybe_newline ();
6287 message_buf_print = 1;
6288 }
6289 else
6290 {
6291 if (NILP (echo_area_buffer[0]))
6292 {
6293 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6294 echo_area_buffer[0] = echo_buffer[1];
6295 else
6296 echo_area_buffer[0] = echo_buffer[0];
6297 }
6298
6299 if (current_buffer != XBUFFER (echo_area_buffer[0]))
6300 {
6301 /* Someone switched buffers between print requests. */
6302 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6303 current_buffer->truncate_lines = Qnil;
6304 }
6305 }
6306 }
6307
6308
6309 /* Display an echo area message in window W. Value is non-zero if W's
6310 height is changed. If display_last_displayed_message_p is
6311 non-zero, display the message that was last displayed, otherwise
6312 display the current message. */
6313
6314 static int
6315 display_echo_area (w)
6316 struct window *w;
6317 {
6318 int i, no_message_p, window_height_changed_p, count;
6319
6320 /* Temporarily disable garbage collections while displaying the echo
6321 area. This is done because a GC can print a message itself.
6322 That message would modify the echo area buffer's contents while a
6323 redisplay of the buffer is going on, and seriously confuse
6324 redisplay. */
6325 count = inhibit_garbage_collection ();
6326
6327 /* If there is no message, we must call display_echo_area_1
6328 nevertheless because it resizes the window. But we will have to
6329 reset the echo_area_buffer in question to nil at the end because
6330 with_echo_area_buffer will sets it to an empty buffer. */
6331 i = display_last_displayed_message_p ? 1 : 0;
6332 no_message_p = NILP (echo_area_buffer[i]);
6333
6334 window_height_changed_p
6335 = with_echo_area_buffer (w, display_last_displayed_message_p,
6336 display_echo_area_1,
6337 (EMACS_INT) w, Qnil, 0, 0);
6338
6339 if (no_message_p)
6340 echo_area_buffer[i] = Qnil;
6341
6342 unbind_to (count, Qnil);
6343 return window_height_changed_p;
6344 }
6345
6346
6347 /* Helper for display_echo_area. Display the current buffer which
6348 contains the current echo area message in window W, a mini-window,
6349 a pointer to which is passed in A1. A2..A4 are currently not used.
6350 Change the height of W so that all of the message is displayed.
6351 Value is non-zero if height of W was changed. */
6352
6353 static int
6354 display_echo_area_1 (a1, a2, a3, a4)
6355 EMACS_INT a1;
6356 Lisp_Object a2;
6357 EMACS_INT a3, a4;
6358 {
6359 struct window *w = (struct window *) a1;
6360 Lisp_Object window;
6361 struct text_pos start;
6362 int window_height_changed_p = 0;
6363
6364 /* Do this before displaying, so that we have a large enough glyph
6365 matrix for the display. */
6366 window_height_changed_p = resize_mini_window (w, 0);
6367
6368 /* Display. */
6369 clear_glyph_matrix (w->desired_matrix);
6370 XSETWINDOW (window, w);
6371 SET_TEXT_POS (start, BEG, BEG_BYTE);
6372 try_window (window, start);
6373
6374 return window_height_changed_p;
6375 }
6376
6377
6378 /* Resize the echo area window to exactly the size needed for the
6379 currently displayed message, if there is one. */
6380
6381 void
6382 resize_echo_area_axactly ()
6383 {
6384 if (BUFFERP (echo_area_buffer[0])
6385 && WINDOWP (echo_area_window))
6386 {
6387 struct window *w = XWINDOW (echo_area_window);
6388 int resized_p;
6389
6390 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
6391 (EMACS_INT) w, Qnil, 0, 0);
6392 if (resized_p)
6393 {
6394 ++windows_or_buffers_changed;
6395 ++update_mode_lines;
6396 redisplay_internal (0);
6397 }
6398 }
6399 }
6400
6401
6402 /* Callback function for with_echo_area_buffer, when used from
6403 resize_echo_area_axactly. A1 contains a pointer to the window to
6404 resize, A2 to A4 are not used. Value is what resize_mini_window
6405 returns. */
6406
6407 static int
6408 resize_mini_window_1 (a1, a2, a3, a4)
6409 EMACS_INT a1;
6410 Lisp_Object a2;
6411 EMACS_INT a3, a4;
6412 {
6413 return resize_mini_window ((struct window *) a1, 1);
6414 }
6415
6416
6417 /* Resize mini-window W to fit the size of its contents. EXACT:P
6418 means size the window exactly to the size needed. Otherwise, it's
6419 only enlarged until W's buffer is empty. Value is non-zero if
6420 the window height has been changed. */
6421
6422 int
6423 resize_mini_window (w, exact_p)
6424 struct window *w;
6425 int exact_p;
6426 {
6427 struct frame *f = XFRAME (w->frame);
6428 int window_height_changed_p = 0;
6429
6430 xassert (MINI_WINDOW_P (w));
6431
6432 /* Nil means don't try to resize. */
6433 if (NILP (Vresize_mini_windows)
6434 || (FRAME_X_P (f) && f->output_data.x == NULL))
6435 return 0;
6436
6437 if (!FRAME_MINIBUF_ONLY_P (f))
6438 {
6439 struct it it;
6440 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
6441 int total_height = XFASTINT (root->height) + XFASTINT (w->height);
6442 int height, max_height;
6443 int unit = CANON_Y_UNIT (f);
6444 struct text_pos start;
6445 struct buffer *old_current_buffer = NULL;
6446
6447 if (current_buffer != XBUFFER (w->buffer))
6448 {
6449 old_current_buffer = current_buffer;
6450 set_buffer_internal (XBUFFER (w->buffer));
6451 }
6452
6453 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
6454
6455 /* Compute the max. number of lines specified by the user. */
6456 if (FLOATP (Vmax_mini_window_height))
6457 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
6458 else if (INTEGERP (Vmax_mini_window_height))
6459 max_height = XINT (Vmax_mini_window_height);
6460 else
6461 max_height = total_height / 4;
6462
6463 /* Correct that max. height if it's bogus. */
6464 max_height = max (1, max_height);
6465 max_height = min (total_height, max_height);
6466
6467 /* Find out the height of the text in the window. */
6468 if (it.truncate_lines_p)
6469 height = 1;
6470 else
6471 {
6472 last_height = 0;
6473 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
6474 if (it.max_ascent == 0 && it.max_descent == 0)
6475 height = it.current_y + last_height;
6476 else
6477 height = it.current_y + it.max_ascent + it.max_descent;
6478 height -= it.extra_line_spacing;
6479 height = (height + unit - 1) / unit;
6480 }
6481
6482 /* Compute a suitable window start. */
6483 if (height > max_height)
6484 {
6485 height = max_height;
6486 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
6487 move_it_vertically_backward (&it, (height - 1) * unit);
6488 start = it.current.pos;
6489 }
6490 else
6491 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
6492 SET_MARKER_FROM_TEXT_POS (w->start, start);
6493
6494 if (EQ (Vresize_mini_windows, Qgrow_only))
6495 {
6496 /* Let it grow only, until we display an empty message, in which
6497 case the window shrinks again. */
6498 if (height > XFASTINT (w->height))
6499 {
6500 int old_height = XFASTINT (w->height);
6501 freeze_window_starts (f, 1);
6502 grow_mini_window (w, height - XFASTINT (w->height));
6503 window_height_changed_p = XFASTINT (w->height) != old_height;
6504 }
6505 else if (height < XFASTINT (w->height)
6506 && (exact_p || BEGV == ZV))
6507 {
6508 int old_height = XFASTINT (w->height);
6509 freeze_window_starts (f, 0);
6510 shrink_mini_window (w);
6511 window_height_changed_p = XFASTINT (w->height) != old_height;
6512 }
6513 }
6514 else
6515 {
6516 /* Always resize to exact size needed. */
6517 if (height > XFASTINT (w->height))
6518 {
6519 int old_height = XFASTINT (w->height);
6520 freeze_window_starts (f, 1);
6521 grow_mini_window (w, height - XFASTINT (w->height));
6522 window_height_changed_p = XFASTINT (w->height) != old_height;
6523 }
6524 else if (height < XFASTINT (w->height))
6525 {
6526 int old_height = XFASTINT (w->height);
6527 freeze_window_starts (f, 0);
6528 shrink_mini_window (w);
6529
6530 if (height)
6531 {
6532 freeze_window_starts (f, 1);
6533 grow_mini_window (w, height - XFASTINT (w->height));
6534 }
6535
6536 window_height_changed_p = XFASTINT (w->height) != old_height;
6537 }
6538 }
6539
6540 if (old_current_buffer)
6541 set_buffer_internal (old_current_buffer);
6542 }
6543
6544 return window_height_changed_p;
6545 }
6546
6547
6548 /* Value is the current message, a string, or nil if there is no
6549 current message. */
6550
6551 Lisp_Object
6552 current_message ()
6553 {
6554 Lisp_Object msg;
6555
6556 if (NILP (echo_area_buffer[0]))
6557 msg = Qnil;
6558 else
6559 {
6560 with_echo_area_buffer (0, 0, current_message_1,
6561 (EMACS_INT) &msg, Qnil, 0, 0);
6562 if (NILP (msg))
6563 echo_area_buffer[0] = Qnil;
6564 }
6565
6566 return msg;
6567 }
6568
6569
6570 static int
6571 current_message_1 (a1, a2, a3, a4)
6572 EMACS_INT a1;
6573 Lisp_Object a2;
6574 EMACS_INT a3, a4;
6575 {
6576 Lisp_Object *msg = (Lisp_Object *) a1;
6577
6578 if (Z > BEG)
6579 *msg = make_buffer_string (BEG, Z, 1);
6580 else
6581 *msg = Qnil;
6582 return 0;
6583 }
6584
6585
6586 /* Push the current message on Vmessage_stack for later restauration
6587 by restore_message. Value is non-zero if the current message isn't
6588 empty. This is a relatively infrequent operation, so it's not
6589 worth optimizing. */
6590
6591 int
6592 push_message ()
6593 {
6594 Lisp_Object msg;
6595 msg = current_message ();
6596 Vmessage_stack = Fcons (msg, Vmessage_stack);
6597 return STRINGP (msg);
6598 }
6599
6600
6601 /* Handler for record_unwind_protect calling pop_message. */
6602
6603 Lisp_Object
6604 push_message_unwind (dummy)
6605 Lisp_Object dummy;
6606 {
6607 pop_message ();
6608 return Qnil;
6609 }
6610
6611
6612 /* Restore message display from the top of Vmessage_stack. */
6613
6614 void
6615 restore_message ()
6616 {
6617 Lisp_Object msg;
6618
6619 xassert (CONSP (Vmessage_stack));
6620 msg = XCAR (Vmessage_stack);
6621 if (STRINGP (msg))
6622 message3_nolog (msg, STRING_BYTES (XSTRING (msg)), STRING_MULTIBYTE (msg));
6623 else
6624 message3_nolog (msg, 0, 0);
6625 }
6626
6627
6628 /* Pop the top-most entry off Vmessage_stack. */
6629
6630 void
6631 pop_message ()
6632 {
6633 xassert (CONSP (Vmessage_stack));
6634 Vmessage_stack = XCDR (Vmessage_stack);
6635 }
6636
6637
6638 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
6639 exits. If the stack is not empty, we have a missing pop_message
6640 somewhere. */
6641
6642 void
6643 check_message_stack ()
6644 {
6645 if (!NILP (Vmessage_stack))
6646 abort ();
6647 }
6648
6649
6650 /* Truncate to NCHARS what will be displayed in the echo area the next
6651 time we display it---but don't redisplay it now. */
6652
6653 void
6654 truncate_echo_area (nchars)
6655 int nchars;
6656 {
6657 if (nchars == 0)
6658 echo_area_buffer[0] = Qnil;
6659 /* A null message buffer means that the frame hasn't really been
6660 initialized yet. Error messages get reported properly by
6661 cmd_error, so this must be just an informative message; toss it. */
6662 else if (!noninteractive
6663 && INTERACTIVE
6664 && !NILP (echo_area_buffer[0]))
6665 {
6666 struct frame *sf = SELECTED_FRAME ();
6667 if (FRAME_MESSAGE_BUF (sf))
6668 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
6669 }
6670 }
6671
6672
6673 /* Helper function for truncate_echo_area. Truncate the current
6674 message to at most NCHARS characters. */
6675
6676 static int
6677 truncate_message_1 (nchars, a2, a3, a4)
6678 EMACS_INT nchars;
6679 Lisp_Object a2;
6680 EMACS_INT a3, a4;
6681 {
6682 if (BEG + nchars < Z)
6683 del_range (BEG + nchars, Z);
6684 if (Z == BEG)
6685 echo_area_buffer[0] = Qnil;
6686 return 0;
6687 }
6688
6689
6690 /* Set the current message to a substring of S or STRING.
6691
6692 If STRING is a Lisp string, set the message to the first NBYTES
6693 bytes from STRING. NBYTES zero means use the whole string. If
6694 STRING is multibyte, the message will be displayed multibyte.
6695
6696 If S is not null, set the message to the first LEN bytes of S. LEN
6697 zero means use the whole string. MULTIBYTE_P non-zero means S is
6698 multibyte. Display the message multibyte in that case. */
6699
6700 void
6701 set_message (s, string, nbytes, multibyte_p)
6702 char *s;
6703 Lisp_Object string;
6704 int nbytes;
6705 {
6706 message_enable_multibyte
6707 = ((s && multibyte_p)
6708 || (STRINGP (string) && STRING_MULTIBYTE (string)));
6709
6710 with_echo_area_buffer (0, -1, set_message_1,
6711 (EMACS_INT) s, string, nbytes, multibyte_p);
6712 message_buf_print = 0;
6713 help_echo_showing_p = 0;
6714 }
6715
6716
6717 /* Helper function for set_message. Arguments have the same meaning
6718 as there, with A1 corresponding to S and A2 corresponding to STRING
6719 This function is called with the echo area buffer being
6720 current. */
6721
6722 static int
6723 set_message_1 (a1, a2, nbytes, multibyte_p)
6724 EMACS_INT a1;
6725 Lisp_Object a2;
6726 EMACS_INT nbytes, multibyte_p;
6727 {
6728 char *s = (char *) a1;
6729 Lisp_Object string = a2;
6730
6731 xassert (BEG == Z);
6732
6733 /* Change multibyteness of the echo buffer appropriately. */
6734 if (message_enable_multibyte
6735 != !NILP (current_buffer->enable_multibyte_characters))
6736 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
6737
6738 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
6739
6740 /* Insert new message at BEG. */
6741 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6742
6743 if (STRINGP (string))
6744 {
6745 int nchars;
6746
6747 if (nbytes == 0)
6748 nbytes = XSTRING (string)->size_byte;
6749 nchars = string_byte_to_char (string, nbytes);
6750
6751 /* This function takes care of single/multibyte conversion. We
6752 just have to ensure that the echo area buffer has the right
6753 setting of enable_multibyte_characters. */
6754 insert_from_string (string, 0, 0, nchars, nbytes, 1);
6755 }
6756 else if (s)
6757 {
6758 if (nbytes == 0)
6759 nbytes = strlen (s);
6760
6761 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
6762 {
6763 /* Convert from multi-byte to single-byte. */
6764 int i, c, n;
6765 unsigned char work[1];
6766
6767 /* Convert a multibyte string to single-byte. */
6768 for (i = 0; i < nbytes; i += n)
6769 {
6770 c = string_char_and_length (s + i, nbytes - i, &n);
6771 work[0] = (SINGLE_BYTE_CHAR_P (c)
6772 ? c
6773 : multibyte_char_to_unibyte (c, Qnil));
6774 insert_1_both (work, 1, 1, 1, 0, 0);
6775 }
6776 }
6777 else if (!multibyte_p
6778 && !NILP (current_buffer->enable_multibyte_characters))
6779 {
6780 /* Convert from single-byte to multi-byte. */
6781 int i, c, n;
6782 unsigned char *msg = (unsigned char *) s;
6783 unsigned char str[MAX_MULTIBYTE_LENGTH];
6784
6785 /* Convert a single-byte string to multibyte. */
6786 for (i = 0; i < nbytes; i++)
6787 {
6788 c = unibyte_char_to_multibyte (msg[i]);
6789 n = CHAR_STRING (c, str);
6790 insert_1_both (str, 1, n, 1, 0, 0);
6791 }
6792 }
6793 else
6794 insert_1 (s, nbytes, 1, 0, 0);
6795 }
6796
6797 return 0;
6798 }
6799
6800
6801 /* Clear messages. CURRENT_P non-zero means clear the current
6802 message. LAST_DISPLAYED_P non-zero means clear the message
6803 last displayed. */
6804
6805 void
6806 clear_message (current_p, last_displayed_p)
6807 int current_p, last_displayed_p;
6808 {
6809 if (current_p)
6810 echo_area_buffer[0] = Qnil;
6811
6812 if (last_displayed_p)
6813 echo_area_buffer[1] = Qnil;
6814
6815 message_buf_print = 0;
6816 }
6817
6818 /* Clear garbaged frames.
6819
6820 This function is used where the old redisplay called
6821 redraw_garbaged_frames which in turn called redraw_frame which in
6822 turn called clear_frame. The call to clear_frame was a source of
6823 flickering. I believe a clear_frame is not necessary. It should
6824 suffice in the new redisplay to invalidate all current matrices,
6825 and ensure a complete redisplay of all windows. */
6826
6827 static void
6828 clear_garbaged_frames ()
6829 {
6830 if (frame_garbaged)
6831 {
6832 Lisp_Object tail, frame;
6833
6834 FOR_EACH_FRAME (tail, frame)
6835 {
6836 struct frame *f = XFRAME (frame);
6837
6838 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
6839 {
6840 clear_current_matrices (f);
6841 f->garbaged = 0;
6842 }
6843 }
6844
6845 frame_garbaged = 0;
6846 ++windows_or_buffers_changed;
6847 }
6848 }
6849
6850
6851 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
6852 is non-zero update selected_frame. Value is non-zero if the
6853 mini-windows height has been changed. */
6854
6855 static int
6856 echo_area_display (update_frame_p)
6857 int update_frame_p;
6858 {
6859 Lisp_Object mini_window;
6860 struct window *w;
6861 struct frame *f;
6862 int window_height_changed_p = 0;
6863 struct frame *sf = SELECTED_FRAME ();
6864
6865 mini_window = FRAME_MINIBUF_WINDOW (sf);
6866 w = XWINDOW (mini_window);
6867 f = XFRAME (WINDOW_FRAME (w));
6868
6869 /* Don't display if frame is invisible or not yet initialized. */
6870 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
6871 return 0;
6872
6873 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
6874 #ifndef macintosh
6875 #ifdef HAVE_WINDOW_SYSTEM
6876 /* When Emacs starts, selected_frame may be a visible terminal
6877 frame, even if we run under a window system. If we let this
6878 through, a message would be displayed on the terminal. */
6879 if (EQ (selected_frame, Vterminal_frame)
6880 && !NILP (Vwindow_system))
6881 return 0;
6882 #endif /* HAVE_WINDOW_SYSTEM */
6883 #endif
6884
6885 /* Redraw garbaged frames. */
6886 if (frame_garbaged)
6887 clear_garbaged_frames ();
6888
6889 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
6890 {
6891 echo_area_window = mini_window;
6892 window_height_changed_p = display_echo_area (w);
6893 w->must_be_updated_p = 1;
6894
6895 /* Update the display, unless called from redisplay_internal.
6896 Also don't update the screen during redisplay itself. The
6897 update will happen at the end of redisplay, and an update
6898 here could cause confusion. */
6899 if (update_frame_p && !redisplaying_p)
6900 {
6901 int n = 0;
6902
6903 /* If the display update has been interrupted by pending
6904 input, update mode lines in the frame. Due to the
6905 pending input, it might have been that redisplay hasn't
6906 been called, so that mode lines above the echo area are
6907 garbaged. This looks odd, so we prevent it here. */
6908 if (!display_completed)
6909 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
6910
6911 if (window_height_changed_p
6912 /* Don't do this if Emacs is shutting down. Redisplay
6913 needs to run hooks. */
6914 && !NILP (Vrun_hooks))
6915 {
6916 /* Must update other windows. Likewise as in other
6917 cases, don't let this update be interrupted by
6918 pending input. */
6919 int count = BINDING_STACK_SIZE ();
6920 specbind (Qredisplay_dont_pause, Qt);
6921 windows_or_buffers_changed = 1;
6922 redisplay_internal (0);
6923 unbind_to (count, Qnil);
6924 }
6925 else if (FRAME_WINDOW_P (f) && n == 0)
6926 {
6927 /* Window configuration is the same as before.
6928 Can do with a display update of the echo area,
6929 unless we displayed some mode lines. */
6930 update_single_window (w, 1);
6931 rif->flush_display (f);
6932 }
6933 else
6934 update_frame (f, 1, 1);
6935
6936 /* If cursor is in the echo area, make sure that the next
6937 redisplay displays the minibuffer, so that the cursor will
6938 be replaced with what the minibuffer wants. */
6939 if (cursor_in_echo_area)
6940 ++windows_or_buffers_changed;
6941 }
6942 }
6943 else if (!EQ (mini_window, selected_window))
6944 windows_or_buffers_changed++;
6945
6946 /* Last displayed message is now the current message. */
6947 echo_area_buffer[1] = echo_area_buffer[0];
6948
6949 /* Prevent redisplay optimization in redisplay_internal by resetting
6950 this_line_start_pos. This is done because the mini-buffer now
6951 displays the message instead of its buffer text. */
6952 if (EQ (mini_window, selected_window))
6953 CHARPOS (this_line_start_pos) = 0;
6954
6955 return window_height_changed_p;
6956 }
6957
6958
6959 \f
6960 /***********************************************************************
6961 Frame Titles
6962 ***********************************************************************/
6963
6964
6965 #ifdef HAVE_WINDOW_SYSTEM
6966
6967 /* A buffer for constructing frame titles in it; allocated from the
6968 heap in init_xdisp and resized as needed in store_frame_title_char. */
6969
6970 static char *frame_title_buf;
6971
6972 /* The buffer's end, and a current output position in it. */
6973
6974 static char *frame_title_buf_end;
6975 static char *frame_title_ptr;
6976
6977
6978 /* Store a single character C for the frame title in frame_title_buf.
6979 Re-allocate frame_title_buf if necessary. */
6980
6981 static void
6982 store_frame_title_char (c)
6983 char c;
6984 {
6985 /* If output position has reached the end of the allocated buffer,
6986 double the buffer's size. */
6987 if (frame_title_ptr == frame_title_buf_end)
6988 {
6989 int len = frame_title_ptr - frame_title_buf;
6990 int new_size = 2 * len * sizeof *frame_title_buf;
6991 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
6992 frame_title_buf_end = frame_title_buf + new_size;
6993 frame_title_ptr = frame_title_buf + len;
6994 }
6995
6996 *frame_title_ptr++ = c;
6997 }
6998
6999
7000 /* Store part of a frame title in frame_title_buf, beginning at
7001 frame_title_ptr. STR is the string to store. Do not copy
7002 characters that yield more columns than PRECISION; PRECISION <= 0
7003 means copy the whole string. Pad with spaces until FIELD_WIDTH
7004 number of characters have been copied; FIELD_WIDTH <= 0 means don't
7005 pad. Called from display_mode_element when it is used to build a
7006 frame title. */
7007
7008 static int
7009 store_frame_title (str, field_width, precision)
7010 unsigned char *str;
7011 int field_width, precision;
7012 {
7013 int n = 0;
7014 int dummy, nbytes, width;
7015
7016 /* Copy at most PRECISION chars from STR. */
7017 nbytes = strlen (str);
7018 n+= c_string_width (str, nbytes, precision, &dummy, &nbytes);
7019 while (nbytes--)
7020 store_frame_title_char (*str++);
7021
7022 /* Fill up with spaces until FIELD_WIDTH reached. */
7023 while (field_width > 0
7024 && n < field_width)
7025 {
7026 store_frame_title_char (' ');
7027 ++n;
7028 }
7029
7030 return n;
7031 }
7032
7033
7034 /* Set the title of FRAME, if it has changed. The title format is
7035 Vicon_title_format if FRAME is iconified, otherwise it is
7036 frame_title_format. */
7037
7038 static void
7039 x_consider_frame_title (frame)
7040 Lisp_Object frame;
7041 {
7042 struct frame *f = XFRAME (frame);
7043
7044 if (FRAME_WINDOW_P (f)
7045 || FRAME_MINIBUF_ONLY_P (f)
7046 || f->explicit_name)
7047 {
7048 /* Do we have more than one visible frame on this X display? */
7049 Lisp_Object tail;
7050 Lisp_Object fmt;
7051 struct buffer *obuf;
7052 int len;
7053 struct it it;
7054
7055 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
7056 {
7057 struct frame *tf = XFRAME (XCAR (tail));
7058
7059 if (tf != f
7060 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
7061 && !FRAME_MINIBUF_ONLY_P (tf)
7062 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
7063 break;
7064 }
7065
7066 /* Set global variable indicating that multiple frames exist. */
7067 multiple_frames = CONSP (tail);
7068
7069 /* Switch to the buffer of selected window of the frame. Set up
7070 frame_title_ptr so that display_mode_element will output into it;
7071 then display the title. */
7072 obuf = current_buffer;
7073 Fset_buffer (XWINDOW (f->selected_window)->buffer);
7074 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
7075 frame_title_ptr = frame_title_buf;
7076 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
7077 NULL, DEFAULT_FACE_ID);
7078 display_mode_element (&it, 0, -1, -1, fmt);
7079 len = frame_title_ptr - frame_title_buf;
7080 frame_title_ptr = NULL;
7081 set_buffer_internal (obuf);
7082
7083 /* Set the title only if it's changed. This avoids consing in
7084 the common case where it hasn't. (If it turns out that we've
7085 already wasted too much time by walking through the list with
7086 display_mode_element, then we might need to optimize at a
7087 higher level than this.) */
7088 if (! STRINGP (f->name)
7089 || STRING_BYTES (XSTRING (f->name)) != len
7090 || bcmp (frame_title_buf, XSTRING (f->name)->data, len) != 0)
7091 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
7092 }
7093 }
7094
7095 #else /* not HAVE_WINDOW_SYSTEM */
7096
7097 #define frame_title_ptr ((char *)0)
7098 #define store_frame_title(str, mincol, maxcol) 0
7099
7100 #endif /* not HAVE_WINDOW_SYSTEM */
7101
7102
7103
7104 \f
7105 /***********************************************************************
7106 Menu Bars
7107 ***********************************************************************/
7108
7109
7110 /* Prepare for redisplay by updating menu-bar item lists when
7111 appropriate. This can call eval. */
7112
7113 void
7114 prepare_menu_bars ()
7115 {
7116 int all_windows;
7117 struct gcpro gcpro1, gcpro2;
7118 struct frame *f;
7119 Lisp_Object tooltip_frame;
7120
7121 #ifdef HAVE_X_WINDOWS
7122 tooltip_frame = tip_frame;
7123 #else
7124 tooltip_frame = Qnil;
7125 #endif
7126
7127 /* Update all frame titles based on their buffer names, etc. We do
7128 this before the menu bars so that the buffer-menu will show the
7129 up-to-date frame titles. */
7130 #ifdef HAVE_WINDOW_SYSTEM
7131 if (windows_or_buffers_changed || update_mode_lines)
7132 {
7133 Lisp_Object tail, frame;
7134
7135 FOR_EACH_FRAME (tail, frame)
7136 {
7137 f = XFRAME (frame);
7138 if (!EQ (frame, tooltip_frame)
7139 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
7140 x_consider_frame_title (frame);
7141 }
7142 }
7143 #endif /* HAVE_WINDOW_SYSTEM */
7144
7145 /* Update the menu bar item lists, if appropriate. This has to be
7146 done before any actual redisplay or generation of display lines. */
7147 all_windows = (update_mode_lines
7148 || buffer_shared > 1
7149 || windows_or_buffers_changed);
7150 if (all_windows)
7151 {
7152 Lisp_Object tail, frame;
7153 int count = BINDING_STACK_SIZE ();
7154
7155 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7156
7157 FOR_EACH_FRAME (tail, frame)
7158 {
7159 f = XFRAME (frame);
7160
7161 /* Ignore tooltip frame. */
7162 if (EQ (frame, tooltip_frame))
7163 continue;
7164
7165 /* If a window on this frame changed size, report that to
7166 the user and clear the size-change flag. */
7167 if (FRAME_WINDOW_SIZES_CHANGED (f))
7168 {
7169 Lisp_Object functions;
7170
7171 /* Clear flag first in case we get an error below. */
7172 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
7173 functions = Vwindow_size_change_functions;
7174 GCPRO2 (tail, functions);
7175
7176 while (CONSP (functions))
7177 {
7178 call1 (XCAR (functions), frame);
7179 functions = XCDR (functions);
7180 }
7181 UNGCPRO;
7182 }
7183
7184 GCPRO1 (tail);
7185 update_menu_bar (f, 0);
7186 #ifdef HAVE_WINDOW_SYSTEM
7187 update_tool_bar (f, 0);
7188 #endif
7189 UNGCPRO;
7190 }
7191
7192 unbind_to (count, Qnil);
7193 }
7194 else
7195 {
7196 struct frame *sf = SELECTED_FRAME ();
7197 update_menu_bar (sf, 1);
7198 #ifdef HAVE_WINDOW_SYSTEM
7199 update_tool_bar (sf, 1);
7200 #endif
7201 }
7202
7203 /* Motif needs this. See comment in xmenu.c. Turn it off when
7204 pending_menu_activation is not defined. */
7205 #ifdef USE_X_TOOLKIT
7206 pending_menu_activation = 0;
7207 #endif
7208 }
7209
7210
7211 /* Update the menu bar item list for frame F. This has to be done
7212 before we start to fill in any display lines, because it can call
7213 eval.
7214
7215 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
7216
7217 static void
7218 update_menu_bar (f, save_match_data)
7219 struct frame *f;
7220 int save_match_data;
7221 {
7222 Lisp_Object window;
7223 register struct window *w;
7224
7225 /* If called recursively during a menu update, do nothing. This can
7226 happen when, for instance, an activate-menubar-hook causes a
7227 redisplay. */
7228 if (inhibit_menubar_update)
7229 return;
7230
7231 window = FRAME_SELECTED_WINDOW (f);
7232 w = XWINDOW (window);
7233
7234 if (update_mode_lines)
7235 w->update_mode_line = Qt;
7236
7237 if (FRAME_WINDOW_P (f)
7238 ?
7239 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
7240 FRAME_EXTERNAL_MENU_BAR (f)
7241 #else
7242 FRAME_MENU_BAR_LINES (f) > 0
7243 #endif
7244 : FRAME_MENU_BAR_LINES (f) > 0)
7245 {
7246 /* If the user has switched buffers or windows, we need to
7247 recompute to reflect the new bindings. But we'll
7248 recompute when update_mode_lines is set too; that means
7249 that people can use force-mode-line-update to request
7250 that the menu bar be recomputed. The adverse effect on
7251 the rest of the redisplay algorithm is about the same as
7252 windows_or_buffers_changed anyway. */
7253 if (windows_or_buffers_changed
7254 || !NILP (w->update_mode_line)
7255 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7256 < BUF_MODIFF (XBUFFER (w->buffer)))
7257 != !NILP (w->last_had_star))
7258 || ((!NILP (Vtransient_mark_mode)
7259 && !NILP (XBUFFER (w->buffer)->mark_active))
7260 != !NILP (w->region_showing)))
7261 {
7262 struct buffer *prev = current_buffer;
7263 int count = BINDING_STACK_SIZE ();
7264
7265 specbind (Qinhibit_menubar_update, Qt);
7266
7267 set_buffer_internal_1 (XBUFFER (w->buffer));
7268 if (save_match_data)
7269 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7270 if (NILP (Voverriding_local_map_menu_flag))
7271 {
7272 specbind (Qoverriding_terminal_local_map, Qnil);
7273 specbind (Qoverriding_local_map, Qnil);
7274 }
7275
7276 /* Run the Lucid hook. */
7277 safe_run_hooks (Qactivate_menubar_hook);
7278
7279 /* If it has changed current-menubar from previous value,
7280 really recompute the menu-bar from the value. */
7281 if (! NILP (Vlucid_menu_bar_dirty_flag))
7282 call0 (Qrecompute_lucid_menubar);
7283
7284 safe_run_hooks (Qmenu_bar_update_hook);
7285 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
7286
7287 /* Redisplay the menu bar in case we changed it. */
7288 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
7289 if (FRAME_WINDOW_P (f)
7290 #if defined (macintosh)
7291 /* All frames on Mac OS share the same menubar. So only the
7292 selected frame should be allowed to set it. */
7293 && f == SELECTED_FRAME ()
7294 #endif
7295 )
7296 set_frame_menubar (f, 0, 0);
7297 else
7298 /* On a terminal screen, the menu bar is an ordinary screen
7299 line, and this makes it get updated. */
7300 w->update_mode_line = Qt;
7301 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7302 /* In the non-toolkit version, the menu bar is an ordinary screen
7303 line, and this makes it get updated. */
7304 w->update_mode_line = Qt;
7305 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7306
7307 unbind_to (count, Qnil);
7308 set_buffer_internal_1 (prev);
7309 }
7310 }
7311 }
7312
7313
7314 \f
7315 /***********************************************************************
7316 Tool-bars
7317 ***********************************************************************/
7318
7319 #ifdef HAVE_WINDOW_SYSTEM
7320
7321 /* Update the tool-bar item list for frame F. This has to be done
7322 before we start to fill in any display lines. Called from
7323 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
7324 and restore it here. */
7325
7326 static void
7327 update_tool_bar (f, save_match_data)
7328 struct frame *f;
7329 int save_match_data;
7330 {
7331 if (WINDOWP (f->tool_bar_window)
7332 && XFASTINT (XWINDOW (f->tool_bar_window)->height) > 0)
7333 {
7334 Lisp_Object window;
7335 struct window *w;
7336
7337 window = FRAME_SELECTED_WINDOW (f);
7338 w = XWINDOW (window);
7339
7340 /* If the user has switched buffers or windows, we need to
7341 recompute to reflect the new bindings. But we'll
7342 recompute when update_mode_lines is set too; that means
7343 that people can use force-mode-line-update to request
7344 that the menu bar be recomputed. The adverse effect on
7345 the rest of the redisplay algorithm is about the same as
7346 windows_or_buffers_changed anyway. */
7347 if (windows_or_buffers_changed
7348 || !NILP (w->update_mode_line)
7349 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7350 < BUF_MODIFF (XBUFFER (w->buffer)))
7351 != !NILP (w->last_had_star))
7352 || ((!NILP (Vtransient_mark_mode)
7353 && !NILP (XBUFFER (w->buffer)->mark_active))
7354 != !NILP (w->region_showing)))
7355 {
7356 struct buffer *prev = current_buffer;
7357 int count = BINDING_STACK_SIZE ();
7358
7359 /* Set current_buffer to the buffer of the selected
7360 window of the frame, so that we get the right local
7361 keymaps. */
7362 set_buffer_internal_1 (XBUFFER (w->buffer));
7363
7364 /* Save match data, if we must. */
7365 if (save_match_data)
7366 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7367
7368 /* Make sure that we don't accidentally use bogus keymaps. */
7369 if (NILP (Voverriding_local_map_menu_flag))
7370 {
7371 specbind (Qoverriding_terminal_local_map, Qnil);
7372 specbind (Qoverriding_local_map, Qnil);
7373 }
7374
7375 /* Build desired tool-bar items from keymaps. */
7376 f->tool_bar_items
7377 = tool_bar_items (f->tool_bar_items, &f->n_tool_bar_items);
7378
7379 /* Redisplay the tool-bar in case we changed it. */
7380 w->update_mode_line = Qt;
7381
7382 unbind_to (count, Qnil);
7383 set_buffer_internal_1 (prev);
7384 }
7385 }
7386 }
7387
7388
7389 /* Set F->desired_tool_bar_string to a Lisp string representing frame
7390 F's desired tool-bar contents. F->tool_bar_items must have
7391 been set up previously by calling prepare_menu_bars. */
7392
7393 static void
7394 build_desired_tool_bar_string (f)
7395 struct frame *f;
7396 {
7397 int i, size, size_needed;
7398 struct gcpro gcpro1, gcpro2, gcpro3;
7399 Lisp_Object image, plist, props;
7400
7401 image = plist = props = Qnil;
7402 GCPRO3 (image, plist, props);
7403
7404 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
7405 Otherwise, make a new string. */
7406
7407 /* The size of the string we might be able to reuse. */
7408 size = (STRINGP (f->desired_tool_bar_string)
7409 ? XSTRING (f->desired_tool_bar_string)->size
7410 : 0);
7411
7412 /* We need one space in the string for each image. */
7413 size_needed = f->n_tool_bar_items;
7414
7415 /* Reuse f->desired_tool_bar_string, if possible. */
7416 if (size < size_needed || NILP (f->desired_tool_bar_string))
7417 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
7418 make_number (' '));
7419 else
7420 {
7421 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
7422 Fremove_text_properties (make_number (0), make_number (size),
7423 props, f->desired_tool_bar_string);
7424 }
7425
7426 /* Put a `display' property on the string for the images to display,
7427 put a `menu_item' property on tool-bar items with a value that
7428 is the index of the item in F's tool-bar item vector. */
7429 for (i = 0; i < f->n_tool_bar_items; ++i)
7430 {
7431 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
7432
7433 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
7434 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
7435 int hmargin, vmargin, relief, idx, end;
7436 extern Lisp_Object QCrelief, QCmargin, QCconversion, Qimage;
7437 extern Lisp_Object Qlaplace;
7438
7439 /* If image is a vector, choose the image according to the
7440 button state. */
7441 image = PROP (TOOL_BAR_ITEM_IMAGES);
7442 if (VECTORP (image))
7443 {
7444 if (enabled_p)
7445 idx = (selected_p
7446 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
7447 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
7448 else
7449 idx = (selected_p
7450 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
7451 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
7452
7453 xassert (ASIZE (image) >= idx);
7454 image = AREF (image, idx);
7455 }
7456 else
7457 idx = -1;
7458
7459 /* Ignore invalid image specifications. */
7460 if (!valid_image_p (image))
7461 continue;
7462
7463 /* Display the tool-bar button pressed, or depressed. */
7464 plist = Fcopy_sequence (XCDR (image));
7465
7466 /* Compute margin and relief to draw. */
7467 relief = (tool_bar_button_relief > 0
7468 ? tool_bar_button_relief
7469 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
7470 hmargin = vmargin = relief;
7471
7472 if (INTEGERP (Vtool_bar_button_margin)
7473 && XINT (Vtool_bar_button_margin) > 0)
7474 {
7475 hmargin += XFASTINT (Vtool_bar_button_margin);
7476 vmargin += XFASTINT (Vtool_bar_button_margin);
7477 }
7478 else if (CONSP (Vtool_bar_button_margin))
7479 {
7480 if (INTEGERP (XCAR (Vtool_bar_button_margin))
7481 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
7482 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
7483
7484 if (INTEGERP (XCDR (Vtool_bar_button_margin))
7485 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
7486 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
7487 }
7488
7489 if (auto_raise_tool_bar_buttons_p)
7490 {
7491 /* Add a `:relief' property to the image spec if the item is
7492 selected. */
7493 if (selected_p)
7494 {
7495 plist = Fplist_put (plist, QCrelief, make_number (-relief));
7496 hmargin -= relief;
7497 vmargin -= relief;
7498 }
7499 }
7500 else
7501 {
7502 /* If image is selected, display it pressed, i.e. with a
7503 negative relief. If it's not selected, display it with a
7504 raised relief. */
7505 plist = Fplist_put (plist, QCrelief,
7506 (selected_p
7507 ? make_number (-relief)
7508 : make_number (relief)));
7509 hmargin -= relief;
7510 vmargin -= relief;
7511 }
7512
7513 /* Put a margin around the image. */
7514 if (hmargin || vmargin)
7515 {
7516 if (hmargin == vmargin)
7517 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
7518 else
7519 plist = Fplist_put (plist, QCmargin,
7520 Fcons (make_number (hmargin),
7521 make_number (vmargin)));
7522 }
7523
7524 /* If button is not enabled, and we don't have special images
7525 for the disabled state, make the image appear disabled by
7526 applying an appropriate algorithm to it. */
7527 if (!enabled_p && idx < 0)
7528 plist = Fplist_put (plist, QCconversion, Qdisabled);
7529
7530 /* Put a `display' text property on the string for the image to
7531 display. Put a `menu-item' property on the string that gives
7532 the start of this item's properties in the tool-bar items
7533 vector. */
7534 image = Fcons (Qimage, plist);
7535 props = list4 (Qdisplay, image,
7536 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
7537
7538 /* Let the last image hide all remaining spaces in the tool bar
7539 string. The string can be longer than needed when we reuse a
7540 previous string. */
7541 if (i + 1 == f->n_tool_bar_items)
7542 end = XSTRING (f->desired_tool_bar_string)->size;
7543 else
7544 end = i + 1;
7545 Fadd_text_properties (make_number (i), make_number (end),
7546 props, f->desired_tool_bar_string);
7547 #undef PROP
7548 }
7549
7550 UNGCPRO;
7551 }
7552
7553
7554 /* Display one line of the tool-bar of frame IT->f. */
7555
7556 static void
7557 display_tool_bar_line (it)
7558 struct it *it;
7559 {
7560 struct glyph_row *row = it->glyph_row;
7561 int max_x = it->last_visible_x;
7562 struct glyph *last;
7563
7564 prepare_desired_row (row);
7565 row->y = it->current_y;
7566
7567 /* Note that this isn't made use of if the face hasn't a box,
7568 so there's no need to check the face here. */
7569 it->start_of_box_run_p = 1;
7570
7571 while (it->current_x < max_x)
7572 {
7573 int x_before, x, n_glyphs_before, i, nglyphs;
7574
7575 /* Get the next display element. */
7576 if (!get_next_display_element (it))
7577 break;
7578
7579 /* Produce glyphs. */
7580 x_before = it->current_x;
7581 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
7582 PRODUCE_GLYPHS (it);
7583
7584 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
7585 i = 0;
7586 x = x_before;
7587 while (i < nglyphs)
7588 {
7589 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
7590
7591 if (x + glyph->pixel_width > max_x)
7592 {
7593 /* Glyph doesn't fit on line. */
7594 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
7595 it->current_x = x;
7596 goto out;
7597 }
7598
7599 ++it->hpos;
7600 x += glyph->pixel_width;
7601 ++i;
7602 }
7603
7604 /* Stop at line ends. */
7605 if (ITERATOR_AT_END_OF_LINE_P (it))
7606 break;
7607
7608 set_iterator_to_next (it, 1);
7609 }
7610
7611 out:;
7612
7613 row->displays_text_p = row->used[TEXT_AREA] != 0;
7614 extend_face_to_end_of_line (it);
7615 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
7616 last->right_box_line_p = 1;
7617 if (last == row->glyphs[TEXT_AREA])
7618 last->left_box_line_p = 1;
7619 compute_line_metrics (it);
7620
7621 /* If line is empty, make it occupy the rest of the tool-bar. */
7622 if (!row->displays_text_p)
7623 {
7624 row->height = row->phys_height = it->last_visible_y - row->y;
7625 row->ascent = row->phys_ascent = 0;
7626 }
7627
7628 row->full_width_p = 1;
7629 row->continued_p = 0;
7630 row->truncated_on_left_p = 0;
7631 row->truncated_on_right_p = 0;
7632
7633 it->current_x = it->hpos = 0;
7634 it->current_y += row->height;
7635 ++it->vpos;
7636 ++it->glyph_row;
7637 }
7638
7639
7640 /* Value is the number of screen lines needed to make all tool-bar
7641 items of frame F visible. */
7642
7643 static int
7644 tool_bar_lines_needed (f)
7645 struct frame *f;
7646 {
7647 struct window *w = XWINDOW (f->tool_bar_window);
7648 struct it it;
7649
7650 /* Initialize an iterator for iteration over
7651 F->desired_tool_bar_string in the tool-bar window of frame F. */
7652 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7653 it.first_visible_x = 0;
7654 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7655 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7656
7657 while (!ITERATOR_AT_END_P (&it))
7658 {
7659 it.glyph_row = w->desired_matrix->rows;
7660 clear_glyph_row (it.glyph_row);
7661 display_tool_bar_line (&it);
7662 }
7663
7664 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
7665 }
7666
7667
7668 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
7669 0, 1, 0,
7670 "Return the number of lines occupied by the tool bar of FRAME.")
7671 (frame)
7672 Lisp_Object frame;
7673 {
7674 struct frame *f;
7675 struct window *w;
7676 int nlines = 0;
7677
7678 if (NILP (frame))
7679 frame = selected_frame;
7680 else
7681 CHECK_FRAME (frame, 0);
7682 f = XFRAME (frame);
7683
7684 if (WINDOWP (f->tool_bar_window)
7685 || (w = XWINDOW (f->tool_bar_window),
7686 XFASTINT (w->height) > 0))
7687 {
7688 update_tool_bar (f, 1);
7689 if (f->n_tool_bar_items)
7690 {
7691 build_desired_tool_bar_string (f);
7692 nlines = tool_bar_lines_needed (f);
7693 }
7694 }
7695
7696 return make_number (nlines);
7697 }
7698
7699
7700 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
7701 height should be changed. */
7702
7703 static int
7704 redisplay_tool_bar (f)
7705 struct frame *f;
7706 {
7707 struct window *w;
7708 struct it it;
7709 struct glyph_row *row;
7710 int change_height_p = 0;
7711
7712 /* If frame hasn't a tool-bar window or if it is zero-height, don't
7713 do anything. This means you must start with tool-bar-lines
7714 non-zero to get the auto-sizing effect. Or in other words, you
7715 can turn off tool-bars by specifying tool-bar-lines zero. */
7716 if (!WINDOWP (f->tool_bar_window)
7717 || (w = XWINDOW (f->tool_bar_window),
7718 XFASTINT (w->height) == 0))
7719 return 0;
7720
7721 /* Set up an iterator for the tool-bar window. */
7722 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7723 it.first_visible_x = 0;
7724 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7725 row = it.glyph_row;
7726
7727 /* Build a string that represents the contents of the tool-bar. */
7728 build_desired_tool_bar_string (f);
7729 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7730
7731 /* Display as many lines as needed to display all tool-bar items. */
7732 while (it.current_y < it.last_visible_y)
7733 display_tool_bar_line (&it);
7734
7735 /* It doesn't make much sense to try scrolling in the tool-bar
7736 window, so don't do it. */
7737 w->desired_matrix->no_scrolling_p = 1;
7738 w->must_be_updated_p = 1;
7739
7740 if (auto_resize_tool_bars_p)
7741 {
7742 int nlines;
7743
7744 /* If we couldn't display everything, change the tool-bar's
7745 height. */
7746 if (IT_STRING_CHARPOS (it) < it.end_charpos)
7747 change_height_p = 1;
7748
7749 /* If there are blank lines at the end, except for a partially
7750 visible blank line at the end that is smaller than
7751 CANON_Y_UNIT, change the tool-bar's height. */
7752 row = it.glyph_row - 1;
7753 if (!row->displays_text_p
7754 && row->height >= CANON_Y_UNIT (f))
7755 change_height_p = 1;
7756
7757 /* If row displays tool-bar items, but is partially visible,
7758 change the tool-bar's height. */
7759 if (row->displays_text_p
7760 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
7761 change_height_p = 1;
7762
7763 /* Resize windows as needed by changing the `tool-bar-lines'
7764 frame parameter. */
7765 if (change_height_p
7766 && (nlines = tool_bar_lines_needed (f),
7767 nlines != XFASTINT (w->height)))
7768 {
7769 extern Lisp_Object Qtool_bar_lines;
7770 Lisp_Object frame;
7771 int old_height = XFASTINT (w->height);
7772
7773 XSETFRAME (frame, f);
7774 clear_glyph_matrix (w->desired_matrix);
7775 Fmodify_frame_parameters (frame,
7776 Fcons (Fcons (Qtool_bar_lines,
7777 make_number (nlines)),
7778 Qnil));
7779 if (XFASTINT (w->height) != old_height)
7780 fonts_changed_p = 1;
7781 }
7782 }
7783
7784 return change_height_p;
7785 }
7786
7787
7788 /* Get information about the tool-bar item which is displayed in GLYPH
7789 on frame F. Return in *PROP_IDX the index where tool-bar item
7790 properties start in F->tool_bar_items. Value is zero if
7791 GLYPH doesn't display a tool-bar item. */
7792
7793 int
7794 tool_bar_item_info (f, glyph, prop_idx)
7795 struct frame *f;
7796 struct glyph *glyph;
7797 int *prop_idx;
7798 {
7799 Lisp_Object prop;
7800 int success_p;
7801
7802 /* Get the text property `menu-item' at pos. The value of that
7803 property is the start index of this item's properties in
7804 F->tool_bar_items. */
7805 prop = Fget_text_property (make_number (glyph->charpos),
7806 Qmenu_item, f->current_tool_bar_string);
7807 if (INTEGERP (prop))
7808 {
7809 *prop_idx = XINT (prop);
7810 success_p = 1;
7811 }
7812 else
7813 success_p = 0;
7814
7815 return success_p;
7816 }
7817
7818 #endif /* HAVE_WINDOW_SYSTEM */
7819
7820
7821 \f
7822 /************************************************************************
7823 Horizontal scrolling
7824 ************************************************************************/
7825
7826 static int hscroll_window_tree P_ ((Lisp_Object));
7827 static int hscroll_windows P_ ((Lisp_Object));
7828
7829 /* For all leaf windows in the window tree rooted at WINDOW, set their
7830 hscroll value so that PT is (i) visible in the window, and (ii) so
7831 that it is not within a certain margin at the window's left and
7832 right border. Value is non-zero if any window's hscroll has been
7833 changed. */
7834
7835 static int
7836 hscroll_window_tree (window)
7837 Lisp_Object window;
7838 {
7839 int hscrolled_p = 0;
7840
7841 while (WINDOWP (window))
7842 {
7843 struct window *w = XWINDOW (window);
7844
7845 if (WINDOWP (w->hchild))
7846 hscrolled_p |= hscroll_window_tree (w->hchild);
7847 else if (WINDOWP (w->vchild))
7848 hscrolled_p |= hscroll_window_tree (w->vchild);
7849 else if (w->cursor.vpos >= 0)
7850 {
7851 int hscroll_margin, text_area_x, text_area_y;
7852 int text_area_width, text_area_height;
7853 struct glyph_row *current_cursor_row
7854 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
7855 struct glyph_row *desired_cursor_row
7856 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
7857 struct glyph_row *cursor_row
7858 = (desired_cursor_row->enabled_p
7859 ? desired_cursor_row
7860 : current_cursor_row);
7861
7862 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
7863 &text_area_width, &text_area_height);
7864
7865 /* Scroll when cursor is inside this scroll margin. */
7866 hscroll_margin = 5 * CANON_X_UNIT (XFRAME (w->frame));
7867
7868 if ((XFASTINT (w->hscroll)
7869 && w->cursor.x < hscroll_margin)
7870 || (cursor_row->enabled_p
7871 && cursor_row->truncated_on_right_p
7872 && (w->cursor.x > text_area_width - hscroll_margin)))
7873 {
7874 struct it it;
7875 int hscroll;
7876 struct buffer *saved_current_buffer;
7877 int pt;
7878
7879 /* Find point in a display of infinite width. */
7880 saved_current_buffer = current_buffer;
7881 current_buffer = XBUFFER (w->buffer);
7882
7883 if (w == XWINDOW (selected_window))
7884 pt = BUF_PT (current_buffer);
7885 else
7886 {
7887 pt = marker_position (w->pointm);
7888 pt = max (BEGV, pt);
7889 pt = min (ZV, pt);
7890 }
7891
7892 /* Move iterator to pt starting at cursor_row->start in
7893 a line with infinite width. */
7894 init_to_row_start (&it, w, cursor_row);
7895 it.last_visible_x = INFINITY;
7896 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
7897 current_buffer = saved_current_buffer;
7898
7899 /* Center cursor in window. */
7900 hscroll = (max (0, it.current_x - text_area_width / 2)
7901 / CANON_X_UNIT (it.f));
7902 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
7903
7904 /* Don't call Fset_window_hscroll if value hasn't
7905 changed because it will prevent redisplay
7906 optimizations. */
7907 if (XFASTINT (w->hscroll) != hscroll)
7908 {
7909 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
7910 w->hscroll = make_number (hscroll);
7911 hscrolled_p = 1;
7912 }
7913 }
7914 }
7915
7916 window = w->next;
7917 }
7918
7919 /* Value is non-zero if hscroll of any leaf window has been changed. */
7920 return hscrolled_p;
7921 }
7922
7923
7924 /* Set hscroll so that cursor is visible and not inside horizontal
7925 scroll margins for all windows in the tree rooted at WINDOW. See
7926 also hscroll_window_tree above. Value is non-zero if any window's
7927 hscroll has been changed. If it has, desired matrices on the frame
7928 of WINDOW are cleared. */
7929
7930 static int
7931 hscroll_windows (window)
7932 Lisp_Object window;
7933 {
7934 int hscrolled_p;
7935
7936 if (automatic_hscrolling_p)
7937 {
7938 hscrolled_p = hscroll_window_tree (window);
7939 if (hscrolled_p)
7940 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
7941 }
7942 else
7943 hscrolled_p = 0;
7944 return hscrolled_p;
7945 }
7946
7947
7948 \f
7949 /************************************************************************
7950 Redisplay
7951 ************************************************************************/
7952
7953 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
7954 to a non-zero value. This is sometimes handy to have in a debugger
7955 session. */
7956
7957 #if GLYPH_DEBUG
7958
7959 /* First and last unchanged row for try_window_id. */
7960
7961 int debug_first_unchanged_at_end_vpos;
7962 int debug_last_unchanged_at_beg_vpos;
7963
7964 /* Delta vpos and y. */
7965
7966 int debug_dvpos, debug_dy;
7967
7968 /* Delta in characters and bytes for try_window_id. */
7969
7970 int debug_delta, debug_delta_bytes;
7971
7972 /* Values of window_end_pos and window_end_vpos at the end of
7973 try_window_id. */
7974
7975 int debug_end_pos, debug_end_vpos;
7976
7977 /* Append a string to W->desired_matrix->method. FMT is a printf
7978 format string. A1...A9 are a supplement for a variable-length
7979 argument list. If trace_redisplay_p is non-zero also printf the
7980 resulting string to stderr. */
7981
7982 static void
7983 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
7984 struct window *w;
7985 char *fmt;
7986 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
7987 {
7988 char buffer[512];
7989 char *method = w->desired_matrix->method;
7990 int len = strlen (method);
7991 int size = sizeof w->desired_matrix->method;
7992 int remaining = size - len - 1;
7993
7994 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
7995 if (len && remaining)
7996 {
7997 method[len] = '|';
7998 --remaining, ++len;
7999 }
8000
8001 strncpy (method + len, buffer, remaining);
8002
8003 if (trace_redisplay_p)
8004 fprintf (stderr, "%p (%s): %s\n",
8005 w,
8006 ((BUFFERP (w->buffer)
8007 && STRINGP (XBUFFER (w->buffer)->name))
8008 ? (char *) XSTRING (XBUFFER (w->buffer)->name)->data
8009 : "no buffer"),
8010 buffer);
8011 }
8012
8013 #endif /* GLYPH_DEBUG */
8014
8015
8016 /* This counter is used to clear the face cache every once in a while
8017 in redisplay_internal. It is incremented for each redisplay.
8018 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
8019 cleared. */
8020
8021 #define CLEAR_FACE_CACHE_COUNT 10000
8022 static int clear_face_cache_count;
8023
8024 /* Record the previous terminal frame we displayed. */
8025
8026 static struct frame *previous_terminal_frame;
8027
8028 /* Non-zero while redisplay_internal is in progress. */
8029
8030 int redisplaying_p;
8031
8032
8033 /* Value is non-zero if all changes in window W, which displays
8034 current_buffer, are in the text between START and END. START is a
8035 buffer position, END is given as a distance from Z. Used in
8036 redisplay_internal for display optimization. */
8037
8038 static INLINE int
8039 text_outside_line_unchanged_p (w, start, end)
8040 struct window *w;
8041 int start, end;
8042 {
8043 int unchanged_p = 1;
8044
8045 /* If text or overlays have changed, see where. */
8046 if (XFASTINT (w->last_modified) < MODIFF
8047 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8048 {
8049 /* Gap in the line? */
8050 if (GPT < start || Z - GPT < end)
8051 unchanged_p = 0;
8052
8053 /* Changes start in front of the line, or end after it? */
8054 if (unchanged_p
8055 && (BEG_UNCHANGED < start - 1
8056 || END_UNCHANGED < end))
8057 unchanged_p = 0;
8058
8059 /* If selective display, can't optimize if changes start at the
8060 beginning of the line. */
8061 if (unchanged_p
8062 && INTEGERP (current_buffer->selective_display)
8063 && XINT (current_buffer->selective_display) > 0
8064 && (BEG_UNCHANGED < start || GPT <= start))
8065 unchanged_p = 0;
8066 }
8067
8068 return unchanged_p;
8069 }
8070
8071
8072 /* Do a frame update, taking possible shortcuts into account. This is
8073 the main external entry point for redisplay.
8074
8075 If the last redisplay displayed an echo area message and that message
8076 is no longer requested, we clear the echo area or bring back the
8077 mini-buffer if that is in use. */
8078
8079 void
8080 redisplay ()
8081 {
8082 redisplay_internal (0);
8083 }
8084
8085 /* Return 1 if point moved out of or into a composition. Otherwise
8086 return 0. PREV_BUF and PREV_PT are the last point buffer and
8087 position. BUF and PT are the current point buffer and position. */
8088
8089 int
8090 check_point_in_composition (prev_buf, prev_pt, buf, pt)
8091 struct buffer *prev_buf, *buf;
8092 int prev_pt, pt;
8093 {
8094 int start, end;
8095 Lisp_Object prop;
8096 Lisp_Object buffer;
8097
8098 XSETBUFFER (buffer, buf);
8099 /* Check a composition at the last point if point moved within the
8100 same buffer. */
8101 if (prev_buf == buf)
8102 {
8103 if (prev_pt == pt)
8104 /* Point didn't move. */
8105 return 0;
8106
8107 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
8108 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
8109 && COMPOSITION_VALID_P (start, end, prop)
8110 && start < prev_pt && end > prev_pt)
8111 /* The last point was within the composition. Return 1 iff
8112 point moved out of the composition. */
8113 return (pt <= start || pt >= end);
8114 }
8115
8116 /* Check a composition at the current point. */
8117 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
8118 && find_composition (pt, -1, &start, &end, &prop, buffer)
8119 && COMPOSITION_VALID_P (start, end, prop)
8120 && start < pt && end > pt);
8121 }
8122
8123 /* Reconsider the setting of B->clip_changed which is displayed
8124 in window W. */
8125
8126 static INLINE void
8127 reconsider_clip_changes (w, b)
8128 struct window *w;
8129 struct buffer *b;
8130 {
8131 if (b->prevent_redisplay_optimizations_p)
8132 b->clip_changed = 1;
8133 else if (b->clip_changed
8134 && !NILP (w->window_end_valid)
8135 && w->current_matrix->buffer == b
8136 && w->current_matrix->zv == BUF_ZV (b)
8137 && w->current_matrix->begv == BUF_BEGV (b))
8138 b->clip_changed = 0;
8139
8140 /* If display wasn't paused, and W is not a tool bar window, see if
8141 point has been moved into or out of a composition. In that case,
8142 we set b->clip_changed to 1 to force updating the screen. If
8143 b->clip_changed has already been set to 1, we can skip this
8144 check. */
8145 if (!b->clip_changed
8146 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
8147 {
8148 int pt;
8149
8150 if (w == XWINDOW (selected_window))
8151 pt = BUF_PT (current_buffer);
8152 else
8153 pt = marker_position (w->pointm);
8154
8155 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
8156 || pt != XINT (w->last_point))
8157 && check_point_in_composition (w->current_matrix->buffer,
8158 XINT (w->last_point),
8159 XBUFFER (w->buffer), pt))
8160 b->clip_changed = 1;
8161 }
8162 }
8163
8164
8165 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
8166 response to any user action; therefore, we should preserve the echo
8167 area. (Actually, our caller does that job.) Perhaps in the future
8168 avoid recentering windows if it is not necessary; currently that
8169 causes some problems. */
8170
8171 static void
8172 redisplay_internal (preserve_echo_area)
8173 int preserve_echo_area;
8174 {
8175 struct window *w = XWINDOW (selected_window);
8176 struct frame *f = XFRAME (w->frame);
8177 int pause;
8178 int must_finish = 0;
8179 struct text_pos tlbufpos, tlendpos;
8180 int number_of_visible_frames;
8181 int count;
8182 struct frame *sf = SELECTED_FRAME ();
8183
8184 /* Non-zero means redisplay has to consider all windows on all
8185 frames. Zero means, only selected_window is considered. */
8186 int consider_all_windows_p;
8187
8188 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
8189
8190 /* No redisplay if running in batch mode or frame is not yet fully
8191 initialized, or redisplay is explicitly turned off by setting
8192 Vinhibit_redisplay. */
8193 if (noninteractive
8194 || !NILP (Vinhibit_redisplay)
8195 || !f->glyphs_initialized_p)
8196 return;
8197
8198 /* The flag redisplay_performed_directly_p is set by
8199 direct_output_for_insert when it already did the whole screen
8200 update necessary. */
8201 if (redisplay_performed_directly_p)
8202 {
8203 redisplay_performed_directly_p = 0;
8204 if (!hscroll_windows (selected_window))
8205 return;
8206 }
8207
8208 #ifdef USE_X_TOOLKIT
8209 if (popup_activated ())
8210 return;
8211 #endif
8212
8213 /* I don't think this happens but let's be paranoid. */
8214 if (redisplaying_p)
8215 return;
8216
8217 /* Record a function that resets redisplaying_p to its old value
8218 when we leave this function. */
8219 count = BINDING_STACK_SIZE ();
8220 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
8221 ++redisplaying_p;
8222
8223 retry:
8224 pause = 0;
8225 reconsider_clip_changes (w, current_buffer);
8226
8227 /* If new fonts have been loaded that make a glyph matrix adjustment
8228 necessary, do it. */
8229 if (fonts_changed_p)
8230 {
8231 adjust_glyphs (NULL);
8232 ++windows_or_buffers_changed;
8233 fonts_changed_p = 0;
8234 }
8235
8236 /* If face_change_count is non-zero, init_iterator will free all
8237 realized faces, which includes the faces referenced from current
8238 matrices. So, we can't reuse current matrices in this case. */
8239 if (face_change_count)
8240 ++windows_or_buffers_changed;
8241
8242 if (! FRAME_WINDOW_P (sf)
8243 && previous_terminal_frame != sf)
8244 {
8245 /* Since frames on an ASCII terminal share the same display
8246 area, displaying a different frame means redisplay the whole
8247 thing. */
8248 windows_or_buffers_changed++;
8249 SET_FRAME_GARBAGED (sf);
8250 XSETFRAME (Vterminal_frame, sf);
8251 }
8252 previous_terminal_frame = sf;
8253
8254 /* Set the visible flags for all frames. Do this before checking
8255 for resized or garbaged frames; they want to know if their frames
8256 are visible. See the comment in frame.h for
8257 FRAME_SAMPLE_VISIBILITY. */
8258 {
8259 Lisp_Object tail, frame;
8260
8261 number_of_visible_frames = 0;
8262
8263 FOR_EACH_FRAME (tail, frame)
8264 {
8265 struct frame *f = XFRAME (frame);
8266
8267 FRAME_SAMPLE_VISIBILITY (f);
8268 if (FRAME_VISIBLE_P (f))
8269 ++number_of_visible_frames;
8270 clear_desired_matrices (f);
8271 }
8272 }
8273
8274 /* Notice any pending interrupt request to change frame size. */
8275 do_pending_window_change (1);
8276
8277 /* Clear frames marked as garbaged. */
8278 if (frame_garbaged)
8279 clear_garbaged_frames ();
8280
8281 /* Build menubar and tool-bar items. */
8282 prepare_menu_bars ();
8283
8284 if (windows_or_buffers_changed)
8285 update_mode_lines++;
8286
8287 /* Detect case that we need to write or remove a star in the mode line. */
8288 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
8289 {
8290 w->update_mode_line = Qt;
8291 if (buffer_shared > 1)
8292 update_mode_lines++;
8293 }
8294
8295 /* If %c is in the mode line, update it if needed. */
8296 if (!NILP (w->column_number_displayed)
8297 /* This alternative quickly identifies a common case
8298 where no change is needed. */
8299 && !(PT == XFASTINT (w->last_point)
8300 && XFASTINT (w->last_modified) >= MODIFF
8301 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
8302 && XFASTINT (w->column_number_displayed) != current_column ())
8303 w->update_mode_line = Qt;
8304
8305 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
8306
8307 /* The variable buffer_shared is set in redisplay_window and
8308 indicates that we redisplay a buffer in different windows. See
8309 there. */
8310 consider_all_windows_p = update_mode_lines || buffer_shared > 1;
8311
8312 /* If specs for an arrow have changed, do thorough redisplay
8313 to ensure we remove any arrow that should no longer exist. */
8314 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
8315 || ! EQ (Voverlay_arrow_string, last_arrow_string))
8316 consider_all_windows_p = windows_or_buffers_changed = 1;
8317
8318 /* Normally the message* functions will have already displayed and
8319 updated the echo area, but the frame may have been trashed, or
8320 the update may have been preempted, so display the echo area
8321 again here. Checking both message buffers captures the case that
8322 the echo area should be cleared. */
8323 if (!NILP (echo_area_buffer[0]) || !NILP (echo_area_buffer[1]))
8324 {
8325 int window_height_changed_p = echo_area_display (0);
8326 must_finish = 1;
8327
8328 if (fonts_changed_p)
8329 goto retry;
8330 else if (window_height_changed_p)
8331 {
8332 consider_all_windows_p = 1;
8333 ++update_mode_lines;
8334 ++windows_or_buffers_changed;
8335
8336 /* If window configuration was changed, frames may have been
8337 marked garbaged. Clear them or we will experience
8338 surprises wrt scrolling. */
8339 if (frame_garbaged)
8340 clear_garbaged_frames ();
8341 }
8342 }
8343 else if (EQ (selected_window, minibuf_window)
8344 && (current_buffer->clip_changed
8345 || XFASTINT (w->last_modified) < MODIFF
8346 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8347 && resize_mini_window (w, 0))
8348 {
8349 /* Resized active mini-window to fit the size of what it is
8350 showing if its contents might have changed. */
8351 must_finish = 1;
8352 consider_all_windows_p = 1;
8353 ++windows_or_buffers_changed;
8354 ++update_mode_lines;
8355
8356 /* If window configuration was changed, frames may have been
8357 marked garbaged. Clear them or we will experience
8358 surprises wrt scrolling. */
8359 if (frame_garbaged)
8360 clear_garbaged_frames ();
8361 }
8362
8363
8364 /* If showing the region, and mark has changed, we must redisplay
8365 the whole window. The assignment to this_line_start_pos prevents
8366 the optimization directly below this if-statement. */
8367 if (((!NILP (Vtransient_mark_mode)
8368 && !NILP (XBUFFER (w->buffer)->mark_active))
8369 != !NILP (w->region_showing))
8370 || (!NILP (w->region_showing)
8371 && !EQ (w->region_showing,
8372 Fmarker_position (XBUFFER (w->buffer)->mark))))
8373 CHARPOS (this_line_start_pos) = 0;
8374
8375 /* Optimize the case that only the line containing the cursor in the
8376 selected window has changed. Variables starting with this_ are
8377 set in display_line and record information about the line
8378 containing the cursor. */
8379 tlbufpos = this_line_start_pos;
8380 tlendpos = this_line_end_pos;
8381 if (!consider_all_windows_p
8382 && CHARPOS (tlbufpos) > 0
8383 && NILP (w->update_mode_line)
8384 && !current_buffer->clip_changed
8385 && FRAME_VISIBLE_P (XFRAME (w->frame))
8386 && !FRAME_OBSCURED_P (XFRAME (w->frame))
8387 /* Make sure recorded data applies to current buffer, etc. */
8388 && this_line_buffer == current_buffer
8389 && current_buffer == XBUFFER (w->buffer)
8390 && NILP (w->force_start)
8391 /* Point must be on the line that we have info recorded about. */
8392 && PT >= CHARPOS (tlbufpos)
8393 && PT <= Z - CHARPOS (tlendpos)
8394 /* All text outside that line, including its final newline,
8395 must be unchanged */
8396 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
8397 CHARPOS (tlendpos)))
8398 {
8399 if (CHARPOS (tlbufpos) > BEGV
8400 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
8401 && (CHARPOS (tlbufpos) == ZV
8402 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
8403 /* Former continuation line has disappeared by becoming empty */
8404 goto cancel;
8405 else if (XFASTINT (w->last_modified) < MODIFF
8406 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
8407 || MINI_WINDOW_P (w))
8408 {
8409 /* We have to handle the case of continuation around a
8410 wide-column character (See the comment in indent.c around
8411 line 885).
8412
8413 For instance, in the following case:
8414
8415 -------- Insert --------
8416 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
8417 J_I_ ==> J_I_ `^^' are cursors.
8418 ^^ ^^
8419 -------- --------
8420
8421 As we have to redraw the line above, we should goto cancel. */
8422
8423 struct it it;
8424 int line_height_before = this_line_pixel_height;
8425
8426 /* Note that start_display will handle the case that the
8427 line starting at tlbufpos is a continuation lines. */
8428 start_display (&it, w, tlbufpos);
8429
8430 /* Implementation note: It this still necessary? */
8431 if (it.current_x != this_line_start_x)
8432 goto cancel;
8433
8434 TRACE ((stderr, "trying display optimization 1\n"));
8435 w->cursor.vpos = -1;
8436 overlay_arrow_seen = 0;
8437 it.vpos = this_line_vpos;
8438 it.current_y = this_line_y;
8439 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
8440 display_line (&it);
8441
8442 /* If line contains point, is not continued,
8443 and ends at same distance from eob as before, we win */
8444 if (w->cursor.vpos >= 0
8445 /* Line is not continued, otherwise this_line_start_pos
8446 would have been set to 0 in display_line. */
8447 && CHARPOS (this_line_start_pos)
8448 /* Line ends as before. */
8449 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
8450 /* Line has same height as before. Otherwise other lines
8451 would have to be shifted up or down. */
8452 && this_line_pixel_height == line_height_before)
8453 {
8454 /* If this is not the window's last line, we must adjust
8455 the charstarts of the lines below. */
8456 if (it.current_y < it.last_visible_y)
8457 {
8458 struct glyph_row *row
8459 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
8460 int delta, delta_bytes;
8461
8462 if (Z - CHARPOS (tlendpos) == ZV)
8463 {
8464 /* This line ends at end of (accessible part of)
8465 buffer. There is no newline to count. */
8466 delta = (Z
8467 - CHARPOS (tlendpos)
8468 - MATRIX_ROW_START_CHARPOS (row));
8469 delta_bytes = (Z_BYTE
8470 - BYTEPOS (tlendpos)
8471 - MATRIX_ROW_START_BYTEPOS (row));
8472 }
8473 else
8474 {
8475 /* This line ends in a newline. Must take
8476 account of the newline and the rest of the
8477 text that follows. */
8478 delta = (Z
8479 - CHARPOS (tlendpos)
8480 - MATRIX_ROW_START_CHARPOS (row));
8481 delta_bytes = (Z_BYTE
8482 - BYTEPOS (tlendpos)
8483 - MATRIX_ROW_START_BYTEPOS (row));
8484 }
8485
8486 increment_matrix_positions (w->current_matrix,
8487 this_line_vpos + 1,
8488 w->current_matrix->nrows,
8489 delta, delta_bytes);
8490 }
8491
8492 /* If this row displays text now but previously didn't,
8493 or vice versa, w->window_end_vpos may have to be
8494 adjusted. */
8495 if ((it.glyph_row - 1)->displays_text_p)
8496 {
8497 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
8498 XSETINT (w->window_end_vpos, this_line_vpos);
8499 }
8500 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
8501 && this_line_vpos > 0)
8502 XSETINT (w->window_end_vpos, this_line_vpos - 1);
8503 w->window_end_valid = Qnil;
8504
8505 /* Update hint: No need to try to scroll in update_window. */
8506 w->desired_matrix->no_scrolling_p = 1;
8507
8508 #if GLYPH_DEBUG
8509 *w->desired_matrix->method = 0;
8510 debug_method_add (w, "optimization 1");
8511 #endif
8512 goto update;
8513 }
8514 else
8515 goto cancel;
8516 }
8517 else if (/* Cursor position hasn't changed. */
8518 PT == XFASTINT (w->last_point)
8519 /* Make sure the cursor was last displayed
8520 in this window. Otherwise we have to reposition it. */
8521 && 0 <= w->cursor.vpos
8522 && XINT (w->height) > w->cursor.vpos)
8523 {
8524 if (!must_finish)
8525 {
8526 do_pending_window_change (1);
8527
8528 /* We used to always goto end_of_redisplay here, but this
8529 isn't enough if we have a blinking cursor. */
8530 if (w->cursor_off_p == w->last_cursor_off_p)
8531 goto end_of_redisplay;
8532 }
8533 goto update;
8534 }
8535 /* If highlighting the region, or if the cursor is in the echo area,
8536 then we can't just move the cursor. */
8537 else if (! (!NILP (Vtransient_mark_mode)
8538 && !NILP (current_buffer->mark_active))
8539 && (EQ (selected_window, current_buffer->last_selected_window)
8540 || highlight_nonselected_windows)
8541 && NILP (w->region_showing)
8542 && NILP (Vshow_trailing_whitespace)
8543 && !cursor_in_echo_area)
8544 {
8545 struct it it;
8546 struct glyph_row *row;
8547
8548 /* Skip from tlbufpos to PT and see where it is. Note that
8549 PT may be in invisible text. If so, we will end at the
8550 next visible position. */
8551 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
8552 NULL, DEFAULT_FACE_ID);
8553 it.current_x = this_line_start_x;
8554 it.current_y = this_line_y;
8555 it.vpos = this_line_vpos;
8556
8557 /* The call to move_it_to stops in front of PT, but
8558 moves over before-strings. */
8559 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
8560
8561 if (it.vpos == this_line_vpos
8562 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
8563 row->enabled_p))
8564 {
8565 xassert (this_line_vpos == it.vpos);
8566 xassert (this_line_y == it.current_y);
8567 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
8568 #if GLYPH_DEBUG
8569 *w->desired_matrix->method = 0;
8570 debug_method_add (w, "optimization 3");
8571 #endif
8572 goto update;
8573 }
8574 else
8575 goto cancel;
8576 }
8577
8578 cancel:
8579 /* Text changed drastically or point moved off of line. */
8580 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
8581 }
8582
8583 CHARPOS (this_line_start_pos) = 0;
8584 consider_all_windows_p |= buffer_shared > 1;
8585 ++clear_face_cache_count;
8586
8587
8588 /* Build desired matrices, and update the display. If
8589 consider_all_windows_p is non-zero, do it for all windows on all
8590 frames. Otherwise do it for selected_window, only. */
8591
8592 if (consider_all_windows_p)
8593 {
8594 Lisp_Object tail, frame;
8595 int i, n = 0, size = 50;
8596 struct frame **updated
8597 = (struct frame **) alloca (size * sizeof *updated);
8598
8599 /* Clear the face cache eventually. */
8600 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
8601 {
8602 clear_face_cache (0);
8603 clear_face_cache_count = 0;
8604 }
8605
8606 /* Recompute # windows showing selected buffer. This will be
8607 incremented each time such a window is displayed. */
8608 buffer_shared = 0;
8609
8610 FOR_EACH_FRAME (tail, frame)
8611 {
8612 struct frame *f = XFRAME (frame);
8613
8614 if (FRAME_WINDOW_P (f) || f == sf)
8615 {
8616 /* Mark all the scroll bars to be removed; we'll redeem
8617 the ones we want when we redisplay their windows. */
8618 if (condemn_scroll_bars_hook)
8619 condemn_scroll_bars_hook (f);
8620
8621 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8622 redisplay_windows (FRAME_ROOT_WINDOW (f));
8623
8624 /* Any scroll bars which redisplay_windows should have
8625 nuked should now go away. */
8626 if (judge_scroll_bars_hook)
8627 judge_scroll_bars_hook (f);
8628
8629 /* If fonts changed, display again. */
8630 if (fonts_changed_p)
8631 goto retry;
8632
8633 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8634 {
8635 /* See if we have to hscroll. */
8636 if (hscroll_windows (f->root_window))
8637 goto retry;
8638
8639 /* Prevent various kinds of signals during display
8640 update. stdio is not robust about handling
8641 signals, which can cause an apparent I/O
8642 error. */
8643 if (interrupt_input)
8644 unrequest_sigio ();
8645 stop_polling ();
8646
8647 /* Update the display. */
8648 set_window_update_flags (XWINDOW (f->root_window), 1);
8649 pause |= update_frame (f, 0, 0);
8650 if (pause)
8651 break;
8652
8653 if (n == size)
8654 {
8655 int nbytes = size * sizeof *updated;
8656 struct frame **p = (struct frame **) alloca (2 * nbytes);
8657 bcopy (updated, p, nbytes);
8658 size *= 2;
8659 }
8660
8661 updated[n++] = f;
8662 }
8663 }
8664 }
8665
8666 /* Do the mark_window_display_accurate after all windows have
8667 been redisplayed because this call resets flags in buffers
8668 which are needed for proper redisplay. */
8669 for (i = 0; i < n; ++i)
8670 {
8671 struct frame *f = updated[i];
8672 mark_window_display_accurate (f->root_window, 1);
8673 if (frame_up_to_date_hook)
8674 frame_up_to_date_hook (f);
8675 }
8676 }
8677 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8678 {
8679 Lisp_Object mini_window;
8680 struct frame *mini_frame;
8681
8682 redisplay_window (selected_window, 1);
8683
8684 /* Compare desired and current matrices, perform output. */
8685 update:
8686
8687 /* If fonts changed, display again. */
8688 if (fonts_changed_p)
8689 goto retry;
8690
8691 /* Prevent various kinds of signals during display update.
8692 stdio is not robust about handling signals,
8693 which can cause an apparent I/O error. */
8694 if (interrupt_input)
8695 unrequest_sigio ();
8696 stop_polling ();
8697
8698 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8699 {
8700 if (hscroll_windows (selected_window))
8701 goto retry;
8702
8703 XWINDOW (selected_window)->must_be_updated_p = 1;
8704 pause = update_frame (sf, 0, 0);
8705 }
8706
8707 /* We may have called echo_area_display at the top of this
8708 function. If the echo area is on another frame, that may
8709 have put text on a frame other than the selected one, so the
8710 above call to update_frame would not have caught it. Catch
8711 it here. */
8712 mini_window = FRAME_MINIBUF_WINDOW (sf);
8713 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8714
8715 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
8716 {
8717 XWINDOW (mini_window)->must_be_updated_p = 1;
8718 pause |= update_frame (mini_frame, 0, 0);
8719 if (!pause && hscroll_windows (mini_window))
8720 goto retry;
8721 }
8722 }
8723
8724 /* If display was paused because of pending input, make sure we do a
8725 thorough update the next time. */
8726 if (pause)
8727 {
8728 /* Prevent the optimization at the beginning of
8729 redisplay_internal that tries a single-line update of the
8730 line containing the cursor in the selected window. */
8731 CHARPOS (this_line_start_pos) = 0;
8732
8733 /* Let the overlay arrow be updated the next time. */
8734 if (!NILP (last_arrow_position))
8735 {
8736 last_arrow_position = Qt;
8737 last_arrow_string = Qt;
8738 }
8739
8740 /* If we pause after scrolling, some rows in the current
8741 matrices of some windows are not valid. */
8742 if (!WINDOW_FULL_WIDTH_P (w)
8743 && !FRAME_WINDOW_P (XFRAME (w->frame)))
8744 update_mode_lines = 1;
8745 }
8746 else
8747 {
8748 if (!consider_all_windows_p)
8749 {
8750 /* This has already been done above if
8751 consider_all_windows_p is set. */
8752 mark_window_display_accurate_1 (w, 1);
8753
8754 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
8755 last_arrow_string = Voverlay_arrow_string;
8756
8757 if (frame_up_to_date_hook != 0)
8758 frame_up_to_date_hook (sf);
8759 }
8760
8761 update_mode_lines = 0;
8762 windows_or_buffers_changed = 0;
8763 }
8764
8765 /* Start SIGIO interrupts coming again. Having them off during the
8766 code above makes it less likely one will discard output, but not
8767 impossible, since there might be stuff in the system buffer here.
8768 But it is much hairier to try to do anything about that. */
8769 if (interrupt_input)
8770 request_sigio ();
8771 start_polling ();
8772
8773 /* If a frame has become visible which was not before, redisplay
8774 again, so that we display it. Expose events for such a frame
8775 (which it gets when becoming visible) don't call the parts of
8776 redisplay constructing glyphs, so simply exposing a frame won't
8777 display anything in this case. So, we have to display these
8778 frames here explicitly. */
8779 if (!pause)
8780 {
8781 Lisp_Object tail, frame;
8782 int new_count = 0;
8783
8784 FOR_EACH_FRAME (tail, frame)
8785 {
8786 int this_is_visible = 0;
8787
8788 if (XFRAME (frame)->visible)
8789 this_is_visible = 1;
8790 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
8791 if (XFRAME (frame)->visible)
8792 this_is_visible = 1;
8793
8794 if (this_is_visible)
8795 new_count++;
8796 }
8797
8798 if (new_count != number_of_visible_frames)
8799 windows_or_buffers_changed++;
8800 }
8801
8802 /* Change frame size now if a change is pending. */
8803 do_pending_window_change (1);
8804
8805 /* If we just did a pending size change, or have additional
8806 visible frames, redisplay again. */
8807 if (windows_or_buffers_changed && !pause)
8808 goto retry;
8809
8810 end_of_redisplay:;
8811
8812 unbind_to (count, Qnil);
8813 }
8814
8815
8816 /* Redisplay, but leave alone any recent echo area message unless
8817 another message has been requested in its place.
8818
8819 This is useful in situations where you need to redisplay but no
8820 user action has occurred, making it inappropriate for the message
8821 area to be cleared. See tracking_off and
8822 wait_reading_process_input for examples of these situations.
8823
8824 FROM_WHERE is an integer saying from where this function was
8825 called. This is useful for debugging. */
8826
8827 void
8828 redisplay_preserve_echo_area (from_where)
8829 int from_where;
8830 {
8831 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
8832
8833 if (!NILP (echo_area_buffer[1]))
8834 {
8835 /* We have a previously displayed message, but no current
8836 message. Redisplay the previous message. */
8837 display_last_displayed_message_p = 1;
8838 redisplay_internal (1);
8839 display_last_displayed_message_p = 0;
8840 }
8841 else
8842 redisplay_internal (1);
8843 }
8844
8845
8846 /* Function registered with record_unwind_protect in
8847 redisplay_internal. Clears the flag indicating that a redisplay is
8848 in progress. */
8849
8850 static Lisp_Object
8851 unwind_redisplay (old_redisplaying_p)
8852 Lisp_Object old_redisplaying_p;
8853 {
8854 redisplaying_p = XFASTINT (old_redisplaying_p);
8855 return Qnil;
8856 }
8857
8858
8859 /* Mark the display of window W as accurate or inaccurate. If
8860 ACCURATE_P is non-zero mark display of W as accurate. If
8861 ACCURATE_P is zero, arrange for W to be redisplayed the next time
8862 redisplay_internal is called. */
8863
8864 static void
8865 mark_window_display_accurate_1 (w, accurate_p)
8866 struct window *w;
8867 int accurate_p;
8868 {
8869 if (BUFFERP (w->buffer))
8870 {
8871 struct buffer *b = XBUFFER (w->buffer);
8872
8873 w->last_modified
8874 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
8875 w->last_overlay_modified
8876 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
8877 w->last_had_star
8878 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
8879
8880 if (accurate_p)
8881 {
8882 b->clip_changed = 0;
8883 b->prevent_redisplay_optimizations_p = 0;
8884
8885 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
8886 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
8887 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
8888 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
8889
8890 w->current_matrix->buffer = b;
8891 w->current_matrix->begv = BUF_BEGV (b);
8892 w->current_matrix->zv = BUF_ZV (b);
8893
8894 w->last_cursor = w->cursor;
8895 w->last_cursor_off_p = w->cursor_off_p;
8896
8897 if (w == XWINDOW (selected_window))
8898 w->last_point = make_number (BUF_PT (b));
8899 else
8900 w->last_point = make_number (XMARKER (w->pointm)->charpos);
8901 }
8902 }
8903
8904 if (accurate_p)
8905 {
8906 w->window_end_valid = w->buffer;
8907 w->update_mode_line = Qnil;
8908 }
8909 }
8910
8911
8912 /* Mark the display of windows in the window tree rooted at WINDOW as
8913 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
8914 windows as accurate. If ACCURATE_P is zero, arrange for windows to
8915 be redisplayed the next time redisplay_internal is called. */
8916
8917 void
8918 mark_window_display_accurate (window, accurate_p)
8919 Lisp_Object window;
8920 int accurate_p;
8921 {
8922 struct window *w;
8923
8924 for (; !NILP (window); window = w->next)
8925 {
8926 w = XWINDOW (window);
8927 mark_window_display_accurate_1 (w, accurate_p);
8928
8929 if (!NILP (w->vchild))
8930 mark_window_display_accurate (w->vchild, accurate_p);
8931 if (!NILP (w->hchild))
8932 mark_window_display_accurate (w->hchild, accurate_p);
8933 }
8934
8935 if (accurate_p)
8936 {
8937 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
8938 last_arrow_string = Voverlay_arrow_string;
8939 }
8940 else
8941 {
8942 /* Force a thorough redisplay the next time by setting
8943 last_arrow_position and last_arrow_string to t, which is
8944 unequal to any useful value of Voverlay_arrow_... */
8945 last_arrow_position = Qt;
8946 last_arrow_string = Qt;
8947 }
8948 }
8949
8950
8951 /* Return value in display table DP (Lisp_Char_Table *) for character
8952 C. Since a display table doesn't have any parent, we don't have to
8953 follow parent. Do not call this function directly but use the
8954 macro DISP_CHAR_VECTOR. */
8955
8956 Lisp_Object
8957 disp_char_vector (dp, c)
8958 struct Lisp_Char_Table *dp;
8959 int c;
8960 {
8961 int code[4], i;
8962 Lisp_Object val;
8963
8964 if (SINGLE_BYTE_CHAR_P (c))
8965 return (dp->contents[c]);
8966
8967 SPLIT_CHAR (c, code[0], code[1], code[2]);
8968 if (code[1] < 32)
8969 code[1] = -1;
8970 else if (code[2] < 32)
8971 code[2] = -1;
8972
8973 /* Here, the possible range of code[0] (== charset ID) is
8974 128..max_charset. Since the top level char table contains data
8975 for multibyte characters after 256th element, we must increment
8976 code[0] by 128 to get a correct index. */
8977 code[0] += 128;
8978 code[3] = -1; /* anchor */
8979
8980 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
8981 {
8982 val = dp->contents[code[i]];
8983 if (!SUB_CHAR_TABLE_P (val))
8984 return (NILP (val) ? dp->defalt : val);
8985 }
8986
8987 /* Here, val is a sub char table. We return the default value of
8988 it. */
8989 return (dp->defalt);
8990 }
8991
8992
8993 \f
8994 /***********************************************************************
8995 Window Redisplay
8996 ***********************************************************************/
8997
8998 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
8999
9000 static void
9001 redisplay_windows (window)
9002 Lisp_Object window;
9003 {
9004 while (!NILP (window))
9005 {
9006 struct window *w = XWINDOW (window);
9007
9008 if (!NILP (w->hchild))
9009 redisplay_windows (w->hchild);
9010 else if (!NILP (w->vchild))
9011 redisplay_windows (w->vchild);
9012 else
9013 redisplay_window (window, 0);
9014
9015 window = w->next;
9016 }
9017 }
9018
9019
9020 /* Set cursor position of W. PT is assumed to be displayed in ROW.
9021 DELTA is the number of bytes by which positions recorded in ROW
9022 differ from current buffer positions. */
9023
9024 void
9025 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
9026 struct window *w;
9027 struct glyph_row *row;
9028 struct glyph_matrix *matrix;
9029 int delta, delta_bytes, dy, dvpos;
9030 {
9031 struct glyph *glyph = row->glyphs[TEXT_AREA];
9032 struct glyph *end = glyph + row->used[TEXT_AREA];
9033 int x = row->x;
9034 int pt_old = PT - delta;
9035
9036 /* Skip over glyphs not having an object at the start of the row.
9037 These are special glyphs like truncation marks on terminal
9038 frames. */
9039 if (row->displays_text_p)
9040 while (glyph < end
9041 && INTEGERP (glyph->object)
9042 && glyph->charpos < 0)
9043 {
9044 x += glyph->pixel_width;
9045 ++glyph;
9046 }
9047
9048 while (glyph < end
9049 && !INTEGERP (glyph->object)
9050 && (!BUFFERP (glyph->object)
9051 || glyph->charpos < pt_old))
9052 {
9053 x += glyph->pixel_width;
9054 ++glyph;
9055 }
9056
9057 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
9058 w->cursor.x = x;
9059 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
9060 w->cursor.y = row->y + dy;
9061
9062 if (w == XWINDOW (selected_window))
9063 {
9064 if (!row->continued_p
9065 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
9066 && row->x == 0)
9067 {
9068 this_line_buffer = XBUFFER (w->buffer);
9069
9070 CHARPOS (this_line_start_pos)
9071 = MATRIX_ROW_START_CHARPOS (row) + delta;
9072 BYTEPOS (this_line_start_pos)
9073 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
9074
9075 CHARPOS (this_line_end_pos)
9076 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
9077 BYTEPOS (this_line_end_pos)
9078 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
9079
9080 this_line_y = w->cursor.y;
9081 this_line_pixel_height = row->height;
9082 this_line_vpos = w->cursor.vpos;
9083 this_line_start_x = row->x;
9084 }
9085 else
9086 CHARPOS (this_line_start_pos) = 0;
9087 }
9088 }
9089
9090
9091 /* Run window scroll functions, if any, for WINDOW with new window
9092 start STARTP. Sets the window start of WINDOW to that position.
9093
9094 We assume that the window's buffer is really current. */
9095
9096 static INLINE struct text_pos
9097 run_window_scroll_functions (window, startp)
9098 Lisp_Object window;
9099 struct text_pos startp;
9100 {
9101 struct window *w = XWINDOW (window);
9102 SET_MARKER_FROM_TEXT_POS (w->start, startp);
9103
9104 if (current_buffer != XBUFFER (w->buffer))
9105 abort ();
9106
9107 if (!NILP (Vwindow_scroll_functions))
9108 {
9109 run_hook_with_args_2 (Qwindow_scroll_functions, window,
9110 make_number (CHARPOS (startp)));
9111 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9112 /* In case the hook functions switch buffers. */
9113 if (current_buffer != XBUFFER (w->buffer))
9114 set_buffer_internal_1 (XBUFFER (w->buffer));
9115 }
9116
9117 return startp;
9118 }
9119
9120
9121 /* Modify the desired matrix of window W and W->vscroll so that the
9122 line containing the cursor is fully visible. */
9123
9124 static void
9125 make_cursor_line_fully_visible (w)
9126 struct window *w;
9127 {
9128 struct glyph_matrix *matrix;
9129 struct glyph_row *row;
9130 int window_height;
9131
9132 /* It's not always possible to find the cursor, e.g, when a window
9133 is full of overlay strings. Don't do anything in that case. */
9134 if (w->cursor.vpos < 0)
9135 return;
9136
9137 matrix = w->desired_matrix;
9138 row = MATRIX_ROW (matrix, w->cursor.vpos);
9139
9140 /* If the cursor row is not partially visible, there's nothing
9141 to do. */
9142 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
9143 return;
9144
9145 /* If the row the cursor is in is taller than the window's height,
9146 it's not clear what to do, so do nothing. */
9147 window_height = window_box_height (w);
9148 if (row->height >= window_height)
9149 return;
9150
9151 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
9152 {
9153 int dy = row->height - row->visible_height;
9154 w->vscroll = 0;
9155 w->cursor.y += dy;
9156 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9157 }
9158 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
9159 {
9160 int dy = - (row->height - row->visible_height);
9161 w->vscroll = dy;
9162 w->cursor.y += dy;
9163 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9164 }
9165
9166 /* When we change the cursor y-position of the selected window,
9167 change this_line_y as well so that the display optimization for
9168 the cursor line of the selected window in redisplay_internal uses
9169 the correct y-position. */
9170 if (w == XWINDOW (selected_window))
9171 this_line_y = w->cursor.y;
9172 }
9173
9174
9175 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
9176 non-zero means only WINDOW is redisplayed in redisplay_internal.
9177 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
9178 in redisplay_window to bring a partially visible line into view in
9179 the case that only the cursor has moved.
9180
9181 Value is
9182
9183 1 if scrolling succeeded
9184
9185 0 if scrolling didn't find point.
9186
9187 -1 if new fonts have been loaded so that we must interrupt
9188 redisplay, adjust glyph matrices, and try again. */
9189
9190 static int
9191 try_scrolling (window, just_this_one_p, scroll_conservatively,
9192 scroll_step, temp_scroll_step)
9193 Lisp_Object window;
9194 int just_this_one_p;
9195 int scroll_conservatively, scroll_step;
9196 int temp_scroll_step;
9197 {
9198 struct window *w = XWINDOW (window);
9199 struct frame *f = XFRAME (w->frame);
9200 struct text_pos scroll_margin_pos;
9201 struct text_pos pos;
9202 struct text_pos startp;
9203 struct it it;
9204 Lisp_Object window_end;
9205 int this_scroll_margin;
9206 int dy = 0;
9207 int scroll_max;
9208 int rc;
9209 int amount_to_scroll = 0;
9210 Lisp_Object aggressive;
9211 int height;
9212
9213 #if GLYPH_DEBUG
9214 debug_method_add (w, "try_scrolling");
9215 #endif
9216
9217 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9218
9219 /* Compute scroll margin height in pixels. We scroll when point is
9220 within this distance from the top or bottom of the window. */
9221 if (scroll_margin > 0)
9222 {
9223 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
9224 this_scroll_margin *= CANON_Y_UNIT (f);
9225 }
9226 else
9227 this_scroll_margin = 0;
9228
9229 /* Compute how much we should try to scroll maximally to bring point
9230 into view. */
9231 if (scroll_step || scroll_conservatively || temp_scroll_step)
9232 scroll_max = max (scroll_step,
9233 max (scroll_conservatively, temp_scroll_step));
9234 else if (NUMBERP (current_buffer->scroll_down_aggressively)
9235 || NUMBERP (current_buffer->scroll_up_aggressively))
9236 /* We're trying to scroll because of aggressive scrolling
9237 but no scroll_step is set. Choose an arbitrary one. Maybe
9238 there should be a variable for this. */
9239 scroll_max = 10;
9240 else
9241 scroll_max = 0;
9242 scroll_max *= CANON_Y_UNIT (f);
9243
9244 /* Decide whether we have to scroll down. Start at the window end
9245 and move this_scroll_margin up to find the position of the scroll
9246 margin. */
9247 window_end = Fwindow_end (window, Qt);
9248 CHARPOS (scroll_margin_pos) = XINT (window_end);
9249 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
9250 if (this_scroll_margin)
9251 {
9252 start_display (&it, w, scroll_margin_pos);
9253 move_it_vertically (&it, - this_scroll_margin);
9254 scroll_margin_pos = it.current.pos;
9255 }
9256
9257 if (PT >= CHARPOS (scroll_margin_pos))
9258 {
9259 int y0;
9260
9261 /* Point is in the scroll margin at the bottom of the window, or
9262 below. Compute a new window start that makes point visible. */
9263
9264 /* Compute the distance from the scroll margin to PT.
9265 Give up if the distance is greater than scroll_max. */
9266 start_display (&it, w, scroll_margin_pos);
9267 y0 = it.current_y;
9268 move_it_to (&it, PT, 0, it.last_visible_y, -1,
9269 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9270
9271 /* With a scroll_margin of 0, scroll_margin_pos is at the window
9272 end, which is one line below the window. The iterator's
9273 current_y will be same as y0 in that case, but we have to
9274 scroll a line to make PT visible. That's the reason why 1 is
9275 added below. */
9276 dy = 1 + it.current_y - y0;
9277
9278 if (dy > scroll_max)
9279 return 0;
9280
9281 /* Move the window start down. If scrolling conservatively,
9282 move it just enough down to make point visible. If
9283 scroll_step is set, move it down by scroll_step. */
9284 start_display (&it, w, startp);
9285
9286 if (scroll_conservatively)
9287 amount_to_scroll
9288 = max (max (dy, CANON_Y_UNIT (f)),
9289 CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
9290 else if (scroll_step || temp_scroll_step)
9291 amount_to_scroll = scroll_max;
9292 else
9293 {
9294 aggressive = current_buffer->scroll_down_aggressively;
9295 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
9296 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
9297 if (NUMBERP (aggressive))
9298 amount_to_scroll = XFLOATINT (aggressive) * height;
9299 }
9300
9301 if (amount_to_scroll <= 0)
9302 return 0;
9303
9304 move_it_vertically (&it, amount_to_scroll);
9305 startp = it.current.pos;
9306 }
9307 else
9308 {
9309 /* See if point is inside the scroll margin at the top of the
9310 window. */
9311 scroll_margin_pos = startp;
9312 if (this_scroll_margin)
9313 {
9314 start_display (&it, w, startp);
9315 move_it_vertically (&it, this_scroll_margin);
9316 scroll_margin_pos = it.current.pos;
9317 }
9318
9319 if (PT < CHARPOS (scroll_margin_pos))
9320 {
9321 /* Point is in the scroll margin at the top of the window or
9322 above what is displayed in the window. */
9323 int y0;
9324
9325 /* Compute the vertical distance from PT to the scroll
9326 margin position. Give up if distance is greater than
9327 scroll_max. */
9328 SET_TEXT_POS (pos, PT, PT_BYTE);
9329 start_display (&it, w, pos);
9330 y0 = it.current_y;
9331 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
9332 it.last_visible_y, -1,
9333 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9334 dy = it.current_y - y0;
9335 if (dy > scroll_max)
9336 return 0;
9337
9338 /* Compute new window start. */
9339 start_display (&it, w, startp);
9340
9341 if (scroll_conservatively)
9342 amount_to_scroll =
9343 max (dy, CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
9344 else if (scroll_step || temp_scroll_step)
9345 amount_to_scroll = scroll_max;
9346 else
9347 {
9348 aggressive = current_buffer->scroll_up_aggressively;
9349 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
9350 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
9351 if (NUMBERP (aggressive))
9352 amount_to_scroll = XFLOATINT (aggressive) * height;
9353 }
9354
9355 if (amount_to_scroll <= 0)
9356 return 0;
9357
9358 move_it_vertically (&it, - amount_to_scroll);
9359 startp = it.current.pos;
9360 }
9361 }
9362
9363 /* Run window scroll functions. */
9364 startp = run_window_scroll_functions (window, startp);
9365
9366 /* Display the window. Give up if new fonts are loaded, or if point
9367 doesn't appear. */
9368 if (!try_window (window, startp))
9369 rc = -1;
9370 else if (w->cursor.vpos < 0)
9371 {
9372 clear_glyph_matrix (w->desired_matrix);
9373 rc = 0;
9374 }
9375 else
9376 {
9377 /* Maybe forget recorded base line for line number display. */
9378 if (!just_this_one_p
9379 || current_buffer->clip_changed
9380 || BEG_UNCHANGED < CHARPOS (startp))
9381 w->base_line_number = Qnil;
9382
9383 /* If cursor ends up on a partially visible line, shift display
9384 lines up or down. */
9385 make_cursor_line_fully_visible (w);
9386 rc = 1;
9387 }
9388
9389 return rc;
9390 }
9391
9392
9393 /* Compute a suitable window start for window W if display of W starts
9394 on a continuation line. Value is non-zero if a new window start
9395 was computed.
9396
9397 The new window start will be computed, based on W's width, starting
9398 from the start of the continued line. It is the start of the
9399 screen line with the minimum distance from the old start W->start. */
9400
9401 static int
9402 compute_window_start_on_continuation_line (w)
9403 struct window *w;
9404 {
9405 struct text_pos pos, start_pos;
9406 int window_start_changed_p = 0;
9407
9408 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
9409
9410 /* If window start is on a continuation line... Window start may be
9411 < BEGV in case there's invisible text at the start of the
9412 buffer (M-x rmail, for example). */
9413 if (CHARPOS (start_pos) > BEGV
9414 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
9415 {
9416 struct it it;
9417 struct glyph_row *row;
9418
9419 /* Handle the case that the window start is out of range. */
9420 if (CHARPOS (start_pos) < BEGV)
9421 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
9422 else if (CHARPOS (start_pos) > ZV)
9423 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
9424
9425 /* Find the start of the continued line. This should be fast
9426 because scan_buffer is fast (newline cache). */
9427 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
9428 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
9429 row, DEFAULT_FACE_ID);
9430 reseat_at_previous_visible_line_start (&it);
9431
9432 /* If the line start is "too far" away from the window start,
9433 say it takes too much time to compute a new window start. */
9434 if (CHARPOS (start_pos) - IT_CHARPOS (it)
9435 < XFASTINT (w->height) * XFASTINT (w->width))
9436 {
9437 int min_distance, distance;
9438
9439 /* Move forward by display lines to find the new window
9440 start. If window width was enlarged, the new start can
9441 be expected to be > the old start. If window width was
9442 decreased, the new window start will be < the old start.
9443 So, we're looking for the display line start with the
9444 minimum distance from the old window start. */
9445 pos = it.current.pos;
9446 min_distance = INFINITY;
9447 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
9448 distance < min_distance)
9449 {
9450 min_distance = distance;
9451 pos = it.current.pos;
9452 move_it_by_lines (&it, 1, 0);
9453 }
9454
9455 /* Set the window start there. */
9456 SET_MARKER_FROM_TEXT_POS (w->start, pos);
9457 window_start_changed_p = 1;
9458 }
9459 }
9460
9461 return window_start_changed_p;
9462 }
9463
9464
9465 /* Try cursor movement in case text has not changes in window WINDOW,
9466 with window start STARTP. Value is
9467
9468 1 if successful
9469
9470 0 if this method cannot be used
9471
9472 -1 if we know we have to scroll the display. *SCROLL_STEP is
9473 set to 1, under certain circumstances, if we want to scroll as
9474 if scroll-step were set to 1. See the code. */
9475
9476 static int
9477 try_cursor_movement (window, startp, scroll_step)
9478 Lisp_Object window;
9479 struct text_pos startp;
9480 int *scroll_step;
9481 {
9482 struct window *w = XWINDOW (window);
9483 struct frame *f = XFRAME (w->frame);
9484 int rc = 0;
9485
9486 /* Handle case where text has not changed, only point, and it has
9487 not moved off the frame. */
9488 if (/* Point may be in this window. */
9489 PT >= CHARPOS (startp)
9490 /* Selective display hasn't changed. */
9491 && !current_buffer->clip_changed
9492 /* Function force-mode-line-update is used to force a thorough
9493 redisplay. It sets either windows_or_buffers_changed or
9494 update_mode_lines. So don't take a shortcut here for these
9495 cases. */
9496 && !update_mode_lines
9497 && !windows_or_buffers_changed
9498 /* Can't use this case if highlighting a region. When a
9499 region exists, cursor movement has to do more than just
9500 set the cursor. */
9501 && !(!NILP (Vtransient_mark_mode)
9502 && !NILP (current_buffer->mark_active))
9503 && NILP (w->region_showing)
9504 && NILP (Vshow_trailing_whitespace)
9505 /* Right after splitting windows, last_point may be nil. */
9506 && INTEGERP (w->last_point)
9507 /* This code is not used for mini-buffer for the sake of the case
9508 of redisplaying to replace an echo area message; since in
9509 that case the mini-buffer contents per se are usually
9510 unchanged. This code is of no real use in the mini-buffer
9511 since the handling of this_line_start_pos, etc., in redisplay
9512 handles the same cases. */
9513 && !EQ (window, minibuf_window)
9514 /* When splitting windows or for new windows, it happens that
9515 redisplay is called with a nil window_end_vpos or one being
9516 larger than the window. This should really be fixed in
9517 window.c. I don't have this on my list, now, so we do
9518 approximately the same as the old redisplay code. --gerd. */
9519 && INTEGERP (w->window_end_vpos)
9520 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
9521 && (FRAME_WINDOW_P (f)
9522 || !MARKERP (Voverlay_arrow_position)
9523 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
9524 {
9525 int this_scroll_margin;
9526 struct glyph_row *row;
9527
9528 #if GLYPH_DEBUG
9529 debug_method_add (w, "cursor movement");
9530 #endif
9531
9532 /* Scroll if point within this distance from the top or bottom
9533 of the window. This is a pixel value. */
9534 this_scroll_margin = max (0, scroll_margin);
9535 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
9536 this_scroll_margin *= CANON_Y_UNIT (f);
9537
9538 /* Start with the row the cursor was displayed during the last
9539 not paused redisplay. Give up if that row is not valid. */
9540 if (w->last_cursor.vpos < 0
9541 || w->last_cursor.vpos >= w->current_matrix->nrows)
9542 rc = -1;
9543 else
9544 {
9545 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
9546 if (row->mode_line_p)
9547 ++row;
9548 if (!row->enabled_p)
9549 rc = -1;
9550 }
9551
9552 if (rc == 0)
9553 {
9554 int scroll_p = 0;
9555 int last_y = window_text_bottom_y (w) - this_scroll_margin;
9556
9557 if (PT > XFASTINT (w->last_point))
9558 {
9559 /* Point has moved forward. */
9560 while (MATRIX_ROW_END_CHARPOS (row) < PT
9561 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
9562 {
9563 xassert (row->enabled_p);
9564 ++row;
9565 }
9566
9567 /* The end position of a row equals the start position
9568 of the next row. If PT is there, we would rather
9569 display it in the next line. */
9570 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9571 && MATRIX_ROW_END_CHARPOS (row) == PT
9572 && !cursor_row_p (w, row))
9573 ++row;
9574
9575 /* If within the scroll margin, scroll. Note that
9576 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
9577 the next line would be drawn, and that
9578 this_scroll_margin can be zero. */
9579 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
9580 || PT > MATRIX_ROW_END_CHARPOS (row)
9581 /* Line is completely visible last line in window
9582 and PT is to be set in the next line. */
9583 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
9584 && PT == MATRIX_ROW_END_CHARPOS (row)
9585 && !row->ends_at_zv_p
9586 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
9587 scroll_p = 1;
9588 }
9589 else if (PT < XFASTINT (w->last_point))
9590 {
9591 /* Cursor has to be moved backward. Note that PT >=
9592 CHARPOS (startp) because of the outer
9593 if-statement. */
9594 while (!row->mode_line_p
9595 && (MATRIX_ROW_START_CHARPOS (row) > PT
9596 || (MATRIX_ROW_START_CHARPOS (row) == PT
9597 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
9598 && (row->y > this_scroll_margin
9599 || CHARPOS (startp) == BEGV))
9600 {
9601 xassert (row->enabled_p);
9602 --row;
9603 }
9604
9605 /* Consider the following case: Window starts at BEGV,
9606 there is invisible, intangible text at BEGV, so that
9607 display starts at some point START > BEGV. It can
9608 happen that we are called with PT somewhere between
9609 BEGV and START. Try to handle that case. */
9610 if (row < w->current_matrix->rows
9611 || row->mode_line_p)
9612 {
9613 row = w->current_matrix->rows;
9614 if (row->mode_line_p)
9615 ++row;
9616 }
9617
9618 /* Due to newlines in overlay strings, we may have to
9619 skip forward over overlay strings. */
9620 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9621 && MATRIX_ROW_END_CHARPOS (row) == PT
9622 && !cursor_row_p (w, row))
9623 ++row;
9624
9625 /* If within the scroll margin, scroll. */
9626 if (row->y < this_scroll_margin
9627 && CHARPOS (startp) != BEGV)
9628 scroll_p = 1;
9629 }
9630
9631 if (PT < MATRIX_ROW_START_CHARPOS (row)
9632 || PT > MATRIX_ROW_END_CHARPOS (row))
9633 {
9634 /* if PT is not in the glyph row, give up. */
9635 rc = -1;
9636 }
9637 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
9638 {
9639 if (PT == MATRIX_ROW_END_CHARPOS (row)
9640 && !row->ends_at_zv_p
9641 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
9642 rc = -1;
9643 else if (row->height > window_box_height (w))
9644 {
9645 /* If we end up in a partially visible line, let's
9646 make it fully visible, except when it's taller
9647 than the window, in which case we can't do much
9648 about it. */
9649 *scroll_step = 1;
9650 rc = -1;
9651 }
9652 else
9653 {
9654 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9655 try_window (window, startp);
9656 make_cursor_line_fully_visible (w);
9657 rc = 1;
9658 }
9659 }
9660 else if (scroll_p)
9661 rc = -1;
9662 else
9663 {
9664 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9665 rc = 1;
9666 }
9667 }
9668 }
9669
9670 return rc;
9671 }
9672
9673
9674 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
9675 selected_window is redisplayed. */
9676
9677 static void
9678 redisplay_window (window, just_this_one_p)
9679 Lisp_Object window;
9680 int just_this_one_p;
9681 {
9682 struct window *w = XWINDOW (window);
9683 struct frame *f = XFRAME (w->frame);
9684 struct buffer *buffer = XBUFFER (w->buffer);
9685 struct buffer *old = current_buffer;
9686 struct text_pos lpoint, opoint, startp;
9687 int update_mode_line;
9688 int tem;
9689 struct it it;
9690 /* Record it now because it's overwritten. */
9691 int current_matrix_up_to_date_p = 0;
9692 int temp_scroll_step = 0;
9693 int count = BINDING_STACK_SIZE ();
9694 int rc;
9695
9696 SET_TEXT_POS (lpoint, PT, PT_BYTE);
9697 opoint = lpoint;
9698
9699 /* W must be a leaf window here. */
9700 xassert (!NILP (w->buffer));
9701 #if GLYPH_DEBUG
9702 *w->desired_matrix->method = 0;
9703 #endif
9704
9705 specbind (Qinhibit_point_motion_hooks, Qt);
9706
9707 reconsider_clip_changes (w, buffer);
9708
9709 /* Has the mode line to be updated? */
9710 update_mode_line = (!NILP (w->update_mode_line)
9711 || update_mode_lines
9712 || buffer->clip_changed);
9713
9714 if (MINI_WINDOW_P (w))
9715 {
9716 if (w == XWINDOW (echo_area_window)
9717 && !NILP (echo_area_buffer[0]))
9718 {
9719 if (update_mode_line)
9720 /* We may have to update a tty frame's menu bar or a
9721 tool-bar. Example `M-x C-h C-h C-g'. */
9722 goto finish_menu_bars;
9723 else
9724 /* We've already displayed the echo area glyphs in this window. */
9725 goto finish_scroll_bars;
9726 }
9727 else if (w != XWINDOW (minibuf_window))
9728 {
9729 /* W is a mini-buffer window, but it's not the currently
9730 active one, so clear it. */
9731 int yb = window_text_bottom_y (w);
9732 struct glyph_row *row;
9733 int y;
9734
9735 for (y = 0, row = w->desired_matrix->rows;
9736 y < yb;
9737 y += row->height, ++row)
9738 blank_row (w, row, y);
9739 goto finish_scroll_bars;
9740 }
9741 }
9742
9743 /* Otherwise set up data on this window; select its buffer and point
9744 value. */
9745 /* Really select the buffer, for the sake of buffer-local
9746 variables. */
9747 set_buffer_internal_1 (XBUFFER (w->buffer));
9748 SET_TEXT_POS (opoint, PT, PT_BYTE);
9749
9750 current_matrix_up_to_date_p
9751 = (!NILP (w->window_end_valid)
9752 && !current_buffer->clip_changed
9753 && XFASTINT (w->last_modified) >= MODIFF
9754 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
9755
9756 /* When windows_or_buffers_changed is non-zero, we can't rely on
9757 the window end being valid, so set it to nil there. */
9758 if (windows_or_buffers_changed)
9759 {
9760 /* If window starts on a continuation line, maybe adjust the
9761 window start in case the window's width changed. */
9762 if (XMARKER (w->start)->buffer == current_buffer)
9763 compute_window_start_on_continuation_line (w);
9764
9765 w->window_end_valid = Qnil;
9766 }
9767
9768 /* Some sanity checks. */
9769 CHECK_WINDOW_END (w);
9770 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
9771 abort ();
9772 if (BYTEPOS (opoint) < CHARPOS (opoint))
9773 abort ();
9774
9775 /* If %c is in mode line, update it if needed. */
9776 if (!NILP (w->column_number_displayed)
9777 /* This alternative quickly identifies a common case
9778 where no change is needed. */
9779 && !(PT == XFASTINT (w->last_point)
9780 && XFASTINT (w->last_modified) >= MODIFF
9781 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
9782 && XFASTINT (w->column_number_displayed) != current_column ())
9783 update_mode_line = 1;
9784
9785 /* Count number of windows showing the selected buffer. An indirect
9786 buffer counts as its base buffer. */
9787 if (!just_this_one_p)
9788 {
9789 struct buffer *current_base, *window_base;
9790 current_base = current_buffer;
9791 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
9792 if (current_base->base_buffer)
9793 current_base = current_base->base_buffer;
9794 if (window_base->base_buffer)
9795 window_base = window_base->base_buffer;
9796 if (current_base == window_base)
9797 buffer_shared++;
9798 }
9799
9800 /* Point refers normally to the selected window. For any other
9801 window, set up appropriate value. */
9802 if (!EQ (window, selected_window))
9803 {
9804 int new_pt = XMARKER (w->pointm)->charpos;
9805 int new_pt_byte = marker_byte_position (w->pointm);
9806 if (new_pt < BEGV)
9807 {
9808 new_pt = BEGV;
9809 new_pt_byte = BEGV_BYTE;
9810 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
9811 }
9812 else if (new_pt > (ZV - 1))
9813 {
9814 new_pt = ZV;
9815 new_pt_byte = ZV_BYTE;
9816 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
9817 }
9818
9819 /* We don't use SET_PT so that the point-motion hooks don't run. */
9820 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
9821 }
9822
9823 /* If any of the character widths specified in the display table
9824 have changed, invalidate the width run cache. It's true that
9825 this may be a bit late to catch such changes, but the rest of
9826 redisplay goes (non-fatally) haywire when the display table is
9827 changed, so why should we worry about doing any better? */
9828 if (current_buffer->width_run_cache)
9829 {
9830 struct Lisp_Char_Table *disptab = buffer_display_table ();
9831
9832 if (! disptab_matches_widthtab (disptab,
9833 XVECTOR (current_buffer->width_table)))
9834 {
9835 invalidate_region_cache (current_buffer,
9836 current_buffer->width_run_cache,
9837 BEG, Z);
9838 recompute_width_table (current_buffer, disptab);
9839 }
9840 }
9841
9842 /* If window-start is screwed up, choose a new one. */
9843 if (XMARKER (w->start)->buffer != current_buffer)
9844 goto recenter;
9845
9846 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9847
9848 /* If someone specified a new starting point but did not insist,
9849 check whether it can be used. */
9850 if (!NILP (w->optional_new_start)
9851 && CHARPOS (startp) >= BEGV
9852 && CHARPOS (startp) <= ZV)
9853 {
9854 w->optional_new_start = Qnil;
9855 start_display (&it, w, startp);
9856 move_it_to (&it, PT, 0, it.last_visible_y, -1,
9857 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9858 if (IT_CHARPOS (it) == PT)
9859 w->force_start = Qt;
9860 }
9861
9862 /* Handle case where place to start displaying has been specified,
9863 unless the specified location is outside the accessible range. */
9864 if (!NILP (w->force_start)
9865 || w->frozen_window_start_p)
9866 {
9867 w->force_start = Qnil;
9868 w->vscroll = 0;
9869 w->window_end_valid = Qnil;
9870
9871 /* Forget any recorded base line for line number display. */
9872 if (!current_matrix_up_to_date_p
9873 || current_buffer->clip_changed)
9874 w->base_line_number = Qnil;
9875
9876 /* Redisplay the mode line. Select the buffer properly for that.
9877 Also, run the hook window-scroll-functions
9878 because we have scrolled. */
9879 /* Note, we do this after clearing force_start because
9880 if there's an error, it is better to forget about force_start
9881 than to get into an infinite loop calling the hook functions
9882 and having them get more errors. */
9883 if (!update_mode_line
9884 || ! NILP (Vwindow_scroll_functions))
9885 {
9886 update_mode_line = 1;
9887 w->update_mode_line = Qt;
9888 startp = run_window_scroll_functions (window, startp);
9889 }
9890
9891 w->last_modified = make_number (0);
9892 w->last_overlay_modified = make_number (0);
9893 if (CHARPOS (startp) < BEGV)
9894 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
9895 else if (CHARPOS (startp) > ZV)
9896 SET_TEXT_POS (startp, ZV, ZV_BYTE);
9897
9898 /* Redisplay, then check if cursor has been set during the
9899 redisplay. Give up if new fonts were loaded. */
9900 if (!try_window (window, startp))
9901 {
9902 w->force_start = Qt;
9903 clear_glyph_matrix (w->desired_matrix);
9904 goto finish_scroll_bars;
9905 }
9906
9907 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
9908 {
9909 /* If point does not appear, try to move point so it does
9910 appear. The desired matrix has been built above, so we
9911 can use it here. */
9912 int window_height;
9913 struct glyph_row *row;
9914
9915 window_height = window_box_height (w) / 2;
9916 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
9917 while (MATRIX_ROW_BOTTOM_Y (row) < window_height)
9918 ++row;
9919
9920 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
9921 MATRIX_ROW_START_BYTEPOS (row));
9922
9923 if (w != XWINDOW (selected_window))
9924 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
9925 else if (current_buffer == old)
9926 SET_TEXT_POS (lpoint, PT, PT_BYTE);
9927
9928 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
9929
9930 /* If we are highlighting the region, then we just changed
9931 the region, so redisplay to show it. */
9932 if (!NILP (Vtransient_mark_mode)
9933 && !NILP (current_buffer->mark_active))
9934 {
9935 clear_glyph_matrix (w->desired_matrix);
9936 if (!try_window (window, startp))
9937 goto finish_scroll_bars;
9938 }
9939 }
9940
9941 make_cursor_line_fully_visible (w);
9942 #if GLYPH_DEBUG
9943 debug_method_add (w, "forced window start");
9944 #endif
9945 goto done;
9946 }
9947
9948 /* Handle case where text has not changed, only point, and it has
9949 not moved off the frame. */
9950 if (current_matrix_up_to_date_p
9951 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
9952 rc != 0))
9953 {
9954 if (rc == -1)
9955 goto try_to_scroll;
9956 else
9957 goto done;
9958 }
9959 /* If current starting point was originally the beginning of a line
9960 but no longer is, find a new starting point. */
9961 else if (!NILP (w->start_at_line_beg)
9962 && !(CHARPOS (startp) <= BEGV
9963 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
9964 {
9965 #if GLYPH_DEBUG
9966 debug_method_add (w, "recenter 1");
9967 #endif
9968 goto recenter;
9969 }
9970
9971 /* Try scrolling with try_window_id. */
9972 else if (/* Windows and buffers haven't changed. */
9973 !windows_or_buffers_changed
9974 /* Window must be either use window-based redisplay or
9975 be full width. */
9976 && (FRAME_WINDOW_P (f)
9977 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)))
9978 && !MINI_WINDOW_P (w)
9979 /* Point is not known NOT to appear in window. */
9980 && PT >= CHARPOS (startp)
9981 && XFASTINT (w->last_modified)
9982 /* Window is not hscrolled. */
9983 && XFASTINT (w->hscroll) == 0
9984 /* Selective display has not changed. */
9985 && !current_buffer->clip_changed
9986 /* Current matrix is up to date. */
9987 && !NILP (w->window_end_valid)
9988 /* Can't use this case if highlighting a region because
9989 a cursor movement will do more than just set the cursor. */
9990 && !(!NILP (Vtransient_mark_mode)
9991 && !NILP (current_buffer->mark_active))
9992 && NILP (w->region_showing)
9993 && NILP (Vshow_trailing_whitespace)
9994 /* Overlay arrow position and string not changed. */
9995 && EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
9996 && EQ (last_arrow_string, Voverlay_arrow_string)
9997 /* Value is > 0 if update has been done, it is -1 if we
9998 know that the same window start will not work. It is 0
9999 if unsuccessful for some other reason. */
10000 && (tem = try_window_id (w)) != 0)
10001 {
10002 #if GLYPH_DEBUG
10003 debug_method_add (w, "try_window_id %d", tem);
10004 #endif
10005
10006 if (fonts_changed_p)
10007 goto finish_scroll_bars;
10008 if (tem > 0)
10009 goto done;
10010
10011 /* Otherwise try_window_id has returned -1 which means that we
10012 don't want the alternative below this comment to execute. */
10013 }
10014 else if (CHARPOS (startp) >= BEGV
10015 && CHARPOS (startp) <= ZV
10016 && PT >= CHARPOS (startp)
10017 && (CHARPOS (startp) < ZV
10018 /* Avoid starting at end of buffer. */
10019 || CHARPOS (startp) == BEGV
10020 || (XFASTINT (w->last_modified) >= MODIFF
10021 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
10022 {
10023 #if GLYPH_DEBUG
10024 debug_method_add (w, "same window start");
10025 #endif
10026
10027 /* Try to redisplay starting at same place as before.
10028 If point has not moved off frame, accept the results. */
10029 if (!current_matrix_up_to_date_p
10030 /* Don't use try_window_reusing_current_matrix in this case
10031 because a window scroll function can have changed the
10032 buffer. */
10033 || !NILP (Vwindow_scroll_functions)
10034 || MINI_WINDOW_P (w)
10035 || !try_window_reusing_current_matrix (w))
10036 {
10037 IF_DEBUG (debug_method_add (w, "1"));
10038 try_window (window, startp);
10039 }
10040
10041 if (fonts_changed_p)
10042 goto finish_scroll_bars;
10043
10044 if (w->cursor.vpos >= 0)
10045 {
10046 if (!just_this_one_p
10047 || current_buffer->clip_changed
10048 || BEG_UNCHANGED < CHARPOS (startp))
10049 /* Forget any recorded base line for line number display. */
10050 w->base_line_number = Qnil;
10051
10052 make_cursor_line_fully_visible (w);
10053 goto done;
10054 }
10055 else
10056 clear_glyph_matrix (w->desired_matrix);
10057 }
10058
10059 try_to_scroll:
10060
10061 w->last_modified = make_number (0);
10062 w->last_overlay_modified = make_number (0);
10063
10064 /* Redisplay the mode line. Select the buffer properly for that. */
10065 if (!update_mode_line)
10066 {
10067 update_mode_line = 1;
10068 w->update_mode_line = Qt;
10069 }
10070
10071 /* Try to scroll by specified few lines. */
10072 if ((scroll_conservatively
10073 || scroll_step
10074 || temp_scroll_step
10075 || NUMBERP (current_buffer->scroll_up_aggressively)
10076 || NUMBERP (current_buffer->scroll_down_aggressively))
10077 && !current_buffer->clip_changed
10078 && CHARPOS (startp) >= BEGV
10079 && CHARPOS (startp) <= ZV)
10080 {
10081 /* The function returns -1 if new fonts were loaded, 1 if
10082 successful, 0 if not successful. */
10083 int rc = try_scrolling (window, just_this_one_p,
10084 scroll_conservatively,
10085 scroll_step,
10086 temp_scroll_step);
10087 if (rc > 0)
10088 goto done;
10089 else if (rc < 0)
10090 goto finish_scroll_bars;
10091 }
10092
10093 /* Finally, just choose place to start which centers point */
10094
10095 recenter:
10096
10097 #if GLYPH_DEBUG
10098 debug_method_add (w, "recenter");
10099 #endif
10100
10101 /* w->vscroll = 0; */
10102
10103 /* Forget any previously recorded base line for line number display. */
10104 if (!current_matrix_up_to_date_p
10105 || current_buffer->clip_changed)
10106 w->base_line_number = Qnil;
10107
10108 /* Move backward half the height of the window. */
10109 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10110 it.current_y = it.last_visible_y;
10111 move_it_vertically_backward (&it, it.last_visible_y / 2);
10112 xassert (IT_CHARPOS (it) >= BEGV);
10113
10114 /* The function move_it_vertically_backward may move over more
10115 than the specified y-distance. If it->w is small, e.g. a
10116 mini-buffer window, we may end up in front of the window's
10117 display area. Start displaying at the start of the line
10118 containing PT in this case. */
10119 if (it.current_y <= 0)
10120 {
10121 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10122 move_it_vertically (&it, 0);
10123 xassert (IT_CHARPOS (it) <= PT);
10124 it.current_y = 0;
10125 }
10126
10127 it.current_x = it.hpos = 0;
10128
10129 /* Set startp here explicitly in case that helps avoid an infinite loop
10130 in case the window-scroll-functions functions get errors. */
10131 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
10132
10133 /* Run scroll hooks. */
10134 startp = run_window_scroll_functions (window, it.current.pos);
10135
10136 /* Redisplay the window. */
10137 if (!current_matrix_up_to_date_p
10138 || windows_or_buffers_changed
10139 /* Don't use try_window_reusing_current_matrix in this case
10140 because it can have changed the buffer. */
10141 || !NILP (Vwindow_scroll_functions)
10142 || !just_this_one_p
10143 || MINI_WINDOW_P (w)
10144 || !try_window_reusing_current_matrix (w))
10145 try_window (window, startp);
10146
10147 /* If new fonts have been loaded (due to fontsets), give up. We
10148 have to start a new redisplay since we need to re-adjust glyph
10149 matrices. */
10150 if (fonts_changed_p)
10151 goto finish_scroll_bars;
10152
10153 /* If cursor did not appear assume that the middle of the window is
10154 in the first line of the window. Do it again with the next line.
10155 (Imagine a window of height 100, displaying two lines of height
10156 60. Moving back 50 from it->last_visible_y will end in the first
10157 line.) */
10158 if (w->cursor.vpos < 0)
10159 {
10160 if (!NILP (w->window_end_valid)
10161 && PT >= Z - XFASTINT (w->window_end_pos))
10162 {
10163 clear_glyph_matrix (w->desired_matrix);
10164 move_it_by_lines (&it, 1, 0);
10165 try_window (window, it.current.pos);
10166 }
10167 else if (PT < IT_CHARPOS (it))
10168 {
10169 clear_glyph_matrix (w->desired_matrix);
10170 move_it_by_lines (&it, -1, 0);
10171 try_window (window, it.current.pos);
10172 }
10173 else
10174 {
10175 /* Not much we can do about it. */
10176 }
10177 }
10178
10179 /* Consider the following case: Window starts at BEGV, there is
10180 invisible, intangible text at BEGV, so that display starts at
10181 some point START > BEGV. It can happen that we are called with
10182 PT somewhere between BEGV and START. Try to handle that case. */
10183 if (w->cursor.vpos < 0)
10184 {
10185 struct glyph_row *row = w->current_matrix->rows;
10186 if (row->mode_line_p)
10187 ++row;
10188 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10189 }
10190
10191 make_cursor_line_fully_visible (w);
10192
10193 done:
10194
10195 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10196 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
10197 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
10198 ? Qt : Qnil);
10199
10200 /* Display the mode line, if we must. */
10201 if ((update_mode_line
10202 /* If window not full width, must redo its mode line
10203 if (a) the window to its side is being redone and
10204 (b) we do a frame-based redisplay. This is a consequence
10205 of how inverted lines are drawn in frame-based redisplay. */
10206 || (!just_this_one_p
10207 && !FRAME_WINDOW_P (f)
10208 && !WINDOW_FULL_WIDTH_P (w))
10209 /* Line number to display. */
10210 || INTEGERP (w->base_line_pos)
10211 /* Column number is displayed and different from the one displayed. */
10212 || (!NILP (w->column_number_displayed)
10213 && XFASTINT (w->column_number_displayed) != current_column ()))
10214 /* This means that the window has a mode line. */
10215 && (WINDOW_WANTS_MODELINE_P (w)
10216 || WINDOW_WANTS_HEADER_LINE_P (w)))
10217 {
10218 Lisp_Object old_selected_frame;
10219
10220 old_selected_frame = selected_frame;
10221
10222 XSETFRAME (selected_frame, f);
10223 display_mode_lines (w);
10224 selected_frame = old_selected_frame;
10225
10226 /* If mode line height has changed, arrange for a thorough
10227 immediate redisplay using the correct mode line height. */
10228 if (WINDOW_WANTS_MODELINE_P (w)
10229 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
10230 {
10231 fonts_changed_p = 1;
10232 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
10233 = DESIRED_MODE_LINE_HEIGHT (w);
10234 }
10235
10236 /* If top line height has changed, arrange for a thorough
10237 immediate redisplay using the correct mode line height. */
10238 if (WINDOW_WANTS_HEADER_LINE_P (w)
10239 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
10240 {
10241 fonts_changed_p = 1;
10242 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
10243 = DESIRED_HEADER_LINE_HEIGHT (w);
10244 }
10245
10246 if (fonts_changed_p)
10247 goto finish_scroll_bars;
10248 }
10249
10250 if (!line_number_displayed
10251 && !BUFFERP (w->base_line_pos))
10252 {
10253 w->base_line_pos = Qnil;
10254 w->base_line_number = Qnil;
10255 }
10256
10257 finish_menu_bars:
10258
10259 /* When we reach a frame's selected window, redo the frame's menu bar. */
10260 if (update_mode_line
10261 && EQ (FRAME_SELECTED_WINDOW (f), window))
10262 {
10263 int redisplay_menu_p = 0;
10264
10265 if (FRAME_WINDOW_P (f))
10266 {
10267 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
10268 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
10269 #else
10270 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10271 #endif
10272 }
10273 else
10274 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10275
10276 if (redisplay_menu_p)
10277 display_menu_bar (w);
10278
10279 #ifdef HAVE_WINDOW_SYSTEM
10280 if (WINDOWP (f->tool_bar_window)
10281 && (FRAME_TOOL_BAR_LINES (f) > 0
10282 || auto_resize_tool_bars_p))
10283 redisplay_tool_bar (f);
10284 #endif
10285 }
10286
10287 finish_scroll_bars:
10288
10289 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
10290 {
10291 int start, end, whole;
10292
10293 /* Calculate the start and end positions for the current window.
10294 At some point, it would be nice to choose between scrollbars
10295 which reflect the whole buffer size, with special markers
10296 indicating narrowing, and scrollbars which reflect only the
10297 visible region.
10298
10299 Note that mini-buffers sometimes aren't displaying any text. */
10300 if (!MINI_WINDOW_P (w)
10301 || (w == XWINDOW (minibuf_window)
10302 && NILP (echo_area_buffer[0])))
10303 {
10304 whole = ZV - BEGV;
10305 start = marker_position (w->start) - BEGV;
10306 /* I don't think this is guaranteed to be right. For the
10307 moment, we'll pretend it is. */
10308 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
10309
10310 if (end < start)
10311 end = start;
10312 if (whole < (end - start))
10313 whole = end - start;
10314 }
10315 else
10316 start = end = whole = 0;
10317
10318 /* Indicate what this scroll bar ought to be displaying now. */
10319 set_vertical_scroll_bar_hook (w, end - start, whole, start);
10320
10321 /* Note that we actually used the scroll bar attached to this
10322 window, so it shouldn't be deleted at the end of redisplay. */
10323 redeem_scroll_bar_hook (w);
10324 }
10325
10326 /* Restore current_buffer and value of point in it. */
10327 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
10328 set_buffer_internal_1 (old);
10329 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
10330
10331 unbind_to (count, Qnil);
10332 }
10333
10334
10335 /* Build the complete desired matrix of WINDOW with a window start
10336 buffer position POS. Value is non-zero if successful. It is zero
10337 if fonts were loaded during redisplay which makes re-adjusting
10338 glyph matrices necessary. */
10339
10340 int
10341 try_window (window, pos)
10342 Lisp_Object window;
10343 struct text_pos pos;
10344 {
10345 struct window *w = XWINDOW (window);
10346 struct it it;
10347 struct glyph_row *last_text_row = NULL;
10348
10349 /* Make POS the new window start. */
10350 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
10351
10352 /* Mark cursor position as unknown. No overlay arrow seen. */
10353 w->cursor.vpos = -1;
10354 overlay_arrow_seen = 0;
10355
10356 /* Initialize iterator and info to start at POS. */
10357 start_display (&it, w, pos);
10358
10359 /* Display all lines of W. */
10360 while (it.current_y < it.last_visible_y)
10361 {
10362 if (display_line (&it))
10363 last_text_row = it.glyph_row - 1;
10364 if (fonts_changed_p)
10365 return 0;
10366 }
10367
10368 /* If bottom moved off end of frame, change mode line percentage. */
10369 if (XFASTINT (w->window_end_pos) <= 0
10370 && Z != IT_CHARPOS (it))
10371 w->update_mode_line = Qt;
10372
10373 /* Set window_end_pos to the offset of the last character displayed
10374 on the window from the end of current_buffer. Set
10375 window_end_vpos to its row number. */
10376 if (last_text_row)
10377 {
10378 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
10379 w->window_end_bytepos
10380 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10381 w->window_end_pos
10382 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10383 w->window_end_vpos
10384 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10385 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
10386 ->displays_text_p);
10387 }
10388 else
10389 {
10390 w->window_end_bytepos = 0;
10391 w->window_end_pos = w->window_end_vpos = make_number (0);
10392 }
10393
10394 /* But that is not valid info until redisplay finishes. */
10395 w->window_end_valid = Qnil;
10396 return 1;
10397 }
10398
10399
10400 \f
10401 /************************************************************************
10402 Window redisplay reusing current matrix when buffer has not changed
10403 ************************************************************************/
10404
10405 /* Try redisplay of window W showing an unchanged buffer with a
10406 different window start than the last time it was displayed by
10407 reusing its current matrix. Value is non-zero if successful.
10408 W->start is the new window start. */
10409
10410 static int
10411 try_window_reusing_current_matrix (w)
10412 struct window *w;
10413 {
10414 struct frame *f = XFRAME (w->frame);
10415 struct glyph_row *row, *bottom_row;
10416 struct it it;
10417 struct run run;
10418 struct text_pos start, new_start;
10419 int nrows_scrolled, i;
10420 struct glyph_row *last_text_row;
10421 struct glyph_row *last_reused_text_row;
10422 struct glyph_row *start_row;
10423 int start_vpos, min_y, max_y;
10424
10425 if (/* This function doesn't handle terminal frames. */
10426 !FRAME_WINDOW_P (f)
10427 /* Don't try to reuse the display if windows have been split
10428 or such. */
10429 || windows_or_buffers_changed)
10430 return 0;
10431
10432 /* Can't do this if region may have changed. */
10433 if ((!NILP (Vtransient_mark_mode)
10434 && !NILP (current_buffer->mark_active))
10435 || !NILP (w->region_showing)
10436 || !NILP (Vshow_trailing_whitespace))
10437 return 0;
10438
10439 /* If top-line visibility has changed, give up. */
10440 if (WINDOW_WANTS_HEADER_LINE_P (w)
10441 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
10442 return 0;
10443
10444 /* Give up if old or new display is scrolled vertically. We could
10445 make this function handle this, but right now it doesn't. */
10446 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10447 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
10448 return 0;
10449
10450 /* The variable new_start now holds the new window start. The old
10451 start `start' can be determined from the current matrix. */
10452 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
10453 start = start_row->start.pos;
10454 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
10455
10456 /* Clear the desired matrix for the display below. */
10457 clear_glyph_matrix (w->desired_matrix);
10458
10459 if (CHARPOS (new_start) <= CHARPOS (start))
10460 {
10461 int first_row_y;
10462
10463 /* Don't use this method if the display starts with an ellipsis
10464 displayed for invisible text. It's not easy to handle that case
10465 below, and it's certainly not worth the effort since this is
10466 not a frequent case. */
10467 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
10468 return 0;
10469
10470 IF_DEBUG (debug_method_add (w, "twu1"));
10471
10472 /* Display up to a row that can be reused. The variable
10473 last_text_row is set to the last row displayed that displays
10474 text. Note that it.vpos == 0 if or if not there is a
10475 header-line; it's not the same as the MATRIX_ROW_VPOS! */
10476 start_display (&it, w, new_start);
10477 first_row_y = it.current_y;
10478 w->cursor.vpos = -1;
10479 last_text_row = last_reused_text_row = NULL;
10480
10481 while (it.current_y < it.last_visible_y
10482 && IT_CHARPOS (it) < CHARPOS (start)
10483 && !fonts_changed_p)
10484 if (display_line (&it))
10485 last_text_row = it.glyph_row - 1;
10486
10487 /* A value of current_y < last_visible_y means that we stopped
10488 at the previous window start, which in turn means that we
10489 have at least one reusable row. */
10490 if (it.current_y < it.last_visible_y)
10491 {
10492 /* IT.vpos always starts from 0; it counts text lines. */
10493 nrows_scrolled = it.vpos;
10494
10495 /* Find PT if not already found in the lines displayed. */
10496 if (w->cursor.vpos < 0)
10497 {
10498 int dy = it.current_y - first_row_y;
10499
10500 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10501 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10502 {
10503 if (PT >= MATRIX_ROW_START_CHARPOS (row)
10504 && PT < MATRIX_ROW_END_CHARPOS (row))
10505 {
10506 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
10507 dy, nrows_scrolled);
10508 break;
10509 }
10510
10511 if (MATRIX_ROW_BOTTOM_Y (row) + dy >= it.last_visible_y)
10512 break;
10513
10514 ++row;
10515 }
10516
10517 /* Give up if point was not found. This shouldn't
10518 happen often; not more often than with try_window
10519 itself. */
10520 if (w->cursor.vpos < 0)
10521 {
10522 clear_glyph_matrix (w->desired_matrix);
10523 return 0;
10524 }
10525 }
10526
10527 /* Scroll the display. Do it before the current matrix is
10528 changed. The problem here is that update has not yet
10529 run, i.e. part of the current matrix is not up to date.
10530 scroll_run_hook will clear the cursor, and use the
10531 current matrix to get the height of the row the cursor is
10532 in. */
10533 run.current_y = first_row_y;
10534 run.desired_y = it.current_y;
10535 run.height = it.last_visible_y - it.current_y;
10536
10537 if (run.height > 0 && run.current_y != run.desired_y)
10538 {
10539 update_begin (f);
10540 rif->update_window_begin_hook (w);
10541 rif->clear_mouse_face (w);
10542 rif->scroll_run_hook (w, &run);
10543 rif->update_window_end_hook (w, 0, 0);
10544 update_end (f);
10545 }
10546
10547 /* Shift current matrix down by nrows_scrolled lines. */
10548 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
10549 rotate_matrix (w->current_matrix,
10550 start_vpos,
10551 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10552 nrows_scrolled);
10553
10554 /* Disable lines that must be updated. */
10555 for (i = 0; i < it.vpos; ++i)
10556 (start_row + i)->enabled_p = 0;
10557
10558 /* Re-compute Y positions. */
10559 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10560 max_y = it.last_visible_y;
10561 for (row = start_row + nrows_scrolled;
10562 row < bottom_row;
10563 ++row)
10564 {
10565 row->y = it.current_y;
10566
10567 if (row->y < min_y)
10568 row->visible_height = row->height - (min_y - row->y);
10569 else if (row->y + row->height > max_y)
10570 row->visible_height
10571 = row->height - (row->y + row->height - max_y);
10572 else
10573 row->visible_height = row->height;
10574
10575 it.current_y += row->height;
10576
10577 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10578 last_reused_text_row = row;
10579 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
10580 break;
10581 }
10582
10583 /* Disable lines in the current matrix which are now
10584 below the window. */
10585 for (++row; row < bottom_row; ++row)
10586 row->enabled_p = 0;
10587 }
10588
10589 /* Update window_end_pos etc.; last_reused_text_row is the last
10590 reused row from the current matrix containing text, if any.
10591 The value of last_text_row is the last displayed line
10592 containing text. */
10593 if (last_reused_text_row)
10594 {
10595 w->window_end_bytepos
10596 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
10597 w->window_end_pos
10598 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
10599 w->window_end_vpos
10600 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
10601 w->current_matrix));
10602 }
10603 else if (last_text_row)
10604 {
10605 w->window_end_bytepos
10606 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10607 w->window_end_pos
10608 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10609 w->window_end_vpos
10610 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10611 }
10612 else
10613 {
10614 /* This window must be completely empty. */
10615 w->window_end_bytepos = 0;
10616 w->window_end_pos = w->window_end_vpos = make_number (0);
10617 }
10618 w->window_end_valid = Qnil;
10619
10620 /* Update hint: don't try scrolling again in update_window. */
10621 w->desired_matrix->no_scrolling_p = 1;
10622
10623 #if GLYPH_DEBUG
10624 debug_method_add (w, "try_window_reusing_current_matrix 1");
10625 #endif
10626 return 1;
10627 }
10628 else if (CHARPOS (new_start) > CHARPOS (start))
10629 {
10630 struct glyph_row *pt_row, *row;
10631 struct glyph_row *first_reusable_row;
10632 struct glyph_row *first_row_to_display;
10633 int dy;
10634 int yb = window_text_bottom_y (w);
10635
10636 /* Find the row starting at new_start, if there is one. Don't
10637 reuse a partially visible line at the end. */
10638 first_reusable_row = start_row;
10639 while (first_reusable_row->enabled_p
10640 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
10641 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
10642 < CHARPOS (new_start)))
10643 ++first_reusable_row;
10644
10645 /* Give up if there is no row to reuse. */
10646 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
10647 || !first_reusable_row->enabled_p
10648 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
10649 != CHARPOS (new_start)))
10650 return 0;
10651
10652 /* We can reuse fully visible rows beginning with
10653 first_reusable_row to the end of the window. Set
10654 first_row_to_display to the first row that cannot be reused.
10655 Set pt_row to the row containing point, if there is any. */
10656 pt_row = NULL;
10657 for (first_row_to_display = first_reusable_row;
10658 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
10659 ++first_row_to_display)
10660 {
10661 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
10662 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
10663 pt_row = first_row_to_display;
10664 }
10665
10666 /* Start displaying at the start of first_row_to_display. */
10667 xassert (first_row_to_display->y < yb);
10668 init_to_row_start (&it, w, first_row_to_display);
10669
10670 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
10671 - start_vpos);
10672 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
10673 - nrows_scrolled);
10674 it.current_y = (first_row_to_display->y - first_reusable_row->y
10675 + WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
10676
10677 /* Display lines beginning with first_row_to_display in the
10678 desired matrix. Set last_text_row to the last row displayed
10679 that displays text. */
10680 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
10681 if (pt_row == NULL)
10682 w->cursor.vpos = -1;
10683 last_text_row = NULL;
10684 while (it.current_y < it.last_visible_y && !fonts_changed_p)
10685 if (display_line (&it))
10686 last_text_row = it.glyph_row - 1;
10687
10688 /* Give up If point isn't in a row displayed or reused. */
10689 if (w->cursor.vpos < 0)
10690 {
10691 clear_glyph_matrix (w->desired_matrix);
10692 return 0;
10693 }
10694
10695 /* If point is in a reused row, adjust y and vpos of the cursor
10696 position. */
10697 if (pt_row)
10698 {
10699 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
10700 w->current_matrix);
10701 w->cursor.y -= first_reusable_row->y;
10702 }
10703
10704 /* Scroll the display. */
10705 run.current_y = first_reusable_row->y;
10706 run.desired_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10707 run.height = it.last_visible_y - run.current_y;
10708 dy = run.current_y - run.desired_y;
10709
10710 if (run.height)
10711 {
10712 struct frame *f = XFRAME (WINDOW_FRAME (w));
10713 update_begin (f);
10714 rif->update_window_begin_hook (w);
10715 rif->clear_mouse_face (w);
10716 rif->scroll_run_hook (w, &run);
10717 rif->update_window_end_hook (w, 0, 0);
10718 update_end (f);
10719 }
10720
10721 /* Adjust Y positions of reused rows. */
10722 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
10723 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10724 max_y = it.last_visible_y;
10725 for (row = first_reusable_row; row < first_row_to_display; ++row)
10726 {
10727 row->y -= dy;
10728 if (row->y < min_y)
10729 row->visible_height = row->height - (min_y - row->y);
10730 else if (row->y + row->height > max_y)
10731 row->visible_height
10732 = row->height - (row->y + row->height - max_y);
10733 else
10734 row->visible_height = row->height;
10735 }
10736
10737 /* Scroll the current matrix. */
10738 xassert (nrows_scrolled > 0);
10739 rotate_matrix (w->current_matrix,
10740 start_vpos,
10741 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10742 -nrows_scrolled);
10743
10744 /* Disable rows not reused. */
10745 for (row -= nrows_scrolled; row < bottom_row; ++row)
10746 row->enabled_p = 0;
10747
10748 /* Adjust window end. A null value of last_text_row means that
10749 the window end is in reused rows which in turn means that
10750 only its vpos can have changed. */
10751 if (last_text_row)
10752 {
10753 w->window_end_bytepos
10754 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10755 w->window_end_pos
10756 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10757 w->window_end_vpos
10758 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10759 }
10760 else
10761 {
10762 w->window_end_vpos
10763 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
10764 }
10765
10766 w->window_end_valid = Qnil;
10767 w->desired_matrix->no_scrolling_p = 1;
10768
10769 #if GLYPH_DEBUG
10770 debug_method_add (w, "try_window_reusing_current_matrix 2");
10771 #endif
10772 return 1;
10773 }
10774
10775 return 0;
10776 }
10777
10778
10779 \f
10780 /************************************************************************
10781 Window redisplay reusing current matrix when buffer has changed
10782 ************************************************************************/
10783
10784 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
10785 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
10786 int *, int *));
10787 static struct glyph_row *
10788 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
10789 struct glyph_row *));
10790
10791
10792 /* Return the last row in MATRIX displaying text. If row START is
10793 non-null, start searching with that row. IT gives the dimensions
10794 of the display. Value is null if matrix is empty; otherwise it is
10795 a pointer to the row found. */
10796
10797 static struct glyph_row *
10798 find_last_row_displaying_text (matrix, it, start)
10799 struct glyph_matrix *matrix;
10800 struct it *it;
10801 struct glyph_row *start;
10802 {
10803 struct glyph_row *row, *row_found;
10804
10805 /* Set row_found to the last row in IT->w's current matrix
10806 displaying text. The loop looks funny but think of partially
10807 visible lines. */
10808 row_found = NULL;
10809 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
10810 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10811 {
10812 xassert (row->enabled_p);
10813 row_found = row;
10814 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
10815 break;
10816 ++row;
10817 }
10818
10819 return row_found;
10820 }
10821
10822
10823 /* Return the last row in the current matrix of W that is not affected
10824 by changes at the start of current_buffer that occurred since the
10825 last time W was redisplayed. Value is null if no such row exists.
10826
10827 The global variable beg_unchanged has to contain the number of
10828 bytes unchanged at the start of current_buffer. BEG +
10829 beg_unchanged is the buffer position of the first changed byte in
10830 current_buffer. Characters at positions < BEG + beg_unchanged are
10831 at the same buffer positions as they were when the current matrix
10832 was built. */
10833
10834 static struct glyph_row *
10835 find_last_unchanged_at_beg_row (w)
10836 struct window *w;
10837 {
10838 int first_changed_pos = BEG + BEG_UNCHANGED;
10839 struct glyph_row *row;
10840 struct glyph_row *row_found = NULL;
10841 int yb = window_text_bottom_y (w);
10842
10843 /* Find the last row displaying unchanged text. */
10844 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10845 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
10846 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
10847 {
10848 if (/* If row ends before first_changed_pos, it is unchanged,
10849 except in some case. */
10850 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
10851 /* When row ends in ZV and we write at ZV it is not
10852 unchanged. */
10853 && !row->ends_at_zv_p
10854 /* When first_changed_pos is the end of a continued line,
10855 row is not unchanged because it may be no longer
10856 continued. */
10857 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
10858 && row->continued_p))
10859 row_found = row;
10860
10861 /* Stop if last visible row. */
10862 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
10863 break;
10864
10865 ++row;
10866 }
10867
10868 return row_found;
10869 }
10870
10871
10872 /* Find the first glyph row in the current matrix of W that is not
10873 affected by changes at the end of current_buffer since the last
10874 time the window was redisplayed. Return in *DELTA the number of
10875 chars by which buffer positions in unchanged text at the end of
10876 current_buffer must be adjusted. Return in *DELTA_BYTES the
10877 corresponding number of bytes. Value is null if no such row
10878 exists, i.e. all rows are affected by changes. */
10879
10880 static struct glyph_row *
10881 find_first_unchanged_at_end_row (w, delta, delta_bytes)
10882 struct window *w;
10883 int *delta, *delta_bytes;
10884 {
10885 struct glyph_row *row;
10886 struct glyph_row *row_found = NULL;
10887
10888 *delta = *delta_bytes = 0;
10889
10890 /* Display must not have been paused, otherwise the current matrix
10891 is not up to date. */
10892 if (NILP (w->window_end_valid))
10893 abort ();
10894
10895 /* A value of window_end_pos >= END_UNCHANGED means that the window
10896 end is in the range of changed text. If so, there is no
10897 unchanged row at the end of W's current matrix. */
10898 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
10899 return NULL;
10900
10901 /* Set row to the last row in W's current matrix displaying text. */
10902 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
10903
10904 /* If matrix is entirely empty, no unchanged row exists. */
10905 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10906 {
10907 /* The value of row is the last glyph row in the matrix having a
10908 meaningful buffer position in it. The end position of row
10909 corresponds to window_end_pos. This allows us to translate
10910 buffer positions in the current matrix to current buffer
10911 positions for characters not in changed text. */
10912 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
10913 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
10914 int last_unchanged_pos, last_unchanged_pos_old;
10915 struct glyph_row *first_text_row
10916 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10917
10918 *delta = Z - Z_old;
10919 *delta_bytes = Z_BYTE - Z_BYTE_old;
10920
10921 /* Set last_unchanged_pos to the buffer position of the last
10922 character in the buffer that has not been changed. Z is the
10923 index + 1 of the last byte in current_buffer, i.e. by
10924 subtracting end_unchanged we get the index of the last
10925 unchanged character, and we have to add BEG to get its buffer
10926 position. */
10927 last_unchanged_pos = Z - END_UNCHANGED + BEG;
10928 last_unchanged_pos_old = last_unchanged_pos - *delta;
10929
10930 /* Search backward from ROW for a row displaying a line that
10931 starts at a minimum position >= last_unchanged_pos_old. */
10932 for (; row > first_text_row; --row)
10933 {
10934 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
10935 abort ();
10936
10937 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
10938 row_found = row;
10939 }
10940 }
10941
10942 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
10943 abort ();
10944
10945 return row_found;
10946 }
10947
10948
10949 /* Make sure that glyph rows in the current matrix of window W
10950 reference the same glyph memory as corresponding rows in the
10951 frame's frame matrix. This function is called after scrolling W's
10952 current matrix on a terminal frame in try_window_id and
10953 try_window_reusing_current_matrix. */
10954
10955 static void
10956 sync_frame_with_window_matrix_rows (w)
10957 struct window *w;
10958 {
10959 struct frame *f = XFRAME (w->frame);
10960 struct glyph_row *window_row, *window_row_end, *frame_row;
10961
10962 /* Preconditions: W must be a leaf window and full-width. Its frame
10963 must have a frame matrix. */
10964 xassert (NILP (w->hchild) && NILP (w->vchild));
10965 xassert (WINDOW_FULL_WIDTH_P (w));
10966 xassert (!FRAME_WINDOW_P (f));
10967
10968 /* If W is a full-width window, glyph pointers in W's current matrix
10969 have, by definition, to be the same as glyph pointers in the
10970 corresponding frame matrix. */
10971 window_row = w->current_matrix->rows;
10972 window_row_end = window_row + w->current_matrix->nrows;
10973 frame_row = f->current_matrix->rows + XFASTINT (w->top);
10974 while (window_row < window_row_end)
10975 {
10976 int area;
10977
10978 for (area = LEFT_MARGIN_AREA; area <= LAST_AREA; ++area)
10979 frame_row->glyphs[area] = window_row->glyphs[area];
10980
10981 /* Disable frame rows whose corresponding window rows have
10982 been disabled in try_window_id. */
10983 if (!window_row->enabled_p)
10984 frame_row->enabled_p = 0;
10985
10986 ++window_row, ++frame_row;
10987 }
10988 }
10989
10990
10991 /* Find the glyph row in window W containing CHARPOS. Consider all
10992 rows between START and END (not inclusive). END null means search
10993 all rows to the end of the display area of W. Value is the row
10994 containing CHARPOS or null. */
10995
10996 static struct glyph_row *
10997 row_containing_pos (w, charpos, start, end)
10998 struct window *w;
10999 int charpos;
11000 struct glyph_row *start, *end;
11001 {
11002 struct glyph_row *row = start;
11003 int last_y;
11004
11005 /* If we happen to start on a header-line, skip that. */
11006 if (row->mode_line_p)
11007 ++row;
11008
11009 if ((end && row >= end) || !row->enabled_p)
11010 return NULL;
11011
11012 last_y = window_text_bottom_y (w);
11013
11014 while ((end == NULL || row < end)
11015 && (MATRIX_ROW_END_CHARPOS (row) < charpos
11016 /* The end position of a row equals the start
11017 position of the next row. If CHARPOS is there, we
11018 would rather display it in the next line, except
11019 when this line ends in ZV. */
11020 || (MATRIX_ROW_END_CHARPOS (row) == charpos
11021 && (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
11022 || !row->ends_at_zv_p)))
11023 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
11024 ++row;
11025
11026 /* Give up if CHARPOS not found. */
11027 if ((end && row >= end)
11028 || charpos < MATRIX_ROW_START_CHARPOS (row)
11029 || charpos > MATRIX_ROW_END_CHARPOS (row))
11030 row = NULL;
11031
11032 return row;
11033 }
11034
11035
11036 /* Try to redisplay window W by reusing its existing display. W's
11037 current matrix must be up to date when this function is called,
11038 i.e. window_end_valid must not be nil.
11039
11040 Value is
11041
11042 1 if display has been updated
11043 0 if otherwise unsuccessful
11044 -1 if redisplay with same window start is known not to succeed
11045
11046 The following steps are performed:
11047
11048 1. Find the last row in the current matrix of W that is not
11049 affected by changes at the start of current_buffer. If no such row
11050 is found, give up.
11051
11052 2. Find the first row in W's current matrix that is not affected by
11053 changes at the end of current_buffer. Maybe there is no such row.
11054
11055 3. Display lines beginning with the row + 1 found in step 1 to the
11056 row found in step 2 or, if step 2 didn't find a row, to the end of
11057 the window.
11058
11059 4. If cursor is not known to appear on the window, give up.
11060
11061 5. If display stopped at the row found in step 2, scroll the
11062 display and current matrix as needed.
11063
11064 6. Maybe display some lines at the end of W, if we must. This can
11065 happen under various circumstances, like a partially visible line
11066 becoming fully visible, or because newly displayed lines are displayed
11067 in smaller font sizes.
11068
11069 7. Update W's window end information. */
11070
11071 /* Check that window end is what we expect it to be. */
11072
11073 static int
11074 try_window_id (w)
11075 struct window *w;
11076 {
11077 struct frame *f = XFRAME (w->frame);
11078 struct glyph_matrix *current_matrix = w->current_matrix;
11079 struct glyph_matrix *desired_matrix = w->desired_matrix;
11080 struct glyph_row *last_unchanged_at_beg_row;
11081 struct glyph_row *first_unchanged_at_end_row;
11082 struct glyph_row *row;
11083 struct glyph_row *bottom_row;
11084 int bottom_vpos;
11085 struct it it;
11086 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
11087 struct text_pos start_pos;
11088 struct run run;
11089 int first_unchanged_at_end_vpos = 0;
11090 struct glyph_row *last_text_row, *last_text_row_at_end;
11091 struct text_pos start;
11092
11093 SET_TEXT_POS_FROM_MARKER (start, w->start);
11094
11095 /* Check pre-conditions. Window end must be valid, otherwise
11096 the current matrix would not be up to date. */
11097 xassert (!NILP (w->window_end_valid));
11098 xassert (FRAME_WINDOW_P (XFRAME (w->frame))
11099 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)));
11100
11101 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
11102 only if buffer has really changed. The reason is that the gap is
11103 initially at Z for freshly visited files. The code below would
11104 set end_unchanged to 0 in that case. */
11105 if (MODIFF > SAVE_MODIFF
11106 /* This seems to happen sometimes after saving a buffer. */
11107 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
11108 {
11109 if (GPT - BEG < BEG_UNCHANGED)
11110 BEG_UNCHANGED = GPT - BEG;
11111 if (Z - GPT < END_UNCHANGED)
11112 END_UNCHANGED = Z - GPT;
11113 }
11114
11115 /* If window starts after a line end, and the last change is in
11116 front of that newline, then changes don't affect the display.
11117 This case happens with stealth-fontification. Note that although
11118 the display is unchanged, glyph positions in the matrix have to
11119 be adjusted, of course. */
11120 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
11121 if (CHARPOS (start) > BEGV
11122 && Z - END_UNCHANGED < CHARPOS (start) - 1
11123 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n'
11124 && PT < MATRIX_ROW_END_CHARPOS (row))
11125 {
11126 struct glyph_row *r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
11127 int delta = CHARPOS (start) - MATRIX_ROW_START_CHARPOS (r0);
11128
11129 if (delta)
11130 {
11131 struct glyph_row *r1 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11132 int delta_bytes = BYTEPOS (start) - MATRIX_ROW_START_BYTEPOS (r0);
11133
11134 increment_matrix_positions (w->current_matrix,
11135 MATRIX_ROW_VPOS (r0, current_matrix),
11136 MATRIX_ROW_VPOS (r1, current_matrix),
11137 delta, delta_bytes);
11138 }
11139
11140 #if 0 /* If changes are all in front of the window start, the
11141 distance of the last displayed glyph from Z hasn't
11142 changed. */
11143 w->window_end_pos
11144 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
11145 w->window_end_bytepos
11146 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11147 #endif
11148
11149 row = row_containing_pos (w, PT, r0, NULL);
11150 if (row == NULL)
11151 return 0;
11152
11153 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11154 return 1;
11155 }
11156
11157 /* Return quickly if changes are all below what is displayed in the
11158 window, and if PT is in the window. */
11159 if (BEG_UNCHANGED > MATRIX_ROW_END_CHARPOS (row)
11160 && PT < MATRIX_ROW_END_CHARPOS (row))
11161 {
11162 /* We have to update window end positions because the buffer's
11163 size has changed. */
11164 w->window_end_pos
11165 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
11166 w->window_end_bytepos
11167 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11168
11169 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11170 row = row_containing_pos (w, PT, row, NULL);
11171 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11172 return 2;
11173 }
11174
11175 /* Check that window start agrees with the start of the first glyph
11176 row in its current matrix. Check this after we know the window
11177 start is not in changed text, otherwise positions would not be
11178 comparable. */
11179 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11180 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
11181 return 0;
11182
11183 /* Compute the position at which we have to start displaying new
11184 lines. Some of the lines at the top of the window might be
11185 reusable because they are not displaying changed text. Find the
11186 last row in W's current matrix not affected by changes at the
11187 start of current_buffer. Value is null if changes start in the
11188 first line of window. */
11189 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
11190 if (last_unchanged_at_beg_row)
11191 {
11192 /* Avoid starting to display in the moddle of a character, a TAB
11193 for instance. This is easier than to set up the iterator
11194 exactly, and it's not a frequent case, so the additional
11195 effort wouldn't really pay off. */
11196 while (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
11197 && last_unchanged_at_beg_row > w->current_matrix->rows)
11198 --last_unchanged_at_beg_row;
11199
11200 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
11201 return 0;
11202
11203 init_to_row_end (&it, w, last_unchanged_at_beg_row);
11204 start_pos = it.current.pos;
11205
11206 /* Start displaying new lines in the desired matrix at the same
11207 vpos we would use in the current matrix, i.e. below
11208 last_unchanged_at_beg_row. */
11209 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
11210 current_matrix);
11211 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11212 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
11213
11214 xassert (it.hpos == 0 && it.current_x == 0);
11215 }
11216 else
11217 {
11218 /* There are no reusable lines at the start of the window.
11219 Start displaying in the first line. */
11220 start_display (&it, w, start);
11221 start_pos = it.current.pos;
11222 }
11223
11224 /* Find the first row that is not affected by changes at the end of
11225 the buffer. Value will be null if there is no unchanged row, in
11226 which case we must redisplay to the end of the window. delta
11227 will be set to the value by which buffer positions beginning with
11228 first_unchanged_at_end_row have to be adjusted due to text
11229 changes. */
11230 first_unchanged_at_end_row
11231 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
11232 IF_DEBUG (debug_delta = delta);
11233 IF_DEBUG (debug_delta_bytes = delta_bytes);
11234
11235 /* Set stop_pos to the buffer position up to which we will have to
11236 display new lines. If first_unchanged_at_end_row != NULL, this
11237 is the buffer position of the start of the line displayed in that
11238 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
11239 that we don't stop at a buffer position. */
11240 stop_pos = 0;
11241 if (first_unchanged_at_end_row)
11242 {
11243 xassert (last_unchanged_at_beg_row == NULL
11244 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
11245
11246 /* If this is a continuation line, move forward to the next one
11247 that isn't. Changes in lines above affect this line.
11248 Caution: this may move first_unchanged_at_end_row to a row
11249 not displaying text. */
11250 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
11251 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11252 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11253 < it.last_visible_y))
11254 ++first_unchanged_at_end_row;
11255
11256 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11257 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11258 >= it.last_visible_y))
11259 first_unchanged_at_end_row = NULL;
11260 else
11261 {
11262 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
11263 + delta);
11264 first_unchanged_at_end_vpos
11265 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
11266 xassert (stop_pos >= Z - END_UNCHANGED);
11267 }
11268 }
11269 else if (last_unchanged_at_beg_row == NULL)
11270 return 0;
11271
11272
11273 #if GLYPH_DEBUG
11274
11275 /* Either there is no unchanged row at the end, or the one we have
11276 now displays text. This is a necessary condition for the window
11277 end pos calculation at the end of this function. */
11278 xassert (first_unchanged_at_end_row == NULL
11279 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
11280
11281 debug_last_unchanged_at_beg_vpos
11282 = (last_unchanged_at_beg_row
11283 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
11284 : -1);
11285 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
11286
11287 #endif /* GLYPH_DEBUG != 0 */
11288
11289
11290 /* Display new lines. Set last_text_row to the last new line
11291 displayed which has text on it, i.e. might end up as being the
11292 line where the window_end_vpos is. */
11293 w->cursor.vpos = -1;
11294 last_text_row = NULL;
11295 overlay_arrow_seen = 0;
11296 while (it.current_y < it.last_visible_y
11297 && !fonts_changed_p
11298 && (first_unchanged_at_end_row == NULL
11299 || IT_CHARPOS (it) < stop_pos))
11300 {
11301 if (display_line (&it))
11302 last_text_row = it.glyph_row - 1;
11303 }
11304
11305 if (fonts_changed_p)
11306 return -1;
11307
11308
11309 /* Compute differences in buffer positions, y-positions etc. for
11310 lines reused at the bottom of the window. Compute what we can
11311 scroll. */
11312 if (first_unchanged_at_end_row
11313 /* No lines reused because we displayed everything up to the
11314 bottom of the window. */
11315 && it.current_y < it.last_visible_y)
11316 {
11317 dvpos = (it.vpos
11318 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
11319 current_matrix));
11320 dy = it.current_y - first_unchanged_at_end_row->y;
11321 run.current_y = first_unchanged_at_end_row->y;
11322 run.desired_y = run.current_y + dy;
11323 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
11324 }
11325 else
11326 {
11327 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
11328 first_unchanged_at_end_row = NULL;
11329 }
11330 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
11331
11332
11333 /* Find the cursor if not already found. We have to decide whether
11334 PT will appear on this window (it sometimes doesn't, but this is
11335 not a very frequent case.) This decision has to be made before
11336 the current matrix is altered. A value of cursor.vpos < 0 means
11337 that PT is either in one of the lines beginning at
11338 first_unchanged_at_end_row or below the window. Don't care for
11339 lines that might be displayed later at the window end; as
11340 mentioned, this is not a frequent case. */
11341 if (w->cursor.vpos < 0)
11342 {
11343 /* Cursor in unchanged rows at the top? */
11344 if (PT < CHARPOS (start_pos)
11345 && last_unchanged_at_beg_row)
11346 {
11347 row = row_containing_pos (w, PT,
11348 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
11349 last_unchanged_at_beg_row + 1);
11350 if (row)
11351 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11352 }
11353
11354 /* Start from first_unchanged_at_end_row looking for PT. */
11355 else if (first_unchanged_at_end_row)
11356 {
11357 row = row_containing_pos (w, PT - delta,
11358 first_unchanged_at_end_row, NULL);
11359 if (row)
11360 set_cursor_from_row (w, row, w->current_matrix, delta,
11361 delta_bytes, dy, dvpos);
11362 }
11363
11364 /* Give up if cursor was not found. */
11365 if (w->cursor.vpos < 0)
11366 {
11367 clear_glyph_matrix (w->desired_matrix);
11368 return -1;
11369 }
11370 }
11371
11372 /* Don't let the cursor end in the scroll margins. */
11373 {
11374 int this_scroll_margin, cursor_height;
11375
11376 this_scroll_margin = max (0, scroll_margin);
11377 this_scroll_margin = min (this_scroll_margin,
11378 XFASTINT (w->height) / 4);
11379 this_scroll_margin *= CANON_Y_UNIT (it.f);
11380 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
11381
11382 if ((w->cursor.y < this_scroll_margin
11383 && CHARPOS (start) > BEGV)
11384 /* Don't take scroll margin into account at the bottom because
11385 old redisplay didn't do it either. */
11386 || w->cursor.y + cursor_height > it.last_visible_y)
11387 {
11388 w->cursor.vpos = -1;
11389 clear_glyph_matrix (w->desired_matrix);
11390 return -1;
11391 }
11392 }
11393
11394 /* Scroll the display. Do it before changing the current matrix so
11395 that xterm.c doesn't get confused about where the cursor glyph is
11396 found. */
11397 if (dy && run.height)
11398 {
11399 update_begin (f);
11400
11401 if (FRAME_WINDOW_P (f))
11402 {
11403 rif->update_window_begin_hook (w);
11404 rif->clear_mouse_face (w);
11405 rif->scroll_run_hook (w, &run);
11406 rif->update_window_end_hook (w, 0, 0);
11407 }
11408 else
11409 {
11410 /* Terminal frame. In this case, dvpos gives the number of
11411 lines to scroll by; dvpos < 0 means scroll up. */
11412 int first_unchanged_at_end_vpos
11413 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
11414 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
11415 int end = XFASTINT (w->top) + window_internal_height (w);
11416
11417 /* Perform the operation on the screen. */
11418 if (dvpos > 0)
11419 {
11420 /* Scroll last_unchanged_at_beg_row to the end of the
11421 window down dvpos lines. */
11422 set_terminal_window (end);
11423
11424 /* On dumb terminals delete dvpos lines at the end
11425 before inserting dvpos empty lines. */
11426 if (!scroll_region_ok)
11427 ins_del_lines (end - dvpos, -dvpos);
11428
11429 /* Insert dvpos empty lines in front of
11430 last_unchanged_at_beg_row. */
11431 ins_del_lines (from, dvpos);
11432 }
11433 else if (dvpos < 0)
11434 {
11435 /* Scroll up last_unchanged_at_beg_vpos to the end of
11436 the window to last_unchanged_at_beg_vpos - |dvpos|. */
11437 set_terminal_window (end);
11438
11439 /* Delete dvpos lines in front of
11440 last_unchanged_at_beg_vpos. ins_del_lines will set
11441 the cursor to the given vpos and emit |dvpos| delete
11442 line sequences. */
11443 ins_del_lines (from + dvpos, dvpos);
11444
11445 /* On a dumb terminal insert dvpos empty lines at the
11446 end. */
11447 if (!scroll_region_ok)
11448 ins_del_lines (end + dvpos, -dvpos);
11449 }
11450
11451 set_terminal_window (0);
11452 }
11453
11454 update_end (f);
11455 }
11456
11457 /* Shift reused rows of the current matrix to the right position.
11458 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
11459 text. */
11460 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11461 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
11462 if (dvpos < 0)
11463 {
11464 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
11465 bottom_vpos, dvpos);
11466 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
11467 bottom_vpos, 0);
11468 }
11469 else if (dvpos > 0)
11470 {
11471 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
11472 bottom_vpos, dvpos);
11473 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
11474 first_unchanged_at_end_vpos + dvpos, 0);
11475 }
11476
11477 /* For frame-based redisplay, make sure that current frame and window
11478 matrix are in sync with respect to glyph memory. */
11479 if (!FRAME_WINDOW_P (f))
11480 sync_frame_with_window_matrix_rows (w);
11481
11482 /* Adjust buffer positions in reused rows. */
11483 if (delta)
11484 increment_matrix_positions (current_matrix,
11485 first_unchanged_at_end_vpos + dvpos,
11486 bottom_vpos, delta, delta_bytes);
11487
11488 /* Adjust Y positions. */
11489 if (dy)
11490 shift_glyph_matrix (w, current_matrix,
11491 first_unchanged_at_end_vpos + dvpos,
11492 bottom_vpos, dy);
11493
11494 if (first_unchanged_at_end_row)
11495 first_unchanged_at_end_row += dvpos;
11496
11497 /* If scrolling up, there may be some lines to display at the end of
11498 the window. */
11499 last_text_row_at_end = NULL;
11500 if (dy < 0)
11501 {
11502 /* Set last_row to the glyph row in the current matrix where the
11503 window end line is found. It has been moved up or down in
11504 the matrix by dvpos. */
11505 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
11506 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
11507
11508 /* If last_row is the window end line, it should display text. */
11509 xassert (last_row->displays_text_p);
11510
11511 /* If window end line was partially visible before, begin
11512 displaying at that line. Otherwise begin displaying with the
11513 line following it. */
11514 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
11515 {
11516 init_to_row_start (&it, w, last_row);
11517 it.vpos = last_vpos;
11518 it.current_y = last_row->y;
11519 }
11520 else
11521 {
11522 init_to_row_end (&it, w, last_row);
11523 it.vpos = 1 + last_vpos;
11524 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
11525 ++last_row;
11526 }
11527
11528 /* We may start in a continuation line. If so, we have to get
11529 the right continuation_lines_width and current_x. */
11530 it.continuation_lines_width = last_row->continuation_lines_width;
11531 it.hpos = it.current_x = 0;
11532
11533 /* Display the rest of the lines at the window end. */
11534 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11535 while (it.current_y < it.last_visible_y
11536 && !fonts_changed_p)
11537 {
11538 /* Is it always sure that the display agrees with lines in
11539 the current matrix? I don't think so, so we mark rows
11540 displayed invalid in the current matrix by setting their
11541 enabled_p flag to zero. */
11542 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
11543 if (display_line (&it))
11544 last_text_row_at_end = it.glyph_row - 1;
11545 }
11546 }
11547
11548 /* Update window_end_pos and window_end_vpos. */
11549 if (first_unchanged_at_end_row
11550 && first_unchanged_at_end_row->y < it.last_visible_y
11551 && !last_text_row_at_end)
11552 {
11553 /* Window end line if one of the preserved rows from the current
11554 matrix. Set row to the last row displaying text in current
11555 matrix starting at first_unchanged_at_end_row, after
11556 scrolling. */
11557 xassert (first_unchanged_at_end_row->displays_text_p);
11558 row = find_last_row_displaying_text (w->current_matrix, &it,
11559 first_unchanged_at_end_row);
11560 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
11561
11562 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
11563 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11564 w->window_end_vpos
11565 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
11566 }
11567 else if (last_text_row_at_end)
11568 {
11569 w->window_end_pos
11570 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
11571 w->window_end_bytepos
11572 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
11573 w->window_end_vpos
11574 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
11575 }
11576 else if (last_text_row)
11577 {
11578 /* We have displayed either to the end of the window or at the
11579 end of the window, i.e. the last row with text is to be found
11580 in the desired matrix. */
11581 w->window_end_pos
11582 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
11583 w->window_end_bytepos
11584 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
11585 w->window_end_vpos
11586 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
11587 }
11588 else if (first_unchanged_at_end_row == NULL
11589 && last_text_row == NULL
11590 && last_text_row_at_end == NULL)
11591 {
11592 /* Displayed to end of window, but no line containing text was
11593 displayed. Lines were deleted at the end of the window. */
11594 int vpos;
11595 int header_line_p = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
11596
11597 for (vpos = XFASTINT (w->window_end_vpos); vpos > 0; --vpos)
11598 if ((w->desired_matrix->rows[vpos + header_line_p].enabled_p
11599 && w->desired_matrix->rows[vpos + header_line_p].displays_text_p)
11600 || (!w->desired_matrix->rows[vpos + header_line_p].enabled_p
11601 && w->current_matrix->rows[vpos + header_line_p].displays_text_p))
11602 break;
11603
11604 w->window_end_vpos = make_number (vpos);
11605 }
11606 else
11607 abort ();
11608
11609 #if 0 /* This leads to problems, for instance when the cursor is
11610 at ZV, and the cursor line displays no text. */
11611 /* Disable rows below what's displayed in the window. This makes
11612 debugging easier. */
11613 enable_glyph_matrix_rows (current_matrix,
11614 XFASTINT (w->window_end_vpos) + 1,
11615 bottom_vpos, 0);
11616 #endif
11617
11618 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
11619 debug_end_vpos = XFASTINT (w->window_end_vpos));
11620
11621 /* Record that display has not been completed. */
11622 w->window_end_valid = Qnil;
11623 w->desired_matrix->no_scrolling_p = 1;
11624 return 3;
11625 }
11626
11627
11628 \f
11629 /***********************************************************************
11630 More debugging support
11631 ***********************************************************************/
11632
11633 #if GLYPH_DEBUG
11634
11635 void dump_glyph_row P_ ((struct glyph_matrix *, int, int));
11636 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
11637 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
11638
11639
11640 /* Dump the contents of glyph matrix MATRIX on stderr.
11641
11642 GLYPHS 0 means don't show glyph contents.
11643 GLYPHS 1 means show glyphs in short form
11644 GLYPHS > 1 means show glyphs in long form. */
11645
11646 void
11647 dump_glyph_matrix (matrix, glyphs)
11648 struct glyph_matrix *matrix;
11649 int glyphs;
11650 {
11651 int i;
11652 for (i = 0; i < matrix->nrows; ++i)
11653 dump_glyph_row (matrix, i, glyphs);
11654 }
11655
11656
11657 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
11658 the glyph row and area where the glyph comes from. */
11659
11660 void
11661 dump_glyph (row, glyph, area)
11662 struct glyph_row *row;
11663 struct glyph *glyph;
11664 int area;
11665 {
11666 if (glyph->type == CHAR_GLYPH)
11667 {
11668 fprintf (stderr,
11669 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11670 glyph - row->glyphs[TEXT_AREA],
11671 'C',
11672 glyph->charpos,
11673 (BUFFERP (glyph->object)
11674 ? 'B'
11675 : (STRINGP (glyph->object)
11676 ? 'S'
11677 : '-')),
11678 glyph->pixel_width,
11679 glyph->u.ch,
11680 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
11681 ? glyph->u.ch
11682 : '.'),
11683 glyph->face_id,
11684 glyph->left_box_line_p,
11685 glyph->right_box_line_p);
11686 }
11687 else if (glyph->type == STRETCH_GLYPH)
11688 {
11689 fprintf (stderr,
11690 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11691 glyph - row->glyphs[TEXT_AREA],
11692 'S',
11693 glyph->charpos,
11694 (BUFFERP (glyph->object)
11695 ? 'B'
11696 : (STRINGP (glyph->object)
11697 ? 'S'
11698 : '-')),
11699 glyph->pixel_width,
11700 0,
11701 '.',
11702 glyph->face_id,
11703 glyph->left_box_line_p,
11704 glyph->right_box_line_p);
11705 }
11706 else if (glyph->type == IMAGE_GLYPH)
11707 {
11708 fprintf (stderr,
11709 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11710 glyph - row->glyphs[TEXT_AREA],
11711 'I',
11712 glyph->charpos,
11713 (BUFFERP (glyph->object)
11714 ? 'B'
11715 : (STRINGP (glyph->object)
11716 ? 'S'
11717 : '-')),
11718 glyph->pixel_width,
11719 glyph->u.img_id,
11720 '.',
11721 glyph->face_id,
11722 glyph->left_box_line_p,
11723 glyph->right_box_line_p);
11724 }
11725 }
11726
11727
11728 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
11729 GLYPHS 0 means don't show glyph contents.
11730 GLYPHS 1 means show glyphs in short form
11731 GLYPHS > 1 means show glyphs in long form. */
11732
11733 void
11734 dump_glyph_row (matrix, vpos, glyphs)
11735 struct glyph_matrix *matrix;
11736 int vpos, glyphs;
11737 {
11738 struct glyph_row *row;
11739
11740 if (vpos < 0 || vpos >= matrix->nrows)
11741 return;
11742
11743 row = MATRIX_ROW (matrix, vpos);
11744
11745 if (glyphs != 1)
11746 {
11747 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
11748 fprintf (stderr, "=======================================================================\n");
11749
11750 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d\
11751 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
11752 row - matrix->rows,
11753 MATRIX_ROW_START_CHARPOS (row),
11754 MATRIX_ROW_END_CHARPOS (row),
11755 row->used[TEXT_AREA],
11756 row->contains_overlapping_glyphs_p,
11757 row->enabled_p,
11758 row->inverse_p,
11759 row->truncated_on_left_p,
11760 row->truncated_on_right_p,
11761 row->overlay_arrow_p,
11762 row->continued_p,
11763 MATRIX_ROW_CONTINUATION_LINE_P (row),
11764 row->displays_text_p,
11765 row->ends_at_zv_p,
11766 row->fill_line_p,
11767 row->ends_in_middle_of_char_p,
11768 row->starts_in_middle_of_char_p,
11769 row->mouse_face_p,
11770 row->x,
11771 row->y,
11772 row->pixel_width,
11773 row->height,
11774 row->visible_height,
11775 row->ascent,
11776 row->phys_ascent);
11777 fprintf (stderr, "%9d %5d\n", row->start.overlay_string_index,
11778 row->end.overlay_string_index);
11779 fprintf (stderr, "%9d %5d\n",
11780 CHARPOS (row->start.string_pos),
11781 CHARPOS (row->end.string_pos));
11782 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
11783 row->end.dpvec_index);
11784 }
11785
11786 if (glyphs > 1)
11787 {
11788 int area;
11789
11790 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
11791 {
11792 struct glyph *glyph = row->glyphs[area];
11793 struct glyph *glyph_end = glyph + row->used[area];
11794
11795 /* Glyph for a line end in text. */
11796 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
11797 ++glyph_end;
11798
11799 if (glyph < glyph_end)
11800 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
11801
11802 for (; glyph < glyph_end; ++glyph)
11803 dump_glyph (row, glyph, area);
11804 }
11805 }
11806 else if (glyphs == 1)
11807 {
11808 int area;
11809
11810 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
11811 {
11812 char *s = (char *) alloca (row->used[area] + 1);
11813 int i;
11814
11815 for (i = 0; i < row->used[area]; ++i)
11816 {
11817 struct glyph *glyph = row->glyphs[area] + i;
11818 if (glyph->type == CHAR_GLYPH
11819 && glyph->u.ch < 0x80
11820 && glyph->u.ch >= ' ')
11821 s[i] = glyph->u.ch;
11822 else
11823 s[i] = '.';
11824 }
11825
11826 s[i] = '\0';
11827 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
11828 }
11829 }
11830 }
11831
11832
11833 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
11834 Sdump_glyph_matrix, 0, 1, "p",
11835 "Dump the current matrix of the selected window to stderr.\n\
11836 Shows contents of glyph row structures. With non-nil\n\
11837 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show\n\
11838 glyphs in short form, otherwise show glyphs in long form.")
11839 (glyphs)
11840 Lisp_Object glyphs;
11841 {
11842 struct window *w = XWINDOW (selected_window);
11843 struct buffer *buffer = XBUFFER (w->buffer);
11844
11845 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
11846 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
11847 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
11848 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
11849 fprintf (stderr, "=============================================\n");
11850 dump_glyph_matrix (w->current_matrix,
11851 NILP (glyphs) ? 0 : XINT (glyphs));
11852 return Qnil;
11853 }
11854
11855
11856 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
11857 "Dump glyph row ROW to stderr.\n\
11858 GLYPH 0 means don't dump glyphs.\n\
11859 GLYPH 1 means dump glyphs in short form.\n\
11860 GLYPH > 1 or omitted means dump glyphs in long form.")
11861 (row, glyphs)
11862 Lisp_Object row, glyphs;
11863 {
11864 CHECK_NUMBER (row, 0);
11865 dump_glyph_row (XWINDOW (selected_window)->current_matrix,
11866 XINT (row),
11867 INTEGERP (glyphs) ? XINT (glyphs) : 2);
11868 return Qnil;
11869 }
11870
11871
11872 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
11873 "Dump glyph row ROW of the tool-bar of the current frame to stderr.\n\
11874 GLYPH 0 means don't dump glyphs.\n\
11875 GLYPH 1 means dump glyphs in short form.\n\
11876 GLYPH > 1 or omitted means dump glyphs in long form.")
11877 (row, glyphs)
11878 Lisp_Object row, glyphs;
11879 {
11880 struct frame *sf = SELECTED_FRAME ();
11881 struct glyph_matrix *m = (XWINDOW (sf->tool_bar_window)->current_matrix);
11882 dump_glyph_row (m, XINT (row), INTEGERP (glyphs) ? XINT (glyphs) : 2);
11883 return Qnil;
11884 }
11885
11886
11887 DEFUN ("trace-redisplay-toggle", Ftrace_redisplay_toggle,
11888 Strace_redisplay_toggle, 0, 0, "",
11889 "Toggle tracing of redisplay.")
11890 ()
11891 {
11892 trace_redisplay_p = !trace_redisplay_p;
11893 return Qnil;
11894 }
11895
11896
11897 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, 1, "",
11898 "Print STRING to stderr.")
11899 (string)
11900 Lisp_Object string;
11901 {
11902 CHECK_STRING (string, 0);
11903 fprintf (stderr, "%s", XSTRING (string)->data);
11904 return Qnil;
11905 }
11906
11907 #endif /* GLYPH_DEBUG */
11908
11909
11910 \f
11911 /***********************************************************************
11912 Building Desired Matrix Rows
11913 ***********************************************************************/
11914
11915 /* Return a temporary glyph row holding the glyphs of an overlay
11916 arrow. Only used for non-window-redisplay windows. */
11917
11918 static struct glyph_row *
11919 get_overlay_arrow_glyph_row (w)
11920 struct window *w;
11921 {
11922 struct frame *f = XFRAME (WINDOW_FRAME (w));
11923 struct buffer *buffer = XBUFFER (w->buffer);
11924 struct buffer *old = current_buffer;
11925 unsigned char *arrow_string = XSTRING (Voverlay_arrow_string)->data;
11926 int arrow_len = XSTRING (Voverlay_arrow_string)->size;
11927 unsigned char *arrow_end = arrow_string + arrow_len;
11928 unsigned char *p;
11929 struct it it;
11930 int multibyte_p;
11931 int n_glyphs_before;
11932
11933 set_buffer_temp (buffer);
11934 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
11935 it.glyph_row->used[TEXT_AREA] = 0;
11936 SET_TEXT_POS (it.position, 0, 0);
11937
11938 multibyte_p = !NILP (buffer->enable_multibyte_characters);
11939 p = arrow_string;
11940 while (p < arrow_end)
11941 {
11942 Lisp_Object face, ilisp;
11943
11944 /* Get the next character. */
11945 if (multibyte_p)
11946 it.c = string_char_and_length (p, arrow_len, &it.len);
11947 else
11948 it.c = *p, it.len = 1;
11949 p += it.len;
11950
11951 /* Get its face. */
11952 ilisp = make_number (p - arrow_string);
11953 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
11954 it.face_id = compute_char_face (f, it.c, face);
11955
11956 /* Compute its width, get its glyphs. */
11957 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
11958 SET_TEXT_POS (it.position, -1, -1);
11959 PRODUCE_GLYPHS (&it);
11960
11961 /* If this character doesn't fit any more in the line, we have
11962 to remove some glyphs. */
11963 if (it.current_x > it.last_visible_x)
11964 {
11965 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
11966 break;
11967 }
11968 }
11969
11970 set_buffer_temp (old);
11971 return it.glyph_row;
11972 }
11973
11974
11975 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
11976 glyphs are only inserted for terminal frames since we can't really
11977 win with truncation glyphs when partially visible glyphs are
11978 involved. Which glyphs to insert is determined by
11979 produce_special_glyphs. */
11980
11981 static void
11982 insert_left_trunc_glyphs (it)
11983 struct it *it;
11984 {
11985 struct it truncate_it;
11986 struct glyph *from, *end, *to, *toend;
11987
11988 xassert (!FRAME_WINDOW_P (it->f));
11989
11990 /* Get the truncation glyphs. */
11991 truncate_it = *it;
11992 truncate_it.current_x = 0;
11993 truncate_it.face_id = DEFAULT_FACE_ID;
11994 truncate_it.glyph_row = &scratch_glyph_row;
11995 truncate_it.glyph_row->used[TEXT_AREA] = 0;
11996 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
11997 truncate_it.object = make_number (0);
11998 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
11999
12000 /* Overwrite glyphs from IT with truncation glyphs. */
12001 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
12002 end = from + truncate_it.glyph_row->used[TEXT_AREA];
12003 to = it->glyph_row->glyphs[TEXT_AREA];
12004 toend = to + it->glyph_row->used[TEXT_AREA];
12005
12006 while (from < end)
12007 *to++ = *from++;
12008
12009 /* There may be padding glyphs left over. Overwrite them too. */
12010 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
12011 {
12012 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
12013 while (from < end)
12014 *to++ = *from++;
12015 }
12016
12017 if (to > toend)
12018 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
12019 }
12020
12021
12022 /* Compute the pixel height and width of IT->glyph_row.
12023
12024 Most of the time, ascent and height of a display line will be equal
12025 to the max_ascent and max_height values of the display iterator
12026 structure. This is not the case if
12027
12028 1. We hit ZV without displaying anything. In this case, max_ascent
12029 and max_height will be zero.
12030
12031 2. We have some glyphs that don't contribute to the line height.
12032 (The glyph row flag contributes_to_line_height_p is for future
12033 pixmap extensions).
12034
12035 The first case is easily covered by using default values because in
12036 these cases, the line height does not really matter, except that it
12037 must not be zero. */
12038
12039 static void
12040 compute_line_metrics (it)
12041 struct it *it;
12042 {
12043 struct glyph_row *row = it->glyph_row;
12044 int area, i;
12045
12046 if (FRAME_WINDOW_P (it->f))
12047 {
12048 int i, header_line_height;
12049
12050 /* The line may consist of one space only, that was added to
12051 place the cursor on it. If so, the row's height hasn't been
12052 computed yet. */
12053 if (row->height == 0)
12054 {
12055 if (it->max_ascent + it->max_descent == 0)
12056 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
12057 row->ascent = it->max_ascent;
12058 row->height = it->max_ascent + it->max_descent;
12059 row->phys_ascent = it->max_phys_ascent;
12060 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
12061 }
12062
12063 /* Compute the width of this line. */
12064 row->pixel_width = row->x;
12065 for (i = 0; i < row->used[TEXT_AREA]; ++i)
12066 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
12067
12068 xassert (row->pixel_width >= 0);
12069 xassert (row->ascent >= 0 && row->height > 0);
12070
12071 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
12072 || MATRIX_ROW_OVERLAPS_PRED_P (row));
12073
12074 /* If first line's physical ascent is larger than its logical
12075 ascent, use the physical ascent, and make the row taller.
12076 This makes accented characters fully visible. */
12077 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
12078 && row->phys_ascent > row->ascent)
12079 {
12080 row->height += row->phys_ascent - row->ascent;
12081 row->ascent = row->phys_ascent;
12082 }
12083
12084 /* Compute how much of the line is visible. */
12085 row->visible_height = row->height;
12086
12087 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (it->w);
12088 if (row->y < header_line_height)
12089 row->visible_height -= header_line_height - row->y;
12090 else
12091 {
12092 int max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
12093 if (row->y + row->height > max_y)
12094 row->visible_height -= row->y + row->height - max_y;
12095 }
12096 }
12097 else
12098 {
12099 row->pixel_width = row->used[TEXT_AREA];
12100 row->ascent = row->phys_ascent = 0;
12101 row->height = row->phys_height = row->visible_height = 1;
12102 }
12103
12104 /* Compute a hash code for this row. */
12105 row->hash = 0;
12106 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12107 for (i = 0; i < row->used[area]; ++i)
12108 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
12109 + row->glyphs[area][i].u.val
12110 + row->glyphs[area][i].face_id
12111 + row->glyphs[area][i].padding_p
12112 + (row->glyphs[area][i].type << 2));
12113
12114 it->max_ascent = it->max_descent = 0;
12115 it->max_phys_ascent = it->max_phys_descent = 0;
12116 }
12117
12118
12119 /* Append one space to the glyph row of iterator IT if doing a
12120 window-based redisplay. DEFAULT_FACE_P non-zero means let the
12121 space have the default face, otherwise let it have the same face as
12122 IT->face_id. Value is non-zero if a space was added.
12123
12124 This function is called to make sure that there is always one glyph
12125 at the end of a glyph row that the cursor can be set on under
12126 window-systems. (If there weren't such a glyph we would not know
12127 how wide and tall a box cursor should be displayed).
12128
12129 At the same time this space let's a nicely handle clearing to the
12130 end of the line if the row ends in italic text. */
12131
12132 static int
12133 append_space (it, default_face_p)
12134 struct it *it;
12135 int default_face_p;
12136 {
12137 if (FRAME_WINDOW_P (it->f))
12138 {
12139 int n = it->glyph_row->used[TEXT_AREA];
12140
12141 if (it->glyph_row->glyphs[TEXT_AREA] + n
12142 < it->glyph_row->glyphs[1 + TEXT_AREA])
12143 {
12144 /* Save some values that must not be changed.
12145 Must save IT->c and IT->len because otherwise
12146 ITERATOR_AT_END_P wouldn't work anymore after
12147 append_space has been called. */
12148 enum display_element_type saved_what = it->what;
12149 int saved_c = it->c, saved_len = it->len;
12150 int saved_x = it->current_x;
12151 int saved_face_id = it->face_id;
12152 struct text_pos saved_pos;
12153 Lisp_Object saved_object;
12154 struct face *face;
12155
12156 saved_object = it->object;
12157 saved_pos = it->position;
12158
12159 it->what = IT_CHARACTER;
12160 bzero (&it->position, sizeof it->position);
12161 it->object = make_number (0);
12162 it->c = ' ';
12163 it->len = 1;
12164
12165 if (default_face_p)
12166 it->face_id = DEFAULT_FACE_ID;
12167 else if (it->face_before_selective_p)
12168 it->face_id = it->saved_face_id;
12169 face = FACE_FROM_ID (it->f, it->face_id);
12170 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
12171
12172 PRODUCE_GLYPHS (it);
12173
12174 it->current_x = saved_x;
12175 it->object = saved_object;
12176 it->position = saved_pos;
12177 it->what = saved_what;
12178 it->face_id = saved_face_id;
12179 it->len = saved_len;
12180 it->c = saved_c;
12181 return 1;
12182 }
12183 }
12184
12185 return 0;
12186 }
12187
12188
12189 /* Extend the face of the last glyph in the text area of IT->glyph_row
12190 to the end of the display line. Called from display_line.
12191 If the glyph row is empty, add a space glyph to it so that we
12192 know the face to draw. Set the glyph row flag fill_line_p. */
12193
12194 static void
12195 extend_face_to_end_of_line (it)
12196 struct it *it;
12197 {
12198 struct face *face;
12199 struct frame *f = it->f;
12200
12201 /* If line is already filled, do nothing. */
12202 if (it->current_x >= it->last_visible_x)
12203 return;
12204
12205 /* Face extension extends the background and box of IT->face_id
12206 to the end of the line. If the background equals the background
12207 of the frame, we don't have to do anything. */
12208 if (it->face_before_selective_p)
12209 face = FACE_FROM_ID (it->f, it->saved_face_id);
12210 else
12211 face = FACE_FROM_ID (f, it->face_id);
12212
12213 if (FRAME_WINDOW_P (f)
12214 && face->box == FACE_NO_BOX
12215 && face->background == FRAME_BACKGROUND_PIXEL (f)
12216 && !face->stipple)
12217 return;
12218
12219 /* Set the glyph row flag indicating that the face of the last glyph
12220 in the text area has to be drawn to the end of the text area. */
12221 it->glyph_row->fill_line_p = 1;
12222
12223 /* If current character of IT is not ASCII, make sure we have the
12224 ASCII face. This will be automatically undone the next time
12225 get_next_display_element returns a multibyte character. Note
12226 that the character will always be single byte in unibyte text. */
12227 if (!SINGLE_BYTE_CHAR_P (it->c))
12228 {
12229 it->face_id = FACE_FOR_CHAR (f, face, 0);
12230 }
12231
12232 if (FRAME_WINDOW_P (f))
12233 {
12234 /* If the row is empty, add a space with the current face of IT,
12235 so that we know which face to draw. */
12236 if (it->glyph_row->used[TEXT_AREA] == 0)
12237 {
12238 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
12239 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
12240 it->glyph_row->used[TEXT_AREA] = 1;
12241 }
12242 }
12243 else
12244 {
12245 /* Save some values that must not be changed. */
12246 int saved_x = it->current_x;
12247 struct text_pos saved_pos;
12248 Lisp_Object saved_object;
12249 enum display_element_type saved_what = it->what;
12250 int saved_face_id = it->face_id;
12251
12252 saved_object = it->object;
12253 saved_pos = it->position;
12254
12255 it->what = IT_CHARACTER;
12256 bzero (&it->position, sizeof it->position);
12257 it->object = make_number (0);
12258 it->c = ' ';
12259 it->len = 1;
12260 it->face_id = face->id;
12261
12262 PRODUCE_GLYPHS (it);
12263
12264 while (it->current_x <= it->last_visible_x)
12265 PRODUCE_GLYPHS (it);
12266
12267 /* Don't count these blanks really. It would let us insert a left
12268 truncation glyph below and make us set the cursor on them, maybe. */
12269 it->current_x = saved_x;
12270 it->object = saved_object;
12271 it->position = saved_pos;
12272 it->what = saved_what;
12273 it->face_id = saved_face_id;
12274 }
12275 }
12276
12277
12278 /* Value is non-zero if text starting at CHARPOS in current_buffer is
12279 trailing whitespace. */
12280
12281 static int
12282 trailing_whitespace_p (charpos)
12283 int charpos;
12284 {
12285 int bytepos = CHAR_TO_BYTE (charpos);
12286 int c = 0;
12287
12288 while (bytepos < ZV_BYTE
12289 && (c = FETCH_CHAR (bytepos),
12290 c == ' ' || c == '\t'))
12291 ++bytepos;
12292
12293 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
12294 {
12295 if (bytepos != PT_BYTE)
12296 return 1;
12297 }
12298 return 0;
12299 }
12300
12301
12302 /* Highlight trailing whitespace, if any, in ROW. */
12303
12304 void
12305 highlight_trailing_whitespace (f, row)
12306 struct frame *f;
12307 struct glyph_row *row;
12308 {
12309 int used = row->used[TEXT_AREA];
12310
12311 if (used)
12312 {
12313 struct glyph *start = row->glyphs[TEXT_AREA];
12314 struct glyph *glyph = start + used - 1;
12315
12316 /* Skip over glyphs inserted to display the cursor at the
12317 end of a line, for extending the face of the last glyph
12318 to the end of the line on terminals, and for truncation
12319 and continuation glyphs. */
12320 while (glyph >= start
12321 && glyph->type == CHAR_GLYPH
12322 && INTEGERP (glyph->object))
12323 --glyph;
12324
12325 /* If last glyph is a space or stretch, and it's trailing
12326 whitespace, set the face of all trailing whitespace glyphs in
12327 IT->glyph_row to `trailing-whitespace'. */
12328 if (glyph >= start
12329 && BUFFERP (glyph->object)
12330 && (glyph->type == STRETCH_GLYPH
12331 || (glyph->type == CHAR_GLYPH
12332 && glyph->u.ch == ' '))
12333 && trailing_whitespace_p (glyph->charpos))
12334 {
12335 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
12336
12337 while (glyph >= start
12338 && BUFFERP (glyph->object)
12339 && (glyph->type == STRETCH_GLYPH
12340 || (glyph->type == CHAR_GLYPH
12341 && glyph->u.ch == ' ')))
12342 (glyph--)->face_id = face_id;
12343 }
12344 }
12345 }
12346
12347
12348 /* Value is non-zero if glyph row ROW in window W should be
12349 used to hold the cursor. */
12350
12351 static int
12352 cursor_row_p (w, row)
12353 struct window *w;
12354 struct glyph_row *row;
12355 {
12356 int cursor_row_p = 1;
12357
12358 if (PT == MATRIX_ROW_END_CHARPOS (row))
12359 {
12360 /* If the row ends with a newline from a string, we don't want
12361 the cursor there (if the row is continued it doesn't end in a
12362 newline). */
12363 if (CHARPOS (row->end.string_pos) >= 0
12364 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12365 cursor_row_p = row->continued_p;
12366
12367 /* If the row ends at ZV, display the cursor at the end of that
12368 row instead of at the start of the row below. */
12369 else if (row->ends_at_zv_p)
12370 cursor_row_p = 1;
12371 else
12372 cursor_row_p = 0;
12373 }
12374
12375 return cursor_row_p;
12376 }
12377
12378
12379 /* Construct the glyph row IT->glyph_row in the desired matrix of
12380 IT->w from text at the current position of IT. See dispextern.h
12381 for an overview of struct it. Value is non-zero if
12382 IT->glyph_row displays text, as opposed to a line displaying ZV
12383 only. */
12384
12385 static int
12386 display_line (it)
12387 struct it *it;
12388 {
12389 struct glyph_row *row = it->glyph_row;
12390
12391 /* We always start displaying at hpos zero even if hscrolled. */
12392 xassert (it->hpos == 0 && it->current_x == 0);
12393
12394 /* We must not display in a row that's not a text row. */
12395 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
12396 < it->w->desired_matrix->nrows);
12397
12398 /* Is IT->w showing the region? */
12399 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
12400
12401 /* Clear the result glyph row and enable it. */
12402 prepare_desired_row (row);
12403
12404 row->y = it->current_y;
12405 row->start = it->current;
12406 row->continuation_lines_width = it->continuation_lines_width;
12407 row->displays_text_p = 1;
12408 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
12409 it->starts_in_middle_of_char_p = 0;
12410
12411 /* Arrange the overlays nicely for our purposes. Usually, we call
12412 display_line on only one line at a time, in which case this
12413 can't really hurt too much, or we call it on lines which appear
12414 one after another in the buffer, in which case all calls to
12415 recenter_overlay_lists but the first will be pretty cheap. */
12416 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
12417
12418 /* Move over display elements that are not visible because we are
12419 hscrolled. This may stop at an x-position < IT->first_visible_x
12420 if the first glyph is partially visible or if we hit a line end. */
12421 if (it->current_x < it->first_visible_x)
12422 move_it_in_display_line_to (it, ZV, it->first_visible_x,
12423 MOVE_TO_POS | MOVE_TO_X);
12424
12425 /* Get the initial row height. This is either the height of the
12426 text hscrolled, if there is any, or zero. */
12427 row->ascent = it->max_ascent;
12428 row->height = it->max_ascent + it->max_descent;
12429 row->phys_ascent = it->max_phys_ascent;
12430 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
12431
12432 /* Loop generating characters. The loop is left with IT on the next
12433 character to display. */
12434 while (1)
12435 {
12436 int n_glyphs_before, hpos_before, x_before;
12437 int x, i, nglyphs;
12438 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
12439
12440 /* Retrieve the next thing to display. Value is zero if end of
12441 buffer reached. */
12442 if (!get_next_display_element (it))
12443 {
12444 /* Maybe add a space at the end of this line that is used to
12445 display the cursor there under X. Set the charpos of the
12446 first glyph of blank lines not corresponding to any text
12447 to -1. */
12448 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
12449 || row->used[TEXT_AREA] == 0)
12450 {
12451 row->glyphs[TEXT_AREA]->charpos = -1;
12452 row->displays_text_p = 0;
12453
12454 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines))
12455 row->indicate_empty_line_p = 1;
12456 }
12457
12458 it->continuation_lines_width = 0;
12459 row->ends_at_zv_p = 1;
12460 break;
12461 }
12462
12463 /* Now, get the metrics of what we want to display. This also
12464 generates glyphs in `row' (which is IT->glyph_row). */
12465 n_glyphs_before = row->used[TEXT_AREA];
12466 x = it->current_x;
12467
12468 /* Remember the line height so far in case the next element doesn't
12469 fit on the line. */
12470 if (!it->truncate_lines_p)
12471 {
12472 ascent = it->max_ascent;
12473 descent = it->max_descent;
12474 phys_ascent = it->max_phys_ascent;
12475 phys_descent = it->max_phys_descent;
12476 }
12477
12478 PRODUCE_GLYPHS (it);
12479
12480 /* If this display element was in marginal areas, continue with
12481 the next one. */
12482 if (it->area != TEXT_AREA)
12483 {
12484 row->ascent = max (row->ascent, it->max_ascent);
12485 row->height = max (row->height, it->max_ascent + it->max_descent);
12486 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12487 row->phys_height = max (row->phys_height,
12488 it->max_phys_ascent + it->max_phys_descent);
12489 set_iterator_to_next (it, 1);
12490 continue;
12491 }
12492
12493 /* Does the display element fit on the line? If we truncate
12494 lines, we should draw past the right edge of the window. If
12495 we don't truncate, we want to stop so that we can display the
12496 continuation glyph before the right margin. If lines are
12497 continued, there are two possible strategies for characters
12498 resulting in more than 1 glyph (e.g. tabs): Display as many
12499 glyphs as possible in this line and leave the rest for the
12500 continuation line, or display the whole element in the next
12501 line. Original redisplay did the former, so we do it also. */
12502 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
12503 hpos_before = it->hpos;
12504 x_before = x;
12505
12506 if (/* Not a newline. */
12507 nglyphs > 0
12508 /* Glyphs produced fit entirely in the line. */
12509 && it->current_x < it->last_visible_x)
12510 {
12511 it->hpos += nglyphs;
12512 row->ascent = max (row->ascent, it->max_ascent);
12513 row->height = max (row->height, it->max_ascent + it->max_descent);
12514 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12515 row->phys_height = max (row->phys_height,
12516 it->max_phys_ascent + it->max_phys_descent);
12517 if (it->current_x - it->pixel_width < it->first_visible_x)
12518 row->x = x - it->first_visible_x;
12519 }
12520 else
12521 {
12522 int new_x;
12523 struct glyph *glyph;
12524
12525 for (i = 0; i < nglyphs; ++i, x = new_x)
12526 {
12527 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12528 new_x = x + glyph->pixel_width;
12529
12530 if (/* Lines are continued. */
12531 !it->truncate_lines_p
12532 && (/* Glyph doesn't fit on the line. */
12533 new_x > it->last_visible_x
12534 /* Or it fits exactly on a window system frame. */
12535 || (new_x == it->last_visible_x
12536 && FRAME_WINDOW_P (it->f))))
12537 {
12538 /* End of a continued line. */
12539
12540 if (it->hpos == 0
12541 || (new_x == it->last_visible_x
12542 && FRAME_WINDOW_P (it->f)))
12543 {
12544 /* Current glyph is the only one on the line or
12545 fits exactly on the line. We must continue
12546 the line because we can't draw the cursor
12547 after the glyph. */
12548 row->continued_p = 1;
12549 it->current_x = new_x;
12550 it->continuation_lines_width += new_x;
12551 ++it->hpos;
12552 if (i == nglyphs - 1)
12553 set_iterator_to_next (it, 1);
12554 }
12555 else if (CHAR_GLYPH_PADDING_P (*glyph)
12556 && !FRAME_WINDOW_P (it->f))
12557 {
12558 /* A padding glyph that doesn't fit on this line.
12559 This means the whole character doesn't fit
12560 on the line. */
12561 row->used[TEXT_AREA] = n_glyphs_before;
12562
12563 /* Fill the rest of the row with continuation
12564 glyphs like in 20.x. */
12565 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
12566 < row->glyphs[1 + TEXT_AREA])
12567 produce_special_glyphs (it, IT_CONTINUATION);
12568
12569 row->continued_p = 1;
12570 it->current_x = x_before;
12571 it->continuation_lines_width += x_before;
12572
12573 /* Restore the height to what it was before the
12574 element not fitting on the line. */
12575 it->max_ascent = ascent;
12576 it->max_descent = descent;
12577 it->max_phys_ascent = phys_ascent;
12578 it->max_phys_descent = phys_descent;
12579 }
12580 else
12581 {
12582 /* Display element draws past the right edge of
12583 the window. Restore positions to values
12584 before the element. The next line starts
12585 with current_x before the glyph that could
12586 not be displayed, so that TAB works right. */
12587 row->used[TEXT_AREA] = n_glyphs_before + i;
12588
12589 /* Display continuation glyphs. */
12590 if (!FRAME_WINDOW_P (it->f))
12591 produce_special_glyphs (it, IT_CONTINUATION);
12592 row->continued_p = 1;
12593
12594 it->current_x = x;
12595 it->continuation_lines_width += x;
12596 if (nglyphs > 1 && i > 0)
12597 {
12598 row->ends_in_middle_of_char_p = 1;
12599 it->starts_in_middle_of_char_p = 1;
12600 }
12601
12602 /* Restore the height to what it was before the
12603 element not fitting on the line. */
12604 it->max_ascent = ascent;
12605 it->max_descent = descent;
12606 it->max_phys_ascent = phys_ascent;
12607 it->max_phys_descent = phys_descent;
12608 }
12609
12610 break;
12611 }
12612 else if (new_x > it->first_visible_x)
12613 {
12614 /* Increment number of glyphs actually displayed. */
12615 ++it->hpos;
12616
12617 if (x < it->first_visible_x)
12618 /* Glyph is partially visible, i.e. row starts at
12619 negative X position. */
12620 row->x = x - it->first_visible_x;
12621 }
12622 else
12623 {
12624 /* Glyph is completely off the left margin of the
12625 window. This should not happen because of the
12626 move_it_in_display_line at the start of
12627 this function. */
12628 abort ();
12629 }
12630 }
12631
12632 row->ascent = max (row->ascent, it->max_ascent);
12633 row->height = max (row->height, it->max_ascent + it->max_descent);
12634 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12635 row->phys_height = max (row->phys_height,
12636 it->max_phys_ascent + it->max_phys_descent);
12637
12638 /* End of this display line if row is continued. */
12639 if (row->continued_p)
12640 break;
12641 }
12642
12643 /* Is this a line end? If yes, we're also done, after making
12644 sure that a non-default face is extended up to the right
12645 margin of the window. */
12646 if (ITERATOR_AT_END_OF_LINE_P (it))
12647 {
12648 int used_before = row->used[TEXT_AREA];
12649
12650 /* Add a space at the end of the line that is used to
12651 display the cursor there. */
12652 append_space (it, 0);
12653
12654 /* Extend the face to the end of the line. */
12655 extend_face_to_end_of_line (it);
12656
12657 /* Make sure we have the position. */
12658 if (used_before == 0)
12659 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
12660
12661 /* Consume the line end. This skips over invisible lines. */
12662 set_iterator_to_next (it, 1);
12663 it->continuation_lines_width = 0;
12664 break;
12665 }
12666
12667 /* Proceed with next display element. Note that this skips
12668 over lines invisible because of selective display. */
12669 set_iterator_to_next (it, 1);
12670
12671 /* If we truncate lines, we are done when the last displayed
12672 glyphs reach past the right margin of the window. */
12673 if (it->truncate_lines_p
12674 && (FRAME_WINDOW_P (it->f)
12675 ? (it->current_x >= it->last_visible_x)
12676 : (it->current_x > it->last_visible_x)))
12677 {
12678 /* Maybe add truncation glyphs. */
12679 if (!FRAME_WINDOW_P (it->f))
12680 {
12681 int i, n;
12682
12683 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
12684 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
12685 break;
12686
12687 for (n = row->used[TEXT_AREA]; i < n; ++i)
12688 {
12689 row->used[TEXT_AREA] = i;
12690 produce_special_glyphs (it, IT_TRUNCATION);
12691 }
12692 }
12693
12694 row->truncated_on_right_p = 1;
12695 it->continuation_lines_width = 0;
12696 reseat_at_next_visible_line_start (it, 0);
12697 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
12698 it->hpos = hpos_before;
12699 it->current_x = x_before;
12700 break;
12701 }
12702 }
12703
12704 /* If line is not empty and hscrolled, maybe insert truncation glyphs
12705 at the left window margin. */
12706 if (it->first_visible_x
12707 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
12708 {
12709 if (!FRAME_WINDOW_P (it->f))
12710 insert_left_trunc_glyphs (it);
12711 row->truncated_on_left_p = 1;
12712 }
12713
12714 /* If the start of this line is the overlay arrow-position, then
12715 mark this glyph row as the one containing the overlay arrow.
12716 This is clearly a mess with variable size fonts. It would be
12717 better to let it be displayed like cursors under X. */
12718 if (MARKERP (Voverlay_arrow_position)
12719 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
12720 && (MATRIX_ROW_START_CHARPOS (row)
12721 == marker_position (Voverlay_arrow_position))
12722 && STRINGP (Voverlay_arrow_string)
12723 && ! overlay_arrow_seen)
12724 {
12725 /* Overlay arrow in window redisplay is a bitmap. */
12726 if (!FRAME_WINDOW_P (it->f))
12727 {
12728 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
12729 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
12730 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
12731 struct glyph *p = row->glyphs[TEXT_AREA];
12732 struct glyph *p2, *end;
12733
12734 /* Copy the arrow glyphs. */
12735 while (glyph < arrow_end)
12736 *p++ = *glyph++;
12737
12738 /* Throw away padding glyphs. */
12739 p2 = p;
12740 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
12741 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
12742 ++p2;
12743 if (p2 > p)
12744 {
12745 while (p2 < end)
12746 *p++ = *p2++;
12747 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
12748 }
12749 }
12750
12751 overlay_arrow_seen = 1;
12752 row->overlay_arrow_p = 1;
12753 }
12754
12755 /* Compute pixel dimensions of this line. */
12756 compute_line_metrics (it);
12757
12758 /* Remember the position at which this line ends. */
12759 row->end = it->current;
12760
12761 /* Maybe set the cursor. */
12762 if (it->w->cursor.vpos < 0
12763 && PT >= MATRIX_ROW_START_CHARPOS (row)
12764 && PT <= MATRIX_ROW_END_CHARPOS (row)
12765 && cursor_row_p (it->w, row))
12766 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
12767
12768 /* Highlight trailing whitespace. */
12769 if (!NILP (Vshow_trailing_whitespace))
12770 highlight_trailing_whitespace (it->f, it->glyph_row);
12771
12772 /* Prepare for the next line. This line starts horizontally at (X
12773 HPOS) = (0 0). Vertical positions are incremented. As a
12774 convenience for the caller, IT->glyph_row is set to the next
12775 row to be used. */
12776 it->current_x = it->hpos = 0;
12777 it->current_y += row->height;
12778 ++it->vpos;
12779 ++it->glyph_row;
12780 return row->displays_text_p;
12781 }
12782
12783
12784 \f
12785 /***********************************************************************
12786 Menu Bar
12787 ***********************************************************************/
12788
12789 /* Redisplay the menu bar in the frame for window W.
12790
12791 The menu bar of X frames that don't have X toolkit support is
12792 displayed in a special window W->frame->menu_bar_window.
12793
12794 The menu bar of terminal frames is treated specially as far as
12795 glyph matrices are concerned. Menu bar lines are not part of
12796 windows, so the update is done directly on the frame matrix rows
12797 for the menu bar. */
12798
12799 static void
12800 display_menu_bar (w)
12801 struct window *w;
12802 {
12803 struct frame *f = XFRAME (WINDOW_FRAME (w));
12804 struct it it;
12805 Lisp_Object items;
12806 int i;
12807
12808 /* Don't do all this for graphical frames. */
12809 #ifdef HAVE_NTGUI
12810 if (!NILP (Vwindow_system))
12811 return;
12812 #endif
12813 #ifdef USE_X_TOOLKIT
12814 if (FRAME_X_P (f))
12815 return;
12816 #endif
12817 #ifdef macintosh
12818 if (FRAME_MAC_P (f))
12819 return;
12820 #endif
12821
12822 #ifdef USE_X_TOOLKIT
12823 xassert (!FRAME_WINDOW_P (f));
12824 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
12825 it.first_visible_x = 0;
12826 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
12827 #else /* not USE_X_TOOLKIT */
12828 if (FRAME_WINDOW_P (f))
12829 {
12830 /* Menu bar lines are displayed in the desired matrix of the
12831 dummy window menu_bar_window. */
12832 struct window *menu_w;
12833 xassert (WINDOWP (f->menu_bar_window));
12834 menu_w = XWINDOW (f->menu_bar_window);
12835 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
12836 MENU_FACE_ID);
12837 it.first_visible_x = 0;
12838 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
12839 }
12840 else
12841 {
12842 /* This is a TTY frame, i.e. character hpos/vpos are used as
12843 pixel x/y. */
12844 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
12845 MENU_FACE_ID);
12846 it.first_visible_x = 0;
12847 it.last_visible_x = FRAME_WIDTH (f);
12848 }
12849 #endif /* not USE_X_TOOLKIT */
12850
12851 if (! mode_line_inverse_video)
12852 /* Force the menu-bar to be displayed in the default face. */
12853 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
12854
12855 /* Clear all rows of the menu bar. */
12856 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
12857 {
12858 struct glyph_row *row = it.glyph_row + i;
12859 clear_glyph_row (row);
12860 row->enabled_p = 1;
12861 row->full_width_p = 1;
12862 }
12863
12864 /* Display all items of the menu bar. */
12865 items = FRAME_MENU_BAR_ITEMS (it.f);
12866 for (i = 0; i < XVECTOR (items)->size; i += 4)
12867 {
12868 Lisp_Object string;
12869
12870 /* Stop at nil string. */
12871 string = AREF (items, i + 1);
12872 if (NILP (string))
12873 break;
12874
12875 /* Remember where item was displayed. */
12876 AREF (items, i + 3) = make_number (it.hpos);
12877
12878 /* Display the item, pad with one space. */
12879 if (it.current_x < it.last_visible_x)
12880 display_string (NULL, string, Qnil, 0, 0, &it,
12881 XSTRING (string)->size + 1, 0, 0, -1);
12882 }
12883
12884 /* Fill out the line with spaces. */
12885 if (it.current_x < it.last_visible_x)
12886 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
12887
12888 /* Compute the total height of the lines. */
12889 compute_line_metrics (&it);
12890 }
12891
12892
12893 \f
12894 /***********************************************************************
12895 Mode Line
12896 ***********************************************************************/
12897
12898 /* Redisplay mode lines in the window tree whose root is WINDOW. If
12899 FORCE is non-zero, redisplay mode lines unconditionally.
12900 Otherwise, redisplay only mode lines that are garbaged. Value is
12901 the number of windows whose mode lines were redisplayed. */
12902
12903 static int
12904 redisplay_mode_lines (window, force)
12905 Lisp_Object window;
12906 int force;
12907 {
12908 int nwindows = 0;
12909
12910 while (!NILP (window))
12911 {
12912 struct window *w = XWINDOW (window);
12913
12914 if (WINDOWP (w->hchild))
12915 nwindows += redisplay_mode_lines (w->hchild, force);
12916 else if (WINDOWP (w->vchild))
12917 nwindows += redisplay_mode_lines (w->vchild, force);
12918 else if (force
12919 || FRAME_GARBAGED_P (XFRAME (w->frame))
12920 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
12921 {
12922 Lisp_Object old_selected_frame;
12923 struct text_pos lpoint;
12924 struct buffer *old = current_buffer;
12925
12926 /* Set the window's buffer for the mode line display. */
12927 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12928 set_buffer_internal_1 (XBUFFER (w->buffer));
12929
12930 /* Point refers normally to the selected window. For any
12931 other window, set up appropriate value. */
12932 if (!EQ (window, selected_window))
12933 {
12934 struct text_pos pt;
12935
12936 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
12937 if (CHARPOS (pt) < BEGV)
12938 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
12939 else if (CHARPOS (pt) > (ZV - 1))
12940 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
12941 else
12942 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
12943 }
12944
12945 /* Temporarily set up the selected frame. */
12946 old_selected_frame = selected_frame;
12947 selected_frame = w->frame;
12948
12949 /* Display mode lines. */
12950 clear_glyph_matrix (w->desired_matrix);
12951 if (display_mode_lines (w))
12952 {
12953 ++nwindows;
12954 w->must_be_updated_p = 1;
12955 }
12956
12957 /* Restore old settings. */
12958 selected_frame = old_selected_frame;
12959 set_buffer_internal_1 (old);
12960 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
12961 }
12962
12963 window = w->next;
12964 }
12965
12966 return nwindows;
12967 }
12968
12969
12970 /* Display the mode and/or top line of window W. Value is the number
12971 of mode lines displayed. */
12972
12973 static int
12974 display_mode_lines (w)
12975 struct window *w;
12976 {
12977 int n = 0;
12978
12979 /* These will be set while the mode line specs are processed. */
12980 line_number_displayed = 0;
12981 w->column_number_displayed = Qnil;
12982
12983 if (WINDOW_WANTS_MODELINE_P (w))
12984 {
12985 display_mode_line (w, MODE_LINE_FACE_ID,
12986 current_buffer->mode_line_format);
12987 ++n;
12988 }
12989
12990 if (WINDOW_WANTS_HEADER_LINE_P (w))
12991 {
12992 display_mode_line (w, HEADER_LINE_FACE_ID,
12993 current_buffer->header_line_format);
12994 ++n;
12995 }
12996
12997 return n;
12998 }
12999
13000
13001 /* Display mode or top line of window W. FACE_ID specifies which line
13002 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
13003 FORMAT is the mode line format to display. Value is the pixel
13004 height of the mode line displayed. */
13005
13006 static int
13007 display_mode_line (w, face_id, format)
13008 struct window *w;
13009 enum face_id face_id;
13010 Lisp_Object format;
13011 {
13012 struct it it;
13013 struct face *face;
13014
13015 init_iterator (&it, w, -1, -1, NULL, face_id);
13016 prepare_desired_row (it.glyph_row);
13017
13018 if (! mode_line_inverse_video)
13019 /* Force the mode-line to be displayed in the default face. */
13020 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
13021
13022 /* Temporarily make frame's keyboard the current kboard so that
13023 kboard-local variables in the mode_line_format will get the right
13024 values. */
13025 push_frame_kboard (it.f);
13026 display_mode_element (&it, 0, 0, 0, format);
13027 pop_frame_kboard ();
13028
13029 /* Fill up with spaces. */
13030 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
13031
13032 compute_line_metrics (&it);
13033 it.glyph_row->full_width_p = 1;
13034 it.glyph_row->mode_line_p = 1;
13035 it.glyph_row->inverse_p = 0;
13036 it.glyph_row->continued_p = 0;
13037 it.glyph_row->truncated_on_left_p = 0;
13038 it.glyph_row->truncated_on_right_p = 0;
13039
13040 /* Make a 3D mode-line have a shadow at its right end. */
13041 face = FACE_FROM_ID (it.f, face_id);
13042 extend_face_to_end_of_line (&it);
13043 if (face->box != FACE_NO_BOX)
13044 {
13045 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
13046 + it.glyph_row->used[TEXT_AREA] - 1);
13047 last->right_box_line_p = 1;
13048 }
13049
13050 return it.glyph_row->height;
13051 }
13052
13053
13054 /* Contribute ELT to the mode line for window IT->w. How it
13055 translates into text depends on its data type.
13056
13057 IT describes the display environment in which we display, as usual.
13058
13059 DEPTH is the depth in recursion. It is used to prevent
13060 infinite recursion here.
13061
13062 FIELD_WIDTH is the number of characters the display of ELT should
13063 occupy in the mode line, and PRECISION is the maximum number of
13064 characters to display from ELT's representation. See
13065 display_string for details. *
13066
13067 Returns the hpos of the end of the text generated by ELT. */
13068
13069 static int
13070 display_mode_element (it, depth, field_width, precision, elt)
13071 struct it *it;
13072 int depth;
13073 int field_width, precision;
13074 Lisp_Object elt;
13075 {
13076 int n = 0, field, prec;
13077
13078 tail_recurse:
13079 if (depth > 10)
13080 goto invalid;
13081
13082 depth++;
13083
13084 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
13085 {
13086 case Lisp_String:
13087 {
13088 /* A string: output it and check for %-constructs within it. */
13089 unsigned char c;
13090 unsigned char *this = XSTRING (elt)->data;
13091 unsigned char *lisp_string = this;
13092
13093 while ((precision <= 0 || n < precision)
13094 && *this
13095 && (frame_title_ptr
13096 || it->current_x < it->last_visible_x))
13097 {
13098 unsigned char *last = this;
13099
13100 /* Advance to end of string or next format specifier. */
13101 while ((c = *this++) != '\0' && c != '%')
13102 ;
13103
13104 if (this - 1 != last)
13105 {
13106 /* Output to end of string or up to '%'. Field width
13107 is length of string. Don't output more than
13108 PRECISION allows us. */
13109 --this;
13110 prec = strwidth (last, this - last);
13111 if (precision > 0 && prec > precision - n)
13112 prec = precision - n;
13113
13114 if (frame_title_ptr)
13115 n += store_frame_title (last, 0, prec);
13116 else
13117 n += display_string (NULL, elt, Qnil, 0, last - lisp_string,
13118 it, 0, prec, 0, -1);
13119 }
13120 else /* c == '%' */
13121 {
13122 unsigned char *percent_position = this;
13123
13124 /* Get the specified minimum width. Zero means
13125 don't pad. */
13126 field = 0;
13127 while ((c = *this++) >= '0' && c <= '9')
13128 field = field * 10 + c - '0';
13129
13130 /* Don't pad beyond the total padding allowed. */
13131 if (field_width - n > 0 && field > field_width - n)
13132 field = field_width - n;
13133
13134 /* Note that either PRECISION <= 0 or N < PRECISION. */
13135 prec = precision - n;
13136
13137 if (c == 'M')
13138 n += display_mode_element (it, depth, field, prec,
13139 Vglobal_mode_string);
13140 else if (c != 0)
13141 {
13142 unsigned char *spec
13143 = decode_mode_spec (it->w, c, field, prec);
13144
13145 if (frame_title_ptr)
13146 n += store_frame_title (spec, field, prec);
13147 else
13148 {
13149 int nglyphs_before
13150 = it->glyph_row->used[TEXT_AREA];
13151 int charpos
13152 = percent_position - XSTRING (elt)->data;
13153 int nwritten
13154 = display_string (spec, Qnil, elt, charpos, 0, it,
13155 field, prec, 0, -1);
13156
13157 /* Assign to the glyphs written above the
13158 string where the `%x' came from, position
13159 of the `%'. */
13160 if (nwritten > 0)
13161 {
13162 struct glyph *glyph
13163 = (it->glyph_row->glyphs[TEXT_AREA]
13164 + nglyphs_before);
13165 int i;
13166
13167 for (i = 0; i < nwritten; ++i)
13168 {
13169 glyph[i].object = elt;
13170 glyph[i].charpos = charpos;
13171 }
13172
13173 n += nwritten;
13174 }
13175 }
13176 }
13177 }
13178 }
13179 }
13180 break;
13181
13182 case Lisp_Symbol:
13183 /* A symbol: process the value of the symbol recursively
13184 as if it appeared here directly. Avoid error if symbol void.
13185 Special case: if value of symbol is a string, output the string
13186 literally. */
13187 {
13188 register Lisp_Object tem;
13189 tem = Fboundp (elt);
13190 if (!NILP (tem))
13191 {
13192 tem = Fsymbol_value (elt);
13193 /* If value is a string, output that string literally:
13194 don't check for % within it. */
13195 if (STRINGP (tem))
13196 {
13197 prec = precision - n;
13198 if (frame_title_ptr)
13199 n += store_frame_title (XSTRING (tem)->data, -1, prec);
13200 else
13201 n += display_string (NULL, tem, Qnil, 0, 0, it,
13202 0, prec, 0, -1);
13203 }
13204 else if (!EQ (tem, elt))
13205 {
13206 /* Give up right away for nil or t. */
13207 elt = tem;
13208 goto tail_recurse;
13209 }
13210 }
13211 }
13212 break;
13213
13214 case Lisp_Cons:
13215 {
13216 register Lisp_Object car, tem;
13217
13218 /* A cons cell: three distinct cases.
13219 If first element is a string or a cons, process all the elements
13220 and effectively concatenate them.
13221 If first element is a negative number, truncate displaying cdr to
13222 at most that many characters. If positive, pad (with spaces)
13223 to at least that many characters.
13224 If first element is a symbol, process the cadr or caddr recursively
13225 according to whether the symbol's value is non-nil or nil. */
13226 car = XCAR (elt);
13227 if (EQ (car, QCeval) && CONSP (XCDR (elt)))
13228 {
13229 /* An element of the form (:eval FORM) means evaluate FORM
13230 and use the result as mode line elements. */
13231 struct gcpro gcpro1;
13232 Lisp_Object spec;
13233
13234 spec = safe_eval (XCAR (XCDR (elt)));
13235 GCPRO1 (spec);
13236 n += display_mode_element (it, depth, field_width - n,
13237 precision - n, spec);
13238 UNGCPRO;
13239 }
13240 else if (SYMBOLP (car))
13241 {
13242 tem = Fboundp (car);
13243 elt = XCDR (elt);
13244 if (!CONSP (elt))
13245 goto invalid;
13246 /* elt is now the cdr, and we know it is a cons cell.
13247 Use its car if CAR has a non-nil value. */
13248 if (!NILP (tem))
13249 {
13250 tem = Fsymbol_value (car);
13251 if (!NILP (tem))
13252 {
13253 elt = XCAR (elt);
13254 goto tail_recurse;
13255 }
13256 }
13257 /* Symbol's value is nil (or symbol is unbound)
13258 Get the cddr of the original list
13259 and if possible find the caddr and use that. */
13260 elt = XCDR (elt);
13261 if (NILP (elt))
13262 break;
13263 else if (!CONSP (elt))
13264 goto invalid;
13265 elt = XCAR (elt);
13266 goto tail_recurse;
13267 }
13268 else if (INTEGERP (car))
13269 {
13270 register int lim = XINT (car);
13271 elt = XCDR (elt);
13272 if (lim < 0)
13273 {
13274 /* Negative int means reduce maximum width. */
13275 if (precision <= 0)
13276 precision = -lim;
13277 else
13278 precision = min (precision, -lim);
13279 }
13280 else if (lim > 0)
13281 {
13282 /* Padding specified. Don't let it be more than
13283 current maximum. */
13284 if (precision > 0)
13285 lim = min (precision, lim);
13286
13287 /* If that's more padding than already wanted, queue it.
13288 But don't reduce padding already specified even if
13289 that is beyond the current truncation point. */
13290 field_width = max (lim, field_width);
13291 }
13292 goto tail_recurse;
13293 }
13294 else if (STRINGP (car) || CONSP (car))
13295 {
13296 register int limit = 50;
13297 /* Limit is to protect against circular lists. */
13298 while (CONSP (elt)
13299 && --limit > 0
13300 && (precision <= 0 || n < precision))
13301 {
13302 n += display_mode_element (it, depth, field_width - n,
13303 precision - n, XCAR (elt));
13304 elt = XCDR (elt);
13305 }
13306 }
13307 }
13308 break;
13309
13310 default:
13311 invalid:
13312 if (frame_title_ptr)
13313 n += store_frame_title ("*invalid*", 0, precision - n);
13314 else
13315 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
13316 precision - n, 0, 0);
13317 return n;
13318 }
13319
13320 /* Pad to FIELD_WIDTH. */
13321 if (field_width > 0 && n < field_width)
13322 {
13323 if (frame_title_ptr)
13324 n += store_frame_title ("", field_width - n, 0);
13325 else
13326 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
13327 0, 0, 0);
13328 }
13329
13330 return n;
13331 }
13332
13333
13334 /* Write a null-terminated, right justified decimal representation of
13335 the positive integer D to BUF using a minimal field width WIDTH. */
13336
13337 static void
13338 pint2str (buf, width, d)
13339 register char *buf;
13340 register int width;
13341 register int d;
13342 {
13343 register char *p = buf;
13344
13345 if (d <= 0)
13346 *p++ = '0';
13347 else
13348 {
13349 while (d > 0)
13350 {
13351 *p++ = d % 10 + '0';
13352 d /= 10;
13353 }
13354 }
13355
13356 for (width -= (int) (p - buf); width > 0; --width)
13357 *p++ = ' ';
13358 *p-- = '\0';
13359 while (p > buf)
13360 {
13361 d = *buf;
13362 *buf++ = *p;
13363 *p-- = d;
13364 }
13365 }
13366
13367 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
13368 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
13369 type of CODING_SYSTEM. Return updated pointer into BUF. */
13370
13371 static unsigned char invalid_eol_type[] = "(*invalid*)";
13372
13373 static char *
13374 decode_mode_spec_coding (coding_system, buf, eol_flag)
13375 Lisp_Object coding_system;
13376 register char *buf;
13377 int eol_flag;
13378 {
13379 Lisp_Object val;
13380 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
13381 unsigned char *eol_str;
13382 int eol_str_len;
13383 /* The EOL conversion we are using. */
13384 Lisp_Object eoltype;
13385
13386 val = Fget (coding_system, Qcoding_system);
13387 eoltype = Qnil;
13388
13389 if (!VECTORP (val)) /* Not yet decided. */
13390 {
13391 if (multibyte)
13392 *buf++ = '-';
13393 if (eol_flag)
13394 eoltype = eol_mnemonic_undecided;
13395 /* Don't mention EOL conversion if it isn't decided. */
13396 }
13397 else
13398 {
13399 Lisp_Object eolvalue;
13400
13401 eolvalue = Fget (coding_system, Qeol_type);
13402
13403 if (multibyte)
13404 *buf++ = XFASTINT (AREF (val, 1));
13405
13406 if (eol_flag)
13407 {
13408 /* The EOL conversion that is normal on this system. */
13409
13410 if (NILP (eolvalue)) /* Not yet decided. */
13411 eoltype = eol_mnemonic_undecided;
13412 else if (VECTORP (eolvalue)) /* Not yet decided. */
13413 eoltype = eol_mnemonic_undecided;
13414 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
13415 eoltype = (XFASTINT (eolvalue) == 0
13416 ? eol_mnemonic_unix
13417 : (XFASTINT (eolvalue) == 1
13418 ? eol_mnemonic_dos : eol_mnemonic_mac));
13419 }
13420 }
13421
13422 if (eol_flag)
13423 {
13424 /* Mention the EOL conversion if it is not the usual one. */
13425 if (STRINGP (eoltype))
13426 {
13427 eol_str = XSTRING (eoltype)->data;
13428 eol_str_len = XSTRING (eoltype)->size;
13429 }
13430 else if (INTEGERP (eoltype)
13431 && CHAR_VALID_P (XINT (eoltype), 0))
13432 {
13433 eol_str = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
13434 eol_str_len = CHAR_STRING (XINT (eoltype), eol_str);
13435 }
13436 else
13437 {
13438 eol_str = invalid_eol_type;
13439 eol_str_len = sizeof (invalid_eol_type) - 1;
13440 }
13441 bcopy (eol_str, buf, eol_str_len);
13442 buf += eol_str_len;
13443 }
13444
13445 return buf;
13446 }
13447
13448 /* Return a string for the output of a mode line %-spec for window W,
13449 generated by character C. PRECISION >= 0 means don't return a
13450 string longer than that value. FIELD_WIDTH > 0 means pad the
13451 string returned with spaces to that value. */
13452
13453 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
13454
13455 static char *
13456 decode_mode_spec (w, c, field_width, precision)
13457 struct window *w;
13458 register int c;
13459 int field_width, precision;
13460 {
13461 Lisp_Object obj;
13462 struct frame *f = XFRAME (WINDOW_FRAME (w));
13463 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
13464 struct buffer *b = XBUFFER (w->buffer);
13465
13466 obj = Qnil;
13467
13468 switch (c)
13469 {
13470 case '*':
13471 if (!NILP (b->read_only))
13472 return "%";
13473 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
13474 return "*";
13475 return "-";
13476
13477 case '+':
13478 /* This differs from %* only for a modified read-only buffer. */
13479 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
13480 return "*";
13481 if (!NILP (b->read_only))
13482 return "%";
13483 return "-";
13484
13485 case '&':
13486 /* This differs from %* in ignoring read-only-ness. */
13487 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
13488 return "*";
13489 return "-";
13490
13491 case '%':
13492 return "%";
13493
13494 case '[':
13495 {
13496 int i;
13497 char *p;
13498
13499 if (command_loop_level > 5)
13500 return "[[[... ";
13501 p = decode_mode_spec_buf;
13502 for (i = 0; i < command_loop_level; i++)
13503 *p++ = '[';
13504 *p = 0;
13505 return decode_mode_spec_buf;
13506 }
13507
13508 case ']':
13509 {
13510 int i;
13511 char *p;
13512
13513 if (command_loop_level > 5)
13514 return " ...]]]";
13515 p = decode_mode_spec_buf;
13516 for (i = 0; i < command_loop_level; i++)
13517 *p++ = ']';
13518 *p = 0;
13519 return decode_mode_spec_buf;
13520 }
13521
13522 case '-':
13523 {
13524 register int i;
13525
13526 /* Let lots_of_dashes be a string of infinite length. */
13527 if (field_width <= 0
13528 || field_width > sizeof (lots_of_dashes))
13529 {
13530 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
13531 decode_mode_spec_buf[i] = '-';
13532 decode_mode_spec_buf[i] = '\0';
13533 return decode_mode_spec_buf;
13534 }
13535 else
13536 return lots_of_dashes;
13537 }
13538
13539 case 'b':
13540 obj = b->name;
13541 break;
13542
13543 case 'c':
13544 {
13545 int col = current_column ();
13546 w->column_number_displayed = make_number (col);
13547 pint2str (decode_mode_spec_buf, field_width, col);
13548 return decode_mode_spec_buf;
13549 }
13550
13551 case 'F':
13552 /* %F displays the frame name. */
13553 if (!NILP (f->title))
13554 return (char *) XSTRING (f->title)->data;
13555 if (f->explicit_name || ! FRAME_WINDOW_P (f))
13556 return (char *) XSTRING (f->name)->data;
13557 return "Emacs";
13558
13559 case 'f':
13560 obj = b->filename;
13561 break;
13562
13563 case 'l':
13564 {
13565 int startpos = XMARKER (w->start)->charpos;
13566 int startpos_byte = marker_byte_position (w->start);
13567 int line, linepos, linepos_byte, topline;
13568 int nlines, junk;
13569 int height = XFASTINT (w->height);
13570
13571 /* If we decided that this buffer isn't suitable for line numbers,
13572 don't forget that too fast. */
13573 if (EQ (w->base_line_pos, w->buffer))
13574 goto no_value;
13575 /* But do forget it, if the window shows a different buffer now. */
13576 else if (BUFFERP (w->base_line_pos))
13577 w->base_line_pos = Qnil;
13578
13579 /* If the buffer is very big, don't waste time. */
13580 if (INTEGERP (Vline_number_display_limit)
13581 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
13582 {
13583 w->base_line_pos = Qnil;
13584 w->base_line_number = Qnil;
13585 goto no_value;
13586 }
13587
13588 if (!NILP (w->base_line_number)
13589 && !NILP (w->base_line_pos)
13590 && XFASTINT (w->base_line_pos) <= startpos)
13591 {
13592 line = XFASTINT (w->base_line_number);
13593 linepos = XFASTINT (w->base_line_pos);
13594 linepos_byte = buf_charpos_to_bytepos (b, linepos);
13595 }
13596 else
13597 {
13598 line = 1;
13599 linepos = BUF_BEGV (b);
13600 linepos_byte = BUF_BEGV_BYTE (b);
13601 }
13602
13603 /* Count lines from base line to window start position. */
13604 nlines = display_count_lines (linepos, linepos_byte,
13605 startpos_byte,
13606 startpos, &junk);
13607
13608 topline = nlines + line;
13609
13610 /* Determine a new base line, if the old one is too close
13611 or too far away, or if we did not have one.
13612 "Too close" means it's plausible a scroll-down would
13613 go back past it. */
13614 if (startpos == BUF_BEGV (b))
13615 {
13616 w->base_line_number = make_number (topline);
13617 w->base_line_pos = make_number (BUF_BEGV (b));
13618 }
13619 else if (nlines < height + 25 || nlines > height * 3 + 50
13620 || linepos == BUF_BEGV (b))
13621 {
13622 int limit = BUF_BEGV (b);
13623 int limit_byte = BUF_BEGV_BYTE (b);
13624 int position;
13625 int distance = (height * 2 + 30) * line_number_display_limit_width;
13626
13627 if (startpos - distance > limit)
13628 {
13629 limit = startpos - distance;
13630 limit_byte = CHAR_TO_BYTE (limit);
13631 }
13632
13633 nlines = display_count_lines (startpos, startpos_byte,
13634 limit_byte,
13635 - (height * 2 + 30),
13636 &position);
13637 /* If we couldn't find the lines we wanted within
13638 line_number_display_limit_width chars per line,
13639 give up on line numbers for this window. */
13640 if (position == limit_byte && limit == startpos - distance)
13641 {
13642 w->base_line_pos = w->buffer;
13643 w->base_line_number = Qnil;
13644 goto no_value;
13645 }
13646
13647 w->base_line_number = make_number (topline - nlines);
13648 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
13649 }
13650
13651 /* Now count lines from the start pos to point. */
13652 nlines = display_count_lines (startpos, startpos_byte,
13653 PT_BYTE, PT, &junk);
13654
13655 /* Record that we did display the line number. */
13656 line_number_displayed = 1;
13657
13658 /* Make the string to show. */
13659 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
13660 return decode_mode_spec_buf;
13661 no_value:
13662 {
13663 char* p = decode_mode_spec_buf;
13664 int pad = field_width - 2;
13665 while (pad-- > 0)
13666 *p++ = ' ';
13667 *p++ = '?';
13668 *p++ = '?';
13669 *p = '\0';
13670 return decode_mode_spec_buf;
13671 }
13672 }
13673 break;
13674
13675 case 'm':
13676 obj = b->mode_name;
13677 break;
13678
13679 case 'n':
13680 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
13681 return " Narrow";
13682 break;
13683
13684 case 'p':
13685 {
13686 int pos = marker_position (w->start);
13687 int total = BUF_ZV (b) - BUF_BEGV (b);
13688
13689 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
13690 {
13691 if (pos <= BUF_BEGV (b))
13692 return "All";
13693 else
13694 return "Bottom";
13695 }
13696 else if (pos <= BUF_BEGV (b))
13697 return "Top";
13698 else
13699 {
13700 if (total > 1000000)
13701 /* Do it differently for a large value, to avoid overflow. */
13702 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
13703 else
13704 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
13705 /* We can't normally display a 3-digit number,
13706 so get us a 2-digit number that is close. */
13707 if (total == 100)
13708 total = 99;
13709 sprintf (decode_mode_spec_buf, "%2d%%", total);
13710 return decode_mode_spec_buf;
13711 }
13712 }
13713
13714 /* Display percentage of size above the bottom of the screen. */
13715 case 'P':
13716 {
13717 int toppos = marker_position (w->start);
13718 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
13719 int total = BUF_ZV (b) - BUF_BEGV (b);
13720
13721 if (botpos >= BUF_ZV (b))
13722 {
13723 if (toppos <= BUF_BEGV (b))
13724 return "All";
13725 else
13726 return "Bottom";
13727 }
13728 else
13729 {
13730 if (total > 1000000)
13731 /* Do it differently for a large value, to avoid overflow. */
13732 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
13733 else
13734 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
13735 /* We can't normally display a 3-digit number,
13736 so get us a 2-digit number that is close. */
13737 if (total == 100)
13738 total = 99;
13739 if (toppos <= BUF_BEGV (b))
13740 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
13741 else
13742 sprintf (decode_mode_spec_buf, "%2d%%", total);
13743 return decode_mode_spec_buf;
13744 }
13745 }
13746
13747 case 's':
13748 /* status of process */
13749 obj = Fget_buffer_process (w->buffer);
13750 if (NILP (obj))
13751 return "no process";
13752 #ifdef subprocesses
13753 obj = Fsymbol_name (Fprocess_status (obj));
13754 #endif
13755 break;
13756
13757 case 't': /* indicate TEXT or BINARY */
13758 #ifdef MODE_LINE_BINARY_TEXT
13759 return MODE_LINE_BINARY_TEXT (b);
13760 #else
13761 return "T";
13762 #endif
13763
13764 case 'z':
13765 /* coding-system (not including end-of-line format) */
13766 case 'Z':
13767 /* coding-system (including end-of-line type) */
13768 {
13769 int eol_flag = (c == 'Z');
13770 char *p = decode_mode_spec_buf;
13771
13772 if (! FRAME_WINDOW_P (f))
13773 {
13774 /* No need to mention EOL here--the terminal never needs
13775 to do EOL conversion. */
13776 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
13777 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
13778 }
13779 p = decode_mode_spec_coding (b->buffer_file_coding_system,
13780 p, eol_flag);
13781
13782 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
13783 #ifdef subprocesses
13784 obj = Fget_buffer_process (Fcurrent_buffer ());
13785 if (PROCESSP (obj))
13786 {
13787 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
13788 p, eol_flag);
13789 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
13790 p, eol_flag);
13791 }
13792 #endif /* subprocesses */
13793 #endif /* 0 */
13794 *p = 0;
13795 return decode_mode_spec_buf;
13796 }
13797 }
13798
13799 if (STRINGP (obj))
13800 return (char *) XSTRING (obj)->data;
13801 else
13802 return "";
13803 }
13804
13805
13806 /* Count up to COUNT lines starting from START / START_BYTE.
13807 But don't go beyond LIMIT_BYTE.
13808 Return the number of lines thus found (always nonnegative).
13809
13810 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
13811
13812 static int
13813 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
13814 int start, start_byte, limit_byte, count;
13815 int *byte_pos_ptr;
13816 {
13817 register unsigned char *cursor;
13818 unsigned char *base;
13819
13820 register int ceiling;
13821 register unsigned char *ceiling_addr;
13822 int orig_count = count;
13823
13824 /* If we are not in selective display mode,
13825 check only for newlines. */
13826 int selective_display = (!NILP (current_buffer->selective_display)
13827 && !INTEGERP (current_buffer->selective_display));
13828
13829 if (count > 0)
13830 {
13831 while (start_byte < limit_byte)
13832 {
13833 ceiling = BUFFER_CEILING_OF (start_byte);
13834 ceiling = min (limit_byte - 1, ceiling);
13835 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
13836 base = (cursor = BYTE_POS_ADDR (start_byte));
13837 while (1)
13838 {
13839 if (selective_display)
13840 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
13841 ;
13842 else
13843 while (*cursor != '\n' && ++cursor != ceiling_addr)
13844 ;
13845
13846 if (cursor != ceiling_addr)
13847 {
13848 if (--count == 0)
13849 {
13850 start_byte += cursor - base + 1;
13851 *byte_pos_ptr = start_byte;
13852 return orig_count;
13853 }
13854 else
13855 if (++cursor == ceiling_addr)
13856 break;
13857 }
13858 else
13859 break;
13860 }
13861 start_byte += cursor - base;
13862 }
13863 }
13864 else
13865 {
13866 while (start_byte > limit_byte)
13867 {
13868 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
13869 ceiling = max (limit_byte, ceiling);
13870 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
13871 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
13872 while (1)
13873 {
13874 if (selective_display)
13875 while (--cursor != ceiling_addr
13876 && *cursor != '\n' && *cursor != 015)
13877 ;
13878 else
13879 while (--cursor != ceiling_addr && *cursor != '\n')
13880 ;
13881
13882 if (cursor != ceiling_addr)
13883 {
13884 if (++count == 0)
13885 {
13886 start_byte += cursor - base + 1;
13887 *byte_pos_ptr = start_byte;
13888 /* When scanning backwards, we should
13889 not count the newline posterior to which we stop. */
13890 return - orig_count - 1;
13891 }
13892 }
13893 else
13894 break;
13895 }
13896 /* Here we add 1 to compensate for the last decrement
13897 of CURSOR, which took it past the valid range. */
13898 start_byte += cursor - base + 1;
13899 }
13900 }
13901
13902 *byte_pos_ptr = limit_byte;
13903
13904 if (count < 0)
13905 return - orig_count + count;
13906 return orig_count - count;
13907
13908 }
13909
13910
13911 \f
13912 /***********************************************************************
13913 Displaying strings
13914 ***********************************************************************/
13915
13916 /* Display a NUL-terminated string, starting with index START.
13917
13918 If STRING is non-null, display that C string. Otherwise, the Lisp
13919 string LISP_STRING is displayed.
13920
13921 If FACE_STRING is not nil, FACE_STRING_POS is a position in
13922 FACE_STRING. Display STRING or LISP_STRING with the face at
13923 FACE_STRING_POS in FACE_STRING:
13924
13925 Display the string in the environment given by IT, but use the
13926 standard display table, temporarily.
13927
13928 FIELD_WIDTH is the minimum number of output glyphs to produce.
13929 If STRING has fewer characters than FIELD_WIDTH, pad to the right
13930 with spaces. If STRING has more characters, more than FIELD_WIDTH
13931 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
13932
13933 PRECISION is the maximum number of characters to output from
13934 STRING. PRECISION < 0 means don't truncate the string.
13935
13936 This is roughly equivalent to printf format specifiers:
13937
13938 FIELD_WIDTH PRECISION PRINTF
13939 ----------------------------------------
13940 -1 -1 %s
13941 -1 10 %.10s
13942 10 -1 %10s
13943 20 10 %20.10s
13944
13945 MULTIBYTE zero means do not display multibyte chars, > 0 means do
13946 display them, and < 0 means obey the current buffer's value of
13947 enable_multibyte_characters.
13948
13949 Value is the number of glyphs produced. */
13950
13951 static int
13952 display_string (string, lisp_string, face_string, face_string_pos,
13953 start, it, field_width, precision, max_x, multibyte)
13954 unsigned char *string;
13955 Lisp_Object lisp_string;
13956 Lisp_Object face_string;
13957 int face_string_pos;
13958 int start;
13959 struct it *it;
13960 int field_width, precision, max_x;
13961 int multibyte;
13962 {
13963 int hpos_at_start = it->hpos;
13964 int saved_face_id = it->face_id;
13965 struct glyph_row *row = it->glyph_row;
13966
13967 /* Initialize the iterator IT for iteration over STRING beginning
13968 with index START. */
13969 reseat_to_string (it, string, lisp_string, start,
13970 precision, field_width, multibyte);
13971
13972 /* If displaying STRING, set up the face of the iterator
13973 from LISP_STRING, if that's given. */
13974 if (STRINGP (face_string))
13975 {
13976 int endptr;
13977 struct face *face;
13978
13979 it->face_id
13980 = face_at_string_position (it->w, face_string, face_string_pos,
13981 0, it->region_beg_charpos,
13982 it->region_end_charpos,
13983 &endptr, it->base_face_id, 0);
13984 face = FACE_FROM_ID (it->f, it->face_id);
13985 it->face_box_p = face->box != FACE_NO_BOX;
13986 }
13987
13988 /* Set max_x to the maximum allowed X position. Don't let it go
13989 beyond the right edge of the window. */
13990 if (max_x <= 0)
13991 max_x = it->last_visible_x;
13992 else
13993 max_x = min (max_x, it->last_visible_x);
13994
13995 /* Skip over display elements that are not visible. because IT->w is
13996 hscrolled. */
13997 if (it->current_x < it->first_visible_x)
13998 move_it_in_display_line_to (it, 100000, it->first_visible_x,
13999 MOVE_TO_POS | MOVE_TO_X);
14000
14001 row->ascent = it->max_ascent;
14002 row->height = it->max_ascent + it->max_descent;
14003 row->phys_ascent = it->max_phys_ascent;
14004 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14005
14006 /* This condition is for the case that we are called with current_x
14007 past last_visible_x. */
14008 while (it->current_x < max_x)
14009 {
14010 int x_before, x, n_glyphs_before, i, nglyphs;
14011
14012 /* Get the next display element. */
14013 if (!get_next_display_element (it))
14014 break;
14015
14016 /* Produce glyphs. */
14017 x_before = it->current_x;
14018 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
14019 PRODUCE_GLYPHS (it);
14020
14021 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
14022 i = 0;
14023 x = x_before;
14024 while (i < nglyphs)
14025 {
14026 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
14027
14028 if (!it->truncate_lines_p
14029 && x + glyph->pixel_width > max_x)
14030 {
14031 /* End of continued line or max_x reached. */
14032 if (CHAR_GLYPH_PADDING_P (*glyph))
14033 {
14034 /* A wide character is unbreakable. */
14035 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
14036 it->current_x = x_before;
14037 }
14038 else
14039 {
14040 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
14041 it->current_x = x;
14042 }
14043 break;
14044 }
14045 else if (x + glyph->pixel_width > it->first_visible_x)
14046 {
14047 /* Glyph is at least partially visible. */
14048 ++it->hpos;
14049 if (x < it->first_visible_x)
14050 it->glyph_row->x = x - it->first_visible_x;
14051 }
14052 else
14053 {
14054 /* Glyph is off the left margin of the display area.
14055 Should not happen. */
14056 abort ();
14057 }
14058
14059 row->ascent = max (row->ascent, it->max_ascent);
14060 row->height = max (row->height, it->max_ascent + it->max_descent);
14061 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14062 row->phys_height = max (row->phys_height,
14063 it->max_phys_ascent + it->max_phys_descent);
14064 x += glyph->pixel_width;
14065 ++i;
14066 }
14067
14068 /* Stop if max_x reached. */
14069 if (i < nglyphs)
14070 break;
14071
14072 /* Stop at line ends. */
14073 if (ITERATOR_AT_END_OF_LINE_P (it))
14074 {
14075 it->continuation_lines_width = 0;
14076 break;
14077 }
14078
14079 set_iterator_to_next (it, 1);
14080
14081 /* Stop if truncating at the right edge. */
14082 if (it->truncate_lines_p
14083 && it->current_x >= it->last_visible_x)
14084 {
14085 /* Add truncation mark, but don't do it if the line is
14086 truncated at a padding space. */
14087 if (IT_CHARPOS (*it) < it->string_nchars)
14088 {
14089 if (!FRAME_WINDOW_P (it->f))
14090 {
14091 int i, n;
14092
14093 if (it->current_x > it->last_visible_x)
14094 {
14095 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
14096 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
14097 break;
14098 for (n = row->used[TEXT_AREA]; i < n; ++i)
14099 {
14100 row->used[TEXT_AREA] = i;
14101 produce_special_glyphs (it, IT_TRUNCATION);
14102 }
14103 }
14104 produce_special_glyphs (it, IT_TRUNCATION);
14105 }
14106 it->glyph_row->truncated_on_right_p = 1;
14107 }
14108 break;
14109 }
14110 }
14111
14112 /* Maybe insert a truncation at the left. */
14113 if (it->first_visible_x
14114 && IT_CHARPOS (*it) > 0)
14115 {
14116 if (!FRAME_WINDOW_P (it->f))
14117 insert_left_trunc_glyphs (it);
14118 it->glyph_row->truncated_on_left_p = 1;
14119 }
14120
14121 it->face_id = saved_face_id;
14122
14123 /* Value is number of columns displayed. */
14124 return it->hpos - hpos_at_start;
14125 }
14126
14127
14128 \f
14129 /* This is like a combination of memq and assq. Return 1 if PROPVAL
14130 appears as an element of LIST or as the car of an element of LIST.
14131 If PROPVAL is a list, compare each element against LIST in that
14132 way, and return 1 if any element of PROPVAL is found in LIST.
14133 Otherwise return 0. This function cannot quit. */
14134
14135 int
14136 invisible_p (propval, list)
14137 register Lisp_Object propval;
14138 Lisp_Object list;
14139 {
14140 register Lisp_Object tail, proptail;
14141
14142 for (tail = list; CONSP (tail); tail = XCDR (tail))
14143 {
14144 register Lisp_Object tem;
14145 tem = XCAR (tail);
14146 if (EQ (propval, tem))
14147 return 1;
14148 if (CONSP (tem) && EQ (propval, XCAR (tem)))
14149 return 1;
14150 }
14151
14152 if (CONSP (propval))
14153 {
14154 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
14155 {
14156 Lisp_Object propelt;
14157 propelt = XCAR (proptail);
14158 for (tail = list; CONSP (tail); tail = XCDR (tail))
14159 {
14160 register Lisp_Object tem;
14161 tem = XCAR (tail);
14162 if (EQ (propelt, tem))
14163 return 1;
14164 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
14165 return 1;
14166 }
14167 }
14168 }
14169
14170 return 0;
14171 }
14172
14173
14174 /* Return 1 if PROPVAL appears as the car of an element of LIST and
14175 the cdr of that element is non-nil. If PROPVAL is a list, check
14176 each element of PROPVAL in that way, and the first time some
14177 element is found, return 1 if the cdr of that element is non-nil.
14178 Otherwise return 0. This function cannot quit. */
14179
14180 int
14181 invisible_ellipsis_p (propval, list)
14182 register Lisp_Object propval;
14183 Lisp_Object list;
14184 {
14185 register Lisp_Object tail, proptail;
14186
14187 for (tail = list; CONSP (tail); tail = XCDR (tail))
14188 {
14189 register Lisp_Object tem;
14190 tem = XCAR (tail);
14191 if (CONSP (tem) && EQ (propval, XCAR (tem)))
14192 return ! NILP (XCDR (tem));
14193 }
14194
14195 if (CONSP (propval))
14196 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
14197 {
14198 Lisp_Object propelt;
14199 propelt = XCAR (proptail);
14200 for (tail = list; CONSP (tail); tail = XCDR (tail))
14201 {
14202 register Lisp_Object tem;
14203 tem = XCAR (tail);
14204 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
14205 return ! NILP (XCDR (tem));
14206 }
14207 }
14208
14209 return 0;
14210 }
14211
14212
14213 \f
14214 /***********************************************************************
14215 Initialization
14216 ***********************************************************************/
14217
14218 void
14219 syms_of_xdisp ()
14220 {
14221 Vwith_echo_area_save_vector = Qnil;
14222 staticpro (&Vwith_echo_area_save_vector);
14223
14224 Vmessage_stack = Qnil;
14225 staticpro (&Vmessage_stack);
14226
14227 Qinhibit_redisplay = intern ("inhibit-redisplay");
14228 staticpro (&Qinhibit_redisplay);
14229
14230 #if GLYPH_DEBUG
14231 defsubr (&Sdump_glyph_matrix);
14232 defsubr (&Sdump_glyph_row);
14233 defsubr (&Sdump_tool_bar_row);
14234 defsubr (&Strace_redisplay_toggle);
14235 defsubr (&Strace_to_stderr);
14236 #endif
14237 #ifdef HAVE_WINDOW_SYSTEM
14238 defsubr (&Stool_bar_lines_needed);
14239 #endif
14240
14241 staticpro (&Qmenu_bar_update_hook);
14242 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
14243
14244 staticpro (&Qoverriding_terminal_local_map);
14245 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
14246
14247 staticpro (&Qoverriding_local_map);
14248 Qoverriding_local_map = intern ("overriding-local-map");
14249
14250 staticpro (&Qwindow_scroll_functions);
14251 Qwindow_scroll_functions = intern ("window-scroll-functions");
14252
14253 staticpro (&Qredisplay_end_trigger_functions);
14254 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
14255
14256 staticpro (&Qinhibit_point_motion_hooks);
14257 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
14258
14259 QCdata = intern (":data");
14260 staticpro (&QCdata);
14261 Qdisplay = intern ("display");
14262 staticpro (&Qdisplay);
14263 Qspace_width = intern ("space-width");
14264 staticpro (&Qspace_width);
14265 Qraise = intern ("raise");
14266 staticpro (&Qraise);
14267 Qspace = intern ("space");
14268 staticpro (&Qspace);
14269 Qmargin = intern ("margin");
14270 staticpro (&Qmargin);
14271 Qleft_margin = intern ("left-margin");
14272 staticpro (&Qleft_margin);
14273 Qright_margin = intern ("right-margin");
14274 staticpro (&Qright_margin);
14275 Qalign_to = intern ("align-to");
14276 staticpro (&Qalign_to);
14277 QCalign_to = intern (":align-to");
14278 staticpro (&QCalign_to);
14279 Qrelative_width = intern ("relative-width");
14280 staticpro (&Qrelative_width);
14281 QCrelative_width = intern (":relative-width");
14282 staticpro (&QCrelative_width);
14283 QCrelative_height = intern (":relative-height");
14284 staticpro (&QCrelative_height);
14285 QCeval = intern (":eval");
14286 staticpro (&QCeval);
14287 Qwhen = intern ("when");
14288 staticpro (&Qwhen);
14289 QCfile = intern (":file");
14290 staticpro (&QCfile);
14291 Qfontified = intern ("fontified");
14292 staticpro (&Qfontified);
14293 Qfontification_functions = intern ("fontification-functions");
14294 staticpro (&Qfontification_functions);
14295 Qtrailing_whitespace = intern ("trailing-whitespace");
14296 staticpro (&Qtrailing_whitespace);
14297 Qimage = intern ("image");
14298 staticpro (&Qimage);
14299 Qmessage_truncate_lines = intern ("message-truncate-lines");
14300 staticpro (&Qmessage_truncate_lines);
14301 Qgrow_only = intern ("grow-only");
14302 staticpro (&Qgrow_only);
14303 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
14304 staticpro (&Qinhibit_menubar_update);
14305 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
14306 staticpro (&Qinhibit_eval_during_redisplay);
14307
14308 last_arrow_position = Qnil;
14309 last_arrow_string = Qnil;
14310 staticpro (&last_arrow_position);
14311 staticpro (&last_arrow_string);
14312
14313 echo_buffer[0] = echo_buffer[1] = Qnil;
14314 staticpro (&echo_buffer[0]);
14315 staticpro (&echo_buffer[1]);
14316
14317 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
14318 staticpro (&echo_area_buffer[0]);
14319 staticpro (&echo_area_buffer[1]);
14320
14321 Vmessages_buffer_name = build_string ("*Messages*");
14322 staticpro (&Vmessages_buffer_name);
14323
14324 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
14325 "Non-nil means highlight trailing whitespace.\n\
14326 The face used for trailing whitespace is `trailing-whitespace'.");
14327 Vshow_trailing_whitespace = Qnil;
14328
14329 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
14330 "Non-nil means don't actually do any redisplay.\n\
14331 This is used for internal purposes.");
14332 Vinhibit_redisplay = Qnil;
14333
14334 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
14335 "String (or mode line construct) included (normally) in `mode-line-format'.");
14336 Vglobal_mode_string = Qnil;
14337
14338 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
14339 "Marker for where to display an arrow on top of the buffer text.\n\
14340 This must be the beginning of a line in order to work.\n\
14341 See also `overlay-arrow-string'.");
14342 Voverlay_arrow_position = Qnil;
14343
14344 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
14345 "String to display as an arrow. See also `overlay-arrow-position'.");
14346 Voverlay_arrow_string = Qnil;
14347
14348 DEFVAR_INT ("scroll-step", &scroll_step,
14349 "*The number of lines to try scrolling a window by when point moves out.\n\
14350 If that fails to bring point back on frame, point is centered instead.\n\
14351 If this is zero, point is always centered after it moves off frame.\n\
14352 If you want scrolling to always be a line at a time, you should set\n\
14353 `scroll-conservatively' to a large value rather than set this to 1.");
14354
14355 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
14356 "*Scroll up to this many lines, to bring point back on screen.\n\
14357 A value of zero means to scroll the text to center point vertically\n\
14358 in the window.");
14359 scroll_conservatively = 0;
14360
14361 DEFVAR_INT ("scroll-margin", &scroll_margin,
14362 "*Number of lines of margin at the top and bottom of a window.\n\
14363 Recenter the window whenever point gets within this many lines\n\
14364 of the top or bottom of the window.");
14365 scroll_margin = 0;
14366
14367 #if GLYPH_DEBUG
14368 DEFVAR_INT ("debug-end-pos", &debug_end_pos, "Don't ask");
14369 #endif
14370
14371 DEFVAR_BOOL ("truncate-partial-width-windows",
14372 &truncate_partial_width_windows,
14373 "*Non-nil means truncate lines in all windows less than full frame wide.");
14374 truncate_partial_width_windows = 1;
14375
14376 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
14377 "nil means display the mode-line/header-line/menu-bar in the default face.\n\
14378 Any other value means to use the appropriate face, `mode-line',\n\
14379 `header-line', or `menu' respectively.\n\
14380 \n\
14381 This variable is deprecated; please change the above faces instead.");
14382 mode_line_inverse_video = 1;
14383
14384 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
14385 "*Maximum buffer size for which line number should be displayed.\n\
14386 If the buffer is bigger than this, the line number does not appear\n\
14387 in the mode line. A value of nil means no limit.");
14388 Vline_number_display_limit = Qnil;
14389
14390 DEFVAR_INT ("line-number-display-limit-width",
14391 &line_number_display_limit_width,
14392 "*Maximum line width (in characters) for line number display.\n\
14393 If the average length of the lines near point is bigger than this, then the\n\
14394 line number may be omitted from the mode line.");
14395 line_number_display_limit_width = 200;
14396
14397 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
14398 "*Non-nil means highlight region even in nonselected windows.");
14399 highlight_nonselected_windows = 0;
14400
14401 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
14402 "Non-nil if more than one frame is visible on this display.\n\
14403 Minibuffer-only frames don't count, but iconified frames do.\n\
14404 This variable is not guaranteed to be accurate except while processing\n\
14405 `frame-title-format' and `icon-title-format'.");
14406
14407 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
14408 "Template for displaying the title bar of visible frames.\n\
14409 \(Assuming the window manager supports this feature.)\n\
14410 This variable has the same structure as `mode-line-format' (which see),\n\
14411 and is used only on frames for which no explicit name has been set\n\
14412 \(see `modify-frame-parameters').");
14413 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
14414 "Template for displaying the title bar of an iconified frame.\n\
14415 \(Assuming the window manager supports this feature.)\n\
14416 This variable has the same structure as `mode-line-format' (which see),\n\
14417 and is used only on frames for which no explicit name has been set\n\
14418 \(see `modify-frame-parameters').");
14419 Vicon_title_format
14420 = Vframe_title_format
14421 = Fcons (intern ("multiple-frames"),
14422 Fcons (build_string ("%b"),
14423 Fcons (Fcons (build_string (""),
14424 Fcons (intern ("invocation-name"),
14425 Fcons (build_string ("@"),
14426 Fcons (intern ("system-name"),
14427 Qnil)))),
14428 Qnil)));
14429
14430 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
14431 "Maximum number of lines to keep in the message log buffer.\n\
14432 If nil, disable message logging. If t, log messages but don't truncate\n\
14433 the buffer when it becomes large.");
14434 Vmessage_log_max = make_number (50);
14435
14436 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
14437 "Functions called before redisplay, if window sizes have changed.\n\
14438 The value should be a list of functions that take one argument.\n\
14439 Just before redisplay, for each frame, if any of its windows have changed\n\
14440 size since the last redisplay, or have been split or deleted,\n\
14441 all the functions in the list are called, with the frame as argument.");
14442 Vwindow_size_change_functions = Qnil;
14443
14444 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
14445 "List of Functions to call before redisplaying a window with scrolling.\n\
14446 Each function is called with two arguments, the window\n\
14447 and its new display-start position. Note that the value of `window-end'\n\
14448 is not valid when these functions are called.");
14449 Vwindow_scroll_functions = Qnil;
14450
14451 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
14452 "*Non-nil means automatically resize tool-bars.\n\
14453 This increases a tool-bar's height if not all tool-bar items are visible.\n\
14454 It decreases a tool-bar's height when it would display blank lines\n\
14455 otherwise.");
14456 auto_resize_tool_bars_p = 1;
14457
14458 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
14459 "*Non-nil means raise tool-bar buttons when the mouse moves over them.");
14460 auto_raise_tool_bar_buttons_p = 1;
14461
14462 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
14463 "*Margin around tool-bar buttons in pixels.\n\
14464 If an integer, use that for both horizontal and vertical margins.\n\
14465 Otherwise, value should be a pair of integers `(HORZ : VERT)' with\n\
14466 HORZ specifying the horizontal margin, and VERT specifying the\n\
14467 vertical margin.");
14468 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
14469
14470 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
14471 "Relief thickness of tool-bar buttons.");
14472 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
14473
14474 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
14475 "List of functions to call to fontify regions of text.\n\
14476 Each function is called with one argument POS. Functions must\n\
14477 fontify a region starting at POS in the current buffer, and give\n\
14478 fontified regions the property `fontified'.\n\
14479 This variable automatically becomes buffer-local when set.");
14480 Vfontification_functions = Qnil;
14481 Fmake_variable_buffer_local (Qfontification_functions);
14482
14483 DEFVAR_BOOL ("unibyte-display-via-language-environment",
14484 &unibyte_display_via_language_environment,
14485 "*Non-nil means display unibyte text according to language environment.\n\
14486 Specifically this means that unibyte non-ASCII characters\n\
14487 are displayed by converting them to the equivalent multibyte characters\n\
14488 according to the current language environment. As a result, they are\n\
14489 displayed according to the current fontset.");
14490 unibyte_display_via_language_environment = 0;
14491
14492 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
14493 "*Maximum height for resizing mini-windows.\n\
14494 If a float, it specifies a fraction of the mini-window frame's height.\n\
14495 If an integer, it specifies a number of lines.");
14496 Vmax_mini_window_height = make_float (0.25);
14497
14498 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
14499 "*How to resize mini-windows.\n\
14500 A value of nil means don't automatically resize mini-windows.\n\
14501 A value of t means resize them to fit the text displayed in them.\n\
14502 A value of `grow-only', the default, means let mini-windows grow\n\
14503 only, until their display becomes empty, at which point the windows\n\
14504 go back to their normal size.");
14505 Vresize_mini_windows = Qgrow_only;
14506
14507 DEFVAR_BOOL ("cursor-in-non-selected-windows",
14508 &cursor_in_non_selected_windows,
14509 "*Non-nil means display a hollow cursor in non-selected windows.\n\
14510 Nil means don't display a cursor there.");
14511 cursor_in_non_selected_windows = 1;
14512
14513 DEFVAR_BOOL ("automatic-hscrolling", &automatic_hscrolling_p,
14514 "*Non-nil means scroll the display automatically to make point visible.");
14515 automatic_hscrolling_p = 1;
14516
14517 DEFVAR_LISP ("image-types", &Vimage_types,
14518 "List of supported image types.\n\
14519 Each element of the list is a symbol for a supported image type.");
14520 Vimage_types = Qnil;
14521
14522 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
14523 "If non-nil, messages are truncated instead of resizing the echo area.\n\
14524 Bind this around calls to `message' to let it take effect.");
14525 message_truncate_lines = 0;
14526
14527 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
14528 "Normal hook run for clicks on menu bar, before displaying a submenu.\n\
14529 Can be used to update submenus whose contents should vary.");
14530 Vmenu_bar_update_hook = Qnil;
14531
14532 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
14533 "Non-nil means don't update menu bars. Internal use only.");
14534 inhibit_menubar_update = 0;
14535
14536 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
14537 "Non-nil means don't eval Lisp during redisplay.");
14538 inhibit_eval_during_redisplay = 0;
14539 }
14540
14541
14542 /* Initialize this module when Emacs starts. */
14543
14544 void
14545 init_xdisp ()
14546 {
14547 Lisp_Object root_window;
14548 struct window *mini_w;
14549
14550 current_header_line_height = current_mode_line_height = -1;
14551
14552 CHARPOS (this_line_start_pos) = 0;
14553
14554 mini_w = XWINDOW (minibuf_window);
14555 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
14556
14557 if (!noninteractive)
14558 {
14559 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
14560 int i;
14561
14562 XWINDOW (root_window)->top = make_number (FRAME_TOP_MARGIN (f));
14563 set_window_height (root_window,
14564 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
14565 0);
14566 mini_w->top = make_number (FRAME_HEIGHT (f) - 1);
14567 set_window_height (minibuf_window, 1, 0);
14568
14569 XWINDOW (root_window)->width = make_number (FRAME_WIDTH (f));
14570 mini_w->width = make_number (FRAME_WIDTH (f));
14571
14572 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
14573 scratch_glyph_row.glyphs[TEXT_AREA + 1]
14574 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
14575
14576 /* The default ellipsis glyphs `...'. */
14577 for (i = 0; i < 3; ++i)
14578 default_invis_vector[i] = make_number ('.');
14579 }
14580
14581 #ifdef HAVE_WINDOW_SYSTEM
14582 {
14583 /* Allocate the buffer for frame titles. */
14584 int size = 100;
14585 frame_title_buf = (char *) xmalloc (size);
14586 frame_title_buf_end = frame_title_buf + size;
14587 frame_title_ptr = NULL;
14588 }
14589 #endif /* HAVE_WINDOW_SYSTEM */
14590
14591 help_echo_showing_p = 0;
14592 }
14593
14594