]> code.delx.au - gnu-emacs/blob - src/xdisp.c
(try_window_id): Reset first_unchanged_at_end_row
[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 #ifdef STDC_HEADERS
173 #include <stdlib.h>
174 #endif
175 #include "lisp.h"
176 #include "frame.h"
177 #include "window.h"
178 #include "termchar.h"
179 #include "dispextern.h"
180 #include "buffer.h"
181 #include "charset.h"
182 #include "indent.h"
183 #include "commands.h"
184 #include "macros.h"
185 #include "disptab.h"
186 #include "termhooks.h"
187 #include "intervals.h"
188 #include "keyboard.h"
189 #include "coding.h"
190 #include "process.h"
191 #include "region-cache.h"
192
193 #ifdef HAVE_X_WINDOWS
194 #include "xterm.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, QCwhen;
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 toolbar buttons raised when the mouse moves
231 over them. */
232
233 int auto_raise_toolbar_buttons_p;
234
235 /* Margin around toolbar buttons in pixels. */
236
237 int toolbar_button_margin;
238
239 /* Thickness of shadow to draw around toolbar buttons. */
240
241 int toolbar_button_relief;
242
243 /* Non-zero means automatically resize toolbars so that all toolbar
244 items are visible, and no blank lines remain. */
245
246 int auto_resize_toolbars_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, Qwidth, Qalign_to;
255 extern Lisp_Object Qface, Qinvisible, Qimage;
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, Qheight, Qraise;
261
262 /* Non-nil means highlight trailing whitespace. */
263
264 Lisp_Object Vshow_trailing_whitespace;
265
266 /* Name of the face used to highlight trailing whitespace. */
267
268 Lisp_Object Qtrailing_whitespace;
269
270 /* The symbol `image' which is the car of the lists used to represent
271 images in Lisp. */
272
273 Lisp_Object Qimage;
274
275 /* Non-zero means print newline to stdout before next mini-buffer
276 message. */
277
278 int noninteractive_need_newline;
279
280 /* Non-zero means print newline to message log before next message. */
281
282 static int message_log_need_newline;
283
284 \f
285 /* The buffer position of the first character appearing entirely or
286 partially on the line of the selected window which contains the
287 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
288 redisplay optimization in redisplay_internal. */
289
290 static struct text_pos this_line_start_pos;
291
292 /* Number of characters past the end of the line above, including the
293 terminating newline. */
294
295 static struct text_pos this_line_end_pos;
296
297 /* The vertical positions and the height of this line. */
298
299 static int this_line_vpos;
300 static int this_line_y;
301 static int this_line_pixel_height;
302
303 /* X position at which this display line starts. Usually zero;
304 negative if first character is partially visible. */
305
306 static int this_line_start_x;
307
308 /* Buffer that this_line_.* variables are referring to. */
309
310 static struct buffer *this_line_buffer;
311
312 /* Nonzero means truncate lines in all windows less wide than the
313 frame. */
314
315 int truncate_partial_width_windows;
316
317 /* A flag to control how to display unibyte 8-bit character. */
318
319 int unibyte_display_via_language_environment;
320
321 /* Nonzero means we have more than one non-mini-buffer-only frame.
322 Not guaranteed to be accurate except while parsing
323 frame-title-format. */
324
325 int multiple_frames;
326
327 Lisp_Object Vglobal_mode_string;
328
329 /* Marker for where to display an arrow on top of the buffer text. */
330
331 Lisp_Object Voverlay_arrow_position;
332
333 /* String to display for the arrow. Only used on terminal frames. */
334
335 Lisp_Object Voverlay_arrow_string;
336
337 /* Values of those variables at last redisplay. However, if
338 Voverlay_arrow_position is a marker, last_arrow_position is its
339 numerical position. */
340
341 static Lisp_Object last_arrow_position, last_arrow_string;
342
343 /* Like mode-line-format, but for the title bar on a visible frame. */
344
345 Lisp_Object Vframe_title_format;
346
347 /* Like mode-line-format, but for the title bar on an iconified frame. */
348
349 Lisp_Object Vicon_title_format;
350
351 /* List of functions to call when a window's size changes. These
352 functions get one arg, a frame on which one or more windows' sizes
353 have changed. */
354
355 static Lisp_Object Vwindow_size_change_functions;
356
357 Lisp_Object Qmenu_bar_update_hook;
358
359 /* Nonzero if overlay arrow has been displayed once in this window. */
360
361 static int overlay_arrow_seen;
362
363 /* Nonzero means highlight the region even in nonselected windows. */
364
365 int highlight_nonselected_windows;
366
367 /* If cursor motion alone moves point off frame, try scrolling this
368 many lines up or down if that will bring it back. */
369
370 static int scroll_step;
371
372 /* Non-0 means scroll just far enough to bring point back on the
373 screen, when appropriate. */
374
375 static int scroll_conservatively;
376
377 /* Recenter the window whenever point gets within this many lines of
378 the top or bottom of the window. This value is translated into a
379 pixel value by multiplying it with CANON_Y_UNIT, which means that
380 there is really a fixed pixel height scroll margin. */
381
382 int scroll_margin;
383
384 /* Number of windows showing the buffer of the selected window (or
385 another buffer with the same base buffer). keyboard.c refers to
386 this. */
387
388 int buffer_shared;
389
390 /* Vector containing glyphs for an ellipsis `...'. */
391
392 static Lisp_Object default_invis_vector[3];
393
394 /* Nonzero means display mode line highlighted. */
395
396 int mode_line_inverse_video;
397
398 /* Prompt to display in front of the mini-buffer contents. */
399
400 Lisp_Object minibuf_prompt;
401
402 /* Width of current mini-buffer prompt. Only set after display_line
403 of the line that contains the prompt. */
404
405 int minibuf_prompt_width;
406 int minibuf_prompt_pixel_width;
407
408 /* This is the window where the echo area message was displayed. It
409 is always a mini-buffer window, but it may not be the same window
410 currently active as a mini-buffer. */
411
412 Lisp_Object echo_area_window;
413
414 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
415 pushes the current message and the value of
416 message_enable_multibyte on the stack, the function restore_message
417 pops the stack and displays MESSAGE again. */
418
419 Lisp_Object Vmessage_stack;
420
421 /* Nonzero means multibyte characters were enabled when the echo area
422 message was specified. */
423
424 int message_enable_multibyte;
425
426 /* True if we should redraw the mode lines on the next redisplay. */
427
428 int update_mode_lines;
429
430 /* Nonzero if window sizes or contents have changed since last
431 redisplay that finished */
432
433 int windows_or_buffers_changed;
434
435 /* Nonzero after display_mode_line if %l was used and it displayed a
436 line number. */
437
438 int line_number_displayed;
439
440 /* Maximum buffer size for which to display line numbers. */
441
442 static int line_number_display_limit;
443
444 /* line width to consider when repostioning for line number display */
445
446 static int line_number_display_limit_width;
447
448 /* Number of lines to keep in the message log buffer. t means
449 infinite. nil means don't log at all. */
450
451 Lisp_Object Vmessage_log_max;
452
453 /* Current, index 0, and last displayed echo area message. Either
454 buffers from echo_buffers, or nil to indicate no message. */
455
456 Lisp_Object echo_area_buffer[2];
457
458 /* The buffers referenced from echo_area_buffer. */
459
460 static Lisp_Object echo_buffer[2];
461
462 /* A vector saved used in with_area_buffer to reduce consing. */
463
464 static Lisp_Object Vwith_echo_area_save_vector;
465
466 /* Non-zero means display_echo_area should display the last echo area
467 message again. Set by redisplay_preserve_echo_area. */
468
469 static int display_last_displayed_message_p;
470
471 /* Nonzero if echo area is being used by print; zero if being used by
472 message. */
473
474 int message_buf_print;
475
476 /* Maximum height for resizing mini-windows. Either a float
477 specifying a fraction of the available height, or an integer
478 specifying a number of lines. */
479
480 static Lisp_Object Vmax_mini_window_height;
481
482 /* Window configuration saved in resize_mini_window. */
483
484 Lisp_Object Vresize_mini_config;
485
486 /* Frmae of the mini-window being resized by resize_mini_window. */
487
488 struct frame *resize_mini_frame;
489
490 /* Initial height of the mini-window having been resized
491 by resize_mini_window. */
492
493 int resize_mini_initial_height;
494
495 /* A scratch glyph row with contents used for generating truncation
496 glyphs. Also used in direct_output_for_insert. */
497
498 #define MAX_SCRATCH_GLYPHS 100
499 struct glyph_row scratch_glyph_row;
500 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
501
502 /* Ascent and height of the last line processed by move_it_to. */
503
504 static int last_max_ascent, last_height;
505
506 /* The maximum distance to look ahead for text properties. Values
507 that are too small let us call compute_char_face and similar
508 functions too often which is expensive. Values that are too large
509 let us call compute_char_face and alike too often because we
510 might not be interested in text properties that far away. */
511
512 #define TEXT_PROP_DISTANCE_LIMIT 100
513
514 /* Non-zero means print traces of redisplay if compiled with
515 GLYPH_DEBUG != 0. */
516
517 #if GLYPH_DEBUG
518 int trace_redisplay_p;
519 #endif
520
521 /* Value returned from text property handlers (see below). */
522
523 enum prop_handled
524 {
525 HANDLED_NORMALLY,
526 HANDLED_RECOMPUTE_PROPS,
527 HANDLED_OVERLAY_STRING_CONSUMED,
528 HANDLED_RETURN
529 };
530
531 /* A description of text properties that redisplay is interested
532 in. */
533
534 struct props
535 {
536 /* The name of the property. */
537 Lisp_Object *name;
538
539 /* A unique index for the property. */
540 enum prop_idx idx;
541
542 /* A handler function called to set up iterator IT from the property
543 at IT's current position. Value is used to steer handle_stop. */
544 enum prop_handled (*handler) P_ ((struct it *it));
545 };
546
547 static enum prop_handled handle_face_prop P_ ((struct it *));
548 static enum prop_handled handle_invisible_prop P_ ((struct it *));
549 static enum prop_handled handle_display_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 {NULL, 0, NULL}
564 };
565
566 /* Value is the position described by X. If X is a marker, value is
567 the marker_position of X. Otherwise, value is X. */
568
569 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
570
571 /* Enumeration returned by some move_it_.* functions internally. */
572
573 enum move_it_result
574 {
575 /* Not used. Undefined value. */
576 MOVE_UNDEFINED,
577
578 /* Move ended at the requested buffer position or ZV. */
579 MOVE_POS_MATCH_OR_ZV,
580
581 /* Move ended at the requested X pixel position. */
582 MOVE_X_REACHED,
583
584 /* Move within a line ended at the end of a line that must be
585 continued. */
586 MOVE_LINE_CONTINUED,
587
588 /* Move within a line ended at the end of a line that would
589 be displayed truncated. */
590 MOVE_LINE_TRUNCATED,
591
592 /* Move within a line ended at a line end. */
593 MOVE_NEWLINE_OR_CR
594 };
595
596
597 \f
598 /* Function prototypes. */
599
600 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
601 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
602 static int resize_mini_window P_ ((struct window *));
603 static void clear_garbaged_frames P_ ((void));
604 static int current_message_1 P_ ((Lisp_Object *));
605 static int truncate_message_1 P_ ((int));
606 static int set_message_1 P_ ((char *s, Lisp_Object, int, int));
607 static int display_echo_area P_ ((struct window *));
608 static int display_echo_area_1 P_ ((struct window *));
609 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
610 static int string_char_and_length P_ ((unsigned char *, int, int *));
611 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
612 struct text_pos));
613 static int compute_window_start_on_continuation_line P_ ((struct window *));
614 static Lisp_Object eval_handler P_ ((Lisp_Object));
615 static Lisp_Object eval_form P_ ((Lisp_Object));
616 static void insert_left_trunc_glyphs P_ ((struct it *));
617 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
618 static void extend_face_to_end_of_line P_ ((struct it *));
619 static void append_space P_ ((struct it *, int));
620 static void make_cursor_line_fully_visible P_ ((struct window *));
621 static int try_scrolling P_ ((Lisp_Object, int, int, int, int));
622 static int trailing_whitespace_p P_ ((int));
623 static int message_log_check_duplicate P_ ((int, int, int, int));
624 int invisible_p P_ ((Lisp_Object, Lisp_Object));
625 int invisible_ellipsis_p P_ ((Lisp_Object, Lisp_Object));
626 static void push_it P_ ((struct it *));
627 static void pop_it P_ ((struct it *));
628 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
629 static void redisplay_internal P_ ((int));
630 static int echo_area_display P_ ((int));
631 static void redisplay_windows P_ ((Lisp_Object));
632 static void redisplay_window P_ ((Lisp_Object, int));
633 static void update_menu_bar P_ ((struct frame *, int));
634 static int try_window_reusing_current_matrix P_ ((struct window *));
635 static int try_window_id P_ ((struct window *));
636 static int display_line P_ ((struct it *));
637 static void display_mode_lines P_ ((struct window *));
638 static void display_mode_line P_ ((struct window *, enum face_id,
639 Lisp_Object));
640 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object));
641 static char *decode_mode_spec P_ ((struct window *, char, int, int));
642 static void display_menu_bar P_ ((struct window *));
643 static int display_count_lines P_ ((int, int, int, int, int *));
644 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
645 int, int, struct it *, int, int, int, int));
646 static void compute_line_metrics P_ ((struct it *));
647 static void run_redisplay_end_trigger_hook P_ ((struct it *));
648 static int get_overlay_strings P_ ((struct it *));
649 static void next_overlay_string P_ ((struct it *));
650 void set_iterator_to_next P_ ((struct it *));
651 static void reseat P_ ((struct it *, struct text_pos, int));
652 static void reseat_1 P_ ((struct it *, struct text_pos, int));
653 static void back_to_previous_visible_line_start P_ ((struct it *));
654 static void reseat_at_previous_visible_line_start P_ ((struct it *));
655 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
656 static int next_element_from_display_vector P_ ((struct it *));
657 static int next_element_from_string P_ ((struct it *));
658 static int next_element_from_c_string P_ ((struct it *));
659 static int next_element_from_buffer P_ ((struct it *));
660 static int next_element_from_image P_ ((struct it *));
661 static int next_element_from_stretch P_ ((struct it *));
662 static void load_overlay_strings P_ ((struct it *));
663 static void init_from_display_pos P_ ((struct it *, struct window *,
664 struct display_pos *));
665 static void reseat_to_string P_ ((struct it *, unsigned char *,
666 Lisp_Object, int, int, int, int));
667 static int charset_at_position P_ ((struct text_pos));
668 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
669 int, int, int));
670 void move_it_vertically_backward P_ ((struct it *, int));
671 static void init_to_row_start P_ ((struct it *, struct window *,
672 struct glyph_row *));
673 static void init_to_row_end P_ ((struct it *, struct window *,
674 struct glyph_row *));
675 static void back_to_previous_line_start P_ ((struct it *));
676 static void forward_to_next_line_start P_ ((struct it *));
677 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
678 Lisp_Object, int));
679 static struct text_pos string_pos P_ ((int, Lisp_Object));
680 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
681 static int number_of_chars P_ ((unsigned char *, int));
682 static void compute_stop_pos P_ ((struct it *));
683 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
684 Lisp_Object));
685 static int face_before_or_after_it_pos P_ ((struct it *, int));
686 static int next_overlay_change P_ ((int));
687 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
688 Lisp_Object, struct text_pos *));
689
690 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
691 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
692
693 #ifdef HAVE_WINDOW_SYSTEM
694
695 static void update_toolbar P_ ((struct frame *, int));
696 static void build_desired_toolbar_string P_ ((struct frame *f));
697 static int redisplay_toolbar P_ ((struct frame *));
698 static void display_toolbar_line P_ ((struct it *));
699
700 #endif /* HAVE_WINDOW_SYSTEM */
701
702 \f
703 /***********************************************************************
704 Window display dimensions
705 ***********************************************************************/
706
707 /* Return the window-relative maximum y + 1 for glyph rows displaying
708 text in window W. This is the height of W minus the height of a
709 mode line, if any. */
710
711 INLINE int
712 window_text_bottom_y (w)
713 struct window *w;
714 {
715 struct frame *f = XFRAME (w->frame);
716 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
717
718 if (WINDOW_WANTS_MODELINE_P (w))
719 height -= CURRENT_MODE_LINE_HEIGHT (w);
720 return height;
721 }
722
723
724 /* Return the pixel width of display area AREA of window W. AREA < 0
725 means return the total width of W, not including bitmap areas to
726 the left and right of the window. */
727
728 INLINE int
729 window_box_width (w, area)
730 struct window *w;
731 int area;
732 {
733 struct frame *f = XFRAME (w->frame);
734 int width = XFASTINT (w->width);
735
736 if (!w->pseudo_window_p)
737 {
738 width -= FRAME_SCROLL_BAR_WIDTH (f) + FRAME_FLAGS_AREA_COLS (f);
739
740 if (area == TEXT_AREA)
741 {
742 if (INTEGERP (w->left_margin_width))
743 width -= XFASTINT (w->left_margin_width);
744 if (INTEGERP (w->right_margin_width))
745 width -= XFASTINT (w->right_margin_width);
746 }
747 else if (area == LEFT_MARGIN_AREA)
748 width = (INTEGERP (w->left_margin_width)
749 ? XFASTINT (w->left_margin_width) : 0);
750 else if (area == RIGHT_MARGIN_AREA)
751 width = (INTEGERP (w->right_margin_width)
752 ? XFASTINT (w->right_margin_width) : 0);
753 }
754
755 return width * CANON_X_UNIT (f);
756 }
757
758
759 /* Return the pixel height of the display area of window W, not
760 including mode lines of W, if any.. */
761
762 INLINE int
763 window_box_height (w)
764 struct window *w;
765 {
766 struct frame *f = XFRAME (w->frame);
767 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
768
769 if (WINDOW_WANTS_MODELINE_P (w))
770 height -= CURRENT_MODE_LINE_HEIGHT (w);
771
772 if (WINDOW_WANTS_TOP_LINE_P (w))
773 height -= CURRENT_TOP_LINE_HEIGHT (w);
774
775 return height;
776 }
777
778
779 /* Return the frame-relative coordinate of the left edge of display
780 area AREA of window W. AREA < 0 means return the left edge of the
781 whole window, to the right of any bitmap area at the left side of
782 W. */
783
784 INLINE int
785 window_box_left (w, area)
786 struct window *w;
787 int area;
788 {
789 struct frame *f = XFRAME (w->frame);
790 int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
791
792 if (!w->pseudo_window_p)
793 {
794 x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
795 + FRAME_LEFT_FLAGS_AREA_WIDTH (f));
796
797 if (area == TEXT_AREA)
798 x += window_box_width (w, LEFT_MARGIN_AREA);
799 else if (area == RIGHT_MARGIN_AREA)
800 x += (window_box_width (w, LEFT_MARGIN_AREA)
801 + window_box_width (w, TEXT_AREA));
802 }
803
804 return x;
805 }
806
807
808 /* Return the frame-relative coordinate of the right edge of display
809 area AREA of window W. AREA < 0 means return the left edge of the
810 whole window, to the left of any bitmap area at the right side of
811 W. */
812
813 INLINE int
814 window_box_right (w, area)
815 struct window *w;
816 int area;
817 {
818 return window_box_left (w, area) + window_box_width (w, area);
819 }
820
821
822 /* Get the bounding box of the display area AREA of window W, without
823 mode lines, in frame-relative coordinates. AREA < 0 means the
824 whole window, not including bitmap areas to the left and right of
825 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
826 coordinates of the upper-left corner of the box. Return in
827 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
828
829 INLINE void
830 window_box (w, area, box_x, box_y, box_width, box_height)
831 struct window *w;
832 int area;
833 int *box_x, *box_y, *box_width, *box_height;
834 {
835 struct frame *f = XFRAME (w->frame);
836
837 *box_width = window_box_width (w, area);
838 *box_height = window_box_height (w);
839 *box_x = window_box_left (w, area);
840 *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
841 + XFASTINT (w->top) * CANON_Y_UNIT (f));
842 if (WINDOW_WANTS_TOP_LINE_P (w))
843 *box_y += CURRENT_TOP_LINE_HEIGHT (w);
844 }
845
846
847 /* Get the bounding box of the display area AREA of window W, without
848 mode lines. AREA < 0 means the whole window, not including bitmap
849 areas to the left and right of the window. Return in *TOP_LEFT_X
850 and TOP_LEFT_Y the frame-relative pixel coordinates of the
851 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
852 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
853 box. */
854
855 INLINE void
856 window_box_edges (w, area, top_left_x, top_left_y,
857 bottom_right_x, bottom_right_y)
858 struct window *w;
859 int area;
860 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
861 {
862 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
863 bottom_right_y);
864 *bottom_right_x += *top_left_x;
865 *bottom_right_y += *top_left_y;
866 }
867
868
869 \f
870 /***********************************************************************
871 Utilities
872 ***********************************************************************/
873
874 /* Return the next character from STR which is MAXLEN bytes long.
875 Return in *LEN the length of the character. This is like
876 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
877 we find one, we return a `?', but with the length of the illegal
878 character. */
879
880 static INLINE int
881 string_char_and_length (str, maxlen, len)
882 unsigned char *str;
883 int maxlen, *len;
884 {
885 int c;
886
887 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
888 if (!CHAR_VALID_P (c, 1))
889 /* We may not change the length here because other places in Emacs
890 don't use this function, i.e. they silently accept illegal
891 characters. */
892 c = '?';
893
894 return c;
895 }
896
897
898
899 /* Given a position POS containing a valid character and byte position
900 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
901
902 static struct text_pos
903 string_pos_nchars_ahead (pos, string, nchars)
904 struct text_pos pos;
905 Lisp_Object string;
906 int nchars;
907 {
908 xassert (STRINGP (string) && nchars >= 0);
909
910 if (STRING_MULTIBYTE (string))
911 {
912 int rest = STRING_BYTES (XSTRING (string)) - BYTEPOS (pos);
913 unsigned char *p = XSTRING (string)->data + BYTEPOS (pos);
914 int len;
915
916 while (nchars--)
917 {
918 string_char_and_length (p, rest, &len);
919 p += len, rest -= len;
920 xassert (rest >= 0);
921 CHARPOS (pos) += 1;
922 BYTEPOS (pos) += len;
923 }
924 }
925 else
926 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
927
928 return pos;
929 }
930
931
932 /* Value is the text position, i.e. character and byte position,
933 for character position CHARPOS in STRING. */
934
935 static INLINE struct text_pos
936 string_pos (charpos, string)
937 int charpos;
938 Lisp_Object string;
939 {
940 struct text_pos pos;
941 xassert (STRINGP (string));
942 xassert (charpos >= 0);
943 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
944 return pos;
945 }
946
947
948 /* Value is a text position, i.e. character and byte position, for
949 character position CHARPOS in C string S. MULTIBYTE_P non-zero
950 means recognize multibyte characters. */
951
952 static struct text_pos
953 c_string_pos (charpos, s, multibyte_p)
954 int charpos;
955 unsigned char *s;
956 int multibyte_p;
957 {
958 struct text_pos pos;
959
960 xassert (s != NULL);
961 xassert (charpos >= 0);
962
963 if (multibyte_p)
964 {
965 int rest = strlen (s), len;
966
967 SET_TEXT_POS (pos, 0, 0);
968 while (charpos--)
969 {
970 string_char_and_length (s, rest, &len);
971 s += len, rest -= len;
972 xassert (rest >= 0);
973 CHARPOS (pos) += 1;
974 BYTEPOS (pos) += len;
975 }
976 }
977 else
978 SET_TEXT_POS (pos, charpos, charpos);
979
980 return pos;
981 }
982
983
984 /* Value is the number of characters in C string S. MULTIBYTE_P
985 non-zero means recognize multibyte characters. */
986
987 static int
988 number_of_chars (s, multibyte_p)
989 unsigned char *s;
990 int multibyte_p;
991 {
992 int nchars;
993
994 if (multibyte_p)
995 {
996 int rest = strlen (s), len;
997 unsigned char *p = (unsigned char *) s;
998
999 for (nchars = 0; rest > 0; ++nchars)
1000 {
1001 string_char_and_length (p, rest, &len);
1002 rest -= len, p += len;
1003 }
1004 }
1005 else
1006 nchars = strlen (s);
1007
1008 return nchars;
1009 }
1010
1011
1012 /* Compute byte position NEWPOS->bytepos corresponding to
1013 NEWPOS->charpos. POS is a known position in string STRING.
1014 NEWPOS->charpos must be >= POS.charpos. */
1015
1016 static void
1017 compute_string_pos (newpos, pos, string)
1018 struct text_pos *newpos, pos;
1019 Lisp_Object string;
1020 {
1021 xassert (STRINGP (string));
1022 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1023
1024 if (STRING_MULTIBYTE (string))
1025 *newpos = string_pos_nchars_ahead (pos, CHARPOS (*newpos) - CHARPOS (pos),
1026 string);
1027 else
1028 BYTEPOS (*newpos) = CHARPOS (*newpos);
1029 }
1030
1031
1032 /* Return the charset of the character at position POS in
1033 current_buffer. */
1034
1035 static int
1036 charset_at_position (pos)
1037 struct text_pos pos;
1038 {
1039 int c, multibyte_p;
1040 unsigned char *p = BYTE_POS_ADDR (BYTEPOS (pos));
1041
1042 multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1043 if (multibyte_p)
1044 {
1045 int maxlen = ((BYTEPOS (pos) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
1046 - BYTEPOS (pos));
1047 int len;
1048 c = string_char_and_length (p, maxlen, &len);
1049 }
1050 else
1051 c = *p;
1052
1053 return CHAR_CHARSET (c);
1054 }
1055
1056
1057 \f
1058 /***********************************************************************
1059 Lisp form evaluation
1060 ***********************************************************************/
1061
1062 /* Error handler for eval_form. */
1063
1064 static Lisp_Object
1065 eval_handler (arg)
1066 Lisp_Object arg;
1067 {
1068 return Qnil;
1069 }
1070
1071
1072 /* Evaluate SEXPR and return the result, or nil if something went
1073 wrong. */
1074
1075 static Lisp_Object
1076 eval_form (sexpr)
1077 Lisp_Object sexpr;
1078 {
1079 int count = specpdl_ptr - specpdl;
1080 Lisp_Object val;
1081 specbind (Qinhibit_redisplay, Qt);
1082 val = internal_condition_case_1 (Feval, sexpr, Qerror, eval_handler);
1083 return unbind_to (count, val);
1084 }
1085
1086
1087 \f
1088 /***********************************************************************
1089 Debugging
1090 ***********************************************************************/
1091
1092 #if 0
1093
1094 /* Define CHECK_IT to perform sanity checks on iterators.
1095 This is for debugging. It is too slow to do unconditionally. */
1096
1097 static void
1098 check_it (it)
1099 struct it *it;
1100 {
1101 if (it->method == next_element_from_string)
1102 {
1103 xassert (STRINGP (it->string));
1104 xassert (IT_STRING_CHARPOS (*it) >= 0);
1105 }
1106 else if (it->method == next_element_from_buffer)
1107 {
1108 /* Check that character and byte positions agree. */
1109 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1110 }
1111
1112 if (it->dpvec)
1113 xassert (it->current.dpvec_index >= 0);
1114 else
1115 xassert (it->current.dpvec_index < 0);
1116 }
1117
1118 #define CHECK_IT(IT) check_it ((IT))
1119
1120 #else /* not 0 */
1121
1122 #define CHECK_IT(IT) (void) 0
1123
1124 #endif /* not 0 */
1125
1126
1127 #if GLYPH_DEBUG
1128
1129 /* Check that the window end of window W is what we expect it
1130 to be---the last row in the current matrix displaying text. */
1131
1132 static void
1133 check_window_end (w)
1134 struct window *w;
1135 {
1136 if (!MINI_WINDOW_P (w)
1137 && !NILP (w->window_end_valid))
1138 {
1139 struct glyph_row *row;
1140 xassert ((row = MATRIX_ROW (w->current_matrix,
1141 XFASTINT (w->window_end_vpos)),
1142 !row->enabled_p
1143 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1144 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1145 }
1146 }
1147
1148 #define CHECK_WINDOW_END(W) check_window_end ((W))
1149
1150 #else /* not GLYPH_DEBUG */
1151
1152 #define CHECK_WINDOW_END(W) (void) 0
1153
1154 #endif /* not GLYPH_DEBUG */
1155
1156
1157 \f
1158 /***********************************************************************
1159 Iterator initialization
1160 ***********************************************************************/
1161
1162 /* Initialize IT for displaying current_buffer in window W, starting
1163 at character position CHARPOS. CHARPOS < 0 means that no buffer
1164 position is specified which is useful when the iterator is assigned
1165 a position later. BYTEPOS is the byte position corresponding to
1166 CHARPOS. BYTEPOS <= 0 means compute it from CHARPOS.
1167
1168 If ROW is not null, calls to produce_glyphs with IT as parameter
1169 will produce glyphs in that row.
1170
1171 BASE_FACE_ID is the id of a base face to use. It must be one of
1172 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID or
1173 TOP_LINE_FACE_ID for displaying mode lines, or TOOLBAR_FACE_ID for
1174 displaying the toolbar.
1175
1176 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID or
1177 TOP_LINE_FACE_ID, the iterator will be initialized to use the
1178 corresponding mode line glyph row of the desired matrix of W. */
1179
1180 void
1181 init_iterator (it, w, charpos, bytepos, row, base_face_id)
1182 struct it *it;
1183 struct window *w;
1184 int charpos, bytepos;
1185 struct glyph_row *row;
1186 enum face_id base_face_id;
1187 {
1188 int highlight_region_p;
1189
1190 /* Some precondition checks. */
1191 xassert (w != NULL && it != NULL);
1192 xassert (charpos < 0 || (charpos > 0 && charpos <= ZV));
1193
1194 /* If face attributes have been changed since the last redisplay,
1195 free realized faces now because they depend on face definitions
1196 that might have changed. */
1197 if (face_change_count)
1198 {
1199 face_change_count = 0;
1200 free_all_realized_faces (Qnil);
1201 }
1202
1203 /* Use one of the mode line rows of W's desired matrix if
1204 appropriate. */
1205 if (row == NULL)
1206 {
1207 if (base_face_id == MODE_LINE_FACE_ID)
1208 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
1209 else if (base_face_id == TOP_LINE_FACE_ID)
1210 row = MATRIX_TOP_LINE_ROW (w->desired_matrix);
1211 }
1212
1213 /* Clear IT. */
1214 bzero (it, sizeof *it);
1215 it->current.overlay_string_index = -1;
1216 it->current.dpvec_index = -1;
1217 it->charset = CHARSET_ASCII;
1218 it->base_face_id = base_face_id;
1219
1220 /* The window in which we iterate over current_buffer: */
1221 XSETWINDOW (it->window, w);
1222 it->w = w;
1223 it->f = XFRAME (w->frame);
1224
1225 /* If realized faces have been removed, e.g. because of face
1226 attribute changes of named faces, recompute them. */
1227 if (FRAME_FACE_CACHE (it->f)->used == 0)
1228 recompute_basic_faces (it->f);
1229
1230 /* Current value of the `space-width', and 'height' properties. */
1231 it->space_width = Qnil;
1232 it->font_height = Qnil;
1233
1234 /* Are control characters displayed as `^C'? */
1235 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
1236
1237 /* -1 means everything between a CR and the following line end
1238 is invisible. >0 means lines indented more than this value are
1239 invisible. */
1240 it->selective = (INTEGERP (current_buffer->selective_display)
1241 ? XFASTINT (current_buffer->selective_display)
1242 : (!NILP (current_buffer->selective_display)
1243 ? -1 : 0));
1244 it->selective_display_ellipsis_p
1245 = !NILP (current_buffer->selective_display_ellipses);
1246
1247 /* Display table to use. */
1248 it->dp = window_display_table (w);
1249
1250 /* Are multibyte characters enabled in current_buffer? */
1251 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1252
1253 /* Non-zero if we should highlight the region. */
1254 highlight_region_p
1255 = (!NILP (Vtransient_mark_mode)
1256 && !NILP (current_buffer->mark_active)
1257 && XMARKER (current_buffer->mark)->buffer != 0);
1258
1259 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
1260 start and end of a visible region in window IT->w. Set both to
1261 -1 to indicate no region. */
1262 if (highlight_region_p
1263 /* Maybe highlight only in selected window. */
1264 && (/* Either show region everywhere. */
1265 highlight_nonselected_windows
1266 /* Or show region in the selected window. */
1267 || w == XWINDOW (selected_window)
1268 /* Or show the region if we are in the mini-buffer and W is
1269 the window the mini-buffer refers to. */
1270 || (MINI_WINDOW_P (XWINDOW (selected_window))
1271 && w == XWINDOW (Vminibuf_scroll_window))))
1272 {
1273 int charpos = marker_position (current_buffer->mark);
1274 it->region_beg_charpos = min (PT, charpos);
1275 it->region_end_charpos = max (PT, charpos);
1276 }
1277 else
1278 it->region_beg_charpos = it->region_end_charpos = -1;
1279
1280 /* Get the position at which the redisplay_end_trigger hook should
1281 be run, if it is to be run at all. */
1282 if (MARKERP (w->redisplay_end_trigger)
1283 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
1284 it->redisplay_end_trigger_charpos
1285 = marker_position (w->redisplay_end_trigger);
1286 else if (INTEGERP (w->redisplay_end_trigger))
1287 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
1288
1289 /* Correct bogus values of tab_width. */
1290 it->tab_width = XINT (current_buffer->tab_width);
1291 if (it->tab_width <= 0 || it->tab_width > 1000)
1292 it->tab_width = 8;
1293
1294 /* Are lines in the display truncated? */
1295 it->truncate_lines_p
1296 = (base_face_id != DEFAULT_FACE_ID
1297 || XINT (it->w->hscroll)
1298 || (truncate_partial_width_windows
1299 && !WINDOW_FULL_WIDTH_P (it->w))
1300 || !NILP (current_buffer->truncate_lines));
1301
1302 /* Get dimensions of truncation and continuation glyphs. These are
1303 displayed as bitmaps under X, so we don't need them for such
1304 frames. */
1305 if (!FRAME_WINDOW_P (it->f))
1306 {
1307 if (it->truncate_lines_p)
1308 {
1309 /* We will need the truncation glyph. */
1310 xassert (it->glyph_row == NULL);
1311 produce_special_glyphs (it, IT_TRUNCATION);
1312 it->truncation_pixel_width = it->pixel_width;
1313 }
1314 else
1315 {
1316 /* We will need the continuation glyph. */
1317 xassert (it->glyph_row == NULL);
1318 produce_special_glyphs (it, IT_CONTINUATION);
1319 it->continuation_pixel_width = it->pixel_width;
1320 }
1321
1322 /* Reset these values to zero becaue the produce_special_glyphs
1323 above has changed them. */
1324 it->pixel_width = it->ascent = it->descent = 0;
1325 it->phys_ascent = it->phys_descent = 0;
1326 }
1327
1328 /* Set this after getting the dimensions of truncation and
1329 continuation glyphs, so that we don't produce glyphs when calling
1330 produce_special_glyphs, above. */
1331 it->glyph_row = row;
1332 it->area = TEXT_AREA;
1333
1334 /* Get the dimensions of the display area. The display area
1335 consists of the visible window area plus a horizontally scrolled
1336 part to the left of the window. All x-values are relative to the
1337 start of this total display area. */
1338 if (base_face_id != DEFAULT_FACE_ID)
1339 {
1340 /* Mode lines, menu bar in terminal frames. */
1341 it->first_visible_x = 0;
1342 it->last_visible_x = XFASTINT (w->width) * CANON_X_UNIT (it->f);
1343 }
1344 else
1345 {
1346 it->first_visible_x
1347 = XFASTINT (it->w->hscroll) * CANON_X_UNIT (it->f);
1348 it->last_visible_x = (it->first_visible_x
1349 + window_box_width (w, TEXT_AREA));
1350
1351 /* If we truncate lines, leave room for the truncator glyph(s) at
1352 the right margin. Otherwise, leave room for the continuation
1353 glyph(s). Truncation and continuation glyphs are not inserted
1354 for window-based redisplay. */
1355 if (!FRAME_WINDOW_P (it->f))
1356 {
1357 if (it->truncate_lines_p)
1358 it->last_visible_x -= it->truncation_pixel_width;
1359 else
1360 it->last_visible_x -= it->continuation_pixel_width;
1361 }
1362
1363 it->top_line_p = WINDOW_WANTS_TOP_LINE_P (w);
1364 it->current_y = WINDOW_DISPLAY_TOP_LINE_HEIGHT (w) + w->vscroll;
1365 }
1366
1367 /* Leave room for a border glyph. */
1368 if (!FRAME_WINDOW_P (it->f)
1369 && !WINDOW_RIGHTMOST_P (it->w))
1370 it->last_visible_x -= 1;
1371
1372 it->last_visible_y = window_text_bottom_y (w);
1373
1374 /* For mode lines and alike, arrange for the first glyph having a
1375 left box line if the face specifies a box. */
1376 if (base_face_id != DEFAULT_FACE_ID)
1377 {
1378 struct face *face;
1379
1380 it->face_id = base_face_id;
1381
1382 /* If we have a boxed mode line, make the first character appear
1383 with a left box line. */
1384 face = FACE_FROM_ID (it->f, base_face_id);
1385 if (face->box != FACE_NO_BOX)
1386 it->start_of_box_run_p = 1;
1387 }
1388
1389 /* If a buffer position was specified, set the iterator there,
1390 getting overlays and face properties from that position. */
1391 if (charpos > 0)
1392 {
1393 it->end_charpos = ZV;
1394 it->face_id = -1;
1395 IT_CHARPOS (*it) = charpos;
1396
1397 /* Compute byte position if not specified. */
1398 if (bytepos <= 0)
1399 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
1400 else
1401 IT_BYTEPOS (*it) = bytepos;
1402
1403 /* Compute faces etc. */
1404 reseat (it, it->current.pos, 1);
1405 }
1406
1407 CHECK_IT (it);
1408 }
1409
1410
1411 /* Initialize IT for the display of window W with window start POS. */
1412
1413 void
1414 start_display (it, w, pos)
1415 struct it *it;
1416 struct window *w;
1417 struct text_pos pos;
1418 {
1419 int start_at_line_beg_p;
1420 struct glyph_row *row;
1421 int first_vpos = WINDOW_WANTS_TOP_LINE_P (w) ? 1 : 0;
1422 int first_y;
1423
1424 row = w->desired_matrix->rows + first_vpos;
1425 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
1426 first_y = it->current_y;
1427
1428 /* If window start is not at a line start, move back to the line
1429 start. This makes sure that we take continuation lines into
1430 account. */
1431 start_at_line_beg_p = (CHARPOS (pos) == BEGV
1432 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
1433 if (!start_at_line_beg_p)
1434 reseat_at_previous_visible_line_start (it);
1435
1436 /* If window start is not at a line start, skip forward to POS to
1437 get the correct continuation_lines_width and current_x. */
1438 if (!start_at_line_beg_p)
1439 {
1440 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
1441
1442 /* If lines are continued, this line may end in the middle of a
1443 multi-glyph character (e.g. a control character displayed as
1444 \003, or in the middle of an overlay string). In this case
1445 move_it_to above will not have taken us to the start of
1446 the continuation line but to the end of the continued line. */
1447 if (!it->truncate_lines_p && it->current_x > 0)
1448 {
1449 if (it->current.dpvec_index >= 0
1450 || it->current.overlay_string_index >= 0)
1451 {
1452 set_iterator_to_next (it);
1453 move_it_in_display_line_to (it, -1, -1, 0);
1454 }
1455 it->continuation_lines_width += it->current_x;
1456 }
1457
1458 it->current_y = first_y;
1459 it->vpos = 0;
1460 it->current_x = it->hpos = 0;
1461 }
1462
1463 #if 0 /* Don't assert the following because start_display is sometimes
1464 called intentionally with a window start that is not at a
1465 line start. Please leave this code in as a comment. */
1466
1467 /* Window start should be on a line start, now. */
1468 xassert (it->continuation_lines_width
1469 || IT_CHARPOS (it) == BEGV
1470 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
1471 #endif /* 0 */
1472 }
1473
1474
1475 /* Initialize IT for stepping through current_buffer in window W,
1476 starting at position POS that includes overlay string and display
1477 vector/ control character translation position information. */
1478
1479 static void
1480 init_from_display_pos (it, w, pos)
1481 struct it *it;
1482 struct window *w;
1483 struct display_pos *pos;
1484 {
1485 /* Keep in mind: the call to reseat in init_iterator skips invisible
1486 text, so we might end up at a position different from POS. This
1487 is only a problem when POS is a row start after a newline and an
1488 overlay starts there with an after-string, and the overlay has an
1489 invisible property. Since we don't skip invisible text in
1490 display_line and elsewhere immediately after consuming the
1491 newline before the row start, such a POS will not be in a string,
1492 but the call to init_iterator below will move us to the
1493 after-string. */
1494 init_iterator (it, w, CHARPOS (pos->pos), BYTEPOS (pos->pos),
1495 NULL, DEFAULT_FACE_ID);
1496
1497 /* If position is within an overlay string, set up IT to
1498 the right overlay string. */
1499 if (pos->overlay_string_index >= 0)
1500 {
1501 int relative_index;
1502
1503 /* We already have the first chunk of overlay strings in
1504 IT->overlay_strings. Load more until the one for
1505 pos->overlay_string_index is in IT->overlay_strings. */
1506 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
1507 {
1508 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
1509 it->current.overlay_string_index = 0;
1510 while (n--)
1511 {
1512 load_overlay_strings (it);
1513 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
1514 }
1515 }
1516
1517 it->current.overlay_string_index = pos->overlay_string_index;
1518 relative_index = (it->current.overlay_string_index
1519 % OVERLAY_STRING_CHUNK_SIZE);
1520 it->string = it->overlay_strings[relative_index];
1521 it->current.string_pos = pos->string_pos;
1522 it->method = next_element_from_string;
1523 }
1524 else if (CHARPOS (pos->string_pos) >= 0)
1525 {
1526 /* Recorded position is not in an overlay string, but in another
1527 string. This can only be a string from a `display' property.
1528 IT should already be filled with that string. */
1529 it->current.string_pos = pos->string_pos;
1530 xassert (STRINGP (it->string));
1531 }
1532
1533 /* Restore position in display vector translations or control
1534 character translations. */
1535 if (pos->dpvec_index >= 0)
1536 {
1537 /* This fills IT->dpvec. */
1538 get_next_display_element (it);
1539 xassert (it->dpvec && it->current.dpvec_index == 0);
1540 it->current.dpvec_index = pos->dpvec_index;
1541 }
1542
1543 CHECK_IT (it);
1544 }
1545
1546
1547 /* Initialize IT for stepping through current_buffer in window W
1548 starting at ROW->start. */
1549
1550 static void
1551 init_to_row_start (it, w, row)
1552 struct it *it;
1553 struct window *w;
1554 struct glyph_row *row;
1555 {
1556 init_from_display_pos (it, w, &row->start);
1557 it->continuation_lines_width = row->continuation_lines_width;
1558 CHECK_IT (it);
1559 }
1560
1561
1562 /* Initialize IT for stepping through current_buffer in window W
1563 starting in the line following ROW, i.e. starting at ROW->end. */
1564
1565 static void
1566 init_to_row_end (it, w, row)
1567 struct it *it;
1568 struct window *w;
1569 struct glyph_row *row;
1570 {
1571 init_from_display_pos (it, w, &row->end);
1572
1573 if (row->continued_p)
1574 it->continuation_lines_width = (row->continuation_lines_width
1575 + row->pixel_width);
1576 CHECK_IT (it);
1577 }
1578
1579
1580
1581 \f
1582 /***********************************************************************
1583 Text properties
1584 ***********************************************************************/
1585
1586 /* Called when IT reaches IT->stop_charpos. Handle text property and
1587 overlay changes. Set IT->stop_charpos to the next position where
1588 to stop. */
1589
1590 static void
1591 handle_stop (it)
1592 struct it *it;
1593 {
1594 enum prop_handled handled;
1595 int handle_overlay_change_p = 1;
1596 struct props *p;
1597
1598 it->dpvec = NULL;
1599 it->current.dpvec_index = -1;
1600
1601 do
1602 {
1603 handled = HANDLED_NORMALLY;
1604
1605 /* Call text property handlers. */
1606 for (p = it_props; p->handler; ++p)
1607 {
1608 handled = p->handler (it);
1609
1610 if (handled == HANDLED_RECOMPUTE_PROPS)
1611 break;
1612 else if (handled == HANDLED_RETURN)
1613 return;
1614 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
1615 handle_overlay_change_p = 0;
1616 }
1617
1618 if (handled != HANDLED_RECOMPUTE_PROPS)
1619 {
1620 /* Don't check for overlay strings below when set to deliver
1621 characters from a display vector. */
1622 if (it->method == next_element_from_display_vector)
1623 handle_overlay_change_p = 0;
1624
1625 /* Handle overlay changes. */
1626 if (handle_overlay_change_p)
1627 handled = handle_overlay_change (it);
1628
1629 /* Determine where to stop next. */
1630 if (handled == HANDLED_NORMALLY)
1631 compute_stop_pos (it);
1632 }
1633 }
1634 while (handled == HANDLED_RECOMPUTE_PROPS);
1635 }
1636
1637
1638 /* Compute IT->stop_charpos from text property and overlay change
1639 information for IT's current position. */
1640
1641 static void
1642 compute_stop_pos (it)
1643 struct it *it;
1644 {
1645 register INTERVAL iv, next_iv;
1646 Lisp_Object object, limit, position;
1647
1648 /* If nowhere else, stop at the end. */
1649 it->stop_charpos = it->end_charpos;
1650
1651 if (STRINGP (it->string))
1652 {
1653 /* Strings are usually short, so don't limit the search for
1654 properties. */
1655 object = it->string;
1656 limit = Qnil;
1657 XSETFASTINT (position, IT_STRING_CHARPOS (*it));
1658 }
1659 else
1660 {
1661 int charpos;
1662
1663 /* If next overlay change is in front of the current stop pos
1664 (which is IT->end_charpos), stop there. Note: value of
1665 next_overlay_change is point-max if no overlay change
1666 follows. */
1667 charpos = next_overlay_change (IT_CHARPOS (*it));
1668 if (charpos < it->stop_charpos)
1669 it->stop_charpos = charpos;
1670
1671 /* If showing the region, we have to stop at the region
1672 start or end because the face might change there. */
1673 if (it->region_beg_charpos > 0)
1674 {
1675 if (IT_CHARPOS (*it) < it->region_beg_charpos)
1676 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
1677 else if (IT_CHARPOS (*it) < it->region_end_charpos)
1678 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
1679 }
1680
1681 /* Set up variables for computing the stop position from text
1682 property changes. */
1683 XSETBUFFER (object, current_buffer);
1684 XSETFASTINT (limit, IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
1685 XSETFASTINT (position, IT_CHARPOS (*it));
1686
1687 }
1688
1689 /* Get the interval containing IT's position. Value is a null
1690 interval if there isn't such an interval. */
1691 iv = validate_interval_range (object, &position, &position, 0);
1692 if (!NULL_INTERVAL_P (iv))
1693 {
1694 Lisp_Object values_here[LAST_PROP_IDX];
1695 struct props *p;
1696
1697 /* Get properties here. */
1698 for (p = it_props; p->handler; ++p)
1699 values_here[p->idx] = textget (iv->plist, *p->name);
1700
1701 /* Look for an interval following iv that has different
1702 properties. */
1703 for (next_iv = next_interval (iv);
1704 (!NULL_INTERVAL_P (next_iv)
1705 && (NILP (limit)
1706 || XFASTINT (limit) > next_iv->position));
1707 next_iv = next_interval (next_iv))
1708 {
1709 for (p = it_props; p->handler; ++p)
1710 {
1711 Lisp_Object new_value;
1712
1713 new_value = textget (next_iv->plist, *p->name);
1714 if (!EQ (values_here[p->idx], new_value))
1715 break;
1716 }
1717
1718 if (p->handler)
1719 break;
1720 }
1721
1722 if (!NULL_INTERVAL_P (next_iv))
1723 {
1724 if (INTEGERP (limit)
1725 && next_iv->position >= XFASTINT (limit))
1726 /* No text property change up to limit. */
1727 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
1728 else
1729 /* Text properties change in next_iv. */
1730 it->stop_charpos = min (it->stop_charpos, next_iv->position);
1731 }
1732 }
1733
1734 xassert (STRINGP (it->string)
1735 || (it->stop_charpos >= BEGV
1736 && it->stop_charpos >= IT_CHARPOS (*it)));
1737 }
1738
1739
1740 /* Return the position of the next overlay change after POS in
1741 current_buffer. Value is point-max if no overlay change
1742 follows. This is like `next-overlay-change' but doesn't use
1743 xmalloc. */
1744
1745 static int
1746 next_overlay_change (pos)
1747 int pos;
1748 {
1749 int noverlays;
1750 int endpos;
1751 Lisp_Object *overlays;
1752 int len;
1753 int i;
1754
1755 /* Get all overlays at the given position. */
1756 len = 10;
1757 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
1758 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL);
1759 if (noverlays > len)
1760 {
1761 len = noverlays;
1762 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
1763 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL);
1764 }
1765
1766 /* If any of these overlays ends before endpos,
1767 use its ending point instead. */
1768 for (i = 0; i < noverlays; ++i)
1769 {
1770 Lisp_Object oend;
1771 int oendpos;
1772
1773 oend = OVERLAY_END (overlays[i]);
1774 oendpos = OVERLAY_POSITION (oend);
1775 endpos = min (endpos, oendpos);
1776 }
1777
1778 return endpos;
1779 }
1780
1781
1782 \f
1783 /***********************************************************************
1784 Fontification
1785 ***********************************************************************/
1786
1787 /* Handle changes in the `fontified' property of the current buffer by
1788 calling hook functions from Qfontification_functions to fontify
1789 regions of text. */
1790
1791 static enum prop_handled
1792 handle_fontified_prop (it)
1793 struct it *it;
1794 {
1795 Lisp_Object prop, pos;
1796 enum prop_handled handled = HANDLED_NORMALLY;
1797
1798 /* Get the value of the `fontified' property at IT's current buffer
1799 position. (The `fontified' property doesn't have a special
1800 meaning in strings.) If the value is nil, call functions from
1801 Qfontification_functions. */
1802 if (!STRINGP (it->string)
1803 && it->s == NULL
1804 && !NILP (Vfontification_functions)
1805 && (pos = make_number (IT_CHARPOS (*it)),
1806 prop = Fget_char_property (pos, Qfontified, Qnil),
1807 NILP (prop)))
1808 {
1809 Lisp_Object args[2];
1810
1811 /* Run the hook functions. */
1812 args[0] = Qfontification_functions;
1813 args[1] = pos;
1814 Frun_hook_with_args (make_number (2), args);
1815
1816 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
1817 something. This avoids an endless loop if they failed to
1818 fontify the text for which reason ever. */
1819 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
1820 handled = HANDLED_RECOMPUTE_PROPS;
1821 }
1822
1823 return handled;
1824 }
1825
1826
1827 \f
1828 /***********************************************************************
1829 Faces
1830 ***********************************************************************/
1831
1832 /* Set up iterator IT from face properties at its current position.
1833 Called from handle_stop. */
1834
1835 static enum prop_handled
1836 handle_face_prop (it)
1837 struct it *it;
1838 {
1839 int new_face_id, next_stop;
1840
1841 if (!STRINGP (it->string))
1842 {
1843 new_face_id
1844 = face_at_buffer_position (it->w,
1845 IT_CHARPOS (*it),
1846 it->region_beg_charpos,
1847 it->region_end_charpos,
1848 &next_stop,
1849 (IT_CHARPOS (*it)
1850 + TEXT_PROP_DISTANCE_LIMIT),
1851 0);
1852
1853 /* Is this a start of a run of characters with box face?
1854 Caveat: this can be called for a freshly initialized
1855 iterator; face_id is -1 is this case. We know that the new
1856 face will not change until limit, i.e. if the new face has a
1857 box, all characters up to limit will have one. But, as
1858 usual, we don't know whether limit is really the end. */
1859 if (new_face_id != it->face_id)
1860 {
1861 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
1862
1863 /* If new face has a box but old face has not, this is
1864 the start of a run of characters with box, i.e. it has
1865 a shadow on the left side. The value of face_id of the
1866 iterator will be -1 if this is the initial call that gets
1867 the face. In this case, we have to look in front of IT's
1868 position and see whether there is a face != new_face_id. */
1869 it->start_of_box_run_p
1870 = (new_face->box != FACE_NO_BOX
1871 && (it->face_id >= 0
1872 || IT_CHARPOS (*it) == BEG
1873 || new_face_id != face_before_it_pos (it)));
1874 it->face_box_p = new_face->box != FACE_NO_BOX;
1875 }
1876 }
1877 else
1878 {
1879 new_face_id
1880 = face_at_string_position (it->w,
1881 it->string,
1882 IT_STRING_CHARPOS (*it),
1883 (it->current.overlay_string_index >= 0
1884 ? IT_CHARPOS (*it)
1885 : 0),
1886 it->region_beg_charpos,
1887 it->region_end_charpos,
1888 &next_stop,
1889 it->base_face_id);
1890
1891 #if 0 /* This shouldn't be neccessary. Let's check it. */
1892 /* If IT is used to display a mode line we would really like to
1893 use the mode line face instead of the frame's default face. */
1894 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
1895 && new_face_id == DEFAULT_FACE_ID)
1896 new_face_id = MODE_LINE_FACE_ID;
1897 #endif
1898
1899 /* Is this a start of a run of characters with box? Caveat:
1900 this can be called for a freshly allocated iterator; face_id
1901 is -1 is this case. We know that the new face will not
1902 change until the next check pos, i.e. if the new face has a
1903 box, all characters up to that position will have a
1904 box. But, as usual, we don't know whether that position
1905 is really the end. */
1906 if (new_face_id != it->face_id)
1907 {
1908 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
1909 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
1910
1911 /* If new face has a box but old face hasn't, this is the
1912 start of a run of characters with box, i.e. it has a
1913 shadow on the left side. */
1914 it->start_of_box_run_p
1915 = new_face->box && (old_face == NULL || !old_face->box);
1916 it->face_box_p = new_face->box != FACE_NO_BOX;
1917 }
1918 }
1919
1920 it->face_id = new_face_id;
1921 it->charset = CHARSET_ASCII;
1922 return HANDLED_NORMALLY;
1923 }
1924
1925
1926 /* Compute the face one character before or after the current position
1927 of IT. BEFORE_P non-zero means get the face in front of IT's
1928 position. Value is the id of the face. */
1929
1930 static int
1931 face_before_or_after_it_pos (it, before_p)
1932 struct it *it;
1933 int before_p;
1934 {
1935 int face_id, limit;
1936 int next_check_charpos;
1937 struct text_pos pos;
1938
1939 xassert (it->s == NULL);
1940
1941 if (STRINGP (it->string))
1942 {
1943 /* No face change past the end of the string (for the case
1944 we are padding with spaces). No face change before the
1945 string start. */
1946 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size
1947 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
1948 return it->face_id;
1949
1950 /* Set pos to the position before or after IT's current position. */
1951 if (before_p)
1952 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
1953 else
1954 pos = string_pos (IT_STRING_CHARPOS (*it) + 1, it->string);
1955
1956 /* Get the face for ASCII, or unibyte. */
1957 face_id
1958 = face_at_string_position (it->w,
1959 it->string,
1960 CHARPOS (pos),
1961 (it->current.overlay_string_index >= 0
1962 ? IT_CHARPOS (*it)
1963 : 0),
1964 it->region_beg_charpos,
1965 it->region_end_charpos,
1966 &next_check_charpos,
1967 it->base_face_id);
1968
1969 /* Correct the face for charsets different from ASCII. Do it
1970 for the multibyte case only. The face returned above is
1971 suitable for unibyte text if IT->string is unibyte. */
1972 if (STRING_MULTIBYTE (it->string))
1973 {
1974 unsigned char *p = XSTRING (it->string)->data + BYTEPOS (pos);
1975 int rest = STRING_BYTES (XSTRING (it->string)) - BYTEPOS (pos);
1976 int c, len, charset;
1977
1978 c = string_char_and_length (p, rest, &len);
1979 charset = CHAR_CHARSET (c);
1980 if (charset != CHARSET_ASCII)
1981 face_id = FACE_FOR_CHARSET (it->f, face_id, charset);
1982 }
1983 }
1984 else
1985 {
1986 if ((IT_CHARPOS (*it) >= ZV && !before_p)
1987 || (IT_CHARPOS (*it) <= BEGV && before_p))
1988 return it->face_id;
1989
1990 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
1991 pos = it->current.pos;
1992
1993 if (before_p)
1994 DEC_TEXT_POS (pos);
1995 else
1996 INC_TEXT_POS (pos);
1997
1998 /* Determine face for CHARSET_ASCII, or unibyte. */
1999 face_id = face_at_buffer_position (it->w,
2000 CHARPOS (pos),
2001 it->region_beg_charpos,
2002 it->region_end_charpos,
2003 &next_check_charpos,
2004 limit, 0);
2005
2006 /* Correct the face for charsets different from ASCII. Do it
2007 for the multibyte case only. The face returned above is
2008 suitable for unibyte text if current_buffer is unibyte. */
2009 if (it->multibyte_p)
2010 {
2011 int charset = charset_at_position (pos);
2012 if (charset != CHARSET_ASCII)
2013 face_id = FACE_FOR_CHARSET (it->f, face_id, charset);
2014 }
2015 }
2016
2017 return face_id;
2018 }
2019
2020
2021 \f
2022 /***********************************************************************
2023 Invisible text
2024 ***********************************************************************/
2025
2026 /* Set up iterator IT from invisible properties at its current
2027 position. Called from handle_stop. */
2028
2029 static enum prop_handled
2030 handle_invisible_prop (it)
2031 struct it *it;
2032 {
2033 enum prop_handled handled = HANDLED_NORMALLY;
2034
2035 if (STRINGP (it->string))
2036 {
2037 extern Lisp_Object Qinvisible;
2038 Lisp_Object prop, end_charpos, limit, charpos;
2039
2040 /* Get the value of the invisible text property at the
2041 current position. Value will be nil if there is no such
2042 property. */
2043 XSETFASTINT (charpos, IT_STRING_CHARPOS (*it));
2044 prop = Fget_text_property (charpos, Qinvisible, it->string);
2045
2046 if (!NILP (prop))
2047 {
2048 handled = HANDLED_RECOMPUTE_PROPS;
2049
2050 /* Get the position at which the next change of the
2051 invisible text property can be found in IT->string.
2052 Value will be nil if the property value is the same for
2053 all the rest of IT->string. */
2054 XSETINT (limit, XSTRING (it->string)->size);
2055 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2056 it->string, limit);
2057
2058 /* Text at current position is invisible. The next
2059 change in the property is at position end_charpos.
2060 Move IT's current position to that position. */
2061 if (INTEGERP (end_charpos)
2062 && XFASTINT (end_charpos) < XFASTINT (limit))
2063 {
2064 struct text_pos old;
2065 old = it->current.string_pos;
2066 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
2067 compute_string_pos (&it->current.string_pos, old, it->string);
2068 }
2069 else
2070 {
2071 /* The rest of the string is invisible. If this is an
2072 overlay string, proceed with the next overlay string
2073 or whatever comes and return a character from there. */
2074 if (it->current.overlay_string_index >= 0)
2075 {
2076 next_overlay_string (it);
2077 /* Don't check for overlay strings when we just
2078 finished processing them. */
2079 handled = HANDLED_OVERLAY_STRING_CONSUMED;
2080 }
2081 else
2082 {
2083 struct Lisp_String *s = XSTRING (it->string);
2084 IT_STRING_CHARPOS (*it) = s->size;
2085 IT_STRING_BYTEPOS (*it) = STRING_BYTES (s);
2086 }
2087 }
2088 }
2089 }
2090 else
2091 {
2092 int visible_p, newpos, next_stop;
2093 Lisp_Object pos, prop;
2094
2095 /* First of all, is there invisible text at this position? */
2096 XSETFASTINT (pos, IT_CHARPOS (*it));
2097 prop = Fget_char_property (pos, Qinvisible, it->window);
2098
2099 /* If we are on invisible text, skip over it. */
2100 if (TEXT_PROP_MEANS_INVISIBLE (prop))
2101 {
2102 /* Record whether we have to display an ellipsis for the
2103 invisible text. */
2104 int display_ellipsis_p
2105 = TEXT_PROP_MEANS_INVISIBLE_WITH_ELLIPSIS (prop);
2106
2107 handled = HANDLED_RECOMPUTE_PROPS;
2108
2109 /* Loop skipping over invisible text. The loop is left at
2110 ZV or with IT on the first char being visible again. */
2111 do
2112 {
2113 /* Try to skip some invisible text. Return value is the
2114 position reached which can be equal to IT's position
2115 if there is nothing invisible here. This skips both
2116 over invisible text properties and overlays with
2117 invisible property. */
2118 newpos = skip_invisible (IT_CHARPOS (*it),
2119 &next_stop, ZV, it->window);
2120
2121 /* If we skipped nothing at all we weren't at invisible
2122 text in the first place. If everything to the end of
2123 the buffer was skipped, end the loop. */
2124 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
2125 visible_p = 1;
2126 else
2127 {
2128 /* We skipped some characters but not necessarily
2129 all there are. Check if we ended up on visible
2130 text. Fget_char_property returns the property of
2131 the char before the given position, i.e. if we
2132 get visible_p = 1, this means that the char at
2133 newpos is visible. */
2134 XSETFASTINT (pos, newpos);
2135 prop = Fget_char_property (pos, Qinvisible, it->window);
2136 visible_p = !TEXT_PROP_MEANS_INVISIBLE (prop);
2137 }
2138
2139 /* If we ended up on invisible text, proceed to
2140 skip starting with next_stop. */
2141 if (!visible_p)
2142 IT_CHARPOS (*it) = next_stop;
2143 }
2144 while (!visible_p);
2145
2146 /* The position newpos is now either ZV or on visible text. */
2147 IT_CHARPOS (*it) = newpos;
2148 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2149
2150 /* Maybe return `...' next for the end of the invisible text. */
2151 if (display_ellipsis_p)
2152 {
2153 if (it->dp
2154 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
2155 {
2156 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
2157 it->dpvec = v->contents;
2158 it->dpend = v->contents + v->size;
2159 }
2160 else
2161 {
2162 /* Default `...'. */
2163 it->dpvec = default_invis_vector;
2164 it->dpend = default_invis_vector + 3;
2165 }
2166
2167 /* The ellipsis display does not replace the display of
2168 the character at the new position. Indicate this by
2169 setting IT->dpvec_char_len to zero. */
2170 it->dpvec_char_len = 0;
2171
2172 it->current.dpvec_index = 0;
2173 it->method = next_element_from_display_vector;
2174 }
2175 }
2176 }
2177
2178 return handled;
2179 }
2180
2181
2182 \f
2183 /***********************************************************************
2184 'display' property
2185 ***********************************************************************/
2186
2187 /* Set up iterator IT from `display' property at its current position.
2188 Called from handle_stop. */
2189
2190 static enum prop_handled
2191 handle_display_prop (it)
2192 struct it *it;
2193 {
2194 Lisp_Object prop, object;
2195 struct text_pos *position;
2196 int space_or_image_found_p;
2197
2198 if (STRINGP (it->string))
2199 {
2200 object = it->string;
2201 position = &it->current.string_pos;
2202 }
2203 else
2204 {
2205 object = Qnil;
2206 position = &it->current.pos;
2207 }
2208
2209 /* Reset those iterator values set from display property values. */
2210 it->font_height = Qnil;
2211 it->space_width = Qnil;
2212 it->voffset = 0;
2213
2214 /* We don't support recursive `display' properties, i.e. string
2215 values that have a string `display' property, that have a string
2216 `display' property etc. */
2217 if (!it->string_from_display_prop_p)
2218 it->area = TEXT_AREA;
2219
2220 prop = Fget_char_property (make_number (position->charpos),
2221 Qdisplay, object);
2222 if (NILP (prop))
2223 return HANDLED_NORMALLY;
2224
2225 space_or_image_found_p = 0;
2226 if (CONSP (prop) && CONSP (XCAR (prop)))
2227 {
2228 while (CONSP (prop))
2229 {
2230 if (handle_single_display_prop (it, XCAR (prop), object, position))
2231 space_or_image_found_p = 1;
2232 prop = XCDR (prop);
2233 }
2234 }
2235 else if (VECTORP (prop))
2236 {
2237 int i;
2238 for (i = 0; i < XVECTOR (prop)->size; ++i)
2239 if (handle_single_display_prop (it, XVECTOR (prop)->contents[i],
2240 object, position))
2241 space_or_image_found_p = 1;
2242 }
2243 else
2244 {
2245 if (handle_single_display_prop (it, prop, object, position))
2246 space_or_image_found_p = 1;
2247 }
2248
2249 return space_or_image_found_p ? HANDLED_RETURN : HANDLED_NORMALLY;
2250 }
2251
2252
2253 /* Value is the position of the end of the `display' property stating
2254 at START_POS in OBJECT. */
2255
2256 static struct text_pos
2257 display_prop_end (it, object, start_pos)
2258 struct it *it;
2259 Lisp_Object object;
2260 struct text_pos start_pos;
2261 {
2262 Lisp_Object end;
2263 struct text_pos end_pos;
2264
2265 /* Characters having this form of property are not displayed, so
2266 we have to find the end of the property. */
2267 end = Fnext_single_property_change (make_number (start_pos.charpos),
2268 Qdisplay, object, Qnil);
2269 if (NILP (end))
2270 {
2271 /* A nil value of `end' means there are no changes of the
2272 property to the end of the buffer or string. */
2273 if (it->current.overlay_string_index >= 0)
2274 end_pos.charpos = XSTRING (it->string)->size;
2275 else
2276 end_pos.charpos = it->end_charpos;
2277 }
2278 else
2279 end_pos.charpos = XFASTINT (end);
2280
2281 if (STRINGP (it->string))
2282 compute_string_pos (&end_pos, start_pos, it->string);
2283 else
2284 end_pos.bytepos = CHAR_TO_BYTE (end_pos.charpos);
2285
2286 return end_pos;
2287 }
2288
2289
2290 /* Set up IT from a single `display' sub-property value PROP. OBJECT
2291 is the object in which the `display' property was found. *POSITION
2292 is the position at which it was found.
2293
2294 If PROP is a `space' or `image' sub-property, set *POSITION to the
2295 end position of the `display' property.
2296
2297 Value is non-zero if a `space' or `image' property value was found. */
2298
2299 static int
2300 handle_single_display_prop (it, prop, object, position)
2301 struct it *it;
2302 Lisp_Object prop;
2303 Lisp_Object object;
2304 struct text_pos *position;
2305 {
2306 Lisp_Object value;
2307 int space_or_image_found_p = 0;
2308
2309 Lisp_Object form;
2310
2311 /* If PROP is a list of the form `(:when FORM VALUE)', FORM is
2312 evaluated. If the result is nil, VALUE is ignored. */
2313 form = Qt;
2314 if (CONSP (prop) && EQ (XCAR (prop), QCwhen))
2315 {
2316 prop = XCDR (prop);
2317 if (!CONSP (prop))
2318 return 0;
2319 form = XCAR (prop);
2320 prop = XCDR (prop);
2321 if (!CONSP (prop))
2322 return 0;
2323 prop = XCAR (prop);
2324 }
2325
2326 if (!NILP (form) && !EQ (form, Qt))
2327 {
2328 struct gcpro gcpro1;
2329 struct text_pos end_pos, pt;
2330
2331 end_pos = display_prop_end (it, object, *position);
2332 GCPRO1 (form);
2333
2334 /* Temporarily set point to the end position, and then evaluate
2335 the form. This makes `(eolp)' work as FORM. */
2336 CHARPOS (pt) = PT;
2337 BYTEPOS (pt) = PT_BYTE;
2338 TEMP_SET_PT_BOTH (CHARPOS (end_pos), BYTEPOS (end_pos));
2339 form = eval_form (form);
2340 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
2341 UNGCPRO;
2342 }
2343
2344 if (NILP (form))
2345 return 0;
2346
2347 if (CONSP (prop)
2348 && EQ (XCAR (prop), Qheight)
2349 && CONSP (XCDR (prop)))
2350 {
2351 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2352 return 0;
2353
2354 /* `(height HEIGHT)'. */
2355 it->font_height = XCAR (XCDR (prop));
2356 if (!NILP (it->font_height))
2357 {
2358 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2359 int new_height = -1;
2360
2361 if (CONSP (it->font_height)
2362 && (EQ (XCAR (it->font_height), Qplus)
2363 || EQ (XCAR (it->font_height), Qminus))
2364 && CONSP (XCDR (it->font_height))
2365 && INTEGERP (XCAR (XCDR (it->font_height))))
2366 {
2367 /* `(+ N)' or `(- N)' where N is an integer. */
2368 int steps = XINT (XCAR (XCDR (it->font_height)));
2369 if (EQ (XCAR (it->font_height), Qplus))
2370 steps = - steps;
2371 it->face_id = smaller_face (it->f, it->face_id, steps);
2372 }
2373 else if (SYMBOLP (it->font_height))
2374 {
2375 /* Call function with current height as argument.
2376 Value is the new height. */
2377 Lisp_Object form, height;
2378 struct gcpro gcpro1;
2379
2380 height = face->lface[LFACE_HEIGHT_INDEX];
2381 form = Fcons (it->font_height, Fcons (height, Qnil));
2382 GCPRO1 (form);
2383 height = eval_form (form);
2384 if (NUMBERP (height))
2385 new_height = XFLOATINT (height);
2386 UNGCPRO;
2387 }
2388 else if (NUMBERP (it->font_height))
2389 {
2390 /* Value is a multiple of the canonical char height. */
2391 struct face *face;
2392
2393 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
2394 new_height = (XFLOATINT (it->font_height)
2395 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
2396 }
2397 else
2398 {
2399 /* Evaluate IT->font_height with `height' bound to the
2400 current specified height to get the new height. */
2401 Lisp_Object value;
2402 int count = specpdl_ptr - specpdl;
2403
2404 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
2405 value = eval_form (it->font_height);
2406 unbind_to (count, Qnil);
2407
2408 if (NUMBERP (value))
2409 new_height = XFLOATINT (value);
2410 }
2411
2412 if (new_height > 0)
2413 it->face_id = face_with_height (it->f, it->face_id, new_height);
2414 }
2415 }
2416 else if (CONSP (prop)
2417 && EQ (XCAR (prop), Qspace_width)
2418 && CONSP (XCDR (prop)))
2419 {
2420 /* `(space_width WIDTH)'. */
2421 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2422 return 0;
2423
2424 value = XCAR (XCDR (prop));
2425 if (NUMBERP (value) && XFLOATINT (value) > 0)
2426 it->space_width = value;
2427 }
2428 else if (CONSP (prop)
2429 && EQ (XCAR (prop), Qraise)
2430 && CONSP (XCDR (prop)))
2431 {
2432 #ifdef HAVE_WINDOW_SYSTEM
2433 /* `(raise FACTOR)'. */
2434 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2435 return 0;
2436
2437 value = XCAR (XCDR (prop));
2438 if (NUMBERP (value))
2439 {
2440 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2441 it->voffset = - (XFLOATINT (value)
2442 * (face->font->ascent + face->font->descent));
2443 }
2444 #endif /* HAVE_WINDOW_SYSTEM */
2445 }
2446 else if (!it->string_from_display_prop_p)
2447 {
2448 /* `(left-margin VALUE)' or `(right-margin VALUE)
2449 or `(nil VALUE)' or VALUE. */
2450 Lisp_Object location, value;
2451 struct text_pos start_pos;
2452 int valid_p;
2453
2454 /* Characters having this form of property are not displayed, so
2455 we have to find the end of the property. */
2456 space_or_image_found_p = 1;
2457 start_pos = *position;
2458 *position = display_prop_end (it, object, start_pos);
2459
2460 /* Let's stop at the new position and assume that all
2461 text properties change there. */
2462 it->stop_charpos = position->charpos;
2463
2464 if (CONSP (prop)
2465 && !EQ (XCAR (prop), Qspace)
2466 && !EQ (XCAR (prop), Qimage))
2467 {
2468 location = XCAR (prop);
2469 value = XCDR (prop);
2470 }
2471 else
2472 {
2473 location = Qnil;
2474 value = prop;
2475 }
2476
2477 #ifdef HAVE_WINDOW_SYSTEM
2478 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2479 valid_p = STRINGP (value);
2480 else
2481 valid_p = (STRINGP (value)
2482 || (CONSP (value) && EQ (XCAR (value), Qspace))
2483 || valid_image_p (value));
2484 #else /* not HAVE_WINDOW_SYSTEM */
2485 valid_p = STRINGP (value);
2486 #endif /* not HAVE_WINDOW_SYSTEM */
2487
2488 if ((EQ (location, Qleft_margin)
2489 || EQ (location, Qright_margin)
2490 || NILP (location))
2491 && valid_p)
2492 {
2493 /* Save current settings of IT so that we can restore them
2494 when we are finished with the glyph property value. */
2495 push_it (it);
2496
2497 if (NILP (location))
2498 it->area = TEXT_AREA;
2499 else if (EQ (location, Qleft_margin))
2500 it->area = LEFT_MARGIN_AREA;
2501 else
2502 it->area = RIGHT_MARGIN_AREA;
2503
2504 if (STRINGP (value))
2505 {
2506 it->string = value;
2507 it->multibyte_p = STRING_MULTIBYTE (it->string);
2508 it->current.overlay_string_index = -1;
2509 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
2510 it->end_charpos = it->string_nchars
2511 = XSTRING (it->string)->size;
2512 it->method = next_element_from_string;
2513 it->stop_charpos = 0;
2514 it->string_from_display_prop_p = 1;
2515 }
2516 else if (CONSP (value) && EQ (XCAR (value), Qspace))
2517 {
2518 it->method = next_element_from_stretch;
2519 it->object = value;
2520 it->current.pos = it->position = start_pos;
2521 }
2522 #ifdef HAVE_WINDOW_SYSTEM
2523 else
2524 {
2525 it->what = IT_IMAGE;
2526 it->image_id = lookup_image (it->f, value);
2527 it->position = start_pos;
2528 it->object = NILP (object) ? it->w->buffer : object;
2529 it->method = next_element_from_image;
2530
2531 /* Say that we don't have consumed the characters with
2532 `display' property yet. The call to pop_it in
2533 set_iterator_to_next will clean this up. */
2534 *position = start_pos;
2535 }
2536 #endif /* HAVE_WINDOW_SYSTEM */
2537 }
2538 }
2539
2540 return space_or_image_found_p;
2541 }
2542
2543
2544 \f
2545 /***********************************************************************
2546 Overlay strings
2547 ***********************************************************************/
2548
2549 /* The following structure is used to record overlay strings for
2550 later sorting in load_overlay_strings. */
2551
2552 struct overlay_entry
2553 {
2554 Lisp_Object string;
2555 int priority;
2556 int after_string_p;
2557 };
2558
2559
2560 /* Set up iterator IT from overlay strings at its current position.
2561 Called from handle_stop. */
2562
2563 static enum prop_handled
2564 handle_overlay_change (it)
2565 struct it *it;
2566 {
2567 /* Overlays are handled in current_buffer only. */
2568 if (STRINGP (it->string))
2569 return HANDLED_NORMALLY;
2570 else
2571 return (get_overlay_strings (it)
2572 ? HANDLED_RECOMPUTE_PROPS
2573 : HANDLED_NORMALLY);
2574 }
2575
2576
2577 /* Set up the next overlay string for delivery by IT, if there is an
2578 overlay string to deliver. Called by set_iterator_to_next when the
2579 end of the current overlay string is reached. If there are more
2580 overlay strings to display, IT->string and
2581 IT->current.overlay_string_index are set appropriately here.
2582 Otherwise IT->string is set to nil. */
2583
2584 static void
2585 next_overlay_string (it)
2586 struct it *it;
2587 {
2588 ++it->current.overlay_string_index;
2589 if (it->current.overlay_string_index == it->n_overlay_strings)
2590 {
2591 /* No more overlay strings. Restore IT's settings to what
2592 they were before overlay strings were processed, and
2593 continue to deliver from current_buffer. */
2594 pop_it (it);
2595 xassert (it->stop_charpos >= BEGV
2596 && it->stop_charpos <= it->end_charpos);
2597 it->string = Qnil;
2598 it->current.overlay_string_index = -1;
2599 SET_TEXT_POS (it->current.string_pos, -1, -1);
2600 it->n_overlay_strings = 0;
2601 it->method = next_element_from_buffer;
2602 }
2603 else
2604 {
2605 /* There are more overlay strings to process. If
2606 IT->current.overlay_string_index has advanced to a position
2607 where we must load IT->overlay_strings with more strings, do
2608 it. */
2609 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
2610
2611 if (it->current.overlay_string_index && i == 0)
2612 load_overlay_strings (it);
2613
2614 /* Initialize IT to deliver display elements from the overlay
2615 string. */
2616 it->string = it->overlay_strings[i];
2617 it->multibyte_p = STRING_MULTIBYTE (it->string);
2618 SET_TEXT_POS (it->current.string_pos, 0, 0);
2619 it->method = next_element_from_string;
2620 it->stop_charpos = 0;
2621 }
2622
2623 CHECK_IT (it);
2624 }
2625
2626
2627 /* Compare two overlay_entry structures E1 and E2. Used as a
2628 comparison function for qsort in load_overlay_strings. Overlay
2629 strings for the same position are sorted so that
2630
2631 1. All after-strings come in front of before-strings.
2632
2633 2. Within after-strings, strings are sorted so that overlay strings
2634 from overlays with higher priorities come first.
2635
2636 2. Within before-strings, strings are sorted so that overlay
2637 strings from overlays with higher priorities come last.
2638
2639 Value is analogous to strcmp. */
2640
2641
2642 static int
2643 compare_overlay_entries (e1, e2)
2644 void *e1, *e2;
2645 {
2646 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
2647 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
2648 int result;
2649
2650 if (entry1->after_string_p != entry2->after_string_p)
2651 /* Let after-strings appear in front of before-strings. */
2652 result = entry1->after_string_p ? -1 : 1;
2653 else if (entry1->after_string_p)
2654 /* After-strings sorted in order of decreasing priority. */
2655 result = entry2->priority - entry1->priority;
2656 else
2657 /* Before-strings sorted in order of increasing priority. */
2658 result = entry1->priority - entry2->priority;
2659
2660 return result;
2661 }
2662
2663
2664 /* Load the vector IT->overlay_strings with overlay strings from IT's
2665 current buffer position. Set IT->n_overlays to the total number of
2666 overlay strings found.
2667
2668 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
2669 a time. On entry into load_overlay_strings,
2670 IT->current.overlay_string_index gives the number of overlay
2671 strings that have already been loaded by previous calls to this
2672 function.
2673
2674 Overlay strings are sorted so that after-string strings come in
2675 front of before-string strings. Within before and after-strings,
2676 strings are sorted by overlay priority. See also function
2677 compare_overlay_entries. */
2678
2679 static void
2680 load_overlay_strings (it)
2681 struct it *it;
2682 {
2683 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
2684 Lisp_Object ov, overlay, window, str;
2685 int start, end;
2686 int size = 20;
2687 int n = 0, i, j;
2688 struct overlay_entry *entries
2689 = (struct overlay_entry *) alloca (size * sizeof *entries);
2690
2691 /* Append the overlay string STRING of overlay OVERLAY to vector
2692 `entries' which has size `size' and currently contains `n'
2693 elements. AFTER_P non-zero means STRING is an after-string of
2694 OVERLAY. */
2695 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
2696 do \
2697 { \
2698 Lisp_Object priority; \
2699 \
2700 if (n == size) \
2701 { \
2702 int new_size = 2 * size; \
2703 struct overlay_entry *old = entries; \
2704 entries = \
2705 (struct overlay_entry *) alloca (new_size \
2706 * sizeof *entries); \
2707 bcopy (old, entries, size * sizeof *entries); \
2708 size = new_size; \
2709 } \
2710 \
2711 entries[n].string = (STRING); \
2712 priority = Foverlay_get ((OVERLAY), Qpriority); \
2713 entries[n].priority \
2714 = INTEGERP (priority) ? XFASTINT (priority) : 0; \
2715 entries[n].after_string_p = (AFTER_P); \
2716 ++n; \
2717 } \
2718 while (0)
2719
2720 /* Process overlay before the overlay center. */
2721 for (ov = current_buffer->overlays_before;
2722 CONSP (ov);
2723 ov = XCONS (ov)->cdr)
2724 {
2725 overlay = XCONS (ov)->car;
2726 xassert (OVERLAYP (overlay));
2727 start = OVERLAY_POSITION (OVERLAY_START (overlay));
2728 end = OVERLAY_POSITION (OVERLAY_END (overlay));
2729
2730 if (end < IT_CHARPOS (*it))
2731 break;
2732
2733 /* Skip this overlay if it doesn't start or end at IT's current
2734 position. */
2735 if (end != IT_CHARPOS (*it) && start != IT_CHARPOS (*it))
2736 continue;
2737
2738 /* Skip this overlay if it doesn't apply to IT->w. */
2739 window = Foverlay_get (overlay, Qwindow);
2740 if (WINDOWP (window) && XWINDOW (window) != it->w)
2741 continue;
2742
2743 /* If overlay has a non-empty before-string, record it. */
2744 if (start == IT_CHARPOS (*it)
2745 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
2746 && XSTRING (str)->size)
2747 RECORD_OVERLAY_STRING (overlay, str, 0);
2748
2749 /* If overlay has a non-empty after-string, record it. */
2750 if (end == IT_CHARPOS (*it)
2751 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
2752 && XSTRING (str)->size)
2753 RECORD_OVERLAY_STRING (overlay, str, 1);
2754 }
2755
2756 /* Process overlays after the overlay center. */
2757 for (ov = current_buffer->overlays_after;
2758 CONSP (ov);
2759 ov = XCONS (ov)->cdr)
2760 {
2761 overlay = XCONS (ov)->car;
2762 xassert (OVERLAYP (overlay));
2763 start = OVERLAY_POSITION (OVERLAY_START (overlay));
2764 end = OVERLAY_POSITION (OVERLAY_END (overlay));
2765
2766 if (start > IT_CHARPOS (*it))
2767 break;
2768
2769 /* Skip this overlay if it doesn't start or end at IT's current
2770 position. */
2771 if (end != IT_CHARPOS (*it) && start != IT_CHARPOS (*it))
2772 continue;
2773
2774 /* Skip this overlay if it doesn't apply to IT->w. */
2775 window = Foverlay_get (overlay, Qwindow);
2776 if (WINDOWP (window) && XWINDOW (window) != it->w)
2777 continue;
2778
2779 /* If overlay has a non-empty before-string, record it. */
2780 if (start == IT_CHARPOS (*it)
2781 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
2782 && XSTRING (str)->size)
2783 RECORD_OVERLAY_STRING (overlay, str, 0);
2784
2785 /* If overlay has a non-empty after-string, record it. */
2786 if (end == IT_CHARPOS (*it)
2787 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
2788 && XSTRING (str)->size)
2789 RECORD_OVERLAY_STRING (overlay, str, 1);
2790 }
2791
2792 #undef RECORD_OVERLAY_STRING
2793
2794 /* Sort entries. */
2795 qsort (entries, n, sizeof *entries, compare_overlay_entries);
2796
2797 /* Record the total number of strings to process. */
2798 it->n_overlay_strings = n;
2799
2800 /* IT->current.overlay_string_index is the number of overlay strings
2801 that have already been consumed by IT. Copy some of the
2802 remaining overlay strings to IT->overlay_strings. */
2803 i = 0;
2804 j = it->current.overlay_string_index;
2805 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
2806 it->overlay_strings[i++] = entries[j++].string;
2807
2808 CHECK_IT (it);
2809 }
2810
2811
2812 /* Get the first chunk of overlay strings at IT's current buffer
2813 position. Value is non-zero if at least one overlay string was
2814 found. */
2815
2816 static int
2817 get_overlay_strings (it)
2818 struct it *it;
2819 {
2820 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
2821 process. This fills IT->overlay_strings with strings, and sets
2822 IT->n_overlay_strings to the total number of strings to process.
2823 IT->pos.overlay_string_index has to be set temporarily to zero
2824 because load_overlay_strings needs this; it must be set to -1
2825 when no overlay strings are found because a zero value would
2826 indicate a position in the first overlay string. */
2827 it->current.overlay_string_index = 0;
2828 load_overlay_strings (it);
2829
2830 /* If we found overlay strings, set up IT to deliver display
2831 elements from the first one. Otherwise set up IT to deliver
2832 from current_buffer. */
2833 if (it->n_overlay_strings)
2834 {
2835 /* Make sure we know settings in current_buffer, so that we can
2836 restore meaningful values when we're done with the overlay
2837 strings. */
2838 compute_stop_pos (it);
2839 xassert (it->face_id >= 0);
2840
2841 /* Save IT's settings. They are restored after all overlay
2842 strings have been processed. */
2843 xassert (it->sp == 0);
2844 push_it (it);
2845
2846 /* Set up IT to deliver display elements from the first overlay
2847 string. */
2848 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
2849 it->stop_charpos = 0;
2850 it->string = it->overlay_strings[0];
2851 it->multibyte_p = STRING_MULTIBYTE (it->string);
2852 xassert (STRINGP (it->string));
2853 it->method = next_element_from_string;
2854 }
2855 else
2856 {
2857 it->string = Qnil;
2858 it->current.overlay_string_index = -1;
2859 it->method = next_element_from_buffer;
2860 }
2861
2862 CHECK_IT (it);
2863
2864 /* Value is non-zero if we found at least one overlay string. */
2865 return STRINGP (it->string);
2866 }
2867
2868
2869 \f
2870 /***********************************************************************
2871 Saving and restoring state
2872 ***********************************************************************/
2873
2874 /* Save current settings of IT on IT->stack. Called, for example,
2875 before setting up IT for an overlay string, to be able to restore
2876 IT's settings to what they were after the overlay string has been
2877 processed. */
2878
2879 static void
2880 push_it (it)
2881 struct it *it;
2882 {
2883 struct iterator_stack_entry *p;
2884
2885 xassert (it->sp < 2);
2886 p = it->stack + it->sp;
2887
2888 p->stop_charpos = it->stop_charpos;
2889 xassert (it->face_id >= 0);
2890 p->face_id = it->face_id;
2891 p->string = it->string;
2892 p->pos = it->current;
2893 p->end_charpos = it->end_charpos;
2894 p->string_nchars = it->string_nchars;
2895 p->area = it->area;
2896 p->multibyte_p = it->multibyte_p;
2897 p->space_width = it->space_width;
2898 p->font_height = it->font_height;
2899 p->voffset = it->voffset;
2900 p->string_from_display_prop_p = it->string_from_display_prop_p;
2901 ++it->sp;
2902 }
2903
2904
2905 /* Restore IT's settings from IT->stack. Called, for example, when no
2906 more overlay strings must be processed, and we return to delivering
2907 display elements from a buffer, or when the end of a string from a
2908 `display' property is reached and we return to delivering display
2909 elements from an overlay string, or from a buffer. */
2910
2911 static void
2912 pop_it (it)
2913 struct it *it;
2914 {
2915 struct iterator_stack_entry *p;
2916
2917 xassert (it->sp > 0);
2918 --it->sp;
2919 p = it->stack + it->sp;
2920 it->stop_charpos = p->stop_charpos;
2921 it->face_id = p->face_id;
2922 it->string = p->string;
2923 it->current = p->pos;
2924 it->end_charpos = p->end_charpos;
2925 it->string_nchars = p->string_nchars;
2926 it->area = p->area;
2927 it->multibyte_p = p->multibyte_p;
2928 it->space_width = p->space_width;
2929 it->font_height = p->font_height;
2930 it->voffset = p->voffset;
2931 it->string_from_display_prop_p = p->string_from_display_prop_p;
2932 }
2933
2934
2935 \f
2936 /***********************************************************************
2937 Moving over lines
2938 ***********************************************************************/
2939
2940 /* Set IT's current position to the previous line start. */
2941
2942 static void
2943 back_to_previous_line_start (it)
2944 struct it *it;
2945 {
2946 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
2947 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
2948 }
2949
2950
2951 /* Set IT's current position to the next line start. */
2952
2953 static void
2954 forward_to_next_line_start (it)
2955 struct it *it;
2956 {
2957 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it), 1);
2958 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
2959 }
2960
2961
2962 /* Set IT's current position to the previous visible line start. Skip
2963 invisible text that is so either due to text properties or due to
2964 selective display. Caution: this does not change IT->current_x and
2965 IT->hpos. */
2966
2967 static void
2968 back_to_previous_visible_line_start (it)
2969 struct it *it;
2970 {
2971 int visible_p = 0;
2972
2973 /* Go back one newline if not on BEGV already. */
2974 if (IT_CHARPOS (*it) > BEGV)
2975 back_to_previous_line_start (it);
2976
2977 /* Move over lines that are invisible because of selective display
2978 or text properties. */
2979 while (IT_CHARPOS (*it) > BEGV
2980 && !visible_p)
2981 {
2982 visible_p = 1;
2983
2984 /* If selective > 0, then lines indented more than that values
2985 are invisible. */
2986 if (it->selective > 0
2987 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
2988 it->selective))
2989 visible_p = 0;
2990 #ifdef USE_TEXT_PROPERTIES
2991 else
2992 {
2993 Lisp_Object prop;
2994
2995 prop = Fget_char_property (IT_CHARPOS (*it), Qinvisible, it->window);
2996 if (TEXT_PROP_MEANS_INVISIBLE (prop))
2997 visible_p = 0;
2998 }
2999 #endif /* USE_TEXT_PROPERTIES */
3000
3001 /* Back one more newline if the current one is invisible. */
3002 if (!visible_p)
3003 back_to_previous_line_start (it);
3004 }
3005
3006 xassert (IT_CHARPOS (*it) >= BEGV);
3007 xassert (IT_CHARPOS (*it) == BEGV
3008 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3009 CHECK_IT (it);
3010 }
3011
3012
3013 /* Reseat iterator IT at the previous visible line start. Skip
3014 invisible text that is so either due to text properties or due to
3015 selective display. At the end, update IT's overlay information,
3016 face information etc. */
3017
3018 static void
3019 reseat_at_previous_visible_line_start (it)
3020 struct it *it;
3021 {
3022 back_to_previous_visible_line_start (it);
3023 reseat (it, it->current.pos, 1);
3024 CHECK_IT (it);
3025 }
3026
3027
3028 /* Reseat iterator IT on the next visible line start in the current
3029 buffer. ON_NEWLINE_P non-zero means position IT on the newline
3030 preceding the line start. Skip over invisible text that is so
3031 because of selective display. Compute faces, overlays etc at the
3032 new position. Note that this function does not skip over text that
3033 is invisible because of text properties. */
3034
3035 static void
3036 reseat_at_next_visible_line_start (it, on_newline_p)
3037 struct it *it;
3038 int on_newline_p;
3039 {
3040 /* Restore the buffer position when currently not delivering display
3041 elements from the current buffer. This is the case, for example,
3042 when called at the end of a truncated overlay string. */
3043 while (it->sp)
3044 pop_it (it);
3045 it->method = next_element_from_buffer;
3046
3047 /* Otherwise, scan_buffer would not work. */
3048 if (IT_CHARPOS (*it) < ZV)
3049 {
3050 /* If on a newline, advance past it. Otherwise, find the next
3051 newline which automatically gives us the position following
3052 the newline. */
3053 if (FETCH_BYTE (IT_BYTEPOS (*it)) == '\n')
3054 {
3055 ++IT_CHARPOS (*it);
3056 ++IT_BYTEPOS (*it);
3057 }
3058 else
3059 forward_to_next_line_start (it);
3060
3061 /* We must either have reached the end of the buffer or end up
3062 after a newline. */
3063 xassert (IT_CHARPOS (*it) == ZV
3064 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3065
3066 /* Skip over lines that are invisible because they are indented
3067 more than the value of IT->selective. */
3068 if (it->selective > 0)
3069 while (IT_CHARPOS (*it) < ZV
3070 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3071 it->selective))
3072 forward_to_next_line_start (it);
3073
3074 /* Position on the newline if we should. */
3075 if (on_newline_p && IT_CHARPOS (*it) > BEGV)
3076 {
3077 --IT_CHARPOS (*it);
3078 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3079 }
3080
3081 /* Set the iterator there. The 0 as the last parameter of
3082 reseat means don't force a text property lookup. The lookup
3083 is then only done if we've skipped past the iterator's
3084 check_charpos'es. This optimization is important because
3085 text property lookups tend to be expensive. */
3086 reseat (it, it->current.pos, 0);
3087 }
3088
3089 CHECK_IT (it);
3090 }
3091
3092
3093 \f
3094 /***********************************************************************
3095 Changing an iterator's position
3096 ***********************************************************************/
3097
3098 /* Change IT's current position to POS in current_buffer. If FORCE_P
3099 is non-zero, always check for text properties at the new position.
3100 Otherwise, text properties are only looked up if POS >=
3101 IT->check_charpos of a property. */
3102
3103 static void
3104 reseat (it, pos, force_p)
3105 struct it *it;
3106 struct text_pos pos;
3107 int force_p;
3108 {
3109 int original_pos = IT_CHARPOS (*it);
3110
3111 reseat_1 (it, pos, 0);
3112
3113 /* Determine where to check text properties. Avoid doing it
3114 where possible because text property lookup is very expensive. */
3115 if (force_p
3116 || CHARPOS (pos) > it->stop_charpos
3117 || CHARPOS (pos) < original_pos)
3118 handle_stop (it);
3119
3120 CHECK_IT (it);
3121 }
3122
3123
3124 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
3125 IT->stop_pos to POS, also. */
3126
3127 static void
3128 reseat_1 (it, pos, set_stop_p)
3129 struct it *it;
3130 struct text_pos pos;
3131 int set_stop_p;
3132 {
3133 /* Don't call this function when scanning a C string. */
3134 xassert (it->s == NULL);
3135
3136 /* POS must be a reasonable value. */
3137 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
3138
3139 it->current.pos = it->position = pos;
3140 XSETBUFFER (it->object, current_buffer);
3141 it->dpvec = NULL;
3142 it->current.dpvec_index = -1;
3143 it->current.overlay_string_index = -1;
3144 IT_STRING_CHARPOS (*it) = -1;
3145 IT_STRING_BYTEPOS (*it) = -1;
3146 it->string = Qnil;
3147 it->method = next_element_from_buffer;
3148 it->sp = 0;
3149
3150 if (set_stop_p)
3151 it->stop_charpos = CHARPOS (pos);
3152 }
3153
3154
3155 /* Set up IT for displaying a string, starting at CHARPOS in window W.
3156 If S is non-null, it is a C string to iterate over. Otherwise,
3157 STRING gives a Lisp string to iterate over.
3158
3159 If PRECISION > 0, don't return more then PRECISION number of
3160 characters from the string.
3161
3162 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
3163 characters have been returned. FIELD_WIDTH < 0 means an infinite
3164 field width.
3165
3166 MULTIBYTE = 0 means disable processing of multibyte characters,
3167 MULTIBYTE > 0 means enable it,
3168 MULTIBYTE < 0 means use IT->multibyte_p.
3169
3170 IT must be initialized via a prior call to init_iterator before
3171 calling this function. */
3172
3173 static void
3174 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
3175 struct it *it;
3176 unsigned char *s;
3177 Lisp_Object string;
3178 int charpos;
3179 int precision, field_width, multibyte;
3180 {
3181 /* No region in strings. */
3182 it->region_beg_charpos = it->region_end_charpos = -1;
3183
3184 /* No text property checks performed by default, but see below. */
3185 it->stop_charpos = -1;
3186
3187 /* Set iterator position and end position. */
3188 bzero (&it->current, sizeof it->current);
3189 it->current.overlay_string_index = -1;
3190 it->current.dpvec_index = -1;
3191 it->charset = CHARSET_ASCII;
3192 xassert (charpos >= 0);
3193
3194 /* Use the setting of MULTIBYTE if specified. */
3195 if (multibyte >= 0)
3196 it->multibyte_p = multibyte > 0;
3197
3198 if (s == NULL)
3199 {
3200 xassert (STRINGP (string));
3201 it->string = string;
3202 it->s = NULL;
3203 it->end_charpos = it->string_nchars = XSTRING (string)->size;
3204 it->method = next_element_from_string;
3205 it->current.string_pos = string_pos (charpos, string);
3206 }
3207 else
3208 {
3209 it->s = s;
3210 it->string = Qnil;
3211
3212 /* Note that we use IT->current.pos, not it->current.string_pos,
3213 for displaying C strings. */
3214 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
3215 if (it->multibyte_p)
3216 {
3217 it->current.pos = c_string_pos (charpos, s, 1);
3218 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
3219 }
3220 else
3221 {
3222 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
3223 it->end_charpos = it->string_nchars = strlen (s);
3224 }
3225
3226 it->method = next_element_from_c_string;
3227 }
3228
3229 /* PRECISION > 0 means don't return more than PRECISION characters
3230 from the string. */
3231 if (precision > 0 && it->end_charpos - charpos > precision)
3232 it->end_charpos = it->string_nchars = charpos + precision;
3233
3234 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
3235 characters have been returned. FIELD_WIDTH == 0 means don't pad,
3236 FIELD_WIDTH < 0 means infinite field width. This is useful for
3237 padding with `-' at the end of a mode line. */
3238 if (field_width < 0)
3239 field_width = INFINITY;
3240 if (field_width > it->end_charpos - charpos)
3241 it->end_charpos = charpos + field_width;
3242
3243 /* Use the standard display table for displaying strings. */
3244 if (DISP_TABLE_P (Vstandard_display_table))
3245 it->dp = XCHAR_TABLE (Vstandard_display_table);
3246
3247 it->stop_charpos = charpos;
3248 CHECK_IT (it);
3249 }
3250
3251
3252 \f
3253 /***********************************************************************
3254 Iteration
3255 ***********************************************************************/
3256
3257 /* Load IT's display element fields with information about the next
3258 display element from the current position of IT. Value is zero if
3259 end of buffer (or C string) is reached. */
3260
3261 int
3262 get_next_display_element (it)
3263 struct it *it;
3264 {
3265 /* Non-zero means that we found an display element. Zero means that
3266 we hit the end of what we iterate over. Performance note: the
3267 function pointer `method' used here turns out to be faster than
3268 using a sequence of if-statements. */
3269 int success_p = (*it->method) (it);
3270 int charset;
3271
3272 if (it->what == IT_CHARACTER)
3273 {
3274 /* Map via display table or translate control characters.
3275 IT->c, IT->len etc. have been set to the next character by
3276 the function call above. If we have a display table, and it
3277 contains an entry for IT->c, translate it. Don't do this if
3278 IT->c itself comes from a display table, otherwise we could
3279 end up in an infinite recursion. (An alternative could be to
3280 count the recursion depth of this function and signal an
3281 error when a certain maximum depth is reached.) Is it worth
3282 it? */
3283 if (success_p && it->dpvec == NULL)
3284 {
3285 Lisp_Object dv;
3286
3287 if (it->dp
3288 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
3289 VECTORP (dv)))
3290 {
3291 struct Lisp_Vector *v = XVECTOR (dv);
3292
3293 /* Return the first character from the display table
3294 entry, if not empty. If empty, don't display the
3295 current character. */
3296 if (v->size)
3297 {
3298 it->dpvec_char_len = it->len;
3299 it->dpvec = v->contents;
3300 it->dpend = v->contents + v->size;
3301 it->current.dpvec_index = 0;
3302 it->method = next_element_from_display_vector;
3303 }
3304
3305 success_p = get_next_display_element (it);
3306 }
3307
3308 /* Translate control characters into `\003' or `^C' form.
3309 Control characters coming from a display table entry are
3310 currently not translated because we use IT->dpvec to hold
3311 the translation. This could easily be changed but I
3312 don't believe that it is worth doing. */
3313 else if ((it->c < ' '
3314 && (it->area != TEXT_AREA
3315 || (it->c != '\n' && it->c != '\t')))
3316 || (it->c >= 127
3317 && it->len == 1))
3318 {
3319 /* IT->c is a control character which must be displayed
3320 either as '\003' or as `^C' where the '\\' and '^'
3321 can be defined in the display table. Fill
3322 IT->ctl_chars with glyphs for what we have to
3323 display. Then, set IT->dpvec to these glyphs. */
3324 GLYPH g;
3325
3326 if (it->c < 128 && it->ctl_arrow_p)
3327 {
3328 /* Set IT->ctl_chars[0] to the glyph for `^'. */
3329 if (it->dp
3330 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
3331 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
3332 g = XINT (DISP_CTRL_GLYPH (it->dp));
3333 else
3334 g = FAST_MAKE_GLYPH ('^', 0);
3335 XSETINT (it->ctl_chars[0], g);
3336
3337 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
3338 XSETINT (it->ctl_chars[1], g);
3339
3340 /* Set up IT->dpvec and return first character from it. */
3341 it->dpvec_char_len = it->len;
3342 it->dpvec = it->ctl_chars;
3343 it->dpend = it->dpvec + 2;
3344 it->current.dpvec_index = 0;
3345 it->method = next_element_from_display_vector;
3346 get_next_display_element (it);
3347 }
3348 else
3349 {
3350 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
3351 if (it->dp
3352 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
3353 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
3354 g = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
3355 else
3356 g = FAST_MAKE_GLYPH ('\\', 0);
3357 XSETINT (it->ctl_chars[0], g);
3358
3359 /* Insert three more glyphs into IT->ctl_chars for
3360 the octal display of the character. */
3361 g = FAST_MAKE_GLYPH (((it->c >> 6) & 7) + '0', 0);
3362 XSETINT (it->ctl_chars[1], g);
3363 g = FAST_MAKE_GLYPH (((it->c >> 3) & 7) + '0', 0);
3364 XSETINT (it->ctl_chars[2], g);
3365 g = FAST_MAKE_GLYPH ((it->c & 7) + '0', 0);
3366 XSETINT (it->ctl_chars[3], g);
3367
3368 /* Set up IT->dpvec and return the first character
3369 from it. */
3370 it->dpvec_char_len = it->len;
3371 it->dpvec = it->ctl_chars;
3372 it->dpend = it->dpvec + 4;
3373 it->current.dpvec_index = 0;
3374 it->method = next_element_from_display_vector;
3375 get_next_display_element (it);
3376 }
3377 }
3378 }
3379
3380 /* Adjust face id if charset changes. There are no charset
3381 changes in unibyte text because Emacs' charsets are not
3382 applicable there. */
3383 if (it->multibyte_p
3384 && success_p
3385 && (charset = CHAR_CHARSET (it->c),
3386 charset != it->charset))
3387 {
3388 it->charset = charset;
3389 it->face_id = FACE_FOR_CHARSET (it->f, it->face_id, charset);
3390 }
3391 }
3392
3393 /* Is this character the last one of a run of characters with
3394 box? If yes, set IT->end_of_box_run_p to 1. */
3395 if (it->face_box_p
3396 && it->s == NULL)
3397 {
3398 int face_id;
3399 struct face *face;
3400
3401 it->end_of_box_run_p
3402 = ((face_id = face_after_it_pos (it),
3403 face_id != it->face_id)
3404 && (face = FACE_FROM_ID (it->f, face_id),
3405 face->box == FACE_NO_BOX));
3406 }
3407
3408 /* Value is 0 if end of buffer or string reached. */
3409 return success_p;
3410 }
3411
3412
3413 /* Move IT to the next display element.
3414
3415 Functions get_next_display_element and set_iterator_to_next are
3416 separate because I find this arrangement easier to handle than a
3417 get_next_display_element function that also increments IT's
3418 position. The way it is we can first look at an iterator's current
3419 display element, decide whether it fits on a line, and if it does,
3420 increment the iterator position. The other way around we probably
3421 would either need a flag indicating whether the iterator has to be
3422 incremented the next time, or we would have to implement a
3423 decrement position function which would not be easy to write. */
3424
3425 void
3426 set_iterator_to_next (it)
3427 struct it *it;
3428 {
3429 if (it->method == next_element_from_buffer)
3430 {
3431 /* The current display element of IT is a character from
3432 current_buffer. Advance in the buffer, and maybe skip over
3433 invisible lines that are so because of selective display. */
3434 if (ITERATOR_AT_END_OF_LINE_P (it))
3435 reseat_at_next_visible_line_start (it, 0);
3436 else
3437 {
3438 xassert (it->len != 0);
3439 IT_BYTEPOS (*it) += it->len;
3440 IT_CHARPOS (*it) += 1;
3441 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
3442 }
3443 }
3444 else if (it->method == next_element_from_c_string)
3445 {
3446 /* Current display element of IT is from a C string. */
3447 IT_BYTEPOS (*it) += it->len;
3448 IT_CHARPOS (*it) += 1;
3449 }
3450 else if (it->method == next_element_from_display_vector)
3451 {
3452 /* Current display element of IT is from a display table entry.
3453 Advance in the display table definition. Reset it to null if
3454 end reached, and continue with characters from buffers/
3455 strings. */
3456 ++it->current.dpvec_index;
3457
3458 /* Restore face and charset of the iterator to what they were
3459 before the display vector entry (these entries may contain
3460 faces, and of course characters of different charsets). */
3461 it->face_id = it->saved_face_id;
3462 it->charset = FACE_FROM_ID (it->f, it->face_id)->charset;
3463
3464 if (it->dpvec + it->current.dpvec_index == it->dpend)
3465 {
3466 if (it->s)
3467 it->method = next_element_from_c_string;
3468 else if (STRINGP (it->string))
3469 it->method = next_element_from_string;
3470 else
3471 it->method = next_element_from_buffer;
3472
3473 it->dpvec = NULL;
3474 it->current.dpvec_index = -1;
3475
3476 /* Skip over characters which were displayed via IT->dpvec. */
3477 if (it->dpvec_char_len < 0)
3478 reseat_at_next_visible_line_start (it, 1);
3479 else if (it->dpvec_char_len > 0)
3480 {
3481 it->len = it->dpvec_char_len;
3482 set_iterator_to_next (it);
3483 }
3484 }
3485 }
3486 else if (it->method == next_element_from_string)
3487 {
3488 /* Current display element is a character from a Lisp string. */
3489 xassert (it->s == NULL && STRINGP (it->string));
3490 IT_STRING_BYTEPOS (*it) += it->len;
3491 IT_STRING_CHARPOS (*it) += 1;
3492
3493 consider_string_end:
3494
3495 if (it->current.overlay_string_index >= 0)
3496 {
3497 /* IT->string is an overlay string. Advance to the
3498 next, if there is one. */
3499 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
3500 next_overlay_string (it);
3501 }
3502 else
3503 {
3504 /* IT->string is not an overlay string. If we reached
3505 its end, and there is something on IT->stack, proceed
3506 with what is on the stack. This can be either another
3507 string, this time an overlay string, or a buffer. */
3508 if (IT_STRING_CHARPOS (*it) == XSTRING (it->string)->size
3509 && it->sp > 0)
3510 {
3511 pop_it (it);
3512 if (!STRINGP (it->string))
3513 it->method = next_element_from_buffer;
3514 }
3515 }
3516 }
3517 else if (it->method == next_element_from_image
3518 || it->method == next_element_from_stretch)
3519 {
3520 /* The position etc with which we have to proceed are on
3521 the stack. The position may be at the end of a string,
3522 if the `display' property takes up the whole string. */
3523 pop_it (it);
3524 it->image_id = 0;
3525 if (STRINGP (it->string))
3526 {
3527 it->method = next_element_from_string;
3528 goto consider_string_end;
3529 }
3530 else
3531 it->method = next_element_from_buffer;
3532 }
3533 else
3534 /* There are no other methods defined, so this should be a bug. */
3535 abort ();
3536
3537 /* Reset flags indicating start and end of a sequence of
3538 characters with box. */
3539 it->start_of_box_run_p = it->end_of_box_run_p = 0;
3540
3541 xassert (it->method != next_element_from_string
3542 || (STRINGP (it->string)
3543 && IT_STRING_CHARPOS (*it) >= 0));
3544 }
3545
3546
3547 /* Load IT's display element fields with information about the next
3548 display element which comes from a display table entry or from the
3549 result of translating a control character to one of the forms `^C'
3550 or `\003'. IT->dpvec holds the glyphs to return as characters. */
3551
3552 static int
3553 next_element_from_display_vector (it)
3554 struct it *it;
3555 {
3556 /* Precondition. */
3557 xassert (it->dpvec && it->current.dpvec_index >= 0);
3558
3559 /* Remember the current face id in case glyphs specify faces.
3560 IT's face is restored in set_iterator_to_next. */
3561 it->saved_face_id = it->face_id;
3562
3563 if (INTEGERP (*it->dpvec)
3564 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
3565 {
3566 int lface_id;
3567 GLYPH g;
3568
3569 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
3570 it->c = FAST_GLYPH_CHAR (g);
3571 it->len = CHAR_LEN (it->c);
3572
3573 /* The entry may contain a face id to use. Such a face id is
3574 the id of a Lisp face, not a realized face. A face id of
3575 zero means no face. */
3576 lface_id = FAST_GLYPH_FACE (g);
3577 if (lface_id)
3578 {
3579 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
3580 if (face_id >= 0)
3581 {
3582 it->face_id = face_id;
3583 it->charset = CHARSET_ASCII;
3584 }
3585 }
3586 }
3587 else
3588 /* Display table entry is invalid. Return a space. */
3589 it->c = ' ', it->len = 1;
3590
3591 /* Don't change position and object of the iterator here. They are
3592 still the values of the character that had this display table
3593 entry or was translated, and that's what we want. */
3594 it->what = IT_CHARACTER;
3595 return 1;
3596 }
3597
3598
3599 /* Load IT with the next display element from Lisp string IT->string.
3600 IT->current.string_pos is the current position within the string.
3601 If IT->current.overlay_string_index >= 0, the Lisp string is an
3602 overlay string. */
3603
3604 static int
3605 next_element_from_string (it)
3606 struct it *it;
3607 {
3608 struct text_pos position;
3609
3610 xassert (STRINGP (it->string));
3611 xassert (IT_STRING_CHARPOS (*it) >= 0);
3612 position = it->current.string_pos;
3613
3614 /* Time to check for invisible text? */
3615 if (IT_STRING_CHARPOS (*it) < it->end_charpos
3616 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
3617 {
3618 handle_stop (it);
3619
3620 /* Since a handler may have changed IT->method, we must
3621 recurse here. */
3622 return get_next_display_element (it);
3623 }
3624
3625 if (it->current.overlay_string_index >= 0)
3626 {
3627 /* Get the next character from an overlay string. In overlay
3628 strings, There is no field width or padding with spaces to
3629 do. */
3630 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
3631 {
3632 it->what = IT_EOB;
3633 return 0;
3634 }
3635 else if (STRING_MULTIBYTE (it->string))
3636 {
3637 int remaining = (STRING_BYTES (XSTRING (it->string))
3638 - IT_STRING_BYTEPOS (*it));
3639 unsigned char *s = (XSTRING (it->string)->data
3640 + IT_STRING_BYTEPOS (*it));
3641 it->c = string_char_and_length (s, remaining, &it->len);
3642 }
3643 else
3644 {
3645 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
3646 it->len = 1;
3647 }
3648 }
3649 else
3650 {
3651 /* Get the next character from a Lisp string that is not an
3652 overlay string. Such strings come from the mode line, for
3653 example. We may have to pad with spaces, or truncate the
3654 string. See also next_element_from_c_string. */
3655 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
3656 {
3657 it->what = IT_EOB;
3658 return 0;
3659 }
3660 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
3661 {
3662 /* Pad with spaces. */
3663 it->c = ' ', it->len = 1;
3664 CHARPOS (position) = BYTEPOS (position) = -1;
3665 }
3666 else if (STRING_MULTIBYTE (it->string))
3667 {
3668 int maxlen = (STRING_BYTES (XSTRING (it->string))
3669 - IT_STRING_BYTEPOS (*it));
3670 unsigned char *s = (XSTRING (it->string)->data
3671 + IT_STRING_BYTEPOS (*it));
3672 it->c = string_char_and_length (s, maxlen, &it->len);
3673 }
3674 else
3675 {
3676 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
3677 it->len = 1;
3678 }
3679 }
3680
3681 /* Record what we have and where it came from. Note that we store a
3682 buffer position in IT->position although it could arguably be a
3683 string position. */
3684 it->what = IT_CHARACTER;
3685 it->object = it->string;
3686 it->position = position;
3687 return 1;
3688 }
3689
3690
3691 /* Load IT with next display element from C string IT->s.
3692 IT->string_nchars is the maximum number of characters to return
3693 from the string. IT->end_charpos may be greater than
3694 IT->string_nchars when this function is called, in which case we
3695 may have to return padding spaces. Value is zero if end of string
3696 reached, including padding spaces. */
3697
3698 static int
3699 next_element_from_c_string (it)
3700 struct it *it;
3701 {
3702 int success_p = 1;
3703
3704 xassert (it->s);
3705 it->what = IT_CHARACTER;
3706 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
3707 it->object = Qnil;
3708
3709 /* IT's position can be greater IT->string_nchars in case a field
3710 width or precision has been specified when the iterator was
3711 initialized. */
3712 if (IT_CHARPOS (*it) >= it->end_charpos)
3713 {
3714 /* End of the game. */
3715 it->what = IT_EOB;
3716 success_p = 0;
3717 }
3718 else if (IT_CHARPOS (*it) >= it->string_nchars)
3719 {
3720 /* Pad with spaces. */
3721 it->c = ' ', it->len = 1;
3722 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
3723 }
3724 else if (it->multibyte_p)
3725 {
3726 /* Implementation note: The calls to strlen apparently aren't a
3727 performance problem because there is no noticeable performance
3728 difference between Emacs running in unibyte or multibyte mode. */
3729 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
3730 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
3731 maxlen, &it->len);
3732 }
3733 else
3734 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
3735
3736 return success_p;
3737 }
3738
3739
3740 /* Set up IT to return characters from an ellipsis, if appropriate.
3741 The definition of the ellipsis glyphs may come from a display table
3742 entry. This function Fills IT with the first glyph from the
3743 ellipsis if an ellipsis is to be displayed. */
3744
3745 static void
3746 next_element_from_ellipsis (it)
3747 struct it *it;
3748 {
3749 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3750 {
3751 /* Use the display table definition for `...'. Invalid glyphs
3752 will be handled by the method returning elements from dpvec. */
3753 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3754 it->dpvec_char_len = it->len;
3755 it->dpvec = v->contents;
3756 it->dpend = v->contents + v->size;
3757 it->current.dpvec_index = 0;
3758 it->method = next_element_from_display_vector;
3759 get_next_display_element (it);
3760 }
3761 else if (it->selective_display_ellipsis_p)
3762 {
3763 /* Use default `...' which is stored in default_invis_vector. */
3764 it->dpvec_char_len = it->len;
3765 it->dpvec = default_invis_vector;
3766 it->dpend = default_invis_vector + 3;
3767 it->current.dpvec_index = 0;
3768 it->method = next_element_from_display_vector;
3769 get_next_display_element (it);
3770 }
3771 }
3772
3773
3774 /* Deliver an image display element. The iterator IT is already
3775 filled with image information (done in handle_display_prop). Value
3776 is always 1. */
3777
3778
3779 static int
3780 next_element_from_image (it)
3781 struct it *it;
3782 {
3783 it->what = IT_IMAGE;
3784 return 1;
3785 }
3786
3787
3788 /* Fill iterator IT with next display element from a stretch glyph
3789 property. IT->object is the value of the text property. Value is
3790 always 1. */
3791
3792 static int
3793 next_element_from_stretch (it)
3794 struct it *it;
3795 {
3796 it->what = IT_STRETCH;
3797 return 1;
3798 }
3799
3800
3801 /* Load IT with the next display element from current_buffer. Value
3802 is zero if end of buffer reached. IT->stop_charpos is the next
3803 position at which to stop and check for text properties or buffer
3804 end. */
3805
3806 static int
3807 next_element_from_buffer (it)
3808 struct it *it;
3809 {
3810 int success_p = 1;
3811
3812 /* Check this assumption, otherwise, we would never enter the
3813 if-statement, below. */
3814 xassert (IT_CHARPOS (*it) >= BEGV
3815 && IT_CHARPOS (*it) <= it->stop_charpos);
3816
3817 if (IT_CHARPOS (*it) >= it->stop_charpos)
3818 {
3819 if (IT_CHARPOS (*it) >= it->end_charpos)
3820 {
3821 int overlay_strings_follow_p;
3822
3823 /* End of the game, except when overlay strings follow that
3824 haven't been returned yet. */
3825 if (it->overlay_strings_at_end_processed_p)
3826 overlay_strings_follow_p = 0;
3827 else
3828 {
3829 it->overlay_strings_at_end_processed_p = 1;
3830 overlay_strings_follow_p
3831 = get_overlay_strings (it);
3832 }
3833
3834 if (overlay_strings_follow_p)
3835 success_p = get_next_display_element (it);
3836 else
3837 {
3838 it->what = IT_EOB;
3839 it->position = it->current.pos;
3840 success_p = 0;
3841 }
3842 }
3843 else
3844 {
3845 handle_stop (it);
3846 return get_next_display_element (it);
3847 }
3848 }
3849 else
3850 {
3851 /* No face changes, overlays etc. in sight, so just return a
3852 character from current_buffer. */
3853 unsigned char *p;
3854
3855 /* Maybe run the redisplay end trigger hook. Performance note:
3856 This doesn't seem to cost measurable time. */
3857 if (it->redisplay_end_trigger_charpos
3858 && it->glyph_row
3859 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
3860 run_redisplay_end_trigger_hook (it);
3861
3862 /* Get the next character, maybe multibyte. */
3863 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
3864 if (it->multibyte_p)
3865 {
3866 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
3867 - IT_BYTEPOS (*it));
3868 it->c = string_char_and_length (p, maxlen, &it->len);
3869 }
3870 else
3871 it->c = *p, it->len = 1;
3872
3873 /* Record what we have and where it came from. */
3874 it->what = IT_CHARACTER;;
3875 it->object = it->w->buffer;
3876 it->position = it->current.pos;
3877
3878 /* Normally we return the character found above, except when we
3879 really want to return an ellipsis for selective display. */
3880 if (it->selective)
3881 {
3882 if (it->c == '\n')
3883 {
3884 /* A value of selective > 0 means hide lines indented more
3885 than that number of columns. */
3886 if (it->selective > 0
3887 && IT_CHARPOS (*it) + 1 < ZV
3888 && indented_beyond_p (IT_CHARPOS (*it) + 1,
3889 IT_BYTEPOS (*it) + 1,
3890 it->selective))
3891 {
3892 next_element_from_ellipsis (it);
3893 it->dpvec_char_len = -1;
3894 }
3895 }
3896 else if (it->c == '\r' && it->selective == -1)
3897 {
3898 /* A value of selective == -1 means that everything from the
3899 CR to the end of the line is invisible, with maybe an
3900 ellipsis displayed for it. */
3901 next_element_from_ellipsis (it);
3902 it->dpvec_char_len = -1;
3903 }
3904 }
3905 }
3906
3907 /* Value is zero if end of buffer reached. */
3908 xassert (!success_p || it->len > 0);
3909 return success_p;
3910 }
3911
3912
3913 /* Run the redisplay end trigger hook for IT. */
3914
3915 static void
3916 run_redisplay_end_trigger_hook (it)
3917 struct it *it;
3918 {
3919 Lisp_Object args[3];
3920
3921 /* IT->glyph_row should be non-null, i.e. we should be actually
3922 displaying something, or otherwise we should not run the hook. */
3923 xassert (it->glyph_row);
3924
3925 /* Set up hook arguments. */
3926 args[0] = Qredisplay_end_trigger_functions;
3927 args[1] = it->window;
3928 XSETINT (args[2], it->redisplay_end_trigger_charpos);
3929 it->redisplay_end_trigger_charpos = 0;
3930
3931 /* Since we are *trying* to run these functions, don't try to run
3932 them again, even if they get an error. */
3933 it->w->redisplay_end_trigger = Qnil;
3934 Frun_hook_with_args (3, args);
3935
3936 /* Notice if it changed the face of the character we are on. */
3937 handle_face_prop (it);
3938 }
3939
3940
3941 \f
3942 /***********************************************************************
3943 Moving an iterator without producing glyphs
3944 ***********************************************************************/
3945
3946 /* Move iterator IT to a specified buffer or X position within one
3947 line on the display without producing glyphs.
3948
3949 Begin to skip at IT's current position. Skip to TO_CHARPOS or TO_X
3950 whichever is reached first.
3951
3952 TO_CHARPOS <= 0 means no TO_CHARPOS is specified.
3953
3954 TO_X < 0 means that no TO_X is specified. TO_X is normally a value
3955 0 <= TO_X <= IT->last_visible_x. This means in particular, that
3956 TO_X includes the amount by which a window is horizontally
3957 scrolled.
3958
3959 Value is
3960
3961 MOVE_POS_MATCH_OR_ZV
3962 - when TO_POS or ZV was reached.
3963
3964 MOVE_X_REACHED
3965 -when TO_X was reached before TO_POS or ZV were reached.
3966
3967 MOVE_LINE_CONTINUED
3968 - when we reached the end of the display area and the line must
3969 be continued.
3970
3971 MOVE_LINE_TRUNCATED
3972 - when we reached the end of the display area and the line is
3973 truncated.
3974
3975 MOVE_NEWLINE_OR_CR
3976 - when we stopped at a line end, i.e. a newline or a CR and selective
3977 display is on. */
3978
3979 enum move_it_result
3980 move_it_in_display_line_to (it, to_charpos, to_x, op)
3981 struct it *it;
3982 int to_charpos, to_x, op;
3983 {
3984 enum move_it_result result = MOVE_UNDEFINED;
3985 struct glyph_row *saved_glyph_row;
3986
3987 /* Don't produce glyphs in produce_glyphs. */
3988 saved_glyph_row = it->glyph_row;
3989 it->glyph_row = NULL;
3990
3991 while (1)
3992 {
3993 int x, i;
3994
3995 /* Stop when ZV or TO_CHARPOS reached. */
3996 if (!get_next_display_element (it)
3997 || ((op & MOVE_TO_POS) != 0
3998 && BUFFERP (it->object)
3999 && IT_CHARPOS (*it) >= to_charpos))
4000 {
4001 result = MOVE_POS_MATCH_OR_ZV;
4002 break;
4003 }
4004
4005 /* The call to produce_glyphs will get the metrics of the
4006 display element IT is loaded with. We record in x the
4007 x-position before this display element in case it does not
4008 fit on the line. */
4009 x = it->current_x;
4010 PRODUCE_GLYPHS (it);
4011
4012 if (it->area != TEXT_AREA)
4013 {
4014 set_iterator_to_next (it);
4015 continue;
4016 }
4017
4018 /* The number of glyphs we get back in IT->nglyphs will normally
4019 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
4020 character on a terminal frame, or (iii) a line end. For the
4021 second case, IT->nglyphs - 1 padding glyphs will be present
4022 (on X frames, there is only one glyph produced for a
4023 composite character.
4024
4025 The behavior implemented below means, for continuation lines,
4026 that as many spaces of a TAB as fit on the current line are
4027 displayed there. For terminal frames, as many glyphs of a
4028 multi-glyph character are displayed in the current line, too.
4029 This is what the old redisplay code did, and we keep it that
4030 way. Under X, the whole shape of a complex character must
4031 fit on the line or it will be completely displayed in the
4032 next line.
4033
4034 Note that both for tabs and padding glyphs, all glyphs have
4035 the same width. */
4036 if (it->nglyphs)
4037 {
4038 /* More than one glyph or glyph doesn't fit on line. All
4039 glyphs have the same width. */
4040 int single_glyph_width = it->pixel_width / it->nglyphs;
4041 int new_x;
4042
4043 for (i = 0; i < it->nglyphs; ++i, x = new_x)
4044 {
4045 new_x = x + single_glyph_width;
4046
4047 /* We want to leave anything reaching TO_X to the caller. */
4048 if ((op & MOVE_TO_X) && new_x > to_x)
4049 {
4050 it->current_x = x;
4051 result = MOVE_X_REACHED;
4052 break;
4053 }
4054 else if (/* Lines are continued. */
4055 !it->truncate_lines_p
4056 && (/* And glyph doesn't fit on the line. */
4057 new_x > it->last_visible_x
4058 /* Or it fits exactly and we're on a window
4059 system frame. */
4060 || (new_x == it->last_visible_x
4061 && FRAME_WINDOW_P (it->f))))
4062 {
4063 if (/* IT->hpos == 0 means the very first glyph
4064 doesn't fit on the line, e.g. a wide image. */
4065 it->hpos == 0
4066 || (new_x == it->last_visible_x
4067 && FRAME_WINDOW_P (it->f)))
4068 {
4069 ++it->hpos;
4070 it->current_x = new_x;
4071 if (i == it->nglyphs - 1)
4072 set_iterator_to_next (it);
4073 }
4074 else
4075 it->current_x = x;
4076
4077 result = MOVE_LINE_CONTINUED;
4078 break;
4079 }
4080 else if (new_x > it->first_visible_x)
4081 {
4082 /* Glyph is visible. Increment number of glyphs that
4083 would be displayed. */
4084 ++it->hpos;
4085 }
4086 else
4087 {
4088 /* Glyph is completely off the left margin of the display
4089 area. Nothing to do. */
4090 }
4091 }
4092
4093 if (result != MOVE_UNDEFINED)
4094 break;
4095 }
4096 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
4097 {
4098 /* Stop when TO_X specified and reached. This check is
4099 necessary here because of lines consisting of a line end,
4100 only. The line end will not produce any glyphs and we
4101 would never get MOVE_X_REACHED. */
4102 xassert (it->nglyphs == 0);
4103 result = MOVE_X_REACHED;
4104 break;
4105 }
4106
4107 /* Is this a line end? If yes, we're done. */
4108 if (ITERATOR_AT_END_OF_LINE_P (it))
4109 {
4110 result = MOVE_NEWLINE_OR_CR;
4111 break;
4112 }
4113
4114 /* The current display element has been consumed. Advance
4115 to the next. */
4116 set_iterator_to_next (it);
4117
4118 /* Stop if lines are truncated and IT's current x-position is
4119 past the right edge of the window now. */
4120 if (it->truncate_lines_p
4121 && it->current_x >= it->last_visible_x)
4122 {
4123 result = MOVE_LINE_TRUNCATED;
4124 break;
4125 }
4126 }
4127
4128 /* Restore the iterator settings altered at the beginning of this
4129 function. */
4130 it->glyph_row = saved_glyph_row;
4131 return result;
4132 }
4133
4134
4135 /* Move IT forward to a specified buffer position TO_CHARPOS, TO_X,
4136 TO_Y, TO_VPOS. OP is a bit-mask that specifies where to stop. See
4137 the description of enum move_operation_enum.
4138
4139 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
4140 screen line, this function will set IT to the next position >
4141 TO_CHARPOS. */
4142
4143 void
4144 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
4145 struct it *it;
4146 int to_charpos, to_x, to_y, to_vpos;
4147 int op;
4148 {
4149 enum move_it_result skip, skip2 = MOVE_X_REACHED;
4150 int line_height;
4151
4152 while (1)
4153 {
4154 if (op & MOVE_TO_VPOS)
4155 {
4156 /* If no TO_CHARPOS and no TO_X specified, stop at the
4157 start of the line TO_VPOS. */
4158 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
4159 {
4160 if (it->vpos == to_vpos)
4161 break;
4162 skip = move_it_in_display_line_to (it, -1, -1, 0);
4163 }
4164 else
4165 {
4166 /* TO_VPOS >= 0 means stop at TO_X in the line at
4167 TO_VPOS, or at TO_POS, whichever comes first. */
4168 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
4169
4170 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
4171 break;
4172 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
4173 {
4174 /* We have reached TO_X but not in the line we want. */
4175 skip = move_it_in_display_line_to (it, to_charpos,
4176 -1, MOVE_TO_POS);
4177 if (skip == MOVE_POS_MATCH_OR_ZV)
4178 break;
4179 }
4180 }
4181 }
4182 else if (op & MOVE_TO_Y)
4183 {
4184 struct it it_backup;
4185 int done_p;
4186
4187 /* TO_Y specified means stop at TO_X in the line containing
4188 TO_Y---or at TO_CHARPOS if this is reached first. The
4189 problem is that we can't really tell whether the line
4190 contains TO_Y before we have completely scanned it, and
4191 this may skip past TO_X. What we do is to first scan to
4192 TO_X.
4193
4194 If TO_X is not specified, use a TO_X of zero. The reason
4195 is to make the outcome of this function more predictable.
4196 If we didn't use TO_X == 0, we would stop at the end of
4197 the line which is probably not what a caller would expect
4198 to happen. */
4199 skip = move_it_in_display_line_to (it, to_charpos,
4200 ((op & MOVE_TO_X)
4201 ? to_x : 0),
4202 (MOVE_TO_X
4203 | (op & MOVE_TO_POS)));
4204
4205 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
4206 if (skip == MOVE_POS_MATCH_OR_ZV)
4207 break;
4208
4209 /* If TO_X was reached, we would like to know whether TO_Y
4210 is in the line. This can only be said if we know the
4211 total line height which requires us to scan the rest of
4212 the line. */
4213 done_p = 0;
4214 if (skip == MOVE_X_REACHED)
4215 {
4216 it_backup = *it;
4217 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
4218 op & MOVE_TO_POS);
4219 }
4220
4221 /* Now, decide whether TO_Y is in this line. */
4222 line_height = it->max_ascent + it->max_descent;
4223
4224 if (to_y >= it->current_y
4225 && to_y < it->current_y + line_height)
4226 {
4227 if (skip == MOVE_X_REACHED)
4228 /* If TO_Y is in this line and TO_X was reached above,
4229 we scanned too far. We have to restore IT's settings
4230 to the ones before skipping. */
4231 *it = it_backup;
4232 done_p = 1;
4233 }
4234 else if (skip == MOVE_X_REACHED)
4235 {
4236 skip = skip2;
4237 if (skip == MOVE_POS_MATCH_OR_ZV)
4238 done_p = 1;
4239 }
4240
4241 if (done_p)
4242 break;
4243 }
4244 else
4245 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
4246
4247 switch (skip)
4248 {
4249 case MOVE_POS_MATCH_OR_ZV:
4250 return;
4251
4252 case MOVE_NEWLINE_OR_CR:
4253 set_iterator_to_next (it);
4254 it->continuation_lines_width = 0;
4255 break;
4256
4257 case MOVE_LINE_TRUNCATED:
4258 it->continuation_lines_width = 0;
4259 reseat_at_next_visible_line_start (it, 0);
4260 if ((op & MOVE_TO_POS) != 0
4261 && IT_CHARPOS (*it) > to_charpos)
4262 goto out;
4263 break;
4264
4265 case MOVE_LINE_CONTINUED:
4266 it->continuation_lines_width += it->current_x;
4267 break;
4268
4269 default:
4270 abort ();
4271 }
4272
4273 /* Reset/increment for the next run. */
4274 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
4275 it->current_x = it->hpos = 0;
4276 it->current_y += it->max_ascent + it->max_descent;
4277 ++it->vpos;
4278 last_height = it->max_ascent + it->max_descent;
4279 last_max_ascent = it->max_ascent;
4280 it->max_ascent = it->max_descent = 0;
4281 }
4282 out:;
4283 }
4284
4285
4286 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
4287
4288 If DY > 0, move IT backward at least that many pixels. DY = 0
4289 means move IT backward to the preceding line start or BEGV. This
4290 function may move over more than DY pixels if IT->current_y - DY
4291 ends up in the middle of a line; in this case IT->current_y will be
4292 set to the top of the line moved to. */
4293
4294 void
4295 move_it_vertically_backward (it, dy)
4296 struct it *it;
4297 int dy;
4298 {
4299 int nlines, h, line_height;
4300 struct it it2;
4301 int start_pos = IT_CHARPOS (*it);
4302
4303 xassert (dy >= 0);
4304
4305 /* Estimate how many newlines we must move back. */
4306 nlines = max (1, dy / CANON_Y_UNIT (it->f));
4307
4308 /* Set the iterator's position that many lines back. */
4309 while (nlines-- && IT_CHARPOS (*it) > BEGV)
4310 back_to_previous_visible_line_start (it);
4311
4312 /* Reseat the iterator here. When moving backward, we don't want
4313 reseat to skip forward over invisible text, set up the iterator
4314 to deliver from overlay strings at the new position etc. So,
4315 use reseat_1 here. */
4316 reseat_1 (it, it->current.pos, 1);
4317
4318 /* We are now surely at a line start. */
4319 it->current_x = it->hpos = 0;
4320
4321 /* Move forward and see what y-distance we moved. First move to the
4322 start of the next line so that we get its height. We need this
4323 height to be able to tell whether we reached the specified
4324 y-distance. */
4325 it2 = *it;
4326 it2.max_ascent = it2.max_descent = 0;
4327 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
4328 MOVE_TO_POS | MOVE_TO_VPOS);
4329 xassert (IT_CHARPOS (*it) >= BEGV);
4330 line_height = it2.max_ascent + it2.max_descent;
4331 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
4332 xassert (IT_CHARPOS (*it) >= BEGV);
4333 h = it2.current_y - it->current_y;
4334 nlines = it2.vpos - it->vpos;
4335
4336 /* Correct IT's y and vpos position. */
4337 it->vpos -= nlines;
4338 it->current_y -= h;
4339
4340 if (dy == 0)
4341 {
4342 /* DY == 0 means move to the start of the screen line. The
4343 value of nlines is > 0 if continuation lines were involved. */
4344 if (nlines > 0)
4345 move_it_by_lines (it, nlines, 1);
4346 xassert (IT_CHARPOS (*it) <= start_pos);
4347 }
4348 else if (nlines)
4349 {
4350 /* The y-position we try to reach. Note that h has been
4351 subtracted in front of the if-statement. */
4352 int target_y = it->current_y + h - dy;
4353
4354 /* If we did not reach target_y, try to move further backward if
4355 we can. If we moved too far backward, try to move forward. */
4356 if (target_y < it->current_y
4357 && IT_CHARPOS (*it) > BEGV)
4358 {
4359 move_it_vertically (it, target_y - it->current_y);
4360 xassert (IT_CHARPOS (*it) >= BEGV);
4361 }
4362 else if (target_y >= it->current_y + line_height
4363 && IT_CHARPOS (*it) < ZV)
4364 {
4365 move_it_vertically (it, target_y - (it->current_y + line_height));
4366 xassert (IT_CHARPOS (*it) >= BEGV);
4367 }
4368 }
4369 }
4370
4371
4372 /* Move IT by a specified amount of pixel lines DY. DY negative means
4373 move backwards. DY = 0 means move to start of screen line. At the
4374 end, IT will be on the start of a screen line. */
4375
4376 void
4377 move_it_vertically (it, dy)
4378 struct it *it;
4379 int dy;
4380 {
4381 if (dy <= 0)
4382 move_it_vertically_backward (it, -dy);
4383 else if (dy > 0)
4384 {
4385 move_it_to (it, ZV, -1, it->current_y + dy, -1,
4386 MOVE_TO_POS | MOVE_TO_Y);
4387
4388 /* If buffer ends in ZV without a newline, move to the start of
4389 the line to satisfy the post-condition. */
4390 if (IT_CHARPOS (*it) == ZV
4391 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
4392 move_it_by_lines (it, 0, 0);
4393 }
4394 }
4395
4396
4397 /* Return non-zero if some text between buffer positions START_CHARPOS
4398 and END_CHARPOS is invisible. IT->window is the window for text
4399 property lookup. */
4400
4401 static int
4402 invisible_text_between_p (it, start_charpos, end_charpos)
4403 struct it *it;
4404 int start_charpos, end_charpos;
4405 {
4406 #ifdef USE_TEXT_PROPERTIES
4407 Lisp_Object prop, limit;
4408 int invisible_found_p;
4409
4410 xassert (it != NULL && start_charpos <= end_charpos);
4411
4412 /* Is text at START invisible? */
4413 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
4414 it->window);
4415 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4416 invisible_found_p = 1;
4417 else
4418 {
4419 limit = Fnext_single_property_change (make_number (start_charpos),
4420 Qinvisible,
4421 Fcurrent_buffer (),
4422 make_number (end_charpos));
4423 invisible_found_p = XFASTINT (limit) < end_charpos;
4424 }
4425
4426 return invisible_found_p;
4427
4428 #else /* not USE_TEXT_PROPERTIES */
4429 return 0;
4430 #endif /* not USE_TEXT_PROPERTIES */
4431 }
4432
4433
4434 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
4435 negative means move up. DVPOS == 0 means move to the start of the
4436 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
4437 NEED_Y_P is zero, IT->current_y will be left unchanged.
4438
4439 Further optimization ideas: If we would know that IT->f doesn't use
4440 a face with proportional font, we could be faster for
4441 truncate-lines nil. */
4442
4443 void
4444 move_it_by_lines (it, dvpos, need_y_p)
4445 struct it *it;
4446 int dvpos, need_y_p;
4447 {
4448 struct position pos;
4449
4450 if (!FRAME_WINDOW_P (it->f))
4451 {
4452 struct text_pos textpos;
4453
4454 /* We can use vmotion on frames without proportional fonts. */
4455 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
4456 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
4457 reseat (it, textpos, 1);
4458 it->vpos += pos.vpos;
4459 it->current_y += pos.vpos;
4460 }
4461 else if (dvpos == 0)
4462 {
4463 /* DVPOS == 0 means move to the start of the screen line. */
4464 move_it_vertically_backward (it, 0);
4465 xassert (it->current_x == 0 && it->hpos == 0);
4466 }
4467 else if (dvpos > 0)
4468 {
4469 /* If there are no continuation lines, and if there is no
4470 selective display, try the simple method of moving forward
4471 DVPOS newlines, then see where we are. */
4472 if (!need_y_p && it->truncate_lines_p && it->selective == 0)
4473 {
4474 int shortage = 0, charpos;
4475
4476 if (FETCH_BYTE (IT_BYTEPOS (*it) == '\n'))
4477 charpos = IT_CHARPOS (*it) + 1;
4478 else
4479 charpos = scan_buffer ('\n', IT_CHARPOS (*it), 0, dvpos,
4480 &shortage, 0);
4481
4482 if (!invisible_text_between_p (it, IT_CHARPOS (*it), charpos))
4483 {
4484 struct text_pos pos;
4485 CHARPOS (pos) = charpos;
4486 BYTEPOS (pos) = CHAR_TO_BYTE (charpos);
4487 reseat (it, pos, 1);
4488 it->vpos += dvpos - shortage;
4489 it->hpos = it->current_x = 0;
4490 return;
4491 }
4492 }
4493
4494 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
4495 }
4496 else
4497 {
4498 struct it it2;
4499 int start_charpos, i;
4500
4501 /* If there are no continuation lines, and if there is no
4502 selective display, try the simple method of moving backward
4503 -DVPOS newlines. */
4504 if (!need_y_p && it->truncate_lines_p && it->selective == 0)
4505 {
4506 int shortage;
4507 int charpos = IT_CHARPOS (*it);
4508 int bytepos = IT_BYTEPOS (*it);
4509
4510 /* If in the middle of a line, go to its start. */
4511 if (charpos > BEGV && FETCH_BYTE (bytepos - 1) != '\n')
4512 {
4513 charpos = find_next_newline_no_quit (charpos, -1);
4514 bytepos = CHAR_TO_BYTE (charpos);
4515 }
4516
4517 if (charpos == BEGV)
4518 {
4519 struct text_pos pos;
4520 CHARPOS (pos) = charpos;
4521 BYTEPOS (pos) = bytepos;
4522 reseat (it, pos, 1);
4523 it->hpos = it->current_x = 0;
4524 return;
4525 }
4526 else
4527 {
4528 charpos = scan_buffer ('\n', charpos - 1, 0, dvpos, &shortage, 0);
4529 if (!invisible_text_between_p (it, charpos, IT_CHARPOS (*it)))
4530 {
4531 struct text_pos pos;
4532 CHARPOS (pos) = charpos;
4533 BYTEPOS (pos) = CHAR_TO_BYTE (charpos);
4534 reseat (it, pos, 1);
4535 it->vpos += dvpos + (shortage ? shortage - 1 : 0);
4536 it->hpos = it->current_x = 0;
4537 return;
4538 }
4539 }
4540 }
4541
4542 /* Go back -DVPOS visible lines and reseat the iterator there. */
4543 start_charpos = IT_CHARPOS (*it);
4544 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
4545 back_to_previous_visible_line_start (it);
4546 reseat (it, it->current.pos, 1);
4547 it->current_x = it->hpos = 0;
4548
4549 /* Above call may have moved too far if continuation lines
4550 are involved. Scan forward and see if it did. */
4551 it2 = *it;
4552 it2.vpos = it2.current_y = 0;
4553 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
4554 it->vpos -= it2.vpos;
4555 it->current_y -= it2.current_y;
4556 it->current_x = it->hpos = 0;
4557
4558 /* If we moved too far, move IT some lines forward. */
4559 if (it2.vpos > -dvpos)
4560 {
4561 int delta = it2.vpos + dvpos;
4562 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
4563 }
4564 }
4565 }
4566
4567
4568 \f
4569 /***********************************************************************
4570 Messages
4571 ***********************************************************************/
4572
4573
4574 /* Output a newline in the *Messages* buffer if "needs" one. */
4575
4576 void
4577 message_log_maybe_newline ()
4578 {
4579 if (message_log_need_newline)
4580 message_dolog ("", 0, 1, 0);
4581 }
4582
4583
4584 /* Add a string M of length LEN to the message log, optionally
4585 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
4586 nonzero, means interpret the contents of M as multibyte. This
4587 function calls low-level routines in order to bypass text property
4588 hooks, etc. which might not be safe to run. */
4589
4590 void
4591 message_dolog (m, len, nlflag, multibyte)
4592 char *m;
4593 int len, nlflag, multibyte;
4594 {
4595 if (!NILP (Vmessage_log_max))
4596 {
4597 struct buffer *oldbuf;
4598 Lisp_Object oldpoint, oldbegv, oldzv;
4599 int old_windows_or_buffers_changed = windows_or_buffers_changed;
4600 int point_at_end = 0;
4601 int zv_at_end = 0;
4602 Lisp_Object old_deactivate_mark, tem;
4603 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4604
4605 old_deactivate_mark = Vdeactivate_mark;
4606 oldbuf = current_buffer;
4607 Fset_buffer (Fget_buffer_create (build_string ("*Messages*")));
4608 current_buffer->undo_list = Qt;
4609
4610 oldpoint = Fpoint_marker ();
4611 oldbegv = Fpoint_min_marker ();
4612 oldzv = Fpoint_max_marker ();
4613 GCPRO4 (oldpoint, oldbegv, oldzv, old_deactivate_mark);
4614
4615 if (PT == Z)
4616 point_at_end = 1;
4617 if (ZV == Z)
4618 zv_at_end = 1;
4619
4620 BEGV = BEG;
4621 BEGV_BYTE = BEG_BYTE;
4622 ZV = Z;
4623 ZV_BYTE = Z_BYTE;
4624 TEMP_SET_PT_BOTH (Z, Z_BYTE);
4625
4626 /* Insert the string--maybe converting multibyte to single byte
4627 or vice versa, so that all the text fits the buffer. */
4628 if (multibyte
4629 && NILP (current_buffer->enable_multibyte_characters))
4630 {
4631 int i, c, nbytes;
4632 unsigned char work[1];
4633
4634 /* Convert a multibyte string to single-byte
4635 for the *Message* buffer. */
4636 for (i = 0; i < len; i += nbytes)
4637 {
4638 c = string_char_and_length (m + i, len - i, &nbytes);
4639 work[0] = (SINGLE_BYTE_CHAR_P (c)
4640 ? c
4641 : multibyte_char_to_unibyte (c, Qnil));
4642 insert_1_both (work, 1, 1, 1, 0, 0);
4643 }
4644 }
4645 else if (! multibyte
4646 && ! NILP (current_buffer->enable_multibyte_characters))
4647 {
4648 int i, c, nbytes;
4649 unsigned char *msg = (unsigned char *) m;
4650 unsigned char *str, work[4];
4651 /* Convert a single-byte string to multibyte
4652 for the *Message* buffer. */
4653 for (i = 0; i < len; i++)
4654 {
4655 c = unibyte_char_to_multibyte (msg[i]);
4656 nbytes = CHAR_STRING (c, work, str);
4657 insert_1_both (work, 1, nbytes, 1, 0, 0);
4658 }
4659 }
4660 else if (len)
4661 insert_1 (m, len, 1, 0, 0);
4662
4663 if (nlflag)
4664 {
4665 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
4666 insert_1 ("\n", 1, 1, 0, 0);
4667
4668 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
4669 this_bol = PT;
4670 this_bol_byte = PT_BYTE;
4671
4672 if (this_bol > BEG)
4673 {
4674 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
4675 prev_bol = PT;
4676 prev_bol_byte = PT_BYTE;
4677
4678 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
4679 this_bol, this_bol_byte);
4680 if (dup)
4681 {
4682 del_range_both (prev_bol, prev_bol_byte,
4683 this_bol, this_bol_byte, 0);
4684 if (dup > 1)
4685 {
4686 char dupstr[40];
4687 int duplen;
4688
4689 /* If you change this format, don't forget to also
4690 change message_log_check_duplicate. */
4691 sprintf (dupstr, " [%d times]", dup);
4692 duplen = strlen (dupstr);
4693 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
4694 insert_1 (dupstr, duplen, 1, 0, 1);
4695 }
4696 }
4697 }
4698
4699 if (NATNUMP (Vmessage_log_max))
4700 {
4701 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
4702 -XFASTINT (Vmessage_log_max) - 1, 0);
4703 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
4704 }
4705 }
4706 BEGV = XMARKER (oldbegv)->charpos;
4707 BEGV_BYTE = marker_byte_position (oldbegv);
4708
4709 if (zv_at_end)
4710 {
4711 ZV = Z;
4712 ZV_BYTE = Z_BYTE;
4713 }
4714 else
4715 {
4716 ZV = XMARKER (oldzv)->charpos;
4717 ZV_BYTE = marker_byte_position (oldzv);
4718 }
4719
4720 if (point_at_end)
4721 TEMP_SET_PT_BOTH (Z, Z_BYTE);
4722 else
4723 /* We can't do Fgoto_char (oldpoint) because it will run some
4724 Lisp code. */
4725 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
4726 XMARKER (oldpoint)->bytepos);
4727
4728 UNGCPRO;
4729 free_marker (oldpoint);
4730 free_marker (oldbegv);
4731 free_marker (oldzv);
4732
4733 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
4734 set_buffer_internal (oldbuf);
4735 if (NILP (tem))
4736 windows_or_buffers_changed = old_windows_or_buffers_changed;
4737 message_log_need_newline = !nlflag;
4738 Vdeactivate_mark = old_deactivate_mark;
4739 }
4740 }
4741
4742
4743 /* We are at the end of the buffer after just having inserted a newline.
4744 (Note: We depend on the fact we won't be crossing the gap.)
4745 Check to see if the most recent message looks a lot like the previous one.
4746 Return 0 if different, 1 if the new one should just replace it, or a
4747 value N > 1 if we should also append " [N times]". */
4748
4749 static int
4750 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
4751 int prev_bol, this_bol;
4752 int prev_bol_byte, this_bol_byte;
4753 {
4754 int i;
4755 int len = Z_BYTE - 1 - this_bol_byte;
4756 int seen_dots = 0;
4757 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
4758 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
4759
4760 for (i = 0; i < len; i++)
4761 {
4762 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.'
4763 && p1[i] != '\n')
4764 seen_dots = 1;
4765 if (p1[i] != p2[i])
4766 return seen_dots;
4767 }
4768 p1 += len;
4769 if (*p1 == '\n')
4770 return 2;
4771 if (*p1++ == ' ' && *p1++ == '[')
4772 {
4773 int n = 0;
4774 while (*p1 >= '0' && *p1 <= '9')
4775 n = n * 10 + *p1++ - '0';
4776 if (strncmp (p1, " times]\n", 8) == 0)
4777 return n+1;
4778 }
4779 return 0;
4780 }
4781
4782
4783 /* Display an echo area message M with a specified length of LEN
4784 chars. The string may include null characters. If M is 0, clear
4785 out any existing message, and let the mini-buffer text show through.
4786
4787 The buffer M must continue to exist until after the echo area gets
4788 cleared or some other message gets displayed there. This means do
4789 not pass text that is stored in a Lisp string; do not pass text in
4790 a buffer that was alloca'd. */
4791
4792 void
4793 message2 (m, len, multibyte)
4794 char *m;
4795 int len;
4796 int multibyte;
4797 {
4798 /* First flush out any partial line written with print. */
4799 message_log_maybe_newline ();
4800 if (m)
4801 message_dolog (m, len, 1, multibyte);
4802 message2_nolog (m, len, multibyte);
4803 }
4804
4805
4806 /* The non-logging counterpart of message2. */
4807
4808 void
4809 message2_nolog (m, len, multibyte)
4810 char *m;
4811 int len;
4812 {
4813 message_enable_multibyte = multibyte;
4814
4815 if (noninteractive)
4816 {
4817 if (noninteractive_need_newline)
4818 putc ('\n', stderr);
4819 noninteractive_need_newline = 0;
4820 if (m)
4821 fwrite (m, len, 1, stderr);
4822 if (cursor_in_echo_area == 0)
4823 fprintf (stderr, "\n");
4824 fflush (stderr);
4825 }
4826 /* A null message buffer means that the frame hasn't really been
4827 initialized yet. Error messages get reported properly by
4828 cmd_error, so this must be just an informative message; toss it. */
4829 else if (INTERACTIVE
4830 && selected_frame->glyphs_initialized_p
4831 && FRAME_MESSAGE_BUF (selected_frame))
4832 {
4833 Lisp_Object mini_window;
4834 struct frame *f;
4835
4836 /* Get the frame containing the mini-buffer
4837 that the selected frame is using. */
4838 mini_window = FRAME_MINIBUF_WINDOW (selected_frame);
4839 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
4840
4841 FRAME_SAMPLE_VISIBILITY (f);
4842 if (FRAME_VISIBLE_P (selected_frame)
4843 && ! FRAME_VISIBLE_P (f))
4844 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
4845
4846 if (m)
4847 {
4848 set_message (m, Qnil, len, multibyte);
4849 if (minibuffer_auto_raise)
4850 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
4851 }
4852 else
4853 clear_message (1, 1);
4854
4855 do_pending_window_change (0);
4856 echo_area_display (1);
4857 do_pending_window_change (0);
4858 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
4859 (*frame_up_to_date_hook) (f);
4860 }
4861 }
4862
4863
4864 /* Display an echo area message M with a specified length of NBYTES
4865 bytes. The string may include null characters. If M is not a
4866 string, clear out any existing message, and let the mini-buffer
4867 text show through. */
4868
4869 void
4870 message3 (m, nbytes, multibyte)
4871 Lisp_Object m;
4872 int nbytes;
4873 int multibyte;
4874 {
4875 struct gcpro gcpro1;
4876
4877 GCPRO1 (m);
4878
4879 /* First flush out any partial line written with print. */
4880 message_log_maybe_newline ();
4881 if (STRINGP (m))
4882 message_dolog (XSTRING (m)->data, nbytes, 1, multibyte);
4883 message3_nolog (m, nbytes, multibyte);
4884
4885 UNGCPRO;
4886 }
4887
4888
4889 /* The non-logging version of message3. */
4890
4891 void
4892 message3_nolog (m, nbytes, multibyte)
4893 Lisp_Object m;
4894 int nbytes, multibyte;
4895 {
4896 message_enable_multibyte = multibyte;
4897
4898 if (noninteractive)
4899 {
4900 if (noninteractive_need_newline)
4901 putc ('\n', stderr);
4902 noninteractive_need_newline = 0;
4903 if (STRINGP (m))
4904 fwrite (XSTRING (m)->data, nbytes, 1, stderr);
4905 if (cursor_in_echo_area == 0)
4906 fprintf (stderr, "\n");
4907 fflush (stderr);
4908 }
4909 /* A null message buffer means that the frame hasn't really been
4910 initialized yet. Error messages get reported properly by
4911 cmd_error, so this must be just an informative message; toss it. */
4912 else if (INTERACTIVE
4913 && selected_frame->glyphs_initialized_p
4914 && FRAME_MESSAGE_BUF (selected_frame))
4915 {
4916 Lisp_Object mini_window;
4917 Lisp_Object frame;
4918 struct frame *f;
4919
4920 /* Get the frame containing the mini-buffer
4921 that the selected frame is using. */
4922 mini_window = FRAME_MINIBUF_WINDOW (selected_frame);
4923 frame = XWINDOW (mini_window)->frame;
4924 f = XFRAME (frame);
4925
4926 FRAME_SAMPLE_VISIBILITY (f);
4927 if (FRAME_VISIBLE_P (selected_frame)
4928 && !FRAME_VISIBLE_P (f))
4929 Fmake_frame_visible (frame);
4930
4931 if (STRINGP (m) && XSTRING (m)->size)
4932 {
4933 set_message (NULL, m, nbytes, multibyte);
4934 if (minibuffer_auto_raise)
4935 Fraise_frame (frame);
4936 }
4937 else
4938 clear_message (1, 1);
4939
4940 do_pending_window_change (0);
4941 echo_area_display (1);
4942 do_pending_window_change (0);
4943 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
4944 (*frame_up_to_date_hook) (f);
4945 }
4946 }
4947
4948
4949 /* Display a null-terminated echo area message M. If M is 0, clear
4950 out any existing message, and let the mini-buffer text show through.
4951
4952 The buffer M must continue to exist until after the echo area gets
4953 cleared or some other message gets displayed there. Do not pass
4954 text that is stored in a Lisp string. Do not pass text in a buffer
4955 that was alloca'd. */
4956
4957 void
4958 message1 (m)
4959 char *m;
4960 {
4961 message2 (m, (m ? strlen (m) : 0), 0);
4962 }
4963
4964
4965 /* The non-logging counterpart of message1. */
4966
4967 void
4968 message1_nolog (m)
4969 char *m;
4970 {
4971 message2_nolog (m, (m ? strlen (m) : 0), 0);
4972 }
4973
4974 /* Display a message M which contains a single %s
4975 which gets replaced with STRING. */
4976
4977 void
4978 message_with_string (m, string, log)
4979 char *m;
4980 Lisp_Object string;
4981 int log;
4982 {
4983 if (noninteractive)
4984 {
4985 if (m)
4986 {
4987 if (noninteractive_need_newline)
4988 putc ('\n', stderr);
4989 noninteractive_need_newline = 0;
4990 fprintf (stderr, m, XSTRING (string)->data);
4991 if (cursor_in_echo_area == 0)
4992 fprintf (stderr, "\n");
4993 fflush (stderr);
4994 }
4995 }
4996 else if (INTERACTIVE)
4997 {
4998 /* The frame whose minibuffer we're going to display the message on.
4999 It may be larger than the selected frame, so we need
5000 to use its buffer, not the selected frame's buffer. */
5001 Lisp_Object mini_window;
5002 FRAME_PTR f;
5003
5004 /* Get the frame containing the minibuffer
5005 that the selected frame is using. */
5006 mini_window = FRAME_MINIBUF_WINDOW (selected_frame);
5007 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5008
5009 /* A null message buffer means that the frame hasn't really been
5010 initialized yet. Error messages get reported properly by
5011 cmd_error, so this must be just an informative message; toss it. */
5012 if (FRAME_MESSAGE_BUF (f))
5013 {
5014 int len;
5015 char *a[1];
5016 a[0] = (char *) XSTRING (string)->data;
5017
5018 len = doprnt (FRAME_MESSAGE_BUF (f),
5019 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5020
5021 if (log)
5022 message2 (FRAME_MESSAGE_BUF (f), len,
5023 STRING_MULTIBYTE (string));
5024 else
5025 message2_nolog (FRAME_MESSAGE_BUF (f), len,
5026 STRING_MULTIBYTE (string));
5027
5028 /* Print should start at the beginning of the message
5029 buffer next time. */
5030 message_buf_print = 0;
5031 }
5032 }
5033 }
5034
5035
5036 /* Dump an informative message to the minibuf. If M is 0, clear out
5037 any existing message, and let the mini-buffer text show through. */
5038
5039 /* VARARGS 1 */
5040 void
5041 message (m, a1, a2, a3)
5042 char *m;
5043 EMACS_INT a1, a2, a3;
5044 {
5045 if (noninteractive)
5046 {
5047 if (m)
5048 {
5049 if (noninteractive_need_newline)
5050 putc ('\n', stderr);
5051 noninteractive_need_newline = 0;
5052 fprintf (stderr, m, a1, a2, a3);
5053 if (cursor_in_echo_area == 0)
5054 fprintf (stderr, "\n");
5055 fflush (stderr);
5056 }
5057 }
5058 else if (INTERACTIVE)
5059 {
5060 /* The frame whose mini-buffer we're going to display the message
5061 on. It may be larger than the selected frame, so we need to
5062 use its buffer, not the selected frame's buffer. */
5063 Lisp_Object mini_window;
5064 struct frame *f;
5065
5066 /* Get the frame containing the mini-buffer
5067 that the selected frame is using. */
5068 mini_window = FRAME_MINIBUF_WINDOW (selected_frame);
5069 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5070
5071 /* A null message buffer means that the frame hasn't really been
5072 initialized yet. Error messages get reported properly by
5073 cmd_error, so this must be just an informative message; toss
5074 it. */
5075 if (FRAME_MESSAGE_BUF (f))
5076 {
5077 if (m)
5078 {
5079 int len;
5080 #ifdef NO_ARG_ARRAY
5081 char *a[3];
5082 a[0] = (char *) a1;
5083 a[1] = (char *) a2;
5084 a[2] = (char *) a3;
5085
5086 len = doprnt (FRAME_MESSAGE_BUF (f),
5087 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5088 #else
5089 len = doprnt (FRAME_MESSAGE_BUF (f),
5090 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
5091 (char **) &a1);
5092 #endif /* NO_ARG_ARRAY */
5093
5094 message2 (FRAME_MESSAGE_BUF (f), len, 0);
5095 }
5096 else
5097 message1 (0);
5098
5099 /* Print should start at the beginning of the message
5100 buffer next time. */
5101 message_buf_print = 0;
5102 }
5103 }
5104 }
5105
5106
5107 /* The non-logging version of message. */
5108
5109 void
5110 message_nolog (m, a1, a2, a3)
5111 char *m;
5112 EMACS_INT a1, a2, a3;
5113 {
5114 Lisp_Object old_log_max;
5115 old_log_max = Vmessage_log_max;
5116 Vmessage_log_max = Qnil;
5117 message (m, a1, a2, a3);
5118 Vmessage_log_max = old_log_max;
5119 }
5120
5121
5122 /* Display the current message in the current mini-buffer. This is
5123 only called from error handlers in process.c, and is not time
5124 critical. */
5125
5126 void
5127 update_echo_area ()
5128 {
5129 if (!NILP (echo_area_buffer[0]))
5130 {
5131 Lisp_Object string;
5132 string = Fcurrent_message ();
5133 message3 (string, XSTRING (string)->size,
5134 !NILP (current_buffer->enable_multibyte_characters));
5135 }
5136 }
5137
5138
5139 /* Call FN with args A1..A5 with either the current or last displayed
5140 echo_area_buffer as current buffer.
5141
5142 WHICH zero means use the current message buffer
5143 echo_area_buffer[0]. If that is nil, choose a suitable buffer
5144 from echo_buffer[] and clear it.
5145
5146 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
5147 suitable buffer from echo_buffer[] and clear it.
5148
5149 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
5150 that the current message becomes the last displayed one, make
5151 choose a suitable buffer for echo_area_buffer[0], and clear it.
5152
5153 Value is what FN returns. */
5154
5155 static int
5156 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4, a5)
5157 struct window *w;
5158 int which;
5159 int (*fn) ();
5160 int a1, a2, a3, a4, a5;
5161 {
5162 Lisp_Object buffer;
5163 int i, this_one, the_other, clear_buffer_p, rc;
5164 int count = specpdl_ptr - specpdl;
5165
5166 /* If buffers aren't life, make new ones. */
5167 for (i = 0; i < 2; ++i)
5168 if (!BUFFERP (echo_buffer[i])
5169 || NILP (XBUFFER (echo_buffer[i])->name))
5170 {
5171 char name[30];
5172 sprintf (name, " *Echo Area %d*", i);
5173 echo_buffer[i] = Fget_buffer_create (build_string (name));
5174 }
5175
5176 clear_buffer_p = 0;
5177
5178 if (which == 0)
5179 this_one = 0, the_other = 1;
5180 else if (which > 0)
5181 this_one = 1, the_other = 0;
5182 else
5183 {
5184 this_one = 0, the_other = 1;
5185 clear_buffer_p = 1;
5186
5187 /* We need a fresh one in case the current echo buffer equals
5188 the one containing the last displayed echo area message. */
5189 if (!NILP (echo_area_buffer[this_one])
5190 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
5191 echo_area_buffer[this_one] = Qnil;
5192
5193 }
5194
5195 /* Choose a suitable buffer from echo_buffer[] is we don't
5196 have one. */
5197 if (NILP (echo_area_buffer[this_one]))
5198 {
5199 echo_area_buffer[this_one]
5200 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
5201 ? echo_buffer[the_other]
5202 : echo_buffer[this_one]);
5203 clear_buffer_p = 1;
5204 }
5205
5206 buffer = echo_area_buffer[this_one];
5207
5208 record_unwind_protect (unwind_with_echo_area_buffer,
5209 with_echo_area_buffer_unwind_data (w));
5210
5211 /* Make the echo area buffer current. Note that for display
5212 purposes, it is not necessary that the displayed window's buffer
5213 == current_buffer, except for text property lookup. So, let's
5214 only set that buffer temporarily here without doing a full
5215 Fset_window_buffer. We must also change w->pointm, though,
5216 because otherwise an assertions in unshow_buffer fails, and Emacs
5217 aborts. */
5218 set_buffer_internal_1 (XBUFFER (buffer));
5219 if (w)
5220 {
5221 w->buffer = buffer;
5222 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
5223 }
5224 current_buffer->truncate_lines = Qnil;
5225 current_buffer->undo_list = Qt;
5226 current_buffer->read_only = Qnil;
5227
5228 if (clear_buffer_p && Z > BEG)
5229 del_range (BEG, Z);
5230
5231 xassert (BEGV >= BEG);
5232 xassert (ZV <= Z && ZV >= BEGV);
5233
5234 rc = fn (a1, a2, a3, a4, a5);
5235
5236 xassert (BEGV >= BEG);
5237 xassert (ZV <= Z && ZV >= BEGV);
5238
5239 unbind_to (count, Qnil);
5240 return rc;
5241 }
5242
5243
5244 /* Save state that should be preserved around the call to the function
5245 FN called in with_echo_area_buffer. */
5246
5247 static Lisp_Object
5248 with_echo_area_buffer_unwind_data (w)
5249 struct window *w;
5250 {
5251 int i = 0;
5252 Lisp_Object vector;
5253
5254 /* Reduce consing by keeping one vector in
5255 Vwith_echo_area_save_vector. */
5256 vector = Vwith_echo_area_save_vector;
5257 Vwith_echo_area_save_vector = Qnil;
5258
5259 if (NILP (vector))
5260 vector = Fmake_vector (make_number (7), Qnil);
5261
5262 XSETBUFFER (XVECTOR (vector)->contents[i], current_buffer); ++i;
5263 XVECTOR (vector)->contents[i++] = Vdeactivate_mark;
5264 XVECTOR (vector)->contents[i++] = make_number (windows_or_buffers_changed);
5265
5266 if (w)
5267 {
5268 XSETWINDOW (XVECTOR (vector)->contents[i], w); ++i;
5269 XVECTOR (vector)->contents[i++] = w->buffer;
5270 XVECTOR (vector)->contents[i++]
5271 = make_number (XMARKER (w->pointm)->charpos);
5272 XVECTOR (vector)->contents[i++]
5273 = make_number (XMARKER (w->pointm)->bytepos);
5274 }
5275 else
5276 {
5277 int end = i + 4;
5278 while (i < end)
5279 XVECTOR (vector)->contents[i++] = Qnil;
5280 }
5281
5282 xassert (i == XVECTOR (vector)->size);
5283 return vector;
5284 }
5285
5286
5287 /* Restore global state from VECTOR which was created by
5288 with_echo_area_buffer_unwind_data. */
5289
5290 static Lisp_Object
5291 unwind_with_echo_area_buffer (vector)
5292 Lisp_Object vector;
5293 {
5294 int i = 0;
5295
5296 set_buffer_internal_1 (XBUFFER (XVECTOR (vector)->contents[i])); ++i;
5297 Vdeactivate_mark = XVECTOR (vector)->contents[i]; ++i;
5298 windows_or_buffers_changed = XFASTINT (XVECTOR (vector)->contents[i]); ++i;
5299
5300 if (WINDOWP (XVECTOR (vector)->contents[i]))
5301 {
5302 struct window *w;
5303 Lisp_Object buffer, charpos, bytepos;
5304
5305 w = XWINDOW (XVECTOR (vector)->contents[i]); ++i;
5306 buffer = XVECTOR (vector)->contents[i]; ++i;
5307 charpos = XVECTOR (vector)->contents[i]; ++i;
5308 bytepos = XVECTOR (vector)->contents[i]; ++i;
5309
5310 w->buffer = buffer;
5311 set_marker_both (w->pointm, buffer,
5312 XFASTINT (charpos), XFASTINT (bytepos));
5313 }
5314
5315 Vwith_echo_area_save_vector = vector;
5316 return Qnil;
5317 }
5318
5319
5320 /* Set up the echo area for use by print functions. MULTIBYTE_P
5321 non-zero means we will print multibyte. */
5322
5323 void
5324 setup_echo_area_for_printing (multibyte_p)
5325 int multibyte_p;
5326 {
5327 if (!message_buf_print)
5328 {
5329 /* A message has been output since the last time we printed.
5330 Choose a fresh echo area buffer. */
5331 if (EQ (echo_area_buffer[1], echo_buffer[0]))
5332 echo_area_buffer[0] = echo_buffer[1];
5333 else
5334 echo_area_buffer[0] = echo_buffer[0];
5335
5336 /* Switch to that buffer and clear it. */
5337 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
5338 if (Z > BEG)
5339 del_range (BEG, Z);
5340 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
5341
5342 /* Set up the buffer for the multibyteness we need. */
5343 if (multibyte_p
5344 != !NILP (current_buffer->enable_multibyte_characters))
5345 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
5346
5347 /* Raise the frame containing the echo area. */
5348 if (minibuffer_auto_raise)
5349 {
5350 Lisp_Object mini_window;
5351 mini_window = FRAME_MINIBUF_WINDOW (selected_frame);
5352 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5353 }
5354
5355 message_buf_print = 1;
5356 }
5357 else if (current_buffer != XBUFFER (echo_area_buffer[0]))
5358 /* Someone switched buffers between print requests. */
5359 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
5360 }
5361
5362
5363 /* Display an echo area message in window W. Value is non-zero if W's
5364 height is changed. If display_last_displayed_message_p is
5365 non-zero, display the message that was last displayed, otherwise
5366 display the current message. */
5367
5368 static int
5369 display_echo_area (w)
5370 struct window *w;
5371 {
5372 int i, no_message_p, window_height_changed_p;
5373
5374 /* If there is no message, we must call display_echo_area_1
5375 nevertheless because it resizes the window. But we will have to
5376 reset the echo_area_buffer in question to nil at the end because
5377 with_echo_area_buffer will sets it to an empty buffer. */
5378 i = display_last_displayed_message_p ? 1 : 0;
5379 no_message_p = NILP (echo_area_buffer[i]);
5380
5381 window_height_changed_p
5382 = with_echo_area_buffer (w, display_last_displayed_message_p,
5383 (int (*) ()) display_echo_area_1, w);
5384
5385 if (no_message_p)
5386 echo_area_buffer[i] = Qnil;
5387
5388 return window_height_changed_p;
5389 }
5390
5391
5392 /* Helper for display_echo_area. Display the current buffer which
5393 contains the current echo area message in window W, a mini-window.
5394 Change the height of W so that all of the message is displayed.
5395 Value is non-zero if height of W was changed. */
5396
5397 static int
5398 display_echo_area_1 (w)
5399 struct window *w;
5400 {
5401 Lisp_Object window;
5402 struct frame *f = XFRAME (w->frame);
5403 struct text_pos start;
5404 int window_height_changed_p = 0;
5405
5406 /* Do this before displaying, so that we have a large enough glyph
5407 matrix for the display. */
5408 window_height_changed_p = resize_mini_window (w);
5409
5410 /* Display. */
5411 clear_glyph_matrix (w->desired_matrix);
5412 XSETWINDOW (window, w);
5413 SET_TEXT_POS (start, BEG, BEG_BYTE);
5414 try_window (window, start);
5415
5416 return window_height_changed_p;
5417 }
5418
5419
5420 /* Resize mini-window W to fit the size of its contents. Value is
5421 non-zero if the window height has been changed. */
5422
5423 static int
5424 resize_mini_window (w)
5425 struct window *w;
5426 {
5427 struct frame *f = XFRAME (w->frame);
5428 int window_height_changed_p = 0;
5429
5430 xassert (MINI_WINDOW_P (w));
5431
5432 /* Nil means don't try to resize. */
5433 if (NILP (Vmax_mini_window_height))
5434 return 0;
5435
5436 if (!FRAME_MINIBUF_ONLY_P (f))
5437 {
5438 struct it it;
5439 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
5440 int total_height = XFASTINT (root->height) + XFASTINT (w->height);
5441 int height, max_height;
5442 int unit = CANON_Y_UNIT (f);
5443 struct text_pos start;
5444
5445 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
5446
5447 /* Compute the max. number of lines specified by the user. */
5448 if (FLOATP (Vmax_mini_window_height))
5449 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
5450 else if (INTEGERP (Vmax_mini_window_height))
5451 max_height = XINT (Vmax_mini_window_height);
5452 else
5453 max_height = total_height / 4;
5454
5455 /* Correct that max. height if it's bogus. */
5456 max_height = max (1, max_height);
5457 max_height = min (total_height, max_height);
5458
5459 /* Find out the height of the text in the window. */
5460 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
5461 height = (unit - 1 + it.current_y + last_height) / unit;
5462 height = max (1, height);
5463
5464 /* Compute a suitable window start. */
5465 if (height > max_height)
5466 {
5467 height = max_height;
5468 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
5469 move_it_vertically_backward (&it, (height - 1) * unit);
5470 start = it.current.pos;
5471 }
5472 else
5473 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
5474 SET_MARKER_FROM_TEXT_POS (w->start, start);
5475
5476 if (NILP (Vresize_mini_config))
5477 {
5478 if (height != XFASTINT (w->height))
5479 {
5480 Lisp_Object old_selected_window;
5481 Lisp_Object config;
5482 struct gcpro gcpro1;
5483
5484 resize_mini_initial_height = XFASTINT (w->height);
5485 config = Fcurrent_window_configuration (Qnil);
5486 GCPRO1 (config);
5487
5488 old_selected_window = selected_window;
5489 XSETWINDOW (selected_window, w);
5490 change_window_height (height - XFASTINT (w->height), 0);
5491 selected_window = old_selected_window;
5492
5493 window_height_changed_p = 1;
5494
5495 /* Set this after changing window sizes, or else
5496 Vresize_mini_config would be reset in
5497 adjust_frame_glyphs. */
5498 resize_mini_frame = XFRAME (w->frame);
5499 Vresize_mini_config = config;
5500 UNGCPRO;
5501 }
5502 }
5503 else if (height != XFASTINT (w->height))
5504 {
5505 if (height == resize_mini_initial_height)
5506 {
5507 Fset_window_configuration (Vresize_mini_config);
5508 Vresize_mini_config = Qnil;
5509 resize_mini_frame = NULL;
5510 }
5511 else
5512 {
5513 Lisp_Object old_selected_window;
5514 old_selected_window = selected_window;
5515 XSETWINDOW (selected_window, w);
5516 change_window_height (height - XFASTINT (w->height), 0);
5517 selected_window = old_selected_window;
5518 }
5519
5520 window_height_changed_p = 1;
5521 }
5522 }
5523
5524 return window_height_changed_p;
5525 }
5526
5527
5528 /* Value is the current message, a string, or nil if there is no
5529 current message. */
5530
5531 Lisp_Object
5532 current_message ()
5533 {
5534 Lisp_Object msg;
5535
5536 if (NILP (echo_area_buffer[0]))
5537 msg = Qnil;
5538 else
5539 {
5540 with_echo_area_buffer (0, 0, (int (*) ()) current_message_1, &msg);
5541 if (NILP (msg))
5542 echo_area_buffer[0] = Qnil;
5543 }
5544
5545 return msg;
5546 }
5547
5548
5549 static int
5550 current_message_1 (msg)
5551 Lisp_Object *msg;
5552 {
5553 if (Z > BEG)
5554 *msg = make_buffer_string (BEG, Z, 1);
5555 else
5556 *msg = Qnil;
5557 return 0;
5558 }
5559
5560
5561 /* Push the current message on Vmessage_stack for later restauration
5562 by restore_message. Value is non-zero if the current message isn't
5563 empty. This is a relatively infrequent operation, so it's not
5564 worth optimizing. */
5565
5566 int
5567 push_message ()
5568 {
5569 Lisp_Object msg;
5570 msg = current_message ();
5571 Vmessage_stack = Fcons (msg, Vmessage_stack);
5572 return STRINGP (msg);
5573 }
5574
5575
5576 /* Restore message display from the top of Vmessage_stack. */
5577
5578 void
5579 restore_message ()
5580 {
5581 Lisp_Object msg;
5582
5583 xassert (CONSP (Vmessage_stack));
5584 msg = XCAR (Vmessage_stack);
5585 if (STRINGP (msg))
5586 message3_nolog (msg, STRING_BYTES (XSTRING (msg)), STRING_MULTIBYTE (msg));
5587 else
5588 message3_nolog (msg, 0, 0);
5589 }
5590
5591
5592 /* Pop the top-most entry off Vmessage_stack. */
5593
5594 void
5595 pop_message ()
5596 {
5597 xassert (CONSP (Vmessage_stack));
5598 Vmessage_stack = XCDR (Vmessage_stack);
5599 }
5600
5601
5602 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
5603 exits. If the stack is not empty, we have a missing pop_message
5604 somewhere. */
5605
5606 void
5607 check_message_stack ()
5608 {
5609 if (!NILP (Vmessage_stack))
5610 abort ();
5611 }
5612
5613
5614 /* Truncate to NCHARS what will be displayed in the echo area the next
5615 time we display it---but don't redisplay it now. */
5616
5617 void
5618 truncate_echo_area (nchars)
5619 int nchars;
5620 {
5621 if (nchars == 0)
5622 echo_area_buffer[0] = Qnil;
5623 /* A null message buffer means that the frame hasn't really been
5624 initialized yet. Error messages get reported properly by
5625 cmd_error, so this must be just an informative message; toss it. */
5626 else if (!noninteractive
5627 && INTERACTIVE
5628 && FRAME_MESSAGE_BUF (selected_frame)
5629 && !NILP (echo_area_buffer[0]))
5630 with_echo_area_buffer (0, 0, (int (*) ()) truncate_message_1, nchars);
5631 }
5632
5633
5634 /* Helper function for truncate_echo_area. Truncate the current
5635 message to at most NCHARS characters. */
5636
5637 static int
5638 truncate_message_1 (nchars)
5639 int nchars;
5640 {
5641 if (BEG + nchars < Z)
5642 del_range (BEG + nchars, Z);
5643 if (Z == BEG)
5644 echo_area_buffer[0] = Qnil;
5645 return 0;
5646 }
5647
5648
5649 /* Set the current message to a substring of S or STRING.
5650
5651 If STRING is a Lisp string, set the message to the first NBYTES
5652 bytes from STRING. NBYTES zero means use the whole string. If
5653 STRING is multibyte, the message will be displayed multibyte.
5654
5655 If S is not null, set the message to the first LEN bytes of S. LEN
5656 zero means use the whole string. MULTIBYTE_P non-zero means S is
5657 multibyte. Display the message multibyte in that case. */
5658
5659 void
5660 set_message (s, string, nbytes, multibyte_p)
5661 char *s;
5662 Lisp_Object string;
5663 int nbytes;
5664 {
5665 message_enable_multibyte
5666 = ((s && multibyte_p)
5667 || (STRINGP (string) && STRING_MULTIBYTE (string)));
5668
5669 with_echo_area_buffer (0, -1, (int (*) ()) set_message_1,
5670 s, string, nbytes, multibyte_p);
5671 message_buf_print = 0;
5672 }
5673
5674
5675 /* Helper function for set_message. Arguments have the same meaning
5676 as there. This function is called with the echo area buffer being
5677 current. */
5678
5679 static int
5680 set_message_1 (s, string, nbytes, multibyte_p)
5681 char *s;
5682 Lisp_Object string;
5683 int nbytes, multibyte_p;
5684 {
5685 xassert (BEG == Z);
5686
5687 /* Change multibyteness of the echo buffer appropriately. */
5688 if (message_enable_multibyte
5689 != !NILP (current_buffer->enable_multibyte_characters))
5690 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
5691
5692 /* Insert new message at BEG. */
5693 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
5694
5695 if (STRINGP (string))
5696 {
5697 int nchars;
5698
5699 if (nbytes == 0)
5700 nbytes = XSTRING (string)->size_byte;
5701 nchars = string_byte_to_char (string, nbytes);
5702
5703 /* This function takes care of single/multibyte conversion. We
5704 just have to ensure that the echo area buffer has the right
5705 setting of enable_multibyte_characters. */
5706 insert_from_string (string, 0, 0, nchars, nbytes, 1);
5707 }
5708 else if (s)
5709 {
5710 if (nbytes == 0)
5711 nbytes = strlen (s);
5712
5713 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
5714 {
5715 /* Convert from multi-byte to single-byte. */
5716 int i, c, n;
5717 unsigned char work[1];
5718
5719 /* Convert a multibyte string to single-byte. */
5720 for (i = 0; i < nbytes; i += n)
5721 {
5722 c = string_char_and_length (s + i, nbytes - i, &n);
5723 work[0] = (SINGLE_BYTE_CHAR_P (c)
5724 ? c
5725 : multibyte_char_to_unibyte (c, Qnil));
5726 insert_1_both (work, 1, 1, 1, 0, 0);
5727 }
5728 }
5729 else if (!multibyte_p
5730 && !NILP (current_buffer->enable_multibyte_characters))
5731 {
5732 /* Convert from single-byte to multi-byte. */
5733 int i, c, n;
5734 unsigned char *msg = (unsigned char *) s;
5735 unsigned char *str, work[4];
5736
5737 /* Convert a single-byte string to multibyte. */
5738 for (i = 0; i < nbytes; i++)
5739 {
5740 c = unibyte_char_to_multibyte (msg[i]);
5741 n = CHAR_STRING (c, work, str);
5742 insert_1_both (work, 1, n, 1, 0, 0);
5743 }
5744 }
5745 else
5746 insert_1 (s, nbytes, 1, 0, 0);
5747 }
5748
5749 return 0;
5750 }
5751
5752
5753 /* Clear messages. CURRENT_P non-zero means clear the current
5754 message. LAST_DISPLAYED_P non-zero means clear the message
5755 last displayed. */
5756
5757 void
5758 clear_message (current_p, last_displayed_p)
5759 int current_p, last_displayed_p;
5760 {
5761 if (current_p)
5762 echo_area_buffer[0] = Qnil;
5763
5764 if (last_displayed_p)
5765 echo_area_buffer[1] = Qnil;
5766
5767 message_buf_print = 0;
5768 }
5769
5770 /* Clear garbaged frames.
5771
5772 This function is used where the old redisplay called
5773 redraw_garbaged_frames which in turn called redraw_frame which in
5774 turn called clear_frame. The call to clear_frame was a source of
5775 flickering. I believe a clear_frame is not necessary. It should
5776 suffice in the new redisplay to invalidate all current matrices,
5777 and ensure a complete redisplay of all windows. */
5778
5779 static void
5780 clear_garbaged_frames ()
5781 {
5782 if (frame_garbaged)
5783 {
5784 Lisp_Object tail, frame;
5785
5786 FOR_EACH_FRAME (tail, frame)
5787 {
5788 struct frame *f = XFRAME (frame);
5789
5790 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
5791 {
5792 clear_current_matrices (f);
5793 f->garbaged = 0;
5794 }
5795 }
5796
5797 frame_garbaged = 0;
5798 ++windows_or_buffers_changed;
5799 }
5800 }
5801
5802
5803 /* Redisplay the echo area of selected_frame. If UPDATE_FRAME_P is
5804 non-zero update selected_frame. Value is non-zero if the
5805 mini-windows height has been changed. */
5806
5807 static int
5808 echo_area_display (update_frame_p)
5809 int update_frame_p;
5810 {
5811 Lisp_Object mini_window;
5812 struct window *w;
5813 struct frame *f;
5814 int window_height_changed_p = 0;
5815
5816 mini_window = FRAME_MINIBUF_WINDOW (selected_frame);
5817 w = XWINDOW (mini_window);
5818 f = XFRAME (WINDOW_FRAME (w));
5819
5820 /* Don't display if frame is invisible or not yet initialized. */
5821 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
5822 return 0;
5823
5824 /* When Emacs starts, selected_frame may be a visible terminal
5825 frame, even if we run under a window system. If we let this
5826 through, a message would be displayed on the terminal. */
5827 #ifdef HAVE_WINDOW_SYSTEM
5828 if (!inhibit_window_system && !FRAME_WINDOW_P (selected_frame))
5829 return 0;
5830 #endif /* HAVE_WINDOW_SYSTEM */
5831
5832 /* Redraw garbaged frames. */
5833 if (frame_garbaged)
5834 clear_garbaged_frames ();
5835
5836 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
5837 {
5838 echo_area_window = mini_window;
5839 window_height_changed_p = display_echo_area (w);
5840 w->must_be_updated_p = 1;
5841
5842 if (update_frame_p)
5843 {
5844 /* Not called from redisplay_internal. If we changed
5845 window configuration, we must redisplay thoroughly.
5846 Otherwise, we can do with updating what we displayed
5847 above. */
5848 if (window_height_changed_p)
5849 {
5850 ++windows_or_buffers_changed;
5851 ++update_mode_lines;
5852 redisplay_internal (0);
5853 }
5854 else if (FRAME_WINDOW_P (f))
5855 {
5856 update_single_window (w, 1);
5857 rif->flush_display (f);
5858 }
5859 else
5860 update_frame (f, 1, 1);
5861 }
5862 }
5863 else if (!EQ (mini_window, selected_window))
5864 windows_or_buffers_changed++;
5865
5866 /* Last displayed message is now the current message. */
5867 echo_area_buffer[1] = echo_area_buffer[0];
5868
5869 /* Prevent redisplay optimization in redisplay_internal by resetting
5870 this_line_start_pos. This is done because the mini-buffer now
5871 displays the message instead of its buffer text. */
5872 if (EQ (mini_window, selected_window))
5873 CHARPOS (this_line_start_pos) = 0;
5874
5875 return window_height_changed_p;
5876 }
5877
5878
5879 \f
5880 /***********************************************************************
5881 Frame Titles
5882 ***********************************************************************/
5883
5884
5885 #ifdef HAVE_WINDOW_SYSTEM
5886
5887 /* A buffer for constructing frame titles in it; allocated from the
5888 heap in init_xdisp and resized as needed in store_frame_title_char. */
5889
5890 static char *frame_title_buf;
5891
5892 /* The buffer's end, and a current output position in it. */
5893
5894 static char *frame_title_buf_end;
5895 static char *frame_title_ptr;
5896
5897
5898 /* Store a single character C for the frame title in frame_title_buf.
5899 Re-allocate frame_title_buf if necessary. */
5900
5901 static void
5902 store_frame_title_char (c)
5903 char c;
5904 {
5905 /* If output position has reached the end of the allocated buffer,
5906 double the buffer's size. */
5907 if (frame_title_ptr == frame_title_buf_end)
5908 {
5909 int len = frame_title_ptr - frame_title_buf;
5910 int new_size = 2 * len * sizeof *frame_title_buf;
5911 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
5912 frame_title_buf_end = frame_title_buf + new_size;
5913 frame_title_ptr = frame_title_buf + len;
5914 }
5915
5916 *frame_title_ptr++ = c;
5917 }
5918
5919
5920 /* Store part of a frame title in frame_title_buf, beginning at
5921 frame_title_ptr. STR is the string to store. Do not copy more
5922 than PRECISION number of bytes from STR; PRECISION <= 0 means copy
5923 the whole string. Pad with spaces until FIELD_WIDTH number of
5924 characters have been copied; FIELD_WIDTH <= 0 means don't pad.
5925 Called from display_mode_element when it is used to build a frame
5926 title. */
5927
5928 static int
5929 store_frame_title (str, field_width, precision)
5930 unsigned char *str;
5931 int field_width, precision;
5932 {
5933 int n = 0;
5934
5935 /* Copy at most PRECISION chars from STR. */
5936 while ((precision <= 0 || n < precision)
5937 && *str)
5938 {
5939 store_frame_title_char (*str++);
5940 ++n;
5941 }
5942
5943 /* Fill up with spaces until FIELD_WIDTH reached. */
5944 while (field_width > 0
5945 && n < field_width)
5946 {
5947 store_frame_title_char (' ');
5948 ++n;
5949 }
5950
5951 return n;
5952 }
5953
5954
5955 /* Set the title of FRAME, if it has changed. The title format is
5956 Vicon_title_format if FRAME is iconified, otherwise it is
5957 frame_title_format. */
5958
5959 static void
5960 x_consider_frame_title (frame)
5961 Lisp_Object frame;
5962 {
5963 struct frame *f = XFRAME (frame);
5964
5965 if (FRAME_WINDOW_P (f)
5966 || FRAME_MINIBUF_ONLY_P (f)
5967 || f->explicit_name)
5968 {
5969 /* Do we have more than one visible frame on this X display? */
5970 Lisp_Object tail;
5971 Lisp_Object fmt;
5972 struct buffer *obuf;
5973 int len;
5974 struct it it;
5975
5976 for (tail = Vframe_list; CONSP (tail); tail = XCONS (tail)->cdr)
5977 {
5978 struct frame *tf = XFRAME (XCONS (tail)->car);
5979
5980 if (tf != f
5981 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
5982 && !FRAME_MINIBUF_ONLY_P (tf)
5983 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
5984 break;
5985 }
5986
5987 /* Set global variable indicating that multiple frames exist. */
5988 multiple_frames = CONSP (tail);
5989
5990 /* Switch to the buffer of selected window of the frame. Set up
5991 frame_title_ptr so that display_mode_element will output into it;
5992 then display the title. */
5993 obuf = current_buffer;
5994 Fset_buffer (XWINDOW (f->selected_window)->buffer);
5995 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
5996 frame_title_ptr = frame_title_buf;
5997 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
5998 NULL, DEFAULT_FACE_ID);
5999 len = display_mode_element (&it, 0, -1, -1, fmt);
6000 frame_title_ptr = NULL;
6001 set_buffer_internal (obuf);
6002
6003 /* Set the title only if it's changed. This avoids consing in
6004 the common case where it hasn't. (If it turns out that we've
6005 already wasted too much time by walking through the list with
6006 display_mode_element, then we might need to optimize at a
6007 higher level than this.) */
6008 if (! STRINGP (f->name)
6009 || STRING_BYTES (XSTRING (f->name)) != len
6010 || bcmp (frame_title_buf, XSTRING (f->name)->data, len) != 0)
6011 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
6012 }
6013 }
6014
6015 #else /* not HAVE_WINDOW_SYSTEM */
6016
6017 #define frame_title_ptr ((char *)0)
6018 #define store_frame_title(str, mincol, maxcol) 0
6019
6020 #endif /* not HAVE_WINDOW_SYSTEM */
6021
6022
6023
6024 \f
6025 /***********************************************************************
6026 Menu Bars
6027 ***********************************************************************/
6028
6029
6030 /* Prepare for redisplay by updating menu-bar item lists when
6031 appropriate. This can call eval. */
6032
6033 void
6034 prepare_menu_bars ()
6035 {
6036 int all_windows;
6037 struct gcpro gcpro1, gcpro2;
6038 struct frame *f;
6039 struct frame *tooltip_frame;
6040
6041 #ifdef HAVE_X_WINDOWS
6042 tooltip_frame = tip_frame;
6043 #else
6044 tooltip_frame = NULL;
6045 #endif
6046
6047 /* Update all frame titles based on their buffer names, etc. We do
6048 this before the menu bars so that the buffer-menu will show the
6049 up-to-date frame titles. */
6050 #ifdef HAVE_WINDOW_SYSTEM
6051 if (windows_or_buffers_changed || update_mode_lines)
6052 {
6053 Lisp_Object tail, frame;
6054
6055 FOR_EACH_FRAME (tail, frame)
6056 {
6057 f = XFRAME (frame);
6058 if (f != tooltip_frame
6059 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
6060 x_consider_frame_title (frame);
6061 }
6062 }
6063 #endif /* HAVE_WINDOW_SYSTEM */
6064
6065 /* Update the menu bar item lists, if appropriate. This has to be
6066 done before any actual redisplay or generation of display lines. */
6067 all_windows = (update_mode_lines
6068 || buffer_shared > 1
6069 || windows_or_buffers_changed);
6070 if (all_windows)
6071 {
6072 Lisp_Object tail, frame;
6073 int count = specpdl_ptr - specpdl;
6074
6075 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6076
6077 FOR_EACH_FRAME (tail, frame)
6078 {
6079 f = XFRAME (frame);
6080
6081 /* Ignore tooltip frame. */
6082 if (f == tooltip_frame)
6083 continue;
6084
6085 /* If a window on this frame changed size, report that to
6086 the user and clear the size-change flag. */
6087 if (FRAME_WINDOW_SIZES_CHANGED (f))
6088 {
6089 Lisp_Object functions;
6090
6091 /* Clear flag first in case we get an error below. */
6092 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
6093 functions = Vwindow_size_change_functions;
6094 GCPRO2 (tail, functions);
6095
6096 while (CONSP (functions))
6097 {
6098 call1 (XCAR (functions), frame);
6099 functions = XCDR (functions);
6100 }
6101 UNGCPRO;
6102 }
6103
6104 GCPRO1 (tail);
6105 update_menu_bar (f, 0);
6106 #ifdef HAVE_WINDOW_SYSTEM
6107 update_toolbar (f, 0);
6108 #endif
6109 UNGCPRO;
6110 }
6111
6112 unbind_to (count, Qnil);
6113 }
6114 else
6115 {
6116 update_menu_bar (selected_frame, 1);
6117 #ifdef HAVE_WINDOW_SYSTEM
6118 update_toolbar (selected_frame, 1);
6119 #endif
6120 }
6121
6122 /* Motif needs this. See comment in xmenu.c. Turn it off when
6123 pending_menu_activation is not defined. */
6124 #ifdef USE_X_TOOLKIT
6125 pending_menu_activation = 0;
6126 #endif
6127 }
6128
6129
6130 /* Update the menu bar item list for frame F. This has to be done
6131 before we start to fill in any display lines, because it can call
6132 eval.
6133
6134 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
6135
6136 static void
6137 update_menu_bar (f, save_match_data)
6138 struct frame *f;
6139 int save_match_data;
6140 {
6141 Lisp_Object window;
6142 register struct window *w;
6143
6144 window = FRAME_SELECTED_WINDOW (f);
6145 w = XWINDOW (window);
6146
6147 if (update_mode_lines)
6148 w->update_mode_line = Qt;
6149
6150 if (FRAME_WINDOW_P (f)
6151 ?
6152 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
6153 FRAME_EXTERNAL_MENU_BAR (f)
6154 #else
6155 FRAME_MENU_BAR_LINES (f) > 0
6156 #endif
6157 : FRAME_MENU_BAR_LINES (f) > 0)
6158 {
6159 /* If the user has switched buffers or windows, we need to
6160 recompute to reflect the new bindings. But we'll
6161 recompute when update_mode_lines is set too; that means
6162 that people can use force-mode-line-update to request
6163 that the menu bar be recomputed. The adverse effect on
6164 the rest of the redisplay algorithm is about the same as
6165 windows_or_buffers_changed anyway. */
6166 if (windows_or_buffers_changed
6167 || !NILP (w->update_mode_line)
6168 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
6169 < BUF_MODIFF (XBUFFER (w->buffer)))
6170 != !NILP (w->last_had_star))
6171 || ((!NILP (Vtransient_mark_mode)
6172 && !NILP (XBUFFER (w->buffer)->mark_active))
6173 != !NILP (w->region_showing)))
6174 {
6175 struct buffer *prev = current_buffer;
6176 int count = specpdl_ptr - specpdl;
6177
6178 set_buffer_internal_1 (XBUFFER (w->buffer));
6179 if (save_match_data)
6180 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6181 if (NILP (Voverriding_local_map_menu_flag))
6182 {
6183 specbind (Qoverriding_terminal_local_map, Qnil);
6184 specbind (Qoverriding_local_map, Qnil);
6185 }
6186
6187 /* Run the Lucid hook. */
6188 call1 (Vrun_hooks, Qactivate_menubar_hook);
6189
6190 /* If it has changed current-menubar from previous value,
6191 really recompute the menu-bar from the value. */
6192 if (! NILP (Vlucid_menu_bar_dirty_flag))
6193 call0 (Qrecompute_lucid_menubar);
6194
6195 safe_run_hooks (Qmenu_bar_update_hook);
6196 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
6197
6198 /* Redisplay the menu bar in case we changed it. */
6199 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
6200 if (FRAME_WINDOW_P (f))
6201 set_frame_menubar (f, 0, 0);
6202 else
6203 /* On a terminal screen, the menu bar is an ordinary screen
6204 line, and this makes it get updated. */
6205 w->update_mode_line = Qt;
6206 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
6207 /* In the non-toolkit version, the menu bar is an ordinary screen
6208 line, and this makes it get updated. */
6209 w->update_mode_line = Qt;
6210 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
6211
6212 unbind_to (count, Qnil);
6213 set_buffer_internal_1 (prev);
6214 }
6215 }
6216 }
6217
6218
6219 \f
6220 /***********************************************************************
6221 Toolbars
6222 ***********************************************************************/
6223
6224 #ifdef HAVE_WINDOW_SYSTEM
6225
6226 /* Update the toolbar item list for frame F. This has to be done
6227 before we start to fill in any display lines. Called from
6228 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
6229 and restore it here. */
6230
6231 static void
6232 update_toolbar (f, save_match_data)
6233 struct frame *f;
6234 int save_match_data;
6235 {
6236 if (WINDOWP (f->toolbar_window)
6237 && XFASTINT (XWINDOW (f->toolbar_window)->height) > 0)
6238 {
6239 Lisp_Object window;
6240 struct window *w;
6241
6242 window = FRAME_SELECTED_WINDOW (f);
6243 w = XWINDOW (window);
6244
6245 /* If the user has switched buffers or windows, we need to
6246 recompute to reflect the new bindings. But we'll
6247 recompute when update_mode_lines is set too; that means
6248 that people can use force-mode-line-update to request
6249 that the menu bar be recomputed. The adverse effect on
6250 the rest of the redisplay algorithm is about the same as
6251 windows_or_buffers_changed anyway. */
6252 if (windows_or_buffers_changed
6253 || !NILP (w->update_mode_line)
6254 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
6255 < BUF_MODIFF (XBUFFER (w->buffer)))
6256 != !NILP (w->last_had_star))
6257 || ((!NILP (Vtransient_mark_mode)
6258 && !NILP (XBUFFER (w->buffer)->mark_active))
6259 != !NILP (w->region_showing)))
6260 {
6261 struct buffer *prev = current_buffer;
6262 int count = specpdl_ptr - specpdl;
6263
6264 /* Set current_buffer to the buffer of the selected
6265 window of the frame, so that we get the right local
6266 keymaps. */
6267 set_buffer_internal_1 (XBUFFER (w->buffer));
6268
6269 /* Save match data, if we must. */
6270 if (save_match_data)
6271 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6272
6273 /* Make sure that we don't accidentally use bogus keymaps. */
6274 if (NILP (Voverriding_local_map_menu_flag))
6275 {
6276 specbind (Qoverriding_terminal_local_map, Qnil);
6277 specbind (Qoverriding_local_map, Qnil);
6278 }
6279
6280 /* Build desired toolbar items from keymaps. */
6281 f->desired_toolbar_items
6282 = toolbar_items (f->desired_toolbar_items,
6283 &f->n_desired_toolbar_items);
6284
6285 /* Redisplay the toolbar in case we changed it. */
6286 w->update_mode_line = Qt;
6287
6288 unbind_to (count, Qnil);
6289 set_buffer_internal_1 (prev);
6290 }
6291 }
6292 }
6293
6294
6295 /* Set F->desired_toolbar_string to a Lisp string representing frame
6296 F's desired toolbar contents. F->desired_toolbar_items must have
6297 been set up previously by calling prepare_menu_bars. */
6298
6299 static void
6300 build_desired_toolbar_string (f)
6301 struct frame *f;
6302 {
6303 int i, size, size_needed, string_idx;
6304 struct gcpro gcpro1, gcpro2, gcpro3;
6305 Lisp_Object image, plist, props;
6306
6307 image = plist = props = Qnil;
6308 GCPRO3 (image, plist, props);
6309
6310 /* Prepare F->desired_toolbar_string. If we can reuse it, do so.
6311 Otherwise, make a new string. */
6312
6313 /* The size of the string we might be able to reuse. */
6314 size = (STRINGP (f->desired_toolbar_string)
6315 ? XSTRING (f->desired_toolbar_string)->size
6316 : 0);
6317
6318 /* Each image in the string we build is preceded by a space,
6319 and there is a space at the end. */
6320 size_needed = f->n_desired_toolbar_items + 1;
6321
6322 /* Reuse f->desired_toolbar_string, if possible. */
6323 if (size < size_needed)
6324 f->desired_toolbar_string = Fmake_string (make_number (size_needed), ' ');
6325 else
6326 {
6327 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
6328 Fremove_text_properties (make_number (0), make_number (size),
6329 props, f->desired_toolbar_string);
6330 }
6331
6332 /* Put a `display' property on the string for the images to display,
6333 put a `menu_item' property on toolbar items with a value that
6334 is the index of the item in F's toolbar item vector. */
6335 for (i = 0, string_idx = 0;
6336 i < f->n_desired_toolbar_items;
6337 ++i, string_idx += 1)
6338 {
6339 #define PROP(IDX) \
6340 (XVECTOR (f->desired_toolbar_items) \
6341 ->contents[i * TOOLBAR_ITEM_NSLOTS + (IDX)])
6342
6343 int enabled_p = !NILP (PROP (TOOLBAR_ITEM_ENABLED_P));
6344 int selected_p = !NILP (PROP (TOOLBAR_ITEM_SELECTED_P));
6345 int margin, relief;
6346 extern Lisp_Object QCrelief, QCmargin, QCalgorithm, Qimage;
6347 extern Lisp_Object Qlaplace;
6348
6349 /* If image is a vector, choose the image according to the
6350 button state. */
6351 image = PROP (TOOLBAR_ITEM_IMAGES);
6352 if (VECTORP (image))
6353 {
6354 enum toolbar_item_image idx;
6355
6356 if (enabled_p)
6357 idx = (selected_p
6358 ? TOOLBAR_IMAGE_ENABLED_SELECTED
6359 : TOOLBAR_IMAGE_ENABLED_DESELECTED);
6360 else
6361 idx = (selected_p
6362 ? TOOLBAR_IMAGE_DISABLED_SELECTED
6363 : TOOLBAR_IMAGE_DISABLED_DESELECTED);
6364
6365 xassert (XVECTOR (image)->size >= idx);
6366 image = XVECTOR (image)->contents[idx];
6367 }
6368
6369 /* Ignore invalid image specifications. */
6370 if (!valid_image_p (image))
6371 continue;
6372
6373 /* Display the toolbar button pressed, or depressed. */
6374 plist = Fcopy_sequence (XCDR (image));
6375
6376 /* Compute margin and relief to draw. */
6377 relief = toolbar_button_relief > 0 ? toolbar_button_relief : 3;
6378 margin = relief + max (0, toolbar_button_margin);
6379
6380 if (auto_raise_toolbar_buttons_p)
6381 {
6382 /* Add a `:relief' property to the image spec if the item is
6383 selected. */
6384 if (selected_p)
6385 {
6386 plist = Fplist_put (plist, QCrelief, make_number (-relief));
6387 margin -= relief;
6388 }
6389 }
6390 else
6391 {
6392 /* If image is selected, display it pressed, i.e. with a
6393 negative relief. If it's not selected, display it with a
6394 raised relief. */
6395 plist = Fplist_put (plist, QCrelief,
6396 (selected_p
6397 ? make_number (-relief)
6398 : make_number (relief)));
6399 margin -= relief;
6400 }
6401
6402 /* Put a margin around the image. */
6403 if (margin)
6404 plist = Fplist_put (plist, QCmargin, make_number (margin));
6405
6406 /* If button is not enabled, make the image appear disabled by
6407 applying an appropriate algorithm to it. */
6408 if (!enabled_p)
6409 plist = Fplist_put (plist, QCalgorithm, Qlaplace);
6410
6411 /* Put a `display' text property on the string for the image to
6412 display. Put a `menu-item' property on the string that gives
6413 the start of this item's properties in the toolbar items
6414 vector. */
6415 image = Fcons (Qimage, plist);
6416 props = list4 (Qdisplay, image,
6417 Qmenu_item, make_number (i * TOOLBAR_ITEM_NSLOTS)),
6418 Fadd_text_properties (make_number (string_idx),
6419 make_number (string_idx + 1),
6420 props, f->desired_toolbar_string);
6421 #undef PROP
6422 }
6423
6424 UNGCPRO;
6425 }
6426
6427
6428 /* Display one line of the toolbar of frame IT->f. */
6429
6430 static void
6431 display_toolbar_line (it)
6432 struct it *it;
6433 {
6434 struct glyph_row *row = it->glyph_row;
6435 int max_x = it->last_visible_x;
6436 struct glyph *last;
6437
6438 prepare_desired_row (row);
6439 row->y = it->current_y;
6440
6441 while (it->current_x < max_x)
6442 {
6443 int x_before, x, n_glyphs_before, i, nglyphs;
6444
6445 /* Get the next display element. */
6446 if (!get_next_display_element (it))
6447 break;
6448
6449 /* Produce glyphs. */
6450 x_before = it->current_x;
6451 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
6452 PRODUCE_GLYPHS (it);
6453
6454 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
6455 i = 0;
6456 x = x_before;
6457 while (i < nglyphs)
6458 {
6459 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
6460
6461 if (x + glyph->pixel_width > max_x)
6462 {
6463 /* Glyph doesn't fit on line. */
6464 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
6465 it->current_x = x;
6466 goto out;
6467 }
6468
6469 ++it->hpos;
6470 x += glyph->pixel_width;
6471 ++i;
6472 }
6473
6474 /* Stop at line ends. */
6475 if (ITERATOR_AT_END_OF_LINE_P (it))
6476 break;
6477
6478 set_iterator_to_next (it);
6479 }
6480
6481 out:;
6482
6483 row->displays_text_p = row->used[TEXT_AREA] != 0;
6484 extend_face_to_end_of_line (it);
6485 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
6486 last->right_box_line_p = 1;
6487 compute_line_metrics (it);
6488
6489 /* If line is empty, make it occupy the rest of the toolbar. */
6490 if (!row->displays_text_p)
6491 {
6492 row->height = row->phys_height = it->last_visible_y - row->y;
6493 row->ascent = row->phys_ascent = 0;
6494 }
6495
6496 row->full_width_p = 1;
6497 row->continued_p = 0;
6498 row->truncated_on_left_p = 0;
6499 row->truncated_on_right_p = 0;
6500
6501 it->current_x = it->hpos = 0;
6502 it->current_y += row->height;
6503 ++it->vpos;
6504 ++it->glyph_row;
6505 }
6506
6507
6508 /* Value is the number of screen lines needed to make all toolbar
6509 items of frame F visible. */
6510
6511 static int
6512 toolbar_lines_needed (f)
6513 struct frame *f;
6514 {
6515 struct window *w = XWINDOW (f->toolbar_window);
6516 struct it it;
6517
6518 /* Initialize an iterator for iteration over F->desired_toolbar_string
6519 in the toolbar window of frame F. */
6520 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOLBAR_FACE_ID);
6521 it.first_visible_x = 0;
6522 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
6523 reseat_to_string (&it, NULL, f->desired_toolbar_string, 0, 0, 0, -1);
6524
6525 while (!ITERATOR_AT_END_P (&it))
6526 {
6527 it.glyph_row = w->desired_matrix->rows;
6528 clear_glyph_row (it.glyph_row);
6529 display_toolbar_line (&it);
6530 }
6531
6532 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
6533 }
6534
6535
6536 /* Display the toolbar of frame F. Value is non-zero if toolbar's
6537 height should be changed. */
6538
6539 static int
6540 redisplay_toolbar (f)
6541 struct frame *f;
6542 {
6543 struct window *w;
6544 struct it it;
6545 struct glyph_row *row;
6546 int change_height_p = 0;
6547
6548 /* If frame hasn't a toolbar window or if it is zero-height, don't
6549 do anything. This means you must start with toolbar-lines
6550 non-zero to get the auto-sizing effect. Or in other words, you
6551 can turn off toolbars by specifying toolbar-lines zero. */
6552 if (!WINDOWP (f->toolbar_window)
6553 || (w = XWINDOW (f->toolbar_window),
6554 XFASTINT (w->height) == 0))
6555 return 0;
6556
6557 /* Set up an iterator for the toolbar window. */
6558 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOLBAR_FACE_ID);
6559 it.first_visible_x = 0;
6560 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
6561 row = it.glyph_row;
6562
6563 /* Build a string that represents the contents of the toolbar. */
6564 build_desired_toolbar_string (f);
6565 reseat_to_string (&it, NULL, f->desired_toolbar_string, 0, 0, 0, -1);
6566
6567 /* Display as many lines as needed to display all toolbar items. */
6568 while (it.current_y < it.last_visible_y)
6569 display_toolbar_line (&it);
6570
6571 /* It doesn't make much sense to try scrolling in the toolbar
6572 window, so don't do it. */
6573 w->desired_matrix->no_scrolling_p = 1;
6574 w->must_be_updated_p = 1;
6575
6576 if (auto_resize_toolbars_p)
6577 {
6578 int nlines;
6579
6580 /* If there are blank lines at the end, except for a partially
6581 visible blank line at the end that is smaller than
6582 CANON_Y_UNIT, change the toolbar's height. */
6583 row = it.glyph_row - 1;
6584 if (!row->displays_text_p
6585 && row->height >= CANON_Y_UNIT (f))
6586 change_height_p = 1;
6587
6588 /* If row displays toolbar items, but is partially visible,
6589 change the toolbar's height. */
6590 if (row->displays_text_p
6591 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
6592 change_height_p = 1;
6593
6594 /* Resize windows as needed by changing the `toolbar-lines'
6595 frame parameter. */
6596 if (change_height_p
6597 && (nlines = toolbar_lines_needed (f),
6598 nlines != XFASTINT (w->height)))
6599 {
6600 extern Lisp_Object Qtoolbar_lines;
6601 Lisp_Object frame;
6602
6603 XSETFRAME (frame, f);
6604 clear_glyph_matrix (w->desired_matrix);
6605 Fmodify_frame_parameters (frame,
6606 Fcons (Fcons (Qtoolbar_lines,
6607 make_number (nlines)),
6608 Qnil));
6609 fonts_changed_p = 1;
6610 }
6611 }
6612
6613 return change_height_p;
6614 }
6615
6616
6617 /* Get information about the toolbar item which is displayed in GLYPH
6618 on frame F. Return in *PROP_IDX the index where toolbar item
6619 properties start in F->current_toolbar_items. Value is zero if
6620 GLYPH doesn't display a toolbar item. */
6621
6622 int
6623 toolbar_item_info (f, glyph, prop_idx)
6624 struct frame *f;
6625 struct glyph *glyph;
6626 int *prop_idx;
6627 {
6628 Lisp_Object prop;
6629 int success_p;
6630
6631 /* Get the text property `menu-item' at pos. The value of that
6632 property is the start index of this item's properties in
6633 F->current_toolbar_items. */
6634 prop = Fget_text_property (make_number (glyph->charpos),
6635 Qmenu_item, f->current_toolbar_string);
6636 if (INTEGERP (prop))
6637 {
6638 *prop_idx = XINT (prop);
6639 success_p = 1;
6640 }
6641 else
6642 success_p = 0;
6643
6644 return success_p;
6645 }
6646
6647 #endif /* HAVE_WINDOW_SYSTEM */
6648
6649
6650 \f
6651 /************************************************************************
6652 Horizontal scrolling
6653 ************************************************************************/
6654
6655 static int hscroll_window_tree P_ ((Lisp_Object));
6656 static int hscroll_windows P_ ((Lisp_Object));
6657
6658 /* For all leaf windows in the window tree rooted at WINDOW, set their
6659 hscroll value so that PT is (i) visible in the window, and (ii) so
6660 that it is not within a certain margin at the window's left and
6661 right border. Value is non-zero if any window's hscroll has been
6662 changed. */
6663
6664 static int
6665 hscroll_window_tree (window)
6666 Lisp_Object window;
6667 {
6668 int hscrolled_p = 0;
6669
6670 while (WINDOWP (window))
6671 {
6672 struct window *w = XWINDOW (window);
6673
6674 if (WINDOWP (w->hchild))
6675 hscrolled_p |= hscroll_window_tree (w->hchild);
6676 else if (WINDOWP (w->vchild))
6677 hscrolled_p |= hscroll_window_tree (w->vchild);
6678 else if (w->cursor.vpos >= 0)
6679 {
6680 int hscroll_margin, text_area_x, text_area_y;
6681 int text_area_width, text_area_height;
6682 struct glyph_row *cursor_row = MATRIX_ROW (w->current_matrix,
6683 w->cursor.vpos);
6684
6685 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
6686 &text_area_width, &text_area_height);
6687
6688 /* Scroll when cursor is inside this scroll margin. */
6689 hscroll_margin = 5 * CANON_X_UNIT (XFRAME (w->frame));
6690
6691 if ((XFASTINT (w->hscroll)
6692 && w->cursor.x < hscroll_margin)
6693 || (cursor_row->truncated_on_right_p
6694 && (w->cursor.x > text_area_width - hscroll_margin)))
6695 {
6696 struct it it;
6697 int hscroll;
6698 struct buffer *saved_current_buffer;
6699 int pt;
6700
6701 /* Find point in a display of infinite width. */
6702 saved_current_buffer = current_buffer;
6703 current_buffer = XBUFFER (w->buffer);
6704
6705 if (w == XWINDOW (selected_window))
6706 pt = BUF_PT (current_buffer);
6707 else
6708 {
6709 pt = marker_position (w->pointm);
6710 pt = max (BEGV, pt);
6711 pt = min (ZV, pt);
6712 }
6713
6714 /* Move iterator to pt starting at cursor_row->start in
6715 a line with infinite width. */
6716 init_to_row_start (&it, w, cursor_row);
6717 it.last_visible_x = INFINITY;
6718 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
6719 current_buffer = saved_current_buffer;
6720
6721 /* Center cursor in window. */
6722 hscroll = (max (0, it.current_x - text_area_width / 2)
6723 / CANON_X_UNIT (it.f));
6724
6725 /* Don't call Fset_window_hscroll if value hasn't
6726 changed because it will prevent redisplay
6727 optimizations. */
6728 if (XFASTINT (w->hscroll) != hscroll)
6729 {
6730 Fset_window_hscroll (window, make_number (hscroll));
6731 hscrolled_p = 1;
6732 }
6733 }
6734 }
6735
6736 window = w->next;
6737 }
6738
6739 /* Value is non-zero if hscroll of any leaf window has been changed. */
6740 return hscrolled_p;
6741 }
6742
6743
6744 /* Set hscroll so that cursor is visible and not inside horizontal
6745 scroll margins for all windows in the tree rooted at WINDOW. See
6746 also hscroll_window_tree above. Value is non-zero if any window's
6747 hscroll has been changed. If it has, desired matrices on the frame
6748 of WINDOW are cleared. */
6749
6750 static int
6751 hscroll_windows (window)
6752 Lisp_Object window;
6753 {
6754 int hscrolled_p = hscroll_window_tree (window);
6755 if (hscrolled_p)
6756 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
6757 return hscrolled_p;
6758 }
6759
6760
6761 \f
6762 /************************************************************************
6763 Redisplay
6764 ************************************************************************/
6765
6766 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
6767 to a non-zero value. This is sometimes handy to have in a debugger
6768 session. */
6769
6770 #if GLYPH_DEBUG
6771
6772 /* First and last unchanged row for try_window_id. */
6773
6774 int debug_first_unchanged_at_end_vpos;
6775 int debug_last_unchanged_at_beg_vpos;
6776
6777 /* Delta vpos and y. */
6778
6779 int debug_dvpos, debug_dy;
6780
6781 /* Delta in characters and bytes for try_window_id. */
6782
6783 int debug_delta, debug_delta_bytes;
6784
6785 /* Values of window_end_pos and window_end_vpos at the end of
6786 try_window_id. */
6787
6788 int debug_end_pos, debug_end_vpos;
6789
6790 /* Append a string to W->desired_matrix->method. FMT is a printf
6791 format string. A1...A9 are a supplement for a variable-length
6792 argument list. If trace_redisplay_p is non-zero also printf the
6793 resulting string to stderr. */
6794
6795 static void
6796 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
6797 struct window *w;
6798 char *fmt;
6799 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
6800 {
6801 char buffer[512];
6802 char *method = w->desired_matrix->method;
6803 int len = strlen (method);
6804 int size = sizeof w->desired_matrix->method;
6805 int remaining = size - len - 1;
6806
6807 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
6808 if (len && remaining)
6809 {
6810 method[len] = '|';
6811 --remaining, ++len;
6812 }
6813
6814 strncpy (method + len, buffer, remaining);
6815
6816 if (trace_redisplay_p)
6817 fprintf (stderr, "%p (%s): %s\n",
6818 w,
6819 ((BUFFERP (w->buffer)
6820 && STRINGP (XBUFFER (w->buffer)->name))
6821 ? (char *) XSTRING (XBUFFER (w->buffer)->name)->data
6822 : "no buffer"),
6823 buffer);
6824 }
6825
6826 #endif /* GLYPH_DEBUG */
6827
6828
6829 /* This counter is used to clear the face cache every once in a while
6830 in redisplay_internal. It is incremented for each redisplay.
6831 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
6832 cleared. */
6833
6834 #define CLEAR_FACE_CACHE_COUNT 10000
6835 static int clear_face_cache_count;
6836
6837 /* Record the previous terminal frame we displayed. */
6838
6839 static struct frame *previous_terminal_frame;
6840
6841 /* Non-zero while redisplay_internal is in progress. */
6842
6843 int redisplaying_p;
6844
6845
6846 /* Value is non-zero if all changes in window W, which displays
6847 current_buffer, are in the text between START and END. START is a
6848 buffer position, END is given as a distance from Z. Used in
6849 redisplay_internal for display optimization. */
6850
6851 static INLINE int
6852 text_outside_line_unchanged_p (w, start, end)
6853 struct window *w;
6854 int start, end;
6855 {
6856 int unchanged_p = 1;
6857
6858 /* If text or overlays have changed, see where. */
6859 if (XFASTINT (w->last_modified) < MODIFF
6860 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
6861 {
6862 /* Gap in the line? */
6863 if (GPT < start || Z - GPT < end)
6864 unchanged_p = 0;
6865
6866 /* Changes start in front of the line, or end after it? */
6867 if (unchanged_p
6868 && (BEG_UNCHANGED < start - 1
6869 || END_UNCHANGED < end))
6870 unchanged_p = 0;
6871
6872 /* If selective display, can't optimize if changes start at the
6873 beginning of the line. */
6874 if (unchanged_p
6875 && INTEGERP (current_buffer->selective_display)
6876 && XINT (current_buffer->selective_display) > 0
6877 && (BEG_UNCHANGED < start || GPT <= start))
6878 unchanged_p = 0;
6879 }
6880
6881 return unchanged_p;
6882 }
6883
6884
6885 /* Do a frame update, taking possible shortcuts into account. This is
6886 the main external entry point for redisplay.
6887
6888 If the last redisplay displayed an echo area message and that message
6889 is no longer requested, we clear the echo area or bring back the
6890 mini-buffer if that is in use. */
6891
6892 void
6893 redisplay ()
6894 {
6895 redisplay_internal (0);
6896 }
6897
6898
6899 /* Reconsider the setting of B->clip_changed which is displayed
6900 in window W. */
6901
6902 static INLINE void
6903 reconsider_clip_changes (w, b)
6904 struct window *w;
6905 struct buffer *b;
6906 {
6907 if (b->prevent_redisplay_optimizations_p)
6908 b->clip_changed = 1;
6909 else if (b->clip_changed
6910 && !NILP (w->window_end_valid)
6911 && w->current_matrix->buffer == b
6912 && w->current_matrix->zv == BUF_ZV (b)
6913 && w->current_matrix->begv == BUF_BEGV (b))
6914 b->clip_changed = 0;
6915 }
6916
6917
6918 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
6919 response to any user action; therefore, we should preserve the echo
6920 area. (Actually, our caller does that job.) Perhaps in the future
6921 avoid recentering windows if it is not necessary; currently that
6922 causes some problems. */
6923
6924 static void
6925 redisplay_internal (preserve_echo_area)
6926 int preserve_echo_area;
6927 {
6928 struct window *w = XWINDOW (selected_window);
6929 struct frame *f = XFRAME (w->frame);
6930 int pause;
6931 int must_finish = 0;
6932 struct text_pos tlbufpos, tlendpos;
6933 int number_of_visible_frames;
6934 int count;
6935
6936 /* Non-zero means redisplay has to consider all windows on all
6937 frames. Zero means, only selected_window is considered. */
6938 int consider_all_windows_p;
6939
6940 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
6941
6942 /* No redisplay if running in batch mode or frame is not yet fully
6943 initialized, or redisplay is explicitly turned off by setting
6944 Vinhibit_redisplay. */
6945 if (noninteractive
6946 || !NILP (Vinhibit_redisplay)
6947 || !f->glyphs_initialized_p)
6948 return;
6949
6950 /* The flag redisplay_performed_directly_p is set by
6951 direct_output_for_insert when it already did the whole screen
6952 update necessary. */
6953 if (redisplay_performed_directly_p)
6954 {
6955 redisplay_performed_directly_p = 0;
6956 if (!hscroll_windows (selected_window))
6957 return;
6958 }
6959
6960 #ifdef USE_X_TOOLKIT
6961 if (popup_activated ())
6962 return;
6963 #endif
6964
6965 /* I don't think this happens but let's be paranoid. */
6966 if (redisplaying_p)
6967 return;
6968
6969 /* Record a function that resets redisplaying_p to its old value
6970 when we leave this function. */
6971 count = specpdl_ptr - specpdl;
6972 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
6973 ++redisplaying_p;
6974
6975 retry:
6976
6977 reconsider_clip_changes (w, current_buffer);
6978
6979 /* If new fonts have been loaded that make a glyph matrix adjustment
6980 necessary, do it. */
6981 if (fonts_changed_p)
6982 {
6983 adjust_glyphs (NULL);
6984 ++windows_or_buffers_changed;
6985 fonts_changed_p = 0;
6986 }
6987
6988 if (! FRAME_WINDOW_P (selected_frame)
6989 && previous_terminal_frame != selected_frame)
6990 {
6991 /* Since frames on an ASCII terminal share the same display
6992 area, displaying a different frame means redisplay the whole
6993 thing. */
6994 windows_or_buffers_changed++;
6995 SET_FRAME_GARBAGED (selected_frame);
6996 XSETFRAME (Vterminal_frame, selected_frame);
6997 }
6998 previous_terminal_frame = selected_frame;
6999
7000 /* Set the visible flags for all frames. Do this before checking
7001 for resized or garbaged frames; they want to know if their frames
7002 are visible. See the comment in frame.h for
7003 FRAME_SAMPLE_VISIBILITY. */
7004 {
7005 Lisp_Object tail, frame;
7006
7007 number_of_visible_frames = 0;
7008
7009 FOR_EACH_FRAME (tail, frame)
7010 {
7011 struct frame *f = XFRAME (frame);
7012
7013 FRAME_SAMPLE_VISIBILITY (f);
7014 if (FRAME_VISIBLE_P (f))
7015 ++number_of_visible_frames;
7016 clear_desired_matrices (f);
7017 }
7018 }
7019
7020 /* Notice any pending interrupt request to change frame size. */
7021 do_pending_window_change (1);
7022
7023 /* Clear frames marked as garbaged. */
7024 if (frame_garbaged)
7025 clear_garbaged_frames ();
7026
7027 /* Build menubar and toolbar items. */
7028 prepare_menu_bars ();
7029
7030 if (windows_or_buffers_changed)
7031 update_mode_lines++;
7032
7033 /* Detect case that we need to write or remove a star in the mode line. */
7034 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
7035 {
7036 w->update_mode_line = Qt;
7037 if (buffer_shared > 1)
7038 update_mode_lines++;
7039 }
7040
7041 /* If %c is in the mode line, update it if needed. */
7042 if (!NILP (w->column_number_displayed)
7043 /* This alternative quickly identifies a common case
7044 where no change is needed. */
7045 && !(PT == XFASTINT (w->last_point)
7046 && XFASTINT (w->last_modified) >= MODIFF
7047 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
7048 && XFASTINT (w->column_number_displayed) != current_column ())
7049 w->update_mode_line = Qt;
7050
7051 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
7052
7053 /* The variable buffer_shared is set in redisplay_window and
7054 indicates that we redisplay a buffer in different windows. See
7055 there. */
7056 consider_all_windows_p = update_mode_lines || buffer_shared > 1;
7057
7058 /* If specs for an arrow have changed, do thorough redisplay
7059 to ensure we remove any arrow that should no longer exist. */
7060 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
7061 || ! EQ (Voverlay_arrow_string, last_arrow_string))
7062 consider_all_windows_p = windows_or_buffers_changed = 1;
7063
7064 /* Normally the message* functions will have already displayed and
7065 updated the echo area, but the frame may have been trashed, or
7066 the update may have been preempted, so display the echo area
7067 again here. Checking both message buffers captures the case that
7068 the echo area should be cleared. */
7069 if (!NILP (echo_area_buffer[0]) || !NILP (echo_area_buffer[1]))
7070 {
7071 int window_height_changed_p = echo_area_display (0);
7072 must_finish = 1;
7073
7074 if (fonts_changed_p)
7075 goto retry;
7076 else if (window_height_changed_p)
7077 {
7078 consider_all_windows_p = 1;
7079 ++update_mode_lines;
7080 ++windows_or_buffers_changed;
7081
7082 /* If window configuration was changed, frames may have been
7083 marked garbaged. Clear them or we will experience
7084 surprises wrt scrolling. */
7085 if (frame_garbaged)
7086 clear_garbaged_frames ();
7087 }
7088 }
7089 else if (w == XWINDOW (minibuf_window)
7090 && (current_buffer->clip_changed
7091 || XFASTINT (w->last_modified) < MODIFF
7092 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
7093 && resize_mini_window (w))
7094 {
7095 /* Resized active mini-window to fit the size of what it is
7096 showing if its contents might have changed. */
7097 must_finish = 1;
7098 consider_all_windows_p = 1;
7099 ++windows_or_buffers_changed;
7100 ++update_mode_lines;
7101
7102 /* If window configuration was changed, frames may have been
7103 marked garbaged. Clear them or we will experience
7104 surprises wrt scrolling. */
7105 if (frame_garbaged)
7106 clear_garbaged_frames ();
7107 }
7108
7109
7110 /* If showing the region, and mark has changed, we must redisplay
7111 the whole window. The assignment to this_line_start_pos prevents
7112 the optimization directly below this if-statement. */
7113 if (((!NILP (Vtransient_mark_mode)
7114 && !NILP (XBUFFER (w->buffer)->mark_active))
7115 != !NILP (w->region_showing))
7116 || (!NILP (w->region_showing)
7117 && !EQ (w->region_showing,
7118 Fmarker_position (XBUFFER (w->buffer)->mark))))
7119 CHARPOS (this_line_start_pos) = 0;
7120
7121 /* Optimize the case that only the line containing the cursor in the
7122 selected window has changed. Variables starting with this_ are
7123 set in display_line and record information about the line
7124 containing the cursor. */
7125 tlbufpos = this_line_start_pos;
7126 tlendpos = this_line_end_pos;
7127 if (!consider_all_windows_p
7128 && CHARPOS (tlbufpos) > 0
7129 && NILP (w->update_mode_line)
7130 && !current_buffer->clip_changed
7131 && FRAME_VISIBLE_P (XFRAME (w->frame))
7132 && !FRAME_OBSCURED_P (XFRAME (w->frame))
7133 /* Make sure recorded data applies to current buffer, etc. */
7134 && this_line_buffer == current_buffer
7135 && current_buffer == XBUFFER (w->buffer)
7136 && NILP (w->force_start)
7137 /* Point must be on the line that we have info recorded about. */
7138 && PT >= CHARPOS (tlbufpos)
7139 && PT <= Z - CHARPOS (tlendpos)
7140 /* All text outside that line, including its final newline,
7141 must be unchanged */
7142 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
7143 CHARPOS (tlendpos)))
7144 {
7145 if (CHARPOS (tlbufpos) > BEGV
7146 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
7147 && (CHARPOS (tlbufpos) == ZV
7148 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
7149 /* Former continuation line has disappeared by becoming empty */
7150 goto cancel;
7151 else if (XFASTINT (w->last_modified) < MODIFF
7152 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
7153 || MINI_WINDOW_P (w))
7154 {
7155 /* We have to handle the case of continuation around a
7156 wide-column character (See the comment in indent.c around
7157 line 885).
7158
7159 For instance, in the following case:
7160
7161 -------- Insert --------
7162 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
7163 J_I_ ==> J_I_ `^^' are cursors.
7164 ^^ ^^
7165 -------- --------
7166
7167 As we have to redraw the line above, we should goto cancel. */
7168
7169 struct it it;
7170 int line_height_before = this_line_pixel_height;
7171
7172 /* Note that start_display will handle the case that the
7173 line starting at tlbufpos is a continuation lines. */
7174 start_display (&it, w, tlbufpos);
7175
7176 /* Implementation note: It this still necessary? */
7177 if (it.current_x != this_line_start_x)
7178 goto cancel;
7179
7180 TRACE ((stderr, "trying display optimization 1\n"));
7181 w->cursor.vpos = -1;
7182 overlay_arrow_seen = 0;
7183 it.vpos = this_line_vpos;
7184 it.current_y = this_line_y;
7185 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
7186 display_line (&it);
7187
7188 /* If line contains point, is not continued,
7189 and ends at same distance from eob as before, we win */
7190 if (w->cursor.vpos >= 0
7191 /* Line is not continued, otherwise this_line_start_pos
7192 would have been set to 0 in display_line. */
7193 && CHARPOS (this_line_start_pos)
7194 /* Line ends as before. */
7195 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
7196 /* Line has same height as before. Otherwise other lines
7197 would have to be shifted up or down. */
7198 && this_line_pixel_height == line_height_before)
7199 {
7200 /* If this is not the window's last line, we must adjust
7201 the charstarts of the lines below. */
7202 if (it.current_y < it.last_visible_y)
7203 {
7204 struct glyph_row *row
7205 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
7206 int delta, delta_bytes;
7207
7208 if (Z - CHARPOS (tlendpos) == ZV)
7209 {
7210 /* This line ends at end of (accessible part of)
7211 buffer. There is no newline to count. */
7212 delta = (Z
7213 - CHARPOS (tlendpos)
7214 - MATRIX_ROW_START_CHARPOS (row));
7215 delta_bytes = (Z_BYTE
7216 - BYTEPOS (tlendpos)
7217 - MATRIX_ROW_START_BYTEPOS (row));
7218 }
7219 else
7220 {
7221 /* This line ends in a newline. Must take
7222 account of the newline and the rest of the
7223 text that follows. */
7224 delta = (Z
7225 - CHARPOS (tlendpos)
7226 - MATRIX_ROW_START_CHARPOS (row));
7227 delta_bytes = (Z_BYTE
7228 - BYTEPOS (tlendpos)
7229 - MATRIX_ROW_START_BYTEPOS (row));
7230 }
7231
7232 increment_glyph_matrix_buffer_positions (w->current_matrix,
7233 this_line_vpos + 1,
7234 w->current_matrix->nrows,
7235 delta, delta_bytes);
7236 }
7237
7238 /* If this row displays text now but previously didn't,
7239 or vice versa, w->window_end_vpos may have to be
7240 adjusted. */
7241 if ((it.glyph_row - 1)->displays_text_p)
7242 {
7243 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
7244 XSETINT (w->window_end_vpos, this_line_vpos);
7245 }
7246 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
7247 && this_line_vpos > 0)
7248 XSETINT (w->window_end_vpos, this_line_vpos - 1);
7249 w->window_end_valid = Qnil;
7250
7251 /* Update hint: No need to try to scroll in update_window. */
7252 w->desired_matrix->no_scrolling_p = 1;
7253
7254 #if GLYPH_DEBUG
7255 *w->desired_matrix->method = 0;
7256 debug_method_add (w, "optimization 1");
7257 #endif
7258 goto update;
7259 }
7260 else
7261 goto cancel;
7262 }
7263 else if (/* Cursor position hasn't changed. */
7264 PT == XFASTINT (w->last_point)
7265 /* Make sure the cursor was last displayed
7266 in this window. Otherwise we have to reposition it. */
7267 && 0 <= w->cursor.vpos
7268 && XINT (w->height) > w->cursor.vpos)
7269 {
7270 if (!must_finish)
7271 {
7272 do_pending_window_change (1);
7273
7274 /* We used to always goto end_of_redisplay here, but this
7275 isn't enough if we have a blinking cursor. */
7276 if (w->cursor_off_p == w->last_cursor_off_p)
7277 goto end_of_redisplay;
7278 }
7279 goto update;
7280 }
7281 /* If highlighting the region, or if the cursor is in the echo area,
7282 then we can't just move the cursor. */
7283 else if (! (!NILP (Vtransient_mark_mode)
7284 && !NILP (current_buffer->mark_active))
7285 && (w == XWINDOW (current_buffer->last_selected_window)
7286 || highlight_nonselected_windows)
7287 && NILP (w->region_showing)
7288 && NILP (Vshow_trailing_whitespace)
7289 && !cursor_in_echo_area)
7290 {
7291 struct it it;
7292 struct glyph_row *row;
7293
7294 /* Skip from tlbufpos to PT and see where it is. Note that
7295 PT may be in invisible text. If so, we will end at the
7296 next visible position. */
7297 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
7298 NULL, DEFAULT_FACE_ID);
7299 it.current_x = this_line_start_x;
7300 it.current_y = this_line_y;
7301 it.vpos = this_line_vpos;
7302
7303 /* The call to move_it_to stops in front of PT, but
7304 moves over before-strings. */
7305 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
7306
7307 if (it.vpos == this_line_vpos
7308 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
7309 row->enabled_p))
7310 {
7311 xassert (this_line_vpos == it.vpos);
7312 xassert (this_line_y == it.current_y);
7313 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
7314 goto update;
7315 }
7316 else
7317 goto cancel;
7318 }
7319
7320 cancel:
7321 /* Text changed drastically or point moved off of line. */
7322 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
7323 }
7324
7325 CHARPOS (this_line_start_pos) = 0;
7326 consider_all_windows_p |= buffer_shared > 1;
7327 ++clear_face_cache_count;
7328
7329
7330 /* Build desired matrices. If consider_all_windows_p is non-zero,
7331 do it for all windows on all frames. Otherwise do it for
7332 selected_window, only. */
7333
7334 if (consider_all_windows_p)
7335 {
7336 Lisp_Object tail, frame;
7337
7338 /* Clear the face cache eventually. */
7339 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
7340 {
7341 clear_face_cache (0);
7342 clear_face_cache_count = 0;
7343 }
7344
7345 /* Recompute # windows showing selected buffer. This will be
7346 incremented each time such a window is displayed. */
7347 buffer_shared = 0;
7348
7349 FOR_EACH_FRAME (tail, frame)
7350 {
7351 struct frame *f = XFRAME (frame);
7352 if (FRAME_WINDOW_P (f) || f == selected_frame)
7353 {
7354 /* Mark all the scroll bars to be removed; we'll redeem
7355 the ones we want when we redisplay their windows. */
7356 if (condemn_scroll_bars_hook)
7357 (*condemn_scroll_bars_hook) (f);
7358
7359 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
7360 redisplay_windows (FRAME_ROOT_WINDOW (f));
7361
7362 /* Any scroll bars which redisplay_windows should have
7363 nuked should now go away. */
7364 if (judge_scroll_bars_hook)
7365 (*judge_scroll_bars_hook) (f);
7366 }
7367 }
7368 }
7369 else if (FRAME_VISIBLE_P (selected_frame)
7370 && !FRAME_OBSCURED_P (selected_frame))
7371 redisplay_window (selected_window, 1);
7372
7373
7374 /* Compare desired and current matrices, perform output. */
7375
7376 update:
7377
7378 /* If fonts changed, display again. */
7379 if (fonts_changed_p)
7380 goto retry;
7381
7382 /* Prevent various kinds of signals during display update.
7383 stdio is not robust about handling signals,
7384 which can cause an apparent I/O error. */
7385 if (interrupt_input)
7386 unrequest_sigio ();
7387 stop_polling ();
7388
7389 if (consider_all_windows_p)
7390 {
7391 Lisp_Object tail;
7392
7393 pause = 0;
7394
7395 for (tail = Vframe_list; CONSP (tail); tail = XCONS (tail)->cdr)
7396 {
7397 struct frame *f;
7398
7399 if (!FRAMEP (XCONS (tail)->car))
7400 continue;
7401
7402 f = XFRAME (XCONS (tail)->car);
7403
7404 if ((FRAME_WINDOW_P (f) || f == selected_frame)
7405 && FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
7406 {
7407 /* Mark all windows as to be updated. */
7408 set_window_update_flags (XWINDOW (f->root_window), 1);
7409 pause |= update_frame (f, 0, 0);
7410 if (!pause)
7411 {
7412 if (hscroll_windows (f->root_window))
7413 goto retry;
7414
7415 mark_window_display_accurate (f->root_window, 1);
7416 if (frame_up_to_date_hook != 0)
7417 (*frame_up_to_date_hook) (f);
7418 }
7419 }
7420 }
7421 }
7422 else
7423 {
7424 if (FRAME_VISIBLE_P (selected_frame)
7425 && !FRAME_OBSCURED_P (selected_frame))
7426 {
7427 XWINDOW (selected_window)->must_be_updated_p = 1;
7428 pause = update_frame (selected_frame, 0, 0);
7429 if (!pause && hscroll_windows (selected_window))
7430 goto retry;
7431 }
7432 else
7433 pause = 0;
7434
7435 /* We may have called echo_area_display at the top of this
7436 function. If the echo area is on another frame, that may
7437 have put text on a frame other than the selected one, so the
7438 above call to update_frame would not have caught it. Catch
7439 it here. */
7440 {
7441 Lisp_Object mini_window;
7442 struct frame *mini_frame;
7443
7444 mini_window = FRAME_MINIBUF_WINDOW (selected_frame);
7445 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7446
7447 if (mini_frame != selected_frame && FRAME_WINDOW_P (mini_frame))
7448 {
7449 XWINDOW (mini_window)->must_be_updated_p = 1;
7450 pause |= update_frame (mini_frame, 0, 0);
7451 if (!pause && hscroll_windows (mini_window))
7452 goto retry;
7453 }
7454 }
7455 }
7456
7457 /* If display was paused because of pending input, make sure we do a
7458 thorough update the next time. */
7459 if (pause)
7460 {
7461 /* Prevent the optimization at the beginning of
7462 redisplay_internal that tries a single-line update of the
7463 line containing the cursor in the selected window. */
7464 CHARPOS (this_line_start_pos) = 0;
7465
7466 /* Let the overlay arrow be updated the next time. */
7467 if (!NILP (last_arrow_position))
7468 {
7469 last_arrow_position = Qt;
7470 last_arrow_string = Qt;
7471 }
7472
7473 /* If we pause after scrolling, some rows in the current
7474 matrices of some windows are not valid. */
7475 if (!WINDOW_FULL_WIDTH_P (w)
7476 && !FRAME_WINDOW_P (XFRAME (w->frame)))
7477 update_mode_lines = 1;
7478 }
7479
7480 /* Now text on frame agrees with windows, so put info into the
7481 windows for partial redisplay to follow. */
7482 if (!pause)
7483 {
7484 register struct buffer *b = XBUFFER (w->buffer);
7485
7486 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
7487 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
7488 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
7489 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
7490
7491 if (consider_all_windows_p)
7492 mark_window_display_accurate (FRAME_ROOT_WINDOW (selected_frame), 1);
7493 else
7494 {
7495 XSETFASTINT (w->last_point, BUF_PT (b));
7496 w->last_cursor = w->cursor;
7497 w->last_cursor_off_p = w->cursor_off_p;
7498
7499 b->clip_changed = 0;
7500 b->prevent_redisplay_optimizations_p = 0;
7501 w->update_mode_line = Qnil;
7502 XSETFASTINT (w->last_modified, BUF_MODIFF (b));
7503 XSETFASTINT (w->last_overlay_modified, BUF_OVERLAY_MODIFF (b));
7504 w->last_had_star
7505 = (BUF_MODIFF (XBUFFER (w->buffer)) > BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7506 ? Qt : Qnil);
7507
7508 /* Record if we are showing a region, so can make sure to
7509 update it fully at next redisplay. */
7510 w->region_showing = (!NILP (Vtransient_mark_mode)
7511 && (w == XWINDOW (current_buffer->last_selected_window)
7512 || highlight_nonselected_windows)
7513 && !NILP (XBUFFER (w->buffer)->mark_active)
7514 ? Fmarker_position (XBUFFER (w->buffer)->mark)
7515 : Qnil);
7516
7517 w->window_end_valid = w->buffer;
7518 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
7519 last_arrow_string = Voverlay_arrow_string;
7520 if (frame_up_to_date_hook != 0)
7521 (*frame_up_to_date_hook) (selected_frame);
7522
7523 w->current_matrix->buffer = b;
7524 w->current_matrix->begv = BUF_BEGV (b);
7525 w->current_matrix->zv = BUF_ZV (b);
7526 }
7527
7528 update_mode_lines = 0;
7529 windows_or_buffers_changed = 0;
7530 }
7531
7532 /* Start SIGIO interrupts coming again. Having them off during the
7533 code above makes it less likely one will discard output, but not
7534 impossible, since there might be stuff in the system buffer here.
7535 But it is much hairier to try to do anything about that. */
7536 if (interrupt_input)
7537 request_sigio ();
7538 start_polling ();
7539
7540 /* If a frame has become visible which was not before, redisplay
7541 again, so that we display it. Expose events for such a frame
7542 (which it gets when becoming visible) don't call the parts of
7543 redisplay constructing glyphs, so simply exposing a frame won't
7544 display anything in this case. So, we have to display these
7545 frames here explicitly. */
7546 if (!pause)
7547 {
7548 Lisp_Object tail, frame;
7549 int new_count = 0;
7550
7551 FOR_EACH_FRAME (tail, frame)
7552 {
7553 int this_is_visible = 0;
7554
7555 if (XFRAME (frame)->visible)
7556 this_is_visible = 1;
7557 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
7558 if (XFRAME (frame)->visible)
7559 this_is_visible = 1;
7560
7561 if (this_is_visible)
7562 new_count++;
7563 }
7564
7565 if (new_count != number_of_visible_frames)
7566 windows_or_buffers_changed++;
7567 }
7568
7569 /* Change frame size now if a change is pending. */
7570 do_pending_window_change (1);
7571
7572 /* If we just did a pending size change, or have additional
7573 visible frames, redisplay again. */
7574 if (windows_or_buffers_changed && !pause)
7575 goto retry;
7576
7577 end_of_redisplay:;
7578
7579 unbind_to (count, Qnil);
7580 }
7581
7582
7583 /* Redisplay, but leave alone any recent echo area message unless
7584 another message has been requested in its place.
7585
7586 This is useful in situations where you need to redisplay but no
7587 user action has occurred, making it inappropriate for the message
7588 area to be cleared. See tracking_off and
7589 wait_reading_process_input for examples of these situations. */
7590
7591 void
7592 redisplay_preserve_echo_area ()
7593 {
7594 if (!NILP (echo_area_buffer[1]))
7595 {
7596 /* We have a previously displayed message, but no current
7597 message. Redisplay the previous message. */
7598 display_last_displayed_message_p = 1;
7599 redisplay_internal (1);
7600 display_last_displayed_message_p = 0;
7601 }
7602 else
7603 redisplay_internal (1);
7604 }
7605
7606
7607 /* Function registered with record_unwind_protect in
7608 redisplay_internal. Clears the flag indicating that a redisplay is
7609 in progress. */
7610
7611 static Lisp_Object
7612 unwind_redisplay (old_redisplaying_p)
7613 Lisp_Object old_redisplaying_p;
7614 {
7615 redisplaying_p = XFASTINT (old_redisplaying_p);
7616 return Qnil;
7617 }
7618
7619
7620 /* Mark the display of windows in the window tree rooted at WINDOW as
7621 accurate or inaccurate. If FLAG is non-zero mark display of WINDOW
7622 as accurate. If FLAG is zero arrange for WINDOW to be redisplayed
7623 the next time redisplay_internal is called. */
7624
7625 void
7626 mark_window_display_accurate (window, accurate_p)
7627 Lisp_Object window;
7628 int accurate_p;
7629 {
7630 struct window *w;
7631
7632 for (; !NILP (window); window = w->next)
7633 {
7634 w = XWINDOW (window);
7635
7636 if (BUFFERP (w->buffer))
7637 {
7638 struct buffer *b = XBUFFER (w->buffer);
7639
7640 XSETFASTINT (w->last_modified,
7641 accurate_p ? BUF_MODIFF (b) : 0);
7642 XSETFASTINT (w->last_overlay_modified,
7643 accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
7644 w->last_had_star = (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b)
7645 ? Qt : Qnil);
7646
7647 #if 0 /* I don't think this is necessary because display_line does it.
7648 Let's check it. */
7649 /* Record if we are showing a region, so can make sure to
7650 update it fully at next redisplay. */
7651 w->region_showing
7652 = (!NILP (Vtransient_mark_mode)
7653 && (w == XWINDOW (current_buffer->last_selected_window)
7654 || highlight_nonselected_windows)
7655 && (!NILP (b->mark_active)
7656 ? Fmarker_position (b->mark)
7657 : Qnil));
7658 #endif
7659
7660 if (accurate_p)
7661 {
7662 b->clip_changed = 0;
7663 b->prevent_redisplay_optimizations_p = 0;
7664 w->current_matrix->buffer = b;
7665 w->current_matrix->begv = BUF_BEGV (b);
7666 w->current_matrix->zv = BUF_ZV (b);
7667 w->last_cursor = w->cursor;
7668 w->last_cursor_off_p = w->cursor_off_p;
7669 if (w == XWINDOW (selected_window))
7670 w->last_point = BUF_PT (b);
7671 else
7672 w->last_point = XMARKER (w->pointm)->charpos;
7673 }
7674 }
7675
7676 w->window_end_valid = w->buffer;
7677 w->update_mode_line = Qnil;
7678
7679 if (!NILP (w->vchild))
7680 mark_window_display_accurate (w->vchild, accurate_p);
7681 if (!NILP (w->hchild))
7682 mark_window_display_accurate (w->hchild, accurate_p);
7683 }
7684
7685 if (accurate_p)
7686 {
7687 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
7688 last_arrow_string = Voverlay_arrow_string;
7689 }
7690 else
7691 {
7692 /* Force a thorough redisplay the next time by setting
7693 last_arrow_position and last_arrow_string to t, which is
7694 unequal to any useful value of Voverlay_arrow_... */
7695 last_arrow_position = Qt;
7696 last_arrow_string = Qt;
7697 }
7698 }
7699
7700
7701 /* Return value in display table DP (Lisp_Char_Table *) for character
7702 C. Since a display table doesn't have any parent, we don't have to
7703 follow parent. Do not call this function directly but use the
7704 macro DISP_CHAR_VECTOR. */
7705
7706 Lisp_Object
7707 disp_char_vector (dp, c)
7708 struct Lisp_Char_Table *dp;
7709 int c;
7710 {
7711 int code[4], i;
7712 Lisp_Object val;
7713
7714 if (SINGLE_BYTE_CHAR_P (c))
7715 return (dp->contents[c]);
7716
7717 SPLIT_NON_ASCII_CHAR (c, code[0], code[1], code[2]);
7718 if (code[0] != CHARSET_COMPOSITION)
7719 {
7720 if (code[1] < 32)
7721 code[1] = -1;
7722 else if (code[2] < 32)
7723 code[2] = -1;
7724 }
7725
7726 /* Here, the possible range of code[0] (== charset ID) is
7727 128..max_charset. Since the top level char table contains data
7728 for multibyte characters after 256th element, we must increment
7729 code[0] by 128 to get a correct index. */
7730 code[0] += 128;
7731 code[3] = -1; /* anchor */
7732
7733 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
7734 {
7735 val = dp->contents[code[i]];
7736 if (!SUB_CHAR_TABLE_P (val))
7737 return (NILP (val) ? dp->defalt : val);
7738 }
7739
7740 /* Here, val is a sub char table. We return the default value of
7741 it. */
7742 return (dp->defalt);
7743 }
7744
7745
7746 \f
7747 /***********************************************************************
7748 Window Redisplay
7749 ***********************************************************************/
7750
7751 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
7752
7753 static void
7754 redisplay_windows (window)
7755 Lisp_Object window;
7756 {
7757 while (!NILP (window))
7758 {
7759 struct window *w = XWINDOW (window);
7760
7761 if (!NILP (w->hchild))
7762 redisplay_windows (w->hchild);
7763 else if (!NILP (w->vchild))
7764 redisplay_windows (w->vchild);
7765 else
7766 redisplay_window (window, 0);
7767
7768 window = w->next;
7769 }
7770 }
7771
7772
7773 /* Set cursor position of W. PT is assumed to be displayed in ROW.
7774 DELTA is the number of bytes by which positions recorded in ROW
7775 differ from current buffer positions. */
7776
7777 void
7778 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
7779 struct window *w;
7780 struct glyph_row *row;
7781 struct glyph_matrix *matrix;
7782 int delta, delta_bytes, dy, dvpos;
7783 {
7784 struct glyph *glyph = row->glyphs[TEXT_AREA];
7785 struct glyph *end = glyph + row->used[TEXT_AREA];
7786 int x = row->x;
7787 int pt_old = PT - delta;
7788
7789 /* Skip over glyphs not having an object at the start of the row.
7790 These are special glyphs like truncation marks on terminal
7791 frames. */
7792 if (row->displays_text_p)
7793 while (glyph < end
7794 && !glyph->object
7795 && glyph->charpos < 0)
7796 {
7797 x += glyph->pixel_width;
7798 ++glyph;
7799 }
7800
7801 while (glyph < end
7802 && glyph->object
7803 && (!BUFFERP (glyph->object)
7804 || glyph->charpos < pt_old))
7805 {
7806 x += glyph->pixel_width;
7807 ++glyph;
7808 }
7809
7810 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
7811 w->cursor.x = x;
7812 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
7813 w->cursor.y = row->y + dy;
7814
7815 if (w == XWINDOW (selected_window))
7816 {
7817 if (!row->continued_p
7818 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
7819 && row->x == 0)
7820 {
7821 this_line_buffer = XBUFFER (w->buffer);
7822
7823 CHARPOS (this_line_start_pos)
7824 = MATRIX_ROW_START_CHARPOS (row) + delta;
7825 BYTEPOS (this_line_start_pos)
7826 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
7827
7828 CHARPOS (this_line_end_pos)
7829 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
7830 BYTEPOS (this_line_end_pos)
7831 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
7832
7833 this_line_y = w->cursor.y;
7834 this_line_pixel_height = row->height;
7835 this_line_vpos = w->cursor.vpos;
7836 this_line_start_x = row->x;
7837 }
7838 else
7839 CHARPOS (this_line_start_pos) = 0;
7840 }
7841 }
7842
7843
7844 /* Run window scroll functions, if any, for WINDOW with new window
7845 start STARTP. Sets the window start of WINDOW to that position. */
7846
7847 static INLINE struct text_pos
7848 run_window_scroll_functions (window, startp)
7849 Lisp_Object window;
7850 struct text_pos startp;
7851 {
7852 struct window *w = XWINDOW (window);
7853 SET_MARKER_FROM_TEXT_POS (w->start, startp);
7854
7855 if (!NILP (Vwindow_scroll_functions))
7856 {
7857 run_hook_with_args_2 (Qwindow_scroll_functions, window,
7858 make_number (CHARPOS (startp)));
7859 SET_TEXT_POS_FROM_MARKER (startp, w->start);
7860 }
7861
7862 return startp;
7863 }
7864
7865
7866 /* Modify the desired matrix of window W and W->vscroll so that the
7867 line containing the cursor is fully visible. */
7868
7869 static void
7870 make_cursor_line_fully_visible (w)
7871 struct window *w;
7872 {
7873 struct glyph_matrix *matrix;
7874 struct glyph_row *row;
7875 int top_line_height;
7876
7877 /* It's not always possible to find the cursor, e.g, when a window
7878 is full of overlay strings. Don't do anything in that case. */
7879 if (w->cursor.vpos < 0)
7880 return;
7881
7882 matrix = w->desired_matrix;
7883 row = MATRIX_ROW (matrix, w->cursor.vpos);
7884
7885 /* If row->y == top y of window display area, the window isn't tall
7886 enough to display a single line. There is nothing we can do
7887 about it. */
7888 top_line_height = WINDOW_DISPLAY_TOP_LINE_HEIGHT (w);
7889 if (row->y == top_line_height)
7890 return;
7891
7892 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
7893 {
7894 int dy = row->height - row->visible_height;
7895 w->vscroll = 0;
7896 w->cursor.y += dy;
7897 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
7898 }
7899 else if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row))
7900 {
7901 int dy = - (row->height - row->visible_height);
7902 w->vscroll = dy;
7903 w->cursor.y += dy;
7904 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
7905 }
7906
7907 /* When we change the cursor y-position of the selected window,
7908 change this_line_y as well so that the display optimization for
7909 the cursor line of the selected window in redisplay_internal uses
7910 the correct y-position. */
7911 if (w == XWINDOW (selected_window))
7912 this_line_y = w->cursor.y;
7913 }
7914
7915
7916 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
7917 non-zero means only WINDOW is redisplayed in redisplay_internal.
7918 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
7919 in redisplay_window to bring a partially visible line into view in
7920 the case that only the cursor has moved.
7921
7922 Value is
7923
7924 1 if scrolling succeeded
7925
7926 0 if scrolling didn't find point.
7927
7928 -1 if new fonts have been loaded so that we must interrupt
7929 redisplay, adjust glyph matrices, and try again. */
7930
7931 static int
7932 try_scrolling (window, just_this_one_p, scroll_conservatively,
7933 scroll_step, temp_scroll_step)
7934 Lisp_Object window;
7935 int just_this_one_p;
7936 int scroll_conservatively, scroll_step;
7937 int temp_scroll_step;
7938 {
7939 struct window *w = XWINDOW (window);
7940 struct frame *f = XFRAME (w->frame);
7941 struct text_pos scroll_margin_pos;
7942 struct text_pos pos;
7943 struct text_pos startp;
7944 struct it it;
7945 Lisp_Object window_end;
7946 int this_scroll_margin;
7947 int dy = 0;
7948 int scroll_max;
7949 int line_height, rc;
7950 int amount_to_scroll = 0;
7951 Lisp_Object aggressive;
7952 int height;
7953
7954 #if GLYPH_DEBUG
7955 debug_method_add (w, "try_scrolling");
7956 #endif
7957
7958 SET_TEXT_POS_FROM_MARKER (startp, w->start);
7959
7960 /* Compute scroll margin height in pixels. We scroll when point is
7961 within this distance from the top or bottom of the window. */
7962 if (scroll_margin > 0)
7963 {
7964 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
7965 this_scroll_margin *= CANON_Y_UNIT (f);
7966 }
7967 else
7968 this_scroll_margin = 0;
7969
7970 /* Compute how much we should try to scroll maximally to bring point
7971 into view. */
7972 if (scroll_step)
7973 scroll_max = scroll_step;
7974 else if (scroll_conservatively)
7975 scroll_max = scroll_conservatively;
7976 else if (temp_scroll_step)
7977 scroll_max = temp_scroll_step;
7978 else if (NUMBERP (current_buffer->scroll_down_aggressively)
7979 || NUMBERP (current_buffer->scroll_up_aggressively))
7980 /* We're trying to scroll because of aggressive scrolling
7981 but no scroll_step is set. Choose an arbitrary one. Maybe
7982 there should be a variable for this. */
7983 scroll_max = 10;
7984 else
7985 scroll_max = 0;
7986 scroll_max *= CANON_Y_UNIT (f);
7987
7988 /* Decide whether we have to scroll down. Start at the window end
7989 and move this_scroll_margin up to find the position of the scroll
7990 margin. */
7991 window_end = Fwindow_end (window, Qt);
7992 CHARPOS (scroll_margin_pos) = XINT (window_end);
7993 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
7994 if (this_scroll_margin)
7995 {
7996 start_display (&it, w, scroll_margin_pos);
7997 move_it_vertically (&it, - this_scroll_margin);
7998 scroll_margin_pos = it.current.pos;
7999 }
8000
8001 if (PT >= CHARPOS (scroll_margin_pos))
8002 {
8003 int y0;
8004
8005 /* Point is in the scroll margin at the bottom of the window, or
8006 below. Compute a new window start that makes point visible. */
8007
8008 /* Compute the distance from the scroll margin to PT.
8009 Give up if the distance is greater than scroll_max. */
8010 start_display (&it, w, scroll_margin_pos);
8011 y0 = it.current_y;
8012 move_it_to (&it, PT, 0, it.last_visible_y, -1,
8013 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
8014 line_height = (it.max_ascent + it.max_descent
8015 ? it.max_ascent + it.max_descent
8016 : last_height);
8017 dy = it.current_y + line_height - y0;
8018 if (dy > scroll_max)
8019 return 0;
8020
8021 /* Move the window start down. If scrolling conservatively,
8022 move it just enough down to make point visible. If
8023 scroll_step is set, move it down by scroll_step. */
8024 start_display (&it, w, startp);
8025
8026 if (scroll_conservatively)
8027 amount_to_scroll = dy;
8028 else if (scroll_step || temp_scroll_step)
8029 amount_to_scroll = scroll_max;
8030 else
8031 {
8032 aggressive = current_buffer->scroll_down_aggressively;
8033 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
8034 - WINDOW_DISPLAY_TOP_LINE_HEIGHT (w));
8035 if (NUMBERP (aggressive))
8036 amount_to_scroll = XFLOATINT (aggressive) * height;
8037 }
8038
8039 if (amount_to_scroll <= 0)
8040 return 0;
8041
8042 move_it_vertically (&it, amount_to_scroll);
8043 startp = it.current.pos;
8044 }
8045 else
8046 {
8047 /* See if point is inside the scroll margin at the top of the
8048 window. */
8049 scroll_margin_pos = startp;
8050 if (this_scroll_margin)
8051 {
8052 start_display (&it, w, startp);
8053 move_it_vertically (&it, this_scroll_margin);
8054 scroll_margin_pos = it.current.pos;
8055 }
8056
8057 if (PT < CHARPOS (scroll_margin_pos))
8058 {
8059 /* Point is in the scroll margin at the top of the window or
8060 above what is displayed in the window. */
8061 int y0;
8062
8063 /* Compute the vertical distance from PT to the scroll
8064 margin position. Give up if distance is greater than
8065 scroll_max. */
8066 SET_TEXT_POS (pos, PT, PT_BYTE);
8067 start_display (&it, w, pos);
8068 y0 = it.current_y;
8069 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
8070 it.last_visible_y, -1,
8071 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
8072 dy = it.current_y - y0;
8073 if (dy > scroll_max)
8074 return 0;
8075
8076 /* Compute new window start. */
8077 start_display (&it, w, startp);
8078
8079 if (scroll_conservatively)
8080 amount_to_scroll = dy;
8081 else if (scroll_step || temp_scroll_step)
8082 amount_to_scroll = scroll_max;
8083 else
8084 {
8085 aggressive = current_buffer->scroll_up_aggressively;
8086 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
8087 - WINDOW_DISPLAY_TOP_LINE_HEIGHT (w));
8088 if (NUMBERP (aggressive))
8089 amount_to_scroll = XFLOATINT (aggressive) * height;
8090 }
8091
8092 if (amount_to_scroll <= 0)
8093 return 0;
8094
8095 move_it_vertically (&it, - amount_to_scroll);
8096 startp = it.current.pos;
8097 }
8098 }
8099
8100 /* Run window scroll functions. */
8101 startp = run_window_scroll_functions (window, startp);
8102
8103 /* Display the window. Give up if new fonts are loaded, or if point
8104 doesn't appear. */
8105 if (!try_window (window, startp))
8106 rc = -1;
8107 else if (w->cursor.vpos < 0)
8108 {
8109 clear_glyph_matrix (w->desired_matrix);
8110 rc = 0;
8111 }
8112 else
8113 {
8114 /* Maybe forget recorded base line for line number display. */
8115 if (!just_this_one_p
8116 || current_buffer->clip_changed
8117 || BEG_UNCHANGED < CHARPOS (startp))
8118 w->base_line_number = Qnil;
8119
8120 /* If cursor ends up on a partially visible line, shift display
8121 lines up or down. */
8122 make_cursor_line_fully_visible (w);
8123 rc = 1;
8124 }
8125
8126 return rc;
8127 }
8128
8129
8130 /* Compute a suitable window start for window W if display of W starts
8131 on a continuation line. Value is non-zero if a new window start
8132 was computed.
8133
8134 The new window start will be computed, based on W's width, starting
8135 from the start of the continued line. It is the start of the
8136 screen line with the minimum distance from the old start W->start. */
8137
8138 static int
8139 compute_window_start_on_continuation_line (w)
8140 struct window *w;
8141 {
8142 struct text_pos pos, start_pos;
8143 int window_start_changed_p = 0;
8144
8145 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
8146
8147 /* If window start is on a continuation line... Window start may be
8148 < BEGV in case there's invisible text at the start of the
8149 buffer (M-x rmail, for example). */
8150 if (CHARPOS (start_pos) > BEGV
8151 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
8152 {
8153 struct it it;
8154 struct glyph_row *row;
8155
8156 /* Find the start of the continued line. This should be fast
8157 because scan_buffer is fast (newline cache). */
8158 row = w->desired_matrix->rows + (WINDOW_WANTS_TOP_LINE_P (w) ? 1 : 0);
8159 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
8160 row, DEFAULT_FACE_ID);
8161 reseat_at_previous_visible_line_start (&it);
8162
8163 /* If the line start is "too far" away from the window start,
8164 say it takes too much time to compute a new window start. */
8165 if (CHARPOS (start_pos) - IT_CHARPOS (it)
8166 < XFASTINT (w->height) * XFASTINT (w->width))
8167 {
8168 int min_distance, distance;
8169
8170 /* Move forward by display lines to find the new window
8171 start. If window width was enlarged, the new start can
8172 be expected to be > the old start. If window width was
8173 decreased, the new window start will be < the old start.
8174 So, we're looking for the display line start with the
8175 minimum distance from the old window start. */
8176 pos = it.current.pos;
8177 min_distance = INFINITY;
8178 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
8179 distance < min_distance)
8180 {
8181 min_distance = distance;
8182 pos = it.current.pos;
8183 move_it_by_lines (&it, 1, 0);
8184 }
8185
8186 /* Set the window start there. */
8187 SET_MARKER_FROM_TEXT_POS (w->start, pos);
8188 window_start_changed_p = 1;
8189 }
8190 }
8191
8192 return window_start_changed_p;
8193 }
8194
8195
8196 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
8197 selected_window is redisplayed. */
8198
8199 static void
8200 redisplay_window (window, just_this_one_p)
8201 Lisp_Object window;
8202 int just_this_one_p;
8203 {
8204 struct window *w = XWINDOW (window);
8205 struct frame *f = XFRAME (w->frame);
8206 struct buffer *buffer = XBUFFER (w->buffer);
8207 struct buffer *old = current_buffer;
8208 struct text_pos lpoint, opoint, startp;
8209 int update_mode_line;
8210 int tem;
8211 struct it it;
8212 /* Record it now because it's overwritten. */
8213 int current_matrix_up_to_date_p = 0;
8214 int really_switched_buffer = 0;
8215 int temp_scroll_step = 0;
8216 int count = specpdl_ptr - specpdl;
8217
8218 SET_TEXT_POS (lpoint, PT, PT_BYTE);
8219 opoint = lpoint;
8220
8221 /* W must be a leaf window here. */
8222 xassert (!NILP (w->buffer));
8223 #if GLYPH_DEBUG
8224 *w->desired_matrix->method = 0;
8225 #endif
8226
8227 specbind (Qinhibit_point_motion_hooks, Qt);
8228
8229 reconsider_clip_changes (w, buffer);
8230
8231 /* Has the mode line to be updated? */
8232 update_mode_line = (!NILP (w->update_mode_line)
8233 || update_mode_lines
8234 || buffer->clip_changed);
8235
8236 if (MINI_WINDOW_P (w))
8237 {
8238 if (w == XWINDOW (echo_area_window)
8239 && !NILP (echo_area_buffer[0]))
8240 {
8241 if (update_mode_line)
8242 /* We may have to update a tty frame's menu bar or a
8243 toolbar. Example `M-x C-h C-h C-g'. */
8244 goto finish_menu_bars;
8245 else
8246 /* We've already displayed the echo area glyphs in this window. */
8247 goto finish_scroll_bars;
8248 }
8249 else if (w != XWINDOW (minibuf_window))
8250 {
8251 /* W is a mini-buffer window, but it's not the currently
8252 active one, so clear it. */
8253 int yb = window_text_bottom_y (w);
8254 struct glyph_row *row;
8255 int y;
8256
8257 for (y = 0, row = w->desired_matrix->rows;
8258 y < yb;
8259 y += row->height, ++row)
8260 blank_row (w, row, y);
8261 goto finish_scroll_bars;
8262 }
8263 }
8264
8265 /* Otherwise set up data on this window; select its buffer and point
8266 value. */
8267 if (update_mode_line)
8268 {
8269 /* Really select the buffer, for the sake of buffer-local
8270 variables. */
8271 set_buffer_internal_1 (XBUFFER (w->buffer));
8272 really_switched_buffer = 1;
8273 }
8274 else
8275 set_buffer_temp (XBUFFER (w->buffer));
8276 SET_TEXT_POS (opoint, PT, PT_BYTE);
8277
8278 current_matrix_up_to_date_p
8279 = (!NILP (w->window_end_valid)
8280 && !current_buffer->clip_changed
8281 && XFASTINT (w->last_modified) >= MODIFF
8282 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
8283
8284 /* When windows_or_buffers_changed is non-zero, we can't rely on
8285 the window end being valid, so set it to nil there. */
8286 if (windows_or_buffers_changed)
8287 {
8288 /* If window starts on a continuation line, maybe adjust the
8289 window start in case the window's width changed. */
8290 if (XMARKER (w->start)->buffer == current_buffer)
8291 compute_window_start_on_continuation_line (w);
8292
8293 w->window_end_valid = Qnil;
8294 }
8295
8296 /* Some sanity checks. */
8297 CHECK_WINDOW_END (w);
8298 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
8299 abort ();
8300 if (BYTEPOS (opoint) < CHARPOS (opoint))
8301 abort ();
8302
8303 /* If %c is in mode line, update it if needed. */
8304 if (!NILP (w->column_number_displayed)
8305 /* This alternative quickly identifies a common case
8306 where no change is needed. */
8307 && !(PT == XFASTINT (w->last_point)
8308 && XFASTINT (w->last_modified) >= MODIFF
8309 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
8310 && XFASTINT (w->column_number_displayed) != current_column ())
8311 update_mode_line = 1;
8312
8313 /* Count number of windows showing the selected buffer. An indirect
8314 buffer counts as its base buffer. */
8315 if (!just_this_one_p)
8316 {
8317 struct buffer *current_base, *window_base;
8318 current_base = current_buffer;
8319 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
8320 if (current_base->base_buffer)
8321 current_base = current_base->base_buffer;
8322 if (window_base->base_buffer)
8323 window_base = window_base->base_buffer;
8324 if (current_base == window_base)
8325 buffer_shared++;
8326 }
8327
8328 /* Point refers normally to the selected window. For any other
8329 window, set up appropriate value. */
8330 if (!EQ (window, selected_window))
8331 {
8332 int new_pt = XMARKER (w->pointm)->charpos;
8333 int new_pt_byte = marker_byte_position (w->pointm);
8334 if (new_pt < BEGV)
8335 {
8336 new_pt = BEGV;
8337 new_pt_byte = BEGV_BYTE;
8338 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
8339 }
8340 else if (new_pt > (ZV - 1))
8341 {
8342 new_pt = ZV;
8343 new_pt_byte = ZV_BYTE;
8344 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
8345 }
8346
8347 /* We don't use SET_PT so that the point-motion hooks don't run. */
8348 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
8349 }
8350
8351 /* If any of the character widths specified in the display table
8352 have changed, invalidate the width run cache. It's true that
8353 this may be a bit late to catch such changes, but the rest of
8354 redisplay goes (non-fatally) haywire when the display table is
8355 changed, so why should we worry about doing any better? */
8356 if (current_buffer->width_run_cache)
8357 {
8358 struct Lisp_Char_Table *disptab = buffer_display_table ();
8359
8360 if (! disptab_matches_widthtab (disptab,
8361 XVECTOR (current_buffer->width_table)))
8362 {
8363 invalidate_region_cache (current_buffer,
8364 current_buffer->width_run_cache,
8365 BEG, Z);
8366 recompute_width_table (current_buffer, disptab);
8367 }
8368 }
8369
8370 /* If window-start is screwed up, choose a new one. */
8371 if (XMARKER (w->start)->buffer != current_buffer)
8372 goto recenter;
8373
8374 SET_TEXT_POS_FROM_MARKER (startp, w->start);
8375
8376 /* If someone specified a new starting point but did not insist,
8377 check whether it can be used. */
8378 if (!NILP (w->optional_new_start))
8379 {
8380 w->optional_new_start = Qnil;
8381 /* This takes a mini-buffer prompt into account. */
8382 start_display (&it, w, startp);
8383 move_it_to (&it, PT, 0, it.last_visible_y, -1,
8384 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
8385 if (IT_CHARPOS (it) == PT)
8386 w->force_start = Qt;
8387 }
8388
8389 /* Handle case where place to start displaying has been specified,
8390 unless the specified location is outside the accessible range. */
8391 if (!NILP (w->force_start))
8392 {
8393 w->force_start = Qnil;
8394 w->vscroll = 0;
8395 w->window_end_valid = Qnil;
8396
8397 /* Forget any recorded base line for line number display. */
8398 if (!current_matrix_up_to_date_p
8399 || current_buffer->clip_changed)
8400 w->base_line_number = Qnil;
8401
8402 /* Redisplay the mode line. Select the buffer properly for that.
8403 Also, run the hook window-scroll-functions
8404 because we have scrolled. */
8405 /* Note, we do this after clearing force_start because
8406 if there's an error, it is better to forget about force_start
8407 than to get into an infinite loop calling the hook functions
8408 and having them get more errors. */
8409 if (!update_mode_line
8410 || ! NILP (Vwindow_scroll_functions))
8411 {
8412 if (!really_switched_buffer)
8413 {
8414 set_buffer_temp (old);
8415 set_buffer_internal_1 (XBUFFER (w->buffer));
8416 really_switched_buffer = 1;
8417 }
8418
8419 update_mode_line = 1;
8420 w->update_mode_line = Qt;
8421 startp = run_window_scroll_functions (window, startp);
8422 }
8423
8424 XSETFASTINT (w->last_modified, 0);
8425 XSETFASTINT (w->last_overlay_modified, 0);
8426 if (CHARPOS (startp) < BEGV)
8427 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
8428 else if (CHARPOS (startp) > ZV)
8429 SET_TEXT_POS (startp, ZV, ZV_BYTE);
8430
8431 /* Redisplay, then check if cursor has been set during the
8432 redisplay. Give up if new fonts were loaded. */
8433 if (!try_window (window, startp))
8434 {
8435 w->force_start = Qt;
8436 clear_glyph_matrix (w->desired_matrix);
8437 goto restore_buffers;
8438 }
8439
8440 if (w->cursor.vpos < 0)
8441 {
8442 /* If point does not appear, or on a line that is not fully
8443 visible, move point so it does appear. The desired
8444 matrix has been built above, so we can use it. */
8445 int height = window_box_height (w) / 2;
8446 struct glyph_row *row = MATRIX_ROW (w->desired_matrix, 0);
8447
8448 while (row->y < height)
8449 ++row;
8450
8451 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
8452 MATRIX_ROW_START_BYTEPOS (row));
8453
8454 if (w != XWINDOW (selected_window))
8455 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
8456 else if (current_buffer == old)
8457 SET_TEXT_POS (lpoint, PT, PT_BYTE);
8458
8459 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
8460
8461 /* If we are highlighting the region, then we just changed
8462 the region, so redisplay to show it. */
8463 if (!NILP (Vtransient_mark_mode)
8464 && !NILP (current_buffer->mark_active))
8465 {
8466 clear_glyph_matrix (w->desired_matrix);
8467 if (!try_window (window, startp))
8468 goto restore_buffers;
8469 }
8470 }
8471
8472 make_cursor_line_fully_visible (w);
8473 #if GLYPH_DEBUG
8474 debug_method_add (w, "forced window start");
8475 #endif
8476 goto done;
8477 }
8478
8479 /* Handle case where text has not changed, only point, and it has
8480 not moved off the frame. */
8481 if (current_matrix_up_to_date_p
8482 /* Point may be in this window. */
8483 && PT >= CHARPOS (startp)
8484 /* If we don't check this, we are called to move the cursor in a
8485 horizontally split window with a current matrix that doesn't
8486 fit the display. */
8487 && !windows_or_buffers_changed
8488 /* Selective display hasn't changed. */
8489 && !current_buffer->clip_changed
8490 /* If force-mode-line-update was called, really redisplay;
8491 that's how redisplay is forced after e.g. changing
8492 buffer-invisibility-spec. */
8493 && NILP (w->update_mode_line)
8494 /* Can't use this case if highlighting a region. When a
8495 region exists, cursor movement has to do more than just
8496 set the cursor. */
8497 && !(!NILP (Vtransient_mark_mode)
8498 && !NILP (current_buffer->mark_active))
8499 && NILP (w->region_showing)
8500 && NILP (Vshow_trailing_whitespace)
8501 /* Right after splitting windows, last_point may be nil. */
8502 && INTEGERP (w->last_point)
8503 /* This code is not used for mini-buffer for the sake of the case
8504 of redisplaying to replace an echo area message; since in
8505 that case the mini-buffer contents per se are usually
8506 unchanged. This code is of no real use in the mini-buffer
8507 since the handling of this_line_start_pos, etc., in redisplay
8508 handles the same cases. */
8509 && !EQ (window, minibuf_window)
8510 /* When splitting windows or for new windows, it happens that
8511 redisplay is called with a nil window_end_vpos or one being
8512 larger than the window. This should really be fixed in
8513 window.c. I don't have this on my list, now, so we do
8514 approximately the same as the old redisplay code. --gerd. */
8515 && INTEGERP (w->window_end_vpos)
8516 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
8517 && (FRAME_WINDOW_P (f)
8518 || !MARKERP (Voverlay_arrow_position)
8519 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
8520 {
8521 int this_scroll_margin;
8522 struct glyph_row *row;
8523 int scroll_p;
8524
8525 #if GLYPH_DEBUG
8526 debug_method_add (w, "cursor movement");
8527 #endif
8528
8529 /* Scroll if point within this distance from the top or bottom
8530 of the window. This is a pixel value. */
8531 this_scroll_margin = max (0, scroll_margin);
8532 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
8533 this_scroll_margin *= CANON_Y_UNIT (f);
8534
8535 /* Start with the row the cursor was displayed during the last
8536 not paused redisplay. Give up if that row is not valid. */
8537 if (w->last_cursor.vpos >= w->current_matrix->nrows)
8538 goto try_to_scroll;
8539 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
8540 if (row->mode_line_p)
8541 ++row;
8542 if (!row->enabled_p)
8543 goto try_to_scroll;
8544
8545 scroll_p = 0;
8546 if (PT > XFASTINT (w->last_point))
8547 {
8548 /* Point has moved forward. */
8549 int last_y = window_text_bottom_y (w) - this_scroll_margin;
8550
8551 while ((MATRIX_ROW_END_CHARPOS (row) < PT
8552 /* The end position of a row equals the start
8553 position of the next row. If PT is there, we
8554 would rather display it in the next line, except
8555 when this line ends in ZV. */
8556 || (MATRIX_ROW_END_CHARPOS (row) == PT
8557 && (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
8558 || !row->ends_at_zv_p)))
8559 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
8560 {
8561 xassert (row->enabled_p);
8562 ++row;
8563 }
8564
8565 /* If within the scroll margin, scroll. Note that
8566 MATRIX_ROW_BOTTOM_Y gives the pixel position at which the
8567 next line would be drawn, and that this_scroll_margin can
8568 be zero. */
8569 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
8570 || PT > MATRIX_ROW_END_CHARPOS (row)
8571 /* Line is completely visible last line in window and PT
8572 is to be set in the next line. */
8573 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
8574 && PT == MATRIX_ROW_END_CHARPOS (row)
8575 && !row->ends_at_zv_p
8576 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
8577 scroll_p = 1;
8578 }
8579 else if (PT < XFASTINT (w->last_point))
8580 {
8581 /* Cursor has to be moved backward. Note that PT >=
8582 CHARPOS (startp) because of the outer if-statement. */
8583 while (!row->mode_line_p
8584 && (MATRIX_ROW_START_CHARPOS (row) > PT
8585 || (MATRIX_ROW_START_CHARPOS (row) == PT
8586 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
8587 && (row->y > this_scroll_margin
8588 || CHARPOS (startp) == BEGV))
8589 {
8590 xassert (row->enabled_p);
8591 --row;
8592 }
8593
8594 /* Consider the following case: Window starts at BEGV, there
8595 is invisible, intangible text at BEGV, so that display
8596 starts at some point START > BEGV. It can happen that
8597 we are called with PT somewhere between BEGV and START.
8598 Try to handle that case. */
8599 if (row < w->current_matrix->rows
8600 || row->mode_line_p)
8601 {
8602 row = w->current_matrix->rows;
8603 if (row->mode_line_p)
8604 ++row;
8605 }
8606
8607 /* Due to newlines in overlay strings, we may have to skip
8608 forward over overlay strings. */
8609 while (MATRIX_ROW_END_CHARPOS (row) == PT
8610 && MATRIX_ROW_ENDS_IN_OVERLAY_STRING_P (row)
8611 && !row->ends_at_zv_p)
8612 ++row;
8613
8614 /* If within the scroll margin, scroll. */
8615 if (row->y < this_scroll_margin
8616 && CHARPOS (startp) != BEGV)
8617 scroll_p = 1;
8618 }
8619
8620 /* if PT is not in the glyph row, give up. */
8621 if (PT < MATRIX_ROW_START_CHARPOS (row)
8622 || PT > MATRIX_ROW_END_CHARPOS (row))
8623 goto try_to_scroll;
8624
8625 /* If we end up in a partially visible line, let's make it fully
8626 visible. This can be done most easily by using the existing
8627 scrolling code. */
8628 if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
8629 {
8630 temp_scroll_step = 1;
8631 goto try_to_scroll;
8632 }
8633 else if (scroll_p)
8634 goto try_to_scroll;
8635
8636 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
8637 goto done;
8638 }
8639
8640 /* If current starting point was originally the beginning of a line
8641 but no longer is, find a new starting point. */
8642 else if (!NILP (w->start_at_line_beg)
8643 && !(CHARPOS (startp) <= BEGV
8644 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
8645 {
8646 #if GLYPH_DEBUG
8647 debug_method_add (w, "recenter 1");
8648 #endif
8649 goto recenter;
8650 }
8651
8652 /* Try scrolling with try_window_id. */
8653 else if (/* Windows and buffers haven't changed. */
8654 !windows_or_buffers_changed
8655 /* Window must be either use window-based redisplay or
8656 be full width. */
8657 && (FRAME_WINDOW_P (f)
8658 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)))
8659 && !MINI_WINDOW_P (w)
8660 /* Point is not known NOT to appear in window. */
8661 && PT >= CHARPOS (startp)
8662 && XFASTINT (w->last_modified)
8663 /* Window is not hscrolled. */
8664 && XFASTINT (w->hscroll) == 0
8665 /* Selective display has not changed. */
8666 && !current_buffer->clip_changed
8667 /* Current matrix is up to date. */
8668 && !NILP (w->window_end_valid)
8669 /* Can't use this case if highlighting a region because
8670 a cursor movement will do more than just set the cursor. */
8671 && !(!NILP (Vtransient_mark_mode)
8672 && !NILP (current_buffer->mark_active))
8673 && NILP (w->region_showing)
8674 && NILP (Vshow_trailing_whitespace)
8675 /* Overlay arrow position and string not changed. */
8676 && EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
8677 && EQ (last_arrow_string, Voverlay_arrow_string)
8678 /* Value is > 0 if update has been done, it is -1 if we
8679 know that the same window start will not work. It is 0
8680 if unsuccessful for some other reason. */
8681 && (tem = try_window_id (w)) != 0)
8682 {
8683 #if GLYPH_DEBUG
8684 debug_method_add (w, "try_window_id");
8685 #endif
8686
8687 if (fonts_changed_p)
8688 goto restore_buffers;
8689 if (tem > 0)
8690 goto done;
8691 /* Otherwise try_window_id has returned -1 which means that we
8692 don't want the alternative below this comment to execute. */
8693 }
8694 else if (CHARPOS (startp) >= BEGV
8695 && CHARPOS (startp) <= ZV
8696 && PT >= CHARPOS (startp)
8697 && (CHARPOS (startp) < ZV
8698 /* Avoid starting at end of buffer. */
8699 || CHARPOS (startp) == BEGV
8700 || (XFASTINT (w->last_modified) >= MODIFF
8701 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
8702 {
8703 #if GLYPH_DEBUG
8704 debug_method_add (w, "same window start");
8705 #endif
8706
8707 /* Try to redisplay starting at same place as before.
8708 If point has not moved off frame, accept the results. */
8709 if (!current_matrix_up_to_date_p
8710 /* Don't use try_window_reusing_current_matrix in this case
8711 because it can have changed the buffer. */
8712 || !NILP (Vwindow_scroll_functions)
8713 || MINI_WINDOW_P (w)
8714 || !try_window_reusing_current_matrix (w))
8715 {
8716 IF_DEBUG (debug_method_add (w, "1"));
8717 try_window (window, startp);
8718 }
8719
8720 if (fonts_changed_p)
8721 goto restore_buffers;
8722
8723 if (w->cursor.vpos >= 0)
8724 {
8725 if (!just_this_one_p
8726 || current_buffer->clip_changed
8727 || BEG_UNCHANGED < CHARPOS (startp))
8728 /* Forget any recorded base line for line number display. */
8729 w->base_line_number = Qnil;
8730
8731 make_cursor_line_fully_visible (w);
8732 goto done;
8733 }
8734 else
8735 clear_glyph_matrix (w->desired_matrix);
8736 }
8737
8738 try_to_scroll:
8739
8740 XSETFASTINT (w->last_modified, 0);
8741 XSETFASTINT (w->last_overlay_modified, 0);
8742
8743 /* Redisplay the mode line. Select the buffer properly for that. */
8744 if (!update_mode_line)
8745 {
8746 if (!really_switched_buffer)
8747 {
8748 set_buffer_temp (old);
8749 set_buffer_internal_1 (XBUFFER (w->buffer));
8750 really_switched_buffer = 1;
8751 }
8752 update_mode_line = 1;
8753 w->update_mode_line = Qt;
8754 }
8755
8756 /* Try to scroll by specified few lines. */
8757 if ((scroll_conservatively
8758 || scroll_step
8759 || temp_scroll_step
8760 || NUMBERP (current_buffer->scroll_up_aggressively)
8761 || NUMBERP (current_buffer->scroll_down_aggressively))
8762 && !current_buffer->clip_changed
8763 && CHARPOS (startp) >= BEGV
8764 && CHARPOS (startp) <= ZV)
8765 {
8766 /* The function returns -1 if new fonts were loaded, 1 if
8767 successful, 0 if not successful. */
8768 int rc = try_scrolling (window, just_this_one_p,
8769 scroll_conservatively,
8770 scroll_step,
8771 temp_scroll_step);
8772 if (rc > 0)
8773 goto done;
8774 else if (rc < 0)
8775 goto restore_buffers;
8776 }
8777
8778 /* Finally, just choose place to start which centers point */
8779
8780 recenter:
8781
8782 #if GLYPH_DEBUG
8783 debug_method_add (w, "recenter");
8784 #endif
8785
8786 /* w->vscroll = 0; */
8787
8788 /* Forget any previously recorded base line for line number display. */
8789 if (!current_matrix_up_to_date_p
8790 || current_buffer->clip_changed)
8791 w->base_line_number = Qnil;
8792
8793 /* Move backward half the height of the window. */
8794 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
8795 it.current_y = it.last_visible_y;
8796 move_it_vertically_backward (&it, it.last_visible_y / 2);
8797 xassert (IT_CHARPOS (it) >= BEGV);
8798
8799 /* The function move_it_vertically_backward may move over more
8800 than the specified y-distance. If it->w is small, e.g. a
8801 mini-buffer window, we may end up in front of the window's
8802 display area. Start displaying at the start of the line
8803 containing PT in this case. */
8804 if (it.current_y <= 0)
8805 {
8806 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
8807 move_it_vertically (&it, 0);
8808 xassert (IT_CHARPOS (it) <= PT);
8809 it.current_y = 0;
8810 }
8811
8812 it.current_x = it.hpos = 0;
8813
8814 /* Set startp here explicitly in case that helps avoid an infinite loop
8815 in case the window-scroll-functions functions get errors. */
8816 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
8817
8818 /* Run scroll hooks. */
8819 startp = run_window_scroll_functions (window, it.current.pos);
8820
8821 /* Redisplay the window. */
8822 if (!current_matrix_up_to_date_p
8823 || windows_or_buffers_changed
8824 /* Don't use try_window_reusing_current_matrix in this case
8825 because it can have changed the buffer. */
8826 || !NILP (Vwindow_scroll_functions)
8827 || !just_this_one_p
8828 || MINI_WINDOW_P (w)
8829 || !try_window_reusing_current_matrix (w))
8830 try_window (window, startp);
8831
8832 /* If new fonts have been loaded (due to fontsets), give up. We
8833 have to start a new redisplay since we need to re-adjust glyph
8834 matrices. */
8835 if (fonts_changed_p)
8836 goto restore_buffers;
8837
8838 /* If cursor did not appear assume that the middle of the window is
8839 in the first line of the window. Do it again with the next line.
8840 (Imagine a window of height 100, displaying two lines of height
8841 60. Moving back 50 from it->last_visible_y will end in the first
8842 line.) */
8843 if (w->cursor.vpos < 0)
8844 {
8845 if (!NILP (w->window_end_valid)
8846 && PT >= Z - XFASTINT (w->window_end_pos))
8847 {
8848 clear_glyph_matrix (w->desired_matrix);
8849 move_it_by_lines (&it, 1, 0);
8850 try_window (window, it.current.pos);
8851 }
8852 else if (PT < IT_CHARPOS (it))
8853 {
8854 clear_glyph_matrix (w->desired_matrix);
8855 move_it_by_lines (&it, -1, 0);
8856 try_window (window, it.current.pos);
8857 }
8858 else
8859 {
8860 /* Not much we can do about it. */
8861 }
8862 }
8863
8864 /* Consider the following case: Window starts at BEGV, there is
8865 invisible, intangible text at BEGV, so that display starts at
8866 some point START > BEGV. It can happen that we are called with
8867 PT somewhere between BEGV and START. Try to handle that case. */
8868 if (w->cursor.vpos < 0)
8869 {
8870 struct glyph_row *row = w->current_matrix->rows;
8871 if (row->mode_line_p)
8872 ++row;
8873 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
8874 }
8875
8876 make_cursor_line_fully_visible (w);
8877
8878 SET_TEXT_POS_FROM_MARKER (startp, w->start);
8879 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
8880 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
8881 ? Qt : Qnil);
8882
8883 done:
8884
8885 /* Display the mode line, if we must. */
8886 if ((update_mode_line
8887 /* If window not full width, must redo its mode line
8888 if (a) the window to its side is being redone and
8889 (b) we do a frame-based redisplay. This is a consequence
8890 of how inverted lines are drawn in frame-based redisplay. */
8891 || (!just_this_one_p
8892 && !FRAME_WINDOW_P (f)
8893 && !WINDOW_FULL_WIDTH_P (w))
8894 /* Line number to display. */
8895 || INTEGERP (w->base_line_pos)
8896 /* Column number is displayed and different from the one displayed. */
8897 || (!NILP (w->column_number_displayed)
8898 && XFASTINT (w->column_number_displayed) != current_column ()))
8899 /* This means that the window has a mode line. */
8900 && (WINDOW_WANTS_MODELINE_P (w)
8901 || WINDOW_WANTS_TOP_LINE_P (w)))
8902 {
8903 display_mode_lines (w);
8904
8905 /* If mode line height has changed, arrange for a thorough
8906 immediate redisplay using the correct mode line height. */
8907 if (WINDOW_WANTS_MODELINE_P (w)
8908 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
8909 {
8910 fonts_changed_p = 1;
8911 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
8912 = DESIRED_MODE_LINE_HEIGHT (w);
8913 }
8914
8915 /* If top line height has changed, arrange for a thorough
8916 immediate redisplay using the correct mode line height. */
8917 if (WINDOW_WANTS_TOP_LINE_P (w)
8918 && CURRENT_TOP_LINE_HEIGHT (w) != DESIRED_TOP_LINE_HEIGHT (w))
8919 {
8920 fonts_changed_p = 1;
8921 MATRIX_TOP_LINE_ROW (w->current_matrix)->height
8922 = DESIRED_TOP_LINE_HEIGHT (w);
8923 }
8924
8925 if (fonts_changed_p)
8926 goto restore_buffers;
8927 }
8928
8929 if (!line_number_displayed
8930 && !BUFFERP (w->base_line_pos))
8931 {
8932 w->base_line_pos = Qnil;
8933 w->base_line_number = Qnil;
8934 }
8935
8936 finish_menu_bars:
8937
8938 /* When we reach a frame's selected window, redo the frame's menu bar. */
8939 if (update_mode_line
8940 && EQ (FRAME_SELECTED_WINDOW (f), window))
8941 {
8942 int redisplay_menu_p = 0;
8943
8944 if (FRAME_WINDOW_P (f))
8945 {
8946 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
8947 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
8948 #else
8949 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
8950 #endif
8951 }
8952 else
8953 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
8954
8955 if (redisplay_menu_p)
8956 display_menu_bar (w);
8957
8958 #ifdef HAVE_WINDOW_SYSTEM
8959 if (WINDOWP (f->toolbar_window)
8960 && (FRAME_TOOLBAR_LINES (f) > 0
8961 || auto_resize_toolbars_p))
8962 redisplay_toolbar (f);
8963 #endif
8964 }
8965
8966 finish_scroll_bars:
8967
8968 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
8969 {
8970 int start, end, whole;
8971
8972 /* Calculate the start and end positions for the current window.
8973 At some point, it would be nice to choose between scrollbars
8974 which reflect the whole buffer size, with special markers
8975 indicating narrowing, and scrollbars which reflect only the
8976 visible region.
8977
8978 Note that mini-buffers sometimes aren't displaying any text. */
8979 if (!MINI_WINDOW_P (w)
8980 || (w == XWINDOW (minibuf_window)
8981 && NILP (echo_area_buffer[0])))
8982 {
8983 whole = ZV - BEGV;
8984 start = marker_position (w->start) - BEGV;
8985 /* I don't think this is guaranteed to be right. For the
8986 moment, we'll pretend it is. */
8987 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
8988
8989 if (end < start)
8990 end = start;
8991 if (whole < (end - start))
8992 whole = end - start;
8993 }
8994 else
8995 start = end = whole = 0;
8996
8997 /* Indicate what this scroll bar ought to be displaying now. */
8998 (*set_vertical_scroll_bar_hook) (w, end - start, whole, start);
8999
9000 /* Note that we actually used the scroll bar attached to this
9001 window, so it shouldn't be deleted at the end of redisplay. */
9002 (*redeem_scroll_bar_hook) (w);
9003 }
9004
9005 restore_buffers:
9006
9007 /* Restore current_buffer and value of point in it. */
9008 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
9009 if (really_switched_buffer)
9010 set_buffer_internal_1 (old);
9011 else
9012 set_buffer_temp (old);
9013 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
9014
9015 unbind_to (count, Qnil);
9016 }
9017
9018
9019 /* Build the complete desired matrix of WINDOW with a window start
9020 buffer position POS. Value is non-zero if successful. It is zero
9021 if fonts were loaded during redisplay which makes re-adjusting
9022 glyph matrices necessary. */
9023
9024 int
9025 try_window (window, pos)
9026 Lisp_Object window;
9027 struct text_pos pos;
9028 {
9029 struct window *w = XWINDOW (window);
9030 struct it it;
9031 struct glyph_row *last_text_row = NULL;
9032
9033 /* Make POS the new window start. */
9034 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
9035
9036 /* Mark cursor position as unknown. No overlay arrow seen. */
9037 w->cursor.vpos = -1;
9038 overlay_arrow_seen = 0;
9039
9040 /* Initialize iterator and info to start at POS. */
9041 start_display (&it, w, pos);
9042
9043 /* Display all lines of W. */
9044 while (it.current_y < it.last_visible_y)
9045 {
9046 if (display_line (&it))
9047 last_text_row = it.glyph_row - 1;
9048 if (fonts_changed_p)
9049 return 0;
9050 }
9051
9052 /* If bottom moved off end of frame, change mode line percentage. */
9053 if (XFASTINT (w->window_end_pos) <= 0
9054 && Z != IT_CHARPOS (it))
9055 w->update_mode_line = Qt;
9056
9057 /* Set window_end_pos to the offset of the last character displayed
9058 on the window from the end of current_buffer. Set
9059 window_end_vpos to its row number. */
9060 if (last_text_row)
9061 {
9062 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
9063 w->window_end_bytepos
9064 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
9065 XSETFASTINT (w->window_end_pos,
9066 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
9067 XSETFASTINT (w->window_end_vpos,
9068 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
9069 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
9070 ->displays_text_p);
9071 }
9072 else
9073 {
9074 w->window_end_bytepos = 0;
9075 XSETFASTINT (w->window_end_pos, 0);
9076 XSETFASTINT (w->window_end_vpos, 0);
9077 }
9078
9079 /* But that is not valid info until redisplay finishes. */
9080 w->window_end_valid = Qnil;
9081 return 1;
9082 }
9083
9084
9085 \f
9086 /************************************************************************
9087 Window redisplay reusing current matrix when buffer has not changed
9088 ************************************************************************/
9089
9090 /* Try redisplay of window W showing an unchanged buffer with a
9091 different window start than the last time it was displayed by
9092 reusing its current matrix. Value is non-zero if successful.
9093 W->start is the new window start. */
9094
9095 static int
9096 try_window_reusing_current_matrix (w)
9097 struct window *w;
9098 {
9099 struct frame *f = XFRAME (w->frame);
9100 struct glyph_row *row, *bottom_row;
9101 struct it it;
9102 struct run run;
9103 struct text_pos start, new_start;
9104 int nrows_scrolled, i;
9105 struct glyph_row *last_text_row;
9106 struct glyph_row *last_reused_text_row;
9107 struct glyph_row *start_row;
9108 int start_vpos, min_y, max_y;
9109
9110 /* Right now this function doesn't handle terminal frames. */
9111 if (!FRAME_WINDOW_P (f))
9112 return 0;
9113
9114 /* Can't do this if region may have changed. */
9115 if ((!NILP (Vtransient_mark_mode)
9116 && !NILP (current_buffer->mark_active))
9117 || !NILP (w->region_showing)
9118 || !NILP (Vshow_trailing_whitespace))
9119 return 0;
9120
9121 /* If top-line visibility has changed, give up. */
9122 if (WINDOW_WANTS_TOP_LINE_P (w)
9123 != MATRIX_TOP_LINE_ROW (w->current_matrix)->mode_line_p)
9124 return 0;
9125
9126 /* Give up if old or new display is scrolled vertically. We could
9127 make this function handle this, but right now it doesn't. */
9128 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9129 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
9130 return 0;
9131
9132 /* The variable new_start now holds the new window start. The old
9133 start `start' can be determined from the current matrix. */
9134 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
9135 start = start_row->start.pos;
9136 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
9137
9138 /* Clear the desired matrix for the display below. */
9139 clear_glyph_matrix (w->desired_matrix);
9140
9141 if (CHARPOS (new_start) <= CHARPOS (start))
9142 {
9143 int first_row_y;
9144
9145 IF_DEBUG (debug_method_add (w, "twu1"));
9146
9147 /* Display up to a row that can be reused. The variable
9148 last_text_row is set to the last row displayed that displays
9149 text. */
9150 start_display (&it, w, new_start);
9151 first_row_y = it.current_y;
9152 w->cursor.vpos = -1;
9153 last_text_row = last_reused_text_row = NULL;
9154 while (it.current_y < it.last_visible_y
9155 && IT_CHARPOS (it) < CHARPOS (start)
9156 && !fonts_changed_p)
9157 if (display_line (&it))
9158 last_text_row = it.glyph_row - 1;
9159
9160 /* A value of current_y < last_visible_y means that we stopped
9161 at the previous window start, which in turn means that we
9162 have at least one reusable row. */
9163 if (it.current_y < it.last_visible_y)
9164 {
9165 nrows_scrolled = it.vpos;
9166
9167 /* Find PT if not already found in the lines displayed. */
9168 if (w->cursor.vpos < 0)
9169 {
9170 int dy = it.current_y - first_row_y;
9171
9172 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9173 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9174 {
9175 if (PT >= MATRIX_ROW_START_CHARPOS (row)
9176 && PT < MATRIX_ROW_END_CHARPOS (row))
9177 {
9178 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
9179 dy, nrows_scrolled);
9180 break;
9181 }
9182
9183 if (MATRIX_ROW_BOTTOM_Y (row) + dy >= it.last_visible_y)
9184 break;
9185
9186 ++row;
9187 }
9188
9189 /* Give up if point was not found. This shouldn't
9190 happen often; not more often than with try_window
9191 itself. */
9192 if (w->cursor.vpos < 0)
9193 {
9194 clear_glyph_matrix (w->desired_matrix);
9195 return 0;
9196 }
9197 }
9198
9199 /* Scroll the display. Do it before the current matrix is
9200 changed. The problem here is that update has not yet
9201 run, i.e. part of the current matrix is not up to date.
9202 scroll_run_hook will clear the cursor, and use the
9203 current matrix to get the height of the row the cursor is
9204 in. */
9205 run.current_y = first_row_y;
9206 run.desired_y = it.current_y;
9207 run.height = it.last_visible_y - it.current_y;
9208 if (run.height > 0)
9209 {
9210 update_begin (f);
9211 rif->update_window_begin_hook (w);
9212 rif->scroll_run_hook (w, &run);
9213 rif->update_window_end_hook (w, 0);
9214 update_end (f);
9215 }
9216
9217 /* Shift current matrix down by nrows_scrolled lines. */
9218 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
9219 rotate_matrix (w->current_matrix,
9220 start_vpos,
9221 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
9222 nrows_scrolled);
9223
9224 /* Disable lines not reused. */
9225 for (i = 0; i < it.vpos; ++i)
9226 MATRIX_ROW (w->current_matrix, i)->enabled_p = 0;
9227
9228 /* Re-compute Y positions. */
9229 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix) + nrows_scrolled;
9230 min_y = WINDOW_DISPLAY_TOP_LINE_HEIGHT (w);
9231 max_y = it.last_visible_y;
9232 while (row < bottom_row)
9233 {
9234 row->y = it.current_y;
9235
9236 if (row->y < min_y)
9237 row->visible_height = row->height - (min_y - row->y);
9238 else if (row->y + row->height > max_y)
9239 row->visible_height
9240 = row->height - (row->y + row->height - max_y);
9241 else
9242 row->visible_height = row->height;
9243
9244 it.current_y += row->height;
9245 ++it.vpos;
9246
9247 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9248 last_reused_text_row = row;
9249 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
9250 break;
9251 ++row;
9252 }
9253 }
9254
9255 /* Update window_end_pos etc.; last_reused_text_row is the last
9256 reused row from the current matrix containing text, if any.
9257 The value of last_text_row is the last displayed line
9258 containing text. */
9259 if (last_reused_text_row)
9260 {
9261 w->window_end_bytepos
9262 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
9263 XSETFASTINT (w->window_end_pos,
9264 Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
9265 XSETFASTINT (w->window_end_vpos,
9266 MATRIX_ROW_VPOS (last_reused_text_row,
9267 w->current_matrix));
9268 }
9269 else if (last_text_row)
9270 {
9271 w->window_end_bytepos
9272 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
9273 XSETFASTINT (w->window_end_pos,
9274 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
9275 XSETFASTINT (w->window_end_vpos,
9276 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
9277 }
9278 else
9279 {
9280 /* This window must be completely empty. */
9281 w->window_end_bytepos = 0;
9282 XSETFASTINT (w->window_end_pos, 0);
9283 XSETFASTINT (w->window_end_vpos, 0);
9284 }
9285 w->window_end_valid = Qnil;
9286
9287 /* Update hint: don't try scrolling again in update_window. */
9288 w->desired_matrix->no_scrolling_p = 1;
9289
9290 #if GLYPH_DEBUG
9291 debug_method_add (w, "try_window_reusing_current_matrix 1");
9292 #endif
9293 return 1;
9294 }
9295 else if (CHARPOS (new_start) > CHARPOS (start))
9296 {
9297 struct glyph_row *pt_row, *row;
9298 struct glyph_row *first_reusable_row;
9299 struct glyph_row *first_row_to_display;
9300 int dy;
9301 int yb = window_text_bottom_y (w);
9302
9303 IF_DEBUG (debug_method_add (w, "twu2"));
9304
9305 /* Find the row starting at new_start, if there is one. Don't
9306 reuse a partially visible line at the end. */
9307 first_reusable_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9308 while (first_reusable_row->enabled_p
9309 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
9310 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
9311 < CHARPOS (new_start)))
9312 ++first_reusable_row;
9313
9314 /* Give up if there is no row to reuse. */
9315 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
9316 || !first_reusable_row->enabled_p
9317 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
9318 != CHARPOS (new_start)))
9319 return 0;
9320
9321 /* We can reuse fully visible rows beginning with
9322 first_reusable_row to the end of the window. Set
9323 first_row_to_display to the first row that cannot be reused.
9324 Set pt_row to the row containing point, if there is any. */
9325 first_row_to_display = first_reusable_row;
9326 pt_row = NULL;
9327 while (MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb)
9328 {
9329 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
9330 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
9331 pt_row = first_row_to_display;
9332
9333 ++first_row_to_display;
9334 }
9335
9336 /* Start displaying at the start of first_row_to_display. */
9337 xassert (first_row_to_display->y < yb);
9338 init_to_row_start (&it, w, first_row_to_display);
9339 nrows_scrolled = MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix);
9340 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
9341 - nrows_scrolled);
9342 it.current_y = first_row_to_display->y - first_reusable_row->y;
9343
9344 /* Display lines beginning with first_row_to_display in the
9345 desired matrix. Set last_text_row to the last row displayed
9346 that displays text. */
9347 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
9348 if (pt_row == NULL)
9349 w->cursor.vpos = -1;
9350 last_text_row = NULL;
9351 while (it.current_y < it.last_visible_y && !fonts_changed_p)
9352 if (display_line (&it))
9353 last_text_row = it.glyph_row - 1;
9354
9355 /* Give up If point isn't in a row displayed or reused. */
9356 if (w->cursor.vpos < 0)
9357 {
9358 clear_glyph_matrix (w->desired_matrix);
9359 return 0;
9360 }
9361
9362 /* If point is in a reused row, adjust y and vpos of the cursor
9363 position. */
9364 if (pt_row)
9365 {
9366 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
9367 w->current_matrix);
9368 w->cursor.y -= first_reusable_row->y;
9369 }
9370
9371 /* Scroll the display. */
9372 run.current_y = first_reusable_row->y;
9373 run.desired_y = WINDOW_DISPLAY_TOP_LINE_HEIGHT (w);
9374 run.height = it.last_visible_y - run.current_y;
9375 if (run.height)
9376 {
9377 struct frame *f = XFRAME (WINDOW_FRAME (w));
9378 update_begin (f);
9379 rif->update_window_begin_hook (w);
9380 rif->scroll_run_hook (w, &run);
9381 rif->update_window_end_hook (w, 0);
9382 update_end (f);
9383 }
9384
9385 /* Adjust Y positions of reused rows. */
9386 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
9387 row = first_reusable_row;
9388 dy = first_reusable_row->y;
9389 min_y = WINDOW_DISPLAY_TOP_LINE_HEIGHT (w);
9390 max_y = it.last_visible_y;
9391 while (row < first_row_to_display)
9392 {
9393 row->y -= dy;
9394 if (row->y < min_y)
9395 row->visible_height = row->height - (min_y - row->y);
9396 else if (row->y + row->height > max_y)
9397 row->visible_height
9398 = row->height - (row->y + row->height - max_y);
9399 else
9400 row->visible_height = row->height;
9401 ++row;
9402 }
9403
9404 /* Disable rows not reused. */
9405 while (row < bottom_row)
9406 {
9407 row->enabled_p = 0;
9408 ++row;
9409 }
9410
9411 /* Scroll the current matrix. */
9412 xassert (nrows_scrolled > 0);
9413 rotate_matrix (w->current_matrix,
9414 start_vpos,
9415 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
9416 -nrows_scrolled);
9417
9418 /* Adjust window end. A null value of last_text_row means that
9419 the window end is in reused rows which in turn means that
9420 only its vpos can have changed. */
9421 if (last_text_row)
9422 {
9423 w->window_end_bytepos
9424 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
9425 XSETFASTINT (w->window_end_pos,
9426 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
9427 XSETFASTINT (w->window_end_vpos,
9428 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
9429 }
9430 else
9431 {
9432 XSETFASTINT (w->window_end_vpos,
9433 XFASTINT (w->window_end_vpos) - nrows_scrolled);
9434 }
9435
9436 w->window_end_valid = Qnil;
9437 w->desired_matrix->no_scrolling_p = 1;
9438
9439 #if GLYPH_DEBUG
9440 debug_method_add (w, "try_window_reusing_current_matrix 2");
9441 #endif
9442 return 1;
9443 }
9444
9445 return 0;
9446 }
9447
9448
9449 \f
9450 /************************************************************************
9451 Window redisplay reusing current matrix when buffer has changed
9452 ************************************************************************/
9453
9454 static struct glyph_row *get_last_unchanged_at_beg_row P_ ((struct window *));
9455 static struct glyph_row *get_first_unchanged_at_end_row P_ ((struct window *,
9456 int *, int *));
9457 static struct glyph_row *
9458 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
9459 struct glyph_row *));
9460
9461
9462 /* Return the last row in MATRIX displaying text. If row START is
9463 non-null, start searching with that row. IT gives the dimensions
9464 of the display. Value is null if matrix is empty; otherwise it is
9465 a pointer to the row found. */
9466
9467 static struct glyph_row *
9468 find_last_row_displaying_text (matrix, it, start)
9469 struct glyph_matrix *matrix;
9470 struct it *it;
9471 struct glyph_row *start;
9472 {
9473 struct glyph_row *row, *row_found;
9474
9475 /* Set row_found to the last row in IT->w's current matrix
9476 displaying text. The loop looks funny but think of partially
9477 visible lines. */
9478 row_found = NULL;
9479 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
9480 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9481 {
9482 xassert (row->enabled_p);
9483 row_found = row;
9484 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
9485 break;
9486 ++row;
9487 }
9488
9489 return row_found;
9490 }
9491
9492
9493 /* Return the last row in the current matrix of W that is not affected
9494 by changes at the start of current_buffer that occurred since the
9495 last time W was redisplayed. Value is null if no such row exists.
9496
9497 The global variable beg_unchanged has to contain the number of
9498 bytes unchanged at the start of current_buffer. BEG +
9499 beg_unchanged is the buffer position of the first changed byte in
9500 current_buffer. Characters at positions < BEG + beg_unchanged are
9501 at the same buffer positions as they were when the current matrix
9502 was built. */
9503
9504 static struct glyph_row *
9505 get_last_unchanged_at_beg_row (w)
9506 struct window *w;
9507 {
9508 int first_changed_pos = BEG + BEG_UNCHANGED;
9509 struct glyph_row *row;
9510 struct glyph_row *row_found = NULL;
9511 int yb = window_text_bottom_y (w);
9512
9513 /* Find the last row displaying unchanged text. */
9514 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9515 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
9516 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
9517 {
9518 if (/* If row ends before first_changed_pos, it is unchanged,
9519 except in some case. */
9520 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
9521 /* When row ends in ZV and we write at ZV it is not
9522 unchanged. */
9523 && !row->ends_at_zv_p
9524 /* When first_changed_pos is the end of a continued line,
9525 row is not unchanged because it may be no longer
9526 continued. */
9527 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
9528 && row->continued_p))
9529 row_found = row;
9530
9531 /* Stop if last visible row. */
9532 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
9533 break;
9534
9535 ++row;
9536 }
9537
9538 return row_found;
9539 }
9540
9541
9542 /* Find the first glyph row in the current matrix of W that is not
9543 affected by changes at the end of current_buffer since the last
9544 time the window was redisplayed. Return in *DELTA the number of
9545 chars by which buffer positions in unchanged text at the end of
9546 current_buffer must be adjusted. Return in *DELTA_BYTES the
9547 corresponding number of bytes. Value is null if no such row
9548 exists, i.e. all rows are affected by changes. */
9549
9550 static struct glyph_row *
9551 get_first_unchanged_at_end_row (w, delta, delta_bytes)
9552 struct window *w;
9553 int *delta, *delta_bytes;
9554 {
9555 struct glyph_row *row;
9556 struct glyph_row *row_found = NULL;
9557
9558 *delta = *delta_bytes = 0;
9559
9560 /* A value of window_end_pos >= end_unchanged means that the window
9561 end is in the range of changed text. If so, there is no
9562 unchanged row at the end of W's current matrix. */
9563 xassert (!NILP (w->window_end_valid));
9564 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
9565 return NULL;
9566
9567 /* Set row to the last row in W's current matrix displaying text. */
9568 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
9569
9570 /* If matrix is entirely empty, no unchanged row exists. */
9571 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9572 {
9573 /* The value of row is the last glyph row in the matrix having a
9574 meaningful buffer position in it. The end position of row
9575 corresponds to window_end_pos. This allows us to translate
9576 buffer positions in the current matrix to current buffer
9577 positions for characters not in changed text. */
9578 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
9579 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
9580 int last_unchanged_pos, last_unchanged_pos_old;
9581 struct glyph_row *first_text_row
9582 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9583
9584 *delta = Z - Z_old;
9585 *delta_bytes = Z_BYTE - Z_BYTE_old;
9586
9587 /* Set last_unchanged_pos to the buffer position of the last
9588 character in the buffer that has not been changed. Z is the
9589 index + 1 of the last byte in current_buffer, i.e. by
9590 subtracting end_unchanged we get the index of the last
9591 unchanged character, and we have to add BEG to get its buffer
9592 position. */
9593 last_unchanged_pos = Z - END_UNCHANGED + BEG;
9594 last_unchanged_pos_old = last_unchanged_pos - *delta;
9595
9596 /* Search backward from ROW for a row displaying a line that
9597 starts at a minimum position >= last_unchanged_pos_old. */
9598 while (row >= first_text_row)
9599 {
9600 xassert (row->enabled_p);
9601 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (row));
9602
9603 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
9604 row_found = row;
9605 --row;
9606 }
9607 }
9608
9609 xassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
9610 return row_found;
9611 }
9612
9613
9614 /* Make sure that glyph rows in the current matrix of window W
9615 reference the same glyph memory as corresponding rows in the
9616 frame's frame matrix. This function is called after scrolling W's
9617 current matrix on a terminal frame in try_window_id and
9618 try_window_reusing_current_matrix. */
9619
9620 static void
9621 sync_frame_with_window_matrix_rows (w)
9622 struct window *w;
9623 {
9624 struct frame *f = XFRAME (w->frame);
9625 struct glyph_row *window_row, *window_row_end, *frame_row;
9626
9627 /* Preconditions: W must be a leaf window and full-width. Its frame
9628 must have a frame matrix. */
9629 xassert (NILP (w->hchild) && NILP (w->vchild));
9630 xassert (WINDOW_FULL_WIDTH_P (w));
9631 xassert (!FRAME_WINDOW_P (f));
9632
9633 /* If W is a full-width window, glyph pointers in W's current matrix
9634 have, by definition, to be the same as glyph pointers in the
9635 corresponding frame matrix. */
9636 window_row = w->current_matrix->rows;
9637 window_row_end = window_row + w->current_matrix->nrows;
9638 frame_row = f->current_matrix->rows + XFASTINT (w->top);
9639 while (window_row < window_row_end)
9640 {
9641 int area;
9642 for (area = LEFT_MARGIN_AREA; area <= LAST_AREA; ++area)
9643 frame_row->glyphs[area] = window_row->glyphs[area];
9644 ++window_row, ++frame_row;
9645 }
9646 }
9647
9648
9649 /* Try to redisplay window W by reusing its existing display. W's
9650 current matrix must be up to date when this function is called,
9651 i.e. window_end_valid must not be nil.
9652
9653 Value is
9654
9655 1 if display has been updated
9656 0 if otherwise unsuccessful
9657 -1 if redisplay with same window start is known not to succeed
9658
9659 The following steps are performed:
9660
9661 1. Find the last row in the current matrix of W that is not
9662 affected by changes at the start of current_buffer. If no such row
9663 is found, give up.
9664
9665 2. Find the first row in W's current matrix that is not affected by
9666 changes at the end of current_buffer. Maybe there is no such row.
9667
9668 3. Display lines beginning with the row + 1 found in step 1 to the
9669 row found in step 2 or, if step 2 didn't find a row, to the end of
9670 the window.
9671
9672 4. If cursor is not known to appear on the window, give up.
9673
9674 5. If display stopped at the row found in step 2, scroll the
9675 display and current matrix as needed.
9676
9677 6. Maybe display some lines at the end of W, if we must. This can
9678 happen under various circumstances, like a partially visible line
9679 becoming fully visible, or because newly displayed lines are displayed
9680 in smaller font sizes.
9681
9682 7. Update W's window end information. */
9683
9684 /* Check that window end is what we expect it to be. */
9685
9686 static int
9687 try_window_id (w)
9688 struct window *w;
9689 {
9690 struct frame *f = XFRAME (w->frame);
9691 struct glyph_matrix *current_matrix = w->current_matrix;
9692 struct glyph_matrix *desired_matrix = w->desired_matrix;
9693 struct glyph_row *last_unchanged_at_beg_row;
9694 struct glyph_row *first_unchanged_at_end_row;
9695 struct glyph_row *row;
9696 struct glyph_row *bottom_row;
9697 int bottom_vpos;
9698 struct it it;
9699 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
9700 struct text_pos start_pos;
9701 struct run run;
9702 int first_unchanged_at_end_vpos = 0;
9703 struct glyph_row *last_text_row, *last_text_row_at_end;
9704 struct text_pos start;
9705
9706 SET_TEXT_POS_FROM_MARKER (start, w->start);
9707
9708 /* Check pre-conditions. Window end must be valid, otherwise
9709 the current matrix would not be up to date. */
9710 xassert (!NILP (w->window_end_valid));
9711 xassert (FRAME_WINDOW_P (XFRAME (w->frame))
9712 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)));
9713
9714 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
9715 only if buffer has really changed. The reason is that the gap is
9716 initially at Z for freshly visited files. The code below would
9717 set end_unchanged to 0 in that case. */
9718 if (MODIFF > SAVE_MODIFF)
9719 {
9720 if (GPT - BEG < BEG_UNCHANGED)
9721 BEG_UNCHANGED = GPT - BEG;
9722 if (Z - GPT < END_UNCHANGED)
9723 END_UNCHANGED = Z - GPT;
9724 }
9725
9726 /* If window starts after a line end, and the last change is in
9727 front of that newline, then changes don't affect the display.
9728 This case happens with stealth-fontification. */
9729 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
9730 if (CHARPOS (start) > BEGV
9731 && Z - END_UNCHANGED < CHARPOS (start) - 1
9732 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n'
9733 && PT < MATRIX_ROW_END_CHARPOS (row))
9734 {
9735 /* We have to update window end positions because the buffer's
9736 size has changed. */
9737 w->window_end_pos
9738 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
9739 w->window_end_bytepos
9740 = make_number (Z_BYTE - MATRIX_ROW_END_BYTEPOS (row));
9741 return 1;
9742 }
9743
9744 /* Return quickly if changes are all below what is displayed in the
9745 window, and if PT is in the window. */
9746 if (BEG_UNCHANGED > MATRIX_ROW_END_CHARPOS (row)
9747 && PT < MATRIX_ROW_END_CHARPOS (row))
9748 {
9749 /* We have to update window end positions because the buffer's
9750 size has changed. */
9751 w->window_end_pos
9752 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
9753 w->window_end_bytepos
9754 = make_number (Z_BYTE - MATRIX_ROW_END_BYTEPOS (row));
9755 return 1;
9756 }
9757
9758 /* Check that window start agrees with the start of the first glyph
9759 row in its current matrix. Check this after we know the window
9760 start is not in changed text, otherwise positions would not be
9761 comparable. */
9762 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9763 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
9764 return 0;
9765
9766 /* Compute the position at which we have to start displaying new
9767 lines. Some of the lines at the top of the window might be
9768 reusable because they are not displaying changed text. Find the
9769 last row in W's current matrix not affected by changes at the
9770 start of current_buffer. Value is null if changes start in the
9771 first line of window. */
9772 last_unchanged_at_beg_row = get_last_unchanged_at_beg_row (w);
9773 if (last_unchanged_at_beg_row)
9774 {
9775 init_to_row_end (&it, w, last_unchanged_at_beg_row);
9776 start_pos = it.current.pos;
9777
9778 /* Start displaying new lines in the desired matrix at the same
9779 vpos we would use in the current matrix, i.e. below
9780 last_unchanged_at_beg_row. */
9781 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
9782 current_matrix);
9783 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
9784 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
9785
9786 xassert (it.hpos == 0 && it.current_x == 0);
9787 }
9788 else
9789 {
9790 /* There are no reusable lines at the start of the window.
9791 Start displaying in the first line. */
9792 start_display (&it, w, start);
9793 start_pos = it.current.pos;
9794 }
9795
9796 /* Find the first row that is not affected by changes at the end of
9797 the buffer. Value will be null if there is no unchanged row, in
9798 which case we must redisplay to the end of the window. delta
9799 will be set to the value by which buffer positions beginning with
9800 first_unchanged_at_end_row have to be adjusted due to text
9801 changes. */
9802 first_unchanged_at_end_row
9803 = get_first_unchanged_at_end_row (w, &delta, &delta_bytes);
9804 IF_DEBUG (debug_delta = delta);
9805 IF_DEBUG (debug_delta_bytes = delta_bytes);
9806
9807 /* Set stop_pos to the buffer position up to which we will have to
9808 display new lines. If first_unchanged_at_end_row != NULL, this
9809 is the buffer position of the start of the line displayed in that
9810 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
9811 that we don't stop at a buffer position. */
9812 stop_pos = 0;
9813 if (first_unchanged_at_end_row)
9814 {
9815 xassert (last_unchanged_at_beg_row == NULL
9816 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
9817
9818 /* If this is a continuation line, move forward to the next one
9819 that isn't. Changes in lines above affect this line.
9820 Caution: this may move first_unchanged_at_end_row to a row
9821 not displaying text. */
9822 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
9823 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
9824 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
9825 < it.last_visible_y))
9826 ++first_unchanged_at_end_row;
9827
9828 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
9829 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
9830 >= it.last_visible_y))
9831 first_unchanged_at_end_row = NULL;
9832 else
9833 {
9834 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
9835 + delta);
9836 first_unchanged_at_end_vpos
9837 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
9838 xassert (stop_pos >= Z - END_UNCHANGED);
9839 }
9840 }
9841 else if (last_unchanged_at_beg_row == NULL)
9842 return 0;
9843
9844
9845 #if GLYPH_DEBUG
9846
9847 /* Either there is no unchanged row at the end, or the one we have
9848 now displays text. This is a necessary condition for the window
9849 end pos calculation at the end of this function. */
9850 xassert (first_unchanged_at_end_row == NULL
9851 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
9852
9853 debug_last_unchanged_at_beg_vpos
9854 = (last_unchanged_at_beg_row
9855 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
9856 : -1);
9857 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
9858
9859 #endif /* GLYPH_DEBUG != 0 */
9860
9861
9862 /* Display new lines. Set last_text_row to the last new line
9863 displayed which has text on it, i.e. might end up as being the
9864 line where the window_end_vpos is. */
9865 w->cursor.vpos = -1;
9866 last_text_row = NULL;
9867 overlay_arrow_seen = 0;
9868 while (it.current_y < it.last_visible_y
9869 && !fonts_changed_p
9870 && (first_unchanged_at_end_row == NULL
9871 || IT_CHARPOS (it) < stop_pos))
9872 {
9873 if (display_line (&it))
9874 last_text_row = it.glyph_row - 1;
9875 }
9876
9877 if (fonts_changed_p)
9878 return -1;
9879
9880
9881 /* Compute differences in buffer positions, y-positions etc. for
9882 lines reused at the bottom of the window. Compute what we can
9883 scroll. */
9884 if (first_unchanged_at_end_row
9885 /* No lines reused because we displayed everything up to the
9886 bottom of the window. */
9887 && it.current_y < it.last_visible_y)
9888 {
9889 dvpos = (it.vpos
9890 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
9891 current_matrix));
9892 dy = it.current_y - first_unchanged_at_end_row->y;
9893 run.current_y = first_unchanged_at_end_row->y;
9894 run.desired_y = run.current_y + dy;
9895 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
9896 }
9897 else
9898 {
9899 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
9900 first_unchanged_at_end_row = NULL;
9901 }
9902 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
9903
9904
9905 /* Find the cursor if not already found. We have to decide whether
9906 PT will appear on this window (it sometimes doesn't, but this is
9907 not a very frequent case.) This decision has to be made before
9908 the current matrix is altered. A value of cursor.vpos < 0 means
9909 that PT is either in one of the lines beginning at
9910 first_unchanged_at_end_row or below the window. Don't care for
9911 lines that might be displayed later at the window end; as
9912 mentioned, this is not a frequent case. */
9913 if (w->cursor.vpos < 0)
9914 {
9915 int last_y = min (it.last_visible_y, it.last_visible_y + dy);
9916
9917 /* Cursor in unchanged rows at the top? */
9918 if (PT < CHARPOS (start_pos)
9919 && last_unchanged_at_beg_row)
9920 {
9921 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9922 while (row <= last_unchanged_at_beg_row
9923 && MATRIX_ROW_END_CHARPOS (row) <= PT)
9924 ++row;
9925 xassert (row <= last_unchanged_at_beg_row);
9926 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9927 }
9928
9929 /* Start from first_unchanged_at_end_row looking for PT. */
9930 else if (first_unchanged_at_end_row)
9931 {
9932 row = first_unchanged_at_end_row;
9933
9934 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9935 {
9936 if (PT - delta >= MATRIX_ROW_START_CHARPOS (row)
9937 && PT - delta < MATRIX_ROW_END_CHARPOS (row))
9938 {
9939 set_cursor_from_row (w, row, w->current_matrix, delta,
9940 delta_bytes, dy, dvpos);
9941 break;
9942 }
9943 else if (MATRIX_ROW_BOTTOM_Y (row) >= last_y)
9944 break;
9945 ++row;
9946 }
9947
9948 /* If PT is at ZV, this is not in a line displaying text.
9949 Check that case. */
9950 if (w->cursor.vpos < 0
9951 && PT - delta == MATRIX_ROW_START_CHARPOS (row)
9952 && row->ends_at_zv_p)
9953 set_cursor_from_row (w, row, w->current_matrix, delta,
9954 delta_bytes, dy, dvpos);
9955 }
9956
9957 /* Give up if cursor was not found. */
9958 if (w->cursor.vpos < 0)
9959 {
9960 clear_glyph_matrix (w->desired_matrix);
9961 return -1;
9962 }
9963 }
9964
9965 /* Don't let the cursor end in the scroll margins. */
9966 {
9967 int this_scroll_margin, cursor_height;
9968
9969 this_scroll_margin = max (0, scroll_margin);
9970 this_scroll_margin = min (this_scroll_margin,
9971 XFASTINT (w->height) / 4);
9972 this_scroll_margin *= CANON_Y_UNIT (it.f);
9973 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
9974
9975 if ((w->cursor.y < this_scroll_margin
9976 && CHARPOS (start) > BEGV)
9977 /* Don't take scroll margin into account at the bottom because
9978 old redisplay didn't do it either. */
9979 || w->cursor.y + cursor_height > it.last_visible_y)
9980 {
9981 w->cursor.vpos = -1;
9982 clear_glyph_matrix (w->desired_matrix);
9983 return -1;
9984 }
9985 }
9986
9987 /* Scroll the display. Do it before changing the current matrix so
9988 that xterm.c doesn't get confused about where the cursor glyph is
9989 found. */
9990 if (dy)
9991 {
9992 update_begin (f);
9993
9994 if (FRAME_WINDOW_P (f))
9995 {
9996 rif->update_window_begin_hook (w);
9997 rif->scroll_run_hook (w, &run);
9998 rif->update_window_end_hook (w, 0);
9999 }
10000 else
10001 {
10002 /* Terminal frame. In this case, dvpos gives the number of
10003 lines to scroll by; dvpos < 0 means scroll up. */
10004 int first_unchanged_at_end_vpos
10005 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
10006 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
10007 int end = XFASTINT (w->top) + window_internal_height (w);
10008
10009 /* Perform the operation on the screen. */
10010 if (dvpos > 0)
10011 {
10012 /* Scroll last_unchanged_at_beg_row to the end of the
10013 window down dvpos lines. */
10014 set_terminal_window (end);
10015
10016 /* On dumb terminals delete dvpos lines at the end
10017 before inserting dvpos empty lines. */
10018 if (!scroll_region_ok)
10019 ins_del_lines (end - dvpos, -dvpos);
10020
10021 /* Insert dvpos empty lines in front of
10022 last_unchanged_at_beg_row. */
10023 ins_del_lines (from, dvpos);
10024 }
10025 else if (dvpos < 0)
10026 {
10027 /* Scroll up last_unchanged_at_beg_vpos to the end of
10028 the window to last_unchanged_at_beg_vpos - |dvpos|. */
10029 set_terminal_window (end);
10030
10031 /* Delete dvpos lines in front of
10032 last_unchanged_at_beg_vpos. ins_del_lines will set
10033 the cursor to the given vpos and emit |dvpos| delete
10034 line sequences. */
10035 ins_del_lines (from + dvpos, dvpos);
10036
10037 /* On a dumb terminal insert dvpos empty lines at the
10038 end. */
10039 if (!scroll_region_ok)
10040 ins_del_lines (end + dvpos, -dvpos);
10041 }
10042
10043 set_terminal_window (0);
10044 }
10045
10046 update_end (f);
10047 }
10048
10049 /* Shift reused rows of the current matrix to the right position.
10050 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
10051 text. */
10052 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
10053 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
10054 if (dvpos < 0)
10055 {
10056 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
10057 bottom_vpos, dvpos);
10058 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
10059 bottom_vpos, 0);
10060 }
10061 else if (dvpos > 0)
10062 {
10063 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
10064 bottom_vpos, dvpos);
10065 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
10066 first_unchanged_at_end_vpos + dvpos, 0);
10067 }
10068
10069 /* For frame-based redisplay, make sure that current frame and window
10070 matrix are in sync with respect to glyph memory. */
10071 if (!FRAME_WINDOW_P (f))
10072 sync_frame_with_window_matrix_rows (w);
10073
10074 /* Adjust buffer positions in reused rows. */
10075 if (delta)
10076 increment_glyph_matrix_buffer_positions (current_matrix,
10077 first_unchanged_at_end_vpos + dvpos,
10078 bottom_vpos, delta, delta_bytes);
10079
10080 /* Adjust Y positions. */
10081 if (dy)
10082 shift_glyph_matrix (w, current_matrix,
10083 first_unchanged_at_end_vpos + dvpos,
10084 bottom_vpos, dy);
10085
10086 if (first_unchanged_at_end_row)
10087 first_unchanged_at_end_row += dvpos;
10088
10089 /* If scrolling up, there may be some lines to display at the end of
10090 the window. */
10091 last_text_row_at_end = NULL;
10092 if (dy < 0)
10093 {
10094 /* Set last_row to the glyph row in the current matrix where the
10095 window end line is found. It has been moved up or down in
10096 the matrix by dvpos. */
10097 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
10098 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
10099
10100 /* If last_row is the window end line, it should display text. */
10101 xassert (last_row->displays_text_p);
10102
10103 /* If window end line was partially visible before, begin
10104 displaying at that line. Otherwise begin displaying with the
10105 line following it. */
10106 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
10107 {
10108 init_to_row_start (&it, w, last_row);
10109 it.vpos = last_vpos;
10110 it.current_y = last_row->y;
10111 }
10112 else
10113 {
10114 init_to_row_end (&it, w, last_row);
10115 it.vpos = 1 + last_vpos;
10116 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
10117 ++last_row;
10118 }
10119
10120 /* We may start in a continuation line. If so, we have to get
10121 the right continuation_lines_width and current_x. */
10122 it.continuation_lines_width = last_row->continuation_lines_width;
10123 it.hpos = it.current_x = 0;
10124
10125 /* Display the rest of the lines at the window end. */
10126 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
10127 while (it.current_y < it.last_visible_y
10128 && !fonts_changed_p)
10129 {
10130 /* Is it always sure that the display agrees with lines in
10131 the current matrix? I don't think so, so we mark rows
10132 displayed invalid in the current matrix by setting their
10133 enabled_p flag to zero. */
10134 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
10135 if (display_line (&it))
10136 last_text_row_at_end = it.glyph_row - 1;
10137 }
10138 }
10139
10140 /* Update window_end_pos and window_end_vpos. */
10141 if (first_unchanged_at_end_row
10142 && first_unchanged_at_end_row->y < it.last_visible_y
10143 && !last_text_row_at_end)
10144 {
10145 /* Window end line if one of the preserved rows from the current
10146 matrix. Set row to the last row displaying text in current
10147 matrix starting at first_unchanged_at_end_row, after
10148 scrolling. */
10149 xassert (first_unchanged_at_end_row->displays_text_p);
10150 row = find_last_row_displaying_text (w->current_matrix, &it,
10151 first_unchanged_at_end_row);
10152 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
10153
10154 XSETFASTINT (w->window_end_pos, Z - MATRIX_ROW_END_CHARPOS (row));
10155 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
10156 XSETFASTINT (w->window_end_vpos,
10157 MATRIX_ROW_VPOS (row, w->current_matrix));
10158 }
10159 else if (last_text_row_at_end)
10160 {
10161 XSETFASTINT (w->window_end_pos,
10162 Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
10163 w->window_end_bytepos
10164 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
10165 XSETFASTINT (w->window_end_vpos,
10166 MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
10167 }
10168 else if (last_text_row)
10169 {
10170 /* We have displayed either to the end of the window or at the
10171 end of the window, i.e. the last row with text is to be found
10172 in the desired matrix. */
10173 XSETFASTINT (w->window_end_pos,
10174 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10175 w->window_end_bytepos
10176 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10177 XSETFASTINT (w->window_end_vpos,
10178 MATRIX_ROW_VPOS (last_text_row, desired_matrix));
10179 }
10180 else if (first_unchanged_at_end_row == NULL
10181 && last_text_row == NULL
10182 && last_text_row_at_end == NULL)
10183 {
10184 /* Displayed to end of window, but no line containing text was
10185 displayed. Lines were deleted at the end of the window. */
10186 int vpos;
10187 int top_line_p = WINDOW_WANTS_TOP_LINE_P (w) ? 1 : 0;
10188
10189 for (vpos = XFASTINT (w->window_end_vpos); vpos > 0; --vpos)
10190 if ((w->desired_matrix->rows[vpos + top_line_p].enabled_p
10191 && w->desired_matrix->rows[vpos + top_line_p].displays_text_p)
10192 || (!w->desired_matrix->rows[vpos + top_line_p].enabled_p
10193 && w->current_matrix->rows[vpos + top_line_p].displays_text_p))
10194 break;
10195
10196 w->window_end_vpos = make_number (vpos);
10197 }
10198 else
10199 abort ();
10200
10201 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
10202 debug_end_vpos = XFASTINT (w->window_end_vpos));
10203
10204 /* Record that display has not been completed. */
10205 w->window_end_valid = Qnil;
10206 w->desired_matrix->no_scrolling_p = 1;
10207 return 1;
10208 }
10209
10210
10211 \f
10212 /***********************************************************************
10213 More debugging support
10214 ***********************************************************************/
10215
10216 #if GLYPH_DEBUG
10217
10218 void dump_glyph_row P_ ((struct glyph_matrix *, int, int));
10219 static void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
10220
10221
10222 /* Dump the contents of glyph matrix MATRIX on stderr. If
10223 WITH_GLYPHS_P is non-zero, dump glyph contents as well. */
10224
10225 void
10226 dump_glyph_matrix (matrix, with_glyphs_p)
10227 struct glyph_matrix *matrix;
10228 int with_glyphs_p;
10229 {
10230 int i;
10231 for (i = 0; i < matrix->nrows; ++i)
10232 dump_glyph_row (matrix, i, with_glyphs_p);
10233 }
10234
10235
10236 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
10237 WITH_GLYPH_SP non-zero means dump glyph contents, too. */
10238
10239 void
10240 dump_glyph_row (matrix, vpos, with_glyphs_p)
10241 struct glyph_matrix *matrix;
10242 int vpos, with_glyphs_p;
10243 {
10244 struct glyph_row *row;
10245
10246 if (vpos < 0 || vpos >= matrix->nrows)
10247 return;
10248
10249 row = MATRIX_ROW (matrix, vpos);
10250
10251 fprintf (stderr, "Row Start End Used oEI><O\\CTZF X Y W\n");
10252 fprintf (stderr, "=============================================\n");
10253
10254 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",
10255 row - matrix->rows,
10256 MATRIX_ROW_START_CHARPOS (row),
10257 MATRIX_ROW_END_CHARPOS (row),
10258 row->used[TEXT_AREA],
10259 row->contains_overlapping_glyphs_p,
10260 row->enabled_p,
10261 row->inverse_p,
10262 row->truncated_on_left_p,
10263 row->truncated_on_right_p,
10264 row->overlay_arrow_p,
10265 row->continued_p,
10266 MATRIX_ROW_CONTINUATION_LINE_P (row),
10267 row->displays_text_p,
10268 row->ends_at_zv_p,
10269 row->fill_line_p,
10270 row->x,
10271 row->y,
10272 row->pixel_width);
10273 fprintf (stderr, "%9d %5d\n", row->start.overlay_string_index,
10274 row->end.overlay_string_index);
10275 fprintf (stderr, "%9d %5d\n",
10276 CHARPOS (row->start.string_pos),
10277 CHARPOS (row->end.string_pos));
10278 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
10279 row->end.dpvec_index);
10280
10281 if (with_glyphs_p)
10282 {
10283 struct glyph *glyph, *glyph_end;
10284 int prev_had_glyphs_p;
10285
10286 glyph = row->glyphs[TEXT_AREA];
10287 glyph_end = glyph + row->used[TEXT_AREA];
10288
10289 /* Glyph for a line end in text. */
10290 if (glyph == glyph_end && glyph->charpos > 0)
10291 ++glyph_end;
10292
10293 if (glyph < glyph_end)
10294 {
10295 fprintf (stderr, " Glyph Type Pos W Code C Face LR\n");
10296 prev_had_glyphs_p = 1;
10297 }
10298 else
10299 prev_had_glyphs_p = 0;
10300
10301 while (glyph < glyph_end)
10302 {
10303 if (glyph->type == CHAR_GLYPH)
10304 {
10305 fprintf (stderr,
10306 " %5d %4c %6d %3d 0x%05x %c %4d %1.1d%1.1d\n",
10307 glyph - row->glyphs[TEXT_AREA],
10308 'C',
10309 glyph->charpos,
10310 glyph->pixel_width,
10311 glyph->u.ch.code,
10312 (glyph->u.ch.code < 0x80 && glyph->u.ch.code >= ' '
10313 ? glyph->u.ch.code
10314 : '.'),
10315 glyph->u.ch.face_id,
10316 glyph->left_box_line_p,
10317 glyph->right_box_line_p);
10318 }
10319 else if (glyph->type == STRETCH_GLYPH)
10320 {
10321 fprintf (stderr,
10322 " %5d %4c %6d %3d 0x%05x %c %4d %1.1d%1.1d\n",
10323 glyph - row->glyphs[TEXT_AREA],
10324 'S',
10325 glyph->charpos,
10326 glyph->pixel_width,
10327 0,
10328 '.',
10329 glyph->u.stretch.face_id,
10330 glyph->left_box_line_p,
10331 glyph->right_box_line_p);
10332 }
10333 else if (glyph->type == IMAGE_GLYPH)
10334 {
10335 fprintf (stderr,
10336 " %5d %4c %6d %3d 0x%05x %c %4d %1.1d%1.1d\n",
10337 glyph - row->glyphs[TEXT_AREA],
10338 'I',
10339 glyph->charpos,
10340 glyph->pixel_width,
10341 glyph->u.img.id,
10342 '.',
10343 glyph->u.img.face_id,
10344 glyph->left_box_line_p,
10345 glyph->right_box_line_p);
10346 }
10347 ++glyph;
10348 }
10349 }
10350 }
10351
10352
10353 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
10354 Sdump_glyph_matrix, 0, 1, "p",
10355 "Dump the current matrix of the selected window to stderr.\n\
10356 Shows contents of glyph row structures. With non-nil optional\n\
10357 parameter WITH-GLYPHS-P, dump glyphs as well.")
10358 (with_glyphs_p)
10359 {
10360 struct window *w = XWINDOW (selected_window);
10361 struct buffer *buffer = XBUFFER (w->buffer);
10362
10363 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
10364 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
10365 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
10366 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
10367 fprintf (stderr, "=============================================\n");
10368 dump_glyph_matrix (w->current_matrix, !NILP (with_glyphs_p));
10369 return Qnil;
10370 }
10371
10372
10373 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 1, "",
10374 "Dump glyph row ROW to stderr.")
10375 (row)
10376 Lisp_Object row;
10377 {
10378 CHECK_NUMBER (row, 0);
10379 dump_glyph_row (XWINDOW (selected_window)->current_matrix, XINT (row), 1);
10380 return Qnil;
10381 }
10382
10383
10384 DEFUN ("dump-toolbar-row", Fdump_toolbar_row, Sdump_toolbar_row,
10385 0, 0, "", "")
10386 ()
10387 {
10388 struct glyph_matrix *m = (XWINDOW (selected_frame->toolbar_window)
10389 ->current_matrix);
10390 dump_glyph_row (m, 0, 1);
10391 return Qnil;
10392 }
10393
10394
10395 DEFUN ("trace-redisplay-toggle", Ftrace_redisplay_toggle,
10396 Strace_redisplay_toggle, 0, 0, "",
10397 "Toggle tracing of redisplay.")
10398 ()
10399 {
10400 trace_redisplay_p = !trace_redisplay_p;
10401 return Qnil;
10402 }
10403
10404
10405 #endif /* GLYPH_DEBUG */
10406
10407
10408 \f
10409 /***********************************************************************
10410 Building Desired Matrix Rows
10411 ***********************************************************************/
10412
10413 /* Return a temporary glyph row holding the glyphs of an overlay
10414 arrow. Only used for non-window-redisplay windows. */
10415
10416 static struct glyph_row *
10417 get_overlay_arrow_glyph_row (w)
10418 struct window *w;
10419 {
10420 struct frame *f = XFRAME (WINDOW_FRAME (w));
10421 struct buffer *buffer = XBUFFER (w->buffer);
10422 struct buffer *old = current_buffer;
10423 unsigned char *arrow_string = XSTRING (Voverlay_arrow_string)->data;
10424 int arrow_len = XSTRING (Voverlay_arrow_string)->size;
10425 unsigned char *arrow_end = arrow_string + arrow_len;
10426 unsigned char *p;
10427 struct it it;
10428 int multibyte_p;
10429 int n_glyphs_before;
10430
10431 set_buffer_temp (buffer);
10432 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
10433 it.glyph_row->used[TEXT_AREA] = 0;
10434 SET_TEXT_POS (it.position, 0, 0);
10435
10436 multibyte_p = !NILP (buffer->enable_multibyte_characters);
10437 p = arrow_string;
10438 while (p < arrow_end)
10439 {
10440 Lisp_Object face, ilisp;
10441
10442 /* Get the next character. */
10443 if (multibyte_p)
10444 it.c = string_char_and_length (p, arrow_len, &it.len);
10445 else
10446 it.c = *p, it.len = 1;
10447 p += it.len;
10448
10449 /* Get its face. */
10450 XSETFASTINT (ilisp, p - arrow_string);
10451 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
10452 it.face_id = compute_char_face (f, it.c, face);
10453
10454 /* Compute its width, get its glyphs. */
10455 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
10456 SET_TEXT_POS (it.position, -1, -1);
10457 PRODUCE_GLYPHS (&it);
10458
10459 /* If this character doesn't fit any more in the line, we have
10460 to remove some glyphs. */
10461 if (it.current_x > it.last_visible_x)
10462 {
10463 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
10464 break;
10465 }
10466 }
10467
10468 set_buffer_temp (old);
10469 return it.glyph_row;
10470 }
10471
10472
10473 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
10474 glyphs are only inserted for terminal frames since we can't really
10475 win with truncation glyphs when partially visible glyphs are
10476 involved. Which glyphs to insert is determined by
10477 produce_special_glyphs. */
10478
10479 static void
10480 insert_left_trunc_glyphs (it)
10481 struct it *it;
10482 {
10483 struct it truncate_it;
10484 struct glyph *from, *end, *to, *toend;
10485
10486 xassert (!FRAME_WINDOW_P (it->f));
10487
10488 /* Get the truncation glyphs. */
10489 truncate_it = *it;
10490 truncate_it.charset = -1;
10491 truncate_it.current_x = 0;
10492 truncate_it.face_id = DEFAULT_FACE_ID;
10493 truncate_it.glyph_row = &scratch_glyph_row;
10494 truncate_it.glyph_row->used[TEXT_AREA] = 0;
10495 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
10496 truncate_it.object = 0;
10497 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
10498
10499 /* Overwrite glyphs from IT with truncation glyphs. */
10500 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
10501 end = from + truncate_it.glyph_row->used[TEXT_AREA];
10502 to = it->glyph_row->glyphs[TEXT_AREA];
10503 toend = to + it->glyph_row->used[TEXT_AREA];
10504
10505 while (from < end)
10506 *to++ = *from++;
10507
10508 /* There may be padding glyphs left over. Remove them. */
10509 from = to;
10510 while (from < toend && CHAR_GLYPH_PADDING_P (*from))
10511 ++from;
10512 while (from < toend)
10513 *to++ = *from++;
10514
10515 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
10516 }
10517
10518
10519 /* Compute the pixel height and width of IT->glyph_row.
10520
10521 Most of the time, ascent and height of a display line will be equal
10522 to the max_ascent and max_height values of the display iterator
10523 structure. This is not the case if
10524
10525 1. We hit ZV without displaying anything. In this case, max_ascent
10526 and max_height will be zero.
10527
10528 2. We have some glyphs that don't contribute to the line height.
10529 (The glyph row flag contributes_to_line_height_p is for future
10530 pixmap extensions).
10531
10532 The first case is easily covered by using default values because in
10533 these cases, the line height does not really matter, except that it
10534 must not be zero. */
10535
10536 static void
10537 compute_line_metrics (it)
10538 struct it *it;
10539 {
10540 struct glyph_row *row = it->glyph_row;
10541 int area, i;
10542
10543 if (FRAME_WINDOW_P (it->f))
10544 {
10545 int i, top_line_height;
10546
10547 /* The line may consist of one space only, that was added to
10548 place the cursor on it. If so, the row's height hasn't been
10549 computed yet. */
10550 if (row->height == 0)
10551 {
10552 if (it->max_ascent + it->max_descent == 0)
10553 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
10554 row->ascent = it->max_ascent;
10555 row->height = it->max_ascent + it->max_descent;
10556 row->phys_ascent = it->max_phys_ascent;
10557 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
10558 }
10559
10560 /* Compute the width of this line. */
10561 row->pixel_width = row->x;
10562 for (i = 0; i < row->used[TEXT_AREA]; ++i)
10563 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
10564
10565 xassert (row->pixel_width >= 0);
10566 xassert (row->ascent >= 0 && row->height > 0);
10567
10568 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
10569 || MATRIX_ROW_OVERLAPS_PRED_P (row));
10570
10571 /* If first line's physical ascent is larger than its logical
10572 ascent, use the physical ascent, and make the row taller.
10573 This makes accented characters fully visible. */
10574 if (row == it->w->desired_matrix->rows
10575 && row->phys_ascent > row->ascent)
10576 {
10577 row->height += row->phys_ascent - row->ascent;
10578 row->ascent = row->phys_ascent;
10579 }
10580
10581 /* Compute how much of the line is visible. */
10582 row->visible_height = row->height;
10583
10584 top_line_height = WINDOW_DISPLAY_TOP_LINE_HEIGHT (it->w);
10585 if (row->y < top_line_height)
10586 row->visible_height -= top_line_height - row->y;
10587 else
10588 {
10589 int max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
10590 if (row->y + row->height > max_y)
10591 row->visible_height -= row->y + row->height - max_y;
10592 }
10593 }
10594 else
10595 {
10596 row->pixel_width = row->used[TEXT_AREA];
10597 row->ascent = row->phys_ascent = 0;
10598 row->height = row->phys_height = row->visible_height = 1;
10599 }
10600
10601 /* Compute a hash code for this row. */
10602 row->hash = 0;
10603 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
10604 for (i = 0; i < row->used[area]; ++i)
10605 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
10606 + row->glyphs[area][i].u.val
10607 + (row->glyphs[area][i].type << 2));
10608
10609 it->max_ascent = it->max_descent = 0;
10610 it->max_phys_ascent = it->max_phys_descent = 0;
10611 }
10612
10613
10614 /* Append one space to the glyph row of iterator IT if doing a
10615 window-based redisplay. DEFAULT_FACE_P non-zero means let the
10616 space have the default face, otherwise let it have the same face as
10617 IT->face_id.
10618
10619 This function is called to make sure that there is always one glyph
10620 at the end of a glyph row that the cursor can be set on under
10621 window-systems. (If there weren't such a glyph we would not know
10622 how wide and tall a box cursor should be displayed).
10623
10624 At the same time this space let's a nicely handle clearing to the
10625 end of the line if the row ends in italic text. */
10626
10627 static void
10628 append_space (it, default_face_p)
10629 struct it *it;
10630 int default_face_p;
10631 {
10632 if (FRAME_WINDOW_P (it->f))
10633 {
10634 int n = it->glyph_row->used[TEXT_AREA];
10635
10636 if (it->glyph_row->glyphs[TEXT_AREA] + n
10637 < it->glyph_row->glyphs[1 + TEXT_AREA])
10638 {
10639 /* Save some values that must not be changed. */
10640 int saved_x = it->current_x;
10641 struct text_pos saved_pos;
10642 int saved_what = it->what;
10643 int saved_face_id = it->face_id;
10644 int saved_charset = it->charset;
10645 Lisp_Object saved_object;
10646
10647 saved_object = it->object;
10648 saved_pos = it->position;
10649
10650 it->what = IT_CHARACTER;
10651 bzero (&it->position, sizeof it->position);
10652 it->object = 0;
10653 it->c = ' ';
10654 it->len = 1;
10655 it->charset = CHARSET_ASCII;
10656
10657 if (default_face_p)
10658 it->face_id = DEFAULT_FACE_ID;
10659 if (it->multibyte_p)
10660 it->face_id = FACE_FOR_CHARSET (it->f, it->face_id, CHARSET_ASCII);
10661 else
10662 it->face_id = FACE_FOR_CHARSET (it->f, it->face_id, -1);
10663
10664 PRODUCE_GLYPHS (it);
10665
10666 it->current_x = saved_x;
10667 it->object = saved_object;
10668 it->position = saved_pos;
10669 it->what = saved_what;
10670 it->face_id = saved_face_id;
10671 it->charset = saved_charset;
10672 }
10673 }
10674 }
10675
10676
10677 /* Extend the face of the last glyph in the text area of IT->glyph_row
10678 to the end of the display line. Called from display_line.
10679 If the glyph row is empty, add a space glyph to it so that we
10680 know the face to draw. Set the glyph row flag fill_line_p. */
10681
10682 static void
10683 extend_face_to_end_of_line (it)
10684 struct it *it;
10685 {
10686 struct face *face;
10687 struct frame *f = it->f;
10688
10689 /* If line is already filled, do nothing. */
10690 if (it->current_x >= it->last_visible_x)
10691 return;
10692
10693 /* Face extension extends the background and box of IT->face_id
10694 to the end of the line. If the background equals the background
10695 of the frame, we haven't to do anything. */
10696 face = FACE_FROM_ID (f, it->face_id);
10697 if (FRAME_WINDOW_P (f)
10698 && face->box == FACE_NO_BOX
10699 && face->background == FRAME_BACKGROUND_PIXEL (f)
10700 && !face->stipple)
10701 return;
10702
10703 /* Set the glyph row flag indicating that the face of the last glyph
10704 in the text area has to be drawn to the end of the text area. */
10705 it->glyph_row->fill_line_p = 1;
10706
10707 /* If current charset of IT is not ASCII, make sure we have the
10708 ASCII face. This will be automatically undone the next time
10709 get_next_display_element returns a character from a different
10710 charset. Note that the charset will always be ASCII in unibyte
10711 text. */
10712 if (it->charset != CHARSET_ASCII)
10713 {
10714 it->charset = CHARSET_ASCII;
10715 it->face_id = FACE_FOR_CHARSET (f, it->face_id, CHARSET_ASCII);
10716 }
10717
10718 if (FRAME_WINDOW_P (f))
10719 {
10720 /* If the row is empty, add a space with the current face of IT,
10721 so that we know which face to draw. */
10722 if (it->glyph_row->used[TEXT_AREA] == 0)
10723 {
10724 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
10725 it->glyph_row->glyphs[TEXT_AREA][0].u.ch.face_id = it->face_id;
10726 it->glyph_row->used[TEXT_AREA] = 1;
10727 }
10728 }
10729 else
10730 {
10731 /* Save some values that must not be changed. */
10732 int saved_x = it->current_x;
10733 struct text_pos saved_pos;
10734 Lisp_Object saved_object;
10735 int saved_what = it->what;
10736
10737 saved_object = it->object;
10738 saved_pos = it->position;
10739
10740 it->what = IT_CHARACTER;
10741 bzero (&it->position, sizeof it->position);
10742 it->object = 0;
10743 it->c = ' ';
10744 it->len = 1;
10745
10746 PRODUCE_GLYPHS (it);
10747
10748 while (it->current_x <= it->last_visible_x)
10749 PRODUCE_GLYPHS (it);
10750
10751 /* Don't count these blanks really. It would let us insert a left
10752 truncation glyph below and make us set the cursor on them, maybe. */
10753 it->current_x = saved_x;
10754 it->object = saved_object;
10755 it->position = saved_pos;
10756 it->what = saved_what;
10757 }
10758 }
10759
10760
10761 /* Value is non-zero if text starting at CHARPOS in current_buffer is
10762 trailing whitespace. */
10763
10764 static int
10765 trailing_whitespace_p (charpos)
10766 int charpos;
10767 {
10768 int bytepos = CHAR_TO_BYTE (charpos);
10769 int c = 0;
10770
10771 while (bytepos < ZV_BYTE
10772 && (c = FETCH_CHAR (bytepos),
10773 c == ' ' || c == '\t'))
10774 ++bytepos;
10775
10776 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
10777 {
10778 if (bytepos != PT_BYTE)
10779 return 1;
10780 }
10781 return 0;
10782 }
10783
10784
10785 /* Highlight trailing whitespace, if any, in ROW. */
10786
10787 void
10788 highlight_trailing_whitespace (f, row)
10789 struct frame *f;
10790 struct glyph_row *row;
10791 {
10792 int used = row->used[TEXT_AREA];
10793
10794 if (used)
10795 {
10796 struct glyph *start = row->glyphs[TEXT_AREA];
10797 struct glyph *glyph = start + used - 1;
10798
10799 /* Skip over the space glyph inserted to display the
10800 cursor at the end of a line. */
10801 if (glyph->type == CHAR_GLYPH
10802 && glyph->u.ch.code == ' '
10803 && glyph->object == 0)
10804 --glyph;
10805
10806 /* If last glyph is a space or stretch, and it's trailing
10807 whitespace, set the face of all trailing whitespace glyphs in
10808 IT->glyph_row to `trailing-whitespace'. */
10809 if (glyph >= start
10810 && BUFFERP (glyph->object)
10811 && (glyph->type == STRETCH_GLYPH
10812 || (glyph->type == CHAR_GLYPH
10813 && glyph->u.ch.code == ' '))
10814 && trailing_whitespace_p (glyph->charpos))
10815 {
10816 int face_id = lookup_named_face (f, Qtrailing_whitespace,
10817 CHARSET_ASCII);
10818
10819 while (glyph >= start
10820 && BUFFERP (glyph->object)
10821 && (glyph->type == STRETCH_GLYPH
10822 || (glyph->type == CHAR_GLYPH
10823 && glyph->u.ch.code == ' ')))
10824 {
10825 if (glyph->type == STRETCH_GLYPH)
10826 glyph->u.stretch.face_id = face_id;
10827 else
10828 glyph->u.ch.face_id = face_id;
10829 --glyph;
10830 }
10831 }
10832 }
10833 }
10834
10835
10836 /* Construct the glyph row IT->glyph_row in the desired matrix of
10837 IT->w from text at the current position of IT. See dispextern.h
10838 for an overview of struct it. Value is non-zero if
10839 IT->glyph_row displays text, as opposed to a line displaying ZV
10840 only. */
10841
10842 static int
10843 display_line (it)
10844 struct it *it;
10845 {
10846 struct glyph_row *row = it->glyph_row;
10847
10848 /* We always start displaying at hpos zero even if hscrolled. */
10849 xassert (it->hpos == 0 && it->current_x == 0);
10850
10851 /* We must not display in a row that's not a text row. */
10852 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
10853 < it->w->desired_matrix->nrows);
10854
10855 /* Is IT->w showing the region? */
10856 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
10857
10858 /* Clear the result glyph row and enable it. */
10859 prepare_desired_row (row);
10860
10861 row->y = it->current_y;
10862 row->start = it->current;
10863 row->continuation_lines_width = it->continuation_lines_width;
10864 row->displays_text_p = 1;
10865
10866 /* Arrange the overlays nicely for our purposes. Usually, we call
10867 display_line on only one line at a time, in which case this
10868 can't really hurt too much, or we call it on lines which appear
10869 one after another in the buffer, in which case all calls to
10870 recenter_overlay_lists but the first will be pretty cheap. */
10871 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
10872
10873 /* Move over display elements that are not visible because we are
10874 hscrolled. This may stop at an x-position < IT->first_visible_x
10875 if the first glyph is partially visible or if we hit a line end. */
10876 if (it->current_x < it->first_visible_x)
10877 move_it_in_display_line_to (it, ZV, it->first_visible_x,
10878 MOVE_TO_POS | MOVE_TO_X);
10879
10880 /* Get the initial row height. This is either the height of the
10881 text hscrolled, if there is any, or zero. */
10882 row->ascent = it->max_ascent;
10883 row->height = it->max_ascent + it->max_descent;
10884 row->phys_ascent = it->max_phys_ascent;
10885 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
10886
10887 /* Loop generating characters. The loop is left with IT on the next
10888 character to display. */
10889 while (1)
10890 {
10891 int n_glyphs_before, hpos_before, x_before;
10892 int x, i, nglyphs;
10893
10894 /* Retrieve the next thing to display. Value is zero if end of
10895 buffer reached. */
10896 if (!get_next_display_element (it))
10897 {
10898 /* Maybe add a space at the end of this line that is used to
10899 display the cursor there under X. */
10900 append_space (it, 1);
10901
10902 /* The position -1 below indicates a blank line not
10903 corresponding to any text, as opposed to an empty line
10904 corresponding to a line end. */
10905 if (row->used[TEXT_AREA] <= 1)
10906 {
10907 row->glyphs[TEXT_AREA]->charpos = -1;
10908 row->displays_text_p = 0;
10909
10910 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines))
10911 row->indicate_empty_line_p = 1;
10912 }
10913
10914 it->continuation_lines_width = 0;
10915 row->ends_at_zv_p = 1;
10916 break;
10917 }
10918
10919 /* Now, get the metrics of what we want to display. This also
10920 generates glyphs in `row' (which is IT->glyph_row). */
10921 n_glyphs_before = row->used[TEXT_AREA];
10922 x = it->current_x;
10923 PRODUCE_GLYPHS (it);
10924
10925 /* If this display element was in marginal areas, continue with
10926 the next one. */
10927 if (it->area != TEXT_AREA)
10928 {
10929 row->ascent = max (row->ascent, it->max_ascent);
10930 row->height = max (row->height, it->max_ascent + it->max_descent);
10931 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
10932 row->phys_height = max (row->phys_height,
10933 it->max_phys_ascent + it->max_phys_descent);
10934 set_iterator_to_next (it);
10935 continue;
10936 }
10937
10938 /* Does the display element fit on the line? If we truncate
10939 lines, we should draw past the right edge of the window. If
10940 we don't truncate, we want to stop so that we can display the
10941 continuation glyph before the right margin. If lines are
10942 continued, there are two possible strategies for characters
10943 resulting in more than 1 glyph (e.g. tabs): Display as many
10944 glyphs as possible in this line and leave the rest for the
10945 continuation line, or display the whole element in the next
10946 line. Original redisplay did the former, so we do it also. */
10947 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
10948 hpos_before = it->hpos;
10949 x_before = x;
10950
10951 if (nglyphs == 1
10952 && it->current_x < it->last_visible_x)
10953 {
10954 ++it->hpos;
10955 row->ascent = max (row->ascent, it->max_ascent);
10956 row->height = max (row->height, it->max_ascent + it->max_descent);
10957 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
10958 row->phys_height = max (row->phys_height,
10959 it->max_phys_ascent + it->max_phys_descent);
10960 if (it->current_x - it->pixel_width < it->first_visible_x)
10961 row->x = x - it->first_visible_x;
10962 }
10963 else
10964 {
10965 int new_x;
10966 struct glyph *glyph;
10967
10968 for (i = 0; i < nglyphs; ++i, x = new_x)
10969 {
10970 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
10971 new_x = x + glyph->pixel_width;
10972
10973 if (/* Lines are continued. */
10974 !it->truncate_lines_p
10975 && (/* Glyph doesn't fit on the line. */
10976 new_x > it->last_visible_x
10977 /* Or it fits exactly on a window system frame. */
10978 || (new_x == it->last_visible_x
10979 && FRAME_WINDOW_P (it->f))))
10980 {
10981 /* End of a continued line. */
10982
10983 if (it->hpos == 0
10984 || (new_x == it->last_visible_x
10985 && FRAME_WINDOW_P (it->f)))
10986 {
10987 /* Current glyph fits exactly on the line. We
10988 must continue the line because we can't draw
10989 the cursor after the glyph. */
10990 row->continued_p = 1;
10991 it->current_x = new_x;
10992 it->continuation_lines_width += new_x;
10993 ++it->hpos;
10994 if (i == nglyphs - 1)
10995 set_iterator_to_next (it);
10996 }
10997 else
10998 {
10999 /* Display element draws past the right edge of
11000 the window. Restore positions to values
11001 before the element. The next line starts
11002 with current_x before the glyph that could
11003 not be displayed, so that TAB works right. */
11004 row->used[TEXT_AREA] = n_glyphs_before + i;
11005
11006 /* Display continuation glyphs. */
11007 if (!FRAME_WINDOW_P (it->f))
11008 produce_special_glyphs (it, IT_CONTINUATION);
11009 row->continued_p = 1;
11010
11011 it->current_x = x;
11012 it->continuation_lines_width += x;
11013 }
11014 break;
11015 }
11016 else if (new_x > it->first_visible_x)
11017 {
11018 /* Increment number of glyphs actually displayed. */
11019 ++it->hpos;
11020
11021 if (x < it->first_visible_x)
11022 /* Glyph is partially visible, i.e. row starts at
11023 negative X position. */
11024 row->x = x - it->first_visible_x;
11025 }
11026 else
11027 {
11028 /* Glyph is completely off the left margin of the
11029 window. This should not happen because of the
11030 move_it_in_display_line at the start of
11031 this function. */
11032 abort ();
11033 }
11034 }
11035
11036 row->ascent = max (row->ascent, it->max_ascent);
11037 row->height = max (row->height, it->max_ascent + it->max_descent);
11038 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
11039 row->phys_height = max (row->phys_height,
11040 it->max_phys_ascent + it->max_phys_descent);
11041
11042 /* End of this display line if row is continued. */
11043 if (row->continued_p)
11044 break;
11045 }
11046
11047 /* Is this a line end? If yes, we're also done, after making
11048 sure that a non-default face is extended up to the right
11049 margin of the window. */
11050 if (ITERATOR_AT_END_OF_LINE_P (it))
11051 {
11052 int used_before = row->used[TEXT_AREA];
11053
11054 /* Add a space at the end of the line that is used to
11055 display the cursor there. */
11056 append_space (it, 0);
11057
11058 /* Extend the face to the end of the line. */
11059 extend_face_to_end_of_line (it);
11060
11061 /* Make sure we have the position. */
11062 if (used_before == 0)
11063 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
11064
11065 /* Consume the line end. This skips over invisible lines. */
11066 set_iterator_to_next (it);
11067 it->continuation_lines_width = 0;
11068 break;
11069 }
11070
11071 /* Proceed with next display element. Note that this skips
11072 over lines invisible because of selective display. */
11073 set_iterator_to_next (it);
11074
11075 /* If we truncate lines, we are done when the last displayed
11076 glyphs reach past the right margin of the window. */
11077 if (it->truncate_lines_p
11078 && (FRAME_WINDOW_P (it->f)
11079 ? (it->current_x >= it->last_visible_x)
11080 : (it->current_x > it->last_visible_x)))
11081 {
11082 /* Maybe add truncation glyphs. */
11083 if (!FRAME_WINDOW_P (it->f))
11084 {
11085 --it->glyph_row->used[TEXT_AREA];
11086 produce_special_glyphs (it, IT_TRUNCATION);
11087 }
11088
11089 row->truncated_on_right_p = 1;
11090 it->continuation_lines_width = 0;
11091 reseat_at_next_visible_line_start (it, 0);
11092 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
11093 it->hpos = hpos_before;
11094 it->current_x = x_before;
11095 break;
11096 }
11097 }
11098
11099 /* If line is not empty and hscrolled, maybe insert truncation glyphs
11100 at the left window margin. */
11101 if (it->first_visible_x
11102 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
11103 {
11104 if (!FRAME_WINDOW_P (it->f))
11105 insert_left_trunc_glyphs (it);
11106 row->truncated_on_left_p = 1;
11107 }
11108
11109 /* If the start of this line is the overlay arrow-position, then
11110 mark this glyph row as the one containing the overlay arrow.
11111 This is clearly a mess with variable size fonts. It would be
11112 better to let it be displayed like cursors under X. */
11113 if (MARKERP (Voverlay_arrow_position)
11114 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
11115 && (MATRIX_ROW_START_CHARPOS (row)
11116 == marker_position (Voverlay_arrow_position))
11117 && STRINGP (Voverlay_arrow_string)
11118 && ! overlay_arrow_seen)
11119 {
11120 /* Overlay arrow in window redisplay is a bitmap. */
11121 if (!FRAME_WINDOW_P (it->f))
11122 {
11123 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
11124 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
11125 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
11126 struct glyph *p = row->glyphs[TEXT_AREA];
11127 struct glyph *p2, *end;
11128
11129 /* Copy the arrow glyphs. */
11130 while (glyph < arrow_end)
11131 *p++ = *glyph++;
11132
11133 /* Throw away padding glyphs. */
11134 p2 = p;
11135 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
11136 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
11137 ++p2;
11138 if (p2 > p)
11139 {
11140 while (p2 < end)
11141 *p++ = *p2++;
11142 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
11143 }
11144 }
11145
11146 overlay_arrow_seen = 1;
11147 row->overlay_arrow_p = 1;
11148 }
11149
11150 /* Compute pixel dimensions of this line. */
11151 compute_line_metrics (it);
11152
11153 /* Remember the position at which this line ends. */
11154 row->end = it->current;
11155
11156 /* Maybe set the cursor. If you change this, it's probably a good
11157 idea to also change the code in redisplay_window for cursor
11158 movement in an unchanged window. */
11159 if (it->w->cursor.vpos < 0
11160 && PT >= MATRIX_ROW_START_CHARPOS (row)
11161 && MATRIX_ROW_END_CHARPOS (row) >= PT
11162 && !(MATRIX_ROW_END_CHARPOS (row) == PT
11163 && (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
11164 || !row->ends_at_zv_p)))
11165 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
11166
11167 /* Highlight trailing whitespace. */
11168 if (!NILP (Vshow_trailing_whitespace))
11169 highlight_trailing_whitespace (it->f, it->glyph_row);
11170
11171 /* Prepare for the next line. This line starts horizontally at (X
11172 HPOS) = (0 0). Vertical positions are incremented. As a
11173 convenience for the caller, IT->glyph_row is set to the next
11174 row to be used. */
11175 it->current_x = it->hpos = 0;
11176 it->current_y += row->height;
11177 ++it->vpos;
11178 ++it->glyph_row;
11179 return row->displays_text_p;
11180 }
11181
11182
11183 \f
11184 /***********************************************************************
11185 Menu Bar
11186 ***********************************************************************/
11187
11188 /* Redisplay the menu bar in the frame for window W.
11189
11190 The menu bar of X frames that don't have X toolkit support is
11191 displayed in a special window W->frame->menu_bar_window.
11192
11193 The menu bar of terminal frames is treated specially as far as
11194 glyph matrices are concerned. Menu bar lines are not part of
11195 windows, so the update is done directly on the frame matrix rows
11196 for the menu bar. */
11197
11198 static void
11199 display_menu_bar (w)
11200 struct window *w;
11201 {
11202 struct frame *f = XFRAME (WINDOW_FRAME (w));
11203 struct it it;
11204 Lisp_Object items;
11205 int i;
11206
11207 /* Don't do all this for graphical frames. */
11208 #ifdef HAVE_NTGUI
11209 if (!NILP (Vwindow_system))
11210 return;
11211 #endif
11212 #ifdef USE_X_TOOLKIT
11213 if (FRAME_X_P (f))
11214 return;
11215 #endif
11216
11217 #ifdef USE_X_TOOLKIT
11218 xassert (!FRAME_WINDOW_P (f));
11219 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MODE_LINE_FACE_ID);
11220 it.first_visible_x = 0;
11221 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
11222 #else /* not USE_X_TOOLKIT */
11223 if (FRAME_WINDOW_P (f))
11224 {
11225 /* Menu bar lines are displayed in the desired matrix of the
11226 dummy window menu_bar_window. */
11227 struct window *menu_w;
11228 xassert (WINDOWP (f->menu_bar_window));
11229 menu_w = XWINDOW (f->menu_bar_window);
11230 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
11231 MODE_LINE_FACE_ID);
11232 it.first_visible_x = 0;
11233 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
11234 }
11235 else
11236 {
11237 /* This is a TTY frame, i.e. character hpos/vpos are used as
11238 pixel x/y. */
11239 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
11240 MODE_LINE_FACE_ID);
11241 it.first_visible_x = 0;
11242 it.last_visible_x = FRAME_WIDTH (f);
11243 }
11244 #endif /* not USE_X_TOOLKIT */
11245
11246 /* Clear all rows of the menu bar. */
11247 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
11248 {
11249 struct glyph_row *row = it.glyph_row + i;
11250 clear_glyph_row (row);
11251 row->enabled_p = 1;
11252 row->full_width_p = 1;
11253 }
11254
11255 /* Make the first line of the menu bar appear in reverse video. */
11256 it.glyph_row->inverse_p = mode_line_inverse_video != 0;
11257
11258 /* Display all items of the menu bar. */
11259 items = FRAME_MENU_BAR_ITEMS (it.f);
11260 for (i = 0; i < XVECTOR (items)->size; i += 4)
11261 {
11262 Lisp_Object string;
11263
11264 /* Stop at nil string. */
11265 string = XVECTOR (items)->contents[i + 1];
11266 if (NILP (string))
11267 break;
11268
11269 /* Remember where item was displayed. */
11270 XSETFASTINT (XVECTOR (items)->contents[i + 3], it.hpos);
11271
11272 /* Display the item, pad with one space. */
11273 if (it.current_x < it.last_visible_x)
11274 display_string (NULL, string, Qnil, 0, 0, &it,
11275 XSTRING (string)->size + 1, 0, 0, -1);
11276 }
11277
11278 /* Fill out the line with spaces. */
11279 if (it.current_x < it.last_visible_x)
11280 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
11281
11282 /* Compute the total height of the lines. */
11283 compute_line_metrics (&it);
11284 }
11285
11286
11287 \f
11288 /***********************************************************************
11289 Mode Line
11290 ***********************************************************************/
11291
11292 /* Display the mode and/or top line of window W. */
11293
11294 static void
11295 display_mode_lines (w)
11296 struct window *w;
11297 {
11298 /* These will be set while the mode line specs are processed. */
11299 line_number_displayed = 0;
11300 w->column_number_displayed = Qnil;
11301
11302 if (WINDOW_WANTS_MODELINE_P (w))
11303 display_mode_line (w, MODE_LINE_FACE_ID, current_buffer->mode_line_format);
11304
11305 if (WINDOW_WANTS_TOP_LINE_P (w))
11306 display_mode_line (w, TOP_LINE_FACE_ID, current_buffer->top_line_format);
11307 }
11308
11309
11310 /* Display mode or top line of window W. FACE_ID specifies which line
11311 to display; it is either MODE_LINE_FACE_ID or TOP_LINE_FACE_ID.
11312 FORMAT is the mode line format to display. */
11313
11314 static void
11315 display_mode_line (w, face_id, format)
11316 struct window *w;
11317 enum face_id face_id;
11318 Lisp_Object format;
11319 {
11320 struct it it;
11321 struct face *face;
11322
11323 init_iterator (&it, w, -1, -1, NULL, face_id);
11324 prepare_desired_row (it.glyph_row);
11325
11326 /* Temporarily make frame's keyboard the current kboard so that
11327 kboard-local variables in the mode_line_format will get the right
11328 values. */
11329 push_frame_kboard (it.f);
11330 display_mode_element (&it, 0, 0, 0, format);
11331 pop_frame_kboard ();
11332
11333 /* Fill up with spaces. */
11334 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
11335
11336 compute_line_metrics (&it);
11337 it.glyph_row->full_width_p = 1;
11338 it.glyph_row->mode_line_p = 1;
11339 it.glyph_row->inverse_p = mode_line_inverse_video != 0;
11340 it.glyph_row->continued_p = 0;
11341 it.glyph_row->truncated_on_left_p = 0;
11342 it.glyph_row->truncated_on_right_p = 0;
11343
11344 /* Make a 3D mode-line have a shadow at its right end. */
11345 face = FACE_FROM_ID (it.f, face_id);
11346 extend_face_to_end_of_line (&it);
11347 if (face->box != FACE_NO_BOX)
11348 {
11349 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
11350 + it.glyph_row->used[TEXT_AREA] - 1);
11351 last->right_box_line_p = 1;
11352 }
11353 }
11354
11355
11356 /* Contribute ELT to the mode line for window IT->w. How it
11357 translates into text depends on its data type.
11358
11359 IT describes the display environment in which we display, as usual.
11360
11361 DEPTH is the depth in recursion. It is used to prevent
11362 infinite recursion here.
11363
11364 FIELD_WIDTH is the number of characters the display of ELT should
11365 occupy in the mode line, and PRECISION is the maximum number of
11366 characters to display from ELT's representation. See
11367 display_string for details. *
11368
11369 Returns the hpos of the end of the text generated by ELT. */
11370
11371 static int
11372 display_mode_element (it, depth, field_width, precision, elt)
11373 struct it *it;
11374 int depth;
11375 int field_width, precision;
11376 Lisp_Object elt;
11377 {
11378 int n = 0, field, prec;
11379
11380 tail_recurse:
11381 if (depth > 10)
11382 goto invalid;
11383
11384 depth++;
11385
11386 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
11387 {
11388 case Lisp_String:
11389 {
11390 /* A string: output it and check for %-constructs within it. */
11391 unsigned char c;
11392 unsigned char *this = XSTRING (elt)->data;
11393 unsigned char *lisp_string = this;
11394
11395 while ((precision <= 0 || n < precision)
11396 && *this
11397 && (frame_title_ptr
11398 || it->current_x < it->last_visible_x))
11399 {
11400 unsigned char *last = this;
11401
11402 /* Advance to end of string or next format specifier. */
11403 while ((c = *this++) != '\0' && c != '%')
11404 ;
11405
11406 if (this - 1 != last)
11407 {
11408 /* Output to end of string or up to '%'. Field width
11409 is length of string. Don't output more than
11410 PRECISION allows us. */
11411 prec = --this - last;
11412 if (precision > 0 && prec > precision - n)
11413 prec = precision - n;
11414
11415 if (frame_title_ptr)
11416 n += store_frame_title (last, prec, prec);
11417 else
11418 n += display_string (NULL, elt, Qnil, 0, last - lisp_string,
11419 it, 0, prec, 0, -1);
11420 }
11421 else /* c == '%' */
11422 {
11423 unsigned char *percent_position = this;
11424
11425 /* Get the specified minimum width. Zero means
11426 don't pad. */
11427 field = 0;
11428 while ((c = *this++) >= '0' && c <= '9')
11429 field = field * 10 + c - '0';
11430
11431 /* Don't pad beyond the total padding allowed. */
11432 if (field_width - n > 0 && field > field_width - n)
11433 field = field_width - n;
11434
11435 /* Note that either PRECISION <= 0 or N < PRECISION. */
11436 prec = precision - n;
11437
11438 if (c == 'M')
11439 n += display_mode_element (it, depth, field, prec,
11440 Vglobal_mode_string);
11441 else if (c != 0)
11442 {
11443 unsigned char *spec
11444 = decode_mode_spec (it->w, c, field, prec);
11445
11446 if (frame_title_ptr)
11447 n += store_frame_title (spec, field, prec);
11448 else
11449 {
11450 int nglyphs_before
11451 = it->glyph_row->used[TEXT_AREA];
11452 int charpos
11453 = percent_position - XSTRING (elt)->data;
11454 int nwritten
11455 = display_string (spec, Qnil, elt, charpos, 0, it,
11456 field, prec, 0, -1);
11457
11458 /* Assign to the glyphs written above the
11459 string where the `%x' came from, position
11460 of the `%'. */
11461 if (nwritten > 0)
11462 {
11463 struct glyph *glyph
11464 = (it->glyph_row->glyphs[TEXT_AREA]
11465 + nglyphs_before);
11466 int i;
11467
11468 for (i = 0; i < nwritten; ++i)
11469 {
11470 glyph[i].object = elt;
11471 glyph[i].charpos = charpos;
11472 }
11473
11474 n += nwritten;
11475 }
11476 }
11477 }
11478 }
11479 }
11480 }
11481 break;
11482
11483 case Lisp_Symbol:
11484 /* A symbol: process the value of the symbol recursively
11485 as if it appeared here directly. Avoid error if symbol void.
11486 Special case: if value of symbol is a string, output the string
11487 literally. */
11488 {
11489 register Lisp_Object tem;
11490 tem = Fboundp (elt);
11491 if (!NILP (tem))
11492 {
11493 tem = Fsymbol_value (elt);
11494 /* If value is a string, output that string literally:
11495 don't check for % within it. */
11496 if (STRINGP (tem))
11497 {
11498 prec = XSTRING (tem)->size;
11499 if (precision > 0 && prec > precision - n)
11500 prec = precision - n;
11501 if (frame_title_ptr)
11502 n += store_frame_title (XSTRING (tem)->data, -1, prec);
11503 else
11504 n += display_string (NULL, tem, Qnil, 0, 0, it,
11505 0, prec, 0, -1);
11506 }
11507 else if (!EQ (tem, elt))
11508 {
11509 /* Give up right away for nil or t. */
11510 elt = tem;
11511 goto tail_recurse;
11512 }
11513 }
11514 }
11515 break;
11516
11517 case Lisp_Cons:
11518 {
11519 register Lisp_Object car, tem;
11520
11521 /* A cons cell: three distinct cases.
11522 If first element is a string or a cons, process all the elements
11523 and effectively concatenate them.
11524 If first element is a negative number, truncate displaying cdr to
11525 at most that many characters. If positive, pad (with spaces)
11526 to at least that many characters.
11527 If first element is a symbol, process the cadr or caddr recursively
11528 according to whether the symbol's value is non-nil or nil. */
11529 car = XCONS (elt)->car;
11530 if (EQ (car, QCeval) && CONSP (XCDR (elt)))
11531 {
11532 /* An element of the form (:eval FORM) means evaluate FORM
11533 and use the result as mode line elements. */
11534 struct gcpro gcpro1;
11535 Lisp_Object spec;
11536
11537 spec = eval_form (XCAR (XCDR (elt)));
11538 GCPRO1 (spec);
11539 n += display_mode_element (it, depth, field_width - n,
11540 precision - n, spec);
11541 UNGCPRO;
11542 }
11543 else if (SYMBOLP (car))
11544 {
11545 tem = Fboundp (car);
11546 elt = XCONS (elt)->cdr;
11547 if (!CONSP (elt))
11548 goto invalid;
11549 /* elt is now the cdr, and we know it is a cons cell.
11550 Use its car if CAR has a non-nil value. */
11551 if (!NILP (tem))
11552 {
11553 tem = Fsymbol_value (car);
11554 if (!NILP (tem))
11555 { elt = XCONS (elt)->car; goto tail_recurse; }
11556 }
11557 /* Symbol's value is nil (or symbol is unbound)
11558 Get the cddr of the original list
11559 and if possible find the caddr and use that. */
11560 elt = XCONS (elt)->cdr;
11561 if (NILP (elt))
11562 break;
11563 else if (!CONSP (elt))
11564 goto invalid;
11565 elt = XCONS (elt)->car;
11566 goto tail_recurse;
11567 }
11568 else if (INTEGERP (car))
11569 {
11570 register int lim = XINT (car);
11571 elt = XCONS (elt)->cdr;
11572 if (lim < 0)
11573 {
11574 /* Negative int means reduce maximum width. */
11575 if (precision <= 0)
11576 precision = -lim;
11577 else
11578 precision = min (precision, -lim);
11579 }
11580 else if (lim > 0)
11581 {
11582 /* Padding specified. Don't let it be more than
11583 current maximum. */
11584 if (precision > 0)
11585 lim = min (precision, lim);
11586
11587 /* If that's more padding than already wanted, queue it.
11588 But don't reduce padding already specified even if
11589 that is beyond the current truncation point. */
11590 field_width = max (lim, field_width);
11591 }
11592 goto tail_recurse;
11593 }
11594 else if (STRINGP (car) || CONSP (car))
11595 {
11596 register int limit = 50;
11597 /* Limit is to protect against circular lists. */
11598 while (CONSP (elt)
11599 && --limit > 0
11600 && (precision <= 0 || n < precision))
11601 {
11602 n += display_mode_element (it, depth, field_width - n,
11603 precision - n, XCONS (elt)->car);
11604 elt = XCONS (elt)->cdr;
11605 }
11606 }
11607 }
11608 break;
11609
11610 default:
11611 invalid:
11612 if (frame_title_ptr)
11613 n += store_frame_title ("*invalid*", 0, precision - n);
11614 else
11615 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
11616 precision - n, 0, 0);
11617 return n;
11618 }
11619
11620 /* Pad to FIELD_WIDTH. */
11621 if (field_width > 0 && n < field_width)
11622 {
11623 if (frame_title_ptr)
11624 n += store_frame_title ("", field_width - n, 0);
11625 else
11626 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
11627 0, 0, 0);
11628 }
11629
11630 return n;
11631 }
11632
11633
11634 /* Write a null-terminated, right justified decimal representation of
11635 the positive integer D to BUF using a minimal field width WIDTH. */
11636
11637 static void
11638 pint2str (buf, width, d)
11639 register char *buf;
11640 register int width;
11641 register int d;
11642 {
11643 register char *p = buf;
11644
11645 if (d <= 0)
11646 *p++ = '0';
11647 else
11648 {
11649 while (d > 0)
11650 {
11651 *p++ = d % 10 + '0';
11652 d /= 10;
11653 }
11654 }
11655
11656 for (width -= (int) (p - buf); width > 0; --width)
11657 *p++ = ' ';
11658 *p-- = '\0';
11659 while (p > buf)
11660 {
11661 d = *buf;
11662 *buf++ = *p;
11663 *p-- = d;
11664 }
11665 }
11666
11667 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
11668 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
11669 type of CODING_SYSTEM. Return updated pointer into BUF. */
11670
11671 static unsigned char invalid_eol_type[] = "(*invalid*)";
11672
11673 static char *
11674 decode_mode_spec_coding (coding_system, buf, eol_flag)
11675 Lisp_Object coding_system;
11676 register char *buf;
11677 int eol_flag;
11678 {
11679 Lisp_Object val;
11680 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
11681 unsigned char *eol_str;
11682 int eol_str_len;
11683 /* The EOL conversion we are using. */
11684 Lisp_Object eoltype;
11685
11686 val = coding_system;
11687
11688 if (NILP (val)) /* Not yet decided. */
11689 {
11690 if (multibyte)
11691 *buf++ = '-';
11692 if (eol_flag)
11693 eoltype = eol_mnemonic_undecided;
11694 /* Don't mention EOL conversion if it isn't decided. */
11695 }
11696 else
11697 {
11698 Lisp_Object eolvalue;
11699
11700 eolvalue = Fget (coding_system, Qeol_type);
11701
11702 while (!NILP (val) && SYMBOLP (val))
11703 {
11704 val = Fget (val, Qcoding_system);
11705 if (NILP (eolvalue))
11706 eolvalue = Fget (val, Qeol_type);
11707 }
11708
11709 if (multibyte)
11710 *buf++ = XFASTINT (XVECTOR (val)->contents[1]);
11711
11712 if (eol_flag)
11713 {
11714 /* The EOL conversion that is normal on this system. */
11715
11716 if (NILP (eolvalue)) /* Not yet decided. */
11717 eoltype = eol_mnemonic_undecided;
11718 else if (VECTORP (eolvalue)) /* Not yet decided. */
11719 eoltype = eol_mnemonic_undecided;
11720 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
11721 eoltype = (XFASTINT (eolvalue) == 0
11722 ? eol_mnemonic_unix
11723 : (XFASTINT (eolvalue) == 1
11724 ? eol_mnemonic_dos : eol_mnemonic_mac));
11725 }
11726 }
11727
11728 if (eol_flag)
11729 {
11730 /* Mention the EOL conversion if it is not the usual one. */
11731 if (STRINGP (eoltype))
11732 {
11733 eol_str = XSTRING (eoltype)->data;
11734 eol_str_len = XSTRING (eoltype)->size;
11735 }
11736 else if (INTEGERP (eoltype)
11737 && CHAR_VALID_P (XINT (eoltype), 0))
11738 {
11739 unsigned char work[4];
11740
11741 eol_str_len = CHAR_STRING (XINT (eoltype), work, eol_str);
11742 }
11743 else
11744 {
11745 eol_str = invalid_eol_type;
11746 eol_str_len = sizeof (invalid_eol_type) - 1;
11747 }
11748 bcopy (eol_str, buf, eol_str_len);
11749 buf += eol_str_len;
11750 }
11751
11752 return buf;
11753 }
11754
11755 /* Return a string for the output of a mode line %-spec for window W,
11756 generated by character C. PRECISION >= 0 means don't return a
11757 string longer than that value. FIELD_WIDTH > 0 means pad the
11758 string returned with spaces to that value. */
11759
11760 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
11761
11762 static char *
11763 decode_mode_spec (w, c, field_width, precision)
11764 struct window *w;
11765 register char c;
11766 int field_width, precision;
11767 {
11768 Lisp_Object obj;
11769 struct frame *f = XFRAME (WINDOW_FRAME (w));
11770 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
11771 struct buffer *b = XBUFFER (w->buffer);
11772
11773 obj = Qnil;
11774
11775 switch (c)
11776 {
11777 case '*':
11778 if (!NILP (b->read_only))
11779 return "%";
11780 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
11781 return "*";
11782 return "-";
11783
11784 case '+':
11785 /* This differs from %* only for a modified read-only buffer. */
11786 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
11787 return "*";
11788 if (!NILP (b->read_only))
11789 return "%";
11790 return "-";
11791
11792 case '&':
11793 /* This differs from %* in ignoring read-only-ness. */
11794 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
11795 return "*";
11796 return "-";
11797
11798 case '%':
11799 return "%";
11800
11801 case '[':
11802 {
11803 int i;
11804 char *p;
11805
11806 if (command_loop_level > 5)
11807 return "[[[... ";
11808 p = decode_mode_spec_buf;
11809 for (i = 0; i < command_loop_level; i++)
11810 *p++ = '[';
11811 *p = 0;
11812 return decode_mode_spec_buf;
11813 }
11814
11815 case ']':
11816 {
11817 int i;
11818 char *p;
11819
11820 if (command_loop_level > 5)
11821 return " ...]]]";
11822 p = decode_mode_spec_buf;
11823 for (i = 0; i < command_loop_level; i++)
11824 *p++ = ']';
11825 *p = 0;
11826 return decode_mode_spec_buf;
11827 }
11828
11829 case '-':
11830 {
11831 register int i;
11832
11833 /* Let lots_of_dashes be a string of infinite length. */
11834 if (field_width <= 0
11835 || field_width > sizeof (lots_of_dashes))
11836 {
11837 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
11838 decode_mode_spec_buf[i] = '-';
11839 decode_mode_spec_buf[i] = '\0';
11840 return decode_mode_spec_buf;
11841 }
11842 else
11843 return lots_of_dashes;
11844 }
11845
11846 case 'b':
11847 obj = b->name;
11848 break;
11849
11850 case 'c':
11851 {
11852 int col = current_column ();
11853 XSETFASTINT (w->column_number_displayed, col);
11854 pint2str (decode_mode_spec_buf, field_width, col);
11855 return decode_mode_spec_buf;
11856 }
11857
11858 case 'F':
11859 /* %F displays the frame name. */
11860 if (!NILP (f->title))
11861 return (char *) XSTRING (f->title)->data;
11862 if (f->explicit_name || ! FRAME_WINDOW_P (f))
11863 return (char *) XSTRING (f->name)->data;
11864 return "Emacs";
11865
11866 case 'f':
11867 obj = b->filename;
11868 break;
11869
11870 case 'l':
11871 {
11872 int startpos = XMARKER (w->start)->charpos;
11873 int startpos_byte = marker_byte_position (w->start);
11874 int line, linepos, linepos_byte, topline;
11875 int nlines, junk;
11876 int height = XFASTINT (w->height);
11877
11878 /* If we decided that this buffer isn't suitable for line numbers,
11879 don't forget that too fast. */
11880 if (EQ (w->base_line_pos, w->buffer))
11881 goto no_value;
11882 /* But do forget it, if the window shows a different buffer now. */
11883 else if (BUFFERP (w->base_line_pos))
11884 w->base_line_pos = Qnil;
11885
11886 /* If the buffer is very big, don't waste time. */
11887 if (BUF_ZV (b) - BUF_BEGV (b) > line_number_display_limit)
11888 {
11889 w->base_line_pos = Qnil;
11890 w->base_line_number = Qnil;
11891 goto no_value;
11892 }
11893
11894 if (!NILP (w->base_line_number)
11895 && !NILP (w->base_line_pos)
11896 && XFASTINT (w->base_line_pos) <= startpos)
11897 {
11898 line = XFASTINT (w->base_line_number);
11899 linepos = XFASTINT (w->base_line_pos);
11900 linepos_byte = buf_charpos_to_bytepos (b, linepos);
11901 }
11902 else
11903 {
11904 line = 1;
11905 linepos = BUF_BEGV (b);
11906 linepos_byte = BUF_BEGV_BYTE (b);
11907 }
11908
11909 /* Count lines from base line to window start position. */
11910 nlines = display_count_lines (linepos, linepos_byte,
11911 startpos_byte,
11912 startpos, &junk);
11913
11914 topline = nlines + line;
11915
11916 /* Determine a new base line, if the old one is too close
11917 or too far away, or if we did not have one.
11918 "Too close" means it's plausible a scroll-down would
11919 go back past it. */
11920 if (startpos == BUF_BEGV (b))
11921 {
11922 XSETFASTINT (w->base_line_number, topline);
11923 XSETFASTINT (w->base_line_pos, BUF_BEGV (b));
11924 }
11925 else if (nlines < height + 25 || nlines > height * 3 + 50
11926 || linepos == BUF_BEGV (b))
11927 {
11928 int limit = BUF_BEGV (b);
11929 int limit_byte = BUF_BEGV_BYTE (b);
11930 int position;
11931 int distance = (height * 2 + 30) * line_number_display_limit_width;
11932
11933 if (startpos - distance > limit)
11934 {
11935 limit = startpos - distance;
11936 limit_byte = CHAR_TO_BYTE (limit);
11937 }
11938
11939 nlines = display_count_lines (startpos, startpos_byte,
11940 limit_byte,
11941 - (height * 2 + 30),
11942 &position);
11943 /* If we couldn't find the lines we wanted within
11944 line_number_display_limit_width chars per line,
11945 give up on line numbers for this window. */
11946 if (position == limit_byte && limit == startpos - distance)
11947 {
11948 w->base_line_pos = w->buffer;
11949 w->base_line_number = Qnil;
11950 goto no_value;
11951 }
11952
11953 XSETFASTINT (w->base_line_number, topline - nlines);
11954 XSETFASTINT (w->base_line_pos, BYTE_TO_CHAR (position));
11955 }
11956
11957 /* Now count lines from the start pos to point. */
11958 nlines = display_count_lines (startpos, startpos_byte,
11959 PT_BYTE, PT, &junk);
11960
11961 /* Record that we did display the line number. */
11962 line_number_displayed = 1;
11963
11964 /* Make the string to show. */
11965 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
11966 return decode_mode_spec_buf;
11967 no_value:
11968 {
11969 char* p = decode_mode_spec_buf;
11970 int pad = field_width - 2;
11971 while (pad-- > 0)
11972 *p++ = ' ';
11973 *p++ = '?';
11974 *p = '?';
11975 return decode_mode_spec_buf;
11976 }
11977 }
11978 break;
11979
11980 case 'm':
11981 obj = b->mode_name;
11982 break;
11983
11984 case 'n':
11985 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
11986 return " Narrow";
11987 break;
11988
11989 case 'p':
11990 {
11991 int pos = marker_position (w->start);
11992 int total = BUF_ZV (b) - BUF_BEGV (b);
11993
11994 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
11995 {
11996 if (pos <= BUF_BEGV (b))
11997 return "All";
11998 else
11999 return "Bottom";
12000 }
12001 else if (pos <= BUF_BEGV (b))
12002 return "Top";
12003 else
12004 {
12005 if (total > 1000000)
12006 /* Do it differently for a large value, to avoid overflow. */
12007 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
12008 else
12009 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
12010 /* We can't normally display a 3-digit number,
12011 so get us a 2-digit number that is close. */
12012 if (total == 100)
12013 total = 99;
12014 sprintf (decode_mode_spec_buf, "%2d%%", total);
12015 return decode_mode_spec_buf;
12016 }
12017 }
12018
12019 /* Display percentage of size above the bottom of the screen. */
12020 case 'P':
12021 {
12022 int toppos = marker_position (w->start);
12023 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
12024 int total = BUF_ZV (b) - BUF_BEGV (b);
12025
12026 if (botpos >= BUF_ZV (b))
12027 {
12028 if (toppos <= BUF_BEGV (b))
12029 return "All";
12030 else
12031 return "Bottom";
12032 }
12033 else
12034 {
12035 if (total > 1000000)
12036 /* Do it differently for a large value, to avoid overflow. */
12037 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
12038 else
12039 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
12040 /* We can't normally display a 3-digit number,
12041 so get us a 2-digit number that is close. */
12042 if (total == 100)
12043 total = 99;
12044 if (toppos <= BUF_BEGV (b))
12045 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
12046 else
12047 sprintf (decode_mode_spec_buf, "%2d%%", total);
12048 return decode_mode_spec_buf;
12049 }
12050 }
12051
12052 case 's':
12053 /* status of process */
12054 obj = Fget_buffer_process (w->buffer);
12055 if (NILP (obj))
12056 return "no process";
12057 #ifdef subprocesses
12058 obj = Fsymbol_name (Fprocess_status (obj));
12059 #endif
12060 break;
12061
12062 case 't': /* indicate TEXT or BINARY */
12063 #ifdef MODE_LINE_BINARY_TEXT
12064 return MODE_LINE_BINARY_TEXT (b);
12065 #else
12066 return "T";
12067 #endif
12068
12069 case 'z':
12070 /* coding-system (not including end-of-line format) */
12071 case 'Z':
12072 /* coding-system (including end-of-line type) */
12073 {
12074 int eol_flag = (c == 'Z');
12075 char *p = decode_mode_spec_buf;
12076
12077 if (! FRAME_WINDOW_P (f))
12078 {
12079 /* No need to mention EOL here--the terminal never needs
12080 to do EOL conversion. */
12081 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
12082 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
12083 }
12084 p = decode_mode_spec_coding (b->buffer_file_coding_system,
12085 p, eol_flag);
12086
12087 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
12088 #ifdef subprocesses
12089 obj = Fget_buffer_process (Fcurrent_buffer ());
12090 if (PROCESSP (obj))
12091 {
12092 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
12093 p, eol_flag);
12094 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
12095 p, eol_flag);
12096 }
12097 #endif /* subprocesses */
12098 #endif /* 0 */
12099 *p = 0;
12100 return decode_mode_spec_buf;
12101 }
12102 }
12103
12104 if (STRINGP (obj))
12105 return (char *) XSTRING (obj)->data;
12106 else
12107 return "";
12108 }
12109
12110
12111 /* Count up to COUNT lines starting from START / START_BYTE.
12112 But don't go beyond LIMIT_BYTE.
12113 Return the number of lines thus found (always nonnegative).
12114
12115 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
12116
12117 static int
12118 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
12119 int start, start_byte, limit_byte, count;
12120 int *byte_pos_ptr;
12121 {
12122 register unsigned char *cursor;
12123 unsigned char *base;
12124
12125 register int ceiling;
12126 register unsigned char *ceiling_addr;
12127 int orig_count = count;
12128
12129 /* If we are not in selective display mode,
12130 check only for newlines. */
12131 int selective_display = (!NILP (current_buffer->selective_display)
12132 && !INTEGERP (current_buffer->selective_display));
12133
12134 if (count > 0)
12135 {
12136 while (start_byte < limit_byte)
12137 {
12138 ceiling = BUFFER_CEILING_OF (start_byte);
12139 ceiling = min (limit_byte - 1, ceiling);
12140 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
12141 base = (cursor = BYTE_POS_ADDR (start_byte));
12142 while (1)
12143 {
12144 if (selective_display)
12145 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
12146 ;
12147 else
12148 while (*cursor != '\n' && ++cursor != ceiling_addr)
12149 ;
12150
12151 if (cursor != ceiling_addr)
12152 {
12153 if (--count == 0)
12154 {
12155 start_byte += cursor - base + 1;
12156 *byte_pos_ptr = start_byte;
12157 return orig_count;
12158 }
12159 else
12160 if (++cursor == ceiling_addr)
12161 break;
12162 }
12163 else
12164 break;
12165 }
12166 start_byte += cursor - base;
12167 }
12168 }
12169 else
12170 {
12171 while (start_byte > limit_byte)
12172 {
12173 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
12174 ceiling = max (limit_byte, ceiling);
12175 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
12176 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
12177 while (1)
12178 {
12179 if (selective_display)
12180 while (--cursor != ceiling_addr
12181 && *cursor != '\n' && *cursor != 015)
12182 ;
12183 else
12184 while (--cursor != ceiling_addr && *cursor != '\n')
12185 ;
12186
12187 if (cursor != ceiling_addr)
12188 {
12189 if (++count == 0)
12190 {
12191 start_byte += cursor - base + 1;
12192 *byte_pos_ptr = start_byte;
12193 /* When scanning backwards, we should
12194 not count the newline posterior to which we stop. */
12195 return - orig_count - 1;
12196 }
12197 }
12198 else
12199 break;
12200 }
12201 /* Here we add 1 to compensate for the last decrement
12202 of CURSOR, which took it past the valid range. */
12203 start_byte += cursor - base + 1;
12204 }
12205 }
12206
12207 *byte_pos_ptr = limit_byte;
12208
12209 if (count < 0)
12210 return - orig_count + count;
12211 return orig_count - count;
12212
12213 }
12214
12215
12216 \f
12217 /***********************************************************************
12218 Displaying strings
12219 ***********************************************************************/
12220
12221 /* Display a NUL-terminated string, starting with index START.
12222
12223 If STRING is non-null, display that C string. Otherwise, the Lisp
12224 string LISP_STRING is displayed.
12225
12226 If FACE_STRING is not nil, FACE_STRING_POS is a position in
12227 FACE_STRING. Display STRING or LISP_STRING with the face at
12228 FACE_STRING_POS in FACE_STRING:
12229
12230 Display the string in the environment given by IT, but use the
12231 standard display table, temporarily.
12232
12233 FIELD_WIDTH is the minimum number of output glyphs to produce.
12234 If STRING has fewer characters than FIELD_WIDTH, pad to the right
12235 with spaces. If STRING has more characters, more than FIELD_WIDTH
12236 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
12237
12238 PRECISION is the maximum number of characters to output from
12239 STRING. PRECISION < 0 means don't truncate the string.
12240
12241 This is roughly equivalent to printf format specifiers:
12242
12243 FIELD_WIDTH PRECISION PRINTF
12244 ----------------------------------------
12245 -1 -1 %s
12246 -1 10 %.10s
12247 10 -1 %10s
12248 20 10 %20.10s
12249
12250 MULTIBYTE zero means do not display multibyte chars, > 0 means do
12251 display them, and < 0 means obey the current buffer's value of
12252 enable_multibyte_characters.
12253
12254 Value is the number of glyphs produced. */
12255
12256 static int
12257 display_string (string, lisp_string, face_string, face_string_pos,
12258 start, it, field_width, precision, max_x, multibyte)
12259 unsigned char *string;
12260 Lisp_Object lisp_string;
12261 int start;
12262 struct it *it;
12263 int field_width, precision, max_x;
12264 int multibyte;
12265 {
12266 int hpos_at_start = it->hpos;
12267 int saved_face_id = it->face_id;
12268 struct glyph_row *row = it->glyph_row;
12269
12270 /* Initialize the iterator IT for iteration over STRING beginning
12271 with index START. We assume that IT may be modified here (which
12272 means that display_line has to do something when displaying a
12273 mini-buffer prompt, which it does). */
12274 reseat_to_string (it, string, lisp_string, start,
12275 precision, field_width, multibyte);
12276
12277 /* If displaying STRING, set up the face of the iterator
12278 from LISP_STRING, if that's given. */
12279 if (STRINGP (face_string))
12280 {
12281 int endptr;
12282 struct face *face;
12283
12284 it->face_id
12285 = face_at_string_position (it->w, face_string, face_string_pos,
12286 0, it->region_beg_charpos,
12287 it->region_end_charpos,
12288 &endptr, it->base_face_id);
12289 face = FACE_FROM_ID (it->f, it->face_id);
12290 it->face_box_p = face->box != FACE_NO_BOX;
12291 }
12292
12293 /* Set max_x to the maximum allowed X position. Don't let it go
12294 beyond the right edge of the window. */
12295 if (max_x <= 0)
12296 max_x = it->last_visible_x;
12297 else
12298 max_x = min (max_x, it->last_visible_x);
12299
12300 /* Skip over display elements that are not visible. because IT->w is
12301 hscrolled. */
12302 if (it->current_x < it->first_visible_x)
12303 move_it_in_display_line_to (it, 100000, it->first_visible_x,
12304 MOVE_TO_POS | MOVE_TO_X);
12305
12306 row->ascent = it->max_ascent;
12307 row->height = it->max_ascent + it->max_descent;
12308 row->phys_ascent = it->max_phys_ascent;
12309 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
12310
12311 /* This condition is for the case that we are called with current_x
12312 past last_visible_x. */
12313 while (it->current_x < max_x)
12314 {
12315 int x_before, x, n_glyphs_before, i, nglyphs;
12316
12317 /* Get the next display element. */
12318 if (!get_next_display_element (it))
12319 break;
12320
12321 /* Produce glyphs. */
12322 x_before = it->current_x;
12323 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
12324 PRODUCE_GLYPHS (it);
12325
12326 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
12327 i = 0;
12328 x = x_before;
12329 while (i < nglyphs)
12330 {
12331 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12332
12333 if (!it->truncate_lines_p
12334 && x + glyph->pixel_width > max_x)
12335 {
12336 /* End of continued line or max_x reached. */
12337 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
12338 it->current_x = x;
12339 break;
12340 }
12341 else if (x + glyph->pixel_width > it->first_visible_x)
12342 {
12343 /* Glyph is at least partially visible. */
12344 ++it->hpos;
12345 if (x < it->first_visible_x)
12346 it->glyph_row->x = x - it->first_visible_x;
12347 }
12348 else
12349 {
12350 /* Glyph is off the left margin of the display area.
12351 Should not happen. */
12352 abort ();
12353 }
12354
12355 row->ascent = max (row->ascent, it->max_ascent);
12356 row->height = max (row->height, it->max_ascent + it->max_descent);
12357 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12358 row->phys_height = max (row->phys_height,
12359 it->max_phys_ascent + it->max_phys_descent);
12360 x += glyph->pixel_width;
12361 ++i;
12362 }
12363
12364 /* Stop if max_x reached. */
12365 if (i < nglyphs)
12366 break;
12367
12368 /* Stop at line ends. */
12369 if (ITERATOR_AT_END_OF_LINE_P (it))
12370 {
12371 it->continuation_lines_width = 0;
12372 break;
12373 }
12374
12375 set_iterator_to_next (it);
12376
12377 /* Stop if truncating at the right edge. */
12378 if (it->truncate_lines_p
12379 && it->current_x >= it->last_visible_x)
12380 {
12381 /* Add truncation mark, but don't do it if the line is
12382 truncated at a padding space. */
12383 if (IT_CHARPOS (*it) < it->string_nchars)
12384 {
12385 if (!FRAME_WINDOW_P (it->f))
12386 produce_special_glyphs (it, IT_TRUNCATION);
12387 it->glyph_row->truncated_on_right_p = 1;
12388 }
12389 break;
12390 }
12391 }
12392
12393 /* Maybe insert a truncation at the left. */
12394 if (it->first_visible_x
12395 && IT_CHARPOS (*it) > 0)
12396 {
12397 if (!FRAME_WINDOW_P (it->f))
12398 insert_left_trunc_glyphs (it);
12399 it->glyph_row->truncated_on_left_p = 1;
12400 }
12401
12402 it->face_id = saved_face_id;
12403
12404 /* Value is number of columns displayed. */
12405 return it->hpos - hpos_at_start;
12406 }
12407
12408
12409 \f
12410 /* This is like a combination of memq and assq. Return 1 if PROPVAL
12411 appears as an element of LIST or as the car of an element of LIST.
12412 If PROPVAL is a list, compare each element against LIST in that
12413 way, and return 1 if any element of PROPVAL is found in LIST.
12414 Otherwise return 0. This function cannot quit. */
12415
12416 int
12417 invisible_p (propval, list)
12418 register Lisp_Object propval;
12419 Lisp_Object list;
12420 {
12421 register Lisp_Object tail, proptail;
12422 for (tail = list; CONSP (tail); tail = XCONS (tail)->cdr)
12423 {
12424 register Lisp_Object tem;
12425 tem = XCONS (tail)->car;
12426 if (EQ (propval, tem))
12427 return 1;
12428 if (CONSP (tem) && EQ (propval, XCONS (tem)->car))
12429 return 1;
12430 }
12431 if (CONSP (propval))
12432 for (proptail = propval; CONSP (proptail);
12433 proptail = XCONS (proptail)->cdr)
12434 {
12435 Lisp_Object propelt;
12436 propelt = XCONS (proptail)->car;
12437 for (tail = list; CONSP (tail); tail = XCONS (tail)->cdr)
12438 {
12439 register Lisp_Object tem;
12440 tem = XCONS (tail)->car;
12441 if (EQ (propelt, tem))
12442 return 1;
12443 if (CONSP (tem) && EQ (propelt, XCONS (tem)->car))
12444 return 1;
12445 }
12446 }
12447 return 0;
12448 }
12449
12450
12451 /* Return 1 if PROPVAL appears as the car of an element of LIST and
12452 the cdr of that element is non-nil. If PROPVAL is a list, check
12453 each element of PROPVAL in that way, and the first time some
12454 element is found, return 1 if the cdr of that element is non-nil.
12455 Otherwise return 0. This function cannot quit. */
12456
12457 int
12458 invisible_ellipsis_p (propval, list)
12459 register Lisp_Object propval;
12460 Lisp_Object list;
12461 {
12462 register Lisp_Object tail, proptail;
12463 for (tail = list; CONSP (tail); tail = XCONS (tail)->cdr)
12464 {
12465 register Lisp_Object tem;
12466 tem = XCONS (tail)->car;
12467 if (CONSP (tem) && EQ (propval, XCONS (tem)->car))
12468 return ! NILP (XCONS (tem)->cdr);
12469 }
12470 if (CONSP (propval))
12471 for (proptail = propval; CONSP (proptail);
12472 proptail = XCONS (proptail)->cdr)
12473 {
12474 Lisp_Object propelt;
12475 propelt = XCONS (proptail)->car;
12476 for (tail = list; CONSP (tail); tail = XCONS (tail)->cdr)
12477 {
12478 register Lisp_Object tem;
12479 tem = XCONS (tail)->car;
12480 if (CONSP (tem) && EQ (propelt, XCONS (tem)->car))
12481 return ! NILP (XCONS (tem)->cdr);
12482 }
12483 }
12484 return 0;
12485 }
12486
12487
12488 \f
12489 /***********************************************************************
12490 Initialization
12491 ***********************************************************************/
12492
12493 void
12494 syms_of_xdisp ()
12495 {
12496 Vresize_mini_config = Qnil;
12497 staticpro (&Vresize_mini_config);
12498
12499 Vwith_echo_area_save_vector = Qnil;
12500 staticpro (&Vwith_echo_area_save_vector);
12501
12502 Vmessage_stack = Qnil;
12503 staticpro (&Vmessage_stack);
12504
12505 Qinhibit_redisplay = intern ("inhibit-redisplay");
12506 staticpro (&Qinhibit_redisplay);
12507
12508 #if GLYPH_DEBUG
12509 defsubr (&Sdump_glyph_matrix);
12510 defsubr (&Sdump_glyph_row);
12511 defsubr (&Sdump_toolbar_row);
12512 defsubr (&Strace_redisplay_toggle);
12513 #endif
12514
12515 staticpro (&Qmenu_bar_update_hook);
12516 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
12517
12518 staticpro (&Qoverriding_terminal_local_map);
12519 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
12520
12521 staticpro (&Qoverriding_local_map);
12522 Qoverriding_local_map = intern ("overriding-local-map");
12523
12524 staticpro (&Qwindow_scroll_functions);
12525 Qwindow_scroll_functions = intern ("window-scroll-functions");
12526
12527 staticpro (&Qredisplay_end_trigger_functions);
12528 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
12529
12530 staticpro (&Qinhibit_point_motion_hooks);
12531 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
12532
12533 staticpro (&Qdisplay);
12534 Qdisplay = intern ("display");
12535 staticpro (&Qleft_margin);
12536 Qspace_width = intern ("space-width");
12537 staticpro (&Qspace_width);
12538 Qheight = intern ("height");
12539 staticpro (&Qheight);
12540 Qraise = intern ("raise");
12541 staticpro (&Qraise);
12542 Qspace = intern ("space");
12543 staticpro (&Qspace);
12544 Qleft_margin = intern ("left-margin");
12545 staticpro (&Qright_margin);
12546 Qright_margin = intern ("right-margin");
12547 Qalign_to = intern ("align-to");
12548 staticpro (&Qalign_to);
12549 QCalign_to = intern (":align-to");
12550 staticpro (&QCalign_to);
12551 Qwidth = intern ("width");
12552 staticpro (&Qwidth);
12553 Qrelative_width = intern ("relative-width");
12554 staticpro (&Qrelative_width);
12555 QCrelative_width = intern (":relative-width");
12556 staticpro (&QCrelative_width);
12557 QCrelative_height = intern (":relative-height");
12558 staticpro (&QCrelative_height);
12559 QCeval = intern (":eval");
12560 staticpro (&QCeval);
12561 QCwhen = intern (":when");
12562 staticpro (&QCwhen);
12563 Qfontified = intern ("fontified");
12564 staticpro (&Qfontified);
12565 Qfontification_functions = intern ("fontification-functions");
12566 staticpro (&Qfontification_functions);
12567 Qtrailing_whitespace = intern ("trailing-whitespace");
12568 staticpro (&Qtrailing_whitespace);
12569 Qimage = intern ("image");
12570 staticpro (&Qimage);
12571
12572 staticpro (&last_arrow_position);
12573 staticpro (&last_arrow_string);
12574 last_arrow_position = Qnil;
12575 last_arrow_string = Qnil;
12576
12577 echo_buffer[0] = echo_buffer[1] = Qnil;
12578 staticpro (&echo_buffer[0]);
12579 staticpro (&echo_buffer[1]);
12580
12581 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
12582 staticpro (&echo_area_buffer[0]);
12583 staticpro (&echo_area_buffer[1]);
12584
12585 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
12586 "Non-nil means highlight trailing whitespace.\n\
12587 The face used for trailing whitespace is `trailing-whitespace'.");
12588 Vshow_trailing_whitespace = Qnil;
12589
12590 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
12591 "Non-nil means don't actually do any redisplay.\n\
12592 This is used for internal purposes.");
12593 Vinhibit_redisplay = Qnil;
12594
12595 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
12596 "String (or mode line construct) included (normally) in `mode-line-format'.");
12597 Vglobal_mode_string = Qnil;
12598
12599 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
12600 "Marker for where to display an arrow on top of the buffer text.\n\
12601 This must be the beginning of a line in order to work.\n\
12602 See also `overlay-arrow-string'.");
12603 Voverlay_arrow_position = Qnil;
12604
12605 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
12606 "String to display as an arrow. See also `overlay-arrow-position'.");
12607 Voverlay_arrow_string = Qnil;
12608
12609 DEFVAR_INT ("scroll-step", &scroll_step,
12610 "*The number of lines to try scrolling a window by when point moves out.\n\
12611 If that fails to bring point back on frame, point is centered instead.\n\
12612 If this is zero, point is always centered after it moves off frame.");
12613
12614 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
12615 "*Scroll up to this many lines, to bring point back on screen.");
12616 scroll_conservatively = 0;
12617
12618 DEFVAR_INT ("scroll-margin", &scroll_margin,
12619 "*Number of lines of margin at the top and bottom of a window.\n\
12620 Recenter the window whenever point gets within this many lines\n\
12621 of the top or bottom of the window.");
12622 scroll_margin = 0;
12623
12624 #if GLYPH_DEBUG
12625 DEFVAR_INT ("debug-end-pos", &debug_end_pos, "Don't ask");
12626 #endif
12627
12628 DEFVAR_BOOL ("truncate-partial-width-windows",
12629 &truncate_partial_width_windows,
12630 "*Non-nil means truncate lines in all windows less than full frame wide.");
12631 truncate_partial_width_windows = 1;
12632
12633 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
12634 "*Non-nil means use inverse video for the mode line.");
12635 mode_line_inverse_video = 1;
12636
12637 DEFVAR_INT ("line-number-display-limit", &line_number_display_limit,
12638 "*Maximum buffer size for which line number should be displayed.\n\
12639 If the buffer is bigger than this, the line number does not appear\n\
12640 in the mode line.");
12641 line_number_display_limit = 1000000;
12642
12643 DEFVAR_INT ("line-number-display-limit-width", &line_number_display_limit_width,
12644 "*Maximum line width (in characters) for line number display.\n\
12645 If the average length of the lines near point is bigger than this, then the\n\
12646 line number may be omitted from the mode line.");
12647 line_number_display_limit_width = 200;
12648
12649 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
12650 "*Non-nil means highlight region even in nonselected windows.");
12651 highlight_nonselected_windows = 0;
12652
12653 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
12654 "Non-nil if more than one frame is visible on this display.\n\
12655 Minibuffer-only frames don't count, but iconified frames do.\n\
12656 This variable is not guaranteed to be accurate except while processing\n\
12657 `frame-title-format' and `icon-title-format'.");
12658
12659 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
12660 "Template for displaying the title bar of visible frames.\n\
12661 \(Assuming the window manager supports this feature.)\n\
12662 This variable has the same structure as `mode-line-format' (which see),\n\
12663 and is used only on frames for which no explicit name has been set\n\
12664 \(see `modify-frame-parameters').");
12665 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
12666 "Template for displaying the title bar of an iconified frame.\n\
12667 \(Assuming the window manager supports this feature.)\n\
12668 This variable has the same structure as `mode-line-format' (which see),\n\
12669 and is used only on frames for which no explicit name has been set\n\
12670 \(see `modify-frame-parameters').");
12671 Vicon_title_format
12672 = Vframe_title_format
12673 = Fcons (intern ("multiple-frames"),
12674 Fcons (build_string ("%b"),
12675 Fcons (Fcons (build_string (""),
12676 Fcons (intern ("invocation-name"),
12677 Fcons (build_string ("@"),
12678 Fcons (intern ("system-name"),
12679 Qnil)))),
12680 Qnil)));
12681
12682 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
12683 "Maximum number of lines to keep in the message log buffer.\n\
12684 If nil, disable message logging. If t, log messages but don't truncate\n\
12685 the buffer when it becomes large.");
12686 XSETFASTINT (Vmessage_log_max, 50);
12687
12688 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
12689 "Functions called before redisplay, if window sizes have changed.\n\
12690 The value should be a list of functions that take one argument.\n\
12691 Just before redisplay, for each frame, if any of its windows have changed\n\
12692 size since the last redisplay, or have been split or deleted,\n\
12693 all the functions in the list are called, with the frame as argument.");
12694 Vwindow_size_change_functions = Qnil;
12695
12696 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
12697 "List of Functions to call before redisplaying a window with scrolling.\n\
12698 Each function is called with two arguments, the window\n\
12699 and its new display-start position. Note that the value of `window-end'\n\
12700 is not valid when these functions are called.");
12701 Vwindow_scroll_functions = Qnil;
12702
12703 DEFVAR_BOOL ("auto-resize-toolbars", &auto_resize_toolbars_p,
12704 "*Non-nil means automatically resize toolbars.\n\
12705 This increases a toolbar's height if not all toolbar items are visible.\n\
12706 It decreases a toolbar's height when it would display blank lines\n\
12707 otherwise.");
12708 auto_resize_toolbars_p = 1;
12709
12710 DEFVAR_BOOL ("auto-raise-toolbar-buttons", &auto_raise_toolbar_buttons_p,
12711 "*Non-nil means raise toolbar buttons when the mouse moves over them.");
12712 auto_raise_toolbar_buttons_p = 1;
12713
12714 DEFVAR_INT ("toolbar-button-margin", &toolbar_button_margin,
12715 "*Margin around toolbar buttons in pixels.");
12716 toolbar_button_margin = 1;
12717
12718 DEFVAR_INT ("toolbar-button-relief", &toolbar_button_relief,
12719 "Relief thickness of toolbar buttons.");
12720 toolbar_button_relief = 3;
12721
12722 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
12723 "List of functions to call to fontify regions of text.\n\
12724 Each function is called with one argument POS. Functions must\n\
12725 fontify a region starting at POS in the current buffer, and give\n\
12726 fontified regions the property `fontified'.\n\
12727 This variable automatically becomes buffer-local when set.");
12728 Vfontification_functions = Qnil;
12729 Fmake_local_variable (Qfontification_functions);
12730
12731 DEFVAR_BOOL ("unibyte-display-via-language-environment",
12732 &unibyte_display_via_language_environment,
12733 "*Non-nil means display unibyte text according to language environment.\n\
12734 Specifically this means that unibyte non-ASCII characters\n\
12735 are displayed by converting them to the equivalent multibyte characters\n\
12736 according to the current language environment. As a result, they are\n\
12737 displayed according to the current fontset.");
12738 unibyte_display_via_language_environment = 0;
12739
12740 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
12741 "*Maximum height for resizing mini-windows.\n\
12742 If a float, it specifies a fraction of the mini-window frame's height.\n\
12743 If an integer, it specifies a number of lines.\n\
12744 If nil, don't resize.");
12745 Vmax_mini_window_height = make_float (0.25);
12746 }
12747
12748
12749 /* Initialize this module when Emacs starts. */
12750
12751 void
12752 init_xdisp ()
12753 {
12754 Lisp_Object root_window;
12755 struct window *mini_w;
12756
12757 CHARPOS (this_line_start_pos) = 0;
12758
12759 mini_w = XWINDOW (minibuf_window);
12760 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
12761
12762 if (!noninteractive)
12763 {
12764 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
12765 int i;
12766
12767 XSETFASTINT (XWINDOW (root_window)->top, FRAME_TOP_MARGIN (f));
12768 set_window_height (root_window,
12769 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
12770 0);
12771 XSETFASTINT (mini_w->top, FRAME_HEIGHT (f) - 1);
12772 set_window_height (minibuf_window, 1, 0);
12773
12774 XSETFASTINT (XWINDOW (root_window)->width, FRAME_WIDTH (f));
12775 XSETFASTINT (mini_w->width, FRAME_WIDTH (f));
12776
12777 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
12778 scratch_glyph_row.glyphs[TEXT_AREA + 1]
12779 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
12780
12781 /* The default ellipsis glyphs `...'. */
12782 for (i = 0; i < 3; ++i)
12783 XSETFASTINT (default_invis_vector[i], '.');
12784 }
12785
12786 #ifdef HAVE_WINDOW_SYSTEM
12787 {
12788 /* Allocate the buffer for frame titles. */
12789 int size = 100;
12790 frame_title_buf = (char *) xmalloc (size);
12791 frame_title_buf_end = frame_title_buf + size;
12792 frame_title_ptr = NULL;
12793 }
12794 #endif /* HAVE_WINDOW_SYSTEM */
12795 }
12796
12797