]> code.delx.au - gnu-emacs/blob - src/xdisp.c
(make_cursor_line_fully_visible): Handle case of rows
[gnu-emacs] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 86, 87, 88, 93, 94, 95, 97, 98, 99
3 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
23
24 Redisplay.
25
26 Emacs separates the task of updating the display from code
27 modifying global state, e.g. buffer text. This way functions
28 operating on buffers don't also have to be concerned with updating
29 the display.
30
31 Updating the display is triggered by the Lisp interpreter when it
32 decides it's time to do it. This is done either automatically for
33 you as part of the interpreter's command loop or as the result of
34 calling Lisp functions like `sit-for'. The C function `redisplay'
35 in xdisp.c is the only entry into the inner redisplay code. (Or,
36 let's say almost---see the the description of direct update
37 operations, below.).
38
39 The following diagram shows how redisplay code is invoked. As you
40 can see, Lisp calls redisplay and vice versa. Under window systems
41 like X, some portions of the redisplay code are also called
42 asynchronously during mouse movement or expose events. It is very
43 important that these code parts do NOT use the C library (malloc,
44 free) because many C libraries under Unix are not reentrant. They
45 may also NOT call functions of the Lisp interpreter which could
46 change the interpreter's state. If you don't follow these rules,
47 you will encounter bugs which are very hard to explain.
48
49 (Direct functions, see below)
50 direct_output_for_insert,
51 direct_forward_char (dispnew.c)
52 +---------------------------------+
53 | |
54 | V
55 +--------------+ redisplay() +----------------+
56 | Lisp machine |---------------->| Redisplay code |<--+
57 +--------------+ (xdisp.c) +----------------+ |
58 ^ | |
59 +----------------------------------+ |
60 Don't use this path when called |
61 asynchronously! |
62 |
63 expose_window (asynchronous) |
64 |
65 X expose events -----+
66
67 What does redisplay? Obviously, it has to figure out somehow what
68 has been changed since the last time the display has been updated,
69 and to make these changes visible. Preferably it would do that in
70 a moderately intelligent way, i.e. fast.
71
72 Changes in buffer text can be deduced from window and buffer
73 structures, and from some global variables like `beg_unchanged' and
74 `end_unchanged'. The contents of the display are additionally
75 recorded in a `glyph matrix', a two-dimensional matrix of glyph
76 structures. Each row in such a matrix corresponds to a line on the
77 display, and each glyph in a row corresponds to a column displaying
78 a character, an image, or what else. This matrix is called the
79 `current glyph matrix' or `current matrix' in redisplay
80 terminology.
81
82 For buffer parts that have been changed since the last update, a
83 second glyph matrix is constructed, the so called `desired glyph
84 matrix' or short `desired matrix'. Current and desired matrix are
85 then compared to find a cheap way to update the display, e.g. by
86 reusing part of the display by scrolling lines.
87
88
89 Direct operations.
90
91 You will find a lot of of redisplay optimizations when you start
92 looking at the innards of redisplay. The overall goal of all these
93 optimizations is to make redisplay fast because it is done
94 frequently.
95
96 Two optimizations are not found in xdisp.c. These are the direct
97 operations mentioned above. As the name suggests they follow a
98 different principle than the rest of redisplay. Instead of
99 building a desired matrix and then comparing it with the current
100 display, they perform their actions directly on the display and on
101 the current matrix.
102
103 One direct operation updates the display after one character has
104 been entered. The other one moves the cursor by one position
105 forward or backward. You find these functions under the names
106 `direct_output_for_insert' and `direct_output_forward_char' in
107 dispnew.c.
108
109
110 Desired matrices.
111
112 Desired matrices are always built per Emacs window. The function
113 `display_line' is the central function to look at if you are
114 interested. It constructs one row in a desired matrix given an
115 iterator structure containing both a buffer position and a
116 description of the environment in which the text is to be
117 displayed. But this is too early, read on.
118
119 Characters and pixmaps displayed for a range of buffer text depend
120 on various settings of buffers and windows, on overlays and text
121 properties, on display tables, on selective display. The good news
122 is that all this hairy stuff is hidden behind a small set of
123 interface functions taking a iterator structure (struct it)
124 argument.
125
126 Iteration over things to be be displayed is then simple. It is
127 started by initializing an iterator with a call to init_iterator
128 (or init_string_iterator for that matter). Calls to
129 get_next_display_element fill the iterator structure with relevant
130 information about the next thing to display. Calls to
131 set_iterator_to_next move the iterator to the next thing.
132
133 Besides this, an iterator also contains information about the
134 display environment in which glyphs for display elements are to be
135 produced. It has fields for the width and height of the display,
136 the information whether long lines are truncated or continued, a
137 current X and Y position, and lots of other stuff you can better
138 see in dispextern.h.
139
140 Glyphs in a desired matrix are normally constructed in a loop
141 calling get_next_display_element and then produce_glyphs. The call
142 to produce_glyphs will fill the iterator structure with pixel
143 information about the element being displayed and at the same time
144 produce glyphs for it. If the display element fits on the line
145 being displayed, set_iterator_to_next is called next, otherwise the
146 glyphs produced are discarded.
147
148
149 Frame matrices.
150
151 That just couldn't be all, could it? What about terminal types not
152 supporting operations on sub-windows of the screen? To update the
153 display on such a terminal, window-based glyph matrices are not
154 well suited. To be able to reuse part of the display (scrolling
155 lines up and down), we must instead have a view of the whole
156 screen. This is what `frame matrices' are for. They are a trick.
157
158 Frames on terminals like above have a glyph pool. Windows on such
159 a frame sub-allocate their glyph memory from their frame's glyph
160 pool. The frame itself is given its own glyph matrices. By
161 coincidence---or maybe something else---rows in window glyph
162 matrices are slices of corresponding rows in frame matrices. Thus
163 writing to window matrices implicitly updates a frame matrix which
164 provides us with the view of the whole screen that we originally
165 wanted to have without having to move many bytes around. To be
166 honest, there is a little bit more done, but not much more. If you
167 plan to extend that code, take a look at dispnew.c. The function
168 build_frame_matrix is a good starting point. */
169
170 #include <config.h>
171 #include <stdio.h>
172 #include "lisp.h"
173 #include "frame.h"
174 #include "window.h"
175 #include "termchar.h"
176 #include "dispextern.h"
177 #include "buffer.h"
178 #include "charset.h"
179 #include "indent.h"
180 #include "commands.h"
181 #include "macros.h"
182 #include "disptab.h"
183 #include "termhooks.h"
184 #include "intervals.h"
185 #include "keyboard.h"
186 #include "coding.h"
187 #include "process.h"
188 #include "region-cache.h"
189
190 #ifdef HAVE_X_WINDOWS
191 #include "xterm.h"
192 #endif
193 #ifdef WINDOWSNT
194 #include "w32term.h"
195 #endif
196
197 #define min(a, b) ((a) < (b) ? (a) : (b))
198 #define max(a, b) ((a) > (b) ? (a) : (b))
199
200 #define INFINITY 10000000
201
202 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
203 extern void set_frame_menubar ();
204 extern int pending_menu_activation;
205 #endif
206
207 extern int interrupt_input;
208 extern int command_loop_level;
209
210 extern int minibuffer_auto_raise;
211
212 extern Lisp_Object Qface;
213
214 extern Lisp_Object Voverriding_local_map;
215 extern Lisp_Object Voverriding_local_map_menu_flag;
216 extern Lisp_Object Qmenu_item;
217
218 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
219 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
220 Lisp_Object Qredisplay_end_trigger_functions;
221 Lisp_Object Qinhibit_point_motion_hooks;
222 Lisp_Object QCeval, Qwhen, QCfile, QCdata;
223 Lisp_Object Qfontified;
224
225 /* Functions called to fontify regions of text. */
226
227 Lisp_Object Vfontification_functions;
228 Lisp_Object Qfontification_functions;
229
230 /* Non-zero means draw tool bar buttons raised when the mouse moves
231 over them. */
232
233 int auto_raise_tool_bar_buttons_p;
234
235 /* Margin around tool bar buttons in pixels. */
236
237 int tool_bar_button_margin;
238
239 /* Thickness of shadow to draw around tool bar buttons. */
240
241 int tool_bar_button_relief;
242
243 /* Non-zero means automatically resize tool-bars so that all tool-bar
244 items are visible, and no blank lines remain. */
245
246 int auto_resize_tool_bars_p;
247
248 /* Non-nil means don't actually do any redisplay. */
249
250 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
251
252 /* Names of text properties relevant for redisplay. */
253
254 Lisp_Object Qdisplay, Qrelative_width, Qalign_to;
255 extern Lisp_Object Qface, Qinvisible, Qimage, Qwidth;
256
257 /* Symbols used in text property values. */
258
259 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
260 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
261 Lisp_Object Qmargin;
262 extern Lisp_Object Qheight;
263
264 /* Non-nil means highlight trailing whitespace. */
265
266 Lisp_Object Vshow_trailing_whitespace;
267
268 /* Name of the face used to highlight trailing whitespace. */
269
270 Lisp_Object Qtrailing_whitespace;
271
272 /* The symbol `image' which is the car of the lists used to represent
273 images in Lisp. */
274
275 Lisp_Object Qimage;
276
277 /* Non-zero means print newline to stdout before next mini-buffer
278 message. */
279
280 int noninteractive_need_newline;
281
282 /* Non-zero means print newline to message log before next message. */
283
284 static int message_log_need_newline;
285
286 \f
287 /* The buffer position of the first character appearing entirely or
288 partially on the line of the selected window which contains the
289 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
290 redisplay optimization in redisplay_internal. */
291
292 static struct text_pos this_line_start_pos;
293
294 /* Number of characters past the end of the line above, including the
295 terminating newline. */
296
297 static struct text_pos this_line_end_pos;
298
299 /* The vertical positions and the height of this line. */
300
301 static int this_line_vpos;
302 static int this_line_y;
303 static int this_line_pixel_height;
304
305 /* X position at which this display line starts. Usually zero;
306 negative if first character is partially visible. */
307
308 static int this_line_start_x;
309
310 /* Buffer that this_line_.* variables are referring to. */
311
312 static struct buffer *this_line_buffer;
313
314 /* Nonzero means truncate lines in all windows less wide than the
315 frame. */
316
317 int truncate_partial_width_windows;
318
319 /* A flag to control how to display unibyte 8-bit character. */
320
321 int unibyte_display_via_language_environment;
322
323 /* Nonzero means we have more than one non-mini-buffer-only frame.
324 Not guaranteed to be accurate except while parsing
325 frame-title-format. */
326
327 int multiple_frames;
328
329 Lisp_Object Vglobal_mode_string;
330
331 /* Marker for where to display an arrow on top of the buffer text. */
332
333 Lisp_Object Voverlay_arrow_position;
334
335 /* String to display for the arrow. Only used on terminal frames. */
336
337 Lisp_Object Voverlay_arrow_string;
338
339 /* Values of those variables at last redisplay. However, if
340 Voverlay_arrow_position is a marker, last_arrow_position is its
341 numerical position. */
342
343 static Lisp_Object last_arrow_position, last_arrow_string;
344
345 /* Like mode-line-format, but for the title bar on a visible frame. */
346
347 Lisp_Object Vframe_title_format;
348
349 /* Like mode-line-format, but for the title bar on an iconified frame. */
350
351 Lisp_Object Vicon_title_format;
352
353 /* List of functions to call when a window's size changes. These
354 functions get one arg, a frame on which one or more windows' sizes
355 have changed. */
356
357 static Lisp_Object Vwindow_size_change_functions;
358
359 Lisp_Object Qmenu_bar_update_hook;
360
361 /* Nonzero if overlay arrow has been displayed once in this window. */
362
363 static int overlay_arrow_seen;
364
365 /* Nonzero means highlight the region even in nonselected windows. */
366
367 int highlight_nonselected_windows;
368
369 /* If cursor motion alone moves point off frame, try scrolling this
370 many lines up or down if that will bring it back. */
371
372 static int scroll_step;
373
374 /* Non-0 means scroll just far enough to bring point back on the
375 screen, when appropriate. */
376
377 static int scroll_conservatively;
378
379 /* Recenter the window whenever point gets within this many lines of
380 the top or bottom of the window. This value is translated into a
381 pixel value by multiplying it with CANON_Y_UNIT, which means that
382 there is really a fixed pixel height scroll margin. */
383
384 int scroll_margin;
385
386 /* Number of windows showing the buffer of the selected window (or
387 another buffer with the same base buffer). keyboard.c refers to
388 this. */
389
390 int buffer_shared;
391
392 /* Vector containing glyphs for an ellipsis `...'. */
393
394 static Lisp_Object default_invis_vector[3];
395
396 /* Nonzero means display mode line highlighted. */
397
398 int mode_line_inverse_video;
399
400 /* Prompt to display in front of the mini-buffer contents. */
401
402 Lisp_Object minibuf_prompt;
403
404 /* Width of current mini-buffer prompt. Only set after display_line
405 of the line that contains the prompt. */
406
407 int minibuf_prompt_width;
408 int minibuf_prompt_pixel_width;
409
410 /* This is the window where the echo area message was displayed. It
411 is always a mini-buffer window, but it may not be the same window
412 currently active as a mini-buffer. */
413
414 Lisp_Object echo_area_window;
415
416 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
417 pushes the current message and the value of
418 message_enable_multibyte on the stack, the function restore_message
419 pops the stack and displays MESSAGE again. */
420
421 Lisp_Object Vmessage_stack;
422
423 /* Nonzero means multibyte characters were enabled when the echo area
424 message was specified. */
425
426 int message_enable_multibyte;
427
428 /* True if we should redraw the mode lines on the next redisplay. */
429
430 int update_mode_lines;
431
432 /* Nonzero if window sizes or contents have changed since last
433 redisplay that finished */
434
435 int windows_or_buffers_changed;
436
437 /* Nonzero after display_mode_line if %l was used and it displayed a
438 line number. */
439
440 int line_number_displayed;
441
442 /* Maximum buffer size for which to display line numbers. */
443
444 static int line_number_display_limit;
445
446 /* line width to consider when repostioning for line number display */
447
448 static int line_number_display_limit_width;
449
450 /* Number of lines to keep in the message log buffer. t means
451 infinite. nil means don't log at all. */
452
453 Lisp_Object Vmessage_log_max;
454
455 /* Current, index 0, and last displayed echo area message. Either
456 buffers from echo_buffers, or nil to indicate no message. */
457
458 Lisp_Object echo_area_buffer[2];
459
460 /* The buffers referenced from echo_area_buffer. */
461
462 static Lisp_Object echo_buffer[2];
463
464 /* A vector saved used in with_area_buffer to reduce consing. */
465
466 static Lisp_Object Vwith_echo_area_save_vector;
467
468 /* Non-zero means display_echo_area should display the last echo area
469 message again. Set by redisplay_preserve_echo_area. */
470
471 static int display_last_displayed_message_p;
472
473 /* Nonzero if echo area is being used by print; zero if being used by
474 message. */
475
476 int message_buf_print;
477
478 /* Maximum height for resizing mini-windows. Either a float
479 specifying a fraction of the available height, or an integer
480 specifying a number of lines. */
481
482 static Lisp_Object Vmax_mini_window_height;
483
484 /* Non-zero means we want a hollow cursor in windows that are not
485 selected. Zero means there's no cursor in such windows. */
486
487 int cursor_in_non_selected_windows;
488
489 /* A scratch glyph row with contents used for generating truncation
490 glyphs. Also used in direct_output_for_insert. */
491
492 #define MAX_SCRATCH_GLYPHS 100
493 struct glyph_row scratch_glyph_row;
494 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
495
496 /* Ascent and height of the last line processed by move_it_to. */
497
498 static int last_max_ascent, last_height;
499
500 /* The maximum distance to look ahead for text properties. Values
501 that are too small let us call compute_char_face and similar
502 functions too often which is expensive. Values that are too large
503 let us call compute_char_face and alike too often because we
504 might not be interested in text properties that far away. */
505
506 #define TEXT_PROP_DISTANCE_LIMIT 100
507
508 /* Non-zero means print traces of redisplay if compiled with
509 GLYPH_DEBUG != 0. */
510
511 #if GLYPH_DEBUG
512 int trace_redisplay_p;
513 #endif
514
515 /* Non-zero means automatically scroll windows horizontally to make
516 point visible. */
517
518 int automatic_hscrolling_p;
519
520 /* Value returned from text property handlers (see below). */
521
522 enum prop_handled
523 {
524 HANDLED_NORMALLY,
525 HANDLED_RECOMPUTE_PROPS,
526 HANDLED_OVERLAY_STRING_CONSUMED,
527 HANDLED_RETURN
528 };
529
530 /* A description of text properties that redisplay is interested
531 in. */
532
533 struct props
534 {
535 /* The name of the property. */
536 Lisp_Object *name;
537
538 /* A unique index for the property. */
539 enum prop_idx idx;
540
541 /* A handler function called to set up iterator IT from the property
542 at IT's current position. Value is used to steer handle_stop. */
543 enum prop_handled (*handler) P_ ((struct it *it));
544 };
545
546 static enum prop_handled handle_face_prop P_ ((struct it *));
547 static enum prop_handled handle_invisible_prop P_ ((struct it *));
548 static enum prop_handled handle_display_prop P_ ((struct it *));
549 static enum prop_handled handle_composition_prop P_ ((struct it *));
550 static enum prop_handled handle_overlay_change P_ ((struct it *));
551 static enum prop_handled handle_fontified_prop P_ ((struct it *));
552
553 /* Properties handled by iterators. */
554
555 static struct props it_props[] =
556 {
557 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
558 /* Handle `face' before `display' because some sub-properties of
559 `display' need to know the face. */
560 {&Qface, FACE_PROP_IDX, handle_face_prop},
561 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
562 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
563 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
564 {NULL, 0, NULL}
565 };
566
567 /* Value is the position described by X. If X is a marker, value is
568 the marker_position of X. Otherwise, value is X. */
569
570 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
571
572 /* Enumeration returned by some move_it_.* functions internally. */
573
574 enum move_it_result
575 {
576 /* Not used. Undefined value. */
577 MOVE_UNDEFINED,
578
579 /* Move ended at the requested buffer position or ZV. */
580 MOVE_POS_MATCH_OR_ZV,
581
582 /* Move ended at the requested X pixel position. */
583 MOVE_X_REACHED,
584
585 /* Move within a line ended at the end of a line that must be
586 continued. */
587 MOVE_LINE_CONTINUED,
588
589 /* Move within a line ended at the end of a line that would
590 be displayed truncated. */
591 MOVE_LINE_TRUNCATED,
592
593 /* Move within a line ended at a line end. */
594 MOVE_NEWLINE_OR_CR
595 };
596
597
598 \f
599 /* Function prototypes. */
600
601 static void ensure_echo_area_buffers P_ ((void));
602 static struct glyph_row *row_containing_pos P_ ((struct window *, int,
603 struct glyph_row *,
604 struct glyph_row *));
605 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
606 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
607 static void clear_garbaged_frames P_ ((void));
608 static int current_message_1 P_ ((Lisp_Object *));
609 static int truncate_message_1 P_ ((int));
610 static int set_message_1 P_ ((char *s, Lisp_Object, int, int));
611 static int display_echo_area P_ ((struct window *));
612 static int display_echo_area_1 P_ ((struct window *));
613 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
614 static int string_char_and_length P_ ((unsigned char *, int, int *));
615 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
616 struct text_pos));
617 static int compute_window_start_on_continuation_line P_ ((struct window *));
618 static Lisp_Object eval_handler P_ ((Lisp_Object));
619 static Lisp_Object eval_form P_ ((Lisp_Object));
620 static void insert_left_trunc_glyphs P_ ((struct it *));
621 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
622 static void extend_face_to_end_of_line P_ ((struct it *));
623 static int append_space P_ ((struct it *, int));
624 static void make_cursor_line_fully_visible P_ ((struct window *));
625 static int try_scrolling P_ ((Lisp_Object, int, int, int, int));
626 static int trailing_whitespace_p P_ ((int));
627 static int message_log_check_duplicate P_ ((int, int, int, int));
628 int invisible_p P_ ((Lisp_Object, Lisp_Object));
629 int invisible_ellipsis_p P_ ((Lisp_Object, Lisp_Object));
630 static void push_it P_ ((struct it *));
631 static void pop_it P_ ((struct it *));
632 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
633 static void redisplay_internal P_ ((int));
634 static int echo_area_display P_ ((int));
635 static void redisplay_windows P_ ((Lisp_Object));
636 static void redisplay_window P_ ((Lisp_Object, int));
637 static void update_menu_bar P_ ((struct frame *, int));
638 static int try_window_reusing_current_matrix P_ ((struct window *));
639 static int try_window_id P_ ((struct window *));
640 static int display_line P_ ((struct it *));
641 static void display_mode_lines P_ ((struct window *));
642 static void display_mode_line P_ ((struct window *, enum face_id,
643 Lisp_Object));
644 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object));
645 static char *decode_mode_spec P_ ((struct window *, int, int, int));
646 static void display_menu_bar P_ ((struct window *));
647 static int display_count_lines P_ ((int, int, int, int, int *));
648 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
649 int, int, struct it *, int, int, int, int));
650 static void compute_line_metrics P_ ((struct it *));
651 static void run_redisplay_end_trigger_hook P_ ((struct it *));
652 static int get_overlay_strings P_ ((struct it *));
653 static void next_overlay_string P_ ((struct it *));
654 void set_iterator_to_next P_ ((struct it *));
655 static void reseat P_ ((struct it *, struct text_pos, int));
656 static void reseat_1 P_ ((struct it *, struct text_pos, int));
657 static void back_to_previous_visible_line_start P_ ((struct it *));
658 static void reseat_at_previous_visible_line_start P_ ((struct it *));
659 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
660 static int next_element_from_display_vector P_ ((struct it *));
661 static int next_element_from_string P_ ((struct it *));
662 static int next_element_from_c_string P_ ((struct it *));
663 static int next_element_from_buffer P_ ((struct it *));
664 static int next_element_from_composition P_ ((struct it *));
665 static int next_element_from_image P_ ((struct it *));
666 static int next_element_from_stretch P_ ((struct it *));
667 static void load_overlay_strings P_ ((struct it *));
668 static void init_from_display_pos P_ ((struct it *, struct window *,
669 struct display_pos *));
670 static void reseat_to_string P_ ((struct it *, unsigned char *,
671 Lisp_Object, int, int, int, int));
672 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
673 int, int, int));
674 void move_it_vertically_backward P_ ((struct it *, int));
675 static void init_to_row_start P_ ((struct it *, struct window *,
676 struct glyph_row *));
677 static void init_to_row_end P_ ((struct it *, struct window *,
678 struct glyph_row *));
679 static void back_to_previous_line_start P_ ((struct it *));
680 static void forward_to_next_line_start P_ ((struct it *));
681 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
682 Lisp_Object, int));
683 static struct text_pos string_pos P_ ((int, Lisp_Object));
684 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
685 static int number_of_chars P_ ((unsigned char *, int));
686 static void compute_stop_pos P_ ((struct it *));
687 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
688 Lisp_Object));
689 static int face_before_or_after_it_pos P_ ((struct it *, int));
690 static int next_overlay_change P_ ((int));
691 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
692 Lisp_Object, struct text_pos *));
693
694 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
695 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
696
697 #ifdef HAVE_WINDOW_SYSTEM
698
699 static void update_tool_bar P_ ((struct frame *, int));
700 static void build_desired_tool_bar_string P_ ((struct frame *f));
701 static int redisplay_tool_bar P_ ((struct frame *));
702 static void display_tool_bar_line P_ ((struct it *));
703
704 #endif /* HAVE_WINDOW_SYSTEM */
705
706 \f
707 /***********************************************************************
708 Window display dimensions
709 ***********************************************************************/
710
711 /* Return the window-relative maximum y + 1 for glyph rows displaying
712 text in window W. This is the height of W minus the height of a
713 mode line, if any. */
714
715 INLINE int
716 window_text_bottom_y (w)
717 struct window *w;
718 {
719 struct frame *f = XFRAME (w->frame);
720 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
721
722 if (WINDOW_WANTS_MODELINE_P (w))
723 height -= CURRENT_MODE_LINE_HEIGHT (w);
724 return height;
725 }
726
727
728 /* Return the pixel width of display area AREA of window W. AREA < 0
729 means return the total width of W, not including bitmap areas to
730 the left and right of the window. */
731
732 INLINE int
733 window_box_width (w, area)
734 struct window *w;
735 int area;
736 {
737 struct frame *f = XFRAME (w->frame);
738 int width = XFASTINT (w->width);
739
740 if (!w->pseudo_window_p)
741 {
742 width -= FRAME_SCROLL_BAR_WIDTH (f) + FRAME_FLAGS_AREA_COLS (f);
743
744 if (area == TEXT_AREA)
745 {
746 if (INTEGERP (w->left_margin_width))
747 width -= XFASTINT (w->left_margin_width);
748 if (INTEGERP (w->right_margin_width))
749 width -= XFASTINT (w->right_margin_width);
750 }
751 else if (area == LEFT_MARGIN_AREA)
752 width = (INTEGERP (w->left_margin_width)
753 ? XFASTINT (w->left_margin_width) : 0);
754 else if (area == RIGHT_MARGIN_AREA)
755 width = (INTEGERP (w->right_margin_width)
756 ? XFASTINT (w->right_margin_width) : 0);
757 }
758
759 return width * CANON_X_UNIT (f);
760 }
761
762
763 /* Return the pixel height of the display area of window W, not
764 including mode lines of W, if any.. */
765
766 INLINE int
767 window_box_height (w)
768 struct window *w;
769 {
770 struct frame *f = XFRAME (w->frame);
771 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
772
773 if (WINDOW_WANTS_MODELINE_P (w))
774 height -= CURRENT_MODE_LINE_HEIGHT (w);
775
776 if (WINDOW_WANTS_HEADER_LINE_P (w))
777 height -= CURRENT_HEADER_LINE_HEIGHT (w);
778
779 return height;
780 }
781
782
783 /* Return the frame-relative coordinate of the left edge of display
784 area AREA of window W. AREA < 0 means return the left edge of the
785 whole window, to the right of any bitmap area at the left side of
786 W. */
787
788 INLINE int
789 window_box_left (w, area)
790 struct window *w;
791 int area;
792 {
793 struct frame *f = XFRAME (w->frame);
794 int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
795
796 if (!w->pseudo_window_p)
797 {
798 x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
799 + FRAME_LEFT_FLAGS_AREA_WIDTH (f));
800
801 if (area == TEXT_AREA)
802 x += window_box_width (w, LEFT_MARGIN_AREA);
803 else if (area == RIGHT_MARGIN_AREA)
804 x += (window_box_width (w, LEFT_MARGIN_AREA)
805 + window_box_width (w, TEXT_AREA));
806 }
807
808 return x;
809 }
810
811
812 /* Return the frame-relative coordinate of the right edge of display
813 area AREA of window W. AREA < 0 means return the left edge of the
814 whole window, to the left of any bitmap area at the right side of
815 W. */
816
817 INLINE int
818 window_box_right (w, area)
819 struct window *w;
820 int area;
821 {
822 return window_box_left (w, area) + window_box_width (w, area);
823 }
824
825
826 /* Get the bounding box of the display area AREA of window W, without
827 mode lines, in frame-relative coordinates. AREA < 0 means the
828 whole window, not including bitmap areas to the left and right of
829 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
830 coordinates of the upper-left corner of the box. Return in
831 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
832
833 INLINE void
834 window_box (w, area, box_x, box_y, box_width, box_height)
835 struct window *w;
836 int area;
837 int *box_x, *box_y, *box_width, *box_height;
838 {
839 struct frame *f = XFRAME (w->frame);
840
841 *box_width = window_box_width (w, area);
842 *box_height = window_box_height (w);
843 *box_x = window_box_left (w, area);
844 *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
845 + XFASTINT (w->top) * CANON_Y_UNIT (f));
846 if (WINDOW_WANTS_HEADER_LINE_P (w))
847 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
848 }
849
850
851 /* Get the bounding box of the display area AREA of window W, without
852 mode lines. AREA < 0 means the whole window, not including bitmap
853 areas to the left and right of the window. Return in *TOP_LEFT_X
854 and TOP_LEFT_Y the frame-relative pixel coordinates of the
855 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
856 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
857 box. */
858
859 INLINE void
860 window_box_edges (w, area, top_left_x, top_left_y,
861 bottom_right_x, bottom_right_y)
862 struct window *w;
863 int area;
864 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
865 {
866 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
867 bottom_right_y);
868 *bottom_right_x += *top_left_x;
869 *bottom_right_y += *top_left_y;
870 }
871
872
873 \f
874 /***********************************************************************
875 Utilities
876 ***********************************************************************/
877
878 /* Return the next character from STR which is MAXLEN bytes long.
879 Return in *LEN the length of the character. This is like
880 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
881 we find one, we return a `?', but with the length of the illegal
882 character. */
883
884 static INLINE int
885 string_char_and_length (str, maxlen, len)
886 unsigned char *str;
887 int maxlen, *len;
888 {
889 int c;
890
891 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
892 if (!CHAR_VALID_P (c, 1))
893 /* We may not change the length here because other places in Emacs
894 don't use this function, i.e. they silently accept illegal
895 characters. */
896 c = '?';
897
898 return c;
899 }
900
901
902
903 /* Given a position POS containing a valid character and byte position
904 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
905
906 static struct text_pos
907 string_pos_nchars_ahead (pos, string, nchars)
908 struct text_pos pos;
909 Lisp_Object string;
910 int nchars;
911 {
912 xassert (STRINGP (string) && nchars >= 0);
913
914 if (STRING_MULTIBYTE (string))
915 {
916 int rest = STRING_BYTES (XSTRING (string)) - BYTEPOS (pos);
917 unsigned char *p = XSTRING (string)->data + BYTEPOS (pos);
918 int len;
919
920 while (nchars--)
921 {
922 string_char_and_length (p, rest, &len);
923 p += len, rest -= len;
924 xassert (rest >= 0);
925 CHARPOS (pos) += 1;
926 BYTEPOS (pos) += len;
927 }
928 }
929 else
930 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
931
932 return pos;
933 }
934
935
936 /* Value is the text position, i.e. character and byte position,
937 for character position CHARPOS in STRING. */
938
939 static INLINE struct text_pos
940 string_pos (charpos, string)
941 int charpos;
942 Lisp_Object string;
943 {
944 struct text_pos pos;
945 xassert (STRINGP (string));
946 xassert (charpos >= 0);
947 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
948 return pos;
949 }
950
951
952 /* Value is a text position, i.e. character and byte position, for
953 character position CHARPOS in C string S. MULTIBYTE_P non-zero
954 means recognize multibyte characters. */
955
956 static struct text_pos
957 c_string_pos (charpos, s, multibyte_p)
958 int charpos;
959 unsigned char *s;
960 int multibyte_p;
961 {
962 struct text_pos pos;
963
964 xassert (s != NULL);
965 xassert (charpos >= 0);
966
967 if (multibyte_p)
968 {
969 int rest = strlen (s), len;
970
971 SET_TEXT_POS (pos, 0, 0);
972 while (charpos--)
973 {
974 string_char_and_length (s, rest, &len);
975 s += len, rest -= len;
976 xassert (rest >= 0);
977 CHARPOS (pos) += 1;
978 BYTEPOS (pos) += len;
979 }
980 }
981 else
982 SET_TEXT_POS (pos, charpos, charpos);
983
984 return pos;
985 }
986
987
988 /* Value is the number of characters in C string S. MULTIBYTE_P
989 non-zero means recognize multibyte characters. */
990
991 static int
992 number_of_chars (s, multibyte_p)
993 unsigned char *s;
994 int multibyte_p;
995 {
996 int nchars;
997
998 if (multibyte_p)
999 {
1000 int rest = strlen (s), len;
1001 unsigned char *p = (unsigned char *) s;
1002
1003 for (nchars = 0; rest > 0; ++nchars)
1004 {
1005 string_char_and_length (p, rest, &len);
1006 rest -= len, p += len;
1007 }
1008 }
1009 else
1010 nchars = strlen (s);
1011
1012 return nchars;
1013 }
1014
1015
1016 /* Compute byte position NEWPOS->bytepos corresponding to
1017 NEWPOS->charpos. POS is a known position in string STRING.
1018 NEWPOS->charpos must be >= POS.charpos. */
1019
1020 static void
1021 compute_string_pos (newpos, pos, string)
1022 struct text_pos *newpos, pos;
1023 Lisp_Object string;
1024 {
1025 xassert (STRINGP (string));
1026 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1027
1028 if (STRING_MULTIBYTE (string))
1029 *newpos = string_pos_nchars_ahead (pos, string,
1030 CHARPOS (*newpos) - CHARPOS (pos));
1031 else
1032 BYTEPOS (*newpos) = CHARPOS (*newpos);
1033 }
1034
1035
1036 \f
1037 /***********************************************************************
1038 Lisp form evaluation
1039 ***********************************************************************/
1040
1041 /* Error handler for eval_form. */
1042
1043 static Lisp_Object
1044 eval_handler (arg)
1045 Lisp_Object arg;
1046 {
1047 return Qnil;
1048 }
1049
1050
1051 /* Evaluate SEXPR and return the result, or nil if something went
1052 wrong. */
1053
1054 static Lisp_Object
1055 eval_form (sexpr)
1056 Lisp_Object sexpr;
1057 {
1058 int count = specpdl_ptr - specpdl;
1059 Lisp_Object val;
1060 specbind (Qinhibit_redisplay, Qt);
1061 val = internal_condition_case_1 (Feval, sexpr, Qerror, eval_handler);
1062 return unbind_to (count, val);
1063 }
1064
1065
1066 \f
1067 /***********************************************************************
1068 Debugging
1069 ***********************************************************************/
1070
1071 #if 0
1072
1073 /* Define CHECK_IT to perform sanity checks on iterators.
1074 This is for debugging. It is too slow to do unconditionally. */
1075
1076 static void
1077 check_it (it)
1078 struct it *it;
1079 {
1080 if (it->method == next_element_from_string)
1081 {
1082 xassert (STRINGP (it->string));
1083 xassert (IT_STRING_CHARPOS (*it) >= 0);
1084 }
1085 else if (it->method == next_element_from_buffer)
1086 {
1087 /* Check that character and byte positions agree. */
1088 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1089 }
1090
1091 if (it->dpvec)
1092 xassert (it->current.dpvec_index >= 0);
1093 else
1094 xassert (it->current.dpvec_index < 0);
1095 }
1096
1097 #define CHECK_IT(IT) check_it ((IT))
1098
1099 #else /* not 0 */
1100
1101 #define CHECK_IT(IT) (void) 0
1102
1103 #endif /* not 0 */
1104
1105
1106 #if GLYPH_DEBUG
1107
1108 /* Check that the window end of window W is what we expect it
1109 to be---the last row in the current matrix displaying text. */
1110
1111 static void
1112 check_window_end (w)
1113 struct window *w;
1114 {
1115 if (!MINI_WINDOW_P (w)
1116 && !NILP (w->window_end_valid))
1117 {
1118 struct glyph_row *row;
1119 xassert ((row = MATRIX_ROW (w->current_matrix,
1120 XFASTINT (w->window_end_vpos)),
1121 !row->enabled_p
1122 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1123 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1124 }
1125 }
1126
1127 #define CHECK_WINDOW_END(W) check_window_end ((W))
1128
1129 #else /* not GLYPH_DEBUG */
1130
1131 #define CHECK_WINDOW_END(W) (void) 0
1132
1133 #endif /* not GLYPH_DEBUG */
1134
1135
1136 \f
1137 /***********************************************************************
1138 Iterator initialization
1139 ***********************************************************************/
1140
1141 /* Initialize IT for displaying current_buffer in window W, starting
1142 at character position CHARPOS. CHARPOS < 0 means that no buffer
1143 position is specified which is useful when the iterator is assigned
1144 a position later. BYTEPOS is the byte position corresponding to
1145 CHARPOS. BYTEPOS <= 0 means compute it from CHARPOS.
1146
1147 If ROW is not null, calls to produce_glyphs with IT as parameter
1148 will produce glyphs in that row.
1149
1150 BASE_FACE_ID is the id of a base face to use. It must be one of
1151 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID or
1152 HEADER_LINE_FACE_ID for displaying mode lines, or TOOL_BAR_FACE_ID for
1153 displaying the tool-bar.
1154
1155 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID or
1156 HEADER_LINE_FACE_ID, the iterator will be initialized to use the
1157 corresponding mode line glyph row of the desired matrix of W. */
1158
1159 void
1160 init_iterator (it, w, charpos, bytepos, row, base_face_id)
1161 struct it *it;
1162 struct window *w;
1163 int charpos, bytepos;
1164 struct glyph_row *row;
1165 enum face_id base_face_id;
1166 {
1167 int highlight_region_p;
1168
1169 /* Some precondition checks. */
1170 xassert (w != NULL && it != NULL);
1171 xassert (charpos < 0 || (charpos > 0 && charpos <= ZV));
1172
1173 /* If face attributes have been changed since the last redisplay,
1174 free realized faces now because they depend on face definitions
1175 that might have changed. */
1176 if (face_change_count)
1177 {
1178 face_change_count = 0;
1179 free_all_realized_faces (Qnil);
1180 }
1181
1182 /* Use one of the mode line rows of W's desired matrix if
1183 appropriate. */
1184 if (row == NULL)
1185 {
1186 if (base_face_id == MODE_LINE_FACE_ID)
1187 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
1188 else if (base_face_id == HEADER_LINE_FACE_ID)
1189 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
1190 }
1191
1192 /* Clear IT. */
1193 bzero (it, sizeof *it);
1194 it->current.overlay_string_index = -1;
1195 it->current.dpvec_index = -1;
1196 it->base_face_id = base_face_id;
1197
1198 /* The window in which we iterate over current_buffer: */
1199 XSETWINDOW (it->window, w);
1200 it->w = w;
1201 it->f = XFRAME (w->frame);
1202
1203 /* Extra space between lines (on window systems only). */
1204 if (base_face_id == DEFAULT_FACE_ID
1205 && FRAME_WINDOW_P (it->f))
1206 {
1207 if (NATNUMP (current_buffer->extra_line_spacing))
1208 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
1209 else if (it->f->extra_line_spacing > 0)
1210 it->extra_line_spacing = it->f->extra_line_spacing;
1211 }
1212
1213 /* If realized faces have been removed, e.g. because of face
1214 attribute changes of named faces, recompute them. */
1215 if (FRAME_FACE_CACHE (it->f)->used == 0)
1216 recompute_basic_faces (it->f);
1217
1218 /* Current value of the `space-width', and 'height' properties. */
1219 it->space_width = Qnil;
1220 it->font_height = Qnil;
1221
1222 /* Are control characters displayed as `^C'? */
1223 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
1224
1225 /* -1 means everything between a CR and the following line end
1226 is invisible. >0 means lines indented more than this value are
1227 invisible. */
1228 it->selective = (INTEGERP (current_buffer->selective_display)
1229 ? XFASTINT (current_buffer->selective_display)
1230 : (!NILP (current_buffer->selective_display)
1231 ? -1 : 0));
1232 it->selective_display_ellipsis_p
1233 = !NILP (current_buffer->selective_display_ellipses);
1234
1235 /* Display table to use. */
1236 it->dp = window_display_table (w);
1237
1238 /* Are multibyte characters enabled in current_buffer? */
1239 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1240
1241 /* Non-zero if we should highlight the region. */
1242 highlight_region_p
1243 = (!NILP (Vtransient_mark_mode)
1244 && !NILP (current_buffer->mark_active)
1245 && XMARKER (current_buffer->mark)->buffer != 0);
1246
1247 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
1248 start and end of a visible region in window IT->w. Set both to
1249 -1 to indicate no region. */
1250 if (highlight_region_p
1251 /* Maybe highlight only in selected window. */
1252 && (/* Either show region everywhere. */
1253 highlight_nonselected_windows
1254 /* Or show region in the selected window. */
1255 || w == XWINDOW (selected_window)
1256 /* Or show the region if we are in the mini-buffer and W is
1257 the window the mini-buffer refers to. */
1258 || (MINI_WINDOW_P (XWINDOW (selected_window))
1259 && w == XWINDOW (Vminibuf_scroll_window))))
1260 {
1261 int charpos = marker_position (current_buffer->mark);
1262 it->region_beg_charpos = min (PT, charpos);
1263 it->region_end_charpos = max (PT, charpos);
1264 }
1265 else
1266 it->region_beg_charpos = it->region_end_charpos = -1;
1267
1268 /* Get the position at which the redisplay_end_trigger hook should
1269 be run, if it is to be run at all. */
1270 if (MARKERP (w->redisplay_end_trigger)
1271 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
1272 it->redisplay_end_trigger_charpos
1273 = marker_position (w->redisplay_end_trigger);
1274 else if (INTEGERP (w->redisplay_end_trigger))
1275 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
1276
1277 /* Correct bogus values of tab_width. */
1278 it->tab_width = XINT (current_buffer->tab_width);
1279 if (it->tab_width <= 0 || it->tab_width > 1000)
1280 it->tab_width = 8;
1281
1282 /* Are lines in the display truncated? */
1283 it->truncate_lines_p
1284 = (base_face_id != DEFAULT_FACE_ID
1285 || XINT (it->w->hscroll)
1286 || (truncate_partial_width_windows
1287 && !WINDOW_FULL_WIDTH_P (it->w))
1288 || !NILP (current_buffer->truncate_lines));
1289
1290 /* Get dimensions of truncation and continuation glyphs. These are
1291 displayed as bitmaps under X, so we don't need them for such
1292 frames. */
1293 if (!FRAME_WINDOW_P (it->f))
1294 {
1295 if (it->truncate_lines_p)
1296 {
1297 /* We will need the truncation glyph. */
1298 xassert (it->glyph_row == NULL);
1299 produce_special_glyphs (it, IT_TRUNCATION);
1300 it->truncation_pixel_width = it->pixel_width;
1301 }
1302 else
1303 {
1304 /* We will need the continuation glyph. */
1305 xassert (it->glyph_row == NULL);
1306 produce_special_glyphs (it, IT_CONTINUATION);
1307 it->continuation_pixel_width = it->pixel_width;
1308 }
1309
1310 /* Reset these values to zero becaue the produce_special_glyphs
1311 above has changed them. */
1312 it->pixel_width = it->ascent = it->descent = 0;
1313 it->phys_ascent = it->phys_descent = 0;
1314 }
1315
1316 /* Set this after getting the dimensions of truncation and
1317 continuation glyphs, so that we don't produce glyphs when calling
1318 produce_special_glyphs, above. */
1319 it->glyph_row = row;
1320 it->area = TEXT_AREA;
1321
1322 /* Get the dimensions of the display area. The display area
1323 consists of the visible window area plus a horizontally scrolled
1324 part to the left of the window. All x-values are relative to the
1325 start of this total display area. */
1326 if (base_face_id != DEFAULT_FACE_ID)
1327 {
1328 /* Mode lines, menu bar in terminal frames. */
1329 it->first_visible_x = 0;
1330 it->last_visible_x = XFASTINT (w->width) * CANON_X_UNIT (it->f);
1331 }
1332 else
1333 {
1334 it->first_visible_x
1335 = XFASTINT (it->w->hscroll) * CANON_X_UNIT (it->f);
1336 it->last_visible_x = (it->first_visible_x
1337 + window_box_width (w, TEXT_AREA));
1338
1339 /* If we truncate lines, leave room for the truncator glyph(s) at
1340 the right margin. Otherwise, leave room for the continuation
1341 glyph(s). Truncation and continuation glyphs are not inserted
1342 for window-based redisplay. */
1343 if (!FRAME_WINDOW_P (it->f))
1344 {
1345 if (it->truncate_lines_p)
1346 it->last_visible_x -= it->truncation_pixel_width;
1347 else
1348 it->last_visible_x -= it->continuation_pixel_width;
1349 }
1350
1351 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
1352 it->current_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w) + w->vscroll;
1353 }
1354
1355 /* Leave room for a border glyph. */
1356 if (!FRAME_WINDOW_P (it->f)
1357 && !WINDOW_RIGHTMOST_P (it->w))
1358 it->last_visible_x -= 1;
1359
1360 it->last_visible_y = window_text_bottom_y (w);
1361
1362 /* For mode lines and alike, arrange for the first glyph having a
1363 left box line if the face specifies a box. */
1364 if (base_face_id != DEFAULT_FACE_ID)
1365 {
1366 struct face *face;
1367
1368 it->face_id = base_face_id;
1369
1370 /* If we have a boxed mode line, make the first character appear
1371 with a left box line. */
1372 face = FACE_FROM_ID (it->f, base_face_id);
1373 if (face->box != FACE_NO_BOX)
1374 it->start_of_box_run_p = 1;
1375 }
1376
1377 /* If a buffer position was specified, set the iterator there,
1378 getting overlays and face properties from that position. */
1379 if (charpos > 0)
1380 {
1381 it->end_charpos = ZV;
1382 it->face_id = -1;
1383 IT_CHARPOS (*it) = charpos;
1384
1385 /* Compute byte position if not specified. */
1386 if (bytepos <= 0)
1387 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
1388 else
1389 IT_BYTEPOS (*it) = bytepos;
1390
1391 /* Compute faces etc. */
1392 reseat (it, it->current.pos, 1);
1393 }
1394
1395 CHECK_IT (it);
1396 }
1397
1398
1399 /* Initialize IT for the display of window W with window start POS. */
1400
1401 void
1402 start_display (it, w, pos)
1403 struct it *it;
1404 struct window *w;
1405 struct text_pos pos;
1406 {
1407 int start_at_line_beg_p;
1408 struct glyph_row *row;
1409 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
1410 int first_y;
1411
1412 row = w->desired_matrix->rows + first_vpos;
1413 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
1414 first_y = it->current_y;
1415
1416 /* If window start is not at a line start, move back to the line
1417 start. This makes sure that we take continuation lines into
1418 account. */
1419 start_at_line_beg_p = (CHARPOS (pos) == BEGV
1420 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
1421 if (!start_at_line_beg_p)
1422 reseat_at_previous_visible_line_start (it);
1423
1424 /* If window start is not at a line start, skip forward to POS to
1425 get the correct continuation_lines_width and current_x. */
1426 if (!start_at_line_beg_p)
1427 {
1428 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
1429
1430 /* If lines are continued, this line may end in the middle of a
1431 multi-glyph character (e.g. a control character displayed as
1432 \003, or in the middle of an overlay string). In this case
1433 move_it_to above will not have taken us to the start of
1434 the continuation line but to the end of the continued line. */
1435 if (!it->truncate_lines_p && it->current_x > 0)
1436 {
1437 if (it->current.dpvec_index >= 0
1438 || it->current.overlay_string_index >= 0)
1439 {
1440 set_iterator_to_next (it);
1441 move_it_in_display_line_to (it, -1, -1, 0);
1442 }
1443 it->continuation_lines_width += it->current_x;
1444 }
1445
1446 it->current_y = first_y;
1447 it->vpos = 0;
1448 it->current_x = it->hpos = 0;
1449 }
1450
1451 #if 0 /* Don't assert the following because start_display is sometimes
1452 called intentionally with a window start that is not at a
1453 line start. Please leave this code in as a comment. */
1454
1455 /* Window start should be on a line start, now. */
1456 xassert (it->continuation_lines_width
1457 || IT_CHARPOS (it) == BEGV
1458 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
1459 #endif /* 0 */
1460 }
1461
1462
1463 /* Initialize IT for stepping through current_buffer in window W,
1464 starting at position POS that includes overlay string and display
1465 vector/ control character translation position information. */
1466
1467 static void
1468 init_from_display_pos (it, w, pos)
1469 struct it *it;
1470 struct window *w;
1471 struct display_pos *pos;
1472 {
1473 /* Keep in mind: the call to reseat in init_iterator skips invisible
1474 text, so we might end up at a position different from POS. This
1475 is only a problem when POS is a row start after a newline and an
1476 overlay starts there with an after-string, and the overlay has an
1477 invisible property. Since we don't skip invisible text in
1478 display_line and elsewhere immediately after consuming the
1479 newline before the row start, such a POS will not be in a string,
1480 but the call to init_iterator below will move us to the
1481 after-string. */
1482 init_iterator (it, w, CHARPOS (pos->pos), BYTEPOS (pos->pos),
1483 NULL, DEFAULT_FACE_ID);
1484
1485 /* If position is within an overlay string, set up IT to
1486 the right overlay string. */
1487 if (pos->overlay_string_index >= 0)
1488 {
1489 int relative_index;
1490
1491 /* We already have the first chunk of overlay strings in
1492 IT->overlay_strings. Load more until the one for
1493 pos->overlay_string_index is in IT->overlay_strings. */
1494 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
1495 {
1496 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
1497 it->current.overlay_string_index = 0;
1498 while (n--)
1499 {
1500 load_overlay_strings (it);
1501 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
1502 }
1503 }
1504
1505 it->current.overlay_string_index = pos->overlay_string_index;
1506 relative_index = (it->current.overlay_string_index
1507 % OVERLAY_STRING_CHUNK_SIZE);
1508 it->string = it->overlay_strings[relative_index];
1509 it->current.string_pos = pos->string_pos;
1510 it->method = next_element_from_string;
1511 }
1512 else if (CHARPOS (pos->string_pos) >= 0)
1513 {
1514 /* Recorded position is not in an overlay string, but in another
1515 string. This can only be a string from a `display' property.
1516 IT should already be filled with that string. */
1517 it->current.string_pos = pos->string_pos;
1518 xassert (STRINGP (it->string));
1519 }
1520
1521 /* Restore position in display vector translations or control
1522 character translations. */
1523 if (pos->dpvec_index >= 0)
1524 {
1525 /* This fills IT->dpvec. */
1526 get_next_display_element (it);
1527 xassert (it->dpvec && it->current.dpvec_index == 0);
1528 it->current.dpvec_index = pos->dpvec_index;
1529 }
1530
1531 CHECK_IT (it);
1532 }
1533
1534
1535 /* Initialize IT for stepping through current_buffer in window W
1536 starting at ROW->start. */
1537
1538 static void
1539 init_to_row_start (it, w, row)
1540 struct it *it;
1541 struct window *w;
1542 struct glyph_row *row;
1543 {
1544 init_from_display_pos (it, w, &row->start);
1545 it->continuation_lines_width = row->continuation_lines_width;
1546 CHECK_IT (it);
1547 }
1548
1549
1550 /* Initialize IT for stepping through current_buffer in window W
1551 starting in the line following ROW, i.e. starting at ROW->end. */
1552
1553 static void
1554 init_to_row_end (it, w, row)
1555 struct it *it;
1556 struct window *w;
1557 struct glyph_row *row;
1558 {
1559 init_from_display_pos (it, w, &row->end);
1560
1561 if (row->continued_p)
1562 it->continuation_lines_width = (row->continuation_lines_width
1563 + row->pixel_width);
1564 CHECK_IT (it);
1565 }
1566
1567
1568
1569 \f
1570 /***********************************************************************
1571 Text properties
1572 ***********************************************************************/
1573
1574 /* Called when IT reaches IT->stop_charpos. Handle text property and
1575 overlay changes. Set IT->stop_charpos to the next position where
1576 to stop. */
1577
1578 static void
1579 handle_stop (it)
1580 struct it *it;
1581 {
1582 enum prop_handled handled;
1583 int handle_overlay_change_p = 1;
1584 struct props *p;
1585
1586 it->dpvec = NULL;
1587 it->current.dpvec_index = -1;
1588
1589 do
1590 {
1591 handled = HANDLED_NORMALLY;
1592
1593 /* Call text property handlers. */
1594 for (p = it_props; p->handler; ++p)
1595 {
1596 handled = p->handler (it);
1597
1598 if (handled == HANDLED_RECOMPUTE_PROPS)
1599 break;
1600 else if (handled == HANDLED_RETURN)
1601 return;
1602 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
1603 handle_overlay_change_p = 0;
1604 }
1605
1606 if (handled != HANDLED_RECOMPUTE_PROPS)
1607 {
1608 /* Don't check for overlay strings below when set to deliver
1609 characters from a display vector. */
1610 if (it->method == next_element_from_display_vector)
1611 handle_overlay_change_p = 0;
1612
1613 /* Handle overlay changes. */
1614 if (handle_overlay_change_p)
1615 handled = handle_overlay_change (it);
1616
1617 /* Determine where to stop next. */
1618 if (handled == HANDLED_NORMALLY)
1619 compute_stop_pos (it);
1620 }
1621 }
1622 while (handled == HANDLED_RECOMPUTE_PROPS);
1623 }
1624
1625
1626 /* Compute IT->stop_charpos from text property and overlay change
1627 information for IT's current position. */
1628
1629 static void
1630 compute_stop_pos (it)
1631 struct it *it;
1632 {
1633 register INTERVAL iv, next_iv;
1634 Lisp_Object object, limit, position;
1635
1636 /* If nowhere else, stop at the end. */
1637 it->stop_charpos = it->end_charpos;
1638
1639 if (STRINGP (it->string))
1640 {
1641 /* Strings are usually short, so don't limit the search for
1642 properties. */
1643 object = it->string;
1644 limit = Qnil;
1645 XSETFASTINT (position, IT_STRING_CHARPOS (*it));
1646 }
1647 else
1648 {
1649 int charpos;
1650
1651 /* If next overlay change is in front of the current stop pos
1652 (which is IT->end_charpos), stop there. Note: value of
1653 next_overlay_change is point-max if no overlay change
1654 follows. */
1655 charpos = next_overlay_change (IT_CHARPOS (*it));
1656 if (charpos < it->stop_charpos)
1657 it->stop_charpos = charpos;
1658
1659 /* If showing the region, we have to stop at the region
1660 start or end because the face might change there. */
1661 if (it->region_beg_charpos > 0)
1662 {
1663 if (IT_CHARPOS (*it) < it->region_beg_charpos)
1664 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
1665 else if (IT_CHARPOS (*it) < it->region_end_charpos)
1666 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
1667 }
1668
1669 /* Set up variables for computing the stop position from text
1670 property changes. */
1671 XSETBUFFER (object, current_buffer);
1672 XSETFASTINT (limit, IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
1673 XSETFASTINT (position, IT_CHARPOS (*it));
1674
1675 }
1676
1677 /* Get the interval containing IT's position. Value is a null
1678 interval if there isn't such an interval. */
1679 iv = validate_interval_range (object, &position, &position, 0);
1680 if (!NULL_INTERVAL_P (iv))
1681 {
1682 Lisp_Object values_here[LAST_PROP_IDX];
1683 struct props *p;
1684
1685 /* Get properties here. */
1686 for (p = it_props; p->handler; ++p)
1687 values_here[p->idx] = textget (iv->plist, *p->name);
1688
1689 /* Look for an interval following iv that has different
1690 properties. */
1691 for (next_iv = next_interval (iv);
1692 (!NULL_INTERVAL_P (next_iv)
1693 && (NILP (limit)
1694 || XFASTINT (limit) > next_iv->position));
1695 next_iv = next_interval (next_iv))
1696 {
1697 for (p = it_props; p->handler; ++p)
1698 {
1699 Lisp_Object new_value;
1700
1701 new_value = textget (next_iv->plist, *p->name);
1702 if (!EQ (values_here[p->idx], new_value))
1703 break;
1704 }
1705
1706 if (p->handler)
1707 break;
1708 }
1709
1710 if (!NULL_INTERVAL_P (next_iv))
1711 {
1712 if (INTEGERP (limit)
1713 && next_iv->position >= XFASTINT (limit))
1714 /* No text property change up to limit. */
1715 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
1716 else
1717 /* Text properties change in next_iv. */
1718 it->stop_charpos = min (it->stop_charpos, next_iv->position);
1719 }
1720 }
1721
1722 xassert (STRINGP (it->string)
1723 || (it->stop_charpos >= BEGV
1724 && it->stop_charpos >= IT_CHARPOS (*it)));
1725 }
1726
1727
1728 /* Return the position of the next overlay change after POS in
1729 current_buffer. Value is point-max if no overlay change
1730 follows. This is like `next-overlay-change' but doesn't use
1731 xmalloc. */
1732
1733 static int
1734 next_overlay_change (pos)
1735 int pos;
1736 {
1737 int noverlays;
1738 int endpos;
1739 Lisp_Object *overlays;
1740 int len;
1741 int i;
1742
1743 /* Get all overlays at the given position. */
1744 len = 10;
1745 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
1746 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL);
1747 if (noverlays > len)
1748 {
1749 len = noverlays;
1750 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
1751 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL);
1752 }
1753
1754 /* If any of these overlays ends before endpos,
1755 use its ending point instead. */
1756 for (i = 0; i < noverlays; ++i)
1757 {
1758 Lisp_Object oend;
1759 int oendpos;
1760
1761 oend = OVERLAY_END (overlays[i]);
1762 oendpos = OVERLAY_POSITION (oend);
1763 endpos = min (endpos, oendpos);
1764 }
1765
1766 return endpos;
1767 }
1768
1769
1770 \f
1771 /***********************************************************************
1772 Fontification
1773 ***********************************************************************/
1774
1775 /* Handle changes in the `fontified' property of the current buffer by
1776 calling hook functions from Qfontification_functions to fontify
1777 regions of text. */
1778
1779 static enum prop_handled
1780 handle_fontified_prop (it)
1781 struct it *it;
1782 {
1783 Lisp_Object prop, pos;
1784 enum prop_handled handled = HANDLED_NORMALLY;
1785 struct gcpro gcpro1;
1786
1787 /* Get the value of the `fontified' property at IT's current buffer
1788 position. (The `fontified' property doesn't have a special
1789 meaning in strings.) If the value is nil, call functions from
1790 Qfontification_functions. */
1791 if (!STRINGP (it->string)
1792 && it->s == NULL
1793 && !NILP (Vfontification_functions)
1794 && (pos = make_number (IT_CHARPOS (*it)),
1795 prop = Fget_char_property (pos, Qfontified, Qnil),
1796 NILP (prop)))
1797 {
1798 Lisp_Object args[2];
1799
1800 GCPRO1 (pos);
1801 /* Run the hook functions. */
1802 args[0] = Qfontification_functions;
1803 args[1] = pos;
1804 Frun_hook_with_args (2, args);
1805
1806 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
1807 something. This avoids an endless loop if they failed to
1808 fontify the text for which reason ever. */
1809 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
1810 handled = HANDLED_RECOMPUTE_PROPS;
1811 UNGCPRO;
1812 }
1813
1814 return handled;
1815 }
1816
1817
1818 \f
1819 /***********************************************************************
1820 Faces
1821 ***********************************************************************/
1822
1823 /* Set up iterator IT from face properties at its current position.
1824 Called from handle_stop. */
1825
1826 static enum prop_handled
1827 handle_face_prop (it)
1828 struct it *it;
1829 {
1830 int new_face_id, next_stop;
1831
1832 if (!STRINGP (it->string))
1833 {
1834 new_face_id
1835 = face_at_buffer_position (it->w,
1836 IT_CHARPOS (*it),
1837 it->region_beg_charpos,
1838 it->region_end_charpos,
1839 &next_stop,
1840 (IT_CHARPOS (*it)
1841 + TEXT_PROP_DISTANCE_LIMIT),
1842 0);
1843
1844 /* Is this a start of a run of characters with box face?
1845 Caveat: this can be called for a freshly initialized
1846 iterator; face_id is -1 is this case. We know that the new
1847 face will not change until limit, i.e. if the new face has a
1848 box, all characters up to limit will have one. But, as
1849 usual, we don't know whether limit is really the end. */
1850 if (new_face_id != it->face_id)
1851 {
1852 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
1853
1854 /* If new face has a box but old face has not, this is
1855 the start of a run of characters with box, i.e. it has
1856 a shadow on the left side. The value of face_id of the
1857 iterator will be -1 if this is the initial call that gets
1858 the face. In this case, we have to look in front of IT's
1859 position and see whether there is a face != new_face_id. */
1860 it->start_of_box_run_p
1861 = (new_face->box != FACE_NO_BOX
1862 && (it->face_id >= 0
1863 || IT_CHARPOS (*it) == BEG
1864 || new_face_id != face_before_it_pos (it)));
1865 it->face_box_p = new_face->box != FACE_NO_BOX;
1866 }
1867 }
1868 else
1869 {
1870 new_face_id
1871 = face_at_string_position (it->w,
1872 it->string,
1873 IT_STRING_CHARPOS (*it),
1874 (it->current.overlay_string_index >= 0
1875 ? IT_CHARPOS (*it)
1876 : 0),
1877 it->region_beg_charpos,
1878 it->region_end_charpos,
1879 &next_stop,
1880 it->base_face_id);
1881
1882 #if 0 /* This shouldn't be neccessary. Let's check it. */
1883 /* If IT is used to display a mode line we would really like to
1884 use the mode line face instead of the frame's default face. */
1885 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
1886 && new_face_id == DEFAULT_FACE_ID)
1887 new_face_id = MODE_LINE_FACE_ID;
1888 #endif
1889
1890 /* Is this a start of a run of characters with box? Caveat:
1891 this can be called for a freshly allocated iterator; face_id
1892 is -1 is this case. We know that the new face will not
1893 change until the next check pos, i.e. if the new face has a
1894 box, all characters up to that position will have a
1895 box. But, as usual, we don't know whether that position
1896 is really the end. */
1897 if (new_face_id != it->face_id)
1898 {
1899 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
1900 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
1901
1902 /* If new face has a box but old face hasn't, this is the
1903 start of a run of characters with box, i.e. it has a
1904 shadow on the left side. */
1905 it->start_of_box_run_p
1906 = new_face->box && (old_face == NULL || !old_face->box);
1907 it->face_box_p = new_face->box != FACE_NO_BOX;
1908 }
1909 }
1910
1911 it->face_id = new_face_id;
1912 return HANDLED_NORMALLY;
1913 }
1914
1915
1916 /* Compute the face one character before or after the current position
1917 of IT. BEFORE_P non-zero means get the face in front of IT's
1918 position. Value is the id of the face. */
1919
1920 static int
1921 face_before_or_after_it_pos (it, before_p)
1922 struct it *it;
1923 int before_p;
1924 {
1925 int face_id, limit;
1926 int next_check_charpos;
1927 struct text_pos pos;
1928
1929 xassert (it->s == NULL);
1930
1931 if (STRINGP (it->string))
1932 {
1933 /* No face change past the end of the string (for the case
1934 we are padding with spaces). No face change before the
1935 string start. */
1936 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size
1937 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
1938 return it->face_id;
1939
1940 /* Set pos to the position before or after IT's current position. */
1941 if (before_p)
1942 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
1943 else
1944 /* For composition, we must check the character after the
1945 composition. */
1946 pos = (it->what == IT_COMPOSITION
1947 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
1948 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
1949
1950 /* Get the face for ASCII, or unibyte. */
1951 face_id
1952 = face_at_string_position (it->w,
1953 it->string,
1954 CHARPOS (pos),
1955 (it->current.overlay_string_index >= 0
1956 ? IT_CHARPOS (*it)
1957 : 0),
1958 it->region_beg_charpos,
1959 it->region_end_charpos,
1960 &next_check_charpos,
1961 it->base_face_id);
1962
1963 /* Correct the face for charsets different from ASCII. Do it
1964 for the multibyte case only. The face returned above is
1965 suitable for unibyte text if IT->string is unibyte. */
1966 if (STRING_MULTIBYTE (it->string))
1967 {
1968 unsigned char *p = XSTRING (it->string)->data + BYTEPOS (pos);
1969 int rest = STRING_BYTES (XSTRING (it->string)) - BYTEPOS (pos);
1970 int c, len;
1971 struct face *face = FACE_FROM_ID (it->f, face_id);
1972
1973 c = string_char_and_length (p, rest, &len);
1974 face_id = FACE_FOR_CHAR (it->f, face, c);
1975 }
1976 }
1977 else
1978 {
1979 if ((IT_CHARPOS (*it) >= ZV && !before_p)
1980 || (IT_CHARPOS (*it) <= BEGV && before_p))
1981 return it->face_id;
1982
1983 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
1984 pos = it->current.pos;
1985
1986 if (before_p)
1987 DEC_TEXT_POS (pos, it->multibyte_p);
1988 else
1989 {
1990 if (it->what == IT_COMPOSITION)
1991 /* For composition, we must check the position after the
1992 composition. */
1993 pos.charpos += it->cmp_len, pos.bytepos += it->len;
1994 else
1995 INC_TEXT_POS (pos, it->multibyte_p);
1996 }
1997 /* Determine face for CHARSET_ASCII, or unibyte. */
1998 face_id = face_at_buffer_position (it->w,
1999 CHARPOS (pos),
2000 it->region_beg_charpos,
2001 it->region_end_charpos,
2002 &next_check_charpos,
2003 limit, 0);
2004
2005 /* Correct the face for charsets different from ASCII. Do it
2006 for the multibyte case only. The face returned above is
2007 suitable for unibyte text if current_buffer is unibyte. */
2008 if (it->multibyte_p)
2009 {
2010 int c = FETCH_MULTIBYTE_CHAR (CHARPOS (pos));
2011 struct face *face = FACE_FROM_ID (it->f, face_id);
2012 face_id = FACE_FOR_CHAR (it->f, face, c);
2013 }
2014 }
2015
2016 return face_id;
2017 }
2018
2019
2020 \f
2021 /***********************************************************************
2022 Invisible text
2023 ***********************************************************************/
2024
2025 /* Set up iterator IT from invisible properties at its current
2026 position. Called from handle_stop. */
2027
2028 static enum prop_handled
2029 handle_invisible_prop (it)
2030 struct it *it;
2031 {
2032 enum prop_handled handled = HANDLED_NORMALLY;
2033
2034 if (STRINGP (it->string))
2035 {
2036 extern Lisp_Object Qinvisible;
2037 Lisp_Object prop, end_charpos, limit, charpos;
2038
2039 /* Get the value of the invisible text property at the
2040 current position. Value will be nil if there is no such
2041 property. */
2042 XSETFASTINT (charpos, IT_STRING_CHARPOS (*it));
2043 prop = Fget_text_property (charpos, Qinvisible, it->string);
2044
2045 if (!NILP (prop))
2046 {
2047 handled = HANDLED_RECOMPUTE_PROPS;
2048
2049 /* Get the position at which the next change of the
2050 invisible text property can be found in IT->string.
2051 Value will be nil if the property value is the same for
2052 all the rest of IT->string. */
2053 XSETINT (limit, XSTRING (it->string)->size);
2054 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2055 it->string, limit);
2056
2057 /* Text at current position is invisible. The next
2058 change in the property is at position end_charpos.
2059 Move IT's current position to that position. */
2060 if (INTEGERP (end_charpos)
2061 && XFASTINT (end_charpos) < XFASTINT (limit))
2062 {
2063 struct text_pos old;
2064 old = it->current.string_pos;
2065 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
2066 compute_string_pos (&it->current.string_pos, old, it->string);
2067 }
2068 else
2069 {
2070 /* The rest of the string is invisible. If this is an
2071 overlay string, proceed with the next overlay string
2072 or whatever comes and return a character from there. */
2073 if (it->current.overlay_string_index >= 0)
2074 {
2075 next_overlay_string (it);
2076 /* Don't check for overlay strings when we just
2077 finished processing them. */
2078 handled = HANDLED_OVERLAY_STRING_CONSUMED;
2079 }
2080 else
2081 {
2082 struct Lisp_String *s = XSTRING (it->string);
2083 IT_STRING_CHARPOS (*it) = s->size;
2084 IT_STRING_BYTEPOS (*it) = STRING_BYTES (s);
2085 }
2086 }
2087 }
2088 }
2089 else
2090 {
2091 int visible_p, newpos, next_stop;
2092 Lisp_Object pos, prop;
2093
2094 /* First of all, is there invisible text at this position? */
2095 XSETFASTINT (pos, IT_CHARPOS (*it));
2096 prop = Fget_char_property (pos, Qinvisible, it->window);
2097
2098 /* If we are on invisible text, skip over it. */
2099 if (TEXT_PROP_MEANS_INVISIBLE (prop))
2100 {
2101 /* Record whether we have to display an ellipsis for the
2102 invisible text. */
2103 int display_ellipsis_p
2104 = TEXT_PROP_MEANS_INVISIBLE_WITH_ELLIPSIS (prop);
2105
2106 handled = HANDLED_RECOMPUTE_PROPS;
2107
2108 /* Loop skipping over invisible text. The loop is left at
2109 ZV or with IT on the first char being visible again. */
2110 do
2111 {
2112 /* Try to skip some invisible text. Return value is the
2113 position reached which can be equal to IT's position
2114 if there is nothing invisible here. This skips both
2115 over invisible text properties and overlays with
2116 invisible property. */
2117 newpos = skip_invisible (IT_CHARPOS (*it),
2118 &next_stop, ZV, it->window);
2119
2120 /* If we skipped nothing at all we weren't at invisible
2121 text in the first place. If everything to the end of
2122 the buffer was skipped, end the loop. */
2123 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
2124 visible_p = 1;
2125 else
2126 {
2127 /* We skipped some characters but not necessarily
2128 all there are. Check if we ended up on visible
2129 text. Fget_char_property returns the property of
2130 the char before the given position, i.e. if we
2131 get visible_p = 1, this means that the char at
2132 newpos is visible. */
2133 XSETFASTINT (pos, newpos);
2134 prop = Fget_char_property (pos, Qinvisible, it->window);
2135 visible_p = !TEXT_PROP_MEANS_INVISIBLE (prop);
2136 }
2137
2138 /* If we ended up on invisible text, proceed to
2139 skip starting with next_stop. */
2140 if (!visible_p)
2141 IT_CHARPOS (*it) = next_stop;
2142 }
2143 while (!visible_p);
2144
2145 /* The position newpos is now either ZV or on visible text. */
2146 IT_CHARPOS (*it) = newpos;
2147 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2148
2149 /* Maybe return `...' next for the end of the invisible text. */
2150 if (display_ellipsis_p)
2151 {
2152 if (it->dp
2153 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
2154 {
2155 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
2156 it->dpvec = v->contents;
2157 it->dpend = v->contents + v->size;
2158 }
2159 else
2160 {
2161 /* Default `...'. */
2162 it->dpvec = default_invis_vector;
2163 it->dpend = default_invis_vector + 3;
2164 }
2165
2166 /* The ellipsis display does not replace the display of
2167 the character at the new position. Indicate this by
2168 setting IT->dpvec_char_len to zero. */
2169 it->dpvec_char_len = 0;
2170
2171 it->current.dpvec_index = 0;
2172 it->method = next_element_from_display_vector;
2173 }
2174 }
2175 }
2176
2177 return handled;
2178 }
2179
2180
2181 \f
2182 /***********************************************************************
2183 'display' property
2184 ***********************************************************************/
2185
2186 /* Set up iterator IT from `display' property at its current position.
2187 Called from handle_stop. */
2188
2189 static enum prop_handled
2190 handle_display_prop (it)
2191 struct it *it;
2192 {
2193 Lisp_Object prop, object;
2194 struct text_pos *position;
2195 int space_or_image_found_p;
2196
2197 if (STRINGP (it->string))
2198 {
2199 object = it->string;
2200 position = &it->current.string_pos;
2201 }
2202 else
2203 {
2204 object = Qnil;
2205 position = &it->current.pos;
2206 }
2207
2208 /* Reset those iterator values set from display property values. */
2209 it->font_height = Qnil;
2210 it->space_width = Qnil;
2211 it->voffset = 0;
2212
2213 /* We don't support recursive `display' properties, i.e. string
2214 values that have a string `display' property, that have a string
2215 `display' property etc. */
2216 if (!it->string_from_display_prop_p)
2217 it->area = TEXT_AREA;
2218
2219 prop = Fget_char_property (make_number (position->charpos),
2220 Qdisplay, object);
2221 if (NILP (prop))
2222 return HANDLED_NORMALLY;
2223
2224 space_or_image_found_p = 0;
2225 if (CONSP (prop)
2226 && CONSP (XCAR (prop))
2227 && !EQ (Qmargin, XCAR (XCAR (prop))))
2228 {
2229 /* A list of sub-properties. */
2230 while (CONSP (prop))
2231 {
2232 if (handle_single_display_prop (it, XCAR (prop), object, position))
2233 space_or_image_found_p = 1;
2234 prop = XCDR (prop);
2235 }
2236 }
2237 else if (VECTORP (prop))
2238 {
2239 int i;
2240 for (i = 0; i < XVECTOR (prop)->size; ++i)
2241 if (handle_single_display_prop (it, XVECTOR (prop)->contents[i],
2242 object, position))
2243 space_or_image_found_p = 1;
2244 }
2245 else
2246 {
2247 if (handle_single_display_prop (it, prop, object, position))
2248 space_or_image_found_p = 1;
2249 }
2250
2251 return space_or_image_found_p ? HANDLED_RETURN : HANDLED_NORMALLY;
2252 }
2253
2254
2255 /* Value is the position of the end of the `display' property starting
2256 at START_POS in OBJECT. */
2257
2258 static struct text_pos
2259 display_prop_end (it, object, start_pos)
2260 struct it *it;
2261 Lisp_Object object;
2262 struct text_pos start_pos;
2263 {
2264 Lisp_Object end;
2265 struct text_pos end_pos;
2266
2267 end = next_single_char_property_change (make_number (CHARPOS (start_pos)),
2268 Qdisplay, object, Qnil);
2269 CHARPOS (end_pos) = XFASTINT (end);
2270 if (STRINGP (object))
2271 compute_string_pos (&end_pos, start_pos, it->string);
2272 else
2273 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
2274
2275 return end_pos;
2276 }
2277
2278
2279 /* Set up IT from a single `display' sub-property value PROP. OBJECT
2280 is the object in which the `display' property was found. *POSITION
2281 is the position at which it was found.
2282
2283 If PROP is a `space' or `image' sub-property, set *POSITION to the
2284 end position of the `display' property.
2285
2286 Value is non-zero if a `space' or `image' property value was found. */
2287
2288 static int
2289 handle_single_display_prop (it, prop, object, position)
2290 struct it *it;
2291 Lisp_Object prop;
2292 Lisp_Object object;
2293 struct text_pos *position;
2294 {
2295 Lisp_Object value;
2296 int space_or_image_found_p = 0;
2297
2298 Lisp_Object form;
2299
2300 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
2301 evaluated. If the result is nil, VALUE is ignored. */
2302 form = Qt;
2303 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2304 {
2305 prop = XCDR (prop);
2306 if (!CONSP (prop))
2307 return 0;
2308 form = XCAR (prop);
2309 prop = XCDR (prop);
2310 }
2311
2312 if (!NILP (form) && !EQ (form, Qt))
2313 {
2314 struct gcpro gcpro1;
2315 struct text_pos end_pos, pt;
2316
2317 end_pos = display_prop_end (it, object, *position);
2318 GCPRO1 (form);
2319
2320 /* Temporarily set point to the end position, and then evaluate
2321 the form. This makes `(eolp)' work as FORM. */
2322 CHARPOS (pt) = PT;
2323 BYTEPOS (pt) = PT_BYTE;
2324 TEMP_SET_PT_BOTH (CHARPOS (end_pos), BYTEPOS (end_pos));
2325 form = eval_form (form);
2326 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
2327 UNGCPRO;
2328 }
2329
2330 if (NILP (form))
2331 return 0;
2332
2333 if (CONSP (prop)
2334 && EQ (XCAR (prop), Qheight)
2335 && CONSP (XCDR (prop)))
2336 {
2337 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2338 return 0;
2339
2340 /* `(height HEIGHT)'. */
2341 it->font_height = XCAR (XCDR (prop));
2342 if (!NILP (it->font_height))
2343 {
2344 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2345 int new_height = -1;
2346
2347 if (CONSP (it->font_height)
2348 && (EQ (XCAR (it->font_height), Qplus)
2349 || EQ (XCAR (it->font_height), Qminus))
2350 && CONSP (XCDR (it->font_height))
2351 && INTEGERP (XCAR (XCDR (it->font_height))))
2352 {
2353 /* `(+ N)' or `(- N)' where N is an integer. */
2354 int steps = XINT (XCAR (XCDR (it->font_height)));
2355 if (EQ (XCAR (it->font_height), Qplus))
2356 steps = - steps;
2357 it->face_id = smaller_face (it->f, it->face_id, steps);
2358 }
2359 else if (SYMBOLP (it->font_height))
2360 {
2361 /* Call function with current height as argument.
2362 Value is the new height. */
2363 Lisp_Object form, height;
2364 struct gcpro gcpro1;
2365
2366 height = face->lface[LFACE_HEIGHT_INDEX];
2367 form = Fcons (it->font_height, Fcons (height, Qnil));
2368 GCPRO1 (form);
2369 height = eval_form (form);
2370 if (NUMBERP (height))
2371 new_height = XFLOATINT (height);
2372 UNGCPRO;
2373 }
2374 else if (NUMBERP (it->font_height))
2375 {
2376 /* Value is a multiple of the canonical char height. */
2377 struct face *face;
2378
2379 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
2380 new_height = (XFLOATINT (it->font_height)
2381 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
2382 }
2383 else
2384 {
2385 /* Evaluate IT->font_height with `height' bound to the
2386 current specified height to get the new height. */
2387 Lisp_Object value;
2388 int count = specpdl_ptr - specpdl;
2389
2390 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
2391 value = eval_form (it->font_height);
2392 unbind_to (count, Qnil);
2393
2394 if (NUMBERP (value))
2395 new_height = XFLOATINT (value);
2396 }
2397
2398 if (new_height > 0)
2399 it->face_id = face_with_height (it->f, it->face_id, new_height);
2400 }
2401 }
2402 else if (CONSP (prop)
2403 && EQ (XCAR (prop), Qspace_width)
2404 && CONSP (XCDR (prop)))
2405 {
2406 /* `(space_width WIDTH)'. */
2407 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2408 return 0;
2409
2410 value = XCAR (XCDR (prop));
2411 if (NUMBERP (value) && XFLOATINT (value) > 0)
2412 it->space_width = value;
2413 }
2414 else if (CONSP (prop)
2415 && EQ (XCAR (prop), Qraise)
2416 && CONSP (XCDR (prop)))
2417 {
2418 /* `(raise FACTOR)'. */
2419 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2420 return 0;
2421
2422 #ifdef HAVE_WINDOW_SYSTEM
2423 value = XCAR (XCDR (prop));
2424 if (NUMBERP (value))
2425 {
2426 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2427 it->voffset = - (XFLOATINT (value)
2428 * (FONT_HEIGHT (face->font)));
2429 }
2430 #endif /* HAVE_WINDOW_SYSTEM */
2431 }
2432 else if (!it->string_from_display_prop_p)
2433 {
2434 /* `((margin left-margin) VALUE)' or `((margin right-margin)
2435 VALUE) or `((margin nil) VALUE)' or VALUE. */
2436 Lisp_Object location, value;
2437 struct text_pos start_pos;
2438 int valid_p;
2439
2440 /* Characters having this form of property are not displayed, so
2441 we have to find the end of the property. */
2442 space_or_image_found_p = 1;
2443 start_pos = *position;
2444 *position = display_prop_end (it, object, start_pos);
2445 value = Qnil;
2446
2447 /* Let's stop at the new position and assume that all
2448 text properties change there. */
2449 it->stop_charpos = position->charpos;
2450
2451 location = Qunbound;
2452 if (CONSP (prop) && CONSP (XCAR (prop)))
2453 {
2454 Lisp_Object tem;
2455
2456 value = XCDR (prop);
2457 if (CONSP (value))
2458 value = XCAR (value);
2459
2460 tem = XCAR (prop);
2461 if (EQ (XCAR (tem), Qmargin)
2462 && (tem = XCDR (tem),
2463 tem = CONSP (tem) ? XCAR (tem) : Qnil,
2464 (NILP (tem)
2465 || EQ (tem, Qleft_margin)
2466 || EQ (tem, Qright_margin))))
2467 location = tem;
2468 }
2469
2470 if (EQ (location, Qunbound))
2471 {
2472 location = Qnil;
2473 value = prop;
2474 }
2475
2476 #ifdef HAVE_WINDOW_SYSTEM
2477 if (FRAME_TERMCAP_P (it->f))
2478 valid_p = STRINGP (value);
2479 else
2480 valid_p = (STRINGP (value)
2481 || (CONSP (value) && EQ (XCAR (value), Qspace))
2482 || valid_image_p (value));
2483 #else /* not HAVE_WINDOW_SYSTEM */
2484 valid_p = STRINGP (value);
2485 #endif /* not HAVE_WINDOW_SYSTEM */
2486
2487 if ((EQ (location, Qleft_margin)
2488 || EQ (location, Qright_margin)
2489 || NILP (location))
2490 && valid_p)
2491 {
2492 /* Save current settings of IT so that we can restore them
2493 when we are finished with the glyph property value. */
2494 push_it (it);
2495
2496 if (NILP (location))
2497 it->area = TEXT_AREA;
2498 else if (EQ (location, Qleft_margin))
2499 it->area = LEFT_MARGIN_AREA;
2500 else
2501 it->area = RIGHT_MARGIN_AREA;
2502
2503 if (STRINGP (value))
2504 {
2505 it->string = value;
2506 it->multibyte_p = STRING_MULTIBYTE (it->string);
2507 it->current.overlay_string_index = -1;
2508 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
2509 it->end_charpos = it->string_nchars
2510 = XSTRING (it->string)->size;
2511 it->method = next_element_from_string;
2512 it->stop_charpos = 0;
2513 it->string_from_display_prop_p = 1;
2514 }
2515 else if (CONSP (value) && EQ (XCAR (value), Qspace))
2516 {
2517 it->method = next_element_from_stretch;
2518 it->object = value;
2519 it->current.pos = it->position = start_pos;
2520 }
2521 #ifdef HAVE_WINDOW_SYSTEM
2522 else
2523 {
2524 it->what = IT_IMAGE;
2525 it->image_id = lookup_image (it->f, value);
2526 it->position = start_pos;
2527 it->object = NILP (object) ? it->w->buffer : object;
2528 it->method = next_element_from_image;
2529
2530 /* Say that we don't have consumed the characters with
2531 `display' property yet. The call to pop_it in
2532 set_iterator_to_next will clean this up. */
2533 *position = start_pos;
2534 }
2535 #endif /* HAVE_WINDOW_SYSTEM */
2536 }
2537 }
2538
2539 return space_or_image_found_p;
2540 }
2541
2542
2543 \f
2544 /***********************************************************************
2545 `composition' property
2546 ***********************************************************************/
2547
2548 /* Set up iterator IT from `composition' property at its current
2549 position. Called from handle_stop. */
2550
2551 static enum prop_handled
2552 handle_composition_prop (it)
2553 struct it *it;
2554 {
2555 Lisp_Object prop, string;
2556 int pos, pos_byte, end;
2557 enum prop_handled handled = HANDLED_NORMALLY;
2558
2559 if (STRINGP (it->string))
2560 {
2561 pos = IT_STRING_CHARPOS (*it);
2562 pos_byte = IT_STRING_BYTEPOS (*it);
2563 string = it->string;
2564 }
2565 else
2566 {
2567 pos = IT_CHARPOS (*it);
2568 pos_byte = IT_BYTEPOS (*it);
2569 string = Qnil;
2570 }
2571
2572 /* If there's a valid composition and point is not inside of the
2573 composition (in the case that the composition is from the current
2574 buffer), draw a glyph composed from the composition components. */
2575 if (find_composition (pos, -1, &pos, &end, &prop, string)
2576 && COMPOSITION_VALID_P (pos, end, prop)
2577 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
2578 {
2579 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
2580
2581 if (id >= 0)
2582 {
2583 it->method = next_element_from_composition;
2584 it->cmp_id = id;
2585 it->cmp_len = COMPOSITION_LENGTH (prop);
2586 /* For a terminal, draw only the first character of the
2587 components. */
2588 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
2589 it->len = (STRINGP (it->string)
2590 ? string_char_to_byte (it->string, end)
2591 : CHAR_TO_BYTE (end)) - pos_byte;
2592 it->stop_charpos = end;
2593 handled = HANDLED_RETURN;
2594 }
2595 }
2596
2597 return handled;
2598 }
2599
2600
2601 \f
2602 /***********************************************************************
2603 Overlay strings
2604 ***********************************************************************/
2605
2606 /* The following structure is used to record overlay strings for
2607 later sorting in load_overlay_strings. */
2608
2609 struct overlay_entry
2610 {
2611 Lisp_Object string;
2612 int priority;
2613 int after_string_p;
2614 };
2615
2616
2617 /* Set up iterator IT from overlay strings at its current position.
2618 Called from handle_stop. */
2619
2620 static enum prop_handled
2621 handle_overlay_change (it)
2622 struct it *it;
2623 {
2624 /* Overlays are handled in current_buffer only. */
2625 if (STRINGP (it->string))
2626 return HANDLED_NORMALLY;
2627 else
2628 return (get_overlay_strings (it)
2629 ? HANDLED_RECOMPUTE_PROPS
2630 : HANDLED_NORMALLY);
2631 }
2632
2633
2634 /* Set up the next overlay string for delivery by IT, if there is an
2635 overlay string to deliver. Called by set_iterator_to_next when the
2636 end of the current overlay string is reached. If there are more
2637 overlay strings to display, IT->string and
2638 IT->current.overlay_string_index are set appropriately here.
2639 Otherwise IT->string is set to nil. */
2640
2641 static void
2642 next_overlay_string (it)
2643 struct it *it;
2644 {
2645 ++it->current.overlay_string_index;
2646 if (it->current.overlay_string_index == it->n_overlay_strings)
2647 {
2648 /* No more overlay strings. Restore IT's settings to what
2649 they were before overlay strings were processed, and
2650 continue to deliver from current_buffer. */
2651 pop_it (it);
2652 xassert (it->stop_charpos >= BEGV
2653 && it->stop_charpos <= it->end_charpos);
2654 it->string = Qnil;
2655 it->current.overlay_string_index = -1;
2656 SET_TEXT_POS (it->current.string_pos, -1, -1);
2657 it->n_overlay_strings = 0;
2658 it->method = next_element_from_buffer;
2659 }
2660 else
2661 {
2662 /* There are more overlay strings to process. If
2663 IT->current.overlay_string_index has advanced to a position
2664 where we must load IT->overlay_strings with more strings, do
2665 it. */
2666 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
2667
2668 if (it->current.overlay_string_index && i == 0)
2669 load_overlay_strings (it);
2670
2671 /* Initialize IT to deliver display elements from the overlay
2672 string. */
2673 it->string = it->overlay_strings[i];
2674 it->multibyte_p = STRING_MULTIBYTE (it->string);
2675 SET_TEXT_POS (it->current.string_pos, 0, 0);
2676 it->method = next_element_from_string;
2677 it->stop_charpos = 0;
2678 }
2679
2680 CHECK_IT (it);
2681 }
2682
2683
2684 /* Compare two overlay_entry structures E1 and E2. Used as a
2685 comparison function for qsort in load_overlay_strings. Overlay
2686 strings for the same position are sorted so that
2687
2688 1. All after-strings come in front of before-strings.
2689
2690 2. Within after-strings, strings are sorted so that overlay strings
2691 from overlays with higher priorities come first.
2692
2693 2. Within before-strings, strings are sorted so that overlay
2694 strings from overlays with higher priorities come last.
2695
2696 Value is analogous to strcmp. */
2697
2698
2699 static int
2700 compare_overlay_entries (e1, e2)
2701 void *e1, *e2;
2702 {
2703 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
2704 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
2705 int result;
2706
2707 if (entry1->after_string_p != entry2->after_string_p)
2708 /* Let after-strings appear in front of before-strings. */
2709 result = entry1->after_string_p ? -1 : 1;
2710 else if (entry1->after_string_p)
2711 /* After-strings sorted in order of decreasing priority. */
2712 result = entry2->priority - entry1->priority;
2713 else
2714 /* Before-strings sorted in order of increasing priority. */
2715 result = entry1->priority - entry2->priority;
2716
2717 return result;
2718 }
2719
2720
2721 /* Load the vector IT->overlay_strings with overlay strings from IT's
2722 current buffer position. Set IT->n_overlays to the total number of
2723 overlay strings found.
2724
2725 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
2726 a time. On entry into load_overlay_strings,
2727 IT->current.overlay_string_index gives the number of overlay
2728 strings that have already been loaded by previous calls to this
2729 function.
2730
2731 Overlay strings are sorted so that after-string strings come in
2732 front of before-string strings. Within before and after-strings,
2733 strings are sorted by overlay priority. See also function
2734 compare_overlay_entries. */
2735
2736 static void
2737 load_overlay_strings (it)
2738 struct it *it;
2739 {
2740 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
2741 Lisp_Object ov, overlay, window, str;
2742 int start, end;
2743 int size = 20;
2744 int n = 0, i, j;
2745 struct overlay_entry *entries
2746 = (struct overlay_entry *) alloca (size * sizeof *entries);
2747
2748 /* Append the overlay string STRING of overlay OVERLAY to vector
2749 `entries' which has size `size' and currently contains `n'
2750 elements. AFTER_P non-zero means STRING is an after-string of
2751 OVERLAY. */
2752 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
2753 do \
2754 { \
2755 Lisp_Object priority; \
2756 \
2757 if (n == size) \
2758 { \
2759 int new_size = 2 * size; \
2760 struct overlay_entry *old = entries; \
2761 entries = \
2762 (struct overlay_entry *) alloca (new_size \
2763 * sizeof *entries); \
2764 bcopy (old, entries, size * sizeof *entries); \
2765 size = new_size; \
2766 } \
2767 \
2768 entries[n].string = (STRING); \
2769 priority = Foverlay_get ((OVERLAY), Qpriority); \
2770 entries[n].priority \
2771 = INTEGERP (priority) ? XFASTINT (priority) : 0; \
2772 entries[n].after_string_p = (AFTER_P); \
2773 ++n; \
2774 } \
2775 while (0)
2776
2777 /* Process overlay before the overlay center. */
2778 for (ov = current_buffer->overlays_before;
2779 CONSP (ov);
2780 ov = XCDR (ov))
2781 {
2782 overlay = XCAR (ov);
2783 xassert (OVERLAYP (overlay));
2784 start = OVERLAY_POSITION (OVERLAY_START (overlay));
2785 end = OVERLAY_POSITION (OVERLAY_END (overlay));
2786
2787 if (end < IT_CHARPOS (*it))
2788 break;
2789
2790 /* Skip this overlay if it doesn't start or end at IT's current
2791 position. */
2792 if (end != IT_CHARPOS (*it) && start != IT_CHARPOS (*it))
2793 continue;
2794
2795 /* Skip this overlay if it doesn't apply to IT->w. */
2796 window = Foverlay_get (overlay, Qwindow);
2797 if (WINDOWP (window) && XWINDOW (window) != it->w)
2798 continue;
2799
2800 /* If overlay has a non-empty before-string, record it. */
2801 if (start == IT_CHARPOS (*it)
2802 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
2803 && XSTRING (str)->size)
2804 RECORD_OVERLAY_STRING (overlay, str, 0);
2805
2806 /* If overlay has a non-empty after-string, record it. */
2807 if (end == IT_CHARPOS (*it)
2808 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
2809 && XSTRING (str)->size)
2810 RECORD_OVERLAY_STRING (overlay, str, 1);
2811 }
2812
2813 /* Process overlays after the overlay center. */
2814 for (ov = current_buffer->overlays_after;
2815 CONSP (ov);
2816 ov = XCDR (ov))
2817 {
2818 overlay = XCAR (ov);
2819 xassert (OVERLAYP (overlay));
2820 start = OVERLAY_POSITION (OVERLAY_START (overlay));
2821 end = OVERLAY_POSITION (OVERLAY_END (overlay));
2822
2823 if (start > IT_CHARPOS (*it))
2824 break;
2825
2826 /* Skip this overlay if it doesn't start or end at IT's current
2827 position. */
2828 if (end != IT_CHARPOS (*it) && start != IT_CHARPOS (*it))
2829 continue;
2830
2831 /* Skip this overlay if it doesn't apply to IT->w. */
2832 window = Foverlay_get (overlay, Qwindow);
2833 if (WINDOWP (window) && XWINDOW (window) != it->w)
2834 continue;
2835
2836 /* If overlay has a non-empty before-string, record it. */
2837 if (start == IT_CHARPOS (*it)
2838 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
2839 && XSTRING (str)->size)
2840 RECORD_OVERLAY_STRING (overlay, str, 0);
2841
2842 /* If overlay has a non-empty after-string, record it. */
2843 if (end == IT_CHARPOS (*it)
2844 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
2845 && XSTRING (str)->size)
2846 RECORD_OVERLAY_STRING (overlay, str, 1);
2847 }
2848
2849 #undef RECORD_OVERLAY_STRING
2850
2851 /* Sort entries. */
2852 qsort (entries, n, sizeof *entries, compare_overlay_entries);
2853
2854 /* Record the total number of strings to process. */
2855 it->n_overlay_strings = n;
2856
2857 /* IT->current.overlay_string_index is the number of overlay strings
2858 that have already been consumed by IT. Copy some of the
2859 remaining overlay strings to IT->overlay_strings. */
2860 i = 0;
2861 j = it->current.overlay_string_index;
2862 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
2863 it->overlay_strings[i++] = entries[j++].string;
2864
2865 CHECK_IT (it);
2866 }
2867
2868
2869 /* Get the first chunk of overlay strings at IT's current buffer
2870 position. Value is non-zero if at least one overlay string was
2871 found. */
2872
2873 static int
2874 get_overlay_strings (it)
2875 struct it *it;
2876 {
2877 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
2878 process. This fills IT->overlay_strings with strings, and sets
2879 IT->n_overlay_strings to the total number of strings to process.
2880 IT->pos.overlay_string_index has to be set temporarily to zero
2881 because load_overlay_strings needs this; it must be set to -1
2882 when no overlay strings are found because a zero value would
2883 indicate a position in the first overlay string. */
2884 it->current.overlay_string_index = 0;
2885 load_overlay_strings (it);
2886
2887 /* If we found overlay strings, set up IT to deliver display
2888 elements from the first one. Otherwise set up IT to deliver
2889 from current_buffer. */
2890 if (it->n_overlay_strings)
2891 {
2892 /* Make sure we know settings in current_buffer, so that we can
2893 restore meaningful values when we're done with the overlay
2894 strings. */
2895 compute_stop_pos (it);
2896 xassert (it->face_id >= 0);
2897
2898 /* Save IT's settings. They are restored after all overlay
2899 strings have been processed. */
2900 xassert (it->sp == 0);
2901 push_it (it);
2902
2903 /* Set up IT to deliver display elements from the first overlay
2904 string. */
2905 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
2906 it->stop_charpos = 0;
2907 it->string = it->overlay_strings[0];
2908 it->multibyte_p = STRING_MULTIBYTE (it->string);
2909 xassert (STRINGP (it->string));
2910 it->method = next_element_from_string;
2911 }
2912 else
2913 {
2914 it->string = Qnil;
2915 it->current.overlay_string_index = -1;
2916 it->method = next_element_from_buffer;
2917 }
2918
2919 CHECK_IT (it);
2920
2921 /* Value is non-zero if we found at least one overlay string. */
2922 return STRINGP (it->string);
2923 }
2924
2925
2926 \f
2927 /***********************************************************************
2928 Saving and restoring state
2929 ***********************************************************************/
2930
2931 /* Save current settings of IT on IT->stack. Called, for example,
2932 before setting up IT for an overlay string, to be able to restore
2933 IT's settings to what they were after the overlay string has been
2934 processed. */
2935
2936 static void
2937 push_it (it)
2938 struct it *it;
2939 {
2940 struct iterator_stack_entry *p;
2941
2942 xassert (it->sp < 2);
2943 p = it->stack + it->sp;
2944
2945 p->stop_charpos = it->stop_charpos;
2946 xassert (it->face_id >= 0);
2947 p->face_id = it->face_id;
2948 p->string = it->string;
2949 p->pos = it->current;
2950 p->end_charpos = it->end_charpos;
2951 p->string_nchars = it->string_nchars;
2952 p->area = it->area;
2953 p->multibyte_p = it->multibyte_p;
2954 p->space_width = it->space_width;
2955 p->font_height = it->font_height;
2956 p->voffset = it->voffset;
2957 p->string_from_display_prop_p = it->string_from_display_prop_p;
2958 ++it->sp;
2959 }
2960
2961
2962 /* Restore IT's settings from IT->stack. Called, for example, when no
2963 more overlay strings must be processed, and we return to delivering
2964 display elements from a buffer, or when the end of a string from a
2965 `display' property is reached and we return to delivering display
2966 elements from an overlay string, or from a buffer. */
2967
2968 static void
2969 pop_it (it)
2970 struct it *it;
2971 {
2972 struct iterator_stack_entry *p;
2973
2974 xassert (it->sp > 0);
2975 --it->sp;
2976 p = it->stack + it->sp;
2977 it->stop_charpos = p->stop_charpos;
2978 it->face_id = p->face_id;
2979 it->string = p->string;
2980 it->current = p->pos;
2981 it->end_charpos = p->end_charpos;
2982 it->string_nchars = p->string_nchars;
2983 it->area = p->area;
2984 it->multibyte_p = p->multibyte_p;
2985 it->space_width = p->space_width;
2986 it->font_height = p->font_height;
2987 it->voffset = p->voffset;
2988 it->string_from_display_prop_p = p->string_from_display_prop_p;
2989 }
2990
2991
2992 \f
2993 /***********************************************************************
2994 Moving over lines
2995 ***********************************************************************/
2996
2997 /* Set IT's current position to the previous line start. */
2998
2999 static void
3000 back_to_previous_line_start (it)
3001 struct it *it;
3002 {
3003 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
3004 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3005 }
3006
3007
3008 /* Set IT's current position to the next line start. */
3009
3010 static void
3011 forward_to_next_line_start (it)
3012 struct it *it;
3013 {
3014 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it), 1);
3015 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3016 }
3017
3018
3019 /* Set IT's current position to the previous visible line start. Skip
3020 invisible text that is so either due to text properties or due to
3021 selective display. Caution: this does not change IT->current_x and
3022 IT->hpos. */
3023
3024 static void
3025 back_to_previous_visible_line_start (it)
3026 struct it *it;
3027 {
3028 int visible_p = 0;
3029
3030 /* Go back one newline if not on BEGV already. */
3031 if (IT_CHARPOS (*it) > BEGV)
3032 back_to_previous_line_start (it);
3033
3034 /* Move over lines that are invisible because of selective display
3035 or text properties. */
3036 while (IT_CHARPOS (*it) > BEGV
3037 && !visible_p)
3038 {
3039 visible_p = 1;
3040
3041 /* If selective > 0, then lines indented more than that values
3042 are invisible. */
3043 if (it->selective > 0
3044 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3045 it->selective))
3046 visible_p = 0;
3047 else
3048 {
3049 Lisp_Object prop;
3050
3051 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
3052 Qinvisible, it->window);
3053 if (TEXT_PROP_MEANS_INVISIBLE (prop))
3054 visible_p = 0;
3055 }
3056
3057 /* Back one more newline if the current one is invisible. */
3058 if (!visible_p)
3059 back_to_previous_line_start (it);
3060 }
3061
3062 xassert (IT_CHARPOS (*it) >= BEGV);
3063 xassert (IT_CHARPOS (*it) == BEGV
3064 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3065 CHECK_IT (it);
3066 }
3067
3068
3069 /* Reseat iterator IT at the previous visible line start. Skip
3070 invisible text that is so either due to text properties or due to
3071 selective display. At the end, update IT's overlay information,
3072 face information etc. */
3073
3074 static void
3075 reseat_at_previous_visible_line_start (it)
3076 struct it *it;
3077 {
3078 back_to_previous_visible_line_start (it);
3079 reseat (it, it->current.pos, 1);
3080 CHECK_IT (it);
3081 }
3082
3083
3084 /* Reseat iterator IT on the next visible line start in the current
3085 buffer. ON_NEWLINE_P non-zero means position IT on the newline
3086 preceding the line start. Skip over invisible text that is so
3087 because of selective display. Compute faces, overlays etc at the
3088 new position. Note that this function does not skip over text that
3089 is invisible because of text properties. */
3090
3091 static void
3092 reseat_at_next_visible_line_start (it, on_newline_p)
3093 struct it *it;
3094 int on_newline_p;
3095 {
3096 /* Restore the buffer position when currently not delivering display
3097 elements from the current buffer. This is the case, for example,
3098 when called at the end of a truncated overlay string. */
3099 while (it->sp)
3100 pop_it (it);
3101 it->method = next_element_from_buffer;
3102
3103 /* Otherwise, scan_buffer would not work. */
3104 if (IT_CHARPOS (*it) < ZV)
3105 {
3106 /* If on a newline, advance past it. Otherwise, find the next
3107 newline which automatically gives us the position following
3108 the newline. */
3109 if (FETCH_BYTE (IT_BYTEPOS (*it)) == '\n')
3110 {
3111 ++IT_CHARPOS (*it);
3112 ++IT_BYTEPOS (*it);
3113 }
3114 else
3115 forward_to_next_line_start (it);
3116
3117 /* We must either have reached the end of the buffer or end up
3118 after a newline. */
3119 xassert (IT_CHARPOS (*it) == ZV
3120 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3121
3122 /* Skip over lines that are invisible because they are indented
3123 more than the value of IT->selective. */
3124 if (it->selective > 0)
3125 while (IT_CHARPOS (*it) < ZV
3126 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3127 it->selective))
3128 forward_to_next_line_start (it);
3129
3130 /* Position on the newline if we should. */
3131 if (on_newline_p
3132 && IT_CHARPOS (*it) > BEGV
3133 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n')
3134 {
3135 --IT_CHARPOS (*it);
3136 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3137 }
3138
3139 /* Set the iterator there. The 0 as the last parameter of
3140 reseat means don't force a text property lookup. The lookup
3141 is then only done if we've skipped past the iterator's
3142 check_charpos'es. This optimization is important because
3143 text property lookups tend to be expensive. */
3144 reseat (it, it->current.pos, 0);
3145 }
3146
3147 CHECK_IT (it);
3148 }
3149
3150
3151 \f
3152 /***********************************************************************
3153 Changing an iterator's position
3154 ***********************************************************************/
3155
3156 /* Change IT's current position to POS in current_buffer. If FORCE_P
3157 is non-zero, always check for text properties at the new position.
3158 Otherwise, text properties are only looked up if POS >=
3159 IT->check_charpos of a property. */
3160
3161 static void
3162 reseat (it, pos, force_p)
3163 struct it *it;
3164 struct text_pos pos;
3165 int force_p;
3166 {
3167 int original_pos = IT_CHARPOS (*it);
3168
3169 reseat_1 (it, pos, 0);
3170
3171 /* Determine where to check text properties. Avoid doing it
3172 where possible because text property lookup is very expensive. */
3173 if (force_p
3174 || CHARPOS (pos) > it->stop_charpos
3175 || CHARPOS (pos) < original_pos)
3176 handle_stop (it);
3177
3178 CHECK_IT (it);
3179 }
3180
3181
3182 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
3183 IT->stop_pos to POS, also. */
3184
3185 static void
3186 reseat_1 (it, pos, set_stop_p)
3187 struct it *it;
3188 struct text_pos pos;
3189 int set_stop_p;
3190 {
3191 /* Don't call this function when scanning a C string. */
3192 xassert (it->s == NULL);
3193
3194 /* POS must be a reasonable value. */
3195 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
3196
3197 it->current.pos = it->position = pos;
3198 XSETBUFFER (it->object, current_buffer);
3199 it->dpvec = NULL;
3200 it->current.dpvec_index = -1;
3201 it->current.overlay_string_index = -1;
3202 IT_STRING_CHARPOS (*it) = -1;
3203 IT_STRING_BYTEPOS (*it) = -1;
3204 it->string = Qnil;
3205 it->method = next_element_from_buffer;
3206 it->sp = 0;
3207
3208 if (set_stop_p)
3209 it->stop_charpos = CHARPOS (pos);
3210 }
3211
3212
3213 /* Set up IT for displaying a string, starting at CHARPOS in window W.
3214 If S is non-null, it is a C string to iterate over. Otherwise,
3215 STRING gives a Lisp string to iterate over.
3216
3217 If PRECISION > 0, don't return more then PRECISION number of
3218 characters from the string.
3219
3220 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
3221 characters have been returned. FIELD_WIDTH < 0 means an infinite
3222 field width.
3223
3224 MULTIBYTE = 0 means disable processing of multibyte characters,
3225 MULTIBYTE > 0 means enable it,
3226 MULTIBYTE < 0 means use IT->multibyte_p.
3227
3228 IT must be initialized via a prior call to init_iterator before
3229 calling this function. */
3230
3231 static void
3232 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
3233 struct it *it;
3234 unsigned char *s;
3235 Lisp_Object string;
3236 int charpos;
3237 int precision, field_width, multibyte;
3238 {
3239 /* No region in strings. */
3240 it->region_beg_charpos = it->region_end_charpos = -1;
3241
3242 /* No text property checks performed by default, but see below. */
3243 it->stop_charpos = -1;
3244
3245 /* Set iterator position and end position. */
3246 bzero (&it->current, sizeof it->current);
3247 it->current.overlay_string_index = -1;
3248 it->current.dpvec_index = -1;
3249 xassert (charpos >= 0);
3250
3251 /* Use the setting of MULTIBYTE if specified. */
3252 if (multibyte >= 0)
3253 it->multibyte_p = multibyte > 0;
3254
3255 if (s == NULL)
3256 {
3257 xassert (STRINGP (string));
3258 it->string = string;
3259 it->s = NULL;
3260 it->end_charpos = it->string_nchars = XSTRING (string)->size;
3261 it->method = next_element_from_string;
3262 it->current.string_pos = string_pos (charpos, string);
3263 }
3264 else
3265 {
3266 it->s = s;
3267 it->string = Qnil;
3268
3269 /* Note that we use IT->current.pos, not it->current.string_pos,
3270 for displaying C strings. */
3271 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
3272 if (it->multibyte_p)
3273 {
3274 it->current.pos = c_string_pos (charpos, s, 1);
3275 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
3276 }
3277 else
3278 {
3279 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
3280 it->end_charpos = it->string_nchars = strlen (s);
3281 }
3282
3283 it->method = next_element_from_c_string;
3284 }
3285
3286 /* PRECISION > 0 means don't return more than PRECISION characters
3287 from the string. */
3288 if (precision > 0 && it->end_charpos - charpos > precision)
3289 it->end_charpos = it->string_nchars = charpos + precision;
3290
3291 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
3292 characters have been returned. FIELD_WIDTH == 0 means don't pad,
3293 FIELD_WIDTH < 0 means infinite field width. This is useful for
3294 padding with `-' at the end of a mode line. */
3295 if (field_width < 0)
3296 field_width = INFINITY;
3297 if (field_width > it->end_charpos - charpos)
3298 it->end_charpos = charpos + field_width;
3299
3300 /* Use the standard display table for displaying strings. */
3301 if (DISP_TABLE_P (Vstandard_display_table))
3302 it->dp = XCHAR_TABLE (Vstandard_display_table);
3303
3304 it->stop_charpos = charpos;
3305 CHECK_IT (it);
3306 }
3307
3308
3309 \f
3310 /***********************************************************************
3311 Iteration
3312 ***********************************************************************/
3313
3314 /* Load IT's display element fields with information about the next
3315 display element from the current position of IT. Value is zero if
3316 end of buffer (or C string) is reached. */
3317
3318 int
3319 get_next_display_element (it)
3320 struct it *it;
3321 {
3322 /* Non-zero means that we found an display element. Zero means that
3323 we hit the end of what we iterate over. Performance note: the
3324 function pointer `method' used here turns out to be faster than
3325 using a sequence of if-statements. */
3326 int success_p = (*it->method) (it);
3327
3328 if (it->what == IT_CHARACTER)
3329 {
3330 /* Map via display table or translate control characters.
3331 IT->c, IT->len etc. have been set to the next character by
3332 the function call above. If we have a display table, and it
3333 contains an entry for IT->c, translate it. Don't do this if
3334 IT->c itself comes from a display table, otherwise we could
3335 end up in an infinite recursion. (An alternative could be to
3336 count the recursion depth of this function and signal an
3337 error when a certain maximum depth is reached.) Is it worth
3338 it? */
3339 if (success_p && it->dpvec == NULL)
3340 {
3341 Lisp_Object dv;
3342
3343 if (it->dp
3344 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
3345 VECTORP (dv)))
3346 {
3347 struct Lisp_Vector *v = XVECTOR (dv);
3348
3349 /* Return the first character from the display table
3350 entry, if not empty. If empty, don't display the
3351 current character. */
3352 if (v->size)
3353 {
3354 it->dpvec_char_len = it->len;
3355 it->dpvec = v->contents;
3356 it->dpend = v->contents + v->size;
3357 it->current.dpvec_index = 0;
3358 it->method = next_element_from_display_vector;
3359 }
3360
3361 success_p = get_next_display_element (it);
3362 }
3363
3364 /* Translate control characters into `\003' or `^C' form.
3365 Control characters coming from a display table entry are
3366 currently not translated because we use IT->dpvec to hold
3367 the translation. This could easily be changed but I
3368 don't believe that it is worth doing.
3369
3370 Non-printable multibyte characters are also translated
3371 octal form. */
3372 else if ((it->c < ' '
3373 && (it->area != TEXT_AREA
3374 || (it->c != '\n' && it->c != '\t')))
3375 || (it->c >= 127
3376 && it->len == 1)
3377 || !CHAR_PRINTABLE_P (it->c))
3378 {
3379 /* IT->c is a control character which must be displayed
3380 either as '\003' or as `^C' where the '\\' and '^'
3381 can be defined in the display table. Fill
3382 IT->ctl_chars with glyphs for what we have to
3383 display. Then, set IT->dpvec to these glyphs. */
3384 GLYPH g;
3385
3386 if (it->c < 128 && it->ctl_arrow_p)
3387 {
3388 /* Set IT->ctl_chars[0] to the glyph for `^'. */
3389 if (it->dp
3390 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
3391 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
3392 g = XINT (DISP_CTRL_GLYPH (it->dp));
3393 else
3394 g = FAST_MAKE_GLYPH ('^', 0);
3395 XSETINT (it->ctl_chars[0], g);
3396
3397 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
3398 XSETINT (it->ctl_chars[1], g);
3399
3400 /* Set up IT->dpvec and return first character from it. */
3401 it->dpvec_char_len = it->len;
3402 it->dpvec = it->ctl_chars;
3403 it->dpend = it->dpvec + 2;
3404 it->current.dpvec_index = 0;
3405 it->method = next_element_from_display_vector;
3406 get_next_display_element (it);
3407 }
3408 else
3409 {
3410 unsigned char str[MAX_MULTIBYTE_LENGTH];
3411 int len = CHAR_STRING (it->c, str);
3412 int i;
3413 GLYPH escape_glyph;
3414
3415 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
3416 if (it->dp
3417 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
3418 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
3419 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
3420 else
3421 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
3422
3423 for (i = 0; i < len; i++)
3424 {
3425 XSETINT (it->ctl_chars[i * 4], escape_glyph);
3426 /* Insert three more glyphs into IT->ctl_chars for
3427 the octal display of the character. */
3428 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
3429 XSETINT (it->ctl_chars[i * 4 + 1], g);
3430 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
3431 XSETINT (it->ctl_chars[i * 4 + 2], g);
3432 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
3433 XSETINT (it->ctl_chars[i * 4 + 3], g);
3434 }
3435
3436 /* Set up IT->dpvec and return the first character
3437 from it. */
3438 it->dpvec_char_len = it->len;
3439 it->dpvec = it->ctl_chars;
3440 it->dpend = it->dpvec + len * 4;
3441 it->current.dpvec_index = 0;
3442 it->method = next_element_from_display_vector;
3443 get_next_display_element (it);
3444 }
3445 }
3446 }
3447
3448 /* Adjust face id for a multibyte character. There are no
3449 multibyte character in unibyte text. */
3450 if (it->multibyte_p
3451 && success_p
3452 && FRAME_WINDOW_P (it->f))
3453 {
3454 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3455 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
3456 }
3457 }
3458
3459 /* Is this character the last one of a run of characters with
3460 box? If yes, set IT->end_of_box_run_p to 1. */
3461 if (it->face_box_p
3462 && it->s == NULL)
3463 {
3464 int face_id;
3465 struct face *face;
3466
3467 it->end_of_box_run_p
3468 = ((face_id = face_after_it_pos (it),
3469 face_id != it->face_id)
3470 && (face = FACE_FROM_ID (it->f, face_id),
3471 face->box == FACE_NO_BOX));
3472 }
3473
3474 /* Value is 0 if end of buffer or string reached. */
3475 return success_p;
3476 }
3477
3478
3479 /* Move IT to the next display element.
3480
3481 Functions get_next_display_element and set_iterator_to_next are
3482 separate because I find this arrangement easier to handle than a
3483 get_next_display_element function that also increments IT's
3484 position. The way it is we can first look at an iterator's current
3485 display element, decide whether it fits on a line, and if it does,
3486 increment the iterator position. The other way around we probably
3487 would either need a flag indicating whether the iterator has to be
3488 incremented the next time, or we would have to implement a
3489 decrement position function which would not be easy to write. */
3490
3491 void
3492 set_iterator_to_next (it)
3493 struct it *it;
3494 {
3495 if (it->method == next_element_from_buffer)
3496 {
3497 /* The current display element of IT is a character from
3498 current_buffer. Advance in the buffer, and maybe skip over
3499 invisible lines that are so because of selective display. */
3500 if (ITERATOR_AT_END_OF_LINE_P (it))
3501 reseat_at_next_visible_line_start (it, 0);
3502 else
3503 {
3504 xassert (it->len != 0);
3505 IT_BYTEPOS (*it) += it->len;
3506 IT_CHARPOS (*it) += 1;
3507 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
3508 }
3509 }
3510 else if (it->method == next_element_from_composition)
3511 {
3512 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
3513 if (STRINGP (it->string))
3514 {
3515 IT_STRING_BYTEPOS (*it) += it->len;
3516 IT_STRING_CHARPOS (*it) += it->cmp_len;
3517 it->method = next_element_from_string;
3518 goto consider_string_end;
3519 }
3520 else
3521 {
3522 IT_BYTEPOS (*it) += it->len;
3523 IT_CHARPOS (*it) += it->cmp_len;
3524 it->method = next_element_from_buffer;
3525 }
3526 }
3527 else if (it->method == next_element_from_c_string)
3528 {
3529 /* Current display element of IT is from a C string. */
3530 IT_BYTEPOS (*it) += it->len;
3531 IT_CHARPOS (*it) += 1;
3532 }
3533 else if (it->method == next_element_from_display_vector)
3534 {
3535 /* Current display element of IT is from a display table entry.
3536 Advance in the display table definition. Reset it to null if
3537 end reached, and continue with characters from buffers/
3538 strings. */
3539 ++it->current.dpvec_index;
3540
3541 /* Restore face of the iterator to what they were before the
3542 display vector entry (these entries may contain faces). */
3543 it->face_id = it->saved_face_id;
3544
3545 if (it->dpvec + it->current.dpvec_index == it->dpend)
3546 {
3547 if (it->s)
3548 it->method = next_element_from_c_string;
3549 else if (STRINGP (it->string))
3550 it->method = next_element_from_string;
3551 else
3552 it->method = next_element_from_buffer;
3553
3554 it->dpvec = NULL;
3555 it->current.dpvec_index = -1;
3556
3557 /* Skip over characters which were displayed via IT->dpvec. */
3558 if (it->dpvec_char_len < 0)
3559 reseat_at_next_visible_line_start (it, 1);
3560 else if (it->dpvec_char_len > 0)
3561 {
3562 it->len = it->dpvec_char_len;
3563 set_iterator_to_next (it);
3564 }
3565 }
3566 }
3567 else if (it->method == next_element_from_string)
3568 {
3569 /* Current display element is a character from a Lisp string. */
3570 xassert (it->s == NULL && STRINGP (it->string));
3571 IT_STRING_BYTEPOS (*it) += it->len;
3572 IT_STRING_CHARPOS (*it) += 1;
3573
3574 consider_string_end:
3575
3576 if (it->current.overlay_string_index >= 0)
3577 {
3578 /* IT->string is an overlay string. Advance to the
3579 next, if there is one. */
3580 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
3581 next_overlay_string (it);
3582 }
3583 else
3584 {
3585 /* IT->string is not an overlay string. If we reached
3586 its end, and there is something on IT->stack, proceed
3587 with what is on the stack. This can be either another
3588 string, this time an overlay string, or a buffer. */
3589 if (IT_STRING_CHARPOS (*it) == XSTRING (it->string)->size
3590 && it->sp > 0)
3591 {
3592 pop_it (it);
3593 if (!STRINGP (it->string))
3594 it->method = next_element_from_buffer;
3595 }
3596 }
3597 }
3598 else if (it->method == next_element_from_image
3599 || it->method == next_element_from_stretch)
3600 {
3601 /* The position etc with which we have to proceed are on
3602 the stack. The position may be at the end of a string,
3603 if the `display' property takes up the whole string. */
3604 pop_it (it);
3605 it->image_id = 0;
3606 if (STRINGP (it->string))
3607 {
3608 it->method = next_element_from_string;
3609 goto consider_string_end;
3610 }
3611 else
3612 it->method = next_element_from_buffer;
3613 }
3614 else
3615 /* There are no other methods defined, so this should be a bug. */
3616 abort ();
3617
3618 /* Reset flags indicating start and end of a sequence of
3619 characters with box. */
3620 it->start_of_box_run_p = it->end_of_box_run_p = 0;
3621
3622 xassert (it->method != next_element_from_string
3623 || (STRINGP (it->string)
3624 && IT_STRING_CHARPOS (*it) >= 0));
3625 }
3626
3627
3628 /* Load IT's display element fields with information about the next
3629 display element which comes from a display table entry or from the
3630 result of translating a control character to one of the forms `^C'
3631 or `\003'. IT->dpvec holds the glyphs to return as characters. */
3632
3633 static int
3634 next_element_from_display_vector (it)
3635 struct it *it;
3636 {
3637 /* Precondition. */
3638 xassert (it->dpvec && it->current.dpvec_index >= 0);
3639
3640 /* Remember the current face id in case glyphs specify faces.
3641 IT's face is restored in set_iterator_to_next. */
3642 it->saved_face_id = it->face_id;
3643
3644 if (INTEGERP (*it->dpvec)
3645 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
3646 {
3647 int lface_id;
3648 GLYPH g;
3649
3650 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
3651 it->c = FAST_GLYPH_CHAR (g);
3652 it->len = CHAR_LEN (it->c);
3653
3654 /* The entry may contain a face id to use. Such a face id is
3655 the id of a Lisp face, not a realized face. A face id of
3656 zero means no face. */
3657 lface_id = FAST_GLYPH_FACE (g);
3658 if (lface_id)
3659 {
3660 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
3661 if (face_id >= 0)
3662 {
3663 it->face_id = face_id;
3664 }
3665 }
3666 }
3667 else
3668 /* Display table entry is invalid. Return a space. */
3669 it->c = ' ', it->len = 1;
3670
3671 /* Don't change position and object of the iterator here. They are
3672 still the values of the character that had this display table
3673 entry or was translated, and that's what we want. */
3674 it->what = IT_CHARACTER;
3675 return 1;
3676 }
3677
3678
3679 /* Load IT with the next display element from Lisp string IT->string.
3680 IT->current.string_pos is the current position within the string.
3681 If IT->current.overlay_string_index >= 0, the Lisp string is an
3682 overlay string. */
3683
3684 static int
3685 next_element_from_string (it)
3686 struct it *it;
3687 {
3688 struct text_pos position;
3689
3690 xassert (STRINGP (it->string));
3691 xassert (IT_STRING_CHARPOS (*it) >= 0);
3692 position = it->current.string_pos;
3693
3694 /* Time to check for invisible text? */
3695 if (IT_STRING_CHARPOS (*it) < it->end_charpos
3696 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
3697 {
3698 handle_stop (it);
3699
3700 /* Since a handler may have changed IT->method, we must
3701 recurse here. */
3702 return get_next_display_element (it);
3703 }
3704
3705 if (it->current.overlay_string_index >= 0)
3706 {
3707 /* Get the next character from an overlay string. In overlay
3708 strings, There is no field width or padding with spaces to
3709 do. */
3710 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
3711 {
3712 it->what = IT_EOB;
3713 return 0;
3714 }
3715 else if (STRING_MULTIBYTE (it->string))
3716 {
3717 int remaining = (STRING_BYTES (XSTRING (it->string))
3718 - IT_STRING_BYTEPOS (*it));
3719 unsigned char *s = (XSTRING (it->string)->data
3720 + IT_STRING_BYTEPOS (*it));
3721 it->c = string_char_and_length (s, remaining, &it->len);
3722 }
3723 else
3724 {
3725 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
3726 it->len = 1;
3727 }
3728 }
3729 else
3730 {
3731 /* Get the next character from a Lisp string that is not an
3732 overlay string. Such strings come from the mode line, for
3733 example. We may have to pad with spaces, or truncate the
3734 string. See also next_element_from_c_string. */
3735 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
3736 {
3737 it->what = IT_EOB;
3738 return 0;
3739 }
3740 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
3741 {
3742 /* Pad with spaces. */
3743 it->c = ' ', it->len = 1;
3744 CHARPOS (position) = BYTEPOS (position) = -1;
3745 }
3746 else if (STRING_MULTIBYTE (it->string))
3747 {
3748 int maxlen = (STRING_BYTES (XSTRING (it->string))
3749 - IT_STRING_BYTEPOS (*it));
3750 unsigned char *s = (XSTRING (it->string)->data
3751 + IT_STRING_BYTEPOS (*it));
3752 it->c = string_char_and_length (s, maxlen, &it->len);
3753 }
3754 else
3755 {
3756 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
3757 it->len = 1;
3758 }
3759 }
3760
3761 /* Record what we have and where it came from. Note that we store a
3762 buffer position in IT->position although it could arguably be a
3763 string position. */
3764 it->what = IT_CHARACTER;
3765 it->object = it->string;
3766 it->position = position;
3767 return 1;
3768 }
3769
3770
3771 /* Load IT with next display element from C string IT->s.
3772 IT->string_nchars is the maximum number of characters to return
3773 from the string. IT->end_charpos may be greater than
3774 IT->string_nchars when this function is called, in which case we
3775 may have to return padding spaces. Value is zero if end of string
3776 reached, including padding spaces. */
3777
3778 static int
3779 next_element_from_c_string (it)
3780 struct it *it;
3781 {
3782 int success_p = 1;
3783
3784 xassert (it->s);
3785 it->what = IT_CHARACTER;
3786 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
3787 it->object = Qnil;
3788
3789 /* IT's position can be greater IT->string_nchars in case a field
3790 width or precision has been specified when the iterator was
3791 initialized. */
3792 if (IT_CHARPOS (*it) >= it->end_charpos)
3793 {
3794 /* End of the game. */
3795 it->what = IT_EOB;
3796 success_p = 0;
3797 }
3798 else if (IT_CHARPOS (*it) >= it->string_nchars)
3799 {
3800 /* Pad with spaces. */
3801 it->c = ' ', it->len = 1;
3802 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
3803 }
3804 else if (it->multibyte_p)
3805 {
3806 /* Implementation note: The calls to strlen apparently aren't a
3807 performance problem because there is no noticeable performance
3808 difference between Emacs running in unibyte or multibyte mode. */
3809 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
3810 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
3811 maxlen, &it->len);
3812 }
3813 else
3814 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
3815
3816 return success_p;
3817 }
3818
3819
3820 /* Set up IT to return characters from an ellipsis, if appropriate.
3821 The definition of the ellipsis glyphs may come from a display table
3822 entry. This function Fills IT with the first glyph from the
3823 ellipsis if an ellipsis is to be displayed. */
3824
3825 static int
3826 next_element_from_ellipsis (it)
3827 struct it *it;
3828 {
3829 if (it->selective_display_ellipsis_p)
3830 {
3831 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3832 {
3833 /* Use the display table definition for `...'. Invalid glyphs
3834 will be handled by the method returning elements from dpvec. */
3835 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3836 it->dpvec_char_len = it->len;
3837 it->dpvec = v->contents;
3838 it->dpend = v->contents + v->size;
3839 it->current.dpvec_index = 0;
3840 it->method = next_element_from_display_vector;
3841 }
3842 else
3843 {
3844 /* Use default `...' which is stored in default_invis_vector. */
3845 it->dpvec_char_len = it->len;
3846 it->dpvec = default_invis_vector;
3847 it->dpend = default_invis_vector + 3;
3848 it->current.dpvec_index = 0;
3849 it->method = next_element_from_display_vector;
3850 }
3851 }
3852 else
3853 reseat_at_next_visible_line_start (it, 1);
3854
3855 return get_next_display_element (it);
3856 }
3857
3858
3859 /* Deliver an image display element. The iterator IT is already
3860 filled with image information (done in handle_display_prop). Value
3861 is always 1. */
3862
3863
3864 static int
3865 next_element_from_image (it)
3866 struct it *it;
3867 {
3868 it->what = IT_IMAGE;
3869 return 1;
3870 }
3871
3872
3873 /* Fill iterator IT with next display element from a stretch glyph
3874 property. IT->object is the value of the text property. Value is
3875 always 1. */
3876
3877 static int
3878 next_element_from_stretch (it)
3879 struct it *it;
3880 {
3881 it->what = IT_STRETCH;
3882 return 1;
3883 }
3884
3885
3886 /* Load IT with the next display element from current_buffer. Value
3887 is zero if end of buffer reached. IT->stop_charpos is the next
3888 position at which to stop and check for text properties or buffer
3889 end. */
3890
3891 static int
3892 next_element_from_buffer (it)
3893 struct it *it;
3894 {
3895 int success_p = 1;
3896
3897 /* Check this assumption, otherwise, we would never enter the
3898 if-statement, below. */
3899 xassert (IT_CHARPOS (*it) >= BEGV
3900 && IT_CHARPOS (*it) <= it->stop_charpos);
3901
3902 if (IT_CHARPOS (*it) >= it->stop_charpos)
3903 {
3904 if (IT_CHARPOS (*it) >= it->end_charpos)
3905 {
3906 int overlay_strings_follow_p;
3907
3908 /* End of the game, except when overlay strings follow that
3909 haven't been returned yet. */
3910 if (it->overlay_strings_at_end_processed_p)
3911 overlay_strings_follow_p = 0;
3912 else
3913 {
3914 it->overlay_strings_at_end_processed_p = 1;
3915 overlay_strings_follow_p = get_overlay_strings (it);
3916 }
3917
3918 if (overlay_strings_follow_p)
3919 success_p = get_next_display_element (it);
3920 else
3921 {
3922 it->what = IT_EOB;
3923 it->position = it->current.pos;
3924 success_p = 0;
3925 }
3926 }
3927 else
3928 {
3929 handle_stop (it);
3930 return get_next_display_element (it);
3931 }
3932 }
3933 else
3934 {
3935 /* No face changes, overlays etc. in sight, so just return a
3936 character from current_buffer. */
3937 unsigned char *p;
3938
3939 /* Maybe run the redisplay end trigger hook. Performance note:
3940 This doesn't seem to cost measurable time. */
3941 if (it->redisplay_end_trigger_charpos
3942 && it->glyph_row
3943 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
3944 run_redisplay_end_trigger_hook (it);
3945
3946 /* Get the next character, maybe multibyte. */
3947 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
3948 if (it->multibyte_p && !ASCII_BYTE_P (*p))
3949 {
3950 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
3951 - IT_BYTEPOS (*it));
3952 it->c = string_char_and_length (p, maxlen, &it->len);
3953 }
3954 else
3955 it->c = *p, it->len = 1;
3956
3957 /* Record what we have and where it came from. */
3958 it->what = IT_CHARACTER;;
3959 it->object = it->w->buffer;
3960 it->position = it->current.pos;
3961
3962 /* Normally we return the character found above, except when we
3963 really want to return an ellipsis for selective display. */
3964 if (it->selective)
3965 {
3966 if (it->c == '\n')
3967 {
3968 /* A value of selective > 0 means hide lines indented more
3969 than that number of columns. */
3970 if (it->selective > 0
3971 && IT_CHARPOS (*it) + 1 < ZV
3972 && indented_beyond_p (IT_CHARPOS (*it) + 1,
3973 IT_BYTEPOS (*it) + 1,
3974 it->selective))
3975 {
3976 success_p = next_element_from_ellipsis (it);
3977 it->dpvec_char_len = -1;
3978 }
3979 }
3980 else if (it->c == '\r' && it->selective == -1)
3981 {
3982 /* A value of selective == -1 means that everything from the
3983 CR to the end of the line is invisible, with maybe an
3984 ellipsis displayed for it. */
3985 success_p = next_element_from_ellipsis (it);
3986 it->dpvec_char_len = -1;
3987 }
3988 }
3989 }
3990
3991 /* Value is zero if end of buffer reached. */
3992 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
3993 return success_p;
3994 }
3995
3996
3997 /* Run the redisplay end trigger hook for IT. */
3998
3999 static void
4000 run_redisplay_end_trigger_hook (it)
4001 struct it *it;
4002 {
4003 Lisp_Object args[3];
4004
4005 /* IT->glyph_row should be non-null, i.e. we should be actually
4006 displaying something, or otherwise we should not run the hook. */
4007 xassert (it->glyph_row);
4008
4009 /* Set up hook arguments. */
4010 args[0] = Qredisplay_end_trigger_functions;
4011 args[1] = it->window;
4012 XSETINT (args[2], it->redisplay_end_trigger_charpos);
4013 it->redisplay_end_trigger_charpos = 0;
4014
4015 /* Since we are *trying* to run these functions, don't try to run
4016 them again, even if they get an error. */
4017 it->w->redisplay_end_trigger = Qnil;
4018 Frun_hook_with_args (3, args);
4019
4020 /* Notice if it changed the face of the character we are on. */
4021 handle_face_prop (it);
4022 }
4023
4024
4025 /* Deliver a composition display element. The iterator IT is already
4026 filled with composition information (done in
4027 handle_composition_prop). Value is always 1. */
4028
4029 static int
4030 next_element_from_composition (it)
4031 struct it *it;
4032 {
4033 it->what = IT_COMPOSITION;
4034 it->position = (STRINGP (it->string)
4035 ? it->current.string_pos
4036 : it->current.pos);
4037 return 1;
4038 }
4039
4040
4041 \f
4042 /***********************************************************************
4043 Moving an iterator without producing glyphs
4044 ***********************************************************************/
4045
4046 /* Move iterator IT to a specified buffer or X position within one
4047 line on the display without producing glyphs.
4048
4049 Begin to skip at IT's current position. Skip to TO_CHARPOS or TO_X
4050 whichever is reached first.
4051
4052 TO_CHARPOS <= 0 means no TO_CHARPOS is specified.
4053
4054 TO_X < 0 means that no TO_X is specified. TO_X is normally a value
4055 0 <= TO_X <= IT->last_visible_x. This means in particular, that
4056 TO_X includes the amount by which a window is horizontally
4057 scrolled.
4058
4059 Value is
4060
4061 MOVE_POS_MATCH_OR_ZV
4062 - when TO_POS or ZV was reached.
4063
4064 MOVE_X_REACHED
4065 -when TO_X was reached before TO_POS or ZV were reached.
4066
4067 MOVE_LINE_CONTINUED
4068 - when we reached the end of the display area and the line must
4069 be continued.
4070
4071 MOVE_LINE_TRUNCATED
4072 - when we reached the end of the display area and the line is
4073 truncated.
4074
4075 MOVE_NEWLINE_OR_CR
4076 - when we stopped at a line end, i.e. a newline or a CR and selective
4077 display is on. */
4078
4079 static enum move_it_result
4080 move_it_in_display_line_to (it, to_charpos, to_x, op)
4081 struct it *it;
4082 int to_charpos, to_x, op;
4083 {
4084 enum move_it_result result = MOVE_UNDEFINED;
4085 struct glyph_row *saved_glyph_row;
4086
4087 /* Don't produce glyphs in produce_glyphs. */
4088 saved_glyph_row = it->glyph_row;
4089 it->glyph_row = NULL;
4090
4091 while (1)
4092 {
4093 int x, i;
4094
4095 /* Stop when ZV or TO_CHARPOS reached. */
4096 if (!get_next_display_element (it)
4097 || ((op & MOVE_TO_POS) != 0
4098 && BUFFERP (it->object)
4099 && IT_CHARPOS (*it) >= to_charpos))
4100 {
4101 result = MOVE_POS_MATCH_OR_ZV;
4102 break;
4103 }
4104
4105 /* The call to produce_glyphs will get the metrics of the
4106 display element IT is loaded with. We record in x the
4107 x-position before this display element in case it does not
4108 fit on the line. */
4109 x = it->current_x;
4110 PRODUCE_GLYPHS (it);
4111
4112 if (it->area != TEXT_AREA)
4113 {
4114 set_iterator_to_next (it);
4115 continue;
4116 }
4117
4118 /* The number of glyphs we get back in IT->nglyphs will normally
4119 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
4120 character on a terminal frame, or (iii) a line end. For the
4121 second case, IT->nglyphs - 1 padding glyphs will be present
4122 (on X frames, there is only one glyph produced for a
4123 composite character.
4124
4125 The behavior implemented below means, for continuation lines,
4126 that as many spaces of a TAB as fit on the current line are
4127 displayed there. For terminal frames, as many glyphs of a
4128 multi-glyph character are displayed in the current line, too.
4129 This is what the old redisplay code did, and we keep it that
4130 way. Under X, the whole shape of a complex character must
4131 fit on the line or it will be completely displayed in the
4132 next line.
4133
4134 Note that both for tabs and padding glyphs, all glyphs have
4135 the same width. */
4136 if (it->nglyphs)
4137 {
4138 /* More than one glyph or glyph doesn't fit on line. All
4139 glyphs have the same width. */
4140 int single_glyph_width = it->pixel_width / it->nglyphs;
4141 int new_x;
4142
4143 for (i = 0; i < it->nglyphs; ++i, x = new_x)
4144 {
4145 new_x = x + single_glyph_width;
4146
4147 /* We want to leave anything reaching TO_X to the caller. */
4148 if ((op & MOVE_TO_X) && new_x > to_x)
4149 {
4150 it->current_x = x;
4151 result = MOVE_X_REACHED;
4152 break;
4153 }
4154 else if (/* Lines are continued. */
4155 !it->truncate_lines_p
4156 && (/* And glyph doesn't fit on the line. */
4157 new_x > it->last_visible_x
4158 /* Or it fits exactly and we're on a window
4159 system frame. */
4160 || (new_x == it->last_visible_x
4161 && FRAME_WINDOW_P (it->f))))
4162 {
4163 if (/* IT->hpos == 0 means the very first glyph
4164 doesn't fit on the line, e.g. a wide image. */
4165 it->hpos == 0
4166 || (new_x == it->last_visible_x
4167 && FRAME_WINDOW_P (it->f)))
4168 {
4169 ++it->hpos;
4170 it->current_x = new_x;
4171 if (i == it->nglyphs - 1)
4172 set_iterator_to_next (it);
4173 }
4174 else
4175 it->current_x = x;
4176
4177 result = MOVE_LINE_CONTINUED;
4178 break;
4179 }
4180 else if (new_x > it->first_visible_x)
4181 {
4182 /* Glyph is visible. Increment number of glyphs that
4183 would be displayed. */
4184 ++it->hpos;
4185 }
4186 else
4187 {
4188 /* Glyph is completely off the left margin of the display
4189 area. Nothing to do. */
4190 }
4191 }
4192
4193 if (result != MOVE_UNDEFINED)
4194 break;
4195 }
4196 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
4197 {
4198 /* Stop when TO_X specified and reached. This check is
4199 necessary here because of lines consisting of a line end,
4200 only. The line end will not produce any glyphs and we
4201 would never get MOVE_X_REACHED. */
4202 xassert (it->nglyphs == 0);
4203 result = MOVE_X_REACHED;
4204 break;
4205 }
4206
4207 /* Is this a line end? If yes, we're done. */
4208 if (ITERATOR_AT_END_OF_LINE_P (it))
4209 {
4210 result = MOVE_NEWLINE_OR_CR;
4211 break;
4212 }
4213
4214 /* The current display element has been consumed. Advance
4215 to the next. */
4216 set_iterator_to_next (it);
4217
4218 /* Stop if lines are truncated and IT's current x-position is
4219 past the right edge of the window now. */
4220 if (it->truncate_lines_p
4221 && it->current_x >= it->last_visible_x)
4222 {
4223 result = MOVE_LINE_TRUNCATED;
4224 break;
4225 }
4226 }
4227
4228 /* Restore the iterator settings altered at the beginning of this
4229 function. */
4230 it->glyph_row = saved_glyph_row;
4231 return result;
4232 }
4233
4234
4235 /* Move IT forward to a specified buffer position TO_CHARPOS, TO_X,
4236 TO_Y, TO_VPOS. OP is a bit-mask that specifies where to stop. See
4237 the description of enum move_operation_enum.
4238
4239 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
4240 screen line, this function will set IT to the next position >
4241 TO_CHARPOS. */
4242
4243 void
4244 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
4245 struct it *it;
4246 int to_charpos, to_x, to_y, to_vpos;
4247 int op;
4248 {
4249 enum move_it_result skip, skip2 = MOVE_X_REACHED;
4250 int line_height;
4251
4252 while (1)
4253 {
4254 if (op & MOVE_TO_VPOS)
4255 {
4256 /* If no TO_CHARPOS and no TO_X specified, stop at the
4257 start of the line TO_VPOS. */
4258 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
4259 {
4260 if (it->vpos == to_vpos)
4261 break;
4262 skip = move_it_in_display_line_to (it, -1, -1, 0);
4263 }
4264 else
4265 {
4266 /* TO_VPOS >= 0 means stop at TO_X in the line at
4267 TO_VPOS, or at TO_POS, whichever comes first. */
4268 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
4269
4270 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
4271 break;
4272 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
4273 {
4274 /* We have reached TO_X but not in the line we want. */
4275 skip = move_it_in_display_line_to (it, to_charpos,
4276 -1, MOVE_TO_POS);
4277 if (skip == MOVE_POS_MATCH_OR_ZV)
4278 break;
4279 }
4280 }
4281 }
4282 else if (op & MOVE_TO_Y)
4283 {
4284 struct it it_backup;
4285 int done_p;
4286
4287 /* TO_Y specified means stop at TO_X in the line containing
4288 TO_Y---or at TO_CHARPOS if this is reached first. The
4289 problem is that we can't really tell whether the line
4290 contains TO_Y before we have completely scanned it, and
4291 this may skip past TO_X. What we do is to first scan to
4292 TO_X.
4293
4294 If TO_X is not specified, use a TO_X of zero. The reason
4295 is to make the outcome of this function more predictable.
4296 If we didn't use TO_X == 0, we would stop at the end of
4297 the line which is probably not what a caller would expect
4298 to happen. */
4299 skip = move_it_in_display_line_to (it, to_charpos,
4300 ((op & MOVE_TO_X)
4301 ? to_x : 0),
4302 (MOVE_TO_X
4303 | (op & MOVE_TO_POS)));
4304
4305 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
4306 if (skip == MOVE_POS_MATCH_OR_ZV)
4307 break;
4308
4309 /* If TO_X was reached, we would like to know whether TO_Y
4310 is in the line. This can only be said if we know the
4311 total line height which requires us to scan the rest of
4312 the line. */
4313 done_p = 0;
4314 if (skip == MOVE_X_REACHED)
4315 {
4316 it_backup = *it;
4317 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
4318 op & MOVE_TO_POS);
4319 }
4320
4321 /* Now, decide whether TO_Y is in this line. */
4322 line_height = it->max_ascent + it->max_descent;
4323
4324 if (to_y >= it->current_y
4325 && to_y < it->current_y + line_height)
4326 {
4327 if (skip == MOVE_X_REACHED)
4328 /* If TO_Y is in this line and TO_X was reached above,
4329 we scanned too far. We have to restore IT's settings
4330 to the ones before skipping. */
4331 *it = it_backup;
4332 done_p = 1;
4333 }
4334 else if (skip == MOVE_X_REACHED)
4335 {
4336 skip = skip2;
4337 if (skip == MOVE_POS_MATCH_OR_ZV)
4338 done_p = 1;
4339 }
4340
4341 if (done_p)
4342 break;
4343 }
4344 else
4345 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
4346
4347 switch (skip)
4348 {
4349 case MOVE_POS_MATCH_OR_ZV:
4350 return;
4351
4352 case MOVE_NEWLINE_OR_CR:
4353 set_iterator_to_next (it);
4354 it->continuation_lines_width = 0;
4355 break;
4356
4357 case MOVE_LINE_TRUNCATED:
4358 it->continuation_lines_width = 0;
4359 reseat_at_next_visible_line_start (it, 0);
4360 if ((op & MOVE_TO_POS) != 0
4361 && IT_CHARPOS (*it) > to_charpos)
4362 goto out;
4363 break;
4364
4365 case MOVE_LINE_CONTINUED:
4366 it->continuation_lines_width += it->current_x;
4367 break;
4368
4369 default:
4370 abort ();
4371 }
4372
4373 /* Reset/increment for the next run. */
4374 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
4375 it->current_x = it->hpos = 0;
4376 it->current_y += it->max_ascent + it->max_descent;
4377 ++it->vpos;
4378 last_height = it->max_ascent + it->max_descent;
4379 last_max_ascent = it->max_ascent;
4380 it->max_ascent = it->max_descent = 0;
4381 }
4382 out:;
4383 }
4384
4385
4386 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
4387
4388 If DY > 0, move IT backward at least that many pixels. DY = 0
4389 means move IT backward to the preceding line start or BEGV. This
4390 function may move over more than DY pixels if IT->current_y - DY
4391 ends up in the middle of a line; in this case IT->current_y will be
4392 set to the top of the line moved to. */
4393
4394 void
4395 move_it_vertically_backward (it, dy)
4396 struct it *it;
4397 int dy;
4398 {
4399 int nlines, h, line_height;
4400 struct it it2;
4401 int start_pos = IT_CHARPOS (*it);
4402
4403 xassert (dy >= 0);
4404
4405 /* Estimate how many newlines we must move back. */
4406 nlines = max (1, dy / CANON_Y_UNIT (it->f));
4407
4408 /* Set the iterator's position that many lines back. */
4409 while (nlines-- && IT_CHARPOS (*it) > BEGV)
4410 back_to_previous_visible_line_start (it);
4411
4412 /* Reseat the iterator here. When moving backward, we don't want
4413 reseat to skip forward over invisible text, set up the iterator
4414 to deliver from overlay strings at the new position etc. So,
4415 use reseat_1 here. */
4416 reseat_1 (it, it->current.pos, 1);
4417
4418 /* We are now surely at a line start. */
4419 it->current_x = it->hpos = 0;
4420
4421 /* Move forward and see what y-distance we moved. First move to the
4422 start of the next line so that we get its height. We need this
4423 height to be able to tell whether we reached the specified
4424 y-distance. */
4425 it2 = *it;
4426 it2.max_ascent = it2.max_descent = 0;
4427 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
4428 MOVE_TO_POS | MOVE_TO_VPOS);
4429 xassert (IT_CHARPOS (*it) >= BEGV);
4430 line_height = it2.max_ascent + it2.max_descent;
4431 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
4432 xassert (IT_CHARPOS (*it) >= BEGV);
4433 h = it2.current_y - it->current_y;
4434 nlines = it2.vpos - it->vpos;
4435
4436 /* Correct IT's y and vpos position. */
4437 it->vpos -= nlines;
4438 it->current_y -= h;
4439
4440 if (dy == 0)
4441 {
4442 /* DY == 0 means move to the start of the screen line. The
4443 value of nlines is > 0 if continuation lines were involved. */
4444 if (nlines > 0)
4445 move_it_by_lines (it, nlines, 1);
4446 xassert (IT_CHARPOS (*it) <= start_pos);
4447 }
4448 else if (nlines)
4449 {
4450 /* The y-position we try to reach. Note that h has been
4451 subtracted in front of the if-statement. */
4452 int target_y = it->current_y + h - dy;
4453
4454 /* If we did not reach target_y, try to move further backward if
4455 we can. If we moved too far backward, try to move forward. */
4456 if (target_y < it->current_y
4457 && IT_CHARPOS (*it) > BEGV)
4458 {
4459 move_it_vertically (it, target_y - it->current_y);
4460 xassert (IT_CHARPOS (*it) >= BEGV);
4461 }
4462 else if (target_y >= it->current_y + line_height
4463 && IT_CHARPOS (*it) < ZV)
4464 {
4465 move_it_vertically (it, target_y - (it->current_y + line_height));
4466 xassert (IT_CHARPOS (*it) >= BEGV);
4467 }
4468 }
4469 }
4470
4471
4472 /* Move IT by a specified amount of pixel lines DY. DY negative means
4473 move backwards. DY = 0 means move to start of screen line. At the
4474 end, IT will be on the start of a screen line. */
4475
4476 void
4477 move_it_vertically (it, dy)
4478 struct it *it;
4479 int dy;
4480 {
4481 if (dy <= 0)
4482 move_it_vertically_backward (it, -dy);
4483 else if (dy > 0)
4484 {
4485 move_it_to (it, ZV, -1, it->current_y + dy, -1,
4486 MOVE_TO_POS | MOVE_TO_Y);
4487
4488 /* If buffer ends in ZV without a newline, move to the start of
4489 the line to satisfy the post-condition. */
4490 if (IT_CHARPOS (*it) == ZV
4491 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
4492 move_it_by_lines (it, 0, 0);
4493 }
4494 }
4495
4496
4497 /* Return non-zero if some text between buffer positions START_CHARPOS
4498 and END_CHARPOS is invisible. IT->window is the window for text
4499 property lookup. */
4500
4501 static int
4502 invisible_text_between_p (it, start_charpos, end_charpos)
4503 struct it *it;
4504 int start_charpos, end_charpos;
4505 {
4506 Lisp_Object prop, limit;
4507 int invisible_found_p;
4508
4509 xassert (it != NULL && start_charpos <= end_charpos);
4510
4511 /* Is text at START invisible? */
4512 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
4513 it->window);
4514 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4515 invisible_found_p = 1;
4516 else
4517 {
4518 limit = next_single_char_property_change (make_number (start_charpos),
4519 Qinvisible, Qnil,
4520 make_number (end_charpos));
4521 invisible_found_p = XFASTINT (limit) < end_charpos;
4522 }
4523
4524 return invisible_found_p;
4525 }
4526
4527
4528 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
4529 negative means move up. DVPOS == 0 means move to the start of the
4530 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
4531 NEED_Y_P is zero, IT->current_y will be left unchanged.
4532
4533 Further optimization ideas: If we would know that IT->f doesn't use
4534 a face with proportional font, we could be faster for
4535 truncate-lines nil. */
4536
4537 void
4538 move_it_by_lines (it, dvpos, need_y_p)
4539 struct it *it;
4540 int dvpos, need_y_p;
4541 {
4542 struct position pos;
4543
4544 if (!FRAME_WINDOW_P (it->f))
4545 {
4546 struct text_pos textpos;
4547
4548 /* We can use vmotion on frames without proportional fonts. */
4549 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
4550 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
4551 reseat (it, textpos, 1);
4552 it->vpos += pos.vpos;
4553 it->current_y += pos.vpos;
4554 }
4555 else if (dvpos == 0)
4556 {
4557 /* DVPOS == 0 means move to the start of the screen line. */
4558 move_it_vertically_backward (it, 0);
4559 xassert (it->current_x == 0 && it->hpos == 0);
4560 }
4561 else if (dvpos > 0)
4562 {
4563 /* If there are no continuation lines, and if there is no
4564 selective display, try the simple method of moving forward
4565 DVPOS newlines, then see where we are. */
4566 if (!need_y_p && it->truncate_lines_p && it->selective == 0)
4567 {
4568 int shortage = 0, charpos;
4569
4570 if (FETCH_BYTE (IT_BYTEPOS (*it) == '\n'))
4571 charpos = IT_CHARPOS (*it) + 1;
4572 else
4573 charpos = scan_buffer ('\n', IT_CHARPOS (*it), 0, dvpos,
4574 &shortage, 0);
4575
4576 if (!invisible_text_between_p (it, IT_CHARPOS (*it), charpos))
4577 {
4578 struct text_pos pos;
4579 CHARPOS (pos) = charpos;
4580 BYTEPOS (pos) = CHAR_TO_BYTE (charpos);
4581 reseat (it, pos, 1);
4582 it->vpos += dvpos - shortage;
4583 it->hpos = it->current_x = 0;
4584 return;
4585 }
4586 }
4587
4588 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
4589 }
4590 else
4591 {
4592 struct it it2;
4593 int start_charpos, i;
4594
4595 /* If there are no continuation lines, and if there is no
4596 selective display, try the simple method of moving backward
4597 -DVPOS newlines. */
4598 if (!need_y_p && it->truncate_lines_p && it->selective == 0)
4599 {
4600 int shortage;
4601 int charpos = IT_CHARPOS (*it);
4602 int bytepos = IT_BYTEPOS (*it);
4603
4604 /* If in the middle of a line, go to its start. */
4605 if (charpos > BEGV && FETCH_BYTE (bytepos - 1) != '\n')
4606 {
4607 charpos = find_next_newline_no_quit (charpos, -1);
4608 bytepos = CHAR_TO_BYTE (charpos);
4609 }
4610
4611 if (charpos == BEGV)
4612 {
4613 struct text_pos pos;
4614 CHARPOS (pos) = charpos;
4615 BYTEPOS (pos) = bytepos;
4616 reseat (it, pos, 1);
4617 it->hpos = it->current_x = 0;
4618 return;
4619 }
4620 else
4621 {
4622 charpos = scan_buffer ('\n', charpos - 1, 0, dvpos, &shortage, 0);
4623 if (!invisible_text_between_p (it, charpos, IT_CHARPOS (*it)))
4624 {
4625 struct text_pos pos;
4626 CHARPOS (pos) = charpos;
4627 BYTEPOS (pos) = CHAR_TO_BYTE (charpos);
4628 reseat (it, pos, 1);
4629 it->vpos += dvpos + (shortage ? shortage - 1 : 0);
4630 it->hpos = it->current_x = 0;
4631 return;
4632 }
4633 }
4634 }
4635
4636 /* Go back -DVPOS visible lines and reseat the iterator there. */
4637 start_charpos = IT_CHARPOS (*it);
4638 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
4639 back_to_previous_visible_line_start (it);
4640 reseat (it, it->current.pos, 1);
4641 it->current_x = it->hpos = 0;
4642
4643 /* Above call may have moved too far if continuation lines
4644 are involved. Scan forward and see if it did. */
4645 it2 = *it;
4646 it2.vpos = it2.current_y = 0;
4647 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
4648 it->vpos -= it2.vpos;
4649 it->current_y -= it2.current_y;
4650 it->current_x = it->hpos = 0;
4651
4652 /* If we moved too far, move IT some lines forward. */
4653 if (it2.vpos > -dvpos)
4654 {
4655 int delta = it2.vpos + dvpos;
4656 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
4657 }
4658 }
4659 }
4660
4661
4662 \f
4663 /***********************************************************************
4664 Messages
4665 ***********************************************************************/
4666
4667
4668 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
4669 to *Messages*. */
4670
4671 void
4672 add_to_log (format, arg1, arg2)
4673 char *format;
4674 Lisp_Object arg1, arg2;
4675 {
4676 Lisp_Object args[3];
4677 Lisp_Object msg, fmt;
4678 char *buffer;
4679 int len;
4680 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4681
4682 fmt = msg = Qnil;
4683 GCPRO4 (fmt, msg, arg1, arg2);
4684
4685 args[0] = fmt = build_string (format);
4686 args[1] = arg1;
4687 args[2] = arg2;
4688 msg = Fformat (3, args);
4689
4690 len = STRING_BYTES (XSTRING (msg)) + 1;
4691 buffer = (char *) alloca (len);
4692 strcpy (buffer, XSTRING (msg)->data);
4693
4694 message_dolog (buffer, len, 1, 0);
4695 UNGCPRO;
4696 }
4697
4698
4699 /* Output a newline in the *Messages* buffer if "needs" one. */
4700
4701 void
4702 message_log_maybe_newline ()
4703 {
4704 if (message_log_need_newline)
4705 message_dolog ("", 0, 1, 0);
4706 }
4707
4708
4709 /* Add a string M of length LEN to the message log, optionally
4710 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
4711 nonzero, means interpret the contents of M as multibyte. This
4712 function calls low-level routines in order to bypass text property
4713 hooks, etc. which might not be safe to run. */
4714
4715 void
4716 message_dolog (m, len, nlflag, multibyte)
4717 char *m;
4718 int len, nlflag, multibyte;
4719 {
4720 if (!NILP (Vmessage_log_max))
4721 {
4722 struct buffer *oldbuf;
4723 Lisp_Object oldpoint, oldbegv, oldzv;
4724 int old_windows_or_buffers_changed = windows_or_buffers_changed;
4725 int point_at_end = 0;
4726 int zv_at_end = 0;
4727 Lisp_Object old_deactivate_mark, tem;
4728 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4729
4730 old_deactivate_mark = Vdeactivate_mark;
4731 oldbuf = current_buffer;
4732 Fset_buffer (Fget_buffer_create (build_string ("*Messages*")));
4733 current_buffer->undo_list = Qt;
4734
4735 oldpoint = Fpoint_marker ();
4736 oldbegv = Fpoint_min_marker ();
4737 oldzv = Fpoint_max_marker ();
4738 GCPRO4 (oldpoint, oldbegv, oldzv, old_deactivate_mark);
4739
4740 if (PT == Z)
4741 point_at_end = 1;
4742 if (ZV == Z)
4743 zv_at_end = 1;
4744
4745 BEGV = BEG;
4746 BEGV_BYTE = BEG_BYTE;
4747 ZV = Z;
4748 ZV_BYTE = Z_BYTE;
4749 TEMP_SET_PT_BOTH (Z, Z_BYTE);
4750
4751 /* Insert the string--maybe converting multibyte to single byte
4752 or vice versa, so that all the text fits the buffer. */
4753 if (multibyte
4754 && NILP (current_buffer->enable_multibyte_characters))
4755 {
4756 int i, c, nbytes;
4757 unsigned char work[1];
4758
4759 /* Convert a multibyte string to single-byte
4760 for the *Message* buffer. */
4761 for (i = 0; i < len; i += nbytes)
4762 {
4763 c = string_char_and_length (m + i, len - i, &nbytes);
4764 work[0] = (SINGLE_BYTE_CHAR_P (c)
4765 ? c
4766 : multibyte_char_to_unibyte (c, Qnil));
4767 insert_1_both (work, 1, 1, 1, 0, 0);
4768 }
4769 }
4770 else if (! multibyte
4771 && ! NILP (current_buffer->enable_multibyte_characters))
4772 {
4773 int i, c, nbytes;
4774 unsigned char *msg = (unsigned char *) m;
4775 unsigned char str[MAX_MULTIBYTE_LENGTH];
4776 /* Convert a single-byte string to multibyte
4777 for the *Message* buffer. */
4778 for (i = 0; i < len; i++)
4779 {
4780 c = unibyte_char_to_multibyte (msg[i]);
4781 nbytes = CHAR_STRING (c, str);
4782 insert_1_both (str, 1, nbytes, 1, 0, 0);
4783 }
4784 }
4785 else if (len)
4786 insert_1 (m, len, 1, 0, 0);
4787
4788 if (nlflag)
4789 {
4790 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
4791 insert_1 ("\n", 1, 1, 0, 0);
4792
4793 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
4794 this_bol = PT;
4795 this_bol_byte = PT_BYTE;
4796
4797 if (this_bol > BEG)
4798 {
4799 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
4800 prev_bol = PT;
4801 prev_bol_byte = PT_BYTE;
4802
4803 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
4804 this_bol, this_bol_byte);
4805 if (dup)
4806 {
4807 del_range_both (prev_bol, prev_bol_byte,
4808 this_bol, this_bol_byte, 0);
4809 if (dup > 1)
4810 {
4811 char dupstr[40];
4812 int duplen;
4813
4814 /* If you change this format, don't forget to also
4815 change message_log_check_duplicate. */
4816 sprintf (dupstr, " [%d times]", dup);
4817 duplen = strlen (dupstr);
4818 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
4819 insert_1 (dupstr, duplen, 1, 0, 1);
4820 }
4821 }
4822 }
4823
4824 if (NATNUMP (Vmessage_log_max))
4825 {
4826 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
4827 -XFASTINT (Vmessage_log_max) - 1, 0);
4828 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
4829 }
4830 }
4831 BEGV = XMARKER (oldbegv)->charpos;
4832 BEGV_BYTE = marker_byte_position (oldbegv);
4833
4834 if (zv_at_end)
4835 {
4836 ZV = Z;
4837 ZV_BYTE = Z_BYTE;
4838 }
4839 else
4840 {
4841 ZV = XMARKER (oldzv)->charpos;
4842 ZV_BYTE = marker_byte_position (oldzv);
4843 }
4844
4845 if (point_at_end)
4846 TEMP_SET_PT_BOTH (Z, Z_BYTE);
4847 else
4848 /* We can't do Fgoto_char (oldpoint) because it will run some
4849 Lisp code. */
4850 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
4851 XMARKER (oldpoint)->bytepos);
4852
4853 UNGCPRO;
4854 free_marker (oldpoint);
4855 free_marker (oldbegv);
4856 free_marker (oldzv);
4857
4858 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
4859 set_buffer_internal (oldbuf);
4860 if (NILP (tem))
4861 windows_or_buffers_changed = old_windows_or_buffers_changed;
4862 message_log_need_newline = !nlflag;
4863 Vdeactivate_mark = old_deactivate_mark;
4864 }
4865 }
4866
4867
4868 /* We are at the end of the buffer after just having inserted a newline.
4869 (Note: We depend on the fact we won't be crossing the gap.)
4870 Check to see if the most recent message looks a lot like the previous one.
4871 Return 0 if different, 1 if the new one should just replace it, or a
4872 value N > 1 if we should also append " [N times]". */
4873
4874 static int
4875 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
4876 int prev_bol, this_bol;
4877 int prev_bol_byte, this_bol_byte;
4878 {
4879 int i;
4880 int len = Z_BYTE - 1 - this_bol_byte;
4881 int seen_dots = 0;
4882 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
4883 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
4884
4885 for (i = 0; i < len; i++)
4886 {
4887 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.'
4888 && p1[i] != '\n')
4889 seen_dots = 1;
4890 if (p1[i] != p2[i])
4891 return seen_dots;
4892 }
4893 p1 += len;
4894 if (*p1 == '\n')
4895 return 2;
4896 if (*p1++ == ' ' && *p1++ == '[')
4897 {
4898 int n = 0;
4899 while (*p1 >= '0' && *p1 <= '9')
4900 n = n * 10 + *p1++ - '0';
4901 if (strncmp (p1, " times]\n", 8) == 0)
4902 return n+1;
4903 }
4904 return 0;
4905 }
4906
4907
4908 /* Display an echo area message M with a specified length of LEN
4909 chars. The string may include null characters. If M is 0, clear
4910 out any existing message, and let the mini-buffer text show through.
4911
4912 The buffer M must continue to exist until after the echo area gets
4913 cleared or some other message gets displayed there. This means do
4914 not pass text that is stored in a Lisp string; do not pass text in
4915 a buffer that was alloca'd. */
4916
4917 void
4918 message2 (m, len, multibyte)
4919 char *m;
4920 int len;
4921 int multibyte;
4922 {
4923 /* First flush out any partial line written with print. */
4924 message_log_maybe_newline ();
4925 if (m)
4926 message_dolog (m, len, 1, multibyte);
4927 message2_nolog (m, len, multibyte);
4928 }
4929
4930
4931 /* The non-logging counterpart of message2. */
4932
4933 void
4934 message2_nolog (m, len, multibyte)
4935 char *m;
4936 int len;
4937 {
4938 struct frame *sf = SELECTED_FRAME ();
4939 message_enable_multibyte = multibyte;
4940
4941 if (noninteractive)
4942 {
4943 if (noninteractive_need_newline)
4944 putc ('\n', stderr);
4945 noninteractive_need_newline = 0;
4946 if (m)
4947 fwrite (m, len, 1, stderr);
4948 if (cursor_in_echo_area == 0)
4949 fprintf (stderr, "\n");
4950 fflush (stderr);
4951 }
4952 /* A null message buffer means that the frame hasn't really been
4953 initialized yet. Error messages get reported properly by
4954 cmd_error, so this must be just an informative message; toss it. */
4955 else if (INTERACTIVE
4956 && sf->glyphs_initialized_p
4957 && FRAME_MESSAGE_BUF (sf))
4958 {
4959 Lisp_Object mini_window;
4960 struct frame *f;
4961
4962 /* Get the frame containing the mini-buffer
4963 that the selected frame is using. */
4964 mini_window = FRAME_MINIBUF_WINDOW (sf);
4965 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
4966
4967 FRAME_SAMPLE_VISIBILITY (f);
4968 if (FRAME_VISIBLE_P (sf)
4969 && ! FRAME_VISIBLE_P (f))
4970 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
4971
4972 if (m)
4973 {
4974 set_message (m, Qnil, len, multibyte);
4975 if (minibuffer_auto_raise)
4976 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
4977 }
4978 else
4979 clear_message (1, 1);
4980
4981 do_pending_window_change (0);
4982 echo_area_display (1);
4983 do_pending_window_change (0);
4984 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
4985 (*frame_up_to_date_hook) (f);
4986 }
4987 }
4988
4989
4990 /* Display an echo area message M with a specified length of NBYTES
4991 bytes. The string may include null characters. If M is not a
4992 string, clear out any existing message, and let the mini-buffer
4993 text show through. */
4994
4995 void
4996 message3 (m, nbytes, multibyte)
4997 Lisp_Object m;
4998 int nbytes;
4999 int multibyte;
5000 {
5001 struct gcpro gcpro1;
5002
5003 GCPRO1 (m);
5004
5005 /* First flush out any partial line written with print. */
5006 message_log_maybe_newline ();
5007 if (STRINGP (m))
5008 message_dolog (XSTRING (m)->data, nbytes, 1, multibyte);
5009 message3_nolog (m, nbytes, multibyte);
5010
5011 UNGCPRO;
5012 }
5013
5014
5015 /* The non-logging version of message3. */
5016
5017 void
5018 message3_nolog (m, nbytes, multibyte)
5019 Lisp_Object m;
5020 int nbytes, multibyte;
5021 {
5022 struct frame *sf = SELECTED_FRAME ();
5023 message_enable_multibyte = multibyte;
5024
5025 if (noninteractive)
5026 {
5027 if (noninteractive_need_newline)
5028 putc ('\n', stderr);
5029 noninteractive_need_newline = 0;
5030 if (STRINGP (m))
5031 fwrite (XSTRING (m)->data, nbytes, 1, stderr);
5032 if (cursor_in_echo_area == 0)
5033 fprintf (stderr, "\n");
5034 fflush (stderr);
5035 }
5036 /* A null message buffer means that the frame hasn't really been
5037 initialized yet. Error messages get reported properly by
5038 cmd_error, so this must be just an informative message; toss it. */
5039 else if (INTERACTIVE
5040 && sf->glyphs_initialized_p
5041 && FRAME_MESSAGE_BUF (sf))
5042 {
5043 Lisp_Object mini_window;
5044 Lisp_Object frame;
5045 struct frame *f;
5046
5047 /* Get the frame containing the mini-buffer
5048 that the selected frame is using. */
5049 mini_window = FRAME_MINIBUF_WINDOW (sf);
5050 frame = XWINDOW (mini_window)->frame;
5051 f = XFRAME (frame);
5052
5053 FRAME_SAMPLE_VISIBILITY (f);
5054 if (FRAME_VISIBLE_P (sf)
5055 && !FRAME_VISIBLE_P (f))
5056 Fmake_frame_visible (frame);
5057
5058 if (STRINGP (m) && XSTRING (m)->size)
5059 {
5060 set_message (NULL, m, nbytes, multibyte);
5061 if (minibuffer_auto_raise)
5062 Fraise_frame (frame);
5063 }
5064 else
5065 clear_message (1, 1);
5066
5067 do_pending_window_change (0);
5068 echo_area_display (1);
5069 do_pending_window_change (0);
5070 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5071 (*frame_up_to_date_hook) (f);
5072 }
5073 }
5074
5075
5076 /* Display a null-terminated echo area message M. If M is 0, clear
5077 out any existing message, and let the mini-buffer text show through.
5078
5079 The buffer M must continue to exist until after the echo area gets
5080 cleared or some other message gets displayed there. Do not pass
5081 text that is stored in a Lisp string. Do not pass text in a buffer
5082 that was alloca'd. */
5083
5084 void
5085 message1 (m)
5086 char *m;
5087 {
5088 message2 (m, (m ? strlen (m) : 0), 0);
5089 }
5090
5091
5092 /* The non-logging counterpart of message1. */
5093
5094 void
5095 message1_nolog (m)
5096 char *m;
5097 {
5098 message2_nolog (m, (m ? strlen (m) : 0), 0);
5099 }
5100
5101 /* Display a message M which contains a single %s
5102 which gets replaced with STRING. */
5103
5104 void
5105 message_with_string (m, string, log)
5106 char *m;
5107 Lisp_Object string;
5108 int log;
5109 {
5110 if (noninteractive)
5111 {
5112 if (m)
5113 {
5114 if (noninteractive_need_newline)
5115 putc ('\n', stderr);
5116 noninteractive_need_newline = 0;
5117 fprintf (stderr, m, XSTRING (string)->data);
5118 if (cursor_in_echo_area == 0)
5119 fprintf (stderr, "\n");
5120 fflush (stderr);
5121 }
5122 }
5123 else if (INTERACTIVE)
5124 {
5125 /* The frame whose minibuffer we're going to display the message on.
5126 It may be larger than the selected frame, so we need
5127 to use its buffer, not the selected frame's buffer. */
5128 Lisp_Object mini_window;
5129 struct frame *f, *sf = SELECTED_FRAME ();
5130
5131 /* Get the frame containing the minibuffer
5132 that the selected frame is using. */
5133 mini_window = FRAME_MINIBUF_WINDOW (sf);
5134 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5135
5136 /* A null message buffer means that the frame hasn't really been
5137 initialized yet. Error messages get reported properly by
5138 cmd_error, so this must be just an informative message; toss it. */
5139 if (FRAME_MESSAGE_BUF (f))
5140 {
5141 int len;
5142 char *a[1];
5143 a[0] = (char *) XSTRING (string)->data;
5144
5145 len = doprnt (FRAME_MESSAGE_BUF (f),
5146 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5147
5148 if (log)
5149 message2 (FRAME_MESSAGE_BUF (f), len,
5150 STRING_MULTIBYTE (string));
5151 else
5152 message2_nolog (FRAME_MESSAGE_BUF (f), len,
5153 STRING_MULTIBYTE (string));
5154
5155 /* Print should start at the beginning of the message
5156 buffer next time. */
5157 message_buf_print = 0;
5158 }
5159 }
5160 }
5161
5162
5163 /* Dump an informative message to the minibuf. If M is 0, clear out
5164 any existing message, and let the mini-buffer text show through. */
5165
5166 /* VARARGS 1 */
5167 void
5168 message (m, a1, a2, a3)
5169 char *m;
5170 EMACS_INT a1, a2, a3;
5171 {
5172 if (noninteractive)
5173 {
5174 if (m)
5175 {
5176 if (noninteractive_need_newline)
5177 putc ('\n', stderr);
5178 noninteractive_need_newline = 0;
5179 fprintf (stderr, m, a1, a2, a3);
5180 if (cursor_in_echo_area == 0)
5181 fprintf (stderr, "\n");
5182 fflush (stderr);
5183 }
5184 }
5185 else if (INTERACTIVE)
5186 {
5187 /* The frame whose mini-buffer we're going to display the message
5188 on. It may be larger than the selected frame, so we need to
5189 use its buffer, not the selected frame's buffer. */
5190 Lisp_Object mini_window;
5191 struct frame *f, *sf = SELECTED_FRAME ();
5192
5193 /* Get the frame containing the mini-buffer
5194 that the selected frame is using. */
5195 mini_window = FRAME_MINIBUF_WINDOW (sf);
5196 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5197
5198 /* A null message buffer means that the frame hasn't really been
5199 initialized yet. Error messages get reported properly by
5200 cmd_error, so this must be just an informative message; toss
5201 it. */
5202 if (FRAME_MESSAGE_BUF (f))
5203 {
5204 if (m)
5205 {
5206 int len;
5207 #ifdef NO_ARG_ARRAY
5208 char *a[3];
5209 a[0] = (char *) a1;
5210 a[1] = (char *) a2;
5211 a[2] = (char *) a3;
5212
5213 len = doprnt (FRAME_MESSAGE_BUF (f),
5214 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5215 #else
5216 len = doprnt (FRAME_MESSAGE_BUF (f),
5217 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
5218 (char **) &a1);
5219 #endif /* NO_ARG_ARRAY */
5220
5221 message2 (FRAME_MESSAGE_BUF (f), len, 0);
5222 }
5223 else
5224 message1 (0);
5225
5226 /* Print should start at the beginning of the message
5227 buffer next time. */
5228 message_buf_print = 0;
5229 }
5230 }
5231 }
5232
5233
5234 /* The non-logging version of message. */
5235
5236 void
5237 message_nolog (m, a1, a2, a3)
5238 char *m;
5239 EMACS_INT a1, a2, a3;
5240 {
5241 Lisp_Object old_log_max;
5242 old_log_max = Vmessage_log_max;
5243 Vmessage_log_max = Qnil;
5244 message (m, a1, a2, a3);
5245 Vmessage_log_max = old_log_max;
5246 }
5247
5248
5249 /* Display the current message in the current mini-buffer. This is
5250 only called from error handlers in process.c, and is not time
5251 critical. */
5252
5253 void
5254 update_echo_area ()
5255 {
5256 if (!NILP (echo_area_buffer[0]))
5257 {
5258 Lisp_Object string;
5259 string = Fcurrent_message ();
5260 message3 (string, XSTRING (string)->size,
5261 !NILP (current_buffer->enable_multibyte_characters));
5262 }
5263 }
5264
5265
5266 /* Make sure echo area buffers in echo_buffers[] are life. If they
5267 aren't, make new ones. */
5268
5269 static void
5270 ensure_echo_area_buffers ()
5271 {
5272 int i;
5273
5274 for (i = 0; i < 2; ++i)
5275 if (!BUFFERP (echo_buffer[i])
5276 || NILP (XBUFFER (echo_buffer[i])->name))
5277 {
5278 char name[30];
5279 sprintf (name, " *Echo Area %d*", i);
5280 echo_buffer[i] = Fget_buffer_create (build_string (name));
5281 }
5282 }
5283
5284
5285 /* Call FN with args A1..A5 with either the current or last displayed
5286 echo_area_buffer as current buffer.
5287
5288 WHICH zero means use the current message buffer
5289 echo_area_buffer[0]. If that is nil, choose a suitable buffer
5290 from echo_buffer[] and clear it.
5291
5292 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
5293 suitable buffer from echo_buffer[] and clear it.
5294
5295 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
5296 that the current message becomes the last displayed one, make
5297 choose a suitable buffer for echo_area_buffer[0], and clear it.
5298
5299 Value is what FN returns. */
5300
5301 static int
5302 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10)
5303 struct window *w;
5304 int which;
5305 int (*fn) ();
5306 int a1, a2, a3, a4, a5, a6, a7, a8, a9, a10;
5307 {
5308 Lisp_Object buffer;
5309 int this_one, the_other, clear_buffer_p, rc;
5310 int count = specpdl_ptr - specpdl;
5311
5312 /* If buffers aren't life, make new ones. */
5313 ensure_echo_area_buffers ();
5314
5315 clear_buffer_p = 0;
5316
5317 if (which == 0)
5318 this_one = 0, the_other = 1;
5319 else if (which > 0)
5320 this_one = 1, the_other = 0;
5321 else
5322 {
5323 this_one = 0, the_other = 1;
5324 clear_buffer_p = 1;
5325
5326 /* We need a fresh one in case the current echo buffer equals
5327 the one containing the last displayed echo area message. */
5328 if (!NILP (echo_area_buffer[this_one])
5329 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
5330 echo_area_buffer[this_one] = Qnil;
5331 }
5332
5333 /* Choose a suitable buffer from echo_buffer[] is we don't
5334 have one. */
5335 if (NILP (echo_area_buffer[this_one]))
5336 {
5337 echo_area_buffer[this_one]
5338 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
5339 ? echo_buffer[the_other]
5340 : echo_buffer[this_one]);
5341 clear_buffer_p = 1;
5342 }
5343
5344 buffer = echo_area_buffer[this_one];
5345
5346 record_unwind_protect (unwind_with_echo_area_buffer,
5347 with_echo_area_buffer_unwind_data (w));
5348
5349 /* Make the echo area buffer current. Note that for display
5350 purposes, it is not necessary that the displayed window's buffer
5351 == current_buffer, except for text property lookup. So, let's
5352 only set that buffer temporarily here without doing a full
5353 Fset_window_buffer. We must also change w->pointm, though,
5354 because otherwise an assertions in unshow_buffer fails, and Emacs
5355 aborts. */
5356 set_buffer_internal_1 (XBUFFER (buffer));
5357 if (w)
5358 {
5359 w->buffer = buffer;
5360 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
5361 }
5362 current_buffer->truncate_lines = Qnil;
5363 current_buffer->undo_list = Qt;
5364 current_buffer->read_only = Qnil;
5365
5366 if (clear_buffer_p && Z > BEG)
5367 del_range (BEG, Z);
5368
5369 xassert (BEGV >= BEG);
5370 xassert (ZV <= Z && ZV >= BEGV);
5371
5372 rc = fn (a1, a2, a3, a4, a5);
5373
5374 xassert (BEGV >= BEG);
5375 xassert (ZV <= Z && ZV >= BEGV);
5376
5377 unbind_to (count, Qnil);
5378 return rc;
5379 }
5380
5381
5382 /* Save state that should be preserved around the call to the function
5383 FN called in with_echo_area_buffer. */
5384
5385 static Lisp_Object
5386 with_echo_area_buffer_unwind_data (w)
5387 struct window *w;
5388 {
5389 int i = 0;
5390 Lisp_Object vector;
5391
5392 /* Reduce consing by keeping one vector in
5393 Vwith_echo_area_save_vector. */
5394 vector = Vwith_echo_area_save_vector;
5395 Vwith_echo_area_save_vector = Qnil;
5396
5397 if (NILP (vector))
5398 vector = Fmake_vector (make_number (7), Qnil);
5399
5400 XSETBUFFER (XVECTOR (vector)->contents[i], current_buffer); ++i;
5401 XVECTOR (vector)->contents[i++] = Vdeactivate_mark;
5402 XVECTOR (vector)->contents[i++] = make_number (windows_or_buffers_changed);
5403
5404 if (w)
5405 {
5406 XSETWINDOW (XVECTOR (vector)->contents[i], w); ++i;
5407 XVECTOR (vector)->contents[i++] = w->buffer;
5408 XVECTOR (vector)->contents[i++]
5409 = make_number (XMARKER (w->pointm)->charpos);
5410 XVECTOR (vector)->contents[i++]
5411 = make_number (XMARKER (w->pointm)->bytepos);
5412 }
5413 else
5414 {
5415 int end = i + 4;
5416 while (i < end)
5417 XVECTOR (vector)->contents[i++] = Qnil;
5418 }
5419
5420 xassert (i == XVECTOR (vector)->size);
5421 return vector;
5422 }
5423
5424
5425 /* Restore global state from VECTOR which was created by
5426 with_echo_area_buffer_unwind_data. */
5427
5428 static Lisp_Object
5429 unwind_with_echo_area_buffer (vector)
5430 Lisp_Object vector;
5431 {
5432 int i = 0;
5433
5434 set_buffer_internal_1 (XBUFFER (XVECTOR (vector)->contents[i])); ++i;
5435 Vdeactivate_mark = XVECTOR (vector)->contents[i]; ++i;
5436 windows_or_buffers_changed = XFASTINT (XVECTOR (vector)->contents[i]); ++i;
5437
5438 if (WINDOWP (XVECTOR (vector)->contents[i]))
5439 {
5440 struct window *w;
5441 Lisp_Object buffer, charpos, bytepos;
5442
5443 w = XWINDOW (XVECTOR (vector)->contents[i]); ++i;
5444 buffer = XVECTOR (vector)->contents[i]; ++i;
5445 charpos = XVECTOR (vector)->contents[i]; ++i;
5446 bytepos = XVECTOR (vector)->contents[i]; ++i;
5447
5448 w->buffer = buffer;
5449 set_marker_both (w->pointm, buffer,
5450 XFASTINT (charpos), XFASTINT (bytepos));
5451 }
5452
5453 Vwith_echo_area_save_vector = vector;
5454 return Qnil;
5455 }
5456
5457
5458 /* Set up the echo area for use by print functions. MULTIBYTE_P
5459 non-zero means we will print multibyte. */
5460
5461 void
5462 setup_echo_area_for_printing (multibyte_p)
5463 int multibyte_p;
5464 {
5465 ensure_echo_area_buffers ();
5466
5467 if (!message_buf_print)
5468 {
5469 /* A message has been output since the last time we printed.
5470 Choose a fresh echo area buffer. */
5471 if (EQ (echo_area_buffer[1], echo_buffer[0]))
5472 echo_area_buffer[0] = echo_buffer[1];
5473 else
5474 echo_area_buffer[0] = echo_buffer[0];
5475
5476 /* Switch to that buffer and clear it. */
5477 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
5478 if (Z > BEG)
5479 del_range (BEG, Z);
5480 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
5481
5482 /* Set up the buffer for the multibyteness we need. */
5483 if (multibyte_p
5484 != !NILP (current_buffer->enable_multibyte_characters))
5485 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
5486
5487 /* Raise the frame containing the echo area. */
5488 if (minibuffer_auto_raise)
5489 {
5490 struct frame *sf = SELECTED_FRAME ();
5491 Lisp_Object mini_window;
5492 mini_window = FRAME_MINIBUF_WINDOW (sf);
5493 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5494 }
5495
5496 message_buf_print = 1;
5497 }
5498 else
5499 {
5500 if (NILP (echo_area_buffer[0]))
5501 {
5502 if (EQ (echo_area_buffer[1], echo_buffer[0]))
5503 echo_area_buffer[0] = echo_buffer[1];
5504 else
5505 echo_area_buffer[0] = echo_buffer[0];
5506 }
5507
5508 if (current_buffer != XBUFFER (echo_area_buffer[0]))
5509 /* Someone switched buffers between print requests. */
5510 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
5511 }
5512 }
5513
5514
5515 /* Display an echo area message in window W. Value is non-zero if W's
5516 height is changed. If display_last_displayed_message_p is
5517 non-zero, display the message that was last displayed, otherwise
5518 display the current message. */
5519
5520 static int
5521 display_echo_area (w)
5522 struct window *w;
5523 {
5524 int i, no_message_p, window_height_changed_p, count;
5525
5526 /* Temporarily disable garbage collections while displaying the echo
5527 area. This is done because a GC can print a message itself.
5528 That message would modify the echo area buffer's contents while a
5529 redisplay of the buffer is going on, and seriously confuse
5530 redisplay. */
5531 count = inhibit_garbage_collection ();
5532
5533 /* If there is no message, we must call display_echo_area_1
5534 nevertheless because it resizes the window. But we will have to
5535 reset the echo_area_buffer in question to nil at the end because
5536 with_echo_area_buffer will sets it to an empty buffer. */
5537 i = display_last_displayed_message_p ? 1 : 0;
5538 no_message_p = NILP (echo_area_buffer[i]);
5539
5540 window_height_changed_p
5541 = with_echo_area_buffer (w, display_last_displayed_message_p,
5542 (int (*) ()) display_echo_area_1, w);
5543
5544 if (no_message_p)
5545 echo_area_buffer[i] = Qnil;
5546
5547 unbind_to (count, Qnil);
5548 return window_height_changed_p;
5549 }
5550
5551
5552 /* Helper for display_echo_area. Display the current buffer which
5553 contains the current echo area message in window W, a mini-window.
5554 Change the height of W so that all of the message is displayed.
5555 Value is non-zero if height of W was changed. */
5556
5557 static int
5558 display_echo_area_1 (w)
5559 struct window *w;
5560 {
5561 Lisp_Object window;
5562 struct text_pos start;
5563 int window_height_changed_p = 0;
5564
5565 /* Do this before displaying, so that we have a large enough glyph
5566 matrix for the display. */
5567 window_height_changed_p = resize_mini_window (w, 0);
5568
5569 /* Display. */
5570 clear_glyph_matrix (w->desired_matrix);
5571 XSETWINDOW (window, w);
5572 SET_TEXT_POS (start, BEG, BEG_BYTE);
5573 try_window (window, start);
5574
5575 return window_height_changed_p;
5576 }
5577
5578
5579 /* Resize the echo area window to exactly the size needed for the
5580 currently displayed message, if there is one. */
5581
5582 void
5583 resize_echo_area_axactly ()
5584 {
5585 if (BUFFERP (echo_area_buffer[0])
5586 && WINDOWP (echo_area_window))
5587 {
5588 struct window *w = XWINDOW (echo_area_window);
5589 int resized_p;
5590
5591 resized_p = with_echo_area_buffer (w, 0,
5592 (int (*) ()) resize_mini_window,
5593 w, 1);
5594 if (resized_p)
5595 {
5596 ++windows_or_buffers_changed;
5597 ++update_mode_lines;
5598 redisplay_internal (0);
5599 }
5600 }
5601 }
5602
5603
5604 /* Resize mini-window W to fit the size of its contents. EXACT:P
5605 means size the window exactly to the size needed. Otherwise, it's
5606 only enlarged until W's buffer is empty. Value is non-zero if
5607 the window height has been changed. */
5608
5609 int
5610 resize_mini_window (w, exact_p)
5611 struct window *w;
5612 int exact_p;
5613 {
5614 struct frame *f = XFRAME (w->frame);
5615 int window_height_changed_p = 0;
5616
5617 xassert (MINI_WINDOW_P (w));
5618
5619 /* Nil means don't try to resize. */
5620 if (NILP (Vmax_mini_window_height)
5621 || (FRAME_X_P (f) && f->output_data.x == NULL))
5622 return 0;
5623
5624 if (!FRAME_MINIBUF_ONLY_P (f))
5625 {
5626 struct it it;
5627 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
5628 int total_height = XFASTINT (root->height) + XFASTINT (w->height);
5629 int height, max_height;
5630 int unit = CANON_Y_UNIT (f);
5631 struct text_pos start;
5632
5633 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
5634
5635 /* Compute the max. number of lines specified by the user. */
5636 if (FLOATP (Vmax_mini_window_height))
5637 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
5638 else if (INTEGERP (Vmax_mini_window_height))
5639 max_height = XINT (Vmax_mini_window_height);
5640 else
5641 max_height = total_height / 4;
5642
5643 /* Correct that max. height if it's bogus. */
5644 max_height = max (1, max_height);
5645 max_height = min (total_height, max_height);
5646
5647 /* Find out the height of the text in the window. */
5648 last_height = 0;
5649 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
5650 if (it.max_ascent == 0 && it.max_descent == 0)
5651 height = it.current_y + last_height;
5652 else
5653 height = it.current_y + it.max_ascent + it.max_descent;
5654 height = (height + unit - 1) / unit;
5655
5656 /* Compute a suitable window start. */
5657 if (height > max_height)
5658 {
5659 height = max_height;
5660 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
5661 move_it_vertically_backward (&it, (height - 1) * unit);
5662 start = it.current.pos;
5663 }
5664 else
5665 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
5666 SET_MARKER_FROM_TEXT_POS (w->start, start);
5667
5668 /* Let it grow only, until we display an empty message, in which
5669 case the window shrinks again. */
5670 if (height > XFASTINT (w->height))
5671 {
5672 int old_height = XFASTINT (w->height);
5673 freeze_window_starts (f, 1);
5674 grow_mini_window (w, height - XFASTINT (w->height));
5675 window_height_changed_p = XFASTINT (w->height) != old_height;
5676 }
5677 else if (height < XFASTINT (w->height)
5678 && (exact_p || BEGV == ZV))
5679 {
5680 int old_height = XFASTINT (w->height);
5681 freeze_window_starts (f, 0);
5682 shrink_mini_window (w);
5683 window_height_changed_p = XFASTINT (w->height) != old_height;
5684 }
5685 }
5686
5687 return window_height_changed_p;
5688 }
5689
5690
5691 /* Value is the current message, a string, or nil if there is no
5692 current message. */
5693
5694 Lisp_Object
5695 current_message ()
5696 {
5697 Lisp_Object msg;
5698
5699 if (NILP (echo_area_buffer[0]))
5700 msg = Qnil;
5701 else
5702 {
5703 with_echo_area_buffer (0, 0, (int (*) ()) current_message_1, &msg);
5704 if (NILP (msg))
5705 echo_area_buffer[0] = Qnil;
5706 }
5707
5708 return msg;
5709 }
5710
5711
5712 static int
5713 current_message_1 (msg)
5714 Lisp_Object *msg;
5715 {
5716 if (Z > BEG)
5717 *msg = make_buffer_string (BEG, Z, 1);
5718 else
5719 *msg = Qnil;
5720 return 0;
5721 }
5722
5723
5724 /* Push the current message on Vmessage_stack for later restauration
5725 by restore_message. Value is non-zero if the current message isn't
5726 empty. This is a relatively infrequent operation, so it's not
5727 worth optimizing. */
5728
5729 int
5730 push_message ()
5731 {
5732 Lisp_Object msg;
5733 msg = current_message ();
5734 Vmessage_stack = Fcons (msg, Vmessage_stack);
5735 return STRINGP (msg);
5736 }
5737
5738
5739 /* Restore message display from the top of Vmessage_stack. */
5740
5741 void
5742 restore_message ()
5743 {
5744 Lisp_Object msg;
5745
5746 xassert (CONSP (Vmessage_stack));
5747 msg = XCAR (Vmessage_stack);
5748 if (STRINGP (msg))
5749 message3_nolog (msg, STRING_BYTES (XSTRING (msg)), STRING_MULTIBYTE (msg));
5750 else
5751 message3_nolog (msg, 0, 0);
5752 }
5753
5754
5755 /* Pop the top-most entry off Vmessage_stack. */
5756
5757 void
5758 pop_message ()
5759 {
5760 xassert (CONSP (Vmessage_stack));
5761 Vmessage_stack = XCDR (Vmessage_stack);
5762 }
5763
5764
5765 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
5766 exits. If the stack is not empty, we have a missing pop_message
5767 somewhere. */
5768
5769 void
5770 check_message_stack ()
5771 {
5772 if (!NILP (Vmessage_stack))
5773 abort ();
5774 }
5775
5776
5777 /* Truncate to NCHARS what will be displayed in the echo area the next
5778 time we display it---but don't redisplay it now. */
5779
5780 void
5781 truncate_echo_area (nchars)
5782 int nchars;
5783 {
5784 if (nchars == 0)
5785 echo_area_buffer[0] = Qnil;
5786 /* A null message buffer means that the frame hasn't really been
5787 initialized yet. Error messages get reported properly by
5788 cmd_error, so this must be just an informative message; toss it. */
5789 else if (!noninteractive
5790 && INTERACTIVE
5791 && !NILP (echo_area_buffer[0]))
5792 {
5793 struct frame *sf = SELECTED_FRAME ();
5794 if (FRAME_MESSAGE_BUF (sf))
5795 with_echo_area_buffer (0, 0, (int (*) ()) truncate_message_1, nchars);
5796 }
5797 }
5798
5799
5800 /* Helper function for truncate_echo_area. Truncate the current
5801 message to at most NCHARS characters. */
5802
5803 static int
5804 truncate_message_1 (nchars)
5805 int nchars;
5806 {
5807 if (BEG + nchars < Z)
5808 del_range (BEG + nchars, Z);
5809 if (Z == BEG)
5810 echo_area_buffer[0] = Qnil;
5811 return 0;
5812 }
5813
5814
5815 /* Set the current message to a substring of S or STRING.
5816
5817 If STRING is a Lisp string, set the message to the first NBYTES
5818 bytes from STRING. NBYTES zero means use the whole string. If
5819 STRING is multibyte, the message will be displayed multibyte.
5820
5821 If S is not null, set the message to the first LEN bytes of S. LEN
5822 zero means use the whole string. MULTIBYTE_P non-zero means S is
5823 multibyte. Display the message multibyte in that case. */
5824
5825 void
5826 set_message (s, string, nbytes, multibyte_p)
5827 char *s;
5828 Lisp_Object string;
5829 int nbytes;
5830 {
5831 message_enable_multibyte
5832 = ((s && multibyte_p)
5833 || (STRINGP (string) && STRING_MULTIBYTE (string)));
5834
5835 with_echo_area_buffer (0, -1, (int (*) ()) set_message_1,
5836 s, string, nbytes, multibyte_p);
5837 message_buf_print = 0;
5838 }
5839
5840
5841 /* Helper function for set_message. Arguments have the same meaning
5842 as there. This function is called with the echo area buffer being
5843 current. */
5844
5845 static int
5846 set_message_1 (s, string, nbytes, multibyte_p)
5847 char *s;
5848 Lisp_Object string;
5849 int nbytes, multibyte_p;
5850 {
5851 xassert (BEG == Z);
5852
5853 /* Change multibyteness of the echo buffer appropriately. */
5854 if (message_enable_multibyte
5855 != !NILP (current_buffer->enable_multibyte_characters))
5856 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
5857
5858 /* Insert new message at BEG. */
5859 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
5860
5861 if (STRINGP (string))
5862 {
5863 int nchars;
5864
5865 if (nbytes == 0)
5866 nbytes = XSTRING (string)->size_byte;
5867 nchars = string_byte_to_char (string, nbytes);
5868
5869 /* This function takes care of single/multibyte conversion. We
5870 just have to ensure that the echo area buffer has the right
5871 setting of enable_multibyte_characters. */
5872 insert_from_string (string, 0, 0, nchars, nbytes, 1);
5873 }
5874 else if (s)
5875 {
5876 if (nbytes == 0)
5877 nbytes = strlen (s);
5878
5879 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
5880 {
5881 /* Convert from multi-byte to single-byte. */
5882 int i, c, n;
5883 unsigned char work[1];
5884
5885 /* Convert a multibyte string to single-byte. */
5886 for (i = 0; i < nbytes; i += n)
5887 {
5888 c = string_char_and_length (s + i, nbytes - i, &n);
5889 work[0] = (SINGLE_BYTE_CHAR_P (c)
5890 ? c
5891 : multibyte_char_to_unibyte (c, Qnil));
5892 insert_1_both (work, 1, 1, 1, 0, 0);
5893 }
5894 }
5895 else if (!multibyte_p
5896 && !NILP (current_buffer->enable_multibyte_characters))
5897 {
5898 /* Convert from single-byte to multi-byte. */
5899 int i, c, n;
5900 unsigned char *msg = (unsigned char *) s;
5901 unsigned char str[MAX_MULTIBYTE_LENGTH];
5902
5903 /* Convert a single-byte string to multibyte. */
5904 for (i = 0; i < nbytes; i++)
5905 {
5906 c = unibyte_char_to_multibyte (msg[i]);
5907 n = CHAR_STRING (c, str);
5908 insert_1_both (str, 1, n, 1, 0, 0);
5909 }
5910 }
5911 else
5912 insert_1 (s, nbytes, 1, 0, 0);
5913 }
5914
5915 return 0;
5916 }
5917
5918
5919 /* Clear messages. CURRENT_P non-zero means clear the current
5920 message. LAST_DISPLAYED_P non-zero means clear the message
5921 last displayed. */
5922
5923 void
5924 clear_message (current_p, last_displayed_p)
5925 int current_p, last_displayed_p;
5926 {
5927 if (current_p)
5928 echo_area_buffer[0] = Qnil;
5929
5930 if (last_displayed_p)
5931 echo_area_buffer[1] = Qnil;
5932
5933 message_buf_print = 0;
5934 }
5935
5936 /* Clear garbaged frames.
5937
5938 This function is used where the old redisplay called
5939 redraw_garbaged_frames which in turn called redraw_frame which in
5940 turn called clear_frame. The call to clear_frame was a source of
5941 flickering. I believe a clear_frame is not necessary. It should
5942 suffice in the new redisplay to invalidate all current matrices,
5943 and ensure a complete redisplay of all windows. */
5944
5945 static void
5946 clear_garbaged_frames ()
5947 {
5948 if (frame_garbaged)
5949 {
5950 Lisp_Object tail, frame;
5951
5952 FOR_EACH_FRAME (tail, frame)
5953 {
5954 struct frame *f = XFRAME (frame);
5955
5956 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
5957 {
5958 clear_current_matrices (f);
5959 f->garbaged = 0;
5960 }
5961 }
5962
5963 frame_garbaged = 0;
5964 ++windows_or_buffers_changed;
5965 }
5966 }
5967
5968
5969 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
5970 is non-zero update selected_frame. Value is non-zero if the
5971 mini-windows height has been changed. */
5972
5973 static int
5974 echo_area_display (update_frame_p)
5975 int update_frame_p;
5976 {
5977 Lisp_Object mini_window;
5978 struct window *w;
5979 struct frame *f;
5980 int window_height_changed_p = 0;
5981 struct frame *sf = SELECTED_FRAME ();
5982
5983 mini_window = FRAME_MINIBUF_WINDOW (sf);
5984 w = XWINDOW (mini_window);
5985 f = XFRAME (WINDOW_FRAME (w));
5986
5987 /* Don't display if frame is invisible or not yet initialized. */
5988 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
5989 return 0;
5990
5991 #ifdef HAVE_WINDOW_SYSTEM
5992 /* When Emacs starts, selected_frame may be a visible terminal
5993 frame, even if we run under a window system. If we let this
5994 through, a message would be displayed on the terminal. */
5995 if (EQ (selected_frame, Vterminal_frame)
5996 && !NILP (Vwindow_system))
5997 return 0;
5998 #endif /* HAVE_WINDOW_SYSTEM */
5999
6000 /* Redraw garbaged frames. */
6001 if (frame_garbaged)
6002 clear_garbaged_frames ();
6003
6004 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
6005 {
6006 echo_area_window = mini_window;
6007 window_height_changed_p = display_echo_area (w);
6008 w->must_be_updated_p = 1;
6009
6010 if (update_frame_p)
6011 {
6012 /* Not called from redisplay_internal. If we changed
6013 window configuration, we must redisplay thoroughly.
6014 Otherwise, we can do with updating what we displayed
6015 above. */
6016 if (window_height_changed_p)
6017 {
6018 ++windows_or_buffers_changed;
6019 ++update_mode_lines;
6020 redisplay_internal (0);
6021 }
6022 else if (FRAME_WINDOW_P (f))
6023 {
6024 update_single_window (w, 1);
6025 rif->flush_display (f);
6026 }
6027 else
6028 update_frame (f, 1, 1);
6029 }
6030 }
6031 else if (!EQ (mini_window, selected_window))
6032 windows_or_buffers_changed++;
6033
6034 /* Last displayed message is now the current message. */
6035 echo_area_buffer[1] = echo_area_buffer[0];
6036
6037 /* Prevent redisplay optimization in redisplay_internal by resetting
6038 this_line_start_pos. This is done because the mini-buffer now
6039 displays the message instead of its buffer text. */
6040 if (EQ (mini_window, selected_window))
6041 CHARPOS (this_line_start_pos) = 0;
6042
6043 return window_height_changed_p;
6044 }
6045
6046
6047 \f
6048 /***********************************************************************
6049 Frame Titles
6050 ***********************************************************************/
6051
6052
6053 #ifdef HAVE_WINDOW_SYSTEM
6054
6055 /* A buffer for constructing frame titles in it; allocated from the
6056 heap in init_xdisp and resized as needed in store_frame_title_char. */
6057
6058 static char *frame_title_buf;
6059
6060 /* The buffer's end, and a current output position in it. */
6061
6062 static char *frame_title_buf_end;
6063 static char *frame_title_ptr;
6064
6065
6066 /* Store a single character C for the frame title in frame_title_buf.
6067 Re-allocate frame_title_buf if necessary. */
6068
6069 static void
6070 store_frame_title_char (c)
6071 char c;
6072 {
6073 /* If output position has reached the end of the allocated buffer,
6074 double the buffer's size. */
6075 if (frame_title_ptr == frame_title_buf_end)
6076 {
6077 int len = frame_title_ptr - frame_title_buf;
6078 int new_size = 2 * len * sizeof *frame_title_buf;
6079 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
6080 frame_title_buf_end = frame_title_buf + new_size;
6081 frame_title_ptr = frame_title_buf + len;
6082 }
6083
6084 *frame_title_ptr++ = c;
6085 }
6086
6087
6088 /* Store part of a frame title in frame_title_buf, beginning at
6089 frame_title_ptr. STR is the string to store. Do not copy more
6090 than PRECISION number of bytes from STR; PRECISION <= 0 means copy
6091 the whole string. Pad with spaces until FIELD_WIDTH number of
6092 characters have been copied; FIELD_WIDTH <= 0 means don't pad.
6093 Called from display_mode_element when it is used to build a frame
6094 title. */
6095
6096 static int
6097 store_frame_title (str, field_width, precision)
6098 unsigned char *str;
6099 int field_width, precision;
6100 {
6101 int n = 0;
6102
6103 /* Copy at most PRECISION chars from STR. */
6104 while ((precision <= 0 || n < precision)
6105 && *str)
6106 {
6107 store_frame_title_char (*str++);
6108 ++n;
6109 }
6110
6111 /* Fill up with spaces until FIELD_WIDTH reached. */
6112 while (field_width > 0
6113 && n < field_width)
6114 {
6115 store_frame_title_char (' ');
6116 ++n;
6117 }
6118
6119 return n;
6120 }
6121
6122
6123 /* Set the title of FRAME, if it has changed. The title format is
6124 Vicon_title_format if FRAME is iconified, otherwise it is
6125 frame_title_format. */
6126
6127 static void
6128 x_consider_frame_title (frame)
6129 Lisp_Object frame;
6130 {
6131 struct frame *f = XFRAME (frame);
6132
6133 if (FRAME_WINDOW_P (f)
6134 || FRAME_MINIBUF_ONLY_P (f)
6135 || f->explicit_name)
6136 {
6137 /* Do we have more than one visible frame on this X display? */
6138 Lisp_Object tail;
6139 Lisp_Object fmt;
6140 struct buffer *obuf;
6141 int len;
6142 struct it it;
6143
6144 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
6145 {
6146 struct frame *tf = XFRAME (XCAR (tail));
6147
6148 if (tf != f
6149 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
6150 && !FRAME_MINIBUF_ONLY_P (tf)
6151 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
6152 break;
6153 }
6154
6155 /* Set global variable indicating that multiple frames exist. */
6156 multiple_frames = CONSP (tail);
6157
6158 /* Switch to the buffer of selected window of the frame. Set up
6159 frame_title_ptr so that display_mode_element will output into it;
6160 then display the title. */
6161 obuf = current_buffer;
6162 Fset_buffer (XWINDOW (f->selected_window)->buffer);
6163 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
6164 frame_title_ptr = frame_title_buf;
6165 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
6166 NULL, DEFAULT_FACE_ID);
6167 len = display_mode_element (&it, 0, -1, -1, fmt);
6168 frame_title_ptr = NULL;
6169 set_buffer_internal (obuf);
6170
6171 /* Set the title only if it's changed. This avoids consing in
6172 the common case where it hasn't. (If it turns out that we've
6173 already wasted too much time by walking through the list with
6174 display_mode_element, then we might need to optimize at a
6175 higher level than this.) */
6176 if (! STRINGP (f->name)
6177 || STRING_BYTES (XSTRING (f->name)) != len
6178 || bcmp (frame_title_buf, XSTRING (f->name)->data, len) != 0)
6179 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
6180 }
6181 }
6182
6183 #else /* not HAVE_WINDOW_SYSTEM */
6184
6185 #define frame_title_ptr ((char *)0)
6186 #define store_frame_title(str, mincol, maxcol) 0
6187
6188 #endif /* not HAVE_WINDOW_SYSTEM */
6189
6190
6191
6192 \f
6193 /***********************************************************************
6194 Menu Bars
6195 ***********************************************************************/
6196
6197
6198 /* Prepare for redisplay by updating menu-bar item lists when
6199 appropriate. This can call eval. */
6200
6201 void
6202 prepare_menu_bars ()
6203 {
6204 int all_windows;
6205 struct gcpro gcpro1, gcpro2;
6206 struct frame *f;
6207 struct frame *tooltip_frame;
6208
6209 #ifdef HAVE_X_WINDOWS
6210 tooltip_frame = tip_frame;
6211 #else
6212 tooltip_frame = NULL;
6213 #endif
6214
6215 /* Update all frame titles based on their buffer names, etc. We do
6216 this before the menu bars so that the buffer-menu will show the
6217 up-to-date frame titles. */
6218 #ifdef HAVE_WINDOW_SYSTEM
6219 if (windows_or_buffers_changed || update_mode_lines)
6220 {
6221 Lisp_Object tail, frame;
6222
6223 FOR_EACH_FRAME (tail, frame)
6224 {
6225 f = XFRAME (frame);
6226 if (f != tooltip_frame
6227 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
6228 x_consider_frame_title (frame);
6229 }
6230 }
6231 #endif /* HAVE_WINDOW_SYSTEM */
6232
6233 /* Update the menu bar item lists, if appropriate. This has to be
6234 done before any actual redisplay or generation of display lines. */
6235 all_windows = (update_mode_lines
6236 || buffer_shared > 1
6237 || windows_or_buffers_changed);
6238 if (all_windows)
6239 {
6240 Lisp_Object tail, frame;
6241 int count = specpdl_ptr - specpdl;
6242
6243 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6244
6245 FOR_EACH_FRAME (tail, frame)
6246 {
6247 f = XFRAME (frame);
6248
6249 /* Ignore tooltip frame. */
6250 if (f == tooltip_frame)
6251 continue;
6252
6253 /* If a window on this frame changed size, report that to
6254 the user and clear the size-change flag. */
6255 if (FRAME_WINDOW_SIZES_CHANGED (f))
6256 {
6257 Lisp_Object functions;
6258
6259 /* Clear flag first in case we get an error below. */
6260 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
6261 functions = Vwindow_size_change_functions;
6262 GCPRO2 (tail, functions);
6263
6264 while (CONSP (functions))
6265 {
6266 call1 (XCAR (functions), frame);
6267 functions = XCDR (functions);
6268 }
6269 UNGCPRO;
6270 }
6271
6272 GCPRO1 (tail);
6273 update_menu_bar (f, 0);
6274 #ifdef HAVE_WINDOW_SYSTEM
6275 update_tool_bar (f, 0);
6276 #endif
6277 UNGCPRO;
6278 }
6279
6280 unbind_to (count, Qnil);
6281 }
6282 else
6283 {
6284 struct frame *sf = SELECTED_FRAME ();
6285 update_menu_bar (sf, 1);
6286 #ifdef HAVE_WINDOW_SYSTEM
6287 update_tool_bar (sf, 1);
6288 #endif
6289 }
6290
6291 /* Motif needs this. See comment in xmenu.c. Turn it off when
6292 pending_menu_activation is not defined. */
6293 #ifdef USE_X_TOOLKIT
6294 pending_menu_activation = 0;
6295 #endif
6296 }
6297
6298
6299 /* Update the menu bar item list for frame F. This has to be done
6300 before we start to fill in any display lines, because it can call
6301 eval.
6302
6303 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
6304
6305 static void
6306 update_menu_bar (f, save_match_data)
6307 struct frame *f;
6308 int save_match_data;
6309 {
6310 Lisp_Object window;
6311 register struct window *w;
6312
6313 window = FRAME_SELECTED_WINDOW (f);
6314 w = XWINDOW (window);
6315
6316 if (update_mode_lines)
6317 w->update_mode_line = Qt;
6318
6319 if (FRAME_WINDOW_P (f)
6320 ?
6321 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
6322 FRAME_EXTERNAL_MENU_BAR (f)
6323 #else
6324 FRAME_MENU_BAR_LINES (f) > 0
6325 #endif
6326 : FRAME_MENU_BAR_LINES (f) > 0)
6327 {
6328 /* If the user has switched buffers or windows, we need to
6329 recompute to reflect the new bindings. But we'll
6330 recompute when update_mode_lines is set too; that means
6331 that people can use force-mode-line-update to request
6332 that the menu bar be recomputed. The adverse effect on
6333 the rest of the redisplay algorithm is about the same as
6334 windows_or_buffers_changed anyway. */
6335 if (windows_or_buffers_changed
6336 || !NILP (w->update_mode_line)
6337 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
6338 < BUF_MODIFF (XBUFFER (w->buffer)))
6339 != !NILP (w->last_had_star))
6340 || ((!NILP (Vtransient_mark_mode)
6341 && !NILP (XBUFFER (w->buffer)->mark_active))
6342 != !NILP (w->region_showing)))
6343 {
6344 struct buffer *prev = current_buffer;
6345 int count = specpdl_ptr - specpdl;
6346
6347 set_buffer_internal_1 (XBUFFER (w->buffer));
6348 if (save_match_data)
6349 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6350 if (NILP (Voverriding_local_map_menu_flag))
6351 {
6352 specbind (Qoverriding_terminal_local_map, Qnil);
6353 specbind (Qoverriding_local_map, Qnil);
6354 }
6355
6356 /* Run the Lucid hook. */
6357 call1 (Vrun_hooks, Qactivate_menubar_hook);
6358
6359 /* If it has changed current-menubar from previous value,
6360 really recompute the menu-bar from the value. */
6361 if (! NILP (Vlucid_menu_bar_dirty_flag))
6362 call0 (Qrecompute_lucid_menubar);
6363
6364 safe_run_hooks (Qmenu_bar_update_hook);
6365 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
6366
6367 /* Redisplay the menu bar in case we changed it. */
6368 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
6369 if (FRAME_WINDOW_P (f))
6370 set_frame_menubar (f, 0, 0);
6371 else
6372 /* On a terminal screen, the menu bar is an ordinary screen
6373 line, and this makes it get updated. */
6374 w->update_mode_line = Qt;
6375 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
6376 /* In the non-toolkit version, the menu bar is an ordinary screen
6377 line, and this makes it get updated. */
6378 w->update_mode_line = Qt;
6379 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
6380
6381 unbind_to (count, Qnil);
6382 set_buffer_internal_1 (prev);
6383 }
6384 }
6385 }
6386
6387
6388 \f
6389 /***********************************************************************
6390 Tool-bars
6391 ***********************************************************************/
6392
6393 #ifdef HAVE_WINDOW_SYSTEM
6394
6395 /* Update the tool-bar item list for frame F. This has to be done
6396 before we start to fill in any display lines. Called from
6397 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
6398 and restore it here. */
6399
6400 static void
6401 update_tool_bar (f, save_match_data)
6402 struct frame *f;
6403 int save_match_data;
6404 {
6405 if (WINDOWP (f->tool_bar_window)
6406 && XFASTINT (XWINDOW (f->tool_bar_window)->height) > 0)
6407 {
6408 Lisp_Object window;
6409 struct window *w;
6410
6411 window = FRAME_SELECTED_WINDOW (f);
6412 w = XWINDOW (window);
6413
6414 /* If the user has switched buffers or windows, we need to
6415 recompute to reflect the new bindings. But we'll
6416 recompute when update_mode_lines is set too; that means
6417 that people can use force-mode-line-update to request
6418 that the menu bar be recomputed. The adverse effect on
6419 the rest of the redisplay algorithm is about the same as
6420 windows_or_buffers_changed anyway. */
6421 if (windows_or_buffers_changed
6422 || !NILP (w->update_mode_line)
6423 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
6424 < BUF_MODIFF (XBUFFER (w->buffer)))
6425 != !NILP (w->last_had_star))
6426 || ((!NILP (Vtransient_mark_mode)
6427 && !NILP (XBUFFER (w->buffer)->mark_active))
6428 != !NILP (w->region_showing)))
6429 {
6430 struct buffer *prev = current_buffer;
6431 int count = specpdl_ptr - specpdl;
6432
6433 /* Set current_buffer to the buffer of the selected
6434 window of the frame, so that we get the right local
6435 keymaps. */
6436 set_buffer_internal_1 (XBUFFER (w->buffer));
6437
6438 /* Save match data, if we must. */
6439 if (save_match_data)
6440 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6441
6442 /* Make sure that we don't accidentally use bogus keymaps. */
6443 if (NILP (Voverriding_local_map_menu_flag))
6444 {
6445 specbind (Qoverriding_terminal_local_map, Qnil);
6446 specbind (Qoverriding_local_map, Qnil);
6447 }
6448
6449 /* Build desired tool-bar items from keymaps. */
6450 f->desired_tool_bar_items
6451 = tool_bar_items (f->desired_tool_bar_items,
6452 &f->n_desired_tool_bar_items);
6453
6454 /* Redisplay the tool-bar in case we changed it. */
6455 w->update_mode_line = Qt;
6456
6457 unbind_to (count, Qnil);
6458 set_buffer_internal_1 (prev);
6459 }
6460 }
6461 }
6462
6463
6464 /* Set F->desired_tool_bar_string to a Lisp string representing frame
6465 F's desired tool-bar contents. F->desired_tool_bar_items must have
6466 been set up previously by calling prepare_menu_bars. */
6467
6468 static void
6469 build_desired_tool_bar_string (f)
6470 struct frame *f;
6471 {
6472 int i, size, size_needed, string_idx;
6473 struct gcpro gcpro1, gcpro2, gcpro3;
6474 Lisp_Object image, plist, props;
6475
6476 image = plist = props = Qnil;
6477 GCPRO3 (image, plist, props);
6478
6479 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
6480 Otherwise, make a new string. */
6481
6482 /* The size of the string we might be able to reuse. */
6483 size = (STRINGP (f->desired_tool_bar_string)
6484 ? XSTRING (f->desired_tool_bar_string)->size
6485 : 0);
6486
6487 /* Each image in the string we build is preceded by a space,
6488 and there is a space at the end. */
6489 size_needed = f->n_desired_tool_bar_items + 1;
6490
6491 /* Reuse f->desired_tool_bar_string, if possible. */
6492 if (size < size_needed)
6493 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
6494 make_number (' '));
6495 else
6496 {
6497 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
6498 Fremove_text_properties (make_number (0), make_number (size),
6499 props, f->desired_tool_bar_string);
6500 }
6501
6502 /* Put a `display' property on the string for the images to display,
6503 put a `menu_item' property on tool-bar items with a value that
6504 is the index of the item in F's tool-bar item vector. */
6505 for (i = 0, string_idx = 0;
6506 i < f->n_desired_tool_bar_items;
6507 ++i, string_idx += 1)
6508 {
6509 #define PROP(IDX) \
6510 (XVECTOR (f->desired_tool_bar_items) \
6511 ->contents[i * TOOL_BAR_ITEM_NSLOTS + (IDX)])
6512
6513 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
6514 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
6515 int margin, relief;
6516 extern Lisp_Object QCrelief, QCmargin, QCalgorithm, Qimage;
6517 extern Lisp_Object Qlaplace;
6518
6519 /* If image is a vector, choose the image according to the
6520 button state. */
6521 image = PROP (TOOL_BAR_ITEM_IMAGES);
6522 if (VECTORP (image))
6523 {
6524 enum tool_bar_item_image idx;
6525
6526 if (enabled_p)
6527 idx = (selected_p
6528 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
6529 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
6530 else
6531 idx = (selected_p
6532 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
6533 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
6534
6535 xassert (XVECTOR (image)->size >= idx);
6536 image = XVECTOR (image)->contents[idx];
6537 }
6538
6539 /* Ignore invalid image specifications. */
6540 if (!valid_image_p (image))
6541 continue;
6542
6543 /* Display the tool-bar button pressed, or depressed. */
6544 plist = Fcopy_sequence (XCDR (image));
6545
6546 /* Compute margin and relief to draw. */
6547 relief = tool_bar_button_relief > 0 ? tool_bar_button_relief : 3;
6548 margin = relief + max (0, tool_bar_button_margin);
6549
6550 if (auto_raise_tool_bar_buttons_p)
6551 {
6552 /* Add a `:relief' property to the image spec if the item is
6553 selected. */
6554 if (selected_p)
6555 {
6556 plist = Fplist_put (plist, QCrelief, make_number (-relief));
6557 margin -= relief;
6558 }
6559 }
6560 else
6561 {
6562 /* If image is selected, display it pressed, i.e. with a
6563 negative relief. If it's not selected, display it with a
6564 raised relief. */
6565 plist = Fplist_put (plist, QCrelief,
6566 (selected_p
6567 ? make_number (-relief)
6568 : make_number (relief)));
6569 margin -= relief;
6570 }
6571
6572 /* Put a margin around the image. */
6573 if (margin)
6574 plist = Fplist_put (plist, QCmargin, make_number (margin));
6575
6576 /* If button is not enabled, make the image appear disabled by
6577 applying an appropriate algorithm to it. */
6578 if (!enabled_p)
6579 plist = Fplist_put (plist, QCalgorithm, Qlaplace);
6580
6581 /* Put a `display' text property on the string for the image to
6582 display. Put a `menu-item' property on the string that gives
6583 the start of this item's properties in the tool-bar items
6584 vector. */
6585 image = Fcons (Qimage, plist);
6586 props = list4 (Qdisplay, image,
6587 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS)),
6588 Fadd_text_properties (make_number (string_idx),
6589 make_number (string_idx + 1),
6590 props, f->desired_tool_bar_string);
6591 #undef PROP
6592 }
6593
6594 UNGCPRO;
6595 }
6596
6597
6598 /* Display one line of the tool-bar of frame IT->f. */
6599
6600 static void
6601 display_tool_bar_line (it)
6602 struct it *it;
6603 {
6604 struct glyph_row *row = it->glyph_row;
6605 int max_x = it->last_visible_x;
6606 struct glyph *last;
6607
6608 prepare_desired_row (row);
6609 row->y = it->current_y;
6610
6611 while (it->current_x < max_x)
6612 {
6613 int x_before, x, n_glyphs_before, i, nglyphs;
6614
6615 /* Get the next display element. */
6616 if (!get_next_display_element (it))
6617 break;
6618
6619 /* Produce glyphs. */
6620 x_before = it->current_x;
6621 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
6622 PRODUCE_GLYPHS (it);
6623
6624 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
6625 i = 0;
6626 x = x_before;
6627 while (i < nglyphs)
6628 {
6629 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
6630
6631 if (x + glyph->pixel_width > max_x)
6632 {
6633 /* Glyph doesn't fit on line. */
6634 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
6635 it->current_x = x;
6636 goto out;
6637 }
6638
6639 ++it->hpos;
6640 x += glyph->pixel_width;
6641 ++i;
6642 }
6643
6644 /* Stop at line ends. */
6645 if (ITERATOR_AT_END_OF_LINE_P (it))
6646 break;
6647
6648 set_iterator_to_next (it);
6649 }
6650
6651 out:;
6652
6653 row->displays_text_p = row->used[TEXT_AREA] != 0;
6654 extend_face_to_end_of_line (it);
6655 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
6656 last->right_box_line_p = 1;
6657 compute_line_metrics (it);
6658
6659 /* If line is empty, make it occupy the rest of the tool-bar. */
6660 if (!row->displays_text_p)
6661 {
6662 row->height = row->phys_height = it->last_visible_y - row->y;
6663 row->ascent = row->phys_ascent = 0;
6664 }
6665
6666 row->full_width_p = 1;
6667 row->continued_p = 0;
6668 row->truncated_on_left_p = 0;
6669 row->truncated_on_right_p = 0;
6670
6671 it->current_x = it->hpos = 0;
6672 it->current_y += row->height;
6673 ++it->vpos;
6674 ++it->glyph_row;
6675 }
6676
6677
6678 /* Value is the number of screen lines needed to make all tool-bar
6679 items of frame F visible. */
6680
6681 static int
6682 tool_bar_lines_needed (f)
6683 struct frame *f;
6684 {
6685 struct window *w = XWINDOW (f->tool_bar_window);
6686 struct it it;
6687
6688 /* Initialize an iterator for iteration over
6689 F->desired_tool_bar_string in the tool-bar window of frame F. */
6690 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
6691 it.first_visible_x = 0;
6692 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
6693 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
6694
6695 while (!ITERATOR_AT_END_P (&it))
6696 {
6697 it.glyph_row = w->desired_matrix->rows;
6698 clear_glyph_row (it.glyph_row);
6699 display_tool_bar_line (&it);
6700 }
6701
6702 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
6703 }
6704
6705
6706 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
6707 height should be changed. */
6708
6709 static int
6710 redisplay_tool_bar (f)
6711 struct frame *f;
6712 {
6713 struct window *w;
6714 struct it it;
6715 struct glyph_row *row;
6716 int change_height_p = 0;
6717
6718 /* If frame hasn't a tool-bar window or if it is zero-height, don't
6719 do anything. This means you must start with tool-bar-lines
6720 non-zero to get the auto-sizing effect. Or in other words, you
6721 can turn off tool-bars by specifying tool-bar-lines zero. */
6722 if (!WINDOWP (f->tool_bar_window)
6723 || (w = XWINDOW (f->tool_bar_window),
6724 XFASTINT (w->height) == 0))
6725 return 0;
6726
6727 /* Set up an iterator for the tool-bar window. */
6728 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
6729 it.first_visible_x = 0;
6730 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
6731 row = it.glyph_row;
6732
6733 /* Build a string that represents the contents of the tool-bar. */
6734 build_desired_tool_bar_string (f);
6735 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
6736
6737 /* Display as many lines as needed to display all tool-bar items. */
6738 while (it.current_y < it.last_visible_y)
6739 display_tool_bar_line (&it);
6740
6741 /* It doesn't make much sense to try scrolling in the tool-bar
6742 window, so don't do it. */
6743 w->desired_matrix->no_scrolling_p = 1;
6744 w->must_be_updated_p = 1;
6745
6746 if (auto_resize_tool_bars_p)
6747 {
6748 int nlines;
6749
6750 /* If there are blank lines at the end, except for a partially
6751 visible blank line at the end that is smaller than
6752 CANON_Y_UNIT, change the tool-bar's height. */
6753 row = it.glyph_row - 1;
6754 if (!row->displays_text_p
6755 && row->height >= CANON_Y_UNIT (f))
6756 change_height_p = 1;
6757
6758 /* If row displays tool-bar items, but is partially visible,
6759 change the tool-bar's height. */
6760 if (row->displays_text_p
6761 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
6762 change_height_p = 1;
6763
6764 /* Resize windows as needed by changing the `tool-bar-lines'
6765 frame parameter. */
6766 if (change_height_p
6767 && (nlines = tool_bar_lines_needed (f),
6768 nlines != XFASTINT (w->height)))
6769 {
6770 extern Lisp_Object Qtool_bar_lines;
6771 Lisp_Object frame;
6772
6773 XSETFRAME (frame, f);
6774 clear_glyph_matrix (w->desired_matrix);
6775 Fmodify_frame_parameters (frame,
6776 Fcons (Fcons (Qtool_bar_lines,
6777 make_number (nlines)),
6778 Qnil));
6779 fonts_changed_p = 1;
6780 }
6781 }
6782
6783 return change_height_p;
6784 }
6785
6786
6787 /* Get information about the tool-bar item which is displayed in GLYPH
6788 on frame F. Return in *PROP_IDX the index where tool-bar item
6789 properties start in F->current_tool_bar_items. Value is zero if
6790 GLYPH doesn't display a tool-bar item. */
6791
6792 int
6793 tool_bar_item_info (f, glyph, prop_idx)
6794 struct frame *f;
6795 struct glyph *glyph;
6796 int *prop_idx;
6797 {
6798 Lisp_Object prop;
6799 int success_p;
6800
6801 /* Get the text property `menu-item' at pos. The value of that
6802 property is the start index of this item's properties in
6803 F->current_tool_bar_items. */
6804 prop = Fget_text_property (make_number (glyph->charpos),
6805 Qmenu_item, f->current_tool_bar_string);
6806 if (INTEGERP (prop))
6807 {
6808 *prop_idx = XINT (prop);
6809 success_p = 1;
6810 }
6811 else
6812 success_p = 0;
6813
6814 return success_p;
6815 }
6816
6817 #endif /* HAVE_WINDOW_SYSTEM */
6818
6819
6820 \f
6821 /************************************************************************
6822 Horizontal scrolling
6823 ************************************************************************/
6824
6825 static int hscroll_window_tree P_ ((Lisp_Object));
6826 static int hscroll_windows P_ ((Lisp_Object));
6827
6828 /* For all leaf windows in the window tree rooted at WINDOW, set their
6829 hscroll value so that PT is (i) visible in the window, and (ii) so
6830 that it is not within a certain margin at the window's left and
6831 right border. Value is non-zero if any window's hscroll has been
6832 changed. */
6833
6834 static int
6835 hscroll_window_tree (window)
6836 Lisp_Object window;
6837 {
6838 int hscrolled_p = 0;
6839
6840 while (WINDOWP (window))
6841 {
6842 struct window *w = XWINDOW (window);
6843
6844 if (WINDOWP (w->hchild))
6845 hscrolled_p |= hscroll_window_tree (w->hchild);
6846 else if (WINDOWP (w->vchild))
6847 hscrolled_p |= hscroll_window_tree (w->vchild);
6848 else if (w->cursor.vpos >= 0)
6849 {
6850 int hscroll_margin, text_area_x, text_area_y;
6851 int text_area_width, text_area_height;
6852 struct glyph_row *current_cursor_row
6853 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
6854 struct glyph_row *desired_cursor_row
6855 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
6856 struct glyph_row *cursor_row
6857 = (desired_cursor_row->enabled_p
6858 ? desired_cursor_row
6859 : current_cursor_row);
6860
6861 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
6862 &text_area_width, &text_area_height);
6863
6864 /* Scroll when cursor is inside this scroll margin. */
6865 hscroll_margin = 5 * CANON_X_UNIT (XFRAME (w->frame));
6866
6867 if ((XFASTINT (w->hscroll)
6868 && w->cursor.x < hscroll_margin)
6869 || (cursor_row->enabled_p
6870 && cursor_row->truncated_on_right_p
6871 && (w->cursor.x > text_area_width - hscroll_margin)))
6872 {
6873 struct it it;
6874 int hscroll;
6875 struct buffer *saved_current_buffer;
6876 int pt;
6877
6878 /* Find point in a display of infinite width. */
6879 saved_current_buffer = current_buffer;
6880 current_buffer = XBUFFER (w->buffer);
6881
6882 if (w == XWINDOW (selected_window))
6883 pt = BUF_PT (current_buffer);
6884 else
6885 {
6886 pt = marker_position (w->pointm);
6887 pt = max (BEGV, pt);
6888 pt = min (ZV, pt);
6889 }
6890
6891 /* Move iterator to pt starting at cursor_row->start in
6892 a line with infinite width. */
6893 init_to_row_start (&it, w, cursor_row);
6894 it.last_visible_x = INFINITY;
6895 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
6896 current_buffer = saved_current_buffer;
6897
6898 /* Center cursor in window. */
6899 hscroll = (max (0, it.current_x - text_area_width / 2)
6900 / CANON_X_UNIT (it.f));
6901
6902 /* Don't call Fset_window_hscroll if value hasn't
6903 changed because it will prevent redisplay
6904 optimizations. */
6905 if (XFASTINT (w->hscroll) != hscroll)
6906 {
6907 Fset_window_hscroll (window, make_number (hscroll));
6908 hscrolled_p = 1;
6909 }
6910 }
6911 }
6912
6913 window = w->next;
6914 }
6915
6916 /* Value is non-zero if hscroll of any leaf window has been changed. */
6917 return hscrolled_p;
6918 }
6919
6920
6921 /* Set hscroll so that cursor is visible and not inside horizontal
6922 scroll margins for all windows in the tree rooted at WINDOW. See
6923 also hscroll_window_tree above. Value is non-zero if any window's
6924 hscroll has been changed. If it has, desired matrices on the frame
6925 of WINDOW are cleared. */
6926
6927 static int
6928 hscroll_windows (window)
6929 Lisp_Object window;
6930 {
6931 int hscrolled_p;
6932
6933 if (automatic_hscrolling_p)
6934 {
6935 hscrolled_p = hscroll_window_tree (window);
6936 if (hscrolled_p)
6937 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
6938 }
6939 else
6940 hscrolled_p = 0;
6941 return hscrolled_p;
6942 }
6943
6944
6945 \f
6946 /************************************************************************
6947 Redisplay
6948 ************************************************************************/
6949
6950 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
6951 to a non-zero value. This is sometimes handy to have in a debugger
6952 session. */
6953
6954 #if GLYPH_DEBUG
6955
6956 /* First and last unchanged row for try_window_id. */
6957
6958 int debug_first_unchanged_at_end_vpos;
6959 int debug_last_unchanged_at_beg_vpos;
6960
6961 /* Delta vpos and y. */
6962
6963 int debug_dvpos, debug_dy;
6964
6965 /* Delta in characters and bytes for try_window_id. */
6966
6967 int debug_delta, debug_delta_bytes;
6968
6969 /* Values of window_end_pos and window_end_vpos at the end of
6970 try_window_id. */
6971
6972 int debug_end_pos, debug_end_vpos;
6973
6974 /* Append a string to W->desired_matrix->method. FMT is a printf
6975 format string. A1...A9 are a supplement for a variable-length
6976 argument list. If trace_redisplay_p is non-zero also printf the
6977 resulting string to stderr. */
6978
6979 static void
6980 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
6981 struct window *w;
6982 char *fmt;
6983 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
6984 {
6985 char buffer[512];
6986 char *method = w->desired_matrix->method;
6987 int len = strlen (method);
6988 int size = sizeof w->desired_matrix->method;
6989 int remaining = size - len - 1;
6990
6991 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
6992 if (len && remaining)
6993 {
6994 method[len] = '|';
6995 --remaining, ++len;
6996 }
6997
6998 strncpy (method + len, buffer, remaining);
6999
7000 if (trace_redisplay_p)
7001 fprintf (stderr, "%p (%s): %s\n",
7002 w,
7003 ((BUFFERP (w->buffer)
7004 && STRINGP (XBUFFER (w->buffer)->name))
7005 ? (char *) XSTRING (XBUFFER (w->buffer)->name)->data
7006 : "no buffer"),
7007 buffer);
7008 }
7009
7010 #endif /* GLYPH_DEBUG */
7011
7012
7013 /* This counter is used to clear the face cache every once in a while
7014 in redisplay_internal. It is incremented for each redisplay.
7015 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
7016 cleared. */
7017
7018 #define CLEAR_FACE_CACHE_COUNT 10000
7019 static int clear_face_cache_count;
7020
7021 /* Record the previous terminal frame we displayed. */
7022
7023 static struct frame *previous_terminal_frame;
7024
7025 /* Non-zero while redisplay_internal is in progress. */
7026
7027 int redisplaying_p;
7028
7029
7030 /* Value is non-zero if all changes in window W, which displays
7031 current_buffer, are in the text between START and END. START is a
7032 buffer position, END is given as a distance from Z. Used in
7033 redisplay_internal for display optimization. */
7034
7035 static INLINE int
7036 text_outside_line_unchanged_p (w, start, end)
7037 struct window *w;
7038 int start, end;
7039 {
7040 int unchanged_p = 1;
7041
7042 /* If text or overlays have changed, see where. */
7043 if (XFASTINT (w->last_modified) < MODIFF
7044 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
7045 {
7046 /* Gap in the line? */
7047 if (GPT < start || Z - GPT < end)
7048 unchanged_p = 0;
7049
7050 /* Changes start in front of the line, or end after it? */
7051 if (unchanged_p
7052 && (BEG_UNCHANGED < start - 1
7053 || END_UNCHANGED < end))
7054 unchanged_p = 0;
7055
7056 /* If selective display, can't optimize if changes start at the
7057 beginning of the line. */
7058 if (unchanged_p
7059 && INTEGERP (current_buffer->selective_display)
7060 && XINT (current_buffer->selective_display) > 0
7061 && (BEG_UNCHANGED < start || GPT <= start))
7062 unchanged_p = 0;
7063 }
7064
7065 return unchanged_p;
7066 }
7067
7068
7069 /* Do a frame update, taking possible shortcuts into account. This is
7070 the main external entry point for redisplay.
7071
7072 If the last redisplay displayed an echo area message and that message
7073 is no longer requested, we clear the echo area or bring back the
7074 mini-buffer if that is in use. */
7075
7076 void
7077 redisplay ()
7078 {
7079 redisplay_internal (0);
7080 }
7081
7082 /* Return 1 if point moved out of or into a composition. Otherwise
7083 return 0. PREV_BUF and PREV_PT are the last point buffer and
7084 position. BUF and PT are the current point buffer and position. */
7085
7086 int
7087 check_point_in_composition (prev_buf, prev_pt, buf, pt)
7088 struct buffer *prev_buf, *buf;
7089 int prev_pt, pt;
7090 {
7091 int start, end;
7092 Lisp_Object prop;
7093 Lisp_Object buffer;
7094
7095 XSETBUFFER (buffer, buf);
7096 /* Check a composition at the last point if point moved within the
7097 same buffer. */
7098 if (prev_buf == buf)
7099 {
7100 if (prev_pt == pt)
7101 /* Point didn't move. */
7102 return 0;
7103
7104 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
7105 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
7106 && COMPOSITION_VALID_P (start, end, prop)
7107 && start < prev_pt && end > prev_pt)
7108 /* The last point was within the composition. Return 1 iff
7109 point moved out of the composition. */
7110 return (pt <= start || pt >= end);
7111 }
7112
7113 /* Check a composition at the current point. */
7114 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
7115 && find_composition (pt, -1, &start, &end, &prop, buffer)
7116 && COMPOSITION_VALID_P (start, end, prop)
7117 && start < pt && end > pt);
7118 }
7119
7120 /* Reconsider the setting of B->clip_changed which is displayed
7121 in window W. */
7122
7123 static INLINE void
7124 reconsider_clip_changes (w, b)
7125 struct window *w;
7126 struct buffer *b;
7127 {
7128 if (b->prevent_redisplay_optimizations_p)
7129 b->clip_changed = 1;
7130 else if (b->clip_changed
7131 && !NILP (w->window_end_valid)
7132 && w->current_matrix->buffer == b
7133 && w->current_matrix->zv == BUF_ZV (b)
7134 && w->current_matrix->begv == BUF_BEGV (b))
7135 b->clip_changed = 0;
7136
7137 /* If display wasn't paused, and W is not a tool bar window, see if
7138 point has been moved into or out of a composition. In that case,
7139 we set b->clip_changed to 1 to force updating the screen. If
7140 b->clip_changed has already been set to 1, we can skip this
7141 check. */
7142 if (!b->clip_changed
7143 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
7144 {
7145 int pt;
7146
7147 if (w == XWINDOW (selected_window))
7148 pt = BUF_PT (current_buffer);
7149 else
7150 pt = marker_position (w->pointm);
7151
7152 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
7153 || pt != XINT (w->last_point))
7154 && check_point_in_composition (w->current_matrix->buffer,
7155 XINT (w->last_point),
7156 XBUFFER (w->buffer), pt))
7157 b->clip_changed = 1;
7158 }
7159 }
7160
7161
7162 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
7163 response to any user action; therefore, we should preserve the echo
7164 area. (Actually, our caller does that job.) Perhaps in the future
7165 avoid recentering windows if it is not necessary; currently that
7166 causes some problems. */
7167
7168 static void
7169 redisplay_internal (preserve_echo_area)
7170 int preserve_echo_area;
7171 {
7172 struct window *w = XWINDOW (selected_window);
7173 struct frame *f = XFRAME (w->frame);
7174 int pause;
7175 int must_finish = 0;
7176 struct text_pos tlbufpos, tlendpos;
7177 int number_of_visible_frames;
7178 int count;
7179 struct frame *sf = SELECTED_FRAME ();
7180
7181 /* Non-zero means redisplay has to consider all windows on all
7182 frames. Zero means, only selected_window is considered. */
7183 int consider_all_windows_p;
7184
7185 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
7186
7187 /* No redisplay if running in batch mode or frame is not yet fully
7188 initialized, or redisplay is explicitly turned off by setting
7189 Vinhibit_redisplay. */
7190 if (noninteractive
7191 || !NILP (Vinhibit_redisplay)
7192 || !f->glyphs_initialized_p)
7193 return;
7194
7195 /* The flag redisplay_performed_directly_p is set by
7196 direct_output_for_insert when it already did the whole screen
7197 update necessary. */
7198 if (redisplay_performed_directly_p)
7199 {
7200 redisplay_performed_directly_p = 0;
7201 if (!hscroll_windows (selected_window))
7202 return;
7203 }
7204
7205 #ifdef USE_X_TOOLKIT
7206 if (popup_activated ())
7207 return;
7208 #endif
7209
7210 /* I don't think this happens but let's be paranoid. */
7211 if (redisplaying_p)
7212 return;
7213
7214 /* Record a function that resets redisplaying_p to its old value
7215 when we leave this function. */
7216 count = specpdl_ptr - specpdl;
7217 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
7218 ++redisplaying_p;
7219
7220 retry:
7221
7222 reconsider_clip_changes (w, current_buffer);
7223
7224 /* If new fonts have been loaded that make a glyph matrix adjustment
7225 necessary, do it. */
7226 if (fonts_changed_p)
7227 {
7228 adjust_glyphs (NULL);
7229 ++windows_or_buffers_changed;
7230 fonts_changed_p = 0;
7231 }
7232
7233 if (! FRAME_WINDOW_P (sf)
7234 && previous_terminal_frame != sf)
7235 {
7236 /* Since frames on an ASCII terminal share the same display
7237 area, displaying a different frame means redisplay the whole
7238 thing. */
7239 windows_or_buffers_changed++;
7240 SET_FRAME_GARBAGED (sf);
7241 XSETFRAME (Vterminal_frame, sf);
7242 }
7243 previous_terminal_frame = sf;
7244
7245 /* Set the visible flags for all frames. Do this before checking
7246 for resized or garbaged frames; they want to know if their frames
7247 are visible. See the comment in frame.h for
7248 FRAME_SAMPLE_VISIBILITY. */
7249 {
7250 Lisp_Object tail, frame;
7251
7252 number_of_visible_frames = 0;
7253
7254 FOR_EACH_FRAME (tail, frame)
7255 {
7256 struct frame *f = XFRAME (frame);
7257
7258 FRAME_SAMPLE_VISIBILITY (f);
7259 if (FRAME_VISIBLE_P (f))
7260 ++number_of_visible_frames;
7261 clear_desired_matrices (f);
7262 }
7263 }
7264
7265 /* Notice any pending interrupt request to change frame size. */
7266 do_pending_window_change (1);
7267
7268 /* Clear frames marked as garbaged. */
7269 if (frame_garbaged)
7270 clear_garbaged_frames ();
7271
7272 /* Build menubar and tool-bar items. */
7273 prepare_menu_bars ();
7274
7275 if (windows_or_buffers_changed)
7276 update_mode_lines++;
7277
7278 /* Detect case that we need to write or remove a star in the mode line. */
7279 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
7280 {
7281 w->update_mode_line = Qt;
7282 if (buffer_shared > 1)
7283 update_mode_lines++;
7284 }
7285
7286 /* If %c is in the mode line, update it if needed. */
7287 if (!NILP (w->column_number_displayed)
7288 /* This alternative quickly identifies a common case
7289 where no change is needed. */
7290 && !(PT == XFASTINT (w->last_point)
7291 && XFASTINT (w->last_modified) >= MODIFF
7292 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
7293 && XFASTINT (w->column_number_displayed) != current_column ())
7294 w->update_mode_line = Qt;
7295
7296 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
7297
7298 /* The variable buffer_shared is set in redisplay_window and
7299 indicates that we redisplay a buffer in different windows. See
7300 there. */
7301 consider_all_windows_p = update_mode_lines || buffer_shared > 1;
7302
7303 /* If specs for an arrow have changed, do thorough redisplay
7304 to ensure we remove any arrow that should no longer exist. */
7305 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
7306 || ! EQ (Voverlay_arrow_string, last_arrow_string))
7307 consider_all_windows_p = windows_or_buffers_changed = 1;
7308
7309 /* Normally the message* functions will have already displayed and
7310 updated the echo area, but the frame may have been trashed, or
7311 the update may have been preempted, so display the echo area
7312 again here. Checking both message buffers captures the case that
7313 the echo area should be cleared. */
7314 if (!NILP (echo_area_buffer[0]) || !NILP (echo_area_buffer[1]))
7315 {
7316 int window_height_changed_p = echo_area_display (0);
7317 must_finish = 1;
7318
7319 if (fonts_changed_p)
7320 goto retry;
7321 else if (window_height_changed_p)
7322 {
7323 consider_all_windows_p = 1;
7324 ++update_mode_lines;
7325 ++windows_or_buffers_changed;
7326
7327 /* If window configuration was changed, frames may have been
7328 marked garbaged. Clear them or we will experience
7329 surprises wrt scrolling. */
7330 if (frame_garbaged)
7331 clear_garbaged_frames ();
7332 }
7333 }
7334 else if (w == XWINDOW (minibuf_window)
7335 && (current_buffer->clip_changed
7336 || XFASTINT (w->last_modified) < MODIFF
7337 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
7338 && resize_mini_window (w, 0))
7339 {
7340 /* Resized active mini-window to fit the size of what it is
7341 showing if its contents might have changed. */
7342 must_finish = 1;
7343 consider_all_windows_p = 1;
7344 ++windows_or_buffers_changed;
7345 ++update_mode_lines;
7346
7347 /* If window configuration was changed, frames may have been
7348 marked garbaged. Clear them or we will experience
7349 surprises wrt scrolling. */
7350 if (frame_garbaged)
7351 clear_garbaged_frames ();
7352 }
7353
7354
7355 /* If showing the region, and mark has changed, we must redisplay
7356 the whole window. The assignment to this_line_start_pos prevents
7357 the optimization directly below this if-statement. */
7358 if (((!NILP (Vtransient_mark_mode)
7359 && !NILP (XBUFFER (w->buffer)->mark_active))
7360 != !NILP (w->region_showing))
7361 || (!NILP (w->region_showing)
7362 && !EQ (w->region_showing,
7363 Fmarker_position (XBUFFER (w->buffer)->mark))))
7364 CHARPOS (this_line_start_pos) = 0;
7365
7366 /* Optimize the case that only the line containing the cursor in the
7367 selected window has changed. Variables starting with this_ are
7368 set in display_line and record information about the line
7369 containing the cursor. */
7370 tlbufpos = this_line_start_pos;
7371 tlendpos = this_line_end_pos;
7372 if (!consider_all_windows_p
7373 && CHARPOS (tlbufpos) > 0
7374 && NILP (w->update_mode_line)
7375 && !current_buffer->clip_changed
7376 && FRAME_VISIBLE_P (XFRAME (w->frame))
7377 && !FRAME_OBSCURED_P (XFRAME (w->frame))
7378 /* Make sure recorded data applies to current buffer, etc. */
7379 && this_line_buffer == current_buffer
7380 && current_buffer == XBUFFER (w->buffer)
7381 && NILP (w->force_start)
7382 /* Point must be on the line that we have info recorded about. */
7383 && PT >= CHARPOS (tlbufpos)
7384 && PT <= Z - CHARPOS (tlendpos)
7385 /* All text outside that line, including its final newline,
7386 must be unchanged */
7387 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
7388 CHARPOS (tlendpos)))
7389 {
7390 if (CHARPOS (tlbufpos) > BEGV
7391 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
7392 && (CHARPOS (tlbufpos) == ZV
7393 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
7394 /* Former continuation line has disappeared by becoming empty */
7395 goto cancel;
7396 else if (XFASTINT (w->last_modified) < MODIFF
7397 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
7398 || MINI_WINDOW_P (w))
7399 {
7400 /* We have to handle the case of continuation around a
7401 wide-column character (See the comment in indent.c around
7402 line 885).
7403
7404 For instance, in the following case:
7405
7406 -------- Insert --------
7407 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
7408 J_I_ ==> J_I_ `^^' are cursors.
7409 ^^ ^^
7410 -------- --------
7411
7412 As we have to redraw the line above, we should goto cancel. */
7413
7414 struct it it;
7415 int line_height_before = this_line_pixel_height;
7416
7417 /* Note that start_display will handle the case that the
7418 line starting at tlbufpos is a continuation lines. */
7419 start_display (&it, w, tlbufpos);
7420
7421 /* Implementation note: It this still necessary? */
7422 if (it.current_x != this_line_start_x)
7423 goto cancel;
7424
7425 TRACE ((stderr, "trying display optimization 1\n"));
7426 w->cursor.vpos = -1;
7427 overlay_arrow_seen = 0;
7428 it.vpos = this_line_vpos;
7429 it.current_y = this_line_y;
7430 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
7431 display_line (&it);
7432
7433 /* If line contains point, is not continued,
7434 and ends at same distance from eob as before, we win */
7435 if (w->cursor.vpos >= 0
7436 /* Line is not continued, otherwise this_line_start_pos
7437 would have been set to 0 in display_line. */
7438 && CHARPOS (this_line_start_pos)
7439 /* Line ends as before. */
7440 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
7441 /* Line has same height as before. Otherwise other lines
7442 would have to be shifted up or down. */
7443 && this_line_pixel_height == line_height_before)
7444 {
7445 /* If this is not the window's last line, we must adjust
7446 the charstarts of the lines below. */
7447 if (it.current_y < it.last_visible_y)
7448 {
7449 struct glyph_row *row
7450 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
7451 int delta, delta_bytes;
7452
7453 if (Z - CHARPOS (tlendpos) == ZV)
7454 {
7455 /* This line ends at end of (accessible part of)
7456 buffer. There is no newline to count. */
7457 delta = (Z
7458 - CHARPOS (tlendpos)
7459 - MATRIX_ROW_START_CHARPOS (row));
7460 delta_bytes = (Z_BYTE
7461 - BYTEPOS (tlendpos)
7462 - MATRIX_ROW_START_BYTEPOS (row));
7463 }
7464 else
7465 {
7466 /* This line ends in a newline. Must take
7467 account of the newline and the rest of the
7468 text that follows. */
7469 delta = (Z
7470 - CHARPOS (tlendpos)
7471 - MATRIX_ROW_START_CHARPOS (row));
7472 delta_bytes = (Z_BYTE
7473 - BYTEPOS (tlendpos)
7474 - MATRIX_ROW_START_BYTEPOS (row));
7475 }
7476
7477 increment_matrix_positions (w->current_matrix,
7478 this_line_vpos + 1,
7479 w->current_matrix->nrows,
7480 delta, delta_bytes);
7481 }
7482
7483 /* If this row displays text now but previously didn't,
7484 or vice versa, w->window_end_vpos may have to be
7485 adjusted. */
7486 if ((it.glyph_row - 1)->displays_text_p)
7487 {
7488 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
7489 XSETINT (w->window_end_vpos, this_line_vpos);
7490 }
7491 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
7492 && this_line_vpos > 0)
7493 XSETINT (w->window_end_vpos, this_line_vpos - 1);
7494 w->window_end_valid = Qnil;
7495
7496 /* Update hint: No need to try to scroll in update_window. */
7497 w->desired_matrix->no_scrolling_p = 1;
7498
7499 #if GLYPH_DEBUG
7500 *w->desired_matrix->method = 0;
7501 debug_method_add (w, "optimization 1");
7502 #endif
7503 goto update;
7504 }
7505 else
7506 goto cancel;
7507 }
7508 else if (/* Cursor position hasn't changed. */
7509 PT == XFASTINT (w->last_point)
7510 /* Make sure the cursor was last displayed
7511 in this window. Otherwise we have to reposition it. */
7512 && 0 <= w->cursor.vpos
7513 && XINT (w->height) > w->cursor.vpos)
7514 {
7515 if (!must_finish)
7516 {
7517 do_pending_window_change (1);
7518
7519 /* We used to always goto end_of_redisplay here, but this
7520 isn't enough if we have a blinking cursor. */
7521 if (w->cursor_off_p == w->last_cursor_off_p)
7522 goto end_of_redisplay;
7523 }
7524 goto update;
7525 }
7526 /* If highlighting the region, or if the cursor is in the echo area,
7527 then we can't just move the cursor. */
7528 else if (! (!NILP (Vtransient_mark_mode)
7529 && !NILP (current_buffer->mark_active))
7530 && (w == XWINDOW (current_buffer->last_selected_window)
7531 || highlight_nonselected_windows)
7532 && NILP (w->region_showing)
7533 && NILP (Vshow_trailing_whitespace)
7534 && !cursor_in_echo_area)
7535 {
7536 struct it it;
7537 struct glyph_row *row;
7538
7539 /* Skip from tlbufpos to PT and see where it is. Note that
7540 PT may be in invisible text. If so, we will end at the
7541 next visible position. */
7542 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
7543 NULL, DEFAULT_FACE_ID);
7544 it.current_x = this_line_start_x;
7545 it.current_y = this_line_y;
7546 it.vpos = this_line_vpos;
7547
7548 /* The call to move_it_to stops in front of PT, but
7549 moves over before-strings. */
7550 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
7551
7552 if (it.vpos == this_line_vpos
7553 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
7554 row->enabled_p))
7555 {
7556 xassert (this_line_vpos == it.vpos);
7557 xassert (this_line_y == it.current_y);
7558 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
7559 goto update;
7560 }
7561 else
7562 goto cancel;
7563 }
7564
7565 cancel:
7566 /* Text changed drastically or point moved off of line. */
7567 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
7568 }
7569
7570 CHARPOS (this_line_start_pos) = 0;
7571 consider_all_windows_p |= buffer_shared > 1;
7572 ++clear_face_cache_count;
7573
7574
7575 /* Build desired matrices. If consider_all_windows_p is non-zero,
7576 do it for all windows on all frames. Otherwise do it for
7577 selected_window, only. */
7578
7579 if (consider_all_windows_p)
7580 {
7581 Lisp_Object tail, frame;
7582
7583 /* Clear the face cache eventually. */
7584 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
7585 {
7586 clear_face_cache (0);
7587 clear_face_cache_count = 0;
7588 }
7589
7590 /* Recompute # windows showing selected buffer. This will be
7591 incremented each time such a window is displayed. */
7592 buffer_shared = 0;
7593
7594 FOR_EACH_FRAME (tail, frame)
7595 {
7596 struct frame *f = XFRAME (frame);
7597 if (FRAME_WINDOW_P (f) || f == sf)
7598 {
7599 /* Mark all the scroll bars to be removed; we'll redeem
7600 the ones we want when we redisplay their windows. */
7601 if (condemn_scroll_bars_hook)
7602 (*condemn_scroll_bars_hook) (f);
7603
7604 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
7605 redisplay_windows (FRAME_ROOT_WINDOW (f));
7606
7607 /* Any scroll bars which redisplay_windows should have
7608 nuked should now go away. */
7609 if (judge_scroll_bars_hook)
7610 (*judge_scroll_bars_hook) (f);
7611 }
7612 }
7613 }
7614 else if (FRAME_VISIBLE_P (sf)
7615 && !FRAME_OBSCURED_P (sf))
7616 redisplay_window (selected_window, 1);
7617
7618
7619 /* Compare desired and current matrices, perform output. */
7620
7621 update:
7622
7623 /* If fonts changed, display again. */
7624 if (fonts_changed_p)
7625 goto retry;
7626
7627 /* Prevent various kinds of signals during display update.
7628 stdio is not robust about handling signals,
7629 which can cause an apparent I/O error. */
7630 if (interrupt_input)
7631 unrequest_sigio ();
7632 stop_polling ();
7633
7634 if (consider_all_windows_p)
7635 {
7636 Lisp_Object tail;
7637 struct frame *f;
7638 int hscrolled_p;
7639
7640 pause = 0;
7641 hscrolled_p = 0;
7642
7643 /* See if we have to hscroll. */
7644 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
7645 if (FRAMEP (XCAR (tail)))
7646 {
7647 f = XFRAME (XCAR (tail));
7648
7649 if ((FRAME_WINDOW_P (f)
7650 || f == sf)
7651 && FRAME_VISIBLE_P (f)
7652 && !FRAME_OBSCURED_P (f)
7653 && hscroll_windows (f->root_window))
7654 hscrolled_p = 1;
7655 }
7656
7657 if (hscrolled_p)
7658 goto retry;
7659
7660 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
7661 {
7662 if (!FRAMEP (XCAR (tail)))
7663 continue;
7664
7665 f = XFRAME (XCAR (tail));
7666
7667 if ((FRAME_WINDOW_P (f) || f == sf)
7668 && FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
7669 {
7670 /* Mark all windows as to be updated. */
7671 set_window_update_flags (XWINDOW (f->root_window), 1);
7672 pause |= update_frame (f, 0, 0);
7673 if (!pause)
7674 {
7675 mark_window_display_accurate (f->root_window, 1);
7676 if (frame_up_to_date_hook != 0)
7677 (*frame_up_to_date_hook) (f);
7678 }
7679 }
7680 }
7681 }
7682 else
7683 {
7684 if (FRAME_VISIBLE_P (sf)
7685 && !FRAME_OBSCURED_P (sf))
7686 {
7687 if (hscroll_windows (selected_window))
7688 goto retry;
7689
7690 XWINDOW (selected_window)->must_be_updated_p = 1;
7691 pause = update_frame (sf, 0, 0);
7692 }
7693 else
7694 pause = 0;
7695
7696 /* We may have called echo_area_display at the top of this
7697 function. If the echo area is on another frame, that may
7698 have put text on a frame other than the selected one, so the
7699 above call to update_frame would not have caught it. Catch
7700 it here. */
7701 {
7702 Lisp_Object mini_window;
7703 struct frame *mini_frame;
7704
7705 mini_window = FRAME_MINIBUF_WINDOW (sf);
7706 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7707
7708 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
7709 {
7710 XWINDOW (mini_window)->must_be_updated_p = 1;
7711 pause |= update_frame (mini_frame, 0, 0);
7712 if (!pause && hscroll_windows (mini_window))
7713 goto retry;
7714 }
7715 }
7716 }
7717
7718 /* If display was paused because of pending input, make sure we do a
7719 thorough update the next time. */
7720 if (pause)
7721 {
7722 /* Prevent the optimization at the beginning of
7723 redisplay_internal that tries a single-line update of the
7724 line containing the cursor in the selected window. */
7725 CHARPOS (this_line_start_pos) = 0;
7726
7727 /* Let the overlay arrow be updated the next time. */
7728 if (!NILP (last_arrow_position))
7729 {
7730 last_arrow_position = Qt;
7731 last_arrow_string = Qt;
7732 }
7733
7734 /* If we pause after scrolling, some rows in the current
7735 matrices of some windows are not valid. */
7736 if (!WINDOW_FULL_WIDTH_P (w)
7737 && !FRAME_WINDOW_P (XFRAME (w->frame)))
7738 update_mode_lines = 1;
7739 }
7740
7741 /* Now text on frame agrees with windows, so put info into the
7742 windows for partial redisplay to follow. */
7743 if (!pause)
7744 {
7745 register struct buffer *b = XBUFFER (w->buffer);
7746
7747 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
7748 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
7749 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
7750 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
7751
7752 if (consider_all_windows_p)
7753 mark_window_display_accurate (FRAME_ROOT_WINDOW (sf), 1);
7754 else
7755 {
7756 XSETFASTINT (w->last_point, BUF_PT (b));
7757 w->last_cursor = w->cursor;
7758 w->last_cursor_off_p = w->cursor_off_p;
7759
7760 b->clip_changed = 0;
7761 b->prevent_redisplay_optimizations_p = 0;
7762 w->update_mode_line = Qnil;
7763 XSETFASTINT (w->last_modified, BUF_MODIFF (b));
7764 XSETFASTINT (w->last_overlay_modified, BUF_OVERLAY_MODIFF (b));
7765 w->last_had_star
7766 = (BUF_MODIFF (XBUFFER (w->buffer)) > BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7767 ? Qt : Qnil);
7768
7769 /* Record if we are showing a region, so can make sure to
7770 update it fully at next redisplay. */
7771 w->region_showing = (!NILP (Vtransient_mark_mode)
7772 && (w == XWINDOW (current_buffer->last_selected_window)
7773 || highlight_nonselected_windows)
7774 && !NILP (XBUFFER (w->buffer)->mark_active)
7775 ? Fmarker_position (XBUFFER (w->buffer)->mark)
7776 : Qnil);
7777
7778 w->window_end_valid = w->buffer;
7779 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
7780 last_arrow_string = Voverlay_arrow_string;
7781 if (frame_up_to_date_hook != 0)
7782 (*frame_up_to_date_hook) (sf);
7783
7784 w->current_matrix->buffer = b;
7785 w->current_matrix->begv = BUF_BEGV (b);
7786 w->current_matrix->zv = BUF_ZV (b);
7787 }
7788
7789 update_mode_lines = 0;
7790 windows_or_buffers_changed = 0;
7791 }
7792
7793 /* Start SIGIO interrupts coming again. Having them off during the
7794 code above makes it less likely one will discard output, but not
7795 impossible, since there might be stuff in the system buffer here.
7796 But it is much hairier to try to do anything about that. */
7797 if (interrupt_input)
7798 request_sigio ();
7799 start_polling ();
7800
7801 /* If a frame has become visible which was not before, redisplay
7802 again, so that we display it. Expose events for such a frame
7803 (which it gets when becoming visible) don't call the parts of
7804 redisplay constructing glyphs, so simply exposing a frame won't
7805 display anything in this case. So, we have to display these
7806 frames here explicitly. */
7807 if (!pause)
7808 {
7809 Lisp_Object tail, frame;
7810 int new_count = 0;
7811
7812 FOR_EACH_FRAME (tail, frame)
7813 {
7814 int this_is_visible = 0;
7815
7816 if (XFRAME (frame)->visible)
7817 this_is_visible = 1;
7818 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
7819 if (XFRAME (frame)->visible)
7820 this_is_visible = 1;
7821
7822 if (this_is_visible)
7823 new_count++;
7824 }
7825
7826 if (new_count != number_of_visible_frames)
7827 windows_or_buffers_changed++;
7828 }
7829
7830 /* Change frame size now if a change is pending. */
7831 do_pending_window_change (1);
7832
7833 /* If we just did a pending size change, or have additional
7834 visible frames, redisplay again. */
7835 if (windows_or_buffers_changed && !pause)
7836 goto retry;
7837
7838 end_of_redisplay:;
7839
7840 unbind_to (count, Qnil);
7841 }
7842
7843
7844 /* Redisplay, but leave alone any recent echo area message unless
7845 another message has been requested in its place.
7846
7847 This is useful in situations where you need to redisplay but no
7848 user action has occurred, making it inappropriate for the message
7849 area to be cleared. See tracking_off and
7850 wait_reading_process_input for examples of these situations. */
7851
7852 void
7853 redisplay_preserve_echo_area ()
7854 {
7855 if (!NILP (echo_area_buffer[1]))
7856 {
7857 /* We have a previously displayed message, but no current
7858 message. Redisplay the previous message. */
7859 display_last_displayed_message_p = 1;
7860 redisplay_internal (1);
7861 display_last_displayed_message_p = 0;
7862 }
7863 else
7864 redisplay_internal (1);
7865 }
7866
7867
7868 /* Function registered with record_unwind_protect in
7869 redisplay_internal. Clears the flag indicating that a redisplay is
7870 in progress. */
7871
7872 static Lisp_Object
7873 unwind_redisplay (old_redisplaying_p)
7874 Lisp_Object old_redisplaying_p;
7875 {
7876 redisplaying_p = XFASTINT (old_redisplaying_p);
7877 return Qnil;
7878 }
7879
7880
7881 /* Mark the display of windows in the window tree rooted at WINDOW as
7882 accurate or inaccurate. If FLAG is non-zero mark display of WINDOW
7883 as accurate. If FLAG is zero arrange for WINDOW to be redisplayed
7884 the next time redisplay_internal is called. */
7885
7886 void
7887 mark_window_display_accurate (window, accurate_p)
7888 Lisp_Object window;
7889 int accurate_p;
7890 {
7891 struct window *w;
7892
7893 for (; !NILP (window); window = w->next)
7894 {
7895 w = XWINDOW (window);
7896
7897 if (BUFFERP (w->buffer))
7898 {
7899 struct buffer *b = XBUFFER (w->buffer);
7900
7901 XSETFASTINT (w->last_modified,
7902 accurate_p ? BUF_MODIFF (b) : 0);
7903 XSETFASTINT (w->last_overlay_modified,
7904 accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
7905 w->last_had_star = (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b)
7906 ? Qt : Qnil);
7907
7908 #if 0 /* I don't think this is necessary because display_line does it.
7909 Let's check it. */
7910 /* Record if we are showing a region, so can make sure to
7911 update it fully at next redisplay. */
7912 w->region_showing
7913 = (!NILP (Vtransient_mark_mode)
7914 && (w == XWINDOW (current_buffer->last_selected_window)
7915 || highlight_nonselected_windows)
7916 && (!NILP (b->mark_active)
7917 ? Fmarker_position (b->mark)
7918 : Qnil));
7919 #endif
7920
7921 if (accurate_p)
7922 {
7923 b->clip_changed = 0;
7924 b->prevent_redisplay_optimizations_p = 0;
7925 w->current_matrix->buffer = b;
7926 w->current_matrix->begv = BUF_BEGV (b);
7927 w->current_matrix->zv = BUF_ZV (b);
7928 w->last_cursor = w->cursor;
7929 w->last_cursor_off_p = w->cursor_off_p;
7930 if (w == XWINDOW (selected_window))
7931 w->last_point = make_number (BUF_PT (b));
7932 else
7933 w->last_point = make_number (XMARKER (w->pointm)->charpos);
7934 }
7935 }
7936
7937 w->window_end_valid = w->buffer;
7938 w->update_mode_line = Qnil;
7939
7940 if (!NILP (w->vchild))
7941 mark_window_display_accurate (w->vchild, accurate_p);
7942 if (!NILP (w->hchild))
7943 mark_window_display_accurate (w->hchild, accurate_p);
7944 }
7945
7946 if (accurate_p)
7947 {
7948 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
7949 last_arrow_string = Voverlay_arrow_string;
7950 }
7951 else
7952 {
7953 /* Force a thorough redisplay the next time by setting
7954 last_arrow_position and last_arrow_string to t, which is
7955 unequal to any useful value of Voverlay_arrow_... */
7956 last_arrow_position = Qt;
7957 last_arrow_string = Qt;
7958 }
7959 }
7960
7961
7962 /* Return value in display table DP (Lisp_Char_Table *) for character
7963 C. Since a display table doesn't have any parent, we don't have to
7964 follow parent. Do not call this function directly but use the
7965 macro DISP_CHAR_VECTOR. */
7966
7967 Lisp_Object
7968 disp_char_vector (dp, c)
7969 struct Lisp_Char_Table *dp;
7970 int c;
7971 {
7972 int code[4], i;
7973 Lisp_Object val;
7974
7975 if (SINGLE_BYTE_CHAR_P (c))
7976 return (dp->contents[c]);
7977
7978 SPLIT_NON_ASCII_CHAR (c, code[0], code[1], code[2]);
7979 if (code[1] < 32)
7980 code[1] = -1;
7981 else if (code[2] < 32)
7982 code[2] = -1;
7983
7984 /* Here, the possible range of code[0] (== charset ID) is
7985 128..max_charset. Since the top level char table contains data
7986 for multibyte characters after 256th element, we must increment
7987 code[0] by 128 to get a correct index. */
7988 code[0] += 128;
7989 code[3] = -1; /* anchor */
7990
7991 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
7992 {
7993 val = dp->contents[code[i]];
7994 if (!SUB_CHAR_TABLE_P (val))
7995 return (NILP (val) ? dp->defalt : val);
7996 }
7997
7998 /* Here, val is a sub char table. We return the default value of
7999 it. */
8000 return (dp->defalt);
8001 }
8002
8003
8004 \f
8005 /***********************************************************************
8006 Window Redisplay
8007 ***********************************************************************/
8008
8009 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
8010
8011 static void
8012 redisplay_windows (window)
8013 Lisp_Object window;
8014 {
8015 while (!NILP (window))
8016 {
8017 struct window *w = XWINDOW (window);
8018
8019 if (!NILP (w->hchild))
8020 redisplay_windows (w->hchild);
8021 else if (!NILP (w->vchild))
8022 redisplay_windows (w->vchild);
8023 else
8024 redisplay_window (window, 0);
8025
8026 window = w->next;
8027 }
8028 }
8029
8030
8031 /* Set cursor position of W. PT is assumed to be displayed in ROW.
8032 DELTA is the number of bytes by which positions recorded in ROW
8033 differ from current buffer positions. */
8034
8035 void
8036 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
8037 struct window *w;
8038 struct glyph_row *row;
8039 struct glyph_matrix *matrix;
8040 int delta, delta_bytes, dy, dvpos;
8041 {
8042 struct glyph *glyph = row->glyphs[TEXT_AREA];
8043 struct glyph *end = glyph + row->used[TEXT_AREA];
8044 int x = row->x;
8045 int pt_old = PT - delta;
8046
8047 /* Skip over glyphs not having an object at the start of the row.
8048 These are special glyphs like truncation marks on terminal
8049 frames. */
8050 if (row->displays_text_p)
8051 while (glyph < end
8052 && INTEGERP (glyph->object)
8053 && glyph->charpos < 0)
8054 {
8055 x += glyph->pixel_width;
8056 ++glyph;
8057 }
8058
8059 while (glyph < end
8060 && !INTEGERP (glyph->object)
8061 && (!BUFFERP (glyph->object)
8062 || glyph->charpos < pt_old))
8063 {
8064 x += glyph->pixel_width;
8065 ++glyph;
8066 }
8067
8068 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
8069 w->cursor.x = x;
8070 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
8071 w->cursor.y = row->y + dy;
8072
8073 if (w == XWINDOW (selected_window))
8074 {
8075 if (!row->continued_p
8076 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
8077 && row->x == 0)
8078 {
8079 this_line_buffer = XBUFFER (w->buffer);
8080
8081 CHARPOS (this_line_start_pos)
8082 = MATRIX_ROW_START_CHARPOS (row) + delta;
8083 BYTEPOS (this_line_start_pos)
8084 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
8085
8086 CHARPOS (this_line_end_pos)
8087 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
8088 BYTEPOS (this_line_end_pos)
8089 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
8090
8091 this_line_y = w->cursor.y;
8092 this_line_pixel_height = row->height;
8093 this_line_vpos = w->cursor.vpos;
8094 this_line_start_x = row->x;
8095 }
8096 else
8097 CHARPOS (this_line_start_pos) = 0;
8098 }
8099 }
8100
8101
8102 /* Run window scroll functions, if any, for WINDOW with new window
8103 start STARTP. Sets the window start of WINDOW to that position.
8104
8105 We assume that the window's buffer is really current. */
8106
8107 static INLINE struct text_pos
8108 run_window_scroll_functions (window, startp)
8109 Lisp_Object window;
8110 struct text_pos startp;
8111 {
8112 struct window *w = XWINDOW (window);
8113 SET_MARKER_FROM_TEXT_POS (w->start, startp);
8114
8115 if (current_buffer != XBUFFER (w->buffer))
8116 abort ();
8117
8118 if (!NILP (Vwindow_scroll_functions))
8119 {
8120 run_hook_with_args_2 (Qwindow_scroll_functions, window,
8121 make_number (CHARPOS (startp)));
8122 SET_TEXT_POS_FROM_MARKER (startp, w->start);
8123 /* In case the hook functions switch buffers. */
8124 if (current_buffer != XBUFFER (w->buffer))
8125 set_buffer_internal_1 (XBUFFER (w->buffer));
8126 }
8127
8128 return startp;
8129 }
8130
8131
8132 /* Modify the desired matrix of window W and W->vscroll so that the
8133 line containing the cursor is fully visible. */
8134
8135 static void
8136 make_cursor_line_fully_visible (w)
8137 struct window *w;
8138 {
8139 struct glyph_matrix *matrix;
8140 struct glyph_row *row;
8141 int header_line_height;
8142
8143 /* It's not always possible to find the cursor, e.g, when a window
8144 is full of overlay strings. Don't do anything in that case. */
8145 if (w->cursor.vpos < 0)
8146 return;
8147
8148 matrix = w->desired_matrix;
8149 row = MATRIX_ROW (matrix, w->cursor.vpos);
8150
8151 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row)
8152 /* The row may be partially visible at the top because we
8153 already have chosen a vscroll to align the bottom of the
8154 row with the bottom of the window. This happens for rows
8155 taller than the window. */
8156 && row->y + row->height < window_box_height (w))
8157 {
8158 int dy = row->height - row->visible_height;
8159 w->vscroll = 0;
8160 w->cursor.y += dy;
8161 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
8162 }
8163 else if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)
8164 /* The row may be partially visible at the bottom because
8165 we chose a vscroll to align the row's top with the
8166 window's top. This happens for rows taller than the
8167 window. */
8168 && row->y > WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w))
8169 {
8170 int dy = - (row->height - row->visible_height);
8171 w->vscroll = dy;
8172 w->cursor.y += dy;
8173 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
8174 }
8175
8176 /* When we change the cursor y-position of the selected window,
8177 change this_line_y as well so that the display optimization for
8178 the cursor line of the selected window in redisplay_internal uses
8179 the correct y-position. */
8180 if (w == XWINDOW (selected_window))
8181 this_line_y = w->cursor.y;
8182 }
8183
8184
8185 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
8186 non-zero means only WINDOW is redisplayed in redisplay_internal.
8187 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
8188 in redisplay_window to bring a partially visible line into view in
8189 the case that only the cursor has moved.
8190
8191 Value is
8192
8193 1 if scrolling succeeded
8194
8195 0 if scrolling didn't find point.
8196
8197 -1 if new fonts have been loaded so that we must interrupt
8198 redisplay, adjust glyph matrices, and try again. */
8199
8200 static int
8201 try_scrolling (window, just_this_one_p, scroll_conservatively,
8202 scroll_step, temp_scroll_step)
8203 Lisp_Object window;
8204 int just_this_one_p;
8205 int scroll_conservatively, scroll_step;
8206 int temp_scroll_step;
8207 {
8208 struct window *w = XWINDOW (window);
8209 struct frame *f = XFRAME (w->frame);
8210 struct text_pos scroll_margin_pos;
8211 struct text_pos pos;
8212 struct text_pos startp;
8213 struct it it;
8214 Lisp_Object window_end;
8215 int this_scroll_margin;
8216 int dy = 0;
8217 int scroll_max;
8218 int line_height, rc;
8219 int amount_to_scroll = 0;
8220 Lisp_Object aggressive;
8221 int height;
8222
8223 #if GLYPH_DEBUG
8224 debug_method_add (w, "try_scrolling");
8225 #endif
8226
8227 SET_TEXT_POS_FROM_MARKER (startp, w->start);
8228
8229 /* Compute scroll margin height in pixels. We scroll when point is
8230 within this distance from the top or bottom of the window. */
8231 if (scroll_margin > 0)
8232 {
8233 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
8234 this_scroll_margin *= CANON_Y_UNIT (f);
8235 }
8236 else
8237 this_scroll_margin = 0;
8238
8239 /* Compute how much we should try to scroll maximally to bring point
8240 into view. */
8241 if (scroll_step)
8242 scroll_max = scroll_step;
8243 else if (scroll_conservatively)
8244 scroll_max = scroll_conservatively;
8245 else if (temp_scroll_step)
8246 scroll_max = temp_scroll_step;
8247 else if (NUMBERP (current_buffer->scroll_down_aggressively)
8248 || NUMBERP (current_buffer->scroll_up_aggressively))
8249 /* We're trying to scroll because of aggressive scrolling
8250 but no scroll_step is set. Choose an arbitrary one. Maybe
8251 there should be a variable for this. */
8252 scroll_max = 10;
8253 else
8254 scroll_max = 0;
8255 scroll_max *= CANON_Y_UNIT (f);
8256
8257 /* Decide whether we have to scroll down. Start at the window end
8258 and move this_scroll_margin up to find the position of the scroll
8259 margin. */
8260 window_end = Fwindow_end (window, Qt);
8261 CHARPOS (scroll_margin_pos) = XINT (window_end);
8262 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
8263 if (this_scroll_margin)
8264 {
8265 start_display (&it, w, scroll_margin_pos);
8266 move_it_vertically (&it, - this_scroll_margin);
8267 scroll_margin_pos = it.current.pos;
8268 }
8269
8270 if (PT >= CHARPOS (scroll_margin_pos))
8271 {
8272 int y0;
8273
8274 /* Point is in the scroll margin at the bottom of the window, or
8275 below. Compute a new window start that makes point visible. */
8276
8277 /* Compute the distance from the scroll margin to PT.
8278 Give up if the distance is greater than scroll_max. */
8279 start_display (&it, w, scroll_margin_pos);
8280 y0 = it.current_y;
8281 move_it_to (&it, PT, 0, it.last_visible_y, -1,
8282 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
8283 line_height = (it.max_ascent + it.max_descent
8284 ? it.max_ascent + it.max_descent
8285 : last_height);
8286 dy = it.current_y + line_height - y0;
8287 if (dy > scroll_max)
8288 return 0;
8289
8290 /* Move the window start down. If scrolling conservatively,
8291 move it just enough down to make point visible. If
8292 scroll_step is set, move it down by scroll_step. */
8293 start_display (&it, w, startp);
8294
8295 if (scroll_conservatively)
8296 amount_to_scroll = dy;
8297 else if (scroll_step || temp_scroll_step)
8298 amount_to_scroll = scroll_max;
8299 else
8300 {
8301 aggressive = current_buffer->scroll_down_aggressively;
8302 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
8303 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
8304 if (NUMBERP (aggressive))
8305 amount_to_scroll = XFLOATINT (aggressive) * height;
8306 }
8307
8308 if (amount_to_scroll <= 0)
8309 return 0;
8310
8311 move_it_vertically (&it, amount_to_scroll);
8312 startp = it.current.pos;
8313 }
8314 else
8315 {
8316 /* See if point is inside the scroll margin at the top of the
8317 window. */
8318 scroll_margin_pos = startp;
8319 if (this_scroll_margin)
8320 {
8321 start_display (&it, w, startp);
8322 move_it_vertically (&it, this_scroll_margin);
8323 scroll_margin_pos = it.current.pos;
8324 }
8325
8326 if (PT < CHARPOS (scroll_margin_pos))
8327 {
8328 /* Point is in the scroll margin at the top of the window or
8329 above what is displayed in the window. */
8330 int y0;
8331
8332 /* Compute the vertical distance from PT to the scroll
8333 margin position. Give up if distance is greater than
8334 scroll_max. */
8335 SET_TEXT_POS (pos, PT, PT_BYTE);
8336 start_display (&it, w, pos);
8337 y0 = it.current_y;
8338 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
8339 it.last_visible_y, -1,
8340 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
8341 dy = it.current_y - y0;
8342 if (dy > scroll_max)
8343 return 0;
8344
8345 /* Compute new window start. */
8346 start_display (&it, w, startp);
8347
8348 if (scroll_conservatively)
8349 amount_to_scroll = dy;
8350 else if (scroll_step || temp_scroll_step)
8351 amount_to_scroll = scroll_max;
8352 else
8353 {
8354 aggressive = current_buffer->scroll_up_aggressively;
8355 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
8356 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
8357 if (NUMBERP (aggressive))
8358 amount_to_scroll = XFLOATINT (aggressive) * height;
8359 }
8360
8361 if (amount_to_scroll <= 0)
8362 return 0;
8363
8364 move_it_vertically (&it, - amount_to_scroll);
8365 startp = it.current.pos;
8366 }
8367 }
8368
8369 /* Run window scroll functions. */
8370 startp = run_window_scroll_functions (window, startp);
8371
8372 /* Display the window. Give up if new fonts are loaded, or if point
8373 doesn't appear. */
8374 if (!try_window (window, startp))
8375 rc = -1;
8376 else if (w->cursor.vpos < 0)
8377 {
8378 clear_glyph_matrix (w->desired_matrix);
8379 rc = 0;
8380 }
8381 else
8382 {
8383 /* Maybe forget recorded base line for line number display. */
8384 if (!just_this_one_p
8385 || current_buffer->clip_changed
8386 || BEG_UNCHANGED < CHARPOS (startp))
8387 w->base_line_number = Qnil;
8388
8389 /* If cursor ends up on a partially visible line, shift display
8390 lines up or down. */
8391 make_cursor_line_fully_visible (w);
8392 rc = 1;
8393 }
8394
8395 return rc;
8396 }
8397
8398
8399 /* Compute a suitable window start for window W if display of W starts
8400 on a continuation line. Value is non-zero if a new window start
8401 was computed.
8402
8403 The new window start will be computed, based on W's width, starting
8404 from the start of the continued line. It is the start of the
8405 screen line with the minimum distance from the old start W->start. */
8406
8407 static int
8408 compute_window_start_on_continuation_line (w)
8409 struct window *w;
8410 {
8411 struct text_pos pos, start_pos;
8412 int window_start_changed_p = 0;
8413
8414 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
8415
8416 /* If window start is on a continuation line... Window start may be
8417 < BEGV in case there's invisible text at the start of the
8418 buffer (M-x rmail, for example). */
8419 if (CHARPOS (start_pos) > BEGV
8420 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
8421 {
8422 struct it it;
8423 struct glyph_row *row;
8424
8425 /* Handle the case that the window start is out of range. */
8426 if (CHARPOS (start_pos) < BEGV)
8427 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
8428 else if (CHARPOS (start_pos) > ZV)
8429 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
8430
8431 /* Find the start of the continued line. This should be fast
8432 because scan_buffer is fast (newline cache). */
8433 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
8434 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
8435 row, DEFAULT_FACE_ID);
8436 reseat_at_previous_visible_line_start (&it);
8437
8438 /* If the line start is "too far" away from the window start,
8439 say it takes too much time to compute a new window start. */
8440 if (CHARPOS (start_pos) - IT_CHARPOS (it)
8441 < XFASTINT (w->height) * XFASTINT (w->width))
8442 {
8443 int min_distance, distance;
8444
8445 /* Move forward by display lines to find the new window
8446 start. If window width was enlarged, the new start can
8447 be expected to be > the old start. If window width was
8448 decreased, the new window start will be < the old start.
8449 So, we're looking for the display line start with the
8450 minimum distance from the old window start. */
8451 pos = it.current.pos;
8452 min_distance = INFINITY;
8453 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
8454 distance < min_distance)
8455 {
8456 min_distance = distance;
8457 pos = it.current.pos;
8458 move_it_by_lines (&it, 1, 0);
8459 }
8460
8461 /* Set the window start there. */
8462 SET_MARKER_FROM_TEXT_POS (w->start, pos);
8463 window_start_changed_p = 1;
8464 }
8465 }
8466
8467 return window_start_changed_p;
8468 }
8469
8470
8471 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
8472 selected_window is redisplayed. */
8473
8474 static void
8475 redisplay_window (window, just_this_one_p)
8476 Lisp_Object window;
8477 int just_this_one_p;
8478 {
8479 struct window *w = XWINDOW (window);
8480 struct frame *f = XFRAME (w->frame);
8481 struct buffer *buffer = XBUFFER (w->buffer);
8482 struct buffer *old = current_buffer;
8483 struct text_pos lpoint, opoint, startp;
8484 int update_mode_line;
8485 int tem;
8486 struct it it;
8487 /* Record it now because it's overwritten. */
8488 int current_matrix_up_to_date_p = 0;
8489 int really_switched_buffer = 0;
8490 int temp_scroll_step = 0;
8491 int count = specpdl_ptr - specpdl;
8492
8493 SET_TEXT_POS (lpoint, PT, PT_BYTE);
8494 opoint = lpoint;
8495
8496 /* W must be a leaf window here. */
8497 xassert (!NILP (w->buffer));
8498 #if GLYPH_DEBUG
8499 *w->desired_matrix->method = 0;
8500 #endif
8501
8502 specbind (Qinhibit_point_motion_hooks, Qt);
8503
8504 reconsider_clip_changes (w, buffer);
8505
8506 /* Has the mode line to be updated? */
8507 update_mode_line = (!NILP (w->update_mode_line)
8508 || update_mode_lines
8509 || buffer->clip_changed);
8510
8511 if (MINI_WINDOW_P (w))
8512 {
8513 if (w == XWINDOW (echo_area_window)
8514 && !NILP (echo_area_buffer[0]))
8515 {
8516 if (update_mode_line)
8517 /* We may have to update a tty frame's menu bar or a
8518 tool-bar. Example `M-x C-h C-h C-g'. */
8519 goto finish_menu_bars;
8520 else
8521 /* We've already displayed the echo area glyphs in this window. */
8522 goto finish_scroll_bars;
8523 }
8524 else if (w != XWINDOW (minibuf_window))
8525 {
8526 /* W is a mini-buffer window, but it's not the currently
8527 active one, so clear it. */
8528 int yb = window_text_bottom_y (w);
8529 struct glyph_row *row;
8530 int y;
8531
8532 for (y = 0, row = w->desired_matrix->rows;
8533 y < yb;
8534 y += row->height, ++row)
8535 blank_row (w, row, y);
8536 goto finish_scroll_bars;
8537 }
8538 }
8539
8540 /* Otherwise set up data on this window; select its buffer and point
8541 value. */
8542 if (update_mode_line)
8543 {
8544 /* Really select the buffer, for the sake of buffer-local
8545 variables. */
8546 set_buffer_internal_1 (XBUFFER (w->buffer));
8547 really_switched_buffer = 1;
8548 }
8549 else
8550 set_buffer_temp (XBUFFER (w->buffer));
8551 SET_TEXT_POS (opoint, PT, PT_BYTE);
8552
8553 current_matrix_up_to_date_p
8554 = (!NILP (w->window_end_valid)
8555 && !current_buffer->clip_changed
8556 && XFASTINT (w->last_modified) >= MODIFF
8557 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
8558
8559 /* When windows_or_buffers_changed is non-zero, we can't rely on
8560 the window end being valid, so set it to nil there. */
8561 if (windows_or_buffers_changed)
8562 {
8563 /* If window starts on a continuation line, maybe adjust the
8564 window start in case the window's width changed. */
8565 if (XMARKER (w->start)->buffer == current_buffer)
8566 compute_window_start_on_continuation_line (w);
8567
8568 w->window_end_valid = Qnil;
8569 }
8570
8571 /* Some sanity checks. */
8572 CHECK_WINDOW_END (w);
8573 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
8574 abort ();
8575 if (BYTEPOS (opoint) < CHARPOS (opoint))
8576 abort ();
8577
8578 /* If %c is in mode line, update it if needed. */
8579 if (!NILP (w->column_number_displayed)
8580 /* This alternative quickly identifies a common case
8581 where no change is needed. */
8582 && !(PT == XFASTINT (w->last_point)
8583 && XFASTINT (w->last_modified) >= MODIFF
8584 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
8585 && XFASTINT (w->column_number_displayed) != current_column ())
8586 update_mode_line = 1;
8587
8588 /* Count number of windows showing the selected buffer. An indirect
8589 buffer counts as its base buffer. */
8590 if (!just_this_one_p)
8591 {
8592 struct buffer *current_base, *window_base;
8593 current_base = current_buffer;
8594 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
8595 if (current_base->base_buffer)
8596 current_base = current_base->base_buffer;
8597 if (window_base->base_buffer)
8598 window_base = window_base->base_buffer;
8599 if (current_base == window_base)
8600 buffer_shared++;
8601 }
8602
8603 /* Point refers normally to the selected window. For any other
8604 window, set up appropriate value. */
8605 if (!EQ (window, selected_window))
8606 {
8607 int new_pt = XMARKER (w->pointm)->charpos;
8608 int new_pt_byte = marker_byte_position (w->pointm);
8609 if (new_pt < BEGV)
8610 {
8611 new_pt = BEGV;
8612 new_pt_byte = BEGV_BYTE;
8613 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
8614 }
8615 else if (new_pt > (ZV - 1))
8616 {
8617 new_pt = ZV;
8618 new_pt_byte = ZV_BYTE;
8619 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
8620 }
8621
8622 /* We don't use SET_PT so that the point-motion hooks don't run. */
8623 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
8624 }
8625
8626 /* If any of the character widths specified in the display table
8627 have changed, invalidate the width run cache. It's true that
8628 this may be a bit late to catch such changes, but the rest of
8629 redisplay goes (non-fatally) haywire when the display table is
8630 changed, so why should we worry about doing any better? */
8631 if (current_buffer->width_run_cache)
8632 {
8633 struct Lisp_Char_Table *disptab = buffer_display_table ();
8634
8635 if (! disptab_matches_widthtab (disptab,
8636 XVECTOR (current_buffer->width_table)))
8637 {
8638 invalidate_region_cache (current_buffer,
8639 current_buffer->width_run_cache,
8640 BEG, Z);
8641 recompute_width_table (current_buffer, disptab);
8642 }
8643 }
8644
8645 /* If window-start is screwed up, choose a new one. */
8646 if (XMARKER (w->start)->buffer != current_buffer)
8647 goto recenter;
8648
8649 SET_TEXT_POS_FROM_MARKER (startp, w->start);
8650
8651 /* If someone specified a new starting point but did not insist,
8652 check whether it can be used. */
8653 if (!NILP (w->optional_new_start)
8654 && CHARPOS (startp) >= BEGV
8655 && CHARPOS (startp) <= ZV)
8656 {
8657 w->optional_new_start = Qnil;
8658 /* This takes a mini-buffer prompt into account. */
8659 start_display (&it, w, startp);
8660 move_it_to (&it, PT, 0, it.last_visible_y, -1,
8661 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
8662 if (IT_CHARPOS (it) == PT)
8663 w->force_start = Qt;
8664 }
8665
8666 /* Handle case where place to start displaying has been specified,
8667 unless the specified location is outside the accessible range. */
8668 if (!NILP (w->force_start)
8669 || w->frozen_window_start_p)
8670 {
8671 w->force_start = Qnil;
8672 w->vscroll = 0;
8673 w->window_end_valid = Qnil;
8674
8675 /* Forget any recorded base line for line number display. */
8676 if (!current_matrix_up_to_date_p
8677 || current_buffer->clip_changed)
8678 w->base_line_number = Qnil;
8679
8680 /* Redisplay the mode line. Select the buffer properly for that.
8681 Also, run the hook window-scroll-functions
8682 because we have scrolled. */
8683 /* Note, we do this after clearing force_start because
8684 if there's an error, it is better to forget about force_start
8685 than to get into an infinite loop calling the hook functions
8686 and having them get more errors. */
8687 if (!update_mode_line
8688 || ! NILP (Vwindow_scroll_functions))
8689 {
8690 if (!really_switched_buffer)
8691 {
8692 set_buffer_temp (old);
8693 set_buffer_internal_1 (XBUFFER (w->buffer));
8694 really_switched_buffer = 1;
8695 }
8696
8697 update_mode_line = 1;
8698 w->update_mode_line = Qt;
8699 startp = run_window_scroll_functions (window, startp);
8700 }
8701
8702 XSETFASTINT (w->last_modified, 0);
8703 XSETFASTINT (w->last_overlay_modified, 0);
8704 if (CHARPOS (startp) < BEGV)
8705 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
8706 else if (CHARPOS (startp) > ZV)
8707 SET_TEXT_POS (startp, ZV, ZV_BYTE);
8708
8709 /* Redisplay, then check if cursor has been set during the
8710 redisplay. Give up if new fonts were loaded. */
8711 if (!try_window (window, startp))
8712 {
8713 w->force_start = Qt;
8714 clear_glyph_matrix (w->desired_matrix);
8715 goto restore_buffers;
8716 }
8717
8718 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
8719 {
8720 /* If point does not appear, or on a line that is not fully
8721 visible, move point so it does appear. The desired
8722 matrix has been built above, so we can use it. */
8723 int height = window_box_height (w) / 2;
8724 struct glyph_row *row = MATRIX_ROW (w->desired_matrix, 0);
8725
8726 while (row->y < height)
8727 ++row;
8728
8729 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
8730 MATRIX_ROW_START_BYTEPOS (row));
8731
8732 if (w != XWINDOW (selected_window))
8733 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
8734 else if (current_buffer == old)
8735 SET_TEXT_POS (lpoint, PT, PT_BYTE);
8736
8737 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
8738
8739 /* If we are highlighting the region, then we just changed
8740 the region, so redisplay to show it. */
8741 if (!NILP (Vtransient_mark_mode)
8742 && !NILP (current_buffer->mark_active))
8743 {
8744 clear_glyph_matrix (w->desired_matrix);
8745 if (!try_window (window, startp))
8746 goto restore_buffers;
8747 }
8748 }
8749
8750 make_cursor_line_fully_visible (w);
8751 #if GLYPH_DEBUG
8752 debug_method_add (w, "forced window start");
8753 #endif
8754 goto done;
8755 }
8756
8757 /* Handle case where text has not changed, only point, and it has
8758 not moved off the frame. */
8759 if (current_matrix_up_to_date_p
8760 /* Point may be in this window. */
8761 && PT >= CHARPOS (startp)
8762 /* If we don't check this, we are called to move the cursor in a
8763 horizontally split window with a current matrix that doesn't
8764 fit the display. */
8765 && !windows_or_buffers_changed
8766 /* Selective display hasn't changed. */
8767 && !current_buffer->clip_changed
8768 /* If force-mode-line-update was called, really redisplay;
8769 that's how redisplay is forced after e.g. changing
8770 buffer-invisibility-spec. */
8771 && NILP (w->update_mode_line)
8772 /* Can't use this case if highlighting a region. When a
8773 region exists, cursor movement has to do more than just
8774 set the cursor. */
8775 && !(!NILP (Vtransient_mark_mode)
8776 && !NILP (current_buffer->mark_active))
8777 && NILP (w->region_showing)
8778 && NILP (Vshow_trailing_whitespace)
8779 /* Right after splitting windows, last_point may be nil. */
8780 && INTEGERP (w->last_point)
8781 /* This code is not used for mini-buffer for the sake of the case
8782 of redisplaying to replace an echo area message; since in
8783 that case the mini-buffer contents per se are usually
8784 unchanged. This code is of no real use in the mini-buffer
8785 since the handling of this_line_start_pos, etc., in redisplay
8786 handles the same cases. */
8787 && !EQ (window, minibuf_window)
8788 /* When splitting windows or for new windows, it happens that
8789 redisplay is called with a nil window_end_vpos or one being
8790 larger than the window. This should really be fixed in
8791 window.c. I don't have this on my list, now, so we do
8792 approximately the same as the old redisplay code. --gerd. */
8793 && INTEGERP (w->window_end_vpos)
8794 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
8795 && (FRAME_WINDOW_P (f)
8796 || !MARKERP (Voverlay_arrow_position)
8797 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
8798 {
8799 int this_scroll_margin;
8800 struct glyph_row *row;
8801 int scroll_p;
8802
8803 #if GLYPH_DEBUG
8804 debug_method_add (w, "cursor movement");
8805 #endif
8806
8807 /* Scroll if point within this distance from the top or bottom
8808 of the window. This is a pixel value. */
8809 this_scroll_margin = max (0, scroll_margin);
8810 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
8811 this_scroll_margin *= CANON_Y_UNIT (f);
8812
8813 /* Start with the row the cursor was displayed during the last
8814 not paused redisplay. Give up if that row is not valid. */
8815 if (w->last_cursor.vpos >= w->current_matrix->nrows)
8816 goto try_to_scroll;
8817 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
8818 if (row->mode_line_p)
8819 ++row;
8820 if (!row->enabled_p)
8821 goto try_to_scroll;
8822
8823 scroll_p = 0;
8824 if (PT > XFASTINT (w->last_point))
8825 {
8826 /* Point has moved forward. */
8827 int last_y = window_text_bottom_y (w) - this_scroll_margin;
8828
8829 while ((MATRIX_ROW_END_CHARPOS (row) < PT
8830 /* The end position of a row equals the start
8831 position of the next row. If PT is there, we
8832 would rather display it in the next line, except
8833 when this line ends in ZV. */
8834 || (MATRIX_ROW_END_CHARPOS (row) == PT
8835 && (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
8836 || !row->ends_at_zv_p)))
8837 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
8838 {
8839 xassert (row->enabled_p);
8840 ++row;
8841 }
8842
8843 /* If within the scroll margin, scroll. Note that
8844 MATRIX_ROW_BOTTOM_Y gives the pixel position at which the
8845 next line would be drawn, and that this_scroll_margin can
8846 be zero. */
8847 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
8848 || PT > MATRIX_ROW_END_CHARPOS (row)
8849 /* Line is completely visible last line in window and PT
8850 is to be set in the next line. */
8851 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
8852 && PT == MATRIX_ROW_END_CHARPOS (row)
8853 && !row->ends_at_zv_p
8854 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
8855 scroll_p = 1;
8856 }
8857 else if (PT < XFASTINT (w->last_point))
8858 {
8859 /* Cursor has to be moved backward. Note that PT >=
8860 CHARPOS (startp) because of the outer if-statement. */
8861 while (!row->mode_line_p
8862 && (MATRIX_ROW_START_CHARPOS (row) > PT
8863 || (MATRIX_ROW_START_CHARPOS (row) == PT
8864 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
8865 && (row->y > this_scroll_margin
8866 || CHARPOS (startp) == BEGV))
8867 {
8868 xassert (row->enabled_p);
8869 --row;
8870 }
8871
8872 /* Consider the following case: Window starts at BEGV, there
8873 is invisible, intangible text at BEGV, so that display
8874 starts at some point START > BEGV. It can happen that
8875 we are called with PT somewhere between BEGV and START.
8876 Try to handle that case. */
8877 if (row < w->current_matrix->rows
8878 || row->mode_line_p)
8879 {
8880 row = w->current_matrix->rows;
8881 if (row->mode_line_p)
8882 ++row;
8883 }
8884
8885 /* Due to newlines in overlay strings, we may have to skip
8886 forward over overlay strings. */
8887 while (MATRIX_ROW_END_CHARPOS (row) == PT
8888 && MATRIX_ROW_ENDS_IN_OVERLAY_STRING_P (row)
8889 && !row->ends_at_zv_p)
8890 ++row;
8891
8892 /* If within the scroll margin, scroll. */
8893 if (row->y < this_scroll_margin
8894 && CHARPOS (startp) != BEGV)
8895 scroll_p = 1;
8896 }
8897
8898 /* if PT is not in the glyph row, give up. */
8899 if (PT < MATRIX_ROW_START_CHARPOS (row)
8900 || PT > MATRIX_ROW_END_CHARPOS (row))
8901 goto try_to_scroll;
8902
8903 /* If we end up in a partially visible line, let's make it fully
8904 visible. This can be done most easily by using the existing
8905 scrolling code. */
8906 if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
8907 {
8908 temp_scroll_step = 1;
8909 goto try_to_scroll;
8910 }
8911 else if (scroll_p)
8912 goto try_to_scroll;
8913
8914 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
8915 goto done;
8916 }
8917
8918 /* If current starting point was originally the beginning of a line
8919 but no longer is, find a new starting point. */
8920 else if (!NILP (w->start_at_line_beg)
8921 && !(CHARPOS (startp) <= BEGV
8922 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
8923 {
8924 #if GLYPH_DEBUG
8925 debug_method_add (w, "recenter 1");
8926 #endif
8927 goto recenter;
8928 }
8929
8930 /* Try scrolling with try_window_id. */
8931 else if (/* Windows and buffers haven't changed. */
8932 !windows_or_buffers_changed
8933 /* Window must be either use window-based redisplay or
8934 be full width. */
8935 && (FRAME_WINDOW_P (f)
8936 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)))
8937 && !MINI_WINDOW_P (w)
8938 /* Point is not known NOT to appear in window. */
8939 && PT >= CHARPOS (startp)
8940 && XFASTINT (w->last_modified)
8941 /* Window is not hscrolled. */
8942 && XFASTINT (w->hscroll) == 0
8943 /* Selective display has not changed. */
8944 && !current_buffer->clip_changed
8945 /* Current matrix is up to date. */
8946 && !NILP (w->window_end_valid)
8947 /* Can't use this case if highlighting a region because
8948 a cursor movement will do more than just set the cursor. */
8949 && !(!NILP (Vtransient_mark_mode)
8950 && !NILP (current_buffer->mark_active))
8951 && NILP (w->region_showing)
8952 && NILP (Vshow_trailing_whitespace)
8953 /* Overlay arrow position and string not changed. */
8954 && EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
8955 && EQ (last_arrow_string, Voverlay_arrow_string)
8956 /* Value is > 0 if update has been done, it is -1 if we
8957 know that the same window start will not work. It is 0
8958 if unsuccessful for some other reason. */
8959 && (tem = try_window_id (w)) != 0)
8960 {
8961 #if GLYPH_DEBUG
8962 debug_method_add (w, "try_window_id");
8963 #endif
8964
8965 if (fonts_changed_p)
8966 goto restore_buffers;
8967 if (tem > 0)
8968 goto done;
8969 /* Otherwise try_window_id has returned -1 which means that we
8970 don't want the alternative below this comment to execute. */
8971 }
8972 else if (CHARPOS (startp) >= BEGV
8973 && CHARPOS (startp) <= ZV
8974 && PT >= CHARPOS (startp)
8975 && (CHARPOS (startp) < ZV
8976 /* Avoid starting at end of buffer. */
8977 || CHARPOS (startp) == BEGV
8978 || (XFASTINT (w->last_modified) >= MODIFF
8979 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
8980 {
8981 #if GLYPH_DEBUG
8982 debug_method_add (w, "same window start");
8983 #endif
8984
8985 /* Try to redisplay starting at same place as before.
8986 If point has not moved off frame, accept the results. */
8987 if (!current_matrix_up_to_date_p
8988 /* Don't use try_window_reusing_current_matrix in this case
8989 because a window scroll function can have changed the
8990 buffer. */
8991 || !NILP (Vwindow_scroll_functions)
8992 || MINI_WINDOW_P (w)
8993 || !try_window_reusing_current_matrix (w))
8994 {
8995 IF_DEBUG (debug_method_add (w, "1"));
8996 try_window (window, startp);
8997 }
8998
8999 if (fonts_changed_p)
9000 goto restore_buffers;
9001
9002 if (w->cursor.vpos >= 0)
9003 {
9004 if (!just_this_one_p
9005 || current_buffer->clip_changed
9006 || BEG_UNCHANGED < CHARPOS (startp))
9007 /* Forget any recorded base line for line number display. */
9008 w->base_line_number = Qnil;
9009
9010 make_cursor_line_fully_visible (w);
9011 goto done;
9012 }
9013 else
9014 clear_glyph_matrix (w->desired_matrix);
9015 }
9016
9017 try_to_scroll:
9018
9019 XSETFASTINT (w->last_modified, 0);
9020 XSETFASTINT (w->last_overlay_modified, 0);
9021
9022 /* Redisplay the mode line. Select the buffer properly for that. */
9023 if (!update_mode_line)
9024 {
9025 if (!really_switched_buffer)
9026 {
9027 set_buffer_temp (old);
9028 set_buffer_internal_1 (XBUFFER (w->buffer));
9029 really_switched_buffer = 1;
9030 }
9031 update_mode_line = 1;
9032 w->update_mode_line = Qt;
9033 }
9034
9035 /* Try to scroll by specified few lines. */
9036 if ((scroll_conservatively
9037 || scroll_step
9038 || temp_scroll_step
9039 || NUMBERP (current_buffer->scroll_up_aggressively)
9040 || NUMBERP (current_buffer->scroll_down_aggressively))
9041 && !current_buffer->clip_changed
9042 && CHARPOS (startp) >= BEGV
9043 && CHARPOS (startp) <= ZV)
9044 {
9045 /* The function returns -1 if new fonts were loaded, 1 if
9046 successful, 0 if not successful. */
9047 int rc = try_scrolling (window, just_this_one_p,
9048 scroll_conservatively,
9049 scroll_step,
9050 temp_scroll_step);
9051 if (rc > 0)
9052 goto done;
9053 else if (rc < 0)
9054 goto restore_buffers;
9055 }
9056
9057 /* Finally, just choose place to start which centers point */
9058
9059 recenter:
9060
9061 #if GLYPH_DEBUG
9062 debug_method_add (w, "recenter");
9063 #endif
9064
9065 /* w->vscroll = 0; */
9066
9067 /* Forget any previously recorded base line for line number display. */
9068 if (!current_matrix_up_to_date_p
9069 || current_buffer->clip_changed)
9070 w->base_line_number = Qnil;
9071
9072 /* Move backward half the height of the window. */
9073 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
9074 it.current_y = it.last_visible_y;
9075 move_it_vertically_backward (&it, it.last_visible_y / 2);
9076 xassert (IT_CHARPOS (it) >= BEGV);
9077
9078 /* The function move_it_vertically_backward may move over more
9079 than the specified y-distance. If it->w is small, e.g. a
9080 mini-buffer window, we may end up in front of the window's
9081 display area. Start displaying at the start of the line
9082 containing PT in this case. */
9083 if (it.current_y <= 0)
9084 {
9085 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
9086 move_it_vertically (&it, 0);
9087 xassert (IT_CHARPOS (it) <= PT);
9088 it.current_y = 0;
9089 }
9090
9091 it.current_x = it.hpos = 0;
9092
9093 /* Set startp here explicitly in case that helps avoid an infinite loop
9094 in case the window-scroll-functions functions get errors. */
9095 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
9096
9097 /* Run scroll hooks. */
9098 startp = run_window_scroll_functions (window, it.current.pos);
9099
9100 /* Redisplay the window. */
9101 if (!current_matrix_up_to_date_p
9102 || windows_or_buffers_changed
9103 /* Don't use try_window_reusing_current_matrix in this case
9104 because it can have changed the buffer. */
9105 || !NILP (Vwindow_scroll_functions)
9106 || !just_this_one_p
9107 || MINI_WINDOW_P (w)
9108 || !try_window_reusing_current_matrix (w))
9109 try_window (window, startp);
9110
9111 /* If new fonts have been loaded (due to fontsets), give up. We
9112 have to start a new redisplay since we need to re-adjust glyph
9113 matrices. */
9114 if (fonts_changed_p)
9115 goto restore_buffers;
9116
9117 /* If cursor did not appear assume that the middle of the window is
9118 in the first line of the window. Do it again with the next line.
9119 (Imagine a window of height 100, displaying two lines of height
9120 60. Moving back 50 from it->last_visible_y will end in the first
9121 line.) */
9122 if (w->cursor.vpos < 0)
9123 {
9124 if (!NILP (w->window_end_valid)
9125 && PT >= Z - XFASTINT (w->window_end_pos))
9126 {
9127 clear_glyph_matrix (w->desired_matrix);
9128 move_it_by_lines (&it, 1, 0);
9129 try_window (window, it.current.pos);
9130 }
9131 else if (PT < IT_CHARPOS (it))
9132 {
9133 clear_glyph_matrix (w->desired_matrix);
9134 move_it_by_lines (&it, -1, 0);
9135 try_window (window, it.current.pos);
9136 }
9137 else
9138 {
9139 /* Not much we can do about it. */
9140 }
9141 }
9142
9143 /* Consider the following case: Window starts at BEGV, there is
9144 invisible, intangible text at BEGV, so that display starts at
9145 some point START > BEGV. It can happen that we are called with
9146 PT somewhere between BEGV and START. Try to handle that case. */
9147 if (w->cursor.vpos < 0)
9148 {
9149 struct glyph_row *row = w->current_matrix->rows;
9150 if (row->mode_line_p)
9151 ++row;
9152 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9153 }
9154
9155 make_cursor_line_fully_visible (w);
9156
9157 done:
9158
9159 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9160 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
9161 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
9162 ? Qt : Qnil);
9163
9164 /* Display the mode line, if we must. */
9165 if ((update_mode_line
9166 /* If window not full width, must redo its mode line
9167 if (a) the window to its side is being redone and
9168 (b) we do a frame-based redisplay. This is a consequence
9169 of how inverted lines are drawn in frame-based redisplay. */
9170 || (!just_this_one_p
9171 && !FRAME_WINDOW_P (f)
9172 && !WINDOW_FULL_WIDTH_P (w))
9173 /* Line number to display. */
9174 || INTEGERP (w->base_line_pos)
9175 /* Column number is displayed and different from the one displayed. */
9176 || (!NILP (w->column_number_displayed)
9177 && XFASTINT (w->column_number_displayed) != current_column ()))
9178 /* This means that the window has a mode line. */
9179 && (WINDOW_WANTS_MODELINE_P (w)
9180 || WINDOW_WANTS_HEADER_LINE_P (w)))
9181 {
9182 display_mode_lines (w);
9183
9184 /* If mode line height has changed, arrange for a thorough
9185 immediate redisplay using the correct mode line height. */
9186 if (WINDOW_WANTS_MODELINE_P (w)
9187 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
9188 {
9189 fonts_changed_p = 1;
9190 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
9191 = DESIRED_MODE_LINE_HEIGHT (w);
9192 }
9193
9194 /* If top line height has changed, arrange for a thorough
9195 immediate redisplay using the correct mode line height. */
9196 if (WINDOW_WANTS_HEADER_LINE_P (w)
9197 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
9198 {
9199 fonts_changed_p = 1;
9200 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
9201 = DESIRED_HEADER_LINE_HEIGHT (w);
9202 }
9203
9204 if (fonts_changed_p)
9205 goto restore_buffers;
9206 }
9207
9208 if (!line_number_displayed
9209 && !BUFFERP (w->base_line_pos))
9210 {
9211 w->base_line_pos = Qnil;
9212 w->base_line_number = Qnil;
9213 }
9214
9215 finish_menu_bars:
9216
9217 /* When we reach a frame's selected window, redo the frame's menu bar. */
9218 if (update_mode_line
9219 && EQ (FRAME_SELECTED_WINDOW (f), window))
9220 {
9221 int redisplay_menu_p = 0;
9222
9223 if (FRAME_WINDOW_P (f))
9224 {
9225 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
9226 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
9227 #else
9228 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
9229 #endif
9230 }
9231 else
9232 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
9233
9234 if (redisplay_menu_p)
9235 display_menu_bar (w);
9236
9237 #ifdef HAVE_WINDOW_SYSTEM
9238 if (WINDOWP (f->tool_bar_window)
9239 && (FRAME_TOOL_BAR_LINES (f) > 0
9240 || auto_resize_tool_bars_p))
9241 redisplay_tool_bar (f);
9242 #endif
9243 }
9244
9245 finish_scroll_bars:
9246
9247 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
9248 {
9249 int start, end, whole;
9250
9251 /* Calculate the start and end positions for the current window.
9252 At some point, it would be nice to choose between scrollbars
9253 which reflect the whole buffer size, with special markers
9254 indicating narrowing, and scrollbars which reflect only the
9255 visible region.
9256
9257 Note that mini-buffers sometimes aren't displaying any text. */
9258 if (!MINI_WINDOW_P (w)
9259 || (w == XWINDOW (minibuf_window)
9260 && NILP (echo_area_buffer[0])))
9261 {
9262 whole = ZV - BEGV;
9263 start = marker_position (w->start) - BEGV;
9264 /* I don't think this is guaranteed to be right. For the
9265 moment, we'll pretend it is. */
9266 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
9267
9268 if (end < start)
9269 end = start;
9270 if (whole < (end - start))
9271 whole = end - start;
9272 }
9273 else
9274 start = end = whole = 0;
9275
9276 /* Indicate what this scroll bar ought to be displaying now. */
9277 (*set_vertical_scroll_bar_hook) (w, end - start, whole, start);
9278
9279 /* Note that we actually used the scroll bar attached to this
9280 window, so it shouldn't be deleted at the end of redisplay. */
9281 (*redeem_scroll_bar_hook) (w);
9282 }
9283
9284 restore_buffers:
9285
9286 /* Restore current_buffer and value of point in it. */
9287 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
9288 if (really_switched_buffer)
9289 set_buffer_internal_1 (old);
9290 else
9291 set_buffer_temp (old);
9292 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
9293
9294 unbind_to (count, Qnil);
9295 }
9296
9297
9298 /* Build the complete desired matrix of WINDOW with a window start
9299 buffer position POS. Value is non-zero if successful. It is zero
9300 if fonts were loaded during redisplay which makes re-adjusting
9301 glyph matrices necessary. */
9302
9303 int
9304 try_window (window, pos)
9305 Lisp_Object window;
9306 struct text_pos pos;
9307 {
9308 struct window *w = XWINDOW (window);
9309 struct it it;
9310 struct glyph_row *last_text_row = NULL;
9311
9312 /* Make POS the new window start. */
9313 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
9314
9315 /* Mark cursor position as unknown. No overlay arrow seen. */
9316 w->cursor.vpos = -1;
9317 overlay_arrow_seen = 0;
9318
9319 /* Initialize iterator and info to start at POS. */
9320 start_display (&it, w, pos);
9321
9322 /* Display all lines of W. */
9323 while (it.current_y < it.last_visible_y)
9324 {
9325 if (display_line (&it))
9326 last_text_row = it.glyph_row - 1;
9327 if (fonts_changed_p)
9328 return 0;
9329 }
9330
9331 /* If bottom moved off end of frame, change mode line percentage. */
9332 if (XFASTINT (w->window_end_pos) <= 0
9333 && Z != IT_CHARPOS (it))
9334 w->update_mode_line = Qt;
9335
9336 /* Set window_end_pos to the offset of the last character displayed
9337 on the window from the end of current_buffer. Set
9338 window_end_vpos to its row number. */
9339 if (last_text_row)
9340 {
9341 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
9342 w->window_end_bytepos
9343 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
9344 XSETFASTINT (w->window_end_pos,
9345 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
9346 XSETFASTINT (w->window_end_vpos,
9347 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
9348 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
9349 ->displays_text_p);
9350 }
9351 else
9352 {
9353 w->window_end_bytepos = 0;
9354 XSETFASTINT (w->window_end_pos, 0);
9355 XSETFASTINT (w->window_end_vpos, 0);
9356 }
9357
9358 /* But that is not valid info until redisplay finishes. */
9359 w->window_end_valid = Qnil;
9360 return 1;
9361 }
9362
9363
9364 \f
9365 /************************************************************************
9366 Window redisplay reusing current matrix when buffer has not changed
9367 ************************************************************************/
9368
9369 /* Try redisplay of window W showing an unchanged buffer with a
9370 different window start than the last time it was displayed by
9371 reusing its current matrix. Value is non-zero if successful.
9372 W->start is the new window start. */
9373
9374 static int
9375 try_window_reusing_current_matrix (w)
9376 struct window *w;
9377 {
9378 struct frame *f = XFRAME (w->frame);
9379 struct glyph_row *row, *bottom_row;
9380 struct it it;
9381 struct run run;
9382 struct text_pos start, new_start;
9383 int nrows_scrolled, i;
9384 struct glyph_row *last_text_row;
9385 struct glyph_row *last_reused_text_row;
9386 struct glyph_row *start_row;
9387 int start_vpos, min_y, max_y;
9388
9389 /* Right now this function doesn't handle terminal frames. */
9390 if (!FRAME_WINDOW_P (f))
9391 return 0;
9392
9393 /* Can't do this if region may have changed. */
9394 if ((!NILP (Vtransient_mark_mode)
9395 && !NILP (current_buffer->mark_active))
9396 || !NILP (w->region_showing)
9397 || !NILP (Vshow_trailing_whitespace))
9398 return 0;
9399
9400 /* If top-line visibility has changed, give up. */
9401 if (WINDOW_WANTS_HEADER_LINE_P (w)
9402 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
9403 return 0;
9404
9405 /* Give up if old or new display is scrolled vertically. We could
9406 make this function handle this, but right now it doesn't. */
9407 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9408 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
9409 return 0;
9410
9411 /* The variable new_start now holds the new window start. The old
9412 start `start' can be determined from the current matrix. */
9413 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
9414 start = start_row->start.pos;
9415 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
9416
9417 /* Clear the desired matrix for the display below. */
9418 clear_glyph_matrix (w->desired_matrix);
9419
9420 if (CHARPOS (new_start) <= CHARPOS (start))
9421 {
9422 int first_row_y;
9423
9424 IF_DEBUG (debug_method_add (w, "twu1"));
9425
9426 /* Display up to a row that can be reused. The variable
9427 last_text_row is set to the last row displayed that displays
9428 text. */
9429 start_display (&it, w, new_start);
9430 first_row_y = it.current_y;
9431 w->cursor.vpos = -1;
9432 last_text_row = last_reused_text_row = NULL;
9433 while (it.current_y < it.last_visible_y
9434 && IT_CHARPOS (it) < CHARPOS (start)
9435 && !fonts_changed_p)
9436 if (display_line (&it))
9437 last_text_row = it.glyph_row - 1;
9438
9439 /* A value of current_y < last_visible_y means that we stopped
9440 at the previous window start, which in turn means that we
9441 have at least one reusable row. */
9442 if (it.current_y < it.last_visible_y)
9443 {
9444 nrows_scrolled = it.vpos;
9445
9446 /* Find PT if not already found in the lines displayed. */
9447 if (w->cursor.vpos < 0)
9448 {
9449 int dy = it.current_y - first_row_y;
9450
9451 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9452 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9453 {
9454 if (PT >= MATRIX_ROW_START_CHARPOS (row)
9455 && PT < MATRIX_ROW_END_CHARPOS (row))
9456 {
9457 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
9458 dy, nrows_scrolled);
9459 break;
9460 }
9461
9462 if (MATRIX_ROW_BOTTOM_Y (row) + dy >= it.last_visible_y)
9463 break;
9464
9465 ++row;
9466 }
9467
9468 /* Give up if point was not found. This shouldn't
9469 happen often; not more often than with try_window
9470 itself. */
9471 if (w->cursor.vpos < 0)
9472 {
9473 clear_glyph_matrix (w->desired_matrix);
9474 return 0;
9475 }
9476 }
9477
9478 /* Scroll the display. Do it before the current matrix is
9479 changed. The problem here is that update has not yet
9480 run, i.e. part of the current matrix is not up to date.
9481 scroll_run_hook will clear the cursor, and use the
9482 current matrix to get the height of the row the cursor is
9483 in. */
9484 run.current_y = first_row_y;
9485 run.desired_y = it.current_y;
9486 run.height = it.last_visible_y - it.current_y;
9487 if (run.height > 0
9488 && run.current_y != run.desired_y)
9489 {
9490 update_begin (f);
9491 rif->update_window_begin_hook (w);
9492 rif->scroll_run_hook (w, &run);
9493 rif->update_window_end_hook (w, 0);
9494 update_end (f);
9495 }
9496
9497 /* Shift current matrix down by nrows_scrolled lines. */
9498 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
9499 rotate_matrix (w->current_matrix,
9500 start_vpos,
9501 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
9502 nrows_scrolled);
9503
9504 /* Disable lines not reused. */
9505 for (i = 0; i < it.vpos; ++i)
9506 MATRIX_ROW (w->current_matrix, i)->enabled_p = 0;
9507
9508 /* Re-compute Y positions. */
9509 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix) + nrows_scrolled;
9510 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
9511 max_y = it.last_visible_y;
9512 while (row < bottom_row)
9513 {
9514 row->y = it.current_y;
9515
9516 if (row->y < min_y)
9517 row->visible_height = row->height - (min_y - row->y);
9518 else if (row->y + row->height > max_y)
9519 row->visible_height
9520 = row->height - (row->y + row->height - max_y);
9521 else
9522 row->visible_height = row->height;
9523
9524 it.current_y += row->height;
9525 ++it.vpos;
9526
9527 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9528 last_reused_text_row = row;
9529 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
9530 break;
9531 ++row;
9532 }
9533 }
9534
9535 /* Update window_end_pos etc.; last_reused_text_row is the last
9536 reused row from the current matrix containing text, if any.
9537 The value of last_text_row is the last displayed line
9538 containing text. */
9539 if (last_reused_text_row)
9540 {
9541 w->window_end_bytepos
9542 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
9543 XSETFASTINT (w->window_end_pos,
9544 Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
9545 XSETFASTINT (w->window_end_vpos,
9546 MATRIX_ROW_VPOS (last_reused_text_row,
9547 w->current_matrix));
9548 }
9549 else if (last_text_row)
9550 {
9551 w->window_end_bytepos
9552 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
9553 XSETFASTINT (w->window_end_pos,
9554 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
9555 XSETFASTINT (w->window_end_vpos,
9556 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
9557 }
9558 else
9559 {
9560 /* This window must be completely empty. */
9561 w->window_end_bytepos = 0;
9562 XSETFASTINT (w->window_end_pos, 0);
9563 XSETFASTINT (w->window_end_vpos, 0);
9564 }
9565 w->window_end_valid = Qnil;
9566
9567 /* Update hint: don't try scrolling again in update_window. */
9568 w->desired_matrix->no_scrolling_p = 1;
9569
9570 #if GLYPH_DEBUG
9571 debug_method_add (w, "try_window_reusing_current_matrix 1");
9572 #endif
9573 return 1;
9574 }
9575 else if (CHARPOS (new_start) > CHARPOS (start))
9576 {
9577 struct glyph_row *pt_row, *row;
9578 struct glyph_row *first_reusable_row;
9579 struct glyph_row *first_row_to_display;
9580 int dy;
9581 int yb = window_text_bottom_y (w);
9582
9583 IF_DEBUG (debug_method_add (w, "twu2"));
9584
9585 /* Find the row starting at new_start, if there is one. Don't
9586 reuse a partially visible line at the end. */
9587 first_reusable_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9588 while (first_reusable_row->enabled_p
9589 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
9590 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
9591 < CHARPOS (new_start)))
9592 ++first_reusable_row;
9593
9594 /* Give up if there is no row to reuse. */
9595 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
9596 || !first_reusable_row->enabled_p
9597 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
9598 != CHARPOS (new_start)))
9599 return 0;
9600
9601 /* We can reuse fully visible rows beginning with
9602 first_reusable_row to the end of the window. Set
9603 first_row_to_display to the first row that cannot be reused.
9604 Set pt_row to the row containing point, if there is any. */
9605 first_row_to_display = first_reusable_row;
9606 pt_row = NULL;
9607 while (MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb)
9608 {
9609 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
9610 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
9611 pt_row = first_row_to_display;
9612
9613 ++first_row_to_display;
9614 }
9615
9616 /* Start displaying at the start of first_row_to_display. */
9617 xassert (first_row_to_display->y < yb);
9618 init_to_row_start (&it, w, first_row_to_display);
9619 nrows_scrolled = MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix);
9620 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
9621 - nrows_scrolled);
9622 it.current_y = first_row_to_display->y - first_reusable_row->y;
9623
9624 /* Display lines beginning with first_row_to_display in the
9625 desired matrix. Set last_text_row to the last row displayed
9626 that displays text. */
9627 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
9628 if (pt_row == NULL)
9629 w->cursor.vpos = -1;
9630 last_text_row = NULL;
9631 while (it.current_y < it.last_visible_y && !fonts_changed_p)
9632 if (display_line (&it))
9633 last_text_row = it.glyph_row - 1;
9634
9635 /* Give up If point isn't in a row displayed or reused. */
9636 if (w->cursor.vpos < 0)
9637 {
9638 clear_glyph_matrix (w->desired_matrix);
9639 return 0;
9640 }
9641
9642 /* If point is in a reused row, adjust y and vpos of the cursor
9643 position. */
9644 if (pt_row)
9645 {
9646 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
9647 w->current_matrix);
9648 w->cursor.y -= first_reusable_row->y;
9649 }
9650
9651 /* Scroll the display. */
9652 run.current_y = first_reusable_row->y;
9653 run.desired_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
9654 run.height = it.last_visible_y - run.current_y;
9655 if (run.height)
9656 {
9657 struct frame *f = XFRAME (WINDOW_FRAME (w));
9658 update_begin (f);
9659 rif->update_window_begin_hook (w);
9660 rif->scroll_run_hook (w, &run);
9661 rif->update_window_end_hook (w, 0);
9662 update_end (f);
9663 }
9664
9665 /* Adjust Y positions of reused rows. */
9666 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
9667 row = first_reusable_row;
9668 dy = first_reusable_row->y;
9669 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
9670 max_y = it.last_visible_y;
9671 while (row < first_row_to_display)
9672 {
9673 row->y -= dy;
9674 if (row->y < min_y)
9675 row->visible_height = row->height - (min_y - row->y);
9676 else if (row->y + row->height > max_y)
9677 row->visible_height
9678 = row->height - (row->y + row->height - max_y);
9679 else
9680 row->visible_height = row->height;
9681 ++row;
9682 }
9683
9684 /* Disable rows not reused. */
9685 while (row < bottom_row)
9686 {
9687 row->enabled_p = 0;
9688 ++row;
9689 }
9690
9691 /* Scroll the current matrix. */
9692 xassert (nrows_scrolled > 0);
9693 rotate_matrix (w->current_matrix,
9694 start_vpos,
9695 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
9696 -nrows_scrolled);
9697
9698 /* Adjust window end. A null value of last_text_row means that
9699 the window end is in reused rows which in turn means that
9700 only its vpos can have changed. */
9701 if (last_text_row)
9702 {
9703 w->window_end_bytepos
9704 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
9705 XSETFASTINT (w->window_end_pos,
9706 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
9707 XSETFASTINT (w->window_end_vpos,
9708 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
9709 }
9710 else
9711 {
9712 XSETFASTINT (w->window_end_vpos,
9713 XFASTINT (w->window_end_vpos) - nrows_scrolled);
9714 }
9715
9716 w->window_end_valid = Qnil;
9717 w->desired_matrix->no_scrolling_p = 1;
9718
9719 #if GLYPH_DEBUG
9720 debug_method_add (w, "try_window_reusing_current_matrix 2");
9721 #endif
9722 return 1;
9723 }
9724
9725 return 0;
9726 }
9727
9728
9729 \f
9730 /************************************************************************
9731 Window redisplay reusing current matrix when buffer has changed
9732 ************************************************************************/
9733
9734 static struct glyph_row *get_last_unchanged_at_beg_row P_ ((struct window *));
9735 static struct glyph_row *get_first_unchanged_at_end_row P_ ((struct window *,
9736 int *, int *));
9737 static struct glyph_row *
9738 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
9739 struct glyph_row *));
9740
9741
9742 /* Return the last row in MATRIX displaying text. If row START is
9743 non-null, start searching with that row. IT gives the dimensions
9744 of the display. Value is null if matrix is empty; otherwise it is
9745 a pointer to the row found. */
9746
9747 static struct glyph_row *
9748 find_last_row_displaying_text (matrix, it, start)
9749 struct glyph_matrix *matrix;
9750 struct it *it;
9751 struct glyph_row *start;
9752 {
9753 struct glyph_row *row, *row_found;
9754
9755 /* Set row_found to the last row in IT->w's current matrix
9756 displaying text. The loop looks funny but think of partially
9757 visible lines. */
9758 row_found = NULL;
9759 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
9760 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9761 {
9762 xassert (row->enabled_p);
9763 row_found = row;
9764 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
9765 break;
9766 ++row;
9767 }
9768
9769 return row_found;
9770 }
9771
9772
9773 /* Return the last row in the current matrix of W that is not affected
9774 by changes at the start of current_buffer that occurred since the
9775 last time W was redisplayed. Value is null if no such row exists.
9776
9777 The global variable beg_unchanged has to contain the number of
9778 bytes unchanged at the start of current_buffer. BEG +
9779 beg_unchanged is the buffer position of the first changed byte in
9780 current_buffer. Characters at positions < BEG + beg_unchanged are
9781 at the same buffer positions as they were when the current matrix
9782 was built. */
9783
9784 static struct glyph_row *
9785 get_last_unchanged_at_beg_row (w)
9786 struct window *w;
9787 {
9788 int first_changed_pos = BEG + BEG_UNCHANGED;
9789 struct glyph_row *row;
9790 struct glyph_row *row_found = NULL;
9791 int yb = window_text_bottom_y (w);
9792
9793 /* Find the last row displaying unchanged text. */
9794 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9795 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
9796 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
9797 {
9798 if (/* If row ends before first_changed_pos, it is unchanged,
9799 except in some case. */
9800 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
9801 /* When row ends in ZV and we write at ZV it is not
9802 unchanged. */
9803 && !row->ends_at_zv_p
9804 /* When first_changed_pos is the end of a continued line,
9805 row is not unchanged because it may be no longer
9806 continued. */
9807 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
9808 && row->continued_p))
9809 row_found = row;
9810
9811 /* Stop if last visible row. */
9812 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
9813 break;
9814
9815 ++row;
9816 }
9817
9818 return row_found;
9819 }
9820
9821
9822 /* Find the first glyph row in the current matrix of W that is not
9823 affected by changes at the end of current_buffer since the last
9824 time the window was redisplayed. Return in *DELTA the number of
9825 chars by which buffer positions in unchanged text at the end of
9826 current_buffer must be adjusted. Return in *DELTA_BYTES the
9827 corresponding number of bytes. Value is null if no such row
9828 exists, i.e. all rows are affected by changes. */
9829
9830 static struct glyph_row *
9831 get_first_unchanged_at_end_row (w, delta, delta_bytes)
9832 struct window *w;
9833 int *delta, *delta_bytes;
9834 {
9835 struct glyph_row *row;
9836 struct glyph_row *row_found = NULL;
9837
9838 *delta = *delta_bytes = 0;
9839
9840 /* A value of window_end_pos >= end_unchanged means that the window
9841 end is in the range of changed text. If so, there is no
9842 unchanged row at the end of W's current matrix. */
9843 xassert (!NILP (w->window_end_valid));
9844 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
9845 return NULL;
9846
9847 /* Set row to the last row in W's current matrix displaying text. */
9848 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
9849
9850 /* If matrix is entirely empty, no unchanged row exists. */
9851 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9852 {
9853 /* The value of row is the last glyph row in the matrix having a
9854 meaningful buffer position in it. The end position of row
9855 corresponds to window_end_pos. This allows us to translate
9856 buffer positions in the current matrix to current buffer
9857 positions for characters not in changed text. */
9858 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
9859 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
9860 int last_unchanged_pos, last_unchanged_pos_old;
9861 struct glyph_row *first_text_row
9862 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9863
9864 *delta = Z - Z_old;
9865 *delta_bytes = Z_BYTE - Z_BYTE_old;
9866
9867 /* Set last_unchanged_pos to the buffer position of the last
9868 character in the buffer that has not been changed. Z is the
9869 index + 1 of the last byte in current_buffer, i.e. by
9870 subtracting end_unchanged we get the index of the last
9871 unchanged character, and we have to add BEG to get its buffer
9872 position. */
9873 last_unchanged_pos = Z - END_UNCHANGED + BEG;
9874 last_unchanged_pos_old = last_unchanged_pos - *delta;
9875
9876 /* Search backward from ROW for a row displaying a line that
9877 starts at a minimum position >= last_unchanged_pos_old. */
9878 while (row >= first_text_row)
9879 {
9880 xassert (row->enabled_p);
9881 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (row));
9882
9883 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
9884 row_found = row;
9885 --row;
9886 }
9887 }
9888
9889 xassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
9890 return row_found;
9891 }
9892
9893
9894 /* Make sure that glyph rows in the current matrix of window W
9895 reference the same glyph memory as corresponding rows in the
9896 frame's frame matrix. This function is called after scrolling W's
9897 current matrix on a terminal frame in try_window_id and
9898 try_window_reusing_current_matrix. */
9899
9900 static void
9901 sync_frame_with_window_matrix_rows (w)
9902 struct window *w;
9903 {
9904 struct frame *f = XFRAME (w->frame);
9905 struct glyph_row *window_row, *window_row_end, *frame_row;
9906
9907 /* Preconditions: W must be a leaf window and full-width. Its frame
9908 must have a frame matrix. */
9909 xassert (NILP (w->hchild) && NILP (w->vchild));
9910 xassert (WINDOW_FULL_WIDTH_P (w));
9911 xassert (!FRAME_WINDOW_P (f));
9912
9913 /* If W is a full-width window, glyph pointers in W's current matrix
9914 have, by definition, to be the same as glyph pointers in the
9915 corresponding frame matrix. */
9916 window_row = w->current_matrix->rows;
9917 window_row_end = window_row + w->current_matrix->nrows;
9918 frame_row = f->current_matrix->rows + XFASTINT (w->top);
9919 while (window_row < window_row_end)
9920 {
9921 int area;
9922
9923 for (area = LEFT_MARGIN_AREA; area <= LAST_AREA; ++area)
9924 frame_row->glyphs[area] = window_row->glyphs[area];
9925
9926 /* Disable frame rows whose corresponding window rows have
9927 been disabled in try_window_id. */
9928 if (!window_row->enabled_p)
9929 frame_row->enabled_p = 0;
9930
9931 ++window_row, ++frame_row;
9932 }
9933 }
9934
9935
9936 /* Find the glyph row in window W containing CHARPOS. Consider all
9937 rows between START and END (not inclusive). END null means search
9938 all rows to the end of the display area of W. Value is the row
9939 containing CHARPOS or null. */
9940
9941 static struct glyph_row *
9942 row_containing_pos (w, charpos, start, end)
9943 struct window *w;
9944 int charpos;
9945 struct glyph_row *start, *end;
9946 {
9947 struct glyph_row *row = start;
9948 int last_y;
9949
9950 /* If we happen to start on a header-line, skip that. */
9951 if (row->mode_line_p)
9952 ++row;
9953
9954 if ((end && row >= end) || !row->enabled_p)
9955 return NULL;
9956
9957 last_y = window_text_bottom_y (w);
9958
9959 while ((end == NULL || row < end)
9960 && (MATRIX_ROW_END_CHARPOS (row) < charpos
9961 /* The end position of a row equals the start
9962 position of the next row. If CHARPOS is there, we
9963 would rather display it in the next line, except
9964 when this line ends in ZV. */
9965 || (MATRIX_ROW_END_CHARPOS (row) == charpos
9966 && (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
9967 || !row->ends_at_zv_p)))
9968 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
9969 ++row;
9970
9971 /* Give up if CHARPOS not found. */
9972 if ((end && row >= end)
9973 || charpos < MATRIX_ROW_START_CHARPOS (row)
9974 || charpos > MATRIX_ROW_END_CHARPOS (row))
9975 row = NULL;
9976
9977 return row;
9978 }
9979
9980
9981 /* Try to redisplay window W by reusing its existing display. W's
9982 current matrix must be up to date when this function is called,
9983 i.e. window_end_valid must not be nil.
9984
9985 Value is
9986
9987 1 if display has been updated
9988 0 if otherwise unsuccessful
9989 -1 if redisplay with same window start is known not to succeed
9990
9991 The following steps are performed:
9992
9993 1. Find the last row in the current matrix of W that is not
9994 affected by changes at the start of current_buffer. If no such row
9995 is found, give up.
9996
9997 2. Find the first row in W's current matrix that is not affected by
9998 changes at the end of current_buffer. Maybe there is no such row.
9999
10000 3. Display lines beginning with the row + 1 found in step 1 to the
10001 row found in step 2 or, if step 2 didn't find a row, to the end of
10002 the window.
10003
10004 4. If cursor is not known to appear on the window, give up.
10005
10006 5. If display stopped at the row found in step 2, scroll the
10007 display and current matrix as needed.
10008
10009 6. Maybe display some lines at the end of W, if we must. This can
10010 happen under various circumstances, like a partially visible line
10011 becoming fully visible, or because newly displayed lines are displayed
10012 in smaller font sizes.
10013
10014 7. Update W's window end information. */
10015
10016 /* Check that window end is what we expect it to be. */
10017
10018 static int
10019 try_window_id (w)
10020 struct window *w;
10021 {
10022 struct frame *f = XFRAME (w->frame);
10023 struct glyph_matrix *current_matrix = w->current_matrix;
10024 struct glyph_matrix *desired_matrix = w->desired_matrix;
10025 struct glyph_row *last_unchanged_at_beg_row;
10026 struct glyph_row *first_unchanged_at_end_row;
10027 struct glyph_row *row;
10028 struct glyph_row *bottom_row;
10029 int bottom_vpos;
10030 struct it it;
10031 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
10032 struct text_pos start_pos;
10033 struct run run;
10034 int first_unchanged_at_end_vpos = 0;
10035 struct glyph_row *last_text_row, *last_text_row_at_end;
10036 struct text_pos start;
10037
10038 SET_TEXT_POS_FROM_MARKER (start, w->start);
10039
10040 /* Check pre-conditions. Window end must be valid, otherwise
10041 the current matrix would not be up to date. */
10042 xassert (!NILP (w->window_end_valid));
10043 xassert (FRAME_WINDOW_P (XFRAME (w->frame))
10044 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)));
10045
10046 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
10047 only if buffer has really changed. The reason is that the gap is
10048 initially at Z for freshly visited files. The code below would
10049 set end_unchanged to 0 in that case. */
10050 if (MODIFF > SAVE_MODIFF
10051 /* This seems to happen sometimes after saving a buffer. */
10052 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
10053 {
10054 if (GPT - BEG < BEG_UNCHANGED)
10055 BEG_UNCHANGED = GPT - BEG;
10056 if (Z - GPT < END_UNCHANGED)
10057 END_UNCHANGED = Z - GPT;
10058 }
10059
10060 /* If window starts after a line end, and the last change is in
10061 front of that newline, then changes don't affect the display.
10062 This case happens with stealth-fontification. Note that although
10063 the display is unchanged, glyph positions in the matrix have to
10064 be adjusted, of course. */
10065 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
10066 if (CHARPOS (start) > BEGV
10067 && Z - END_UNCHANGED < CHARPOS (start) - 1
10068 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n'
10069 && PT < MATRIX_ROW_END_CHARPOS (row))
10070 {
10071 struct glyph_row *r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
10072 int delta = CHARPOS (start) - MATRIX_ROW_START_CHARPOS (r0);
10073
10074 if (delta)
10075 {
10076 struct glyph_row *r1 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
10077 int delta_bytes = BYTEPOS (start) - MATRIX_ROW_START_BYTEPOS (r0);
10078
10079 increment_matrix_positions (w->current_matrix,
10080 MATRIX_ROW_VPOS (r0, current_matrix),
10081 MATRIX_ROW_VPOS (r1, current_matrix),
10082 delta, delta_bytes);
10083 }
10084
10085 #if 0 /* If changes are all in front of the window start, the
10086 distance of the last displayed glyph from Z hasn't
10087 changed. */
10088 w->window_end_pos
10089 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
10090 w->window_end_bytepos
10091 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
10092 #endif
10093
10094 return 1;
10095 }
10096
10097 /* Return quickly if changes are all below what is displayed in the
10098 window, and if PT is in the window. */
10099 if (BEG_UNCHANGED > MATRIX_ROW_END_CHARPOS (row)
10100 && PT < MATRIX_ROW_END_CHARPOS (row))
10101 {
10102 /* We have to update window end positions because the buffer's
10103 size has changed. */
10104 w->window_end_pos
10105 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
10106 w->window_end_bytepos
10107 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
10108 return 1;
10109 }
10110
10111 /* Check that window start agrees with the start of the first glyph
10112 row in its current matrix. Check this after we know the window
10113 start is not in changed text, otherwise positions would not be
10114 comparable. */
10115 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10116 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
10117 return 0;
10118
10119 /* Compute the position at which we have to start displaying new
10120 lines. Some of the lines at the top of the window might be
10121 reusable because they are not displaying changed text. Find the
10122 last row in W's current matrix not affected by changes at the
10123 start of current_buffer. Value is null if changes start in the
10124 first line of window. */
10125 last_unchanged_at_beg_row = get_last_unchanged_at_beg_row (w);
10126 if (last_unchanged_at_beg_row)
10127 {
10128 init_to_row_end (&it, w, last_unchanged_at_beg_row);
10129 start_pos = it.current.pos;
10130
10131 /* Start displaying new lines in the desired matrix at the same
10132 vpos we would use in the current matrix, i.e. below
10133 last_unchanged_at_beg_row. */
10134 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
10135 current_matrix);
10136 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
10137 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
10138
10139 xassert (it.hpos == 0 && it.current_x == 0);
10140 }
10141 else
10142 {
10143 /* There are no reusable lines at the start of the window.
10144 Start displaying in the first line. */
10145 start_display (&it, w, start);
10146 start_pos = it.current.pos;
10147 }
10148
10149 /* Find the first row that is not affected by changes at the end of
10150 the buffer. Value will be null if there is no unchanged row, in
10151 which case we must redisplay to the end of the window. delta
10152 will be set to the value by which buffer positions beginning with
10153 first_unchanged_at_end_row have to be adjusted due to text
10154 changes. */
10155 first_unchanged_at_end_row
10156 = get_first_unchanged_at_end_row (w, &delta, &delta_bytes);
10157 IF_DEBUG (debug_delta = delta);
10158 IF_DEBUG (debug_delta_bytes = delta_bytes);
10159
10160 /* Set stop_pos to the buffer position up to which we will have to
10161 display new lines. If first_unchanged_at_end_row != NULL, this
10162 is the buffer position of the start of the line displayed in that
10163 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
10164 that we don't stop at a buffer position. */
10165 stop_pos = 0;
10166 if (first_unchanged_at_end_row)
10167 {
10168 xassert (last_unchanged_at_beg_row == NULL
10169 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
10170
10171 /* If this is a continuation line, move forward to the next one
10172 that isn't. Changes in lines above affect this line.
10173 Caution: this may move first_unchanged_at_end_row to a row
10174 not displaying text. */
10175 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
10176 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
10177 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
10178 < it.last_visible_y))
10179 ++first_unchanged_at_end_row;
10180
10181 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
10182 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
10183 >= it.last_visible_y))
10184 first_unchanged_at_end_row = NULL;
10185 else
10186 {
10187 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
10188 + delta);
10189 first_unchanged_at_end_vpos
10190 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
10191 xassert (stop_pos >= Z - END_UNCHANGED);
10192 }
10193 }
10194 else if (last_unchanged_at_beg_row == NULL)
10195 return 0;
10196
10197
10198 #if GLYPH_DEBUG
10199
10200 /* Either there is no unchanged row at the end, or the one we have
10201 now displays text. This is a necessary condition for the window
10202 end pos calculation at the end of this function. */
10203 xassert (first_unchanged_at_end_row == NULL
10204 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
10205
10206 debug_last_unchanged_at_beg_vpos
10207 = (last_unchanged_at_beg_row
10208 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
10209 : -1);
10210 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
10211
10212 #endif /* GLYPH_DEBUG != 0 */
10213
10214
10215 /* Display new lines. Set last_text_row to the last new line
10216 displayed which has text on it, i.e. might end up as being the
10217 line where the window_end_vpos is. */
10218 w->cursor.vpos = -1;
10219 last_text_row = NULL;
10220 overlay_arrow_seen = 0;
10221 while (it.current_y < it.last_visible_y
10222 && !fonts_changed_p
10223 && (first_unchanged_at_end_row == NULL
10224 || IT_CHARPOS (it) < stop_pos))
10225 {
10226 if (display_line (&it))
10227 last_text_row = it.glyph_row - 1;
10228 }
10229
10230 if (fonts_changed_p)
10231 return -1;
10232
10233
10234 /* Compute differences in buffer positions, y-positions etc. for
10235 lines reused at the bottom of the window. Compute what we can
10236 scroll. */
10237 if (first_unchanged_at_end_row
10238 /* No lines reused because we displayed everything up to the
10239 bottom of the window. */
10240 && it.current_y < it.last_visible_y)
10241 {
10242 dvpos = (it.vpos
10243 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
10244 current_matrix));
10245 dy = it.current_y - first_unchanged_at_end_row->y;
10246 run.current_y = first_unchanged_at_end_row->y;
10247 run.desired_y = run.current_y + dy;
10248 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
10249 }
10250 else
10251 {
10252 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
10253 first_unchanged_at_end_row = NULL;
10254 }
10255 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
10256
10257
10258 /* Find the cursor if not already found. We have to decide whether
10259 PT will appear on this window (it sometimes doesn't, but this is
10260 not a very frequent case.) This decision has to be made before
10261 the current matrix is altered. A value of cursor.vpos < 0 means
10262 that PT is either in one of the lines beginning at
10263 first_unchanged_at_end_row or below the window. Don't care for
10264 lines that might be displayed later at the window end; as
10265 mentioned, this is not a frequent case. */
10266 if (w->cursor.vpos < 0)
10267 {
10268 /* Cursor in unchanged rows at the top? */
10269 if (PT < CHARPOS (start_pos)
10270 && last_unchanged_at_beg_row)
10271 {
10272 row = row_containing_pos (w, PT,
10273 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
10274 last_unchanged_at_beg_row + 1);
10275 xassert (row && row <= last_unchanged_at_beg_row);
10276 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10277 }
10278
10279 /* Start from first_unchanged_at_end_row looking for PT. */
10280 else if (first_unchanged_at_end_row)
10281 {
10282 row = row_containing_pos (w, PT - delta,
10283 first_unchanged_at_end_row, NULL);
10284 if (row)
10285 set_cursor_from_row (w, row, w->current_matrix, delta,
10286 delta_bytes, dy, dvpos);
10287 }
10288
10289 /* Give up if cursor was not found. */
10290 if (w->cursor.vpos < 0)
10291 {
10292 clear_glyph_matrix (w->desired_matrix);
10293 return -1;
10294 }
10295 }
10296
10297 /* Don't let the cursor end in the scroll margins. */
10298 {
10299 int this_scroll_margin, cursor_height;
10300
10301 this_scroll_margin = max (0, scroll_margin);
10302 this_scroll_margin = min (this_scroll_margin,
10303 XFASTINT (w->height) / 4);
10304 this_scroll_margin *= CANON_Y_UNIT (it.f);
10305 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
10306
10307 if ((w->cursor.y < this_scroll_margin
10308 && CHARPOS (start) > BEGV)
10309 /* Don't take scroll margin into account at the bottom because
10310 old redisplay didn't do it either. */
10311 || w->cursor.y + cursor_height > it.last_visible_y)
10312 {
10313 w->cursor.vpos = -1;
10314 clear_glyph_matrix (w->desired_matrix);
10315 return -1;
10316 }
10317 }
10318
10319 /* Scroll the display. Do it before changing the current matrix so
10320 that xterm.c doesn't get confused about where the cursor glyph is
10321 found. */
10322 if (dy && run.height)
10323 {
10324 update_begin (f);
10325
10326 if (FRAME_WINDOW_P (f))
10327 {
10328 rif->update_window_begin_hook (w);
10329 rif->scroll_run_hook (w, &run);
10330 rif->update_window_end_hook (w, 0);
10331 }
10332 else
10333 {
10334 /* Terminal frame. In this case, dvpos gives the number of
10335 lines to scroll by; dvpos < 0 means scroll up. */
10336 int first_unchanged_at_end_vpos
10337 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
10338 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
10339 int end = XFASTINT (w->top) + window_internal_height (w);
10340
10341 /* Perform the operation on the screen. */
10342 if (dvpos > 0)
10343 {
10344 /* Scroll last_unchanged_at_beg_row to the end of the
10345 window down dvpos lines. */
10346 set_terminal_window (end);
10347
10348 /* On dumb terminals delete dvpos lines at the end
10349 before inserting dvpos empty lines. */
10350 if (!scroll_region_ok)
10351 ins_del_lines (end - dvpos, -dvpos);
10352
10353 /* Insert dvpos empty lines in front of
10354 last_unchanged_at_beg_row. */
10355 ins_del_lines (from, dvpos);
10356 }
10357 else if (dvpos < 0)
10358 {
10359 /* Scroll up last_unchanged_at_beg_vpos to the end of
10360 the window to last_unchanged_at_beg_vpos - |dvpos|. */
10361 set_terminal_window (end);
10362
10363 /* Delete dvpos lines in front of
10364 last_unchanged_at_beg_vpos. ins_del_lines will set
10365 the cursor to the given vpos and emit |dvpos| delete
10366 line sequences. */
10367 ins_del_lines (from + dvpos, dvpos);
10368
10369 /* On a dumb terminal insert dvpos empty lines at the
10370 end. */
10371 if (!scroll_region_ok)
10372 ins_del_lines (end + dvpos, -dvpos);
10373 }
10374
10375 set_terminal_window (0);
10376 }
10377
10378 update_end (f);
10379 }
10380
10381 /* Shift reused rows of the current matrix to the right position.
10382 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
10383 text. */
10384 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
10385 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
10386 if (dvpos < 0)
10387 {
10388 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
10389 bottom_vpos, dvpos);
10390 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
10391 bottom_vpos, 0);
10392 }
10393 else if (dvpos > 0)
10394 {
10395 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
10396 bottom_vpos, dvpos);
10397 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
10398 first_unchanged_at_end_vpos + dvpos, 0);
10399 }
10400
10401 /* For frame-based redisplay, make sure that current frame and window
10402 matrix are in sync with respect to glyph memory. */
10403 if (!FRAME_WINDOW_P (f))
10404 sync_frame_with_window_matrix_rows (w);
10405
10406 /* Adjust buffer positions in reused rows. */
10407 if (delta)
10408 increment_matrix_positions (current_matrix,
10409 first_unchanged_at_end_vpos + dvpos,
10410 bottom_vpos, delta, delta_bytes);
10411
10412 /* Adjust Y positions. */
10413 if (dy)
10414 shift_glyph_matrix (w, current_matrix,
10415 first_unchanged_at_end_vpos + dvpos,
10416 bottom_vpos, dy);
10417
10418 if (first_unchanged_at_end_row)
10419 first_unchanged_at_end_row += dvpos;
10420
10421 /* If scrolling up, there may be some lines to display at the end of
10422 the window. */
10423 last_text_row_at_end = NULL;
10424 if (dy < 0)
10425 {
10426 /* Set last_row to the glyph row in the current matrix where the
10427 window end line is found. It has been moved up or down in
10428 the matrix by dvpos. */
10429 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
10430 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
10431
10432 /* If last_row is the window end line, it should display text. */
10433 xassert (last_row->displays_text_p);
10434
10435 /* If window end line was partially visible before, begin
10436 displaying at that line. Otherwise begin displaying with the
10437 line following it. */
10438 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
10439 {
10440 init_to_row_start (&it, w, last_row);
10441 it.vpos = last_vpos;
10442 it.current_y = last_row->y;
10443 }
10444 else
10445 {
10446 init_to_row_end (&it, w, last_row);
10447 it.vpos = 1 + last_vpos;
10448 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
10449 ++last_row;
10450 }
10451
10452 /* We may start in a continuation line. If so, we have to get
10453 the right continuation_lines_width and current_x. */
10454 it.continuation_lines_width = last_row->continuation_lines_width;
10455 it.hpos = it.current_x = 0;
10456
10457 /* Display the rest of the lines at the window end. */
10458 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
10459 while (it.current_y < it.last_visible_y
10460 && !fonts_changed_p)
10461 {
10462 /* Is it always sure that the display agrees with lines in
10463 the current matrix? I don't think so, so we mark rows
10464 displayed invalid in the current matrix by setting their
10465 enabled_p flag to zero. */
10466 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
10467 if (display_line (&it))
10468 last_text_row_at_end = it.glyph_row - 1;
10469 }
10470 }
10471
10472 /* Update window_end_pos and window_end_vpos. */
10473 if (first_unchanged_at_end_row
10474 && first_unchanged_at_end_row->y < it.last_visible_y
10475 && !last_text_row_at_end)
10476 {
10477 /* Window end line if one of the preserved rows from the current
10478 matrix. Set row to the last row displaying text in current
10479 matrix starting at first_unchanged_at_end_row, after
10480 scrolling. */
10481 xassert (first_unchanged_at_end_row->displays_text_p);
10482 row = find_last_row_displaying_text (w->current_matrix, &it,
10483 first_unchanged_at_end_row);
10484 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
10485
10486 XSETFASTINT (w->window_end_pos, Z - MATRIX_ROW_END_CHARPOS (row));
10487 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
10488 XSETFASTINT (w->window_end_vpos,
10489 MATRIX_ROW_VPOS (row, w->current_matrix));
10490 }
10491 else if (last_text_row_at_end)
10492 {
10493 XSETFASTINT (w->window_end_pos,
10494 Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
10495 w->window_end_bytepos
10496 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
10497 XSETFASTINT (w->window_end_vpos,
10498 MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
10499 }
10500 else if (last_text_row)
10501 {
10502 /* We have displayed either to the end of the window or at the
10503 end of the window, i.e. the last row with text is to be found
10504 in the desired matrix. */
10505 XSETFASTINT (w->window_end_pos,
10506 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10507 w->window_end_bytepos
10508 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10509 XSETFASTINT (w->window_end_vpos,
10510 MATRIX_ROW_VPOS (last_text_row, desired_matrix));
10511 }
10512 else if (first_unchanged_at_end_row == NULL
10513 && last_text_row == NULL
10514 && last_text_row_at_end == NULL)
10515 {
10516 /* Displayed to end of window, but no line containing text was
10517 displayed. Lines were deleted at the end of the window. */
10518 int vpos;
10519 int header_line_p = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
10520
10521 for (vpos = XFASTINT (w->window_end_vpos); vpos > 0; --vpos)
10522 if ((w->desired_matrix->rows[vpos + header_line_p].enabled_p
10523 && w->desired_matrix->rows[vpos + header_line_p].displays_text_p)
10524 || (!w->desired_matrix->rows[vpos + header_line_p].enabled_p
10525 && w->current_matrix->rows[vpos + header_line_p].displays_text_p))
10526 break;
10527
10528 w->window_end_vpos = make_number (vpos);
10529 }
10530 else
10531 abort ();
10532
10533 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
10534 debug_end_vpos = XFASTINT (w->window_end_vpos));
10535
10536 /* Record that display has not been completed. */
10537 w->window_end_valid = Qnil;
10538 w->desired_matrix->no_scrolling_p = 1;
10539 return 1;
10540 }
10541
10542
10543 \f
10544 /***********************************************************************
10545 More debugging support
10546 ***********************************************************************/
10547
10548 #if GLYPH_DEBUG
10549
10550 void dump_glyph_row P_ ((struct glyph_matrix *, int, int));
10551 static void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
10552
10553
10554 /* Dump the contents of glyph matrix MATRIX on stderr. If
10555 WITH_GLYPHS_P is non-zero, dump glyph contents as well. */
10556
10557 void
10558 dump_glyph_matrix (matrix, with_glyphs_p)
10559 struct glyph_matrix *matrix;
10560 int with_glyphs_p;
10561 {
10562 int i;
10563 for (i = 0; i < matrix->nrows; ++i)
10564 dump_glyph_row (matrix, i, with_glyphs_p);
10565 }
10566
10567
10568 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
10569 WITH_GLYPH_SP non-zero means dump glyph contents, too. */
10570
10571 void
10572 dump_glyph_row (matrix, vpos, with_glyphs_p)
10573 struct glyph_matrix *matrix;
10574 int vpos, with_glyphs_p;
10575 {
10576 struct glyph_row *row;
10577
10578 if (vpos < 0 || vpos >= matrix->nrows)
10579 return;
10580
10581 row = MATRIX_ROW (matrix, vpos);
10582
10583 fprintf (stderr, "Row Start End Used oEI><O\\CTZF X Y W\n");
10584 fprintf (stderr, "=============================================\n");
10585
10586 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",
10587 row - matrix->rows,
10588 MATRIX_ROW_START_CHARPOS (row),
10589 MATRIX_ROW_END_CHARPOS (row),
10590 row->used[TEXT_AREA],
10591 row->contains_overlapping_glyphs_p,
10592 row->enabled_p,
10593 row->inverse_p,
10594 row->truncated_on_left_p,
10595 row->truncated_on_right_p,
10596 row->overlay_arrow_p,
10597 row->continued_p,
10598 MATRIX_ROW_CONTINUATION_LINE_P (row),
10599 row->displays_text_p,
10600 row->ends_at_zv_p,
10601 row->fill_line_p,
10602 row->x,
10603 row->y,
10604 row->pixel_width);
10605 fprintf (stderr, "%9d %5d\n", row->start.overlay_string_index,
10606 row->end.overlay_string_index);
10607 fprintf (stderr, "%9d %5d\n",
10608 CHARPOS (row->start.string_pos),
10609 CHARPOS (row->end.string_pos));
10610 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
10611 row->end.dpvec_index);
10612
10613 if (with_glyphs_p)
10614 {
10615 struct glyph *glyph, *glyph_end;
10616 int prev_had_glyphs_p;
10617
10618 glyph = row->glyphs[TEXT_AREA];
10619 glyph_end = glyph + row->used[TEXT_AREA];
10620
10621 /* Glyph for a line end in text. */
10622 if (glyph == glyph_end && glyph->charpos > 0)
10623 ++glyph_end;
10624
10625 if (glyph < glyph_end)
10626 {
10627 fprintf (stderr, " Glyph Type Pos W Code C Face LR\n");
10628 prev_had_glyphs_p = 1;
10629 }
10630 else
10631 prev_had_glyphs_p = 0;
10632
10633 while (glyph < glyph_end)
10634 {
10635 if (glyph->type == CHAR_GLYPH)
10636 {
10637 fprintf (stderr,
10638 " %5d %4c %6d %3d 0x%05x %c %4d %1.1d%1.1d\n",
10639 glyph - row->glyphs[TEXT_AREA],
10640 'C',
10641 glyph->charpos,
10642 glyph->pixel_width,
10643 glyph->u.ch,
10644 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
10645 ? glyph->u.ch
10646 : '.'),
10647 glyph->face_id,
10648 glyph->left_box_line_p,
10649 glyph->right_box_line_p);
10650 }
10651 else if (glyph->type == STRETCH_GLYPH)
10652 {
10653 fprintf (stderr,
10654 " %5d %4c %6d %3d 0x%05x %c %4d %1.1d%1.1d\n",
10655 glyph - row->glyphs[TEXT_AREA],
10656 'S',
10657 glyph->charpos,
10658 glyph->pixel_width,
10659 0,
10660 '.',
10661 glyph->face_id,
10662 glyph->left_box_line_p,
10663 glyph->right_box_line_p);
10664 }
10665 else if (glyph->type == IMAGE_GLYPH)
10666 {
10667 fprintf (stderr,
10668 " %5d %4c %6d %3d 0x%05x %c %4d %1.1d%1.1d\n",
10669 glyph - row->glyphs[TEXT_AREA],
10670 'I',
10671 glyph->charpos,
10672 glyph->pixel_width,
10673 glyph->u.img_id,
10674 '.',
10675 glyph->face_id,
10676 glyph->left_box_line_p,
10677 glyph->right_box_line_p);
10678 }
10679 ++glyph;
10680 }
10681 }
10682 }
10683
10684
10685 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
10686 Sdump_glyph_matrix, 0, 1, "p",
10687 "Dump the current matrix of the selected window to stderr.\n\
10688 Shows contents of glyph row structures. With non-nil optional\n\
10689 parameter WITH-GLYPHS-P, dump glyphs as well.")
10690 (with_glyphs_p)
10691 {
10692 struct window *w = XWINDOW (selected_window);
10693 struct buffer *buffer = XBUFFER (w->buffer);
10694
10695 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
10696 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
10697 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
10698 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
10699 fprintf (stderr, "=============================================\n");
10700 dump_glyph_matrix (w->current_matrix, !NILP (with_glyphs_p));
10701 return Qnil;
10702 }
10703
10704
10705 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 1, "",
10706 "Dump glyph row ROW to stderr.")
10707 (row)
10708 Lisp_Object row;
10709 {
10710 CHECK_NUMBER (row, 0);
10711 dump_glyph_row (XWINDOW (selected_window)->current_matrix, XINT (row), 1);
10712 return Qnil;
10713 }
10714
10715
10716 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row,
10717 0, 0, "", "")
10718 ()
10719 {
10720 struct frame *sf = SELECTED_FRAME ();
10721 struct glyph_matrix *m = (XWINDOW (sf->tool_bar_window)
10722 ->current_matrix);
10723 dump_glyph_row (m, 0, 1);
10724 return Qnil;
10725 }
10726
10727
10728 DEFUN ("trace-redisplay-toggle", Ftrace_redisplay_toggle,
10729 Strace_redisplay_toggle, 0, 0, "",
10730 "Toggle tracing of redisplay.")
10731 ()
10732 {
10733 trace_redisplay_p = !trace_redisplay_p;
10734 return Qnil;
10735 }
10736
10737
10738 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, 1, "",
10739 "Print STRING to stderr.")
10740 (string)
10741 Lisp_Object string;
10742 {
10743 CHECK_STRING (string, 0);
10744 fprintf (stderr, "%s", XSTRING (string)->data);
10745 return Qnil;
10746 }
10747
10748 #endif /* GLYPH_DEBUG */
10749
10750
10751 \f
10752 /***********************************************************************
10753 Building Desired Matrix Rows
10754 ***********************************************************************/
10755
10756 /* Return a temporary glyph row holding the glyphs of an overlay
10757 arrow. Only used for non-window-redisplay windows. */
10758
10759 static struct glyph_row *
10760 get_overlay_arrow_glyph_row (w)
10761 struct window *w;
10762 {
10763 struct frame *f = XFRAME (WINDOW_FRAME (w));
10764 struct buffer *buffer = XBUFFER (w->buffer);
10765 struct buffer *old = current_buffer;
10766 unsigned char *arrow_string = XSTRING (Voverlay_arrow_string)->data;
10767 int arrow_len = XSTRING (Voverlay_arrow_string)->size;
10768 unsigned char *arrow_end = arrow_string + arrow_len;
10769 unsigned char *p;
10770 struct it it;
10771 int multibyte_p;
10772 int n_glyphs_before;
10773
10774 set_buffer_temp (buffer);
10775 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
10776 it.glyph_row->used[TEXT_AREA] = 0;
10777 SET_TEXT_POS (it.position, 0, 0);
10778
10779 multibyte_p = !NILP (buffer->enable_multibyte_characters);
10780 p = arrow_string;
10781 while (p < arrow_end)
10782 {
10783 Lisp_Object face, ilisp;
10784
10785 /* Get the next character. */
10786 if (multibyte_p)
10787 it.c = string_char_and_length (p, arrow_len, &it.len);
10788 else
10789 it.c = *p, it.len = 1;
10790 p += it.len;
10791
10792 /* Get its face. */
10793 XSETFASTINT (ilisp, p - arrow_string);
10794 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
10795 it.face_id = compute_char_face (f, it.c, face);
10796
10797 /* Compute its width, get its glyphs. */
10798 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
10799 SET_TEXT_POS (it.position, -1, -1);
10800 PRODUCE_GLYPHS (&it);
10801
10802 /* If this character doesn't fit any more in the line, we have
10803 to remove some glyphs. */
10804 if (it.current_x > it.last_visible_x)
10805 {
10806 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
10807 break;
10808 }
10809 }
10810
10811 set_buffer_temp (old);
10812 return it.glyph_row;
10813 }
10814
10815
10816 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
10817 glyphs are only inserted for terminal frames since we can't really
10818 win with truncation glyphs when partially visible glyphs are
10819 involved. Which glyphs to insert is determined by
10820 produce_special_glyphs. */
10821
10822 static void
10823 insert_left_trunc_glyphs (it)
10824 struct it *it;
10825 {
10826 struct it truncate_it;
10827 struct glyph *from, *end, *to, *toend;
10828
10829 xassert (!FRAME_WINDOW_P (it->f));
10830
10831 /* Get the truncation glyphs. */
10832 truncate_it = *it;
10833 truncate_it.current_x = 0;
10834 truncate_it.face_id = DEFAULT_FACE_ID;
10835 truncate_it.glyph_row = &scratch_glyph_row;
10836 truncate_it.glyph_row->used[TEXT_AREA] = 0;
10837 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
10838 truncate_it.object = make_number (0);
10839 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
10840
10841 /* Overwrite glyphs from IT with truncation glyphs. */
10842 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
10843 end = from + truncate_it.glyph_row->used[TEXT_AREA];
10844 to = it->glyph_row->glyphs[TEXT_AREA];
10845 toend = to + it->glyph_row->used[TEXT_AREA];
10846
10847 while (from < end)
10848 *to++ = *from++;
10849
10850 /* There may be padding glyphs left over. Remove them. */
10851 from = to;
10852 while (from < toend && CHAR_GLYPH_PADDING_P (*from))
10853 ++from;
10854 while (from < toend)
10855 *to++ = *from++;
10856
10857 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
10858 }
10859
10860
10861 /* Compute the pixel height and width of IT->glyph_row.
10862
10863 Most of the time, ascent and height of a display line will be equal
10864 to the max_ascent and max_height values of the display iterator
10865 structure. This is not the case if
10866
10867 1. We hit ZV without displaying anything. In this case, max_ascent
10868 and max_height will be zero.
10869
10870 2. We have some glyphs that don't contribute to the line height.
10871 (The glyph row flag contributes_to_line_height_p is for future
10872 pixmap extensions).
10873
10874 The first case is easily covered by using default values because in
10875 these cases, the line height does not really matter, except that it
10876 must not be zero. */
10877
10878 static void
10879 compute_line_metrics (it)
10880 struct it *it;
10881 {
10882 struct glyph_row *row = it->glyph_row;
10883 int area, i;
10884
10885 if (FRAME_WINDOW_P (it->f))
10886 {
10887 int i, header_line_height;
10888
10889 /* The line may consist of one space only, that was added to
10890 place the cursor on it. If so, the row's height hasn't been
10891 computed yet. */
10892 if (row->height == 0)
10893 {
10894 if (it->max_ascent + it->max_descent == 0)
10895 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
10896 row->ascent = it->max_ascent;
10897 row->height = it->max_ascent + it->max_descent;
10898 row->phys_ascent = it->max_phys_ascent;
10899 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
10900 }
10901
10902 /* Compute the width of this line. */
10903 row->pixel_width = row->x;
10904 for (i = 0; i < row->used[TEXT_AREA]; ++i)
10905 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
10906
10907 xassert (row->pixel_width >= 0);
10908 xassert (row->ascent >= 0 && row->height > 0);
10909
10910 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
10911 || MATRIX_ROW_OVERLAPS_PRED_P (row));
10912
10913 /* If first line's physical ascent is larger than its logical
10914 ascent, use the physical ascent, and make the row taller.
10915 This makes accented characters fully visible. */
10916 if (row == it->w->desired_matrix->rows
10917 && row->phys_ascent > row->ascent)
10918 {
10919 row->height += row->phys_ascent - row->ascent;
10920 row->ascent = row->phys_ascent;
10921 }
10922
10923 /* Compute how much of the line is visible. */
10924 row->visible_height = row->height;
10925
10926 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (it->w);
10927 if (row->y < header_line_height)
10928 row->visible_height -= header_line_height - row->y;
10929 else
10930 {
10931 int max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
10932 if (row->y + row->height > max_y)
10933 row->visible_height -= row->y + row->height - max_y;
10934 }
10935 }
10936 else
10937 {
10938 row->pixel_width = row->used[TEXT_AREA];
10939 row->ascent = row->phys_ascent = 0;
10940 row->height = row->phys_height = row->visible_height = 1;
10941 }
10942
10943 /* Compute a hash code for this row. */
10944 row->hash = 0;
10945 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
10946 for (i = 0; i < row->used[area]; ++i)
10947 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
10948 + row->glyphs[area][i].u.val
10949 + row->glyphs[area][i].face_id
10950 + row->glyphs[area][i].padding_p
10951 + (row->glyphs[area][i].type << 2));
10952
10953 it->max_ascent = it->max_descent = 0;
10954 it->max_phys_ascent = it->max_phys_descent = 0;
10955 }
10956
10957
10958 /* Append one space to the glyph row of iterator IT if doing a
10959 window-based redisplay. DEFAULT_FACE_P non-zero means let the
10960 space have the default face, otherwise let it have the same face as
10961 IT->face_id. Value is non-zero if a space was added.
10962
10963 This function is called to make sure that there is always one glyph
10964 at the end of a glyph row that the cursor can be set on under
10965 window-systems. (If there weren't such a glyph we would not know
10966 how wide and tall a box cursor should be displayed).
10967
10968 At the same time this space let's a nicely handle clearing to the
10969 end of the line if the row ends in italic text. */
10970
10971 static int
10972 append_space (it, default_face_p)
10973 struct it *it;
10974 int default_face_p;
10975 {
10976 if (FRAME_WINDOW_P (it->f))
10977 {
10978 int n = it->glyph_row->used[TEXT_AREA];
10979
10980 if (it->glyph_row->glyphs[TEXT_AREA] + n
10981 < it->glyph_row->glyphs[1 + TEXT_AREA])
10982 {
10983 /* Save some values that must not be changed. */
10984 int saved_x = it->current_x;
10985 struct text_pos saved_pos;
10986 int saved_what = it->what;
10987 int saved_face_id = it->face_id;
10988 Lisp_Object saved_object;
10989 struct face *face;
10990
10991 saved_object = it->object;
10992 saved_pos = it->position;
10993
10994 it->what = IT_CHARACTER;
10995 bzero (&it->position, sizeof it->position);
10996 it->object = make_number (0);
10997 it->c = ' ';
10998 it->len = 1;
10999
11000 if (default_face_p)
11001 it->face_id = DEFAULT_FACE_ID;
11002 face = FACE_FROM_ID (it->f, it->face_id);
11003 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
11004
11005 PRODUCE_GLYPHS (it);
11006
11007 it->current_x = saved_x;
11008 it->object = saved_object;
11009 it->position = saved_pos;
11010 it->what = saved_what;
11011 it->face_id = saved_face_id;
11012 return 1;
11013 }
11014 }
11015
11016 return 0;
11017 }
11018
11019
11020 /* Extend the face of the last glyph in the text area of IT->glyph_row
11021 to the end of the display line. Called from display_line.
11022 If the glyph row is empty, add a space glyph to it so that we
11023 know the face to draw. Set the glyph row flag fill_line_p. */
11024
11025 static void
11026 extend_face_to_end_of_line (it)
11027 struct it *it;
11028 {
11029 struct face *face;
11030 struct frame *f = it->f;
11031
11032 /* If line is already filled, do nothing. */
11033 if (it->current_x >= it->last_visible_x)
11034 return;
11035
11036 /* Face extension extends the background and box of IT->face_id
11037 to the end of the line. If the background equals the background
11038 of the frame, we haven't to do anything. */
11039 face = FACE_FROM_ID (f, it->face_id);
11040 if (FRAME_WINDOW_P (f)
11041 && face->box == FACE_NO_BOX
11042 && face->background == FRAME_BACKGROUND_PIXEL (f)
11043 && !face->stipple)
11044 return;
11045
11046 /* Set the glyph row flag indicating that the face of the last glyph
11047 in the text area has to be drawn to the end of the text area. */
11048 it->glyph_row->fill_line_p = 1;
11049
11050 /* If current character of IT is not ASCII, make sure we have the
11051 ASCII face. This will be automatically undone the next time
11052 get_next_display_element returns a multibyte character. Note
11053 that the character will always be single byte in unibyte text. */
11054 if (!SINGLE_BYTE_CHAR_P (it->c))
11055 {
11056 it->face_id = FACE_FOR_CHAR (f, face, 0);
11057 }
11058
11059 if (FRAME_WINDOW_P (f))
11060 {
11061 /* If the row is empty, add a space with the current face of IT,
11062 so that we know which face to draw. */
11063 if (it->glyph_row->used[TEXT_AREA] == 0)
11064 {
11065 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
11066 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
11067 it->glyph_row->used[TEXT_AREA] = 1;
11068 }
11069 }
11070 else
11071 {
11072 /* Save some values that must not be changed. */
11073 int saved_x = it->current_x;
11074 struct text_pos saved_pos;
11075 Lisp_Object saved_object;
11076 int saved_what = it->what;
11077
11078 saved_object = it->object;
11079 saved_pos = it->position;
11080
11081 it->what = IT_CHARACTER;
11082 bzero (&it->position, sizeof it->position);
11083 it->object = make_number (0);
11084 it->c = ' ';
11085 it->len = 1;
11086
11087 PRODUCE_GLYPHS (it);
11088
11089 while (it->current_x <= it->last_visible_x)
11090 PRODUCE_GLYPHS (it);
11091
11092 /* Don't count these blanks really. It would let us insert a left
11093 truncation glyph below and make us set the cursor on them, maybe. */
11094 it->current_x = saved_x;
11095 it->object = saved_object;
11096 it->position = saved_pos;
11097 it->what = saved_what;
11098 }
11099 }
11100
11101
11102 /* Value is non-zero if text starting at CHARPOS in current_buffer is
11103 trailing whitespace. */
11104
11105 static int
11106 trailing_whitespace_p (charpos)
11107 int charpos;
11108 {
11109 int bytepos = CHAR_TO_BYTE (charpos);
11110 int c = 0;
11111
11112 while (bytepos < ZV_BYTE
11113 && (c = FETCH_CHAR (bytepos),
11114 c == ' ' || c == '\t'))
11115 ++bytepos;
11116
11117 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
11118 {
11119 if (bytepos != PT_BYTE)
11120 return 1;
11121 }
11122 return 0;
11123 }
11124
11125
11126 /* Highlight trailing whitespace, if any, in ROW. */
11127
11128 void
11129 highlight_trailing_whitespace (f, row)
11130 struct frame *f;
11131 struct glyph_row *row;
11132 {
11133 int used = row->used[TEXT_AREA];
11134
11135 if (used)
11136 {
11137 struct glyph *start = row->glyphs[TEXT_AREA];
11138 struct glyph *glyph = start + used - 1;
11139
11140 /* Skip over the space glyph inserted to display the
11141 cursor at the end of a line. */
11142 if (glyph->type == CHAR_GLYPH
11143 && glyph->u.ch == ' '
11144 && INTEGERP (glyph->object))
11145 --glyph;
11146
11147 /* If last glyph is a space or stretch, and it's trailing
11148 whitespace, set the face of all trailing whitespace glyphs in
11149 IT->glyph_row to `trailing-whitespace'. */
11150 if (glyph >= start
11151 && BUFFERP (glyph->object)
11152 && (glyph->type == STRETCH_GLYPH
11153 || (glyph->type == CHAR_GLYPH
11154 && glyph->u.ch == ' '))
11155 && trailing_whitespace_p (glyph->charpos))
11156 {
11157 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
11158
11159 while (glyph >= start
11160 && BUFFERP (glyph->object)
11161 && (glyph->type == STRETCH_GLYPH
11162 || (glyph->type == CHAR_GLYPH
11163 && glyph->u.ch == ' ')))
11164 (glyph--)->face_id = face_id;
11165 }
11166 }
11167 }
11168
11169
11170 /* Construct the glyph row IT->glyph_row in the desired matrix of
11171 IT->w from text at the current position of IT. See dispextern.h
11172 for an overview of struct it. Value is non-zero if
11173 IT->glyph_row displays text, as opposed to a line displaying ZV
11174 only. */
11175
11176 static int
11177 display_line (it)
11178 struct it *it;
11179 {
11180 struct glyph_row *row = it->glyph_row;
11181
11182 /* We always start displaying at hpos zero even if hscrolled. */
11183 xassert (it->hpos == 0 && it->current_x == 0);
11184
11185 /* We must not display in a row that's not a text row. */
11186 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
11187 < it->w->desired_matrix->nrows);
11188
11189 /* Is IT->w showing the region? */
11190 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
11191
11192 /* Clear the result glyph row and enable it. */
11193 prepare_desired_row (row);
11194
11195 row->y = it->current_y;
11196 row->start = it->current;
11197 row->continuation_lines_width = it->continuation_lines_width;
11198 row->displays_text_p = 1;
11199
11200 /* Arrange the overlays nicely for our purposes. Usually, we call
11201 display_line on only one line at a time, in which case this
11202 can't really hurt too much, or we call it on lines which appear
11203 one after another in the buffer, in which case all calls to
11204 recenter_overlay_lists but the first will be pretty cheap. */
11205 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
11206
11207 /* Move over display elements that are not visible because we are
11208 hscrolled. This may stop at an x-position < IT->first_visible_x
11209 if the first glyph is partially visible or if we hit a line end. */
11210 if (it->current_x < it->first_visible_x)
11211 move_it_in_display_line_to (it, ZV, it->first_visible_x,
11212 MOVE_TO_POS | MOVE_TO_X);
11213
11214 /* Get the initial row height. This is either the height of the
11215 text hscrolled, if there is any, or zero. */
11216 row->ascent = it->max_ascent;
11217 row->height = it->max_ascent + it->max_descent;
11218 row->phys_ascent = it->max_phys_ascent;
11219 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
11220
11221 /* Loop generating characters. The loop is left with IT on the next
11222 character to display. */
11223 while (1)
11224 {
11225 int n_glyphs_before, hpos_before, x_before;
11226 int x, i, nglyphs;
11227 int ascent, descent, phys_ascent, phys_descent;
11228
11229 /* Retrieve the next thing to display. Value is zero if end of
11230 buffer reached. */
11231 if (!get_next_display_element (it))
11232 {
11233 /* Maybe add a space at the end of this line that is used to
11234 display the cursor there under X. Set the charpos of the
11235 first glyph of blank lines not corresponding to any text
11236 to -1. */
11237 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
11238 || row->used[TEXT_AREA] == 0)
11239 {
11240 row->glyphs[TEXT_AREA]->charpos = -1;
11241 row->displays_text_p = 0;
11242
11243 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines))
11244 row->indicate_empty_line_p = 1;
11245 }
11246
11247 it->continuation_lines_width = 0;
11248 row->ends_at_zv_p = 1;
11249 break;
11250 }
11251
11252 /* Now, get the metrics of what we want to display. This also
11253 generates glyphs in `row' (which is IT->glyph_row). */
11254 n_glyphs_before = row->used[TEXT_AREA];
11255 x = it->current_x;
11256
11257 /* Remember the line height so far in case the next element doesn't
11258 fit on the line. */
11259 if (!it->truncate_lines_p)
11260 {
11261 ascent = it->max_ascent;
11262 descent = it->max_descent;
11263 phys_ascent = it->max_phys_ascent;
11264 phys_descent = it->max_phys_descent;
11265 }
11266
11267 PRODUCE_GLYPHS (it);
11268
11269 /* If this display element was in marginal areas, continue with
11270 the next one. */
11271 if (it->area != TEXT_AREA)
11272 {
11273 row->ascent = max (row->ascent, it->max_ascent);
11274 row->height = max (row->height, it->max_ascent + it->max_descent);
11275 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
11276 row->phys_height = max (row->phys_height,
11277 it->max_phys_ascent + it->max_phys_descent);
11278 set_iterator_to_next (it);
11279 continue;
11280 }
11281
11282 /* Does the display element fit on the line? If we truncate
11283 lines, we should draw past the right edge of the window. If
11284 we don't truncate, we want to stop so that we can display the
11285 continuation glyph before the right margin. If lines are
11286 continued, there are two possible strategies for characters
11287 resulting in more than 1 glyph (e.g. tabs): Display as many
11288 glyphs as possible in this line and leave the rest for the
11289 continuation line, or display the whole element in the next
11290 line. Original redisplay did the former, so we do it also. */
11291 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11292 hpos_before = it->hpos;
11293 x_before = x;
11294
11295 if (nglyphs == 1
11296 && it->current_x < it->last_visible_x)
11297 {
11298 ++it->hpos;
11299 row->ascent = max (row->ascent, it->max_ascent);
11300 row->height = max (row->height, it->max_ascent + it->max_descent);
11301 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
11302 row->phys_height = max (row->phys_height,
11303 it->max_phys_ascent + it->max_phys_descent);
11304 if (it->current_x - it->pixel_width < it->first_visible_x)
11305 row->x = x - it->first_visible_x;
11306 }
11307 else
11308 {
11309 int new_x;
11310 struct glyph *glyph;
11311
11312 for (i = 0; i < nglyphs; ++i, x = new_x)
11313 {
11314 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11315 new_x = x + glyph->pixel_width;
11316
11317 if (/* Lines are continued. */
11318 !it->truncate_lines_p
11319 && (/* Glyph doesn't fit on the line. */
11320 new_x > it->last_visible_x
11321 /* Or it fits exactly on a window system frame. */
11322 || (new_x == it->last_visible_x
11323 && FRAME_WINDOW_P (it->f))))
11324 {
11325 /* End of a continued line. */
11326
11327 if (it->hpos == 0
11328 || (new_x == it->last_visible_x
11329 && FRAME_WINDOW_P (it->f)))
11330 {
11331 /* Current glyph is the only one on the line or
11332 fits exactly on the line. We must continue
11333 the line because we can't draw the cursor
11334 after the glyph. */
11335 row->continued_p = 1;
11336 it->current_x = new_x;
11337 it->continuation_lines_width += new_x;
11338 ++it->hpos;
11339 if (i == nglyphs - 1)
11340 set_iterator_to_next (it);
11341 }
11342 else
11343 {
11344 /* Display element draws past the right edge of
11345 the window. Restore positions to values
11346 before the element. The next line starts
11347 with current_x before the glyph that could
11348 not be displayed, so that TAB works right. */
11349 row->used[TEXT_AREA] = n_glyphs_before + i;
11350
11351 /* Display continuation glyphs. */
11352 if (!FRAME_WINDOW_P (it->f))
11353 produce_special_glyphs (it, IT_CONTINUATION);
11354 row->continued_p = 1;
11355
11356 it->current_x = x;
11357 it->continuation_lines_width += x;
11358
11359 /* Restore the height to what it was before the
11360 element not fitting on the line. */
11361 it->max_ascent = ascent;
11362 it->max_descent = descent;
11363 it->max_phys_ascent = phys_ascent;
11364 it->max_phys_descent = phys_descent;
11365 }
11366
11367 break;
11368 }
11369 else if (new_x > it->first_visible_x)
11370 {
11371 /* Increment number of glyphs actually displayed. */
11372 ++it->hpos;
11373
11374 if (x < it->first_visible_x)
11375 /* Glyph is partially visible, i.e. row starts at
11376 negative X position. */
11377 row->x = x - it->first_visible_x;
11378 }
11379 else
11380 {
11381 /* Glyph is completely off the left margin of the
11382 window. This should not happen because of the
11383 move_it_in_display_line at the start of
11384 this function. */
11385 abort ();
11386 }
11387 }
11388
11389 row->ascent = max (row->ascent, it->max_ascent);
11390 row->height = max (row->height, it->max_ascent + it->max_descent);
11391 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
11392 row->phys_height = max (row->phys_height,
11393 it->max_phys_ascent + it->max_phys_descent);
11394
11395 /* End of this display line if row is continued. */
11396 if (row->continued_p)
11397 break;
11398 }
11399
11400 /* Is this a line end? If yes, we're also done, after making
11401 sure that a non-default face is extended up to the right
11402 margin of the window. */
11403 if (ITERATOR_AT_END_OF_LINE_P (it))
11404 {
11405 int used_before = row->used[TEXT_AREA];
11406
11407 /* Add a space at the end of the line that is used to
11408 display the cursor there. */
11409 append_space (it, 0);
11410
11411 /* Extend the face to the end of the line. */
11412 extend_face_to_end_of_line (it);
11413
11414 /* Make sure we have the position. */
11415 if (used_before == 0)
11416 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
11417
11418 /* Consume the line end. This skips over invisible lines. */
11419 set_iterator_to_next (it);
11420 it->continuation_lines_width = 0;
11421 break;
11422 }
11423
11424 /* Proceed with next display element. Note that this skips
11425 over lines invisible because of selective display. */
11426 set_iterator_to_next (it);
11427
11428 /* If we truncate lines, we are done when the last displayed
11429 glyphs reach past the right margin of the window. */
11430 if (it->truncate_lines_p
11431 && (FRAME_WINDOW_P (it->f)
11432 ? (it->current_x >= it->last_visible_x)
11433 : (it->current_x > it->last_visible_x)))
11434 {
11435 /* Maybe add truncation glyphs. */
11436 if (!FRAME_WINDOW_P (it->f))
11437 {
11438 --it->glyph_row->used[TEXT_AREA];
11439 produce_special_glyphs (it, IT_TRUNCATION);
11440 }
11441
11442 row->truncated_on_right_p = 1;
11443 it->continuation_lines_width = 0;
11444 reseat_at_next_visible_line_start (it, 0);
11445 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
11446 it->hpos = hpos_before;
11447 it->current_x = x_before;
11448 break;
11449 }
11450 }
11451
11452 /* If line is not empty and hscrolled, maybe insert truncation glyphs
11453 at the left window margin. */
11454 if (it->first_visible_x
11455 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
11456 {
11457 if (!FRAME_WINDOW_P (it->f))
11458 insert_left_trunc_glyphs (it);
11459 row->truncated_on_left_p = 1;
11460 }
11461
11462 /* If the start of this line is the overlay arrow-position, then
11463 mark this glyph row as the one containing the overlay arrow.
11464 This is clearly a mess with variable size fonts. It would be
11465 better to let it be displayed like cursors under X. */
11466 if (MARKERP (Voverlay_arrow_position)
11467 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
11468 && (MATRIX_ROW_START_CHARPOS (row)
11469 == marker_position (Voverlay_arrow_position))
11470 && STRINGP (Voverlay_arrow_string)
11471 && ! overlay_arrow_seen)
11472 {
11473 /* Overlay arrow in window redisplay is a bitmap. */
11474 if (!FRAME_WINDOW_P (it->f))
11475 {
11476 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
11477 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
11478 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
11479 struct glyph *p = row->glyphs[TEXT_AREA];
11480 struct glyph *p2, *end;
11481
11482 /* Copy the arrow glyphs. */
11483 while (glyph < arrow_end)
11484 *p++ = *glyph++;
11485
11486 /* Throw away padding glyphs. */
11487 p2 = p;
11488 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
11489 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
11490 ++p2;
11491 if (p2 > p)
11492 {
11493 while (p2 < end)
11494 *p++ = *p2++;
11495 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
11496 }
11497 }
11498
11499 overlay_arrow_seen = 1;
11500 row->overlay_arrow_p = 1;
11501 }
11502
11503 /* Compute pixel dimensions of this line. */
11504 compute_line_metrics (it);
11505
11506 /* Remember the position at which this line ends. */
11507 row->end = it->current;
11508
11509 /* Maybe set the cursor. If you change this, it's probably a good
11510 idea to also change the code in redisplay_window for cursor
11511 movement in an unchanged window. */
11512 if (it->w->cursor.vpos < 0
11513 && PT >= MATRIX_ROW_START_CHARPOS (row)
11514 && MATRIX_ROW_END_CHARPOS (row) >= PT
11515 && !(MATRIX_ROW_END_CHARPOS (row) == PT
11516 && (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
11517 || !row->ends_at_zv_p)))
11518 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
11519
11520 /* Highlight trailing whitespace. */
11521 if (!NILP (Vshow_trailing_whitespace))
11522 highlight_trailing_whitespace (it->f, it->glyph_row);
11523
11524 /* Prepare for the next line. This line starts horizontally at (X
11525 HPOS) = (0 0). Vertical positions are incremented. As a
11526 convenience for the caller, IT->glyph_row is set to the next
11527 row to be used. */
11528 it->current_x = it->hpos = 0;
11529 it->current_y += row->height;
11530 ++it->vpos;
11531 ++it->glyph_row;
11532 return row->displays_text_p;
11533 }
11534
11535
11536 \f
11537 /***********************************************************************
11538 Menu Bar
11539 ***********************************************************************/
11540
11541 /* Redisplay the menu bar in the frame for window W.
11542
11543 The menu bar of X frames that don't have X toolkit support is
11544 displayed in a special window W->frame->menu_bar_window.
11545
11546 The menu bar of terminal frames is treated specially as far as
11547 glyph matrices are concerned. Menu bar lines are not part of
11548 windows, so the update is done directly on the frame matrix rows
11549 for the menu bar. */
11550
11551 static void
11552 display_menu_bar (w)
11553 struct window *w;
11554 {
11555 struct frame *f = XFRAME (WINDOW_FRAME (w));
11556 struct it it;
11557 Lisp_Object items;
11558 int i;
11559
11560 /* Don't do all this for graphical frames. */
11561 #ifdef HAVE_NTGUI
11562 if (!NILP (Vwindow_system))
11563 return;
11564 #endif
11565 #ifdef USE_X_TOOLKIT
11566 if (FRAME_X_P (f))
11567 return;
11568 #endif
11569
11570 #ifdef USE_X_TOOLKIT
11571 xassert (!FRAME_WINDOW_P (f));
11572 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
11573 it.first_visible_x = 0;
11574 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
11575 #else /* not USE_X_TOOLKIT */
11576 if (FRAME_WINDOW_P (f))
11577 {
11578 /* Menu bar lines are displayed in the desired matrix of the
11579 dummy window menu_bar_window. */
11580 struct window *menu_w;
11581 xassert (WINDOWP (f->menu_bar_window));
11582 menu_w = XWINDOW (f->menu_bar_window);
11583 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
11584 MENU_FACE_ID);
11585 it.first_visible_x = 0;
11586 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
11587 }
11588 else
11589 {
11590 /* This is a TTY frame, i.e. character hpos/vpos are used as
11591 pixel x/y. */
11592 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
11593 MENU_FACE_ID);
11594 it.first_visible_x = 0;
11595 it.last_visible_x = FRAME_WIDTH (f);
11596 }
11597 #endif /* not USE_X_TOOLKIT */
11598
11599 /* Clear all rows of the menu bar. */
11600 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
11601 {
11602 struct glyph_row *row = it.glyph_row + i;
11603 clear_glyph_row (row);
11604 row->enabled_p = 1;
11605 row->full_width_p = 1;
11606 }
11607
11608 /* Make the first line of the menu bar appear in reverse video. */
11609 it.glyph_row->inverse_p = mode_line_inverse_video != 0;
11610
11611 /* Display all items of the menu bar. */
11612 items = FRAME_MENU_BAR_ITEMS (it.f);
11613 for (i = 0; i < XVECTOR (items)->size; i += 4)
11614 {
11615 Lisp_Object string;
11616
11617 /* Stop at nil string. */
11618 string = XVECTOR (items)->contents[i + 1];
11619 if (NILP (string))
11620 break;
11621
11622 /* Remember where item was displayed. */
11623 XSETFASTINT (XVECTOR (items)->contents[i + 3], it.hpos);
11624
11625 /* Display the item, pad with one space. */
11626 if (it.current_x < it.last_visible_x)
11627 display_string (NULL, string, Qnil, 0, 0, &it,
11628 XSTRING (string)->size + 1, 0, 0, -1);
11629 }
11630
11631 /* Fill out the line with spaces. */
11632 if (it.current_x < it.last_visible_x)
11633 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
11634
11635 /* Compute the total height of the lines. */
11636 compute_line_metrics (&it);
11637 }
11638
11639
11640 \f
11641 /***********************************************************************
11642 Mode Line
11643 ***********************************************************************/
11644
11645 /* Display the mode and/or top line of window W. */
11646
11647 static void
11648 display_mode_lines (w)
11649 struct window *w;
11650 {
11651 /* These will be set while the mode line specs are processed. */
11652 line_number_displayed = 0;
11653 w->column_number_displayed = Qnil;
11654
11655 if (WINDOW_WANTS_MODELINE_P (w))
11656 display_mode_line (w, MODE_LINE_FACE_ID,
11657 current_buffer->mode_line_format);
11658
11659 if (WINDOW_WANTS_HEADER_LINE_P (w))
11660 display_mode_line (w, HEADER_LINE_FACE_ID,
11661 current_buffer->header_line_format);
11662 }
11663
11664
11665 /* Display mode or top line of window W. FACE_ID specifies which line
11666 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
11667 FORMAT is the mode line format to display. */
11668
11669 static void
11670 display_mode_line (w, face_id, format)
11671 struct window *w;
11672 enum face_id face_id;
11673 Lisp_Object format;
11674 {
11675 struct it it;
11676 struct face *face;
11677
11678 init_iterator (&it, w, -1, -1, NULL, face_id);
11679 prepare_desired_row (it.glyph_row);
11680
11681 /* Temporarily make frame's keyboard the current kboard so that
11682 kboard-local variables in the mode_line_format will get the right
11683 values. */
11684 push_frame_kboard (it.f);
11685 display_mode_element (&it, 0, 0, 0, format);
11686 pop_frame_kboard ();
11687
11688 /* Fill up with spaces. */
11689 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
11690
11691 compute_line_metrics (&it);
11692 it.glyph_row->full_width_p = 1;
11693 it.glyph_row->mode_line_p = 1;
11694 it.glyph_row->inverse_p = mode_line_inverse_video != 0;
11695 it.glyph_row->continued_p = 0;
11696 it.glyph_row->truncated_on_left_p = 0;
11697 it.glyph_row->truncated_on_right_p = 0;
11698
11699 /* Make a 3D mode-line have a shadow at its right end. */
11700 face = FACE_FROM_ID (it.f, face_id);
11701 extend_face_to_end_of_line (&it);
11702 if (face->box != FACE_NO_BOX)
11703 {
11704 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
11705 + it.glyph_row->used[TEXT_AREA] - 1);
11706 last->right_box_line_p = 1;
11707 }
11708 }
11709
11710
11711 /* Contribute ELT to the mode line for window IT->w. How it
11712 translates into text depends on its data type.
11713
11714 IT describes the display environment in which we display, as usual.
11715
11716 DEPTH is the depth in recursion. It is used to prevent
11717 infinite recursion here.
11718
11719 FIELD_WIDTH is the number of characters the display of ELT should
11720 occupy in the mode line, and PRECISION is the maximum number of
11721 characters to display from ELT's representation. See
11722 display_string for details. *
11723
11724 Returns the hpos of the end of the text generated by ELT. */
11725
11726 static int
11727 display_mode_element (it, depth, field_width, precision, elt)
11728 struct it *it;
11729 int depth;
11730 int field_width, precision;
11731 Lisp_Object elt;
11732 {
11733 int n = 0, field, prec;
11734
11735 tail_recurse:
11736 if (depth > 10)
11737 goto invalid;
11738
11739 depth++;
11740
11741 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
11742 {
11743 case Lisp_String:
11744 {
11745 /* A string: output it and check for %-constructs within it. */
11746 unsigned char c;
11747 unsigned char *this = XSTRING (elt)->data;
11748 unsigned char *lisp_string = this;
11749
11750 while ((precision <= 0 || n < precision)
11751 && *this
11752 && (frame_title_ptr
11753 || it->current_x < it->last_visible_x))
11754 {
11755 unsigned char *last = this;
11756
11757 /* Advance to end of string or next format specifier. */
11758 while ((c = *this++) != '\0' && c != '%')
11759 ;
11760
11761 if (this - 1 != last)
11762 {
11763 /* Output to end of string or up to '%'. Field width
11764 is length of string. Don't output more than
11765 PRECISION allows us. */
11766 prec = --this - last;
11767 if (precision > 0 && prec > precision - n)
11768 prec = precision - n;
11769
11770 if (frame_title_ptr)
11771 n += store_frame_title (last, prec, prec);
11772 else
11773 n += display_string (NULL, elt, Qnil, 0, last - lisp_string,
11774 it, 0, prec, 0, -1);
11775 }
11776 else /* c == '%' */
11777 {
11778 unsigned char *percent_position = this;
11779
11780 /* Get the specified minimum width. Zero means
11781 don't pad. */
11782 field = 0;
11783 while ((c = *this++) >= '0' && c <= '9')
11784 field = field * 10 + c - '0';
11785
11786 /* Don't pad beyond the total padding allowed. */
11787 if (field_width - n > 0 && field > field_width - n)
11788 field = field_width - n;
11789
11790 /* Note that either PRECISION <= 0 or N < PRECISION. */
11791 prec = precision - n;
11792
11793 if (c == 'M')
11794 n += display_mode_element (it, depth, field, prec,
11795 Vglobal_mode_string);
11796 else if (c != 0)
11797 {
11798 unsigned char *spec
11799 = decode_mode_spec (it->w, c, field, prec);
11800
11801 if (frame_title_ptr)
11802 n += store_frame_title (spec, field, prec);
11803 else
11804 {
11805 int nglyphs_before
11806 = it->glyph_row->used[TEXT_AREA];
11807 int charpos
11808 = percent_position - XSTRING (elt)->data;
11809 int nwritten
11810 = display_string (spec, Qnil, elt, charpos, 0, it,
11811 field, prec, 0, -1);
11812
11813 /* Assign to the glyphs written above the
11814 string where the `%x' came from, position
11815 of the `%'. */
11816 if (nwritten > 0)
11817 {
11818 struct glyph *glyph
11819 = (it->glyph_row->glyphs[TEXT_AREA]
11820 + nglyphs_before);
11821 int i;
11822
11823 for (i = 0; i < nwritten; ++i)
11824 {
11825 glyph[i].object = elt;
11826 glyph[i].charpos = charpos;
11827 }
11828
11829 n += nwritten;
11830 }
11831 }
11832 }
11833 }
11834 }
11835 }
11836 break;
11837
11838 case Lisp_Symbol:
11839 /* A symbol: process the value of the symbol recursively
11840 as if it appeared here directly. Avoid error if symbol void.
11841 Special case: if value of symbol is a string, output the string
11842 literally. */
11843 {
11844 register Lisp_Object tem;
11845 tem = Fboundp (elt);
11846 if (!NILP (tem))
11847 {
11848 tem = Fsymbol_value (elt);
11849 /* If value is a string, output that string literally:
11850 don't check for % within it. */
11851 if (STRINGP (tem))
11852 {
11853 prec = XSTRING (tem)->size;
11854 if (precision > 0 && prec > precision - n)
11855 prec = precision - n;
11856 if (frame_title_ptr)
11857 n += store_frame_title (XSTRING (tem)->data, -1, prec);
11858 else
11859 n += display_string (NULL, tem, Qnil, 0, 0, it,
11860 0, prec, 0, -1);
11861 }
11862 else if (!EQ (tem, elt))
11863 {
11864 /* Give up right away for nil or t. */
11865 elt = tem;
11866 goto tail_recurse;
11867 }
11868 }
11869 }
11870 break;
11871
11872 case Lisp_Cons:
11873 {
11874 register Lisp_Object car, tem;
11875
11876 /* A cons cell: three distinct cases.
11877 If first element is a string or a cons, process all the elements
11878 and effectively concatenate them.
11879 If first element is a negative number, truncate displaying cdr to
11880 at most that many characters. If positive, pad (with spaces)
11881 to at least that many characters.
11882 If first element is a symbol, process the cadr or caddr recursively
11883 according to whether the symbol's value is non-nil or nil. */
11884 car = XCAR (elt);
11885 if (EQ (car, QCeval) && CONSP (XCDR (elt)))
11886 {
11887 /* An element of the form (:eval FORM) means evaluate FORM
11888 and use the result as mode line elements. */
11889 struct gcpro gcpro1;
11890 Lisp_Object spec;
11891
11892 spec = eval_form (XCAR (XCDR (elt)));
11893 GCPRO1 (spec);
11894 n += display_mode_element (it, depth, field_width - n,
11895 precision - n, spec);
11896 UNGCPRO;
11897 }
11898 else if (SYMBOLP (car))
11899 {
11900 tem = Fboundp (car);
11901 elt = XCDR (elt);
11902 if (!CONSP (elt))
11903 goto invalid;
11904 /* elt is now the cdr, and we know it is a cons cell.
11905 Use its car if CAR has a non-nil value. */
11906 if (!NILP (tem))
11907 {
11908 tem = Fsymbol_value (car);
11909 if (!NILP (tem))
11910 {
11911 elt = XCAR (elt);
11912 goto tail_recurse;
11913 }
11914 }
11915 /* Symbol's value is nil (or symbol is unbound)
11916 Get the cddr of the original list
11917 and if possible find the caddr and use that. */
11918 elt = XCDR (elt);
11919 if (NILP (elt))
11920 break;
11921 else if (!CONSP (elt))
11922 goto invalid;
11923 elt = XCAR (elt);
11924 goto tail_recurse;
11925 }
11926 else if (INTEGERP (car))
11927 {
11928 register int lim = XINT (car);
11929 elt = XCDR (elt);
11930 if (lim < 0)
11931 {
11932 /* Negative int means reduce maximum width. */
11933 if (precision <= 0)
11934 precision = -lim;
11935 else
11936 precision = min (precision, -lim);
11937 }
11938 else if (lim > 0)
11939 {
11940 /* Padding specified. Don't let it be more than
11941 current maximum. */
11942 if (precision > 0)
11943 lim = min (precision, lim);
11944
11945 /* If that's more padding than already wanted, queue it.
11946 But don't reduce padding already specified even if
11947 that is beyond the current truncation point. */
11948 field_width = max (lim, field_width);
11949 }
11950 goto tail_recurse;
11951 }
11952 else if (STRINGP (car) || CONSP (car))
11953 {
11954 register int limit = 50;
11955 /* Limit is to protect against circular lists. */
11956 while (CONSP (elt)
11957 && --limit > 0
11958 && (precision <= 0 || n < precision))
11959 {
11960 n += display_mode_element (it, depth, field_width - n,
11961 precision - n, XCAR (elt));
11962 elt = XCDR (elt);
11963 }
11964 }
11965 }
11966 break;
11967
11968 default:
11969 invalid:
11970 if (frame_title_ptr)
11971 n += store_frame_title ("*invalid*", 0, precision - n);
11972 else
11973 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
11974 precision - n, 0, 0);
11975 return n;
11976 }
11977
11978 /* Pad to FIELD_WIDTH. */
11979 if (field_width > 0 && n < field_width)
11980 {
11981 if (frame_title_ptr)
11982 n += store_frame_title ("", field_width - n, 0);
11983 else
11984 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
11985 0, 0, 0);
11986 }
11987
11988 return n;
11989 }
11990
11991
11992 /* Write a null-terminated, right justified decimal representation of
11993 the positive integer D to BUF using a minimal field width WIDTH. */
11994
11995 static void
11996 pint2str (buf, width, d)
11997 register char *buf;
11998 register int width;
11999 register int d;
12000 {
12001 register char *p = buf;
12002
12003 if (d <= 0)
12004 *p++ = '0';
12005 else
12006 {
12007 while (d > 0)
12008 {
12009 *p++ = d % 10 + '0';
12010 d /= 10;
12011 }
12012 }
12013
12014 for (width -= (int) (p - buf); width > 0; --width)
12015 *p++ = ' ';
12016 *p-- = '\0';
12017 while (p > buf)
12018 {
12019 d = *buf;
12020 *buf++ = *p;
12021 *p-- = d;
12022 }
12023 }
12024
12025 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
12026 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
12027 type of CODING_SYSTEM. Return updated pointer into BUF. */
12028
12029 static unsigned char invalid_eol_type[] = "(*invalid*)";
12030
12031 static char *
12032 decode_mode_spec_coding (coding_system, buf, eol_flag)
12033 Lisp_Object coding_system;
12034 register char *buf;
12035 int eol_flag;
12036 {
12037 Lisp_Object val;
12038 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
12039 unsigned char *eol_str;
12040 int eol_str_len;
12041 /* The EOL conversion we are using. */
12042 Lisp_Object eoltype;
12043
12044 val = Fget (coding_system, Qcoding_system);
12045
12046 if (!VECTORP (val)) /* Not yet decided. */
12047 {
12048 if (multibyte)
12049 *buf++ = '-';
12050 if (eol_flag)
12051 eoltype = eol_mnemonic_undecided;
12052 /* Don't mention EOL conversion if it isn't decided. */
12053 }
12054 else
12055 {
12056 Lisp_Object eolvalue;
12057
12058 eolvalue = Fget (coding_system, Qeol_type);
12059
12060 if (multibyte)
12061 *buf++ = XFASTINT (XVECTOR (val)->contents[1]);
12062
12063 if (eol_flag)
12064 {
12065 /* The EOL conversion that is normal on this system. */
12066
12067 if (NILP (eolvalue)) /* Not yet decided. */
12068 eoltype = eol_mnemonic_undecided;
12069 else if (VECTORP (eolvalue)) /* Not yet decided. */
12070 eoltype = eol_mnemonic_undecided;
12071 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
12072 eoltype = (XFASTINT (eolvalue) == 0
12073 ? eol_mnemonic_unix
12074 : (XFASTINT (eolvalue) == 1
12075 ? eol_mnemonic_dos : eol_mnemonic_mac));
12076 }
12077 }
12078
12079 if (eol_flag)
12080 {
12081 /* Mention the EOL conversion if it is not the usual one. */
12082 if (STRINGP (eoltype))
12083 {
12084 eol_str = XSTRING (eoltype)->data;
12085 eol_str_len = XSTRING (eoltype)->size;
12086 }
12087 else if (INTEGERP (eoltype)
12088 && CHAR_VALID_P (XINT (eoltype), 0))
12089 {
12090 eol_str = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
12091 eol_str_len = CHAR_STRING (XINT (eoltype), eol_str);
12092 }
12093 else
12094 {
12095 eol_str = invalid_eol_type;
12096 eol_str_len = sizeof (invalid_eol_type) - 1;
12097 }
12098 bcopy (eol_str, buf, eol_str_len);
12099 buf += eol_str_len;
12100 }
12101
12102 return buf;
12103 }
12104
12105 /* Return a string for the output of a mode line %-spec for window W,
12106 generated by character C. PRECISION >= 0 means don't return a
12107 string longer than that value. FIELD_WIDTH > 0 means pad the
12108 string returned with spaces to that value. */
12109
12110 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
12111
12112 static char *
12113 decode_mode_spec (w, c, field_width, precision)
12114 struct window *w;
12115 register int c;
12116 int field_width, precision;
12117 {
12118 Lisp_Object obj;
12119 struct frame *f = XFRAME (WINDOW_FRAME (w));
12120 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
12121 struct buffer *b = XBUFFER (w->buffer);
12122
12123 obj = Qnil;
12124
12125 switch (c)
12126 {
12127 case '*':
12128 if (!NILP (b->read_only))
12129 return "%";
12130 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
12131 return "*";
12132 return "-";
12133
12134 case '+':
12135 /* This differs from %* only for a modified read-only buffer. */
12136 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
12137 return "*";
12138 if (!NILP (b->read_only))
12139 return "%";
12140 return "-";
12141
12142 case '&':
12143 /* This differs from %* in ignoring read-only-ness. */
12144 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
12145 return "*";
12146 return "-";
12147
12148 case '%':
12149 return "%";
12150
12151 case '[':
12152 {
12153 int i;
12154 char *p;
12155
12156 if (command_loop_level > 5)
12157 return "[[[... ";
12158 p = decode_mode_spec_buf;
12159 for (i = 0; i < command_loop_level; i++)
12160 *p++ = '[';
12161 *p = 0;
12162 return decode_mode_spec_buf;
12163 }
12164
12165 case ']':
12166 {
12167 int i;
12168 char *p;
12169
12170 if (command_loop_level > 5)
12171 return " ...]]]";
12172 p = decode_mode_spec_buf;
12173 for (i = 0; i < command_loop_level; i++)
12174 *p++ = ']';
12175 *p = 0;
12176 return decode_mode_spec_buf;
12177 }
12178
12179 case '-':
12180 {
12181 register int i;
12182
12183 /* Let lots_of_dashes be a string of infinite length. */
12184 if (field_width <= 0
12185 || field_width > sizeof (lots_of_dashes))
12186 {
12187 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
12188 decode_mode_spec_buf[i] = '-';
12189 decode_mode_spec_buf[i] = '\0';
12190 return decode_mode_spec_buf;
12191 }
12192 else
12193 return lots_of_dashes;
12194 }
12195
12196 case 'b':
12197 obj = b->name;
12198 break;
12199
12200 case 'c':
12201 {
12202 int col = current_column ();
12203 XSETFASTINT (w->column_number_displayed, col);
12204 pint2str (decode_mode_spec_buf, field_width, col);
12205 return decode_mode_spec_buf;
12206 }
12207
12208 case 'F':
12209 /* %F displays the frame name. */
12210 if (!NILP (f->title))
12211 return (char *) XSTRING (f->title)->data;
12212 if (f->explicit_name || ! FRAME_WINDOW_P (f))
12213 return (char *) XSTRING (f->name)->data;
12214 return "Emacs";
12215
12216 case 'f':
12217 obj = b->filename;
12218 break;
12219
12220 case 'l':
12221 {
12222 int startpos = XMARKER (w->start)->charpos;
12223 int startpos_byte = marker_byte_position (w->start);
12224 int line, linepos, linepos_byte, topline;
12225 int nlines, junk;
12226 int height = XFASTINT (w->height);
12227
12228 /* If we decided that this buffer isn't suitable for line numbers,
12229 don't forget that too fast. */
12230 if (EQ (w->base_line_pos, w->buffer))
12231 goto no_value;
12232 /* But do forget it, if the window shows a different buffer now. */
12233 else if (BUFFERP (w->base_line_pos))
12234 w->base_line_pos = Qnil;
12235
12236 /* If the buffer is very big, don't waste time. */
12237 if (BUF_ZV (b) - BUF_BEGV (b) > line_number_display_limit)
12238 {
12239 w->base_line_pos = Qnil;
12240 w->base_line_number = Qnil;
12241 goto no_value;
12242 }
12243
12244 if (!NILP (w->base_line_number)
12245 && !NILP (w->base_line_pos)
12246 && XFASTINT (w->base_line_pos) <= startpos)
12247 {
12248 line = XFASTINT (w->base_line_number);
12249 linepos = XFASTINT (w->base_line_pos);
12250 linepos_byte = buf_charpos_to_bytepos (b, linepos);
12251 }
12252 else
12253 {
12254 line = 1;
12255 linepos = BUF_BEGV (b);
12256 linepos_byte = BUF_BEGV_BYTE (b);
12257 }
12258
12259 /* Count lines from base line to window start position. */
12260 nlines = display_count_lines (linepos, linepos_byte,
12261 startpos_byte,
12262 startpos, &junk);
12263
12264 topline = nlines + line;
12265
12266 /* Determine a new base line, if the old one is too close
12267 or too far away, or if we did not have one.
12268 "Too close" means it's plausible a scroll-down would
12269 go back past it. */
12270 if (startpos == BUF_BEGV (b))
12271 {
12272 XSETFASTINT (w->base_line_number, topline);
12273 XSETFASTINT (w->base_line_pos, BUF_BEGV (b));
12274 }
12275 else if (nlines < height + 25 || nlines > height * 3 + 50
12276 || linepos == BUF_BEGV (b))
12277 {
12278 int limit = BUF_BEGV (b);
12279 int limit_byte = BUF_BEGV_BYTE (b);
12280 int position;
12281 int distance = (height * 2 + 30) * line_number_display_limit_width;
12282
12283 if (startpos - distance > limit)
12284 {
12285 limit = startpos - distance;
12286 limit_byte = CHAR_TO_BYTE (limit);
12287 }
12288
12289 nlines = display_count_lines (startpos, startpos_byte,
12290 limit_byte,
12291 - (height * 2 + 30),
12292 &position);
12293 /* If we couldn't find the lines we wanted within
12294 line_number_display_limit_width chars per line,
12295 give up on line numbers for this window. */
12296 if (position == limit_byte && limit == startpos - distance)
12297 {
12298 w->base_line_pos = w->buffer;
12299 w->base_line_number = Qnil;
12300 goto no_value;
12301 }
12302
12303 XSETFASTINT (w->base_line_number, topline - nlines);
12304 XSETFASTINT (w->base_line_pos, BYTE_TO_CHAR (position));
12305 }
12306
12307 /* Now count lines from the start pos to point. */
12308 nlines = display_count_lines (startpos, startpos_byte,
12309 PT_BYTE, PT, &junk);
12310
12311 /* Record that we did display the line number. */
12312 line_number_displayed = 1;
12313
12314 /* Make the string to show. */
12315 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
12316 return decode_mode_spec_buf;
12317 no_value:
12318 {
12319 char* p = decode_mode_spec_buf;
12320 int pad = field_width - 2;
12321 while (pad-- > 0)
12322 *p++ = ' ';
12323 *p++ = '?';
12324 *p = '?';
12325 return decode_mode_spec_buf;
12326 }
12327 }
12328 break;
12329
12330 case 'm':
12331 obj = b->mode_name;
12332 break;
12333
12334 case 'n':
12335 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
12336 return " Narrow";
12337 break;
12338
12339 case 'p':
12340 {
12341 int pos = marker_position (w->start);
12342 int total = BUF_ZV (b) - BUF_BEGV (b);
12343
12344 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
12345 {
12346 if (pos <= BUF_BEGV (b))
12347 return "All";
12348 else
12349 return "Bottom";
12350 }
12351 else if (pos <= BUF_BEGV (b))
12352 return "Top";
12353 else
12354 {
12355 if (total > 1000000)
12356 /* Do it differently for a large value, to avoid overflow. */
12357 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
12358 else
12359 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
12360 /* We can't normally display a 3-digit number,
12361 so get us a 2-digit number that is close. */
12362 if (total == 100)
12363 total = 99;
12364 sprintf (decode_mode_spec_buf, "%2d%%", total);
12365 return decode_mode_spec_buf;
12366 }
12367 }
12368
12369 /* Display percentage of size above the bottom of the screen. */
12370 case 'P':
12371 {
12372 int toppos = marker_position (w->start);
12373 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
12374 int total = BUF_ZV (b) - BUF_BEGV (b);
12375
12376 if (botpos >= BUF_ZV (b))
12377 {
12378 if (toppos <= BUF_BEGV (b))
12379 return "All";
12380 else
12381 return "Bottom";
12382 }
12383 else
12384 {
12385 if (total > 1000000)
12386 /* Do it differently for a large value, to avoid overflow. */
12387 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
12388 else
12389 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
12390 /* We can't normally display a 3-digit number,
12391 so get us a 2-digit number that is close. */
12392 if (total == 100)
12393 total = 99;
12394 if (toppos <= BUF_BEGV (b))
12395 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
12396 else
12397 sprintf (decode_mode_spec_buf, "%2d%%", total);
12398 return decode_mode_spec_buf;
12399 }
12400 }
12401
12402 case 's':
12403 /* status of process */
12404 obj = Fget_buffer_process (w->buffer);
12405 if (NILP (obj))
12406 return "no process";
12407 #ifdef subprocesses
12408 obj = Fsymbol_name (Fprocess_status (obj));
12409 #endif
12410 break;
12411
12412 case 't': /* indicate TEXT or BINARY */
12413 #ifdef MODE_LINE_BINARY_TEXT
12414 return MODE_LINE_BINARY_TEXT (b);
12415 #else
12416 return "T";
12417 #endif
12418
12419 case 'z':
12420 /* coding-system (not including end-of-line format) */
12421 case 'Z':
12422 /* coding-system (including end-of-line type) */
12423 {
12424 int eol_flag = (c == 'Z');
12425 char *p = decode_mode_spec_buf;
12426
12427 if (! FRAME_WINDOW_P (f))
12428 {
12429 /* No need to mention EOL here--the terminal never needs
12430 to do EOL conversion. */
12431 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
12432 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
12433 }
12434 p = decode_mode_spec_coding (b->buffer_file_coding_system,
12435 p, eol_flag);
12436
12437 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
12438 #ifdef subprocesses
12439 obj = Fget_buffer_process (Fcurrent_buffer ());
12440 if (PROCESSP (obj))
12441 {
12442 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
12443 p, eol_flag);
12444 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
12445 p, eol_flag);
12446 }
12447 #endif /* subprocesses */
12448 #endif /* 0 */
12449 *p = 0;
12450 return decode_mode_spec_buf;
12451 }
12452 }
12453
12454 if (STRINGP (obj))
12455 return (char *) XSTRING (obj)->data;
12456 else
12457 return "";
12458 }
12459
12460
12461 /* Count up to COUNT lines starting from START / START_BYTE.
12462 But don't go beyond LIMIT_BYTE.
12463 Return the number of lines thus found (always nonnegative).
12464
12465 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
12466
12467 static int
12468 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
12469 int start, start_byte, limit_byte, count;
12470 int *byte_pos_ptr;
12471 {
12472 register unsigned char *cursor;
12473 unsigned char *base;
12474
12475 register int ceiling;
12476 register unsigned char *ceiling_addr;
12477 int orig_count = count;
12478
12479 /* If we are not in selective display mode,
12480 check only for newlines. */
12481 int selective_display = (!NILP (current_buffer->selective_display)
12482 && !INTEGERP (current_buffer->selective_display));
12483
12484 if (count > 0)
12485 {
12486 while (start_byte < limit_byte)
12487 {
12488 ceiling = BUFFER_CEILING_OF (start_byte);
12489 ceiling = min (limit_byte - 1, ceiling);
12490 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
12491 base = (cursor = BYTE_POS_ADDR (start_byte));
12492 while (1)
12493 {
12494 if (selective_display)
12495 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
12496 ;
12497 else
12498 while (*cursor != '\n' && ++cursor != ceiling_addr)
12499 ;
12500
12501 if (cursor != ceiling_addr)
12502 {
12503 if (--count == 0)
12504 {
12505 start_byte += cursor - base + 1;
12506 *byte_pos_ptr = start_byte;
12507 return orig_count;
12508 }
12509 else
12510 if (++cursor == ceiling_addr)
12511 break;
12512 }
12513 else
12514 break;
12515 }
12516 start_byte += cursor - base;
12517 }
12518 }
12519 else
12520 {
12521 while (start_byte > limit_byte)
12522 {
12523 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
12524 ceiling = max (limit_byte, ceiling);
12525 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
12526 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
12527 while (1)
12528 {
12529 if (selective_display)
12530 while (--cursor != ceiling_addr
12531 && *cursor != '\n' && *cursor != 015)
12532 ;
12533 else
12534 while (--cursor != ceiling_addr && *cursor != '\n')
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 /* When scanning backwards, we should
12544 not count the newline posterior to which we stop. */
12545 return - orig_count - 1;
12546 }
12547 }
12548 else
12549 break;
12550 }
12551 /* Here we add 1 to compensate for the last decrement
12552 of CURSOR, which took it past the valid range. */
12553 start_byte += cursor - base + 1;
12554 }
12555 }
12556
12557 *byte_pos_ptr = limit_byte;
12558
12559 if (count < 0)
12560 return - orig_count + count;
12561 return orig_count - count;
12562
12563 }
12564
12565
12566 \f
12567 /***********************************************************************
12568 Displaying strings
12569 ***********************************************************************/
12570
12571 /* Display a NUL-terminated string, starting with index START.
12572
12573 If STRING is non-null, display that C string. Otherwise, the Lisp
12574 string LISP_STRING is displayed.
12575
12576 If FACE_STRING is not nil, FACE_STRING_POS is a position in
12577 FACE_STRING. Display STRING or LISP_STRING with the face at
12578 FACE_STRING_POS in FACE_STRING:
12579
12580 Display the string in the environment given by IT, but use the
12581 standard display table, temporarily.
12582
12583 FIELD_WIDTH is the minimum number of output glyphs to produce.
12584 If STRING has fewer characters than FIELD_WIDTH, pad to the right
12585 with spaces. If STRING has more characters, more than FIELD_WIDTH
12586 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
12587
12588 PRECISION is the maximum number of characters to output from
12589 STRING. PRECISION < 0 means don't truncate the string.
12590
12591 This is roughly equivalent to printf format specifiers:
12592
12593 FIELD_WIDTH PRECISION PRINTF
12594 ----------------------------------------
12595 -1 -1 %s
12596 -1 10 %.10s
12597 10 -1 %10s
12598 20 10 %20.10s
12599
12600 MULTIBYTE zero means do not display multibyte chars, > 0 means do
12601 display them, and < 0 means obey the current buffer's value of
12602 enable_multibyte_characters.
12603
12604 Value is the number of glyphs produced. */
12605
12606 static int
12607 display_string (string, lisp_string, face_string, face_string_pos,
12608 start, it, field_width, precision, max_x, multibyte)
12609 unsigned char *string;
12610 Lisp_Object lisp_string;
12611 Lisp_Object face_string;
12612 int face_string_pos;
12613 int start;
12614 struct it *it;
12615 int field_width, precision, max_x;
12616 int multibyte;
12617 {
12618 int hpos_at_start = it->hpos;
12619 int saved_face_id = it->face_id;
12620 struct glyph_row *row = it->glyph_row;
12621
12622 /* Initialize the iterator IT for iteration over STRING beginning
12623 with index START. We assume that IT may be modified here (which
12624 means that display_line has to do something when displaying a
12625 mini-buffer prompt, which it does). */
12626 reseat_to_string (it, string, lisp_string, start,
12627 precision, field_width, multibyte);
12628
12629 /* If displaying STRING, set up the face of the iterator
12630 from LISP_STRING, if that's given. */
12631 if (STRINGP (face_string))
12632 {
12633 int endptr;
12634 struct face *face;
12635
12636 it->face_id
12637 = face_at_string_position (it->w, face_string, face_string_pos,
12638 0, it->region_beg_charpos,
12639 it->region_end_charpos,
12640 &endptr, it->base_face_id);
12641 face = FACE_FROM_ID (it->f, it->face_id);
12642 it->face_box_p = face->box != FACE_NO_BOX;
12643 }
12644
12645 /* Set max_x to the maximum allowed X position. Don't let it go
12646 beyond the right edge of the window. */
12647 if (max_x <= 0)
12648 max_x = it->last_visible_x;
12649 else
12650 max_x = min (max_x, it->last_visible_x);
12651
12652 /* Skip over display elements that are not visible. because IT->w is
12653 hscrolled. */
12654 if (it->current_x < it->first_visible_x)
12655 move_it_in_display_line_to (it, 100000, it->first_visible_x,
12656 MOVE_TO_POS | MOVE_TO_X);
12657
12658 row->ascent = it->max_ascent;
12659 row->height = it->max_ascent + it->max_descent;
12660 row->phys_ascent = it->max_phys_ascent;
12661 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
12662
12663 /* This condition is for the case that we are called with current_x
12664 past last_visible_x. */
12665 while (it->current_x < max_x)
12666 {
12667 int x_before, x, n_glyphs_before, i, nglyphs;
12668
12669 /* Get the next display element. */
12670 if (!get_next_display_element (it))
12671 break;
12672
12673 /* Produce glyphs. */
12674 x_before = it->current_x;
12675 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
12676 PRODUCE_GLYPHS (it);
12677
12678 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
12679 i = 0;
12680 x = x_before;
12681 while (i < nglyphs)
12682 {
12683 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12684
12685 if (!it->truncate_lines_p
12686 && x + glyph->pixel_width > max_x)
12687 {
12688 /* End of continued line or max_x reached. */
12689 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
12690 it->current_x = x;
12691 break;
12692 }
12693 else if (x + glyph->pixel_width > it->first_visible_x)
12694 {
12695 /* Glyph is at least partially visible. */
12696 ++it->hpos;
12697 if (x < it->first_visible_x)
12698 it->glyph_row->x = x - it->first_visible_x;
12699 }
12700 else
12701 {
12702 /* Glyph is off the left margin of the display area.
12703 Should not happen. */
12704 abort ();
12705 }
12706
12707 row->ascent = max (row->ascent, it->max_ascent);
12708 row->height = max (row->height, it->max_ascent + it->max_descent);
12709 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12710 row->phys_height = max (row->phys_height,
12711 it->max_phys_ascent + it->max_phys_descent);
12712 x += glyph->pixel_width;
12713 ++i;
12714 }
12715
12716 /* Stop if max_x reached. */
12717 if (i < nglyphs)
12718 break;
12719
12720 /* Stop at line ends. */
12721 if (ITERATOR_AT_END_OF_LINE_P (it))
12722 {
12723 it->continuation_lines_width = 0;
12724 break;
12725 }
12726
12727 set_iterator_to_next (it);
12728
12729 /* Stop if truncating at the right edge. */
12730 if (it->truncate_lines_p
12731 && it->current_x >= it->last_visible_x)
12732 {
12733 /* Add truncation mark, but don't do it if the line is
12734 truncated at a padding space. */
12735 if (IT_CHARPOS (*it) < it->string_nchars)
12736 {
12737 if (!FRAME_WINDOW_P (it->f))
12738 produce_special_glyphs (it, IT_TRUNCATION);
12739 it->glyph_row->truncated_on_right_p = 1;
12740 }
12741 break;
12742 }
12743 }
12744
12745 /* Maybe insert a truncation at the left. */
12746 if (it->first_visible_x
12747 && IT_CHARPOS (*it) > 0)
12748 {
12749 if (!FRAME_WINDOW_P (it->f))
12750 insert_left_trunc_glyphs (it);
12751 it->glyph_row->truncated_on_left_p = 1;
12752 }
12753
12754 it->face_id = saved_face_id;
12755
12756 /* Value is number of columns displayed. */
12757 return it->hpos - hpos_at_start;
12758 }
12759
12760
12761 \f
12762 /* This is like a combination of memq and assq. Return 1 if PROPVAL
12763 appears as an element of LIST or as the car of an element of LIST.
12764 If PROPVAL is a list, compare each element against LIST in that
12765 way, and return 1 if any element of PROPVAL is found in LIST.
12766 Otherwise return 0. This function cannot quit. */
12767
12768 int
12769 invisible_p (propval, list)
12770 register Lisp_Object propval;
12771 Lisp_Object list;
12772 {
12773 register Lisp_Object tail, proptail;
12774 for (tail = list; CONSP (tail); tail = XCDR (tail))
12775 {
12776 register Lisp_Object tem;
12777 tem = XCAR (tail);
12778 if (EQ (propval, tem))
12779 return 1;
12780 if (CONSP (tem) && EQ (propval, XCAR (tem)))
12781 return 1;
12782 }
12783 if (CONSP (propval))
12784 for (proptail = propval; CONSP (proptail);
12785 proptail = XCDR (proptail))
12786 {
12787 Lisp_Object propelt;
12788 propelt = XCAR (proptail);
12789 for (tail = list; CONSP (tail); tail = XCDR (tail))
12790 {
12791 register Lisp_Object tem;
12792 tem = XCAR (tail);
12793 if (EQ (propelt, tem))
12794 return 1;
12795 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
12796 return 1;
12797 }
12798 }
12799 return 0;
12800 }
12801
12802
12803 /* Return 1 if PROPVAL appears as the car of an element of LIST and
12804 the cdr of that element is non-nil. If PROPVAL is a list, check
12805 each element of PROPVAL in that way, and the first time some
12806 element is found, return 1 if the cdr of that element is non-nil.
12807 Otherwise return 0. This function cannot quit. */
12808
12809 int
12810 invisible_ellipsis_p (propval, list)
12811 register Lisp_Object propval;
12812 Lisp_Object list;
12813 {
12814 register Lisp_Object tail, proptail;
12815
12816 for (tail = list; CONSP (tail); tail = XCDR (tail))
12817 {
12818 register Lisp_Object tem;
12819 tem = XCAR (tail);
12820 if (CONSP (tem) && EQ (propval, XCAR (tem)))
12821 return ! NILP (XCDR (tem));
12822 }
12823
12824 if (CONSP (propval))
12825 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
12826 {
12827 Lisp_Object propelt;
12828 propelt = XCAR (proptail);
12829 for (tail = list; CONSP (tail); tail = XCDR (tail))
12830 {
12831 register Lisp_Object tem;
12832 tem = XCAR (tail);
12833 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
12834 return ! NILP (XCDR (tem));
12835 }
12836 }
12837
12838 return 0;
12839 }
12840
12841
12842 \f
12843 /***********************************************************************
12844 Initialization
12845 ***********************************************************************/
12846
12847 void
12848 syms_of_xdisp ()
12849 {
12850 Vwith_echo_area_save_vector = Qnil;
12851 staticpro (&Vwith_echo_area_save_vector);
12852
12853 Vmessage_stack = Qnil;
12854 staticpro (&Vmessage_stack);
12855
12856 Qinhibit_redisplay = intern ("inhibit-redisplay");
12857 staticpro (&Qinhibit_redisplay);
12858
12859 #if GLYPH_DEBUG
12860 defsubr (&Sdump_glyph_matrix);
12861 defsubr (&Sdump_glyph_row);
12862 defsubr (&Sdump_tool_bar_row);
12863 defsubr (&Strace_redisplay_toggle);
12864 defsubr (&Strace_to_stderr);
12865 #endif
12866
12867 staticpro (&Qmenu_bar_update_hook);
12868 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
12869
12870 staticpro (&Qoverriding_terminal_local_map);
12871 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
12872
12873 staticpro (&Qoverriding_local_map);
12874 Qoverriding_local_map = intern ("overriding-local-map");
12875
12876 staticpro (&Qwindow_scroll_functions);
12877 Qwindow_scroll_functions = intern ("window-scroll-functions");
12878
12879 staticpro (&Qredisplay_end_trigger_functions);
12880 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
12881
12882 staticpro (&Qinhibit_point_motion_hooks);
12883 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
12884
12885 QCdata = intern (":data");
12886 staticpro (&QCdata);
12887 Qdisplay = intern ("display");
12888 staticpro (&Qdisplay);
12889 Qspace_width = intern ("space-width");
12890 staticpro (&Qspace_width);
12891 Qraise = intern ("raise");
12892 staticpro (&Qraise);
12893 Qspace = intern ("space");
12894 staticpro (&Qspace);
12895 Qmargin = intern ("margin");
12896 staticpro (&Qmargin);
12897 Qleft_margin = intern ("left-margin");
12898 staticpro (&Qleft_margin);
12899 Qright_margin = intern ("right-margin");
12900 staticpro (&Qright_margin);
12901 Qalign_to = intern ("align-to");
12902 staticpro (&Qalign_to);
12903 QCalign_to = intern (":align-to");
12904 staticpro (&QCalign_to);
12905 Qrelative_width = intern ("relative-width");
12906 staticpro (&Qrelative_width);
12907 QCrelative_width = intern (":relative-width");
12908 staticpro (&QCrelative_width);
12909 QCrelative_height = intern (":relative-height");
12910 staticpro (&QCrelative_height);
12911 QCeval = intern (":eval");
12912 staticpro (&QCeval);
12913 Qwhen = intern ("when");
12914 staticpro (&Qwhen);
12915 QCfile = intern (":file");
12916 staticpro (&QCfile);
12917 Qfontified = intern ("fontified");
12918 staticpro (&Qfontified);
12919 Qfontification_functions = intern ("fontification-functions");
12920 staticpro (&Qfontification_functions);
12921 Qtrailing_whitespace = intern ("trailing-whitespace");
12922 staticpro (&Qtrailing_whitespace);
12923 Qimage = intern ("image");
12924 staticpro (&Qimage);
12925
12926 last_arrow_position = Qnil;
12927 last_arrow_string = Qnil;
12928 staticpro (&last_arrow_position);
12929 staticpro (&last_arrow_string);
12930
12931 echo_buffer[0] = echo_buffer[1] = Qnil;
12932 staticpro (&echo_buffer[0]);
12933 staticpro (&echo_buffer[1]);
12934
12935 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
12936 staticpro (&echo_area_buffer[0]);
12937 staticpro (&echo_area_buffer[1]);
12938
12939 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
12940 "Non-nil means highlight trailing whitespace.\n\
12941 The face used for trailing whitespace is `trailing-whitespace'.");
12942 Vshow_trailing_whitespace = Qnil;
12943
12944 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
12945 "Non-nil means don't actually do any redisplay.\n\
12946 This is used for internal purposes.");
12947 Vinhibit_redisplay = Qnil;
12948
12949 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
12950 "String (or mode line construct) included (normally) in `mode-line-format'.");
12951 Vglobal_mode_string = Qnil;
12952
12953 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
12954 "Marker for where to display an arrow on top of the buffer text.\n\
12955 This must be the beginning of a line in order to work.\n\
12956 See also `overlay-arrow-string'.");
12957 Voverlay_arrow_position = Qnil;
12958
12959 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
12960 "String to display as an arrow. See also `overlay-arrow-position'.");
12961 Voverlay_arrow_string = Qnil;
12962
12963 DEFVAR_INT ("scroll-step", &scroll_step,
12964 "*The number of lines to try scrolling a window by when point moves out.\n\
12965 If that fails to bring point back on frame, point is centered instead.\n\
12966 If this is zero, point is always centered after it moves off frame.");
12967
12968 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
12969 "*Scroll up to this many lines, to bring point back on screen.\n\
12970 A value of zero means to scroll the text to center point vertically\n\
12971 in the window.");
12972 scroll_conservatively = 0;
12973
12974 DEFVAR_INT ("scroll-margin", &scroll_margin,
12975 "*Number of lines of margin at the top and bottom of a window.\n\
12976 Recenter the window whenever point gets within this many lines\n\
12977 of the top or bottom of the window.");
12978 scroll_margin = 0;
12979
12980 #if GLYPH_DEBUG
12981 DEFVAR_INT ("debug-end-pos", &debug_end_pos, "Don't ask");
12982 #endif
12983
12984 DEFVAR_BOOL ("truncate-partial-width-windows",
12985 &truncate_partial_width_windows,
12986 "*Non-nil means truncate lines in all windows less than full frame wide.");
12987 truncate_partial_width_windows = 1;
12988
12989 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
12990 "*Non-nil means use inverse video for the mode line.");
12991 mode_line_inverse_video = 1;
12992
12993 DEFVAR_INT ("line-number-display-limit", &line_number_display_limit,
12994 "*Maximum buffer size for which line number should be displayed.\n\
12995 If the buffer is bigger than this, the line number does not appear\n\
12996 in the mode line.");
12997 line_number_display_limit = 1000000;
12998
12999 DEFVAR_INT ("line-number-display-limit-width", &line_number_display_limit_width,
13000 "*Maximum line width (in characters) for line number display.\n\
13001 If the average length of the lines near point is bigger than this, then the\n\
13002 line number may be omitted from the mode line.");
13003 line_number_display_limit_width = 200;
13004
13005 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
13006 "*Non-nil means highlight region even in nonselected windows.");
13007 highlight_nonselected_windows = 0;
13008
13009 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
13010 "Non-nil if more than one frame is visible on this display.\n\
13011 Minibuffer-only frames don't count, but iconified frames do.\n\
13012 This variable is not guaranteed to be accurate except while processing\n\
13013 `frame-title-format' and `icon-title-format'.");
13014
13015 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
13016 "Template for displaying the title bar of visible frames.\n\
13017 \(Assuming the window manager supports this feature.)\n\
13018 This variable has the same structure as `mode-line-format' (which see),\n\
13019 and is used only on frames for which no explicit name has been set\n\
13020 \(see `modify-frame-parameters').");
13021 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
13022 "Template for displaying the title bar of an iconified frame.\n\
13023 \(Assuming the window manager supports this feature.)\n\
13024 This variable has the same structure as `mode-line-format' (which see),\n\
13025 and is used only on frames for which no explicit name has been set\n\
13026 \(see `modify-frame-parameters').");
13027 Vicon_title_format
13028 = Vframe_title_format
13029 = Fcons (intern ("multiple-frames"),
13030 Fcons (build_string ("%b"),
13031 Fcons (Fcons (build_string (""),
13032 Fcons (intern ("invocation-name"),
13033 Fcons (build_string ("@"),
13034 Fcons (intern ("system-name"),
13035 Qnil)))),
13036 Qnil)));
13037
13038 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
13039 "Maximum number of lines to keep in the message log buffer.\n\
13040 If nil, disable message logging. If t, log messages but don't truncate\n\
13041 the buffer when it becomes large.");
13042 XSETFASTINT (Vmessage_log_max, 50);
13043
13044 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
13045 "Functions called before redisplay, if window sizes have changed.\n\
13046 The value should be a list of functions that take one argument.\n\
13047 Just before redisplay, for each frame, if any of its windows have changed\n\
13048 size since the last redisplay, or have been split or deleted,\n\
13049 all the functions in the list are called, with the frame as argument.");
13050 Vwindow_size_change_functions = Qnil;
13051
13052 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
13053 "List of Functions to call before redisplaying a window with scrolling.\n\
13054 Each function is called with two arguments, the window\n\
13055 and its new display-start position. Note that the value of `window-end'\n\
13056 is not valid when these functions are called.");
13057 Vwindow_scroll_functions = Qnil;
13058
13059 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
13060 "*Non-nil means automatically resize tool-bars.\n\
13061 This increases a tool-bar's height if not all tool-bar items are visible.\n\
13062 It decreases a tool-bar's height when it would display blank lines\n\
13063 otherwise.");
13064 auto_resize_tool_bars_p = 1;
13065
13066 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
13067 "*Non-nil means raise tool-bar buttons when the mouse moves over them.");
13068 auto_raise_tool_bar_buttons_p = 1;
13069
13070 DEFVAR_INT ("tool-bar-button-margin", &tool_bar_button_margin,
13071 "*Margin around tool-bar buttons in pixels.");
13072 tool_bar_button_margin = 1;
13073
13074 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
13075 "Relief thickness of tool-bar buttons.");
13076 tool_bar_button_relief = 3;
13077
13078 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
13079 "List of functions to call to fontify regions of text.\n\
13080 Each function is called with one argument POS. Functions must\n\
13081 fontify a region starting at POS in the current buffer, and give\n\
13082 fontified regions the property `fontified'.\n\
13083 This variable automatically becomes buffer-local when set.");
13084 Vfontification_functions = Qnil;
13085 Fmake_local_variable (Qfontification_functions);
13086
13087 DEFVAR_BOOL ("unibyte-display-via-language-environment",
13088 &unibyte_display_via_language_environment,
13089 "*Non-nil means display unibyte text according to language environment.\n\
13090 Specifically this means that unibyte non-ASCII characters\n\
13091 are displayed by converting them to the equivalent multibyte characters\n\
13092 according to the current language environment. As a result, they are\n\
13093 displayed according to the current fontset.");
13094 unibyte_display_via_language_environment = 0;
13095
13096 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
13097 "*Maximum height for resizing mini-windows.\n\
13098 If a float, it specifies a fraction of the mini-window frame's height.\n\
13099 If an integer, it specifies a number of lines.\n\
13100 If nil, don't resize.");
13101 Vmax_mini_window_height = make_float (0.25);
13102
13103 DEFVAR_BOOL ("cursor-in-non-selected-windows",
13104 &cursor_in_non_selected_windows,
13105 "*Non-nil means display a hollow cursor in non-selected windows.\n\
13106 Nil means don't display a cursor there.");
13107 cursor_in_non_selected_windows = 1;
13108
13109 DEFVAR_BOOL ("automatic-hscrolling", &automatic_hscrolling_p,
13110 "*Non-nil means scroll the display automatically to make point visible.");
13111 automatic_hscrolling_p = 1;
13112 }
13113
13114
13115 /* Initialize this module when Emacs starts. */
13116
13117 void
13118 init_xdisp ()
13119 {
13120 Lisp_Object root_window;
13121 struct window *mini_w;
13122
13123 CHARPOS (this_line_start_pos) = 0;
13124
13125 mini_w = XWINDOW (minibuf_window);
13126 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
13127
13128 if (!noninteractive)
13129 {
13130 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
13131 int i;
13132
13133 XSETFASTINT (XWINDOW (root_window)->top, FRAME_TOP_MARGIN (f));
13134 set_window_height (root_window,
13135 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
13136 0);
13137 XSETFASTINT (mini_w->top, FRAME_HEIGHT (f) - 1);
13138 set_window_height (minibuf_window, 1, 0);
13139
13140 XSETFASTINT (XWINDOW (root_window)->width, FRAME_WIDTH (f));
13141 XSETFASTINT (mini_w->width, FRAME_WIDTH (f));
13142
13143 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
13144 scratch_glyph_row.glyphs[TEXT_AREA + 1]
13145 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
13146
13147 /* The default ellipsis glyphs `...'. */
13148 for (i = 0; i < 3; ++i)
13149 XSETFASTINT (default_invis_vector[i], '.');
13150 }
13151
13152 #ifdef HAVE_WINDOW_SYSTEM
13153 {
13154 /* Allocate the buffer for frame titles. */
13155 int size = 100;
13156 frame_title_buf = (char *) xmalloc (size);
13157 frame_title_buf_end = frame_title_buf + size;
13158 frame_title_ptr = NULL;
13159 }
13160 #endif /* HAVE_WINDOW_SYSTEM */
13161 }
13162
13163