(redisplay_window): Really switch buffers when
[bpt/emacs.git] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 86, 87, 88, 93, 94, 95, 97, 98, 99
3 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
23
24 Redisplay.
25
26 Emacs separates the task of updating the display from code
27 modifying global state, e.g. buffer text. This way functions
28 operating on buffers don't also have to be concerned with updating
29 the display.
30
31 Updating the display is triggered by the Lisp interpreter when it
32 decides it's time to do it. This is done either automatically for
33 you as part of the interpreter's command loop or as the result of
34 calling Lisp functions like `sit-for'. The C function `redisplay'
35 in xdisp.c is the only entry into the inner redisplay code. (Or,
36 let's say almost---see the the description of direct update
37 operations, below.).
38
39 The following diagram shows how redisplay code is invoked. As you
40 can see, Lisp calls redisplay and vice versa. Under window systems
41 like X, some portions of the redisplay code are also called
42 asynchronously during mouse movement or expose events. It is very
43 important that these code parts do NOT use the C library (malloc,
44 free) because many C libraries under Unix are not reentrant. They
45 may also NOT call functions of the Lisp interpreter which could
46 change the interpreter's state. If you don't follow these rules,
47 you will encounter bugs which are very hard to explain.
48
49 (Direct functions, see below)
50 direct_output_for_insert,
51 direct_forward_char (dispnew.c)
52 +---------------------------------+
53 | |
54 | V
55 +--------------+ redisplay() +----------------+
56 | Lisp machine |---------------->| Redisplay code |<--+
57 +--------------+ (xdisp.c) +----------------+ |
58 ^ | |
59 +----------------------------------+ |
60 Don't use this path when called |
61 asynchronously! |
62 |
63 expose_window (asynchronous) |
64 |
65 X expose events -----+
66
67 What does redisplay? Obviously, it has to figure out somehow what
68 has been changed since the last time the display has been updated,
69 and to make these changes visible. Preferably it would do that in
70 a moderately intelligent way, i.e. fast.
71
72 Changes in buffer text can be deduced from window and buffer
73 structures, and from some global variables like `beg_unchanged' and
74 `end_unchanged'. The contents of the display are additionally
75 recorded in a `glyph matrix', a two-dimensional matrix of glyph
76 structures. Each row in such a matrix corresponds to a line on the
77 display, and each glyph in a row corresponds to a column displaying
78 a character, an image, or what else. This matrix is called the
79 `current glyph matrix' or `current matrix' in redisplay
80 terminology.
81
82 For buffer parts that have been changed since the last update, a
83 second glyph matrix is constructed, the so called `desired glyph
84 matrix' or short `desired matrix'. Current and desired matrix are
85 then compared to find a cheap way to update the display, e.g. by
86 reusing part of the display by scrolling lines.
87
88
89 Direct operations.
90
91 You will find a lot of of redisplay optimizations when you start
92 looking at the innards of redisplay. The overall goal of all these
93 optimizations is to make redisplay fast because it is done
94 frequently.
95
96 Two optimizations are not found in xdisp.c. These are the direct
97 operations mentioned above. As the name suggests they follow a
98 different principle than the rest of redisplay. Instead of
99 building a desired matrix and then comparing it with the current
100 display, they perform their actions directly on the display and on
101 the current matrix.
102
103 One direct operation updates the display after one character has
104 been entered. The other one moves the cursor by one position
105 forward or backward. You find these functions under the names
106 `direct_output_for_insert' and `direct_output_forward_char' in
107 dispnew.c.
108
109
110 Desired matrices.
111
112 Desired matrices are always built per Emacs window. The function
113 `display_line' is the central function to look at if you are
114 interested. It constructs one row in a desired matrix given an
115 iterator structure containing both a buffer position and a
116 description of the environment in which the text is to be
117 displayed. But this is too early, read on.
118
119 Characters and pixmaps displayed for a range of buffer text depend
120 on various settings of buffers and windows, on overlays and text
121 properties, on display tables, on selective display. The good news
122 is that all this hairy stuff is hidden behind a small set of
123 interface functions taking a iterator structure (struct it)
124 argument.
125
126 Iteration over things to be be displayed is then simple. It is
127 started by initializing an iterator with a call to init_iterator
128 (or init_string_iterator for that matter). Calls to
129 get_next_display_element fill the iterator structure with relevant
130 information about the next thing to display. Calls to
131 set_iterator_to_next move the iterator to the next thing.
132
133 Besides this, an iterator also contains information about the
134 display environment in which glyphs for display elements are to be
135 produced. It has fields for the width and height of the display,
136 the information whether long lines are truncated or continued, a
137 current X and Y position, and lots of other stuff you can better
138 see in dispextern.h.
139
140 Glyphs in a desired matrix are normally constructed in a loop
141 calling get_next_display_element and then produce_glyphs. The call
142 to produce_glyphs will fill the iterator structure with pixel
143 information about the element being displayed and at the same time
144 produce glyphs for it. If the display element fits on the line
145 being displayed, set_iterator_to_next is called next, otherwise the
146 glyphs produced are discarded.
147
148
149 Frame matrices.
150
151 That just couldn't be all, could it? What about terminal types not
152 supporting operations on sub-windows of the screen? To update the
153 display on such a terminal, window-based glyph matrices are not
154 well suited. To be able to reuse part of the display (scrolling
155 lines up and down), we must instead have a view of the whole
156 screen. This is what `frame matrices' are for. They are a trick.
157
158 Frames on terminals like above have a glyph pool. Windows on such
159 a frame sub-allocate their glyph memory from their frame's glyph
160 pool. The frame itself is given its own glyph matrices. By
161 coincidence---or maybe something else---rows in window glyph
162 matrices are slices of corresponding rows in frame matrices. Thus
163 writing to window matrices implicitly updates a frame matrix which
164 provides us with the view of the whole screen that we originally
165 wanted to have without having to move many bytes around. To be
166 honest, there is a little bit more done, but not much more. If you
167 plan to extend that code, take a look at dispnew.c. The function
168 build_frame_matrix is a good starting point. */
169
170 #include <config.h>
171 #include <stdio.h>
172 #include "lisp.h"
173 #include "frame.h"
174 #include "window.h"
175 #include "termchar.h"
176 #include "dispextern.h"
177 #include "buffer.h"
178 #include "charset.h"
179 #include "indent.h"
180 #include "commands.h"
181 #include "macros.h"
182 #include "disptab.h"
183 #include "termhooks.h"
184 #include "intervals.h"
185 #include "keyboard.h"
186 #include "coding.h"
187 #include "process.h"
188 #include "region-cache.h"
189
190 #ifdef HAVE_X_WINDOWS
191 #include "xterm.h"
192 #endif
193 #ifdef WINDOWSNT
194 #include "w32term.h"
195 #endif
196
197 #define min(a, b) ((a) < (b) ? (a) : (b))
198 #define max(a, b) ((a) > (b) ? (a) : (b))
199
200 #define INFINITY 10000000
201
202 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
203 extern void set_frame_menubar ();
204 extern int pending_menu_activation;
205 #endif
206
207 extern int interrupt_input;
208 extern int command_loop_level;
209
210 extern int minibuffer_auto_raise;
211
212 extern Lisp_Object Qface;
213
214 extern Lisp_Object Voverriding_local_map;
215 extern Lisp_Object Voverriding_local_map_menu_flag;
216 extern Lisp_Object Qmenu_item;
217
218 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
219 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
220 Lisp_Object Qredisplay_end_trigger_functions;
221 Lisp_Object Qinhibit_point_motion_hooks;
222 Lisp_Object QCeval, Qwhen, QCfile, QCdata;
223 Lisp_Object Qfontified;
224
225 /* Functions called to fontify regions of text. */
226
227 Lisp_Object Vfontification_functions;
228 Lisp_Object Qfontification_functions;
229
230 /* Non-zero means draw tool bar buttons raised when the mouse moves
231 over them. */
232
233 int auto_raise_tool_bar_buttons_p;
234
235 /* Margin around tool bar buttons in pixels. */
236
237 int tool_bar_button_margin;
238
239 /* Thickness of shadow to draw around tool bar buttons. */
240
241 int tool_bar_button_relief;
242
243 /* Non-zero means automatically resize tool-bars so that all tool-bar
244 items are visible, and no blank lines remain. */
245
246 int auto_resize_tool_bars_p;
247
248 /* Non-nil means don't actually do any redisplay. */
249
250 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
251
252 /* Names of text properties relevant for redisplay. */
253
254 Lisp_Object Qdisplay, Qrelative_width, Qalign_to;
255 extern Lisp_Object Qface, Qinvisible, Qimage, Qwidth;
256
257 /* Symbols used in text property values. */
258
259 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
260 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
261 Lisp_Object Qmargin;
262 extern Lisp_Object Qheight;
263
264 /* Non-nil means highlight trailing whitespace. */
265
266 Lisp_Object Vshow_trailing_whitespace;
267
268 /* Name of the face used to highlight trailing whitespace. */
269
270 Lisp_Object Qtrailing_whitespace;
271
272 /* The symbol `image' which is the car of the lists used to represent
273 images in Lisp. */
274
275 Lisp_Object Qimage;
276
277 /* Non-zero means print newline to stdout before next mini-buffer
278 message. */
279
280 int noninteractive_need_newline;
281
282 /* Non-zero means print newline to message log before next message. */
283
284 static int message_log_need_newline;
285
286 \f
287 /* The buffer position of the first character appearing entirely or
288 partially on the line of the selected window which contains the
289 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
290 redisplay optimization in redisplay_internal. */
291
292 static struct text_pos this_line_start_pos;
293
294 /* Number of characters past the end of the line above, including the
295 terminating newline. */
296
297 static struct text_pos this_line_end_pos;
298
299 /* The vertical positions and the height of this line. */
300
301 static int this_line_vpos;
302 static int this_line_y;
303 static int this_line_pixel_height;
304
305 /* X position at which this display line starts. Usually zero;
306 negative if first character is partially visible. */
307
308 static int this_line_start_x;
309
310 /* Buffer that this_line_.* variables are referring to. */
311
312 static struct buffer *this_line_buffer;
313
314 /* Nonzero means truncate lines in all windows less wide than the
315 frame. */
316
317 int truncate_partial_width_windows;
318
319 /* A flag to control how to display unibyte 8-bit character. */
320
321 int unibyte_display_via_language_environment;
322
323 /* Nonzero means we have more than one non-mini-buffer-only frame.
324 Not guaranteed to be accurate except while parsing
325 frame-title-format. */
326
327 int multiple_frames;
328
329 Lisp_Object Vglobal_mode_string;
330
331 /* Marker for where to display an arrow on top of the buffer text. */
332
333 Lisp_Object Voverlay_arrow_position;
334
335 /* String to display for the arrow. Only used on terminal frames. */
336
337 Lisp_Object Voverlay_arrow_string;
338
339 /* Values of those variables at last redisplay. However, if
340 Voverlay_arrow_position is a marker, last_arrow_position is its
341 numerical position. */
342
343 static Lisp_Object last_arrow_position, last_arrow_string;
344
345 /* Like mode-line-format, but for the title bar on a visible frame. */
346
347 Lisp_Object Vframe_title_format;
348
349 /* Like mode-line-format, but for the title bar on an iconified frame. */
350
351 Lisp_Object Vicon_title_format;
352
353 /* List of functions to call when a window's size changes. These
354 functions get one arg, a frame on which one or more windows' sizes
355 have changed. */
356
357 static Lisp_Object Vwindow_size_change_functions;
358
359 Lisp_Object Qmenu_bar_update_hook;
360
361 /* Nonzero if overlay arrow has been displayed once in this window. */
362
363 static int overlay_arrow_seen;
364
365 /* Nonzero means highlight the region even in nonselected windows. */
366
367 int highlight_nonselected_windows;
368
369 /* If cursor motion alone moves point off frame, try scrolling this
370 many lines up or down if that will bring it back. */
371
372 static int scroll_step;
373
374 /* Non-0 means scroll just far enough to bring point back on the
375 screen, when appropriate. */
376
377 static int scroll_conservatively;
378
379 /* Recenter the window whenever point gets within this many lines of
380 the top or bottom of the window. This value is translated into a
381 pixel value by multiplying it with CANON_Y_UNIT, which means that
382 there is really a fixed pixel height scroll margin. */
383
384 int scroll_margin;
385
386 /* Number of windows showing the buffer of the selected window (or
387 another buffer with the same base buffer). keyboard.c refers to
388 this. */
389
390 int buffer_shared;
391
392 /* Vector containing glyphs for an ellipsis `...'. */
393
394 static Lisp_Object default_invis_vector[3];
395
396 /* Nonzero means display mode line highlighted. */
397
398 int mode_line_inverse_video;
399
400 /* Prompt to display in front of the mini-buffer contents. */
401
402 Lisp_Object minibuf_prompt;
403
404 /* Width of current mini-buffer prompt. Only set after display_line
405 of the line that contains the prompt. */
406
407 int minibuf_prompt_width;
408 int minibuf_prompt_pixel_width;
409
410 /* This is the window where the echo area message was displayed. It
411 is always a mini-buffer window, but it may not be the same window
412 currently active as a mini-buffer. */
413
414 Lisp_Object echo_area_window;
415
416 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
417 pushes the current message and the value of
418 message_enable_multibyte on the stack, the function restore_message
419 pops the stack and displays MESSAGE again. */
420
421 Lisp_Object Vmessage_stack;
422
423 /* Nonzero means multibyte characters were enabled when the echo area
424 message was specified. */
425
426 int message_enable_multibyte;
427
428 /* True if we should redraw the mode lines on the next redisplay. */
429
430 int update_mode_lines;
431
432 /* Nonzero if window sizes or contents have changed since last
433 redisplay that finished */
434
435 int windows_or_buffers_changed;
436
437 /* Nonzero after display_mode_line if %l was used and it displayed a
438 line number. */
439
440 int line_number_displayed;
441
442 /* Maximum buffer size for which to display line numbers. */
443
444 static int line_number_display_limit;
445
446 /* line width to consider when repostioning for line number display */
447
448 static int line_number_display_limit_width;
449
450 /* Number of lines to keep in the message log buffer. t means
451 infinite. nil means don't log at all. */
452
453 Lisp_Object Vmessage_log_max;
454
455 /* Current, index 0, and last displayed echo area message. Either
456 buffers from echo_buffers, or nil to indicate no message. */
457
458 Lisp_Object echo_area_buffer[2];
459
460 /* The buffers referenced from echo_area_buffer. */
461
462 static Lisp_Object echo_buffer[2];
463
464 /* A vector saved used in with_area_buffer to reduce consing. */
465
466 static Lisp_Object Vwith_echo_area_save_vector;
467
468 /* Non-zero means display_echo_area should display the last echo area
469 message again. Set by redisplay_preserve_echo_area. */
470
471 static int display_last_displayed_message_p;
472
473 /* Nonzero if echo area is being used by print; zero if being used by
474 message. */
475
476 int message_buf_print;
477
478 /* Maximum height for resizing mini-windows. Either a float
479 specifying a fraction of the available height, or an integer
480 specifying a number of lines. */
481
482 static Lisp_Object Vmax_mini_window_height;
483
484 /* Non-zero means we want a hollow cursor in windows that are not
485 selected. Zero means there's no cursor in such windows. */
486
487 int cursor_in_non_selected_windows;
488
489 /* A scratch glyph row with contents used for generating truncation
490 glyphs. Also used in direct_output_for_insert. */
491
492 #define MAX_SCRATCH_GLYPHS 100
493 struct glyph_row scratch_glyph_row;
494 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
495
496 /* Ascent and height of the last line processed by move_it_to. */
497
498 static int last_max_ascent, last_height;
499
500 /* The maximum distance to look ahead for text properties. Values
501 that are too small let us call compute_char_face and similar
502 functions too often which is expensive. Values that are too large
503 let us call compute_char_face and alike too often because we
504 might not be interested in text properties that far away. */
505
506 #define TEXT_PROP_DISTANCE_LIMIT 100
507
508 /* Non-zero means print traces of redisplay if compiled with
509 GLYPH_DEBUG != 0. */
510
511 #if GLYPH_DEBUG
512 int trace_redisplay_p;
513 #endif
514
515 /* Non-zero means automatically scroll windows horizontally to make
516 point visible. */
517
518 int automatic_hscrolling_p;
519
520 /* A list of symbols, one for each supported image type. */
521
522 Lisp_Object Vimage_types;
523
524 /* Value returned from text property handlers (see below). */
525
526 enum prop_handled
527 {
528 HANDLED_NORMALLY,
529 HANDLED_RECOMPUTE_PROPS,
530 HANDLED_OVERLAY_STRING_CONSUMED,
531 HANDLED_RETURN
532 };
533
534 /* A description of text properties that redisplay is interested
535 in. */
536
537 struct props
538 {
539 /* The name of the property. */
540 Lisp_Object *name;
541
542 /* A unique index for the property. */
543 enum prop_idx idx;
544
545 /* A handler function called to set up iterator IT from the property
546 at IT's current position. Value is used to steer handle_stop. */
547 enum prop_handled (*handler) P_ ((struct it *it));
548 };
549
550 static enum prop_handled handle_face_prop P_ ((struct it *));
551 static enum prop_handled handle_invisible_prop P_ ((struct it *));
552 static enum prop_handled handle_display_prop P_ ((struct it *));
553 static enum prop_handled handle_composition_prop P_ ((struct it *));
554 static enum prop_handled handle_overlay_change P_ ((struct it *));
555 static enum prop_handled handle_fontified_prop P_ ((struct it *));
556
557 /* Properties handled by iterators. */
558
559 static struct props it_props[] =
560 {
561 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
562 /* Handle `face' before `display' because some sub-properties of
563 `display' need to know the face. */
564 {&Qface, FACE_PROP_IDX, handle_face_prop},
565 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
566 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
567 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
568 {NULL, 0, NULL}
569 };
570
571 /* Value is the position described by X. If X is a marker, value is
572 the marker_position of X. Otherwise, value is X. */
573
574 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
575
576 /* Enumeration returned by some move_it_.* functions internally. */
577
578 enum move_it_result
579 {
580 /* Not used. Undefined value. */
581 MOVE_UNDEFINED,
582
583 /* Move ended at the requested buffer position or ZV. */
584 MOVE_POS_MATCH_OR_ZV,
585
586 /* Move ended at the requested X pixel position. */
587 MOVE_X_REACHED,
588
589 /* Move within a line ended at the end of a line that must be
590 continued. */
591 MOVE_LINE_CONTINUED,
592
593 /* Move within a line ended at the end of a line that would
594 be displayed truncated. */
595 MOVE_LINE_TRUNCATED,
596
597 /* Move within a line ended at a line end. */
598 MOVE_NEWLINE_OR_CR
599 };
600
601
602 \f
603 /* Function prototypes. */
604
605 static void ensure_echo_area_buffers P_ ((void));
606 static struct glyph_row *row_containing_pos P_ ((struct window *, int,
607 struct glyph_row *,
608 struct glyph_row *));
609 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
610 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
611 static void clear_garbaged_frames P_ ((void));
612 static int current_message_1 P_ ((Lisp_Object *));
613 static int truncate_message_1 P_ ((int));
614 static int set_message_1 P_ ((char *s, Lisp_Object, int, int));
615 static int display_echo_area P_ ((struct window *));
616 static int display_echo_area_1 P_ ((struct window *));
617 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
618 static int string_char_and_length P_ ((unsigned char *, int, int *));
619 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
620 struct text_pos));
621 static int compute_window_start_on_continuation_line P_ ((struct window *));
622 static Lisp_Object eval_handler P_ ((Lisp_Object));
623 static Lisp_Object eval_form P_ ((Lisp_Object));
624 static void insert_left_trunc_glyphs P_ ((struct it *));
625 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
626 static void extend_face_to_end_of_line P_ ((struct it *));
627 static int append_space P_ ((struct it *, int));
628 static void make_cursor_line_fully_visible P_ ((struct window *));
629 static int try_scrolling P_ ((Lisp_Object, int, int, int, int));
630 static int trailing_whitespace_p P_ ((int));
631 static int message_log_check_duplicate P_ ((int, int, int, int));
632 int invisible_p P_ ((Lisp_Object, Lisp_Object));
633 int invisible_ellipsis_p P_ ((Lisp_Object, Lisp_Object));
634 static void push_it P_ ((struct it *));
635 static void pop_it P_ ((struct it *));
636 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
637 static void redisplay_internal P_ ((int));
638 static int echo_area_display P_ ((int));
639 static void redisplay_windows P_ ((Lisp_Object));
640 static void redisplay_window P_ ((Lisp_Object, int));
641 static void update_menu_bar P_ ((struct frame *, int));
642 static int try_window_reusing_current_matrix P_ ((struct window *));
643 static int try_window_id P_ ((struct window *));
644 static int display_line P_ ((struct it *));
645 static void display_mode_lines P_ ((struct window *));
646 static void display_mode_line P_ ((struct window *, enum face_id,
647 Lisp_Object));
648 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object));
649 static char *decode_mode_spec P_ ((struct window *, int, int, int));
650 static void display_menu_bar P_ ((struct window *));
651 static int display_count_lines P_ ((int, int, int, int, int *));
652 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
653 int, int, struct it *, int, int, int, int));
654 static void compute_line_metrics P_ ((struct it *));
655 static void run_redisplay_end_trigger_hook P_ ((struct it *));
656 static int get_overlay_strings P_ ((struct it *));
657 static void next_overlay_string P_ ((struct it *));
658 void set_iterator_to_next P_ ((struct it *));
659 static void reseat P_ ((struct it *, struct text_pos, int));
660 static void reseat_1 P_ ((struct it *, struct text_pos, int));
661 static void back_to_previous_visible_line_start P_ ((struct it *));
662 static void reseat_at_previous_visible_line_start P_ ((struct it *));
663 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
664 static int next_element_from_display_vector P_ ((struct it *));
665 static int next_element_from_string P_ ((struct it *));
666 static int next_element_from_c_string P_ ((struct it *));
667 static int next_element_from_buffer P_ ((struct it *));
668 static int next_element_from_composition P_ ((struct it *));
669 static int next_element_from_image P_ ((struct it *));
670 static int next_element_from_stretch P_ ((struct it *));
671 static void load_overlay_strings P_ ((struct it *));
672 static void init_from_display_pos P_ ((struct it *, struct window *,
673 struct display_pos *));
674 static void reseat_to_string P_ ((struct it *, unsigned char *,
675 Lisp_Object, int, int, int, int));
676 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
677 int, int, int));
678 void move_it_vertically_backward P_ ((struct it *, int));
679 static void init_to_row_start P_ ((struct it *, struct window *,
680 struct glyph_row *));
681 static void init_to_row_end P_ ((struct it *, struct window *,
682 struct glyph_row *));
683 static void back_to_previous_line_start P_ ((struct it *));
684 static void forward_to_next_line_start P_ ((struct it *));
685 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
686 Lisp_Object, int));
687 static struct text_pos string_pos P_ ((int, Lisp_Object));
688 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
689 static int number_of_chars P_ ((unsigned char *, int));
690 static void compute_stop_pos P_ ((struct it *));
691 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
692 Lisp_Object));
693 static int face_before_or_after_it_pos P_ ((struct it *, int));
694 static int next_overlay_change P_ ((int));
695 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
696 Lisp_Object, struct text_pos *));
697
698 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
699 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
700
701 #ifdef HAVE_WINDOW_SYSTEM
702
703 static void update_tool_bar P_ ((struct frame *, int));
704 static void build_desired_tool_bar_string P_ ((struct frame *f));
705 static int redisplay_tool_bar P_ ((struct frame *));
706 static void display_tool_bar_line P_ ((struct it *));
707
708 #endif /* HAVE_WINDOW_SYSTEM */
709
710 \f
711 /***********************************************************************
712 Window display dimensions
713 ***********************************************************************/
714
715 /* Return the window-relative maximum y + 1 for glyph rows displaying
716 text in window W. This is the height of W minus the height of a
717 mode line, if any. */
718
719 INLINE int
720 window_text_bottom_y (w)
721 struct window *w;
722 {
723 struct frame *f = XFRAME (w->frame);
724 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
725
726 if (WINDOW_WANTS_MODELINE_P (w))
727 height -= CURRENT_MODE_LINE_HEIGHT (w);
728 return height;
729 }
730
731
732 /* Return the pixel width of display area AREA of window W. AREA < 0
733 means return the total width of W, not including bitmap areas to
734 the left and right of the window. */
735
736 INLINE int
737 window_box_width (w, area)
738 struct window *w;
739 int area;
740 {
741 struct frame *f = XFRAME (w->frame);
742 int width = XFASTINT (w->width);
743
744 if (!w->pseudo_window_p)
745 {
746 width -= FRAME_SCROLL_BAR_WIDTH (f) + FRAME_FLAGS_AREA_COLS (f);
747
748 if (area == TEXT_AREA)
749 {
750 if (INTEGERP (w->left_margin_width))
751 width -= XFASTINT (w->left_margin_width);
752 if (INTEGERP (w->right_margin_width))
753 width -= XFASTINT (w->right_margin_width);
754 }
755 else if (area == LEFT_MARGIN_AREA)
756 width = (INTEGERP (w->left_margin_width)
757 ? XFASTINT (w->left_margin_width) : 0);
758 else if (area == RIGHT_MARGIN_AREA)
759 width = (INTEGERP (w->right_margin_width)
760 ? XFASTINT (w->right_margin_width) : 0);
761 }
762
763 return width * CANON_X_UNIT (f);
764 }
765
766
767 /* Return the pixel height of the display area of window W, not
768 including mode lines of W, if any.. */
769
770 INLINE int
771 window_box_height (w)
772 struct window *w;
773 {
774 struct frame *f = XFRAME (w->frame);
775 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
776
777 if (WINDOW_WANTS_MODELINE_P (w))
778 height -= CURRENT_MODE_LINE_HEIGHT (w);
779
780 if (WINDOW_WANTS_HEADER_LINE_P (w))
781 height -= CURRENT_HEADER_LINE_HEIGHT (w);
782
783 return height;
784 }
785
786
787 /* Return the frame-relative coordinate of the left edge of display
788 area AREA of window W. AREA < 0 means return the left edge of the
789 whole window, to the right of any bitmap area at the left side of
790 W. */
791
792 INLINE int
793 window_box_left (w, area)
794 struct window *w;
795 int area;
796 {
797 struct frame *f = XFRAME (w->frame);
798 int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
799
800 if (!w->pseudo_window_p)
801 {
802 x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
803 + FRAME_LEFT_FLAGS_AREA_WIDTH (f));
804
805 if (area == TEXT_AREA)
806 x += window_box_width (w, LEFT_MARGIN_AREA);
807 else if (area == RIGHT_MARGIN_AREA)
808 x += (window_box_width (w, LEFT_MARGIN_AREA)
809 + window_box_width (w, TEXT_AREA));
810 }
811
812 return x;
813 }
814
815
816 /* Return the frame-relative coordinate of the right edge of display
817 area AREA of window W. AREA < 0 means return the left edge of the
818 whole window, to the left of any bitmap area at the right side of
819 W. */
820
821 INLINE int
822 window_box_right (w, area)
823 struct window *w;
824 int area;
825 {
826 return window_box_left (w, area) + window_box_width (w, area);
827 }
828
829
830 /* Get the bounding box of the display area AREA of window W, without
831 mode lines, in frame-relative coordinates. AREA < 0 means the
832 whole window, not including bitmap areas to the left and right of
833 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
834 coordinates of the upper-left corner of the box. Return in
835 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
836
837 INLINE void
838 window_box (w, area, box_x, box_y, box_width, box_height)
839 struct window *w;
840 int area;
841 int *box_x, *box_y, *box_width, *box_height;
842 {
843 struct frame *f = XFRAME (w->frame);
844
845 *box_width = window_box_width (w, area);
846 *box_height = window_box_height (w);
847 *box_x = window_box_left (w, area);
848 *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
849 + XFASTINT (w->top) * CANON_Y_UNIT (f));
850 if (WINDOW_WANTS_HEADER_LINE_P (w))
851 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
852 }
853
854
855 /* Get the bounding box of the display area AREA of window W, without
856 mode lines. AREA < 0 means the whole window, not including bitmap
857 areas to the left and right of the window. Return in *TOP_LEFT_X
858 and TOP_LEFT_Y the frame-relative pixel coordinates of the
859 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
860 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
861 box. */
862
863 INLINE void
864 window_box_edges (w, area, top_left_x, top_left_y,
865 bottom_right_x, bottom_right_y)
866 struct window *w;
867 int area;
868 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
869 {
870 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
871 bottom_right_y);
872 *bottom_right_x += *top_left_x;
873 *bottom_right_y += *top_left_y;
874 }
875
876
877 \f
878 /***********************************************************************
879 Utilities
880 ***********************************************************************/
881
882 /* Return the next character from STR which is MAXLEN bytes long.
883 Return in *LEN the length of the character. This is like
884 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
885 we find one, we return a `?', but with the length of the invalid
886 character. */
887
888 static INLINE int
889 string_char_and_length (str, maxlen, len)
890 unsigned char *str;
891 int maxlen, *len;
892 {
893 int c;
894
895 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
896 if (!CHAR_VALID_P (c, 1))
897 /* We may not change the length here because other places in Emacs
898 don't use this function, i.e. they silently accept invalid
899 characters. */
900 c = '?';
901
902 return c;
903 }
904
905
906
907 /* Given a position POS containing a valid character and byte position
908 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
909
910 static struct text_pos
911 string_pos_nchars_ahead (pos, string, nchars)
912 struct text_pos pos;
913 Lisp_Object string;
914 int nchars;
915 {
916 xassert (STRINGP (string) && nchars >= 0);
917
918 if (STRING_MULTIBYTE (string))
919 {
920 int rest = STRING_BYTES (XSTRING (string)) - BYTEPOS (pos);
921 unsigned char *p = XSTRING (string)->data + BYTEPOS (pos);
922 int len;
923
924 while (nchars--)
925 {
926 string_char_and_length (p, rest, &len);
927 p += len, rest -= len;
928 xassert (rest >= 0);
929 CHARPOS (pos) += 1;
930 BYTEPOS (pos) += len;
931 }
932 }
933 else
934 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
935
936 return pos;
937 }
938
939
940 /* Value is the text position, i.e. character and byte position,
941 for character position CHARPOS in STRING. */
942
943 static INLINE struct text_pos
944 string_pos (charpos, string)
945 int charpos;
946 Lisp_Object string;
947 {
948 struct text_pos pos;
949 xassert (STRINGP (string));
950 xassert (charpos >= 0);
951 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
952 return pos;
953 }
954
955
956 /* Value is a text position, i.e. character and byte position, for
957 character position CHARPOS in C string S. MULTIBYTE_P non-zero
958 means recognize multibyte characters. */
959
960 static struct text_pos
961 c_string_pos (charpos, s, multibyte_p)
962 int charpos;
963 unsigned char *s;
964 int multibyte_p;
965 {
966 struct text_pos pos;
967
968 xassert (s != NULL);
969 xassert (charpos >= 0);
970
971 if (multibyte_p)
972 {
973 int rest = strlen (s), len;
974
975 SET_TEXT_POS (pos, 0, 0);
976 while (charpos--)
977 {
978 string_char_and_length (s, rest, &len);
979 s += len, rest -= len;
980 xassert (rest >= 0);
981 CHARPOS (pos) += 1;
982 BYTEPOS (pos) += len;
983 }
984 }
985 else
986 SET_TEXT_POS (pos, charpos, charpos);
987
988 return pos;
989 }
990
991
992 /* Value is the number of characters in C string S. MULTIBYTE_P
993 non-zero means recognize multibyte characters. */
994
995 static int
996 number_of_chars (s, multibyte_p)
997 unsigned char *s;
998 int multibyte_p;
999 {
1000 int nchars;
1001
1002 if (multibyte_p)
1003 {
1004 int rest = strlen (s), len;
1005 unsigned char *p = (unsigned char *) s;
1006
1007 for (nchars = 0; rest > 0; ++nchars)
1008 {
1009 string_char_and_length (p, rest, &len);
1010 rest -= len, p += len;
1011 }
1012 }
1013 else
1014 nchars = strlen (s);
1015
1016 return nchars;
1017 }
1018
1019
1020 /* Compute byte position NEWPOS->bytepos corresponding to
1021 NEWPOS->charpos. POS is a known position in string STRING.
1022 NEWPOS->charpos must be >= POS.charpos. */
1023
1024 static void
1025 compute_string_pos (newpos, pos, string)
1026 struct text_pos *newpos, pos;
1027 Lisp_Object string;
1028 {
1029 xassert (STRINGP (string));
1030 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1031
1032 if (STRING_MULTIBYTE (string))
1033 *newpos = string_pos_nchars_ahead (pos, string,
1034 CHARPOS (*newpos) - CHARPOS (pos));
1035 else
1036 BYTEPOS (*newpos) = CHARPOS (*newpos);
1037 }
1038
1039
1040 \f
1041 /***********************************************************************
1042 Lisp form evaluation
1043 ***********************************************************************/
1044
1045 /* Error handler for eval_form. */
1046
1047 static Lisp_Object
1048 eval_handler (arg)
1049 Lisp_Object arg;
1050 {
1051 return Qnil;
1052 }
1053
1054
1055 /* Evaluate SEXPR and return the result, or nil if something went
1056 wrong. */
1057
1058 static Lisp_Object
1059 eval_form (sexpr)
1060 Lisp_Object sexpr;
1061 {
1062 int count = specpdl_ptr - specpdl;
1063 Lisp_Object val;
1064 specbind (Qinhibit_redisplay, Qt);
1065 val = internal_condition_case_1 (Feval, sexpr, Qerror, eval_handler);
1066 return unbind_to (count, val);
1067 }
1068
1069
1070 \f
1071 /***********************************************************************
1072 Debugging
1073 ***********************************************************************/
1074
1075 #if 0
1076
1077 /* Define CHECK_IT to perform sanity checks on iterators.
1078 This is for debugging. It is too slow to do unconditionally. */
1079
1080 static void
1081 check_it (it)
1082 struct it *it;
1083 {
1084 if (it->method == next_element_from_string)
1085 {
1086 xassert (STRINGP (it->string));
1087 xassert (IT_STRING_CHARPOS (*it) >= 0);
1088 }
1089 else if (it->method == next_element_from_buffer)
1090 {
1091 /* Check that character and byte positions agree. */
1092 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1093 }
1094
1095 if (it->dpvec)
1096 xassert (it->current.dpvec_index >= 0);
1097 else
1098 xassert (it->current.dpvec_index < 0);
1099 }
1100
1101 #define CHECK_IT(IT) check_it ((IT))
1102
1103 #else /* not 0 */
1104
1105 #define CHECK_IT(IT) (void) 0
1106
1107 #endif /* not 0 */
1108
1109
1110 #if GLYPH_DEBUG
1111
1112 /* Check that the window end of window W is what we expect it
1113 to be---the last row in the current matrix displaying text. */
1114
1115 static void
1116 check_window_end (w)
1117 struct window *w;
1118 {
1119 if (!MINI_WINDOW_P (w)
1120 && !NILP (w->window_end_valid))
1121 {
1122 struct glyph_row *row;
1123 xassert ((row = MATRIX_ROW (w->current_matrix,
1124 XFASTINT (w->window_end_vpos)),
1125 !row->enabled_p
1126 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1127 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1128 }
1129 }
1130
1131 #define CHECK_WINDOW_END(W) check_window_end ((W))
1132
1133 #else /* not GLYPH_DEBUG */
1134
1135 #define CHECK_WINDOW_END(W) (void) 0
1136
1137 #endif /* not GLYPH_DEBUG */
1138
1139
1140 \f
1141 /***********************************************************************
1142 Iterator initialization
1143 ***********************************************************************/
1144
1145 /* Initialize IT for displaying current_buffer in window W, starting
1146 at character position CHARPOS. CHARPOS < 0 means that no buffer
1147 position is specified which is useful when the iterator is assigned
1148 a position later. BYTEPOS is the byte position corresponding to
1149 CHARPOS. BYTEPOS <= 0 means compute it from CHARPOS.
1150
1151 If ROW is not null, calls to produce_glyphs with IT as parameter
1152 will produce glyphs in that row.
1153
1154 BASE_FACE_ID is the id of a base face to use. It must be one of
1155 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID or
1156 HEADER_LINE_FACE_ID for displaying mode lines, or TOOL_BAR_FACE_ID for
1157 displaying the tool-bar.
1158
1159 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID or
1160 HEADER_LINE_FACE_ID, the iterator will be initialized to use the
1161 corresponding mode line glyph row of the desired matrix of W. */
1162
1163 void
1164 init_iterator (it, w, charpos, bytepos, row, base_face_id)
1165 struct it *it;
1166 struct window *w;
1167 int charpos, bytepos;
1168 struct glyph_row *row;
1169 enum face_id base_face_id;
1170 {
1171 int highlight_region_p;
1172
1173 /* Some precondition checks. */
1174 xassert (w != NULL && it != NULL);
1175 xassert (charpos < 0 || (charpos > 0 && charpos <= ZV));
1176
1177 /* If face attributes have been changed since the last redisplay,
1178 free realized faces now because they depend on face definitions
1179 that might have changed. */
1180 if (face_change_count)
1181 {
1182 face_change_count = 0;
1183 free_all_realized_faces (Qnil);
1184 }
1185
1186 /* Use one of the mode line rows of W's desired matrix if
1187 appropriate. */
1188 if (row == NULL)
1189 {
1190 if (base_face_id == MODE_LINE_FACE_ID)
1191 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
1192 else if (base_face_id == HEADER_LINE_FACE_ID)
1193 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
1194 }
1195
1196 /* Clear IT. */
1197 bzero (it, sizeof *it);
1198 it->current.overlay_string_index = -1;
1199 it->current.dpvec_index = -1;
1200 it->base_face_id = base_face_id;
1201
1202 /* The window in which we iterate over current_buffer: */
1203 XSETWINDOW (it->window, w);
1204 it->w = w;
1205 it->f = XFRAME (w->frame);
1206
1207 /* Extra space between lines (on window systems only). */
1208 if (base_face_id == DEFAULT_FACE_ID
1209 && FRAME_WINDOW_P (it->f))
1210 {
1211 if (NATNUMP (current_buffer->extra_line_spacing))
1212 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
1213 else if (it->f->extra_line_spacing > 0)
1214 it->extra_line_spacing = it->f->extra_line_spacing;
1215 }
1216
1217 /* If realized faces have been removed, e.g. because of face
1218 attribute changes of named faces, recompute them. */
1219 if (FRAME_FACE_CACHE (it->f)->used == 0)
1220 recompute_basic_faces (it->f);
1221
1222 /* Current value of the `space-width', and 'height' properties. */
1223 it->space_width = Qnil;
1224 it->font_height = Qnil;
1225
1226 /* Are control characters displayed as `^C'? */
1227 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
1228
1229 /* -1 means everything between a CR and the following line end
1230 is invisible. >0 means lines indented more than this value are
1231 invisible. */
1232 it->selective = (INTEGERP (current_buffer->selective_display)
1233 ? XFASTINT (current_buffer->selective_display)
1234 : (!NILP (current_buffer->selective_display)
1235 ? -1 : 0));
1236 it->selective_display_ellipsis_p
1237 = !NILP (current_buffer->selective_display_ellipses);
1238
1239 /* Display table to use. */
1240 it->dp = window_display_table (w);
1241
1242 /* Are multibyte characters enabled in current_buffer? */
1243 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1244
1245 /* Non-zero if we should highlight the region. */
1246 highlight_region_p
1247 = (!NILP (Vtransient_mark_mode)
1248 && !NILP (current_buffer->mark_active)
1249 && XMARKER (current_buffer->mark)->buffer != 0);
1250
1251 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
1252 start and end of a visible region in window IT->w. Set both to
1253 -1 to indicate no region. */
1254 if (highlight_region_p
1255 /* Maybe highlight only in selected window. */
1256 && (/* Either show region everywhere. */
1257 highlight_nonselected_windows
1258 /* Or show region in the selected window. */
1259 || w == XWINDOW (selected_window)
1260 /* Or show the region if we are in the mini-buffer and W is
1261 the window the mini-buffer refers to. */
1262 || (MINI_WINDOW_P (XWINDOW (selected_window))
1263 && w == XWINDOW (Vminibuf_scroll_window))))
1264 {
1265 int charpos = marker_position (current_buffer->mark);
1266 it->region_beg_charpos = min (PT, charpos);
1267 it->region_end_charpos = max (PT, charpos);
1268 }
1269 else
1270 it->region_beg_charpos = it->region_end_charpos = -1;
1271
1272 /* Get the position at which the redisplay_end_trigger hook should
1273 be run, if it is to be run at all. */
1274 if (MARKERP (w->redisplay_end_trigger)
1275 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
1276 it->redisplay_end_trigger_charpos
1277 = marker_position (w->redisplay_end_trigger);
1278 else if (INTEGERP (w->redisplay_end_trigger))
1279 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
1280
1281 /* Correct bogus values of tab_width. */
1282 it->tab_width = XINT (current_buffer->tab_width);
1283 if (it->tab_width <= 0 || it->tab_width > 1000)
1284 it->tab_width = 8;
1285
1286 /* Are lines in the display truncated? */
1287 it->truncate_lines_p
1288 = (base_face_id != DEFAULT_FACE_ID
1289 || XINT (it->w->hscroll)
1290 || (truncate_partial_width_windows
1291 && !WINDOW_FULL_WIDTH_P (it->w))
1292 || !NILP (current_buffer->truncate_lines));
1293
1294 /* Get dimensions of truncation and continuation glyphs. These are
1295 displayed as bitmaps under X, so we don't need them for such
1296 frames. */
1297 if (!FRAME_WINDOW_P (it->f))
1298 {
1299 if (it->truncate_lines_p)
1300 {
1301 /* We will need the truncation glyph. */
1302 xassert (it->glyph_row == NULL);
1303 produce_special_glyphs (it, IT_TRUNCATION);
1304 it->truncation_pixel_width = it->pixel_width;
1305 }
1306 else
1307 {
1308 /* We will need the continuation glyph. */
1309 xassert (it->glyph_row == NULL);
1310 produce_special_glyphs (it, IT_CONTINUATION);
1311 it->continuation_pixel_width = it->pixel_width;
1312 }
1313
1314 /* Reset these values to zero becaue the produce_special_glyphs
1315 above has changed them. */
1316 it->pixel_width = it->ascent = it->descent = 0;
1317 it->phys_ascent = it->phys_descent = 0;
1318 }
1319
1320 /* Set this after getting the dimensions of truncation and
1321 continuation glyphs, so that we don't produce glyphs when calling
1322 produce_special_glyphs, above. */
1323 it->glyph_row = row;
1324 it->area = TEXT_AREA;
1325
1326 /* Get the dimensions of the display area. The display area
1327 consists of the visible window area plus a horizontally scrolled
1328 part to the left of the window. All x-values are relative to the
1329 start of this total display area. */
1330 if (base_face_id != DEFAULT_FACE_ID)
1331 {
1332 /* Mode lines, menu bar in terminal frames. */
1333 it->first_visible_x = 0;
1334 it->last_visible_x = XFASTINT (w->width) * CANON_X_UNIT (it->f);
1335 }
1336 else
1337 {
1338 it->first_visible_x
1339 = XFASTINT (it->w->hscroll) * CANON_X_UNIT (it->f);
1340 it->last_visible_x = (it->first_visible_x
1341 + window_box_width (w, TEXT_AREA));
1342
1343 /* If we truncate lines, leave room for the truncator glyph(s) at
1344 the right margin. Otherwise, leave room for the continuation
1345 glyph(s). Truncation and continuation glyphs are not inserted
1346 for window-based redisplay. */
1347 if (!FRAME_WINDOW_P (it->f))
1348 {
1349 if (it->truncate_lines_p)
1350 it->last_visible_x -= it->truncation_pixel_width;
1351 else
1352 it->last_visible_x -= it->continuation_pixel_width;
1353 }
1354
1355 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
1356 it->current_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w) + w->vscroll;
1357 }
1358
1359 /* Leave room for a border glyph. */
1360 if (!FRAME_WINDOW_P (it->f)
1361 && !WINDOW_RIGHTMOST_P (it->w))
1362 it->last_visible_x -= 1;
1363
1364 it->last_visible_y = window_text_bottom_y (w);
1365
1366 /* For mode lines and alike, arrange for the first glyph having a
1367 left box line if the face specifies a box. */
1368 if (base_face_id != DEFAULT_FACE_ID)
1369 {
1370 struct face *face;
1371
1372 it->face_id = base_face_id;
1373
1374 /* If we have a boxed mode line, make the first character appear
1375 with a left box line. */
1376 face = FACE_FROM_ID (it->f, base_face_id);
1377 if (face->box != FACE_NO_BOX)
1378 it->start_of_box_run_p = 1;
1379 }
1380
1381 /* If a buffer position was specified, set the iterator there,
1382 getting overlays and face properties from that position. */
1383 if (charpos > 0)
1384 {
1385 it->end_charpos = ZV;
1386 it->face_id = -1;
1387 IT_CHARPOS (*it) = charpos;
1388
1389 /* Compute byte position if not specified. */
1390 if (bytepos <= 0)
1391 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
1392 else
1393 IT_BYTEPOS (*it) = bytepos;
1394
1395 /* Compute faces etc. */
1396 reseat (it, it->current.pos, 1);
1397 }
1398
1399 CHECK_IT (it);
1400 }
1401
1402
1403 /* Initialize IT for the display of window W with window start POS. */
1404
1405 void
1406 start_display (it, w, pos)
1407 struct it *it;
1408 struct window *w;
1409 struct text_pos pos;
1410 {
1411 int start_at_line_beg_p;
1412 struct glyph_row *row;
1413 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
1414 int first_y;
1415
1416 row = w->desired_matrix->rows + first_vpos;
1417 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
1418 first_y = it->current_y;
1419
1420 /* If window start is not at a line start, move back to the line
1421 start. This makes sure that we take continuation lines into
1422 account. */
1423 start_at_line_beg_p = (CHARPOS (pos) == BEGV
1424 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
1425 if (!start_at_line_beg_p)
1426 reseat_at_previous_visible_line_start (it);
1427
1428 /* If window start is not at a line start, skip forward to POS to
1429 get the correct continuation_lines_width and current_x. */
1430 if (!start_at_line_beg_p)
1431 {
1432 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
1433
1434 /* If lines are continued, this line may end in the middle of a
1435 multi-glyph character (e.g. a control character displayed as
1436 \003, or in the middle of an overlay string). In this case
1437 move_it_to above will not have taken us to the start of
1438 the continuation line but to the end of the continued line. */
1439 if (!it->truncate_lines_p && it->current_x > 0)
1440 {
1441 if (it->current.dpvec_index >= 0
1442 || it->current.overlay_string_index >= 0)
1443 {
1444 set_iterator_to_next (it);
1445 move_it_in_display_line_to (it, -1, -1, 0);
1446 }
1447 it->continuation_lines_width += it->current_x;
1448 }
1449
1450 it->current_y = first_y;
1451 it->vpos = 0;
1452 it->current_x = it->hpos = 0;
1453 }
1454
1455 #if 0 /* Don't assert the following because start_display is sometimes
1456 called intentionally with a window start that is not at a
1457 line start. Please leave this code in as a comment. */
1458
1459 /* Window start should be on a line start, now. */
1460 xassert (it->continuation_lines_width
1461 || IT_CHARPOS (it) == BEGV
1462 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
1463 #endif /* 0 */
1464 }
1465
1466
1467 /* Initialize IT for stepping through current_buffer in window W,
1468 starting at position POS that includes overlay string and display
1469 vector/ control character translation position information. */
1470
1471 static void
1472 init_from_display_pos (it, w, pos)
1473 struct it *it;
1474 struct window *w;
1475 struct display_pos *pos;
1476 {
1477 /* Keep in mind: the call to reseat in init_iterator skips invisible
1478 text, so we might end up at a position different from POS. This
1479 is only a problem when POS is a row start after a newline and an
1480 overlay starts there with an after-string, and the overlay has an
1481 invisible property. Since we don't skip invisible text in
1482 display_line and elsewhere immediately after consuming the
1483 newline before the row start, such a POS will not be in a string,
1484 but the call to init_iterator below will move us to the
1485 after-string. */
1486 init_iterator (it, w, CHARPOS (pos->pos), BYTEPOS (pos->pos),
1487 NULL, DEFAULT_FACE_ID);
1488
1489 /* If position is within an overlay string, set up IT to
1490 the right overlay string. */
1491 if (pos->overlay_string_index >= 0)
1492 {
1493 int relative_index;
1494
1495 /* We already have the first chunk of overlay strings in
1496 IT->overlay_strings. Load more until the one for
1497 pos->overlay_string_index is in IT->overlay_strings. */
1498 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
1499 {
1500 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
1501 it->current.overlay_string_index = 0;
1502 while (n--)
1503 {
1504 load_overlay_strings (it);
1505 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
1506 }
1507 }
1508
1509 it->current.overlay_string_index = pos->overlay_string_index;
1510 relative_index = (it->current.overlay_string_index
1511 % OVERLAY_STRING_CHUNK_SIZE);
1512 it->string = it->overlay_strings[relative_index];
1513 it->current.string_pos = pos->string_pos;
1514 it->method = next_element_from_string;
1515 }
1516 else if (CHARPOS (pos->string_pos) >= 0)
1517 {
1518 /* Recorded position is not in an overlay string, but in another
1519 string. This can only be a string from a `display' property.
1520 IT should already be filled with that string. */
1521 it->current.string_pos = pos->string_pos;
1522 xassert (STRINGP (it->string));
1523 }
1524
1525 /* Restore position in display vector translations or control
1526 character translations. */
1527 if (pos->dpvec_index >= 0)
1528 {
1529 /* This fills IT->dpvec. */
1530 get_next_display_element (it);
1531 xassert (it->dpvec && it->current.dpvec_index == 0);
1532 it->current.dpvec_index = pos->dpvec_index;
1533 }
1534
1535 CHECK_IT (it);
1536 }
1537
1538
1539 /* Initialize IT for stepping through current_buffer in window W
1540 starting at ROW->start. */
1541
1542 static void
1543 init_to_row_start (it, w, row)
1544 struct it *it;
1545 struct window *w;
1546 struct glyph_row *row;
1547 {
1548 init_from_display_pos (it, w, &row->start);
1549 it->continuation_lines_width = row->continuation_lines_width;
1550 CHECK_IT (it);
1551 }
1552
1553
1554 /* Initialize IT for stepping through current_buffer in window W
1555 starting in the line following ROW, i.e. starting at ROW->end. */
1556
1557 static void
1558 init_to_row_end (it, w, row)
1559 struct it *it;
1560 struct window *w;
1561 struct glyph_row *row;
1562 {
1563 init_from_display_pos (it, w, &row->end);
1564
1565 if (row->continued_p)
1566 it->continuation_lines_width = (row->continuation_lines_width
1567 + row->pixel_width);
1568 CHECK_IT (it);
1569 }
1570
1571
1572
1573 \f
1574 /***********************************************************************
1575 Text properties
1576 ***********************************************************************/
1577
1578 /* Called when IT reaches IT->stop_charpos. Handle text property and
1579 overlay changes. Set IT->stop_charpos to the next position where
1580 to stop. */
1581
1582 static void
1583 handle_stop (it)
1584 struct it *it;
1585 {
1586 enum prop_handled handled;
1587 int handle_overlay_change_p = 1;
1588 struct props *p;
1589
1590 it->dpvec = NULL;
1591 it->current.dpvec_index = -1;
1592
1593 do
1594 {
1595 handled = HANDLED_NORMALLY;
1596
1597 /* Call text property handlers. */
1598 for (p = it_props; p->handler; ++p)
1599 {
1600 handled = p->handler (it);
1601
1602 if (handled == HANDLED_RECOMPUTE_PROPS)
1603 break;
1604 else if (handled == HANDLED_RETURN)
1605 return;
1606 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
1607 handle_overlay_change_p = 0;
1608 }
1609
1610 if (handled != HANDLED_RECOMPUTE_PROPS)
1611 {
1612 /* Don't check for overlay strings below when set to deliver
1613 characters from a display vector. */
1614 if (it->method == next_element_from_display_vector)
1615 handle_overlay_change_p = 0;
1616
1617 /* Handle overlay changes. */
1618 if (handle_overlay_change_p)
1619 handled = handle_overlay_change (it);
1620
1621 /* Determine where to stop next. */
1622 if (handled == HANDLED_NORMALLY)
1623 compute_stop_pos (it);
1624 }
1625 }
1626 while (handled == HANDLED_RECOMPUTE_PROPS);
1627 }
1628
1629
1630 /* Compute IT->stop_charpos from text property and overlay change
1631 information for IT's current position. */
1632
1633 static void
1634 compute_stop_pos (it)
1635 struct it *it;
1636 {
1637 register INTERVAL iv, next_iv;
1638 Lisp_Object object, limit, position;
1639
1640 /* If nowhere else, stop at the end. */
1641 it->stop_charpos = it->end_charpos;
1642
1643 if (STRINGP (it->string))
1644 {
1645 /* Strings are usually short, so don't limit the search for
1646 properties. */
1647 object = it->string;
1648 limit = Qnil;
1649 XSETFASTINT (position, IT_STRING_CHARPOS (*it));
1650 }
1651 else
1652 {
1653 int charpos;
1654
1655 /* If next overlay change is in front of the current stop pos
1656 (which is IT->end_charpos), stop there. Note: value of
1657 next_overlay_change is point-max if no overlay change
1658 follows. */
1659 charpos = next_overlay_change (IT_CHARPOS (*it));
1660 if (charpos < it->stop_charpos)
1661 it->stop_charpos = charpos;
1662
1663 /* If showing the region, we have to stop at the region
1664 start or end because the face might change there. */
1665 if (it->region_beg_charpos > 0)
1666 {
1667 if (IT_CHARPOS (*it) < it->region_beg_charpos)
1668 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
1669 else if (IT_CHARPOS (*it) < it->region_end_charpos)
1670 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
1671 }
1672
1673 /* Set up variables for computing the stop position from text
1674 property changes. */
1675 XSETBUFFER (object, current_buffer);
1676 XSETFASTINT (limit, IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
1677 XSETFASTINT (position, IT_CHARPOS (*it));
1678
1679 }
1680
1681 /* Get the interval containing IT's position. Value is a null
1682 interval if there isn't such an interval. */
1683 iv = validate_interval_range (object, &position, &position, 0);
1684 if (!NULL_INTERVAL_P (iv))
1685 {
1686 Lisp_Object values_here[LAST_PROP_IDX];
1687 struct props *p;
1688
1689 /* Get properties here. */
1690 for (p = it_props; p->handler; ++p)
1691 values_here[p->idx] = textget (iv->plist, *p->name);
1692
1693 /* Look for an interval following iv that has different
1694 properties. */
1695 for (next_iv = next_interval (iv);
1696 (!NULL_INTERVAL_P (next_iv)
1697 && (NILP (limit)
1698 || XFASTINT (limit) > next_iv->position));
1699 next_iv = next_interval (next_iv))
1700 {
1701 for (p = it_props; p->handler; ++p)
1702 {
1703 Lisp_Object new_value;
1704
1705 new_value = textget (next_iv->plist, *p->name);
1706 if (!EQ (values_here[p->idx], new_value))
1707 break;
1708 }
1709
1710 if (p->handler)
1711 break;
1712 }
1713
1714 if (!NULL_INTERVAL_P (next_iv))
1715 {
1716 if (INTEGERP (limit)
1717 && next_iv->position >= XFASTINT (limit))
1718 /* No text property change up to limit. */
1719 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
1720 else
1721 /* Text properties change in next_iv. */
1722 it->stop_charpos = min (it->stop_charpos, next_iv->position);
1723 }
1724 }
1725
1726 xassert (STRINGP (it->string)
1727 || (it->stop_charpos >= BEGV
1728 && it->stop_charpos >= IT_CHARPOS (*it)));
1729 }
1730
1731
1732 /* Return the position of the next overlay change after POS in
1733 current_buffer. Value is point-max if no overlay change
1734 follows. This is like `next-overlay-change' but doesn't use
1735 xmalloc. */
1736
1737 static int
1738 next_overlay_change (pos)
1739 int pos;
1740 {
1741 int noverlays;
1742 int endpos;
1743 Lisp_Object *overlays;
1744 int len;
1745 int i;
1746
1747 /* Get all overlays at the given position. */
1748 len = 10;
1749 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
1750 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL);
1751 if (noverlays > len)
1752 {
1753 len = noverlays;
1754 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
1755 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL);
1756 }
1757
1758 /* If any of these overlays ends before endpos,
1759 use its ending point instead. */
1760 for (i = 0; i < noverlays; ++i)
1761 {
1762 Lisp_Object oend;
1763 int oendpos;
1764
1765 oend = OVERLAY_END (overlays[i]);
1766 oendpos = OVERLAY_POSITION (oend);
1767 endpos = min (endpos, oendpos);
1768 }
1769
1770 return endpos;
1771 }
1772
1773
1774 \f
1775 /***********************************************************************
1776 Fontification
1777 ***********************************************************************/
1778
1779 /* Handle changes in the `fontified' property of the current buffer by
1780 calling hook functions from Qfontification_functions to fontify
1781 regions of text. */
1782
1783 static enum prop_handled
1784 handle_fontified_prop (it)
1785 struct it *it;
1786 {
1787 Lisp_Object prop, pos;
1788 enum prop_handled handled = HANDLED_NORMALLY;
1789 struct gcpro gcpro1;
1790
1791 /* Get the value of the `fontified' property at IT's current buffer
1792 position. (The `fontified' property doesn't have a special
1793 meaning in strings.) If the value is nil, call functions from
1794 Qfontification_functions. */
1795 if (!STRINGP (it->string)
1796 && it->s == NULL
1797 && !NILP (Vfontification_functions)
1798 && (pos = make_number (IT_CHARPOS (*it)),
1799 prop = Fget_char_property (pos, Qfontified, Qnil),
1800 NILP (prop)))
1801 {
1802 Lisp_Object args[2];
1803
1804 GCPRO1 (pos);
1805 /* Run the hook functions. */
1806 args[0] = Qfontification_functions;
1807 args[1] = pos;
1808 Frun_hook_with_args (2, args);
1809
1810 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
1811 something. This avoids an endless loop if they failed to
1812 fontify the text for which reason ever. */
1813 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
1814 handled = HANDLED_RECOMPUTE_PROPS;
1815 UNGCPRO;
1816 }
1817
1818 return handled;
1819 }
1820
1821
1822 \f
1823 /***********************************************************************
1824 Faces
1825 ***********************************************************************/
1826
1827 /* Set up iterator IT from face properties at its current position.
1828 Called from handle_stop. */
1829
1830 static enum prop_handled
1831 handle_face_prop (it)
1832 struct it *it;
1833 {
1834 int new_face_id, next_stop;
1835
1836 if (!STRINGP (it->string))
1837 {
1838 new_face_id
1839 = face_at_buffer_position (it->w,
1840 IT_CHARPOS (*it),
1841 it->region_beg_charpos,
1842 it->region_end_charpos,
1843 &next_stop,
1844 (IT_CHARPOS (*it)
1845 + TEXT_PROP_DISTANCE_LIMIT),
1846 0);
1847
1848 /* Is this a start of a run of characters with box face?
1849 Caveat: this can be called for a freshly initialized
1850 iterator; face_id is -1 is this case. We know that the new
1851 face will not change until limit, i.e. if the new face has a
1852 box, all characters up to limit will have one. But, as
1853 usual, we don't know whether limit is really the end. */
1854 if (new_face_id != it->face_id)
1855 {
1856 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
1857
1858 /* If new face has a box but old face has not, this is
1859 the start of a run of characters with box, i.e. it has
1860 a shadow on the left side. The value of face_id of the
1861 iterator will be -1 if this is the initial call that gets
1862 the face. In this case, we have to look in front of IT's
1863 position and see whether there is a face != new_face_id. */
1864 it->start_of_box_run_p
1865 = (new_face->box != FACE_NO_BOX
1866 && (it->face_id >= 0
1867 || IT_CHARPOS (*it) == BEG
1868 || new_face_id != face_before_it_pos (it)));
1869 it->face_box_p = new_face->box != FACE_NO_BOX;
1870 }
1871 }
1872 else
1873 {
1874 new_face_id
1875 = face_at_string_position (it->w,
1876 it->string,
1877 IT_STRING_CHARPOS (*it),
1878 (it->current.overlay_string_index >= 0
1879 ? IT_CHARPOS (*it)
1880 : 0),
1881 it->region_beg_charpos,
1882 it->region_end_charpos,
1883 &next_stop,
1884 it->base_face_id);
1885
1886 #if 0 /* This shouldn't be neccessary. Let's check it. */
1887 /* If IT is used to display a mode line we would really like to
1888 use the mode line face instead of the frame's default face. */
1889 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
1890 && new_face_id == DEFAULT_FACE_ID)
1891 new_face_id = MODE_LINE_FACE_ID;
1892 #endif
1893
1894 /* Is this a start of a run of characters with box? Caveat:
1895 this can be called for a freshly allocated iterator; face_id
1896 is -1 is this case. We know that the new face will not
1897 change until the next check pos, i.e. if the new face has a
1898 box, all characters up to that position will have a
1899 box. But, as usual, we don't know whether that position
1900 is really the end. */
1901 if (new_face_id != it->face_id)
1902 {
1903 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
1904 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
1905
1906 /* If new face has a box but old face hasn't, this is the
1907 start of a run of characters with box, i.e. it has a
1908 shadow on the left side. */
1909 it->start_of_box_run_p
1910 = new_face->box && (old_face == NULL || !old_face->box);
1911 it->face_box_p = new_face->box != FACE_NO_BOX;
1912 }
1913 }
1914
1915 it->face_id = new_face_id;
1916 return HANDLED_NORMALLY;
1917 }
1918
1919
1920 /* Compute the face one character before or after the current position
1921 of IT. BEFORE_P non-zero means get the face in front of IT's
1922 position. Value is the id of the face. */
1923
1924 static int
1925 face_before_or_after_it_pos (it, before_p)
1926 struct it *it;
1927 int before_p;
1928 {
1929 int face_id, limit;
1930 int next_check_charpos;
1931 struct text_pos pos;
1932
1933 xassert (it->s == NULL);
1934
1935 if (STRINGP (it->string))
1936 {
1937 /* No face change past the end of the string (for the case
1938 we are padding with spaces). No face change before the
1939 string start. */
1940 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size
1941 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
1942 return it->face_id;
1943
1944 /* Set pos to the position before or after IT's current position. */
1945 if (before_p)
1946 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
1947 else
1948 /* For composition, we must check the character after the
1949 composition. */
1950 pos = (it->what == IT_COMPOSITION
1951 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
1952 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
1953
1954 /* Get the face for ASCII, or unibyte. */
1955 face_id
1956 = face_at_string_position (it->w,
1957 it->string,
1958 CHARPOS (pos),
1959 (it->current.overlay_string_index >= 0
1960 ? IT_CHARPOS (*it)
1961 : 0),
1962 it->region_beg_charpos,
1963 it->region_end_charpos,
1964 &next_check_charpos,
1965 it->base_face_id);
1966
1967 /* Correct the face for charsets different from ASCII. Do it
1968 for the multibyte case only. The face returned above is
1969 suitable for unibyte text if IT->string is unibyte. */
1970 if (STRING_MULTIBYTE (it->string))
1971 {
1972 unsigned char *p = XSTRING (it->string)->data + BYTEPOS (pos);
1973 int rest = STRING_BYTES (XSTRING (it->string)) - BYTEPOS (pos);
1974 int c, len;
1975 struct face *face = FACE_FROM_ID (it->f, face_id);
1976
1977 c = string_char_and_length (p, rest, &len);
1978 face_id = FACE_FOR_CHAR (it->f, face, c);
1979 }
1980 }
1981 else
1982 {
1983 if ((IT_CHARPOS (*it) >= ZV && !before_p)
1984 || (IT_CHARPOS (*it) <= BEGV && before_p))
1985 return it->face_id;
1986
1987 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
1988 pos = it->current.pos;
1989
1990 if (before_p)
1991 DEC_TEXT_POS (pos, it->multibyte_p);
1992 else
1993 {
1994 if (it->what == IT_COMPOSITION)
1995 /* For composition, we must check the position after the
1996 composition. */
1997 pos.charpos += it->cmp_len, pos.bytepos += it->len;
1998 else
1999 INC_TEXT_POS (pos, it->multibyte_p);
2000 }
2001 /* Determine face for CHARSET_ASCII, or unibyte. */
2002 face_id = face_at_buffer_position (it->w,
2003 CHARPOS (pos),
2004 it->region_beg_charpos,
2005 it->region_end_charpos,
2006 &next_check_charpos,
2007 limit, 0);
2008
2009 /* Correct the face for charsets different from ASCII. Do it
2010 for the multibyte case only. The face returned above is
2011 suitable for unibyte text if current_buffer is unibyte. */
2012 if (it->multibyte_p)
2013 {
2014 int c = FETCH_MULTIBYTE_CHAR (CHARPOS (pos));
2015 struct face *face = FACE_FROM_ID (it->f, face_id);
2016 face_id = FACE_FOR_CHAR (it->f, face, c);
2017 }
2018 }
2019
2020 return face_id;
2021 }
2022
2023
2024 \f
2025 /***********************************************************************
2026 Invisible text
2027 ***********************************************************************/
2028
2029 /* Set up iterator IT from invisible properties at its current
2030 position. Called from handle_stop. */
2031
2032 static enum prop_handled
2033 handle_invisible_prop (it)
2034 struct it *it;
2035 {
2036 enum prop_handled handled = HANDLED_NORMALLY;
2037
2038 if (STRINGP (it->string))
2039 {
2040 extern Lisp_Object Qinvisible;
2041 Lisp_Object prop, end_charpos, limit, charpos;
2042
2043 /* Get the value of the invisible text property at the
2044 current position. Value will be nil if there is no such
2045 property. */
2046 XSETFASTINT (charpos, IT_STRING_CHARPOS (*it));
2047 prop = Fget_text_property (charpos, Qinvisible, it->string);
2048
2049 if (!NILP (prop)
2050 && IT_STRING_CHARPOS (*it) < it->end_charpos)
2051 {
2052 handled = HANDLED_RECOMPUTE_PROPS;
2053
2054 /* Get the position at which the next change of the
2055 invisible text property can be found in IT->string.
2056 Value will be nil if the property value is the same for
2057 all the rest of IT->string. */
2058 XSETINT (limit, XSTRING (it->string)->size);
2059 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2060 it->string, limit);
2061
2062 /* Text at current position is invisible. The next
2063 change in the property is at position end_charpos.
2064 Move IT's current position to that position. */
2065 if (INTEGERP (end_charpos)
2066 && XFASTINT (end_charpos) < XFASTINT (limit))
2067 {
2068 struct text_pos old;
2069 old = it->current.string_pos;
2070 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
2071 compute_string_pos (&it->current.string_pos, old, it->string);
2072 }
2073 else
2074 {
2075 /* The rest of the string is invisible. If this is an
2076 overlay string, proceed with the next overlay string
2077 or whatever comes and return a character from there. */
2078 if (it->current.overlay_string_index >= 0)
2079 {
2080 next_overlay_string (it);
2081 /* Don't check for overlay strings when we just
2082 finished processing them. */
2083 handled = HANDLED_OVERLAY_STRING_CONSUMED;
2084 }
2085 else
2086 {
2087 struct Lisp_String *s = XSTRING (it->string);
2088 IT_STRING_CHARPOS (*it) = s->size;
2089 IT_STRING_BYTEPOS (*it) = STRING_BYTES (s);
2090 }
2091 }
2092 }
2093 }
2094 else
2095 {
2096 int visible_p, newpos, next_stop;
2097 Lisp_Object pos, prop;
2098
2099 /* First of all, is there invisible text at this position? */
2100 XSETFASTINT (pos, IT_CHARPOS (*it));
2101 prop = Fget_char_property (pos, Qinvisible, it->window);
2102
2103 /* If we are on invisible text, skip over it. */
2104 if (TEXT_PROP_MEANS_INVISIBLE (prop)
2105 && IT_CHARPOS (*it) < it->end_charpos)
2106 {
2107 /* Record whether we have to display an ellipsis for the
2108 invisible text. */
2109 int display_ellipsis_p
2110 = TEXT_PROP_MEANS_INVISIBLE_WITH_ELLIPSIS (prop);
2111
2112 handled = HANDLED_RECOMPUTE_PROPS;
2113
2114 /* Loop skipping over invisible text. The loop is left at
2115 ZV or with IT on the first char being visible again. */
2116 do
2117 {
2118 /* Try to skip some invisible text. Return value is the
2119 position reached which can be equal to IT's position
2120 if there is nothing invisible here. This skips both
2121 over invisible text properties and overlays with
2122 invisible property. */
2123 newpos = skip_invisible (IT_CHARPOS (*it),
2124 &next_stop, ZV, it->window);
2125
2126 /* If we skipped nothing at all we weren't at invisible
2127 text in the first place. If everything to the end of
2128 the buffer was skipped, end the loop. */
2129 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
2130 visible_p = 1;
2131 else
2132 {
2133 /* We skipped some characters but not necessarily
2134 all there are. Check if we ended up on visible
2135 text. Fget_char_property returns the property of
2136 the char before the given position, i.e. if we
2137 get visible_p = 1, this means that the char at
2138 newpos is visible. */
2139 XSETFASTINT (pos, newpos);
2140 prop = Fget_char_property (pos, Qinvisible, it->window);
2141 visible_p = !TEXT_PROP_MEANS_INVISIBLE (prop);
2142 }
2143
2144 /* If we ended up on invisible text, proceed to
2145 skip starting with next_stop. */
2146 if (!visible_p)
2147 IT_CHARPOS (*it) = next_stop;
2148 }
2149 while (!visible_p);
2150
2151 /* The position newpos is now either ZV or on visible text. */
2152 IT_CHARPOS (*it) = newpos;
2153 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2154
2155 /* Maybe return `...' next for the end of the invisible text. */
2156 if (display_ellipsis_p)
2157 {
2158 if (it->dp
2159 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
2160 {
2161 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
2162 it->dpvec = v->contents;
2163 it->dpend = v->contents + v->size;
2164 }
2165 else
2166 {
2167 /* Default `...'. */
2168 it->dpvec = default_invis_vector;
2169 it->dpend = default_invis_vector + 3;
2170 }
2171
2172 /* The ellipsis display does not replace the display of
2173 the character at the new position. Indicate this by
2174 setting IT->dpvec_char_len to zero. */
2175 it->dpvec_char_len = 0;
2176
2177 it->current.dpvec_index = 0;
2178 it->method = next_element_from_display_vector;
2179 }
2180 }
2181 }
2182
2183 return handled;
2184 }
2185
2186
2187 \f
2188 /***********************************************************************
2189 'display' property
2190 ***********************************************************************/
2191
2192 /* Set up iterator IT from `display' property at its current position.
2193 Called from handle_stop. */
2194
2195 static enum prop_handled
2196 handle_display_prop (it)
2197 struct it *it;
2198 {
2199 Lisp_Object prop, object;
2200 struct text_pos *position;
2201 int space_or_image_found_p;
2202
2203 if (STRINGP (it->string))
2204 {
2205 object = it->string;
2206 position = &it->current.string_pos;
2207 }
2208 else
2209 {
2210 object = Qnil;
2211 position = &it->current.pos;
2212 }
2213
2214 /* Reset those iterator values set from display property values. */
2215 it->font_height = Qnil;
2216 it->space_width = Qnil;
2217 it->voffset = 0;
2218
2219 /* We don't support recursive `display' properties, i.e. string
2220 values that have a string `display' property, that have a string
2221 `display' property etc. */
2222 if (!it->string_from_display_prop_p)
2223 it->area = TEXT_AREA;
2224
2225 prop = Fget_char_property (make_number (position->charpos),
2226 Qdisplay, object);
2227 if (NILP (prop))
2228 return HANDLED_NORMALLY;
2229
2230 space_or_image_found_p = 0;
2231 if (CONSP (prop)
2232 && CONSP (XCAR (prop))
2233 && !EQ (Qmargin, XCAR (XCAR (prop))))
2234 {
2235 /* A list of sub-properties. */
2236 while (CONSP (prop))
2237 {
2238 if (handle_single_display_prop (it, XCAR (prop), object, position))
2239 space_or_image_found_p = 1;
2240 prop = XCDR (prop);
2241 }
2242 }
2243 else if (VECTORP (prop))
2244 {
2245 int i;
2246 for (i = 0; i < XVECTOR (prop)->size; ++i)
2247 if (handle_single_display_prop (it, XVECTOR (prop)->contents[i],
2248 object, position))
2249 space_or_image_found_p = 1;
2250 }
2251 else
2252 {
2253 if (handle_single_display_prop (it, prop, object, position))
2254 space_or_image_found_p = 1;
2255 }
2256
2257 return space_or_image_found_p ? HANDLED_RETURN : HANDLED_NORMALLY;
2258 }
2259
2260
2261 /* Value is the position of the end of the `display' property starting
2262 at START_POS in OBJECT. */
2263
2264 static struct text_pos
2265 display_prop_end (it, object, start_pos)
2266 struct it *it;
2267 Lisp_Object object;
2268 struct text_pos start_pos;
2269 {
2270 Lisp_Object end;
2271 struct text_pos end_pos;
2272
2273 end = next_single_char_property_change (make_number (CHARPOS (start_pos)),
2274 Qdisplay, object, Qnil);
2275 CHARPOS (end_pos) = XFASTINT (end);
2276 if (STRINGP (object))
2277 compute_string_pos (&end_pos, start_pos, it->string);
2278 else
2279 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
2280
2281 return end_pos;
2282 }
2283
2284
2285 /* Set up IT from a single `display' sub-property value PROP. OBJECT
2286 is the object in which the `display' property was found. *POSITION
2287 is the position at which it was found.
2288
2289 If PROP is a `space' or `image' sub-property, set *POSITION to the
2290 end position of the `display' property.
2291
2292 Value is non-zero if a `space' or `image' property value was found. */
2293
2294 static int
2295 handle_single_display_prop (it, prop, object, position)
2296 struct it *it;
2297 Lisp_Object prop;
2298 Lisp_Object object;
2299 struct text_pos *position;
2300 {
2301 Lisp_Object value;
2302 int space_or_image_found_p = 0;
2303
2304 Lisp_Object form;
2305
2306 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
2307 evaluated. If the result is nil, VALUE is ignored. */
2308 form = Qt;
2309 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2310 {
2311 prop = XCDR (prop);
2312 if (!CONSP (prop))
2313 return 0;
2314 form = XCAR (prop);
2315 prop = XCDR (prop);
2316 }
2317
2318 if (!NILP (form) && !EQ (form, Qt))
2319 {
2320 struct gcpro gcpro1;
2321 struct text_pos end_pos, pt;
2322
2323 GCPRO1 (form);
2324 end_pos = display_prop_end (it, object, *position);
2325
2326 /* Temporarily set point to the end position, and then evaluate
2327 the form. This makes `(eolp)' work as FORM. */
2328 if (BUFFERP (object))
2329 {
2330 CHARPOS (pt) = PT;
2331 BYTEPOS (pt) = PT_BYTE;
2332 TEMP_SET_PT_BOTH (CHARPOS (end_pos), BYTEPOS (end_pos));
2333 }
2334
2335 form = eval_form (form);
2336
2337 if (BUFFERP (object))
2338 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
2339 UNGCPRO;
2340 }
2341
2342 if (NILP (form))
2343 return 0;
2344
2345 if (CONSP (prop)
2346 && EQ (XCAR (prop), Qheight)
2347 && CONSP (XCDR (prop)))
2348 {
2349 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2350 return 0;
2351
2352 /* `(height HEIGHT)'. */
2353 it->font_height = XCAR (XCDR (prop));
2354 if (!NILP (it->font_height))
2355 {
2356 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2357 int new_height = -1;
2358
2359 if (CONSP (it->font_height)
2360 && (EQ (XCAR (it->font_height), Qplus)
2361 || EQ (XCAR (it->font_height), Qminus))
2362 && CONSP (XCDR (it->font_height))
2363 && INTEGERP (XCAR (XCDR (it->font_height))))
2364 {
2365 /* `(+ N)' or `(- N)' where N is an integer. */
2366 int steps = XINT (XCAR (XCDR (it->font_height)));
2367 if (EQ (XCAR (it->font_height), Qplus))
2368 steps = - steps;
2369 it->face_id = smaller_face (it->f, it->face_id, steps);
2370 }
2371 else if (SYMBOLP (it->font_height))
2372 {
2373 /* Call function with current height as argument.
2374 Value is the new height. */
2375 Lisp_Object form, height;
2376 struct gcpro gcpro1;
2377
2378 height = face->lface[LFACE_HEIGHT_INDEX];
2379 form = Fcons (it->font_height, Fcons (height, Qnil));
2380 GCPRO1 (form);
2381 height = eval_form (form);
2382 if (NUMBERP (height))
2383 new_height = XFLOATINT (height);
2384 UNGCPRO;
2385 }
2386 else if (NUMBERP (it->font_height))
2387 {
2388 /* Value is a multiple of the canonical char height. */
2389 struct face *face;
2390
2391 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
2392 new_height = (XFLOATINT (it->font_height)
2393 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
2394 }
2395 else
2396 {
2397 /* Evaluate IT->font_height with `height' bound to the
2398 current specified height to get the new height. */
2399 Lisp_Object value;
2400 int count = specpdl_ptr - specpdl;
2401
2402 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
2403 value = eval_form (it->font_height);
2404 unbind_to (count, Qnil);
2405
2406 if (NUMBERP (value))
2407 new_height = XFLOATINT (value);
2408 }
2409
2410 if (new_height > 0)
2411 it->face_id = face_with_height (it->f, it->face_id, new_height);
2412 }
2413 }
2414 else if (CONSP (prop)
2415 && EQ (XCAR (prop), Qspace_width)
2416 && CONSP (XCDR (prop)))
2417 {
2418 /* `(space_width WIDTH)'. */
2419 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2420 return 0;
2421
2422 value = XCAR (XCDR (prop));
2423 if (NUMBERP (value) && XFLOATINT (value) > 0)
2424 it->space_width = value;
2425 }
2426 else if (CONSP (prop)
2427 && EQ (XCAR (prop), Qraise)
2428 && CONSP (XCDR (prop)))
2429 {
2430 /* `(raise FACTOR)'. */
2431 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2432 return 0;
2433
2434 #ifdef HAVE_WINDOW_SYSTEM
2435 value = XCAR (XCDR (prop));
2436 if (NUMBERP (value))
2437 {
2438 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2439 it->voffset = - (XFLOATINT (value)
2440 * (FONT_HEIGHT (face->font)));
2441 }
2442 #endif /* HAVE_WINDOW_SYSTEM */
2443 }
2444 else if (!it->string_from_display_prop_p)
2445 {
2446 /* `((margin left-margin) VALUE)' or `((margin right-margin)
2447 VALUE) or `((margin nil) VALUE)' or VALUE. */
2448 Lisp_Object location, value;
2449 struct text_pos start_pos;
2450 int valid_p;
2451
2452 /* Characters having this form of property are not displayed, so
2453 we have to find the end of the property. */
2454 start_pos = *position;
2455 *position = display_prop_end (it, object, start_pos);
2456 value = Qnil;
2457
2458 /* Let's stop at the new position and assume that all
2459 text properties change there. */
2460 it->stop_charpos = position->charpos;
2461
2462 location = Qunbound;
2463 if (CONSP (prop) && CONSP (XCAR (prop)))
2464 {
2465 Lisp_Object tem;
2466
2467 value = XCDR (prop);
2468 if (CONSP (value))
2469 value = XCAR (value);
2470
2471 tem = XCAR (prop);
2472 if (EQ (XCAR (tem), Qmargin)
2473 && (tem = XCDR (tem),
2474 tem = CONSP (tem) ? XCAR (tem) : Qnil,
2475 (NILP (tem)
2476 || EQ (tem, Qleft_margin)
2477 || EQ (tem, Qright_margin))))
2478 location = tem;
2479 }
2480
2481 if (EQ (location, Qunbound))
2482 {
2483 location = Qnil;
2484 value = prop;
2485 }
2486
2487 #ifdef HAVE_WINDOW_SYSTEM
2488 if (FRAME_TERMCAP_P (it->f))
2489 valid_p = STRINGP (value);
2490 else
2491 valid_p = (STRINGP (value)
2492 || (CONSP (value) && EQ (XCAR (value), Qspace))
2493 || valid_image_p (value));
2494 #else /* not HAVE_WINDOW_SYSTEM */
2495 valid_p = STRINGP (value);
2496 #endif /* not HAVE_WINDOW_SYSTEM */
2497
2498 if ((EQ (location, Qleft_margin)
2499 || EQ (location, Qright_margin)
2500 || NILP (location))
2501 && valid_p)
2502 {
2503 space_or_image_found_p = 1;
2504
2505 /* Save current settings of IT so that we can restore them
2506 when we are finished with the glyph property value. */
2507 push_it (it);
2508
2509 if (NILP (location))
2510 it->area = TEXT_AREA;
2511 else if (EQ (location, Qleft_margin))
2512 it->area = LEFT_MARGIN_AREA;
2513 else
2514 it->area = RIGHT_MARGIN_AREA;
2515
2516 if (STRINGP (value))
2517 {
2518 it->string = value;
2519 it->multibyte_p = STRING_MULTIBYTE (it->string);
2520 it->current.overlay_string_index = -1;
2521 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
2522 it->end_charpos = it->string_nchars
2523 = XSTRING (it->string)->size;
2524 it->method = next_element_from_string;
2525 it->stop_charpos = 0;
2526 it->string_from_display_prop_p = 1;
2527 }
2528 else if (CONSP (value) && EQ (XCAR (value), Qspace))
2529 {
2530 it->method = next_element_from_stretch;
2531 it->object = value;
2532 it->current.pos = it->position = start_pos;
2533 }
2534 #ifdef HAVE_WINDOW_SYSTEM
2535 else
2536 {
2537 it->what = IT_IMAGE;
2538 it->image_id = lookup_image (it->f, value);
2539 it->position = start_pos;
2540 it->object = NILP (object) ? it->w->buffer : object;
2541 it->method = next_element_from_image;
2542
2543 /* Say that we haven't consumed the characters with
2544 `display' property yet. The call to pop_it in
2545 set_iterator_to_next will clean this up. */
2546 *position = start_pos;
2547 }
2548 #endif /* HAVE_WINDOW_SYSTEM */
2549 }
2550 else
2551 /* Invalid property or property not supported. Restore
2552 the position to what it was before. */
2553 *position = start_pos;
2554 }
2555
2556 return space_or_image_found_p;
2557 }
2558
2559
2560 \f
2561 /***********************************************************************
2562 `composition' property
2563 ***********************************************************************/
2564
2565 /* Set up iterator IT from `composition' property at its current
2566 position. Called from handle_stop. */
2567
2568 static enum prop_handled
2569 handle_composition_prop (it)
2570 struct it *it;
2571 {
2572 Lisp_Object prop, string;
2573 int pos, pos_byte, end;
2574 enum prop_handled handled = HANDLED_NORMALLY;
2575
2576 if (STRINGP (it->string))
2577 {
2578 pos = IT_STRING_CHARPOS (*it);
2579 pos_byte = IT_STRING_BYTEPOS (*it);
2580 string = it->string;
2581 }
2582 else
2583 {
2584 pos = IT_CHARPOS (*it);
2585 pos_byte = IT_BYTEPOS (*it);
2586 string = Qnil;
2587 }
2588
2589 /* If there's a valid composition and point is not inside of the
2590 composition (in the case that the composition is from the current
2591 buffer), draw a glyph composed from the composition components. */
2592 if (find_composition (pos, -1, &pos, &end, &prop, string)
2593 && COMPOSITION_VALID_P (pos, end, prop)
2594 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
2595 {
2596 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
2597
2598 if (id >= 0)
2599 {
2600 it->method = next_element_from_composition;
2601 it->cmp_id = id;
2602 it->cmp_len = COMPOSITION_LENGTH (prop);
2603 /* For a terminal, draw only the first character of the
2604 components. */
2605 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
2606 it->len = (STRINGP (it->string)
2607 ? string_char_to_byte (it->string, end)
2608 : CHAR_TO_BYTE (end)) - pos_byte;
2609 it->stop_charpos = end;
2610 handled = HANDLED_RETURN;
2611 }
2612 }
2613
2614 return handled;
2615 }
2616
2617
2618 \f
2619 /***********************************************************************
2620 Overlay strings
2621 ***********************************************************************/
2622
2623 /* The following structure is used to record overlay strings for
2624 later sorting in load_overlay_strings. */
2625
2626 struct overlay_entry
2627 {
2628 Lisp_Object string;
2629 int priority;
2630 int after_string_p;
2631 };
2632
2633
2634 /* Set up iterator IT from overlay strings at its current position.
2635 Called from handle_stop. */
2636
2637 static enum prop_handled
2638 handle_overlay_change (it)
2639 struct it *it;
2640 {
2641 /* Overlays are handled in current_buffer only. */
2642 if (STRINGP (it->string))
2643 return HANDLED_NORMALLY;
2644 else
2645 return (get_overlay_strings (it)
2646 ? HANDLED_RECOMPUTE_PROPS
2647 : HANDLED_NORMALLY);
2648 }
2649
2650
2651 /* Set up the next overlay string for delivery by IT, if there is an
2652 overlay string to deliver. Called by set_iterator_to_next when the
2653 end of the current overlay string is reached. If there are more
2654 overlay strings to display, IT->string and
2655 IT->current.overlay_string_index are set appropriately here.
2656 Otherwise IT->string is set to nil. */
2657
2658 static void
2659 next_overlay_string (it)
2660 struct it *it;
2661 {
2662 ++it->current.overlay_string_index;
2663 if (it->current.overlay_string_index == it->n_overlay_strings)
2664 {
2665 /* No more overlay strings. Restore IT's settings to what
2666 they were before overlay strings were processed, and
2667 continue to deliver from current_buffer. */
2668 pop_it (it);
2669 xassert (it->stop_charpos >= BEGV
2670 && it->stop_charpos <= it->end_charpos);
2671 it->string = Qnil;
2672 it->current.overlay_string_index = -1;
2673 SET_TEXT_POS (it->current.string_pos, -1, -1);
2674 it->n_overlay_strings = 0;
2675 it->method = next_element_from_buffer;
2676 }
2677 else
2678 {
2679 /* There are more overlay strings to process. If
2680 IT->current.overlay_string_index has advanced to a position
2681 where we must load IT->overlay_strings with more strings, do
2682 it. */
2683 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
2684
2685 if (it->current.overlay_string_index && i == 0)
2686 load_overlay_strings (it);
2687
2688 /* Initialize IT to deliver display elements from the overlay
2689 string. */
2690 it->string = it->overlay_strings[i];
2691 it->multibyte_p = STRING_MULTIBYTE (it->string);
2692 SET_TEXT_POS (it->current.string_pos, 0, 0);
2693 it->method = next_element_from_string;
2694 it->stop_charpos = 0;
2695 }
2696
2697 CHECK_IT (it);
2698 }
2699
2700
2701 /* Compare two overlay_entry structures E1 and E2. Used as a
2702 comparison function for qsort in load_overlay_strings. Overlay
2703 strings for the same position are sorted so that
2704
2705 1. All after-strings come in front of before-strings.
2706
2707 2. Within after-strings, strings are sorted so that overlay strings
2708 from overlays with higher priorities come first.
2709
2710 2. Within before-strings, strings are sorted so that overlay
2711 strings from overlays with higher priorities come last.
2712
2713 Value is analogous to strcmp. */
2714
2715
2716 static int
2717 compare_overlay_entries (e1, e2)
2718 void *e1, *e2;
2719 {
2720 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
2721 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
2722 int result;
2723
2724 if (entry1->after_string_p != entry2->after_string_p)
2725 /* Let after-strings appear in front of before-strings. */
2726 result = entry1->after_string_p ? -1 : 1;
2727 else if (entry1->after_string_p)
2728 /* After-strings sorted in order of decreasing priority. */
2729 result = entry2->priority - entry1->priority;
2730 else
2731 /* Before-strings sorted in order of increasing priority. */
2732 result = entry1->priority - entry2->priority;
2733
2734 return result;
2735 }
2736
2737
2738 /* Load the vector IT->overlay_strings with overlay strings from IT's
2739 current buffer position. Set IT->n_overlays to the total number of
2740 overlay strings found.
2741
2742 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
2743 a time. On entry into load_overlay_strings,
2744 IT->current.overlay_string_index gives the number of overlay
2745 strings that have already been loaded by previous calls to this
2746 function.
2747
2748 Overlay strings are sorted so that after-string strings come in
2749 front of before-string strings. Within before and after-strings,
2750 strings are sorted by overlay priority. See also function
2751 compare_overlay_entries. */
2752
2753 static void
2754 load_overlay_strings (it)
2755 struct it *it;
2756 {
2757 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
2758 Lisp_Object ov, overlay, window, str;
2759 int start, end;
2760 int size = 20;
2761 int n = 0, i, j;
2762 struct overlay_entry *entries
2763 = (struct overlay_entry *) alloca (size * sizeof *entries);
2764
2765 /* Append the overlay string STRING of overlay OVERLAY to vector
2766 `entries' which has size `size' and currently contains `n'
2767 elements. AFTER_P non-zero means STRING is an after-string of
2768 OVERLAY. */
2769 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
2770 do \
2771 { \
2772 Lisp_Object priority; \
2773 \
2774 if (n == size) \
2775 { \
2776 int new_size = 2 * size; \
2777 struct overlay_entry *old = entries; \
2778 entries = \
2779 (struct overlay_entry *) alloca (new_size \
2780 * sizeof *entries); \
2781 bcopy (old, entries, size * sizeof *entries); \
2782 size = new_size; \
2783 } \
2784 \
2785 entries[n].string = (STRING); \
2786 priority = Foverlay_get ((OVERLAY), Qpriority); \
2787 entries[n].priority \
2788 = INTEGERP (priority) ? XFASTINT (priority) : 0; \
2789 entries[n].after_string_p = (AFTER_P); \
2790 ++n; \
2791 } \
2792 while (0)
2793
2794 /* Process overlay before the overlay center. */
2795 for (ov = current_buffer->overlays_before;
2796 CONSP (ov);
2797 ov = XCDR (ov))
2798 {
2799 overlay = XCAR (ov);
2800 xassert (OVERLAYP (overlay));
2801 start = OVERLAY_POSITION (OVERLAY_START (overlay));
2802 end = OVERLAY_POSITION (OVERLAY_END (overlay));
2803
2804 if (end < IT_CHARPOS (*it))
2805 break;
2806
2807 /* Skip this overlay if it doesn't start or end at IT's current
2808 position. */
2809 if (end != IT_CHARPOS (*it) && start != IT_CHARPOS (*it))
2810 continue;
2811
2812 /* Skip this overlay if it doesn't apply to IT->w. */
2813 window = Foverlay_get (overlay, Qwindow);
2814 if (WINDOWP (window) && XWINDOW (window) != it->w)
2815 continue;
2816
2817 /* If overlay has a non-empty before-string, record it. */
2818 if (start == IT_CHARPOS (*it)
2819 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
2820 && XSTRING (str)->size)
2821 RECORD_OVERLAY_STRING (overlay, str, 0);
2822
2823 /* If overlay has a non-empty after-string, record it. */
2824 if (end == IT_CHARPOS (*it)
2825 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
2826 && XSTRING (str)->size)
2827 RECORD_OVERLAY_STRING (overlay, str, 1);
2828 }
2829
2830 /* Process overlays after the overlay center. */
2831 for (ov = current_buffer->overlays_after;
2832 CONSP (ov);
2833 ov = XCDR (ov))
2834 {
2835 overlay = XCAR (ov);
2836 xassert (OVERLAYP (overlay));
2837 start = OVERLAY_POSITION (OVERLAY_START (overlay));
2838 end = OVERLAY_POSITION (OVERLAY_END (overlay));
2839
2840 if (start > IT_CHARPOS (*it))
2841 break;
2842
2843 /* Skip this overlay if it doesn't start or end at IT's current
2844 position. */
2845 if (end != IT_CHARPOS (*it) && start != IT_CHARPOS (*it))
2846 continue;
2847
2848 /* Skip this overlay if it doesn't apply to IT->w. */
2849 window = Foverlay_get (overlay, Qwindow);
2850 if (WINDOWP (window) && XWINDOW (window) != it->w)
2851 continue;
2852
2853 /* If overlay has a non-empty before-string, record it. */
2854 if (start == IT_CHARPOS (*it)
2855 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
2856 && XSTRING (str)->size)
2857 RECORD_OVERLAY_STRING (overlay, str, 0);
2858
2859 /* If overlay has a non-empty after-string, record it. */
2860 if (end == IT_CHARPOS (*it)
2861 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
2862 && XSTRING (str)->size)
2863 RECORD_OVERLAY_STRING (overlay, str, 1);
2864 }
2865
2866 #undef RECORD_OVERLAY_STRING
2867
2868 /* Sort entries. */
2869 qsort (entries, n, sizeof *entries, compare_overlay_entries);
2870
2871 /* Record the total number of strings to process. */
2872 it->n_overlay_strings = n;
2873
2874 /* IT->current.overlay_string_index is the number of overlay strings
2875 that have already been consumed by IT. Copy some of the
2876 remaining overlay strings to IT->overlay_strings. */
2877 i = 0;
2878 j = it->current.overlay_string_index;
2879 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
2880 it->overlay_strings[i++] = entries[j++].string;
2881
2882 CHECK_IT (it);
2883 }
2884
2885
2886 /* Get the first chunk of overlay strings at IT's current buffer
2887 position. Value is non-zero if at least one overlay string was
2888 found. */
2889
2890 static int
2891 get_overlay_strings (it)
2892 struct it *it;
2893 {
2894 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
2895 process. This fills IT->overlay_strings with strings, and sets
2896 IT->n_overlay_strings to the total number of strings to process.
2897 IT->pos.overlay_string_index has to be set temporarily to zero
2898 because load_overlay_strings needs this; it must be set to -1
2899 when no overlay strings are found because a zero value would
2900 indicate a position in the first overlay string. */
2901 it->current.overlay_string_index = 0;
2902 load_overlay_strings (it);
2903
2904 /* If we found overlay strings, set up IT to deliver display
2905 elements from the first one. Otherwise set up IT to deliver
2906 from current_buffer. */
2907 if (it->n_overlay_strings)
2908 {
2909 /* Make sure we know settings in current_buffer, so that we can
2910 restore meaningful values when we're done with the overlay
2911 strings. */
2912 compute_stop_pos (it);
2913 xassert (it->face_id >= 0);
2914
2915 /* Save IT's settings. They are restored after all overlay
2916 strings have been processed. */
2917 xassert (it->sp == 0);
2918 push_it (it);
2919
2920 /* Set up IT to deliver display elements from the first overlay
2921 string. */
2922 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
2923 it->stop_charpos = 0;
2924 it->string = it->overlay_strings[0];
2925 it->multibyte_p = STRING_MULTIBYTE (it->string);
2926 xassert (STRINGP (it->string));
2927 it->method = next_element_from_string;
2928 }
2929 else
2930 {
2931 it->string = Qnil;
2932 it->current.overlay_string_index = -1;
2933 it->method = next_element_from_buffer;
2934 }
2935
2936 CHECK_IT (it);
2937
2938 /* Value is non-zero if we found at least one overlay string. */
2939 return STRINGP (it->string);
2940 }
2941
2942
2943 \f
2944 /***********************************************************************
2945 Saving and restoring state
2946 ***********************************************************************/
2947
2948 /* Save current settings of IT on IT->stack. Called, for example,
2949 before setting up IT for an overlay string, to be able to restore
2950 IT's settings to what they were after the overlay string has been
2951 processed. */
2952
2953 static void
2954 push_it (it)
2955 struct it *it;
2956 {
2957 struct iterator_stack_entry *p;
2958
2959 xassert (it->sp < 2);
2960 p = it->stack + it->sp;
2961
2962 p->stop_charpos = it->stop_charpos;
2963 xassert (it->face_id >= 0);
2964 p->face_id = it->face_id;
2965 p->string = it->string;
2966 p->pos = it->current;
2967 p->end_charpos = it->end_charpos;
2968 p->string_nchars = it->string_nchars;
2969 p->area = it->area;
2970 p->multibyte_p = it->multibyte_p;
2971 p->space_width = it->space_width;
2972 p->font_height = it->font_height;
2973 p->voffset = it->voffset;
2974 p->string_from_display_prop_p = it->string_from_display_prop_p;
2975 ++it->sp;
2976 }
2977
2978
2979 /* Restore IT's settings from IT->stack. Called, for example, when no
2980 more overlay strings must be processed, and we return to delivering
2981 display elements from a buffer, or when the end of a string from a
2982 `display' property is reached and we return to delivering display
2983 elements from an overlay string, or from a buffer. */
2984
2985 static void
2986 pop_it (it)
2987 struct it *it;
2988 {
2989 struct iterator_stack_entry *p;
2990
2991 xassert (it->sp > 0);
2992 --it->sp;
2993 p = it->stack + it->sp;
2994 it->stop_charpos = p->stop_charpos;
2995 it->face_id = p->face_id;
2996 it->string = p->string;
2997 it->current = p->pos;
2998 it->end_charpos = p->end_charpos;
2999 it->string_nchars = p->string_nchars;
3000 it->area = p->area;
3001 it->multibyte_p = p->multibyte_p;
3002 it->space_width = p->space_width;
3003 it->font_height = p->font_height;
3004 it->voffset = p->voffset;
3005 it->string_from_display_prop_p = p->string_from_display_prop_p;
3006 }
3007
3008
3009 \f
3010 /***********************************************************************
3011 Moving over lines
3012 ***********************************************************************/
3013
3014 /* Set IT's current position to the previous line start. */
3015
3016 static void
3017 back_to_previous_line_start (it)
3018 struct it *it;
3019 {
3020 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
3021 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3022 }
3023
3024
3025 /* Set IT's current position to the next line start. */
3026
3027 static void
3028 forward_to_next_line_start (it)
3029 struct it *it;
3030 {
3031 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it), 1);
3032 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3033 }
3034
3035
3036 /* Set IT's current position to the previous visible line start. Skip
3037 invisible text that is so either due to text properties or due to
3038 selective display. Caution: this does not change IT->current_x and
3039 IT->hpos. */
3040
3041 static void
3042 back_to_previous_visible_line_start (it)
3043 struct it *it;
3044 {
3045 int visible_p = 0;
3046
3047 /* Go back one newline if not on BEGV already. */
3048 if (IT_CHARPOS (*it) > BEGV)
3049 back_to_previous_line_start (it);
3050
3051 /* Move over lines that are invisible because of selective display
3052 or text properties. */
3053 while (IT_CHARPOS (*it) > BEGV
3054 && !visible_p)
3055 {
3056 visible_p = 1;
3057
3058 /* If selective > 0, then lines indented more than that values
3059 are invisible. */
3060 if (it->selective > 0
3061 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3062 it->selective))
3063 visible_p = 0;
3064 else
3065 {
3066 Lisp_Object prop;
3067
3068 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
3069 Qinvisible, it->window);
3070 if (TEXT_PROP_MEANS_INVISIBLE (prop))
3071 visible_p = 0;
3072 }
3073
3074 /* Back one more newline if the current one is invisible. */
3075 if (!visible_p)
3076 back_to_previous_line_start (it);
3077 }
3078
3079 xassert (IT_CHARPOS (*it) >= BEGV);
3080 xassert (IT_CHARPOS (*it) == BEGV
3081 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3082 CHECK_IT (it);
3083 }
3084
3085
3086 /* Reseat iterator IT at the previous visible line start. Skip
3087 invisible text that is so either due to text properties or due to
3088 selective display. At the end, update IT's overlay information,
3089 face information etc. */
3090
3091 static void
3092 reseat_at_previous_visible_line_start (it)
3093 struct it *it;
3094 {
3095 back_to_previous_visible_line_start (it);
3096 reseat (it, it->current.pos, 1);
3097 CHECK_IT (it);
3098 }
3099
3100
3101 /* Reseat iterator IT on the next visible line start in the current
3102 buffer. ON_NEWLINE_P non-zero means position IT on the newline
3103 preceding the line start. Skip over invisible text that is so
3104 because of selective display. Compute faces, overlays etc at the
3105 new position. Note that this function does not skip over text that
3106 is invisible because of text properties. */
3107
3108 static void
3109 reseat_at_next_visible_line_start (it, on_newline_p)
3110 struct it *it;
3111 int on_newline_p;
3112 {
3113 /* Restore the buffer position when currently not delivering display
3114 elements from the current buffer. This is the case, for example,
3115 when called at the end of a truncated overlay string. */
3116 while (it->sp)
3117 pop_it (it);
3118 it->method = next_element_from_buffer;
3119
3120 /* Otherwise, scan_buffer would not work. */
3121 if (IT_CHARPOS (*it) < ZV)
3122 {
3123 /* If on a newline, advance past it. Otherwise, find the next
3124 newline which automatically gives us the position following
3125 the newline. */
3126 if (FETCH_BYTE (IT_BYTEPOS (*it)) == '\n')
3127 {
3128 ++IT_CHARPOS (*it);
3129 ++IT_BYTEPOS (*it);
3130 }
3131 else
3132 forward_to_next_line_start (it);
3133
3134 /* We must either have reached the end of the buffer or end up
3135 after a newline. */
3136 xassert (IT_CHARPOS (*it) == ZV
3137 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3138
3139 /* Skip over lines that are invisible because they are indented
3140 more than the value of IT->selective. */
3141 if (it->selective > 0)
3142 while (IT_CHARPOS (*it) < ZV
3143 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3144 it->selective))
3145 forward_to_next_line_start (it);
3146
3147 /* Position on the newline if we should. */
3148 if (on_newline_p
3149 && IT_CHARPOS (*it) > BEGV
3150 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n')
3151 {
3152 --IT_CHARPOS (*it);
3153 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3154 }
3155
3156 /* Set the iterator there. The 0 as the last parameter of
3157 reseat means don't force a text property lookup. The lookup
3158 is then only done if we've skipped past the iterator's
3159 check_charpos'es. This optimization is important because
3160 text property lookups tend to be expensive. */
3161 reseat (it, it->current.pos, 0);
3162 }
3163
3164 CHECK_IT (it);
3165 }
3166
3167
3168 \f
3169 /***********************************************************************
3170 Changing an iterator's position
3171 ***********************************************************************/
3172
3173 /* Change IT's current position to POS in current_buffer. If FORCE_P
3174 is non-zero, always check for text properties at the new position.
3175 Otherwise, text properties are only looked up if POS >=
3176 IT->check_charpos of a property. */
3177
3178 static void
3179 reseat (it, pos, force_p)
3180 struct it *it;
3181 struct text_pos pos;
3182 int force_p;
3183 {
3184 int original_pos = IT_CHARPOS (*it);
3185
3186 reseat_1 (it, pos, 0);
3187
3188 /* Determine where to check text properties. Avoid doing it
3189 where possible because text property lookup is very expensive. */
3190 if (force_p
3191 || CHARPOS (pos) > it->stop_charpos
3192 || CHARPOS (pos) < original_pos)
3193 handle_stop (it);
3194
3195 CHECK_IT (it);
3196 }
3197
3198
3199 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
3200 IT->stop_pos to POS, also. */
3201
3202 static void
3203 reseat_1 (it, pos, set_stop_p)
3204 struct it *it;
3205 struct text_pos pos;
3206 int set_stop_p;
3207 {
3208 /* Don't call this function when scanning a C string. */
3209 xassert (it->s == NULL);
3210
3211 /* POS must be a reasonable value. */
3212 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
3213
3214 it->current.pos = it->position = pos;
3215 XSETBUFFER (it->object, current_buffer);
3216 it->dpvec = NULL;
3217 it->current.dpvec_index = -1;
3218 it->current.overlay_string_index = -1;
3219 IT_STRING_CHARPOS (*it) = -1;
3220 IT_STRING_BYTEPOS (*it) = -1;
3221 it->string = Qnil;
3222 it->method = next_element_from_buffer;
3223 it->sp = 0;
3224
3225 if (set_stop_p)
3226 it->stop_charpos = CHARPOS (pos);
3227 }
3228
3229
3230 /* Set up IT for displaying a string, starting at CHARPOS in window W.
3231 If S is non-null, it is a C string to iterate over. Otherwise,
3232 STRING gives a Lisp string to iterate over.
3233
3234 If PRECISION > 0, don't return more then PRECISION number of
3235 characters from the string.
3236
3237 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
3238 characters have been returned. FIELD_WIDTH < 0 means an infinite
3239 field width.
3240
3241 MULTIBYTE = 0 means disable processing of multibyte characters,
3242 MULTIBYTE > 0 means enable it,
3243 MULTIBYTE < 0 means use IT->multibyte_p.
3244
3245 IT must be initialized via a prior call to init_iterator before
3246 calling this function. */
3247
3248 static void
3249 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
3250 struct it *it;
3251 unsigned char *s;
3252 Lisp_Object string;
3253 int charpos;
3254 int precision, field_width, multibyte;
3255 {
3256 /* No region in strings. */
3257 it->region_beg_charpos = it->region_end_charpos = -1;
3258
3259 /* No text property checks performed by default, but see below. */
3260 it->stop_charpos = -1;
3261
3262 /* Set iterator position and end position. */
3263 bzero (&it->current, sizeof it->current);
3264 it->current.overlay_string_index = -1;
3265 it->current.dpvec_index = -1;
3266 xassert (charpos >= 0);
3267
3268 /* Use the setting of MULTIBYTE if specified. */
3269 if (multibyte >= 0)
3270 it->multibyte_p = multibyte > 0;
3271
3272 if (s == NULL)
3273 {
3274 xassert (STRINGP (string));
3275 it->string = string;
3276 it->s = NULL;
3277 it->end_charpos = it->string_nchars = XSTRING (string)->size;
3278 it->method = next_element_from_string;
3279 it->current.string_pos = string_pos (charpos, string);
3280 }
3281 else
3282 {
3283 it->s = s;
3284 it->string = Qnil;
3285
3286 /* Note that we use IT->current.pos, not it->current.string_pos,
3287 for displaying C strings. */
3288 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
3289 if (it->multibyte_p)
3290 {
3291 it->current.pos = c_string_pos (charpos, s, 1);
3292 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
3293 }
3294 else
3295 {
3296 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
3297 it->end_charpos = it->string_nchars = strlen (s);
3298 }
3299
3300 it->method = next_element_from_c_string;
3301 }
3302
3303 /* PRECISION > 0 means don't return more than PRECISION characters
3304 from the string. */
3305 if (precision > 0 && it->end_charpos - charpos > precision)
3306 it->end_charpos = it->string_nchars = charpos + precision;
3307
3308 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
3309 characters have been returned. FIELD_WIDTH == 0 means don't pad,
3310 FIELD_WIDTH < 0 means infinite field width. This is useful for
3311 padding with `-' at the end of a mode line. */
3312 if (field_width < 0)
3313 field_width = INFINITY;
3314 if (field_width > it->end_charpos - charpos)
3315 it->end_charpos = charpos + field_width;
3316
3317 /* Use the standard display table for displaying strings. */
3318 if (DISP_TABLE_P (Vstandard_display_table))
3319 it->dp = XCHAR_TABLE (Vstandard_display_table);
3320
3321 it->stop_charpos = charpos;
3322 CHECK_IT (it);
3323 }
3324
3325
3326 \f
3327 /***********************************************************************
3328 Iteration
3329 ***********************************************************************/
3330
3331 /* Load IT's display element fields with information about the next
3332 display element from the current position of IT. Value is zero if
3333 end of buffer (or C string) is reached. */
3334
3335 int
3336 get_next_display_element (it)
3337 struct it *it;
3338 {
3339 /* Non-zero means that we found an display element. Zero means that
3340 we hit the end of what we iterate over. Performance note: the
3341 function pointer `method' used here turns out to be faster than
3342 using a sequence of if-statements. */
3343 int success_p = (*it->method) (it);
3344
3345 if (it->what == IT_CHARACTER)
3346 {
3347 /* Map via display table or translate control characters.
3348 IT->c, IT->len etc. have been set to the next character by
3349 the function call above. If we have a display table, and it
3350 contains an entry for IT->c, translate it. Don't do this if
3351 IT->c itself comes from a display table, otherwise we could
3352 end up in an infinite recursion. (An alternative could be to
3353 count the recursion depth of this function and signal an
3354 error when a certain maximum depth is reached.) Is it worth
3355 it? */
3356 if (success_p && it->dpvec == NULL)
3357 {
3358 Lisp_Object dv;
3359
3360 if (it->dp
3361 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
3362 VECTORP (dv)))
3363 {
3364 struct Lisp_Vector *v = XVECTOR (dv);
3365
3366 /* Return the first character from the display table
3367 entry, if not empty. If empty, don't display the
3368 current character. */
3369 if (v->size)
3370 {
3371 it->dpvec_char_len = it->len;
3372 it->dpvec = v->contents;
3373 it->dpend = v->contents + v->size;
3374 it->current.dpvec_index = 0;
3375 it->method = next_element_from_display_vector;
3376 }
3377
3378 success_p = get_next_display_element (it);
3379 }
3380
3381 /* Translate control characters into `\003' or `^C' form.
3382 Control characters coming from a display table entry are
3383 currently not translated because we use IT->dpvec to hold
3384 the translation. This could easily be changed but I
3385 don't believe that it is worth doing.
3386
3387 Non-printable multibyte characters are also translated
3388 octal form. */
3389 else if ((it->c < ' '
3390 && (it->area != TEXT_AREA
3391 || (it->c != '\n' && it->c != '\t')))
3392 || (it->c >= 127
3393 && it->len == 1)
3394 || !CHAR_PRINTABLE_P (it->c))
3395 {
3396 /* IT->c is a control character which must be displayed
3397 either as '\003' or as `^C' where the '\\' and '^'
3398 can be defined in the display table. Fill
3399 IT->ctl_chars with glyphs for what we have to
3400 display. Then, set IT->dpvec to these glyphs. */
3401 GLYPH g;
3402
3403 if (it->c < 128 && it->ctl_arrow_p)
3404 {
3405 /* Set IT->ctl_chars[0] to the glyph for `^'. */
3406 if (it->dp
3407 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
3408 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
3409 g = XINT (DISP_CTRL_GLYPH (it->dp));
3410 else
3411 g = FAST_MAKE_GLYPH ('^', 0);
3412 XSETINT (it->ctl_chars[0], g);
3413
3414 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
3415 XSETINT (it->ctl_chars[1], g);
3416
3417 /* Set up IT->dpvec and return first character from it. */
3418 it->dpvec_char_len = it->len;
3419 it->dpvec = it->ctl_chars;
3420 it->dpend = it->dpvec + 2;
3421 it->current.dpvec_index = 0;
3422 it->method = next_element_from_display_vector;
3423 get_next_display_element (it);
3424 }
3425 else
3426 {
3427 unsigned char str[MAX_MULTIBYTE_LENGTH];
3428 int len;
3429 int i;
3430 GLYPH escape_glyph;
3431
3432 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
3433 if (it->dp
3434 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
3435 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
3436 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
3437 else
3438 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
3439
3440 if (SINGLE_BYTE_CHAR_P (it->c))
3441 str[0] = it->c, len = 1;
3442 else
3443 len = CHAR_STRING (it->c, str);
3444
3445 for (i = 0; i < len; i++)
3446 {
3447 XSETINT (it->ctl_chars[i * 4], escape_glyph);
3448 /* Insert three more glyphs into IT->ctl_chars for
3449 the octal display of the character. */
3450 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
3451 XSETINT (it->ctl_chars[i * 4 + 1], g);
3452 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
3453 XSETINT (it->ctl_chars[i * 4 + 2], g);
3454 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
3455 XSETINT (it->ctl_chars[i * 4 + 3], g);
3456 }
3457
3458 /* Set up IT->dpvec and return the first character
3459 from it. */
3460 it->dpvec_char_len = it->len;
3461 it->dpvec = it->ctl_chars;
3462 it->dpend = it->dpvec + len * 4;
3463 it->current.dpvec_index = 0;
3464 it->method = next_element_from_display_vector;
3465 get_next_display_element (it);
3466 }
3467 }
3468 }
3469
3470 /* Adjust face id for a multibyte character. There are no
3471 multibyte character in unibyte text. */
3472 if (it->multibyte_p
3473 && success_p
3474 && FRAME_WINDOW_P (it->f))
3475 {
3476 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3477 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
3478 }
3479 }
3480
3481 /* Is this character the last one of a run of characters with
3482 box? If yes, set IT->end_of_box_run_p to 1. */
3483 if (it->face_box_p
3484 && it->s == NULL)
3485 {
3486 int face_id;
3487 struct face *face;
3488
3489 it->end_of_box_run_p
3490 = ((face_id = face_after_it_pos (it),
3491 face_id != it->face_id)
3492 && (face = FACE_FROM_ID (it->f, face_id),
3493 face->box == FACE_NO_BOX));
3494 }
3495
3496 /* Value is 0 if end of buffer or string reached. */
3497 return success_p;
3498 }
3499
3500
3501 /* Move IT to the next display element.
3502
3503 Functions get_next_display_element and set_iterator_to_next are
3504 separate because I find this arrangement easier to handle than a
3505 get_next_display_element function that also increments IT's
3506 position. The way it is we can first look at an iterator's current
3507 display element, decide whether it fits on a line, and if it does,
3508 increment the iterator position. The other way around we probably
3509 would either need a flag indicating whether the iterator has to be
3510 incremented the next time, or we would have to implement a
3511 decrement position function which would not be easy to write. */
3512
3513 void
3514 set_iterator_to_next (it)
3515 struct it *it;
3516 {
3517 if (it->method == next_element_from_buffer)
3518 {
3519 /* The current display element of IT is a character from
3520 current_buffer. Advance in the buffer, and maybe skip over
3521 invisible lines that are so because of selective display. */
3522 if (ITERATOR_AT_END_OF_LINE_P (it))
3523 reseat_at_next_visible_line_start (it, 0);
3524 else
3525 {
3526 xassert (it->len != 0);
3527 IT_BYTEPOS (*it) += it->len;
3528 IT_CHARPOS (*it) += 1;
3529 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
3530 }
3531 }
3532 else if (it->method == next_element_from_composition)
3533 {
3534 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
3535 if (STRINGP (it->string))
3536 {
3537 IT_STRING_BYTEPOS (*it) += it->len;
3538 IT_STRING_CHARPOS (*it) += it->cmp_len;
3539 it->method = next_element_from_string;
3540 goto consider_string_end;
3541 }
3542 else
3543 {
3544 IT_BYTEPOS (*it) += it->len;
3545 IT_CHARPOS (*it) += it->cmp_len;
3546 it->method = next_element_from_buffer;
3547 }
3548 }
3549 else if (it->method == next_element_from_c_string)
3550 {
3551 /* Current display element of IT is from a C string. */
3552 IT_BYTEPOS (*it) += it->len;
3553 IT_CHARPOS (*it) += 1;
3554 }
3555 else if (it->method == next_element_from_display_vector)
3556 {
3557 /* Current display element of IT is from a display table entry.
3558 Advance in the display table definition. Reset it to null if
3559 end reached, and continue with characters from buffers/
3560 strings. */
3561 ++it->current.dpvec_index;
3562
3563 /* Restore face of the iterator to what they were before the
3564 display vector entry (these entries may contain faces). */
3565 it->face_id = it->saved_face_id;
3566
3567 if (it->dpvec + it->current.dpvec_index == it->dpend)
3568 {
3569 if (it->s)
3570 it->method = next_element_from_c_string;
3571 else if (STRINGP (it->string))
3572 it->method = next_element_from_string;
3573 else
3574 it->method = next_element_from_buffer;
3575
3576 it->dpvec = NULL;
3577 it->current.dpvec_index = -1;
3578
3579 /* Skip over characters which were displayed via IT->dpvec. */
3580 if (it->dpvec_char_len < 0)
3581 reseat_at_next_visible_line_start (it, 1);
3582 else if (it->dpvec_char_len > 0)
3583 {
3584 it->len = it->dpvec_char_len;
3585 set_iterator_to_next (it);
3586 }
3587 }
3588 }
3589 else if (it->method == next_element_from_string)
3590 {
3591 /* Current display element is a character from a Lisp string. */
3592 xassert (it->s == NULL && STRINGP (it->string));
3593 IT_STRING_BYTEPOS (*it) += it->len;
3594 IT_STRING_CHARPOS (*it) += 1;
3595
3596 consider_string_end:
3597
3598 if (it->current.overlay_string_index >= 0)
3599 {
3600 /* IT->string is an overlay string. Advance to the
3601 next, if there is one. */
3602 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
3603 next_overlay_string (it);
3604 }
3605 else
3606 {
3607 /* IT->string is not an overlay string. If we reached
3608 its end, and there is something on IT->stack, proceed
3609 with what is on the stack. This can be either another
3610 string, this time an overlay string, or a buffer. */
3611 if (IT_STRING_CHARPOS (*it) == XSTRING (it->string)->size
3612 && it->sp > 0)
3613 {
3614 pop_it (it);
3615 if (!STRINGP (it->string))
3616 it->method = next_element_from_buffer;
3617 }
3618 }
3619 }
3620 else if (it->method == next_element_from_image
3621 || it->method == next_element_from_stretch)
3622 {
3623 /* The position etc with which we have to proceed are on
3624 the stack. The position may be at the end of a string,
3625 if the `display' property takes up the whole string. */
3626 pop_it (it);
3627 it->image_id = 0;
3628 if (STRINGP (it->string))
3629 {
3630 it->method = next_element_from_string;
3631 goto consider_string_end;
3632 }
3633 else
3634 it->method = next_element_from_buffer;
3635 }
3636 else
3637 /* There are no other methods defined, so this should be a bug. */
3638 abort ();
3639
3640 /* Reset flags indicating start and end of a sequence of
3641 characters with box. */
3642 it->start_of_box_run_p = it->end_of_box_run_p = 0;
3643
3644 xassert (it->method != next_element_from_string
3645 || (STRINGP (it->string)
3646 && IT_STRING_CHARPOS (*it) >= 0));
3647 }
3648
3649
3650 /* Load IT's display element fields with information about the next
3651 display element which comes from a display table entry or from the
3652 result of translating a control character to one of the forms `^C'
3653 or `\003'. IT->dpvec holds the glyphs to return as characters. */
3654
3655 static int
3656 next_element_from_display_vector (it)
3657 struct it *it;
3658 {
3659 /* Precondition. */
3660 xassert (it->dpvec && it->current.dpvec_index >= 0);
3661
3662 /* Remember the current face id in case glyphs specify faces.
3663 IT's face is restored in set_iterator_to_next. */
3664 it->saved_face_id = it->face_id;
3665
3666 if (INTEGERP (*it->dpvec)
3667 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
3668 {
3669 int lface_id;
3670 GLYPH g;
3671
3672 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
3673 it->c = FAST_GLYPH_CHAR (g);
3674 it->len = CHAR_BYTES (it->c);
3675
3676 /* The entry may contain a face id to use. Such a face id is
3677 the id of a Lisp face, not a realized face. A face id of
3678 zero means no face. */
3679 lface_id = FAST_GLYPH_FACE (g);
3680 if (lface_id)
3681 {
3682 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
3683 if (face_id >= 0)
3684 {
3685 it->face_id = face_id;
3686 }
3687 }
3688 }
3689 else
3690 /* Display table entry is invalid. Return a space. */
3691 it->c = ' ', it->len = 1;
3692
3693 /* Don't change position and object of the iterator here. They are
3694 still the values of the character that had this display table
3695 entry or was translated, and that's what we want. */
3696 it->what = IT_CHARACTER;
3697 return 1;
3698 }
3699
3700
3701 /* Load IT with the next display element from Lisp string IT->string.
3702 IT->current.string_pos is the current position within the string.
3703 If IT->current.overlay_string_index >= 0, the Lisp string is an
3704 overlay string. */
3705
3706 static int
3707 next_element_from_string (it)
3708 struct it *it;
3709 {
3710 struct text_pos position;
3711
3712 xassert (STRINGP (it->string));
3713 xassert (IT_STRING_CHARPOS (*it) >= 0);
3714 position = it->current.string_pos;
3715
3716 /* Time to check for invisible text? */
3717 if (IT_STRING_CHARPOS (*it) < it->end_charpos
3718 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
3719 {
3720 handle_stop (it);
3721
3722 /* Since a handler may have changed IT->method, we must
3723 recurse here. */
3724 return get_next_display_element (it);
3725 }
3726
3727 if (it->current.overlay_string_index >= 0)
3728 {
3729 /* Get the next character from an overlay string. In overlay
3730 strings, There is no field width or padding with spaces to
3731 do. */
3732 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
3733 {
3734 it->what = IT_EOB;
3735 return 0;
3736 }
3737 else if (STRING_MULTIBYTE (it->string))
3738 {
3739 int remaining = (STRING_BYTES (XSTRING (it->string))
3740 - IT_STRING_BYTEPOS (*it));
3741 unsigned char *s = (XSTRING (it->string)->data
3742 + IT_STRING_BYTEPOS (*it));
3743 it->c = string_char_and_length (s, remaining, &it->len);
3744 }
3745 else
3746 {
3747 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
3748 it->len = 1;
3749 }
3750 }
3751 else
3752 {
3753 /* Get the next character from a Lisp string that is not an
3754 overlay string. Such strings come from the mode line, for
3755 example. We may have to pad with spaces, or truncate the
3756 string. See also next_element_from_c_string. */
3757 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
3758 {
3759 it->what = IT_EOB;
3760 return 0;
3761 }
3762 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
3763 {
3764 /* Pad with spaces. */
3765 it->c = ' ', it->len = 1;
3766 CHARPOS (position) = BYTEPOS (position) = -1;
3767 }
3768 else if (STRING_MULTIBYTE (it->string))
3769 {
3770 int maxlen = (STRING_BYTES (XSTRING (it->string))
3771 - IT_STRING_BYTEPOS (*it));
3772 unsigned char *s = (XSTRING (it->string)->data
3773 + IT_STRING_BYTEPOS (*it));
3774 it->c = string_char_and_length (s, maxlen, &it->len);
3775 }
3776 else
3777 {
3778 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
3779 it->len = 1;
3780 }
3781 }
3782
3783 /* Record what we have and where it came from. Note that we store a
3784 buffer position in IT->position although it could arguably be a
3785 string position. */
3786 it->what = IT_CHARACTER;
3787 it->object = it->string;
3788 it->position = position;
3789 return 1;
3790 }
3791
3792
3793 /* Load IT with next display element from C string IT->s.
3794 IT->string_nchars is the maximum number of characters to return
3795 from the string. IT->end_charpos may be greater than
3796 IT->string_nchars when this function is called, in which case we
3797 may have to return padding spaces. Value is zero if end of string
3798 reached, including padding spaces. */
3799
3800 static int
3801 next_element_from_c_string (it)
3802 struct it *it;
3803 {
3804 int success_p = 1;
3805
3806 xassert (it->s);
3807 it->what = IT_CHARACTER;
3808 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
3809 it->object = Qnil;
3810
3811 /* IT's position can be greater IT->string_nchars in case a field
3812 width or precision has been specified when the iterator was
3813 initialized. */
3814 if (IT_CHARPOS (*it) >= it->end_charpos)
3815 {
3816 /* End of the game. */
3817 it->what = IT_EOB;
3818 success_p = 0;
3819 }
3820 else if (IT_CHARPOS (*it) >= it->string_nchars)
3821 {
3822 /* Pad with spaces. */
3823 it->c = ' ', it->len = 1;
3824 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
3825 }
3826 else if (it->multibyte_p)
3827 {
3828 /* Implementation note: The calls to strlen apparently aren't a
3829 performance problem because there is no noticeable performance
3830 difference between Emacs running in unibyte or multibyte mode. */
3831 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
3832 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
3833 maxlen, &it->len);
3834 }
3835 else
3836 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
3837
3838 return success_p;
3839 }
3840
3841
3842 /* Set up IT to return characters from an ellipsis, if appropriate.
3843 The definition of the ellipsis glyphs may come from a display table
3844 entry. This function Fills IT with the first glyph from the
3845 ellipsis if an ellipsis is to be displayed. */
3846
3847 static int
3848 next_element_from_ellipsis (it)
3849 struct it *it;
3850 {
3851 if (it->selective_display_ellipsis_p)
3852 {
3853 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3854 {
3855 /* Use the display table definition for `...'. Invalid glyphs
3856 will be handled by the method returning elements from dpvec. */
3857 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3858 it->dpvec_char_len = it->len;
3859 it->dpvec = v->contents;
3860 it->dpend = v->contents + v->size;
3861 it->current.dpvec_index = 0;
3862 it->method = next_element_from_display_vector;
3863 }
3864 else
3865 {
3866 /* Use default `...' which is stored in default_invis_vector. */
3867 it->dpvec_char_len = it->len;
3868 it->dpvec = default_invis_vector;
3869 it->dpend = default_invis_vector + 3;
3870 it->current.dpvec_index = 0;
3871 it->method = next_element_from_display_vector;
3872 }
3873 }
3874 else
3875 reseat_at_next_visible_line_start (it, 1);
3876
3877 return get_next_display_element (it);
3878 }
3879
3880
3881 /* Deliver an image display element. The iterator IT is already
3882 filled with image information (done in handle_display_prop). Value
3883 is always 1. */
3884
3885
3886 static int
3887 next_element_from_image (it)
3888 struct it *it;
3889 {
3890 it->what = IT_IMAGE;
3891 return 1;
3892 }
3893
3894
3895 /* Fill iterator IT with next display element from a stretch glyph
3896 property. IT->object is the value of the text property. Value is
3897 always 1. */
3898
3899 static int
3900 next_element_from_stretch (it)
3901 struct it *it;
3902 {
3903 it->what = IT_STRETCH;
3904 return 1;
3905 }
3906
3907
3908 /* Load IT with the next display element from current_buffer. Value
3909 is zero if end of buffer reached. IT->stop_charpos is the next
3910 position at which to stop and check for text properties or buffer
3911 end. */
3912
3913 static int
3914 next_element_from_buffer (it)
3915 struct it *it;
3916 {
3917 int success_p = 1;
3918
3919 /* Check this assumption, otherwise, we would never enter the
3920 if-statement, below. */
3921 xassert (IT_CHARPOS (*it) >= BEGV
3922 && IT_CHARPOS (*it) <= it->stop_charpos);
3923
3924 if (IT_CHARPOS (*it) >= it->stop_charpos)
3925 {
3926 if (IT_CHARPOS (*it) >= it->end_charpos)
3927 {
3928 int overlay_strings_follow_p;
3929
3930 /* End of the game, except when overlay strings follow that
3931 haven't been returned yet. */
3932 if (it->overlay_strings_at_end_processed_p)
3933 overlay_strings_follow_p = 0;
3934 else
3935 {
3936 it->overlay_strings_at_end_processed_p = 1;
3937 overlay_strings_follow_p = get_overlay_strings (it);
3938 }
3939
3940 if (overlay_strings_follow_p)
3941 success_p = get_next_display_element (it);
3942 else
3943 {
3944 it->what = IT_EOB;
3945 it->position = it->current.pos;
3946 success_p = 0;
3947 }
3948 }
3949 else
3950 {
3951 handle_stop (it);
3952 return get_next_display_element (it);
3953 }
3954 }
3955 else
3956 {
3957 /* No face changes, overlays etc. in sight, so just return a
3958 character from current_buffer. */
3959 unsigned char *p;
3960
3961 /* Maybe run the redisplay end trigger hook. Performance note:
3962 This doesn't seem to cost measurable time. */
3963 if (it->redisplay_end_trigger_charpos
3964 && it->glyph_row
3965 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
3966 run_redisplay_end_trigger_hook (it);
3967
3968 /* Get the next character, maybe multibyte. */
3969 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
3970 if (it->multibyte_p && !ASCII_BYTE_P (*p))
3971 {
3972 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
3973 - IT_BYTEPOS (*it));
3974 it->c = string_char_and_length (p, maxlen, &it->len);
3975 }
3976 else
3977 it->c = *p, it->len = 1;
3978
3979 /* Record what we have and where it came from. */
3980 it->what = IT_CHARACTER;;
3981 it->object = it->w->buffer;
3982 it->position = it->current.pos;
3983
3984 /* Normally we return the character found above, except when we
3985 really want to return an ellipsis for selective display. */
3986 if (it->selective)
3987 {
3988 if (it->c == '\n')
3989 {
3990 /* A value of selective > 0 means hide lines indented more
3991 than that number of columns. */
3992 if (it->selective > 0
3993 && IT_CHARPOS (*it) + 1 < ZV
3994 && indented_beyond_p (IT_CHARPOS (*it) + 1,
3995 IT_BYTEPOS (*it) + 1,
3996 it->selective))
3997 {
3998 success_p = next_element_from_ellipsis (it);
3999 it->dpvec_char_len = -1;
4000 }
4001 }
4002 else if (it->c == '\r' && it->selective == -1)
4003 {
4004 /* A value of selective == -1 means that everything from the
4005 CR to the end of the line is invisible, with maybe an
4006 ellipsis displayed for it. */
4007 success_p = next_element_from_ellipsis (it);
4008 it->dpvec_char_len = -1;
4009 }
4010 }
4011 }
4012
4013 /* Value is zero if end of buffer reached. */
4014 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
4015 return success_p;
4016 }
4017
4018
4019 /* Run the redisplay end trigger hook for IT. */
4020
4021 static void
4022 run_redisplay_end_trigger_hook (it)
4023 struct it *it;
4024 {
4025 Lisp_Object args[3];
4026
4027 /* IT->glyph_row should be non-null, i.e. we should be actually
4028 displaying something, or otherwise we should not run the hook. */
4029 xassert (it->glyph_row);
4030
4031 /* Set up hook arguments. */
4032 args[0] = Qredisplay_end_trigger_functions;
4033 args[1] = it->window;
4034 XSETINT (args[2], it->redisplay_end_trigger_charpos);
4035 it->redisplay_end_trigger_charpos = 0;
4036
4037 /* Since we are *trying* to run these functions, don't try to run
4038 them again, even if they get an error. */
4039 it->w->redisplay_end_trigger = Qnil;
4040 Frun_hook_with_args (3, args);
4041
4042 /* Notice if it changed the face of the character we are on. */
4043 handle_face_prop (it);
4044 }
4045
4046
4047 /* Deliver a composition display element. The iterator IT is already
4048 filled with composition information (done in
4049 handle_composition_prop). Value is always 1. */
4050
4051 static int
4052 next_element_from_composition (it)
4053 struct it *it;
4054 {
4055 it->what = IT_COMPOSITION;
4056 it->position = (STRINGP (it->string)
4057 ? it->current.string_pos
4058 : it->current.pos);
4059 return 1;
4060 }
4061
4062
4063 \f
4064 /***********************************************************************
4065 Moving an iterator without producing glyphs
4066 ***********************************************************************/
4067
4068 /* Move iterator IT to a specified buffer or X position within one
4069 line on the display without producing glyphs.
4070
4071 Begin to skip at IT's current position. Skip to TO_CHARPOS or TO_X
4072 whichever is reached first.
4073
4074 TO_CHARPOS <= 0 means no TO_CHARPOS is specified.
4075
4076 TO_X < 0 means that no TO_X is specified. TO_X is normally a value
4077 0 <= TO_X <= IT->last_visible_x. This means in particular, that
4078 TO_X includes the amount by which a window is horizontally
4079 scrolled.
4080
4081 Value is
4082
4083 MOVE_POS_MATCH_OR_ZV
4084 - when TO_POS or ZV was reached.
4085
4086 MOVE_X_REACHED
4087 -when TO_X was reached before TO_POS or ZV were reached.
4088
4089 MOVE_LINE_CONTINUED
4090 - when we reached the end of the display area and the line must
4091 be continued.
4092
4093 MOVE_LINE_TRUNCATED
4094 - when we reached the end of the display area and the line is
4095 truncated.
4096
4097 MOVE_NEWLINE_OR_CR
4098 - when we stopped at a line end, i.e. a newline or a CR and selective
4099 display is on. */
4100
4101 static enum move_it_result
4102 move_it_in_display_line_to (it, to_charpos, to_x, op)
4103 struct it *it;
4104 int to_charpos, to_x, op;
4105 {
4106 enum move_it_result result = MOVE_UNDEFINED;
4107 struct glyph_row *saved_glyph_row;
4108
4109 /* Don't produce glyphs in produce_glyphs. */
4110 saved_glyph_row = it->glyph_row;
4111 it->glyph_row = NULL;
4112
4113 while (1)
4114 {
4115 int x, i;
4116
4117 /* Stop when ZV or TO_CHARPOS reached. */
4118 if (!get_next_display_element (it)
4119 || ((op & MOVE_TO_POS) != 0
4120 && BUFFERP (it->object)
4121 && IT_CHARPOS (*it) >= to_charpos))
4122 {
4123 result = MOVE_POS_MATCH_OR_ZV;
4124 break;
4125 }
4126
4127 /* The call to produce_glyphs will get the metrics of the
4128 display element IT is loaded with. We record in x the
4129 x-position before this display element in case it does not
4130 fit on the line. */
4131 x = it->current_x;
4132 PRODUCE_GLYPHS (it);
4133
4134 if (it->area != TEXT_AREA)
4135 {
4136 set_iterator_to_next (it);
4137 continue;
4138 }
4139
4140 /* The number of glyphs we get back in IT->nglyphs will normally
4141 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
4142 character on a terminal frame, or (iii) a line end. For the
4143 second case, IT->nglyphs - 1 padding glyphs will be present
4144 (on X frames, there is only one glyph produced for a
4145 composite character.
4146
4147 The behavior implemented below means, for continuation lines,
4148 that as many spaces of a TAB as fit on the current line are
4149 displayed there. For terminal frames, as many glyphs of a
4150 multi-glyph character are displayed in the current line, too.
4151 This is what the old redisplay code did, and we keep it that
4152 way. Under X, the whole shape of a complex character must
4153 fit on the line or it will be completely displayed in the
4154 next line.
4155
4156 Note that both for tabs and padding glyphs, all glyphs have
4157 the same width. */
4158 if (it->nglyphs)
4159 {
4160 /* More than one glyph or glyph doesn't fit on line. All
4161 glyphs have the same width. */
4162 int single_glyph_width = it->pixel_width / it->nglyphs;
4163 int new_x;
4164
4165 for (i = 0; i < it->nglyphs; ++i, x = new_x)
4166 {
4167 new_x = x + single_glyph_width;
4168
4169 /* We want to leave anything reaching TO_X to the caller. */
4170 if ((op & MOVE_TO_X) && new_x > to_x)
4171 {
4172 it->current_x = x;
4173 result = MOVE_X_REACHED;
4174 break;
4175 }
4176 else if (/* Lines are continued. */
4177 !it->truncate_lines_p
4178 && (/* And glyph doesn't fit on the line. */
4179 new_x > it->last_visible_x
4180 /* Or it fits exactly and we're on a window
4181 system frame. */
4182 || (new_x == it->last_visible_x
4183 && FRAME_WINDOW_P (it->f))))
4184 {
4185 if (/* IT->hpos == 0 means the very first glyph
4186 doesn't fit on the line, e.g. a wide image. */
4187 it->hpos == 0
4188 || (new_x == it->last_visible_x
4189 && FRAME_WINDOW_P (it->f)))
4190 {
4191 ++it->hpos;
4192 it->current_x = new_x;
4193 if (i == it->nglyphs - 1)
4194 set_iterator_to_next (it);
4195 }
4196 else
4197 it->current_x = x;
4198
4199 result = MOVE_LINE_CONTINUED;
4200 break;
4201 }
4202 else if (new_x > it->first_visible_x)
4203 {
4204 /* Glyph is visible. Increment number of glyphs that
4205 would be displayed. */
4206 ++it->hpos;
4207 }
4208 else
4209 {
4210 /* Glyph is completely off the left margin of the display
4211 area. Nothing to do. */
4212 }
4213 }
4214
4215 if (result != MOVE_UNDEFINED)
4216 break;
4217 }
4218 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
4219 {
4220 /* Stop when TO_X specified and reached. This check is
4221 necessary here because of lines consisting of a line end,
4222 only. The line end will not produce any glyphs and we
4223 would never get MOVE_X_REACHED. */
4224 xassert (it->nglyphs == 0);
4225 result = MOVE_X_REACHED;
4226 break;
4227 }
4228
4229 /* Is this a line end? If yes, we're done. */
4230 if (ITERATOR_AT_END_OF_LINE_P (it))
4231 {
4232 result = MOVE_NEWLINE_OR_CR;
4233 break;
4234 }
4235
4236 /* The current display element has been consumed. Advance
4237 to the next. */
4238 set_iterator_to_next (it);
4239
4240 /* Stop if lines are truncated and IT's current x-position is
4241 past the right edge of the window now. */
4242 if (it->truncate_lines_p
4243 && it->current_x >= it->last_visible_x)
4244 {
4245 result = MOVE_LINE_TRUNCATED;
4246 break;
4247 }
4248 }
4249
4250 /* Restore the iterator settings altered at the beginning of this
4251 function. */
4252 it->glyph_row = saved_glyph_row;
4253 return result;
4254 }
4255
4256
4257 /* Move IT forward to a specified buffer position TO_CHARPOS, TO_X,
4258 TO_Y, TO_VPOS. OP is a bit-mask that specifies where to stop. See
4259 the description of enum move_operation_enum.
4260
4261 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
4262 screen line, this function will set IT to the next position >
4263 TO_CHARPOS. */
4264
4265 void
4266 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
4267 struct it *it;
4268 int to_charpos, to_x, to_y, to_vpos;
4269 int op;
4270 {
4271 enum move_it_result skip, skip2 = MOVE_X_REACHED;
4272 int line_height;
4273
4274 while (1)
4275 {
4276 if (op & MOVE_TO_VPOS)
4277 {
4278 /* If no TO_CHARPOS and no TO_X specified, stop at the
4279 start of the line TO_VPOS. */
4280 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
4281 {
4282 if (it->vpos == to_vpos)
4283 break;
4284 skip = move_it_in_display_line_to (it, -1, -1, 0);
4285 }
4286 else
4287 {
4288 /* TO_VPOS >= 0 means stop at TO_X in the line at
4289 TO_VPOS, or at TO_POS, whichever comes first. */
4290 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
4291
4292 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
4293 break;
4294 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
4295 {
4296 /* We have reached TO_X but not in the line we want. */
4297 skip = move_it_in_display_line_to (it, to_charpos,
4298 -1, MOVE_TO_POS);
4299 if (skip == MOVE_POS_MATCH_OR_ZV)
4300 break;
4301 }
4302 }
4303 }
4304 else if (op & MOVE_TO_Y)
4305 {
4306 struct it it_backup;
4307 int done_p;
4308
4309 /* TO_Y specified means stop at TO_X in the line containing
4310 TO_Y---or at TO_CHARPOS if this is reached first. The
4311 problem is that we can't really tell whether the line
4312 contains TO_Y before we have completely scanned it, and
4313 this may skip past TO_X. What we do is to first scan to
4314 TO_X.
4315
4316 If TO_X is not specified, use a TO_X of zero. The reason
4317 is to make the outcome of this function more predictable.
4318 If we didn't use TO_X == 0, we would stop at the end of
4319 the line which is probably not what a caller would expect
4320 to happen. */
4321 skip = move_it_in_display_line_to (it, to_charpos,
4322 ((op & MOVE_TO_X)
4323 ? to_x : 0),
4324 (MOVE_TO_X
4325 | (op & MOVE_TO_POS)));
4326
4327 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
4328 if (skip == MOVE_POS_MATCH_OR_ZV)
4329 break;
4330
4331 /* If TO_X was reached, we would like to know whether TO_Y
4332 is in the line. This can only be said if we know the
4333 total line height which requires us to scan the rest of
4334 the line. */
4335 done_p = 0;
4336 if (skip == MOVE_X_REACHED)
4337 {
4338 it_backup = *it;
4339 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
4340 op & MOVE_TO_POS);
4341 }
4342
4343 /* Now, decide whether TO_Y is in this line. */
4344 line_height = it->max_ascent + it->max_descent;
4345
4346 if (to_y >= it->current_y
4347 && to_y < it->current_y + line_height)
4348 {
4349 if (skip == MOVE_X_REACHED)
4350 /* If TO_Y is in this line and TO_X was reached above,
4351 we scanned too far. We have to restore IT's settings
4352 to the ones before skipping. */
4353 *it = it_backup;
4354 done_p = 1;
4355 }
4356 else if (skip == MOVE_X_REACHED)
4357 {
4358 skip = skip2;
4359 if (skip == MOVE_POS_MATCH_OR_ZV)
4360 done_p = 1;
4361 }
4362
4363 if (done_p)
4364 break;
4365 }
4366 else
4367 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
4368
4369 switch (skip)
4370 {
4371 case MOVE_POS_MATCH_OR_ZV:
4372 return;
4373
4374 case MOVE_NEWLINE_OR_CR:
4375 set_iterator_to_next (it);
4376 it->continuation_lines_width = 0;
4377 break;
4378
4379 case MOVE_LINE_TRUNCATED:
4380 it->continuation_lines_width = 0;
4381 reseat_at_next_visible_line_start (it, 0);
4382 if ((op & MOVE_TO_POS) != 0
4383 && IT_CHARPOS (*it) > to_charpos)
4384 goto out;
4385 break;
4386
4387 case MOVE_LINE_CONTINUED:
4388 it->continuation_lines_width += it->current_x;
4389 break;
4390
4391 default:
4392 abort ();
4393 }
4394
4395 /* Reset/increment for the next run. */
4396 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
4397 it->current_x = it->hpos = 0;
4398 it->current_y += it->max_ascent + it->max_descent;
4399 ++it->vpos;
4400 last_height = it->max_ascent + it->max_descent;
4401 last_max_ascent = it->max_ascent;
4402 it->max_ascent = it->max_descent = 0;
4403 }
4404 out:;
4405 }
4406
4407
4408 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
4409
4410 If DY > 0, move IT backward at least that many pixels. DY = 0
4411 means move IT backward to the preceding line start or BEGV. This
4412 function may move over more than DY pixels if IT->current_y - DY
4413 ends up in the middle of a line; in this case IT->current_y will be
4414 set to the top of the line moved to. */
4415
4416 void
4417 move_it_vertically_backward (it, dy)
4418 struct it *it;
4419 int dy;
4420 {
4421 int nlines, h, line_height;
4422 struct it it2;
4423 int start_pos = IT_CHARPOS (*it);
4424
4425 xassert (dy >= 0);
4426
4427 /* Estimate how many newlines we must move back. */
4428 nlines = max (1, dy / CANON_Y_UNIT (it->f));
4429
4430 /* Set the iterator's position that many lines back. */
4431 while (nlines-- && IT_CHARPOS (*it) > BEGV)
4432 back_to_previous_visible_line_start (it);
4433
4434 /* Reseat the iterator here. When moving backward, we don't want
4435 reseat to skip forward over invisible text, set up the iterator
4436 to deliver from overlay strings at the new position etc. So,
4437 use reseat_1 here. */
4438 reseat_1 (it, it->current.pos, 1);
4439
4440 /* We are now surely at a line start. */
4441 it->current_x = it->hpos = 0;
4442
4443 /* Move forward and see what y-distance we moved. First move to the
4444 start of the next line so that we get its height. We need this
4445 height to be able to tell whether we reached the specified
4446 y-distance. */
4447 it2 = *it;
4448 it2.max_ascent = it2.max_descent = 0;
4449 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
4450 MOVE_TO_POS | MOVE_TO_VPOS);
4451 xassert (IT_CHARPOS (*it) >= BEGV);
4452 line_height = it2.max_ascent + it2.max_descent;
4453 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
4454 xassert (IT_CHARPOS (*it) >= BEGV);
4455 h = it2.current_y - it->current_y;
4456 nlines = it2.vpos - it->vpos;
4457
4458 /* Correct IT's y and vpos position. */
4459 it->vpos -= nlines;
4460 it->current_y -= h;
4461
4462 if (dy == 0)
4463 {
4464 /* DY == 0 means move to the start of the screen line. The
4465 value of nlines is > 0 if continuation lines were involved. */
4466 if (nlines > 0)
4467 move_it_by_lines (it, nlines, 1);
4468 xassert (IT_CHARPOS (*it) <= start_pos);
4469 }
4470 else if (nlines)
4471 {
4472 /* The y-position we try to reach. Note that h has been
4473 subtracted in front of the if-statement. */
4474 int target_y = it->current_y + h - dy;
4475
4476 /* If we did not reach target_y, try to move further backward if
4477 we can. If we moved too far backward, try to move forward. */
4478 if (target_y < it->current_y
4479 && IT_CHARPOS (*it) > BEGV)
4480 {
4481 move_it_vertically (it, target_y - it->current_y);
4482 xassert (IT_CHARPOS (*it) >= BEGV);
4483 }
4484 else if (target_y >= it->current_y + line_height
4485 && IT_CHARPOS (*it) < ZV)
4486 {
4487 move_it_vertically (it, target_y - (it->current_y + line_height));
4488 xassert (IT_CHARPOS (*it) >= BEGV);
4489 }
4490 }
4491 }
4492
4493
4494 /* Move IT by a specified amount of pixel lines DY. DY negative means
4495 move backwards. DY = 0 means move to start of screen line. At the
4496 end, IT will be on the start of a screen line. */
4497
4498 void
4499 move_it_vertically (it, dy)
4500 struct it *it;
4501 int dy;
4502 {
4503 if (dy <= 0)
4504 move_it_vertically_backward (it, -dy);
4505 else if (dy > 0)
4506 {
4507 move_it_to (it, ZV, -1, it->current_y + dy, -1,
4508 MOVE_TO_POS | MOVE_TO_Y);
4509
4510 /* If buffer ends in ZV without a newline, move to the start of
4511 the line to satisfy the post-condition. */
4512 if (IT_CHARPOS (*it) == ZV
4513 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
4514 move_it_by_lines (it, 0, 0);
4515 }
4516 }
4517
4518
4519 /* Return non-zero if some text between buffer positions START_CHARPOS
4520 and END_CHARPOS is invisible. IT->window is the window for text
4521 property lookup. */
4522
4523 static int
4524 invisible_text_between_p (it, start_charpos, end_charpos)
4525 struct it *it;
4526 int start_charpos, end_charpos;
4527 {
4528 Lisp_Object prop, limit;
4529 int invisible_found_p;
4530
4531 xassert (it != NULL && start_charpos <= end_charpos);
4532
4533 /* Is text at START invisible? */
4534 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
4535 it->window);
4536 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4537 invisible_found_p = 1;
4538 else
4539 {
4540 limit = next_single_char_property_change (make_number (start_charpos),
4541 Qinvisible, Qnil,
4542 make_number (end_charpos));
4543 invisible_found_p = XFASTINT (limit) < end_charpos;
4544 }
4545
4546 return invisible_found_p;
4547 }
4548
4549
4550 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
4551 negative means move up. DVPOS == 0 means move to the start of the
4552 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
4553 NEED_Y_P is zero, IT->current_y will be left unchanged.
4554
4555 Further optimization ideas: If we would know that IT->f doesn't use
4556 a face with proportional font, we could be faster for
4557 truncate-lines nil. */
4558
4559 void
4560 move_it_by_lines (it, dvpos, need_y_p)
4561 struct it *it;
4562 int dvpos, need_y_p;
4563 {
4564 struct position pos;
4565
4566 if (!FRAME_WINDOW_P (it->f))
4567 {
4568 struct text_pos textpos;
4569
4570 /* We can use vmotion on frames without proportional fonts. */
4571 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
4572 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
4573 reseat (it, textpos, 1);
4574 it->vpos += pos.vpos;
4575 it->current_y += pos.vpos;
4576 }
4577 else if (dvpos == 0)
4578 {
4579 /* DVPOS == 0 means move to the start of the screen line. */
4580 move_it_vertically_backward (it, 0);
4581 xassert (it->current_x == 0 && it->hpos == 0);
4582 }
4583 else if (dvpos > 0)
4584 {
4585 /* If there are no continuation lines, and if there is no
4586 selective display, try the simple method of moving forward
4587 DVPOS newlines, then see where we are. */
4588 if (!need_y_p && it->truncate_lines_p && it->selective == 0)
4589 {
4590 int shortage = 0, charpos;
4591
4592 if (FETCH_BYTE (IT_BYTEPOS (*it) == '\n'))
4593 charpos = IT_CHARPOS (*it) + 1;
4594 else
4595 charpos = scan_buffer ('\n', IT_CHARPOS (*it), 0, dvpos,
4596 &shortage, 0);
4597
4598 if (!invisible_text_between_p (it, IT_CHARPOS (*it), charpos))
4599 {
4600 struct text_pos pos;
4601 CHARPOS (pos) = charpos;
4602 BYTEPOS (pos) = CHAR_TO_BYTE (charpos);
4603 reseat (it, pos, 1);
4604 it->vpos += dvpos - shortage;
4605 it->hpos = it->current_x = 0;
4606 return;
4607 }
4608 }
4609
4610 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
4611 }
4612 else
4613 {
4614 struct it it2;
4615 int start_charpos, i;
4616
4617 /* If there are no continuation lines, and if there is no
4618 selective display, try the simple method of moving backward
4619 -DVPOS newlines. */
4620 if (!need_y_p && it->truncate_lines_p && it->selective == 0)
4621 {
4622 int shortage;
4623 int charpos = IT_CHARPOS (*it);
4624 int bytepos = IT_BYTEPOS (*it);
4625
4626 /* If in the middle of a line, go to its start. */
4627 if (charpos > BEGV && FETCH_BYTE (bytepos - 1) != '\n')
4628 {
4629 charpos = find_next_newline_no_quit (charpos, -1);
4630 bytepos = CHAR_TO_BYTE (charpos);
4631 }
4632
4633 if (charpos == BEGV)
4634 {
4635 struct text_pos pos;
4636 CHARPOS (pos) = charpos;
4637 BYTEPOS (pos) = bytepos;
4638 reseat (it, pos, 1);
4639 it->hpos = it->current_x = 0;
4640 return;
4641 }
4642 else
4643 {
4644 charpos = scan_buffer ('\n', charpos - 1, 0, dvpos, &shortage, 0);
4645 if (!invisible_text_between_p (it, charpos, IT_CHARPOS (*it)))
4646 {
4647 struct text_pos pos;
4648 CHARPOS (pos) = charpos;
4649 BYTEPOS (pos) = CHAR_TO_BYTE (charpos);
4650 reseat (it, pos, 1);
4651 it->vpos += dvpos + (shortage ? shortage - 1 : 0);
4652 it->hpos = it->current_x = 0;
4653 return;
4654 }
4655 }
4656 }
4657
4658 /* Go back -DVPOS visible lines and reseat the iterator there. */
4659 start_charpos = IT_CHARPOS (*it);
4660 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
4661 back_to_previous_visible_line_start (it);
4662 reseat (it, it->current.pos, 1);
4663 it->current_x = it->hpos = 0;
4664
4665 /* Above call may have moved too far if continuation lines
4666 are involved. Scan forward and see if it did. */
4667 it2 = *it;
4668 it2.vpos = it2.current_y = 0;
4669 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
4670 it->vpos -= it2.vpos;
4671 it->current_y -= it2.current_y;
4672 it->current_x = it->hpos = 0;
4673
4674 /* If we moved too far, move IT some lines forward. */
4675 if (it2.vpos > -dvpos)
4676 {
4677 int delta = it2.vpos + dvpos;
4678 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
4679 }
4680 }
4681 }
4682
4683
4684 \f
4685 /***********************************************************************
4686 Messages
4687 ***********************************************************************/
4688
4689
4690 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
4691 to *Messages*. */
4692
4693 void
4694 add_to_log (format, arg1, arg2)
4695 char *format;
4696 Lisp_Object arg1, arg2;
4697 {
4698 Lisp_Object args[3];
4699 Lisp_Object msg, fmt;
4700 char *buffer;
4701 int len;
4702 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4703
4704 fmt = msg = Qnil;
4705 GCPRO4 (fmt, msg, arg1, arg2);
4706
4707 args[0] = fmt = build_string (format);
4708 args[1] = arg1;
4709 args[2] = arg2;
4710 msg = Fformat (3, args);
4711
4712 len = STRING_BYTES (XSTRING (msg)) + 1;
4713 buffer = (char *) alloca (len);
4714 strcpy (buffer, XSTRING (msg)->data);
4715
4716 message_dolog (buffer, len - 1, 1, 0);
4717 UNGCPRO;
4718 }
4719
4720
4721 /* Output a newline in the *Messages* buffer if "needs" one. */
4722
4723 void
4724 message_log_maybe_newline ()
4725 {
4726 if (message_log_need_newline)
4727 message_dolog ("", 0, 1, 0);
4728 }
4729
4730
4731 /* Add a string M of length LEN to the message log, optionally
4732 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
4733 nonzero, means interpret the contents of M as multibyte. This
4734 function calls low-level routines in order to bypass text property
4735 hooks, etc. which might not be safe to run. */
4736
4737 void
4738 message_dolog (m, len, nlflag, multibyte)
4739 char *m;
4740 int len, nlflag, multibyte;
4741 {
4742 if (!NILP (Vmessage_log_max))
4743 {
4744 struct buffer *oldbuf;
4745 Lisp_Object oldpoint, oldbegv, oldzv;
4746 int old_windows_or_buffers_changed = windows_or_buffers_changed;
4747 int point_at_end = 0;
4748 int zv_at_end = 0;
4749 Lisp_Object old_deactivate_mark, tem;
4750 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4751
4752 old_deactivate_mark = Vdeactivate_mark;
4753 oldbuf = current_buffer;
4754 Fset_buffer (Fget_buffer_create (build_string ("*Messages*")));
4755 current_buffer->undo_list = Qt;
4756
4757 oldpoint = Fpoint_marker ();
4758 oldbegv = Fpoint_min_marker ();
4759 oldzv = Fpoint_max_marker ();
4760 GCPRO4 (oldpoint, oldbegv, oldzv, old_deactivate_mark);
4761
4762 if (PT == Z)
4763 point_at_end = 1;
4764 if (ZV == Z)
4765 zv_at_end = 1;
4766
4767 BEGV = BEG;
4768 BEGV_BYTE = BEG_BYTE;
4769 ZV = Z;
4770 ZV_BYTE = Z_BYTE;
4771 TEMP_SET_PT_BOTH (Z, Z_BYTE);
4772
4773 /* Insert the string--maybe converting multibyte to single byte
4774 or vice versa, so that all the text fits the buffer. */
4775 if (multibyte
4776 && NILP (current_buffer->enable_multibyte_characters))
4777 {
4778 int i, c, nbytes;
4779 unsigned char work[1];
4780
4781 /* Convert a multibyte string to single-byte
4782 for the *Message* buffer. */
4783 for (i = 0; i < len; i += nbytes)
4784 {
4785 c = string_char_and_length (m + i, len - i, &nbytes);
4786 work[0] = (SINGLE_BYTE_CHAR_P (c)
4787 ? c
4788 : multibyte_char_to_unibyte (c, Qnil));
4789 insert_1_both (work, 1, 1, 1, 0, 0);
4790 }
4791 }
4792 else if (! multibyte
4793 && ! NILP (current_buffer->enable_multibyte_characters))
4794 {
4795 int i, c, nbytes;
4796 unsigned char *msg = (unsigned char *) m;
4797 unsigned char str[MAX_MULTIBYTE_LENGTH];
4798 /* Convert a single-byte string to multibyte
4799 for the *Message* buffer. */
4800 for (i = 0; i < len; i++)
4801 {
4802 c = unibyte_char_to_multibyte (msg[i]);
4803 nbytes = CHAR_STRING (c, str);
4804 insert_1_both (str, 1, nbytes, 1, 0, 0);
4805 }
4806 }
4807 else if (len)
4808 insert_1 (m, len, 1, 0, 0);
4809
4810 if (nlflag)
4811 {
4812 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
4813 insert_1 ("\n", 1, 1, 0, 0);
4814
4815 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
4816 this_bol = PT;
4817 this_bol_byte = PT_BYTE;
4818
4819 if (this_bol > BEG)
4820 {
4821 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
4822 prev_bol = PT;
4823 prev_bol_byte = PT_BYTE;
4824
4825 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
4826 this_bol, this_bol_byte);
4827 if (dup)
4828 {
4829 del_range_both (prev_bol, prev_bol_byte,
4830 this_bol, this_bol_byte, 0);
4831 if (dup > 1)
4832 {
4833 char dupstr[40];
4834 int duplen;
4835
4836 /* If you change this format, don't forget to also
4837 change message_log_check_duplicate. */
4838 sprintf (dupstr, " [%d times]", dup);
4839 duplen = strlen (dupstr);
4840 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
4841 insert_1 (dupstr, duplen, 1, 0, 1);
4842 }
4843 }
4844 }
4845
4846 if (NATNUMP (Vmessage_log_max))
4847 {
4848 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
4849 -XFASTINT (Vmessage_log_max) - 1, 0);
4850 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
4851 }
4852 }
4853 BEGV = XMARKER (oldbegv)->charpos;
4854 BEGV_BYTE = marker_byte_position (oldbegv);
4855
4856 if (zv_at_end)
4857 {
4858 ZV = Z;
4859 ZV_BYTE = Z_BYTE;
4860 }
4861 else
4862 {
4863 ZV = XMARKER (oldzv)->charpos;
4864 ZV_BYTE = marker_byte_position (oldzv);
4865 }
4866
4867 if (point_at_end)
4868 TEMP_SET_PT_BOTH (Z, Z_BYTE);
4869 else
4870 /* We can't do Fgoto_char (oldpoint) because it will run some
4871 Lisp code. */
4872 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
4873 XMARKER (oldpoint)->bytepos);
4874
4875 UNGCPRO;
4876 free_marker (oldpoint);
4877 free_marker (oldbegv);
4878 free_marker (oldzv);
4879
4880 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
4881 set_buffer_internal (oldbuf);
4882 if (NILP (tem))
4883 windows_or_buffers_changed = old_windows_or_buffers_changed;
4884 message_log_need_newline = !nlflag;
4885 Vdeactivate_mark = old_deactivate_mark;
4886 }
4887 }
4888
4889
4890 /* We are at the end of the buffer after just having inserted a newline.
4891 (Note: We depend on the fact we won't be crossing the gap.)
4892 Check to see if the most recent message looks a lot like the previous one.
4893 Return 0 if different, 1 if the new one should just replace it, or a
4894 value N > 1 if we should also append " [N times]". */
4895
4896 static int
4897 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
4898 int prev_bol, this_bol;
4899 int prev_bol_byte, this_bol_byte;
4900 {
4901 int i;
4902 int len = Z_BYTE - 1 - this_bol_byte;
4903 int seen_dots = 0;
4904 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
4905 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
4906
4907 for (i = 0; i < len; i++)
4908 {
4909 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.'
4910 && p1[i] != '\n')
4911 seen_dots = 1;
4912 if (p1[i] != p2[i])
4913 return seen_dots;
4914 }
4915 p1 += len;
4916 if (*p1 == '\n')
4917 return 2;
4918 if (*p1++ == ' ' && *p1++ == '[')
4919 {
4920 int n = 0;
4921 while (*p1 >= '0' && *p1 <= '9')
4922 n = n * 10 + *p1++ - '0';
4923 if (strncmp (p1, " times]\n", 8) == 0)
4924 return n+1;
4925 }
4926 return 0;
4927 }
4928
4929
4930 /* Display an echo area message M with a specified length of LEN
4931 chars. The string may include null characters. If M is 0, clear
4932 out any existing message, and let the mini-buffer text show through.
4933
4934 The buffer M must continue to exist until after the echo area gets
4935 cleared or some other message gets displayed there. This means do
4936 not pass text that is stored in a Lisp string; do not pass text in
4937 a buffer that was alloca'd. */
4938
4939 void
4940 message2 (m, len, multibyte)
4941 char *m;
4942 int len;
4943 int multibyte;
4944 {
4945 /* First flush out any partial line written with print. */
4946 message_log_maybe_newline ();
4947 if (m)
4948 message_dolog (m, len, 1, multibyte);
4949 message2_nolog (m, len, multibyte);
4950 }
4951
4952
4953 /* The non-logging counterpart of message2. */
4954
4955 void
4956 message2_nolog (m, len, multibyte)
4957 char *m;
4958 int len;
4959 {
4960 struct frame *sf = SELECTED_FRAME ();
4961 message_enable_multibyte = multibyte;
4962
4963 if (noninteractive)
4964 {
4965 if (noninteractive_need_newline)
4966 putc ('\n', stderr);
4967 noninteractive_need_newline = 0;
4968 if (m)
4969 fwrite (m, len, 1, stderr);
4970 if (cursor_in_echo_area == 0)
4971 fprintf (stderr, "\n");
4972 fflush (stderr);
4973 }
4974 /* A null message buffer means that the frame hasn't really been
4975 initialized yet. Error messages get reported properly by
4976 cmd_error, so this must be just an informative message; toss it. */
4977 else if (INTERACTIVE
4978 && sf->glyphs_initialized_p
4979 && FRAME_MESSAGE_BUF (sf))
4980 {
4981 Lisp_Object mini_window;
4982 struct frame *f;
4983
4984 /* Get the frame containing the mini-buffer
4985 that the selected frame is using. */
4986 mini_window = FRAME_MINIBUF_WINDOW (sf);
4987 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
4988
4989 FRAME_SAMPLE_VISIBILITY (f);
4990 if (FRAME_VISIBLE_P (sf)
4991 && ! FRAME_VISIBLE_P (f))
4992 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
4993
4994 if (m)
4995 {
4996 set_message (m, Qnil, len, multibyte);
4997 if (minibuffer_auto_raise)
4998 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
4999 }
5000 else
5001 clear_message (1, 1);
5002
5003 do_pending_window_change (0);
5004 echo_area_display (1);
5005 do_pending_window_change (0);
5006 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5007 (*frame_up_to_date_hook) (f);
5008 }
5009 }
5010
5011
5012 /* Display an echo area message M with a specified length of NBYTES
5013 bytes. The string may include null characters. If M is not a
5014 string, clear out any existing message, and let the mini-buffer
5015 text show through. */
5016
5017 void
5018 message3 (m, nbytes, multibyte)
5019 Lisp_Object m;
5020 int nbytes;
5021 int multibyte;
5022 {
5023 struct gcpro gcpro1;
5024
5025 GCPRO1 (m);
5026
5027 /* First flush out any partial line written with print. */
5028 message_log_maybe_newline ();
5029 if (STRINGP (m))
5030 message_dolog (XSTRING (m)->data, nbytes, 1, multibyte);
5031 message3_nolog (m, nbytes, multibyte);
5032
5033 UNGCPRO;
5034 }
5035
5036
5037 /* The non-logging version of message3. */
5038
5039 void
5040 message3_nolog (m, nbytes, multibyte)
5041 Lisp_Object m;
5042 int nbytes, multibyte;
5043 {
5044 struct frame *sf = SELECTED_FRAME ();
5045 message_enable_multibyte = multibyte;
5046
5047 if (noninteractive)
5048 {
5049 if (noninteractive_need_newline)
5050 putc ('\n', stderr);
5051 noninteractive_need_newline = 0;
5052 if (STRINGP (m))
5053 fwrite (XSTRING (m)->data, nbytes, 1, stderr);
5054 if (cursor_in_echo_area == 0)
5055 fprintf (stderr, "\n");
5056 fflush (stderr);
5057 }
5058 /* A null message buffer means that the frame hasn't really been
5059 initialized yet. Error messages get reported properly by
5060 cmd_error, so this must be just an informative message; toss it. */
5061 else if (INTERACTIVE
5062 && sf->glyphs_initialized_p
5063 && FRAME_MESSAGE_BUF (sf))
5064 {
5065 Lisp_Object mini_window;
5066 Lisp_Object frame;
5067 struct frame *f;
5068
5069 /* Get the frame containing the mini-buffer
5070 that the selected frame is using. */
5071 mini_window = FRAME_MINIBUF_WINDOW (sf);
5072 frame = XWINDOW (mini_window)->frame;
5073 f = XFRAME (frame);
5074
5075 FRAME_SAMPLE_VISIBILITY (f);
5076 if (FRAME_VISIBLE_P (sf)
5077 && !FRAME_VISIBLE_P (f))
5078 Fmake_frame_visible (frame);
5079
5080 if (STRINGP (m) && XSTRING (m)->size)
5081 {
5082 set_message (NULL, m, nbytes, multibyte);
5083 if (minibuffer_auto_raise)
5084 Fraise_frame (frame);
5085 }
5086 else
5087 clear_message (1, 1);
5088
5089 do_pending_window_change (0);
5090 echo_area_display (1);
5091 do_pending_window_change (0);
5092 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5093 (*frame_up_to_date_hook) (f);
5094 }
5095 }
5096
5097
5098 /* Display a null-terminated echo area message M. If M is 0, clear
5099 out any existing message, and let the mini-buffer text show through.
5100
5101 The buffer M must continue to exist until after the echo area gets
5102 cleared or some other message gets displayed there. Do not pass
5103 text that is stored in a Lisp string. Do not pass text in a buffer
5104 that was alloca'd. */
5105
5106 void
5107 message1 (m)
5108 char *m;
5109 {
5110 message2 (m, (m ? strlen (m) : 0), 0);
5111 }
5112
5113
5114 /* The non-logging counterpart of message1. */
5115
5116 void
5117 message1_nolog (m)
5118 char *m;
5119 {
5120 message2_nolog (m, (m ? strlen (m) : 0), 0);
5121 }
5122
5123 /* Display a message M which contains a single %s
5124 which gets replaced with STRING. */
5125
5126 void
5127 message_with_string (m, string, log)
5128 char *m;
5129 Lisp_Object string;
5130 int log;
5131 {
5132 if (noninteractive)
5133 {
5134 if (m)
5135 {
5136 if (noninteractive_need_newline)
5137 putc ('\n', stderr);
5138 noninteractive_need_newline = 0;
5139 fprintf (stderr, m, XSTRING (string)->data);
5140 if (cursor_in_echo_area == 0)
5141 fprintf (stderr, "\n");
5142 fflush (stderr);
5143 }
5144 }
5145 else if (INTERACTIVE)
5146 {
5147 /* The frame whose minibuffer we're going to display the message on.
5148 It may be larger than the selected frame, so we need
5149 to use its buffer, not the selected frame's buffer. */
5150 Lisp_Object mini_window;
5151 struct frame *f, *sf = SELECTED_FRAME ();
5152
5153 /* Get the frame containing the minibuffer
5154 that the selected frame is using. */
5155 mini_window = FRAME_MINIBUF_WINDOW (sf);
5156 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5157
5158 /* A null message buffer means that the frame hasn't really been
5159 initialized yet. Error messages get reported properly by
5160 cmd_error, so this must be just an informative message; toss it. */
5161 if (FRAME_MESSAGE_BUF (f))
5162 {
5163 int len;
5164 char *a[1];
5165 a[0] = (char *) XSTRING (string)->data;
5166
5167 len = doprnt (FRAME_MESSAGE_BUF (f),
5168 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5169
5170 if (log)
5171 message2 (FRAME_MESSAGE_BUF (f), len,
5172 STRING_MULTIBYTE (string));
5173 else
5174 message2_nolog (FRAME_MESSAGE_BUF (f), len,
5175 STRING_MULTIBYTE (string));
5176
5177 /* Print should start at the beginning of the message
5178 buffer next time. */
5179 message_buf_print = 0;
5180 }
5181 }
5182 }
5183
5184
5185 /* Dump an informative message to the minibuf. If M is 0, clear out
5186 any existing message, and let the mini-buffer text show through. */
5187
5188 /* VARARGS 1 */
5189 void
5190 message (m, a1, a2, a3)
5191 char *m;
5192 EMACS_INT a1, a2, a3;
5193 {
5194 if (noninteractive)
5195 {
5196 if (m)
5197 {
5198 if (noninteractive_need_newline)
5199 putc ('\n', stderr);
5200 noninteractive_need_newline = 0;
5201 fprintf (stderr, m, a1, a2, a3);
5202 if (cursor_in_echo_area == 0)
5203 fprintf (stderr, "\n");
5204 fflush (stderr);
5205 }
5206 }
5207 else if (INTERACTIVE)
5208 {
5209 /* The frame whose mini-buffer we're going to display the message
5210 on. It may be larger than the selected frame, so we need to
5211 use its buffer, not the selected frame's buffer. */
5212 Lisp_Object mini_window;
5213 struct frame *f, *sf = SELECTED_FRAME ();
5214
5215 /* Get the frame containing the mini-buffer
5216 that the selected frame is using. */
5217 mini_window = FRAME_MINIBUF_WINDOW (sf);
5218 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5219
5220 /* A null message buffer means that the frame hasn't really been
5221 initialized yet. Error messages get reported properly by
5222 cmd_error, so this must be just an informative message; toss
5223 it. */
5224 if (FRAME_MESSAGE_BUF (f))
5225 {
5226 if (m)
5227 {
5228 int len;
5229 #ifdef NO_ARG_ARRAY
5230 char *a[3];
5231 a[0] = (char *) a1;
5232 a[1] = (char *) a2;
5233 a[2] = (char *) a3;
5234
5235 len = doprnt (FRAME_MESSAGE_BUF (f),
5236 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5237 #else
5238 len = doprnt (FRAME_MESSAGE_BUF (f),
5239 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
5240 (char **) &a1);
5241 #endif /* NO_ARG_ARRAY */
5242
5243 message2 (FRAME_MESSAGE_BUF (f), len, 0);
5244 }
5245 else
5246 message1 (0);
5247
5248 /* Print should start at the beginning of the message
5249 buffer next time. */
5250 message_buf_print = 0;
5251 }
5252 }
5253 }
5254
5255
5256 /* The non-logging version of message. */
5257
5258 void
5259 message_nolog (m, a1, a2, a3)
5260 char *m;
5261 EMACS_INT a1, a2, a3;
5262 {
5263 Lisp_Object old_log_max;
5264 old_log_max = Vmessage_log_max;
5265 Vmessage_log_max = Qnil;
5266 message (m, a1, a2, a3);
5267 Vmessage_log_max = old_log_max;
5268 }
5269
5270
5271 /* Display the current message in the current mini-buffer. This is
5272 only called from error handlers in process.c, and is not time
5273 critical. */
5274
5275 void
5276 update_echo_area ()
5277 {
5278 if (!NILP (echo_area_buffer[0]))
5279 {
5280 Lisp_Object string;
5281 string = Fcurrent_message ();
5282 message3 (string, XSTRING (string)->size,
5283 !NILP (current_buffer->enable_multibyte_characters));
5284 }
5285 }
5286
5287
5288 /* Make sure echo area buffers in echo_buffers[] are life. If they
5289 aren't, make new ones. */
5290
5291 static void
5292 ensure_echo_area_buffers ()
5293 {
5294 int i;
5295
5296 for (i = 0; i < 2; ++i)
5297 if (!BUFFERP (echo_buffer[i])
5298 || NILP (XBUFFER (echo_buffer[i])->name))
5299 {
5300 char name[30];
5301 sprintf (name, " *Echo Area %d*", i);
5302 echo_buffer[i] = Fget_buffer_create (build_string (name));
5303 }
5304 }
5305
5306
5307 /* Call FN with args A1..A5 with either the current or last displayed
5308 echo_area_buffer as current buffer.
5309
5310 WHICH zero means use the current message buffer
5311 echo_area_buffer[0]. If that is nil, choose a suitable buffer
5312 from echo_buffer[] and clear it.
5313
5314 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
5315 suitable buffer from echo_buffer[] and clear it.
5316
5317 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
5318 that the current message becomes the last displayed one, make
5319 choose a suitable buffer for echo_area_buffer[0], and clear it.
5320
5321 Value is what FN returns. */
5322
5323 static int
5324 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10)
5325 struct window *w;
5326 int which;
5327 int (*fn) ();
5328 int a1, a2, a3, a4, a5, a6, a7, a8, a9, a10;
5329 {
5330 Lisp_Object buffer;
5331 int this_one, the_other, clear_buffer_p, rc;
5332 int count = specpdl_ptr - specpdl;
5333
5334 /* If buffers aren't life, make new ones. */
5335 ensure_echo_area_buffers ();
5336
5337 clear_buffer_p = 0;
5338
5339 if (which == 0)
5340 this_one = 0, the_other = 1;
5341 else if (which > 0)
5342 this_one = 1, the_other = 0;
5343 else
5344 {
5345 this_one = 0, the_other = 1;
5346 clear_buffer_p = 1;
5347
5348 /* We need a fresh one in case the current echo buffer equals
5349 the one containing the last displayed echo area message. */
5350 if (!NILP (echo_area_buffer[this_one])
5351 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
5352 echo_area_buffer[this_one] = Qnil;
5353 }
5354
5355 /* Choose a suitable buffer from echo_buffer[] is we don't
5356 have one. */
5357 if (NILP (echo_area_buffer[this_one]))
5358 {
5359 echo_area_buffer[this_one]
5360 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
5361 ? echo_buffer[the_other]
5362 : echo_buffer[this_one]);
5363 clear_buffer_p = 1;
5364 }
5365
5366 buffer = echo_area_buffer[this_one];
5367
5368 record_unwind_protect (unwind_with_echo_area_buffer,
5369 with_echo_area_buffer_unwind_data (w));
5370
5371 /* Make the echo area buffer current. Note that for display
5372 purposes, it is not necessary that the displayed window's buffer
5373 == current_buffer, except for text property lookup. So, let's
5374 only set that buffer temporarily here without doing a full
5375 Fset_window_buffer. We must also change w->pointm, though,
5376 because otherwise an assertions in unshow_buffer fails, and Emacs
5377 aborts. */
5378 set_buffer_internal_1 (XBUFFER (buffer));
5379 if (w)
5380 {
5381 w->buffer = buffer;
5382 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
5383 }
5384 current_buffer->truncate_lines = Qnil;
5385 current_buffer->undo_list = Qt;
5386 current_buffer->read_only = Qnil;
5387
5388 if (clear_buffer_p && Z > BEG)
5389 del_range (BEG, Z);
5390
5391 xassert (BEGV >= BEG);
5392 xassert (ZV <= Z && ZV >= BEGV);
5393
5394 rc = fn (a1, a2, a3, a4, a5);
5395
5396 xassert (BEGV >= BEG);
5397 xassert (ZV <= Z && ZV >= BEGV);
5398
5399 unbind_to (count, Qnil);
5400 return rc;
5401 }
5402
5403
5404 /* Save state that should be preserved around the call to the function
5405 FN called in with_echo_area_buffer. */
5406
5407 static Lisp_Object
5408 with_echo_area_buffer_unwind_data (w)
5409 struct window *w;
5410 {
5411 int i = 0;
5412 Lisp_Object vector;
5413
5414 /* Reduce consing by keeping one vector in
5415 Vwith_echo_area_save_vector. */
5416 vector = Vwith_echo_area_save_vector;
5417 Vwith_echo_area_save_vector = Qnil;
5418
5419 if (NILP (vector))
5420 vector = Fmake_vector (make_number (7), Qnil);
5421
5422 XSETBUFFER (XVECTOR (vector)->contents[i], current_buffer); ++i;
5423 XVECTOR (vector)->contents[i++] = Vdeactivate_mark;
5424 XVECTOR (vector)->contents[i++] = make_number (windows_or_buffers_changed);
5425
5426 if (w)
5427 {
5428 XSETWINDOW (XVECTOR (vector)->contents[i], w); ++i;
5429 XVECTOR (vector)->contents[i++] = w->buffer;
5430 XVECTOR (vector)->contents[i++]
5431 = make_number (XMARKER (w->pointm)->charpos);
5432 XVECTOR (vector)->contents[i++]
5433 = make_number (XMARKER (w->pointm)->bytepos);
5434 }
5435 else
5436 {
5437 int end = i + 4;
5438 while (i < end)
5439 XVECTOR (vector)->contents[i++] = Qnil;
5440 }
5441
5442 xassert (i == XVECTOR (vector)->size);
5443 return vector;
5444 }
5445
5446
5447 /* Restore global state from VECTOR which was created by
5448 with_echo_area_buffer_unwind_data. */
5449
5450 static Lisp_Object
5451 unwind_with_echo_area_buffer (vector)
5452 Lisp_Object vector;
5453 {
5454 int i = 0;
5455
5456 set_buffer_internal_1 (XBUFFER (XVECTOR (vector)->contents[i])); ++i;
5457 Vdeactivate_mark = XVECTOR (vector)->contents[i]; ++i;
5458 windows_or_buffers_changed = XFASTINT (XVECTOR (vector)->contents[i]); ++i;
5459
5460 if (WINDOWP (XVECTOR (vector)->contents[i]))
5461 {
5462 struct window *w;
5463 Lisp_Object buffer, charpos, bytepos;
5464
5465 w = XWINDOW (XVECTOR (vector)->contents[i]); ++i;
5466 buffer = XVECTOR (vector)->contents[i]; ++i;
5467 charpos = XVECTOR (vector)->contents[i]; ++i;
5468 bytepos = XVECTOR (vector)->contents[i]; ++i;
5469
5470 w->buffer = buffer;
5471 set_marker_both (w->pointm, buffer,
5472 XFASTINT (charpos), XFASTINT (bytepos));
5473 }
5474
5475 Vwith_echo_area_save_vector = vector;
5476 return Qnil;
5477 }
5478
5479
5480 /* Set up the echo area for use by print functions. MULTIBYTE_P
5481 non-zero means we will print multibyte. */
5482
5483 void
5484 setup_echo_area_for_printing (multibyte_p)
5485 int multibyte_p;
5486 {
5487 ensure_echo_area_buffers ();
5488
5489 if (!message_buf_print)
5490 {
5491 /* A message has been output since the last time we printed.
5492 Choose a fresh echo area buffer. */
5493 if (EQ (echo_area_buffer[1], echo_buffer[0]))
5494 echo_area_buffer[0] = echo_buffer[1];
5495 else
5496 echo_area_buffer[0] = echo_buffer[0];
5497
5498 /* Switch to that buffer and clear it. */
5499 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
5500 if (Z > BEG)
5501 del_range (BEG, Z);
5502 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
5503
5504 /* Set up the buffer for the multibyteness we need. */
5505 if (multibyte_p
5506 != !NILP (current_buffer->enable_multibyte_characters))
5507 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
5508
5509 /* Raise the frame containing the echo area. */
5510 if (minibuffer_auto_raise)
5511 {
5512 struct frame *sf = SELECTED_FRAME ();
5513 Lisp_Object mini_window;
5514 mini_window = FRAME_MINIBUF_WINDOW (sf);
5515 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5516 }
5517
5518 message_buf_print = 1;
5519 }
5520 else
5521 {
5522 if (NILP (echo_area_buffer[0]))
5523 {
5524 if (EQ (echo_area_buffer[1], echo_buffer[0]))
5525 echo_area_buffer[0] = echo_buffer[1];
5526 else
5527 echo_area_buffer[0] = echo_buffer[0];
5528 }
5529
5530 if (current_buffer != XBUFFER (echo_area_buffer[0]))
5531 /* Someone switched buffers between print requests. */
5532 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
5533 }
5534 }
5535
5536
5537 /* Display an echo area message in window W. Value is non-zero if W's
5538 height is changed. If display_last_displayed_message_p is
5539 non-zero, display the message that was last displayed, otherwise
5540 display the current message. */
5541
5542 static int
5543 display_echo_area (w)
5544 struct window *w;
5545 {
5546 int i, no_message_p, window_height_changed_p, count;
5547
5548 /* Temporarily disable garbage collections while displaying the echo
5549 area. This is done because a GC can print a message itself.
5550 That message would modify the echo area buffer's contents while a
5551 redisplay of the buffer is going on, and seriously confuse
5552 redisplay. */
5553 count = inhibit_garbage_collection ();
5554
5555 /* If there is no message, we must call display_echo_area_1
5556 nevertheless because it resizes the window. But we will have to
5557 reset the echo_area_buffer in question to nil at the end because
5558 with_echo_area_buffer will sets it to an empty buffer. */
5559 i = display_last_displayed_message_p ? 1 : 0;
5560 no_message_p = NILP (echo_area_buffer[i]);
5561
5562 window_height_changed_p
5563 = with_echo_area_buffer (w, display_last_displayed_message_p,
5564 (int (*) ()) display_echo_area_1, w);
5565
5566 if (no_message_p)
5567 echo_area_buffer[i] = Qnil;
5568
5569 unbind_to (count, Qnil);
5570 return window_height_changed_p;
5571 }
5572
5573
5574 /* Helper for display_echo_area. Display the current buffer which
5575 contains the current echo area message in window W, a mini-window.
5576 Change the height of W so that all of the message is displayed.
5577 Value is non-zero if height of W was changed. */
5578
5579 static int
5580 display_echo_area_1 (w)
5581 struct window *w;
5582 {
5583 Lisp_Object window;
5584 struct text_pos start;
5585 int window_height_changed_p = 0;
5586
5587 /* Do this before displaying, so that we have a large enough glyph
5588 matrix for the display. */
5589 window_height_changed_p = resize_mini_window (w, 0);
5590
5591 /* Display. */
5592 clear_glyph_matrix (w->desired_matrix);
5593 XSETWINDOW (window, w);
5594 SET_TEXT_POS (start, BEG, BEG_BYTE);
5595 try_window (window, start);
5596
5597 return window_height_changed_p;
5598 }
5599
5600
5601 /* Resize the echo area window to exactly the size needed for the
5602 currently displayed message, if there is one. */
5603
5604 void
5605 resize_echo_area_axactly ()
5606 {
5607 if (BUFFERP (echo_area_buffer[0])
5608 && WINDOWP (echo_area_window))
5609 {
5610 struct window *w = XWINDOW (echo_area_window);
5611 int resized_p;
5612
5613 resized_p = with_echo_area_buffer (w, 0,
5614 (int (*) ()) resize_mini_window,
5615 w, 1);
5616 if (resized_p)
5617 {
5618 ++windows_or_buffers_changed;
5619 ++update_mode_lines;
5620 redisplay_internal (0);
5621 }
5622 }
5623 }
5624
5625
5626 /* Resize mini-window W to fit the size of its contents. EXACT:P
5627 means size the window exactly to the size needed. Otherwise, it's
5628 only enlarged until W's buffer is empty. Value is non-zero if
5629 the window height has been changed. */
5630
5631 int
5632 resize_mini_window (w, exact_p)
5633 struct window *w;
5634 int exact_p;
5635 {
5636 struct frame *f = XFRAME (w->frame);
5637 int window_height_changed_p = 0;
5638
5639 xassert (MINI_WINDOW_P (w));
5640
5641 /* Nil means don't try to resize. */
5642 if (NILP (Vmax_mini_window_height)
5643 || (FRAME_X_P (f) && f->output_data.x == NULL))
5644 return 0;
5645
5646 if (!FRAME_MINIBUF_ONLY_P (f))
5647 {
5648 struct it it;
5649 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
5650 int total_height = XFASTINT (root->height) + XFASTINT (w->height);
5651 int height, max_height;
5652 int unit = CANON_Y_UNIT (f);
5653 struct text_pos start;
5654
5655 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
5656
5657 /* Compute the max. number of lines specified by the user. */
5658 if (FLOATP (Vmax_mini_window_height))
5659 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
5660 else if (INTEGERP (Vmax_mini_window_height))
5661 max_height = XINT (Vmax_mini_window_height);
5662 else
5663 max_height = total_height / 4;
5664
5665 /* Correct that max. height if it's bogus. */
5666 max_height = max (1, max_height);
5667 max_height = min (total_height, max_height);
5668
5669 /* Find out the height of the text in the window. */
5670 last_height = 0;
5671 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
5672 if (it.max_ascent == 0 && it.max_descent == 0)
5673 height = it.current_y + last_height;
5674 else
5675 height = it.current_y + it.max_ascent + it.max_descent;
5676 height = (height + unit - 1) / unit;
5677
5678 /* Compute a suitable window start. */
5679 if (height > max_height)
5680 {
5681 height = max_height;
5682 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
5683 move_it_vertically_backward (&it, (height - 1) * unit);
5684 start = it.current.pos;
5685 }
5686 else
5687 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
5688 SET_MARKER_FROM_TEXT_POS (w->start, start);
5689
5690 /* Let it grow only, until we display an empty message, in which
5691 case the window shrinks again. */
5692 if (height > XFASTINT (w->height))
5693 {
5694 int old_height = XFASTINT (w->height);
5695 freeze_window_starts (f, 1);
5696 grow_mini_window (w, height - XFASTINT (w->height));
5697 window_height_changed_p = XFASTINT (w->height) != old_height;
5698 }
5699 else if (height < XFASTINT (w->height)
5700 && (exact_p || BEGV == ZV))
5701 {
5702 int old_height = XFASTINT (w->height);
5703 freeze_window_starts (f, 0);
5704 shrink_mini_window (w);
5705 window_height_changed_p = XFASTINT (w->height) != old_height;
5706 }
5707 }
5708
5709 return window_height_changed_p;
5710 }
5711
5712
5713 /* Value is the current message, a string, or nil if there is no
5714 current message. */
5715
5716 Lisp_Object
5717 current_message ()
5718 {
5719 Lisp_Object msg;
5720
5721 if (NILP (echo_area_buffer[0]))
5722 msg = Qnil;
5723 else
5724 {
5725 with_echo_area_buffer (0, 0, (int (*) ()) current_message_1, &msg);
5726 if (NILP (msg))
5727 echo_area_buffer[0] = Qnil;
5728 }
5729
5730 return msg;
5731 }
5732
5733
5734 static int
5735 current_message_1 (msg)
5736 Lisp_Object *msg;
5737 {
5738 if (Z > BEG)
5739 *msg = make_buffer_string (BEG, Z, 1);
5740 else
5741 *msg = Qnil;
5742 return 0;
5743 }
5744
5745
5746 /* Push the current message on Vmessage_stack for later restauration
5747 by restore_message. Value is non-zero if the current message isn't
5748 empty. This is a relatively infrequent operation, so it's not
5749 worth optimizing. */
5750
5751 int
5752 push_message ()
5753 {
5754 Lisp_Object msg;
5755 msg = current_message ();
5756 Vmessage_stack = Fcons (msg, Vmessage_stack);
5757 return STRINGP (msg);
5758 }
5759
5760
5761 /* Restore message display from the top of Vmessage_stack. */
5762
5763 void
5764 restore_message ()
5765 {
5766 Lisp_Object msg;
5767
5768 xassert (CONSP (Vmessage_stack));
5769 msg = XCAR (Vmessage_stack);
5770 if (STRINGP (msg))
5771 message3_nolog (msg, STRING_BYTES (XSTRING (msg)), STRING_MULTIBYTE (msg));
5772 else
5773 message3_nolog (msg, 0, 0);
5774 }
5775
5776
5777 /* Pop the top-most entry off Vmessage_stack. */
5778
5779 void
5780 pop_message ()
5781 {
5782 xassert (CONSP (Vmessage_stack));
5783 Vmessage_stack = XCDR (Vmessage_stack);
5784 }
5785
5786
5787 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
5788 exits. If the stack is not empty, we have a missing pop_message
5789 somewhere. */
5790
5791 void
5792 check_message_stack ()
5793 {
5794 if (!NILP (Vmessage_stack))
5795 abort ();
5796 }
5797
5798
5799 /* Truncate to NCHARS what will be displayed in the echo area the next
5800 time we display it---but don't redisplay it now. */
5801
5802 void
5803 truncate_echo_area (nchars)
5804 int nchars;
5805 {
5806 if (nchars == 0)
5807 echo_area_buffer[0] = Qnil;
5808 /* A null message buffer means that the frame hasn't really been
5809 initialized yet. Error messages get reported properly by
5810 cmd_error, so this must be just an informative message; toss it. */
5811 else if (!noninteractive
5812 && INTERACTIVE
5813 && !NILP (echo_area_buffer[0]))
5814 {
5815 struct frame *sf = SELECTED_FRAME ();
5816 if (FRAME_MESSAGE_BUF (sf))
5817 with_echo_area_buffer (0, 0, (int (*) ()) truncate_message_1, nchars);
5818 }
5819 }
5820
5821
5822 /* Helper function for truncate_echo_area. Truncate the current
5823 message to at most NCHARS characters. */
5824
5825 static int
5826 truncate_message_1 (nchars)
5827 int nchars;
5828 {
5829 if (BEG + nchars < Z)
5830 del_range (BEG + nchars, Z);
5831 if (Z == BEG)
5832 echo_area_buffer[0] = Qnil;
5833 return 0;
5834 }
5835
5836
5837 /* Set the current message to a substring of S or STRING.
5838
5839 If STRING is a Lisp string, set the message to the first NBYTES
5840 bytes from STRING. NBYTES zero means use the whole string. If
5841 STRING is multibyte, the message will be displayed multibyte.
5842
5843 If S is not null, set the message to the first LEN bytes of S. LEN
5844 zero means use the whole string. MULTIBYTE_P non-zero means S is
5845 multibyte. Display the message multibyte in that case. */
5846
5847 void
5848 set_message (s, string, nbytes, multibyte_p)
5849 char *s;
5850 Lisp_Object string;
5851 int nbytes;
5852 {
5853 message_enable_multibyte
5854 = ((s && multibyte_p)
5855 || (STRINGP (string) && STRING_MULTIBYTE (string)));
5856
5857 with_echo_area_buffer (0, -1, (int (*) ()) set_message_1,
5858 s, string, nbytes, multibyte_p);
5859 message_buf_print = 0;
5860 }
5861
5862
5863 /* Helper function for set_message. Arguments have the same meaning
5864 as there. This function is called with the echo area buffer being
5865 current. */
5866
5867 static int
5868 set_message_1 (s, string, nbytes, multibyte_p)
5869 char *s;
5870 Lisp_Object string;
5871 int nbytes, multibyte_p;
5872 {
5873 xassert (BEG == Z);
5874
5875 /* Change multibyteness of the echo buffer appropriately. */
5876 if (message_enable_multibyte
5877 != !NILP (current_buffer->enable_multibyte_characters))
5878 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
5879
5880 /* Insert new message at BEG. */
5881 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
5882
5883 if (STRINGP (string))
5884 {
5885 int nchars;
5886
5887 if (nbytes == 0)
5888 nbytes = XSTRING (string)->size_byte;
5889 nchars = string_byte_to_char (string, nbytes);
5890
5891 /* This function takes care of single/multibyte conversion. We
5892 just have to ensure that the echo area buffer has the right
5893 setting of enable_multibyte_characters. */
5894 insert_from_string (string, 0, 0, nchars, nbytes, 1);
5895 }
5896 else if (s)
5897 {
5898 if (nbytes == 0)
5899 nbytes = strlen (s);
5900
5901 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
5902 {
5903 /* Convert from multi-byte to single-byte. */
5904 int i, c, n;
5905 unsigned char work[1];
5906
5907 /* Convert a multibyte string to single-byte. */
5908 for (i = 0; i < nbytes; i += n)
5909 {
5910 c = string_char_and_length (s + i, nbytes - i, &n);
5911 work[0] = (SINGLE_BYTE_CHAR_P (c)
5912 ? c
5913 : multibyte_char_to_unibyte (c, Qnil));
5914 insert_1_both (work, 1, 1, 1, 0, 0);
5915 }
5916 }
5917 else if (!multibyte_p
5918 && !NILP (current_buffer->enable_multibyte_characters))
5919 {
5920 /* Convert from single-byte to multi-byte. */
5921 int i, c, n;
5922 unsigned char *msg = (unsigned char *) s;
5923 unsigned char str[MAX_MULTIBYTE_LENGTH];
5924
5925 /* Convert a single-byte string to multibyte. */
5926 for (i = 0; i < nbytes; i++)
5927 {
5928 c = unibyte_char_to_multibyte (msg[i]);
5929 n = CHAR_STRING (c, str);
5930 insert_1_both (str, 1, n, 1, 0, 0);
5931 }
5932 }
5933 else
5934 insert_1 (s, nbytes, 1, 0, 0);
5935 }
5936
5937 return 0;
5938 }
5939
5940
5941 /* Clear messages. CURRENT_P non-zero means clear the current
5942 message. LAST_DISPLAYED_P non-zero means clear the message
5943 last displayed. */
5944
5945 void
5946 clear_message (current_p, last_displayed_p)
5947 int current_p, last_displayed_p;
5948 {
5949 if (current_p)
5950 echo_area_buffer[0] = Qnil;
5951
5952 if (last_displayed_p)
5953 echo_area_buffer[1] = Qnil;
5954
5955 message_buf_print = 0;
5956 }
5957
5958 /* Clear garbaged frames.
5959
5960 This function is used where the old redisplay called
5961 redraw_garbaged_frames which in turn called redraw_frame which in
5962 turn called clear_frame. The call to clear_frame was a source of
5963 flickering. I believe a clear_frame is not necessary. It should
5964 suffice in the new redisplay to invalidate all current matrices,
5965 and ensure a complete redisplay of all windows. */
5966
5967 static void
5968 clear_garbaged_frames ()
5969 {
5970 if (frame_garbaged)
5971 {
5972 Lisp_Object tail, frame;
5973
5974 FOR_EACH_FRAME (tail, frame)
5975 {
5976 struct frame *f = XFRAME (frame);
5977
5978 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
5979 {
5980 clear_current_matrices (f);
5981 f->garbaged = 0;
5982 }
5983 }
5984
5985 frame_garbaged = 0;
5986 ++windows_or_buffers_changed;
5987 }
5988 }
5989
5990
5991 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
5992 is non-zero update selected_frame. Value is non-zero if the
5993 mini-windows height has been changed. */
5994
5995 static int
5996 echo_area_display (update_frame_p)
5997 int update_frame_p;
5998 {
5999 Lisp_Object mini_window;
6000 struct window *w;
6001 struct frame *f;
6002 int window_height_changed_p = 0;
6003 struct frame *sf = SELECTED_FRAME ();
6004
6005 mini_window = FRAME_MINIBUF_WINDOW (sf);
6006 w = XWINDOW (mini_window);
6007 f = XFRAME (WINDOW_FRAME (w));
6008
6009 /* Don't display if frame is invisible or not yet initialized. */
6010 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
6011 return 0;
6012
6013 #ifdef HAVE_WINDOW_SYSTEM
6014 /* When Emacs starts, selected_frame may be a visible terminal
6015 frame, even if we run under a window system. If we let this
6016 through, a message would be displayed on the terminal. */
6017 if (EQ (selected_frame, Vterminal_frame)
6018 && !NILP (Vwindow_system))
6019 return 0;
6020 #endif /* HAVE_WINDOW_SYSTEM */
6021
6022 /* Redraw garbaged frames. */
6023 if (frame_garbaged)
6024 clear_garbaged_frames ();
6025
6026 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
6027 {
6028 echo_area_window = mini_window;
6029 window_height_changed_p = display_echo_area (w);
6030 w->must_be_updated_p = 1;
6031
6032 if (update_frame_p)
6033 {
6034 /* Not called from redisplay_internal. If we changed
6035 window configuration, we must redisplay thoroughly.
6036 Otherwise, we can do with updating what we displayed
6037 above. */
6038 if (window_height_changed_p)
6039 {
6040 ++windows_or_buffers_changed;
6041 ++update_mode_lines;
6042 redisplay_internal (0);
6043 }
6044 else if (FRAME_WINDOW_P (f))
6045 {
6046 update_single_window (w, 1);
6047 rif->flush_display (f);
6048 }
6049 else
6050 update_frame (f, 1, 1);
6051 }
6052 }
6053 else if (!EQ (mini_window, selected_window))
6054 windows_or_buffers_changed++;
6055
6056 /* Last displayed message is now the current message. */
6057 echo_area_buffer[1] = echo_area_buffer[0];
6058
6059 /* Prevent redisplay optimization in redisplay_internal by resetting
6060 this_line_start_pos. This is done because the mini-buffer now
6061 displays the message instead of its buffer text. */
6062 if (EQ (mini_window, selected_window))
6063 CHARPOS (this_line_start_pos) = 0;
6064
6065 return window_height_changed_p;
6066 }
6067
6068
6069 \f
6070 /***********************************************************************
6071 Frame Titles
6072 ***********************************************************************/
6073
6074
6075 #ifdef HAVE_WINDOW_SYSTEM
6076
6077 /* A buffer for constructing frame titles in it; allocated from the
6078 heap in init_xdisp and resized as needed in store_frame_title_char. */
6079
6080 static char *frame_title_buf;
6081
6082 /* The buffer's end, and a current output position in it. */
6083
6084 static char *frame_title_buf_end;
6085 static char *frame_title_ptr;
6086
6087
6088 /* Store a single character C for the frame title in frame_title_buf.
6089 Re-allocate frame_title_buf if necessary. */
6090
6091 static void
6092 store_frame_title_char (c)
6093 char c;
6094 {
6095 /* If output position has reached the end of the allocated buffer,
6096 double the buffer's size. */
6097 if (frame_title_ptr == frame_title_buf_end)
6098 {
6099 int len = frame_title_ptr - frame_title_buf;
6100 int new_size = 2 * len * sizeof *frame_title_buf;
6101 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
6102 frame_title_buf_end = frame_title_buf + new_size;
6103 frame_title_ptr = frame_title_buf + len;
6104 }
6105
6106 *frame_title_ptr++ = c;
6107 }
6108
6109
6110 /* Store part of a frame title in frame_title_buf, beginning at
6111 frame_title_ptr. STR is the string to store. Do not copy more
6112 than PRECISION number of bytes from STR; PRECISION <= 0 means copy
6113 the whole string. Pad with spaces until FIELD_WIDTH number of
6114 characters have been copied; FIELD_WIDTH <= 0 means don't pad.
6115 Called from display_mode_element when it is used to build a frame
6116 title. */
6117
6118 static int
6119 store_frame_title (str, field_width, precision)
6120 unsigned char *str;
6121 int field_width, precision;
6122 {
6123 int n = 0;
6124
6125 /* Copy at most PRECISION chars from STR. */
6126 while ((precision <= 0 || n < precision)
6127 && *str)
6128 {
6129 store_frame_title_char (*str++);
6130 ++n;
6131 }
6132
6133 /* Fill up with spaces until FIELD_WIDTH reached. */
6134 while (field_width > 0
6135 && n < field_width)
6136 {
6137 store_frame_title_char (' ');
6138 ++n;
6139 }
6140
6141 return n;
6142 }
6143
6144
6145 /* Set the title of FRAME, if it has changed. The title format is
6146 Vicon_title_format if FRAME is iconified, otherwise it is
6147 frame_title_format. */
6148
6149 static void
6150 x_consider_frame_title (frame)
6151 Lisp_Object frame;
6152 {
6153 struct frame *f = XFRAME (frame);
6154
6155 if (FRAME_WINDOW_P (f)
6156 || FRAME_MINIBUF_ONLY_P (f)
6157 || f->explicit_name)
6158 {
6159 /* Do we have more than one visible frame on this X display? */
6160 Lisp_Object tail;
6161 Lisp_Object fmt;
6162 struct buffer *obuf;
6163 int len;
6164 struct it it;
6165
6166 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
6167 {
6168 struct frame *tf = XFRAME (XCAR (tail));
6169
6170 if (tf != f
6171 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
6172 && !FRAME_MINIBUF_ONLY_P (tf)
6173 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
6174 break;
6175 }
6176
6177 /* Set global variable indicating that multiple frames exist. */
6178 multiple_frames = CONSP (tail);
6179
6180 /* Switch to the buffer of selected window of the frame. Set up
6181 frame_title_ptr so that display_mode_element will output into it;
6182 then display the title. */
6183 obuf = current_buffer;
6184 Fset_buffer (XWINDOW (f->selected_window)->buffer);
6185 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
6186 frame_title_ptr = frame_title_buf;
6187 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
6188 NULL, DEFAULT_FACE_ID);
6189 len = display_mode_element (&it, 0, -1, -1, fmt);
6190 frame_title_ptr = NULL;
6191 set_buffer_internal (obuf);
6192
6193 /* Set the title only if it's changed. This avoids consing in
6194 the common case where it hasn't. (If it turns out that we've
6195 already wasted too much time by walking through the list with
6196 display_mode_element, then we might need to optimize at a
6197 higher level than this.) */
6198 if (! STRINGP (f->name)
6199 || STRING_BYTES (XSTRING (f->name)) != len
6200 || bcmp (frame_title_buf, XSTRING (f->name)->data, len) != 0)
6201 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
6202 }
6203 }
6204
6205 #else /* not HAVE_WINDOW_SYSTEM */
6206
6207 #define frame_title_ptr ((char *)0)
6208 #define store_frame_title(str, mincol, maxcol) 0
6209
6210 #endif /* not HAVE_WINDOW_SYSTEM */
6211
6212
6213
6214 \f
6215 /***********************************************************************
6216 Menu Bars
6217 ***********************************************************************/
6218
6219
6220 /* Prepare for redisplay by updating menu-bar item lists when
6221 appropriate. This can call eval. */
6222
6223 void
6224 prepare_menu_bars ()
6225 {
6226 int all_windows;
6227 struct gcpro gcpro1, gcpro2;
6228 struct frame *f;
6229 struct frame *tooltip_frame;
6230
6231 #ifdef HAVE_X_WINDOWS
6232 tooltip_frame = tip_frame;
6233 #else
6234 tooltip_frame = NULL;
6235 #endif
6236
6237 /* Update all frame titles based on their buffer names, etc. We do
6238 this before the menu bars so that the buffer-menu will show the
6239 up-to-date frame titles. */
6240 #ifdef HAVE_WINDOW_SYSTEM
6241 if (windows_or_buffers_changed || update_mode_lines)
6242 {
6243 Lisp_Object tail, frame;
6244
6245 FOR_EACH_FRAME (tail, frame)
6246 {
6247 f = XFRAME (frame);
6248 if (f != tooltip_frame
6249 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
6250 x_consider_frame_title (frame);
6251 }
6252 }
6253 #endif /* HAVE_WINDOW_SYSTEM */
6254
6255 /* Update the menu bar item lists, if appropriate. This has to be
6256 done before any actual redisplay or generation of display lines. */
6257 all_windows = (update_mode_lines
6258 || buffer_shared > 1
6259 || windows_or_buffers_changed);
6260 if (all_windows)
6261 {
6262 Lisp_Object tail, frame;
6263 int count = specpdl_ptr - specpdl;
6264
6265 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6266
6267 FOR_EACH_FRAME (tail, frame)
6268 {
6269 f = XFRAME (frame);
6270
6271 /* Ignore tooltip frame. */
6272 if (f == tooltip_frame)
6273 continue;
6274
6275 /* If a window on this frame changed size, report that to
6276 the user and clear the size-change flag. */
6277 if (FRAME_WINDOW_SIZES_CHANGED (f))
6278 {
6279 Lisp_Object functions;
6280
6281 /* Clear flag first in case we get an error below. */
6282 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
6283 functions = Vwindow_size_change_functions;
6284 GCPRO2 (tail, functions);
6285
6286 while (CONSP (functions))
6287 {
6288 call1 (XCAR (functions), frame);
6289 functions = XCDR (functions);
6290 }
6291 UNGCPRO;
6292 }
6293
6294 GCPRO1 (tail);
6295 update_menu_bar (f, 0);
6296 #ifdef HAVE_WINDOW_SYSTEM
6297 update_tool_bar (f, 0);
6298 #endif
6299 UNGCPRO;
6300 }
6301
6302 unbind_to (count, Qnil);
6303 }
6304 else
6305 {
6306 struct frame *sf = SELECTED_FRAME ();
6307 update_menu_bar (sf, 1);
6308 #ifdef HAVE_WINDOW_SYSTEM
6309 update_tool_bar (sf, 1);
6310 #endif
6311 }
6312
6313 /* Motif needs this. See comment in xmenu.c. Turn it off when
6314 pending_menu_activation is not defined. */
6315 #ifdef USE_X_TOOLKIT
6316 pending_menu_activation = 0;
6317 #endif
6318 }
6319
6320
6321 /* Update the menu bar item list for frame F. This has to be done
6322 before we start to fill in any display lines, because it can call
6323 eval.
6324
6325 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
6326
6327 static void
6328 update_menu_bar (f, save_match_data)
6329 struct frame *f;
6330 int save_match_data;
6331 {
6332 Lisp_Object window;
6333 register struct window *w;
6334
6335 window = FRAME_SELECTED_WINDOW (f);
6336 w = XWINDOW (window);
6337
6338 if (update_mode_lines)
6339 w->update_mode_line = Qt;
6340
6341 if (FRAME_WINDOW_P (f)
6342 ?
6343 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
6344 FRAME_EXTERNAL_MENU_BAR (f)
6345 #else
6346 FRAME_MENU_BAR_LINES (f) > 0
6347 #endif
6348 : FRAME_MENU_BAR_LINES (f) > 0)
6349 {
6350 /* If the user has switched buffers or windows, we need to
6351 recompute to reflect the new bindings. But we'll
6352 recompute when update_mode_lines is set too; that means
6353 that people can use force-mode-line-update to request
6354 that the menu bar be recomputed. The adverse effect on
6355 the rest of the redisplay algorithm is about the same as
6356 windows_or_buffers_changed anyway. */
6357 if (windows_or_buffers_changed
6358 || !NILP (w->update_mode_line)
6359 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
6360 < BUF_MODIFF (XBUFFER (w->buffer)))
6361 != !NILP (w->last_had_star))
6362 || ((!NILP (Vtransient_mark_mode)
6363 && !NILP (XBUFFER (w->buffer)->mark_active))
6364 != !NILP (w->region_showing)))
6365 {
6366 struct buffer *prev = current_buffer;
6367 int count = specpdl_ptr - specpdl;
6368
6369 set_buffer_internal_1 (XBUFFER (w->buffer));
6370 if (save_match_data)
6371 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6372 if (NILP (Voverriding_local_map_menu_flag))
6373 {
6374 specbind (Qoverriding_terminal_local_map, Qnil);
6375 specbind (Qoverriding_local_map, Qnil);
6376 }
6377
6378 /* Run the Lucid hook. */
6379 call1 (Vrun_hooks, Qactivate_menubar_hook);
6380
6381 /* If it has changed current-menubar from previous value,
6382 really recompute the menu-bar from the value. */
6383 if (! NILP (Vlucid_menu_bar_dirty_flag))
6384 call0 (Qrecompute_lucid_menubar);
6385
6386 safe_run_hooks (Qmenu_bar_update_hook);
6387 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
6388
6389 /* Redisplay the menu bar in case we changed it. */
6390 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
6391 if (FRAME_WINDOW_P (f))
6392 set_frame_menubar (f, 0, 0);
6393 else
6394 /* On a terminal screen, the menu bar is an ordinary screen
6395 line, and this makes it get updated. */
6396 w->update_mode_line = Qt;
6397 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
6398 /* In the non-toolkit version, the menu bar is an ordinary screen
6399 line, and this makes it get updated. */
6400 w->update_mode_line = Qt;
6401 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
6402
6403 unbind_to (count, Qnil);
6404 set_buffer_internal_1 (prev);
6405 }
6406 }
6407 }
6408
6409
6410 \f
6411 /***********************************************************************
6412 Tool-bars
6413 ***********************************************************************/
6414
6415 #ifdef HAVE_WINDOW_SYSTEM
6416
6417 /* Update the tool-bar item list for frame F. This has to be done
6418 before we start to fill in any display lines. Called from
6419 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
6420 and restore it here. */
6421
6422 static void
6423 update_tool_bar (f, save_match_data)
6424 struct frame *f;
6425 int save_match_data;
6426 {
6427 if (WINDOWP (f->tool_bar_window)
6428 && XFASTINT (XWINDOW (f->tool_bar_window)->height) > 0)
6429 {
6430 Lisp_Object window;
6431 struct window *w;
6432
6433 window = FRAME_SELECTED_WINDOW (f);
6434 w = XWINDOW (window);
6435
6436 /* If the user has switched buffers or windows, we need to
6437 recompute to reflect the new bindings. But we'll
6438 recompute when update_mode_lines is set too; that means
6439 that people can use force-mode-line-update to request
6440 that the menu bar be recomputed. The adverse effect on
6441 the rest of the redisplay algorithm is about the same as
6442 windows_or_buffers_changed anyway. */
6443 if (windows_or_buffers_changed
6444 || !NILP (w->update_mode_line)
6445 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
6446 < BUF_MODIFF (XBUFFER (w->buffer)))
6447 != !NILP (w->last_had_star))
6448 || ((!NILP (Vtransient_mark_mode)
6449 && !NILP (XBUFFER (w->buffer)->mark_active))
6450 != !NILP (w->region_showing)))
6451 {
6452 struct buffer *prev = current_buffer;
6453 int count = specpdl_ptr - specpdl;
6454
6455 /* Set current_buffer to the buffer of the selected
6456 window of the frame, so that we get the right local
6457 keymaps. */
6458 set_buffer_internal_1 (XBUFFER (w->buffer));
6459
6460 /* Save match data, if we must. */
6461 if (save_match_data)
6462 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6463
6464 /* Make sure that we don't accidentally use bogus keymaps. */
6465 if (NILP (Voverriding_local_map_menu_flag))
6466 {
6467 specbind (Qoverriding_terminal_local_map, Qnil);
6468 specbind (Qoverriding_local_map, Qnil);
6469 }
6470
6471 /* Build desired tool-bar items from keymaps. */
6472 f->desired_tool_bar_items
6473 = tool_bar_items (f->desired_tool_bar_items,
6474 &f->n_desired_tool_bar_items);
6475
6476 /* Redisplay the tool-bar in case we changed it. */
6477 w->update_mode_line = Qt;
6478
6479 unbind_to (count, Qnil);
6480 set_buffer_internal_1 (prev);
6481 }
6482 }
6483 }
6484
6485
6486 /* Set F->desired_tool_bar_string to a Lisp string representing frame
6487 F's desired tool-bar contents. F->desired_tool_bar_items must have
6488 been set up previously by calling prepare_menu_bars. */
6489
6490 static void
6491 build_desired_tool_bar_string (f)
6492 struct frame *f;
6493 {
6494 int i, size, size_needed, string_idx;
6495 struct gcpro gcpro1, gcpro2, gcpro3;
6496 Lisp_Object image, plist, props;
6497
6498 image = plist = props = Qnil;
6499 GCPRO3 (image, plist, props);
6500
6501 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
6502 Otherwise, make a new string. */
6503
6504 /* The size of the string we might be able to reuse. */
6505 size = (STRINGP (f->desired_tool_bar_string)
6506 ? XSTRING (f->desired_tool_bar_string)->size
6507 : 0);
6508
6509 /* Each image in the string we build is preceded by a space,
6510 and there is a space at the end. */
6511 size_needed = f->n_desired_tool_bar_items + 1;
6512
6513 /* Reuse f->desired_tool_bar_string, if possible. */
6514 if (size < size_needed)
6515 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
6516 make_number (' '));
6517 else
6518 {
6519 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
6520 Fremove_text_properties (make_number (0), make_number (size),
6521 props, f->desired_tool_bar_string);
6522 }
6523
6524 /* Put a `display' property on the string for the images to display,
6525 put a `menu_item' property on tool-bar items with a value that
6526 is the index of the item in F's tool-bar item vector. */
6527 for (i = 0, string_idx = 0;
6528 i < f->n_desired_tool_bar_items;
6529 ++i, string_idx += 1)
6530 {
6531 #define PROP(IDX) \
6532 (XVECTOR (f->desired_tool_bar_items) \
6533 ->contents[i * TOOL_BAR_ITEM_NSLOTS + (IDX)])
6534
6535 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
6536 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
6537 int margin, relief;
6538 extern Lisp_Object QCrelief, QCmargin, QCalgorithm, Qimage;
6539 extern Lisp_Object Qlaplace;
6540
6541 /* If image is a vector, choose the image according to the
6542 button state. */
6543 image = PROP (TOOL_BAR_ITEM_IMAGES);
6544 if (VECTORP (image))
6545 {
6546 enum tool_bar_item_image idx;
6547
6548 if (enabled_p)
6549 idx = (selected_p
6550 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
6551 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
6552 else
6553 idx = (selected_p
6554 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
6555 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
6556
6557 xassert (XVECTOR (image)->size >= idx);
6558 image = XVECTOR (image)->contents[idx];
6559 }
6560
6561 /* Ignore invalid image specifications. */
6562 if (!valid_image_p (image))
6563 continue;
6564
6565 /* Display the tool-bar button pressed, or depressed. */
6566 plist = Fcopy_sequence (XCDR (image));
6567
6568 /* Compute margin and relief to draw. */
6569 relief = tool_bar_button_relief > 0 ? tool_bar_button_relief : 3;
6570 margin = relief + max (0, tool_bar_button_margin);
6571
6572 if (auto_raise_tool_bar_buttons_p)
6573 {
6574 /* Add a `:relief' property to the image spec if the item is
6575 selected. */
6576 if (selected_p)
6577 {
6578 plist = Fplist_put (plist, QCrelief, make_number (-relief));
6579 margin -= relief;
6580 }
6581 }
6582 else
6583 {
6584 /* If image is selected, display it pressed, i.e. with a
6585 negative relief. If it's not selected, display it with a
6586 raised relief. */
6587 plist = Fplist_put (plist, QCrelief,
6588 (selected_p
6589 ? make_number (-relief)
6590 : make_number (relief)));
6591 margin -= relief;
6592 }
6593
6594 /* Put a margin around the image. */
6595 if (margin)
6596 plist = Fplist_put (plist, QCmargin, make_number (margin));
6597
6598 /* If button is not enabled, make the image appear disabled by
6599 applying an appropriate algorithm to it. */
6600 if (!enabled_p)
6601 plist = Fplist_put (plist, QCalgorithm, Qlaplace);
6602
6603 /* Put a `display' text property on the string for the image to
6604 display. Put a `menu-item' property on the string that gives
6605 the start of this item's properties in the tool-bar items
6606 vector. */
6607 image = Fcons (Qimage, plist);
6608 props = list4 (Qdisplay, image,
6609 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS)),
6610 Fadd_text_properties (make_number (string_idx),
6611 make_number (string_idx + 1),
6612 props, f->desired_tool_bar_string);
6613 #undef PROP
6614 }
6615
6616 UNGCPRO;
6617 }
6618
6619
6620 /* Display one line of the tool-bar of frame IT->f. */
6621
6622 static void
6623 display_tool_bar_line (it)
6624 struct it *it;
6625 {
6626 struct glyph_row *row = it->glyph_row;
6627 int max_x = it->last_visible_x;
6628 struct glyph *last;
6629
6630 prepare_desired_row (row);
6631 row->y = it->current_y;
6632
6633 while (it->current_x < max_x)
6634 {
6635 int x_before, x, n_glyphs_before, i, nglyphs;
6636
6637 /* Get the next display element. */
6638 if (!get_next_display_element (it))
6639 break;
6640
6641 /* Produce glyphs. */
6642 x_before = it->current_x;
6643 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
6644 PRODUCE_GLYPHS (it);
6645
6646 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
6647 i = 0;
6648 x = x_before;
6649 while (i < nglyphs)
6650 {
6651 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
6652
6653 if (x + glyph->pixel_width > max_x)
6654 {
6655 /* Glyph doesn't fit on line. */
6656 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
6657 it->current_x = x;
6658 goto out;
6659 }
6660
6661 ++it->hpos;
6662 x += glyph->pixel_width;
6663 ++i;
6664 }
6665
6666 /* Stop at line ends. */
6667 if (ITERATOR_AT_END_OF_LINE_P (it))
6668 break;
6669
6670 set_iterator_to_next (it);
6671 }
6672
6673 out:;
6674
6675 row->displays_text_p = row->used[TEXT_AREA] != 0;
6676 extend_face_to_end_of_line (it);
6677 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
6678 last->right_box_line_p = 1;
6679 compute_line_metrics (it);
6680
6681 /* If line is empty, make it occupy the rest of the tool-bar. */
6682 if (!row->displays_text_p)
6683 {
6684 row->height = row->phys_height = it->last_visible_y - row->y;
6685 row->ascent = row->phys_ascent = 0;
6686 }
6687
6688 row->full_width_p = 1;
6689 row->continued_p = 0;
6690 row->truncated_on_left_p = 0;
6691 row->truncated_on_right_p = 0;
6692
6693 it->current_x = it->hpos = 0;
6694 it->current_y += row->height;
6695 ++it->vpos;
6696 ++it->glyph_row;
6697 }
6698
6699
6700 /* Value is the number of screen lines needed to make all tool-bar
6701 items of frame F visible. */
6702
6703 static int
6704 tool_bar_lines_needed (f)
6705 struct frame *f;
6706 {
6707 struct window *w = XWINDOW (f->tool_bar_window);
6708 struct it it;
6709
6710 /* Initialize an iterator for iteration over
6711 F->desired_tool_bar_string in the tool-bar window of frame F. */
6712 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
6713 it.first_visible_x = 0;
6714 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
6715 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
6716
6717 while (!ITERATOR_AT_END_P (&it))
6718 {
6719 it.glyph_row = w->desired_matrix->rows;
6720 clear_glyph_row (it.glyph_row);
6721 display_tool_bar_line (&it);
6722 }
6723
6724 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
6725 }
6726
6727
6728 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
6729 height should be changed. */
6730
6731 static int
6732 redisplay_tool_bar (f)
6733 struct frame *f;
6734 {
6735 struct window *w;
6736 struct it it;
6737 struct glyph_row *row;
6738 int change_height_p = 0;
6739
6740 /* If frame hasn't a tool-bar window or if it is zero-height, don't
6741 do anything. This means you must start with tool-bar-lines
6742 non-zero to get the auto-sizing effect. Or in other words, you
6743 can turn off tool-bars by specifying tool-bar-lines zero. */
6744 if (!WINDOWP (f->tool_bar_window)
6745 || (w = XWINDOW (f->tool_bar_window),
6746 XFASTINT (w->height) == 0))
6747 return 0;
6748
6749 /* Set up an iterator for the tool-bar window. */
6750 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
6751 it.first_visible_x = 0;
6752 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
6753 row = it.glyph_row;
6754
6755 /* Build a string that represents the contents of the tool-bar. */
6756 build_desired_tool_bar_string (f);
6757 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
6758
6759 /* Display as many lines as needed to display all tool-bar items. */
6760 while (it.current_y < it.last_visible_y)
6761 display_tool_bar_line (&it);
6762
6763 /* It doesn't make much sense to try scrolling in the tool-bar
6764 window, so don't do it. */
6765 w->desired_matrix->no_scrolling_p = 1;
6766 w->must_be_updated_p = 1;
6767
6768 if (auto_resize_tool_bars_p)
6769 {
6770 int nlines;
6771
6772 /* If there are blank lines at the end, except for a partially
6773 visible blank line at the end that is smaller than
6774 CANON_Y_UNIT, change the tool-bar's height. */
6775 row = it.glyph_row - 1;
6776 if (!row->displays_text_p
6777 && row->height >= CANON_Y_UNIT (f))
6778 change_height_p = 1;
6779
6780 /* If row displays tool-bar items, but is partially visible,
6781 change the tool-bar's height. */
6782 if (row->displays_text_p
6783 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
6784 change_height_p = 1;
6785
6786 /* Resize windows as needed by changing the `tool-bar-lines'
6787 frame parameter. */
6788 if (change_height_p
6789 && (nlines = tool_bar_lines_needed (f),
6790 nlines != XFASTINT (w->height)))
6791 {
6792 extern Lisp_Object Qtool_bar_lines;
6793 Lisp_Object frame;
6794
6795 XSETFRAME (frame, f);
6796 clear_glyph_matrix (w->desired_matrix);
6797 Fmodify_frame_parameters (frame,
6798 Fcons (Fcons (Qtool_bar_lines,
6799 make_number (nlines)),
6800 Qnil));
6801 fonts_changed_p = 1;
6802 }
6803 }
6804
6805 return change_height_p;
6806 }
6807
6808
6809 /* Get information about the tool-bar item which is displayed in GLYPH
6810 on frame F. Return in *PROP_IDX the index where tool-bar item
6811 properties start in F->current_tool_bar_items. Value is zero if
6812 GLYPH doesn't display a tool-bar item. */
6813
6814 int
6815 tool_bar_item_info (f, glyph, prop_idx)
6816 struct frame *f;
6817 struct glyph *glyph;
6818 int *prop_idx;
6819 {
6820 Lisp_Object prop;
6821 int success_p;
6822
6823 /* Get the text property `menu-item' at pos. The value of that
6824 property is the start index of this item's properties in
6825 F->current_tool_bar_items. */
6826 prop = Fget_text_property (make_number (glyph->charpos),
6827 Qmenu_item, f->current_tool_bar_string);
6828 if (INTEGERP (prop))
6829 {
6830 *prop_idx = XINT (prop);
6831 success_p = 1;
6832 }
6833 else
6834 success_p = 0;
6835
6836 return success_p;
6837 }
6838
6839 #endif /* HAVE_WINDOW_SYSTEM */
6840
6841
6842 \f
6843 /************************************************************************
6844 Horizontal scrolling
6845 ************************************************************************/
6846
6847 static int hscroll_window_tree P_ ((Lisp_Object));
6848 static int hscroll_windows P_ ((Lisp_Object));
6849
6850 /* For all leaf windows in the window tree rooted at WINDOW, set their
6851 hscroll value so that PT is (i) visible in the window, and (ii) so
6852 that it is not within a certain margin at the window's left and
6853 right border. Value is non-zero if any window's hscroll has been
6854 changed. */
6855
6856 static int
6857 hscroll_window_tree (window)
6858 Lisp_Object window;
6859 {
6860 int hscrolled_p = 0;
6861
6862 while (WINDOWP (window))
6863 {
6864 struct window *w = XWINDOW (window);
6865
6866 if (WINDOWP (w->hchild))
6867 hscrolled_p |= hscroll_window_tree (w->hchild);
6868 else if (WINDOWP (w->vchild))
6869 hscrolled_p |= hscroll_window_tree (w->vchild);
6870 else if (w->cursor.vpos >= 0)
6871 {
6872 int hscroll_margin, text_area_x, text_area_y;
6873 int text_area_width, text_area_height;
6874 struct glyph_row *current_cursor_row
6875 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
6876 struct glyph_row *desired_cursor_row
6877 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
6878 struct glyph_row *cursor_row
6879 = (desired_cursor_row->enabled_p
6880 ? desired_cursor_row
6881 : current_cursor_row);
6882
6883 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
6884 &text_area_width, &text_area_height);
6885
6886 /* Scroll when cursor is inside this scroll margin. */
6887 hscroll_margin = 5 * CANON_X_UNIT (XFRAME (w->frame));
6888
6889 if ((XFASTINT (w->hscroll)
6890 && w->cursor.x < hscroll_margin)
6891 || (cursor_row->enabled_p
6892 && cursor_row->truncated_on_right_p
6893 && (w->cursor.x > text_area_width - hscroll_margin)))
6894 {
6895 struct it it;
6896 int hscroll;
6897 struct buffer *saved_current_buffer;
6898 int pt;
6899
6900 /* Find point in a display of infinite width. */
6901 saved_current_buffer = current_buffer;
6902 current_buffer = XBUFFER (w->buffer);
6903
6904 if (w == XWINDOW (selected_window))
6905 pt = BUF_PT (current_buffer);
6906 else
6907 {
6908 pt = marker_position (w->pointm);
6909 pt = max (BEGV, pt);
6910 pt = min (ZV, pt);
6911 }
6912
6913 /* Move iterator to pt starting at cursor_row->start in
6914 a line with infinite width. */
6915 init_to_row_start (&it, w, cursor_row);
6916 it.last_visible_x = INFINITY;
6917 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
6918 current_buffer = saved_current_buffer;
6919
6920 /* Center cursor in window. */
6921 hscroll = (max (0, it.current_x - text_area_width / 2)
6922 / CANON_X_UNIT (it.f));
6923
6924 /* Don't call Fset_window_hscroll if value hasn't
6925 changed because it will prevent redisplay
6926 optimizations. */
6927 if (XFASTINT (w->hscroll) != hscroll)
6928 {
6929 Fset_window_hscroll (window, make_number (hscroll));
6930 hscrolled_p = 1;
6931 }
6932 }
6933 }
6934
6935 window = w->next;
6936 }
6937
6938 /* Value is non-zero if hscroll of any leaf window has been changed. */
6939 return hscrolled_p;
6940 }
6941
6942
6943 /* Set hscroll so that cursor is visible and not inside horizontal
6944 scroll margins for all windows in the tree rooted at WINDOW. See
6945 also hscroll_window_tree above. Value is non-zero if any window's
6946 hscroll has been changed. If it has, desired matrices on the frame
6947 of WINDOW are cleared. */
6948
6949 static int
6950 hscroll_windows (window)
6951 Lisp_Object window;
6952 {
6953 int hscrolled_p;
6954
6955 if (automatic_hscrolling_p)
6956 {
6957 hscrolled_p = hscroll_window_tree (window);
6958 if (hscrolled_p)
6959 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
6960 }
6961 else
6962 hscrolled_p = 0;
6963 return hscrolled_p;
6964 }
6965
6966
6967 \f
6968 /************************************************************************
6969 Redisplay
6970 ************************************************************************/
6971
6972 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
6973 to a non-zero value. This is sometimes handy to have in a debugger
6974 session. */
6975
6976 #if GLYPH_DEBUG
6977
6978 /* First and last unchanged row for try_window_id. */
6979
6980 int debug_first_unchanged_at_end_vpos;
6981 int debug_last_unchanged_at_beg_vpos;
6982
6983 /* Delta vpos and y. */
6984
6985 int debug_dvpos, debug_dy;
6986
6987 /* Delta in characters and bytes for try_window_id. */
6988
6989 int debug_delta, debug_delta_bytes;
6990
6991 /* Values of window_end_pos and window_end_vpos at the end of
6992 try_window_id. */
6993
6994 int debug_end_pos, debug_end_vpos;
6995
6996 /* Append a string to W->desired_matrix->method. FMT is a printf
6997 format string. A1...A9 are a supplement for a variable-length
6998 argument list. If trace_redisplay_p is non-zero also printf the
6999 resulting string to stderr. */
7000
7001 static void
7002 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
7003 struct window *w;
7004 char *fmt;
7005 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
7006 {
7007 char buffer[512];
7008 char *method = w->desired_matrix->method;
7009 int len = strlen (method);
7010 int size = sizeof w->desired_matrix->method;
7011 int remaining = size - len - 1;
7012
7013 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
7014 if (len && remaining)
7015 {
7016 method[len] = '|';
7017 --remaining, ++len;
7018 }
7019
7020 strncpy (method + len, buffer, remaining);
7021
7022 if (trace_redisplay_p)
7023 fprintf (stderr, "%p (%s): %s\n",
7024 w,
7025 ((BUFFERP (w->buffer)
7026 && STRINGP (XBUFFER (w->buffer)->name))
7027 ? (char *) XSTRING (XBUFFER (w->buffer)->name)->data
7028 : "no buffer"),
7029 buffer);
7030 }
7031
7032 #endif /* GLYPH_DEBUG */
7033
7034
7035 /* This counter is used to clear the face cache every once in a while
7036 in redisplay_internal. It is incremented for each redisplay.
7037 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
7038 cleared. */
7039
7040 #define CLEAR_FACE_CACHE_COUNT 10000
7041 static int clear_face_cache_count;
7042
7043 /* Record the previous terminal frame we displayed. */
7044
7045 static struct frame *previous_terminal_frame;
7046
7047 /* Non-zero while redisplay_internal is in progress. */
7048
7049 int redisplaying_p;
7050
7051
7052 /* Value is non-zero if all changes in window W, which displays
7053 current_buffer, are in the text between START and END. START is a
7054 buffer position, END is given as a distance from Z. Used in
7055 redisplay_internal for display optimization. */
7056
7057 static INLINE int
7058 text_outside_line_unchanged_p (w, start, end)
7059 struct window *w;
7060 int start, end;
7061 {
7062 int unchanged_p = 1;
7063
7064 /* If text or overlays have changed, see where. */
7065 if (XFASTINT (w->last_modified) < MODIFF
7066 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
7067 {
7068 /* Gap in the line? */
7069 if (GPT < start || Z - GPT < end)
7070 unchanged_p = 0;
7071
7072 /* Changes start in front of the line, or end after it? */
7073 if (unchanged_p
7074 && (BEG_UNCHANGED < start - 1
7075 || END_UNCHANGED < end))
7076 unchanged_p = 0;
7077
7078 /* If selective display, can't optimize if changes start at the
7079 beginning of the line. */
7080 if (unchanged_p
7081 && INTEGERP (current_buffer->selective_display)
7082 && XINT (current_buffer->selective_display) > 0
7083 && (BEG_UNCHANGED < start || GPT <= start))
7084 unchanged_p = 0;
7085 }
7086
7087 return unchanged_p;
7088 }
7089
7090
7091 /* Do a frame update, taking possible shortcuts into account. This is
7092 the main external entry point for redisplay.
7093
7094 If the last redisplay displayed an echo area message and that message
7095 is no longer requested, we clear the echo area or bring back the
7096 mini-buffer if that is in use. */
7097
7098 void
7099 redisplay ()
7100 {
7101 redisplay_internal (0);
7102 }
7103
7104 /* Return 1 if point moved out of or into a composition. Otherwise
7105 return 0. PREV_BUF and PREV_PT are the last point buffer and
7106 position. BUF and PT are the current point buffer and position. */
7107
7108 int
7109 check_point_in_composition (prev_buf, prev_pt, buf, pt)
7110 struct buffer *prev_buf, *buf;
7111 int prev_pt, pt;
7112 {
7113 int start, end;
7114 Lisp_Object prop;
7115 Lisp_Object buffer;
7116
7117 XSETBUFFER (buffer, buf);
7118 /* Check a composition at the last point if point moved within the
7119 same buffer. */
7120 if (prev_buf == buf)
7121 {
7122 if (prev_pt == pt)
7123 /* Point didn't move. */
7124 return 0;
7125
7126 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
7127 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
7128 && COMPOSITION_VALID_P (start, end, prop)
7129 && start < prev_pt && end > prev_pt)
7130 /* The last point was within the composition. Return 1 iff
7131 point moved out of the composition. */
7132 return (pt <= start || pt >= end);
7133 }
7134
7135 /* Check a composition at the current point. */
7136 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
7137 && find_composition (pt, -1, &start, &end, &prop, buffer)
7138 && COMPOSITION_VALID_P (start, end, prop)
7139 && start < pt && end > pt);
7140 }
7141
7142 /* Reconsider the setting of B->clip_changed which is displayed
7143 in window W. */
7144
7145 static INLINE void
7146 reconsider_clip_changes (w, b)
7147 struct window *w;
7148 struct buffer *b;
7149 {
7150 if (b->prevent_redisplay_optimizations_p)
7151 b->clip_changed = 1;
7152 else if (b->clip_changed
7153 && !NILP (w->window_end_valid)
7154 && w->current_matrix->buffer == b
7155 && w->current_matrix->zv == BUF_ZV (b)
7156 && w->current_matrix->begv == BUF_BEGV (b))
7157 b->clip_changed = 0;
7158
7159 /* If display wasn't paused, and W is not a tool bar window, see if
7160 point has been moved into or out of a composition. In that case,
7161 we set b->clip_changed to 1 to force updating the screen. If
7162 b->clip_changed has already been set to 1, we can skip this
7163 check. */
7164 if (!b->clip_changed
7165 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
7166 {
7167 int pt;
7168
7169 if (w == XWINDOW (selected_window))
7170 pt = BUF_PT (current_buffer);
7171 else
7172 pt = marker_position (w->pointm);
7173
7174 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
7175 || pt != XINT (w->last_point))
7176 && check_point_in_composition (w->current_matrix->buffer,
7177 XINT (w->last_point),
7178 XBUFFER (w->buffer), pt))
7179 b->clip_changed = 1;
7180 }
7181 }
7182
7183
7184 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
7185 response to any user action; therefore, we should preserve the echo
7186 area. (Actually, our caller does that job.) Perhaps in the future
7187 avoid recentering windows if it is not necessary; currently that
7188 causes some problems. */
7189
7190 static void
7191 redisplay_internal (preserve_echo_area)
7192 int preserve_echo_area;
7193 {
7194 struct window *w = XWINDOW (selected_window);
7195 struct frame *f = XFRAME (w->frame);
7196 int pause;
7197 int must_finish = 0;
7198 struct text_pos tlbufpos, tlendpos;
7199 int number_of_visible_frames;
7200 int count;
7201 struct frame *sf = SELECTED_FRAME ();
7202
7203 /* Non-zero means redisplay has to consider all windows on all
7204 frames. Zero means, only selected_window is considered. */
7205 int consider_all_windows_p;
7206
7207 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
7208
7209 /* No redisplay if running in batch mode or frame is not yet fully
7210 initialized, or redisplay is explicitly turned off by setting
7211 Vinhibit_redisplay. */
7212 if (noninteractive
7213 || !NILP (Vinhibit_redisplay)
7214 || !f->glyphs_initialized_p)
7215 return;
7216
7217 /* The flag redisplay_performed_directly_p is set by
7218 direct_output_for_insert when it already did the whole screen
7219 update necessary. */
7220 if (redisplay_performed_directly_p)
7221 {
7222 redisplay_performed_directly_p = 0;
7223 if (!hscroll_windows (selected_window))
7224 return;
7225 }
7226
7227 #ifdef USE_X_TOOLKIT
7228 if (popup_activated ())
7229 return;
7230 #endif
7231
7232 /* I don't think this happens but let's be paranoid. */
7233 if (redisplaying_p)
7234 return;
7235
7236 /* Record a function that resets redisplaying_p to its old value
7237 when we leave this function. */
7238 count = specpdl_ptr - specpdl;
7239 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
7240 ++redisplaying_p;
7241
7242 retry:
7243
7244 reconsider_clip_changes (w, current_buffer);
7245
7246 /* If new fonts have been loaded that make a glyph matrix adjustment
7247 necessary, do it. */
7248 if (fonts_changed_p)
7249 {
7250 adjust_glyphs (NULL);
7251 ++windows_or_buffers_changed;
7252 fonts_changed_p = 0;
7253 }
7254
7255 if (! FRAME_WINDOW_P (sf)
7256 && previous_terminal_frame != sf)
7257 {
7258 /* Since frames on an ASCII terminal share the same display
7259 area, displaying a different frame means redisplay the whole
7260 thing. */
7261 windows_or_buffers_changed++;
7262 SET_FRAME_GARBAGED (sf);
7263 XSETFRAME (Vterminal_frame, sf);
7264 }
7265 previous_terminal_frame = sf;
7266
7267 /* Set the visible flags for all frames. Do this before checking
7268 for resized or garbaged frames; they want to know if their frames
7269 are visible. See the comment in frame.h for
7270 FRAME_SAMPLE_VISIBILITY. */
7271 {
7272 Lisp_Object tail, frame;
7273
7274 number_of_visible_frames = 0;
7275
7276 FOR_EACH_FRAME (tail, frame)
7277 {
7278 struct frame *f = XFRAME (frame);
7279
7280 FRAME_SAMPLE_VISIBILITY (f);
7281 if (FRAME_VISIBLE_P (f))
7282 ++number_of_visible_frames;
7283 clear_desired_matrices (f);
7284 }
7285 }
7286
7287 /* Notice any pending interrupt request to change frame size. */
7288 do_pending_window_change (1);
7289
7290 /* Clear frames marked as garbaged. */
7291 if (frame_garbaged)
7292 clear_garbaged_frames ();
7293
7294 /* Build menubar and tool-bar items. */
7295 prepare_menu_bars ();
7296
7297 if (windows_or_buffers_changed)
7298 update_mode_lines++;
7299
7300 /* Detect case that we need to write or remove a star in the mode line. */
7301 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
7302 {
7303 w->update_mode_line = Qt;
7304 if (buffer_shared > 1)
7305 update_mode_lines++;
7306 }
7307
7308 /* If %c is in the mode line, update it if needed. */
7309 if (!NILP (w->column_number_displayed)
7310 /* This alternative quickly identifies a common case
7311 where no change is needed. */
7312 && !(PT == XFASTINT (w->last_point)
7313 && XFASTINT (w->last_modified) >= MODIFF
7314 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
7315 && XFASTINT (w->column_number_displayed) != current_column ())
7316 w->update_mode_line = Qt;
7317
7318 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
7319
7320 /* The variable buffer_shared is set in redisplay_window and
7321 indicates that we redisplay a buffer in different windows. See
7322 there. */
7323 consider_all_windows_p = update_mode_lines || buffer_shared > 1;
7324
7325 /* If specs for an arrow have changed, do thorough redisplay
7326 to ensure we remove any arrow that should no longer exist. */
7327 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
7328 || ! EQ (Voverlay_arrow_string, last_arrow_string))
7329 consider_all_windows_p = windows_or_buffers_changed = 1;
7330
7331 /* Normally the message* functions will have already displayed and
7332 updated the echo area, but the frame may have been trashed, or
7333 the update may have been preempted, so display the echo area
7334 again here. Checking both message buffers captures the case that
7335 the echo area should be cleared. */
7336 if (!NILP (echo_area_buffer[0]) || !NILP (echo_area_buffer[1]))
7337 {
7338 int window_height_changed_p = echo_area_display (0);
7339 must_finish = 1;
7340
7341 if (fonts_changed_p)
7342 goto retry;
7343 else if (window_height_changed_p)
7344 {
7345 consider_all_windows_p = 1;
7346 ++update_mode_lines;
7347 ++windows_or_buffers_changed;
7348
7349 /* If window configuration was changed, frames may have been
7350 marked garbaged. Clear them or we will experience
7351 surprises wrt scrolling. */
7352 if (frame_garbaged)
7353 clear_garbaged_frames ();
7354 }
7355 }
7356 else if (w == XWINDOW (minibuf_window)
7357 && (current_buffer->clip_changed
7358 || XFASTINT (w->last_modified) < MODIFF
7359 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
7360 && resize_mini_window (w, 0))
7361 {
7362 /* Resized active mini-window to fit the size of what it is
7363 showing if its contents might have changed. */
7364 must_finish = 1;
7365 consider_all_windows_p = 1;
7366 ++windows_or_buffers_changed;
7367 ++update_mode_lines;
7368
7369 /* If window configuration was changed, frames may have been
7370 marked garbaged. Clear them or we will experience
7371 surprises wrt scrolling. */
7372 if (frame_garbaged)
7373 clear_garbaged_frames ();
7374 }
7375
7376
7377 /* If showing the region, and mark has changed, we must redisplay
7378 the whole window. The assignment to this_line_start_pos prevents
7379 the optimization directly below this if-statement. */
7380 if (((!NILP (Vtransient_mark_mode)
7381 && !NILP (XBUFFER (w->buffer)->mark_active))
7382 != !NILP (w->region_showing))
7383 || (!NILP (w->region_showing)
7384 && !EQ (w->region_showing,
7385 Fmarker_position (XBUFFER (w->buffer)->mark))))
7386 CHARPOS (this_line_start_pos) = 0;
7387
7388 /* Optimize the case that only the line containing the cursor in the
7389 selected window has changed. Variables starting with this_ are
7390 set in display_line and record information about the line
7391 containing the cursor. */
7392 tlbufpos = this_line_start_pos;
7393 tlendpos = this_line_end_pos;
7394 if (!consider_all_windows_p
7395 && CHARPOS (tlbufpos) > 0
7396 && NILP (w->update_mode_line)
7397 && !current_buffer->clip_changed
7398 && FRAME_VISIBLE_P (XFRAME (w->frame))
7399 && !FRAME_OBSCURED_P (XFRAME (w->frame))
7400 /* Make sure recorded data applies to current buffer, etc. */
7401 && this_line_buffer == current_buffer
7402 && current_buffer == XBUFFER (w->buffer)
7403 && NILP (w->force_start)
7404 /* Point must be on the line that we have info recorded about. */
7405 && PT >= CHARPOS (tlbufpos)
7406 && PT <= Z - CHARPOS (tlendpos)
7407 /* All text outside that line, including its final newline,
7408 must be unchanged */
7409 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
7410 CHARPOS (tlendpos)))
7411 {
7412 if (CHARPOS (tlbufpos) > BEGV
7413 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
7414 && (CHARPOS (tlbufpos) == ZV
7415 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
7416 /* Former continuation line has disappeared by becoming empty */
7417 goto cancel;
7418 else if (XFASTINT (w->last_modified) < MODIFF
7419 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
7420 || MINI_WINDOW_P (w))
7421 {
7422 /* We have to handle the case of continuation around a
7423 wide-column character (See the comment in indent.c around
7424 line 885).
7425
7426 For instance, in the following case:
7427
7428 -------- Insert --------
7429 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
7430 J_I_ ==> J_I_ `^^' are cursors.
7431 ^^ ^^
7432 -------- --------
7433
7434 As we have to redraw the line above, we should goto cancel. */
7435
7436 struct it it;
7437 int line_height_before = this_line_pixel_height;
7438
7439 /* Note that start_display will handle the case that the
7440 line starting at tlbufpos is a continuation lines. */
7441 start_display (&it, w, tlbufpos);
7442
7443 /* Implementation note: It this still necessary? */
7444 if (it.current_x != this_line_start_x)
7445 goto cancel;
7446
7447 TRACE ((stderr, "trying display optimization 1\n"));
7448 w->cursor.vpos = -1;
7449 overlay_arrow_seen = 0;
7450 it.vpos = this_line_vpos;
7451 it.current_y = this_line_y;
7452 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
7453 display_line (&it);
7454
7455 /* If line contains point, is not continued,
7456 and ends at same distance from eob as before, we win */
7457 if (w->cursor.vpos >= 0
7458 /* Line is not continued, otherwise this_line_start_pos
7459 would have been set to 0 in display_line. */
7460 && CHARPOS (this_line_start_pos)
7461 /* Line ends as before. */
7462 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
7463 /* Line has same height as before. Otherwise other lines
7464 would have to be shifted up or down. */
7465 && this_line_pixel_height == line_height_before)
7466 {
7467 /* If this is not the window's last line, we must adjust
7468 the charstarts of the lines below. */
7469 if (it.current_y < it.last_visible_y)
7470 {
7471 struct glyph_row *row
7472 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
7473 int delta, delta_bytes;
7474
7475 if (Z - CHARPOS (tlendpos) == ZV)
7476 {
7477 /* This line ends at end of (accessible part of)
7478 buffer. There is no newline to count. */
7479 delta = (Z
7480 - CHARPOS (tlendpos)
7481 - MATRIX_ROW_START_CHARPOS (row));
7482 delta_bytes = (Z_BYTE
7483 - BYTEPOS (tlendpos)
7484 - MATRIX_ROW_START_BYTEPOS (row));
7485 }
7486 else
7487 {
7488 /* This line ends in a newline. Must take
7489 account of the newline and the rest of the
7490 text that follows. */
7491 delta = (Z
7492 - CHARPOS (tlendpos)
7493 - MATRIX_ROW_START_CHARPOS (row));
7494 delta_bytes = (Z_BYTE
7495 - BYTEPOS (tlendpos)
7496 - MATRIX_ROW_START_BYTEPOS (row));
7497 }
7498
7499 increment_matrix_positions (w->current_matrix,
7500 this_line_vpos + 1,
7501 w->current_matrix->nrows,
7502 delta, delta_bytes);
7503 }
7504
7505 /* If this row displays text now but previously didn't,
7506 or vice versa, w->window_end_vpos may have to be
7507 adjusted. */
7508 if ((it.glyph_row - 1)->displays_text_p)
7509 {
7510 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
7511 XSETINT (w->window_end_vpos, this_line_vpos);
7512 }
7513 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
7514 && this_line_vpos > 0)
7515 XSETINT (w->window_end_vpos, this_line_vpos - 1);
7516 w->window_end_valid = Qnil;
7517
7518 /* Update hint: No need to try to scroll in update_window. */
7519 w->desired_matrix->no_scrolling_p = 1;
7520
7521 #if GLYPH_DEBUG
7522 *w->desired_matrix->method = 0;
7523 debug_method_add (w, "optimization 1");
7524 #endif
7525 goto update;
7526 }
7527 else
7528 goto cancel;
7529 }
7530 else if (/* Cursor position hasn't changed. */
7531 PT == XFASTINT (w->last_point)
7532 /* Make sure the cursor was last displayed
7533 in this window. Otherwise we have to reposition it. */
7534 && 0 <= w->cursor.vpos
7535 && XINT (w->height) > w->cursor.vpos)
7536 {
7537 if (!must_finish)
7538 {
7539 do_pending_window_change (1);
7540
7541 /* We used to always goto end_of_redisplay here, but this
7542 isn't enough if we have a blinking cursor. */
7543 if (w->cursor_off_p == w->last_cursor_off_p)
7544 goto end_of_redisplay;
7545 }
7546 goto update;
7547 }
7548 /* If highlighting the region, or if the cursor is in the echo area,
7549 then we can't just move the cursor. */
7550 else if (! (!NILP (Vtransient_mark_mode)
7551 && !NILP (current_buffer->mark_active))
7552 && (w == XWINDOW (current_buffer->last_selected_window)
7553 || highlight_nonselected_windows)
7554 && NILP (w->region_showing)
7555 && NILP (Vshow_trailing_whitespace)
7556 && !cursor_in_echo_area)
7557 {
7558 struct it it;
7559 struct glyph_row *row;
7560
7561 /* Skip from tlbufpos to PT and see where it is. Note that
7562 PT may be in invisible text. If so, we will end at the
7563 next visible position. */
7564 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
7565 NULL, DEFAULT_FACE_ID);
7566 it.current_x = this_line_start_x;
7567 it.current_y = this_line_y;
7568 it.vpos = this_line_vpos;
7569
7570 /* The call to move_it_to stops in front of PT, but
7571 moves over before-strings. */
7572 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
7573
7574 if (it.vpos == this_line_vpos
7575 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
7576 row->enabled_p))
7577 {
7578 xassert (this_line_vpos == it.vpos);
7579 xassert (this_line_y == it.current_y);
7580 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
7581 goto update;
7582 }
7583 else
7584 goto cancel;
7585 }
7586
7587 cancel:
7588 /* Text changed drastically or point moved off of line. */
7589 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
7590 }
7591
7592 CHARPOS (this_line_start_pos) = 0;
7593 consider_all_windows_p |= buffer_shared > 1;
7594 ++clear_face_cache_count;
7595
7596
7597 /* Build desired matrices. If consider_all_windows_p is non-zero,
7598 do it for all windows on all frames. Otherwise do it for
7599 selected_window, only. */
7600
7601 if (consider_all_windows_p)
7602 {
7603 Lisp_Object tail, frame;
7604
7605 /* Clear the face cache eventually. */
7606 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
7607 {
7608 clear_face_cache (0);
7609 clear_face_cache_count = 0;
7610 }
7611
7612 /* Recompute # windows showing selected buffer. This will be
7613 incremented each time such a window is displayed. */
7614 buffer_shared = 0;
7615
7616 FOR_EACH_FRAME (tail, frame)
7617 {
7618 struct frame *f = XFRAME (frame);
7619 if (FRAME_WINDOW_P (f) || f == sf)
7620 {
7621 /* Mark all the scroll bars to be removed; we'll redeem
7622 the ones we want when we redisplay their windows. */
7623 if (condemn_scroll_bars_hook)
7624 (*condemn_scroll_bars_hook) (f);
7625
7626 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
7627 redisplay_windows (FRAME_ROOT_WINDOW (f));
7628
7629 /* Any scroll bars which redisplay_windows should have
7630 nuked should now go away. */
7631 if (judge_scroll_bars_hook)
7632 (*judge_scroll_bars_hook) (f);
7633 }
7634 }
7635 }
7636 else if (FRAME_VISIBLE_P (sf)
7637 && !FRAME_OBSCURED_P (sf))
7638 redisplay_window (selected_window, 1);
7639
7640
7641 /* Compare desired and current matrices, perform output. */
7642
7643 update:
7644
7645 /* If fonts changed, display again. */
7646 if (fonts_changed_p)
7647 goto retry;
7648
7649 /* Prevent various kinds of signals during display update.
7650 stdio is not robust about handling signals,
7651 which can cause an apparent I/O error. */
7652 if (interrupt_input)
7653 unrequest_sigio ();
7654 stop_polling ();
7655
7656 if (consider_all_windows_p)
7657 {
7658 Lisp_Object tail;
7659 struct frame *f;
7660 int hscrolled_p;
7661
7662 pause = 0;
7663 hscrolled_p = 0;
7664
7665 /* See if we have to hscroll. */
7666 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
7667 if (FRAMEP (XCAR (tail)))
7668 {
7669 f = XFRAME (XCAR (tail));
7670
7671 if ((FRAME_WINDOW_P (f)
7672 || f == sf)
7673 && FRAME_VISIBLE_P (f)
7674 && !FRAME_OBSCURED_P (f)
7675 && hscroll_windows (f->root_window))
7676 hscrolled_p = 1;
7677 }
7678
7679 if (hscrolled_p)
7680 goto retry;
7681
7682 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
7683 {
7684 if (!FRAMEP (XCAR (tail)))
7685 continue;
7686
7687 f = XFRAME (XCAR (tail));
7688
7689 if ((FRAME_WINDOW_P (f) || f == sf)
7690 && FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
7691 {
7692 /* Mark all windows as to be updated. */
7693 set_window_update_flags (XWINDOW (f->root_window), 1);
7694 pause |= update_frame (f, 0, 0);
7695 if (!pause)
7696 {
7697 mark_window_display_accurate (f->root_window, 1);
7698 if (frame_up_to_date_hook != 0)
7699 (*frame_up_to_date_hook) (f);
7700 }
7701 }
7702 }
7703 }
7704 else
7705 {
7706 if (FRAME_VISIBLE_P (sf)
7707 && !FRAME_OBSCURED_P (sf))
7708 {
7709 if (hscroll_windows (selected_window))
7710 goto retry;
7711
7712 XWINDOW (selected_window)->must_be_updated_p = 1;
7713 pause = update_frame (sf, 0, 0);
7714 }
7715 else
7716 pause = 0;
7717
7718 /* We may have called echo_area_display at the top of this
7719 function. If the echo area is on another frame, that may
7720 have put text on a frame other than the selected one, so the
7721 above call to update_frame would not have caught it. Catch
7722 it here. */
7723 {
7724 Lisp_Object mini_window;
7725 struct frame *mini_frame;
7726
7727 mini_window = FRAME_MINIBUF_WINDOW (sf);
7728 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7729
7730 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
7731 {
7732 XWINDOW (mini_window)->must_be_updated_p = 1;
7733 pause |= update_frame (mini_frame, 0, 0);
7734 if (!pause && hscroll_windows (mini_window))
7735 goto retry;
7736 }
7737 }
7738 }
7739
7740 /* If display was paused because of pending input, make sure we do a
7741 thorough update the next time. */
7742 if (pause)
7743 {
7744 /* Prevent the optimization at the beginning of
7745 redisplay_internal that tries a single-line update of the
7746 line containing the cursor in the selected window. */
7747 CHARPOS (this_line_start_pos) = 0;
7748
7749 /* Let the overlay arrow be updated the next time. */
7750 if (!NILP (last_arrow_position))
7751 {
7752 last_arrow_position = Qt;
7753 last_arrow_string = Qt;
7754 }
7755
7756 /* If we pause after scrolling, some rows in the current
7757 matrices of some windows are not valid. */
7758 if (!WINDOW_FULL_WIDTH_P (w)
7759 && !FRAME_WINDOW_P (XFRAME (w->frame)))
7760 update_mode_lines = 1;
7761 }
7762
7763 /* Now text on frame agrees with windows, so put info into the
7764 windows for partial redisplay to follow. */
7765 if (!pause)
7766 {
7767 register struct buffer *b = XBUFFER (w->buffer);
7768
7769 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
7770 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
7771 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
7772 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
7773
7774 if (consider_all_windows_p)
7775 mark_window_display_accurate (FRAME_ROOT_WINDOW (sf), 1);
7776 else
7777 {
7778 XSETFASTINT (w->last_point, BUF_PT (b));
7779 w->last_cursor = w->cursor;
7780 w->last_cursor_off_p = w->cursor_off_p;
7781
7782 b->clip_changed = 0;
7783 b->prevent_redisplay_optimizations_p = 0;
7784 w->update_mode_line = Qnil;
7785 XSETFASTINT (w->last_modified, BUF_MODIFF (b));
7786 XSETFASTINT (w->last_overlay_modified, BUF_OVERLAY_MODIFF (b));
7787 w->last_had_star
7788 = (BUF_MODIFF (XBUFFER (w->buffer)) > BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7789 ? Qt : Qnil);
7790
7791 /* Record if we are showing a region, so can make sure to
7792 update it fully at next redisplay. */
7793 w->region_showing = (!NILP (Vtransient_mark_mode)
7794 && (w == XWINDOW (current_buffer->last_selected_window)
7795 || highlight_nonselected_windows)
7796 && !NILP (XBUFFER (w->buffer)->mark_active)
7797 ? Fmarker_position (XBUFFER (w->buffer)->mark)
7798 : Qnil);
7799
7800 w->window_end_valid = w->buffer;
7801 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
7802 last_arrow_string = Voverlay_arrow_string;
7803 if (frame_up_to_date_hook != 0)
7804 (*frame_up_to_date_hook) (sf);
7805
7806 w->current_matrix->buffer = b;
7807 w->current_matrix->begv = BUF_BEGV (b);
7808 w->current_matrix->zv = BUF_ZV (b);
7809 }
7810
7811 update_mode_lines = 0;
7812 windows_or_buffers_changed = 0;
7813 }
7814
7815 /* Start SIGIO interrupts coming again. Having them off during the
7816 code above makes it less likely one will discard output, but not
7817 impossible, since there might be stuff in the system buffer here.
7818 But it is much hairier to try to do anything about that. */
7819 if (interrupt_input)
7820 request_sigio ();
7821 start_polling ();
7822
7823 /* If a frame has become visible which was not before, redisplay
7824 again, so that we display it. Expose events for such a frame
7825 (which it gets when becoming visible) don't call the parts of
7826 redisplay constructing glyphs, so simply exposing a frame won't
7827 display anything in this case. So, we have to display these
7828 frames here explicitly. */
7829 if (!pause)
7830 {
7831 Lisp_Object tail, frame;
7832 int new_count = 0;
7833
7834 FOR_EACH_FRAME (tail, frame)
7835 {
7836 int this_is_visible = 0;
7837
7838 if (XFRAME (frame)->visible)
7839 this_is_visible = 1;
7840 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
7841 if (XFRAME (frame)->visible)
7842 this_is_visible = 1;
7843
7844 if (this_is_visible)
7845 new_count++;
7846 }
7847
7848 if (new_count != number_of_visible_frames)
7849 windows_or_buffers_changed++;
7850 }
7851
7852 /* Change frame size now if a change is pending. */
7853 do_pending_window_change (1);
7854
7855 /* If we just did a pending size change, or have additional
7856 visible frames, redisplay again. */
7857 if (windows_or_buffers_changed && !pause)
7858 goto retry;
7859
7860 end_of_redisplay:;
7861
7862 unbind_to (count, Qnil);
7863 }
7864
7865
7866 /* Redisplay, but leave alone any recent echo area message unless
7867 another message has been requested in its place.
7868
7869 This is useful in situations where you need to redisplay but no
7870 user action has occurred, making it inappropriate for the message
7871 area to be cleared. See tracking_off and
7872 wait_reading_process_input for examples of these situations. */
7873
7874 void
7875 redisplay_preserve_echo_area ()
7876 {
7877 if (!NILP (echo_area_buffer[1]))
7878 {
7879 /* We have a previously displayed message, but no current
7880 message. Redisplay the previous message. */
7881 display_last_displayed_message_p = 1;
7882 redisplay_internal (1);
7883 display_last_displayed_message_p = 0;
7884 }
7885 else
7886 redisplay_internal (1);
7887 }
7888
7889
7890 /* Function registered with record_unwind_protect in
7891 redisplay_internal. Clears the flag indicating that a redisplay is
7892 in progress. */
7893
7894 static Lisp_Object
7895 unwind_redisplay (old_redisplaying_p)
7896 Lisp_Object old_redisplaying_p;
7897 {
7898 redisplaying_p = XFASTINT (old_redisplaying_p);
7899 return Qnil;
7900 }
7901
7902
7903 /* Mark the display of windows in the window tree rooted at WINDOW as
7904 accurate or inaccurate. If FLAG is non-zero mark display of WINDOW
7905 as accurate. If FLAG is zero arrange for WINDOW to be redisplayed
7906 the next time redisplay_internal is called. */
7907
7908 void
7909 mark_window_display_accurate (window, accurate_p)
7910 Lisp_Object window;
7911 int accurate_p;
7912 {
7913 struct window *w;
7914
7915 for (; !NILP (window); window = w->next)
7916 {
7917 w = XWINDOW (window);
7918
7919 if (BUFFERP (w->buffer))
7920 {
7921 struct buffer *b = XBUFFER (w->buffer);
7922
7923 XSETFASTINT (w->last_modified,
7924 accurate_p ? BUF_MODIFF (b) : 0);
7925 XSETFASTINT (w->last_overlay_modified,
7926 accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
7927 w->last_had_star = (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b)
7928 ? Qt : Qnil);
7929
7930 #if 0 /* I don't think this is necessary because display_line does it.
7931 Let's check it. */
7932 /* Record if we are showing a region, so can make sure to
7933 update it fully at next redisplay. */
7934 w->region_showing
7935 = (!NILP (Vtransient_mark_mode)
7936 && (w == XWINDOW (current_buffer->last_selected_window)
7937 || highlight_nonselected_windows)
7938 && (!NILP (b->mark_active)
7939 ? Fmarker_position (b->mark)
7940 : Qnil));
7941 #endif
7942
7943 if (accurate_p)
7944 {
7945 b->clip_changed = 0;
7946 b->prevent_redisplay_optimizations_p = 0;
7947 w->current_matrix->buffer = b;
7948 w->current_matrix->begv = BUF_BEGV (b);
7949 w->current_matrix->zv = BUF_ZV (b);
7950 w->last_cursor = w->cursor;
7951 w->last_cursor_off_p = w->cursor_off_p;
7952 if (w == XWINDOW (selected_window))
7953 w->last_point = make_number (BUF_PT (b));
7954 else
7955 w->last_point = make_number (XMARKER (w->pointm)->charpos);
7956 }
7957 }
7958
7959 w->window_end_valid = w->buffer;
7960 w->update_mode_line = Qnil;
7961
7962 if (!NILP (w->vchild))
7963 mark_window_display_accurate (w->vchild, accurate_p);
7964 if (!NILP (w->hchild))
7965 mark_window_display_accurate (w->hchild, accurate_p);
7966 }
7967
7968 if (accurate_p)
7969 {
7970 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
7971 last_arrow_string = Voverlay_arrow_string;
7972 }
7973 else
7974 {
7975 /* Force a thorough redisplay the next time by setting
7976 last_arrow_position and last_arrow_string to t, which is
7977 unequal to any useful value of Voverlay_arrow_... */
7978 last_arrow_position = Qt;
7979 last_arrow_string = Qt;
7980 }
7981 }
7982
7983
7984 /* Return value in display table DP (Lisp_Char_Table *) for character
7985 C. Since a display table doesn't have any parent, we don't have to
7986 follow parent. Do not call this function directly but use the
7987 macro DISP_CHAR_VECTOR. */
7988
7989 Lisp_Object
7990 disp_char_vector (dp, c)
7991 struct Lisp_Char_Table *dp;
7992 int c;
7993 {
7994 int code[4], i;
7995 Lisp_Object val;
7996
7997 if (SINGLE_BYTE_CHAR_P (c))
7998 return (dp->contents[c]);
7999
8000 SPLIT_CHAR (c, code[0], code[1], code[2]);
8001 if (code[1] < 32)
8002 code[1] = -1;
8003 else if (code[2] < 32)
8004 code[2] = -1;
8005
8006 /* Here, the possible range of code[0] (== charset ID) is
8007 128..max_charset. Since the top level char table contains data
8008 for multibyte characters after 256th element, we must increment
8009 code[0] by 128 to get a correct index. */
8010 code[0] += 128;
8011 code[3] = -1; /* anchor */
8012
8013 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
8014 {
8015 val = dp->contents[code[i]];
8016 if (!SUB_CHAR_TABLE_P (val))
8017 return (NILP (val) ? dp->defalt : val);
8018 }
8019
8020 /* Here, val is a sub char table. We return the default value of
8021 it. */
8022 return (dp->defalt);
8023 }
8024
8025
8026 \f
8027 /***********************************************************************
8028 Window Redisplay
8029 ***********************************************************************/
8030
8031 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
8032
8033 static void
8034 redisplay_windows (window)
8035 Lisp_Object window;
8036 {
8037 while (!NILP (window))
8038 {
8039 struct window *w = XWINDOW (window);
8040
8041 if (!NILP (w->hchild))
8042 redisplay_windows (w->hchild);
8043 else if (!NILP (w->vchild))
8044 redisplay_windows (w->vchild);
8045 else
8046 redisplay_window (window, 0);
8047
8048 window = w->next;
8049 }
8050 }
8051
8052
8053 /* Set cursor position of W. PT is assumed to be displayed in ROW.
8054 DELTA is the number of bytes by which positions recorded in ROW
8055 differ from current buffer positions. */
8056
8057 void
8058 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
8059 struct window *w;
8060 struct glyph_row *row;
8061 struct glyph_matrix *matrix;
8062 int delta, delta_bytes, dy, dvpos;
8063 {
8064 struct glyph *glyph = row->glyphs[TEXT_AREA];
8065 struct glyph *end = glyph + row->used[TEXT_AREA];
8066 int x = row->x;
8067 int pt_old = PT - delta;
8068
8069 /* Skip over glyphs not having an object at the start of the row.
8070 These are special glyphs like truncation marks on terminal
8071 frames. */
8072 if (row->displays_text_p)
8073 while (glyph < end
8074 && INTEGERP (glyph->object)
8075 && glyph->charpos < 0)
8076 {
8077 x += glyph->pixel_width;
8078 ++glyph;
8079 }
8080
8081 while (glyph < end
8082 && !INTEGERP (glyph->object)
8083 && (!BUFFERP (glyph->object)
8084 || glyph->charpos < pt_old))
8085 {
8086 x += glyph->pixel_width;
8087 ++glyph;
8088 }
8089
8090 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
8091 w->cursor.x = x;
8092 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
8093 w->cursor.y = row->y + dy;
8094
8095 if (w == XWINDOW (selected_window))
8096 {
8097 if (!row->continued_p
8098 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
8099 && row->x == 0)
8100 {
8101 this_line_buffer = XBUFFER (w->buffer);
8102
8103 CHARPOS (this_line_start_pos)
8104 = MATRIX_ROW_START_CHARPOS (row) + delta;
8105 BYTEPOS (this_line_start_pos)
8106 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
8107
8108 CHARPOS (this_line_end_pos)
8109 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
8110 BYTEPOS (this_line_end_pos)
8111 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
8112
8113 this_line_y = w->cursor.y;
8114 this_line_pixel_height = row->height;
8115 this_line_vpos = w->cursor.vpos;
8116 this_line_start_x = row->x;
8117 }
8118 else
8119 CHARPOS (this_line_start_pos) = 0;
8120 }
8121 }
8122
8123
8124 /* Run window scroll functions, if any, for WINDOW with new window
8125 start STARTP. Sets the window start of WINDOW to that position.
8126
8127 We assume that the window's buffer is really current. */
8128
8129 static INLINE struct text_pos
8130 run_window_scroll_functions (window, startp)
8131 Lisp_Object window;
8132 struct text_pos startp;
8133 {
8134 struct window *w = XWINDOW (window);
8135 SET_MARKER_FROM_TEXT_POS (w->start, startp);
8136
8137 if (current_buffer != XBUFFER (w->buffer))
8138 abort ();
8139
8140 if (!NILP (Vwindow_scroll_functions))
8141 {
8142 run_hook_with_args_2 (Qwindow_scroll_functions, window,
8143 make_number (CHARPOS (startp)));
8144 SET_TEXT_POS_FROM_MARKER (startp, w->start);
8145 /* In case the hook functions switch buffers. */
8146 if (current_buffer != XBUFFER (w->buffer))
8147 set_buffer_internal_1 (XBUFFER (w->buffer));
8148 }
8149
8150 return startp;
8151 }
8152
8153
8154 /* Modify the desired matrix of window W and W->vscroll so that the
8155 line containing the cursor is fully visible. */
8156
8157 static void
8158 make_cursor_line_fully_visible (w)
8159 struct window *w;
8160 {
8161 struct glyph_matrix *matrix;
8162 struct glyph_row *row;
8163 int header_line_height;
8164
8165 /* It's not always possible to find the cursor, e.g, when a window
8166 is full of overlay strings. Don't do anything in that case. */
8167 if (w->cursor.vpos < 0)
8168 return;
8169
8170 matrix = w->desired_matrix;
8171 row = MATRIX_ROW (matrix, w->cursor.vpos);
8172
8173 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row)
8174 /* The row may be partially visible at the top because we
8175 already have chosen a vscroll to align the bottom of the
8176 row with the bottom of the window. This happens for rows
8177 taller than the window. */
8178 && row->y + row->height < window_box_height (w))
8179 {
8180 int dy = row->height - row->visible_height;
8181 w->vscroll = 0;
8182 w->cursor.y += dy;
8183 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
8184 }
8185 else if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)
8186 /* The row may be partially visible at the bottom because
8187 we chose a vscroll to align the row's top with the
8188 window's top. This happens for rows taller than the
8189 window. */
8190 && row->y > WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w))
8191 {
8192 int dy = - (row->height - row->visible_height);
8193 w->vscroll = dy;
8194 w->cursor.y += dy;
8195 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
8196 }
8197
8198 /* When we change the cursor y-position of the selected window,
8199 change this_line_y as well so that the display optimization for
8200 the cursor line of the selected window in redisplay_internal uses
8201 the correct y-position. */
8202 if (w == XWINDOW (selected_window))
8203 this_line_y = w->cursor.y;
8204 }
8205
8206
8207 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
8208 non-zero means only WINDOW is redisplayed in redisplay_internal.
8209 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
8210 in redisplay_window to bring a partially visible line into view in
8211 the case that only the cursor has moved.
8212
8213 Value is
8214
8215 1 if scrolling succeeded
8216
8217 0 if scrolling didn't find point.
8218
8219 -1 if new fonts have been loaded so that we must interrupt
8220 redisplay, adjust glyph matrices, and try again. */
8221
8222 static int
8223 try_scrolling (window, just_this_one_p, scroll_conservatively,
8224 scroll_step, temp_scroll_step)
8225 Lisp_Object window;
8226 int just_this_one_p;
8227 int scroll_conservatively, scroll_step;
8228 int temp_scroll_step;
8229 {
8230 struct window *w = XWINDOW (window);
8231 struct frame *f = XFRAME (w->frame);
8232 struct text_pos scroll_margin_pos;
8233 struct text_pos pos;
8234 struct text_pos startp;
8235 struct it it;
8236 Lisp_Object window_end;
8237 int this_scroll_margin;
8238 int dy = 0;
8239 int scroll_max;
8240 int line_height, rc;
8241 int amount_to_scroll = 0;
8242 Lisp_Object aggressive;
8243 int height;
8244
8245 #if GLYPH_DEBUG
8246 debug_method_add (w, "try_scrolling");
8247 #endif
8248
8249 SET_TEXT_POS_FROM_MARKER (startp, w->start);
8250
8251 /* Compute scroll margin height in pixels. We scroll when point is
8252 within this distance from the top or bottom of the window. */
8253 if (scroll_margin > 0)
8254 {
8255 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
8256 this_scroll_margin *= CANON_Y_UNIT (f);
8257 }
8258 else
8259 this_scroll_margin = 0;
8260
8261 /* Compute how much we should try to scroll maximally to bring point
8262 into view. */
8263 if (scroll_step)
8264 scroll_max = scroll_step;
8265 else if (scroll_conservatively)
8266 scroll_max = scroll_conservatively;
8267 else if (temp_scroll_step)
8268 scroll_max = temp_scroll_step;
8269 else if (NUMBERP (current_buffer->scroll_down_aggressively)
8270 || NUMBERP (current_buffer->scroll_up_aggressively))
8271 /* We're trying to scroll because of aggressive scrolling
8272 but no scroll_step is set. Choose an arbitrary one. Maybe
8273 there should be a variable for this. */
8274 scroll_max = 10;
8275 else
8276 scroll_max = 0;
8277 scroll_max *= CANON_Y_UNIT (f);
8278
8279 /* Decide whether we have to scroll down. Start at the window end
8280 and move this_scroll_margin up to find the position of the scroll
8281 margin. */
8282 window_end = Fwindow_end (window, Qt);
8283 CHARPOS (scroll_margin_pos) = XINT (window_end);
8284 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
8285 if (this_scroll_margin)
8286 {
8287 start_display (&it, w, scroll_margin_pos);
8288 move_it_vertically (&it, - this_scroll_margin);
8289 scroll_margin_pos = it.current.pos;
8290 }
8291
8292 if (PT >= CHARPOS (scroll_margin_pos))
8293 {
8294 int y0;
8295
8296 /* Point is in the scroll margin at the bottom of the window, or
8297 below. Compute a new window start that makes point visible. */
8298
8299 /* Compute the distance from the scroll margin to PT.
8300 Give up if the distance is greater than scroll_max. */
8301 start_display (&it, w, scroll_margin_pos);
8302 y0 = it.current_y;
8303 move_it_to (&it, PT, 0, it.last_visible_y, -1,
8304 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
8305 line_height = (it.max_ascent + it.max_descent
8306 ? it.max_ascent + it.max_descent
8307 : last_height);
8308 dy = it.current_y + line_height - y0;
8309 if (dy > scroll_max)
8310 return 0;
8311
8312 /* Move the window start down. If scrolling conservatively,
8313 move it just enough down to make point visible. If
8314 scroll_step is set, move it down by scroll_step. */
8315 start_display (&it, w, startp);
8316
8317 if (scroll_conservatively)
8318 amount_to_scroll = dy;
8319 else if (scroll_step || temp_scroll_step)
8320 amount_to_scroll = scroll_max;
8321 else
8322 {
8323 aggressive = current_buffer->scroll_down_aggressively;
8324 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
8325 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
8326 if (NUMBERP (aggressive))
8327 amount_to_scroll = XFLOATINT (aggressive) * height;
8328 }
8329
8330 if (amount_to_scroll <= 0)
8331 return 0;
8332
8333 move_it_vertically (&it, amount_to_scroll);
8334 startp = it.current.pos;
8335 }
8336 else
8337 {
8338 /* See if point is inside the scroll margin at the top of the
8339 window. */
8340 scroll_margin_pos = startp;
8341 if (this_scroll_margin)
8342 {
8343 start_display (&it, w, startp);
8344 move_it_vertically (&it, this_scroll_margin);
8345 scroll_margin_pos = it.current.pos;
8346 }
8347
8348 if (PT < CHARPOS (scroll_margin_pos))
8349 {
8350 /* Point is in the scroll margin at the top of the window or
8351 above what is displayed in the window. */
8352 int y0;
8353
8354 /* Compute the vertical distance from PT to the scroll
8355 margin position. Give up if distance is greater than
8356 scroll_max. */
8357 SET_TEXT_POS (pos, PT, PT_BYTE);
8358 start_display (&it, w, pos);
8359 y0 = it.current_y;
8360 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
8361 it.last_visible_y, -1,
8362 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
8363 dy = it.current_y - y0;
8364 if (dy > scroll_max)
8365 return 0;
8366
8367 /* Compute new window start. */
8368 start_display (&it, w, startp);
8369
8370 if (scroll_conservatively)
8371 amount_to_scroll = dy;
8372 else if (scroll_step || temp_scroll_step)
8373 amount_to_scroll = scroll_max;
8374 else
8375 {
8376 aggressive = current_buffer->scroll_up_aggressively;
8377 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
8378 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
8379 if (NUMBERP (aggressive))
8380 amount_to_scroll = XFLOATINT (aggressive) * height;
8381 }
8382
8383 if (amount_to_scroll <= 0)
8384 return 0;
8385
8386 move_it_vertically (&it, - amount_to_scroll);
8387 startp = it.current.pos;
8388 }
8389 }
8390
8391 /* Run window scroll functions. */
8392 startp = run_window_scroll_functions (window, startp);
8393
8394 /* Display the window. Give up if new fonts are loaded, or if point
8395 doesn't appear. */
8396 if (!try_window (window, startp))
8397 rc = -1;
8398 else if (w->cursor.vpos < 0)
8399 {
8400 clear_glyph_matrix (w->desired_matrix);
8401 rc = 0;
8402 }
8403 else
8404 {
8405 /* Maybe forget recorded base line for line number display. */
8406 if (!just_this_one_p
8407 || current_buffer->clip_changed
8408 || BEG_UNCHANGED < CHARPOS (startp))
8409 w->base_line_number = Qnil;
8410
8411 /* If cursor ends up on a partially visible line, shift display
8412 lines up or down. */
8413 make_cursor_line_fully_visible (w);
8414 rc = 1;
8415 }
8416
8417 return rc;
8418 }
8419
8420
8421 /* Compute a suitable window start for window W if display of W starts
8422 on a continuation line. Value is non-zero if a new window start
8423 was computed.
8424
8425 The new window start will be computed, based on W's width, starting
8426 from the start of the continued line. It is the start of the
8427 screen line with the minimum distance from the old start W->start. */
8428
8429 static int
8430 compute_window_start_on_continuation_line (w)
8431 struct window *w;
8432 {
8433 struct text_pos pos, start_pos;
8434 int window_start_changed_p = 0;
8435
8436 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
8437
8438 /* If window start is on a continuation line... Window start may be
8439 < BEGV in case there's invisible text at the start of the
8440 buffer (M-x rmail, for example). */
8441 if (CHARPOS (start_pos) > BEGV
8442 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
8443 {
8444 struct it it;
8445 struct glyph_row *row;
8446
8447 /* Handle the case that the window start is out of range. */
8448 if (CHARPOS (start_pos) < BEGV)
8449 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
8450 else if (CHARPOS (start_pos) > ZV)
8451 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
8452
8453 /* Find the start of the continued line. This should be fast
8454 because scan_buffer is fast (newline cache). */
8455 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
8456 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
8457 row, DEFAULT_FACE_ID);
8458 reseat_at_previous_visible_line_start (&it);
8459
8460 /* If the line start is "too far" away from the window start,
8461 say it takes too much time to compute a new window start. */
8462 if (CHARPOS (start_pos) - IT_CHARPOS (it)
8463 < XFASTINT (w->height) * XFASTINT (w->width))
8464 {
8465 int min_distance, distance;
8466
8467 /* Move forward by display lines to find the new window
8468 start. If window width was enlarged, the new start can
8469 be expected to be > the old start. If window width was
8470 decreased, the new window start will be < the old start.
8471 So, we're looking for the display line start with the
8472 minimum distance from the old window start. */
8473 pos = it.current.pos;
8474 min_distance = INFINITY;
8475 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
8476 distance < min_distance)
8477 {
8478 min_distance = distance;
8479 pos = it.current.pos;
8480 move_it_by_lines (&it, 1, 0);
8481 }
8482
8483 /* Set the window start there. */
8484 SET_MARKER_FROM_TEXT_POS (w->start, pos);
8485 window_start_changed_p = 1;
8486 }
8487 }
8488
8489 return window_start_changed_p;
8490 }
8491
8492
8493 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
8494 selected_window is redisplayed. */
8495
8496 static void
8497 redisplay_window (window, just_this_one_p)
8498 Lisp_Object window;
8499 int just_this_one_p;
8500 {
8501 struct window *w = XWINDOW (window);
8502 struct frame *f = XFRAME (w->frame);
8503 struct buffer *buffer = XBUFFER (w->buffer);
8504 struct buffer *old = current_buffer;
8505 struct text_pos lpoint, opoint, startp;
8506 int update_mode_line;
8507 int tem;
8508 struct it it;
8509 /* Record it now because it's overwritten. */
8510 int current_matrix_up_to_date_p = 0;
8511 int really_switched_buffer = 0;
8512 int temp_scroll_step = 0;
8513 int count = specpdl_ptr - specpdl;
8514
8515 SET_TEXT_POS (lpoint, PT, PT_BYTE);
8516 opoint = lpoint;
8517
8518 /* W must be a leaf window here. */
8519 xassert (!NILP (w->buffer));
8520 #if GLYPH_DEBUG
8521 *w->desired_matrix->method = 0;
8522 #endif
8523
8524 specbind (Qinhibit_point_motion_hooks, Qt);
8525
8526 reconsider_clip_changes (w, buffer);
8527
8528 /* Has the mode line to be updated? */
8529 update_mode_line = (!NILP (w->update_mode_line)
8530 || update_mode_lines
8531 || buffer->clip_changed);
8532
8533 if (MINI_WINDOW_P (w))
8534 {
8535 if (w == XWINDOW (echo_area_window)
8536 && !NILP (echo_area_buffer[0]))
8537 {
8538 if (update_mode_line)
8539 /* We may have to update a tty frame's menu bar or a
8540 tool-bar. Example `M-x C-h C-h C-g'. */
8541 goto finish_menu_bars;
8542 else
8543 /* We've already displayed the echo area glyphs in this window. */
8544 goto finish_scroll_bars;
8545 }
8546 else if (w != XWINDOW (minibuf_window))
8547 {
8548 /* W is a mini-buffer window, but it's not the currently
8549 active one, so clear it. */
8550 int yb = window_text_bottom_y (w);
8551 struct glyph_row *row;
8552 int y;
8553
8554 for (y = 0, row = w->desired_matrix->rows;
8555 y < yb;
8556 y += row->height, ++row)
8557 blank_row (w, row, y);
8558 goto finish_scroll_bars;
8559 }
8560 }
8561
8562 /* Otherwise set up data on this window; select its buffer and point
8563 value. */
8564 if (update_mode_line)
8565 {
8566 /* Really select the buffer, for the sake of buffer-local
8567 variables. */
8568 set_buffer_internal_1 (XBUFFER (w->buffer));
8569 really_switched_buffer = 1;
8570 }
8571 else
8572 set_buffer_temp (XBUFFER (w->buffer));
8573 SET_TEXT_POS (opoint, PT, PT_BYTE);
8574
8575 current_matrix_up_to_date_p
8576 = (!NILP (w->window_end_valid)
8577 && !current_buffer->clip_changed
8578 && XFASTINT (w->last_modified) >= MODIFF
8579 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
8580
8581 /* When windows_or_buffers_changed is non-zero, we can't rely on
8582 the window end being valid, so set it to nil there. */
8583 if (windows_or_buffers_changed)
8584 {
8585 /* If window starts on a continuation line, maybe adjust the
8586 window start in case the window's width changed. */
8587 if (XMARKER (w->start)->buffer == current_buffer)
8588 compute_window_start_on_continuation_line (w);
8589
8590 w->window_end_valid = Qnil;
8591 }
8592
8593 /* Some sanity checks. */
8594 CHECK_WINDOW_END (w);
8595 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
8596 abort ();
8597 if (BYTEPOS (opoint) < CHARPOS (opoint))
8598 abort ();
8599
8600 /* If %c is in mode line, update it if needed. */
8601 if (!NILP (w->column_number_displayed)
8602 /* This alternative quickly identifies a common case
8603 where no change is needed. */
8604 && !(PT == XFASTINT (w->last_point)
8605 && XFASTINT (w->last_modified) >= MODIFF
8606 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
8607 && XFASTINT (w->column_number_displayed) != current_column ())
8608 update_mode_line = 1;
8609
8610 /* Count number of windows showing the selected buffer. An indirect
8611 buffer counts as its base buffer. */
8612 if (!just_this_one_p)
8613 {
8614 struct buffer *current_base, *window_base;
8615 current_base = current_buffer;
8616 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
8617 if (current_base->base_buffer)
8618 current_base = current_base->base_buffer;
8619 if (window_base->base_buffer)
8620 window_base = window_base->base_buffer;
8621 if (current_base == window_base)
8622 buffer_shared++;
8623 }
8624
8625 /* Point refers normally to the selected window. For any other
8626 window, set up appropriate value. */
8627 if (!EQ (window, selected_window))
8628 {
8629 int new_pt = XMARKER (w->pointm)->charpos;
8630 int new_pt_byte = marker_byte_position (w->pointm);
8631 if (new_pt < BEGV)
8632 {
8633 new_pt = BEGV;
8634 new_pt_byte = BEGV_BYTE;
8635 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
8636 }
8637 else if (new_pt > (ZV - 1))
8638 {
8639 new_pt = ZV;
8640 new_pt_byte = ZV_BYTE;
8641 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
8642 }
8643
8644 /* We don't use SET_PT so that the point-motion hooks don't run. */
8645 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
8646 }
8647
8648 /* If any of the character widths specified in the display table
8649 have changed, invalidate the width run cache. It's true that
8650 this may be a bit late to catch such changes, but the rest of
8651 redisplay goes (non-fatally) haywire when the display table is
8652 changed, so why should we worry about doing any better? */
8653 if (current_buffer->width_run_cache)
8654 {
8655 struct Lisp_Char_Table *disptab = buffer_display_table ();
8656
8657 if (! disptab_matches_widthtab (disptab,
8658 XVECTOR (current_buffer->width_table)))
8659 {
8660 invalidate_region_cache (current_buffer,
8661 current_buffer->width_run_cache,
8662 BEG, Z);
8663 recompute_width_table (current_buffer, disptab);
8664 }
8665 }
8666
8667 /* If window-start is screwed up, choose a new one. */
8668 if (XMARKER (w->start)->buffer != current_buffer)
8669 goto recenter;
8670
8671 SET_TEXT_POS_FROM_MARKER (startp, w->start);
8672
8673 /* If someone specified a new starting point but did not insist,
8674 check whether it can be used. */
8675 if (!NILP (w->optional_new_start)
8676 && CHARPOS (startp) >= BEGV
8677 && CHARPOS (startp) <= ZV)
8678 {
8679 w->optional_new_start = Qnil;
8680 /* This takes a mini-buffer prompt into account. */
8681 start_display (&it, w, startp);
8682 move_it_to (&it, PT, 0, it.last_visible_y, -1,
8683 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
8684 if (IT_CHARPOS (it) == PT)
8685 w->force_start = Qt;
8686 }
8687
8688 /* Handle case where place to start displaying has been specified,
8689 unless the specified location is outside the accessible range. */
8690 if (!NILP (w->force_start)
8691 || w->frozen_window_start_p)
8692 {
8693 w->force_start = Qnil;
8694 w->vscroll = 0;
8695 w->window_end_valid = Qnil;
8696
8697 /* Forget any recorded base line for line number display. */
8698 if (!current_matrix_up_to_date_p
8699 || current_buffer->clip_changed)
8700 w->base_line_number = Qnil;
8701
8702 /* Redisplay the mode line. Select the buffer properly for that.
8703 Also, run the hook window-scroll-functions
8704 because we have scrolled. */
8705 /* Note, we do this after clearing force_start because
8706 if there's an error, it is better to forget about force_start
8707 than to get into an infinite loop calling the hook functions
8708 and having them get more errors. */
8709 if (!update_mode_line
8710 || ! NILP (Vwindow_scroll_functions))
8711 {
8712 if (!really_switched_buffer)
8713 {
8714 set_buffer_temp (old);
8715 set_buffer_internal_1 (XBUFFER (w->buffer));
8716 really_switched_buffer = 1;
8717 }
8718
8719 update_mode_line = 1;
8720 w->update_mode_line = Qt;
8721 startp = run_window_scroll_functions (window, startp);
8722 }
8723
8724 XSETFASTINT (w->last_modified, 0);
8725 XSETFASTINT (w->last_overlay_modified, 0);
8726 if (CHARPOS (startp) < BEGV)
8727 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
8728 else if (CHARPOS (startp) > ZV)
8729 SET_TEXT_POS (startp, ZV, ZV_BYTE);
8730
8731 /* Redisplay, then check if cursor has been set during the
8732 redisplay. Give up if new fonts were loaded. */
8733 if (!try_window (window, startp))
8734 {
8735 w->force_start = Qt;
8736 clear_glyph_matrix (w->desired_matrix);
8737 goto restore_buffers;
8738 }
8739
8740 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
8741 {
8742 /* If point does not appear, or on a line that is not fully
8743 visible, move point so it does appear. The desired
8744 matrix has been built above, so we can use it. */
8745 int height = window_box_height (w) / 2;
8746 struct glyph_row *row = MATRIX_ROW (w->desired_matrix, 0);
8747
8748 while (row->y < height)
8749 ++row;
8750
8751 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
8752 MATRIX_ROW_START_BYTEPOS (row));
8753
8754 if (w != XWINDOW (selected_window))
8755 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
8756 else if (current_buffer == old)
8757 SET_TEXT_POS (lpoint, PT, PT_BYTE);
8758
8759 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
8760
8761 /* If we are highlighting the region, then we just changed
8762 the region, so redisplay to show it. */
8763 if (!NILP (Vtransient_mark_mode)
8764 && !NILP (current_buffer->mark_active))
8765 {
8766 clear_glyph_matrix (w->desired_matrix);
8767 if (!try_window (window, startp))
8768 goto restore_buffers;
8769 }
8770 }
8771
8772 make_cursor_line_fully_visible (w);
8773 #if GLYPH_DEBUG
8774 debug_method_add (w, "forced window start");
8775 #endif
8776 goto done;
8777 }
8778
8779 /* Handle case where text has not changed, only point, and it has
8780 not moved off the frame. */
8781 if (current_matrix_up_to_date_p
8782 /* Point may be in this window. */
8783 && PT >= CHARPOS (startp)
8784 /* If we don't check this, we are called to move the cursor in a
8785 horizontally split window with a current matrix that doesn't
8786 fit the display. */
8787 && !windows_or_buffers_changed
8788 /* Selective display hasn't changed. */
8789 && !current_buffer->clip_changed
8790 /* If force-mode-line-update was called, really redisplay;
8791 that's how redisplay is forced after e.g. changing
8792 buffer-invisibility-spec. */
8793 && NILP (w->update_mode_line)
8794 /* Can't use this case if highlighting a region. When a
8795 region exists, cursor movement has to do more than just
8796 set the cursor. */
8797 && !(!NILP (Vtransient_mark_mode)
8798 && !NILP (current_buffer->mark_active))
8799 && NILP (w->region_showing)
8800 && NILP (Vshow_trailing_whitespace)
8801 /* Right after splitting windows, last_point may be nil. */
8802 && INTEGERP (w->last_point)
8803 /* This code is not used for mini-buffer for the sake of the case
8804 of redisplaying to replace an echo area message; since in
8805 that case the mini-buffer contents per se are usually
8806 unchanged. This code is of no real use in the mini-buffer
8807 since the handling of this_line_start_pos, etc., in redisplay
8808 handles the same cases. */
8809 && !EQ (window, minibuf_window)
8810 /* When splitting windows or for new windows, it happens that
8811 redisplay is called with a nil window_end_vpos or one being
8812 larger than the window. This should really be fixed in
8813 window.c. I don't have this on my list, now, so we do
8814 approximately the same as the old redisplay code. --gerd. */
8815 && INTEGERP (w->window_end_vpos)
8816 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
8817 && (FRAME_WINDOW_P (f)
8818 || !MARKERP (Voverlay_arrow_position)
8819 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
8820 {
8821 int this_scroll_margin;
8822 struct glyph_row *row;
8823 int scroll_p;
8824
8825 #if GLYPH_DEBUG
8826 debug_method_add (w, "cursor movement");
8827 #endif
8828
8829 /* Scroll if point within this distance from the top or bottom
8830 of the window. This is a pixel value. */
8831 this_scroll_margin = max (0, scroll_margin);
8832 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
8833 this_scroll_margin *= CANON_Y_UNIT (f);
8834
8835 /* Start with the row the cursor was displayed during the last
8836 not paused redisplay. Give up if that row is not valid. */
8837 if (w->last_cursor.vpos >= w->current_matrix->nrows)
8838 goto try_to_scroll;
8839 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
8840 if (row->mode_line_p)
8841 ++row;
8842 if (!row->enabled_p)
8843 goto try_to_scroll;
8844
8845 scroll_p = 0;
8846 if (PT > XFASTINT (w->last_point))
8847 {
8848 /* Point has moved forward. */
8849 int last_y = window_text_bottom_y (w) - this_scroll_margin;
8850
8851 while ((MATRIX_ROW_END_CHARPOS (row) < PT
8852 /* The end position of a row equals the start
8853 position of the next row. If PT is there, we
8854 would rather display it in the next line, except
8855 when this line ends in ZV. */
8856 || (MATRIX_ROW_END_CHARPOS (row) == PT
8857 && (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
8858 || !row->ends_at_zv_p)))
8859 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
8860 {
8861 xassert (row->enabled_p);
8862 ++row;
8863 }
8864
8865 /* If within the scroll margin, scroll. Note that
8866 MATRIX_ROW_BOTTOM_Y gives the pixel position at which the
8867 next line would be drawn, and that this_scroll_margin can
8868 be zero. */
8869 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
8870 || PT > MATRIX_ROW_END_CHARPOS (row)
8871 /* Line is completely visible last line in window and PT
8872 is to be set in the next line. */
8873 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
8874 && PT == MATRIX_ROW_END_CHARPOS (row)
8875 && !row->ends_at_zv_p
8876 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
8877 scroll_p = 1;
8878 }
8879 else if (PT < XFASTINT (w->last_point))
8880 {
8881 /* Cursor has to be moved backward. Note that PT >=
8882 CHARPOS (startp) because of the outer if-statement. */
8883 while (!row->mode_line_p
8884 && (MATRIX_ROW_START_CHARPOS (row) > PT
8885 || (MATRIX_ROW_START_CHARPOS (row) == PT
8886 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
8887 && (row->y > this_scroll_margin
8888 || CHARPOS (startp) == BEGV))
8889 {
8890 xassert (row->enabled_p);
8891 --row;
8892 }
8893
8894 /* Consider the following case: Window starts at BEGV, there
8895 is invisible, intangible text at BEGV, so that display
8896 starts at some point START > BEGV. It can happen that
8897 we are called with PT somewhere between BEGV and START.
8898 Try to handle that case. */
8899 if (row < w->current_matrix->rows
8900 || row->mode_line_p)
8901 {
8902 row = w->current_matrix->rows;
8903 if (row->mode_line_p)
8904 ++row;
8905 }
8906
8907 /* Due to newlines in overlay strings, we may have to skip
8908 forward over overlay strings. */
8909 while (MATRIX_ROW_END_CHARPOS (row) == PT
8910 && MATRIX_ROW_ENDS_IN_OVERLAY_STRING_P (row)
8911 && !row->ends_at_zv_p)
8912 ++row;
8913
8914 /* If within the scroll margin, scroll. */
8915 if (row->y < this_scroll_margin
8916 && CHARPOS (startp) != BEGV)
8917 scroll_p = 1;
8918 }
8919
8920 /* if PT is not in the glyph row, give up. */
8921 if (PT < MATRIX_ROW_START_CHARPOS (row)
8922 || PT > MATRIX_ROW_END_CHARPOS (row))
8923 goto try_to_scroll;
8924
8925 /* If we end up in a partially visible line, let's make it fully
8926 visible. This can be done most easily by using the existing
8927 scrolling code. */
8928 if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
8929 {
8930 temp_scroll_step = 1;
8931 goto try_to_scroll;
8932 }
8933 else if (scroll_p)
8934 goto try_to_scroll;
8935
8936 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
8937 goto done;
8938 }
8939
8940 /* If current starting point was originally the beginning of a line
8941 but no longer is, find a new starting point. */
8942 else if (!NILP (w->start_at_line_beg)
8943 && !(CHARPOS (startp) <= BEGV
8944 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
8945 {
8946 #if GLYPH_DEBUG
8947 debug_method_add (w, "recenter 1");
8948 #endif
8949 goto recenter;
8950 }
8951
8952 /* Try scrolling with try_window_id. */
8953 else if (/* Windows and buffers haven't changed. */
8954 !windows_or_buffers_changed
8955 /* Window must be either use window-based redisplay or
8956 be full width. */
8957 && (FRAME_WINDOW_P (f)
8958 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)))
8959 && !MINI_WINDOW_P (w)
8960 /* Point is not known NOT to appear in window. */
8961 && PT >= CHARPOS (startp)
8962 && XFASTINT (w->last_modified)
8963 /* Window is not hscrolled. */
8964 && XFASTINT (w->hscroll) == 0
8965 /* Selective display has not changed. */
8966 && !current_buffer->clip_changed
8967 /* Current matrix is up to date. */
8968 && !NILP (w->window_end_valid)
8969 /* Can't use this case if highlighting a region because
8970 a cursor movement will do more than just set the cursor. */
8971 && !(!NILP (Vtransient_mark_mode)
8972 && !NILP (current_buffer->mark_active))
8973 && NILP (w->region_showing)
8974 && NILP (Vshow_trailing_whitespace)
8975 /* Overlay arrow position and string not changed. */
8976 && EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
8977 && EQ (last_arrow_string, Voverlay_arrow_string)
8978 /* Value is > 0 if update has been done, it is -1 if we
8979 know that the same window start will not work. It is 0
8980 if unsuccessful for some other reason. */
8981 && (tem = try_window_id (w)) != 0)
8982 {
8983 #if GLYPH_DEBUG
8984 debug_method_add (w, "try_window_id");
8985 #endif
8986
8987 if (fonts_changed_p)
8988 goto restore_buffers;
8989 if (tem > 0)
8990 goto done;
8991 /* Otherwise try_window_id has returned -1 which means that we
8992 don't want the alternative below this comment to execute. */
8993 }
8994 else if (CHARPOS (startp) >= BEGV
8995 && CHARPOS (startp) <= ZV
8996 && PT >= CHARPOS (startp)
8997 && (CHARPOS (startp) < ZV
8998 /* Avoid starting at end of buffer. */
8999 || CHARPOS (startp) == BEGV
9000 || (XFASTINT (w->last_modified) >= MODIFF
9001 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
9002 {
9003 #if GLYPH_DEBUG
9004 debug_method_add (w, "same window start");
9005 #endif
9006
9007 /* Try to redisplay starting at same place as before.
9008 If point has not moved off frame, accept the results. */
9009 if (!current_matrix_up_to_date_p
9010 /* Don't use try_window_reusing_current_matrix in this case
9011 because a window scroll function can have changed the
9012 buffer. */
9013 || !NILP (Vwindow_scroll_functions)
9014 || MINI_WINDOW_P (w)
9015 || !try_window_reusing_current_matrix (w))
9016 {
9017 IF_DEBUG (debug_method_add (w, "1"));
9018 try_window (window, startp);
9019 }
9020
9021 if (fonts_changed_p)
9022 goto restore_buffers;
9023
9024 if (w->cursor.vpos >= 0)
9025 {
9026 if (!just_this_one_p
9027 || current_buffer->clip_changed
9028 || BEG_UNCHANGED < CHARPOS (startp))
9029 /* Forget any recorded base line for line number display. */
9030 w->base_line_number = Qnil;
9031
9032 make_cursor_line_fully_visible (w);
9033 goto done;
9034 }
9035 else
9036 clear_glyph_matrix (w->desired_matrix);
9037 }
9038
9039 try_to_scroll:
9040
9041 XSETFASTINT (w->last_modified, 0);
9042 XSETFASTINT (w->last_overlay_modified, 0);
9043
9044 /* Redisplay the mode line. Select the buffer properly for that. */
9045 if (!update_mode_line)
9046 {
9047 if (!really_switched_buffer)
9048 {
9049 set_buffer_temp (old);
9050 set_buffer_internal_1 (XBUFFER (w->buffer));
9051 really_switched_buffer = 1;
9052 }
9053 update_mode_line = 1;
9054 w->update_mode_line = Qt;
9055 }
9056
9057 /* Try to scroll by specified few lines. */
9058 if ((scroll_conservatively
9059 || scroll_step
9060 || temp_scroll_step
9061 || NUMBERP (current_buffer->scroll_up_aggressively)
9062 || NUMBERP (current_buffer->scroll_down_aggressively))
9063 && !current_buffer->clip_changed
9064 && CHARPOS (startp) >= BEGV
9065 && CHARPOS (startp) <= ZV)
9066 {
9067 /* The function returns -1 if new fonts were loaded, 1 if
9068 successful, 0 if not successful. */
9069 int rc = try_scrolling (window, just_this_one_p,
9070 scroll_conservatively,
9071 scroll_step,
9072 temp_scroll_step);
9073 if (rc > 0)
9074 goto done;
9075 else if (rc < 0)
9076 goto restore_buffers;
9077 }
9078
9079 /* Finally, just choose place to start which centers point */
9080
9081 recenter:
9082
9083 #if GLYPH_DEBUG
9084 debug_method_add (w, "recenter");
9085 #endif
9086
9087 /* w->vscroll = 0; */
9088
9089 /* Forget any previously recorded base line for line number display. */
9090 if (!current_matrix_up_to_date_p
9091 || current_buffer->clip_changed)
9092 w->base_line_number = Qnil;
9093
9094 /* Move backward half the height of the window. */
9095 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
9096 it.current_y = it.last_visible_y;
9097 move_it_vertically_backward (&it, it.last_visible_y / 2);
9098 xassert (IT_CHARPOS (it) >= BEGV);
9099
9100 /* The function move_it_vertically_backward may move over more
9101 than the specified y-distance. If it->w is small, e.g. a
9102 mini-buffer window, we may end up in front of the window's
9103 display area. Start displaying at the start of the line
9104 containing PT in this case. */
9105 if (it.current_y <= 0)
9106 {
9107 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
9108 move_it_vertically (&it, 0);
9109 xassert (IT_CHARPOS (it) <= PT);
9110 it.current_y = 0;
9111 }
9112
9113 it.current_x = it.hpos = 0;
9114
9115 /* Set startp here explicitly in case that helps avoid an infinite loop
9116 in case the window-scroll-functions functions get errors. */
9117 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
9118
9119 /* Run scroll hooks. */
9120 startp = run_window_scroll_functions (window, it.current.pos);
9121
9122 /* Redisplay the window. */
9123 if (!current_matrix_up_to_date_p
9124 || windows_or_buffers_changed
9125 /* Don't use try_window_reusing_current_matrix in this case
9126 because it can have changed the buffer. */
9127 || !NILP (Vwindow_scroll_functions)
9128 || !just_this_one_p
9129 || MINI_WINDOW_P (w)
9130 || !try_window_reusing_current_matrix (w))
9131 try_window (window, startp);
9132
9133 /* If new fonts have been loaded (due to fontsets), give up. We
9134 have to start a new redisplay since we need to re-adjust glyph
9135 matrices. */
9136 if (fonts_changed_p)
9137 goto restore_buffers;
9138
9139 /* If cursor did not appear assume that the middle of the window is
9140 in the first line of the window. Do it again with the next line.
9141 (Imagine a window of height 100, displaying two lines of height
9142 60. Moving back 50 from it->last_visible_y will end in the first
9143 line.) */
9144 if (w->cursor.vpos < 0)
9145 {
9146 if (!NILP (w->window_end_valid)
9147 && PT >= Z - XFASTINT (w->window_end_pos))
9148 {
9149 clear_glyph_matrix (w->desired_matrix);
9150 move_it_by_lines (&it, 1, 0);
9151 try_window (window, it.current.pos);
9152 }
9153 else if (PT < IT_CHARPOS (it))
9154 {
9155 clear_glyph_matrix (w->desired_matrix);
9156 move_it_by_lines (&it, -1, 0);
9157 try_window (window, it.current.pos);
9158 }
9159 else
9160 {
9161 /* Not much we can do about it. */
9162 }
9163 }
9164
9165 /* Consider the following case: Window starts at BEGV, there is
9166 invisible, intangible text at BEGV, so that display starts at
9167 some point START > BEGV. It can happen that we are called with
9168 PT somewhere between BEGV and START. Try to handle that case. */
9169 if (w->cursor.vpos < 0)
9170 {
9171 struct glyph_row *row = w->current_matrix->rows;
9172 if (row->mode_line_p)
9173 ++row;
9174 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9175 }
9176
9177 make_cursor_line_fully_visible (w);
9178
9179 done:
9180
9181 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9182 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
9183 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
9184 ? Qt : Qnil);
9185
9186 /* Display the mode line, if we must. */
9187 if ((update_mode_line
9188 /* If window not full width, must redo its mode line
9189 if (a) the window to its side is being redone and
9190 (b) we do a frame-based redisplay. This is a consequence
9191 of how inverted lines are drawn in frame-based redisplay. */
9192 || (!just_this_one_p
9193 && !FRAME_WINDOW_P (f)
9194 && !WINDOW_FULL_WIDTH_P (w))
9195 /* Line number to display. */
9196 || INTEGERP (w->base_line_pos)
9197 /* Column number is displayed and different from the one displayed. */
9198 || (!NILP (w->column_number_displayed)
9199 && XFASTINT (w->column_number_displayed) != current_column ()))
9200 /* This means that the window has a mode line. */
9201 && (WINDOW_WANTS_MODELINE_P (w)
9202 || WINDOW_WANTS_HEADER_LINE_P (w)))
9203 {
9204 Lisp_Object old_selected_frame;
9205
9206 old_selected_frame = selected_frame;
9207
9208 if (!really_switched_buffer)
9209 {
9210 set_buffer_temp (old);
9211 set_buffer_internal_1 (XBUFFER (w->buffer));
9212 really_switched_buffer = 1;
9213 }
9214
9215 XSETFRAME (selected_frame, f);
9216 display_mode_lines (w);
9217 selected_frame = old_selected_frame;
9218
9219 /* If mode line height has changed, arrange for a thorough
9220 immediate redisplay using the correct mode line height. */
9221 if (WINDOW_WANTS_MODELINE_P (w)
9222 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
9223 {
9224 fonts_changed_p = 1;
9225 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
9226 = DESIRED_MODE_LINE_HEIGHT (w);
9227 }
9228
9229 /* If top line height has changed, arrange for a thorough
9230 immediate redisplay using the correct mode line height. */
9231 if (WINDOW_WANTS_HEADER_LINE_P (w)
9232 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
9233 {
9234 fonts_changed_p = 1;
9235 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
9236 = DESIRED_HEADER_LINE_HEIGHT (w);
9237 }
9238
9239 if (fonts_changed_p)
9240 goto restore_buffers;
9241 }
9242
9243 if (!line_number_displayed
9244 && !BUFFERP (w->base_line_pos))
9245 {
9246 w->base_line_pos = Qnil;
9247 w->base_line_number = Qnil;
9248 }
9249
9250 finish_menu_bars:
9251
9252 /* When we reach a frame's selected window, redo the frame's menu bar. */
9253 if (update_mode_line
9254 && EQ (FRAME_SELECTED_WINDOW (f), window))
9255 {
9256 int redisplay_menu_p = 0;
9257
9258 if (FRAME_WINDOW_P (f))
9259 {
9260 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
9261 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
9262 #else
9263 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
9264 #endif
9265 }
9266 else
9267 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
9268
9269 if (redisplay_menu_p)
9270 display_menu_bar (w);
9271
9272 #ifdef HAVE_WINDOW_SYSTEM
9273 if (WINDOWP (f->tool_bar_window)
9274 && (FRAME_TOOL_BAR_LINES (f) > 0
9275 || auto_resize_tool_bars_p))
9276 redisplay_tool_bar (f);
9277 #endif
9278 }
9279
9280 finish_scroll_bars:
9281
9282 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
9283 {
9284 int start, end, whole;
9285
9286 /* Calculate the start and end positions for the current window.
9287 At some point, it would be nice to choose between scrollbars
9288 which reflect the whole buffer size, with special markers
9289 indicating narrowing, and scrollbars which reflect only the
9290 visible region.
9291
9292 Note that mini-buffers sometimes aren't displaying any text. */
9293 if (!MINI_WINDOW_P (w)
9294 || (w == XWINDOW (minibuf_window)
9295 && NILP (echo_area_buffer[0])))
9296 {
9297 whole = ZV - BEGV;
9298 start = marker_position (w->start) - BEGV;
9299 /* I don't think this is guaranteed to be right. For the
9300 moment, we'll pretend it is. */
9301 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
9302
9303 if (end < start)
9304 end = start;
9305 if (whole < (end - start))
9306 whole = end - start;
9307 }
9308 else
9309 start = end = whole = 0;
9310
9311 /* Indicate what this scroll bar ought to be displaying now. */
9312 (*set_vertical_scroll_bar_hook) (w, end - start, whole, start);
9313
9314 /* Note that we actually used the scroll bar attached to this
9315 window, so it shouldn't be deleted at the end of redisplay. */
9316 (*redeem_scroll_bar_hook) (w);
9317 }
9318
9319 restore_buffers:
9320
9321 /* Restore current_buffer and value of point in it. */
9322 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
9323 if (really_switched_buffer)
9324 set_buffer_internal_1 (old);
9325 else
9326 set_buffer_temp (old);
9327 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
9328
9329 unbind_to (count, Qnil);
9330 }
9331
9332
9333 /* Build the complete desired matrix of WINDOW with a window start
9334 buffer position POS. Value is non-zero if successful. It is zero
9335 if fonts were loaded during redisplay which makes re-adjusting
9336 glyph matrices necessary. */
9337
9338 int
9339 try_window (window, pos)
9340 Lisp_Object window;
9341 struct text_pos pos;
9342 {
9343 struct window *w = XWINDOW (window);
9344 struct it it;
9345 struct glyph_row *last_text_row = NULL;
9346
9347 /* Make POS the new window start. */
9348 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
9349
9350 /* Mark cursor position as unknown. No overlay arrow seen. */
9351 w->cursor.vpos = -1;
9352 overlay_arrow_seen = 0;
9353
9354 /* Initialize iterator and info to start at POS. */
9355 start_display (&it, w, pos);
9356
9357 /* Display all lines of W. */
9358 while (it.current_y < it.last_visible_y)
9359 {
9360 if (display_line (&it))
9361 last_text_row = it.glyph_row - 1;
9362 if (fonts_changed_p)
9363 return 0;
9364 }
9365
9366 /* If bottom moved off end of frame, change mode line percentage. */
9367 if (XFASTINT (w->window_end_pos) <= 0
9368 && Z != IT_CHARPOS (it))
9369 w->update_mode_line = Qt;
9370
9371 /* Set window_end_pos to the offset of the last character displayed
9372 on the window from the end of current_buffer. Set
9373 window_end_vpos to its row number. */
9374 if (last_text_row)
9375 {
9376 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
9377 w->window_end_bytepos
9378 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
9379 XSETFASTINT (w->window_end_pos,
9380 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
9381 XSETFASTINT (w->window_end_vpos,
9382 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
9383 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
9384 ->displays_text_p);
9385 }
9386 else
9387 {
9388 w->window_end_bytepos = 0;
9389 XSETFASTINT (w->window_end_pos, 0);
9390 XSETFASTINT (w->window_end_vpos, 0);
9391 }
9392
9393 /* But that is not valid info until redisplay finishes. */
9394 w->window_end_valid = Qnil;
9395 return 1;
9396 }
9397
9398
9399 \f
9400 /************************************************************************
9401 Window redisplay reusing current matrix when buffer has not changed
9402 ************************************************************************/
9403
9404 /* Try redisplay of window W showing an unchanged buffer with a
9405 different window start than the last time it was displayed by
9406 reusing its current matrix. Value is non-zero if successful.
9407 W->start is the new window start. */
9408
9409 static int
9410 try_window_reusing_current_matrix (w)
9411 struct window *w;
9412 {
9413 struct frame *f = XFRAME (w->frame);
9414 struct glyph_row *row, *bottom_row;
9415 struct it it;
9416 struct run run;
9417 struct text_pos start, new_start;
9418 int nrows_scrolled, i;
9419 struct glyph_row *last_text_row;
9420 struct glyph_row *last_reused_text_row;
9421 struct glyph_row *start_row;
9422 int start_vpos, min_y, max_y;
9423
9424 /* Right now this function doesn't handle terminal frames. */
9425 if (!FRAME_WINDOW_P (f))
9426 return 0;
9427
9428 /* Can't do this if region may have changed. */
9429 if ((!NILP (Vtransient_mark_mode)
9430 && !NILP (current_buffer->mark_active))
9431 || !NILP (w->region_showing)
9432 || !NILP (Vshow_trailing_whitespace))
9433 return 0;
9434
9435 /* If top-line visibility has changed, give up. */
9436 if (WINDOW_WANTS_HEADER_LINE_P (w)
9437 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
9438 return 0;
9439
9440 /* Give up if old or new display is scrolled vertically. We could
9441 make this function handle this, but right now it doesn't. */
9442 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9443 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
9444 return 0;
9445
9446 /* The variable new_start now holds the new window start. The old
9447 start `start' can be determined from the current matrix. */
9448 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
9449 start = start_row->start.pos;
9450 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
9451
9452 /* Clear the desired matrix for the display below. */
9453 clear_glyph_matrix (w->desired_matrix);
9454
9455 if (CHARPOS (new_start) <= CHARPOS (start))
9456 {
9457 int first_row_y;
9458
9459 IF_DEBUG (debug_method_add (w, "twu1"));
9460
9461 /* Display up to a row that can be reused. The variable
9462 last_text_row is set to the last row displayed that displays
9463 text. */
9464 start_display (&it, w, new_start);
9465 first_row_y = it.current_y;
9466 w->cursor.vpos = -1;
9467 last_text_row = last_reused_text_row = NULL;
9468 while (it.current_y < it.last_visible_y
9469 && IT_CHARPOS (it) < CHARPOS (start)
9470 && !fonts_changed_p)
9471 if (display_line (&it))
9472 last_text_row = it.glyph_row - 1;
9473
9474 /* A value of current_y < last_visible_y means that we stopped
9475 at the previous window start, which in turn means that we
9476 have at least one reusable row. */
9477 if (it.current_y < it.last_visible_y)
9478 {
9479 nrows_scrolled = it.vpos;
9480
9481 /* Find PT if not already found in the lines displayed. */
9482 if (w->cursor.vpos < 0)
9483 {
9484 int dy = it.current_y - first_row_y;
9485
9486 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9487 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9488 {
9489 if (PT >= MATRIX_ROW_START_CHARPOS (row)
9490 && PT < MATRIX_ROW_END_CHARPOS (row))
9491 {
9492 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
9493 dy, nrows_scrolled);
9494 break;
9495 }
9496
9497 if (MATRIX_ROW_BOTTOM_Y (row) + dy >= it.last_visible_y)
9498 break;
9499
9500 ++row;
9501 }
9502
9503 /* Give up if point was not found. This shouldn't
9504 happen often; not more often than with try_window
9505 itself. */
9506 if (w->cursor.vpos < 0)
9507 {
9508 clear_glyph_matrix (w->desired_matrix);
9509 return 0;
9510 }
9511 }
9512
9513 /* Scroll the display. Do it before the current matrix is
9514 changed. The problem here is that update has not yet
9515 run, i.e. part of the current matrix is not up to date.
9516 scroll_run_hook will clear the cursor, and use the
9517 current matrix to get the height of the row the cursor is
9518 in. */
9519 run.current_y = first_row_y;
9520 run.desired_y = it.current_y;
9521 run.height = it.last_visible_y - it.current_y;
9522 if (run.height > 0
9523 && run.current_y != run.desired_y)
9524 {
9525 update_begin (f);
9526 rif->update_window_begin_hook (w);
9527 rif->scroll_run_hook (w, &run);
9528 rif->update_window_end_hook (w, 0);
9529 update_end (f);
9530 }
9531
9532 /* Shift current matrix down by nrows_scrolled lines. */
9533 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
9534 rotate_matrix (w->current_matrix,
9535 start_vpos,
9536 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
9537 nrows_scrolled);
9538
9539 /* Disable lines not reused. */
9540 for (i = 0; i < it.vpos; ++i)
9541 MATRIX_ROW (w->current_matrix, i)->enabled_p = 0;
9542
9543 /* Re-compute Y positions. */
9544 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix) + nrows_scrolled;
9545 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
9546 max_y = it.last_visible_y;
9547 while (row < bottom_row)
9548 {
9549 row->y = it.current_y;
9550
9551 if (row->y < min_y)
9552 row->visible_height = row->height - (min_y - row->y);
9553 else if (row->y + row->height > max_y)
9554 row->visible_height
9555 = row->height - (row->y + row->height - max_y);
9556 else
9557 row->visible_height = row->height;
9558
9559 it.current_y += row->height;
9560 ++it.vpos;
9561
9562 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9563 last_reused_text_row = row;
9564 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
9565 break;
9566 ++row;
9567 }
9568 }
9569
9570 /* Update window_end_pos etc.; last_reused_text_row is the last
9571 reused row from the current matrix containing text, if any.
9572 The value of last_text_row is the last displayed line
9573 containing text. */
9574 if (last_reused_text_row)
9575 {
9576 w->window_end_bytepos
9577 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
9578 XSETFASTINT (w->window_end_pos,
9579 Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
9580 XSETFASTINT (w->window_end_vpos,
9581 MATRIX_ROW_VPOS (last_reused_text_row,
9582 w->current_matrix));
9583 }
9584 else if (last_text_row)
9585 {
9586 w->window_end_bytepos
9587 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
9588 XSETFASTINT (w->window_end_pos,
9589 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
9590 XSETFASTINT (w->window_end_vpos,
9591 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
9592 }
9593 else
9594 {
9595 /* This window must be completely empty. */
9596 w->window_end_bytepos = 0;
9597 XSETFASTINT (w->window_end_pos, 0);
9598 XSETFASTINT (w->window_end_vpos, 0);
9599 }
9600 w->window_end_valid = Qnil;
9601
9602 /* Update hint: don't try scrolling again in update_window. */
9603 w->desired_matrix->no_scrolling_p = 1;
9604
9605 #if GLYPH_DEBUG
9606 debug_method_add (w, "try_window_reusing_current_matrix 1");
9607 #endif
9608 return 1;
9609 }
9610 else if (CHARPOS (new_start) > CHARPOS (start))
9611 {
9612 struct glyph_row *pt_row, *row;
9613 struct glyph_row *first_reusable_row;
9614 struct glyph_row *first_row_to_display;
9615 int dy;
9616 int yb = window_text_bottom_y (w);
9617
9618 IF_DEBUG (debug_method_add (w, "twu2"));
9619
9620 /* Find the row starting at new_start, if there is one. Don't
9621 reuse a partially visible line at the end. */
9622 first_reusable_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9623 while (first_reusable_row->enabled_p
9624 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
9625 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
9626 < CHARPOS (new_start)))
9627 ++first_reusable_row;
9628
9629 /* Give up if there is no row to reuse. */
9630 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
9631 || !first_reusable_row->enabled_p
9632 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
9633 != CHARPOS (new_start)))
9634 return 0;
9635
9636 /* We can reuse fully visible rows beginning with
9637 first_reusable_row to the end of the window. Set
9638 first_row_to_display to the first row that cannot be reused.
9639 Set pt_row to the row containing point, if there is any. */
9640 first_row_to_display = first_reusable_row;
9641 pt_row = NULL;
9642 while (MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb)
9643 {
9644 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
9645 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
9646 pt_row = first_row_to_display;
9647
9648 ++first_row_to_display;
9649 }
9650
9651 /* Start displaying at the start of first_row_to_display. */
9652 xassert (first_row_to_display->y < yb);
9653 init_to_row_start (&it, w, first_row_to_display);
9654 nrows_scrolled = MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix);
9655 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
9656 - nrows_scrolled);
9657 it.current_y = first_row_to_display->y - first_reusable_row->y;
9658
9659 /* Display lines beginning with first_row_to_display in the
9660 desired matrix. Set last_text_row to the last row displayed
9661 that displays text. */
9662 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
9663 if (pt_row == NULL)
9664 w->cursor.vpos = -1;
9665 last_text_row = NULL;
9666 while (it.current_y < it.last_visible_y && !fonts_changed_p)
9667 if (display_line (&it))
9668 last_text_row = it.glyph_row - 1;
9669
9670 /* Give up If point isn't in a row displayed or reused. */
9671 if (w->cursor.vpos < 0)
9672 {
9673 clear_glyph_matrix (w->desired_matrix);
9674 return 0;
9675 }
9676
9677 /* If point is in a reused row, adjust y and vpos of the cursor
9678 position. */
9679 if (pt_row)
9680 {
9681 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
9682 w->current_matrix);
9683 w->cursor.y -= first_reusable_row->y;
9684 }
9685
9686 /* Scroll the display. */
9687 run.current_y = first_reusable_row->y;
9688 run.desired_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
9689 run.height = it.last_visible_y - run.current_y;
9690 if (run.height)
9691 {
9692 struct frame *f = XFRAME (WINDOW_FRAME (w));
9693 update_begin (f);
9694 rif->update_window_begin_hook (w);
9695 rif->scroll_run_hook (w, &run);
9696 rif->update_window_end_hook (w, 0);
9697 update_end (f);
9698 }
9699
9700 /* Adjust Y positions of reused rows. */
9701 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
9702 row = first_reusable_row;
9703 dy = first_reusable_row->y;
9704 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
9705 max_y = it.last_visible_y;
9706 while (row < first_row_to_display)
9707 {
9708 row->y -= dy;
9709 if (row->y < min_y)
9710 row->visible_height = row->height - (min_y - row->y);
9711 else if (row->y + row->height > max_y)
9712 row->visible_height
9713 = row->height - (row->y + row->height - max_y);
9714 else
9715 row->visible_height = row->height;
9716 ++row;
9717 }
9718
9719 /* Disable rows not reused. */
9720 while (row < bottom_row)
9721 {
9722 row->enabled_p = 0;
9723 ++row;
9724 }
9725
9726 /* Scroll the current matrix. */
9727 xassert (nrows_scrolled > 0);
9728 rotate_matrix (w->current_matrix,
9729 start_vpos,
9730 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
9731 -nrows_scrolled);
9732
9733 /* Adjust window end. A null value of last_text_row means that
9734 the window end is in reused rows which in turn means that
9735 only its vpos can have changed. */
9736 if (last_text_row)
9737 {
9738 w->window_end_bytepos
9739 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
9740 XSETFASTINT (w->window_end_pos,
9741 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
9742 XSETFASTINT (w->window_end_vpos,
9743 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
9744 }
9745 else
9746 {
9747 XSETFASTINT (w->window_end_vpos,
9748 XFASTINT (w->window_end_vpos) - nrows_scrolled);
9749 }
9750
9751 w->window_end_valid = Qnil;
9752 w->desired_matrix->no_scrolling_p = 1;
9753
9754 #if GLYPH_DEBUG
9755 debug_method_add (w, "try_window_reusing_current_matrix 2");
9756 #endif
9757 return 1;
9758 }
9759
9760 return 0;
9761 }
9762
9763
9764 \f
9765 /************************************************************************
9766 Window redisplay reusing current matrix when buffer has changed
9767 ************************************************************************/
9768
9769 static struct glyph_row *get_last_unchanged_at_beg_row P_ ((struct window *));
9770 static struct glyph_row *get_first_unchanged_at_end_row P_ ((struct window *,
9771 int *, int *));
9772 static struct glyph_row *
9773 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
9774 struct glyph_row *));
9775
9776
9777 /* Return the last row in MATRIX displaying text. If row START is
9778 non-null, start searching with that row. IT gives the dimensions
9779 of the display. Value is null if matrix is empty; otherwise it is
9780 a pointer to the row found. */
9781
9782 static struct glyph_row *
9783 find_last_row_displaying_text (matrix, it, start)
9784 struct glyph_matrix *matrix;
9785 struct it *it;
9786 struct glyph_row *start;
9787 {
9788 struct glyph_row *row, *row_found;
9789
9790 /* Set row_found to the last row in IT->w's current matrix
9791 displaying text. The loop looks funny but think of partially
9792 visible lines. */
9793 row_found = NULL;
9794 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
9795 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9796 {
9797 xassert (row->enabled_p);
9798 row_found = row;
9799 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
9800 break;
9801 ++row;
9802 }
9803
9804 return row_found;
9805 }
9806
9807
9808 /* Return the last row in the current matrix of W that is not affected
9809 by changes at the start of current_buffer that occurred since the
9810 last time W was redisplayed. Value is null if no such row exists.
9811
9812 The global variable beg_unchanged has to contain the number of
9813 bytes unchanged at the start of current_buffer. BEG +
9814 beg_unchanged is the buffer position of the first changed byte in
9815 current_buffer. Characters at positions < BEG + beg_unchanged are
9816 at the same buffer positions as they were when the current matrix
9817 was built. */
9818
9819 static struct glyph_row *
9820 get_last_unchanged_at_beg_row (w)
9821 struct window *w;
9822 {
9823 int first_changed_pos = BEG + BEG_UNCHANGED;
9824 struct glyph_row *row;
9825 struct glyph_row *row_found = NULL;
9826 int yb = window_text_bottom_y (w);
9827
9828 /* Find the last row displaying unchanged text. */
9829 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9830 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
9831 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
9832 {
9833 if (/* If row ends before first_changed_pos, it is unchanged,
9834 except in some case. */
9835 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
9836 /* When row ends in ZV and we write at ZV it is not
9837 unchanged. */
9838 && !row->ends_at_zv_p
9839 /* When first_changed_pos is the end of a continued line,
9840 row is not unchanged because it may be no longer
9841 continued. */
9842 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
9843 && row->continued_p))
9844 row_found = row;
9845
9846 /* Stop if last visible row. */
9847 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
9848 break;
9849
9850 ++row;
9851 }
9852
9853 return row_found;
9854 }
9855
9856
9857 /* Find the first glyph row in the current matrix of W that is not
9858 affected by changes at the end of current_buffer since the last
9859 time the window was redisplayed. Return in *DELTA the number of
9860 chars by which buffer positions in unchanged text at the end of
9861 current_buffer must be adjusted. Return in *DELTA_BYTES the
9862 corresponding number of bytes. Value is null if no such row
9863 exists, i.e. all rows are affected by changes. */
9864
9865 static struct glyph_row *
9866 get_first_unchanged_at_end_row (w, delta, delta_bytes)
9867 struct window *w;
9868 int *delta, *delta_bytes;
9869 {
9870 struct glyph_row *row;
9871 struct glyph_row *row_found = NULL;
9872
9873 *delta = *delta_bytes = 0;
9874
9875 /* A value of window_end_pos >= end_unchanged means that the window
9876 end is in the range of changed text. If so, there is no
9877 unchanged row at the end of W's current matrix. */
9878 xassert (!NILP (w->window_end_valid));
9879 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
9880 return NULL;
9881
9882 /* Set row to the last row in W's current matrix displaying text. */
9883 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
9884
9885 /* If matrix is entirely empty, no unchanged row exists. */
9886 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9887 {
9888 /* The value of row is the last glyph row in the matrix having a
9889 meaningful buffer position in it. The end position of row
9890 corresponds to window_end_pos. This allows us to translate
9891 buffer positions in the current matrix to current buffer
9892 positions for characters not in changed text. */
9893 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
9894 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
9895 int last_unchanged_pos, last_unchanged_pos_old;
9896 struct glyph_row *first_text_row
9897 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9898
9899 *delta = Z - Z_old;
9900 *delta_bytes = Z_BYTE - Z_BYTE_old;
9901
9902 /* Set last_unchanged_pos to the buffer position of the last
9903 character in the buffer that has not been changed. Z is the
9904 index + 1 of the last byte in current_buffer, i.e. by
9905 subtracting end_unchanged we get the index of the last
9906 unchanged character, and we have to add BEG to get its buffer
9907 position. */
9908 last_unchanged_pos = Z - END_UNCHANGED + BEG;
9909 last_unchanged_pos_old = last_unchanged_pos - *delta;
9910
9911 /* Search backward from ROW for a row displaying a line that
9912 starts at a minimum position >= last_unchanged_pos_old. */
9913 while (row >= first_text_row)
9914 {
9915 xassert (row->enabled_p);
9916 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (row));
9917
9918 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
9919 row_found = row;
9920 --row;
9921 }
9922 }
9923
9924 xassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
9925 return row_found;
9926 }
9927
9928
9929 /* Make sure that glyph rows in the current matrix of window W
9930 reference the same glyph memory as corresponding rows in the
9931 frame's frame matrix. This function is called after scrolling W's
9932 current matrix on a terminal frame in try_window_id and
9933 try_window_reusing_current_matrix. */
9934
9935 static void
9936 sync_frame_with_window_matrix_rows (w)
9937 struct window *w;
9938 {
9939 struct frame *f = XFRAME (w->frame);
9940 struct glyph_row *window_row, *window_row_end, *frame_row;
9941
9942 /* Preconditions: W must be a leaf window and full-width. Its frame
9943 must have a frame matrix. */
9944 xassert (NILP (w->hchild) && NILP (w->vchild));
9945 xassert (WINDOW_FULL_WIDTH_P (w));
9946 xassert (!FRAME_WINDOW_P (f));
9947
9948 /* If W is a full-width window, glyph pointers in W's current matrix
9949 have, by definition, to be the same as glyph pointers in the
9950 corresponding frame matrix. */
9951 window_row = w->current_matrix->rows;
9952 window_row_end = window_row + w->current_matrix->nrows;
9953 frame_row = f->current_matrix->rows + XFASTINT (w->top);
9954 while (window_row < window_row_end)
9955 {
9956 int area;
9957
9958 for (area = LEFT_MARGIN_AREA; area <= LAST_AREA; ++area)
9959 frame_row->glyphs[area] = window_row->glyphs[area];
9960
9961 /* Disable frame rows whose corresponding window rows have
9962 been disabled in try_window_id. */
9963 if (!window_row->enabled_p)
9964 frame_row->enabled_p = 0;
9965
9966 ++window_row, ++frame_row;
9967 }
9968 }
9969
9970
9971 /* Find the glyph row in window W containing CHARPOS. Consider all
9972 rows between START and END (not inclusive). END null means search
9973 all rows to the end of the display area of W. Value is the row
9974 containing CHARPOS or null. */
9975
9976 static struct glyph_row *
9977 row_containing_pos (w, charpos, start, end)
9978 struct window *w;
9979 int charpos;
9980 struct glyph_row *start, *end;
9981 {
9982 struct glyph_row *row = start;
9983 int last_y;
9984
9985 /* If we happen to start on a header-line, skip that. */
9986 if (row->mode_line_p)
9987 ++row;
9988
9989 if ((end && row >= end) || !row->enabled_p)
9990 return NULL;
9991
9992 last_y = window_text_bottom_y (w);
9993
9994 while ((end == NULL || row < end)
9995 && (MATRIX_ROW_END_CHARPOS (row) < charpos
9996 /* The end position of a row equals the start
9997 position of the next row. If CHARPOS is there, we
9998 would rather display it in the next line, except
9999 when this line ends in ZV. */
10000 || (MATRIX_ROW_END_CHARPOS (row) == charpos
10001 && (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
10002 || !row->ends_at_zv_p)))
10003 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
10004 ++row;
10005
10006 /* Give up if CHARPOS not found. */
10007 if ((end && row >= end)
10008 || charpos < MATRIX_ROW_START_CHARPOS (row)
10009 || charpos > MATRIX_ROW_END_CHARPOS (row))
10010 row = NULL;
10011
10012 return row;
10013 }
10014
10015
10016 /* Try to redisplay window W by reusing its existing display. W's
10017 current matrix must be up to date when this function is called,
10018 i.e. window_end_valid must not be nil.
10019
10020 Value is
10021
10022 1 if display has been updated
10023 0 if otherwise unsuccessful
10024 -1 if redisplay with same window start is known not to succeed
10025
10026 The following steps are performed:
10027
10028 1. Find the last row in the current matrix of W that is not
10029 affected by changes at the start of current_buffer. If no such row
10030 is found, give up.
10031
10032 2. Find the first row in W's current matrix that is not affected by
10033 changes at the end of current_buffer. Maybe there is no such row.
10034
10035 3. Display lines beginning with the row + 1 found in step 1 to the
10036 row found in step 2 or, if step 2 didn't find a row, to the end of
10037 the window.
10038
10039 4. If cursor is not known to appear on the window, give up.
10040
10041 5. If display stopped at the row found in step 2, scroll the
10042 display and current matrix as needed.
10043
10044 6. Maybe display some lines at the end of W, if we must. This can
10045 happen under various circumstances, like a partially visible line
10046 becoming fully visible, or because newly displayed lines are displayed
10047 in smaller font sizes.
10048
10049 7. Update W's window end information. */
10050
10051 /* Check that window end is what we expect it to be. */
10052
10053 static int
10054 try_window_id (w)
10055 struct window *w;
10056 {
10057 struct frame *f = XFRAME (w->frame);
10058 struct glyph_matrix *current_matrix = w->current_matrix;
10059 struct glyph_matrix *desired_matrix = w->desired_matrix;
10060 struct glyph_row *last_unchanged_at_beg_row;
10061 struct glyph_row *first_unchanged_at_end_row;
10062 struct glyph_row *row;
10063 struct glyph_row *bottom_row;
10064 int bottom_vpos;
10065 struct it it;
10066 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
10067 struct text_pos start_pos;
10068 struct run run;
10069 int first_unchanged_at_end_vpos = 0;
10070 struct glyph_row *last_text_row, *last_text_row_at_end;
10071 struct text_pos start;
10072
10073 SET_TEXT_POS_FROM_MARKER (start, w->start);
10074
10075 /* Check pre-conditions. Window end must be valid, otherwise
10076 the current matrix would not be up to date. */
10077 xassert (!NILP (w->window_end_valid));
10078 xassert (FRAME_WINDOW_P (XFRAME (w->frame))
10079 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)));
10080
10081 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
10082 only if buffer has really changed. The reason is that the gap is
10083 initially at Z for freshly visited files. The code below would
10084 set end_unchanged to 0 in that case. */
10085 if (MODIFF > SAVE_MODIFF
10086 /* This seems to happen sometimes after saving a buffer. */
10087 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
10088 {
10089 if (GPT - BEG < BEG_UNCHANGED)
10090 BEG_UNCHANGED = GPT - BEG;
10091 if (Z - GPT < END_UNCHANGED)
10092 END_UNCHANGED = Z - GPT;
10093 }
10094
10095 /* If window starts after a line end, and the last change is in
10096 front of that newline, then changes don't affect the display.
10097 This case happens with stealth-fontification. Note that although
10098 the display is unchanged, glyph positions in the matrix have to
10099 be adjusted, of course. */
10100 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
10101 if (CHARPOS (start) > BEGV
10102 && Z - END_UNCHANGED < CHARPOS (start) - 1
10103 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n'
10104 && PT < MATRIX_ROW_END_CHARPOS (row))
10105 {
10106 struct glyph_row *r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
10107 int delta = CHARPOS (start) - MATRIX_ROW_START_CHARPOS (r0);
10108
10109 if (delta)
10110 {
10111 struct glyph_row *r1 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
10112 int delta_bytes = BYTEPOS (start) - MATRIX_ROW_START_BYTEPOS (r0);
10113
10114 increment_matrix_positions (w->current_matrix,
10115 MATRIX_ROW_VPOS (r0, current_matrix),
10116 MATRIX_ROW_VPOS (r1, current_matrix),
10117 delta, delta_bytes);
10118 }
10119
10120 #if 0 /* If changes are all in front of the window start, the
10121 distance of the last displayed glyph from Z hasn't
10122 changed. */
10123 w->window_end_pos
10124 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
10125 w->window_end_bytepos
10126 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
10127 #endif
10128
10129 return 1;
10130 }
10131
10132 /* Return quickly if changes are all below what is displayed in the
10133 window, and if PT is in the window. */
10134 if (BEG_UNCHANGED > MATRIX_ROW_END_CHARPOS (row)
10135 && PT < MATRIX_ROW_END_CHARPOS (row))
10136 {
10137 /* We have to update window end positions because the buffer's
10138 size has changed. */
10139 w->window_end_pos
10140 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
10141 w->window_end_bytepos
10142 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
10143 return 1;
10144 }
10145
10146 /* Check that window start agrees with the start of the first glyph
10147 row in its current matrix. Check this after we know the window
10148 start is not in changed text, otherwise positions would not be
10149 comparable. */
10150 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10151 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
10152 return 0;
10153
10154 /* Compute the position at which we have to start displaying new
10155 lines. Some of the lines at the top of the window might be
10156 reusable because they are not displaying changed text. Find the
10157 last row in W's current matrix not affected by changes at the
10158 start of current_buffer. Value is null if changes start in the
10159 first line of window. */
10160 last_unchanged_at_beg_row = get_last_unchanged_at_beg_row (w);
10161 if (last_unchanged_at_beg_row)
10162 {
10163 init_to_row_end (&it, w, last_unchanged_at_beg_row);
10164 start_pos = it.current.pos;
10165
10166 /* Start displaying new lines in the desired matrix at the same
10167 vpos we would use in the current matrix, i.e. below
10168 last_unchanged_at_beg_row. */
10169 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
10170 current_matrix);
10171 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
10172 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
10173
10174 xassert (it.hpos == 0 && it.current_x == 0);
10175 }
10176 else
10177 {
10178 /* There are no reusable lines at the start of the window.
10179 Start displaying in the first line. */
10180 start_display (&it, w, start);
10181 start_pos = it.current.pos;
10182 }
10183
10184 /* Find the first row that is not affected by changes at the end of
10185 the buffer. Value will be null if there is no unchanged row, in
10186 which case we must redisplay to the end of the window. delta
10187 will be set to the value by which buffer positions beginning with
10188 first_unchanged_at_end_row have to be adjusted due to text
10189 changes. */
10190 first_unchanged_at_end_row
10191 = get_first_unchanged_at_end_row (w, &delta, &delta_bytes);
10192 IF_DEBUG (debug_delta = delta);
10193 IF_DEBUG (debug_delta_bytes = delta_bytes);
10194
10195 /* Set stop_pos to the buffer position up to which we will have to
10196 display new lines. If first_unchanged_at_end_row != NULL, this
10197 is the buffer position of the start of the line displayed in that
10198 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
10199 that we don't stop at a buffer position. */
10200 stop_pos = 0;
10201 if (first_unchanged_at_end_row)
10202 {
10203 xassert (last_unchanged_at_beg_row == NULL
10204 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
10205
10206 /* If this is a continuation line, move forward to the next one
10207 that isn't. Changes in lines above affect this line.
10208 Caution: this may move first_unchanged_at_end_row to a row
10209 not displaying text. */
10210 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
10211 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
10212 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
10213 < it.last_visible_y))
10214 ++first_unchanged_at_end_row;
10215
10216 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
10217 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
10218 >= it.last_visible_y))
10219 first_unchanged_at_end_row = NULL;
10220 else
10221 {
10222 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
10223 + delta);
10224 first_unchanged_at_end_vpos
10225 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
10226 xassert (stop_pos >= Z - END_UNCHANGED);
10227 }
10228 }
10229 else if (last_unchanged_at_beg_row == NULL)
10230 return 0;
10231
10232
10233 #if GLYPH_DEBUG
10234
10235 /* Either there is no unchanged row at the end, or the one we have
10236 now displays text. This is a necessary condition for the window
10237 end pos calculation at the end of this function. */
10238 xassert (first_unchanged_at_end_row == NULL
10239 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
10240
10241 debug_last_unchanged_at_beg_vpos
10242 = (last_unchanged_at_beg_row
10243 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
10244 : -1);
10245 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
10246
10247 #endif /* GLYPH_DEBUG != 0 */
10248
10249
10250 /* Display new lines. Set last_text_row to the last new line
10251 displayed which has text on it, i.e. might end up as being the
10252 line where the window_end_vpos is. */
10253 w->cursor.vpos = -1;
10254 last_text_row = NULL;
10255 overlay_arrow_seen = 0;
10256 while (it.current_y < it.last_visible_y
10257 && !fonts_changed_p
10258 && (first_unchanged_at_end_row == NULL
10259 || IT_CHARPOS (it) < stop_pos))
10260 {
10261 if (display_line (&it))
10262 last_text_row = it.glyph_row - 1;
10263 }
10264
10265 if (fonts_changed_p)
10266 return -1;
10267
10268
10269 /* Compute differences in buffer positions, y-positions etc. for
10270 lines reused at the bottom of the window. Compute what we can
10271 scroll. */
10272 if (first_unchanged_at_end_row
10273 /* No lines reused because we displayed everything up to the
10274 bottom of the window. */
10275 && it.current_y < it.last_visible_y)
10276 {
10277 dvpos = (it.vpos
10278 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
10279 current_matrix));
10280 dy = it.current_y - first_unchanged_at_end_row->y;
10281 run.current_y = first_unchanged_at_end_row->y;
10282 run.desired_y = run.current_y + dy;
10283 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
10284 }
10285 else
10286 {
10287 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
10288 first_unchanged_at_end_row = NULL;
10289 }
10290 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
10291
10292
10293 /* Find the cursor if not already found. We have to decide whether
10294 PT will appear on this window (it sometimes doesn't, but this is
10295 not a very frequent case.) This decision has to be made before
10296 the current matrix is altered. A value of cursor.vpos < 0 means
10297 that PT is either in one of the lines beginning at
10298 first_unchanged_at_end_row or below the window. Don't care for
10299 lines that might be displayed later at the window end; as
10300 mentioned, this is not a frequent case. */
10301 if (w->cursor.vpos < 0)
10302 {
10303 /* Cursor in unchanged rows at the top? */
10304 if (PT < CHARPOS (start_pos)
10305 && last_unchanged_at_beg_row)
10306 {
10307 row = row_containing_pos (w, PT,
10308 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
10309 last_unchanged_at_beg_row + 1);
10310 xassert (row && row <= last_unchanged_at_beg_row);
10311 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10312 }
10313
10314 /* Start from first_unchanged_at_end_row looking for PT. */
10315 else if (first_unchanged_at_end_row)
10316 {
10317 row = row_containing_pos (w, PT - delta,
10318 first_unchanged_at_end_row, NULL);
10319 if (row)
10320 set_cursor_from_row (w, row, w->current_matrix, delta,
10321 delta_bytes, dy, dvpos);
10322 }
10323
10324 /* Give up if cursor was not found. */
10325 if (w->cursor.vpos < 0)
10326 {
10327 clear_glyph_matrix (w->desired_matrix);
10328 return -1;
10329 }
10330 }
10331
10332 /* Don't let the cursor end in the scroll margins. */
10333 {
10334 int this_scroll_margin, cursor_height;
10335
10336 this_scroll_margin = max (0, scroll_margin);
10337 this_scroll_margin = min (this_scroll_margin,
10338 XFASTINT (w->height) / 4);
10339 this_scroll_margin *= CANON_Y_UNIT (it.f);
10340 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
10341
10342 if ((w->cursor.y < this_scroll_margin
10343 && CHARPOS (start) > BEGV)
10344 /* Don't take scroll margin into account at the bottom because
10345 old redisplay didn't do it either. */
10346 || w->cursor.y + cursor_height > it.last_visible_y)
10347 {
10348 w->cursor.vpos = -1;
10349 clear_glyph_matrix (w->desired_matrix);
10350 return -1;
10351 }
10352 }
10353
10354 /* Scroll the display. Do it before changing the current matrix so
10355 that xterm.c doesn't get confused about where the cursor glyph is
10356 found. */
10357 if (dy && run.height)
10358 {
10359 update_begin (f);
10360
10361 if (FRAME_WINDOW_P (f))
10362 {
10363 rif->update_window_begin_hook (w);
10364 rif->scroll_run_hook (w, &run);
10365 rif->update_window_end_hook (w, 0);
10366 }
10367 else
10368 {
10369 /* Terminal frame. In this case, dvpos gives the number of
10370 lines to scroll by; dvpos < 0 means scroll up. */
10371 int first_unchanged_at_end_vpos
10372 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
10373 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
10374 int end = XFASTINT (w->top) + window_internal_height (w);
10375
10376 /* Perform the operation on the screen. */
10377 if (dvpos > 0)
10378 {
10379 /* Scroll last_unchanged_at_beg_row to the end of the
10380 window down dvpos lines. */
10381 set_terminal_window (end);
10382
10383 /* On dumb terminals delete dvpos lines at the end
10384 before inserting dvpos empty lines. */
10385 if (!scroll_region_ok)
10386 ins_del_lines (end - dvpos, -dvpos);
10387
10388 /* Insert dvpos empty lines in front of
10389 last_unchanged_at_beg_row. */
10390 ins_del_lines (from, dvpos);
10391 }
10392 else if (dvpos < 0)
10393 {
10394 /* Scroll up last_unchanged_at_beg_vpos to the end of
10395 the window to last_unchanged_at_beg_vpos - |dvpos|. */
10396 set_terminal_window (end);
10397
10398 /* Delete dvpos lines in front of
10399 last_unchanged_at_beg_vpos. ins_del_lines will set
10400 the cursor to the given vpos and emit |dvpos| delete
10401 line sequences. */
10402 ins_del_lines (from + dvpos, dvpos);
10403
10404 /* On a dumb terminal insert dvpos empty lines at the
10405 end. */
10406 if (!scroll_region_ok)
10407 ins_del_lines (end + dvpos, -dvpos);
10408 }
10409
10410 set_terminal_window (0);
10411 }
10412
10413 update_end (f);
10414 }
10415
10416 /* Shift reused rows of the current matrix to the right position.
10417 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
10418 text. */
10419 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
10420 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
10421 if (dvpos < 0)
10422 {
10423 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
10424 bottom_vpos, dvpos);
10425 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
10426 bottom_vpos, 0);
10427 }
10428 else if (dvpos > 0)
10429 {
10430 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
10431 bottom_vpos, dvpos);
10432 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
10433 first_unchanged_at_end_vpos + dvpos, 0);
10434 }
10435
10436 /* For frame-based redisplay, make sure that current frame and window
10437 matrix are in sync with respect to glyph memory. */
10438 if (!FRAME_WINDOW_P (f))
10439 sync_frame_with_window_matrix_rows (w);
10440
10441 /* Adjust buffer positions in reused rows. */
10442 if (delta)
10443 increment_matrix_positions (current_matrix,
10444 first_unchanged_at_end_vpos + dvpos,
10445 bottom_vpos, delta, delta_bytes);
10446
10447 /* Adjust Y positions. */
10448 if (dy)
10449 shift_glyph_matrix (w, current_matrix,
10450 first_unchanged_at_end_vpos + dvpos,
10451 bottom_vpos, dy);
10452
10453 if (first_unchanged_at_end_row)
10454 first_unchanged_at_end_row += dvpos;
10455
10456 /* If scrolling up, there may be some lines to display at the end of
10457 the window. */
10458 last_text_row_at_end = NULL;
10459 if (dy < 0)
10460 {
10461 /* Set last_row to the glyph row in the current matrix where the
10462 window end line is found. It has been moved up or down in
10463 the matrix by dvpos. */
10464 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
10465 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
10466
10467 /* If last_row is the window end line, it should display text. */
10468 xassert (last_row->displays_text_p);
10469
10470 /* If window end line was partially visible before, begin
10471 displaying at that line. Otherwise begin displaying with the
10472 line following it. */
10473 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
10474 {
10475 init_to_row_start (&it, w, last_row);
10476 it.vpos = last_vpos;
10477 it.current_y = last_row->y;
10478 }
10479 else
10480 {
10481 init_to_row_end (&it, w, last_row);
10482 it.vpos = 1 + last_vpos;
10483 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
10484 ++last_row;
10485 }
10486
10487 /* We may start in a continuation line. If so, we have to get
10488 the right continuation_lines_width and current_x. */
10489 it.continuation_lines_width = last_row->continuation_lines_width;
10490 it.hpos = it.current_x = 0;
10491
10492 /* Display the rest of the lines at the window end. */
10493 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
10494 while (it.current_y < it.last_visible_y
10495 && !fonts_changed_p)
10496 {
10497 /* Is it always sure that the display agrees with lines in
10498 the current matrix? I don't think so, so we mark rows
10499 displayed invalid in the current matrix by setting their
10500 enabled_p flag to zero. */
10501 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
10502 if (display_line (&it))
10503 last_text_row_at_end = it.glyph_row - 1;
10504 }
10505 }
10506
10507 /* Update window_end_pos and window_end_vpos. */
10508 if (first_unchanged_at_end_row
10509 && first_unchanged_at_end_row->y < it.last_visible_y
10510 && !last_text_row_at_end)
10511 {
10512 /* Window end line if one of the preserved rows from the current
10513 matrix. Set row to the last row displaying text in current
10514 matrix starting at first_unchanged_at_end_row, after
10515 scrolling. */
10516 xassert (first_unchanged_at_end_row->displays_text_p);
10517 row = find_last_row_displaying_text (w->current_matrix, &it,
10518 first_unchanged_at_end_row);
10519 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
10520
10521 XSETFASTINT (w->window_end_pos, Z - MATRIX_ROW_END_CHARPOS (row));
10522 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
10523 XSETFASTINT (w->window_end_vpos,
10524 MATRIX_ROW_VPOS (row, w->current_matrix));
10525 }
10526 else if (last_text_row_at_end)
10527 {
10528 XSETFASTINT (w->window_end_pos,
10529 Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
10530 w->window_end_bytepos
10531 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
10532 XSETFASTINT (w->window_end_vpos,
10533 MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
10534 }
10535 else if (last_text_row)
10536 {
10537 /* We have displayed either to the end of the window or at the
10538 end of the window, i.e. the last row with text is to be found
10539 in the desired matrix. */
10540 XSETFASTINT (w->window_end_pos,
10541 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10542 w->window_end_bytepos
10543 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10544 XSETFASTINT (w->window_end_vpos,
10545 MATRIX_ROW_VPOS (last_text_row, desired_matrix));
10546 }
10547 else if (first_unchanged_at_end_row == NULL
10548 && last_text_row == NULL
10549 && last_text_row_at_end == NULL)
10550 {
10551 /* Displayed to end of window, but no line containing text was
10552 displayed. Lines were deleted at the end of the window. */
10553 int vpos;
10554 int header_line_p = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
10555
10556 for (vpos = XFASTINT (w->window_end_vpos); vpos > 0; --vpos)
10557 if ((w->desired_matrix->rows[vpos + header_line_p].enabled_p
10558 && w->desired_matrix->rows[vpos + header_line_p].displays_text_p)
10559 || (!w->desired_matrix->rows[vpos + header_line_p].enabled_p
10560 && w->current_matrix->rows[vpos + header_line_p].displays_text_p))
10561 break;
10562
10563 w->window_end_vpos = make_number (vpos);
10564 }
10565 else
10566 abort ();
10567
10568 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
10569 debug_end_vpos = XFASTINT (w->window_end_vpos));
10570
10571 /* Record that display has not been completed. */
10572 w->window_end_valid = Qnil;
10573 w->desired_matrix->no_scrolling_p = 1;
10574 return 1;
10575 }
10576
10577
10578 \f
10579 /***********************************************************************
10580 More debugging support
10581 ***********************************************************************/
10582
10583 #if GLYPH_DEBUG
10584
10585 void dump_glyph_row P_ ((struct glyph_matrix *, int, int));
10586 static void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
10587
10588
10589 /* Dump the contents of glyph matrix MATRIX on stderr. If
10590 WITH_GLYPHS_P is non-zero, dump glyph contents as well. */
10591
10592 void
10593 dump_glyph_matrix (matrix, with_glyphs_p)
10594 struct glyph_matrix *matrix;
10595 int with_glyphs_p;
10596 {
10597 int i;
10598 for (i = 0; i < matrix->nrows; ++i)
10599 dump_glyph_row (matrix, i, with_glyphs_p);
10600 }
10601
10602
10603 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
10604 WITH_GLYPH_SP non-zero means dump glyph contents, too. */
10605
10606 void
10607 dump_glyph_row (matrix, vpos, with_glyphs_p)
10608 struct glyph_matrix *matrix;
10609 int vpos, with_glyphs_p;
10610 {
10611 struct glyph_row *row;
10612
10613 if (vpos < 0 || vpos >= matrix->nrows)
10614 return;
10615
10616 row = MATRIX_ROW (matrix, vpos);
10617
10618 fprintf (stderr, "Row Start End Used oEI><O\\CTZF X Y W\n");
10619 fprintf (stderr, "=============================================\n");
10620
10621 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",
10622 row - matrix->rows,
10623 MATRIX_ROW_START_CHARPOS (row),
10624 MATRIX_ROW_END_CHARPOS (row),
10625 row->used[TEXT_AREA],
10626 row->contains_overlapping_glyphs_p,
10627 row->enabled_p,
10628 row->inverse_p,
10629 row->truncated_on_left_p,
10630 row->truncated_on_right_p,
10631 row->overlay_arrow_p,
10632 row->continued_p,
10633 MATRIX_ROW_CONTINUATION_LINE_P (row),
10634 row->displays_text_p,
10635 row->ends_at_zv_p,
10636 row->fill_line_p,
10637 row->x,
10638 row->y,
10639 row->pixel_width);
10640 fprintf (stderr, "%9d %5d\n", row->start.overlay_string_index,
10641 row->end.overlay_string_index);
10642 fprintf (stderr, "%9d %5d\n",
10643 CHARPOS (row->start.string_pos),
10644 CHARPOS (row->end.string_pos));
10645 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
10646 row->end.dpvec_index);
10647
10648 if (with_glyphs_p)
10649 {
10650 struct glyph *glyph, *glyph_end;
10651 int prev_had_glyphs_p;
10652
10653 glyph = row->glyphs[TEXT_AREA];
10654 glyph_end = glyph + row->used[TEXT_AREA];
10655
10656 /* Glyph for a line end in text. */
10657 if (glyph == glyph_end && glyph->charpos > 0)
10658 ++glyph_end;
10659
10660 if (glyph < glyph_end)
10661 {
10662 fprintf (stderr, " Glyph Type Pos W Code C Face LR\n");
10663 prev_had_glyphs_p = 1;
10664 }
10665 else
10666 prev_had_glyphs_p = 0;
10667
10668 while (glyph < glyph_end)
10669 {
10670 if (glyph->type == CHAR_GLYPH)
10671 {
10672 fprintf (stderr,
10673 " %5d %4c %6d %3d 0x%05x %c %4d %1.1d%1.1d\n",
10674 glyph - row->glyphs[TEXT_AREA],
10675 'C',
10676 glyph->charpos,
10677 glyph->pixel_width,
10678 glyph->u.ch,
10679 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
10680 ? glyph->u.ch
10681 : '.'),
10682 glyph->face_id,
10683 glyph->left_box_line_p,
10684 glyph->right_box_line_p);
10685 }
10686 else if (glyph->type == STRETCH_GLYPH)
10687 {
10688 fprintf (stderr,
10689 " %5d %4c %6d %3d 0x%05x %c %4d %1.1d%1.1d\n",
10690 glyph - row->glyphs[TEXT_AREA],
10691 'S',
10692 glyph->charpos,
10693 glyph->pixel_width,
10694 0,
10695 '.',
10696 glyph->face_id,
10697 glyph->left_box_line_p,
10698 glyph->right_box_line_p);
10699 }
10700 else if (glyph->type == IMAGE_GLYPH)
10701 {
10702 fprintf (stderr,
10703 " %5d %4c %6d %3d 0x%05x %c %4d %1.1d%1.1d\n",
10704 glyph - row->glyphs[TEXT_AREA],
10705 'I',
10706 glyph->charpos,
10707 glyph->pixel_width,
10708 glyph->u.img_id,
10709 '.',
10710 glyph->face_id,
10711 glyph->left_box_line_p,
10712 glyph->right_box_line_p);
10713 }
10714 ++glyph;
10715 }
10716 }
10717 }
10718
10719
10720 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
10721 Sdump_glyph_matrix, 0, 1, "p",
10722 "Dump the current matrix of the selected window to stderr.\n\
10723 Shows contents of glyph row structures. With non-nil optional\n\
10724 parameter WITH-GLYPHS-P, dump glyphs as well.")
10725 (with_glyphs_p)
10726 Lisp_Object with_glyphs_p;
10727 {
10728 struct window *w = XWINDOW (selected_window);
10729 struct buffer *buffer = XBUFFER (w->buffer);
10730
10731 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
10732 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
10733 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
10734 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
10735 fprintf (stderr, "=============================================\n");
10736 dump_glyph_matrix (w->current_matrix, !NILP (with_glyphs_p));
10737 return Qnil;
10738 }
10739
10740
10741 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 1, "",
10742 "Dump glyph row ROW to stderr.")
10743 (row)
10744 Lisp_Object row;
10745 {
10746 CHECK_NUMBER (row, 0);
10747 dump_glyph_row (XWINDOW (selected_window)->current_matrix, XINT (row), 1);
10748 return Qnil;
10749 }
10750
10751
10752 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row,
10753 0, 0, "", "")
10754 ()
10755 {
10756 struct frame *sf = SELECTED_FRAME ();
10757 struct glyph_matrix *m = (XWINDOW (sf->tool_bar_window)
10758 ->current_matrix);
10759 dump_glyph_row (m, 0, 1);
10760 return Qnil;
10761 }
10762
10763
10764 DEFUN ("trace-redisplay-toggle", Ftrace_redisplay_toggle,
10765 Strace_redisplay_toggle, 0, 0, "",
10766 "Toggle tracing of redisplay.")
10767 ()
10768 {
10769 trace_redisplay_p = !trace_redisplay_p;
10770 return Qnil;
10771 }
10772
10773
10774 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, 1, "",
10775 "Print STRING to stderr.")
10776 (string)
10777 Lisp_Object string;
10778 {
10779 CHECK_STRING (string, 0);
10780 fprintf (stderr, "%s", XSTRING (string)->data);
10781 return Qnil;
10782 }
10783
10784 #endif /* GLYPH_DEBUG */
10785
10786
10787 \f
10788 /***********************************************************************
10789 Building Desired Matrix Rows
10790 ***********************************************************************/
10791
10792 /* Return a temporary glyph row holding the glyphs of an overlay
10793 arrow. Only used for non-window-redisplay windows. */
10794
10795 static struct glyph_row *
10796 get_overlay_arrow_glyph_row (w)
10797 struct window *w;
10798 {
10799 struct frame *f = XFRAME (WINDOW_FRAME (w));
10800 struct buffer *buffer = XBUFFER (w->buffer);
10801 struct buffer *old = current_buffer;
10802 unsigned char *arrow_string = XSTRING (Voverlay_arrow_string)->data;
10803 int arrow_len = XSTRING (Voverlay_arrow_string)->size;
10804 unsigned char *arrow_end = arrow_string + arrow_len;
10805 unsigned char *p;
10806 struct it it;
10807 int multibyte_p;
10808 int n_glyphs_before;
10809
10810 set_buffer_temp (buffer);
10811 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
10812 it.glyph_row->used[TEXT_AREA] = 0;
10813 SET_TEXT_POS (it.position, 0, 0);
10814
10815 multibyte_p = !NILP (buffer->enable_multibyte_characters);
10816 p = arrow_string;
10817 while (p < arrow_end)
10818 {
10819 Lisp_Object face, ilisp;
10820
10821 /* Get the next character. */
10822 if (multibyte_p)
10823 it.c = string_char_and_length (p, arrow_len, &it.len);
10824 else
10825 it.c = *p, it.len = 1;
10826 p += it.len;
10827
10828 /* Get its face. */
10829 XSETFASTINT (ilisp, p - arrow_string);
10830 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
10831 it.face_id = compute_char_face (f, it.c, face);
10832
10833 /* Compute its width, get its glyphs. */
10834 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
10835 SET_TEXT_POS (it.position, -1, -1);
10836 PRODUCE_GLYPHS (&it);
10837
10838 /* If this character doesn't fit any more in the line, we have
10839 to remove some glyphs. */
10840 if (it.current_x > it.last_visible_x)
10841 {
10842 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
10843 break;
10844 }
10845 }
10846
10847 set_buffer_temp (old);
10848 return it.glyph_row;
10849 }
10850
10851
10852 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
10853 glyphs are only inserted for terminal frames since we can't really
10854 win with truncation glyphs when partially visible glyphs are
10855 involved. Which glyphs to insert is determined by
10856 produce_special_glyphs. */
10857
10858 static void
10859 insert_left_trunc_glyphs (it)
10860 struct it *it;
10861 {
10862 struct it truncate_it;
10863 struct glyph *from, *end, *to, *toend;
10864
10865 xassert (!FRAME_WINDOW_P (it->f));
10866
10867 /* Get the truncation glyphs. */
10868 truncate_it = *it;
10869 truncate_it.current_x = 0;
10870 truncate_it.face_id = DEFAULT_FACE_ID;
10871 truncate_it.glyph_row = &scratch_glyph_row;
10872 truncate_it.glyph_row->used[TEXT_AREA] = 0;
10873 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
10874 truncate_it.object = make_number (0);
10875 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
10876
10877 /* Overwrite glyphs from IT with truncation glyphs. */
10878 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
10879 end = from + truncate_it.glyph_row->used[TEXT_AREA];
10880 to = it->glyph_row->glyphs[TEXT_AREA];
10881 toend = to + it->glyph_row->used[TEXT_AREA];
10882
10883 while (from < end)
10884 *to++ = *from++;
10885
10886 /* There may be padding glyphs left over. Remove them. */
10887 from = to;
10888 while (from < toend && CHAR_GLYPH_PADDING_P (*from))
10889 ++from;
10890 while (from < toend)
10891 *to++ = *from++;
10892
10893 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
10894 }
10895
10896
10897 /* Compute the pixel height and width of IT->glyph_row.
10898
10899 Most of the time, ascent and height of a display line will be equal
10900 to the max_ascent and max_height values of the display iterator
10901 structure. This is not the case if
10902
10903 1. We hit ZV without displaying anything. In this case, max_ascent
10904 and max_height will be zero.
10905
10906 2. We have some glyphs that don't contribute to the line height.
10907 (The glyph row flag contributes_to_line_height_p is for future
10908 pixmap extensions).
10909
10910 The first case is easily covered by using default values because in
10911 these cases, the line height does not really matter, except that it
10912 must not be zero. */
10913
10914 static void
10915 compute_line_metrics (it)
10916 struct it *it;
10917 {
10918 struct glyph_row *row = it->glyph_row;
10919 int area, i;
10920
10921 if (FRAME_WINDOW_P (it->f))
10922 {
10923 int i, header_line_height;
10924
10925 /* The line may consist of one space only, that was added to
10926 place the cursor on it. If so, the row's height hasn't been
10927 computed yet. */
10928 if (row->height == 0)
10929 {
10930 if (it->max_ascent + it->max_descent == 0)
10931 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
10932 row->ascent = it->max_ascent;
10933 row->height = it->max_ascent + it->max_descent;
10934 row->phys_ascent = it->max_phys_ascent;
10935 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
10936 }
10937
10938 /* Compute the width of this line. */
10939 row->pixel_width = row->x;
10940 for (i = 0; i < row->used[TEXT_AREA]; ++i)
10941 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
10942
10943 xassert (row->pixel_width >= 0);
10944 xassert (row->ascent >= 0 && row->height > 0);
10945
10946 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
10947 || MATRIX_ROW_OVERLAPS_PRED_P (row));
10948
10949 /* If first line's physical ascent is larger than its logical
10950 ascent, use the physical ascent, and make the row taller.
10951 This makes accented characters fully visible. */
10952 if (row == it->w->desired_matrix->rows
10953 && row->phys_ascent > row->ascent)
10954 {
10955 row->height += row->phys_ascent - row->ascent;
10956 row->ascent = row->phys_ascent;
10957 }
10958
10959 /* Compute how much of the line is visible. */
10960 row->visible_height = row->height;
10961
10962 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (it->w);
10963 if (row->y < header_line_height)
10964 row->visible_height -= header_line_height - row->y;
10965 else
10966 {
10967 int max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
10968 if (row->y + row->height > max_y)
10969 row->visible_height -= row->y + row->height - max_y;
10970 }
10971 }
10972 else
10973 {
10974 row->pixel_width = row->used[TEXT_AREA];
10975 row->ascent = row->phys_ascent = 0;
10976 row->height = row->phys_height = row->visible_height = 1;
10977 }
10978
10979 /* Compute a hash code for this row. */
10980 row->hash = 0;
10981 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
10982 for (i = 0; i < row->used[area]; ++i)
10983 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
10984 + row->glyphs[area][i].u.val
10985 + row->glyphs[area][i].face_id
10986 + row->glyphs[area][i].padding_p
10987 + (row->glyphs[area][i].type << 2));
10988
10989 it->max_ascent = it->max_descent = 0;
10990 it->max_phys_ascent = it->max_phys_descent = 0;
10991 }
10992
10993
10994 /* Append one space to the glyph row of iterator IT if doing a
10995 window-based redisplay. DEFAULT_FACE_P non-zero means let the
10996 space have the default face, otherwise let it have the same face as
10997 IT->face_id. Value is non-zero if a space was added.
10998
10999 This function is called to make sure that there is always one glyph
11000 at the end of a glyph row that the cursor can be set on under
11001 window-systems. (If there weren't such a glyph we would not know
11002 how wide and tall a box cursor should be displayed).
11003
11004 At the same time this space let's a nicely handle clearing to the
11005 end of the line if the row ends in italic text. */
11006
11007 static int
11008 append_space (it, default_face_p)
11009 struct it *it;
11010 int default_face_p;
11011 {
11012 if (FRAME_WINDOW_P (it->f))
11013 {
11014 int n = it->glyph_row->used[TEXT_AREA];
11015
11016 if (it->glyph_row->glyphs[TEXT_AREA] + n
11017 < it->glyph_row->glyphs[1 + TEXT_AREA])
11018 {
11019 /* Save some values that must not be changed. */
11020 int saved_x = it->current_x;
11021 struct text_pos saved_pos;
11022 int saved_what = it->what;
11023 int saved_face_id = it->face_id;
11024 Lisp_Object saved_object;
11025 struct face *face;
11026
11027 saved_object = it->object;
11028 saved_pos = it->position;
11029
11030 it->what = IT_CHARACTER;
11031 bzero (&it->position, sizeof it->position);
11032 it->object = make_number (0);
11033 it->c = ' ';
11034 it->len = 1;
11035
11036 if (default_face_p)
11037 it->face_id = DEFAULT_FACE_ID;
11038 face = FACE_FROM_ID (it->f, it->face_id);
11039 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
11040
11041 PRODUCE_GLYPHS (it);
11042
11043 it->current_x = saved_x;
11044 it->object = saved_object;
11045 it->position = saved_pos;
11046 it->what = saved_what;
11047 it->face_id = saved_face_id;
11048 return 1;
11049 }
11050 }
11051
11052 return 0;
11053 }
11054
11055
11056 /* Extend the face of the last glyph in the text area of IT->glyph_row
11057 to the end of the display line. Called from display_line.
11058 If the glyph row is empty, add a space glyph to it so that we
11059 know the face to draw. Set the glyph row flag fill_line_p. */
11060
11061 static void
11062 extend_face_to_end_of_line (it)
11063 struct it *it;
11064 {
11065 struct face *face;
11066 struct frame *f = it->f;
11067
11068 /* If line is already filled, do nothing. */
11069 if (it->current_x >= it->last_visible_x)
11070 return;
11071
11072 /* Face extension extends the background and box of IT->face_id
11073 to the end of the line. If the background equals the background
11074 of the frame, we haven't to do anything. */
11075 face = FACE_FROM_ID (f, it->face_id);
11076 if (FRAME_WINDOW_P (f)
11077 && face->box == FACE_NO_BOX
11078 && face->background == FRAME_BACKGROUND_PIXEL (f)
11079 && !face->stipple)
11080 return;
11081
11082 /* Set the glyph row flag indicating that the face of the last glyph
11083 in the text area has to be drawn to the end of the text area. */
11084 it->glyph_row->fill_line_p = 1;
11085
11086 /* If current character of IT is not ASCII, make sure we have the
11087 ASCII face. This will be automatically undone the next time
11088 get_next_display_element returns a multibyte character. Note
11089 that the character will always be single byte in unibyte text. */
11090 if (!SINGLE_BYTE_CHAR_P (it->c))
11091 {
11092 it->face_id = FACE_FOR_CHAR (f, face, 0);
11093 }
11094
11095 if (FRAME_WINDOW_P (f))
11096 {
11097 /* If the row is empty, add a space with the current face of IT,
11098 so that we know which face to draw. */
11099 if (it->glyph_row->used[TEXT_AREA] == 0)
11100 {
11101 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
11102 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
11103 it->glyph_row->used[TEXT_AREA] = 1;
11104 }
11105 }
11106 else
11107 {
11108 /* Save some values that must not be changed. */
11109 int saved_x = it->current_x;
11110 struct text_pos saved_pos;
11111 Lisp_Object saved_object;
11112 int saved_what = it->what;
11113
11114 saved_object = it->object;
11115 saved_pos = it->position;
11116
11117 it->what = IT_CHARACTER;
11118 bzero (&it->position, sizeof it->position);
11119 it->object = make_number (0);
11120 it->c = ' ';
11121 it->len = 1;
11122
11123 PRODUCE_GLYPHS (it);
11124
11125 while (it->current_x <= it->last_visible_x)
11126 PRODUCE_GLYPHS (it);
11127
11128 /* Don't count these blanks really. It would let us insert a left
11129 truncation glyph below and make us set the cursor on them, maybe. */
11130 it->current_x = saved_x;
11131 it->object = saved_object;
11132 it->position = saved_pos;
11133 it->what = saved_what;
11134 }
11135 }
11136
11137
11138 /* Value is non-zero if text starting at CHARPOS in current_buffer is
11139 trailing whitespace. */
11140
11141 static int
11142 trailing_whitespace_p (charpos)
11143 int charpos;
11144 {
11145 int bytepos = CHAR_TO_BYTE (charpos);
11146 int c = 0;
11147
11148 while (bytepos < ZV_BYTE
11149 && (c = FETCH_CHAR (bytepos),
11150 c == ' ' || c == '\t'))
11151 ++bytepos;
11152
11153 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
11154 {
11155 if (bytepos != PT_BYTE)
11156 return 1;
11157 }
11158 return 0;
11159 }
11160
11161
11162 /* Highlight trailing whitespace, if any, in ROW. */
11163
11164 void
11165 highlight_trailing_whitespace (f, row)
11166 struct frame *f;
11167 struct glyph_row *row;
11168 {
11169 int used = row->used[TEXT_AREA];
11170
11171 if (used)
11172 {
11173 struct glyph *start = row->glyphs[TEXT_AREA];
11174 struct glyph *glyph = start + used - 1;
11175
11176 /* Skip over the space glyph inserted to display the
11177 cursor at the end of a line. */
11178 if (glyph->type == CHAR_GLYPH
11179 && glyph->u.ch == ' '
11180 && INTEGERP (glyph->object))
11181 --glyph;
11182
11183 /* If last glyph is a space or stretch, and it's trailing
11184 whitespace, set the face of all trailing whitespace glyphs in
11185 IT->glyph_row to `trailing-whitespace'. */
11186 if (glyph >= start
11187 && BUFFERP (glyph->object)
11188 && (glyph->type == STRETCH_GLYPH
11189 || (glyph->type == CHAR_GLYPH
11190 && glyph->u.ch == ' '))
11191 && trailing_whitespace_p (glyph->charpos))
11192 {
11193 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
11194
11195 while (glyph >= start
11196 && BUFFERP (glyph->object)
11197 && (glyph->type == STRETCH_GLYPH
11198 || (glyph->type == CHAR_GLYPH
11199 && glyph->u.ch == ' ')))
11200 (glyph--)->face_id = face_id;
11201 }
11202 }
11203 }
11204
11205
11206 /* Construct the glyph row IT->glyph_row in the desired matrix of
11207 IT->w from text at the current position of IT. See dispextern.h
11208 for an overview of struct it. Value is non-zero if
11209 IT->glyph_row displays text, as opposed to a line displaying ZV
11210 only. */
11211
11212 static int
11213 display_line (it)
11214 struct it *it;
11215 {
11216 struct glyph_row *row = it->glyph_row;
11217
11218 /* We always start displaying at hpos zero even if hscrolled. */
11219 xassert (it->hpos == 0 && it->current_x == 0);
11220
11221 /* We must not display in a row that's not a text row. */
11222 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
11223 < it->w->desired_matrix->nrows);
11224
11225 /* Is IT->w showing the region? */
11226 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
11227
11228 /* Clear the result glyph row and enable it. */
11229 prepare_desired_row (row);
11230
11231 row->y = it->current_y;
11232 row->start = it->current;
11233 row->continuation_lines_width = it->continuation_lines_width;
11234 row->displays_text_p = 1;
11235
11236 /* Arrange the overlays nicely for our purposes. Usually, we call
11237 display_line on only one line at a time, in which case this
11238 can't really hurt too much, or we call it on lines which appear
11239 one after another in the buffer, in which case all calls to
11240 recenter_overlay_lists but the first will be pretty cheap. */
11241 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
11242
11243 /* Move over display elements that are not visible because we are
11244 hscrolled. This may stop at an x-position < IT->first_visible_x
11245 if the first glyph is partially visible or if we hit a line end. */
11246 if (it->current_x < it->first_visible_x)
11247 move_it_in_display_line_to (it, ZV, it->first_visible_x,
11248 MOVE_TO_POS | MOVE_TO_X);
11249
11250 /* Get the initial row height. This is either the height of the
11251 text hscrolled, if there is any, or zero. */
11252 row->ascent = it->max_ascent;
11253 row->height = it->max_ascent + it->max_descent;
11254 row->phys_ascent = it->max_phys_ascent;
11255 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
11256
11257 /* Loop generating characters. The loop is left with IT on the next
11258 character to display. */
11259 while (1)
11260 {
11261 int n_glyphs_before, hpos_before, x_before;
11262 int x, i, nglyphs;
11263 int ascent, descent, phys_ascent, phys_descent;
11264
11265 /* Retrieve the next thing to display. Value is zero if end of
11266 buffer reached. */
11267 if (!get_next_display_element (it))
11268 {
11269 /* Maybe add a space at the end of this line that is used to
11270 display the cursor there under X. Set the charpos of the
11271 first glyph of blank lines not corresponding to any text
11272 to -1. */
11273 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
11274 || row->used[TEXT_AREA] == 0)
11275 {
11276 row->glyphs[TEXT_AREA]->charpos = -1;
11277 row->displays_text_p = 0;
11278
11279 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines))
11280 row->indicate_empty_line_p = 1;
11281 }
11282
11283 it->continuation_lines_width = 0;
11284 row->ends_at_zv_p = 1;
11285 break;
11286 }
11287
11288 /* Now, get the metrics of what we want to display. This also
11289 generates glyphs in `row' (which is IT->glyph_row). */
11290 n_glyphs_before = row->used[TEXT_AREA];
11291 x = it->current_x;
11292
11293 /* Remember the line height so far in case the next element doesn't
11294 fit on the line. */
11295 if (!it->truncate_lines_p)
11296 {
11297 ascent = it->max_ascent;
11298 descent = it->max_descent;
11299 phys_ascent = it->max_phys_ascent;
11300 phys_descent = it->max_phys_descent;
11301 }
11302
11303 PRODUCE_GLYPHS (it);
11304
11305 /* If this display element was in marginal areas, continue with
11306 the next one. */
11307 if (it->area != TEXT_AREA)
11308 {
11309 row->ascent = max (row->ascent, it->max_ascent);
11310 row->height = max (row->height, it->max_ascent + it->max_descent);
11311 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
11312 row->phys_height = max (row->phys_height,
11313 it->max_phys_ascent + it->max_phys_descent);
11314 set_iterator_to_next (it);
11315 continue;
11316 }
11317
11318 /* Does the display element fit on the line? If we truncate
11319 lines, we should draw past the right edge of the window. If
11320 we don't truncate, we want to stop so that we can display the
11321 continuation glyph before the right margin. If lines are
11322 continued, there are two possible strategies for characters
11323 resulting in more than 1 glyph (e.g. tabs): Display as many
11324 glyphs as possible in this line and leave the rest for the
11325 continuation line, or display the whole element in the next
11326 line. Original redisplay did the former, so we do it also. */
11327 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11328 hpos_before = it->hpos;
11329 x_before = x;
11330
11331 if (nglyphs == 1
11332 && it->current_x < it->last_visible_x)
11333 {
11334 ++it->hpos;
11335 row->ascent = max (row->ascent, it->max_ascent);
11336 row->height = max (row->height, it->max_ascent + it->max_descent);
11337 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
11338 row->phys_height = max (row->phys_height,
11339 it->max_phys_ascent + it->max_phys_descent);
11340 if (it->current_x - it->pixel_width < it->first_visible_x)
11341 row->x = x - it->first_visible_x;
11342 }
11343 else
11344 {
11345 int new_x;
11346 struct glyph *glyph;
11347
11348 for (i = 0; i < nglyphs; ++i, x = new_x)
11349 {
11350 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11351 new_x = x + glyph->pixel_width;
11352
11353 if (/* Lines are continued. */
11354 !it->truncate_lines_p
11355 && (/* Glyph doesn't fit on the line. */
11356 new_x > it->last_visible_x
11357 /* Or it fits exactly on a window system frame. */
11358 || (new_x == it->last_visible_x
11359 && FRAME_WINDOW_P (it->f))))
11360 {
11361 /* End of a continued line. */
11362
11363 if (it->hpos == 0
11364 || (new_x == it->last_visible_x
11365 && FRAME_WINDOW_P (it->f)))
11366 {
11367 /* Current glyph is the only one on the line or
11368 fits exactly on the line. We must continue
11369 the line because we can't draw the cursor
11370 after the glyph. */
11371 row->continued_p = 1;
11372 it->current_x = new_x;
11373 it->continuation_lines_width += new_x;
11374 ++it->hpos;
11375 if (i == nglyphs - 1)
11376 set_iterator_to_next (it);
11377 }
11378 else
11379 {
11380 /* Display element draws past the right edge of
11381 the window. Restore positions to values
11382 before the element. The next line starts
11383 with current_x before the glyph that could
11384 not be displayed, so that TAB works right. */
11385 row->used[TEXT_AREA] = n_glyphs_before + i;
11386
11387 /* Display continuation glyphs. */
11388 if (!FRAME_WINDOW_P (it->f))
11389 produce_special_glyphs (it, IT_CONTINUATION);
11390 row->continued_p = 1;
11391
11392 it->current_x = x;
11393 it->continuation_lines_width += x;
11394
11395 /* Restore the height to what it was before the
11396 element not fitting on the line. */
11397 it->max_ascent = ascent;
11398 it->max_descent = descent;
11399 it->max_phys_ascent = phys_ascent;
11400 it->max_phys_descent = phys_descent;
11401 }
11402
11403 break;
11404 }
11405 else if (new_x > it->first_visible_x)
11406 {
11407 /* Increment number of glyphs actually displayed. */
11408 ++it->hpos;
11409
11410 if (x < it->first_visible_x)
11411 /* Glyph is partially visible, i.e. row starts at
11412 negative X position. */
11413 row->x = x - it->first_visible_x;
11414 }
11415 else
11416 {
11417 /* Glyph is completely off the left margin of the
11418 window. This should not happen because of the
11419 move_it_in_display_line at the start of
11420 this function. */
11421 abort ();
11422 }
11423 }
11424
11425 row->ascent = max (row->ascent, it->max_ascent);
11426 row->height = max (row->height, it->max_ascent + it->max_descent);
11427 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
11428 row->phys_height = max (row->phys_height,
11429 it->max_phys_ascent + it->max_phys_descent);
11430
11431 /* End of this display line if row is continued. */
11432 if (row->continued_p)
11433 break;
11434 }
11435
11436 /* Is this a line end? If yes, we're also done, after making
11437 sure that a non-default face is extended up to the right
11438 margin of the window. */
11439 if (ITERATOR_AT_END_OF_LINE_P (it))
11440 {
11441 int used_before = row->used[TEXT_AREA];
11442
11443 /* Add a space at the end of the line that is used to
11444 display the cursor there. */
11445 append_space (it, 0);
11446
11447 /* Extend the face to the end of the line. */
11448 extend_face_to_end_of_line (it);
11449
11450 /* Make sure we have the position. */
11451 if (used_before == 0)
11452 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
11453
11454 /* Consume the line end. This skips over invisible lines. */
11455 set_iterator_to_next (it);
11456 it->continuation_lines_width = 0;
11457 break;
11458 }
11459
11460 /* Proceed with next display element. Note that this skips
11461 over lines invisible because of selective display. */
11462 set_iterator_to_next (it);
11463
11464 /* If we truncate lines, we are done when the last displayed
11465 glyphs reach past the right margin of the window. */
11466 if (it->truncate_lines_p
11467 && (FRAME_WINDOW_P (it->f)
11468 ? (it->current_x >= it->last_visible_x)
11469 : (it->current_x > it->last_visible_x)))
11470 {
11471 /* Maybe add truncation glyphs. */
11472 if (!FRAME_WINDOW_P (it->f))
11473 {
11474 --it->glyph_row->used[TEXT_AREA];
11475 produce_special_glyphs (it, IT_TRUNCATION);
11476 }
11477
11478 row->truncated_on_right_p = 1;
11479 it->continuation_lines_width = 0;
11480 reseat_at_next_visible_line_start (it, 0);
11481 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
11482 it->hpos = hpos_before;
11483 it->current_x = x_before;
11484 break;
11485 }
11486 }
11487
11488 /* If line is not empty and hscrolled, maybe insert truncation glyphs
11489 at the left window margin. */
11490 if (it->first_visible_x
11491 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
11492 {
11493 if (!FRAME_WINDOW_P (it->f))
11494 insert_left_trunc_glyphs (it);
11495 row->truncated_on_left_p = 1;
11496 }
11497
11498 /* If the start of this line is the overlay arrow-position, then
11499 mark this glyph row as the one containing the overlay arrow.
11500 This is clearly a mess with variable size fonts. It would be
11501 better to let it be displayed like cursors under X. */
11502 if (MARKERP (Voverlay_arrow_position)
11503 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
11504 && (MATRIX_ROW_START_CHARPOS (row)
11505 == marker_position (Voverlay_arrow_position))
11506 && STRINGP (Voverlay_arrow_string)
11507 && ! overlay_arrow_seen)
11508 {
11509 /* Overlay arrow in window redisplay is a bitmap. */
11510 if (!FRAME_WINDOW_P (it->f))
11511 {
11512 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
11513 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
11514 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
11515 struct glyph *p = row->glyphs[TEXT_AREA];
11516 struct glyph *p2, *end;
11517
11518 /* Copy the arrow glyphs. */
11519 while (glyph < arrow_end)
11520 *p++ = *glyph++;
11521
11522 /* Throw away padding glyphs. */
11523 p2 = p;
11524 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
11525 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
11526 ++p2;
11527 if (p2 > p)
11528 {
11529 while (p2 < end)
11530 *p++ = *p2++;
11531 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
11532 }
11533 }
11534
11535 overlay_arrow_seen = 1;
11536 row->overlay_arrow_p = 1;
11537 }
11538
11539 /* Compute pixel dimensions of this line. */
11540 compute_line_metrics (it);
11541
11542 /* Remember the position at which this line ends. */
11543 row->end = it->current;
11544
11545 /* Maybe set the cursor. If you change this, it's probably a good
11546 idea to also change the code in redisplay_window for cursor
11547 movement in an unchanged window. */
11548 if (it->w->cursor.vpos < 0
11549 && PT >= MATRIX_ROW_START_CHARPOS (row)
11550 && MATRIX_ROW_END_CHARPOS (row) >= PT
11551 && !(MATRIX_ROW_END_CHARPOS (row) == PT
11552 && (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
11553 || !row->ends_at_zv_p)))
11554 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
11555
11556 /* Highlight trailing whitespace. */
11557 if (!NILP (Vshow_trailing_whitespace))
11558 highlight_trailing_whitespace (it->f, it->glyph_row);
11559
11560 /* Prepare for the next line. This line starts horizontally at (X
11561 HPOS) = (0 0). Vertical positions are incremented. As a
11562 convenience for the caller, IT->glyph_row is set to the next
11563 row to be used. */
11564 it->current_x = it->hpos = 0;
11565 it->current_y += row->height;
11566 ++it->vpos;
11567 ++it->glyph_row;
11568 return row->displays_text_p;
11569 }
11570
11571
11572 \f
11573 /***********************************************************************
11574 Menu Bar
11575 ***********************************************************************/
11576
11577 /* Redisplay the menu bar in the frame for window W.
11578
11579 The menu bar of X frames that don't have X toolkit support is
11580 displayed in a special window W->frame->menu_bar_window.
11581
11582 The menu bar of terminal frames is treated specially as far as
11583 glyph matrices are concerned. Menu bar lines are not part of
11584 windows, so the update is done directly on the frame matrix rows
11585 for the menu bar. */
11586
11587 static void
11588 display_menu_bar (w)
11589 struct window *w;
11590 {
11591 struct frame *f = XFRAME (WINDOW_FRAME (w));
11592 struct it it;
11593 Lisp_Object items;
11594 int i;
11595
11596 /* Don't do all this for graphical frames. */
11597 #ifdef HAVE_NTGUI
11598 if (!NILP (Vwindow_system))
11599 return;
11600 #endif
11601 #ifdef USE_X_TOOLKIT
11602 if (FRAME_X_P (f))
11603 return;
11604 #endif
11605
11606 #ifdef USE_X_TOOLKIT
11607 xassert (!FRAME_WINDOW_P (f));
11608 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
11609 it.first_visible_x = 0;
11610 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
11611 #else /* not USE_X_TOOLKIT */
11612 if (FRAME_WINDOW_P (f))
11613 {
11614 /* Menu bar lines are displayed in the desired matrix of the
11615 dummy window menu_bar_window. */
11616 struct window *menu_w;
11617 xassert (WINDOWP (f->menu_bar_window));
11618 menu_w = XWINDOW (f->menu_bar_window);
11619 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
11620 MENU_FACE_ID);
11621 it.first_visible_x = 0;
11622 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
11623 }
11624 else
11625 {
11626 /* This is a TTY frame, i.e. character hpos/vpos are used as
11627 pixel x/y. */
11628 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
11629 MENU_FACE_ID);
11630 it.first_visible_x = 0;
11631 it.last_visible_x = FRAME_WIDTH (f);
11632 }
11633 #endif /* not USE_X_TOOLKIT */
11634
11635 /* Clear all rows of the menu bar. */
11636 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
11637 {
11638 struct glyph_row *row = it.glyph_row + i;
11639 clear_glyph_row (row);
11640 row->enabled_p = 1;
11641 row->full_width_p = 1;
11642 }
11643
11644 /* Make the first line of the menu bar appear in reverse video. */
11645 it.glyph_row->inverse_p = mode_line_inverse_video != 0;
11646
11647 /* Display all items of the menu bar. */
11648 items = FRAME_MENU_BAR_ITEMS (it.f);
11649 for (i = 0; i < XVECTOR (items)->size; i += 4)
11650 {
11651 Lisp_Object string;
11652
11653 /* Stop at nil string. */
11654 string = XVECTOR (items)->contents[i + 1];
11655 if (NILP (string))
11656 break;
11657
11658 /* Remember where item was displayed. */
11659 XSETFASTINT (XVECTOR (items)->contents[i + 3], it.hpos);
11660
11661 /* Display the item, pad with one space. */
11662 if (it.current_x < it.last_visible_x)
11663 display_string (NULL, string, Qnil, 0, 0, &it,
11664 XSTRING (string)->size + 1, 0, 0, -1);
11665 }
11666
11667 /* Fill out the line with spaces. */
11668 if (it.current_x < it.last_visible_x)
11669 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
11670
11671 /* Compute the total height of the lines. */
11672 compute_line_metrics (&it);
11673 }
11674
11675
11676 \f
11677 /***********************************************************************
11678 Mode Line
11679 ***********************************************************************/
11680
11681 /* Display the mode and/or top line of window W. */
11682
11683 static void
11684 display_mode_lines (w)
11685 struct window *w;
11686 {
11687 /* These will be set while the mode line specs are processed. */
11688 line_number_displayed = 0;
11689 w->column_number_displayed = Qnil;
11690
11691 if (WINDOW_WANTS_MODELINE_P (w))
11692 display_mode_line (w, MODE_LINE_FACE_ID,
11693 current_buffer->mode_line_format);
11694
11695 if (WINDOW_WANTS_HEADER_LINE_P (w))
11696 display_mode_line (w, HEADER_LINE_FACE_ID,
11697 current_buffer->header_line_format);
11698 }
11699
11700
11701 /* Display mode or top line of window W. FACE_ID specifies which line
11702 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
11703 FORMAT is the mode line format to display. */
11704
11705 static void
11706 display_mode_line (w, face_id, format)
11707 struct window *w;
11708 enum face_id face_id;
11709 Lisp_Object format;
11710 {
11711 struct it it;
11712 struct face *face;
11713
11714 init_iterator (&it, w, -1, -1, NULL, face_id);
11715 prepare_desired_row (it.glyph_row);
11716
11717 /* Temporarily make frame's keyboard the current kboard so that
11718 kboard-local variables in the mode_line_format will get the right
11719 values. */
11720 push_frame_kboard (it.f);
11721 display_mode_element (&it, 0, 0, 0, format);
11722 pop_frame_kboard ();
11723
11724 /* Fill up with spaces. */
11725 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
11726
11727 compute_line_metrics (&it);
11728 it.glyph_row->full_width_p = 1;
11729 it.glyph_row->mode_line_p = 1;
11730 it.glyph_row->inverse_p = mode_line_inverse_video != 0;
11731 it.glyph_row->continued_p = 0;
11732 it.glyph_row->truncated_on_left_p = 0;
11733 it.glyph_row->truncated_on_right_p = 0;
11734
11735 /* Make a 3D mode-line have a shadow at its right end. */
11736 face = FACE_FROM_ID (it.f, face_id);
11737 extend_face_to_end_of_line (&it);
11738 if (face->box != FACE_NO_BOX)
11739 {
11740 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
11741 + it.glyph_row->used[TEXT_AREA] - 1);
11742 last->right_box_line_p = 1;
11743 }
11744 }
11745
11746
11747 /* Contribute ELT to the mode line for window IT->w. How it
11748 translates into text depends on its data type.
11749
11750 IT describes the display environment in which we display, as usual.
11751
11752 DEPTH is the depth in recursion. It is used to prevent
11753 infinite recursion here.
11754
11755 FIELD_WIDTH is the number of characters the display of ELT should
11756 occupy in the mode line, and PRECISION is the maximum number of
11757 characters to display from ELT's representation. See
11758 display_string for details. *
11759
11760 Returns the hpos of the end of the text generated by ELT. */
11761
11762 static int
11763 display_mode_element (it, depth, field_width, precision, elt)
11764 struct it *it;
11765 int depth;
11766 int field_width, precision;
11767 Lisp_Object elt;
11768 {
11769 int n = 0, field, prec;
11770
11771 tail_recurse:
11772 if (depth > 10)
11773 goto invalid;
11774
11775 depth++;
11776
11777 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
11778 {
11779 case Lisp_String:
11780 {
11781 /* A string: output it and check for %-constructs within it. */
11782 unsigned char c;
11783 unsigned char *this = XSTRING (elt)->data;
11784 unsigned char *lisp_string = this;
11785
11786 while ((precision <= 0 || n < precision)
11787 && *this
11788 && (frame_title_ptr
11789 || it->current_x < it->last_visible_x))
11790 {
11791 unsigned char *last = this;
11792
11793 /* Advance to end of string or next format specifier. */
11794 while ((c = *this++) != '\0' && c != '%')
11795 ;
11796
11797 if (this - 1 != last)
11798 {
11799 /* Output to end of string or up to '%'. Field width
11800 is length of string. Don't output more than
11801 PRECISION allows us. */
11802 prec = --this - last;
11803 if (precision > 0 && prec > precision - n)
11804 prec = precision - n;
11805
11806 if (frame_title_ptr)
11807 n += store_frame_title (last, prec, prec);
11808 else
11809 n += display_string (NULL, elt, Qnil, 0, last - lisp_string,
11810 it, 0, prec, 0, -1);
11811 }
11812 else /* c == '%' */
11813 {
11814 unsigned char *percent_position = this;
11815
11816 /* Get the specified minimum width. Zero means
11817 don't pad. */
11818 field = 0;
11819 while ((c = *this++) >= '0' && c <= '9')
11820 field = field * 10 + c - '0';
11821
11822 /* Don't pad beyond the total padding allowed. */
11823 if (field_width - n > 0 && field > field_width - n)
11824 field = field_width - n;
11825
11826 /* Note that either PRECISION <= 0 or N < PRECISION. */
11827 prec = precision - n;
11828
11829 if (c == 'M')
11830 n += display_mode_element (it, depth, field, prec,
11831 Vglobal_mode_string);
11832 else if (c != 0)
11833 {
11834 unsigned char *spec
11835 = decode_mode_spec (it->w, c, field, prec);
11836
11837 if (frame_title_ptr)
11838 n += store_frame_title (spec, field, prec);
11839 else
11840 {
11841 int nglyphs_before
11842 = it->glyph_row->used[TEXT_AREA];
11843 int charpos
11844 = percent_position - XSTRING (elt)->data;
11845 int nwritten
11846 = display_string (spec, Qnil, elt, charpos, 0, it,
11847 field, prec, 0, -1);
11848
11849 /* Assign to the glyphs written above the
11850 string where the `%x' came from, position
11851 of the `%'. */
11852 if (nwritten > 0)
11853 {
11854 struct glyph *glyph
11855 = (it->glyph_row->glyphs[TEXT_AREA]
11856 + nglyphs_before);
11857 int i;
11858
11859 for (i = 0; i < nwritten; ++i)
11860 {
11861 glyph[i].object = elt;
11862 glyph[i].charpos = charpos;
11863 }
11864
11865 n += nwritten;
11866 }
11867 }
11868 }
11869 }
11870 }
11871 }
11872 break;
11873
11874 case Lisp_Symbol:
11875 /* A symbol: process the value of the symbol recursively
11876 as if it appeared here directly. Avoid error if symbol void.
11877 Special case: if value of symbol is a string, output the string
11878 literally. */
11879 {
11880 register Lisp_Object tem;
11881 tem = Fboundp (elt);
11882 if (!NILP (tem))
11883 {
11884 tem = Fsymbol_value (elt);
11885 /* If value is a string, output that string literally:
11886 don't check for % within it. */
11887 if (STRINGP (tem))
11888 {
11889 prec = XSTRING (tem)->size;
11890 if (precision > 0 && prec > precision - n)
11891 prec = precision - n;
11892 if (frame_title_ptr)
11893 n += store_frame_title (XSTRING (tem)->data, -1, prec);
11894 else
11895 n += display_string (NULL, tem, Qnil, 0, 0, it,
11896 0, prec, 0, -1);
11897 }
11898 else if (!EQ (tem, elt))
11899 {
11900 /* Give up right away for nil or t. */
11901 elt = tem;
11902 goto tail_recurse;
11903 }
11904 }
11905 }
11906 break;
11907
11908 case Lisp_Cons:
11909 {
11910 register Lisp_Object car, tem;
11911
11912 /* A cons cell: three distinct cases.
11913 If first element is a string or a cons, process all the elements
11914 and effectively concatenate them.
11915 If first element is a negative number, truncate displaying cdr to
11916 at most that many characters. If positive, pad (with spaces)
11917 to at least that many characters.
11918 If first element is a symbol, process the cadr or caddr recursively
11919 according to whether the symbol's value is non-nil or nil. */
11920 car = XCAR (elt);
11921 if (EQ (car, QCeval) && CONSP (XCDR (elt)))
11922 {
11923 /* An element of the form (:eval FORM) means evaluate FORM
11924 and use the result as mode line elements. */
11925 struct gcpro gcpro1;
11926 Lisp_Object spec;
11927
11928 spec = eval_form (XCAR (XCDR (elt)));
11929 GCPRO1 (spec);
11930 n += display_mode_element (it, depth, field_width - n,
11931 precision - n, spec);
11932 UNGCPRO;
11933 }
11934 else if (SYMBOLP (car))
11935 {
11936 tem = Fboundp (car);
11937 elt = XCDR (elt);
11938 if (!CONSP (elt))
11939 goto invalid;
11940 /* elt is now the cdr, and we know it is a cons cell.
11941 Use its car if CAR has a non-nil value. */
11942 if (!NILP (tem))
11943 {
11944 tem = Fsymbol_value (car);
11945 if (!NILP (tem))
11946 {
11947 elt = XCAR (elt);
11948 goto tail_recurse;
11949 }
11950 }
11951 /* Symbol's value is nil (or symbol is unbound)
11952 Get the cddr of the original list
11953 and if possible find the caddr and use that. */
11954 elt = XCDR (elt);
11955 if (NILP (elt))
11956 break;
11957 else if (!CONSP (elt))
11958 goto invalid;
11959 elt = XCAR (elt);
11960 goto tail_recurse;
11961 }
11962 else if (INTEGERP (car))
11963 {
11964 register int lim = XINT (car);
11965 elt = XCDR (elt);
11966 if (lim < 0)
11967 {
11968 /* Negative int means reduce maximum width. */
11969 if (precision <= 0)
11970 precision = -lim;
11971 else
11972 precision = min (precision, -lim);
11973 }
11974 else if (lim > 0)
11975 {
11976 /* Padding specified. Don't let it be more than
11977 current maximum. */
11978 if (precision > 0)
11979 lim = min (precision, lim);
11980
11981 /* If that's more padding than already wanted, queue it.
11982 But don't reduce padding already specified even if
11983 that is beyond the current truncation point. */
11984 field_width = max (lim, field_width);
11985 }
11986 goto tail_recurse;
11987 }
11988 else if (STRINGP (car) || CONSP (car))
11989 {
11990 register int limit = 50;
11991 /* Limit is to protect against circular lists. */
11992 while (CONSP (elt)
11993 && --limit > 0
11994 && (precision <= 0 || n < precision))
11995 {
11996 n += display_mode_element (it, depth, field_width - n,
11997 precision - n, XCAR (elt));
11998 elt = XCDR (elt);
11999 }
12000 }
12001 }
12002 break;
12003
12004 default:
12005 invalid:
12006 if (frame_title_ptr)
12007 n += store_frame_title ("*invalid*", 0, precision - n);
12008 else
12009 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
12010 precision - n, 0, 0);
12011 return n;
12012 }
12013
12014 /* Pad to FIELD_WIDTH. */
12015 if (field_width > 0 && n < field_width)
12016 {
12017 if (frame_title_ptr)
12018 n += store_frame_title ("", field_width - n, 0);
12019 else
12020 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
12021 0, 0, 0);
12022 }
12023
12024 return n;
12025 }
12026
12027
12028 /* Write a null-terminated, right justified decimal representation of
12029 the positive integer D to BUF using a minimal field width WIDTH. */
12030
12031 static void
12032 pint2str (buf, width, d)
12033 register char *buf;
12034 register int width;
12035 register int d;
12036 {
12037 register char *p = buf;
12038
12039 if (d <= 0)
12040 *p++ = '0';
12041 else
12042 {
12043 while (d > 0)
12044 {
12045 *p++ = d % 10 + '0';
12046 d /= 10;
12047 }
12048 }
12049
12050 for (width -= (int) (p - buf); width > 0; --width)
12051 *p++ = ' ';
12052 *p-- = '\0';
12053 while (p > buf)
12054 {
12055 d = *buf;
12056 *buf++ = *p;
12057 *p-- = d;
12058 }
12059 }
12060
12061 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
12062 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
12063 type of CODING_SYSTEM. Return updated pointer into BUF. */
12064
12065 static unsigned char invalid_eol_type[] = "(*invalid*)";
12066
12067 static char *
12068 decode_mode_spec_coding (coding_system, buf, eol_flag)
12069 Lisp_Object coding_system;
12070 register char *buf;
12071 int eol_flag;
12072 {
12073 Lisp_Object val;
12074 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
12075 unsigned char *eol_str;
12076 int eol_str_len;
12077 /* The EOL conversion we are using. */
12078 Lisp_Object eoltype;
12079
12080 val = Fget (coding_system, Qcoding_system);
12081
12082 if (!VECTORP (val)) /* Not yet decided. */
12083 {
12084 if (multibyte)
12085 *buf++ = '-';
12086 if (eol_flag)
12087 eoltype = eol_mnemonic_undecided;
12088 /* Don't mention EOL conversion if it isn't decided. */
12089 }
12090 else
12091 {
12092 Lisp_Object eolvalue;
12093
12094 eolvalue = Fget (coding_system, Qeol_type);
12095
12096 if (multibyte)
12097 *buf++ = XFASTINT (XVECTOR (val)->contents[1]);
12098
12099 if (eol_flag)
12100 {
12101 /* The EOL conversion that is normal on this system. */
12102
12103 if (NILP (eolvalue)) /* Not yet decided. */
12104 eoltype = eol_mnemonic_undecided;
12105 else if (VECTORP (eolvalue)) /* Not yet decided. */
12106 eoltype = eol_mnemonic_undecided;
12107 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
12108 eoltype = (XFASTINT (eolvalue) == 0
12109 ? eol_mnemonic_unix
12110 : (XFASTINT (eolvalue) == 1
12111 ? eol_mnemonic_dos : eol_mnemonic_mac));
12112 }
12113 }
12114
12115 if (eol_flag)
12116 {
12117 /* Mention the EOL conversion if it is not the usual one. */
12118 if (STRINGP (eoltype))
12119 {
12120 eol_str = XSTRING (eoltype)->data;
12121 eol_str_len = XSTRING (eoltype)->size;
12122 }
12123 else if (INTEGERP (eoltype)
12124 && CHAR_VALID_P (XINT (eoltype), 0))
12125 {
12126 eol_str = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
12127 eol_str_len = CHAR_STRING (XINT (eoltype), eol_str);
12128 }
12129 else
12130 {
12131 eol_str = invalid_eol_type;
12132 eol_str_len = sizeof (invalid_eol_type) - 1;
12133 }
12134 bcopy (eol_str, buf, eol_str_len);
12135 buf += eol_str_len;
12136 }
12137
12138 return buf;
12139 }
12140
12141 /* Return a string for the output of a mode line %-spec for window W,
12142 generated by character C. PRECISION >= 0 means don't return a
12143 string longer than that value. FIELD_WIDTH > 0 means pad the
12144 string returned with spaces to that value. */
12145
12146 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
12147
12148 static char *
12149 decode_mode_spec (w, c, field_width, precision)
12150 struct window *w;
12151 register int c;
12152 int field_width, precision;
12153 {
12154 Lisp_Object obj;
12155 struct frame *f = XFRAME (WINDOW_FRAME (w));
12156 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
12157 struct buffer *b = XBUFFER (w->buffer);
12158
12159 obj = Qnil;
12160
12161 switch (c)
12162 {
12163 case '*':
12164 if (!NILP (b->read_only))
12165 return "%";
12166 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
12167 return "*";
12168 return "-";
12169
12170 case '+':
12171 /* This differs from %* only for a modified read-only buffer. */
12172 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
12173 return "*";
12174 if (!NILP (b->read_only))
12175 return "%";
12176 return "-";
12177
12178 case '&':
12179 /* This differs from %* in ignoring read-only-ness. */
12180 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
12181 return "*";
12182 return "-";
12183
12184 case '%':
12185 return "%";
12186
12187 case '[':
12188 {
12189 int i;
12190 char *p;
12191
12192 if (command_loop_level > 5)
12193 return "[[[... ";
12194 p = decode_mode_spec_buf;
12195 for (i = 0; i < command_loop_level; i++)
12196 *p++ = '[';
12197 *p = 0;
12198 return decode_mode_spec_buf;
12199 }
12200
12201 case ']':
12202 {
12203 int i;
12204 char *p;
12205
12206 if (command_loop_level > 5)
12207 return " ...]]]";
12208 p = decode_mode_spec_buf;
12209 for (i = 0; i < command_loop_level; i++)
12210 *p++ = ']';
12211 *p = 0;
12212 return decode_mode_spec_buf;
12213 }
12214
12215 case '-':
12216 {
12217 register int i;
12218
12219 /* Let lots_of_dashes be a string of infinite length. */
12220 if (field_width <= 0
12221 || field_width > sizeof (lots_of_dashes))
12222 {
12223 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
12224 decode_mode_spec_buf[i] = '-';
12225 decode_mode_spec_buf[i] = '\0';
12226 return decode_mode_spec_buf;
12227 }
12228 else
12229 return lots_of_dashes;
12230 }
12231
12232 case 'b':
12233 obj = b->name;
12234 break;
12235
12236 case 'c':
12237 {
12238 int col = current_column ();
12239 XSETFASTINT (w->column_number_displayed, col);
12240 pint2str (decode_mode_spec_buf, field_width, col);
12241 return decode_mode_spec_buf;
12242 }
12243
12244 case 'F':
12245 /* %F displays the frame name. */
12246 if (!NILP (f->title))
12247 return (char *) XSTRING (f->title)->data;
12248 if (f->explicit_name || ! FRAME_WINDOW_P (f))
12249 return (char *) XSTRING (f->name)->data;
12250 return "Emacs";
12251
12252 case 'f':
12253 obj = b->filename;
12254 break;
12255
12256 case 'l':
12257 {
12258 int startpos = XMARKER (w->start)->charpos;
12259 int startpos_byte = marker_byte_position (w->start);
12260 int line, linepos, linepos_byte, topline;
12261 int nlines, junk;
12262 int height = XFASTINT (w->height);
12263
12264 /* If we decided that this buffer isn't suitable for line numbers,
12265 don't forget that too fast. */
12266 if (EQ (w->base_line_pos, w->buffer))
12267 goto no_value;
12268 /* But do forget it, if the window shows a different buffer now. */
12269 else if (BUFFERP (w->base_line_pos))
12270 w->base_line_pos = Qnil;
12271
12272 /* If the buffer is very big, don't waste time. */
12273 if (BUF_ZV (b) - BUF_BEGV (b) > line_number_display_limit)
12274 {
12275 w->base_line_pos = Qnil;
12276 w->base_line_number = Qnil;
12277 goto no_value;
12278 }
12279
12280 if (!NILP (w->base_line_number)
12281 && !NILP (w->base_line_pos)
12282 && XFASTINT (w->base_line_pos) <= startpos)
12283 {
12284 line = XFASTINT (w->base_line_number);
12285 linepos = XFASTINT (w->base_line_pos);
12286 linepos_byte = buf_charpos_to_bytepos (b, linepos);
12287 }
12288 else
12289 {
12290 line = 1;
12291 linepos = BUF_BEGV (b);
12292 linepos_byte = BUF_BEGV_BYTE (b);
12293 }
12294
12295 /* Count lines from base line to window start position. */
12296 nlines = display_count_lines (linepos, linepos_byte,
12297 startpos_byte,
12298 startpos, &junk);
12299
12300 topline = nlines + line;
12301
12302 /* Determine a new base line, if the old one is too close
12303 or too far away, or if we did not have one.
12304 "Too close" means it's plausible a scroll-down would
12305 go back past it. */
12306 if (startpos == BUF_BEGV (b))
12307 {
12308 XSETFASTINT (w->base_line_number, topline);
12309 XSETFASTINT (w->base_line_pos, BUF_BEGV (b));
12310 }
12311 else if (nlines < height + 25 || nlines > height * 3 + 50
12312 || linepos == BUF_BEGV (b))
12313 {
12314 int limit = BUF_BEGV (b);
12315 int limit_byte = BUF_BEGV_BYTE (b);
12316 int position;
12317 int distance = (height * 2 + 30) * line_number_display_limit_width;
12318
12319 if (startpos - distance > limit)
12320 {
12321 limit = startpos - distance;
12322 limit_byte = CHAR_TO_BYTE (limit);
12323 }
12324
12325 nlines = display_count_lines (startpos, startpos_byte,
12326 limit_byte,
12327 - (height * 2 + 30),
12328 &position);
12329 /* If we couldn't find the lines we wanted within
12330 line_number_display_limit_width chars per line,
12331 give up on line numbers for this window. */
12332 if (position == limit_byte && limit == startpos - distance)
12333 {
12334 w->base_line_pos = w->buffer;
12335 w->base_line_number = Qnil;
12336 goto no_value;
12337 }
12338
12339 XSETFASTINT (w->base_line_number, topline - nlines);
12340 XSETFASTINT (w->base_line_pos, BYTE_TO_CHAR (position));
12341 }
12342
12343 /* Now count lines from the start pos to point. */
12344 nlines = display_count_lines (startpos, startpos_byte,
12345 PT_BYTE, PT, &junk);
12346
12347 /* Record that we did display the line number. */
12348 line_number_displayed = 1;
12349
12350 /* Make the string to show. */
12351 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
12352 return decode_mode_spec_buf;
12353 no_value:
12354 {
12355 char* p = decode_mode_spec_buf;
12356 int pad = field_width - 2;
12357 while (pad-- > 0)
12358 *p++ = ' ';
12359 *p++ = '?';
12360 *p = '?';
12361 return decode_mode_spec_buf;
12362 }
12363 }
12364 break;
12365
12366 case 'm':
12367 obj = b->mode_name;
12368 break;
12369
12370 case 'n':
12371 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
12372 return " Narrow";
12373 break;
12374
12375 case 'p':
12376 {
12377 int pos = marker_position (w->start);
12378 int total = BUF_ZV (b) - BUF_BEGV (b);
12379
12380 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
12381 {
12382 if (pos <= BUF_BEGV (b))
12383 return "All";
12384 else
12385 return "Bottom";
12386 }
12387 else if (pos <= BUF_BEGV (b))
12388 return "Top";
12389 else
12390 {
12391 if (total > 1000000)
12392 /* Do it differently for a large value, to avoid overflow. */
12393 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
12394 else
12395 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
12396 /* We can't normally display a 3-digit number,
12397 so get us a 2-digit number that is close. */
12398 if (total == 100)
12399 total = 99;
12400 sprintf (decode_mode_spec_buf, "%2d%%", total);
12401 return decode_mode_spec_buf;
12402 }
12403 }
12404
12405 /* Display percentage of size above the bottom of the screen. */
12406 case 'P':
12407 {
12408 int toppos = marker_position (w->start);
12409 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
12410 int total = BUF_ZV (b) - BUF_BEGV (b);
12411
12412 if (botpos >= BUF_ZV (b))
12413 {
12414 if (toppos <= BUF_BEGV (b))
12415 return "All";
12416 else
12417 return "Bottom";
12418 }
12419 else
12420 {
12421 if (total > 1000000)
12422 /* Do it differently for a large value, to avoid overflow. */
12423 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
12424 else
12425 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
12426 /* We can't normally display a 3-digit number,
12427 so get us a 2-digit number that is close. */
12428 if (total == 100)
12429 total = 99;
12430 if (toppos <= BUF_BEGV (b))
12431 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
12432 else
12433 sprintf (decode_mode_spec_buf, "%2d%%", total);
12434 return decode_mode_spec_buf;
12435 }
12436 }
12437
12438 case 's':
12439 /* status of process */
12440 obj = Fget_buffer_process (w->buffer);
12441 if (NILP (obj))
12442 return "no process";
12443 #ifdef subprocesses
12444 obj = Fsymbol_name (Fprocess_status (obj));
12445 #endif
12446 break;
12447
12448 case 't': /* indicate TEXT or BINARY */
12449 #ifdef MODE_LINE_BINARY_TEXT
12450 return MODE_LINE_BINARY_TEXT (b);
12451 #else
12452 return "T";
12453 #endif
12454
12455 case 'z':
12456 /* coding-system (not including end-of-line format) */
12457 case 'Z':
12458 /* coding-system (including end-of-line type) */
12459 {
12460 int eol_flag = (c == 'Z');
12461 char *p = decode_mode_spec_buf;
12462
12463 if (! FRAME_WINDOW_P (f))
12464 {
12465 /* No need to mention EOL here--the terminal never needs
12466 to do EOL conversion. */
12467 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
12468 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
12469 }
12470 p = decode_mode_spec_coding (b->buffer_file_coding_system,
12471 p, eol_flag);
12472
12473 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
12474 #ifdef subprocesses
12475 obj = Fget_buffer_process (Fcurrent_buffer ());
12476 if (PROCESSP (obj))
12477 {
12478 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
12479 p, eol_flag);
12480 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
12481 p, eol_flag);
12482 }
12483 #endif /* subprocesses */
12484 #endif /* 0 */
12485 *p = 0;
12486 return decode_mode_spec_buf;
12487 }
12488 }
12489
12490 if (STRINGP (obj))
12491 return (char *) XSTRING (obj)->data;
12492 else
12493 return "";
12494 }
12495
12496
12497 /* Count up to COUNT lines starting from START / START_BYTE.
12498 But don't go beyond LIMIT_BYTE.
12499 Return the number of lines thus found (always nonnegative).
12500
12501 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
12502
12503 static int
12504 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
12505 int start, start_byte, limit_byte, count;
12506 int *byte_pos_ptr;
12507 {
12508 register unsigned char *cursor;
12509 unsigned char *base;
12510
12511 register int ceiling;
12512 register unsigned char *ceiling_addr;
12513 int orig_count = count;
12514
12515 /* If we are not in selective display mode,
12516 check only for newlines. */
12517 int selective_display = (!NILP (current_buffer->selective_display)
12518 && !INTEGERP (current_buffer->selective_display));
12519
12520 if (count > 0)
12521 {
12522 while (start_byte < limit_byte)
12523 {
12524 ceiling = BUFFER_CEILING_OF (start_byte);
12525 ceiling = min (limit_byte - 1, ceiling);
12526 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
12527 base = (cursor = BYTE_POS_ADDR (start_byte));
12528 while (1)
12529 {
12530 if (selective_display)
12531 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
12532 ;
12533 else
12534 while (*cursor != '\n' && ++cursor != ceiling_addr)
12535 ;
12536
12537 if (cursor != ceiling_addr)
12538 {
12539 if (--count == 0)
12540 {
12541 start_byte += cursor - base + 1;
12542 *byte_pos_ptr = start_byte;
12543 return orig_count;
12544 }
12545 else
12546 if (++cursor == ceiling_addr)
12547 break;
12548 }
12549 else
12550 break;
12551 }
12552 start_byte += cursor - base;
12553 }
12554 }
12555 else
12556 {
12557 while (start_byte > limit_byte)
12558 {
12559 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
12560 ceiling = max (limit_byte, ceiling);
12561 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
12562 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
12563 while (1)
12564 {
12565 if (selective_display)
12566 while (--cursor != ceiling_addr
12567 && *cursor != '\n' && *cursor != 015)
12568 ;
12569 else
12570 while (--cursor != ceiling_addr && *cursor != '\n')
12571 ;
12572
12573 if (cursor != ceiling_addr)
12574 {
12575 if (++count == 0)
12576 {
12577 start_byte += cursor - base + 1;
12578 *byte_pos_ptr = start_byte;
12579 /* When scanning backwards, we should
12580 not count the newline posterior to which we stop. */
12581 return - orig_count - 1;
12582 }
12583 }
12584 else
12585 break;
12586 }
12587 /* Here we add 1 to compensate for the last decrement
12588 of CURSOR, which took it past the valid range. */
12589 start_byte += cursor - base + 1;
12590 }
12591 }
12592
12593 *byte_pos_ptr = limit_byte;
12594
12595 if (count < 0)
12596 return - orig_count + count;
12597 return orig_count - count;
12598
12599 }
12600
12601
12602 \f
12603 /***********************************************************************
12604 Displaying strings
12605 ***********************************************************************/
12606
12607 /* Display a NUL-terminated string, starting with index START.
12608
12609 If STRING is non-null, display that C string. Otherwise, the Lisp
12610 string LISP_STRING is displayed.
12611
12612 If FACE_STRING is not nil, FACE_STRING_POS is a position in
12613 FACE_STRING. Display STRING or LISP_STRING with the face at
12614 FACE_STRING_POS in FACE_STRING:
12615
12616 Display the string in the environment given by IT, but use the
12617 standard display table, temporarily.
12618
12619 FIELD_WIDTH is the minimum number of output glyphs to produce.
12620 If STRING has fewer characters than FIELD_WIDTH, pad to the right
12621 with spaces. If STRING has more characters, more than FIELD_WIDTH
12622 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
12623
12624 PRECISION is the maximum number of characters to output from
12625 STRING. PRECISION < 0 means don't truncate the string.
12626
12627 This is roughly equivalent to printf format specifiers:
12628
12629 FIELD_WIDTH PRECISION PRINTF
12630 ----------------------------------------
12631 -1 -1 %s
12632 -1 10 %.10s
12633 10 -1 %10s
12634 20 10 %20.10s
12635
12636 MULTIBYTE zero means do not display multibyte chars, > 0 means do
12637 display them, and < 0 means obey the current buffer's value of
12638 enable_multibyte_characters.
12639
12640 Value is the number of glyphs produced. */
12641
12642 static int
12643 display_string (string, lisp_string, face_string, face_string_pos,
12644 start, it, field_width, precision, max_x, multibyte)
12645 unsigned char *string;
12646 Lisp_Object lisp_string;
12647 Lisp_Object face_string;
12648 int face_string_pos;
12649 int start;
12650 struct it *it;
12651 int field_width, precision, max_x;
12652 int multibyte;
12653 {
12654 int hpos_at_start = it->hpos;
12655 int saved_face_id = it->face_id;
12656 struct glyph_row *row = it->glyph_row;
12657
12658 /* Initialize the iterator IT for iteration over STRING beginning
12659 with index START. We assume that IT may be modified here (which
12660 means that display_line has to do something when displaying a
12661 mini-buffer prompt, which it does). */
12662 reseat_to_string (it, string, lisp_string, start,
12663 precision, field_width, multibyte);
12664
12665 /* If displaying STRING, set up the face of the iterator
12666 from LISP_STRING, if that's given. */
12667 if (STRINGP (face_string))
12668 {
12669 int endptr;
12670 struct face *face;
12671
12672 it->face_id
12673 = face_at_string_position (it->w, face_string, face_string_pos,
12674 0, it->region_beg_charpos,
12675 it->region_end_charpos,
12676 &endptr, it->base_face_id);
12677 face = FACE_FROM_ID (it->f, it->face_id);
12678 it->face_box_p = face->box != FACE_NO_BOX;
12679 }
12680
12681 /* Set max_x to the maximum allowed X position. Don't let it go
12682 beyond the right edge of the window. */
12683 if (max_x <= 0)
12684 max_x = it->last_visible_x;
12685 else
12686 max_x = min (max_x, it->last_visible_x);
12687
12688 /* Skip over display elements that are not visible. because IT->w is
12689 hscrolled. */
12690 if (it->current_x < it->first_visible_x)
12691 move_it_in_display_line_to (it, 100000, it->first_visible_x,
12692 MOVE_TO_POS | MOVE_TO_X);
12693
12694 row->ascent = it->max_ascent;
12695 row->height = it->max_ascent + it->max_descent;
12696 row->phys_ascent = it->max_phys_ascent;
12697 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
12698
12699 /* This condition is for the case that we are called with current_x
12700 past last_visible_x. */
12701 while (it->current_x < max_x)
12702 {
12703 int x_before, x, n_glyphs_before, i, nglyphs;
12704
12705 /* Get the next display element. */
12706 if (!get_next_display_element (it))
12707 break;
12708
12709 /* Produce glyphs. */
12710 x_before = it->current_x;
12711 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
12712 PRODUCE_GLYPHS (it);
12713
12714 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
12715 i = 0;
12716 x = x_before;
12717 while (i < nglyphs)
12718 {
12719 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12720
12721 if (!it->truncate_lines_p
12722 && x + glyph->pixel_width > max_x)
12723 {
12724 /* End of continued line or max_x reached. */
12725 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
12726 it->current_x = x;
12727 break;
12728 }
12729 else if (x + glyph->pixel_width > it->first_visible_x)
12730 {
12731 /* Glyph is at least partially visible. */
12732 ++it->hpos;
12733 if (x < it->first_visible_x)
12734 it->glyph_row->x = x - it->first_visible_x;
12735 }
12736 else
12737 {
12738 /* Glyph is off the left margin of the display area.
12739 Should not happen. */
12740 abort ();
12741 }
12742
12743 row->ascent = max (row->ascent, it->max_ascent);
12744 row->height = max (row->height, it->max_ascent + it->max_descent);
12745 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12746 row->phys_height = max (row->phys_height,
12747 it->max_phys_ascent + it->max_phys_descent);
12748 x += glyph->pixel_width;
12749 ++i;
12750 }
12751
12752 /* Stop if max_x reached. */
12753 if (i < nglyphs)
12754 break;
12755
12756 /* Stop at line ends. */
12757 if (ITERATOR_AT_END_OF_LINE_P (it))
12758 {
12759 it->continuation_lines_width = 0;
12760 break;
12761 }
12762
12763 set_iterator_to_next (it);
12764
12765 /* Stop if truncating at the right edge. */
12766 if (it->truncate_lines_p
12767 && it->current_x >= it->last_visible_x)
12768 {
12769 /* Add truncation mark, but don't do it if the line is
12770 truncated at a padding space. */
12771 if (IT_CHARPOS (*it) < it->string_nchars)
12772 {
12773 if (!FRAME_WINDOW_P (it->f))
12774 produce_special_glyphs (it, IT_TRUNCATION);
12775 it->glyph_row->truncated_on_right_p = 1;
12776 }
12777 break;
12778 }
12779 }
12780
12781 /* Maybe insert a truncation at the left. */
12782 if (it->first_visible_x
12783 && IT_CHARPOS (*it) > 0)
12784 {
12785 if (!FRAME_WINDOW_P (it->f))
12786 insert_left_trunc_glyphs (it);
12787 it->glyph_row->truncated_on_left_p = 1;
12788 }
12789
12790 it->face_id = saved_face_id;
12791
12792 /* Value is number of columns displayed. */
12793 return it->hpos - hpos_at_start;
12794 }
12795
12796
12797 \f
12798 /* This is like a combination of memq and assq. Return 1 if PROPVAL
12799 appears as an element of LIST or as the car of an element of LIST.
12800 If PROPVAL is a list, compare each element against LIST in that
12801 way, and return 1 if any element of PROPVAL is found in LIST.
12802 Otherwise return 0. This function cannot quit. */
12803
12804 int
12805 invisible_p (propval, list)
12806 register Lisp_Object propval;
12807 Lisp_Object list;
12808 {
12809 register Lisp_Object tail, proptail;
12810 for (tail = list; CONSP (tail); tail = XCDR (tail))
12811 {
12812 register Lisp_Object tem;
12813 tem = XCAR (tail);
12814 if (EQ (propval, tem))
12815 return 1;
12816 if (CONSP (tem) && EQ (propval, XCAR (tem)))
12817 return 1;
12818 }
12819 if (CONSP (propval))
12820 for (proptail = propval; CONSP (proptail);
12821 proptail = XCDR (proptail))
12822 {
12823 Lisp_Object propelt;
12824 propelt = XCAR (proptail);
12825 for (tail = list; CONSP (tail); tail = XCDR (tail))
12826 {
12827 register Lisp_Object tem;
12828 tem = XCAR (tail);
12829 if (EQ (propelt, tem))
12830 return 1;
12831 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
12832 return 1;
12833 }
12834 }
12835 return 0;
12836 }
12837
12838
12839 /* Return 1 if PROPVAL appears as the car of an element of LIST and
12840 the cdr of that element is non-nil. If PROPVAL is a list, check
12841 each element of PROPVAL in that way, and the first time some
12842 element is found, return 1 if the cdr of that element is non-nil.
12843 Otherwise return 0. This function cannot quit. */
12844
12845 int
12846 invisible_ellipsis_p (propval, list)
12847 register Lisp_Object propval;
12848 Lisp_Object list;
12849 {
12850 register Lisp_Object tail, proptail;
12851
12852 for (tail = list; CONSP (tail); tail = XCDR (tail))
12853 {
12854 register Lisp_Object tem;
12855 tem = XCAR (tail);
12856 if (CONSP (tem) && EQ (propval, XCAR (tem)))
12857 return ! NILP (XCDR (tem));
12858 }
12859
12860 if (CONSP (propval))
12861 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
12862 {
12863 Lisp_Object propelt;
12864 propelt = XCAR (proptail);
12865 for (tail = list; CONSP (tail); tail = XCDR (tail))
12866 {
12867 register Lisp_Object tem;
12868 tem = XCAR (tail);
12869 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
12870 return ! NILP (XCDR (tem));
12871 }
12872 }
12873
12874 return 0;
12875 }
12876
12877
12878 \f
12879 /***********************************************************************
12880 Initialization
12881 ***********************************************************************/
12882
12883 void
12884 syms_of_xdisp ()
12885 {
12886 Vwith_echo_area_save_vector = Qnil;
12887 staticpro (&Vwith_echo_area_save_vector);
12888
12889 Vmessage_stack = Qnil;
12890 staticpro (&Vmessage_stack);
12891
12892 Qinhibit_redisplay = intern ("inhibit-redisplay");
12893 staticpro (&Qinhibit_redisplay);
12894
12895 #if GLYPH_DEBUG
12896 defsubr (&Sdump_glyph_matrix);
12897 defsubr (&Sdump_glyph_row);
12898 defsubr (&Sdump_tool_bar_row);
12899 defsubr (&Strace_redisplay_toggle);
12900 defsubr (&Strace_to_stderr);
12901 #endif
12902
12903 staticpro (&Qmenu_bar_update_hook);
12904 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
12905
12906 staticpro (&Qoverriding_terminal_local_map);
12907 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
12908
12909 staticpro (&Qoverriding_local_map);
12910 Qoverriding_local_map = intern ("overriding-local-map");
12911
12912 staticpro (&Qwindow_scroll_functions);
12913 Qwindow_scroll_functions = intern ("window-scroll-functions");
12914
12915 staticpro (&Qredisplay_end_trigger_functions);
12916 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
12917
12918 staticpro (&Qinhibit_point_motion_hooks);
12919 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
12920
12921 QCdata = intern (":data");
12922 staticpro (&QCdata);
12923 Qdisplay = intern ("display");
12924 staticpro (&Qdisplay);
12925 Qspace_width = intern ("space-width");
12926 staticpro (&Qspace_width);
12927 Qraise = intern ("raise");
12928 staticpro (&Qraise);
12929 Qspace = intern ("space");
12930 staticpro (&Qspace);
12931 Qmargin = intern ("margin");
12932 staticpro (&Qmargin);
12933 Qleft_margin = intern ("left-margin");
12934 staticpro (&Qleft_margin);
12935 Qright_margin = intern ("right-margin");
12936 staticpro (&Qright_margin);
12937 Qalign_to = intern ("align-to");
12938 staticpro (&Qalign_to);
12939 QCalign_to = intern (":align-to");
12940 staticpro (&QCalign_to);
12941 Qrelative_width = intern ("relative-width");
12942 staticpro (&Qrelative_width);
12943 QCrelative_width = intern (":relative-width");
12944 staticpro (&QCrelative_width);
12945 QCrelative_height = intern (":relative-height");
12946 staticpro (&QCrelative_height);
12947 QCeval = intern (":eval");
12948 staticpro (&QCeval);
12949 Qwhen = intern ("when");
12950 staticpro (&Qwhen);
12951 QCfile = intern (":file");
12952 staticpro (&QCfile);
12953 Qfontified = intern ("fontified");
12954 staticpro (&Qfontified);
12955 Qfontification_functions = intern ("fontification-functions");
12956 staticpro (&Qfontification_functions);
12957 Qtrailing_whitespace = intern ("trailing-whitespace");
12958 staticpro (&Qtrailing_whitespace);
12959 Qimage = intern ("image");
12960 staticpro (&Qimage);
12961
12962 last_arrow_position = Qnil;
12963 last_arrow_string = Qnil;
12964 staticpro (&last_arrow_position);
12965 staticpro (&last_arrow_string);
12966
12967 echo_buffer[0] = echo_buffer[1] = Qnil;
12968 staticpro (&echo_buffer[0]);
12969 staticpro (&echo_buffer[1]);
12970
12971 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
12972 staticpro (&echo_area_buffer[0]);
12973 staticpro (&echo_area_buffer[1]);
12974
12975 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
12976 "Non-nil means highlight trailing whitespace.\n\
12977 The face used for trailing whitespace is `trailing-whitespace'.");
12978 Vshow_trailing_whitespace = Qnil;
12979
12980 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
12981 "Non-nil means don't actually do any redisplay.\n\
12982 This is used for internal purposes.");
12983 Vinhibit_redisplay = Qnil;
12984
12985 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
12986 "String (or mode line construct) included (normally) in `mode-line-format'.");
12987 Vglobal_mode_string = Qnil;
12988
12989 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
12990 "Marker for where to display an arrow on top of the buffer text.\n\
12991 This must be the beginning of a line in order to work.\n\
12992 See also `overlay-arrow-string'.");
12993 Voverlay_arrow_position = Qnil;
12994
12995 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
12996 "String to display as an arrow. See also `overlay-arrow-position'.");
12997 Voverlay_arrow_string = Qnil;
12998
12999 DEFVAR_INT ("scroll-step", &scroll_step,
13000 "*The number of lines to try scrolling a window by when point moves out.\n\
13001 If that fails to bring point back on frame, point is centered instead.\n\
13002 If this is zero, point is always centered after it moves off frame.");
13003
13004 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
13005 "*Scroll up to this many lines, to bring point back on screen.\n\
13006 A value of zero means to scroll the text to center point vertically\n\
13007 in the window.");
13008 scroll_conservatively = 0;
13009
13010 DEFVAR_INT ("scroll-margin", &scroll_margin,
13011 "*Number of lines of margin at the top and bottom of a window.\n\
13012 Recenter the window whenever point gets within this many lines\n\
13013 of the top or bottom of the window.");
13014 scroll_margin = 0;
13015
13016 #if GLYPH_DEBUG
13017 DEFVAR_INT ("debug-end-pos", &debug_end_pos, "Don't ask");
13018 #endif
13019
13020 DEFVAR_BOOL ("truncate-partial-width-windows",
13021 &truncate_partial_width_windows,
13022 "*Non-nil means truncate lines in all windows less than full frame wide.");
13023 truncate_partial_width_windows = 1;
13024
13025 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
13026 "*Non-nil means use inverse video for the mode line.");
13027 mode_line_inverse_video = 1;
13028
13029 DEFVAR_INT ("line-number-display-limit", &line_number_display_limit,
13030 "*Maximum buffer size for which line number should be displayed.\n\
13031 If the buffer is bigger than this, the line number does not appear\n\
13032 in the mode line.");
13033 line_number_display_limit = 1000000;
13034
13035 DEFVAR_INT ("line-number-display-limit-width", &line_number_display_limit_width,
13036 "*Maximum line width (in characters) for line number display.\n\
13037 If the average length of the lines near point is bigger than this, then the\n\
13038 line number may be omitted from the mode line.");
13039 line_number_display_limit_width = 200;
13040
13041 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
13042 "*Non-nil means highlight region even in nonselected windows.");
13043 highlight_nonselected_windows = 0;
13044
13045 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
13046 "Non-nil if more than one frame is visible on this display.\n\
13047 Minibuffer-only frames don't count, but iconified frames do.\n\
13048 This variable is not guaranteed to be accurate except while processing\n\
13049 `frame-title-format' and `icon-title-format'.");
13050
13051 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
13052 "Template for displaying the title bar of visible frames.\n\
13053 \(Assuming the window manager supports this feature.)\n\
13054 This variable has the same structure as `mode-line-format' (which see),\n\
13055 and is used only on frames for which no explicit name has been set\n\
13056 \(see `modify-frame-parameters').");
13057 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
13058 "Template for displaying the title bar of an iconified frame.\n\
13059 \(Assuming the window manager supports this feature.)\n\
13060 This variable has the same structure as `mode-line-format' (which see),\n\
13061 and is used only on frames for which no explicit name has been set\n\
13062 \(see `modify-frame-parameters').");
13063 Vicon_title_format
13064 = Vframe_title_format
13065 = Fcons (intern ("multiple-frames"),
13066 Fcons (build_string ("%b"),
13067 Fcons (Fcons (build_string (""),
13068 Fcons (intern ("invocation-name"),
13069 Fcons (build_string ("@"),
13070 Fcons (intern ("system-name"),
13071 Qnil)))),
13072 Qnil)));
13073
13074 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
13075 "Maximum number of lines to keep in the message log buffer.\n\
13076 If nil, disable message logging. If t, log messages but don't truncate\n\
13077 the buffer when it becomes large.");
13078 XSETFASTINT (Vmessage_log_max, 50);
13079
13080 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
13081 "Functions called before redisplay, if window sizes have changed.\n\
13082 The value should be a list of functions that take one argument.\n\
13083 Just before redisplay, for each frame, if any of its windows have changed\n\
13084 size since the last redisplay, or have been split or deleted,\n\
13085 all the functions in the list are called, with the frame as argument.");
13086 Vwindow_size_change_functions = Qnil;
13087
13088 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
13089 "List of Functions to call before redisplaying a window with scrolling.\n\
13090 Each function is called with two arguments, the window\n\
13091 and its new display-start position. Note that the value of `window-end'\n\
13092 is not valid when these functions are called.");
13093 Vwindow_scroll_functions = Qnil;
13094
13095 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
13096 "*Non-nil means automatically resize tool-bars.\n\
13097 This increases a tool-bar's height if not all tool-bar items are visible.\n\
13098 It decreases a tool-bar's height when it would display blank lines\n\
13099 otherwise.");
13100 auto_resize_tool_bars_p = 1;
13101
13102 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
13103 "*Non-nil means raise tool-bar buttons when the mouse moves over them.");
13104 auto_raise_tool_bar_buttons_p = 1;
13105
13106 DEFVAR_INT ("tool-bar-button-margin", &tool_bar_button_margin,
13107 "*Margin around tool-bar buttons in pixels.");
13108 tool_bar_button_margin = 1;
13109
13110 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
13111 "Relief thickness of tool-bar buttons.");
13112 tool_bar_button_relief = 3;
13113
13114 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
13115 "List of functions to call to fontify regions of text.\n\
13116 Each function is called with one argument POS. Functions must\n\
13117 fontify a region starting at POS in the current buffer, and give\n\
13118 fontified regions the property `fontified'.\n\
13119 This variable automatically becomes buffer-local when set.");
13120 Vfontification_functions = Qnil;
13121 Fmake_local_variable (Qfontification_functions);
13122
13123 DEFVAR_BOOL ("unibyte-display-via-language-environment",
13124 &unibyte_display_via_language_environment,
13125 "*Non-nil means display unibyte text according to language environment.\n\
13126 Specifically this means that unibyte non-ASCII characters\n\
13127 are displayed by converting them to the equivalent multibyte characters\n\
13128 according to the current language environment. As a result, they are\n\
13129 displayed according to the current fontset.");
13130 unibyte_display_via_language_environment = 0;
13131
13132 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
13133 "*Maximum height for resizing mini-windows.\n\
13134 If a float, it specifies a fraction of the mini-window frame's height.\n\
13135 If an integer, it specifies a number of lines.\n\
13136 If nil, don't resize.");
13137 Vmax_mini_window_height = make_float (0.25);
13138
13139 DEFVAR_BOOL ("cursor-in-non-selected-windows",
13140 &cursor_in_non_selected_windows,
13141 "*Non-nil means display a hollow cursor in non-selected windows.\n\
13142 Nil means don't display a cursor there.");
13143 cursor_in_non_selected_windows = 1;
13144
13145 DEFVAR_BOOL ("automatic-hscrolling", &automatic_hscrolling_p,
13146 "*Non-nil means scroll the display automatically to make point visible.");
13147 automatic_hscrolling_p = 1;
13148
13149 DEFVAR_LISP ("image-types", &Vimage_types,
13150 "List of supported image types.\n\
13151 Each element of the list is a symbol for a supported image type.");
13152 Vimage_types = Qnil;
13153 }
13154
13155
13156 /* Initialize this module when Emacs starts. */
13157
13158 void
13159 init_xdisp ()
13160 {
13161 Lisp_Object root_window;
13162 struct window *mini_w;
13163
13164 CHARPOS (this_line_start_pos) = 0;
13165
13166 mini_w = XWINDOW (minibuf_window);
13167 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
13168
13169 if (!noninteractive)
13170 {
13171 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
13172 int i;
13173
13174 XSETFASTINT (XWINDOW (root_window)->top, FRAME_TOP_MARGIN (f));
13175 set_window_height (root_window,
13176 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
13177 0);
13178 XSETFASTINT (mini_w->top, FRAME_HEIGHT (f) - 1);
13179 set_window_height (minibuf_window, 1, 0);
13180
13181 XSETFASTINT (XWINDOW (root_window)->width, FRAME_WIDTH (f));
13182 XSETFASTINT (mini_w->width, FRAME_WIDTH (f));
13183
13184 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
13185 scratch_glyph_row.glyphs[TEXT_AREA + 1]
13186 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
13187
13188 /* The default ellipsis glyphs `...'. */
13189 for (i = 0; i < 3; ++i)
13190 XSETFASTINT (default_invis_vector[i], '.');
13191 }
13192
13193 #ifdef HAVE_WINDOW_SYSTEM
13194 {
13195 /* Allocate the buffer for frame titles. */
13196 int size = 100;
13197 frame_title_buf = (char *) xmalloc (size);
13198 frame_title_buf_end = frame_title_buf + size;
13199 frame_title_ptr = NULL;
13200 }
13201 #endif /* HAVE_WINDOW_SYSTEM */
13202 }
13203
13204