(displayed_window_lines): Take empty lines at
[bpt/emacs.git] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 86, 87, 88, 93, 94, 95, 97, 98, 99
3 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
23
24 Redisplay.
25
26 Emacs separates the task of updating the display from code
27 modifying global state, e.g. buffer text. This way functions
28 operating on buffers don't also have to be concerned with updating
29 the display.
30
31 Updating the display is triggered by the Lisp interpreter when it
32 decides it's time to do it. This is done either automatically for
33 you as part of the interpreter's command loop or as the result of
34 calling Lisp functions like `sit-for'. The C function `redisplay'
35 in xdisp.c is the only entry into the inner redisplay code. (Or,
36 let's say almost---see the the description of direct update
37 operations, below.).
38
39 The following diagram shows how redisplay code is invoked. As you
40 can see, Lisp calls redisplay and vice versa. Under window systems
41 like X, some portions of the redisplay code are also called
42 asynchronously during mouse movement or expose events. It is very
43 important that these code parts do NOT use the C library (malloc,
44 free) because many C libraries under Unix are not reentrant. They
45 may also NOT call functions of the Lisp interpreter which could
46 change the interpreter's state. If you don't follow these rules,
47 you will encounter bugs which are very hard to explain.
48
49 (Direct functions, see below)
50 direct_output_for_insert,
51 direct_forward_char (dispnew.c)
52 +---------------------------------+
53 | |
54 | V
55 +--------------+ redisplay() +----------------+
56 | Lisp machine |---------------->| Redisplay code |<--+
57 +--------------+ (xdisp.c) +----------------+ |
58 ^ | |
59 +----------------------------------+ |
60 Don't use this path when called |
61 asynchronously! |
62 |
63 expose_window (asynchronous) |
64 |
65 X expose events -----+
66
67 What does redisplay? Obviously, it has to figure out somehow what
68 has been changed since the last time the display has been updated,
69 and to make these changes visible. Preferably it would do that in
70 a moderately intelligent way, i.e. fast.
71
72 Changes in buffer text can be deduced from window and buffer
73 structures, and from some global variables like `beg_unchanged' and
74 `end_unchanged'. The contents of the display are additionally
75 recorded in a `glyph matrix', a two-dimensional matrix of glyph
76 structures. Each row in such a matrix corresponds to a line on the
77 display, and each glyph in a row corresponds to a column displaying
78 a character, an image, or what else. This matrix is called the
79 `current glyph matrix' or `current matrix' in redisplay
80 terminology.
81
82 For buffer parts that have been changed since the last update, a
83 second glyph matrix is constructed, the so called `desired glyph
84 matrix' or short `desired matrix'. Current and desired matrix are
85 then compared to find a cheap way to update the display, e.g. by
86 reusing part of the display by scrolling lines.
87
88
89 Direct operations.
90
91 You will find a lot of of redisplay optimizations when you start
92 looking at the innards of redisplay. The overall goal of all these
93 optimizations is to make redisplay fast because it is done
94 frequently.
95
96 Two optimizations are not found in xdisp.c. These are the direct
97 operations mentioned above. As the name suggests they follow a
98 different principle than the rest of redisplay. Instead of
99 building a desired matrix and then comparing it with the current
100 display, they perform their actions directly on the display and on
101 the current matrix.
102
103 One direct operation updates the display after one character has
104 been entered. The other one moves the cursor by one position
105 forward or backward. You find these functions under the names
106 `direct_output_for_insert' and `direct_output_forward_char' in
107 dispnew.c.
108
109
110 Desired matrices.
111
112 Desired matrices are always built per Emacs window. The function
113 `display_line' is the central function to look at if you are
114 interested. It constructs one row in a desired matrix given an
115 iterator structure containing both a buffer position and a
116 description of the environment in which the text is to be
117 displayed. But this is too early, read on.
118
119 Characters and pixmaps displayed for a range of buffer text depend
120 on various settings of buffers and windows, on overlays and text
121 properties, on display tables, on selective display. The good news
122 is that all this hairy stuff is hidden behind a small set of
123 interface functions taking a iterator structure (struct it)
124 argument.
125
126 Iteration over things to be be displayed is then simple. It is
127 started by initializing an iterator with a call to init_iterator
128 (or init_string_iterator for that matter). Calls to
129 get_next_display_element fill the iterator structure with relevant
130 information about the next thing to display. Calls to
131 set_iterator_to_next move the iterator to the next thing.
132
133 Besides this, an iterator also contains information about the
134 display environment in which glyphs for display elements are to be
135 produced. It has fields for the width and height of the display,
136 the information whether long lines are truncated or continued, a
137 current X and Y position, and lots of other stuff you can better
138 see in dispextern.h.
139
140 Glyphs in a desired matrix are normally constructed in a loop
141 calling get_next_display_element and then produce_glyphs. The call
142 to produce_glyphs will fill the iterator structure with pixel
143 information about the element being displayed and at the same time
144 produce glyphs for it. If the display element fits on the line
145 being displayed, set_iterator_to_next is called next, otherwise the
146 glyphs produced are discarded.
147
148
149 Frame matrices.
150
151 That just couldn't be all, could it? What about terminal types not
152 supporting operations on sub-windows of the screen? To update the
153 display on such a terminal, window-based glyph matrices are not
154 well suited. To be able to reuse part of the display (scrolling
155 lines up and down), we must instead have a view of the whole
156 screen. This is what `frame matrices' are for. They are a trick.
157
158 Frames on terminals like above have a glyph pool. Windows on such
159 a frame sub-allocate their glyph memory from their frame's glyph
160 pool. The frame itself is given its own glyph matrices. By
161 coincidence---or maybe something else---rows in window glyph
162 matrices are slices of corresponding rows in frame matrices. Thus
163 writing to window matrices implicitly updates a frame matrix which
164 provides us with the view of the whole screen that we originally
165 wanted to have without having to move many bytes around. To be
166 honest, there is a little bit more done, but not much more. If you
167 plan to extend that code, take a look at dispnew.c. The function
168 build_frame_matrix is a good starting point. */
169
170 #include <config.h>
171 #include <stdio.h>
172 #include "lisp.h"
173 #include "frame.h"
174 #include "window.h"
175 #include "termchar.h"
176 #include "dispextern.h"
177 #include "buffer.h"
178 #include "charset.h"
179 #include "indent.h"
180 #include "commands.h"
181 #include "macros.h"
182 #include "disptab.h"
183 #include "termhooks.h"
184 #include "intervals.h"
185 #include "keyboard.h"
186 #include "coding.h"
187 #include "process.h"
188 #include "region-cache.h"
189 #include "fontset.h"
190
191 #ifdef HAVE_X_WINDOWS
192 #include "xterm.h"
193 #endif
194 #ifdef WINDOWSNT
195 #include "w32term.h"
196 #endif
197
198 #define min(a, b) ((a) < (b) ? (a) : (b))
199 #define max(a, b) ((a) > (b) ? (a) : (b))
200
201 #define INFINITY 10000000
202
203 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
204 extern void set_frame_menubar ();
205 extern int pending_menu_activation;
206 #endif
207
208 extern int interrupt_input;
209 extern int command_loop_level;
210
211 extern int minibuffer_auto_raise;
212
213 extern Lisp_Object Qface;
214
215 extern Lisp_Object Voverriding_local_map;
216 extern Lisp_Object Voverriding_local_map_menu_flag;
217 extern Lisp_Object Qmenu_item;
218
219 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
220 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
221 Lisp_Object Qredisplay_end_trigger_functions;
222 Lisp_Object Qinhibit_point_motion_hooks;
223 Lisp_Object QCeval, Qwhen, QCfile, QCdata;
224 Lisp_Object Qfontified;
225
226 /* Functions called to fontify regions of text. */
227
228 Lisp_Object Vfontification_functions;
229 Lisp_Object Qfontification_functions;
230
231 /* Non-zero means draw tool bar buttons raised when the mouse moves
232 over them. */
233
234 int auto_raise_tool_bar_buttons_p;
235
236 /* Margin around tool bar buttons in pixels. */
237
238 int tool_bar_button_margin;
239
240 /* Thickness of shadow to draw around tool bar buttons. */
241
242 int tool_bar_button_relief;
243
244 /* Non-zero means automatically resize tool-bars so that all tool-bar
245 items are visible, and no blank lines remain. */
246
247 int auto_resize_tool_bars_p;
248
249 /* Non-nil means don't actually do any redisplay. */
250
251 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
252
253 /* Names of text properties relevant for redisplay. */
254
255 Lisp_Object Qdisplay, Qrelative_width, Qalign_to;
256 extern Lisp_Object Qface, Qinvisible, Qimage, Qwidth;
257
258 /* Symbols used in text property values. */
259
260 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
261 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
262 Lisp_Object Qmargin;
263 extern Lisp_Object Qheight;
264
265 /* Non-nil means highlight trailing whitespace. */
266
267 Lisp_Object Vshow_trailing_whitespace;
268
269 /* Name of the face used to highlight trailing whitespace. */
270
271 Lisp_Object Qtrailing_whitespace;
272
273 /* The symbol `image' which is the car of the lists used to represent
274 images in Lisp. */
275
276 Lisp_Object Qimage;
277
278 /* Non-zero means print newline to stdout before next mini-buffer
279 message. */
280
281 int noninteractive_need_newline;
282
283 /* Non-zero means print newline to message log before next message. */
284
285 static int message_log_need_newline;
286
287 \f
288 /* The buffer position of the first character appearing entirely or
289 partially on the line of the selected window which contains the
290 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
291 redisplay optimization in redisplay_internal. */
292
293 static struct text_pos this_line_start_pos;
294
295 /* Number of characters past the end of the line above, including the
296 terminating newline. */
297
298 static struct text_pos this_line_end_pos;
299
300 /* The vertical positions and the height of this line. */
301
302 static int this_line_vpos;
303 static int this_line_y;
304 static int this_line_pixel_height;
305
306 /* X position at which this display line starts. Usually zero;
307 negative if first character is partially visible. */
308
309 static int this_line_start_x;
310
311 /* Buffer that this_line_.* variables are referring to. */
312
313 static struct buffer *this_line_buffer;
314
315 /* Nonzero means truncate lines in all windows less wide than the
316 frame. */
317
318 int truncate_partial_width_windows;
319
320 /* A flag to control how to display unibyte 8-bit character. */
321
322 int unibyte_display_via_language_environment;
323
324 /* Nonzero means we have more than one non-mini-buffer-only frame.
325 Not guaranteed to be accurate except while parsing
326 frame-title-format. */
327
328 int multiple_frames;
329
330 Lisp_Object Vglobal_mode_string;
331
332 /* Marker for where to display an arrow on top of the buffer text. */
333
334 Lisp_Object Voverlay_arrow_position;
335
336 /* String to display for the arrow. Only used on terminal frames. */
337
338 Lisp_Object Voverlay_arrow_string;
339
340 /* Values of those variables at last redisplay. However, if
341 Voverlay_arrow_position is a marker, last_arrow_position is its
342 numerical position. */
343
344 static Lisp_Object last_arrow_position, last_arrow_string;
345
346 /* Like mode-line-format, but for the title bar on a visible frame. */
347
348 Lisp_Object Vframe_title_format;
349
350 /* Like mode-line-format, but for the title bar on an iconified frame. */
351
352 Lisp_Object Vicon_title_format;
353
354 /* List of functions to call when a window's size changes. These
355 functions get one arg, a frame on which one or more windows' sizes
356 have changed. */
357
358 static Lisp_Object Vwindow_size_change_functions;
359
360 Lisp_Object Qmenu_bar_update_hook;
361
362 /* Nonzero if overlay arrow has been displayed once in this window. */
363
364 static int overlay_arrow_seen;
365
366 /* Nonzero means highlight the region even in nonselected windows. */
367
368 int highlight_nonselected_windows;
369
370 /* If cursor motion alone moves point off frame, try scrolling this
371 many lines up or down if that will bring it back. */
372
373 static int scroll_step;
374
375 /* Non-0 means scroll just far enough to bring point back on the
376 screen, when appropriate. */
377
378 static int scroll_conservatively;
379
380 /* Recenter the window whenever point gets within this many lines of
381 the top or bottom of the window. This value is translated into a
382 pixel value by multiplying it with CANON_Y_UNIT, which means that
383 there is really a fixed pixel height scroll margin. */
384
385 int scroll_margin;
386
387 /* Number of windows showing the buffer of the selected window (or
388 another buffer with the same base buffer). keyboard.c refers to
389 this. */
390
391 int buffer_shared;
392
393 /* Vector containing glyphs for an ellipsis `...'. */
394
395 static Lisp_Object default_invis_vector[3];
396
397 /* Nonzero means display mode line highlighted. */
398
399 int mode_line_inverse_video;
400
401 /* Prompt to display in front of the mini-buffer contents. */
402
403 Lisp_Object minibuf_prompt;
404
405 /* Width of current mini-buffer prompt. Only set after display_line
406 of the line that contains the prompt. */
407
408 int minibuf_prompt_width;
409 int minibuf_prompt_pixel_width;
410
411 /* This is the window where the echo area message was displayed. It
412 is always a mini-buffer window, but it may not be the same window
413 currently active as a mini-buffer. */
414
415 Lisp_Object echo_area_window;
416
417 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
418 pushes the current message and the value of
419 message_enable_multibyte on the stack, the function restore_message
420 pops the stack and displays MESSAGE again. */
421
422 Lisp_Object Vmessage_stack;
423
424 /* Nonzero means multibyte characters were enabled when the echo area
425 message was specified. */
426
427 int message_enable_multibyte;
428
429 /* True if we should redraw the mode lines on the next redisplay. */
430
431 int update_mode_lines;
432
433 /* Nonzero if window sizes or contents have changed since last
434 redisplay that finished */
435
436 int windows_or_buffers_changed;
437
438 /* Nonzero after display_mode_line if %l was used and it displayed a
439 line number. */
440
441 int line_number_displayed;
442
443 /* Maximum buffer size for which to display line numbers. */
444
445 static int line_number_display_limit;
446
447 /* line width to consider when repostioning for line number display */
448
449 static int line_number_display_limit_width;
450
451 /* Number of lines to keep in the message log buffer. t means
452 infinite. nil means don't log at all. */
453
454 Lisp_Object Vmessage_log_max;
455
456 /* Current, index 0, and last displayed echo area message. Either
457 buffers from echo_buffers, or nil to indicate no message. */
458
459 Lisp_Object echo_area_buffer[2];
460
461 /* The buffers referenced from echo_area_buffer. */
462
463 static Lisp_Object echo_buffer[2];
464
465 /* A vector saved used in with_area_buffer to reduce consing. */
466
467 static Lisp_Object Vwith_echo_area_save_vector;
468
469 /* Non-zero means display_echo_area should display the last echo area
470 message again. Set by redisplay_preserve_echo_area. */
471
472 static int display_last_displayed_message_p;
473
474 /* Nonzero if echo area is being used by print; zero if being used by
475 message. */
476
477 int message_buf_print;
478
479 /* Maximum height for resizing mini-windows. Either a float
480 specifying a fraction of the available height, or an integer
481 specifying a number of lines. */
482
483 static Lisp_Object Vmax_mini_window_height;
484
485 /* Non-zero means we want a hollow cursor in windows that are not
486 selected. Zero means there's no cursor in such windows. */
487
488 int cursor_in_non_selected_windows;
489
490 /* A scratch glyph row with contents used for generating truncation
491 glyphs. Also used in direct_output_for_insert. */
492
493 #define MAX_SCRATCH_GLYPHS 100
494 struct glyph_row scratch_glyph_row;
495 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
496
497 /* Ascent and height of the last line processed by move_it_to. */
498
499 static int last_max_ascent, last_height;
500
501 /* The maximum distance to look ahead for text properties. Values
502 that are too small let us call compute_char_face and similar
503 functions too often which is expensive. Values that are too large
504 let us call compute_char_face and alike too often because we
505 might not be interested in text properties that far away. */
506
507 #define TEXT_PROP_DISTANCE_LIMIT 100
508
509 /* Non-zero means print traces of redisplay if compiled with
510 GLYPH_DEBUG != 0. */
511
512 #if GLYPH_DEBUG
513 int trace_redisplay_p;
514 #endif
515
516 /* Non-zero means automatically scroll windows horizontally to make
517 point visible. */
518
519 int automatic_hscrolling_p;
520
521 /* A list of symbols, one for each supported image type. */
522
523 Lisp_Object Vimage_types;
524
525 /* Value returned from text property handlers (see below). */
526
527 enum prop_handled
528 {
529 HANDLED_NORMALLY,
530 HANDLED_RECOMPUTE_PROPS,
531 HANDLED_OVERLAY_STRING_CONSUMED,
532 HANDLED_RETURN
533 };
534
535 /* A description of text properties that redisplay is interested
536 in. */
537
538 struct props
539 {
540 /* The name of the property. */
541 Lisp_Object *name;
542
543 /* A unique index for the property. */
544 enum prop_idx idx;
545
546 /* A handler function called to set up iterator IT from the property
547 at IT's current position. Value is used to steer handle_stop. */
548 enum prop_handled (*handler) P_ ((struct it *it));
549 };
550
551 static enum prop_handled handle_face_prop P_ ((struct it *));
552 static enum prop_handled handle_invisible_prop P_ ((struct it *));
553 static enum prop_handled handle_display_prop P_ ((struct it *));
554 static enum prop_handled handle_composition_prop P_ ((struct it *));
555 static enum prop_handled handle_overlay_change P_ ((struct it *));
556 static enum prop_handled handle_fontified_prop P_ ((struct it *));
557
558 /* Properties handled by iterators. */
559
560 static struct props it_props[] =
561 {
562 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
563 /* Handle `face' before `display' because some sub-properties of
564 `display' need to know the face. */
565 {&Qface, FACE_PROP_IDX, handle_face_prop},
566 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
567 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
568 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
569 {NULL, 0, NULL}
570 };
571
572 /* Value is the position described by X. If X is a marker, value is
573 the marker_position of X. Otherwise, value is X. */
574
575 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
576
577 /* Enumeration returned by some move_it_.* functions internally. */
578
579 enum move_it_result
580 {
581 /* Not used. Undefined value. */
582 MOVE_UNDEFINED,
583
584 /* Move ended at the requested buffer position or ZV. */
585 MOVE_POS_MATCH_OR_ZV,
586
587 /* Move ended at the requested X pixel position. */
588 MOVE_X_REACHED,
589
590 /* Move within a line ended at the end of a line that must be
591 continued. */
592 MOVE_LINE_CONTINUED,
593
594 /* Move within a line ended at the end of a line that would
595 be displayed truncated. */
596 MOVE_LINE_TRUNCATED,
597
598 /* Move within a line ended at a line end. */
599 MOVE_NEWLINE_OR_CR
600 };
601
602
603 \f
604 /* Function prototypes. */
605
606 static void ensure_echo_area_buffers P_ ((void));
607 static struct glyph_row *row_containing_pos P_ ((struct window *, int,
608 struct glyph_row *,
609 struct glyph_row *));
610 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
611 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
612 static void clear_garbaged_frames P_ ((void));
613 static int current_message_1 P_ ((Lisp_Object *));
614 static int truncate_message_1 P_ ((int));
615 static int set_message_1 P_ ((char *s, Lisp_Object, int, int));
616 static int display_echo_area P_ ((struct window *));
617 static int display_echo_area_1 P_ ((struct window *));
618 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
619 static int string_char_and_length P_ ((unsigned char *, int, int *));
620 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
621 struct text_pos));
622 static int compute_window_start_on_continuation_line P_ ((struct window *));
623 static Lisp_Object eval_handler P_ ((Lisp_Object));
624 static Lisp_Object eval_form P_ ((Lisp_Object));
625 static void insert_left_trunc_glyphs P_ ((struct it *));
626 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
627 static void extend_face_to_end_of_line P_ ((struct it *));
628 static int append_space P_ ((struct it *, int));
629 static void make_cursor_line_fully_visible P_ ((struct window *));
630 static int try_scrolling P_ ((Lisp_Object, int, int, int, int));
631 static int trailing_whitespace_p P_ ((int));
632 static int message_log_check_duplicate P_ ((int, int, int, int));
633 int invisible_p P_ ((Lisp_Object, Lisp_Object));
634 int invisible_ellipsis_p P_ ((Lisp_Object, Lisp_Object));
635 static void push_it P_ ((struct it *));
636 static void pop_it P_ ((struct it *));
637 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
638 static void redisplay_internal P_ ((int));
639 static int echo_area_display P_ ((int));
640 static void redisplay_windows P_ ((Lisp_Object));
641 static void redisplay_window P_ ((Lisp_Object, int));
642 static void update_menu_bar P_ ((struct frame *, int));
643 static int try_window_reusing_current_matrix P_ ((struct window *));
644 static int try_window_id P_ ((struct window *));
645 static int display_line P_ ((struct it *));
646 static void display_mode_lines P_ ((struct window *));
647 static void display_mode_line P_ ((struct window *, enum face_id,
648 Lisp_Object));
649 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object));
650 static char *decode_mode_spec P_ ((struct window *, int, int, int));
651 static void display_menu_bar P_ ((struct window *));
652 static int display_count_lines P_ ((int, int, int, int, int *));
653 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
654 int, int, struct it *, int, int, int, int));
655 static void compute_line_metrics P_ ((struct it *));
656 static void run_redisplay_end_trigger_hook P_ ((struct it *));
657 static int get_overlay_strings P_ ((struct it *));
658 static void next_overlay_string P_ ((struct it *));
659 void set_iterator_to_next P_ ((struct it *));
660 static void reseat P_ ((struct it *, struct text_pos, int));
661 static void reseat_1 P_ ((struct it *, struct text_pos, int));
662 static void back_to_previous_visible_line_start P_ ((struct it *));
663 static void reseat_at_previous_visible_line_start P_ ((struct it *));
664 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
665 static int next_element_from_display_vector P_ ((struct it *));
666 static int next_element_from_string P_ ((struct it *));
667 static int next_element_from_c_string P_ ((struct it *));
668 static int next_element_from_buffer P_ ((struct it *));
669 static int next_element_from_composition P_ ((struct it *));
670 static int next_element_from_image P_ ((struct it *));
671 static int next_element_from_stretch P_ ((struct it *));
672 static void load_overlay_strings P_ ((struct it *));
673 static void init_from_display_pos P_ ((struct it *, struct window *,
674 struct display_pos *));
675 static void reseat_to_string P_ ((struct it *, unsigned char *,
676 Lisp_Object, int, int, int, int));
677 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
678 int, int, int));
679 void move_it_vertically_backward P_ ((struct it *, int));
680 static void init_to_row_start P_ ((struct it *, struct window *,
681 struct glyph_row *));
682 static void init_to_row_end P_ ((struct it *, struct window *,
683 struct glyph_row *));
684 static void back_to_previous_line_start P_ ((struct it *));
685 static void forward_to_next_line_start P_ ((struct it *));
686 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
687 Lisp_Object, int));
688 static struct text_pos string_pos P_ ((int, Lisp_Object));
689 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
690 static int number_of_chars P_ ((unsigned char *, int));
691 static void compute_stop_pos P_ ((struct it *));
692 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
693 Lisp_Object));
694 static int face_before_or_after_it_pos P_ ((struct it *, int));
695 static int next_overlay_change P_ ((int));
696 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
697 Lisp_Object, struct text_pos *));
698
699 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
700 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
701
702 #ifdef HAVE_WINDOW_SYSTEM
703
704 static void update_tool_bar P_ ((struct frame *, int));
705 static void build_desired_tool_bar_string P_ ((struct frame *f));
706 static int redisplay_tool_bar P_ ((struct frame *));
707 static void display_tool_bar_line P_ ((struct it *));
708
709 #endif /* HAVE_WINDOW_SYSTEM */
710
711 \f
712 /***********************************************************************
713 Window display dimensions
714 ***********************************************************************/
715
716 /* Return the window-relative maximum y + 1 for glyph rows displaying
717 text in window W. This is the height of W minus the height of a
718 mode line, if any. */
719
720 INLINE int
721 window_text_bottom_y (w)
722 struct window *w;
723 {
724 struct frame *f = XFRAME (w->frame);
725 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
726
727 if (WINDOW_WANTS_MODELINE_P (w))
728 height -= CURRENT_MODE_LINE_HEIGHT (w);
729 return height;
730 }
731
732
733 /* Return the pixel width of display area AREA of window W. AREA < 0
734 means return the total width of W, not including bitmap areas to
735 the left and right of the window. */
736
737 INLINE int
738 window_box_width (w, area)
739 struct window *w;
740 int area;
741 {
742 struct frame *f = XFRAME (w->frame);
743 int width = XFASTINT (w->width);
744
745 if (!w->pseudo_window_p)
746 {
747 width -= FRAME_SCROLL_BAR_WIDTH (f) + FRAME_FLAGS_AREA_COLS (f);
748
749 if (area == TEXT_AREA)
750 {
751 if (INTEGERP (w->left_margin_width))
752 width -= XFASTINT (w->left_margin_width);
753 if (INTEGERP (w->right_margin_width))
754 width -= XFASTINT (w->right_margin_width);
755 }
756 else if (area == LEFT_MARGIN_AREA)
757 width = (INTEGERP (w->left_margin_width)
758 ? XFASTINT (w->left_margin_width) : 0);
759 else if (area == RIGHT_MARGIN_AREA)
760 width = (INTEGERP (w->right_margin_width)
761 ? XFASTINT (w->right_margin_width) : 0);
762 }
763
764 return width * CANON_X_UNIT (f);
765 }
766
767
768 /* Return the pixel height of the display area of window W, not
769 including mode lines of W, if any.. */
770
771 INLINE int
772 window_box_height (w)
773 struct window *w;
774 {
775 struct frame *f = XFRAME (w->frame);
776 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
777
778 if (WINDOW_WANTS_MODELINE_P (w))
779 height -= CURRENT_MODE_LINE_HEIGHT (w);
780
781 if (WINDOW_WANTS_HEADER_LINE_P (w))
782 height -= CURRENT_HEADER_LINE_HEIGHT (w);
783
784 return height;
785 }
786
787
788 /* Return the frame-relative coordinate of the left edge of display
789 area AREA of window W. AREA < 0 means return the left edge of the
790 whole window, to the right of any bitmap area at the left side of
791 W. */
792
793 INLINE int
794 window_box_left (w, area)
795 struct window *w;
796 int area;
797 {
798 struct frame *f = XFRAME (w->frame);
799 int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
800
801 if (!w->pseudo_window_p)
802 {
803 x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
804 + FRAME_LEFT_FLAGS_AREA_WIDTH (f));
805
806 if (area == TEXT_AREA)
807 x += window_box_width (w, LEFT_MARGIN_AREA);
808 else if (area == RIGHT_MARGIN_AREA)
809 x += (window_box_width (w, LEFT_MARGIN_AREA)
810 + window_box_width (w, TEXT_AREA));
811 }
812
813 return x;
814 }
815
816
817 /* Return the frame-relative coordinate of the right edge of display
818 area AREA of window W. AREA < 0 means return the left edge of the
819 whole window, to the left of any bitmap area at the right side of
820 W. */
821
822 INLINE int
823 window_box_right (w, area)
824 struct window *w;
825 int area;
826 {
827 return window_box_left (w, area) + window_box_width (w, area);
828 }
829
830
831 /* Get the bounding box of the display area AREA of window W, without
832 mode lines, in frame-relative coordinates. AREA < 0 means the
833 whole window, not including bitmap areas to the left and right of
834 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
835 coordinates of the upper-left corner of the box. Return in
836 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
837
838 INLINE void
839 window_box (w, area, box_x, box_y, box_width, box_height)
840 struct window *w;
841 int area;
842 int *box_x, *box_y, *box_width, *box_height;
843 {
844 struct frame *f = XFRAME (w->frame);
845
846 *box_width = window_box_width (w, area);
847 *box_height = window_box_height (w);
848 *box_x = window_box_left (w, area);
849 *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
850 + XFASTINT (w->top) * CANON_Y_UNIT (f));
851 if (WINDOW_WANTS_HEADER_LINE_P (w))
852 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
853 }
854
855
856 /* Get the bounding box of the display area AREA of window W, without
857 mode lines. AREA < 0 means the whole window, not including bitmap
858 areas to the left and right of the window. Return in *TOP_LEFT_X
859 and TOP_LEFT_Y the frame-relative pixel coordinates of the
860 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
861 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
862 box. */
863
864 INLINE void
865 window_box_edges (w, area, top_left_x, top_left_y,
866 bottom_right_x, bottom_right_y)
867 struct window *w;
868 int area;
869 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
870 {
871 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
872 bottom_right_y);
873 *bottom_right_x += *top_left_x;
874 *bottom_right_y += *top_left_y;
875 }
876
877
878 \f
879 /***********************************************************************
880 Utilities
881 ***********************************************************************/
882
883 /* Return the next character from STR which is MAXLEN bytes long.
884 Return in *LEN the length of the character. This is like
885 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
886 we find one, we return a `?', but with the length of the invalid
887 character. */
888
889 static INLINE int
890 string_char_and_length (str, maxlen, len)
891 unsigned char *str;
892 int maxlen, *len;
893 {
894 int c;
895
896 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
897 if (!CHAR_VALID_P (c, 1))
898 /* We may not change the length here because other places in Emacs
899 don't use this function, i.e. they silently accept invalid
900 characters. */
901 c = '?';
902
903 return c;
904 }
905
906
907
908 /* Given a position POS containing a valid character and byte position
909 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
910
911 static struct text_pos
912 string_pos_nchars_ahead (pos, string, nchars)
913 struct text_pos pos;
914 Lisp_Object string;
915 int nchars;
916 {
917 xassert (STRINGP (string) && nchars >= 0);
918
919 if (STRING_MULTIBYTE (string))
920 {
921 int rest = STRING_BYTES (XSTRING (string)) - BYTEPOS (pos);
922 unsigned char *p = XSTRING (string)->data + BYTEPOS (pos);
923 int len;
924
925 while (nchars--)
926 {
927 string_char_and_length (p, rest, &len);
928 p += len, rest -= len;
929 xassert (rest >= 0);
930 CHARPOS (pos) += 1;
931 BYTEPOS (pos) += len;
932 }
933 }
934 else
935 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
936
937 return pos;
938 }
939
940
941 /* Value is the text position, i.e. character and byte position,
942 for character position CHARPOS in STRING. */
943
944 static INLINE struct text_pos
945 string_pos (charpos, string)
946 int charpos;
947 Lisp_Object string;
948 {
949 struct text_pos pos;
950 xassert (STRINGP (string));
951 xassert (charpos >= 0);
952 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
953 return pos;
954 }
955
956
957 /* Value is a text position, i.e. character and byte position, for
958 character position CHARPOS in C string S. MULTIBYTE_P non-zero
959 means recognize multibyte characters. */
960
961 static struct text_pos
962 c_string_pos (charpos, s, multibyte_p)
963 int charpos;
964 unsigned char *s;
965 int multibyte_p;
966 {
967 struct text_pos pos;
968
969 xassert (s != NULL);
970 xassert (charpos >= 0);
971
972 if (multibyte_p)
973 {
974 int rest = strlen (s), len;
975
976 SET_TEXT_POS (pos, 0, 0);
977 while (charpos--)
978 {
979 string_char_and_length (s, rest, &len);
980 s += len, rest -= len;
981 xassert (rest >= 0);
982 CHARPOS (pos) += 1;
983 BYTEPOS (pos) += len;
984 }
985 }
986 else
987 SET_TEXT_POS (pos, charpos, charpos);
988
989 return pos;
990 }
991
992
993 /* Value is the number of characters in C string S. MULTIBYTE_P
994 non-zero means recognize multibyte characters. */
995
996 static int
997 number_of_chars (s, multibyte_p)
998 unsigned char *s;
999 int multibyte_p;
1000 {
1001 int nchars;
1002
1003 if (multibyte_p)
1004 {
1005 int rest = strlen (s), len;
1006 unsigned char *p = (unsigned char *) s;
1007
1008 for (nchars = 0; rest > 0; ++nchars)
1009 {
1010 string_char_and_length (p, rest, &len);
1011 rest -= len, p += len;
1012 }
1013 }
1014 else
1015 nchars = strlen (s);
1016
1017 return nchars;
1018 }
1019
1020
1021 /* Compute byte position NEWPOS->bytepos corresponding to
1022 NEWPOS->charpos. POS is a known position in string STRING.
1023 NEWPOS->charpos must be >= POS.charpos. */
1024
1025 static void
1026 compute_string_pos (newpos, pos, string)
1027 struct text_pos *newpos, pos;
1028 Lisp_Object string;
1029 {
1030 xassert (STRINGP (string));
1031 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1032
1033 if (STRING_MULTIBYTE (string))
1034 *newpos = string_pos_nchars_ahead (pos, string,
1035 CHARPOS (*newpos) - CHARPOS (pos));
1036 else
1037 BYTEPOS (*newpos) = CHARPOS (*newpos);
1038 }
1039
1040
1041 \f
1042 /***********************************************************************
1043 Lisp form evaluation
1044 ***********************************************************************/
1045
1046 /* Error handler for eval_form. */
1047
1048 static Lisp_Object
1049 eval_handler (arg)
1050 Lisp_Object arg;
1051 {
1052 return Qnil;
1053 }
1054
1055
1056 /* Evaluate SEXPR and return the result, or nil if something went
1057 wrong. */
1058
1059 static Lisp_Object
1060 eval_form (sexpr)
1061 Lisp_Object sexpr;
1062 {
1063 int count = specpdl_ptr - specpdl;
1064 Lisp_Object val;
1065 specbind (Qinhibit_redisplay, Qt);
1066 val = internal_condition_case_1 (Feval, sexpr, Qerror, eval_handler);
1067 return unbind_to (count, val);
1068 }
1069
1070
1071 \f
1072 /***********************************************************************
1073 Debugging
1074 ***********************************************************************/
1075
1076 #if 0
1077
1078 /* Define CHECK_IT to perform sanity checks on iterators.
1079 This is for debugging. It is too slow to do unconditionally. */
1080
1081 static void
1082 check_it (it)
1083 struct it *it;
1084 {
1085 if (it->method == next_element_from_string)
1086 {
1087 xassert (STRINGP (it->string));
1088 xassert (IT_STRING_CHARPOS (*it) >= 0);
1089 }
1090 else if (it->method == next_element_from_buffer)
1091 {
1092 /* Check that character and byte positions agree. */
1093 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1094 }
1095
1096 if (it->dpvec)
1097 xassert (it->current.dpvec_index >= 0);
1098 else
1099 xassert (it->current.dpvec_index < 0);
1100 }
1101
1102 #define CHECK_IT(IT) check_it ((IT))
1103
1104 #else /* not 0 */
1105
1106 #define CHECK_IT(IT) (void) 0
1107
1108 #endif /* not 0 */
1109
1110
1111 #if GLYPH_DEBUG
1112
1113 /* Check that the window end of window W is what we expect it
1114 to be---the last row in the current matrix displaying text. */
1115
1116 static void
1117 check_window_end (w)
1118 struct window *w;
1119 {
1120 if (!MINI_WINDOW_P (w)
1121 && !NILP (w->window_end_valid))
1122 {
1123 struct glyph_row *row;
1124 xassert ((row = MATRIX_ROW (w->current_matrix,
1125 XFASTINT (w->window_end_vpos)),
1126 !row->enabled_p
1127 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1128 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1129 }
1130 }
1131
1132 #define CHECK_WINDOW_END(W) check_window_end ((W))
1133
1134 #else /* not GLYPH_DEBUG */
1135
1136 #define CHECK_WINDOW_END(W) (void) 0
1137
1138 #endif /* not GLYPH_DEBUG */
1139
1140
1141 \f
1142 /***********************************************************************
1143 Iterator initialization
1144 ***********************************************************************/
1145
1146 /* Initialize IT for displaying current_buffer in window W, starting
1147 at character position CHARPOS. CHARPOS < 0 means that no buffer
1148 position is specified which is useful when the iterator is assigned
1149 a position later. BYTEPOS is the byte position corresponding to
1150 CHARPOS. BYTEPOS <= 0 means compute it from CHARPOS.
1151
1152 If ROW is not null, calls to produce_glyphs with IT as parameter
1153 will produce glyphs in that row.
1154
1155 BASE_FACE_ID is the id of a base face to use. It must be one of
1156 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID or
1157 HEADER_LINE_FACE_ID for displaying mode lines, or TOOL_BAR_FACE_ID for
1158 displaying the tool-bar.
1159
1160 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID or
1161 HEADER_LINE_FACE_ID, the iterator will be initialized to use the
1162 corresponding mode line glyph row of the desired matrix of W. */
1163
1164 void
1165 init_iterator (it, w, charpos, bytepos, row, base_face_id)
1166 struct it *it;
1167 struct window *w;
1168 int charpos, bytepos;
1169 struct glyph_row *row;
1170 enum face_id base_face_id;
1171 {
1172 int highlight_region_p;
1173
1174 /* Some precondition checks. */
1175 xassert (w != NULL && it != NULL);
1176 xassert (charpos < 0 || (charpos > 0 && charpos <= ZV));
1177
1178 /* If face attributes have been changed since the last redisplay,
1179 free realized faces now because they depend on face definitions
1180 that might have changed. */
1181 if (face_change_count)
1182 {
1183 face_change_count = 0;
1184 free_all_realized_faces (Qnil);
1185 }
1186
1187 /* Use one of the mode line rows of W's desired matrix if
1188 appropriate. */
1189 if (row == NULL)
1190 {
1191 if (base_face_id == MODE_LINE_FACE_ID)
1192 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
1193 else if (base_face_id == HEADER_LINE_FACE_ID)
1194 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
1195 }
1196
1197 /* Clear IT. */
1198 bzero (it, sizeof *it);
1199 it->current.overlay_string_index = -1;
1200 it->current.dpvec_index = -1;
1201 it->base_face_id = base_face_id;
1202
1203 /* The window in which we iterate over current_buffer: */
1204 XSETWINDOW (it->window, w);
1205 it->w = w;
1206 it->f = XFRAME (w->frame);
1207
1208 /* Extra space between lines (on window systems only). */
1209 if (base_face_id == DEFAULT_FACE_ID
1210 && FRAME_WINDOW_P (it->f))
1211 {
1212 if (NATNUMP (current_buffer->extra_line_spacing))
1213 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
1214 else if (it->f->extra_line_spacing > 0)
1215 it->extra_line_spacing = it->f->extra_line_spacing;
1216 }
1217
1218 /* If realized faces have been removed, e.g. because of face
1219 attribute changes of named faces, recompute them. */
1220 if (FRAME_FACE_CACHE (it->f)->used == 0)
1221 recompute_basic_faces (it->f);
1222
1223 /* Current value of the `space-width', and 'height' properties. */
1224 it->space_width = Qnil;
1225 it->font_height = Qnil;
1226
1227 /* Are control characters displayed as `^C'? */
1228 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
1229
1230 /* -1 means everything between a CR and the following line end
1231 is invisible. >0 means lines indented more than this value are
1232 invisible. */
1233 it->selective = (INTEGERP (current_buffer->selective_display)
1234 ? XFASTINT (current_buffer->selective_display)
1235 : (!NILP (current_buffer->selective_display)
1236 ? -1 : 0));
1237 it->selective_display_ellipsis_p
1238 = !NILP (current_buffer->selective_display_ellipses);
1239
1240 /* Display table to use. */
1241 it->dp = window_display_table (w);
1242
1243 /* Are multibyte characters enabled in current_buffer? */
1244 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1245
1246 /* Non-zero if we should highlight the region. */
1247 highlight_region_p
1248 = (!NILP (Vtransient_mark_mode)
1249 && !NILP (current_buffer->mark_active)
1250 && XMARKER (current_buffer->mark)->buffer != 0);
1251
1252 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
1253 start and end of a visible region in window IT->w. Set both to
1254 -1 to indicate no region. */
1255 if (highlight_region_p
1256 /* Maybe highlight only in selected window. */
1257 && (/* Either show region everywhere. */
1258 highlight_nonselected_windows
1259 /* Or show region in the selected window. */
1260 || w == XWINDOW (selected_window)
1261 /* Or show the region if we are in the mini-buffer and W is
1262 the window the mini-buffer refers to. */
1263 || (MINI_WINDOW_P (XWINDOW (selected_window))
1264 && w == XWINDOW (Vminibuf_scroll_window))))
1265 {
1266 int charpos = marker_position (current_buffer->mark);
1267 it->region_beg_charpos = min (PT, charpos);
1268 it->region_end_charpos = max (PT, charpos);
1269 }
1270 else
1271 it->region_beg_charpos = it->region_end_charpos = -1;
1272
1273 /* Get the position at which the redisplay_end_trigger hook should
1274 be run, if it is to be run at all. */
1275 if (MARKERP (w->redisplay_end_trigger)
1276 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
1277 it->redisplay_end_trigger_charpos
1278 = marker_position (w->redisplay_end_trigger);
1279 else if (INTEGERP (w->redisplay_end_trigger))
1280 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
1281
1282 /* Correct bogus values of tab_width. */
1283 it->tab_width = XINT (current_buffer->tab_width);
1284 if (it->tab_width <= 0 || it->tab_width > 1000)
1285 it->tab_width = 8;
1286
1287 /* Are lines in the display truncated? */
1288 it->truncate_lines_p
1289 = (base_face_id != DEFAULT_FACE_ID
1290 || XINT (it->w->hscroll)
1291 || (truncate_partial_width_windows
1292 && !WINDOW_FULL_WIDTH_P (it->w))
1293 || !NILP (current_buffer->truncate_lines));
1294
1295 /* Get dimensions of truncation and continuation glyphs. These are
1296 displayed as bitmaps under X, so we don't need them for such
1297 frames. */
1298 if (!FRAME_WINDOW_P (it->f))
1299 {
1300 if (it->truncate_lines_p)
1301 {
1302 /* We will need the truncation glyph. */
1303 xassert (it->glyph_row == NULL);
1304 produce_special_glyphs (it, IT_TRUNCATION);
1305 it->truncation_pixel_width = it->pixel_width;
1306 }
1307 else
1308 {
1309 /* We will need the continuation glyph. */
1310 xassert (it->glyph_row == NULL);
1311 produce_special_glyphs (it, IT_CONTINUATION);
1312 it->continuation_pixel_width = it->pixel_width;
1313 }
1314
1315 /* Reset these values to zero becaue the produce_special_glyphs
1316 above has changed them. */
1317 it->pixel_width = it->ascent = it->descent = 0;
1318 it->phys_ascent = it->phys_descent = 0;
1319 }
1320
1321 /* Set this after getting the dimensions of truncation and
1322 continuation glyphs, so that we don't produce glyphs when calling
1323 produce_special_glyphs, above. */
1324 it->glyph_row = row;
1325 it->area = TEXT_AREA;
1326
1327 /* Get the dimensions of the display area. The display area
1328 consists of the visible window area plus a horizontally scrolled
1329 part to the left of the window. All x-values are relative to the
1330 start of this total display area. */
1331 if (base_face_id != DEFAULT_FACE_ID)
1332 {
1333 /* Mode lines, menu bar in terminal frames. */
1334 it->first_visible_x = 0;
1335 it->last_visible_x = XFASTINT (w->width) * CANON_X_UNIT (it->f);
1336 }
1337 else
1338 {
1339 it->first_visible_x
1340 = XFASTINT (it->w->hscroll) * CANON_X_UNIT (it->f);
1341 it->last_visible_x = (it->first_visible_x
1342 + window_box_width (w, TEXT_AREA));
1343
1344 /* If we truncate lines, leave room for the truncator glyph(s) at
1345 the right margin. Otherwise, leave room for the continuation
1346 glyph(s). Truncation and continuation glyphs are not inserted
1347 for window-based redisplay. */
1348 if (!FRAME_WINDOW_P (it->f))
1349 {
1350 if (it->truncate_lines_p)
1351 it->last_visible_x -= it->truncation_pixel_width;
1352 else
1353 it->last_visible_x -= it->continuation_pixel_width;
1354 }
1355
1356 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
1357 it->current_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w) + w->vscroll;
1358 }
1359
1360 /* Leave room for a border glyph. */
1361 if (!FRAME_WINDOW_P (it->f)
1362 && !WINDOW_RIGHTMOST_P (it->w))
1363 it->last_visible_x -= 1;
1364
1365 it->last_visible_y = window_text_bottom_y (w);
1366
1367 /* For mode lines and alike, arrange for the first glyph having a
1368 left box line if the face specifies a box. */
1369 if (base_face_id != DEFAULT_FACE_ID)
1370 {
1371 struct face *face;
1372
1373 it->face_id = base_face_id;
1374
1375 /* If we have a boxed mode line, make the first character appear
1376 with a left box line. */
1377 face = FACE_FROM_ID (it->f, base_face_id);
1378 if (face->box != FACE_NO_BOX)
1379 it->start_of_box_run_p = 1;
1380 }
1381
1382 /* If a buffer position was specified, set the iterator there,
1383 getting overlays and face properties from that position. */
1384 if (charpos > 0)
1385 {
1386 it->end_charpos = ZV;
1387 it->face_id = -1;
1388 IT_CHARPOS (*it) = charpos;
1389
1390 /* Compute byte position if not specified. */
1391 if (bytepos <= 0)
1392 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
1393 else
1394 IT_BYTEPOS (*it) = bytepos;
1395
1396 /* Compute faces etc. */
1397 reseat (it, it->current.pos, 1);
1398 }
1399
1400 CHECK_IT (it);
1401 }
1402
1403
1404 /* Initialize IT for the display of window W with window start POS. */
1405
1406 void
1407 start_display (it, w, pos)
1408 struct it *it;
1409 struct window *w;
1410 struct text_pos pos;
1411 {
1412 int start_at_line_beg_p;
1413 struct glyph_row *row;
1414 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
1415 int first_y;
1416
1417 row = w->desired_matrix->rows + first_vpos;
1418 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
1419 first_y = it->current_y;
1420
1421 /* If window start is not at a line start, move back to the line
1422 start. This makes sure that we take continuation lines into
1423 account. */
1424 start_at_line_beg_p = (CHARPOS (pos) == BEGV
1425 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
1426 if (!start_at_line_beg_p)
1427 reseat_at_previous_visible_line_start (it);
1428
1429 /* If window start is not at a line start, skip forward to POS to
1430 get the correct continuation_lines_width and current_x. */
1431 if (!start_at_line_beg_p)
1432 {
1433 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
1434
1435 /* If lines are continued, this line may end in the middle of a
1436 multi-glyph character (e.g. a control character displayed as
1437 \003, or in the middle of an overlay string). In this case
1438 move_it_to above will not have taken us to the start of
1439 the continuation line but to the end of the continued line. */
1440 if (!it->truncate_lines_p && it->current_x > 0)
1441 {
1442 if (it->current.dpvec_index >= 0
1443 || it->current.overlay_string_index >= 0)
1444 {
1445 set_iterator_to_next (it);
1446 move_it_in_display_line_to (it, -1, -1, 0);
1447 }
1448 it->continuation_lines_width += it->current_x;
1449 }
1450
1451 it->current_y = first_y;
1452 it->vpos = 0;
1453 it->current_x = it->hpos = 0;
1454 }
1455
1456 #if 0 /* Don't assert the following because start_display is sometimes
1457 called intentionally with a window start that is not at a
1458 line start. Please leave this code in as a comment. */
1459
1460 /* Window start should be on a line start, now. */
1461 xassert (it->continuation_lines_width
1462 || IT_CHARPOS (it) == BEGV
1463 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
1464 #endif /* 0 */
1465 }
1466
1467
1468 /* Initialize IT for stepping through current_buffer in window W,
1469 starting at position POS that includes overlay string and display
1470 vector/ control character translation position information. */
1471
1472 static void
1473 init_from_display_pos (it, w, pos)
1474 struct it *it;
1475 struct window *w;
1476 struct display_pos *pos;
1477 {
1478 /* Keep in mind: the call to reseat in init_iterator skips invisible
1479 text, so we might end up at a position different from POS. This
1480 is only a problem when POS is a row start after a newline and an
1481 overlay starts there with an after-string, and the overlay has an
1482 invisible property. Since we don't skip invisible text in
1483 display_line and elsewhere immediately after consuming the
1484 newline before the row start, such a POS will not be in a string,
1485 but the call to init_iterator below will move us to the
1486 after-string. */
1487 init_iterator (it, w, CHARPOS (pos->pos), BYTEPOS (pos->pos),
1488 NULL, DEFAULT_FACE_ID);
1489
1490 /* If position is within an overlay string, set up IT to
1491 the right overlay string. */
1492 if (pos->overlay_string_index >= 0)
1493 {
1494 int relative_index;
1495
1496 /* We already have the first chunk of overlay strings in
1497 IT->overlay_strings. Load more until the one for
1498 pos->overlay_string_index is in IT->overlay_strings. */
1499 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
1500 {
1501 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
1502 it->current.overlay_string_index = 0;
1503 while (n--)
1504 {
1505 load_overlay_strings (it);
1506 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
1507 }
1508 }
1509
1510 it->current.overlay_string_index = pos->overlay_string_index;
1511 relative_index = (it->current.overlay_string_index
1512 % OVERLAY_STRING_CHUNK_SIZE);
1513 it->string = it->overlay_strings[relative_index];
1514 it->current.string_pos = pos->string_pos;
1515 it->method = next_element_from_string;
1516 }
1517 else if (CHARPOS (pos->string_pos) >= 0)
1518 {
1519 /* Recorded position is not in an overlay string, but in another
1520 string. This can only be a string from a `display' property.
1521 IT should already be filled with that string. */
1522 it->current.string_pos = pos->string_pos;
1523 xassert (STRINGP (it->string));
1524 }
1525
1526 /* Restore position in display vector translations or control
1527 character translations. */
1528 if (pos->dpvec_index >= 0)
1529 {
1530 /* This fills IT->dpvec. */
1531 get_next_display_element (it);
1532 xassert (it->dpvec && it->current.dpvec_index == 0);
1533 it->current.dpvec_index = pos->dpvec_index;
1534 }
1535
1536 CHECK_IT (it);
1537 }
1538
1539
1540 /* Initialize IT for stepping through current_buffer in window W
1541 starting at ROW->start. */
1542
1543 static void
1544 init_to_row_start (it, w, row)
1545 struct it *it;
1546 struct window *w;
1547 struct glyph_row *row;
1548 {
1549 init_from_display_pos (it, w, &row->start);
1550 it->continuation_lines_width = row->continuation_lines_width;
1551 CHECK_IT (it);
1552 }
1553
1554
1555 /* Initialize IT for stepping through current_buffer in window W
1556 starting in the line following ROW, i.e. starting at ROW->end. */
1557
1558 static void
1559 init_to_row_end (it, w, row)
1560 struct it *it;
1561 struct window *w;
1562 struct glyph_row *row;
1563 {
1564 init_from_display_pos (it, w, &row->end);
1565
1566 if (row->continued_p)
1567 it->continuation_lines_width = (row->continuation_lines_width
1568 + row->pixel_width);
1569 CHECK_IT (it);
1570 }
1571
1572
1573
1574 \f
1575 /***********************************************************************
1576 Text properties
1577 ***********************************************************************/
1578
1579 /* Called when IT reaches IT->stop_charpos. Handle text property and
1580 overlay changes. Set IT->stop_charpos to the next position where
1581 to stop. */
1582
1583 static void
1584 handle_stop (it)
1585 struct it *it;
1586 {
1587 enum prop_handled handled;
1588 int handle_overlay_change_p = 1;
1589 struct props *p;
1590
1591 it->dpvec = NULL;
1592 it->current.dpvec_index = -1;
1593
1594 do
1595 {
1596 handled = HANDLED_NORMALLY;
1597
1598 /* Call text property handlers. */
1599 for (p = it_props; p->handler; ++p)
1600 {
1601 handled = p->handler (it);
1602
1603 if (handled == HANDLED_RECOMPUTE_PROPS)
1604 break;
1605 else if (handled == HANDLED_RETURN)
1606 return;
1607 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
1608 handle_overlay_change_p = 0;
1609 }
1610
1611 if (handled != HANDLED_RECOMPUTE_PROPS)
1612 {
1613 /* Don't check for overlay strings below when set to deliver
1614 characters from a display vector. */
1615 if (it->method == next_element_from_display_vector)
1616 handle_overlay_change_p = 0;
1617
1618 /* Handle overlay changes. */
1619 if (handle_overlay_change_p)
1620 handled = handle_overlay_change (it);
1621
1622 /* Determine where to stop next. */
1623 if (handled == HANDLED_NORMALLY)
1624 compute_stop_pos (it);
1625 }
1626 }
1627 while (handled == HANDLED_RECOMPUTE_PROPS);
1628 }
1629
1630
1631 /* Compute IT->stop_charpos from text property and overlay change
1632 information for IT's current position. */
1633
1634 static void
1635 compute_stop_pos (it)
1636 struct it *it;
1637 {
1638 register INTERVAL iv, next_iv;
1639 Lisp_Object object, limit, position;
1640
1641 /* If nowhere else, stop at the end. */
1642 it->stop_charpos = it->end_charpos;
1643
1644 if (STRINGP (it->string))
1645 {
1646 /* Strings are usually short, so don't limit the search for
1647 properties. */
1648 object = it->string;
1649 limit = Qnil;
1650 XSETFASTINT (position, IT_STRING_CHARPOS (*it));
1651 }
1652 else
1653 {
1654 int charpos;
1655
1656 /* If next overlay change is in front of the current stop pos
1657 (which is IT->end_charpos), stop there. Note: value of
1658 next_overlay_change is point-max if no overlay change
1659 follows. */
1660 charpos = next_overlay_change (IT_CHARPOS (*it));
1661 if (charpos < it->stop_charpos)
1662 it->stop_charpos = charpos;
1663
1664 /* If showing the region, we have to stop at the region
1665 start or end because the face might change there. */
1666 if (it->region_beg_charpos > 0)
1667 {
1668 if (IT_CHARPOS (*it) < it->region_beg_charpos)
1669 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
1670 else if (IT_CHARPOS (*it) < it->region_end_charpos)
1671 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
1672 }
1673
1674 /* Set up variables for computing the stop position from text
1675 property changes. */
1676 XSETBUFFER (object, current_buffer);
1677 XSETFASTINT (limit, IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
1678 XSETFASTINT (position, IT_CHARPOS (*it));
1679
1680 }
1681
1682 /* Get the interval containing IT's position. Value is a null
1683 interval if there isn't such an interval. */
1684 iv = validate_interval_range (object, &position, &position, 0);
1685 if (!NULL_INTERVAL_P (iv))
1686 {
1687 Lisp_Object values_here[LAST_PROP_IDX];
1688 struct props *p;
1689
1690 /* Get properties here. */
1691 for (p = it_props; p->handler; ++p)
1692 values_here[p->idx] = textget (iv->plist, *p->name);
1693
1694 /* Look for an interval following iv that has different
1695 properties. */
1696 for (next_iv = next_interval (iv);
1697 (!NULL_INTERVAL_P (next_iv)
1698 && (NILP (limit)
1699 || XFASTINT (limit) > next_iv->position));
1700 next_iv = next_interval (next_iv))
1701 {
1702 for (p = it_props; p->handler; ++p)
1703 {
1704 Lisp_Object new_value;
1705
1706 new_value = textget (next_iv->plist, *p->name);
1707 if (!EQ (values_here[p->idx], new_value))
1708 break;
1709 }
1710
1711 if (p->handler)
1712 break;
1713 }
1714
1715 if (!NULL_INTERVAL_P (next_iv))
1716 {
1717 if (INTEGERP (limit)
1718 && next_iv->position >= XFASTINT (limit))
1719 /* No text property change up to limit. */
1720 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
1721 else
1722 /* Text properties change in next_iv. */
1723 it->stop_charpos = min (it->stop_charpos, next_iv->position);
1724 }
1725 }
1726
1727 xassert (STRINGP (it->string)
1728 || (it->stop_charpos >= BEGV
1729 && it->stop_charpos >= IT_CHARPOS (*it)));
1730 }
1731
1732
1733 /* Return the position of the next overlay change after POS in
1734 current_buffer. Value is point-max if no overlay change
1735 follows. This is like `next-overlay-change' but doesn't use
1736 xmalloc. */
1737
1738 static int
1739 next_overlay_change (pos)
1740 int pos;
1741 {
1742 int noverlays;
1743 int endpos;
1744 Lisp_Object *overlays;
1745 int len;
1746 int i;
1747
1748 /* Get all overlays at the given position. */
1749 len = 10;
1750 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
1751 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL);
1752 if (noverlays > len)
1753 {
1754 len = noverlays;
1755 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
1756 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL);
1757 }
1758
1759 /* If any of these overlays ends before endpos,
1760 use its ending point instead. */
1761 for (i = 0; i < noverlays; ++i)
1762 {
1763 Lisp_Object oend;
1764 int oendpos;
1765
1766 oend = OVERLAY_END (overlays[i]);
1767 oendpos = OVERLAY_POSITION (oend);
1768 endpos = min (endpos, oendpos);
1769 }
1770
1771 return endpos;
1772 }
1773
1774
1775 \f
1776 /***********************************************************************
1777 Fontification
1778 ***********************************************************************/
1779
1780 /* Handle changes in the `fontified' property of the current buffer by
1781 calling hook functions from Qfontification_functions to fontify
1782 regions of text. */
1783
1784 static enum prop_handled
1785 handle_fontified_prop (it)
1786 struct it *it;
1787 {
1788 Lisp_Object prop, pos;
1789 enum prop_handled handled = HANDLED_NORMALLY;
1790 struct gcpro gcpro1;
1791
1792 /* Get the value of the `fontified' property at IT's current buffer
1793 position. (The `fontified' property doesn't have a special
1794 meaning in strings.) If the value is nil, call functions from
1795 Qfontification_functions. */
1796 if (!STRINGP (it->string)
1797 && it->s == NULL
1798 && !NILP (Vfontification_functions)
1799 && (pos = make_number (IT_CHARPOS (*it)),
1800 prop = Fget_char_property (pos, Qfontified, Qnil),
1801 NILP (prop)))
1802 {
1803 Lisp_Object args[2];
1804
1805 GCPRO1 (pos);
1806 /* Run the hook functions. */
1807 args[0] = Qfontification_functions;
1808 args[1] = pos;
1809 Frun_hook_with_args (2, args);
1810
1811 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
1812 something. This avoids an endless loop if they failed to
1813 fontify the text for which reason ever. */
1814 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
1815 handled = HANDLED_RECOMPUTE_PROPS;
1816 UNGCPRO;
1817 }
1818
1819 return handled;
1820 }
1821
1822
1823 \f
1824 /***********************************************************************
1825 Faces
1826 ***********************************************************************/
1827
1828 /* Set up iterator IT from face properties at its current position.
1829 Called from handle_stop. */
1830
1831 static enum prop_handled
1832 handle_face_prop (it)
1833 struct it *it;
1834 {
1835 int new_face_id, next_stop;
1836
1837 if (!STRINGP (it->string))
1838 {
1839 new_face_id
1840 = face_at_buffer_position (it->w,
1841 IT_CHARPOS (*it),
1842 it->region_beg_charpos,
1843 it->region_end_charpos,
1844 &next_stop,
1845 (IT_CHARPOS (*it)
1846 + TEXT_PROP_DISTANCE_LIMIT),
1847 0);
1848
1849 /* Is this a start of a run of characters with box face?
1850 Caveat: this can be called for a freshly initialized
1851 iterator; face_id is -1 is this case. We know that the new
1852 face will not change until limit, i.e. if the new face has a
1853 box, all characters up to limit will have one. But, as
1854 usual, we don't know whether limit is really the end. */
1855 if (new_face_id != it->face_id)
1856 {
1857 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
1858
1859 /* If new face has a box but old face has not, this is
1860 the start of a run of characters with box, i.e. it has
1861 a shadow on the left side. The value of face_id of the
1862 iterator will be -1 if this is the initial call that gets
1863 the face. In this case, we have to look in front of IT's
1864 position and see whether there is a face != new_face_id. */
1865 it->start_of_box_run_p
1866 = (new_face->box != FACE_NO_BOX
1867 && (it->face_id >= 0
1868 || IT_CHARPOS (*it) == BEG
1869 || new_face_id != face_before_it_pos (it)));
1870 it->face_box_p = new_face->box != FACE_NO_BOX;
1871 }
1872 }
1873 else
1874 {
1875 new_face_id
1876 = face_at_string_position (it->w,
1877 it->string,
1878 IT_STRING_CHARPOS (*it),
1879 (it->current.overlay_string_index >= 0
1880 ? IT_CHARPOS (*it)
1881 : 0),
1882 it->region_beg_charpos,
1883 it->region_end_charpos,
1884 &next_stop,
1885 it->base_face_id);
1886
1887 #if 0 /* This shouldn't be neccessary. Let's check it. */
1888 /* If IT is used to display a mode line we would really like to
1889 use the mode line face instead of the frame's default face. */
1890 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
1891 && new_face_id == DEFAULT_FACE_ID)
1892 new_face_id = MODE_LINE_FACE_ID;
1893 #endif
1894
1895 /* Is this a start of a run of characters with box? Caveat:
1896 this can be called for a freshly allocated iterator; face_id
1897 is -1 is this case. We know that the new face will not
1898 change until the next check pos, i.e. if the new face has a
1899 box, all characters up to that position will have a
1900 box. But, as usual, we don't know whether that position
1901 is really the end. */
1902 if (new_face_id != it->face_id)
1903 {
1904 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
1905 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
1906
1907 /* If new face has a box but old face hasn't, this is the
1908 start of a run of characters with box, i.e. it has a
1909 shadow on the left side. */
1910 it->start_of_box_run_p
1911 = new_face->box && (old_face == NULL || !old_face->box);
1912 it->face_box_p = new_face->box != FACE_NO_BOX;
1913 }
1914 }
1915
1916 it->face_id = new_face_id;
1917 return HANDLED_NORMALLY;
1918 }
1919
1920
1921 /* Compute the face one character before or after the current position
1922 of IT. BEFORE_P non-zero means get the face in front of IT's
1923 position. Value is the id of the face. */
1924
1925 static int
1926 face_before_or_after_it_pos (it, before_p)
1927 struct it *it;
1928 int before_p;
1929 {
1930 int face_id, limit;
1931 int next_check_charpos;
1932 struct text_pos pos;
1933
1934 xassert (it->s == NULL);
1935
1936 if (STRINGP (it->string))
1937 {
1938 /* No face change past the end of the string (for the case
1939 we are padding with spaces). No face change before the
1940 string start. */
1941 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size
1942 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
1943 return it->face_id;
1944
1945 /* Set pos to the position before or after IT's current position. */
1946 if (before_p)
1947 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
1948 else
1949 /* For composition, we must check the character after the
1950 composition. */
1951 pos = (it->what == IT_COMPOSITION
1952 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
1953 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
1954
1955 /* Get the face for ASCII, or unibyte. */
1956 face_id
1957 = face_at_string_position (it->w,
1958 it->string,
1959 CHARPOS (pos),
1960 (it->current.overlay_string_index >= 0
1961 ? IT_CHARPOS (*it)
1962 : 0),
1963 it->region_beg_charpos,
1964 it->region_end_charpos,
1965 &next_check_charpos,
1966 it->base_face_id);
1967
1968 /* Correct the face for charsets different from ASCII. Do it
1969 for the multibyte case only. The face returned above is
1970 suitable for unibyte text if IT->string is unibyte. */
1971 if (STRING_MULTIBYTE (it->string))
1972 {
1973 unsigned char *p = XSTRING (it->string)->data + BYTEPOS (pos);
1974 int rest = STRING_BYTES (XSTRING (it->string)) - BYTEPOS (pos);
1975 int c, len;
1976 struct face *face = FACE_FROM_ID (it->f, face_id);
1977
1978 c = string_char_and_length (p, rest, &len);
1979 face_id = FACE_FOR_CHAR (it->f, face, c);
1980 }
1981 }
1982 else
1983 {
1984 if ((IT_CHARPOS (*it) >= ZV && !before_p)
1985 || (IT_CHARPOS (*it) <= BEGV && before_p))
1986 return it->face_id;
1987
1988 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
1989 pos = it->current.pos;
1990
1991 if (before_p)
1992 DEC_TEXT_POS (pos, it->multibyte_p);
1993 else
1994 {
1995 if (it->what == IT_COMPOSITION)
1996 /* For composition, we must check the position after the
1997 composition. */
1998 pos.charpos += it->cmp_len, pos.bytepos += it->len;
1999 else
2000 INC_TEXT_POS (pos, it->multibyte_p);
2001 }
2002 /* Determine face for CHARSET_ASCII, or unibyte. */
2003 face_id = face_at_buffer_position (it->w,
2004 CHARPOS (pos),
2005 it->region_beg_charpos,
2006 it->region_end_charpos,
2007 &next_check_charpos,
2008 limit, 0);
2009
2010 /* Correct the face for charsets different from ASCII. Do it
2011 for the multibyte case only. The face returned above is
2012 suitable for unibyte text if current_buffer is unibyte. */
2013 if (it->multibyte_p)
2014 {
2015 int c = FETCH_MULTIBYTE_CHAR (CHARPOS (pos));
2016 struct face *face = FACE_FROM_ID (it->f, face_id);
2017 face_id = FACE_FOR_CHAR (it->f, face, c);
2018 }
2019 }
2020
2021 return face_id;
2022 }
2023
2024
2025 \f
2026 /***********************************************************************
2027 Invisible text
2028 ***********************************************************************/
2029
2030 /* Set up iterator IT from invisible properties at its current
2031 position. Called from handle_stop. */
2032
2033 static enum prop_handled
2034 handle_invisible_prop (it)
2035 struct it *it;
2036 {
2037 enum prop_handled handled = HANDLED_NORMALLY;
2038
2039 if (STRINGP (it->string))
2040 {
2041 extern Lisp_Object Qinvisible;
2042 Lisp_Object prop, end_charpos, limit, charpos;
2043
2044 /* Get the value of the invisible text property at the
2045 current position. Value will be nil if there is no such
2046 property. */
2047 XSETFASTINT (charpos, IT_STRING_CHARPOS (*it));
2048 prop = Fget_text_property (charpos, Qinvisible, it->string);
2049
2050 if (!NILP (prop)
2051 && IT_STRING_CHARPOS (*it) < it->end_charpos)
2052 {
2053 handled = HANDLED_RECOMPUTE_PROPS;
2054
2055 /* Get the position at which the next change of the
2056 invisible text property can be found in IT->string.
2057 Value will be nil if the property value is the same for
2058 all the rest of IT->string. */
2059 XSETINT (limit, XSTRING (it->string)->size);
2060 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2061 it->string, limit);
2062
2063 /* Text at current position is invisible. The next
2064 change in the property is at position end_charpos.
2065 Move IT's current position to that position. */
2066 if (INTEGERP (end_charpos)
2067 && XFASTINT (end_charpos) < XFASTINT (limit))
2068 {
2069 struct text_pos old;
2070 old = it->current.string_pos;
2071 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
2072 compute_string_pos (&it->current.string_pos, old, it->string);
2073 }
2074 else
2075 {
2076 /* The rest of the string is invisible. If this is an
2077 overlay string, proceed with the next overlay string
2078 or whatever comes and return a character from there. */
2079 if (it->current.overlay_string_index >= 0)
2080 {
2081 next_overlay_string (it);
2082 /* Don't check for overlay strings when we just
2083 finished processing them. */
2084 handled = HANDLED_OVERLAY_STRING_CONSUMED;
2085 }
2086 else
2087 {
2088 struct Lisp_String *s = XSTRING (it->string);
2089 IT_STRING_CHARPOS (*it) = s->size;
2090 IT_STRING_BYTEPOS (*it) = STRING_BYTES (s);
2091 }
2092 }
2093 }
2094 }
2095 else
2096 {
2097 int visible_p, newpos, next_stop;
2098 Lisp_Object pos, prop;
2099
2100 /* First of all, is there invisible text at this position? */
2101 XSETFASTINT (pos, IT_CHARPOS (*it));
2102 prop = Fget_char_property (pos, Qinvisible, it->window);
2103
2104 /* If we are on invisible text, skip over it. */
2105 if (TEXT_PROP_MEANS_INVISIBLE (prop)
2106 && IT_CHARPOS (*it) < it->end_charpos)
2107 {
2108 /* Record whether we have to display an ellipsis for the
2109 invisible text. */
2110 int display_ellipsis_p
2111 = TEXT_PROP_MEANS_INVISIBLE_WITH_ELLIPSIS (prop);
2112
2113 handled = HANDLED_RECOMPUTE_PROPS;
2114
2115 /* Loop skipping over invisible text. The loop is left at
2116 ZV or with IT on the first char being visible again. */
2117 do
2118 {
2119 /* Try to skip some invisible text. Return value is the
2120 position reached which can be equal to IT's position
2121 if there is nothing invisible here. This skips both
2122 over invisible text properties and overlays with
2123 invisible property. */
2124 newpos = skip_invisible (IT_CHARPOS (*it),
2125 &next_stop, ZV, it->window);
2126
2127 /* If we skipped nothing at all we weren't at invisible
2128 text in the first place. If everything to the end of
2129 the buffer was skipped, end the loop. */
2130 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
2131 visible_p = 1;
2132 else
2133 {
2134 /* We skipped some characters but not necessarily
2135 all there are. Check if we ended up on visible
2136 text. Fget_char_property returns the property of
2137 the char before the given position, i.e. if we
2138 get visible_p = 1, this means that the char at
2139 newpos is visible. */
2140 XSETFASTINT (pos, newpos);
2141 prop = Fget_char_property (pos, Qinvisible, it->window);
2142 visible_p = !TEXT_PROP_MEANS_INVISIBLE (prop);
2143 }
2144
2145 /* If we ended up on invisible text, proceed to
2146 skip starting with next_stop. */
2147 if (!visible_p)
2148 IT_CHARPOS (*it) = next_stop;
2149 }
2150 while (!visible_p);
2151
2152 /* The position newpos is now either ZV or on visible text. */
2153 IT_CHARPOS (*it) = newpos;
2154 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2155
2156 /* Maybe return `...' next for the end of the invisible text. */
2157 if (display_ellipsis_p)
2158 {
2159 if (it->dp
2160 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
2161 {
2162 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
2163 it->dpvec = v->contents;
2164 it->dpend = v->contents + v->size;
2165 }
2166 else
2167 {
2168 /* Default `...'. */
2169 it->dpvec = default_invis_vector;
2170 it->dpend = default_invis_vector + 3;
2171 }
2172
2173 /* The ellipsis display does not replace the display of
2174 the character at the new position. Indicate this by
2175 setting IT->dpvec_char_len to zero. */
2176 it->dpvec_char_len = 0;
2177
2178 it->current.dpvec_index = 0;
2179 it->method = next_element_from_display_vector;
2180 }
2181 }
2182 }
2183
2184 return handled;
2185 }
2186
2187
2188 \f
2189 /***********************************************************************
2190 'display' property
2191 ***********************************************************************/
2192
2193 /* Set up iterator IT from `display' property at its current position.
2194 Called from handle_stop. */
2195
2196 static enum prop_handled
2197 handle_display_prop (it)
2198 struct it *it;
2199 {
2200 Lisp_Object prop, object;
2201 struct text_pos *position;
2202 int space_or_image_found_p;
2203
2204 if (STRINGP (it->string))
2205 {
2206 object = it->string;
2207 position = &it->current.string_pos;
2208 }
2209 else
2210 {
2211 object = Qnil;
2212 position = &it->current.pos;
2213 }
2214
2215 /* Reset those iterator values set from display property values. */
2216 it->font_height = Qnil;
2217 it->space_width = Qnil;
2218 it->voffset = 0;
2219
2220 /* We don't support recursive `display' properties, i.e. string
2221 values that have a string `display' property, that have a string
2222 `display' property etc. */
2223 if (!it->string_from_display_prop_p)
2224 it->area = TEXT_AREA;
2225
2226 prop = Fget_char_property (make_number (position->charpos),
2227 Qdisplay, object);
2228 if (NILP (prop))
2229 return HANDLED_NORMALLY;
2230
2231 space_or_image_found_p = 0;
2232 if (CONSP (prop)
2233 && CONSP (XCAR (prop))
2234 && !EQ (Qmargin, XCAR (XCAR (prop))))
2235 {
2236 /* A list of sub-properties. */
2237 while (CONSP (prop))
2238 {
2239 if (handle_single_display_prop (it, XCAR (prop), object, position))
2240 space_or_image_found_p = 1;
2241 prop = XCDR (prop);
2242 }
2243 }
2244 else if (VECTORP (prop))
2245 {
2246 int i;
2247 for (i = 0; i < XVECTOR (prop)->size; ++i)
2248 if (handle_single_display_prop (it, XVECTOR (prop)->contents[i],
2249 object, position))
2250 space_or_image_found_p = 1;
2251 }
2252 else
2253 {
2254 if (handle_single_display_prop (it, prop, object, position))
2255 space_or_image_found_p = 1;
2256 }
2257
2258 return space_or_image_found_p ? HANDLED_RETURN : HANDLED_NORMALLY;
2259 }
2260
2261
2262 /* Value is the position of the end of the `display' property starting
2263 at START_POS in OBJECT. */
2264
2265 static struct text_pos
2266 display_prop_end (it, object, start_pos)
2267 struct it *it;
2268 Lisp_Object object;
2269 struct text_pos start_pos;
2270 {
2271 Lisp_Object end;
2272 struct text_pos end_pos;
2273
2274 end = next_single_char_property_change (make_number (CHARPOS (start_pos)),
2275 Qdisplay, object, Qnil);
2276 CHARPOS (end_pos) = XFASTINT (end);
2277 if (STRINGP (object))
2278 compute_string_pos (&end_pos, start_pos, it->string);
2279 else
2280 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
2281
2282 return end_pos;
2283 }
2284
2285
2286 /* Set up IT from a single `display' sub-property value PROP. OBJECT
2287 is the object in which the `display' property was found. *POSITION
2288 is the position at which it was found.
2289
2290 If PROP is a `space' or `image' sub-property, set *POSITION to the
2291 end position of the `display' property.
2292
2293 Value is non-zero if a `space' or `image' property value was found. */
2294
2295 static int
2296 handle_single_display_prop (it, prop, object, position)
2297 struct it *it;
2298 Lisp_Object prop;
2299 Lisp_Object object;
2300 struct text_pos *position;
2301 {
2302 Lisp_Object value;
2303 int space_or_image_found_p = 0;
2304
2305 Lisp_Object form;
2306
2307 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
2308 evaluated. If the result is nil, VALUE is ignored. */
2309 form = Qt;
2310 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2311 {
2312 prop = XCDR (prop);
2313 if (!CONSP (prop))
2314 return 0;
2315 form = XCAR (prop);
2316 prop = XCDR (prop);
2317 }
2318
2319 if (!NILP (form) && !EQ (form, Qt))
2320 {
2321 struct gcpro gcpro1;
2322 struct text_pos end_pos, pt;
2323
2324 GCPRO1 (form);
2325 end_pos = display_prop_end (it, object, *position);
2326
2327 /* Temporarily set point to the end position, and then evaluate
2328 the form. This makes `(eolp)' work as FORM. */
2329 if (BUFFERP (object))
2330 {
2331 CHARPOS (pt) = PT;
2332 BYTEPOS (pt) = PT_BYTE;
2333 TEMP_SET_PT_BOTH (CHARPOS (end_pos), BYTEPOS (end_pos));
2334 }
2335
2336 form = eval_form (form);
2337
2338 if (BUFFERP (object))
2339 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
2340 UNGCPRO;
2341 }
2342
2343 if (NILP (form))
2344 return 0;
2345
2346 if (CONSP (prop)
2347 && EQ (XCAR (prop), Qheight)
2348 && CONSP (XCDR (prop)))
2349 {
2350 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2351 return 0;
2352
2353 /* `(height HEIGHT)'. */
2354 it->font_height = XCAR (XCDR (prop));
2355 if (!NILP (it->font_height))
2356 {
2357 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2358 int new_height = -1;
2359
2360 if (CONSP (it->font_height)
2361 && (EQ (XCAR (it->font_height), Qplus)
2362 || EQ (XCAR (it->font_height), Qminus))
2363 && CONSP (XCDR (it->font_height))
2364 && INTEGERP (XCAR (XCDR (it->font_height))))
2365 {
2366 /* `(+ N)' or `(- N)' where N is an integer. */
2367 int steps = XINT (XCAR (XCDR (it->font_height)));
2368 if (EQ (XCAR (it->font_height), Qplus))
2369 steps = - steps;
2370 it->face_id = smaller_face (it->f, it->face_id, steps);
2371 }
2372 else if (SYMBOLP (it->font_height))
2373 {
2374 /* Call function with current height as argument.
2375 Value is the new height. */
2376 Lisp_Object form, height;
2377 struct gcpro gcpro1;
2378
2379 height = face->lface[LFACE_HEIGHT_INDEX];
2380 form = Fcons (it->font_height, Fcons (height, Qnil));
2381 GCPRO1 (form);
2382 height = eval_form (form);
2383 if (NUMBERP (height))
2384 new_height = XFLOATINT (height);
2385 UNGCPRO;
2386 }
2387 else if (NUMBERP (it->font_height))
2388 {
2389 /* Value is a multiple of the canonical char height. */
2390 struct face *face;
2391
2392 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
2393 new_height = (XFLOATINT (it->font_height)
2394 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
2395 }
2396 else
2397 {
2398 /* Evaluate IT->font_height with `height' bound to the
2399 current specified height to get the new height. */
2400 Lisp_Object value;
2401 int count = specpdl_ptr - specpdl;
2402
2403 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
2404 value = eval_form (it->font_height);
2405 unbind_to (count, Qnil);
2406
2407 if (NUMBERP (value))
2408 new_height = XFLOATINT (value);
2409 }
2410
2411 if (new_height > 0)
2412 it->face_id = face_with_height (it->f, it->face_id, new_height);
2413 }
2414 }
2415 else if (CONSP (prop)
2416 && EQ (XCAR (prop), Qspace_width)
2417 && CONSP (XCDR (prop)))
2418 {
2419 /* `(space_width WIDTH)'. */
2420 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2421 return 0;
2422
2423 value = XCAR (XCDR (prop));
2424 if (NUMBERP (value) && XFLOATINT (value) > 0)
2425 it->space_width = value;
2426 }
2427 else if (CONSP (prop)
2428 && EQ (XCAR (prop), Qraise)
2429 && CONSP (XCDR (prop)))
2430 {
2431 /* `(raise FACTOR)'. */
2432 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2433 return 0;
2434
2435 #ifdef HAVE_WINDOW_SYSTEM
2436 value = XCAR (XCDR (prop));
2437 if (NUMBERP (value))
2438 {
2439 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2440 it->voffset = - (XFLOATINT (value)
2441 * (FONT_HEIGHT (face->font)));
2442 }
2443 #endif /* HAVE_WINDOW_SYSTEM */
2444 }
2445 else if (!it->string_from_display_prop_p)
2446 {
2447 /* `((margin left-margin) VALUE)' or `((margin right-margin)
2448 VALUE) or `((margin nil) VALUE)' or VALUE. */
2449 Lisp_Object location, value;
2450 struct text_pos start_pos;
2451 int valid_p;
2452
2453 /* Characters having this form of property are not displayed, so
2454 we have to find the end of the property. */
2455 start_pos = *position;
2456 *position = display_prop_end (it, object, start_pos);
2457 value = Qnil;
2458
2459 /* Let's stop at the new position and assume that all
2460 text properties change there. */
2461 it->stop_charpos = position->charpos;
2462
2463 location = Qunbound;
2464 if (CONSP (prop) && CONSP (XCAR (prop)))
2465 {
2466 Lisp_Object tem;
2467
2468 value = XCDR (prop);
2469 if (CONSP (value))
2470 value = XCAR (value);
2471
2472 tem = XCAR (prop);
2473 if (EQ (XCAR (tem), Qmargin)
2474 && (tem = XCDR (tem),
2475 tem = CONSP (tem) ? XCAR (tem) : Qnil,
2476 (NILP (tem)
2477 || EQ (tem, Qleft_margin)
2478 || EQ (tem, Qright_margin))))
2479 location = tem;
2480 }
2481
2482 if (EQ (location, Qunbound))
2483 {
2484 location = Qnil;
2485 value = prop;
2486 }
2487
2488 #ifdef HAVE_WINDOW_SYSTEM
2489 if (FRAME_TERMCAP_P (it->f))
2490 valid_p = STRINGP (value);
2491 else
2492 valid_p = (STRINGP (value)
2493 || (CONSP (value) && EQ (XCAR (value), Qspace))
2494 || valid_image_p (value));
2495 #else /* not HAVE_WINDOW_SYSTEM */
2496 valid_p = STRINGP (value);
2497 #endif /* not HAVE_WINDOW_SYSTEM */
2498
2499 if ((EQ (location, Qleft_margin)
2500 || EQ (location, Qright_margin)
2501 || NILP (location))
2502 && valid_p)
2503 {
2504 space_or_image_found_p = 1;
2505
2506 /* Save current settings of IT so that we can restore them
2507 when we are finished with the glyph property value. */
2508 push_it (it);
2509
2510 if (NILP (location))
2511 it->area = TEXT_AREA;
2512 else if (EQ (location, Qleft_margin))
2513 it->area = LEFT_MARGIN_AREA;
2514 else
2515 it->area = RIGHT_MARGIN_AREA;
2516
2517 if (STRINGP (value))
2518 {
2519 it->string = value;
2520 it->multibyte_p = STRING_MULTIBYTE (it->string);
2521 it->current.overlay_string_index = -1;
2522 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
2523 it->end_charpos = it->string_nchars
2524 = XSTRING (it->string)->size;
2525 it->method = next_element_from_string;
2526 it->stop_charpos = 0;
2527 it->string_from_display_prop_p = 1;
2528 }
2529 else if (CONSP (value) && EQ (XCAR (value), Qspace))
2530 {
2531 it->method = next_element_from_stretch;
2532 it->object = value;
2533 it->current.pos = it->position = start_pos;
2534 }
2535 #ifdef HAVE_WINDOW_SYSTEM
2536 else
2537 {
2538 it->what = IT_IMAGE;
2539 it->image_id = lookup_image (it->f, value);
2540 it->position = start_pos;
2541 it->object = NILP (object) ? it->w->buffer : object;
2542 it->method = next_element_from_image;
2543
2544 /* Say that we haven't consumed the characters with
2545 `display' property yet. The call to pop_it in
2546 set_iterator_to_next will clean this up. */
2547 *position = start_pos;
2548 }
2549 #endif /* HAVE_WINDOW_SYSTEM */
2550 }
2551 else
2552 /* Invalid property or property not supported. Restore
2553 the position to what it was before. */
2554 *position = start_pos;
2555 }
2556
2557 return space_or_image_found_p;
2558 }
2559
2560
2561 \f
2562 /***********************************************************************
2563 `composition' property
2564 ***********************************************************************/
2565
2566 /* Set up iterator IT from `composition' property at its current
2567 position. Called from handle_stop. */
2568
2569 static enum prop_handled
2570 handle_composition_prop (it)
2571 struct it *it;
2572 {
2573 Lisp_Object prop, string;
2574 int pos, pos_byte, end;
2575 enum prop_handled handled = HANDLED_NORMALLY;
2576
2577 if (STRINGP (it->string))
2578 {
2579 pos = IT_STRING_CHARPOS (*it);
2580 pos_byte = IT_STRING_BYTEPOS (*it);
2581 string = it->string;
2582 }
2583 else
2584 {
2585 pos = IT_CHARPOS (*it);
2586 pos_byte = IT_BYTEPOS (*it);
2587 string = Qnil;
2588 }
2589
2590 /* If there's a valid composition and point is not inside of the
2591 composition (in the case that the composition is from the current
2592 buffer), draw a glyph composed from the composition components. */
2593 if (find_composition (pos, -1, &pos, &end, &prop, string)
2594 && COMPOSITION_VALID_P (pos, end, prop)
2595 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
2596 {
2597 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
2598
2599 if (id >= 0)
2600 {
2601 it->method = next_element_from_composition;
2602 it->cmp_id = id;
2603 it->cmp_len = COMPOSITION_LENGTH (prop);
2604 /* For a terminal, draw only the first character of the
2605 components. */
2606 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
2607 it->len = (STRINGP (it->string)
2608 ? string_char_to_byte (it->string, end)
2609 : CHAR_TO_BYTE (end)) - pos_byte;
2610 it->stop_charpos = end;
2611 handled = HANDLED_RETURN;
2612 }
2613 }
2614
2615 return handled;
2616 }
2617
2618
2619 \f
2620 /***********************************************************************
2621 Overlay strings
2622 ***********************************************************************/
2623
2624 /* The following structure is used to record overlay strings for
2625 later sorting in load_overlay_strings. */
2626
2627 struct overlay_entry
2628 {
2629 Lisp_Object string;
2630 int priority;
2631 int after_string_p;
2632 };
2633
2634
2635 /* Set up iterator IT from overlay strings at its current position.
2636 Called from handle_stop. */
2637
2638 static enum prop_handled
2639 handle_overlay_change (it)
2640 struct it *it;
2641 {
2642 /* Overlays are handled in current_buffer only. */
2643 if (STRINGP (it->string))
2644 return HANDLED_NORMALLY;
2645 else
2646 return (get_overlay_strings (it)
2647 ? HANDLED_RECOMPUTE_PROPS
2648 : HANDLED_NORMALLY);
2649 }
2650
2651
2652 /* Set up the next overlay string for delivery by IT, if there is an
2653 overlay string to deliver. Called by set_iterator_to_next when the
2654 end of the current overlay string is reached. If there are more
2655 overlay strings to display, IT->string and
2656 IT->current.overlay_string_index are set appropriately here.
2657 Otherwise IT->string is set to nil. */
2658
2659 static void
2660 next_overlay_string (it)
2661 struct it *it;
2662 {
2663 ++it->current.overlay_string_index;
2664 if (it->current.overlay_string_index == it->n_overlay_strings)
2665 {
2666 /* No more overlay strings. Restore IT's settings to what
2667 they were before overlay strings were processed, and
2668 continue to deliver from current_buffer. */
2669 pop_it (it);
2670 xassert (it->stop_charpos >= BEGV
2671 && it->stop_charpos <= it->end_charpos);
2672 it->string = Qnil;
2673 it->current.overlay_string_index = -1;
2674 SET_TEXT_POS (it->current.string_pos, -1, -1);
2675 it->n_overlay_strings = 0;
2676 it->method = next_element_from_buffer;
2677 }
2678 else
2679 {
2680 /* There are more overlay strings to process. If
2681 IT->current.overlay_string_index has advanced to a position
2682 where we must load IT->overlay_strings with more strings, do
2683 it. */
2684 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
2685
2686 if (it->current.overlay_string_index && i == 0)
2687 load_overlay_strings (it);
2688
2689 /* Initialize IT to deliver display elements from the overlay
2690 string. */
2691 it->string = it->overlay_strings[i];
2692 it->multibyte_p = STRING_MULTIBYTE (it->string);
2693 SET_TEXT_POS (it->current.string_pos, 0, 0);
2694 it->method = next_element_from_string;
2695 it->stop_charpos = 0;
2696 }
2697
2698 CHECK_IT (it);
2699 }
2700
2701
2702 /* Compare two overlay_entry structures E1 and E2. Used as a
2703 comparison function for qsort in load_overlay_strings. Overlay
2704 strings for the same position are sorted so that
2705
2706 1. All after-strings come in front of before-strings.
2707
2708 2. Within after-strings, strings are sorted so that overlay strings
2709 from overlays with higher priorities come first.
2710
2711 2. Within before-strings, strings are sorted so that overlay
2712 strings from overlays with higher priorities come last.
2713
2714 Value is analogous to strcmp. */
2715
2716
2717 static int
2718 compare_overlay_entries (e1, e2)
2719 void *e1, *e2;
2720 {
2721 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
2722 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
2723 int result;
2724
2725 if (entry1->after_string_p != entry2->after_string_p)
2726 /* Let after-strings appear in front of before-strings. */
2727 result = entry1->after_string_p ? -1 : 1;
2728 else if (entry1->after_string_p)
2729 /* After-strings sorted in order of decreasing priority. */
2730 result = entry2->priority - entry1->priority;
2731 else
2732 /* Before-strings sorted in order of increasing priority. */
2733 result = entry1->priority - entry2->priority;
2734
2735 return result;
2736 }
2737
2738
2739 /* Load the vector IT->overlay_strings with overlay strings from IT's
2740 current buffer position. Set IT->n_overlays to the total number of
2741 overlay strings found.
2742
2743 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
2744 a time. On entry into load_overlay_strings,
2745 IT->current.overlay_string_index gives the number of overlay
2746 strings that have already been loaded by previous calls to this
2747 function.
2748
2749 Overlay strings are sorted so that after-string strings come in
2750 front of before-string strings. Within before and after-strings,
2751 strings are sorted by overlay priority. See also function
2752 compare_overlay_entries. */
2753
2754 static void
2755 load_overlay_strings (it)
2756 struct it *it;
2757 {
2758 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
2759 Lisp_Object ov, overlay, window, str;
2760 int start, end;
2761 int size = 20;
2762 int n = 0, i, j;
2763 struct overlay_entry *entries
2764 = (struct overlay_entry *) alloca (size * sizeof *entries);
2765
2766 /* Append the overlay string STRING of overlay OVERLAY to vector
2767 `entries' which has size `size' and currently contains `n'
2768 elements. AFTER_P non-zero means STRING is an after-string of
2769 OVERLAY. */
2770 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
2771 do \
2772 { \
2773 Lisp_Object priority; \
2774 \
2775 if (n == size) \
2776 { \
2777 int new_size = 2 * size; \
2778 struct overlay_entry *old = entries; \
2779 entries = \
2780 (struct overlay_entry *) alloca (new_size \
2781 * sizeof *entries); \
2782 bcopy (old, entries, size * sizeof *entries); \
2783 size = new_size; \
2784 } \
2785 \
2786 entries[n].string = (STRING); \
2787 priority = Foverlay_get ((OVERLAY), Qpriority); \
2788 entries[n].priority \
2789 = INTEGERP (priority) ? XFASTINT (priority) : 0; \
2790 entries[n].after_string_p = (AFTER_P); \
2791 ++n; \
2792 } \
2793 while (0)
2794
2795 /* Process overlay before the overlay center. */
2796 for (ov = current_buffer->overlays_before;
2797 CONSP (ov);
2798 ov = XCDR (ov))
2799 {
2800 overlay = XCAR (ov);
2801 xassert (OVERLAYP (overlay));
2802 start = OVERLAY_POSITION (OVERLAY_START (overlay));
2803 end = OVERLAY_POSITION (OVERLAY_END (overlay));
2804
2805 if (end < IT_CHARPOS (*it))
2806 break;
2807
2808 /* Skip this overlay if it doesn't start or end at IT's current
2809 position. */
2810 if (end != IT_CHARPOS (*it) && start != IT_CHARPOS (*it))
2811 continue;
2812
2813 /* Skip this overlay if it doesn't apply to IT->w. */
2814 window = Foverlay_get (overlay, Qwindow);
2815 if (WINDOWP (window) && XWINDOW (window) != it->w)
2816 continue;
2817
2818 /* If overlay has a non-empty before-string, record it. */
2819 if (start == IT_CHARPOS (*it)
2820 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
2821 && XSTRING (str)->size)
2822 RECORD_OVERLAY_STRING (overlay, str, 0);
2823
2824 /* If overlay has a non-empty after-string, record it. */
2825 if (end == IT_CHARPOS (*it)
2826 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
2827 && XSTRING (str)->size)
2828 RECORD_OVERLAY_STRING (overlay, str, 1);
2829 }
2830
2831 /* Process overlays after the overlay center. */
2832 for (ov = current_buffer->overlays_after;
2833 CONSP (ov);
2834 ov = XCDR (ov))
2835 {
2836 overlay = XCAR (ov);
2837 xassert (OVERLAYP (overlay));
2838 start = OVERLAY_POSITION (OVERLAY_START (overlay));
2839 end = OVERLAY_POSITION (OVERLAY_END (overlay));
2840
2841 if (start > IT_CHARPOS (*it))
2842 break;
2843
2844 /* Skip this overlay if it doesn't start or end at IT's current
2845 position. */
2846 if (end != IT_CHARPOS (*it) && start != IT_CHARPOS (*it))
2847 continue;
2848
2849 /* Skip this overlay if it doesn't apply to IT->w. */
2850 window = Foverlay_get (overlay, Qwindow);
2851 if (WINDOWP (window) && XWINDOW (window) != it->w)
2852 continue;
2853
2854 /* If overlay has a non-empty before-string, record it. */
2855 if (start == IT_CHARPOS (*it)
2856 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
2857 && XSTRING (str)->size)
2858 RECORD_OVERLAY_STRING (overlay, str, 0);
2859
2860 /* If overlay has a non-empty after-string, record it. */
2861 if (end == IT_CHARPOS (*it)
2862 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
2863 && XSTRING (str)->size)
2864 RECORD_OVERLAY_STRING (overlay, str, 1);
2865 }
2866
2867 #undef RECORD_OVERLAY_STRING
2868
2869 /* Sort entries. */
2870 qsort (entries, n, sizeof *entries, compare_overlay_entries);
2871
2872 /* Record the total number of strings to process. */
2873 it->n_overlay_strings = n;
2874
2875 /* IT->current.overlay_string_index is the number of overlay strings
2876 that have already been consumed by IT. Copy some of the
2877 remaining overlay strings to IT->overlay_strings. */
2878 i = 0;
2879 j = it->current.overlay_string_index;
2880 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
2881 it->overlay_strings[i++] = entries[j++].string;
2882
2883 CHECK_IT (it);
2884 }
2885
2886
2887 /* Get the first chunk of overlay strings at IT's current buffer
2888 position. Value is non-zero if at least one overlay string was
2889 found. */
2890
2891 static int
2892 get_overlay_strings (it)
2893 struct it *it;
2894 {
2895 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
2896 process. This fills IT->overlay_strings with strings, and sets
2897 IT->n_overlay_strings to the total number of strings to process.
2898 IT->pos.overlay_string_index has to be set temporarily to zero
2899 because load_overlay_strings needs this; it must be set to -1
2900 when no overlay strings are found because a zero value would
2901 indicate a position in the first overlay string. */
2902 it->current.overlay_string_index = 0;
2903 load_overlay_strings (it);
2904
2905 /* If we found overlay strings, set up IT to deliver display
2906 elements from the first one. Otherwise set up IT to deliver
2907 from current_buffer. */
2908 if (it->n_overlay_strings)
2909 {
2910 /* Make sure we know settings in current_buffer, so that we can
2911 restore meaningful values when we're done with the overlay
2912 strings. */
2913 compute_stop_pos (it);
2914 xassert (it->face_id >= 0);
2915
2916 /* Save IT's settings. They are restored after all overlay
2917 strings have been processed. */
2918 xassert (it->sp == 0);
2919 push_it (it);
2920
2921 /* Set up IT to deliver display elements from the first overlay
2922 string. */
2923 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
2924 it->stop_charpos = 0;
2925 it->string = it->overlay_strings[0];
2926 it->multibyte_p = STRING_MULTIBYTE (it->string);
2927 xassert (STRINGP (it->string));
2928 it->method = next_element_from_string;
2929 }
2930 else
2931 {
2932 it->string = Qnil;
2933 it->current.overlay_string_index = -1;
2934 it->method = next_element_from_buffer;
2935 }
2936
2937 CHECK_IT (it);
2938
2939 /* Value is non-zero if we found at least one overlay string. */
2940 return STRINGP (it->string);
2941 }
2942
2943
2944 \f
2945 /***********************************************************************
2946 Saving and restoring state
2947 ***********************************************************************/
2948
2949 /* Save current settings of IT on IT->stack. Called, for example,
2950 before setting up IT for an overlay string, to be able to restore
2951 IT's settings to what they were after the overlay string has been
2952 processed. */
2953
2954 static void
2955 push_it (it)
2956 struct it *it;
2957 {
2958 struct iterator_stack_entry *p;
2959
2960 xassert (it->sp < 2);
2961 p = it->stack + it->sp;
2962
2963 p->stop_charpos = it->stop_charpos;
2964 xassert (it->face_id >= 0);
2965 p->face_id = it->face_id;
2966 p->string = it->string;
2967 p->pos = it->current;
2968 p->end_charpos = it->end_charpos;
2969 p->string_nchars = it->string_nchars;
2970 p->area = it->area;
2971 p->multibyte_p = it->multibyte_p;
2972 p->space_width = it->space_width;
2973 p->font_height = it->font_height;
2974 p->voffset = it->voffset;
2975 p->string_from_display_prop_p = it->string_from_display_prop_p;
2976 ++it->sp;
2977 }
2978
2979
2980 /* Restore IT's settings from IT->stack. Called, for example, when no
2981 more overlay strings must be processed, and we return to delivering
2982 display elements from a buffer, or when the end of a string from a
2983 `display' property is reached and we return to delivering display
2984 elements from an overlay string, or from a buffer. */
2985
2986 static void
2987 pop_it (it)
2988 struct it *it;
2989 {
2990 struct iterator_stack_entry *p;
2991
2992 xassert (it->sp > 0);
2993 --it->sp;
2994 p = it->stack + it->sp;
2995 it->stop_charpos = p->stop_charpos;
2996 it->face_id = p->face_id;
2997 it->string = p->string;
2998 it->current = p->pos;
2999 it->end_charpos = p->end_charpos;
3000 it->string_nchars = p->string_nchars;
3001 it->area = p->area;
3002 it->multibyte_p = p->multibyte_p;
3003 it->space_width = p->space_width;
3004 it->font_height = p->font_height;
3005 it->voffset = p->voffset;
3006 it->string_from_display_prop_p = p->string_from_display_prop_p;
3007 }
3008
3009
3010 \f
3011 /***********************************************************************
3012 Moving over lines
3013 ***********************************************************************/
3014
3015 /* Set IT's current position to the previous line start. */
3016
3017 static void
3018 back_to_previous_line_start (it)
3019 struct it *it;
3020 {
3021 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
3022 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3023 }
3024
3025
3026 /* Set IT's current position to the next line start. */
3027
3028 static void
3029 forward_to_next_line_start (it)
3030 struct it *it;
3031 {
3032 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it), 1);
3033 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3034 }
3035
3036
3037 /* Set IT's current position to the previous visible line start. Skip
3038 invisible text that is so either due to text properties or due to
3039 selective display. Caution: this does not change IT->current_x and
3040 IT->hpos. */
3041
3042 static void
3043 back_to_previous_visible_line_start (it)
3044 struct it *it;
3045 {
3046 int visible_p = 0;
3047
3048 /* Go back one newline if not on BEGV already. */
3049 if (IT_CHARPOS (*it) > BEGV)
3050 back_to_previous_line_start (it);
3051
3052 /* Move over lines that are invisible because of selective display
3053 or text properties. */
3054 while (IT_CHARPOS (*it) > BEGV
3055 && !visible_p)
3056 {
3057 visible_p = 1;
3058
3059 /* If selective > 0, then lines indented more than that values
3060 are invisible. */
3061 if (it->selective > 0
3062 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3063 it->selective))
3064 visible_p = 0;
3065 else
3066 {
3067 Lisp_Object prop;
3068
3069 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
3070 Qinvisible, it->window);
3071 if (TEXT_PROP_MEANS_INVISIBLE (prop))
3072 visible_p = 0;
3073 }
3074
3075 /* Back one more newline if the current one is invisible. */
3076 if (!visible_p)
3077 back_to_previous_line_start (it);
3078 }
3079
3080 xassert (IT_CHARPOS (*it) >= BEGV);
3081 xassert (IT_CHARPOS (*it) == BEGV
3082 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3083 CHECK_IT (it);
3084 }
3085
3086
3087 /* Reseat iterator IT at the previous visible line start. Skip
3088 invisible text that is so either due to text properties or due to
3089 selective display. At the end, update IT's overlay information,
3090 face information etc. */
3091
3092 static void
3093 reseat_at_previous_visible_line_start (it)
3094 struct it *it;
3095 {
3096 back_to_previous_visible_line_start (it);
3097 reseat (it, it->current.pos, 1);
3098 CHECK_IT (it);
3099 }
3100
3101
3102 /* Reseat iterator IT on the next visible line start in the current
3103 buffer. ON_NEWLINE_P non-zero means position IT on the newline
3104 preceding the line start. Skip over invisible text that is so
3105 because of selective display. Compute faces, overlays etc at the
3106 new position. Note that this function does not skip over text that
3107 is invisible because of text properties. */
3108
3109 static void
3110 reseat_at_next_visible_line_start (it, on_newline_p)
3111 struct it *it;
3112 int on_newline_p;
3113 {
3114 /* Restore the buffer position when currently not delivering display
3115 elements from the current buffer. This is the case, for example,
3116 when called at the end of a truncated overlay string. */
3117 while (it->sp)
3118 pop_it (it);
3119 it->method = next_element_from_buffer;
3120
3121 /* Otherwise, scan_buffer would not work. */
3122 if (IT_CHARPOS (*it) < ZV)
3123 {
3124 /* If on a newline, advance past it. Otherwise, find the next
3125 newline which automatically gives us the position following
3126 the newline. */
3127 if (FETCH_BYTE (IT_BYTEPOS (*it)) == '\n')
3128 {
3129 ++IT_CHARPOS (*it);
3130 ++IT_BYTEPOS (*it);
3131 }
3132 else
3133 forward_to_next_line_start (it);
3134
3135 /* We must either have reached the end of the buffer or end up
3136 after a newline. */
3137 xassert (IT_CHARPOS (*it) == ZV
3138 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3139
3140 /* Skip over lines that are invisible because they are indented
3141 more than the value of IT->selective. */
3142 if (it->selective > 0)
3143 while (IT_CHARPOS (*it) < ZV
3144 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3145 it->selective))
3146 forward_to_next_line_start (it);
3147
3148 /* Position on the newline if we should. */
3149 if (on_newline_p
3150 && IT_CHARPOS (*it) > BEGV
3151 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n')
3152 {
3153 --IT_CHARPOS (*it);
3154 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3155 }
3156
3157 /* Set the iterator there. The 0 as the last parameter of
3158 reseat means don't force a text property lookup. The lookup
3159 is then only done if we've skipped past the iterator's
3160 check_charpos'es. This optimization is important because
3161 text property lookups tend to be expensive. */
3162 reseat (it, it->current.pos, 0);
3163 }
3164
3165 CHECK_IT (it);
3166 }
3167
3168
3169 \f
3170 /***********************************************************************
3171 Changing an iterator's position
3172 ***********************************************************************/
3173
3174 /* Change IT's current position to POS in current_buffer. If FORCE_P
3175 is non-zero, always check for text properties at the new position.
3176 Otherwise, text properties are only looked up if POS >=
3177 IT->check_charpos of a property. */
3178
3179 static void
3180 reseat (it, pos, force_p)
3181 struct it *it;
3182 struct text_pos pos;
3183 int force_p;
3184 {
3185 int original_pos = IT_CHARPOS (*it);
3186
3187 reseat_1 (it, pos, 0);
3188
3189 /* Determine where to check text properties. Avoid doing it
3190 where possible because text property lookup is very expensive. */
3191 if (force_p
3192 || CHARPOS (pos) > it->stop_charpos
3193 || CHARPOS (pos) < original_pos)
3194 handle_stop (it);
3195
3196 CHECK_IT (it);
3197 }
3198
3199
3200 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
3201 IT->stop_pos to POS, also. */
3202
3203 static void
3204 reseat_1 (it, pos, set_stop_p)
3205 struct it *it;
3206 struct text_pos pos;
3207 int set_stop_p;
3208 {
3209 /* Don't call this function when scanning a C string. */
3210 xassert (it->s == NULL);
3211
3212 /* POS must be a reasonable value. */
3213 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
3214
3215 it->current.pos = it->position = pos;
3216 XSETBUFFER (it->object, current_buffer);
3217 it->dpvec = NULL;
3218 it->current.dpvec_index = -1;
3219 it->current.overlay_string_index = -1;
3220 IT_STRING_CHARPOS (*it) = -1;
3221 IT_STRING_BYTEPOS (*it) = -1;
3222 it->string = Qnil;
3223 it->method = next_element_from_buffer;
3224 it->sp = 0;
3225
3226 if (set_stop_p)
3227 it->stop_charpos = CHARPOS (pos);
3228 }
3229
3230
3231 /* Set up IT for displaying a string, starting at CHARPOS in window W.
3232 If S is non-null, it is a C string to iterate over. Otherwise,
3233 STRING gives a Lisp string to iterate over.
3234
3235 If PRECISION > 0, don't return more then PRECISION number of
3236 characters from the string.
3237
3238 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
3239 characters have been returned. FIELD_WIDTH < 0 means an infinite
3240 field width.
3241
3242 MULTIBYTE = 0 means disable processing of multibyte characters,
3243 MULTIBYTE > 0 means enable it,
3244 MULTIBYTE < 0 means use IT->multibyte_p.
3245
3246 IT must be initialized via a prior call to init_iterator before
3247 calling this function. */
3248
3249 static void
3250 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
3251 struct it *it;
3252 unsigned char *s;
3253 Lisp_Object string;
3254 int charpos;
3255 int precision, field_width, multibyte;
3256 {
3257 /* No region in strings. */
3258 it->region_beg_charpos = it->region_end_charpos = -1;
3259
3260 /* No text property checks performed by default, but see below. */
3261 it->stop_charpos = -1;
3262
3263 /* Set iterator position and end position. */
3264 bzero (&it->current, sizeof it->current);
3265 it->current.overlay_string_index = -1;
3266 it->current.dpvec_index = -1;
3267 xassert (charpos >= 0);
3268
3269 /* Use the setting of MULTIBYTE if specified. */
3270 if (multibyte >= 0)
3271 it->multibyte_p = multibyte > 0;
3272
3273 if (s == NULL)
3274 {
3275 xassert (STRINGP (string));
3276 it->string = string;
3277 it->s = NULL;
3278 it->end_charpos = it->string_nchars = XSTRING (string)->size;
3279 it->method = next_element_from_string;
3280 it->current.string_pos = string_pos (charpos, string);
3281 }
3282 else
3283 {
3284 it->s = s;
3285 it->string = Qnil;
3286
3287 /* Note that we use IT->current.pos, not it->current.string_pos,
3288 for displaying C strings. */
3289 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
3290 if (it->multibyte_p)
3291 {
3292 it->current.pos = c_string_pos (charpos, s, 1);
3293 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
3294 }
3295 else
3296 {
3297 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
3298 it->end_charpos = it->string_nchars = strlen (s);
3299 }
3300
3301 it->method = next_element_from_c_string;
3302 }
3303
3304 /* PRECISION > 0 means don't return more than PRECISION characters
3305 from the string. */
3306 if (precision > 0 && it->end_charpos - charpos > precision)
3307 it->end_charpos = it->string_nchars = charpos + precision;
3308
3309 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
3310 characters have been returned. FIELD_WIDTH == 0 means don't pad,
3311 FIELD_WIDTH < 0 means infinite field width. This is useful for
3312 padding with `-' at the end of a mode line. */
3313 if (field_width < 0)
3314 field_width = INFINITY;
3315 if (field_width > it->end_charpos - charpos)
3316 it->end_charpos = charpos + field_width;
3317
3318 /* Use the standard display table for displaying strings. */
3319 if (DISP_TABLE_P (Vstandard_display_table))
3320 it->dp = XCHAR_TABLE (Vstandard_display_table);
3321
3322 it->stop_charpos = charpos;
3323 CHECK_IT (it);
3324 }
3325
3326
3327 \f
3328 /***********************************************************************
3329 Iteration
3330 ***********************************************************************/
3331
3332 /* Load IT's display element fields with information about the next
3333 display element from the current position of IT. Value is zero if
3334 end of buffer (or C string) is reached. */
3335
3336 int
3337 get_next_display_element (it)
3338 struct it *it;
3339 {
3340 /* Non-zero means that we found an display element. Zero means that
3341 we hit the end of what we iterate over. Performance note: the
3342 function pointer `method' used here turns out to be faster than
3343 using a sequence of if-statements. */
3344 int success_p = (*it->method) (it);
3345
3346 if (it->what == IT_CHARACTER)
3347 {
3348 /* Map via display table or translate control characters.
3349 IT->c, IT->len etc. have been set to the next character by
3350 the function call above. If we have a display table, and it
3351 contains an entry for IT->c, translate it. Don't do this if
3352 IT->c itself comes from a display table, otherwise we could
3353 end up in an infinite recursion. (An alternative could be to
3354 count the recursion depth of this function and signal an
3355 error when a certain maximum depth is reached.) Is it worth
3356 it? */
3357 if (success_p && it->dpvec == NULL)
3358 {
3359 Lisp_Object dv;
3360
3361 if (it->dp
3362 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
3363 VECTORP (dv)))
3364 {
3365 struct Lisp_Vector *v = XVECTOR (dv);
3366
3367 /* Return the first character from the display table
3368 entry, if not empty. If empty, don't display the
3369 current character. */
3370 if (v->size)
3371 {
3372 it->dpvec_char_len = it->len;
3373 it->dpvec = v->contents;
3374 it->dpend = v->contents + v->size;
3375 it->current.dpvec_index = 0;
3376 it->method = next_element_from_display_vector;
3377 }
3378
3379 success_p = get_next_display_element (it);
3380 }
3381
3382 /* Translate control characters into `\003' or `^C' form.
3383 Control characters coming from a display table entry are
3384 currently not translated because we use IT->dpvec to hold
3385 the translation. This could easily be changed but I
3386 don't believe that it is worth doing.
3387
3388 Non-printable multibyte characters are also translated
3389 octal form. */
3390 else if ((it->c < ' '
3391 && (it->area != TEXT_AREA
3392 || (it->c != '\n' && it->c != '\t')))
3393 || (it->c >= 127
3394 && it->len == 1)
3395 || !CHAR_PRINTABLE_P (it->c))
3396 {
3397 /* IT->c is a control character which must be displayed
3398 either as '\003' or as `^C' where the '\\' and '^'
3399 can be defined in the display table. Fill
3400 IT->ctl_chars with glyphs for what we have to
3401 display. Then, set IT->dpvec to these glyphs. */
3402 GLYPH g;
3403
3404 if (it->c < 128 && it->ctl_arrow_p)
3405 {
3406 /* Set IT->ctl_chars[0] to the glyph for `^'. */
3407 if (it->dp
3408 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
3409 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
3410 g = XINT (DISP_CTRL_GLYPH (it->dp));
3411 else
3412 g = FAST_MAKE_GLYPH ('^', 0);
3413 XSETINT (it->ctl_chars[0], g);
3414
3415 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
3416 XSETINT (it->ctl_chars[1], g);
3417
3418 /* Set up IT->dpvec and return first character from it. */
3419 it->dpvec_char_len = it->len;
3420 it->dpvec = it->ctl_chars;
3421 it->dpend = it->dpvec + 2;
3422 it->current.dpvec_index = 0;
3423 it->method = next_element_from_display_vector;
3424 get_next_display_element (it);
3425 }
3426 else
3427 {
3428 unsigned char str[MAX_MULTIBYTE_LENGTH];
3429 int len;
3430 int i;
3431 GLYPH escape_glyph;
3432
3433 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
3434 if (it->dp
3435 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
3436 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
3437 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
3438 else
3439 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
3440
3441 if (SINGLE_BYTE_CHAR_P (it->c))
3442 str[0] = it->c, len = 1;
3443 else
3444 len = CHAR_STRING (it->c, str);
3445
3446 for (i = 0; i < len; i++)
3447 {
3448 XSETINT (it->ctl_chars[i * 4], escape_glyph);
3449 /* Insert three more glyphs into IT->ctl_chars for
3450 the octal display of the character. */
3451 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
3452 XSETINT (it->ctl_chars[i * 4 + 1], g);
3453 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
3454 XSETINT (it->ctl_chars[i * 4 + 2], g);
3455 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
3456 XSETINT (it->ctl_chars[i * 4 + 3], g);
3457 }
3458
3459 /* Set up IT->dpvec and return the first character
3460 from it. */
3461 it->dpvec_char_len = it->len;
3462 it->dpvec = it->ctl_chars;
3463 it->dpend = it->dpvec + len * 4;
3464 it->current.dpvec_index = 0;
3465 it->method = next_element_from_display_vector;
3466 get_next_display_element (it);
3467 }
3468 }
3469 }
3470
3471 /* Adjust face id for a multibyte character. There are no
3472 multibyte character in unibyte text. */
3473 if (it->multibyte_p
3474 && success_p
3475 && FRAME_WINDOW_P (it->f))
3476 {
3477 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3478 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
3479 }
3480 }
3481
3482 /* Is this character the last one of a run of characters with
3483 box? If yes, set IT->end_of_box_run_p to 1. */
3484 if (it->face_box_p
3485 && it->s == NULL)
3486 {
3487 int face_id;
3488 struct face *face;
3489
3490 it->end_of_box_run_p
3491 = ((face_id = face_after_it_pos (it),
3492 face_id != it->face_id)
3493 && (face = FACE_FROM_ID (it->f, face_id),
3494 face->box == FACE_NO_BOX));
3495 }
3496
3497 /* Value is 0 if end of buffer or string reached. */
3498 return success_p;
3499 }
3500
3501
3502 /* Move IT to the next display element.
3503
3504 Functions get_next_display_element and set_iterator_to_next are
3505 separate because I find this arrangement easier to handle than a
3506 get_next_display_element function that also increments IT's
3507 position. The way it is we can first look at an iterator's current
3508 display element, decide whether it fits on a line, and if it does,
3509 increment the iterator position. The other way around we probably
3510 would either need a flag indicating whether the iterator has to be
3511 incremented the next time, or we would have to implement a
3512 decrement position function which would not be easy to write. */
3513
3514 void
3515 set_iterator_to_next (it)
3516 struct it *it;
3517 {
3518 if (it->method == next_element_from_buffer)
3519 {
3520 /* The current display element of IT is a character from
3521 current_buffer. Advance in the buffer, and maybe skip over
3522 invisible lines that are so because of selective display. */
3523 if (ITERATOR_AT_END_OF_LINE_P (it))
3524 reseat_at_next_visible_line_start (it, 0);
3525 else
3526 {
3527 xassert (it->len != 0);
3528 IT_BYTEPOS (*it) += it->len;
3529 IT_CHARPOS (*it) += 1;
3530 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
3531 }
3532 }
3533 else if (it->method == next_element_from_composition)
3534 {
3535 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
3536 if (STRINGP (it->string))
3537 {
3538 IT_STRING_BYTEPOS (*it) += it->len;
3539 IT_STRING_CHARPOS (*it) += it->cmp_len;
3540 it->method = next_element_from_string;
3541 goto consider_string_end;
3542 }
3543 else
3544 {
3545 IT_BYTEPOS (*it) += it->len;
3546 IT_CHARPOS (*it) += it->cmp_len;
3547 it->method = next_element_from_buffer;
3548 }
3549 }
3550 else if (it->method == next_element_from_c_string)
3551 {
3552 /* Current display element of IT is from a C string. */
3553 IT_BYTEPOS (*it) += it->len;
3554 IT_CHARPOS (*it) += 1;
3555 }
3556 else if (it->method == next_element_from_display_vector)
3557 {
3558 /* Current display element of IT is from a display table entry.
3559 Advance in the display table definition. Reset it to null if
3560 end reached, and continue with characters from buffers/
3561 strings. */
3562 ++it->current.dpvec_index;
3563
3564 /* Restore face of the iterator to what they were before the
3565 display vector entry (these entries may contain faces). */
3566 it->face_id = it->saved_face_id;
3567
3568 if (it->dpvec + it->current.dpvec_index == it->dpend)
3569 {
3570 if (it->s)
3571 it->method = next_element_from_c_string;
3572 else if (STRINGP (it->string))
3573 it->method = next_element_from_string;
3574 else
3575 it->method = next_element_from_buffer;
3576
3577 it->dpvec = NULL;
3578 it->current.dpvec_index = -1;
3579
3580 /* Skip over characters which were displayed via IT->dpvec. */
3581 if (it->dpvec_char_len < 0)
3582 reseat_at_next_visible_line_start (it, 1);
3583 else if (it->dpvec_char_len > 0)
3584 {
3585 it->len = it->dpvec_char_len;
3586 set_iterator_to_next (it);
3587 }
3588 }
3589 }
3590 else if (it->method == next_element_from_string)
3591 {
3592 /* Current display element is a character from a Lisp string. */
3593 xassert (it->s == NULL && STRINGP (it->string));
3594 IT_STRING_BYTEPOS (*it) += it->len;
3595 IT_STRING_CHARPOS (*it) += 1;
3596
3597 consider_string_end:
3598
3599 if (it->current.overlay_string_index >= 0)
3600 {
3601 /* IT->string is an overlay string. Advance to the
3602 next, if there is one. */
3603 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
3604 next_overlay_string (it);
3605 }
3606 else
3607 {
3608 /* IT->string is not an overlay string. If we reached
3609 its end, and there is something on IT->stack, proceed
3610 with what is on the stack. This can be either another
3611 string, this time an overlay string, or a buffer. */
3612 if (IT_STRING_CHARPOS (*it) == XSTRING (it->string)->size
3613 && it->sp > 0)
3614 {
3615 pop_it (it);
3616 if (!STRINGP (it->string))
3617 it->method = next_element_from_buffer;
3618 }
3619 }
3620 }
3621 else if (it->method == next_element_from_image
3622 || it->method == next_element_from_stretch)
3623 {
3624 /* The position etc with which we have to proceed are on
3625 the stack. The position may be at the end of a string,
3626 if the `display' property takes up the whole string. */
3627 pop_it (it);
3628 it->image_id = 0;
3629 if (STRINGP (it->string))
3630 {
3631 it->method = next_element_from_string;
3632 goto consider_string_end;
3633 }
3634 else
3635 it->method = next_element_from_buffer;
3636 }
3637 else
3638 /* There are no other methods defined, so this should be a bug. */
3639 abort ();
3640
3641 /* Reset flags indicating start and end of a sequence of
3642 characters with box. */
3643 it->start_of_box_run_p = it->end_of_box_run_p = 0;
3644
3645 xassert (it->method != next_element_from_string
3646 || (STRINGP (it->string)
3647 && IT_STRING_CHARPOS (*it) >= 0));
3648 }
3649
3650
3651 /* Load IT's display element fields with information about the next
3652 display element which comes from a display table entry or from the
3653 result of translating a control character to one of the forms `^C'
3654 or `\003'. IT->dpvec holds the glyphs to return as characters. */
3655
3656 static int
3657 next_element_from_display_vector (it)
3658 struct it *it;
3659 {
3660 /* Precondition. */
3661 xassert (it->dpvec && it->current.dpvec_index >= 0);
3662
3663 /* Remember the current face id in case glyphs specify faces.
3664 IT's face is restored in set_iterator_to_next. */
3665 it->saved_face_id = it->face_id;
3666
3667 if (INTEGERP (*it->dpvec)
3668 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
3669 {
3670 int lface_id;
3671 GLYPH g;
3672
3673 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
3674 it->c = FAST_GLYPH_CHAR (g);
3675 it->len = CHAR_BYTES (it->c);
3676
3677 /* The entry may contain a face id to use. Such a face id is
3678 the id of a Lisp face, not a realized face. A face id of
3679 zero means no face. */
3680 lface_id = FAST_GLYPH_FACE (g);
3681 if (lface_id)
3682 {
3683 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
3684 if (face_id >= 0)
3685 {
3686 it->face_id = face_id;
3687 }
3688 }
3689 }
3690 else
3691 /* Display table entry is invalid. Return a space. */
3692 it->c = ' ', it->len = 1;
3693
3694 /* Don't change position and object of the iterator here. They are
3695 still the values of the character that had this display table
3696 entry or was translated, and that's what we want. */
3697 it->what = IT_CHARACTER;
3698 return 1;
3699 }
3700
3701
3702 /* Load IT with the next display element from Lisp string IT->string.
3703 IT->current.string_pos is the current position within the string.
3704 If IT->current.overlay_string_index >= 0, the Lisp string is an
3705 overlay string. */
3706
3707 static int
3708 next_element_from_string (it)
3709 struct it *it;
3710 {
3711 struct text_pos position;
3712
3713 xassert (STRINGP (it->string));
3714 xassert (IT_STRING_CHARPOS (*it) >= 0);
3715 position = it->current.string_pos;
3716
3717 /* Time to check for invisible text? */
3718 if (IT_STRING_CHARPOS (*it) < it->end_charpos
3719 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
3720 {
3721 handle_stop (it);
3722
3723 /* Since a handler may have changed IT->method, we must
3724 recurse here. */
3725 return get_next_display_element (it);
3726 }
3727
3728 if (it->current.overlay_string_index >= 0)
3729 {
3730 /* Get the next character from an overlay string. In overlay
3731 strings, There is no field width or padding with spaces to
3732 do. */
3733 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
3734 {
3735 it->what = IT_EOB;
3736 return 0;
3737 }
3738 else if (STRING_MULTIBYTE (it->string))
3739 {
3740 int remaining = (STRING_BYTES (XSTRING (it->string))
3741 - IT_STRING_BYTEPOS (*it));
3742 unsigned char *s = (XSTRING (it->string)->data
3743 + IT_STRING_BYTEPOS (*it));
3744 it->c = string_char_and_length (s, remaining, &it->len);
3745 }
3746 else
3747 {
3748 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
3749 it->len = 1;
3750 }
3751 }
3752 else
3753 {
3754 /* Get the next character from a Lisp string that is not an
3755 overlay string. Such strings come from the mode line, for
3756 example. We may have to pad with spaces, or truncate the
3757 string. See also next_element_from_c_string. */
3758 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
3759 {
3760 it->what = IT_EOB;
3761 return 0;
3762 }
3763 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
3764 {
3765 /* Pad with spaces. */
3766 it->c = ' ', it->len = 1;
3767 CHARPOS (position) = BYTEPOS (position) = -1;
3768 }
3769 else if (STRING_MULTIBYTE (it->string))
3770 {
3771 int maxlen = (STRING_BYTES (XSTRING (it->string))
3772 - IT_STRING_BYTEPOS (*it));
3773 unsigned char *s = (XSTRING (it->string)->data
3774 + IT_STRING_BYTEPOS (*it));
3775 it->c = string_char_and_length (s, maxlen, &it->len);
3776 }
3777 else
3778 {
3779 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
3780 it->len = 1;
3781 }
3782 }
3783
3784 /* Record what we have and where it came from. Note that we store a
3785 buffer position in IT->position although it could arguably be a
3786 string position. */
3787 it->what = IT_CHARACTER;
3788 it->object = it->string;
3789 it->position = position;
3790 return 1;
3791 }
3792
3793
3794 /* Load IT with next display element from C string IT->s.
3795 IT->string_nchars is the maximum number of characters to return
3796 from the string. IT->end_charpos may be greater than
3797 IT->string_nchars when this function is called, in which case we
3798 may have to return padding spaces. Value is zero if end of string
3799 reached, including padding spaces. */
3800
3801 static int
3802 next_element_from_c_string (it)
3803 struct it *it;
3804 {
3805 int success_p = 1;
3806
3807 xassert (it->s);
3808 it->what = IT_CHARACTER;
3809 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
3810 it->object = Qnil;
3811
3812 /* IT's position can be greater IT->string_nchars in case a field
3813 width or precision has been specified when the iterator was
3814 initialized. */
3815 if (IT_CHARPOS (*it) >= it->end_charpos)
3816 {
3817 /* End of the game. */
3818 it->what = IT_EOB;
3819 success_p = 0;
3820 }
3821 else if (IT_CHARPOS (*it) >= it->string_nchars)
3822 {
3823 /* Pad with spaces. */
3824 it->c = ' ', it->len = 1;
3825 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
3826 }
3827 else if (it->multibyte_p)
3828 {
3829 /* Implementation note: The calls to strlen apparently aren't a
3830 performance problem because there is no noticeable performance
3831 difference between Emacs running in unibyte or multibyte mode. */
3832 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
3833 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
3834 maxlen, &it->len);
3835 }
3836 else
3837 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
3838
3839 return success_p;
3840 }
3841
3842
3843 /* Set up IT to return characters from an ellipsis, if appropriate.
3844 The definition of the ellipsis glyphs may come from a display table
3845 entry. This function Fills IT with the first glyph from the
3846 ellipsis if an ellipsis is to be displayed. */
3847
3848 static int
3849 next_element_from_ellipsis (it)
3850 struct it *it;
3851 {
3852 if (it->selective_display_ellipsis_p)
3853 {
3854 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3855 {
3856 /* Use the display table definition for `...'. Invalid glyphs
3857 will be handled by the method returning elements from dpvec. */
3858 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3859 it->dpvec_char_len = it->len;
3860 it->dpvec = v->contents;
3861 it->dpend = v->contents + v->size;
3862 it->current.dpvec_index = 0;
3863 it->method = next_element_from_display_vector;
3864 }
3865 else
3866 {
3867 /* Use default `...' which is stored in default_invis_vector. */
3868 it->dpvec_char_len = it->len;
3869 it->dpvec = default_invis_vector;
3870 it->dpend = default_invis_vector + 3;
3871 it->current.dpvec_index = 0;
3872 it->method = next_element_from_display_vector;
3873 }
3874 }
3875 else
3876 reseat_at_next_visible_line_start (it, 1);
3877
3878 return get_next_display_element (it);
3879 }
3880
3881
3882 /* Deliver an image display element. The iterator IT is already
3883 filled with image information (done in handle_display_prop). Value
3884 is always 1. */
3885
3886
3887 static int
3888 next_element_from_image (it)
3889 struct it *it;
3890 {
3891 it->what = IT_IMAGE;
3892 return 1;
3893 }
3894
3895
3896 /* Fill iterator IT with next display element from a stretch glyph
3897 property. IT->object is the value of the text property. Value is
3898 always 1. */
3899
3900 static int
3901 next_element_from_stretch (it)
3902 struct it *it;
3903 {
3904 it->what = IT_STRETCH;
3905 return 1;
3906 }
3907
3908
3909 /* Load IT with the next display element from current_buffer. Value
3910 is zero if end of buffer reached. IT->stop_charpos is the next
3911 position at which to stop and check for text properties or buffer
3912 end. */
3913
3914 static int
3915 next_element_from_buffer (it)
3916 struct it *it;
3917 {
3918 int success_p = 1;
3919
3920 /* Check this assumption, otherwise, we would never enter the
3921 if-statement, below. */
3922 xassert (IT_CHARPOS (*it) >= BEGV
3923 && IT_CHARPOS (*it) <= it->stop_charpos);
3924
3925 if (IT_CHARPOS (*it) >= it->stop_charpos)
3926 {
3927 if (IT_CHARPOS (*it) >= it->end_charpos)
3928 {
3929 int overlay_strings_follow_p;
3930
3931 /* End of the game, except when overlay strings follow that
3932 haven't been returned yet. */
3933 if (it->overlay_strings_at_end_processed_p)
3934 overlay_strings_follow_p = 0;
3935 else
3936 {
3937 it->overlay_strings_at_end_processed_p = 1;
3938 overlay_strings_follow_p = get_overlay_strings (it);
3939 }
3940
3941 if (overlay_strings_follow_p)
3942 success_p = get_next_display_element (it);
3943 else
3944 {
3945 it->what = IT_EOB;
3946 it->position = it->current.pos;
3947 success_p = 0;
3948 }
3949 }
3950 else
3951 {
3952 handle_stop (it);
3953 return get_next_display_element (it);
3954 }
3955 }
3956 else
3957 {
3958 /* No face changes, overlays etc. in sight, so just return a
3959 character from current_buffer. */
3960 unsigned char *p;
3961
3962 /* Maybe run the redisplay end trigger hook. Performance note:
3963 This doesn't seem to cost measurable time. */
3964 if (it->redisplay_end_trigger_charpos
3965 && it->glyph_row
3966 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
3967 run_redisplay_end_trigger_hook (it);
3968
3969 /* Get the next character, maybe multibyte. */
3970 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
3971 if (it->multibyte_p && !ASCII_BYTE_P (*p))
3972 {
3973 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
3974 - IT_BYTEPOS (*it));
3975 it->c = string_char_and_length (p, maxlen, &it->len);
3976 }
3977 else
3978 it->c = *p, it->len = 1;
3979
3980 /* Record what we have and where it came from. */
3981 it->what = IT_CHARACTER;;
3982 it->object = it->w->buffer;
3983 it->position = it->current.pos;
3984
3985 /* Normally we return the character found above, except when we
3986 really want to return an ellipsis for selective display. */
3987 if (it->selective)
3988 {
3989 if (it->c == '\n')
3990 {
3991 /* A value of selective > 0 means hide lines indented more
3992 than that number of columns. */
3993 if (it->selective > 0
3994 && IT_CHARPOS (*it) + 1 < ZV
3995 && indented_beyond_p (IT_CHARPOS (*it) + 1,
3996 IT_BYTEPOS (*it) + 1,
3997 it->selective))
3998 {
3999 success_p = next_element_from_ellipsis (it);
4000 it->dpvec_char_len = -1;
4001 }
4002 }
4003 else if (it->c == '\r' && it->selective == -1)
4004 {
4005 /* A value of selective == -1 means that everything from the
4006 CR to the end of the line is invisible, with maybe an
4007 ellipsis displayed for it. */
4008 success_p = next_element_from_ellipsis (it);
4009 it->dpvec_char_len = -1;
4010 }
4011 }
4012 }
4013
4014 /* Value is zero if end of buffer reached. */
4015 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
4016 return success_p;
4017 }
4018
4019
4020 /* Run the redisplay end trigger hook for IT. */
4021
4022 static void
4023 run_redisplay_end_trigger_hook (it)
4024 struct it *it;
4025 {
4026 Lisp_Object args[3];
4027
4028 /* IT->glyph_row should be non-null, i.e. we should be actually
4029 displaying something, or otherwise we should not run the hook. */
4030 xassert (it->glyph_row);
4031
4032 /* Set up hook arguments. */
4033 args[0] = Qredisplay_end_trigger_functions;
4034 args[1] = it->window;
4035 XSETINT (args[2], it->redisplay_end_trigger_charpos);
4036 it->redisplay_end_trigger_charpos = 0;
4037
4038 /* Since we are *trying* to run these functions, don't try to run
4039 them again, even if they get an error. */
4040 it->w->redisplay_end_trigger = Qnil;
4041 Frun_hook_with_args (3, args);
4042
4043 /* Notice if it changed the face of the character we are on. */
4044 handle_face_prop (it);
4045 }
4046
4047
4048 /* Deliver a composition display element. The iterator IT is already
4049 filled with composition information (done in
4050 handle_composition_prop). Value is always 1. */
4051
4052 static int
4053 next_element_from_composition (it)
4054 struct it *it;
4055 {
4056 it->what = IT_COMPOSITION;
4057 it->position = (STRINGP (it->string)
4058 ? it->current.string_pos
4059 : it->current.pos);
4060 return 1;
4061 }
4062
4063
4064 \f
4065 /***********************************************************************
4066 Moving an iterator without producing glyphs
4067 ***********************************************************************/
4068
4069 /* Move iterator IT to a specified buffer or X position within one
4070 line on the display without producing glyphs.
4071
4072 Begin to skip at IT's current position. Skip to TO_CHARPOS or TO_X
4073 whichever is reached first.
4074
4075 TO_CHARPOS <= 0 means no TO_CHARPOS is specified.
4076
4077 TO_X < 0 means that no TO_X is specified. TO_X is normally a value
4078 0 <= TO_X <= IT->last_visible_x. This means in particular, that
4079 TO_X includes the amount by which a window is horizontally
4080 scrolled.
4081
4082 Value is
4083
4084 MOVE_POS_MATCH_OR_ZV
4085 - when TO_POS or ZV was reached.
4086
4087 MOVE_X_REACHED
4088 -when TO_X was reached before TO_POS or ZV were reached.
4089
4090 MOVE_LINE_CONTINUED
4091 - when we reached the end of the display area and the line must
4092 be continued.
4093
4094 MOVE_LINE_TRUNCATED
4095 - when we reached the end of the display area and the line is
4096 truncated.
4097
4098 MOVE_NEWLINE_OR_CR
4099 - when we stopped at a line end, i.e. a newline or a CR and selective
4100 display is on. */
4101
4102 static enum move_it_result
4103 move_it_in_display_line_to (it, to_charpos, to_x, op)
4104 struct it *it;
4105 int to_charpos, to_x, op;
4106 {
4107 enum move_it_result result = MOVE_UNDEFINED;
4108 struct glyph_row *saved_glyph_row;
4109
4110 /* Don't produce glyphs in produce_glyphs. */
4111 saved_glyph_row = it->glyph_row;
4112 it->glyph_row = NULL;
4113
4114 while (1)
4115 {
4116 int x, i;
4117
4118 /* Stop when ZV or TO_CHARPOS reached. */
4119 if (!get_next_display_element (it)
4120 || ((op & MOVE_TO_POS) != 0
4121 && BUFFERP (it->object)
4122 && IT_CHARPOS (*it) >= to_charpos))
4123 {
4124 result = MOVE_POS_MATCH_OR_ZV;
4125 break;
4126 }
4127
4128 /* The call to produce_glyphs will get the metrics of the
4129 display element IT is loaded with. We record in x the
4130 x-position before this display element in case it does not
4131 fit on the line. */
4132 x = it->current_x;
4133 PRODUCE_GLYPHS (it);
4134
4135 if (it->area != TEXT_AREA)
4136 {
4137 set_iterator_to_next (it);
4138 continue;
4139 }
4140
4141 /* The number of glyphs we get back in IT->nglyphs will normally
4142 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
4143 character on a terminal frame, or (iii) a line end. For the
4144 second case, IT->nglyphs - 1 padding glyphs will be present
4145 (on X frames, there is only one glyph produced for a
4146 composite character.
4147
4148 The behavior implemented below means, for continuation lines,
4149 that as many spaces of a TAB as fit on the current line are
4150 displayed there. For terminal frames, as many glyphs of a
4151 multi-glyph character are displayed in the current line, too.
4152 This is what the old redisplay code did, and we keep it that
4153 way. Under X, the whole shape of a complex character must
4154 fit on the line or it will be completely displayed in the
4155 next line.
4156
4157 Note that both for tabs and padding glyphs, all glyphs have
4158 the same width. */
4159 if (it->nglyphs)
4160 {
4161 /* More than one glyph or glyph doesn't fit on line. All
4162 glyphs have the same width. */
4163 int single_glyph_width = it->pixel_width / it->nglyphs;
4164 int new_x;
4165
4166 for (i = 0; i < it->nglyphs; ++i, x = new_x)
4167 {
4168 new_x = x + single_glyph_width;
4169
4170 /* We want to leave anything reaching TO_X to the caller. */
4171 if ((op & MOVE_TO_X) && new_x > to_x)
4172 {
4173 it->current_x = x;
4174 result = MOVE_X_REACHED;
4175 break;
4176 }
4177 else if (/* Lines are continued. */
4178 !it->truncate_lines_p
4179 && (/* And glyph doesn't fit on the line. */
4180 new_x > it->last_visible_x
4181 /* Or it fits exactly and we're on a window
4182 system frame. */
4183 || (new_x == it->last_visible_x
4184 && FRAME_WINDOW_P (it->f))))
4185 {
4186 if (/* IT->hpos == 0 means the very first glyph
4187 doesn't fit on the line, e.g. a wide image. */
4188 it->hpos == 0
4189 || (new_x == it->last_visible_x
4190 && FRAME_WINDOW_P (it->f)))
4191 {
4192 ++it->hpos;
4193 it->current_x = new_x;
4194 if (i == it->nglyphs - 1)
4195 set_iterator_to_next (it);
4196 }
4197 else
4198 it->current_x = x;
4199
4200 result = MOVE_LINE_CONTINUED;
4201 break;
4202 }
4203 else if (new_x > it->first_visible_x)
4204 {
4205 /* Glyph is visible. Increment number of glyphs that
4206 would be displayed. */
4207 ++it->hpos;
4208 }
4209 else
4210 {
4211 /* Glyph is completely off the left margin of the display
4212 area. Nothing to do. */
4213 }
4214 }
4215
4216 if (result != MOVE_UNDEFINED)
4217 break;
4218 }
4219 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
4220 {
4221 /* Stop when TO_X specified and reached. This check is
4222 necessary here because of lines consisting of a line end,
4223 only. The line end will not produce any glyphs and we
4224 would never get MOVE_X_REACHED. */
4225 xassert (it->nglyphs == 0);
4226 result = MOVE_X_REACHED;
4227 break;
4228 }
4229
4230 /* Is this a line end? If yes, we're done. */
4231 if (ITERATOR_AT_END_OF_LINE_P (it))
4232 {
4233 result = MOVE_NEWLINE_OR_CR;
4234 break;
4235 }
4236
4237 /* The current display element has been consumed. Advance
4238 to the next. */
4239 set_iterator_to_next (it);
4240
4241 /* Stop if lines are truncated and IT's current x-position is
4242 past the right edge of the window now. */
4243 if (it->truncate_lines_p
4244 && it->current_x >= it->last_visible_x)
4245 {
4246 result = MOVE_LINE_TRUNCATED;
4247 break;
4248 }
4249 }
4250
4251 /* Restore the iterator settings altered at the beginning of this
4252 function. */
4253 it->glyph_row = saved_glyph_row;
4254 return result;
4255 }
4256
4257
4258 /* Move IT forward to a specified buffer position TO_CHARPOS, TO_X,
4259 TO_Y, TO_VPOS. OP is a bit-mask that specifies where to stop. See
4260 the description of enum move_operation_enum.
4261
4262 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
4263 screen line, this function will set IT to the next position >
4264 TO_CHARPOS. */
4265
4266 void
4267 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
4268 struct it *it;
4269 int to_charpos, to_x, to_y, to_vpos;
4270 int op;
4271 {
4272 enum move_it_result skip, skip2 = MOVE_X_REACHED;
4273 int line_height;
4274
4275 while (1)
4276 {
4277 if (op & MOVE_TO_VPOS)
4278 {
4279 /* If no TO_CHARPOS and no TO_X specified, stop at the
4280 start of the line TO_VPOS. */
4281 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
4282 {
4283 if (it->vpos == to_vpos)
4284 break;
4285 skip = move_it_in_display_line_to (it, -1, -1, 0);
4286 }
4287 else
4288 {
4289 /* TO_VPOS >= 0 means stop at TO_X in the line at
4290 TO_VPOS, or at TO_POS, whichever comes first. */
4291 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
4292
4293 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
4294 break;
4295 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
4296 {
4297 /* We have reached TO_X but not in the line we want. */
4298 skip = move_it_in_display_line_to (it, to_charpos,
4299 -1, MOVE_TO_POS);
4300 if (skip == MOVE_POS_MATCH_OR_ZV)
4301 break;
4302 }
4303 }
4304 }
4305 else if (op & MOVE_TO_Y)
4306 {
4307 struct it it_backup;
4308 int done_p;
4309
4310 /* TO_Y specified means stop at TO_X in the line containing
4311 TO_Y---or at TO_CHARPOS if this is reached first. The
4312 problem is that we can't really tell whether the line
4313 contains TO_Y before we have completely scanned it, and
4314 this may skip past TO_X. What we do is to first scan to
4315 TO_X.
4316
4317 If TO_X is not specified, use a TO_X of zero. The reason
4318 is to make the outcome of this function more predictable.
4319 If we didn't use TO_X == 0, we would stop at the end of
4320 the line which is probably not what a caller would expect
4321 to happen. */
4322 skip = move_it_in_display_line_to (it, to_charpos,
4323 ((op & MOVE_TO_X)
4324 ? to_x : 0),
4325 (MOVE_TO_X
4326 | (op & MOVE_TO_POS)));
4327
4328 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
4329 if (skip == MOVE_POS_MATCH_OR_ZV)
4330 break;
4331
4332 /* If TO_X was reached, we would like to know whether TO_Y
4333 is in the line. This can only be said if we know the
4334 total line height which requires us to scan the rest of
4335 the line. */
4336 done_p = 0;
4337 if (skip == MOVE_X_REACHED)
4338 {
4339 it_backup = *it;
4340 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
4341 op & MOVE_TO_POS);
4342 }
4343
4344 /* Now, decide whether TO_Y is in this line. */
4345 line_height = it->max_ascent + it->max_descent;
4346
4347 if (to_y >= it->current_y
4348 && to_y < it->current_y + line_height)
4349 {
4350 if (skip == MOVE_X_REACHED)
4351 /* If TO_Y is in this line and TO_X was reached above,
4352 we scanned too far. We have to restore IT's settings
4353 to the ones before skipping. */
4354 *it = it_backup;
4355 done_p = 1;
4356 }
4357 else if (skip == MOVE_X_REACHED)
4358 {
4359 skip = skip2;
4360 if (skip == MOVE_POS_MATCH_OR_ZV)
4361 done_p = 1;
4362 }
4363
4364 if (done_p)
4365 break;
4366 }
4367 else
4368 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
4369
4370 switch (skip)
4371 {
4372 case MOVE_POS_MATCH_OR_ZV:
4373 return;
4374
4375 case MOVE_NEWLINE_OR_CR:
4376 set_iterator_to_next (it);
4377 it->continuation_lines_width = 0;
4378 break;
4379
4380 case MOVE_LINE_TRUNCATED:
4381 it->continuation_lines_width = 0;
4382 reseat_at_next_visible_line_start (it, 0);
4383 if ((op & MOVE_TO_POS) != 0
4384 && IT_CHARPOS (*it) > to_charpos)
4385 goto out;
4386 break;
4387
4388 case MOVE_LINE_CONTINUED:
4389 it->continuation_lines_width += it->current_x;
4390 break;
4391
4392 default:
4393 abort ();
4394 }
4395
4396 /* Reset/increment for the next run. */
4397 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
4398 it->current_x = it->hpos = 0;
4399 it->current_y += it->max_ascent + it->max_descent;
4400 ++it->vpos;
4401 last_height = it->max_ascent + it->max_descent;
4402 last_max_ascent = it->max_ascent;
4403 it->max_ascent = it->max_descent = 0;
4404 }
4405 out:;
4406 }
4407
4408
4409 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
4410
4411 If DY > 0, move IT backward at least that many pixels. DY = 0
4412 means move IT backward to the preceding line start or BEGV. This
4413 function may move over more than DY pixels if IT->current_y - DY
4414 ends up in the middle of a line; in this case IT->current_y will be
4415 set to the top of the line moved to. */
4416
4417 void
4418 move_it_vertically_backward (it, dy)
4419 struct it *it;
4420 int dy;
4421 {
4422 int nlines, h, line_height;
4423 struct it it2;
4424 int start_pos = IT_CHARPOS (*it);
4425
4426 xassert (dy >= 0);
4427
4428 /* Estimate how many newlines we must move back. */
4429 nlines = max (1, dy / CANON_Y_UNIT (it->f));
4430
4431 /* Set the iterator's position that many lines back. */
4432 while (nlines-- && IT_CHARPOS (*it) > BEGV)
4433 back_to_previous_visible_line_start (it);
4434
4435 /* Reseat the iterator here. When moving backward, we don't want
4436 reseat to skip forward over invisible text, set up the iterator
4437 to deliver from overlay strings at the new position etc. So,
4438 use reseat_1 here. */
4439 reseat_1 (it, it->current.pos, 1);
4440
4441 /* We are now surely at a line start. */
4442 it->current_x = it->hpos = 0;
4443
4444 /* Move forward and see what y-distance we moved. First move to the
4445 start of the next line so that we get its height. We need this
4446 height to be able to tell whether we reached the specified
4447 y-distance. */
4448 it2 = *it;
4449 it2.max_ascent = it2.max_descent = 0;
4450 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
4451 MOVE_TO_POS | MOVE_TO_VPOS);
4452 xassert (IT_CHARPOS (*it) >= BEGV);
4453 line_height = it2.max_ascent + it2.max_descent;
4454 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
4455 xassert (IT_CHARPOS (*it) >= BEGV);
4456 h = it2.current_y - it->current_y;
4457 nlines = it2.vpos - it->vpos;
4458
4459 /* Correct IT's y and vpos position. */
4460 it->vpos -= nlines;
4461 it->current_y -= h;
4462
4463 if (dy == 0)
4464 {
4465 /* DY == 0 means move to the start of the screen line. The
4466 value of nlines is > 0 if continuation lines were involved. */
4467 if (nlines > 0)
4468 move_it_by_lines (it, nlines, 1);
4469 xassert (IT_CHARPOS (*it) <= start_pos);
4470 }
4471 else if (nlines)
4472 {
4473 /* The y-position we try to reach. Note that h has been
4474 subtracted in front of the if-statement. */
4475 int target_y = it->current_y + h - dy;
4476
4477 /* If we did not reach target_y, try to move further backward if
4478 we can. If we moved too far backward, try to move forward. */
4479 if (target_y < it->current_y
4480 && IT_CHARPOS (*it) > BEGV)
4481 {
4482 move_it_vertically (it, target_y - it->current_y);
4483 xassert (IT_CHARPOS (*it) >= BEGV);
4484 }
4485 else if (target_y >= it->current_y + line_height
4486 && IT_CHARPOS (*it) < ZV)
4487 {
4488 move_it_vertically (it, target_y - (it->current_y + line_height));
4489 xassert (IT_CHARPOS (*it) >= BEGV);
4490 }
4491 }
4492 }
4493
4494
4495 /* Move IT by a specified amount of pixel lines DY. DY negative means
4496 move backwards. DY = 0 means move to start of screen line. At the
4497 end, IT will be on the start of a screen line. */
4498
4499 void
4500 move_it_vertically (it, dy)
4501 struct it *it;
4502 int dy;
4503 {
4504 if (dy <= 0)
4505 move_it_vertically_backward (it, -dy);
4506 else if (dy > 0)
4507 {
4508 move_it_to (it, ZV, -1, it->current_y + dy, -1,
4509 MOVE_TO_POS | MOVE_TO_Y);
4510
4511 /* If buffer ends in ZV without a newline, move to the start of
4512 the line to satisfy the post-condition. */
4513 if (IT_CHARPOS (*it) == ZV
4514 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
4515 move_it_by_lines (it, 0, 0);
4516 }
4517 }
4518
4519
4520 /* Return non-zero if some text between buffer positions START_CHARPOS
4521 and END_CHARPOS is invisible. IT->window is the window for text
4522 property lookup. */
4523
4524 static int
4525 invisible_text_between_p (it, start_charpos, end_charpos)
4526 struct it *it;
4527 int start_charpos, end_charpos;
4528 {
4529 Lisp_Object prop, limit;
4530 int invisible_found_p;
4531
4532 xassert (it != NULL && start_charpos <= end_charpos);
4533
4534 /* Is text at START invisible? */
4535 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
4536 it->window);
4537 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4538 invisible_found_p = 1;
4539 else
4540 {
4541 limit = next_single_char_property_change (make_number (start_charpos),
4542 Qinvisible, Qnil,
4543 make_number (end_charpos));
4544 invisible_found_p = XFASTINT (limit) < end_charpos;
4545 }
4546
4547 return invisible_found_p;
4548 }
4549
4550
4551 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
4552 negative means move up. DVPOS == 0 means move to the start of the
4553 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
4554 NEED_Y_P is zero, IT->current_y will be left unchanged.
4555
4556 Further optimization ideas: If we would know that IT->f doesn't use
4557 a face with proportional font, we could be faster for
4558 truncate-lines nil. */
4559
4560 void
4561 move_it_by_lines (it, dvpos, need_y_p)
4562 struct it *it;
4563 int dvpos, need_y_p;
4564 {
4565 struct position pos;
4566
4567 if (!FRAME_WINDOW_P (it->f))
4568 {
4569 struct text_pos textpos;
4570
4571 /* We can use vmotion on frames without proportional fonts. */
4572 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
4573 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
4574 reseat (it, textpos, 1);
4575 it->vpos += pos.vpos;
4576 it->current_y += pos.vpos;
4577 }
4578 else if (dvpos == 0)
4579 {
4580 /* DVPOS == 0 means move to the start of the screen line. */
4581 move_it_vertically_backward (it, 0);
4582 xassert (it->current_x == 0 && it->hpos == 0);
4583 }
4584 else if (dvpos > 0)
4585 {
4586 /* If there are no continuation lines, and if there is no
4587 selective display, try the simple method of moving forward
4588 DVPOS newlines, then see where we are. */
4589 if (!need_y_p && it->truncate_lines_p && it->selective == 0)
4590 {
4591 int shortage = 0, charpos;
4592
4593 if (FETCH_BYTE (IT_BYTEPOS (*it) == '\n'))
4594 charpos = IT_CHARPOS (*it) + 1;
4595 else
4596 charpos = scan_buffer ('\n', IT_CHARPOS (*it), 0, dvpos,
4597 &shortage, 0);
4598
4599 if (!invisible_text_between_p (it, IT_CHARPOS (*it), charpos))
4600 {
4601 struct text_pos pos;
4602 CHARPOS (pos) = charpos;
4603 BYTEPOS (pos) = CHAR_TO_BYTE (charpos);
4604 reseat (it, pos, 1);
4605 it->vpos += dvpos - shortage;
4606 it->hpos = it->current_x = 0;
4607 return;
4608 }
4609 }
4610
4611 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
4612 }
4613 else
4614 {
4615 struct it it2;
4616 int start_charpos, i;
4617
4618 /* If there are no continuation lines, and if there is no
4619 selective display, try the simple method of moving backward
4620 -DVPOS newlines. */
4621 if (!need_y_p && it->truncate_lines_p && it->selective == 0)
4622 {
4623 int shortage;
4624 int charpos = IT_CHARPOS (*it);
4625 int bytepos = IT_BYTEPOS (*it);
4626
4627 /* If in the middle of a line, go to its start. */
4628 if (charpos > BEGV && FETCH_BYTE (bytepos - 1) != '\n')
4629 {
4630 charpos = find_next_newline_no_quit (charpos, -1);
4631 bytepos = CHAR_TO_BYTE (charpos);
4632 }
4633
4634 if (charpos == BEGV)
4635 {
4636 struct text_pos pos;
4637 CHARPOS (pos) = charpos;
4638 BYTEPOS (pos) = bytepos;
4639 reseat (it, pos, 1);
4640 it->hpos = it->current_x = 0;
4641 return;
4642 }
4643 else
4644 {
4645 charpos = scan_buffer ('\n', charpos - 1, 0, dvpos, &shortage, 0);
4646 if (!invisible_text_between_p (it, charpos, IT_CHARPOS (*it)))
4647 {
4648 struct text_pos pos;
4649 CHARPOS (pos) = charpos;
4650 BYTEPOS (pos) = CHAR_TO_BYTE (charpos);
4651 reseat (it, pos, 1);
4652 it->vpos += dvpos + (shortage ? shortage - 1 : 0);
4653 it->hpos = it->current_x = 0;
4654 return;
4655 }
4656 }
4657 }
4658
4659 /* Go back -DVPOS visible lines and reseat the iterator there. */
4660 start_charpos = IT_CHARPOS (*it);
4661 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
4662 back_to_previous_visible_line_start (it);
4663 reseat (it, it->current.pos, 1);
4664 it->current_x = it->hpos = 0;
4665
4666 /* Above call may have moved too far if continuation lines
4667 are involved. Scan forward and see if it did. */
4668 it2 = *it;
4669 it2.vpos = it2.current_y = 0;
4670 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
4671 it->vpos -= it2.vpos;
4672 it->current_y -= it2.current_y;
4673 it->current_x = it->hpos = 0;
4674
4675 /* If we moved too far, move IT some lines forward. */
4676 if (it2.vpos > -dvpos)
4677 {
4678 int delta = it2.vpos + dvpos;
4679 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
4680 }
4681 }
4682 }
4683
4684
4685 \f
4686 /***********************************************************************
4687 Messages
4688 ***********************************************************************/
4689
4690
4691 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
4692 to *Messages*. */
4693
4694 void
4695 add_to_log (format, arg1, arg2)
4696 char *format;
4697 Lisp_Object arg1, arg2;
4698 {
4699 Lisp_Object args[3];
4700 Lisp_Object msg, fmt;
4701 char *buffer;
4702 int len;
4703 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4704
4705 fmt = msg = Qnil;
4706 GCPRO4 (fmt, msg, arg1, arg2);
4707
4708 args[0] = fmt = build_string (format);
4709 args[1] = arg1;
4710 args[2] = arg2;
4711 msg = Fformat (3, args);
4712
4713 len = STRING_BYTES (XSTRING (msg)) + 1;
4714 buffer = (char *) alloca (len);
4715 strcpy (buffer, XSTRING (msg)->data);
4716
4717 message_dolog (buffer, len - 1, 1, 0);
4718 UNGCPRO;
4719 }
4720
4721
4722 /* Output a newline in the *Messages* buffer if "needs" one. */
4723
4724 void
4725 message_log_maybe_newline ()
4726 {
4727 if (message_log_need_newline)
4728 message_dolog ("", 0, 1, 0);
4729 }
4730
4731
4732 /* Add a string M of length LEN to the message log, optionally
4733 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
4734 nonzero, means interpret the contents of M as multibyte. This
4735 function calls low-level routines in order to bypass text property
4736 hooks, etc. which might not be safe to run. */
4737
4738 void
4739 message_dolog (m, len, nlflag, multibyte)
4740 char *m;
4741 int len, nlflag, multibyte;
4742 {
4743 if (!NILP (Vmessage_log_max))
4744 {
4745 struct buffer *oldbuf;
4746 Lisp_Object oldpoint, oldbegv, oldzv;
4747 int old_windows_or_buffers_changed = windows_or_buffers_changed;
4748 int point_at_end = 0;
4749 int zv_at_end = 0;
4750 Lisp_Object old_deactivate_mark, tem;
4751 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4752
4753 old_deactivate_mark = Vdeactivate_mark;
4754 oldbuf = current_buffer;
4755 Fset_buffer (Fget_buffer_create (build_string ("*Messages*")));
4756 current_buffer->undo_list = Qt;
4757
4758 oldpoint = Fpoint_marker ();
4759 oldbegv = Fpoint_min_marker ();
4760 oldzv = Fpoint_max_marker ();
4761 GCPRO4 (oldpoint, oldbegv, oldzv, old_deactivate_mark);
4762
4763 if (PT == Z)
4764 point_at_end = 1;
4765 if (ZV == Z)
4766 zv_at_end = 1;
4767
4768 BEGV = BEG;
4769 BEGV_BYTE = BEG_BYTE;
4770 ZV = Z;
4771 ZV_BYTE = Z_BYTE;
4772 TEMP_SET_PT_BOTH (Z, Z_BYTE);
4773
4774 /* Insert the string--maybe converting multibyte to single byte
4775 or vice versa, so that all the text fits the buffer. */
4776 if (multibyte
4777 && NILP (current_buffer->enable_multibyte_characters))
4778 {
4779 int i, c, nbytes;
4780 unsigned char work[1];
4781
4782 /* Convert a multibyte string to single-byte
4783 for the *Message* buffer. */
4784 for (i = 0; i < len; i += nbytes)
4785 {
4786 c = string_char_and_length (m + i, len - i, &nbytes);
4787 work[0] = (SINGLE_BYTE_CHAR_P (c)
4788 ? c
4789 : multibyte_char_to_unibyte (c, Qnil));
4790 insert_1_both (work, 1, 1, 1, 0, 0);
4791 }
4792 }
4793 else if (! multibyte
4794 && ! NILP (current_buffer->enable_multibyte_characters))
4795 {
4796 int i, c, nbytes;
4797 unsigned char *msg = (unsigned char *) m;
4798 unsigned char str[MAX_MULTIBYTE_LENGTH];
4799 /* Convert a single-byte string to multibyte
4800 for the *Message* buffer. */
4801 for (i = 0; i < len; i++)
4802 {
4803 c = unibyte_char_to_multibyte (msg[i]);
4804 nbytes = CHAR_STRING (c, str);
4805 insert_1_both (str, 1, nbytes, 1, 0, 0);
4806 }
4807 }
4808 else if (len)
4809 insert_1 (m, len, 1, 0, 0);
4810
4811 if (nlflag)
4812 {
4813 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
4814 insert_1 ("\n", 1, 1, 0, 0);
4815
4816 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
4817 this_bol = PT;
4818 this_bol_byte = PT_BYTE;
4819
4820 if (this_bol > BEG)
4821 {
4822 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
4823 prev_bol = PT;
4824 prev_bol_byte = PT_BYTE;
4825
4826 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
4827 this_bol, this_bol_byte);
4828 if (dup)
4829 {
4830 del_range_both (prev_bol, prev_bol_byte,
4831 this_bol, this_bol_byte, 0);
4832 if (dup > 1)
4833 {
4834 char dupstr[40];
4835 int duplen;
4836
4837 /* If you change this format, don't forget to also
4838 change message_log_check_duplicate. */
4839 sprintf (dupstr, " [%d times]", dup);
4840 duplen = strlen (dupstr);
4841 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
4842 insert_1 (dupstr, duplen, 1, 0, 1);
4843 }
4844 }
4845 }
4846
4847 if (NATNUMP (Vmessage_log_max))
4848 {
4849 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
4850 -XFASTINT (Vmessage_log_max) - 1, 0);
4851 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
4852 }
4853 }
4854 BEGV = XMARKER (oldbegv)->charpos;
4855 BEGV_BYTE = marker_byte_position (oldbegv);
4856
4857 if (zv_at_end)
4858 {
4859 ZV = Z;
4860 ZV_BYTE = Z_BYTE;
4861 }
4862 else
4863 {
4864 ZV = XMARKER (oldzv)->charpos;
4865 ZV_BYTE = marker_byte_position (oldzv);
4866 }
4867
4868 if (point_at_end)
4869 TEMP_SET_PT_BOTH (Z, Z_BYTE);
4870 else
4871 /* We can't do Fgoto_char (oldpoint) because it will run some
4872 Lisp code. */
4873 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
4874 XMARKER (oldpoint)->bytepos);
4875
4876 UNGCPRO;
4877 free_marker (oldpoint);
4878 free_marker (oldbegv);
4879 free_marker (oldzv);
4880
4881 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
4882 set_buffer_internal (oldbuf);
4883 if (NILP (tem))
4884 windows_or_buffers_changed = old_windows_or_buffers_changed;
4885 message_log_need_newline = !nlflag;
4886 Vdeactivate_mark = old_deactivate_mark;
4887 }
4888 }
4889
4890
4891 /* We are at the end of the buffer after just having inserted a newline.
4892 (Note: We depend on the fact we won't be crossing the gap.)
4893 Check to see if the most recent message looks a lot like the previous one.
4894 Return 0 if different, 1 if the new one should just replace it, or a
4895 value N > 1 if we should also append " [N times]". */
4896
4897 static int
4898 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
4899 int prev_bol, this_bol;
4900 int prev_bol_byte, this_bol_byte;
4901 {
4902 int i;
4903 int len = Z_BYTE - 1 - this_bol_byte;
4904 int seen_dots = 0;
4905 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
4906 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
4907
4908 for (i = 0; i < len; i++)
4909 {
4910 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.'
4911 && p1[i] != '\n')
4912 seen_dots = 1;
4913 if (p1[i] != p2[i])
4914 return seen_dots;
4915 }
4916 p1 += len;
4917 if (*p1 == '\n')
4918 return 2;
4919 if (*p1++ == ' ' && *p1++ == '[')
4920 {
4921 int n = 0;
4922 while (*p1 >= '0' && *p1 <= '9')
4923 n = n * 10 + *p1++ - '0';
4924 if (strncmp (p1, " times]\n", 8) == 0)
4925 return n+1;
4926 }
4927 return 0;
4928 }
4929
4930
4931 /* Display an echo area message M with a specified length of LEN
4932 chars. The string may include null characters. If M is 0, clear
4933 out any existing message, and let the mini-buffer text show through.
4934
4935 The buffer M must continue to exist until after the echo area gets
4936 cleared or some other message gets displayed there. This means do
4937 not pass text that is stored in a Lisp string; do not pass text in
4938 a buffer that was alloca'd. */
4939
4940 void
4941 message2 (m, len, multibyte)
4942 char *m;
4943 int len;
4944 int multibyte;
4945 {
4946 /* First flush out any partial line written with print. */
4947 message_log_maybe_newline ();
4948 if (m)
4949 message_dolog (m, len, 1, multibyte);
4950 message2_nolog (m, len, multibyte);
4951 }
4952
4953
4954 /* The non-logging counterpart of message2. */
4955
4956 void
4957 message2_nolog (m, len, multibyte)
4958 char *m;
4959 int len;
4960 {
4961 struct frame *sf = SELECTED_FRAME ();
4962 message_enable_multibyte = multibyte;
4963
4964 if (noninteractive)
4965 {
4966 if (noninteractive_need_newline)
4967 putc ('\n', stderr);
4968 noninteractive_need_newline = 0;
4969 if (m)
4970 fwrite (m, len, 1, stderr);
4971 if (cursor_in_echo_area == 0)
4972 fprintf (stderr, "\n");
4973 fflush (stderr);
4974 }
4975 /* A null message buffer means that the frame hasn't really been
4976 initialized yet. Error messages get reported properly by
4977 cmd_error, so this must be just an informative message; toss it. */
4978 else if (INTERACTIVE
4979 && sf->glyphs_initialized_p
4980 && FRAME_MESSAGE_BUF (sf))
4981 {
4982 Lisp_Object mini_window;
4983 struct frame *f;
4984
4985 /* Get the frame containing the mini-buffer
4986 that the selected frame is using. */
4987 mini_window = FRAME_MINIBUF_WINDOW (sf);
4988 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
4989
4990 FRAME_SAMPLE_VISIBILITY (f);
4991 if (FRAME_VISIBLE_P (sf)
4992 && ! FRAME_VISIBLE_P (f))
4993 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
4994
4995 if (m)
4996 {
4997 set_message (m, Qnil, len, multibyte);
4998 if (minibuffer_auto_raise)
4999 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5000 }
5001 else
5002 clear_message (1, 1);
5003
5004 do_pending_window_change (0);
5005 echo_area_display (1);
5006 do_pending_window_change (0);
5007 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5008 (*frame_up_to_date_hook) (f);
5009 }
5010 }
5011
5012
5013 /* Display an echo area message M with a specified length of NBYTES
5014 bytes. The string may include null characters. If M is not a
5015 string, clear out any existing message, and let the mini-buffer
5016 text show through. */
5017
5018 void
5019 message3 (m, nbytes, multibyte)
5020 Lisp_Object m;
5021 int nbytes;
5022 int multibyte;
5023 {
5024 struct gcpro gcpro1;
5025
5026 GCPRO1 (m);
5027
5028 /* First flush out any partial line written with print. */
5029 message_log_maybe_newline ();
5030 if (STRINGP (m))
5031 message_dolog (XSTRING (m)->data, nbytes, 1, multibyte);
5032 message3_nolog (m, nbytes, multibyte);
5033
5034 UNGCPRO;
5035 }
5036
5037
5038 /* The non-logging version of message3. */
5039
5040 void
5041 message3_nolog (m, nbytes, multibyte)
5042 Lisp_Object m;
5043 int nbytes, multibyte;
5044 {
5045 struct frame *sf = SELECTED_FRAME ();
5046 message_enable_multibyte = multibyte;
5047
5048 if (noninteractive)
5049 {
5050 if (noninteractive_need_newline)
5051 putc ('\n', stderr);
5052 noninteractive_need_newline = 0;
5053 if (STRINGP (m))
5054 fwrite (XSTRING (m)->data, nbytes, 1, stderr);
5055 if (cursor_in_echo_area == 0)
5056 fprintf (stderr, "\n");
5057 fflush (stderr);
5058 }
5059 /* A null message buffer means that the frame hasn't really been
5060 initialized yet. Error messages get reported properly by
5061 cmd_error, so this must be just an informative message; toss it. */
5062 else if (INTERACTIVE
5063 && sf->glyphs_initialized_p
5064 && FRAME_MESSAGE_BUF (sf))
5065 {
5066 Lisp_Object mini_window;
5067 Lisp_Object frame;
5068 struct frame *f;
5069
5070 /* Get the frame containing the mini-buffer
5071 that the selected frame is using. */
5072 mini_window = FRAME_MINIBUF_WINDOW (sf);
5073 frame = XWINDOW (mini_window)->frame;
5074 f = XFRAME (frame);
5075
5076 FRAME_SAMPLE_VISIBILITY (f);
5077 if (FRAME_VISIBLE_P (sf)
5078 && !FRAME_VISIBLE_P (f))
5079 Fmake_frame_visible (frame);
5080
5081 if (STRINGP (m) && XSTRING (m)->size)
5082 {
5083 set_message (NULL, m, nbytes, multibyte);
5084 if (minibuffer_auto_raise)
5085 Fraise_frame (frame);
5086 }
5087 else
5088 clear_message (1, 1);
5089
5090 do_pending_window_change (0);
5091 echo_area_display (1);
5092 do_pending_window_change (0);
5093 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5094 (*frame_up_to_date_hook) (f);
5095 }
5096 }
5097
5098
5099 /* Display a null-terminated echo area message M. If M is 0, clear
5100 out any existing message, and let the mini-buffer text show through.
5101
5102 The buffer M must continue to exist until after the echo area gets
5103 cleared or some other message gets displayed there. Do not pass
5104 text that is stored in a Lisp string. Do not pass text in a buffer
5105 that was alloca'd. */
5106
5107 void
5108 message1 (m)
5109 char *m;
5110 {
5111 message2 (m, (m ? strlen (m) : 0), 0);
5112 }
5113
5114
5115 /* The non-logging counterpart of message1. */
5116
5117 void
5118 message1_nolog (m)
5119 char *m;
5120 {
5121 message2_nolog (m, (m ? strlen (m) : 0), 0);
5122 }
5123
5124 /* Display a message M which contains a single %s
5125 which gets replaced with STRING. */
5126
5127 void
5128 message_with_string (m, string, log)
5129 char *m;
5130 Lisp_Object string;
5131 int log;
5132 {
5133 if (noninteractive)
5134 {
5135 if (m)
5136 {
5137 if (noninteractive_need_newline)
5138 putc ('\n', stderr);
5139 noninteractive_need_newline = 0;
5140 fprintf (stderr, m, XSTRING (string)->data);
5141 if (cursor_in_echo_area == 0)
5142 fprintf (stderr, "\n");
5143 fflush (stderr);
5144 }
5145 }
5146 else if (INTERACTIVE)
5147 {
5148 /* The frame whose minibuffer we're going to display the message on.
5149 It may be larger than the selected frame, so we need
5150 to use its buffer, not the selected frame's buffer. */
5151 Lisp_Object mini_window;
5152 struct frame *f, *sf = SELECTED_FRAME ();
5153
5154 /* Get the frame containing the minibuffer
5155 that the selected frame is using. */
5156 mini_window = FRAME_MINIBUF_WINDOW (sf);
5157 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5158
5159 /* A null message buffer means that the frame hasn't really been
5160 initialized yet. Error messages get reported properly by
5161 cmd_error, so this must be just an informative message; toss it. */
5162 if (FRAME_MESSAGE_BUF (f))
5163 {
5164 int len;
5165 char *a[1];
5166 a[0] = (char *) XSTRING (string)->data;
5167
5168 len = doprnt (FRAME_MESSAGE_BUF (f),
5169 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5170
5171 if (log)
5172 message2 (FRAME_MESSAGE_BUF (f), len,
5173 STRING_MULTIBYTE (string));
5174 else
5175 message2_nolog (FRAME_MESSAGE_BUF (f), len,
5176 STRING_MULTIBYTE (string));
5177
5178 /* Print should start at the beginning of the message
5179 buffer next time. */
5180 message_buf_print = 0;
5181 }
5182 }
5183 }
5184
5185
5186 /* Dump an informative message to the minibuf. If M is 0, clear out
5187 any existing message, and let the mini-buffer text show through. */
5188
5189 /* VARARGS 1 */
5190 void
5191 message (m, a1, a2, a3)
5192 char *m;
5193 EMACS_INT a1, a2, a3;
5194 {
5195 if (noninteractive)
5196 {
5197 if (m)
5198 {
5199 if (noninteractive_need_newline)
5200 putc ('\n', stderr);
5201 noninteractive_need_newline = 0;
5202 fprintf (stderr, m, a1, a2, a3);
5203 if (cursor_in_echo_area == 0)
5204 fprintf (stderr, "\n");
5205 fflush (stderr);
5206 }
5207 }
5208 else if (INTERACTIVE)
5209 {
5210 /* The frame whose mini-buffer we're going to display the message
5211 on. It may be larger than the selected frame, so we need to
5212 use its buffer, not the selected frame's buffer. */
5213 Lisp_Object mini_window;
5214 struct frame *f, *sf = SELECTED_FRAME ();
5215
5216 /* Get the frame containing the mini-buffer
5217 that the selected frame is using. */
5218 mini_window = FRAME_MINIBUF_WINDOW (sf);
5219 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5220
5221 /* A null message buffer means that the frame hasn't really been
5222 initialized yet. Error messages get reported properly by
5223 cmd_error, so this must be just an informative message; toss
5224 it. */
5225 if (FRAME_MESSAGE_BUF (f))
5226 {
5227 if (m)
5228 {
5229 int len;
5230 #ifdef NO_ARG_ARRAY
5231 char *a[3];
5232 a[0] = (char *) a1;
5233 a[1] = (char *) a2;
5234 a[2] = (char *) a3;
5235
5236 len = doprnt (FRAME_MESSAGE_BUF (f),
5237 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5238 #else
5239 len = doprnt (FRAME_MESSAGE_BUF (f),
5240 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
5241 (char **) &a1);
5242 #endif /* NO_ARG_ARRAY */
5243
5244 message2 (FRAME_MESSAGE_BUF (f), len, 0);
5245 }
5246 else
5247 message1 (0);
5248
5249 /* Print should start at the beginning of the message
5250 buffer next time. */
5251 message_buf_print = 0;
5252 }
5253 }
5254 }
5255
5256
5257 /* The non-logging version of message. */
5258
5259 void
5260 message_nolog (m, a1, a2, a3)
5261 char *m;
5262 EMACS_INT a1, a2, a3;
5263 {
5264 Lisp_Object old_log_max;
5265 old_log_max = Vmessage_log_max;
5266 Vmessage_log_max = Qnil;
5267 message (m, a1, a2, a3);
5268 Vmessage_log_max = old_log_max;
5269 }
5270
5271
5272 /* Display the current message in the current mini-buffer. This is
5273 only called from error handlers in process.c, and is not time
5274 critical. */
5275
5276 void
5277 update_echo_area ()
5278 {
5279 if (!NILP (echo_area_buffer[0]))
5280 {
5281 Lisp_Object string;
5282 string = Fcurrent_message ();
5283 message3 (string, XSTRING (string)->size,
5284 !NILP (current_buffer->enable_multibyte_characters));
5285 }
5286 }
5287
5288
5289 /* Make sure echo area buffers in echo_buffers[] are life. If they
5290 aren't, make new ones. */
5291
5292 static void
5293 ensure_echo_area_buffers ()
5294 {
5295 int i;
5296
5297 for (i = 0; i < 2; ++i)
5298 if (!BUFFERP (echo_buffer[i])
5299 || NILP (XBUFFER (echo_buffer[i])->name))
5300 {
5301 char name[30];
5302 sprintf (name, " *Echo Area %d*", i);
5303 echo_buffer[i] = Fget_buffer_create (build_string (name));
5304 }
5305 }
5306
5307
5308 /* Call FN with args A1..A5 with either the current or last displayed
5309 echo_area_buffer as current buffer.
5310
5311 WHICH zero means use the current message buffer
5312 echo_area_buffer[0]. If that is nil, choose a suitable buffer
5313 from echo_buffer[] and clear it.
5314
5315 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
5316 suitable buffer from echo_buffer[] and clear it.
5317
5318 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
5319 that the current message becomes the last displayed one, make
5320 choose a suitable buffer for echo_area_buffer[0], and clear it.
5321
5322 Value is what FN returns. */
5323
5324 static int
5325 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10)
5326 struct window *w;
5327 int which;
5328 int (*fn) ();
5329 int a1, a2, a3, a4, a5, a6, a7, a8, a9, a10;
5330 {
5331 Lisp_Object buffer;
5332 int this_one, the_other, clear_buffer_p, rc;
5333 int count = specpdl_ptr - specpdl;
5334
5335 /* If buffers aren't life, make new ones. */
5336 ensure_echo_area_buffers ();
5337
5338 clear_buffer_p = 0;
5339
5340 if (which == 0)
5341 this_one = 0, the_other = 1;
5342 else if (which > 0)
5343 this_one = 1, the_other = 0;
5344 else
5345 {
5346 this_one = 0, the_other = 1;
5347 clear_buffer_p = 1;
5348
5349 /* We need a fresh one in case the current echo buffer equals
5350 the one containing the last displayed echo area message. */
5351 if (!NILP (echo_area_buffer[this_one])
5352 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
5353 echo_area_buffer[this_one] = Qnil;
5354 }
5355
5356 /* Choose a suitable buffer from echo_buffer[] is we don't
5357 have one. */
5358 if (NILP (echo_area_buffer[this_one]))
5359 {
5360 echo_area_buffer[this_one]
5361 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
5362 ? echo_buffer[the_other]
5363 : echo_buffer[this_one]);
5364 clear_buffer_p = 1;
5365 }
5366
5367 buffer = echo_area_buffer[this_one];
5368
5369 record_unwind_protect (unwind_with_echo_area_buffer,
5370 with_echo_area_buffer_unwind_data (w));
5371
5372 /* Make the echo area buffer current. Note that for display
5373 purposes, it is not necessary that the displayed window's buffer
5374 == current_buffer, except for text property lookup. So, let's
5375 only set that buffer temporarily here without doing a full
5376 Fset_window_buffer. We must also change w->pointm, though,
5377 because otherwise an assertions in unshow_buffer fails, and Emacs
5378 aborts. */
5379 set_buffer_internal_1 (XBUFFER (buffer));
5380 if (w)
5381 {
5382 w->buffer = buffer;
5383 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
5384 }
5385 current_buffer->truncate_lines = Qnil;
5386 current_buffer->undo_list = Qt;
5387 current_buffer->read_only = Qnil;
5388
5389 if (clear_buffer_p && Z > BEG)
5390 del_range (BEG, Z);
5391
5392 xassert (BEGV >= BEG);
5393 xassert (ZV <= Z && ZV >= BEGV);
5394
5395 rc = fn (a1, a2, a3, a4, a5);
5396
5397 xassert (BEGV >= BEG);
5398 xassert (ZV <= Z && ZV >= BEGV);
5399
5400 unbind_to (count, Qnil);
5401 return rc;
5402 }
5403
5404
5405 /* Save state that should be preserved around the call to the function
5406 FN called in with_echo_area_buffer. */
5407
5408 static Lisp_Object
5409 with_echo_area_buffer_unwind_data (w)
5410 struct window *w;
5411 {
5412 int i = 0;
5413 Lisp_Object vector;
5414
5415 /* Reduce consing by keeping one vector in
5416 Vwith_echo_area_save_vector. */
5417 vector = Vwith_echo_area_save_vector;
5418 Vwith_echo_area_save_vector = Qnil;
5419
5420 if (NILP (vector))
5421 vector = Fmake_vector (make_number (7), Qnil);
5422
5423 XSETBUFFER (XVECTOR (vector)->contents[i], current_buffer); ++i;
5424 XVECTOR (vector)->contents[i++] = Vdeactivate_mark;
5425 XVECTOR (vector)->contents[i++] = make_number (windows_or_buffers_changed);
5426
5427 if (w)
5428 {
5429 XSETWINDOW (XVECTOR (vector)->contents[i], w); ++i;
5430 XVECTOR (vector)->contents[i++] = w->buffer;
5431 XVECTOR (vector)->contents[i++]
5432 = make_number (XMARKER (w->pointm)->charpos);
5433 XVECTOR (vector)->contents[i++]
5434 = make_number (XMARKER (w->pointm)->bytepos);
5435 }
5436 else
5437 {
5438 int end = i + 4;
5439 while (i < end)
5440 XVECTOR (vector)->contents[i++] = Qnil;
5441 }
5442
5443 xassert (i == XVECTOR (vector)->size);
5444 return vector;
5445 }
5446
5447
5448 /* Restore global state from VECTOR which was created by
5449 with_echo_area_buffer_unwind_data. */
5450
5451 static Lisp_Object
5452 unwind_with_echo_area_buffer (vector)
5453 Lisp_Object vector;
5454 {
5455 int i = 0;
5456
5457 set_buffer_internal_1 (XBUFFER (XVECTOR (vector)->contents[i])); ++i;
5458 Vdeactivate_mark = XVECTOR (vector)->contents[i]; ++i;
5459 windows_or_buffers_changed = XFASTINT (XVECTOR (vector)->contents[i]); ++i;
5460
5461 if (WINDOWP (XVECTOR (vector)->contents[i]))
5462 {
5463 struct window *w;
5464 Lisp_Object buffer, charpos, bytepos;
5465
5466 w = XWINDOW (XVECTOR (vector)->contents[i]); ++i;
5467 buffer = XVECTOR (vector)->contents[i]; ++i;
5468 charpos = XVECTOR (vector)->contents[i]; ++i;
5469 bytepos = XVECTOR (vector)->contents[i]; ++i;
5470
5471 w->buffer = buffer;
5472 set_marker_both (w->pointm, buffer,
5473 XFASTINT (charpos), XFASTINT (bytepos));
5474 }
5475
5476 Vwith_echo_area_save_vector = vector;
5477 return Qnil;
5478 }
5479
5480
5481 /* Set up the echo area for use by print functions. MULTIBYTE_P
5482 non-zero means we will print multibyte. */
5483
5484 void
5485 setup_echo_area_for_printing (multibyte_p)
5486 int multibyte_p;
5487 {
5488 ensure_echo_area_buffers ();
5489
5490 if (!message_buf_print)
5491 {
5492 /* A message has been output since the last time we printed.
5493 Choose a fresh echo area buffer. */
5494 if (EQ (echo_area_buffer[1], echo_buffer[0]))
5495 echo_area_buffer[0] = echo_buffer[1];
5496 else
5497 echo_area_buffer[0] = echo_buffer[0];
5498
5499 /* Switch to that buffer and clear it. */
5500 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
5501 if (Z > BEG)
5502 del_range (BEG, Z);
5503 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
5504
5505 /* Set up the buffer for the multibyteness we need. */
5506 if (multibyte_p
5507 != !NILP (current_buffer->enable_multibyte_characters))
5508 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
5509
5510 /* Raise the frame containing the echo area. */
5511 if (minibuffer_auto_raise)
5512 {
5513 struct frame *sf = SELECTED_FRAME ();
5514 Lisp_Object mini_window;
5515 mini_window = FRAME_MINIBUF_WINDOW (sf);
5516 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5517 }
5518
5519 message_buf_print = 1;
5520 }
5521 else
5522 {
5523 if (NILP (echo_area_buffer[0]))
5524 {
5525 if (EQ (echo_area_buffer[1], echo_buffer[0]))
5526 echo_area_buffer[0] = echo_buffer[1];
5527 else
5528 echo_area_buffer[0] = echo_buffer[0];
5529 }
5530
5531 if (current_buffer != XBUFFER (echo_area_buffer[0]))
5532 /* Someone switched buffers between print requests. */
5533 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
5534 }
5535 }
5536
5537
5538 /* Display an echo area message in window W. Value is non-zero if W's
5539 height is changed. If display_last_displayed_message_p is
5540 non-zero, display the message that was last displayed, otherwise
5541 display the current message. */
5542
5543 static int
5544 display_echo_area (w)
5545 struct window *w;
5546 {
5547 int i, no_message_p, window_height_changed_p, count;
5548
5549 /* Temporarily disable garbage collections while displaying the echo
5550 area. This is done because a GC can print a message itself.
5551 That message would modify the echo area buffer's contents while a
5552 redisplay of the buffer is going on, and seriously confuse
5553 redisplay. */
5554 count = inhibit_garbage_collection ();
5555
5556 /* If there is no message, we must call display_echo_area_1
5557 nevertheless because it resizes the window. But we will have to
5558 reset the echo_area_buffer in question to nil at the end because
5559 with_echo_area_buffer will sets it to an empty buffer. */
5560 i = display_last_displayed_message_p ? 1 : 0;
5561 no_message_p = NILP (echo_area_buffer[i]);
5562
5563 window_height_changed_p
5564 = with_echo_area_buffer (w, display_last_displayed_message_p,
5565 (int (*) ()) display_echo_area_1, w);
5566
5567 if (no_message_p)
5568 echo_area_buffer[i] = Qnil;
5569
5570 unbind_to (count, Qnil);
5571 return window_height_changed_p;
5572 }
5573
5574
5575 /* Helper for display_echo_area. Display the current buffer which
5576 contains the current echo area message in window W, a mini-window.
5577 Change the height of W so that all of the message is displayed.
5578 Value is non-zero if height of W was changed. */
5579
5580 static int
5581 display_echo_area_1 (w)
5582 struct window *w;
5583 {
5584 Lisp_Object window;
5585 struct text_pos start;
5586 int window_height_changed_p = 0;
5587
5588 /* Do this before displaying, so that we have a large enough glyph
5589 matrix for the display. */
5590 window_height_changed_p = resize_mini_window (w, 0);
5591
5592 /* Display. */
5593 clear_glyph_matrix (w->desired_matrix);
5594 XSETWINDOW (window, w);
5595 SET_TEXT_POS (start, BEG, BEG_BYTE);
5596 try_window (window, start);
5597
5598 return window_height_changed_p;
5599 }
5600
5601
5602 /* Resize the echo area window to exactly the size needed for the
5603 currently displayed message, if there is one. */
5604
5605 void
5606 resize_echo_area_axactly ()
5607 {
5608 if (BUFFERP (echo_area_buffer[0])
5609 && WINDOWP (echo_area_window))
5610 {
5611 struct window *w = XWINDOW (echo_area_window);
5612 int resized_p;
5613
5614 resized_p = with_echo_area_buffer (w, 0,
5615 (int (*) ()) resize_mini_window,
5616 w, 1);
5617 if (resized_p)
5618 {
5619 ++windows_or_buffers_changed;
5620 ++update_mode_lines;
5621 redisplay_internal (0);
5622 }
5623 }
5624 }
5625
5626
5627 /* Resize mini-window W to fit the size of its contents. EXACT:P
5628 means size the window exactly to the size needed. Otherwise, it's
5629 only enlarged until W's buffer is empty. Value is non-zero if
5630 the window height has been changed. */
5631
5632 int
5633 resize_mini_window (w, exact_p)
5634 struct window *w;
5635 int exact_p;
5636 {
5637 struct frame *f = XFRAME (w->frame);
5638 int window_height_changed_p = 0;
5639
5640 xassert (MINI_WINDOW_P (w));
5641
5642 /* Nil means don't try to resize. */
5643 if (NILP (Vmax_mini_window_height)
5644 || (FRAME_X_P (f) && f->output_data.x == NULL))
5645 return 0;
5646
5647 if (!FRAME_MINIBUF_ONLY_P (f))
5648 {
5649 struct it it;
5650 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
5651 int total_height = XFASTINT (root->height) + XFASTINT (w->height);
5652 int height, max_height;
5653 int unit = CANON_Y_UNIT (f);
5654 struct text_pos start;
5655
5656 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
5657
5658 /* Compute the max. number of lines specified by the user. */
5659 if (FLOATP (Vmax_mini_window_height))
5660 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
5661 else if (INTEGERP (Vmax_mini_window_height))
5662 max_height = XINT (Vmax_mini_window_height);
5663 else
5664 max_height = total_height / 4;
5665
5666 /* Correct that max. height if it's bogus. */
5667 max_height = max (1, max_height);
5668 max_height = min (total_height, max_height);
5669
5670 /* Find out the height of the text in the window. */
5671 last_height = 0;
5672 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
5673 if (it.max_ascent == 0 && it.max_descent == 0)
5674 height = it.current_y + last_height;
5675 else
5676 height = it.current_y + it.max_ascent + it.max_descent;
5677 height = (height + unit - 1) / unit;
5678
5679 /* Compute a suitable window start. */
5680 if (height > max_height)
5681 {
5682 height = max_height;
5683 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
5684 move_it_vertically_backward (&it, (height - 1) * unit);
5685 start = it.current.pos;
5686 }
5687 else
5688 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
5689 SET_MARKER_FROM_TEXT_POS (w->start, start);
5690
5691 /* Let it grow only, until we display an empty message, in which
5692 case the window shrinks again. */
5693 if (height > XFASTINT (w->height))
5694 {
5695 int old_height = XFASTINT (w->height);
5696 freeze_window_starts (f, 1);
5697 grow_mini_window (w, height - XFASTINT (w->height));
5698 window_height_changed_p = XFASTINT (w->height) != old_height;
5699 }
5700 else if (height < XFASTINT (w->height)
5701 && (exact_p || BEGV == ZV))
5702 {
5703 int old_height = XFASTINT (w->height);
5704 freeze_window_starts (f, 0);
5705 shrink_mini_window (w);
5706 window_height_changed_p = XFASTINT (w->height) != old_height;
5707 }
5708 }
5709
5710 return window_height_changed_p;
5711 }
5712
5713
5714 /* Value is the current message, a string, or nil if there is no
5715 current message. */
5716
5717 Lisp_Object
5718 current_message ()
5719 {
5720 Lisp_Object msg;
5721
5722 if (NILP (echo_area_buffer[0]))
5723 msg = Qnil;
5724 else
5725 {
5726 with_echo_area_buffer (0, 0, (int (*) ()) current_message_1, &msg);
5727 if (NILP (msg))
5728 echo_area_buffer[0] = Qnil;
5729 }
5730
5731 return msg;
5732 }
5733
5734
5735 static int
5736 current_message_1 (msg)
5737 Lisp_Object *msg;
5738 {
5739 if (Z > BEG)
5740 *msg = make_buffer_string (BEG, Z, 1);
5741 else
5742 *msg = Qnil;
5743 return 0;
5744 }
5745
5746
5747 /* Push the current message on Vmessage_stack for later restauration
5748 by restore_message. Value is non-zero if the current message isn't
5749 empty. This is a relatively infrequent operation, so it's not
5750 worth optimizing. */
5751
5752 int
5753 push_message ()
5754 {
5755 Lisp_Object msg;
5756 msg = current_message ();
5757 Vmessage_stack = Fcons (msg, Vmessage_stack);
5758 return STRINGP (msg);
5759 }
5760
5761
5762 /* Restore message display from the top of Vmessage_stack. */
5763
5764 void
5765 restore_message ()
5766 {
5767 Lisp_Object msg;
5768
5769 xassert (CONSP (Vmessage_stack));
5770 msg = XCAR (Vmessage_stack);
5771 if (STRINGP (msg))
5772 message3_nolog (msg, STRING_BYTES (XSTRING (msg)), STRING_MULTIBYTE (msg));
5773 else
5774 message3_nolog (msg, 0, 0);
5775 }
5776
5777
5778 /* Pop the top-most entry off Vmessage_stack. */
5779
5780 void
5781 pop_message ()
5782 {
5783 xassert (CONSP (Vmessage_stack));
5784 Vmessage_stack = XCDR (Vmessage_stack);
5785 }
5786
5787
5788 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
5789 exits. If the stack is not empty, we have a missing pop_message
5790 somewhere. */
5791
5792 void
5793 check_message_stack ()
5794 {
5795 if (!NILP (Vmessage_stack))
5796 abort ();
5797 }
5798
5799
5800 /* Truncate to NCHARS what will be displayed in the echo area the next
5801 time we display it---but don't redisplay it now. */
5802
5803 void
5804 truncate_echo_area (nchars)
5805 int nchars;
5806 {
5807 if (nchars == 0)
5808 echo_area_buffer[0] = Qnil;
5809 /* A null message buffer means that the frame hasn't really been
5810 initialized yet. Error messages get reported properly by
5811 cmd_error, so this must be just an informative message; toss it. */
5812 else if (!noninteractive
5813 && INTERACTIVE
5814 && !NILP (echo_area_buffer[0]))
5815 {
5816 struct frame *sf = SELECTED_FRAME ();
5817 if (FRAME_MESSAGE_BUF (sf))
5818 with_echo_area_buffer (0, 0, (int (*) ()) truncate_message_1, nchars);
5819 }
5820 }
5821
5822
5823 /* Helper function for truncate_echo_area. Truncate the current
5824 message to at most NCHARS characters. */
5825
5826 static int
5827 truncate_message_1 (nchars)
5828 int nchars;
5829 {
5830 if (BEG + nchars < Z)
5831 del_range (BEG + nchars, Z);
5832 if (Z == BEG)
5833 echo_area_buffer[0] = Qnil;
5834 return 0;
5835 }
5836
5837
5838 /* Set the current message to a substring of S or STRING.
5839
5840 If STRING is a Lisp string, set the message to the first NBYTES
5841 bytes from STRING. NBYTES zero means use the whole string. If
5842 STRING is multibyte, the message will be displayed multibyte.
5843
5844 If S is not null, set the message to the first LEN bytes of S. LEN
5845 zero means use the whole string. MULTIBYTE_P non-zero means S is
5846 multibyte. Display the message multibyte in that case. */
5847
5848 void
5849 set_message (s, string, nbytes, multibyte_p)
5850 char *s;
5851 Lisp_Object string;
5852 int nbytes;
5853 {
5854 message_enable_multibyte
5855 = ((s && multibyte_p)
5856 || (STRINGP (string) && STRING_MULTIBYTE (string)));
5857
5858 with_echo_area_buffer (0, -1, (int (*) ()) set_message_1,
5859 s, string, nbytes, multibyte_p);
5860 message_buf_print = 0;
5861 }
5862
5863
5864 /* Helper function for set_message. Arguments have the same meaning
5865 as there. This function is called with the echo area buffer being
5866 current. */
5867
5868 static int
5869 set_message_1 (s, string, nbytes, multibyte_p)
5870 char *s;
5871 Lisp_Object string;
5872 int nbytes, multibyte_p;
5873 {
5874 xassert (BEG == Z);
5875
5876 /* Change multibyteness of the echo buffer appropriately. */
5877 if (message_enable_multibyte
5878 != !NILP (current_buffer->enable_multibyte_characters))
5879 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
5880
5881 /* Insert new message at BEG. */
5882 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
5883
5884 if (STRINGP (string))
5885 {
5886 int nchars;
5887
5888 if (nbytes == 0)
5889 nbytes = XSTRING (string)->size_byte;
5890 nchars = string_byte_to_char (string, nbytes);
5891
5892 /* This function takes care of single/multibyte conversion. We
5893 just have to ensure that the echo area buffer has the right
5894 setting of enable_multibyte_characters. */
5895 insert_from_string (string, 0, 0, nchars, nbytes, 1);
5896 }
5897 else if (s)
5898 {
5899 if (nbytes == 0)
5900 nbytes = strlen (s);
5901
5902 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
5903 {
5904 /* Convert from multi-byte to single-byte. */
5905 int i, c, n;
5906 unsigned char work[1];
5907
5908 /* Convert a multibyte string to single-byte. */
5909 for (i = 0; i < nbytes; i += n)
5910 {
5911 c = string_char_and_length (s + i, nbytes - i, &n);
5912 work[0] = (SINGLE_BYTE_CHAR_P (c)
5913 ? c
5914 : multibyte_char_to_unibyte (c, Qnil));
5915 insert_1_both (work, 1, 1, 1, 0, 0);
5916 }
5917 }
5918 else if (!multibyte_p
5919 && !NILP (current_buffer->enable_multibyte_characters))
5920 {
5921 /* Convert from single-byte to multi-byte. */
5922 int i, c, n;
5923 unsigned char *msg = (unsigned char *) s;
5924 unsigned char str[MAX_MULTIBYTE_LENGTH];
5925
5926 /* Convert a single-byte string to multibyte. */
5927 for (i = 0; i < nbytes; i++)
5928 {
5929 c = unibyte_char_to_multibyte (msg[i]);
5930 n = CHAR_STRING (c, str);
5931 insert_1_both (str, 1, n, 1, 0, 0);
5932 }
5933 }
5934 else
5935 insert_1 (s, nbytes, 1, 0, 0);
5936 }
5937
5938 return 0;
5939 }
5940
5941
5942 /* Clear messages. CURRENT_P non-zero means clear the current
5943 message. LAST_DISPLAYED_P non-zero means clear the message
5944 last displayed. */
5945
5946 void
5947 clear_message (current_p, last_displayed_p)
5948 int current_p, last_displayed_p;
5949 {
5950 if (current_p)
5951 echo_area_buffer[0] = Qnil;
5952
5953 if (last_displayed_p)
5954 echo_area_buffer[1] = Qnil;
5955
5956 message_buf_print = 0;
5957 }
5958
5959 /* Clear garbaged frames.
5960
5961 This function is used where the old redisplay called
5962 redraw_garbaged_frames which in turn called redraw_frame which in
5963 turn called clear_frame. The call to clear_frame was a source of
5964 flickering. I believe a clear_frame is not necessary. It should
5965 suffice in the new redisplay to invalidate all current matrices,
5966 and ensure a complete redisplay of all windows. */
5967
5968 static void
5969 clear_garbaged_frames ()
5970 {
5971 if (frame_garbaged)
5972 {
5973 Lisp_Object tail, frame;
5974
5975 FOR_EACH_FRAME (tail, frame)
5976 {
5977 struct frame *f = XFRAME (frame);
5978
5979 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
5980 {
5981 clear_current_matrices (f);
5982 f->garbaged = 0;
5983 }
5984 }
5985
5986 frame_garbaged = 0;
5987 ++windows_or_buffers_changed;
5988 }
5989 }
5990
5991
5992 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
5993 is non-zero update selected_frame. Value is non-zero if the
5994 mini-windows height has been changed. */
5995
5996 static int
5997 echo_area_display (update_frame_p)
5998 int update_frame_p;
5999 {
6000 Lisp_Object mini_window;
6001 struct window *w;
6002 struct frame *f;
6003 int window_height_changed_p = 0;
6004 struct frame *sf = SELECTED_FRAME ();
6005
6006 mini_window = FRAME_MINIBUF_WINDOW (sf);
6007 w = XWINDOW (mini_window);
6008 f = XFRAME (WINDOW_FRAME (w));
6009
6010 /* Don't display if frame is invisible or not yet initialized. */
6011 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
6012 return 0;
6013
6014 #ifdef HAVE_WINDOW_SYSTEM
6015 /* When Emacs starts, selected_frame may be a visible terminal
6016 frame, even if we run under a window system. If we let this
6017 through, a message would be displayed on the terminal. */
6018 if (EQ (selected_frame, Vterminal_frame)
6019 && !NILP (Vwindow_system))
6020 return 0;
6021 #endif /* HAVE_WINDOW_SYSTEM */
6022
6023 /* Redraw garbaged frames. */
6024 if (frame_garbaged)
6025 clear_garbaged_frames ();
6026
6027 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
6028 {
6029 echo_area_window = mini_window;
6030 window_height_changed_p = display_echo_area (w);
6031 w->must_be_updated_p = 1;
6032
6033 if (update_frame_p)
6034 {
6035 /* Not called from redisplay_internal. If we changed
6036 window configuration, we must redisplay thoroughly.
6037 Otherwise, we can do with updating what we displayed
6038 above. */
6039 if (window_height_changed_p)
6040 {
6041 ++windows_or_buffers_changed;
6042 ++update_mode_lines;
6043 redisplay_internal (0);
6044 }
6045 else if (FRAME_WINDOW_P (f))
6046 {
6047 update_single_window (w, 1);
6048 rif->flush_display (f);
6049 }
6050 else
6051 update_frame (f, 1, 1);
6052 }
6053 }
6054 else if (!EQ (mini_window, selected_window))
6055 windows_or_buffers_changed++;
6056
6057 /* Last displayed message is now the current message. */
6058 echo_area_buffer[1] = echo_area_buffer[0];
6059
6060 /* Prevent redisplay optimization in redisplay_internal by resetting
6061 this_line_start_pos. This is done because the mini-buffer now
6062 displays the message instead of its buffer text. */
6063 if (EQ (mini_window, selected_window))
6064 CHARPOS (this_line_start_pos) = 0;
6065
6066 return window_height_changed_p;
6067 }
6068
6069
6070 \f
6071 /***********************************************************************
6072 Frame Titles
6073 ***********************************************************************/
6074
6075
6076 #ifdef HAVE_WINDOW_SYSTEM
6077
6078 /* A buffer for constructing frame titles in it; allocated from the
6079 heap in init_xdisp and resized as needed in store_frame_title_char. */
6080
6081 static char *frame_title_buf;
6082
6083 /* The buffer's end, and a current output position in it. */
6084
6085 static char *frame_title_buf_end;
6086 static char *frame_title_ptr;
6087
6088
6089 /* Store a single character C for the frame title in frame_title_buf.
6090 Re-allocate frame_title_buf if necessary. */
6091
6092 static void
6093 store_frame_title_char (c)
6094 char c;
6095 {
6096 /* If output position has reached the end of the allocated buffer,
6097 double the buffer's size. */
6098 if (frame_title_ptr == frame_title_buf_end)
6099 {
6100 int len = frame_title_ptr - frame_title_buf;
6101 int new_size = 2 * len * sizeof *frame_title_buf;
6102 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
6103 frame_title_buf_end = frame_title_buf + new_size;
6104 frame_title_ptr = frame_title_buf + len;
6105 }
6106
6107 *frame_title_ptr++ = c;
6108 }
6109
6110
6111 /* Store part of a frame title in frame_title_buf, beginning at
6112 frame_title_ptr. STR is the string to store. Do not copy more
6113 than PRECISION number of bytes from STR; PRECISION <= 0 means copy
6114 the whole string. Pad with spaces until FIELD_WIDTH number of
6115 characters have been copied; FIELD_WIDTH <= 0 means don't pad.
6116 Called from display_mode_element when it is used to build a frame
6117 title. */
6118
6119 static int
6120 store_frame_title (str, field_width, precision)
6121 unsigned char *str;
6122 int field_width, precision;
6123 {
6124 int n = 0;
6125
6126 /* Copy at most PRECISION chars from STR. */
6127 while ((precision <= 0 || n < precision)
6128 && *str)
6129 {
6130 store_frame_title_char (*str++);
6131 ++n;
6132 }
6133
6134 /* Fill up with spaces until FIELD_WIDTH reached. */
6135 while (field_width > 0
6136 && n < field_width)
6137 {
6138 store_frame_title_char (' ');
6139 ++n;
6140 }
6141
6142 return n;
6143 }
6144
6145
6146 /* Set the title of FRAME, if it has changed. The title format is
6147 Vicon_title_format if FRAME is iconified, otherwise it is
6148 frame_title_format. */
6149
6150 static void
6151 x_consider_frame_title (frame)
6152 Lisp_Object frame;
6153 {
6154 struct frame *f = XFRAME (frame);
6155
6156 if (FRAME_WINDOW_P (f)
6157 || FRAME_MINIBUF_ONLY_P (f)
6158 || f->explicit_name)
6159 {
6160 /* Do we have more than one visible frame on this X display? */
6161 Lisp_Object tail;
6162 Lisp_Object fmt;
6163 struct buffer *obuf;
6164 int len;
6165 struct it it;
6166
6167 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
6168 {
6169 struct frame *tf = XFRAME (XCAR (tail));
6170
6171 if (tf != f
6172 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
6173 && !FRAME_MINIBUF_ONLY_P (tf)
6174 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
6175 break;
6176 }
6177
6178 /* Set global variable indicating that multiple frames exist. */
6179 multiple_frames = CONSP (tail);
6180
6181 /* Switch to the buffer of selected window of the frame. Set up
6182 frame_title_ptr so that display_mode_element will output into it;
6183 then display the title. */
6184 obuf = current_buffer;
6185 Fset_buffer (XWINDOW (f->selected_window)->buffer);
6186 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
6187 frame_title_ptr = frame_title_buf;
6188 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
6189 NULL, DEFAULT_FACE_ID);
6190 len = display_mode_element (&it, 0, -1, -1, fmt);
6191 frame_title_ptr = NULL;
6192 set_buffer_internal (obuf);
6193
6194 /* Set the title only if it's changed. This avoids consing in
6195 the common case where it hasn't. (If it turns out that we've
6196 already wasted too much time by walking through the list with
6197 display_mode_element, then we might need to optimize at a
6198 higher level than this.) */
6199 if (! STRINGP (f->name)
6200 || STRING_BYTES (XSTRING (f->name)) != len
6201 || bcmp (frame_title_buf, XSTRING (f->name)->data, len) != 0)
6202 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
6203 }
6204 }
6205
6206 #else /* not HAVE_WINDOW_SYSTEM */
6207
6208 #define frame_title_ptr ((char *)0)
6209 #define store_frame_title(str, mincol, maxcol) 0
6210
6211 #endif /* not HAVE_WINDOW_SYSTEM */
6212
6213
6214
6215 \f
6216 /***********************************************************************
6217 Menu Bars
6218 ***********************************************************************/
6219
6220
6221 /* Prepare for redisplay by updating menu-bar item lists when
6222 appropriate. This can call eval. */
6223
6224 void
6225 prepare_menu_bars ()
6226 {
6227 int all_windows;
6228 struct gcpro gcpro1, gcpro2;
6229 struct frame *f;
6230 struct frame *tooltip_frame;
6231
6232 #ifdef HAVE_X_WINDOWS
6233 tooltip_frame = tip_frame;
6234 #else
6235 tooltip_frame = NULL;
6236 #endif
6237
6238 /* Update all frame titles based on their buffer names, etc. We do
6239 this before the menu bars so that the buffer-menu will show the
6240 up-to-date frame titles. */
6241 #ifdef HAVE_WINDOW_SYSTEM
6242 if (windows_or_buffers_changed || update_mode_lines)
6243 {
6244 Lisp_Object tail, frame;
6245
6246 FOR_EACH_FRAME (tail, frame)
6247 {
6248 f = XFRAME (frame);
6249 if (f != tooltip_frame
6250 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
6251 x_consider_frame_title (frame);
6252 }
6253 }
6254 #endif /* HAVE_WINDOW_SYSTEM */
6255
6256 /* Update the menu bar item lists, if appropriate. This has to be
6257 done before any actual redisplay or generation of display lines. */
6258 all_windows = (update_mode_lines
6259 || buffer_shared > 1
6260 || windows_or_buffers_changed);
6261 if (all_windows)
6262 {
6263 Lisp_Object tail, frame;
6264 int count = specpdl_ptr - specpdl;
6265
6266 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6267
6268 FOR_EACH_FRAME (tail, frame)
6269 {
6270 f = XFRAME (frame);
6271
6272 /* Ignore tooltip frame. */
6273 if (f == tooltip_frame)
6274 continue;
6275
6276 /* If a window on this frame changed size, report that to
6277 the user and clear the size-change flag. */
6278 if (FRAME_WINDOW_SIZES_CHANGED (f))
6279 {
6280 Lisp_Object functions;
6281
6282 /* Clear flag first in case we get an error below. */
6283 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
6284 functions = Vwindow_size_change_functions;
6285 GCPRO2 (tail, functions);
6286
6287 while (CONSP (functions))
6288 {
6289 call1 (XCAR (functions), frame);
6290 functions = XCDR (functions);
6291 }
6292 UNGCPRO;
6293 }
6294
6295 GCPRO1 (tail);
6296 update_menu_bar (f, 0);
6297 #ifdef HAVE_WINDOW_SYSTEM
6298 update_tool_bar (f, 0);
6299 #endif
6300 UNGCPRO;
6301 }
6302
6303 unbind_to (count, Qnil);
6304 }
6305 else
6306 {
6307 struct frame *sf = SELECTED_FRAME ();
6308 update_menu_bar (sf, 1);
6309 #ifdef HAVE_WINDOW_SYSTEM
6310 update_tool_bar (sf, 1);
6311 #endif
6312 }
6313
6314 /* Motif needs this. See comment in xmenu.c. Turn it off when
6315 pending_menu_activation is not defined. */
6316 #ifdef USE_X_TOOLKIT
6317 pending_menu_activation = 0;
6318 #endif
6319 }
6320
6321
6322 /* Update the menu bar item list for frame F. This has to be done
6323 before we start to fill in any display lines, because it can call
6324 eval.
6325
6326 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
6327
6328 static void
6329 update_menu_bar (f, save_match_data)
6330 struct frame *f;
6331 int save_match_data;
6332 {
6333 Lisp_Object window;
6334 register struct window *w;
6335
6336 window = FRAME_SELECTED_WINDOW (f);
6337 w = XWINDOW (window);
6338
6339 if (update_mode_lines)
6340 w->update_mode_line = Qt;
6341
6342 if (FRAME_WINDOW_P (f)
6343 ?
6344 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
6345 FRAME_EXTERNAL_MENU_BAR (f)
6346 #else
6347 FRAME_MENU_BAR_LINES (f) > 0
6348 #endif
6349 : FRAME_MENU_BAR_LINES (f) > 0)
6350 {
6351 /* If the user has switched buffers or windows, we need to
6352 recompute to reflect the new bindings. But we'll
6353 recompute when update_mode_lines is set too; that means
6354 that people can use force-mode-line-update to request
6355 that the menu bar be recomputed. The adverse effect on
6356 the rest of the redisplay algorithm is about the same as
6357 windows_or_buffers_changed anyway. */
6358 if (windows_or_buffers_changed
6359 || !NILP (w->update_mode_line)
6360 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
6361 < BUF_MODIFF (XBUFFER (w->buffer)))
6362 != !NILP (w->last_had_star))
6363 || ((!NILP (Vtransient_mark_mode)
6364 && !NILP (XBUFFER (w->buffer)->mark_active))
6365 != !NILP (w->region_showing)))
6366 {
6367 struct buffer *prev = current_buffer;
6368 int count = specpdl_ptr - specpdl;
6369
6370 set_buffer_internal_1 (XBUFFER (w->buffer));
6371 if (save_match_data)
6372 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6373 if (NILP (Voverriding_local_map_menu_flag))
6374 {
6375 specbind (Qoverriding_terminal_local_map, Qnil);
6376 specbind (Qoverriding_local_map, Qnil);
6377 }
6378
6379 /* Run the Lucid hook. */
6380 call1 (Vrun_hooks, Qactivate_menubar_hook);
6381
6382 /* If it has changed current-menubar from previous value,
6383 really recompute the menu-bar from the value. */
6384 if (! NILP (Vlucid_menu_bar_dirty_flag))
6385 call0 (Qrecompute_lucid_menubar);
6386
6387 safe_run_hooks (Qmenu_bar_update_hook);
6388 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
6389
6390 /* Redisplay the menu bar in case we changed it. */
6391 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
6392 if (FRAME_WINDOW_P (f))
6393 set_frame_menubar (f, 0, 0);
6394 else
6395 /* On a terminal screen, the menu bar is an ordinary screen
6396 line, and this makes it get updated. */
6397 w->update_mode_line = Qt;
6398 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
6399 /* In the non-toolkit version, the menu bar is an ordinary screen
6400 line, and this makes it get updated. */
6401 w->update_mode_line = Qt;
6402 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
6403
6404 unbind_to (count, Qnil);
6405 set_buffer_internal_1 (prev);
6406 }
6407 }
6408 }
6409
6410
6411 \f
6412 /***********************************************************************
6413 Tool-bars
6414 ***********************************************************************/
6415
6416 #ifdef HAVE_WINDOW_SYSTEM
6417
6418 /* Update the tool-bar item list for frame F. This has to be done
6419 before we start to fill in any display lines. Called from
6420 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
6421 and restore it here. */
6422
6423 static void
6424 update_tool_bar (f, save_match_data)
6425 struct frame *f;
6426 int save_match_data;
6427 {
6428 if (WINDOWP (f->tool_bar_window)
6429 && XFASTINT (XWINDOW (f->tool_bar_window)->height) > 0)
6430 {
6431 Lisp_Object window;
6432 struct window *w;
6433
6434 window = FRAME_SELECTED_WINDOW (f);
6435 w = XWINDOW (window);
6436
6437 /* If the user has switched buffers or windows, we need to
6438 recompute to reflect the new bindings. But we'll
6439 recompute when update_mode_lines is set too; that means
6440 that people can use force-mode-line-update to request
6441 that the menu bar be recomputed. The adverse effect on
6442 the rest of the redisplay algorithm is about the same as
6443 windows_or_buffers_changed anyway. */
6444 if (windows_or_buffers_changed
6445 || !NILP (w->update_mode_line)
6446 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
6447 < BUF_MODIFF (XBUFFER (w->buffer)))
6448 != !NILP (w->last_had_star))
6449 || ((!NILP (Vtransient_mark_mode)
6450 && !NILP (XBUFFER (w->buffer)->mark_active))
6451 != !NILP (w->region_showing)))
6452 {
6453 struct buffer *prev = current_buffer;
6454 int count = specpdl_ptr - specpdl;
6455
6456 /* Set current_buffer to the buffer of the selected
6457 window of the frame, so that we get the right local
6458 keymaps. */
6459 set_buffer_internal_1 (XBUFFER (w->buffer));
6460
6461 /* Save match data, if we must. */
6462 if (save_match_data)
6463 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6464
6465 /* Make sure that we don't accidentally use bogus keymaps. */
6466 if (NILP (Voverriding_local_map_menu_flag))
6467 {
6468 specbind (Qoverriding_terminal_local_map, Qnil);
6469 specbind (Qoverriding_local_map, Qnil);
6470 }
6471
6472 /* Build desired tool-bar items from keymaps. */
6473 f->desired_tool_bar_items
6474 = tool_bar_items (f->desired_tool_bar_items,
6475 &f->n_desired_tool_bar_items);
6476
6477 /* Redisplay the tool-bar in case we changed it. */
6478 w->update_mode_line = Qt;
6479
6480 unbind_to (count, Qnil);
6481 set_buffer_internal_1 (prev);
6482 }
6483 }
6484 }
6485
6486
6487 /* Set F->desired_tool_bar_string to a Lisp string representing frame
6488 F's desired tool-bar contents. F->desired_tool_bar_items must have
6489 been set up previously by calling prepare_menu_bars. */
6490
6491 static void
6492 build_desired_tool_bar_string (f)
6493 struct frame *f;
6494 {
6495 int i, size, size_needed, string_idx;
6496 struct gcpro gcpro1, gcpro2, gcpro3;
6497 Lisp_Object image, plist, props;
6498
6499 image = plist = props = Qnil;
6500 GCPRO3 (image, plist, props);
6501
6502 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
6503 Otherwise, make a new string. */
6504
6505 /* The size of the string we might be able to reuse. */
6506 size = (STRINGP (f->desired_tool_bar_string)
6507 ? XSTRING (f->desired_tool_bar_string)->size
6508 : 0);
6509
6510 /* Each image in the string we build is preceded by a space,
6511 and there is a space at the end. */
6512 size_needed = f->n_desired_tool_bar_items + 1;
6513
6514 /* Reuse f->desired_tool_bar_string, if possible. */
6515 if (size < size_needed)
6516 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
6517 make_number (' '));
6518 else
6519 {
6520 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
6521 Fremove_text_properties (make_number (0), make_number (size),
6522 props, f->desired_tool_bar_string);
6523 }
6524
6525 /* Put a `display' property on the string for the images to display,
6526 put a `menu_item' property on tool-bar items with a value that
6527 is the index of the item in F's tool-bar item vector. */
6528 for (i = 0, string_idx = 0;
6529 i < f->n_desired_tool_bar_items;
6530 ++i, string_idx += 1)
6531 {
6532 #define PROP(IDX) \
6533 (XVECTOR (f->desired_tool_bar_items) \
6534 ->contents[i * TOOL_BAR_ITEM_NSLOTS + (IDX)])
6535
6536 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
6537 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
6538 int margin, relief;
6539 extern Lisp_Object QCrelief, QCmargin, QCalgorithm, Qimage;
6540 extern Lisp_Object Qlaplace;
6541
6542 /* If image is a vector, choose the image according to the
6543 button state. */
6544 image = PROP (TOOL_BAR_ITEM_IMAGES);
6545 if (VECTORP (image))
6546 {
6547 enum tool_bar_item_image idx;
6548
6549 if (enabled_p)
6550 idx = (selected_p
6551 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
6552 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
6553 else
6554 idx = (selected_p
6555 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
6556 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
6557
6558 xassert (XVECTOR (image)->size >= idx);
6559 image = XVECTOR (image)->contents[idx];
6560 }
6561
6562 /* Ignore invalid image specifications. */
6563 if (!valid_image_p (image))
6564 continue;
6565
6566 /* Display the tool-bar button pressed, or depressed. */
6567 plist = Fcopy_sequence (XCDR (image));
6568
6569 /* Compute margin and relief to draw. */
6570 relief = tool_bar_button_relief > 0 ? tool_bar_button_relief : 3;
6571 margin = relief + max (0, tool_bar_button_margin);
6572
6573 if (auto_raise_tool_bar_buttons_p)
6574 {
6575 /* Add a `:relief' property to the image spec if the item is
6576 selected. */
6577 if (selected_p)
6578 {
6579 plist = Fplist_put (plist, QCrelief, make_number (-relief));
6580 margin -= relief;
6581 }
6582 }
6583 else
6584 {
6585 /* If image is selected, display it pressed, i.e. with a
6586 negative relief. If it's not selected, display it with a
6587 raised relief. */
6588 plist = Fplist_put (plist, QCrelief,
6589 (selected_p
6590 ? make_number (-relief)
6591 : make_number (relief)));
6592 margin -= relief;
6593 }
6594
6595 /* Put a margin around the image. */
6596 if (margin)
6597 plist = Fplist_put (plist, QCmargin, make_number (margin));
6598
6599 /* If button is not enabled, make the image appear disabled by
6600 applying an appropriate algorithm to it. */
6601 if (!enabled_p)
6602 plist = Fplist_put (plist, QCalgorithm, Qlaplace);
6603
6604 /* Put a `display' text property on the string for the image to
6605 display. Put a `menu-item' property on the string that gives
6606 the start of this item's properties in the tool-bar items
6607 vector. */
6608 image = Fcons (Qimage, plist);
6609 props = list4 (Qdisplay, image,
6610 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS)),
6611 Fadd_text_properties (make_number (string_idx),
6612 make_number (string_idx + 1),
6613 props, f->desired_tool_bar_string);
6614 #undef PROP
6615 }
6616
6617 UNGCPRO;
6618 }
6619
6620
6621 /* Display one line of the tool-bar of frame IT->f. */
6622
6623 static void
6624 display_tool_bar_line (it)
6625 struct it *it;
6626 {
6627 struct glyph_row *row = it->glyph_row;
6628 int max_x = it->last_visible_x;
6629 struct glyph *last;
6630
6631 prepare_desired_row (row);
6632 row->y = it->current_y;
6633
6634 while (it->current_x < max_x)
6635 {
6636 int x_before, x, n_glyphs_before, i, nglyphs;
6637
6638 /* Get the next display element. */
6639 if (!get_next_display_element (it))
6640 break;
6641
6642 /* Produce glyphs. */
6643 x_before = it->current_x;
6644 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
6645 PRODUCE_GLYPHS (it);
6646
6647 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
6648 i = 0;
6649 x = x_before;
6650 while (i < nglyphs)
6651 {
6652 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
6653
6654 if (x + glyph->pixel_width > max_x)
6655 {
6656 /* Glyph doesn't fit on line. */
6657 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
6658 it->current_x = x;
6659 goto out;
6660 }
6661
6662 ++it->hpos;
6663 x += glyph->pixel_width;
6664 ++i;
6665 }
6666
6667 /* Stop at line ends. */
6668 if (ITERATOR_AT_END_OF_LINE_P (it))
6669 break;
6670
6671 set_iterator_to_next (it);
6672 }
6673
6674 out:;
6675
6676 row->displays_text_p = row->used[TEXT_AREA] != 0;
6677 extend_face_to_end_of_line (it);
6678 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
6679 last->right_box_line_p = 1;
6680 compute_line_metrics (it);
6681
6682 /* If line is empty, make it occupy the rest of the tool-bar. */
6683 if (!row->displays_text_p)
6684 {
6685 row->height = row->phys_height = it->last_visible_y - row->y;
6686 row->ascent = row->phys_ascent = 0;
6687 }
6688
6689 row->full_width_p = 1;
6690 row->continued_p = 0;
6691 row->truncated_on_left_p = 0;
6692 row->truncated_on_right_p = 0;
6693
6694 it->current_x = it->hpos = 0;
6695 it->current_y += row->height;
6696 ++it->vpos;
6697 ++it->glyph_row;
6698 }
6699
6700
6701 /* Value is the number of screen lines needed to make all tool-bar
6702 items of frame F visible. */
6703
6704 static int
6705 tool_bar_lines_needed (f)
6706 struct frame *f;
6707 {
6708 struct window *w = XWINDOW (f->tool_bar_window);
6709 struct it it;
6710
6711 /* Initialize an iterator for iteration over
6712 F->desired_tool_bar_string in the tool-bar window of frame F. */
6713 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
6714 it.first_visible_x = 0;
6715 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
6716 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
6717
6718 while (!ITERATOR_AT_END_P (&it))
6719 {
6720 it.glyph_row = w->desired_matrix->rows;
6721 clear_glyph_row (it.glyph_row);
6722 display_tool_bar_line (&it);
6723 }
6724
6725 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
6726 }
6727
6728
6729 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
6730 height should be changed. */
6731
6732 static int
6733 redisplay_tool_bar (f)
6734 struct frame *f;
6735 {
6736 struct window *w;
6737 struct it it;
6738 struct glyph_row *row;
6739 int change_height_p = 0;
6740
6741 /* If frame hasn't a tool-bar window or if it is zero-height, don't
6742 do anything. This means you must start with tool-bar-lines
6743 non-zero to get the auto-sizing effect. Or in other words, you
6744 can turn off tool-bars by specifying tool-bar-lines zero. */
6745 if (!WINDOWP (f->tool_bar_window)
6746 || (w = XWINDOW (f->tool_bar_window),
6747 XFASTINT (w->height) == 0))
6748 return 0;
6749
6750 /* Set up an iterator for the tool-bar window. */
6751 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
6752 it.first_visible_x = 0;
6753 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
6754 row = it.glyph_row;
6755
6756 /* Build a string that represents the contents of the tool-bar. */
6757 build_desired_tool_bar_string (f);
6758 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
6759
6760 /* Display as many lines as needed to display all tool-bar items. */
6761 while (it.current_y < it.last_visible_y)
6762 display_tool_bar_line (&it);
6763
6764 /* It doesn't make much sense to try scrolling in the tool-bar
6765 window, so don't do it. */
6766 w->desired_matrix->no_scrolling_p = 1;
6767 w->must_be_updated_p = 1;
6768
6769 if (auto_resize_tool_bars_p)
6770 {
6771 int nlines;
6772
6773 /* If there are blank lines at the end, except for a partially
6774 visible blank line at the end that is smaller than
6775 CANON_Y_UNIT, change the tool-bar's height. */
6776 row = it.glyph_row - 1;
6777 if (!row->displays_text_p
6778 && row->height >= CANON_Y_UNIT (f))
6779 change_height_p = 1;
6780
6781 /* If row displays tool-bar items, but is partially visible,
6782 change the tool-bar's height. */
6783 if (row->displays_text_p
6784 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
6785 change_height_p = 1;
6786
6787 /* Resize windows as needed by changing the `tool-bar-lines'
6788 frame parameter. */
6789 if (change_height_p
6790 && (nlines = tool_bar_lines_needed (f),
6791 nlines != XFASTINT (w->height)))
6792 {
6793 extern Lisp_Object Qtool_bar_lines;
6794 Lisp_Object frame;
6795
6796 XSETFRAME (frame, f);
6797 clear_glyph_matrix (w->desired_matrix);
6798 Fmodify_frame_parameters (frame,
6799 Fcons (Fcons (Qtool_bar_lines,
6800 make_number (nlines)),
6801 Qnil));
6802 fonts_changed_p = 1;
6803 }
6804 }
6805
6806 return change_height_p;
6807 }
6808
6809
6810 /* Get information about the tool-bar item which is displayed in GLYPH
6811 on frame F. Return in *PROP_IDX the index where tool-bar item
6812 properties start in F->current_tool_bar_items. Value is zero if
6813 GLYPH doesn't display a tool-bar item. */
6814
6815 int
6816 tool_bar_item_info (f, glyph, prop_idx)
6817 struct frame *f;
6818 struct glyph *glyph;
6819 int *prop_idx;
6820 {
6821 Lisp_Object prop;
6822 int success_p;
6823
6824 /* Get the text property `menu-item' at pos. The value of that
6825 property is the start index of this item's properties in
6826 F->current_tool_bar_items. */
6827 prop = Fget_text_property (make_number (glyph->charpos),
6828 Qmenu_item, f->current_tool_bar_string);
6829 if (INTEGERP (prop))
6830 {
6831 *prop_idx = XINT (prop);
6832 success_p = 1;
6833 }
6834 else
6835 success_p = 0;
6836
6837 return success_p;
6838 }
6839
6840 #endif /* HAVE_WINDOW_SYSTEM */
6841
6842
6843 \f
6844 /************************************************************************
6845 Horizontal scrolling
6846 ************************************************************************/
6847
6848 static int hscroll_window_tree P_ ((Lisp_Object));
6849 static int hscroll_windows P_ ((Lisp_Object));
6850
6851 /* For all leaf windows in the window tree rooted at WINDOW, set their
6852 hscroll value so that PT is (i) visible in the window, and (ii) so
6853 that it is not within a certain margin at the window's left and
6854 right border. Value is non-zero if any window's hscroll has been
6855 changed. */
6856
6857 static int
6858 hscroll_window_tree (window)
6859 Lisp_Object window;
6860 {
6861 int hscrolled_p = 0;
6862
6863 while (WINDOWP (window))
6864 {
6865 struct window *w = XWINDOW (window);
6866
6867 if (WINDOWP (w->hchild))
6868 hscrolled_p |= hscroll_window_tree (w->hchild);
6869 else if (WINDOWP (w->vchild))
6870 hscrolled_p |= hscroll_window_tree (w->vchild);
6871 else if (w->cursor.vpos >= 0)
6872 {
6873 int hscroll_margin, text_area_x, text_area_y;
6874 int text_area_width, text_area_height;
6875 struct glyph_row *current_cursor_row
6876 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
6877 struct glyph_row *desired_cursor_row
6878 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
6879 struct glyph_row *cursor_row
6880 = (desired_cursor_row->enabled_p
6881 ? desired_cursor_row
6882 : current_cursor_row);
6883
6884 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
6885 &text_area_width, &text_area_height);
6886
6887 /* Scroll when cursor is inside this scroll margin. */
6888 hscroll_margin = 5 * CANON_X_UNIT (XFRAME (w->frame));
6889
6890 if ((XFASTINT (w->hscroll)
6891 && w->cursor.x < hscroll_margin)
6892 || (cursor_row->enabled_p
6893 && cursor_row->truncated_on_right_p
6894 && (w->cursor.x > text_area_width - hscroll_margin)))
6895 {
6896 struct it it;
6897 int hscroll;
6898 struct buffer *saved_current_buffer;
6899 int pt;
6900
6901 /* Find point in a display of infinite width. */
6902 saved_current_buffer = current_buffer;
6903 current_buffer = XBUFFER (w->buffer);
6904
6905 if (w == XWINDOW (selected_window))
6906 pt = BUF_PT (current_buffer);
6907 else
6908 {
6909 pt = marker_position (w->pointm);
6910 pt = max (BEGV, pt);
6911 pt = min (ZV, pt);
6912 }
6913
6914 /* Move iterator to pt starting at cursor_row->start in
6915 a line with infinite width. */
6916 init_to_row_start (&it, w, cursor_row);
6917 it.last_visible_x = INFINITY;
6918 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
6919 current_buffer = saved_current_buffer;
6920
6921 /* Center cursor in window. */
6922 hscroll = (max (0, it.current_x - text_area_width / 2)
6923 / CANON_X_UNIT (it.f));
6924
6925 /* Don't call Fset_window_hscroll if value hasn't
6926 changed because it will prevent redisplay
6927 optimizations. */
6928 if (XFASTINT (w->hscroll) != hscroll)
6929 {
6930 Fset_window_hscroll (window, make_number (hscroll));
6931 hscrolled_p = 1;
6932 }
6933 }
6934 }
6935
6936 window = w->next;
6937 }
6938
6939 /* Value is non-zero if hscroll of any leaf window has been changed. */
6940 return hscrolled_p;
6941 }
6942
6943
6944 /* Set hscroll so that cursor is visible and not inside horizontal
6945 scroll margins for all windows in the tree rooted at WINDOW. See
6946 also hscroll_window_tree above. Value is non-zero if any window's
6947 hscroll has been changed. If it has, desired matrices on the frame
6948 of WINDOW are cleared. */
6949
6950 static int
6951 hscroll_windows (window)
6952 Lisp_Object window;
6953 {
6954 int hscrolled_p;
6955
6956 if (automatic_hscrolling_p)
6957 {
6958 hscrolled_p = hscroll_window_tree (window);
6959 if (hscrolled_p)
6960 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
6961 }
6962 else
6963 hscrolled_p = 0;
6964 return hscrolled_p;
6965 }
6966
6967
6968 \f
6969 /************************************************************************
6970 Redisplay
6971 ************************************************************************/
6972
6973 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
6974 to a non-zero value. This is sometimes handy to have in a debugger
6975 session. */
6976
6977 #if GLYPH_DEBUG
6978
6979 /* First and last unchanged row for try_window_id. */
6980
6981 int debug_first_unchanged_at_end_vpos;
6982 int debug_last_unchanged_at_beg_vpos;
6983
6984 /* Delta vpos and y. */
6985
6986 int debug_dvpos, debug_dy;
6987
6988 /* Delta in characters and bytes for try_window_id. */
6989
6990 int debug_delta, debug_delta_bytes;
6991
6992 /* Values of window_end_pos and window_end_vpos at the end of
6993 try_window_id. */
6994
6995 int debug_end_pos, debug_end_vpos;
6996
6997 /* Append a string to W->desired_matrix->method. FMT is a printf
6998 format string. A1...A9 are a supplement for a variable-length
6999 argument list. If trace_redisplay_p is non-zero also printf the
7000 resulting string to stderr. */
7001
7002 static void
7003 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
7004 struct window *w;
7005 char *fmt;
7006 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
7007 {
7008 char buffer[512];
7009 char *method = w->desired_matrix->method;
7010 int len = strlen (method);
7011 int size = sizeof w->desired_matrix->method;
7012 int remaining = size - len - 1;
7013
7014 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
7015 if (len && remaining)
7016 {
7017 method[len] = '|';
7018 --remaining, ++len;
7019 }
7020
7021 strncpy (method + len, buffer, remaining);
7022
7023 if (trace_redisplay_p)
7024 fprintf (stderr, "%p (%s): %s\n",
7025 w,
7026 ((BUFFERP (w->buffer)
7027 && STRINGP (XBUFFER (w->buffer)->name))
7028 ? (char *) XSTRING (XBUFFER (w->buffer)->name)->data
7029 : "no buffer"),
7030 buffer);
7031 }
7032
7033 #endif /* GLYPH_DEBUG */
7034
7035
7036 /* This counter is used to clear the face cache every once in a while
7037 in redisplay_internal. It is incremented for each redisplay.
7038 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
7039 cleared. */
7040
7041 #define CLEAR_FACE_CACHE_COUNT 10000
7042 static int clear_face_cache_count;
7043
7044 /* Record the previous terminal frame we displayed. */
7045
7046 static struct frame *previous_terminal_frame;
7047
7048 /* Non-zero while redisplay_internal is in progress. */
7049
7050 int redisplaying_p;
7051
7052
7053 /* Value is non-zero if all changes in window W, which displays
7054 current_buffer, are in the text between START and END. START is a
7055 buffer position, END is given as a distance from Z. Used in
7056 redisplay_internal for display optimization. */
7057
7058 static INLINE int
7059 text_outside_line_unchanged_p (w, start, end)
7060 struct window *w;
7061 int start, end;
7062 {
7063 int unchanged_p = 1;
7064
7065 /* If text or overlays have changed, see where. */
7066 if (XFASTINT (w->last_modified) < MODIFF
7067 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
7068 {
7069 /* Gap in the line? */
7070 if (GPT < start || Z - GPT < end)
7071 unchanged_p = 0;
7072
7073 /* Changes start in front of the line, or end after it? */
7074 if (unchanged_p
7075 && (BEG_UNCHANGED < start - 1
7076 || END_UNCHANGED < end))
7077 unchanged_p = 0;
7078
7079 /* If selective display, can't optimize if changes start at the
7080 beginning of the line. */
7081 if (unchanged_p
7082 && INTEGERP (current_buffer->selective_display)
7083 && XINT (current_buffer->selective_display) > 0
7084 && (BEG_UNCHANGED < start || GPT <= start))
7085 unchanged_p = 0;
7086 }
7087
7088 return unchanged_p;
7089 }
7090
7091
7092 /* Do a frame update, taking possible shortcuts into account. This is
7093 the main external entry point for redisplay.
7094
7095 If the last redisplay displayed an echo area message and that message
7096 is no longer requested, we clear the echo area or bring back the
7097 mini-buffer if that is in use. */
7098
7099 void
7100 redisplay ()
7101 {
7102 redisplay_internal (0);
7103 }
7104
7105 /* Return 1 if point moved out of or into a composition. Otherwise
7106 return 0. PREV_BUF and PREV_PT are the last point buffer and
7107 position. BUF and PT are the current point buffer and position. */
7108
7109 int
7110 check_point_in_composition (prev_buf, prev_pt, buf, pt)
7111 struct buffer *prev_buf, *buf;
7112 int prev_pt, pt;
7113 {
7114 int start, end;
7115 Lisp_Object prop;
7116 Lisp_Object buffer;
7117
7118 XSETBUFFER (buffer, buf);
7119 /* Check a composition at the last point if point moved within the
7120 same buffer. */
7121 if (prev_buf == buf)
7122 {
7123 if (prev_pt == pt)
7124 /* Point didn't move. */
7125 return 0;
7126
7127 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
7128 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
7129 && COMPOSITION_VALID_P (start, end, prop)
7130 && start < prev_pt && end > prev_pt)
7131 /* The last point was within the composition. Return 1 iff
7132 point moved out of the composition. */
7133 return (pt <= start || pt >= end);
7134 }
7135
7136 /* Check a composition at the current point. */
7137 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
7138 && find_composition (pt, -1, &start, &end, &prop, buffer)
7139 && COMPOSITION_VALID_P (start, end, prop)
7140 && start < pt && end > pt);
7141 }
7142
7143 /* Reconsider the setting of B->clip_changed which is displayed
7144 in window W. */
7145
7146 static INLINE void
7147 reconsider_clip_changes (w, b)
7148 struct window *w;
7149 struct buffer *b;
7150 {
7151 if (b->prevent_redisplay_optimizations_p)
7152 b->clip_changed = 1;
7153 else if (b->clip_changed
7154 && !NILP (w->window_end_valid)
7155 && w->current_matrix->buffer == b
7156 && w->current_matrix->zv == BUF_ZV (b)
7157 && w->current_matrix->begv == BUF_BEGV (b))
7158 b->clip_changed = 0;
7159
7160 /* If display wasn't paused, and W is not a tool bar window, see if
7161 point has been moved into or out of a composition. In that case,
7162 we set b->clip_changed to 1 to force updating the screen. If
7163 b->clip_changed has already been set to 1, we can skip this
7164 check. */
7165 if (!b->clip_changed
7166 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
7167 {
7168 int pt;
7169
7170 if (w == XWINDOW (selected_window))
7171 pt = BUF_PT (current_buffer);
7172 else
7173 pt = marker_position (w->pointm);
7174
7175 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
7176 || pt != XINT (w->last_point))
7177 && check_point_in_composition (w->current_matrix->buffer,
7178 XINT (w->last_point),
7179 XBUFFER (w->buffer), pt))
7180 b->clip_changed = 1;
7181 }
7182 }
7183
7184
7185 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
7186 response to any user action; therefore, we should preserve the echo
7187 area. (Actually, our caller does that job.) Perhaps in the future
7188 avoid recentering windows if it is not necessary; currently that
7189 causes some problems. */
7190
7191 static void
7192 redisplay_internal (preserve_echo_area)
7193 int preserve_echo_area;
7194 {
7195 struct window *w = XWINDOW (selected_window);
7196 struct frame *f = XFRAME (w->frame);
7197 int pause;
7198 int must_finish = 0;
7199 struct text_pos tlbufpos, tlendpos;
7200 int number_of_visible_frames;
7201 int count;
7202 struct frame *sf = SELECTED_FRAME ();
7203
7204 /* Non-zero means redisplay has to consider all windows on all
7205 frames. Zero means, only selected_window is considered. */
7206 int consider_all_windows_p;
7207
7208 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
7209
7210 /* No redisplay if running in batch mode or frame is not yet fully
7211 initialized, or redisplay is explicitly turned off by setting
7212 Vinhibit_redisplay. */
7213 if (noninteractive
7214 || !NILP (Vinhibit_redisplay)
7215 || !f->glyphs_initialized_p)
7216 return;
7217
7218 /* The flag redisplay_performed_directly_p is set by
7219 direct_output_for_insert when it already did the whole screen
7220 update necessary. */
7221 if (redisplay_performed_directly_p)
7222 {
7223 redisplay_performed_directly_p = 0;
7224 if (!hscroll_windows (selected_window))
7225 return;
7226 }
7227
7228 #ifdef USE_X_TOOLKIT
7229 if (popup_activated ())
7230 return;
7231 #endif
7232
7233 /* I don't think this happens but let's be paranoid. */
7234 if (redisplaying_p)
7235 return;
7236
7237 /* Record a function that resets redisplaying_p to its old value
7238 when we leave this function. */
7239 count = specpdl_ptr - specpdl;
7240 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
7241 ++redisplaying_p;
7242
7243 retry:
7244
7245 reconsider_clip_changes (w, current_buffer);
7246
7247 /* If new fonts have been loaded that make a glyph matrix adjustment
7248 necessary, do it. */
7249 if (fonts_changed_p)
7250 {
7251 adjust_glyphs (NULL);
7252 ++windows_or_buffers_changed;
7253 fonts_changed_p = 0;
7254 }
7255
7256 if (! FRAME_WINDOW_P (sf)
7257 && previous_terminal_frame != sf)
7258 {
7259 /* Since frames on an ASCII terminal share the same display
7260 area, displaying a different frame means redisplay the whole
7261 thing. */
7262 windows_or_buffers_changed++;
7263 SET_FRAME_GARBAGED (sf);
7264 XSETFRAME (Vterminal_frame, sf);
7265 }
7266 previous_terminal_frame = sf;
7267
7268 /* Set the visible flags for all frames. Do this before checking
7269 for resized or garbaged frames; they want to know if their frames
7270 are visible. See the comment in frame.h for
7271 FRAME_SAMPLE_VISIBILITY. */
7272 {
7273 Lisp_Object tail, frame;
7274
7275 number_of_visible_frames = 0;
7276
7277 FOR_EACH_FRAME (tail, frame)
7278 {
7279 struct frame *f = XFRAME (frame);
7280
7281 FRAME_SAMPLE_VISIBILITY (f);
7282 if (FRAME_VISIBLE_P (f))
7283 ++number_of_visible_frames;
7284 clear_desired_matrices (f);
7285 }
7286 }
7287
7288 /* Notice any pending interrupt request to change frame size. */
7289 do_pending_window_change (1);
7290
7291 /* Clear frames marked as garbaged. */
7292 if (frame_garbaged)
7293 clear_garbaged_frames ();
7294
7295 /* Build menubar and tool-bar items. */
7296 prepare_menu_bars ();
7297
7298 if (windows_or_buffers_changed)
7299 update_mode_lines++;
7300
7301 /* Detect case that we need to write or remove a star in the mode line. */
7302 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
7303 {
7304 w->update_mode_line = Qt;
7305 if (buffer_shared > 1)
7306 update_mode_lines++;
7307 }
7308
7309 /* If %c is in the mode line, update it if needed. */
7310 if (!NILP (w->column_number_displayed)
7311 /* This alternative quickly identifies a common case
7312 where no change is needed. */
7313 && !(PT == XFASTINT (w->last_point)
7314 && XFASTINT (w->last_modified) >= MODIFF
7315 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
7316 && XFASTINT (w->column_number_displayed) != current_column ())
7317 w->update_mode_line = Qt;
7318
7319 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
7320
7321 /* The variable buffer_shared is set in redisplay_window and
7322 indicates that we redisplay a buffer in different windows. See
7323 there. */
7324 consider_all_windows_p = update_mode_lines || buffer_shared > 1;
7325
7326 /* If specs for an arrow have changed, do thorough redisplay
7327 to ensure we remove any arrow that should no longer exist. */
7328 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
7329 || ! EQ (Voverlay_arrow_string, last_arrow_string))
7330 consider_all_windows_p = windows_or_buffers_changed = 1;
7331
7332 /* Normally the message* functions will have already displayed and
7333 updated the echo area, but the frame may have been trashed, or
7334 the update may have been preempted, so display the echo area
7335 again here. Checking both message buffers captures the case that
7336 the echo area should be cleared. */
7337 if (!NILP (echo_area_buffer[0]) || !NILP (echo_area_buffer[1]))
7338 {
7339 int window_height_changed_p = echo_area_display (0);
7340 must_finish = 1;
7341
7342 if (fonts_changed_p)
7343 goto retry;
7344 else if (window_height_changed_p)
7345 {
7346 consider_all_windows_p = 1;
7347 ++update_mode_lines;
7348 ++windows_or_buffers_changed;
7349
7350 /* If window configuration was changed, frames may have been
7351 marked garbaged. Clear them or we will experience
7352 surprises wrt scrolling. */
7353 if (frame_garbaged)
7354 clear_garbaged_frames ();
7355 }
7356 }
7357 else if (w == XWINDOW (minibuf_window)
7358 && (current_buffer->clip_changed
7359 || XFASTINT (w->last_modified) < MODIFF
7360 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
7361 && resize_mini_window (w, 0))
7362 {
7363 /* Resized active mini-window to fit the size of what it is
7364 showing if its contents might have changed. */
7365 must_finish = 1;
7366 consider_all_windows_p = 1;
7367 ++windows_or_buffers_changed;
7368 ++update_mode_lines;
7369
7370 /* If window configuration was changed, frames may have been
7371 marked garbaged. Clear them or we will experience
7372 surprises wrt scrolling. */
7373 if (frame_garbaged)
7374 clear_garbaged_frames ();
7375 }
7376
7377
7378 /* If showing the region, and mark has changed, we must redisplay
7379 the whole window. The assignment to this_line_start_pos prevents
7380 the optimization directly below this if-statement. */
7381 if (((!NILP (Vtransient_mark_mode)
7382 && !NILP (XBUFFER (w->buffer)->mark_active))
7383 != !NILP (w->region_showing))
7384 || (!NILP (w->region_showing)
7385 && !EQ (w->region_showing,
7386 Fmarker_position (XBUFFER (w->buffer)->mark))))
7387 CHARPOS (this_line_start_pos) = 0;
7388
7389 /* Optimize the case that only the line containing the cursor in the
7390 selected window has changed. Variables starting with this_ are
7391 set in display_line and record information about the line
7392 containing the cursor. */
7393 tlbufpos = this_line_start_pos;
7394 tlendpos = this_line_end_pos;
7395 if (!consider_all_windows_p
7396 && CHARPOS (tlbufpos) > 0
7397 && NILP (w->update_mode_line)
7398 && !current_buffer->clip_changed
7399 && FRAME_VISIBLE_P (XFRAME (w->frame))
7400 && !FRAME_OBSCURED_P (XFRAME (w->frame))
7401 /* Make sure recorded data applies to current buffer, etc. */
7402 && this_line_buffer == current_buffer
7403 && current_buffer == XBUFFER (w->buffer)
7404 && NILP (w->force_start)
7405 /* Point must be on the line that we have info recorded about. */
7406 && PT >= CHARPOS (tlbufpos)
7407 && PT <= Z - CHARPOS (tlendpos)
7408 /* All text outside that line, including its final newline,
7409 must be unchanged */
7410 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
7411 CHARPOS (tlendpos)))
7412 {
7413 if (CHARPOS (tlbufpos) > BEGV
7414 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
7415 && (CHARPOS (tlbufpos) == ZV
7416 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
7417 /* Former continuation line has disappeared by becoming empty */
7418 goto cancel;
7419 else if (XFASTINT (w->last_modified) < MODIFF
7420 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
7421 || MINI_WINDOW_P (w))
7422 {
7423 /* We have to handle the case of continuation around a
7424 wide-column character (See the comment in indent.c around
7425 line 885).
7426
7427 For instance, in the following case:
7428
7429 -------- Insert --------
7430 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
7431 J_I_ ==> J_I_ `^^' are cursors.
7432 ^^ ^^
7433 -------- --------
7434
7435 As we have to redraw the line above, we should goto cancel. */
7436
7437 struct it it;
7438 int line_height_before = this_line_pixel_height;
7439
7440 /* Note that start_display will handle the case that the
7441 line starting at tlbufpos is a continuation lines. */
7442 start_display (&it, w, tlbufpos);
7443
7444 /* Implementation note: It this still necessary? */
7445 if (it.current_x != this_line_start_x)
7446 goto cancel;
7447
7448 TRACE ((stderr, "trying display optimization 1\n"));
7449 w->cursor.vpos = -1;
7450 overlay_arrow_seen = 0;
7451 it.vpos = this_line_vpos;
7452 it.current_y = this_line_y;
7453 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
7454 display_line (&it);
7455
7456 /* If line contains point, is not continued,
7457 and ends at same distance from eob as before, we win */
7458 if (w->cursor.vpos >= 0
7459 /* Line is not continued, otherwise this_line_start_pos
7460 would have been set to 0 in display_line. */
7461 && CHARPOS (this_line_start_pos)
7462 /* Line ends as before. */
7463 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
7464 /* Line has same height as before. Otherwise other lines
7465 would have to be shifted up or down. */
7466 && this_line_pixel_height == line_height_before)
7467 {
7468 /* If this is not the window's last line, we must adjust
7469 the charstarts of the lines below. */
7470 if (it.current_y < it.last_visible_y)
7471 {
7472 struct glyph_row *row
7473 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
7474 int delta, delta_bytes;
7475
7476 if (Z - CHARPOS (tlendpos) == ZV)
7477 {
7478 /* This line ends at end of (accessible part of)
7479 buffer. There is no newline to count. */
7480 delta = (Z
7481 - CHARPOS (tlendpos)
7482 - MATRIX_ROW_START_CHARPOS (row));
7483 delta_bytes = (Z_BYTE
7484 - BYTEPOS (tlendpos)
7485 - MATRIX_ROW_START_BYTEPOS (row));
7486 }
7487 else
7488 {
7489 /* This line ends in a newline. Must take
7490 account of the newline and the rest of the
7491 text that follows. */
7492 delta = (Z
7493 - CHARPOS (tlendpos)
7494 - MATRIX_ROW_START_CHARPOS (row));
7495 delta_bytes = (Z_BYTE
7496 - BYTEPOS (tlendpos)
7497 - MATRIX_ROW_START_BYTEPOS (row));
7498 }
7499
7500 increment_matrix_positions (w->current_matrix,
7501 this_line_vpos + 1,
7502 w->current_matrix->nrows,
7503 delta, delta_bytes);
7504 }
7505
7506 /* If this row displays text now but previously didn't,
7507 or vice versa, w->window_end_vpos may have to be
7508 adjusted. */
7509 if ((it.glyph_row - 1)->displays_text_p)
7510 {
7511 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
7512 XSETINT (w->window_end_vpos, this_line_vpos);
7513 }
7514 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
7515 && this_line_vpos > 0)
7516 XSETINT (w->window_end_vpos, this_line_vpos - 1);
7517 w->window_end_valid = Qnil;
7518
7519 /* Update hint: No need to try to scroll in update_window. */
7520 w->desired_matrix->no_scrolling_p = 1;
7521
7522 #if GLYPH_DEBUG
7523 *w->desired_matrix->method = 0;
7524 debug_method_add (w, "optimization 1");
7525 #endif
7526 goto update;
7527 }
7528 else
7529 goto cancel;
7530 }
7531 else if (/* Cursor position hasn't changed. */
7532 PT == XFASTINT (w->last_point)
7533 /* Make sure the cursor was last displayed
7534 in this window. Otherwise we have to reposition it. */
7535 && 0 <= w->cursor.vpos
7536 && XINT (w->height) > w->cursor.vpos)
7537 {
7538 if (!must_finish)
7539 {
7540 do_pending_window_change (1);
7541
7542 /* We used to always goto end_of_redisplay here, but this
7543 isn't enough if we have a blinking cursor. */
7544 if (w->cursor_off_p == w->last_cursor_off_p)
7545 goto end_of_redisplay;
7546 }
7547 goto update;
7548 }
7549 /* If highlighting the region, or if the cursor is in the echo area,
7550 then we can't just move the cursor. */
7551 else if (! (!NILP (Vtransient_mark_mode)
7552 && !NILP (current_buffer->mark_active))
7553 && (w == XWINDOW (current_buffer->last_selected_window)
7554 || highlight_nonselected_windows)
7555 && NILP (w->region_showing)
7556 && NILP (Vshow_trailing_whitespace)
7557 && !cursor_in_echo_area)
7558 {
7559 struct it it;
7560 struct glyph_row *row;
7561
7562 /* Skip from tlbufpos to PT and see where it is. Note that
7563 PT may be in invisible text. If so, we will end at the
7564 next visible position. */
7565 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
7566 NULL, DEFAULT_FACE_ID);
7567 it.current_x = this_line_start_x;
7568 it.current_y = this_line_y;
7569 it.vpos = this_line_vpos;
7570
7571 /* The call to move_it_to stops in front of PT, but
7572 moves over before-strings. */
7573 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
7574
7575 if (it.vpos == this_line_vpos
7576 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
7577 row->enabled_p))
7578 {
7579 xassert (this_line_vpos == it.vpos);
7580 xassert (this_line_y == it.current_y);
7581 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
7582 goto update;
7583 }
7584 else
7585 goto cancel;
7586 }
7587
7588 cancel:
7589 /* Text changed drastically or point moved off of line. */
7590 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
7591 }
7592
7593 CHARPOS (this_line_start_pos) = 0;
7594 consider_all_windows_p |= buffer_shared > 1;
7595 ++clear_face_cache_count;
7596
7597
7598 /* Build desired matrices. If consider_all_windows_p is non-zero,
7599 do it for all windows on all frames. Otherwise do it for
7600 selected_window, only. */
7601
7602 if (consider_all_windows_p)
7603 {
7604 Lisp_Object tail, frame;
7605
7606 /* Clear the face cache eventually. */
7607 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
7608 {
7609 clear_face_cache (0);
7610 clear_face_cache_count = 0;
7611 }
7612
7613 /* Recompute # windows showing selected buffer. This will be
7614 incremented each time such a window is displayed. */
7615 buffer_shared = 0;
7616
7617 FOR_EACH_FRAME (tail, frame)
7618 {
7619 struct frame *f = XFRAME (frame);
7620 if (FRAME_WINDOW_P (f) || f == sf)
7621 {
7622 /* Mark all the scroll bars to be removed; we'll redeem
7623 the ones we want when we redisplay their windows. */
7624 if (condemn_scroll_bars_hook)
7625 (*condemn_scroll_bars_hook) (f);
7626
7627 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
7628 redisplay_windows (FRAME_ROOT_WINDOW (f));
7629
7630 /* Any scroll bars which redisplay_windows should have
7631 nuked should now go away. */
7632 if (judge_scroll_bars_hook)
7633 (*judge_scroll_bars_hook) (f);
7634 }
7635 }
7636 }
7637 else if (FRAME_VISIBLE_P (sf)
7638 && !FRAME_OBSCURED_P (sf))
7639 redisplay_window (selected_window, 1);
7640
7641
7642 /* Compare desired and current matrices, perform output. */
7643
7644 update:
7645
7646 /* If fonts changed, display again. */
7647 if (fonts_changed_p)
7648 goto retry;
7649
7650 /* Prevent various kinds of signals during display update.
7651 stdio is not robust about handling signals,
7652 which can cause an apparent I/O error. */
7653 if (interrupt_input)
7654 unrequest_sigio ();
7655 stop_polling ();
7656
7657 if (consider_all_windows_p)
7658 {
7659 Lisp_Object tail;
7660 struct frame *f;
7661 int hscrolled_p;
7662
7663 pause = 0;
7664 hscrolled_p = 0;
7665
7666 /* See if we have to hscroll. */
7667 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
7668 if (FRAMEP (XCAR (tail)))
7669 {
7670 f = XFRAME (XCAR (tail));
7671
7672 if ((FRAME_WINDOW_P (f)
7673 || f == sf)
7674 && FRAME_VISIBLE_P (f)
7675 && !FRAME_OBSCURED_P (f)
7676 && hscroll_windows (f->root_window))
7677 hscrolled_p = 1;
7678 }
7679
7680 if (hscrolled_p)
7681 goto retry;
7682
7683 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
7684 {
7685 if (!FRAMEP (XCAR (tail)))
7686 continue;
7687
7688 f = XFRAME (XCAR (tail));
7689
7690 if ((FRAME_WINDOW_P (f) || f == sf)
7691 && FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
7692 {
7693 /* Mark all windows as to be updated. */
7694 set_window_update_flags (XWINDOW (f->root_window), 1);
7695 pause |= update_frame (f, 0, 0);
7696 if (!pause)
7697 {
7698 mark_window_display_accurate (f->root_window, 1);
7699 if (frame_up_to_date_hook != 0)
7700 (*frame_up_to_date_hook) (f);
7701 }
7702 }
7703 }
7704 }
7705 else
7706 {
7707 if (FRAME_VISIBLE_P (sf)
7708 && !FRAME_OBSCURED_P (sf))
7709 {
7710 if (hscroll_windows (selected_window))
7711 goto retry;
7712
7713 XWINDOW (selected_window)->must_be_updated_p = 1;
7714 pause = update_frame (sf, 0, 0);
7715 }
7716 else
7717 pause = 0;
7718
7719 /* We may have called echo_area_display at the top of this
7720 function. If the echo area is on another frame, that may
7721 have put text on a frame other than the selected one, so the
7722 above call to update_frame would not have caught it. Catch
7723 it here. */
7724 {
7725 Lisp_Object mini_window;
7726 struct frame *mini_frame;
7727
7728 mini_window = FRAME_MINIBUF_WINDOW (sf);
7729 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7730
7731 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
7732 {
7733 XWINDOW (mini_window)->must_be_updated_p = 1;
7734 pause |= update_frame (mini_frame, 0, 0);
7735 if (!pause && hscroll_windows (mini_window))
7736 goto retry;
7737 }
7738 }
7739 }
7740
7741 /* If display was paused because of pending input, make sure we do a
7742 thorough update the next time. */
7743 if (pause)
7744 {
7745 /* Prevent the optimization at the beginning of
7746 redisplay_internal that tries a single-line update of the
7747 line containing the cursor in the selected window. */
7748 CHARPOS (this_line_start_pos) = 0;
7749
7750 /* Let the overlay arrow be updated the next time. */
7751 if (!NILP (last_arrow_position))
7752 {
7753 last_arrow_position = Qt;
7754 last_arrow_string = Qt;
7755 }
7756
7757 /* If we pause after scrolling, some rows in the current
7758 matrices of some windows are not valid. */
7759 if (!WINDOW_FULL_WIDTH_P (w)
7760 && !FRAME_WINDOW_P (XFRAME (w->frame)))
7761 update_mode_lines = 1;
7762 }
7763
7764 /* Now text on frame agrees with windows, so put info into the
7765 windows for partial redisplay to follow. */
7766 if (!pause)
7767 {
7768 register struct buffer *b = XBUFFER (w->buffer);
7769
7770 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
7771 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
7772 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
7773 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
7774
7775 if (consider_all_windows_p)
7776 mark_window_display_accurate (FRAME_ROOT_WINDOW (sf), 1);
7777 else
7778 {
7779 XSETFASTINT (w->last_point, BUF_PT (b));
7780 w->last_cursor = w->cursor;
7781 w->last_cursor_off_p = w->cursor_off_p;
7782
7783 b->clip_changed = 0;
7784 b->prevent_redisplay_optimizations_p = 0;
7785 w->update_mode_line = Qnil;
7786 XSETFASTINT (w->last_modified, BUF_MODIFF (b));
7787 XSETFASTINT (w->last_overlay_modified, BUF_OVERLAY_MODIFF (b));
7788 w->last_had_star
7789 = (BUF_MODIFF (XBUFFER (w->buffer)) > BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7790 ? Qt : Qnil);
7791
7792 /* Record if we are showing a region, so can make sure to
7793 update it fully at next redisplay. */
7794 w->region_showing = (!NILP (Vtransient_mark_mode)
7795 && (w == XWINDOW (current_buffer->last_selected_window)
7796 || highlight_nonselected_windows)
7797 && !NILP (XBUFFER (w->buffer)->mark_active)
7798 ? Fmarker_position (XBUFFER (w->buffer)->mark)
7799 : Qnil);
7800
7801 w->window_end_valid = w->buffer;
7802 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
7803 last_arrow_string = Voverlay_arrow_string;
7804 if (frame_up_to_date_hook != 0)
7805 (*frame_up_to_date_hook) (sf);
7806
7807 w->current_matrix->buffer = b;
7808 w->current_matrix->begv = BUF_BEGV (b);
7809 w->current_matrix->zv = BUF_ZV (b);
7810 }
7811
7812 update_mode_lines = 0;
7813 windows_or_buffers_changed = 0;
7814 }
7815
7816 /* Start SIGIO interrupts coming again. Having them off during the
7817 code above makes it less likely one will discard output, but not
7818 impossible, since there might be stuff in the system buffer here.
7819 But it is much hairier to try to do anything about that. */
7820 if (interrupt_input)
7821 request_sigio ();
7822 start_polling ();
7823
7824 /* If a frame has become visible which was not before, redisplay
7825 again, so that we display it. Expose events for such a frame
7826 (which it gets when becoming visible) don't call the parts of
7827 redisplay constructing glyphs, so simply exposing a frame won't
7828 display anything in this case. So, we have to display these
7829 frames here explicitly. */
7830 if (!pause)
7831 {
7832 Lisp_Object tail, frame;
7833 int new_count = 0;
7834
7835 FOR_EACH_FRAME (tail, frame)
7836 {
7837 int this_is_visible = 0;
7838
7839 if (XFRAME (frame)->visible)
7840 this_is_visible = 1;
7841 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
7842 if (XFRAME (frame)->visible)
7843 this_is_visible = 1;
7844
7845 if (this_is_visible)
7846 new_count++;
7847 }
7848
7849 if (new_count != number_of_visible_frames)
7850 windows_or_buffers_changed++;
7851 }
7852
7853 /* Change frame size now if a change is pending. */
7854 do_pending_window_change (1);
7855
7856 /* If we just did a pending size change, or have additional
7857 visible frames, redisplay again. */
7858 if (windows_or_buffers_changed && !pause)
7859 goto retry;
7860
7861 end_of_redisplay:;
7862
7863 unbind_to (count, Qnil);
7864 }
7865
7866
7867 /* Redisplay, but leave alone any recent echo area message unless
7868 another message has been requested in its place.
7869
7870 This is useful in situations where you need to redisplay but no
7871 user action has occurred, making it inappropriate for the message
7872 area to be cleared. See tracking_off and
7873 wait_reading_process_input for examples of these situations. */
7874
7875 void
7876 redisplay_preserve_echo_area ()
7877 {
7878 if (!NILP (echo_area_buffer[1]))
7879 {
7880 /* We have a previously displayed message, but no current
7881 message. Redisplay the previous message. */
7882 display_last_displayed_message_p = 1;
7883 redisplay_internal (1);
7884 display_last_displayed_message_p = 0;
7885 }
7886 else
7887 redisplay_internal (1);
7888 }
7889
7890
7891 /* Function registered with record_unwind_protect in
7892 redisplay_internal. Clears the flag indicating that a redisplay is
7893 in progress. */
7894
7895 static Lisp_Object
7896 unwind_redisplay (old_redisplaying_p)
7897 Lisp_Object old_redisplaying_p;
7898 {
7899 redisplaying_p = XFASTINT (old_redisplaying_p);
7900 return Qnil;
7901 }
7902
7903
7904 /* Mark the display of windows in the window tree rooted at WINDOW as
7905 accurate or inaccurate. If FLAG is non-zero mark display of WINDOW
7906 as accurate. If FLAG is zero arrange for WINDOW to be redisplayed
7907 the next time redisplay_internal is called. */
7908
7909 void
7910 mark_window_display_accurate (window, accurate_p)
7911 Lisp_Object window;
7912 int accurate_p;
7913 {
7914 struct window *w;
7915
7916 for (; !NILP (window); window = w->next)
7917 {
7918 w = XWINDOW (window);
7919
7920 if (BUFFERP (w->buffer))
7921 {
7922 struct buffer *b = XBUFFER (w->buffer);
7923
7924 XSETFASTINT (w->last_modified,
7925 accurate_p ? BUF_MODIFF (b) : 0);
7926 XSETFASTINT (w->last_overlay_modified,
7927 accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
7928 w->last_had_star = (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b)
7929 ? Qt : Qnil);
7930
7931 #if 0 /* I don't think this is necessary because display_line does it.
7932 Let's check it. */
7933 /* Record if we are showing a region, so can make sure to
7934 update it fully at next redisplay. */
7935 w->region_showing
7936 = (!NILP (Vtransient_mark_mode)
7937 && (w == XWINDOW (current_buffer->last_selected_window)
7938 || highlight_nonselected_windows)
7939 && (!NILP (b->mark_active)
7940 ? Fmarker_position (b->mark)
7941 : Qnil));
7942 #endif
7943
7944 if (accurate_p)
7945 {
7946 b->clip_changed = 0;
7947 b->prevent_redisplay_optimizations_p = 0;
7948 w->current_matrix->buffer = b;
7949 w->current_matrix->begv = BUF_BEGV (b);
7950 w->current_matrix->zv = BUF_ZV (b);
7951 w->last_cursor = w->cursor;
7952 w->last_cursor_off_p = w->cursor_off_p;
7953 if (w == XWINDOW (selected_window))
7954 w->last_point = make_number (BUF_PT (b));
7955 else
7956 w->last_point = make_number (XMARKER (w->pointm)->charpos);
7957 }
7958 }
7959
7960 w->window_end_valid = w->buffer;
7961 w->update_mode_line = Qnil;
7962
7963 if (!NILP (w->vchild))
7964 mark_window_display_accurate (w->vchild, accurate_p);
7965 if (!NILP (w->hchild))
7966 mark_window_display_accurate (w->hchild, accurate_p);
7967 }
7968
7969 if (accurate_p)
7970 {
7971 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
7972 last_arrow_string = Voverlay_arrow_string;
7973 }
7974 else
7975 {
7976 /* Force a thorough redisplay the next time by setting
7977 last_arrow_position and last_arrow_string to t, which is
7978 unequal to any useful value of Voverlay_arrow_... */
7979 last_arrow_position = Qt;
7980 last_arrow_string = Qt;
7981 }
7982 }
7983
7984
7985 /* Return value in display table DP (Lisp_Char_Table *) for character
7986 C. Since a display table doesn't have any parent, we don't have to
7987 follow parent. Do not call this function directly but use the
7988 macro DISP_CHAR_VECTOR. */
7989
7990 Lisp_Object
7991 disp_char_vector (dp, c)
7992 struct Lisp_Char_Table *dp;
7993 int c;
7994 {
7995 int code[4], i;
7996 Lisp_Object val;
7997
7998 if (SINGLE_BYTE_CHAR_P (c))
7999 return (dp->contents[c]);
8000
8001 SPLIT_CHAR (c, code[0], code[1], code[2]);
8002 if (code[1] < 32)
8003 code[1] = -1;
8004 else if (code[2] < 32)
8005 code[2] = -1;
8006
8007 /* Here, the possible range of code[0] (== charset ID) is
8008 128..max_charset. Since the top level char table contains data
8009 for multibyte characters after 256th element, we must increment
8010 code[0] by 128 to get a correct index. */
8011 code[0] += 128;
8012 code[3] = -1; /* anchor */
8013
8014 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
8015 {
8016 val = dp->contents[code[i]];
8017 if (!SUB_CHAR_TABLE_P (val))
8018 return (NILP (val) ? dp->defalt : val);
8019 }
8020
8021 /* Here, val is a sub char table. We return the default value of
8022 it. */
8023 return (dp->defalt);
8024 }
8025
8026
8027 \f
8028 /***********************************************************************
8029 Window Redisplay
8030 ***********************************************************************/
8031
8032 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
8033
8034 static void
8035 redisplay_windows (window)
8036 Lisp_Object window;
8037 {
8038 while (!NILP (window))
8039 {
8040 struct window *w = XWINDOW (window);
8041
8042 if (!NILP (w->hchild))
8043 redisplay_windows (w->hchild);
8044 else if (!NILP (w->vchild))
8045 redisplay_windows (w->vchild);
8046 else
8047 redisplay_window (window, 0);
8048
8049 window = w->next;
8050 }
8051 }
8052
8053
8054 /* Set cursor position of W. PT is assumed to be displayed in ROW.
8055 DELTA is the number of bytes by which positions recorded in ROW
8056 differ from current buffer positions. */
8057
8058 void
8059 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
8060 struct window *w;
8061 struct glyph_row *row;
8062 struct glyph_matrix *matrix;
8063 int delta, delta_bytes, dy, dvpos;
8064 {
8065 struct glyph *glyph = row->glyphs[TEXT_AREA];
8066 struct glyph *end = glyph + row->used[TEXT_AREA];
8067 int x = row->x;
8068 int pt_old = PT - delta;
8069
8070 /* Skip over glyphs not having an object at the start of the row.
8071 These are special glyphs like truncation marks on terminal
8072 frames. */
8073 if (row->displays_text_p)
8074 while (glyph < end
8075 && INTEGERP (glyph->object)
8076 && glyph->charpos < 0)
8077 {
8078 x += glyph->pixel_width;
8079 ++glyph;
8080 }
8081
8082 while (glyph < end
8083 && !INTEGERP (glyph->object)
8084 && (!BUFFERP (glyph->object)
8085 || glyph->charpos < pt_old))
8086 {
8087 x += glyph->pixel_width;
8088 ++glyph;
8089 }
8090
8091 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
8092 w->cursor.x = x;
8093 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
8094 w->cursor.y = row->y + dy;
8095
8096 if (w == XWINDOW (selected_window))
8097 {
8098 if (!row->continued_p
8099 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
8100 && row->x == 0)
8101 {
8102 this_line_buffer = XBUFFER (w->buffer);
8103
8104 CHARPOS (this_line_start_pos)
8105 = MATRIX_ROW_START_CHARPOS (row) + delta;
8106 BYTEPOS (this_line_start_pos)
8107 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
8108
8109 CHARPOS (this_line_end_pos)
8110 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
8111 BYTEPOS (this_line_end_pos)
8112 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
8113
8114 this_line_y = w->cursor.y;
8115 this_line_pixel_height = row->height;
8116 this_line_vpos = w->cursor.vpos;
8117 this_line_start_x = row->x;
8118 }
8119 else
8120 CHARPOS (this_line_start_pos) = 0;
8121 }
8122 }
8123
8124
8125 /* Run window scroll functions, if any, for WINDOW with new window
8126 start STARTP. Sets the window start of WINDOW to that position.
8127
8128 We assume that the window's buffer is really current. */
8129
8130 static INLINE struct text_pos
8131 run_window_scroll_functions (window, startp)
8132 Lisp_Object window;
8133 struct text_pos startp;
8134 {
8135 struct window *w = XWINDOW (window);
8136 SET_MARKER_FROM_TEXT_POS (w->start, startp);
8137
8138 if (current_buffer != XBUFFER (w->buffer))
8139 abort ();
8140
8141 if (!NILP (Vwindow_scroll_functions))
8142 {
8143 run_hook_with_args_2 (Qwindow_scroll_functions, window,
8144 make_number (CHARPOS (startp)));
8145 SET_TEXT_POS_FROM_MARKER (startp, w->start);
8146 /* In case the hook functions switch buffers. */
8147 if (current_buffer != XBUFFER (w->buffer))
8148 set_buffer_internal_1 (XBUFFER (w->buffer));
8149 }
8150
8151 return startp;
8152 }
8153
8154
8155 /* Modify the desired matrix of window W and W->vscroll so that the
8156 line containing the cursor is fully visible. */
8157
8158 static void
8159 make_cursor_line_fully_visible (w)
8160 struct window *w;
8161 {
8162 struct glyph_matrix *matrix;
8163 struct glyph_row *row;
8164 int header_line_height;
8165
8166 /* It's not always possible to find the cursor, e.g, when a window
8167 is full of overlay strings. Don't do anything in that case. */
8168 if (w->cursor.vpos < 0)
8169 return;
8170
8171 matrix = w->desired_matrix;
8172 row = MATRIX_ROW (matrix, w->cursor.vpos);
8173
8174 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row)
8175 /* The row may be partially visible at the top because we
8176 already have chosen a vscroll to align the bottom of the
8177 row with the bottom of the window. This happens for rows
8178 taller than the window. */
8179 && row->y + row->height < window_box_height (w))
8180 {
8181 int dy = row->height - row->visible_height;
8182 w->vscroll = 0;
8183 w->cursor.y += dy;
8184 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
8185 }
8186 else if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)
8187 /* The row may be partially visible at the bottom because
8188 we chose a vscroll to align the row's top with the
8189 window's top. This happens for rows taller than the
8190 window. */
8191 && row->y > WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w))
8192 {
8193 int dy = - (row->height - row->visible_height);
8194 w->vscroll = dy;
8195 w->cursor.y += dy;
8196 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
8197 }
8198
8199 /* When we change the cursor y-position of the selected window,
8200 change this_line_y as well so that the display optimization for
8201 the cursor line of the selected window in redisplay_internal uses
8202 the correct y-position. */
8203 if (w == XWINDOW (selected_window))
8204 this_line_y = w->cursor.y;
8205 }
8206
8207
8208 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
8209 non-zero means only WINDOW is redisplayed in redisplay_internal.
8210 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
8211 in redisplay_window to bring a partially visible line into view in
8212 the case that only the cursor has moved.
8213
8214 Value is
8215
8216 1 if scrolling succeeded
8217
8218 0 if scrolling didn't find point.
8219
8220 -1 if new fonts have been loaded so that we must interrupt
8221 redisplay, adjust glyph matrices, and try again. */
8222
8223 static int
8224 try_scrolling (window, just_this_one_p, scroll_conservatively,
8225 scroll_step, temp_scroll_step)
8226 Lisp_Object window;
8227 int just_this_one_p;
8228 int scroll_conservatively, scroll_step;
8229 int temp_scroll_step;
8230 {
8231 struct window *w = XWINDOW (window);
8232 struct frame *f = XFRAME (w->frame);
8233 struct text_pos scroll_margin_pos;
8234 struct text_pos pos;
8235 struct text_pos startp;
8236 struct it it;
8237 Lisp_Object window_end;
8238 int this_scroll_margin;
8239 int dy = 0;
8240 int scroll_max;
8241 int line_height, rc;
8242 int amount_to_scroll = 0;
8243 Lisp_Object aggressive;
8244 int height;
8245
8246 #if GLYPH_DEBUG
8247 debug_method_add (w, "try_scrolling");
8248 #endif
8249
8250 SET_TEXT_POS_FROM_MARKER (startp, w->start);
8251
8252 /* Compute scroll margin height in pixels. We scroll when point is
8253 within this distance from the top or bottom of the window. */
8254 if (scroll_margin > 0)
8255 {
8256 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
8257 this_scroll_margin *= CANON_Y_UNIT (f);
8258 }
8259 else
8260 this_scroll_margin = 0;
8261
8262 /* Compute how much we should try to scroll maximally to bring point
8263 into view. */
8264 if (scroll_step)
8265 scroll_max = scroll_step;
8266 else if (scroll_conservatively)
8267 scroll_max = scroll_conservatively;
8268 else if (temp_scroll_step)
8269 scroll_max = temp_scroll_step;
8270 else if (NUMBERP (current_buffer->scroll_down_aggressively)
8271 || NUMBERP (current_buffer->scroll_up_aggressively))
8272 /* We're trying to scroll because of aggressive scrolling
8273 but no scroll_step is set. Choose an arbitrary one. Maybe
8274 there should be a variable for this. */
8275 scroll_max = 10;
8276 else
8277 scroll_max = 0;
8278 scroll_max *= CANON_Y_UNIT (f);
8279
8280 /* Decide whether we have to scroll down. Start at the window end
8281 and move this_scroll_margin up to find the position of the scroll
8282 margin. */
8283 window_end = Fwindow_end (window, Qt);
8284 CHARPOS (scroll_margin_pos) = XINT (window_end);
8285 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
8286 if (this_scroll_margin)
8287 {
8288 start_display (&it, w, scroll_margin_pos);
8289 move_it_vertically (&it, - this_scroll_margin);
8290 scroll_margin_pos = it.current.pos;
8291 }
8292
8293 if (PT >= CHARPOS (scroll_margin_pos))
8294 {
8295 int y0;
8296
8297 /* Point is in the scroll margin at the bottom of the window, or
8298 below. Compute a new window start that makes point visible. */
8299
8300 /* Compute the distance from the scroll margin to PT.
8301 Give up if the distance is greater than scroll_max. */
8302 start_display (&it, w, scroll_margin_pos);
8303 y0 = it.current_y;
8304 move_it_to (&it, PT, 0, it.last_visible_y, -1,
8305 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
8306 line_height = (it.max_ascent + it.max_descent
8307 ? it.max_ascent + it.max_descent
8308 : last_height);
8309 dy = it.current_y + line_height - y0;
8310 if (dy > scroll_max)
8311 return 0;
8312
8313 /* Move the window start down. If scrolling conservatively,
8314 move it just enough down to make point visible. If
8315 scroll_step is set, move it down by scroll_step. */
8316 start_display (&it, w, startp);
8317
8318 if (scroll_conservatively)
8319 amount_to_scroll = dy;
8320 else if (scroll_step || temp_scroll_step)
8321 amount_to_scroll = scroll_max;
8322 else
8323 {
8324 aggressive = current_buffer->scroll_down_aggressively;
8325 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
8326 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
8327 if (NUMBERP (aggressive))
8328 amount_to_scroll = XFLOATINT (aggressive) * height;
8329 }
8330
8331 if (amount_to_scroll <= 0)
8332 return 0;
8333
8334 move_it_vertically (&it, amount_to_scroll);
8335 startp = it.current.pos;
8336 }
8337 else
8338 {
8339 /* See if point is inside the scroll margin at the top of the
8340 window. */
8341 scroll_margin_pos = startp;
8342 if (this_scroll_margin)
8343 {
8344 start_display (&it, w, startp);
8345 move_it_vertically (&it, this_scroll_margin);
8346 scroll_margin_pos = it.current.pos;
8347 }
8348
8349 if (PT < CHARPOS (scroll_margin_pos))
8350 {
8351 /* Point is in the scroll margin at the top of the window or
8352 above what is displayed in the window. */
8353 int y0;
8354
8355 /* Compute the vertical distance from PT to the scroll
8356 margin position. Give up if distance is greater than
8357 scroll_max. */
8358 SET_TEXT_POS (pos, PT, PT_BYTE);
8359 start_display (&it, w, pos);
8360 y0 = it.current_y;
8361 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
8362 it.last_visible_y, -1,
8363 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
8364 dy = it.current_y - y0;
8365 if (dy > scroll_max)
8366 return 0;
8367
8368 /* Compute new window start. */
8369 start_display (&it, w, startp);
8370
8371 if (scroll_conservatively)
8372 amount_to_scroll = dy;
8373 else if (scroll_step || temp_scroll_step)
8374 amount_to_scroll = scroll_max;
8375 else
8376 {
8377 aggressive = current_buffer->scroll_up_aggressively;
8378 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
8379 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
8380 if (NUMBERP (aggressive))
8381 amount_to_scroll = XFLOATINT (aggressive) * height;
8382 }
8383
8384 if (amount_to_scroll <= 0)
8385 return 0;
8386
8387 move_it_vertically (&it, - amount_to_scroll);
8388 startp = it.current.pos;
8389 }
8390 }
8391
8392 /* Run window scroll functions. */
8393 startp = run_window_scroll_functions (window, startp);
8394
8395 /* Display the window. Give up if new fonts are loaded, or if point
8396 doesn't appear. */
8397 if (!try_window (window, startp))
8398 rc = -1;
8399 else if (w->cursor.vpos < 0)
8400 {
8401 clear_glyph_matrix (w->desired_matrix);
8402 rc = 0;
8403 }
8404 else
8405 {
8406 /* Maybe forget recorded base line for line number display. */
8407 if (!just_this_one_p
8408 || current_buffer->clip_changed
8409 || BEG_UNCHANGED < CHARPOS (startp))
8410 w->base_line_number = Qnil;
8411
8412 /* If cursor ends up on a partially visible line, shift display
8413 lines up or down. */
8414 make_cursor_line_fully_visible (w);
8415 rc = 1;
8416 }
8417
8418 return rc;
8419 }
8420
8421
8422 /* Compute a suitable window start for window W if display of W starts
8423 on a continuation line. Value is non-zero if a new window start
8424 was computed.
8425
8426 The new window start will be computed, based on W's width, starting
8427 from the start of the continued line. It is the start of the
8428 screen line with the minimum distance from the old start W->start. */
8429
8430 static int
8431 compute_window_start_on_continuation_line (w)
8432 struct window *w;
8433 {
8434 struct text_pos pos, start_pos;
8435 int window_start_changed_p = 0;
8436
8437 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
8438
8439 /* If window start is on a continuation line... Window start may be
8440 < BEGV in case there's invisible text at the start of the
8441 buffer (M-x rmail, for example). */
8442 if (CHARPOS (start_pos) > BEGV
8443 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
8444 {
8445 struct it it;
8446 struct glyph_row *row;
8447
8448 /* Handle the case that the window start is out of range. */
8449 if (CHARPOS (start_pos) < BEGV)
8450 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
8451 else if (CHARPOS (start_pos) > ZV)
8452 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
8453
8454 /* Find the start of the continued line. This should be fast
8455 because scan_buffer is fast (newline cache). */
8456 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
8457 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
8458 row, DEFAULT_FACE_ID);
8459 reseat_at_previous_visible_line_start (&it);
8460
8461 /* If the line start is "too far" away from the window start,
8462 say it takes too much time to compute a new window start. */
8463 if (CHARPOS (start_pos) - IT_CHARPOS (it)
8464 < XFASTINT (w->height) * XFASTINT (w->width))
8465 {
8466 int min_distance, distance;
8467
8468 /* Move forward by display lines to find the new window
8469 start. If window width was enlarged, the new start can
8470 be expected to be > the old start. If window width was
8471 decreased, the new window start will be < the old start.
8472 So, we're looking for the display line start with the
8473 minimum distance from the old window start. */
8474 pos = it.current.pos;
8475 min_distance = INFINITY;
8476 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
8477 distance < min_distance)
8478 {
8479 min_distance = distance;
8480 pos = it.current.pos;
8481 move_it_by_lines (&it, 1, 0);
8482 }
8483
8484 /* Set the window start there. */
8485 SET_MARKER_FROM_TEXT_POS (w->start, pos);
8486 window_start_changed_p = 1;
8487 }
8488 }
8489
8490 return window_start_changed_p;
8491 }
8492
8493
8494 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
8495 selected_window is redisplayed. */
8496
8497 static void
8498 redisplay_window (window, just_this_one_p)
8499 Lisp_Object window;
8500 int just_this_one_p;
8501 {
8502 struct window *w = XWINDOW (window);
8503 struct frame *f = XFRAME (w->frame);
8504 struct buffer *buffer = XBUFFER (w->buffer);
8505 struct buffer *old = current_buffer;
8506 struct text_pos lpoint, opoint, startp;
8507 int update_mode_line;
8508 int tem;
8509 struct it it;
8510 /* Record it now because it's overwritten. */
8511 int current_matrix_up_to_date_p = 0;
8512 int temp_scroll_step = 0;
8513 int count = specpdl_ptr - specpdl;
8514
8515 SET_TEXT_POS (lpoint, PT, PT_BYTE);
8516 opoint = lpoint;
8517
8518 /* W must be a leaf window here. */
8519 xassert (!NILP (w->buffer));
8520 #if GLYPH_DEBUG
8521 *w->desired_matrix->method = 0;
8522 #endif
8523
8524 specbind (Qinhibit_point_motion_hooks, Qt);
8525
8526 reconsider_clip_changes (w, buffer);
8527
8528 /* Has the mode line to be updated? */
8529 update_mode_line = (!NILP (w->update_mode_line)
8530 || update_mode_lines
8531 || buffer->clip_changed);
8532
8533 if (MINI_WINDOW_P (w))
8534 {
8535 if (w == XWINDOW (echo_area_window)
8536 && !NILP (echo_area_buffer[0]))
8537 {
8538 if (update_mode_line)
8539 /* We may have to update a tty frame's menu bar or a
8540 tool-bar. Example `M-x C-h C-h C-g'. */
8541 goto finish_menu_bars;
8542 else
8543 /* We've already displayed the echo area glyphs in this window. */
8544 goto finish_scroll_bars;
8545 }
8546 else if (w != XWINDOW (minibuf_window))
8547 {
8548 /* W is a mini-buffer window, but it's not the currently
8549 active one, so clear it. */
8550 int yb = window_text_bottom_y (w);
8551 struct glyph_row *row;
8552 int y;
8553
8554 for (y = 0, row = w->desired_matrix->rows;
8555 y < yb;
8556 y += row->height, ++row)
8557 blank_row (w, row, y);
8558 goto finish_scroll_bars;
8559 }
8560 }
8561
8562 /* Otherwise set up data on this window; select its buffer and point
8563 value. */
8564 /* Really select the buffer, for the sake of buffer-local
8565 variables. */
8566 set_buffer_internal_1 (XBUFFER (w->buffer));
8567 SET_TEXT_POS (opoint, PT, PT_BYTE);
8568
8569 current_matrix_up_to_date_p
8570 = (!NILP (w->window_end_valid)
8571 && !current_buffer->clip_changed
8572 && XFASTINT (w->last_modified) >= MODIFF
8573 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
8574
8575 /* When windows_or_buffers_changed is non-zero, we can't rely on
8576 the window end being valid, so set it to nil there. */
8577 if (windows_or_buffers_changed)
8578 {
8579 /* If window starts on a continuation line, maybe adjust the
8580 window start in case the window's width changed. */
8581 if (XMARKER (w->start)->buffer == current_buffer)
8582 compute_window_start_on_continuation_line (w);
8583
8584 w->window_end_valid = Qnil;
8585 }
8586
8587 /* Some sanity checks. */
8588 CHECK_WINDOW_END (w);
8589 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
8590 abort ();
8591 if (BYTEPOS (opoint) < CHARPOS (opoint))
8592 abort ();
8593
8594 /* If %c is in mode line, update it if needed. */
8595 if (!NILP (w->column_number_displayed)
8596 /* This alternative quickly identifies a common case
8597 where no change is needed. */
8598 && !(PT == XFASTINT (w->last_point)
8599 && XFASTINT (w->last_modified) >= MODIFF
8600 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
8601 && XFASTINT (w->column_number_displayed) != current_column ())
8602 update_mode_line = 1;
8603
8604 /* Count number of windows showing the selected buffer. An indirect
8605 buffer counts as its base buffer. */
8606 if (!just_this_one_p)
8607 {
8608 struct buffer *current_base, *window_base;
8609 current_base = current_buffer;
8610 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
8611 if (current_base->base_buffer)
8612 current_base = current_base->base_buffer;
8613 if (window_base->base_buffer)
8614 window_base = window_base->base_buffer;
8615 if (current_base == window_base)
8616 buffer_shared++;
8617 }
8618
8619 /* Point refers normally to the selected window. For any other
8620 window, set up appropriate value. */
8621 if (!EQ (window, selected_window))
8622 {
8623 int new_pt = XMARKER (w->pointm)->charpos;
8624 int new_pt_byte = marker_byte_position (w->pointm);
8625 if (new_pt < BEGV)
8626 {
8627 new_pt = BEGV;
8628 new_pt_byte = BEGV_BYTE;
8629 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
8630 }
8631 else if (new_pt > (ZV - 1))
8632 {
8633 new_pt = ZV;
8634 new_pt_byte = ZV_BYTE;
8635 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
8636 }
8637
8638 /* We don't use SET_PT so that the point-motion hooks don't run. */
8639 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
8640 }
8641
8642 /* If any of the character widths specified in the display table
8643 have changed, invalidate the width run cache. It's true that
8644 this may be a bit late to catch such changes, but the rest of
8645 redisplay goes (non-fatally) haywire when the display table is
8646 changed, so why should we worry about doing any better? */
8647 if (current_buffer->width_run_cache)
8648 {
8649 struct Lisp_Char_Table *disptab = buffer_display_table ();
8650
8651 if (! disptab_matches_widthtab (disptab,
8652 XVECTOR (current_buffer->width_table)))
8653 {
8654 invalidate_region_cache (current_buffer,
8655 current_buffer->width_run_cache,
8656 BEG, Z);
8657 recompute_width_table (current_buffer, disptab);
8658 }
8659 }
8660
8661 /* If window-start is screwed up, choose a new one. */
8662 if (XMARKER (w->start)->buffer != current_buffer)
8663 goto recenter;
8664
8665 SET_TEXT_POS_FROM_MARKER (startp, w->start);
8666
8667 /* If someone specified a new starting point but did not insist,
8668 check whether it can be used. */
8669 if (!NILP (w->optional_new_start)
8670 && CHARPOS (startp) >= BEGV
8671 && CHARPOS (startp) <= ZV)
8672 {
8673 w->optional_new_start = Qnil;
8674 /* This takes a mini-buffer prompt into account. */
8675 start_display (&it, w, startp);
8676 move_it_to (&it, PT, 0, it.last_visible_y, -1,
8677 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
8678 if (IT_CHARPOS (it) == PT)
8679 w->force_start = Qt;
8680 }
8681
8682 /* Handle case where place to start displaying has been specified,
8683 unless the specified location is outside the accessible range. */
8684 if (!NILP (w->force_start)
8685 || w->frozen_window_start_p)
8686 {
8687 w->force_start = Qnil;
8688 w->vscroll = 0;
8689 w->window_end_valid = Qnil;
8690
8691 /* Forget any recorded base line for line number display. */
8692 if (!current_matrix_up_to_date_p
8693 || current_buffer->clip_changed)
8694 w->base_line_number = Qnil;
8695
8696 /* Redisplay the mode line. Select the buffer properly for that.
8697 Also, run the hook window-scroll-functions
8698 because we have scrolled. */
8699 /* Note, we do this after clearing force_start because
8700 if there's an error, it is better to forget about force_start
8701 than to get into an infinite loop calling the hook functions
8702 and having them get more errors. */
8703 if (!update_mode_line
8704 || ! NILP (Vwindow_scroll_functions))
8705 {
8706 update_mode_line = 1;
8707 w->update_mode_line = Qt;
8708 startp = run_window_scroll_functions (window, startp);
8709 }
8710
8711 XSETFASTINT (w->last_modified, 0);
8712 XSETFASTINT (w->last_overlay_modified, 0);
8713 if (CHARPOS (startp) < BEGV)
8714 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
8715 else if (CHARPOS (startp) > ZV)
8716 SET_TEXT_POS (startp, ZV, ZV_BYTE);
8717
8718 /* Redisplay, then check if cursor has been set during the
8719 redisplay. Give up if new fonts were loaded. */
8720 if (!try_window (window, startp))
8721 {
8722 w->force_start = Qt;
8723 clear_glyph_matrix (w->desired_matrix);
8724 goto restore_buffers;
8725 }
8726
8727 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
8728 {
8729 /* If point does not appear, or on a line that is not fully
8730 visible, move point so it does appear. The desired
8731 matrix has been built above, so we can use it. */
8732 int height = window_box_height (w) / 2;
8733 struct glyph_row *row = MATRIX_ROW (w->desired_matrix, 0);
8734
8735 while (row->y < height)
8736 ++row;
8737
8738 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
8739 MATRIX_ROW_START_BYTEPOS (row));
8740
8741 if (w != XWINDOW (selected_window))
8742 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
8743 else if (current_buffer == old)
8744 SET_TEXT_POS (lpoint, PT, PT_BYTE);
8745
8746 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
8747
8748 /* If we are highlighting the region, then we just changed
8749 the region, so redisplay to show it. */
8750 if (!NILP (Vtransient_mark_mode)
8751 && !NILP (current_buffer->mark_active))
8752 {
8753 clear_glyph_matrix (w->desired_matrix);
8754 if (!try_window (window, startp))
8755 goto restore_buffers;
8756 }
8757 }
8758
8759 make_cursor_line_fully_visible (w);
8760 #if GLYPH_DEBUG
8761 debug_method_add (w, "forced window start");
8762 #endif
8763 goto done;
8764 }
8765
8766 /* Handle case where text has not changed, only point, and it has
8767 not moved off the frame. */
8768 if (current_matrix_up_to_date_p
8769 /* Point may be in this window. */
8770 && PT >= CHARPOS (startp)
8771 /* If we don't check this, we are called to move the cursor in a
8772 horizontally split window with a current matrix that doesn't
8773 fit the display. */
8774 && !windows_or_buffers_changed
8775 /* Selective display hasn't changed. */
8776 && !current_buffer->clip_changed
8777 /* If force-mode-line-update was called, really redisplay;
8778 that's how redisplay is forced after e.g. changing
8779 buffer-invisibility-spec. */
8780 && NILP (w->update_mode_line)
8781 /* Can't use this case if highlighting a region. When a
8782 region exists, cursor movement has to do more than just
8783 set the cursor. */
8784 && !(!NILP (Vtransient_mark_mode)
8785 && !NILP (current_buffer->mark_active))
8786 && NILP (w->region_showing)
8787 && NILP (Vshow_trailing_whitespace)
8788 /* Right after splitting windows, last_point may be nil. */
8789 && INTEGERP (w->last_point)
8790 /* This code is not used for mini-buffer for the sake of the case
8791 of redisplaying to replace an echo area message; since in
8792 that case the mini-buffer contents per se are usually
8793 unchanged. This code is of no real use in the mini-buffer
8794 since the handling of this_line_start_pos, etc., in redisplay
8795 handles the same cases. */
8796 && !EQ (window, minibuf_window)
8797 /* When splitting windows or for new windows, it happens that
8798 redisplay is called with a nil window_end_vpos or one being
8799 larger than the window. This should really be fixed in
8800 window.c. I don't have this on my list, now, so we do
8801 approximately the same as the old redisplay code. --gerd. */
8802 && INTEGERP (w->window_end_vpos)
8803 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
8804 && (FRAME_WINDOW_P (f)
8805 || !MARKERP (Voverlay_arrow_position)
8806 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
8807 {
8808 int this_scroll_margin;
8809 struct glyph_row *row;
8810 int scroll_p;
8811
8812 #if GLYPH_DEBUG
8813 debug_method_add (w, "cursor movement");
8814 #endif
8815
8816 /* Scroll if point within this distance from the top or bottom
8817 of the window. This is a pixel value. */
8818 this_scroll_margin = max (0, scroll_margin);
8819 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
8820 this_scroll_margin *= CANON_Y_UNIT (f);
8821
8822 /* Start with the row the cursor was displayed during the last
8823 not paused redisplay. Give up if that row is not valid. */
8824 if (w->last_cursor.vpos >= w->current_matrix->nrows)
8825 goto try_to_scroll;
8826 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
8827 if (row->mode_line_p)
8828 ++row;
8829 if (!row->enabled_p)
8830 goto try_to_scroll;
8831
8832 scroll_p = 0;
8833 if (PT > XFASTINT (w->last_point))
8834 {
8835 /* Point has moved forward. */
8836 int last_y = window_text_bottom_y (w) - this_scroll_margin;
8837
8838 while (MATRIX_ROW_END_CHARPOS (row) < PT
8839 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
8840 {
8841 xassert (row->enabled_p);
8842 ++row;
8843 }
8844
8845 /* The end position of a row equals the start position of
8846 the next row. If PT is there, we would rather display it
8847 in the next line. Exceptions are when the row ends in
8848 the middle of a character, or ends in ZV. */
8849 if (MATRIX_ROW_BOTTOM_Y (row) < last_y
8850 && MATRIX_ROW_END_CHARPOS (row) == PT
8851 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
8852 && !row->ends_at_zv_p)
8853 {
8854 xassert (row->enabled_p);
8855 ++row;
8856 }
8857
8858 /* If within the scroll margin, scroll. Note that
8859 MATRIX_ROW_BOTTOM_Y gives the pixel position at which the
8860 next line would be drawn, and that this_scroll_margin can
8861 be zero. */
8862 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
8863 || PT > MATRIX_ROW_END_CHARPOS (row)
8864 /* Line is completely visible last line in window and PT
8865 is to be set in the next line. */
8866 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
8867 && PT == MATRIX_ROW_END_CHARPOS (row)
8868 && !row->ends_at_zv_p
8869 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
8870 scroll_p = 1;
8871 }
8872 else if (PT < XFASTINT (w->last_point))
8873 {
8874 /* Cursor has to be moved backward. Note that PT >=
8875 CHARPOS (startp) because of the outer if-statement. */
8876 while (!row->mode_line_p
8877 && (MATRIX_ROW_START_CHARPOS (row) > PT
8878 || (MATRIX_ROW_START_CHARPOS (row) == PT
8879 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
8880 && (row->y > this_scroll_margin
8881 || CHARPOS (startp) == BEGV))
8882 {
8883 xassert (row->enabled_p);
8884 --row;
8885 }
8886
8887 /* Consider the following case: Window starts at BEGV, there
8888 is invisible, intangible text at BEGV, so that display
8889 starts at some point START > BEGV. It can happen that
8890 we are called with PT somewhere between BEGV and START.
8891 Try to handle that case. */
8892 if (row < w->current_matrix->rows
8893 || row->mode_line_p)
8894 {
8895 row = w->current_matrix->rows;
8896 if (row->mode_line_p)
8897 ++row;
8898 }
8899
8900 /* Due to newlines in overlay strings, we may have to skip
8901 forward over overlay strings. */
8902 while (MATRIX_ROW_END_CHARPOS (row) == PT
8903 && MATRIX_ROW_ENDS_IN_OVERLAY_STRING_P (row)
8904 && !row->ends_at_zv_p)
8905 ++row;
8906
8907 /* If within the scroll margin, scroll. */
8908 if (row->y < this_scroll_margin
8909 && CHARPOS (startp) != BEGV)
8910 scroll_p = 1;
8911 }
8912
8913 /* if PT is not in the glyph row, give up. */
8914 if (PT < MATRIX_ROW_START_CHARPOS (row)
8915 || PT > MATRIX_ROW_END_CHARPOS (row))
8916 goto try_to_scroll;
8917
8918 /* If we end up in a partially visible line, let's make it fully
8919 visible. This can be done most easily by using the existing
8920 scrolling code. */
8921 if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
8922 {
8923 temp_scroll_step = 1;
8924 goto try_to_scroll;
8925 }
8926 else if (scroll_p)
8927 goto try_to_scroll;
8928
8929 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
8930 goto done;
8931 }
8932
8933 /* If current starting point was originally the beginning of a line
8934 but no longer is, find a new starting point. */
8935 else if (!NILP (w->start_at_line_beg)
8936 && !(CHARPOS (startp) <= BEGV
8937 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
8938 {
8939 #if GLYPH_DEBUG
8940 debug_method_add (w, "recenter 1");
8941 #endif
8942 goto recenter;
8943 }
8944
8945 /* Try scrolling with try_window_id. */
8946 else if (/* Windows and buffers haven't changed. */
8947 !windows_or_buffers_changed
8948 /* Window must be either use window-based redisplay or
8949 be full width. */
8950 && (FRAME_WINDOW_P (f)
8951 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)))
8952 && !MINI_WINDOW_P (w)
8953 /* Point is not known NOT to appear in window. */
8954 && PT >= CHARPOS (startp)
8955 && XFASTINT (w->last_modified)
8956 /* Window is not hscrolled. */
8957 && XFASTINT (w->hscroll) == 0
8958 /* Selective display has not changed. */
8959 && !current_buffer->clip_changed
8960 /* Current matrix is up to date. */
8961 && !NILP (w->window_end_valid)
8962 /* Can't use this case if highlighting a region because
8963 a cursor movement will do more than just set the cursor. */
8964 && !(!NILP (Vtransient_mark_mode)
8965 && !NILP (current_buffer->mark_active))
8966 && NILP (w->region_showing)
8967 && NILP (Vshow_trailing_whitespace)
8968 /* Overlay arrow position and string not changed. */
8969 && EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
8970 && EQ (last_arrow_string, Voverlay_arrow_string)
8971 /* Value is > 0 if update has been done, it is -1 if we
8972 know that the same window start will not work. It is 0
8973 if unsuccessful for some other reason. */
8974 && (tem = try_window_id (w)) != 0)
8975 {
8976 #if GLYPH_DEBUG
8977 debug_method_add (w, "try_window_id");
8978 #endif
8979
8980 if (fonts_changed_p)
8981 goto restore_buffers;
8982 if (tem > 0)
8983 goto done;
8984 /* Otherwise try_window_id has returned -1 which means that we
8985 don't want the alternative below this comment to execute. */
8986 }
8987 else if (CHARPOS (startp) >= BEGV
8988 && CHARPOS (startp) <= ZV
8989 && PT >= CHARPOS (startp)
8990 && (CHARPOS (startp) < ZV
8991 /* Avoid starting at end of buffer. */
8992 || CHARPOS (startp) == BEGV
8993 || (XFASTINT (w->last_modified) >= MODIFF
8994 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
8995 {
8996 #if GLYPH_DEBUG
8997 debug_method_add (w, "same window start");
8998 #endif
8999
9000 /* Try to redisplay starting at same place as before.
9001 If point has not moved off frame, accept the results. */
9002 if (!current_matrix_up_to_date_p
9003 /* Don't use try_window_reusing_current_matrix in this case
9004 because a window scroll function can have changed the
9005 buffer. */
9006 || !NILP (Vwindow_scroll_functions)
9007 || MINI_WINDOW_P (w)
9008 || !try_window_reusing_current_matrix (w))
9009 {
9010 IF_DEBUG (debug_method_add (w, "1"));
9011 try_window (window, startp);
9012 }
9013
9014 if (fonts_changed_p)
9015 goto restore_buffers;
9016
9017 if (w->cursor.vpos >= 0)
9018 {
9019 if (!just_this_one_p
9020 || current_buffer->clip_changed
9021 || BEG_UNCHANGED < CHARPOS (startp))
9022 /* Forget any recorded base line for line number display. */
9023 w->base_line_number = Qnil;
9024
9025 make_cursor_line_fully_visible (w);
9026 goto done;
9027 }
9028 else
9029 clear_glyph_matrix (w->desired_matrix);
9030 }
9031
9032 try_to_scroll:
9033
9034 XSETFASTINT (w->last_modified, 0);
9035 XSETFASTINT (w->last_overlay_modified, 0);
9036
9037 /* Redisplay the mode line. Select the buffer properly for that. */
9038 if (!update_mode_line)
9039 {
9040 update_mode_line = 1;
9041 w->update_mode_line = Qt;
9042 }
9043
9044 /* Try to scroll by specified few lines. */
9045 if ((scroll_conservatively
9046 || scroll_step
9047 || temp_scroll_step
9048 || NUMBERP (current_buffer->scroll_up_aggressively)
9049 || NUMBERP (current_buffer->scroll_down_aggressively))
9050 && !current_buffer->clip_changed
9051 && CHARPOS (startp) >= BEGV
9052 && CHARPOS (startp) <= ZV)
9053 {
9054 /* The function returns -1 if new fonts were loaded, 1 if
9055 successful, 0 if not successful. */
9056 int rc = try_scrolling (window, just_this_one_p,
9057 scroll_conservatively,
9058 scroll_step,
9059 temp_scroll_step);
9060 if (rc > 0)
9061 goto done;
9062 else if (rc < 0)
9063 goto restore_buffers;
9064 }
9065
9066 /* Finally, just choose place to start which centers point */
9067
9068 recenter:
9069
9070 #if GLYPH_DEBUG
9071 debug_method_add (w, "recenter");
9072 #endif
9073
9074 /* w->vscroll = 0; */
9075
9076 /* Forget any previously recorded base line for line number display. */
9077 if (!current_matrix_up_to_date_p
9078 || current_buffer->clip_changed)
9079 w->base_line_number = Qnil;
9080
9081 /* Move backward half the height of the window. */
9082 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
9083 it.current_y = it.last_visible_y;
9084 move_it_vertically_backward (&it, it.last_visible_y / 2);
9085 xassert (IT_CHARPOS (it) >= BEGV);
9086
9087 /* The function move_it_vertically_backward may move over more
9088 than the specified y-distance. If it->w is small, e.g. a
9089 mini-buffer window, we may end up in front of the window's
9090 display area. Start displaying at the start of the line
9091 containing PT in this case. */
9092 if (it.current_y <= 0)
9093 {
9094 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
9095 move_it_vertically (&it, 0);
9096 xassert (IT_CHARPOS (it) <= PT);
9097 it.current_y = 0;
9098 }
9099
9100 it.current_x = it.hpos = 0;
9101
9102 /* Set startp here explicitly in case that helps avoid an infinite loop
9103 in case the window-scroll-functions functions get errors. */
9104 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
9105
9106 /* Run scroll hooks. */
9107 startp = run_window_scroll_functions (window, it.current.pos);
9108
9109 /* Redisplay the window. */
9110 if (!current_matrix_up_to_date_p
9111 || windows_or_buffers_changed
9112 /* Don't use try_window_reusing_current_matrix in this case
9113 because it can have changed the buffer. */
9114 || !NILP (Vwindow_scroll_functions)
9115 || !just_this_one_p
9116 || MINI_WINDOW_P (w)
9117 || !try_window_reusing_current_matrix (w))
9118 try_window (window, startp);
9119
9120 /* If new fonts have been loaded (due to fontsets), give up. We
9121 have to start a new redisplay since we need to re-adjust glyph
9122 matrices. */
9123 if (fonts_changed_p)
9124 goto restore_buffers;
9125
9126 /* If cursor did not appear assume that the middle of the window is
9127 in the first line of the window. Do it again with the next line.
9128 (Imagine a window of height 100, displaying two lines of height
9129 60. Moving back 50 from it->last_visible_y will end in the first
9130 line.) */
9131 if (w->cursor.vpos < 0)
9132 {
9133 if (!NILP (w->window_end_valid)
9134 && PT >= Z - XFASTINT (w->window_end_pos))
9135 {
9136 clear_glyph_matrix (w->desired_matrix);
9137 move_it_by_lines (&it, 1, 0);
9138 try_window (window, it.current.pos);
9139 }
9140 else if (PT < IT_CHARPOS (it))
9141 {
9142 clear_glyph_matrix (w->desired_matrix);
9143 move_it_by_lines (&it, -1, 0);
9144 try_window (window, it.current.pos);
9145 }
9146 else
9147 {
9148 /* Not much we can do about it. */
9149 }
9150 }
9151
9152 /* Consider the following case: Window starts at BEGV, there is
9153 invisible, intangible text at BEGV, so that display starts at
9154 some point START > BEGV. It can happen that we are called with
9155 PT somewhere between BEGV and START. Try to handle that case. */
9156 if (w->cursor.vpos < 0)
9157 {
9158 struct glyph_row *row = w->current_matrix->rows;
9159 if (row->mode_line_p)
9160 ++row;
9161 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9162 }
9163
9164 make_cursor_line_fully_visible (w);
9165
9166 done:
9167
9168 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9169 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
9170 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
9171 ? Qt : Qnil);
9172
9173 /* Display the mode line, if we must. */
9174 if ((update_mode_line
9175 /* If window not full width, must redo its mode line
9176 if (a) the window to its side is being redone and
9177 (b) we do a frame-based redisplay. This is a consequence
9178 of how inverted lines are drawn in frame-based redisplay. */
9179 || (!just_this_one_p
9180 && !FRAME_WINDOW_P (f)
9181 && !WINDOW_FULL_WIDTH_P (w))
9182 /* Line number to display. */
9183 || INTEGERP (w->base_line_pos)
9184 /* Column number is displayed and different from the one displayed. */
9185 || (!NILP (w->column_number_displayed)
9186 && XFASTINT (w->column_number_displayed) != current_column ()))
9187 /* This means that the window has a mode line. */
9188 && (WINDOW_WANTS_MODELINE_P (w)
9189 || WINDOW_WANTS_HEADER_LINE_P (w)))
9190 {
9191 Lisp_Object old_selected_frame;
9192
9193 old_selected_frame = selected_frame;
9194
9195 XSETFRAME (selected_frame, f);
9196 display_mode_lines (w);
9197 selected_frame = old_selected_frame;
9198
9199 /* If mode line height has changed, arrange for a thorough
9200 immediate redisplay using the correct mode line height. */
9201 if (WINDOW_WANTS_MODELINE_P (w)
9202 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
9203 {
9204 fonts_changed_p = 1;
9205 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
9206 = DESIRED_MODE_LINE_HEIGHT (w);
9207 }
9208
9209 /* If top line height has changed, arrange for a thorough
9210 immediate redisplay using the correct mode line height. */
9211 if (WINDOW_WANTS_HEADER_LINE_P (w)
9212 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
9213 {
9214 fonts_changed_p = 1;
9215 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
9216 = DESIRED_HEADER_LINE_HEIGHT (w);
9217 }
9218
9219 if (fonts_changed_p)
9220 goto restore_buffers;
9221 }
9222
9223 if (!line_number_displayed
9224 && !BUFFERP (w->base_line_pos))
9225 {
9226 w->base_line_pos = Qnil;
9227 w->base_line_number = Qnil;
9228 }
9229
9230 finish_menu_bars:
9231
9232 /* When we reach a frame's selected window, redo the frame's menu bar. */
9233 if (update_mode_line
9234 && EQ (FRAME_SELECTED_WINDOW (f), window))
9235 {
9236 int redisplay_menu_p = 0;
9237
9238 if (FRAME_WINDOW_P (f))
9239 {
9240 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
9241 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
9242 #else
9243 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
9244 #endif
9245 }
9246 else
9247 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
9248
9249 if (redisplay_menu_p)
9250 display_menu_bar (w);
9251
9252 #ifdef HAVE_WINDOW_SYSTEM
9253 if (WINDOWP (f->tool_bar_window)
9254 && (FRAME_TOOL_BAR_LINES (f) > 0
9255 || auto_resize_tool_bars_p))
9256 redisplay_tool_bar (f);
9257 #endif
9258 }
9259
9260 finish_scroll_bars:
9261
9262 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
9263 {
9264 int start, end, whole;
9265
9266 /* Calculate the start and end positions for the current window.
9267 At some point, it would be nice to choose between scrollbars
9268 which reflect the whole buffer size, with special markers
9269 indicating narrowing, and scrollbars which reflect only the
9270 visible region.
9271
9272 Note that mini-buffers sometimes aren't displaying any text. */
9273 if (!MINI_WINDOW_P (w)
9274 || (w == XWINDOW (minibuf_window)
9275 && NILP (echo_area_buffer[0])))
9276 {
9277 whole = ZV - BEGV;
9278 start = marker_position (w->start) - BEGV;
9279 /* I don't think this is guaranteed to be right. For the
9280 moment, we'll pretend it is. */
9281 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
9282
9283 if (end < start)
9284 end = start;
9285 if (whole < (end - start))
9286 whole = end - start;
9287 }
9288 else
9289 start = end = whole = 0;
9290
9291 /* Indicate what this scroll bar ought to be displaying now. */
9292 (*set_vertical_scroll_bar_hook) (w, end - start, whole, start);
9293
9294 /* Note that we actually used the scroll bar attached to this
9295 window, so it shouldn't be deleted at the end of redisplay. */
9296 (*redeem_scroll_bar_hook) (w);
9297 }
9298
9299 restore_buffers:
9300
9301 /* Restore current_buffer and value of point in it. */
9302 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
9303 set_buffer_internal_1 (old);
9304 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
9305
9306 unbind_to (count, Qnil);
9307 }
9308
9309
9310 /* Build the complete desired matrix of WINDOW with a window start
9311 buffer position POS. Value is non-zero if successful. It is zero
9312 if fonts were loaded during redisplay which makes re-adjusting
9313 glyph matrices necessary. */
9314
9315 int
9316 try_window (window, pos)
9317 Lisp_Object window;
9318 struct text_pos pos;
9319 {
9320 struct window *w = XWINDOW (window);
9321 struct it it;
9322 struct glyph_row *last_text_row = NULL;
9323
9324 /* Make POS the new window start. */
9325 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
9326
9327 /* Mark cursor position as unknown. No overlay arrow seen. */
9328 w->cursor.vpos = -1;
9329 overlay_arrow_seen = 0;
9330
9331 /* Initialize iterator and info to start at POS. */
9332 start_display (&it, w, pos);
9333
9334 /* Display all lines of W. */
9335 while (it.current_y < it.last_visible_y)
9336 {
9337 if (display_line (&it))
9338 last_text_row = it.glyph_row - 1;
9339 if (fonts_changed_p)
9340 return 0;
9341 }
9342
9343 /* If bottom moved off end of frame, change mode line percentage. */
9344 if (XFASTINT (w->window_end_pos) <= 0
9345 && Z != IT_CHARPOS (it))
9346 w->update_mode_line = Qt;
9347
9348 /* Set window_end_pos to the offset of the last character displayed
9349 on the window from the end of current_buffer. Set
9350 window_end_vpos to its row number. */
9351 if (last_text_row)
9352 {
9353 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
9354 w->window_end_bytepos
9355 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
9356 XSETFASTINT (w->window_end_pos,
9357 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
9358 XSETFASTINT (w->window_end_vpos,
9359 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
9360 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
9361 ->displays_text_p);
9362 }
9363 else
9364 {
9365 w->window_end_bytepos = 0;
9366 XSETFASTINT (w->window_end_pos, 0);
9367 XSETFASTINT (w->window_end_vpos, 0);
9368 }
9369
9370 /* But that is not valid info until redisplay finishes. */
9371 w->window_end_valid = Qnil;
9372 return 1;
9373 }
9374
9375
9376 \f
9377 /************************************************************************
9378 Window redisplay reusing current matrix when buffer has not changed
9379 ************************************************************************/
9380
9381 /* Try redisplay of window W showing an unchanged buffer with a
9382 different window start than the last time it was displayed by
9383 reusing its current matrix. Value is non-zero if successful.
9384 W->start is the new window start. */
9385
9386 static int
9387 try_window_reusing_current_matrix (w)
9388 struct window *w;
9389 {
9390 struct frame *f = XFRAME (w->frame);
9391 struct glyph_row *row, *bottom_row;
9392 struct it it;
9393 struct run run;
9394 struct text_pos start, new_start;
9395 int nrows_scrolled, i;
9396 struct glyph_row *last_text_row;
9397 struct glyph_row *last_reused_text_row;
9398 struct glyph_row *start_row;
9399 int start_vpos, min_y, max_y;
9400
9401 /* Right now this function doesn't handle terminal frames. */
9402 if (!FRAME_WINDOW_P (f))
9403 return 0;
9404
9405 /* Can't do this if region may have changed. */
9406 if ((!NILP (Vtransient_mark_mode)
9407 && !NILP (current_buffer->mark_active))
9408 || !NILP (w->region_showing)
9409 || !NILP (Vshow_trailing_whitespace))
9410 return 0;
9411
9412 /* If top-line visibility has changed, give up. */
9413 if (WINDOW_WANTS_HEADER_LINE_P (w)
9414 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
9415 return 0;
9416
9417 /* Give up if old or new display is scrolled vertically. We could
9418 make this function handle this, but right now it doesn't. */
9419 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9420 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
9421 return 0;
9422
9423 /* The variable new_start now holds the new window start. The old
9424 start `start' can be determined from the current matrix. */
9425 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
9426 start = start_row->start.pos;
9427 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
9428
9429 /* Clear the desired matrix for the display below. */
9430 clear_glyph_matrix (w->desired_matrix);
9431
9432 if (CHARPOS (new_start) <= CHARPOS (start))
9433 {
9434 int first_row_y;
9435
9436 IF_DEBUG (debug_method_add (w, "twu1"));
9437
9438 /* Display up to a row that can be reused. The variable
9439 last_text_row is set to the last row displayed that displays
9440 text. */
9441 start_display (&it, w, new_start);
9442 first_row_y = it.current_y;
9443 w->cursor.vpos = -1;
9444 last_text_row = last_reused_text_row = NULL;
9445 while (it.current_y < it.last_visible_y
9446 && IT_CHARPOS (it) < CHARPOS (start)
9447 && !fonts_changed_p)
9448 if (display_line (&it))
9449 last_text_row = it.glyph_row - 1;
9450
9451 /* A value of current_y < last_visible_y means that we stopped
9452 at the previous window start, which in turn means that we
9453 have at least one reusable row. */
9454 if (it.current_y < it.last_visible_y)
9455 {
9456 nrows_scrolled = it.vpos;
9457
9458 /* Find PT if not already found in the lines displayed. */
9459 if (w->cursor.vpos < 0)
9460 {
9461 int dy = it.current_y - first_row_y;
9462
9463 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9464 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9465 {
9466 if (PT >= MATRIX_ROW_START_CHARPOS (row)
9467 && PT < MATRIX_ROW_END_CHARPOS (row))
9468 {
9469 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
9470 dy, nrows_scrolled);
9471 break;
9472 }
9473
9474 if (MATRIX_ROW_BOTTOM_Y (row) + dy >= it.last_visible_y)
9475 break;
9476
9477 ++row;
9478 }
9479
9480 /* Give up if point was not found. This shouldn't
9481 happen often; not more often than with try_window
9482 itself. */
9483 if (w->cursor.vpos < 0)
9484 {
9485 clear_glyph_matrix (w->desired_matrix);
9486 return 0;
9487 }
9488 }
9489
9490 /* Scroll the display. Do it before the current matrix is
9491 changed. The problem here is that update has not yet
9492 run, i.e. part of the current matrix is not up to date.
9493 scroll_run_hook will clear the cursor, and use the
9494 current matrix to get the height of the row the cursor is
9495 in. */
9496 run.current_y = first_row_y;
9497 run.desired_y = it.current_y;
9498 run.height = it.last_visible_y - it.current_y;
9499 if (run.height > 0
9500 && run.current_y != run.desired_y)
9501 {
9502 update_begin (f);
9503 rif->update_window_begin_hook (w);
9504 rif->scroll_run_hook (w, &run);
9505 rif->update_window_end_hook (w, 0);
9506 update_end (f);
9507 }
9508
9509 /* Shift current matrix down by nrows_scrolled lines. */
9510 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
9511 rotate_matrix (w->current_matrix,
9512 start_vpos,
9513 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
9514 nrows_scrolled);
9515
9516 /* Disable lines not reused. */
9517 for (i = 0; i < it.vpos; ++i)
9518 MATRIX_ROW (w->current_matrix, i)->enabled_p = 0;
9519
9520 /* Re-compute Y positions. */
9521 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix) + nrows_scrolled;
9522 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
9523 max_y = it.last_visible_y;
9524 while (row < bottom_row)
9525 {
9526 row->y = it.current_y;
9527
9528 if (row->y < min_y)
9529 row->visible_height = row->height - (min_y - row->y);
9530 else if (row->y + row->height > max_y)
9531 row->visible_height
9532 = row->height - (row->y + row->height - max_y);
9533 else
9534 row->visible_height = row->height;
9535
9536 it.current_y += row->height;
9537 ++it.vpos;
9538
9539 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9540 last_reused_text_row = row;
9541 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
9542 break;
9543 ++row;
9544 }
9545 }
9546
9547 /* Update window_end_pos etc.; last_reused_text_row is the last
9548 reused row from the current matrix containing text, if any.
9549 The value of last_text_row is the last displayed line
9550 containing text. */
9551 if (last_reused_text_row)
9552 {
9553 w->window_end_bytepos
9554 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
9555 XSETFASTINT (w->window_end_pos,
9556 Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
9557 XSETFASTINT (w->window_end_vpos,
9558 MATRIX_ROW_VPOS (last_reused_text_row,
9559 w->current_matrix));
9560 }
9561 else if (last_text_row)
9562 {
9563 w->window_end_bytepos
9564 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
9565 XSETFASTINT (w->window_end_pos,
9566 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
9567 XSETFASTINT (w->window_end_vpos,
9568 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
9569 }
9570 else
9571 {
9572 /* This window must be completely empty. */
9573 w->window_end_bytepos = 0;
9574 XSETFASTINT (w->window_end_pos, 0);
9575 XSETFASTINT (w->window_end_vpos, 0);
9576 }
9577 w->window_end_valid = Qnil;
9578
9579 /* Update hint: don't try scrolling again in update_window. */
9580 w->desired_matrix->no_scrolling_p = 1;
9581
9582 #if GLYPH_DEBUG
9583 debug_method_add (w, "try_window_reusing_current_matrix 1");
9584 #endif
9585 return 1;
9586 }
9587 else if (CHARPOS (new_start) > CHARPOS (start))
9588 {
9589 struct glyph_row *pt_row, *row;
9590 struct glyph_row *first_reusable_row;
9591 struct glyph_row *first_row_to_display;
9592 int dy;
9593 int yb = window_text_bottom_y (w);
9594
9595 IF_DEBUG (debug_method_add (w, "twu2"));
9596
9597 /* Find the row starting at new_start, if there is one. Don't
9598 reuse a partially visible line at the end. */
9599 first_reusable_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9600 while (first_reusable_row->enabled_p
9601 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
9602 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
9603 < CHARPOS (new_start)))
9604 ++first_reusable_row;
9605
9606 /* Give up if there is no row to reuse. */
9607 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
9608 || !first_reusable_row->enabled_p
9609 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
9610 != CHARPOS (new_start)))
9611 return 0;
9612
9613 /* We can reuse fully visible rows beginning with
9614 first_reusable_row to the end of the window. Set
9615 first_row_to_display to the first row that cannot be reused.
9616 Set pt_row to the row containing point, if there is any. */
9617 first_row_to_display = first_reusable_row;
9618 pt_row = NULL;
9619 while (MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb)
9620 {
9621 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
9622 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
9623 pt_row = first_row_to_display;
9624
9625 ++first_row_to_display;
9626 }
9627
9628 /* Start displaying at the start of first_row_to_display. */
9629 xassert (first_row_to_display->y < yb);
9630 init_to_row_start (&it, w, first_row_to_display);
9631 nrows_scrolled = MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix);
9632 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
9633 - nrows_scrolled);
9634 it.current_y = first_row_to_display->y - first_reusable_row->y;
9635
9636 /* Display lines beginning with first_row_to_display in the
9637 desired matrix. Set last_text_row to the last row displayed
9638 that displays text. */
9639 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
9640 if (pt_row == NULL)
9641 w->cursor.vpos = -1;
9642 last_text_row = NULL;
9643 while (it.current_y < it.last_visible_y && !fonts_changed_p)
9644 if (display_line (&it))
9645 last_text_row = it.glyph_row - 1;
9646
9647 /* Give up If point isn't in a row displayed or reused. */
9648 if (w->cursor.vpos < 0)
9649 {
9650 clear_glyph_matrix (w->desired_matrix);
9651 return 0;
9652 }
9653
9654 /* If point is in a reused row, adjust y and vpos of the cursor
9655 position. */
9656 if (pt_row)
9657 {
9658 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
9659 w->current_matrix);
9660 w->cursor.y -= first_reusable_row->y;
9661 }
9662
9663 /* Scroll the display. */
9664 run.current_y = first_reusable_row->y;
9665 run.desired_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
9666 run.height = it.last_visible_y - run.current_y;
9667 if (run.height)
9668 {
9669 struct frame *f = XFRAME (WINDOW_FRAME (w));
9670 update_begin (f);
9671 rif->update_window_begin_hook (w);
9672 rif->scroll_run_hook (w, &run);
9673 rif->update_window_end_hook (w, 0);
9674 update_end (f);
9675 }
9676
9677 /* Adjust Y positions of reused rows. */
9678 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
9679 row = first_reusable_row;
9680 dy = first_reusable_row->y;
9681 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
9682 max_y = it.last_visible_y;
9683 while (row < first_row_to_display)
9684 {
9685 row->y -= dy;
9686 if (row->y < min_y)
9687 row->visible_height = row->height - (min_y - row->y);
9688 else if (row->y + row->height > max_y)
9689 row->visible_height
9690 = row->height - (row->y + row->height - max_y);
9691 else
9692 row->visible_height = row->height;
9693 ++row;
9694 }
9695
9696 /* Disable rows not reused. */
9697 while (row < bottom_row)
9698 {
9699 row->enabled_p = 0;
9700 ++row;
9701 }
9702
9703 /* Scroll the current matrix. */
9704 xassert (nrows_scrolled > 0);
9705 rotate_matrix (w->current_matrix,
9706 start_vpos,
9707 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
9708 -nrows_scrolled);
9709
9710 /* Adjust window end. A null value of last_text_row means that
9711 the window end is in reused rows which in turn means that
9712 only its vpos can have changed. */
9713 if (last_text_row)
9714 {
9715 w->window_end_bytepos
9716 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
9717 XSETFASTINT (w->window_end_pos,
9718 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
9719 XSETFASTINT (w->window_end_vpos,
9720 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
9721 }
9722 else
9723 {
9724 XSETFASTINT (w->window_end_vpos,
9725 XFASTINT (w->window_end_vpos) - nrows_scrolled);
9726 }
9727
9728 w->window_end_valid = Qnil;
9729 w->desired_matrix->no_scrolling_p = 1;
9730
9731 #if GLYPH_DEBUG
9732 debug_method_add (w, "try_window_reusing_current_matrix 2");
9733 #endif
9734 return 1;
9735 }
9736
9737 return 0;
9738 }
9739
9740
9741 \f
9742 /************************************************************************
9743 Window redisplay reusing current matrix when buffer has changed
9744 ************************************************************************/
9745
9746 static struct glyph_row *get_last_unchanged_at_beg_row P_ ((struct window *));
9747 static struct glyph_row *get_first_unchanged_at_end_row P_ ((struct window *,
9748 int *, int *));
9749 static struct glyph_row *
9750 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
9751 struct glyph_row *));
9752
9753
9754 /* Return the last row in MATRIX displaying text. If row START is
9755 non-null, start searching with that row. IT gives the dimensions
9756 of the display. Value is null if matrix is empty; otherwise it is
9757 a pointer to the row found. */
9758
9759 static struct glyph_row *
9760 find_last_row_displaying_text (matrix, it, start)
9761 struct glyph_matrix *matrix;
9762 struct it *it;
9763 struct glyph_row *start;
9764 {
9765 struct glyph_row *row, *row_found;
9766
9767 /* Set row_found to the last row in IT->w's current matrix
9768 displaying text. The loop looks funny but think of partially
9769 visible lines. */
9770 row_found = NULL;
9771 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
9772 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9773 {
9774 xassert (row->enabled_p);
9775 row_found = row;
9776 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
9777 break;
9778 ++row;
9779 }
9780
9781 return row_found;
9782 }
9783
9784
9785 /* Return the last row in the current matrix of W that is not affected
9786 by changes at the start of current_buffer that occurred since the
9787 last time W was redisplayed. Value is null if no such row exists.
9788
9789 The global variable beg_unchanged has to contain the number of
9790 bytes unchanged at the start of current_buffer. BEG +
9791 beg_unchanged is the buffer position of the first changed byte in
9792 current_buffer. Characters at positions < BEG + beg_unchanged are
9793 at the same buffer positions as they were when the current matrix
9794 was built. */
9795
9796 static struct glyph_row *
9797 get_last_unchanged_at_beg_row (w)
9798 struct window *w;
9799 {
9800 int first_changed_pos = BEG + BEG_UNCHANGED;
9801 struct glyph_row *row;
9802 struct glyph_row *row_found = NULL;
9803 int yb = window_text_bottom_y (w);
9804
9805 /* Find the last row displaying unchanged text. */
9806 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9807 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
9808 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
9809 {
9810 if (/* If row ends before first_changed_pos, it is unchanged,
9811 except in some case. */
9812 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
9813 /* When row ends in ZV and we write at ZV it is not
9814 unchanged. */
9815 && !row->ends_at_zv_p
9816 /* When first_changed_pos is the end of a continued line,
9817 row is not unchanged because it may be no longer
9818 continued. */
9819 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
9820 && row->continued_p))
9821 row_found = row;
9822
9823 /* Stop if last visible row. */
9824 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
9825 break;
9826
9827 ++row;
9828 }
9829
9830 return row_found;
9831 }
9832
9833
9834 /* Find the first glyph row in the current matrix of W that is not
9835 affected by changes at the end of current_buffer since the last
9836 time the window was redisplayed. Return in *DELTA the number of
9837 chars by which buffer positions in unchanged text at the end of
9838 current_buffer must be adjusted. Return in *DELTA_BYTES the
9839 corresponding number of bytes. Value is null if no such row
9840 exists, i.e. all rows are affected by changes. */
9841
9842 static struct glyph_row *
9843 get_first_unchanged_at_end_row (w, delta, delta_bytes)
9844 struct window *w;
9845 int *delta, *delta_bytes;
9846 {
9847 struct glyph_row *row;
9848 struct glyph_row *row_found = NULL;
9849
9850 *delta = *delta_bytes = 0;
9851
9852 /* A value of window_end_pos >= end_unchanged means that the window
9853 end is in the range of changed text. If so, there is no
9854 unchanged row at the end of W's current matrix. */
9855 xassert (!NILP (w->window_end_valid));
9856 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
9857 return NULL;
9858
9859 /* Set row to the last row in W's current matrix displaying text. */
9860 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
9861
9862 /* If matrix is entirely empty, no unchanged row exists. */
9863 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9864 {
9865 /* The value of row is the last glyph row in the matrix having a
9866 meaningful buffer position in it. The end position of row
9867 corresponds to window_end_pos. This allows us to translate
9868 buffer positions in the current matrix to current buffer
9869 positions for characters not in changed text. */
9870 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
9871 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
9872 int last_unchanged_pos, last_unchanged_pos_old;
9873 struct glyph_row *first_text_row
9874 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9875
9876 *delta = Z - Z_old;
9877 *delta_bytes = Z_BYTE - Z_BYTE_old;
9878
9879 /* Set last_unchanged_pos to the buffer position of the last
9880 character in the buffer that has not been changed. Z is the
9881 index + 1 of the last byte in current_buffer, i.e. by
9882 subtracting end_unchanged we get the index of the last
9883 unchanged character, and we have to add BEG to get its buffer
9884 position. */
9885 last_unchanged_pos = Z - END_UNCHANGED + BEG;
9886 last_unchanged_pos_old = last_unchanged_pos - *delta;
9887
9888 /* Search backward from ROW for a row displaying a line that
9889 starts at a minimum position >= last_unchanged_pos_old. */
9890 while (row >= first_text_row)
9891 {
9892 xassert (row->enabled_p);
9893 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (row));
9894
9895 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
9896 row_found = row;
9897 --row;
9898 }
9899 }
9900
9901 xassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
9902 return row_found;
9903 }
9904
9905
9906 /* Make sure that glyph rows in the current matrix of window W
9907 reference the same glyph memory as corresponding rows in the
9908 frame's frame matrix. This function is called after scrolling W's
9909 current matrix on a terminal frame in try_window_id and
9910 try_window_reusing_current_matrix. */
9911
9912 static void
9913 sync_frame_with_window_matrix_rows (w)
9914 struct window *w;
9915 {
9916 struct frame *f = XFRAME (w->frame);
9917 struct glyph_row *window_row, *window_row_end, *frame_row;
9918
9919 /* Preconditions: W must be a leaf window and full-width. Its frame
9920 must have a frame matrix. */
9921 xassert (NILP (w->hchild) && NILP (w->vchild));
9922 xassert (WINDOW_FULL_WIDTH_P (w));
9923 xassert (!FRAME_WINDOW_P (f));
9924
9925 /* If W is a full-width window, glyph pointers in W's current matrix
9926 have, by definition, to be the same as glyph pointers in the
9927 corresponding frame matrix. */
9928 window_row = w->current_matrix->rows;
9929 window_row_end = window_row + w->current_matrix->nrows;
9930 frame_row = f->current_matrix->rows + XFASTINT (w->top);
9931 while (window_row < window_row_end)
9932 {
9933 int area;
9934
9935 for (area = LEFT_MARGIN_AREA; area <= LAST_AREA; ++area)
9936 frame_row->glyphs[area] = window_row->glyphs[area];
9937
9938 /* Disable frame rows whose corresponding window rows have
9939 been disabled in try_window_id. */
9940 if (!window_row->enabled_p)
9941 frame_row->enabled_p = 0;
9942
9943 ++window_row, ++frame_row;
9944 }
9945 }
9946
9947
9948 /* Find the glyph row in window W containing CHARPOS. Consider all
9949 rows between START and END (not inclusive). END null means search
9950 all rows to the end of the display area of W. Value is the row
9951 containing CHARPOS or null. */
9952
9953 static struct glyph_row *
9954 row_containing_pos (w, charpos, start, end)
9955 struct window *w;
9956 int charpos;
9957 struct glyph_row *start, *end;
9958 {
9959 struct glyph_row *row = start;
9960 int last_y;
9961
9962 /* If we happen to start on a header-line, skip that. */
9963 if (row->mode_line_p)
9964 ++row;
9965
9966 if ((end && row >= end) || !row->enabled_p)
9967 return NULL;
9968
9969 last_y = window_text_bottom_y (w);
9970
9971 while ((end == NULL || row < end)
9972 && (MATRIX_ROW_END_CHARPOS (row) < charpos
9973 /* The end position of a row equals the start
9974 position of the next row. If CHARPOS is there, we
9975 would rather display it in the next line, except
9976 when this line ends in ZV. */
9977 || (MATRIX_ROW_END_CHARPOS (row) == charpos
9978 && (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
9979 || !row->ends_at_zv_p)))
9980 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
9981 ++row;
9982
9983 /* Give up if CHARPOS not found. */
9984 if ((end && row >= end)
9985 || charpos < MATRIX_ROW_START_CHARPOS (row)
9986 || charpos > MATRIX_ROW_END_CHARPOS (row))
9987 row = NULL;
9988
9989 return row;
9990 }
9991
9992
9993 /* Try to redisplay window W by reusing its existing display. W's
9994 current matrix must be up to date when this function is called,
9995 i.e. window_end_valid must not be nil.
9996
9997 Value is
9998
9999 1 if display has been updated
10000 0 if otherwise unsuccessful
10001 -1 if redisplay with same window start is known not to succeed
10002
10003 The following steps are performed:
10004
10005 1. Find the last row in the current matrix of W that is not
10006 affected by changes at the start of current_buffer. If no such row
10007 is found, give up.
10008
10009 2. Find the first row in W's current matrix that is not affected by
10010 changes at the end of current_buffer. Maybe there is no such row.
10011
10012 3. Display lines beginning with the row + 1 found in step 1 to the
10013 row found in step 2 or, if step 2 didn't find a row, to the end of
10014 the window.
10015
10016 4. If cursor is not known to appear on the window, give up.
10017
10018 5. If display stopped at the row found in step 2, scroll the
10019 display and current matrix as needed.
10020
10021 6. Maybe display some lines at the end of W, if we must. This can
10022 happen under various circumstances, like a partially visible line
10023 becoming fully visible, or because newly displayed lines are displayed
10024 in smaller font sizes.
10025
10026 7. Update W's window end information. */
10027
10028 /* Check that window end is what we expect it to be. */
10029
10030 static int
10031 try_window_id (w)
10032 struct window *w;
10033 {
10034 struct frame *f = XFRAME (w->frame);
10035 struct glyph_matrix *current_matrix = w->current_matrix;
10036 struct glyph_matrix *desired_matrix = w->desired_matrix;
10037 struct glyph_row *last_unchanged_at_beg_row;
10038 struct glyph_row *first_unchanged_at_end_row;
10039 struct glyph_row *row;
10040 struct glyph_row *bottom_row;
10041 int bottom_vpos;
10042 struct it it;
10043 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
10044 struct text_pos start_pos;
10045 struct run run;
10046 int first_unchanged_at_end_vpos = 0;
10047 struct glyph_row *last_text_row, *last_text_row_at_end;
10048 struct text_pos start;
10049
10050 SET_TEXT_POS_FROM_MARKER (start, w->start);
10051
10052 /* Check pre-conditions. Window end must be valid, otherwise
10053 the current matrix would not be up to date. */
10054 xassert (!NILP (w->window_end_valid));
10055 xassert (FRAME_WINDOW_P (XFRAME (w->frame))
10056 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)));
10057
10058 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
10059 only if buffer has really changed. The reason is that the gap is
10060 initially at Z for freshly visited files. The code below would
10061 set end_unchanged to 0 in that case. */
10062 if (MODIFF > SAVE_MODIFF
10063 /* This seems to happen sometimes after saving a buffer. */
10064 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
10065 {
10066 if (GPT - BEG < BEG_UNCHANGED)
10067 BEG_UNCHANGED = GPT - BEG;
10068 if (Z - GPT < END_UNCHANGED)
10069 END_UNCHANGED = Z - GPT;
10070 }
10071
10072 /* If window starts after a line end, and the last change is in
10073 front of that newline, then changes don't affect the display.
10074 This case happens with stealth-fontification. Note that although
10075 the display is unchanged, glyph positions in the matrix have to
10076 be adjusted, of course. */
10077 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
10078 if (CHARPOS (start) > BEGV
10079 && Z - END_UNCHANGED < CHARPOS (start) - 1
10080 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n'
10081 && PT < MATRIX_ROW_END_CHARPOS (row))
10082 {
10083 struct glyph_row *r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
10084 int delta = CHARPOS (start) - MATRIX_ROW_START_CHARPOS (r0);
10085
10086 if (delta)
10087 {
10088 struct glyph_row *r1 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
10089 int delta_bytes = BYTEPOS (start) - MATRIX_ROW_START_BYTEPOS (r0);
10090
10091 increment_matrix_positions (w->current_matrix,
10092 MATRIX_ROW_VPOS (r0, current_matrix),
10093 MATRIX_ROW_VPOS (r1, current_matrix),
10094 delta, delta_bytes);
10095 }
10096
10097 #if 0 /* If changes are all in front of the window start, the
10098 distance of the last displayed glyph from Z hasn't
10099 changed. */
10100 w->window_end_pos
10101 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
10102 w->window_end_bytepos
10103 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
10104 #endif
10105
10106 return 1;
10107 }
10108
10109 /* Return quickly if changes are all below what is displayed in the
10110 window, and if PT is in the window. */
10111 if (BEG_UNCHANGED > MATRIX_ROW_END_CHARPOS (row)
10112 && PT < MATRIX_ROW_END_CHARPOS (row))
10113 {
10114 /* We have to update window end positions because the buffer's
10115 size has changed. */
10116 w->window_end_pos
10117 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
10118 w->window_end_bytepos
10119 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
10120 return 1;
10121 }
10122
10123 /* Check that window start agrees with the start of the first glyph
10124 row in its current matrix. Check this after we know the window
10125 start is not in changed text, otherwise positions would not be
10126 comparable. */
10127 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10128 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
10129 return 0;
10130
10131 /* Compute the position at which we have to start displaying new
10132 lines. Some of the lines at the top of the window might be
10133 reusable because they are not displaying changed text. Find the
10134 last row in W's current matrix not affected by changes at the
10135 start of current_buffer. Value is null if changes start in the
10136 first line of window. */
10137 last_unchanged_at_beg_row = get_last_unchanged_at_beg_row (w);
10138 if (last_unchanged_at_beg_row)
10139 {
10140 init_to_row_end (&it, w, last_unchanged_at_beg_row);
10141 start_pos = it.current.pos;
10142
10143 /* Start displaying new lines in the desired matrix at the same
10144 vpos we would use in the current matrix, i.e. below
10145 last_unchanged_at_beg_row. */
10146 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
10147 current_matrix);
10148 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
10149 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
10150
10151 xassert (it.hpos == 0 && it.current_x == 0);
10152 }
10153 else
10154 {
10155 /* There are no reusable lines at the start of the window.
10156 Start displaying in the first line. */
10157 start_display (&it, w, start);
10158 start_pos = it.current.pos;
10159 }
10160
10161 /* Find the first row that is not affected by changes at the end of
10162 the buffer. Value will be null if there is no unchanged row, in
10163 which case we must redisplay to the end of the window. delta
10164 will be set to the value by which buffer positions beginning with
10165 first_unchanged_at_end_row have to be adjusted due to text
10166 changes. */
10167 first_unchanged_at_end_row
10168 = get_first_unchanged_at_end_row (w, &delta, &delta_bytes);
10169 IF_DEBUG (debug_delta = delta);
10170 IF_DEBUG (debug_delta_bytes = delta_bytes);
10171
10172 /* Set stop_pos to the buffer position up to which we will have to
10173 display new lines. If first_unchanged_at_end_row != NULL, this
10174 is the buffer position of the start of the line displayed in that
10175 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
10176 that we don't stop at a buffer position. */
10177 stop_pos = 0;
10178 if (first_unchanged_at_end_row)
10179 {
10180 xassert (last_unchanged_at_beg_row == NULL
10181 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
10182
10183 /* If this is a continuation line, move forward to the next one
10184 that isn't. Changes in lines above affect this line.
10185 Caution: this may move first_unchanged_at_end_row to a row
10186 not displaying text. */
10187 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
10188 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
10189 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
10190 < it.last_visible_y))
10191 ++first_unchanged_at_end_row;
10192
10193 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
10194 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
10195 >= it.last_visible_y))
10196 first_unchanged_at_end_row = NULL;
10197 else
10198 {
10199 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
10200 + delta);
10201 first_unchanged_at_end_vpos
10202 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
10203 xassert (stop_pos >= Z - END_UNCHANGED);
10204 }
10205 }
10206 else if (last_unchanged_at_beg_row == NULL)
10207 return 0;
10208
10209
10210 #if GLYPH_DEBUG
10211
10212 /* Either there is no unchanged row at the end, or the one we have
10213 now displays text. This is a necessary condition for the window
10214 end pos calculation at the end of this function. */
10215 xassert (first_unchanged_at_end_row == NULL
10216 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
10217
10218 debug_last_unchanged_at_beg_vpos
10219 = (last_unchanged_at_beg_row
10220 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
10221 : -1);
10222 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
10223
10224 #endif /* GLYPH_DEBUG != 0 */
10225
10226
10227 /* Display new lines. Set last_text_row to the last new line
10228 displayed which has text on it, i.e. might end up as being the
10229 line where the window_end_vpos is. */
10230 w->cursor.vpos = -1;
10231 last_text_row = NULL;
10232 overlay_arrow_seen = 0;
10233 while (it.current_y < it.last_visible_y
10234 && !fonts_changed_p
10235 && (first_unchanged_at_end_row == NULL
10236 || IT_CHARPOS (it) < stop_pos))
10237 {
10238 if (display_line (&it))
10239 last_text_row = it.glyph_row - 1;
10240 }
10241
10242 if (fonts_changed_p)
10243 return -1;
10244
10245
10246 /* Compute differences in buffer positions, y-positions etc. for
10247 lines reused at the bottom of the window. Compute what we can
10248 scroll. */
10249 if (first_unchanged_at_end_row
10250 /* No lines reused because we displayed everything up to the
10251 bottom of the window. */
10252 && it.current_y < it.last_visible_y)
10253 {
10254 dvpos = (it.vpos
10255 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
10256 current_matrix));
10257 dy = it.current_y - first_unchanged_at_end_row->y;
10258 run.current_y = first_unchanged_at_end_row->y;
10259 run.desired_y = run.current_y + dy;
10260 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
10261 }
10262 else
10263 {
10264 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
10265 first_unchanged_at_end_row = NULL;
10266 }
10267 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
10268
10269
10270 /* Find the cursor if not already found. We have to decide whether
10271 PT will appear on this window (it sometimes doesn't, but this is
10272 not a very frequent case.) This decision has to be made before
10273 the current matrix is altered. A value of cursor.vpos < 0 means
10274 that PT is either in one of the lines beginning at
10275 first_unchanged_at_end_row or below the window. Don't care for
10276 lines that might be displayed later at the window end; as
10277 mentioned, this is not a frequent case. */
10278 if (w->cursor.vpos < 0)
10279 {
10280 /* Cursor in unchanged rows at the top? */
10281 if (PT < CHARPOS (start_pos)
10282 && last_unchanged_at_beg_row)
10283 {
10284 row = row_containing_pos (w, PT,
10285 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
10286 last_unchanged_at_beg_row + 1);
10287 xassert (row && row <= last_unchanged_at_beg_row);
10288 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10289 }
10290
10291 /* Start from first_unchanged_at_end_row looking for PT. */
10292 else if (first_unchanged_at_end_row)
10293 {
10294 row = row_containing_pos (w, PT - delta,
10295 first_unchanged_at_end_row, NULL);
10296 if (row)
10297 set_cursor_from_row (w, row, w->current_matrix, delta,
10298 delta_bytes, dy, dvpos);
10299 }
10300
10301 /* Give up if cursor was not found. */
10302 if (w->cursor.vpos < 0)
10303 {
10304 clear_glyph_matrix (w->desired_matrix);
10305 return -1;
10306 }
10307 }
10308
10309 /* Don't let the cursor end in the scroll margins. */
10310 {
10311 int this_scroll_margin, cursor_height;
10312
10313 this_scroll_margin = max (0, scroll_margin);
10314 this_scroll_margin = min (this_scroll_margin,
10315 XFASTINT (w->height) / 4);
10316 this_scroll_margin *= CANON_Y_UNIT (it.f);
10317 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
10318
10319 if ((w->cursor.y < this_scroll_margin
10320 && CHARPOS (start) > BEGV)
10321 /* Don't take scroll margin into account at the bottom because
10322 old redisplay didn't do it either. */
10323 || w->cursor.y + cursor_height > it.last_visible_y)
10324 {
10325 w->cursor.vpos = -1;
10326 clear_glyph_matrix (w->desired_matrix);
10327 return -1;
10328 }
10329 }
10330
10331 /* Scroll the display. Do it before changing the current matrix so
10332 that xterm.c doesn't get confused about where the cursor glyph is
10333 found. */
10334 if (dy && run.height)
10335 {
10336 update_begin (f);
10337
10338 if (FRAME_WINDOW_P (f))
10339 {
10340 rif->update_window_begin_hook (w);
10341 rif->scroll_run_hook (w, &run);
10342 rif->update_window_end_hook (w, 0);
10343 }
10344 else
10345 {
10346 /* Terminal frame. In this case, dvpos gives the number of
10347 lines to scroll by; dvpos < 0 means scroll up. */
10348 int first_unchanged_at_end_vpos
10349 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
10350 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
10351 int end = XFASTINT (w->top) + window_internal_height (w);
10352
10353 /* Perform the operation on the screen. */
10354 if (dvpos > 0)
10355 {
10356 /* Scroll last_unchanged_at_beg_row to the end of the
10357 window down dvpos lines. */
10358 set_terminal_window (end);
10359
10360 /* On dumb terminals delete dvpos lines at the end
10361 before inserting dvpos empty lines. */
10362 if (!scroll_region_ok)
10363 ins_del_lines (end - dvpos, -dvpos);
10364
10365 /* Insert dvpos empty lines in front of
10366 last_unchanged_at_beg_row. */
10367 ins_del_lines (from, dvpos);
10368 }
10369 else if (dvpos < 0)
10370 {
10371 /* Scroll up last_unchanged_at_beg_vpos to the end of
10372 the window to last_unchanged_at_beg_vpos - |dvpos|. */
10373 set_terminal_window (end);
10374
10375 /* Delete dvpos lines in front of
10376 last_unchanged_at_beg_vpos. ins_del_lines will set
10377 the cursor to the given vpos and emit |dvpos| delete
10378 line sequences. */
10379 ins_del_lines (from + dvpos, dvpos);
10380
10381 /* On a dumb terminal insert dvpos empty lines at the
10382 end. */
10383 if (!scroll_region_ok)
10384 ins_del_lines (end + dvpos, -dvpos);
10385 }
10386
10387 set_terminal_window (0);
10388 }
10389
10390 update_end (f);
10391 }
10392
10393 /* Shift reused rows of the current matrix to the right position.
10394 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
10395 text. */
10396 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
10397 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
10398 if (dvpos < 0)
10399 {
10400 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
10401 bottom_vpos, dvpos);
10402 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
10403 bottom_vpos, 0);
10404 }
10405 else if (dvpos > 0)
10406 {
10407 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
10408 bottom_vpos, dvpos);
10409 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
10410 first_unchanged_at_end_vpos + dvpos, 0);
10411 }
10412
10413 /* For frame-based redisplay, make sure that current frame and window
10414 matrix are in sync with respect to glyph memory. */
10415 if (!FRAME_WINDOW_P (f))
10416 sync_frame_with_window_matrix_rows (w);
10417
10418 /* Adjust buffer positions in reused rows. */
10419 if (delta)
10420 increment_matrix_positions (current_matrix,
10421 first_unchanged_at_end_vpos + dvpos,
10422 bottom_vpos, delta, delta_bytes);
10423
10424 /* Adjust Y positions. */
10425 if (dy)
10426 shift_glyph_matrix (w, current_matrix,
10427 first_unchanged_at_end_vpos + dvpos,
10428 bottom_vpos, dy);
10429
10430 if (first_unchanged_at_end_row)
10431 first_unchanged_at_end_row += dvpos;
10432
10433 /* If scrolling up, there may be some lines to display at the end of
10434 the window. */
10435 last_text_row_at_end = NULL;
10436 if (dy < 0)
10437 {
10438 /* Set last_row to the glyph row in the current matrix where the
10439 window end line is found. It has been moved up or down in
10440 the matrix by dvpos. */
10441 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
10442 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
10443
10444 /* If last_row is the window end line, it should display text. */
10445 xassert (last_row->displays_text_p);
10446
10447 /* If window end line was partially visible before, begin
10448 displaying at that line. Otherwise begin displaying with the
10449 line following it. */
10450 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
10451 {
10452 init_to_row_start (&it, w, last_row);
10453 it.vpos = last_vpos;
10454 it.current_y = last_row->y;
10455 }
10456 else
10457 {
10458 init_to_row_end (&it, w, last_row);
10459 it.vpos = 1 + last_vpos;
10460 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
10461 ++last_row;
10462 }
10463
10464 /* We may start in a continuation line. If so, we have to get
10465 the right continuation_lines_width and current_x. */
10466 it.continuation_lines_width = last_row->continuation_lines_width;
10467 it.hpos = it.current_x = 0;
10468
10469 /* Display the rest of the lines at the window end. */
10470 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
10471 while (it.current_y < it.last_visible_y
10472 && !fonts_changed_p)
10473 {
10474 /* Is it always sure that the display agrees with lines in
10475 the current matrix? I don't think so, so we mark rows
10476 displayed invalid in the current matrix by setting their
10477 enabled_p flag to zero. */
10478 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
10479 if (display_line (&it))
10480 last_text_row_at_end = it.glyph_row - 1;
10481 }
10482 }
10483
10484 /* Update window_end_pos and window_end_vpos. */
10485 if (first_unchanged_at_end_row
10486 && first_unchanged_at_end_row->y < it.last_visible_y
10487 && !last_text_row_at_end)
10488 {
10489 /* Window end line if one of the preserved rows from the current
10490 matrix. Set row to the last row displaying text in current
10491 matrix starting at first_unchanged_at_end_row, after
10492 scrolling. */
10493 xassert (first_unchanged_at_end_row->displays_text_p);
10494 row = find_last_row_displaying_text (w->current_matrix, &it,
10495 first_unchanged_at_end_row);
10496 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
10497
10498 XSETFASTINT (w->window_end_pos, Z - MATRIX_ROW_END_CHARPOS (row));
10499 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
10500 XSETFASTINT (w->window_end_vpos,
10501 MATRIX_ROW_VPOS (row, w->current_matrix));
10502 }
10503 else if (last_text_row_at_end)
10504 {
10505 XSETFASTINT (w->window_end_pos,
10506 Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
10507 w->window_end_bytepos
10508 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
10509 XSETFASTINT (w->window_end_vpos,
10510 MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
10511 }
10512 else if (last_text_row)
10513 {
10514 /* We have displayed either to the end of the window or at the
10515 end of the window, i.e. the last row with text is to be found
10516 in the desired matrix. */
10517 XSETFASTINT (w->window_end_pos,
10518 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10519 w->window_end_bytepos
10520 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10521 XSETFASTINT (w->window_end_vpos,
10522 MATRIX_ROW_VPOS (last_text_row, desired_matrix));
10523 }
10524 else if (first_unchanged_at_end_row == NULL
10525 && last_text_row == NULL
10526 && last_text_row_at_end == NULL)
10527 {
10528 /* Displayed to end of window, but no line containing text was
10529 displayed. Lines were deleted at the end of the window. */
10530 int vpos;
10531 int header_line_p = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
10532
10533 for (vpos = XFASTINT (w->window_end_vpos); vpos > 0; --vpos)
10534 if ((w->desired_matrix->rows[vpos + header_line_p].enabled_p
10535 && w->desired_matrix->rows[vpos + header_line_p].displays_text_p)
10536 || (!w->desired_matrix->rows[vpos + header_line_p].enabled_p
10537 && w->current_matrix->rows[vpos + header_line_p].displays_text_p))
10538 break;
10539
10540 w->window_end_vpos = make_number (vpos);
10541 }
10542 else
10543 abort ();
10544
10545 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
10546 debug_end_vpos = XFASTINT (w->window_end_vpos));
10547
10548 /* Record that display has not been completed. */
10549 w->window_end_valid = Qnil;
10550 w->desired_matrix->no_scrolling_p = 1;
10551 return 1;
10552 }
10553
10554
10555 \f
10556 /***********************************************************************
10557 More debugging support
10558 ***********************************************************************/
10559
10560 #if GLYPH_DEBUG
10561
10562 void dump_glyph_row P_ ((struct glyph_matrix *, int, int));
10563 static void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
10564
10565
10566 /* Dump the contents of glyph matrix MATRIX on stderr. If
10567 WITH_GLYPHS_P is non-zero, dump glyph contents as well. */
10568
10569 void
10570 dump_glyph_matrix (matrix, with_glyphs_p)
10571 struct glyph_matrix *matrix;
10572 int with_glyphs_p;
10573 {
10574 int i;
10575 for (i = 0; i < matrix->nrows; ++i)
10576 dump_glyph_row (matrix, i, with_glyphs_p);
10577 }
10578
10579
10580 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
10581 WITH_GLYPH_SP non-zero means dump glyph contents, too. */
10582
10583 void
10584 dump_glyph_row (matrix, vpos, with_glyphs_p)
10585 struct glyph_matrix *matrix;
10586 int vpos, with_glyphs_p;
10587 {
10588 struct glyph_row *row;
10589
10590 if (vpos < 0 || vpos >= matrix->nrows)
10591 return;
10592
10593 row = MATRIX_ROW (matrix, vpos);
10594
10595 fprintf (stderr, "Row Start End Used oEI><O\\CTZFes X Y W\n");
10596 fprintf (stderr, "=============================================\n");
10597
10598 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%1.1d%1.1d %4d %4d %4d\n",
10599 row - matrix->rows,
10600 MATRIX_ROW_START_CHARPOS (row),
10601 MATRIX_ROW_END_CHARPOS (row),
10602 row->used[TEXT_AREA],
10603 row->contains_overlapping_glyphs_p,
10604 row->enabled_p,
10605 row->inverse_p,
10606 row->truncated_on_left_p,
10607 row->truncated_on_right_p,
10608 row->overlay_arrow_p,
10609 row->continued_p,
10610 MATRIX_ROW_CONTINUATION_LINE_P (row),
10611 row->displays_text_p,
10612 row->ends_at_zv_p,
10613 row->fill_line_p,
10614 row->ends_in_middle_of_char_p,
10615 row->starts_in_middle_of_char_p,
10616 row->x,
10617 row->y,
10618 row->pixel_width);
10619 fprintf (stderr, "%9d %5d\n", row->start.overlay_string_index,
10620 row->end.overlay_string_index);
10621 fprintf (stderr, "%9d %5d\n",
10622 CHARPOS (row->start.string_pos),
10623 CHARPOS (row->end.string_pos));
10624 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
10625 row->end.dpvec_index);
10626
10627 if (with_glyphs_p)
10628 {
10629 struct glyph *glyph, *glyph_end;
10630 int prev_had_glyphs_p;
10631
10632 glyph = row->glyphs[TEXT_AREA];
10633 glyph_end = glyph + row->used[TEXT_AREA];
10634
10635 /* Glyph for a line end in text. */
10636 if (glyph == glyph_end && glyph->charpos > 0)
10637 ++glyph_end;
10638
10639 if (glyph < glyph_end)
10640 {
10641 fprintf (stderr, " Glyph Type Pos W Code C Face LR\n");
10642 prev_had_glyphs_p = 1;
10643 }
10644 else
10645 prev_had_glyphs_p = 0;
10646
10647 while (glyph < glyph_end)
10648 {
10649 if (glyph->type == CHAR_GLYPH)
10650 {
10651 fprintf (stderr,
10652 " %5d %4c %6d %3d 0x%05x %c %4d %1.1d%1.1d\n",
10653 glyph - row->glyphs[TEXT_AREA],
10654 'C',
10655 glyph->charpos,
10656 glyph->pixel_width,
10657 glyph->u.ch,
10658 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
10659 ? glyph->u.ch
10660 : '.'),
10661 glyph->face_id,
10662 glyph->left_box_line_p,
10663 glyph->right_box_line_p);
10664 }
10665 else if (glyph->type == STRETCH_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 'S',
10671 glyph->charpos,
10672 glyph->pixel_width,
10673 0,
10674 '.',
10675 glyph->face_id,
10676 glyph->left_box_line_p,
10677 glyph->right_box_line_p);
10678 }
10679 else if (glyph->type == IMAGE_GLYPH)
10680 {
10681 fprintf (stderr,
10682 " %5d %4c %6d %3d 0x%05x %c %4d %1.1d%1.1d\n",
10683 glyph - row->glyphs[TEXT_AREA],
10684 'I',
10685 glyph->charpos,
10686 glyph->pixel_width,
10687 glyph->u.img_id,
10688 '.',
10689 glyph->face_id,
10690 glyph->left_box_line_p,
10691 glyph->right_box_line_p);
10692 }
10693 ++glyph;
10694 }
10695 }
10696 }
10697
10698
10699 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
10700 Sdump_glyph_matrix, 0, 1, "p",
10701 "Dump the current matrix of the selected window to stderr.\n\
10702 Shows contents of glyph row structures. With non-nil optional\n\
10703 parameter WITH-GLYPHS-P, dump glyphs as well.")
10704 (with_glyphs_p)
10705 Lisp_Object with_glyphs_p;
10706 {
10707 struct window *w = XWINDOW (selected_window);
10708 struct buffer *buffer = XBUFFER (w->buffer);
10709
10710 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
10711 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
10712 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
10713 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
10714 fprintf (stderr, "=============================================\n");
10715 dump_glyph_matrix (w->current_matrix, !NILP (with_glyphs_p));
10716 return Qnil;
10717 }
10718
10719
10720 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 1, "",
10721 "Dump glyph row ROW to stderr.")
10722 (row)
10723 Lisp_Object row;
10724 {
10725 CHECK_NUMBER (row, 0);
10726 dump_glyph_row (XWINDOW (selected_window)->current_matrix, XINT (row), 1);
10727 return Qnil;
10728 }
10729
10730
10731 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row,
10732 0, 0, "", "")
10733 ()
10734 {
10735 struct frame *sf = SELECTED_FRAME ();
10736 struct glyph_matrix *m = (XWINDOW (sf->tool_bar_window)
10737 ->current_matrix);
10738 dump_glyph_row (m, 0, 1);
10739 return Qnil;
10740 }
10741
10742
10743 DEFUN ("trace-redisplay-toggle", Ftrace_redisplay_toggle,
10744 Strace_redisplay_toggle, 0, 0, "",
10745 "Toggle tracing of redisplay.")
10746 ()
10747 {
10748 trace_redisplay_p = !trace_redisplay_p;
10749 return Qnil;
10750 }
10751
10752
10753 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, 1, "",
10754 "Print STRING to stderr.")
10755 (string)
10756 Lisp_Object string;
10757 {
10758 CHECK_STRING (string, 0);
10759 fprintf (stderr, "%s", XSTRING (string)->data);
10760 return Qnil;
10761 }
10762
10763 #endif /* GLYPH_DEBUG */
10764
10765
10766 \f
10767 /***********************************************************************
10768 Building Desired Matrix Rows
10769 ***********************************************************************/
10770
10771 /* Return a temporary glyph row holding the glyphs of an overlay
10772 arrow. Only used for non-window-redisplay windows. */
10773
10774 static struct glyph_row *
10775 get_overlay_arrow_glyph_row (w)
10776 struct window *w;
10777 {
10778 struct frame *f = XFRAME (WINDOW_FRAME (w));
10779 struct buffer *buffer = XBUFFER (w->buffer);
10780 struct buffer *old = current_buffer;
10781 unsigned char *arrow_string = XSTRING (Voverlay_arrow_string)->data;
10782 int arrow_len = XSTRING (Voverlay_arrow_string)->size;
10783 unsigned char *arrow_end = arrow_string + arrow_len;
10784 unsigned char *p;
10785 struct it it;
10786 int multibyte_p;
10787 int n_glyphs_before;
10788
10789 set_buffer_temp (buffer);
10790 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
10791 it.glyph_row->used[TEXT_AREA] = 0;
10792 SET_TEXT_POS (it.position, 0, 0);
10793
10794 multibyte_p = !NILP (buffer->enable_multibyte_characters);
10795 p = arrow_string;
10796 while (p < arrow_end)
10797 {
10798 Lisp_Object face, ilisp;
10799
10800 /* Get the next character. */
10801 if (multibyte_p)
10802 it.c = string_char_and_length (p, arrow_len, &it.len);
10803 else
10804 it.c = *p, it.len = 1;
10805 p += it.len;
10806
10807 /* Get its face. */
10808 XSETFASTINT (ilisp, p - arrow_string);
10809 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
10810 it.face_id = compute_char_face (f, it.c, face);
10811
10812 /* Compute its width, get its glyphs. */
10813 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
10814 SET_TEXT_POS (it.position, -1, -1);
10815 PRODUCE_GLYPHS (&it);
10816
10817 /* If this character doesn't fit any more in the line, we have
10818 to remove some glyphs. */
10819 if (it.current_x > it.last_visible_x)
10820 {
10821 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
10822 break;
10823 }
10824 }
10825
10826 set_buffer_temp (old);
10827 return it.glyph_row;
10828 }
10829
10830
10831 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
10832 glyphs are only inserted for terminal frames since we can't really
10833 win with truncation glyphs when partially visible glyphs are
10834 involved. Which glyphs to insert is determined by
10835 produce_special_glyphs. */
10836
10837 static void
10838 insert_left_trunc_glyphs (it)
10839 struct it *it;
10840 {
10841 struct it truncate_it;
10842 struct glyph *from, *end, *to, *toend;
10843
10844 xassert (!FRAME_WINDOW_P (it->f));
10845
10846 /* Get the truncation glyphs. */
10847 truncate_it = *it;
10848 truncate_it.current_x = 0;
10849 truncate_it.face_id = DEFAULT_FACE_ID;
10850 truncate_it.glyph_row = &scratch_glyph_row;
10851 truncate_it.glyph_row->used[TEXT_AREA] = 0;
10852 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
10853 truncate_it.object = make_number (0);
10854 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
10855
10856 /* Overwrite glyphs from IT with truncation glyphs. */
10857 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
10858 end = from + truncate_it.glyph_row->used[TEXT_AREA];
10859 to = it->glyph_row->glyphs[TEXT_AREA];
10860 toend = to + it->glyph_row->used[TEXT_AREA];
10861
10862 while (from < end)
10863 *to++ = *from++;
10864
10865 /* There may be padding glyphs left over. Remove them. */
10866 from = to;
10867 while (from < toend && CHAR_GLYPH_PADDING_P (*from))
10868 ++from;
10869 while (from < toend)
10870 *to++ = *from++;
10871
10872 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
10873 }
10874
10875
10876 /* Compute the pixel height and width of IT->glyph_row.
10877
10878 Most of the time, ascent and height of a display line will be equal
10879 to the max_ascent and max_height values of the display iterator
10880 structure. This is not the case if
10881
10882 1. We hit ZV without displaying anything. In this case, max_ascent
10883 and max_height will be zero.
10884
10885 2. We have some glyphs that don't contribute to the line height.
10886 (The glyph row flag contributes_to_line_height_p is for future
10887 pixmap extensions).
10888
10889 The first case is easily covered by using default values because in
10890 these cases, the line height does not really matter, except that it
10891 must not be zero. */
10892
10893 static void
10894 compute_line_metrics (it)
10895 struct it *it;
10896 {
10897 struct glyph_row *row = it->glyph_row;
10898 int area, i;
10899
10900 if (FRAME_WINDOW_P (it->f))
10901 {
10902 int i, header_line_height;
10903
10904 /* The line may consist of one space only, that was added to
10905 place the cursor on it. If so, the row's height hasn't been
10906 computed yet. */
10907 if (row->height == 0)
10908 {
10909 if (it->max_ascent + it->max_descent == 0)
10910 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
10911 row->ascent = it->max_ascent;
10912 row->height = it->max_ascent + it->max_descent;
10913 row->phys_ascent = it->max_phys_ascent;
10914 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
10915 }
10916
10917 /* Compute the width of this line. */
10918 row->pixel_width = row->x;
10919 for (i = 0; i < row->used[TEXT_AREA]; ++i)
10920 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
10921
10922 xassert (row->pixel_width >= 0);
10923 xassert (row->ascent >= 0 && row->height > 0);
10924
10925 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
10926 || MATRIX_ROW_OVERLAPS_PRED_P (row));
10927
10928 /* If first line's physical ascent is larger than its logical
10929 ascent, use the physical ascent, and make the row taller.
10930 This makes accented characters fully visible. */
10931 if (row == it->w->desired_matrix->rows
10932 && row->phys_ascent > row->ascent)
10933 {
10934 row->height += row->phys_ascent - row->ascent;
10935 row->ascent = row->phys_ascent;
10936 }
10937
10938 /* Compute how much of the line is visible. */
10939 row->visible_height = row->height;
10940
10941 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (it->w);
10942 if (row->y < header_line_height)
10943 row->visible_height -= header_line_height - row->y;
10944 else
10945 {
10946 int max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
10947 if (row->y + row->height > max_y)
10948 row->visible_height -= row->y + row->height - max_y;
10949 }
10950 }
10951 else
10952 {
10953 row->pixel_width = row->used[TEXT_AREA];
10954 row->ascent = row->phys_ascent = 0;
10955 row->height = row->phys_height = row->visible_height = 1;
10956 }
10957
10958 /* Compute a hash code for this row. */
10959 row->hash = 0;
10960 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
10961 for (i = 0; i < row->used[area]; ++i)
10962 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
10963 + row->glyphs[area][i].u.val
10964 + row->glyphs[area][i].face_id
10965 + row->glyphs[area][i].padding_p
10966 + (row->glyphs[area][i].type << 2));
10967
10968 it->max_ascent = it->max_descent = 0;
10969 it->max_phys_ascent = it->max_phys_descent = 0;
10970 }
10971
10972
10973 /* Append one space to the glyph row of iterator IT if doing a
10974 window-based redisplay. DEFAULT_FACE_P non-zero means let the
10975 space have the default face, otherwise let it have the same face as
10976 IT->face_id. Value is non-zero if a space was added.
10977
10978 This function is called to make sure that there is always one glyph
10979 at the end of a glyph row that the cursor can be set on under
10980 window-systems. (If there weren't such a glyph we would not know
10981 how wide and tall a box cursor should be displayed).
10982
10983 At the same time this space let's a nicely handle clearing to the
10984 end of the line if the row ends in italic text. */
10985
10986 static int
10987 append_space (it, default_face_p)
10988 struct it *it;
10989 int default_face_p;
10990 {
10991 if (FRAME_WINDOW_P (it->f))
10992 {
10993 int n = it->glyph_row->used[TEXT_AREA];
10994
10995 if (it->glyph_row->glyphs[TEXT_AREA] + n
10996 < it->glyph_row->glyphs[1 + TEXT_AREA])
10997 {
10998 /* Save some values that must not be changed. */
10999 int saved_x = it->current_x;
11000 struct text_pos saved_pos;
11001 int saved_what = it->what;
11002 int saved_face_id = it->face_id;
11003 Lisp_Object saved_object;
11004 struct face *face;
11005
11006 saved_object = it->object;
11007 saved_pos = it->position;
11008
11009 it->what = IT_CHARACTER;
11010 bzero (&it->position, sizeof it->position);
11011 it->object = make_number (0);
11012 it->c = ' ';
11013 it->len = 1;
11014
11015 if (default_face_p)
11016 it->face_id = DEFAULT_FACE_ID;
11017 face = FACE_FROM_ID (it->f, it->face_id);
11018 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
11019
11020 PRODUCE_GLYPHS (it);
11021
11022 it->current_x = saved_x;
11023 it->object = saved_object;
11024 it->position = saved_pos;
11025 it->what = saved_what;
11026 it->face_id = saved_face_id;
11027 return 1;
11028 }
11029 }
11030
11031 return 0;
11032 }
11033
11034
11035 /* Extend the face of the last glyph in the text area of IT->glyph_row
11036 to the end of the display line. Called from display_line.
11037 If the glyph row is empty, add a space glyph to it so that we
11038 know the face to draw. Set the glyph row flag fill_line_p. */
11039
11040 static void
11041 extend_face_to_end_of_line (it)
11042 struct it *it;
11043 {
11044 struct face *face;
11045 struct frame *f = it->f;
11046
11047 /* If line is already filled, do nothing. */
11048 if (it->current_x >= it->last_visible_x)
11049 return;
11050
11051 /* Face extension extends the background and box of IT->face_id
11052 to the end of the line. If the background equals the background
11053 of the frame, we haven't to do anything. */
11054 face = FACE_FROM_ID (f, it->face_id);
11055 if (FRAME_WINDOW_P (f)
11056 && face->box == FACE_NO_BOX
11057 && face->background == FRAME_BACKGROUND_PIXEL (f)
11058 && !face->stipple)
11059 return;
11060
11061 /* Set the glyph row flag indicating that the face of the last glyph
11062 in the text area has to be drawn to the end of the text area. */
11063 it->glyph_row->fill_line_p = 1;
11064
11065 /* If current character of IT is not ASCII, make sure we have the
11066 ASCII face. This will be automatically undone the next time
11067 get_next_display_element returns a multibyte character. Note
11068 that the character will always be single byte in unibyte text. */
11069 if (!SINGLE_BYTE_CHAR_P (it->c))
11070 {
11071 it->face_id = FACE_FOR_CHAR (f, face, 0);
11072 }
11073
11074 if (FRAME_WINDOW_P (f))
11075 {
11076 /* If the row is empty, add a space with the current face of IT,
11077 so that we know which face to draw. */
11078 if (it->glyph_row->used[TEXT_AREA] == 0)
11079 {
11080 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
11081 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
11082 it->glyph_row->used[TEXT_AREA] = 1;
11083 }
11084 }
11085 else
11086 {
11087 /* Save some values that must not be changed. */
11088 int saved_x = it->current_x;
11089 struct text_pos saved_pos;
11090 Lisp_Object saved_object;
11091 int saved_what = it->what;
11092
11093 saved_object = it->object;
11094 saved_pos = it->position;
11095
11096 it->what = IT_CHARACTER;
11097 bzero (&it->position, sizeof it->position);
11098 it->object = make_number (0);
11099 it->c = ' ';
11100 it->len = 1;
11101
11102 PRODUCE_GLYPHS (it);
11103
11104 while (it->current_x <= it->last_visible_x)
11105 PRODUCE_GLYPHS (it);
11106
11107 /* Don't count these blanks really. It would let us insert a left
11108 truncation glyph below and make us set the cursor on them, maybe. */
11109 it->current_x = saved_x;
11110 it->object = saved_object;
11111 it->position = saved_pos;
11112 it->what = saved_what;
11113 }
11114 }
11115
11116
11117 /* Value is non-zero if text starting at CHARPOS in current_buffer is
11118 trailing whitespace. */
11119
11120 static int
11121 trailing_whitespace_p (charpos)
11122 int charpos;
11123 {
11124 int bytepos = CHAR_TO_BYTE (charpos);
11125 int c = 0;
11126
11127 while (bytepos < ZV_BYTE
11128 && (c = FETCH_CHAR (bytepos),
11129 c == ' ' || c == '\t'))
11130 ++bytepos;
11131
11132 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
11133 {
11134 if (bytepos != PT_BYTE)
11135 return 1;
11136 }
11137 return 0;
11138 }
11139
11140
11141 /* Highlight trailing whitespace, if any, in ROW. */
11142
11143 void
11144 highlight_trailing_whitespace (f, row)
11145 struct frame *f;
11146 struct glyph_row *row;
11147 {
11148 int used = row->used[TEXT_AREA];
11149
11150 if (used)
11151 {
11152 struct glyph *start = row->glyphs[TEXT_AREA];
11153 struct glyph *glyph = start + used - 1;
11154
11155 /* Skip over the space glyph inserted to display the
11156 cursor at the end of a line. */
11157 if (glyph->type == CHAR_GLYPH
11158 && glyph->u.ch == ' '
11159 && INTEGERP (glyph->object))
11160 --glyph;
11161
11162 /* If last glyph is a space or stretch, and it's trailing
11163 whitespace, set the face of all trailing whitespace glyphs in
11164 IT->glyph_row to `trailing-whitespace'. */
11165 if (glyph >= start
11166 && BUFFERP (glyph->object)
11167 && (glyph->type == STRETCH_GLYPH
11168 || (glyph->type == CHAR_GLYPH
11169 && glyph->u.ch == ' '))
11170 && trailing_whitespace_p (glyph->charpos))
11171 {
11172 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
11173
11174 while (glyph >= start
11175 && BUFFERP (glyph->object)
11176 && (glyph->type == STRETCH_GLYPH
11177 || (glyph->type == CHAR_GLYPH
11178 && glyph->u.ch == ' ')))
11179 (glyph--)->face_id = face_id;
11180 }
11181 }
11182 }
11183
11184
11185 /* Construct the glyph row IT->glyph_row in the desired matrix of
11186 IT->w from text at the current position of IT. See dispextern.h
11187 for an overview of struct it. Value is non-zero if
11188 IT->glyph_row displays text, as opposed to a line displaying ZV
11189 only. */
11190
11191 static int
11192 display_line (it)
11193 struct it *it;
11194 {
11195 struct glyph_row *row = it->glyph_row;
11196
11197 /* We always start displaying at hpos zero even if hscrolled. */
11198 xassert (it->hpos == 0 && it->current_x == 0);
11199
11200 /* We must not display in a row that's not a text row. */
11201 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
11202 < it->w->desired_matrix->nrows);
11203
11204 /* Is IT->w showing the region? */
11205 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
11206
11207 /* Clear the result glyph row and enable it. */
11208 prepare_desired_row (row);
11209
11210 row->y = it->current_y;
11211 row->start = it->current;
11212 row->continuation_lines_width = it->continuation_lines_width;
11213 row->displays_text_p = 1;
11214 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
11215 it->starts_in_middle_of_char_p = 0;
11216
11217 /* Arrange the overlays nicely for our purposes. Usually, we call
11218 display_line on only one line at a time, in which case this
11219 can't really hurt too much, or we call it on lines which appear
11220 one after another in the buffer, in which case all calls to
11221 recenter_overlay_lists but the first will be pretty cheap. */
11222 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
11223
11224 /* Move over display elements that are not visible because we are
11225 hscrolled. This may stop at an x-position < IT->first_visible_x
11226 if the first glyph is partially visible or if we hit a line end. */
11227 if (it->current_x < it->first_visible_x)
11228 move_it_in_display_line_to (it, ZV, it->first_visible_x,
11229 MOVE_TO_POS | MOVE_TO_X);
11230
11231 /* Get the initial row height. This is either the height of the
11232 text hscrolled, if there is any, or zero. */
11233 row->ascent = it->max_ascent;
11234 row->height = it->max_ascent + it->max_descent;
11235 row->phys_ascent = it->max_phys_ascent;
11236 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
11237
11238 /* Loop generating characters. The loop is left with IT on the next
11239 character to display. */
11240 while (1)
11241 {
11242 int n_glyphs_before, hpos_before, x_before;
11243 int x, i, nglyphs;
11244 int ascent, descent, phys_ascent, phys_descent;
11245
11246 /* Retrieve the next thing to display. Value is zero if end of
11247 buffer reached. */
11248 if (!get_next_display_element (it))
11249 {
11250 /* Maybe add a space at the end of this line that is used to
11251 display the cursor there under X. Set the charpos of the
11252 first glyph of blank lines not corresponding to any text
11253 to -1. */
11254 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
11255 || row->used[TEXT_AREA] == 0)
11256 {
11257 row->glyphs[TEXT_AREA]->charpos = -1;
11258 row->displays_text_p = 0;
11259
11260 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines))
11261 row->indicate_empty_line_p = 1;
11262 }
11263
11264 it->continuation_lines_width = 0;
11265 row->ends_at_zv_p = 1;
11266 break;
11267 }
11268
11269 /* Now, get the metrics of what we want to display. This also
11270 generates glyphs in `row' (which is IT->glyph_row). */
11271 n_glyphs_before = row->used[TEXT_AREA];
11272 x = it->current_x;
11273
11274 /* Remember the line height so far in case the next element doesn't
11275 fit on the line. */
11276 if (!it->truncate_lines_p)
11277 {
11278 ascent = it->max_ascent;
11279 descent = it->max_descent;
11280 phys_ascent = it->max_phys_ascent;
11281 phys_descent = it->max_phys_descent;
11282 }
11283
11284 PRODUCE_GLYPHS (it);
11285
11286 /* If this display element was in marginal areas, continue with
11287 the next one. */
11288 if (it->area != TEXT_AREA)
11289 {
11290 row->ascent = max (row->ascent, it->max_ascent);
11291 row->height = max (row->height, it->max_ascent + it->max_descent);
11292 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
11293 row->phys_height = max (row->phys_height,
11294 it->max_phys_ascent + it->max_phys_descent);
11295 set_iterator_to_next (it);
11296 continue;
11297 }
11298
11299 /* Does the display element fit on the line? If we truncate
11300 lines, we should draw past the right edge of the window. If
11301 we don't truncate, we want to stop so that we can display the
11302 continuation glyph before the right margin. If lines are
11303 continued, there are two possible strategies for characters
11304 resulting in more than 1 glyph (e.g. tabs): Display as many
11305 glyphs as possible in this line and leave the rest for the
11306 continuation line, or display the whole element in the next
11307 line. Original redisplay did the former, so we do it also. */
11308 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11309 hpos_before = it->hpos;
11310 x_before = x;
11311
11312 if (nglyphs == 1
11313 && it->current_x < it->last_visible_x)
11314 {
11315 ++it->hpos;
11316 row->ascent = max (row->ascent, it->max_ascent);
11317 row->height = max (row->height, it->max_ascent + it->max_descent);
11318 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
11319 row->phys_height = max (row->phys_height,
11320 it->max_phys_ascent + it->max_phys_descent);
11321 if (it->current_x - it->pixel_width < it->first_visible_x)
11322 row->x = x - it->first_visible_x;
11323 }
11324 else
11325 {
11326 int new_x;
11327 struct glyph *glyph;
11328
11329 for (i = 0; i < nglyphs; ++i, x = new_x)
11330 {
11331 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11332 new_x = x + glyph->pixel_width;
11333
11334 if (/* Lines are continued. */
11335 !it->truncate_lines_p
11336 && (/* Glyph doesn't fit on the line. */
11337 new_x > it->last_visible_x
11338 /* Or it fits exactly on a window system frame. */
11339 || (new_x == it->last_visible_x
11340 && FRAME_WINDOW_P (it->f))))
11341 {
11342 /* End of a continued line. */
11343
11344 if (it->hpos == 0
11345 || (new_x == it->last_visible_x
11346 && FRAME_WINDOW_P (it->f)))
11347 {
11348 /* Current glyph is the only one on the line or
11349 fits exactly on the line. We must continue
11350 the line because we can't draw the cursor
11351 after the glyph. */
11352 row->continued_p = 1;
11353 it->current_x = new_x;
11354 it->continuation_lines_width += new_x;
11355 ++it->hpos;
11356 if (i == nglyphs - 1)
11357 set_iterator_to_next (it);
11358 }
11359 else if (CHAR_GLYPH_PADDING_P (*glyph)
11360 && !FRAME_WINDOW_P (it->f))
11361 {
11362 /* A padding glyph that doesn't fit on this line.
11363 This means the whole character doesn't fit
11364 on the line. */
11365 row->used[TEXT_AREA] = n_glyphs_before;
11366
11367 /* Fill the rest of the row with continuation
11368 glyphs like in 20.x. */
11369 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
11370 < row->glyphs[1 + TEXT_AREA])
11371 produce_special_glyphs (it, IT_CONTINUATION);
11372
11373 row->continued_p = 1;
11374 it->current_x = x_before;
11375 it->continuation_lines_width += x_before;
11376
11377 /* Restore the height to what it was before the
11378 element not fitting on the line. */
11379 it->max_ascent = ascent;
11380 it->max_descent = descent;
11381 it->max_phys_ascent = phys_ascent;
11382 it->max_phys_descent = phys_descent;
11383 }
11384 else
11385 {
11386 /* Display element draws past the right edge of
11387 the window. Restore positions to values
11388 before the element. The next line starts
11389 with current_x before the glyph that could
11390 not be displayed, so that TAB works right. */
11391 row->used[TEXT_AREA] = n_glyphs_before + i;
11392
11393 /* Display continuation glyphs. */
11394 if (!FRAME_WINDOW_P (it->f))
11395 produce_special_glyphs (it, IT_CONTINUATION);
11396 row->continued_p = 1;
11397
11398 it->current_x = x;
11399 it->continuation_lines_width += x;
11400 if (nglyphs > 1 && i > 0)
11401 {
11402 row->ends_in_middle_of_char_p = 1;
11403 it->starts_in_middle_of_char_p = 1;
11404 }
11405
11406 /* Restore the height to what it was before the
11407 element not fitting on the line. */
11408 it->max_ascent = ascent;
11409 it->max_descent = descent;
11410 it->max_phys_ascent = phys_ascent;
11411 it->max_phys_descent = phys_descent;
11412 }
11413
11414 break;
11415 }
11416 else if (new_x > it->first_visible_x)
11417 {
11418 /* Increment number of glyphs actually displayed. */
11419 ++it->hpos;
11420
11421 if (x < it->first_visible_x)
11422 /* Glyph is partially visible, i.e. row starts at
11423 negative X position. */
11424 row->x = x - it->first_visible_x;
11425 }
11426 else
11427 {
11428 /* Glyph is completely off the left margin of the
11429 window. This should not happen because of the
11430 move_it_in_display_line at the start of
11431 this function. */
11432 abort ();
11433 }
11434 }
11435
11436 row->ascent = max (row->ascent, it->max_ascent);
11437 row->height = max (row->height, it->max_ascent + it->max_descent);
11438 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
11439 row->phys_height = max (row->phys_height,
11440 it->max_phys_ascent + it->max_phys_descent);
11441
11442 /* End of this display line if row is continued. */
11443 if (row->continued_p)
11444 break;
11445 }
11446
11447 /* Is this a line end? If yes, we're also done, after making
11448 sure that a non-default face is extended up to the right
11449 margin of the window. */
11450 if (ITERATOR_AT_END_OF_LINE_P (it))
11451 {
11452 int used_before = row->used[TEXT_AREA];
11453
11454 /* Add a space at the end of the line that is used to
11455 display the cursor there. */
11456 append_space (it, 0);
11457
11458 /* Extend the face to the end of the line. */
11459 extend_face_to_end_of_line (it);
11460
11461 /* Make sure we have the position. */
11462 if (used_before == 0)
11463 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
11464
11465 /* Consume the line end. This skips over invisible lines. */
11466 set_iterator_to_next (it);
11467 it->continuation_lines_width = 0;
11468 break;
11469 }
11470
11471 /* Proceed with next display element. Note that this skips
11472 over lines invisible because of selective display. */
11473 set_iterator_to_next (it);
11474
11475 /* If we truncate lines, we are done when the last displayed
11476 glyphs reach past the right margin of the window. */
11477 if (it->truncate_lines_p
11478 && (FRAME_WINDOW_P (it->f)
11479 ? (it->current_x >= it->last_visible_x)
11480 : (it->current_x > it->last_visible_x)))
11481 {
11482 /* Maybe add truncation glyphs. */
11483 if (!FRAME_WINDOW_P (it->f))
11484 {
11485 --it->glyph_row->used[TEXT_AREA];
11486 produce_special_glyphs (it, IT_TRUNCATION);
11487 }
11488
11489 row->truncated_on_right_p = 1;
11490 it->continuation_lines_width = 0;
11491 reseat_at_next_visible_line_start (it, 0);
11492 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
11493 it->hpos = hpos_before;
11494 it->current_x = x_before;
11495 break;
11496 }
11497 }
11498
11499 /* If line is not empty and hscrolled, maybe insert truncation glyphs
11500 at the left window margin. */
11501 if (it->first_visible_x
11502 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
11503 {
11504 if (!FRAME_WINDOW_P (it->f))
11505 insert_left_trunc_glyphs (it);
11506 row->truncated_on_left_p = 1;
11507 }
11508
11509 /* If the start of this line is the overlay arrow-position, then
11510 mark this glyph row as the one containing the overlay arrow.
11511 This is clearly a mess with variable size fonts. It would be
11512 better to let it be displayed like cursors under X. */
11513 if (MARKERP (Voverlay_arrow_position)
11514 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
11515 && (MATRIX_ROW_START_CHARPOS (row)
11516 == marker_position (Voverlay_arrow_position))
11517 && STRINGP (Voverlay_arrow_string)
11518 && ! overlay_arrow_seen)
11519 {
11520 /* Overlay arrow in window redisplay is a bitmap. */
11521 if (!FRAME_WINDOW_P (it->f))
11522 {
11523 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
11524 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
11525 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
11526 struct glyph *p = row->glyphs[TEXT_AREA];
11527 struct glyph *p2, *end;
11528
11529 /* Copy the arrow glyphs. */
11530 while (glyph < arrow_end)
11531 *p++ = *glyph++;
11532
11533 /* Throw away padding glyphs. */
11534 p2 = p;
11535 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
11536 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
11537 ++p2;
11538 if (p2 > p)
11539 {
11540 while (p2 < end)
11541 *p++ = *p2++;
11542 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
11543 }
11544 }
11545
11546 overlay_arrow_seen = 1;
11547 row->overlay_arrow_p = 1;
11548 }
11549
11550 /* Compute pixel dimensions of this line. */
11551 compute_line_metrics (it);
11552
11553 /* Remember the position at which this line ends. */
11554 row->end = it->current;
11555
11556 /* Maybe set the cursor. */
11557 if (it->w->cursor.vpos < 0
11558 && PT >= MATRIX_ROW_START_CHARPOS (row)
11559 && PT <= MATRIX_ROW_END_CHARPOS (row))
11560 {
11561 /* Also see redisplay_window, case cursor movement in unchanged
11562 window. */
11563 if (MATRIX_ROW_END_CHARPOS (row) == PT
11564 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
11565 && !row->ends_at_zv_p)
11566 ;
11567 else
11568 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
11569 }
11570
11571 /* Highlight trailing whitespace. */
11572 if (!NILP (Vshow_trailing_whitespace))
11573 highlight_trailing_whitespace (it->f, it->glyph_row);
11574
11575 /* Prepare for the next line. This line starts horizontally at (X
11576 HPOS) = (0 0). Vertical positions are incremented. As a
11577 convenience for the caller, IT->glyph_row is set to the next
11578 row to be used. */
11579 it->current_x = it->hpos = 0;
11580 it->current_y += row->height;
11581 ++it->vpos;
11582 ++it->glyph_row;
11583 return row->displays_text_p;
11584 }
11585
11586
11587 \f
11588 /***********************************************************************
11589 Menu Bar
11590 ***********************************************************************/
11591
11592 /* Redisplay the menu bar in the frame for window W.
11593
11594 The menu bar of X frames that don't have X toolkit support is
11595 displayed in a special window W->frame->menu_bar_window.
11596
11597 The menu bar of terminal frames is treated specially as far as
11598 glyph matrices are concerned. Menu bar lines are not part of
11599 windows, so the update is done directly on the frame matrix rows
11600 for the menu bar. */
11601
11602 static void
11603 display_menu_bar (w)
11604 struct window *w;
11605 {
11606 struct frame *f = XFRAME (WINDOW_FRAME (w));
11607 struct it it;
11608 Lisp_Object items;
11609 int i;
11610
11611 /* Don't do all this for graphical frames. */
11612 #ifdef HAVE_NTGUI
11613 if (!NILP (Vwindow_system))
11614 return;
11615 #endif
11616 #ifdef USE_X_TOOLKIT
11617 if (FRAME_X_P (f))
11618 return;
11619 #endif
11620
11621 #ifdef USE_X_TOOLKIT
11622 xassert (!FRAME_WINDOW_P (f));
11623 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
11624 it.first_visible_x = 0;
11625 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
11626 #else /* not USE_X_TOOLKIT */
11627 if (FRAME_WINDOW_P (f))
11628 {
11629 /* Menu bar lines are displayed in the desired matrix of the
11630 dummy window menu_bar_window. */
11631 struct window *menu_w;
11632 xassert (WINDOWP (f->menu_bar_window));
11633 menu_w = XWINDOW (f->menu_bar_window);
11634 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
11635 MENU_FACE_ID);
11636 it.first_visible_x = 0;
11637 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
11638 }
11639 else
11640 {
11641 /* This is a TTY frame, i.e. character hpos/vpos are used as
11642 pixel x/y. */
11643 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
11644 MENU_FACE_ID);
11645 it.first_visible_x = 0;
11646 it.last_visible_x = FRAME_WIDTH (f);
11647 }
11648 #endif /* not USE_X_TOOLKIT */
11649
11650 /* Clear all rows of the menu bar. */
11651 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
11652 {
11653 struct glyph_row *row = it.glyph_row + i;
11654 clear_glyph_row (row);
11655 row->enabled_p = 1;
11656 row->full_width_p = 1;
11657 }
11658
11659 /* Make the first line of the menu bar appear in reverse video. */
11660 it.glyph_row->inverse_p = mode_line_inverse_video != 0;
11661
11662 /* Display all items of the menu bar. */
11663 items = FRAME_MENU_BAR_ITEMS (it.f);
11664 for (i = 0; i < XVECTOR (items)->size; i += 4)
11665 {
11666 Lisp_Object string;
11667
11668 /* Stop at nil string. */
11669 string = XVECTOR (items)->contents[i + 1];
11670 if (NILP (string))
11671 break;
11672
11673 /* Remember where item was displayed. */
11674 XSETFASTINT (XVECTOR (items)->contents[i + 3], it.hpos);
11675
11676 /* Display the item, pad with one space. */
11677 if (it.current_x < it.last_visible_x)
11678 display_string (NULL, string, Qnil, 0, 0, &it,
11679 XSTRING (string)->size + 1, 0, 0, -1);
11680 }
11681
11682 /* Fill out the line with spaces. */
11683 if (it.current_x < it.last_visible_x)
11684 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
11685
11686 /* Compute the total height of the lines. */
11687 compute_line_metrics (&it);
11688 }
11689
11690
11691 \f
11692 /***********************************************************************
11693 Mode Line
11694 ***********************************************************************/
11695
11696 /* Display the mode and/or top line of window W. */
11697
11698 static void
11699 display_mode_lines (w)
11700 struct window *w;
11701 {
11702 /* These will be set while the mode line specs are processed. */
11703 line_number_displayed = 0;
11704 w->column_number_displayed = Qnil;
11705
11706 if (WINDOW_WANTS_MODELINE_P (w))
11707 display_mode_line (w, MODE_LINE_FACE_ID,
11708 current_buffer->mode_line_format);
11709
11710 if (WINDOW_WANTS_HEADER_LINE_P (w))
11711 display_mode_line (w, HEADER_LINE_FACE_ID,
11712 current_buffer->header_line_format);
11713 }
11714
11715
11716 /* Display mode or top line of window W. FACE_ID specifies which line
11717 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
11718 FORMAT is the mode line format to display. */
11719
11720 static void
11721 display_mode_line (w, face_id, format)
11722 struct window *w;
11723 enum face_id face_id;
11724 Lisp_Object format;
11725 {
11726 struct it it;
11727 struct face *face;
11728
11729 init_iterator (&it, w, -1, -1, NULL, face_id);
11730 prepare_desired_row (it.glyph_row);
11731
11732 /* Temporarily make frame's keyboard the current kboard so that
11733 kboard-local variables in the mode_line_format will get the right
11734 values. */
11735 push_frame_kboard (it.f);
11736 display_mode_element (&it, 0, 0, 0, format);
11737 pop_frame_kboard ();
11738
11739 /* Fill up with spaces. */
11740 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
11741
11742 compute_line_metrics (&it);
11743 it.glyph_row->full_width_p = 1;
11744 it.glyph_row->mode_line_p = 1;
11745 it.glyph_row->inverse_p = mode_line_inverse_video != 0;
11746 it.glyph_row->continued_p = 0;
11747 it.glyph_row->truncated_on_left_p = 0;
11748 it.glyph_row->truncated_on_right_p = 0;
11749
11750 /* Make a 3D mode-line have a shadow at its right end. */
11751 face = FACE_FROM_ID (it.f, face_id);
11752 extend_face_to_end_of_line (&it);
11753 if (face->box != FACE_NO_BOX)
11754 {
11755 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
11756 + it.glyph_row->used[TEXT_AREA] - 1);
11757 last->right_box_line_p = 1;
11758 }
11759 }
11760
11761
11762 /* Contribute ELT to the mode line for window IT->w. How it
11763 translates into text depends on its data type.
11764
11765 IT describes the display environment in which we display, as usual.
11766
11767 DEPTH is the depth in recursion. It is used to prevent
11768 infinite recursion here.
11769
11770 FIELD_WIDTH is the number of characters the display of ELT should
11771 occupy in the mode line, and PRECISION is the maximum number of
11772 characters to display from ELT's representation. See
11773 display_string for details. *
11774
11775 Returns the hpos of the end of the text generated by ELT. */
11776
11777 static int
11778 display_mode_element (it, depth, field_width, precision, elt)
11779 struct it *it;
11780 int depth;
11781 int field_width, precision;
11782 Lisp_Object elt;
11783 {
11784 int n = 0, field, prec;
11785
11786 tail_recurse:
11787 if (depth > 10)
11788 goto invalid;
11789
11790 depth++;
11791
11792 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
11793 {
11794 case Lisp_String:
11795 {
11796 /* A string: output it and check for %-constructs within it. */
11797 unsigned char c;
11798 unsigned char *this = XSTRING (elt)->data;
11799 unsigned char *lisp_string = this;
11800
11801 while ((precision <= 0 || n < precision)
11802 && *this
11803 && (frame_title_ptr
11804 || it->current_x < it->last_visible_x))
11805 {
11806 unsigned char *last = this;
11807
11808 /* Advance to end of string or next format specifier. */
11809 while ((c = *this++) != '\0' && c != '%')
11810 ;
11811
11812 if (this - 1 != last)
11813 {
11814 /* Output to end of string or up to '%'. Field width
11815 is length of string. Don't output more than
11816 PRECISION allows us. */
11817 prec = --this - last;
11818 if (precision > 0 && prec > precision - n)
11819 prec = precision - n;
11820
11821 if (frame_title_ptr)
11822 n += store_frame_title (last, prec, prec);
11823 else
11824 n += display_string (NULL, elt, Qnil, 0, last - lisp_string,
11825 it, 0, prec, 0, -1);
11826 }
11827 else /* c == '%' */
11828 {
11829 unsigned char *percent_position = this;
11830
11831 /* Get the specified minimum width. Zero means
11832 don't pad. */
11833 field = 0;
11834 while ((c = *this++) >= '0' && c <= '9')
11835 field = field * 10 + c - '0';
11836
11837 /* Don't pad beyond the total padding allowed. */
11838 if (field_width - n > 0 && field > field_width - n)
11839 field = field_width - n;
11840
11841 /* Note that either PRECISION <= 0 or N < PRECISION. */
11842 prec = precision - n;
11843
11844 if (c == 'M')
11845 n += display_mode_element (it, depth, field, prec,
11846 Vglobal_mode_string);
11847 else if (c != 0)
11848 {
11849 unsigned char *spec
11850 = decode_mode_spec (it->w, c, field, prec);
11851
11852 if (frame_title_ptr)
11853 n += store_frame_title (spec, field, prec);
11854 else
11855 {
11856 int nglyphs_before
11857 = it->glyph_row->used[TEXT_AREA];
11858 int charpos
11859 = percent_position - XSTRING (elt)->data;
11860 int nwritten
11861 = display_string (spec, Qnil, elt, charpos, 0, it,
11862 field, prec, 0, -1);
11863
11864 /* Assign to the glyphs written above the
11865 string where the `%x' came from, position
11866 of the `%'. */
11867 if (nwritten > 0)
11868 {
11869 struct glyph *glyph
11870 = (it->glyph_row->glyphs[TEXT_AREA]
11871 + nglyphs_before);
11872 int i;
11873
11874 for (i = 0; i < nwritten; ++i)
11875 {
11876 glyph[i].object = elt;
11877 glyph[i].charpos = charpos;
11878 }
11879
11880 n += nwritten;
11881 }
11882 }
11883 }
11884 }
11885 }
11886 }
11887 break;
11888
11889 case Lisp_Symbol:
11890 /* A symbol: process the value of the symbol recursively
11891 as if it appeared here directly. Avoid error if symbol void.
11892 Special case: if value of symbol is a string, output the string
11893 literally. */
11894 {
11895 register Lisp_Object tem;
11896 tem = Fboundp (elt);
11897 if (!NILP (tem))
11898 {
11899 tem = Fsymbol_value (elt);
11900 /* If value is a string, output that string literally:
11901 don't check for % within it. */
11902 if (STRINGP (tem))
11903 {
11904 prec = XSTRING (tem)->size;
11905 if (precision > 0 && prec > precision - n)
11906 prec = precision - n;
11907 if (frame_title_ptr)
11908 n += store_frame_title (XSTRING (tem)->data, -1, prec);
11909 else
11910 n += display_string (NULL, tem, Qnil, 0, 0, it,
11911 0, prec, 0, -1);
11912 }
11913 else if (!EQ (tem, elt))
11914 {
11915 /* Give up right away for nil or t. */
11916 elt = tem;
11917 goto tail_recurse;
11918 }
11919 }
11920 }
11921 break;
11922
11923 case Lisp_Cons:
11924 {
11925 register Lisp_Object car, tem;
11926
11927 /* A cons cell: three distinct cases.
11928 If first element is a string or a cons, process all the elements
11929 and effectively concatenate them.
11930 If first element is a negative number, truncate displaying cdr to
11931 at most that many characters. If positive, pad (with spaces)
11932 to at least that many characters.
11933 If first element is a symbol, process the cadr or caddr recursively
11934 according to whether the symbol's value is non-nil or nil. */
11935 car = XCAR (elt);
11936 if (EQ (car, QCeval) && CONSP (XCDR (elt)))
11937 {
11938 /* An element of the form (:eval FORM) means evaluate FORM
11939 and use the result as mode line elements. */
11940 struct gcpro gcpro1;
11941 Lisp_Object spec;
11942
11943 spec = eval_form (XCAR (XCDR (elt)));
11944 GCPRO1 (spec);
11945 n += display_mode_element (it, depth, field_width - n,
11946 precision - n, spec);
11947 UNGCPRO;
11948 }
11949 else if (SYMBOLP (car))
11950 {
11951 tem = Fboundp (car);
11952 elt = XCDR (elt);
11953 if (!CONSP (elt))
11954 goto invalid;
11955 /* elt is now the cdr, and we know it is a cons cell.
11956 Use its car if CAR has a non-nil value. */
11957 if (!NILP (tem))
11958 {
11959 tem = Fsymbol_value (car);
11960 if (!NILP (tem))
11961 {
11962 elt = XCAR (elt);
11963 goto tail_recurse;
11964 }
11965 }
11966 /* Symbol's value is nil (or symbol is unbound)
11967 Get the cddr of the original list
11968 and if possible find the caddr and use that. */
11969 elt = XCDR (elt);
11970 if (NILP (elt))
11971 break;
11972 else if (!CONSP (elt))
11973 goto invalid;
11974 elt = XCAR (elt);
11975 goto tail_recurse;
11976 }
11977 else if (INTEGERP (car))
11978 {
11979 register int lim = XINT (car);
11980 elt = XCDR (elt);
11981 if (lim < 0)
11982 {
11983 /* Negative int means reduce maximum width. */
11984 if (precision <= 0)
11985 precision = -lim;
11986 else
11987 precision = min (precision, -lim);
11988 }
11989 else if (lim > 0)
11990 {
11991 /* Padding specified. Don't let it be more than
11992 current maximum. */
11993 if (precision > 0)
11994 lim = min (precision, lim);
11995
11996 /* If that's more padding than already wanted, queue it.
11997 But don't reduce padding already specified even if
11998 that is beyond the current truncation point. */
11999 field_width = max (lim, field_width);
12000 }
12001 goto tail_recurse;
12002 }
12003 else if (STRINGP (car) || CONSP (car))
12004 {
12005 register int limit = 50;
12006 /* Limit is to protect against circular lists. */
12007 while (CONSP (elt)
12008 && --limit > 0
12009 && (precision <= 0 || n < precision))
12010 {
12011 n += display_mode_element (it, depth, field_width - n,
12012 precision - n, XCAR (elt));
12013 elt = XCDR (elt);
12014 }
12015 }
12016 }
12017 break;
12018
12019 default:
12020 invalid:
12021 if (frame_title_ptr)
12022 n += store_frame_title ("*invalid*", 0, precision - n);
12023 else
12024 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
12025 precision - n, 0, 0);
12026 return n;
12027 }
12028
12029 /* Pad to FIELD_WIDTH. */
12030 if (field_width > 0 && n < field_width)
12031 {
12032 if (frame_title_ptr)
12033 n += store_frame_title ("", field_width - n, 0);
12034 else
12035 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
12036 0, 0, 0);
12037 }
12038
12039 return n;
12040 }
12041
12042
12043 /* Write a null-terminated, right justified decimal representation of
12044 the positive integer D to BUF using a minimal field width WIDTH. */
12045
12046 static void
12047 pint2str (buf, width, d)
12048 register char *buf;
12049 register int width;
12050 register int d;
12051 {
12052 register char *p = buf;
12053
12054 if (d <= 0)
12055 *p++ = '0';
12056 else
12057 {
12058 while (d > 0)
12059 {
12060 *p++ = d % 10 + '0';
12061 d /= 10;
12062 }
12063 }
12064
12065 for (width -= (int) (p - buf); width > 0; --width)
12066 *p++ = ' ';
12067 *p-- = '\0';
12068 while (p > buf)
12069 {
12070 d = *buf;
12071 *buf++ = *p;
12072 *p-- = d;
12073 }
12074 }
12075
12076 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
12077 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
12078 type of CODING_SYSTEM. Return updated pointer into BUF. */
12079
12080 static unsigned char invalid_eol_type[] = "(*invalid*)";
12081
12082 static char *
12083 decode_mode_spec_coding (coding_system, buf, eol_flag)
12084 Lisp_Object coding_system;
12085 register char *buf;
12086 int eol_flag;
12087 {
12088 Lisp_Object val;
12089 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
12090 unsigned char *eol_str;
12091 int eol_str_len;
12092 /* The EOL conversion we are using. */
12093 Lisp_Object eoltype;
12094
12095 val = Fget (coding_system, Qcoding_system);
12096
12097 if (!VECTORP (val)) /* Not yet decided. */
12098 {
12099 if (multibyte)
12100 *buf++ = '-';
12101 if (eol_flag)
12102 eoltype = eol_mnemonic_undecided;
12103 /* Don't mention EOL conversion if it isn't decided. */
12104 }
12105 else
12106 {
12107 Lisp_Object eolvalue;
12108
12109 eolvalue = Fget (coding_system, Qeol_type);
12110
12111 if (multibyte)
12112 *buf++ = XFASTINT (XVECTOR (val)->contents[1]);
12113
12114 if (eol_flag)
12115 {
12116 /* The EOL conversion that is normal on this system. */
12117
12118 if (NILP (eolvalue)) /* Not yet decided. */
12119 eoltype = eol_mnemonic_undecided;
12120 else if (VECTORP (eolvalue)) /* Not yet decided. */
12121 eoltype = eol_mnemonic_undecided;
12122 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
12123 eoltype = (XFASTINT (eolvalue) == 0
12124 ? eol_mnemonic_unix
12125 : (XFASTINT (eolvalue) == 1
12126 ? eol_mnemonic_dos : eol_mnemonic_mac));
12127 }
12128 }
12129
12130 if (eol_flag)
12131 {
12132 /* Mention the EOL conversion if it is not the usual one. */
12133 if (STRINGP (eoltype))
12134 {
12135 eol_str = XSTRING (eoltype)->data;
12136 eol_str_len = XSTRING (eoltype)->size;
12137 }
12138 else if (INTEGERP (eoltype)
12139 && CHAR_VALID_P (XINT (eoltype), 0))
12140 {
12141 eol_str = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
12142 eol_str_len = CHAR_STRING (XINT (eoltype), eol_str);
12143 }
12144 else
12145 {
12146 eol_str = invalid_eol_type;
12147 eol_str_len = sizeof (invalid_eol_type) - 1;
12148 }
12149 bcopy (eol_str, buf, eol_str_len);
12150 buf += eol_str_len;
12151 }
12152
12153 return buf;
12154 }
12155
12156 /* Return a string for the output of a mode line %-spec for window W,
12157 generated by character C. PRECISION >= 0 means don't return a
12158 string longer than that value. FIELD_WIDTH > 0 means pad the
12159 string returned with spaces to that value. */
12160
12161 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
12162
12163 static char *
12164 decode_mode_spec (w, c, field_width, precision)
12165 struct window *w;
12166 register int c;
12167 int field_width, precision;
12168 {
12169 Lisp_Object obj;
12170 struct frame *f = XFRAME (WINDOW_FRAME (w));
12171 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
12172 struct buffer *b = XBUFFER (w->buffer);
12173
12174 obj = Qnil;
12175
12176 switch (c)
12177 {
12178 case '*':
12179 if (!NILP (b->read_only))
12180 return "%";
12181 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
12182 return "*";
12183 return "-";
12184
12185 case '+':
12186 /* This differs from %* only for a modified read-only buffer. */
12187 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
12188 return "*";
12189 if (!NILP (b->read_only))
12190 return "%";
12191 return "-";
12192
12193 case '&':
12194 /* This differs from %* in ignoring read-only-ness. */
12195 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
12196 return "*";
12197 return "-";
12198
12199 case '%':
12200 return "%";
12201
12202 case '[':
12203 {
12204 int i;
12205 char *p;
12206
12207 if (command_loop_level > 5)
12208 return "[[[... ";
12209 p = decode_mode_spec_buf;
12210 for (i = 0; i < command_loop_level; i++)
12211 *p++ = '[';
12212 *p = 0;
12213 return decode_mode_spec_buf;
12214 }
12215
12216 case ']':
12217 {
12218 int i;
12219 char *p;
12220
12221 if (command_loop_level > 5)
12222 return " ...]]]";
12223 p = decode_mode_spec_buf;
12224 for (i = 0; i < command_loop_level; i++)
12225 *p++ = ']';
12226 *p = 0;
12227 return decode_mode_spec_buf;
12228 }
12229
12230 case '-':
12231 {
12232 register int i;
12233
12234 /* Let lots_of_dashes be a string of infinite length. */
12235 if (field_width <= 0
12236 || field_width > sizeof (lots_of_dashes))
12237 {
12238 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
12239 decode_mode_spec_buf[i] = '-';
12240 decode_mode_spec_buf[i] = '\0';
12241 return decode_mode_spec_buf;
12242 }
12243 else
12244 return lots_of_dashes;
12245 }
12246
12247 case 'b':
12248 obj = b->name;
12249 break;
12250
12251 case 'c':
12252 {
12253 int col = current_column ();
12254 XSETFASTINT (w->column_number_displayed, col);
12255 pint2str (decode_mode_spec_buf, field_width, col);
12256 return decode_mode_spec_buf;
12257 }
12258
12259 case 'F':
12260 /* %F displays the frame name. */
12261 if (!NILP (f->title))
12262 return (char *) XSTRING (f->title)->data;
12263 if (f->explicit_name || ! FRAME_WINDOW_P (f))
12264 return (char *) XSTRING (f->name)->data;
12265 return "Emacs";
12266
12267 case 'f':
12268 obj = b->filename;
12269 break;
12270
12271 case 'l':
12272 {
12273 int startpos = XMARKER (w->start)->charpos;
12274 int startpos_byte = marker_byte_position (w->start);
12275 int line, linepos, linepos_byte, topline;
12276 int nlines, junk;
12277 int height = XFASTINT (w->height);
12278
12279 /* If we decided that this buffer isn't suitable for line numbers,
12280 don't forget that too fast. */
12281 if (EQ (w->base_line_pos, w->buffer))
12282 goto no_value;
12283 /* But do forget it, if the window shows a different buffer now. */
12284 else if (BUFFERP (w->base_line_pos))
12285 w->base_line_pos = Qnil;
12286
12287 /* If the buffer is very big, don't waste time. */
12288 if (BUF_ZV (b) - BUF_BEGV (b) > line_number_display_limit)
12289 {
12290 w->base_line_pos = Qnil;
12291 w->base_line_number = Qnil;
12292 goto no_value;
12293 }
12294
12295 if (!NILP (w->base_line_number)
12296 && !NILP (w->base_line_pos)
12297 && XFASTINT (w->base_line_pos) <= startpos)
12298 {
12299 line = XFASTINT (w->base_line_number);
12300 linepos = XFASTINT (w->base_line_pos);
12301 linepos_byte = buf_charpos_to_bytepos (b, linepos);
12302 }
12303 else
12304 {
12305 line = 1;
12306 linepos = BUF_BEGV (b);
12307 linepos_byte = BUF_BEGV_BYTE (b);
12308 }
12309
12310 /* Count lines from base line to window start position. */
12311 nlines = display_count_lines (linepos, linepos_byte,
12312 startpos_byte,
12313 startpos, &junk);
12314
12315 topline = nlines + line;
12316
12317 /* Determine a new base line, if the old one is too close
12318 or too far away, or if we did not have one.
12319 "Too close" means it's plausible a scroll-down would
12320 go back past it. */
12321 if (startpos == BUF_BEGV (b))
12322 {
12323 XSETFASTINT (w->base_line_number, topline);
12324 XSETFASTINT (w->base_line_pos, BUF_BEGV (b));
12325 }
12326 else if (nlines < height + 25 || nlines > height * 3 + 50
12327 || linepos == BUF_BEGV (b))
12328 {
12329 int limit = BUF_BEGV (b);
12330 int limit_byte = BUF_BEGV_BYTE (b);
12331 int position;
12332 int distance = (height * 2 + 30) * line_number_display_limit_width;
12333
12334 if (startpos - distance > limit)
12335 {
12336 limit = startpos - distance;
12337 limit_byte = CHAR_TO_BYTE (limit);
12338 }
12339
12340 nlines = display_count_lines (startpos, startpos_byte,
12341 limit_byte,
12342 - (height * 2 + 30),
12343 &position);
12344 /* If we couldn't find the lines we wanted within
12345 line_number_display_limit_width chars per line,
12346 give up on line numbers for this window. */
12347 if (position == limit_byte && limit == startpos - distance)
12348 {
12349 w->base_line_pos = w->buffer;
12350 w->base_line_number = Qnil;
12351 goto no_value;
12352 }
12353
12354 XSETFASTINT (w->base_line_number, topline - nlines);
12355 XSETFASTINT (w->base_line_pos, BYTE_TO_CHAR (position));
12356 }
12357
12358 /* Now count lines from the start pos to point. */
12359 nlines = display_count_lines (startpos, startpos_byte,
12360 PT_BYTE, PT, &junk);
12361
12362 /* Record that we did display the line number. */
12363 line_number_displayed = 1;
12364
12365 /* Make the string to show. */
12366 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
12367 return decode_mode_spec_buf;
12368 no_value:
12369 {
12370 char* p = decode_mode_spec_buf;
12371 int pad = field_width - 2;
12372 while (pad-- > 0)
12373 *p++ = ' ';
12374 *p++ = '?';
12375 *p = '?';
12376 return decode_mode_spec_buf;
12377 }
12378 }
12379 break;
12380
12381 case 'm':
12382 obj = b->mode_name;
12383 break;
12384
12385 case 'n':
12386 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
12387 return " Narrow";
12388 break;
12389
12390 case 'p':
12391 {
12392 int pos = marker_position (w->start);
12393 int total = BUF_ZV (b) - BUF_BEGV (b);
12394
12395 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
12396 {
12397 if (pos <= BUF_BEGV (b))
12398 return "All";
12399 else
12400 return "Bottom";
12401 }
12402 else if (pos <= BUF_BEGV (b))
12403 return "Top";
12404 else
12405 {
12406 if (total > 1000000)
12407 /* Do it differently for a large value, to avoid overflow. */
12408 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
12409 else
12410 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
12411 /* We can't normally display a 3-digit number,
12412 so get us a 2-digit number that is close. */
12413 if (total == 100)
12414 total = 99;
12415 sprintf (decode_mode_spec_buf, "%2d%%", total);
12416 return decode_mode_spec_buf;
12417 }
12418 }
12419
12420 /* Display percentage of size above the bottom of the screen. */
12421 case 'P':
12422 {
12423 int toppos = marker_position (w->start);
12424 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
12425 int total = BUF_ZV (b) - BUF_BEGV (b);
12426
12427 if (botpos >= BUF_ZV (b))
12428 {
12429 if (toppos <= BUF_BEGV (b))
12430 return "All";
12431 else
12432 return "Bottom";
12433 }
12434 else
12435 {
12436 if (total > 1000000)
12437 /* Do it differently for a large value, to avoid overflow. */
12438 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
12439 else
12440 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
12441 /* We can't normally display a 3-digit number,
12442 so get us a 2-digit number that is close. */
12443 if (total == 100)
12444 total = 99;
12445 if (toppos <= BUF_BEGV (b))
12446 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
12447 else
12448 sprintf (decode_mode_spec_buf, "%2d%%", total);
12449 return decode_mode_spec_buf;
12450 }
12451 }
12452
12453 case 's':
12454 /* status of process */
12455 obj = Fget_buffer_process (w->buffer);
12456 if (NILP (obj))
12457 return "no process";
12458 #ifdef subprocesses
12459 obj = Fsymbol_name (Fprocess_status (obj));
12460 #endif
12461 break;
12462
12463 case 't': /* indicate TEXT or BINARY */
12464 #ifdef MODE_LINE_BINARY_TEXT
12465 return MODE_LINE_BINARY_TEXT (b);
12466 #else
12467 return "T";
12468 #endif
12469
12470 case 'z':
12471 /* coding-system (not including end-of-line format) */
12472 case 'Z':
12473 /* coding-system (including end-of-line type) */
12474 {
12475 int eol_flag = (c == 'Z');
12476 char *p = decode_mode_spec_buf;
12477
12478 if (! FRAME_WINDOW_P (f))
12479 {
12480 /* No need to mention EOL here--the terminal never needs
12481 to do EOL conversion. */
12482 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
12483 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
12484 }
12485 p = decode_mode_spec_coding (b->buffer_file_coding_system,
12486 p, eol_flag);
12487
12488 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
12489 #ifdef subprocesses
12490 obj = Fget_buffer_process (Fcurrent_buffer ());
12491 if (PROCESSP (obj))
12492 {
12493 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
12494 p, eol_flag);
12495 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
12496 p, eol_flag);
12497 }
12498 #endif /* subprocesses */
12499 #endif /* 0 */
12500 *p = 0;
12501 return decode_mode_spec_buf;
12502 }
12503 }
12504
12505 if (STRINGP (obj))
12506 return (char *) XSTRING (obj)->data;
12507 else
12508 return "";
12509 }
12510
12511
12512 /* Count up to COUNT lines starting from START / START_BYTE.
12513 But don't go beyond LIMIT_BYTE.
12514 Return the number of lines thus found (always nonnegative).
12515
12516 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
12517
12518 static int
12519 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
12520 int start, start_byte, limit_byte, count;
12521 int *byte_pos_ptr;
12522 {
12523 register unsigned char *cursor;
12524 unsigned char *base;
12525
12526 register int ceiling;
12527 register unsigned char *ceiling_addr;
12528 int orig_count = count;
12529
12530 /* If we are not in selective display mode,
12531 check only for newlines. */
12532 int selective_display = (!NILP (current_buffer->selective_display)
12533 && !INTEGERP (current_buffer->selective_display));
12534
12535 if (count > 0)
12536 {
12537 while (start_byte < limit_byte)
12538 {
12539 ceiling = BUFFER_CEILING_OF (start_byte);
12540 ceiling = min (limit_byte - 1, ceiling);
12541 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
12542 base = (cursor = BYTE_POS_ADDR (start_byte));
12543 while (1)
12544 {
12545 if (selective_display)
12546 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
12547 ;
12548 else
12549 while (*cursor != '\n' && ++cursor != ceiling_addr)
12550 ;
12551
12552 if (cursor != ceiling_addr)
12553 {
12554 if (--count == 0)
12555 {
12556 start_byte += cursor - base + 1;
12557 *byte_pos_ptr = start_byte;
12558 return orig_count;
12559 }
12560 else
12561 if (++cursor == ceiling_addr)
12562 break;
12563 }
12564 else
12565 break;
12566 }
12567 start_byte += cursor - base;
12568 }
12569 }
12570 else
12571 {
12572 while (start_byte > limit_byte)
12573 {
12574 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
12575 ceiling = max (limit_byte, ceiling);
12576 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
12577 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
12578 while (1)
12579 {
12580 if (selective_display)
12581 while (--cursor != ceiling_addr
12582 && *cursor != '\n' && *cursor != 015)
12583 ;
12584 else
12585 while (--cursor != ceiling_addr && *cursor != '\n')
12586 ;
12587
12588 if (cursor != ceiling_addr)
12589 {
12590 if (++count == 0)
12591 {
12592 start_byte += cursor - base + 1;
12593 *byte_pos_ptr = start_byte;
12594 /* When scanning backwards, we should
12595 not count the newline posterior to which we stop. */
12596 return - orig_count - 1;
12597 }
12598 }
12599 else
12600 break;
12601 }
12602 /* Here we add 1 to compensate for the last decrement
12603 of CURSOR, which took it past the valid range. */
12604 start_byte += cursor - base + 1;
12605 }
12606 }
12607
12608 *byte_pos_ptr = limit_byte;
12609
12610 if (count < 0)
12611 return - orig_count + count;
12612 return orig_count - count;
12613
12614 }
12615
12616
12617 \f
12618 /***********************************************************************
12619 Displaying strings
12620 ***********************************************************************/
12621
12622 /* Display a NUL-terminated string, starting with index START.
12623
12624 If STRING is non-null, display that C string. Otherwise, the Lisp
12625 string LISP_STRING is displayed.
12626
12627 If FACE_STRING is not nil, FACE_STRING_POS is a position in
12628 FACE_STRING. Display STRING or LISP_STRING with the face at
12629 FACE_STRING_POS in FACE_STRING:
12630
12631 Display the string in the environment given by IT, but use the
12632 standard display table, temporarily.
12633
12634 FIELD_WIDTH is the minimum number of output glyphs to produce.
12635 If STRING has fewer characters than FIELD_WIDTH, pad to the right
12636 with spaces. If STRING has more characters, more than FIELD_WIDTH
12637 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
12638
12639 PRECISION is the maximum number of characters to output from
12640 STRING. PRECISION < 0 means don't truncate the string.
12641
12642 This is roughly equivalent to printf format specifiers:
12643
12644 FIELD_WIDTH PRECISION PRINTF
12645 ----------------------------------------
12646 -1 -1 %s
12647 -1 10 %.10s
12648 10 -1 %10s
12649 20 10 %20.10s
12650
12651 MULTIBYTE zero means do not display multibyte chars, > 0 means do
12652 display them, and < 0 means obey the current buffer's value of
12653 enable_multibyte_characters.
12654
12655 Value is the number of glyphs produced. */
12656
12657 static int
12658 display_string (string, lisp_string, face_string, face_string_pos,
12659 start, it, field_width, precision, max_x, multibyte)
12660 unsigned char *string;
12661 Lisp_Object lisp_string;
12662 Lisp_Object face_string;
12663 int face_string_pos;
12664 int start;
12665 struct it *it;
12666 int field_width, precision, max_x;
12667 int multibyte;
12668 {
12669 int hpos_at_start = it->hpos;
12670 int saved_face_id = it->face_id;
12671 struct glyph_row *row = it->glyph_row;
12672
12673 /* Initialize the iterator IT for iteration over STRING beginning
12674 with index START. We assume that IT may be modified here (which
12675 means that display_line has to do something when displaying a
12676 mini-buffer prompt, which it does). */
12677 reseat_to_string (it, string, lisp_string, start,
12678 precision, field_width, multibyte);
12679
12680 /* If displaying STRING, set up the face of the iterator
12681 from LISP_STRING, if that's given. */
12682 if (STRINGP (face_string))
12683 {
12684 int endptr;
12685 struct face *face;
12686
12687 it->face_id
12688 = face_at_string_position (it->w, face_string, face_string_pos,
12689 0, it->region_beg_charpos,
12690 it->region_end_charpos,
12691 &endptr, it->base_face_id);
12692 face = FACE_FROM_ID (it->f, it->face_id);
12693 it->face_box_p = face->box != FACE_NO_BOX;
12694 }
12695
12696 /* Set max_x to the maximum allowed X position. Don't let it go
12697 beyond the right edge of the window. */
12698 if (max_x <= 0)
12699 max_x = it->last_visible_x;
12700 else
12701 max_x = min (max_x, it->last_visible_x);
12702
12703 /* Skip over display elements that are not visible. because IT->w is
12704 hscrolled. */
12705 if (it->current_x < it->first_visible_x)
12706 move_it_in_display_line_to (it, 100000, it->first_visible_x,
12707 MOVE_TO_POS | MOVE_TO_X);
12708
12709 row->ascent = it->max_ascent;
12710 row->height = it->max_ascent + it->max_descent;
12711 row->phys_ascent = it->max_phys_ascent;
12712 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
12713
12714 /* This condition is for the case that we are called with current_x
12715 past last_visible_x. */
12716 while (it->current_x < max_x)
12717 {
12718 int x_before, x, n_glyphs_before, i, nglyphs;
12719
12720 /* Get the next display element. */
12721 if (!get_next_display_element (it))
12722 break;
12723
12724 /* Produce glyphs. */
12725 x_before = it->current_x;
12726 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
12727 PRODUCE_GLYPHS (it);
12728
12729 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
12730 i = 0;
12731 x = x_before;
12732 while (i < nglyphs)
12733 {
12734 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12735
12736 if (!it->truncate_lines_p
12737 && x + glyph->pixel_width > max_x)
12738 {
12739 /* End of continued line or max_x reached. */
12740 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
12741 it->current_x = x;
12742 break;
12743 }
12744 else if (x + glyph->pixel_width > it->first_visible_x)
12745 {
12746 /* Glyph is at least partially visible. */
12747 ++it->hpos;
12748 if (x < it->first_visible_x)
12749 it->glyph_row->x = x - it->first_visible_x;
12750 }
12751 else
12752 {
12753 /* Glyph is off the left margin of the display area.
12754 Should not happen. */
12755 abort ();
12756 }
12757
12758 row->ascent = max (row->ascent, it->max_ascent);
12759 row->height = max (row->height, it->max_ascent + it->max_descent);
12760 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12761 row->phys_height = max (row->phys_height,
12762 it->max_phys_ascent + it->max_phys_descent);
12763 x += glyph->pixel_width;
12764 ++i;
12765 }
12766
12767 /* Stop if max_x reached. */
12768 if (i < nglyphs)
12769 break;
12770
12771 /* Stop at line ends. */
12772 if (ITERATOR_AT_END_OF_LINE_P (it))
12773 {
12774 it->continuation_lines_width = 0;
12775 break;
12776 }
12777
12778 set_iterator_to_next (it);
12779
12780 /* Stop if truncating at the right edge. */
12781 if (it->truncate_lines_p
12782 && it->current_x >= it->last_visible_x)
12783 {
12784 /* Add truncation mark, but don't do it if the line is
12785 truncated at a padding space. */
12786 if (IT_CHARPOS (*it) < it->string_nchars)
12787 {
12788 if (!FRAME_WINDOW_P (it->f))
12789 produce_special_glyphs (it, IT_TRUNCATION);
12790 it->glyph_row->truncated_on_right_p = 1;
12791 }
12792 break;
12793 }
12794 }
12795
12796 /* Maybe insert a truncation at the left. */
12797 if (it->first_visible_x
12798 && IT_CHARPOS (*it) > 0)
12799 {
12800 if (!FRAME_WINDOW_P (it->f))
12801 insert_left_trunc_glyphs (it);
12802 it->glyph_row->truncated_on_left_p = 1;
12803 }
12804
12805 it->face_id = saved_face_id;
12806
12807 /* Value is number of columns displayed. */
12808 return it->hpos - hpos_at_start;
12809 }
12810
12811
12812 \f
12813 /* This is like a combination of memq and assq. Return 1 if PROPVAL
12814 appears as an element of LIST or as the car of an element of LIST.
12815 If PROPVAL is a list, compare each element against LIST in that
12816 way, and return 1 if any element of PROPVAL is found in LIST.
12817 Otherwise return 0. This function cannot quit. */
12818
12819 int
12820 invisible_p (propval, list)
12821 register Lisp_Object propval;
12822 Lisp_Object list;
12823 {
12824 register Lisp_Object tail, proptail;
12825 for (tail = list; CONSP (tail); tail = XCDR (tail))
12826 {
12827 register Lisp_Object tem;
12828 tem = XCAR (tail);
12829 if (EQ (propval, tem))
12830 return 1;
12831 if (CONSP (tem) && EQ (propval, XCAR (tem)))
12832 return 1;
12833 }
12834 if (CONSP (propval))
12835 for (proptail = propval; CONSP (proptail);
12836 proptail = XCDR (proptail))
12837 {
12838 Lisp_Object propelt;
12839 propelt = XCAR (proptail);
12840 for (tail = list; CONSP (tail); tail = XCDR (tail))
12841 {
12842 register Lisp_Object tem;
12843 tem = XCAR (tail);
12844 if (EQ (propelt, tem))
12845 return 1;
12846 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
12847 return 1;
12848 }
12849 }
12850 return 0;
12851 }
12852
12853
12854 /* Return 1 if PROPVAL appears as the car of an element of LIST and
12855 the cdr of that element is non-nil. If PROPVAL is a list, check
12856 each element of PROPVAL in that way, and the first time some
12857 element is found, return 1 if the cdr of that element is non-nil.
12858 Otherwise return 0. This function cannot quit. */
12859
12860 int
12861 invisible_ellipsis_p (propval, list)
12862 register Lisp_Object propval;
12863 Lisp_Object list;
12864 {
12865 register Lisp_Object tail, proptail;
12866
12867 for (tail = list; CONSP (tail); tail = XCDR (tail))
12868 {
12869 register Lisp_Object tem;
12870 tem = XCAR (tail);
12871 if (CONSP (tem) && EQ (propval, XCAR (tem)))
12872 return ! NILP (XCDR (tem));
12873 }
12874
12875 if (CONSP (propval))
12876 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
12877 {
12878 Lisp_Object propelt;
12879 propelt = XCAR (proptail);
12880 for (tail = list; CONSP (tail); tail = XCDR (tail))
12881 {
12882 register Lisp_Object tem;
12883 tem = XCAR (tail);
12884 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
12885 return ! NILP (XCDR (tem));
12886 }
12887 }
12888
12889 return 0;
12890 }
12891
12892
12893 \f
12894 /***********************************************************************
12895 Initialization
12896 ***********************************************************************/
12897
12898 void
12899 syms_of_xdisp ()
12900 {
12901 Vwith_echo_area_save_vector = Qnil;
12902 staticpro (&Vwith_echo_area_save_vector);
12903
12904 Vmessage_stack = Qnil;
12905 staticpro (&Vmessage_stack);
12906
12907 Qinhibit_redisplay = intern ("inhibit-redisplay");
12908 staticpro (&Qinhibit_redisplay);
12909
12910 #if GLYPH_DEBUG
12911 defsubr (&Sdump_glyph_matrix);
12912 defsubr (&Sdump_glyph_row);
12913 defsubr (&Sdump_tool_bar_row);
12914 defsubr (&Strace_redisplay_toggle);
12915 defsubr (&Strace_to_stderr);
12916 #endif
12917
12918 staticpro (&Qmenu_bar_update_hook);
12919 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
12920
12921 staticpro (&Qoverriding_terminal_local_map);
12922 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
12923
12924 staticpro (&Qoverriding_local_map);
12925 Qoverriding_local_map = intern ("overriding-local-map");
12926
12927 staticpro (&Qwindow_scroll_functions);
12928 Qwindow_scroll_functions = intern ("window-scroll-functions");
12929
12930 staticpro (&Qredisplay_end_trigger_functions);
12931 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
12932
12933 staticpro (&Qinhibit_point_motion_hooks);
12934 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
12935
12936 QCdata = intern (":data");
12937 staticpro (&QCdata);
12938 Qdisplay = intern ("display");
12939 staticpro (&Qdisplay);
12940 Qspace_width = intern ("space-width");
12941 staticpro (&Qspace_width);
12942 Qraise = intern ("raise");
12943 staticpro (&Qraise);
12944 Qspace = intern ("space");
12945 staticpro (&Qspace);
12946 Qmargin = intern ("margin");
12947 staticpro (&Qmargin);
12948 Qleft_margin = intern ("left-margin");
12949 staticpro (&Qleft_margin);
12950 Qright_margin = intern ("right-margin");
12951 staticpro (&Qright_margin);
12952 Qalign_to = intern ("align-to");
12953 staticpro (&Qalign_to);
12954 QCalign_to = intern (":align-to");
12955 staticpro (&QCalign_to);
12956 Qrelative_width = intern ("relative-width");
12957 staticpro (&Qrelative_width);
12958 QCrelative_width = intern (":relative-width");
12959 staticpro (&QCrelative_width);
12960 QCrelative_height = intern (":relative-height");
12961 staticpro (&QCrelative_height);
12962 QCeval = intern (":eval");
12963 staticpro (&QCeval);
12964 Qwhen = intern ("when");
12965 staticpro (&Qwhen);
12966 QCfile = intern (":file");
12967 staticpro (&QCfile);
12968 Qfontified = intern ("fontified");
12969 staticpro (&Qfontified);
12970 Qfontification_functions = intern ("fontification-functions");
12971 staticpro (&Qfontification_functions);
12972 Qtrailing_whitespace = intern ("trailing-whitespace");
12973 staticpro (&Qtrailing_whitespace);
12974 Qimage = intern ("image");
12975 staticpro (&Qimage);
12976
12977 last_arrow_position = Qnil;
12978 last_arrow_string = Qnil;
12979 staticpro (&last_arrow_position);
12980 staticpro (&last_arrow_string);
12981
12982 echo_buffer[0] = echo_buffer[1] = Qnil;
12983 staticpro (&echo_buffer[0]);
12984 staticpro (&echo_buffer[1]);
12985
12986 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
12987 staticpro (&echo_area_buffer[0]);
12988 staticpro (&echo_area_buffer[1]);
12989
12990 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
12991 "Non-nil means highlight trailing whitespace.\n\
12992 The face used for trailing whitespace is `trailing-whitespace'.");
12993 Vshow_trailing_whitespace = Qnil;
12994
12995 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
12996 "Non-nil means don't actually do any redisplay.\n\
12997 This is used for internal purposes.");
12998 Vinhibit_redisplay = Qnil;
12999
13000 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
13001 "String (or mode line construct) included (normally) in `mode-line-format'.");
13002 Vglobal_mode_string = Qnil;
13003
13004 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
13005 "Marker for where to display an arrow on top of the buffer text.\n\
13006 This must be the beginning of a line in order to work.\n\
13007 See also `overlay-arrow-string'.");
13008 Voverlay_arrow_position = Qnil;
13009
13010 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
13011 "String to display as an arrow. See also `overlay-arrow-position'.");
13012 Voverlay_arrow_string = Qnil;
13013
13014 DEFVAR_INT ("scroll-step", &scroll_step,
13015 "*The number of lines to try scrolling a window by when point moves out.\n\
13016 If that fails to bring point back on frame, point is centered instead.\n\
13017 If this is zero, point is always centered after it moves off frame.");
13018
13019 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
13020 "*Scroll up to this many lines, to bring point back on screen.\n\
13021 A value of zero means to scroll the text to center point vertically\n\
13022 in the window.");
13023 scroll_conservatively = 0;
13024
13025 DEFVAR_INT ("scroll-margin", &scroll_margin,
13026 "*Number of lines of margin at the top and bottom of a window.\n\
13027 Recenter the window whenever point gets within this many lines\n\
13028 of the top or bottom of the window.");
13029 scroll_margin = 0;
13030
13031 #if GLYPH_DEBUG
13032 DEFVAR_INT ("debug-end-pos", &debug_end_pos, "Don't ask");
13033 #endif
13034
13035 DEFVAR_BOOL ("truncate-partial-width-windows",
13036 &truncate_partial_width_windows,
13037 "*Non-nil means truncate lines in all windows less than full frame wide.");
13038 truncate_partial_width_windows = 1;
13039
13040 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
13041 "*Non-nil means use inverse video for the mode line.");
13042 mode_line_inverse_video = 1;
13043
13044 DEFVAR_INT ("line-number-display-limit", &line_number_display_limit,
13045 "*Maximum buffer size for which line number should be displayed.\n\
13046 If the buffer is bigger than this, the line number does not appear\n\
13047 in the mode line.");
13048 line_number_display_limit = 1000000;
13049
13050 DEFVAR_INT ("line-number-display-limit-width", &line_number_display_limit_width,
13051 "*Maximum line width (in characters) for line number display.\n\
13052 If the average length of the lines near point is bigger than this, then the\n\
13053 line number may be omitted from the mode line.");
13054 line_number_display_limit_width = 200;
13055
13056 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
13057 "*Non-nil means highlight region even in nonselected windows.");
13058 highlight_nonselected_windows = 0;
13059
13060 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
13061 "Non-nil if more than one frame is visible on this display.\n\
13062 Minibuffer-only frames don't count, but iconified frames do.\n\
13063 This variable is not guaranteed to be accurate except while processing\n\
13064 `frame-title-format' and `icon-title-format'.");
13065
13066 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
13067 "Template for displaying the title bar of visible frames.\n\
13068 \(Assuming the window manager supports this feature.)\n\
13069 This variable has the same structure as `mode-line-format' (which see),\n\
13070 and is used only on frames for which no explicit name has been set\n\
13071 \(see `modify-frame-parameters').");
13072 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
13073 "Template for displaying the title bar of an iconified frame.\n\
13074 \(Assuming the window manager supports this feature.)\n\
13075 This variable has the same structure as `mode-line-format' (which see),\n\
13076 and is used only on frames for which no explicit name has been set\n\
13077 \(see `modify-frame-parameters').");
13078 Vicon_title_format
13079 = Vframe_title_format
13080 = Fcons (intern ("multiple-frames"),
13081 Fcons (build_string ("%b"),
13082 Fcons (Fcons (build_string (""),
13083 Fcons (intern ("invocation-name"),
13084 Fcons (build_string ("@"),
13085 Fcons (intern ("system-name"),
13086 Qnil)))),
13087 Qnil)));
13088
13089 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
13090 "Maximum number of lines to keep in the message log buffer.\n\
13091 If nil, disable message logging. If t, log messages but don't truncate\n\
13092 the buffer when it becomes large.");
13093 XSETFASTINT (Vmessage_log_max, 50);
13094
13095 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
13096 "Functions called before redisplay, if window sizes have changed.\n\
13097 The value should be a list of functions that take one argument.\n\
13098 Just before redisplay, for each frame, if any of its windows have changed\n\
13099 size since the last redisplay, or have been split or deleted,\n\
13100 all the functions in the list are called, with the frame as argument.");
13101 Vwindow_size_change_functions = Qnil;
13102
13103 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
13104 "List of Functions to call before redisplaying a window with scrolling.\n\
13105 Each function is called with two arguments, the window\n\
13106 and its new display-start position. Note that the value of `window-end'\n\
13107 is not valid when these functions are called.");
13108 Vwindow_scroll_functions = Qnil;
13109
13110 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
13111 "*Non-nil means automatically resize tool-bars.\n\
13112 This increases a tool-bar's height if not all tool-bar items are visible.\n\
13113 It decreases a tool-bar's height when it would display blank lines\n\
13114 otherwise.");
13115 auto_resize_tool_bars_p = 1;
13116
13117 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
13118 "*Non-nil means raise tool-bar buttons when the mouse moves over them.");
13119 auto_raise_tool_bar_buttons_p = 1;
13120
13121 DEFVAR_INT ("tool-bar-button-margin", &tool_bar_button_margin,
13122 "*Margin around tool-bar buttons in pixels.");
13123 tool_bar_button_margin = 1;
13124
13125 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
13126 "Relief thickness of tool-bar buttons.");
13127 tool_bar_button_relief = 3;
13128
13129 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
13130 "List of functions to call to fontify regions of text.\n\
13131 Each function is called with one argument POS. Functions must\n\
13132 fontify a region starting at POS in the current buffer, and give\n\
13133 fontified regions the property `fontified'.\n\
13134 This variable automatically becomes buffer-local when set.");
13135 Vfontification_functions = Qnil;
13136 Fmake_local_variable (Qfontification_functions);
13137
13138 DEFVAR_BOOL ("unibyte-display-via-language-environment",
13139 &unibyte_display_via_language_environment,
13140 "*Non-nil means display unibyte text according to language environment.\n\
13141 Specifically this means that unibyte non-ASCII characters\n\
13142 are displayed by converting them to the equivalent multibyte characters\n\
13143 according to the current language environment. As a result, they are\n\
13144 displayed according to the current fontset.");
13145 unibyte_display_via_language_environment = 0;
13146
13147 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
13148 "*Maximum height for resizing mini-windows.\n\
13149 If a float, it specifies a fraction of the mini-window frame's height.\n\
13150 If an integer, it specifies a number of lines.\n\
13151 If nil, don't resize.");
13152 Vmax_mini_window_height = make_float (0.25);
13153
13154 DEFVAR_BOOL ("cursor-in-non-selected-windows",
13155 &cursor_in_non_selected_windows,
13156 "*Non-nil means display a hollow cursor in non-selected windows.\n\
13157 Nil means don't display a cursor there.");
13158 cursor_in_non_selected_windows = 1;
13159
13160 DEFVAR_BOOL ("automatic-hscrolling", &automatic_hscrolling_p,
13161 "*Non-nil means scroll the display automatically to make point visible.");
13162 automatic_hscrolling_p = 1;
13163
13164 DEFVAR_LISP ("image-types", &Vimage_types,
13165 "List of supported image types.\n\
13166 Each element of the list is a symbol for a supported image type.");
13167 Vimage_types = Qnil;
13168 }
13169
13170
13171 /* Initialize this module when Emacs starts. */
13172
13173 void
13174 init_xdisp ()
13175 {
13176 Lisp_Object root_window;
13177 struct window *mini_w;
13178
13179 CHARPOS (this_line_start_pos) = 0;
13180
13181 mini_w = XWINDOW (minibuf_window);
13182 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
13183
13184 if (!noninteractive)
13185 {
13186 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
13187 int i;
13188
13189 XSETFASTINT (XWINDOW (root_window)->top, FRAME_TOP_MARGIN (f));
13190 set_window_height (root_window,
13191 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
13192 0);
13193 XSETFASTINT (mini_w->top, FRAME_HEIGHT (f) - 1);
13194 set_window_height (minibuf_window, 1, 0);
13195
13196 XSETFASTINT (XWINDOW (root_window)->width, FRAME_WIDTH (f));
13197 XSETFASTINT (mini_w->width, FRAME_WIDTH (f));
13198
13199 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
13200 scratch_glyph_row.glyphs[TEXT_AREA + 1]
13201 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
13202
13203 /* The default ellipsis glyphs `...'. */
13204 for (i = 0; i < 3; ++i)
13205 XSETFASTINT (default_invis_vector[i], '.');
13206 }
13207
13208 #ifdef HAVE_WINDOW_SYSTEM
13209 {
13210 /* Allocate the buffer for frame titles. */
13211 int size = 100;
13212 frame_title_buf = (char *) xmalloc (size);
13213 frame_title_buf_end = frame_title_buf + size;
13214 frame_title_ptr = NULL;
13215 }
13216 #endif /* HAVE_WINDOW_SYSTEM */
13217 }
13218
13219