]> code.delx.au - gnu-emacs/blob - src/xdisp.c
(try_window_id) <all changes above window start>: Adjust
[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
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 "frame.h"
174 #include "window.h"
175 #include "termchar.h"
176 #include "dispextern.h"
177 #include "buffer.h"
178 #include "charset.h"
179 #include "indent.h"
180 #include "commands.h"
181 #include "macros.h"
182 #include "disptab.h"
183 #include "termhooks.h"
184 #include "intervals.h"
185 #include "keyboard.h"
186 #include "coding.h"
187 #include "process.h"
188 #include "region-cache.h"
189
190 #ifdef HAVE_X_WINDOWS
191 #include "xterm.h"
192 #endif
193 #ifdef WINDOWSNT
194 #include "w32term.h"
195 #endif
196
197 #define min(a, b) ((a) < (b) ? (a) : (b))
198 #define max(a, b) ((a) > (b) ? (a) : (b))
199
200 #define INFINITY 10000000
201
202 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
203 extern void set_frame_menubar ();
204 extern int pending_menu_activation;
205 #endif
206
207 extern int interrupt_input;
208 extern int command_loop_level;
209
210 extern int minibuffer_auto_raise;
211
212 extern Lisp_Object Qface;
213
214 extern Lisp_Object Voverriding_local_map;
215 extern Lisp_Object Voverriding_local_map_menu_flag;
216 extern Lisp_Object Qmenu_item;
217
218 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
219 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
220 Lisp_Object Qredisplay_end_trigger_functions;
221 Lisp_Object Qinhibit_point_motion_hooks;
222 Lisp_Object QCeval, Qwhen, QCfile, QCdata;
223 Lisp_Object Qfontified;
224
225 /* Functions called to fontify regions of text. */
226
227 Lisp_Object Vfontification_functions;
228 Lisp_Object Qfontification_functions;
229
230 /* Non-zero means draw tool bar buttons raised when the mouse moves
231 over them. */
232
233 int auto_raise_tool_bar_buttons_p;
234
235 /* Margin around tool bar buttons in pixels. */
236
237 int tool_bar_button_margin;
238
239 /* Thickness of shadow to draw around tool bar buttons. */
240
241 int tool_bar_button_relief;
242
243 /* Non-zero means automatically resize tool-bars so that all tool-bar
244 items are visible, and no blank lines remain. */
245
246 int auto_resize_tool_bars_p;
247
248 /* Non-nil means don't actually do any redisplay. */
249
250 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
251
252 /* Names of text properties relevant for redisplay. */
253
254 Lisp_Object Qdisplay, Qrelative_width, Qalign_to;
255 extern Lisp_Object Qface, Qinvisible, Qimage, Qwidth;
256
257 /* Symbols used in text property values. */
258
259 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
260 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
261 Lisp_Object Qmargin;
262 extern Lisp_Object Qheight;
263
264 /* Non-nil means highlight trailing whitespace. */
265
266 Lisp_Object Vshow_trailing_whitespace;
267
268 /* Name of the face used to highlight trailing whitespace. */
269
270 Lisp_Object Qtrailing_whitespace;
271
272 /* The symbol `image' which is the car of the lists used to represent
273 images in Lisp. */
274
275 Lisp_Object Qimage;
276
277 /* Non-zero means print newline to stdout before next mini-buffer
278 message. */
279
280 int noninteractive_need_newline;
281
282 /* Non-zero means print newline to message log before next message. */
283
284 static int message_log_need_newline;
285
286 \f
287 /* The buffer position of the first character appearing entirely or
288 partially on the line of the selected window which contains the
289 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
290 redisplay optimization in redisplay_internal. */
291
292 static struct text_pos this_line_start_pos;
293
294 /* Number of characters past the end of the line above, including the
295 terminating newline. */
296
297 static struct text_pos this_line_end_pos;
298
299 /* The vertical positions and the height of this line. */
300
301 static int this_line_vpos;
302 static int this_line_y;
303 static int this_line_pixel_height;
304
305 /* X position at which this display line starts. Usually zero;
306 negative if first character is partially visible. */
307
308 static int this_line_start_x;
309
310 /* Buffer that this_line_.* variables are referring to. */
311
312 static struct buffer *this_line_buffer;
313
314 /* Nonzero means truncate lines in all windows less wide than the
315 frame. */
316
317 int truncate_partial_width_windows;
318
319 /* A flag to control how to display unibyte 8-bit character. */
320
321 int unibyte_display_via_language_environment;
322
323 /* Nonzero means we have more than one non-mini-buffer-only frame.
324 Not guaranteed to be accurate except while parsing
325 frame-title-format. */
326
327 int multiple_frames;
328
329 Lisp_Object Vglobal_mode_string;
330
331 /* Marker for where to display an arrow on top of the buffer text. */
332
333 Lisp_Object Voverlay_arrow_position;
334
335 /* String to display for the arrow. Only used on terminal frames. */
336
337 Lisp_Object Voverlay_arrow_string;
338
339 /* Values of those variables at last redisplay. However, if
340 Voverlay_arrow_position is a marker, last_arrow_position is its
341 numerical position. */
342
343 static Lisp_Object last_arrow_position, last_arrow_string;
344
345 /* Like mode-line-format, but for the title bar on a visible frame. */
346
347 Lisp_Object Vframe_title_format;
348
349 /* Like mode-line-format, but for the title bar on an iconified frame. */
350
351 Lisp_Object Vicon_title_format;
352
353 /* List of functions to call when a window's size changes. These
354 functions get one arg, a frame on which one or more windows' sizes
355 have changed. */
356
357 static Lisp_Object Vwindow_size_change_functions;
358
359 Lisp_Object Qmenu_bar_update_hook;
360
361 /* Nonzero if overlay arrow has been displayed once in this window. */
362
363 static int overlay_arrow_seen;
364
365 /* Nonzero means highlight the region even in nonselected windows. */
366
367 int highlight_nonselected_windows;
368
369 /* If cursor motion alone moves point off frame, try scrolling this
370 many lines up or down if that will bring it back. */
371
372 static int scroll_step;
373
374 /* Non-0 means scroll just far enough to bring point back on the
375 screen, when appropriate. */
376
377 static int scroll_conservatively;
378
379 /* Recenter the window whenever point gets within this many lines of
380 the top or bottom of the window. This value is translated into a
381 pixel value by multiplying it with CANON_Y_UNIT, which means that
382 there is really a fixed pixel height scroll margin. */
383
384 int scroll_margin;
385
386 /* Number of windows showing the buffer of the selected window (or
387 another buffer with the same base buffer). keyboard.c refers to
388 this. */
389
390 int buffer_shared;
391
392 /* Vector containing glyphs for an ellipsis `...'. */
393
394 static Lisp_Object default_invis_vector[3];
395
396 /* Nonzero means display mode line highlighted. */
397
398 int mode_line_inverse_video;
399
400 /* Prompt to display in front of the mini-buffer contents. */
401
402 Lisp_Object minibuf_prompt;
403
404 /* Width of current mini-buffer prompt. Only set after display_line
405 of the line that contains the prompt. */
406
407 int minibuf_prompt_width;
408 int minibuf_prompt_pixel_width;
409
410 /* This is the window where the echo area message was displayed. It
411 is always a mini-buffer window, but it may not be the same window
412 currently active as a mini-buffer. */
413
414 Lisp_Object echo_area_window;
415
416 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
417 pushes the current message and the value of
418 message_enable_multibyte on the stack, the function restore_message
419 pops the stack and displays MESSAGE again. */
420
421 Lisp_Object Vmessage_stack;
422
423 /* Nonzero means multibyte characters were enabled when the echo area
424 message was specified. */
425
426 int message_enable_multibyte;
427
428 /* True if we should redraw the mode lines on the next redisplay. */
429
430 int update_mode_lines;
431
432 /* Nonzero if window sizes or contents have changed since last
433 redisplay that finished */
434
435 int windows_or_buffers_changed;
436
437 /* Nonzero after display_mode_line if %l was used and it displayed a
438 line number. */
439
440 int line_number_displayed;
441
442 /* Maximum buffer size for which to display line numbers. */
443
444 static int line_number_display_limit;
445
446 /* line width to consider when repostioning for line number display */
447
448 static int line_number_display_limit_width;
449
450 /* Number of lines to keep in the message log buffer. t means
451 infinite. nil means don't log at all. */
452
453 Lisp_Object Vmessage_log_max;
454
455 /* Current, index 0, and last displayed echo area message. Either
456 buffers from echo_buffers, or nil to indicate no message. */
457
458 Lisp_Object echo_area_buffer[2];
459
460 /* The buffers referenced from echo_area_buffer. */
461
462 static Lisp_Object echo_buffer[2];
463
464 /* A vector saved used in with_area_buffer to reduce consing. */
465
466 static Lisp_Object Vwith_echo_area_save_vector;
467
468 /* Non-zero means display_echo_area should display the last echo area
469 message again. Set by redisplay_preserve_echo_area. */
470
471 static int display_last_displayed_message_p;
472
473 /* Nonzero if echo area is being used by print; zero if being used by
474 message. */
475
476 int message_buf_print;
477
478 /* Maximum height for resizing mini-windows. Either a float
479 specifying a fraction of the available height, or an integer
480 specifying a number of lines. */
481
482 static Lisp_Object Vmax_mini_window_height;
483
484 /* Non-zero means we want a hollow cursor in windows that are not
485 selected. Zero means there's no cursor in such windows. */
486
487 int cursor_in_non_selected_windows;
488
489 /* A scratch glyph row with contents used for generating truncation
490 glyphs. Also used in direct_output_for_insert. */
491
492 #define MAX_SCRATCH_GLYPHS 100
493 struct glyph_row scratch_glyph_row;
494 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
495
496 /* Ascent and height of the last line processed by move_it_to. */
497
498 static int last_max_ascent, last_height;
499
500 /* The maximum distance to look ahead for text properties. Values
501 that are too small let us call compute_char_face and similar
502 functions too often which is expensive. Values that are too large
503 let us call compute_char_face and alike too often because we
504 might not be interested in text properties that far away. */
505
506 #define TEXT_PROP_DISTANCE_LIMIT 100
507
508 /* Non-zero means print traces of redisplay if compiled with
509 GLYPH_DEBUG != 0. */
510
511 #if GLYPH_DEBUG
512 int trace_redisplay_p;
513 #endif
514
515 /* Non-zero means automatically scroll windows horizontally to make
516 point visible. */
517
518 int automatic_hscrolling_p;
519
520 /* Value returned from text property handlers (see below). */
521
522 enum prop_handled
523 {
524 HANDLED_NORMALLY,
525 HANDLED_RECOMPUTE_PROPS,
526 HANDLED_OVERLAY_STRING_CONSUMED,
527 HANDLED_RETURN
528 };
529
530 /* A description of text properties that redisplay is interested
531 in. */
532
533 struct props
534 {
535 /* The name of the property. */
536 Lisp_Object *name;
537
538 /* A unique index for the property. */
539 enum prop_idx idx;
540
541 /* A handler function called to set up iterator IT from the property
542 at IT's current position. Value is used to steer handle_stop. */
543 enum prop_handled (*handler) P_ ((struct it *it));
544 };
545
546 static enum prop_handled handle_face_prop P_ ((struct it *));
547 static enum prop_handled handle_invisible_prop P_ ((struct it *));
548 static enum prop_handled handle_display_prop P_ ((struct it *));
549 static enum prop_handled handle_composition_prop P_ ((struct it *));
550 static enum prop_handled handle_overlay_change P_ ((struct it *));
551 static enum prop_handled handle_fontified_prop P_ ((struct it *));
552
553 /* Properties handled by iterators. */
554
555 static struct props it_props[] =
556 {
557 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
558 /* Handle `face' before `display' because some sub-properties of
559 `display' need to know the face. */
560 {&Qface, FACE_PROP_IDX, handle_face_prop},
561 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
562 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
563 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
564 {NULL, 0, NULL}
565 };
566
567 /* Value is the position described by X. If X is a marker, value is
568 the marker_position of X. Otherwise, value is X. */
569
570 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
571
572 /* Enumeration returned by some move_it_.* functions internally. */
573
574 enum move_it_result
575 {
576 /* Not used. Undefined value. */
577 MOVE_UNDEFINED,
578
579 /* Move ended at the requested buffer position or ZV. */
580 MOVE_POS_MATCH_OR_ZV,
581
582 /* Move ended at the requested X pixel position. */
583 MOVE_X_REACHED,
584
585 /* Move within a line ended at the end of a line that must be
586 continued. */
587 MOVE_LINE_CONTINUED,
588
589 /* Move within a line ended at the end of a line that would
590 be displayed truncated. */
591 MOVE_LINE_TRUNCATED,
592
593 /* Move within a line ended at a line end. */
594 MOVE_NEWLINE_OR_CR
595 };
596
597
598 \f
599 /* Function prototypes. */
600
601 static void ensure_echo_area_buffers P_ ((void));
602 static struct glyph_row *row_containing_pos P_ ((struct window *, int,
603 struct glyph_row *,
604 struct glyph_row *));
605 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
606 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
607 static void clear_garbaged_frames P_ ((void));
608 static int current_message_1 P_ ((Lisp_Object *));
609 static int truncate_message_1 P_ ((int));
610 static int set_message_1 P_ ((char *s, Lisp_Object, int, int));
611 static int display_echo_area P_ ((struct window *));
612 static int display_echo_area_1 P_ ((struct window *));
613 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
614 static int string_char_and_length P_ ((unsigned char *, int, int *));
615 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
616 struct text_pos));
617 static int compute_window_start_on_continuation_line P_ ((struct window *));
618 static Lisp_Object eval_handler P_ ((Lisp_Object));
619 static Lisp_Object eval_form P_ ((Lisp_Object));
620 static void insert_left_trunc_glyphs P_ ((struct it *));
621 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
622 static void extend_face_to_end_of_line P_ ((struct it *));
623 static int append_space P_ ((struct it *, int));
624 static void make_cursor_line_fully_visible P_ ((struct window *));
625 static int try_scrolling P_ ((Lisp_Object, int, int, int, int));
626 static int trailing_whitespace_p P_ ((int));
627 static int message_log_check_duplicate P_ ((int, int, int, int));
628 int invisible_p P_ ((Lisp_Object, Lisp_Object));
629 int invisible_ellipsis_p P_ ((Lisp_Object, Lisp_Object));
630 static void push_it P_ ((struct it *));
631 static void pop_it P_ ((struct it *));
632 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
633 static void redisplay_internal P_ ((int));
634 static int echo_area_display P_ ((int));
635 static void redisplay_windows P_ ((Lisp_Object));
636 static void redisplay_window P_ ((Lisp_Object, int));
637 static void update_menu_bar P_ ((struct frame *, int));
638 static int try_window_reusing_current_matrix P_ ((struct window *));
639 static int try_window_id P_ ((struct window *));
640 static int display_line P_ ((struct it *));
641 static void display_mode_lines P_ ((struct window *));
642 static void display_mode_line P_ ((struct window *, enum face_id,
643 Lisp_Object));
644 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object));
645 static char *decode_mode_spec P_ ((struct window *, int, int, int));
646 static void display_menu_bar P_ ((struct window *));
647 static int display_count_lines P_ ((int, int, int, int, int *));
648 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
649 int, int, struct it *, int, int, int, int));
650 static void compute_line_metrics P_ ((struct it *));
651 static void run_redisplay_end_trigger_hook P_ ((struct it *));
652 static int get_overlay_strings P_ ((struct it *));
653 static void next_overlay_string P_ ((struct it *));
654 void set_iterator_to_next P_ ((struct it *));
655 static void reseat P_ ((struct it *, struct text_pos, int));
656 static void reseat_1 P_ ((struct it *, struct text_pos, int));
657 static void back_to_previous_visible_line_start P_ ((struct it *));
658 static void reseat_at_previous_visible_line_start P_ ((struct it *));
659 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
660 static int next_element_from_display_vector P_ ((struct it *));
661 static int next_element_from_string P_ ((struct it *));
662 static int next_element_from_c_string P_ ((struct it *));
663 static int next_element_from_buffer P_ ((struct it *));
664 static int next_element_from_composition P_ ((struct it *));
665 static int next_element_from_image P_ ((struct it *));
666 static int next_element_from_stretch P_ ((struct it *));
667 static void load_overlay_strings P_ ((struct it *));
668 static void init_from_display_pos P_ ((struct it *, struct window *,
669 struct display_pos *));
670 static void reseat_to_string P_ ((struct it *, unsigned char *,
671 Lisp_Object, int, int, int, int));
672 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
673 int, int, int));
674 void move_it_vertically_backward P_ ((struct it *, int));
675 static void init_to_row_start P_ ((struct it *, struct window *,
676 struct glyph_row *));
677 static void init_to_row_end P_ ((struct it *, struct window *,
678 struct glyph_row *));
679 static void back_to_previous_line_start P_ ((struct it *));
680 static void forward_to_next_line_start P_ ((struct it *));
681 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
682 Lisp_Object, int));
683 static struct text_pos string_pos P_ ((int, Lisp_Object));
684 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
685 static int number_of_chars P_ ((unsigned char *, int));
686 static void compute_stop_pos P_ ((struct it *));
687 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
688 Lisp_Object));
689 static int face_before_or_after_it_pos P_ ((struct it *, int));
690 static int next_overlay_change P_ ((int));
691 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
692 Lisp_Object, struct text_pos *));
693
694 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
695 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
696
697 #ifdef HAVE_WINDOW_SYSTEM
698
699 static void update_tool_bar P_ ((struct frame *, int));
700 static void build_desired_tool_bar_string P_ ((struct frame *f));
701 static int redisplay_tool_bar P_ ((struct frame *));
702 static void display_tool_bar_line P_ ((struct it *));
703
704 #endif /* HAVE_WINDOW_SYSTEM */
705
706 \f
707 /***********************************************************************
708 Window display dimensions
709 ***********************************************************************/
710
711 /* Return the window-relative maximum y + 1 for glyph rows displaying
712 text in window W. This is the height of W minus the height of a
713 mode line, if any. */
714
715 INLINE int
716 window_text_bottom_y (w)
717 struct window *w;
718 {
719 struct frame *f = XFRAME (w->frame);
720 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
721
722 if (WINDOW_WANTS_MODELINE_P (w))
723 height -= CURRENT_MODE_LINE_HEIGHT (w);
724 return height;
725 }
726
727
728 /* Return the pixel width of display area AREA of window W. AREA < 0
729 means return the total width of W, not including bitmap areas to
730 the left and right of the window. */
731
732 INLINE int
733 window_box_width (w, area)
734 struct window *w;
735 int area;
736 {
737 struct frame *f = XFRAME (w->frame);
738 int width = XFASTINT (w->width);
739
740 if (!w->pseudo_window_p)
741 {
742 width -= FRAME_SCROLL_BAR_WIDTH (f) + FRAME_FLAGS_AREA_COLS (f);
743
744 if (area == TEXT_AREA)
745 {
746 if (INTEGERP (w->left_margin_width))
747 width -= XFASTINT (w->left_margin_width);
748 if (INTEGERP (w->right_margin_width))
749 width -= XFASTINT (w->right_margin_width);
750 }
751 else if (area == LEFT_MARGIN_AREA)
752 width = (INTEGERP (w->left_margin_width)
753 ? XFASTINT (w->left_margin_width) : 0);
754 else if (area == RIGHT_MARGIN_AREA)
755 width = (INTEGERP (w->right_margin_width)
756 ? XFASTINT (w->right_margin_width) : 0);
757 }
758
759 return width * CANON_X_UNIT (f);
760 }
761
762
763 /* Return the pixel height of the display area of window W, not
764 including mode lines of W, if any.. */
765
766 INLINE int
767 window_box_height (w)
768 struct window *w;
769 {
770 struct frame *f = XFRAME (w->frame);
771 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
772
773 if (WINDOW_WANTS_MODELINE_P (w))
774 height -= CURRENT_MODE_LINE_HEIGHT (w);
775
776 if (WINDOW_WANTS_HEADER_LINE_P (w))
777 height -= CURRENT_HEADER_LINE_HEIGHT (w);
778
779 return height;
780 }
781
782
783 /* Return the frame-relative coordinate of the left edge of display
784 area AREA of window W. AREA < 0 means return the left edge of the
785 whole window, to the right of any bitmap area at the left side of
786 W. */
787
788 INLINE int
789 window_box_left (w, area)
790 struct window *w;
791 int area;
792 {
793 struct frame *f = XFRAME (w->frame);
794 int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
795
796 if (!w->pseudo_window_p)
797 {
798 x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
799 + FRAME_LEFT_FLAGS_AREA_WIDTH (f));
800
801 if (area == TEXT_AREA)
802 x += window_box_width (w, LEFT_MARGIN_AREA);
803 else if (area == RIGHT_MARGIN_AREA)
804 x += (window_box_width (w, LEFT_MARGIN_AREA)
805 + window_box_width (w, TEXT_AREA));
806 }
807
808 return x;
809 }
810
811
812 /* Return the frame-relative coordinate of the right edge of display
813 area AREA of window W. AREA < 0 means return the left edge of the
814 whole window, to the left of any bitmap area at the right side of
815 W. */
816
817 INLINE int
818 window_box_right (w, area)
819 struct window *w;
820 int area;
821 {
822 return window_box_left (w, area) + window_box_width (w, area);
823 }
824
825
826 /* Get the bounding box of the display area AREA of window W, without
827 mode lines, in frame-relative coordinates. AREA < 0 means the
828 whole window, not including bitmap areas to the left and right of
829 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
830 coordinates of the upper-left corner of the box. Return in
831 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
832
833 INLINE void
834 window_box (w, area, box_x, box_y, box_width, box_height)
835 struct window *w;
836 int area;
837 int *box_x, *box_y, *box_width, *box_height;
838 {
839 struct frame *f = XFRAME (w->frame);
840
841 *box_width = window_box_width (w, area);
842 *box_height = window_box_height (w);
843 *box_x = window_box_left (w, area);
844 *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
845 + XFASTINT (w->top) * CANON_Y_UNIT (f));
846 if (WINDOW_WANTS_HEADER_LINE_P (w))
847 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
848 }
849
850
851 /* Get the bounding box of the display area AREA of window W, without
852 mode lines. AREA < 0 means the whole window, not including bitmap
853 areas to the left and right of the window. Return in *TOP_LEFT_X
854 and TOP_LEFT_Y the frame-relative pixel coordinates of the
855 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
856 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
857 box. */
858
859 INLINE void
860 window_box_edges (w, area, top_left_x, top_left_y,
861 bottom_right_x, bottom_right_y)
862 struct window *w;
863 int area;
864 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
865 {
866 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
867 bottom_right_y);
868 *bottom_right_x += *top_left_x;
869 *bottom_right_y += *top_left_y;
870 }
871
872
873 \f
874 /***********************************************************************
875 Utilities
876 ***********************************************************************/
877
878 /* Return the next character from STR which is MAXLEN bytes long.
879 Return in *LEN the length of the character. This is like
880 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
881 we find one, we return a `?', but with the length of the illegal
882 character. */
883
884 static INLINE int
885 string_char_and_length (str, maxlen, len)
886 unsigned char *str;
887 int maxlen, *len;
888 {
889 int c;
890
891 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
892 if (!CHAR_VALID_P (c, 1))
893 /* We may not change the length here because other places in Emacs
894 don't use this function, i.e. they silently accept illegal
895 characters. */
896 c = '?';
897
898 return c;
899 }
900
901
902
903 /* Given a position POS containing a valid character and byte position
904 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
905
906 static struct text_pos
907 string_pos_nchars_ahead (pos, string, nchars)
908 struct text_pos pos;
909 Lisp_Object string;
910 int nchars;
911 {
912 xassert (STRINGP (string) && nchars >= 0);
913
914 if (STRING_MULTIBYTE (string))
915 {
916 int rest = STRING_BYTES (XSTRING (string)) - BYTEPOS (pos);
917 unsigned char *p = XSTRING (string)->data + BYTEPOS (pos);
918 int len;
919
920 while (nchars--)
921 {
922 string_char_and_length (p, rest, &len);
923 p += len, rest -= len;
924 xassert (rest >= 0);
925 CHARPOS (pos) += 1;
926 BYTEPOS (pos) += len;
927 }
928 }
929 else
930 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
931
932 return pos;
933 }
934
935
936 /* Value is the text position, i.e. character and byte position,
937 for character position CHARPOS in STRING. */
938
939 static INLINE struct text_pos
940 string_pos (charpos, string)
941 int charpos;
942 Lisp_Object string;
943 {
944 struct text_pos pos;
945 xassert (STRINGP (string));
946 xassert (charpos >= 0);
947 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
948 return pos;
949 }
950
951
952 /* Value is a text position, i.e. character and byte position, for
953 character position CHARPOS in C string S. MULTIBYTE_P non-zero
954 means recognize multibyte characters. */
955
956 static struct text_pos
957 c_string_pos (charpos, s, multibyte_p)
958 int charpos;
959 unsigned char *s;
960 int multibyte_p;
961 {
962 struct text_pos pos;
963
964 xassert (s != NULL);
965 xassert (charpos >= 0);
966
967 if (multibyte_p)
968 {
969 int rest = strlen (s), len;
970
971 SET_TEXT_POS (pos, 0, 0);
972 while (charpos--)
973 {
974 string_char_and_length (s, rest, &len);
975 s += len, rest -= len;
976 xassert (rest >= 0);
977 CHARPOS (pos) += 1;
978 BYTEPOS (pos) += len;
979 }
980 }
981 else
982 SET_TEXT_POS (pos, charpos, charpos);
983
984 return pos;
985 }
986
987
988 /* Value is the number of characters in C string S. MULTIBYTE_P
989 non-zero means recognize multibyte characters. */
990
991 static int
992 number_of_chars (s, multibyte_p)
993 unsigned char *s;
994 int multibyte_p;
995 {
996 int nchars;
997
998 if (multibyte_p)
999 {
1000 int rest = strlen (s), len;
1001 unsigned char *p = (unsigned char *) s;
1002
1003 for (nchars = 0; rest > 0; ++nchars)
1004 {
1005 string_char_and_length (p, rest, &len);
1006 rest -= len, p += len;
1007 }
1008 }
1009 else
1010 nchars = strlen (s);
1011
1012 return nchars;
1013 }
1014
1015
1016 /* Compute byte position NEWPOS->bytepos corresponding to
1017 NEWPOS->charpos. POS is a known position in string STRING.
1018 NEWPOS->charpos must be >= POS.charpos. */
1019
1020 static void
1021 compute_string_pos (newpos, pos, string)
1022 struct text_pos *newpos, pos;
1023 Lisp_Object string;
1024 {
1025 xassert (STRINGP (string));
1026 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1027
1028 if (STRING_MULTIBYTE (string))
1029 *newpos = string_pos_nchars_ahead (pos, string,
1030 CHARPOS (*newpos) - CHARPOS (pos));
1031 else
1032 BYTEPOS (*newpos) = CHARPOS (*newpos);
1033 }
1034
1035
1036 \f
1037 /***********************************************************************
1038 Lisp form evaluation
1039 ***********************************************************************/
1040
1041 /* Error handler for eval_form. */
1042
1043 static Lisp_Object
1044 eval_handler (arg)
1045 Lisp_Object arg;
1046 {
1047 return Qnil;
1048 }
1049
1050
1051 /* Evaluate SEXPR and return the result, or nil if something went
1052 wrong. */
1053
1054 static Lisp_Object
1055 eval_form (sexpr)
1056 Lisp_Object sexpr;
1057 {
1058 int count = specpdl_ptr - specpdl;
1059 Lisp_Object val;
1060 specbind (Qinhibit_redisplay, Qt);
1061 val = internal_condition_case_1 (Feval, sexpr, Qerror, eval_handler);
1062 return unbind_to (count, val);
1063 }
1064
1065
1066 \f
1067 /***********************************************************************
1068 Debugging
1069 ***********************************************************************/
1070
1071 #if 0
1072
1073 /* Define CHECK_IT to perform sanity checks on iterators.
1074 This is for debugging. It is too slow to do unconditionally. */
1075
1076 static void
1077 check_it (it)
1078 struct it *it;
1079 {
1080 if (it->method == next_element_from_string)
1081 {
1082 xassert (STRINGP (it->string));
1083 xassert (IT_STRING_CHARPOS (*it) >= 0);
1084 }
1085 else if (it->method == next_element_from_buffer)
1086 {
1087 /* Check that character and byte positions agree. */
1088 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1089 }
1090
1091 if (it->dpvec)
1092 xassert (it->current.dpvec_index >= 0);
1093 else
1094 xassert (it->current.dpvec_index < 0);
1095 }
1096
1097 #define CHECK_IT(IT) check_it ((IT))
1098
1099 #else /* not 0 */
1100
1101 #define CHECK_IT(IT) (void) 0
1102
1103 #endif /* not 0 */
1104
1105
1106 #if GLYPH_DEBUG
1107
1108 /* Check that the window end of window W is what we expect it
1109 to be---the last row in the current matrix displaying text. */
1110
1111 static void
1112 check_window_end (w)
1113 struct window *w;
1114 {
1115 if (!MINI_WINDOW_P (w)
1116 && !NILP (w->window_end_valid))
1117 {
1118 struct glyph_row *row;
1119 xassert ((row = MATRIX_ROW (w->current_matrix,
1120 XFASTINT (w->window_end_vpos)),
1121 !row->enabled_p
1122 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1123 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1124 }
1125 }
1126
1127 #define CHECK_WINDOW_END(W) check_window_end ((W))
1128
1129 #else /* not GLYPH_DEBUG */
1130
1131 #define CHECK_WINDOW_END(W) (void) 0
1132
1133 #endif /* not GLYPH_DEBUG */
1134
1135
1136 \f
1137 /***********************************************************************
1138 Iterator initialization
1139 ***********************************************************************/
1140
1141 /* Initialize IT for displaying current_buffer in window W, starting
1142 at character position CHARPOS. CHARPOS < 0 means that no buffer
1143 position is specified which is useful when the iterator is assigned
1144 a position later. BYTEPOS is the byte position corresponding to
1145 CHARPOS. BYTEPOS <= 0 means compute it from CHARPOS.
1146
1147 If ROW is not null, calls to produce_glyphs with IT as parameter
1148 will produce glyphs in that row.
1149
1150 BASE_FACE_ID is the id of a base face to use. It must be one of
1151 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID or
1152 HEADER_LINE_FACE_ID for displaying mode lines, or TOOL_BAR_FACE_ID for
1153 displaying the tool-bar.
1154
1155 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID or
1156 HEADER_LINE_FACE_ID, the iterator will be initialized to use the
1157 corresponding mode line glyph row of the desired matrix of W. */
1158
1159 void
1160 init_iterator (it, w, charpos, bytepos, row, base_face_id)
1161 struct it *it;
1162 struct window *w;
1163 int charpos, bytepos;
1164 struct glyph_row *row;
1165 enum face_id base_face_id;
1166 {
1167 int highlight_region_p;
1168
1169 /* Some precondition checks. */
1170 xassert (w != NULL && it != NULL);
1171 xassert (charpos < 0 || (charpos > 0 && charpos <= ZV));
1172
1173 /* If face attributes have been changed since the last redisplay,
1174 free realized faces now because they depend on face definitions
1175 that might have changed. */
1176 if (face_change_count)
1177 {
1178 face_change_count = 0;
1179 free_all_realized_faces (Qnil);
1180 }
1181
1182 /* Use one of the mode line rows of W's desired matrix if
1183 appropriate. */
1184 if (row == NULL)
1185 {
1186 if (base_face_id == MODE_LINE_FACE_ID)
1187 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
1188 else if (base_face_id == HEADER_LINE_FACE_ID)
1189 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
1190 }
1191
1192 /* Clear IT. */
1193 bzero (it, sizeof *it);
1194 it->current.overlay_string_index = -1;
1195 it->current.dpvec_index = -1;
1196 it->base_face_id = base_face_id;
1197
1198 /* The window in which we iterate over current_buffer: */
1199 XSETWINDOW (it->window, w);
1200 it->w = w;
1201 it->f = XFRAME (w->frame);
1202
1203 /* Extra space between lines (on window systems only). */
1204 if (base_face_id == DEFAULT_FACE_ID
1205 && FRAME_WINDOW_P (it->f))
1206 {
1207 if (NATNUMP (current_buffer->extra_line_spacing))
1208 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
1209 else if (it->f->extra_line_spacing > 0)
1210 it->extra_line_spacing = it->f->extra_line_spacing;
1211 }
1212
1213 /* If realized faces have been removed, e.g. because of face
1214 attribute changes of named faces, recompute them. */
1215 if (FRAME_FACE_CACHE (it->f)->used == 0)
1216 recompute_basic_faces (it->f);
1217
1218 /* Current value of the `space-width', and 'height' properties. */
1219 it->space_width = Qnil;
1220 it->font_height = Qnil;
1221
1222 /* Are control characters displayed as `^C'? */
1223 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
1224
1225 /* -1 means everything between a CR and the following line end
1226 is invisible. >0 means lines indented more than this value are
1227 invisible. */
1228 it->selective = (INTEGERP (current_buffer->selective_display)
1229 ? XFASTINT (current_buffer->selective_display)
1230 : (!NILP (current_buffer->selective_display)
1231 ? -1 : 0));
1232 it->selective_display_ellipsis_p
1233 = !NILP (current_buffer->selective_display_ellipses);
1234
1235 /* Display table to use. */
1236 it->dp = window_display_table (w);
1237
1238 /* Are multibyte characters enabled in current_buffer? */
1239 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1240
1241 /* Non-zero if we should highlight the region. */
1242 highlight_region_p
1243 = (!NILP (Vtransient_mark_mode)
1244 && !NILP (current_buffer->mark_active)
1245 && XMARKER (current_buffer->mark)->buffer != 0);
1246
1247 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
1248 start and end of a visible region in window IT->w. Set both to
1249 -1 to indicate no region. */
1250 if (highlight_region_p
1251 /* Maybe highlight only in selected window. */
1252 && (/* Either show region everywhere. */
1253 highlight_nonselected_windows
1254 /* Or show region in the selected window. */
1255 || w == XWINDOW (selected_window)
1256 /* Or show the region if we are in the mini-buffer and W is
1257 the window the mini-buffer refers to. */
1258 || (MINI_WINDOW_P (XWINDOW (selected_window))
1259 && w == XWINDOW (Vminibuf_scroll_window))))
1260 {
1261 int charpos = marker_position (current_buffer->mark);
1262 it->region_beg_charpos = min (PT, charpos);
1263 it->region_end_charpos = max (PT, charpos);
1264 }
1265 else
1266 it->region_beg_charpos = it->region_end_charpos = -1;
1267
1268 /* Get the position at which the redisplay_end_trigger hook should
1269 be run, if it is to be run at all. */
1270 if (MARKERP (w->redisplay_end_trigger)
1271 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
1272 it->redisplay_end_trigger_charpos
1273 = marker_position (w->redisplay_end_trigger);
1274 else if (INTEGERP (w->redisplay_end_trigger))
1275 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
1276
1277 /* Correct bogus values of tab_width. */
1278 it->tab_width = XINT (current_buffer->tab_width);
1279 if (it->tab_width <= 0 || it->tab_width > 1000)
1280 it->tab_width = 8;
1281
1282 /* Are lines in the display truncated? */
1283 it->truncate_lines_p
1284 = (base_face_id != DEFAULT_FACE_ID
1285 || XINT (it->w->hscroll)
1286 || (truncate_partial_width_windows
1287 && !WINDOW_FULL_WIDTH_P (it->w))
1288 || !NILP (current_buffer->truncate_lines));
1289
1290 /* Get dimensions of truncation and continuation glyphs. These are
1291 displayed as bitmaps under X, so we don't need them for such
1292 frames. */
1293 if (!FRAME_WINDOW_P (it->f))
1294 {
1295 if (it->truncate_lines_p)
1296 {
1297 /* We will need the truncation glyph. */
1298 xassert (it->glyph_row == NULL);
1299 produce_special_glyphs (it, IT_TRUNCATION);
1300 it->truncation_pixel_width = it->pixel_width;
1301 }
1302 else
1303 {
1304 /* We will need the continuation glyph. */
1305 xassert (it->glyph_row == NULL);
1306 produce_special_glyphs (it, IT_CONTINUATION);
1307 it->continuation_pixel_width = it->pixel_width;
1308 }
1309
1310 /* Reset these values to zero becaue the produce_special_glyphs
1311 above has changed them. */
1312 it->pixel_width = it->ascent = it->descent = 0;
1313 it->phys_ascent = it->phys_descent = 0;
1314 }
1315
1316 /* Set this after getting the dimensions of truncation and
1317 continuation glyphs, so that we don't produce glyphs when calling
1318 produce_special_glyphs, above. */
1319 it->glyph_row = row;
1320 it->area = TEXT_AREA;
1321
1322 /* Get the dimensions of the display area. The display area
1323 consists of the visible window area plus a horizontally scrolled
1324 part to the left of the window. All x-values are relative to the
1325 start of this total display area. */
1326 if (base_face_id != DEFAULT_FACE_ID)
1327 {
1328 /* Mode lines, menu bar in terminal frames. */
1329 it->first_visible_x = 0;
1330 it->last_visible_x = XFASTINT (w->width) * CANON_X_UNIT (it->f);
1331 }
1332 else
1333 {
1334 it->first_visible_x
1335 = XFASTINT (it->w->hscroll) * CANON_X_UNIT (it->f);
1336 it->last_visible_x = (it->first_visible_x
1337 + window_box_width (w, TEXT_AREA));
1338
1339 /* If we truncate lines, leave room for the truncator glyph(s) at
1340 the right margin. Otherwise, leave room for the continuation
1341 glyph(s). Truncation and continuation glyphs are not inserted
1342 for window-based redisplay. */
1343 if (!FRAME_WINDOW_P (it->f))
1344 {
1345 if (it->truncate_lines_p)
1346 it->last_visible_x -= it->truncation_pixel_width;
1347 else
1348 it->last_visible_x -= it->continuation_pixel_width;
1349 }
1350
1351 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
1352 it->current_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w) + w->vscroll;
1353 }
1354
1355 /* Leave room for a border glyph. */
1356 if (!FRAME_WINDOW_P (it->f)
1357 && !WINDOW_RIGHTMOST_P (it->w))
1358 it->last_visible_x -= 1;
1359
1360 it->last_visible_y = window_text_bottom_y (w);
1361
1362 /* For mode lines and alike, arrange for the first glyph having a
1363 left box line if the face specifies a box. */
1364 if (base_face_id != DEFAULT_FACE_ID)
1365 {
1366 struct face *face;
1367
1368 it->face_id = base_face_id;
1369
1370 /* If we have a boxed mode line, make the first character appear
1371 with a left box line. */
1372 face = FACE_FROM_ID (it->f, base_face_id);
1373 if (face->box != FACE_NO_BOX)
1374 it->start_of_box_run_p = 1;
1375 }
1376
1377 /* If a buffer position was specified, set the iterator there,
1378 getting overlays and face properties from that position. */
1379 if (charpos > 0)
1380 {
1381 it->end_charpos = ZV;
1382 it->face_id = -1;
1383 IT_CHARPOS (*it) = charpos;
1384
1385 /* Compute byte position if not specified. */
1386 if (bytepos <= 0)
1387 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
1388 else
1389 IT_BYTEPOS (*it) = bytepos;
1390
1391 /* Compute faces etc. */
1392 reseat (it, it->current.pos, 1);
1393 }
1394
1395 CHECK_IT (it);
1396 }
1397
1398
1399 /* Initialize IT for the display of window W with window start POS. */
1400
1401 void
1402 start_display (it, w, pos)
1403 struct it *it;
1404 struct window *w;
1405 struct text_pos pos;
1406 {
1407 int start_at_line_beg_p;
1408 struct glyph_row *row;
1409 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
1410 int first_y;
1411
1412 row = w->desired_matrix->rows + first_vpos;
1413 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
1414 first_y = it->current_y;
1415
1416 /* If window start is not at a line start, move back to the line
1417 start. This makes sure that we take continuation lines into
1418 account. */
1419 start_at_line_beg_p = (CHARPOS (pos) == BEGV
1420 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
1421 if (!start_at_line_beg_p)
1422 reseat_at_previous_visible_line_start (it);
1423
1424 /* If window start is not at a line start, skip forward to POS to
1425 get the correct continuation_lines_width and current_x. */
1426 if (!start_at_line_beg_p)
1427 {
1428 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
1429
1430 /* If lines are continued, this line may end in the middle of a
1431 multi-glyph character (e.g. a control character displayed as
1432 \003, or in the middle of an overlay string). In this case
1433 move_it_to above will not have taken us to the start of
1434 the continuation line but to the end of the continued line. */
1435 if (!it->truncate_lines_p && it->current_x > 0)
1436 {
1437 if (it->current.dpvec_index >= 0
1438 || it->current.overlay_string_index >= 0)
1439 {
1440 set_iterator_to_next (it);
1441 move_it_in_display_line_to (it, -1, -1, 0);
1442 }
1443 it->continuation_lines_width += it->current_x;
1444 }
1445
1446 it->current_y = first_y;
1447 it->vpos = 0;
1448 it->current_x = it->hpos = 0;
1449 }
1450
1451 #if 0 /* Don't assert the following because start_display is sometimes
1452 called intentionally with a window start that is not at a
1453 line start. Please leave this code in as a comment. */
1454
1455 /* Window start should be on a line start, now. */
1456 xassert (it->continuation_lines_width
1457 || IT_CHARPOS (it) == BEGV
1458 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
1459 #endif /* 0 */
1460 }
1461
1462
1463 /* Initialize IT for stepping through current_buffer in window W,
1464 starting at position POS that includes overlay string and display
1465 vector/ control character translation position information. */
1466
1467 static void
1468 init_from_display_pos (it, w, pos)
1469 struct it *it;
1470 struct window *w;
1471 struct display_pos *pos;
1472 {
1473 /* Keep in mind: the call to reseat in init_iterator skips invisible
1474 text, so we might end up at a position different from POS. This
1475 is only a problem when POS is a row start after a newline and an
1476 overlay starts there with an after-string, and the overlay has an
1477 invisible property. Since we don't skip invisible text in
1478 display_line and elsewhere immediately after consuming the
1479 newline before the row start, such a POS will not be in a string,
1480 but the call to init_iterator below will move us to the
1481 after-string. */
1482 init_iterator (it, w, CHARPOS (pos->pos), BYTEPOS (pos->pos),
1483 NULL, DEFAULT_FACE_ID);
1484
1485 /* If position is within an overlay string, set up IT to
1486 the right overlay string. */
1487 if (pos->overlay_string_index >= 0)
1488 {
1489 int relative_index;
1490
1491 /* We already have the first chunk of overlay strings in
1492 IT->overlay_strings. Load more until the one for
1493 pos->overlay_string_index is in IT->overlay_strings. */
1494 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
1495 {
1496 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
1497 it->current.overlay_string_index = 0;
1498 while (n--)
1499 {
1500 load_overlay_strings (it);
1501 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
1502 }
1503 }
1504
1505 it->current.overlay_string_index = pos->overlay_string_index;
1506 relative_index = (it->current.overlay_string_index
1507 % OVERLAY_STRING_CHUNK_SIZE);
1508 it->string = it->overlay_strings[relative_index];
1509 it->current.string_pos = pos->string_pos;
1510 it->method = next_element_from_string;
1511 }
1512 else if (CHARPOS (pos->string_pos) >= 0)
1513 {
1514 /* Recorded position is not in an overlay string, but in another
1515 string. This can only be a string from a `display' property.
1516 IT should already be filled with that string. */
1517 it->current.string_pos = pos->string_pos;
1518 xassert (STRINGP (it->string));
1519 }
1520
1521 /* Restore position in display vector translations or control
1522 character translations. */
1523 if (pos->dpvec_index >= 0)
1524 {
1525 /* This fills IT->dpvec. */
1526 get_next_display_element (it);
1527 xassert (it->dpvec && it->current.dpvec_index == 0);
1528 it->current.dpvec_index = pos->dpvec_index;
1529 }
1530
1531 CHECK_IT (it);
1532 }
1533
1534
1535 /* Initialize IT for stepping through current_buffer in window W
1536 starting at ROW->start. */
1537
1538 static void
1539 init_to_row_start (it, w, row)
1540 struct it *it;
1541 struct window *w;
1542 struct glyph_row *row;
1543 {
1544 init_from_display_pos (it, w, &row->start);
1545 it->continuation_lines_width = row->continuation_lines_width;
1546 CHECK_IT (it);
1547 }
1548
1549
1550 /* Initialize IT for stepping through current_buffer in window W
1551 starting in the line following ROW, i.e. starting at ROW->end. */
1552
1553 static void
1554 init_to_row_end (it, w, row)
1555 struct it *it;
1556 struct window *w;
1557 struct glyph_row *row;
1558 {
1559 init_from_display_pos (it, w, &row->end);
1560
1561 if (row->continued_p)
1562 it->continuation_lines_width = (row->continuation_lines_width
1563 + row->pixel_width);
1564 CHECK_IT (it);
1565 }
1566
1567
1568
1569 \f
1570 /***********************************************************************
1571 Text properties
1572 ***********************************************************************/
1573
1574 /* Called when IT reaches IT->stop_charpos. Handle text property and
1575 overlay changes. Set IT->stop_charpos to the next position where
1576 to stop. */
1577
1578 static void
1579 handle_stop (it)
1580 struct it *it;
1581 {
1582 enum prop_handled handled;
1583 int handle_overlay_change_p = 1;
1584 struct props *p;
1585
1586 it->dpvec = NULL;
1587 it->current.dpvec_index = -1;
1588
1589 do
1590 {
1591 handled = HANDLED_NORMALLY;
1592
1593 /* Call text property handlers. */
1594 for (p = it_props; p->handler; ++p)
1595 {
1596 handled = p->handler (it);
1597
1598 if (handled == HANDLED_RECOMPUTE_PROPS)
1599 break;
1600 else if (handled == HANDLED_RETURN)
1601 return;
1602 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
1603 handle_overlay_change_p = 0;
1604 }
1605
1606 if (handled != HANDLED_RECOMPUTE_PROPS)
1607 {
1608 /* Don't check for overlay strings below when set to deliver
1609 characters from a display vector. */
1610 if (it->method == next_element_from_display_vector)
1611 handle_overlay_change_p = 0;
1612
1613 /* Handle overlay changes. */
1614 if (handle_overlay_change_p)
1615 handled = handle_overlay_change (it);
1616
1617 /* Determine where to stop next. */
1618 if (handled == HANDLED_NORMALLY)
1619 compute_stop_pos (it);
1620 }
1621 }
1622 while (handled == HANDLED_RECOMPUTE_PROPS);
1623 }
1624
1625
1626 /* Compute IT->stop_charpos from text property and overlay change
1627 information for IT's current position. */
1628
1629 static void
1630 compute_stop_pos (it)
1631 struct it *it;
1632 {
1633 register INTERVAL iv, next_iv;
1634 Lisp_Object object, limit, position;
1635
1636 /* If nowhere else, stop at the end. */
1637 it->stop_charpos = it->end_charpos;
1638
1639 if (STRINGP (it->string))
1640 {
1641 /* Strings are usually short, so don't limit the search for
1642 properties. */
1643 object = it->string;
1644 limit = Qnil;
1645 XSETFASTINT (position, IT_STRING_CHARPOS (*it));
1646 }
1647 else
1648 {
1649 int charpos;
1650
1651 /* If next overlay change is in front of the current stop pos
1652 (which is IT->end_charpos), stop there. Note: value of
1653 next_overlay_change is point-max if no overlay change
1654 follows. */
1655 charpos = next_overlay_change (IT_CHARPOS (*it));
1656 if (charpos < it->stop_charpos)
1657 it->stop_charpos = charpos;
1658
1659 /* If showing the region, we have to stop at the region
1660 start or end because the face might change there. */
1661 if (it->region_beg_charpos > 0)
1662 {
1663 if (IT_CHARPOS (*it) < it->region_beg_charpos)
1664 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
1665 else if (IT_CHARPOS (*it) < it->region_end_charpos)
1666 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
1667 }
1668
1669 /* Set up variables for computing the stop position from text
1670 property changes. */
1671 XSETBUFFER (object, current_buffer);
1672 XSETFASTINT (limit, IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
1673 XSETFASTINT (position, IT_CHARPOS (*it));
1674
1675 }
1676
1677 /* Get the interval containing IT's position. Value is a null
1678 interval if there isn't such an interval. */
1679 iv = validate_interval_range (object, &position, &position, 0);
1680 if (!NULL_INTERVAL_P (iv))
1681 {
1682 Lisp_Object values_here[LAST_PROP_IDX];
1683 struct props *p;
1684
1685 /* Get properties here. */
1686 for (p = it_props; p->handler; ++p)
1687 values_here[p->idx] = textget (iv->plist, *p->name);
1688
1689 /* Look for an interval following iv that has different
1690 properties. */
1691 for (next_iv = next_interval (iv);
1692 (!NULL_INTERVAL_P (next_iv)
1693 && (NILP (limit)
1694 || XFASTINT (limit) > next_iv->position));
1695 next_iv = next_interval (next_iv))
1696 {
1697 for (p = it_props; p->handler; ++p)
1698 {
1699 Lisp_Object new_value;
1700
1701 new_value = textget (next_iv->plist, *p->name);
1702 if (!EQ (values_here[p->idx], new_value))
1703 break;
1704 }
1705
1706 if (p->handler)
1707 break;
1708 }
1709
1710 if (!NULL_INTERVAL_P (next_iv))
1711 {
1712 if (INTEGERP (limit)
1713 && next_iv->position >= XFASTINT (limit))
1714 /* No text property change up to limit. */
1715 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
1716 else
1717 /* Text properties change in next_iv. */
1718 it->stop_charpos = min (it->stop_charpos, next_iv->position);
1719 }
1720 }
1721
1722 xassert (STRINGP (it->string)
1723 || (it->stop_charpos >= BEGV
1724 && it->stop_charpos >= IT_CHARPOS (*it)));
1725 }
1726
1727
1728 /* Return the position of the next overlay change after POS in
1729 current_buffer. Value is point-max if no overlay change
1730 follows. This is like `next-overlay-change' but doesn't use
1731 xmalloc. */
1732
1733 static int
1734 next_overlay_change (pos)
1735 int pos;
1736 {
1737 int noverlays;
1738 int endpos;
1739 Lisp_Object *overlays;
1740 int len;
1741 int i;
1742
1743 /* Get all overlays at the given position. */
1744 len = 10;
1745 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
1746 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL);
1747 if (noverlays > len)
1748 {
1749 len = noverlays;
1750 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
1751 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL);
1752 }
1753
1754 /* If any of these overlays ends before endpos,
1755 use its ending point instead. */
1756 for (i = 0; i < noverlays; ++i)
1757 {
1758 Lisp_Object oend;
1759 int oendpos;
1760
1761 oend = OVERLAY_END (overlays[i]);
1762 oendpos = OVERLAY_POSITION (oend);
1763 endpos = min (endpos, oendpos);
1764 }
1765
1766 return endpos;
1767 }
1768
1769
1770 \f
1771 /***********************************************************************
1772 Fontification
1773 ***********************************************************************/
1774
1775 /* Handle changes in the `fontified' property of the current buffer by
1776 calling hook functions from Qfontification_functions to fontify
1777 regions of text. */
1778
1779 static enum prop_handled
1780 handle_fontified_prop (it)
1781 struct it *it;
1782 {
1783 Lisp_Object prop, pos;
1784 enum prop_handled handled = HANDLED_NORMALLY;
1785 struct gcpro gcpro1;
1786
1787 /* Get the value of the `fontified' property at IT's current buffer
1788 position. (The `fontified' property doesn't have a special
1789 meaning in strings.) If the value is nil, call functions from
1790 Qfontification_functions. */
1791 if (!STRINGP (it->string)
1792 && it->s == NULL
1793 && !NILP (Vfontification_functions)
1794 && (pos = make_number (IT_CHARPOS (*it)),
1795 prop = Fget_char_property (pos, Qfontified, Qnil),
1796 NILP (prop)))
1797 {
1798 Lisp_Object args[2];
1799
1800 GCPRO1 (pos);
1801 /* Run the hook functions. */
1802 args[0] = Qfontification_functions;
1803 args[1] = pos;
1804 Frun_hook_with_args (2, args);
1805
1806 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
1807 something. This avoids an endless loop if they failed to
1808 fontify the text for which reason ever. */
1809 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
1810 handled = HANDLED_RECOMPUTE_PROPS;
1811 UNGCPRO;
1812 }
1813
1814 return handled;
1815 }
1816
1817
1818 \f
1819 /***********************************************************************
1820 Faces
1821 ***********************************************************************/
1822
1823 /* Set up iterator IT from face properties at its current position.
1824 Called from handle_stop. */
1825
1826 static enum prop_handled
1827 handle_face_prop (it)
1828 struct it *it;
1829 {
1830 int new_face_id, next_stop;
1831
1832 if (!STRINGP (it->string))
1833 {
1834 new_face_id
1835 = face_at_buffer_position (it->w,
1836 IT_CHARPOS (*it),
1837 it->region_beg_charpos,
1838 it->region_end_charpos,
1839 &next_stop,
1840 (IT_CHARPOS (*it)
1841 + TEXT_PROP_DISTANCE_LIMIT),
1842 0);
1843
1844 /* Is this a start of a run of characters with box face?
1845 Caveat: this can be called for a freshly initialized
1846 iterator; face_id is -1 is this case. We know that the new
1847 face will not change until limit, i.e. if the new face has a
1848 box, all characters up to limit will have one. But, as
1849 usual, we don't know whether limit is really the end. */
1850 if (new_face_id != it->face_id)
1851 {
1852 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
1853
1854 /* If new face has a box but old face has not, this is
1855 the start of a run of characters with box, i.e. it has
1856 a shadow on the left side. The value of face_id of the
1857 iterator will be -1 if this is the initial call that gets
1858 the face. In this case, we have to look in front of IT's
1859 position and see whether there is a face != new_face_id. */
1860 it->start_of_box_run_p
1861 = (new_face->box != FACE_NO_BOX
1862 && (it->face_id >= 0
1863 || IT_CHARPOS (*it) == BEG
1864 || new_face_id != face_before_it_pos (it)));
1865 it->face_box_p = new_face->box != FACE_NO_BOX;
1866 }
1867 }
1868 else
1869 {
1870 new_face_id
1871 = face_at_string_position (it->w,
1872 it->string,
1873 IT_STRING_CHARPOS (*it),
1874 (it->current.overlay_string_index >= 0
1875 ? IT_CHARPOS (*it)
1876 : 0),
1877 it->region_beg_charpos,
1878 it->region_end_charpos,
1879 &next_stop,
1880 it->base_face_id);
1881
1882 #if 0 /* This shouldn't be neccessary. Let's check it. */
1883 /* If IT is used to display a mode line we would really like to
1884 use the mode line face instead of the frame's default face. */
1885 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
1886 && new_face_id == DEFAULT_FACE_ID)
1887 new_face_id = MODE_LINE_FACE_ID;
1888 #endif
1889
1890 /* Is this a start of a run of characters with box? Caveat:
1891 this can be called for a freshly allocated iterator; face_id
1892 is -1 is this case. We know that the new face will not
1893 change until the next check pos, i.e. if the new face has a
1894 box, all characters up to that position will have a
1895 box. But, as usual, we don't know whether that position
1896 is really the end. */
1897 if (new_face_id != it->face_id)
1898 {
1899 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
1900 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
1901
1902 /* If new face has a box but old face hasn't, this is the
1903 start of a run of characters with box, i.e. it has a
1904 shadow on the left side. */
1905 it->start_of_box_run_p
1906 = new_face->box && (old_face == NULL || !old_face->box);
1907 it->face_box_p = new_face->box != FACE_NO_BOX;
1908 }
1909 }
1910
1911 it->face_id = new_face_id;
1912 return HANDLED_NORMALLY;
1913 }
1914
1915
1916 /* Compute the face one character before or after the current position
1917 of IT. BEFORE_P non-zero means get the face in front of IT's
1918 position. Value is the id of the face. */
1919
1920 static int
1921 face_before_or_after_it_pos (it, before_p)
1922 struct it *it;
1923 int before_p;
1924 {
1925 int face_id, limit;
1926 int next_check_charpos;
1927 struct text_pos pos;
1928
1929 xassert (it->s == NULL);
1930
1931 if (STRINGP (it->string))
1932 {
1933 /* No face change past the end of the string (for the case
1934 we are padding with spaces). No face change before the
1935 string start. */
1936 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size
1937 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
1938 return it->face_id;
1939
1940 /* Set pos to the position before or after IT's current position. */
1941 if (before_p)
1942 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
1943 else
1944 /* For composition, we must check the character after the
1945 composition. */
1946 pos = (it->what == IT_COMPOSITION
1947 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
1948 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
1949
1950 /* Get the face for ASCII, or unibyte. */
1951 face_id
1952 = face_at_string_position (it->w,
1953 it->string,
1954 CHARPOS (pos),
1955 (it->current.overlay_string_index >= 0
1956 ? IT_CHARPOS (*it)
1957 : 0),
1958 it->region_beg_charpos,
1959 it->region_end_charpos,
1960 &next_check_charpos,
1961 it->base_face_id);
1962
1963 /* Correct the face for charsets different from ASCII. Do it
1964 for the multibyte case only. The face returned above is
1965 suitable for unibyte text if IT->string is unibyte. */
1966 if (STRING_MULTIBYTE (it->string))
1967 {
1968 unsigned char *p = XSTRING (it->string)->data + BYTEPOS (pos);
1969 int rest = STRING_BYTES (XSTRING (it->string)) - BYTEPOS (pos);
1970 int c, len;
1971 struct face *face = FACE_FROM_ID (it->f, face_id);
1972
1973 c = string_char_and_length (p, rest, &len);
1974 face_id = FACE_FOR_CHAR (it->f, face, c);
1975 }
1976 }
1977 else
1978 {
1979 if ((IT_CHARPOS (*it) >= ZV && !before_p)
1980 || (IT_CHARPOS (*it) <= BEGV && before_p))
1981 return it->face_id;
1982
1983 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
1984 pos = it->current.pos;
1985
1986 if (before_p)
1987 DEC_TEXT_POS (pos, it->multibyte_p);
1988 else
1989 {
1990 if (it->what == IT_COMPOSITION)
1991 /* For composition, we must check the position after the
1992 composition. */
1993 pos.charpos += it->cmp_len, pos.bytepos += it->len;
1994 else
1995 INC_TEXT_POS (pos, it->multibyte_p);
1996 }
1997 /* Determine face for CHARSET_ASCII, or unibyte. */
1998 face_id = face_at_buffer_position (it->w,
1999 CHARPOS (pos),
2000 it->region_beg_charpos,
2001 it->region_end_charpos,
2002 &next_check_charpos,
2003 limit, 0);
2004
2005 /* Correct the face for charsets different from ASCII. Do it
2006 for the multibyte case only. The face returned above is
2007 suitable for unibyte text if current_buffer is unibyte. */
2008 if (it->multibyte_p)
2009 {
2010 int c = FETCH_MULTIBYTE_CHAR (CHARPOS (pos));
2011 struct face *face = FACE_FROM_ID (it->f, face_id);
2012 face_id = FACE_FOR_CHAR (it->f, face, c);
2013 }
2014 }
2015
2016 return face_id;
2017 }
2018
2019
2020 \f
2021 /***********************************************************************
2022 Invisible text
2023 ***********************************************************************/
2024
2025 /* Set up iterator IT from invisible properties at its current
2026 position. Called from handle_stop. */
2027
2028 static enum prop_handled
2029 handle_invisible_prop (it)
2030 struct it *it;
2031 {
2032 enum prop_handled handled = HANDLED_NORMALLY;
2033
2034 if (STRINGP (it->string))
2035 {
2036 extern Lisp_Object Qinvisible;
2037 Lisp_Object prop, end_charpos, limit, charpos;
2038
2039 /* Get the value of the invisible text property at the
2040 current position. Value will be nil if there is no such
2041 property. */
2042 XSETFASTINT (charpos, IT_STRING_CHARPOS (*it));
2043 prop = Fget_text_property (charpos, Qinvisible, it->string);
2044
2045 if (!NILP (prop))
2046 {
2047 handled = HANDLED_RECOMPUTE_PROPS;
2048
2049 /* Get the position at which the next change of the
2050 invisible text property can be found in IT->string.
2051 Value will be nil if the property value is the same for
2052 all the rest of IT->string. */
2053 XSETINT (limit, XSTRING (it->string)->size);
2054 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2055 it->string, limit);
2056
2057 /* Text at current position is invisible. The next
2058 change in the property is at position end_charpos.
2059 Move IT's current position to that position. */
2060 if (INTEGERP (end_charpos)
2061 && XFASTINT (end_charpos) < XFASTINT (limit))
2062 {
2063 struct text_pos old;
2064 old = it->current.string_pos;
2065 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
2066 compute_string_pos (&it->current.string_pos, old, it->string);
2067 }
2068 else
2069 {
2070 /* The rest of the string is invisible. If this is an
2071 overlay string, proceed with the next overlay string
2072 or whatever comes and return a character from there. */
2073 if (it->current.overlay_string_index >= 0)
2074 {
2075 next_overlay_string (it);
2076 /* Don't check for overlay strings when we just
2077 finished processing them. */
2078 handled = HANDLED_OVERLAY_STRING_CONSUMED;
2079 }
2080 else
2081 {
2082 struct Lisp_String *s = XSTRING (it->string);
2083 IT_STRING_CHARPOS (*it) = s->size;
2084 IT_STRING_BYTEPOS (*it) = STRING_BYTES (s);
2085 }
2086 }
2087 }
2088 }
2089 else
2090 {
2091 int visible_p, newpos, next_stop;
2092 Lisp_Object pos, prop;
2093
2094 /* First of all, is there invisible text at this position? */
2095 XSETFASTINT (pos, IT_CHARPOS (*it));
2096 prop = Fget_char_property (pos, Qinvisible, it->window);
2097
2098 /* If we are on invisible text, skip over it. */
2099 if (TEXT_PROP_MEANS_INVISIBLE (prop))
2100 {
2101 /* Record whether we have to display an ellipsis for the
2102 invisible text. */
2103 int display_ellipsis_p
2104 = TEXT_PROP_MEANS_INVISIBLE_WITH_ELLIPSIS (prop);
2105
2106 handled = HANDLED_RECOMPUTE_PROPS;
2107
2108 /* Loop skipping over invisible text. The loop is left at
2109 ZV or with IT on the first char being visible again. */
2110 do
2111 {
2112 /* Try to skip some invisible text. Return value is the
2113 position reached which can be equal to IT's position
2114 if there is nothing invisible here. This skips both
2115 over invisible text properties and overlays with
2116 invisible property. */
2117 newpos = skip_invisible (IT_CHARPOS (*it),
2118 &next_stop, ZV, it->window);
2119
2120 /* If we skipped nothing at all we weren't at invisible
2121 text in the first place. If everything to the end of
2122 the buffer was skipped, end the loop. */
2123 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
2124 visible_p = 1;
2125 else
2126 {
2127 /* We skipped some characters but not necessarily
2128 all there are. Check if we ended up on visible
2129 text. Fget_char_property returns the property of
2130 the char before the given position, i.e. if we
2131 get visible_p = 1, this means that the char at
2132 newpos is visible. */
2133 XSETFASTINT (pos, newpos);
2134 prop = Fget_char_property (pos, Qinvisible, it->window);
2135 visible_p = !TEXT_PROP_MEANS_INVISIBLE (prop);
2136 }
2137
2138 /* If we ended up on invisible text, proceed to
2139 skip starting with next_stop. */
2140 if (!visible_p)
2141 IT_CHARPOS (*it) = next_stop;
2142 }
2143 while (!visible_p);
2144
2145 /* The position newpos is now either ZV or on visible text. */
2146 IT_CHARPOS (*it) = newpos;
2147 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2148
2149 /* Maybe return `...' next for the end of the invisible text. */
2150 if (display_ellipsis_p)
2151 {
2152 if (it->dp
2153 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
2154 {
2155 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
2156 it->dpvec = v->contents;
2157 it->dpend = v->contents + v->size;
2158 }
2159 else
2160 {
2161 /* Default `...'. */
2162 it->dpvec = default_invis_vector;
2163 it->dpend = default_invis_vector + 3;
2164 }
2165
2166 /* The ellipsis display does not replace the display of
2167 the character at the new position. Indicate this by
2168 setting IT->dpvec_char_len to zero. */
2169 it->dpvec_char_len = 0;
2170
2171 it->current.dpvec_index = 0;
2172 it->method = next_element_from_display_vector;
2173 }
2174 }
2175 }
2176
2177 return handled;
2178 }
2179
2180
2181 \f
2182 /***********************************************************************
2183 'display' property
2184 ***********************************************************************/
2185
2186 /* Set up iterator IT from `display' property at its current position.
2187 Called from handle_stop. */
2188
2189 static enum prop_handled
2190 handle_display_prop (it)
2191 struct it *it;
2192 {
2193 Lisp_Object prop, object;
2194 struct text_pos *position;
2195 int space_or_image_found_p;
2196
2197 if (STRINGP (it->string))
2198 {
2199 object = it->string;
2200 position = &it->current.string_pos;
2201 }
2202 else
2203 {
2204 object = Qnil;
2205 position = &it->current.pos;
2206 }
2207
2208 /* Reset those iterator values set from display property values. */
2209 it->font_height = Qnil;
2210 it->space_width = Qnil;
2211 it->voffset = 0;
2212
2213 /* We don't support recursive `display' properties, i.e. string
2214 values that have a string `display' property, that have a string
2215 `display' property etc. */
2216 if (!it->string_from_display_prop_p)
2217 it->area = TEXT_AREA;
2218
2219 prop = Fget_char_property (make_number (position->charpos),
2220 Qdisplay, object);
2221 if (NILP (prop))
2222 return HANDLED_NORMALLY;
2223
2224 space_or_image_found_p = 0;
2225 if (CONSP (prop)
2226 && CONSP (XCAR (prop))
2227 && !EQ (Qmargin, XCAR (XCAR (prop))))
2228 {
2229 /* A list of sub-properties. */
2230 while (CONSP (prop))
2231 {
2232 if (handle_single_display_prop (it, XCAR (prop), object, position))
2233 space_or_image_found_p = 1;
2234 prop = XCDR (prop);
2235 }
2236 }
2237 else if (VECTORP (prop))
2238 {
2239 int i;
2240 for (i = 0; i < XVECTOR (prop)->size; ++i)
2241 if (handle_single_display_prop (it, XVECTOR (prop)->contents[i],
2242 object, position))
2243 space_or_image_found_p = 1;
2244 }
2245 else
2246 {
2247 if (handle_single_display_prop (it, prop, object, position))
2248 space_or_image_found_p = 1;
2249 }
2250
2251 return space_or_image_found_p ? HANDLED_RETURN : HANDLED_NORMALLY;
2252 }
2253
2254
2255 /* Value is the position of the end of the `display' property starting
2256 at START_POS in OBJECT. */
2257
2258 static struct text_pos
2259 display_prop_end (it, object, start_pos)
2260 struct it *it;
2261 Lisp_Object object;
2262 struct text_pos start_pos;
2263 {
2264 Lisp_Object end;
2265 struct text_pos end_pos;
2266
2267 end = next_single_char_property_change (make_number (CHARPOS (start_pos)),
2268 Qdisplay, object, Qnil);
2269 CHARPOS (end_pos) = XFASTINT (end);
2270 if (STRINGP (object))
2271 compute_string_pos (&end_pos, start_pos, it->string);
2272 else
2273 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
2274
2275 return end_pos;
2276 }
2277
2278
2279 /* Set up IT from a single `display' sub-property value PROP. OBJECT
2280 is the object in which the `display' property was found. *POSITION
2281 is the position at which it was found.
2282
2283 If PROP is a `space' or `image' sub-property, set *POSITION to the
2284 end position of the `display' property.
2285
2286 Value is non-zero if a `space' or `image' property value was found. */
2287
2288 static int
2289 handle_single_display_prop (it, prop, object, position)
2290 struct it *it;
2291 Lisp_Object prop;
2292 Lisp_Object object;
2293 struct text_pos *position;
2294 {
2295 Lisp_Object value;
2296 int space_or_image_found_p = 0;
2297
2298 Lisp_Object form;
2299
2300 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
2301 evaluated. If the result is nil, VALUE is ignored. */
2302 form = Qt;
2303 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2304 {
2305 prop = XCDR (prop);
2306 if (!CONSP (prop))
2307 return 0;
2308 form = XCAR (prop);
2309 prop = XCDR (prop);
2310 }
2311
2312 if (!NILP (form) && !EQ (form, Qt))
2313 {
2314 struct gcpro gcpro1;
2315 struct text_pos end_pos, pt;
2316
2317 end_pos = display_prop_end (it, object, *position);
2318 GCPRO1 (form);
2319
2320 /* Temporarily set point to the end position, and then evaluate
2321 the form. This makes `(eolp)' work as FORM. */
2322 CHARPOS (pt) = PT;
2323 BYTEPOS (pt) = PT_BYTE;
2324 TEMP_SET_PT_BOTH (CHARPOS (end_pos), BYTEPOS (end_pos));
2325 form = eval_form (form);
2326 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
2327 UNGCPRO;
2328 }
2329
2330 if (NILP (form))
2331 return 0;
2332
2333 if (CONSP (prop)
2334 && EQ (XCAR (prop), Qheight)
2335 && CONSP (XCDR (prop)))
2336 {
2337 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2338 return 0;
2339
2340 /* `(height HEIGHT)'. */
2341 it->font_height = XCAR (XCDR (prop));
2342 if (!NILP (it->font_height))
2343 {
2344 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2345 int new_height = -1;
2346
2347 if (CONSP (it->font_height)
2348 && (EQ (XCAR (it->font_height), Qplus)
2349 || EQ (XCAR (it->font_height), Qminus))
2350 && CONSP (XCDR (it->font_height))
2351 && INTEGERP (XCAR (XCDR (it->font_height))))
2352 {
2353 /* `(+ N)' or `(- N)' where N is an integer. */
2354 int steps = XINT (XCAR (XCDR (it->font_height)));
2355 if (EQ (XCAR (it->font_height), Qplus))
2356 steps = - steps;
2357 it->face_id = smaller_face (it->f, it->face_id, steps);
2358 }
2359 else if (SYMBOLP (it->font_height))
2360 {
2361 /* Call function with current height as argument.
2362 Value is the new height. */
2363 Lisp_Object form, height;
2364 struct gcpro gcpro1;
2365
2366 height = face->lface[LFACE_HEIGHT_INDEX];
2367 form = Fcons (it->font_height, Fcons (height, Qnil));
2368 GCPRO1 (form);
2369 height = eval_form (form);
2370 if (NUMBERP (height))
2371 new_height = XFLOATINT (height);
2372 UNGCPRO;
2373 }
2374 else if (NUMBERP (it->font_height))
2375 {
2376 /* Value is a multiple of the canonical char height. */
2377 struct face *face;
2378
2379 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
2380 new_height = (XFLOATINT (it->font_height)
2381 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
2382 }
2383 else
2384 {
2385 /* Evaluate IT->font_height with `height' bound to the
2386 current specified height to get the new height. */
2387 Lisp_Object value;
2388 int count = specpdl_ptr - specpdl;
2389
2390 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
2391 value = eval_form (it->font_height);
2392 unbind_to (count, Qnil);
2393
2394 if (NUMBERP (value))
2395 new_height = XFLOATINT (value);
2396 }
2397
2398 if (new_height > 0)
2399 it->face_id = face_with_height (it->f, it->face_id, new_height);
2400 }
2401 }
2402 else if (CONSP (prop)
2403 && EQ (XCAR (prop), Qspace_width)
2404 && CONSP (XCDR (prop)))
2405 {
2406 /* `(space_width WIDTH)'. */
2407 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2408 return 0;
2409
2410 value = XCAR (XCDR (prop));
2411 if (NUMBERP (value) && XFLOATINT (value) > 0)
2412 it->space_width = value;
2413 }
2414 else if (CONSP (prop)
2415 && EQ (XCAR (prop), Qraise)
2416 && CONSP (XCDR (prop)))
2417 {
2418 /* `(raise FACTOR)'. */
2419 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2420 return 0;
2421
2422 #ifdef HAVE_WINDOW_SYSTEM
2423 value = XCAR (XCDR (prop));
2424 if (NUMBERP (value))
2425 {
2426 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2427 it->voffset = - (XFLOATINT (value)
2428 * (FONT_HEIGHT (face->font)));
2429 }
2430 #endif /* HAVE_WINDOW_SYSTEM */
2431 }
2432 else if (!it->string_from_display_prop_p)
2433 {
2434 /* `((margin left-margin) VALUE)' or `((margin right-margin)
2435 VALUE) or `((margin nil) VALUE)' or VALUE. */
2436 Lisp_Object location, value;
2437 struct text_pos start_pos;
2438 int valid_p;
2439
2440 /* Characters having this form of property are not displayed, so
2441 we have to find the end of the property. */
2442 space_or_image_found_p = 1;
2443 start_pos = *position;
2444 *position = display_prop_end (it, object, start_pos);
2445 value = Qnil;
2446
2447 /* Let's stop at the new position and assume that all
2448 text properties change there. */
2449 it->stop_charpos = position->charpos;
2450
2451 location = Qunbound;
2452 if (CONSP (prop) && CONSP (XCAR (prop)))
2453 {
2454 Lisp_Object tem;
2455
2456 value = XCDR (prop);
2457 if (CONSP (value))
2458 value = XCAR (value);
2459
2460 tem = XCAR (prop);
2461 if (EQ (XCAR (tem), Qmargin)
2462 && (tem = XCDR (tem),
2463 tem = CONSP (tem) ? XCAR (tem) : Qnil,
2464 (NILP (tem)
2465 || EQ (tem, Qleft_margin)
2466 || EQ (tem, Qright_margin))))
2467 location = tem;
2468 }
2469
2470 if (EQ (location, Qunbound))
2471 {
2472 location = Qnil;
2473 value = prop;
2474 }
2475
2476 #ifdef HAVE_WINDOW_SYSTEM
2477 if (FRAME_TERMCAP_P (it->f))
2478 valid_p = STRINGP (value);
2479 else
2480 valid_p = (STRINGP (value)
2481 || (CONSP (value) && EQ (XCAR (value), Qspace))
2482 || valid_image_p (value));
2483 #else /* not HAVE_WINDOW_SYSTEM */
2484 valid_p = STRINGP (value);
2485 #endif /* not HAVE_WINDOW_SYSTEM */
2486
2487 if ((EQ (location, Qleft_margin)
2488 || EQ (location, Qright_margin)
2489 || NILP (location))
2490 && valid_p)
2491 {
2492 /* Save current settings of IT so that we can restore them
2493 when we are finished with the glyph property value. */
2494 push_it (it);
2495
2496 if (NILP (location))
2497 it->area = TEXT_AREA;
2498 else if (EQ (location, Qleft_margin))
2499 it->area = LEFT_MARGIN_AREA;
2500 else
2501 it->area = RIGHT_MARGIN_AREA;
2502
2503 if (STRINGP (value))
2504 {
2505 it->string = value;
2506 it->multibyte_p = STRING_MULTIBYTE (it->string);
2507 it->current.overlay_string_index = -1;
2508 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
2509 it->end_charpos = it->string_nchars
2510 = XSTRING (it->string)->size;
2511 it->method = next_element_from_string;
2512 it->stop_charpos = 0;
2513 it->string_from_display_prop_p = 1;
2514 }
2515 else if (CONSP (value) && EQ (XCAR (value), Qspace))
2516 {
2517 it->method = next_element_from_stretch;
2518 it->object = value;
2519 it->current.pos = it->position = start_pos;
2520 }
2521 #ifdef HAVE_WINDOW_SYSTEM
2522 else
2523 {
2524 it->what = IT_IMAGE;
2525 it->image_id = lookup_image (it->f, value);
2526 it->position = start_pos;
2527 it->object = NILP (object) ? it->w->buffer : object;
2528 it->method = next_element_from_image;
2529
2530 /* Say that we don't have consumed the characters with
2531 `display' property yet. The call to pop_it in
2532 set_iterator_to_next will clean this up. */
2533 *position = start_pos;
2534 }
2535 #endif /* HAVE_WINDOW_SYSTEM */
2536 }
2537 }
2538
2539 return space_or_image_found_p;
2540 }
2541
2542
2543 \f
2544 /***********************************************************************
2545 `composition' property
2546 ***********************************************************************/
2547
2548 /* Set up iterator IT from `composition' property at its current
2549 position. Called from handle_stop. */
2550
2551 static enum prop_handled
2552 handle_composition_prop (it)
2553 struct it *it;
2554 {
2555 Lisp_Object prop, string;
2556 int pos, pos_byte, end;
2557 enum prop_handled handled = HANDLED_NORMALLY;
2558
2559 if (STRINGP (it->string))
2560 {
2561 pos = IT_STRING_CHARPOS (*it);
2562 pos_byte = IT_STRING_BYTEPOS (*it);
2563 string = it->string;
2564 }
2565 else
2566 {
2567 pos = IT_CHARPOS (*it);
2568 pos_byte = IT_BYTEPOS (*it);
2569 string = Qnil;
2570 }
2571
2572 /* If there's a valid composition and point is not inside of the
2573 composition (in the case that the composition is from the current
2574 buffer), draw a glyph composed from the composition components. */
2575 if (find_composition (pos, -1, &pos, &end, &prop, string)
2576 && COMPOSITION_VALID_P (pos, end, prop)
2577 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
2578 {
2579 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
2580
2581 if (id >= 0)
2582 {
2583 it->method = next_element_from_composition;
2584 it->cmp_id = id;
2585 it->cmp_len = COMPOSITION_LENGTH (prop);
2586 /* For a terminal, draw only the first character of the
2587 components. */
2588 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
2589 it->len = (STRINGP (it->string)
2590 ? string_char_to_byte (it->string, end)
2591 : CHAR_TO_BYTE (end)) - pos_byte;
2592 it->stop_charpos = end;
2593 handled = HANDLED_RETURN;
2594 }
2595 }
2596
2597 return handled;
2598 }
2599
2600
2601 \f
2602 /***********************************************************************
2603 Overlay strings
2604 ***********************************************************************/
2605
2606 /* The following structure is used to record overlay strings for
2607 later sorting in load_overlay_strings. */
2608
2609 struct overlay_entry
2610 {
2611 Lisp_Object string;
2612 int priority;
2613 int after_string_p;
2614 };
2615
2616
2617 /* Set up iterator IT from overlay strings at its current position.
2618 Called from handle_stop. */
2619
2620 static enum prop_handled
2621 handle_overlay_change (it)
2622 struct it *it;
2623 {
2624 /* Overlays are handled in current_buffer only. */
2625 if (STRINGP (it->string))
2626 return HANDLED_NORMALLY;
2627 else
2628 return (get_overlay_strings (it)
2629 ? HANDLED_RECOMPUTE_PROPS
2630 : HANDLED_NORMALLY);
2631 }
2632
2633
2634 /* Set up the next overlay string for delivery by IT, if there is an
2635 overlay string to deliver. Called by set_iterator_to_next when the
2636 end of the current overlay string is reached. If there are more
2637 overlay strings to display, IT->string and
2638 IT->current.overlay_string_index are set appropriately here.
2639 Otherwise IT->string is set to nil. */
2640
2641 static void
2642 next_overlay_string (it)
2643 struct it *it;
2644 {
2645 ++it->current.overlay_string_index;
2646 if (it->current.overlay_string_index == it->n_overlay_strings)
2647 {
2648 /* No more overlay strings. Restore IT's settings to what
2649 they were before overlay strings were processed, and
2650 continue to deliver from current_buffer. */
2651 pop_it (it);
2652 xassert (it->stop_charpos >= BEGV
2653 && it->stop_charpos <= it->end_charpos);
2654 it->string = Qnil;
2655 it->current.overlay_string_index = -1;
2656 SET_TEXT_POS (it->current.string_pos, -1, -1);
2657 it->n_overlay_strings = 0;
2658 it->method = next_element_from_buffer;
2659 }
2660 else
2661 {
2662 /* There are more overlay strings to process. If
2663 IT->current.overlay_string_index has advanced to a position
2664 where we must load IT->overlay_strings with more strings, do
2665 it. */
2666 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
2667
2668 if (it->current.overlay_string_index && i == 0)
2669 load_overlay_strings (it);
2670
2671 /* Initialize IT to deliver display elements from the overlay
2672 string. */
2673 it->string = it->overlay_strings[i];
2674 it->multibyte_p = STRING_MULTIBYTE (it->string);
2675 SET_TEXT_POS (it->current.string_pos, 0, 0);
2676 it->method = next_element_from_string;
2677 it->stop_charpos = 0;
2678 }
2679
2680 CHECK_IT (it);
2681 }
2682
2683
2684 /* Compare two overlay_entry structures E1 and E2. Used as a
2685 comparison function for qsort in load_overlay_strings. Overlay
2686 strings for the same position are sorted so that
2687
2688 1. All after-strings come in front of before-strings.
2689
2690 2. Within after-strings, strings are sorted so that overlay strings
2691 from overlays with higher priorities come first.
2692
2693 2. Within before-strings, strings are sorted so that overlay
2694 strings from overlays with higher priorities come last.
2695
2696 Value is analogous to strcmp. */
2697
2698
2699 static int
2700 compare_overlay_entries (e1, e2)
2701 void *e1, *e2;
2702 {
2703 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
2704 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
2705 int result;
2706
2707 if (entry1->after_string_p != entry2->after_string_p)
2708 /* Let after-strings appear in front of before-strings. */
2709 result = entry1->after_string_p ? -1 : 1;
2710 else if (entry1->after_string_p)
2711 /* After-strings sorted in order of decreasing priority. */
2712 result = entry2->priority - entry1->priority;
2713 else
2714 /* Before-strings sorted in order of increasing priority. */
2715 result = entry1->priority - entry2->priority;
2716
2717 return result;
2718 }
2719
2720
2721 /* Load the vector IT->overlay_strings with overlay strings from IT's
2722 current buffer position. Set IT->n_overlays to the total number of
2723 overlay strings found.
2724
2725 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
2726 a time. On entry into load_overlay_strings,
2727 IT->current.overlay_string_index gives the number of overlay
2728 strings that have already been loaded by previous calls to this
2729 function.
2730
2731 Overlay strings are sorted so that after-string strings come in
2732 front of before-string strings. Within before and after-strings,
2733 strings are sorted by overlay priority. See also function
2734 compare_overlay_entries. */
2735
2736 static void
2737 load_overlay_strings (it)
2738 struct it *it;
2739 {
2740 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
2741 Lisp_Object ov, overlay, window, str;
2742 int start, end;
2743 int size = 20;
2744 int n = 0, i, j;
2745 struct overlay_entry *entries
2746 = (struct overlay_entry *) alloca (size * sizeof *entries);
2747
2748 /* Append the overlay string STRING of overlay OVERLAY to vector
2749 `entries' which has size `size' and currently contains `n'
2750 elements. AFTER_P non-zero means STRING is an after-string of
2751 OVERLAY. */
2752 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
2753 do \
2754 { \
2755 Lisp_Object priority; \
2756 \
2757 if (n == size) \
2758 { \
2759 int new_size = 2 * size; \
2760 struct overlay_entry *old = entries; \
2761 entries = \
2762 (struct overlay_entry *) alloca (new_size \
2763 * sizeof *entries); \
2764 bcopy (old, entries, size * sizeof *entries); \
2765 size = new_size; \
2766 } \
2767 \
2768 entries[n].string = (STRING); \
2769 priority = Foverlay_get ((OVERLAY), Qpriority); \
2770 entries[n].priority \
2771 = INTEGERP (priority) ? XFASTINT (priority) : 0; \
2772 entries[n].after_string_p = (AFTER_P); \
2773 ++n; \
2774 } \
2775 while (0)
2776
2777 /* Process overlay before the overlay center. */
2778 for (ov = current_buffer->overlays_before;
2779 CONSP (ov);
2780 ov = XCDR (ov))
2781 {
2782 overlay = XCAR (ov);
2783 xassert (OVERLAYP (overlay));
2784 start = OVERLAY_POSITION (OVERLAY_START (overlay));
2785 end = OVERLAY_POSITION (OVERLAY_END (overlay));
2786
2787 if (end < IT_CHARPOS (*it))
2788 break;
2789
2790 /* Skip this overlay if it doesn't start or end at IT's current
2791 position. */
2792 if (end != IT_CHARPOS (*it) && start != IT_CHARPOS (*it))
2793 continue;
2794
2795 /* Skip this overlay if it doesn't apply to IT->w. */
2796 window = Foverlay_get (overlay, Qwindow);
2797 if (WINDOWP (window) && XWINDOW (window) != it->w)
2798 continue;
2799
2800 /* If overlay has a non-empty before-string, record it. */
2801 if (start == IT_CHARPOS (*it)
2802 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
2803 && XSTRING (str)->size)
2804 RECORD_OVERLAY_STRING (overlay, str, 0);
2805
2806 /* If overlay has a non-empty after-string, record it. */
2807 if (end == IT_CHARPOS (*it)
2808 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
2809 && XSTRING (str)->size)
2810 RECORD_OVERLAY_STRING (overlay, str, 1);
2811 }
2812
2813 /* Process overlays after the overlay center. */
2814 for (ov = current_buffer->overlays_after;
2815 CONSP (ov);
2816 ov = XCDR (ov))
2817 {
2818 overlay = XCAR (ov);
2819 xassert (OVERLAYP (overlay));
2820 start = OVERLAY_POSITION (OVERLAY_START (overlay));
2821 end = OVERLAY_POSITION (OVERLAY_END (overlay));
2822
2823 if (start > IT_CHARPOS (*it))
2824 break;
2825
2826 /* Skip this overlay if it doesn't start or end at IT's current
2827 position. */
2828 if (end != IT_CHARPOS (*it) && start != IT_CHARPOS (*it))
2829 continue;
2830
2831 /* Skip this overlay if it doesn't apply to IT->w. */
2832 window = Foverlay_get (overlay, Qwindow);
2833 if (WINDOWP (window) && XWINDOW (window) != it->w)
2834 continue;
2835
2836 /* If overlay has a non-empty before-string, record it. */
2837 if (start == IT_CHARPOS (*it)
2838 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
2839 && XSTRING (str)->size)
2840 RECORD_OVERLAY_STRING (overlay, str, 0);
2841
2842 /* If overlay has a non-empty after-string, record it. */
2843 if (end == IT_CHARPOS (*it)
2844 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
2845 && XSTRING (str)->size)
2846 RECORD_OVERLAY_STRING (overlay, str, 1);
2847 }
2848
2849 #undef RECORD_OVERLAY_STRING
2850
2851 /* Sort entries. */
2852 qsort (entries, n, sizeof *entries, compare_overlay_entries);
2853
2854 /* Record the total number of strings to process. */
2855 it->n_overlay_strings = n;
2856
2857 /* IT->current.overlay_string_index is the number of overlay strings
2858 that have already been consumed by IT. Copy some of the
2859 remaining overlay strings to IT->overlay_strings. */
2860 i = 0;
2861 j = it->current.overlay_string_index;
2862 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
2863 it->overlay_strings[i++] = entries[j++].string;
2864
2865 CHECK_IT (it);
2866 }
2867
2868
2869 /* Get the first chunk of overlay strings at IT's current buffer
2870 position. Value is non-zero if at least one overlay string was
2871 found. */
2872
2873 static int
2874 get_overlay_strings (it)
2875 struct it *it;
2876 {
2877 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
2878 process. This fills IT->overlay_strings with strings, and sets
2879 IT->n_overlay_strings to the total number of strings to process.
2880 IT->pos.overlay_string_index has to be set temporarily to zero
2881 because load_overlay_strings needs this; it must be set to -1
2882 when no overlay strings are found because a zero value would
2883 indicate a position in the first overlay string. */
2884 it->current.overlay_string_index = 0;
2885 load_overlay_strings (it);
2886
2887 /* If we found overlay strings, set up IT to deliver display
2888 elements from the first one. Otherwise set up IT to deliver
2889 from current_buffer. */
2890 if (it->n_overlay_strings)
2891 {
2892 /* Make sure we know settings in current_buffer, so that we can
2893 restore meaningful values when we're done with the overlay
2894 strings. */
2895 compute_stop_pos (it);
2896 xassert (it->face_id >= 0);
2897
2898 /* Save IT's settings. They are restored after all overlay
2899 strings have been processed. */
2900 xassert (it->sp == 0);
2901 push_it (it);
2902
2903 /* Set up IT to deliver display elements from the first overlay
2904 string. */
2905 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
2906 it->stop_charpos = 0;
2907 it->string = it->overlay_strings[0];
2908 it->multibyte_p = STRING_MULTIBYTE (it->string);
2909 xassert (STRINGP (it->string));
2910 it->method = next_element_from_string;
2911 }
2912 else
2913 {
2914 it->string = Qnil;
2915 it->current.overlay_string_index = -1;
2916 it->method = next_element_from_buffer;
2917 }
2918
2919 CHECK_IT (it);
2920
2921 /* Value is non-zero if we found at least one overlay string. */
2922 return STRINGP (it->string);
2923 }
2924
2925
2926 \f
2927 /***********************************************************************
2928 Saving and restoring state
2929 ***********************************************************************/
2930
2931 /* Save current settings of IT on IT->stack. Called, for example,
2932 before setting up IT for an overlay string, to be able to restore
2933 IT's settings to what they were after the overlay string has been
2934 processed. */
2935
2936 static void
2937 push_it (it)
2938 struct it *it;
2939 {
2940 struct iterator_stack_entry *p;
2941
2942 xassert (it->sp < 2);
2943 p = it->stack + it->sp;
2944
2945 p->stop_charpos = it->stop_charpos;
2946 xassert (it->face_id >= 0);
2947 p->face_id = it->face_id;
2948 p->string = it->string;
2949 p->pos = it->current;
2950 p->end_charpos = it->end_charpos;
2951 p->string_nchars = it->string_nchars;
2952 p->area = it->area;
2953 p->multibyte_p = it->multibyte_p;
2954 p->space_width = it->space_width;
2955 p->font_height = it->font_height;
2956 p->voffset = it->voffset;
2957 p->string_from_display_prop_p = it->string_from_display_prop_p;
2958 ++it->sp;
2959 }
2960
2961
2962 /* Restore IT's settings from IT->stack. Called, for example, when no
2963 more overlay strings must be processed, and we return to delivering
2964 display elements from a buffer, or when the end of a string from a
2965 `display' property is reached and we return to delivering display
2966 elements from an overlay string, or from a buffer. */
2967
2968 static void
2969 pop_it (it)
2970 struct it *it;
2971 {
2972 struct iterator_stack_entry *p;
2973
2974 xassert (it->sp > 0);
2975 --it->sp;
2976 p = it->stack + it->sp;
2977 it->stop_charpos = p->stop_charpos;
2978 it->face_id = p->face_id;
2979 it->string = p->string;
2980 it->current = p->pos;
2981 it->end_charpos = p->end_charpos;
2982 it->string_nchars = p->string_nchars;
2983 it->area = p->area;
2984 it->multibyte_p = p->multibyte_p;
2985 it->space_width = p->space_width;
2986 it->font_height = p->font_height;
2987 it->voffset = p->voffset;
2988 it->string_from_display_prop_p = p->string_from_display_prop_p;
2989 }
2990
2991
2992 \f
2993 /***********************************************************************
2994 Moving over lines
2995 ***********************************************************************/
2996
2997 /* Set IT's current position to the previous line start. */
2998
2999 static void
3000 back_to_previous_line_start (it)
3001 struct it *it;
3002 {
3003 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
3004 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3005 }
3006
3007
3008 /* Set IT's current position to the next line start. */
3009
3010 static void
3011 forward_to_next_line_start (it)
3012 struct it *it;
3013 {
3014 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it), 1);
3015 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3016 }
3017
3018
3019 /* Set IT's current position to the previous visible line start. Skip
3020 invisible text that is so either due to text properties or due to
3021 selective display. Caution: this does not change IT->current_x and
3022 IT->hpos. */
3023
3024 static void
3025 back_to_previous_visible_line_start (it)
3026 struct it *it;
3027 {
3028 int visible_p = 0;
3029
3030 /* Go back one newline if not on BEGV already. */
3031 if (IT_CHARPOS (*it) > BEGV)
3032 back_to_previous_line_start (it);
3033
3034 /* Move over lines that are invisible because of selective display
3035 or text properties. */
3036 while (IT_CHARPOS (*it) > BEGV
3037 && !visible_p)
3038 {
3039 visible_p = 1;
3040
3041 /* If selective > 0, then lines indented more than that values
3042 are invisible. */
3043 if (it->selective > 0
3044 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3045 it->selective))
3046 visible_p = 0;
3047 else
3048 {
3049 Lisp_Object prop;
3050
3051 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
3052 Qinvisible, it->window);
3053 if (TEXT_PROP_MEANS_INVISIBLE (prop))
3054 visible_p = 0;
3055 }
3056
3057 /* Back one more newline if the current one is invisible. */
3058 if (!visible_p)
3059 back_to_previous_line_start (it);
3060 }
3061
3062 xassert (IT_CHARPOS (*it) >= BEGV);
3063 xassert (IT_CHARPOS (*it) == BEGV
3064 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3065 CHECK_IT (it);
3066 }
3067
3068
3069 /* Reseat iterator IT at the previous visible line start. Skip
3070 invisible text that is so either due to text properties or due to
3071 selective display. At the end, update IT's overlay information,
3072 face information etc. */
3073
3074 static void
3075 reseat_at_previous_visible_line_start (it)
3076 struct it *it;
3077 {
3078 back_to_previous_visible_line_start (it);
3079 reseat (it, it->current.pos, 1);
3080 CHECK_IT (it);
3081 }
3082
3083
3084 /* Reseat iterator IT on the next visible line start in the current
3085 buffer. ON_NEWLINE_P non-zero means position IT on the newline
3086 preceding the line start. Skip over invisible text that is so
3087 because of selective display. Compute faces, overlays etc at the
3088 new position. Note that this function does not skip over text that
3089 is invisible because of text properties. */
3090
3091 static void
3092 reseat_at_next_visible_line_start (it, on_newline_p)
3093 struct it *it;
3094 int on_newline_p;
3095 {
3096 /* Restore the buffer position when currently not delivering display
3097 elements from the current buffer. This is the case, for example,
3098 when called at the end of a truncated overlay string. */
3099 while (it->sp)
3100 pop_it (it);
3101 it->method = next_element_from_buffer;
3102
3103 /* Otherwise, scan_buffer would not work. */
3104 if (IT_CHARPOS (*it) < ZV)
3105 {
3106 /* If on a newline, advance past it. Otherwise, find the next
3107 newline which automatically gives us the position following
3108 the newline. */
3109 if (FETCH_BYTE (IT_BYTEPOS (*it)) == '\n')
3110 {
3111 ++IT_CHARPOS (*it);
3112 ++IT_BYTEPOS (*it);
3113 }
3114 else
3115 forward_to_next_line_start (it);
3116
3117 /* We must either have reached the end of the buffer or end up
3118 after a newline. */
3119 xassert (IT_CHARPOS (*it) == ZV
3120 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3121
3122 /* Skip over lines that are invisible because they are indented
3123 more than the value of IT->selective. */
3124 if (it->selective > 0)
3125 while (IT_CHARPOS (*it) < ZV
3126 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3127 it->selective))
3128 forward_to_next_line_start (it);
3129
3130 /* Position on the newline if we should. */
3131 if (on_newline_p
3132 && IT_CHARPOS (*it) > BEGV
3133 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n')
3134 {
3135 --IT_CHARPOS (*it);
3136 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3137 }
3138
3139 /* Set the iterator there. The 0 as the last parameter of
3140 reseat means don't force a text property lookup. The lookup
3141 is then only done if we've skipped past the iterator's
3142 check_charpos'es. This optimization is important because
3143 text property lookups tend to be expensive. */
3144 reseat (it, it->current.pos, 0);
3145 }
3146
3147 CHECK_IT (it);
3148 }
3149
3150
3151 \f
3152 /***********************************************************************
3153 Changing an iterator's position
3154 ***********************************************************************/
3155
3156 /* Change IT's current position to POS in current_buffer. If FORCE_P
3157 is non-zero, always check for text properties at the new position.
3158 Otherwise, text properties are only looked up if POS >=
3159 IT->check_charpos of a property. */
3160
3161 static void
3162 reseat (it, pos, force_p)
3163 struct it *it;
3164 struct text_pos pos;
3165 int force_p;
3166 {
3167 int original_pos = IT_CHARPOS (*it);
3168
3169 reseat_1 (it, pos, 0);
3170
3171 /* Determine where to check text properties. Avoid doing it
3172 where possible because text property lookup is very expensive. */
3173 if (force_p
3174 || CHARPOS (pos) > it->stop_charpos
3175 || CHARPOS (pos) < original_pos)
3176 handle_stop (it);
3177
3178 CHECK_IT (it);
3179 }
3180
3181
3182 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
3183 IT->stop_pos to POS, also. */
3184
3185 static void
3186 reseat_1 (it, pos, set_stop_p)
3187 struct it *it;
3188 struct text_pos pos;
3189 int set_stop_p;
3190 {
3191 /* Don't call this function when scanning a C string. */
3192 xassert (it->s == NULL);
3193
3194 /* POS must be a reasonable value. */
3195 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
3196
3197 it->current.pos = it->position = pos;
3198 XSETBUFFER (it->object, current_buffer);
3199 it->dpvec = NULL;
3200 it->current.dpvec_index = -1;
3201 it->current.overlay_string_index = -1;
3202 IT_STRING_CHARPOS (*it) = -1;
3203 IT_STRING_BYTEPOS (*it) = -1;
3204 it->string = Qnil;
3205 it->method = next_element_from_buffer;
3206 it->sp = 0;
3207
3208 if (set_stop_p)
3209 it->stop_charpos = CHARPOS (pos);
3210 }
3211
3212
3213 /* Set up IT for displaying a string, starting at CHARPOS in window W.
3214 If S is non-null, it is a C string to iterate over. Otherwise,
3215 STRING gives a Lisp string to iterate over.
3216
3217 If PRECISION > 0, don't return more then PRECISION number of
3218 characters from the string.
3219
3220 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
3221 characters have been returned. FIELD_WIDTH < 0 means an infinite
3222 field width.
3223
3224 MULTIBYTE = 0 means disable processing of multibyte characters,
3225 MULTIBYTE > 0 means enable it,
3226 MULTIBYTE < 0 means use IT->multibyte_p.
3227
3228 IT must be initialized via a prior call to init_iterator before
3229 calling this function. */
3230
3231 static void
3232 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
3233 struct it *it;
3234 unsigned char *s;
3235 Lisp_Object string;
3236 int charpos;
3237 int precision, field_width, multibyte;
3238 {
3239 /* No region in strings. */
3240 it->region_beg_charpos = it->region_end_charpos = -1;
3241
3242 /* No text property checks performed by default, but see below. */
3243 it->stop_charpos = -1;
3244
3245 /* Set iterator position and end position. */
3246 bzero (&it->current, sizeof it->current);
3247 it->current.overlay_string_index = -1;
3248 it->current.dpvec_index = -1;
3249 xassert (charpos >= 0);
3250
3251 /* Use the setting of MULTIBYTE if specified. */
3252 if (multibyte >= 0)
3253 it->multibyte_p = multibyte > 0;
3254
3255 if (s == NULL)
3256 {
3257 xassert (STRINGP (string));
3258 it->string = string;
3259 it->s = NULL;
3260 it->end_charpos = it->string_nchars = XSTRING (string)->size;
3261 it->method = next_element_from_string;
3262 it->current.string_pos = string_pos (charpos, string);
3263 }
3264 else
3265 {
3266 it->s = s;
3267 it->string = Qnil;
3268
3269 /* Note that we use IT->current.pos, not it->current.string_pos,
3270 for displaying C strings. */
3271 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
3272 if (it->multibyte_p)
3273 {
3274 it->current.pos = c_string_pos (charpos, s, 1);
3275 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
3276 }
3277 else
3278 {
3279 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
3280 it->end_charpos = it->string_nchars = strlen (s);
3281 }
3282
3283 it->method = next_element_from_c_string;
3284 }
3285
3286 /* PRECISION > 0 means don't return more than PRECISION characters
3287 from the string. */
3288 if (precision > 0 && it->end_charpos - charpos > precision)
3289 it->end_charpos = it->string_nchars = charpos + precision;
3290
3291 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
3292 characters have been returned. FIELD_WIDTH == 0 means don't pad,
3293 FIELD_WIDTH < 0 means infinite field width. This is useful for
3294 padding with `-' at the end of a mode line. */
3295 if (field_width < 0)
3296 field_width = INFINITY;
3297 if (field_width > it->end_charpos - charpos)
3298 it->end_charpos = charpos + field_width;
3299
3300 /* Use the standard display table for displaying strings. */
3301 if (DISP_TABLE_P (Vstandard_display_table))
3302 it->dp = XCHAR_TABLE (Vstandard_display_table);
3303
3304 it->stop_charpos = charpos;
3305 CHECK_IT (it);
3306 }
3307
3308
3309 \f
3310 /***********************************************************************
3311 Iteration
3312 ***********************************************************************/
3313
3314 /* Load IT's display element fields with information about the next
3315 display element from the current position of IT. Value is zero if
3316 end of buffer (or C string) is reached. */
3317
3318 int
3319 get_next_display_element (it)
3320 struct it *it;
3321 {
3322 /* Non-zero means that we found an display element. Zero means that
3323 we hit the end of what we iterate over. Performance note: the
3324 function pointer `method' used here turns out to be faster than
3325 using a sequence of if-statements. */
3326 int success_p = (*it->method) (it);
3327
3328 if (it->what == IT_CHARACTER)
3329 {
3330 /* Map via display table or translate control characters.
3331 IT->c, IT->len etc. have been set to the next character by
3332 the function call above. If we have a display table, and it
3333 contains an entry for IT->c, translate it. Don't do this if
3334 IT->c itself comes from a display table, otherwise we could
3335 end up in an infinite recursion. (An alternative could be to
3336 count the recursion depth of this function and signal an
3337 error when a certain maximum depth is reached.) Is it worth
3338 it? */
3339 if (success_p && it->dpvec == NULL)
3340 {
3341 Lisp_Object dv;
3342
3343 if (it->dp
3344 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
3345 VECTORP (dv)))
3346 {
3347 struct Lisp_Vector *v = XVECTOR (dv);
3348
3349 /* Return the first character from the display table
3350 entry, if not empty. If empty, don't display the
3351 current character. */
3352 if (v->size)
3353 {
3354 it->dpvec_char_len = it->len;
3355 it->dpvec = v->contents;
3356 it->dpend = v->contents + v->size;
3357 it->current.dpvec_index = 0;
3358 it->method = next_element_from_display_vector;
3359 }
3360
3361 success_p = get_next_display_element (it);
3362 }
3363
3364 /* Translate control characters into `\003' or `^C' form.
3365 Control characters coming from a display table entry are
3366 currently not translated because we use IT->dpvec to hold
3367 the translation. This could easily be changed but I
3368 don't believe that it is worth doing.
3369
3370 Non-printable multibyte characters are also translated
3371 octal form. */
3372 else if ((it->c < ' '
3373 && (it->area != TEXT_AREA
3374 || (it->c != '\n' && it->c != '\t')))
3375 || (it->c >= 127
3376 && it->len == 1)
3377 || !CHAR_PRINTABLE_P (it->c))
3378 {
3379 /* IT->c is a control character which must be displayed
3380 either as '\003' or as `^C' where the '\\' and '^'
3381 can be defined in the display table. Fill
3382 IT->ctl_chars with glyphs for what we have to
3383 display. Then, set IT->dpvec to these glyphs. */
3384 GLYPH g;
3385
3386 if (it->c < 128 && it->ctl_arrow_p)
3387 {
3388 /* Set IT->ctl_chars[0] to the glyph for `^'. */
3389 if (it->dp
3390 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
3391 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
3392 g = XINT (DISP_CTRL_GLYPH (it->dp));
3393 else
3394 g = FAST_MAKE_GLYPH ('^', 0);
3395 XSETINT (it->ctl_chars[0], g);
3396
3397 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
3398 XSETINT (it->ctl_chars[1], g);
3399
3400 /* Set up IT->dpvec and return first character from it. */
3401 it->dpvec_char_len = it->len;
3402 it->dpvec = it->ctl_chars;
3403 it->dpend = it->dpvec + 2;
3404 it->current.dpvec_index = 0;
3405 it->method = next_element_from_display_vector;
3406 get_next_display_element (it);
3407 }
3408 else
3409 {
3410 unsigned char str[MAX_MULTIBYTE_LENGTH];
3411 int len = CHAR_STRING (it->c, str);
3412 int i;
3413 GLYPH escape_glyph;
3414
3415 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
3416 if (it->dp
3417 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
3418 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
3419 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
3420 else
3421 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
3422
3423 for (i = 0; i < len; i++)
3424 {
3425 XSETINT (it->ctl_chars[i * 4], escape_glyph);
3426 /* Insert three more glyphs into IT->ctl_chars for
3427 the octal display of the character. */
3428 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
3429 XSETINT (it->ctl_chars[i * 4 + 1], g);
3430 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
3431 XSETINT (it->ctl_chars[i * 4 + 2], g);
3432 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
3433 XSETINT (it->ctl_chars[i * 4 + 3], g);
3434 }
3435
3436 /* Set up IT->dpvec and return the first character
3437 from it. */
3438 it->dpvec_char_len = it->len;
3439 it->dpvec = it->ctl_chars;
3440 it->dpend = it->dpvec + len * 4;
3441 it->current.dpvec_index = 0;
3442 it->method = next_element_from_display_vector;
3443 get_next_display_element (it);
3444 }
3445 }
3446 }
3447
3448 /* Adjust face id for a multibyte character. There are no
3449 multibyte character in unibyte text. */
3450 if (it->multibyte_p
3451 && success_p
3452 && FRAME_WINDOW_P (it->f))
3453 {
3454 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3455 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
3456 }
3457 }
3458
3459 /* Is this character the last one of a run of characters with
3460 box? If yes, set IT->end_of_box_run_p to 1. */
3461 if (it->face_box_p
3462 && it->s == NULL)
3463 {
3464 int face_id;
3465 struct face *face;
3466
3467 it->end_of_box_run_p
3468 = ((face_id = face_after_it_pos (it),
3469 face_id != it->face_id)
3470 && (face = FACE_FROM_ID (it->f, face_id),
3471 face->box == FACE_NO_BOX));
3472 }
3473
3474 /* Value is 0 if end of buffer or string reached. */
3475 return success_p;
3476 }
3477
3478
3479 /* Move IT to the next display element.
3480
3481 Functions get_next_display_element and set_iterator_to_next are
3482 separate because I find this arrangement easier to handle than a
3483 get_next_display_element function that also increments IT's
3484 position. The way it is we can first look at an iterator's current
3485 display element, decide whether it fits on a line, and if it does,
3486 increment the iterator position. The other way around we probably
3487 would either need a flag indicating whether the iterator has to be
3488 incremented the next time, or we would have to implement a
3489 decrement position function which would not be easy to write. */
3490
3491 void
3492 set_iterator_to_next (it)
3493 struct it *it;
3494 {
3495 if (it->method == next_element_from_buffer)
3496 {
3497 /* The current display element of IT is a character from
3498 current_buffer. Advance in the buffer, and maybe skip over
3499 invisible lines that are so because of selective display. */
3500 if (ITERATOR_AT_END_OF_LINE_P (it))
3501 reseat_at_next_visible_line_start (it, 0);
3502 else
3503 {
3504 xassert (it->len != 0);
3505 IT_BYTEPOS (*it) += it->len;
3506 IT_CHARPOS (*it) += 1;
3507 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
3508 }
3509 }
3510 else if (it->method == next_element_from_composition)
3511 {
3512 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
3513 if (STRINGP (it->string))
3514 {
3515 IT_STRING_BYTEPOS (*it) += it->len;
3516 IT_STRING_CHARPOS (*it) += it->cmp_len;
3517 it->method = next_element_from_string;
3518 goto consider_string_end;
3519 }
3520 else
3521 {
3522 IT_BYTEPOS (*it) += it->len;
3523 IT_CHARPOS (*it) += it->cmp_len;
3524 it->method = next_element_from_buffer;
3525 }
3526 }
3527 else if (it->method == next_element_from_c_string)
3528 {
3529 /* Current display element of IT is from a C string. */
3530 IT_BYTEPOS (*it) += it->len;
3531 IT_CHARPOS (*it) += 1;
3532 }
3533 else if (it->method == next_element_from_display_vector)
3534 {
3535 /* Current display element of IT is from a display table entry.
3536 Advance in the display table definition. Reset it to null if
3537 end reached, and continue with characters from buffers/
3538 strings. */
3539 ++it->current.dpvec_index;
3540
3541 /* Restore face of the iterator to what they were before the
3542 display vector entry (these entries may contain faces). */
3543 it->face_id = it->saved_face_id;
3544
3545 if (it->dpvec + it->current.dpvec_index == it->dpend)
3546 {
3547 if (it->s)
3548 it->method = next_element_from_c_string;
3549 else if (STRINGP (it->string))
3550 it->method = next_element_from_string;
3551 else
3552 it->method = next_element_from_buffer;
3553
3554 it->dpvec = NULL;
3555 it->current.dpvec_index = -1;
3556
3557 /* Skip over characters which were displayed via IT->dpvec. */
3558 if (it->dpvec_char_len < 0)
3559 reseat_at_next_visible_line_start (it, 1);
3560 else if (it->dpvec_char_len > 0)
3561 {
3562 it->len = it->dpvec_char_len;
3563 set_iterator_to_next (it);
3564 }
3565 }
3566 }
3567 else if (it->method == next_element_from_string)
3568 {
3569 /* Current display element is a character from a Lisp string. */
3570 xassert (it->s == NULL && STRINGP (it->string));
3571 IT_STRING_BYTEPOS (*it) += it->len;
3572 IT_STRING_CHARPOS (*it) += 1;
3573
3574 consider_string_end:
3575
3576 if (it->current.overlay_string_index >= 0)
3577 {
3578 /* IT->string is an overlay string. Advance to the
3579 next, if there is one. */
3580 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
3581 next_overlay_string (it);
3582 }
3583 else
3584 {
3585 /* IT->string is not an overlay string. If we reached
3586 its end, and there is something on IT->stack, proceed
3587 with what is on the stack. This can be either another
3588 string, this time an overlay string, or a buffer. */
3589 if (IT_STRING_CHARPOS (*it) == XSTRING (it->string)->size
3590 && it->sp > 0)
3591 {
3592 pop_it (it);
3593 if (!STRINGP (it->string))
3594 it->method = next_element_from_buffer;
3595 }
3596 }
3597 }
3598 else if (it->method == next_element_from_image
3599 || it->method == next_element_from_stretch)
3600 {
3601 /* The position etc with which we have to proceed are on
3602 the stack. The position may be at the end of a string,
3603 if the `display' property takes up the whole string. */
3604 pop_it (it);
3605 it->image_id = 0;
3606 if (STRINGP (it->string))
3607 {
3608 it->method = next_element_from_string;
3609 goto consider_string_end;
3610 }
3611 else
3612 it->method = next_element_from_buffer;
3613 }
3614 else
3615 /* There are no other methods defined, so this should be a bug. */
3616 abort ();
3617
3618 /* Reset flags indicating start and end of a sequence of
3619 characters with box. */
3620 it->start_of_box_run_p = it->end_of_box_run_p = 0;
3621
3622 xassert (it->method != next_element_from_string
3623 || (STRINGP (it->string)
3624 && IT_STRING_CHARPOS (*it) >= 0));
3625 }
3626
3627
3628 /* Load IT's display element fields with information about the next
3629 display element which comes from a display table entry or from the
3630 result of translating a control character to one of the forms `^C'
3631 or `\003'. IT->dpvec holds the glyphs to return as characters. */
3632
3633 static int
3634 next_element_from_display_vector (it)
3635 struct it *it;
3636 {
3637 /* Precondition. */
3638 xassert (it->dpvec && it->current.dpvec_index >= 0);
3639
3640 /* Remember the current face id in case glyphs specify faces.
3641 IT's face is restored in set_iterator_to_next. */
3642 it->saved_face_id = it->face_id;
3643
3644 if (INTEGERP (*it->dpvec)
3645 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
3646 {
3647 int lface_id;
3648 GLYPH g;
3649
3650 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
3651 it->c = FAST_GLYPH_CHAR (g);
3652 it->len = CHAR_LEN (it->c);
3653
3654 /* The entry may contain a face id to use. Such a face id is
3655 the id of a Lisp face, not a realized face. A face id of
3656 zero means no face. */
3657 lface_id = FAST_GLYPH_FACE (g);
3658 if (lface_id)
3659 {
3660 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
3661 if (face_id >= 0)
3662 {
3663 it->face_id = face_id;
3664 }
3665 }
3666 }
3667 else
3668 /* Display table entry is invalid. Return a space. */
3669 it->c = ' ', it->len = 1;
3670
3671 /* Don't change position and object of the iterator here. They are
3672 still the values of the character that had this display table
3673 entry or was translated, and that's what we want. */
3674 it->what = IT_CHARACTER;
3675 return 1;
3676 }
3677
3678
3679 /* Load IT with the next display element from Lisp string IT->string.
3680 IT->current.string_pos is the current position within the string.
3681 If IT->current.overlay_string_index >= 0, the Lisp string is an
3682 overlay string. */
3683
3684 static int
3685 next_element_from_string (it)
3686 struct it *it;
3687 {
3688 struct text_pos position;
3689
3690 xassert (STRINGP (it->string));
3691 xassert (IT_STRING_CHARPOS (*it) >= 0);
3692 position = it->current.string_pos;
3693
3694 /* Time to check for invisible text? */
3695 if (IT_STRING_CHARPOS (*it) < it->end_charpos
3696 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
3697 {
3698 handle_stop (it);
3699
3700 /* Since a handler may have changed IT->method, we must
3701 recurse here. */
3702 return get_next_display_element (it);
3703 }
3704
3705 if (it->current.overlay_string_index >= 0)
3706 {
3707 /* Get the next character from an overlay string. In overlay
3708 strings, There is no field width or padding with spaces to
3709 do. */
3710 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
3711 {
3712 it->what = IT_EOB;
3713 return 0;
3714 }
3715 else if (STRING_MULTIBYTE (it->string))
3716 {
3717 int remaining = (STRING_BYTES (XSTRING (it->string))
3718 - IT_STRING_BYTEPOS (*it));
3719 unsigned char *s = (XSTRING (it->string)->data
3720 + IT_STRING_BYTEPOS (*it));
3721 it->c = string_char_and_length (s, remaining, &it->len);
3722 }
3723 else
3724 {
3725 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
3726 it->len = 1;
3727 }
3728 }
3729 else
3730 {
3731 /* Get the next character from a Lisp string that is not an
3732 overlay string. Such strings come from the mode line, for
3733 example. We may have to pad with spaces, or truncate the
3734 string. See also next_element_from_c_string. */
3735 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
3736 {
3737 it->what = IT_EOB;
3738 return 0;
3739 }
3740 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
3741 {
3742 /* Pad with spaces. */
3743 it->c = ' ', it->len = 1;
3744 CHARPOS (position) = BYTEPOS (position) = -1;
3745 }
3746 else if (STRING_MULTIBYTE (it->string))
3747 {
3748 int maxlen = (STRING_BYTES (XSTRING (it->string))
3749 - IT_STRING_BYTEPOS (*it));
3750 unsigned char *s = (XSTRING (it->string)->data
3751 + IT_STRING_BYTEPOS (*it));
3752 it->c = string_char_and_length (s, maxlen, &it->len);
3753 }
3754 else
3755 {
3756 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
3757 it->len = 1;
3758 }
3759 }
3760
3761 /* Record what we have and where it came from. Note that we store a
3762 buffer position in IT->position although it could arguably be a
3763 string position. */
3764 it->what = IT_CHARACTER;
3765 it->object = it->string;
3766 it->position = position;
3767 return 1;
3768 }
3769
3770
3771 /* Load IT with next display element from C string IT->s.
3772 IT->string_nchars is the maximum number of characters to return
3773 from the string. IT->end_charpos may be greater than
3774 IT->string_nchars when this function is called, in which case we
3775 may have to return padding spaces. Value is zero if end of string
3776 reached, including padding spaces. */
3777
3778 static int
3779 next_element_from_c_string (it)
3780 struct it *it;
3781 {
3782 int success_p = 1;
3783
3784 xassert (it->s);
3785 it->what = IT_CHARACTER;
3786 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
3787 it->object = Qnil;
3788
3789 /* IT's position can be greater IT->string_nchars in case a field
3790 width or precision has been specified when the iterator was
3791 initialized. */
3792 if (IT_CHARPOS (*it) >= it->end_charpos)
3793 {
3794 /* End of the game. */
3795 it->what = IT_EOB;
3796 success_p = 0;
3797 }
3798 else if (IT_CHARPOS (*it) >= it->string_nchars)
3799 {
3800 /* Pad with spaces. */
3801 it->c = ' ', it->len = 1;
3802 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
3803 }
3804 else if (it->multibyte_p)
3805 {
3806 /* Implementation note: The calls to strlen apparently aren't a
3807 performance problem because there is no noticeable performance
3808 difference between Emacs running in unibyte or multibyte mode. */
3809 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
3810 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
3811 maxlen, &it->len);
3812 }
3813 else
3814 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
3815
3816 return success_p;
3817 }
3818
3819
3820 /* Set up IT to return characters from an ellipsis, if appropriate.
3821 The definition of the ellipsis glyphs may come from a display table
3822 entry. This function Fills IT with the first glyph from the
3823 ellipsis if an ellipsis is to be displayed. */
3824
3825 static int
3826 next_element_from_ellipsis (it)
3827 struct it *it;
3828 {
3829 if (it->selective_display_ellipsis_p)
3830 {
3831 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3832 {
3833 /* Use the display table definition for `...'. Invalid glyphs
3834 will be handled by the method returning elements from dpvec. */
3835 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3836 it->dpvec_char_len = it->len;
3837 it->dpvec = v->contents;
3838 it->dpend = v->contents + v->size;
3839 it->current.dpvec_index = 0;
3840 it->method = next_element_from_display_vector;
3841 }
3842 else
3843 {
3844 /* Use default `...' which is stored in default_invis_vector. */
3845 it->dpvec_char_len = it->len;
3846 it->dpvec = default_invis_vector;
3847 it->dpend = default_invis_vector + 3;
3848 it->current.dpvec_index = 0;
3849 it->method = next_element_from_display_vector;
3850 }
3851 }
3852 else
3853 reseat_at_next_visible_line_start (it, 1);
3854
3855 return get_next_display_element (it);
3856 }
3857
3858
3859 /* Deliver an image display element. The iterator IT is already
3860 filled with image information (done in handle_display_prop). Value
3861 is always 1. */
3862
3863
3864 static int
3865 next_element_from_image (it)
3866 struct it *it;
3867 {
3868 it->what = IT_IMAGE;
3869 return 1;
3870 }
3871
3872
3873 /* Fill iterator IT with next display element from a stretch glyph
3874 property. IT->object is the value of the text property. Value is
3875 always 1. */
3876
3877 static int
3878 next_element_from_stretch (it)
3879 struct it *it;
3880 {
3881 it->what = IT_STRETCH;
3882 return 1;
3883 }
3884
3885
3886 /* Load IT with the next display element from current_buffer. Value
3887 is zero if end of buffer reached. IT->stop_charpos is the next
3888 position at which to stop and check for text properties or buffer
3889 end. */
3890
3891 static int
3892 next_element_from_buffer (it)
3893 struct it *it;
3894 {
3895 int success_p = 1;
3896
3897 /* Check this assumption, otherwise, we would never enter the
3898 if-statement, below. */
3899 xassert (IT_CHARPOS (*it) >= BEGV
3900 && IT_CHARPOS (*it) <= it->stop_charpos);
3901
3902 if (IT_CHARPOS (*it) >= it->stop_charpos)
3903 {
3904 if (IT_CHARPOS (*it) >= it->end_charpos)
3905 {
3906 int overlay_strings_follow_p;
3907
3908 /* End of the game, except when overlay strings follow that
3909 haven't been returned yet. */
3910 if (it->overlay_strings_at_end_processed_p)
3911 overlay_strings_follow_p = 0;
3912 else
3913 {
3914 it->overlay_strings_at_end_processed_p = 1;
3915 overlay_strings_follow_p = get_overlay_strings (it);
3916 }
3917
3918 if (overlay_strings_follow_p)
3919 success_p = get_next_display_element (it);
3920 else
3921 {
3922 it->what = IT_EOB;
3923 it->position = it->current.pos;
3924 success_p = 0;
3925 }
3926 }
3927 else
3928 {
3929 handle_stop (it);
3930 return get_next_display_element (it);
3931 }
3932 }
3933 else
3934 {
3935 /* No face changes, overlays etc. in sight, so just return a
3936 character from current_buffer. */
3937 unsigned char *p;
3938
3939 /* Maybe run the redisplay end trigger hook. Performance note:
3940 This doesn't seem to cost measurable time. */
3941 if (it->redisplay_end_trigger_charpos
3942 && it->glyph_row
3943 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
3944 run_redisplay_end_trigger_hook (it);
3945
3946 /* Get the next character, maybe multibyte. */
3947 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
3948 if (it->multibyte_p && !ASCII_BYTE_P (*p))
3949 {
3950 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
3951 - IT_BYTEPOS (*it));
3952 it->c = string_char_and_length (p, maxlen, &it->len);
3953 }
3954 else
3955 it->c = *p, it->len = 1;
3956
3957 /* Record what we have and where it came from. */
3958 it->what = IT_CHARACTER;;
3959 it->object = it->w->buffer;
3960 it->position = it->current.pos;
3961
3962 /* Normally we return the character found above, except when we
3963 really want to return an ellipsis for selective display. */
3964 if (it->selective)
3965 {
3966 if (it->c == '\n')
3967 {
3968 /* A value of selective > 0 means hide lines indented more
3969 than that number of columns. */
3970 if (it->selective > 0
3971 && IT_CHARPOS (*it) + 1 < ZV
3972 && indented_beyond_p (IT_CHARPOS (*it) + 1,
3973 IT_BYTEPOS (*it) + 1,
3974 it->selective))
3975 {
3976 success_p = next_element_from_ellipsis (it);
3977 it->dpvec_char_len = -1;
3978 }
3979 }
3980 else if (it->c == '\r' && it->selective == -1)
3981 {
3982 /* A value of selective == -1 means that everything from the
3983 CR to the end of the line is invisible, with maybe an
3984 ellipsis displayed for it. */
3985 success_p = next_element_from_ellipsis (it);
3986 it->dpvec_char_len = -1;
3987 }
3988 }
3989 }
3990
3991 /* Value is zero if end of buffer reached. */
3992 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
3993 return success_p;
3994 }
3995
3996
3997 /* Run the redisplay end trigger hook for IT. */
3998
3999 static void
4000 run_redisplay_end_trigger_hook (it)
4001 struct it *it;
4002 {
4003 Lisp_Object args[3];
4004
4005 /* IT->glyph_row should be non-null, i.e. we should be actually
4006 displaying something, or otherwise we should not run the hook. */
4007 xassert (it->glyph_row);
4008
4009 /* Set up hook arguments. */
4010 args[0] = Qredisplay_end_trigger_functions;
4011 args[1] = it->window;
4012 XSETINT (args[2], it->redisplay_end_trigger_charpos);
4013 it->redisplay_end_trigger_charpos = 0;
4014
4015 /* Since we are *trying* to run these functions, don't try to run
4016 them again, even if they get an error. */
4017 it->w->redisplay_end_trigger = Qnil;
4018 Frun_hook_with_args (3, args);
4019
4020 /* Notice if it changed the face of the character we are on. */
4021 handle_face_prop (it);
4022 }
4023
4024
4025 /* Deliver a composition display element. The iterator IT is already
4026 filled with composition information (done in
4027 handle_composition_prop). Value is always 1. */
4028
4029 static int
4030 next_element_from_composition (it)
4031 struct it *it;
4032 {
4033 it->what = IT_COMPOSITION;
4034 it->position = (STRINGP (it->string)
4035 ? it->current.string_pos
4036 : it->current.pos);
4037 return 1;
4038 }
4039
4040
4041 \f
4042 /***********************************************************************
4043 Moving an iterator without producing glyphs
4044 ***********************************************************************/
4045
4046 /* Move iterator IT to a specified buffer or X position within one
4047 line on the display without producing glyphs.
4048
4049 Begin to skip at IT's current position. Skip to TO_CHARPOS or TO_X
4050 whichever is reached first.
4051
4052 TO_CHARPOS <= 0 means no TO_CHARPOS is specified.
4053
4054 TO_X < 0 means that no TO_X is specified. TO_X is normally a value
4055 0 <= TO_X <= IT->last_visible_x. This means in particular, that
4056 TO_X includes the amount by which a window is horizontally
4057 scrolled.
4058
4059 Value is
4060
4061 MOVE_POS_MATCH_OR_ZV
4062 - when TO_POS or ZV was reached.
4063
4064 MOVE_X_REACHED
4065 -when TO_X was reached before TO_POS or ZV were reached.
4066
4067 MOVE_LINE_CONTINUED
4068 - when we reached the end of the display area and the line must
4069 be continued.
4070
4071 MOVE_LINE_TRUNCATED
4072 - when we reached the end of the display area and the line is
4073 truncated.
4074
4075 MOVE_NEWLINE_OR_CR
4076 - when we stopped at a line end, i.e. a newline or a CR and selective
4077 display is on. */
4078
4079 static enum move_it_result
4080 move_it_in_display_line_to (it, to_charpos, to_x, op)
4081 struct it *it;
4082 int to_charpos, to_x, op;
4083 {
4084 enum move_it_result result = MOVE_UNDEFINED;
4085 struct glyph_row *saved_glyph_row;
4086
4087 /* Don't produce glyphs in produce_glyphs. */
4088 saved_glyph_row = it->glyph_row;
4089 it->glyph_row = NULL;
4090
4091 while (1)
4092 {
4093 int x, i;
4094
4095 /* Stop when ZV or TO_CHARPOS reached. */
4096 if (!get_next_display_element (it)
4097 || ((op & MOVE_TO_POS) != 0
4098 && BUFFERP (it->object)
4099 && IT_CHARPOS (*it) >= to_charpos))
4100 {
4101 result = MOVE_POS_MATCH_OR_ZV;
4102 break;
4103 }
4104
4105 /* The call to produce_glyphs will get the metrics of the
4106 display element IT is loaded with. We record in x the
4107 x-position before this display element in case it does not
4108 fit on the line. */
4109 x = it->current_x;
4110 PRODUCE_GLYPHS (it);
4111
4112 if (it->area != TEXT_AREA)
4113 {
4114 set_iterator_to_next (it);
4115 continue;
4116 }
4117
4118 /* The number of glyphs we get back in IT->nglyphs will normally
4119 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
4120 character on a terminal frame, or (iii) a line end. For the
4121 second case, IT->nglyphs - 1 padding glyphs will be present
4122 (on X frames, there is only one glyph produced for a
4123 composite character.
4124
4125 The behavior implemented below means, for continuation lines,
4126 that as many spaces of a TAB as fit on the current line are
4127 displayed there. For terminal frames, as many glyphs of a
4128 multi-glyph character are displayed in the current line, too.
4129 This is what the old redisplay code did, and we keep it that
4130 way. Under X, the whole shape of a complex character must
4131 fit on the line or it will be completely displayed in the
4132 next line.
4133
4134 Note that both for tabs and padding glyphs, all glyphs have
4135 the same width. */
4136 if (it->nglyphs)
4137 {
4138 /* More than one glyph or glyph doesn't fit on line. All
4139 glyphs have the same width. */
4140 int single_glyph_width = it->pixel_width / it->nglyphs;
4141 int new_x;
4142
4143 for (i = 0; i < it->nglyphs; ++i, x = new_x)
4144 {
4145 new_x = x + single_glyph_width;
4146
4147 /* We want to leave anything reaching TO_X to the caller. */
4148 if ((op & MOVE_TO_X) && new_x > to_x)
4149 {
4150 it->current_x = x;
4151 result = MOVE_X_REACHED;
4152 break;
4153 }
4154 else if (/* Lines are continued. */
4155 !it->truncate_lines_p
4156 && (/* And glyph doesn't fit on the line. */
4157 new_x > it->last_visible_x
4158 /* Or it fits exactly and we're on a window
4159 system frame. */
4160 || (new_x == it->last_visible_x
4161 && FRAME_WINDOW_P (it->f))))
4162 {
4163 if (/* IT->hpos == 0 means the very first glyph
4164 doesn't fit on the line, e.g. a wide image. */
4165 it->hpos == 0
4166 || (new_x == it->last_visible_x
4167 && FRAME_WINDOW_P (it->f)))
4168 {
4169 ++it->hpos;
4170 it->current_x = new_x;
4171 if (i == it->nglyphs - 1)
4172 set_iterator_to_next (it);
4173 }
4174 else
4175 it->current_x = x;
4176
4177 result = MOVE_LINE_CONTINUED;
4178 break;
4179 }
4180 else if (new_x > it->first_visible_x)
4181 {
4182 /* Glyph is visible. Increment number of glyphs that
4183 would be displayed. */
4184 ++it->hpos;
4185 }
4186 else
4187 {
4188 /* Glyph is completely off the left margin of the display
4189 area. Nothing to do. */
4190 }
4191 }
4192
4193 if (result != MOVE_UNDEFINED)
4194 break;
4195 }
4196 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
4197 {
4198 /* Stop when TO_X specified and reached. This check is
4199 necessary here because of lines consisting of a line end,
4200 only. The line end will not produce any glyphs and we
4201 would never get MOVE_X_REACHED. */
4202 xassert (it->nglyphs == 0);
4203 result = MOVE_X_REACHED;
4204 break;
4205 }
4206
4207 /* Is this a line end? If yes, we're done. */
4208 if (ITERATOR_AT_END_OF_LINE_P (it))
4209 {
4210 result = MOVE_NEWLINE_OR_CR;
4211 break;
4212 }
4213
4214 /* The current display element has been consumed. Advance
4215 to the next. */
4216 set_iterator_to_next (it);
4217
4218 /* Stop if lines are truncated and IT's current x-position is
4219 past the right edge of the window now. */
4220 if (it->truncate_lines_p
4221 && it->current_x >= it->last_visible_x)
4222 {
4223 result = MOVE_LINE_TRUNCATED;
4224 break;
4225 }
4226 }
4227
4228 /* Restore the iterator settings altered at the beginning of this
4229 function. */
4230 it->glyph_row = saved_glyph_row;
4231 return result;
4232 }
4233
4234
4235 /* Move IT forward to a specified buffer position TO_CHARPOS, TO_X,
4236 TO_Y, TO_VPOS. OP is a bit-mask that specifies where to stop. See
4237 the description of enum move_operation_enum.
4238
4239 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
4240 screen line, this function will set IT to the next position >
4241 TO_CHARPOS. */
4242
4243 void
4244 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
4245 struct it *it;
4246 int to_charpos, to_x, to_y, to_vpos;
4247 int op;
4248 {
4249 enum move_it_result skip, skip2 = MOVE_X_REACHED;
4250 int line_height;
4251
4252 while (1)
4253 {
4254 if (op & MOVE_TO_VPOS)
4255 {
4256 /* If no TO_CHARPOS and no TO_X specified, stop at the
4257 start of the line TO_VPOS. */
4258 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
4259 {
4260 if (it->vpos == to_vpos)
4261 break;
4262 skip = move_it_in_display_line_to (it, -1, -1, 0);
4263 }
4264 else
4265 {
4266 /* TO_VPOS >= 0 means stop at TO_X in the line at
4267 TO_VPOS, or at TO_POS, whichever comes first. */
4268 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
4269
4270 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
4271 break;
4272 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
4273 {
4274 /* We have reached TO_X but not in the line we want. */
4275 skip = move_it_in_display_line_to (it, to_charpos,
4276 -1, MOVE_TO_POS);
4277 if (skip == MOVE_POS_MATCH_OR_ZV)
4278 break;
4279 }
4280 }
4281 }
4282 else if (op & MOVE_TO_Y)
4283 {
4284 struct it it_backup;
4285 int done_p;
4286
4287 /* TO_Y specified means stop at TO_X in the line containing
4288 TO_Y---or at TO_CHARPOS if this is reached first. The
4289 problem is that we can't really tell whether the line
4290 contains TO_Y before we have completely scanned it, and
4291 this may skip past TO_X. What we do is to first scan to
4292 TO_X.
4293
4294 If TO_X is not specified, use a TO_X of zero. The reason
4295 is to make the outcome of this function more predictable.
4296 If we didn't use TO_X == 0, we would stop at the end of
4297 the line which is probably not what a caller would expect
4298 to happen. */
4299 skip = move_it_in_display_line_to (it, to_charpos,
4300 ((op & MOVE_TO_X)
4301 ? to_x : 0),
4302 (MOVE_TO_X
4303 | (op & MOVE_TO_POS)));
4304
4305 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
4306 if (skip == MOVE_POS_MATCH_OR_ZV)
4307 break;
4308
4309 /* If TO_X was reached, we would like to know whether TO_Y
4310 is in the line. This can only be said if we know the
4311 total line height which requires us to scan the rest of
4312 the line. */
4313 done_p = 0;
4314 if (skip == MOVE_X_REACHED)
4315 {
4316 it_backup = *it;
4317 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
4318 op & MOVE_TO_POS);
4319 }
4320
4321 /* Now, decide whether TO_Y is in this line. */
4322 line_height = it->max_ascent + it->max_descent;
4323
4324 if (to_y >= it->current_y
4325 && to_y < it->current_y + line_height)
4326 {
4327 if (skip == MOVE_X_REACHED)
4328 /* If TO_Y is in this line and TO_X was reached above,
4329 we scanned too far. We have to restore IT's settings
4330 to the ones before skipping. */
4331 *it = it_backup;
4332 done_p = 1;
4333 }
4334 else if (skip == MOVE_X_REACHED)
4335 {
4336 skip = skip2;
4337 if (skip == MOVE_POS_MATCH_OR_ZV)
4338 done_p = 1;
4339 }
4340
4341 if (done_p)
4342 break;
4343 }
4344 else
4345 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
4346
4347 switch (skip)
4348 {
4349 case MOVE_POS_MATCH_OR_ZV:
4350 return;
4351
4352 case MOVE_NEWLINE_OR_CR:
4353 set_iterator_to_next (it);
4354 it->continuation_lines_width = 0;
4355 break;
4356
4357 case MOVE_LINE_TRUNCATED:
4358 it->continuation_lines_width = 0;
4359 reseat_at_next_visible_line_start (it, 0);
4360 if ((op & MOVE_TO_POS) != 0
4361 && IT_CHARPOS (*it) > to_charpos)
4362 goto out;
4363 break;
4364
4365 case MOVE_LINE_CONTINUED:
4366 it->continuation_lines_width += it->current_x;
4367 break;
4368
4369 default:
4370 abort ();
4371 }
4372
4373 /* Reset/increment for the next run. */
4374 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
4375 it->current_x = it->hpos = 0;
4376 it->current_y += it->max_ascent + it->max_descent;
4377 ++it->vpos;
4378 last_height = it->max_ascent + it->max_descent;
4379 last_max_ascent = it->max_ascent;
4380 it->max_ascent = it->max_descent = 0;
4381 }
4382 out:;
4383 }
4384
4385
4386 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
4387
4388 If DY > 0, move IT backward at least that many pixels. DY = 0
4389 means move IT backward to the preceding line start or BEGV. This
4390 function may move over more than DY pixels if IT->current_y - DY
4391 ends up in the middle of a line; in this case IT->current_y will be
4392 set to the top of the line moved to. */
4393
4394 void
4395 move_it_vertically_backward (it, dy)
4396 struct it *it;
4397 int dy;
4398 {
4399 int nlines, h, line_height;
4400 struct it it2;
4401 int start_pos = IT_CHARPOS (*it);
4402
4403 xassert (dy >= 0);
4404
4405 /* Estimate how many newlines we must move back. */
4406 nlines = max (1, dy / CANON_Y_UNIT (it->f));
4407
4408 /* Set the iterator's position that many lines back. */
4409 while (nlines-- && IT_CHARPOS (*it) > BEGV)
4410 back_to_previous_visible_line_start (it);
4411
4412 /* Reseat the iterator here. When moving backward, we don't want
4413 reseat to skip forward over invisible text, set up the iterator
4414 to deliver from overlay strings at the new position etc. So,
4415 use reseat_1 here. */
4416 reseat_1 (it, it->current.pos, 1);
4417
4418 /* We are now surely at a line start. */
4419 it->current_x = it->hpos = 0;
4420
4421 /* Move forward and see what y-distance we moved. First move to the
4422 start of the next line so that we get its height. We need this
4423 height to be able to tell whether we reached the specified
4424 y-distance. */
4425 it2 = *it;
4426 it2.max_ascent = it2.max_descent = 0;
4427 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
4428 MOVE_TO_POS | MOVE_TO_VPOS);
4429 xassert (IT_CHARPOS (*it) >= BEGV);
4430 line_height = it2.max_ascent + it2.max_descent;
4431 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
4432 xassert (IT_CHARPOS (*it) >= BEGV);
4433 h = it2.current_y - it->current_y;
4434 nlines = it2.vpos - it->vpos;
4435
4436 /* Correct IT's y and vpos position. */
4437 it->vpos -= nlines;
4438 it->current_y -= h;
4439
4440 if (dy == 0)
4441 {
4442 /* DY == 0 means move to the start of the screen line. The
4443 value of nlines is > 0 if continuation lines were involved. */
4444 if (nlines > 0)
4445 move_it_by_lines (it, nlines, 1);
4446 xassert (IT_CHARPOS (*it) <= start_pos);
4447 }
4448 else if (nlines)
4449 {
4450 /* The y-position we try to reach. Note that h has been
4451 subtracted in front of the if-statement. */
4452 int target_y = it->current_y + h - dy;
4453
4454 /* If we did not reach target_y, try to move further backward if
4455 we can. If we moved too far backward, try to move forward. */
4456 if (target_y < it->current_y
4457 && IT_CHARPOS (*it) > BEGV)
4458 {
4459 move_it_vertically (it, target_y - it->current_y);
4460 xassert (IT_CHARPOS (*it) >= BEGV);
4461 }
4462 else if (target_y >= it->current_y + line_height
4463 && IT_CHARPOS (*it) < ZV)
4464 {
4465 move_it_vertically (it, target_y - (it->current_y + line_height));
4466 xassert (IT_CHARPOS (*it) >= BEGV);
4467 }
4468 }
4469 }
4470
4471
4472 /* Move IT by a specified amount of pixel lines DY. DY negative means
4473 move backwards. DY = 0 means move to start of screen line. At the
4474 end, IT will be on the start of a screen line. */
4475
4476 void
4477 move_it_vertically (it, dy)
4478 struct it *it;
4479 int dy;
4480 {
4481 if (dy <= 0)
4482 move_it_vertically_backward (it, -dy);
4483 else if (dy > 0)
4484 {
4485 move_it_to (it, ZV, -1, it->current_y + dy, -1,
4486 MOVE_TO_POS | MOVE_TO_Y);
4487
4488 /* If buffer ends in ZV without a newline, move to the start of
4489 the line to satisfy the post-condition. */
4490 if (IT_CHARPOS (*it) == ZV
4491 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
4492 move_it_by_lines (it, 0, 0);
4493 }
4494 }
4495
4496
4497 /* Return non-zero if some text between buffer positions START_CHARPOS
4498 and END_CHARPOS is invisible. IT->window is the window for text
4499 property lookup. */
4500
4501 static int
4502 invisible_text_between_p (it, start_charpos, end_charpos)
4503 struct it *it;
4504 int start_charpos, end_charpos;
4505 {
4506 Lisp_Object prop, limit;
4507 int invisible_found_p;
4508
4509 xassert (it != NULL && start_charpos <= end_charpos);
4510
4511 /* Is text at START invisible? */
4512 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
4513 it->window);
4514 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4515 invisible_found_p = 1;
4516 else
4517 {
4518 limit = next_single_char_property_change (make_number (start_charpos),
4519 Qinvisible, Qnil,
4520 make_number (end_charpos));
4521 invisible_found_p = XFASTINT (limit) < end_charpos;
4522 }
4523
4524 return invisible_found_p;
4525 }
4526
4527
4528 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
4529 negative means move up. DVPOS == 0 means move to the start of the
4530 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
4531 NEED_Y_P is zero, IT->current_y will be left unchanged.
4532
4533 Further optimization ideas: If we would know that IT->f doesn't use
4534 a face with proportional font, we could be faster for
4535 truncate-lines nil. */
4536
4537 void
4538 move_it_by_lines (it, dvpos, need_y_p)
4539 struct it *it;
4540 int dvpos, need_y_p;
4541 {
4542 struct position pos;
4543
4544 if (!FRAME_WINDOW_P (it->f))
4545 {
4546 struct text_pos textpos;
4547
4548 /* We can use vmotion on frames without proportional fonts. */
4549 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
4550 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
4551 reseat (it, textpos, 1);
4552 it->vpos += pos.vpos;
4553 it->current_y += pos.vpos;
4554 }
4555 else if (dvpos == 0)
4556 {
4557 /* DVPOS == 0 means move to the start of the screen line. */
4558 move_it_vertically_backward (it, 0);
4559 xassert (it->current_x == 0 && it->hpos == 0);
4560 }
4561 else if (dvpos > 0)
4562 {
4563 /* If there are no continuation lines, and if there is no
4564 selective display, try the simple method of moving forward
4565 DVPOS newlines, then see where we are. */
4566 if (!need_y_p && it->truncate_lines_p && it->selective == 0)
4567 {
4568 int shortage = 0, charpos;
4569
4570 if (FETCH_BYTE (IT_BYTEPOS (*it) == '\n'))
4571 charpos = IT_CHARPOS (*it) + 1;
4572 else
4573 charpos = scan_buffer ('\n', IT_CHARPOS (*it), 0, dvpos,
4574 &shortage, 0);
4575
4576 if (!invisible_text_between_p (it, IT_CHARPOS (*it), charpos))
4577 {
4578 struct text_pos pos;
4579 CHARPOS (pos) = charpos;
4580 BYTEPOS (pos) = CHAR_TO_BYTE (charpos);
4581 reseat (it, pos, 1);
4582 it->vpos += dvpos - shortage;
4583 it->hpos = it->current_x = 0;
4584 return;
4585 }
4586 }
4587
4588 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
4589 }
4590 else
4591 {
4592 struct it it2;
4593 int start_charpos, i;
4594
4595 /* If there are no continuation lines, and if there is no
4596 selective display, try the simple method of moving backward
4597 -DVPOS newlines. */
4598 if (!need_y_p && it->truncate_lines_p && it->selective == 0)
4599 {
4600 int shortage;
4601 int charpos = IT_CHARPOS (*it);
4602 int bytepos = IT_BYTEPOS (*it);
4603
4604 /* If in the middle of a line, go to its start. */
4605 if (charpos > BEGV && FETCH_BYTE (bytepos - 1) != '\n')
4606 {
4607 charpos = find_next_newline_no_quit (charpos, -1);
4608 bytepos = CHAR_TO_BYTE (charpos);
4609 }
4610
4611 if (charpos == BEGV)
4612 {
4613 struct text_pos pos;
4614 CHARPOS (pos) = charpos;
4615 BYTEPOS (pos) = bytepos;
4616 reseat (it, pos, 1);
4617 it->hpos = it->current_x = 0;
4618 return;
4619 }
4620 else
4621 {
4622 charpos = scan_buffer ('\n', charpos - 1, 0, dvpos, &shortage, 0);
4623 if (!invisible_text_between_p (it, charpos, IT_CHARPOS (*it)))
4624 {
4625 struct text_pos pos;
4626 CHARPOS (pos) = charpos;
4627 BYTEPOS (pos) = CHAR_TO_BYTE (charpos);
4628 reseat (it, pos, 1);
4629 it->vpos += dvpos + (shortage ? shortage - 1 : 0);
4630 it->hpos = it->current_x = 0;
4631 return;
4632 }
4633 }
4634 }
4635
4636 /* Go back -DVPOS visible lines and reseat the iterator there. */
4637 start_charpos = IT_CHARPOS (*it);
4638 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
4639 back_to_previous_visible_line_start (it);
4640 reseat (it, it->current.pos, 1);
4641 it->current_x = it->hpos = 0;
4642
4643 /* Above call may have moved too far if continuation lines
4644 are involved. Scan forward and see if it did. */
4645 it2 = *it;
4646 it2.vpos = it2.current_y = 0;
4647 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
4648 it->vpos -= it2.vpos;
4649 it->current_y -= it2.current_y;
4650 it->current_x = it->hpos = 0;
4651
4652 /* If we moved too far, move IT some lines forward. */
4653 if (it2.vpos > -dvpos)
4654 {
4655 int delta = it2.vpos + dvpos;
4656 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
4657 }
4658 }
4659 }
4660
4661
4662 \f
4663 /***********************************************************************
4664 Messages
4665 ***********************************************************************/
4666
4667
4668 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
4669 to *Messages*. */
4670
4671 void
4672 add_to_log (format, arg1, arg2)
4673 char *format;
4674 Lisp_Object arg1, arg2;
4675 {
4676 Lisp_Object args[3];
4677 Lisp_Object msg, fmt;
4678 char *buffer;
4679 int len;
4680 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4681
4682 fmt = msg = Qnil;
4683 GCPRO4 (fmt, msg, arg1, arg2);
4684
4685 args[0] = fmt = build_string (format);
4686 args[1] = arg1;
4687 args[2] = arg2;
4688 msg = Fformat (3, args);
4689
4690 len = STRING_BYTES (XSTRING (msg)) + 1;
4691 buffer = (char *) alloca (len);
4692 strcpy (buffer, XSTRING (msg)->data);
4693
4694 message_dolog (buffer, len, 1, 0);
4695 UNGCPRO;
4696 }
4697
4698
4699 /* Output a newline in the *Messages* buffer if "needs" one. */
4700
4701 void
4702 message_log_maybe_newline ()
4703 {
4704 if (message_log_need_newline)
4705 message_dolog ("", 0, 1, 0);
4706 }
4707
4708
4709 /* Add a string M of length LEN to the message log, optionally
4710 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
4711 nonzero, means interpret the contents of M as multibyte. This
4712 function calls low-level routines in order to bypass text property
4713 hooks, etc. which might not be safe to run. */
4714
4715 void
4716 message_dolog (m, len, nlflag, multibyte)
4717 char *m;
4718 int len, nlflag, multibyte;
4719 {
4720 if (!NILP (Vmessage_log_max))
4721 {
4722 struct buffer *oldbuf;
4723 Lisp_Object oldpoint, oldbegv, oldzv;
4724 int old_windows_or_buffers_changed = windows_or_buffers_changed;
4725 int point_at_end = 0;
4726 int zv_at_end = 0;
4727 Lisp_Object old_deactivate_mark, tem;
4728 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4729
4730 old_deactivate_mark = Vdeactivate_mark;
4731 oldbuf = current_buffer;
4732 Fset_buffer (Fget_buffer_create (build_string ("*Messages*")));
4733 current_buffer->undo_list = Qt;
4734
4735 oldpoint = Fpoint_marker ();
4736 oldbegv = Fpoint_min_marker ();
4737 oldzv = Fpoint_max_marker ();
4738 GCPRO4 (oldpoint, oldbegv, oldzv, old_deactivate_mark);
4739
4740 if (PT == Z)
4741 point_at_end = 1;
4742 if (ZV == Z)
4743 zv_at_end = 1;
4744
4745 BEGV = BEG;
4746 BEGV_BYTE = BEG_BYTE;
4747 ZV = Z;
4748 ZV_BYTE = Z_BYTE;
4749 TEMP_SET_PT_BOTH (Z, Z_BYTE);
4750
4751 /* Insert the string--maybe converting multibyte to single byte
4752 or vice versa, so that all the text fits the buffer. */
4753 if (multibyte
4754 && NILP (current_buffer->enable_multibyte_characters))
4755 {
4756 int i, c, nbytes;
4757 unsigned char work[1];
4758
4759 /* Convert a multibyte string to single-byte
4760 for the *Message* buffer. */
4761 for (i = 0; i < len; i += nbytes)
4762 {
4763 c = string_char_and_length (m + i, len - i, &nbytes);
4764 work[0] = (SINGLE_BYTE_CHAR_P (c)
4765 ? c
4766 : multibyte_char_to_unibyte (c, Qnil));
4767 insert_1_both (work, 1, 1, 1, 0, 0);
4768 }
4769 }
4770 else if (! multibyte
4771 && ! NILP (current_buffer->enable_multibyte_characters))
4772 {
4773 int i, c, nbytes;
4774 unsigned char *msg = (unsigned char *) m;
4775 unsigned char str[MAX_MULTIBYTE_LENGTH];
4776 /* Convert a single-byte string to multibyte
4777 for the *Message* buffer. */
4778 for (i = 0; i < len; i++)
4779 {
4780 c = unibyte_char_to_multibyte (msg[i]);
4781 nbytes = CHAR_STRING (c, str);
4782 insert_1_both (str, 1, nbytes, 1, 0, 0);
4783 }
4784 }
4785 else if (len)
4786 insert_1 (m, len, 1, 0, 0);
4787
4788 if (nlflag)
4789 {
4790 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
4791 insert_1 ("\n", 1, 1, 0, 0);
4792
4793 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
4794 this_bol = PT;
4795 this_bol_byte = PT_BYTE;
4796
4797 if (this_bol > BEG)
4798 {
4799 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
4800 prev_bol = PT;
4801 prev_bol_byte = PT_BYTE;
4802
4803 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
4804 this_bol, this_bol_byte);
4805 if (dup)
4806 {
4807 del_range_both (prev_bol, prev_bol_byte,
4808 this_bol, this_bol_byte, 0);
4809 if (dup > 1)
4810 {
4811 char dupstr[40];
4812 int duplen;
4813
4814 /* If you change this format, don't forget to also
4815 change message_log_check_duplicate. */
4816 sprintf (dupstr, " [%d times]", dup);
4817 duplen = strlen (dupstr);
4818 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
4819 insert_1 (dupstr, duplen, 1, 0, 1);
4820 }
4821 }
4822 }
4823
4824 if (NATNUMP (Vmessage_log_max))
4825 {
4826 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
4827 -XFASTINT (Vmessage_log_max) - 1, 0);
4828 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
4829 }
4830 }
4831 BEGV = XMARKER (oldbegv)->charpos;
4832 BEGV_BYTE = marker_byte_position (oldbegv);
4833
4834 if (zv_at_end)
4835 {
4836 ZV = Z;
4837 ZV_BYTE = Z_BYTE;
4838 }
4839 else
4840 {
4841 ZV = XMARKER (oldzv)->charpos;
4842 ZV_BYTE = marker_byte_position (oldzv);
4843 }
4844
4845 if (point_at_end)
4846 TEMP_SET_PT_BOTH (Z, Z_BYTE);
4847 else
4848 /* We can't do Fgoto_char (oldpoint) because it will run some
4849 Lisp code. */
4850 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
4851 XMARKER (oldpoint)->bytepos);
4852
4853 UNGCPRO;
4854 free_marker (oldpoint);
4855 free_marker (oldbegv);
4856 free_marker (oldzv);
4857
4858 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
4859 set_buffer_internal (oldbuf);
4860 if (NILP (tem))
4861 windows_or_buffers_changed = old_windows_or_buffers_changed;
4862 message_log_need_newline = !nlflag;
4863 Vdeactivate_mark = old_deactivate_mark;
4864 }
4865 }
4866
4867
4868 /* We are at the end of the buffer after just having inserted a newline.
4869 (Note: We depend on the fact we won't be crossing the gap.)
4870 Check to see if the most recent message looks a lot like the previous one.
4871 Return 0 if different, 1 if the new one should just replace it, or a
4872 value N > 1 if we should also append " [N times]". */
4873
4874 static int
4875 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
4876 int prev_bol, this_bol;
4877 int prev_bol_byte, this_bol_byte;
4878 {
4879 int i;
4880 int len = Z_BYTE - 1 - this_bol_byte;
4881 int seen_dots = 0;
4882 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
4883 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
4884
4885 for (i = 0; i < len; i++)
4886 {
4887 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.'
4888 && p1[i] != '\n')
4889 seen_dots = 1;
4890 if (p1[i] != p2[i])
4891 return seen_dots;
4892 }
4893 p1 += len;
4894 if (*p1 == '\n')
4895 return 2;
4896 if (*p1++ == ' ' && *p1++ == '[')
4897 {
4898 int n = 0;
4899 while (*p1 >= '0' && *p1 <= '9')
4900 n = n * 10 + *p1++ - '0';
4901 if (strncmp (p1, " times]\n", 8) == 0)
4902 return n+1;
4903 }
4904 return 0;
4905 }
4906
4907
4908 /* Display an echo area message M with a specified length of LEN
4909 chars. The string may include null characters. If M is 0, clear
4910 out any existing message, and let the mini-buffer text show through.
4911
4912 The buffer M must continue to exist until after the echo area gets
4913 cleared or some other message gets displayed there. This means do
4914 not pass text that is stored in a Lisp string; do not pass text in
4915 a buffer that was alloca'd. */
4916
4917 void
4918 message2 (m, len, multibyte)
4919 char *m;
4920 int len;
4921 int multibyte;
4922 {
4923 /* First flush out any partial line written with print. */
4924 message_log_maybe_newline ();
4925 if (m)
4926 message_dolog (m, len, 1, multibyte);
4927 message2_nolog (m, len, multibyte);
4928 }
4929
4930
4931 /* The non-logging counterpart of message2. */
4932
4933 void
4934 message2_nolog (m, len, multibyte)
4935 char *m;
4936 int len;
4937 {
4938 struct frame *sf = SELECTED_FRAME ();
4939 message_enable_multibyte = multibyte;
4940
4941 if (noninteractive)
4942 {
4943 if (noninteractive_need_newline)
4944 putc ('\n', stderr);
4945 noninteractive_need_newline = 0;
4946 if (m)
4947 fwrite (m, len, 1, stderr);
4948 if (cursor_in_echo_area == 0)
4949 fprintf (stderr, "\n");
4950 fflush (stderr);
4951 }
4952 /* A null message buffer means that the frame hasn't really been
4953 initialized yet. Error messages get reported properly by
4954 cmd_error, so this must be just an informative message; toss it. */
4955 else if (INTERACTIVE
4956 && sf->glyphs_initialized_p
4957 && FRAME_MESSAGE_BUF (sf))
4958 {
4959 Lisp_Object mini_window;
4960 struct frame *f;
4961
4962 /* Get the frame containing the mini-buffer
4963 that the selected frame is using. */
4964 mini_window = FRAME_MINIBUF_WINDOW (sf);
4965 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
4966
4967 FRAME_SAMPLE_VISIBILITY (f);
4968 if (FRAME_VISIBLE_P (sf)
4969 && ! FRAME_VISIBLE_P (f))
4970 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
4971
4972 if (m)
4973 {
4974 set_message (m, Qnil, len, multibyte);
4975 if (minibuffer_auto_raise)
4976 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
4977 }
4978 else
4979 clear_message (1, 1);
4980
4981 do_pending_window_change (0);
4982 echo_area_display (1);
4983 do_pending_window_change (0);
4984 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
4985 (*frame_up_to_date_hook) (f);
4986 }
4987 }
4988
4989
4990 /* Display an echo area message M with a specified length of NBYTES
4991 bytes. The string may include null characters. If M is not a
4992 string, clear out any existing message, and let the mini-buffer
4993 text show through. */
4994
4995 void
4996 message3 (m, nbytes, multibyte)
4997 Lisp_Object m;
4998 int nbytes;
4999 int multibyte;
5000 {
5001 struct gcpro gcpro1;
5002
5003 GCPRO1 (m);
5004
5005 /* First flush out any partial line written with print. */
5006 message_log_maybe_newline ();
5007 if (STRINGP (m))
5008 message_dolog (XSTRING (m)->data, nbytes, 1, multibyte);
5009 message3_nolog (m, nbytes, multibyte);
5010
5011 UNGCPRO;
5012 }
5013
5014
5015 /* The non-logging version of message3. */
5016
5017 void
5018 message3_nolog (m, nbytes, multibyte)
5019 Lisp_Object m;
5020 int nbytes, multibyte;
5021 {
5022 struct frame *sf = SELECTED_FRAME ();
5023 message_enable_multibyte = multibyte;
5024
5025 if (noninteractive)
5026 {
5027 if (noninteractive_need_newline)
5028 putc ('\n', stderr);
5029 noninteractive_need_newline = 0;
5030 if (STRINGP (m))
5031 fwrite (XSTRING (m)->data, nbytes, 1, stderr);
5032 if (cursor_in_echo_area == 0)
5033 fprintf (stderr, "\n");
5034 fflush (stderr);
5035 }
5036 /* A null message buffer means that the frame hasn't really been
5037 initialized yet. Error messages get reported properly by
5038 cmd_error, so this must be just an informative message; toss it. */
5039 else if (INTERACTIVE
5040 && sf->glyphs_initialized_p
5041 && FRAME_MESSAGE_BUF (sf))
5042 {
5043 Lisp_Object mini_window;
5044 Lisp_Object frame;
5045 struct frame *f;
5046
5047 /* Get the frame containing the mini-buffer
5048 that the selected frame is using. */
5049 mini_window = FRAME_MINIBUF_WINDOW (sf);
5050 frame = XWINDOW (mini_window)->frame;
5051 f = XFRAME (frame);
5052
5053 FRAME_SAMPLE_VISIBILITY (f);
5054 if (FRAME_VISIBLE_P (sf)
5055 && !FRAME_VISIBLE_P (f))
5056 Fmake_frame_visible (frame);
5057
5058 if (STRINGP (m) && XSTRING (m)->size)
5059 {
5060 set_message (NULL, m, nbytes, multibyte);
5061 if (minibuffer_auto_raise)
5062 Fraise_frame (frame);
5063 }
5064 else
5065 clear_message (1, 1);
5066
5067 do_pending_window_change (0);
5068 echo_area_display (1);
5069 do_pending_window_change (0);
5070 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5071 (*frame_up_to_date_hook) (f);
5072 }
5073 }
5074
5075
5076 /* Display a null-terminated echo area message M. If M is 0, clear
5077 out any existing message, and let the mini-buffer text show through.
5078
5079 The buffer M must continue to exist until after the echo area gets
5080 cleared or some other message gets displayed there. Do not pass
5081 text that is stored in a Lisp string. Do not pass text in a buffer
5082 that was alloca'd. */
5083
5084 void
5085 message1 (m)
5086 char *m;
5087 {
5088 message2 (m, (m ? strlen (m) : 0), 0);
5089 }
5090
5091
5092 /* The non-logging counterpart of message1. */
5093
5094 void
5095 message1_nolog (m)
5096 char *m;
5097 {
5098 message2_nolog (m, (m ? strlen (m) : 0), 0);
5099 }
5100
5101 /* Display a message M which contains a single %s
5102 which gets replaced with STRING. */
5103
5104 void
5105 message_with_string (m, string, log)
5106 char *m;
5107 Lisp_Object string;
5108 int log;
5109 {
5110 if (noninteractive)
5111 {
5112 if (m)
5113 {
5114 if (noninteractive_need_newline)
5115 putc ('\n', stderr);
5116 noninteractive_need_newline = 0;
5117 fprintf (stderr, m, XSTRING (string)->data);
5118 if (cursor_in_echo_area == 0)
5119 fprintf (stderr, "\n");
5120 fflush (stderr);
5121 }
5122 }
5123 else if (INTERACTIVE)
5124 {
5125 /* The frame whose minibuffer we're going to display the message on.
5126 It may be larger than the selected frame, so we need
5127 to use its buffer, not the selected frame's buffer. */
5128 Lisp_Object mini_window;
5129 struct frame *f, *sf = SELECTED_FRAME ();
5130
5131 /* Get the frame containing the minibuffer
5132 that the selected frame is using. */
5133 mini_window = FRAME_MINIBUF_WINDOW (sf);
5134 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5135
5136 /* A null message buffer means that the frame hasn't really been
5137 initialized yet. Error messages get reported properly by
5138 cmd_error, so this must be just an informative message; toss it. */
5139 if (FRAME_MESSAGE_BUF (f))
5140 {
5141 int len;
5142 char *a[1];
5143 a[0] = (char *) XSTRING (string)->data;
5144
5145 len = doprnt (FRAME_MESSAGE_BUF (f),
5146 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5147
5148 if (log)
5149 message2 (FRAME_MESSAGE_BUF (f), len,
5150 STRING_MULTIBYTE (string));
5151 else
5152 message2_nolog (FRAME_MESSAGE_BUF (f), len,
5153 STRING_MULTIBYTE (string));
5154
5155 /* Print should start at the beginning of the message
5156 buffer next time. */
5157 message_buf_print = 0;
5158 }
5159 }
5160 }
5161
5162
5163 /* Dump an informative message to the minibuf. If M is 0, clear out
5164 any existing message, and let the mini-buffer text show through. */
5165
5166 /* VARARGS 1 */
5167 void
5168 message (m, a1, a2, a3)
5169 char *m;
5170 EMACS_INT a1, a2, a3;
5171 {
5172 if (noninteractive)
5173 {
5174 if (m)
5175 {
5176 if (noninteractive_need_newline)
5177 putc ('\n', stderr);
5178 noninteractive_need_newline = 0;
5179 fprintf (stderr, m, a1, a2, a3);
5180 if (cursor_in_echo_area == 0)
5181 fprintf (stderr, "\n");
5182 fflush (stderr);
5183 }
5184 }
5185 else if (INTERACTIVE)
5186 {
5187 /* The frame whose mini-buffer we're going to display the message
5188 on. It may be larger than the selected frame, so we need to
5189 use its buffer, not the selected frame's buffer. */
5190 Lisp_Object mini_window;
5191 struct frame *f, *sf = SELECTED_FRAME ();
5192
5193 /* Get the frame containing the mini-buffer
5194 that the selected frame is using. */
5195 mini_window = FRAME_MINIBUF_WINDOW (sf);
5196 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5197
5198 /* A null message buffer means that the frame hasn't really been
5199 initialized yet. Error messages get reported properly by
5200 cmd_error, so this must be just an informative message; toss
5201 it. */
5202 if (FRAME_MESSAGE_BUF (f))
5203 {
5204 if (m)
5205 {
5206 int len;
5207 #ifdef NO_ARG_ARRAY
5208 char *a[3];
5209 a[0] = (char *) a1;
5210 a[1] = (char *) a2;
5211 a[2] = (char *) a3;
5212
5213 len = doprnt (FRAME_MESSAGE_BUF (f),
5214 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5215 #else
5216 len = doprnt (FRAME_MESSAGE_BUF (f),
5217 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
5218 (char **) &a1);
5219 #endif /* NO_ARG_ARRAY */
5220
5221 message2 (FRAME_MESSAGE_BUF (f), len, 0);
5222 }
5223 else
5224 message1 (0);
5225
5226 /* Print should start at the beginning of the message
5227 buffer next time. */
5228 message_buf_print = 0;
5229 }
5230 }
5231 }
5232
5233
5234 /* The non-logging version of message. */
5235
5236 void
5237 message_nolog (m, a1, a2, a3)
5238 char *m;
5239 EMACS_INT a1, a2, a3;
5240 {
5241 Lisp_Object old_log_max;
5242 old_log_max = Vmessage_log_max;
5243 Vmessage_log_max = Qnil;
5244 message (m, a1, a2, a3);
5245 Vmessage_log_max = old_log_max;
5246 }
5247
5248
5249 /* Display the current message in the current mini-buffer. This is
5250 only called from error handlers in process.c, and is not time
5251 critical. */
5252
5253 void
5254 update_echo_area ()
5255 {
5256 if (!NILP (echo_area_buffer[0]))
5257 {
5258 Lisp_Object string;
5259 string = Fcurrent_message ();
5260 message3 (string, XSTRING (string)->size,
5261 !NILP (current_buffer->enable_multibyte_characters));
5262 }
5263 }
5264
5265
5266 /* Make sure echo area buffers in echo_buffers[] are life. If they
5267 aren't, make new ones. */
5268
5269 static void
5270 ensure_echo_area_buffers ()
5271 {
5272 int i;
5273
5274 for (i = 0; i < 2; ++i)
5275 if (!BUFFERP (echo_buffer[i])
5276 || NILP (XBUFFER (echo_buffer[i])->name))
5277 {
5278 char name[30];
5279 sprintf (name, " *Echo Area %d*", i);
5280 echo_buffer[i] = Fget_buffer_create (build_string (name));
5281 }
5282 }
5283
5284
5285 /* Call FN with args A1..A5 with either the current or last displayed
5286 echo_area_buffer as current buffer.
5287
5288 WHICH zero means use the current message buffer
5289 echo_area_buffer[0]. If that is nil, choose a suitable buffer
5290 from echo_buffer[] and clear it.
5291
5292 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
5293 suitable buffer from echo_buffer[] and clear it.
5294
5295 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
5296 that the current message becomes the last displayed one, make
5297 choose a suitable buffer for echo_area_buffer[0], and clear it.
5298
5299 Value is what FN returns. */
5300
5301 static int
5302 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10)
5303 struct window *w;
5304 int which;
5305 int (*fn) ();
5306 int a1, a2, a3, a4, a5, a6, a7, a8, a9, a10;
5307 {
5308 Lisp_Object buffer;
5309 int this_one, the_other, clear_buffer_p, rc;
5310 int count = specpdl_ptr - specpdl;
5311
5312 /* If buffers aren't life, make new ones. */
5313 ensure_echo_area_buffers ();
5314
5315 clear_buffer_p = 0;
5316
5317 if (which == 0)
5318 this_one = 0, the_other = 1;
5319 else if (which > 0)
5320 this_one = 1, the_other = 0;
5321 else
5322 {
5323 this_one = 0, the_other = 1;
5324 clear_buffer_p = 1;
5325
5326 /* We need a fresh one in case the current echo buffer equals
5327 the one containing the last displayed echo area message. */
5328 if (!NILP (echo_area_buffer[this_one])
5329 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
5330 echo_area_buffer[this_one] = Qnil;
5331 }
5332
5333 /* Choose a suitable buffer from echo_buffer[] is we don't
5334 have one. */
5335 if (NILP (echo_area_buffer[this_one]))
5336 {
5337 echo_area_buffer[this_one]
5338 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
5339 ? echo_buffer[the_other]
5340 : echo_buffer[this_one]);
5341 clear_buffer_p = 1;
5342 }
5343
5344 buffer = echo_area_buffer[this_one];
5345
5346 record_unwind_protect (unwind_with_echo_area_buffer,
5347 with_echo_area_buffer_unwind_data (w));
5348
5349 /* Make the echo area buffer current. Note that for display
5350 purposes, it is not necessary that the displayed window's buffer
5351 == current_buffer, except for text property lookup. So, let's
5352 only set that buffer temporarily here without doing a full
5353 Fset_window_buffer. We must also change w->pointm, though,
5354 because otherwise an assertions in unshow_buffer fails, and Emacs
5355 aborts. */
5356 set_buffer_internal_1 (XBUFFER (buffer));
5357 if (w)
5358 {
5359 w->buffer = buffer;
5360 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
5361 }
5362 current_buffer->truncate_lines = Qnil;
5363 current_buffer->undo_list = Qt;
5364 current_buffer->read_only = Qnil;
5365
5366 if (clear_buffer_p && Z > BEG)
5367 del_range (BEG, Z);
5368
5369 xassert (BEGV >= BEG);
5370 xassert (ZV <= Z && ZV >= BEGV);
5371
5372 rc = fn (a1, a2, a3, a4, a5);
5373
5374 xassert (BEGV >= BEG);
5375 xassert (ZV <= Z && ZV >= BEGV);
5376
5377 unbind_to (count, Qnil);
5378 return rc;
5379 }
5380
5381
5382 /* Save state that should be preserved around the call to the function
5383 FN called in with_echo_area_buffer. */
5384
5385 static Lisp_Object
5386 with_echo_area_buffer_unwind_data (w)
5387 struct window *w;
5388 {
5389 int i = 0;
5390 Lisp_Object vector;
5391
5392 /* Reduce consing by keeping one vector in
5393 Vwith_echo_area_save_vector. */
5394 vector = Vwith_echo_area_save_vector;
5395 Vwith_echo_area_save_vector = Qnil;
5396
5397 if (NILP (vector))
5398 vector = Fmake_vector (make_number (7), Qnil);
5399
5400 XSETBUFFER (XVECTOR (vector)->contents[i], current_buffer); ++i;
5401 XVECTOR (vector)->contents[i++] = Vdeactivate_mark;
5402 XVECTOR (vector)->contents[i++] = make_number (windows_or_buffers_changed);
5403
5404 if (w)
5405 {
5406 XSETWINDOW (XVECTOR (vector)->contents[i], w); ++i;
5407 XVECTOR (vector)->contents[i++] = w->buffer;
5408 XVECTOR (vector)->contents[i++]
5409 = make_number (XMARKER (w->pointm)->charpos);
5410 XVECTOR (vector)->contents[i++]
5411 = make_number (XMARKER (w->pointm)->bytepos);
5412 }
5413 else
5414 {
5415 int end = i + 4;
5416 while (i < end)
5417 XVECTOR (vector)->contents[i++] = Qnil;
5418 }
5419
5420 xassert (i == XVECTOR (vector)->size);
5421 return vector;
5422 }
5423
5424
5425 /* Restore global state from VECTOR which was created by
5426 with_echo_area_buffer_unwind_data. */
5427
5428 static Lisp_Object
5429 unwind_with_echo_area_buffer (vector)
5430 Lisp_Object vector;
5431 {
5432 int i = 0;
5433
5434 set_buffer_internal_1 (XBUFFER (XVECTOR (vector)->contents[i])); ++i;
5435 Vdeactivate_mark = XVECTOR (vector)->contents[i]; ++i;
5436 windows_or_buffers_changed = XFASTINT (XVECTOR (vector)->contents[i]); ++i;
5437
5438 if (WINDOWP (XVECTOR (vector)->contents[i]))
5439 {
5440 struct window *w;
5441 Lisp_Object buffer, charpos, bytepos;
5442
5443 w = XWINDOW (XVECTOR (vector)->contents[i]); ++i;
5444 buffer = XVECTOR (vector)->contents[i]; ++i;
5445 charpos = XVECTOR (vector)->contents[i]; ++i;
5446 bytepos = XVECTOR (vector)->contents[i]; ++i;
5447
5448 w->buffer = buffer;
5449 set_marker_both (w->pointm, buffer,
5450 XFASTINT (charpos), XFASTINT (bytepos));
5451 }
5452
5453 Vwith_echo_area_save_vector = vector;
5454 return Qnil;
5455 }
5456
5457
5458 /* Set up the echo area for use by print functions. MULTIBYTE_P
5459 non-zero means we will print multibyte. */
5460
5461 void
5462 setup_echo_area_for_printing (multibyte_p)
5463 int multibyte_p;
5464 {
5465 ensure_echo_area_buffers ();
5466
5467 if (!message_buf_print)
5468 {
5469 /* A message has been output since the last time we printed.
5470 Choose a fresh echo area buffer. */
5471 if (EQ (echo_area_buffer[1], echo_buffer[0]))
5472 echo_area_buffer[0] = echo_buffer[1];
5473 else
5474 echo_area_buffer[0] = echo_buffer[0];
5475
5476 /* Switch to that buffer and clear it. */
5477 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
5478 if (Z > BEG)
5479 del_range (BEG, Z);
5480 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
5481
5482 /* Set up the buffer for the multibyteness we need. */
5483 if (multibyte_p
5484 != !NILP (current_buffer->enable_multibyte_characters))
5485 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
5486
5487 /* Raise the frame containing the echo area. */
5488 if (minibuffer_auto_raise)
5489 {
5490 struct frame *sf = SELECTED_FRAME ();
5491 Lisp_Object mini_window;
5492 mini_window = FRAME_MINIBUF_WINDOW (sf);
5493 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5494 }
5495
5496 message_buf_print = 1;
5497 }
5498 else
5499 {
5500 if (NILP (echo_area_buffer[0]))
5501 {
5502 if (EQ (echo_area_buffer[1], echo_buffer[0]))
5503 echo_area_buffer[0] = echo_buffer[1];
5504 else
5505 echo_area_buffer[0] = echo_buffer[0];
5506 }
5507
5508 if (current_buffer != XBUFFER (echo_area_buffer[0]))
5509 /* Someone switched buffers between print requests. */
5510 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
5511 }
5512 }
5513
5514
5515 /* Display an echo area message in window W. Value is non-zero if W's
5516 height is changed. If display_last_displayed_message_p is
5517 non-zero, display the message that was last displayed, otherwise
5518 display the current message. */
5519
5520 static int
5521 display_echo_area (w)
5522 struct window *w;
5523 {
5524 int i, no_message_p, window_height_changed_p, count;
5525
5526 /* Temporarily disable garbage collections while displaying the echo
5527 area. This is done because a GC can print a message itself.
5528 That message would modify the echo area buffer's contents while a
5529 redisplay of the buffer is going on, and seriously confuse
5530 redisplay. */
5531 count = inhibit_garbage_collection ();
5532
5533 /* If there is no message, we must call display_echo_area_1
5534 nevertheless because it resizes the window. But we will have to
5535 reset the echo_area_buffer in question to nil at the end because
5536 with_echo_area_buffer will sets it to an empty buffer. */
5537 i = display_last_displayed_message_p ? 1 : 0;
5538 no_message_p = NILP (echo_area_buffer[i]);
5539
5540 window_height_changed_p
5541 = with_echo_area_buffer (w, display_last_displayed_message_p,
5542 (int (*) ()) display_echo_area_1, w);
5543
5544 if (no_message_p)
5545 echo_area_buffer[i] = Qnil;
5546
5547 unbind_to (count, Qnil);
5548 return window_height_changed_p;
5549 }
5550
5551
5552 /* Helper for display_echo_area. Display the current buffer which
5553 contains the current echo area message in window W, a mini-window.
5554 Change the height of W so that all of the message is displayed.
5555 Value is non-zero if height of W was changed. */
5556
5557 static int
5558 display_echo_area_1 (w)
5559 struct window *w;
5560 {
5561 Lisp_Object window;
5562 struct text_pos start;
5563 int window_height_changed_p = 0;
5564
5565 /* Do this before displaying, so that we have a large enough glyph
5566 matrix for the display. */
5567 window_height_changed_p = resize_mini_window (w, 0);
5568
5569 /* Display. */
5570 clear_glyph_matrix (w->desired_matrix);
5571 XSETWINDOW (window, w);
5572 SET_TEXT_POS (start, BEG, BEG_BYTE);
5573 try_window (window, start);
5574
5575 return window_height_changed_p;
5576 }
5577
5578
5579 /* Resize the echo area window to exactly the size needed for the
5580 currently displayed message, if there is one. */
5581
5582 void
5583 resize_echo_area_axactly ()
5584 {
5585 if (BUFFERP (echo_area_buffer[0])
5586 && WINDOWP (echo_area_window))
5587 {
5588 struct window *w = XWINDOW (echo_area_window);
5589 int resized_p;
5590
5591 resized_p = with_echo_area_buffer (w, 0,
5592 (int (*) ()) resize_mini_window,
5593 w, 1);
5594 if (resized_p)
5595 {
5596 ++windows_or_buffers_changed;
5597 ++update_mode_lines;
5598 redisplay_internal (0);
5599 }
5600 }
5601 }
5602
5603
5604 /* Resize mini-window W to fit the size of its contents. EXACT:P
5605 means size the window exactly to the size needed. Otherwise, it's
5606 only enlarged until W's buffer is empty. Value is non-zero if
5607 the window height has been changed. */
5608
5609 int
5610 resize_mini_window (w, exact_p)
5611 struct window *w;
5612 int exact_p;
5613 {
5614 struct frame *f = XFRAME (w->frame);
5615 int window_height_changed_p = 0;
5616
5617 xassert (MINI_WINDOW_P (w));
5618
5619 /* Nil means don't try to resize. */
5620 if (NILP (Vmax_mini_window_height)
5621 || (FRAME_X_P (f) && f->output_data.x == NULL))
5622 return 0;
5623
5624 if (!FRAME_MINIBUF_ONLY_P (f))
5625 {
5626 struct it it;
5627 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
5628 int total_height = XFASTINT (root->height) + XFASTINT (w->height);
5629 int height, max_height;
5630 int unit = CANON_Y_UNIT (f);
5631 struct text_pos start;
5632
5633 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
5634
5635 /* Compute the max. number of lines specified by the user. */
5636 if (FLOATP (Vmax_mini_window_height))
5637 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
5638 else if (INTEGERP (Vmax_mini_window_height))
5639 max_height = XINT (Vmax_mini_window_height);
5640 else
5641 max_height = total_height / 4;
5642
5643 /* Correct that max. height if it's bogus. */
5644 max_height = max (1, max_height);
5645 max_height = min (total_height, max_height);
5646
5647 /* Find out the height of the text in the window. */
5648 last_height = 0;
5649 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
5650 if (it.max_ascent == 0 && it.max_descent == 0)
5651 height = it.current_y + last_height;
5652 else
5653 height = it.current_y + it.max_ascent + it.max_descent;
5654 height = (height + unit - 1) / unit;
5655
5656 /* Compute a suitable window start. */
5657 if (height > max_height)
5658 {
5659 height = max_height;
5660 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
5661 move_it_vertically_backward (&it, (height - 1) * unit);
5662 start = it.current.pos;
5663 }
5664 else
5665 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
5666 SET_MARKER_FROM_TEXT_POS (w->start, start);
5667
5668 /* Let it grow only, until we display an empty message, in which
5669 case the window shrinks again. */
5670 if (height > XFASTINT (w->height))
5671 {
5672 int old_height = XFASTINT (w->height);
5673 freeze_window_starts (f, 1);
5674 grow_mini_window (w, height - XFASTINT (w->height));
5675 window_height_changed_p = XFASTINT (w->height) != old_height;
5676 }
5677 else if (height < XFASTINT (w->height)
5678 && (exact_p || BEGV == ZV))
5679 {
5680 int old_height = XFASTINT (w->height);
5681 freeze_window_starts (f, 0);
5682 shrink_mini_window (w);
5683 window_height_changed_p = XFASTINT (w->height) != old_height;
5684 }
5685 }
5686
5687 return window_height_changed_p;
5688 }
5689
5690
5691 /* Value is the current message, a string, or nil if there is no
5692 current message. */
5693
5694 Lisp_Object
5695 current_message ()
5696 {
5697 Lisp_Object msg;
5698
5699 if (NILP (echo_area_buffer[0]))
5700 msg = Qnil;
5701 else
5702 {
5703 with_echo_area_buffer (0, 0, (int (*) ()) current_message_1, &msg);
5704 if (NILP (msg))
5705 echo_area_buffer[0] = Qnil;
5706 }
5707
5708 return msg;
5709 }
5710
5711
5712 static int
5713 current_message_1 (msg)
5714 Lisp_Object *msg;
5715 {
5716 if (Z > BEG)
5717 *msg = make_buffer_string (BEG, Z, 1);
5718 else
5719 *msg = Qnil;
5720 return 0;
5721 }
5722
5723
5724 /* Push the current message on Vmessage_stack for later restauration
5725 by restore_message. Value is non-zero if the current message isn't
5726 empty. This is a relatively infrequent operation, so it's not
5727 worth optimizing. */
5728
5729 int
5730 push_message ()
5731 {
5732 Lisp_Object msg;
5733 msg = current_message ();
5734 Vmessage_stack = Fcons (msg, Vmessage_stack);
5735 return STRINGP (msg);
5736 }
5737
5738
5739 /* Restore message display from the top of Vmessage_stack. */
5740
5741 void
5742 restore_message ()
5743 {
5744 Lisp_Object msg;
5745
5746 xassert (CONSP (Vmessage_stack));
5747 msg = XCAR (Vmessage_stack);
5748 if (STRINGP (msg))
5749 message3_nolog (msg, STRING_BYTES (XSTRING (msg)), STRING_MULTIBYTE (msg));
5750 else
5751 message3_nolog (msg, 0, 0);
5752 }
5753
5754
5755 /* Pop the top-most entry off Vmessage_stack. */
5756
5757 void
5758 pop_message ()
5759 {
5760 xassert (CONSP (Vmessage_stack));
5761 Vmessage_stack = XCDR (Vmessage_stack);
5762 }
5763
5764
5765 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
5766 exits. If the stack is not empty, we have a missing pop_message
5767 somewhere. */
5768
5769 void
5770 check_message_stack ()
5771 {
5772 if (!NILP (Vmessage_stack))
5773 abort ();
5774 }
5775
5776
5777 /* Truncate to NCHARS what will be displayed in the echo area the next
5778 time we display it---but don't redisplay it now. */
5779
5780 void
5781 truncate_echo_area (nchars)
5782 int nchars;
5783 {
5784 if (nchars == 0)
5785 echo_area_buffer[0] = Qnil;
5786 /* A null message buffer means that the frame hasn't really been
5787 initialized yet. Error messages get reported properly by
5788 cmd_error, so this must be just an informative message; toss it. */
5789 else if (!noninteractive
5790 && INTERACTIVE
5791 && !NILP (echo_area_buffer[0]))
5792 {
5793 struct frame *sf = SELECTED_FRAME ();
5794 if (FRAME_MESSAGE_BUF (sf))
5795 with_echo_area_buffer (0, 0, (int (*) ()) truncate_message_1, nchars);
5796 }
5797 }
5798
5799
5800 /* Helper function for truncate_echo_area. Truncate the current
5801 message to at most NCHARS characters. */
5802
5803 static int
5804 truncate_message_1 (nchars)
5805 int nchars;
5806 {
5807 if (BEG + nchars < Z)
5808 del_range (BEG + nchars, Z);
5809 if (Z == BEG)
5810 echo_area_buffer[0] = Qnil;
5811 return 0;
5812 }
5813
5814
5815 /* Set the current message to a substring of S or STRING.
5816
5817 If STRING is a Lisp string, set the message to the first NBYTES
5818 bytes from STRING. NBYTES zero means use the whole string. If
5819 STRING is multibyte, the message will be displayed multibyte.
5820
5821 If S is not null, set the message to the first LEN bytes of S. LEN
5822 zero means use the whole string. MULTIBYTE_P non-zero means S is
5823 multibyte. Display the message multibyte in that case. */
5824
5825 void
5826 set_message (s, string, nbytes, multibyte_p)
5827 char *s;
5828 Lisp_Object string;
5829 int nbytes;
5830 {
5831 message_enable_multibyte
5832 = ((s && multibyte_p)
5833 || (STRINGP (string) && STRING_MULTIBYTE (string)));
5834
5835 with_echo_area_buffer (0, -1, (int (*) ()) set_message_1,
5836 s, string, nbytes, multibyte_p);
5837 message_buf_print = 0;
5838 }
5839
5840
5841 /* Helper function for set_message. Arguments have the same meaning
5842 as there. This function is called with the echo area buffer being
5843 current. */
5844
5845 static int
5846 set_message_1 (s, string, nbytes, multibyte_p)
5847 char *s;
5848 Lisp_Object string;
5849 int nbytes, multibyte_p;
5850 {
5851 xassert (BEG == Z);
5852
5853 /* Change multibyteness of the echo buffer appropriately. */
5854 if (message_enable_multibyte
5855 != !NILP (current_buffer->enable_multibyte_characters))
5856 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
5857
5858 /* Insert new message at BEG. */
5859 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
5860
5861 if (STRINGP (string))
5862 {
5863 int nchars;
5864
5865 if (nbytes == 0)
5866 nbytes = XSTRING (string)->size_byte;
5867 nchars = string_byte_to_char (string, nbytes);
5868
5869 /* This function takes care of single/multibyte conversion. We
5870 just have to ensure that the echo area buffer has the right
5871 setting of enable_multibyte_characters. */
5872 insert_from_string (string, 0, 0, nchars, nbytes, 1);
5873 }
5874 else if (s)
5875 {
5876 if (nbytes == 0)
5877 nbytes = strlen (s);
5878
5879 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
5880 {
5881 /* Convert from multi-byte to single-byte. */
5882 int i, c, n;
5883 unsigned char work[1];
5884
5885 /* Convert a multibyte string to single-byte. */
5886 for (i = 0; i < nbytes; i += n)
5887 {
5888 c = string_char_and_length (s + i, nbytes - i, &n);
5889 work[0] = (SINGLE_BYTE_CHAR_P (c)
5890 ? c
5891 : multibyte_char_to_unibyte (c, Qnil));
5892 insert_1_both (work, 1, 1, 1, 0, 0);
5893 }
5894 }
5895 else if (!multibyte_p
5896 && !NILP (current_buffer->enable_multibyte_characters))
5897 {
5898 /* Convert from single-byte to multi-byte. */
5899 int i, c, n;
5900 unsigned char *msg = (unsigned char *) s;
5901 unsigned char str[MAX_MULTIBYTE_LENGTH];
5902
5903 /* Convert a single-byte string to multibyte. */
5904 for (i = 0; i < nbytes; i++)
5905 {
5906 c = unibyte_char_to_multibyte (msg[i]);
5907 n = CHAR_STRING (c, str);
5908 insert_1_both (str, 1, n, 1, 0, 0);
5909 }
5910 }
5911 else
5912 insert_1 (s, nbytes, 1, 0, 0);
5913 }
5914
5915 return 0;
5916 }
5917
5918
5919 /* Clear messages. CURRENT_P non-zero means clear the current
5920 message. LAST_DISPLAYED_P non-zero means clear the message
5921 last displayed. */
5922
5923 void
5924 clear_message (current_p, last_displayed_p)
5925 int current_p, last_displayed_p;
5926 {
5927 if (current_p)
5928 echo_area_buffer[0] = Qnil;
5929
5930 if (last_displayed_p)
5931 echo_area_buffer[1] = Qnil;
5932
5933 message_buf_print = 0;
5934 }
5935
5936 /* Clear garbaged frames.
5937
5938 This function is used where the old redisplay called
5939 redraw_garbaged_frames which in turn called redraw_frame which in
5940 turn called clear_frame. The call to clear_frame was a source of
5941 flickering. I believe a clear_frame is not necessary. It should
5942 suffice in the new redisplay to invalidate all current matrices,
5943 and ensure a complete redisplay of all windows. */
5944
5945 static void
5946 clear_garbaged_frames ()
5947 {
5948 if (frame_garbaged)
5949 {
5950 Lisp_Object tail, frame;
5951
5952 FOR_EACH_FRAME (tail, frame)
5953 {
5954 struct frame *f = XFRAME (frame);
5955
5956 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
5957 {
5958 clear_current_matrices (f);
5959 f->garbaged = 0;
5960 }
5961 }
5962
5963 frame_garbaged = 0;
5964 ++windows_or_buffers_changed;
5965 }
5966 }
5967
5968
5969 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
5970 is non-zero update selected_frame. Value is non-zero if the
5971 mini-windows height has been changed. */
5972
5973 static int
5974 echo_area_display (update_frame_p)
5975 int update_frame_p;
5976 {
5977 Lisp_Object mini_window;
5978 struct window *w;
5979 struct frame *f;
5980 int window_height_changed_p = 0;
5981 struct frame *sf = SELECTED_FRAME ();
5982
5983 mini_window = FRAME_MINIBUF_WINDOW (sf);
5984 w = XWINDOW (mini_window);
5985 f = XFRAME (WINDOW_FRAME (w));
5986
5987 /* Don't display if frame is invisible or not yet initialized. */
5988 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
5989 return 0;
5990
5991 #ifdef HAVE_WINDOW_SYSTEM
5992 /* When Emacs starts, selected_frame may be a visible terminal
5993 frame, even if we run under a window system. If we let this
5994 through, a message would be displayed on the terminal. */
5995 if (EQ (selected_frame, Vterminal_frame)
5996 && !NILP (Vwindow_system))
5997 return 0;
5998 #endif /* HAVE_WINDOW_SYSTEM */
5999
6000 /* Redraw garbaged frames. */
6001 if (frame_garbaged)
6002 clear_garbaged_frames ();
6003
6004 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
6005 {
6006 echo_area_window = mini_window;
6007 window_height_changed_p = display_echo_area (w);
6008 w->must_be_updated_p = 1;
6009
6010 if (update_frame_p)
6011 {
6012 /* Not called from redisplay_internal. If we changed
6013 window configuration, we must redisplay thoroughly.
6014 Otherwise, we can do with updating what we displayed
6015 above. */
6016 if (window_height_changed_p)
6017 {
6018 ++windows_or_buffers_changed;
6019 ++update_mode_lines;
6020 redisplay_internal (0);
6021 }
6022 else if (FRAME_WINDOW_P (f))
6023 {
6024 update_single_window (w, 1);
6025 rif->flush_display (f);
6026 }
6027 else
6028 update_frame (f, 1, 1);
6029 }
6030 }
6031 else if (!EQ (mini_window, selected_window))
6032 windows_or_buffers_changed++;
6033
6034 /* Last displayed message is now the current message. */
6035 echo_area_buffer[1] = echo_area_buffer[0];
6036
6037 /* Prevent redisplay optimization in redisplay_internal by resetting
6038 this_line_start_pos. This is done because the mini-buffer now
6039 displays the message instead of its buffer text. */
6040 if (EQ (mini_window, selected_window))
6041 CHARPOS (this_line_start_pos) = 0;
6042
6043 return window_height_changed_p;
6044 }
6045
6046
6047 \f
6048 /***********************************************************************
6049 Frame Titles
6050 ***********************************************************************/
6051
6052
6053 #ifdef HAVE_WINDOW_SYSTEM
6054
6055 /* A buffer for constructing frame titles in it; allocated from the
6056 heap in init_xdisp and resized as needed in store_frame_title_char. */
6057
6058 static char *frame_title_buf;
6059
6060 /* The buffer's end, and a current output position in it. */
6061
6062 static char *frame_title_buf_end;
6063 static char *frame_title_ptr;
6064
6065
6066 /* Store a single character C for the frame title in frame_title_buf.
6067 Re-allocate frame_title_buf if necessary. */
6068
6069 static void
6070 store_frame_title_char (c)
6071 char c;
6072 {
6073 /* If output position has reached the end of the allocated buffer,
6074 double the buffer's size. */
6075 if (frame_title_ptr == frame_title_buf_end)
6076 {
6077 int len = frame_title_ptr - frame_title_buf;
6078 int new_size = 2 * len * sizeof *frame_title_buf;
6079 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
6080 frame_title_buf_end = frame_title_buf + new_size;
6081 frame_title_ptr = frame_title_buf + len;
6082 }
6083
6084 *frame_title_ptr++ = c;
6085 }
6086
6087
6088 /* Store part of a frame title in frame_title_buf, beginning at
6089 frame_title_ptr. STR is the string to store. Do not copy more
6090 than PRECISION number of bytes from STR; PRECISION <= 0 means copy
6091 the whole string. Pad with spaces until FIELD_WIDTH number of
6092 characters have been copied; FIELD_WIDTH <= 0 means don't pad.
6093 Called from display_mode_element when it is used to build a frame
6094 title. */
6095
6096 static int
6097 store_frame_title (str, field_width, precision)
6098 unsigned char *str;
6099 int field_width, precision;
6100 {
6101 int n = 0;
6102
6103 /* Copy at most PRECISION chars from STR. */
6104 while ((precision <= 0 || n < precision)
6105 && *str)
6106 {
6107 store_frame_title_char (*str++);
6108 ++n;
6109 }
6110
6111 /* Fill up with spaces until FIELD_WIDTH reached. */
6112 while (field_width > 0
6113 && n < field_width)
6114 {
6115 store_frame_title_char (' ');
6116 ++n;
6117 }
6118
6119 return n;
6120 }
6121
6122
6123 /* Set the title of FRAME, if it has changed. The title format is
6124 Vicon_title_format if FRAME is iconified, otherwise it is
6125 frame_title_format. */
6126
6127 static void
6128 x_consider_frame_title (frame)
6129 Lisp_Object frame;
6130 {
6131 struct frame *f = XFRAME (frame);
6132
6133 if (FRAME_WINDOW_P (f)
6134 || FRAME_MINIBUF_ONLY_P (f)
6135 || f->explicit_name)
6136 {
6137 /* Do we have more than one visible frame on this X display? */
6138 Lisp_Object tail;
6139 Lisp_Object fmt;
6140 struct buffer *obuf;
6141 int len;
6142 struct it it;
6143
6144 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
6145 {
6146 struct frame *tf = XFRAME (XCAR (tail));
6147
6148 if (tf != f
6149 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
6150 && !FRAME_MINIBUF_ONLY_P (tf)
6151 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
6152 break;
6153 }
6154
6155 /* Set global variable indicating that multiple frames exist. */
6156 multiple_frames = CONSP (tail);
6157
6158 /* Switch to the buffer of selected window of the frame. Set up
6159 frame_title_ptr so that display_mode_element will output into it;
6160 then display the title. */
6161 obuf = current_buffer;
6162 Fset_buffer (XWINDOW (f->selected_window)->buffer);
6163 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
6164 frame_title_ptr = frame_title_buf;
6165 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
6166 NULL, DEFAULT_FACE_ID);
6167 len = display_mode_element (&it, 0, -1, -1, fmt);
6168 frame_title_ptr = NULL;
6169 set_buffer_internal (obuf);
6170
6171 /* Set the title only if it's changed. This avoids consing in
6172 the common case where it hasn't. (If it turns out that we've
6173 already wasted too much time by walking through the list with
6174 display_mode_element, then we might need to optimize at a
6175 higher level than this.) */
6176 if (! STRINGP (f->name)
6177 || STRING_BYTES (XSTRING (f->name)) != len
6178 || bcmp (frame_title_buf, XSTRING (f->name)->data, len) != 0)
6179 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
6180 }
6181 }
6182
6183 #else /* not HAVE_WINDOW_SYSTEM */
6184
6185 #define frame_title_ptr ((char *)0)
6186 #define store_frame_title(str, mincol, maxcol) 0
6187
6188 #endif /* not HAVE_WINDOW_SYSTEM */
6189
6190
6191
6192 \f
6193 /***********************************************************************
6194 Menu Bars
6195 ***********************************************************************/
6196
6197
6198 /* Prepare for redisplay by updating menu-bar item lists when
6199 appropriate. This can call eval. */
6200
6201 void
6202 prepare_menu_bars ()
6203 {
6204 int all_windows;
6205 struct gcpro gcpro1, gcpro2;
6206 struct frame *f;
6207 struct frame *tooltip_frame;
6208
6209 #ifdef HAVE_X_WINDOWS
6210 tooltip_frame = tip_frame;
6211 #else
6212 tooltip_frame = NULL;
6213 #endif
6214
6215 /* Update all frame titles based on their buffer names, etc. We do
6216 this before the menu bars so that the buffer-menu will show the
6217 up-to-date frame titles. */
6218 #ifdef HAVE_WINDOW_SYSTEM
6219 if (windows_or_buffers_changed || update_mode_lines)
6220 {
6221 Lisp_Object tail, frame;
6222
6223 FOR_EACH_FRAME (tail, frame)
6224 {
6225 f = XFRAME (frame);
6226 if (f != tooltip_frame
6227 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
6228 x_consider_frame_title (frame);
6229 }
6230 }
6231 #endif /* HAVE_WINDOW_SYSTEM */
6232
6233 /* Update the menu bar item lists, if appropriate. This has to be
6234 done before any actual redisplay or generation of display lines. */
6235 all_windows = (update_mode_lines
6236 || buffer_shared > 1
6237 || windows_or_buffers_changed);
6238 if (all_windows)
6239 {
6240 Lisp_Object tail, frame;
6241 int count = specpdl_ptr - specpdl;
6242
6243 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6244
6245 FOR_EACH_FRAME (tail, frame)
6246 {
6247 f = XFRAME (frame);
6248
6249 /* Ignore tooltip frame. */
6250 if (f == tooltip_frame)
6251 continue;
6252
6253 /* If a window on this frame changed size, report that to
6254 the user and clear the size-change flag. */
6255 if (FRAME_WINDOW_SIZES_CHANGED (f))
6256 {
6257 Lisp_Object functions;
6258
6259 /* Clear flag first in case we get an error below. */
6260 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
6261 functions = Vwindow_size_change_functions;
6262 GCPRO2 (tail, functions);
6263
6264 while (CONSP (functions))
6265 {
6266 call1 (XCAR (functions), frame);
6267 functions = XCDR (functions);
6268 }
6269 UNGCPRO;
6270 }
6271
6272 GCPRO1 (tail);
6273 update_menu_bar (f, 0);
6274 #ifdef HAVE_WINDOW_SYSTEM
6275 update_tool_bar (f, 0);
6276 #endif
6277 UNGCPRO;
6278 }
6279
6280 unbind_to (count, Qnil);
6281 }
6282 else
6283 {
6284 struct frame *sf = SELECTED_FRAME ();
6285 update_menu_bar (sf, 1);
6286 #ifdef HAVE_WINDOW_SYSTEM
6287 update_tool_bar (sf, 1);
6288 #endif
6289 }
6290
6291 /* Motif needs this. See comment in xmenu.c. Turn it off when
6292 pending_menu_activation is not defined. */
6293 #ifdef USE_X_TOOLKIT
6294 pending_menu_activation = 0;
6295 #endif
6296 }
6297
6298
6299 /* Update the menu bar item list for frame F. This has to be done
6300 before we start to fill in any display lines, because it can call
6301 eval.
6302
6303 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
6304
6305 static void
6306 update_menu_bar (f, save_match_data)
6307 struct frame *f;
6308 int save_match_data;
6309 {
6310 Lisp_Object window;
6311 register struct window *w;
6312
6313 window = FRAME_SELECTED_WINDOW (f);
6314 w = XWINDOW (window);
6315
6316 if (update_mode_lines)
6317 w->update_mode_line = Qt;
6318
6319 if (FRAME_WINDOW_P (f)
6320 ?
6321 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
6322 FRAME_EXTERNAL_MENU_BAR (f)
6323 #else
6324 FRAME_MENU_BAR_LINES (f) > 0
6325 #endif
6326 : FRAME_MENU_BAR_LINES (f) > 0)
6327 {
6328 /* If the user has switched buffers or windows, we need to
6329 recompute to reflect the new bindings. But we'll
6330 recompute when update_mode_lines is set too; that means
6331 that people can use force-mode-line-update to request
6332 that the menu bar be recomputed. The adverse effect on
6333 the rest of the redisplay algorithm is about the same as
6334 windows_or_buffers_changed anyway. */
6335 if (windows_or_buffers_changed
6336 || !NILP (w->update_mode_line)
6337 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
6338 < BUF_MODIFF (XBUFFER (w->buffer)))
6339 != !NILP (w->last_had_star))
6340 || ((!NILP (Vtransient_mark_mode)
6341 && !NILP (XBUFFER (w->buffer)->mark_active))
6342 != !NILP (w->region_showing)))
6343 {
6344 struct buffer *prev = current_buffer;
6345 int count = specpdl_ptr - specpdl;
6346
6347 set_buffer_internal_1 (XBUFFER (w->buffer));
6348 if (save_match_data)
6349 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6350 if (NILP (Voverriding_local_map_menu_flag))
6351 {
6352 specbind (Qoverriding_terminal_local_map, Qnil);
6353 specbind (Qoverriding_local_map, Qnil);
6354 }
6355
6356 /* Run the Lucid hook. */
6357 call1 (Vrun_hooks, Qactivate_menubar_hook);
6358
6359 /* If it has changed current-menubar from previous value,
6360 really recompute the menu-bar from the value. */
6361 if (! NILP (Vlucid_menu_bar_dirty_flag))
6362 call0 (Qrecompute_lucid_menubar);
6363
6364 safe_run_hooks (Qmenu_bar_update_hook);
6365 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
6366
6367 /* Redisplay the menu bar in case we changed it. */
6368 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
6369 if (FRAME_WINDOW_P (f))
6370 set_frame_menubar (f, 0, 0);
6371 else
6372 /* On a terminal screen, the menu bar is an ordinary screen
6373 line, and this makes it get updated. */
6374 w->update_mode_line = Qt;
6375 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
6376 /* In the non-toolkit version, the menu bar is an ordinary screen
6377 line, and this makes it get updated. */
6378 w->update_mode_line = Qt;
6379 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
6380
6381 unbind_to (count, Qnil);
6382 set_buffer_internal_1 (prev);
6383 }
6384 }
6385 }
6386
6387
6388 \f
6389 /***********************************************************************
6390 Tool-bars
6391 ***********************************************************************/
6392
6393 #ifdef HAVE_WINDOW_SYSTEM
6394
6395 /* Update the tool-bar item list for frame F. This has to be done
6396 before we start to fill in any display lines. Called from
6397 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
6398 and restore it here. */
6399
6400 static void
6401 update_tool_bar (f, save_match_data)
6402 struct frame *f;
6403 int save_match_data;
6404 {
6405 if (WINDOWP (f->tool_bar_window)
6406 && XFASTINT (XWINDOW (f->tool_bar_window)->height) > 0)
6407 {
6408 Lisp_Object window;
6409 struct window *w;
6410
6411 window = FRAME_SELECTED_WINDOW (f);
6412 w = XWINDOW (window);
6413
6414 /* If the user has switched buffers or windows, we need to
6415 recompute to reflect the new bindings. But we'll
6416 recompute when update_mode_lines is set too; that means
6417 that people can use force-mode-line-update to request
6418 that the menu bar be recomputed. The adverse effect on
6419 the rest of the redisplay algorithm is about the same as
6420 windows_or_buffers_changed anyway. */
6421 if (windows_or_buffers_changed
6422 || !NILP (w->update_mode_line)
6423 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
6424 < BUF_MODIFF (XBUFFER (w->buffer)))
6425 != !NILP (w->last_had_star))
6426 || ((!NILP (Vtransient_mark_mode)
6427 && !NILP (XBUFFER (w->buffer)->mark_active))
6428 != !NILP (w->region_showing)))
6429 {
6430 struct buffer *prev = current_buffer;
6431 int count = specpdl_ptr - specpdl;
6432
6433 /* Set current_buffer to the buffer of the selected
6434 window of the frame, so that we get the right local
6435 keymaps. */
6436 set_buffer_internal_1 (XBUFFER (w->buffer));
6437
6438 /* Save match data, if we must. */
6439 if (save_match_data)
6440 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6441
6442 /* Make sure that we don't accidentally use bogus keymaps. */
6443 if (NILP (Voverriding_local_map_menu_flag))
6444 {
6445 specbind (Qoverriding_terminal_local_map, Qnil);
6446 specbind (Qoverriding_local_map, Qnil);
6447 }
6448
6449 /* Build desired tool-bar items from keymaps. */
6450 f->desired_tool_bar_items
6451 = tool_bar_items (f->desired_tool_bar_items,
6452 &f->n_desired_tool_bar_items);
6453
6454 /* Redisplay the tool-bar in case we changed it. */
6455 w->update_mode_line = Qt;
6456
6457 unbind_to (count, Qnil);
6458 set_buffer_internal_1 (prev);
6459 }
6460 }
6461 }
6462
6463
6464 /* Set F->desired_tool_bar_string to a Lisp string representing frame
6465 F's desired tool-bar contents. F->desired_tool_bar_items must have
6466 been set up previously by calling prepare_menu_bars. */
6467
6468 static void
6469 build_desired_tool_bar_string (f)
6470 struct frame *f;
6471 {
6472 int i, size, size_needed, string_idx;
6473 struct gcpro gcpro1, gcpro2, gcpro3;
6474 Lisp_Object image, plist, props;
6475
6476 image = plist = props = Qnil;
6477 GCPRO3 (image, plist, props);
6478
6479 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
6480 Otherwise, make a new string. */
6481
6482 /* The size of the string we might be able to reuse. */
6483 size = (STRINGP (f->desired_tool_bar_string)
6484 ? XSTRING (f->desired_tool_bar_string)->size
6485 : 0);
6486
6487 /* Each image in the string we build is preceded by a space,
6488 and there is a space at the end. */
6489 size_needed = f->n_desired_tool_bar_items + 1;
6490
6491 /* Reuse f->desired_tool_bar_string, if possible. */
6492 if (size < size_needed)
6493 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
6494 make_number (' '));
6495 else
6496 {
6497 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
6498 Fremove_text_properties (make_number (0), make_number (size),
6499 props, f->desired_tool_bar_string);
6500 }
6501
6502 /* Put a `display' property on the string for the images to display,
6503 put a `menu_item' property on tool-bar items with a value that
6504 is the index of the item in F's tool-bar item vector. */
6505 for (i = 0, string_idx = 0;
6506 i < f->n_desired_tool_bar_items;
6507 ++i, string_idx += 1)
6508 {
6509 #define PROP(IDX) \
6510 (XVECTOR (f->desired_tool_bar_items) \
6511 ->contents[i * TOOL_BAR_ITEM_NSLOTS + (IDX)])
6512
6513 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
6514 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
6515 int margin, relief;
6516 extern Lisp_Object QCrelief, QCmargin, QCalgorithm, Qimage;
6517 extern Lisp_Object Qlaplace;
6518
6519 /* If image is a vector, choose the image according to the
6520 button state. */
6521 image = PROP (TOOL_BAR_ITEM_IMAGES);
6522 if (VECTORP (image))
6523 {
6524 enum tool_bar_item_image idx;
6525
6526 if (enabled_p)
6527 idx = (selected_p
6528 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
6529 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
6530 else
6531 idx = (selected_p
6532 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
6533 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
6534
6535 xassert (XVECTOR (image)->size >= idx);
6536 image = XVECTOR (image)->contents[idx];
6537 }
6538
6539 /* Ignore invalid image specifications. */
6540 if (!valid_image_p (image))
6541 continue;
6542
6543 /* Display the tool-bar button pressed, or depressed. */
6544 plist = Fcopy_sequence (XCDR (image));
6545
6546 /* Compute margin and relief to draw. */
6547 relief = tool_bar_button_relief > 0 ? tool_bar_button_relief : 3;
6548 margin = relief + max (0, tool_bar_button_margin);
6549
6550 if (auto_raise_tool_bar_buttons_p)
6551 {
6552 /* Add a `:relief' property to the image spec if the item is
6553 selected. */
6554 if (selected_p)
6555 {
6556 plist = Fplist_put (plist, QCrelief, make_number (-relief));
6557 margin -= relief;
6558 }
6559 }
6560 else
6561 {
6562 /* If image is selected, display it pressed, i.e. with a
6563 negative relief. If it's not selected, display it with a
6564 raised relief. */
6565 plist = Fplist_put (plist, QCrelief,
6566 (selected_p
6567 ? make_number (-relief)
6568 : make_number (relief)));
6569 margin -= relief;
6570 }
6571
6572 /* Put a margin around the image. */
6573 if (margin)
6574 plist = Fplist_put (plist, QCmargin, make_number (margin));
6575
6576 /* If button is not enabled, make the image appear disabled by
6577 applying an appropriate algorithm to it. */
6578 if (!enabled_p)
6579 plist = Fplist_put (plist, QCalgorithm, Qlaplace);
6580
6581 /* Put a `display' text property on the string for the image to
6582 display. Put a `menu-item' property on the string that gives
6583 the start of this item's properties in the tool-bar items
6584 vector. */
6585 image = Fcons (Qimage, plist);
6586 props = list4 (Qdisplay, image,
6587 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS)),
6588 Fadd_text_properties (make_number (string_idx),
6589 make_number (string_idx + 1),
6590 props, f->desired_tool_bar_string);
6591 #undef PROP
6592 }
6593
6594 UNGCPRO;
6595 }
6596
6597
6598 /* Display one line of the tool-bar of frame IT->f. */
6599
6600 static void
6601 display_tool_bar_line (it)
6602 struct it *it;
6603 {
6604 struct glyph_row *row = it->glyph_row;
6605 int max_x = it->last_visible_x;
6606 struct glyph *last;
6607
6608 prepare_desired_row (row);
6609 row->y = it->current_y;
6610
6611 while (it->current_x < max_x)
6612 {
6613 int x_before, x, n_glyphs_before, i, nglyphs;
6614
6615 /* Get the next display element. */
6616 if (!get_next_display_element (it))
6617 break;
6618
6619 /* Produce glyphs. */
6620 x_before = it->current_x;
6621 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
6622 PRODUCE_GLYPHS (it);
6623
6624 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
6625 i = 0;
6626 x = x_before;
6627 while (i < nglyphs)
6628 {
6629 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
6630
6631 if (x + glyph->pixel_width > max_x)
6632 {
6633 /* Glyph doesn't fit on line. */
6634 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
6635 it->current_x = x;
6636 goto out;
6637 }
6638
6639 ++it->hpos;
6640 x += glyph->pixel_width;
6641 ++i;
6642 }
6643
6644 /* Stop at line ends. */
6645 if (ITERATOR_AT_END_OF_LINE_P (it))
6646 break;
6647
6648 set_iterator_to_next (it);
6649 }
6650
6651 out:;
6652
6653 row->displays_text_p = row->used[TEXT_AREA] != 0;
6654 extend_face_to_end_of_line (it);
6655 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
6656 last->right_box_line_p = 1;
6657 compute_line_metrics (it);
6658
6659 /* If line is empty, make it occupy the rest of the tool-bar. */
6660 if (!row->displays_text_p)
6661 {
6662 row->height = row->phys_height = it->last_visible_y - row->y;
6663 row->ascent = row->phys_ascent = 0;
6664 }
6665
6666 row->full_width_p = 1;
6667 row->continued_p = 0;
6668 row->truncated_on_left_p = 0;
6669 row->truncated_on_right_p = 0;
6670
6671 it->current_x = it->hpos = 0;
6672 it->current_y += row->height;
6673 ++it->vpos;
6674 ++it->glyph_row;
6675 }
6676
6677
6678 /* Value is the number of screen lines needed to make all tool-bar
6679 items of frame F visible. */
6680
6681 static int
6682 tool_bar_lines_needed (f)
6683 struct frame *f;
6684 {
6685 struct window *w = XWINDOW (f->tool_bar_window);
6686 struct it it;
6687
6688 /* Initialize an iterator for iteration over
6689 F->desired_tool_bar_string in the tool-bar window of frame F. */
6690 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
6691 it.first_visible_x = 0;
6692 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
6693 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
6694
6695 while (!ITERATOR_AT_END_P (&it))
6696 {
6697 it.glyph_row = w->desired_matrix->rows;
6698 clear_glyph_row (it.glyph_row);
6699 display_tool_bar_line (&it);
6700 }
6701
6702 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
6703 }
6704
6705
6706 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
6707 height should be changed. */
6708
6709 static int
6710 redisplay_tool_bar (f)
6711 struct frame *f;
6712 {
6713 struct window *w;
6714 struct it it;
6715 struct glyph_row *row;
6716 int change_height_p = 0;
6717
6718 /* If frame hasn't a tool-bar window or if it is zero-height, don't
6719 do anything. This means you must start with tool-bar-lines
6720 non-zero to get the auto-sizing effect. Or in other words, you
6721 can turn off tool-bars by specifying tool-bar-lines zero. */
6722 if (!WINDOWP (f->tool_bar_window)
6723 || (w = XWINDOW (f->tool_bar_window),
6724 XFASTINT (w->height) == 0))
6725 return 0;
6726
6727 /* Set up an iterator for the tool-bar window. */
6728 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
6729 it.first_visible_x = 0;
6730 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
6731 row = it.glyph_row;
6732
6733 /* Build a string that represents the contents of the tool-bar. */
6734 build_desired_tool_bar_string (f);
6735 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
6736
6737 /* Display as many lines as needed to display all tool-bar items. */
6738 while (it.current_y < it.last_visible_y)
6739 display_tool_bar_line (&it);
6740
6741 /* It doesn't make much sense to try scrolling in the tool-bar
6742 window, so don't do it. */
6743 w->desired_matrix->no_scrolling_p = 1;
6744 w->must_be_updated_p = 1;
6745
6746 if (auto_resize_tool_bars_p)
6747 {
6748 int nlines;
6749
6750 /* If there are blank lines at the end, except for a partially
6751 visible blank line at the end that is smaller than
6752 CANON_Y_UNIT, change the tool-bar's height. */
6753 row = it.glyph_row - 1;
6754 if (!row->displays_text_p
6755 && row->height >= CANON_Y_UNIT (f))
6756 change_height_p = 1;
6757
6758 /* If row displays tool-bar items, but is partially visible,
6759 change the tool-bar's height. */
6760 if (row->displays_text_p
6761 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
6762 change_height_p = 1;
6763
6764 /* Resize windows as needed by changing the `tool-bar-lines'
6765 frame parameter. */
6766 if (change_height_p
6767 && (nlines = tool_bar_lines_needed (f),
6768 nlines != XFASTINT (w->height)))
6769 {
6770 extern Lisp_Object Qtool_bar_lines;
6771 Lisp_Object frame;
6772
6773 XSETFRAME (frame, f);
6774 clear_glyph_matrix (w->desired_matrix);
6775 Fmodify_frame_parameters (frame,
6776 Fcons (Fcons (Qtool_bar_lines,
6777 make_number (nlines)),
6778 Qnil));
6779 fonts_changed_p = 1;
6780 }
6781 }
6782
6783 return change_height_p;
6784 }
6785
6786
6787 /* Get information about the tool-bar item which is displayed in GLYPH
6788 on frame F. Return in *PROP_IDX the index where tool-bar item
6789 properties start in F->current_tool_bar_items. Value is zero if
6790 GLYPH doesn't display a tool-bar item. */
6791
6792 int
6793 tool_bar_item_info (f, glyph, prop_idx)
6794 struct frame *f;
6795 struct glyph *glyph;
6796 int *prop_idx;
6797 {
6798 Lisp_Object prop;
6799 int success_p;
6800
6801 /* Get the text property `menu-item' at pos. The value of that
6802 property is the start index of this item's properties in
6803 F->current_tool_bar_items. */
6804 prop = Fget_text_property (make_number (glyph->charpos),
6805 Qmenu_item, f->current_tool_bar_string);
6806 if (INTEGERP (prop))
6807 {
6808 *prop_idx = XINT (prop);
6809 success_p = 1;
6810 }
6811 else
6812 success_p = 0;
6813
6814 return success_p;
6815 }
6816
6817 #endif /* HAVE_WINDOW_SYSTEM */
6818
6819
6820 \f
6821 /************************************************************************
6822 Horizontal scrolling
6823 ************************************************************************/
6824
6825 static int hscroll_window_tree P_ ((Lisp_Object));
6826 static int hscroll_windows P_ ((Lisp_Object));
6827
6828 /* For all leaf windows in the window tree rooted at WINDOW, set their
6829 hscroll value so that PT is (i) visible in the window, and (ii) so
6830 that it is not within a certain margin at the window's left and
6831 right border. Value is non-zero if any window's hscroll has been
6832 changed. */
6833
6834 static int
6835 hscroll_window_tree (window)
6836 Lisp_Object window;
6837 {
6838 int hscrolled_p = 0;
6839
6840 while (WINDOWP (window))
6841 {
6842 struct window *w = XWINDOW (window);
6843
6844 if (WINDOWP (w->hchild))
6845 hscrolled_p |= hscroll_window_tree (w->hchild);
6846 else if (WINDOWP (w->vchild))
6847 hscrolled_p |= hscroll_window_tree (w->vchild);
6848 else if (w->cursor.vpos >= 0)
6849 {
6850 int hscroll_margin, text_area_x, text_area_y;
6851 int text_area_width, text_area_height;
6852 struct glyph_row *current_cursor_row
6853 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
6854 struct glyph_row *desired_cursor_row
6855 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
6856 struct glyph_row *cursor_row
6857 = (desired_cursor_row->enabled_p
6858 ? desired_cursor_row
6859 : current_cursor_row);
6860
6861 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
6862 &text_area_width, &text_area_height);
6863
6864 /* Scroll when cursor is inside this scroll margin. */
6865 hscroll_margin = 5 * CANON_X_UNIT (XFRAME (w->frame));
6866
6867 if ((XFASTINT (w->hscroll)
6868 && w->cursor.x < hscroll_margin)
6869 || (cursor_row->enabled_p
6870 && cursor_row->truncated_on_right_p
6871 && (w->cursor.x > text_area_width - hscroll_margin)))
6872 {
6873 struct it it;
6874 int hscroll;
6875 struct buffer *saved_current_buffer;
6876 int pt;
6877
6878 /* Find point in a display of infinite width. */
6879 saved_current_buffer = current_buffer;
6880 current_buffer = XBUFFER (w->buffer);
6881
6882 if (w == XWINDOW (selected_window))
6883 pt = BUF_PT (current_buffer);
6884 else
6885 {
6886 pt = marker_position (w->pointm);
6887 pt = max (BEGV, pt);
6888 pt = min (ZV, pt);
6889 }
6890
6891 /* Move iterator to pt starting at cursor_row->start in
6892 a line with infinite width. */
6893 init_to_row_start (&it, w, cursor_row);
6894 it.last_visible_x = INFINITY;
6895 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
6896 current_buffer = saved_current_buffer;
6897
6898 /* Center cursor in window. */
6899 hscroll = (max (0, it.current_x - text_area_width / 2)
6900 / CANON_X_UNIT (it.f));
6901
6902 /* Don't call Fset_window_hscroll if value hasn't
6903 changed because it will prevent redisplay
6904 optimizations. */
6905 if (XFASTINT (w->hscroll) != hscroll)
6906 {
6907 Fset_window_hscroll (window, make_number (hscroll));
6908 hscrolled_p = 1;
6909 }
6910 }
6911 }
6912
6913 window = w->next;
6914 }
6915
6916 /* Value is non-zero if hscroll of any leaf window has been changed. */
6917 return hscrolled_p;
6918 }
6919
6920
6921 /* Set hscroll so that cursor is visible and not inside horizontal
6922 scroll margins for all windows in the tree rooted at WINDOW. See
6923 also hscroll_window_tree above. Value is non-zero if any window's
6924 hscroll has been changed. If it has, desired matrices on the frame
6925 of WINDOW are cleared. */
6926
6927 static int
6928 hscroll_windows (window)
6929 Lisp_Object window;
6930 {
6931 int hscrolled_p;
6932
6933 if (automatic_hscrolling_p)
6934 {
6935 hscrolled_p = hscroll_window_tree (window);
6936 if (hscrolled_p)
6937 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
6938 }
6939 else
6940 hscrolled_p = 0;
6941 return hscrolled_p;
6942 }
6943
6944
6945 \f
6946 /************************************************************************
6947 Redisplay
6948 ************************************************************************/
6949
6950 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
6951 to a non-zero value. This is sometimes handy to have in a debugger
6952 session. */
6953
6954 #if GLYPH_DEBUG
6955
6956 /* First and last unchanged row for try_window_id. */
6957
6958 int debug_first_unchanged_at_end_vpos;
6959 int debug_last_unchanged_at_beg_vpos;
6960
6961 /* Delta vpos and y. */
6962
6963 int debug_dvpos, debug_dy;
6964
6965 /* Delta in characters and bytes for try_window_id. */
6966
6967 int debug_delta, debug_delta_bytes;
6968
6969 /* Values of window_end_pos and window_end_vpos at the end of
6970 try_window_id. */
6971
6972 int debug_end_pos, debug_end_vpos;
6973
6974 /* Append a string to W->desired_matrix->method. FMT is a printf
6975 format string. A1...A9 are a supplement for a variable-length
6976 argument list. If trace_redisplay_p is non-zero also printf the
6977 resulting string to stderr. */
6978
6979 static void
6980 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
6981 struct window *w;
6982 char *fmt;
6983 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
6984 {
6985 char buffer[512];
6986 char *method = w->desired_matrix->method;
6987 int len = strlen (method);
6988 int size = sizeof w->desired_matrix->method;
6989 int remaining = size - len - 1;
6990
6991 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
6992 if (len && remaining)
6993 {
6994 method[len] = '|';
6995 --remaining, ++len;
6996 }
6997
6998 strncpy (method + len, buffer, remaining);
6999
7000 if (trace_redisplay_p)
7001 fprintf (stderr, "%p (%s): %s\n",
7002 w,
7003 ((BUFFERP (w->buffer)
7004 && STRINGP (XBUFFER (w->buffer)->name))
7005 ? (char *) XSTRING (XBUFFER (w->buffer)->name)->data
7006 : "no buffer"),
7007 buffer);
7008 }
7009
7010 #endif /* GLYPH_DEBUG */
7011
7012
7013 /* This counter is used to clear the face cache every once in a while
7014 in redisplay_internal. It is incremented for each redisplay.
7015 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
7016 cleared. */
7017
7018 #define CLEAR_FACE_CACHE_COUNT 10000
7019 static int clear_face_cache_count;
7020
7021 /* Record the previous terminal frame we displayed. */
7022
7023 static struct frame *previous_terminal_frame;
7024
7025 /* Non-zero while redisplay_internal is in progress. */
7026
7027 int redisplaying_p;
7028
7029
7030 /* Value is non-zero if all changes in window W, which displays
7031 current_buffer, are in the text between START and END. START is a
7032 buffer position, END is given as a distance from Z. Used in
7033 redisplay_internal for display optimization. */
7034
7035 static INLINE int
7036 text_outside_line_unchanged_p (w, start, end)
7037 struct window *w;
7038 int start, end;
7039 {
7040 int unchanged_p = 1;
7041
7042 /* If text or overlays have changed, see where. */
7043 if (XFASTINT (w->last_modified) < MODIFF
7044 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
7045 {
7046 /* Gap in the line? */
7047 if (GPT < start || Z - GPT < end)
7048 unchanged_p = 0;
7049
7050 /* Changes start in front of the line, or end after it? */
7051 if (unchanged_p
7052 && (BEG_UNCHANGED < start - 1
7053 || END_UNCHANGED < end))
7054 unchanged_p = 0;
7055
7056 /* If selective display, can't optimize if changes start at the
7057 beginning of the line. */
7058 if (unchanged_p
7059 && INTEGERP (current_buffer->selective_display)
7060 && XINT (current_buffer->selective_display) > 0
7061 && (BEG_UNCHANGED < start || GPT <= start))
7062 unchanged_p = 0;
7063 }
7064
7065 return unchanged_p;
7066 }
7067
7068
7069 /* Do a frame update, taking possible shortcuts into account. This is
7070 the main external entry point for redisplay.
7071
7072 If the last redisplay displayed an echo area message and that message
7073 is no longer requested, we clear the echo area or bring back the
7074 mini-buffer if that is in use. */
7075
7076 void
7077 redisplay ()
7078 {
7079 redisplay_internal (0);
7080 }
7081
7082 /* Return 1 if point moved out of or into a composition. Otherwise
7083 return 0. PREV_BUF and PREV_PT are the last point buffer and
7084 position. BUF and PT are the current point buffer and position. */
7085
7086 int
7087 check_point_in_composition (prev_buf, prev_pt, buf, pt)
7088 struct buffer *prev_buf, *buf;
7089 int prev_pt, pt;
7090 {
7091 int start, end;
7092 Lisp_Object prop;
7093 Lisp_Object buffer;
7094
7095 XSETBUFFER (buffer, buf);
7096 /* Check a composition at the last point if point moved within the
7097 same buffer. */
7098 if (prev_buf == buf)
7099 {
7100 if (prev_pt == pt)
7101 /* Point didn't move. */
7102 return 0;
7103
7104 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
7105 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
7106 && COMPOSITION_VALID_P (start, end, prop)
7107 && start < prev_pt && end > prev_pt)
7108 /* The last point was within the composition. Return 1 iff
7109 point moved out of the composition. */
7110 return (pt <= start || pt >= end);
7111 }
7112
7113 /* Check a composition at the current point. */
7114 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
7115 && find_composition (pt, -1, &start, &end, &prop, buffer)
7116 && COMPOSITION_VALID_P (start, end, prop)
7117 && start < pt && end > pt);
7118 }
7119
7120 /* Reconsider the setting of B->clip_changed which is displayed
7121 in window W. */
7122
7123 static INLINE void
7124 reconsider_clip_changes (w, b)
7125 struct window *w;
7126 struct buffer *b;
7127 {
7128 if (b->prevent_redisplay_optimizations_p)
7129 b->clip_changed = 1;
7130 else if (b->clip_changed
7131 && !NILP (w->window_end_valid)
7132 && w->current_matrix->buffer == b
7133 && w->current_matrix->zv == BUF_ZV (b)
7134 && w->current_matrix->begv == BUF_BEGV (b))
7135 b->clip_changed = 0;
7136
7137 /* If display wasn't paused, and W is not a tool bar window, see if
7138 point has been moved into or out of a composition. In that case,
7139 we set b->clip_changed to 1 to force updating the screen. If
7140 b->clip_changed has already been set to 1, we can skip this
7141 check. */
7142 if (!b->clip_changed
7143 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
7144 {
7145 int pt;
7146
7147 if (w == XWINDOW (selected_window))
7148 pt = BUF_PT (current_buffer);
7149 else
7150 pt = marker_position (w->pointm);
7151
7152 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
7153 || pt != XINT (w->last_point))
7154 && check_point_in_composition (w->current_matrix->buffer,
7155 XINT (w->last_point),
7156 XBUFFER (w->buffer), pt))
7157 b->clip_changed = 1;
7158 }
7159 }
7160
7161
7162 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
7163 response to any user action; therefore, we should preserve the echo
7164 area. (Actually, our caller does that job.) Perhaps in the future
7165 avoid recentering windows if it is not necessary; currently that
7166 causes some problems. */
7167
7168 static void
7169 redisplay_internal (preserve_echo_area)
7170 int preserve_echo_area;
7171 {
7172 struct window *w = XWINDOW (selected_window);
7173 struct frame *f = XFRAME (w->frame);
7174 int pause;
7175 int must_finish = 0;
7176 struct text_pos tlbufpos, tlendpos;
7177 int number_of_visible_frames;
7178 int count;
7179 struct frame *sf = SELECTED_FRAME ();
7180
7181 /* Non-zero means redisplay has to consider all windows on all
7182 frames. Zero means, only selected_window is considered. */
7183 int consider_all_windows_p;
7184
7185 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
7186
7187 /* No redisplay if running in batch mode or frame is not yet fully
7188 initialized, or redisplay is explicitly turned off by setting
7189 Vinhibit_redisplay. */
7190 if (noninteractive
7191 || !NILP (Vinhibit_redisplay)
7192 || !f->glyphs_initialized_p)
7193 return;
7194
7195 /* The flag redisplay_performed_directly_p is set by
7196 direct_output_for_insert when it already did the whole screen
7197 update necessary. */
7198 if (redisplay_performed_directly_p)
7199 {
7200 redisplay_performed_directly_p = 0;
7201 if (!hscroll_windows (selected_window))
7202 return;
7203 }
7204
7205 #ifdef USE_X_TOOLKIT
7206 if (popup_activated ())
7207 return;
7208 #endif
7209
7210 /* I don't think this happens but let's be paranoid. */
7211 if (redisplaying_p)
7212 return;
7213
7214 /* Record a function that resets redisplaying_p to its old value
7215 when we leave this function. */
7216 count = specpdl_ptr - specpdl;
7217 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
7218 ++redisplaying_p;
7219
7220 retry:
7221
7222 reconsider_clip_changes (w, current_buffer);
7223
7224 /* If new fonts have been loaded that make a glyph matrix adjustment
7225 necessary, do it. */
7226 if (fonts_changed_p)
7227 {
7228 adjust_glyphs (NULL);
7229 ++windows_or_buffers_changed;
7230 fonts_changed_p = 0;
7231 }
7232
7233 if (! FRAME_WINDOW_P (sf)
7234 && previous_terminal_frame != sf)
7235 {
7236 /* Since frames on an ASCII terminal share the same display
7237 area, displaying a different frame means redisplay the whole
7238 thing. */
7239 windows_or_buffers_changed++;
7240 SET_FRAME_GARBAGED (sf);
7241 XSETFRAME (Vterminal_frame, sf);
7242 }
7243 previous_terminal_frame = sf;
7244
7245 /* Set the visible flags for all frames. Do this before checking
7246 for resized or garbaged frames; they want to know if their frames
7247 are visible. See the comment in frame.h for
7248 FRAME_SAMPLE_VISIBILITY. */
7249 {
7250 Lisp_Object tail, frame;
7251
7252 number_of_visible_frames = 0;
7253
7254 FOR_EACH_FRAME (tail, frame)
7255 {
7256 struct frame *f = XFRAME (frame);
7257
7258 FRAME_SAMPLE_VISIBILITY (f);
7259 if (FRAME_VISIBLE_P (f))
7260 ++number_of_visible_frames;
7261 clear_desired_matrices (f);
7262 }
7263 }
7264
7265 /* Notice any pending interrupt request to change frame size. */
7266 do_pending_window_change (1);
7267
7268 /* Clear frames marked as garbaged. */
7269 if (frame_garbaged)
7270 clear_garbaged_frames ();
7271
7272 /* Build menubar and tool-bar items. */
7273 prepare_menu_bars ();
7274
7275 if (windows_or_buffers_changed)
7276 update_mode_lines++;
7277
7278 /* Detect case that we need to write or remove a star in the mode line. */
7279 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
7280 {
7281 w->update_mode_line = Qt;
7282 if (buffer_shared > 1)
7283 update_mode_lines++;
7284 }
7285
7286 /* If %c is in the mode line, update it if needed. */
7287 if (!NILP (w->column_number_displayed)
7288 /* This alternative quickly identifies a common case
7289 where no change is needed. */
7290 && !(PT == XFASTINT (w->last_point)
7291 && XFASTINT (w->last_modified) >= MODIFF
7292 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
7293 && XFASTINT (w->column_number_displayed) != current_column ())
7294 w->update_mode_line = Qt;
7295
7296 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
7297
7298 /* The variable buffer_shared is set in redisplay_window and
7299 indicates that we redisplay a buffer in different windows. See
7300 there. */
7301 consider_all_windows_p = update_mode_lines || buffer_shared > 1;
7302
7303 /* If specs for an arrow have changed, do thorough redisplay
7304 to ensure we remove any arrow that should no longer exist. */
7305 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
7306 || ! EQ (Voverlay_arrow_string, last_arrow_string))
7307 consider_all_windows_p = windows_or_buffers_changed = 1;
7308
7309 /* Normally the message* functions will have already displayed and
7310 updated the echo area, but the frame may have been trashed, or
7311 the update may have been preempted, so display the echo area
7312 again here. Checking both message buffers captures the case that
7313 the echo area should be cleared. */
7314 if (!NILP (echo_area_buffer[0]) || !NILP (echo_area_buffer[1]))
7315 {
7316 int window_height_changed_p = echo_area_display (0);
7317 must_finish = 1;
7318
7319 if (fonts_changed_p)
7320 goto retry;
7321 else if (window_height_changed_p)
7322 {
7323 consider_all_windows_p = 1;
7324 ++update_mode_lines;
7325 ++windows_or_buffers_changed;
7326
7327 /* If window configuration was changed, frames may have been
7328 marked garbaged. Clear them or we will experience
7329 surprises wrt scrolling. */
7330 if (frame_garbaged)
7331 clear_garbaged_frames ();
7332 }
7333 }
7334 else if (w == XWINDOW (minibuf_window)
7335 && (current_buffer->clip_changed
7336 || XFASTINT (w->last_modified) < MODIFF
7337 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
7338 && resize_mini_window (w, 0))
7339 {
7340 /* Resized active mini-window to fit the size of what it is
7341 showing if its contents might have changed. */
7342 must_finish = 1;
7343 consider_all_windows_p = 1;
7344 ++windows_or_buffers_changed;
7345 ++update_mode_lines;
7346
7347 /* If window configuration was changed, frames may have been
7348 marked garbaged. Clear them or we will experience
7349 surprises wrt scrolling. */
7350 if (frame_garbaged)
7351 clear_garbaged_frames ();
7352 }
7353
7354
7355 /* If showing the region, and mark has changed, we must redisplay
7356 the whole window. The assignment to this_line_start_pos prevents
7357 the optimization directly below this if-statement. */
7358 if (((!NILP (Vtransient_mark_mode)
7359 && !NILP (XBUFFER (w->buffer)->mark_active))
7360 != !NILP (w->region_showing))
7361 || (!NILP (w->region_showing)
7362 && !EQ (w->region_showing,
7363 Fmarker_position (XBUFFER (w->buffer)->mark))))
7364 CHARPOS (this_line_start_pos) = 0;
7365
7366 /* Optimize the case that only the line containing the cursor in the
7367 selected window has changed. Variables starting with this_ are
7368 set in display_line and record information about the line
7369 containing the cursor. */
7370 tlbufpos = this_line_start_pos;
7371 tlendpos = this_line_end_pos;
7372 if (!consider_all_windows_p
7373 && CHARPOS (tlbufpos) > 0
7374 && NILP (w->update_mode_line)
7375 && !current_buffer->clip_changed
7376 && FRAME_VISIBLE_P (XFRAME (w->frame))
7377 && !FRAME_OBSCURED_P (XFRAME (w->frame))
7378 /* Make sure recorded data applies to current buffer, etc. */
7379 && this_line_buffer == current_buffer
7380 && current_buffer == XBUFFER (w->buffer)
7381 && NILP (w->force_start)
7382 /* Point must be on the line that we have info recorded about. */
7383 && PT >= CHARPOS (tlbufpos)
7384 && PT <= Z - CHARPOS (tlendpos)
7385 /* All text outside that line, including its final newline,
7386 must be unchanged */
7387 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
7388 CHARPOS (tlendpos)))
7389 {
7390 if (CHARPOS (tlbufpos) > BEGV
7391 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
7392 && (CHARPOS (tlbufpos) == ZV
7393 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
7394 /* Former continuation line has disappeared by becoming empty */
7395 goto cancel;
7396 else if (XFASTINT (w->last_modified) < MODIFF
7397 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
7398 || MINI_WINDOW_P (w))
7399 {
7400 /* We have to handle the case of continuation around a
7401 wide-column character (See the comment in indent.c around
7402 line 885).
7403
7404 For instance, in the following case:
7405
7406 -------- Insert --------
7407 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
7408 J_I_ ==> J_I_ `^^' are cursors.
7409 ^^ ^^
7410 -------- --------
7411
7412 As we have to redraw the line above, we should goto cancel. */
7413
7414 struct it it;
7415 int line_height_before = this_line_pixel_height;
7416
7417 /* Note that start_display will handle the case that the
7418 line starting at tlbufpos is a continuation lines. */
7419 start_display (&it, w, tlbufpos);
7420
7421 /* Implementation note: It this still necessary? */
7422 if (it.current_x != this_line_start_x)
7423 goto cancel;
7424
7425 TRACE ((stderr, "trying display optimization 1\n"));
7426 w->cursor.vpos = -1;
7427 overlay_arrow_seen = 0;
7428 it.vpos = this_line_vpos;
7429 it.current_y = this_line_y;
7430 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
7431 display_line (&it);
7432
7433 /* If line contains point, is not continued,
7434 and ends at same distance from eob as before, we win */
7435 if (w->cursor.vpos >= 0
7436 /* Line is not continued, otherwise this_line_start_pos
7437 would have been set to 0 in display_line. */
7438 && CHARPOS (this_line_start_pos)
7439 /* Line ends as before. */
7440 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
7441 /* Line has same height as before. Otherwise other lines
7442 would have to be shifted up or down. */
7443 && this_line_pixel_height == line_height_before)
7444 {
7445 /* If this is not the window's last line, we must adjust
7446 the charstarts of the lines below. */
7447 if (it.current_y < it.last_visible_y)
7448 {
7449 struct glyph_row *row
7450 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
7451 int delta, delta_bytes;
7452
7453 if (Z - CHARPOS (tlendpos) == ZV)
7454 {
7455 /* This line ends at end of (accessible part of)
7456 buffer. There is no newline to count. */
7457 delta = (Z
7458 - CHARPOS (tlendpos)
7459 - MATRIX_ROW_START_CHARPOS (row));
7460 delta_bytes = (Z_BYTE
7461 - BYTEPOS (tlendpos)
7462 - MATRIX_ROW_START_BYTEPOS (row));
7463 }
7464 else
7465 {
7466 /* This line ends in a newline. Must take
7467 account of the newline and the rest of the
7468 text that follows. */
7469 delta = (Z
7470 - CHARPOS (tlendpos)
7471 - MATRIX_ROW_START_CHARPOS (row));
7472 delta_bytes = (Z_BYTE
7473 - BYTEPOS (tlendpos)
7474 - MATRIX_ROW_START_BYTEPOS (row));
7475 }
7476
7477 increment_matrix_positions (w->current_matrix,
7478 this_line_vpos + 1,
7479 w->current_matrix->nrows,
7480 delta, delta_bytes);
7481 }
7482
7483 /* If this row displays text now but previously didn't,
7484 or vice versa, w->window_end_vpos may have to be
7485 adjusted. */
7486 if ((it.glyph_row - 1)->displays_text_p)
7487 {
7488 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
7489 XSETINT (w->window_end_vpos, this_line_vpos);
7490 }
7491 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
7492 && this_line_vpos > 0)
7493 XSETINT (w->window_end_vpos, this_line_vpos - 1);
7494 w->window_end_valid = Qnil;
7495
7496 /* Update hint: No need to try to scroll in update_window. */
7497 w->desired_matrix->no_scrolling_p = 1;
7498
7499 #if GLYPH_DEBUG
7500 *w->desired_matrix->method = 0;
7501 debug_method_add (w, "optimization 1");
7502 #endif
7503 goto update;
7504 }
7505 else
7506 goto cancel;
7507 }
7508 else if (/* Cursor position hasn't changed. */
7509 PT == XFASTINT (w->last_point)
7510 /* Make sure the cursor was last displayed
7511 in this window. Otherwise we have to reposition it. */
7512 && 0 <= w->cursor.vpos
7513 && XINT (w->height) > w->cursor.vpos)
7514 {
7515 if (!must_finish)
7516 {
7517 do_pending_window_change (1);
7518
7519 /* We used to always goto end_of_redisplay here, but this
7520 isn't enough if we have a blinking cursor. */
7521 if (w->cursor_off_p == w->last_cursor_off_p)
7522 goto end_of_redisplay;
7523 }
7524 goto update;
7525 }
7526 /* If highlighting the region, or if the cursor is in the echo area,
7527 then we can't just move the cursor. */
7528 else if (! (!NILP (Vtransient_mark_mode)
7529 && !NILP (current_buffer->mark_active))
7530 && (w == XWINDOW (current_buffer->last_selected_window)
7531 || highlight_nonselected_windows)
7532 && NILP (w->region_showing)
7533 && NILP (Vshow_trailing_whitespace)
7534 && !cursor_in_echo_area)
7535 {
7536 struct it it;
7537 struct glyph_row *row;
7538
7539 /* Skip from tlbufpos to PT and see where it is. Note that
7540 PT may be in invisible text. If so, we will end at the
7541 next visible position. */
7542 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
7543 NULL, DEFAULT_FACE_ID);
7544 it.current_x = this_line_start_x;
7545 it.current_y = this_line_y;
7546 it.vpos = this_line_vpos;
7547
7548 /* The call to move_it_to stops in front of PT, but
7549 moves over before-strings. */
7550 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
7551
7552 if (it.vpos == this_line_vpos
7553 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
7554 row->enabled_p))
7555 {
7556 xassert (this_line_vpos == it.vpos);
7557 xassert (this_line_y == it.current_y);
7558 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
7559 goto update;
7560 }
7561 else
7562 goto cancel;
7563 }
7564
7565 cancel:
7566 /* Text changed drastically or point moved off of line. */
7567 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
7568 }
7569
7570 CHARPOS (this_line_start_pos) = 0;
7571 consider_all_windows_p |= buffer_shared > 1;
7572 ++clear_face_cache_count;
7573
7574
7575 /* Build desired matrices. If consider_all_windows_p is non-zero,
7576 do it for all windows on all frames. Otherwise do it for
7577 selected_window, only. */
7578
7579 if (consider_all_windows_p)
7580 {
7581 Lisp_Object tail, frame;
7582
7583 /* Clear the face cache eventually. */
7584 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
7585 {
7586 clear_face_cache (0);
7587 clear_face_cache_count = 0;
7588 }
7589
7590 /* Recompute # windows showing selected buffer. This will be
7591 incremented each time such a window is displayed. */
7592 buffer_shared = 0;
7593
7594 FOR_EACH_FRAME (tail, frame)
7595 {
7596 struct frame *f = XFRAME (frame);
7597 if (FRAME_WINDOW_P (f) || f == sf)
7598 {
7599 /* Mark all the scroll bars to be removed; we'll redeem
7600 the ones we want when we redisplay their windows. */
7601 if (condemn_scroll_bars_hook)
7602 (*condemn_scroll_bars_hook) (f);
7603
7604 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
7605 redisplay_windows (FRAME_ROOT_WINDOW (f));
7606
7607 /* Any scroll bars which redisplay_windows should have
7608 nuked should now go away. */
7609 if (judge_scroll_bars_hook)
7610 (*judge_scroll_bars_hook) (f);
7611 }
7612 }
7613 }
7614 else if (FRAME_VISIBLE_P (sf)
7615 && !FRAME_OBSCURED_P (sf))
7616 redisplay_window (selected_window, 1);
7617
7618
7619 /* Compare desired and current matrices, perform output. */
7620
7621 update:
7622
7623 /* If fonts changed, display again. */
7624 if (fonts_changed_p)
7625 goto retry;
7626
7627 /* Prevent various kinds of signals during display update.
7628 stdio is not robust about handling signals,
7629 which can cause an apparent I/O error. */
7630 if (interrupt_input)
7631 unrequest_sigio ();
7632 stop_polling ();
7633
7634 if (consider_all_windows_p)
7635 {
7636 Lisp_Object tail;
7637 struct frame *f;
7638 int hscrolled_p;
7639
7640 pause = 0;
7641 hscrolled_p = 0;
7642
7643 /* See if we have to hscroll. */
7644 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
7645 if (FRAMEP (XCAR (tail)))
7646 {
7647 f = XFRAME (XCAR (tail));
7648
7649 if ((FRAME_WINDOW_P (f)
7650 || f == sf)
7651 && FRAME_VISIBLE_P (f)
7652 && !FRAME_OBSCURED_P (f)
7653 && hscroll_windows (f->root_window))
7654 hscrolled_p = 1;
7655 }
7656
7657 if (hscrolled_p)
7658 goto retry;
7659
7660 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
7661 {
7662 if (!FRAMEP (XCAR (tail)))
7663 continue;
7664
7665 f = XFRAME (XCAR (tail));
7666
7667 if ((FRAME_WINDOW_P (f) || f == sf)
7668 && FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
7669 {
7670 /* Mark all windows as to be updated. */
7671 set_window_update_flags (XWINDOW (f->root_window), 1);
7672 pause |= update_frame (f, 0, 0);
7673 if (!pause)
7674 {
7675 mark_window_display_accurate (f->root_window, 1);
7676 if (frame_up_to_date_hook != 0)
7677 (*frame_up_to_date_hook) (f);
7678 }
7679 }
7680 }
7681 }
7682 else
7683 {
7684 if (FRAME_VISIBLE_P (sf)
7685 && !FRAME_OBSCURED_P (sf))
7686 {
7687 if (hscroll_windows (selected_window))
7688 goto retry;
7689
7690 XWINDOW (selected_window)->must_be_updated_p = 1;
7691 pause = update_frame (sf, 0, 0);
7692 }
7693 else
7694 pause = 0;
7695
7696 /* We may have called echo_area_display at the top of this
7697 function. If the echo area is on another frame, that may
7698 have put text on a frame other than the selected one, so the
7699 above call to update_frame would not have caught it. Catch
7700 it here. */
7701 {
7702 Lisp_Object mini_window;
7703 struct frame *mini_frame;
7704
7705 mini_window = FRAME_MINIBUF_WINDOW (sf);
7706 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7707
7708 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
7709 {
7710 XWINDOW (mini_window)->must_be_updated_p = 1;
7711 pause |= update_frame (mini_frame, 0, 0);
7712 if (!pause && hscroll_windows (mini_window))
7713 goto retry;
7714 }
7715 }
7716 }
7717
7718 /* If display was paused because of pending input, make sure we do a
7719 thorough update the next time. */
7720 if (pause)
7721 {
7722 /* Prevent the optimization at the beginning of
7723 redisplay_internal that tries a single-line update of the
7724 line containing the cursor in the selected window. */
7725 CHARPOS (this_line_start_pos) = 0;
7726
7727 /* Let the overlay arrow be updated the next time. */
7728 if (!NILP (last_arrow_position))
7729 {
7730 last_arrow_position = Qt;
7731 last_arrow_string = Qt;
7732 }
7733
7734 /* If we pause after scrolling, some rows in the current
7735 matrices of some windows are not valid. */
7736 if (!WINDOW_FULL_WIDTH_P (w)
7737 && !FRAME_WINDOW_P (XFRAME (w->frame)))
7738 update_mode_lines = 1;
7739 }
7740
7741 /* Now text on frame agrees with windows, so put info into the
7742 windows for partial redisplay to follow. */
7743 if (!pause)
7744 {
7745 register struct buffer *b = XBUFFER (w->buffer);
7746
7747 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
7748 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
7749 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
7750 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
7751
7752 if (consider_all_windows_p)
7753 mark_window_display_accurate (FRAME_ROOT_WINDOW (sf), 1);
7754 else
7755 {
7756 XSETFASTINT (w->last_point, BUF_PT (b));
7757 w->last_cursor = w->cursor;
7758 w->last_cursor_off_p = w->cursor_off_p;
7759
7760 b->clip_changed = 0;
7761 b->prevent_redisplay_optimizations_p = 0;
7762 w->update_mode_line = Qnil;
7763 XSETFASTINT (w->last_modified, BUF_MODIFF (b));
7764 XSETFASTINT (w->last_overlay_modified, BUF_OVERLAY_MODIFF (b));
7765 w->last_had_star
7766 = (BUF_MODIFF (XBUFFER (w->buffer)) > BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7767 ? Qt : Qnil);
7768
7769 /* Record if we are showing a region, so can make sure to
7770 update it fully at next redisplay. */
7771 w->region_showing = (!NILP (Vtransient_mark_mode)
7772 && (w == XWINDOW (current_buffer->last_selected_window)
7773 || highlight_nonselected_windows)
7774 && !NILP (XBUFFER (w->buffer)->mark_active)
7775 ? Fmarker_position (XBUFFER (w->buffer)->mark)
7776 : Qnil);
7777
7778 w->window_end_valid = w->buffer;
7779 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
7780 last_arrow_string = Voverlay_arrow_string;
7781 if (frame_up_to_date_hook != 0)
7782 (*frame_up_to_date_hook) (sf);
7783
7784 w->current_matrix->buffer = b;
7785 w->current_matrix->begv = BUF_BEGV (b);
7786 w->current_matrix->zv = BUF_ZV (b);
7787 }
7788
7789 update_mode_lines = 0;
7790 windows_or_buffers_changed = 0;
7791 }
7792
7793 /* Start SIGIO interrupts coming again. Having them off during the
7794 code above makes it less likely one will discard output, but not
7795 impossible, since there might be stuff in the system buffer here.
7796 But it is much hairier to try to do anything about that. */
7797 if (interrupt_input)
7798 request_sigio ();
7799 start_polling ();
7800
7801 /* If a frame has become visible which was not before, redisplay
7802 again, so that we display it. Expose events for such a frame
7803 (which it gets when becoming visible) don't call the parts of
7804 redisplay constructing glyphs, so simply exposing a frame won't
7805 display anything in this case. So, we have to display these
7806 frames here explicitly. */
7807 if (!pause)
7808 {
7809 Lisp_Object tail, frame;
7810 int new_count = 0;
7811
7812 FOR_EACH_FRAME (tail, frame)
7813 {
7814 int this_is_visible = 0;
7815
7816 if (XFRAME (frame)->visible)
7817 this_is_visible = 1;
7818 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
7819 if (XFRAME (frame)->visible)
7820 this_is_visible = 1;
7821
7822 if (this_is_visible)
7823 new_count++;
7824 }
7825
7826 if (new_count != number_of_visible_frames)
7827 windows_or_buffers_changed++;
7828 }
7829
7830 /* Change frame size now if a change is pending. */
7831 do_pending_window_change (1);
7832
7833 /* If we just did a pending size change, or have additional
7834 visible frames, redisplay again. */
7835 if (windows_or_buffers_changed && !pause)
7836 goto retry;
7837
7838 end_of_redisplay:;
7839
7840 unbind_to (count, Qnil);
7841 }
7842
7843
7844 /* Redisplay, but leave alone any recent echo area message unless
7845 another message has been requested in its place.
7846
7847 This is useful in situations where you need to redisplay but no
7848 user action has occurred, making it inappropriate for the message
7849 area to be cleared. See tracking_off and
7850 wait_reading_process_input for examples of these situations. */
7851
7852 void
7853 redisplay_preserve_echo_area ()
7854 {
7855 if (!NILP (echo_area_buffer[1]))
7856 {
7857 /* We have a previously displayed message, but no current
7858 message. Redisplay the previous message. */
7859 display_last_displayed_message_p = 1;
7860 redisplay_internal (1);
7861 display_last_displayed_message_p = 0;
7862 }
7863 else
7864 redisplay_internal (1);
7865 }
7866
7867
7868 /* Function registered with record_unwind_protect in
7869 redisplay_internal. Clears the flag indicating that a redisplay is
7870 in progress. */
7871
7872 static Lisp_Object
7873 unwind_redisplay (old_redisplaying_p)
7874 Lisp_Object old_redisplaying_p;
7875 {
7876 redisplaying_p = XFASTINT (old_redisplaying_p);
7877 return Qnil;
7878 }
7879
7880
7881 /* Mark the display of windows in the window tree rooted at WINDOW as
7882 accurate or inaccurate. If FLAG is non-zero mark display of WINDOW
7883 as accurate. If FLAG is zero arrange for WINDOW to be redisplayed
7884 the next time redisplay_internal is called. */
7885
7886 void
7887 mark_window_display_accurate (window, accurate_p)
7888 Lisp_Object window;
7889 int accurate_p;
7890 {
7891 struct window *w;
7892
7893 for (; !NILP (window); window = w->next)
7894 {
7895 w = XWINDOW (window);
7896
7897 if (BUFFERP (w->buffer))
7898 {
7899 struct buffer *b = XBUFFER (w->buffer);
7900
7901 XSETFASTINT (w->last_modified,
7902 accurate_p ? BUF_MODIFF (b) : 0);
7903 XSETFASTINT (w->last_overlay_modified,
7904 accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
7905 w->last_had_star = (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b)
7906 ? Qt : Qnil);
7907
7908 #if 0 /* I don't think this is necessary because display_line does it.
7909 Let's check it. */
7910 /* Record if we are showing a region, so can make sure to
7911 update it fully at next redisplay. */
7912 w->region_showing
7913 = (!NILP (Vtransient_mark_mode)
7914 && (w == XWINDOW (current_buffer->last_selected_window)
7915 || highlight_nonselected_windows)
7916 && (!NILP (b->mark_active)
7917 ? Fmarker_position (b->mark)
7918 : Qnil));
7919 #endif
7920
7921 if (accurate_p)
7922 {
7923 b->clip_changed = 0;
7924 b->prevent_redisplay_optimizations_p = 0;
7925 w->current_matrix->buffer = b;
7926 w->current_matrix->begv = BUF_BEGV (b);
7927 w->current_matrix->zv = BUF_ZV (b);
7928 w->last_cursor = w->cursor;
7929 w->last_cursor_off_p = w->cursor_off_p;
7930 if (w == XWINDOW (selected_window))
7931 w->last_point = make_number (BUF_PT (b));
7932 else
7933 w->last_point = make_number (XMARKER (w->pointm)->charpos);
7934 }
7935 }
7936
7937 w->window_end_valid = w->buffer;
7938 w->update_mode_line = Qnil;
7939
7940 if (!NILP (w->vchild))
7941 mark_window_display_accurate (w->vchild, accurate_p);
7942 if (!NILP (w->hchild))
7943 mark_window_display_accurate (w->hchild, accurate_p);
7944 }
7945
7946 if (accurate_p)
7947 {
7948 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
7949 last_arrow_string = Voverlay_arrow_string;
7950 }
7951 else
7952 {
7953 /* Force a thorough redisplay the next time by setting
7954 last_arrow_position and last_arrow_string to t, which is
7955 unequal to any useful value of Voverlay_arrow_... */
7956 last_arrow_position = Qt;
7957 last_arrow_string = Qt;
7958 }
7959 }
7960
7961
7962 /* Return value in display table DP (Lisp_Char_Table *) for character
7963 C. Since a display table doesn't have any parent, we don't have to
7964 follow parent. Do not call this function directly but use the
7965 macro DISP_CHAR_VECTOR. */
7966
7967 Lisp_Object
7968 disp_char_vector (dp, c)
7969 struct Lisp_Char_Table *dp;
7970 int c;
7971 {
7972 int code[4], i;
7973 Lisp_Object val;
7974
7975 if (SINGLE_BYTE_CHAR_P (c))
7976 return (dp->contents[c]);
7977
7978 SPLIT_NON_ASCII_CHAR (c, code[0], code[1], code[2]);
7979 if (code[1] < 32)
7980 code[1] = -1;
7981 else if (code[2] < 32)
7982 code[2] = -1;
7983
7984 /* Here, the possible range of code[0] (== charset ID) is
7985 128..max_charset. Since the top level char table contains data
7986 for multibyte characters after 256th element, we must increment
7987 code[0] by 128 to get a correct index. */
7988 code[0] += 128;
7989 code[3] = -1; /* anchor */
7990
7991 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
7992 {
7993 val = dp->contents[code[i]];
7994 if (!SUB_CHAR_TABLE_P (val))
7995 return (NILP (val) ? dp->defalt : val);
7996 }
7997
7998 /* Here, val is a sub char table. We return the default value of
7999 it. */
8000 return (dp->defalt);
8001 }
8002
8003
8004 \f
8005 /***********************************************************************
8006 Window Redisplay
8007 ***********************************************************************/
8008
8009 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
8010
8011 static void
8012 redisplay_windows (window)
8013 Lisp_Object window;
8014 {
8015 while (!NILP (window))
8016 {
8017 struct window *w = XWINDOW (window);
8018
8019 if (!NILP (w->hchild))
8020 redisplay_windows (w->hchild);
8021 else if (!NILP (w->vchild))
8022 redisplay_windows (w->vchild);
8023 else
8024 redisplay_window (window, 0);
8025
8026 window = w->next;
8027 }
8028 }
8029
8030
8031 /* Set cursor position of W. PT is assumed to be displayed in ROW.
8032 DELTA is the number of bytes by which positions recorded in ROW
8033 differ from current buffer positions. */
8034
8035 void
8036 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
8037 struct window *w;
8038 struct glyph_row *row;
8039 struct glyph_matrix *matrix;
8040 int delta, delta_bytes, dy, dvpos;
8041 {
8042 struct glyph *glyph = row->glyphs[TEXT_AREA];
8043 struct glyph *end = glyph + row->used[TEXT_AREA];
8044 int x = row->x;
8045 int pt_old = PT - delta;
8046
8047 /* Skip over glyphs not having an object at the start of the row.
8048 These are special glyphs like truncation marks on terminal
8049 frames. */
8050 if (row->displays_text_p)
8051 while (glyph < end
8052 && INTEGERP (glyph->object)
8053 && glyph->charpos < 0)
8054 {
8055 x += glyph->pixel_width;
8056 ++glyph;
8057 }
8058
8059 while (glyph < end
8060 && !INTEGERP (glyph->object)
8061 && (!BUFFERP (glyph->object)
8062 || glyph->charpos < pt_old))
8063 {
8064 x += glyph->pixel_width;
8065 ++glyph;
8066 }
8067
8068 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
8069 w->cursor.x = x;
8070 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
8071 w->cursor.y = row->y + dy;
8072
8073 if (w == XWINDOW (selected_window))
8074 {
8075 if (!row->continued_p
8076 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
8077 && row->x == 0)
8078 {
8079 this_line_buffer = XBUFFER (w->buffer);
8080
8081 CHARPOS (this_line_start_pos)
8082 = MATRIX_ROW_START_CHARPOS (row) + delta;
8083 BYTEPOS (this_line_start_pos)
8084 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
8085
8086 CHARPOS (this_line_end_pos)
8087 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
8088 BYTEPOS (this_line_end_pos)
8089 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
8090
8091 this_line_y = w->cursor.y;
8092 this_line_pixel_height = row->height;
8093 this_line_vpos = w->cursor.vpos;
8094 this_line_start_x = row->x;
8095 }
8096 else
8097 CHARPOS (this_line_start_pos) = 0;
8098 }
8099 }
8100
8101
8102 /* Run window scroll functions, if any, for WINDOW with new window
8103 start STARTP. Sets the window start of WINDOW to that position.
8104
8105 We assume that the window's buffer is really current. */
8106
8107 static INLINE struct text_pos
8108 run_window_scroll_functions (window, startp)
8109 Lisp_Object window;
8110 struct text_pos startp;
8111 {
8112 struct window *w = XWINDOW (window);
8113 SET_MARKER_FROM_TEXT_POS (w->start, startp);
8114
8115 if (current_buffer != XBUFFER (w->buffer))
8116 abort ();
8117
8118 if (!NILP (Vwindow_scroll_functions))
8119 {
8120 run_hook_with_args_2 (Qwindow_scroll_functions, window,
8121 make_number (CHARPOS (startp)));
8122 SET_TEXT_POS_FROM_MARKER (startp, w->start);
8123 /* In case the hook functions switch buffers. */
8124 if (current_buffer != XBUFFER (w->buffer))
8125 set_buffer_internal_1 (XBUFFER (w->buffer));
8126 }
8127
8128 return startp;
8129 }
8130
8131
8132 /* Modify the desired matrix of window W and W->vscroll so that the
8133 line containing the cursor is fully visible. */
8134
8135 static void
8136 make_cursor_line_fully_visible (w)
8137 struct window *w;
8138 {
8139 struct glyph_matrix *matrix;
8140 struct glyph_row *row;
8141 int header_line_height;
8142
8143 /* It's not always possible to find the cursor, e.g, when a window
8144 is full of overlay strings. Don't do anything in that case. */
8145 if (w->cursor.vpos < 0)
8146 return;
8147
8148 matrix = w->desired_matrix;
8149 row = MATRIX_ROW (matrix, w->cursor.vpos);
8150
8151 /* If row->y == top y of window display area, the window isn't tall
8152 enough to display a single line. There is nothing we can do
8153 about it. */
8154 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
8155 if (row->y == header_line_height)
8156 return;
8157
8158 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
8159 {
8160 int dy = row->height - row->visible_height;
8161 w->vscroll = 0;
8162 w->cursor.y += dy;
8163 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
8164 }
8165 else if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row))
8166 {
8167 int dy = - (row->height - row->visible_height);
8168 w->vscroll = dy;
8169 w->cursor.y += dy;
8170 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
8171 }
8172
8173 /* When we change the cursor y-position of the selected window,
8174 change this_line_y as well so that the display optimization for
8175 the cursor line of the selected window in redisplay_internal uses
8176 the correct y-position. */
8177 if (w == XWINDOW (selected_window))
8178 this_line_y = w->cursor.y;
8179 }
8180
8181
8182 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
8183 non-zero means only WINDOW is redisplayed in redisplay_internal.
8184 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
8185 in redisplay_window to bring a partially visible line into view in
8186 the case that only the cursor has moved.
8187
8188 Value is
8189
8190 1 if scrolling succeeded
8191
8192 0 if scrolling didn't find point.
8193
8194 -1 if new fonts have been loaded so that we must interrupt
8195 redisplay, adjust glyph matrices, and try again. */
8196
8197 static int
8198 try_scrolling (window, just_this_one_p, scroll_conservatively,
8199 scroll_step, temp_scroll_step)
8200 Lisp_Object window;
8201 int just_this_one_p;
8202 int scroll_conservatively, scroll_step;
8203 int temp_scroll_step;
8204 {
8205 struct window *w = XWINDOW (window);
8206 struct frame *f = XFRAME (w->frame);
8207 struct text_pos scroll_margin_pos;
8208 struct text_pos pos;
8209 struct text_pos startp;
8210 struct it it;
8211 Lisp_Object window_end;
8212 int this_scroll_margin;
8213 int dy = 0;
8214 int scroll_max;
8215 int line_height, rc;
8216 int amount_to_scroll = 0;
8217 Lisp_Object aggressive;
8218 int height;
8219
8220 #if GLYPH_DEBUG
8221 debug_method_add (w, "try_scrolling");
8222 #endif
8223
8224 SET_TEXT_POS_FROM_MARKER (startp, w->start);
8225
8226 /* Compute scroll margin height in pixels. We scroll when point is
8227 within this distance from the top or bottom of the window. */
8228 if (scroll_margin > 0)
8229 {
8230 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
8231 this_scroll_margin *= CANON_Y_UNIT (f);
8232 }
8233 else
8234 this_scroll_margin = 0;
8235
8236 /* Compute how much we should try to scroll maximally to bring point
8237 into view. */
8238 if (scroll_step)
8239 scroll_max = scroll_step;
8240 else if (scroll_conservatively)
8241 scroll_max = scroll_conservatively;
8242 else if (temp_scroll_step)
8243 scroll_max = temp_scroll_step;
8244 else if (NUMBERP (current_buffer->scroll_down_aggressively)
8245 || NUMBERP (current_buffer->scroll_up_aggressively))
8246 /* We're trying to scroll because of aggressive scrolling
8247 but no scroll_step is set. Choose an arbitrary one. Maybe
8248 there should be a variable for this. */
8249 scroll_max = 10;
8250 else
8251 scroll_max = 0;
8252 scroll_max *= CANON_Y_UNIT (f);
8253
8254 /* Decide whether we have to scroll down. Start at the window end
8255 and move this_scroll_margin up to find the position of the scroll
8256 margin. */
8257 window_end = Fwindow_end (window, Qt);
8258 CHARPOS (scroll_margin_pos) = XINT (window_end);
8259 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
8260 if (this_scroll_margin)
8261 {
8262 start_display (&it, w, scroll_margin_pos);
8263 move_it_vertically (&it, - this_scroll_margin);
8264 scroll_margin_pos = it.current.pos;
8265 }
8266
8267 if (PT >= CHARPOS (scroll_margin_pos))
8268 {
8269 int y0;
8270
8271 /* Point is in the scroll margin at the bottom of the window, or
8272 below. Compute a new window start that makes point visible. */
8273
8274 /* Compute the distance from the scroll margin to PT.
8275 Give up if the distance is greater than scroll_max. */
8276 start_display (&it, w, scroll_margin_pos);
8277 y0 = it.current_y;
8278 move_it_to (&it, PT, 0, it.last_visible_y, -1,
8279 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
8280 line_height = (it.max_ascent + it.max_descent
8281 ? it.max_ascent + it.max_descent
8282 : last_height);
8283 dy = it.current_y + line_height - y0;
8284 if (dy > scroll_max)
8285 return 0;
8286
8287 /* Move the window start down. If scrolling conservatively,
8288 move it just enough down to make point visible. If
8289 scroll_step is set, move it down by scroll_step. */
8290 start_display (&it, w, startp);
8291
8292 if (scroll_conservatively)
8293 amount_to_scroll = dy;
8294 else if (scroll_step || temp_scroll_step)
8295 amount_to_scroll = scroll_max;
8296 else
8297 {
8298 aggressive = current_buffer->scroll_down_aggressively;
8299 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
8300 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
8301 if (NUMBERP (aggressive))
8302 amount_to_scroll = XFLOATINT (aggressive) * height;
8303 }
8304
8305 if (amount_to_scroll <= 0)
8306 return 0;
8307
8308 move_it_vertically (&it, amount_to_scroll);
8309 startp = it.current.pos;
8310 }
8311 else
8312 {
8313 /* See if point is inside the scroll margin at the top of the
8314 window. */
8315 scroll_margin_pos = startp;
8316 if (this_scroll_margin)
8317 {
8318 start_display (&it, w, startp);
8319 move_it_vertically (&it, this_scroll_margin);
8320 scroll_margin_pos = it.current.pos;
8321 }
8322
8323 if (PT < CHARPOS (scroll_margin_pos))
8324 {
8325 /* Point is in the scroll margin at the top of the window or
8326 above what is displayed in the window. */
8327 int y0;
8328
8329 /* Compute the vertical distance from PT to the scroll
8330 margin position. Give up if distance is greater than
8331 scroll_max. */
8332 SET_TEXT_POS (pos, PT, PT_BYTE);
8333 start_display (&it, w, pos);
8334 y0 = it.current_y;
8335 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
8336 it.last_visible_y, -1,
8337 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
8338 dy = it.current_y - y0;
8339 if (dy > scroll_max)
8340 return 0;
8341
8342 /* Compute new window start. */
8343 start_display (&it, w, startp);
8344
8345 if (scroll_conservatively)
8346 amount_to_scroll = dy;
8347 else if (scroll_step || temp_scroll_step)
8348 amount_to_scroll = scroll_max;
8349 else
8350 {
8351 aggressive = current_buffer->scroll_up_aggressively;
8352 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
8353 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
8354 if (NUMBERP (aggressive))
8355 amount_to_scroll = XFLOATINT (aggressive) * height;
8356 }
8357
8358 if (amount_to_scroll <= 0)
8359 return 0;
8360
8361 move_it_vertically (&it, - amount_to_scroll);
8362 startp = it.current.pos;
8363 }
8364 }
8365
8366 /* Run window scroll functions. */
8367 startp = run_window_scroll_functions (window, startp);
8368
8369 /* Display the window. Give up if new fonts are loaded, or if point
8370 doesn't appear. */
8371 if (!try_window (window, startp))
8372 rc = -1;
8373 else if (w->cursor.vpos < 0)
8374 {
8375 clear_glyph_matrix (w->desired_matrix);
8376 rc = 0;
8377 }
8378 else
8379 {
8380 /* Maybe forget recorded base line for line number display. */
8381 if (!just_this_one_p
8382 || current_buffer->clip_changed
8383 || BEG_UNCHANGED < CHARPOS (startp))
8384 w->base_line_number = Qnil;
8385
8386 /* If cursor ends up on a partially visible line, shift display
8387 lines up or down. */
8388 make_cursor_line_fully_visible (w);
8389 rc = 1;
8390 }
8391
8392 return rc;
8393 }
8394
8395
8396 /* Compute a suitable window start for window W if display of W starts
8397 on a continuation line. Value is non-zero if a new window start
8398 was computed.
8399
8400 The new window start will be computed, based on W's width, starting
8401 from the start of the continued line. It is the start of the
8402 screen line with the minimum distance from the old start W->start. */
8403
8404 static int
8405 compute_window_start_on_continuation_line (w)
8406 struct window *w;
8407 {
8408 struct text_pos pos, start_pos;
8409 int window_start_changed_p = 0;
8410
8411 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
8412
8413 /* If window start is on a continuation line... Window start may be
8414 < BEGV in case there's invisible text at the start of the
8415 buffer (M-x rmail, for example). */
8416 if (CHARPOS (start_pos) > BEGV
8417 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
8418 {
8419 struct it it;
8420 struct glyph_row *row;
8421
8422 /* Handle the case that the window start is out of range. */
8423 if (CHARPOS (start_pos) < BEGV)
8424 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
8425 else if (CHARPOS (start_pos) > ZV)
8426 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
8427
8428 /* Find the start of the continued line. This should be fast
8429 because scan_buffer is fast (newline cache). */
8430 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
8431 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
8432 row, DEFAULT_FACE_ID);
8433 reseat_at_previous_visible_line_start (&it);
8434
8435 /* If the line start is "too far" away from the window start,
8436 say it takes too much time to compute a new window start. */
8437 if (CHARPOS (start_pos) - IT_CHARPOS (it)
8438 < XFASTINT (w->height) * XFASTINT (w->width))
8439 {
8440 int min_distance, distance;
8441
8442 /* Move forward by display lines to find the new window
8443 start. If window width was enlarged, the new start can
8444 be expected to be > the old start. If window width was
8445 decreased, the new window start will be < the old start.
8446 So, we're looking for the display line start with the
8447 minimum distance from the old window start. */
8448 pos = it.current.pos;
8449 min_distance = INFINITY;
8450 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
8451 distance < min_distance)
8452 {
8453 min_distance = distance;
8454 pos = it.current.pos;
8455 move_it_by_lines (&it, 1, 0);
8456 }
8457
8458 /* Set the window start there. */
8459 SET_MARKER_FROM_TEXT_POS (w->start, pos);
8460 window_start_changed_p = 1;
8461 }
8462 }
8463
8464 return window_start_changed_p;
8465 }
8466
8467
8468 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
8469 selected_window is redisplayed. */
8470
8471 static void
8472 redisplay_window (window, just_this_one_p)
8473 Lisp_Object window;
8474 int just_this_one_p;
8475 {
8476 struct window *w = XWINDOW (window);
8477 struct frame *f = XFRAME (w->frame);
8478 struct buffer *buffer = XBUFFER (w->buffer);
8479 struct buffer *old = current_buffer;
8480 struct text_pos lpoint, opoint, startp;
8481 int update_mode_line;
8482 int tem;
8483 struct it it;
8484 /* Record it now because it's overwritten. */
8485 int current_matrix_up_to_date_p = 0;
8486 int really_switched_buffer = 0;
8487 int temp_scroll_step = 0;
8488 int count = specpdl_ptr - specpdl;
8489
8490 SET_TEXT_POS (lpoint, PT, PT_BYTE);
8491 opoint = lpoint;
8492
8493 /* W must be a leaf window here. */
8494 xassert (!NILP (w->buffer));
8495 #if GLYPH_DEBUG
8496 *w->desired_matrix->method = 0;
8497 #endif
8498
8499 specbind (Qinhibit_point_motion_hooks, Qt);
8500
8501 reconsider_clip_changes (w, buffer);
8502
8503 /* Has the mode line to be updated? */
8504 update_mode_line = (!NILP (w->update_mode_line)
8505 || update_mode_lines
8506 || buffer->clip_changed);
8507
8508 if (MINI_WINDOW_P (w))
8509 {
8510 if (w == XWINDOW (echo_area_window)
8511 && !NILP (echo_area_buffer[0]))
8512 {
8513 if (update_mode_line)
8514 /* We may have to update a tty frame's menu bar or a
8515 tool-bar. Example `M-x C-h C-h C-g'. */
8516 goto finish_menu_bars;
8517 else
8518 /* We've already displayed the echo area glyphs in this window. */
8519 goto finish_scroll_bars;
8520 }
8521 else if (w != XWINDOW (minibuf_window))
8522 {
8523 /* W is a mini-buffer window, but it's not the currently
8524 active one, so clear it. */
8525 int yb = window_text_bottom_y (w);
8526 struct glyph_row *row;
8527 int y;
8528
8529 for (y = 0, row = w->desired_matrix->rows;
8530 y < yb;
8531 y += row->height, ++row)
8532 blank_row (w, row, y);
8533 goto finish_scroll_bars;
8534 }
8535 }
8536
8537 /* Otherwise set up data on this window; select its buffer and point
8538 value. */
8539 if (update_mode_line)
8540 {
8541 /* Really select the buffer, for the sake of buffer-local
8542 variables. */
8543 set_buffer_internal_1 (XBUFFER (w->buffer));
8544 really_switched_buffer = 1;
8545 }
8546 else
8547 set_buffer_temp (XBUFFER (w->buffer));
8548 SET_TEXT_POS (opoint, PT, PT_BYTE);
8549
8550 current_matrix_up_to_date_p
8551 = (!NILP (w->window_end_valid)
8552 && !current_buffer->clip_changed
8553 && XFASTINT (w->last_modified) >= MODIFF
8554 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
8555
8556 /* When windows_or_buffers_changed is non-zero, we can't rely on
8557 the window end being valid, so set it to nil there. */
8558 if (windows_or_buffers_changed)
8559 {
8560 /* If window starts on a continuation line, maybe adjust the
8561 window start in case the window's width changed. */
8562 if (XMARKER (w->start)->buffer == current_buffer)
8563 compute_window_start_on_continuation_line (w);
8564
8565 w->window_end_valid = Qnil;
8566 }
8567
8568 /* Some sanity checks. */
8569 CHECK_WINDOW_END (w);
8570 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
8571 abort ();
8572 if (BYTEPOS (opoint) < CHARPOS (opoint))
8573 abort ();
8574
8575 /* If %c is in mode line, update it if needed. */
8576 if (!NILP (w->column_number_displayed)
8577 /* This alternative quickly identifies a common case
8578 where no change is needed. */
8579 && !(PT == XFASTINT (w->last_point)
8580 && XFASTINT (w->last_modified) >= MODIFF
8581 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
8582 && XFASTINT (w->column_number_displayed) != current_column ())
8583 update_mode_line = 1;
8584
8585 /* Count number of windows showing the selected buffer. An indirect
8586 buffer counts as its base buffer. */
8587 if (!just_this_one_p)
8588 {
8589 struct buffer *current_base, *window_base;
8590 current_base = current_buffer;
8591 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
8592 if (current_base->base_buffer)
8593 current_base = current_base->base_buffer;
8594 if (window_base->base_buffer)
8595 window_base = window_base->base_buffer;
8596 if (current_base == window_base)
8597 buffer_shared++;
8598 }
8599
8600 /* Point refers normally to the selected window. For any other
8601 window, set up appropriate value. */
8602 if (!EQ (window, selected_window))
8603 {
8604 int new_pt = XMARKER (w->pointm)->charpos;
8605 int new_pt_byte = marker_byte_position (w->pointm);
8606 if (new_pt < BEGV)
8607 {
8608 new_pt = BEGV;
8609 new_pt_byte = BEGV_BYTE;
8610 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
8611 }
8612 else if (new_pt > (ZV - 1))
8613 {
8614 new_pt = ZV;
8615 new_pt_byte = ZV_BYTE;
8616 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
8617 }
8618
8619 /* We don't use SET_PT so that the point-motion hooks don't run. */
8620 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
8621 }
8622
8623 /* If any of the character widths specified in the display table
8624 have changed, invalidate the width run cache. It's true that
8625 this may be a bit late to catch such changes, but the rest of
8626 redisplay goes (non-fatally) haywire when the display table is
8627 changed, so why should we worry about doing any better? */
8628 if (current_buffer->width_run_cache)
8629 {
8630 struct Lisp_Char_Table *disptab = buffer_display_table ();
8631
8632 if (! disptab_matches_widthtab (disptab,
8633 XVECTOR (current_buffer->width_table)))
8634 {
8635 invalidate_region_cache (current_buffer,
8636 current_buffer->width_run_cache,
8637 BEG, Z);
8638 recompute_width_table (current_buffer, disptab);
8639 }
8640 }
8641
8642 /* If window-start is screwed up, choose a new one. */
8643 if (XMARKER (w->start)->buffer != current_buffer)
8644 goto recenter;
8645
8646 SET_TEXT_POS_FROM_MARKER (startp, w->start);
8647
8648 /* If someone specified a new starting point but did not insist,
8649 check whether it can be used. */
8650 if (!NILP (w->optional_new_start)
8651 && CHARPOS (startp) >= BEGV
8652 && CHARPOS (startp) <= ZV)
8653 {
8654 w->optional_new_start = Qnil;
8655 /* This takes a mini-buffer prompt into account. */
8656 start_display (&it, w, startp);
8657 move_it_to (&it, PT, 0, it.last_visible_y, -1,
8658 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
8659 if (IT_CHARPOS (it) == PT)
8660 w->force_start = Qt;
8661 }
8662
8663 /* Handle case where place to start displaying has been specified,
8664 unless the specified location is outside the accessible range. */
8665 if (!NILP (w->force_start)
8666 || w->frozen_window_start_p)
8667 {
8668 w->force_start = Qnil;
8669 w->vscroll = 0;
8670 w->window_end_valid = Qnil;
8671
8672 /* Forget any recorded base line for line number display. */
8673 if (!current_matrix_up_to_date_p
8674 || current_buffer->clip_changed)
8675 w->base_line_number = Qnil;
8676
8677 /* Redisplay the mode line. Select the buffer properly for that.
8678 Also, run the hook window-scroll-functions
8679 because we have scrolled. */
8680 /* Note, we do this after clearing force_start because
8681 if there's an error, it is better to forget about force_start
8682 than to get into an infinite loop calling the hook functions
8683 and having them get more errors. */
8684 if (!update_mode_line
8685 || ! NILP (Vwindow_scroll_functions))
8686 {
8687 if (!really_switched_buffer)
8688 {
8689 set_buffer_temp (old);
8690 set_buffer_internal_1 (XBUFFER (w->buffer));
8691 really_switched_buffer = 1;
8692 }
8693
8694 update_mode_line = 1;
8695 w->update_mode_line = Qt;
8696 startp = run_window_scroll_functions (window, startp);
8697 }
8698
8699 XSETFASTINT (w->last_modified, 0);
8700 XSETFASTINT (w->last_overlay_modified, 0);
8701 if (CHARPOS (startp) < BEGV)
8702 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
8703 else if (CHARPOS (startp) > ZV)
8704 SET_TEXT_POS (startp, ZV, ZV_BYTE);
8705
8706 /* Redisplay, then check if cursor has been set during the
8707 redisplay. Give up if new fonts were loaded. */
8708 if (!try_window (window, startp))
8709 {
8710 w->force_start = Qt;
8711 clear_glyph_matrix (w->desired_matrix);
8712 goto restore_buffers;
8713 }
8714
8715 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
8716 {
8717 /* If point does not appear, or on a line that is not fully
8718 visible, move point so it does appear. The desired
8719 matrix has been built above, so we can use it. */
8720 int height = window_box_height (w) / 2;
8721 struct glyph_row *row = MATRIX_ROW (w->desired_matrix, 0);
8722
8723 while (row->y < height)
8724 ++row;
8725
8726 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
8727 MATRIX_ROW_START_BYTEPOS (row));
8728
8729 if (w != XWINDOW (selected_window))
8730 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
8731 else if (current_buffer == old)
8732 SET_TEXT_POS (lpoint, PT, PT_BYTE);
8733
8734 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
8735
8736 /* If we are highlighting the region, then we just changed
8737 the region, so redisplay to show it. */
8738 if (!NILP (Vtransient_mark_mode)
8739 && !NILP (current_buffer->mark_active))
8740 {
8741 clear_glyph_matrix (w->desired_matrix);
8742 if (!try_window (window, startp))
8743 goto restore_buffers;
8744 }
8745 }
8746
8747 make_cursor_line_fully_visible (w);
8748 #if GLYPH_DEBUG
8749 debug_method_add (w, "forced window start");
8750 #endif
8751 goto done;
8752 }
8753
8754 /* Handle case where text has not changed, only point, and it has
8755 not moved off the frame. */
8756 if (current_matrix_up_to_date_p
8757 /* Point may be in this window. */
8758 && PT >= CHARPOS (startp)
8759 /* If we don't check this, we are called to move the cursor in a
8760 horizontally split window with a current matrix that doesn't
8761 fit the display. */
8762 && !windows_or_buffers_changed
8763 /* Selective display hasn't changed. */
8764 && !current_buffer->clip_changed
8765 /* If force-mode-line-update was called, really redisplay;
8766 that's how redisplay is forced after e.g. changing
8767 buffer-invisibility-spec. */
8768 && NILP (w->update_mode_line)
8769 /* Can't use this case if highlighting a region. When a
8770 region exists, cursor movement has to do more than just
8771 set the cursor. */
8772 && !(!NILP (Vtransient_mark_mode)
8773 && !NILP (current_buffer->mark_active))
8774 && NILP (w->region_showing)
8775 && NILP (Vshow_trailing_whitespace)
8776 /* Right after splitting windows, last_point may be nil. */
8777 && INTEGERP (w->last_point)
8778 /* This code is not used for mini-buffer for the sake of the case
8779 of redisplaying to replace an echo area message; since in
8780 that case the mini-buffer contents per se are usually
8781 unchanged. This code is of no real use in the mini-buffer
8782 since the handling of this_line_start_pos, etc., in redisplay
8783 handles the same cases. */
8784 && !EQ (window, minibuf_window)
8785 /* When splitting windows or for new windows, it happens that
8786 redisplay is called with a nil window_end_vpos or one being
8787 larger than the window. This should really be fixed in
8788 window.c. I don't have this on my list, now, so we do
8789 approximately the same as the old redisplay code. --gerd. */
8790 && INTEGERP (w->window_end_vpos)
8791 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
8792 && (FRAME_WINDOW_P (f)
8793 || !MARKERP (Voverlay_arrow_position)
8794 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
8795 {
8796 int this_scroll_margin;
8797 struct glyph_row *row;
8798 int scroll_p;
8799
8800 #if GLYPH_DEBUG
8801 debug_method_add (w, "cursor movement");
8802 #endif
8803
8804 /* Scroll if point within this distance from the top or bottom
8805 of the window. This is a pixel value. */
8806 this_scroll_margin = max (0, scroll_margin);
8807 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
8808 this_scroll_margin *= CANON_Y_UNIT (f);
8809
8810 /* Start with the row the cursor was displayed during the last
8811 not paused redisplay. Give up if that row is not valid. */
8812 if (w->last_cursor.vpos >= w->current_matrix->nrows)
8813 goto try_to_scroll;
8814 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
8815 if (row->mode_line_p)
8816 ++row;
8817 if (!row->enabled_p)
8818 goto try_to_scroll;
8819
8820 scroll_p = 0;
8821 if (PT > XFASTINT (w->last_point))
8822 {
8823 /* Point has moved forward. */
8824 int last_y = window_text_bottom_y (w) - this_scroll_margin;
8825
8826 while ((MATRIX_ROW_END_CHARPOS (row) < PT
8827 /* The end position of a row equals the start
8828 position of the next row. If PT is there, we
8829 would rather display it in the next line, except
8830 when this line ends in ZV. */
8831 || (MATRIX_ROW_END_CHARPOS (row) == PT
8832 && (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
8833 || !row->ends_at_zv_p)))
8834 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
8835 {
8836 xassert (row->enabled_p);
8837 ++row;
8838 }
8839
8840 /* If within the scroll margin, scroll. Note that
8841 MATRIX_ROW_BOTTOM_Y gives the pixel position at which the
8842 next line would be drawn, and that this_scroll_margin can
8843 be zero. */
8844 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
8845 || PT > MATRIX_ROW_END_CHARPOS (row)
8846 /* Line is completely visible last line in window and PT
8847 is to be set in the next line. */
8848 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
8849 && PT == MATRIX_ROW_END_CHARPOS (row)
8850 && !row->ends_at_zv_p
8851 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
8852 scroll_p = 1;
8853 }
8854 else if (PT < XFASTINT (w->last_point))
8855 {
8856 /* Cursor has to be moved backward. Note that PT >=
8857 CHARPOS (startp) because of the outer if-statement. */
8858 while (!row->mode_line_p
8859 && (MATRIX_ROW_START_CHARPOS (row) > PT
8860 || (MATRIX_ROW_START_CHARPOS (row) == PT
8861 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
8862 && (row->y > this_scroll_margin
8863 || CHARPOS (startp) == BEGV))
8864 {
8865 xassert (row->enabled_p);
8866 --row;
8867 }
8868
8869 /* Consider the following case: Window starts at BEGV, there
8870 is invisible, intangible text at BEGV, so that display
8871 starts at some point START > BEGV. It can happen that
8872 we are called with PT somewhere between BEGV and START.
8873 Try to handle that case. */
8874 if (row < w->current_matrix->rows
8875 || row->mode_line_p)
8876 {
8877 row = w->current_matrix->rows;
8878 if (row->mode_line_p)
8879 ++row;
8880 }
8881
8882 /* Due to newlines in overlay strings, we may have to skip
8883 forward over overlay strings. */
8884 while (MATRIX_ROW_END_CHARPOS (row) == PT
8885 && MATRIX_ROW_ENDS_IN_OVERLAY_STRING_P (row)
8886 && !row->ends_at_zv_p)
8887 ++row;
8888
8889 /* If within the scroll margin, scroll. */
8890 if (row->y < this_scroll_margin
8891 && CHARPOS (startp) != BEGV)
8892 scroll_p = 1;
8893 }
8894
8895 /* if PT is not in the glyph row, give up. */
8896 if (PT < MATRIX_ROW_START_CHARPOS (row)
8897 || PT > MATRIX_ROW_END_CHARPOS (row))
8898 goto try_to_scroll;
8899
8900 /* If we end up in a partially visible line, let's make it fully
8901 visible. This can be done most easily by using the existing
8902 scrolling code. */
8903 if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
8904 {
8905 temp_scroll_step = 1;
8906 goto try_to_scroll;
8907 }
8908 else if (scroll_p)
8909 goto try_to_scroll;
8910
8911 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
8912 goto done;
8913 }
8914
8915 /* If current starting point was originally the beginning of a line
8916 but no longer is, find a new starting point. */
8917 else if (!NILP (w->start_at_line_beg)
8918 && !(CHARPOS (startp) <= BEGV
8919 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
8920 {
8921 #if GLYPH_DEBUG
8922 debug_method_add (w, "recenter 1");
8923 #endif
8924 goto recenter;
8925 }
8926
8927 /* Try scrolling with try_window_id. */
8928 else if (/* Windows and buffers haven't changed. */
8929 !windows_or_buffers_changed
8930 /* Window must be either use window-based redisplay or
8931 be full width. */
8932 && (FRAME_WINDOW_P (f)
8933 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)))
8934 && !MINI_WINDOW_P (w)
8935 /* Point is not known NOT to appear in window. */
8936 && PT >= CHARPOS (startp)
8937 && XFASTINT (w->last_modified)
8938 /* Window is not hscrolled. */
8939 && XFASTINT (w->hscroll) == 0
8940 /* Selective display has not changed. */
8941 && !current_buffer->clip_changed
8942 /* Current matrix is up to date. */
8943 && !NILP (w->window_end_valid)
8944 /* Can't use this case if highlighting a region because
8945 a cursor movement will do more than just set the cursor. */
8946 && !(!NILP (Vtransient_mark_mode)
8947 && !NILP (current_buffer->mark_active))
8948 && NILP (w->region_showing)
8949 && NILP (Vshow_trailing_whitespace)
8950 /* Overlay arrow position and string not changed. */
8951 && EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
8952 && EQ (last_arrow_string, Voverlay_arrow_string)
8953 /* Value is > 0 if update has been done, it is -1 if we
8954 know that the same window start will not work. It is 0
8955 if unsuccessful for some other reason. */
8956 && (tem = try_window_id (w)) != 0)
8957 {
8958 #if GLYPH_DEBUG
8959 debug_method_add (w, "try_window_id");
8960 #endif
8961
8962 if (fonts_changed_p)
8963 goto restore_buffers;
8964 if (tem > 0)
8965 goto done;
8966 /* Otherwise try_window_id has returned -1 which means that we
8967 don't want the alternative below this comment to execute. */
8968 }
8969 else if (CHARPOS (startp) >= BEGV
8970 && CHARPOS (startp) <= ZV
8971 && PT >= CHARPOS (startp)
8972 && (CHARPOS (startp) < ZV
8973 /* Avoid starting at end of buffer. */
8974 || CHARPOS (startp) == BEGV
8975 || (XFASTINT (w->last_modified) >= MODIFF
8976 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
8977 {
8978 #if GLYPH_DEBUG
8979 debug_method_add (w, "same window start");
8980 #endif
8981
8982 /* Try to redisplay starting at same place as before.
8983 If point has not moved off frame, accept the results. */
8984 if (!current_matrix_up_to_date_p
8985 /* Don't use try_window_reusing_current_matrix in this case
8986 because a window scroll function can have changed the
8987 buffer. */
8988 || !NILP (Vwindow_scroll_functions)
8989 || MINI_WINDOW_P (w)
8990 || !try_window_reusing_current_matrix (w))
8991 {
8992 IF_DEBUG (debug_method_add (w, "1"));
8993 try_window (window, startp);
8994 }
8995
8996 if (fonts_changed_p)
8997 goto restore_buffers;
8998
8999 if (w->cursor.vpos >= 0)
9000 {
9001 if (!just_this_one_p
9002 || current_buffer->clip_changed
9003 || BEG_UNCHANGED < CHARPOS (startp))
9004 /* Forget any recorded base line for line number display. */
9005 w->base_line_number = Qnil;
9006
9007 make_cursor_line_fully_visible (w);
9008 goto done;
9009 }
9010 else
9011 clear_glyph_matrix (w->desired_matrix);
9012 }
9013
9014 try_to_scroll:
9015
9016 XSETFASTINT (w->last_modified, 0);
9017 XSETFASTINT (w->last_overlay_modified, 0);
9018
9019 /* Redisplay the mode line. Select the buffer properly for that. */
9020 if (!update_mode_line)
9021 {
9022 if (!really_switched_buffer)
9023 {
9024 set_buffer_temp (old);
9025 set_buffer_internal_1 (XBUFFER (w->buffer));
9026 really_switched_buffer = 1;
9027 }
9028 update_mode_line = 1;
9029 w->update_mode_line = Qt;
9030 }
9031
9032 /* Try to scroll by specified few lines. */
9033 if ((scroll_conservatively
9034 || scroll_step
9035 || temp_scroll_step
9036 || NUMBERP (current_buffer->scroll_up_aggressively)
9037 || NUMBERP (current_buffer->scroll_down_aggressively))
9038 && !current_buffer->clip_changed
9039 && CHARPOS (startp) >= BEGV
9040 && CHARPOS (startp) <= ZV)
9041 {
9042 /* The function returns -1 if new fonts were loaded, 1 if
9043 successful, 0 if not successful. */
9044 int rc = try_scrolling (window, just_this_one_p,
9045 scroll_conservatively,
9046 scroll_step,
9047 temp_scroll_step);
9048 if (rc > 0)
9049 goto done;
9050 else if (rc < 0)
9051 goto restore_buffers;
9052 }
9053
9054 /* Finally, just choose place to start which centers point */
9055
9056 recenter:
9057
9058 #if GLYPH_DEBUG
9059 debug_method_add (w, "recenter");
9060 #endif
9061
9062 /* w->vscroll = 0; */
9063
9064 /* Forget any previously recorded base line for line number display. */
9065 if (!current_matrix_up_to_date_p
9066 || current_buffer->clip_changed)
9067 w->base_line_number = Qnil;
9068
9069 /* Move backward half the height of the window. */
9070 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
9071 it.current_y = it.last_visible_y;
9072 move_it_vertically_backward (&it, it.last_visible_y / 2);
9073 xassert (IT_CHARPOS (it) >= BEGV);
9074
9075 /* The function move_it_vertically_backward may move over more
9076 than the specified y-distance. If it->w is small, e.g. a
9077 mini-buffer window, we may end up in front of the window's
9078 display area. Start displaying at the start of the line
9079 containing PT in this case. */
9080 if (it.current_y <= 0)
9081 {
9082 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
9083 move_it_vertically (&it, 0);
9084 xassert (IT_CHARPOS (it) <= PT);
9085 it.current_y = 0;
9086 }
9087
9088 it.current_x = it.hpos = 0;
9089
9090 /* Set startp here explicitly in case that helps avoid an infinite loop
9091 in case the window-scroll-functions functions get errors. */
9092 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
9093
9094 /* Run scroll hooks. */
9095 startp = run_window_scroll_functions (window, it.current.pos);
9096
9097 /* Redisplay the window. */
9098 if (!current_matrix_up_to_date_p
9099 || windows_or_buffers_changed
9100 /* Don't use try_window_reusing_current_matrix in this case
9101 because it can have changed the buffer. */
9102 || !NILP (Vwindow_scroll_functions)
9103 || !just_this_one_p
9104 || MINI_WINDOW_P (w)
9105 || !try_window_reusing_current_matrix (w))
9106 try_window (window, startp);
9107
9108 /* If new fonts have been loaded (due to fontsets), give up. We
9109 have to start a new redisplay since we need to re-adjust glyph
9110 matrices. */
9111 if (fonts_changed_p)
9112 goto restore_buffers;
9113
9114 /* If cursor did not appear assume that the middle of the window is
9115 in the first line of the window. Do it again with the next line.
9116 (Imagine a window of height 100, displaying two lines of height
9117 60. Moving back 50 from it->last_visible_y will end in the first
9118 line.) */
9119 if (w->cursor.vpos < 0)
9120 {
9121 if (!NILP (w->window_end_valid)
9122 && PT >= Z - XFASTINT (w->window_end_pos))
9123 {
9124 clear_glyph_matrix (w->desired_matrix);
9125 move_it_by_lines (&it, 1, 0);
9126 try_window (window, it.current.pos);
9127 }
9128 else if (PT < IT_CHARPOS (it))
9129 {
9130 clear_glyph_matrix (w->desired_matrix);
9131 move_it_by_lines (&it, -1, 0);
9132 try_window (window, it.current.pos);
9133 }
9134 else
9135 {
9136 /* Not much we can do about it. */
9137 }
9138 }
9139
9140 /* Consider the following case: Window starts at BEGV, there is
9141 invisible, intangible text at BEGV, so that display starts at
9142 some point START > BEGV. It can happen that we are called with
9143 PT somewhere between BEGV and START. Try to handle that case. */
9144 if (w->cursor.vpos < 0)
9145 {
9146 struct glyph_row *row = w->current_matrix->rows;
9147 if (row->mode_line_p)
9148 ++row;
9149 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9150 }
9151
9152 make_cursor_line_fully_visible (w);
9153
9154 done:
9155
9156 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9157 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
9158 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
9159 ? Qt : Qnil);
9160
9161 /* Display the mode line, if we must. */
9162 if ((update_mode_line
9163 /* If window not full width, must redo its mode line
9164 if (a) the window to its side is being redone and
9165 (b) we do a frame-based redisplay. This is a consequence
9166 of how inverted lines are drawn in frame-based redisplay. */
9167 || (!just_this_one_p
9168 && !FRAME_WINDOW_P (f)
9169 && !WINDOW_FULL_WIDTH_P (w))
9170 /* Line number to display. */
9171 || INTEGERP (w->base_line_pos)
9172 /* Column number is displayed and different from the one displayed. */
9173 || (!NILP (w->column_number_displayed)
9174 && XFASTINT (w->column_number_displayed) != current_column ()))
9175 /* This means that the window has a mode line. */
9176 && (WINDOW_WANTS_MODELINE_P (w)
9177 || WINDOW_WANTS_HEADER_LINE_P (w)))
9178 {
9179 display_mode_lines (w);
9180
9181 /* If mode line height has changed, arrange for a thorough
9182 immediate redisplay using the correct mode line height. */
9183 if (WINDOW_WANTS_MODELINE_P (w)
9184 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
9185 {
9186 fonts_changed_p = 1;
9187 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
9188 = DESIRED_MODE_LINE_HEIGHT (w);
9189 }
9190
9191 /* If top line height has changed, arrange for a thorough
9192 immediate redisplay using the correct mode line height. */
9193 if (WINDOW_WANTS_HEADER_LINE_P (w)
9194 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
9195 {
9196 fonts_changed_p = 1;
9197 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
9198 = DESIRED_HEADER_LINE_HEIGHT (w);
9199 }
9200
9201 if (fonts_changed_p)
9202 goto restore_buffers;
9203 }
9204
9205 if (!line_number_displayed
9206 && !BUFFERP (w->base_line_pos))
9207 {
9208 w->base_line_pos = Qnil;
9209 w->base_line_number = Qnil;
9210 }
9211
9212 finish_menu_bars:
9213
9214 /* When we reach a frame's selected window, redo the frame's menu bar. */
9215 if (update_mode_line
9216 && EQ (FRAME_SELECTED_WINDOW (f), window))
9217 {
9218 int redisplay_menu_p = 0;
9219
9220 if (FRAME_WINDOW_P (f))
9221 {
9222 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
9223 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
9224 #else
9225 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
9226 #endif
9227 }
9228 else
9229 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
9230
9231 if (redisplay_menu_p)
9232 display_menu_bar (w);
9233
9234 #ifdef HAVE_WINDOW_SYSTEM
9235 if (WINDOWP (f->tool_bar_window)
9236 && (FRAME_TOOL_BAR_LINES (f) > 0
9237 || auto_resize_tool_bars_p))
9238 redisplay_tool_bar (f);
9239 #endif
9240 }
9241
9242 finish_scroll_bars:
9243
9244 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
9245 {
9246 int start, end, whole;
9247
9248 /* Calculate the start and end positions for the current window.
9249 At some point, it would be nice to choose between scrollbars
9250 which reflect the whole buffer size, with special markers
9251 indicating narrowing, and scrollbars which reflect only the
9252 visible region.
9253
9254 Note that mini-buffers sometimes aren't displaying any text. */
9255 if (!MINI_WINDOW_P (w)
9256 || (w == XWINDOW (minibuf_window)
9257 && NILP (echo_area_buffer[0])))
9258 {
9259 whole = ZV - BEGV;
9260 start = marker_position (w->start) - BEGV;
9261 /* I don't think this is guaranteed to be right. For the
9262 moment, we'll pretend it is. */
9263 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
9264
9265 if (end < start)
9266 end = start;
9267 if (whole < (end - start))
9268 whole = end - start;
9269 }
9270 else
9271 start = end = whole = 0;
9272
9273 /* Indicate what this scroll bar ought to be displaying now. */
9274 (*set_vertical_scroll_bar_hook) (w, end - start, whole, start);
9275
9276 /* Note that we actually used the scroll bar attached to this
9277 window, so it shouldn't be deleted at the end of redisplay. */
9278 (*redeem_scroll_bar_hook) (w);
9279 }
9280
9281 restore_buffers:
9282
9283 /* Restore current_buffer and value of point in it. */
9284 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
9285 if (really_switched_buffer)
9286 set_buffer_internal_1 (old);
9287 else
9288 set_buffer_temp (old);
9289 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
9290
9291 unbind_to (count, Qnil);
9292 }
9293
9294
9295 /* Build the complete desired matrix of WINDOW with a window start
9296 buffer position POS. Value is non-zero if successful. It is zero
9297 if fonts were loaded during redisplay which makes re-adjusting
9298 glyph matrices necessary. */
9299
9300 int
9301 try_window (window, pos)
9302 Lisp_Object window;
9303 struct text_pos pos;
9304 {
9305 struct window *w = XWINDOW (window);
9306 struct it it;
9307 struct glyph_row *last_text_row = NULL;
9308
9309 /* Make POS the new window start. */
9310 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
9311
9312 /* Mark cursor position as unknown. No overlay arrow seen. */
9313 w->cursor.vpos = -1;
9314 overlay_arrow_seen = 0;
9315
9316 /* Initialize iterator and info to start at POS. */
9317 start_display (&it, w, pos);
9318
9319 /* Display all lines of W. */
9320 while (it.current_y < it.last_visible_y)
9321 {
9322 if (display_line (&it))
9323 last_text_row = it.glyph_row - 1;
9324 if (fonts_changed_p)
9325 return 0;
9326 }
9327
9328 /* If bottom moved off end of frame, change mode line percentage. */
9329 if (XFASTINT (w->window_end_pos) <= 0
9330 && Z != IT_CHARPOS (it))
9331 w->update_mode_line = Qt;
9332
9333 /* Set window_end_pos to the offset of the last character displayed
9334 on the window from the end of current_buffer. Set
9335 window_end_vpos to its row number. */
9336 if (last_text_row)
9337 {
9338 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
9339 w->window_end_bytepos
9340 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
9341 XSETFASTINT (w->window_end_pos,
9342 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
9343 XSETFASTINT (w->window_end_vpos,
9344 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
9345 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
9346 ->displays_text_p);
9347 }
9348 else
9349 {
9350 w->window_end_bytepos = 0;
9351 XSETFASTINT (w->window_end_pos, 0);
9352 XSETFASTINT (w->window_end_vpos, 0);
9353 }
9354
9355 /* But that is not valid info until redisplay finishes. */
9356 w->window_end_valid = Qnil;
9357 return 1;
9358 }
9359
9360
9361 \f
9362 /************************************************************************
9363 Window redisplay reusing current matrix when buffer has not changed
9364 ************************************************************************/
9365
9366 /* Try redisplay of window W showing an unchanged buffer with a
9367 different window start than the last time it was displayed by
9368 reusing its current matrix. Value is non-zero if successful.
9369 W->start is the new window start. */
9370
9371 static int
9372 try_window_reusing_current_matrix (w)
9373 struct window *w;
9374 {
9375 struct frame *f = XFRAME (w->frame);
9376 struct glyph_row *row, *bottom_row;
9377 struct it it;
9378 struct run run;
9379 struct text_pos start, new_start;
9380 int nrows_scrolled, i;
9381 struct glyph_row *last_text_row;
9382 struct glyph_row *last_reused_text_row;
9383 struct glyph_row *start_row;
9384 int start_vpos, min_y, max_y;
9385
9386 /* Right now this function doesn't handle terminal frames. */
9387 if (!FRAME_WINDOW_P (f))
9388 return 0;
9389
9390 /* Can't do this if region may have changed. */
9391 if ((!NILP (Vtransient_mark_mode)
9392 && !NILP (current_buffer->mark_active))
9393 || !NILP (w->region_showing)
9394 || !NILP (Vshow_trailing_whitespace))
9395 return 0;
9396
9397 /* If top-line visibility has changed, give up. */
9398 if (WINDOW_WANTS_HEADER_LINE_P (w)
9399 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
9400 return 0;
9401
9402 /* Give up if old or new display is scrolled vertically. We could
9403 make this function handle this, but right now it doesn't. */
9404 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9405 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
9406 return 0;
9407
9408 /* The variable new_start now holds the new window start. The old
9409 start `start' can be determined from the current matrix. */
9410 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
9411 start = start_row->start.pos;
9412 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
9413
9414 /* Clear the desired matrix for the display below. */
9415 clear_glyph_matrix (w->desired_matrix);
9416
9417 if (CHARPOS (new_start) <= CHARPOS (start))
9418 {
9419 int first_row_y;
9420
9421 IF_DEBUG (debug_method_add (w, "twu1"));
9422
9423 /* Display up to a row that can be reused. The variable
9424 last_text_row is set to the last row displayed that displays
9425 text. */
9426 start_display (&it, w, new_start);
9427 first_row_y = it.current_y;
9428 w->cursor.vpos = -1;
9429 last_text_row = last_reused_text_row = NULL;
9430 while (it.current_y < it.last_visible_y
9431 && IT_CHARPOS (it) < CHARPOS (start)
9432 && !fonts_changed_p)
9433 if (display_line (&it))
9434 last_text_row = it.glyph_row - 1;
9435
9436 /* A value of current_y < last_visible_y means that we stopped
9437 at the previous window start, which in turn means that we
9438 have at least one reusable row. */
9439 if (it.current_y < it.last_visible_y)
9440 {
9441 nrows_scrolled = it.vpos;
9442
9443 /* Find PT if not already found in the lines displayed. */
9444 if (w->cursor.vpos < 0)
9445 {
9446 int dy = it.current_y - first_row_y;
9447
9448 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9449 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9450 {
9451 if (PT >= MATRIX_ROW_START_CHARPOS (row)
9452 && PT < MATRIX_ROW_END_CHARPOS (row))
9453 {
9454 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
9455 dy, nrows_scrolled);
9456 break;
9457 }
9458
9459 if (MATRIX_ROW_BOTTOM_Y (row) + dy >= it.last_visible_y)
9460 break;
9461
9462 ++row;
9463 }
9464
9465 /* Give up if point was not found. This shouldn't
9466 happen often; not more often than with try_window
9467 itself. */
9468 if (w->cursor.vpos < 0)
9469 {
9470 clear_glyph_matrix (w->desired_matrix);
9471 return 0;
9472 }
9473 }
9474
9475 /* Scroll the display. Do it before the current matrix is
9476 changed. The problem here is that update has not yet
9477 run, i.e. part of the current matrix is not up to date.
9478 scroll_run_hook will clear the cursor, and use the
9479 current matrix to get the height of the row the cursor is
9480 in. */
9481 run.current_y = first_row_y;
9482 run.desired_y = it.current_y;
9483 run.height = it.last_visible_y - it.current_y;
9484 if (run.height > 0
9485 && run.current_y != run.desired_y)
9486 {
9487 update_begin (f);
9488 rif->update_window_begin_hook (w);
9489 rif->scroll_run_hook (w, &run);
9490 rif->update_window_end_hook (w, 0);
9491 update_end (f);
9492 }
9493
9494 /* Shift current matrix down by nrows_scrolled lines. */
9495 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
9496 rotate_matrix (w->current_matrix,
9497 start_vpos,
9498 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
9499 nrows_scrolled);
9500
9501 /* Disable lines not reused. */
9502 for (i = 0; i < it.vpos; ++i)
9503 MATRIX_ROW (w->current_matrix, i)->enabled_p = 0;
9504
9505 /* Re-compute Y positions. */
9506 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix) + nrows_scrolled;
9507 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
9508 max_y = it.last_visible_y;
9509 while (row < bottom_row)
9510 {
9511 row->y = it.current_y;
9512
9513 if (row->y < min_y)
9514 row->visible_height = row->height - (min_y - row->y);
9515 else if (row->y + row->height > max_y)
9516 row->visible_height
9517 = row->height - (row->y + row->height - max_y);
9518 else
9519 row->visible_height = row->height;
9520
9521 it.current_y += row->height;
9522 ++it.vpos;
9523
9524 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9525 last_reused_text_row = row;
9526 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
9527 break;
9528 ++row;
9529 }
9530 }
9531
9532 /* Update window_end_pos etc.; last_reused_text_row is the last
9533 reused row from the current matrix containing text, if any.
9534 The value of last_text_row is the last displayed line
9535 containing text. */
9536 if (last_reused_text_row)
9537 {
9538 w->window_end_bytepos
9539 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
9540 XSETFASTINT (w->window_end_pos,
9541 Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
9542 XSETFASTINT (w->window_end_vpos,
9543 MATRIX_ROW_VPOS (last_reused_text_row,
9544 w->current_matrix));
9545 }
9546 else if (last_text_row)
9547 {
9548 w->window_end_bytepos
9549 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
9550 XSETFASTINT (w->window_end_pos,
9551 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
9552 XSETFASTINT (w->window_end_vpos,
9553 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
9554 }
9555 else
9556 {
9557 /* This window must be completely empty. */
9558 w->window_end_bytepos = 0;
9559 XSETFASTINT (w->window_end_pos, 0);
9560 XSETFASTINT (w->window_end_vpos, 0);
9561 }
9562 w->window_end_valid = Qnil;
9563
9564 /* Update hint: don't try scrolling again in update_window. */
9565 w->desired_matrix->no_scrolling_p = 1;
9566
9567 #if GLYPH_DEBUG
9568 debug_method_add (w, "try_window_reusing_current_matrix 1");
9569 #endif
9570 return 1;
9571 }
9572 else if (CHARPOS (new_start) > CHARPOS (start))
9573 {
9574 struct glyph_row *pt_row, *row;
9575 struct glyph_row *first_reusable_row;
9576 struct glyph_row *first_row_to_display;
9577 int dy;
9578 int yb = window_text_bottom_y (w);
9579
9580 IF_DEBUG (debug_method_add (w, "twu2"));
9581
9582 /* Find the row starting at new_start, if there is one. Don't
9583 reuse a partially visible line at the end. */
9584 first_reusable_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9585 while (first_reusable_row->enabled_p
9586 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
9587 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
9588 < CHARPOS (new_start)))
9589 ++first_reusable_row;
9590
9591 /* Give up if there is no row to reuse. */
9592 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
9593 || !first_reusable_row->enabled_p
9594 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
9595 != CHARPOS (new_start)))
9596 return 0;
9597
9598 /* We can reuse fully visible rows beginning with
9599 first_reusable_row to the end of the window. Set
9600 first_row_to_display to the first row that cannot be reused.
9601 Set pt_row to the row containing point, if there is any. */
9602 first_row_to_display = first_reusable_row;
9603 pt_row = NULL;
9604 while (MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb)
9605 {
9606 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
9607 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
9608 pt_row = first_row_to_display;
9609
9610 ++first_row_to_display;
9611 }
9612
9613 /* Start displaying at the start of first_row_to_display. */
9614 xassert (first_row_to_display->y < yb);
9615 init_to_row_start (&it, w, first_row_to_display);
9616 nrows_scrolled = MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix);
9617 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
9618 - nrows_scrolled);
9619 it.current_y = first_row_to_display->y - first_reusable_row->y;
9620
9621 /* Display lines beginning with first_row_to_display in the
9622 desired matrix. Set last_text_row to the last row displayed
9623 that displays text. */
9624 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
9625 if (pt_row == NULL)
9626 w->cursor.vpos = -1;
9627 last_text_row = NULL;
9628 while (it.current_y < it.last_visible_y && !fonts_changed_p)
9629 if (display_line (&it))
9630 last_text_row = it.glyph_row - 1;
9631
9632 /* Give up If point isn't in a row displayed or reused. */
9633 if (w->cursor.vpos < 0)
9634 {
9635 clear_glyph_matrix (w->desired_matrix);
9636 return 0;
9637 }
9638
9639 /* If point is in a reused row, adjust y and vpos of the cursor
9640 position. */
9641 if (pt_row)
9642 {
9643 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
9644 w->current_matrix);
9645 w->cursor.y -= first_reusable_row->y;
9646 }
9647
9648 /* Scroll the display. */
9649 run.current_y = first_reusable_row->y;
9650 run.desired_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
9651 run.height = it.last_visible_y - run.current_y;
9652 if (run.height)
9653 {
9654 struct frame *f = XFRAME (WINDOW_FRAME (w));
9655 update_begin (f);
9656 rif->update_window_begin_hook (w);
9657 rif->scroll_run_hook (w, &run);
9658 rif->update_window_end_hook (w, 0);
9659 update_end (f);
9660 }
9661
9662 /* Adjust Y positions of reused rows. */
9663 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
9664 row = first_reusable_row;
9665 dy = first_reusable_row->y;
9666 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
9667 max_y = it.last_visible_y;
9668 while (row < first_row_to_display)
9669 {
9670 row->y -= dy;
9671 if (row->y < min_y)
9672 row->visible_height = row->height - (min_y - row->y);
9673 else if (row->y + row->height > max_y)
9674 row->visible_height
9675 = row->height - (row->y + row->height - max_y);
9676 else
9677 row->visible_height = row->height;
9678 ++row;
9679 }
9680
9681 /* Disable rows not reused. */
9682 while (row < bottom_row)
9683 {
9684 row->enabled_p = 0;
9685 ++row;
9686 }
9687
9688 /* Scroll the current matrix. */
9689 xassert (nrows_scrolled > 0);
9690 rotate_matrix (w->current_matrix,
9691 start_vpos,
9692 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
9693 -nrows_scrolled);
9694
9695 /* Adjust window end. A null value of last_text_row means that
9696 the window end is in reused rows which in turn means that
9697 only its vpos can have changed. */
9698 if (last_text_row)
9699 {
9700 w->window_end_bytepos
9701 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
9702 XSETFASTINT (w->window_end_pos,
9703 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
9704 XSETFASTINT (w->window_end_vpos,
9705 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
9706 }
9707 else
9708 {
9709 XSETFASTINT (w->window_end_vpos,
9710 XFASTINT (w->window_end_vpos) - nrows_scrolled);
9711 }
9712
9713 w->window_end_valid = Qnil;
9714 w->desired_matrix->no_scrolling_p = 1;
9715
9716 #if GLYPH_DEBUG
9717 debug_method_add (w, "try_window_reusing_current_matrix 2");
9718 #endif
9719 return 1;
9720 }
9721
9722 return 0;
9723 }
9724
9725
9726 \f
9727 /************************************************************************
9728 Window redisplay reusing current matrix when buffer has changed
9729 ************************************************************************/
9730
9731 static struct glyph_row *get_last_unchanged_at_beg_row P_ ((struct window *));
9732 static struct glyph_row *get_first_unchanged_at_end_row P_ ((struct window *,
9733 int *, int *));
9734 static struct glyph_row *
9735 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
9736 struct glyph_row *));
9737
9738
9739 /* Return the last row in MATRIX displaying text. If row START is
9740 non-null, start searching with that row. IT gives the dimensions
9741 of the display. Value is null if matrix is empty; otherwise it is
9742 a pointer to the row found. */
9743
9744 static struct glyph_row *
9745 find_last_row_displaying_text (matrix, it, start)
9746 struct glyph_matrix *matrix;
9747 struct it *it;
9748 struct glyph_row *start;
9749 {
9750 struct glyph_row *row, *row_found;
9751
9752 /* Set row_found to the last row in IT->w's current matrix
9753 displaying text. The loop looks funny but think of partially
9754 visible lines. */
9755 row_found = NULL;
9756 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
9757 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9758 {
9759 xassert (row->enabled_p);
9760 row_found = row;
9761 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
9762 break;
9763 ++row;
9764 }
9765
9766 return row_found;
9767 }
9768
9769
9770 /* Return the last row in the current matrix of W that is not affected
9771 by changes at the start of current_buffer that occurred since the
9772 last time W was redisplayed. Value is null if no such row exists.
9773
9774 The global variable beg_unchanged has to contain the number of
9775 bytes unchanged at the start of current_buffer. BEG +
9776 beg_unchanged is the buffer position of the first changed byte in
9777 current_buffer. Characters at positions < BEG + beg_unchanged are
9778 at the same buffer positions as they were when the current matrix
9779 was built. */
9780
9781 static struct glyph_row *
9782 get_last_unchanged_at_beg_row (w)
9783 struct window *w;
9784 {
9785 int first_changed_pos = BEG + BEG_UNCHANGED;
9786 struct glyph_row *row;
9787 struct glyph_row *row_found = NULL;
9788 int yb = window_text_bottom_y (w);
9789
9790 /* Find the last row displaying unchanged text. */
9791 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9792 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
9793 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
9794 {
9795 if (/* If row ends before first_changed_pos, it is unchanged,
9796 except in some case. */
9797 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
9798 /* When row ends in ZV and we write at ZV it is not
9799 unchanged. */
9800 && !row->ends_at_zv_p
9801 /* When first_changed_pos is the end of a continued line,
9802 row is not unchanged because it may be no longer
9803 continued. */
9804 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
9805 && row->continued_p))
9806 row_found = row;
9807
9808 /* Stop if last visible row. */
9809 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
9810 break;
9811
9812 ++row;
9813 }
9814
9815 return row_found;
9816 }
9817
9818
9819 /* Find the first glyph row in the current matrix of W that is not
9820 affected by changes at the end of current_buffer since the last
9821 time the window was redisplayed. Return in *DELTA the number of
9822 chars by which buffer positions in unchanged text at the end of
9823 current_buffer must be adjusted. Return in *DELTA_BYTES the
9824 corresponding number of bytes. Value is null if no such row
9825 exists, i.e. all rows are affected by changes. */
9826
9827 static struct glyph_row *
9828 get_first_unchanged_at_end_row (w, delta, delta_bytes)
9829 struct window *w;
9830 int *delta, *delta_bytes;
9831 {
9832 struct glyph_row *row;
9833 struct glyph_row *row_found = NULL;
9834
9835 *delta = *delta_bytes = 0;
9836
9837 /* A value of window_end_pos >= end_unchanged means that the window
9838 end is in the range of changed text. If so, there is no
9839 unchanged row at the end of W's current matrix. */
9840 xassert (!NILP (w->window_end_valid));
9841 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
9842 return NULL;
9843
9844 /* Set row to the last row in W's current matrix displaying text. */
9845 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
9846
9847 /* If matrix is entirely empty, no unchanged row exists. */
9848 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9849 {
9850 /* The value of row is the last glyph row in the matrix having a
9851 meaningful buffer position in it. The end position of row
9852 corresponds to window_end_pos. This allows us to translate
9853 buffer positions in the current matrix to current buffer
9854 positions for characters not in changed text. */
9855 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
9856 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
9857 int last_unchanged_pos, last_unchanged_pos_old;
9858 struct glyph_row *first_text_row
9859 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9860
9861 *delta = Z - Z_old;
9862 *delta_bytes = Z_BYTE - Z_BYTE_old;
9863
9864 /* Set last_unchanged_pos to the buffer position of the last
9865 character in the buffer that has not been changed. Z is the
9866 index + 1 of the last byte in current_buffer, i.e. by
9867 subtracting end_unchanged we get the index of the last
9868 unchanged character, and we have to add BEG to get its buffer
9869 position. */
9870 last_unchanged_pos = Z - END_UNCHANGED + BEG;
9871 last_unchanged_pos_old = last_unchanged_pos - *delta;
9872
9873 /* Search backward from ROW for a row displaying a line that
9874 starts at a minimum position >= last_unchanged_pos_old. */
9875 while (row >= first_text_row)
9876 {
9877 xassert (row->enabled_p);
9878 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (row));
9879
9880 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
9881 row_found = row;
9882 --row;
9883 }
9884 }
9885
9886 xassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
9887 return row_found;
9888 }
9889
9890
9891 /* Make sure that glyph rows in the current matrix of window W
9892 reference the same glyph memory as corresponding rows in the
9893 frame's frame matrix. This function is called after scrolling W's
9894 current matrix on a terminal frame in try_window_id and
9895 try_window_reusing_current_matrix. */
9896
9897 static void
9898 sync_frame_with_window_matrix_rows (w)
9899 struct window *w;
9900 {
9901 struct frame *f = XFRAME (w->frame);
9902 struct glyph_row *window_row, *window_row_end, *frame_row;
9903
9904 /* Preconditions: W must be a leaf window and full-width. Its frame
9905 must have a frame matrix. */
9906 xassert (NILP (w->hchild) && NILP (w->vchild));
9907 xassert (WINDOW_FULL_WIDTH_P (w));
9908 xassert (!FRAME_WINDOW_P (f));
9909
9910 /* If W is a full-width window, glyph pointers in W's current matrix
9911 have, by definition, to be the same as glyph pointers in the
9912 corresponding frame matrix. */
9913 window_row = w->current_matrix->rows;
9914 window_row_end = window_row + w->current_matrix->nrows;
9915 frame_row = f->current_matrix->rows + XFASTINT (w->top);
9916 while (window_row < window_row_end)
9917 {
9918 int area;
9919
9920 for (area = LEFT_MARGIN_AREA; area <= LAST_AREA; ++area)
9921 frame_row->glyphs[area] = window_row->glyphs[area];
9922
9923 /* Disable frame rows whose corresponding window rows have
9924 been disabled in try_window_id. */
9925 if (!window_row->enabled_p)
9926 frame_row->enabled_p = 0;
9927
9928 ++window_row, ++frame_row;
9929 }
9930 }
9931
9932
9933 /* Find the glyph row in window W containing CHARPOS. Consider all
9934 rows between START and END (not inclusive). END null means search
9935 all rows to the end of the display area of W. Value is the row
9936 containing CHARPOS or null. */
9937
9938 static struct glyph_row *
9939 row_containing_pos (w, charpos, start, end)
9940 struct window *w;
9941 int charpos;
9942 struct glyph_row *start, *end;
9943 {
9944 struct glyph_row *row = start;
9945 int last_y;
9946
9947 /* If we happen to start on a header-line, skip that. */
9948 if (row->mode_line_p)
9949 ++row;
9950
9951 if ((end && row >= end) || !row->enabled_p)
9952 return NULL;
9953
9954 last_y = window_text_bottom_y (w);
9955
9956 while ((end == NULL || row < end)
9957 && (MATRIX_ROW_END_CHARPOS (row) < charpos
9958 /* The end position of a row equals the start
9959 position of the next row. If CHARPOS is there, we
9960 would rather display it in the next line, except
9961 when this line ends in ZV. */
9962 || (MATRIX_ROW_END_CHARPOS (row) == charpos
9963 && (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
9964 || !row->ends_at_zv_p)))
9965 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
9966 ++row;
9967
9968 /* Give up if CHARPOS not found. */
9969 if ((end && row >= end)
9970 || charpos < MATRIX_ROW_START_CHARPOS (row)
9971 || charpos > MATRIX_ROW_END_CHARPOS (row))
9972 row = NULL;
9973
9974 return row;
9975 }
9976
9977
9978 /* Try to redisplay window W by reusing its existing display. W's
9979 current matrix must be up to date when this function is called,
9980 i.e. window_end_valid must not be nil.
9981
9982 Value is
9983
9984 1 if display has been updated
9985 0 if otherwise unsuccessful
9986 -1 if redisplay with same window start is known not to succeed
9987
9988 The following steps are performed:
9989
9990 1. Find the last row in the current matrix of W that is not
9991 affected by changes at the start of current_buffer. If no such row
9992 is found, give up.
9993
9994 2. Find the first row in W's current matrix that is not affected by
9995 changes at the end of current_buffer. Maybe there is no such row.
9996
9997 3. Display lines beginning with the row + 1 found in step 1 to the
9998 row found in step 2 or, if step 2 didn't find a row, to the end of
9999 the window.
10000
10001 4. If cursor is not known to appear on the window, give up.
10002
10003 5. If display stopped at the row found in step 2, scroll the
10004 display and current matrix as needed.
10005
10006 6. Maybe display some lines at the end of W, if we must. This can
10007 happen under various circumstances, like a partially visible line
10008 becoming fully visible, or because newly displayed lines are displayed
10009 in smaller font sizes.
10010
10011 7. Update W's window end information. */
10012
10013 /* Check that window end is what we expect it to be. */
10014
10015 static int
10016 try_window_id (w)
10017 struct window *w;
10018 {
10019 struct frame *f = XFRAME (w->frame);
10020 struct glyph_matrix *current_matrix = w->current_matrix;
10021 struct glyph_matrix *desired_matrix = w->desired_matrix;
10022 struct glyph_row *last_unchanged_at_beg_row;
10023 struct glyph_row *first_unchanged_at_end_row;
10024 struct glyph_row *row;
10025 struct glyph_row *bottom_row;
10026 int bottom_vpos;
10027 struct it it;
10028 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
10029 struct text_pos start_pos;
10030 struct run run;
10031 int first_unchanged_at_end_vpos = 0;
10032 struct glyph_row *last_text_row, *last_text_row_at_end;
10033 struct text_pos start;
10034
10035 SET_TEXT_POS_FROM_MARKER (start, w->start);
10036
10037 /* Check pre-conditions. Window end must be valid, otherwise
10038 the current matrix would not be up to date. */
10039 xassert (!NILP (w->window_end_valid));
10040 xassert (FRAME_WINDOW_P (XFRAME (w->frame))
10041 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)));
10042
10043 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
10044 only if buffer has really changed. The reason is that the gap is
10045 initially at Z for freshly visited files. The code below would
10046 set end_unchanged to 0 in that case. */
10047 if (MODIFF > SAVE_MODIFF
10048 /* This seems to happen sometimes after saving a buffer. */
10049 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
10050 {
10051 if (GPT - BEG < BEG_UNCHANGED)
10052 BEG_UNCHANGED = GPT - BEG;
10053 if (Z - GPT < END_UNCHANGED)
10054 END_UNCHANGED = Z - GPT;
10055 }
10056
10057 /* If window starts after a line end, and the last change is in
10058 front of that newline, then changes don't affect the display.
10059 This case happens with stealth-fontification. Note that although
10060 the display is unchanged, glyph positions in the matrix have to
10061 be adjusted, of course. */
10062 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
10063 if (CHARPOS (start) > BEGV
10064 && Z - END_UNCHANGED < CHARPOS (start) - 1
10065 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n'
10066 && PT < MATRIX_ROW_END_CHARPOS (row))
10067 {
10068 struct glyph_row *r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
10069 int delta = CHARPOS (start) - MATRIX_ROW_START_CHARPOS (r0);
10070
10071 if (delta)
10072 {
10073 struct glyph_row *r1 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
10074 int delta_bytes = BYTEPOS (start) - MATRIX_ROW_START_BYTEPOS (r0);
10075
10076 increment_matrix_positions (w->current_matrix,
10077 MATRIX_ROW_VPOS (r0, current_matrix),
10078 MATRIX_ROW_VPOS (r1, current_matrix),
10079 delta, delta_bytes);
10080 }
10081
10082 #if 0 /* If changes are all in front of the window start, the
10083 distance of the last displayed glyph from Z hasn't
10084 changed. */
10085 w->window_end_pos
10086 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
10087 w->window_end_bytepos
10088 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
10089 #endif
10090
10091 return 1;
10092 }
10093
10094 /* Return quickly if changes are all below what is displayed in the
10095 window, and if PT is in the window. */
10096 if (BEG_UNCHANGED > MATRIX_ROW_END_CHARPOS (row)
10097 && PT < MATRIX_ROW_END_CHARPOS (row))
10098 {
10099 /* We have to update window end positions because the buffer's
10100 size has changed. */
10101 w->window_end_pos
10102 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
10103 w->window_end_bytepos
10104 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
10105 return 1;
10106 }
10107
10108 /* Check that window start agrees with the start of the first glyph
10109 row in its current matrix. Check this after we know the window
10110 start is not in changed text, otherwise positions would not be
10111 comparable. */
10112 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10113 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
10114 return 0;
10115
10116 /* Compute the position at which we have to start displaying new
10117 lines. Some of the lines at the top of the window might be
10118 reusable because they are not displaying changed text. Find the
10119 last row in W's current matrix not affected by changes at the
10120 start of current_buffer. Value is null if changes start in the
10121 first line of window. */
10122 last_unchanged_at_beg_row = get_last_unchanged_at_beg_row (w);
10123 if (last_unchanged_at_beg_row)
10124 {
10125 init_to_row_end (&it, w, last_unchanged_at_beg_row);
10126 start_pos = it.current.pos;
10127
10128 /* Start displaying new lines in the desired matrix at the same
10129 vpos we would use in the current matrix, i.e. below
10130 last_unchanged_at_beg_row. */
10131 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
10132 current_matrix);
10133 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
10134 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
10135
10136 xassert (it.hpos == 0 && it.current_x == 0);
10137 }
10138 else
10139 {
10140 /* There are no reusable lines at the start of the window.
10141 Start displaying in the first line. */
10142 start_display (&it, w, start);
10143 start_pos = it.current.pos;
10144 }
10145
10146 /* Find the first row that is not affected by changes at the end of
10147 the buffer. Value will be null if there is no unchanged row, in
10148 which case we must redisplay to the end of the window. delta
10149 will be set to the value by which buffer positions beginning with
10150 first_unchanged_at_end_row have to be adjusted due to text
10151 changes. */
10152 first_unchanged_at_end_row
10153 = get_first_unchanged_at_end_row (w, &delta, &delta_bytes);
10154 IF_DEBUG (debug_delta = delta);
10155 IF_DEBUG (debug_delta_bytes = delta_bytes);
10156
10157 /* Set stop_pos to the buffer position up to which we will have to
10158 display new lines. If first_unchanged_at_end_row != NULL, this
10159 is the buffer position of the start of the line displayed in that
10160 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
10161 that we don't stop at a buffer position. */
10162 stop_pos = 0;
10163 if (first_unchanged_at_end_row)
10164 {
10165 xassert (last_unchanged_at_beg_row == NULL
10166 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
10167
10168 /* If this is a continuation line, move forward to the next one
10169 that isn't. Changes in lines above affect this line.
10170 Caution: this may move first_unchanged_at_end_row to a row
10171 not displaying text. */
10172 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
10173 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
10174 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
10175 < it.last_visible_y))
10176 ++first_unchanged_at_end_row;
10177
10178 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
10179 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
10180 >= it.last_visible_y))
10181 first_unchanged_at_end_row = NULL;
10182 else
10183 {
10184 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
10185 + delta);
10186 first_unchanged_at_end_vpos
10187 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
10188 xassert (stop_pos >= Z - END_UNCHANGED);
10189 }
10190 }
10191 else if (last_unchanged_at_beg_row == NULL)
10192 return 0;
10193
10194
10195 #if GLYPH_DEBUG
10196
10197 /* Either there is no unchanged row at the end, or the one we have
10198 now displays text. This is a necessary condition for the window
10199 end pos calculation at the end of this function. */
10200 xassert (first_unchanged_at_end_row == NULL
10201 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
10202
10203 debug_last_unchanged_at_beg_vpos
10204 = (last_unchanged_at_beg_row
10205 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
10206 : -1);
10207 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
10208
10209 #endif /* GLYPH_DEBUG != 0 */
10210
10211
10212 /* Display new lines. Set last_text_row to the last new line
10213 displayed which has text on it, i.e. might end up as being the
10214 line where the window_end_vpos is. */
10215 w->cursor.vpos = -1;
10216 last_text_row = NULL;
10217 overlay_arrow_seen = 0;
10218 while (it.current_y < it.last_visible_y
10219 && !fonts_changed_p
10220 && (first_unchanged_at_end_row == NULL
10221 || IT_CHARPOS (it) < stop_pos))
10222 {
10223 if (display_line (&it))
10224 last_text_row = it.glyph_row - 1;
10225 }
10226
10227 if (fonts_changed_p)
10228 return -1;
10229
10230
10231 /* Compute differences in buffer positions, y-positions etc. for
10232 lines reused at the bottom of the window. Compute what we can
10233 scroll. */
10234 if (first_unchanged_at_end_row
10235 /* No lines reused because we displayed everything up to the
10236 bottom of the window. */
10237 && it.current_y < it.last_visible_y)
10238 {
10239 dvpos = (it.vpos
10240 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
10241 current_matrix));
10242 dy = it.current_y - first_unchanged_at_end_row->y;
10243 run.current_y = first_unchanged_at_end_row->y;
10244 run.desired_y = run.current_y + dy;
10245 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
10246 }
10247 else
10248 {
10249 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
10250 first_unchanged_at_end_row = NULL;
10251 }
10252 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
10253
10254
10255 /* Find the cursor if not already found. We have to decide whether
10256 PT will appear on this window (it sometimes doesn't, but this is
10257 not a very frequent case.) This decision has to be made before
10258 the current matrix is altered. A value of cursor.vpos < 0 means
10259 that PT is either in one of the lines beginning at
10260 first_unchanged_at_end_row or below the window. Don't care for
10261 lines that might be displayed later at the window end; as
10262 mentioned, this is not a frequent case. */
10263 if (w->cursor.vpos < 0)
10264 {
10265 /* Cursor in unchanged rows at the top? */
10266 if (PT < CHARPOS (start_pos)
10267 && last_unchanged_at_beg_row)
10268 {
10269 row = row_containing_pos (w, PT,
10270 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
10271 last_unchanged_at_beg_row + 1);
10272 xassert (row && row <= last_unchanged_at_beg_row);
10273 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10274 }
10275
10276 /* Start from first_unchanged_at_end_row looking for PT. */
10277 else if (first_unchanged_at_end_row)
10278 {
10279 row = row_containing_pos (w, PT - delta,
10280 first_unchanged_at_end_row, NULL);
10281 if (row)
10282 set_cursor_from_row (w, row, w->current_matrix, delta,
10283 delta_bytes, dy, dvpos);
10284 }
10285
10286 /* Give up if cursor was not found. */
10287 if (w->cursor.vpos < 0)
10288 {
10289 clear_glyph_matrix (w->desired_matrix);
10290 return -1;
10291 }
10292 }
10293
10294 /* Don't let the cursor end in the scroll margins. */
10295 {
10296 int this_scroll_margin, cursor_height;
10297
10298 this_scroll_margin = max (0, scroll_margin);
10299 this_scroll_margin = min (this_scroll_margin,
10300 XFASTINT (w->height) / 4);
10301 this_scroll_margin *= CANON_Y_UNIT (it.f);
10302 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
10303
10304 if ((w->cursor.y < this_scroll_margin
10305 && CHARPOS (start) > BEGV)
10306 /* Don't take scroll margin into account at the bottom because
10307 old redisplay didn't do it either. */
10308 || w->cursor.y + cursor_height > it.last_visible_y)
10309 {
10310 w->cursor.vpos = -1;
10311 clear_glyph_matrix (w->desired_matrix);
10312 return -1;
10313 }
10314 }
10315
10316 /* Scroll the display. Do it before changing the current matrix so
10317 that xterm.c doesn't get confused about where the cursor glyph is
10318 found. */
10319 if (dy && run.height)
10320 {
10321 update_begin (f);
10322
10323 if (FRAME_WINDOW_P (f))
10324 {
10325 rif->update_window_begin_hook (w);
10326 rif->scroll_run_hook (w, &run);
10327 rif->update_window_end_hook (w, 0);
10328 }
10329 else
10330 {
10331 /* Terminal frame. In this case, dvpos gives the number of
10332 lines to scroll by; dvpos < 0 means scroll up. */
10333 int first_unchanged_at_end_vpos
10334 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
10335 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
10336 int end = XFASTINT (w->top) + window_internal_height (w);
10337
10338 /* Perform the operation on the screen. */
10339 if (dvpos > 0)
10340 {
10341 /* Scroll last_unchanged_at_beg_row to the end of the
10342 window down dvpos lines. */
10343 set_terminal_window (end);
10344
10345 /* On dumb terminals delete dvpos lines at the end
10346 before inserting dvpos empty lines. */
10347 if (!scroll_region_ok)
10348 ins_del_lines (end - dvpos, -dvpos);
10349
10350 /* Insert dvpos empty lines in front of
10351 last_unchanged_at_beg_row. */
10352 ins_del_lines (from, dvpos);
10353 }
10354 else if (dvpos < 0)
10355 {
10356 /* Scroll up last_unchanged_at_beg_vpos to the end of
10357 the window to last_unchanged_at_beg_vpos - |dvpos|. */
10358 set_terminal_window (end);
10359
10360 /* Delete dvpos lines in front of
10361 last_unchanged_at_beg_vpos. ins_del_lines will set
10362 the cursor to the given vpos and emit |dvpos| delete
10363 line sequences. */
10364 ins_del_lines (from + dvpos, dvpos);
10365
10366 /* On a dumb terminal insert dvpos empty lines at the
10367 end. */
10368 if (!scroll_region_ok)
10369 ins_del_lines (end + dvpos, -dvpos);
10370 }
10371
10372 set_terminal_window (0);
10373 }
10374
10375 update_end (f);
10376 }
10377
10378 /* Shift reused rows of the current matrix to the right position.
10379 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
10380 text. */
10381 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
10382 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
10383 if (dvpos < 0)
10384 {
10385 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
10386 bottom_vpos, dvpos);
10387 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
10388 bottom_vpos, 0);
10389 }
10390 else if (dvpos > 0)
10391 {
10392 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
10393 bottom_vpos, dvpos);
10394 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
10395 first_unchanged_at_end_vpos + dvpos, 0);
10396 }
10397
10398 /* For frame-based redisplay, make sure that current frame and window
10399 matrix are in sync with respect to glyph memory. */
10400 if (!FRAME_WINDOW_P (f))
10401 sync_frame_with_window_matrix_rows (w);
10402
10403 /* Adjust buffer positions in reused rows. */
10404 if (delta)
10405 increment_matrix_positions (current_matrix,
10406 first_unchanged_at_end_vpos + dvpos,
10407 bottom_vpos, delta, delta_bytes);
10408
10409 /* Adjust Y positions. */
10410 if (dy)
10411 shift_glyph_matrix (w, current_matrix,
10412 first_unchanged_at_end_vpos + dvpos,
10413 bottom_vpos, dy);
10414
10415 if (first_unchanged_at_end_row)
10416 first_unchanged_at_end_row += dvpos;
10417
10418 /* If scrolling up, there may be some lines to display at the end of
10419 the window. */
10420 last_text_row_at_end = NULL;
10421 if (dy < 0)
10422 {
10423 /* Set last_row to the glyph row in the current matrix where the
10424 window end line is found. It has been moved up or down in
10425 the matrix by dvpos. */
10426 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
10427 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
10428
10429 /* If last_row is the window end line, it should display text. */
10430 xassert (last_row->displays_text_p);
10431
10432 /* If window end line was partially visible before, begin
10433 displaying at that line. Otherwise begin displaying with the
10434 line following it. */
10435 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
10436 {
10437 init_to_row_start (&it, w, last_row);
10438 it.vpos = last_vpos;
10439 it.current_y = last_row->y;
10440 }
10441 else
10442 {
10443 init_to_row_end (&it, w, last_row);
10444 it.vpos = 1 + last_vpos;
10445 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
10446 ++last_row;
10447 }
10448
10449 /* We may start in a continuation line. If so, we have to get
10450 the right continuation_lines_width and current_x. */
10451 it.continuation_lines_width = last_row->continuation_lines_width;
10452 it.hpos = it.current_x = 0;
10453
10454 /* Display the rest of the lines at the window end. */
10455 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
10456 while (it.current_y < it.last_visible_y
10457 && !fonts_changed_p)
10458 {
10459 /* Is it always sure that the display agrees with lines in
10460 the current matrix? I don't think so, so we mark rows
10461 displayed invalid in the current matrix by setting their
10462 enabled_p flag to zero. */
10463 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
10464 if (display_line (&it))
10465 last_text_row_at_end = it.glyph_row - 1;
10466 }
10467 }
10468
10469 /* Update window_end_pos and window_end_vpos. */
10470 if (first_unchanged_at_end_row
10471 && first_unchanged_at_end_row->y < it.last_visible_y
10472 && !last_text_row_at_end)
10473 {
10474 /* Window end line if one of the preserved rows from the current
10475 matrix. Set row to the last row displaying text in current
10476 matrix starting at first_unchanged_at_end_row, after
10477 scrolling. */
10478 xassert (first_unchanged_at_end_row->displays_text_p);
10479 row = find_last_row_displaying_text (w->current_matrix, &it,
10480 first_unchanged_at_end_row);
10481 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
10482
10483 XSETFASTINT (w->window_end_pos, Z - MATRIX_ROW_END_CHARPOS (row));
10484 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
10485 XSETFASTINT (w->window_end_vpos,
10486 MATRIX_ROW_VPOS (row, w->current_matrix));
10487 }
10488 else if (last_text_row_at_end)
10489 {
10490 XSETFASTINT (w->window_end_pos,
10491 Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
10492 w->window_end_bytepos
10493 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
10494 XSETFASTINT (w->window_end_vpos,
10495 MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
10496 }
10497 else if (last_text_row)
10498 {
10499 /* We have displayed either to the end of the window or at the
10500 end of the window, i.e. the last row with text is to be found
10501 in the desired matrix. */
10502 XSETFASTINT (w->window_end_pos,
10503 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10504 w->window_end_bytepos
10505 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10506 XSETFASTINT (w->window_end_vpos,
10507 MATRIX_ROW_VPOS (last_text_row, desired_matrix));
10508 }
10509 else if (first_unchanged_at_end_row == NULL
10510 && last_text_row == NULL
10511 && last_text_row_at_end == NULL)
10512 {
10513 /* Displayed to end of window, but no line containing text was
10514 displayed. Lines were deleted at the end of the window. */
10515 int vpos;
10516 int header_line_p = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
10517
10518 for (vpos = XFASTINT (w->window_end_vpos); vpos > 0; --vpos)
10519 if ((w->desired_matrix->rows[vpos + header_line_p].enabled_p
10520 && w->desired_matrix->rows[vpos + header_line_p].displays_text_p)
10521 || (!w->desired_matrix->rows[vpos + header_line_p].enabled_p
10522 && w->current_matrix->rows[vpos + header_line_p].displays_text_p))
10523 break;
10524
10525 w->window_end_vpos = make_number (vpos);
10526 }
10527 else
10528 abort ();
10529
10530 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
10531 debug_end_vpos = XFASTINT (w->window_end_vpos));
10532
10533 /* Record that display has not been completed. */
10534 w->window_end_valid = Qnil;
10535 w->desired_matrix->no_scrolling_p = 1;
10536 return 1;
10537 }
10538
10539
10540 \f
10541 /***********************************************************************
10542 More debugging support
10543 ***********************************************************************/
10544
10545 #if GLYPH_DEBUG
10546
10547 void dump_glyph_row P_ ((struct glyph_matrix *, int, int));
10548 static void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
10549
10550
10551 /* Dump the contents of glyph matrix MATRIX on stderr. If
10552 WITH_GLYPHS_P is non-zero, dump glyph contents as well. */
10553
10554 void
10555 dump_glyph_matrix (matrix, with_glyphs_p)
10556 struct glyph_matrix *matrix;
10557 int with_glyphs_p;
10558 {
10559 int i;
10560 for (i = 0; i < matrix->nrows; ++i)
10561 dump_glyph_row (matrix, i, with_glyphs_p);
10562 }
10563
10564
10565 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
10566 WITH_GLYPH_SP non-zero means dump glyph contents, too. */
10567
10568 void
10569 dump_glyph_row (matrix, vpos, with_glyphs_p)
10570 struct glyph_matrix *matrix;
10571 int vpos, with_glyphs_p;
10572 {
10573 struct glyph_row *row;
10574
10575 if (vpos < 0 || vpos >= matrix->nrows)
10576 return;
10577
10578 row = MATRIX_ROW (matrix, vpos);
10579
10580 fprintf (stderr, "Row Start End Used oEI><O\\CTZF X Y W\n");
10581 fprintf (stderr, "=============================================\n");
10582
10583 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d\n",
10584 row - matrix->rows,
10585 MATRIX_ROW_START_CHARPOS (row),
10586 MATRIX_ROW_END_CHARPOS (row),
10587 row->used[TEXT_AREA],
10588 row->contains_overlapping_glyphs_p,
10589 row->enabled_p,
10590 row->inverse_p,
10591 row->truncated_on_left_p,
10592 row->truncated_on_right_p,
10593 row->overlay_arrow_p,
10594 row->continued_p,
10595 MATRIX_ROW_CONTINUATION_LINE_P (row),
10596 row->displays_text_p,
10597 row->ends_at_zv_p,
10598 row->fill_line_p,
10599 row->x,
10600 row->y,
10601 row->pixel_width);
10602 fprintf (stderr, "%9d %5d\n", row->start.overlay_string_index,
10603 row->end.overlay_string_index);
10604 fprintf (stderr, "%9d %5d\n",
10605 CHARPOS (row->start.string_pos),
10606 CHARPOS (row->end.string_pos));
10607 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
10608 row->end.dpvec_index);
10609
10610 if (with_glyphs_p)
10611 {
10612 struct glyph *glyph, *glyph_end;
10613 int prev_had_glyphs_p;
10614
10615 glyph = row->glyphs[TEXT_AREA];
10616 glyph_end = glyph + row->used[TEXT_AREA];
10617
10618 /* Glyph for a line end in text. */
10619 if (glyph == glyph_end && glyph->charpos > 0)
10620 ++glyph_end;
10621
10622 if (glyph < glyph_end)
10623 {
10624 fprintf (stderr, " Glyph Type Pos W Code C Face LR\n");
10625 prev_had_glyphs_p = 1;
10626 }
10627 else
10628 prev_had_glyphs_p = 0;
10629
10630 while (glyph < glyph_end)
10631 {
10632 if (glyph->type == CHAR_GLYPH)
10633 {
10634 fprintf (stderr,
10635 " %5d %4c %6d %3d 0x%05x %c %4d %1.1d%1.1d\n",
10636 glyph - row->glyphs[TEXT_AREA],
10637 'C',
10638 glyph->charpos,
10639 glyph->pixel_width,
10640 glyph->u.ch,
10641 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
10642 ? glyph->u.ch
10643 : '.'),
10644 glyph->face_id,
10645 glyph->left_box_line_p,
10646 glyph->right_box_line_p);
10647 }
10648 else if (glyph->type == STRETCH_GLYPH)
10649 {
10650 fprintf (stderr,
10651 " %5d %4c %6d %3d 0x%05x %c %4d %1.1d%1.1d\n",
10652 glyph - row->glyphs[TEXT_AREA],
10653 'S',
10654 glyph->charpos,
10655 glyph->pixel_width,
10656 0,
10657 '.',
10658 glyph->face_id,
10659 glyph->left_box_line_p,
10660 glyph->right_box_line_p);
10661 }
10662 else if (glyph->type == IMAGE_GLYPH)
10663 {
10664 fprintf (stderr,
10665 " %5d %4c %6d %3d 0x%05x %c %4d %1.1d%1.1d\n",
10666 glyph - row->glyphs[TEXT_AREA],
10667 'I',
10668 glyph->charpos,
10669 glyph->pixel_width,
10670 glyph->u.img_id,
10671 '.',
10672 glyph->face_id,
10673 glyph->left_box_line_p,
10674 glyph->right_box_line_p);
10675 }
10676 ++glyph;
10677 }
10678 }
10679 }
10680
10681
10682 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
10683 Sdump_glyph_matrix, 0, 1, "p",
10684 "Dump the current matrix of the selected window to stderr.\n\
10685 Shows contents of glyph row structures. With non-nil optional\n\
10686 parameter WITH-GLYPHS-P, dump glyphs as well.")
10687 (with_glyphs_p)
10688 {
10689 struct window *w = XWINDOW (selected_window);
10690 struct buffer *buffer = XBUFFER (w->buffer);
10691
10692 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
10693 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
10694 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
10695 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
10696 fprintf (stderr, "=============================================\n");
10697 dump_glyph_matrix (w->current_matrix, !NILP (with_glyphs_p));
10698 return Qnil;
10699 }
10700
10701
10702 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 1, "",
10703 "Dump glyph row ROW to stderr.")
10704 (row)
10705 Lisp_Object row;
10706 {
10707 CHECK_NUMBER (row, 0);
10708 dump_glyph_row (XWINDOW (selected_window)->current_matrix, XINT (row), 1);
10709 return Qnil;
10710 }
10711
10712
10713 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row,
10714 0, 0, "", "")
10715 ()
10716 {
10717 struct frame *sf = SELECTED_FRAME ();
10718 struct glyph_matrix *m = (XWINDOW (sf->tool_bar_window)
10719 ->current_matrix);
10720 dump_glyph_row (m, 0, 1);
10721 return Qnil;
10722 }
10723
10724
10725 DEFUN ("trace-redisplay-toggle", Ftrace_redisplay_toggle,
10726 Strace_redisplay_toggle, 0, 0, "",
10727 "Toggle tracing of redisplay.")
10728 ()
10729 {
10730 trace_redisplay_p = !trace_redisplay_p;
10731 return Qnil;
10732 }
10733
10734
10735 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, 1, "",
10736 "Print STRING to stderr.")
10737 (string)
10738 Lisp_Object string;
10739 {
10740 CHECK_STRING (string, 0);
10741 fprintf (stderr, "%s", XSTRING (string)->data);
10742 return Qnil;
10743 }
10744
10745 #endif /* GLYPH_DEBUG */
10746
10747
10748 \f
10749 /***********************************************************************
10750 Building Desired Matrix Rows
10751 ***********************************************************************/
10752
10753 /* Return a temporary glyph row holding the glyphs of an overlay
10754 arrow. Only used for non-window-redisplay windows. */
10755
10756 static struct glyph_row *
10757 get_overlay_arrow_glyph_row (w)
10758 struct window *w;
10759 {
10760 struct frame *f = XFRAME (WINDOW_FRAME (w));
10761 struct buffer *buffer = XBUFFER (w->buffer);
10762 struct buffer *old = current_buffer;
10763 unsigned char *arrow_string = XSTRING (Voverlay_arrow_string)->data;
10764 int arrow_len = XSTRING (Voverlay_arrow_string)->size;
10765 unsigned char *arrow_end = arrow_string + arrow_len;
10766 unsigned char *p;
10767 struct it it;
10768 int multibyte_p;
10769 int n_glyphs_before;
10770
10771 set_buffer_temp (buffer);
10772 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
10773 it.glyph_row->used[TEXT_AREA] = 0;
10774 SET_TEXT_POS (it.position, 0, 0);
10775
10776 multibyte_p = !NILP (buffer->enable_multibyte_characters);
10777 p = arrow_string;
10778 while (p < arrow_end)
10779 {
10780 Lisp_Object face, ilisp;
10781
10782 /* Get the next character. */
10783 if (multibyte_p)
10784 it.c = string_char_and_length (p, arrow_len, &it.len);
10785 else
10786 it.c = *p, it.len = 1;
10787 p += it.len;
10788
10789 /* Get its face. */
10790 XSETFASTINT (ilisp, p - arrow_string);
10791 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
10792 it.face_id = compute_char_face (f, it.c, face);
10793
10794 /* Compute its width, get its glyphs. */
10795 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
10796 SET_TEXT_POS (it.position, -1, -1);
10797 PRODUCE_GLYPHS (&it);
10798
10799 /* If this character doesn't fit any more in the line, we have
10800 to remove some glyphs. */
10801 if (it.current_x > it.last_visible_x)
10802 {
10803 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
10804 break;
10805 }
10806 }
10807
10808 set_buffer_temp (old);
10809 return it.glyph_row;
10810 }
10811
10812
10813 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
10814 glyphs are only inserted for terminal frames since we can't really
10815 win with truncation glyphs when partially visible glyphs are
10816 involved. Which glyphs to insert is determined by
10817 produce_special_glyphs. */
10818
10819 static void
10820 insert_left_trunc_glyphs (it)
10821 struct it *it;
10822 {
10823 struct it truncate_it;
10824 struct glyph *from, *end, *to, *toend;
10825
10826 xassert (!FRAME_WINDOW_P (it->f));
10827
10828 /* Get the truncation glyphs. */
10829 truncate_it = *it;
10830 truncate_it.current_x = 0;
10831 truncate_it.face_id = DEFAULT_FACE_ID;
10832 truncate_it.glyph_row = &scratch_glyph_row;
10833 truncate_it.glyph_row->used[TEXT_AREA] = 0;
10834 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
10835 truncate_it.object = make_number (0);
10836 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
10837
10838 /* Overwrite glyphs from IT with truncation glyphs. */
10839 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
10840 end = from + truncate_it.glyph_row->used[TEXT_AREA];
10841 to = it->glyph_row->glyphs[TEXT_AREA];
10842 toend = to + it->glyph_row->used[TEXT_AREA];
10843
10844 while (from < end)
10845 *to++ = *from++;
10846
10847 /* There may be padding glyphs left over. Remove them. */
10848 from = to;
10849 while (from < toend && CHAR_GLYPH_PADDING_P (*from))
10850 ++from;
10851 while (from < toend)
10852 *to++ = *from++;
10853
10854 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
10855 }
10856
10857
10858 /* Compute the pixel height and width of IT->glyph_row.
10859
10860 Most of the time, ascent and height of a display line will be equal
10861 to the max_ascent and max_height values of the display iterator
10862 structure. This is not the case if
10863
10864 1. We hit ZV without displaying anything. In this case, max_ascent
10865 and max_height will be zero.
10866
10867 2. We have some glyphs that don't contribute to the line height.
10868 (The glyph row flag contributes_to_line_height_p is for future
10869 pixmap extensions).
10870
10871 The first case is easily covered by using default values because in
10872 these cases, the line height does not really matter, except that it
10873 must not be zero. */
10874
10875 static void
10876 compute_line_metrics (it)
10877 struct it *it;
10878 {
10879 struct glyph_row *row = it->glyph_row;
10880 int area, i;
10881
10882 if (FRAME_WINDOW_P (it->f))
10883 {
10884 int i, header_line_height;
10885
10886 /* The line may consist of one space only, that was added to
10887 place the cursor on it. If so, the row's height hasn't been
10888 computed yet. */
10889 if (row->height == 0)
10890 {
10891 if (it->max_ascent + it->max_descent == 0)
10892 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
10893 row->ascent = it->max_ascent;
10894 row->height = it->max_ascent + it->max_descent;
10895 row->phys_ascent = it->max_phys_ascent;
10896 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
10897 }
10898
10899 /* Compute the width of this line. */
10900 row->pixel_width = row->x;
10901 for (i = 0; i < row->used[TEXT_AREA]; ++i)
10902 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
10903
10904 xassert (row->pixel_width >= 0);
10905 xassert (row->ascent >= 0 && row->height > 0);
10906
10907 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
10908 || MATRIX_ROW_OVERLAPS_PRED_P (row));
10909
10910 /* If first line's physical ascent is larger than its logical
10911 ascent, use the physical ascent, and make the row taller.
10912 This makes accented characters fully visible. */
10913 if (row == it->w->desired_matrix->rows
10914 && row->phys_ascent > row->ascent)
10915 {
10916 row->height += row->phys_ascent - row->ascent;
10917 row->ascent = row->phys_ascent;
10918 }
10919
10920 /* Compute how much of the line is visible. */
10921 row->visible_height = row->height;
10922
10923 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (it->w);
10924 if (row->y < header_line_height)
10925 row->visible_height -= header_line_height - row->y;
10926 else
10927 {
10928 int max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
10929 if (row->y + row->height > max_y)
10930 row->visible_height -= row->y + row->height - max_y;
10931 }
10932 }
10933 else
10934 {
10935 row->pixel_width = row->used[TEXT_AREA];
10936 row->ascent = row->phys_ascent = 0;
10937 row->height = row->phys_height = row->visible_height = 1;
10938 }
10939
10940 /* Compute a hash code for this row. */
10941 row->hash = 0;
10942 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
10943 for (i = 0; i < row->used[area]; ++i)
10944 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
10945 + row->glyphs[area][i].u.val
10946 + row->glyphs[area][i].face_id
10947 + row->glyphs[area][i].padding_p
10948 + (row->glyphs[area][i].type << 2));
10949
10950 it->max_ascent = it->max_descent = 0;
10951 it->max_phys_ascent = it->max_phys_descent = 0;
10952 }
10953
10954
10955 /* Append one space to the glyph row of iterator IT if doing a
10956 window-based redisplay. DEFAULT_FACE_P non-zero means let the
10957 space have the default face, otherwise let it have the same face as
10958 IT->face_id. Value is non-zero if a space was added.
10959
10960 This function is called to make sure that there is always one glyph
10961 at the end of a glyph row that the cursor can be set on under
10962 window-systems. (If there weren't such a glyph we would not know
10963 how wide and tall a box cursor should be displayed).
10964
10965 At the same time this space let's a nicely handle clearing to the
10966 end of the line if the row ends in italic text. */
10967
10968 static int
10969 append_space (it, default_face_p)
10970 struct it *it;
10971 int default_face_p;
10972 {
10973 if (FRAME_WINDOW_P (it->f))
10974 {
10975 int n = it->glyph_row->used[TEXT_AREA];
10976
10977 if (it->glyph_row->glyphs[TEXT_AREA] + n
10978 < it->glyph_row->glyphs[1 + TEXT_AREA])
10979 {
10980 /* Save some values that must not be changed. */
10981 int saved_x = it->current_x;
10982 struct text_pos saved_pos;
10983 int saved_what = it->what;
10984 int saved_face_id = it->face_id;
10985 Lisp_Object saved_object;
10986 struct face *face;
10987
10988 saved_object = it->object;
10989 saved_pos = it->position;
10990
10991 it->what = IT_CHARACTER;
10992 bzero (&it->position, sizeof it->position);
10993 it->object = make_number (0);
10994 it->c = ' ';
10995 it->len = 1;
10996
10997 if (default_face_p)
10998 it->face_id = DEFAULT_FACE_ID;
10999 face = FACE_FROM_ID (it->f, it->face_id);
11000 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
11001
11002 PRODUCE_GLYPHS (it);
11003
11004 it->current_x = saved_x;
11005 it->object = saved_object;
11006 it->position = saved_pos;
11007 it->what = saved_what;
11008 it->face_id = saved_face_id;
11009 return 1;
11010 }
11011 }
11012
11013 return 0;
11014 }
11015
11016
11017 /* Extend the face of the last glyph in the text area of IT->glyph_row
11018 to the end of the display line. Called from display_line.
11019 If the glyph row is empty, add a space glyph to it so that we
11020 know the face to draw. Set the glyph row flag fill_line_p. */
11021
11022 static void
11023 extend_face_to_end_of_line (it)
11024 struct it *it;
11025 {
11026 struct face *face;
11027 struct frame *f = it->f;
11028
11029 /* If line is already filled, do nothing. */
11030 if (it->current_x >= it->last_visible_x)
11031 return;
11032
11033 /* Face extension extends the background and box of IT->face_id
11034 to the end of the line. If the background equals the background
11035 of the frame, we haven't to do anything. */
11036 face = FACE_FROM_ID (f, it->face_id);
11037 if (FRAME_WINDOW_P (f)
11038 && face->box == FACE_NO_BOX
11039 && face->background == FRAME_BACKGROUND_PIXEL (f)
11040 && !face->stipple)
11041 return;
11042
11043 /* Set the glyph row flag indicating that the face of the last glyph
11044 in the text area has to be drawn to the end of the text area. */
11045 it->glyph_row->fill_line_p = 1;
11046
11047 /* If current character of IT is not ASCII, make sure we have the
11048 ASCII face. This will be automatically undone the next time
11049 get_next_display_element returns a multibyte character. Note
11050 that the character will always be single byte in unibyte text. */
11051 if (!SINGLE_BYTE_CHAR_P (it->c))
11052 {
11053 it->face_id = FACE_FOR_CHAR (f, face, 0);
11054 }
11055
11056 if (FRAME_WINDOW_P (f))
11057 {
11058 /* If the row is empty, add a space with the current face of IT,
11059 so that we know which face to draw. */
11060 if (it->glyph_row->used[TEXT_AREA] == 0)
11061 {
11062 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
11063 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
11064 it->glyph_row->used[TEXT_AREA] = 1;
11065 }
11066 }
11067 else
11068 {
11069 /* Save some values that must not be changed. */
11070 int saved_x = it->current_x;
11071 struct text_pos saved_pos;
11072 Lisp_Object saved_object;
11073 int saved_what = it->what;
11074
11075 saved_object = it->object;
11076 saved_pos = it->position;
11077
11078 it->what = IT_CHARACTER;
11079 bzero (&it->position, sizeof it->position);
11080 it->object = make_number (0);
11081 it->c = ' ';
11082 it->len = 1;
11083
11084 PRODUCE_GLYPHS (it);
11085
11086 while (it->current_x <= it->last_visible_x)
11087 PRODUCE_GLYPHS (it);
11088
11089 /* Don't count these blanks really. It would let us insert a left
11090 truncation glyph below and make us set the cursor on them, maybe. */
11091 it->current_x = saved_x;
11092 it->object = saved_object;
11093 it->position = saved_pos;
11094 it->what = saved_what;
11095 }
11096 }
11097
11098
11099 /* Value is non-zero if text starting at CHARPOS in current_buffer is
11100 trailing whitespace. */
11101
11102 static int
11103 trailing_whitespace_p (charpos)
11104 int charpos;
11105 {
11106 int bytepos = CHAR_TO_BYTE (charpos);
11107 int c = 0;
11108
11109 while (bytepos < ZV_BYTE
11110 && (c = FETCH_CHAR (bytepos),
11111 c == ' ' || c == '\t'))
11112 ++bytepos;
11113
11114 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
11115 {
11116 if (bytepos != PT_BYTE)
11117 return 1;
11118 }
11119 return 0;
11120 }
11121
11122
11123 /* Highlight trailing whitespace, if any, in ROW. */
11124
11125 void
11126 highlight_trailing_whitespace (f, row)
11127 struct frame *f;
11128 struct glyph_row *row;
11129 {
11130 int used = row->used[TEXT_AREA];
11131
11132 if (used)
11133 {
11134 struct glyph *start = row->glyphs[TEXT_AREA];
11135 struct glyph *glyph = start + used - 1;
11136
11137 /* Skip over the space glyph inserted to display the
11138 cursor at the end of a line. */
11139 if (glyph->type == CHAR_GLYPH
11140 && glyph->u.ch == ' '
11141 && INTEGERP (glyph->object))
11142 --glyph;
11143
11144 /* If last glyph is a space or stretch, and it's trailing
11145 whitespace, set the face of all trailing whitespace glyphs in
11146 IT->glyph_row to `trailing-whitespace'. */
11147 if (glyph >= start
11148 && BUFFERP (glyph->object)
11149 && (glyph->type == STRETCH_GLYPH
11150 || (glyph->type == CHAR_GLYPH
11151 && glyph->u.ch == ' '))
11152 && trailing_whitespace_p (glyph->charpos))
11153 {
11154 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
11155
11156 while (glyph >= start
11157 && BUFFERP (glyph->object)
11158 && (glyph->type == STRETCH_GLYPH
11159 || (glyph->type == CHAR_GLYPH
11160 && glyph->u.ch == ' ')))
11161 (glyph--)->face_id = face_id;
11162 }
11163 }
11164 }
11165
11166
11167 /* Construct the glyph row IT->glyph_row in the desired matrix of
11168 IT->w from text at the current position of IT. See dispextern.h
11169 for an overview of struct it. Value is non-zero if
11170 IT->glyph_row displays text, as opposed to a line displaying ZV
11171 only. */
11172
11173 static int
11174 display_line (it)
11175 struct it *it;
11176 {
11177 struct glyph_row *row = it->glyph_row;
11178
11179 /* We always start displaying at hpos zero even if hscrolled. */
11180 xassert (it->hpos == 0 && it->current_x == 0);
11181
11182 /* We must not display in a row that's not a text row. */
11183 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
11184 < it->w->desired_matrix->nrows);
11185
11186 /* Is IT->w showing the region? */
11187 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
11188
11189 /* Clear the result glyph row and enable it. */
11190 prepare_desired_row (row);
11191
11192 row->y = it->current_y;
11193 row->start = it->current;
11194 row->continuation_lines_width = it->continuation_lines_width;
11195 row->displays_text_p = 1;
11196
11197 /* Arrange the overlays nicely for our purposes. Usually, we call
11198 display_line on only one line at a time, in which case this
11199 can't really hurt too much, or we call it on lines which appear
11200 one after another in the buffer, in which case all calls to
11201 recenter_overlay_lists but the first will be pretty cheap. */
11202 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
11203
11204 /* Move over display elements that are not visible because we are
11205 hscrolled. This may stop at an x-position < IT->first_visible_x
11206 if the first glyph is partially visible or if we hit a line end. */
11207 if (it->current_x < it->first_visible_x)
11208 move_it_in_display_line_to (it, ZV, it->first_visible_x,
11209 MOVE_TO_POS | MOVE_TO_X);
11210
11211 /* Get the initial row height. This is either the height of the
11212 text hscrolled, if there is any, or zero. */
11213 row->ascent = it->max_ascent;
11214 row->height = it->max_ascent + it->max_descent;
11215 row->phys_ascent = it->max_phys_ascent;
11216 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
11217
11218 /* Loop generating characters. The loop is left with IT on the next
11219 character to display. */
11220 while (1)
11221 {
11222 int n_glyphs_before, hpos_before, x_before;
11223 int x, i, nglyphs;
11224
11225 /* Retrieve the next thing to display. Value is zero if end of
11226 buffer reached. */
11227 if (!get_next_display_element (it))
11228 {
11229 /* Maybe add a space at the end of this line that is used to
11230 display the cursor there under X. Set the charpos of the
11231 first glyph of blank lines not corresponding to any text
11232 to -1. */
11233 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
11234 || row->used[TEXT_AREA] == 0)
11235 {
11236 row->glyphs[TEXT_AREA]->charpos = -1;
11237 row->displays_text_p = 0;
11238
11239 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines))
11240 row->indicate_empty_line_p = 1;
11241 }
11242
11243 it->continuation_lines_width = 0;
11244 row->ends_at_zv_p = 1;
11245 break;
11246 }
11247
11248 /* Now, get the metrics of what we want to display. This also
11249 generates glyphs in `row' (which is IT->glyph_row). */
11250 n_glyphs_before = row->used[TEXT_AREA];
11251 x = it->current_x;
11252 PRODUCE_GLYPHS (it);
11253
11254 /* If this display element was in marginal areas, continue with
11255 the next one. */
11256 if (it->area != TEXT_AREA)
11257 {
11258 row->ascent = max (row->ascent, it->max_ascent);
11259 row->height = max (row->height, it->max_ascent + it->max_descent);
11260 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
11261 row->phys_height = max (row->phys_height,
11262 it->max_phys_ascent + it->max_phys_descent);
11263 set_iterator_to_next (it);
11264 continue;
11265 }
11266
11267 /* Does the display element fit on the line? If we truncate
11268 lines, we should draw past the right edge of the window. If
11269 we don't truncate, we want to stop so that we can display the
11270 continuation glyph before the right margin. If lines are
11271 continued, there are two possible strategies for characters
11272 resulting in more than 1 glyph (e.g. tabs): Display as many
11273 glyphs as possible in this line and leave the rest for the
11274 continuation line, or display the whole element in the next
11275 line. Original redisplay did the former, so we do it also. */
11276 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11277 hpos_before = it->hpos;
11278 x_before = x;
11279
11280 if (nglyphs == 1
11281 && it->current_x < it->last_visible_x)
11282 {
11283 ++it->hpos;
11284 row->ascent = max (row->ascent, it->max_ascent);
11285 row->height = max (row->height, it->max_ascent + it->max_descent);
11286 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
11287 row->phys_height = max (row->phys_height,
11288 it->max_phys_ascent + it->max_phys_descent);
11289 if (it->current_x - it->pixel_width < it->first_visible_x)
11290 row->x = x - it->first_visible_x;
11291 }
11292 else
11293 {
11294 int new_x;
11295 struct glyph *glyph;
11296
11297 for (i = 0; i < nglyphs; ++i, x = new_x)
11298 {
11299 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11300 new_x = x + glyph->pixel_width;
11301
11302 if (/* Lines are continued. */
11303 !it->truncate_lines_p
11304 && (/* Glyph doesn't fit on the line. */
11305 new_x > it->last_visible_x
11306 /* Or it fits exactly on a window system frame. */
11307 || (new_x == it->last_visible_x
11308 && FRAME_WINDOW_P (it->f))))
11309 {
11310 /* End of a continued line. */
11311
11312 if (it->hpos == 0
11313 || (new_x == it->last_visible_x
11314 && FRAME_WINDOW_P (it->f)))
11315 {
11316 /* Current glyph fits exactly on the line. We
11317 must continue the line because we can't draw
11318 the cursor after the glyph. */
11319 row->continued_p = 1;
11320 it->current_x = new_x;
11321 it->continuation_lines_width += new_x;
11322 ++it->hpos;
11323 if (i == nglyphs - 1)
11324 set_iterator_to_next (it);
11325 }
11326 else
11327 {
11328 /* Display element draws past the right edge of
11329 the window. Restore positions to values
11330 before the element. The next line starts
11331 with current_x before the glyph that could
11332 not be displayed, so that TAB works right. */
11333 row->used[TEXT_AREA] = n_glyphs_before + i;
11334
11335 /* Display continuation glyphs. */
11336 if (!FRAME_WINDOW_P (it->f))
11337 produce_special_glyphs (it, IT_CONTINUATION);
11338 row->continued_p = 1;
11339
11340 it->current_x = x;
11341 it->continuation_lines_width += x;
11342 }
11343 break;
11344 }
11345 else if (new_x > it->first_visible_x)
11346 {
11347 /* Increment number of glyphs actually displayed. */
11348 ++it->hpos;
11349
11350 if (x < it->first_visible_x)
11351 /* Glyph is partially visible, i.e. row starts at
11352 negative X position. */
11353 row->x = x - it->first_visible_x;
11354 }
11355 else
11356 {
11357 /* Glyph is completely off the left margin of the
11358 window. This should not happen because of the
11359 move_it_in_display_line at the start of
11360 this function. */
11361 abort ();
11362 }
11363 }
11364
11365 row->ascent = max (row->ascent, it->max_ascent);
11366 row->height = max (row->height, it->max_ascent + it->max_descent);
11367 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
11368 row->phys_height = max (row->phys_height,
11369 it->max_phys_ascent + it->max_phys_descent);
11370
11371 /* End of this display line if row is continued. */
11372 if (row->continued_p)
11373 break;
11374 }
11375
11376 /* Is this a line end? If yes, we're also done, after making
11377 sure that a non-default face is extended up to the right
11378 margin of the window. */
11379 if (ITERATOR_AT_END_OF_LINE_P (it))
11380 {
11381 int used_before = row->used[TEXT_AREA];
11382
11383 /* Add a space at the end of the line that is used to
11384 display the cursor there. */
11385 append_space (it, 0);
11386
11387 /* Extend the face to the end of the line. */
11388 extend_face_to_end_of_line (it);
11389
11390 /* Make sure we have the position. */
11391 if (used_before == 0)
11392 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
11393
11394 /* Consume the line end. This skips over invisible lines. */
11395 set_iterator_to_next (it);
11396 it->continuation_lines_width = 0;
11397 break;
11398 }
11399
11400 /* Proceed with next display element. Note that this skips
11401 over lines invisible because of selective display. */
11402 set_iterator_to_next (it);
11403
11404 /* If we truncate lines, we are done when the last displayed
11405 glyphs reach past the right margin of the window. */
11406 if (it->truncate_lines_p
11407 && (FRAME_WINDOW_P (it->f)
11408 ? (it->current_x >= it->last_visible_x)
11409 : (it->current_x > it->last_visible_x)))
11410 {
11411 /* Maybe add truncation glyphs. */
11412 if (!FRAME_WINDOW_P (it->f))
11413 {
11414 --it->glyph_row->used[TEXT_AREA];
11415 produce_special_glyphs (it, IT_TRUNCATION);
11416 }
11417
11418 row->truncated_on_right_p = 1;
11419 it->continuation_lines_width = 0;
11420 reseat_at_next_visible_line_start (it, 0);
11421 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
11422 it->hpos = hpos_before;
11423 it->current_x = x_before;
11424 break;
11425 }
11426 }
11427
11428 /* If line is not empty and hscrolled, maybe insert truncation glyphs
11429 at the left window margin. */
11430 if (it->first_visible_x
11431 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
11432 {
11433 if (!FRAME_WINDOW_P (it->f))
11434 insert_left_trunc_glyphs (it);
11435 row->truncated_on_left_p = 1;
11436 }
11437
11438 /* If the start of this line is the overlay arrow-position, then
11439 mark this glyph row as the one containing the overlay arrow.
11440 This is clearly a mess with variable size fonts. It would be
11441 better to let it be displayed like cursors under X. */
11442 if (MARKERP (Voverlay_arrow_position)
11443 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
11444 && (MATRIX_ROW_START_CHARPOS (row)
11445 == marker_position (Voverlay_arrow_position))
11446 && STRINGP (Voverlay_arrow_string)
11447 && ! overlay_arrow_seen)
11448 {
11449 /* Overlay arrow in window redisplay is a bitmap. */
11450 if (!FRAME_WINDOW_P (it->f))
11451 {
11452 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
11453 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
11454 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
11455 struct glyph *p = row->glyphs[TEXT_AREA];
11456 struct glyph *p2, *end;
11457
11458 /* Copy the arrow glyphs. */
11459 while (glyph < arrow_end)
11460 *p++ = *glyph++;
11461
11462 /* Throw away padding glyphs. */
11463 p2 = p;
11464 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
11465 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
11466 ++p2;
11467 if (p2 > p)
11468 {
11469 while (p2 < end)
11470 *p++ = *p2++;
11471 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
11472 }
11473 }
11474
11475 overlay_arrow_seen = 1;
11476 row->overlay_arrow_p = 1;
11477 }
11478
11479 /* Compute pixel dimensions of this line. */
11480 compute_line_metrics (it);
11481
11482 /* Remember the position at which this line ends. */
11483 row->end = it->current;
11484
11485 /* Maybe set the cursor. If you change this, it's probably a good
11486 idea to also change the code in redisplay_window for cursor
11487 movement in an unchanged window. */
11488 if (it->w->cursor.vpos < 0
11489 && PT >= MATRIX_ROW_START_CHARPOS (row)
11490 && MATRIX_ROW_END_CHARPOS (row) >= PT
11491 && !(MATRIX_ROW_END_CHARPOS (row) == PT
11492 && (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
11493 || !row->ends_at_zv_p)))
11494 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
11495
11496 /* Highlight trailing whitespace. */
11497 if (!NILP (Vshow_trailing_whitespace))
11498 highlight_trailing_whitespace (it->f, it->glyph_row);
11499
11500 /* Prepare for the next line. This line starts horizontally at (X
11501 HPOS) = (0 0). Vertical positions are incremented. As a
11502 convenience for the caller, IT->glyph_row is set to the next
11503 row to be used. */
11504 it->current_x = it->hpos = 0;
11505 it->current_y += row->height;
11506 ++it->vpos;
11507 ++it->glyph_row;
11508 return row->displays_text_p;
11509 }
11510
11511
11512 \f
11513 /***********************************************************************
11514 Menu Bar
11515 ***********************************************************************/
11516
11517 /* Redisplay the menu bar in the frame for window W.
11518
11519 The menu bar of X frames that don't have X toolkit support is
11520 displayed in a special window W->frame->menu_bar_window.
11521
11522 The menu bar of terminal frames is treated specially as far as
11523 glyph matrices are concerned. Menu bar lines are not part of
11524 windows, so the update is done directly on the frame matrix rows
11525 for the menu bar. */
11526
11527 static void
11528 display_menu_bar (w)
11529 struct window *w;
11530 {
11531 struct frame *f = XFRAME (WINDOW_FRAME (w));
11532 struct it it;
11533 Lisp_Object items;
11534 int i;
11535
11536 /* Don't do all this for graphical frames. */
11537 #ifdef HAVE_NTGUI
11538 if (!NILP (Vwindow_system))
11539 return;
11540 #endif
11541 #ifdef USE_X_TOOLKIT
11542 if (FRAME_X_P (f))
11543 return;
11544 #endif
11545
11546 #ifdef USE_X_TOOLKIT
11547 xassert (!FRAME_WINDOW_P (f));
11548 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
11549 it.first_visible_x = 0;
11550 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
11551 #else /* not USE_X_TOOLKIT */
11552 if (FRAME_WINDOW_P (f))
11553 {
11554 /* Menu bar lines are displayed in the desired matrix of the
11555 dummy window menu_bar_window. */
11556 struct window *menu_w;
11557 xassert (WINDOWP (f->menu_bar_window));
11558 menu_w = XWINDOW (f->menu_bar_window);
11559 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
11560 MENU_FACE_ID);
11561 it.first_visible_x = 0;
11562 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
11563 }
11564 else
11565 {
11566 /* This is a TTY frame, i.e. character hpos/vpos are used as
11567 pixel x/y. */
11568 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
11569 MENU_FACE_ID);
11570 it.first_visible_x = 0;
11571 it.last_visible_x = FRAME_WIDTH (f);
11572 }
11573 #endif /* not USE_X_TOOLKIT */
11574
11575 /* Clear all rows of the menu bar. */
11576 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
11577 {
11578 struct glyph_row *row = it.glyph_row + i;
11579 clear_glyph_row (row);
11580 row->enabled_p = 1;
11581 row->full_width_p = 1;
11582 }
11583
11584 /* Make the first line of the menu bar appear in reverse video. */
11585 it.glyph_row->inverse_p = mode_line_inverse_video != 0;
11586
11587 /* Display all items of the menu bar. */
11588 items = FRAME_MENU_BAR_ITEMS (it.f);
11589 for (i = 0; i < XVECTOR (items)->size; i += 4)
11590 {
11591 Lisp_Object string;
11592
11593 /* Stop at nil string. */
11594 string = XVECTOR (items)->contents[i + 1];
11595 if (NILP (string))
11596 break;
11597
11598 /* Remember where item was displayed. */
11599 XSETFASTINT (XVECTOR (items)->contents[i + 3], it.hpos);
11600
11601 /* Display the item, pad with one space. */
11602 if (it.current_x < it.last_visible_x)
11603 display_string (NULL, string, Qnil, 0, 0, &it,
11604 XSTRING (string)->size + 1, 0, 0, -1);
11605 }
11606
11607 /* Fill out the line with spaces. */
11608 if (it.current_x < it.last_visible_x)
11609 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
11610
11611 /* Compute the total height of the lines. */
11612 compute_line_metrics (&it);
11613 }
11614
11615
11616 \f
11617 /***********************************************************************
11618 Mode Line
11619 ***********************************************************************/
11620
11621 /* Display the mode and/or top line of window W. */
11622
11623 static void
11624 display_mode_lines (w)
11625 struct window *w;
11626 {
11627 /* These will be set while the mode line specs are processed. */
11628 line_number_displayed = 0;
11629 w->column_number_displayed = Qnil;
11630
11631 if (WINDOW_WANTS_MODELINE_P (w))
11632 display_mode_line (w, MODE_LINE_FACE_ID,
11633 current_buffer->mode_line_format);
11634
11635 if (WINDOW_WANTS_HEADER_LINE_P (w))
11636 display_mode_line (w, HEADER_LINE_FACE_ID,
11637 current_buffer->header_line_format);
11638 }
11639
11640
11641 /* Display mode or top line of window W. FACE_ID specifies which line
11642 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
11643 FORMAT is the mode line format to display. */
11644
11645 static void
11646 display_mode_line (w, face_id, format)
11647 struct window *w;
11648 enum face_id face_id;
11649 Lisp_Object format;
11650 {
11651 struct it it;
11652 struct face *face;
11653
11654 init_iterator (&it, w, -1, -1, NULL, face_id);
11655 prepare_desired_row (it.glyph_row);
11656
11657 /* Temporarily make frame's keyboard the current kboard so that
11658 kboard-local variables in the mode_line_format will get the right
11659 values. */
11660 push_frame_kboard (it.f);
11661 display_mode_element (&it, 0, 0, 0, format);
11662 pop_frame_kboard ();
11663
11664 /* Fill up with spaces. */
11665 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
11666
11667 compute_line_metrics (&it);
11668 it.glyph_row->full_width_p = 1;
11669 it.glyph_row->mode_line_p = 1;
11670 it.glyph_row->inverse_p = mode_line_inverse_video != 0;
11671 it.glyph_row->continued_p = 0;
11672 it.glyph_row->truncated_on_left_p = 0;
11673 it.glyph_row->truncated_on_right_p = 0;
11674
11675 /* Make a 3D mode-line have a shadow at its right end. */
11676 face = FACE_FROM_ID (it.f, face_id);
11677 extend_face_to_end_of_line (&it);
11678 if (face->box != FACE_NO_BOX)
11679 {
11680 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
11681 + it.glyph_row->used[TEXT_AREA] - 1);
11682 last->right_box_line_p = 1;
11683 }
11684 }
11685
11686
11687 /* Contribute ELT to the mode line for window IT->w. How it
11688 translates into text depends on its data type.
11689
11690 IT describes the display environment in which we display, as usual.
11691
11692 DEPTH is the depth in recursion. It is used to prevent
11693 infinite recursion here.
11694
11695 FIELD_WIDTH is the number of characters the display of ELT should
11696 occupy in the mode line, and PRECISION is the maximum number of
11697 characters to display from ELT's representation. See
11698 display_string for details. *
11699
11700 Returns the hpos of the end of the text generated by ELT. */
11701
11702 static int
11703 display_mode_element (it, depth, field_width, precision, elt)
11704 struct it *it;
11705 int depth;
11706 int field_width, precision;
11707 Lisp_Object elt;
11708 {
11709 int n = 0, field, prec;
11710
11711 tail_recurse:
11712 if (depth > 10)
11713 goto invalid;
11714
11715 depth++;
11716
11717 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
11718 {
11719 case Lisp_String:
11720 {
11721 /* A string: output it and check for %-constructs within it. */
11722 unsigned char c;
11723 unsigned char *this = XSTRING (elt)->data;
11724 unsigned char *lisp_string = this;
11725
11726 while ((precision <= 0 || n < precision)
11727 && *this
11728 && (frame_title_ptr
11729 || it->current_x < it->last_visible_x))
11730 {
11731 unsigned char *last = this;
11732
11733 /* Advance to end of string or next format specifier. */
11734 while ((c = *this++) != '\0' && c != '%')
11735 ;
11736
11737 if (this - 1 != last)
11738 {
11739 /* Output to end of string or up to '%'. Field width
11740 is length of string. Don't output more than
11741 PRECISION allows us. */
11742 prec = --this - last;
11743 if (precision > 0 && prec > precision - n)
11744 prec = precision - n;
11745
11746 if (frame_title_ptr)
11747 n += store_frame_title (last, prec, prec);
11748 else
11749 n += display_string (NULL, elt, Qnil, 0, last - lisp_string,
11750 it, 0, prec, 0, -1);
11751 }
11752 else /* c == '%' */
11753 {
11754 unsigned char *percent_position = this;
11755
11756 /* Get the specified minimum width. Zero means
11757 don't pad. */
11758 field = 0;
11759 while ((c = *this++) >= '0' && c <= '9')
11760 field = field * 10 + c - '0';
11761
11762 /* Don't pad beyond the total padding allowed. */
11763 if (field_width - n > 0 && field > field_width - n)
11764 field = field_width - n;
11765
11766 /* Note that either PRECISION <= 0 or N < PRECISION. */
11767 prec = precision - n;
11768
11769 if (c == 'M')
11770 n += display_mode_element (it, depth, field, prec,
11771 Vglobal_mode_string);
11772 else if (c != 0)
11773 {
11774 unsigned char *spec
11775 = decode_mode_spec (it->w, c, field, prec);
11776
11777 if (frame_title_ptr)
11778 n += store_frame_title (spec, field, prec);
11779 else
11780 {
11781 int nglyphs_before
11782 = it->glyph_row->used[TEXT_AREA];
11783 int charpos
11784 = percent_position - XSTRING (elt)->data;
11785 int nwritten
11786 = display_string (spec, Qnil, elt, charpos, 0, it,
11787 field, prec, 0, -1);
11788
11789 /* Assign to the glyphs written above the
11790 string where the `%x' came from, position
11791 of the `%'. */
11792 if (nwritten > 0)
11793 {
11794 struct glyph *glyph
11795 = (it->glyph_row->glyphs[TEXT_AREA]
11796 + nglyphs_before);
11797 int i;
11798
11799 for (i = 0; i < nwritten; ++i)
11800 {
11801 glyph[i].object = elt;
11802 glyph[i].charpos = charpos;
11803 }
11804
11805 n += nwritten;
11806 }
11807 }
11808 }
11809 }
11810 }
11811 }
11812 break;
11813
11814 case Lisp_Symbol:
11815 /* A symbol: process the value of the symbol recursively
11816 as if it appeared here directly. Avoid error if symbol void.
11817 Special case: if value of symbol is a string, output the string
11818 literally. */
11819 {
11820 register Lisp_Object tem;
11821 tem = Fboundp (elt);
11822 if (!NILP (tem))
11823 {
11824 tem = Fsymbol_value (elt);
11825 /* If value is a string, output that string literally:
11826 don't check for % within it. */
11827 if (STRINGP (tem))
11828 {
11829 prec = XSTRING (tem)->size;
11830 if (precision > 0 && prec > precision - n)
11831 prec = precision - n;
11832 if (frame_title_ptr)
11833 n += store_frame_title (XSTRING (tem)->data, -1, prec);
11834 else
11835 n += display_string (NULL, tem, Qnil, 0, 0, it,
11836 0, prec, 0, -1);
11837 }
11838 else if (!EQ (tem, elt))
11839 {
11840 /* Give up right away for nil or t. */
11841 elt = tem;
11842 goto tail_recurse;
11843 }
11844 }
11845 }
11846 break;
11847
11848 case Lisp_Cons:
11849 {
11850 register Lisp_Object car, tem;
11851
11852 /* A cons cell: three distinct cases.
11853 If first element is a string or a cons, process all the elements
11854 and effectively concatenate them.
11855 If first element is a negative number, truncate displaying cdr to
11856 at most that many characters. If positive, pad (with spaces)
11857 to at least that many characters.
11858 If first element is a symbol, process the cadr or caddr recursively
11859 according to whether the symbol's value is non-nil or nil. */
11860 car = XCAR (elt);
11861 if (EQ (car, QCeval) && CONSP (XCDR (elt)))
11862 {
11863 /* An element of the form (:eval FORM) means evaluate FORM
11864 and use the result as mode line elements. */
11865 struct gcpro gcpro1;
11866 Lisp_Object spec;
11867
11868 spec = eval_form (XCAR (XCDR (elt)));
11869 GCPRO1 (spec);
11870 n += display_mode_element (it, depth, field_width - n,
11871 precision - n, spec);
11872 UNGCPRO;
11873 }
11874 else if (SYMBOLP (car))
11875 {
11876 tem = Fboundp (car);
11877 elt = XCDR (elt);
11878 if (!CONSP (elt))
11879 goto invalid;
11880 /* elt is now the cdr, and we know it is a cons cell.
11881 Use its car if CAR has a non-nil value. */
11882 if (!NILP (tem))
11883 {
11884 tem = Fsymbol_value (car);
11885 if (!NILP (tem))
11886 {
11887 elt = XCAR (elt);
11888 goto tail_recurse;
11889 }
11890 }
11891 /* Symbol's value is nil (or symbol is unbound)
11892 Get the cddr of the original list
11893 and if possible find the caddr and use that. */
11894 elt = XCDR (elt);
11895 if (NILP (elt))
11896 break;
11897 else if (!CONSP (elt))
11898 goto invalid;
11899 elt = XCAR (elt);
11900 goto tail_recurse;
11901 }
11902 else if (INTEGERP (car))
11903 {
11904 register int lim = XINT (car);
11905 elt = XCDR (elt);
11906 if (lim < 0)
11907 {
11908 /* Negative int means reduce maximum width. */
11909 if (precision <= 0)
11910 precision = -lim;
11911 else
11912 precision = min (precision, -lim);
11913 }
11914 else if (lim > 0)
11915 {
11916 /* Padding specified. Don't let it be more than
11917 current maximum. */
11918 if (precision > 0)
11919 lim = min (precision, lim);
11920
11921 /* If that's more padding than already wanted, queue it.
11922 But don't reduce padding already specified even if
11923 that is beyond the current truncation point. */
11924 field_width = max (lim, field_width);
11925 }
11926 goto tail_recurse;
11927 }
11928 else if (STRINGP (car) || CONSP (car))
11929 {
11930 register int limit = 50;
11931 /* Limit is to protect against circular lists. */
11932 while (CONSP (elt)
11933 && --limit > 0
11934 && (precision <= 0 || n < precision))
11935 {
11936 n += display_mode_element (it, depth, field_width - n,
11937 precision - n, XCAR (elt));
11938 elt = XCDR (elt);
11939 }
11940 }
11941 }
11942 break;
11943
11944 default:
11945 invalid:
11946 if (frame_title_ptr)
11947 n += store_frame_title ("*invalid*", 0, precision - n);
11948 else
11949 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
11950 precision - n, 0, 0);
11951 return n;
11952 }
11953
11954 /* Pad to FIELD_WIDTH. */
11955 if (field_width > 0 && n < field_width)
11956 {
11957 if (frame_title_ptr)
11958 n += store_frame_title ("", field_width - n, 0);
11959 else
11960 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
11961 0, 0, 0);
11962 }
11963
11964 return n;
11965 }
11966
11967
11968 /* Write a null-terminated, right justified decimal representation of
11969 the positive integer D to BUF using a minimal field width WIDTH. */
11970
11971 static void
11972 pint2str (buf, width, d)
11973 register char *buf;
11974 register int width;
11975 register int d;
11976 {
11977 register char *p = buf;
11978
11979 if (d <= 0)
11980 *p++ = '0';
11981 else
11982 {
11983 while (d > 0)
11984 {
11985 *p++ = d % 10 + '0';
11986 d /= 10;
11987 }
11988 }
11989
11990 for (width -= (int) (p - buf); width > 0; --width)
11991 *p++ = ' ';
11992 *p-- = '\0';
11993 while (p > buf)
11994 {
11995 d = *buf;
11996 *buf++ = *p;
11997 *p-- = d;
11998 }
11999 }
12000
12001 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
12002 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
12003 type of CODING_SYSTEM. Return updated pointer into BUF. */
12004
12005 static unsigned char invalid_eol_type[] = "(*invalid*)";
12006
12007 static char *
12008 decode_mode_spec_coding (coding_system, buf, eol_flag)
12009 Lisp_Object coding_system;
12010 register char *buf;
12011 int eol_flag;
12012 {
12013 Lisp_Object val;
12014 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
12015 unsigned char *eol_str;
12016 int eol_str_len;
12017 /* The EOL conversion we are using. */
12018 Lisp_Object eoltype;
12019
12020 val = Fget (coding_system, Qcoding_system);
12021
12022 if (!VECTORP (val)) /* Not yet decided. */
12023 {
12024 if (multibyte)
12025 *buf++ = '-';
12026 if (eol_flag)
12027 eoltype = eol_mnemonic_undecided;
12028 /* Don't mention EOL conversion if it isn't decided. */
12029 }
12030 else
12031 {
12032 Lisp_Object eolvalue;
12033
12034 eolvalue = Fget (coding_system, Qeol_type);
12035
12036 if (multibyte)
12037 *buf++ = XFASTINT (XVECTOR (val)->contents[1]);
12038
12039 if (eol_flag)
12040 {
12041 /* The EOL conversion that is normal on this system. */
12042
12043 if (NILP (eolvalue)) /* Not yet decided. */
12044 eoltype = eol_mnemonic_undecided;
12045 else if (VECTORP (eolvalue)) /* Not yet decided. */
12046 eoltype = eol_mnemonic_undecided;
12047 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
12048 eoltype = (XFASTINT (eolvalue) == 0
12049 ? eol_mnemonic_unix
12050 : (XFASTINT (eolvalue) == 1
12051 ? eol_mnemonic_dos : eol_mnemonic_mac));
12052 }
12053 }
12054
12055 if (eol_flag)
12056 {
12057 /* Mention the EOL conversion if it is not the usual one. */
12058 if (STRINGP (eoltype))
12059 {
12060 eol_str = XSTRING (eoltype)->data;
12061 eol_str_len = XSTRING (eoltype)->size;
12062 }
12063 else if (INTEGERP (eoltype)
12064 && CHAR_VALID_P (XINT (eoltype), 0))
12065 {
12066 eol_str = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
12067 eol_str_len = CHAR_STRING (XINT (eoltype), eol_str);
12068 }
12069 else
12070 {
12071 eol_str = invalid_eol_type;
12072 eol_str_len = sizeof (invalid_eol_type) - 1;
12073 }
12074 bcopy (eol_str, buf, eol_str_len);
12075 buf += eol_str_len;
12076 }
12077
12078 return buf;
12079 }
12080
12081 /* Return a string for the output of a mode line %-spec for window W,
12082 generated by character C. PRECISION >= 0 means don't return a
12083 string longer than that value. FIELD_WIDTH > 0 means pad the
12084 string returned with spaces to that value. */
12085
12086 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
12087
12088 static char *
12089 decode_mode_spec (w, c, field_width, precision)
12090 struct window *w;
12091 register int c;
12092 int field_width, precision;
12093 {
12094 Lisp_Object obj;
12095 struct frame *f = XFRAME (WINDOW_FRAME (w));
12096 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
12097 struct buffer *b = XBUFFER (w->buffer);
12098
12099 obj = Qnil;
12100
12101 switch (c)
12102 {
12103 case '*':
12104 if (!NILP (b->read_only))
12105 return "%";
12106 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
12107 return "*";
12108 return "-";
12109
12110 case '+':
12111 /* This differs from %* only for a modified read-only buffer. */
12112 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
12113 return "*";
12114 if (!NILP (b->read_only))
12115 return "%";
12116 return "-";
12117
12118 case '&':
12119 /* This differs from %* in ignoring read-only-ness. */
12120 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
12121 return "*";
12122 return "-";
12123
12124 case '%':
12125 return "%";
12126
12127 case '[':
12128 {
12129 int i;
12130 char *p;
12131
12132 if (command_loop_level > 5)
12133 return "[[[... ";
12134 p = decode_mode_spec_buf;
12135 for (i = 0; i < command_loop_level; i++)
12136 *p++ = '[';
12137 *p = 0;
12138 return decode_mode_spec_buf;
12139 }
12140
12141 case ']':
12142 {
12143 int i;
12144 char *p;
12145
12146 if (command_loop_level > 5)
12147 return " ...]]]";
12148 p = decode_mode_spec_buf;
12149 for (i = 0; i < command_loop_level; i++)
12150 *p++ = ']';
12151 *p = 0;
12152 return decode_mode_spec_buf;
12153 }
12154
12155 case '-':
12156 {
12157 register int i;
12158
12159 /* Let lots_of_dashes be a string of infinite length. */
12160 if (field_width <= 0
12161 || field_width > sizeof (lots_of_dashes))
12162 {
12163 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
12164 decode_mode_spec_buf[i] = '-';
12165 decode_mode_spec_buf[i] = '\0';
12166 return decode_mode_spec_buf;
12167 }
12168 else
12169 return lots_of_dashes;
12170 }
12171
12172 case 'b':
12173 obj = b->name;
12174 break;
12175
12176 case 'c':
12177 {
12178 int col = current_column ();
12179 XSETFASTINT (w->column_number_displayed, col);
12180 pint2str (decode_mode_spec_buf, field_width, col);
12181 return decode_mode_spec_buf;
12182 }
12183
12184 case 'F':
12185 /* %F displays the frame name. */
12186 if (!NILP (f->title))
12187 return (char *) XSTRING (f->title)->data;
12188 if (f->explicit_name || ! FRAME_WINDOW_P (f))
12189 return (char *) XSTRING (f->name)->data;
12190 return "Emacs";
12191
12192 case 'f':
12193 obj = b->filename;
12194 break;
12195
12196 case 'l':
12197 {
12198 int startpos = XMARKER (w->start)->charpos;
12199 int startpos_byte = marker_byte_position (w->start);
12200 int line, linepos, linepos_byte, topline;
12201 int nlines, junk;
12202 int height = XFASTINT (w->height);
12203
12204 /* If we decided that this buffer isn't suitable for line numbers,
12205 don't forget that too fast. */
12206 if (EQ (w->base_line_pos, w->buffer))
12207 goto no_value;
12208 /* But do forget it, if the window shows a different buffer now. */
12209 else if (BUFFERP (w->base_line_pos))
12210 w->base_line_pos = Qnil;
12211
12212 /* If the buffer is very big, don't waste time. */
12213 if (BUF_ZV (b) - BUF_BEGV (b) > line_number_display_limit)
12214 {
12215 w->base_line_pos = Qnil;
12216 w->base_line_number = Qnil;
12217 goto no_value;
12218 }
12219
12220 if (!NILP (w->base_line_number)
12221 && !NILP (w->base_line_pos)
12222 && XFASTINT (w->base_line_pos) <= startpos)
12223 {
12224 line = XFASTINT (w->base_line_number);
12225 linepos = XFASTINT (w->base_line_pos);
12226 linepos_byte = buf_charpos_to_bytepos (b, linepos);
12227 }
12228 else
12229 {
12230 line = 1;
12231 linepos = BUF_BEGV (b);
12232 linepos_byte = BUF_BEGV_BYTE (b);
12233 }
12234
12235 /* Count lines from base line to window start position. */
12236 nlines = display_count_lines (linepos, linepos_byte,
12237 startpos_byte,
12238 startpos, &junk);
12239
12240 topline = nlines + line;
12241
12242 /* Determine a new base line, if the old one is too close
12243 or too far away, or if we did not have one.
12244 "Too close" means it's plausible a scroll-down would
12245 go back past it. */
12246 if (startpos == BUF_BEGV (b))
12247 {
12248 XSETFASTINT (w->base_line_number, topline);
12249 XSETFASTINT (w->base_line_pos, BUF_BEGV (b));
12250 }
12251 else if (nlines < height + 25 || nlines > height * 3 + 50
12252 || linepos == BUF_BEGV (b))
12253 {
12254 int limit = BUF_BEGV (b);
12255 int limit_byte = BUF_BEGV_BYTE (b);
12256 int position;
12257 int distance = (height * 2 + 30) * line_number_display_limit_width;
12258
12259 if (startpos - distance > limit)
12260 {
12261 limit = startpos - distance;
12262 limit_byte = CHAR_TO_BYTE (limit);
12263 }
12264
12265 nlines = display_count_lines (startpos, startpos_byte,
12266 limit_byte,
12267 - (height * 2 + 30),
12268 &position);
12269 /* If we couldn't find the lines we wanted within
12270 line_number_display_limit_width chars per line,
12271 give up on line numbers for this window. */
12272 if (position == limit_byte && limit == startpos - distance)
12273 {
12274 w->base_line_pos = w->buffer;
12275 w->base_line_number = Qnil;
12276 goto no_value;
12277 }
12278
12279 XSETFASTINT (w->base_line_number, topline - nlines);
12280 XSETFASTINT (w->base_line_pos, BYTE_TO_CHAR (position));
12281 }
12282
12283 /* Now count lines from the start pos to point. */
12284 nlines = display_count_lines (startpos, startpos_byte,
12285 PT_BYTE, PT, &junk);
12286
12287 /* Record that we did display the line number. */
12288 line_number_displayed = 1;
12289
12290 /* Make the string to show. */
12291 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
12292 return decode_mode_spec_buf;
12293 no_value:
12294 {
12295 char* p = decode_mode_spec_buf;
12296 int pad = field_width - 2;
12297 while (pad-- > 0)
12298 *p++ = ' ';
12299 *p++ = '?';
12300 *p = '?';
12301 return decode_mode_spec_buf;
12302 }
12303 }
12304 break;
12305
12306 case 'm':
12307 obj = b->mode_name;
12308 break;
12309
12310 case 'n':
12311 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
12312 return " Narrow";
12313 break;
12314
12315 case 'p':
12316 {
12317 int pos = marker_position (w->start);
12318 int total = BUF_ZV (b) - BUF_BEGV (b);
12319
12320 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
12321 {
12322 if (pos <= BUF_BEGV (b))
12323 return "All";
12324 else
12325 return "Bottom";
12326 }
12327 else if (pos <= BUF_BEGV (b))
12328 return "Top";
12329 else
12330 {
12331 if (total > 1000000)
12332 /* Do it differently for a large value, to avoid overflow. */
12333 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
12334 else
12335 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
12336 /* We can't normally display a 3-digit number,
12337 so get us a 2-digit number that is close. */
12338 if (total == 100)
12339 total = 99;
12340 sprintf (decode_mode_spec_buf, "%2d%%", total);
12341 return decode_mode_spec_buf;
12342 }
12343 }
12344
12345 /* Display percentage of size above the bottom of the screen. */
12346 case 'P':
12347 {
12348 int toppos = marker_position (w->start);
12349 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
12350 int total = BUF_ZV (b) - BUF_BEGV (b);
12351
12352 if (botpos >= BUF_ZV (b))
12353 {
12354 if (toppos <= BUF_BEGV (b))
12355 return "All";
12356 else
12357 return "Bottom";
12358 }
12359 else
12360 {
12361 if (total > 1000000)
12362 /* Do it differently for a large value, to avoid overflow. */
12363 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
12364 else
12365 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
12366 /* We can't normally display a 3-digit number,
12367 so get us a 2-digit number that is close. */
12368 if (total == 100)
12369 total = 99;
12370 if (toppos <= BUF_BEGV (b))
12371 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
12372 else
12373 sprintf (decode_mode_spec_buf, "%2d%%", total);
12374 return decode_mode_spec_buf;
12375 }
12376 }
12377
12378 case 's':
12379 /* status of process */
12380 obj = Fget_buffer_process (w->buffer);
12381 if (NILP (obj))
12382 return "no process";
12383 #ifdef subprocesses
12384 obj = Fsymbol_name (Fprocess_status (obj));
12385 #endif
12386 break;
12387
12388 case 't': /* indicate TEXT or BINARY */
12389 #ifdef MODE_LINE_BINARY_TEXT
12390 return MODE_LINE_BINARY_TEXT (b);
12391 #else
12392 return "T";
12393 #endif
12394
12395 case 'z':
12396 /* coding-system (not including end-of-line format) */
12397 case 'Z':
12398 /* coding-system (including end-of-line type) */
12399 {
12400 int eol_flag = (c == 'Z');
12401 char *p = decode_mode_spec_buf;
12402
12403 if (! FRAME_WINDOW_P (f))
12404 {
12405 /* No need to mention EOL here--the terminal never needs
12406 to do EOL conversion. */
12407 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
12408 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
12409 }
12410 p = decode_mode_spec_coding (b->buffer_file_coding_system,
12411 p, eol_flag);
12412
12413 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
12414 #ifdef subprocesses
12415 obj = Fget_buffer_process (Fcurrent_buffer ());
12416 if (PROCESSP (obj))
12417 {
12418 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
12419 p, eol_flag);
12420 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
12421 p, eol_flag);
12422 }
12423 #endif /* subprocesses */
12424 #endif /* 0 */
12425 *p = 0;
12426 return decode_mode_spec_buf;
12427 }
12428 }
12429
12430 if (STRINGP (obj))
12431 return (char *) XSTRING (obj)->data;
12432 else
12433 return "";
12434 }
12435
12436
12437 /* Count up to COUNT lines starting from START / START_BYTE.
12438 But don't go beyond LIMIT_BYTE.
12439 Return the number of lines thus found (always nonnegative).
12440
12441 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
12442
12443 static int
12444 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
12445 int start, start_byte, limit_byte, count;
12446 int *byte_pos_ptr;
12447 {
12448 register unsigned char *cursor;
12449 unsigned char *base;
12450
12451 register int ceiling;
12452 register unsigned char *ceiling_addr;
12453 int orig_count = count;
12454
12455 /* If we are not in selective display mode,
12456 check only for newlines. */
12457 int selective_display = (!NILP (current_buffer->selective_display)
12458 && !INTEGERP (current_buffer->selective_display));
12459
12460 if (count > 0)
12461 {
12462 while (start_byte < limit_byte)
12463 {
12464 ceiling = BUFFER_CEILING_OF (start_byte);
12465 ceiling = min (limit_byte - 1, ceiling);
12466 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
12467 base = (cursor = BYTE_POS_ADDR (start_byte));
12468 while (1)
12469 {
12470 if (selective_display)
12471 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
12472 ;
12473 else
12474 while (*cursor != '\n' && ++cursor != ceiling_addr)
12475 ;
12476
12477 if (cursor != ceiling_addr)
12478 {
12479 if (--count == 0)
12480 {
12481 start_byte += cursor - base + 1;
12482 *byte_pos_ptr = start_byte;
12483 return orig_count;
12484 }
12485 else
12486 if (++cursor == ceiling_addr)
12487 break;
12488 }
12489 else
12490 break;
12491 }
12492 start_byte += cursor - base;
12493 }
12494 }
12495 else
12496 {
12497 while (start_byte > limit_byte)
12498 {
12499 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
12500 ceiling = max (limit_byte, ceiling);
12501 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
12502 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
12503 while (1)
12504 {
12505 if (selective_display)
12506 while (--cursor != ceiling_addr
12507 && *cursor != '\n' && *cursor != 015)
12508 ;
12509 else
12510 while (--cursor != ceiling_addr && *cursor != '\n')
12511 ;
12512
12513 if (cursor != ceiling_addr)
12514 {
12515 if (++count == 0)
12516 {
12517 start_byte += cursor - base + 1;
12518 *byte_pos_ptr = start_byte;
12519 /* When scanning backwards, we should
12520 not count the newline posterior to which we stop. */
12521 return - orig_count - 1;
12522 }
12523 }
12524 else
12525 break;
12526 }
12527 /* Here we add 1 to compensate for the last decrement
12528 of CURSOR, which took it past the valid range. */
12529 start_byte += cursor - base + 1;
12530 }
12531 }
12532
12533 *byte_pos_ptr = limit_byte;
12534
12535 if (count < 0)
12536 return - orig_count + count;
12537 return orig_count - count;
12538
12539 }
12540
12541
12542 \f
12543 /***********************************************************************
12544 Displaying strings
12545 ***********************************************************************/
12546
12547 /* Display a NUL-terminated string, starting with index START.
12548
12549 If STRING is non-null, display that C string. Otherwise, the Lisp
12550 string LISP_STRING is displayed.
12551
12552 If FACE_STRING is not nil, FACE_STRING_POS is a position in
12553 FACE_STRING. Display STRING or LISP_STRING with the face at
12554 FACE_STRING_POS in FACE_STRING:
12555
12556 Display the string in the environment given by IT, but use the
12557 standard display table, temporarily.
12558
12559 FIELD_WIDTH is the minimum number of output glyphs to produce.
12560 If STRING has fewer characters than FIELD_WIDTH, pad to the right
12561 with spaces. If STRING has more characters, more than FIELD_WIDTH
12562 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
12563
12564 PRECISION is the maximum number of characters to output from
12565 STRING. PRECISION < 0 means don't truncate the string.
12566
12567 This is roughly equivalent to printf format specifiers:
12568
12569 FIELD_WIDTH PRECISION PRINTF
12570 ----------------------------------------
12571 -1 -1 %s
12572 -1 10 %.10s
12573 10 -1 %10s
12574 20 10 %20.10s
12575
12576 MULTIBYTE zero means do not display multibyte chars, > 0 means do
12577 display them, and < 0 means obey the current buffer's value of
12578 enable_multibyte_characters.
12579
12580 Value is the number of glyphs produced. */
12581
12582 static int
12583 display_string (string, lisp_string, face_string, face_string_pos,
12584 start, it, field_width, precision, max_x, multibyte)
12585 unsigned char *string;
12586 Lisp_Object lisp_string;
12587 Lisp_Object face_string;
12588 int face_string_pos;
12589 int start;
12590 struct it *it;
12591 int field_width, precision, max_x;
12592 int multibyte;
12593 {
12594 int hpos_at_start = it->hpos;
12595 int saved_face_id = it->face_id;
12596 struct glyph_row *row = it->glyph_row;
12597
12598 /* Initialize the iterator IT for iteration over STRING beginning
12599 with index START. We assume that IT may be modified here (which
12600 means that display_line has to do something when displaying a
12601 mini-buffer prompt, which it does). */
12602 reseat_to_string (it, string, lisp_string, start,
12603 precision, field_width, multibyte);
12604
12605 /* If displaying STRING, set up the face of the iterator
12606 from LISP_STRING, if that's given. */
12607 if (STRINGP (face_string))
12608 {
12609 int endptr;
12610 struct face *face;
12611
12612 it->face_id
12613 = face_at_string_position (it->w, face_string, face_string_pos,
12614 0, it->region_beg_charpos,
12615 it->region_end_charpos,
12616 &endptr, it->base_face_id);
12617 face = FACE_FROM_ID (it->f, it->face_id);
12618 it->face_box_p = face->box != FACE_NO_BOX;
12619 }
12620
12621 /* Set max_x to the maximum allowed X position. Don't let it go
12622 beyond the right edge of the window. */
12623 if (max_x <= 0)
12624 max_x = it->last_visible_x;
12625 else
12626 max_x = min (max_x, it->last_visible_x);
12627
12628 /* Skip over display elements that are not visible. because IT->w is
12629 hscrolled. */
12630 if (it->current_x < it->first_visible_x)
12631 move_it_in_display_line_to (it, 100000, it->first_visible_x,
12632 MOVE_TO_POS | MOVE_TO_X);
12633
12634 row->ascent = it->max_ascent;
12635 row->height = it->max_ascent + it->max_descent;
12636 row->phys_ascent = it->max_phys_ascent;
12637 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
12638
12639 /* This condition is for the case that we are called with current_x
12640 past last_visible_x. */
12641 while (it->current_x < max_x)
12642 {
12643 int x_before, x, n_glyphs_before, i, nglyphs;
12644
12645 /* Get the next display element. */
12646 if (!get_next_display_element (it))
12647 break;
12648
12649 /* Produce glyphs. */
12650 x_before = it->current_x;
12651 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
12652 PRODUCE_GLYPHS (it);
12653
12654 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
12655 i = 0;
12656 x = x_before;
12657 while (i < nglyphs)
12658 {
12659 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12660
12661 if (!it->truncate_lines_p
12662 && x + glyph->pixel_width > max_x)
12663 {
12664 /* End of continued line or max_x reached. */
12665 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
12666 it->current_x = x;
12667 break;
12668 }
12669 else if (x + glyph->pixel_width > it->first_visible_x)
12670 {
12671 /* Glyph is at least partially visible. */
12672 ++it->hpos;
12673 if (x < it->first_visible_x)
12674 it->glyph_row->x = x - it->first_visible_x;
12675 }
12676 else
12677 {
12678 /* Glyph is off the left margin of the display area.
12679 Should not happen. */
12680 abort ();
12681 }
12682
12683 row->ascent = max (row->ascent, it->max_ascent);
12684 row->height = max (row->height, it->max_ascent + it->max_descent);
12685 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12686 row->phys_height = max (row->phys_height,
12687 it->max_phys_ascent + it->max_phys_descent);
12688 x += glyph->pixel_width;
12689 ++i;
12690 }
12691
12692 /* Stop if max_x reached. */
12693 if (i < nglyphs)
12694 break;
12695
12696 /* Stop at line ends. */
12697 if (ITERATOR_AT_END_OF_LINE_P (it))
12698 {
12699 it->continuation_lines_width = 0;
12700 break;
12701 }
12702
12703 set_iterator_to_next (it);
12704
12705 /* Stop if truncating at the right edge. */
12706 if (it->truncate_lines_p
12707 && it->current_x >= it->last_visible_x)
12708 {
12709 /* Add truncation mark, but don't do it if the line is
12710 truncated at a padding space. */
12711 if (IT_CHARPOS (*it) < it->string_nchars)
12712 {
12713 if (!FRAME_WINDOW_P (it->f))
12714 produce_special_glyphs (it, IT_TRUNCATION);
12715 it->glyph_row->truncated_on_right_p = 1;
12716 }
12717 break;
12718 }
12719 }
12720
12721 /* Maybe insert a truncation at the left. */
12722 if (it->first_visible_x
12723 && IT_CHARPOS (*it) > 0)
12724 {
12725 if (!FRAME_WINDOW_P (it->f))
12726 insert_left_trunc_glyphs (it);
12727 it->glyph_row->truncated_on_left_p = 1;
12728 }
12729
12730 it->face_id = saved_face_id;
12731
12732 /* Value is number of columns displayed. */
12733 return it->hpos - hpos_at_start;
12734 }
12735
12736
12737 \f
12738 /* This is like a combination of memq and assq. Return 1 if PROPVAL
12739 appears as an element of LIST or as the car of an element of LIST.
12740 If PROPVAL is a list, compare each element against LIST in that
12741 way, and return 1 if any element of PROPVAL is found in LIST.
12742 Otherwise return 0. This function cannot quit. */
12743
12744 int
12745 invisible_p (propval, list)
12746 register Lisp_Object propval;
12747 Lisp_Object list;
12748 {
12749 register Lisp_Object tail, proptail;
12750 for (tail = list; CONSP (tail); tail = XCDR (tail))
12751 {
12752 register Lisp_Object tem;
12753 tem = XCAR (tail);
12754 if (EQ (propval, tem))
12755 return 1;
12756 if (CONSP (tem) && EQ (propval, XCAR (tem)))
12757 return 1;
12758 }
12759 if (CONSP (propval))
12760 for (proptail = propval; CONSP (proptail);
12761 proptail = XCDR (proptail))
12762 {
12763 Lisp_Object propelt;
12764 propelt = XCAR (proptail);
12765 for (tail = list; CONSP (tail); tail = XCDR (tail))
12766 {
12767 register Lisp_Object tem;
12768 tem = XCAR (tail);
12769 if (EQ (propelt, tem))
12770 return 1;
12771 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
12772 return 1;
12773 }
12774 }
12775 return 0;
12776 }
12777
12778
12779 /* Return 1 if PROPVAL appears as the car of an element of LIST and
12780 the cdr of that element is non-nil. If PROPVAL is a list, check
12781 each element of PROPVAL in that way, and the first time some
12782 element is found, return 1 if the cdr of that element is non-nil.
12783 Otherwise return 0. This function cannot quit. */
12784
12785 int
12786 invisible_ellipsis_p (propval, list)
12787 register Lisp_Object propval;
12788 Lisp_Object list;
12789 {
12790 register Lisp_Object tail, proptail;
12791
12792 for (tail = list; CONSP (tail); tail = XCDR (tail))
12793 {
12794 register Lisp_Object tem;
12795 tem = XCAR (tail);
12796 if (CONSP (tem) && EQ (propval, XCAR (tem)))
12797 return ! NILP (XCDR (tem));
12798 }
12799
12800 if (CONSP (propval))
12801 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
12802 {
12803 Lisp_Object propelt;
12804 propelt = XCAR (proptail);
12805 for (tail = list; CONSP (tail); tail = XCDR (tail))
12806 {
12807 register Lisp_Object tem;
12808 tem = XCAR (tail);
12809 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
12810 return ! NILP (XCDR (tem));
12811 }
12812 }
12813
12814 return 0;
12815 }
12816
12817
12818 \f
12819 /***********************************************************************
12820 Initialization
12821 ***********************************************************************/
12822
12823 void
12824 syms_of_xdisp ()
12825 {
12826 Vwith_echo_area_save_vector = Qnil;
12827 staticpro (&Vwith_echo_area_save_vector);
12828
12829 Vmessage_stack = Qnil;
12830 staticpro (&Vmessage_stack);
12831
12832 Qinhibit_redisplay = intern ("inhibit-redisplay");
12833 staticpro (&Qinhibit_redisplay);
12834
12835 #if GLYPH_DEBUG
12836 defsubr (&Sdump_glyph_matrix);
12837 defsubr (&Sdump_glyph_row);
12838 defsubr (&Sdump_tool_bar_row);
12839 defsubr (&Strace_redisplay_toggle);
12840 defsubr (&Strace_to_stderr);
12841 #endif
12842
12843 staticpro (&Qmenu_bar_update_hook);
12844 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
12845
12846 staticpro (&Qoverriding_terminal_local_map);
12847 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
12848
12849 staticpro (&Qoverriding_local_map);
12850 Qoverriding_local_map = intern ("overriding-local-map");
12851
12852 staticpro (&Qwindow_scroll_functions);
12853 Qwindow_scroll_functions = intern ("window-scroll-functions");
12854
12855 staticpro (&Qredisplay_end_trigger_functions);
12856 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
12857
12858 staticpro (&Qinhibit_point_motion_hooks);
12859 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
12860
12861 QCdata = intern (":data");
12862 staticpro (&QCdata);
12863 Qdisplay = intern ("display");
12864 staticpro (&Qdisplay);
12865 Qspace_width = intern ("space-width");
12866 staticpro (&Qspace_width);
12867 Qraise = intern ("raise");
12868 staticpro (&Qraise);
12869 Qspace = intern ("space");
12870 staticpro (&Qspace);
12871 Qmargin = intern ("margin");
12872 staticpro (&Qmargin);
12873 Qleft_margin = intern ("left-margin");
12874 staticpro (&Qleft_margin);
12875 Qright_margin = intern ("right-margin");
12876 staticpro (&Qright_margin);
12877 Qalign_to = intern ("align-to");
12878 staticpro (&Qalign_to);
12879 QCalign_to = intern (":align-to");
12880 staticpro (&QCalign_to);
12881 Qrelative_width = intern ("relative-width");
12882 staticpro (&Qrelative_width);
12883 QCrelative_width = intern (":relative-width");
12884 staticpro (&QCrelative_width);
12885 QCrelative_height = intern (":relative-height");
12886 staticpro (&QCrelative_height);
12887 QCeval = intern (":eval");
12888 staticpro (&QCeval);
12889 Qwhen = intern ("when");
12890 staticpro (&Qwhen);
12891 QCfile = intern (":file");
12892 staticpro (&QCfile);
12893 Qfontified = intern ("fontified");
12894 staticpro (&Qfontified);
12895 Qfontification_functions = intern ("fontification-functions");
12896 staticpro (&Qfontification_functions);
12897 Qtrailing_whitespace = intern ("trailing-whitespace");
12898 staticpro (&Qtrailing_whitespace);
12899 Qimage = intern ("image");
12900 staticpro (&Qimage);
12901
12902 last_arrow_position = Qnil;
12903 last_arrow_string = Qnil;
12904 staticpro (&last_arrow_position);
12905 staticpro (&last_arrow_string);
12906
12907 echo_buffer[0] = echo_buffer[1] = Qnil;
12908 staticpro (&echo_buffer[0]);
12909 staticpro (&echo_buffer[1]);
12910
12911 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
12912 staticpro (&echo_area_buffer[0]);
12913 staticpro (&echo_area_buffer[1]);
12914
12915 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
12916 "Non-nil means highlight trailing whitespace.\n\
12917 The face used for trailing whitespace is `trailing-whitespace'.");
12918 Vshow_trailing_whitespace = Qnil;
12919
12920 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
12921 "Non-nil means don't actually do any redisplay.\n\
12922 This is used for internal purposes.");
12923 Vinhibit_redisplay = Qnil;
12924
12925 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
12926 "String (or mode line construct) included (normally) in `mode-line-format'.");
12927 Vglobal_mode_string = Qnil;
12928
12929 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
12930 "Marker for where to display an arrow on top of the buffer text.\n\
12931 This must be the beginning of a line in order to work.\n\
12932 See also `overlay-arrow-string'.");
12933 Voverlay_arrow_position = Qnil;
12934
12935 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
12936 "String to display as an arrow. See also `overlay-arrow-position'.");
12937 Voverlay_arrow_string = Qnil;
12938
12939 DEFVAR_INT ("scroll-step", &scroll_step,
12940 "*The number of lines to try scrolling a window by when point moves out.\n\
12941 If that fails to bring point back on frame, point is centered instead.\n\
12942 If this is zero, point is always centered after it moves off frame.");
12943
12944 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
12945 "*Scroll up to this many lines, to bring point back on screen.\n\
12946 A value of zero means to scroll the text to center point vertically\n\
12947 in the window.");
12948 scroll_conservatively = 0;
12949
12950 DEFVAR_INT ("scroll-margin", &scroll_margin,
12951 "*Number of lines of margin at the top and bottom of a window.\n\
12952 Recenter the window whenever point gets within this many lines\n\
12953 of the top or bottom of the window.");
12954 scroll_margin = 0;
12955
12956 #if GLYPH_DEBUG
12957 DEFVAR_INT ("debug-end-pos", &debug_end_pos, "Don't ask");
12958 #endif
12959
12960 DEFVAR_BOOL ("truncate-partial-width-windows",
12961 &truncate_partial_width_windows,
12962 "*Non-nil means truncate lines in all windows less than full frame wide.");
12963 truncate_partial_width_windows = 1;
12964
12965 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
12966 "*Non-nil means use inverse video for the mode line.");
12967 mode_line_inverse_video = 1;
12968
12969 DEFVAR_INT ("line-number-display-limit", &line_number_display_limit,
12970 "*Maximum buffer size for which line number should be displayed.\n\
12971 If the buffer is bigger than this, the line number does not appear\n\
12972 in the mode line.");
12973 line_number_display_limit = 1000000;
12974
12975 DEFVAR_INT ("line-number-display-limit-width", &line_number_display_limit_width,
12976 "*Maximum line width (in characters) for line number display.\n\
12977 If the average length of the lines near point is bigger than this, then the\n\
12978 line number may be omitted from the mode line.");
12979 line_number_display_limit_width = 200;
12980
12981 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
12982 "*Non-nil means highlight region even in nonselected windows.");
12983 highlight_nonselected_windows = 0;
12984
12985 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
12986 "Non-nil if more than one frame is visible on this display.\n\
12987 Minibuffer-only frames don't count, but iconified frames do.\n\
12988 This variable is not guaranteed to be accurate except while processing\n\
12989 `frame-title-format' and `icon-title-format'.");
12990
12991 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
12992 "Template for displaying the title bar of visible frames.\n\
12993 \(Assuming the window manager supports this feature.)\n\
12994 This variable has the same structure as `mode-line-format' (which see),\n\
12995 and is used only on frames for which no explicit name has been set\n\
12996 \(see `modify-frame-parameters').");
12997 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
12998 "Template for displaying the title bar of an iconified frame.\n\
12999 \(Assuming the window manager supports this feature.)\n\
13000 This variable has the same structure as `mode-line-format' (which see),\n\
13001 and is used only on frames for which no explicit name has been set\n\
13002 \(see `modify-frame-parameters').");
13003 Vicon_title_format
13004 = Vframe_title_format
13005 = Fcons (intern ("multiple-frames"),
13006 Fcons (build_string ("%b"),
13007 Fcons (Fcons (build_string (""),
13008 Fcons (intern ("invocation-name"),
13009 Fcons (build_string ("@"),
13010 Fcons (intern ("system-name"),
13011 Qnil)))),
13012 Qnil)));
13013
13014 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
13015 "Maximum number of lines to keep in the message log buffer.\n\
13016 If nil, disable message logging. If t, log messages but don't truncate\n\
13017 the buffer when it becomes large.");
13018 XSETFASTINT (Vmessage_log_max, 50);
13019
13020 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
13021 "Functions called before redisplay, if window sizes have changed.\n\
13022 The value should be a list of functions that take one argument.\n\
13023 Just before redisplay, for each frame, if any of its windows have changed\n\
13024 size since the last redisplay, or have been split or deleted,\n\
13025 all the functions in the list are called, with the frame as argument.");
13026 Vwindow_size_change_functions = Qnil;
13027
13028 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
13029 "List of Functions to call before redisplaying a window with scrolling.\n\
13030 Each function is called with two arguments, the window\n\
13031 and its new display-start position. Note that the value of `window-end'\n\
13032 is not valid when these functions are called.");
13033 Vwindow_scroll_functions = Qnil;
13034
13035 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
13036 "*Non-nil means automatically resize tool-bars.\n\
13037 This increases a tool-bar's height if not all tool-bar items are visible.\n\
13038 It decreases a tool-bar's height when it would display blank lines\n\
13039 otherwise.");
13040 auto_resize_tool_bars_p = 1;
13041
13042 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
13043 "*Non-nil means raise tool-bar buttons when the mouse moves over them.");
13044 auto_raise_tool_bar_buttons_p = 1;
13045
13046 DEFVAR_INT ("tool-bar-button-margin", &tool_bar_button_margin,
13047 "*Margin around tool-bar buttons in pixels.");
13048 tool_bar_button_margin = 1;
13049
13050 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
13051 "Relief thickness of tool-bar buttons.");
13052 tool_bar_button_relief = 3;
13053
13054 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
13055 "List of functions to call to fontify regions of text.\n\
13056 Each function is called with one argument POS. Functions must\n\
13057 fontify a region starting at POS in the current buffer, and give\n\
13058 fontified regions the property `fontified'.\n\
13059 This variable automatically becomes buffer-local when set.");
13060 Vfontification_functions = Qnil;
13061 Fmake_local_variable (Qfontification_functions);
13062
13063 DEFVAR_BOOL ("unibyte-display-via-language-environment",
13064 &unibyte_display_via_language_environment,
13065 "*Non-nil means display unibyte text according to language environment.\n\
13066 Specifically this means that unibyte non-ASCII characters\n\
13067 are displayed by converting them to the equivalent multibyte characters\n\
13068 according to the current language environment. As a result, they are\n\
13069 displayed according to the current fontset.");
13070 unibyte_display_via_language_environment = 0;
13071
13072 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
13073 "*Maximum height for resizing mini-windows.\n\
13074 If a float, it specifies a fraction of the mini-window frame's height.\n\
13075 If an integer, it specifies a number of lines.\n\
13076 If nil, don't resize.");
13077 Vmax_mini_window_height = make_float (0.25);
13078
13079 DEFVAR_BOOL ("cursor-in-non-selected-windows",
13080 &cursor_in_non_selected_windows,
13081 "*Non-nil means display a hollow cursor in non-selected windows.\n\
13082 Nil means don't display a cursor there.");
13083 cursor_in_non_selected_windows = 1;
13084
13085 DEFVAR_BOOL ("automatic-hscrolling", &automatic_hscrolling_p,
13086 "*Non-nil means scroll the display automatically to make point visible.");
13087 automatic_hscrolling_p = 1;
13088 }
13089
13090
13091 /* Initialize this module when Emacs starts. */
13092
13093 void
13094 init_xdisp ()
13095 {
13096 Lisp_Object root_window;
13097 struct window *mini_w;
13098
13099 CHARPOS (this_line_start_pos) = 0;
13100
13101 mini_w = XWINDOW (minibuf_window);
13102 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
13103
13104 if (!noninteractive)
13105 {
13106 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
13107 int i;
13108
13109 XSETFASTINT (XWINDOW (root_window)->top, FRAME_TOP_MARGIN (f));
13110 set_window_height (root_window,
13111 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
13112 0);
13113 XSETFASTINT (mini_w->top, FRAME_HEIGHT (f) - 1);
13114 set_window_height (minibuf_window, 1, 0);
13115
13116 XSETFASTINT (XWINDOW (root_window)->width, FRAME_WIDTH (f));
13117 XSETFASTINT (mini_w->width, FRAME_WIDTH (f));
13118
13119 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
13120 scratch_glyph_row.glyphs[TEXT_AREA + 1]
13121 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
13122
13123 /* The default ellipsis glyphs `...'. */
13124 for (i = 0; i < 3; ++i)
13125 XSETFASTINT (default_invis_vector[i], '.');
13126 }
13127
13128 #ifdef HAVE_WINDOW_SYSTEM
13129 {
13130 /* Allocate the buffer for frame titles. */
13131 int size = 100;
13132 frame_title_buf = (char *) xmalloc (size);
13133 frame_title_buf_end = frame_title_buf + size;
13134 frame_title_ptr = NULL;
13135 }
13136 #endif /* HAVE_WINDOW_SYSTEM */
13137 }
13138
13139