]> code.delx.au - gnu-emacs/blob - src/xdisp.c
(init_from_display_pos): Test invisible property
[gnu-emacs] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 86, 87, 88, 93, 94, 95, 97, 98, 99, 2000, 2001
3 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
23
24 Redisplay.
25
26 Emacs separates the task of updating the display from code
27 modifying global state, e.g. buffer text. This way functions
28 operating on buffers don't also have to be concerned with updating
29 the display.
30
31 Updating the display is triggered by the Lisp interpreter when it
32 decides it's time to do it. This is done either automatically for
33 you as part of the interpreter's command loop or as the result of
34 calling Lisp functions like `sit-for'. The C function `redisplay'
35 in xdisp.c is the only entry into the inner redisplay code. (Or,
36 let's say almost---see the 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 /* Names of text properties relevant for redisplay. */
258
259 Lisp_Object Qdisplay, Qrelative_width, Qalign_to;
260 extern Lisp_Object Qface, Qinvisible, Qimage, Qwidth;
261
262 /* Symbols used in text property values. */
263
264 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
265 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
266 Lisp_Object Qmargin;
267 extern Lisp_Object Qheight;
268
269 /* Non-nil means highlight trailing whitespace. */
270
271 Lisp_Object Vshow_trailing_whitespace;
272
273 /* Name of the face used to highlight trailing whitespace. */
274
275 Lisp_Object Qtrailing_whitespace;
276
277 /* The symbol `image' which is the car of the lists used to represent
278 images in Lisp. */
279
280 Lisp_Object Qimage;
281
282 /* Non-zero means print newline to stdout before next mini-buffer
283 message. */
284
285 int noninteractive_need_newline;
286
287 /* Non-zero means print newline to message log before next message. */
288
289 static int message_log_need_newline;
290
291 \f
292 /* The buffer position of the first character appearing entirely or
293 partially on the line of the selected window which contains the
294 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
295 redisplay optimization in redisplay_internal. */
296
297 static struct text_pos this_line_start_pos;
298
299 /* Number of characters past the end of the line above, including the
300 terminating newline. */
301
302 static struct text_pos this_line_end_pos;
303
304 /* The vertical positions and the height of this line. */
305
306 static int this_line_vpos;
307 static int this_line_y;
308 static int this_line_pixel_height;
309
310 /* X position at which this display line starts. Usually zero;
311 negative if first character is partially visible. */
312
313 static int this_line_start_x;
314
315 /* Buffer that this_line_.* variables are referring to. */
316
317 static struct buffer *this_line_buffer;
318
319 /* Nonzero means truncate lines in all windows less wide than the
320 frame. */
321
322 int truncate_partial_width_windows;
323
324 /* A flag to control how to display unibyte 8-bit character. */
325
326 int unibyte_display_via_language_environment;
327
328 /* Nonzero means we have more than one non-mini-buffer-only frame.
329 Not guaranteed to be accurate except while parsing
330 frame-title-format. */
331
332 int multiple_frames;
333
334 Lisp_Object Vglobal_mode_string;
335
336 /* Marker for where to display an arrow on top of the buffer text. */
337
338 Lisp_Object Voverlay_arrow_position;
339
340 /* String to display for the arrow. Only used on terminal frames. */
341
342 Lisp_Object Voverlay_arrow_string;
343
344 /* Values of those variables at last redisplay. However, if
345 Voverlay_arrow_position is a marker, last_arrow_position is its
346 numerical position. */
347
348 static Lisp_Object last_arrow_position, last_arrow_string;
349
350 /* Like mode-line-format, but for the title bar on a visible frame. */
351
352 Lisp_Object Vframe_title_format;
353
354 /* Like mode-line-format, but for the title bar on an iconified frame. */
355
356 Lisp_Object Vicon_title_format;
357
358 /* List of functions to call when a window's size changes. These
359 functions get one arg, a frame on which one or more windows' sizes
360 have changed. */
361
362 static Lisp_Object Vwindow_size_change_functions;
363
364 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
365
366 /* Nonzero if overlay arrow has been displayed once in this window. */
367
368 static int overlay_arrow_seen;
369
370 /* Nonzero means highlight the region even in nonselected windows. */
371
372 int highlight_nonselected_windows;
373
374 /* If cursor motion alone moves point off frame, try scrolling this
375 many lines up or down if that will bring it back. */
376
377 static int scroll_step;
378
379 /* Non-0 means scroll just far enough to bring point back on the
380 screen, when appropriate. */
381
382 static int scroll_conservatively;
383
384 /* Recenter the window whenever point gets within this many lines of
385 the top or bottom of the window. This value is translated into a
386 pixel value by multiplying it with CANON_Y_UNIT, which means that
387 there is really a fixed pixel height scroll margin. */
388
389 int scroll_margin;
390
391 /* Number of windows showing the buffer of the selected window (or
392 another buffer with the same base buffer). keyboard.c refers to
393 this. */
394
395 int buffer_shared;
396
397 /* Vector containing glyphs for an ellipsis `...'. */
398
399 static Lisp_Object default_invis_vector[3];
400
401 /* Zero means display the mode-line/header-line/menu-bar in the default face
402 (this slightly odd definition is for compatibility with previous versions
403 of emacs), non-zero means display them using their respective faces.
404
405 This variable is deprecated. */
406
407 int mode_line_inverse_video;
408
409 /* Prompt to display in front of the mini-buffer contents. */
410
411 Lisp_Object minibuf_prompt;
412
413 /* Width of current mini-buffer prompt. Only set after display_line
414 of the line that contains the prompt. */
415
416 int minibuf_prompt_width;
417 int minibuf_prompt_pixel_width;
418
419 /* This is the window where the echo area message was displayed. It
420 is always a mini-buffer window, but it may not be the same window
421 currently active as a mini-buffer. */
422
423 Lisp_Object echo_area_window;
424
425 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
426 pushes the current message and the value of
427 message_enable_multibyte on the stack, the function restore_message
428 pops the stack and displays MESSAGE again. */
429
430 Lisp_Object Vmessage_stack;
431
432 /* Nonzero means multibyte characters were enabled when the echo area
433 message was specified. */
434
435 int message_enable_multibyte;
436
437 /* True if we should redraw the mode lines on the next redisplay. */
438
439 int update_mode_lines;
440
441 /* Nonzero if window sizes or contents have changed since last
442 redisplay that finished */
443
444 int windows_or_buffers_changed;
445
446 /* Nonzero after display_mode_line if %l was used and it displayed a
447 line number. */
448
449 int line_number_displayed;
450
451 /* Maximum buffer size for which to display line numbers. */
452
453 Lisp_Object Vline_number_display_limit;
454
455 /* line width to consider when repostioning for line number display */
456
457 static int line_number_display_limit_width;
458
459 /* Number of lines to keep in the message log buffer. t means
460 infinite. nil means don't log at all. */
461
462 Lisp_Object Vmessage_log_max;
463
464 /* The name of the *Messages* buffer, a string. */
465
466 static Lisp_Object Vmessages_buffer_name;
467
468 /* Current, index 0, and last displayed echo area message. Either
469 buffers from echo_buffers, or nil to indicate no message. */
470
471 Lisp_Object echo_area_buffer[2];
472
473 /* The buffers referenced from echo_area_buffer. */
474
475 static Lisp_Object echo_buffer[2];
476
477 /* A vector saved used in with_area_buffer to reduce consing. */
478
479 static Lisp_Object Vwith_echo_area_save_vector;
480
481 /* Non-zero means display_echo_area should display the last echo area
482 message again. Set by redisplay_preserve_echo_area. */
483
484 static int display_last_displayed_message_p;
485
486 /* Nonzero if echo area is being used by print; zero if being used by
487 message. */
488
489 int message_buf_print;
490
491 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
492
493 Lisp_Object Qinhibit_menubar_update;
494 int inhibit_menubar_update;
495
496 /* Maximum height for resizing mini-windows. Either a float
497 specifying a fraction of the available height, or an integer
498 specifying a number of lines. */
499
500 Lisp_Object Vmax_mini_window_height;
501
502 /* Non-zero means messages should be displayed with truncated
503 lines instead of being continued. */
504
505 int message_truncate_lines;
506 Lisp_Object Qmessage_truncate_lines;
507
508 /* Non-zero means we want a hollow cursor in windows that are not
509 selected. Zero means there's no cursor in such windows. */
510
511 int cursor_in_non_selected_windows;
512
513 /* A scratch glyph row with contents used for generating truncation
514 glyphs. Also used in direct_output_for_insert. */
515
516 #define MAX_SCRATCH_GLYPHS 100
517 struct glyph_row scratch_glyph_row;
518 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
519
520 /* Ascent and height of the last line processed by move_it_to. */
521
522 static int last_max_ascent, last_height;
523
524 /* Non-zero if there's a help-echo in the echo area. */
525
526 int help_echo_showing_p;
527
528 /* If >= 0, computed, exact values of mode-line and header-line height
529 to use in the macros CURRENT_MODE_LINE_HEIGHT and
530 CURRENT_HEADER_LINE_HEIGHT. */
531
532 int current_mode_line_height, current_header_line_height;
533
534 /* The maximum distance to look ahead for text properties. Values
535 that are too small let us call compute_char_face and similar
536 functions too often which is expensive. Values that are too large
537 let us call compute_char_face and alike too often because we
538 might not be interested in text properties that far away. */
539
540 #define TEXT_PROP_DISTANCE_LIMIT 100
541
542 #if GLYPH_DEBUG
543
544 /* Non-zero means print traces of redisplay if compiled with
545 GLYPH_DEBUG != 0. */
546
547 int trace_redisplay_p;
548
549 #endif /* GLYPH_DEBUG */
550
551 #ifdef DEBUG_TRACE_MOVE
552 /* Non-zero means trace with TRACE_MOVE to stderr. */
553 int trace_move;
554
555 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
556 #else
557 #define TRACE_MOVE(x) (void) 0
558 #endif
559
560 /* Non-zero means automatically scroll windows horizontally to make
561 point visible. */
562
563 int automatic_hscrolling_p;
564
565 /* A list of symbols, one for each supported image type. */
566
567 Lisp_Object Vimage_types;
568
569 /* The variable `resize-mini-windows'. If nil, don't resize
570 mini-windows. If t, always resize them to fit the text they
571 display. If `grow-only', let mini-windows grow only until they
572 become empty. */
573
574 Lisp_Object Vresize_mini_windows;
575
576 /* Value returned from text property handlers (see below). */
577
578 enum prop_handled
579 {
580 HANDLED_NORMALLY,
581 HANDLED_RECOMPUTE_PROPS,
582 HANDLED_OVERLAY_STRING_CONSUMED,
583 HANDLED_RETURN
584 };
585
586 /* A description of text properties that redisplay is interested
587 in. */
588
589 struct props
590 {
591 /* The name of the property. */
592 Lisp_Object *name;
593
594 /* A unique index for the property. */
595 enum prop_idx idx;
596
597 /* A handler function called to set up iterator IT from the property
598 at IT's current position. Value is used to steer handle_stop. */
599 enum prop_handled (*handler) P_ ((struct it *it));
600 };
601
602 static enum prop_handled handle_face_prop P_ ((struct it *));
603 static enum prop_handled handle_invisible_prop P_ ((struct it *));
604 static enum prop_handled handle_display_prop P_ ((struct it *));
605 static enum prop_handled handle_composition_prop P_ ((struct it *));
606 static enum prop_handled handle_overlay_change P_ ((struct it *));
607 static enum prop_handled handle_fontified_prop P_ ((struct it *));
608
609 /* Properties handled by iterators. */
610
611 static struct props it_props[] =
612 {
613 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
614 /* Handle `face' before `display' because some sub-properties of
615 `display' need to know the face. */
616 {&Qface, FACE_PROP_IDX, handle_face_prop},
617 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
618 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
619 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
620 {NULL, 0, NULL}
621 };
622
623 /* Value is the position described by X. If X is a marker, value is
624 the marker_position of X. Otherwise, value is X. */
625
626 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
627
628 /* Enumeration returned by some move_it_.* functions internally. */
629
630 enum move_it_result
631 {
632 /* Not used. Undefined value. */
633 MOVE_UNDEFINED,
634
635 /* Move ended at the requested buffer position or ZV. */
636 MOVE_POS_MATCH_OR_ZV,
637
638 /* Move ended at the requested X pixel position. */
639 MOVE_X_REACHED,
640
641 /* Move within a line ended at the end of a line that must be
642 continued. */
643 MOVE_LINE_CONTINUED,
644
645 /* Move within a line ended at the end of a line that would
646 be displayed truncated. */
647 MOVE_LINE_TRUNCATED,
648
649 /* Move within a line ended at a line end. */
650 MOVE_NEWLINE_OR_CR
651 };
652
653
654 \f
655 /* Function prototypes. */
656
657 static void mark_window_display_accurate_1 P_ ((struct window *, int));
658 static int single_display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
659 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
660 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
661 static int redisplay_mode_lines P_ ((Lisp_Object, int));
662 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
663 static int invisible_text_between_p P_ ((struct it *, int, int));
664 static int next_element_from_ellipsis P_ ((struct it *));
665 static void pint2str P_ ((char *, int, int));
666 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
667 struct text_pos));
668 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
669 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
670 static void store_frame_title_char P_ ((char));
671 static int store_frame_title P_ ((unsigned char *, int, int));
672 static void x_consider_frame_title P_ ((Lisp_Object));
673 static void handle_stop P_ ((struct it *));
674 static int tool_bar_lines_needed P_ ((struct frame *));
675 static int single_display_prop_intangible_p P_ ((Lisp_Object));
676 static void ensure_echo_area_buffers P_ ((void));
677 static struct glyph_row *row_containing_pos P_ ((struct window *, int,
678 struct glyph_row *,
679 struct glyph_row *));
680 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
681 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
682 static int with_echo_area_buffer P_ ((struct window *, int,
683 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
684 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
685 static void clear_garbaged_frames P_ ((void));
686 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
687 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
688 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
689 static int display_echo_area P_ ((struct window *));
690 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
691 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
692 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
693 static int string_char_and_length P_ ((unsigned char *, int, int *));
694 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
695 struct text_pos));
696 static int compute_window_start_on_continuation_line P_ ((struct window *));
697 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
698 static void insert_left_trunc_glyphs P_ ((struct it *));
699 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
700 static void extend_face_to_end_of_line P_ ((struct it *));
701 static int append_space P_ ((struct it *, int));
702 static void make_cursor_line_fully_visible P_ ((struct window *));
703 static int try_scrolling P_ ((Lisp_Object, int, int, int, int));
704 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
705 static int trailing_whitespace_p P_ ((int));
706 static int message_log_check_duplicate P_ ((int, int, int, int));
707 int invisible_p P_ ((Lisp_Object, Lisp_Object));
708 int invisible_ellipsis_p P_ ((Lisp_Object, Lisp_Object));
709 static void push_it P_ ((struct it *));
710 static void pop_it P_ ((struct it *));
711 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
712 static void redisplay_internal P_ ((int));
713 static int echo_area_display P_ ((int));
714 static void redisplay_windows P_ ((Lisp_Object));
715 static void redisplay_window P_ ((Lisp_Object, int));
716 static void update_menu_bar P_ ((struct frame *, int));
717 static int try_window_reusing_current_matrix P_ ((struct window *));
718 static int try_window_id P_ ((struct window *));
719 static int display_line P_ ((struct it *));
720 static int display_mode_lines P_ ((struct window *));
721 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
722 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object));
723 static char *decode_mode_spec P_ ((struct window *, int, int, int));
724 static void display_menu_bar P_ ((struct window *));
725 static int display_count_lines P_ ((int, int, int, int, int *));
726 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
727 int, int, struct it *, int, int, int, int));
728 static void compute_line_metrics P_ ((struct it *));
729 static void run_redisplay_end_trigger_hook P_ ((struct it *));
730 static int get_overlay_strings P_ ((struct it *));
731 static void next_overlay_string P_ ((struct it *));
732 static void reseat P_ ((struct it *, struct text_pos, int));
733 static void reseat_1 P_ ((struct it *, struct text_pos, int));
734 static void back_to_previous_visible_line_start P_ ((struct it *));
735 static void reseat_at_previous_visible_line_start P_ ((struct it *));
736 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
737 static int next_element_from_display_vector P_ ((struct it *));
738 static int next_element_from_string P_ ((struct it *));
739 static int next_element_from_c_string P_ ((struct it *));
740 static int next_element_from_buffer P_ ((struct it *));
741 static int next_element_from_composition P_ ((struct it *));
742 static int next_element_from_image P_ ((struct it *));
743 static int next_element_from_stretch P_ ((struct it *));
744 static void load_overlay_strings P_ ((struct it *));
745 static void init_from_display_pos P_ ((struct it *, struct window *,
746 struct display_pos *));
747 static void reseat_to_string P_ ((struct it *, unsigned char *,
748 Lisp_Object, int, int, int, int));
749 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
750 int, int, int));
751 void move_it_vertically_backward P_ ((struct it *, int));
752 static void init_to_row_start P_ ((struct it *, struct window *,
753 struct glyph_row *));
754 static void init_to_row_end P_ ((struct it *, struct window *,
755 struct glyph_row *));
756 static void back_to_previous_line_start P_ ((struct it *));
757 static int forward_to_next_line_start P_ ((struct it *, int *));
758 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
759 Lisp_Object, int));
760 static struct text_pos string_pos P_ ((int, Lisp_Object));
761 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
762 static int number_of_chars P_ ((unsigned char *, int));
763 static void compute_stop_pos P_ ((struct it *));
764 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
765 Lisp_Object));
766 static int face_before_or_after_it_pos P_ ((struct it *, int));
767 static int next_overlay_change P_ ((int));
768 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
769 Lisp_Object, struct text_pos *,
770 int));
771 static int underlying_face_id P_ ((struct it *));
772
773 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
774 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
775
776 #ifdef HAVE_WINDOW_SYSTEM
777
778 static void update_tool_bar P_ ((struct frame *, int));
779 static void build_desired_tool_bar_string P_ ((struct frame *f));
780 static int redisplay_tool_bar P_ ((struct frame *));
781 static void display_tool_bar_line P_ ((struct it *));
782
783 #endif /* HAVE_WINDOW_SYSTEM */
784
785 \f
786 /***********************************************************************
787 Window display dimensions
788 ***********************************************************************/
789
790 /* Return the window-relative maximum y + 1 for glyph rows displaying
791 text in window W. This is the height of W minus the height of a
792 mode line, if any. */
793
794 INLINE int
795 window_text_bottom_y (w)
796 struct window *w;
797 {
798 struct frame *f = XFRAME (w->frame);
799 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
800
801 if (WINDOW_WANTS_MODELINE_P (w))
802 height -= CURRENT_MODE_LINE_HEIGHT (w);
803 return height;
804 }
805
806
807 /* Return the pixel width of display area AREA of window W. AREA < 0
808 means return the total width of W, not including bitmap areas to
809 the left and right of the window. */
810
811 INLINE int
812 window_box_width (w, area)
813 struct window *w;
814 int area;
815 {
816 struct frame *f = XFRAME (w->frame);
817 int width = XFASTINT (w->width);
818
819 if (!w->pseudo_window_p)
820 {
821 width -= FRAME_SCROLL_BAR_WIDTH (f) + FRAME_FLAGS_AREA_COLS (f);
822
823 if (area == TEXT_AREA)
824 {
825 if (INTEGERP (w->left_margin_width))
826 width -= XFASTINT (w->left_margin_width);
827 if (INTEGERP (w->right_margin_width))
828 width -= XFASTINT (w->right_margin_width);
829 }
830 else if (area == LEFT_MARGIN_AREA)
831 width = (INTEGERP (w->left_margin_width)
832 ? XFASTINT (w->left_margin_width) : 0);
833 else if (area == RIGHT_MARGIN_AREA)
834 width = (INTEGERP (w->right_margin_width)
835 ? XFASTINT (w->right_margin_width) : 0);
836 }
837
838 return width * CANON_X_UNIT (f);
839 }
840
841
842 /* Return the pixel height of the display area of window W, not
843 including mode lines of W, if any.. */
844
845 INLINE int
846 window_box_height (w)
847 struct window *w;
848 {
849 struct frame *f = XFRAME (w->frame);
850 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
851
852 xassert (height >= 0);
853
854 /* Note: the code below that determines the mode-line/header-line
855 height is essentially the same as that contained in the macro
856 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
857 the appropriate glyph row has its `mode_line_p' flag set,
858 and if it doesn't, uses estimate_mode_line_height instead. */
859
860 if (WINDOW_WANTS_MODELINE_P (w))
861 {
862 struct glyph_row *ml_row
863 = (w->current_matrix && w->current_matrix->rows
864 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
865 : 0);
866 if (ml_row && ml_row->mode_line_p)
867 height -= ml_row->height;
868 else
869 height -= estimate_mode_line_height (f, MODE_LINE_FACE_ID);
870 }
871
872 if (WINDOW_WANTS_HEADER_LINE_P (w))
873 {
874 struct glyph_row *hl_row
875 = (w->current_matrix && w->current_matrix->rows
876 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
877 : 0);
878 if (hl_row && hl_row->mode_line_p)
879 height -= hl_row->height;
880 else
881 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
882 }
883
884 return height;
885 }
886
887
888 /* Return the frame-relative coordinate of the left edge of display
889 area AREA of window W. AREA < 0 means return the left edge of the
890 whole window, to the right of any bitmap area at the left side of
891 W. */
892
893 INLINE int
894 window_box_left (w, area)
895 struct window *w;
896 int area;
897 {
898 struct frame *f = XFRAME (w->frame);
899 int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
900
901 if (!w->pseudo_window_p)
902 {
903 x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
904 + FRAME_LEFT_FLAGS_AREA_WIDTH (f));
905
906 if (area == TEXT_AREA)
907 x += window_box_width (w, LEFT_MARGIN_AREA);
908 else if (area == RIGHT_MARGIN_AREA)
909 x += (window_box_width (w, LEFT_MARGIN_AREA)
910 + window_box_width (w, TEXT_AREA));
911 }
912
913 return x;
914 }
915
916
917 /* Return the frame-relative coordinate of the right edge of display
918 area AREA of window W. AREA < 0 means return the left edge of the
919 whole window, to the left of any bitmap area at the right side of
920 W. */
921
922 INLINE int
923 window_box_right (w, area)
924 struct window *w;
925 int area;
926 {
927 return window_box_left (w, area) + window_box_width (w, area);
928 }
929
930
931 /* Get the bounding box of the display area AREA of window W, without
932 mode lines, in frame-relative coordinates. AREA < 0 means the
933 whole window, not including bitmap areas to the left and right of
934 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
935 coordinates of the upper-left corner of the box. Return in
936 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
937
938 INLINE void
939 window_box (w, area, box_x, box_y, box_width, box_height)
940 struct window *w;
941 int area;
942 int *box_x, *box_y, *box_width, *box_height;
943 {
944 struct frame *f = XFRAME (w->frame);
945
946 *box_width = window_box_width (w, area);
947 *box_height = window_box_height (w);
948 *box_x = window_box_left (w, area);
949 *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
950 + XFASTINT (w->top) * CANON_Y_UNIT (f));
951 if (WINDOW_WANTS_HEADER_LINE_P (w))
952 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
953 }
954
955
956 /* Get the bounding box of the display area AREA of window W, without
957 mode lines. AREA < 0 means the whole window, not including bitmap
958 areas to the left and right of the window. Return in *TOP_LEFT_X
959 and TOP_LEFT_Y the frame-relative pixel coordinates of the
960 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
961 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
962 box. */
963
964 INLINE void
965 window_box_edges (w, area, top_left_x, top_left_y,
966 bottom_right_x, bottom_right_y)
967 struct window *w;
968 int area;
969 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
970 {
971 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
972 bottom_right_y);
973 *bottom_right_x += *top_left_x;
974 *bottom_right_y += *top_left_y;
975 }
976
977
978 \f
979 /***********************************************************************
980 Utilities
981 ***********************************************************************/
982
983 /* Return the bottom y-position of the line the iterator IT is in.
984 This can modify IT's settings. */
985
986 int
987 line_bottom_y (it)
988 struct it *it;
989 {
990 int line_height = it->max_ascent + it->max_descent;
991 int line_top_y = it->current_y;
992
993 if (line_height == 0)
994 {
995 if (last_height)
996 line_height = last_height;
997 else if (IT_CHARPOS (*it) < ZV)
998 {
999 move_it_by_lines (it, 1, 1);
1000 line_height = (it->max_ascent || it->max_descent
1001 ? it->max_ascent + it->max_descent
1002 : last_height);
1003 }
1004 else
1005 {
1006 struct glyph_row *row = it->glyph_row;
1007
1008 /* Use the default character height. */
1009 it->glyph_row = NULL;
1010 it->what = IT_CHARACTER;
1011 it->c = ' ';
1012 it->len = 1;
1013 PRODUCE_GLYPHS (it);
1014 line_height = it->ascent + it->descent;
1015 it->glyph_row = row;
1016 }
1017 }
1018
1019 return line_top_y + line_height;
1020 }
1021
1022
1023 /* Return 1 if position CHARPOS is visible in window W. Set *FULLY to
1024 1 if POS is visible and the line containing POS is fully visible.
1025 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1026 and header-lines heights. */
1027
1028 int
1029 pos_visible_p (w, charpos, fully, exact_mode_line_heights_p)
1030 struct window *w;
1031 int charpos, *fully, exact_mode_line_heights_p;
1032 {
1033 struct it it;
1034 struct text_pos top;
1035 int visible_p;
1036 struct buffer *old_buffer = NULL;
1037
1038 if (XBUFFER (w->buffer) != current_buffer)
1039 {
1040 old_buffer = current_buffer;
1041 set_buffer_internal_1 (XBUFFER (w->buffer));
1042 }
1043
1044 *fully = visible_p = 0;
1045 SET_TEXT_POS_FROM_MARKER (top, w->start);
1046
1047 /* Compute exact mode line heights, if requested. */
1048 if (exact_mode_line_heights_p)
1049 {
1050 if (WINDOW_WANTS_MODELINE_P (w))
1051 current_mode_line_height
1052 = display_mode_line (w, MODE_LINE_FACE_ID,
1053 current_buffer->mode_line_format);
1054
1055 if (WINDOW_WANTS_HEADER_LINE_P (w))
1056 current_header_line_height
1057 = display_mode_line (w, HEADER_LINE_FACE_ID,
1058 current_buffer->header_line_format);
1059 }
1060
1061 start_display (&it, w, top);
1062 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
1063 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
1064
1065 /* Note that we may overshoot because of invisible text. */
1066 if (IT_CHARPOS (it) >= charpos)
1067 {
1068 int top_y = it.current_y;
1069 int bottom_y = line_bottom_y (&it);
1070 int window_top_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
1071
1072 if (top_y < window_top_y)
1073 visible_p = bottom_y > window_top_y;
1074 else if (top_y < it.last_visible_y)
1075 {
1076 visible_p = 1;
1077 *fully = bottom_y <= it.last_visible_y;
1078 }
1079 }
1080 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1081 {
1082 move_it_by_lines (&it, 1, 0);
1083 if (charpos < IT_CHARPOS (it))
1084 {
1085 visible_p = 1;
1086 *fully = 0;
1087 }
1088 }
1089
1090 if (old_buffer)
1091 set_buffer_internal_1 (old_buffer);
1092
1093 current_header_line_height = current_mode_line_height = -1;
1094 return visible_p;
1095 }
1096
1097
1098 /* Return the next character from STR which is MAXLEN bytes long.
1099 Return in *LEN the length of the character. This is like
1100 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1101 we find one, we return a `?', but with the length of the invalid
1102 character. */
1103
1104 static INLINE int
1105 string_char_and_length (str, maxlen, len)
1106 unsigned char *str;
1107 int maxlen, *len;
1108 {
1109 int c;
1110
1111 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1112 if (!CHAR_VALID_P (c, 1))
1113 /* We may not change the length here because other places in Emacs
1114 don't use this function, i.e. they silently accept invalid
1115 characters. */
1116 c = '?';
1117
1118 return c;
1119 }
1120
1121
1122
1123 /* Given a position POS containing a valid character and byte position
1124 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1125
1126 static struct text_pos
1127 string_pos_nchars_ahead (pos, string, nchars)
1128 struct text_pos pos;
1129 Lisp_Object string;
1130 int nchars;
1131 {
1132 xassert (STRINGP (string) && nchars >= 0);
1133
1134 if (STRING_MULTIBYTE (string))
1135 {
1136 int rest = STRING_BYTES (XSTRING (string)) - BYTEPOS (pos);
1137 unsigned char *p = XSTRING (string)->data + BYTEPOS (pos);
1138 int len;
1139
1140 while (nchars--)
1141 {
1142 string_char_and_length (p, rest, &len);
1143 p += len, rest -= len;
1144 xassert (rest >= 0);
1145 CHARPOS (pos) += 1;
1146 BYTEPOS (pos) += len;
1147 }
1148 }
1149 else
1150 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1151
1152 return pos;
1153 }
1154
1155
1156 /* Value is the text position, i.e. character and byte position,
1157 for character position CHARPOS in STRING. */
1158
1159 static INLINE struct text_pos
1160 string_pos (charpos, string)
1161 int charpos;
1162 Lisp_Object string;
1163 {
1164 struct text_pos pos;
1165 xassert (STRINGP (string));
1166 xassert (charpos >= 0);
1167 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1168 return pos;
1169 }
1170
1171
1172 /* Value is a text position, i.e. character and byte position, for
1173 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1174 means recognize multibyte characters. */
1175
1176 static struct text_pos
1177 c_string_pos (charpos, s, multibyte_p)
1178 int charpos;
1179 unsigned char *s;
1180 int multibyte_p;
1181 {
1182 struct text_pos pos;
1183
1184 xassert (s != NULL);
1185 xassert (charpos >= 0);
1186
1187 if (multibyte_p)
1188 {
1189 int rest = strlen (s), len;
1190
1191 SET_TEXT_POS (pos, 0, 0);
1192 while (charpos--)
1193 {
1194 string_char_and_length (s, rest, &len);
1195 s += len, rest -= len;
1196 xassert (rest >= 0);
1197 CHARPOS (pos) += 1;
1198 BYTEPOS (pos) += len;
1199 }
1200 }
1201 else
1202 SET_TEXT_POS (pos, charpos, charpos);
1203
1204 return pos;
1205 }
1206
1207
1208 /* Value is the number of characters in C string S. MULTIBYTE_P
1209 non-zero means recognize multibyte characters. */
1210
1211 static int
1212 number_of_chars (s, multibyte_p)
1213 unsigned char *s;
1214 int multibyte_p;
1215 {
1216 int nchars;
1217
1218 if (multibyte_p)
1219 {
1220 int rest = strlen (s), len;
1221 unsigned char *p = (unsigned char *) s;
1222
1223 for (nchars = 0; rest > 0; ++nchars)
1224 {
1225 string_char_and_length (p, rest, &len);
1226 rest -= len, p += len;
1227 }
1228 }
1229 else
1230 nchars = strlen (s);
1231
1232 return nchars;
1233 }
1234
1235
1236 /* Compute byte position NEWPOS->bytepos corresponding to
1237 NEWPOS->charpos. POS is a known position in string STRING.
1238 NEWPOS->charpos must be >= POS.charpos. */
1239
1240 static void
1241 compute_string_pos (newpos, pos, string)
1242 struct text_pos *newpos, pos;
1243 Lisp_Object string;
1244 {
1245 xassert (STRINGP (string));
1246 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1247
1248 if (STRING_MULTIBYTE (string))
1249 *newpos = string_pos_nchars_ahead (pos, string,
1250 CHARPOS (*newpos) - CHARPOS (pos));
1251 else
1252 BYTEPOS (*newpos) = CHARPOS (*newpos);
1253 }
1254
1255
1256 \f
1257 /***********************************************************************
1258 Lisp form evaluation
1259 ***********************************************************************/
1260
1261 /* Error handler for safe_eval and safe_call. */
1262
1263 static Lisp_Object
1264 safe_eval_handler (arg)
1265 Lisp_Object arg;
1266 {
1267 add_to_log ("Error during redisplay: %s", arg, Qnil);
1268 return Qnil;
1269 }
1270
1271
1272 /* Evaluate SEXPR and return the result, or nil if something went
1273 wrong. */
1274
1275 Lisp_Object
1276 safe_eval (sexpr)
1277 Lisp_Object sexpr;
1278 {
1279 int count = BINDING_STACK_SIZE ();
1280 struct gcpro gcpro1;
1281 Lisp_Object val;
1282
1283 GCPRO1 (sexpr);
1284 specbind (Qinhibit_redisplay, Qt);
1285 val = internal_condition_case_1 (Feval, sexpr, Qerror, safe_eval_handler);
1286 UNGCPRO;
1287 return unbind_to (count, val);
1288 }
1289
1290
1291 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1292 Return the result, or nil if something went wrong. */
1293
1294 Lisp_Object
1295 safe_call (nargs, args)
1296 int nargs;
1297 Lisp_Object *args;
1298 {
1299 int count = BINDING_STACK_SIZE ();
1300 Lisp_Object val;
1301 struct gcpro gcpro1;
1302
1303 GCPRO1 (args[0]);
1304 gcpro1.nvars = nargs;
1305 specbind (Qinhibit_redisplay, Qt);
1306 val = internal_condition_case_2 (Ffuncall, nargs, args, Qerror,
1307 safe_eval_handler);
1308 UNGCPRO;
1309 return unbind_to (count, val);
1310 }
1311
1312
1313 /* Call function FN with one argument ARG.
1314 Return the result, or nil if something went wrong. */
1315
1316 Lisp_Object
1317 safe_call1 (fn, arg)
1318 Lisp_Object fn, arg;
1319 {
1320 Lisp_Object args[2];
1321 args[0] = fn;
1322 args[1] = arg;
1323 return safe_call (2, args);
1324 }
1325
1326
1327 \f
1328 /***********************************************************************
1329 Debugging
1330 ***********************************************************************/
1331
1332 #if 0
1333
1334 /* Define CHECK_IT to perform sanity checks on iterators.
1335 This is for debugging. It is too slow to do unconditionally. */
1336
1337 static void
1338 check_it (it)
1339 struct it *it;
1340 {
1341 if (it->method == next_element_from_string)
1342 {
1343 xassert (STRINGP (it->string));
1344 xassert (IT_STRING_CHARPOS (*it) >= 0);
1345 }
1346 else if (it->method == next_element_from_buffer)
1347 {
1348 /* Check that character and byte positions agree. */
1349 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1350 }
1351
1352 if (it->dpvec)
1353 xassert (it->current.dpvec_index >= 0);
1354 else
1355 xassert (it->current.dpvec_index < 0);
1356 }
1357
1358 #define CHECK_IT(IT) check_it ((IT))
1359
1360 #else /* not 0 */
1361
1362 #define CHECK_IT(IT) (void) 0
1363
1364 #endif /* not 0 */
1365
1366
1367 #if GLYPH_DEBUG
1368
1369 /* Check that the window end of window W is what we expect it
1370 to be---the last row in the current matrix displaying text. */
1371
1372 static void
1373 check_window_end (w)
1374 struct window *w;
1375 {
1376 if (!MINI_WINDOW_P (w)
1377 && !NILP (w->window_end_valid))
1378 {
1379 struct glyph_row *row;
1380 xassert ((row = MATRIX_ROW (w->current_matrix,
1381 XFASTINT (w->window_end_vpos)),
1382 !row->enabled_p
1383 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1384 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1385 }
1386 }
1387
1388 #define CHECK_WINDOW_END(W) check_window_end ((W))
1389
1390 #else /* not GLYPH_DEBUG */
1391
1392 #define CHECK_WINDOW_END(W) (void) 0
1393
1394 #endif /* not GLYPH_DEBUG */
1395
1396
1397 \f
1398 /***********************************************************************
1399 Iterator initialization
1400 ***********************************************************************/
1401
1402 /* Initialize IT for displaying current_buffer in window W, starting
1403 at character position CHARPOS. CHARPOS < 0 means that no buffer
1404 position is specified which is useful when the iterator is assigned
1405 a position later. BYTEPOS is the byte position corresponding to
1406 CHARPOS. BYTEPOS <= 0 means compute it from CHARPOS.
1407
1408 If ROW is not null, calls to produce_glyphs with IT as parameter
1409 will produce glyphs in that row.
1410
1411 BASE_FACE_ID is the id of a base face to use. It must be one of
1412 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID or
1413 HEADER_LINE_FACE_ID for displaying mode lines, or TOOL_BAR_FACE_ID for
1414 displaying the tool-bar.
1415
1416 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID or
1417 HEADER_LINE_FACE_ID, the iterator will be initialized to use the
1418 corresponding mode line glyph row of the desired matrix of W. */
1419
1420 void
1421 init_iterator (it, w, charpos, bytepos, row, base_face_id)
1422 struct it *it;
1423 struct window *w;
1424 int charpos, bytepos;
1425 struct glyph_row *row;
1426 enum face_id base_face_id;
1427 {
1428 int highlight_region_p;
1429
1430 /* Some precondition checks. */
1431 xassert (w != NULL && it != NULL);
1432 xassert (charpos < 0 || (charpos > 0 && charpos <= ZV));
1433
1434 /* If face attributes have been changed since the last redisplay,
1435 free realized faces now because they depend on face definitions
1436 that might have changed. */
1437 if (face_change_count)
1438 {
1439 face_change_count = 0;
1440 free_all_realized_faces (Qnil);
1441 }
1442
1443 /* Use one of the mode line rows of W's desired matrix if
1444 appropriate. */
1445 if (row == NULL)
1446 {
1447 if (base_face_id == MODE_LINE_FACE_ID)
1448 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
1449 else if (base_face_id == HEADER_LINE_FACE_ID)
1450 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
1451 }
1452
1453 /* Clear IT. */
1454 bzero (it, sizeof *it);
1455 it->current.overlay_string_index = -1;
1456 it->current.dpvec_index = -1;
1457 it->base_face_id = base_face_id;
1458
1459 /* The window in which we iterate over current_buffer: */
1460 XSETWINDOW (it->window, w);
1461 it->w = w;
1462 it->f = XFRAME (w->frame);
1463
1464 /* Extra space between lines (on window systems only). */
1465 if (base_face_id == DEFAULT_FACE_ID
1466 && FRAME_WINDOW_P (it->f))
1467 {
1468 if (NATNUMP (current_buffer->extra_line_spacing))
1469 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
1470 else if (it->f->extra_line_spacing > 0)
1471 it->extra_line_spacing = it->f->extra_line_spacing;
1472 }
1473
1474 /* If realized faces have been removed, e.g. because of face
1475 attribute changes of named faces, recompute them. When running
1476 in batch mode, the face cache of Vterminal_frame is null. If
1477 we happen to get called, make a dummy face cache. */
1478 if (
1479 #ifndef WINDOWSNT
1480 noninteractive &&
1481 #endif
1482 FRAME_FACE_CACHE (it->f) == NULL)
1483 init_frame_faces (it->f);
1484 if (FRAME_FACE_CACHE (it->f)->used == 0)
1485 recompute_basic_faces (it->f);
1486
1487 /* Current value of the `space-width', and 'height' properties. */
1488 it->space_width = Qnil;
1489 it->font_height = Qnil;
1490
1491 /* Are control characters displayed as `^C'? */
1492 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
1493
1494 /* -1 means everything between a CR and the following line end
1495 is invisible. >0 means lines indented more than this value are
1496 invisible. */
1497 it->selective = (INTEGERP (current_buffer->selective_display)
1498 ? XFASTINT (current_buffer->selective_display)
1499 : (!NILP (current_buffer->selective_display)
1500 ? -1 : 0));
1501 it->selective_display_ellipsis_p
1502 = !NILP (current_buffer->selective_display_ellipses);
1503
1504 /* Display table to use. */
1505 it->dp = window_display_table (w);
1506
1507 /* Are multibyte characters enabled in current_buffer? */
1508 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1509
1510 /* Non-zero if we should highlight the region. */
1511 highlight_region_p
1512 = (!NILP (Vtransient_mark_mode)
1513 && !NILP (current_buffer->mark_active)
1514 && XMARKER (current_buffer->mark)->buffer != 0);
1515
1516 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
1517 start and end of a visible region in window IT->w. Set both to
1518 -1 to indicate no region. */
1519 if (highlight_region_p
1520 /* Maybe highlight only in selected window. */
1521 && (/* Either show region everywhere. */
1522 highlight_nonselected_windows
1523 /* Or show region in the selected window. */
1524 || w == XWINDOW (selected_window)
1525 /* Or show the region if we are in the mini-buffer and W is
1526 the window the mini-buffer refers to. */
1527 || (MINI_WINDOW_P (XWINDOW (selected_window))
1528 && WINDOWP (Vminibuf_scroll_window)
1529 && w == XWINDOW (Vminibuf_scroll_window))))
1530 {
1531 int charpos = marker_position (current_buffer->mark);
1532 it->region_beg_charpos = min (PT, charpos);
1533 it->region_end_charpos = max (PT, charpos);
1534 }
1535 else
1536 it->region_beg_charpos = it->region_end_charpos = -1;
1537
1538 /* Get the position at which the redisplay_end_trigger hook should
1539 be run, if it is to be run at all. */
1540 if (MARKERP (w->redisplay_end_trigger)
1541 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
1542 it->redisplay_end_trigger_charpos
1543 = marker_position (w->redisplay_end_trigger);
1544 else if (INTEGERP (w->redisplay_end_trigger))
1545 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
1546
1547 /* Correct bogus values of tab_width. */
1548 it->tab_width = XINT (current_buffer->tab_width);
1549 if (it->tab_width <= 0 || it->tab_width > 1000)
1550 it->tab_width = 8;
1551
1552 /* Are lines in the display truncated? */
1553 it->truncate_lines_p
1554 = (base_face_id != DEFAULT_FACE_ID
1555 || XINT (it->w->hscroll)
1556 || (truncate_partial_width_windows
1557 && !WINDOW_FULL_WIDTH_P (it->w))
1558 || !NILP (current_buffer->truncate_lines));
1559
1560 /* Get dimensions of truncation and continuation glyphs. These are
1561 displayed as bitmaps under X, so we don't need them for such
1562 frames. */
1563 if (!FRAME_WINDOW_P (it->f))
1564 {
1565 if (it->truncate_lines_p)
1566 {
1567 /* We will need the truncation glyph. */
1568 xassert (it->glyph_row == NULL);
1569 produce_special_glyphs (it, IT_TRUNCATION);
1570 it->truncation_pixel_width = it->pixel_width;
1571 }
1572 else
1573 {
1574 /* We will need the continuation glyph. */
1575 xassert (it->glyph_row == NULL);
1576 produce_special_glyphs (it, IT_CONTINUATION);
1577 it->continuation_pixel_width = it->pixel_width;
1578 }
1579
1580 /* Reset these values to zero becaue the produce_special_glyphs
1581 above has changed them. */
1582 it->pixel_width = it->ascent = it->descent = 0;
1583 it->phys_ascent = it->phys_descent = 0;
1584 }
1585
1586 /* Set this after getting the dimensions of truncation and
1587 continuation glyphs, so that we don't produce glyphs when calling
1588 produce_special_glyphs, above. */
1589 it->glyph_row = row;
1590 it->area = TEXT_AREA;
1591
1592 /* Get the dimensions of the display area. The display area
1593 consists of the visible window area plus a horizontally scrolled
1594 part to the left of the window. All x-values are relative to the
1595 start of this total display area. */
1596 if (base_face_id != DEFAULT_FACE_ID)
1597 {
1598 /* Mode lines, menu bar in terminal frames. */
1599 it->first_visible_x = 0;
1600 it->last_visible_x = XFASTINT (w->width) * CANON_X_UNIT (it->f);
1601 }
1602 else
1603 {
1604 it->first_visible_x
1605 = XFASTINT (it->w->hscroll) * CANON_X_UNIT (it->f);
1606 it->last_visible_x = (it->first_visible_x
1607 + window_box_width (w, TEXT_AREA));
1608
1609 /* If we truncate lines, leave room for the truncator glyph(s) at
1610 the right margin. Otherwise, leave room for the continuation
1611 glyph(s). Truncation and continuation glyphs are not inserted
1612 for window-based redisplay. */
1613 if (!FRAME_WINDOW_P (it->f))
1614 {
1615 if (it->truncate_lines_p)
1616 it->last_visible_x -= it->truncation_pixel_width;
1617 else
1618 it->last_visible_x -= it->continuation_pixel_width;
1619 }
1620
1621 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
1622 it->current_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w) + w->vscroll;
1623 }
1624
1625 /* Leave room for a border glyph. */
1626 if (!FRAME_WINDOW_P (it->f)
1627 && !WINDOW_RIGHTMOST_P (it->w))
1628 it->last_visible_x -= 1;
1629
1630 it->last_visible_y = window_text_bottom_y (w);
1631
1632 /* For mode lines and alike, arrange for the first glyph having a
1633 left box line if the face specifies a box. */
1634 if (base_face_id != DEFAULT_FACE_ID)
1635 {
1636 struct face *face;
1637
1638 it->face_id = base_face_id;
1639
1640 /* If we have a boxed mode line, make the first character appear
1641 with a left box line. */
1642 face = FACE_FROM_ID (it->f, base_face_id);
1643 if (face->box != FACE_NO_BOX)
1644 it->start_of_box_run_p = 1;
1645 }
1646
1647 /* If a buffer position was specified, set the iterator there,
1648 getting overlays and face properties from that position. */
1649 if (charpos > 0)
1650 {
1651 it->end_charpos = ZV;
1652 it->face_id = -1;
1653 IT_CHARPOS (*it) = charpos;
1654
1655 /* Compute byte position if not specified. */
1656 if (bytepos <= 0)
1657 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
1658 else
1659 IT_BYTEPOS (*it) = bytepos;
1660
1661 /* Compute faces etc. */
1662 reseat (it, it->current.pos, 1);
1663 }
1664
1665 CHECK_IT (it);
1666 }
1667
1668
1669 /* Initialize IT for the display of window W with window start POS. */
1670
1671 void
1672 start_display (it, w, pos)
1673 struct it *it;
1674 struct window *w;
1675 struct text_pos pos;
1676 {
1677 int start_at_line_beg_p;
1678 struct glyph_row *row;
1679 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
1680 int first_y;
1681
1682 row = w->desired_matrix->rows + first_vpos;
1683 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
1684 first_y = it->current_y;
1685
1686 /* If window start is not at a line start, move back to the line
1687 start. This makes sure that we take continuation lines into
1688 account. */
1689 start_at_line_beg_p = (CHARPOS (pos) == BEGV
1690 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
1691 if (!start_at_line_beg_p)
1692 reseat_at_previous_visible_line_start (it);
1693
1694 /* If window start is not at a line start, skip forward to POS to
1695 get the correct continuation_lines_width and current_x. */
1696 if (!start_at_line_beg_p)
1697 {
1698 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
1699
1700 /* If lines are continued, this line may end in the middle of a
1701 multi-glyph character (e.g. a control character displayed as
1702 \003, or in the middle of an overlay string). In this case
1703 move_it_to above will not have taken us to the start of
1704 the continuation line but to the end of the continued line. */
1705 if (!it->truncate_lines_p)
1706 {
1707 if (it->current_x > 0)
1708 {
1709 if (it->current.dpvec_index >= 0
1710 || it->current.overlay_string_index >= 0)
1711 {
1712 set_iterator_to_next (it, 1);
1713 move_it_in_display_line_to (it, -1, -1, 0);
1714 }
1715
1716 it->continuation_lines_width += it->current_x;
1717 }
1718
1719 /* We're starting a new display line, not affected by the
1720 height of the continued line, so clear the appropriate
1721 fields in the iterator structure. */
1722 it->max_ascent = it->max_descent = 0;
1723 it->max_phys_ascent = it->max_phys_descent = 0;
1724 }
1725
1726 it->current_y = first_y;
1727 it->vpos = 0;
1728 it->current_x = it->hpos = 0;
1729 }
1730
1731 #if 0 /* Don't assert the following because start_display is sometimes
1732 called intentionally with a window start that is not at a
1733 line start. Please leave this code in as a comment. */
1734
1735 /* Window start should be on a line start, now. */
1736 xassert (it->continuation_lines_width
1737 || IT_CHARPOS (it) == BEGV
1738 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
1739 #endif /* 0 */
1740 }
1741
1742
1743 /* Initialize IT for stepping through current_buffer in window W,
1744 starting at position POS that includes overlay string and display
1745 vector/ control character translation position information. */
1746
1747 static void
1748 init_from_display_pos (it, w, pos)
1749 struct it *it;
1750 struct window *w;
1751 struct display_pos *pos;
1752 {
1753 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
1754 Lisp_Object prop, window;
1755
1756 /* If POS specifies a position in a display vector, this might
1757 be for an ellipsis displayed for invisible text. We won't
1758 get the iterator set up for delivering that ellipsis unless
1759 we make sure that it gets aware of the invisible text. */
1760 if (pos->dpvec_index >= 0
1761 && pos->overlay_string_index < 0
1762 && CHARPOS (pos->string_pos) < 0
1763 && charpos > BEGV
1764 && (XSETWINDOW (window, w),
1765 prop = Fget_char_property (make_number (charpos),
1766 Qinvisible, window),
1767 !TEXT_PROP_MEANS_INVISIBLE (prop)))
1768 {
1769 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
1770 window);
1771 if (TEXT_PROP_MEANS_INVISIBLE (prop)
1772 && TEXT_PROP_MEANS_INVISIBLE_WITH_ELLIPSIS (prop))
1773 {
1774 --charpos;
1775 bytepos = 0;
1776 }
1777 }
1778
1779 /* Keep in mind: the call to reseat in init_iterator skips invisible
1780 text, so we might end up at a position different from POS. This
1781 is only a problem when POS is a row start after a newline and an
1782 overlay starts there with an after-string, and the overlay has an
1783 invisible property. Since we don't skip invisible text in
1784 display_line and elsewhere immediately after consuming the
1785 newline before the row start, such a POS will not be in a string,
1786 but the call to init_iterator below will move us to the
1787 after-string. */
1788 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
1789 xassert (IT_CHARPOS (*it) == CHARPOS (pos->pos));
1790
1791 /* If position is within an overlay string, set up IT to
1792 the right overlay string. */
1793 if (pos->overlay_string_index >= 0)
1794 {
1795 int relative_index;
1796
1797 /* We already have the first chunk of overlay strings in
1798 IT->overlay_strings. Load more until the one for
1799 pos->overlay_string_index is in IT->overlay_strings. */
1800 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
1801 {
1802 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
1803 it->current.overlay_string_index = 0;
1804 while (n--)
1805 {
1806 load_overlay_strings (it);
1807 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
1808 }
1809 }
1810
1811 it->current.overlay_string_index = pos->overlay_string_index;
1812 relative_index = (it->current.overlay_string_index
1813 % OVERLAY_STRING_CHUNK_SIZE);
1814 it->string = it->overlay_strings[relative_index];
1815 xassert (STRINGP (it->string));
1816 it->current.string_pos = pos->string_pos;
1817 it->method = next_element_from_string;
1818 }
1819 else if (it->current.overlay_string_index >= 0)
1820 {
1821 /* If POS says we're already after an overlay string ending at
1822 POS, make sure to pop the iterator because it will be in
1823 front of that overlay string. When POS is ZV, we've thereby
1824 also ``processed'' overlay strings at ZV. */
1825 while (it->sp)
1826 pop_it (it);
1827 it->current.overlay_string_index = -1;
1828 it->method = next_element_from_buffer;
1829 if (CHARPOS (pos->pos) == ZV)
1830 it->overlay_strings_at_end_processed_p = 1;
1831 }
1832
1833 if (CHARPOS (pos->string_pos) >= 0)
1834 {
1835 /* Recorded position is not in an overlay string, but in another
1836 string. This can only be a string from a `display' property.
1837 IT should already be filled with that string. */
1838 it->current.string_pos = pos->string_pos;
1839 xassert (STRINGP (it->string));
1840 }
1841
1842 /* Restore position in display vector translations, control
1843 character translations or ellipses. */
1844 if (pos->dpvec_index >= 0)
1845 {
1846 if (it->dpvec == NULL)
1847 get_next_display_element (it);
1848 xassert (it->dpvec && it->current.dpvec_index == 0);
1849 it->current.dpvec_index = pos->dpvec_index;
1850 }
1851 else if (it->current.dpvec_index >= 0)
1852 {
1853 /* I don't think this can happen, just being paranoid... */
1854 it->dpvec = NULL;
1855 it->current.dpvec_index = -1;
1856 if (it->s)
1857 it->method = next_element_from_c_string;
1858 else if (STRINGP (it->string))
1859 it->method = next_element_from_string;
1860 else
1861 it->method = next_element_from_buffer;
1862 }
1863
1864 CHECK_IT (it);
1865 }
1866
1867
1868 /* Initialize IT for stepping through current_buffer in window W
1869 starting at ROW->start. */
1870
1871 static void
1872 init_to_row_start (it, w, row)
1873 struct it *it;
1874 struct window *w;
1875 struct glyph_row *row;
1876 {
1877 init_from_display_pos (it, w, &row->start);
1878 it->continuation_lines_width = row->continuation_lines_width;
1879 CHECK_IT (it);
1880 }
1881
1882
1883 /* Initialize IT for stepping through current_buffer in window W
1884 starting in the line following ROW, i.e. starting at ROW->end. */
1885
1886 static void
1887 init_to_row_end (it, w, row)
1888 struct it *it;
1889 struct window *w;
1890 struct glyph_row *row;
1891 {
1892 init_from_display_pos (it, w, &row->end);
1893
1894 if (row->continued_p)
1895 it->continuation_lines_width = (row->continuation_lines_width
1896 + row->pixel_width);
1897 CHECK_IT (it);
1898 }
1899
1900
1901
1902 \f
1903 /***********************************************************************
1904 Text properties
1905 ***********************************************************************/
1906
1907 /* Called when IT reaches IT->stop_charpos. Handle text property and
1908 overlay changes. Set IT->stop_charpos to the next position where
1909 to stop. */
1910
1911 static void
1912 handle_stop (it)
1913 struct it *it;
1914 {
1915 enum prop_handled handled;
1916 int handle_overlay_change_p = 1;
1917 struct props *p;
1918
1919 it->dpvec = NULL;
1920 it->current.dpvec_index = -1;
1921
1922 do
1923 {
1924 handled = HANDLED_NORMALLY;
1925
1926 /* Call text property handlers. */
1927 for (p = it_props; p->handler; ++p)
1928 {
1929 handled = p->handler (it);
1930
1931 if (handled == HANDLED_RECOMPUTE_PROPS)
1932 break;
1933 else if (handled == HANDLED_RETURN)
1934 return;
1935 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
1936 handle_overlay_change_p = 0;
1937 }
1938
1939 if (handled != HANDLED_RECOMPUTE_PROPS)
1940 {
1941 /* Don't check for overlay strings below when set to deliver
1942 characters from a display vector. */
1943 if (it->method == next_element_from_display_vector)
1944 handle_overlay_change_p = 0;
1945
1946 /* Handle overlay changes. */
1947 if (handle_overlay_change_p)
1948 handled = handle_overlay_change (it);
1949
1950 /* Determine where to stop next. */
1951 if (handled == HANDLED_NORMALLY)
1952 compute_stop_pos (it);
1953 }
1954 }
1955 while (handled == HANDLED_RECOMPUTE_PROPS);
1956 }
1957
1958
1959 /* Compute IT->stop_charpos from text property and overlay change
1960 information for IT's current position. */
1961
1962 static void
1963 compute_stop_pos (it)
1964 struct it *it;
1965 {
1966 register INTERVAL iv, next_iv;
1967 Lisp_Object object, limit, position;
1968
1969 /* If nowhere else, stop at the end. */
1970 it->stop_charpos = it->end_charpos;
1971
1972 if (STRINGP (it->string))
1973 {
1974 /* Strings are usually short, so don't limit the search for
1975 properties. */
1976 object = it->string;
1977 limit = Qnil;
1978 position = make_number (IT_STRING_CHARPOS (*it));
1979 }
1980 else
1981 {
1982 int charpos;
1983
1984 /* If next overlay change is in front of the current stop pos
1985 (which is IT->end_charpos), stop there. Note: value of
1986 next_overlay_change is point-max if no overlay change
1987 follows. */
1988 charpos = next_overlay_change (IT_CHARPOS (*it));
1989 if (charpos < it->stop_charpos)
1990 it->stop_charpos = charpos;
1991
1992 /* If showing the region, we have to stop at the region
1993 start or end because the face might change there. */
1994 if (it->region_beg_charpos > 0)
1995 {
1996 if (IT_CHARPOS (*it) < it->region_beg_charpos)
1997 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
1998 else if (IT_CHARPOS (*it) < it->region_end_charpos)
1999 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2000 }
2001
2002 /* Set up variables for computing the stop position from text
2003 property changes. */
2004 XSETBUFFER (object, current_buffer);
2005 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2006 position = make_number (IT_CHARPOS (*it));
2007
2008 }
2009
2010 /* Get the interval containing IT's position. Value is a null
2011 interval if there isn't such an interval. */
2012 iv = validate_interval_range (object, &position, &position, 0);
2013 if (!NULL_INTERVAL_P (iv))
2014 {
2015 Lisp_Object values_here[LAST_PROP_IDX];
2016 struct props *p;
2017
2018 /* Get properties here. */
2019 for (p = it_props; p->handler; ++p)
2020 values_here[p->idx] = textget (iv->plist, *p->name);
2021
2022 /* Look for an interval following iv that has different
2023 properties. */
2024 for (next_iv = next_interval (iv);
2025 (!NULL_INTERVAL_P (next_iv)
2026 && (NILP (limit)
2027 || XFASTINT (limit) > next_iv->position));
2028 next_iv = next_interval (next_iv))
2029 {
2030 for (p = it_props; p->handler; ++p)
2031 {
2032 Lisp_Object new_value;
2033
2034 new_value = textget (next_iv->plist, *p->name);
2035 if (!EQ (values_here[p->idx], new_value))
2036 break;
2037 }
2038
2039 if (p->handler)
2040 break;
2041 }
2042
2043 if (!NULL_INTERVAL_P (next_iv))
2044 {
2045 if (INTEGERP (limit)
2046 && next_iv->position >= XFASTINT (limit))
2047 /* No text property change up to limit. */
2048 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2049 else
2050 /* Text properties change in next_iv. */
2051 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2052 }
2053 }
2054
2055 xassert (STRINGP (it->string)
2056 || (it->stop_charpos >= BEGV
2057 && it->stop_charpos >= IT_CHARPOS (*it)));
2058 }
2059
2060
2061 /* Return the position of the next overlay change after POS in
2062 current_buffer. Value is point-max if no overlay change
2063 follows. This is like `next-overlay-change' but doesn't use
2064 xmalloc. */
2065
2066 static int
2067 next_overlay_change (pos)
2068 int pos;
2069 {
2070 int noverlays;
2071 int endpos;
2072 Lisp_Object *overlays;
2073 int len;
2074 int i;
2075
2076 /* Get all overlays at the given position. */
2077 len = 10;
2078 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2079 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2080 if (noverlays > len)
2081 {
2082 len = noverlays;
2083 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2084 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2085 }
2086
2087 /* If any of these overlays ends before endpos,
2088 use its ending point instead. */
2089 for (i = 0; i < noverlays; ++i)
2090 {
2091 Lisp_Object oend;
2092 int oendpos;
2093
2094 oend = OVERLAY_END (overlays[i]);
2095 oendpos = OVERLAY_POSITION (oend);
2096 endpos = min (endpos, oendpos);
2097 }
2098
2099 return endpos;
2100 }
2101
2102
2103 \f
2104 /***********************************************************************
2105 Fontification
2106 ***********************************************************************/
2107
2108 /* Handle changes in the `fontified' property of the current buffer by
2109 calling hook functions from Qfontification_functions to fontify
2110 regions of text. */
2111
2112 static enum prop_handled
2113 handle_fontified_prop (it)
2114 struct it *it;
2115 {
2116 Lisp_Object prop, pos;
2117 enum prop_handled handled = HANDLED_NORMALLY;
2118
2119 /* Get the value of the `fontified' property at IT's current buffer
2120 position. (The `fontified' property doesn't have a special
2121 meaning in strings.) If the value is nil, call functions from
2122 Qfontification_functions. */
2123 if (!STRINGP (it->string)
2124 && it->s == NULL
2125 && !NILP (Vfontification_functions)
2126 && !NILP (Vrun_hooks)
2127 && (pos = make_number (IT_CHARPOS (*it)),
2128 prop = Fget_char_property (pos, Qfontified, Qnil),
2129 NILP (prop)))
2130 {
2131 int count = BINDING_STACK_SIZE ();
2132 Lisp_Object val;
2133
2134 val = Vfontification_functions;
2135 specbind (Qfontification_functions, Qnil);
2136 specbind (Qafter_change_functions, Qnil);
2137
2138 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2139 safe_call1 (val, pos);
2140 else
2141 {
2142 Lisp_Object globals, fn;
2143 struct gcpro gcpro1, gcpro2;
2144
2145 globals = Qnil;
2146 GCPRO2 (val, globals);
2147
2148 for (; CONSP (val); val = XCDR (val))
2149 {
2150 fn = XCAR (val);
2151
2152 if (EQ (fn, Qt))
2153 {
2154 /* A value of t indicates this hook has a local
2155 binding; it means to run the global binding too.
2156 In a global value, t should not occur. If it
2157 does, we must ignore it to avoid an endless
2158 loop. */
2159 for (globals = Fdefault_value (Qfontification_functions);
2160 CONSP (globals);
2161 globals = XCDR (globals))
2162 {
2163 fn = XCAR (globals);
2164 if (!EQ (fn, Qt))
2165 safe_call1 (fn, pos);
2166 }
2167 }
2168 else
2169 safe_call1 (fn, pos);
2170 }
2171
2172 UNGCPRO;
2173 }
2174
2175 unbind_to (count, Qnil);
2176
2177 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2178 something. This avoids an endless loop if they failed to
2179 fontify the text for which reason ever. */
2180 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2181 handled = HANDLED_RECOMPUTE_PROPS;
2182 }
2183
2184 return handled;
2185 }
2186
2187
2188 \f
2189 /***********************************************************************
2190 Faces
2191 ***********************************************************************/
2192
2193 /* Set up iterator IT from face properties at its current position.
2194 Called from handle_stop. */
2195
2196 static enum prop_handled
2197 handle_face_prop (it)
2198 struct it *it;
2199 {
2200 int new_face_id, next_stop;
2201
2202 if (!STRINGP (it->string))
2203 {
2204 new_face_id
2205 = face_at_buffer_position (it->w,
2206 IT_CHARPOS (*it),
2207 it->region_beg_charpos,
2208 it->region_end_charpos,
2209 &next_stop,
2210 (IT_CHARPOS (*it)
2211 + TEXT_PROP_DISTANCE_LIMIT),
2212 0);
2213
2214 /* Is this a start of a run of characters with box face?
2215 Caveat: this can be called for a freshly initialized
2216 iterator; face_id is -1 is this case. We know that the new
2217 face will not change until limit, i.e. if the new face has a
2218 box, all characters up to limit will have one. But, as
2219 usual, we don't know whether limit is really the end. */
2220 if (new_face_id != it->face_id)
2221 {
2222 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2223
2224 /* If new face has a box but old face has not, this is
2225 the start of a run of characters with box, i.e. it has
2226 a shadow on the left side. The value of face_id of the
2227 iterator will be -1 if this is the initial call that gets
2228 the face. In this case, we have to look in front of IT's
2229 position and see whether there is a face != new_face_id. */
2230 it->start_of_box_run_p
2231 = (new_face->box != FACE_NO_BOX
2232 && (it->face_id >= 0
2233 || IT_CHARPOS (*it) == BEG
2234 || new_face_id != face_before_it_pos (it)));
2235 it->face_box_p = new_face->box != FACE_NO_BOX;
2236 }
2237 }
2238 else
2239 {
2240 int base_face_id, bufpos;
2241
2242 if (it->current.overlay_string_index >= 0)
2243 bufpos = IT_CHARPOS (*it);
2244 else
2245 bufpos = 0;
2246
2247 /* For strings from a buffer, i.e. overlay strings or strings
2248 from a `display' property, use the face at IT's current
2249 buffer position as the base face to merge with, so that
2250 overlay strings appear in the same face as surrounding
2251 text, unless they specify their own faces. */
2252 base_face_id = underlying_face_id (it);
2253
2254 new_face_id = face_at_string_position (it->w,
2255 it->string,
2256 IT_STRING_CHARPOS (*it),
2257 bufpos,
2258 it->region_beg_charpos,
2259 it->region_end_charpos,
2260 &next_stop,
2261 base_face_id, 0);
2262
2263 #if 0 /* This shouldn't be neccessary. Let's check it. */
2264 /* If IT is used to display a mode line we would really like to
2265 use the mode line face instead of the frame's default face. */
2266 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2267 && new_face_id == DEFAULT_FACE_ID)
2268 new_face_id = MODE_LINE_FACE_ID;
2269 #endif
2270
2271 /* Is this a start of a run of characters with box? Caveat:
2272 this can be called for a freshly allocated iterator; face_id
2273 is -1 is this case. We know that the new face will not
2274 change until the next check pos, i.e. if the new face has a
2275 box, all characters up to that position will have a
2276 box. But, as usual, we don't know whether that position
2277 is really the end. */
2278 if (new_face_id != it->face_id)
2279 {
2280 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2281 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2282
2283 /* If new face has a box but old face hasn't, this is the
2284 start of a run of characters with box, i.e. it has a
2285 shadow on the left side. */
2286 it->start_of_box_run_p
2287 = new_face->box && (old_face == NULL || !old_face->box);
2288 it->face_box_p = new_face->box != FACE_NO_BOX;
2289 }
2290 }
2291
2292 it->face_id = new_face_id;
2293 return HANDLED_NORMALLY;
2294 }
2295
2296
2297 /* Return the ID of the face ``underlying'' IT's current position,
2298 which is in a string. If the iterator is associated with a
2299 buffer, return the face at IT's current buffer position.
2300 Otherwise, use the iterator's base_face_id. */
2301
2302 static int
2303 underlying_face_id (it)
2304 struct it *it;
2305 {
2306 int face_id = it->base_face_id, i;
2307
2308 xassert (STRINGP (it->string));
2309
2310 for (i = it->sp - 1; i >= 0; --i)
2311 if (NILP (it->stack[i].string))
2312 face_id = it->stack[i].face_id;
2313
2314 return face_id;
2315 }
2316
2317
2318 /* Compute the face one character before or after the current position
2319 of IT. BEFORE_P non-zero means get the face in front of IT's
2320 position. Value is the id of the face. */
2321
2322 static int
2323 face_before_or_after_it_pos (it, before_p)
2324 struct it *it;
2325 int before_p;
2326 {
2327 int face_id, limit;
2328 int next_check_charpos;
2329 struct text_pos pos;
2330
2331 xassert (it->s == NULL);
2332
2333 if (STRINGP (it->string))
2334 {
2335 int bufpos, base_face_id;
2336
2337 /* No face change past the end of the string (for the case
2338 we are padding with spaces). No face change before the
2339 string start. */
2340 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size
2341 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2342 return it->face_id;
2343
2344 /* Set pos to the position before or after IT's current position. */
2345 if (before_p)
2346 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2347 else
2348 /* For composition, we must check the character after the
2349 composition. */
2350 pos = (it->what == IT_COMPOSITION
2351 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
2352 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
2353
2354 if (it->current.overlay_string_index >= 0)
2355 bufpos = IT_CHARPOS (*it);
2356 else
2357 bufpos = 0;
2358
2359 base_face_id = underlying_face_id (it);
2360
2361 /* Get the face for ASCII, or unibyte. */
2362 face_id = face_at_string_position (it->w,
2363 it->string,
2364 CHARPOS (pos),
2365 bufpos,
2366 it->region_beg_charpos,
2367 it->region_end_charpos,
2368 &next_check_charpos,
2369 base_face_id, 0);
2370
2371 /* Correct the face for charsets different from ASCII. Do it
2372 for the multibyte case only. The face returned above is
2373 suitable for unibyte text if IT->string is unibyte. */
2374 if (STRING_MULTIBYTE (it->string))
2375 {
2376 unsigned char *p = XSTRING (it->string)->data + BYTEPOS (pos);
2377 int rest = STRING_BYTES (XSTRING (it->string)) - BYTEPOS (pos);
2378 int c, len;
2379 struct face *face = FACE_FROM_ID (it->f, face_id);
2380
2381 c = string_char_and_length (p, rest, &len);
2382 face_id = FACE_FOR_CHAR (it->f, face, c);
2383 }
2384 }
2385 else
2386 {
2387 if ((IT_CHARPOS (*it) >= ZV && !before_p)
2388 || (IT_CHARPOS (*it) <= BEGV && before_p))
2389 return it->face_id;
2390
2391 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
2392 pos = it->current.pos;
2393
2394 if (before_p)
2395 DEC_TEXT_POS (pos, it->multibyte_p);
2396 else
2397 {
2398 if (it->what == IT_COMPOSITION)
2399 /* For composition, we must check the position after the
2400 composition. */
2401 pos.charpos += it->cmp_len, pos.bytepos += it->len;
2402 else
2403 INC_TEXT_POS (pos, it->multibyte_p);
2404 }
2405
2406 /* Determine face for CHARSET_ASCII, or unibyte. */
2407 face_id = face_at_buffer_position (it->w,
2408 CHARPOS (pos),
2409 it->region_beg_charpos,
2410 it->region_end_charpos,
2411 &next_check_charpos,
2412 limit, 0);
2413
2414 /* Correct the face for charsets different from ASCII. Do it
2415 for the multibyte case only. The face returned above is
2416 suitable for unibyte text if current_buffer is unibyte. */
2417 if (it->multibyte_p)
2418 {
2419 int c = FETCH_MULTIBYTE_CHAR (CHARPOS (pos));
2420 struct face *face = FACE_FROM_ID (it->f, face_id);
2421 face_id = FACE_FOR_CHAR (it->f, face, c);
2422 }
2423 }
2424
2425 return face_id;
2426 }
2427
2428
2429 \f
2430 /***********************************************************************
2431 Invisible text
2432 ***********************************************************************/
2433
2434 /* Set up iterator IT from invisible properties at its current
2435 position. Called from handle_stop. */
2436
2437 static enum prop_handled
2438 handle_invisible_prop (it)
2439 struct it *it;
2440 {
2441 enum prop_handled handled = HANDLED_NORMALLY;
2442
2443 if (STRINGP (it->string))
2444 {
2445 extern Lisp_Object Qinvisible;
2446 Lisp_Object prop, end_charpos, limit, charpos;
2447
2448 /* Get the value of the invisible text property at the
2449 current position. Value will be nil if there is no such
2450 property. */
2451 charpos = make_number (IT_STRING_CHARPOS (*it));
2452 prop = Fget_text_property (charpos, Qinvisible, it->string);
2453
2454 if (!NILP (prop)
2455 && IT_STRING_CHARPOS (*it) < it->end_charpos)
2456 {
2457 handled = HANDLED_RECOMPUTE_PROPS;
2458
2459 /* Get the position at which the next change of the
2460 invisible text property can be found in IT->string.
2461 Value will be nil if the property value is the same for
2462 all the rest of IT->string. */
2463 XSETINT (limit, XSTRING (it->string)->size);
2464 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2465 it->string, limit);
2466
2467 /* Text at current position is invisible. The next
2468 change in the property is at position end_charpos.
2469 Move IT's current position to that position. */
2470 if (INTEGERP (end_charpos)
2471 && XFASTINT (end_charpos) < XFASTINT (limit))
2472 {
2473 struct text_pos old;
2474 old = it->current.string_pos;
2475 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
2476 compute_string_pos (&it->current.string_pos, old, it->string);
2477 }
2478 else
2479 {
2480 /* The rest of the string is invisible. If this is an
2481 overlay string, proceed with the next overlay string
2482 or whatever comes and return a character from there. */
2483 if (it->current.overlay_string_index >= 0)
2484 {
2485 next_overlay_string (it);
2486 /* Don't check for overlay strings when we just
2487 finished processing them. */
2488 handled = HANDLED_OVERLAY_STRING_CONSUMED;
2489 }
2490 else
2491 {
2492 struct Lisp_String *s = XSTRING (it->string);
2493 IT_STRING_CHARPOS (*it) = s->size;
2494 IT_STRING_BYTEPOS (*it) = STRING_BYTES (s);
2495 }
2496 }
2497 }
2498 }
2499 else
2500 {
2501 int visible_p, newpos, next_stop;
2502 Lisp_Object pos, prop;
2503
2504 /* First of all, is there invisible text at this position? */
2505 pos = make_number (IT_CHARPOS (*it));
2506 prop = Fget_char_property (pos, Qinvisible, it->window);
2507
2508 /* If we are on invisible text, skip over it. */
2509 if (TEXT_PROP_MEANS_INVISIBLE (prop)
2510 && IT_CHARPOS (*it) < it->end_charpos)
2511 {
2512 /* Record whether we have to display an ellipsis for the
2513 invisible text. */
2514 int display_ellipsis_p
2515 = TEXT_PROP_MEANS_INVISIBLE_WITH_ELLIPSIS (prop);
2516
2517 handled = HANDLED_RECOMPUTE_PROPS;
2518
2519 /* Loop skipping over invisible text. The loop is left at
2520 ZV or with IT on the first char being visible again. */
2521 do
2522 {
2523 /* Try to skip some invisible text. Return value is the
2524 position reached which can be equal to IT's position
2525 if there is nothing invisible here. This skips both
2526 over invisible text properties and overlays with
2527 invisible property. */
2528 newpos = skip_invisible (IT_CHARPOS (*it),
2529 &next_stop, ZV, it->window);
2530
2531 /* If we skipped nothing at all we weren't at invisible
2532 text in the first place. If everything to the end of
2533 the buffer was skipped, end the loop. */
2534 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
2535 visible_p = 1;
2536 else
2537 {
2538 /* We skipped some characters but not necessarily
2539 all there are. Check if we ended up on visible
2540 text. Fget_char_property returns the property of
2541 the char before the given position, i.e. if we
2542 get visible_p = 1, this means that the char at
2543 newpos is visible. */
2544 pos = make_number (newpos);
2545 prop = Fget_char_property (pos, Qinvisible, it->window);
2546 visible_p = !TEXT_PROP_MEANS_INVISIBLE (prop);
2547 }
2548
2549 /* If we ended up on invisible text, proceed to
2550 skip starting with next_stop. */
2551 if (!visible_p)
2552 IT_CHARPOS (*it) = next_stop;
2553 }
2554 while (!visible_p);
2555
2556 /* The position newpos is now either ZV or on visible text. */
2557 IT_CHARPOS (*it) = newpos;
2558 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2559
2560 /* Maybe return `...' next for the end of the invisible text. */
2561 if (display_ellipsis_p)
2562 {
2563 if (it->dp
2564 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
2565 {
2566 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
2567 it->dpvec = v->contents;
2568 it->dpend = v->contents + v->size;
2569 }
2570 else
2571 {
2572 /* Default `...'. */
2573 it->dpvec = default_invis_vector;
2574 it->dpend = default_invis_vector + 3;
2575 }
2576
2577 /* The ellipsis display does not replace the display of
2578 the character at the new position. Indicate this by
2579 setting IT->dpvec_char_len to zero. */
2580 it->dpvec_char_len = 0;
2581
2582 it->current.dpvec_index = 0;
2583 it->method = next_element_from_display_vector;
2584 }
2585 }
2586 }
2587
2588 return handled;
2589 }
2590
2591
2592 \f
2593 /***********************************************************************
2594 'display' property
2595 ***********************************************************************/
2596
2597 /* Set up iterator IT from `display' property at its current position.
2598 Called from handle_stop. */
2599
2600 static enum prop_handled
2601 handle_display_prop (it)
2602 struct it *it;
2603 {
2604 Lisp_Object prop, object;
2605 struct text_pos *position;
2606 int display_replaced_p = 0;
2607
2608 if (STRINGP (it->string))
2609 {
2610 object = it->string;
2611 position = &it->current.string_pos;
2612 }
2613 else
2614 {
2615 object = it->w->buffer;
2616 position = &it->current.pos;
2617 }
2618
2619 /* Reset those iterator values set from display property values. */
2620 it->font_height = Qnil;
2621 it->space_width = Qnil;
2622 it->voffset = 0;
2623
2624 /* We don't support recursive `display' properties, i.e. string
2625 values that have a string `display' property, that have a string
2626 `display' property etc. */
2627 if (!it->string_from_display_prop_p)
2628 it->area = TEXT_AREA;
2629
2630 prop = Fget_char_property (make_number (position->charpos),
2631 Qdisplay, object);
2632 if (NILP (prop))
2633 return HANDLED_NORMALLY;
2634
2635 if (CONSP (prop)
2636 && CONSP (XCAR (prop))
2637 && !EQ (Qmargin, XCAR (XCAR (prop))))
2638 {
2639 /* A list of sub-properties. */
2640 for (; CONSP (prop); prop = XCDR (prop))
2641 {
2642 if (handle_single_display_prop (it, XCAR (prop), object,
2643 position, display_replaced_p))
2644 display_replaced_p = 1;
2645 }
2646 }
2647 else if (VECTORP (prop))
2648 {
2649 int i;
2650 for (i = 0; i < ASIZE (prop); ++i)
2651 if (handle_single_display_prop (it, AREF (prop, i), object,
2652 position, display_replaced_p))
2653 display_replaced_p = 1;
2654 }
2655 else
2656 {
2657 if (handle_single_display_prop (it, prop, object, position, 0))
2658 display_replaced_p = 1;
2659 }
2660
2661 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
2662 }
2663
2664
2665 /* Value is the position of the end of the `display' property starting
2666 at START_POS in OBJECT. */
2667
2668 static struct text_pos
2669 display_prop_end (it, object, start_pos)
2670 struct it *it;
2671 Lisp_Object object;
2672 struct text_pos start_pos;
2673 {
2674 Lisp_Object end;
2675 struct text_pos end_pos;
2676
2677 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
2678 Qdisplay, object, Qnil);
2679 CHARPOS (end_pos) = XFASTINT (end);
2680 if (STRINGP (object))
2681 compute_string_pos (&end_pos, start_pos, it->string);
2682 else
2683 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
2684
2685 return end_pos;
2686 }
2687
2688
2689 /* Set up IT from a single `display' sub-property value PROP. OBJECT
2690 is the object in which the `display' property was found. *POSITION
2691 is the position at which it was found. DISPLAY_REPLACED_P non-zero
2692 means that we previously saw a display sub-property which already
2693 replaced text display with something else, for example an image;
2694 ignore such properties after the first one has been processed.
2695
2696 If PROP is a `space' or `image' sub-property, set *POSITION to the
2697 end position of the `display' property.
2698
2699 Value is non-zero something was found which replaces the display
2700 of buffer or string text. */
2701
2702 static int
2703 handle_single_display_prop (it, prop, object, position,
2704 display_replaced_before_p)
2705 struct it *it;
2706 Lisp_Object prop;
2707 Lisp_Object object;
2708 struct text_pos *position;
2709 int display_replaced_before_p;
2710 {
2711 Lisp_Object value;
2712 int replaces_text_display_p = 0;
2713 Lisp_Object form;
2714
2715 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
2716 evaluated. If the result is nil, VALUE is ignored. */
2717 form = Qt;
2718 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2719 {
2720 prop = XCDR (prop);
2721 if (!CONSP (prop))
2722 return 0;
2723 form = XCAR (prop);
2724 prop = XCDR (prop);
2725 }
2726
2727 if (!NILP (form) && !EQ (form, Qt))
2728 {
2729 struct gcpro gcpro1;
2730 struct text_pos end_pos, pt;
2731
2732 GCPRO1 (form);
2733 end_pos = display_prop_end (it, object, *position);
2734
2735 /* Temporarily set point to the end position, and then evaluate
2736 the form. This makes `(eolp)' work as FORM. */
2737 if (BUFFERP (object))
2738 {
2739 CHARPOS (pt) = PT;
2740 BYTEPOS (pt) = PT_BYTE;
2741 TEMP_SET_PT_BOTH (CHARPOS (end_pos), BYTEPOS (end_pos));
2742 }
2743
2744 form = safe_eval (form);
2745
2746 if (BUFFERP (object))
2747 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
2748 UNGCPRO;
2749 }
2750
2751 if (NILP (form))
2752 return 0;
2753
2754 if (CONSP (prop)
2755 && EQ (XCAR (prop), Qheight)
2756 && CONSP (XCDR (prop)))
2757 {
2758 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2759 return 0;
2760
2761 /* `(height HEIGHT)'. */
2762 it->font_height = XCAR (XCDR (prop));
2763 if (!NILP (it->font_height))
2764 {
2765 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2766 int new_height = -1;
2767
2768 if (CONSP (it->font_height)
2769 && (EQ (XCAR (it->font_height), Qplus)
2770 || EQ (XCAR (it->font_height), Qminus))
2771 && CONSP (XCDR (it->font_height))
2772 && INTEGERP (XCAR (XCDR (it->font_height))))
2773 {
2774 /* `(+ N)' or `(- N)' where N is an integer. */
2775 int steps = XINT (XCAR (XCDR (it->font_height)));
2776 if (EQ (XCAR (it->font_height), Qplus))
2777 steps = - steps;
2778 it->face_id = smaller_face (it->f, it->face_id, steps);
2779 }
2780 else if (FUNCTIONP (it->font_height))
2781 {
2782 /* Call function with current height as argument.
2783 Value is the new height. */
2784 Lisp_Object height;
2785 height = safe_call1 (it->font_height,
2786 face->lface[LFACE_HEIGHT_INDEX]);
2787 if (NUMBERP (height))
2788 new_height = XFLOATINT (height);
2789 }
2790 else if (NUMBERP (it->font_height))
2791 {
2792 /* Value is a multiple of the canonical char height. */
2793 struct face *face;
2794
2795 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
2796 new_height = (XFLOATINT (it->font_height)
2797 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
2798 }
2799 else
2800 {
2801 /* Evaluate IT->font_height with `height' bound to the
2802 current specified height to get the new height. */
2803 Lisp_Object value;
2804 int count = BINDING_STACK_SIZE ();
2805
2806 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
2807 value = safe_eval (it->font_height);
2808 unbind_to (count, Qnil);
2809
2810 if (NUMBERP (value))
2811 new_height = XFLOATINT (value);
2812 }
2813
2814 if (new_height > 0)
2815 it->face_id = face_with_height (it->f, it->face_id, new_height);
2816 }
2817 }
2818 else if (CONSP (prop)
2819 && EQ (XCAR (prop), Qspace_width)
2820 && CONSP (XCDR (prop)))
2821 {
2822 /* `(space_width WIDTH)'. */
2823 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2824 return 0;
2825
2826 value = XCAR (XCDR (prop));
2827 if (NUMBERP (value) && XFLOATINT (value) > 0)
2828 it->space_width = value;
2829 }
2830 else if (CONSP (prop)
2831 && EQ (XCAR (prop), Qraise)
2832 && CONSP (XCDR (prop)))
2833 {
2834 /* `(raise FACTOR)'. */
2835 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2836 return 0;
2837
2838 #ifdef HAVE_WINDOW_SYSTEM
2839 value = XCAR (XCDR (prop));
2840 if (NUMBERP (value))
2841 {
2842 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2843 it->voffset = - (XFLOATINT (value)
2844 * (FONT_HEIGHT (face->font)));
2845 }
2846 #endif /* HAVE_WINDOW_SYSTEM */
2847 }
2848 else if (!it->string_from_display_prop_p)
2849 {
2850 /* `((margin left-margin) VALUE)' or `((margin right-margin)
2851 VALUE) or `((margin nil) VALUE)' or VALUE. */
2852 Lisp_Object location, value;
2853 struct text_pos start_pos;
2854 int valid_p;
2855
2856 /* Characters having this form of property are not displayed, so
2857 we have to find the end of the property. */
2858 start_pos = *position;
2859 *position = display_prop_end (it, object, start_pos);
2860 value = Qnil;
2861
2862 /* Let's stop at the new position and assume that all
2863 text properties change there. */
2864 it->stop_charpos = position->charpos;
2865
2866 location = Qunbound;
2867 if (CONSP (prop) && CONSP (XCAR (prop)))
2868 {
2869 Lisp_Object tem;
2870
2871 value = XCDR (prop);
2872 if (CONSP (value))
2873 value = XCAR (value);
2874
2875 tem = XCAR (prop);
2876 if (EQ (XCAR (tem), Qmargin)
2877 && (tem = XCDR (tem),
2878 tem = CONSP (tem) ? XCAR (tem) : Qnil,
2879 (NILP (tem)
2880 || EQ (tem, Qleft_margin)
2881 || EQ (tem, Qright_margin))))
2882 location = tem;
2883 }
2884
2885 if (EQ (location, Qunbound))
2886 {
2887 location = Qnil;
2888 value = prop;
2889 }
2890
2891 #ifdef HAVE_WINDOW_SYSTEM
2892 if (FRAME_TERMCAP_P (it->f))
2893 valid_p = STRINGP (value);
2894 else
2895 valid_p = (STRINGP (value)
2896 || (CONSP (value) && EQ (XCAR (value), Qspace))
2897 || valid_image_p (value));
2898 #else /* not HAVE_WINDOW_SYSTEM */
2899 valid_p = STRINGP (value);
2900 #endif /* not HAVE_WINDOW_SYSTEM */
2901
2902 if ((EQ (location, Qleft_margin)
2903 || EQ (location, Qright_margin)
2904 || NILP (location))
2905 && valid_p
2906 && !display_replaced_before_p)
2907 {
2908 replaces_text_display_p = 1;
2909
2910 /* Save current settings of IT so that we can restore them
2911 when we are finished with the glyph property value. */
2912 push_it (it);
2913
2914 if (NILP (location))
2915 it->area = TEXT_AREA;
2916 else if (EQ (location, Qleft_margin))
2917 it->area = LEFT_MARGIN_AREA;
2918 else
2919 it->area = RIGHT_MARGIN_AREA;
2920
2921 if (STRINGP (value))
2922 {
2923 it->string = value;
2924 it->multibyte_p = STRING_MULTIBYTE (it->string);
2925 it->current.overlay_string_index = -1;
2926 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
2927 it->end_charpos = it->string_nchars
2928 = XSTRING (it->string)->size;
2929 it->method = next_element_from_string;
2930 it->stop_charpos = 0;
2931 it->string_from_display_prop_p = 1;
2932 /* Say that we haven't consumed the characters with
2933 `display' property yet. The call to pop_it in
2934 set_iterator_to_next will clean this up. */
2935 *position = start_pos;
2936 }
2937 else if (CONSP (value) && EQ (XCAR (value), Qspace))
2938 {
2939 it->method = next_element_from_stretch;
2940 it->object = value;
2941 it->current.pos = it->position = start_pos;
2942 }
2943 #ifdef HAVE_WINDOW_SYSTEM
2944 else
2945 {
2946 it->what = IT_IMAGE;
2947 it->image_id = lookup_image (it->f, value);
2948 it->position = start_pos;
2949 it->object = NILP (object) ? it->w->buffer : object;
2950 it->method = next_element_from_image;
2951
2952 /* Say that we haven't consumed the characters with
2953 `display' property yet. The call to pop_it in
2954 set_iterator_to_next will clean this up. */
2955 *position = start_pos;
2956 }
2957 #endif /* HAVE_WINDOW_SYSTEM */
2958 }
2959 else
2960 /* Invalid property or property not supported. Restore
2961 the position to what it was before. */
2962 *position = start_pos;
2963 }
2964
2965 return replaces_text_display_p;
2966 }
2967
2968
2969 /* Check if PROP is a display sub-property value whose text should be
2970 treated as intangible. */
2971
2972 static int
2973 single_display_prop_intangible_p (prop)
2974 Lisp_Object prop;
2975 {
2976 /* Skip over `when FORM'. */
2977 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2978 {
2979 prop = XCDR (prop);
2980 if (!CONSP (prop))
2981 return 0;
2982 prop = XCDR (prop);
2983 }
2984
2985 if (!CONSP (prop))
2986 return 0;
2987
2988 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
2989 we don't need to treat text as intangible. */
2990 if (EQ (XCAR (prop), Qmargin))
2991 {
2992 prop = XCDR (prop);
2993 if (!CONSP (prop))
2994 return 0;
2995
2996 prop = XCDR (prop);
2997 if (!CONSP (prop)
2998 || EQ (XCAR (prop), Qleft_margin)
2999 || EQ (XCAR (prop), Qright_margin))
3000 return 0;
3001 }
3002
3003 return CONSP (prop) && EQ (XCAR (prop), Qimage);
3004 }
3005
3006
3007 /* Check if PROP is a display property value whose text should be
3008 treated as intangible. */
3009
3010 int
3011 display_prop_intangible_p (prop)
3012 Lisp_Object prop;
3013 {
3014 if (CONSP (prop)
3015 && CONSP (XCAR (prop))
3016 && !EQ (Qmargin, XCAR (XCAR (prop))))
3017 {
3018 /* A list of sub-properties. */
3019 while (CONSP (prop))
3020 {
3021 if (single_display_prop_intangible_p (XCAR (prop)))
3022 return 1;
3023 prop = XCDR (prop);
3024 }
3025 }
3026 else if (VECTORP (prop))
3027 {
3028 /* A vector of sub-properties. */
3029 int i;
3030 for (i = 0; i < ASIZE (prop); ++i)
3031 if (single_display_prop_intangible_p (AREF (prop, i)))
3032 return 1;
3033 }
3034 else
3035 return single_display_prop_intangible_p (prop);
3036
3037 return 0;
3038 }
3039
3040
3041 /* Return 1 if PROP is a display sub-property value containing STRING. */
3042
3043 static int
3044 single_display_prop_string_p (prop, string)
3045 Lisp_Object prop, string;
3046 {
3047 extern Lisp_Object Qwhen, Qmargin;
3048
3049 if (EQ (string, prop))
3050 return 1;
3051
3052 /* Skip over `when FORM'. */
3053 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3054 {
3055 prop = XCDR (prop);
3056 if (!CONSP (prop))
3057 return 0;
3058 prop = XCDR (prop);
3059 }
3060
3061 if (CONSP (prop))
3062 /* Skip over `margin LOCATION'. */
3063 if (EQ (XCAR (prop), Qmargin))
3064 {
3065 prop = XCDR (prop);
3066 if (!CONSP (prop))
3067 return 0;
3068
3069 prop = XCDR (prop);
3070 if (!CONSP (prop))
3071 return 0;
3072 }
3073
3074 return CONSP (prop) && EQ (XCAR (prop), string);
3075 }
3076
3077
3078 /* Return 1 if STRING appears in the `display' property PROP. */
3079
3080 static int
3081 display_prop_string_p (prop, string)
3082 Lisp_Object prop, string;
3083 {
3084 extern Lisp_Object Qwhen, Qmargin;
3085
3086 if (CONSP (prop)
3087 && CONSP (XCAR (prop))
3088 && !EQ (Qmargin, XCAR (XCAR (prop))))
3089 {
3090 /* A list of sub-properties. */
3091 while (CONSP (prop))
3092 {
3093 if (single_display_prop_string_p (XCAR (prop), string))
3094 return 1;
3095 prop = XCDR (prop);
3096 }
3097 }
3098 else if (VECTORP (prop))
3099 {
3100 /* A vector of sub-properties. */
3101 int i;
3102 for (i = 0; i < ASIZE (prop); ++i)
3103 if (single_display_prop_string_p (AREF (prop, i), string))
3104 return 1;
3105 }
3106 else
3107 return single_display_prop_string_p (prop, string);
3108
3109 return 0;
3110 }
3111
3112
3113 /* Determine from which buffer position in W's buffer STRING comes
3114 from. AROUND_CHARPOS is an approximate position where it could
3115 be from. Value is the buffer position or 0 if it couldn't be
3116 determined.
3117
3118 W's buffer must be current.
3119
3120 This function is necessary because we don't record buffer positions
3121 in glyphs generated from strings (to keep struct glyph small).
3122 This function may only use code that doesn't eval because it is
3123 called asynchronously from note_mouse_highlight. */
3124
3125 int
3126 string_buffer_position (w, string, around_charpos)
3127 struct window *w;
3128 Lisp_Object string;
3129 int around_charpos;
3130 {
3131 Lisp_Object around = make_number (around_charpos);
3132 Lisp_Object limit, prop, pos;
3133 const int MAX_DISTANCE = 1000;
3134 int found = 0;
3135
3136 pos = make_number (around_charpos);
3137 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
3138 while (!found && !EQ (pos, limit))
3139 {
3140 prop = Fget_char_property (pos, Qdisplay, Qnil);
3141 if (!NILP (prop) && display_prop_string_p (prop, string))
3142 found = 1;
3143 else
3144 pos = Fnext_single_property_change (pos, Qdisplay, Qnil, limit);
3145 }
3146
3147 if (!found)
3148 {
3149 pos = make_number (around_charpos);
3150 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
3151 while (!found && !EQ (pos, limit))
3152 {
3153 prop = Fget_char_property (pos, Qdisplay, Qnil);
3154 if (!NILP (prop) && display_prop_string_p (prop, string))
3155 found = 1;
3156 else
3157 pos = Fprevious_single_property_change (pos, Qdisplay, Qnil,
3158 limit);
3159 }
3160 }
3161
3162 return found ? XINT (pos) : 0;
3163 }
3164
3165
3166 \f
3167 /***********************************************************************
3168 `composition' property
3169 ***********************************************************************/
3170
3171 /* Set up iterator IT from `composition' property at its current
3172 position. Called from handle_stop. */
3173
3174 static enum prop_handled
3175 handle_composition_prop (it)
3176 struct it *it;
3177 {
3178 Lisp_Object prop, string;
3179 int pos, pos_byte, end;
3180 enum prop_handled handled = HANDLED_NORMALLY;
3181
3182 if (STRINGP (it->string))
3183 {
3184 pos = IT_STRING_CHARPOS (*it);
3185 pos_byte = IT_STRING_BYTEPOS (*it);
3186 string = it->string;
3187 }
3188 else
3189 {
3190 pos = IT_CHARPOS (*it);
3191 pos_byte = IT_BYTEPOS (*it);
3192 string = Qnil;
3193 }
3194
3195 /* If there's a valid composition and point is not inside of the
3196 composition (in the case that the composition is from the current
3197 buffer), draw a glyph composed from the composition components. */
3198 if (find_composition (pos, -1, &pos, &end, &prop, string)
3199 && COMPOSITION_VALID_P (pos, end, prop)
3200 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
3201 {
3202 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
3203
3204 if (id >= 0)
3205 {
3206 it->method = next_element_from_composition;
3207 it->cmp_id = id;
3208 it->cmp_len = COMPOSITION_LENGTH (prop);
3209 /* For a terminal, draw only the first character of the
3210 components. */
3211 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
3212 it->len = (STRINGP (it->string)
3213 ? string_char_to_byte (it->string, end)
3214 : CHAR_TO_BYTE (end)) - pos_byte;
3215 it->stop_charpos = end;
3216 handled = HANDLED_RETURN;
3217 }
3218 }
3219
3220 return handled;
3221 }
3222
3223
3224 \f
3225 /***********************************************************************
3226 Overlay strings
3227 ***********************************************************************/
3228
3229 /* The following structure is used to record overlay strings for
3230 later sorting in load_overlay_strings. */
3231
3232 struct overlay_entry
3233 {
3234 Lisp_Object overlay;
3235 Lisp_Object string;
3236 int priority;
3237 int after_string_p;
3238 };
3239
3240
3241 /* Set up iterator IT from overlay strings at its current position.
3242 Called from handle_stop. */
3243
3244 static enum prop_handled
3245 handle_overlay_change (it)
3246 struct it *it;
3247 {
3248 if (!STRINGP (it->string) && get_overlay_strings (it))
3249 return HANDLED_RECOMPUTE_PROPS;
3250 else
3251 return HANDLED_NORMALLY;
3252 }
3253
3254
3255 /* Set up the next overlay string for delivery by IT, if there is an
3256 overlay string to deliver. Called by set_iterator_to_next when the
3257 end of the current overlay string is reached. If there are more
3258 overlay strings to display, IT->string and
3259 IT->current.overlay_string_index are set appropriately here.
3260 Otherwise IT->string is set to nil. */
3261
3262 static void
3263 next_overlay_string (it)
3264 struct it *it;
3265 {
3266 ++it->current.overlay_string_index;
3267 if (it->current.overlay_string_index == it->n_overlay_strings)
3268 {
3269 /* No more overlay strings. Restore IT's settings to what
3270 they were before overlay strings were processed, and
3271 continue to deliver from current_buffer. */
3272 pop_it (it);
3273 xassert (it->stop_charpos >= BEGV
3274 && it->stop_charpos <= it->end_charpos);
3275 it->string = Qnil;
3276 it->current.overlay_string_index = -1;
3277 SET_TEXT_POS (it->current.string_pos, -1, -1);
3278 it->n_overlay_strings = 0;
3279 it->method = next_element_from_buffer;
3280
3281 /* If we're at the end of the buffer, record that we have
3282 processed the overlay strings there already, so that
3283 next_element_from_buffer doesn't try it again. */
3284 if (IT_CHARPOS (*it) >= it->end_charpos)
3285 it->overlay_strings_at_end_processed_p = 1;
3286 }
3287 else
3288 {
3289 /* There are more overlay strings to process. If
3290 IT->current.overlay_string_index has advanced to a position
3291 where we must load IT->overlay_strings with more strings, do
3292 it. */
3293 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
3294
3295 if (it->current.overlay_string_index && i == 0)
3296 load_overlay_strings (it);
3297
3298 /* Initialize IT to deliver display elements from the overlay
3299 string. */
3300 it->string = it->overlay_strings[i];
3301 it->multibyte_p = STRING_MULTIBYTE (it->string);
3302 SET_TEXT_POS (it->current.string_pos, 0, 0);
3303 it->method = next_element_from_string;
3304 it->stop_charpos = 0;
3305 }
3306
3307 CHECK_IT (it);
3308 }
3309
3310
3311 /* Compare two overlay_entry structures E1 and E2. Used as a
3312 comparison function for qsort in load_overlay_strings. Overlay
3313 strings for the same position are sorted so that
3314
3315 1. All after-strings come in front of before-strings, except
3316 when they come from the same overlay.
3317
3318 2. Within after-strings, strings are sorted so that overlay strings
3319 from overlays with higher priorities come first.
3320
3321 2. Within before-strings, strings are sorted so that overlay
3322 strings from overlays with higher priorities come last.
3323
3324 Value is analogous to strcmp. */
3325
3326
3327 static int
3328 compare_overlay_entries (e1, e2)
3329 void *e1, *e2;
3330 {
3331 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
3332 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
3333 int result;
3334
3335 if (entry1->after_string_p != entry2->after_string_p)
3336 {
3337 /* Let after-strings appear in front of before-strings if
3338 they come from different overlays. */
3339 if (EQ (entry1->overlay, entry2->overlay))
3340 result = entry1->after_string_p ? 1 : -1;
3341 else
3342 result = entry1->after_string_p ? -1 : 1;
3343 }
3344 else if (entry1->after_string_p)
3345 /* After-strings sorted in order of decreasing priority. */
3346 result = entry2->priority - entry1->priority;
3347 else
3348 /* Before-strings sorted in order of increasing priority. */
3349 result = entry1->priority - entry2->priority;
3350
3351 return result;
3352 }
3353
3354
3355 /* Load the vector IT->overlay_strings with overlay strings from IT's
3356 current buffer position. Set IT->n_overlays to the total number of
3357 overlay strings found.
3358
3359 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
3360 a time. On entry into load_overlay_strings,
3361 IT->current.overlay_string_index gives the number of overlay
3362 strings that have already been loaded by previous calls to this
3363 function.
3364
3365 IT->add_overlay_start contains an additional overlay start
3366 position to consider for taking overlay strings from, if non-zero.
3367 This position comes into play when the overlay has an `invisible'
3368 property, and both before and after-strings. When we've skipped to
3369 the end of the overlay, because of its `invisible' property, we
3370 nevertheless want its before-string to appear.
3371 IT->add_overlay_start will contain the overlay start position
3372 in this case.
3373
3374 Overlay strings are sorted so that after-string strings come in
3375 front of before-string strings. Within before and after-strings,
3376 strings are sorted by overlay priority. See also function
3377 compare_overlay_entries. */
3378
3379 static void
3380 load_overlay_strings (it)
3381 struct it *it;
3382 {
3383 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
3384 Lisp_Object ov, overlay, window, str, invisible;
3385 int start, end;
3386 int size = 20;
3387 int n = 0, i, j, invis_p;
3388 struct overlay_entry *entries
3389 = (struct overlay_entry *) alloca (size * sizeof *entries);
3390 int charpos = IT_CHARPOS (*it);
3391
3392 /* Append the overlay string STRING of overlay OVERLAY to vector
3393 `entries' which has size `size' and currently contains `n'
3394 elements. AFTER_P non-zero means STRING is an after-string of
3395 OVERLAY. */
3396 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
3397 do \
3398 { \
3399 Lisp_Object priority; \
3400 \
3401 if (n == size) \
3402 { \
3403 int new_size = 2 * size; \
3404 struct overlay_entry *old = entries; \
3405 entries = \
3406 (struct overlay_entry *) alloca (new_size \
3407 * sizeof *entries); \
3408 bcopy (old, entries, size * sizeof *entries); \
3409 size = new_size; \
3410 } \
3411 \
3412 entries[n].string = (STRING); \
3413 entries[n].overlay = (OVERLAY); \
3414 priority = Foverlay_get ((OVERLAY), Qpriority); \
3415 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
3416 entries[n].after_string_p = (AFTER_P); \
3417 ++n; \
3418 } \
3419 while (0)
3420
3421 /* Process overlay before the overlay center. */
3422 for (ov = current_buffer->overlays_before; CONSP (ov); ov = XCDR (ov))
3423 {
3424 overlay = XCAR (ov);
3425 xassert (OVERLAYP (overlay));
3426 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3427 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3428
3429 if (end < charpos)
3430 break;
3431
3432 /* Skip this overlay if it doesn't start or end at IT's current
3433 position. */
3434 if (end != charpos && start != charpos)
3435 continue;
3436
3437 /* Skip this overlay if it doesn't apply to IT->w. */
3438 window = Foverlay_get (overlay, Qwindow);
3439 if (WINDOWP (window) && XWINDOW (window) != it->w)
3440 continue;
3441
3442 /* If the text ``under'' the overlay is invisible, both before-
3443 and after-strings from this overlay are visible; start and
3444 end position are indistinguishable. */
3445 invisible = Foverlay_get (overlay, Qinvisible);
3446 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3447
3448 /* If overlay has a non-empty before-string, record it. */
3449 if ((start == charpos || (end == charpos && invis_p))
3450 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3451 && XSTRING (str)->size)
3452 RECORD_OVERLAY_STRING (overlay, str, 0);
3453
3454 /* If overlay has a non-empty after-string, record it. */
3455 if ((end == charpos || (start == charpos && invis_p))
3456 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3457 && XSTRING (str)->size)
3458 RECORD_OVERLAY_STRING (overlay, str, 1);
3459 }
3460
3461 /* Process overlays after the overlay center. */
3462 for (ov = current_buffer->overlays_after; CONSP (ov); ov = XCDR (ov))
3463 {
3464 overlay = XCAR (ov);
3465 xassert (OVERLAYP (overlay));
3466 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3467 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3468
3469 if (start > charpos)
3470 break;
3471
3472 /* Skip this overlay if it doesn't start or end at IT's current
3473 position. */
3474 if (end != charpos && start != charpos)
3475 continue;
3476
3477 /* Skip this overlay if it doesn't apply to IT->w. */
3478 window = Foverlay_get (overlay, Qwindow);
3479 if (WINDOWP (window) && XWINDOW (window) != it->w)
3480 continue;
3481
3482 /* If the text ``under'' the overlay is invisible, it has a zero
3483 dimension, and both before- and after-strings apply. */
3484 invisible = Foverlay_get (overlay, Qinvisible);
3485 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3486
3487 /* If overlay has a non-empty before-string, record it. */
3488 if ((start == charpos || (end == charpos && invis_p))
3489 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3490 && XSTRING (str)->size)
3491 RECORD_OVERLAY_STRING (overlay, str, 0);
3492
3493 /* If overlay has a non-empty after-string, record it. */
3494 if ((end == charpos || (start == charpos && invis_p))
3495 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3496 && XSTRING (str)->size)
3497 RECORD_OVERLAY_STRING (overlay, str, 1);
3498 }
3499
3500 #undef RECORD_OVERLAY_STRING
3501
3502 /* Sort entries. */
3503 if (n > 1)
3504 qsort (entries, n, sizeof *entries, compare_overlay_entries);
3505
3506 /* Record the total number of strings to process. */
3507 it->n_overlay_strings = n;
3508
3509 /* IT->current.overlay_string_index is the number of overlay strings
3510 that have already been consumed by IT. Copy some of the
3511 remaining overlay strings to IT->overlay_strings. */
3512 i = 0;
3513 j = it->current.overlay_string_index;
3514 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
3515 it->overlay_strings[i++] = entries[j++].string;
3516
3517 CHECK_IT (it);
3518 }
3519
3520
3521 /* Get the first chunk of overlay strings at IT's current buffer
3522 position. Value is non-zero if at least one overlay string was
3523 found. */
3524
3525 static int
3526 get_overlay_strings (it)
3527 struct it *it;
3528 {
3529 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
3530 process. This fills IT->overlay_strings with strings, and sets
3531 IT->n_overlay_strings to the total number of strings to process.
3532 IT->pos.overlay_string_index has to be set temporarily to zero
3533 because load_overlay_strings needs this; it must be set to -1
3534 when no overlay strings are found because a zero value would
3535 indicate a position in the first overlay string. */
3536 it->current.overlay_string_index = 0;
3537 load_overlay_strings (it);
3538
3539 /* If we found overlay strings, set up IT to deliver display
3540 elements from the first one. Otherwise set up IT to deliver
3541 from current_buffer. */
3542 if (it->n_overlay_strings)
3543 {
3544 /* Make sure we know settings in current_buffer, so that we can
3545 restore meaningful values when we're done with the overlay
3546 strings. */
3547 compute_stop_pos (it);
3548 xassert (it->face_id >= 0);
3549
3550 /* Save IT's settings. They are restored after all overlay
3551 strings have been processed. */
3552 xassert (it->sp == 0);
3553 push_it (it);
3554
3555 /* Set up IT to deliver display elements from the first overlay
3556 string. */
3557 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3558 it->stop_charpos = 0;
3559 it->string = it->overlay_strings[0];
3560 it->multibyte_p = STRING_MULTIBYTE (it->string);
3561 xassert (STRINGP (it->string));
3562 it->method = next_element_from_string;
3563 }
3564 else
3565 {
3566 it->string = Qnil;
3567 it->current.overlay_string_index = -1;
3568 it->method = next_element_from_buffer;
3569 }
3570
3571 CHECK_IT (it);
3572
3573 /* Value is non-zero if we found at least one overlay string. */
3574 return STRINGP (it->string);
3575 }
3576
3577
3578 \f
3579 /***********************************************************************
3580 Saving and restoring state
3581 ***********************************************************************/
3582
3583 /* Save current settings of IT on IT->stack. Called, for example,
3584 before setting up IT for an overlay string, to be able to restore
3585 IT's settings to what they were after the overlay string has been
3586 processed. */
3587
3588 static void
3589 push_it (it)
3590 struct it *it;
3591 {
3592 struct iterator_stack_entry *p;
3593
3594 xassert (it->sp < 2);
3595 p = it->stack + it->sp;
3596
3597 p->stop_charpos = it->stop_charpos;
3598 xassert (it->face_id >= 0);
3599 p->face_id = it->face_id;
3600 p->string = it->string;
3601 p->pos = it->current;
3602 p->end_charpos = it->end_charpos;
3603 p->string_nchars = it->string_nchars;
3604 p->area = it->area;
3605 p->multibyte_p = it->multibyte_p;
3606 p->space_width = it->space_width;
3607 p->font_height = it->font_height;
3608 p->voffset = it->voffset;
3609 p->string_from_display_prop_p = it->string_from_display_prop_p;
3610 ++it->sp;
3611 }
3612
3613
3614 /* Restore IT's settings from IT->stack. Called, for example, when no
3615 more overlay strings must be processed, and we return to delivering
3616 display elements from a buffer, or when the end of a string from a
3617 `display' property is reached and we return to delivering display
3618 elements from an overlay string, or from a buffer. */
3619
3620 static void
3621 pop_it (it)
3622 struct it *it;
3623 {
3624 struct iterator_stack_entry *p;
3625
3626 xassert (it->sp > 0);
3627 --it->sp;
3628 p = it->stack + it->sp;
3629 it->stop_charpos = p->stop_charpos;
3630 it->face_id = p->face_id;
3631 it->string = p->string;
3632 it->current = p->pos;
3633 it->end_charpos = p->end_charpos;
3634 it->string_nchars = p->string_nchars;
3635 it->area = p->area;
3636 it->multibyte_p = p->multibyte_p;
3637 it->space_width = p->space_width;
3638 it->font_height = p->font_height;
3639 it->voffset = p->voffset;
3640 it->string_from_display_prop_p = p->string_from_display_prop_p;
3641 }
3642
3643
3644 \f
3645 /***********************************************************************
3646 Moving over lines
3647 ***********************************************************************/
3648
3649 /* Set IT's current position to the previous line start. */
3650
3651 static void
3652 back_to_previous_line_start (it)
3653 struct it *it;
3654 {
3655 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
3656 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3657 }
3658
3659
3660 /* Move IT to the next line start.
3661
3662 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
3663 we skipped over part of the text (as opposed to moving the iterator
3664 continuously over the text). Otherwise, don't change the value
3665 of *SKIPPED_P.
3666
3667 Newlines may come from buffer text, overlay strings, or strings
3668 displayed via the `display' property. That's the reason we can't
3669 simply use find_next_newline_no_quit.
3670
3671 Note that this function may not skip over invisible text that is so
3672 because of text properties and immediately follows a newline. If
3673 it would, function reseat_at_next_visible_line_start, when called
3674 from set_iterator_to_next, would effectively make invisible
3675 characters following a newline part of the wrong glyph row, which
3676 leads to wrong cursor motion. */
3677
3678 static int
3679 forward_to_next_line_start (it, skipped_p)
3680 struct it *it;
3681 int *skipped_p;
3682 {
3683 int old_selective, newline_found_p, n;
3684 const int MAX_NEWLINE_DISTANCE = 500;
3685
3686 /* If already on a newline, just consume it to avoid unintended
3687 skipping over invisible text below. */
3688 if (it->what == IT_CHARACTER
3689 && it->c == '\n'
3690 && CHARPOS (it->position) == IT_CHARPOS (*it))
3691 {
3692 set_iterator_to_next (it, 0);
3693 it->c = 0;
3694 return 1;
3695 }
3696
3697 /* Don't handle selective display in the following. It's (a)
3698 unnecessary because it's done by the caller, and (b) leads to an
3699 infinite recursion because next_element_from_ellipsis indirectly
3700 calls this function. */
3701 old_selective = it->selective;
3702 it->selective = 0;
3703
3704 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
3705 from buffer text. */
3706 for (n = newline_found_p = 0;
3707 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
3708 n += STRINGP (it->string) ? 0 : 1)
3709 {
3710 if (!get_next_display_element (it))
3711 break;
3712 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
3713 set_iterator_to_next (it, 0);
3714 }
3715
3716 /* If we didn't find a newline near enough, see if we can use a
3717 short-cut. */
3718 if (n == MAX_NEWLINE_DISTANCE)
3719 {
3720 int start = IT_CHARPOS (*it);
3721 int limit = find_next_newline_no_quit (start, 1);
3722 Lisp_Object pos;
3723
3724 xassert (!STRINGP (it->string));
3725
3726 /* If there isn't any `display' property in sight, and no
3727 overlays, we can just use the position of the newline in
3728 buffer text. */
3729 if (it->stop_charpos >= limit
3730 || ((pos = Fnext_single_property_change (make_number (start),
3731 Qdisplay,
3732 Qnil, make_number (limit)),
3733 NILP (pos))
3734 && next_overlay_change (start) == ZV))
3735 {
3736 IT_CHARPOS (*it) = limit;
3737 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
3738 *skipped_p = newline_found_p = 1;
3739 }
3740 else
3741 {
3742 while (get_next_display_element (it)
3743 && !newline_found_p)
3744 {
3745 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
3746 set_iterator_to_next (it, 0);
3747 }
3748 }
3749 }
3750
3751 it->selective = old_selective;
3752 return newline_found_p;
3753 }
3754
3755
3756 /* Set IT's current position to the previous visible line start. Skip
3757 invisible text that is so either due to text properties or due to
3758 selective display. Caution: this does not change IT->current_x and
3759 IT->hpos. */
3760
3761 static void
3762 back_to_previous_visible_line_start (it)
3763 struct it *it;
3764 {
3765 int visible_p = 0;
3766
3767 /* Go back one newline if not on BEGV already. */
3768 if (IT_CHARPOS (*it) > BEGV)
3769 back_to_previous_line_start (it);
3770
3771 /* Move over lines that are invisible because of selective display
3772 or text properties. */
3773 while (IT_CHARPOS (*it) > BEGV
3774 && !visible_p)
3775 {
3776 visible_p = 1;
3777
3778 /* If selective > 0, then lines indented more than that values
3779 are invisible. */
3780 if (it->selective > 0
3781 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3782 it->selective))
3783 visible_p = 0;
3784 else
3785 {
3786 Lisp_Object prop;
3787
3788 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
3789 Qinvisible, it->window);
3790 if (TEXT_PROP_MEANS_INVISIBLE (prop))
3791 visible_p = 0;
3792 }
3793
3794 /* Back one more newline if the current one is invisible. */
3795 if (!visible_p)
3796 back_to_previous_line_start (it);
3797 }
3798
3799 xassert (IT_CHARPOS (*it) >= BEGV);
3800 xassert (IT_CHARPOS (*it) == BEGV
3801 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3802 CHECK_IT (it);
3803 }
3804
3805
3806 /* Reseat iterator IT at the previous visible line start. Skip
3807 invisible text that is so either due to text properties or due to
3808 selective display. At the end, update IT's overlay information,
3809 face information etc. */
3810
3811 static void
3812 reseat_at_previous_visible_line_start (it)
3813 struct it *it;
3814 {
3815 back_to_previous_visible_line_start (it);
3816 reseat (it, it->current.pos, 1);
3817 CHECK_IT (it);
3818 }
3819
3820
3821 /* Reseat iterator IT on the next visible line start in the current
3822 buffer. ON_NEWLINE_P non-zero means position IT on the newline
3823 preceding the line start. Skip over invisible text that is so
3824 because of selective display. Compute faces, overlays etc at the
3825 new position. Note that this function does not skip over text that
3826 is invisible because of text properties. */
3827
3828 static void
3829 reseat_at_next_visible_line_start (it, on_newline_p)
3830 struct it *it;
3831 int on_newline_p;
3832 {
3833 int newline_found_p, skipped_p = 0;
3834
3835 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3836
3837 /* Skip over lines that are invisible because they are indented
3838 more than the value of IT->selective. */
3839 if (it->selective > 0)
3840 while (IT_CHARPOS (*it) < ZV
3841 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3842 it->selective))
3843 {
3844 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3845 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3846 }
3847
3848 /* Position on the newline if that's what's requested. */
3849 if (on_newline_p && newline_found_p)
3850 {
3851 if (STRINGP (it->string))
3852 {
3853 if (IT_STRING_CHARPOS (*it) > 0)
3854 {
3855 --IT_STRING_CHARPOS (*it);
3856 --IT_STRING_BYTEPOS (*it);
3857 }
3858 }
3859 else if (IT_CHARPOS (*it) > BEGV)
3860 {
3861 --IT_CHARPOS (*it);
3862 --IT_BYTEPOS (*it);
3863 reseat (it, it->current.pos, 0);
3864 }
3865 }
3866 else if (skipped_p)
3867 reseat (it, it->current.pos, 0);
3868
3869 CHECK_IT (it);
3870 }
3871
3872
3873 \f
3874 /***********************************************************************
3875 Changing an iterator's position
3876 ***********************************************************************/
3877
3878 /* Change IT's current position to POS in current_buffer. If FORCE_P
3879 is non-zero, always check for text properties at the new position.
3880 Otherwise, text properties are only looked up if POS >=
3881 IT->check_charpos of a property. */
3882
3883 static void
3884 reseat (it, pos, force_p)
3885 struct it *it;
3886 struct text_pos pos;
3887 int force_p;
3888 {
3889 int original_pos = IT_CHARPOS (*it);
3890
3891 reseat_1 (it, pos, 0);
3892
3893 /* Determine where to check text properties. Avoid doing it
3894 where possible because text property lookup is very expensive. */
3895 if (force_p
3896 || CHARPOS (pos) > it->stop_charpos
3897 || CHARPOS (pos) < original_pos)
3898 handle_stop (it);
3899
3900 CHECK_IT (it);
3901 }
3902
3903
3904 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
3905 IT->stop_pos to POS, also. */
3906
3907 static void
3908 reseat_1 (it, pos, set_stop_p)
3909 struct it *it;
3910 struct text_pos pos;
3911 int set_stop_p;
3912 {
3913 /* Don't call this function when scanning a C string. */
3914 xassert (it->s == NULL);
3915
3916 /* POS must be a reasonable value. */
3917 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
3918
3919 it->current.pos = it->position = pos;
3920 XSETBUFFER (it->object, current_buffer);
3921 it->end_charpos = ZV;
3922 it->dpvec = NULL;
3923 it->current.dpvec_index = -1;
3924 it->current.overlay_string_index = -1;
3925 IT_STRING_CHARPOS (*it) = -1;
3926 IT_STRING_BYTEPOS (*it) = -1;
3927 it->string = Qnil;
3928 it->method = next_element_from_buffer;
3929 it->sp = 0;
3930 it->face_before_selective_p = 0;
3931
3932 if (set_stop_p)
3933 it->stop_charpos = CHARPOS (pos);
3934 }
3935
3936
3937 /* Set up IT for displaying a string, starting at CHARPOS in window W.
3938 If S is non-null, it is a C string to iterate over. Otherwise,
3939 STRING gives a Lisp string to iterate over.
3940
3941 If PRECISION > 0, don't return more then PRECISION number of
3942 characters from the string.
3943
3944 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
3945 characters have been returned. FIELD_WIDTH < 0 means an infinite
3946 field width.
3947
3948 MULTIBYTE = 0 means disable processing of multibyte characters,
3949 MULTIBYTE > 0 means enable it,
3950 MULTIBYTE < 0 means use IT->multibyte_p.
3951
3952 IT must be initialized via a prior call to init_iterator before
3953 calling this function. */
3954
3955 static void
3956 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
3957 struct it *it;
3958 unsigned char *s;
3959 Lisp_Object string;
3960 int charpos;
3961 int precision, field_width, multibyte;
3962 {
3963 /* No region in strings. */
3964 it->region_beg_charpos = it->region_end_charpos = -1;
3965
3966 /* No text property checks performed by default, but see below. */
3967 it->stop_charpos = -1;
3968
3969 /* Set iterator position and end position. */
3970 bzero (&it->current, sizeof it->current);
3971 it->current.overlay_string_index = -1;
3972 it->current.dpvec_index = -1;
3973 xassert (charpos >= 0);
3974
3975 /* Use the setting of MULTIBYTE if specified. */
3976 if (multibyte >= 0)
3977 it->multibyte_p = multibyte > 0;
3978
3979 if (s == NULL)
3980 {
3981 xassert (STRINGP (string));
3982 it->string = string;
3983 it->s = NULL;
3984 it->end_charpos = it->string_nchars = XSTRING (string)->size;
3985 it->method = next_element_from_string;
3986 it->current.string_pos = string_pos (charpos, string);
3987 }
3988 else
3989 {
3990 it->s = s;
3991 it->string = Qnil;
3992
3993 /* Note that we use IT->current.pos, not it->current.string_pos,
3994 for displaying C strings. */
3995 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
3996 if (it->multibyte_p)
3997 {
3998 it->current.pos = c_string_pos (charpos, s, 1);
3999 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4000 }
4001 else
4002 {
4003 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4004 it->end_charpos = it->string_nchars = strlen (s);
4005 }
4006
4007 it->method = next_element_from_c_string;
4008 }
4009
4010 /* PRECISION > 0 means don't return more than PRECISION characters
4011 from the string. */
4012 if (precision > 0 && it->end_charpos - charpos > precision)
4013 it->end_charpos = it->string_nchars = charpos + precision;
4014
4015 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4016 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4017 FIELD_WIDTH < 0 means infinite field width. This is useful for
4018 padding with `-' at the end of a mode line. */
4019 if (field_width < 0)
4020 field_width = INFINITY;
4021 if (field_width > it->end_charpos - charpos)
4022 it->end_charpos = charpos + field_width;
4023
4024 /* Use the standard display table for displaying strings. */
4025 if (DISP_TABLE_P (Vstandard_display_table))
4026 it->dp = XCHAR_TABLE (Vstandard_display_table);
4027
4028 it->stop_charpos = charpos;
4029 CHECK_IT (it);
4030 }
4031
4032
4033 \f
4034 /***********************************************************************
4035 Iteration
4036 ***********************************************************************/
4037
4038 /* Load IT's display element fields with information about the next
4039 display element from the current position of IT. Value is zero if
4040 end of buffer (or C string) is reached. */
4041
4042 int
4043 get_next_display_element (it)
4044 struct it *it;
4045 {
4046 /* Non-zero means that we found an display element. Zero means that
4047 we hit the end of what we iterate over. Performance note: the
4048 function pointer `method' used here turns out to be faster than
4049 using a sequence of if-statements. */
4050 int success_p = (*it->method) (it);
4051
4052 if (it->what == IT_CHARACTER)
4053 {
4054 /* Map via display table or translate control characters.
4055 IT->c, IT->len etc. have been set to the next character by
4056 the function call above. If we have a display table, and it
4057 contains an entry for IT->c, translate it. Don't do this if
4058 IT->c itself comes from a display table, otherwise we could
4059 end up in an infinite recursion. (An alternative could be to
4060 count the recursion depth of this function and signal an
4061 error when a certain maximum depth is reached.) Is it worth
4062 it? */
4063 if (success_p && it->dpvec == NULL)
4064 {
4065 Lisp_Object dv;
4066
4067 if (it->dp
4068 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
4069 VECTORP (dv)))
4070 {
4071 struct Lisp_Vector *v = XVECTOR (dv);
4072
4073 /* Return the first character from the display table
4074 entry, if not empty. If empty, don't display the
4075 current character. */
4076 if (v->size)
4077 {
4078 it->dpvec_char_len = it->len;
4079 it->dpvec = v->contents;
4080 it->dpend = v->contents + v->size;
4081 it->current.dpvec_index = 0;
4082 it->method = next_element_from_display_vector;
4083 success_p = get_next_display_element (it);
4084 }
4085 else
4086 {
4087 set_iterator_to_next (it, 0);
4088 success_p = get_next_display_element (it);
4089 }
4090 }
4091
4092 /* Translate control characters into `\003' or `^C' form.
4093 Control characters coming from a display table entry are
4094 currently not translated because we use IT->dpvec to hold
4095 the translation. This could easily be changed but I
4096 don't believe that it is worth doing.
4097
4098 Non-printable multibyte characters are also translated
4099 octal form. */
4100 else if ((it->c < ' '
4101 && (it->area != TEXT_AREA
4102 || (it->c != '\n' && it->c != '\t')))
4103 || (it->c >= 127
4104 && it->len == 1)
4105 || !CHAR_PRINTABLE_P (it->c))
4106 {
4107 /* IT->c is a control character which must be displayed
4108 either as '\003' or as `^C' where the '\\' and '^'
4109 can be defined in the display table. Fill
4110 IT->ctl_chars with glyphs for what we have to
4111 display. Then, set IT->dpvec to these glyphs. */
4112 GLYPH g;
4113
4114 if (it->c < 128 && it->ctl_arrow_p)
4115 {
4116 /* Set IT->ctl_chars[0] to the glyph for `^'. */
4117 if (it->dp
4118 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
4119 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
4120 g = XINT (DISP_CTRL_GLYPH (it->dp));
4121 else
4122 g = FAST_MAKE_GLYPH ('^', 0);
4123 XSETINT (it->ctl_chars[0], g);
4124
4125 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
4126 XSETINT (it->ctl_chars[1], g);
4127
4128 /* Set up IT->dpvec and return first character from it. */
4129 it->dpvec_char_len = it->len;
4130 it->dpvec = it->ctl_chars;
4131 it->dpend = it->dpvec + 2;
4132 it->current.dpvec_index = 0;
4133 it->method = next_element_from_display_vector;
4134 get_next_display_element (it);
4135 }
4136 else
4137 {
4138 unsigned char str[MAX_MULTIBYTE_LENGTH];
4139 int len;
4140 int i;
4141 GLYPH escape_glyph;
4142
4143 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
4144 if (it->dp
4145 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
4146 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
4147 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
4148 else
4149 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
4150
4151 if (SINGLE_BYTE_CHAR_P (it->c))
4152 str[0] = it->c, len = 1;
4153 else
4154 len = CHAR_STRING (it->c, str);
4155
4156 for (i = 0; i < len; i++)
4157 {
4158 XSETINT (it->ctl_chars[i * 4], escape_glyph);
4159 /* Insert three more glyphs into IT->ctl_chars for
4160 the octal display of the character. */
4161 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
4162 XSETINT (it->ctl_chars[i * 4 + 1], g);
4163 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
4164 XSETINT (it->ctl_chars[i * 4 + 2], g);
4165 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
4166 XSETINT (it->ctl_chars[i * 4 + 3], g);
4167 }
4168
4169 /* Set up IT->dpvec and return the first character
4170 from it. */
4171 it->dpvec_char_len = it->len;
4172 it->dpvec = it->ctl_chars;
4173 it->dpend = it->dpvec + len * 4;
4174 it->current.dpvec_index = 0;
4175 it->method = next_element_from_display_vector;
4176 get_next_display_element (it);
4177 }
4178 }
4179 }
4180
4181 /* Adjust face id for a multibyte character. There are no
4182 multibyte character in unibyte text. */
4183 if (it->multibyte_p
4184 && success_p
4185 && FRAME_WINDOW_P (it->f))
4186 {
4187 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4188 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
4189 }
4190 }
4191
4192 /* Is this character the last one of a run of characters with
4193 box? If yes, set IT->end_of_box_run_p to 1. */
4194 if (it->face_box_p
4195 && it->s == NULL)
4196 {
4197 int face_id;
4198 struct face *face;
4199
4200 it->end_of_box_run_p
4201 = ((face_id = face_after_it_pos (it),
4202 face_id != it->face_id)
4203 && (face = FACE_FROM_ID (it->f, face_id),
4204 face->box == FACE_NO_BOX));
4205 }
4206
4207 /* Value is 0 if end of buffer or string reached. */
4208 return success_p;
4209 }
4210
4211
4212 /* Move IT to the next display element.
4213
4214 RESEAT_P non-zero means if called on a newline in buffer text,
4215 skip to the next visible line start.
4216
4217 Functions get_next_display_element and set_iterator_to_next are
4218 separate because I find this arrangement easier to handle than a
4219 get_next_display_element function that also increments IT's
4220 position. The way it is we can first look at an iterator's current
4221 display element, decide whether it fits on a line, and if it does,
4222 increment the iterator position. The other way around we probably
4223 would either need a flag indicating whether the iterator has to be
4224 incremented the next time, or we would have to implement a
4225 decrement position function which would not be easy to write. */
4226
4227 void
4228 set_iterator_to_next (it, reseat_p)
4229 struct it *it;
4230 int reseat_p;
4231 {
4232 /* Reset flags indicating start and end of a sequence of characters
4233 with box. Reset them at the start of this function because
4234 moving the iterator to a new position might set them. */
4235 it->start_of_box_run_p = it->end_of_box_run_p = 0;
4236
4237 if (it->method == next_element_from_buffer)
4238 {
4239 /* The current display element of IT is a character from
4240 current_buffer. Advance in the buffer, and maybe skip over
4241 invisible lines that are so because of selective display. */
4242 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
4243 reseat_at_next_visible_line_start (it, 0);
4244 else
4245 {
4246 xassert (it->len != 0);
4247 IT_BYTEPOS (*it) += it->len;
4248 IT_CHARPOS (*it) += 1;
4249 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
4250 }
4251 }
4252 else if (it->method == next_element_from_composition)
4253 {
4254 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
4255 if (STRINGP (it->string))
4256 {
4257 IT_STRING_BYTEPOS (*it) += it->len;
4258 IT_STRING_CHARPOS (*it) += it->cmp_len;
4259 it->method = next_element_from_string;
4260 goto consider_string_end;
4261 }
4262 else
4263 {
4264 IT_BYTEPOS (*it) += it->len;
4265 IT_CHARPOS (*it) += it->cmp_len;
4266 it->method = next_element_from_buffer;
4267 }
4268 }
4269 else if (it->method == next_element_from_c_string)
4270 {
4271 /* Current display element of IT is from a C string. */
4272 IT_BYTEPOS (*it) += it->len;
4273 IT_CHARPOS (*it) += 1;
4274 }
4275 else if (it->method == next_element_from_display_vector)
4276 {
4277 /* Current display element of IT is from a display table entry.
4278 Advance in the display table definition. Reset it to null if
4279 end reached, and continue with characters from buffers/
4280 strings. */
4281 ++it->current.dpvec_index;
4282
4283 /* Restore face of the iterator to what they were before the
4284 display vector entry (these entries may contain faces). */
4285 it->face_id = it->saved_face_id;
4286
4287 if (it->dpvec + it->current.dpvec_index == it->dpend)
4288 {
4289 if (it->s)
4290 it->method = next_element_from_c_string;
4291 else if (STRINGP (it->string))
4292 it->method = next_element_from_string;
4293 else
4294 it->method = next_element_from_buffer;
4295
4296 it->dpvec = NULL;
4297 it->current.dpvec_index = -1;
4298
4299 /* Skip over characters which were displayed via IT->dpvec. */
4300 if (it->dpvec_char_len < 0)
4301 reseat_at_next_visible_line_start (it, 1);
4302 else if (it->dpvec_char_len > 0)
4303 {
4304 it->len = it->dpvec_char_len;
4305 set_iterator_to_next (it, reseat_p);
4306 }
4307 }
4308 }
4309 else if (it->method == next_element_from_string)
4310 {
4311 /* Current display element is a character from a Lisp string. */
4312 xassert (it->s == NULL && STRINGP (it->string));
4313 IT_STRING_BYTEPOS (*it) += it->len;
4314 IT_STRING_CHARPOS (*it) += 1;
4315
4316 consider_string_end:
4317
4318 if (it->current.overlay_string_index >= 0)
4319 {
4320 /* IT->string is an overlay string. Advance to the
4321 next, if there is one. */
4322 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
4323 next_overlay_string (it);
4324 }
4325 else
4326 {
4327 /* IT->string is not an overlay string. If we reached
4328 its end, and there is something on IT->stack, proceed
4329 with what is on the stack. This can be either another
4330 string, this time an overlay string, or a buffer. */
4331 if (IT_STRING_CHARPOS (*it) == XSTRING (it->string)->size
4332 && it->sp > 0)
4333 {
4334 pop_it (it);
4335 if (!STRINGP (it->string))
4336 it->method = next_element_from_buffer;
4337 }
4338 }
4339 }
4340 else if (it->method == next_element_from_image
4341 || it->method == next_element_from_stretch)
4342 {
4343 /* The position etc with which we have to proceed are on
4344 the stack. The position may be at the end of a string,
4345 if the `display' property takes up the whole string. */
4346 pop_it (it);
4347 it->image_id = 0;
4348 if (STRINGP (it->string))
4349 {
4350 it->method = next_element_from_string;
4351 goto consider_string_end;
4352 }
4353 else
4354 it->method = next_element_from_buffer;
4355 }
4356 else
4357 /* There are no other methods defined, so this should be a bug. */
4358 abort ();
4359
4360 xassert (it->method != next_element_from_string
4361 || (STRINGP (it->string)
4362 && IT_STRING_CHARPOS (*it) >= 0));
4363 }
4364
4365
4366 /* Load IT's display element fields with information about the next
4367 display element which comes from a display table entry or from the
4368 result of translating a control character to one of the forms `^C'
4369 or `\003'. IT->dpvec holds the glyphs to return as characters. */
4370
4371 static int
4372 next_element_from_display_vector (it)
4373 struct it *it;
4374 {
4375 /* Precondition. */
4376 xassert (it->dpvec && it->current.dpvec_index >= 0);
4377
4378 /* Remember the current face id in case glyphs specify faces.
4379 IT's face is restored in set_iterator_to_next. */
4380 it->saved_face_id = it->face_id;
4381
4382 if (INTEGERP (*it->dpvec)
4383 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
4384 {
4385 int lface_id;
4386 GLYPH g;
4387
4388 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
4389 it->c = FAST_GLYPH_CHAR (g);
4390 it->len = CHAR_BYTES (it->c);
4391
4392 /* The entry may contain a face id to use. Such a face id is
4393 the id of a Lisp face, not a realized face. A face id of
4394 zero means no face is specified. */
4395 lface_id = FAST_GLYPH_FACE (g);
4396 if (lface_id)
4397 {
4398 /* The function returns -1 if lface_id is invalid. */
4399 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
4400 if (face_id >= 0)
4401 it->face_id = face_id;
4402 }
4403 }
4404 else
4405 /* Display table entry is invalid. Return a space. */
4406 it->c = ' ', it->len = 1;
4407
4408 /* Don't change position and object of the iterator here. They are
4409 still the values of the character that had this display table
4410 entry or was translated, and that's what we want. */
4411 it->what = IT_CHARACTER;
4412 return 1;
4413 }
4414
4415
4416 /* Load IT with the next display element from Lisp string IT->string.
4417 IT->current.string_pos is the current position within the string.
4418 If IT->current.overlay_string_index >= 0, the Lisp string is an
4419 overlay string. */
4420
4421 static int
4422 next_element_from_string (it)
4423 struct it *it;
4424 {
4425 struct text_pos position;
4426
4427 xassert (STRINGP (it->string));
4428 xassert (IT_STRING_CHARPOS (*it) >= 0);
4429 position = it->current.string_pos;
4430
4431 /* Time to check for invisible text? */
4432 if (IT_STRING_CHARPOS (*it) < it->end_charpos
4433 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
4434 {
4435 handle_stop (it);
4436
4437 /* Since a handler may have changed IT->method, we must
4438 recurse here. */
4439 return get_next_display_element (it);
4440 }
4441
4442 if (it->current.overlay_string_index >= 0)
4443 {
4444 /* Get the next character from an overlay string. In overlay
4445 strings, There is no field width or padding with spaces to
4446 do. */
4447 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
4448 {
4449 it->what = IT_EOB;
4450 return 0;
4451 }
4452 else if (STRING_MULTIBYTE (it->string))
4453 {
4454 int remaining = (STRING_BYTES (XSTRING (it->string))
4455 - IT_STRING_BYTEPOS (*it));
4456 unsigned char *s = (XSTRING (it->string)->data
4457 + IT_STRING_BYTEPOS (*it));
4458 it->c = string_char_and_length (s, remaining, &it->len);
4459 }
4460 else
4461 {
4462 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
4463 it->len = 1;
4464 }
4465 }
4466 else
4467 {
4468 /* Get the next character from a Lisp string that is not an
4469 overlay string. Such strings come from the mode line, for
4470 example. We may have to pad with spaces, or truncate the
4471 string. See also next_element_from_c_string. */
4472 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
4473 {
4474 it->what = IT_EOB;
4475 return 0;
4476 }
4477 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
4478 {
4479 /* Pad with spaces. */
4480 it->c = ' ', it->len = 1;
4481 CHARPOS (position) = BYTEPOS (position) = -1;
4482 }
4483 else if (STRING_MULTIBYTE (it->string))
4484 {
4485 int maxlen = (STRING_BYTES (XSTRING (it->string))
4486 - IT_STRING_BYTEPOS (*it));
4487 unsigned char *s = (XSTRING (it->string)->data
4488 + IT_STRING_BYTEPOS (*it));
4489 it->c = string_char_and_length (s, maxlen, &it->len);
4490 }
4491 else
4492 {
4493 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
4494 it->len = 1;
4495 }
4496 }
4497
4498 /* Record what we have and where it came from. Note that we store a
4499 buffer position in IT->position although it could arguably be a
4500 string position. */
4501 it->what = IT_CHARACTER;
4502 it->object = it->string;
4503 it->position = position;
4504 return 1;
4505 }
4506
4507
4508 /* Load IT with next display element from C string IT->s.
4509 IT->string_nchars is the maximum number of characters to return
4510 from the string. IT->end_charpos may be greater than
4511 IT->string_nchars when this function is called, in which case we
4512 may have to return padding spaces. Value is zero if end of string
4513 reached, including padding spaces. */
4514
4515 static int
4516 next_element_from_c_string (it)
4517 struct it *it;
4518 {
4519 int success_p = 1;
4520
4521 xassert (it->s);
4522 it->what = IT_CHARACTER;
4523 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
4524 it->object = Qnil;
4525
4526 /* IT's position can be greater IT->string_nchars in case a field
4527 width or precision has been specified when the iterator was
4528 initialized. */
4529 if (IT_CHARPOS (*it) >= it->end_charpos)
4530 {
4531 /* End of the game. */
4532 it->what = IT_EOB;
4533 success_p = 0;
4534 }
4535 else if (IT_CHARPOS (*it) >= it->string_nchars)
4536 {
4537 /* Pad with spaces. */
4538 it->c = ' ', it->len = 1;
4539 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
4540 }
4541 else if (it->multibyte_p)
4542 {
4543 /* Implementation note: The calls to strlen apparently aren't a
4544 performance problem because there is no noticeable performance
4545 difference between Emacs running in unibyte or multibyte mode. */
4546 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
4547 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
4548 maxlen, &it->len);
4549 }
4550 else
4551 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
4552
4553 return success_p;
4554 }
4555
4556
4557 /* Set up IT to return characters from an ellipsis, if appropriate.
4558 The definition of the ellipsis glyphs may come from a display table
4559 entry. This function Fills IT with the first glyph from the
4560 ellipsis if an ellipsis is to be displayed. */
4561
4562 static int
4563 next_element_from_ellipsis (it)
4564 struct it *it;
4565 {
4566 if (it->selective_display_ellipsis_p)
4567 {
4568 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4569 {
4570 /* Use the display table definition for `...'. Invalid glyphs
4571 will be handled by the method returning elements from dpvec. */
4572 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4573 it->dpvec_char_len = it->len;
4574 it->dpvec = v->contents;
4575 it->dpend = v->contents + v->size;
4576 it->current.dpvec_index = 0;
4577 it->method = next_element_from_display_vector;
4578 }
4579 else
4580 {
4581 /* Use default `...' which is stored in default_invis_vector. */
4582 it->dpvec_char_len = it->len;
4583 it->dpvec = default_invis_vector;
4584 it->dpend = default_invis_vector + 3;
4585 it->current.dpvec_index = 0;
4586 it->method = next_element_from_display_vector;
4587 }
4588 }
4589 else
4590 {
4591 /* The face at the current position may be different from the
4592 face we find after the invisible text. Remember what it
4593 was in IT->saved_face_id, and signal that it's there by
4594 setting face_before_selective_p. */
4595 it->saved_face_id = it->face_id;
4596 it->method = next_element_from_buffer;
4597 reseat_at_next_visible_line_start (it, 1);
4598 it->face_before_selective_p = 1;
4599 }
4600
4601 return get_next_display_element (it);
4602 }
4603
4604
4605 /* Deliver an image display element. The iterator IT is already
4606 filled with image information (done in handle_display_prop). Value
4607 is always 1. */
4608
4609
4610 static int
4611 next_element_from_image (it)
4612 struct it *it;
4613 {
4614 it->what = IT_IMAGE;
4615 return 1;
4616 }
4617
4618
4619 /* Fill iterator IT with next display element from a stretch glyph
4620 property. IT->object is the value of the text property. Value is
4621 always 1. */
4622
4623 static int
4624 next_element_from_stretch (it)
4625 struct it *it;
4626 {
4627 it->what = IT_STRETCH;
4628 return 1;
4629 }
4630
4631
4632 /* Load IT with the next display element from current_buffer. Value
4633 is zero if end of buffer reached. IT->stop_charpos is the next
4634 position at which to stop and check for text properties or buffer
4635 end. */
4636
4637 static int
4638 next_element_from_buffer (it)
4639 struct it *it;
4640 {
4641 int success_p = 1;
4642
4643 /* Check this assumption, otherwise, we would never enter the
4644 if-statement, below. */
4645 xassert (IT_CHARPOS (*it) >= BEGV
4646 && IT_CHARPOS (*it) <= it->stop_charpos);
4647
4648 if (IT_CHARPOS (*it) >= it->stop_charpos)
4649 {
4650 if (IT_CHARPOS (*it) >= it->end_charpos)
4651 {
4652 int overlay_strings_follow_p;
4653
4654 /* End of the game, except when overlay strings follow that
4655 haven't been returned yet. */
4656 if (it->overlay_strings_at_end_processed_p)
4657 overlay_strings_follow_p = 0;
4658 else
4659 {
4660 it->overlay_strings_at_end_processed_p = 1;
4661 overlay_strings_follow_p = get_overlay_strings (it);
4662 }
4663
4664 if (overlay_strings_follow_p)
4665 success_p = get_next_display_element (it);
4666 else
4667 {
4668 it->what = IT_EOB;
4669 it->position = it->current.pos;
4670 success_p = 0;
4671 }
4672 }
4673 else
4674 {
4675 handle_stop (it);
4676 return get_next_display_element (it);
4677 }
4678 }
4679 else
4680 {
4681 /* No face changes, overlays etc. in sight, so just return a
4682 character from current_buffer. */
4683 unsigned char *p;
4684
4685 /* Maybe run the redisplay end trigger hook. Performance note:
4686 This doesn't seem to cost measurable time. */
4687 if (it->redisplay_end_trigger_charpos
4688 && it->glyph_row
4689 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
4690 run_redisplay_end_trigger_hook (it);
4691
4692 /* Get the next character, maybe multibyte. */
4693 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
4694 if (it->multibyte_p && !ASCII_BYTE_P (*p))
4695 {
4696 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
4697 - IT_BYTEPOS (*it));
4698 it->c = string_char_and_length (p, maxlen, &it->len);
4699 }
4700 else
4701 it->c = *p, it->len = 1;
4702
4703 /* Record what we have and where it came from. */
4704 it->what = IT_CHARACTER;;
4705 it->object = it->w->buffer;
4706 it->position = it->current.pos;
4707
4708 /* Normally we return the character found above, except when we
4709 really want to return an ellipsis for selective display. */
4710 if (it->selective)
4711 {
4712 if (it->c == '\n')
4713 {
4714 /* A value of selective > 0 means hide lines indented more
4715 than that number of columns. */
4716 if (it->selective > 0
4717 && IT_CHARPOS (*it) + 1 < ZV
4718 && indented_beyond_p (IT_CHARPOS (*it) + 1,
4719 IT_BYTEPOS (*it) + 1,
4720 it->selective))
4721 {
4722 success_p = next_element_from_ellipsis (it);
4723 it->dpvec_char_len = -1;
4724 }
4725 }
4726 else if (it->c == '\r' && it->selective == -1)
4727 {
4728 /* A value of selective == -1 means that everything from the
4729 CR to the end of the line is invisible, with maybe an
4730 ellipsis displayed for it. */
4731 success_p = next_element_from_ellipsis (it);
4732 it->dpvec_char_len = -1;
4733 }
4734 }
4735 }
4736
4737 /* Value is zero if end of buffer reached. */
4738 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
4739 return success_p;
4740 }
4741
4742
4743 /* Run the redisplay end trigger hook for IT. */
4744
4745 static void
4746 run_redisplay_end_trigger_hook (it)
4747 struct it *it;
4748 {
4749 Lisp_Object args[3];
4750
4751 /* IT->glyph_row should be non-null, i.e. we should be actually
4752 displaying something, or otherwise we should not run the hook. */
4753 xassert (it->glyph_row);
4754
4755 /* Set up hook arguments. */
4756 args[0] = Qredisplay_end_trigger_functions;
4757 args[1] = it->window;
4758 XSETINT (args[2], it->redisplay_end_trigger_charpos);
4759 it->redisplay_end_trigger_charpos = 0;
4760
4761 /* Since we are *trying* to run these functions, don't try to run
4762 them again, even if they get an error. */
4763 it->w->redisplay_end_trigger = Qnil;
4764 Frun_hook_with_args (3, args);
4765
4766 /* Notice if it changed the face of the character we are on. */
4767 handle_face_prop (it);
4768 }
4769
4770
4771 /* Deliver a composition display element. The iterator IT is already
4772 filled with composition information (done in
4773 handle_composition_prop). Value is always 1. */
4774
4775 static int
4776 next_element_from_composition (it)
4777 struct it *it;
4778 {
4779 it->what = IT_COMPOSITION;
4780 it->position = (STRINGP (it->string)
4781 ? it->current.string_pos
4782 : it->current.pos);
4783 return 1;
4784 }
4785
4786
4787 \f
4788 /***********************************************************************
4789 Moving an iterator without producing glyphs
4790 ***********************************************************************/
4791
4792 /* Move iterator IT to a specified buffer or X position within one
4793 line on the display without producing glyphs.
4794
4795 Begin to skip at IT's current position. Skip to TO_CHARPOS or TO_X
4796 whichever is reached first.
4797
4798 TO_CHARPOS <= 0 means no TO_CHARPOS is specified.
4799
4800 TO_X < 0 means that no TO_X is specified. TO_X is normally a value
4801 0 <= TO_X <= IT->last_visible_x. This means in particular, that
4802 TO_X includes the amount by which a window is horizontally
4803 scrolled.
4804
4805 Value is
4806
4807 MOVE_POS_MATCH_OR_ZV
4808 - when TO_POS or ZV was reached.
4809
4810 MOVE_X_REACHED
4811 -when TO_X was reached before TO_POS or ZV were reached.
4812
4813 MOVE_LINE_CONTINUED
4814 - when we reached the end of the display area and the line must
4815 be continued.
4816
4817 MOVE_LINE_TRUNCATED
4818 - when we reached the end of the display area and the line is
4819 truncated.
4820
4821 MOVE_NEWLINE_OR_CR
4822 - when we stopped at a line end, i.e. a newline or a CR and selective
4823 display is on. */
4824
4825 static enum move_it_result
4826 move_it_in_display_line_to (it, to_charpos, to_x, op)
4827 struct it *it;
4828 int to_charpos, to_x, op;
4829 {
4830 enum move_it_result result = MOVE_UNDEFINED;
4831 struct glyph_row *saved_glyph_row;
4832
4833 /* Don't produce glyphs in produce_glyphs. */
4834 saved_glyph_row = it->glyph_row;
4835 it->glyph_row = NULL;
4836
4837 while (1)
4838 {
4839 int x, i, ascent = 0, descent = 0;
4840
4841 /* Stop when ZV or TO_CHARPOS reached. */
4842 if (!get_next_display_element (it)
4843 || ((op & MOVE_TO_POS) != 0
4844 && BUFFERP (it->object)
4845 && IT_CHARPOS (*it) >= to_charpos))
4846 {
4847 result = MOVE_POS_MATCH_OR_ZV;
4848 break;
4849 }
4850
4851 /* The call to produce_glyphs will get the metrics of the
4852 display element IT is loaded with. We record in x the
4853 x-position before this display element in case it does not
4854 fit on the line. */
4855 x = it->current_x;
4856
4857 /* Remember the line height so far in case the next element doesn't
4858 fit on the line. */
4859 if (!it->truncate_lines_p)
4860 {
4861 ascent = it->max_ascent;
4862 descent = it->max_descent;
4863 }
4864
4865 PRODUCE_GLYPHS (it);
4866
4867 if (it->area != TEXT_AREA)
4868 {
4869 set_iterator_to_next (it, 1);
4870 continue;
4871 }
4872
4873 /* The number of glyphs we get back in IT->nglyphs will normally
4874 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
4875 character on a terminal frame, or (iii) a line end. For the
4876 second case, IT->nglyphs - 1 padding glyphs will be present
4877 (on X frames, there is only one glyph produced for a
4878 composite character.
4879
4880 The behavior implemented below means, for continuation lines,
4881 that as many spaces of a TAB as fit on the current line are
4882 displayed there. For terminal frames, as many glyphs of a
4883 multi-glyph character are displayed in the current line, too.
4884 This is what the old redisplay code did, and we keep it that
4885 way. Under X, the whole shape of a complex character must
4886 fit on the line or it will be completely displayed in the
4887 next line.
4888
4889 Note that both for tabs and padding glyphs, all glyphs have
4890 the same width. */
4891 if (it->nglyphs)
4892 {
4893 /* More than one glyph or glyph doesn't fit on line. All
4894 glyphs have the same width. */
4895 int single_glyph_width = it->pixel_width / it->nglyphs;
4896 int new_x;
4897
4898 for (i = 0; i < it->nglyphs; ++i, x = new_x)
4899 {
4900 new_x = x + single_glyph_width;
4901
4902 /* We want to leave anything reaching TO_X to the caller. */
4903 if ((op & MOVE_TO_X) && new_x > to_x)
4904 {
4905 it->current_x = x;
4906 result = MOVE_X_REACHED;
4907 break;
4908 }
4909 else if (/* Lines are continued. */
4910 !it->truncate_lines_p
4911 && (/* And glyph doesn't fit on the line. */
4912 new_x > it->last_visible_x
4913 /* Or it fits exactly and we're on a window
4914 system frame. */
4915 || (new_x == it->last_visible_x
4916 && FRAME_WINDOW_P (it->f))))
4917 {
4918 if (/* IT->hpos == 0 means the very first glyph
4919 doesn't fit on the line, e.g. a wide image. */
4920 it->hpos == 0
4921 || (new_x == it->last_visible_x
4922 && FRAME_WINDOW_P (it->f)))
4923 {
4924 ++it->hpos;
4925 it->current_x = new_x;
4926 if (i == it->nglyphs - 1)
4927 set_iterator_to_next (it, 1);
4928 }
4929 else
4930 {
4931 it->current_x = x;
4932 it->max_ascent = ascent;
4933 it->max_descent = descent;
4934 }
4935
4936 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
4937 IT_CHARPOS (*it)));
4938 result = MOVE_LINE_CONTINUED;
4939 break;
4940 }
4941 else if (new_x > it->first_visible_x)
4942 {
4943 /* Glyph is visible. Increment number of glyphs that
4944 would be displayed. */
4945 ++it->hpos;
4946 }
4947 else
4948 {
4949 /* Glyph is completely off the left margin of the display
4950 area. Nothing to do. */
4951 }
4952 }
4953
4954 if (result != MOVE_UNDEFINED)
4955 break;
4956 }
4957 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
4958 {
4959 /* Stop when TO_X specified and reached. This check is
4960 necessary here because of lines consisting of a line end,
4961 only. The line end will not produce any glyphs and we
4962 would never get MOVE_X_REACHED. */
4963 xassert (it->nglyphs == 0);
4964 result = MOVE_X_REACHED;
4965 break;
4966 }
4967
4968 /* Is this a line end? If yes, we're done. */
4969 if (ITERATOR_AT_END_OF_LINE_P (it))
4970 {
4971 result = MOVE_NEWLINE_OR_CR;
4972 break;
4973 }
4974
4975 /* The current display element has been consumed. Advance
4976 to the next. */
4977 set_iterator_to_next (it, 1);
4978
4979 /* Stop if lines are truncated and IT's current x-position is
4980 past the right edge of the window now. */
4981 if (it->truncate_lines_p
4982 && it->current_x >= it->last_visible_x)
4983 {
4984 result = MOVE_LINE_TRUNCATED;
4985 break;
4986 }
4987 }
4988
4989 /* Restore the iterator settings altered at the beginning of this
4990 function. */
4991 it->glyph_row = saved_glyph_row;
4992 return result;
4993 }
4994
4995
4996 /* Move IT forward to a specified buffer position TO_CHARPOS, TO_X,
4997 TO_Y, TO_VPOS. OP is a bit-mask that specifies where to stop. See
4998 the description of enum move_operation_enum.
4999
5000 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
5001 screen line, this function will set IT to the next position >
5002 TO_CHARPOS. */
5003
5004 void
5005 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
5006 struct it *it;
5007 int to_charpos, to_x, to_y, to_vpos;
5008 int op;
5009 {
5010 enum move_it_result skip, skip2 = MOVE_X_REACHED;
5011 int line_height;
5012 int reached = 0;
5013
5014 for (;;)
5015 {
5016 if (op & MOVE_TO_VPOS)
5017 {
5018 /* If no TO_CHARPOS and no TO_X specified, stop at the
5019 start of the line TO_VPOS. */
5020 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
5021 {
5022 if (it->vpos == to_vpos)
5023 {
5024 reached = 1;
5025 break;
5026 }
5027 else
5028 skip = move_it_in_display_line_to (it, -1, -1, 0);
5029 }
5030 else
5031 {
5032 /* TO_VPOS >= 0 means stop at TO_X in the line at
5033 TO_VPOS, or at TO_POS, whichever comes first. */
5034 if (it->vpos == to_vpos)
5035 {
5036 reached = 2;
5037 break;
5038 }
5039
5040 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
5041
5042 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
5043 {
5044 reached = 3;
5045 break;
5046 }
5047 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
5048 {
5049 /* We have reached TO_X but not in the line we want. */
5050 skip = move_it_in_display_line_to (it, to_charpos,
5051 -1, MOVE_TO_POS);
5052 if (skip == MOVE_POS_MATCH_OR_ZV)
5053 {
5054 reached = 4;
5055 break;
5056 }
5057 }
5058 }
5059 }
5060 else if (op & MOVE_TO_Y)
5061 {
5062 struct it it_backup;
5063
5064 /* TO_Y specified means stop at TO_X in the line containing
5065 TO_Y---or at TO_CHARPOS if this is reached first. The
5066 problem is that we can't really tell whether the line
5067 contains TO_Y before we have completely scanned it, and
5068 this may skip past TO_X. What we do is to first scan to
5069 TO_X.
5070
5071 If TO_X is not specified, use a TO_X of zero. The reason
5072 is to make the outcome of this function more predictable.
5073 If we didn't use TO_X == 0, we would stop at the end of
5074 the line which is probably not what a caller would expect
5075 to happen. */
5076 skip = move_it_in_display_line_to (it, to_charpos,
5077 ((op & MOVE_TO_X)
5078 ? to_x : 0),
5079 (MOVE_TO_X
5080 | (op & MOVE_TO_POS)));
5081
5082 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
5083 if (skip == MOVE_POS_MATCH_OR_ZV)
5084 {
5085 reached = 5;
5086 break;
5087 }
5088
5089 /* If TO_X was reached, we would like to know whether TO_Y
5090 is in the line. This can only be said if we know the
5091 total line height which requires us to scan the rest of
5092 the line. */
5093 if (skip == MOVE_X_REACHED)
5094 {
5095 it_backup = *it;
5096 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
5097 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
5098 op & MOVE_TO_POS);
5099 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
5100 }
5101
5102 /* Now, decide whether TO_Y is in this line. */
5103 line_height = it->max_ascent + it->max_descent;
5104 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
5105
5106 if (to_y >= it->current_y
5107 && to_y < it->current_y + line_height)
5108 {
5109 if (skip == MOVE_X_REACHED)
5110 /* If TO_Y is in this line and TO_X was reached above,
5111 we scanned too far. We have to restore IT's settings
5112 to the ones before skipping. */
5113 *it = it_backup;
5114 reached = 6;
5115 }
5116 else if (skip == MOVE_X_REACHED)
5117 {
5118 skip = skip2;
5119 if (skip == MOVE_POS_MATCH_OR_ZV)
5120 reached = 7;
5121 }
5122
5123 if (reached)
5124 break;
5125 }
5126 else
5127 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
5128
5129 switch (skip)
5130 {
5131 case MOVE_POS_MATCH_OR_ZV:
5132 reached = 8;
5133 goto out;
5134
5135 case MOVE_NEWLINE_OR_CR:
5136 set_iterator_to_next (it, 1);
5137 it->continuation_lines_width = 0;
5138 break;
5139
5140 case MOVE_LINE_TRUNCATED:
5141 it->continuation_lines_width = 0;
5142 reseat_at_next_visible_line_start (it, 0);
5143 if ((op & MOVE_TO_POS) != 0
5144 && IT_CHARPOS (*it) > to_charpos)
5145 {
5146 reached = 9;
5147 goto out;
5148 }
5149 break;
5150
5151 case MOVE_LINE_CONTINUED:
5152 it->continuation_lines_width += it->current_x;
5153 break;
5154
5155 default:
5156 abort ();
5157 }
5158
5159 /* Reset/increment for the next run. */
5160 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
5161 it->current_x = it->hpos = 0;
5162 it->current_y += it->max_ascent + it->max_descent;
5163 ++it->vpos;
5164 last_height = it->max_ascent + it->max_descent;
5165 last_max_ascent = it->max_ascent;
5166 it->max_ascent = it->max_descent = 0;
5167 }
5168
5169 out:
5170
5171 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
5172 }
5173
5174
5175 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
5176
5177 If DY > 0, move IT backward at least that many pixels. DY = 0
5178 means move IT backward to the preceding line start or BEGV. This
5179 function may move over more than DY pixels if IT->current_y - DY
5180 ends up in the middle of a line; in this case IT->current_y will be
5181 set to the top of the line moved to. */
5182
5183 void
5184 move_it_vertically_backward (it, dy)
5185 struct it *it;
5186 int dy;
5187 {
5188 int nlines, h, line_height;
5189 struct it it2;
5190 int start_pos = IT_CHARPOS (*it);
5191
5192 xassert (dy >= 0);
5193
5194 /* Estimate how many newlines we must move back. */
5195 nlines = max (1, dy / CANON_Y_UNIT (it->f));
5196
5197 /* Set the iterator's position that many lines back. */
5198 while (nlines-- && IT_CHARPOS (*it) > BEGV)
5199 back_to_previous_visible_line_start (it);
5200
5201 /* Reseat the iterator here. When moving backward, we don't want
5202 reseat to skip forward over invisible text, set up the iterator
5203 to deliver from overlay strings at the new position etc. So,
5204 use reseat_1 here. */
5205 reseat_1 (it, it->current.pos, 1);
5206
5207 /* We are now surely at a line start. */
5208 it->current_x = it->hpos = 0;
5209
5210 /* Move forward and see what y-distance we moved. First move to the
5211 start of the next line so that we get its height. We need this
5212 height to be able to tell whether we reached the specified
5213 y-distance. */
5214 it2 = *it;
5215 it2.max_ascent = it2.max_descent = 0;
5216 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
5217 MOVE_TO_POS | MOVE_TO_VPOS);
5218 xassert (IT_CHARPOS (*it) >= BEGV);
5219 line_height = it2.max_ascent + it2.max_descent;
5220
5221 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
5222 xassert (IT_CHARPOS (*it) >= BEGV);
5223 h = it2.current_y - it->current_y;
5224 nlines = it2.vpos - it->vpos;
5225
5226 /* Correct IT's y and vpos position. */
5227 it->vpos -= nlines;
5228 it->current_y -= h;
5229
5230 if (dy == 0)
5231 {
5232 /* DY == 0 means move to the start of the screen line. The
5233 value of nlines is > 0 if continuation lines were involved. */
5234 if (nlines > 0)
5235 move_it_by_lines (it, nlines, 1);
5236 xassert (IT_CHARPOS (*it) <= start_pos);
5237 }
5238 else if (nlines)
5239 {
5240 /* The y-position we try to reach. Note that h has been
5241 subtracted in front of the if-statement. */
5242 int target_y = it->current_y + h - dy;
5243
5244 /* If we did not reach target_y, try to move further backward if
5245 we can. If we moved too far backward, try to move forward. */
5246 if (target_y < it->current_y
5247 && IT_CHARPOS (*it) > BEGV)
5248 {
5249 move_it_vertically (it, target_y - it->current_y);
5250 xassert (IT_CHARPOS (*it) >= BEGV);
5251 }
5252 else if (target_y >= it->current_y + line_height
5253 && IT_CHARPOS (*it) < ZV)
5254 {
5255 move_it_vertically (it, target_y - (it->current_y + line_height));
5256 xassert (IT_CHARPOS (*it) >= BEGV);
5257 }
5258 }
5259 }
5260
5261
5262 /* Move IT by a specified amount of pixel lines DY. DY negative means
5263 move backwards. DY = 0 means move to start of screen line. At the
5264 end, IT will be on the start of a screen line. */
5265
5266 void
5267 move_it_vertically (it, dy)
5268 struct it *it;
5269 int dy;
5270 {
5271 if (dy <= 0)
5272 move_it_vertically_backward (it, -dy);
5273 else if (dy > 0)
5274 {
5275 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
5276 move_it_to (it, ZV, -1, it->current_y + dy, -1,
5277 MOVE_TO_POS | MOVE_TO_Y);
5278 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
5279
5280 /* If buffer ends in ZV without a newline, move to the start of
5281 the line to satisfy the post-condition. */
5282 if (IT_CHARPOS (*it) == ZV
5283 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
5284 move_it_by_lines (it, 0, 0);
5285 }
5286 }
5287
5288
5289 /* Move iterator IT past the end of the text line it is in. */
5290
5291 void
5292 move_it_past_eol (it)
5293 struct it *it;
5294 {
5295 enum move_it_result rc;
5296
5297 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
5298 if (rc == MOVE_NEWLINE_OR_CR)
5299 set_iterator_to_next (it, 0);
5300 }
5301
5302
5303 #if 0 /* Currently not used. */
5304
5305 /* Return non-zero if some text between buffer positions START_CHARPOS
5306 and END_CHARPOS is invisible. IT->window is the window for text
5307 property lookup. */
5308
5309 static int
5310 invisible_text_between_p (it, start_charpos, end_charpos)
5311 struct it *it;
5312 int start_charpos, end_charpos;
5313 {
5314 Lisp_Object prop, limit;
5315 int invisible_found_p;
5316
5317 xassert (it != NULL && start_charpos <= end_charpos);
5318
5319 /* Is text at START invisible? */
5320 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
5321 it->window);
5322 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5323 invisible_found_p = 1;
5324 else
5325 {
5326 limit = Fnext_single_char_property_change (make_number (start_charpos),
5327 Qinvisible, Qnil,
5328 make_number (end_charpos));
5329 invisible_found_p = XFASTINT (limit) < end_charpos;
5330 }
5331
5332 return invisible_found_p;
5333 }
5334
5335 #endif /* 0 */
5336
5337
5338 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
5339 negative means move up. DVPOS == 0 means move to the start of the
5340 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
5341 NEED_Y_P is zero, IT->current_y will be left unchanged.
5342
5343 Further optimization ideas: If we would know that IT->f doesn't use
5344 a face with proportional font, we could be faster for
5345 truncate-lines nil. */
5346
5347 void
5348 move_it_by_lines (it, dvpos, need_y_p)
5349 struct it *it;
5350 int dvpos, need_y_p;
5351 {
5352 struct position pos;
5353
5354 if (!FRAME_WINDOW_P (it->f))
5355 {
5356 struct text_pos textpos;
5357
5358 /* We can use vmotion on frames without proportional fonts. */
5359 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
5360 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
5361 reseat (it, textpos, 1);
5362 it->vpos += pos.vpos;
5363 it->current_y += pos.vpos;
5364 }
5365 else if (dvpos == 0)
5366 {
5367 /* DVPOS == 0 means move to the start of the screen line. */
5368 move_it_vertically_backward (it, 0);
5369 xassert (it->current_x == 0 && it->hpos == 0);
5370 }
5371 else if (dvpos > 0)
5372 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
5373 else
5374 {
5375 struct it it2;
5376 int start_charpos, i;
5377
5378 /* Go back -DVPOS visible lines and reseat the iterator there. */
5379 start_charpos = IT_CHARPOS (*it);
5380 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
5381 back_to_previous_visible_line_start (it);
5382 reseat (it, it->current.pos, 1);
5383 it->current_x = it->hpos = 0;
5384
5385 /* Above call may have moved too far if continuation lines
5386 are involved. Scan forward and see if it did. */
5387 it2 = *it;
5388 it2.vpos = it2.current_y = 0;
5389 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
5390 it->vpos -= it2.vpos;
5391 it->current_y -= it2.current_y;
5392 it->current_x = it->hpos = 0;
5393
5394 /* If we moved too far, move IT some lines forward. */
5395 if (it2.vpos > -dvpos)
5396 {
5397 int delta = it2.vpos + dvpos;
5398 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
5399 }
5400 }
5401 }
5402
5403
5404 \f
5405 /***********************************************************************
5406 Messages
5407 ***********************************************************************/
5408
5409
5410 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
5411 to *Messages*. */
5412
5413 void
5414 add_to_log (format, arg1, arg2)
5415 char *format;
5416 Lisp_Object arg1, arg2;
5417 {
5418 Lisp_Object args[3];
5419 Lisp_Object msg, fmt;
5420 char *buffer;
5421 int len;
5422 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5423
5424 fmt = msg = Qnil;
5425 GCPRO4 (fmt, msg, arg1, arg2);
5426
5427 args[0] = fmt = build_string (format);
5428 args[1] = arg1;
5429 args[2] = arg2;
5430 msg = Fformat (3, args);
5431
5432 len = STRING_BYTES (XSTRING (msg)) + 1;
5433 buffer = (char *) alloca (len);
5434 strcpy (buffer, XSTRING (msg)->data);
5435
5436 message_dolog (buffer, len - 1, 1, 0);
5437 UNGCPRO;
5438 }
5439
5440
5441 /* Output a newline in the *Messages* buffer if "needs" one. */
5442
5443 void
5444 message_log_maybe_newline ()
5445 {
5446 if (message_log_need_newline)
5447 message_dolog ("", 0, 1, 0);
5448 }
5449
5450
5451 /* Add a string M of length NBYTES to the message log, optionally
5452 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
5453 nonzero, means interpret the contents of M as multibyte. This
5454 function calls low-level routines in order to bypass text property
5455 hooks, etc. which might not be safe to run. */
5456
5457 void
5458 message_dolog (m, nbytes, nlflag, multibyte)
5459 char *m;
5460 int nbytes, nlflag, multibyte;
5461 {
5462 if (!NILP (Vmessage_log_max))
5463 {
5464 struct buffer *oldbuf;
5465 Lisp_Object oldpoint, oldbegv, oldzv;
5466 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5467 int point_at_end = 0;
5468 int zv_at_end = 0;
5469 Lisp_Object old_deactivate_mark, tem;
5470 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5471
5472 old_deactivate_mark = Vdeactivate_mark;
5473 oldbuf = current_buffer;
5474 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
5475 current_buffer->undo_list = Qt;
5476
5477 oldpoint = Fpoint_marker ();
5478 oldbegv = Fpoint_min_marker ();
5479 oldzv = Fpoint_max_marker ();
5480 GCPRO4 (oldpoint, oldbegv, oldzv, old_deactivate_mark);
5481
5482 if (PT == Z)
5483 point_at_end = 1;
5484 if (ZV == Z)
5485 zv_at_end = 1;
5486
5487 BEGV = BEG;
5488 BEGV_BYTE = BEG_BYTE;
5489 ZV = Z;
5490 ZV_BYTE = Z_BYTE;
5491 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5492
5493 /* Insert the string--maybe converting multibyte to single byte
5494 or vice versa, so that all the text fits the buffer. */
5495 if (multibyte
5496 && NILP (current_buffer->enable_multibyte_characters))
5497 {
5498 int i, c, char_bytes;
5499 unsigned char work[1];
5500
5501 /* Convert a multibyte string to single-byte
5502 for the *Message* buffer. */
5503 for (i = 0; i < nbytes; i += nbytes)
5504 {
5505 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
5506 work[0] = (SINGLE_BYTE_CHAR_P (c)
5507 ? c
5508 : multibyte_char_to_unibyte (c, Qnil));
5509 insert_1_both (work, 1, 1, 1, 0, 0);
5510 }
5511 }
5512 else if (! multibyte
5513 && ! NILP (current_buffer->enable_multibyte_characters))
5514 {
5515 int i, c, char_bytes;
5516 unsigned char *msg = (unsigned char *) m;
5517 unsigned char str[MAX_MULTIBYTE_LENGTH];
5518 /* Convert a single-byte string to multibyte
5519 for the *Message* buffer. */
5520 for (i = 0; i < nbytes; i++)
5521 {
5522 c = unibyte_char_to_multibyte (msg[i]);
5523 char_bytes = CHAR_STRING (c, str);
5524 insert_1_both (str, 1, char_bytes, 1, 0, 0);
5525 }
5526 }
5527 else if (nbytes)
5528 insert_1 (m, nbytes, 1, 0, 0);
5529
5530 if (nlflag)
5531 {
5532 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
5533 insert_1 ("\n", 1, 1, 0, 0);
5534
5535 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
5536 this_bol = PT;
5537 this_bol_byte = PT_BYTE;
5538
5539 if (this_bol > BEG)
5540 {
5541 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
5542 prev_bol = PT;
5543 prev_bol_byte = PT_BYTE;
5544
5545 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
5546 this_bol, this_bol_byte);
5547 if (dup)
5548 {
5549 del_range_both (prev_bol, prev_bol_byte,
5550 this_bol, this_bol_byte, 0);
5551 if (dup > 1)
5552 {
5553 char dupstr[40];
5554 int duplen;
5555
5556 /* If you change this format, don't forget to also
5557 change message_log_check_duplicate. */
5558 sprintf (dupstr, " [%d times]", dup);
5559 duplen = strlen (dupstr);
5560 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
5561 insert_1 (dupstr, duplen, 1, 0, 1);
5562 }
5563 }
5564 }
5565
5566 if (NATNUMP (Vmessage_log_max))
5567 {
5568 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
5569 -XFASTINT (Vmessage_log_max) - 1, 0);
5570 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
5571 }
5572 }
5573 BEGV = XMARKER (oldbegv)->charpos;
5574 BEGV_BYTE = marker_byte_position (oldbegv);
5575
5576 if (zv_at_end)
5577 {
5578 ZV = Z;
5579 ZV_BYTE = Z_BYTE;
5580 }
5581 else
5582 {
5583 ZV = XMARKER (oldzv)->charpos;
5584 ZV_BYTE = marker_byte_position (oldzv);
5585 }
5586
5587 if (point_at_end)
5588 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5589 else
5590 /* We can't do Fgoto_char (oldpoint) because it will run some
5591 Lisp code. */
5592 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
5593 XMARKER (oldpoint)->bytepos);
5594
5595 UNGCPRO;
5596 free_marker (oldpoint);
5597 free_marker (oldbegv);
5598 free_marker (oldzv);
5599
5600 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
5601 set_buffer_internal (oldbuf);
5602 if (NILP (tem))
5603 windows_or_buffers_changed = old_windows_or_buffers_changed;
5604 message_log_need_newline = !nlflag;
5605 Vdeactivate_mark = old_deactivate_mark;
5606 }
5607 }
5608
5609
5610 /* We are at the end of the buffer after just having inserted a newline.
5611 (Note: We depend on the fact we won't be crossing the gap.)
5612 Check to see if the most recent message looks a lot like the previous one.
5613 Return 0 if different, 1 if the new one should just replace it, or a
5614 value N > 1 if we should also append " [N times]". */
5615
5616 static int
5617 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
5618 int prev_bol, this_bol;
5619 int prev_bol_byte, this_bol_byte;
5620 {
5621 int i;
5622 int len = Z_BYTE - 1 - this_bol_byte;
5623 int seen_dots = 0;
5624 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
5625 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
5626
5627 for (i = 0; i < len; i++)
5628 {
5629 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
5630 seen_dots = 1;
5631 if (p1[i] != p2[i])
5632 return seen_dots;
5633 }
5634 p1 += len;
5635 if (*p1 == '\n')
5636 return 2;
5637 if (*p1++ == ' ' && *p1++ == '[')
5638 {
5639 int n = 0;
5640 while (*p1 >= '0' && *p1 <= '9')
5641 n = n * 10 + *p1++ - '0';
5642 if (strncmp (p1, " times]\n", 8) == 0)
5643 return n+1;
5644 }
5645 return 0;
5646 }
5647
5648
5649 /* Display an echo area message M with a specified length of NBYTES
5650 bytes. The string may include null characters. If M is 0, clear
5651 out any existing message, and let the mini-buffer text show
5652 through.
5653
5654 The buffer M must continue to exist until after the echo area gets
5655 cleared or some other message gets displayed there. This means do
5656 not pass text that is stored in a Lisp string; do not pass text in
5657 a buffer that was alloca'd. */
5658
5659 void
5660 message2 (m, nbytes, multibyte)
5661 char *m;
5662 int nbytes;
5663 int multibyte;
5664 {
5665 /* First flush out any partial line written with print. */
5666 message_log_maybe_newline ();
5667 if (m)
5668 message_dolog (m, nbytes, 1, multibyte);
5669 message2_nolog (m, nbytes, multibyte);
5670 }
5671
5672
5673 /* The non-logging counterpart of message2. */
5674
5675 void
5676 message2_nolog (m, nbytes, multibyte)
5677 char *m;
5678 int nbytes;
5679 {
5680 struct frame *sf = SELECTED_FRAME ();
5681 message_enable_multibyte = multibyte;
5682
5683 if (noninteractive)
5684 {
5685 if (noninteractive_need_newline)
5686 putc ('\n', stderr);
5687 noninteractive_need_newline = 0;
5688 if (m)
5689 fwrite (m, nbytes, 1, stderr);
5690 if (cursor_in_echo_area == 0)
5691 fprintf (stderr, "\n");
5692 fflush (stderr);
5693 }
5694 /* A null message buffer means that the frame hasn't really been
5695 initialized yet. Error messages get reported properly by
5696 cmd_error, so this must be just an informative message; toss it. */
5697 else if (INTERACTIVE
5698 && sf->glyphs_initialized_p
5699 && FRAME_MESSAGE_BUF (sf))
5700 {
5701 Lisp_Object mini_window;
5702 struct frame *f;
5703
5704 /* Get the frame containing the mini-buffer
5705 that the selected frame is using. */
5706 mini_window = FRAME_MINIBUF_WINDOW (sf);
5707 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5708
5709 FRAME_SAMPLE_VISIBILITY (f);
5710 if (FRAME_VISIBLE_P (sf)
5711 && ! FRAME_VISIBLE_P (f))
5712 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
5713
5714 if (m)
5715 {
5716 set_message (m, Qnil, nbytes, multibyte);
5717 if (minibuffer_auto_raise)
5718 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5719 }
5720 else
5721 clear_message (1, 1);
5722
5723 do_pending_window_change (0);
5724 echo_area_display (1);
5725 do_pending_window_change (0);
5726 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5727 (*frame_up_to_date_hook) (f);
5728 }
5729 }
5730
5731
5732 /* Display an echo area message M with a specified length of NBYTES
5733 bytes. The string may include null characters. If M is not a
5734 string, clear out any existing message, and let the mini-buffer
5735 text show through. */
5736
5737 void
5738 message3 (m, nbytes, multibyte)
5739 Lisp_Object m;
5740 int nbytes;
5741 int multibyte;
5742 {
5743 struct gcpro gcpro1;
5744
5745 GCPRO1 (m);
5746
5747 /* First flush out any partial line written with print. */
5748 message_log_maybe_newline ();
5749 if (STRINGP (m))
5750 message_dolog (XSTRING (m)->data, nbytes, 1, multibyte);
5751 message3_nolog (m, nbytes, multibyte);
5752
5753 UNGCPRO;
5754 }
5755
5756
5757 /* The non-logging version of message3. */
5758
5759 void
5760 message3_nolog (m, nbytes, multibyte)
5761 Lisp_Object m;
5762 int nbytes, multibyte;
5763 {
5764 struct frame *sf = SELECTED_FRAME ();
5765 message_enable_multibyte = multibyte;
5766
5767 if (noninteractive)
5768 {
5769 if (noninteractive_need_newline)
5770 putc ('\n', stderr);
5771 noninteractive_need_newline = 0;
5772 if (STRINGP (m))
5773 fwrite (XSTRING (m)->data, nbytes, 1, stderr);
5774 if (cursor_in_echo_area == 0)
5775 fprintf (stderr, "\n");
5776 fflush (stderr);
5777 }
5778 /* A null message buffer means that the frame hasn't really been
5779 initialized yet. Error messages get reported properly by
5780 cmd_error, so this must be just an informative message; toss it. */
5781 else if (INTERACTIVE
5782 && sf->glyphs_initialized_p
5783 && FRAME_MESSAGE_BUF (sf))
5784 {
5785 Lisp_Object mini_window;
5786 Lisp_Object frame;
5787 struct frame *f;
5788
5789 /* Get the frame containing the mini-buffer
5790 that the selected frame is using. */
5791 mini_window = FRAME_MINIBUF_WINDOW (sf);
5792 frame = XWINDOW (mini_window)->frame;
5793 f = XFRAME (frame);
5794
5795 FRAME_SAMPLE_VISIBILITY (f);
5796 if (FRAME_VISIBLE_P (sf)
5797 && !FRAME_VISIBLE_P (f))
5798 Fmake_frame_visible (frame);
5799
5800 if (STRINGP (m) && XSTRING (m)->size)
5801 {
5802 set_message (NULL, m, nbytes, multibyte);
5803 if (minibuffer_auto_raise)
5804 Fraise_frame (frame);
5805 }
5806 else
5807 clear_message (1, 1);
5808
5809 do_pending_window_change (0);
5810 echo_area_display (1);
5811 do_pending_window_change (0);
5812 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5813 (*frame_up_to_date_hook) (f);
5814 }
5815 }
5816
5817
5818 /* Display a null-terminated echo area message M. If M is 0, clear
5819 out any existing message, and let the mini-buffer text show through.
5820
5821 The buffer M must continue to exist until after the echo area gets
5822 cleared or some other message gets displayed there. Do not pass
5823 text that is stored in a Lisp string. Do not pass text in a buffer
5824 that was alloca'd. */
5825
5826 void
5827 message1 (m)
5828 char *m;
5829 {
5830 message2 (m, (m ? strlen (m) : 0), 0);
5831 }
5832
5833
5834 /* The non-logging counterpart of message1. */
5835
5836 void
5837 message1_nolog (m)
5838 char *m;
5839 {
5840 message2_nolog (m, (m ? strlen (m) : 0), 0);
5841 }
5842
5843 /* Display a message M which contains a single %s
5844 which gets replaced with STRING. */
5845
5846 void
5847 message_with_string (m, string, log)
5848 char *m;
5849 Lisp_Object string;
5850 int log;
5851 {
5852 if (noninteractive)
5853 {
5854 if (m)
5855 {
5856 if (noninteractive_need_newline)
5857 putc ('\n', stderr);
5858 noninteractive_need_newline = 0;
5859 fprintf (stderr, m, XSTRING (string)->data);
5860 if (cursor_in_echo_area == 0)
5861 fprintf (stderr, "\n");
5862 fflush (stderr);
5863 }
5864 }
5865 else if (INTERACTIVE)
5866 {
5867 /* The frame whose minibuffer we're going to display the message on.
5868 It may be larger than the selected frame, so we need
5869 to use its buffer, not the selected frame's buffer. */
5870 Lisp_Object mini_window;
5871 struct frame *f, *sf = SELECTED_FRAME ();
5872
5873 /* Get the frame containing the minibuffer
5874 that the selected frame is using. */
5875 mini_window = FRAME_MINIBUF_WINDOW (sf);
5876 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5877
5878 /* A null message buffer means that the frame hasn't really been
5879 initialized yet. Error messages get reported properly by
5880 cmd_error, so this must be just an informative message; toss it. */
5881 if (FRAME_MESSAGE_BUF (f))
5882 {
5883 int len;
5884 char *a[1];
5885 a[0] = (char *) XSTRING (string)->data;
5886
5887 len = doprnt (FRAME_MESSAGE_BUF (f),
5888 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5889
5890 if (log)
5891 message2 (FRAME_MESSAGE_BUF (f), len,
5892 STRING_MULTIBYTE (string));
5893 else
5894 message2_nolog (FRAME_MESSAGE_BUF (f), len,
5895 STRING_MULTIBYTE (string));
5896
5897 /* Print should start at the beginning of the message
5898 buffer next time. */
5899 message_buf_print = 0;
5900 }
5901 }
5902 }
5903
5904
5905 /* Dump an informative message to the minibuf. If M is 0, clear out
5906 any existing message, and let the mini-buffer text show through. */
5907
5908 /* VARARGS 1 */
5909 void
5910 message (m, a1, a2, a3)
5911 char *m;
5912 EMACS_INT a1, a2, a3;
5913 {
5914 if (noninteractive)
5915 {
5916 if (m)
5917 {
5918 if (noninteractive_need_newline)
5919 putc ('\n', stderr);
5920 noninteractive_need_newline = 0;
5921 fprintf (stderr, m, a1, a2, a3);
5922 if (cursor_in_echo_area == 0)
5923 fprintf (stderr, "\n");
5924 fflush (stderr);
5925 }
5926 }
5927 else if (INTERACTIVE)
5928 {
5929 /* The frame whose mini-buffer we're going to display the message
5930 on. It may be larger than the selected frame, so we need to
5931 use its buffer, not the selected frame's buffer. */
5932 Lisp_Object mini_window;
5933 struct frame *f, *sf = SELECTED_FRAME ();
5934
5935 /* Get the frame containing the mini-buffer
5936 that the selected frame is using. */
5937 mini_window = FRAME_MINIBUF_WINDOW (sf);
5938 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5939
5940 /* A null message buffer means that the frame hasn't really been
5941 initialized yet. Error messages get reported properly by
5942 cmd_error, so this must be just an informative message; toss
5943 it. */
5944 if (FRAME_MESSAGE_BUF (f))
5945 {
5946 if (m)
5947 {
5948 int len;
5949 #ifdef NO_ARG_ARRAY
5950 char *a[3];
5951 a[0] = (char *) a1;
5952 a[1] = (char *) a2;
5953 a[2] = (char *) a3;
5954
5955 len = doprnt (FRAME_MESSAGE_BUF (f),
5956 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5957 #else
5958 len = doprnt (FRAME_MESSAGE_BUF (f),
5959 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
5960 (char **) &a1);
5961 #endif /* NO_ARG_ARRAY */
5962
5963 message2 (FRAME_MESSAGE_BUF (f), len, 0);
5964 }
5965 else
5966 message1 (0);
5967
5968 /* Print should start at the beginning of the message
5969 buffer next time. */
5970 message_buf_print = 0;
5971 }
5972 }
5973 }
5974
5975
5976 /* The non-logging version of message. */
5977
5978 void
5979 message_nolog (m, a1, a2, a3)
5980 char *m;
5981 EMACS_INT a1, a2, a3;
5982 {
5983 Lisp_Object old_log_max;
5984 old_log_max = Vmessage_log_max;
5985 Vmessage_log_max = Qnil;
5986 message (m, a1, a2, a3);
5987 Vmessage_log_max = old_log_max;
5988 }
5989
5990
5991 /* Display the current message in the current mini-buffer. This is
5992 only called from error handlers in process.c, and is not time
5993 critical. */
5994
5995 void
5996 update_echo_area ()
5997 {
5998 if (!NILP (echo_area_buffer[0]))
5999 {
6000 Lisp_Object string;
6001 string = Fcurrent_message ();
6002 message3 (string, XSTRING (string)->size,
6003 !NILP (current_buffer->enable_multibyte_characters));
6004 }
6005 }
6006
6007
6008 /* Make sure echo area buffers in echo_buffers[] are life. If they
6009 aren't, make new ones. */
6010
6011 static void
6012 ensure_echo_area_buffers ()
6013 {
6014 int i;
6015
6016 for (i = 0; i < 2; ++i)
6017 if (!BUFFERP (echo_buffer[i])
6018 || NILP (XBUFFER (echo_buffer[i])->name))
6019 {
6020 char name[30];
6021 Lisp_Object old_buffer;
6022 int j;
6023
6024 old_buffer = echo_buffer[i];
6025 sprintf (name, " *Echo Area %d*", i);
6026 echo_buffer[i] = Fget_buffer_create (build_string (name));
6027 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
6028
6029 for (j = 0; j < 2; ++j)
6030 if (EQ (old_buffer, echo_area_buffer[j]))
6031 echo_area_buffer[j] = echo_buffer[i];
6032 }
6033 }
6034
6035
6036 /* Call FN with args A1..A4 with either the current or last displayed
6037 echo_area_buffer as current buffer.
6038
6039 WHICH zero means use the current message buffer
6040 echo_area_buffer[0]. If that is nil, choose a suitable buffer
6041 from echo_buffer[] and clear it.
6042
6043 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
6044 suitable buffer from echo_buffer[] and clear it.
6045
6046 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
6047 that the current message becomes the last displayed one, make
6048 choose a suitable buffer for echo_area_buffer[0], and clear it.
6049
6050 Value is what FN returns. */
6051
6052 static int
6053 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
6054 struct window *w;
6055 int which;
6056 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
6057 EMACS_INT a1;
6058 Lisp_Object a2;
6059 EMACS_INT a3, a4;
6060 {
6061 Lisp_Object buffer;
6062 int this_one, the_other, clear_buffer_p, rc;
6063 int count = BINDING_STACK_SIZE ();
6064
6065 /* If buffers aren't life, make new ones. */
6066 ensure_echo_area_buffers ();
6067
6068 clear_buffer_p = 0;
6069
6070 if (which == 0)
6071 this_one = 0, the_other = 1;
6072 else if (which > 0)
6073 this_one = 1, the_other = 0;
6074 else
6075 {
6076 this_one = 0, the_other = 1;
6077 clear_buffer_p = 1;
6078
6079 /* We need a fresh one in case the current echo buffer equals
6080 the one containing the last displayed echo area message. */
6081 if (!NILP (echo_area_buffer[this_one])
6082 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
6083 echo_area_buffer[this_one] = Qnil;
6084 }
6085
6086 /* Choose a suitable buffer from echo_buffer[] is we don't
6087 have one. */
6088 if (NILP (echo_area_buffer[this_one]))
6089 {
6090 echo_area_buffer[this_one]
6091 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
6092 ? echo_buffer[the_other]
6093 : echo_buffer[this_one]);
6094 clear_buffer_p = 1;
6095 }
6096
6097 buffer = echo_area_buffer[this_one];
6098
6099 record_unwind_protect (unwind_with_echo_area_buffer,
6100 with_echo_area_buffer_unwind_data (w));
6101
6102 /* Make the echo area buffer current. Note that for display
6103 purposes, it is not necessary that the displayed window's buffer
6104 == current_buffer, except for text property lookup. So, let's
6105 only set that buffer temporarily here without doing a full
6106 Fset_window_buffer. We must also change w->pointm, though,
6107 because otherwise an assertions in unshow_buffer fails, and Emacs
6108 aborts. */
6109 set_buffer_internal_1 (XBUFFER (buffer));
6110 if (w)
6111 {
6112 w->buffer = buffer;
6113 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
6114 }
6115
6116 current_buffer->undo_list = Qt;
6117 current_buffer->read_only = Qnil;
6118 specbind (Qinhibit_read_only, Qt);
6119
6120 if (clear_buffer_p && Z > BEG)
6121 del_range (BEG, Z);
6122
6123 xassert (BEGV >= BEG);
6124 xassert (ZV <= Z && ZV >= BEGV);
6125
6126 rc = fn (a1, a2, a3, a4);
6127
6128 xassert (BEGV >= BEG);
6129 xassert (ZV <= Z && ZV >= BEGV);
6130
6131 unbind_to (count, Qnil);
6132 return rc;
6133 }
6134
6135
6136 /* Save state that should be preserved around the call to the function
6137 FN called in with_echo_area_buffer. */
6138
6139 static Lisp_Object
6140 with_echo_area_buffer_unwind_data (w)
6141 struct window *w;
6142 {
6143 int i = 0;
6144 Lisp_Object vector;
6145
6146 /* Reduce consing by keeping one vector in
6147 Vwith_echo_area_save_vector. */
6148 vector = Vwith_echo_area_save_vector;
6149 Vwith_echo_area_save_vector = Qnil;
6150
6151 if (NILP (vector))
6152 vector = Fmake_vector (make_number (7), Qnil);
6153
6154 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
6155 AREF (vector, i) = Vdeactivate_mark, ++i;
6156 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
6157
6158 if (w)
6159 {
6160 XSETWINDOW (AREF (vector, i), w); ++i;
6161 AREF (vector, i) = w->buffer; ++i;
6162 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
6163 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
6164 }
6165 else
6166 {
6167 int end = i + 4;
6168 for (; i < end; ++i)
6169 AREF (vector, i) = Qnil;
6170 }
6171
6172 xassert (i == ASIZE (vector));
6173 return vector;
6174 }
6175
6176
6177 /* Restore global state from VECTOR which was created by
6178 with_echo_area_buffer_unwind_data. */
6179
6180 static Lisp_Object
6181 unwind_with_echo_area_buffer (vector)
6182 Lisp_Object vector;
6183 {
6184 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
6185 Vdeactivate_mark = AREF (vector, 1);
6186 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
6187
6188 if (WINDOWP (AREF (vector, 3)))
6189 {
6190 struct window *w;
6191 Lisp_Object buffer, charpos, bytepos;
6192
6193 w = XWINDOW (AREF (vector, 3));
6194 buffer = AREF (vector, 4);
6195 charpos = AREF (vector, 5);
6196 bytepos = AREF (vector, 6);
6197
6198 w->buffer = buffer;
6199 set_marker_both (w->pointm, buffer,
6200 XFASTINT (charpos), XFASTINT (bytepos));
6201 }
6202
6203 Vwith_echo_area_save_vector = vector;
6204 return Qnil;
6205 }
6206
6207
6208 /* Set up the echo area for use by print functions. MULTIBYTE_P
6209 non-zero means we will print multibyte. */
6210
6211 void
6212 setup_echo_area_for_printing (multibyte_p)
6213 int multibyte_p;
6214 {
6215 ensure_echo_area_buffers ();
6216
6217 if (!message_buf_print)
6218 {
6219 /* A message has been output since the last time we printed.
6220 Choose a fresh echo area buffer. */
6221 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6222 echo_area_buffer[0] = echo_buffer[1];
6223 else
6224 echo_area_buffer[0] = echo_buffer[0];
6225
6226 /* Switch to that buffer and clear it. */
6227 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6228 current_buffer->truncate_lines = Qnil;
6229
6230 if (Z > BEG)
6231 {
6232 int count = BINDING_STACK_SIZE ();
6233 specbind (Qinhibit_read_only, Qt);
6234 del_range (BEG, Z);
6235 unbind_to (count, Qnil);
6236 }
6237 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6238
6239 /* Set up the buffer for the multibyteness we need. */
6240 if (multibyte_p
6241 != !NILP (current_buffer->enable_multibyte_characters))
6242 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
6243
6244 /* Raise the frame containing the echo area. */
6245 if (minibuffer_auto_raise)
6246 {
6247 struct frame *sf = SELECTED_FRAME ();
6248 Lisp_Object mini_window;
6249 mini_window = FRAME_MINIBUF_WINDOW (sf);
6250 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6251 }
6252
6253 message_log_maybe_newline ();
6254 message_buf_print = 1;
6255 }
6256 else
6257 {
6258 if (NILP (echo_area_buffer[0]))
6259 {
6260 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6261 echo_area_buffer[0] = echo_buffer[1];
6262 else
6263 echo_area_buffer[0] = echo_buffer[0];
6264 }
6265
6266 if (current_buffer != XBUFFER (echo_area_buffer[0]))
6267 {
6268 /* Someone switched buffers between print requests. */
6269 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6270 current_buffer->truncate_lines = Qnil;
6271 }
6272 }
6273 }
6274
6275
6276 /* Display an echo area message in window W. Value is non-zero if W's
6277 height is changed. If display_last_displayed_message_p is
6278 non-zero, display the message that was last displayed, otherwise
6279 display the current message. */
6280
6281 static int
6282 display_echo_area (w)
6283 struct window *w;
6284 {
6285 int i, no_message_p, window_height_changed_p, count;
6286
6287 /* Temporarily disable garbage collections while displaying the echo
6288 area. This is done because a GC can print a message itself.
6289 That message would modify the echo area buffer's contents while a
6290 redisplay of the buffer is going on, and seriously confuse
6291 redisplay. */
6292 count = inhibit_garbage_collection ();
6293
6294 /* If there is no message, we must call display_echo_area_1
6295 nevertheless because it resizes the window. But we will have to
6296 reset the echo_area_buffer in question to nil at the end because
6297 with_echo_area_buffer will sets it to an empty buffer. */
6298 i = display_last_displayed_message_p ? 1 : 0;
6299 no_message_p = NILP (echo_area_buffer[i]);
6300
6301 window_height_changed_p
6302 = with_echo_area_buffer (w, display_last_displayed_message_p,
6303 display_echo_area_1,
6304 (EMACS_INT) w, Qnil, 0, 0);
6305
6306 if (no_message_p)
6307 echo_area_buffer[i] = Qnil;
6308
6309 unbind_to (count, Qnil);
6310 return window_height_changed_p;
6311 }
6312
6313
6314 /* Helper for display_echo_area. Display the current buffer which
6315 contains the current echo area message in window W, a mini-window,
6316 a pointer to which is passed in A1. A2..A4 are currently not used.
6317 Change the height of W so that all of the message is displayed.
6318 Value is non-zero if height of W was changed. */
6319
6320 static int
6321 display_echo_area_1 (a1, a2, a3, a4)
6322 EMACS_INT a1;
6323 Lisp_Object a2;
6324 EMACS_INT a3, a4;
6325 {
6326 struct window *w = (struct window *) a1;
6327 Lisp_Object window;
6328 struct text_pos start;
6329 int window_height_changed_p = 0;
6330
6331 /* Do this before displaying, so that we have a large enough glyph
6332 matrix for the display. */
6333 window_height_changed_p = resize_mini_window (w, 0);
6334
6335 /* Display. */
6336 clear_glyph_matrix (w->desired_matrix);
6337 XSETWINDOW (window, w);
6338 SET_TEXT_POS (start, BEG, BEG_BYTE);
6339 try_window (window, start);
6340
6341 return window_height_changed_p;
6342 }
6343
6344
6345 /* Resize the echo area window to exactly the size needed for the
6346 currently displayed message, if there is one. */
6347
6348 void
6349 resize_echo_area_axactly ()
6350 {
6351 if (BUFFERP (echo_area_buffer[0])
6352 && WINDOWP (echo_area_window))
6353 {
6354 struct window *w = XWINDOW (echo_area_window);
6355 int resized_p;
6356
6357 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
6358 (EMACS_INT) w, Qnil, 0, 0);
6359 if (resized_p)
6360 {
6361 ++windows_or_buffers_changed;
6362 ++update_mode_lines;
6363 redisplay_internal (0);
6364 }
6365 }
6366 }
6367
6368
6369 /* Callback function for with_echo_area_buffer, when used from
6370 resize_echo_area_axactly. A1 contains a pointer to the window to
6371 resize, A2 to A4 are not used. Value is what resize_mini_window
6372 returns. */
6373
6374 static int
6375 resize_mini_window_1 (a1, a2, a3, a4)
6376 EMACS_INT a1;
6377 Lisp_Object a2;
6378 EMACS_INT a3, a4;
6379 {
6380 return resize_mini_window ((struct window *) a1, 1);
6381 }
6382
6383
6384 /* Resize mini-window W to fit the size of its contents. EXACT:P
6385 means size the window exactly to the size needed. Otherwise, it's
6386 only enlarged until W's buffer is empty. Value is non-zero if
6387 the window height has been changed. */
6388
6389 int
6390 resize_mini_window (w, exact_p)
6391 struct window *w;
6392 int exact_p;
6393 {
6394 struct frame *f = XFRAME (w->frame);
6395 int window_height_changed_p = 0;
6396
6397 xassert (MINI_WINDOW_P (w));
6398
6399 /* Nil means don't try to resize. */
6400 if (NILP (Vresize_mini_windows)
6401 || (FRAME_X_P (f) && f->output_data.x == NULL))
6402 return 0;
6403
6404 if (!FRAME_MINIBUF_ONLY_P (f))
6405 {
6406 struct it it;
6407 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
6408 int total_height = XFASTINT (root->height) + XFASTINT (w->height);
6409 int height, max_height;
6410 int unit = CANON_Y_UNIT (f);
6411 struct text_pos start;
6412 struct buffer *old_current_buffer = NULL;
6413
6414 if (current_buffer != XBUFFER (w->buffer))
6415 {
6416 old_current_buffer = current_buffer;
6417 set_buffer_internal (XBUFFER (w->buffer));
6418 }
6419
6420 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
6421
6422 /* Compute the max. number of lines specified by the user. */
6423 if (FLOATP (Vmax_mini_window_height))
6424 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
6425 else if (INTEGERP (Vmax_mini_window_height))
6426 max_height = XINT (Vmax_mini_window_height);
6427 else
6428 max_height = total_height / 4;
6429
6430 /* Correct that max. height if it's bogus. */
6431 max_height = max (1, max_height);
6432 max_height = min (total_height, max_height);
6433
6434 /* Find out the height of the text in the window. */
6435 if (it.truncate_lines_p)
6436 height = 1;
6437 else
6438 {
6439 last_height = 0;
6440 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
6441 if (it.max_ascent == 0 && it.max_descent == 0)
6442 height = it.current_y + last_height;
6443 else
6444 height = it.current_y + it.max_ascent + it.max_descent;
6445 height -= it.extra_line_spacing;
6446 height = (height + unit - 1) / unit;
6447 }
6448
6449 /* Compute a suitable window start. */
6450 if (height > max_height)
6451 {
6452 height = max_height;
6453 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
6454 move_it_vertically_backward (&it, (height - 1) * unit);
6455 start = it.current.pos;
6456 }
6457 else
6458 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
6459 SET_MARKER_FROM_TEXT_POS (w->start, start);
6460
6461 if (EQ (Vresize_mini_windows, Qgrow_only))
6462 {
6463 /* Let it grow only, until we display an empty message, in which
6464 case the window shrinks again. */
6465 if (height > XFASTINT (w->height))
6466 {
6467 int old_height = XFASTINT (w->height);
6468 freeze_window_starts (f, 1);
6469 grow_mini_window (w, height - XFASTINT (w->height));
6470 window_height_changed_p = XFASTINT (w->height) != old_height;
6471 }
6472 else if (height < XFASTINT (w->height)
6473 && (exact_p || BEGV == ZV))
6474 {
6475 int old_height = XFASTINT (w->height);
6476 freeze_window_starts (f, 0);
6477 shrink_mini_window (w);
6478 window_height_changed_p = XFASTINT (w->height) != old_height;
6479 }
6480 }
6481 else
6482 {
6483 /* Always resize to exact size needed. */
6484 if (height > XFASTINT (w->height))
6485 {
6486 int old_height = XFASTINT (w->height);
6487 freeze_window_starts (f, 1);
6488 grow_mini_window (w, height - XFASTINT (w->height));
6489 window_height_changed_p = XFASTINT (w->height) != old_height;
6490 }
6491 else if (height < XFASTINT (w->height))
6492 {
6493 int old_height = XFASTINT (w->height);
6494 freeze_window_starts (f, 0);
6495 shrink_mini_window (w);
6496
6497 if (height)
6498 {
6499 freeze_window_starts (f, 1);
6500 grow_mini_window (w, height - XFASTINT (w->height));
6501 }
6502
6503 window_height_changed_p = XFASTINT (w->height) != old_height;
6504 }
6505 }
6506
6507 if (old_current_buffer)
6508 set_buffer_internal (old_current_buffer);
6509 }
6510
6511 return window_height_changed_p;
6512 }
6513
6514
6515 /* Value is the current message, a string, or nil if there is no
6516 current message. */
6517
6518 Lisp_Object
6519 current_message ()
6520 {
6521 Lisp_Object msg;
6522
6523 if (NILP (echo_area_buffer[0]))
6524 msg = Qnil;
6525 else
6526 {
6527 with_echo_area_buffer (0, 0, current_message_1,
6528 (EMACS_INT) &msg, Qnil, 0, 0);
6529 if (NILP (msg))
6530 echo_area_buffer[0] = Qnil;
6531 }
6532
6533 return msg;
6534 }
6535
6536
6537 static int
6538 current_message_1 (a1, a2, a3, a4)
6539 EMACS_INT a1;
6540 Lisp_Object a2;
6541 EMACS_INT a3, a4;
6542 {
6543 Lisp_Object *msg = (Lisp_Object *) a1;
6544
6545 if (Z > BEG)
6546 *msg = make_buffer_string (BEG, Z, 1);
6547 else
6548 *msg = Qnil;
6549 return 0;
6550 }
6551
6552
6553 /* Push the current message on Vmessage_stack for later restauration
6554 by restore_message. Value is non-zero if the current message isn't
6555 empty. This is a relatively infrequent operation, so it's not
6556 worth optimizing. */
6557
6558 int
6559 push_message ()
6560 {
6561 Lisp_Object msg;
6562 msg = current_message ();
6563 Vmessage_stack = Fcons (msg, Vmessage_stack);
6564 return STRINGP (msg);
6565 }
6566
6567
6568 /* Handler for record_unwind_protect calling pop_message. */
6569
6570 Lisp_Object
6571 push_message_unwind (dummy)
6572 Lisp_Object dummy;
6573 {
6574 pop_message ();
6575 return Qnil;
6576 }
6577
6578
6579 /* Restore message display from the top of Vmessage_stack. */
6580
6581 void
6582 restore_message ()
6583 {
6584 Lisp_Object msg;
6585
6586 xassert (CONSP (Vmessage_stack));
6587 msg = XCAR (Vmessage_stack);
6588 if (STRINGP (msg))
6589 message3_nolog (msg, STRING_BYTES (XSTRING (msg)), STRING_MULTIBYTE (msg));
6590 else
6591 message3_nolog (msg, 0, 0);
6592 }
6593
6594
6595 /* Pop the top-most entry off Vmessage_stack. */
6596
6597 void
6598 pop_message ()
6599 {
6600 xassert (CONSP (Vmessage_stack));
6601 Vmessage_stack = XCDR (Vmessage_stack);
6602 }
6603
6604
6605 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
6606 exits. If the stack is not empty, we have a missing pop_message
6607 somewhere. */
6608
6609 void
6610 check_message_stack ()
6611 {
6612 if (!NILP (Vmessage_stack))
6613 abort ();
6614 }
6615
6616
6617 /* Truncate to NCHARS what will be displayed in the echo area the next
6618 time we display it---but don't redisplay it now. */
6619
6620 void
6621 truncate_echo_area (nchars)
6622 int nchars;
6623 {
6624 if (nchars == 0)
6625 echo_area_buffer[0] = Qnil;
6626 /* A null message buffer means that the frame hasn't really been
6627 initialized yet. Error messages get reported properly by
6628 cmd_error, so this must be just an informative message; toss it. */
6629 else if (!noninteractive
6630 && INTERACTIVE
6631 && !NILP (echo_area_buffer[0]))
6632 {
6633 struct frame *sf = SELECTED_FRAME ();
6634 if (FRAME_MESSAGE_BUF (sf))
6635 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
6636 }
6637 }
6638
6639
6640 /* Helper function for truncate_echo_area. Truncate the current
6641 message to at most NCHARS characters. */
6642
6643 static int
6644 truncate_message_1 (nchars, a2, a3, a4)
6645 EMACS_INT nchars;
6646 Lisp_Object a2;
6647 EMACS_INT a3, a4;
6648 {
6649 if (BEG + nchars < Z)
6650 del_range (BEG + nchars, Z);
6651 if (Z == BEG)
6652 echo_area_buffer[0] = Qnil;
6653 return 0;
6654 }
6655
6656
6657 /* Set the current message to a substring of S or STRING.
6658
6659 If STRING is a Lisp string, set the message to the first NBYTES
6660 bytes from STRING. NBYTES zero means use the whole string. If
6661 STRING is multibyte, the message will be displayed multibyte.
6662
6663 If S is not null, set the message to the first LEN bytes of S. LEN
6664 zero means use the whole string. MULTIBYTE_P non-zero means S is
6665 multibyte. Display the message multibyte in that case. */
6666
6667 void
6668 set_message (s, string, nbytes, multibyte_p)
6669 char *s;
6670 Lisp_Object string;
6671 int nbytes;
6672 {
6673 message_enable_multibyte
6674 = ((s && multibyte_p)
6675 || (STRINGP (string) && STRING_MULTIBYTE (string)));
6676
6677 with_echo_area_buffer (0, -1, set_message_1,
6678 (EMACS_INT) s, string, nbytes, multibyte_p);
6679 message_buf_print = 0;
6680 help_echo_showing_p = 0;
6681 }
6682
6683
6684 /* Helper function for set_message. Arguments have the same meaning
6685 as there, with A1 corresponding to S and A2 corresponding to STRING
6686 This function is called with the echo area buffer being
6687 current. */
6688
6689 static int
6690 set_message_1 (a1, a2, nbytes, multibyte_p)
6691 EMACS_INT a1;
6692 Lisp_Object a2;
6693 EMACS_INT nbytes, multibyte_p;
6694 {
6695 char *s = (char *) a1;
6696 Lisp_Object string = a2;
6697
6698 xassert (BEG == Z);
6699
6700 /* Change multibyteness of the echo buffer appropriately. */
6701 if (message_enable_multibyte
6702 != !NILP (current_buffer->enable_multibyte_characters))
6703 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
6704
6705 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
6706
6707 /* Insert new message at BEG. */
6708 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6709
6710 if (STRINGP (string))
6711 {
6712 int nchars;
6713
6714 if (nbytes == 0)
6715 nbytes = XSTRING (string)->size_byte;
6716 nchars = string_byte_to_char (string, nbytes);
6717
6718 /* This function takes care of single/multibyte conversion. We
6719 just have to ensure that the echo area buffer has the right
6720 setting of enable_multibyte_characters. */
6721 insert_from_string (string, 0, 0, nchars, nbytes, 1);
6722 }
6723 else if (s)
6724 {
6725 if (nbytes == 0)
6726 nbytes = strlen (s);
6727
6728 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
6729 {
6730 /* Convert from multi-byte to single-byte. */
6731 int i, c, n;
6732 unsigned char work[1];
6733
6734 /* Convert a multibyte string to single-byte. */
6735 for (i = 0; i < nbytes; i += n)
6736 {
6737 c = string_char_and_length (s + i, nbytes - i, &n);
6738 work[0] = (SINGLE_BYTE_CHAR_P (c)
6739 ? c
6740 : multibyte_char_to_unibyte (c, Qnil));
6741 insert_1_both (work, 1, 1, 1, 0, 0);
6742 }
6743 }
6744 else if (!multibyte_p
6745 && !NILP (current_buffer->enable_multibyte_characters))
6746 {
6747 /* Convert from single-byte to multi-byte. */
6748 int i, c, n;
6749 unsigned char *msg = (unsigned char *) s;
6750 unsigned char str[MAX_MULTIBYTE_LENGTH];
6751
6752 /* Convert a single-byte string to multibyte. */
6753 for (i = 0; i < nbytes; i++)
6754 {
6755 c = unibyte_char_to_multibyte (msg[i]);
6756 n = CHAR_STRING (c, str);
6757 insert_1_both (str, 1, n, 1, 0, 0);
6758 }
6759 }
6760 else
6761 insert_1 (s, nbytes, 1, 0, 0);
6762 }
6763
6764 return 0;
6765 }
6766
6767
6768 /* Clear messages. CURRENT_P non-zero means clear the current
6769 message. LAST_DISPLAYED_P non-zero means clear the message
6770 last displayed. */
6771
6772 void
6773 clear_message (current_p, last_displayed_p)
6774 int current_p, last_displayed_p;
6775 {
6776 if (current_p)
6777 echo_area_buffer[0] = Qnil;
6778
6779 if (last_displayed_p)
6780 echo_area_buffer[1] = Qnil;
6781
6782 message_buf_print = 0;
6783 }
6784
6785 /* Clear garbaged frames.
6786
6787 This function is used where the old redisplay called
6788 redraw_garbaged_frames which in turn called redraw_frame which in
6789 turn called clear_frame. The call to clear_frame was a source of
6790 flickering. I believe a clear_frame is not necessary. It should
6791 suffice in the new redisplay to invalidate all current matrices,
6792 and ensure a complete redisplay of all windows. */
6793
6794 static void
6795 clear_garbaged_frames ()
6796 {
6797 if (frame_garbaged)
6798 {
6799 Lisp_Object tail, frame;
6800
6801 FOR_EACH_FRAME (tail, frame)
6802 {
6803 struct frame *f = XFRAME (frame);
6804
6805 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
6806 {
6807 clear_current_matrices (f);
6808 f->garbaged = 0;
6809 }
6810 }
6811
6812 frame_garbaged = 0;
6813 ++windows_or_buffers_changed;
6814 }
6815 }
6816
6817
6818 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
6819 is non-zero update selected_frame. Value is non-zero if the
6820 mini-windows height has been changed. */
6821
6822 static int
6823 echo_area_display (update_frame_p)
6824 int update_frame_p;
6825 {
6826 Lisp_Object mini_window;
6827 struct window *w;
6828 struct frame *f;
6829 int window_height_changed_p = 0;
6830 struct frame *sf = SELECTED_FRAME ();
6831
6832 mini_window = FRAME_MINIBUF_WINDOW (sf);
6833 w = XWINDOW (mini_window);
6834 f = XFRAME (WINDOW_FRAME (w));
6835
6836 /* Don't display if frame is invisible or not yet initialized. */
6837 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
6838 return 0;
6839
6840 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
6841 #ifndef macintosh
6842 #ifdef HAVE_WINDOW_SYSTEM
6843 /* When Emacs starts, selected_frame may be a visible terminal
6844 frame, even if we run under a window system. If we let this
6845 through, a message would be displayed on the terminal. */
6846 if (EQ (selected_frame, Vterminal_frame)
6847 && !NILP (Vwindow_system))
6848 return 0;
6849 #endif /* HAVE_WINDOW_SYSTEM */
6850 #endif
6851
6852 /* Redraw garbaged frames. */
6853 if (frame_garbaged)
6854 clear_garbaged_frames ();
6855
6856 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
6857 {
6858 echo_area_window = mini_window;
6859 window_height_changed_p = display_echo_area (w);
6860 w->must_be_updated_p = 1;
6861
6862 /* Update the display, unless called from redisplay_internal.
6863 Also don't update the screen during redisplay itself. The
6864 update will happen at the end of redisplay, and an update
6865 here could cause confusion. */
6866 if (update_frame_p && !redisplaying_p)
6867 {
6868 int n = 0;
6869
6870 /* If the display update has been interrupted by pending
6871 input, update mode lines in the frame. Due to the
6872 pending input, it might have been that redisplay hasn't
6873 been called, so that mode lines above the echo area are
6874 garbaged. This looks odd, so we prevent it here. */
6875 if (!display_completed)
6876 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
6877
6878 if (window_height_changed_p
6879 /* Don't do this if Emacs is shutting down. Redisplay
6880 needs to run hooks. */
6881 && !NILP (Vrun_hooks))
6882 {
6883 /* Must update other windows. Likewise as in other
6884 cases, don't let this update be interrupted by
6885 pending input. */
6886 int count = BINDING_STACK_SIZE ();
6887 specbind (Qredisplay_dont_pause, Qt);
6888 windows_or_buffers_changed = 1;
6889 redisplay_internal (0);
6890 unbind_to (count, Qnil);
6891 }
6892 else if (FRAME_WINDOW_P (f) && n == 0)
6893 {
6894 /* Window configuration is the same as before.
6895 Can do with a display update of the echo area,
6896 unless we displayed some mode lines. */
6897 update_single_window (w, 1);
6898 rif->flush_display (f);
6899 }
6900 else
6901 update_frame (f, 1, 1);
6902
6903 /* If cursor is in the echo area, make sure that the next
6904 redisplay displays the minibuffer, so that the cursor will
6905 be replaced with what the minibuffer wants. */
6906 if (cursor_in_echo_area)
6907 ++windows_or_buffers_changed;
6908 }
6909 }
6910 else if (!EQ (mini_window, selected_window))
6911 windows_or_buffers_changed++;
6912
6913 /* Last displayed message is now the current message. */
6914 echo_area_buffer[1] = echo_area_buffer[0];
6915
6916 /* Prevent redisplay optimization in redisplay_internal by resetting
6917 this_line_start_pos. This is done because the mini-buffer now
6918 displays the message instead of its buffer text. */
6919 if (EQ (mini_window, selected_window))
6920 CHARPOS (this_line_start_pos) = 0;
6921
6922 return window_height_changed_p;
6923 }
6924
6925
6926 \f
6927 /***********************************************************************
6928 Frame Titles
6929 ***********************************************************************/
6930
6931
6932 #ifdef HAVE_WINDOW_SYSTEM
6933
6934 /* A buffer for constructing frame titles in it; allocated from the
6935 heap in init_xdisp and resized as needed in store_frame_title_char. */
6936
6937 static char *frame_title_buf;
6938
6939 /* The buffer's end, and a current output position in it. */
6940
6941 static char *frame_title_buf_end;
6942 static char *frame_title_ptr;
6943
6944
6945 /* Store a single character C for the frame title in frame_title_buf.
6946 Re-allocate frame_title_buf if necessary. */
6947
6948 static void
6949 store_frame_title_char (c)
6950 char c;
6951 {
6952 /* If output position has reached the end of the allocated buffer,
6953 double the buffer's size. */
6954 if (frame_title_ptr == frame_title_buf_end)
6955 {
6956 int len = frame_title_ptr - frame_title_buf;
6957 int new_size = 2 * len * sizeof *frame_title_buf;
6958 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
6959 frame_title_buf_end = frame_title_buf + new_size;
6960 frame_title_ptr = frame_title_buf + len;
6961 }
6962
6963 *frame_title_ptr++ = c;
6964 }
6965
6966
6967 /* Store part of a frame title in frame_title_buf, beginning at
6968 frame_title_ptr. STR is the string to store. Do not copy
6969 characters that yield more columns than PRECISION; PRECISION <= 0
6970 means copy the whole string. Pad with spaces until FIELD_WIDTH
6971 number of characters have been copied; FIELD_WIDTH <= 0 means don't
6972 pad. Called from display_mode_element when it is used to build a
6973 frame title. */
6974
6975 static int
6976 store_frame_title (str, field_width, precision)
6977 unsigned char *str;
6978 int field_width, precision;
6979 {
6980 int n = 0;
6981 int dummy, nbytes, width;
6982
6983 /* Copy at most PRECISION chars from STR. */
6984 nbytes = strlen (str);
6985 n+= c_string_width (str, nbytes, precision, &dummy, &nbytes);
6986 while (nbytes--)
6987 store_frame_title_char (*str++);
6988
6989 /* Fill up with spaces until FIELD_WIDTH reached. */
6990 while (field_width > 0
6991 && n < field_width)
6992 {
6993 store_frame_title_char (' ');
6994 ++n;
6995 }
6996
6997 return n;
6998 }
6999
7000
7001 /* Set the title of FRAME, if it has changed. The title format is
7002 Vicon_title_format if FRAME is iconified, otherwise it is
7003 frame_title_format. */
7004
7005 static void
7006 x_consider_frame_title (frame)
7007 Lisp_Object frame;
7008 {
7009 struct frame *f = XFRAME (frame);
7010
7011 if (FRAME_WINDOW_P (f)
7012 || FRAME_MINIBUF_ONLY_P (f)
7013 || f->explicit_name)
7014 {
7015 /* Do we have more than one visible frame on this X display? */
7016 Lisp_Object tail;
7017 Lisp_Object fmt;
7018 struct buffer *obuf;
7019 int len;
7020 struct it it;
7021
7022 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
7023 {
7024 struct frame *tf = XFRAME (XCAR (tail));
7025
7026 if (tf != f
7027 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
7028 && !FRAME_MINIBUF_ONLY_P (tf)
7029 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
7030 break;
7031 }
7032
7033 /* Set global variable indicating that multiple frames exist. */
7034 multiple_frames = CONSP (tail);
7035
7036 /* Switch to the buffer of selected window of the frame. Set up
7037 frame_title_ptr so that display_mode_element will output into it;
7038 then display the title. */
7039 obuf = current_buffer;
7040 Fset_buffer (XWINDOW (f->selected_window)->buffer);
7041 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
7042 frame_title_ptr = frame_title_buf;
7043 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
7044 NULL, DEFAULT_FACE_ID);
7045 display_mode_element (&it, 0, -1, -1, fmt);
7046 len = frame_title_ptr - frame_title_buf;
7047 frame_title_ptr = NULL;
7048 set_buffer_internal (obuf);
7049
7050 /* Set the title only if it's changed. This avoids consing in
7051 the common case where it hasn't. (If it turns out that we've
7052 already wasted too much time by walking through the list with
7053 display_mode_element, then we might need to optimize at a
7054 higher level than this.) */
7055 if (! STRINGP (f->name)
7056 || STRING_BYTES (XSTRING (f->name)) != len
7057 || bcmp (frame_title_buf, XSTRING (f->name)->data, len) != 0)
7058 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
7059 }
7060 }
7061
7062 #else /* not HAVE_WINDOW_SYSTEM */
7063
7064 #define frame_title_ptr ((char *)0)
7065 #define store_frame_title(str, mincol, maxcol) 0
7066
7067 #endif /* not HAVE_WINDOW_SYSTEM */
7068
7069
7070
7071 \f
7072 /***********************************************************************
7073 Menu Bars
7074 ***********************************************************************/
7075
7076
7077 /* Prepare for redisplay by updating menu-bar item lists when
7078 appropriate. This can call eval. */
7079
7080 void
7081 prepare_menu_bars ()
7082 {
7083 int all_windows;
7084 struct gcpro gcpro1, gcpro2;
7085 struct frame *f;
7086 Lisp_Object tooltip_frame;
7087
7088 #ifdef HAVE_X_WINDOWS
7089 tooltip_frame = tip_frame;
7090 #else
7091 tooltip_frame = Qnil;
7092 #endif
7093
7094 /* Update all frame titles based on their buffer names, etc. We do
7095 this before the menu bars so that the buffer-menu will show the
7096 up-to-date frame titles. */
7097 #ifdef HAVE_WINDOW_SYSTEM
7098 if (windows_or_buffers_changed || update_mode_lines)
7099 {
7100 Lisp_Object tail, frame;
7101
7102 FOR_EACH_FRAME (tail, frame)
7103 {
7104 f = XFRAME (frame);
7105 if (!EQ (frame, tooltip_frame)
7106 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
7107 x_consider_frame_title (frame);
7108 }
7109 }
7110 #endif /* HAVE_WINDOW_SYSTEM */
7111
7112 /* Update the menu bar item lists, if appropriate. This has to be
7113 done before any actual redisplay or generation of display lines. */
7114 all_windows = (update_mode_lines
7115 || buffer_shared > 1
7116 || windows_or_buffers_changed);
7117 if (all_windows)
7118 {
7119 Lisp_Object tail, frame;
7120 int count = BINDING_STACK_SIZE ();
7121
7122 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7123
7124 FOR_EACH_FRAME (tail, frame)
7125 {
7126 f = XFRAME (frame);
7127
7128 /* Ignore tooltip frame. */
7129 if (EQ (frame, tooltip_frame))
7130 continue;
7131
7132 /* If a window on this frame changed size, report that to
7133 the user and clear the size-change flag. */
7134 if (FRAME_WINDOW_SIZES_CHANGED (f))
7135 {
7136 Lisp_Object functions;
7137
7138 /* Clear flag first in case we get an error below. */
7139 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
7140 functions = Vwindow_size_change_functions;
7141 GCPRO2 (tail, functions);
7142
7143 while (CONSP (functions))
7144 {
7145 call1 (XCAR (functions), frame);
7146 functions = XCDR (functions);
7147 }
7148 UNGCPRO;
7149 }
7150
7151 GCPRO1 (tail);
7152 update_menu_bar (f, 0);
7153 #ifdef HAVE_WINDOW_SYSTEM
7154 update_tool_bar (f, 0);
7155 #endif
7156 UNGCPRO;
7157 }
7158
7159 unbind_to (count, Qnil);
7160 }
7161 else
7162 {
7163 struct frame *sf = SELECTED_FRAME ();
7164 update_menu_bar (sf, 1);
7165 #ifdef HAVE_WINDOW_SYSTEM
7166 update_tool_bar (sf, 1);
7167 #endif
7168 }
7169
7170 /* Motif needs this. See comment in xmenu.c. Turn it off when
7171 pending_menu_activation is not defined. */
7172 #ifdef USE_X_TOOLKIT
7173 pending_menu_activation = 0;
7174 #endif
7175 }
7176
7177
7178 /* Update the menu bar item list for frame F. This has to be done
7179 before we start to fill in any display lines, because it can call
7180 eval.
7181
7182 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
7183
7184 static void
7185 update_menu_bar (f, save_match_data)
7186 struct frame *f;
7187 int save_match_data;
7188 {
7189 Lisp_Object window;
7190 register struct window *w;
7191
7192 /* If called recursively during a menu update, do nothing. This can
7193 happen when, for instance, an activate-menubar-hook causes a
7194 redisplay. */
7195 if (inhibit_menubar_update)
7196 return;
7197
7198 window = FRAME_SELECTED_WINDOW (f);
7199 w = XWINDOW (window);
7200
7201 if (update_mode_lines)
7202 w->update_mode_line = Qt;
7203
7204 if (FRAME_WINDOW_P (f)
7205 ?
7206 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
7207 FRAME_EXTERNAL_MENU_BAR (f)
7208 #else
7209 FRAME_MENU_BAR_LINES (f) > 0
7210 #endif
7211 : FRAME_MENU_BAR_LINES (f) > 0)
7212 {
7213 /* If the user has switched buffers or windows, we need to
7214 recompute to reflect the new bindings. But we'll
7215 recompute when update_mode_lines is set too; that means
7216 that people can use force-mode-line-update to request
7217 that the menu bar be recomputed. The adverse effect on
7218 the rest of the redisplay algorithm is about the same as
7219 windows_or_buffers_changed anyway. */
7220 if (windows_or_buffers_changed
7221 || !NILP (w->update_mode_line)
7222 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7223 < BUF_MODIFF (XBUFFER (w->buffer)))
7224 != !NILP (w->last_had_star))
7225 || ((!NILP (Vtransient_mark_mode)
7226 && !NILP (XBUFFER (w->buffer)->mark_active))
7227 != !NILP (w->region_showing)))
7228 {
7229 struct buffer *prev = current_buffer;
7230 int count = BINDING_STACK_SIZE ();
7231
7232 specbind (Qinhibit_menubar_update, Qt);
7233
7234 set_buffer_internal_1 (XBUFFER (w->buffer));
7235 if (save_match_data)
7236 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7237 if (NILP (Voverriding_local_map_menu_flag))
7238 {
7239 specbind (Qoverriding_terminal_local_map, Qnil);
7240 specbind (Qoverriding_local_map, Qnil);
7241 }
7242
7243 /* Run the Lucid hook. */
7244 safe_run_hooks (Qactivate_menubar_hook);
7245
7246 /* If it has changed current-menubar from previous value,
7247 really recompute the menu-bar from the value. */
7248 if (! NILP (Vlucid_menu_bar_dirty_flag))
7249 call0 (Qrecompute_lucid_menubar);
7250
7251 safe_run_hooks (Qmenu_bar_update_hook);
7252 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
7253
7254 /* Redisplay the menu bar in case we changed it. */
7255 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
7256 if (FRAME_WINDOW_P (f)
7257 #if defined (macintosh)
7258 /* All frames on Mac OS share the same menubar. So only the
7259 selected frame should be allowed to set it. */
7260 && f == SELECTED_FRAME ()
7261 #endif
7262 )
7263 set_frame_menubar (f, 0, 0);
7264 else
7265 /* On a terminal screen, the menu bar is an ordinary screen
7266 line, and this makes it get updated. */
7267 w->update_mode_line = Qt;
7268 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7269 /* In the non-toolkit version, the menu bar is an ordinary screen
7270 line, and this makes it get updated. */
7271 w->update_mode_line = Qt;
7272 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7273
7274 unbind_to (count, Qnil);
7275 set_buffer_internal_1 (prev);
7276 }
7277 }
7278 }
7279
7280
7281 \f
7282 /***********************************************************************
7283 Tool-bars
7284 ***********************************************************************/
7285
7286 #ifdef HAVE_WINDOW_SYSTEM
7287
7288 /* Update the tool-bar item list for frame F. This has to be done
7289 before we start to fill in any display lines. Called from
7290 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
7291 and restore it here. */
7292
7293 static void
7294 update_tool_bar (f, save_match_data)
7295 struct frame *f;
7296 int save_match_data;
7297 {
7298 if (WINDOWP (f->tool_bar_window)
7299 && XFASTINT (XWINDOW (f->tool_bar_window)->height) > 0)
7300 {
7301 Lisp_Object window;
7302 struct window *w;
7303
7304 window = FRAME_SELECTED_WINDOW (f);
7305 w = XWINDOW (window);
7306
7307 /* If the user has switched buffers or windows, we need to
7308 recompute to reflect the new bindings. But we'll
7309 recompute when update_mode_lines is set too; that means
7310 that people can use force-mode-line-update to request
7311 that the menu bar be recomputed. The adverse effect on
7312 the rest of the redisplay algorithm is about the same as
7313 windows_or_buffers_changed anyway. */
7314 if (windows_or_buffers_changed
7315 || !NILP (w->update_mode_line)
7316 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7317 < BUF_MODIFF (XBUFFER (w->buffer)))
7318 != !NILP (w->last_had_star))
7319 || ((!NILP (Vtransient_mark_mode)
7320 && !NILP (XBUFFER (w->buffer)->mark_active))
7321 != !NILP (w->region_showing)))
7322 {
7323 struct buffer *prev = current_buffer;
7324 int count = BINDING_STACK_SIZE ();
7325
7326 /* Set current_buffer to the buffer of the selected
7327 window of the frame, so that we get the right local
7328 keymaps. */
7329 set_buffer_internal_1 (XBUFFER (w->buffer));
7330
7331 /* Save match data, if we must. */
7332 if (save_match_data)
7333 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7334
7335 /* Make sure that we don't accidentally use bogus keymaps. */
7336 if (NILP (Voverriding_local_map_menu_flag))
7337 {
7338 specbind (Qoverriding_terminal_local_map, Qnil);
7339 specbind (Qoverriding_local_map, Qnil);
7340 }
7341
7342 /* Build desired tool-bar items from keymaps. */
7343 f->tool_bar_items
7344 = tool_bar_items (f->tool_bar_items, &f->n_tool_bar_items);
7345
7346 /* Redisplay the tool-bar in case we changed it. */
7347 w->update_mode_line = Qt;
7348
7349 unbind_to (count, Qnil);
7350 set_buffer_internal_1 (prev);
7351 }
7352 }
7353 }
7354
7355
7356 /* Set F->desired_tool_bar_string to a Lisp string representing frame
7357 F's desired tool-bar contents. F->tool_bar_items must have
7358 been set up previously by calling prepare_menu_bars. */
7359
7360 static void
7361 build_desired_tool_bar_string (f)
7362 struct frame *f;
7363 {
7364 int i, size, size_needed;
7365 struct gcpro gcpro1, gcpro2, gcpro3;
7366 Lisp_Object image, plist, props;
7367
7368 image = plist = props = Qnil;
7369 GCPRO3 (image, plist, props);
7370
7371 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
7372 Otherwise, make a new string. */
7373
7374 /* The size of the string we might be able to reuse. */
7375 size = (STRINGP (f->desired_tool_bar_string)
7376 ? XSTRING (f->desired_tool_bar_string)->size
7377 : 0);
7378
7379 /* We need one space in the string for each image. */
7380 size_needed = f->n_tool_bar_items;
7381
7382 /* Reuse f->desired_tool_bar_string, if possible. */
7383 if (size < size_needed || NILP (f->desired_tool_bar_string))
7384 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
7385 make_number (' '));
7386 else
7387 {
7388 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
7389 Fremove_text_properties (make_number (0), make_number (size),
7390 props, f->desired_tool_bar_string);
7391 }
7392
7393 /* Put a `display' property on the string for the images to display,
7394 put a `menu_item' property on tool-bar items with a value that
7395 is the index of the item in F's tool-bar item vector. */
7396 for (i = 0; i < f->n_tool_bar_items; ++i)
7397 {
7398 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
7399
7400 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
7401 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
7402 int hmargin, vmargin, relief, idx, end;
7403 extern Lisp_Object QCrelief, QCmargin, QCconversion, Qimage;
7404 extern Lisp_Object Qlaplace;
7405
7406 /* If image is a vector, choose the image according to the
7407 button state. */
7408 image = PROP (TOOL_BAR_ITEM_IMAGES);
7409 if (VECTORP (image))
7410 {
7411 if (enabled_p)
7412 idx = (selected_p
7413 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
7414 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
7415 else
7416 idx = (selected_p
7417 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
7418 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
7419
7420 xassert (ASIZE (image) >= idx);
7421 image = AREF (image, idx);
7422 }
7423 else
7424 idx = -1;
7425
7426 /* Ignore invalid image specifications. */
7427 if (!valid_image_p (image))
7428 continue;
7429
7430 /* Display the tool-bar button pressed, or depressed. */
7431 plist = Fcopy_sequence (XCDR (image));
7432
7433 /* Compute margin and relief to draw. */
7434 relief = (tool_bar_button_relief > 0
7435 ? tool_bar_button_relief
7436 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
7437 hmargin = vmargin = relief;
7438
7439 if (INTEGERP (Vtool_bar_button_margin)
7440 && XINT (Vtool_bar_button_margin) > 0)
7441 {
7442 hmargin += XFASTINT (Vtool_bar_button_margin);
7443 vmargin += XFASTINT (Vtool_bar_button_margin);
7444 }
7445 else if (CONSP (Vtool_bar_button_margin))
7446 {
7447 if (INTEGERP (XCAR (Vtool_bar_button_margin))
7448 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
7449 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
7450
7451 if (INTEGERP (XCDR (Vtool_bar_button_margin))
7452 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
7453 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
7454 }
7455
7456 if (auto_raise_tool_bar_buttons_p)
7457 {
7458 /* Add a `:relief' property to the image spec if the item is
7459 selected. */
7460 if (selected_p)
7461 {
7462 plist = Fplist_put (plist, QCrelief, make_number (-relief));
7463 hmargin -= relief;
7464 vmargin -= relief;
7465 }
7466 }
7467 else
7468 {
7469 /* If image is selected, display it pressed, i.e. with a
7470 negative relief. If it's not selected, display it with a
7471 raised relief. */
7472 plist = Fplist_put (plist, QCrelief,
7473 (selected_p
7474 ? make_number (-relief)
7475 : make_number (relief)));
7476 hmargin -= relief;
7477 vmargin -= relief;
7478 }
7479
7480 /* Put a margin around the image. */
7481 if (hmargin || vmargin)
7482 {
7483 if (hmargin == vmargin)
7484 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
7485 else
7486 plist = Fplist_put (plist, QCmargin,
7487 Fcons (make_number (hmargin),
7488 make_number (vmargin)));
7489 }
7490
7491 /* If button is not enabled, and we don't have special images
7492 for the disabled state, make the image appear disabled by
7493 applying an appropriate algorithm to it. */
7494 if (!enabled_p && idx < 0)
7495 plist = Fplist_put (plist, QCconversion, Qdisabled);
7496
7497 /* Put a `display' text property on the string for the image to
7498 display. Put a `menu-item' property on the string that gives
7499 the start of this item's properties in the tool-bar items
7500 vector. */
7501 image = Fcons (Qimage, plist);
7502 props = list4 (Qdisplay, image,
7503 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
7504
7505 /* Let the last image hide all remaining spaces in the tool bar
7506 string. The string can be longer than needed when we reuse a
7507 previous string. */
7508 if (i + 1 == f->n_tool_bar_items)
7509 end = XSTRING (f->desired_tool_bar_string)->size;
7510 else
7511 end = i + 1;
7512 Fadd_text_properties (make_number (i), make_number (end),
7513 props, f->desired_tool_bar_string);
7514 #undef PROP
7515 }
7516
7517 UNGCPRO;
7518 }
7519
7520
7521 /* Display one line of the tool-bar of frame IT->f. */
7522
7523 static void
7524 display_tool_bar_line (it)
7525 struct it *it;
7526 {
7527 struct glyph_row *row = it->glyph_row;
7528 int max_x = it->last_visible_x;
7529 struct glyph *last;
7530
7531 prepare_desired_row (row);
7532 row->y = it->current_y;
7533
7534 /* Note that this isn't made use of if the face hasn't a box,
7535 so there's no need to check the face here. */
7536 it->start_of_box_run_p = 1;
7537
7538 while (it->current_x < max_x)
7539 {
7540 int x_before, x, n_glyphs_before, i, nglyphs;
7541
7542 /* Get the next display element. */
7543 if (!get_next_display_element (it))
7544 break;
7545
7546 /* Produce glyphs. */
7547 x_before = it->current_x;
7548 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
7549 PRODUCE_GLYPHS (it);
7550
7551 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
7552 i = 0;
7553 x = x_before;
7554 while (i < nglyphs)
7555 {
7556 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
7557
7558 if (x + glyph->pixel_width > max_x)
7559 {
7560 /* Glyph doesn't fit on line. */
7561 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
7562 it->current_x = x;
7563 goto out;
7564 }
7565
7566 ++it->hpos;
7567 x += glyph->pixel_width;
7568 ++i;
7569 }
7570
7571 /* Stop at line ends. */
7572 if (ITERATOR_AT_END_OF_LINE_P (it))
7573 break;
7574
7575 set_iterator_to_next (it, 1);
7576 }
7577
7578 out:;
7579
7580 row->displays_text_p = row->used[TEXT_AREA] != 0;
7581 extend_face_to_end_of_line (it);
7582 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
7583 last->right_box_line_p = 1;
7584 if (last == row->glyphs[TEXT_AREA])
7585 last->left_box_line_p = 1;
7586 compute_line_metrics (it);
7587
7588 /* If line is empty, make it occupy the rest of the tool-bar. */
7589 if (!row->displays_text_p)
7590 {
7591 row->height = row->phys_height = it->last_visible_y - row->y;
7592 row->ascent = row->phys_ascent = 0;
7593 }
7594
7595 row->full_width_p = 1;
7596 row->continued_p = 0;
7597 row->truncated_on_left_p = 0;
7598 row->truncated_on_right_p = 0;
7599
7600 it->current_x = it->hpos = 0;
7601 it->current_y += row->height;
7602 ++it->vpos;
7603 ++it->glyph_row;
7604 }
7605
7606
7607 /* Value is the number of screen lines needed to make all tool-bar
7608 items of frame F visible. */
7609
7610 static int
7611 tool_bar_lines_needed (f)
7612 struct frame *f;
7613 {
7614 struct window *w = XWINDOW (f->tool_bar_window);
7615 struct it it;
7616
7617 /* Initialize an iterator for iteration over
7618 F->desired_tool_bar_string in the tool-bar window of frame F. */
7619 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7620 it.first_visible_x = 0;
7621 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7622 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7623
7624 while (!ITERATOR_AT_END_P (&it))
7625 {
7626 it.glyph_row = w->desired_matrix->rows;
7627 clear_glyph_row (it.glyph_row);
7628 display_tool_bar_line (&it);
7629 }
7630
7631 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
7632 }
7633
7634
7635 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
7636 0, 1, 0,
7637 "Return the number of lines occupied by the tool bar of FRAME.")
7638 (frame)
7639 Lisp_Object frame;
7640 {
7641 struct frame *f;
7642 struct window *w;
7643 int nlines = 0;
7644
7645 if (NILP (frame))
7646 frame = selected_frame;
7647 else
7648 CHECK_FRAME (frame, 0);
7649 f = XFRAME (frame);
7650
7651 if (WINDOWP (f->tool_bar_window)
7652 || (w = XWINDOW (f->tool_bar_window),
7653 XFASTINT (w->height) > 0))
7654 {
7655 update_tool_bar (f, 1);
7656 if (f->n_tool_bar_items)
7657 {
7658 build_desired_tool_bar_string (f);
7659 nlines = tool_bar_lines_needed (f);
7660 }
7661 }
7662
7663 return make_number (nlines);
7664 }
7665
7666
7667 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
7668 height should be changed. */
7669
7670 static int
7671 redisplay_tool_bar (f)
7672 struct frame *f;
7673 {
7674 struct window *w;
7675 struct it it;
7676 struct glyph_row *row;
7677 int change_height_p = 0;
7678
7679 /* If frame hasn't a tool-bar window or if it is zero-height, don't
7680 do anything. This means you must start with tool-bar-lines
7681 non-zero to get the auto-sizing effect. Or in other words, you
7682 can turn off tool-bars by specifying tool-bar-lines zero. */
7683 if (!WINDOWP (f->tool_bar_window)
7684 || (w = XWINDOW (f->tool_bar_window),
7685 XFASTINT (w->height) == 0))
7686 return 0;
7687
7688 /* Set up an iterator for the tool-bar window. */
7689 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7690 it.first_visible_x = 0;
7691 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7692 row = it.glyph_row;
7693
7694 /* Build a string that represents the contents of the tool-bar. */
7695 build_desired_tool_bar_string (f);
7696 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7697
7698 /* Display as many lines as needed to display all tool-bar items. */
7699 while (it.current_y < it.last_visible_y)
7700 display_tool_bar_line (&it);
7701
7702 /* It doesn't make much sense to try scrolling in the tool-bar
7703 window, so don't do it. */
7704 w->desired_matrix->no_scrolling_p = 1;
7705 w->must_be_updated_p = 1;
7706
7707 if (auto_resize_tool_bars_p)
7708 {
7709 int nlines;
7710
7711 /* If we couldn't display everything, change the tool-bar's
7712 height. */
7713 if (IT_STRING_CHARPOS (it) < it.end_charpos)
7714 change_height_p = 1;
7715
7716 /* If there are blank lines at the end, except for a partially
7717 visible blank line at the end that is smaller than
7718 CANON_Y_UNIT, change the tool-bar's height. */
7719 row = it.glyph_row - 1;
7720 if (!row->displays_text_p
7721 && row->height >= CANON_Y_UNIT (f))
7722 change_height_p = 1;
7723
7724 /* If row displays tool-bar items, but is partially visible,
7725 change the tool-bar's height. */
7726 if (row->displays_text_p
7727 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
7728 change_height_p = 1;
7729
7730 /* Resize windows as needed by changing the `tool-bar-lines'
7731 frame parameter. */
7732 if (change_height_p
7733 && (nlines = tool_bar_lines_needed (f),
7734 nlines != XFASTINT (w->height)))
7735 {
7736 extern Lisp_Object Qtool_bar_lines;
7737 Lisp_Object frame;
7738 int old_height = XFASTINT (w->height);
7739
7740 XSETFRAME (frame, f);
7741 clear_glyph_matrix (w->desired_matrix);
7742 Fmodify_frame_parameters (frame,
7743 Fcons (Fcons (Qtool_bar_lines,
7744 make_number (nlines)),
7745 Qnil));
7746 if (XFASTINT (w->height) != old_height)
7747 fonts_changed_p = 1;
7748 }
7749 }
7750
7751 return change_height_p;
7752 }
7753
7754
7755 /* Get information about the tool-bar item which is displayed in GLYPH
7756 on frame F. Return in *PROP_IDX the index where tool-bar item
7757 properties start in F->tool_bar_items. Value is zero if
7758 GLYPH doesn't display a tool-bar item. */
7759
7760 int
7761 tool_bar_item_info (f, glyph, prop_idx)
7762 struct frame *f;
7763 struct glyph *glyph;
7764 int *prop_idx;
7765 {
7766 Lisp_Object prop;
7767 int success_p;
7768
7769 /* Get the text property `menu-item' at pos. The value of that
7770 property is the start index of this item's properties in
7771 F->tool_bar_items. */
7772 prop = Fget_text_property (make_number (glyph->charpos),
7773 Qmenu_item, f->current_tool_bar_string);
7774 if (INTEGERP (prop))
7775 {
7776 *prop_idx = XINT (prop);
7777 success_p = 1;
7778 }
7779 else
7780 success_p = 0;
7781
7782 return success_p;
7783 }
7784
7785 #endif /* HAVE_WINDOW_SYSTEM */
7786
7787
7788 \f
7789 /************************************************************************
7790 Horizontal scrolling
7791 ************************************************************************/
7792
7793 static int hscroll_window_tree P_ ((Lisp_Object));
7794 static int hscroll_windows P_ ((Lisp_Object));
7795
7796 /* For all leaf windows in the window tree rooted at WINDOW, set their
7797 hscroll value so that PT is (i) visible in the window, and (ii) so
7798 that it is not within a certain margin at the window's left and
7799 right border. Value is non-zero if any window's hscroll has been
7800 changed. */
7801
7802 static int
7803 hscroll_window_tree (window)
7804 Lisp_Object window;
7805 {
7806 int hscrolled_p = 0;
7807
7808 while (WINDOWP (window))
7809 {
7810 struct window *w = XWINDOW (window);
7811
7812 if (WINDOWP (w->hchild))
7813 hscrolled_p |= hscroll_window_tree (w->hchild);
7814 else if (WINDOWP (w->vchild))
7815 hscrolled_p |= hscroll_window_tree (w->vchild);
7816 else if (w->cursor.vpos >= 0)
7817 {
7818 int hscroll_margin, text_area_x, text_area_y;
7819 int text_area_width, text_area_height;
7820 struct glyph_row *current_cursor_row
7821 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
7822 struct glyph_row *desired_cursor_row
7823 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
7824 struct glyph_row *cursor_row
7825 = (desired_cursor_row->enabled_p
7826 ? desired_cursor_row
7827 : current_cursor_row);
7828
7829 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
7830 &text_area_width, &text_area_height);
7831
7832 /* Scroll when cursor is inside this scroll margin. */
7833 hscroll_margin = 5 * CANON_X_UNIT (XFRAME (w->frame));
7834
7835 if ((XFASTINT (w->hscroll)
7836 && w->cursor.x < hscroll_margin)
7837 || (cursor_row->enabled_p
7838 && cursor_row->truncated_on_right_p
7839 && (w->cursor.x > text_area_width - hscroll_margin)))
7840 {
7841 struct it it;
7842 int hscroll;
7843 struct buffer *saved_current_buffer;
7844 int pt;
7845
7846 /* Find point in a display of infinite width. */
7847 saved_current_buffer = current_buffer;
7848 current_buffer = XBUFFER (w->buffer);
7849
7850 if (w == XWINDOW (selected_window))
7851 pt = BUF_PT (current_buffer);
7852 else
7853 {
7854 pt = marker_position (w->pointm);
7855 pt = max (BEGV, pt);
7856 pt = min (ZV, pt);
7857 }
7858
7859 /* Move iterator to pt starting at cursor_row->start in
7860 a line with infinite width. */
7861 init_to_row_start (&it, w, cursor_row);
7862 it.last_visible_x = INFINITY;
7863 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
7864 current_buffer = saved_current_buffer;
7865
7866 /* Center cursor in window. */
7867 hscroll = (max (0, it.current_x - text_area_width / 2)
7868 / CANON_X_UNIT (it.f));
7869 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
7870
7871 /* Don't call Fset_window_hscroll if value hasn't
7872 changed because it will prevent redisplay
7873 optimizations. */
7874 if (XFASTINT (w->hscroll) != hscroll)
7875 {
7876 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
7877 w->hscroll = make_number (hscroll);
7878 hscrolled_p = 1;
7879 }
7880 }
7881 }
7882
7883 window = w->next;
7884 }
7885
7886 /* Value is non-zero if hscroll of any leaf window has been changed. */
7887 return hscrolled_p;
7888 }
7889
7890
7891 /* Set hscroll so that cursor is visible and not inside horizontal
7892 scroll margins for all windows in the tree rooted at WINDOW. See
7893 also hscroll_window_tree above. Value is non-zero if any window's
7894 hscroll has been changed. If it has, desired matrices on the frame
7895 of WINDOW are cleared. */
7896
7897 static int
7898 hscroll_windows (window)
7899 Lisp_Object window;
7900 {
7901 int hscrolled_p;
7902
7903 if (automatic_hscrolling_p)
7904 {
7905 hscrolled_p = hscroll_window_tree (window);
7906 if (hscrolled_p)
7907 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
7908 }
7909 else
7910 hscrolled_p = 0;
7911 return hscrolled_p;
7912 }
7913
7914
7915 \f
7916 /************************************************************************
7917 Redisplay
7918 ************************************************************************/
7919
7920 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
7921 to a non-zero value. This is sometimes handy to have in a debugger
7922 session. */
7923
7924 #if GLYPH_DEBUG
7925
7926 /* First and last unchanged row for try_window_id. */
7927
7928 int debug_first_unchanged_at_end_vpos;
7929 int debug_last_unchanged_at_beg_vpos;
7930
7931 /* Delta vpos and y. */
7932
7933 int debug_dvpos, debug_dy;
7934
7935 /* Delta in characters and bytes for try_window_id. */
7936
7937 int debug_delta, debug_delta_bytes;
7938
7939 /* Values of window_end_pos and window_end_vpos at the end of
7940 try_window_id. */
7941
7942 int debug_end_pos, debug_end_vpos;
7943
7944 /* Append a string to W->desired_matrix->method. FMT is a printf
7945 format string. A1...A9 are a supplement for a variable-length
7946 argument list. If trace_redisplay_p is non-zero also printf the
7947 resulting string to stderr. */
7948
7949 static void
7950 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
7951 struct window *w;
7952 char *fmt;
7953 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
7954 {
7955 char buffer[512];
7956 char *method = w->desired_matrix->method;
7957 int len = strlen (method);
7958 int size = sizeof w->desired_matrix->method;
7959 int remaining = size - len - 1;
7960
7961 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
7962 if (len && remaining)
7963 {
7964 method[len] = '|';
7965 --remaining, ++len;
7966 }
7967
7968 strncpy (method + len, buffer, remaining);
7969
7970 if (trace_redisplay_p)
7971 fprintf (stderr, "%p (%s): %s\n",
7972 w,
7973 ((BUFFERP (w->buffer)
7974 && STRINGP (XBUFFER (w->buffer)->name))
7975 ? (char *) XSTRING (XBUFFER (w->buffer)->name)->data
7976 : "no buffer"),
7977 buffer);
7978 }
7979
7980 #endif /* GLYPH_DEBUG */
7981
7982
7983 /* This counter is used to clear the face cache every once in a while
7984 in redisplay_internal. It is incremented for each redisplay.
7985 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
7986 cleared. */
7987
7988 #define CLEAR_FACE_CACHE_COUNT 10000
7989 static int clear_face_cache_count;
7990
7991 /* Record the previous terminal frame we displayed. */
7992
7993 static struct frame *previous_terminal_frame;
7994
7995 /* Non-zero while redisplay_internal is in progress. */
7996
7997 int redisplaying_p;
7998
7999
8000 /* Value is non-zero if all changes in window W, which displays
8001 current_buffer, are in the text between START and END. START is a
8002 buffer position, END is given as a distance from Z. Used in
8003 redisplay_internal for display optimization. */
8004
8005 static INLINE int
8006 text_outside_line_unchanged_p (w, start, end)
8007 struct window *w;
8008 int start, end;
8009 {
8010 int unchanged_p = 1;
8011
8012 /* If text or overlays have changed, see where. */
8013 if (XFASTINT (w->last_modified) < MODIFF
8014 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8015 {
8016 /* Gap in the line? */
8017 if (GPT < start || Z - GPT < end)
8018 unchanged_p = 0;
8019
8020 /* Changes start in front of the line, or end after it? */
8021 if (unchanged_p
8022 && (BEG_UNCHANGED < start - 1
8023 || END_UNCHANGED < end))
8024 unchanged_p = 0;
8025
8026 /* If selective display, can't optimize if changes start at the
8027 beginning of the line. */
8028 if (unchanged_p
8029 && INTEGERP (current_buffer->selective_display)
8030 && XINT (current_buffer->selective_display) > 0
8031 && (BEG_UNCHANGED < start || GPT <= start))
8032 unchanged_p = 0;
8033 }
8034
8035 return unchanged_p;
8036 }
8037
8038
8039 /* Do a frame update, taking possible shortcuts into account. This is
8040 the main external entry point for redisplay.
8041
8042 If the last redisplay displayed an echo area message and that message
8043 is no longer requested, we clear the echo area or bring back the
8044 mini-buffer if that is in use. */
8045
8046 void
8047 redisplay ()
8048 {
8049 redisplay_internal (0);
8050 }
8051
8052 /* Return 1 if point moved out of or into a composition. Otherwise
8053 return 0. PREV_BUF and PREV_PT are the last point buffer and
8054 position. BUF and PT are the current point buffer and position. */
8055
8056 int
8057 check_point_in_composition (prev_buf, prev_pt, buf, pt)
8058 struct buffer *prev_buf, *buf;
8059 int prev_pt, pt;
8060 {
8061 int start, end;
8062 Lisp_Object prop;
8063 Lisp_Object buffer;
8064
8065 XSETBUFFER (buffer, buf);
8066 /* Check a composition at the last point if point moved within the
8067 same buffer. */
8068 if (prev_buf == buf)
8069 {
8070 if (prev_pt == pt)
8071 /* Point didn't move. */
8072 return 0;
8073
8074 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
8075 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
8076 && COMPOSITION_VALID_P (start, end, prop)
8077 && start < prev_pt && end > prev_pt)
8078 /* The last point was within the composition. Return 1 iff
8079 point moved out of the composition. */
8080 return (pt <= start || pt >= end);
8081 }
8082
8083 /* Check a composition at the current point. */
8084 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
8085 && find_composition (pt, -1, &start, &end, &prop, buffer)
8086 && COMPOSITION_VALID_P (start, end, prop)
8087 && start < pt && end > pt);
8088 }
8089
8090 /* Reconsider the setting of B->clip_changed which is displayed
8091 in window W. */
8092
8093 static INLINE void
8094 reconsider_clip_changes (w, b)
8095 struct window *w;
8096 struct buffer *b;
8097 {
8098 if (b->prevent_redisplay_optimizations_p)
8099 b->clip_changed = 1;
8100 else if (b->clip_changed
8101 && !NILP (w->window_end_valid)
8102 && w->current_matrix->buffer == b
8103 && w->current_matrix->zv == BUF_ZV (b)
8104 && w->current_matrix->begv == BUF_BEGV (b))
8105 b->clip_changed = 0;
8106
8107 /* If display wasn't paused, and W is not a tool bar window, see if
8108 point has been moved into or out of a composition. In that case,
8109 we set b->clip_changed to 1 to force updating the screen. If
8110 b->clip_changed has already been set to 1, we can skip this
8111 check. */
8112 if (!b->clip_changed
8113 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
8114 {
8115 int pt;
8116
8117 if (w == XWINDOW (selected_window))
8118 pt = BUF_PT (current_buffer);
8119 else
8120 pt = marker_position (w->pointm);
8121
8122 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
8123 || pt != XINT (w->last_point))
8124 && check_point_in_composition (w->current_matrix->buffer,
8125 XINT (w->last_point),
8126 XBUFFER (w->buffer), pt))
8127 b->clip_changed = 1;
8128 }
8129 }
8130
8131
8132 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
8133 response to any user action; therefore, we should preserve the echo
8134 area. (Actually, our caller does that job.) Perhaps in the future
8135 avoid recentering windows if it is not necessary; currently that
8136 causes some problems. */
8137
8138 static void
8139 redisplay_internal (preserve_echo_area)
8140 int preserve_echo_area;
8141 {
8142 struct window *w = XWINDOW (selected_window);
8143 struct frame *f = XFRAME (w->frame);
8144 int pause;
8145 int must_finish = 0;
8146 struct text_pos tlbufpos, tlendpos;
8147 int number_of_visible_frames;
8148 int count;
8149 struct frame *sf = SELECTED_FRAME ();
8150
8151 /* Non-zero means redisplay has to consider all windows on all
8152 frames. Zero means, only selected_window is considered. */
8153 int consider_all_windows_p;
8154
8155 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
8156
8157 /* No redisplay if running in batch mode or frame is not yet fully
8158 initialized, or redisplay is explicitly turned off by setting
8159 Vinhibit_redisplay. */
8160 if (noninteractive
8161 || !NILP (Vinhibit_redisplay)
8162 || !f->glyphs_initialized_p)
8163 return;
8164
8165 /* The flag redisplay_performed_directly_p is set by
8166 direct_output_for_insert when it already did the whole screen
8167 update necessary. */
8168 if (redisplay_performed_directly_p)
8169 {
8170 redisplay_performed_directly_p = 0;
8171 if (!hscroll_windows (selected_window))
8172 return;
8173 }
8174
8175 #ifdef USE_X_TOOLKIT
8176 if (popup_activated ())
8177 return;
8178 #endif
8179
8180 /* I don't think this happens but let's be paranoid. */
8181 if (redisplaying_p)
8182 return;
8183
8184 /* Record a function that resets redisplaying_p to its old value
8185 when we leave this function. */
8186 count = BINDING_STACK_SIZE ();
8187 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
8188 ++redisplaying_p;
8189
8190 retry:
8191 pause = 0;
8192 reconsider_clip_changes (w, current_buffer);
8193
8194 /* If new fonts have been loaded that make a glyph matrix adjustment
8195 necessary, do it. */
8196 if (fonts_changed_p)
8197 {
8198 adjust_glyphs (NULL);
8199 ++windows_or_buffers_changed;
8200 fonts_changed_p = 0;
8201 }
8202
8203 /* If face_change_count is non-zero, init_iterator will free all
8204 realized faces, which includes the faces referenced from current
8205 matrices. So, we can't reuse current matrices in this case. */
8206 if (face_change_count)
8207 ++windows_or_buffers_changed;
8208
8209 if (! FRAME_WINDOW_P (sf)
8210 && previous_terminal_frame != sf)
8211 {
8212 /* Since frames on an ASCII terminal share the same display
8213 area, displaying a different frame means redisplay the whole
8214 thing. */
8215 windows_or_buffers_changed++;
8216 SET_FRAME_GARBAGED (sf);
8217 XSETFRAME (Vterminal_frame, sf);
8218 }
8219 previous_terminal_frame = sf;
8220
8221 /* Set the visible flags for all frames. Do this before checking
8222 for resized or garbaged frames; they want to know if their frames
8223 are visible. See the comment in frame.h for
8224 FRAME_SAMPLE_VISIBILITY. */
8225 {
8226 Lisp_Object tail, frame;
8227
8228 number_of_visible_frames = 0;
8229
8230 FOR_EACH_FRAME (tail, frame)
8231 {
8232 struct frame *f = XFRAME (frame);
8233
8234 FRAME_SAMPLE_VISIBILITY (f);
8235 if (FRAME_VISIBLE_P (f))
8236 ++number_of_visible_frames;
8237 clear_desired_matrices (f);
8238 }
8239 }
8240
8241 /* Notice any pending interrupt request to change frame size. */
8242 do_pending_window_change (1);
8243
8244 /* Clear frames marked as garbaged. */
8245 if (frame_garbaged)
8246 clear_garbaged_frames ();
8247
8248 /* Build menubar and tool-bar items. */
8249 prepare_menu_bars ();
8250
8251 if (windows_or_buffers_changed)
8252 update_mode_lines++;
8253
8254 /* Detect case that we need to write or remove a star in the mode line. */
8255 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
8256 {
8257 w->update_mode_line = Qt;
8258 if (buffer_shared > 1)
8259 update_mode_lines++;
8260 }
8261
8262 /* If %c is in the mode line, update it if needed. */
8263 if (!NILP (w->column_number_displayed)
8264 /* This alternative quickly identifies a common case
8265 where no change is needed. */
8266 && !(PT == XFASTINT (w->last_point)
8267 && XFASTINT (w->last_modified) >= MODIFF
8268 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
8269 && XFASTINT (w->column_number_displayed) != current_column ())
8270 w->update_mode_line = Qt;
8271
8272 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
8273
8274 /* The variable buffer_shared is set in redisplay_window and
8275 indicates that we redisplay a buffer in different windows. See
8276 there. */
8277 consider_all_windows_p = update_mode_lines || buffer_shared > 1;
8278
8279 /* If specs for an arrow have changed, do thorough redisplay
8280 to ensure we remove any arrow that should no longer exist. */
8281 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
8282 || ! EQ (Voverlay_arrow_string, last_arrow_string))
8283 consider_all_windows_p = windows_or_buffers_changed = 1;
8284
8285 /* Normally the message* functions will have already displayed and
8286 updated the echo area, but the frame may have been trashed, or
8287 the update may have been preempted, so display the echo area
8288 again here. Checking both message buffers captures the case that
8289 the echo area should be cleared. */
8290 if (!NILP (echo_area_buffer[0]) || !NILP (echo_area_buffer[1]))
8291 {
8292 int window_height_changed_p = echo_area_display (0);
8293 must_finish = 1;
8294
8295 if (fonts_changed_p)
8296 goto retry;
8297 else if (window_height_changed_p)
8298 {
8299 consider_all_windows_p = 1;
8300 ++update_mode_lines;
8301 ++windows_or_buffers_changed;
8302
8303 /* If window configuration was changed, frames may have been
8304 marked garbaged. Clear them or we will experience
8305 surprises wrt scrolling. */
8306 if (frame_garbaged)
8307 clear_garbaged_frames ();
8308 }
8309 }
8310 else if (EQ (selected_window, minibuf_window)
8311 && (current_buffer->clip_changed
8312 || XFASTINT (w->last_modified) < MODIFF
8313 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8314 && resize_mini_window (w, 0))
8315 {
8316 /* Resized active mini-window to fit the size of what it is
8317 showing if its contents might have changed. */
8318 must_finish = 1;
8319 consider_all_windows_p = 1;
8320 ++windows_or_buffers_changed;
8321 ++update_mode_lines;
8322
8323 /* If window configuration was changed, frames may have been
8324 marked garbaged. Clear them or we will experience
8325 surprises wrt scrolling. */
8326 if (frame_garbaged)
8327 clear_garbaged_frames ();
8328 }
8329
8330
8331 /* If showing the region, and mark has changed, we must redisplay
8332 the whole window. The assignment to this_line_start_pos prevents
8333 the optimization directly below this if-statement. */
8334 if (((!NILP (Vtransient_mark_mode)
8335 && !NILP (XBUFFER (w->buffer)->mark_active))
8336 != !NILP (w->region_showing))
8337 || (!NILP (w->region_showing)
8338 && !EQ (w->region_showing,
8339 Fmarker_position (XBUFFER (w->buffer)->mark))))
8340 CHARPOS (this_line_start_pos) = 0;
8341
8342 /* Optimize the case that only the line containing the cursor in the
8343 selected window has changed. Variables starting with this_ are
8344 set in display_line and record information about the line
8345 containing the cursor. */
8346 tlbufpos = this_line_start_pos;
8347 tlendpos = this_line_end_pos;
8348 if (!consider_all_windows_p
8349 && CHARPOS (tlbufpos) > 0
8350 && NILP (w->update_mode_line)
8351 && !current_buffer->clip_changed
8352 && FRAME_VISIBLE_P (XFRAME (w->frame))
8353 && !FRAME_OBSCURED_P (XFRAME (w->frame))
8354 /* Make sure recorded data applies to current buffer, etc. */
8355 && this_line_buffer == current_buffer
8356 && current_buffer == XBUFFER (w->buffer)
8357 && NILP (w->force_start)
8358 /* Point must be on the line that we have info recorded about. */
8359 && PT >= CHARPOS (tlbufpos)
8360 && PT <= Z - CHARPOS (tlendpos)
8361 /* All text outside that line, including its final newline,
8362 must be unchanged */
8363 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
8364 CHARPOS (tlendpos)))
8365 {
8366 if (CHARPOS (tlbufpos) > BEGV
8367 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
8368 && (CHARPOS (tlbufpos) == ZV
8369 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
8370 /* Former continuation line has disappeared by becoming empty */
8371 goto cancel;
8372 else if (XFASTINT (w->last_modified) < MODIFF
8373 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
8374 || MINI_WINDOW_P (w))
8375 {
8376 /* We have to handle the case of continuation around a
8377 wide-column character (See the comment in indent.c around
8378 line 885).
8379
8380 For instance, in the following case:
8381
8382 -------- Insert --------
8383 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
8384 J_I_ ==> J_I_ `^^' are cursors.
8385 ^^ ^^
8386 -------- --------
8387
8388 As we have to redraw the line above, we should goto cancel. */
8389
8390 struct it it;
8391 int line_height_before = this_line_pixel_height;
8392
8393 /* Note that start_display will handle the case that the
8394 line starting at tlbufpos is a continuation lines. */
8395 start_display (&it, w, tlbufpos);
8396
8397 /* Implementation note: It this still necessary? */
8398 if (it.current_x != this_line_start_x)
8399 goto cancel;
8400
8401 TRACE ((stderr, "trying display optimization 1\n"));
8402 w->cursor.vpos = -1;
8403 overlay_arrow_seen = 0;
8404 it.vpos = this_line_vpos;
8405 it.current_y = this_line_y;
8406 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
8407 display_line (&it);
8408
8409 /* If line contains point, is not continued,
8410 and ends at same distance from eob as before, we win */
8411 if (w->cursor.vpos >= 0
8412 /* Line is not continued, otherwise this_line_start_pos
8413 would have been set to 0 in display_line. */
8414 && CHARPOS (this_line_start_pos)
8415 /* Line ends as before. */
8416 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
8417 /* Line has same height as before. Otherwise other lines
8418 would have to be shifted up or down. */
8419 && this_line_pixel_height == line_height_before)
8420 {
8421 /* If this is not the window's last line, we must adjust
8422 the charstarts of the lines below. */
8423 if (it.current_y < it.last_visible_y)
8424 {
8425 struct glyph_row *row
8426 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
8427 int delta, delta_bytes;
8428
8429 if (Z - CHARPOS (tlendpos) == ZV)
8430 {
8431 /* This line ends at end of (accessible part of)
8432 buffer. There is no newline to count. */
8433 delta = (Z
8434 - CHARPOS (tlendpos)
8435 - MATRIX_ROW_START_CHARPOS (row));
8436 delta_bytes = (Z_BYTE
8437 - BYTEPOS (tlendpos)
8438 - MATRIX_ROW_START_BYTEPOS (row));
8439 }
8440 else
8441 {
8442 /* This line ends in a newline. Must take
8443 account of the newline and the rest of the
8444 text that follows. */
8445 delta = (Z
8446 - CHARPOS (tlendpos)
8447 - MATRIX_ROW_START_CHARPOS (row));
8448 delta_bytes = (Z_BYTE
8449 - BYTEPOS (tlendpos)
8450 - MATRIX_ROW_START_BYTEPOS (row));
8451 }
8452
8453 increment_matrix_positions (w->current_matrix,
8454 this_line_vpos + 1,
8455 w->current_matrix->nrows,
8456 delta, delta_bytes);
8457 }
8458
8459 /* If this row displays text now but previously didn't,
8460 or vice versa, w->window_end_vpos may have to be
8461 adjusted. */
8462 if ((it.glyph_row - 1)->displays_text_p)
8463 {
8464 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
8465 XSETINT (w->window_end_vpos, this_line_vpos);
8466 }
8467 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
8468 && this_line_vpos > 0)
8469 XSETINT (w->window_end_vpos, this_line_vpos - 1);
8470 w->window_end_valid = Qnil;
8471
8472 /* Update hint: No need to try to scroll in update_window. */
8473 w->desired_matrix->no_scrolling_p = 1;
8474
8475 #if GLYPH_DEBUG
8476 *w->desired_matrix->method = 0;
8477 debug_method_add (w, "optimization 1");
8478 #endif
8479 goto update;
8480 }
8481 else
8482 goto cancel;
8483 }
8484 else if (/* Cursor position hasn't changed. */
8485 PT == XFASTINT (w->last_point)
8486 /* Make sure the cursor was last displayed
8487 in this window. Otherwise we have to reposition it. */
8488 && 0 <= w->cursor.vpos
8489 && XINT (w->height) > w->cursor.vpos)
8490 {
8491 if (!must_finish)
8492 {
8493 do_pending_window_change (1);
8494
8495 /* We used to always goto end_of_redisplay here, but this
8496 isn't enough if we have a blinking cursor. */
8497 if (w->cursor_off_p == w->last_cursor_off_p)
8498 goto end_of_redisplay;
8499 }
8500 goto update;
8501 }
8502 /* If highlighting the region, or if the cursor is in the echo area,
8503 then we can't just move the cursor. */
8504 else if (! (!NILP (Vtransient_mark_mode)
8505 && !NILP (current_buffer->mark_active))
8506 && (EQ (selected_window, current_buffer->last_selected_window)
8507 || highlight_nonselected_windows)
8508 && NILP (w->region_showing)
8509 && NILP (Vshow_trailing_whitespace)
8510 && !cursor_in_echo_area)
8511 {
8512 struct it it;
8513 struct glyph_row *row;
8514
8515 /* Skip from tlbufpos to PT and see where it is. Note that
8516 PT may be in invisible text. If so, we will end at the
8517 next visible position. */
8518 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
8519 NULL, DEFAULT_FACE_ID);
8520 it.current_x = this_line_start_x;
8521 it.current_y = this_line_y;
8522 it.vpos = this_line_vpos;
8523
8524 /* The call to move_it_to stops in front of PT, but
8525 moves over before-strings. */
8526 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
8527
8528 if (it.vpos == this_line_vpos
8529 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
8530 row->enabled_p))
8531 {
8532 xassert (this_line_vpos == it.vpos);
8533 xassert (this_line_y == it.current_y);
8534 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
8535 #if GLYPH_DEBUG
8536 *w->desired_matrix->method = 0;
8537 debug_method_add (w, "optimization 3");
8538 #endif
8539 goto update;
8540 }
8541 else
8542 goto cancel;
8543 }
8544
8545 cancel:
8546 /* Text changed drastically or point moved off of line. */
8547 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
8548 }
8549
8550 CHARPOS (this_line_start_pos) = 0;
8551 consider_all_windows_p |= buffer_shared > 1;
8552 ++clear_face_cache_count;
8553
8554
8555 /* Build desired matrices, and update the display. If
8556 consider_all_windows_p is non-zero, do it for all windows on all
8557 frames. Otherwise do it for selected_window, only. */
8558
8559 if (consider_all_windows_p)
8560 {
8561 Lisp_Object tail, frame;
8562 int i, n = 0, size = 50;
8563 struct frame **updated
8564 = (struct frame **) alloca (size * sizeof *updated);
8565
8566 /* Clear the face cache eventually. */
8567 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
8568 {
8569 clear_face_cache (0);
8570 clear_face_cache_count = 0;
8571 }
8572
8573 /* Recompute # windows showing selected buffer. This will be
8574 incremented each time such a window is displayed. */
8575 buffer_shared = 0;
8576
8577 FOR_EACH_FRAME (tail, frame)
8578 {
8579 struct frame *f = XFRAME (frame);
8580
8581 if (FRAME_WINDOW_P (f) || f == sf)
8582 {
8583 /* Mark all the scroll bars to be removed; we'll redeem
8584 the ones we want when we redisplay their windows. */
8585 if (condemn_scroll_bars_hook)
8586 condemn_scroll_bars_hook (f);
8587
8588 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8589 redisplay_windows (FRAME_ROOT_WINDOW (f));
8590
8591 /* Any scroll bars which redisplay_windows should have
8592 nuked should now go away. */
8593 if (judge_scroll_bars_hook)
8594 judge_scroll_bars_hook (f);
8595
8596 /* If fonts changed, display again. */
8597 if (fonts_changed_p)
8598 goto retry;
8599
8600 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8601 {
8602 /* See if we have to hscroll. */
8603 if (hscroll_windows (f->root_window))
8604 goto retry;
8605
8606 /* Prevent various kinds of signals during display
8607 update. stdio is not robust about handling
8608 signals, which can cause an apparent I/O
8609 error. */
8610 if (interrupt_input)
8611 unrequest_sigio ();
8612 stop_polling ();
8613
8614 /* Update the display. */
8615 set_window_update_flags (XWINDOW (f->root_window), 1);
8616 pause |= update_frame (f, 0, 0);
8617 if (pause)
8618 break;
8619
8620 if (n == size)
8621 {
8622 int nbytes = size * sizeof *updated;
8623 struct frame **p = (struct frame **) alloca (2 * nbytes);
8624 bcopy (updated, p, nbytes);
8625 size *= 2;
8626 }
8627
8628 updated[n++] = f;
8629 }
8630 }
8631 }
8632
8633 /* Do the mark_window_display_accurate after all windows have
8634 been redisplayed because this call resets flags in buffers
8635 which are needed for proper redisplay. */
8636 for (i = 0; i < n; ++i)
8637 {
8638 struct frame *f = updated[i];
8639 mark_window_display_accurate (f->root_window, 1);
8640 if (frame_up_to_date_hook)
8641 frame_up_to_date_hook (f);
8642 }
8643 }
8644 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8645 {
8646 Lisp_Object mini_window;
8647 struct frame *mini_frame;
8648
8649 redisplay_window (selected_window, 1);
8650
8651 /* Compare desired and current matrices, perform output. */
8652 update:
8653
8654 /* If fonts changed, display again. */
8655 if (fonts_changed_p)
8656 goto retry;
8657
8658 /* Prevent various kinds of signals during display update.
8659 stdio is not robust about handling signals,
8660 which can cause an apparent I/O error. */
8661 if (interrupt_input)
8662 unrequest_sigio ();
8663 stop_polling ();
8664
8665 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8666 {
8667 if (hscroll_windows (selected_window))
8668 goto retry;
8669
8670 XWINDOW (selected_window)->must_be_updated_p = 1;
8671 pause = update_frame (sf, 0, 0);
8672 }
8673
8674 /* We may have called echo_area_display at the top of this
8675 function. If the echo area is on another frame, that may
8676 have put text on a frame other than the selected one, so the
8677 above call to update_frame would not have caught it. Catch
8678 it here. */
8679 mini_window = FRAME_MINIBUF_WINDOW (sf);
8680 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8681
8682 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
8683 {
8684 XWINDOW (mini_window)->must_be_updated_p = 1;
8685 pause |= update_frame (mini_frame, 0, 0);
8686 if (!pause && hscroll_windows (mini_window))
8687 goto retry;
8688 }
8689 }
8690
8691 /* If display was paused because of pending input, make sure we do a
8692 thorough update the next time. */
8693 if (pause)
8694 {
8695 /* Prevent the optimization at the beginning of
8696 redisplay_internal that tries a single-line update of the
8697 line containing the cursor in the selected window. */
8698 CHARPOS (this_line_start_pos) = 0;
8699
8700 /* Let the overlay arrow be updated the next time. */
8701 if (!NILP (last_arrow_position))
8702 {
8703 last_arrow_position = Qt;
8704 last_arrow_string = Qt;
8705 }
8706
8707 /* If we pause after scrolling, some rows in the current
8708 matrices of some windows are not valid. */
8709 if (!WINDOW_FULL_WIDTH_P (w)
8710 && !FRAME_WINDOW_P (XFRAME (w->frame)))
8711 update_mode_lines = 1;
8712 }
8713 else
8714 {
8715 if (!consider_all_windows_p)
8716 {
8717 /* This has already been done above if
8718 consider_all_windows_p is set. */
8719 mark_window_display_accurate_1 (w, 1);
8720
8721 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
8722 last_arrow_string = Voverlay_arrow_string;
8723
8724 if (frame_up_to_date_hook != 0)
8725 frame_up_to_date_hook (sf);
8726 }
8727
8728 update_mode_lines = 0;
8729 windows_or_buffers_changed = 0;
8730 }
8731
8732 /* Start SIGIO interrupts coming again. Having them off during the
8733 code above makes it less likely one will discard output, but not
8734 impossible, since there might be stuff in the system buffer here.
8735 But it is much hairier to try to do anything about that. */
8736 if (interrupt_input)
8737 request_sigio ();
8738 start_polling ();
8739
8740 /* If a frame has become visible which was not before, redisplay
8741 again, so that we display it. Expose events for such a frame
8742 (which it gets when becoming visible) don't call the parts of
8743 redisplay constructing glyphs, so simply exposing a frame won't
8744 display anything in this case. So, we have to display these
8745 frames here explicitly. */
8746 if (!pause)
8747 {
8748 Lisp_Object tail, frame;
8749 int new_count = 0;
8750
8751 FOR_EACH_FRAME (tail, frame)
8752 {
8753 int this_is_visible = 0;
8754
8755 if (XFRAME (frame)->visible)
8756 this_is_visible = 1;
8757 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
8758 if (XFRAME (frame)->visible)
8759 this_is_visible = 1;
8760
8761 if (this_is_visible)
8762 new_count++;
8763 }
8764
8765 if (new_count != number_of_visible_frames)
8766 windows_or_buffers_changed++;
8767 }
8768
8769 /* Change frame size now if a change is pending. */
8770 do_pending_window_change (1);
8771
8772 /* If we just did a pending size change, or have additional
8773 visible frames, redisplay again. */
8774 if (windows_or_buffers_changed && !pause)
8775 goto retry;
8776
8777 end_of_redisplay:;
8778
8779 unbind_to (count, Qnil);
8780 }
8781
8782
8783 /* Redisplay, but leave alone any recent echo area message unless
8784 another message has been requested in its place.
8785
8786 This is useful in situations where you need to redisplay but no
8787 user action has occurred, making it inappropriate for the message
8788 area to be cleared. See tracking_off and
8789 wait_reading_process_input for examples of these situations.
8790
8791 FROM_WHERE is an integer saying from where this function was
8792 called. This is useful for debugging. */
8793
8794 void
8795 redisplay_preserve_echo_area (from_where)
8796 int from_where;
8797 {
8798 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
8799
8800 if (!NILP (echo_area_buffer[1]))
8801 {
8802 /* We have a previously displayed message, but no current
8803 message. Redisplay the previous message. */
8804 display_last_displayed_message_p = 1;
8805 redisplay_internal (1);
8806 display_last_displayed_message_p = 0;
8807 }
8808 else
8809 redisplay_internal (1);
8810 }
8811
8812
8813 /* Function registered with record_unwind_protect in
8814 redisplay_internal. Clears the flag indicating that a redisplay is
8815 in progress. */
8816
8817 static Lisp_Object
8818 unwind_redisplay (old_redisplaying_p)
8819 Lisp_Object old_redisplaying_p;
8820 {
8821 redisplaying_p = XFASTINT (old_redisplaying_p);
8822 return Qnil;
8823 }
8824
8825
8826 /* Mark the display of window W as accurate or inaccurate. If
8827 ACCURATE_P is non-zero mark display of W as accurate. If
8828 ACCURATE_P is zero, arrange for W to be redisplayed the next time
8829 redisplay_internal is called. */
8830
8831 static void
8832 mark_window_display_accurate_1 (w, accurate_p)
8833 struct window *w;
8834 int accurate_p;
8835 {
8836 if (BUFFERP (w->buffer))
8837 {
8838 struct buffer *b = XBUFFER (w->buffer);
8839
8840 w->last_modified
8841 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
8842 w->last_overlay_modified
8843 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
8844 w->last_had_star
8845 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
8846
8847 if (accurate_p)
8848 {
8849 b->clip_changed = 0;
8850 b->prevent_redisplay_optimizations_p = 0;
8851
8852 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
8853 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
8854 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
8855 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
8856
8857 w->current_matrix->buffer = b;
8858 w->current_matrix->begv = BUF_BEGV (b);
8859 w->current_matrix->zv = BUF_ZV (b);
8860
8861 w->last_cursor = w->cursor;
8862 w->last_cursor_off_p = w->cursor_off_p;
8863
8864 if (w == XWINDOW (selected_window))
8865 w->last_point = make_number (BUF_PT (b));
8866 else
8867 w->last_point = make_number (XMARKER (w->pointm)->charpos);
8868 }
8869 }
8870
8871 if (accurate_p)
8872 {
8873 w->window_end_valid = w->buffer;
8874 w->update_mode_line = Qnil;
8875 }
8876 }
8877
8878
8879 /* Mark the display of windows in the window tree rooted at WINDOW as
8880 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
8881 windows as accurate. If ACCURATE_P is zero, arrange for windows to
8882 be redisplayed the next time redisplay_internal is called. */
8883
8884 void
8885 mark_window_display_accurate (window, accurate_p)
8886 Lisp_Object window;
8887 int accurate_p;
8888 {
8889 struct window *w;
8890
8891 for (; !NILP (window); window = w->next)
8892 {
8893 w = XWINDOW (window);
8894 mark_window_display_accurate_1 (w, accurate_p);
8895
8896 if (!NILP (w->vchild))
8897 mark_window_display_accurate (w->vchild, accurate_p);
8898 if (!NILP (w->hchild))
8899 mark_window_display_accurate (w->hchild, accurate_p);
8900 }
8901
8902 if (accurate_p)
8903 {
8904 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
8905 last_arrow_string = Voverlay_arrow_string;
8906 }
8907 else
8908 {
8909 /* Force a thorough redisplay the next time by setting
8910 last_arrow_position and last_arrow_string to t, which is
8911 unequal to any useful value of Voverlay_arrow_... */
8912 last_arrow_position = Qt;
8913 last_arrow_string = Qt;
8914 }
8915 }
8916
8917
8918 /* Return value in display table DP (Lisp_Char_Table *) for character
8919 C. Since a display table doesn't have any parent, we don't have to
8920 follow parent. Do not call this function directly but use the
8921 macro DISP_CHAR_VECTOR. */
8922
8923 Lisp_Object
8924 disp_char_vector (dp, c)
8925 struct Lisp_Char_Table *dp;
8926 int c;
8927 {
8928 int code[4], i;
8929 Lisp_Object val;
8930
8931 if (SINGLE_BYTE_CHAR_P (c))
8932 return (dp->contents[c]);
8933
8934 SPLIT_CHAR (c, code[0], code[1], code[2]);
8935 if (code[1] < 32)
8936 code[1] = -1;
8937 else if (code[2] < 32)
8938 code[2] = -1;
8939
8940 /* Here, the possible range of code[0] (== charset ID) is
8941 128..max_charset. Since the top level char table contains data
8942 for multibyte characters after 256th element, we must increment
8943 code[0] by 128 to get a correct index. */
8944 code[0] += 128;
8945 code[3] = -1; /* anchor */
8946
8947 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
8948 {
8949 val = dp->contents[code[i]];
8950 if (!SUB_CHAR_TABLE_P (val))
8951 return (NILP (val) ? dp->defalt : val);
8952 }
8953
8954 /* Here, val is a sub char table. We return the default value of
8955 it. */
8956 return (dp->defalt);
8957 }
8958
8959
8960 \f
8961 /***********************************************************************
8962 Window Redisplay
8963 ***********************************************************************/
8964
8965 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
8966
8967 static void
8968 redisplay_windows (window)
8969 Lisp_Object window;
8970 {
8971 while (!NILP (window))
8972 {
8973 struct window *w = XWINDOW (window);
8974
8975 if (!NILP (w->hchild))
8976 redisplay_windows (w->hchild);
8977 else if (!NILP (w->vchild))
8978 redisplay_windows (w->vchild);
8979 else
8980 redisplay_window (window, 0);
8981
8982 window = w->next;
8983 }
8984 }
8985
8986
8987 /* Set cursor position of W. PT is assumed to be displayed in ROW.
8988 DELTA is the number of bytes by which positions recorded in ROW
8989 differ from current buffer positions. */
8990
8991 void
8992 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
8993 struct window *w;
8994 struct glyph_row *row;
8995 struct glyph_matrix *matrix;
8996 int delta, delta_bytes, dy, dvpos;
8997 {
8998 struct glyph *glyph = row->glyphs[TEXT_AREA];
8999 struct glyph *end = glyph + row->used[TEXT_AREA];
9000 int x = row->x;
9001 int pt_old = PT - delta;
9002
9003 /* Skip over glyphs not having an object at the start of the row.
9004 These are special glyphs like truncation marks on terminal
9005 frames. */
9006 if (row->displays_text_p)
9007 while (glyph < end
9008 && INTEGERP (glyph->object)
9009 && glyph->charpos < 0)
9010 {
9011 x += glyph->pixel_width;
9012 ++glyph;
9013 }
9014
9015 while (glyph < end
9016 && !INTEGERP (glyph->object)
9017 && (!BUFFERP (glyph->object)
9018 || glyph->charpos < pt_old))
9019 {
9020 x += glyph->pixel_width;
9021 ++glyph;
9022 }
9023
9024 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
9025 w->cursor.x = x;
9026 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
9027 w->cursor.y = row->y + dy;
9028
9029 if (w == XWINDOW (selected_window))
9030 {
9031 if (!row->continued_p
9032 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
9033 && row->x == 0)
9034 {
9035 this_line_buffer = XBUFFER (w->buffer);
9036
9037 CHARPOS (this_line_start_pos)
9038 = MATRIX_ROW_START_CHARPOS (row) + delta;
9039 BYTEPOS (this_line_start_pos)
9040 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
9041
9042 CHARPOS (this_line_end_pos)
9043 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
9044 BYTEPOS (this_line_end_pos)
9045 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
9046
9047 this_line_y = w->cursor.y;
9048 this_line_pixel_height = row->height;
9049 this_line_vpos = w->cursor.vpos;
9050 this_line_start_x = row->x;
9051 }
9052 else
9053 CHARPOS (this_line_start_pos) = 0;
9054 }
9055 }
9056
9057
9058 /* Run window scroll functions, if any, for WINDOW with new window
9059 start STARTP. Sets the window start of WINDOW to that position.
9060
9061 We assume that the window's buffer is really current. */
9062
9063 static INLINE struct text_pos
9064 run_window_scroll_functions (window, startp)
9065 Lisp_Object window;
9066 struct text_pos startp;
9067 {
9068 struct window *w = XWINDOW (window);
9069 SET_MARKER_FROM_TEXT_POS (w->start, startp);
9070
9071 if (current_buffer != XBUFFER (w->buffer))
9072 abort ();
9073
9074 if (!NILP (Vwindow_scroll_functions))
9075 {
9076 run_hook_with_args_2 (Qwindow_scroll_functions, window,
9077 make_number (CHARPOS (startp)));
9078 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9079 /* In case the hook functions switch buffers. */
9080 if (current_buffer != XBUFFER (w->buffer))
9081 set_buffer_internal_1 (XBUFFER (w->buffer));
9082 }
9083
9084 return startp;
9085 }
9086
9087
9088 /* Modify the desired matrix of window W and W->vscroll so that the
9089 line containing the cursor is fully visible. */
9090
9091 static void
9092 make_cursor_line_fully_visible (w)
9093 struct window *w;
9094 {
9095 struct glyph_matrix *matrix;
9096 struct glyph_row *row;
9097 int window_height;
9098
9099 /* It's not always possible to find the cursor, e.g, when a window
9100 is full of overlay strings. Don't do anything in that case. */
9101 if (w->cursor.vpos < 0)
9102 return;
9103
9104 matrix = w->desired_matrix;
9105 row = MATRIX_ROW (matrix, w->cursor.vpos);
9106
9107 /* If the cursor row is not partially visible, there's nothing
9108 to do. */
9109 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
9110 return;
9111
9112 /* If the row the cursor is in is taller than the window's height,
9113 it's not clear what to do, so do nothing. */
9114 window_height = window_box_height (w);
9115 if (row->height >= window_height)
9116 return;
9117
9118 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
9119 {
9120 int dy = row->height - row->visible_height;
9121 w->vscroll = 0;
9122 w->cursor.y += dy;
9123 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9124 }
9125 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
9126 {
9127 int dy = - (row->height - row->visible_height);
9128 w->vscroll = dy;
9129 w->cursor.y += dy;
9130 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9131 }
9132
9133 /* When we change the cursor y-position of the selected window,
9134 change this_line_y as well so that the display optimization for
9135 the cursor line of the selected window in redisplay_internal uses
9136 the correct y-position. */
9137 if (w == XWINDOW (selected_window))
9138 this_line_y = w->cursor.y;
9139 }
9140
9141
9142 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
9143 non-zero means only WINDOW is redisplayed in redisplay_internal.
9144 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
9145 in redisplay_window to bring a partially visible line into view in
9146 the case that only the cursor has moved.
9147
9148 Value is
9149
9150 1 if scrolling succeeded
9151
9152 0 if scrolling didn't find point.
9153
9154 -1 if new fonts have been loaded so that we must interrupt
9155 redisplay, adjust glyph matrices, and try again. */
9156
9157 static int
9158 try_scrolling (window, just_this_one_p, scroll_conservatively,
9159 scroll_step, temp_scroll_step)
9160 Lisp_Object window;
9161 int just_this_one_p;
9162 int scroll_conservatively, scroll_step;
9163 int temp_scroll_step;
9164 {
9165 struct window *w = XWINDOW (window);
9166 struct frame *f = XFRAME (w->frame);
9167 struct text_pos scroll_margin_pos;
9168 struct text_pos pos;
9169 struct text_pos startp;
9170 struct it it;
9171 Lisp_Object window_end;
9172 int this_scroll_margin;
9173 int dy = 0;
9174 int scroll_max;
9175 int rc;
9176 int amount_to_scroll = 0;
9177 Lisp_Object aggressive;
9178 int height;
9179
9180 #if GLYPH_DEBUG
9181 debug_method_add (w, "try_scrolling");
9182 #endif
9183
9184 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9185
9186 /* Compute scroll margin height in pixels. We scroll when point is
9187 within this distance from the top or bottom of the window. */
9188 if (scroll_margin > 0)
9189 {
9190 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
9191 this_scroll_margin *= CANON_Y_UNIT (f);
9192 }
9193 else
9194 this_scroll_margin = 0;
9195
9196 /* Compute how much we should try to scroll maximally to bring point
9197 into view. */
9198 if (scroll_step || scroll_conservatively || temp_scroll_step)
9199 scroll_max = max (scroll_step,
9200 max (scroll_conservatively, temp_scroll_step));
9201 else if (NUMBERP (current_buffer->scroll_down_aggressively)
9202 || NUMBERP (current_buffer->scroll_up_aggressively))
9203 /* We're trying to scroll because of aggressive scrolling
9204 but no scroll_step is set. Choose an arbitrary one. Maybe
9205 there should be a variable for this. */
9206 scroll_max = 10;
9207 else
9208 scroll_max = 0;
9209 scroll_max *= CANON_Y_UNIT (f);
9210
9211 /* Decide whether we have to scroll down. Start at the window end
9212 and move this_scroll_margin up to find the position of the scroll
9213 margin. */
9214 window_end = Fwindow_end (window, Qt);
9215 CHARPOS (scroll_margin_pos) = XINT (window_end);
9216 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
9217 if (this_scroll_margin)
9218 {
9219 start_display (&it, w, scroll_margin_pos);
9220 move_it_vertically (&it, - this_scroll_margin);
9221 scroll_margin_pos = it.current.pos;
9222 }
9223
9224 if (PT >= CHARPOS (scroll_margin_pos))
9225 {
9226 int y0;
9227
9228 /* Point is in the scroll margin at the bottom of the window, or
9229 below. Compute a new window start that makes point visible. */
9230
9231 /* Compute the distance from the scroll margin to PT.
9232 Give up if the distance is greater than scroll_max. */
9233 start_display (&it, w, scroll_margin_pos);
9234 y0 = it.current_y;
9235 move_it_to (&it, PT, 0, it.last_visible_y, -1,
9236 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9237
9238 /* With a scroll_margin of 0, scroll_margin_pos is at the window
9239 end, which is one line below the window. The iterator's
9240 current_y will be same as y0 in that case, but we have to
9241 scroll a line to make PT visible. That's the reason why 1 is
9242 added below. */
9243 dy = 1 + it.current_y - y0;
9244
9245 if (dy > scroll_max)
9246 return 0;
9247
9248 /* Move the window start down. If scrolling conservatively,
9249 move it just enough down to make point visible. If
9250 scroll_step is set, move it down by scroll_step. */
9251 start_display (&it, w, startp);
9252
9253 if (scroll_conservatively)
9254 amount_to_scroll
9255 = max (max (dy, CANON_Y_UNIT (f)),
9256 CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
9257 else if (scroll_step || temp_scroll_step)
9258 amount_to_scroll = scroll_max;
9259 else
9260 {
9261 aggressive = current_buffer->scroll_down_aggressively;
9262 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
9263 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
9264 if (NUMBERP (aggressive))
9265 amount_to_scroll = XFLOATINT (aggressive) * height;
9266 }
9267
9268 if (amount_to_scroll <= 0)
9269 return 0;
9270
9271 move_it_vertically (&it, amount_to_scroll);
9272 startp = it.current.pos;
9273 }
9274 else
9275 {
9276 /* See if point is inside the scroll margin at the top of the
9277 window. */
9278 scroll_margin_pos = startp;
9279 if (this_scroll_margin)
9280 {
9281 start_display (&it, w, startp);
9282 move_it_vertically (&it, this_scroll_margin);
9283 scroll_margin_pos = it.current.pos;
9284 }
9285
9286 if (PT < CHARPOS (scroll_margin_pos))
9287 {
9288 /* Point is in the scroll margin at the top of the window or
9289 above what is displayed in the window. */
9290 int y0;
9291
9292 /* Compute the vertical distance from PT to the scroll
9293 margin position. Give up if distance is greater than
9294 scroll_max. */
9295 SET_TEXT_POS (pos, PT, PT_BYTE);
9296 start_display (&it, w, pos);
9297 y0 = it.current_y;
9298 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
9299 it.last_visible_y, -1,
9300 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9301 dy = it.current_y - y0;
9302 if (dy > scroll_max)
9303 return 0;
9304
9305 /* Compute new window start. */
9306 start_display (&it, w, startp);
9307
9308 if (scroll_conservatively)
9309 amount_to_scroll =
9310 max (dy, CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
9311 else if (scroll_step || temp_scroll_step)
9312 amount_to_scroll = scroll_max;
9313 else
9314 {
9315 aggressive = current_buffer->scroll_up_aggressively;
9316 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
9317 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
9318 if (NUMBERP (aggressive))
9319 amount_to_scroll = XFLOATINT (aggressive) * height;
9320 }
9321
9322 if (amount_to_scroll <= 0)
9323 return 0;
9324
9325 move_it_vertically (&it, - amount_to_scroll);
9326 startp = it.current.pos;
9327 }
9328 }
9329
9330 /* Run window scroll functions. */
9331 startp = run_window_scroll_functions (window, startp);
9332
9333 /* Display the window. Give up if new fonts are loaded, or if point
9334 doesn't appear. */
9335 if (!try_window (window, startp))
9336 rc = -1;
9337 else if (w->cursor.vpos < 0)
9338 {
9339 clear_glyph_matrix (w->desired_matrix);
9340 rc = 0;
9341 }
9342 else
9343 {
9344 /* Maybe forget recorded base line for line number display. */
9345 if (!just_this_one_p
9346 || current_buffer->clip_changed
9347 || BEG_UNCHANGED < CHARPOS (startp))
9348 w->base_line_number = Qnil;
9349
9350 /* If cursor ends up on a partially visible line, shift display
9351 lines up or down. */
9352 make_cursor_line_fully_visible (w);
9353 rc = 1;
9354 }
9355
9356 return rc;
9357 }
9358
9359
9360 /* Compute a suitable window start for window W if display of W starts
9361 on a continuation line. Value is non-zero if a new window start
9362 was computed.
9363
9364 The new window start will be computed, based on W's width, starting
9365 from the start of the continued line. It is the start of the
9366 screen line with the minimum distance from the old start W->start. */
9367
9368 static int
9369 compute_window_start_on_continuation_line (w)
9370 struct window *w;
9371 {
9372 struct text_pos pos, start_pos;
9373 int window_start_changed_p = 0;
9374
9375 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
9376
9377 /* If window start is on a continuation line... Window start may be
9378 < BEGV in case there's invisible text at the start of the
9379 buffer (M-x rmail, for example). */
9380 if (CHARPOS (start_pos) > BEGV
9381 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
9382 {
9383 struct it it;
9384 struct glyph_row *row;
9385
9386 /* Handle the case that the window start is out of range. */
9387 if (CHARPOS (start_pos) < BEGV)
9388 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
9389 else if (CHARPOS (start_pos) > ZV)
9390 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
9391
9392 /* Find the start of the continued line. This should be fast
9393 because scan_buffer is fast (newline cache). */
9394 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
9395 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
9396 row, DEFAULT_FACE_ID);
9397 reseat_at_previous_visible_line_start (&it);
9398
9399 /* If the line start is "too far" away from the window start,
9400 say it takes too much time to compute a new window start. */
9401 if (CHARPOS (start_pos) - IT_CHARPOS (it)
9402 < XFASTINT (w->height) * XFASTINT (w->width))
9403 {
9404 int min_distance, distance;
9405
9406 /* Move forward by display lines to find the new window
9407 start. If window width was enlarged, the new start can
9408 be expected to be > the old start. If window width was
9409 decreased, the new window start will be < the old start.
9410 So, we're looking for the display line start with the
9411 minimum distance from the old window start. */
9412 pos = it.current.pos;
9413 min_distance = INFINITY;
9414 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
9415 distance < min_distance)
9416 {
9417 min_distance = distance;
9418 pos = it.current.pos;
9419 move_it_by_lines (&it, 1, 0);
9420 }
9421
9422 /* Set the window start there. */
9423 SET_MARKER_FROM_TEXT_POS (w->start, pos);
9424 window_start_changed_p = 1;
9425 }
9426 }
9427
9428 return window_start_changed_p;
9429 }
9430
9431
9432 /* Try cursor movement in case text has not changes in window WINDOW,
9433 with window start STARTP. Value is
9434
9435 1 if successful
9436
9437 0 if this method cannot be used
9438
9439 -1 if we know we have to scroll the display. *SCROLL_STEP is
9440 set to 1, under certain circumstances, if we want to scroll as
9441 if scroll-step were set to 1. See the code. */
9442
9443 static int
9444 try_cursor_movement (window, startp, scroll_step)
9445 Lisp_Object window;
9446 struct text_pos startp;
9447 int *scroll_step;
9448 {
9449 struct window *w = XWINDOW (window);
9450 struct frame *f = XFRAME (w->frame);
9451 int rc = 0;
9452
9453 /* Handle case where text has not changed, only point, and it has
9454 not moved off the frame. */
9455 if (/* Point may be in this window. */
9456 PT >= CHARPOS (startp)
9457 /* Selective display hasn't changed. */
9458 && !current_buffer->clip_changed
9459 /* Function force-mode-line-update is used to force a thorough
9460 redisplay. It sets either windows_or_buffers_changed or
9461 update_mode_lines. So don't take a shortcut here for these
9462 cases. */
9463 && !update_mode_lines
9464 && !windows_or_buffers_changed
9465 /* Can't use this case if highlighting a region. When a
9466 region exists, cursor movement has to do more than just
9467 set the cursor. */
9468 && !(!NILP (Vtransient_mark_mode)
9469 && !NILP (current_buffer->mark_active))
9470 && NILP (w->region_showing)
9471 && NILP (Vshow_trailing_whitespace)
9472 /* Right after splitting windows, last_point may be nil. */
9473 && INTEGERP (w->last_point)
9474 /* This code is not used for mini-buffer for the sake of the case
9475 of redisplaying to replace an echo area message; since in
9476 that case the mini-buffer contents per se are usually
9477 unchanged. This code is of no real use in the mini-buffer
9478 since the handling of this_line_start_pos, etc., in redisplay
9479 handles the same cases. */
9480 && !EQ (window, minibuf_window)
9481 /* When splitting windows or for new windows, it happens that
9482 redisplay is called with a nil window_end_vpos or one being
9483 larger than the window. This should really be fixed in
9484 window.c. I don't have this on my list, now, so we do
9485 approximately the same as the old redisplay code. --gerd. */
9486 && INTEGERP (w->window_end_vpos)
9487 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
9488 && (FRAME_WINDOW_P (f)
9489 || !MARKERP (Voverlay_arrow_position)
9490 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
9491 {
9492 int this_scroll_margin;
9493 struct glyph_row *row;
9494
9495 #if GLYPH_DEBUG
9496 debug_method_add (w, "cursor movement");
9497 #endif
9498
9499 /* Scroll if point within this distance from the top or bottom
9500 of the window. This is a pixel value. */
9501 this_scroll_margin = max (0, scroll_margin);
9502 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
9503 this_scroll_margin *= CANON_Y_UNIT (f);
9504
9505 /* Start with the row the cursor was displayed during the last
9506 not paused redisplay. Give up if that row is not valid. */
9507 if (w->last_cursor.vpos < 0
9508 || w->last_cursor.vpos >= w->current_matrix->nrows)
9509 rc = -1;
9510 else
9511 {
9512 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
9513 if (row->mode_line_p)
9514 ++row;
9515 if (!row->enabled_p)
9516 rc = -1;
9517 }
9518
9519 if (rc == 0)
9520 {
9521 int scroll_p = 0;
9522 int last_y = window_text_bottom_y (w) - this_scroll_margin;
9523
9524 if (PT > XFASTINT (w->last_point))
9525 {
9526 /* Point has moved forward. */
9527 while (MATRIX_ROW_END_CHARPOS (row) < PT
9528 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
9529 {
9530 xassert (row->enabled_p);
9531 ++row;
9532 }
9533
9534 /* The end position of a row equals the start position
9535 of the next row. If PT is there, we would rather
9536 display it in the next line. */
9537 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9538 && MATRIX_ROW_END_CHARPOS (row) == PT
9539 && !cursor_row_p (w, row))
9540 ++row;
9541
9542 /* If within the scroll margin, scroll. Note that
9543 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
9544 the next line would be drawn, and that
9545 this_scroll_margin can be zero. */
9546 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
9547 || PT > MATRIX_ROW_END_CHARPOS (row)
9548 /* Line is completely visible last line in window
9549 and PT is to be set in the next line. */
9550 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
9551 && PT == MATRIX_ROW_END_CHARPOS (row)
9552 && !row->ends_at_zv_p
9553 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
9554 scroll_p = 1;
9555 }
9556 else if (PT < XFASTINT (w->last_point))
9557 {
9558 /* Cursor has to be moved backward. Note that PT >=
9559 CHARPOS (startp) because of the outer
9560 if-statement. */
9561 while (!row->mode_line_p
9562 && (MATRIX_ROW_START_CHARPOS (row) > PT
9563 || (MATRIX_ROW_START_CHARPOS (row) == PT
9564 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
9565 && (row->y > this_scroll_margin
9566 || CHARPOS (startp) == BEGV))
9567 {
9568 xassert (row->enabled_p);
9569 --row;
9570 }
9571
9572 /* Consider the following case: Window starts at BEGV,
9573 there is invisible, intangible text at BEGV, so that
9574 display starts at some point START > BEGV. It can
9575 happen that we are called with PT somewhere between
9576 BEGV and START. Try to handle that case. */
9577 if (row < w->current_matrix->rows
9578 || row->mode_line_p)
9579 {
9580 row = w->current_matrix->rows;
9581 if (row->mode_line_p)
9582 ++row;
9583 }
9584
9585 /* Due to newlines in overlay strings, we may have to
9586 skip forward over overlay strings. */
9587 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9588 && MATRIX_ROW_END_CHARPOS (row) == PT
9589 && !cursor_row_p (w, row))
9590 ++row;
9591
9592 /* If within the scroll margin, scroll. */
9593 if (row->y < this_scroll_margin
9594 && CHARPOS (startp) != BEGV)
9595 scroll_p = 1;
9596 }
9597
9598 if (PT < MATRIX_ROW_START_CHARPOS (row)
9599 || PT > MATRIX_ROW_END_CHARPOS (row))
9600 {
9601 /* if PT is not in the glyph row, give up. */
9602 rc = -1;
9603 }
9604 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
9605 {
9606 if (PT == MATRIX_ROW_END_CHARPOS (row)
9607 && !row->ends_at_zv_p
9608 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
9609 rc = -1;
9610 else if (row->height > window_box_height (w))
9611 {
9612 /* If we end up in a partially visible line, let's
9613 make it fully visible, except when it's taller
9614 than the window, in which case we can't do much
9615 about it. */
9616 *scroll_step = 1;
9617 rc = -1;
9618 }
9619 else
9620 {
9621 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9622 try_window (window, startp);
9623 make_cursor_line_fully_visible (w);
9624 rc = 1;
9625 }
9626 }
9627 else if (scroll_p)
9628 rc = -1;
9629 else
9630 {
9631 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9632 rc = 1;
9633 }
9634 }
9635 }
9636
9637 return rc;
9638 }
9639
9640
9641 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
9642 selected_window is redisplayed. */
9643
9644 static void
9645 redisplay_window (window, just_this_one_p)
9646 Lisp_Object window;
9647 int just_this_one_p;
9648 {
9649 struct window *w = XWINDOW (window);
9650 struct frame *f = XFRAME (w->frame);
9651 struct buffer *buffer = XBUFFER (w->buffer);
9652 struct buffer *old = current_buffer;
9653 struct text_pos lpoint, opoint, startp;
9654 int update_mode_line;
9655 int tem;
9656 struct it it;
9657 /* Record it now because it's overwritten. */
9658 int current_matrix_up_to_date_p = 0;
9659 int temp_scroll_step = 0;
9660 int count = BINDING_STACK_SIZE ();
9661 int rc;
9662
9663 SET_TEXT_POS (lpoint, PT, PT_BYTE);
9664 opoint = lpoint;
9665
9666 /* W must be a leaf window here. */
9667 xassert (!NILP (w->buffer));
9668 #if GLYPH_DEBUG
9669 *w->desired_matrix->method = 0;
9670 #endif
9671
9672 specbind (Qinhibit_point_motion_hooks, Qt);
9673
9674 reconsider_clip_changes (w, buffer);
9675
9676 /* Has the mode line to be updated? */
9677 update_mode_line = (!NILP (w->update_mode_line)
9678 || update_mode_lines
9679 || buffer->clip_changed);
9680
9681 if (MINI_WINDOW_P (w))
9682 {
9683 if (w == XWINDOW (echo_area_window)
9684 && !NILP (echo_area_buffer[0]))
9685 {
9686 if (update_mode_line)
9687 /* We may have to update a tty frame's menu bar or a
9688 tool-bar. Example `M-x C-h C-h C-g'. */
9689 goto finish_menu_bars;
9690 else
9691 /* We've already displayed the echo area glyphs in this window. */
9692 goto finish_scroll_bars;
9693 }
9694 else if (w != XWINDOW (minibuf_window))
9695 {
9696 /* W is a mini-buffer window, but it's not the currently
9697 active one, so clear it. */
9698 int yb = window_text_bottom_y (w);
9699 struct glyph_row *row;
9700 int y;
9701
9702 for (y = 0, row = w->desired_matrix->rows;
9703 y < yb;
9704 y += row->height, ++row)
9705 blank_row (w, row, y);
9706 goto finish_scroll_bars;
9707 }
9708 }
9709
9710 /* Otherwise set up data on this window; select its buffer and point
9711 value. */
9712 /* Really select the buffer, for the sake of buffer-local
9713 variables. */
9714 set_buffer_internal_1 (XBUFFER (w->buffer));
9715 SET_TEXT_POS (opoint, PT, PT_BYTE);
9716
9717 current_matrix_up_to_date_p
9718 = (!NILP (w->window_end_valid)
9719 && !current_buffer->clip_changed
9720 && XFASTINT (w->last_modified) >= MODIFF
9721 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
9722
9723 /* When windows_or_buffers_changed is non-zero, we can't rely on
9724 the window end being valid, so set it to nil there. */
9725 if (windows_or_buffers_changed)
9726 {
9727 /* If window starts on a continuation line, maybe adjust the
9728 window start in case the window's width changed. */
9729 if (XMARKER (w->start)->buffer == current_buffer)
9730 compute_window_start_on_continuation_line (w);
9731
9732 w->window_end_valid = Qnil;
9733 }
9734
9735 /* Some sanity checks. */
9736 CHECK_WINDOW_END (w);
9737 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
9738 abort ();
9739 if (BYTEPOS (opoint) < CHARPOS (opoint))
9740 abort ();
9741
9742 /* If %c is in mode line, update it if needed. */
9743 if (!NILP (w->column_number_displayed)
9744 /* This alternative quickly identifies a common case
9745 where no change is needed. */
9746 && !(PT == XFASTINT (w->last_point)
9747 && XFASTINT (w->last_modified) >= MODIFF
9748 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
9749 && XFASTINT (w->column_number_displayed) != current_column ())
9750 update_mode_line = 1;
9751
9752 /* Count number of windows showing the selected buffer. An indirect
9753 buffer counts as its base buffer. */
9754 if (!just_this_one_p)
9755 {
9756 struct buffer *current_base, *window_base;
9757 current_base = current_buffer;
9758 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
9759 if (current_base->base_buffer)
9760 current_base = current_base->base_buffer;
9761 if (window_base->base_buffer)
9762 window_base = window_base->base_buffer;
9763 if (current_base == window_base)
9764 buffer_shared++;
9765 }
9766
9767 /* Point refers normally to the selected window. For any other
9768 window, set up appropriate value. */
9769 if (!EQ (window, selected_window))
9770 {
9771 int new_pt = XMARKER (w->pointm)->charpos;
9772 int new_pt_byte = marker_byte_position (w->pointm);
9773 if (new_pt < BEGV)
9774 {
9775 new_pt = BEGV;
9776 new_pt_byte = BEGV_BYTE;
9777 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
9778 }
9779 else if (new_pt > (ZV - 1))
9780 {
9781 new_pt = ZV;
9782 new_pt_byte = ZV_BYTE;
9783 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
9784 }
9785
9786 /* We don't use SET_PT so that the point-motion hooks don't run. */
9787 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
9788 }
9789
9790 /* If any of the character widths specified in the display table
9791 have changed, invalidate the width run cache. It's true that
9792 this may be a bit late to catch such changes, but the rest of
9793 redisplay goes (non-fatally) haywire when the display table is
9794 changed, so why should we worry about doing any better? */
9795 if (current_buffer->width_run_cache)
9796 {
9797 struct Lisp_Char_Table *disptab = buffer_display_table ();
9798
9799 if (! disptab_matches_widthtab (disptab,
9800 XVECTOR (current_buffer->width_table)))
9801 {
9802 invalidate_region_cache (current_buffer,
9803 current_buffer->width_run_cache,
9804 BEG, Z);
9805 recompute_width_table (current_buffer, disptab);
9806 }
9807 }
9808
9809 /* If window-start is screwed up, choose a new one. */
9810 if (XMARKER (w->start)->buffer != current_buffer)
9811 goto recenter;
9812
9813 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9814
9815 /* If someone specified a new starting point but did not insist,
9816 check whether it can be used. */
9817 if (!NILP (w->optional_new_start)
9818 && CHARPOS (startp) >= BEGV
9819 && CHARPOS (startp) <= ZV)
9820 {
9821 w->optional_new_start = Qnil;
9822 start_display (&it, w, startp);
9823 move_it_to (&it, PT, 0, it.last_visible_y, -1,
9824 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9825 if (IT_CHARPOS (it) == PT)
9826 w->force_start = Qt;
9827 }
9828
9829 /* Handle case where place to start displaying has been specified,
9830 unless the specified location is outside the accessible range. */
9831 if (!NILP (w->force_start)
9832 || w->frozen_window_start_p)
9833 {
9834 w->force_start = Qnil;
9835 w->vscroll = 0;
9836 w->window_end_valid = Qnil;
9837
9838 /* Forget any recorded base line for line number display. */
9839 if (!current_matrix_up_to_date_p
9840 || current_buffer->clip_changed)
9841 w->base_line_number = Qnil;
9842
9843 /* Redisplay the mode line. Select the buffer properly for that.
9844 Also, run the hook window-scroll-functions
9845 because we have scrolled. */
9846 /* Note, we do this after clearing force_start because
9847 if there's an error, it is better to forget about force_start
9848 than to get into an infinite loop calling the hook functions
9849 and having them get more errors. */
9850 if (!update_mode_line
9851 || ! NILP (Vwindow_scroll_functions))
9852 {
9853 update_mode_line = 1;
9854 w->update_mode_line = Qt;
9855 startp = run_window_scroll_functions (window, startp);
9856 }
9857
9858 w->last_modified = make_number (0);
9859 w->last_overlay_modified = make_number (0);
9860 if (CHARPOS (startp) < BEGV)
9861 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
9862 else if (CHARPOS (startp) > ZV)
9863 SET_TEXT_POS (startp, ZV, ZV_BYTE);
9864
9865 /* Redisplay, then check if cursor has been set during the
9866 redisplay. Give up if new fonts were loaded. */
9867 if (!try_window (window, startp))
9868 {
9869 w->force_start = Qt;
9870 clear_glyph_matrix (w->desired_matrix);
9871 goto finish_scroll_bars;
9872 }
9873
9874 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
9875 {
9876 /* If point does not appear, try to move point so it does
9877 appear. The desired matrix has been built above, so we
9878 can use it here. */
9879 int window_height;
9880 struct glyph_row *row;
9881
9882 window_height = window_box_height (w) / 2;
9883 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
9884 while (MATRIX_ROW_BOTTOM_Y (row) < window_height)
9885 ++row;
9886
9887 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
9888 MATRIX_ROW_START_BYTEPOS (row));
9889
9890 if (w != XWINDOW (selected_window))
9891 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
9892 else if (current_buffer == old)
9893 SET_TEXT_POS (lpoint, PT, PT_BYTE);
9894
9895 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
9896
9897 /* If we are highlighting the region, then we just changed
9898 the region, so redisplay to show it. */
9899 if (!NILP (Vtransient_mark_mode)
9900 && !NILP (current_buffer->mark_active))
9901 {
9902 clear_glyph_matrix (w->desired_matrix);
9903 if (!try_window (window, startp))
9904 goto finish_scroll_bars;
9905 }
9906 }
9907
9908 make_cursor_line_fully_visible (w);
9909 #if GLYPH_DEBUG
9910 debug_method_add (w, "forced window start");
9911 #endif
9912 goto done;
9913 }
9914
9915 /* Handle case where text has not changed, only point, and it has
9916 not moved off the frame. */
9917 if (current_matrix_up_to_date_p
9918 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
9919 rc != 0))
9920 {
9921 if (rc == -1)
9922 goto try_to_scroll;
9923 else
9924 goto done;
9925 }
9926 /* If current starting point was originally the beginning of a line
9927 but no longer is, find a new starting point. */
9928 else if (!NILP (w->start_at_line_beg)
9929 && !(CHARPOS (startp) <= BEGV
9930 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
9931 {
9932 #if GLYPH_DEBUG
9933 debug_method_add (w, "recenter 1");
9934 #endif
9935 goto recenter;
9936 }
9937
9938 /* Try scrolling with try_window_id. */
9939 else if (/* Windows and buffers haven't changed. */
9940 !windows_or_buffers_changed
9941 /* Window must be either use window-based redisplay or
9942 be full width. */
9943 && (FRAME_WINDOW_P (f)
9944 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)))
9945 && !MINI_WINDOW_P (w)
9946 /* Point is not known NOT to appear in window. */
9947 && PT >= CHARPOS (startp)
9948 && XFASTINT (w->last_modified)
9949 /* Window is not hscrolled. */
9950 && XFASTINT (w->hscroll) == 0
9951 /* Selective display has not changed. */
9952 && !current_buffer->clip_changed
9953 /* Current matrix is up to date. */
9954 && !NILP (w->window_end_valid)
9955 /* Can't use this case if highlighting a region because
9956 a cursor movement will do more than just set the cursor. */
9957 && !(!NILP (Vtransient_mark_mode)
9958 && !NILP (current_buffer->mark_active))
9959 && NILP (w->region_showing)
9960 && NILP (Vshow_trailing_whitespace)
9961 /* Overlay arrow position and string not changed. */
9962 && EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
9963 && EQ (last_arrow_string, Voverlay_arrow_string)
9964 /* Value is > 0 if update has been done, it is -1 if we
9965 know that the same window start will not work. It is 0
9966 if unsuccessful for some other reason. */
9967 && (tem = try_window_id (w)) != 0)
9968 {
9969 #if GLYPH_DEBUG
9970 debug_method_add (w, "try_window_id %d", tem);
9971 #endif
9972
9973 if (fonts_changed_p)
9974 goto finish_scroll_bars;
9975 if (tem > 0)
9976 goto done;
9977
9978 /* Otherwise try_window_id has returned -1 which means that we
9979 don't want the alternative below this comment to execute. */
9980 }
9981 else if (CHARPOS (startp) >= BEGV
9982 && CHARPOS (startp) <= ZV
9983 && PT >= CHARPOS (startp)
9984 && (CHARPOS (startp) < ZV
9985 /* Avoid starting at end of buffer. */
9986 || CHARPOS (startp) == BEGV
9987 || (XFASTINT (w->last_modified) >= MODIFF
9988 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
9989 {
9990 #if GLYPH_DEBUG
9991 debug_method_add (w, "same window start");
9992 #endif
9993
9994 /* Try to redisplay starting at same place as before.
9995 If point has not moved off frame, accept the results. */
9996 if (!current_matrix_up_to_date_p
9997 /* Don't use try_window_reusing_current_matrix in this case
9998 because a window scroll function can have changed the
9999 buffer. */
10000 || !NILP (Vwindow_scroll_functions)
10001 || MINI_WINDOW_P (w)
10002 || !try_window_reusing_current_matrix (w))
10003 {
10004 IF_DEBUG (debug_method_add (w, "1"));
10005 try_window (window, startp);
10006 }
10007
10008 if (fonts_changed_p)
10009 goto finish_scroll_bars;
10010
10011 if (w->cursor.vpos >= 0)
10012 {
10013 if (!just_this_one_p
10014 || current_buffer->clip_changed
10015 || BEG_UNCHANGED < CHARPOS (startp))
10016 /* Forget any recorded base line for line number display. */
10017 w->base_line_number = Qnil;
10018
10019 make_cursor_line_fully_visible (w);
10020 goto done;
10021 }
10022 else
10023 clear_glyph_matrix (w->desired_matrix);
10024 }
10025
10026 try_to_scroll:
10027
10028 w->last_modified = make_number (0);
10029 w->last_overlay_modified = make_number (0);
10030
10031 /* Redisplay the mode line. Select the buffer properly for that. */
10032 if (!update_mode_line)
10033 {
10034 update_mode_line = 1;
10035 w->update_mode_line = Qt;
10036 }
10037
10038 /* Try to scroll by specified few lines. */
10039 if ((scroll_conservatively
10040 || scroll_step
10041 || temp_scroll_step
10042 || NUMBERP (current_buffer->scroll_up_aggressively)
10043 || NUMBERP (current_buffer->scroll_down_aggressively))
10044 && !current_buffer->clip_changed
10045 && CHARPOS (startp) >= BEGV
10046 && CHARPOS (startp) <= ZV)
10047 {
10048 /* The function returns -1 if new fonts were loaded, 1 if
10049 successful, 0 if not successful. */
10050 int rc = try_scrolling (window, just_this_one_p,
10051 scroll_conservatively,
10052 scroll_step,
10053 temp_scroll_step);
10054 if (rc > 0)
10055 goto done;
10056 else if (rc < 0)
10057 goto finish_scroll_bars;
10058 }
10059
10060 /* Finally, just choose place to start which centers point */
10061
10062 recenter:
10063
10064 #if GLYPH_DEBUG
10065 debug_method_add (w, "recenter");
10066 #endif
10067
10068 /* w->vscroll = 0; */
10069
10070 /* Forget any previously recorded base line for line number display. */
10071 if (!current_matrix_up_to_date_p
10072 || current_buffer->clip_changed)
10073 w->base_line_number = Qnil;
10074
10075 /* Move backward half the height of the window. */
10076 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10077 it.current_y = it.last_visible_y;
10078 move_it_vertically_backward (&it, it.last_visible_y / 2);
10079 xassert (IT_CHARPOS (it) >= BEGV);
10080
10081 /* The function move_it_vertically_backward may move over more
10082 than the specified y-distance. If it->w is small, e.g. a
10083 mini-buffer window, we may end up in front of the window's
10084 display area. Start displaying at the start of the line
10085 containing PT in this case. */
10086 if (it.current_y <= 0)
10087 {
10088 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10089 move_it_vertically (&it, 0);
10090 xassert (IT_CHARPOS (it) <= PT);
10091 it.current_y = 0;
10092 }
10093
10094 it.current_x = it.hpos = 0;
10095
10096 /* Set startp here explicitly in case that helps avoid an infinite loop
10097 in case the window-scroll-functions functions get errors. */
10098 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
10099
10100 /* Run scroll hooks. */
10101 startp = run_window_scroll_functions (window, it.current.pos);
10102
10103 /* Redisplay the window. */
10104 if (!current_matrix_up_to_date_p
10105 || windows_or_buffers_changed
10106 /* Don't use try_window_reusing_current_matrix in this case
10107 because it can have changed the buffer. */
10108 || !NILP (Vwindow_scroll_functions)
10109 || !just_this_one_p
10110 || MINI_WINDOW_P (w)
10111 || !try_window_reusing_current_matrix (w))
10112 try_window (window, startp);
10113
10114 /* If new fonts have been loaded (due to fontsets), give up. We
10115 have to start a new redisplay since we need to re-adjust glyph
10116 matrices. */
10117 if (fonts_changed_p)
10118 goto finish_scroll_bars;
10119
10120 /* If cursor did not appear assume that the middle of the window is
10121 in the first line of the window. Do it again with the next line.
10122 (Imagine a window of height 100, displaying two lines of height
10123 60. Moving back 50 from it->last_visible_y will end in the first
10124 line.) */
10125 if (w->cursor.vpos < 0)
10126 {
10127 if (!NILP (w->window_end_valid)
10128 && PT >= Z - XFASTINT (w->window_end_pos))
10129 {
10130 clear_glyph_matrix (w->desired_matrix);
10131 move_it_by_lines (&it, 1, 0);
10132 try_window (window, it.current.pos);
10133 }
10134 else if (PT < IT_CHARPOS (it))
10135 {
10136 clear_glyph_matrix (w->desired_matrix);
10137 move_it_by_lines (&it, -1, 0);
10138 try_window (window, it.current.pos);
10139 }
10140 else
10141 {
10142 /* Not much we can do about it. */
10143 }
10144 }
10145
10146 /* Consider the following case: Window starts at BEGV, there is
10147 invisible, intangible text at BEGV, so that display starts at
10148 some point START > BEGV. It can happen that we are called with
10149 PT somewhere between BEGV and START. Try to handle that case. */
10150 if (w->cursor.vpos < 0)
10151 {
10152 struct glyph_row *row = w->current_matrix->rows;
10153 if (row->mode_line_p)
10154 ++row;
10155 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10156 }
10157
10158 make_cursor_line_fully_visible (w);
10159
10160 done:
10161
10162 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10163 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
10164 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
10165 ? Qt : Qnil);
10166
10167 /* Display the mode line, if we must. */
10168 if ((update_mode_line
10169 /* If window not full width, must redo its mode line
10170 if (a) the window to its side is being redone and
10171 (b) we do a frame-based redisplay. This is a consequence
10172 of how inverted lines are drawn in frame-based redisplay. */
10173 || (!just_this_one_p
10174 && !FRAME_WINDOW_P (f)
10175 && !WINDOW_FULL_WIDTH_P (w))
10176 /* Line number to display. */
10177 || INTEGERP (w->base_line_pos)
10178 /* Column number is displayed and different from the one displayed. */
10179 || (!NILP (w->column_number_displayed)
10180 && XFASTINT (w->column_number_displayed) != current_column ()))
10181 /* This means that the window has a mode line. */
10182 && (WINDOW_WANTS_MODELINE_P (w)
10183 || WINDOW_WANTS_HEADER_LINE_P (w)))
10184 {
10185 Lisp_Object old_selected_frame;
10186
10187 old_selected_frame = selected_frame;
10188
10189 XSETFRAME (selected_frame, f);
10190 display_mode_lines (w);
10191 selected_frame = old_selected_frame;
10192
10193 /* If mode line height has changed, arrange for a thorough
10194 immediate redisplay using the correct mode line height. */
10195 if (WINDOW_WANTS_MODELINE_P (w)
10196 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
10197 {
10198 fonts_changed_p = 1;
10199 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
10200 = DESIRED_MODE_LINE_HEIGHT (w);
10201 }
10202
10203 /* If top line height has changed, arrange for a thorough
10204 immediate redisplay using the correct mode line height. */
10205 if (WINDOW_WANTS_HEADER_LINE_P (w)
10206 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
10207 {
10208 fonts_changed_p = 1;
10209 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
10210 = DESIRED_HEADER_LINE_HEIGHT (w);
10211 }
10212
10213 if (fonts_changed_p)
10214 goto finish_scroll_bars;
10215 }
10216
10217 if (!line_number_displayed
10218 && !BUFFERP (w->base_line_pos))
10219 {
10220 w->base_line_pos = Qnil;
10221 w->base_line_number = Qnil;
10222 }
10223
10224 finish_menu_bars:
10225
10226 /* When we reach a frame's selected window, redo the frame's menu bar. */
10227 if (update_mode_line
10228 && EQ (FRAME_SELECTED_WINDOW (f), window))
10229 {
10230 int redisplay_menu_p = 0;
10231
10232 if (FRAME_WINDOW_P (f))
10233 {
10234 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
10235 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
10236 #else
10237 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10238 #endif
10239 }
10240 else
10241 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10242
10243 if (redisplay_menu_p)
10244 display_menu_bar (w);
10245
10246 #ifdef HAVE_WINDOW_SYSTEM
10247 if (WINDOWP (f->tool_bar_window)
10248 && (FRAME_TOOL_BAR_LINES (f) > 0
10249 || auto_resize_tool_bars_p))
10250 redisplay_tool_bar (f);
10251 #endif
10252 }
10253
10254 finish_scroll_bars:
10255
10256 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
10257 {
10258 int start, end, whole;
10259
10260 /* Calculate the start and end positions for the current window.
10261 At some point, it would be nice to choose between scrollbars
10262 which reflect the whole buffer size, with special markers
10263 indicating narrowing, and scrollbars which reflect only the
10264 visible region.
10265
10266 Note that mini-buffers sometimes aren't displaying any text. */
10267 if (!MINI_WINDOW_P (w)
10268 || (w == XWINDOW (minibuf_window)
10269 && NILP (echo_area_buffer[0])))
10270 {
10271 whole = ZV - BEGV;
10272 start = marker_position (w->start) - BEGV;
10273 /* I don't think this is guaranteed to be right. For the
10274 moment, we'll pretend it is. */
10275 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
10276
10277 if (end < start)
10278 end = start;
10279 if (whole < (end - start))
10280 whole = end - start;
10281 }
10282 else
10283 start = end = whole = 0;
10284
10285 /* Indicate what this scroll bar ought to be displaying now. */
10286 set_vertical_scroll_bar_hook (w, end - start, whole, start);
10287
10288 /* Note that we actually used the scroll bar attached to this
10289 window, so it shouldn't be deleted at the end of redisplay. */
10290 redeem_scroll_bar_hook (w);
10291 }
10292
10293 /* Restore current_buffer and value of point in it. */
10294 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
10295 set_buffer_internal_1 (old);
10296 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
10297
10298 unbind_to (count, Qnil);
10299 }
10300
10301
10302 /* Build the complete desired matrix of WINDOW with a window start
10303 buffer position POS. Value is non-zero if successful. It is zero
10304 if fonts were loaded during redisplay which makes re-adjusting
10305 glyph matrices necessary. */
10306
10307 int
10308 try_window (window, pos)
10309 Lisp_Object window;
10310 struct text_pos pos;
10311 {
10312 struct window *w = XWINDOW (window);
10313 struct it it;
10314 struct glyph_row *last_text_row = NULL;
10315
10316 /* Make POS the new window start. */
10317 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
10318
10319 /* Mark cursor position as unknown. No overlay arrow seen. */
10320 w->cursor.vpos = -1;
10321 overlay_arrow_seen = 0;
10322
10323 /* Initialize iterator and info to start at POS. */
10324 start_display (&it, w, pos);
10325
10326 /* Display all lines of W. */
10327 while (it.current_y < it.last_visible_y)
10328 {
10329 if (display_line (&it))
10330 last_text_row = it.glyph_row - 1;
10331 if (fonts_changed_p)
10332 return 0;
10333 }
10334
10335 /* If bottom moved off end of frame, change mode line percentage. */
10336 if (XFASTINT (w->window_end_pos) <= 0
10337 && Z != IT_CHARPOS (it))
10338 w->update_mode_line = Qt;
10339
10340 /* Set window_end_pos to the offset of the last character displayed
10341 on the window from the end of current_buffer. Set
10342 window_end_vpos to its row number. */
10343 if (last_text_row)
10344 {
10345 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
10346 w->window_end_bytepos
10347 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10348 w->window_end_pos
10349 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10350 w->window_end_vpos
10351 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10352 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
10353 ->displays_text_p);
10354 }
10355 else
10356 {
10357 w->window_end_bytepos = 0;
10358 w->window_end_pos = w->window_end_vpos = make_number (0);
10359 }
10360
10361 /* But that is not valid info until redisplay finishes. */
10362 w->window_end_valid = Qnil;
10363 return 1;
10364 }
10365
10366
10367 \f
10368 /************************************************************************
10369 Window redisplay reusing current matrix when buffer has not changed
10370 ************************************************************************/
10371
10372 /* Try redisplay of window W showing an unchanged buffer with a
10373 different window start than the last time it was displayed by
10374 reusing its current matrix. Value is non-zero if successful.
10375 W->start is the new window start. */
10376
10377 static int
10378 try_window_reusing_current_matrix (w)
10379 struct window *w;
10380 {
10381 struct frame *f = XFRAME (w->frame);
10382 struct glyph_row *row, *bottom_row;
10383 struct it it;
10384 struct run run;
10385 struct text_pos start, new_start;
10386 int nrows_scrolled, i;
10387 struct glyph_row *last_text_row;
10388 struct glyph_row *last_reused_text_row;
10389 struct glyph_row *start_row;
10390 int start_vpos, min_y, max_y;
10391
10392 if (/* This function doesn't handle terminal frames. */
10393 !FRAME_WINDOW_P (f)
10394 /* Don't try to reuse the display if windows have been split
10395 or such. */
10396 || windows_or_buffers_changed)
10397 return 0;
10398
10399 /* Can't do this if region may have changed. */
10400 if ((!NILP (Vtransient_mark_mode)
10401 && !NILP (current_buffer->mark_active))
10402 || !NILP (w->region_showing)
10403 || !NILP (Vshow_trailing_whitespace))
10404 return 0;
10405
10406 /* If top-line visibility has changed, give up. */
10407 if (WINDOW_WANTS_HEADER_LINE_P (w)
10408 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
10409 return 0;
10410
10411 /* Give up if old or new display is scrolled vertically. We could
10412 make this function handle this, but right now it doesn't. */
10413 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10414 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
10415 return 0;
10416
10417 /* The variable new_start now holds the new window start. The old
10418 start `start' can be determined from the current matrix. */
10419 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
10420 start = start_row->start.pos;
10421 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
10422
10423 /* Clear the desired matrix for the display below. */
10424 clear_glyph_matrix (w->desired_matrix);
10425
10426 if (CHARPOS (new_start) <= CHARPOS (start))
10427 {
10428 int first_row_y;
10429
10430 IF_DEBUG (debug_method_add (w, "twu1"));
10431
10432 /* Display up to a row that can be reused. The variable
10433 last_text_row is set to the last row displayed that displays
10434 text. Note that it.vpos == 0 if or if not there is a
10435 header-line; it's not the same as the MATRIX_ROW_VPOS! */
10436 start_display (&it, w, new_start);
10437 first_row_y = it.current_y;
10438 w->cursor.vpos = -1;
10439 last_text_row = last_reused_text_row = NULL;
10440
10441 while (it.current_y < it.last_visible_y
10442 && IT_CHARPOS (it) < CHARPOS (start)
10443 && !fonts_changed_p)
10444 if (display_line (&it))
10445 last_text_row = it.glyph_row - 1;
10446
10447 /* A value of current_y < last_visible_y means that we stopped
10448 at the previous window start, which in turn means that we
10449 have at least one reusable row. */
10450 if (it.current_y < it.last_visible_y)
10451 {
10452 /* IT.vpos always starts from 0; it counts text lines. */
10453 nrows_scrolled = it.vpos;
10454
10455 /* Find PT if not already found in the lines displayed. */
10456 if (w->cursor.vpos < 0)
10457 {
10458 int dy = it.current_y - first_row_y;
10459
10460 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10461 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10462 {
10463 if (cursor_row_p (w, row))
10464 {
10465 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
10466 dy, nrows_scrolled);
10467 break;
10468 }
10469
10470 if (MATRIX_ROW_BOTTOM_Y (row) + dy >= it.last_visible_y)
10471 break;
10472
10473 ++row;
10474 }
10475
10476 /* Give up if point was not found. This shouldn't
10477 happen often; not more often than with try_window
10478 itself. */
10479 if (w->cursor.vpos < 0)
10480 {
10481 clear_glyph_matrix (w->desired_matrix);
10482 return 0;
10483 }
10484 }
10485
10486 /* Scroll the display. Do it before the current matrix is
10487 changed. The problem here is that update has not yet
10488 run, i.e. part of the current matrix is not up to date.
10489 scroll_run_hook will clear the cursor, and use the
10490 current matrix to get the height of the row the cursor is
10491 in. */
10492 run.current_y = first_row_y;
10493 run.desired_y = it.current_y;
10494 run.height = it.last_visible_y - it.current_y;
10495
10496 if (run.height > 0 && run.current_y != run.desired_y)
10497 {
10498 update_begin (f);
10499 rif->update_window_begin_hook (w);
10500 rif->clear_mouse_face (w);
10501 rif->scroll_run_hook (w, &run);
10502 rif->update_window_end_hook (w, 0, 0);
10503 update_end (f);
10504 }
10505
10506 /* Shift current matrix down by nrows_scrolled lines. */
10507 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
10508 rotate_matrix (w->current_matrix,
10509 start_vpos,
10510 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10511 nrows_scrolled);
10512
10513 /* Disable lines that must be updated. */
10514 for (i = 0; i < it.vpos; ++i)
10515 (start_row + i)->enabled_p = 0;
10516
10517 /* Re-compute Y positions. */
10518 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10519 max_y = it.last_visible_y;
10520 for (row = start_row + nrows_scrolled;
10521 row < bottom_row;
10522 ++row)
10523 {
10524 row->y = it.current_y;
10525
10526 if (row->y < min_y)
10527 row->visible_height = row->height - (min_y - row->y);
10528 else if (row->y + row->height > max_y)
10529 row->visible_height
10530 = row->height - (row->y + row->height - max_y);
10531 else
10532 row->visible_height = row->height;
10533
10534 it.current_y += row->height;
10535
10536 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10537 last_reused_text_row = row;
10538 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
10539 break;
10540 }
10541
10542 /* Disable lines in the current matrix which are now
10543 below the window. */
10544 for (++row; row < bottom_row; ++row)
10545 row->enabled_p = 0;
10546 }
10547
10548 /* Update window_end_pos etc.; last_reused_text_row is the last
10549 reused row from the current matrix containing text, if any.
10550 The value of last_text_row is the last displayed line
10551 containing text. */
10552 if (last_reused_text_row)
10553 {
10554 w->window_end_bytepos
10555 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
10556 w->window_end_pos
10557 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
10558 w->window_end_vpos
10559 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
10560 w->current_matrix));
10561 }
10562 else if (last_text_row)
10563 {
10564 w->window_end_bytepos
10565 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10566 w->window_end_pos
10567 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10568 w->window_end_vpos
10569 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10570 }
10571 else
10572 {
10573 /* This window must be completely empty. */
10574 w->window_end_bytepos = 0;
10575 w->window_end_pos = w->window_end_vpos = make_number (0);
10576 }
10577 w->window_end_valid = Qnil;
10578
10579 /* Update hint: don't try scrolling again in update_window. */
10580 w->desired_matrix->no_scrolling_p = 1;
10581
10582 #if GLYPH_DEBUG
10583 debug_method_add (w, "try_window_reusing_current_matrix 1");
10584 #endif
10585 return 1;
10586 }
10587 else if (CHARPOS (new_start) > CHARPOS (start))
10588 {
10589 struct glyph_row *pt_row, *row;
10590 struct glyph_row *first_reusable_row;
10591 struct glyph_row *first_row_to_display;
10592 int dy;
10593 int yb = window_text_bottom_y (w);
10594
10595 /* Find the row starting at new_start, if there is one. Don't
10596 reuse a partially visible line at the end. */
10597 first_reusable_row = start_row;
10598 while (first_reusable_row->enabled_p
10599 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
10600 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
10601 < CHARPOS (new_start)))
10602 ++first_reusable_row;
10603
10604 /* Give up if there is no row to reuse. */
10605 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
10606 || !first_reusable_row->enabled_p
10607 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
10608 != CHARPOS (new_start)))
10609 return 0;
10610
10611 /* We can reuse fully visible rows beginning with
10612 first_reusable_row to the end of the window. Set
10613 first_row_to_display to the first row that cannot be reused.
10614 Set pt_row to the row containing point, if there is any. */
10615 pt_row = NULL;
10616 for (first_row_to_display = first_reusable_row;
10617 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
10618 ++first_row_to_display)
10619 {
10620 if (cursor_row_p (w, first_row_to_display))
10621 pt_row = first_row_to_display;
10622 }
10623
10624 /* Start displaying at the start of first_row_to_display. */
10625 xassert (first_row_to_display->y < yb);
10626 init_to_row_start (&it, w, first_row_to_display);
10627 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
10628 - start_vpos);
10629 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
10630 - nrows_scrolled);
10631 it.current_y = (first_row_to_display->y - first_reusable_row->y
10632 + WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
10633
10634 /* Display lines beginning with first_row_to_display in the
10635 desired matrix. Set last_text_row to the last row displayed
10636 that displays text. */
10637 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
10638 if (pt_row == NULL)
10639 w->cursor.vpos = -1;
10640 last_text_row = NULL;
10641 while (it.current_y < it.last_visible_y && !fonts_changed_p)
10642 if (display_line (&it))
10643 last_text_row = it.glyph_row - 1;
10644
10645 /* Give up If point isn't in a row displayed or reused. */
10646 if (w->cursor.vpos < 0)
10647 {
10648 clear_glyph_matrix (w->desired_matrix);
10649 return 0;
10650 }
10651
10652 /* If point is in a reused row, adjust y and vpos of the cursor
10653 position. */
10654 if (pt_row)
10655 {
10656 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
10657 w->current_matrix);
10658 w->cursor.y -= first_reusable_row->y;
10659 }
10660
10661 /* Scroll the display. */
10662 run.current_y = first_reusable_row->y;
10663 run.desired_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10664 run.height = it.last_visible_y - run.current_y;
10665 dy = run.current_y - run.desired_y;
10666
10667 if (run.height)
10668 {
10669 struct frame *f = XFRAME (WINDOW_FRAME (w));
10670 update_begin (f);
10671 rif->update_window_begin_hook (w);
10672 rif->clear_mouse_face (w);
10673 rif->scroll_run_hook (w, &run);
10674 rif->update_window_end_hook (w, 0, 0);
10675 update_end (f);
10676 }
10677
10678 /* Adjust Y positions of reused rows. */
10679 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
10680 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10681 max_y = it.last_visible_y;
10682 for (row = first_reusable_row; row < first_row_to_display; ++row)
10683 {
10684 row->y -= dy;
10685 if (row->y < min_y)
10686 row->visible_height = row->height - (min_y - row->y);
10687 else if (row->y + row->height > max_y)
10688 row->visible_height
10689 = row->height - (row->y + row->height - max_y);
10690 else
10691 row->visible_height = row->height;
10692 }
10693
10694 /* Scroll the current matrix. */
10695 xassert (nrows_scrolled > 0);
10696 rotate_matrix (w->current_matrix,
10697 start_vpos,
10698 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10699 -nrows_scrolled);
10700
10701 /* Disable rows not reused. */
10702 for (row -= nrows_scrolled; row < bottom_row; ++row)
10703 row->enabled_p = 0;
10704
10705 /* Adjust window end. A null value of last_text_row means that
10706 the window end is in reused rows which in turn means that
10707 only its vpos can have changed. */
10708 if (last_text_row)
10709 {
10710 w->window_end_bytepos
10711 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10712 w->window_end_pos
10713 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10714 w->window_end_vpos
10715 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10716 }
10717 else
10718 {
10719 w->window_end_vpos
10720 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
10721 }
10722
10723 w->window_end_valid = Qnil;
10724 w->desired_matrix->no_scrolling_p = 1;
10725
10726 #if GLYPH_DEBUG
10727 debug_method_add (w, "try_window_reusing_current_matrix 2");
10728 #endif
10729 return 1;
10730 }
10731
10732 return 0;
10733 }
10734
10735
10736 \f
10737 /************************************************************************
10738 Window redisplay reusing current matrix when buffer has changed
10739 ************************************************************************/
10740
10741 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
10742 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
10743 int *, int *));
10744 static struct glyph_row *
10745 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
10746 struct glyph_row *));
10747
10748
10749 /* Return the last row in MATRIX displaying text. If row START is
10750 non-null, start searching with that row. IT gives the dimensions
10751 of the display. Value is null if matrix is empty; otherwise it is
10752 a pointer to the row found. */
10753
10754 static struct glyph_row *
10755 find_last_row_displaying_text (matrix, it, start)
10756 struct glyph_matrix *matrix;
10757 struct it *it;
10758 struct glyph_row *start;
10759 {
10760 struct glyph_row *row, *row_found;
10761
10762 /* Set row_found to the last row in IT->w's current matrix
10763 displaying text. The loop looks funny but think of partially
10764 visible lines. */
10765 row_found = NULL;
10766 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
10767 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10768 {
10769 xassert (row->enabled_p);
10770 row_found = row;
10771 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
10772 break;
10773 ++row;
10774 }
10775
10776 return row_found;
10777 }
10778
10779
10780 /* Return the last row in the current matrix of W that is not affected
10781 by changes at the start of current_buffer that occurred since the
10782 last time W was redisplayed. Value is null if no such row exists.
10783
10784 The global variable beg_unchanged has to contain the number of
10785 bytes unchanged at the start of current_buffer. BEG +
10786 beg_unchanged is the buffer position of the first changed byte in
10787 current_buffer. Characters at positions < BEG + beg_unchanged are
10788 at the same buffer positions as they were when the current matrix
10789 was built. */
10790
10791 static struct glyph_row *
10792 find_last_unchanged_at_beg_row (w)
10793 struct window *w;
10794 {
10795 int first_changed_pos = BEG + BEG_UNCHANGED;
10796 struct glyph_row *row;
10797 struct glyph_row *row_found = NULL;
10798 int yb = window_text_bottom_y (w);
10799
10800 /* Find the last row displaying unchanged text. */
10801 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10802 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
10803 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
10804 {
10805 if (/* If row ends before first_changed_pos, it is unchanged,
10806 except in some case. */
10807 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
10808 /* When row ends in ZV and we write at ZV it is not
10809 unchanged. */
10810 && !row->ends_at_zv_p
10811 /* When first_changed_pos is the end of a continued line,
10812 row is not unchanged because it may be no longer
10813 continued. */
10814 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
10815 && row->continued_p))
10816 row_found = row;
10817
10818 /* Stop if last visible row. */
10819 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
10820 break;
10821
10822 ++row;
10823 }
10824
10825 return row_found;
10826 }
10827
10828
10829 /* Find the first glyph row in the current matrix of W that is not
10830 affected by changes at the end of current_buffer since the last
10831 time the window was redisplayed. Return in *DELTA the number of
10832 chars by which buffer positions in unchanged text at the end of
10833 current_buffer must be adjusted. Return in *DELTA_BYTES the
10834 corresponding number of bytes. Value is null if no such row
10835 exists, i.e. all rows are affected by changes. */
10836
10837 static struct glyph_row *
10838 find_first_unchanged_at_end_row (w, delta, delta_bytes)
10839 struct window *w;
10840 int *delta, *delta_bytes;
10841 {
10842 struct glyph_row *row;
10843 struct glyph_row *row_found = NULL;
10844
10845 *delta = *delta_bytes = 0;
10846
10847 /* Display must not have been paused, otherwise the current matrix
10848 is not up to date. */
10849 if (NILP (w->window_end_valid))
10850 abort ();
10851
10852 /* A value of window_end_pos >= END_UNCHANGED means that the window
10853 end is in the range of changed text. If so, there is no
10854 unchanged row at the end of W's current matrix. */
10855 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
10856 return NULL;
10857
10858 /* Set row to the last row in W's current matrix displaying text. */
10859 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
10860
10861 /* If matrix is entirely empty, no unchanged row exists. */
10862 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10863 {
10864 /* The value of row is the last glyph row in the matrix having a
10865 meaningful buffer position in it. The end position of row
10866 corresponds to window_end_pos. This allows us to translate
10867 buffer positions in the current matrix to current buffer
10868 positions for characters not in changed text. */
10869 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
10870 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
10871 int last_unchanged_pos, last_unchanged_pos_old;
10872 struct glyph_row *first_text_row
10873 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10874
10875 *delta = Z - Z_old;
10876 *delta_bytes = Z_BYTE - Z_BYTE_old;
10877
10878 /* Set last_unchanged_pos to the buffer position of the last
10879 character in the buffer that has not been changed. Z is the
10880 index + 1 of the last byte in current_buffer, i.e. by
10881 subtracting end_unchanged we get the index of the last
10882 unchanged character, and we have to add BEG to get its buffer
10883 position. */
10884 last_unchanged_pos = Z - END_UNCHANGED + BEG;
10885 last_unchanged_pos_old = last_unchanged_pos - *delta;
10886
10887 /* Search backward from ROW for a row displaying a line that
10888 starts at a minimum position >= last_unchanged_pos_old. */
10889 for (; row > first_text_row; --row)
10890 {
10891 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
10892 abort ();
10893
10894 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
10895 row_found = row;
10896 }
10897 }
10898
10899 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
10900 abort ();
10901
10902 return row_found;
10903 }
10904
10905
10906 /* Make sure that glyph rows in the current matrix of window W
10907 reference the same glyph memory as corresponding rows in the
10908 frame's frame matrix. This function is called after scrolling W's
10909 current matrix on a terminal frame in try_window_id and
10910 try_window_reusing_current_matrix. */
10911
10912 static void
10913 sync_frame_with_window_matrix_rows (w)
10914 struct window *w;
10915 {
10916 struct frame *f = XFRAME (w->frame);
10917 struct glyph_row *window_row, *window_row_end, *frame_row;
10918
10919 /* Preconditions: W must be a leaf window and full-width. Its frame
10920 must have a frame matrix. */
10921 xassert (NILP (w->hchild) && NILP (w->vchild));
10922 xassert (WINDOW_FULL_WIDTH_P (w));
10923 xassert (!FRAME_WINDOW_P (f));
10924
10925 /* If W is a full-width window, glyph pointers in W's current matrix
10926 have, by definition, to be the same as glyph pointers in the
10927 corresponding frame matrix. */
10928 window_row = w->current_matrix->rows;
10929 window_row_end = window_row + w->current_matrix->nrows;
10930 frame_row = f->current_matrix->rows + XFASTINT (w->top);
10931 while (window_row < window_row_end)
10932 {
10933 int area;
10934
10935 for (area = LEFT_MARGIN_AREA; area <= LAST_AREA; ++area)
10936 frame_row->glyphs[area] = window_row->glyphs[area];
10937
10938 /* Disable frame rows whose corresponding window rows have
10939 been disabled in try_window_id. */
10940 if (!window_row->enabled_p)
10941 frame_row->enabled_p = 0;
10942
10943 ++window_row, ++frame_row;
10944 }
10945 }
10946
10947
10948 /* Find the glyph row in window W containing CHARPOS. Consider all
10949 rows between START and END (not inclusive). END null means search
10950 all rows to the end of the display area of W. Value is the row
10951 containing CHARPOS or null. */
10952
10953 static struct glyph_row *
10954 row_containing_pos (w, charpos, start, end)
10955 struct window *w;
10956 int charpos;
10957 struct glyph_row *start, *end;
10958 {
10959 struct glyph_row *row = start;
10960 int last_y;
10961
10962 /* If we happen to start on a header-line, skip that. */
10963 if (row->mode_line_p)
10964 ++row;
10965
10966 if ((end && row >= end) || !row->enabled_p)
10967 return NULL;
10968
10969 last_y = window_text_bottom_y (w);
10970
10971 while ((end == NULL || row < end)
10972 && (MATRIX_ROW_END_CHARPOS (row) < charpos
10973 /* The end position of a row equals the start
10974 position of the next row. If CHARPOS is there, we
10975 would rather display it in the next line, except
10976 when this line ends in ZV. */
10977 || (MATRIX_ROW_END_CHARPOS (row) == charpos
10978 && (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
10979 || !row->ends_at_zv_p)))
10980 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
10981 ++row;
10982
10983 /* Give up if CHARPOS not found. */
10984 if ((end && row >= end)
10985 || charpos < MATRIX_ROW_START_CHARPOS (row)
10986 || charpos > MATRIX_ROW_END_CHARPOS (row))
10987 row = NULL;
10988
10989 return row;
10990 }
10991
10992
10993 /* Try to redisplay window W by reusing its existing display. W's
10994 current matrix must be up to date when this function is called,
10995 i.e. window_end_valid must not be nil.
10996
10997 Value is
10998
10999 1 if display has been updated
11000 0 if otherwise unsuccessful
11001 -1 if redisplay with same window start is known not to succeed
11002
11003 The following steps are performed:
11004
11005 1. Find the last row in the current matrix of W that is not
11006 affected by changes at the start of current_buffer. If no such row
11007 is found, give up.
11008
11009 2. Find the first row in W's current matrix that is not affected by
11010 changes at the end of current_buffer. Maybe there is no such row.
11011
11012 3. Display lines beginning with the row + 1 found in step 1 to the
11013 row found in step 2 or, if step 2 didn't find a row, to the end of
11014 the window.
11015
11016 4. If cursor is not known to appear on the window, give up.
11017
11018 5. If display stopped at the row found in step 2, scroll the
11019 display and current matrix as needed.
11020
11021 6. Maybe display some lines at the end of W, if we must. This can
11022 happen under various circumstances, like a partially visible line
11023 becoming fully visible, or because newly displayed lines are displayed
11024 in smaller font sizes.
11025
11026 7. Update W's window end information. */
11027
11028 /* Check that window end is what we expect it to be. */
11029
11030 static int
11031 try_window_id (w)
11032 struct window *w;
11033 {
11034 struct frame *f = XFRAME (w->frame);
11035 struct glyph_matrix *current_matrix = w->current_matrix;
11036 struct glyph_matrix *desired_matrix = w->desired_matrix;
11037 struct glyph_row *last_unchanged_at_beg_row;
11038 struct glyph_row *first_unchanged_at_end_row;
11039 struct glyph_row *row;
11040 struct glyph_row *bottom_row;
11041 int bottom_vpos;
11042 struct it it;
11043 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
11044 struct text_pos start_pos;
11045 struct run run;
11046 int first_unchanged_at_end_vpos = 0;
11047 struct glyph_row *last_text_row, *last_text_row_at_end;
11048 struct text_pos start;
11049
11050 SET_TEXT_POS_FROM_MARKER (start, w->start);
11051
11052 /* Check pre-conditions. Window end must be valid, otherwise
11053 the current matrix would not be up to date. */
11054 xassert (!NILP (w->window_end_valid));
11055 xassert (FRAME_WINDOW_P (XFRAME (w->frame))
11056 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)));
11057
11058 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
11059 only if buffer has really changed. The reason is that the gap is
11060 initially at Z for freshly visited files. The code below would
11061 set end_unchanged to 0 in that case. */
11062 if (MODIFF > SAVE_MODIFF
11063 /* This seems to happen sometimes after saving a buffer. */
11064 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
11065 {
11066 if (GPT - BEG < BEG_UNCHANGED)
11067 BEG_UNCHANGED = GPT - BEG;
11068 if (Z - GPT < END_UNCHANGED)
11069 END_UNCHANGED = Z - GPT;
11070 }
11071
11072 /* If window starts after a line end, and the last change is in
11073 front of that newline, then changes don't affect the display.
11074 This case happens with stealth-fontification. Note that although
11075 the display is unchanged, glyph positions in the matrix have to
11076 be adjusted, of course. */
11077 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
11078 if (CHARPOS (start) > BEGV
11079 && Z - END_UNCHANGED < CHARPOS (start) - 1
11080 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n'
11081 && PT < MATRIX_ROW_END_CHARPOS (row))
11082 {
11083 struct glyph_row *r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
11084 int delta = CHARPOS (start) - MATRIX_ROW_START_CHARPOS (r0);
11085
11086 if (delta)
11087 {
11088 struct glyph_row *r1 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11089 int delta_bytes = BYTEPOS (start) - MATRIX_ROW_START_BYTEPOS (r0);
11090
11091 increment_matrix_positions (w->current_matrix,
11092 MATRIX_ROW_VPOS (r0, current_matrix),
11093 MATRIX_ROW_VPOS (r1, current_matrix),
11094 delta, delta_bytes);
11095 }
11096
11097 #if 0 /* If changes are all in front of the window start, the
11098 distance of the last displayed glyph from Z hasn't
11099 changed. */
11100 w->window_end_pos
11101 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
11102 w->window_end_bytepos
11103 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11104 #endif
11105
11106 row = row_containing_pos (w, PT, r0, NULL);
11107 if (row == NULL)
11108 return 0;
11109
11110 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11111 return 1;
11112 }
11113
11114 /* Return quickly if changes are all below what is displayed in the
11115 window, and if PT is in the window. */
11116 if (BEG_UNCHANGED > MATRIX_ROW_END_CHARPOS (row)
11117 && PT < MATRIX_ROW_END_CHARPOS (row))
11118 {
11119 /* We have to update window end positions because the buffer's
11120 size has changed. */
11121 w->window_end_pos
11122 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
11123 w->window_end_bytepos
11124 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11125
11126 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11127 row = row_containing_pos (w, PT, row, NULL);
11128 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11129 return 2;
11130 }
11131
11132 /* Check that window start agrees with the start of the first glyph
11133 row in its current matrix. Check this after we know the window
11134 start is not in changed text, otherwise positions would not be
11135 comparable. */
11136 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11137 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
11138 return 0;
11139
11140 /* Compute the position at which we have to start displaying new
11141 lines. Some of the lines at the top of the window might be
11142 reusable because they are not displaying changed text. Find the
11143 last row in W's current matrix not affected by changes at the
11144 start of current_buffer. Value is null if changes start in the
11145 first line of window. */
11146 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
11147 if (last_unchanged_at_beg_row)
11148 {
11149 /* Avoid starting to display in the moddle of a character, a TAB
11150 for instance. This is easier than to set up the iterator
11151 exactly, and it's not a frequent case, so the additional
11152 effort wouldn't really pay off. */
11153 while (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
11154 && last_unchanged_at_beg_row > w->current_matrix->rows)
11155 --last_unchanged_at_beg_row;
11156
11157 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
11158 return 0;
11159
11160 init_to_row_end (&it, w, last_unchanged_at_beg_row);
11161 start_pos = it.current.pos;
11162
11163 /* Start displaying new lines in the desired matrix at the same
11164 vpos we would use in the current matrix, i.e. below
11165 last_unchanged_at_beg_row. */
11166 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
11167 current_matrix);
11168 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11169 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
11170
11171 xassert (it.hpos == 0 && it.current_x == 0);
11172 }
11173 else
11174 {
11175 /* There are no reusable lines at the start of the window.
11176 Start displaying in the first line. */
11177 start_display (&it, w, start);
11178 start_pos = it.current.pos;
11179 }
11180
11181 /* Find the first row that is not affected by changes at the end of
11182 the buffer. Value will be null if there is no unchanged row, in
11183 which case we must redisplay to the end of the window. delta
11184 will be set to the value by which buffer positions beginning with
11185 first_unchanged_at_end_row have to be adjusted due to text
11186 changes. */
11187 first_unchanged_at_end_row
11188 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
11189 IF_DEBUG (debug_delta = delta);
11190 IF_DEBUG (debug_delta_bytes = delta_bytes);
11191
11192 /* Set stop_pos to the buffer position up to which we will have to
11193 display new lines. If first_unchanged_at_end_row != NULL, this
11194 is the buffer position of the start of the line displayed in that
11195 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
11196 that we don't stop at a buffer position. */
11197 stop_pos = 0;
11198 if (first_unchanged_at_end_row)
11199 {
11200 xassert (last_unchanged_at_beg_row == NULL
11201 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
11202
11203 /* If this is a continuation line, move forward to the next one
11204 that isn't. Changes in lines above affect this line.
11205 Caution: this may move first_unchanged_at_end_row to a row
11206 not displaying text. */
11207 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
11208 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11209 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11210 < it.last_visible_y))
11211 ++first_unchanged_at_end_row;
11212
11213 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11214 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11215 >= it.last_visible_y))
11216 first_unchanged_at_end_row = NULL;
11217 else
11218 {
11219 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
11220 + delta);
11221 first_unchanged_at_end_vpos
11222 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
11223 xassert (stop_pos >= Z - END_UNCHANGED);
11224 }
11225 }
11226 else if (last_unchanged_at_beg_row == NULL)
11227 return 0;
11228
11229
11230 #if GLYPH_DEBUG
11231
11232 /* Either there is no unchanged row at the end, or the one we have
11233 now displays text. This is a necessary condition for the window
11234 end pos calculation at the end of this function. */
11235 xassert (first_unchanged_at_end_row == NULL
11236 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
11237
11238 debug_last_unchanged_at_beg_vpos
11239 = (last_unchanged_at_beg_row
11240 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
11241 : -1);
11242 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
11243
11244 #endif /* GLYPH_DEBUG != 0 */
11245
11246
11247 /* Display new lines. Set last_text_row to the last new line
11248 displayed which has text on it, i.e. might end up as being the
11249 line where the window_end_vpos is. */
11250 w->cursor.vpos = -1;
11251 last_text_row = NULL;
11252 overlay_arrow_seen = 0;
11253 while (it.current_y < it.last_visible_y
11254 && !fonts_changed_p
11255 && (first_unchanged_at_end_row == NULL
11256 || IT_CHARPOS (it) < stop_pos))
11257 {
11258 if (display_line (&it))
11259 last_text_row = it.glyph_row - 1;
11260 }
11261
11262 if (fonts_changed_p)
11263 return -1;
11264
11265
11266 /* Compute differences in buffer positions, y-positions etc. for
11267 lines reused at the bottom of the window. Compute what we can
11268 scroll. */
11269 if (first_unchanged_at_end_row
11270 /* No lines reused because we displayed everything up to the
11271 bottom of the window. */
11272 && it.current_y < it.last_visible_y)
11273 {
11274 dvpos = (it.vpos
11275 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
11276 current_matrix));
11277 dy = it.current_y - first_unchanged_at_end_row->y;
11278 run.current_y = first_unchanged_at_end_row->y;
11279 run.desired_y = run.current_y + dy;
11280 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
11281 }
11282 else
11283 {
11284 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
11285 first_unchanged_at_end_row = NULL;
11286 }
11287 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
11288
11289
11290 /* Find the cursor if not already found. We have to decide whether
11291 PT will appear on this window (it sometimes doesn't, but this is
11292 not a very frequent case.) This decision has to be made before
11293 the current matrix is altered. A value of cursor.vpos < 0 means
11294 that PT is either in one of the lines beginning at
11295 first_unchanged_at_end_row or below the window. Don't care for
11296 lines that might be displayed later at the window end; as
11297 mentioned, this is not a frequent case. */
11298 if (w->cursor.vpos < 0)
11299 {
11300 /* Cursor in unchanged rows at the top? */
11301 if (PT < CHARPOS (start_pos)
11302 && last_unchanged_at_beg_row)
11303 {
11304 row = row_containing_pos (w, PT,
11305 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
11306 last_unchanged_at_beg_row + 1);
11307 if (row)
11308 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11309 }
11310
11311 /* Start from first_unchanged_at_end_row looking for PT. */
11312 else if (first_unchanged_at_end_row)
11313 {
11314 row = row_containing_pos (w, PT - delta,
11315 first_unchanged_at_end_row, NULL);
11316 if (row)
11317 set_cursor_from_row (w, row, w->current_matrix, delta,
11318 delta_bytes, dy, dvpos);
11319 }
11320
11321 /* Give up if cursor was not found. */
11322 if (w->cursor.vpos < 0)
11323 {
11324 clear_glyph_matrix (w->desired_matrix);
11325 return -1;
11326 }
11327 }
11328
11329 /* Don't let the cursor end in the scroll margins. */
11330 {
11331 int this_scroll_margin, cursor_height;
11332
11333 this_scroll_margin = max (0, scroll_margin);
11334 this_scroll_margin = min (this_scroll_margin,
11335 XFASTINT (w->height) / 4);
11336 this_scroll_margin *= CANON_Y_UNIT (it.f);
11337 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
11338
11339 if ((w->cursor.y < this_scroll_margin
11340 && CHARPOS (start) > BEGV)
11341 /* Don't take scroll margin into account at the bottom because
11342 old redisplay didn't do it either. */
11343 || w->cursor.y + cursor_height > it.last_visible_y)
11344 {
11345 w->cursor.vpos = -1;
11346 clear_glyph_matrix (w->desired_matrix);
11347 return -1;
11348 }
11349 }
11350
11351 /* Scroll the display. Do it before changing the current matrix so
11352 that xterm.c doesn't get confused about where the cursor glyph is
11353 found. */
11354 if (dy && run.height)
11355 {
11356 update_begin (f);
11357
11358 if (FRAME_WINDOW_P (f))
11359 {
11360 rif->update_window_begin_hook (w);
11361 rif->clear_mouse_face (w);
11362 rif->scroll_run_hook (w, &run);
11363 rif->update_window_end_hook (w, 0, 0);
11364 }
11365 else
11366 {
11367 /* Terminal frame. In this case, dvpos gives the number of
11368 lines to scroll by; dvpos < 0 means scroll up. */
11369 int first_unchanged_at_end_vpos
11370 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
11371 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
11372 int end = XFASTINT (w->top) + window_internal_height (w);
11373
11374 /* Perform the operation on the screen. */
11375 if (dvpos > 0)
11376 {
11377 /* Scroll last_unchanged_at_beg_row to the end of the
11378 window down dvpos lines. */
11379 set_terminal_window (end);
11380
11381 /* On dumb terminals delete dvpos lines at the end
11382 before inserting dvpos empty lines. */
11383 if (!scroll_region_ok)
11384 ins_del_lines (end - dvpos, -dvpos);
11385
11386 /* Insert dvpos empty lines in front of
11387 last_unchanged_at_beg_row. */
11388 ins_del_lines (from, dvpos);
11389 }
11390 else if (dvpos < 0)
11391 {
11392 /* Scroll up last_unchanged_at_beg_vpos to the end of
11393 the window to last_unchanged_at_beg_vpos - |dvpos|. */
11394 set_terminal_window (end);
11395
11396 /* Delete dvpos lines in front of
11397 last_unchanged_at_beg_vpos. ins_del_lines will set
11398 the cursor to the given vpos and emit |dvpos| delete
11399 line sequences. */
11400 ins_del_lines (from + dvpos, dvpos);
11401
11402 /* On a dumb terminal insert dvpos empty lines at the
11403 end. */
11404 if (!scroll_region_ok)
11405 ins_del_lines (end + dvpos, -dvpos);
11406 }
11407
11408 set_terminal_window (0);
11409 }
11410
11411 update_end (f);
11412 }
11413
11414 /* Shift reused rows of the current matrix to the right position.
11415 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
11416 text. */
11417 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11418 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
11419 if (dvpos < 0)
11420 {
11421 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
11422 bottom_vpos, dvpos);
11423 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
11424 bottom_vpos, 0);
11425 }
11426 else if (dvpos > 0)
11427 {
11428 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
11429 bottom_vpos, dvpos);
11430 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
11431 first_unchanged_at_end_vpos + dvpos, 0);
11432 }
11433
11434 /* For frame-based redisplay, make sure that current frame and window
11435 matrix are in sync with respect to glyph memory. */
11436 if (!FRAME_WINDOW_P (f))
11437 sync_frame_with_window_matrix_rows (w);
11438
11439 /* Adjust buffer positions in reused rows. */
11440 if (delta)
11441 increment_matrix_positions (current_matrix,
11442 first_unchanged_at_end_vpos + dvpos,
11443 bottom_vpos, delta, delta_bytes);
11444
11445 /* Adjust Y positions. */
11446 if (dy)
11447 shift_glyph_matrix (w, current_matrix,
11448 first_unchanged_at_end_vpos + dvpos,
11449 bottom_vpos, dy);
11450
11451 if (first_unchanged_at_end_row)
11452 first_unchanged_at_end_row += dvpos;
11453
11454 /* If scrolling up, there may be some lines to display at the end of
11455 the window. */
11456 last_text_row_at_end = NULL;
11457 if (dy < 0)
11458 {
11459 /* Set last_row to the glyph row in the current matrix where the
11460 window end line is found. It has been moved up or down in
11461 the matrix by dvpos. */
11462 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
11463 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
11464
11465 /* If last_row is the window end line, it should display text. */
11466 xassert (last_row->displays_text_p);
11467
11468 /* If window end line was partially visible before, begin
11469 displaying at that line. Otherwise begin displaying with the
11470 line following it. */
11471 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
11472 {
11473 init_to_row_start (&it, w, last_row);
11474 it.vpos = last_vpos;
11475 it.current_y = last_row->y;
11476 }
11477 else
11478 {
11479 init_to_row_end (&it, w, last_row);
11480 it.vpos = 1 + last_vpos;
11481 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
11482 ++last_row;
11483 }
11484
11485 /* We may start in a continuation line. If so, we have to get
11486 the right continuation_lines_width and current_x. */
11487 it.continuation_lines_width = last_row->continuation_lines_width;
11488 it.hpos = it.current_x = 0;
11489
11490 /* Display the rest of the lines at the window end. */
11491 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11492 while (it.current_y < it.last_visible_y
11493 && !fonts_changed_p)
11494 {
11495 /* Is it always sure that the display agrees with lines in
11496 the current matrix? I don't think so, so we mark rows
11497 displayed invalid in the current matrix by setting their
11498 enabled_p flag to zero. */
11499 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
11500 if (display_line (&it))
11501 last_text_row_at_end = it.glyph_row - 1;
11502 }
11503 }
11504
11505 /* Update window_end_pos and window_end_vpos. */
11506 if (first_unchanged_at_end_row
11507 && first_unchanged_at_end_row->y < it.last_visible_y
11508 && !last_text_row_at_end)
11509 {
11510 /* Window end line if one of the preserved rows from the current
11511 matrix. Set row to the last row displaying text in current
11512 matrix starting at first_unchanged_at_end_row, after
11513 scrolling. */
11514 xassert (first_unchanged_at_end_row->displays_text_p);
11515 row = find_last_row_displaying_text (w->current_matrix, &it,
11516 first_unchanged_at_end_row);
11517 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
11518
11519 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
11520 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11521 w->window_end_vpos
11522 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
11523 }
11524 else if (last_text_row_at_end)
11525 {
11526 w->window_end_pos
11527 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
11528 w->window_end_bytepos
11529 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
11530 w->window_end_vpos
11531 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
11532 }
11533 else if (last_text_row)
11534 {
11535 /* We have displayed either to the end of the window or at the
11536 end of the window, i.e. the last row with text is to be found
11537 in the desired matrix. */
11538 w->window_end_pos
11539 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
11540 w->window_end_bytepos
11541 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
11542 w->window_end_vpos
11543 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
11544 }
11545 else if (first_unchanged_at_end_row == NULL
11546 && last_text_row == NULL
11547 && last_text_row_at_end == NULL)
11548 {
11549 /* Displayed to end of window, but no line containing text was
11550 displayed. Lines were deleted at the end of the window. */
11551 int vpos;
11552 int header_line_p = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
11553
11554 for (vpos = XFASTINT (w->window_end_vpos); vpos > 0; --vpos)
11555 if ((w->desired_matrix->rows[vpos + header_line_p].enabled_p
11556 && w->desired_matrix->rows[vpos + header_line_p].displays_text_p)
11557 || (!w->desired_matrix->rows[vpos + header_line_p].enabled_p
11558 && w->current_matrix->rows[vpos + header_line_p].displays_text_p))
11559 break;
11560
11561 w->window_end_vpos = make_number (vpos);
11562 }
11563 else
11564 abort ();
11565
11566 /* Disable rows below what's displayed in the window. This makes
11567 debugging easier. */
11568 enable_glyph_matrix_rows (current_matrix,
11569 XFASTINT (w->window_end_vpos) + 1,
11570 bottom_vpos, 0);
11571
11572 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
11573 debug_end_vpos = XFASTINT (w->window_end_vpos));
11574
11575 /* Record that display has not been completed. */
11576 w->window_end_valid = Qnil;
11577 w->desired_matrix->no_scrolling_p = 1;
11578 return 3;
11579 }
11580
11581
11582 \f
11583 /***********************************************************************
11584 More debugging support
11585 ***********************************************************************/
11586
11587 #if GLYPH_DEBUG
11588
11589 void dump_glyph_row P_ ((struct glyph_matrix *, int, int));
11590 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
11591 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
11592
11593
11594 /* Dump the contents of glyph matrix MATRIX on stderr.
11595
11596 GLYPHS 0 means don't show glyph contents.
11597 GLYPHS 1 means show glyphs in short form
11598 GLYPHS > 1 means show glyphs in long form. */
11599
11600 void
11601 dump_glyph_matrix (matrix, glyphs)
11602 struct glyph_matrix *matrix;
11603 int glyphs;
11604 {
11605 int i;
11606 for (i = 0; i < matrix->nrows; ++i)
11607 dump_glyph_row (matrix, i, glyphs);
11608 }
11609
11610
11611 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
11612 the glyph row and area where the glyph comes from. */
11613
11614 void
11615 dump_glyph (row, glyph, area)
11616 struct glyph_row *row;
11617 struct glyph *glyph;
11618 int area;
11619 {
11620 if (glyph->type == CHAR_GLYPH)
11621 {
11622 fprintf (stderr,
11623 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11624 glyph - row->glyphs[TEXT_AREA],
11625 'C',
11626 glyph->charpos,
11627 (BUFFERP (glyph->object)
11628 ? 'B'
11629 : (STRINGP (glyph->object)
11630 ? 'S'
11631 : '-')),
11632 glyph->pixel_width,
11633 glyph->u.ch,
11634 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
11635 ? glyph->u.ch
11636 : '.'),
11637 glyph->face_id,
11638 glyph->left_box_line_p,
11639 glyph->right_box_line_p);
11640 }
11641 else if (glyph->type == STRETCH_GLYPH)
11642 {
11643 fprintf (stderr,
11644 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11645 glyph - row->glyphs[TEXT_AREA],
11646 'S',
11647 glyph->charpos,
11648 (BUFFERP (glyph->object)
11649 ? 'B'
11650 : (STRINGP (glyph->object)
11651 ? 'S'
11652 : '-')),
11653 glyph->pixel_width,
11654 0,
11655 '.',
11656 glyph->face_id,
11657 glyph->left_box_line_p,
11658 glyph->right_box_line_p);
11659 }
11660 else if (glyph->type == IMAGE_GLYPH)
11661 {
11662 fprintf (stderr,
11663 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11664 glyph - row->glyphs[TEXT_AREA],
11665 'I',
11666 glyph->charpos,
11667 (BUFFERP (glyph->object)
11668 ? 'B'
11669 : (STRINGP (glyph->object)
11670 ? 'S'
11671 : '-')),
11672 glyph->pixel_width,
11673 glyph->u.img_id,
11674 '.',
11675 glyph->face_id,
11676 glyph->left_box_line_p,
11677 glyph->right_box_line_p);
11678 }
11679 }
11680
11681
11682 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
11683 GLYPHS 0 means don't show glyph contents.
11684 GLYPHS 1 means show glyphs in short form
11685 GLYPHS > 1 means show glyphs in long form. */
11686
11687 void
11688 dump_glyph_row (matrix, vpos, glyphs)
11689 struct glyph_matrix *matrix;
11690 int vpos, glyphs;
11691 {
11692 struct glyph_row *row;
11693
11694 if (vpos < 0 || vpos >= matrix->nrows)
11695 return;
11696
11697 row = MATRIX_ROW (matrix, vpos);
11698
11699 if (glyphs != 1)
11700 {
11701 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
11702 fprintf (stderr, "=======================================================================\n");
11703
11704 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d\
11705 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
11706 row - matrix->rows,
11707 MATRIX_ROW_START_CHARPOS (row),
11708 MATRIX_ROW_END_CHARPOS (row),
11709 row->used[TEXT_AREA],
11710 row->contains_overlapping_glyphs_p,
11711 row->enabled_p,
11712 row->inverse_p,
11713 row->truncated_on_left_p,
11714 row->truncated_on_right_p,
11715 row->overlay_arrow_p,
11716 row->continued_p,
11717 MATRIX_ROW_CONTINUATION_LINE_P (row),
11718 row->displays_text_p,
11719 row->ends_at_zv_p,
11720 row->fill_line_p,
11721 row->ends_in_middle_of_char_p,
11722 row->starts_in_middle_of_char_p,
11723 row->mouse_face_p,
11724 row->x,
11725 row->y,
11726 row->pixel_width,
11727 row->height,
11728 row->visible_height,
11729 row->ascent,
11730 row->phys_ascent);
11731 fprintf (stderr, "%9d %5d\n", row->start.overlay_string_index,
11732 row->end.overlay_string_index);
11733 fprintf (stderr, "%9d %5d\n",
11734 CHARPOS (row->start.string_pos),
11735 CHARPOS (row->end.string_pos));
11736 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
11737 row->end.dpvec_index);
11738 }
11739
11740 if (glyphs > 1)
11741 {
11742 int area;
11743
11744 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
11745 {
11746 struct glyph *glyph = row->glyphs[area];
11747 struct glyph *glyph_end = glyph + row->used[area];
11748
11749 /* Glyph for a line end in text. */
11750 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
11751 ++glyph_end;
11752
11753 if (glyph < glyph_end)
11754 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
11755
11756 for (; glyph < glyph_end; ++glyph)
11757 dump_glyph (row, glyph, area);
11758 }
11759 }
11760 else if (glyphs == 1)
11761 {
11762 int area;
11763
11764 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
11765 {
11766 char *s = (char *) alloca (row->used[area] + 1);
11767 int i;
11768
11769 for (i = 0; i < row->used[area]; ++i)
11770 {
11771 struct glyph *glyph = row->glyphs[area] + i;
11772 if (glyph->type == CHAR_GLYPH
11773 && glyph->u.ch < 0x80
11774 && glyph->u.ch >= ' ')
11775 s[i] = glyph->u.ch;
11776 else
11777 s[i] = '.';
11778 }
11779
11780 s[i] = '\0';
11781 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
11782 }
11783 }
11784 }
11785
11786
11787 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
11788 Sdump_glyph_matrix, 0, 1, "p",
11789 "Dump the current matrix of the selected window to stderr.\n\
11790 Shows contents of glyph row structures. With non-nil\n\
11791 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show\n\
11792 glyphs in short form, otherwise show glyphs in long form.")
11793 (glyphs)
11794 Lisp_Object glyphs;
11795 {
11796 struct window *w = XWINDOW (selected_window);
11797 struct buffer *buffer = XBUFFER (w->buffer);
11798
11799 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
11800 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
11801 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
11802 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
11803 fprintf (stderr, "=============================================\n");
11804 dump_glyph_matrix (w->current_matrix,
11805 NILP (glyphs) ? 0 : XINT (glyphs));
11806 return Qnil;
11807 }
11808
11809
11810 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
11811 "Dump glyph row ROW to stderr.\n\
11812 GLYPH 0 means don't dump glyphs.\n\
11813 GLYPH 1 means dump glyphs in short form.\n\
11814 GLYPH > 1 or omitted means dump glyphs in long form.")
11815 (row, glyphs)
11816 Lisp_Object row, glyphs;
11817 {
11818 CHECK_NUMBER (row, 0);
11819 dump_glyph_row (XWINDOW (selected_window)->current_matrix,
11820 XINT (row),
11821 INTEGERP (glyphs) ? XINT (glyphs) : 2);
11822 return Qnil;
11823 }
11824
11825
11826 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
11827 "Dump glyph row ROW of the tool-bar of the current frame to stderr.\n\
11828 GLYPH 0 means don't dump glyphs.\n\
11829 GLYPH 1 means dump glyphs in short form.\n\
11830 GLYPH > 1 or omitted means dump glyphs in long form.")
11831 (row, glyphs)
11832 Lisp_Object row, glyphs;
11833 {
11834 struct frame *sf = SELECTED_FRAME ();
11835 struct glyph_matrix *m = (XWINDOW (sf->tool_bar_window)->current_matrix);
11836 dump_glyph_row (m, XINT (row), INTEGERP (glyphs) ? XINT (glyphs) : 2);
11837 return Qnil;
11838 }
11839
11840
11841 DEFUN ("trace-redisplay-toggle", Ftrace_redisplay_toggle,
11842 Strace_redisplay_toggle, 0, 0, "",
11843 "Toggle tracing of redisplay.")
11844 ()
11845 {
11846 trace_redisplay_p = !trace_redisplay_p;
11847 return Qnil;
11848 }
11849
11850
11851 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, 1, "",
11852 "Print STRING to stderr.")
11853 (string)
11854 Lisp_Object string;
11855 {
11856 CHECK_STRING (string, 0);
11857 fprintf (stderr, "%s", XSTRING (string)->data);
11858 return Qnil;
11859 }
11860
11861 #endif /* GLYPH_DEBUG */
11862
11863
11864 \f
11865 /***********************************************************************
11866 Building Desired Matrix Rows
11867 ***********************************************************************/
11868
11869 /* Return a temporary glyph row holding the glyphs of an overlay
11870 arrow. Only used for non-window-redisplay windows. */
11871
11872 static struct glyph_row *
11873 get_overlay_arrow_glyph_row (w)
11874 struct window *w;
11875 {
11876 struct frame *f = XFRAME (WINDOW_FRAME (w));
11877 struct buffer *buffer = XBUFFER (w->buffer);
11878 struct buffer *old = current_buffer;
11879 unsigned char *arrow_string = XSTRING (Voverlay_arrow_string)->data;
11880 int arrow_len = XSTRING (Voverlay_arrow_string)->size;
11881 unsigned char *arrow_end = arrow_string + arrow_len;
11882 unsigned char *p;
11883 struct it it;
11884 int multibyte_p;
11885 int n_glyphs_before;
11886
11887 set_buffer_temp (buffer);
11888 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
11889 it.glyph_row->used[TEXT_AREA] = 0;
11890 SET_TEXT_POS (it.position, 0, 0);
11891
11892 multibyte_p = !NILP (buffer->enable_multibyte_characters);
11893 p = arrow_string;
11894 while (p < arrow_end)
11895 {
11896 Lisp_Object face, ilisp;
11897
11898 /* Get the next character. */
11899 if (multibyte_p)
11900 it.c = string_char_and_length (p, arrow_len, &it.len);
11901 else
11902 it.c = *p, it.len = 1;
11903 p += it.len;
11904
11905 /* Get its face. */
11906 ilisp = make_number (p - arrow_string);
11907 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
11908 it.face_id = compute_char_face (f, it.c, face);
11909
11910 /* Compute its width, get its glyphs. */
11911 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
11912 SET_TEXT_POS (it.position, -1, -1);
11913 PRODUCE_GLYPHS (&it);
11914
11915 /* If this character doesn't fit any more in the line, we have
11916 to remove some glyphs. */
11917 if (it.current_x > it.last_visible_x)
11918 {
11919 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
11920 break;
11921 }
11922 }
11923
11924 set_buffer_temp (old);
11925 return it.glyph_row;
11926 }
11927
11928
11929 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
11930 glyphs are only inserted for terminal frames since we can't really
11931 win with truncation glyphs when partially visible glyphs are
11932 involved. Which glyphs to insert is determined by
11933 produce_special_glyphs. */
11934
11935 static void
11936 insert_left_trunc_glyphs (it)
11937 struct it *it;
11938 {
11939 struct it truncate_it;
11940 struct glyph *from, *end, *to, *toend;
11941
11942 xassert (!FRAME_WINDOW_P (it->f));
11943
11944 /* Get the truncation glyphs. */
11945 truncate_it = *it;
11946 truncate_it.current_x = 0;
11947 truncate_it.face_id = DEFAULT_FACE_ID;
11948 truncate_it.glyph_row = &scratch_glyph_row;
11949 truncate_it.glyph_row->used[TEXT_AREA] = 0;
11950 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
11951 truncate_it.object = make_number (0);
11952 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
11953
11954 /* Overwrite glyphs from IT with truncation glyphs. */
11955 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
11956 end = from + truncate_it.glyph_row->used[TEXT_AREA];
11957 to = it->glyph_row->glyphs[TEXT_AREA];
11958 toend = to + it->glyph_row->used[TEXT_AREA];
11959
11960 while (from < end)
11961 *to++ = *from++;
11962
11963 /* There may be padding glyphs left over. Overwrite them too. */
11964 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
11965 {
11966 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
11967 while (from < end)
11968 *to++ = *from++;
11969 }
11970
11971 if (to > toend)
11972 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
11973 }
11974
11975
11976 /* Compute the pixel height and width of IT->glyph_row.
11977
11978 Most of the time, ascent and height of a display line will be equal
11979 to the max_ascent and max_height values of the display iterator
11980 structure. This is not the case if
11981
11982 1. We hit ZV without displaying anything. In this case, max_ascent
11983 and max_height will be zero.
11984
11985 2. We have some glyphs that don't contribute to the line height.
11986 (The glyph row flag contributes_to_line_height_p is for future
11987 pixmap extensions).
11988
11989 The first case is easily covered by using default values because in
11990 these cases, the line height does not really matter, except that it
11991 must not be zero. */
11992
11993 static void
11994 compute_line_metrics (it)
11995 struct it *it;
11996 {
11997 struct glyph_row *row = it->glyph_row;
11998 int area, i;
11999
12000 if (FRAME_WINDOW_P (it->f))
12001 {
12002 int i, header_line_height;
12003
12004 /* The line may consist of one space only, that was added to
12005 place the cursor on it. If so, the row's height hasn't been
12006 computed yet. */
12007 if (row->height == 0)
12008 {
12009 if (it->max_ascent + it->max_descent == 0)
12010 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
12011 row->ascent = it->max_ascent;
12012 row->height = it->max_ascent + it->max_descent;
12013 row->phys_ascent = it->max_phys_ascent;
12014 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
12015 }
12016
12017 /* Compute the width of this line. */
12018 row->pixel_width = row->x;
12019 for (i = 0; i < row->used[TEXT_AREA]; ++i)
12020 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
12021
12022 xassert (row->pixel_width >= 0);
12023 xassert (row->ascent >= 0 && row->height > 0);
12024
12025 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
12026 || MATRIX_ROW_OVERLAPS_PRED_P (row));
12027
12028 /* If first line's physical ascent is larger than its logical
12029 ascent, use the physical ascent, and make the row taller.
12030 This makes accented characters fully visible. */
12031 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
12032 && row->phys_ascent > row->ascent)
12033 {
12034 row->height += row->phys_ascent - row->ascent;
12035 row->ascent = row->phys_ascent;
12036 }
12037
12038 /* Compute how much of the line is visible. */
12039 row->visible_height = row->height;
12040
12041 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (it->w);
12042 if (row->y < header_line_height)
12043 row->visible_height -= header_line_height - row->y;
12044 else
12045 {
12046 int max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
12047 if (row->y + row->height > max_y)
12048 row->visible_height -= row->y + row->height - max_y;
12049 }
12050 }
12051 else
12052 {
12053 row->pixel_width = row->used[TEXT_AREA];
12054 row->ascent = row->phys_ascent = 0;
12055 row->height = row->phys_height = row->visible_height = 1;
12056 }
12057
12058 /* Compute a hash code for this row. */
12059 row->hash = 0;
12060 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12061 for (i = 0; i < row->used[area]; ++i)
12062 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
12063 + row->glyphs[area][i].u.val
12064 + row->glyphs[area][i].face_id
12065 + row->glyphs[area][i].padding_p
12066 + (row->glyphs[area][i].type << 2));
12067
12068 it->max_ascent = it->max_descent = 0;
12069 it->max_phys_ascent = it->max_phys_descent = 0;
12070 }
12071
12072
12073 /* Append one space to the glyph row of iterator IT if doing a
12074 window-based redisplay. DEFAULT_FACE_P non-zero means let the
12075 space have the default face, otherwise let it have the same face as
12076 IT->face_id. Value is non-zero if a space was added.
12077
12078 This function is called to make sure that there is always one glyph
12079 at the end of a glyph row that the cursor can be set on under
12080 window-systems. (If there weren't such a glyph we would not know
12081 how wide and tall a box cursor should be displayed).
12082
12083 At the same time this space let's a nicely handle clearing to the
12084 end of the line if the row ends in italic text. */
12085
12086 static int
12087 append_space (it, default_face_p)
12088 struct it *it;
12089 int default_face_p;
12090 {
12091 if (FRAME_WINDOW_P (it->f))
12092 {
12093 int n = it->glyph_row->used[TEXT_AREA];
12094
12095 if (it->glyph_row->glyphs[TEXT_AREA] + n
12096 < it->glyph_row->glyphs[1 + TEXT_AREA])
12097 {
12098 /* Save some values that must not be changed.
12099 Must save IT->c and IT->len because otherwise
12100 ITERATOR_AT_END_P wouldn't work anymore after
12101 append_space has been called. */
12102 enum display_element_type saved_what = it->what;
12103 int saved_c = it->c, saved_len = it->len;
12104 int saved_x = it->current_x;
12105 int saved_face_id = it->face_id;
12106 struct text_pos saved_pos;
12107 Lisp_Object saved_object;
12108 struct face *face;
12109
12110 saved_object = it->object;
12111 saved_pos = it->position;
12112
12113 it->what = IT_CHARACTER;
12114 bzero (&it->position, sizeof it->position);
12115 it->object = make_number (0);
12116 it->c = ' ';
12117 it->len = 1;
12118
12119 if (default_face_p)
12120 it->face_id = DEFAULT_FACE_ID;
12121 else if (it->face_before_selective_p)
12122 it->face_id = it->saved_face_id;
12123 face = FACE_FROM_ID (it->f, it->face_id);
12124 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
12125
12126 PRODUCE_GLYPHS (it);
12127
12128 it->current_x = saved_x;
12129 it->object = saved_object;
12130 it->position = saved_pos;
12131 it->what = saved_what;
12132 it->face_id = saved_face_id;
12133 it->len = saved_len;
12134 it->c = saved_c;
12135 return 1;
12136 }
12137 }
12138
12139 return 0;
12140 }
12141
12142
12143 /* Extend the face of the last glyph in the text area of IT->glyph_row
12144 to the end of the display line. Called from display_line.
12145 If the glyph row is empty, add a space glyph to it so that we
12146 know the face to draw. Set the glyph row flag fill_line_p. */
12147
12148 static void
12149 extend_face_to_end_of_line (it)
12150 struct it *it;
12151 {
12152 struct face *face;
12153 struct frame *f = it->f;
12154
12155 /* If line is already filled, do nothing. */
12156 if (it->current_x >= it->last_visible_x)
12157 return;
12158
12159 /* Face extension extends the background and box of IT->face_id
12160 to the end of the line. If the background equals the background
12161 of the frame, we don't have to do anything. */
12162 if (it->face_before_selective_p)
12163 face = FACE_FROM_ID (it->f, it->saved_face_id);
12164 else
12165 face = FACE_FROM_ID (f, it->face_id);
12166
12167 if (FRAME_WINDOW_P (f)
12168 && face->box == FACE_NO_BOX
12169 && face->background == FRAME_BACKGROUND_PIXEL (f)
12170 && !face->stipple)
12171 return;
12172
12173 /* Set the glyph row flag indicating that the face of the last glyph
12174 in the text area has to be drawn to the end of the text area. */
12175 it->glyph_row->fill_line_p = 1;
12176
12177 /* If current character of IT is not ASCII, make sure we have the
12178 ASCII face. This will be automatically undone the next time
12179 get_next_display_element returns a multibyte character. Note
12180 that the character will always be single byte in unibyte text. */
12181 if (!SINGLE_BYTE_CHAR_P (it->c))
12182 {
12183 it->face_id = FACE_FOR_CHAR (f, face, 0);
12184 }
12185
12186 if (FRAME_WINDOW_P (f))
12187 {
12188 /* If the row is empty, add a space with the current face of IT,
12189 so that we know which face to draw. */
12190 if (it->glyph_row->used[TEXT_AREA] == 0)
12191 {
12192 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
12193 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
12194 it->glyph_row->used[TEXT_AREA] = 1;
12195 }
12196 }
12197 else
12198 {
12199 /* Save some values that must not be changed. */
12200 int saved_x = it->current_x;
12201 struct text_pos saved_pos;
12202 Lisp_Object saved_object;
12203 enum display_element_type saved_what = it->what;
12204 int saved_face_id = it->face_id;
12205
12206 saved_object = it->object;
12207 saved_pos = it->position;
12208
12209 it->what = IT_CHARACTER;
12210 bzero (&it->position, sizeof it->position);
12211 it->object = make_number (0);
12212 it->c = ' ';
12213 it->len = 1;
12214 it->face_id = face->id;
12215
12216 PRODUCE_GLYPHS (it);
12217
12218 while (it->current_x <= it->last_visible_x)
12219 PRODUCE_GLYPHS (it);
12220
12221 /* Don't count these blanks really. It would let us insert a left
12222 truncation glyph below and make us set the cursor on them, maybe. */
12223 it->current_x = saved_x;
12224 it->object = saved_object;
12225 it->position = saved_pos;
12226 it->what = saved_what;
12227 it->face_id = saved_face_id;
12228 }
12229 }
12230
12231
12232 /* Value is non-zero if text starting at CHARPOS in current_buffer is
12233 trailing whitespace. */
12234
12235 static int
12236 trailing_whitespace_p (charpos)
12237 int charpos;
12238 {
12239 int bytepos = CHAR_TO_BYTE (charpos);
12240 int c = 0;
12241
12242 while (bytepos < ZV_BYTE
12243 && (c = FETCH_CHAR (bytepos),
12244 c == ' ' || c == '\t'))
12245 ++bytepos;
12246
12247 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
12248 {
12249 if (bytepos != PT_BYTE)
12250 return 1;
12251 }
12252 return 0;
12253 }
12254
12255
12256 /* Highlight trailing whitespace, if any, in ROW. */
12257
12258 void
12259 highlight_trailing_whitespace (f, row)
12260 struct frame *f;
12261 struct glyph_row *row;
12262 {
12263 int used = row->used[TEXT_AREA];
12264
12265 if (used)
12266 {
12267 struct glyph *start = row->glyphs[TEXT_AREA];
12268 struct glyph *glyph = start + used - 1;
12269
12270 /* Skip over glyphs inserted to display the cursor at the
12271 end of a line, for extending the face of the last glyph
12272 to the end of the line on terminals, and for truncation
12273 and continuation glyphs. */
12274 while (glyph >= start
12275 && glyph->type == CHAR_GLYPH
12276 && INTEGERP (glyph->object))
12277 --glyph;
12278
12279 /* If last glyph is a space or stretch, and it's trailing
12280 whitespace, set the face of all trailing whitespace glyphs in
12281 IT->glyph_row to `trailing-whitespace'. */
12282 if (glyph >= start
12283 && BUFFERP (glyph->object)
12284 && (glyph->type == STRETCH_GLYPH
12285 || (glyph->type == CHAR_GLYPH
12286 && glyph->u.ch == ' '))
12287 && trailing_whitespace_p (glyph->charpos))
12288 {
12289 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
12290
12291 while (glyph >= start
12292 && BUFFERP (glyph->object)
12293 && (glyph->type == STRETCH_GLYPH
12294 || (glyph->type == CHAR_GLYPH
12295 && glyph->u.ch == ' ')))
12296 (glyph--)->face_id = face_id;
12297 }
12298 }
12299 }
12300
12301
12302 /* Value is non-zero if glyph row ROW in window W should be
12303 used to hold the cursor. */
12304
12305 static int
12306 cursor_row_p (w, row)
12307 struct window *w;
12308 struct glyph_row *row;
12309 {
12310 int cursor_row_p = 1;
12311
12312 if (PT == MATRIX_ROW_END_CHARPOS (row))
12313 {
12314 /* If the row ends with a newline from a string, we don't want
12315 the cursor there (if the row is continued it doesn't end in a
12316 newline). */
12317 if (CHARPOS (row->end.string_pos) >= 0
12318 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12319 cursor_row_p = row->continued_p;
12320
12321 /* If the row ends at ZV, display the cursor at the end of that
12322 row instead of at the start of the row below. */
12323 else if (row->ends_at_zv_p)
12324 cursor_row_p = 1;
12325 else
12326 cursor_row_p = 0;
12327 }
12328
12329 return cursor_row_p;
12330 }
12331
12332
12333 /* Construct the glyph row IT->glyph_row in the desired matrix of
12334 IT->w from text at the current position of IT. See dispextern.h
12335 for an overview of struct it. Value is non-zero if
12336 IT->glyph_row displays text, as opposed to a line displaying ZV
12337 only. */
12338
12339 static int
12340 display_line (it)
12341 struct it *it;
12342 {
12343 struct glyph_row *row = it->glyph_row;
12344
12345 /* We always start displaying at hpos zero even if hscrolled. */
12346 xassert (it->hpos == 0 && it->current_x == 0);
12347
12348 /* We must not display in a row that's not a text row. */
12349 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
12350 < it->w->desired_matrix->nrows);
12351
12352 /* Is IT->w showing the region? */
12353 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
12354
12355 /* Clear the result glyph row and enable it. */
12356 prepare_desired_row (row);
12357
12358 row->y = it->current_y;
12359 row->start = it->current;
12360 row->continuation_lines_width = it->continuation_lines_width;
12361 row->displays_text_p = 1;
12362 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
12363 it->starts_in_middle_of_char_p = 0;
12364
12365 /* Arrange the overlays nicely for our purposes. Usually, we call
12366 display_line on only one line at a time, in which case this
12367 can't really hurt too much, or we call it on lines which appear
12368 one after another in the buffer, in which case all calls to
12369 recenter_overlay_lists but the first will be pretty cheap. */
12370 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
12371
12372 /* Move over display elements that are not visible because we are
12373 hscrolled. This may stop at an x-position < IT->first_visible_x
12374 if the first glyph is partially visible or if we hit a line end. */
12375 if (it->current_x < it->first_visible_x)
12376 move_it_in_display_line_to (it, ZV, it->first_visible_x,
12377 MOVE_TO_POS | MOVE_TO_X);
12378
12379 /* Get the initial row height. This is either the height of the
12380 text hscrolled, if there is any, or zero. */
12381 row->ascent = it->max_ascent;
12382 row->height = it->max_ascent + it->max_descent;
12383 row->phys_ascent = it->max_phys_ascent;
12384 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
12385
12386 /* Loop generating characters. The loop is left with IT on the next
12387 character to display. */
12388 while (1)
12389 {
12390 int n_glyphs_before, hpos_before, x_before;
12391 int x, i, nglyphs;
12392 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
12393
12394 /* Retrieve the next thing to display. Value is zero if end of
12395 buffer reached. */
12396 if (!get_next_display_element (it))
12397 {
12398 /* Maybe add a space at the end of this line that is used to
12399 display the cursor there under X. Set the charpos of the
12400 first glyph of blank lines not corresponding to any text
12401 to -1. */
12402 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
12403 || row->used[TEXT_AREA] == 0)
12404 {
12405 row->glyphs[TEXT_AREA]->charpos = -1;
12406 row->displays_text_p = 0;
12407
12408 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines))
12409 row->indicate_empty_line_p = 1;
12410 }
12411
12412 it->continuation_lines_width = 0;
12413 row->ends_at_zv_p = 1;
12414 break;
12415 }
12416
12417 /* Now, get the metrics of what we want to display. This also
12418 generates glyphs in `row' (which is IT->glyph_row). */
12419 n_glyphs_before = row->used[TEXT_AREA];
12420 x = it->current_x;
12421
12422 /* Remember the line height so far in case the next element doesn't
12423 fit on the line. */
12424 if (!it->truncate_lines_p)
12425 {
12426 ascent = it->max_ascent;
12427 descent = it->max_descent;
12428 phys_ascent = it->max_phys_ascent;
12429 phys_descent = it->max_phys_descent;
12430 }
12431
12432 PRODUCE_GLYPHS (it);
12433
12434 /* If this display element was in marginal areas, continue with
12435 the next one. */
12436 if (it->area != TEXT_AREA)
12437 {
12438 row->ascent = max (row->ascent, it->max_ascent);
12439 row->height = max (row->height, it->max_ascent + it->max_descent);
12440 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12441 row->phys_height = max (row->phys_height,
12442 it->max_phys_ascent + it->max_phys_descent);
12443 set_iterator_to_next (it, 1);
12444 continue;
12445 }
12446
12447 /* Does the display element fit on the line? If we truncate
12448 lines, we should draw past the right edge of the window. If
12449 we don't truncate, we want to stop so that we can display the
12450 continuation glyph before the right margin. If lines are
12451 continued, there are two possible strategies for characters
12452 resulting in more than 1 glyph (e.g. tabs): Display as many
12453 glyphs as possible in this line and leave the rest for the
12454 continuation line, or display the whole element in the next
12455 line. Original redisplay did the former, so we do it also. */
12456 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
12457 hpos_before = it->hpos;
12458 x_before = x;
12459
12460 if (/* Not a newline. */
12461 nglyphs > 0
12462 /* Glyphs produced fit entirely in the line. */
12463 && it->current_x < it->last_visible_x)
12464 {
12465 it->hpos += nglyphs;
12466 row->ascent = max (row->ascent, it->max_ascent);
12467 row->height = max (row->height, it->max_ascent + it->max_descent);
12468 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12469 row->phys_height = max (row->phys_height,
12470 it->max_phys_ascent + it->max_phys_descent);
12471 if (it->current_x - it->pixel_width < it->first_visible_x)
12472 row->x = x - it->first_visible_x;
12473 }
12474 else
12475 {
12476 int new_x;
12477 struct glyph *glyph;
12478
12479 for (i = 0; i < nglyphs; ++i, x = new_x)
12480 {
12481 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12482 new_x = x + glyph->pixel_width;
12483
12484 if (/* Lines are continued. */
12485 !it->truncate_lines_p
12486 && (/* Glyph doesn't fit on the line. */
12487 new_x > it->last_visible_x
12488 /* Or it fits exactly on a window system frame. */
12489 || (new_x == it->last_visible_x
12490 && FRAME_WINDOW_P (it->f))))
12491 {
12492 /* End of a continued line. */
12493
12494 if (it->hpos == 0
12495 || (new_x == it->last_visible_x
12496 && FRAME_WINDOW_P (it->f)))
12497 {
12498 /* Current glyph is the only one on the line or
12499 fits exactly on the line. We must continue
12500 the line because we can't draw the cursor
12501 after the glyph. */
12502 row->continued_p = 1;
12503 it->current_x = new_x;
12504 it->continuation_lines_width += new_x;
12505 ++it->hpos;
12506 if (i == nglyphs - 1)
12507 set_iterator_to_next (it, 1);
12508 }
12509 else if (CHAR_GLYPH_PADDING_P (*glyph)
12510 && !FRAME_WINDOW_P (it->f))
12511 {
12512 /* A padding glyph that doesn't fit on this line.
12513 This means the whole character doesn't fit
12514 on the line. */
12515 row->used[TEXT_AREA] = n_glyphs_before;
12516
12517 /* Fill the rest of the row with continuation
12518 glyphs like in 20.x. */
12519 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
12520 < row->glyphs[1 + TEXT_AREA])
12521 produce_special_glyphs (it, IT_CONTINUATION);
12522
12523 row->continued_p = 1;
12524 it->current_x = x_before;
12525 it->continuation_lines_width += x_before;
12526
12527 /* Restore the height to what it was before the
12528 element not fitting on the line. */
12529 it->max_ascent = ascent;
12530 it->max_descent = descent;
12531 it->max_phys_ascent = phys_ascent;
12532 it->max_phys_descent = phys_descent;
12533 }
12534 else
12535 {
12536 /* Display element draws past the right edge of
12537 the window. Restore positions to values
12538 before the element. The next line starts
12539 with current_x before the glyph that could
12540 not be displayed, so that TAB works right. */
12541 row->used[TEXT_AREA] = n_glyphs_before + i;
12542
12543 /* Display continuation glyphs. */
12544 if (!FRAME_WINDOW_P (it->f))
12545 produce_special_glyphs (it, IT_CONTINUATION);
12546 row->continued_p = 1;
12547
12548 it->current_x = x;
12549 it->continuation_lines_width += x;
12550 if (nglyphs > 1 && i > 0)
12551 {
12552 row->ends_in_middle_of_char_p = 1;
12553 it->starts_in_middle_of_char_p = 1;
12554 }
12555
12556 /* Restore the height to what it was before the
12557 element not fitting on the line. */
12558 it->max_ascent = ascent;
12559 it->max_descent = descent;
12560 it->max_phys_ascent = phys_ascent;
12561 it->max_phys_descent = phys_descent;
12562 }
12563
12564 break;
12565 }
12566 else if (new_x > it->first_visible_x)
12567 {
12568 /* Increment number of glyphs actually displayed. */
12569 ++it->hpos;
12570
12571 if (x < it->first_visible_x)
12572 /* Glyph is partially visible, i.e. row starts at
12573 negative X position. */
12574 row->x = x - it->first_visible_x;
12575 }
12576 else
12577 {
12578 /* Glyph is completely off the left margin of the
12579 window. This should not happen because of the
12580 move_it_in_display_line at the start of
12581 this function. */
12582 abort ();
12583 }
12584 }
12585
12586 row->ascent = max (row->ascent, it->max_ascent);
12587 row->height = max (row->height, it->max_ascent + it->max_descent);
12588 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12589 row->phys_height = max (row->phys_height,
12590 it->max_phys_ascent + it->max_phys_descent);
12591
12592 /* End of this display line if row is continued. */
12593 if (row->continued_p)
12594 break;
12595 }
12596
12597 /* Is this a line end? If yes, we're also done, after making
12598 sure that a non-default face is extended up to the right
12599 margin of the window. */
12600 if (ITERATOR_AT_END_OF_LINE_P (it))
12601 {
12602 int used_before = row->used[TEXT_AREA];
12603
12604 /* Add a space at the end of the line that is used to
12605 display the cursor there. */
12606 append_space (it, 0);
12607
12608 /* Extend the face to the end of the line. */
12609 extend_face_to_end_of_line (it);
12610
12611 /* Make sure we have the position. */
12612 if (used_before == 0)
12613 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
12614
12615 /* Consume the line end. This skips over invisible lines. */
12616 set_iterator_to_next (it, 1);
12617 it->continuation_lines_width = 0;
12618 break;
12619 }
12620
12621 /* Proceed with next display element. Note that this skips
12622 over lines invisible because of selective display. */
12623 set_iterator_to_next (it, 1);
12624
12625 /* If we truncate lines, we are done when the last displayed
12626 glyphs reach past the right margin of the window. */
12627 if (it->truncate_lines_p
12628 && (FRAME_WINDOW_P (it->f)
12629 ? (it->current_x >= it->last_visible_x)
12630 : (it->current_x > it->last_visible_x)))
12631 {
12632 /* Maybe add truncation glyphs. */
12633 if (!FRAME_WINDOW_P (it->f))
12634 {
12635 int i, n;
12636
12637 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
12638 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
12639 break;
12640
12641 for (n = row->used[TEXT_AREA]; i < n; ++i)
12642 {
12643 row->used[TEXT_AREA] = i;
12644 produce_special_glyphs (it, IT_TRUNCATION);
12645 }
12646 }
12647
12648 row->truncated_on_right_p = 1;
12649 it->continuation_lines_width = 0;
12650 reseat_at_next_visible_line_start (it, 0);
12651 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
12652 it->hpos = hpos_before;
12653 it->current_x = x_before;
12654 break;
12655 }
12656 }
12657
12658 /* If line is not empty and hscrolled, maybe insert truncation glyphs
12659 at the left window margin. */
12660 if (it->first_visible_x
12661 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
12662 {
12663 if (!FRAME_WINDOW_P (it->f))
12664 insert_left_trunc_glyphs (it);
12665 row->truncated_on_left_p = 1;
12666 }
12667
12668 /* If the start of this line is the overlay arrow-position, then
12669 mark this glyph row as the one containing the overlay arrow.
12670 This is clearly a mess with variable size fonts. It would be
12671 better to let it be displayed like cursors under X. */
12672 if (MARKERP (Voverlay_arrow_position)
12673 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
12674 && (MATRIX_ROW_START_CHARPOS (row)
12675 == marker_position (Voverlay_arrow_position))
12676 && STRINGP (Voverlay_arrow_string)
12677 && ! overlay_arrow_seen)
12678 {
12679 /* Overlay arrow in window redisplay is a bitmap. */
12680 if (!FRAME_WINDOW_P (it->f))
12681 {
12682 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
12683 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
12684 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
12685 struct glyph *p = row->glyphs[TEXT_AREA];
12686 struct glyph *p2, *end;
12687
12688 /* Copy the arrow glyphs. */
12689 while (glyph < arrow_end)
12690 *p++ = *glyph++;
12691
12692 /* Throw away padding glyphs. */
12693 p2 = p;
12694 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
12695 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
12696 ++p2;
12697 if (p2 > p)
12698 {
12699 while (p2 < end)
12700 *p++ = *p2++;
12701 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
12702 }
12703 }
12704
12705 overlay_arrow_seen = 1;
12706 row->overlay_arrow_p = 1;
12707 }
12708
12709 /* Compute pixel dimensions of this line. */
12710 compute_line_metrics (it);
12711
12712 /* Remember the position at which this line ends. */
12713 row->end = it->current;
12714
12715 /* Maybe set the cursor. */
12716 if (it->w->cursor.vpos < 0
12717 && PT >= MATRIX_ROW_START_CHARPOS (row)
12718 && PT <= MATRIX_ROW_END_CHARPOS (row)
12719 && cursor_row_p (it->w, row))
12720 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
12721
12722 /* Highlight trailing whitespace. */
12723 if (!NILP (Vshow_trailing_whitespace))
12724 highlight_trailing_whitespace (it->f, it->glyph_row);
12725
12726 /* Prepare for the next line. This line starts horizontally at (X
12727 HPOS) = (0 0). Vertical positions are incremented. As a
12728 convenience for the caller, IT->glyph_row is set to the next
12729 row to be used. */
12730 it->current_x = it->hpos = 0;
12731 it->current_y += row->height;
12732 ++it->vpos;
12733 ++it->glyph_row;
12734 return row->displays_text_p;
12735 }
12736
12737
12738 \f
12739 /***********************************************************************
12740 Menu Bar
12741 ***********************************************************************/
12742
12743 /* Redisplay the menu bar in the frame for window W.
12744
12745 The menu bar of X frames that don't have X toolkit support is
12746 displayed in a special window W->frame->menu_bar_window.
12747
12748 The menu bar of terminal frames is treated specially as far as
12749 glyph matrices are concerned. Menu bar lines are not part of
12750 windows, so the update is done directly on the frame matrix rows
12751 for the menu bar. */
12752
12753 static void
12754 display_menu_bar (w)
12755 struct window *w;
12756 {
12757 struct frame *f = XFRAME (WINDOW_FRAME (w));
12758 struct it it;
12759 Lisp_Object items;
12760 int i;
12761
12762 /* Don't do all this for graphical frames. */
12763 #ifdef HAVE_NTGUI
12764 if (!NILP (Vwindow_system))
12765 return;
12766 #endif
12767 #ifdef USE_X_TOOLKIT
12768 if (FRAME_X_P (f))
12769 return;
12770 #endif
12771 #ifdef macintosh
12772 if (FRAME_MAC_P (f))
12773 return;
12774 #endif
12775
12776 #ifdef USE_X_TOOLKIT
12777 xassert (!FRAME_WINDOW_P (f));
12778 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
12779 it.first_visible_x = 0;
12780 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
12781 #else /* not USE_X_TOOLKIT */
12782 if (FRAME_WINDOW_P (f))
12783 {
12784 /* Menu bar lines are displayed in the desired matrix of the
12785 dummy window menu_bar_window. */
12786 struct window *menu_w;
12787 xassert (WINDOWP (f->menu_bar_window));
12788 menu_w = XWINDOW (f->menu_bar_window);
12789 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
12790 MENU_FACE_ID);
12791 it.first_visible_x = 0;
12792 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
12793 }
12794 else
12795 {
12796 /* This is a TTY frame, i.e. character hpos/vpos are used as
12797 pixel x/y. */
12798 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
12799 MENU_FACE_ID);
12800 it.first_visible_x = 0;
12801 it.last_visible_x = FRAME_WIDTH (f);
12802 }
12803 #endif /* not USE_X_TOOLKIT */
12804
12805 if (! mode_line_inverse_video)
12806 /* Force the menu-bar to be displayed in the default face. */
12807 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
12808
12809 /* Clear all rows of the menu bar. */
12810 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
12811 {
12812 struct glyph_row *row = it.glyph_row + i;
12813 clear_glyph_row (row);
12814 row->enabled_p = 1;
12815 row->full_width_p = 1;
12816 }
12817
12818 /* Display all items of the menu bar. */
12819 items = FRAME_MENU_BAR_ITEMS (it.f);
12820 for (i = 0; i < XVECTOR (items)->size; i += 4)
12821 {
12822 Lisp_Object string;
12823
12824 /* Stop at nil string. */
12825 string = AREF (items, i + 1);
12826 if (NILP (string))
12827 break;
12828
12829 /* Remember where item was displayed. */
12830 AREF (items, i + 3) = make_number (it.hpos);
12831
12832 /* Display the item, pad with one space. */
12833 if (it.current_x < it.last_visible_x)
12834 display_string (NULL, string, Qnil, 0, 0, &it,
12835 XSTRING (string)->size + 1, 0, 0, -1);
12836 }
12837
12838 /* Fill out the line with spaces. */
12839 if (it.current_x < it.last_visible_x)
12840 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
12841
12842 /* Compute the total height of the lines. */
12843 compute_line_metrics (&it);
12844 }
12845
12846
12847 \f
12848 /***********************************************************************
12849 Mode Line
12850 ***********************************************************************/
12851
12852 /* Redisplay mode lines in the window tree whose root is WINDOW. If
12853 FORCE is non-zero, redisplay mode lines unconditionally.
12854 Otherwise, redisplay only mode lines that are garbaged. Value is
12855 the number of windows whose mode lines were redisplayed. */
12856
12857 static int
12858 redisplay_mode_lines (window, force)
12859 Lisp_Object window;
12860 int force;
12861 {
12862 int nwindows = 0;
12863
12864 while (!NILP (window))
12865 {
12866 struct window *w = XWINDOW (window);
12867
12868 if (WINDOWP (w->hchild))
12869 nwindows += redisplay_mode_lines (w->hchild, force);
12870 else if (WINDOWP (w->vchild))
12871 nwindows += redisplay_mode_lines (w->vchild, force);
12872 else if (force
12873 || FRAME_GARBAGED_P (XFRAME (w->frame))
12874 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
12875 {
12876 Lisp_Object old_selected_frame;
12877 struct text_pos lpoint;
12878 struct buffer *old = current_buffer;
12879
12880 /* Set the window's buffer for the mode line display. */
12881 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12882 set_buffer_internal_1 (XBUFFER (w->buffer));
12883
12884 /* Point refers normally to the selected window. For any
12885 other window, set up appropriate value. */
12886 if (!EQ (window, selected_window))
12887 {
12888 struct text_pos pt;
12889
12890 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
12891 if (CHARPOS (pt) < BEGV)
12892 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
12893 else if (CHARPOS (pt) > (ZV - 1))
12894 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
12895 else
12896 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
12897 }
12898
12899 /* Temporarily set up the selected frame. */
12900 old_selected_frame = selected_frame;
12901 selected_frame = w->frame;
12902
12903 /* Display mode lines. */
12904 clear_glyph_matrix (w->desired_matrix);
12905 if (display_mode_lines (w))
12906 {
12907 ++nwindows;
12908 w->must_be_updated_p = 1;
12909 }
12910
12911 /* Restore old settings. */
12912 selected_frame = old_selected_frame;
12913 set_buffer_internal_1 (old);
12914 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
12915 }
12916
12917 window = w->next;
12918 }
12919
12920 return nwindows;
12921 }
12922
12923
12924 /* Display the mode and/or top line of window W. Value is the number
12925 of mode lines displayed. */
12926
12927 static int
12928 display_mode_lines (w)
12929 struct window *w;
12930 {
12931 int n = 0;
12932
12933 /* These will be set while the mode line specs are processed. */
12934 line_number_displayed = 0;
12935 w->column_number_displayed = Qnil;
12936
12937 if (WINDOW_WANTS_MODELINE_P (w))
12938 {
12939 display_mode_line (w, MODE_LINE_FACE_ID,
12940 current_buffer->mode_line_format);
12941 ++n;
12942 }
12943
12944 if (WINDOW_WANTS_HEADER_LINE_P (w))
12945 {
12946 display_mode_line (w, HEADER_LINE_FACE_ID,
12947 current_buffer->header_line_format);
12948 ++n;
12949 }
12950
12951 return n;
12952 }
12953
12954
12955 /* Display mode or top line of window W. FACE_ID specifies which line
12956 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
12957 FORMAT is the mode line format to display. Value is the pixel
12958 height of the mode line displayed. */
12959
12960 static int
12961 display_mode_line (w, face_id, format)
12962 struct window *w;
12963 enum face_id face_id;
12964 Lisp_Object format;
12965 {
12966 struct it it;
12967 struct face *face;
12968
12969 init_iterator (&it, w, -1, -1, NULL, face_id);
12970 prepare_desired_row (it.glyph_row);
12971
12972 if (! mode_line_inverse_video)
12973 /* Force the mode-line to be displayed in the default face. */
12974 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
12975
12976 /* Temporarily make frame's keyboard the current kboard so that
12977 kboard-local variables in the mode_line_format will get the right
12978 values. */
12979 push_frame_kboard (it.f);
12980 display_mode_element (&it, 0, 0, 0, format);
12981 pop_frame_kboard ();
12982
12983 /* Fill up with spaces. */
12984 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
12985
12986 compute_line_metrics (&it);
12987 it.glyph_row->full_width_p = 1;
12988 it.glyph_row->mode_line_p = 1;
12989 it.glyph_row->inverse_p = 0;
12990 it.glyph_row->continued_p = 0;
12991 it.glyph_row->truncated_on_left_p = 0;
12992 it.glyph_row->truncated_on_right_p = 0;
12993
12994 /* Make a 3D mode-line have a shadow at its right end. */
12995 face = FACE_FROM_ID (it.f, face_id);
12996 extend_face_to_end_of_line (&it);
12997 if (face->box != FACE_NO_BOX)
12998 {
12999 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
13000 + it.glyph_row->used[TEXT_AREA] - 1);
13001 last->right_box_line_p = 1;
13002 }
13003
13004 return it.glyph_row->height;
13005 }
13006
13007
13008 /* Contribute ELT to the mode line for window IT->w. How it
13009 translates into text depends on its data type.
13010
13011 IT describes the display environment in which we display, as usual.
13012
13013 DEPTH is the depth in recursion. It is used to prevent
13014 infinite recursion here.
13015
13016 FIELD_WIDTH is the number of characters the display of ELT should
13017 occupy in the mode line, and PRECISION is the maximum number of
13018 characters to display from ELT's representation. See
13019 display_string for details. *
13020
13021 Returns the hpos of the end of the text generated by ELT. */
13022
13023 static int
13024 display_mode_element (it, depth, field_width, precision, elt)
13025 struct it *it;
13026 int depth;
13027 int field_width, precision;
13028 Lisp_Object elt;
13029 {
13030 int n = 0, field, prec;
13031
13032 tail_recurse:
13033 if (depth > 10)
13034 goto invalid;
13035
13036 depth++;
13037
13038 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
13039 {
13040 case Lisp_String:
13041 {
13042 /* A string: output it and check for %-constructs within it. */
13043 unsigned char c;
13044 unsigned char *this = XSTRING (elt)->data;
13045 unsigned char *lisp_string = this;
13046
13047 while ((precision <= 0 || n < precision)
13048 && *this
13049 && (frame_title_ptr
13050 || it->current_x < it->last_visible_x))
13051 {
13052 unsigned char *last = this;
13053
13054 /* Advance to end of string or next format specifier. */
13055 while ((c = *this++) != '\0' && c != '%')
13056 ;
13057
13058 if (this - 1 != last)
13059 {
13060 /* Output to end of string or up to '%'. Field width
13061 is length of string. Don't output more than
13062 PRECISION allows us. */
13063 --this;
13064 prec = strwidth (last, this - last);
13065 if (precision > 0 && prec > precision - n)
13066 prec = precision - n;
13067
13068 if (frame_title_ptr)
13069 n += store_frame_title (last, 0, prec);
13070 else
13071 n += display_string (NULL, elt, Qnil, 0, last - lisp_string,
13072 it, 0, prec, 0, -1);
13073 }
13074 else /* c == '%' */
13075 {
13076 unsigned char *percent_position = this;
13077
13078 /* Get the specified minimum width. Zero means
13079 don't pad. */
13080 field = 0;
13081 while ((c = *this++) >= '0' && c <= '9')
13082 field = field * 10 + c - '0';
13083
13084 /* Don't pad beyond the total padding allowed. */
13085 if (field_width - n > 0 && field > field_width - n)
13086 field = field_width - n;
13087
13088 /* Note that either PRECISION <= 0 or N < PRECISION. */
13089 prec = precision - n;
13090
13091 if (c == 'M')
13092 n += display_mode_element (it, depth, field, prec,
13093 Vglobal_mode_string);
13094 else if (c != 0)
13095 {
13096 unsigned char *spec
13097 = decode_mode_spec (it->w, c, field, prec);
13098
13099 if (frame_title_ptr)
13100 n += store_frame_title (spec, field, prec);
13101 else
13102 {
13103 int nglyphs_before
13104 = it->glyph_row->used[TEXT_AREA];
13105 int charpos
13106 = percent_position - XSTRING (elt)->data;
13107 int nwritten
13108 = display_string (spec, Qnil, elt, charpos, 0, it,
13109 field, prec, 0, -1);
13110
13111 /* Assign to the glyphs written above the
13112 string where the `%x' came from, position
13113 of the `%'. */
13114 if (nwritten > 0)
13115 {
13116 struct glyph *glyph
13117 = (it->glyph_row->glyphs[TEXT_AREA]
13118 + nglyphs_before);
13119 int i;
13120
13121 for (i = 0; i < nwritten; ++i)
13122 {
13123 glyph[i].object = elt;
13124 glyph[i].charpos = charpos;
13125 }
13126
13127 n += nwritten;
13128 }
13129 }
13130 }
13131 }
13132 }
13133 }
13134 break;
13135
13136 case Lisp_Symbol:
13137 /* A symbol: process the value of the symbol recursively
13138 as if it appeared here directly. Avoid error if symbol void.
13139 Special case: if value of symbol is a string, output the string
13140 literally. */
13141 {
13142 register Lisp_Object tem;
13143 tem = Fboundp (elt);
13144 if (!NILP (tem))
13145 {
13146 tem = Fsymbol_value (elt);
13147 /* If value is a string, output that string literally:
13148 don't check for % within it. */
13149 if (STRINGP (tem))
13150 {
13151 prec = precision - n;
13152 if (frame_title_ptr)
13153 n += store_frame_title (XSTRING (tem)->data, -1, prec);
13154 else
13155 n += display_string (NULL, tem, Qnil, 0, 0, it,
13156 0, prec, 0, -1);
13157 }
13158 else if (!EQ (tem, elt))
13159 {
13160 /* Give up right away for nil or t. */
13161 elt = tem;
13162 goto tail_recurse;
13163 }
13164 }
13165 }
13166 break;
13167
13168 case Lisp_Cons:
13169 {
13170 register Lisp_Object car, tem;
13171
13172 /* A cons cell: three distinct cases.
13173 If first element is a string or a cons, process all the elements
13174 and effectively concatenate them.
13175 If first element is a negative number, truncate displaying cdr to
13176 at most that many characters. If positive, pad (with spaces)
13177 to at least that many characters.
13178 If first element is a symbol, process the cadr or caddr recursively
13179 according to whether the symbol's value is non-nil or nil. */
13180 car = XCAR (elt);
13181 if (EQ (car, QCeval) && CONSP (XCDR (elt)))
13182 {
13183 /* An element of the form (:eval FORM) means evaluate FORM
13184 and use the result as mode line elements. */
13185 struct gcpro gcpro1;
13186 Lisp_Object spec;
13187
13188 spec = safe_eval (XCAR (XCDR (elt)));
13189 GCPRO1 (spec);
13190 n += display_mode_element (it, depth, field_width - n,
13191 precision - n, spec);
13192 UNGCPRO;
13193 }
13194 else if (SYMBOLP (car))
13195 {
13196 tem = Fboundp (car);
13197 elt = XCDR (elt);
13198 if (!CONSP (elt))
13199 goto invalid;
13200 /* elt is now the cdr, and we know it is a cons cell.
13201 Use its car if CAR has a non-nil value. */
13202 if (!NILP (tem))
13203 {
13204 tem = Fsymbol_value (car);
13205 if (!NILP (tem))
13206 {
13207 elt = XCAR (elt);
13208 goto tail_recurse;
13209 }
13210 }
13211 /* Symbol's value is nil (or symbol is unbound)
13212 Get the cddr of the original list
13213 and if possible find the caddr and use that. */
13214 elt = XCDR (elt);
13215 if (NILP (elt))
13216 break;
13217 else if (!CONSP (elt))
13218 goto invalid;
13219 elt = XCAR (elt);
13220 goto tail_recurse;
13221 }
13222 else if (INTEGERP (car))
13223 {
13224 register int lim = XINT (car);
13225 elt = XCDR (elt);
13226 if (lim < 0)
13227 {
13228 /* Negative int means reduce maximum width. */
13229 if (precision <= 0)
13230 precision = -lim;
13231 else
13232 precision = min (precision, -lim);
13233 }
13234 else if (lim > 0)
13235 {
13236 /* Padding specified. Don't let it be more than
13237 current maximum. */
13238 if (precision > 0)
13239 lim = min (precision, lim);
13240
13241 /* If that's more padding than already wanted, queue it.
13242 But don't reduce padding already specified even if
13243 that is beyond the current truncation point. */
13244 field_width = max (lim, field_width);
13245 }
13246 goto tail_recurse;
13247 }
13248 else if (STRINGP (car) || CONSP (car))
13249 {
13250 register int limit = 50;
13251 /* Limit is to protect against circular lists. */
13252 while (CONSP (elt)
13253 && --limit > 0
13254 && (precision <= 0 || n < precision))
13255 {
13256 n += display_mode_element (it, depth, field_width - n,
13257 precision - n, XCAR (elt));
13258 elt = XCDR (elt);
13259 }
13260 }
13261 }
13262 break;
13263
13264 default:
13265 invalid:
13266 if (frame_title_ptr)
13267 n += store_frame_title ("*invalid*", 0, precision - n);
13268 else
13269 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
13270 precision - n, 0, 0);
13271 return n;
13272 }
13273
13274 /* Pad to FIELD_WIDTH. */
13275 if (field_width > 0 && n < field_width)
13276 {
13277 if (frame_title_ptr)
13278 n += store_frame_title ("", field_width - n, 0);
13279 else
13280 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
13281 0, 0, 0);
13282 }
13283
13284 return n;
13285 }
13286
13287
13288 /* Write a null-terminated, right justified decimal representation of
13289 the positive integer D to BUF using a minimal field width WIDTH. */
13290
13291 static void
13292 pint2str (buf, width, d)
13293 register char *buf;
13294 register int width;
13295 register int d;
13296 {
13297 register char *p = buf;
13298
13299 if (d <= 0)
13300 *p++ = '0';
13301 else
13302 {
13303 while (d > 0)
13304 {
13305 *p++ = d % 10 + '0';
13306 d /= 10;
13307 }
13308 }
13309
13310 for (width -= (int) (p - buf); width > 0; --width)
13311 *p++ = ' ';
13312 *p-- = '\0';
13313 while (p > buf)
13314 {
13315 d = *buf;
13316 *buf++ = *p;
13317 *p-- = d;
13318 }
13319 }
13320
13321 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
13322 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
13323 type of CODING_SYSTEM. Return updated pointer into BUF. */
13324
13325 static unsigned char invalid_eol_type[] = "(*invalid*)";
13326
13327 static char *
13328 decode_mode_spec_coding (coding_system, buf, eol_flag)
13329 Lisp_Object coding_system;
13330 register char *buf;
13331 int eol_flag;
13332 {
13333 Lisp_Object val;
13334 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
13335 unsigned char *eol_str;
13336 int eol_str_len;
13337 /* The EOL conversion we are using. */
13338 Lisp_Object eoltype;
13339
13340 val = Fget (coding_system, Qcoding_system);
13341 eoltype = Qnil;
13342
13343 if (!VECTORP (val)) /* Not yet decided. */
13344 {
13345 if (multibyte)
13346 *buf++ = '-';
13347 if (eol_flag)
13348 eoltype = eol_mnemonic_undecided;
13349 /* Don't mention EOL conversion if it isn't decided. */
13350 }
13351 else
13352 {
13353 Lisp_Object eolvalue;
13354
13355 eolvalue = Fget (coding_system, Qeol_type);
13356
13357 if (multibyte)
13358 *buf++ = XFASTINT (AREF (val, 1));
13359
13360 if (eol_flag)
13361 {
13362 /* The EOL conversion that is normal on this system. */
13363
13364 if (NILP (eolvalue)) /* Not yet decided. */
13365 eoltype = eol_mnemonic_undecided;
13366 else if (VECTORP (eolvalue)) /* Not yet decided. */
13367 eoltype = eol_mnemonic_undecided;
13368 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
13369 eoltype = (XFASTINT (eolvalue) == 0
13370 ? eol_mnemonic_unix
13371 : (XFASTINT (eolvalue) == 1
13372 ? eol_mnemonic_dos : eol_mnemonic_mac));
13373 }
13374 }
13375
13376 if (eol_flag)
13377 {
13378 /* Mention the EOL conversion if it is not the usual one. */
13379 if (STRINGP (eoltype))
13380 {
13381 eol_str = XSTRING (eoltype)->data;
13382 eol_str_len = XSTRING (eoltype)->size;
13383 }
13384 else if (INTEGERP (eoltype)
13385 && CHAR_VALID_P (XINT (eoltype), 0))
13386 {
13387 eol_str = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
13388 eol_str_len = CHAR_STRING (XINT (eoltype), eol_str);
13389 }
13390 else
13391 {
13392 eol_str = invalid_eol_type;
13393 eol_str_len = sizeof (invalid_eol_type) - 1;
13394 }
13395 bcopy (eol_str, buf, eol_str_len);
13396 buf += eol_str_len;
13397 }
13398
13399 return buf;
13400 }
13401
13402 /* Return a string for the output of a mode line %-spec for window W,
13403 generated by character C. PRECISION >= 0 means don't return a
13404 string longer than that value. FIELD_WIDTH > 0 means pad the
13405 string returned with spaces to that value. */
13406
13407 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
13408
13409 static char *
13410 decode_mode_spec (w, c, field_width, precision)
13411 struct window *w;
13412 register int c;
13413 int field_width, precision;
13414 {
13415 Lisp_Object obj;
13416 struct frame *f = XFRAME (WINDOW_FRAME (w));
13417 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
13418 struct buffer *b = XBUFFER (w->buffer);
13419
13420 obj = Qnil;
13421
13422 switch (c)
13423 {
13424 case '*':
13425 if (!NILP (b->read_only))
13426 return "%";
13427 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
13428 return "*";
13429 return "-";
13430
13431 case '+':
13432 /* This differs from %* only for a modified read-only buffer. */
13433 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
13434 return "*";
13435 if (!NILP (b->read_only))
13436 return "%";
13437 return "-";
13438
13439 case '&':
13440 /* This differs from %* in ignoring read-only-ness. */
13441 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
13442 return "*";
13443 return "-";
13444
13445 case '%':
13446 return "%";
13447
13448 case '[':
13449 {
13450 int i;
13451 char *p;
13452
13453 if (command_loop_level > 5)
13454 return "[[[... ";
13455 p = decode_mode_spec_buf;
13456 for (i = 0; i < command_loop_level; i++)
13457 *p++ = '[';
13458 *p = 0;
13459 return decode_mode_spec_buf;
13460 }
13461
13462 case ']':
13463 {
13464 int i;
13465 char *p;
13466
13467 if (command_loop_level > 5)
13468 return " ...]]]";
13469 p = decode_mode_spec_buf;
13470 for (i = 0; i < command_loop_level; i++)
13471 *p++ = ']';
13472 *p = 0;
13473 return decode_mode_spec_buf;
13474 }
13475
13476 case '-':
13477 {
13478 register int i;
13479
13480 /* Let lots_of_dashes be a string of infinite length. */
13481 if (field_width <= 0
13482 || field_width > sizeof (lots_of_dashes))
13483 {
13484 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
13485 decode_mode_spec_buf[i] = '-';
13486 decode_mode_spec_buf[i] = '\0';
13487 return decode_mode_spec_buf;
13488 }
13489 else
13490 return lots_of_dashes;
13491 }
13492
13493 case 'b':
13494 obj = b->name;
13495 break;
13496
13497 case 'c':
13498 {
13499 int col = current_column ();
13500 w->column_number_displayed = make_number (col);
13501 pint2str (decode_mode_spec_buf, field_width, col);
13502 return decode_mode_spec_buf;
13503 }
13504
13505 case 'F':
13506 /* %F displays the frame name. */
13507 if (!NILP (f->title))
13508 return (char *) XSTRING (f->title)->data;
13509 if (f->explicit_name || ! FRAME_WINDOW_P (f))
13510 return (char *) XSTRING (f->name)->data;
13511 return "Emacs";
13512
13513 case 'f':
13514 obj = b->filename;
13515 break;
13516
13517 case 'l':
13518 {
13519 int startpos = XMARKER (w->start)->charpos;
13520 int startpos_byte = marker_byte_position (w->start);
13521 int line, linepos, linepos_byte, topline;
13522 int nlines, junk;
13523 int height = XFASTINT (w->height);
13524
13525 /* If we decided that this buffer isn't suitable for line numbers,
13526 don't forget that too fast. */
13527 if (EQ (w->base_line_pos, w->buffer))
13528 goto no_value;
13529 /* But do forget it, if the window shows a different buffer now. */
13530 else if (BUFFERP (w->base_line_pos))
13531 w->base_line_pos = Qnil;
13532
13533 /* If the buffer is very big, don't waste time. */
13534 if (INTEGERP (Vline_number_display_limit)
13535 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
13536 {
13537 w->base_line_pos = Qnil;
13538 w->base_line_number = Qnil;
13539 goto no_value;
13540 }
13541
13542 if (!NILP (w->base_line_number)
13543 && !NILP (w->base_line_pos)
13544 && XFASTINT (w->base_line_pos) <= startpos)
13545 {
13546 line = XFASTINT (w->base_line_number);
13547 linepos = XFASTINT (w->base_line_pos);
13548 linepos_byte = buf_charpos_to_bytepos (b, linepos);
13549 }
13550 else
13551 {
13552 line = 1;
13553 linepos = BUF_BEGV (b);
13554 linepos_byte = BUF_BEGV_BYTE (b);
13555 }
13556
13557 /* Count lines from base line to window start position. */
13558 nlines = display_count_lines (linepos, linepos_byte,
13559 startpos_byte,
13560 startpos, &junk);
13561
13562 topline = nlines + line;
13563
13564 /* Determine a new base line, if the old one is too close
13565 or too far away, or if we did not have one.
13566 "Too close" means it's plausible a scroll-down would
13567 go back past it. */
13568 if (startpos == BUF_BEGV (b))
13569 {
13570 w->base_line_number = make_number (topline);
13571 w->base_line_pos = make_number (BUF_BEGV (b));
13572 }
13573 else if (nlines < height + 25 || nlines > height * 3 + 50
13574 || linepos == BUF_BEGV (b))
13575 {
13576 int limit = BUF_BEGV (b);
13577 int limit_byte = BUF_BEGV_BYTE (b);
13578 int position;
13579 int distance = (height * 2 + 30) * line_number_display_limit_width;
13580
13581 if (startpos - distance > limit)
13582 {
13583 limit = startpos - distance;
13584 limit_byte = CHAR_TO_BYTE (limit);
13585 }
13586
13587 nlines = display_count_lines (startpos, startpos_byte,
13588 limit_byte,
13589 - (height * 2 + 30),
13590 &position);
13591 /* If we couldn't find the lines we wanted within
13592 line_number_display_limit_width chars per line,
13593 give up on line numbers for this window. */
13594 if (position == limit_byte && limit == startpos - distance)
13595 {
13596 w->base_line_pos = w->buffer;
13597 w->base_line_number = Qnil;
13598 goto no_value;
13599 }
13600
13601 w->base_line_number = make_number (topline - nlines);
13602 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
13603 }
13604
13605 /* Now count lines from the start pos to point. */
13606 nlines = display_count_lines (startpos, startpos_byte,
13607 PT_BYTE, PT, &junk);
13608
13609 /* Record that we did display the line number. */
13610 line_number_displayed = 1;
13611
13612 /* Make the string to show. */
13613 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
13614 return decode_mode_spec_buf;
13615 no_value:
13616 {
13617 char* p = decode_mode_spec_buf;
13618 int pad = field_width - 2;
13619 while (pad-- > 0)
13620 *p++ = ' ';
13621 *p++ = '?';
13622 *p++ = '?';
13623 *p = '\0';
13624 return decode_mode_spec_buf;
13625 }
13626 }
13627 break;
13628
13629 case 'm':
13630 obj = b->mode_name;
13631 break;
13632
13633 case 'n':
13634 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
13635 return " Narrow";
13636 break;
13637
13638 case 'p':
13639 {
13640 int pos = marker_position (w->start);
13641 int total = BUF_ZV (b) - BUF_BEGV (b);
13642
13643 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
13644 {
13645 if (pos <= BUF_BEGV (b))
13646 return "All";
13647 else
13648 return "Bottom";
13649 }
13650 else if (pos <= BUF_BEGV (b))
13651 return "Top";
13652 else
13653 {
13654 if (total > 1000000)
13655 /* Do it differently for a large value, to avoid overflow. */
13656 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
13657 else
13658 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
13659 /* We can't normally display a 3-digit number,
13660 so get us a 2-digit number that is close. */
13661 if (total == 100)
13662 total = 99;
13663 sprintf (decode_mode_spec_buf, "%2d%%", total);
13664 return decode_mode_spec_buf;
13665 }
13666 }
13667
13668 /* Display percentage of size above the bottom of the screen. */
13669 case 'P':
13670 {
13671 int toppos = marker_position (w->start);
13672 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
13673 int total = BUF_ZV (b) - BUF_BEGV (b);
13674
13675 if (botpos >= BUF_ZV (b))
13676 {
13677 if (toppos <= BUF_BEGV (b))
13678 return "All";
13679 else
13680 return "Bottom";
13681 }
13682 else
13683 {
13684 if (total > 1000000)
13685 /* Do it differently for a large value, to avoid overflow. */
13686 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
13687 else
13688 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
13689 /* We can't normally display a 3-digit number,
13690 so get us a 2-digit number that is close. */
13691 if (total == 100)
13692 total = 99;
13693 if (toppos <= BUF_BEGV (b))
13694 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
13695 else
13696 sprintf (decode_mode_spec_buf, "%2d%%", total);
13697 return decode_mode_spec_buf;
13698 }
13699 }
13700
13701 case 's':
13702 /* status of process */
13703 obj = Fget_buffer_process (w->buffer);
13704 if (NILP (obj))
13705 return "no process";
13706 #ifdef subprocesses
13707 obj = Fsymbol_name (Fprocess_status (obj));
13708 #endif
13709 break;
13710
13711 case 't': /* indicate TEXT or BINARY */
13712 #ifdef MODE_LINE_BINARY_TEXT
13713 return MODE_LINE_BINARY_TEXT (b);
13714 #else
13715 return "T";
13716 #endif
13717
13718 case 'z':
13719 /* coding-system (not including end-of-line format) */
13720 case 'Z':
13721 /* coding-system (including end-of-line type) */
13722 {
13723 int eol_flag = (c == 'Z');
13724 char *p = decode_mode_spec_buf;
13725
13726 if (! FRAME_WINDOW_P (f))
13727 {
13728 /* No need to mention EOL here--the terminal never needs
13729 to do EOL conversion. */
13730 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
13731 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
13732 }
13733 p = decode_mode_spec_coding (b->buffer_file_coding_system,
13734 p, eol_flag);
13735
13736 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
13737 #ifdef subprocesses
13738 obj = Fget_buffer_process (Fcurrent_buffer ());
13739 if (PROCESSP (obj))
13740 {
13741 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
13742 p, eol_flag);
13743 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
13744 p, eol_flag);
13745 }
13746 #endif /* subprocesses */
13747 #endif /* 0 */
13748 *p = 0;
13749 return decode_mode_spec_buf;
13750 }
13751 }
13752
13753 if (STRINGP (obj))
13754 return (char *) XSTRING (obj)->data;
13755 else
13756 return "";
13757 }
13758
13759
13760 /* Count up to COUNT lines starting from START / START_BYTE.
13761 But don't go beyond LIMIT_BYTE.
13762 Return the number of lines thus found (always nonnegative).
13763
13764 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
13765
13766 static int
13767 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
13768 int start, start_byte, limit_byte, count;
13769 int *byte_pos_ptr;
13770 {
13771 register unsigned char *cursor;
13772 unsigned char *base;
13773
13774 register int ceiling;
13775 register unsigned char *ceiling_addr;
13776 int orig_count = count;
13777
13778 /* If we are not in selective display mode,
13779 check only for newlines. */
13780 int selective_display = (!NILP (current_buffer->selective_display)
13781 && !INTEGERP (current_buffer->selective_display));
13782
13783 if (count > 0)
13784 {
13785 while (start_byte < limit_byte)
13786 {
13787 ceiling = BUFFER_CEILING_OF (start_byte);
13788 ceiling = min (limit_byte - 1, ceiling);
13789 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
13790 base = (cursor = BYTE_POS_ADDR (start_byte));
13791 while (1)
13792 {
13793 if (selective_display)
13794 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
13795 ;
13796 else
13797 while (*cursor != '\n' && ++cursor != ceiling_addr)
13798 ;
13799
13800 if (cursor != ceiling_addr)
13801 {
13802 if (--count == 0)
13803 {
13804 start_byte += cursor - base + 1;
13805 *byte_pos_ptr = start_byte;
13806 return orig_count;
13807 }
13808 else
13809 if (++cursor == ceiling_addr)
13810 break;
13811 }
13812 else
13813 break;
13814 }
13815 start_byte += cursor - base;
13816 }
13817 }
13818 else
13819 {
13820 while (start_byte > limit_byte)
13821 {
13822 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
13823 ceiling = max (limit_byte, ceiling);
13824 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
13825 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
13826 while (1)
13827 {
13828 if (selective_display)
13829 while (--cursor != ceiling_addr
13830 && *cursor != '\n' && *cursor != 015)
13831 ;
13832 else
13833 while (--cursor != ceiling_addr && *cursor != '\n')
13834 ;
13835
13836 if (cursor != ceiling_addr)
13837 {
13838 if (++count == 0)
13839 {
13840 start_byte += cursor - base + 1;
13841 *byte_pos_ptr = start_byte;
13842 /* When scanning backwards, we should
13843 not count the newline posterior to which we stop. */
13844 return - orig_count - 1;
13845 }
13846 }
13847 else
13848 break;
13849 }
13850 /* Here we add 1 to compensate for the last decrement
13851 of CURSOR, which took it past the valid range. */
13852 start_byte += cursor - base + 1;
13853 }
13854 }
13855
13856 *byte_pos_ptr = limit_byte;
13857
13858 if (count < 0)
13859 return - orig_count + count;
13860 return orig_count - count;
13861
13862 }
13863
13864
13865 \f
13866 /***********************************************************************
13867 Displaying strings
13868 ***********************************************************************/
13869
13870 /* Display a NUL-terminated string, starting with index START.
13871
13872 If STRING is non-null, display that C string. Otherwise, the Lisp
13873 string LISP_STRING is displayed.
13874
13875 If FACE_STRING is not nil, FACE_STRING_POS is a position in
13876 FACE_STRING. Display STRING or LISP_STRING with the face at
13877 FACE_STRING_POS in FACE_STRING:
13878
13879 Display the string in the environment given by IT, but use the
13880 standard display table, temporarily.
13881
13882 FIELD_WIDTH is the minimum number of output glyphs to produce.
13883 If STRING has fewer characters than FIELD_WIDTH, pad to the right
13884 with spaces. If STRING has more characters, more than FIELD_WIDTH
13885 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
13886
13887 PRECISION is the maximum number of characters to output from
13888 STRING. PRECISION < 0 means don't truncate the string.
13889
13890 This is roughly equivalent to printf format specifiers:
13891
13892 FIELD_WIDTH PRECISION PRINTF
13893 ----------------------------------------
13894 -1 -1 %s
13895 -1 10 %.10s
13896 10 -1 %10s
13897 20 10 %20.10s
13898
13899 MULTIBYTE zero means do not display multibyte chars, > 0 means do
13900 display them, and < 0 means obey the current buffer's value of
13901 enable_multibyte_characters.
13902
13903 Value is the number of glyphs produced. */
13904
13905 static int
13906 display_string (string, lisp_string, face_string, face_string_pos,
13907 start, it, field_width, precision, max_x, multibyte)
13908 unsigned char *string;
13909 Lisp_Object lisp_string;
13910 Lisp_Object face_string;
13911 int face_string_pos;
13912 int start;
13913 struct it *it;
13914 int field_width, precision, max_x;
13915 int multibyte;
13916 {
13917 int hpos_at_start = it->hpos;
13918 int saved_face_id = it->face_id;
13919 struct glyph_row *row = it->glyph_row;
13920
13921 /* Initialize the iterator IT for iteration over STRING beginning
13922 with index START. We assume that IT may be modified here (which
13923 means that display_line has to do something when displaying a
13924 mini-buffer prompt, which it does). */
13925 reseat_to_string (it, string, lisp_string, start,
13926 precision, field_width, multibyte);
13927
13928 /* If displaying STRING, set up the face of the iterator
13929 from LISP_STRING, if that's given. */
13930 if (STRINGP (face_string))
13931 {
13932 int endptr;
13933 struct face *face;
13934
13935 it->face_id
13936 = face_at_string_position (it->w, face_string, face_string_pos,
13937 0, it->region_beg_charpos,
13938 it->region_end_charpos,
13939 &endptr, it->base_face_id, 0);
13940 face = FACE_FROM_ID (it->f, it->face_id);
13941 it->face_box_p = face->box != FACE_NO_BOX;
13942 }
13943
13944 /* Set max_x to the maximum allowed X position. Don't let it go
13945 beyond the right edge of the window. */
13946 if (max_x <= 0)
13947 max_x = it->last_visible_x;
13948 else
13949 max_x = min (max_x, it->last_visible_x);
13950
13951 /* Skip over display elements that are not visible. because IT->w is
13952 hscrolled. */
13953 if (it->current_x < it->first_visible_x)
13954 move_it_in_display_line_to (it, 100000, it->first_visible_x,
13955 MOVE_TO_POS | MOVE_TO_X);
13956
13957 row->ascent = it->max_ascent;
13958 row->height = it->max_ascent + it->max_descent;
13959 row->phys_ascent = it->max_phys_ascent;
13960 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
13961
13962 /* This condition is for the case that we are called with current_x
13963 past last_visible_x. */
13964 while (it->current_x < max_x)
13965 {
13966 int x_before, x, n_glyphs_before, i, nglyphs;
13967
13968 /* Get the next display element. */
13969 if (!get_next_display_element (it))
13970 break;
13971
13972 /* Produce glyphs. */
13973 x_before = it->current_x;
13974 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
13975 PRODUCE_GLYPHS (it);
13976
13977 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
13978 i = 0;
13979 x = x_before;
13980 while (i < nglyphs)
13981 {
13982 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
13983
13984 if (!it->truncate_lines_p
13985 && x + glyph->pixel_width > max_x)
13986 {
13987 /* End of continued line or max_x reached. */
13988 if (CHAR_GLYPH_PADDING_P (*glyph))
13989 {
13990 /* A wide character is unbreakable. */
13991 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
13992 it->current_x = x_before;
13993 }
13994 else
13995 {
13996 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
13997 it->current_x = x;
13998 }
13999 break;
14000 }
14001 else if (x + glyph->pixel_width > it->first_visible_x)
14002 {
14003 /* Glyph is at least partially visible. */
14004 ++it->hpos;
14005 if (x < it->first_visible_x)
14006 it->glyph_row->x = x - it->first_visible_x;
14007 }
14008 else
14009 {
14010 /* Glyph is off the left margin of the display area.
14011 Should not happen. */
14012 abort ();
14013 }
14014
14015 row->ascent = max (row->ascent, it->max_ascent);
14016 row->height = max (row->height, it->max_ascent + it->max_descent);
14017 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14018 row->phys_height = max (row->phys_height,
14019 it->max_phys_ascent + it->max_phys_descent);
14020 x += glyph->pixel_width;
14021 ++i;
14022 }
14023
14024 /* Stop if max_x reached. */
14025 if (i < nglyphs)
14026 break;
14027
14028 /* Stop at line ends. */
14029 if (ITERATOR_AT_END_OF_LINE_P (it))
14030 {
14031 it->continuation_lines_width = 0;
14032 break;
14033 }
14034
14035 set_iterator_to_next (it, 1);
14036
14037 /* Stop if truncating at the right edge. */
14038 if (it->truncate_lines_p
14039 && it->current_x >= it->last_visible_x)
14040 {
14041 /* Add truncation mark, but don't do it if the line is
14042 truncated at a padding space. */
14043 if (IT_CHARPOS (*it) < it->string_nchars)
14044 {
14045 if (!FRAME_WINDOW_P (it->f))
14046 {
14047 int i, n;
14048
14049 if (it->current_x > it->last_visible_x)
14050 {
14051 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
14052 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
14053 break;
14054 for (n = row->used[TEXT_AREA]; i < n; ++i)
14055 {
14056 row->used[TEXT_AREA] = i;
14057 produce_special_glyphs (it, IT_TRUNCATION);
14058 }
14059 }
14060 produce_special_glyphs (it, IT_TRUNCATION);
14061 }
14062 it->glyph_row->truncated_on_right_p = 1;
14063 }
14064 break;
14065 }
14066 }
14067
14068 /* Maybe insert a truncation at the left. */
14069 if (it->first_visible_x
14070 && IT_CHARPOS (*it) > 0)
14071 {
14072 if (!FRAME_WINDOW_P (it->f))
14073 insert_left_trunc_glyphs (it);
14074 it->glyph_row->truncated_on_left_p = 1;
14075 }
14076
14077 it->face_id = saved_face_id;
14078
14079 /* Value is number of columns displayed. */
14080 return it->hpos - hpos_at_start;
14081 }
14082
14083
14084 \f
14085 /* This is like a combination of memq and assq. Return 1 if PROPVAL
14086 appears as an element of LIST or as the car of an element of LIST.
14087 If PROPVAL is a list, compare each element against LIST in that
14088 way, and return 1 if any element of PROPVAL is found in LIST.
14089 Otherwise return 0. This function cannot quit. */
14090
14091 int
14092 invisible_p (propval, list)
14093 register Lisp_Object propval;
14094 Lisp_Object list;
14095 {
14096 register Lisp_Object tail, proptail;
14097
14098 for (tail = list; CONSP (tail); tail = XCDR (tail))
14099 {
14100 register Lisp_Object tem;
14101 tem = XCAR (tail);
14102 if (EQ (propval, tem))
14103 return 1;
14104 if (CONSP (tem) && EQ (propval, XCAR (tem)))
14105 return 1;
14106 }
14107
14108 if (CONSP (propval))
14109 {
14110 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
14111 {
14112 Lisp_Object propelt;
14113 propelt = XCAR (proptail);
14114 for (tail = list; CONSP (tail); tail = XCDR (tail))
14115 {
14116 register Lisp_Object tem;
14117 tem = XCAR (tail);
14118 if (EQ (propelt, tem))
14119 return 1;
14120 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
14121 return 1;
14122 }
14123 }
14124 }
14125
14126 return 0;
14127 }
14128
14129
14130 /* Return 1 if PROPVAL appears as the car of an element of LIST and
14131 the cdr of that element is non-nil. If PROPVAL is a list, check
14132 each element of PROPVAL in that way, and the first time some
14133 element is found, return 1 if the cdr of that element is non-nil.
14134 Otherwise return 0. This function cannot quit. */
14135
14136 int
14137 invisible_ellipsis_p (propval, list)
14138 register Lisp_Object propval;
14139 Lisp_Object list;
14140 {
14141 register Lisp_Object tail, proptail;
14142
14143 for (tail = list; CONSP (tail); tail = XCDR (tail))
14144 {
14145 register Lisp_Object tem;
14146 tem = XCAR (tail);
14147 if (CONSP (tem) && EQ (propval, XCAR (tem)))
14148 return ! NILP (XCDR (tem));
14149 }
14150
14151 if (CONSP (propval))
14152 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
14153 {
14154 Lisp_Object propelt;
14155 propelt = XCAR (proptail);
14156 for (tail = list; CONSP (tail); tail = XCDR (tail))
14157 {
14158 register Lisp_Object tem;
14159 tem = XCAR (tail);
14160 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
14161 return ! NILP (XCDR (tem));
14162 }
14163 }
14164
14165 return 0;
14166 }
14167
14168
14169 \f
14170 /***********************************************************************
14171 Initialization
14172 ***********************************************************************/
14173
14174 void
14175 syms_of_xdisp ()
14176 {
14177 Vwith_echo_area_save_vector = Qnil;
14178 staticpro (&Vwith_echo_area_save_vector);
14179
14180 Vmessage_stack = Qnil;
14181 staticpro (&Vmessage_stack);
14182
14183 Qinhibit_redisplay = intern ("inhibit-redisplay");
14184 staticpro (&Qinhibit_redisplay);
14185
14186 #if GLYPH_DEBUG
14187 defsubr (&Sdump_glyph_matrix);
14188 defsubr (&Sdump_glyph_row);
14189 defsubr (&Sdump_tool_bar_row);
14190 defsubr (&Strace_redisplay_toggle);
14191 defsubr (&Strace_to_stderr);
14192 #endif
14193 #ifdef HAVE_WINDOW_SYSTEM
14194 defsubr (&Stool_bar_lines_needed);
14195 #endif
14196
14197 staticpro (&Qmenu_bar_update_hook);
14198 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
14199
14200 staticpro (&Qoverriding_terminal_local_map);
14201 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
14202
14203 staticpro (&Qoverriding_local_map);
14204 Qoverriding_local_map = intern ("overriding-local-map");
14205
14206 staticpro (&Qwindow_scroll_functions);
14207 Qwindow_scroll_functions = intern ("window-scroll-functions");
14208
14209 staticpro (&Qredisplay_end_trigger_functions);
14210 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
14211
14212 staticpro (&Qinhibit_point_motion_hooks);
14213 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
14214
14215 QCdata = intern (":data");
14216 staticpro (&QCdata);
14217 Qdisplay = intern ("display");
14218 staticpro (&Qdisplay);
14219 Qspace_width = intern ("space-width");
14220 staticpro (&Qspace_width);
14221 Qraise = intern ("raise");
14222 staticpro (&Qraise);
14223 Qspace = intern ("space");
14224 staticpro (&Qspace);
14225 Qmargin = intern ("margin");
14226 staticpro (&Qmargin);
14227 Qleft_margin = intern ("left-margin");
14228 staticpro (&Qleft_margin);
14229 Qright_margin = intern ("right-margin");
14230 staticpro (&Qright_margin);
14231 Qalign_to = intern ("align-to");
14232 staticpro (&Qalign_to);
14233 QCalign_to = intern (":align-to");
14234 staticpro (&QCalign_to);
14235 Qrelative_width = intern ("relative-width");
14236 staticpro (&Qrelative_width);
14237 QCrelative_width = intern (":relative-width");
14238 staticpro (&QCrelative_width);
14239 QCrelative_height = intern (":relative-height");
14240 staticpro (&QCrelative_height);
14241 QCeval = intern (":eval");
14242 staticpro (&QCeval);
14243 Qwhen = intern ("when");
14244 staticpro (&Qwhen);
14245 QCfile = intern (":file");
14246 staticpro (&QCfile);
14247 Qfontified = intern ("fontified");
14248 staticpro (&Qfontified);
14249 Qfontification_functions = intern ("fontification-functions");
14250 staticpro (&Qfontification_functions);
14251 Qtrailing_whitespace = intern ("trailing-whitespace");
14252 staticpro (&Qtrailing_whitespace);
14253 Qimage = intern ("image");
14254 staticpro (&Qimage);
14255 Qmessage_truncate_lines = intern ("message-truncate-lines");
14256 staticpro (&Qmessage_truncate_lines);
14257 Qgrow_only = intern ("grow-only");
14258 staticpro (&Qgrow_only);
14259 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
14260 staticpro (&Qinhibit_menubar_update);
14261
14262 last_arrow_position = Qnil;
14263 last_arrow_string = Qnil;
14264 staticpro (&last_arrow_position);
14265 staticpro (&last_arrow_string);
14266
14267 echo_buffer[0] = echo_buffer[1] = Qnil;
14268 staticpro (&echo_buffer[0]);
14269 staticpro (&echo_buffer[1]);
14270
14271 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
14272 staticpro (&echo_area_buffer[0]);
14273 staticpro (&echo_area_buffer[1]);
14274
14275 Vmessages_buffer_name = build_string ("*Messages*");
14276 staticpro (&Vmessages_buffer_name);
14277
14278 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
14279 "Non-nil means highlight trailing whitespace.\n\
14280 The face used for trailing whitespace is `trailing-whitespace'.");
14281 Vshow_trailing_whitespace = Qnil;
14282
14283 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
14284 "Non-nil means don't actually do any redisplay.\n\
14285 This is used for internal purposes.");
14286 Vinhibit_redisplay = Qnil;
14287
14288 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
14289 "String (or mode line construct) included (normally) in `mode-line-format'.");
14290 Vglobal_mode_string = Qnil;
14291
14292 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
14293 "Marker for where to display an arrow on top of the buffer text.\n\
14294 This must be the beginning of a line in order to work.\n\
14295 See also `overlay-arrow-string'.");
14296 Voverlay_arrow_position = Qnil;
14297
14298 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
14299 "String to display as an arrow. See also `overlay-arrow-position'.");
14300 Voverlay_arrow_string = Qnil;
14301
14302 DEFVAR_INT ("scroll-step", &scroll_step,
14303 "*The number of lines to try scrolling a window by when point moves out.\n\
14304 If that fails to bring point back on frame, point is centered instead.\n\
14305 If this is zero, point is always centered after it moves off frame.\n\
14306 If you want scrolling to always be a line at a time, you should set\n\
14307 `scroll-conservatively' to a large value rather than set this to 1.");
14308
14309 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
14310 "*Scroll up to this many lines, to bring point back on screen.\n\
14311 A value of zero means to scroll the text to center point vertically\n\
14312 in the window.");
14313 scroll_conservatively = 0;
14314
14315 DEFVAR_INT ("scroll-margin", &scroll_margin,
14316 "*Number of lines of margin at the top and bottom of a window.\n\
14317 Recenter the window whenever point gets within this many lines\n\
14318 of the top or bottom of the window.");
14319 scroll_margin = 0;
14320
14321 #if GLYPH_DEBUG
14322 DEFVAR_INT ("debug-end-pos", &debug_end_pos, "Don't ask");
14323 #endif
14324
14325 DEFVAR_BOOL ("truncate-partial-width-windows",
14326 &truncate_partial_width_windows,
14327 "*Non-nil means truncate lines in all windows less than full frame wide.");
14328 truncate_partial_width_windows = 1;
14329
14330 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
14331 "nil means display the mode-line/header-line/menu-bar in the default face.\n\
14332 Any other value means to use the appropriate face, `mode-line',\n\
14333 `header-line', or `menu' respectively.\n\
14334 \n\
14335 This variable is deprecated; please change the above faces instead.");
14336 mode_line_inverse_video = 1;
14337
14338 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
14339 "*Maximum buffer size for which line number should be displayed.\n\
14340 If the buffer is bigger than this, the line number does not appear\n\
14341 in the mode line. A value of nil means no limit.");
14342 Vline_number_display_limit = Qnil;
14343
14344 DEFVAR_INT ("line-number-display-limit-width",
14345 &line_number_display_limit_width,
14346 "*Maximum line width (in characters) for line number display.\n\
14347 If the average length of the lines near point is bigger than this, then the\n\
14348 line number may be omitted from the mode line.");
14349 line_number_display_limit_width = 200;
14350
14351 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
14352 "*Non-nil means highlight region even in nonselected windows.");
14353 highlight_nonselected_windows = 0;
14354
14355 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
14356 "Non-nil if more than one frame is visible on this display.\n\
14357 Minibuffer-only frames don't count, but iconified frames do.\n\
14358 This variable is not guaranteed to be accurate except while processing\n\
14359 `frame-title-format' and `icon-title-format'.");
14360
14361 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
14362 "Template for displaying the title bar of visible frames.\n\
14363 \(Assuming the window manager supports this feature.)\n\
14364 This variable has the same structure as `mode-line-format' (which see),\n\
14365 and is used only on frames for which no explicit name has been set\n\
14366 \(see `modify-frame-parameters').");
14367 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
14368 "Template for displaying the title bar of an iconified frame.\n\
14369 \(Assuming the window manager supports this feature.)\n\
14370 This variable has the same structure as `mode-line-format' (which see),\n\
14371 and is used only on frames for which no explicit name has been set\n\
14372 \(see `modify-frame-parameters').");
14373 Vicon_title_format
14374 = Vframe_title_format
14375 = Fcons (intern ("multiple-frames"),
14376 Fcons (build_string ("%b"),
14377 Fcons (Fcons (build_string (""),
14378 Fcons (intern ("invocation-name"),
14379 Fcons (build_string ("@"),
14380 Fcons (intern ("system-name"),
14381 Qnil)))),
14382 Qnil)));
14383
14384 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
14385 "Maximum number of lines to keep in the message log buffer.\n\
14386 If nil, disable message logging. If t, log messages but don't truncate\n\
14387 the buffer when it becomes large.");
14388 Vmessage_log_max = make_number (50);
14389
14390 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
14391 "Functions called before redisplay, if window sizes have changed.\n\
14392 The value should be a list of functions that take one argument.\n\
14393 Just before redisplay, for each frame, if any of its windows have changed\n\
14394 size since the last redisplay, or have been split or deleted,\n\
14395 all the functions in the list are called, with the frame as argument.");
14396 Vwindow_size_change_functions = Qnil;
14397
14398 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
14399 "List of Functions to call before redisplaying a window with scrolling.\n\
14400 Each function is called with two arguments, the window\n\
14401 and its new display-start position. Note that the value of `window-end'\n\
14402 is not valid when these functions are called.");
14403 Vwindow_scroll_functions = Qnil;
14404
14405 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
14406 "*Non-nil means automatically resize tool-bars.\n\
14407 This increases a tool-bar's height if not all tool-bar items are visible.\n\
14408 It decreases a tool-bar's height when it would display blank lines\n\
14409 otherwise.");
14410 auto_resize_tool_bars_p = 1;
14411
14412 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
14413 "*Non-nil means raise tool-bar buttons when the mouse moves over them.");
14414 auto_raise_tool_bar_buttons_p = 1;
14415
14416 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
14417 "*Margin around tool-bar buttons in pixels.\n\
14418 If an integer, use that for both horizontal and vertical margins.\n\
14419 Otherwise, value should be a pair of integers `(HORZ : VERT)' with\n\
14420 HORZ specifying the horizontal margin, and VERT specifying the\n\
14421 vertical margin.");
14422 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
14423
14424 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
14425 "Relief thickness of tool-bar buttons.");
14426 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
14427
14428 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
14429 "List of functions to call to fontify regions of text.\n\
14430 Each function is called with one argument POS. Functions must\n\
14431 fontify a region starting at POS in the current buffer, and give\n\
14432 fontified regions the property `fontified'.\n\
14433 This variable automatically becomes buffer-local when set.");
14434 Vfontification_functions = Qnil;
14435 Fmake_variable_buffer_local (Qfontification_functions);
14436
14437 DEFVAR_BOOL ("unibyte-display-via-language-environment",
14438 &unibyte_display_via_language_environment,
14439 "*Non-nil means display unibyte text according to language environment.\n\
14440 Specifically this means that unibyte non-ASCII characters\n\
14441 are displayed by converting them to the equivalent multibyte characters\n\
14442 according to the current language environment. As a result, they are\n\
14443 displayed according to the current fontset.");
14444 unibyte_display_via_language_environment = 0;
14445
14446 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
14447 "*Maximum height for resizing mini-windows.\n\
14448 If a float, it specifies a fraction of the mini-window frame's height.\n\
14449 If an integer, it specifies a number of lines.");
14450 Vmax_mini_window_height = make_float (0.25);
14451
14452 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
14453 "*How to resize mini-windows.\n\
14454 A value of nil means don't automatically resize mini-windows.\n\
14455 A value of t means resize them to fit the text displayed in them.\n\
14456 A value of `grow-only', the default, means let mini-windows grow\n\
14457 only, until their display becomes empty, at which point the windows\n\
14458 go back to their normal size.");
14459 Vresize_mini_windows = Qgrow_only;
14460
14461 DEFVAR_BOOL ("cursor-in-non-selected-windows",
14462 &cursor_in_non_selected_windows,
14463 "*Non-nil means display a hollow cursor in non-selected windows.\n\
14464 Nil means don't display a cursor there.");
14465 cursor_in_non_selected_windows = 1;
14466
14467 DEFVAR_BOOL ("automatic-hscrolling", &automatic_hscrolling_p,
14468 "*Non-nil means scroll the display automatically to make point visible.");
14469 automatic_hscrolling_p = 1;
14470
14471 DEFVAR_LISP ("image-types", &Vimage_types,
14472 "List of supported image types.\n\
14473 Each element of the list is a symbol for a supported image type.");
14474 Vimage_types = Qnil;
14475
14476 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
14477 "If non-nil, messages are truncated instead of resizing the echo area.\n\
14478 Bind this around calls to `message' to let it take effect.");
14479 message_truncate_lines = 0;
14480
14481 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
14482 "Normal hook run for clicks on menu bar, before displaying a submenu.\n\
14483 Can be used to update submenus whose contents should vary.");
14484 Vmenu_bar_update_hook = Qnil;
14485
14486 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
14487 "Non-nil means don't update menu bars. Internal use only.");
14488 inhibit_menubar_update = 0;
14489 }
14490
14491
14492 /* Initialize this module when Emacs starts. */
14493
14494 void
14495 init_xdisp ()
14496 {
14497 Lisp_Object root_window;
14498 struct window *mini_w;
14499
14500 current_header_line_height = current_mode_line_height = -1;
14501
14502 CHARPOS (this_line_start_pos) = 0;
14503
14504 mini_w = XWINDOW (minibuf_window);
14505 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
14506
14507 if (!noninteractive)
14508 {
14509 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
14510 int i;
14511
14512 XWINDOW (root_window)->top = make_number (FRAME_TOP_MARGIN (f));
14513 set_window_height (root_window,
14514 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
14515 0);
14516 mini_w->top = make_number (FRAME_HEIGHT (f) - 1);
14517 set_window_height (minibuf_window, 1, 0);
14518
14519 XWINDOW (root_window)->width = make_number (FRAME_WIDTH (f));
14520 mini_w->width = make_number (FRAME_WIDTH (f));
14521
14522 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
14523 scratch_glyph_row.glyphs[TEXT_AREA + 1]
14524 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
14525
14526 /* The default ellipsis glyphs `...'. */
14527 for (i = 0; i < 3; ++i)
14528 default_invis_vector[i] = make_number ('.');
14529 }
14530
14531 #ifdef HAVE_WINDOW_SYSTEM
14532 {
14533 /* Allocate the buffer for frame titles. */
14534 int size = 100;
14535 frame_title_buf = (char *) xmalloc (size);
14536 frame_title_buf_end = frame_title_buf + size;
14537 frame_title_ptr = NULL;
14538 }
14539 #endif /* HAVE_WINDOW_SYSTEM */
14540
14541 help_echo_showing_p = 0;
14542 }
14543
14544