Remove debugging code.
[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, 2000, 2001
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 "keyboard.h"
174 #include "frame.h"
175 #include "window.h"
176 #include "termchar.h"
177 #include "dispextern.h"
178 #include "buffer.h"
179 #include "charset.h"
180 #include "indent.h"
181 #include "commands.h"
182 #include "macros.h"
183 #include "disptab.h"
184 #include "termhooks.h"
185 #include "intervals.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 #ifdef macintosh
198 #include "macterm.h"
199 #endif
200
201 #define min(a, b) ((a) < (b) ? (a) : (b))
202 #define max(a, b) ((a) > (b) ? (a) : (b))
203
204 #define INFINITY 10000000
205
206 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
207 extern void set_frame_menubar P_ ((struct frame *f, int, int));
208 extern int pending_menu_activation;
209 #endif
210
211 extern int interrupt_input;
212 extern int command_loop_level;
213
214 extern int minibuffer_auto_raise;
215
216 extern Lisp_Object Qface;
217
218 extern Lisp_Object Voverriding_local_map;
219 extern Lisp_Object Voverriding_local_map_menu_flag;
220 extern Lisp_Object Qmenu_item;
221
222 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
223 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
224 Lisp_Object Qredisplay_end_trigger_functions;
225 Lisp_Object Qinhibit_point_motion_hooks;
226 Lisp_Object QCeval, Qwhen, QCfile, QCdata;
227 Lisp_Object Qfontified;
228 Lisp_Object Qgrow_only;
229
230 /* Functions called to fontify regions of text. */
231
232 Lisp_Object Vfontification_functions;
233 Lisp_Object Qfontification_functions;
234
235 /* Non-zero means draw tool bar buttons raised when the mouse moves
236 over them. */
237
238 int auto_raise_tool_bar_buttons_p;
239
240 /* Margin around tool bar buttons in pixels. */
241
242 Lisp_Object Vtool_bar_button_margin;
243
244 /* Thickness of shadow to draw around tool bar buttons. */
245
246 int tool_bar_button_relief;
247
248 /* Non-zero means automatically resize tool-bars so that all tool-bar
249 items are visible, and no blank lines remain. */
250
251 int auto_resize_tool_bars_p;
252
253 /* Non-nil means don't actually do any redisplay. */
254
255 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
256
257 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
258
259 int inhibit_eval_during_redisplay, Qinhibit_eval_during_redisplay;
260
261 /* Names of text properties relevant for redisplay. */
262
263 Lisp_Object Qdisplay, Qrelative_width, Qalign_to;
264 extern Lisp_Object Qface, Qinvisible, Qimage, Qwidth;
265
266 /* Symbols used in text property values. */
267
268 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
269 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
270 Lisp_Object Qmargin;
271 extern Lisp_Object Qheight;
272
273 /* Non-nil means highlight trailing whitespace. */
274
275 Lisp_Object Vshow_trailing_whitespace;
276
277 /* Name of the face used to highlight trailing whitespace. */
278
279 Lisp_Object Qtrailing_whitespace;
280
281 /* The symbol `image' which is the car of the lists used to represent
282 images in Lisp. */
283
284 Lisp_Object Qimage;
285
286 /* Non-zero means print newline to stdout before next mini-buffer
287 message. */
288
289 int noninteractive_need_newline;
290
291 /* Non-zero means print newline to message log before next message. */
292
293 static int message_log_need_newline;
294
295 \f
296 /* The buffer position of the first character appearing entirely or
297 partially on the line of the selected window which contains the
298 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
299 redisplay optimization in redisplay_internal. */
300
301 static struct text_pos this_line_start_pos;
302
303 /* Number of characters past the end of the line above, including the
304 terminating newline. */
305
306 static struct text_pos this_line_end_pos;
307
308 /* The vertical positions and the height of this line. */
309
310 static int this_line_vpos;
311 static int this_line_y;
312 static int this_line_pixel_height;
313
314 /* X position at which this display line starts. Usually zero;
315 negative if first character is partially visible. */
316
317 static int this_line_start_x;
318
319 /* Buffer that this_line_.* variables are referring to. */
320
321 static struct buffer *this_line_buffer;
322
323 /* Nonzero means truncate lines in all windows less wide than the
324 frame. */
325
326 int truncate_partial_width_windows;
327
328 /* A flag to control how to display unibyte 8-bit character. */
329
330 int unibyte_display_via_language_environment;
331
332 /* Nonzero means we have more than one non-mini-buffer-only frame.
333 Not guaranteed to be accurate except while parsing
334 frame-title-format. */
335
336 int multiple_frames;
337
338 Lisp_Object Vglobal_mode_string;
339
340 /* Marker for where to display an arrow on top of the buffer text. */
341
342 Lisp_Object Voverlay_arrow_position;
343
344 /* String to display for the arrow. Only used on terminal frames. */
345
346 Lisp_Object Voverlay_arrow_string;
347
348 /* Values of those variables at last redisplay. However, if
349 Voverlay_arrow_position is a marker, last_arrow_position is its
350 numerical position. */
351
352 static Lisp_Object last_arrow_position, last_arrow_string;
353
354 /* Like mode-line-format, but for the title bar on a visible frame. */
355
356 Lisp_Object Vframe_title_format;
357
358 /* Like mode-line-format, but for the title bar on an iconified frame. */
359
360 Lisp_Object Vicon_title_format;
361
362 /* List of functions to call when a window's size changes. These
363 functions get one arg, a frame on which one or more windows' sizes
364 have changed. */
365
366 static Lisp_Object Vwindow_size_change_functions;
367
368 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
369
370 /* Nonzero if overlay arrow has been displayed once in this window. */
371
372 static int overlay_arrow_seen;
373
374 /* Nonzero means highlight the region even in nonselected windows. */
375
376 int highlight_nonselected_windows;
377
378 /* If cursor motion alone moves point off frame, try scrolling this
379 many lines up or down if that will bring it back. */
380
381 static int scroll_step;
382
383 /* Non-0 means scroll just far enough to bring point back on the
384 screen, when appropriate. */
385
386 static int scroll_conservatively;
387
388 /* Recenter the window whenever point gets within this many lines of
389 the top or bottom of the window. This value is translated into a
390 pixel value by multiplying it with CANON_Y_UNIT, which means that
391 there is really a fixed pixel height scroll margin. */
392
393 int scroll_margin;
394
395 /* Number of windows showing the buffer of the selected window (or
396 another buffer with the same base buffer). keyboard.c refers to
397 this. */
398
399 int buffer_shared;
400
401 /* Vector containing glyphs for an ellipsis `...'. */
402
403 static Lisp_Object default_invis_vector[3];
404
405 /* Zero means display the mode-line/header-line/menu-bar in the default face
406 (this slightly odd definition is for compatibility with previous versions
407 of emacs), non-zero means display them using their respective faces.
408
409 This variable is deprecated. */
410
411 int mode_line_inverse_video;
412
413 /* Prompt to display in front of the mini-buffer contents. */
414
415 Lisp_Object minibuf_prompt;
416
417 /* Width of current mini-buffer prompt. Only set after display_line
418 of the line that contains the prompt. */
419
420 int minibuf_prompt_width;
421 int minibuf_prompt_pixel_width;
422
423 /* This is the window where the echo area message was displayed. It
424 is always a mini-buffer window, but it may not be the same window
425 currently active as a mini-buffer. */
426
427 Lisp_Object echo_area_window;
428
429 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
430 pushes the current message and the value of
431 message_enable_multibyte on the stack, the function restore_message
432 pops the stack and displays MESSAGE again. */
433
434 Lisp_Object Vmessage_stack;
435
436 /* Nonzero means multibyte characters were enabled when the echo area
437 message was specified. */
438
439 int message_enable_multibyte;
440
441 /* True if we should redraw the mode lines on the next redisplay. */
442
443 int update_mode_lines;
444
445 /* Nonzero if window sizes or contents have changed since last
446 redisplay that finished */
447
448 int windows_or_buffers_changed;
449
450 /* Nonzero after display_mode_line if %l was used and it displayed a
451 line number. */
452
453 int line_number_displayed;
454
455 /* Maximum buffer size for which to display line numbers. */
456
457 Lisp_Object Vline_number_display_limit;
458
459 /* line width to consider when repostioning for line number display */
460
461 static int line_number_display_limit_width;
462
463 /* Number of lines to keep in the message log buffer. t means
464 infinite. nil means don't log at all. */
465
466 Lisp_Object Vmessage_log_max;
467
468 /* The name of the *Messages* buffer, a string. */
469
470 static Lisp_Object Vmessages_buffer_name;
471
472 /* Current, index 0, and last displayed echo area message. Either
473 buffers from echo_buffers, or nil to indicate no message. */
474
475 Lisp_Object echo_area_buffer[2];
476
477 /* The buffers referenced from echo_area_buffer. */
478
479 static Lisp_Object echo_buffer[2];
480
481 /* A vector saved used in with_area_buffer to reduce consing. */
482
483 static Lisp_Object Vwith_echo_area_save_vector;
484
485 /* Non-zero means display_echo_area should display the last echo area
486 message again. Set by redisplay_preserve_echo_area. */
487
488 static int display_last_displayed_message_p;
489
490 /* Nonzero if echo area is being used by print; zero if being used by
491 message. */
492
493 int message_buf_print;
494
495 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
496
497 Lisp_Object Qinhibit_menubar_update;
498 int inhibit_menubar_update;
499
500 /* Maximum height for resizing mini-windows. Either a float
501 specifying a fraction of the available height, or an integer
502 specifying a number of lines. */
503
504 Lisp_Object Vmax_mini_window_height;
505
506 /* Non-zero means messages should be displayed with truncated
507 lines instead of being continued. */
508
509 int message_truncate_lines;
510 Lisp_Object Qmessage_truncate_lines;
511
512 /* Non-zero means we want a hollow cursor in windows that are not
513 selected. Zero means there's no cursor in such windows. */
514
515 int cursor_in_non_selected_windows;
516
517 /* A scratch glyph row with contents used for generating truncation
518 glyphs. Also used in direct_output_for_insert. */
519
520 #define MAX_SCRATCH_GLYPHS 100
521 struct glyph_row scratch_glyph_row;
522 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
523
524 /* Ascent and height of the last line processed by move_it_to. */
525
526 static int last_max_ascent, last_height;
527
528 /* Non-zero if there's a help-echo in the echo area. */
529
530 int help_echo_showing_p;
531
532 /* If >= 0, computed, exact values of mode-line and header-line height
533 to use in the macros CURRENT_MODE_LINE_HEIGHT and
534 CURRENT_HEADER_LINE_HEIGHT. */
535
536 int current_mode_line_height, current_header_line_height;
537
538 /* The maximum distance to look ahead for text properties. Values
539 that are too small let us call compute_char_face and similar
540 functions too often which is expensive. Values that are too large
541 let us call compute_char_face and alike too often because we
542 might not be interested in text properties that far away. */
543
544 #define TEXT_PROP_DISTANCE_LIMIT 100
545
546 #if GLYPH_DEBUG
547
548 /* Non-zero means print traces of redisplay if compiled with
549 GLYPH_DEBUG != 0. */
550
551 int trace_redisplay_p;
552
553 #endif /* GLYPH_DEBUG */
554
555 #ifdef DEBUG_TRACE_MOVE
556 /* Non-zero means trace with TRACE_MOVE to stderr. */
557 int trace_move;
558
559 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
560 #else
561 #define TRACE_MOVE(x) (void) 0
562 #endif
563
564 /* Non-zero means automatically scroll windows horizontally to make
565 point visible. */
566
567 int automatic_hscrolling_p;
568
569 /* A list of symbols, one for each supported image type. */
570
571 Lisp_Object Vimage_types;
572
573 /* The variable `resize-mini-windows'. If nil, don't resize
574 mini-windows. If t, always resize them to fit the text they
575 display. If `grow-only', let mini-windows grow only until they
576 become empty. */
577
578 Lisp_Object Vresize_mini_windows;
579
580 /* Value returned from text property handlers (see below). */
581
582 enum prop_handled
583 {
584 HANDLED_NORMALLY,
585 HANDLED_RECOMPUTE_PROPS,
586 HANDLED_OVERLAY_STRING_CONSUMED,
587 HANDLED_RETURN
588 };
589
590 /* A description of text properties that redisplay is interested
591 in. */
592
593 struct props
594 {
595 /* The name of the property. */
596 Lisp_Object *name;
597
598 /* A unique index for the property. */
599 enum prop_idx idx;
600
601 /* A handler function called to set up iterator IT from the property
602 at IT's current position. Value is used to steer handle_stop. */
603 enum prop_handled (*handler) P_ ((struct it *it));
604 };
605
606 static enum prop_handled handle_face_prop P_ ((struct it *));
607 static enum prop_handled handle_invisible_prop P_ ((struct it *));
608 static enum prop_handled handle_display_prop P_ ((struct it *));
609 static enum prop_handled handle_composition_prop P_ ((struct it *));
610 static enum prop_handled handle_overlay_change P_ ((struct it *));
611 static enum prop_handled handle_fontified_prop P_ ((struct it *));
612
613 /* Properties handled by iterators. */
614
615 static struct props it_props[] =
616 {
617 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
618 /* Handle `face' before `display' because some sub-properties of
619 `display' need to know the face. */
620 {&Qface, FACE_PROP_IDX, handle_face_prop},
621 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
622 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
623 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
624 {NULL, 0, NULL}
625 };
626
627 /* Value is the position described by X. If X is a marker, value is
628 the marker_position of X. Otherwise, value is X. */
629
630 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
631
632 /* Enumeration returned by some move_it_.* functions internally. */
633
634 enum move_it_result
635 {
636 /* Not used. Undefined value. */
637 MOVE_UNDEFINED,
638
639 /* Move ended at the requested buffer position or ZV. */
640 MOVE_POS_MATCH_OR_ZV,
641
642 /* Move ended at the requested X pixel position. */
643 MOVE_X_REACHED,
644
645 /* Move within a line ended at the end of a line that must be
646 continued. */
647 MOVE_LINE_CONTINUED,
648
649 /* Move within a line ended at the end of a line that would
650 be displayed truncated. */
651 MOVE_LINE_TRUNCATED,
652
653 /* Move within a line ended at a line end. */
654 MOVE_NEWLINE_OR_CR
655 };
656
657
658 \f
659 /* Function prototypes. */
660
661 static void mark_window_display_accurate_1 P_ ((struct window *, int));
662 static int single_display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
663 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
664 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
665 static int redisplay_mode_lines P_ ((Lisp_Object, int));
666 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
667 static int invisible_text_between_p P_ ((struct it *, int, int));
668 static int next_element_from_ellipsis P_ ((struct it *));
669 static void pint2str P_ ((char *, int, int));
670 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
671 struct text_pos));
672 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
673 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
674 static void store_frame_title_char P_ ((char));
675 static int store_frame_title P_ ((unsigned char *, int, int));
676 static void x_consider_frame_title P_ ((Lisp_Object));
677 static void handle_stop P_ ((struct it *));
678 static int tool_bar_lines_needed P_ ((struct frame *));
679 static int single_display_prop_intangible_p P_ ((Lisp_Object));
680 static void ensure_echo_area_buffers P_ ((void));
681 static struct glyph_row *row_containing_pos P_ ((struct window *, int,
682 struct glyph_row *,
683 struct glyph_row *));
684 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
685 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
686 static int with_echo_area_buffer P_ ((struct window *, int,
687 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
688 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
689 static void clear_garbaged_frames P_ ((void));
690 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
691 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
692 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
693 static int display_echo_area P_ ((struct window *));
694 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
695 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
696 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
697 static int string_char_and_length P_ ((unsigned char *, int, int *));
698 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
699 struct text_pos));
700 static int compute_window_start_on_continuation_line P_ ((struct window *));
701 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
702 static void insert_left_trunc_glyphs P_ ((struct it *));
703 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
704 static void extend_face_to_end_of_line P_ ((struct it *));
705 static int append_space P_ ((struct it *, int));
706 static void make_cursor_line_fully_visible P_ ((struct window *));
707 static int try_scrolling P_ ((Lisp_Object, int, int, int, int));
708 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
709 static int trailing_whitespace_p P_ ((int));
710 static int message_log_check_duplicate P_ ((int, int, int, int));
711 int invisible_p P_ ((Lisp_Object, Lisp_Object));
712 int invisible_ellipsis_p P_ ((Lisp_Object, Lisp_Object));
713 static void push_it P_ ((struct it *));
714 static void pop_it P_ ((struct it *));
715 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
716 static void redisplay_internal P_ ((int));
717 static int echo_area_display P_ ((int));
718 static void redisplay_windows P_ ((Lisp_Object));
719 static void redisplay_window P_ ((Lisp_Object, int));
720 static void update_menu_bar P_ ((struct frame *, int));
721 static int try_window_reusing_current_matrix P_ ((struct window *));
722 static int try_window_id P_ ((struct window *));
723 static int display_line P_ ((struct it *));
724 static int display_mode_lines P_ ((struct window *));
725 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
726 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object));
727 static char *decode_mode_spec P_ ((struct window *, int, int, int));
728 static void display_menu_bar P_ ((struct window *));
729 static int display_count_lines P_ ((int, int, int, int, int *));
730 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
731 int, int, struct it *, int, int, int, int));
732 static void compute_line_metrics P_ ((struct it *));
733 static void run_redisplay_end_trigger_hook P_ ((struct it *));
734 static int get_overlay_strings P_ ((struct it *));
735 static void next_overlay_string P_ ((struct it *));
736 static void reseat P_ ((struct it *, struct text_pos, int));
737 static void reseat_1 P_ ((struct it *, struct text_pos, int));
738 static void back_to_previous_visible_line_start P_ ((struct it *));
739 static void reseat_at_previous_visible_line_start P_ ((struct it *));
740 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
741 static int next_element_from_display_vector P_ ((struct it *));
742 static int next_element_from_string P_ ((struct it *));
743 static int next_element_from_c_string P_ ((struct it *));
744 static int next_element_from_buffer P_ ((struct it *));
745 static int next_element_from_composition P_ ((struct it *));
746 static int next_element_from_image P_ ((struct it *));
747 static int next_element_from_stretch P_ ((struct it *));
748 static void load_overlay_strings P_ ((struct it *));
749 static void init_from_display_pos P_ ((struct it *, struct window *,
750 struct display_pos *));
751 static void reseat_to_string P_ ((struct it *, unsigned char *,
752 Lisp_Object, int, int, int, int));
753 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
754 int, int, int));
755 void move_it_vertically_backward P_ ((struct it *, int));
756 static void init_to_row_start P_ ((struct it *, struct window *,
757 struct glyph_row *));
758 static void init_to_row_end P_ ((struct it *, struct window *,
759 struct glyph_row *));
760 static void back_to_previous_line_start P_ ((struct it *));
761 static int forward_to_next_line_start P_ ((struct it *, int *));
762 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
763 Lisp_Object, int));
764 static struct text_pos string_pos P_ ((int, Lisp_Object));
765 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
766 static int number_of_chars P_ ((unsigned char *, int));
767 static void compute_stop_pos P_ ((struct it *));
768 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
769 Lisp_Object));
770 static int face_before_or_after_it_pos P_ ((struct it *, int));
771 static int next_overlay_change P_ ((int));
772 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
773 Lisp_Object, struct text_pos *,
774 int));
775 static int underlying_face_id P_ ((struct it *));
776 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
777 struct window *));
778
779 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
780 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
781
782 #ifdef HAVE_WINDOW_SYSTEM
783
784 static void update_tool_bar P_ ((struct frame *, int));
785 static void build_desired_tool_bar_string P_ ((struct frame *f));
786 static int redisplay_tool_bar P_ ((struct frame *));
787 static void display_tool_bar_line P_ ((struct it *));
788
789 #endif /* HAVE_WINDOW_SYSTEM */
790
791 \f
792 /***********************************************************************
793 Window display dimensions
794 ***********************************************************************/
795
796 /* Return the window-relative maximum y + 1 for glyph rows displaying
797 text in window W. This is the height of W minus the height of a
798 mode line, if any. */
799
800 INLINE int
801 window_text_bottom_y (w)
802 struct window *w;
803 {
804 struct frame *f = XFRAME (w->frame);
805 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
806
807 if (WINDOW_WANTS_MODELINE_P (w))
808 height -= CURRENT_MODE_LINE_HEIGHT (w);
809 return height;
810 }
811
812
813 /* Return the pixel width of display area AREA of window W. AREA < 0
814 means return the total width of W, not including bitmap areas to
815 the left and right of the window. */
816
817 INLINE int
818 window_box_width (w, area)
819 struct window *w;
820 int area;
821 {
822 struct frame *f = XFRAME (w->frame);
823 int width = XFASTINT (w->width);
824
825 if (!w->pseudo_window_p)
826 {
827 width -= FRAME_SCROLL_BAR_WIDTH (f) + FRAME_FLAGS_AREA_COLS (f);
828
829 if (area == TEXT_AREA)
830 {
831 if (INTEGERP (w->left_margin_width))
832 width -= XFASTINT (w->left_margin_width);
833 if (INTEGERP (w->right_margin_width))
834 width -= XFASTINT (w->right_margin_width);
835 }
836 else if (area == LEFT_MARGIN_AREA)
837 width = (INTEGERP (w->left_margin_width)
838 ? XFASTINT (w->left_margin_width) : 0);
839 else if (area == RIGHT_MARGIN_AREA)
840 width = (INTEGERP (w->right_margin_width)
841 ? XFASTINT (w->right_margin_width) : 0);
842 }
843
844 return width * CANON_X_UNIT (f);
845 }
846
847
848 /* Return the pixel height of the display area of window W, not
849 including mode lines of W, if any.. */
850
851 INLINE int
852 window_box_height (w)
853 struct window *w;
854 {
855 struct frame *f = XFRAME (w->frame);
856 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
857
858 xassert (height >= 0);
859
860 /* Note: the code below that determines the mode-line/header-line
861 height is essentially the same as that contained in the macro
862 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
863 the appropriate glyph row has its `mode_line_p' flag set,
864 and if it doesn't, uses estimate_mode_line_height instead. */
865
866 if (WINDOW_WANTS_MODELINE_P (w))
867 {
868 struct glyph_row *ml_row
869 = (w->current_matrix && w->current_matrix->rows
870 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
871 : 0);
872 if (ml_row && ml_row->mode_line_p)
873 height -= ml_row->height;
874 else
875 height -= estimate_mode_line_height (f, MODE_LINE_FACE_ID);
876 }
877
878 if (WINDOW_WANTS_HEADER_LINE_P (w))
879 {
880 struct glyph_row *hl_row
881 = (w->current_matrix && w->current_matrix->rows
882 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
883 : 0);
884 if (hl_row && hl_row->mode_line_p)
885 height -= hl_row->height;
886 else
887 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
888 }
889
890 return height;
891 }
892
893
894 /* Return the frame-relative coordinate of the left edge of display
895 area AREA of window W. AREA < 0 means return the left edge of the
896 whole window, to the right of any bitmap area at the left side of
897 W. */
898
899 INLINE int
900 window_box_left (w, area)
901 struct window *w;
902 int area;
903 {
904 struct frame *f = XFRAME (w->frame);
905 int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
906
907 if (!w->pseudo_window_p)
908 {
909 x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
910 + FRAME_LEFT_FLAGS_AREA_WIDTH (f));
911
912 if (area == TEXT_AREA)
913 x += window_box_width (w, LEFT_MARGIN_AREA);
914 else if (area == RIGHT_MARGIN_AREA)
915 x += (window_box_width (w, LEFT_MARGIN_AREA)
916 + window_box_width (w, TEXT_AREA));
917 }
918
919 return x;
920 }
921
922
923 /* Return the frame-relative coordinate of the right edge of display
924 area AREA of window W. AREA < 0 means return the left edge of the
925 whole window, to the left of any bitmap area at the right side of
926 W. */
927
928 INLINE int
929 window_box_right (w, area)
930 struct window *w;
931 int area;
932 {
933 return window_box_left (w, area) + window_box_width (w, area);
934 }
935
936
937 /* Get the bounding box of the display area AREA of window W, without
938 mode lines, in frame-relative coordinates. AREA < 0 means the
939 whole window, not including bitmap areas to the left and right of
940 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
941 coordinates of the upper-left corner of the box. Return in
942 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
943
944 INLINE void
945 window_box (w, area, box_x, box_y, box_width, box_height)
946 struct window *w;
947 int area;
948 int *box_x, *box_y, *box_width, *box_height;
949 {
950 struct frame *f = XFRAME (w->frame);
951
952 *box_width = window_box_width (w, area);
953 *box_height = window_box_height (w);
954 *box_x = window_box_left (w, area);
955 *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
956 + XFASTINT (w->top) * CANON_Y_UNIT (f));
957 if (WINDOW_WANTS_HEADER_LINE_P (w))
958 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
959 }
960
961
962 /* Get the bounding box of the display area AREA of window W, without
963 mode lines. AREA < 0 means the whole window, not including bitmap
964 areas to the left and right of the window. Return in *TOP_LEFT_X
965 and TOP_LEFT_Y the frame-relative pixel coordinates of the
966 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
967 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
968 box. */
969
970 INLINE void
971 window_box_edges (w, area, top_left_x, top_left_y,
972 bottom_right_x, bottom_right_y)
973 struct window *w;
974 int area;
975 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
976 {
977 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
978 bottom_right_y);
979 *bottom_right_x += *top_left_x;
980 *bottom_right_y += *top_left_y;
981 }
982
983
984 \f
985 /***********************************************************************
986 Utilities
987 ***********************************************************************/
988
989 /* Return the bottom y-position of the line the iterator IT is in.
990 This can modify IT's settings. */
991
992 int
993 line_bottom_y (it)
994 struct it *it;
995 {
996 int line_height = it->max_ascent + it->max_descent;
997 int line_top_y = it->current_y;
998
999 if (line_height == 0)
1000 {
1001 if (last_height)
1002 line_height = last_height;
1003 else if (IT_CHARPOS (*it) < ZV)
1004 {
1005 move_it_by_lines (it, 1, 1);
1006 line_height = (it->max_ascent || it->max_descent
1007 ? it->max_ascent + it->max_descent
1008 : last_height);
1009 }
1010 else
1011 {
1012 struct glyph_row *row = it->glyph_row;
1013
1014 /* Use the default character height. */
1015 it->glyph_row = NULL;
1016 it->what = IT_CHARACTER;
1017 it->c = ' ';
1018 it->len = 1;
1019 PRODUCE_GLYPHS (it);
1020 line_height = it->ascent + it->descent;
1021 it->glyph_row = row;
1022 }
1023 }
1024
1025 return line_top_y + line_height;
1026 }
1027
1028
1029 /* Return 1 if position CHARPOS is visible in window W. Set *FULLY to
1030 1 if POS is visible and the line containing POS is fully visible.
1031 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1032 and header-lines heights. */
1033
1034 int
1035 pos_visible_p (w, charpos, fully, exact_mode_line_heights_p)
1036 struct window *w;
1037 int charpos, *fully, exact_mode_line_heights_p;
1038 {
1039 struct it it;
1040 struct text_pos top;
1041 int visible_p;
1042 struct buffer *old_buffer = NULL;
1043
1044 if (XBUFFER (w->buffer) != current_buffer)
1045 {
1046 old_buffer = current_buffer;
1047 set_buffer_internal_1 (XBUFFER (w->buffer));
1048 }
1049
1050 *fully = visible_p = 0;
1051 SET_TEXT_POS_FROM_MARKER (top, w->start);
1052
1053 /* Compute exact mode line heights, if requested. */
1054 if (exact_mode_line_heights_p)
1055 {
1056 if (WINDOW_WANTS_MODELINE_P (w))
1057 current_mode_line_height
1058 = display_mode_line (w, MODE_LINE_FACE_ID,
1059 current_buffer->mode_line_format);
1060
1061 if (WINDOW_WANTS_HEADER_LINE_P (w))
1062 current_header_line_height
1063 = display_mode_line (w, HEADER_LINE_FACE_ID,
1064 current_buffer->header_line_format);
1065 }
1066
1067 start_display (&it, w, top);
1068 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
1069 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
1070
1071 /* Note that we may overshoot because of invisible text. */
1072 if (IT_CHARPOS (it) >= charpos)
1073 {
1074 int top_y = it.current_y;
1075 int bottom_y = line_bottom_y (&it);
1076 int window_top_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
1077
1078 if (top_y < window_top_y)
1079 visible_p = bottom_y > window_top_y;
1080 else if (top_y < it.last_visible_y)
1081 {
1082 visible_p = 1;
1083 *fully = bottom_y <= it.last_visible_y;
1084 }
1085 }
1086 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1087 {
1088 move_it_by_lines (&it, 1, 0);
1089 if (charpos < IT_CHARPOS (it))
1090 {
1091 visible_p = 1;
1092 *fully = 0;
1093 }
1094 }
1095
1096 if (old_buffer)
1097 set_buffer_internal_1 (old_buffer);
1098
1099 current_header_line_height = current_mode_line_height = -1;
1100 return visible_p;
1101 }
1102
1103
1104 /* Return the next character from STR which is MAXLEN bytes long.
1105 Return in *LEN the length of the character. This is like
1106 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1107 we find one, we return a `?', but with the length of the invalid
1108 character. */
1109
1110 static INLINE int
1111 string_char_and_length (str, maxlen, len)
1112 unsigned char *str;
1113 int maxlen, *len;
1114 {
1115 int c;
1116
1117 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1118 if (!CHAR_VALID_P (c, 1))
1119 /* We may not change the length here because other places in Emacs
1120 don't use this function, i.e. they silently accept invalid
1121 characters. */
1122 c = '?';
1123
1124 return c;
1125 }
1126
1127
1128
1129 /* Given a position POS containing a valid character and byte position
1130 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1131
1132 static struct text_pos
1133 string_pos_nchars_ahead (pos, string, nchars)
1134 struct text_pos pos;
1135 Lisp_Object string;
1136 int nchars;
1137 {
1138 xassert (STRINGP (string) && nchars >= 0);
1139
1140 if (STRING_MULTIBYTE (string))
1141 {
1142 int rest = STRING_BYTES (XSTRING (string)) - BYTEPOS (pos);
1143 unsigned char *p = XSTRING (string)->data + BYTEPOS (pos);
1144 int len;
1145
1146 while (nchars--)
1147 {
1148 string_char_and_length (p, rest, &len);
1149 p += len, rest -= len;
1150 xassert (rest >= 0);
1151 CHARPOS (pos) += 1;
1152 BYTEPOS (pos) += len;
1153 }
1154 }
1155 else
1156 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1157
1158 return pos;
1159 }
1160
1161
1162 /* Value is the text position, i.e. character and byte position,
1163 for character position CHARPOS in STRING. */
1164
1165 static INLINE struct text_pos
1166 string_pos (charpos, string)
1167 int charpos;
1168 Lisp_Object string;
1169 {
1170 struct text_pos pos;
1171 xassert (STRINGP (string));
1172 xassert (charpos >= 0);
1173 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1174 return pos;
1175 }
1176
1177
1178 /* Value is a text position, i.e. character and byte position, for
1179 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1180 means recognize multibyte characters. */
1181
1182 static struct text_pos
1183 c_string_pos (charpos, s, multibyte_p)
1184 int charpos;
1185 unsigned char *s;
1186 int multibyte_p;
1187 {
1188 struct text_pos pos;
1189
1190 xassert (s != NULL);
1191 xassert (charpos >= 0);
1192
1193 if (multibyte_p)
1194 {
1195 int rest = strlen (s), len;
1196
1197 SET_TEXT_POS (pos, 0, 0);
1198 while (charpos--)
1199 {
1200 string_char_and_length (s, rest, &len);
1201 s += len, rest -= len;
1202 xassert (rest >= 0);
1203 CHARPOS (pos) += 1;
1204 BYTEPOS (pos) += len;
1205 }
1206 }
1207 else
1208 SET_TEXT_POS (pos, charpos, charpos);
1209
1210 return pos;
1211 }
1212
1213
1214 /* Value is the number of characters in C string S. MULTIBYTE_P
1215 non-zero means recognize multibyte characters. */
1216
1217 static int
1218 number_of_chars (s, multibyte_p)
1219 unsigned char *s;
1220 int multibyte_p;
1221 {
1222 int nchars;
1223
1224 if (multibyte_p)
1225 {
1226 int rest = strlen (s), len;
1227 unsigned char *p = (unsigned char *) s;
1228
1229 for (nchars = 0; rest > 0; ++nchars)
1230 {
1231 string_char_and_length (p, rest, &len);
1232 rest -= len, p += len;
1233 }
1234 }
1235 else
1236 nchars = strlen (s);
1237
1238 return nchars;
1239 }
1240
1241
1242 /* Compute byte position NEWPOS->bytepos corresponding to
1243 NEWPOS->charpos. POS is a known position in string STRING.
1244 NEWPOS->charpos must be >= POS.charpos. */
1245
1246 static void
1247 compute_string_pos (newpos, pos, string)
1248 struct text_pos *newpos, pos;
1249 Lisp_Object string;
1250 {
1251 xassert (STRINGP (string));
1252 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1253
1254 if (STRING_MULTIBYTE (string))
1255 *newpos = string_pos_nchars_ahead (pos, string,
1256 CHARPOS (*newpos) - CHARPOS (pos));
1257 else
1258 BYTEPOS (*newpos) = CHARPOS (*newpos);
1259 }
1260
1261
1262 \f
1263 /***********************************************************************
1264 Lisp form evaluation
1265 ***********************************************************************/
1266
1267 /* Error handler for safe_eval and safe_call. */
1268
1269 static Lisp_Object
1270 safe_eval_handler (arg)
1271 Lisp_Object arg;
1272 {
1273 add_to_log ("Error during redisplay: %s", arg, Qnil);
1274 return Qnil;
1275 }
1276
1277
1278 /* Evaluate SEXPR and return the result, or nil if something went
1279 wrong. */
1280
1281 Lisp_Object
1282 safe_eval (sexpr)
1283 Lisp_Object sexpr;
1284 {
1285 Lisp_Object val;
1286
1287 if (inhibit_eval_during_redisplay)
1288 val = Qnil;
1289 else
1290 {
1291 int count = BINDING_STACK_SIZE ();
1292 struct gcpro gcpro1;
1293
1294 GCPRO1 (sexpr);
1295 specbind (Qinhibit_redisplay, Qt);
1296 val = internal_condition_case_1 (Feval, sexpr, Qerror,
1297 safe_eval_handler);
1298 UNGCPRO;
1299 val = unbind_to (count, val);
1300 }
1301
1302 return val;
1303 }
1304
1305
1306 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1307 Return the result, or nil if something went wrong. */
1308
1309 Lisp_Object
1310 safe_call (nargs, args)
1311 int nargs;
1312 Lisp_Object *args;
1313 {
1314 Lisp_Object val;
1315
1316 if (inhibit_eval_during_redisplay)
1317 val = Qnil;
1318 else
1319 {
1320 int count = BINDING_STACK_SIZE ();
1321 struct gcpro gcpro1;
1322
1323 GCPRO1 (args[0]);
1324 gcpro1.nvars = nargs;
1325 specbind (Qinhibit_redisplay, Qt);
1326 val = internal_condition_case_2 (Ffuncall, nargs, args, Qerror,
1327 safe_eval_handler);
1328 UNGCPRO;
1329 val = unbind_to (count, val);
1330 }
1331
1332 return val;
1333 }
1334
1335
1336 /* Call function FN with one argument ARG.
1337 Return the result, or nil if something went wrong. */
1338
1339 Lisp_Object
1340 safe_call1 (fn, arg)
1341 Lisp_Object fn, arg;
1342 {
1343 Lisp_Object args[2];
1344 args[0] = fn;
1345 args[1] = arg;
1346 return safe_call (2, args);
1347 }
1348
1349
1350 \f
1351 /***********************************************************************
1352 Debugging
1353 ***********************************************************************/
1354
1355 #if 0
1356
1357 /* Define CHECK_IT to perform sanity checks on iterators.
1358 This is for debugging. It is too slow to do unconditionally. */
1359
1360 static void
1361 check_it (it)
1362 struct it *it;
1363 {
1364 if (it->method == next_element_from_string)
1365 {
1366 xassert (STRINGP (it->string));
1367 xassert (IT_STRING_CHARPOS (*it) >= 0);
1368 }
1369 else if (it->method == next_element_from_buffer)
1370 {
1371 /* Check that character and byte positions agree. */
1372 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1373 }
1374
1375 if (it->dpvec)
1376 xassert (it->current.dpvec_index >= 0);
1377 else
1378 xassert (it->current.dpvec_index < 0);
1379 }
1380
1381 #define CHECK_IT(IT) check_it ((IT))
1382
1383 #else /* not 0 */
1384
1385 #define CHECK_IT(IT) (void) 0
1386
1387 #endif /* not 0 */
1388
1389
1390 #if GLYPH_DEBUG
1391
1392 /* Check that the window end of window W is what we expect it
1393 to be---the last row in the current matrix displaying text. */
1394
1395 static void
1396 check_window_end (w)
1397 struct window *w;
1398 {
1399 if (!MINI_WINDOW_P (w)
1400 && !NILP (w->window_end_valid))
1401 {
1402 struct glyph_row *row;
1403 xassert ((row = MATRIX_ROW (w->current_matrix,
1404 XFASTINT (w->window_end_vpos)),
1405 !row->enabled_p
1406 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1407 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1408 }
1409 }
1410
1411 #define CHECK_WINDOW_END(W) check_window_end ((W))
1412
1413 #else /* not GLYPH_DEBUG */
1414
1415 #define CHECK_WINDOW_END(W) (void) 0
1416
1417 #endif /* not GLYPH_DEBUG */
1418
1419
1420 \f
1421 /***********************************************************************
1422 Iterator initialization
1423 ***********************************************************************/
1424
1425 /* Initialize IT for displaying current_buffer in window W, starting
1426 at character position CHARPOS. CHARPOS < 0 means that no buffer
1427 position is specified which is useful when the iterator is assigned
1428 a position later. BYTEPOS is the byte position corresponding to
1429 CHARPOS. BYTEPOS <= 0 means compute it from CHARPOS.
1430
1431 If ROW is not null, calls to produce_glyphs with IT as parameter
1432 will produce glyphs in that row.
1433
1434 BASE_FACE_ID is the id of a base face to use. It must be one of
1435 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID or
1436 HEADER_LINE_FACE_ID for displaying mode lines, or TOOL_BAR_FACE_ID for
1437 displaying the tool-bar.
1438
1439 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID or
1440 HEADER_LINE_FACE_ID, the iterator will be initialized to use the
1441 corresponding mode line glyph row of the desired matrix of W. */
1442
1443 void
1444 init_iterator (it, w, charpos, bytepos, row, base_face_id)
1445 struct it *it;
1446 struct window *w;
1447 int charpos, bytepos;
1448 struct glyph_row *row;
1449 enum face_id base_face_id;
1450 {
1451 int highlight_region_p;
1452
1453 /* Some precondition checks. */
1454 xassert (w != NULL && it != NULL);
1455 xassert (charpos < 0 || (charpos > 0 && charpos <= ZV));
1456
1457 /* If face attributes have been changed since the last redisplay,
1458 free realized faces now because they depend on face definitions
1459 that might have changed. */
1460 if (face_change_count)
1461 {
1462 face_change_count = 0;
1463 free_all_realized_faces (Qnil);
1464 }
1465
1466 /* Use one of the mode line rows of W's desired matrix if
1467 appropriate. */
1468 if (row == NULL)
1469 {
1470 if (base_face_id == MODE_LINE_FACE_ID)
1471 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
1472 else if (base_face_id == HEADER_LINE_FACE_ID)
1473 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
1474 }
1475
1476 /* Clear IT. */
1477 bzero (it, sizeof *it);
1478 it->current.overlay_string_index = -1;
1479 it->current.dpvec_index = -1;
1480 it->base_face_id = base_face_id;
1481
1482 /* The window in which we iterate over current_buffer: */
1483 XSETWINDOW (it->window, w);
1484 it->w = w;
1485 it->f = XFRAME (w->frame);
1486
1487 /* Extra space between lines (on window systems only). */
1488 if (base_face_id == DEFAULT_FACE_ID
1489 && FRAME_WINDOW_P (it->f))
1490 {
1491 if (NATNUMP (current_buffer->extra_line_spacing))
1492 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
1493 else if (it->f->extra_line_spacing > 0)
1494 it->extra_line_spacing = it->f->extra_line_spacing;
1495 }
1496
1497 /* If realized faces have been removed, e.g. because of face
1498 attribute changes of named faces, recompute them. When running
1499 in batch mode, the face cache of Vterminal_frame is null. If
1500 we happen to get called, make a dummy face cache. */
1501 if (
1502 #ifndef WINDOWSNT
1503 noninteractive &&
1504 #endif
1505 FRAME_FACE_CACHE (it->f) == NULL)
1506 init_frame_faces (it->f);
1507 if (FRAME_FACE_CACHE (it->f)->used == 0)
1508 recompute_basic_faces (it->f);
1509
1510 /* Current value of the `space-width', and 'height' properties. */
1511 it->space_width = Qnil;
1512 it->font_height = Qnil;
1513
1514 /* Are control characters displayed as `^C'? */
1515 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
1516
1517 /* -1 means everything between a CR and the following line end
1518 is invisible. >0 means lines indented more than this value are
1519 invisible. */
1520 it->selective = (INTEGERP (current_buffer->selective_display)
1521 ? XFASTINT (current_buffer->selective_display)
1522 : (!NILP (current_buffer->selective_display)
1523 ? -1 : 0));
1524 it->selective_display_ellipsis_p
1525 = !NILP (current_buffer->selective_display_ellipses);
1526
1527 /* Display table to use. */
1528 it->dp = window_display_table (w);
1529
1530 /* Are multibyte characters enabled in current_buffer? */
1531 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1532
1533 /* Non-zero if we should highlight the region. */
1534 highlight_region_p
1535 = (!NILP (Vtransient_mark_mode)
1536 && !NILP (current_buffer->mark_active)
1537 && XMARKER (current_buffer->mark)->buffer != 0);
1538
1539 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
1540 start and end of a visible region in window IT->w. Set both to
1541 -1 to indicate no region. */
1542 if (highlight_region_p
1543 /* Maybe highlight only in selected window. */
1544 && (/* Either show region everywhere. */
1545 highlight_nonselected_windows
1546 /* Or show region in the selected window. */
1547 || w == XWINDOW (selected_window)
1548 /* Or show the region if we are in the mini-buffer and W is
1549 the window the mini-buffer refers to. */
1550 || (MINI_WINDOW_P (XWINDOW (selected_window))
1551 && WINDOWP (Vminibuf_scroll_window)
1552 && w == XWINDOW (Vminibuf_scroll_window))))
1553 {
1554 int charpos = marker_position (current_buffer->mark);
1555 it->region_beg_charpos = min (PT, charpos);
1556 it->region_end_charpos = max (PT, charpos);
1557 }
1558 else
1559 it->region_beg_charpos = it->region_end_charpos = -1;
1560
1561 /* Get the position at which the redisplay_end_trigger hook should
1562 be run, if it is to be run at all. */
1563 if (MARKERP (w->redisplay_end_trigger)
1564 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
1565 it->redisplay_end_trigger_charpos
1566 = marker_position (w->redisplay_end_trigger);
1567 else if (INTEGERP (w->redisplay_end_trigger))
1568 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
1569
1570 /* Correct bogus values of tab_width. */
1571 it->tab_width = XINT (current_buffer->tab_width);
1572 if (it->tab_width <= 0 || it->tab_width > 1000)
1573 it->tab_width = 8;
1574
1575 /* Are lines in the display truncated? */
1576 it->truncate_lines_p
1577 = (base_face_id != DEFAULT_FACE_ID
1578 || XINT (it->w->hscroll)
1579 || (truncate_partial_width_windows
1580 && !WINDOW_FULL_WIDTH_P (it->w))
1581 || !NILP (current_buffer->truncate_lines));
1582
1583 /* Get dimensions of truncation and continuation glyphs. These are
1584 displayed as bitmaps under X, so we don't need them for such
1585 frames. */
1586 if (!FRAME_WINDOW_P (it->f))
1587 {
1588 if (it->truncate_lines_p)
1589 {
1590 /* We will need the truncation glyph. */
1591 xassert (it->glyph_row == NULL);
1592 produce_special_glyphs (it, IT_TRUNCATION);
1593 it->truncation_pixel_width = it->pixel_width;
1594 }
1595 else
1596 {
1597 /* We will need the continuation glyph. */
1598 xassert (it->glyph_row == NULL);
1599 produce_special_glyphs (it, IT_CONTINUATION);
1600 it->continuation_pixel_width = it->pixel_width;
1601 }
1602
1603 /* Reset these values to zero becaue the produce_special_glyphs
1604 above has changed them. */
1605 it->pixel_width = it->ascent = it->descent = 0;
1606 it->phys_ascent = it->phys_descent = 0;
1607 }
1608
1609 /* Set this after getting the dimensions of truncation and
1610 continuation glyphs, so that we don't produce glyphs when calling
1611 produce_special_glyphs, above. */
1612 it->glyph_row = row;
1613 it->area = TEXT_AREA;
1614
1615 /* Get the dimensions of the display area. The display area
1616 consists of the visible window area plus a horizontally scrolled
1617 part to the left of the window. All x-values are relative to the
1618 start of this total display area. */
1619 if (base_face_id != DEFAULT_FACE_ID)
1620 {
1621 /* Mode lines, menu bar in terminal frames. */
1622 it->first_visible_x = 0;
1623 it->last_visible_x = XFASTINT (w->width) * CANON_X_UNIT (it->f);
1624 }
1625 else
1626 {
1627 it->first_visible_x
1628 = XFASTINT (it->w->hscroll) * CANON_X_UNIT (it->f);
1629 it->last_visible_x = (it->first_visible_x
1630 + window_box_width (w, TEXT_AREA));
1631
1632 /* If we truncate lines, leave room for the truncator glyph(s) at
1633 the right margin. Otherwise, leave room for the continuation
1634 glyph(s). Truncation and continuation glyphs are not inserted
1635 for window-based redisplay. */
1636 if (!FRAME_WINDOW_P (it->f))
1637 {
1638 if (it->truncate_lines_p)
1639 it->last_visible_x -= it->truncation_pixel_width;
1640 else
1641 it->last_visible_x -= it->continuation_pixel_width;
1642 }
1643
1644 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
1645 it->current_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w) + w->vscroll;
1646 }
1647
1648 /* Leave room for a border glyph. */
1649 if (!FRAME_WINDOW_P (it->f)
1650 && !WINDOW_RIGHTMOST_P (it->w))
1651 it->last_visible_x -= 1;
1652
1653 it->last_visible_y = window_text_bottom_y (w);
1654
1655 /* For mode lines and alike, arrange for the first glyph having a
1656 left box line if the face specifies a box. */
1657 if (base_face_id != DEFAULT_FACE_ID)
1658 {
1659 struct face *face;
1660
1661 it->face_id = base_face_id;
1662
1663 /* If we have a boxed mode line, make the first character appear
1664 with a left box line. */
1665 face = FACE_FROM_ID (it->f, base_face_id);
1666 if (face->box != FACE_NO_BOX)
1667 it->start_of_box_run_p = 1;
1668 }
1669
1670 /* If a buffer position was specified, set the iterator there,
1671 getting overlays and face properties from that position. */
1672 if (charpos > 0)
1673 {
1674 it->end_charpos = ZV;
1675 it->face_id = -1;
1676 IT_CHARPOS (*it) = charpos;
1677
1678 /* Compute byte position if not specified. */
1679 if (bytepos <= 0)
1680 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
1681 else
1682 IT_BYTEPOS (*it) = bytepos;
1683
1684 /* Compute faces etc. */
1685 reseat (it, it->current.pos, 1);
1686 }
1687
1688 CHECK_IT (it);
1689 }
1690
1691
1692 /* Initialize IT for the display of window W with window start POS. */
1693
1694 void
1695 start_display (it, w, pos)
1696 struct it *it;
1697 struct window *w;
1698 struct text_pos pos;
1699 {
1700 int start_at_line_beg_p;
1701 struct glyph_row *row;
1702 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
1703 int first_y;
1704
1705 row = w->desired_matrix->rows + first_vpos;
1706 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
1707 first_y = it->current_y;
1708
1709 /* If window start is not at a line start, move back to the line
1710 start. This makes sure that we take continuation lines into
1711 account. */
1712 start_at_line_beg_p = (CHARPOS (pos) == BEGV
1713 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
1714 if (!start_at_line_beg_p)
1715 reseat_at_previous_visible_line_start (it);
1716
1717 /* If window start is not at a line start, skip forward to POS to
1718 get the correct continuation_lines_width and current_x. */
1719 if (!start_at_line_beg_p)
1720 {
1721 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
1722
1723 /* If lines are continued, this line may end in the middle of a
1724 multi-glyph character (e.g. a control character displayed as
1725 \003, or in the middle of an overlay string). In this case
1726 move_it_to above will not have taken us to the start of
1727 the continuation line but to the end of the continued line. */
1728 if (!it->truncate_lines_p)
1729 {
1730 if (it->current_x > 0)
1731 {
1732 if (it->current.dpvec_index >= 0
1733 || it->current.overlay_string_index >= 0)
1734 {
1735 set_iterator_to_next (it, 1);
1736 move_it_in_display_line_to (it, -1, -1, 0);
1737 }
1738
1739 it->continuation_lines_width += it->current_x;
1740 }
1741
1742 /* We're starting a new display line, not affected by the
1743 height of the continued line, so clear the appropriate
1744 fields in the iterator structure. */
1745 it->max_ascent = it->max_descent = 0;
1746 it->max_phys_ascent = it->max_phys_descent = 0;
1747 }
1748
1749 it->current_y = first_y;
1750 it->vpos = 0;
1751 it->current_x = it->hpos = 0;
1752 }
1753
1754 #if 0 /* Don't assert the following because start_display is sometimes
1755 called intentionally with a window start that is not at a
1756 line start. Please leave this code in as a comment. */
1757
1758 /* Window start should be on a line start, now. */
1759 xassert (it->continuation_lines_width
1760 || IT_CHARPOS (it) == BEGV
1761 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
1762 #endif /* 0 */
1763 }
1764
1765
1766 /* Return 1 if POS is a position in ellipses displayed for invisible
1767 text. W is the window we display, for text property lookup. */
1768
1769 static int
1770 in_ellipses_for_invisible_text_p (pos, w)
1771 struct display_pos *pos;
1772 struct window *w;
1773 {
1774 Lisp_Object prop, window;
1775 int ellipses_p = 0;
1776 int charpos = CHARPOS (pos->pos);
1777
1778 /* If POS specifies a position in a display vector, this might
1779 be for an ellipsis displayed for invisible text. We won't
1780 get the iterator set up for delivering that ellipsis unless
1781 we make sure that it gets aware of the invisible text. */
1782 if (pos->dpvec_index >= 0
1783 && pos->overlay_string_index < 0
1784 && CHARPOS (pos->string_pos) < 0
1785 && charpos > BEGV
1786 && (XSETWINDOW (window, w),
1787 prop = Fget_char_property (make_number (charpos),
1788 Qinvisible, window),
1789 !TEXT_PROP_MEANS_INVISIBLE (prop)))
1790 {
1791 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
1792 window);
1793 if (TEXT_PROP_MEANS_INVISIBLE (prop)
1794 && TEXT_PROP_MEANS_INVISIBLE_WITH_ELLIPSIS (prop))
1795 ellipses_p = 1;
1796 }
1797
1798 return ellipses_p;
1799 }
1800
1801
1802 /* Initialize IT for stepping through current_buffer in window W,
1803 starting at position POS that includes overlay string and display
1804 vector/ control character translation position information. */
1805
1806 static void
1807 init_from_display_pos (it, w, pos)
1808 struct it *it;
1809 struct window *w;
1810 struct display_pos *pos;
1811 {
1812 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
1813
1814 /* If POS specifies a position in a display vector, this might
1815 be for an ellipsis displayed for invisible text. We won't
1816 get the iterator set up for delivering that ellipsis unless
1817 we make sure that it gets aware of the invisible text. */
1818 if (in_ellipses_for_invisible_text_p (pos, w))
1819 {
1820 --charpos;
1821 bytepos = 0;
1822 }
1823
1824 /* Keep in mind: the call to reseat in init_iterator skips invisible
1825 text, so we might end up at a position different from POS. This
1826 is only a problem when POS is a row start after a newline and an
1827 overlay starts there with an after-string, and the overlay has an
1828 invisible property. Since we don't skip invisible text in
1829 display_line and elsewhere immediately after consuming the
1830 newline before the row start, such a POS will not be in a string,
1831 but the call to init_iterator below will move us to the
1832 after-string. */
1833 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
1834
1835 /* If position is within an overlay string, set up IT to
1836 the right overlay string. */
1837 if (pos->overlay_string_index >= 0)
1838 {
1839 int relative_index;
1840
1841 /* We already have the first chunk of overlay strings in
1842 IT->overlay_strings. Load more until the one for
1843 pos->overlay_string_index is in IT->overlay_strings. */
1844 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
1845 {
1846 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
1847 it->current.overlay_string_index = 0;
1848 while (n--)
1849 {
1850 load_overlay_strings (it);
1851 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
1852 }
1853 }
1854
1855 it->current.overlay_string_index = pos->overlay_string_index;
1856 relative_index = (it->current.overlay_string_index
1857 % OVERLAY_STRING_CHUNK_SIZE);
1858 it->string = it->overlay_strings[relative_index];
1859 xassert (STRINGP (it->string));
1860 it->current.string_pos = pos->string_pos;
1861 it->method = next_element_from_string;
1862 }
1863 else if (it->current.overlay_string_index >= 0)
1864 {
1865 /* If POS says we're already after an overlay string ending at
1866 POS, make sure to pop the iterator because it will be in
1867 front of that overlay string. When POS is ZV, we've thereby
1868 also ``processed'' overlay strings at ZV. */
1869 while (it->sp)
1870 pop_it (it);
1871 it->current.overlay_string_index = -1;
1872 it->method = next_element_from_buffer;
1873 if (CHARPOS (pos->pos) == ZV)
1874 it->overlay_strings_at_end_processed_p = 1;
1875 }
1876
1877 if (CHARPOS (pos->string_pos) >= 0)
1878 {
1879 /* Recorded position is not in an overlay string, but in another
1880 string. This can only be a string from a `display' property.
1881 IT should already be filled with that string. */
1882 it->current.string_pos = pos->string_pos;
1883 xassert (STRINGP (it->string));
1884 }
1885
1886 /* Restore position in display vector translations, control
1887 character translations or ellipses. */
1888 if (pos->dpvec_index >= 0)
1889 {
1890 if (it->dpvec == NULL)
1891 get_next_display_element (it);
1892 xassert (it->dpvec && it->current.dpvec_index == 0);
1893 it->current.dpvec_index = pos->dpvec_index;
1894 }
1895
1896 CHECK_IT (it);
1897 }
1898
1899
1900 /* Initialize IT for stepping through current_buffer in window W
1901 starting at ROW->start. */
1902
1903 static void
1904 init_to_row_start (it, w, row)
1905 struct it *it;
1906 struct window *w;
1907 struct glyph_row *row;
1908 {
1909 init_from_display_pos (it, w, &row->start);
1910 it->continuation_lines_width = row->continuation_lines_width;
1911 CHECK_IT (it);
1912 }
1913
1914
1915 /* Initialize IT for stepping through current_buffer in window W
1916 starting in the line following ROW, i.e. starting at ROW->end. */
1917
1918 static void
1919 init_to_row_end (it, w, row)
1920 struct it *it;
1921 struct window *w;
1922 struct glyph_row *row;
1923 {
1924 init_from_display_pos (it, w, &row->end);
1925
1926 if (row->continued_p)
1927 it->continuation_lines_width = (row->continuation_lines_width
1928 + row->pixel_width);
1929 CHECK_IT (it);
1930 }
1931
1932
1933
1934 \f
1935 /***********************************************************************
1936 Text properties
1937 ***********************************************************************/
1938
1939 /* Called when IT reaches IT->stop_charpos. Handle text property and
1940 overlay changes. Set IT->stop_charpos to the next position where
1941 to stop. */
1942
1943 static void
1944 handle_stop (it)
1945 struct it *it;
1946 {
1947 enum prop_handled handled;
1948 int handle_overlay_change_p = 1;
1949 struct props *p;
1950
1951 it->dpvec = NULL;
1952 it->current.dpvec_index = -1;
1953
1954 do
1955 {
1956 handled = HANDLED_NORMALLY;
1957
1958 /* Call text property handlers. */
1959 for (p = it_props; p->handler; ++p)
1960 {
1961 handled = p->handler (it);
1962
1963 if (handled == HANDLED_RECOMPUTE_PROPS)
1964 break;
1965 else if (handled == HANDLED_RETURN)
1966 return;
1967 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
1968 handle_overlay_change_p = 0;
1969 }
1970
1971 if (handled != HANDLED_RECOMPUTE_PROPS)
1972 {
1973 /* Don't check for overlay strings below when set to deliver
1974 characters from a display vector. */
1975 if (it->method == next_element_from_display_vector)
1976 handle_overlay_change_p = 0;
1977
1978 /* Handle overlay changes. */
1979 if (handle_overlay_change_p)
1980 handled = handle_overlay_change (it);
1981
1982 /* Determine where to stop next. */
1983 if (handled == HANDLED_NORMALLY)
1984 compute_stop_pos (it);
1985 }
1986 }
1987 while (handled == HANDLED_RECOMPUTE_PROPS);
1988 }
1989
1990
1991 /* Compute IT->stop_charpos from text property and overlay change
1992 information for IT's current position. */
1993
1994 static void
1995 compute_stop_pos (it)
1996 struct it *it;
1997 {
1998 register INTERVAL iv, next_iv;
1999 Lisp_Object object, limit, position;
2000
2001 /* If nowhere else, stop at the end. */
2002 it->stop_charpos = it->end_charpos;
2003
2004 if (STRINGP (it->string))
2005 {
2006 /* Strings are usually short, so don't limit the search for
2007 properties. */
2008 object = it->string;
2009 limit = Qnil;
2010 position = make_number (IT_STRING_CHARPOS (*it));
2011 }
2012 else
2013 {
2014 int charpos;
2015
2016 /* If next overlay change is in front of the current stop pos
2017 (which is IT->end_charpos), stop there. Note: value of
2018 next_overlay_change is point-max if no overlay change
2019 follows. */
2020 charpos = next_overlay_change (IT_CHARPOS (*it));
2021 if (charpos < it->stop_charpos)
2022 it->stop_charpos = charpos;
2023
2024 /* If showing the region, we have to stop at the region
2025 start or end because the face might change there. */
2026 if (it->region_beg_charpos > 0)
2027 {
2028 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2029 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2030 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2031 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2032 }
2033
2034 /* Set up variables for computing the stop position from text
2035 property changes. */
2036 XSETBUFFER (object, current_buffer);
2037 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2038 position = make_number (IT_CHARPOS (*it));
2039
2040 }
2041
2042 /* Get the interval containing IT's position. Value is a null
2043 interval if there isn't such an interval. */
2044 iv = validate_interval_range (object, &position, &position, 0);
2045 if (!NULL_INTERVAL_P (iv))
2046 {
2047 Lisp_Object values_here[LAST_PROP_IDX];
2048 struct props *p;
2049
2050 /* Get properties here. */
2051 for (p = it_props; p->handler; ++p)
2052 values_here[p->idx] = textget (iv->plist, *p->name);
2053
2054 /* Look for an interval following iv that has different
2055 properties. */
2056 for (next_iv = next_interval (iv);
2057 (!NULL_INTERVAL_P (next_iv)
2058 && (NILP (limit)
2059 || XFASTINT (limit) > next_iv->position));
2060 next_iv = next_interval (next_iv))
2061 {
2062 for (p = it_props; p->handler; ++p)
2063 {
2064 Lisp_Object new_value;
2065
2066 new_value = textget (next_iv->plist, *p->name);
2067 if (!EQ (values_here[p->idx], new_value))
2068 break;
2069 }
2070
2071 if (p->handler)
2072 break;
2073 }
2074
2075 if (!NULL_INTERVAL_P (next_iv))
2076 {
2077 if (INTEGERP (limit)
2078 && next_iv->position >= XFASTINT (limit))
2079 /* No text property change up to limit. */
2080 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2081 else
2082 /* Text properties change in next_iv. */
2083 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2084 }
2085 }
2086
2087 xassert (STRINGP (it->string)
2088 || (it->stop_charpos >= BEGV
2089 && it->stop_charpos >= IT_CHARPOS (*it)));
2090 }
2091
2092
2093 /* Return the position of the next overlay change after POS in
2094 current_buffer. Value is point-max if no overlay change
2095 follows. This is like `next-overlay-change' but doesn't use
2096 xmalloc. */
2097
2098 static int
2099 next_overlay_change (pos)
2100 int pos;
2101 {
2102 int noverlays;
2103 int endpos;
2104 Lisp_Object *overlays;
2105 int len;
2106 int i;
2107
2108 /* Get all overlays at the given position. */
2109 len = 10;
2110 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2111 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2112 if (noverlays > len)
2113 {
2114 len = noverlays;
2115 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2116 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2117 }
2118
2119 /* If any of these overlays ends before endpos,
2120 use its ending point instead. */
2121 for (i = 0; i < noverlays; ++i)
2122 {
2123 Lisp_Object oend;
2124 int oendpos;
2125
2126 oend = OVERLAY_END (overlays[i]);
2127 oendpos = OVERLAY_POSITION (oend);
2128 endpos = min (endpos, oendpos);
2129 }
2130
2131 return endpos;
2132 }
2133
2134
2135 \f
2136 /***********************************************************************
2137 Fontification
2138 ***********************************************************************/
2139
2140 /* Handle changes in the `fontified' property of the current buffer by
2141 calling hook functions from Qfontification_functions to fontify
2142 regions of text. */
2143
2144 static enum prop_handled
2145 handle_fontified_prop (it)
2146 struct it *it;
2147 {
2148 Lisp_Object prop, pos;
2149 enum prop_handled handled = HANDLED_NORMALLY;
2150
2151 /* Get the value of the `fontified' property at IT's current buffer
2152 position. (The `fontified' property doesn't have a special
2153 meaning in strings.) If the value is nil, call functions from
2154 Qfontification_functions. */
2155 if (!STRINGP (it->string)
2156 && it->s == NULL
2157 && !NILP (Vfontification_functions)
2158 && !NILP (Vrun_hooks)
2159 && (pos = make_number (IT_CHARPOS (*it)),
2160 prop = Fget_char_property (pos, Qfontified, Qnil),
2161 NILP (prop)))
2162 {
2163 int count = BINDING_STACK_SIZE ();
2164 Lisp_Object val;
2165
2166 val = Vfontification_functions;
2167 specbind (Qfontification_functions, Qnil);
2168 specbind (Qafter_change_functions, Qnil);
2169
2170 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2171 safe_call1 (val, pos);
2172 else
2173 {
2174 Lisp_Object globals, fn;
2175 struct gcpro gcpro1, gcpro2;
2176
2177 globals = Qnil;
2178 GCPRO2 (val, globals);
2179
2180 for (; CONSP (val); val = XCDR (val))
2181 {
2182 fn = XCAR (val);
2183
2184 if (EQ (fn, Qt))
2185 {
2186 /* A value of t indicates this hook has a local
2187 binding; it means to run the global binding too.
2188 In a global value, t should not occur. If it
2189 does, we must ignore it to avoid an endless
2190 loop. */
2191 for (globals = Fdefault_value (Qfontification_functions);
2192 CONSP (globals);
2193 globals = XCDR (globals))
2194 {
2195 fn = XCAR (globals);
2196 if (!EQ (fn, Qt))
2197 safe_call1 (fn, pos);
2198 }
2199 }
2200 else
2201 safe_call1 (fn, pos);
2202 }
2203
2204 UNGCPRO;
2205 }
2206
2207 unbind_to (count, Qnil);
2208
2209 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2210 something. This avoids an endless loop if they failed to
2211 fontify the text for which reason ever. */
2212 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2213 handled = HANDLED_RECOMPUTE_PROPS;
2214 }
2215
2216 return handled;
2217 }
2218
2219
2220 \f
2221 /***********************************************************************
2222 Faces
2223 ***********************************************************************/
2224
2225 /* Set up iterator IT from face properties at its current position.
2226 Called from handle_stop. */
2227
2228 static enum prop_handled
2229 handle_face_prop (it)
2230 struct it *it;
2231 {
2232 int new_face_id, next_stop;
2233
2234 if (!STRINGP (it->string))
2235 {
2236 new_face_id
2237 = face_at_buffer_position (it->w,
2238 IT_CHARPOS (*it),
2239 it->region_beg_charpos,
2240 it->region_end_charpos,
2241 &next_stop,
2242 (IT_CHARPOS (*it)
2243 + TEXT_PROP_DISTANCE_LIMIT),
2244 0);
2245
2246 /* Is this a start of a run of characters with box face?
2247 Caveat: this can be called for a freshly initialized
2248 iterator; face_id is -1 is this case. We know that the new
2249 face will not change until limit, i.e. if the new face has a
2250 box, all characters up to limit will have one. But, as
2251 usual, we don't know whether limit is really the end. */
2252 if (new_face_id != it->face_id)
2253 {
2254 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2255
2256 /* If new face has a box but old face has not, this is
2257 the start of a run of characters with box, i.e. it has
2258 a shadow on the left side. The value of face_id of the
2259 iterator will be -1 if this is the initial call that gets
2260 the face. In this case, we have to look in front of IT's
2261 position and see whether there is a face != new_face_id. */
2262 it->start_of_box_run_p
2263 = (new_face->box != FACE_NO_BOX
2264 && (it->face_id >= 0
2265 || IT_CHARPOS (*it) == BEG
2266 || new_face_id != face_before_it_pos (it)));
2267 it->face_box_p = new_face->box != FACE_NO_BOX;
2268 }
2269 }
2270 else
2271 {
2272 int base_face_id, bufpos;
2273
2274 if (it->current.overlay_string_index >= 0)
2275 bufpos = IT_CHARPOS (*it);
2276 else
2277 bufpos = 0;
2278
2279 /* For strings from a buffer, i.e. overlay strings or strings
2280 from a `display' property, use the face at IT's current
2281 buffer position as the base face to merge with, so that
2282 overlay strings appear in the same face as surrounding
2283 text, unless they specify their own faces. */
2284 base_face_id = underlying_face_id (it);
2285
2286 new_face_id = face_at_string_position (it->w,
2287 it->string,
2288 IT_STRING_CHARPOS (*it),
2289 bufpos,
2290 it->region_beg_charpos,
2291 it->region_end_charpos,
2292 &next_stop,
2293 base_face_id, 0);
2294
2295 #if 0 /* This shouldn't be neccessary. Let's check it. */
2296 /* If IT is used to display a mode line we would really like to
2297 use the mode line face instead of the frame's default face. */
2298 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2299 && new_face_id == DEFAULT_FACE_ID)
2300 new_face_id = MODE_LINE_FACE_ID;
2301 #endif
2302
2303 /* Is this a start of a run of characters with box? Caveat:
2304 this can be called for a freshly allocated iterator; face_id
2305 is -1 is this case. We know that the new face will not
2306 change until the next check pos, i.e. if the new face has a
2307 box, all characters up to that position will have a
2308 box. But, as usual, we don't know whether that position
2309 is really the end. */
2310 if (new_face_id != it->face_id)
2311 {
2312 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2313 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2314
2315 /* If new face has a box but old face hasn't, this is the
2316 start of a run of characters with box, i.e. it has a
2317 shadow on the left side. */
2318 it->start_of_box_run_p
2319 = new_face->box && (old_face == NULL || !old_face->box);
2320 it->face_box_p = new_face->box != FACE_NO_BOX;
2321 }
2322 }
2323
2324 it->face_id = new_face_id;
2325 return HANDLED_NORMALLY;
2326 }
2327
2328
2329 /* Return the ID of the face ``underlying'' IT's current position,
2330 which is in a string. If the iterator is associated with a
2331 buffer, return the face at IT's current buffer position.
2332 Otherwise, use the iterator's base_face_id. */
2333
2334 static int
2335 underlying_face_id (it)
2336 struct it *it;
2337 {
2338 int face_id = it->base_face_id, i;
2339
2340 xassert (STRINGP (it->string));
2341
2342 for (i = it->sp - 1; i >= 0; --i)
2343 if (NILP (it->stack[i].string))
2344 face_id = it->stack[i].face_id;
2345
2346 return face_id;
2347 }
2348
2349
2350 /* Compute the face one character before or after the current position
2351 of IT. BEFORE_P non-zero means get the face in front of IT's
2352 position. Value is the id of the face. */
2353
2354 static int
2355 face_before_or_after_it_pos (it, before_p)
2356 struct it *it;
2357 int before_p;
2358 {
2359 int face_id, limit;
2360 int next_check_charpos;
2361 struct text_pos pos;
2362
2363 xassert (it->s == NULL);
2364
2365 if (STRINGP (it->string))
2366 {
2367 int bufpos, base_face_id;
2368
2369 /* No face change past the end of the string (for the case
2370 we are padding with spaces). No face change before the
2371 string start. */
2372 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size
2373 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2374 return it->face_id;
2375
2376 /* Set pos to the position before or after IT's current position. */
2377 if (before_p)
2378 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2379 else
2380 /* For composition, we must check the character after the
2381 composition. */
2382 pos = (it->what == IT_COMPOSITION
2383 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
2384 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
2385
2386 if (it->current.overlay_string_index >= 0)
2387 bufpos = IT_CHARPOS (*it);
2388 else
2389 bufpos = 0;
2390
2391 base_face_id = underlying_face_id (it);
2392
2393 /* Get the face for ASCII, or unibyte. */
2394 face_id = face_at_string_position (it->w,
2395 it->string,
2396 CHARPOS (pos),
2397 bufpos,
2398 it->region_beg_charpos,
2399 it->region_end_charpos,
2400 &next_check_charpos,
2401 base_face_id, 0);
2402
2403 /* Correct the face for charsets different from ASCII. Do it
2404 for the multibyte case only. The face returned above is
2405 suitable for unibyte text if IT->string is unibyte. */
2406 if (STRING_MULTIBYTE (it->string))
2407 {
2408 unsigned char *p = XSTRING (it->string)->data + BYTEPOS (pos);
2409 int rest = STRING_BYTES (XSTRING (it->string)) - BYTEPOS (pos);
2410 int c, len;
2411 struct face *face = FACE_FROM_ID (it->f, face_id);
2412
2413 c = string_char_and_length (p, rest, &len);
2414 face_id = FACE_FOR_CHAR (it->f, face, c);
2415 }
2416 }
2417 else
2418 {
2419 if ((IT_CHARPOS (*it) >= ZV && !before_p)
2420 || (IT_CHARPOS (*it) <= BEGV && before_p))
2421 return it->face_id;
2422
2423 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
2424 pos = it->current.pos;
2425
2426 if (before_p)
2427 DEC_TEXT_POS (pos, it->multibyte_p);
2428 else
2429 {
2430 if (it->what == IT_COMPOSITION)
2431 /* For composition, we must check the position after the
2432 composition. */
2433 pos.charpos += it->cmp_len, pos.bytepos += it->len;
2434 else
2435 INC_TEXT_POS (pos, it->multibyte_p);
2436 }
2437
2438 /* Determine face for CHARSET_ASCII, or unibyte. */
2439 face_id = face_at_buffer_position (it->w,
2440 CHARPOS (pos),
2441 it->region_beg_charpos,
2442 it->region_end_charpos,
2443 &next_check_charpos,
2444 limit, 0);
2445
2446 /* Correct the face for charsets different from ASCII. Do it
2447 for the multibyte case only. The face returned above is
2448 suitable for unibyte text if current_buffer is unibyte. */
2449 if (it->multibyte_p)
2450 {
2451 int c = FETCH_MULTIBYTE_CHAR (CHARPOS (pos));
2452 struct face *face = FACE_FROM_ID (it->f, face_id);
2453 face_id = FACE_FOR_CHAR (it->f, face, c);
2454 }
2455 }
2456
2457 return face_id;
2458 }
2459
2460
2461 \f
2462 /***********************************************************************
2463 Invisible text
2464 ***********************************************************************/
2465
2466 /* Set up iterator IT from invisible properties at its current
2467 position. Called from handle_stop. */
2468
2469 static enum prop_handled
2470 handle_invisible_prop (it)
2471 struct it *it;
2472 {
2473 enum prop_handled handled = HANDLED_NORMALLY;
2474
2475 if (STRINGP (it->string))
2476 {
2477 extern Lisp_Object Qinvisible;
2478 Lisp_Object prop, end_charpos, limit, charpos;
2479
2480 /* Get the value of the invisible text property at the
2481 current position. Value will be nil if there is no such
2482 property. */
2483 charpos = make_number (IT_STRING_CHARPOS (*it));
2484 prop = Fget_text_property (charpos, Qinvisible, it->string);
2485
2486 if (!NILP (prop)
2487 && IT_STRING_CHARPOS (*it) < it->end_charpos)
2488 {
2489 handled = HANDLED_RECOMPUTE_PROPS;
2490
2491 /* Get the position at which the next change of the
2492 invisible text property can be found in IT->string.
2493 Value will be nil if the property value is the same for
2494 all the rest of IT->string. */
2495 XSETINT (limit, XSTRING (it->string)->size);
2496 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2497 it->string, limit);
2498
2499 /* Text at current position is invisible. The next
2500 change in the property is at position end_charpos.
2501 Move IT's current position to that position. */
2502 if (INTEGERP (end_charpos)
2503 && XFASTINT (end_charpos) < XFASTINT (limit))
2504 {
2505 struct text_pos old;
2506 old = it->current.string_pos;
2507 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
2508 compute_string_pos (&it->current.string_pos, old, it->string);
2509 }
2510 else
2511 {
2512 /* The rest of the string is invisible. If this is an
2513 overlay string, proceed with the next overlay string
2514 or whatever comes and return a character from there. */
2515 if (it->current.overlay_string_index >= 0)
2516 {
2517 next_overlay_string (it);
2518 /* Don't check for overlay strings when we just
2519 finished processing them. */
2520 handled = HANDLED_OVERLAY_STRING_CONSUMED;
2521 }
2522 else
2523 {
2524 struct Lisp_String *s = XSTRING (it->string);
2525 IT_STRING_CHARPOS (*it) = s->size;
2526 IT_STRING_BYTEPOS (*it) = STRING_BYTES (s);
2527 }
2528 }
2529 }
2530 }
2531 else
2532 {
2533 int visible_p, newpos, next_stop;
2534 Lisp_Object pos, prop;
2535
2536 /* First of all, is there invisible text at this position? */
2537 pos = make_number (IT_CHARPOS (*it));
2538 prop = Fget_char_property (pos, Qinvisible, it->window);
2539
2540 /* If we are on invisible text, skip over it. */
2541 if (TEXT_PROP_MEANS_INVISIBLE (prop)
2542 && IT_CHARPOS (*it) < it->end_charpos)
2543 {
2544 /* Record whether we have to display an ellipsis for the
2545 invisible text. */
2546 int display_ellipsis_p
2547 = TEXT_PROP_MEANS_INVISIBLE_WITH_ELLIPSIS (prop);
2548
2549 handled = HANDLED_RECOMPUTE_PROPS;
2550
2551 /* Loop skipping over invisible text. The loop is left at
2552 ZV or with IT on the first char being visible again. */
2553 do
2554 {
2555 /* Try to skip some invisible text. Return value is the
2556 position reached which can be equal to IT's position
2557 if there is nothing invisible here. This skips both
2558 over invisible text properties and overlays with
2559 invisible property. */
2560 newpos = skip_invisible (IT_CHARPOS (*it),
2561 &next_stop, ZV, it->window);
2562
2563 /* If we skipped nothing at all we weren't at invisible
2564 text in the first place. If everything to the end of
2565 the buffer was skipped, end the loop. */
2566 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
2567 visible_p = 1;
2568 else
2569 {
2570 /* We skipped some characters but not necessarily
2571 all there are. Check if we ended up on visible
2572 text. Fget_char_property returns the property of
2573 the char before the given position, i.e. if we
2574 get visible_p = 1, this means that the char at
2575 newpos is visible. */
2576 pos = make_number (newpos);
2577 prop = Fget_char_property (pos, Qinvisible, it->window);
2578 visible_p = !TEXT_PROP_MEANS_INVISIBLE (prop);
2579 }
2580
2581 /* If we ended up on invisible text, proceed to
2582 skip starting with next_stop. */
2583 if (!visible_p)
2584 IT_CHARPOS (*it) = next_stop;
2585 }
2586 while (!visible_p);
2587
2588 /* The position newpos is now either ZV or on visible text. */
2589 IT_CHARPOS (*it) = newpos;
2590 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2591
2592 /* Maybe return `...' next for the end of the invisible text. */
2593 if (display_ellipsis_p)
2594 {
2595 if (it->dp
2596 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
2597 {
2598 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
2599 it->dpvec = v->contents;
2600 it->dpend = v->contents + v->size;
2601 }
2602 else
2603 {
2604 /* Default `...'. */
2605 it->dpvec = default_invis_vector;
2606 it->dpend = default_invis_vector + 3;
2607 }
2608
2609 /* The ellipsis display does not replace the display of
2610 the character at the new position. Indicate this by
2611 setting IT->dpvec_char_len to zero. */
2612 it->dpvec_char_len = 0;
2613
2614 it->current.dpvec_index = 0;
2615 it->method = next_element_from_display_vector;
2616 }
2617 }
2618 }
2619
2620 return handled;
2621 }
2622
2623
2624 \f
2625 /***********************************************************************
2626 'display' property
2627 ***********************************************************************/
2628
2629 /* Set up iterator IT from `display' property at its current position.
2630 Called from handle_stop. */
2631
2632 static enum prop_handled
2633 handle_display_prop (it)
2634 struct it *it;
2635 {
2636 Lisp_Object prop, object;
2637 struct text_pos *position;
2638 int display_replaced_p = 0;
2639
2640 if (STRINGP (it->string))
2641 {
2642 object = it->string;
2643 position = &it->current.string_pos;
2644 }
2645 else
2646 {
2647 object = it->w->buffer;
2648 position = &it->current.pos;
2649 }
2650
2651 /* Reset those iterator values set from display property values. */
2652 it->font_height = Qnil;
2653 it->space_width = Qnil;
2654 it->voffset = 0;
2655
2656 /* We don't support recursive `display' properties, i.e. string
2657 values that have a string `display' property, that have a string
2658 `display' property etc. */
2659 if (!it->string_from_display_prop_p)
2660 it->area = TEXT_AREA;
2661
2662 prop = Fget_char_property (make_number (position->charpos),
2663 Qdisplay, object);
2664 if (NILP (prop))
2665 return HANDLED_NORMALLY;
2666
2667 if (CONSP (prop)
2668 /* Simple properties. */
2669 && !EQ (XCAR (prop), Qimage)
2670 && !EQ (XCAR (prop), Qspace)
2671 && !EQ (XCAR (prop), Qwhen)
2672 && !EQ (XCAR (prop), Qspace_width)
2673 && !EQ (XCAR (prop), Qheight)
2674 && !EQ (XCAR (prop), Qraise)
2675 /* Marginal area specifications. */
2676 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
2677 && !NILP (XCAR (prop)))
2678 {
2679 for (; CONSP (prop); prop = XCDR (prop))
2680 {
2681 if (handle_single_display_prop (it, XCAR (prop), object,
2682 position, display_replaced_p))
2683 display_replaced_p = 1;
2684 }
2685 }
2686 else if (VECTORP (prop))
2687 {
2688 int i;
2689 for (i = 0; i < ASIZE (prop); ++i)
2690 if (handle_single_display_prop (it, AREF (prop, i), object,
2691 position, display_replaced_p))
2692 display_replaced_p = 1;
2693 }
2694 else
2695 {
2696 if (handle_single_display_prop (it, prop, object, position, 0))
2697 display_replaced_p = 1;
2698 }
2699
2700 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
2701 }
2702
2703
2704 /* Value is the position of the end of the `display' property starting
2705 at START_POS in OBJECT. */
2706
2707 static struct text_pos
2708 display_prop_end (it, object, start_pos)
2709 struct it *it;
2710 Lisp_Object object;
2711 struct text_pos start_pos;
2712 {
2713 Lisp_Object end;
2714 struct text_pos end_pos;
2715
2716 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
2717 Qdisplay, object, Qnil);
2718 CHARPOS (end_pos) = XFASTINT (end);
2719 if (STRINGP (object))
2720 compute_string_pos (&end_pos, start_pos, it->string);
2721 else
2722 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
2723
2724 return end_pos;
2725 }
2726
2727
2728 /* Set up IT from a single `display' sub-property value PROP. OBJECT
2729 is the object in which the `display' property was found. *POSITION
2730 is the position at which it was found. DISPLAY_REPLACED_P non-zero
2731 means that we previously saw a display sub-property which already
2732 replaced text display with something else, for example an image;
2733 ignore such properties after the first one has been processed.
2734
2735 If PROP is a `space' or `image' sub-property, set *POSITION to the
2736 end position of the `display' property.
2737
2738 Value is non-zero something was found which replaces the display
2739 of buffer or string text. */
2740
2741 static int
2742 handle_single_display_prop (it, prop, object, position,
2743 display_replaced_before_p)
2744 struct it *it;
2745 Lisp_Object prop;
2746 Lisp_Object object;
2747 struct text_pos *position;
2748 int display_replaced_before_p;
2749 {
2750 Lisp_Object value;
2751 int replaces_text_display_p = 0;
2752 Lisp_Object form;
2753
2754 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
2755 evaluated. If the result is nil, VALUE is ignored. */
2756 form = Qt;
2757 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2758 {
2759 prop = XCDR (prop);
2760 if (!CONSP (prop))
2761 return 0;
2762 form = XCAR (prop);
2763 prop = XCDR (prop);
2764 }
2765
2766 if (!NILP (form) && !EQ (form, Qt))
2767 {
2768 struct gcpro gcpro1;
2769 struct text_pos end_pos, pt;
2770
2771 GCPRO1 (form);
2772 end_pos = display_prop_end (it, object, *position);
2773
2774 /* Temporarily set point to the end position, and then evaluate
2775 the form. This makes `(eolp)' work as FORM. */
2776 if (BUFFERP (object))
2777 {
2778 CHARPOS (pt) = PT;
2779 BYTEPOS (pt) = PT_BYTE;
2780 TEMP_SET_PT_BOTH (CHARPOS (end_pos), BYTEPOS (end_pos));
2781 }
2782
2783 form = safe_eval (form);
2784
2785 if (BUFFERP (object))
2786 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
2787 UNGCPRO;
2788 }
2789
2790 if (NILP (form))
2791 return 0;
2792
2793 if (CONSP (prop)
2794 && EQ (XCAR (prop), Qheight)
2795 && CONSP (XCDR (prop)))
2796 {
2797 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2798 return 0;
2799
2800 /* `(height HEIGHT)'. */
2801 it->font_height = XCAR (XCDR (prop));
2802 if (!NILP (it->font_height))
2803 {
2804 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2805 int new_height = -1;
2806
2807 if (CONSP (it->font_height)
2808 && (EQ (XCAR (it->font_height), Qplus)
2809 || EQ (XCAR (it->font_height), Qminus))
2810 && CONSP (XCDR (it->font_height))
2811 && INTEGERP (XCAR (XCDR (it->font_height))))
2812 {
2813 /* `(+ N)' or `(- N)' where N is an integer. */
2814 int steps = XINT (XCAR (XCDR (it->font_height)));
2815 if (EQ (XCAR (it->font_height), Qplus))
2816 steps = - steps;
2817 it->face_id = smaller_face (it->f, it->face_id, steps);
2818 }
2819 else if (FUNCTIONP (it->font_height))
2820 {
2821 /* Call function with current height as argument.
2822 Value is the new height. */
2823 Lisp_Object height;
2824 height = safe_call1 (it->font_height,
2825 face->lface[LFACE_HEIGHT_INDEX]);
2826 if (NUMBERP (height))
2827 new_height = XFLOATINT (height);
2828 }
2829 else if (NUMBERP (it->font_height))
2830 {
2831 /* Value is a multiple of the canonical char height. */
2832 struct face *face;
2833
2834 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
2835 new_height = (XFLOATINT (it->font_height)
2836 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
2837 }
2838 else
2839 {
2840 /* Evaluate IT->font_height with `height' bound to the
2841 current specified height to get the new height. */
2842 Lisp_Object value;
2843 int count = BINDING_STACK_SIZE ();
2844
2845 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
2846 value = safe_eval (it->font_height);
2847 unbind_to (count, Qnil);
2848
2849 if (NUMBERP (value))
2850 new_height = XFLOATINT (value);
2851 }
2852
2853 if (new_height > 0)
2854 it->face_id = face_with_height (it->f, it->face_id, new_height);
2855 }
2856 }
2857 else if (CONSP (prop)
2858 && EQ (XCAR (prop), Qspace_width)
2859 && CONSP (XCDR (prop)))
2860 {
2861 /* `(space_width WIDTH)'. */
2862 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2863 return 0;
2864
2865 value = XCAR (XCDR (prop));
2866 if (NUMBERP (value) && XFLOATINT (value) > 0)
2867 it->space_width = value;
2868 }
2869 else if (CONSP (prop)
2870 && EQ (XCAR (prop), Qraise)
2871 && CONSP (XCDR (prop)))
2872 {
2873 /* `(raise FACTOR)'. */
2874 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2875 return 0;
2876
2877 #ifdef HAVE_WINDOW_SYSTEM
2878 value = XCAR (XCDR (prop));
2879 if (NUMBERP (value))
2880 {
2881 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2882 it->voffset = - (XFLOATINT (value)
2883 * (FONT_HEIGHT (face->font)));
2884 }
2885 #endif /* HAVE_WINDOW_SYSTEM */
2886 }
2887 else if (!it->string_from_display_prop_p)
2888 {
2889 /* `((margin left-margin) VALUE)' or `((margin right-margin)
2890 VALUE) or `((margin nil) VALUE)' or VALUE. */
2891 Lisp_Object location, value;
2892 struct text_pos start_pos;
2893 int valid_p;
2894
2895 /* Characters having this form of property are not displayed, so
2896 we have to find the end of the property. */
2897 start_pos = *position;
2898 *position = display_prop_end (it, object, start_pos);
2899 value = Qnil;
2900
2901 /* Let's stop at the new position and assume that all
2902 text properties change there. */
2903 it->stop_charpos = position->charpos;
2904
2905 location = Qunbound;
2906 if (CONSP (prop) && CONSP (XCAR (prop)))
2907 {
2908 Lisp_Object tem;
2909
2910 value = XCDR (prop);
2911 if (CONSP (value))
2912 value = XCAR (value);
2913
2914 tem = XCAR (prop);
2915 if (EQ (XCAR (tem), Qmargin)
2916 && (tem = XCDR (tem),
2917 tem = CONSP (tem) ? XCAR (tem) : Qnil,
2918 (NILP (tem)
2919 || EQ (tem, Qleft_margin)
2920 || EQ (tem, Qright_margin))))
2921 location = tem;
2922 }
2923
2924 if (EQ (location, Qunbound))
2925 {
2926 location = Qnil;
2927 value = prop;
2928 }
2929
2930 #ifdef HAVE_WINDOW_SYSTEM
2931 if (FRAME_TERMCAP_P (it->f))
2932 valid_p = STRINGP (value);
2933 else
2934 valid_p = (STRINGP (value)
2935 || (CONSP (value) && EQ (XCAR (value), Qspace))
2936 || valid_image_p (value));
2937 #else /* not HAVE_WINDOW_SYSTEM */
2938 valid_p = STRINGP (value);
2939 #endif /* not HAVE_WINDOW_SYSTEM */
2940
2941 if ((EQ (location, Qleft_margin)
2942 || EQ (location, Qright_margin)
2943 || NILP (location))
2944 && valid_p
2945 && !display_replaced_before_p)
2946 {
2947 replaces_text_display_p = 1;
2948
2949 /* Save current settings of IT so that we can restore them
2950 when we are finished with the glyph property value. */
2951 push_it (it);
2952
2953 if (NILP (location))
2954 it->area = TEXT_AREA;
2955 else if (EQ (location, Qleft_margin))
2956 it->area = LEFT_MARGIN_AREA;
2957 else
2958 it->area = RIGHT_MARGIN_AREA;
2959
2960 if (STRINGP (value))
2961 {
2962 it->string = value;
2963 it->multibyte_p = STRING_MULTIBYTE (it->string);
2964 it->current.overlay_string_index = -1;
2965 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
2966 it->end_charpos = it->string_nchars
2967 = XSTRING (it->string)->size;
2968 it->method = next_element_from_string;
2969 it->stop_charpos = 0;
2970 it->string_from_display_prop_p = 1;
2971 /* Say that we haven't consumed the characters with
2972 `display' property yet. The call to pop_it in
2973 set_iterator_to_next will clean this up. */
2974 *position = start_pos;
2975 }
2976 else if (CONSP (value) && EQ (XCAR (value), Qspace))
2977 {
2978 it->method = next_element_from_stretch;
2979 it->object = value;
2980 it->current.pos = it->position = start_pos;
2981 }
2982 #ifdef HAVE_WINDOW_SYSTEM
2983 else
2984 {
2985 it->what = IT_IMAGE;
2986 it->image_id = lookup_image (it->f, value);
2987 it->position = start_pos;
2988 it->object = NILP (object) ? it->w->buffer : object;
2989 it->method = next_element_from_image;
2990
2991 /* Say that we haven't consumed the characters with
2992 `display' property yet. The call to pop_it in
2993 set_iterator_to_next will clean this up. */
2994 *position = start_pos;
2995 }
2996 #endif /* HAVE_WINDOW_SYSTEM */
2997 }
2998 else
2999 /* Invalid property or property not supported. Restore
3000 the position to what it was before. */
3001 *position = start_pos;
3002 }
3003
3004 return replaces_text_display_p;
3005 }
3006
3007
3008 /* Check if PROP is a display sub-property value whose text should be
3009 treated as intangible. */
3010
3011 static int
3012 single_display_prop_intangible_p (prop)
3013 Lisp_Object prop;
3014 {
3015 /* Skip over `when FORM'. */
3016 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3017 {
3018 prop = XCDR (prop);
3019 if (!CONSP (prop))
3020 return 0;
3021 prop = XCDR (prop);
3022 }
3023
3024 if (!CONSP (prop))
3025 return 0;
3026
3027 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3028 we don't need to treat text as intangible. */
3029 if (EQ (XCAR (prop), Qmargin))
3030 {
3031 prop = XCDR (prop);
3032 if (!CONSP (prop))
3033 return 0;
3034
3035 prop = XCDR (prop);
3036 if (!CONSP (prop)
3037 || EQ (XCAR (prop), Qleft_margin)
3038 || EQ (XCAR (prop), Qright_margin))
3039 return 0;
3040 }
3041
3042 return CONSP (prop) && EQ (XCAR (prop), Qimage);
3043 }
3044
3045
3046 /* Check if PROP is a display property value whose text should be
3047 treated as intangible. */
3048
3049 int
3050 display_prop_intangible_p (prop)
3051 Lisp_Object prop;
3052 {
3053 if (CONSP (prop)
3054 && CONSP (XCAR (prop))
3055 && !EQ (Qmargin, XCAR (XCAR (prop))))
3056 {
3057 /* A list of sub-properties. */
3058 while (CONSP (prop))
3059 {
3060 if (single_display_prop_intangible_p (XCAR (prop)))
3061 return 1;
3062 prop = XCDR (prop);
3063 }
3064 }
3065 else if (VECTORP (prop))
3066 {
3067 /* A vector of sub-properties. */
3068 int i;
3069 for (i = 0; i < ASIZE (prop); ++i)
3070 if (single_display_prop_intangible_p (AREF (prop, i)))
3071 return 1;
3072 }
3073 else
3074 return single_display_prop_intangible_p (prop);
3075
3076 return 0;
3077 }
3078
3079
3080 /* Return 1 if PROP is a display sub-property value containing STRING. */
3081
3082 static int
3083 single_display_prop_string_p (prop, string)
3084 Lisp_Object prop, string;
3085 {
3086 extern Lisp_Object Qwhen, Qmargin;
3087
3088 if (EQ (string, prop))
3089 return 1;
3090
3091 /* Skip over `when FORM'. */
3092 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3093 {
3094 prop = XCDR (prop);
3095 if (!CONSP (prop))
3096 return 0;
3097 prop = XCDR (prop);
3098 }
3099
3100 if (CONSP (prop))
3101 /* Skip over `margin LOCATION'. */
3102 if (EQ (XCAR (prop), Qmargin))
3103 {
3104 prop = XCDR (prop);
3105 if (!CONSP (prop))
3106 return 0;
3107
3108 prop = XCDR (prop);
3109 if (!CONSP (prop))
3110 return 0;
3111 }
3112
3113 return CONSP (prop) && EQ (XCAR (prop), string);
3114 }
3115
3116
3117 /* Return 1 if STRING appears in the `display' property PROP. */
3118
3119 static int
3120 display_prop_string_p (prop, string)
3121 Lisp_Object prop, string;
3122 {
3123 extern Lisp_Object Qwhen, Qmargin;
3124
3125 if (CONSP (prop)
3126 && CONSP (XCAR (prop))
3127 && !EQ (Qmargin, XCAR (XCAR (prop))))
3128 {
3129 /* A list of sub-properties. */
3130 while (CONSP (prop))
3131 {
3132 if (single_display_prop_string_p (XCAR (prop), string))
3133 return 1;
3134 prop = XCDR (prop);
3135 }
3136 }
3137 else if (VECTORP (prop))
3138 {
3139 /* A vector of sub-properties. */
3140 int i;
3141 for (i = 0; i < ASIZE (prop); ++i)
3142 if (single_display_prop_string_p (AREF (prop, i), string))
3143 return 1;
3144 }
3145 else
3146 return single_display_prop_string_p (prop, string);
3147
3148 return 0;
3149 }
3150
3151
3152 /* Determine from which buffer position in W's buffer STRING comes
3153 from. AROUND_CHARPOS is an approximate position where it could
3154 be from. Value is the buffer position or 0 if it couldn't be
3155 determined.
3156
3157 W's buffer must be current.
3158
3159 This function is necessary because we don't record buffer positions
3160 in glyphs generated from strings (to keep struct glyph small).
3161 This function may only use code that doesn't eval because it is
3162 called asynchronously from note_mouse_highlight. */
3163
3164 int
3165 string_buffer_position (w, string, around_charpos)
3166 struct window *w;
3167 Lisp_Object string;
3168 int around_charpos;
3169 {
3170 Lisp_Object around = make_number (around_charpos);
3171 Lisp_Object limit, prop, pos;
3172 const int MAX_DISTANCE = 1000;
3173 int found = 0;
3174
3175 pos = make_number (around_charpos);
3176 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
3177 while (!found && !EQ (pos, limit))
3178 {
3179 prop = Fget_char_property (pos, Qdisplay, Qnil);
3180 if (!NILP (prop) && display_prop_string_p (prop, string))
3181 found = 1;
3182 else
3183 pos = Fnext_single_property_change (pos, Qdisplay, Qnil, limit);
3184 }
3185
3186 if (!found)
3187 {
3188 pos = make_number (around_charpos);
3189 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
3190 while (!found && !EQ (pos, limit))
3191 {
3192 prop = Fget_char_property (pos, Qdisplay, Qnil);
3193 if (!NILP (prop) && display_prop_string_p (prop, string))
3194 found = 1;
3195 else
3196 pos = Fprevious_single_property_change (pos, Qdisplay, Qnil,
3197 limit);
3198 }
3199 }
3200
3201 return found ? XINT (pos) : 0;
3202 }
3203
3204
3205 \f
3206 /***********************************************************************
3207 `composition' property
3208 ***********************************************************************/
3209
3210 /* Set up iterator IT from `composition' property at its current
3211 position. Called from handle_stop. */
3212
3213 static enum prop_handled
3214 handle_composition_prop (it)
3215 struct it *it;
3216 {
3217 Lisp_Object prop, string;
3218 int pos, pos_byte, end;
3219 enum prop_handled handled = HANDLED_NORMALLY;
3220
3221 if (STRINGP (it->string))
3222 {
3223 pos = IT_STRING_CHARPOS (*it);
3224 pos_byte = IT_STRING_BYTEPOS (*it);
3225 string = it->string;
3226 }
3227 else
3228 {
3229 pos = IT_CHARPOS (*it);
3230 pos_byte = IT_BYTEPOS (*it);
3231 string = Qnil;
3232 }
3233
3234 /* If there's a valid composition and point is not inside of the
3235 composition (in the case that the composition is from the current
3236 buffer), draw a glyph composed from the composition components. */
3237 if (find_composition (pos, -1, &pos, &end, &prop, string)
3238 && COMPOSITION_VALID_P (pos, end, prop)
3239 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
3240 {
3241 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
3242
3243 if (id >= 0)
3244 {
3245 it->method = next_element_from_composition;
3246 it->cmp_id = id;
3247 it->cmp_len = COMPOSITION_LENGTH (prop);
3248 /* For a terminal, draw only the first character of the
3249 components. */
3250 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
3251 it->len = (STRINGP (it->string)
3252 ? string_char_to_byte (it->string, end)
3253 : CHAR_TO_BYTE (end)) - pos_byte;
3254 it->stop_charpos = end;
3255 handled = HANDLED_RETURN;
3256 }
3257 }
3258
3259 return handled;
3260 }
3261
3262
3263 \f
3264 /***********************************************************************
3265 Overlay strings
3266 ***********************************************************************/
3267
3268 /* The following structure is used to record overlay strings for
3269 later sorting in load_overlay_strings. */
3270
3271 struct overlay_entry
3272 {
3273 Lisp_Object overlay;
3274 Lisp_Object string;
3275 int priority;
3276 int after_string_p;
3277 };
3278
3279
3280 /* Set up iterator IT from overlay strings at its current position.
3281 Called from handle_stop. */
3282
3283 static enum prop_handled
3284 handle_overlay_change (it)
3285 struct it *it;
3286 {
3287 if (!STRINGP (it->string) && get_overlay_strings (it))
3288 return HANDLED_RECOMPUTE_PROPS;
3289 else
3290 return HANDLED_NORMALLY;
3291 }
3292
3293
3294 /* Set up the next overlay string for delivery by IT, if there is an
3295 overlay string to deliver. Called by set_iterator_to_next when the
3296 end of the current overlay string is reached. If there are more
3297 overlay strings to display, IT->string and
3298 IT->current.overlay_string_index are set appropriately here.
3299 Otherwise IT->string is set to nil. */
3300
3301 static void
3302 next_overlay_string (it)
3303 struct it *it;
3304 {
3305 ++it->current.overlay_string_index;
3306 if (it->current.overlay_string_index == it->n_overlay_strings)
3307 {
3308 /* No more overlay strings. Restore IT's settings to what
3309 they were before overlay strings were processed, and
3310 continue to deliver from current_buffer. */
3311 pop_it (it);
3312 xassert (it->stop_charpos >= BEGV
3313 && it->stop_charpos <= it->end_charpos);
3314 it->string = Qnil;
3315 it->current.overlay_string_index = -1;
3316 SET_TEXT_POS (it->current.string_pos, -1, -1);
3317 it->n_overlay_strings = 0;
3318 it->method = next_element_from_buffer;
3319
3320 /* If we're at the end of the buffer, record that we have
3321 processed the overlay strings there already, so that
3322 next_element_from_buffer doesn't try it again. */
3323 if (IT_CHARPOS (*it) >= it->end_charpos)
3324 it->overlay_strings_at_end_processed_p = 1;
3325 }
3326 else
3327 {
3328 /* There are more overlay strings to process. If
3329 IT->current.overlay_string_index has advanced to a position
3330 where we must load IT->overlay_strings with more strings, do
3331 it. */
3332 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
3333
3334 if (it->current.overlay_string_index && i == 0)
3335 load_overlay_strings (it);
3336
3337 /* Initialize IT to deliver display elements from the overlay
3338 string. */
3339 it->string = it->overlay_strings[i];
3340 it->multibyte_p = STRING_MULTIBYTE (it->string);
3341 SET_TEXT_POS (it->current.string_pos, 0, 0);
3342 it->method = next_element_from_string;
3343 it->stop_charpos = 0;
3344 }
3345
3346 CHECK_IT (it);
3347 }
3348
3349
3350 /* Compare two overlay_entry structures E1 and E2. Used as a
3351 comparison function for qsort in load_overlay_strings. Overlay
3352 strings for the same position are sorted so that
3353
3354 1. All after-strings come in front of before-strings, except
3355 when they come from the same overlay.
3356
3357 2. Within after-strings, strings are sorted so that overlay strings
3358 from overlays with higher priorities come first.
3359
3360 2. Within before-strings, strings are sorted so that overlay
3361 strings from overlays with higher priorities come last.
3362
3363 Value is analogous to strcmp. */
3364
3365
3366 static int
3367 compare_overlay_entries (e1, e2)
3368 void *e1, *e2;
3369 {
3370 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
3371 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
3372 int result;
3373
3374 if (entry1->after_string_p != entry2->after_string_p)
3375 {
3376 /* Let after-strings appear in front of before-strings if
3377 they come from different overlays. */
3378 if (EQ (entry1->overlay, entry2->overlay))
3379 result = entry1->after_string_p ? 1 : -1;
3380 else
3381 result = entry1->after_string_p ? -1 : 1;
3382 }
3383 else if (entry1->after_string_p)
3384 /* After-strings sorted in order of decreasing priority. */
3385 result = entry2->priority - entry1->priority;
3386 else
3387 /* Before-strings sorted in order of increasing priority. */
3388 result = entry1->priority - entry2->priority;
3389
3390 return result;
3391 }
3392
3393
3394 /* Load the vector IT->overlay_strings with overlay strings from IT's
3395 current buffer position. Set IT->n_overlays to the total number of
3396 overlay strings found.
3397
3398 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
3399 a time. On entry into load_overlay_strings,
3400 IT->current.overlay_string_index gives the number of overlay
3401 strings that have already been loaded by previous calls to this
3402 function.
3403
3404 IT->add_overlay_start contains an additional overlay start
3405 position to consider for taking overlay strings from, if non-zero.
3406 This position comes into play when the overlay has an `invisible'
3407 property, and both before and after-strings. When we've skipped to
3408 the end of the overlay, because of its `invisible' property, we
3409 nevertheless want its before-string to appear.
3410 IT->add_overlay_start will contain the overlay start position
3411 in this case.
3412
3413 Overlay strings are sorted so that after-string strings come in
3414 front of before-string strings. Within before and after-strings,
3415 strings are sorted by overlay priority. See also function
3416 compare_overlay_entries. */
3417
3418 static void
3419 load_overlay_strings (it)
3420 struct it *it;
3421 {
3422 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
3423 Lisp_Object ov, overlay, window, str, invisible;
3424 int start, end;
3425 int size = 20;
3426 int n = 0, i, j, invis_p;
3427 struct overlay_entry *entries
3428 = (struct overlay_entry *) alloca (size * sizeof *entries);
3429 int charpos = IT_CHARPOS (*it);
3430
3431 /* Append the overlay string STRING of overlay OVERLAY to vector
3432 `entries' which has size `size' and currently contains `n'
3433 elements. AFTER_P non-zero means STRING is an after-string of
3434 OVERLAY. */
3435 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
3436 do \
3437 { \
3438 Lisp_Object priority; \
3439 \
3440 if (n == size) \
3441 { \
3442 int new_size = 2 * size; \
3443 struct overlay_entry *old = entries; \
3444 entries = \
3445 (struct overlay_entry *) alloca (new_size \
3446 * sizeof *entries); \
3447 bcopy (old, entries, size * sizeof *entries); \
3448 size = new_size; \
3449 } \
3450 \
3451 entries[n].string = (STRING); \
3452 entries[n].overlay = (OVERLAY); \
3453 priority = Foverlay_get ((OVERLAY), Qpriority); \
3454 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
3455 entries[n].after_string_p = (AFTER_P); \
3456 ++n; \
3457 } \
3458 while (0)
3459
3460 /* Process overlay before the overlay center. */
3461 for (ov = current_buffer->overlays_before; CONSP (ov); ov = XCDR (ov))
3462 {
3463 overlay = XCAR (ov);
3464 xassert (OVERLAYP (overlay));
3465 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3466 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3467
3468 if (end < charpos)
3469 break;
3470
3471 /* Skip this overlay if it doesn't start or end at IT's current
3472 position. */
3473 if (end != charpos && start != charpos)
3474 continue;
3475
3476 /* Skip this overlay if it doesn't apply to IT->w. */
3477 window = Foverlay_get (overlay, Qwindow);
3478 if (WINDOWP (window) && XWINDOW (window) != it->w)
3479 continue;
3480
3481 /* If the text ``under'' the overlay is invisible, both before-
3482 and after-strings from this overlay are visible; start and
3483 end position are indistinguishable. */
3484 invisible = Foverlay_get (overlay, Qinvisible);
3485 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3486
3487 /* If overlay has a non-empty before-string, record it. */
3488 if ((start == charpos || (end == charpos && invis_p))
3489 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3490 && XSTRING (str)->size)
3491 RECORD_OVERLAY_STRING (overlay, str, 0);
3492
3493 /* If overlay has a non-empty after-string, record it. */
3494 if ((end == charpos || (start == charpos && invis_p))
3495 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3496 && XSTRING (str)->size)
3497 RECORD_OVERLAY_STRING (overlay, str, 1);
3498 }
3499
3500 /* Process overlays after the overlay center. */
3501 for (ov = current_buffer->overlays_after; CONSP (ov); ov = XCDR (ov))
3502 {
3503 overlay = XCAR (ov);
3504 xassert (OVERLAYP (overlay));
3505 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3506 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3507
3508 if (start > charpos)
3509 break;
3510
3511 /* Skip this overlay if it doesn't start or end at IT's current
3512 position. */
3513 if (end != charpos && start != charpos)
3514 continue;
3515
3516 /* Skip this overlay if it doesn't apply to IT->w. */
3517 window = Foverlay_get (overlay, Qwindow);
3518 if (WINDOWP (window) && XWINDOW (window) != it->w)
3519 continue;
3520
3521 /* If the text ``under'' the overlay is invisible, it has a zero
3522 dimension, and both before- and after-strings apply. */
3523 invisible = Foverlay_get (overlay, Qinvisible);
3524 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3525
3526 /* If overlay has a non-empty before-string, record it. */
3527 if ((start == charpos || (end == charpos && invis_p))
3528 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3529 && XSTRING (str)->size)
3530 RECORD_OVERLAY_STRING (overlay, str, 0);
3531
3532 /* If overlay has a non-empty after-string, record it. */
3533 if ((end == charpos || (start == charpos && invis_p))
3534 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3535 && XSTRING (str)->size)
3536 RECORD_OVERLAY_STRING (overlay, str, 1);
3537 }
3538
3539 #undef RECORD_OVERLAY_STRING
3540
3541 /* Sort entries. */
3542 if (n > 1)
3543 qsort (entries, n, sizeof *entries, compare_overlay_entries);
3544
3545 /* Record the total number of strings to process. */
3546 it->n_overlay_strings = n;
3547
3548 /* IT->current.overlay_string_index is the number of overlay strings
3549 that have already been consumed by IT. Copy some of the
3550 remaining overlay strings to IT->overlay_strings. */
3551 i = 0;
3552 j = it->current.overlay_string_index;
3553 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
3554 it->overlay_strings[i++] = entries[j++].string;
3555
3556 CHECK_IT (it);
3557 }
3558
3559
3560 /* Get the first chunk of overlay strings at IT's current buffer
3561 position. Value is non-zero if at least one overlay string was
3562 found. */
3563
3564 static int
3565 get_overlay_strings (it)
3566 struct it *it;
3567 {
3568 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
3569 process. This fills IT->overlay_strings with strings, and sets
3570 IT->n_overlay_strings to the total number of strings to process.
3571 IT->pos.overlay_string_index has to be set temporarily to zero
3572 because load_overlay_strings needs this; it must be set to -1
3573 when no overlay strings are found because a zero value would
3574 indicate a position in the first overlay string. */
3575 it->current.overlay_string_index = 0;
3576 load_overlay_strings (it);
3577
3578 /* If we found overlay strings, set up IT to deliver display
3579 elements from the first one. Otherwise set up IT to deliver
3580 from current_buffer. */
3581 if (it->n_overlay_strings)
3582 {
3583 /* Make sure we know settings in current_buffer, so that we can
3584 restore meaningful values when we're done with the overlay
3585 strings. */
3586 compute_stop_pos (it);
3587 xassert (it->face_id >= 0);
3588
3589 /* Save IT's settings. They are restored after all overlay
3590 strings have been processed. */
3591 xassert (it->sp == 0);
3592 push_it (it);
3593
3594 /* Set up IT to deliver display elements from the first overlay
3595 string. */
3596 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3597 it->string = it->overlay_strings[0];
3598 it->stop_charpos = 0;
3599 it->end_charpos = XSTRING (it->string)->size;
3600 it->multibyte_p = STRING_MULTIBYTE (it->string);
3601 xassert (STRINGP (it->string));
3602 it->method = next_element_from_string;
3603 }
3604 else
3605 {
3606 it->string = Qnil;
3607 it->current.overlay_string_index = -1;
3608 it->method = next_element_from_buffer;
3609 }
3610
3611 CHECK_IT (it);
3612
3613 /* Value is non-zero if we found at least one overlay string. */
3614 return STRINGP (it->string);
3615 }
3616
3617
3618 \f
3619 /***********************************************************************
3620 Saving and restoring state
3621 ***********************************************************************/
3622
3623 /* Save current settings of IT on IT->stack. Called, for example,
3624 before setting up IT for an overlay string, to be able to restore
3625 IT's settings to what they were after the overlay string has been
3626 processed. */
3627
3628 static void
3629 push_it (it)
3630 struct it *it;
3631 {
3632 struct iterator_stack_entry *p;
3633
3634 xassert (it->sp < 2);
3635 p = it->stack + it->sp;
3636
3637 p->stop_charpos = it->stop_charpos;
3638 xassert (it->face_id >= 0);
3639 p->face_id = it->face_id;
3640 p->string = it->string;
3641 p->pos = it->current;
3642 p->end_charpos = it->end_charpos;
3643 p->string_nchars = it->string_nchars;
3644 p->area = it->area;
3645 p->multibyte_p = it->multibyte_p;
3646 p->space_width = it->space_width;
3647 p->font_height = it->font_height;
3648 p->voffset = it->voffset;
3649 p->string_from_display_prop_p = it->string_from_display_prop_p;
3650 ++it->sp;
3651 }
3652
3653
3654 /* Restore IT's settings from IT->stack. Called, for example, when no
3655 more overlay strings must be processed, and we return to delivering
3656 display elements from a buffer, or when the end of a string from a
3657 `display' property is reached and we return to delivering display
3658 elements from an overlay string, or from a buffer. */
3659
3660 static void
3661 pop_it (it)
3662 struct it *it;
3663 {
3664 struct iterator_stack_entry *p;
3665
3666 xassert (it->sp > 0);
3667 --it->sp;
3668 p = it->stack + it->sp;
3669 it->stop_charpos = p->stop_charpos;
3670 it->face_id = p->face_id;
3671 it->string = p->string;
3672 it->current = p->pos;
3673 it->end_charpos = p->end_charpos;
3674 it->string_nchars = p->string_nchars;
3675 it->area = p->area;
3676 it->multibyte_p = p->multibyte_p;
3677 it->space_width = p->space_width;
3678 it->font_height = p->font_height;
3679 it->voffset = p->voffset;
3680 it->string_from_display_prop_p = p->string_from_display_prop_p;
3681 }
3682
3683
3684 \f
3685 /***********************************************************************
3686 Moving over lines
3687 ***********************************************************************/
3688
3689 /* Set IT's current position to the previous line start. */
3690
3691 static void
3692 back_to_previous_line_start (it)
3693 struct it *it;
3694 {
3695 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
3696 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3697 }
3698
3699
3700 /* Move IT to the next line start.
3701
3702 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
3703 we skipped over part of the text (as opposed to moving the iterator
3704 continuously over the text). Otherwise, don't change the value
3705 of *SKIPPED_P.
3706
3707 Newlines may come from buffer text, overlay strings, or strings
3708 displayed via the `display' property. That's the reason we can't
3709 simply use find_next_newline_no_quit.
3710
3711 Note that this function may not skip over invisible text that is so
3712 because of text properties and immediately follows a newline. If
3713 it would, function reseat_at_next_visible_line_start, when called
3714 from set_iterator_to_next, would effectively make invisible
3715 characters following a newline part of the wrong glyph row, which
3716 leads to wrong cursor motion. */
3717
3718 static int
3719 forward_to_next_line_start (it, skipped_p)
3720 struct it *it;
3721 int *skipped_p;
3722 {
3723 int old_selective, newline_found_p, n;
3724 const int MAX_NEWLINE_DISTANCE = 500;
3725
3726 /* If already on a newline, just consume it to avoid unintended
3727 skipping over invisible text below. */
3728 if (it->what == IT_CHARACTER
3729 && it->c == '\n'
3730 && CHARPOS (it->position) == IT_CHARPOS (*it))
3731 {
3732 set_iterator_to_next (it, 0);
3733 it->c = 0;
3734 return 1;
3735 }
3736
3737 /* Don't handle selective display in the following. It's (a)
3738 unnecessary because it's done by the caller, and (b) leads to an
3739 infinite recursion because next_element_from_ellipsis indirectly
3740 calls this function. */
3741 old_selective = it->selective;
3742 it->selective = 0;
3743
3744 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
3745 from buffer text. */
3746 for (n = newline_found_p = 0;
3747 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
3748 n += STRINGP (it->string) ? 0 : 1)
3749 {
3750 if (!get_next_display_element (it))
3751 break;
3752 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
3753 set_iterator_to_next (it, 0);
3754 }
3755
3756 /* If we didn't find a newline near enough, see if we can use a
3757 short-cut. */
3758 if (n == MAX_NEWLINE_DISTANCE)
3759 {
3760 int start = IT_CHARPOS (*it);
3761 int limit = find_next_newline_no_quit (start, 1);
3762 Lisp_Object pos;
3763
3764 xassert (!STRINGP (it->string));
3765
3766 /* If there isn't any `display' property in sight, and no
3767 overlays, we can just use the position of the newline in
3768 buffer text. */
3769 if (it->stop_charpos >= limit
3770 || ((pos = Fnext_single_property_change (make_number (start),
3771 Qdisplay,
3772 Qnil, make_number (limit)),
3773 NILP (pos))
3774 && next_overlay_change (start) == ZV))
3775 {
3776 IT_CHARPOS (*it) = limit;
3777 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
3778 *skipped_p = newline_found_p = 1;
3779 }
3780 else
3781 {
3782 while (get_next_display_element (it)
3783 && !newline_found_p)
3784 {
3785 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
3786 set_iterator_to_next (it, 0);
3787 }
3788 }
3789 }
3790
3791 it->selective = old_selective;
3792 return newline_found_p;
3793 }
3794
3795
3796 /* Set IT's current position to the previous visible line start. Skip
3797 invisible text that is so either due to text properties or due to
3798 selective display. Caution: this does not change IT->current_x and
3799 IT->hpos. */
3800
3801 static void
3802 back_to_previous_visible_line_start (it)
3803 struct it *it;
3804 {
3805 int visible_p = 0;
3806
3807 /* Go back one newline if not on BEGV already. */
3808 if (IT_CHARPOS (*it) > BEGV)
3809 back_to_previous_line_start (it);
3810
3811 /* Move over lines that are invisible because of selective display
3812 or text properties. */
3813 while (IT_CHARPOS (*it) > BEGV
3814 && !visible_p)
3815 {
3816 visible_p = 1;
3817
3818 /* If selective > 0, then lines indented more than that values
3819 are invisible. */
3820 if (it->selective > 0
3821 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3822 it->selective))
3823 visible_p = 0;
3824 else
3825 {
3826 Lisp_Object prop;
3827
3828 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
3829 Qinvisible, it->window);
3830 if (TEXT_PROP_MEANS_INVISIBLE (prop))
3831 visible_p = 0;
3832 }
3833
3834 /* Back one more newline if the current one is invisible. */
3835 if (!visible_p)
3836 back_to_previous_line_start (it);
3837 }
3838
3839 xassert (IT_CHARPOS (*it) >= BEGV);
3840 xassert (IT_CHARPOS (*it) == BEGV
3841 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3842 CHECK_IT (it);
3843 }
3844
3845
3846 /* Reseat iterator IT at the previous visible line start. Skip
3847 invisible text that is so either due to text properties or due to
3848 selective display. At the end, update IT's overlay information,
3849 face information etc. */
3850
3851 static void
3852 reseat_at_previous_visible_line_start (it)
3853 struct it *it;
3854 {
3855 back_to_previous_visible_line_start (it);
3856 reseat (it, it->current.pos, 1);
3857 CHECK_IT (it);
3858 }
3859
3860
3861 /* Reseat iterator IT on the next visible line start in the current
3862 buffer. ON_NEWLINE_P non-zero means position IT on the newline
3863 preceding the line start. Skip over invisible text that is so
3864 because of selective display. Compute faces, overlays etc at the
3865 new position. Note that this function does not skip over text that
3866 is invisible because of text properties. */
3867
3868 static void
3869 reseat_at_next_visible_line_start (it, on_newline_p)
3870 struct it *it;
3871 int on_newline_p;
3872 {
3873 int newline_found_p, skipped_p = 0;
3874
3875 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3876
3877 /* Skip over lines that are invisible because they are indented
3878 more than the value of IT->selective. */
3879 if (it->selective > 0)
3880 while (IT_CHARPOS (*it) < ZV
3881 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3882 it->selective))
3883 {
3884 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3885 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3886 }
3887
3888 /* Position on the newline if that's what's requested. */
3889 if (on_newline_p && newline_found_p)
3890 {
3891 if (STRINGP (it->string))
3892 {
3893 if (IT_STRING_CHARPOS (*it) > 0)
3894 {
3895 --IT_STRING_CHARPOS (*it);
3896 --IT_STRING_BYTEPOS (*it);
3897 }
3898 }
3899 else if (IT_CHARPOS (*it) > BEGV)
3900 {
3901 --IT_CHARPOS (*it);
3902 --IT_BYTEPOS (*it);
3903 reseat (it, it->current.pos, 0);
3904 }
3905 }
3906 else if (skipped_p)
3907 reseat (it, it->current.pos, 0);
3908
3909 CHECK_IT (it);
3910 }
3911
3912
3913 \f
3914 /***********************************************************************
3915 Changing an iterator's position
3916 ***********************************************************************/
3917
3918 /* Change IT's current position to POS in current_buffer. If FORCE_P
3919 is non-zero, always check for text properties at the new position.
3920 Otherwise, text properties are only looked up if POS >=
3921 IT->check_charpos of a property. */
3922
3923 static void
3924 reseat (it, pos, force_p)
3925 struct it *it;
3926 struct text_pos pos;
3927 int force_p;
3928 {
3929 int original_pos = IT_CHARPOS (*it);
3930
3931 reseat_1 (it, pos, 0);
3932
3933 /* Determine where to check text properties. Avoid doing it
3934 where possible because text property lookup is very expensive. */
3935 if (force_p
3936 || CHARPOS (pos) > it->stop_charpos
3937 || CHARPOS (pos) < original_pos)
3938 handle_stop (it);
3939
3940 CHECK_IT (it);
3941 }
3942
3943
3944 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
3945 IT->stop_pos to POS, also. */
3946
3947 static void
3948 reseat_1 (it, pos, set_stop_p)
3949 struct it *it;
3950 struct text_pos pos;
3951 int set_stop_p;
3952 {
3953 /* Don't call this function when scanning a C string. */
3954 xassert (it->s == NULL);
3955
3956 /* POS must be a reasonable value. */
3957 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
3958
3959 it->current.pos = it->position = pos;
3960 XSETBUFFER (it->object, current_buffer);
3961 it->end_charpos = ZV;
3962 it->dpvec = NULL;
3963 it->current.dpvec_index = -1;
3964 it->current.overlay_string_index = -1;
3965 IT_STRING_CHARPOS (*it) = -1;
3966 IT_STRING_BYTEPOS (*it) = -1;
3967 it->string = Qnil;
3968 it->method = next_element_from_buffer;
3969 it->sp = 0;
3970 it->face_before_selective_p = 0;
3971
3972 if (set_stop_p)
3973 it->stop_charpos = CHARPOS (pos);
3974 }
3975
3976
3977 /* Set up IT for displaying a string, starting at CHARPOS in window W.
3978 If S is non-null, it is a C string to iterate over. Otherwise,
3979 STRING gives a Lisp string to iterate over.
3980
3981 If PRECISION > 0, don't return more then PRECISION number of
3982 characters from the string.
3983
3984 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
3985 characters have been returned. FIELD_WIDTH < 0 means an infinite
3986 field width.
3987
3988 MULTIBYTE = 0 means disable processing of multibyte characters,
3989 MULTIBYTE > 0 means enable it,
3990 MULTIBYTE < 0 means use IT->multibyte_p.
3991
3992 IT must be initialized via a prior call to init_iterator before
3993 calling this function. */
3994
3995 static void
3996 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
3997 struct it *it;
3998 unsigned char *s;
3999 Lisp_Object string;
4000 int charpos;
4001 int precision, field_width, multibyte;
4002 {
4003 /* No region in strings. */
4004 it->region_beg_charpos = it->region_end_charpos = -1;
4005
4006 /* No text property checks performed by default, but see below. */
4007 it->stop_charpos = -1;
4008
4009 /* Set iterator position and end position. */
4010 bzero (&it->current, sizeof it->current);
4011 it->current.overlay_string_index = -1;
4012 it->current.dpvec_index = -1;
4013 xassert (charpos >= 0);
4014
4015 /* If STRING is specified, use its multibyteness, otherwise use the
4016 setting of MULTIBYTE, if specified. */
4017 if (multibyte >= 0)
4018 it->multibyte_p = multibyte > 0;
4019
4020 if (s == NULL)
4021 {
4022 xassert (STRINGP (string));
4023 it->string = string;
4024 it->s = NULL;
4025 it->end_charpos = it->string_nchars = XSTRING (string)->size;
4026 it->method = next_element_from_string;
4027 it->current.string_pos = string_pos (charpos, string);
4028 }
4029 else
4030 {
4031 it->s = s;
4032 it->string = Qnil;
4033
4034 /* Note that we use IT->current.pos, not it->current.string_pos,
4035 for displaying C strings. */
4036 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4037 if (it->multibyte_p)
4038 {
4039 it->current.pos = c_string_pos (charpos, s, 1);
4040 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4041 }
4042 else
4043 {
4044 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4045 it->end_charpos = it->string_nchars = strlen (s);
4046 }
4047
4048 it->method = next_element_from_c_string;
4049 }
4050
4051 /* PRECISION > 0 means don't return more than PRECISION characters
4052 from the string. */
4053 if (precision > 0 && it->end_charpos - charpos > precision)
4054 it->end_charpos = it->string_nchars = charpos + precision;
4055
4056 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4057 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4058 FIELD_WIDTH < 0 means infinite field width. This is useful for
4059 padding with `-' at the end of a mode line. */
4060 if (field_width < 0)
4061 field_width = INFINITY;
4062 if (field_width > it->end_charpos - charpos)
4063 it->end_charpos = charpos + field_width;
4064
4065 /* Use the standard display table for displaying strings. */
4066 if (DISP_TABLE_P (Vstandard_display_table))
4067 it->dp = XCHAR_TABLE (Vstandard_display_table);
4068
4069 it->stop_charpos = charpos;
4070 CHECK_IT (it);
4071 }
4072
4073
4074 \f
4075 /***********************************************************************
4076 Iteration
4077 ***********************************************************************/
4078
4079 /* Load IT's display element fields with information about the next
4080 display element from the current position of IT. Value is zero if
4081 end of buffer (or C string) is reached. */
4082
4083 int
4084 get_next_display_element (it)
4085 struct it *it;
4086 {
4087 /* Non-zero means that we found an display element. Zero means that
4088 we hit the end of what we iterate over. Performance note: the
4089 function pointer `method' used here turns out to be faster than
4090 using a sequence of if-statements. */
4091 int success_p = (*it->method) (it);
4092
4093 if (it->what == IT_CHARACTER)
4094 {
4095 /* Map via display table or translate control characters.
4096 IT->c, IT->len etc. have been set to the next character by
4097 the function call above. If we have a display table, and it
4098 contains an entry for IT->c, translate it. Don't do this if
4099 IT->c itself comes from a display table, otherwise we could
4100 end up in an infinite recursion. (An alternative could be to
4101 count the recursion depth of this function and signal an
4102 error when a certain maximum depth is reached.) Is it worth
4103 it? */
4104 if (success_p && it->dpvec == NULL)
4105 {
4106 Lisp_Object dv;
4107
4108 if (it->dp
4109 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
4110 VECTORP (dv)))
4111 {
4112 struct Lisp_Vector *v = XVECTOR (dv);
4113
4114 /* Return the first character from the display table
4115 entry, if not empty. If empty, don't display the
4116 current character. */
4117 if (v->size)
4118 {
4119 it->dpvec_char_len = it->len;
4120 it->dpvec = v->contents;
4121 it->dpend = v->contents + v->size;
4122 it->current.dpvec_index = 0;
4123 it->method = next_element_from_display_vector;
4124 success_p = get_next_display_element (it);
4125 }
4126 else
4127 {
4128 set_iterator_to_next (it, 0);
4129 success_p = get_next_display_element (it);
4130 }
4131 }
4132
4133 /* Translate control characters into `\003' or `^C' form.
4134 Control characters coming from a display table entry are
4135 currently not translated because we use IT->dpvec to hold
4136 the translation. This could easily be changed but I
4137 don't believe that it is worth doing.
4138
4139 Non-printable multibyte characters are also translated
4140 octal form. */
4141 else if ((it->c < ' '
4142 && (it->area != TEXT_AREA
4143 || (it->c != '\n' && it->c != '\t')))
4144 || (it->c >= 127
4145 && it->len == 1)
4146 || !CHAR_PRINTABLE_P (it->c))
4147 {
4148 /* IT->c is a control character which must be displayed
4149 either as '\003' or as `^C' where the '\\' and '^'
4150 can be defined in the display table. Fill
4151 IT->ctl_chars with glyphs for what we have to
4152 display. Then, set IT->dpvec to these glyphs. */
4153 GLYPH g;
4154
4155 if (it->c < 128 && it->ctl_arrow_p)
4156 {
4157 /* Set IT->ctl_chars[0] to the glyph for `^'. */
4158 if (it->dp
4159 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
4160 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
4161 g = XINT (DISP_CTRL_GLYPH (it->dp));
4162 else
4163 g = FAST_MAKE_GLYPH ('^', 0);
4164 XSETINT (it->ctl_chars[0], g);
4165
4166 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
4167 XSETINT (it->ctl_chars[1], g);
4168
4169 /* Set up IT->dpvec and return first character from it. */
4170 it->dpvec_char_len = it->len;
4171 it->dpvec = it->ctl_chars;
4172 it->dpend = it->dpvec + 2;
4173 it->current.dpvec_index = 0;
4174 it->method = next_element_from_display_vector;
4175 get_next_display_element (it);
4176 }
4177 else
4178 {
4179 unsigned char str[MAX_MULTIBYTE_LENGTH];
4180 int len;
4181 int i;
4182 GLYPH escape_glyph;
4183
4184 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
4185 if (it->dp
4186 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
4187 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
4188 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
4189 else
4190 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
4191
4192 if (SINGLE_BYTE_CHAR_P (it->c))
4193 str[0] = it->c, len = 1;
4194 else
4195 len = CHAR_STRING (it->c, str);
4196
4197 for (i = 0; i < len; i++)
4198 {
4199 XSETINT (it->ctl_chars[i * 4], escape_glyph);
4200 /* Insert three more glyphs into IT->ctl_chars for
4201 the octal display of the character. */
4202 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
4203 XSETINT (it->ctl_chars[i * 4 + 1], g);
4204 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
4205 XSETINT (it->ctl_chars[i * 4 + 2], g);
4206 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
4207 XSETINT (it->ctl_chars[i * 4 + 3], g);
4208 }
4209
4210 /* Set up IT->dpvec and return the first character
4211 from it. */
4212 it->dpvec_char_len = it->len;
4213 it->dpvec = it->ctl_chars;
4214 it->dpend = it->dpvec + len * 4;
4215 it->current.dpvec_index = 0;
4216 it->method = next_element_from_display_vector;
4217 get_next_display_element (it);
4218 }
4219 }
4220 }
4221
4222 /* Adjust face id for a multibyte character. There are no
4223 multibyte character in unibyte text. */
4224 if (it->multibyte_p
4225 && success_p
4226 && FRAME_WINDOW_P (it->f))
4227 {
4228 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4229 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
4230 }
4231 }
4232
4233 /* Is this character the last one of a run of characters with
4234 box? If yes, set IT->end_of_box_run_p to 1. */
4235 if (it->face_box_p
4236 && it->s == NULL)
4237 {
4238 int face_id;
4239 struct face *face;
4240
4241 it->end_of_box_run_p
4242 = ((face_id = face_after_it_pos (it),
4243 face_id != it->face_id)
4244 && (face = FACE_FROM_ID (it->f, face_id),
4245 face->box == FACE_NO_BOX));
4246 }
4247
4248 /* Value is 0 if end of buffer or string reached. */
4249 return success_p;
4250 }
4251
4252
4253 /* Move IT to the next display element.
4254
4255 RESEAT_P non-zero means if called on a newline in buffer text,
4256 skip to the next visible line start.
4257
4258 Functions get_next_display_element and set_iterator_to_next are
4259 separate because I find this arrangement easier to handle than a
4260 get_next_display_element function that also increments IT's
4261 position. The way it is we can first look at an iterator's current
4262 display element, decide whether it fits on a line, and if it does,
4263 increment the iterator position. The other way around we probably
4264 would either need a flag indicating whether the iterator has to be
4265 incremented the next time, or we would have to implement a
4266 decrement position function which would not be easy to write. */
4267
4268 void
4269 set_iterator_to_next (it, reseat_p)
4270 struct it *it;
4271 int reseat_p;
4272 {
4273 /* Reset flags indicating start and end of a sequence of characters
4274 with box. Reset them at the start of this function because
4275 moving the iterator to a new position might set them. */
4276 it->start_of_box_run_p = it->end_of_box_run_p = 0;
4277
4278 if (it->method == next_element_from_buffer)
4279 {
4280 /* The current display element of IT is a character from
4281 current_buffer. Advance in the buffer, and maybe skip over
4282 invisible lines that are so because of selective display. */
4283 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
4284 reseat_at_next_visible_line_start (it, 0);
4285 else
4286 {
4287 xassert (it->len != 0);
4288 IT_BYTEPOS (*it) += it->len;
4289 IT_CHARPOS (*it) += 1;
4290 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
4291 }
4292 }
4293 else if (it->method == next_element_from_composition)
4294 {
4295 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
4296 if (STRINGP (it->string))
4297 {
4298 IT_STRING_BYTEPOS (*it) += it->len;
4299 IT_STRING_CHARPOS (*it) += it->cmp_len;
4300 it->method = next_element_from_string;
4301 goto consider_string_end;
4302 }
4303 else
4304 {
4305 IT_BYTEPOS (*it) += it->len;
4306 IT_CHARPOS (*it) += it->cmp_len;
4307 it->method = next_element_from_buffer;
4308 }
4309 }
4310 else if (it->method == next_element_from_c_string)
4311 {
4312 /* Current display element of IT is from a C string. */
4313 IT_BYTEPOS (*it) += it->len;
4314 IT_CHARPOS (*it) += 1;
4315 }
4316 else if (it->method == next_element_from_display_vector)
4317 {
4318 /* Current display element of IT is from a display table entry.
4319 Advance in the display table definition. Reset it to null if
4320 end reached, and continue with characters from buffers/
4321 strings. */
4322 ++it->current.dpvec_index;
4323
4324 /* Restore face of the iterator to what they were before the
4325 display vector entry (these entries may contain faces). */
4326 it->face_id = it->saved_face_id;
4327
4328 if (it->dpvec + it->current.dpvec_index == it->dpend)
4329 {
4330 if (it->s)
4331 it->method = next_element_from_c_string;
4332 else if (STRINGP (it->string))
4333 it->method = next_element_from_string;
4334 else
4335 it->method = next_element_from_buffer;
4336
4337 it->dpvec = NULL;
4338 it->current.dpvec_index = -1;
4339
4340 /* Skip over characters which were displayed via IT->dpvec. */
4341 if (it->dpvec_char_len < 0)
4342 reseat_at_next_visible_line_start (it, 1);
4343 else if (it->dpvec_char_len > 0)
4344 {
4345 it->len = it->dpvec_char_len;
4346 set_iterator_to_next (it, reseat_p);
4347 }
4348 }
4349 }
4350 else if (it->method == next_element_from_string)
4351 {
4352 /* Current display element is a character from a Lisp string. */
4353 xassert (it->s == NULL && STRINGP (it->string));
4354 IT_STRING_BYTEPOS (*it) += it->len;
4355 IT_STRING_CHARPOS (*it) += 1;
4356
4357 consider_string_end:
4358
4359 if (it->current.overlay_string_index >= 0)
4360 {
4361 /* IT->string is an overlay string. Advance to the
4362 next, if there is one. */
4363 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
4364 next_overlay_string (it);
4365 }
4366 else
4367 {
4368 /* IT->string is not an overlay string. If we reached
4369 its end, and there is something on IT->stack, proceed
4370 with what is on the stack. This can be either another
4371 string, this time an overlay string, or a buffer. */
4372 if (IT_STRING_CHARPOS (*it) == XSTRING (it->string)->size
4373 && it->sp > 0)
4374 {
4375 pop_it (it);
4376 if (!STRINGP (it->string))
4377 it->method = next_element_from_buffer;
4378 else
4379 goto consider_string_end;
4380 }
4381 }
4382 }
4383 else if (it->method == next_element_from_image
4384 || it->method == next_element_from_stretch)
4385 {
4386 /* The position etc with which we have to proceed are on
4387 the stack. The position may be at the end of a string,
4388 if the `display' property takes up the whole string. */
4389 pop_it (it);
4390 it->image_id = 0;
4391 if (STRINGP (it->string))
4392 {
4393 it->method = next_element_from_string;
4394 goto consider_string_end;
4395 }
4396 else
4397 it->method = next_element_from_buffer;
4398 }
4399 else
4400 /* There are no other methods defined, so this should be a bug. */
4401 abort ();
4402
4403 xassert (it->method != next_element_from_string
4404 || (STRINGP (it->string)
4405 && IT_STRING_CHARPOS (*it) >= 0));
4406 }
4407
4408
4409 /* Load IT's display element fields with information about the next
4410 display element which comes from a display table entry or from the
4411 result of translating a control character to one of the forms `^C'
4412 or `\003'. IT->dpvec holds the glyphs to return as characters. */
4413
4414 static int
4415 next_element_from_display_vector (it)
4416 struct it *it;
4417 {
4418 /* Precondition. */
4419 xassert (it->dpvec && it->current.dpvec_index >= 0);
4420
4421 /* Remember the current face id in case glyphs specify faces.
4422 IT's face is restored in set_iterator_to_next. */
4423 it->saved_face_id = it->face_id;
4424
4425 if (INTEGERP (*it->dpvec)
4426 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
4427 {
4428 int lface_id;
4429 GLYPH g;
4430
4431 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
4432 it->c = FAST_GLYPH_CHAR (g);
4433 it->len = CHAR_BYTES (it->c);
4434
4435 /* The entry may contain a face id to use. Such a face id is
4436 the id of a Lisp face, not a realized face. A face id of
4437 zero means no face is specified. */
4438 lface_id = FAST_GLYPH_FACE (g);
4439 if (lface_id)
4440 {
4441 /* The function returns -1 if lface_id is invalid. */
4442 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
4443 if (face_id >= 0)
4444 it->face_id = face_id;
4445 }
4446 }
4447 else
4448 /* Display table entry is invalid. Return a space. */
4449 it->c = ' ', it->len = 1;
4450
4451 /* Don't change position and object of the iterator here. They are
4452 still the values of the character that had this display table
4453 entry or was translated, and that's what we want. */
4454 it->what = IT_CHARACTER;
4455 return 1;
4456 }
4457
4458
4459 /* Load IT with the next display element from Lisp string IT->string.
4460 IT->current.string_pos is the current position within the string.
4461 If IT->current.overlay_string_index >= 0, the Lisp string is an
4462 overlay string. */
4463
4464 static int
4465 next_element_from_string (it)
4466 struct it *it;
4467 {
4468 struct text_pos position;
4469
4470 xassert (STRINGP (it->string));
4471 xassert (IT_STRING_CHARPOS (*it) >= 0);
4472 position = it->current.string_pos;
4473
4474 /* Time to check for invisible text? */
4475 if (IT_STRING_CHARPOS (*it) < it->end_charpos
4476 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
4477 {
4478 handle_stop (it);
4479
4480 /* Since a handler may have changed IT->method, we must
4481 recurse here. */
4482 return get_next_display_element (it);
4483 }
4484
4485 if (it->current.overlay_string_index >= 0)
4486 {
4487 /* Get the next character from an overlay string. In overlay
4488 strings, There is no field width or padding with spaces to
4489 do. */
4490 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
4491 {
4492 it->what = IT_EOB;
4493 return 0;
4494 }
4495 else if (STRING_MULTIBYTE (it->string))
4496 {
4497 int remaining = (STRING_BYTES (XSTRING (it->string))
4498 - IT_STRING_BYTEPOS (*it));
4499 unsigned char *s = (XSTRING (it->string)->data
4500 + IT_STRING_BYTEPOS (*it));
4501 it->c = string_char_and_length (s, remaining, &it->len);
4502 }
4503 else
4504 {
4505 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
4506 it->len = 1;
4507 }
4508 }
4509 else
4510 {
4511 /* Get the next character from a Lisp string that is not an
4512 overlay string. Such strings come from the mode line, for
4513 example. We may have to pad with spaces, or truncate the
4514 string. See also next_element_from_c_string. */
4515 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
4516 {
4517 it->what = IT_EOB;
4518 return 0;
4519 }
4520 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
4521 {
4522 /* Pad with spaces. */
4523 it->c = ' ', it->len = 1;
4524 CHARPOS (position) = BYTEPOS (position) = -1;
4525 }
4526 else if (STRING_MULTIBYTE (it->string))
4527 {
4528 int maxlen = (STRING_BYTES (XSTRING (it->string))
4529 - IT_STRING_BYTEPOS (*it));
4530 unsigned char *s = (XSTRING (it->string)->data
4531 + IT_STRING_BYTEPOS (*it));
4532 it->c = string_char_and_length (s, maxlen, &it->len);
4533 }
4534 else
4535 {
4536 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
4537 it->len = 1;
4538 }
4539 }
4540
4541 /* Record what we have and where it came from. Note that we store a
4542 buffer position in IT->position although it could arguably be a
4543 string position. */
4544 it->what = IT_CHARACTER;
4545 it->object = it->string;
4546 it->position = position;
4547 return 1;
4548 }
4549
4550
4551 /* Load IT with next display element from C string IT->s.
4552 IT->string_nchars is the maximum number of characters to return
4553 from the string. IT->end_charpos may be greater than
4554 IT->string_nchars when this function is called, in which case we
4555 may have to return padding spaces. Value is zero if end of string
4556 reached, including padding spaces. */
4557
4558 static int
4559 next_element_from_c_string (it)
4560 struct it *it;
4561 {
4562 int success_p = 1;
4563
4564 xassert (it->s);
4565 it->what = IT_CHARACTER;
4566 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
4567 it->object = Qnil;
4568
4569 /* IT's position can be greater IT->string_nchars in case a field
4570 width or precision has been specified when the iterator was
4571 initialized. */
4572 if (IT_CHARPOS (*it) >= it->end_charpos)
4573 {
4574 /* End of the game. */
4575 it->what = IT_EOB;
4576 success_p = 0;
4577 }
4578 else if (IT_CHARPOS (*it) >= it->string_nchars)
4579 {
4580 /* Pad with spaces. */
4581 it->c = ' ', it->len = 1;
4582 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
4583 }
4584 else if (it->multibyte_p)
4585 {
4586 /* Implementation note: The calls to strlen apparently aren't a
4587 performance problem because there is no noticeable performance
4588 difference between Emacs running in unibyte or multibyte mode. */
4589 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
4590 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
4591 maxlen, &it->len);
4592 }
4593 else
4594 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
4595
4596 return success_p;
4597 }
4598
4599
4600 /* Set up IT to return characters from an ellipsis, if appropriate.
4601 The definition of the ellipsis glyphs may come from a display table
4602 entry. This function Fills IT with the first glyph from the
4603 ellipsis if an ellipsis is to be displayed. */
4604
4605 static int
4606 next_element_from_ellipsis (it)
4607 struct it *it;
4608 {
4609 if (it->selective_display_ellipsis_p)
4610 {
4611 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4612 {
4613 /* Use the display table definition for `...'. Invalid glyphs
4614 will be handled by the method returning elements from dpvec. */
4615 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4616 it->dpvec_char_len = it->len;
4617 it->dpvec = v->contents;
4618 it->dpend = v->contents + v->size;
4619 it->current.dpvec_index = 0;
4620 it->method = next_element_from_display_vector;
4621 }
4622 else
4623 {
4624 /* Use default `...' which is stored in default_invis_vector. */
4625 it->dpvec_char_len = it->len;
4626 it->dpvec = default_invis_vector;
4627 it->dpend = default_invis_vector + 3;
4628 it->current.dpvec_index = 0;
4629 it->method = next_element_from_display_vector;
4630 }
4631 }
4632 else
4633 {
4634 /* The face at the current position may be different from the
4635 face we find after the invisible text. Remember what it
4636 was in IT->saved_face_id, and signal that it's there by
4637 setting face_before_selective_p. */
4638 it->saved_face_id = it->face_id;
4639 it->method = next_element_from_buffer;
4640 reseat_at_next_visible_line_start (it, 1);
4641 it->face_before_selective_p = 1;
4642 }
4643
4644 return get_next_display_element (it);
4645 }
4646
4647
4648 /* Deliver an image display element. The iterator IT is already
4649 filled with image information (done in handle_display_prop). Value
4650 is always 1. */
4651
4652
4653 static int
4654 next_element_from_image (it)
4655 struct it *it;
4656 {
4657 it->what = IT_IMAGE;
4658 return 1;
4659 }
4660
4661
4662 /* Fill iterator IT with next display element from a stretch glyph
4663 property. IT->object is the value of the text property. Value is
4664 always 1. */
4665
4666 static int
4667 next_element_from_stretch (it)
4668 struct it *it;
4669 {
4670 it->what = IT_STRETCH;
4671 return 1;
4672 }
4673
4674
4675 /* Load IT with the next display element from current_buffer. Value
4676 is zero if end of buffer reached. IT->stop_charpos is the next
4677 position at which to stop and check for text properties or buffer
4678 end. */
4679
4680 static int
4681 next_element_from_buffer (it)
4682 struct it *it;
4683 {
4684 int success_p = 1;
4685
4686 /* Check this assumption, otherwise, we would never enter the
4687 if-statement, below. */
4688 xassert (IT_CHARPOS (*it) >= BEGV
4689 && IT_CHARPOS (*it) <= it->stop_charpos);
4690
4691 if (IT_CHARPOS (*it) >= it->stop_charpos)
4692 {
4693 if (IT_CHARPOS (*it) >= it->end_charpos)
4694 {
4695 int overlay_strings_follow_p;
4696
4697 /* End of the game, except when overlay strings follow that
4698 haven't been returned yet. */
4699 if (it->overlay_strings_at_end_processed_p)
4700 overlay_strings_follow_p = 0;
4701 else
4702 {
4703 it->overlay_strings_at_end_processed_p = 1;
4704 overlay_strings_follow_p = get_overlay_strings (it);
4705 }
4706
4707 if (overlay_strings_follow_p)
4708 success_p = get_next_display_element (it);
4709 else
4710 {
4711 it->what = IT_EOB;
4712 it->position = it->current.pos;
4713 success_p = 0;
4714 }
4715 }
4716 else
4717 {
4718 handle_stop (it);
4719 return get_next_display_element (it);
4720 }
4721 }
4722 else
4723 {
4724 /* No face changes, overlays etc. in sight, so just return a
4725 character from current_buffer. */
4726 unsigned char *p;
4727
4728 /* Maybe run the redisplay end trigger hook. Performance note:
4729 This doesn't seem to cost measurable time. */
4730 if (it->redisplay_end_trigger_charpos
4731 && it->glyph_row
4732 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
4733 run_redisplay_end_trigger_hook (it);
4734
4735 /* Get the next character, maybe multibyte. */
4736 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
4737 if (it->multibyte_p && !ASCII_BYTE_P (*p))
4738 {
4739 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
4740 - IT_BYTEPOS (*it));
4741 it->c = string_char_and_length (p, maxlen, &it->len);
4742 }
4743 else
4744 it->c = *p, it->len = 1;
4745
4746 /* Record what we have and where it came from. */
4747 it->what = IT_CHARACTER;;
4748 it->object = it->w->buffer;
4749 it->position = it->current.pos;
4750
4751 /* Normally we return the character found above, except when we
4752 really want to return an ellipsis for selective display. */
4753 if (it->selective)
4754 {
4755 if (it->c == '\n')
4756 {
4757 /* A value of selective > 0 means hide lines indented more
4758 than that number of columns. */
4759 if (it->selective > 0
4760 && IT_CHARPOS (*it) + 1 < ZV
4761 && indented_beyond_p (IT_CHARPOS (*it) + 1,
4762 IT_BYTEPOS (*it) + 1,
4763 it->selective))
4764 {
4765 success_p = next_element_from_ellipsis (it);
4766 it->dpvec_char_len = -1;
4767 }
4768 }
4769 else if (it->c == '\r' && it->selective == -1)
4770 {
4771 /* A value of selective == -1 means that everything from the
4772 CR to the end of the line is invisible, with maybe an
4773 ellipsis displayed for it. */
4774 success_p = next_element_from_ellipsis (it);
4775 it->dpvec_char_len = -1;
4776 }
4777 }
4778 }
4779
4780 /* Value is zero if end of buffer reached. */
4781 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
4782 return success_p;
4783 }
4784
4785
4786 /* Run the redisplay end trigger hook for IT. */
4787
4788 static void
4789 run_redisplay_end_trigger_hook (it)
4790 struct it *it;
4791 {
4792 Lisp_Object args[3];
4793
4794 /* IT->glyph_row should be non-null, i.e. we should be actually
4795 displaying something, or otherwise we should not run the hook. */
4796 xassert (it->glyph_row);
4797
4798 /* Set up hook arguments. */
4799 args[0] = Qredisplay_end_trigger_functions;
4800 args[1] = it->window;
4801 XSETINT (args[2], it->redisplay_end_trigger_charpos);
4802 it->redisplay_end_trigger_charpos = 0;
4803
4804 /* Since we are *trying* to run these functions, don't try to run
4805 them again, even if they get an error. */
4806 it->w->redisplay_end_trigger = Qnil;
4807 Frun_hook_with_args (3, args);
4808
4809 /* Notice if it changed the face of the character we are on. */
4810 handle_face_prop (it);
4811 }
4812
4813
4814 /* Deliver a composition display element. The iterator IT is already
4815 filled with composition information (done in
4816 handle_composition_prop). Value is always 1. */
4817
4818 static int
4819 next_element_from_composition (it)
4820 struct it *it;
4821 {
4822 it->what = IT_COMPOSITION;
4823 it->position = (STRINGP (it->string)
4824 ? it->current.string_pos
4825 : it->current.pos);
4826 return 1;
4827 }
4828
4829
4830 \f
4831 /***********************************************************************
4832 Moving an iterator without producing glyphs
4833 ***********************************************************************/
4834
4835 /* Move iterator IT to a specified buffer or X position within one
4836 line on the display without producing glyphs.
4837
4838 Begin to skip at IT's current position. Skip to TO_CHARPOS or TO_X
4839 whichever is reached first.
4840
4841 TO_CHARPOS <= 0 means no TO_CHARPOS is specified.
4842
4843 TO_X < 0 means that no TO_X is specified. TO_X is normally a value
4844 0 <= TO_X <= IT->last_visible_x. This means in particular, that
4845 TO_X includes the amount by which a window is horizontally
4846 scrolled.
4847
4848 Value is
4849
4850 MOVE_POS_MATCH_OR_ZV
4851 - when TO_POS or ZV was reached.
4852
4853 MOVE_X_REACHED
4854 -when TO_X was reached before TO_POS or ZV were reached.
4855
4856 MOVE_LINE_CONTINUED
4857 - when we reached the end of the display area and the line must
4858 be continued.
4859
4860 MOVE_LINE_TRUNCATED
4861 - when we reached the end of the display area and the line is
4862 truncated.
4863
4864 MOVE_NEWLINE_OR_CR
4865 - when we stopped at a line end, i.e. a newline or a CR and selective
4866 display is on. */
4867
4868 static enum move_it_result
4869 move_it_in_display_line_to (it, to_charpos, to_x, op)
4870 struct it *it;
4871 int to_charpos, to_x, op;
4872 {
4873 enum move_it_result result = MOVE_UNDEFINED;
4874 struct glyph_row *saved_glyph_row;
4875
4876 /* Don't produce glyphs in produce_glyphs. */
4877 saved_glyph_row = it->glyph_row;
4878 it->glyph_row = NULL;
4879
4880 while (1)
4881 {
4882 int x, i, ascent = 0, descent = 0;
4883
4884 /* Stop when ZV or TO_CHARPOS reached. */
4885 if (!get_next_display_element (it)
4886 || ((op & MOVE_TO_POS) != 0
4887 && BUFFERP (it->object)
4888 && IT_CHARPOS (*it) >= to_charpos))
4889 {
4890 result = MOVE_POS_MATCH_OR_ZV;
4891 break;
4892 }
4893
4894 /* The call to produce_glyphs will get the metrics of the
4895 display element IT is loaded with. We record in x the
4896 x-position before this display element in case it does not
4897 fit on the line. */
4898 x = it->current_x;
4899
4900 /* Remember the line height so far in case the next element doesn't
4901 fit on the line. */
4902 if (!it->truncate_lines_p)
4903 {
4904 ascent = it->max_ascent;
4905 descent = it->max_descent;
4906 }
4907
4908 PRODUCE_GLYPHS (it);
4909
4910 if (it->area != TEXT_AREA)
4911 {
4912 set_iterator_to_next (it, 1);
4913 continue;
4914 }
4915
4916 /* The number of glyphs we get back in IT->nglyphs will normally
4917 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
4918 character on a terminal frame, or (iii) a line end. For the
4919 second case, IT->nglyphs - 1 padding glyphs will be present
4920 (on X frames, there is only one glyph produced for a
4921 composite character.
4922
4923 The behavior implemented below means, for continuation lines,
4924 that as many spaces of a TAB as fit on the current line are
4925 displayed there. For terminal frames, as many glyphs of a
4926 multi-glyph character are displayed in the current line, too.
4927 This is what the old redisplay code did, and we keep it that
4928 way. Under X, the whole shape of a complex character must
4929 fit on the line or it will be completely displayed in the
4930 next line.
4931
4932 Note that both for tabs and padding glyphs, all glyphs have
4933 the same width. */
4934 if (it->nglyphs)
4935 {
4936 /* More than one glyph or glyph doesn't fit on line. All
4937 glyphs have the same width. */
4938 int single_glyph_width = it->pixel_width / it->nglyphs;
4939 int new_x;
4940
4941 for (i = 0; i < it->nglyphs; ++i, x = new_x)
4942 {
4943 new_x = x + single_glyph_width;
4944
4945 /* We want to leave anything reaching TO_X to the caller. */
4946 if ((op & MOVE_TO_X) && new_x > to_x)
4947 {
4948 it->current_x = x;
4949 result = MOVE_X_REACHED;
4950 break;
4951 }
4952 else if (/* Lines are continued. */
4953 !it->truncate_lines_p
4954 && (/* And glyph doesn't fit on the line. */
4955 new_x > it->last_visible_x
4956 /* Or it fits exactly and we're on a window
4957 system frame. */
4958 || (new_x == it->last_visible_x
4959 && FRAME_WINDOW_P (it->f))))
4960 {
4961 if (/* IT->hpos == 0 means the very first glyph
4962 doesn't fit on the line, e.g. a wide image. */
4963 it->hpos == 0
4964 || (new_x == it->last_visible_x
4965 && FRAME_WINDOW_P (it->f)))
4966 {
4967 ++it->hpos;
4968 it->current_x = new_x;
4969 if (i == it->nglyphs - 1)
4970 set_iterator_to_next (it, 1);
4971 }
4972 else
4973 {
4974 it->current_x = x;
4975 it->max_ascent = ascent;
4976 it->max_descent = descent;
4977 }
4978
4979 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
4980 IT_CHARPOS (*it)));
4981 result = MOVE_LINE_CONTINUED;
4982 break;
4983 }
4984 else if (new_x > it->first_visible_x)
4985 {
4986 /* Glyph is visible. Increment number of glyphs that
4987 would be displayed. */
4988 ++it->hpos;
4989 }
4990 else
4991 {
4992 /* Glyph is completely off the left margin of the display
4993 area. Nothing to do. */
4994 }
4995 }
4996
4997 if (result != MOVE_UNDEFINED)
4998 break;
4999 }
5000 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
5001 {
5002 /* Stop when TO_X specified and reached. This check is
5003 necessary here because of lines consisting of a line end,
5004 only. The line end will not produce any glyphs and we
5005 would never get MOVE_X_REACHED. */
5006 xassert (it->nglyphs == 0);
5007 result = MOVE_X_REACHED;
5008 break;
5009 }
5010
5011 /* Is this a line end? If yes, we're done. */
5012 if (ITERATOR_AT_END_OF_LINE_P (it))
5013 {
5014 result = MOVE_NEWLINE_OR_CR;
5015 break;
5016 }
5017
5018 /* The current display element has been consumed. Advance
5019 to the next. */
5020 set_iterator_to_next (it, 1);
5021
5022 /* Stop if lines are truncated and IT's current x-position is
5023 past the right edge of the window now. */
5024 if (it->truncate_lines_p
5025 && it->current_x >= it->last_visible_x)
5026 {
5027 result = MOVE_LINE_TRUNCATED;
5028 break;
5029 }
5030 }
5031
5032 /* Restore the iterator settings altered at the beginning of this
5033 function. */
5034 it->glyph_row = saved_glyph_row;
5035 return result;
5036 }
5037
5038
5039 /* Move IT forward to a specified buffer position TO_CHARPOS, TO_X,
5040 TO_Y, TO_VPOS. OP is a bit-mask that specifies where to stop. See
5041 the description of enum move_operation_enum.
5042
5043 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
5044 screen line, this function will set IT to the next position >
5045 TO_CHARPOS. */
5046
5047 void
5048 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
5049 struct it *it;
5050 int to_charpos, to_x, to_y, to_vpos;
5051 int op;
5052 {
5053 enum move_it_result skip, skip2 = MOVE_X_REACHED;
5054 int line_height;
5055 int reached = 0;
5056
5057 for (;;)
5058 {
5059 if (op & MOVE_TO_VPOS)
5060 {
5061 /* If no TO_CHARPOS and no TO_X specified, stop at the
5062 start of the line TO_VPOS. */
5063 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
5064 {
5065 if (it->vpos == to_vpos)
5066 {
5067 reached = 1;
5068 break;
5069 }
5070 else
5071 skip = move_it_in_display_line_to (it, -1, -1, 0);
5072 }
5073 else
5074 {
5075 /* TO_VPOS >= 0 means stop at TO_X in the line at
5076 TO_VPOS, or at TO_POS, whichever comes first. */
5077 if (it->vpos == to_vpos)
5078 {
5079 reached = 2;
5080 break;
5081 }
5082
5083 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
5084
5085 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
5086 {
5087 reached = 3;
5088 break;
5089 }
5090 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
5091 {
5092 /* We have reached TO_X but not in the line we want. */
5093 skip = move_it_in_display_line_to (it, to_charpos,
5094 -1, MOVE_TO_POS);
5095 if (skip == MOVE_POS_MATCH_OR_ZV)
5096 {
5097 reached = 4;
5098 break;
5099 }
5100 }
5101 }
5102 }
5103 else if (op & MOVE_TO_Y)
5104 {
5105 struct it it_backup;
5106
5107 /* TO_Y specified means stop at TO_X in the line containing
5108 TO_Y---or at TO_CHARPOS if this is reached first. The
5109 problem is that we can't really tell whether the line
5110 contains TO_Y before we have completely scanned it, and
5111 this may skip past TO_X. What we do is to first scan to
5112 TO_X.
5113
5114 If TO_X is not specified, use a TO_X of zero. The reason
5115 is to make the outcome of this function more predictable.
5116 If we didn't use TO_X == 0, we would stop at the end of
5117 the line which is probably not what a caller would expect
5118 to happen. */
5119 skip = move_it_in_display_line_to (it, to_charpos,
5120 ((op & MOVE_TO_X)
5121 ? to_x : 0),
5122 (MOVE_TO_X
5123 | (op & MOVE_TO_POS)));
5124
5125 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
5126 if (skip == MOVE_POS_MATCH_OR_ZV)
5127 {
5128 reached = 5;
5129 break;
5130 }
5131
5132 /* If TO_X was reached, we would like to know whether TO_Y
5133 is in the line. This can only be said if we know the
5134 total line height which requires us to scan the rest of
5135 the line. */
5136 if (skip == MOVE_X_REACHED)
5137 {
5138 it_backup = *it;
5139 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
5140 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
5141 op & MOVE_TO_POS);
5142 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
5143 }
5144
5145 /* Now, decide whether TO_Y is in this line. */
5146 line_height = it->max_ascent + it->max_descent;
5147 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
5148
5149 if (to_y >= it->current_y
5150 && to_y < it->current_y + line_height)
5151 {
5152 if (skip == MOVE_X_REACHED)
5153 /* If TO_Y is in this line and TO_X was reached above,
5154 we scanned too far. We have to restore IT's settings
5155 to the ones before skipping. */
5156 *it = it_backup;
5157 reached = 6;
5158 }
5159 else if (skip == MOVE_X_REACHED)
5160 {
5161 skip = skip2;
5162 if (skip == MOVE_POS_MATCH_OR_ZV)
5163 reached = 7;
5164 }
5165
5166 if (reached)
5167 break;
5168 }
5169 else
5170 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
5171
5172 switch (skip)
5173 {
5174 case MOVE_POS_MATCH_OR_ZV:
5175 reached = 8;
5176 goto out;
5177
5178 case MOVE_NEWLINE_OR_CR:
5179 set_iterator_to_next (it, 1);
5180 it->continuation_lines_width = 0;
5181 break;
5182
5183 case MOVE_LINE_TRUNCATED:
5184 it->continuation_lines_width = 0;
5185 reseat_at_next_visible_line_start (it, 0);
5186 if ((op & MOVE_TO_POS) != 0
5187 && IT_CHARPOS (*it) > to_charpos)
5188 {
5189 reached = 9;
5190 goto out;
5191 }
5192 break;
5193
5194 case MOVE_LINE_CONTINUED:
5195 it->continuation_lines_width += it->current_x;
5196 break;
5197
5198 default:
5199 abort ();
5200 }
5201
5202 /* Reset/increment for the next run. */
5203 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
5204 it->current_x = it->hpos = 0;
5205 it->current_y += it->max_ascent + it->max_descent;
5206 ++it->vpos;
5207 last_height = it->max_ascent + it->max_descent;
5208 last_max_ascent = it->max_ascent;
5209 it->max_ascent = it->max_descent = 0;
5210 }
5211
5212 out:
5213
5214 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
5215 }
5216
5217
5218 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
5219
5220 If DY > 0, move IT backward at least that many pixels. DY = 0
5221 means move IT backward to the preceding line start or BEGV. This
5222 function may move over more than DY pixels if IT->current_y - DY
5223 ends up in the middle of a line; in this case IT->current_y will be
5224 set to the top of the line moved to. */
5225
5226 void
5227 move_it_vertically_backward (it, dy)
5228 struct it *it;
5229 int dy;
5230 {
5231 int nlines, h, line_height;
5232 struct it it2;
5233 int start_pos = IT_CHARPOS (*it);
5234
5235 xassert (dy >= 0);
5236
5237 /* Estimate how many newlines we must move back. */
5238 nlines = max (1, dy / CANON_Y_UNIT (it->f));
5239
5240 /* Set the iterator's position that many lines back. */
5241 while (nlines-- && IT_CHARPOS (*it) > BEGV)
5242 back_to_previous_visible_line_start (it);
5243
5244 /* Reseat the iterator here. When moving backward, we don't want
5245 reseat to skip forward over invisible text, set up the iterator
5246 to deliver from overlay strings at the new position etc. So,
5247 use reseat_1 here. */
5248 reseat_1 (it, it->current.pos, 1);
5249
5250 /* We are now surely at a line start. */
5251 it->current_x = it->hpos = 0;
5252
5253 /* Move forward and see what y-distance we moved. First move to the
5254 start of the next line so that we get its height. We need this
5255 height to be able to tell whether we reached the specified
5256 y-distance. */
5257 it2 = *it;
5258 it2.max_ascent = it2.max_descent = 0;
5259 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
5260 MOVE_TO_POS | MOVE_TO_VPOS);
5261 xassert (IT_CHARPOS (*it) >= BEGV);
5262 line_height = it2.max_ascent + it2.max_descent;
5263
5264 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
5265 xassert (IT_CHARPOS (*it) >= BEGV);
5266 h = it2.current_y - it->current_y;
5267 nlines = it2.vpos - it->vpos;
5268
5269 /* Correct IT's y and vpos position. */
5270 it->vpos -= nlines;
5271 it->current_y -= h;
5272
5273 if (dy == 0)
5274 {
5275 /* DY == 0 means move to the start of the screen line. The
5276 value of nlines is > 0 if continuation lines were involved. */
5277 if (nlines > 0)
5278 move_it_by_lines (it, nlines, 1);
5279 xassert (IT_CHARPOS (*it) <= start_pos);
5280 }
5281 else if (nlines)
5282 {
5283 /* The y-position we try to reach. Note that h has been
5284 subtracted in front of the if-statement. */
5285 int target_y = it->current_y + h - dy;
5286
5287 /* If we did not reach target_y, try to move further backward if
5288 we can. If we moved too far backward, try to move forward. */
5289 if (target_y < it->current_y
5290 && IT_CHARPOS (*it) > BEGV)
5291 {
5292 move_it_vertically (it, target_y - it->current_y);
5293 xassert (IT_CHARPOS (*it) >= BEGV);
5294 }
5295 else if (target_y >= it->current_y + line_height
5296 && IT_CHARPOS (*it) < ZV)
5297 {
5298 move_it_vertically (it, target_y - (it->current_y + line_height));
5299 xassert (IT_CHARPOS (*it) >= BEGV);
5300 }
5301 }
5302 }
5303
5304
5305 /* Move IT by a specified amount of pixel lines DY. DY negative means
5306 move backwards. DY = 0 means move to start of screen line. At the
5307 end, IT will be on the start of a screen line. */
5308
5309 void
5310 move_it_vertically (it, dy)
5311 struct it *it;
5312 int dy;
5313 {
5314 if (dy <= 0)
5315 move_it_vertically_backward (it, -dy);
5316 else if (dy > 0)
5317 {
5318 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
5319 move_it_to (it, ZV, -1, it->current_y + dy, -1,
5320 MOVE_TO_POS | MOVE_TO_Y);
5321 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
5322
5323 /* If buffer ends in ZV without a newline, move to the start of
5324 the line to satisfy the post-condition. */
5325 if (IT_CHARPOS (*it) == ZV
5326 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
5327 move_it_by_lines (it, 0, 0);
5328 }
5329 }
5330
5331
5332 /* Move iterator IT past the end of the text line it is in. */
5333
5334 void
5335 move_it_past_eol (it)
5336 struct it *it;
5337 {
5338 enum move_it_result rc;
5339
5340 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
5341 if (rc == MOVE_NEWLINE_OR_CR)
5342 set_iterator_to_next (it, 0);
5343 }
5344
5345
5346 #if 0 /* Currently not used. */
5347
5348 /* Return non-zero if some text between buffer positions START_CHARPOS
5349 and END_CHARPOS is invisible. IT->window is the window for text
5350 property lookup. */
5351
5352 static int
5353 invisible_text_between_p (it, start_charpos, end_charpos)
5354 struct it *it;
5355 int start_charpos, end_charpos;
5356 {
5357 Lisp_Object prop, limit;
5358 int invisible_found_p;
5359
5360 xassert (it != NULL && start_charpos <= end_charpos);
5361
5362 /* Is text at START invisible? */
5363 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
5364 it->window);
5365 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5366 invisible_found_p = 1;
5367 else
5368 {
5369 limit = Fnext_single_char_property_change (make_number (start_charpos),
5370 Qinvisible, Qnil,
5371 make_number (end_charpos));
5372 invisible_found_p = XFASTINT (limit) < end_charpos;
5373 }
5374
5375 return invisible_found_p;
5376 }
5377
5378 #endif /* 0 */
5379
5380
5381 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
5382 negative means move up. DVPOS == 0 means move to the start of the
5383 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
5384 NEED_Y_P is zero, IT->current_y will be left unchanged.
5385
5386 Further optimization ideas: If we would know that IT->f doesn't use
5387 a face with proportional font, we could be faster for
5388 truncate-lines nil. */
5389
5390 void
5391 move_it_by_lines (it, dvpos, need_y_p)
5392 struct it *it;
5393 int dvpos, need_y_p;
5394 {
5395 struct position pos;
5396
5397 if (!FRAME_WINDOW_P (it->f))
5398 {
5399 struct text_pos textpos;
5400
5401 /* We can use vmotion on frames without proportional fonts. */
5402 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
5403 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
5404 reseat (it, textpos, 1);
5405 it->vpos += pos.vpos;
5406 it->current_y += pos.vpos;
5407 }
5408 else if (dvpos == 0)
5409 {
5410 /* DVPOS == 0 means move to the start of the screen line. */
5411 move_it_vertically_backward (it, 0);
5412 xassert (it->current_x == 0 && it->hpos == 0);
5413 }
5414 else if (dvpos > 0)
5415 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
5416 else
5417 {
5418 struct it it2;
5419 int start_charpos, i;
5420
5421 /* Go back -DVPOS visible lines and reseat the iterator there. */
5422 start_charpos = IT_CHARPOS (*it);
5423 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
5424 back_to_previous_visible_line_start (it);
5425 reseat (it, it->current.pos, 1);
5426 it->current_x = it->hpos = 0;
5427
5428 /* Above call may have moved too far if continuation lines
5429 are involved. Scan forward and see if it did. */
5430 it2 = *it;
5431 it2.vpos = it2.current_y = 0;
5432 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
5433 it->vpos -= it2.vpos;
5434 it->current_y -= it2.current_y;
5435 it->current_x = it->hpos = 0;
5436
5437 /* If we moved too far, move IT some lines forward. */
5438 if (it2.vpos > -dvpos)
5439 {
5440 int delta = it2.vpos + dvpos;
5441 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
5442 }
5443 }
5444 }
5445
5446
5447 \f
5448 /***********************************************************************
5449 Messages
5450 ***********************************************************************/
5451
5452
5453 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
5454 to *Messages*. */
5455
5456 void
5457 add_to_log (format, arg1, arg2)
5458 char *format;
5459 Lisp_Object arg1, arg2;
5460 {
5461 Lisp_Object args[3];
5462 Lisp_Object msg, fmt;
5463 char *buffer;
5464 int len;
5465 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5466
5467 fmt = msg = Qnil;
5468 GCPRO4 (fmt, msg, arg1, arg2);
5469
5470 args[0] = fmt = build_string (format);
5471 args[1] = arg1;
5472 args[2] = arg2;
5473 msg = Fformat (3, args);
5474
5475 len = STRING_BYTES (XSTRING (msg)) + 1;
5476 buffer = (char *) alloca (len);
5477 strcpy (buffer, XSTRING (msg)->data);
5478
5479 message_dolog (buffer, len - 1, 1, 0);
5480 UNGCPRO;
5481 }
5482
5483
5484 /* Output a newline in the *Messages* buffer if "needs" one. */
5485
5486 void
5487 message_log_maybe_newline ()
5488 {
5489 if (message_log_need_newline)
5490 message_dolog ("", 0, 1, 0);
5491 }
5492
5493
5494 /* Add a string M of length NBYTES to the message log, optionally
5495 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
5496 nonzero, means interpret the contents of M as multibyte. This
5497 function calls low-level routines in order to bypass text property
5498 hooks, etc. which might not be safe to run. */
5499
5500 void
5501 message_dolog (m, nbytes, nlflag, multibyte)
5502 char *m;
5503 int nbytes, nlflag, multibyte;
5504 {
5505 if (!NILP (Vmessage_log_max))
5506 {
5507 struct buffer *oldbuf;
5508 Lisp_Object oldpoint, oldbegv, oldzv;
5509 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5510 int point_at_end = 0;
5511 int zv_at_end = 0;
5512 Lisp_Object old_deactivate_mark, tem;
5513 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5514
5515 old_deactivate_mark = Vdeactivate_mark;
5516 oldbuf = current_buffer;
5517 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
5518 current_buffer->undo_list = Qt;
5519
5520 oldpoint = Fpoint_marker ();
5521 oldbegv = Fpoint_min_marker ();
5522 oldzv = Fpoint_max_marker ();
5523 GCPRO4 (oldpoint, oldbegv, oldzv, old_deactivate_mark);
5524
5525 if (PT == Z)
5526 point_at_end = 1;
5527 if (ZV == Z)
5528 zv_at_end = 1;
5529
5530 BEGV = BEG;
5531 BEGV_BYTE = BEG_BYTE;
5532 ZV = Z;
5533 ZV_BYTE = Z_BYTE;
5534 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5535
5536 /* Insert the string--maybe converting multibyte to single byte
5537 or vice versa, so that all the text fits the buffer. */
5538 if (multibyte
5539 && NILP (current_buffer->enable_multibyte_characters))
5540 {
5541 int i, c, char_bytes;
5542 unsigned char work[1];
5543
5544 /* Convert a multibyte string to single-byte
5545 for the *Message* buffer. */
5546 for (i = 0; i < nbytes; i += nbytes)
5547 {
5548 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
5549 work[0] = (SINGLE_BYTE_CHAR_P (c)
5550 ? c
5551 : multibyte_char_to_unibyte (c, Qnil));
5552 insert_1_both (work, 1, 1, 1, 0, 0);
5553 }
5554 }
5555 else if (! multibyte
5556 && ! NILP (current_buffer->enable_multibyte_characters))
5557 {
5558 int i, c, char_bytes;
5559 unsigned char *msg = (unsigned char *) m;
5560 unsigned char str[MAX_MULTIBYTE_LENGTH];
5561 /* Convert a single-byte string to multibyte
5562 for the *Message* buffer. */
5563 for (i = 0; i < nbytes; i++)
5564 {
5565 c = unibyte_char_to_multibyte (msg[i]);
5566 char_bytes = CHAR_STRING (c, str);
5567 insert_1_both (str, 1, char_bytes, 1, 0, 0);
5568 }
5569 }
5570 else if (nbytes)
5571 insert_1 (m, nbytes, 1, 0, 0);
5572
5573 if (nlflag)
5574 {
5575 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
5576 insert_1 ("\n", 1, 1, 0, 0);
5577
5578 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
5579 this_bol = PT;
5580 this_bol_byte = PT_BYTE;
5581
5582 if (this_bol > BEG)
5583 {
5584 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
5585 prev_bol = PT;
5586 prev_bol_byte = PT_BYTE;
5587
5588 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
5589 this_bol, this_bol_byte);
5590 if (dup)
5591 {
5592 del_range_both (prev_bol, prev_bol_byte,
5593 this_bol, this_bol_byte, 0);
5594 if (dup > 1)
5595 {
5596 char dupstr[40];
5597 int duplen;
5598
5599 /* If you change this format, don't forget to also
5600 change message_log_check_duplicate. */
5601 sprintf (dupstr, " [%d times]", dup);
5602 duplen = strlen (dupstr);
5603 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
5604 insert_1 (dupstr, duplen, 1, 0, 1);
5605 }
5606 }
5607 }
5608
5609 if (NATNUMP (Vmessage_log_max))
5610 {
5611 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
5612 -XFASTINT (Vmessage_log_max) - 1, 0);
5613 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
5614 }
5615 }
5616 BEGV = XMARKER (oldbegv)->charpos;
5617 BEGV_BYTE = marker_byte_position (oldbegv);
5618
5619 if (zv_at_end)
5620 {
5621 ZV = Z;
5622 ZV_BYTE = Z_BYTE;
5623 }
5624 else
5625 {
5626 ZV = XMARKER (oldzv)->charpos;
5627 ZV_BYTE = marker_byte_position (oldzv);
5628 }
5629
5630 if (point_at_end)
5631 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5632 else
5633 /* We can't do Fgoto_char (oldpoint) because it will run some
5634 Lisp code. */
5635 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
5636 XMARKER (oldpoint)->bytepos);
5637
5638 UNGCPRO;
5639 free_marker (oldpoint);
5640 free_marker (oldbegv);
5641 free_marker (oldzv);
5642
5643 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
5644 set_buffer_internal (oldbuf);
5645 if (NILP (tem))
5646 windows_or_buffers_changed = old_windows_or_buffers_changed;
5647 message_log_need_newline = !nlflag;
5648 Vdeactivate_mark = old_deactivate_mark;
5649 }
5650 }
5651
5652
5653 /* We are at the end of the buffer after just having inserted a newline.
5654 (Note: We depend on the fact we won't be crossing the gap.)
5655 Check to see if the most recent message looks a lot like the previous one.
5656 Return 0 if different, 1 if the new one should just replace it, or a
5657 value N > 1 if we should also append " [N times]". */
5658
5659 static int
5660 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
5661 int prev_bol, this_bol;
5662 int prev_bol_byte, this_bol_byte;
5663 {
5664 int i;
5665 int len = Z_BYTE - 1 - this_bol_byte;
5666 int seen_dots = 0;
5667 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
5668 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
5669
5670 for (i = 0; i < len; i++)
5671 {
5672 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
5673 seen_dots = 1;
5674 if (p1[i] != p2[i])
5675 return seen_dots;
5676 }
5677 p1 += len;
5678 if (*p1 == '\n')
5679 return 2;
5680 if (*p1++ == ' ' && *p1++ == '[')
5681 {
5682 int n = 0;
5683 while (*p1 >= '0' && *p1 <= '9')
5684 n = n * 10 + *p1++ - '0';
5685 if (strncmp (p1, " times]\n", 8) == 0)
5686 return n+1;
5687 }
5688 return 0;
5689 }
5690
5691
5692 /* Display an echo area message M with a specified length of NBYTES
5693 bytes. The string may include null characters. If M is 0, clear
5694 out any existing message, and let the mini-buffer text show
5695 through.
5696
5697 The buffer M must continue to exist until after the echo area gets
5698 cleared or some other message gets displayed there. This means do
5699 not pass text that is stored in a Lisp string; do not pass text in
5700 a buffer that was alloca'd. */
5701
5702 void
5703 message2 (m, nbytes, multibyte)
5704 char *m;
5705 int nbytes;
5706 int multibyte;
5707 {
5708 /* First flush out any partial line written with print. */
5709 message_log_maybe_newline ();
5710 if (m)
5711 message_dolog (m, nbytes, 1, multibyte);
5712 message2_nolog (m, nbytes, multibyte);
5713 }
5714
5715
5716 /* The non-logging counterpart of message2. */
5717
5718 void
5719 message2_nolog (m, nbytes, multibyte)
5720 char *m;
5721 int nbytes;
5722 {
5723 struct frame *sf = SELECTED_FRAME ();
5724 message_enable_multibyte = multibyte;
5725
5726 if (noninteractive)
5727 {
5728 if (noninteractive_need_newline)
5729 putc ('\n', stderr);
5730 noninteractive_need_newline = 0;
5731 if (m)
5732 fwrite (m, nbytes, 1, stderr);
5733 if (cursor_in_echo_area == 0)
5734 fprintf (stderr, "\n");
5735 fflush (stderr);
5736 }
5737 /* A null message buffer means that the frame hasn't really been
5738 initialized yet. Error messages get reported properly by
5739 cmd_error, so this must be just an informative message; toss it. */
5740 else if (INTERACTIVE
5741 && sf->glyphs_initialized_p
5742 && FRAME_MESSAGE_BUF (sf))
5743 {
5744 Lisp_Object mini_window;
5745 struct frame *f;
5746
5747 /* Get the frame containing the mini-buffer
5748 that the selected frame is using. */
5749 mini_window = FRAME_MINIBUF_WINDOW (sf);
5750 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5751
5752 FRAME_SAMPLE_VISIBILITY (f);
5753 if (FRAME_VISIBLE_P (sf)
5754 && ! FRAME_VISIBLE_P (f))
5755 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
5756
5757 if (m)
5758 {
5759 set_message (m, Qnil, nbytes, multibyte);
5760 if (minibuffer_auto_raise)
5761 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5762 }
5763 else
5764 clear_message (1, 1);
5765
5766 do_pending_window_change (0);
5767 echo_area_display (1);
5768 do_pending_window_change (0);
5769 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5770 (*frame_up_to_date_hook) (f);
5771 }
5772 }
5773
5774
5775 /* Display an echo area message M with a specified length of NBYTES
5776 bytes. The string may include null characters. If M is not a
5777 string, clear out any existing message, and let the mini-buffer
5778 text show through. */
5779
5780 void
5781 message3 (m, nbytes, multibyte)
5782 Lisp_Object m;
5783 int nbytes;
5784 int multibyte;
5785 {
5786 struct gcpro gcpro1;
5787
5788 GCPRO1 (m);
5789
5790 /* First flush out any partial line written with print. */
5791 message_log_maybe_newline ();
5792 if (STRINGP (m))
5793 message_dolog (XSTRING (m)->data, nbytes, 1, multibyte);
5794 message3_nolog (m, nbytes, multibyte);
5795
5796 UNGCPRO;
5797 }
5798
5799
5800 /* The non-logging version of message3. */
5801
5802 void
5803 message3_nolog (m, nbytes, multibyte)
5804 Lisp_Object m;
5805 int nbytes, multibyte;
5806 {
5807 struct frame *sf = SELECTED_FRAME ();
5808 message_enable_multibyte = multibyte;
5809
5810 if (noninteractive)
5811 {
5812 if (noninteractive_need_newline)
5813 putc ('\n', stderr);
5814 noninteractive_need_newline = 0;
5815 if (STRINGP (m))
5816 fwrite (XSTRING (m)->data, nbytes, 1, stderr);
5817 if (cursor_in_echo_area == 0)
5818 fprintf (stderr, "\n");
5819 fflush (stderr);
5820 }
5821 /* A null message buffer means that the frame hasn't really been
5822 initialized yet. Error messages get reported properly by
5823 cmd_error, so this must be just an informative message; toss it. */
5824 else if (INTERACTIVE
5825 && sf->glyphs_initialized_p
5826 && FRAME_MESSAGE_BUF (sf))
5827 {
5828 Lisp_Object mini_window;
5829 Lisp_Object frame;
5830 struct frame *f;
5831
5832 /* Get the frame containing the mini-buffer
5833 that the selected frame is using. */
5834 mini_window = FRAME_MINIBUF_WINDOW (sf);
5835 frame = XWINDOW (mini_window)->frame;
5836 f = XFRAME (frame);
5837
5838 FRAME_SAMPLE_VISIBILITY (f);
5839 if (FRAME_VISIBLE_P (sf)
5840 && !FRAME_VISIBLE_P (f))
5841 Fmake_frame_visible (frame);
5842
5843 if (STRINGP (m) && XSTRING (m)->size)
5844 {
5845 set_message (NULL, m, nbytes, multibyte);
5846 if (minibuffer_auto_raise)
5847 Fraise_frame (frame);
5848 }
5849 else
5850 clear_message (1, 1);
5851
5852 do_pending_window_change (0);
5853 echo_area_display (1);
5854 do_pending_window_change (0);
5855 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5856 (*frame_up_to_date_hook) (f);
5857 }
5858 }
5859
5860
5861 /* Display a null-terminated echo area message M. If M is 0, clear
5862 out any existing message, and let the mini-buffer text show through.
5863
5864 The buffer M must continue to exist until after the echo area gets
5865 cleared or some other message gets displayed there. Do not pass
5866 text that is stored in a Lisp string. Do not pass text in a buffer
5867 that was alloca'd. */
5868
5869 void
5870 message1 (m)
5871 char *m;
5872 {
5873 message2 (m, (m ? strlen (m) : 0), 0);
5874 }
5875
5876
5877 /* The non-logging counterpart of message1. */
5878
5879 void
5880 message1_nolog (m)
5881 char *m;
5882 {
5883 message2_nolog (m, (m ? strlen (m) : 0), 0);
5884 }
5885
5886 /* Display a message M which contains a single %s
5887 which gets replaced with STRING. */
5888
5889 void
5890 message_with_string (m, string, log)
5891 char *m;
5892 Lisp_Object string;
5893 int log;
5894 {
5895 if (noninteractive)
5896 {
5897 if (m)
5898 {
5899 if (noninteractive_need_newline)
5900 putc ('\n', stderr);
5901 noninteractive_need_newline = 0;
5902 fprintf (stderr, m, XSTRING (string)->data);
5903 if (cursor_in_echo_area == 0)
5904 fprintf (stderr, "\n");
5905 fflush (stderr);
5906 }
5907 }
5908 else if (INTERACTIVE)
5909 {
5910 /* The frame whose minibuffer we're going to display the message on.
5911 It may be larger than the selected frame, so we need
5912 to use its buffer, not the selected frame's buffer. */
5913 Lisp_Object mini_window;
5914 struct frame *f, *sf = SELECTED_FRAME ();
5915
5916 /* Get the frame containing the minibuffer
5917 that the selected frame is using. */
5918 mini_window = FRAME_MINIBUF_WINDOW (sf);
5919 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5920
5921 /* A null message buffer means that the frame hasn't really been
5922 initialized yet. Error messages get reported properly by
5923 cmd_error, so this must be just an informative message; toss it. */
5924 if (FRAME_MESSAGE_BUF (f))
5925 {
5926 int len;
5927 char *a[1];
5928 a[0] = (char *) XSTRING (string)->data;
5929
5930 len = doprnt (FRAME_MESSAGE_BUF (f),
5931 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5932
5933 if (log)
5934 message2 (FRAME_MESSAGE_BUF (f), len,
5935 STRING_MULTIBYTE (string));
5936 else
5937 message2_nolog (FRAME_MESSAGE_BUF (f), len,
5938 STRING_MULTIBYTE (string));
5939
5940 /* Print should start at the beginning of the message
5941 buffer next time. */
5942 message_buf_print = 0;
5943 }
5944 }
5945 }
5946
5947
5948 /* Dump an informative message to the minibuf. If M is 0, clear out
5949 any existing message, and let the mini-buffer text show through. */
5950
5951 /* VARARGS 1 */
5952 void
5953 message (m, a1, a2, a3)
5954 char *m;
5955 EMACS_INT a1, a2, a3;
5956 {
5957 if (noninteractive)
5958 {
5959 if (m)
5960 {
5961 if (noninteractive_need_newline)
5962 putc ('\n', stderr);
5963 noninteractive_need_newline = 0;
5964 fprintf (stderr, m, a1, a2, a3);
5965 if (cursor_in_echo_area == 0)
5966 fprintf (stderr, "\n");
5967 fflush (stderr);
5968 }
5969 }
5970 else if (INTERACTIVE)
5971 {
5972 /* The frame whose mini-buffer we're going to display the message
5973 on. It may be larger than the selected frame, so we need to
5974 use its buffer, not the selected frame's buffer. */
5975 Lisp_Object mini_window;
5976 struct frame *f, *sf = SELECTED_FRAME ();
5977
5978 /* Get the frame containing the mini-buffer
5979 that the selected frame is using. */
5980 mini_window = FRAME_MINIBUF_WINDOW (sf);
5981 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5982
5983 /* A null message buffer means that the frame hasn't really been
5984 initialized yet. Error messages get reported properly by
5985 cmd_error, so this must be just an informative message; toss
5986 it. */
5987 if (FRAME_MESSAGE_BUF (f))
5988 {
5989 if (m)
5990 {
5991 int len;
5992 #ifdef NO_ARG_ARRAY
5993 char *a[3];
5994 a[0] = (char *) a1;
5995 a[1] = (char *) a2;
5996 a[2] = (char *) a3;
5997
5998 len = doprnt (FRAME_MESSAGE_BUF (f),
5999 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6000 #else
6001 len = doprnt (FRAME_MESSAGE_BUF (f),
6002 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
6003 (char **) &a1);
6004 #endif /* NO_ARG_ARRAY */
6005
6006 message2 (FRAME_MESSAGE_BUF (f), len, 0);
6007 }
6008 else
6009 message1 (0);
6010
6011 /* Print should start at the beginning of the message
6012 buffer next time. */
6013 message_buf_print = 0;
6014 }
6015 }
6016 }
6017
6018
6019 /* The non-logging version of message. */
6020
6021 void
6022 message_nolog (m, a1, a2, a3)
6023 char *m;
6024 EMACS_INT a1, a2, a3;
6025 {
6026 Lisp_Object old_log_max;
6027 old_log_max = Vmessage_log_max;
6028 Vmessage_log_max = Qnil;
6029 message (m, a1, a2, a3);
6030 Vmessage_log_max = old_log_max;
6031 }
6032
6033
6034 /* Display the current message in the current mini-buffer. This is
6035 only called from error handlers in process.c, and is not time
6036 critical. */
6037
6038 void
6039 update_echo_area ()
6040 {
6041 if (!NILP (echo_area_buffer[0]))
6042 {
6043 Lisp_Object string;
6044 string = Fcurrent_message ();
6045 message3 (string, XSTRING (string)->size,
6046 !NILP (current_buffer->enable_multibyte_characters));
6047 }
6048 }
6049
6050
6051 /* Make sure echo area buffers in echo_buffers[] are life. If they
6052 aren't, make new ones. */
6053
6054 static void
6055 ensure_echo_area_buffers ()
6056 {
6057 int i;
6058
6059 for (i = 0; i < 2; ++i)
6060 if (!BUFFERP (echo_buffer[i])
6061 || NILP (XBUFFER (echo_buffer[i])->name))
6062 {
6063 char name[30];
6064 Lisp_Object old_buffer;
6065 int j;
6066
6067 old_buffer = echo_buffer[i];
6068 sprintf (name, " *Echo Area %d*", i);
6069 echo_buffer[i] = Fget_buffer_create (build_string (name));
6070 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
6071
6072 for (j = 0; j < 2; ++j)
6073 if (EQ (old_buffer, echo_area_buffer[j]))
6074 echo_area_buffer[j] = echo_buffer[i];
6075 }
6076 }
6077
6078
6079 /* Call FN with args A1..A4 with either the current or last displayed
6080 echo_area_buffer as current buffer.
6081
6082 WHICH zero means use the current message buffer
6083 echo_area_buffer[0]. If that is nil, choose a suitable buffer
6084 from echo_buffer[] and clear it.
6085
6086 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
6087 suitable buffer from echo_buffer[] and clear it.
6088
6089 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
6090 that the current message becomes the last displayed one, make
6091 choose a suitable buffer for echo_area_buffer[0], and clear it.
6092
6093 Value is what FN returns. */
6094
6095 static int
6096 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
6097 struct window *w;
6098 int which;
6099 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
6100 EMACS_INT a1;
6101 Lisp_Object a2;
6102 EMACS_INT a3, a4;
6103 {
6104 Lisp_Object buffer;
6105 int this_one, the_other, clear_buffer_p, rc;
6106 int count = BINDING_STACK_SIZE ();
6107
6108 /* If buffers aren't life, make new ones. */
6109 ensure_echo_area_buffers ();
6110
6111 clear_buffer_p = 0;
6112
6113 if (which == 0)
6114 this_one = 0, the_other = 1;
6115 else if (which > 0)
6116 this_one = 1, the_other = 0;
6117 else
6118 {
6119 this_one = 0, the_other = 1;
6120 clear_buffer_p = 1;
6121
6122 /* We need a fresh one in case the current echo buffer equals
6123 the one containing the last displayed echo area message. */
6124 if (!NILP (echo_area_buffer[this_one])
6125 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
6126 echo_area_buffer[this_one] = Qnil;
6127 }
6128
6129 /* Choose a suitable buffer from echo_buffer[] is we don't
6130 have one. */
6131 if (NILP (echo_area_buffer[this_one]))
6132 {
6133 echo_area_buffer[this_one]
6134 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
6135 ? echo_buffer[the_other]
6136 : echo_buffer[this_one]);
6137 clear_buffer_p = 1;
6138 }
6139
6140 buffer = echo_area_buffer[this_one];
6141
6142 record_unwind_protect (unwind_with_echo_area_buffer,
6143 with_echo_area_buffer_unwind_data (w));
6144
6145 /* Make the echo area buffer current. Note that for display
6146 purposes, it is not necessary that the displayed window's buffer
6147 == current_buffer, except for text property lookup. So, let's
6148 only set that buffer temporarily here without doing a full
6149 Fset_window_buffer. We must also change w->pointm, though,
6150 because otherwise an assertions in unshow_buffer fails, and Emacs
6151 aborts. */
6152 set_buffer_internal_1 (XBUFFER (buffer));
6153 if (w)
6154 {
6155 w->buffer = buffer;
6156 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
6157 }
6158
6159 current_buffer->undo_list = Qt;
6160 current_buffer->read_only = Qnil;
6161 specbind (Qinhibit_read_only, Qt);
6162
6163 if (clear_buffer_p && Z > BEG)
6164 del_range (BEG, Z);
6165
6166 xassert (BEGV >= BEG);
6167 xassert (ZV <= Z && ZV >= BEGV);
6168
6169 rc = fn (a1, a2, a3, a4);
6170
6171 xassert (BEGV >= BEG);
6172 xassert (ZV <= Z && ZV >= BEGV);
6173
6174 unbind_to (count, Qnil);
6175 return rc;
6176 }
6177
6178
6179 /* Save state that should be preserved around the call to the function
6180 FN called in with_echo_area_buffer. */
6181
6182 static Lisp_Object
6183 with_echo_area_buffer_unwind_data (w)
6184 struct window *w;
6185 {
6186 int i = 0;
6187 Lisp_Object vector;
6188
6189 /* Reduce consing by keeping one vector in
6190 Vwith_echo_area_save_vector. */
6191 vector = Vwith_echo_area_save_vector;
6192 Vwith_echo_area_save_vector = Qnil;
6193
6194 if (NILP (vector))
6195 vector = Fmake_vector (make_number (7), Qnil);
6196
6197 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
6198 AREF (vector, i) = Vdeactivate_mark, ++i;
6199 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
6200
6201 if (w)
6202 {
6203 XSETWINDOW (AREF (vector, i), w); ++i;
6204 AREF (vector, i) = w->buffer; ++i;
6205 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
6206 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
6207 }
6208 else
6209 {
6210 int end = i + 4;
6211 for (; i < end; ++i)
6212 AREF (vector, i) = Qnil;
6213 }
6214
6215 xassert (i == ASIZE (vector));
6216 return vector;
6217 }
6218
6219
6220 /* Restore global state from VECTOR which was created by
6221 with_echo_area_buffer_unwind_data. */
6222
6223 static Lisp_Object
6224 unwind_with_echo_area_buffer (vector)
6225 Lisp_Object vector;
6226 {
6227 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
6228 Vdeactivate_mark = AREF (vector, 1);
6229 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
6230
6231 if (WINDOWP (AREF (vector, 3)))
6232 {
6233 struct window *w;
6234 Lisp_Object buffer, charpos, bytepos;
6235
6236 w = XWINDOW (AREF (vector, 3));
6237 buffer = AREF (vector, 4);
6238 charpos = AREF (vector, 5);
6239 bytepos = AREF (vector, 6);
6240
6241 w->buffer = buffer;
6242 set_marker_both (w->pointm, buffer,
6243 XFASTINT (charpos), XFASTINT (bytepos));
6244 }
6245
6246 Vwith_echo_area_save_vector = vector;
6247 return Qnil;
6248 }
6249
6250
6251 /* Set up the echo area for use by print functions. MULTIBYTE_P
6252 non-zero means we will print multibyte. */
6253
6254 void
6255 setup_echo_area_for_printing (multibyte_p)
6256 int multibyte_p;
6257 {
6258 ensure_echo_area_buffers ();
6259
6260 if (!message_buf_print)
6261 {
6262 /* A message has been output since the last time we printed.
6263 Choose a fresh echo area buffer. */
6264 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6265 echo_area_buffer[0] = echo_buffer[1];
6266 else
6267 echo_area_buffer[0] = echo_buffer[0];
6268
6269 /* Switch to that buffer and clear it. */
6270 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6271 current_buffer->truncate_lines = Qnil;
6272
6273 if (Z > BEG)
6274 {
6275 int count = BINDING_STACK_SIZE ();
6276 specbind (Qinhibit_read_only, Qt);
6277 del_range (BEG, Z);
6278 unbind_to (count, Qnil);
6279 }
6280 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6281
6282 /* Set up the buffer for the multibyteness we need. */
6283 if (multibyte_p
6284 != !NILP (current_buffer->enable_multibyte_characters))
6285 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
6286
6287 /* Raise the frame containing the echo area. */
6288 if (minibuffer_auto_raise)
6289 {
6290 struct frame *sf = SELECTED_FRAME ();
6291 Lisp_Object mini_window;
6292 mini_window = FRAME_MINIBUF_WINDOW (sf);
6293 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6294 }
6295
6296 message_log_maybe_newline ();
6297 message_buf_print = 1;
6298 }
6299 else
6300 {
6301 if (NILP (echo_area_buffer[0]))
6302 {
6303 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6304 echo_area_buffer[0] = echo_buffer[1];
6305 else
6306 echo_area_buffer[0] = echo_buffer[0];
6307 }
6308
6309 if (current_buffer != XBUFFER (echo_area_buffer[0]))
6310 {
6311 /* Someone switched buffers between print requests. */
6312 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6313 current_buffer->truncate_lines = Qnil;
6314 }
6315 }
6316 }
6317
6318
6319 /* Display an echo area message in window W. Value is non-zero if W's
6320 height is changed. If display_last_displayed_message_p is
6321 non-zero, display the message that was last displayed, otherwise
6322 display the current message. */
6323
6324 static int
6325 display_echo_area (w)
6326 struct window *w;
6327 {
6328 int i, no_message_p, window_height_changed_p, count;
6329
6330 /* Temporarily disable garbage collections while displaying the echo
6331 area. This is done because a GC can print a message itself.
6332 That message would modify the echo area buffer's contents while a
6333 redisplay of the buffer is going on, and seriously confuse
6334 redisplay. */
6335 count = inhibit_garbage_collection ();
6336
6337 /* If there is no message, we must call display_echo_area_1
6338 nevertheless because it resizes the window. But we will have to
6339 reset the echo_area_buffer in question to nil at the end because
6340 with_echo_area_buffer will sets it to an empty buffer. */
6341 i = display_last_displayed_message_p ? 1 : 0;
6342 no_message_p = NILP (echo_area_buffer[i]);
6343
6344 window_height_changed_p
6345 = with_echo_area_buffer (w, display_last_displayed_message_p,
6346 display_echo_area_1,
6347 (EMACS_INT) w, Qnil, 0, 0);
6348
6349 if (no_message_p)
6350 echo_area_buffer[i] = Qnil;
6351
6352 unbind_to (count, Qnil);
6353 return window_height_changed_p;
6354 }
6355
6356
6357 /* Helper for display_echo_area. Display the current buffer which
6358 contains the current echo area message in window W, a mini-window,
6359 a pointer to which is passed in A1. A2..A4 are currently not used.
6360 Change the height of W so that all of the message is displayed.
6361 Value is non-zero if height of W was changed. */
6362
6363 static int
6364 display_echo_area_1 (a1, a2, a3, a4)
6365 EMACS_INT a1;
6366 Lisp_Object a2;
6367 EMACS_INT a3, a4;
6368 {
6369 struct window *w = (struct window *) a1;
6370 Lisp_Object window;
6371 struct text_pos start;
6372 int window_height_changed_p = 0;
6373
6374 /* Do this before displaying, so that we have a large enough glyph
6375 matrix for the display. */
6376 window_height_changed_p = resize_mini_window (w, 0);
6377
6378 /* Display. */
6379 clear_glyph_matrix (w->desired_matrix);
6380 XSETWINDOW (window, w);
6381 SET_TEXT_POS (start, BEG, BEG_BYTE);
6382 try_window (window, start);
6383
6384 return window_height_changed_p;
6385 }
6386
6387
6388 /* Resize the echo area window to exactly the size needed for the
6389 currently displayed message, if there is one. */
6390
6391 void
6392 resize_echo_area_axactly ()
6393 {
6394 if (BUFFERP (echo_area_buffer[0])
6395 && WINDOWP (echo_area_window))
6396 {
6397 struct window *w = XWINDOW (echo_area_window);
6398 int resized_p;
6399
6400 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
6401 (EMACS_INT) w, Qnil, 0, 0);
6402 if (resized_p)
6403 {
6404 ++windows_or_buffers_changed;
6405 ++update_mode_lines;
6406 redisplay_internal (0);
6407 }
6408 }
6409 }
6410
6411
6412 /* Callback function for with_echo_area_buffer, when used from
6413 resize_echo_area_axactly. A1 contains a pointer to the window to
6414 resize, A2 to A4 are not used. Value is what resize_mini_window
6415 returns. */
6416
6417 static int
6418 resize_mini_window_1 (a1, a2, a3, a4)
6419 EMACS_INT a1;
6420 Lisp_Object a2;
6421 EMACS_INT a3, a4;
6422 {
6423 return resize_mini_window ((struct window *) a1, 1);
6424 }
6425
6426
6427 /* Resize mini-window W to fit the size of its contents. EXACT:P
6428 means size the window exactly to the size needed. Otherwise, it's
6429 only enlarged until W's buffer is empty. Value is non-zero if
6430 the window height has been changed. */
6431
6432 int
6433 resize_mini_window (w, exact_p)
6434 struct window *w;
6435 int exact_p;
6436 {
6437 struct frame *f = XFRAME (w->frame);
6438 int window_height_changed_p = 0;
6439
6440 xassert (MINI_WINDOW_P (w));
6441
6442 /* Nil means don't try to resize. */
6443 if (NILP (Vresize_mini_windows)
6444 || (FRAME_X_P (f) && f->output_data.x == NULL))
6445 return 0;
6446
6447 if (!FRAME_MINIBUF_ONLY_P (f))
6448 {
6449 struct it it;
6450 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
6451 int total_height = XFASTINT (root->height) + XFASTINT (w->height);
6452 int height, max_height;
6453 int unit = CANON_Y_UNIT (f);
6454 struct text_pos start;
6455 struct buffer *old_current_buffer = NULL;
6456
6457 if (current_buffer != XBUFFER (w->buffer))
6458 {
6459 old_current_buffer = current_buffer;
6460 set_buffer_internal (XBUFFER (w->buffer));
6461 }
6462
6463 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
6464
6465 /* Compute the max. number of lines specified by the user. */
6466 if (FLOATP (Vmax_mini_window_height))
6467 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
6468 else if (INTEGERP (Vmax_mini_window_height))
6469 max_height = XINT (Vmax_mini_window_height);
6470 else
6471 max_height = total_height / 4;
6472
6473 /* Correct that max. height if it's bogus. */
6474 max_height = max (1, max_height);
6475 max_height = min (total_height, max_height);
6476
6477 /* Find out the height of the text in the window. */
6478 if (it.truncate_lines_p)
6479 height = 1;
6480 else
6481 {
6482 last_height = 0;
6483 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
6484 if (it.max_ascent == 0 && it.max_descent == 0)
6485 height = it.current_y + last_height;
6486 else
6487 height = it.current_y + it.max_ascent + it.max_descent;
6488 height -= it.extra_line_spacing;
6489 height = (height + unit - 1) / unit;
6490 }
6491
6492 /* Compute a suitable window start. */
6493 if (height > max_height)
6494 {
6495 height = max_height;
6496 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
6497 move_it_vertically_backward (&it, (height - 1) * unit);
6498 start = it.current.pos;
6499 }
6500 else
6501 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
6502 SET_MARKER_FROM_TEXT_POS (w->start, start);
6503
6504 if (EQ (Vresize_mini_windows, Qgrow_only))
6505 {
6506 /* Let it grow only, until we display an empty message, in which
6507 case the window shrinks again. */
6508 if (height > XFASTINT (w->height))
6509 {
6510 int old_height = XFASTINT (w->height);
6511 freeze_window_starts (f, 1);
6512 grow_mini_window (w, height - XFASTINT (w->height));
6513 window_height_changed_p = XFASTINT (w->height) != old_height;
6514 }
6515 else if (height < XFASTINT (w->height)
6516 && (exact_p || BEGV == ZV))
6517 {
6518 int old_height = XFASTINT (w->height);
6519 freeze_window_starts (f, 0);
6520 shrink_mini_window (w);
6521 window_height_changed_p = XFASTINT (w->height) != old_height;
6522 }
6523 }
6524 else
6525 {
6526 /* Always resize to exact size needed. */
6527 if (height > XFASTINT (w->height))
6528 {
6529 int old_height = XFASTINT (w->height);
6530 freeze_window_starts (f, 1);
6531 grow_mini_window (w, height - XFASTINT (w->height));
6532 window_height_changed_p = XFASTINT (w->height) != old_height;
6533 }
6534 else if (height < XFASTINT (w->height))
6535 {
6536 int old_height = XFASTINT (w->height);
6537 freeze_window_starts (f, 0);
6538 shrink_mini_window (w);
6539
6540 if (height)
6541 {
6542 freeze_window_starts (f, 1);
6543 grow_mini_window (w, height - XFASTINT (w->height));
6544 }
6545
6546 window_height_changed_p = XFASTINT (w->height) != old_height;
6547 }
6548 }
6549
6550 if (old_current_buffer)
6551 set_buffer_internal (old_current_buffer);
6552 }
6553
6554 return window_height_changed_p;
6555 }
6556
6557
6558 /* Value is the current message, a string, or nil if there is no
6559 current message. */
6560
6561 Lisp_Object
6562 current_message ()
6563 {
6564 Lisp_Object msg;
6565
6566 if (NILP (echo_area_buffer[0]))
6567 msg = Qnil;
6568 else
6569 {
6570 with_echo_area_buffer (0, 0, current_message_1,
6571 (EMACS_INT) &msg, Qnil, 0, 0);
6572 if (NILP (msg))
6573 echo_area_buffer[0] = Qnil;
6574 }
6575
6576 return msg;
6577 }
6578
6579
6580 static int
6581 current_message_1 (a1, a2, a3, a4)
6582 EMACS_INT a1;
6583 Lisp_Object a2;
6584 EMACS_INT a3, a4;
6585 {
6586 Lisp_Object *msg = (Lisp_Object *) a1;
6587
6588 if (Z > BEG)
6589 *msg = make_buffer_string (BEG, Z, 1);
6590 else
6591 *msg = Qnil;
6592 return 0;
6593 }
6594
6595
6596 /* Push the current message on Vmessage_stack for later restauration
6597 by restore_message. Value is non-zero if the current message isn't
6598 empty. This is a relatively infrequent operation, so it's not
6599 worth optimizing. */
6600
6601 int
6602 push_message ()
6603 {
6604 Lisp_Object msg;
6605 msg = current_message ();
6606 Vmessage_stack = Fcons (msg, Vmessage_stack);
6607 return STRINGP (msg);
6608 }
6609
6610
6611 /* Handler for record_unwind_protect calling pop_message. */
6612
6613 Lisp_Object
6614 push_message_unwind (dummy)
6615 Lisp_Object dummy;
6616 {
6617 pop_message ();
6618 return Qnil;
6619 }
6620
6621
6622 /* Restore message display from the top of Vmessage_stack. */
6623
6624 void
6625 restore_message ()
6626 {
6627 Lisp_Object msg;
6628
6629 xassert (CONSP (Vmessage_stack));
6630 msg = XCAR (Vmessage_stack);
6631 if (STRINGP (msg))
6632 message3_nolog (msg, STRING_BYTES (XSTRING (msg)), STRING_MULTIBYTE (msg));
6633 else
6634 message3_nolog (msg, 0, 0);
6635 }
6636
6637
6638 /* Pop the top-most entry off Vmessage_stack. */
6639
6640 void
6641 pop_message ()
6642 {
6643 xassert (CONSP (Vmessage_stack));
6644 Vmessage_stack = XCDR (Vmessage_stack);
6645 }
6646
6647
6648 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
6649 exits. If the stack is not empty, we have a missing pop_message
6650 somewhere. */
6651
6652 void
6653 check_message_stack ()
6654 {
6655 if (!NILP (Vmessage_stack))
6656 abort ();
6657 }
6658
6659
6660 /* Truncate to NCHARS what will be displayed in the echo area the next
6661 time we display it---but don't redisplay it now. */
6662
6663 void
6664 truncate_echo_area (nchars)
6665 int nchars;
6666 {
6667 if (nchars == 0)
6668 echo_area_buffer[0] = Qnil;
6669 /* A null message buffer means that the frame hasn't really been
6670 initialized yet. Error messages get reported properly by
6671 cmd_error, so this must be just an informative message; toss it. */
6672 else if (!noninteractive
6673 && INTERACTIVE
6674 && !NILP (echo_area_buffer[0]))
6675 {
6676 struct frame *sf = SELECTED_FRAME ();
6677 if (FRAME_MESSAGE_BUF (sf))
6678 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
6679 }
6680 }
6681
6682
6683 /* Helper function for truncate_echo_area. Truncate the current
6684 message to at most NCHARS characters. */
6685
6686 static int
6687 truncate_message_1 (nchars, a2, a3, a4)
6688 EMACS_INT nchars;
6689 Lisp_Object a2;
6690 EMACS_INT a3, a4;
6691 {
6692 if (BEG + nchars < Z)
6693 del_range (BEG + nchars, Z);
6694 if (Z == BEG)
6695 echo_area_buffer[0] = Qnil;
6696 return 0;
6697 }
6698
6699
6700 /* Set the current message to a substring of S or STRING.
6701
6702 If STRING is a Lisp string, set the message to the first NBYTES
6703 bytes from STRING. NBYTES zero means use the whole string. If
6704 STRING is multibyte, the message will be displayed multibyte.
6705
6706 If S is not null, set the message to the first LEN bytes of S. LEN
6707 zero means use the whole string. MULTIBYTE_P non-zero means S is
6708 multibyte. Display the message multibyte in that case. */
6709
6710 void
6711 set_message (s, string, nbytes, multibyte_p)
6712 char *s;
6713 Lisp_Object string;
6714 int nbytes;
6715 {
6716 message_enable_multibyte
6717 = ((s && multibyte_p)
6718 || (STRINGP (string) && STRING_MULTIBYTE (string)));
6719
6720 with_echo_area_buffer (0, -1, set_message_1,
6721 (EMACS_INT) s, string, nbytes, multibyte_p);
6722 message_buf_print = 0;
6723 help_echo_showing_p = 0;
6724 }
6725
6726
6727 /* Helper function for set_message. Arguments have the same meaning
6728 as there, with A1 corresponding to S and A2 corresponding to STRING
6729 This function is called with the echo area buffer being
6730 current. */
6731
6732 static int
6733 set_message_1 (a1, a2, nbytes, multibyte_p)
6734 EMACS_INT a1;
6735 Lisp_Object a2;
6736 EMACS_INT nbytes, multibyte_p;
6737 {
6738 char *s = (char *) a1;
6739 Lisp_Object string = a2;
6740
6741 xassert (BEG == Z);
6742
6743 /* Change multibyteness of the echo buffer appropriately. */
6744 if (message_enable_multibyte
6745 != !NILP (current_buffer->enable_multibyte_characters))
6746 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
6747
6748 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
6749
6750 /* Insert new message at BEG. */
6751 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6752
6753 if (STRINGP (string))
6754 {
6755 int nchars;
6756
6757 if (nbytes == 0)
6758 nbytes = XSTRING (string)->size_byte;
6759 nchars = string_byte_to_char (string, nbytes);
6760
6761 /* This function takes care of single/multibyte conversion. We
6762 just have to ensure that the echo area buffer has the right
6763 setting of enable_multibyte_characters. */
6764 insert_from_string (string, 0, 0, nchars, nbytes, 1);
6765 }
6766 else if (s)
6767 {
6768 if (nbytes == 0)
6769 nbytes = strlen (s);
6770
6771 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
6772 {
6773 /* Convert from multi-byte to single-byte. */
6774 int i, c, n;
6775 unsigned char work[1];
6776
6777 /* Convert a multibyte string to single-byte. */
6778 for (i = 0; i < nbytes; i += n)
6779 {
6780 c = string_char_and_length (s + i, nbytes - i, &n);
6781 work[0] = (SINGLE_BYTE_CHAR_P (c)
6782 ? c
6783 : multibyte_char_to_unibyte (c, Qnil));
6784 insert_1_both (work, 1, 1, 1, 0, 0);
6785 }
6786 }
6787 else if (!multibyte_p
6788 && !NILP (current_buffer->enable_multibyte_characters))
6789 {
6790 /* Convert from single-byte to multi-byte. */
6791 int i, c, n;
6792 unsigned char *msg = (unsigned char *) s;
6793 unsigned char str[MAX_MULTIBYTE_LENGTH];
6794
6795 /* Convert a single-byte string to multibyte. */
6796 for (i = 0; i < nbytes; i++)
6797 {
6798 c = unibyte_char_to_multibyte (msg[i]);
6799 n = CHAR_STRING (c, str);
6800 insert_1_both (str, 1, n, 1, 0, 0);
6801 }
6802 }
6803 else
6804 insert_1 (s, nbytes, 1, 0, 0);
6805 }
6806
6807 return 0;
6808 }
6809
6810
6811 /* Clear messages. CURRENT_P non-zero means clear the current
6812 message. LAST_DISPLAYED_P non-zero means clear the message
6813 last displayed. */
6814
6815 void
6816 clear_message (current_p, last_displayed_p)
6817 int current_p, last_displayed_p;
6818 {
6819 if (current_p)
6820 echo_area_buffer[0] = Qnil;
6821
6822 if (last_displayed_p)
6823 echo_area_buffer[1] = Qnil;
6824
6825 message_buf_print = 0;
6826 }
6827
6828 /* Clear garbaged frames.
6829
6830 This function is used where the old redisplay called
6831 redraw_garbaged_frames which in turn called redraw_frame which in
6832 turn called clear_frame. The call to clear_frame was a source of
6833 flickering. I believe a clear_frame is not necessary. It should
6834 suffice in the new redisplay to invalidate all current matrices,
6835 and ensure a complete redisplay of all windows. */
6836
6837 static void
6838 clear_garbaged_frames ()
6839 {
6840 if (frame_garbaged)
6841 {
6842 Lisp_Object tail, frame;
6843
6844 FOR_EACH_FRAME (tail, frame)
6845 {
6846 struct frame *f = XFRAME (frame);
6847
6848 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
6849 {
6850 clear_current_matrices (f);
6851 f->garbaged = 0;
6852 }
6853 }
6854
6855 frame_garbaged = 0;
6856 ++windows_or_buffers_changed;
6857 }
6858 }
6859
6860
6861 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
6862 is non-zero update selected_frame. Value is non-zero if the
6863 mini-windows height has been changed. */
6864
6865 static int
6866 echo_area_display (update_frame_p)
6867 int update_frame_p;
6868 {
6869 Lisp_Object mini_window;
6870 struct window *w;
6871 struct frame *f;
6872 int window_height_changed_p = 0;
6873 struct frame *sf = SELECTED_FRAME ();
6874
6875 mini_window = FRAME_MINIBUF_WINDOW (sf);
6876 w = XWINDOW (mini_window);
6877 f = XFRAME (WINDOW_FRAME (w));
6878
6879 /* Don't display if frame is invisible or not yet initialized. */
6880 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
6881 return 0;
6882
6883 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
6884 #ifndef macintosh
6885 #ifdef HAVE_WINDOW_SYSTEM
6886 /* When Emacs starts, selected_frame may be a visible terminal
6887 frame, even if we run under a window system. If we let this
6888 through, a message would be displayed on the terminal. */
6889 if (EQ (selected_frame, Vterminal_frame)
6890 && !NILP (Vwindow_system))
6891 return 0;
6892 #endif /* HAVE_WINDOW_SYSTEM */
6893 #endif
6894
6895 /* Redraw garbaged frames. */
6896 if (frame_garbaged)
6897 clear_garbaged_frames ();
6898
6899 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
6900 {
6901 echo_area_window = mini_window;
6902 window_height_changed_p = display_echo_area (w);
6903 w->must_be_updated_p = 1;
6904
6905 /* Update the display, unless called from redisplay_internal.
6906 Also don't update the screen during redisplay itself. The
6907 update will happen at the end of redisplay, and an update
6908 here could cause confusion. */
6909 if (update_frame_p && !redisplaying_p)
6910 {
6911 int n = 0;
6912
6913 /* If the display update has been interrupted by pending
6914 input, update mode lines in the frame. Due to the
6915 pending input, it might have been that redisplay hasn't
6916 been called, so that mode lines above the echo area are
6917 garbaged. This looks odd, so we prevent it here. */
6918 if (!display_completed)
6919 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
6920
6921 if (window_height_changed_p
6922 /* Don't do this if Emacs is shutting down. Redisplay
6923 needs to run hooks. */
6924 && !NILP (Vrun_hooks))
6925 {
6926 /* Must update other windows. Likewise as in other
6927 cases, don't let this update be interrupted by
6928 pending input. */
6929 int count = BINDING_STACK_SIZE ();
6930 specbind (Qredisplay_dont_pause, Qt);
6931 windows_or_buffers_changed = 1;
6932 redisplay_internal (0);
6933 unbind_to (count, Qnil);
6934 }
6935 else if (FRAME_WINDOW_P (f) && n == 0)
6936 {
6937 /* Window configuration is the same as before.
6938 Can do with a display update of the echo area,
6939 unless we displayed some mode lines. */
6940 update_single_window (w, 1);
6941 rif->flush_display (f);
6942 }
6943 else
6944 update_frame (f, 1, 1);
6945
6946 /* If cursor is in the echo area, make sure that the next
6947 redisplay displays the minibuffer, so that the cursor will
6948 be replaced with what the minibuffer wants. */
6949 if (cursor_in_echo_area)
6950 ++windows_or_buffers_changed;
6951 }
6952 }
6953 else if (!EQ (mini_window, selected_window))
6954 windows_or_buffers_changed++;
6955
6956 /* Last displayed message is now the current message. */
6957 echo_area_buffer[1] = echo_area_buffer[0];
6958
6959 /* Prevent redisplay optimization in redisplay_internal by resetting
6960 this_line_start_pos. This is done because the mini-buffer now
6961 displays the message instead of its buffer text. */
6962 if (EQ (mini_window, selected_window))
6963 CHARPOS (this_line_start_pos) = 0;
6964
6965 return window_height_changed_p;
6966 }
6967
6968
6969 \f
6970 /***********************************************************************
6971 Frame Titles
6972 ***********************************************************************/
6973
6974
6975 #ifdef HAVE_WINDOW_SYSTEM
6976
6977 /* A buffer for constructing frame titles in it; allocated from the
6978 heap in init_xdisp and resized as needed in store_frame_title_char. */
6979
6980 static char *frame_title_buf;
6981
6982 /* The buffer's end, and a current output position in it. */
6983
6984 static char *frame_title_buf_end;
6985 static char *frame_title_ptr;
6986
6987
6988 /* Store a single character C for the frame title in frame_title_buf.
6989 Re-allocate frame_title_buf if necessary. */
6990
6991 static void
6992 store_frame_title_char (c)
6993 char c;
6994 {
6995 /* If output position has reached the end of the allocated buffer,
6996 double the buffer's size. */
6997 if (frame_title_ptr == frame_title_buf_end)
6998 {
6999 int len = frame_title_ptr - frame_title_buf;
7000 int new_size = 2 * len * sizeof *frame_title_buf;
7001 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
7002 frame_title_buf_end = frame_title_buf + new_size;
7003 frame_title_ptr = frame_title_buf + len;
7004 }
7005
7006 *frame_title_ptr++ = c;
7007 }
7008
7009
7010 /* Store part of a frame title in frame_title_buf, beginning at
7011 frame_title_ptr. STR is the string to store. Do not copy
7012 characters that yield more columns than PRECISION; PRECISION <= 0
7013 means copy the whole string. Pad with spaces until FIELD_WIDTH
7014 number of characters have been copied; FIELD_WIDTH <= 0 means don't
7015 pad. Called from display_mode_element when it is used to build a
7016 frame title. */
7017
7018 static int
7019 store_frame_title (str, field_width, precision)
7020 unsigned char *str;
7021 int field_width, precision;
7022 {
7023 int n = 0;
7024 int dummy, nbytes, width;
7025
7026 /* Copy at most PRECISION chars from STR. */
7027 nbytes = strlen (str);
7028 n+= c_string_width (str, nbytes, precision, &dummy, &nbytes);
7029 while (nbytes--)
7030 store_frame_title_char (*str++);
7031
7032 /* Fill up with spaces until FIELD_WIDTH reached. */
7033 while (field_width > 0
7034 && n < field_width)
7035 {
7036 store_frame_title_char (' ');
7037 ++n;
7038 }
7039
7040 return n;
7041 }
7042
7043
7044 /* Set the title of FRAME, if it has changed. The title format is
7045 Vicon_title_format if FRAME is iconified, otherwise it is
7046 frame_title_format. */
7047
7048 static void
7049 x_consider_frame_title (frame)
7050 Lisp_Object frame;
7051 {
7052 struct frame *f = XFRAME (frame);
7053
7054 if (FRAME_WINDOW_P (f)
7055 || FRAME_MINIBUF_ONLY_P (f)
7056 || f->explicit_name)
7057 {
7058 /* Do we have more than one visible frame on this X display? */
7059 Lisp_Object tail;
7060 Lisp_Object fmt;
7061 struct buffer *obuf;
7062 int len;
7063 struct it it;
7064
7065 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
7066 {
7067 struct frame *tf = XFRAME (XCAR (tail));
7068
7069 if (tf != f
7070 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
7071 && !FRAME_MINIBUF_ONLY_P (tf)
7072 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
7073 break;
7074 }
7075
7076 /* Set global variable indicating that multiple frames exist. */
7077 multiple_frames = CONSP (tail);
7078
7079 /* Switch to the buffer of selected window of the frame. Set up
7080 frame_title_ptr so that display_mode_element will output into it;
7081 then display the title. */
7082 obuf = current_buffer;
7083 Fset_buffer (XWINDOW (f->selected_window)->buffer);
7084 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
7085 frame_title_ptr = frame_title_buf;
7086 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
7087 NULL, DEFAULT_FACE_ID);
7088 display_mode_element (&it, 0, -1, -1, fmt);
7089 len = frame_title_ptr - frame_title_buf;
7090 frame_title_ptr = NULL;
7091 set_buffer_internal (obuf);
7092
7093 /* Set the title only if it's changed. This avoids consing in
7094 the common case where it hasn't. (If it turns out that we've
7095 already wasted too much time by walking through the list with
7096 display_mode_element, then we might need to optimize at a
7097 higher level than this.) */
7098 if (! STRINGP (f->name)
7099 || STRING_BYTES (XSTRING (f->name)) != len
7100 || bcmp (frame_title_buf, XSTRING (f->name)->data, len) != 0)
7101 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
7102 }
7103 }
7104
7105 #else /* not HAVE_WINDOW_SYSTEM */
7106
7107 #define frame_title_ptr ((char *)0)
7108 #define store_frame_title(str, mincol, maxcol) 0
7109
7110 #endif /* not HAVE_WINDOW_SYSTEM */
7111
7112
7113
7114 \f
7115 /***********************************************************************
7116 Menu Bars
7117 ***********************************************************************/
7118
7119
7120 /* Prepare for redisplay by updating menu-bar item lists when
7121 appropriate. This can call eval. */
7122
7123 void
7124 prepare_menu_bars ()
7125 {
7126 int all_windows;
7127 struct gcpro gcpro1, gcpro2;
7128 struct frame *f;
7129 Lisp_Object tooltip_frame;
7130
7131 #ifdef HAVE_X_WINDOWS
7132 tooltip_frame = tip_frame;
7133 #else
7134 tooltip_frame = Qnil;
7135 #endif
7136
7137 /* Update all frame titles based on their buffer names, etc. We do
7138 this before the menu bars so that the buffer-menu will show the
7139 up-to-date frame titles. */
7140 #ifdef HAVE_WINDOW_SYSTEM
7141 if (windows_or_buffers_changed || update_mode_lines)
7142 {
7143 Lisp_Object tail, frame;
7144
7145 FOR_EACH_FRAME (tail, frame)
7146 {
7147 f = XFRAME (frame);
7148 if (!EQ (frame, tooltip_frame)
7149 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
7150 x_consider_frame_title (frame);
7151 }
7152 }
7153 #endif /* HAVE_WINDOW_SYSTEM */
7154
7155 /* Update the menu bar item lists, if appropriate. This has to be
7156 done before any actual redisplay or generation of display lines. */
7157 all_windows = (update_mode_lines
7158 || buffer_shared > 1
7159 || windows_or_buffers_changed);
7160 if (all_windows)
7161 {
7162 Lisp_Object tail, frame;
7163 int count = BINDING_STACK_SIZE ();
7164
7165 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7166
7167 FOR_EACH_FRAME (tail, frame)
7168 {
7169 f = XFRAME (frame);
7170
7171 /* Ignore tooltip frame. */
7172 if (EQ (frame, tooltip_frame))
7173 continue;
7174
7175 /* If a window on this frame changed size, report that to
7176 the user and clear the size-change flag. */
7177 if (FRAME_WINDOW_SIZES_CHANGED (f))
7178 {
7179 Lisp_Object functions;
7180
7181 /* Clear flag first in case we get an error below. */
7182 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
7183 functions = Vwindow_size_change_functions;
7184 GCPRO2 (tail, functions);
7185
7186 while (CONSP (functions))
7187 {
7188 call1 (XCAR (functions), frame);
7189 functions = XCDR (functions);
7190 }
7191 UNGCPRO;
7192 }
7193
7194 GCPRO1 (tail);
7195 update_menu_bar (f, 0);
7196 #ifdef HAVE_WINDOW_SYSTEM
7197 update_tool_bar (f, 0);
7198 #endif
7199 UNGCPRO;
7200 }
7201
7202 unbind_to (count, Qnil);
7203 }
7204 else
7205 {
7206 struct frame *sf = SELECTED_FRAME ();
7207 update_menu_bar (sf, 1);
7208 #ifdef HAVE_WINDOW_SYSTEM
7209 update_tool_bar (sf, 1);
7210 #endif
7211 }
7212
7213 /* Motif needs this. See comment in xmenu.c. Turn it off when
7214 pending_menu_activation is not defined. */
7215 #ifdef USE_X_TOOLKIT
7216 pending_menu_activation = 0;
7217 #endif
7218 }
7219
7220
7221 /* Update the menu bar item list for frame F. This has to be done
7222 before we start to fill in any display lines, because it can call
7223 eval.
7224
7225 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
7226
7227 static void
7228 update_menu_bar (f, save_match_data)
7229 struct frame *f;
7230 int save_match_data;
7231 {
7232 Lisp_Object window;
7233 register struct window *w;
7234
7235 /* If called recursively during a menu update, do nothing. This can
7236 happen when, for instance, an activate-menubar-hook causes a
7237 redisplay. */
7238 if (inhibit_menubar_update)
7239 return;
7240
7241 window = FRAME_SELECTED_WINDOW (f);
7242 w = XWINDOW (window);
7243
7244 if (update_mode_lines)
7245 w->update_mode_line = Qt;
7246
7247 if (FRAME_WINDOW_P (f)
7248 ?
7249 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
7250 FRAME_EXTERNAL_MENU_BAR (f)
7251 #else
7252 FRAME_MENU_BAR_LINES (f) > 0
7253 #endif
7254 : FRAME_MENU_BAR_LINES (f) > 0)
7255 {
7256 /* If the user has switched buffers or windows, we need to
7257 recompute to reflect the new bindings. But we'll
7258 recompute when update_mode_lines is set too; that means
7259 that people can use force-mode-line-update to request
7260 that the menu bar be recomputed. The adverse effect on
7261 the rest of the redisplay algorithm is about the same as
7262 windows_or_buffers_changed anyway. */
7263 if (windows_or_buffers_changed
7264 || !NILP (w->update_mode_line)
7265 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7266 < BUF_MODIFF (XBUFFER (w->buffer)))
7267 != !NILP (w->last_had_star))
7268 || ((!NILP (Vtransient_mark_mode)
7269 && !NILP (XBUFFER (w->buffer)->mark_active))
7270 != !NILP (w->region_showing)))
7271 {
7272 struct buffer *prev = current_buffer;
7273 int count = BINDING_STACK_SIZE ();
7274
7275 specbind (Qinhibit_menubar_update, Qt);
7276
7277 set_buffer_internal_1 (XBUFFER (w->buffer));
7278 if (save_match_data)
7279 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7280 if (NILP (Voverriding_local_map_menu_flag))
7281 {
7282 specbind (Qoverriding_terminal_local_map, Qnil);
7283 specbind (Qoverriding_local_map, Qnil);
7284 }
7285
7286 /* Run the Lucid hook. */
7287 safe_run_hooks (Qactivate_menubar_hook);
7288
7289 /* If it has changed current-menubar from previous value,
7290 really recompute the menu-bar from the value. */
7291 if (! NILP (Vlucid_menu_bar_dirty_flag))
7292 call0 (Qrecompute_lucid_menubar);
7293
7294 safe_run_hooks (Qmenu_bar_update_hook);
7295 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
7296
7297 /* Redisplay the menu bar in case we changed it. */
7298 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
7299 if (FRAME_WINDOW_P (f)
7300 #if defined (macintosh)
7301 /* All frames on Mac OS share the same menubar. So only the
7302 selected frame should be allowed to set it. */
7303 && f == SELECTED_FRAME ()
7304 #endif
7305 )
7306 set_frame_menubar (f, 0, 0);
7307 else
7308 /* On a terminal screen, the menu bar is an ordinary screen
7309 line, and this makes it get updated. */
7310 w->update_mode_line = Qt;
7311 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7312 /* In the non-toolkit version, the menu bar is an ordinary screen
7313 line, and this makes it get updated. */
7314 w->update_mode_line = Qt;
7315 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7316
7317 unbind_to (count, Qnil);
7318 set_buffer_internal_1 (prev);
7319 }
7320 }
7321 }
7322
7323
7324 \f
7325 /***********************************************************************
7326 Tool-bars
7327 ***********************************************************************/
7328
7329 #ifdef HAVE_WINDOW_SYSTEM
7330
7331 /* Update the tool-bar item list for frame F. This has to be done
7332 before we start to fill in any display lines. Called from
7333 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
7334 and restore it here. */
7335
7336 static void
7337 update_tool_bar (f, save_match_data)
7338 struct frame *f;
7339 int save_match_data;
7340 {
7341 if (WINDOWP (f->tool_bar_window)
7342 && XFASTINT (XWINDOW (f->tool_bar_window)->height) > 0)
7343 {
7344 Lisp_Object window;
7345 struct window *w;
7346
7347 window = FRAME_SELECTED_WINDOW (f);
7348 w = XWINDOW (window);
7349
7350 /* If the user has switched buffers or windows, we need to
7351 recompute to reflect the new bindings. But we'll
7352 recompute when update_mode_lines is set too; that means
7353 that people can use force-mode-line-update to request
7354 that the menu bar be recomputed. The adverse effect on
7355 the rest of the redisplay algorithm is about the same as
7356 windows_or_buffers_changed anyway. */
7357 if (windows_or_buffers_changed
7358 || !NILP (w->update_mode_line)
7359 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7360 < BUF_MODIFF (XBUFFER (w->buffer)))
7361 != !NILP (w->last_had_star))
7362 || ((!NILP (Vtransient_mark_mode)
7363 && !NILP (XBUFFER (w->buffer)->mark_active))
7364 != !NILP (w->region_showing)))
7365 {
7366 struct buffer *prev = current_buffer;
7367 int count = BINDING_STACK_SIZE ();
7368
7369 /* Set current_buffer to the buffer of the selected
7370 window of the frame, so that we get the right local
7371 keymaps. */
7372 set_buffer_internal_1 (XBUFFER (w->buffer));
7373
7374 /* Save match data, if we must. */
7375 if (save_match_data)
7376 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7377
7378 /* Make sure that we don't accidentally use bogus keymaps. */
7379 if (NILP (Voverriding_local_map_menu_flag))
7380 {
7381 specbind (Qoverriding_terminal_local_map, Qnil);
7382 specbind (Qoverriding_local_map, Qnil);
7383 }
7384
7385 /* Build desired tool-bar items from keymaps. */
7386 f->tool_bar_items
7387 = tool_bar_items (f->tool_bar_items, &f->n_tool_bar_items);
7388
7389 /* Redisplay the tool-bar in case we changed it. */
7390 w->update_mode_line = Qt;
7391
7392 unbind_to (count, Qnil);
7393 set_buffer_internal_1 (prev);
7394 }
7395 }
7396 }
7397
7398
7399 /* Set F->desired_tool_bar_string to a Lisp string representing frame
7400 F's desired tool-bar contents. F->tool_bar_items must have
7401 been set up previously by calling prepare_menu_bars. */
7402
7403 static void
7404 build_desired_tool_bar_string (f)
7405 struct frame *f;
7406 {
7407 int i, size, size_needed;
7408 struct gcpro gcpro1, gcpro2, gcpro3;
7409 Lisp_Object image, plist, props;
7410
7411 image = plist = props = Qnil;
7412 GCPRO3 (image, plist, props);
7413
7414 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
7415 Otherwise, make a new string. */
7416
7417 /* The size of the string we might be able to reuse. */
7418 size = (STRINGP (f->desired_tool_bar_string)
7419 ? XSTRING (f->desired_tool_bar_string)->size
7420 : 0);
7421
7422 /* We need one space in the string for each image. */
7423 size_needed = f->n_tool_bar_items;
7424
7425 /* Reuse f->desired_tool_bar_string, if possible. */
7426 if (size < size_needed || NILP (f->desired_tool_bar_string))
7427 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
7428 make_number (' '));
7429 else
7430 {
7431 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
7432 Fremove_text_properties (make_number (0), make_number (size),
7433 props, f->desired_tool_bar_string);
7434 }
7435
7436 /* Put a `display' property on the string for the images to display,
7437 put a `menu_item' property on tool-bar items with a value that
7438 is the index of the item in F's tool-bar item vector. */
7439 for (i = 0; i < f->n_tool_bar_items; ++i)
7440 {
7441 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
7442
7443 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
7444 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
7445 int hmargin, vmargin, relief, idx, end;
7446 extern Lisp_Object QCrelief, QCmargin, QCconversion, Qimage;
7447 extern Lisp_Object Qlaplace;
7448
7449 /* If image is a vector, choose the image according to the
7450 button state. */
7451 image = PROP (TOOL_BAR_ITEM_IMAGES);
7452 if (VECTORP (image))
7453 {
7454 if (enabled_p)
7455 idx = (selected_p
7456 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
7457 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
7458 else
7459 idx = (selected_p
7460 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
7461 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
7462
7463 xassert (ASIZE (image) >= idx);
7464 image = AREF (image, idx);
7465 }
7466 else
7467 idx = -1;
7468
7469 /* Ignore invalid image specifications. */
7470 if (!valid_image_p (image))
7471 continue;
7472
7473 /* Display the tool-bar button pressed, or depressed. */
7474 plist = Fcopy_sequence (XCDR (image));
7475
7476 /* Compute margin and relief to draw. */
7477 relief = (tool_bar_button_relief > 0
7478 ? tool_bar_button_relief
7479 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
7480 hmargin = vmargin = relief;
7481
7482 if (INTEGERP (Vtool_bar_button_margin)
7483 && XINT (Vtool_bar_button_margin) > 0)
7484 {
7485 hmargin += XFASTINT (Vtool_bar_button_margin);
7486 vmargin += XFASTINT (Vtool_bar_button_margin);
7487 }
7488 else if (CONSP (Vtool_bar_button_margin))
7489 {
7490 if (INTEGERP (XCAR (Vtool_bar_button_margin))
7491 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
7492 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
7493
7494 if (INTEGERP (XCDR (Vtool_bar_button_margin))
7495 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
7496 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
7497 }
7498
7499 if (auto_raise_tool_bar_buttons_p)
7500 {
7501 /* Add a `:relief' property to the image spec if the item is
7502 selected. */
7503 if (selected_p)
7504 {
7505 plist = Fplist_put (plist, QCrelief, make_number (-relief));
7506 hmargin -= relief;
7507 vmargin -= relief;
7508 }
7509 }
7510 else
7511 {
7512 /* If image is selected, display it pressed, i.e. with a
7513 negative relief. If it's not selected, display it with a
7514 raised relief. */
7515 plist = Fplist_put (plist, QCrelief,
7516 (selected_p
7517 ? make_number (-relief)
7518 : make_number (relief)));
7519 hmargin -= relief;
7520 vmargin -= relief;
7521 }
7522
7523 /* Put a margin around the image. */
7524 if (hmargin || vmargin)
7525 {
7526 if (hmargin == vmargin)
7527 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
7528 else
7529 plist = Fplist_put (plist, QCmargin,
7530 Fcons (make_number (hmargin),
7531 make_number (vmargin)));
7532 }
7533
7534 /* If button is not enabled, and we don't have special images
7535 for the disabled state, make the image appear disabled by
7536 applying an appropriate algorithm to it. */
7537 if (!enabled_p && idx < 0)
7538 plist = Fplist_put (plist, QCconversion, Qdisabled);
7539
7540 /* Put a `display' text property on the string for the image to
7541 display. Put a `menu-item' property on the string that gives
7542 the start of this item's properties in the tool-bar items
7543 vector. */
7544 image = Fcons (Qimage, plist);
7545 props = list4 (Qdisplay, image,
7546 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
7547
7548 /* Let the last image hide all remaining spaces in the tool bar
7549 string. The string can be longer than needed when we reuse a
7550 previous string. */
7551 if (i + 1 == f->n_tool_bar_items)
7552 end = XSTRING (f->desired_tool_bar_string)->size;
7553 else
7554 end = i + 1;
7555 Fadd_text_properties (make_number (i), make_number (end),
7556 props, f->desired_tool_bar_string);
7557 #undef PROP
7558 }
7559
7560 UNGCPRO;
7561 }
7562
7563
7564 /* Display one line of the tool-bar of frame IT->f. */
7565
7566 static void
7567 display_tool_bar_line (it)
7568 struct it *it;
7569 {
7570 struct glyph_row *row = it->glyph_row;
7571 int max_x = it->last_visible_x;
7572 struct glyph *last;
7573
7574 prepare_desired_row (row);
7575 row->y = it->current_y;
7576
7577 /* Note that this isn't made use of if the face hasn't a box,
7578 so there's no need to check the face here. */
7579 it->start_of_box_run_p = 1;
7580
7581 while (it->current_x < max_x)
7582 {
7583 int x_before, x, n_glyphs_before, i, nglyphs;
7584
7585 /* Get the next display element. */
7586 if (!get_next_display_element (it))
7587 break;
7588
7589 /* Produce glyphs. */
7590 x_before = it->current_x;
7591 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
7592 PRODUCE_GLYPHS (it);
7593
7594 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
7595 i = 0;
7596 x = x_before;
7597 while (i < nglyphs)
7598 {
7599 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
7600
7601 if (x + glyph->pixel_width > max_x)
7602 {
7603 /* Glyph doesn't fit on line. */
7604 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
7605 it->current_x = x;
7606 goto out;
7607 }
7608
7609 ++it->hpos;
7610 x += glyph->pixel_width;
7611 ++i;
7612 }
7613
7614 /* Stop at line ends. */
7615 if (ITERATOR_AT_END_OF_LINE_P (it))
7616 break;
7617
7618 set_iterator_to_next (it, 1);
7619 }
7620
7621 out:;
7622
7623 row->displays_text_p = row->used[TEXT_AREA] != 0;
7624 extend_face_to_end_of_line (it);
7625 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
7626 last->right_box_line_p = 1;
7627 if (last == row->glyphs[TEXT_AREA])
7628 last->left_box_line_p = 1;
7629 compute_line_metrics (it);
7630
7631 /* If line is empty, make it occupy the rest of the tool-bar. */
7632 if (!row->displays_text_p)
7633 {
7634 row->height = row->phys_height = it->last_visible_y - row->y;
7635 row->ascent = row->phys_ascent = 0;
7636 }
7637
7638 row->full_width_p = 1;
7639 row->continued_p = 0;
7640 row->truncated_on_left_p = 0;
7641 row->truncated_on_right_p = 0;
7642
7643 it->current_x = it->hpos = 0;
7644 it->current_y += row->height;
7645 ++it->vpos;
7646 ++it->glyph_row;
7647 }
7648
7649
7650 /* Value is the number of screen lines needed to make all tool-bar
7651 items of frame F visible. */
7652
7653 static int
7654 tool_bar_lines_needed (f)
7655 struct frame *f;
7656 {
7657 struct window *w = XWINDOW (f->tool_bar_window);
7658 struct it it;
7659
7660 /* Initialize an iterator for iteration over
7661 F->desired_tool_bar_string in the tool-bar window of frame F. */
7662 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7663 it.first_visible_x = 0;
7664 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7665 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7666
7667 while (!ITERATOR_AT_END_P (&it))
7668 {
7669 it.glyph_row = w->desired_matrix->rows;
7670 clear_glyph_row (it.glyph_row);
7671 display_tool_bar_line (&it);
7672 }
7673
7674 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
7675 }
7676
7677
7678 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
7679 0, 1, 0,
7680 "Return the number of lines occupied by the tool bar of FRAME.")
7681 (frame)
7682 Lisp_Object frame;
7683 {
7684 struct frame *f;
7685 struct window *w;
7686 int nlines = 0;
7687
7688 if (NILP (frame))
7689 frame = selected_frame;
7690 else
7691 CHECK_FRAME (frame, 0);
7692 f = XFRAME (frame);
7693
7694 if (WINDOWP (f->tool_bar_window)
7695 || (w = XWINDOW (f->tool_bar_window),
7696 XFASTINT (w->height) > 0))
7697 {
7698 update_tool_bar (f, 1);
7699 if (f->n_tool_bar_items)
7700 {
7701 build_desired_tool_bar_string (f);
7702 nlines = tool_bar_lines_needed (f);
7703 }
7704 }
7705
7706 return make_number (nlines);
7707 }
7708
7709
7710 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
7711 height should be changed. */
7712
7713 static int
7714 redisplay_tool_bar (f)
7715 struct frame *f;
7716 {
7717 struct window *w;
7718 struct it it;
7719 struct glyph_row *row;
7720 int change_height_p = 0;
7721
7722 /* If frame hasn't a tool-bar window or if it is zero-height, don't
7723 do anything. This means you must start with tool-bar-lines
7724 non-zero to get the auto-sizing effect. Or in other words, you
7725 can turn off tool-bars by specifying tool-bar-lines zero. */
7726 if (!WINDOWP (f->tool_bar_window)
7727 || (w = XWINDOW (f->tool_bar_window),
7728 XFASTINT (w->height) == 0))
7729 return 0;
7730
7731 /* Set up an iterator for the tool-bar window. */
7732 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7733 it.first_visible_x = 0;
7734 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7735 row = it.glyph_row;
7736
7737 /* Build a string that represents the contents of the tool-bar. */
7738 build_desired_tool_bar_string (f);
7739 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7740
7741 /* Display as many lines as needed to display all tool-bar items. */
7742 while (it.current_y < it.last_visible_y)
7743 display_tool_bar_line (&it);
7744
7745 /* It doesn't make much sense to try scrolling in the tool-bar
7746 window, so don't do it. */
7747 w->desired_matrix->no_scrolling_p = 1;
7748 w->must_be_updated_p = 1;
7749
7750 if (auto_resize_tool_bars_p)
7751 {
7752 int nlines;
7753
7754 /* If we couldn't display everything, change the tool-bar's
7755 height. */
7756 if (IT_STRING_CHARPOS (it) < it.end_charpos)
7757 change_height_p = 1;
7758
7759 /* If there are blank lines at the end, except for a partially
7760 visible blank line at the end that is smaller than
7761 CANON_Y_UNIT, change the tool-bar's height. */
7762 row = it.glyph_row - 1;
7763 if (!row->displays_text_p
7764 && row->height >= CANON_Y_UNIT (f))
7765 change_height_p = 1;
7766
7767 /* If row displays tool-bar items, but is partially visible,
7768 change the tool-bar's height. */
7769 if (row->displays_text_p
7770 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
7771 change_height_p = 1;
7772
7773 /* Resize windows as needed by changing the `tool-bar-lines'
7774 frame parameter. */
7775 if (change_height_p
7776 && (nlines = tool_bar_lines_needed (f),
7777 nlines != XFASTINT (w->height)))
7778 {
7779 extern Lisp_Object Qtool_bar_lines;
7780 Lisp_Object frame;
7781 int old_height = XFASTINT (w->height);
7782
7783 XSETFRAME (frame, f);
7784 clear_glyph_matrix (w->desired_matrix);
7785 Fmodify_frame_parameters (frame,
7786 Fcons (Fcons (Qtool_bar_lines,
7787 make_number (nlines)),
7788 Qnil));
7789 if (XFASTINT (w->height) != old_height)
7790 fonts_changed_p = 1;
7791 }
7792 }
7793
7794 return change_height_p;
7795 }
7796
7797
7798 /* Get information about the tool-bar item which is displayed in GLYPH
7799 on frame F. Return in *PROP_IDX the index where tool-bar item
7800 properties start in F->tool_bar_items. Value is zero if
7801 GLYPH doesn't display a tool-bar item. */
7802
7803 int
7804 tool_bar_item_info (f, glyph, prop_idx)
7805 struct frame *f;
7806 struct glyph *glyph;
7807 int *prop_idx;
7808 {
7809 Lisp_Object prop;
7810 int success_p;
7811
7812 /* Get the text property `menu-item' at pos. The value of that
7813 property is the start index of this item's properties in
7814 F->tool_bar_items. */
7815 prop = Fget_text_property (make_number (glyph->charpos),
7816 Qmenu_item, f->current_tool_bar_string);
7817 if (INTEGERP (prop))
7818 {
7819 *prop_idx = XINT (prop);
7820 success_p = 1;
7821 }
7822 else
7823 success_p = 0;
7824
7825 return success_p;
7826 }
7827
7828 #endif /* HAVE_WINDOW_SYSTEM */
7829
7830
7831 \f
7832 /************************************************************************
7833 Horizontal scrolling
7834 ************************************************************************/
7835
7836 static int hscroll_window_tree P_ ((Lisp_Object));
7837 static int hscroll_windows P_ ((Lisp_Object));
7838
7839 /* For all leaf windows in the window tree rooted at WINDOW, set their
7840 hscroll value so that PT is (i) visible in the window, and (ii) so
7841 that it is not within a certain margin at the window's left and
7842 right border. Value is non-zero if any window's hscroll has been
7843 changed. */
7844
7845 static int
7846 hscroll_window_tree (window)
7847 Lisp_Object window;
7848 {
7849 int hscrolled_p = 0;
7850
7851 while (WINDOWP (window))
7852 {
7853 struct window *w = XWINDOW (window);
7854
7855 if (WINDOWP (w->hchild))
7856 hscrolled_p |= hscroll_window_tree (w->hchild);
7857 else if (WINDOWP (w->vchild))
7858 hscrolled_p |= hscroll_window_tree (w->vchild);
7859 else if (w->cursor.vpos >= 0)
7860 {
7861 int hscroll_margin, text_area_x, text_area_y;
7862 int text_area_width, text_area_height;
7863 struct glyph_row *current_cursor_row
7864 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
7865 struct glyph_row *desired_cursor_row
7866 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
7867 struct glyph_row *cursor_row
7868 = (desired_cursor_row->enabled_p
7869 ? desired_cursor_row
7870 : current_cursor_row);
7871
7872 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
7873 &text_area_width, &text_area_height);
7874
7875 /* Scroll when cursor is inside this scroll margin. */
7876 hscroll_margin = 5 * CANON_X_UNIT (XFRAME (w->frame));
7877
7878 if ((XFASTINT (w->hscroll)
7879 && w->cursor.x < hscroll_margin)
7880 || (cursor_row->enabled_p
7881 && cursor_row->truncated_on_right_p
7882 && (w->cursor.x > text_area_width - hscroll_margin)))
7883 {
7884 struct it it;
7885 int hscroll;
7886 struct buffer *saved_current_buffer;
7887 int pt;
7888
7889 /* Find point in a display of infinite width. */
7890 saved_current_buffer = current_buffer;
7891 current_buffer = XBUFFER (w->buffer);
7892
7893 if (w == XWINDOW (selected_window))
7894 pt = BUF_PT (current_buffer);
7895 else
7896 {
7897 pt = marker_position (w->pointm);
7898 pt = max (BEGV, pt);
7899 pt = min (ZV, pt);
7900 }
7901
7902 /* Move iterator to pt starting at cursor_row->start in
7903 a line with infinite width. */
7904 init_to_row_start (&it, w, cursor_row);
7905 it.last_visible_x = INFINITY;
7906 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
7907 current_buffer = saved_current_buffer;
7908
7909 /* Center cursor in window. */
7910 hscroll = (max (0, it.current_x - text_area_width / 2)
7911 / CANON_X_UNIT (it.f));
7912 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
7913
7914 /* Don't call Fset_window_hscroll if value hasn't
7915 changed because it will prevent redisplay
7916 optimizations. */
7917 if (XFASTINT (w->hscroll) != hscroll)
7918 {
7919 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
7920 w->hscroll = make_number (hscroll);
7921 hscrolled_p = 1;
7922 }
7923 }
7924 }
7925
7926 window = w->next;
7927 }
7928
7929 /* Value is non-zero if hscroll of any leaf window has been changed. */
7930 return hscrolled_p;
7931 }
7932
7933
7934 /* Set hscroll so that cursor is visible and not inside horizontal
7935 scroll margins for all windows in the tree rooted at WINDOW. See
7936 also hscroll_window_tree above. Value is non-zero if any window's
7937 hscroll has been changed. If it has, desired matrices on the frame
7938 of WINDOW are cleared. */
7939
7940 static int
7941 hscroll_windows (window)
7942 Lisp_Object window;
7943 {
7944 int hscrolled_p;
7945
7946 if (automatic_hscrolling_p)
7947 {
7948 hscrolled_p = hscroll_window_tree (window);
7949 if (hscrolled_p)
7950 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
7951 }
7952 else
7953 hscrolled_p = 0;
7954 return hscrolled_p;
7955 }
7956
7957
7958 \f
7959 /************************************************************************
7960 Redisplay
7961 ************************************************************************/
7962
7963 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
7964 to a non-zero value. This is sometimes handy to have in a debugger
7965 session. */
7966
7967 #if GLYPH_DEBUG
7968
7969 /* First and last unchanged row for try_window_id. */
7970
7971 int debug_first_unchanged_at_end_vpos;
7972 int debug_last_unchanged_at_beg_vpos;
7973
7974 /* Delta vpos and y. */
7975
7976 int debug_dvpos, debug_dy;
7977
7978 /* Delta in characters and bytes for try_window_id. */
7979
7980 int debug_delta, debug_delta_bytes;
7981
7982 /* Values of window_end_pos and window_end_vpos at the end of
7983 try_window_id. */
7984
7985 int debug_end_pos, debug_end_vpos;
7986
7987 /* Append a string to W->desired_matrix->method. FMT is a printf
7988 format string. A1...A9 are a supplement for a variable-length
7989 argument list. If trace_redisplay_p is non-zero also printf the
7990 resulting string to stderr. */
7991
7992 static void
7993 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
7994 struct window *w;
7995 char *fmt;
7996 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
7997 {
7998 char buffer[512];
7999 char *method = w->desired_matrix->method;
8000 int len = strlen (method);
8001 int size = sizeof w->desired_matrix->method;
8002 int remaining = size - len - 1;
8003
8004 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
8005 if (len && remaining)
8006 {
8007 method[len] = '|';
8008 --remaining, ++len;
8009 }
8010
8011 strncpy (method + len, buffer, remaining);
8012
8013 if (trace_redisplay_p)
8014 fprintf (stderr, "%p (%s): %s\n",
8015 w,
8016 ((BUFFERP (w->buffer)
8017 && STRINGP (XBUFFER (w->buffer)->name))
8018 ? (char *) XSTRING (XBUFFER (w->buffer)->name)->data
8019 : "no buffer"),
8020 buffer);
8021 }
8022
8023 #endif /* GLYPH_DEBUG */
8024
8025
8026 /* This counter is used to clear the face cache every once in a while
8027 in redisplay_internal. It is incremented for each redisplay.
8028 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
8029 cleared. */
8030
8031 #define CLEAR_FACE_CACHE_COUNT 10000
8032 static int clear_face_cache_count;
8033
8034 /* Record the previous terminal frame we displayed. */
8035
8036 static struct frame *previous_terminal_frame;
8037
8038 /* Non-zero while redisplay_internal is in progress. */
8039
8040 int redisplaying_p;
8041
8042
8043 /* Value is non-zero if all changes in window W, which displays
8044 current_buffer, are in the text between START and END. START is a
8045 buffer position, END is given as a distance from Z. Used in
8046 redisplay_internal for display optimization. */
8047
8048 static INLINE int
8049 text_outside_line_unchanged_p (w, start, end)
8050 struct window *w;
8051 int start, end;
8052 {
8053 int unchanged_p = 1;
8054
8055 /* If text or overlays have changed, see where. */
8056 if (XFASTINT (w->last_modified) < MODIFF
8057 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8058 {
8059 /* Gap in the line? */
8060 if (GPT < start || Z - GPT < end)
8061 unchanged_p = 0;
8062
8063 /* Changes start in front of the line, or end after it? */
8064 if (unchanged_p
8065 && (BEG_UNCHANGED < start - 1
8066 || END_UNCHANGED < end))
8067 unchanged_p = 0;
8068
8069 /* If selective display, can't optimize if changes start at the
8070 beginning of the line. */
8071 if (unchanged_p
8072 && INTEGERP (current_buffer->selective_display)
8073 && XINT (current_buffer->selective_display) > 0
8074 && (BEG_UNCHANGED < start || GPT <= start))
8075 unchanged_p = 0;
8076 }
8077
8078 return unchanged_p;
8079 }
8080
8081
8082 /* Do a frame update, taking possible shortcuts into account. This is
8083 the main external entry point for redisplay.
8084
8085 If the last redisplay displayed an echo area message and that message
8086 is no longer requested, we clear the echo area or bring back the
8087 mini-buffer if that is in use. */
8088
8089 void
8090 redisplay ()
8091 {
8092 redisplay_internal (0);
8093 }
8094
8095 /* Return 1 if point moved out of or into a composition. Otherwise
8096 return 0. PREV_BUF and PREV_PT are the last point buffer and
8097 position. BUF and PT are the current point buffer and position. */
8098
8099 int
8100 check_point_in_composition (prev_buf, prev_pt, buf, pt)
8101 struct buffer *prev_buf, *buf;
8102 int prev_pt, pt;
8103 {
8104 int start, end;
8105 Lisp_Object prop;
8106 Lisp_Object buffer;
8107
8108 XSETBUFFER (buffer, buf);
8109 /* Check a composition at the last point if point moved within the
8110 same buffer. */
8111 if (prev_buf == buf)
8112 {
8113 if (prev_pt == pt)
8114 /* Point didn't move. */
8115 return 0;
8116
8117 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
8118 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
8119 && COMPOSITION_VALID_P (start, end, prop)
8120 && start < prev_pt && end > prev_pt)
8121 /* The last point was within the composition. Return 1 iff
8122 point moved out of the composition. */
8123 return (pt <= start || pt >= end);
8124 }
8125
8126 /* Check a composition at the current point. */
8127 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
8128 && find_composition (pt, -1, &start, &end, &prop, buffer)
8129 && COMPOSITION_VALID_P (start, end, prop)
8130 && start < pt && end > pt);
8131 }
8132
8133 /* Reconsider the setting of B->clip_changed which is displayed
8134 in window W. */
8135
8136 static INLINE void
8137 reconsider_clip_changes (w, b)
8138 struct window *w;
8139 struct buffer *b;
8140 {
8141 if (b->prevent_redisplay_optimizations_p)
8142 b->clip_changed = 1;
8143 else if (b->clip_changed
8144 && !NILP (w->window_end_valid)
8145 && w->current_matrix->buffer == b
8146 && w->current_matrix->zv == BUF_ZV (b)
8147 && w->current_matrix->begv == BUF_BEGV (b))
8148 b->clip_changed = 0;
8149
8150 /* If display wasn't paused, and W is not a tool bar window, see if
8151 point has been moved into or out of a composition. In that case,
8152 we set b->clip_changed to 1 to force updating the screen. If
8153 b->clip_changed has already been set to 1, we can skip this
8154 check. */
8155 if (!b->clip_changed
8156 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
8157 {
8158 int pt;
8159
8160 if (w == XWINDOW (selected_window))
8161 pt = BUF_PT (current_buffer);
8162 else
8163 pt = marker_position (w->pointm);
8164
8165 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
8166 || pt != XINT (w->last_point))
8167 && check_point_in_composition (w->current_matrix->buffer,
8168 XINT (w->last_point),
8169 XBUFFER (w->buffer), pt))
8170 b->clip_changed = 1;
8171 }
8172 }
8173
8174
8175 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
8176 response to any user action; therefore, we should preserve the echo
8177 area. (Actually, our caller does that job.) Perhaps in the future
8178 avoid recentering windows if it is not necessary; currently that
8179 causes some problems. */
8180
8181 static void
8182 redisplay_internal (preserve_echo_area)
8183 int preserve_echo_area;
8184 {
8185 struct window *w = XWINDOW (selected_window);
8186 struct frame *f = XFRAME (w->frame);
8187 int pause;
8188 int must_finish = 0;
8189 struct text_pos tlbufpos, tlendpos;
8190 int number_of_visible_frames;
8191 int count;
8192 struct frame *sf = SELECTED_FRAME ();
8193
8194 /* Non-zero means redisplay has to consider all windows on all
8195 frames. Zero means, only selected_window is considered. */
8196 int consider_all_windows_p;
8197
8198 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
8199
8200 /* No redisplay if running in batch mode or frame is not yet fully
8201 initialized, or redisplay is explicitly turned off by setting
8202 Vinhibit_redisplay. */
8203 if (noninteractive
8204 || !NILP (Vinhibit_redisplay)
8205 || !f->glyphs_initialized_p)
8206 return;
8207
8208 /* The flag redisplay_performed_directly_p is set by
8209 direct_output_for_insert when it already did the whole screen
8210 update necessary. */
8211 if (redisplay_performed_directly_p)
8212 {
8213 redisplay_performed_directly_p = 0;
8214 if (!hscroll_windows (selected_window))
8215 return;
8216 }
8217
8218 #ifdef USE_X_TOOLKIT
8219 if (popup_activated ())
8220 return;
8221 #endif
8222
8223 /* I don't think this happens but let's be paranoid. */
8224 if (redisplaying_p)
8225 return;
8226
8227 /* Record a function that resets redisplaying_p to its old value
8228 when we leave this function. */
8229 count = BINDING_STACK_SIZE ();
8230 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
8231 ++redisplaying_p;
8232
8233 retry:
8234 pause = 0;
8235 reconsider_clip_changes (w, current_buffer);
8236
8237 /* If new fonts have been loaded that make a glyph matrix adjustment
8238 necessary, do it. */
8239 if (fonts_changed_p)
8240 {
8241 adjust_glyphs (NULL);
8242 ++windows_or_buffers_changed;
8243 fonts_changed_p = 0;
8244 }
8245
8246 /* If face_change_count is non-zero, init_iterator will free all
8247 realized faces, which includes the faces referenced from current
8248 matrices. So, we can't reuse current matrices in this case. */
8249 if (face_change_count)
8250 ++windows_or_buffers_changed;
8251
8252 if (! FRAME_WINDOW_P (sf)
8253 && previous_terminal_frame != sf)
8254 {
8255 /* Since frames on an ASCII terminal share the same display
8256 area, displaying a different frame means redisplay the whole
8257 thing. */
8258 windows_or_buffers_changed++;
8259 SET_FRAME_GARBAGED (sf);
8260 XSETFRAME (Vterminal_frame, sf);
8261 }
8262 previous_terminal_frame = sf;
8263
8264 /* Set the visible flags for all frames. Do this before checking
8265 for resized or garbaged frames; they want to know if their frames
8266 are visible. See the comment in frame.h for
8267 FRAME_SAMPLE_VISIBILITY. */
8268 {
8269 Lisp_Object tail, frame;
8270
8271 number_of_visible_frames = 0;
8272
8273 FOR_EACH_FRAME (tail, frame)
8274 {
8275 struct frame *f = XFRAME (frame);
8276
8277 FRAME_SAMPLE_VISIBILITY (f);
8278 if (FRAME_VISIBLE_P (f))
8279 ++number_of_visible_frames;
8280 clear_desired_matrices (f);
8281 }
8282 }
8283
8284 /* Notice any pending interrupt request to change frame size. */
8285 do_pending_window_change (1);
8286
8287 /* Clear frames marked as garbaged. */
8288 if (frame_garbaged)
8289 clear_garbaged_frames ();
8290
8291 /* Build menubar and tool-bar items. */
8292 prepare_menu_bars ();
8293
8294 if (windows_or_buffers_changed)
8295 update_mode_lines++;
8296
8297 /* Detect case that we need to write or remove a star in the mode line. */
8298 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
8299 {
8300 w->update_mode_line = Qt;
8301 if (buffer_shared > 1)
8302 update_mode_lines++;
8303 }
8304
8305 /* If %c is in the mode line, update it if needed. */
8306 if (!NILP (w->column_number_displayed)
8307 /* This alternative quickly identifies a common case
8308 where no change is needed. */
8309 && !(PT == XFASTINT (w->last_point)
8310 && XFASTINT (w->last_modified) >= MODIFF
8311 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
8312 && XFASTINT (w->column_number_displayed) != current_column ())
8313 w->update_mode_line = Qt;
8314
8315 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
8316
8317 /* The variable buffer_shared is set in redisplay_window and
8318 indicates that we redisplay a buffer in different windows. See
8319 there. */
8320 consider_all_windows_p = update_mode_lines || buffer_shared > 1;
8321
8322 /* If specs for an arrow have changed, do thorough redisplay
8323 to ensure we remove any arrow that should no longer exist. */
8324 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
8325 || ! EQ (Voverlay_arrow_string, last_arrow_string))
8326 consider_all_windows_p = windows_or_buffers_changed = 1;
8327
8328 /* Normally the message* functions will have already displayed and
8329 updated the echo area, but the frame may have been trashed, or
8330 the update may have been preempted, so display the echo area
8331 again here. Checking both message buffers captures the case that
8332 the echo area should be cleared. */
8333 if (!NILP (echo_area_buffer[0]) || !NILP (echo_area_buffer[1]))
8334 {
8335 int window_height_changed_p = echo_area_display (0);
8336 must_finish = 1;
8337
8338 if (fonts_changed_p)
8339 goto retry;
8340 else if (window_height_changed_p)
8341 {
8342 consider_all_windows_p = 1;
8343 ++update_mode_lines;
8344 ++windows_or_buffers_changed;
8345
8346 /* If window configuration was changed, frames may have been
8347 marked garbaged. Clear them or we will experience
8348 surprises wrt scrolling. */
8349 if (frame_garbaged)
8350 clear_garbaged_frames ();
8351 }
8352 }
8353 else if (EQ (selected_window, minibuf_window)
8354 && (current_buffer->clip_changed
8355 || XFASTINT (w->last_modified) < MODIFF
8356 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8357 && resize_mini_window (w, 0))
8358 {
8359 /* Resized active mini-window to fit the size of what it is
8360 showing if its contents might have changed. */
8361 must_finish = 1;
8362 consider_all_windows_p = 1;
8363 ++windows_or_buffers_changed;
8364 ++update_mode_lines;
8365
8366 /* If window configuration was changed, frames may have been
8367 marked garbaged. Clear them or we will experience
8368 surprises wrt scrolling. */
8369 if (frame_garbaged)
8370 clear_garbaged_frames ();
8371 }
8372
8373
8374 /* If showing the region, and mark has changed, we must redisplay
8375 the whole window. The assignment to this_line_start_pos prevents
8376 the optimization directly below this if-statement. */
8377 if (((!NILP (Vtransient_mark_mode)
8378 && !NILP (XBUFFER (w->buffer)->mark_active))
8379 != !NILP (w->region_showing))
8380 || (!NILP (w->region_showing)
8381 && !EQ (w->region_showing,
8382 Fmarker_position (XBUFFER (w->buffer)->mark))))
8383 CHARPOS (this_line_start_pos) = 0;
8384
8385 /* Optimize the case that only the line containing the cursor in the
8386 selected window has changed. Variables starting with this_ are
8387 set in display_line and record information about the line
8388 containing the cursor. */
8389 tlbufpos = this_line_start_pos;
8390 tlendpos = this_line_end_pos;
8391 if (!consider_all_windows_p
8392 && CHARPOS (tlbufpos) > 0
8393 && NILP (w->update_mode_line)
8394 && !current_buffer->clip_changed
8395 && FRAME_VISIBLE_P (XFRAME (w->frame))
8396 && !FRAME_OBSCURED_P (XFRAME (w->frame))
8397 /* Make sure recorded data applies to current buffer, etc. */
8398 && this_line_buffer == current_buffer
8399 && current_buffer == XBUFFER (w->buffer)
8400 && NILP (w->force_start)
8401 /* Point must be on the line that we have info recorded about. */
8402 && PT >= CHARPOS (tlbufpos)
8403 && PT <= Z - CHARPOS (tlendpos)
8404 /* All text outside that line, including its final newline,
8405 must be unchanged */
8406 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
8407 CHARPOS (tlendpos)))
8408 {
8409 if (CHARPOS (tlbufpos) > BEGV
8410 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
8411 && (CHARPOS (tlbufpos) == ZV
8412 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
8413 /* Former continuation line has disappeared by becoming empty */
8414 goto cancel;
8415 else if (XFASTINT (w->last_modified) < MODIFF
8416 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
8417 || MINI_WINDOW_P (w))
8418 {
8419 /* We have to handle the case of continuation around a
8420 wide-column character (See the comment in indent.c around
8421 line 885).
8422
8423 For instance, in the following case:
8424
8425 -------- Insert --------
8426 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
8427 J_I_ ==> J_I_ `^^' are cursors.
8428 ^^ ^^
8429 -------- --------
8430
8431 As we have to redraw the line above, we should goto cancel. */
8432
8433 struct it it;
8434 int line_height_before = this_line_pixel_height;
8435
8436 /* Note that start_display will handle the case that the
8437 line starting at tlbufpos is a continuation lines. */
8438 start_display (&it, w, tlbufpos);
8439
8440 /* Implementation note: It this still necessary? */
8441 if (it.current_x != this_line_start_x)
8442 goto cancel;
8443
8444 TRACE ((stderr, "trying display optimization 1\n"));
8445 w->cursor.vpos = -1;
8446 overlay_arrow_seen = 0;
8447 it.vpos = this_line_vpos;
8448 it.current_y = this_line_y;
8449 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
8450 display_line (&it);
8451
8452 /* If line contains point, is not continued,
8453 and ends at same distance from eob as before, we win */
8454 if (w->cursor.vpos >= 0
8455 /* Line is not continued, otherwise this_line_start_pos
8456 would have been set to 0 in display_line. */
8457 && CHARPOS (this_line_start_pos)
8458 /* Line ends as before. */
8459 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
8460 /* Line has same height as before. Otherwise other lines
8461 would have to be shifted up or down. */
8462 && this_line_pixel_height == line_height_before)
8463 {
8464 /* If this is not the window's last line, we must adjust
8465 the charstarts of the lines below. */
8466 if (it.current_y < it.last_visible_y)
8467 {
8468 struct glyph_row *row
8469 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
8470 int delta, delta_bytes;
8471
8472 if (Z - CHARPOS (tlendpos) == ZV)
8473 {
8474 /* This line ends at end of (accessible part of)
8475 buffer. There is no newline to count. */
8476 delta = (Z
8477 - CHARPOS (tlendpos)
8478 - MATRIX_ROW_START_CHARPOS (row));
8479 delta_bytes = (Z_BYTE
8480 - BYTEPOS (tlendpos)
8481 - MATRIX_ROW_START_BYTEPOS (row));
8482 }
8483 else
8484 {
8485 /* This line ends in a newline. Must take
8486 account of the newline and the rest of the
8487 text that follows. */
8488 delta = (Z
8489 - CHARPOS (tlendpos)
8490 - MATRIX_ROW_START_CHARPOS (row));
8491 delta_bytes = (Z_BYTE
8492 - BYTEPOS (tlendpos)
8493 - MATRIX_ROW_START_BYTEPOS (row));
8494 }
8495
8496 increment_matrix_positions (w->current_matrix,
8497 this_line_vpos + 1,
8498 w->current_matrix->nrows,
8499 delta, delta_bytes);
8500 }
8501
8502 /* If this row displays text now but previously didn't,
8503 or vice versa, w->window_end_vpos may have to be
8504 adjusted. */
8505 if ((it.glyph_row - 1)->displays_text_p)
8506 {
8507 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
8508 XSETINT (w->window_end_vpos, this_line_vpos);
8509 }
8510 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
8511 && this_line_vpos > 0)
8512 XSETINT (w->window_end_vpos, this_line_vpos - 1);
8513 w->window_end_valid = Qnil;
8514
8515 /* Update hint: No need to try to scroll in update_window. */
8516 w->desired_matrix->no_scrolling_p = 1;
8517
8518 #if GLYPH_DEBUG
8519 *w->desired_matrix->method = 0;
8520 debug_method_add (w, "optimization 1");
8521 #endif
8522 goto update;
8523 }
8524 else
8525 goto cancel;
8526 }
8527 else if (/* Cursor position hasn't changed. */
8528 PT == XFASTINT (w->last_point)
8529 /* Make sure the cursor was last displayed
8530 in this window. Otherwise we have to reposition it. */
8531 && 0 <= w->cursor.vpos
8532 && XINT (w->height) > w->cursor.vpos)
8533 {
8534 if (!must_finish)
8535 {
8536 do_pending_window_change (1);
8537
8538 /* We used to always goto end_of_redisplay here, but this
8539 isn't enough if we have a blinking cursor. */
8540 if (w->cursor_off_p == w->last_cursor_off_p)
8541 goto end_of_redisplay;
8542 }
8543 goto update;
8544 }
8545 /* If highlighting the region, or if the cursor is in the echo area,
8546 then we can't just move the cursor. */
8547 else if (! (!NILP (Vtransient_mark_mode)
8548 && !NILP (current_buffer->mark_active))
8549 && (EQ (selected_window, current_buffer->last_selected_window)
8550 || highlight_nonselected_windows)
8551 && NILP (w->region_showing)
8552 && NILP (Vshow_trailing_whitespace)
8553 && !cursor_in_echo_area)
8554 {
8555 struct it it;
8556 struct glyph_row *row;
8557
8558 /* Skip from tlbufpos to PT and see where it is. Note that
8559 PT may be in invisible text. If so, we will end at the
8560 next visible position. */
8561 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
8562 NULL, DEFAULT_FACE_ID);
8563 it.current_x = this_line_start_x;
8564 it.current_y = this_line_y;
8565 it.vpos = this_line_vpos;
8566
8567 /* The call to move_it_to stops in front of PT, but
8568 moves over before-strings. */
8569 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
8570
8571 if (it.vpos == this_line_vpos
8572 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
8573 row->enabled_p))
8574 {
8575 xassert (this_line_vpos == it.vpos);
8576 xassert (this_line_y == it.current_y);
8577 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
8578 #if GLYPH_DEBUG
8579 *w->desired_matrix->method = 0;
8580 debug_method_add (w, "optimization 3");
8581 #endif
8582 goto update;
8583 }
8584 else
8585 goto cancel;
8586 }
8587
8588 cancel:
8589 /* Text changed drastically or point moved off of line. */
8590 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
8591 }
8592
8593 CHARPOS (this_line_start_pos) = 0;
8594 consider_all_windows_p |= buffer_shared > 1;
8595 ++clear_face_cache_count;
8596
8597
8598 /* Build desired matrices, and update the display. If
8599 consider_all_windows_p is non-zero, do it for all windows on all
8600 frames. Otherwise do it for selected_window, only. */
8601
8602 if (consider_all_windows_p)
8603 {
8604 Lisp_Object tail, frame;
8605 int i, n = 0, size = 50;
8606 struct frame **updated
8607 = (struct frame **) alloca (size * sizeof *updated);
8608
8609 /* Clear the face cache eventually. */
8610 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
8611 {
8612 clear_face_cache (0);
8613 clear_face_cache_count = 0;
8614 }
8615
8616 /* Recompute # windows showing selected buffer. This will be
8617 incremented each time such a window is displayed. */
8618 buffer_shared = 0;
8619
8620 FOR_EACH_FRAME (tail, frame)
8621 {
8622 struct frame *f = XFRAME (frame);
8623
8624 if (FRAME_WINDOW_P (f) || f == sf)
8625 {
8626 /* Mark all the scroll bars to be removed; we'll redeem
8627 the ones we want when we redisplay their windows. */
8628 if (condemn_scroll_bars_hook)
8629 condemn_scroll_bars_hook (f);
8630
8631 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8632 redisplay_windows (FRAME_ROOT_WINDOW (f));
8633
8634 /* Any scroll bars which redisplay_windows should have
8635 nuked should now go away. */
8636 if (judge_scroll_bars_hook)
8637 judge_scroll_bars_hook (f);
8638
8639 /* If fonts changed, display again. */
8640 if (fonts_changed_p)
8641 goto retry;
8642
8643 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8644 {
8645 /* See if we have to hscroll. */
8646 if (hscroll_windows (f->root_window))
8647 goto retry;
8648
8649 /* Prevent various kinds of signals during display
8650 update. stdio is not robust about handling
8651 signals, which can cause an apparent I/O
8652 error. */
8653 if (interrupt_input)
8654 unrequest_sigio ();
8655 stop_polling ();
8656
8657 /* Update the display. */
8658 set_window_update_flags (XWINDOW (f->root_window), 1);
8659 pause |= update_frame (f, 0, 0);
8660 if (pause)
8661 break;
8662
8663 if (n == size)
8664 {
8665 int nbytes = size * sizeof *updated;
8666 struct frame **p = (struct frame **) alloca (2 * nbytes);
8667 bcopy (updated, p, nbytes);
8668 size *= 2;
8669 }
8670
8671 updated[n++] = f;
8672 }
8673 }
8674 }
8675
8676 /* Do the mark_window_display_accurate after all windows have
8677 been redisplayed because this call resets flags in buffers
8678 which are needed for proper redisplay. */
8679 for (i = 0; i < n; ++i)
8680 {
8681 struct frame *f = updated[i];
8682 mark_window_display_accurate (f->root_window, 1);
8683 if (frame_up_to_date_hook)
8684 frame_up_to_date_hook (f);
8685 }
8686 }
8687 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8688 {
8689 Lisp_Object mini_window;
8690 struct frame *mini_frame;
8691
8692 redisplay_window (selected_window, 1);
8693
8694 /* Compare desired and current matrices, perform output. */
8695 update:
8696
8697 /* If fonts changed, display again. */
8698 if (fonts_changed_p)
8699 goto retry;
8700
8701 /* Prevent various kinds of signals during display update.
8702 stdio is not robust about handling signals,
8703 which can cause an apparent I/O error. */
8704 if (interrupt_input)
8705 unrequest_sigio ();
8706 stop_polling ();
8707
8708 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8709 {
8710 if (hscroll_windows (selected_window))
8711 goto retry;
8712
8713 XWINDOW (selected_window)->must_be_updated_p = 1;
8714 pause = update_frame (sf, 0, 0);
8715 }
8716
8717 /* We may have called echo_area_display at the top of this
8718 function. If the echo area is on another frame, that may
8719 have put text on a frame other than the selected one, so the
8720 above call to update_frame would not have caught it. Catch
8721 it here. */
8722 mini_window = FRAME_MINIBUF_WINDOW (sf);
8723 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8724
8725 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
8726 {
8727 XWINDOW (mini_window)->must_be_updated_p = 1;
8728 pause |= update_frame (mini_frame, 0, 0);
8729 if (!pause && hscroll_windows (mini_window))
8730 goto retry;
8731 }
8732 }
8733
8734 /* If display was paused because of pending input, make sure we do a
8735 thorough update the next time. */
8736 if (pause)
8737 {
8738 /* Prevent the optimization at the beginning of
8739 redisplay_internal that tries a single-line update of the
8740 line containing the cursor in the selected window. */
8741 CHARPOS (this_line_start_pos) = 0;
8742
8743 /* Let the overlay arrow be updated the next time. */
8744 if (!NILP (last_arrow_position))
8745 {
8746 last_arrow_position = Qt;
8747 last_arrow_string = Qt;
8748 }
8749
8750 /* If we pause after scrolling, some rows in the current
8751 matrices of some windows are not valid. */
8752 if (!WINDOW_FULL_WIDTH_P (w)
8753 && !FRAME_WINDOW_P (XFRAME (w->frame)))
8754 update_mode_lines = 1;
8755 }
8756 else
8757 {
8758 if (!consider_all_windows_p)
8759 {
8760 /* This has already been done above if
8761 consider_all_windows_p is set. */
8762 mark_window_display_accurate_1 (w, 1);
8763
8764 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
8765 last_arrow_string = Voverlay_arrow_string;
8766
8767 if (frame_up_to_date_hook != 0)
8768 frame_up_to_date_hook (sf);
8769 }
8770
8771 update_mode_lines = 0;
8772 windows_or_buffers_changed = 0;
8773 }
8774
8775 /* Start SIGIO interrupts coming again. Having them off during the
8776 code above makes it less likely one will discard output, but not
8777 impossible, since there might be stuff in the system buffer here.
8778 But it is much hairier to try to do anything about that. */
8779 if (interrupt_input)
8780 request_sigio ();
8781 start_polling ();
8782
8783 /* If a frame has become visible which was not before, redisplay
8784 again, so that we display it. Expose events for such a frame
8785 (which it gets when becoming visible) don't call the parts of
8786 redisplay constructing glyphs, so simply exposing a frame won't
8787 display anything in this case. So, we have to display these
8788 frames here explicitly. */
8789 if (!pause)
8790 {
8791 Lisp_Object tail, frame;
8792 int new_count = 0;
8793
8794 FOR_EACH_FRAME (tail, frame)
8795 {
8796 int this_is_visible = 0;
8797
8798 if (XFRAME (frame)->visible)
8799 this_is_visible = 1;
8800 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
8801 if (XFRAME (frame)->visible)
8802 this_is_visible = 1;
8803
8804 if (this_is_visible)
8805 new_count++;
8806 }
8807
8808 if (new_count != number_of_visible_frames)
8809 windows_or_buffers_changed++;
8810 }
8811
8812 /* Change frame size now if a change is pending. */
8813 do_pending_window_change (1);
8814
8815 /* If we just did a pending size change, or have additional
8816 visible frames, redisplay again. */
8817 if (windows_or_buffers_changed && !pause)
8818 goto retry;
8819
8820 end_of_redisplay:;
8821
8822 unbind_to (count, Qnil);
8823 }
8824
8825
8826 /* Redisplay, but leave alone any recent echo area message unless
8827 another message has been requested in its place.
8828
8829 This is useful in situations where you need to redisplay but no
8830 user action has occurred, making it inappropriate for the message
8831 area to be cleared. See tracking_off and
8832 wait_reading_process_input for examples of these situations.
8833
8834 FROM_WHERE is an integer saying from where this function was
8835 called. This is useful for debugging. */
8836
8837 void
8838 redisplay_preserve_echo_area (from_where)
8839 int from_where;
8840 {
8841 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
8842
8843 if (!NILP (echo_area_buffer[1]))
8844 {
8845 /* We have a previously displayed message, but no current
8846 message. Redisplay the previous message. */
8847 display_last_displayed_message_p = 1;
8848 redisplay_internal (1);
8849 display_last_displayed_message_p = 0;
8850 }
8851 else
8852 redisplay_internal (1);
8853 }
8854
8855
8856 /* Function registered with record_unwind_protect in
8857 redisplay_internal. Clears the flag indicating that a redisplay is
8858 in progress. */
8859
8860 static Lisp_Object
8861 unwind_redisplay (old_redisplaying_p)
8862 Lisp_Object old_redisplaying_p;
8863 {
8864 redisplaying_p = XFASTINT (old_redisplaying_p);
8865 return Qnil;
8866 }
8867
8868
8869 /* Mark the display of window W as accurate or inaccurate. If
8870 ACCURATE_P is non-zero mark display of W as accurate. If
8871 ACCURATE_P is zero, arrange for W to be redisplayed the next time
8872 redisplay_internal is called. */
8873
8874 static void
8875 mark_window_display_accurate_1 (w, accurate_p)
8876 struct window *w;
8877 int accurate_p;
8878 {
8879 if (BUFFERP (w->buffer))
8880 {
8881 struct buffer *b = XBUFFER (w->buffer);
8882
8883 w->last_modified
8884 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
8885 w->last_overlay_modified
8886 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
8887 w->last_had_star
8888 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
8889
8890 if (accurate_p)
8891 {
8892 b->clip_changed = 0;
8893 b->prevent_redisplay_optimizations_p = 0;
8894
8895 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
8896 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
8897 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
8898 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
8899
8900 w->current_matrix->buffer = b;
8901 w->current_matrix->begv = BUF_BEGV (b);
8902 w->current_matrix->zv = BUF_ZV (b);
8903
8904 w->last_cursor = w->cursor;
8905 w->last_cursor_off_p = w->cursor_off_p;
8906
8907 if (w == XWINDOW (selected_window))
8908 w->last_point = make_number (BUF_PT (b));
8909 else
8910 w->last_point = make_number (XMARKER (w->pointm)->charpos);
8911 }
8912 }
8913
8914 if (accurate_p)
8915 {
8916 w->window_end_valid = w->buffer;
8917 w->update_mode_line = Qnil;
8918 }
8919 }
8920
8921
8922 /* Mark the display of windows in the window tree rooted at WINDOW as
8923 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
8924 windows as accurate. If ACCURATE_P is zero, arrange for windows to
8925 be redisplayed the next time redisplay_internal is called. */
8926
8927 void
8928 mark_window_display_accurate (window, accurate_p)
8929 Lisp_Object window;
8930 int accurate_p;
8931 {
8932 struct window *w;
8933
8934 for (; !NILP (window); window = w->next)
8935 {
8936 w = XWINDOW (window);
8937 mark_window_display_accurate_1 (w, accurate_p);
8938
8939 if (!NILP (w->vchild))
8940 mark_window_display_accurate (w->vchild, accurate_p);
8941 if (!NILP (w->hchild))
8942 mark_window_display_accurate (w->hchild, accurate_p);
8943 }
8944
8945 if (accurate_p)
8946 {
8947 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
8948 last_arrow_string = Voverlay_arrow_string;
8949 }
8950 else
8951 {
8952 /* Force a thorough redisplay the next time by setting
8953 last_arrow_position and last_arrow_string to t, which is
8954 unequal to any useful value of Voverlay_arrow_... */
8955 last_arrow_position = Qt;
8956 last_arrow_string = Qt;
8957 }
8958 }
8959
8960
8961 /* Return value in display table DP (Lisp_Char_Table *) for character
8962 C. Since a display table doesn't have any parent, we don't have to
8963 follow parent. Do not call this function directly but use the
8964 macro DISP_CHAR_VECTOR. */
8965
8966 Lisp_Object
8967 disp_char_vector (dp, c)
8968 struct Lisp_Char_Table *dp;
8969 int c;
8970 {
8971 int code[4], i;
8972 Lisp_Object val;
8973
8974 if (SINGLE_BYTE_CHAR_P (c))
8975 return (dp->contents[c]);
8976
8977 SPLIT_CHAR (c, code[0], code[1], code[2]);
8978 if (code[1] < 32)
8979 code[1] = -1;
8980 else if (code[2] < 32)
8981 code[2] = -1;
8982
8983 /* Here, the possible range of code[0] (== charset ID) is
8984 128..max_charset. Since the top level char table contains data
8985 for multibyte characters after 256th element, we must increment
8986 code[0] by 128 to get a correct index. */
8987 code[0] += 128;
8988 code[3] = -1; /* anchor */
8989
8990 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
8991 {
8992 val = dp->contents[code[i]];
8993 if (!SUB_CHAR_TABLE_P (val))
8994 return (NILP (val) ? dp->defalt : val);
8995 }
8996
8997 /* Here, val is a sub char table. We return the default value of
8998 it. */
8999 return (dp->defalt);
9000 }
9001
9002
9003 \f
9004 /***********************************************************************
9005 Window Redisplay
9006 ***********************************************************************/
9007
9008 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
9009
9010 static void
9011 redisplay_windows (window)
9012 Lisp_Object window;
9013 {
9014 while (!NILP (window))
9015 {
9016 struct window *w = XWINDOW (window);
9017
9018 if (!NILP (w->hchild))
9019 redisplay_windows (w->hchild);
9020 else if (!NILP (w->vchild))
9021 redisplay_windows (w->vchild);
9022 else
9023 redisplay_window (window, 0);
9024
9025 window = w->next;
9026 }
9027 }
9028
9029
9030 /* Set cursor position of W. PT is assumed to be displayed in ROW.
9031 DELTA is the number of bytes by which positions recorded in ROW
9032 differ from current buffer positions. */
9033
9034 void
9035 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
9036 struct window *w;
9037 struct glyph_row *row;
9038 struct glyph_matrix *matrix;
9039 int delta, delta_bytes, dy, dvpos;
9040 {
9041 struct glyph *glyph = row->glyphs[TEXT_AREA];
9042 struct glyph *end = glyph + row->used[TEXT_AREA];
9043 int x = row->x;
9044 int pt_old = PT - delta;
9045
9046 /* Skip over glyphs not having an object at the start of the row.
9047 These are special glyphs like truncation marks on terminal
9048 frames. */
9049 if (row->displays_text_p)
9050 while (glyph < end
9051 && INTEGERP (glyph->object)
9052 && glyph->charpos < 0)
9053 {
9054 x += glyph->pixel_width;
9055 ++glyph;
9056 }
9057
9058 while (glyph < end
9059 && !INTEGERP (glyph->object)
9060 && (!BUFFERP (glyph->object)
9061 || glyph->charpos < pt_old))
9062 {
9063 x += glyph->pixel_width;
9064 ++glyph;
9065 }
9066
9067 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
9068 w->cursor.x = x;
9069 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
9070 w->cursor.y = row->y + dy;
9071
9072 if (w == XWINDOW (selected_window))
9073 {
9074 if (!row->continued_p
9075 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
9076 && row->x == 0)
9077 {
9078 this_line_buffer = XBUFFER (w->buffer);
9079
9080 CHARPOS (this_line_start_pos)
9081 = MATRIX_ROW_START_CHARPOS (row) + delta;
9082 BYTEPOS (this_line_start_pos)
9083 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
9084
9085 CHARPOS (this_line_end_pos)
9086 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
9087 BYTEPOS (this_line_end_pos)
9088 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
9089
9090 this_line_y = w->cursor.y;
9091 this_line_pixel_height = row->height;
9092 this_line_vpos = w->cursor.vpos;
9093 this_line_start_x = row->x;
9094 }
9095 else
9096 CHARPOS (this_line_start_pos) = 0;
9097 }
9098 }
9099
9100
9101 /* Run window scroll functions, if any, for WINDOW with new window
9102 start STARTP. Sets the window start of WINDOW to that position.
9103
9104 We assume that the window's buffer is really current. */
9105
9106 static INLINE struct text_pos
9107 run_window_scroll_functions (window, startp)
9108 Lisp_Object window;
9109 struct text_pos startp;
9110 {
9111 struct window *w = XWINDOW (window);
9112 SET_MARKER_FROM_TEXT_POS (w->start, startp);
9113
9114 if (current_buffer != XBUFFER (w->buffer))
9115 abort ();
9116
9117 if (!NILP (Vwindow_scroll_functions))
9118 {
9119 run_hook_with_args_2 (Qwindow_scroll_functions, window,
9120 make_number (CHARPOS (startp)));
9121 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9122 /* In case the hook functions switch buffers. */
9123 if (current_buffer != XBUFFER (w->buffer))
9124 set_buffer_internal_1 (XBUFFER (w->buffer));
9125 }
9126
9127 return startp;
9128 }
9129
9130
9131 /* Modify the desired matrix of window W and W->vscroll so that the
9132 line containing the cursor is fully visible. */
9133
9134 static void
9135 make_cursor_line_fully_visible (w)
9136 struct window *w;
9137 {
9138 struct glyph_matrix *matrix;
9139 struct glyph_row *row;
9140 int window_height;
9141
9142 /* It's not always possible to find the cursor, e.g, when a window
9143 is full of overlay strings. Don't do anything in that case. */
9144 if (w->cursor.vpos < 0)
9145 return;
9146
9147 matrix = w->desired_matrix;
9148 row = MATRIX_ROW (matrix, w->cursor.vpos);
9149
9150 /* If the cursor row is not partially visible, there's nothing
9151 to do. */
9152 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
9153 return;
9154
9155 /* If the row the cursor is in is taller than the window's height,
9156 it's not clear what to do, so do nothing. */
9157 window_height = window_box_height (w);
9158 if (row->height >= window_height)
9159 return;
9160
9161 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
9162 {
9163 int dy = row->height - row->visible_height;
9164 w->vscroll = 0;
9165 w->cursor.y += dy;
9166 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9167 }
9168 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
9169 {
9170 int dy = - (row->height - row->visible_height);
9171 w->vscroll = dy;
9172 w->cursor.y += dy;
9173 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9174 }
9175
9176 /* When we change the cursor y-position of the selected window,
9177 change this_line_y as well so that the display optimization for
9178 the cursor line of the selected window in redisplay_internal uses
9179 the correct y-position. */
9180 if (w == XWINDOW (selected_window))
9181 this_line_y = w->cursor.y;
9182 }
9183
9184
9185 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
9186 non-zero means only WINDOW is redisplayed in redisplay_internal.
9187 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
9188 in redisplay_window to bring a partially visible line into view in
9189 the case that only the cursor has moved.
9190
9191 Value is
9192
9193 1 if scrolling succeeded
9194
9195 0 if scrolling didn't find point.
9196
9197 -1 if new fonts have been loaded so that we must interrupt
9198 redisplay, adjust glyph matrices, and try again. */
9199
9200 static int
9201 try_scrolling (window, just_this_one_p, scroll_conservatively,
9202 scroll_step, temp_scroll_step)
9203 Lisp_Object window;
9204 int just_this_one_p;
9205 int scroll_conservatively, scroll_step;
9206 int temp_scroll_step;
9207 {
9208 struct window *w = XWINDOW (window);
9209 struct frame *f = XFRAME (w->frame);
9210 struct text_pos scroll_margin_pos;
9211 struct text_pos pos;
9212 struct text_pos startp;
9213 struct it it;
9214 Lisp_Object window_end;
9215 int this_scroll_margin;
9216 int dy = 0;
9217 int scroll_max;
9218 int rc;
9219 int amount_to_scroll = 0;
9220 Lisp_Object aggressive;
9221 int height;
9222
9223 #if GLYPH_DEBUG
9224 debug_method_add (w, "try_scrolling");
9225 #endif
9226
9227 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9228
9229 /* Compute scroll margin height in pixels. We scroll when point is
9230 within this distance from the top or bottom of the window. */
9231 if (scroll_margin > 0)
9232 {
9233 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
9234 this_scroll_margin *= CANON_Y_UNIT (f);
9235 }
9236 else
9237 this_scroll_margin = 0;
9238
9239 /* Compute how much we should try to scroll maximally to bring point
9240 into view. */
9241 if (scroll_step || scroll_conservatively || temp_scroll_step)
9242 scroll_max = max (scroll_step,
9243 max (scroll_conservatively, temp_scroll_step));
9244 else if (NUMBERP (current_buffer->scroll_down_aggressively)
9245 || NUMBERP (current_buffer->scroll_up_aggressively))
9246 /* We're trying to scroll because of aggressive scrolling
9247 but no scroll_step is set. Choose an arbitrary one. Maybe
9248 there should be a variable for this. */
9249 scroll_max = 10;
9250 else
9251 scroll_max = 0;
9252 scroll_max *= CANON_Y_UNIT (f);
9253
9254 /* Decide whether we have to scroll down. Start at the window end
9255 and move this_scroll_margin up to find the position of the scroll
9256 margin. */
9257 window_end = Fwindow_end (window, Qt);
9258 CHARPOS (scroll_margin_pos) = XINT (window_end);
9259 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
9260 if (this_scroll_margin)
9261 {
9262 start_display (&it, w, scroll_margin_pos);
9263 move_it_vertically (&it, - this_scroll_margin);
9264 scroll_margin_pos = it.current.pos;
9265 }
9266
9267 if (PT >= CHARPOS (scroll_margin_pos))
9268 {
9269 int y0;
9270
9271 /* Point is in the scroll margin at the bottom of the window, or
9272 below. Compute a new window start that makes point visible. */
9273
9274 /* Compute the distance from the scroll margin to PT.
9275 Give up if the distance is greater than scroll_max. */
9276 start_display (&it, w, scroll_margin_pos);
9277 y0 = it.current_y;
9278 move_it_to (&it, PT, 0, it.last_visible_y, -1,
9279 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9280
9281 /* With a scroll_margin of 0, scroll_margin_pos is at the window
9282 end, which is one line below the window. The iterator's
9283 current_y will be same as y0 in that case, but we have to
9284 scroll a line to make PT visible. That's the reason why 1 is
9285 added below. */
9286 dy = 1 + it.current_y - y0;
9287
9288 if (dy > scroll_max)
9289 return 0;
9290
9291 /* Move the window start down. If scrolling conservatively,
9292 move it just enough down to make point visible. If
9293 scroll_step is set, move it down by scroll_step. */
9294 start_display (&it, w, startp);
9295
9296 if (scroll_conservatively)
9297 amount_to_scroll
9298 = max (max (dy, CANON_Y_UNIT (f)),
9299 CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
9300 else if (scroll_step || temp_scroll_step)
9301 amount_to_scroll = scroll_max;
9302 else
9303 {
9304 aggressive = current_buffer->scroll_down_aggressively;
9305 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
9306 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
9307 if (NUMBERP (aggressive))
9308 amount_to_scroll = XFLOATINT (aggressive) * height;
9309 }
9310
9311 if (amount_to_scroll <= 0)
9312 return 0;
9313
9314 move_it_vertically (&it, amount_to_scroll);
9315 startp = it.current.pos;
9316 }
9317 else
9318 {
9319 /* See if point is inside the scroll margin at the top of the
9320 window. */
9321 scroll_margin_pos = startp;
9322 if (this_scroll_margin)
9323 {
9324 start_display (&it, w, startp);
9325 move_it_vertically (&it, this_scroll_margin);
9326 scroll_margin_pos = it.current.pos;
9327 }
9328
9329 if (PT < CHARPOS (scroll_margin_pos))
9330 {
9331 /* Point is in the scroll margin at the top of the window or
9332 above what is displayed in the window. */
9333 int y0;
9334
9335 /* Compute the vertical distance from PT to the scroll
9336 margin position. Give up if distance is greater than
9337 scroll_max. */
9338 SET_TEXT_POS (pos, PT, PT_BYTE);
9339 start_display (&it, w, pos);
9340 y0 = it.current_y;
9341 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
9342 it.last_visible_y, -1,
9343 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9344 dy = it.current_y - y0;
9345 if (dy > scroll_max)
9346 return 0;
9347
9348 /* Compute new window start. */
9349 start_display (&it, w, startp);
9350
9351 if (scroll_conservatively)
9352 amount_to_scroll =
9353 max (dy, CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
9354 else if (scroll_step || temp_scroll_step)
9355 amount_to_scroll = scroll_max;
9356 else
9357 {
9358 aggressive = current_buffer->scroll_up_aggressively;
9359 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
9360 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
9361 if (NUMBERP (aggressive))
9362 amount_to_scroll = XFLOATINT (aggressive) * height;
9363 }
9364
9365 if (amount_to_scroll <= 0)
9366 return 0;
9367
9368 move_it_vertically (&it, - amount_to_scroll);
9369 startp = it.current.pos;
9370 }
9371 }
9372
9373 /* Run window scroll functions. */
9374 startp = run_window_scroll_functions (window, startp);
9375
9376 /* Display the window. Give up if new fonts are loaded, or if point
9377 doesn't appear. */
9378 if (!try_window (window, startp))
9379 rc = -1;
9380 else if (w->cursor.vpos < 0)
9381 {
9382 clear_glyph_matrix (w->desired_matrix);
9383 rc = 0;
9384 }
9385 else
9386 {
9387 /* Maybe forget recorded base line for line number display. */
9388 if (!just_this_one_p
9389 || current_buffer->clip_changed
9390 || BEG_UNCHANGED < CHARPOS (startp))
9391 w->base_line_number = Qnil;
9392
9393 /* If cursor ends up on a partially visible line, shift display
9394 lines up or down. */
9395 make_cursor_line_fully_visible (w);
9396 rc = 1;
9397 }
9398
9399 return rc;
9400 }
9401
9402
9403 /* Compute a suitable window start for window W if display of W starts
9404 on a continuation line. Value is non-zero if a new window start
9405 was computed.
9406
9407 The new window start will be computed, based on W's width, starting
9408 from the start of the continued line. It is the start of the
9409 screen line with the minimum distance from the old start W->start. */
9410
9411 static int
9412 compute_window_start_on_continuation_line (w)
9413 struct window *w;
9414 {
9415 struct text_pos pos, start_pos;
9416 int window_start_changed_p = 0;
9417
9418 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
9419
9420 /* If window start is on a continuation line... Window start may be
9421 < BEGV in case there's invisible text at the start of the
9422 buffer (M-x rmail, for example). */
9423 if (CHARPOS (start_pos) > BEGV
9424 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
9425 {
9426 struct it it;
9427 struct glyph_row *row;
9428
9429 /* Handle the case that the window start is out of range. */
9430 if (CHARPOS (start_pos) < BEGV)
9431 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
9432 else if (CHARPOS (start_pos) > ZV)
9433 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
9434
9435 /* Find the start of the continued line. This should be fast
9436 because scan_buffer is fast (newline cache). */
9437 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
9438 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
9439 row, DEFAULT_FACE_ID);
9440 reseat_at_previous_visible_line_start (&it);
9441
9442 /* If the line start is "too far" away from the window start,
9443 say it takes too much time to compute a new window start. */
9444 if (CHARPOS (start_pos) - IT_CHARPOS (it)
9445 < XFASTINT (w->height) * XFASTINT (w->width))
9446 {
9447 int min_distance, distance;
9448
9449 /* Move forward by display lines to find the new window
9450 start. If window width was enlarged, the new start can
9451 be expected to be > the old start. If window width was
9452 decreased, the new window start will be < the old start.
9453 So, we're looking for the display line start with the
9454 minimum distance from the old window start. */
9455 pos = it.current.pos;
9456 min_distance = INFINITY;
9457 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
9458 distance < min_distance)
9459 {
9460 min_distance = distance;
9461 pos = it.current.pos;
9462 move_it_by_lines (&it, 1, 0);
9463 }
9464
9465 /* Set the window start there. */
9466 SET_MARKER_FROM_TEXT_POS (w->start, pos);
9467 window_start_changed_p = 1;
9468 }
9469 }
9470
9471 return window_start_changed_p;
9472 }
9473
9474
9475 /* Try cursor movement in case text has not changes in window WINDOW,
9476 with window start STARTP. Value is
9477
9478 1 if successful
9479
9480 0 if this method cannot be used
9481
9482 -1 if we know we have to scroll the display. *SCROLL_STEP is
9483 set to 1, under certain circumstances, if we want to scroll as
9484 if scroll-step were set to 1. See the code. */
9485
9486 static int
9487 try_cursor_movement (window, startp, scroll_step)
9488 Lisp_Object window;
9489 struct text_pos startp;
9490 int *scroll_step;
9491 {
9492 struct window *w = XWINDOW (window);
9493 struct frame *f = XFRAME (w->frame);
9494 int rc = 0;
9495
9496 /* Handle case where text has not changed, only point, and it has
9497 not moved off the frame. */
9498 if (/* Point may be in this window. */
9499 PT >= CHARPOS (startp)
9500 /* Selective display hasn't changed. */
9501 && !current_buffer->clip_changed
9502 /* Function force-mode-line-update is used to force a thorough
9503 redisplay. It sets either windows_or_buffers_changed or
9504 update_mode_lines. So don't take a shortcut here for these
9505 cases. */
9506 && !update_mode_lines
9507 && !windows_or_buffers_changed
9508 /* Can't use this case if highlighting a region. When a
9509 region exists, cursor movement has to do more than just
9510 set the cursor. */
9511 && !(!NILP (Vtransient_mark_mode)
9512 && !NILP (current_buffer->mark_active))
9513 && NILP (w->region_showing)
9514 && NILP (Vshow_trailing_whitespace)
9515 /* Right after splitting windows, last_point may be nil. */
9516 && INTEGERP (w->last_point)
9517 /* This code is not used for mini-buffer for the sake of the case
9518 of redisplaying to replace an echo area message; since in
9519 that case the mini-buffer contents per se are usually
9520 unchanged. This code is of no real use in the mini-buffer
9521 since the handling of this_line_start_pos, etc., in redisplay
9522 handles the same cases. */
9523 && !EQ (window, minibuf_window)
9524 /* When splitting windows or for new windows, it happens that
9525 redisplay is called with a nil window_end_vpos or one being
9526 larger than the window. This should really be fixed in
9527 window.c. I don't have this on my list, now, so we do
9528 approximately the same as the old redisplay code. --gerd. */
9529 && INTEGERP (w->window_end_vpos)
9530 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
9531 && (FRAME_WINDOW_P (f)
9532 || !MARKERP (Voverlay_arrow_position)
9533 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
9534 {
9535 int this_scroll_margin;
9536 struct glyph_row *row;
9537
9538 #if GLYPH_DEBUG
9539 debug_method_add (w, "cursor movement");
9540 #endif
9541
9542 /* Scroll if point within this distance from the top or bottom
9543 of the window. This is a pixel value. */
9544 this_scroll_margin = max (0, scroll_margin);
9545 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
9546 this_scroll_margin *= CANON_Y_UNIT (f);
9547
9548 /* Start with the row the cursor was displayed during the last
9549 not paused redisplay. Give up if that row is not valid. */
9550 if (w->last_cursor.vpos < 0
9551 || w->last_cursor.vpos >= w->current_matrix->nrows)
9552 rc = -1;
9553 else
9554 {
9555 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
9556 if (row->mode_line_p)
9557 ++row;
9558 if (!row->enabled_p)
9559 rc = -1;
9560 }
9561
9562 if (rc == 0)
9563 {
9564 int scroll_p = 0;
9565 int last_y = window_text_bottom_y (w) - this_scroll_margin;
9566
9567 if (PT > XFASTINT (w->last_point))
9568 {
9569 /* Point has moved forward. */
9570 while (MATRIX_ROW_END_CHARPOS (row) < PT
9571 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
9572 {
9573 xassert (row->enabled_p);
9574 ++row;
9575 }
9576
9577 /* The end position of a row equals the start position
9578 of the next row. If PT is there, we would rather
9579 display it in the next line. */
9580 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9581 && MATRIX_ROW_END_CHARPOS (row) == PT
9582 && !cursor_row_p (w, row))
9583 ++row;
9584
9585 /* If within the scroll margin, scroll. Note that
9586 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
9587 the next line would be drawn, and that
9588 this_scroll_margin can be zero. */
9589 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
9590 || PT > MATRIX_ROW_END_CHARPOS (row)
9591 /* Line is completely visible last line in window
9592 and PT is to be set in the next line. */
9593 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
9594 && PT == MATRIX_ROW_END_CHARPOS (row)
9595 && !row->ends_at_zv_p
9596 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
9597 scroll_p = 1;
9598 }
9599 else if (PT < XFASTINT (w->last_point))
9600 {
9601 /* Cursor has to be moved backward. Note that PT >=
9602 CHARPOS (startp) because of the outer
9603 if-statement. */
9604 while (!row->mode_line_p
9605 && (MATRIX_ROW_START_CHARPOS (row) > PT
9606 || (MATRIX_ROW_START_CHARPOS (row) == PT
9607 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
9608 && (row->y > this_scroll_margin
9609 || CHARPOS (startp) == BEGV))
9610 {
9611 xassert (row->enabled_p);
9612 --row;
9613 }
9614
9615 /* Consider the following case: Window starts at BEGV,
9616 there is invisible, intangible text at BEGV, so that
9617 display starts at some point START > BEGV. It can
9618 happen that we are called with PT somewhere between
9619 BEGV and START. Try to handle that case. */
9620 if (row < w->current_matrix->rows
9621 || row->mode_line_p)
9622 {
9623 row = w->current_matrix->rows;
9624 if (row->mode_line_p)
9625 ++row;
9626 }
9627
9628 /* Due to newlines in overlay strings, we may have to
9629 skip forward over overlay strings. */
9630 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9631 && MATRIX_ROW_END_CHARPOS (row) == PT
9632 && !cursor_row_p (w, row))
9633 ++row;
9634
9635 /* If within the scroll margin, scroll. */
9636 if (row->y < this_scroll_margin
9637 && CHARPOS (startp) != BEGV)
9638 scroll_p = 1;
9639 }
9640
9641 if (PT < MATRIX_ROW_START_CHARPOS (row)
9642 || PT > MATRIX_ROW_END_CHARPOS (row))
9643 {
9644 /* if PT is not in the glyph row, give up. */
9645 rc = -1;
9646 }
9647 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
9648 {
9649 if (PT == MATRIX_ROW_END_CHARPOS (row)
9650 && !row->ends_at_zv_p
9651 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
9652 rc = -1;
9653 else if (row->height > window_box_height (w))
9654 {
9655 /* If we end up in a partially visible line, let's
9656 make it fully visible, except when it's taller
9657 than the window, in which case we can't do much
9658 about it. */
9659 *scroll_step = 1;
9660 rc = -1;
9661 }
9662 else
9663 {
9664 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9665 try_window (window, startp);
9666 make_cursor_line_fully_visible (w);
9667 rc = 1;
9668 }
9669 }
9670 else if (scroll_p)
9671 rc = -1;
9672 else
9673 {
9674 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9675 rc = 1;
9676 }
9677 }
9678 }
9679
9680 return rc;
9681 }
9682
9683
9684 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
9685 selected_window is redisplayed. */
9686
9687 static void
9688 redisplay_window (window, just_this_one_p)
9689 Lisp_Object window;
9690 int just_this_one_p;
9691 {
9692 struct window *w = XWINDOW (window);
9693 struct frame *f = XFRAME (w->frame);
9694 struct buffer *buffer = XBUFFER (w->buffer);
9695 struct buffer *old = current_buffer;
9696 struct text_pos lpoint, opoint, startp;
9697 int update_mode_line;
9698 int tem;
9699 struct it it;
9700 /* Record it now because it's overwritten. */
9701 int current_matrix_up_to_date_p = 0;
9702 int temp_scroll_step = 0;
9703 int count = BINDING_STACK_SIZE ();
9704 int rc;
9705
9706 SET_TEXT_POS (lpoint, PT, PT_BYTE);
9707 opoint = lpoint;
9708
9709 /* W must be a leaf window here. */
9710 xassert (!NILP (w->buffer));
9711 #if GLYPH_DEBUG
9712 *w->desired_matrix->method = 0;
9713 #endif
9714
9715 specbind (Qinhibit_point_motion_hooks, Qt);
9716
9717 reconsider_clip_changes (w, buffer);
9718
9719 /* Has the mode line to be updated? */
9720 update_mode_line = (!NILP (w->update_mode_line)
9721 || update_mode_lines
9722 || buffer->clip_changed);
9723
9724 if (MINI_WINDOW_P (w))
9725 {
9726 if (w == XWINDOW (echo_area_window)
9727 && !NILP (echo_area_buffer[0]))
9728 {
9729 if (update_mode_line)
9730 /* We may have to update a tty frame's menu bar or a
9731 tool-bar. Example `M-x C-h C-h C-g'. */
9732 goto finish_menu_bars;
9733 else
9734 /* We've already displayed the echo area glyphs in this window. */
9735 goto finish_scroll_bars;
9736 }
9737 else if (w != XWINDOW (minibuf_window))
9738 {
9739 /* W is a mini-buffer window, but it's not the currently
9740 active one, so clear it. */
9741 int yb = window_text_bottom_y (w);
9742 struct glyph_row *row;
9743 int y;
9744
9745 for (y = 0, row = w->desired_matrix->rows;
9746 y < yb;
9747 y += row->height, ++row)
9748 blank_row (w, row, y);
9749 goto finish_scroll_bars;
9750 }
9751 }
9752
9753 /* Otherwise set up data on this window; select its buffer and point
9754 value. */
9755 /* Really select the buffer, for the sake of buffer-local
9756 variables. */
9757 set_buffer_internal_1 (XBUFFER (w->buffer));
9758 SET_TEXT_POS (opoint, PT, PT_BYTE);
9759
9760 current_matrix_up_to_date_p
9761 = (!NILP (w->window_end_valid)
9762 && !current_buffer->clip_changed
9763 && XFASTINT (w->last_modified) >= MODIFF
9764 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
9765
9766 /* When windows_or_buffers_changed is non-zero, we can't rely on
9767 the window end being valid, so set it to nil there. */
9768 if (windows_or_buffers_changed)
9769 {
9770 /* If window starts on a continuation line, maybe adjust the
9771 window start in case the window's width changed. */
9772 if (XMARKER (w->start)->buffer == current_buffer)
9773 compute_window_start_on_continuation_line (w);
9774
9775 w->window_end_valid = Qnil;
9776 }
9777
9778 /* Some sanity checks. */
9779 CHECK_WINDOW_END (w);
9780 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
9781 abort ();
9782 if (BYTEPOS (opoint) < CHARPOS (opoint))
9783 abort ();
9784
9785 /* If %c is in mode line, update it if needed. */
9786 if (!NILP (w->column_number_displayed)
9787 /* This alternative quickly identifies a common case
9788 where no change is needed. */
9789 && !(PT == XFASTINT (w->last_point)
9790 && XFASTINT (w->last_modified) >= MODIFF
9791 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
9792 && XFASTINT (w->column_number_displayed) != current_column ())
9793 update_mode_line = 1;
9794
9795 /* Count number of windows showing the selected buffer. An indirect
9796 buffer counts as its base buffer. */
9797 if (!just_this_one_p)
9798 {
9799 struct buffer *current_base, *window_base;
9800 current_base = current_buffer;
9801 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
9802 if (current_base->base_buffer)
9803 current_base = current_base->base_buffer;
9804 if (window_base->base_buffer)
9805 window_base = window_base->base_buffer;
9806 if (current_base == window_base)
9807 buffer_shared++;
9808 }
9809
9810 /* Point refers normally to the selected window. For any other
9811 window, set up appropriate value. */
9812 if (!EQ (window, selected_window))
9813 {
9814 int new_pt = XMARKER (w->pointm)->charpos;
9815 int new_pt_byte = marker_byte_position (w->pointm);
9816 if (new_pt < BEGV)
9817 {
9818 new_pt = BEGV;
9819 new_pt_byte = BEGV_BYTE;
9820 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
9821 }
9822 else if (new_pt > (ZV - 1))
9823 {
9824 new_pt = ZV;
9825 new_pt_byte = ZV_BYTE;
9826 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
9827 }
9828
9829 /* We don't use SET_PT so that the point-motion hooks don't run. */
9830 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
9831 }
9832
9833 /* If any of the character widths specified in the display table
9834 have changed, invalidate the width run cache. It's true that
9835 this may be a bit late to catch such changes, but the rest of
9836 redisplay goes (non-fatally) haywire when the display table is
9837 changed, so why should we worry about doing any better? */
9838 if (current_buffer->width_run_cache)
9839 {
9840 struct Lisp_Char_Table *disptab = buffer_display_table ();
9841
9842 if (! disptab_matches_widthtab (disptab,
9843 XVECTOR (current_buffer->width_table)))
9844 {
9845 invalidate_region_cache (current_buffer,
9846 current_buffer->width_run_cache,
9847 BEG, Z);
9848 recompute_width_table (current_buffer, disptab);
9849 }
9850 }
9851
9852 /* If window-start is screwed up, choose a new one. */
9853 if (XMARKER (w->start)->buffer != current_buffer)
9854 goto recenter;
9855
9856 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9857
9858 /* If someone specified a new starting point but did not insist,
9859 check whether it can be used. */
9860 if (!NILP (w->optional_new_start)
9861 && CHARPOS (startp) >= BEGV
9862 && CHARPOS (startp) <= ZV)
9863 {
9864 w->optional_new_start = Qnil;
9865 start_display (&it, w, startp);
9866 move_it_to (&it, PT, 0, it.last_visible_y, -1,
9867 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9868 if (IT_CHARPOS (it) == PT)
9869 w->force_start = Qt;
9870 }
9871
9872 /* Handle case where place to start displaying has been specified,
9873 unless the specified location is outside the accessible range. */
9874 if (!NILP (w->force_start)
9875 || w->frozen_window_start_p)
9876 {
9877 w->force_start = Qnil;
9878 w->vscroll = 0;
9879 w->window_end_valid = Qnil;
9880
9881 /* Forget any recorded base line for line number display. */
9882 if (!current_matrix_up_to_date_p
9883 || current_buffer->clip_changed)
9884 w->base_line_number = Qnil;
9885
9886 /* Redisplay the mode line. Select the buffer properly for that.
9887 Also, run the hook window-scroll-functions
9888 because we have scrolled. */
9889 /* Note, we do this after clearing force_start because
9890 if there's an error, it is better to forget about force_start
9891 than to get into an infinite loop calling the hook functions
9892 and having them get more errors. */
9893 if (!update_mode_line
9894 || ! NILP (Vwindow_scroll_functions))
9895 {
9896 update_mode_line = 1;
9897 w->update_mode_line = Qt;
9898 startp = run_window_scroll_functions (window, startp);
9899 }
9900
9901 w->last_modified = make_number (0);
9902 w->last_overlay_modified = make_number (0);
9903 if (CHARPOS (startp) < BEGV)
9904 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
9905 else if (CHARPOS (startp) > ZV)
9906 SET_TEXT_POS (startp, ZV, ZV_BYTE);
9907
9908 /* Redisplay, then check if cursor has been set during the
9909 redisplay. Give up if new fonts were loaded. */
9910 if (!try_window (window, startp))
9911 {
9912 w->force_start = Qt;
9913 clear_glyph_matrix (w->desired_matrix);
9914 goto finish_scroll_bars;
9915 }
9916
9917 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
9918 {
9919 /* If point does not appear, try to move point so it does
9920 appear. The desired matrix has been built above, so we
9921 can use it here. */
9922 int window_height;
9923 struct glyph_row *row;
9924
9925 window_height = window_box_height (w) / 2;
9926 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
9927 while (MATRIX_ROW_BOTTOM_Y (row) < window_height)
9928 ++row;
9929
9930 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
9931 MATRIX_ROW_START_BYTEPOS (row));
9932
9933 if (w != XWINDOW (selected_window))
9934 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
9935 else if (current_buffer == old)
9936 SET_TEXT_POS (lpoint, PT, PT_BYTE);
9937
9938 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
9939
9940 /* If we are highlighting the region, then we just changed
9941 the region, so redisplay to show it. */
9942 if (!NILP (Vtransient_mark_mode)
9943 && !NILP (current_buffer->mark_active))
9944 {
9945 clear_glyph_matrix (w->desired_matrix);
9946 if (!try_window (window, startp))
9947 goto finish_scroll_bars;
9948 }
9949 }
9950
9951 make_cursor_line_fully_visible (w);
9952 #if GLYPH_DEBUG
9953 debug_method_add (w, "forced window start");
9954 #endif
9955 goto done;
9956 }
9957
9958 /* Handle case where text has not changed, only point, and it has
9959 not moved off the frame. */
9960 if (current_matrix_up_to_date_p
9961 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
9962 rc != 0))
9963 {
9964 if (rc == -1)
9965 goto try_to_scroll;
9966 else
9967 goto done;
9968 }
9969 /* If current starting point was originally the beginning of a line
9970 but no longer is, find a new starting point. */
9971 else if (!NILP (w->start_at_line_beg)
9972 && !(CHARPOS (startp) <= BEGV
9973 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
9974 {
9975 #if GLYPH_DEBUG
9976 debug_method_add (w, "recenter 1");
9977 #endif
9978 goto recenter;
9979 }
9980
9981 /* Try scrolling with try_window_id. */
9982 else if (/* Windows and buffers haven't changed. */
9983 !windows_or_buffers_changed
9984 /* Window must be either use window-based redisplay or
9985 be full width. */
9986 && (FRAME_WINDOW_P (f)
9987 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)))
9988 && !MINI_WINDOW_P (w)
9989 /* Point is not known NOT to appear in window. */
9990 && PT >= CHARPOS (startp)
9991 && XFASTINT (w->last_modified)
9992 /* Window is not hscrolled. */
9993 && XFASTINT (w->hscroll) == 0
9994 /* Selective display has not changed. */
9995 && !current_buffer->clip_changed
9996 /* Current matrix is up to date. */
9997 && !NILP (w->window_end_valid)
9998 /* Can't use this case if highlighting a region because
9999 a cursor movement will do more than just set the cursor. */
10000 && !(!NILP (Vtransient_mark_mode)
10001 && !NILP (current_buffer->mark_active))
10002 && NILP (w->region_showing)
10003 && NILP (Vshow_trailing_whitespace)
10004 /* Overlay arrow position and string not changed. */
10005 && EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
10006 && EQ (last_arrow_string, Voverlay_arrow_string)
10007 /* Value is > 0 if update has been done, it is -1 if we
10008 know that the same window start will not work. It is 0
10009 if unsuccessful for some other reason. */
10010 && (tem = try_window_id (w)) != 0)
10011 {
10012 #if GLYPH_DEBUG
10013 debug_method_add (w, "try_window_id %d", tem);
10014 #endif
10015
10016 if (fonts_changed_p)
10017 goto finish_scroll_bars;
10018 if (tem > 0)
10019 goto done;
10020
10021 /* Otherwise try_window_id has returned -1 which means that we
10022 don't want the alternative below this comment to execute. */
10023 }
10024 else if (CHARPOS (startp) >= BEGV
10025 && CHARPOS (startp) <= ZV
10026 && PT >= CHARPOS (startp)
10027 && (CHARPOS (startp) < ZV
10028 /* Avoid starting at end of buffer. */
10029 || CHARPOS (startp) == BEGV
10030 || (XFASTINT (w->last_modified) >= MODIFF
10031 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
10032 {
10033 #if GLYPH_DEBUG
10034 debug_method_add (w, "same window start");
10035 #endif
10036
10037 /* Try to redisplay starting at same place as before.
10038 If point has not moved off frame, accept the results. */
10039 if (!current_matrix_up_to_date_p
10040 /* Don't use try_window_reusing_current_matrix in this case
10041 because a window scroll function can have changed the
10042 buffer. */
10043 || !NILP (Vwindow_scroll_functions)
10044 || MINI_WINDOW_P (w)
10045 || !try_window_reusing_current_matrix (w))
10046 {
10047 IF_DEBUG (debug_method_add (w, "1"));
10048 try_window (window, startp);
10049 }
10050
10051 if (fonts_changed_p)
10052 goto finish_scroll_bars;
10053
10054 if (w->cursor.vpos >= 0)
10055 {
10056 if (!just_this_one_p
10057 || current_buffer->clip_changed
10058 || BEG_UNCHANGED < CHARPOS (startp))
10059 /* Forget any recorded base line for line number display. */
10060 w->base_line_number = Qnil;
10061
10062 make_cursor_line_fully_visible (w);
10063 goto done;
10064 }
10065 else
10066 clear_glyph_matrix (w->desired_matrix);
10067 }
10068
10069 try_to_scroll:
10070
10071 w->last_modified = make_number (0);
10072 w->last_overlay_modified = make_number (0);
10073
10074 /* Redisplay the mode line. Select the buffer properly for that. */
10075 if (!update_mode_line)
10076 {
10077 update_mode_line = 1;
10078 w->update_mode_line = Qt;
10079 }
10080
10081 /* Try to scroll by specified few lines. */
10082 if ((scroll_conservatively
10083 || scroll_step
10084 || temp_scroll_step
10085 || NUMBERP (current_buffer->scroll_up_aggressively)
10086 || NUMBERP (current_buffer->scroll_down_aggressively))
10087 && !current_buffer->clip_changed
10088 && CHARPOS (startp) >= BEGV
10089 && CHARPOS (startp) <= ZV)
10090 {
10091 /* The function returns -1 if new fonts were loaded, 1 if
10092 successful, 0 if not successful. */
10093 int rc = try_scrolling (window, just_this_one_p,
10094 scroll_conservatively,
10095 scroll_step,
10096 temp_scroll_step);
10097 if (rc > 0)
10098 goto done;
10099 else if (rc < 0)
10100 goto finish_scroll_bars;
10101 }
10102
10103 /* Finally, just choose place to start which centers point */
10104
10105 recenter:
10106
10107 #if GLYPH_DEBUG
10108 debug_method_add (w, "recenter");
10109 #endif
10110
10111 /* w->vscroll = 0; */
10112
10113 /* Forget any previously recorded base line for line number display. */
10114 if (!current_matrix_up_to_date_p
10115 || current_buffer->clip_changed)
10116 w->base_line_number = Qnil;
10117
10118 /* Move backward half the height of the window. */
10119 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10120 it.current_y = it.last_visible_y;
10121 move_it_vertically_backward (&it, it.last_visible_y / 2);
10122 xassert (IT_CHARPOS (it) >= BEGV);
10123
10124 /* The function move_it_vertically_backward may move over more
10125 than the specified y-distance. If it->w is small, e.g. a
10126 mini-buffer window, we may end up in front of the window's
10127 display area. Start displaying at the start of the line
10128 containing PT in this case. */
10129 if (it.current_y <= 0)
10130 {
10131 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10132 move_it_vertically (&it, 0);
10133 xassert (IT_CHARPOS (it) <= PT);
10134 it.current_y = 0;
10135 }
10136
10137 it.current_x = it.hpos = 0;
10138
10139 /* Set startp here explicitly in case that helps avoid an infinite loop
10140 in case the window-scroll-functions functions get errors. */
10141 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
10142
10143 /* Run scroll hooks. */
10144 startp = run_window_scroll_functions (window, it.current.pos);
10145
10146 /* Redisplay the window. */
10147 if (!current_matrix_up_to_date_p
10148 || windows_or_buffers_changed
10149 /* Don't use try_window_reusing_current_matrix in this case
10150 because it can have changed the buffer. */
10151 || !NILP (Vwindow_scroll_functions)
10152 || !just_this_one_p
10153 || MINI_WINDOW_P (w)
10154 || !try_window_reusing_current_matrix (w))
10155 try_window (window, startp);
10156
10157 /* If new fonts have been loaded (due to fontsets), give up. We
10158 have to start a new redisplay since we need to re-adjust glyph
10159 matrices. */
10160 if (fonts_changed_p)
10161 goto finish_scroll_bars;
10162
10163 /* If cursor did not appear assume that the middle of the window is
10164 in the first line of the window. Do it again with the next line.
10165 (Imagine a window of height 100, displaying two lines of height
10166 60. Moving back 50 from it->last_visible_y will end in the first
10167 line.) */
10168 if (w->cursor.vpos < 0)
10169 {
10170 if (!NILP (w->window_end_valid)
10171 && PT >= Z - XFASTINT (w->window_end_pos))
10172 {
10173 clear_glyph_matrix (w->desired_matrix);
10174 move_it_by_lines (&it, 1, 0);
10175 try_window (window, it.current.pos);
10176 }
10177 else if (PT < IT_CHARPOS (it))
10178 {
10179 clear_glyph_matrix (w->desired_matrix);
10180 move_it_by_lines (&it, -1, 0);
10181 try_window (window, it.current.pos);
10182 }
10183 else
10184 {
10185 /* Not much we can do about it. */
10186 }
10187 }
10188
10189 /* Consider the following case: Window starts at BEGV, there is
10190 invisible, intangible text at BEGV, so that display starts at
10191 some point START > BEGV. It can happen that we are called with
10192 PT somewhere between BEGV and START. Try to handle that case. */
10193 if (w->cursor.vpos < 0)
10194 {
10195 struct glyph_row *row = w->current_matrix->rows;
10196 if (row->mode_line_p)
10197 ++row;
10198 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10199 }
10200
10201 make_cursor_line_fully_visible (w);
10202
10203 done:
10204
10205 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10206 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
10207 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
10208 ? Qt : Qnil);
10209
10210 /* Display the mode line, if we must. */
10211 if ((update_mode_line
10212 /* If window not full width, must redo its mode line
10213 if (a) the window to its side is being redone and
10214 (b) we do a frame-based redisplay. This is a consequence
10215 of how inverted lines are drawn in frame-based redisplay. */
10216 || (!just_this_one_p
10217 && !FRAME_WINDOW_P (f)
10218 && !WINDOW_FULL_WIDTH_P (w))
10219 /* Line number to display. */
10220 || INTEGERP (w->base_line_pos)
10221 /* Column number is displayed and different from the one displayed. */
10222 || (!NILP (w->column_number_displayed)
10223 && XFASTINT (w->column_number_displayed) != current_column ()))
10224 /* This means that the window has a mode line. */
10225 && (WINDOW_WANTS_MODELINE_P (w)
10226 || WINDOW_WANTS_HEADER_LINE_P (w)))
10227 {
10228 Lisp_Object old_selected_frame;
10229
10230 old_selected_frame = selected_frame;
10231
10232 XSETFRAME (selected_frame, f);
10233 display_mode_lines (w);
10234 selected_frame = old_selected_frame;
10235
10236 /* If mode line height has changed, arrange for a thorough
10237 immediate redisplay using the correct mode line height. */
10238 if (WINDOW_WANTS_MODELINE_P (w)
10239 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
10240 {
10241 fonts_changed_p = 1;
10242 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
10243 = DESIRED_MODE_LINE_HEIGHT (w);
10244 }
10245
10246 /* If top line height has changed, arrange for a thorough
10247 immediate redisplay using the correct mode line height. */
10248 if (WINDOW_WANTS_HEADER_LINE_P (w)
10249 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
10250 {
10251 fonts_changed_p = 1;
10252 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
10253 = DESIRED_HEADER_LINE_HEIGHT (w);
10254 }
10255
10256 if (fonts_changed_p)
10257 goto finish_scroll_bars;
10258 }
10259
10260 if (!line_number_displayed
10261 && !BUFFERP (w->base_line_pos))
10262 {
10263 w->base_line_pos = Qnil;
10264 w->base_line_number = Qnil;
10265 }
10266
10267 finish_menu_bars:
10268
10269 /* When we reach a frame's selected window, redo the frame's menu bar. */
10270 if (update_mode_line
10271 && EQ (FRAME_SELECTED_WINDOW (f), window))
10272 {
10273 int redisplay_menu_p = 0;
10274
10275 if (FRAME_WINDOW_P (f))
10276 {
10277 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
10278 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
10279 #else
10280 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10281 #endif
10282 }
10283 else
10284 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10285
10286 if (redisplay_menu_p)
10287 display_menu_bar (w);
10288
10289 #ifdef HAVE_WINDOW_SYSTEM
10290 if (WINDOWP (f->tool_bar_window)
10291 && (FRAME_TOOL_BAR_LINES (f) > 0
10292 || auto_resize_tool_bars_p))
10293 redisplay_tool_bar (f);
10294 #endif
10295 }
10296
10297 finish_scroll_bars:
10298
10299 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
10300 {
10301 int start, end, whole;
10302
10303 /* Calculate the start and end positions for the current window.
10304 At some point, it would be nice to choose between scrollbars
10305 which reflect the whole buffer size, with special markers
10306 indicating narrowing, and scrollbars which reflect only the
10307 visible region.
10308
10309 Note that mini-buffers sometimes aren't displaying any text. */
10310 if (!MINI_WINDOW_P (w)
10311 || (w == XWINDOW (minibuf_window)
10312 && NILP (echo_area_buffer[0])))
10313 {
10314 whole = ZV - BEGV;
10315 start = marker_position (w->start) - BEGV;
10316 /* I don't think this is guaranteed to be right. For the
10317 moment, we'll pretend it is. */
10318 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
10319
10320 if (end < start)
10321 end = start;
10322 if (whole < (end - start))
10323 whole = end - start;
10324 }
10325 else
10326 start = end = whole = 0;
10327
10328 /* Indicate what this scroll bar ought to be displaying now. */
10329 set_vertical_scroll_bar_hook (w, end - start, whole, start);
10330
10331 /* Note that we actually used the scroll bar attached to this
10332 window, so it shouldn't be deleted at the end of redisplay. */
10333 redeem_scroll_bar_hook (w);
10334 }
10335
10336 /* Restore current_buffer and value of point in it. */
10337 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
10338 set_buffer_internal_1 (old);
10339 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
10340
10341 unbind_to (count, Qnil);
10342 }
10343
10344
10345 /* Build the complete desired matrix of WINDOW with a window start
10346 buffer position POS. Value is non-zero if successful. It is zero
10347 if fonts were loaded during redisplay which makes re-adjusting
10348 glyph matrices necessary. */
10349
10350 int
10351 try_window (window, pos)
10352 Lisp_Object window;
10353 struct text_pos pos;
10354 {
10355 struct window *w = XWINDOW (window);
10356 struct it it;
10357 struct glyph_row *last_text_row = NULL;
10358
10359 /* Make POS the new window start. */
10360 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
10361
10362 /* Mark cursor position as unknown. No overlay arrow seen. */
10363 w->cursor.vpos = -1;
10364 overlay_arrow_seen = 0;
10365
10366 /* Initialize iterator and info to start at POS. */
10367 start_display (&it, w, pos);
10368
10369 /* Display all lines of W. */
10370 while (it.current_y < it.last_visible_y)
10371 {
10372 if (display_line (&it))
10373 last_text_row = it.glyph_row - 1;
10374 if (fonts_changed_p)
10375 return 0;
10376 }
10377
10378 /* If bottom moved off end of frame, change mode line percentage. */
10379 if (XFASTINT (w->window_end_pos) <= 0
10380 && Z != IT_CHARPOS (it))
10381 w->update_mode_line = Qt;
10382
10383 /* Set window_end_pos to the offset of the last character displayed
10384 on the window from the end of current_buffer. Set
10385 window_end_vpos to its row number. */
10386 if (last_text_row)
10387 {
10388 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
10389 w->window_end_bytepos
10390 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10391 w->window_end_pos
10392 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10393 w->window_end_vpos
10394 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10395 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
10396 ->displays_text_p);
10397 }
10398 else
10399 {
10400 w->window_end_bytepos = 0;
10401 w->window_end_pos = w->window_end_vpos = make_number (0);
10402 }
10403
10404 /* But that is not valid info until redisplay finishes. */
10405 w->window_end_valid = Qnil;
10406 return 1;
10407 }
10408
10409
10410 \f
10411 /************************************************************************
10412 Window redisplay reusing current matrix when buffer has not changed
10413 ************************************************************************/
10414
10415 /* Try redisplay of window W showing an unchanged buffer with a
10416 different window start than the last time it was displayed by
10417 reusing its current matrix. Value is non-zero if successful.
10418 W->start is the new window start. */
10419
10420 static int
10421 try_window_reusing_current_matrix (w)
10422 struct window *w;
10423 {
10424 struct frame *f = XFRAME (w->frame);
10425 struct glyph_row *row, *bottom_row;
10426 struct it it;
10427 struct run run;
10428 struct text_pos start, new_start;
10429 int nrows_scrolled, i;
10430 struct glyph_row *last_text_row;
10431 struct glyph_row *last_reused_text_row;
10432 struct glyph_row *start_row;
10433 int start_vpos, min_y, max_y;
10434
10435 if (/* This function doesn't handle terminal frames. */
10436 !FRAME_WINDOW_P (f)
10437 /* Don't try to reuse the display if windows have been split
10438 or such. */
10439 || windows_or_buffers_changed)
10440 return 0;
10441
10442 /* Can't do this if region may have changed. */
10443 if ((!NILP (Vtransient_mark_mode)
10444 && !NILP (current_buffer->mark_active))
10445 || !NILP (w->region_showing)
10446 || !NILP (Vshow_trailing_whitespace))
10447 return 0;
10448
10449 /* If top-line visibility has changed, give up. */
10450 if (WINDOW_WANTS_HEADER_LINE_P (w)
10451 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
10452 return 0;
10453
10454 /* Give up if old or new display is scrolled vertically. We could
10455 make this function handle this, but right now it doesn't. */
10456 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10457 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
10458 return 0;
10459
10460 /* The variable new_start now holds the new window start. The old
10461 start `start' can be determined from the current matrix. */
10462 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
10463 start = start_row->start.pos;
10464 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
10465
10466 /* Clear the desired matrix for the display below. */
10467 clear_glyph_matrix (w->desired_matrix);
10468
10469 if (CHARPOS (new_start) <= CHARPOS (start))
10470 {
10471 int first_row_y;
10472
10473 /* Don't use this method if the display starts with an ellipsis
10474 displayed for invisible text. It's not easy to handle that case
10475 below, and it's certainly not worth the effort since this is
10476 not a frequent case. */
10477 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
10478 return 0;
10479
10480 IF_DEBUG (debug_method_add (w, "twu1"));
10481
10482 /* Display up to a row that can be reused. The variable
10483 last_text_row is set to the last row displayed that displays
10484 text. Note that it.vpos == 0 if or if not there is a
10485 header-line; it's not the same as the MATRIX_ROW_VPOS! */
10486 start_display (&it, w, new_start);
10487 first_row_y = it.current_y;
10488 w->cursor.vpos = -1;
10489 last_text_row = last_reused_text_row = NULL;
10490
10491 while (it.current_y < it.last_visible_y
10492 && IT_CHARPOS (it) < CHARPOS (start)
10493 && !fonts_changed_p)
10494 if (display_line (&it))
10495 last_text_row = it.glyph_row - 1;
10496
10497 /* A value of current_y < last_visible_y means that we stopped
10498 at the previous window start, which in turn means that we
10499 have at least one reusable row. */
10500 if (it.current_y < it.last_visible_y)
10501 {
10502 /* IT.vpos always starts from 0; it counts text lines. */
10503 nrows_scrolled = it.vpos;
10504
10505 /* Find PT if not already found in the lines displayed. */
10506 if (w->cursor.vpos < 0)
10507 {
10508 int dy = it.current_y - first_row_y;
10509
10510 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10511 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10512 {
10513 if (PT >= MATRIX_ROW_START_CHARPOS (row)
10514 && PT < MATRIX_ROW_END_CHARPOS (row))
10515 {
10516 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
10517 dy, nrows_scrolled);
10518 break;
10519 }
10520
10521 if (MATRIX_ROW_BOTTOM_Y (row) + dy >= it.last_visible_y)
10522 break;
10523
10524 ++row;
10525 }
10526
10527 /* Give up if point was not found. This shouldn't
10528 happen often; not more often than with try_window
10529 itself. */
10530 if (w->cursor.vpos < 0)
10531 {
10532 clear_glyph_matrix (w->desired_matrix);
10533 return 0;
10534 }
10535 }
10536
10537 /* Scroll the display. Do it before the current matrix is
10538 changed. The problem here is that update has not yet
10539 run, i.e. part of the current matrix is not up to date.
10540 scroll_run_hook will clear the cursor, and use the
10541 current matrix to get the height of the row the cursor is
10542 in. */
10543 run.current_y = first_row_y;
10544 run.desired_y = it.current_y;
10545 run.height = it.last_visible_y - it.current_y;
10546
10547 if (run.height > 0 && run.current_y != run.desired_y)
10548 {
10549 update_begin (f);
10550 rif->update_window_begin_hook (w);
10551 rif->clear_mouse_face (w);
10552 rif->scroll_run_hook (w, &run);
10553 rif->update_window_end_hook (w, 0, 0);
10554 update_end (f);
10555 }
10556
10557 /* Shift current matrix down by nrows_scrolled lines. */
10558 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
10559 rotate_matrix (w->current_matrix,
10560 start_vpos,
10561 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10562 nrows_scrolled);
10563
10564 /* Disable lines that must be updated. */
10565 for (i = 0; i < it.vpos; ++i)
10566 (start_row + i)->enabled_p = 0;
10567
10568 /* Re-compute Y positions. */
10569 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10570 max_y = it.last_visible_y;
10571 for (row = start_row + nrows_scrolled;
10572 row < bottom_row;
10573 ++row)
10574 {
10575 row->y = it.current_y;
10576
10577 if (row->y < min_y)
10578 row->visible_height = row->height - (min_y - row->y);
10579 else if (row->y + row->height > max_y)
10580 row->visible_height
10581 = row->height - (row->y + row->height - max_y);
10582 else
10583 row->visible_height = row->height;
10584
10585 it.current_y += row->height;
10586
10587 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10588 last_reused_text_row = row;
10589 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
10590 break;
10591 }
10592
10593 /* Disable lines in the current matrix which are now
10594 below the window. */
10595 for (++row; row < bottom_row; ++row)
10596 row->enabled_p = 0;
10597 }
10598
10599 /* Update window_end_pos etc.; last_reused_text_row is the last
10600 reused row from the current matrix containing text, if any.
10601 The value of last_text_row is the last displayed line
10602 containing text. */
10603 if (last_reused_text_row)
10604 {
10605 w->window_end_bytepos
10606 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
10607 w->window_end_pos
10608 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
10609 w->window_end_vpos
10610 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
10611 w->current_matrix));
10612 }
10613 else if (last_text_row)
10614 {
10615 w->window_end_bytepos
10616 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10617 w->window_end_pos
10618 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10619 w->window_end_vpos
10620 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10621 }
10622 else
10623 {
10624 /* This window must be completely empty. */
10625 w->window_end_bytepos = 0;
10626 w->window_end_pos = w->window_end_vpos = make_number (0);
10627 }
10628 w->window_end_valid = Qnil;
10629
10630 /* Update hint: don't try scrolling again in update_window. */
10631 w->desired_matrix->no_scrolling_p = 1;
10632
10633 #if GLYPH_DEBUG
10634 debug_method_add (w, "try_window_reusing_current_matrix 1");
10635 #endif
10636 return 1;
10637 }
10638 else if (CHARPOS (new_start) > CHARPOS (start))
10639 {
10640 struct glyph_row *pt_row, *row;
10641 struct glyph_row *first_reusable_row;
10642 struct glyph_row *first_row_to_display;
10643 int dy;
10644 int yb = window_text_bottom_y (w);
10645
10646 /* Find the row starting at new_start, if there is one. Don't
10647 reuse a partially visible line at the end. */
10648 first_reusable_row = start_row;
10649 while (first_reusable_row->enabled_p
10650 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
10651 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
10652 < CHARPOS (new_start)))
10653 ++first_reusable_row;
10654
10655 /* Give up if there is no row to reuse. */
10656 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
10657 || !first_reusable_row->enabled_p
10658 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
10659 != CHARPOS (new_start)))
10660 return 0;
10661
10662 /* We can reuse fully visible rows beginning with
10663 first_reusable_row to the end of the window. Set
10664 first_row_to_display to the first row that cannot be reused.
10665 Set pt_row to the row containing point, if there is any. */
10666 pt_row = NULL;
10667 for (first_row_to_display = first_reusable_row;
10668 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
10669 ++first_row_to_display)
10670 {
10671 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
10672 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
10673 pt_row = first_row_to_display;
10674 }
10675
10676 /* Start displaying at the start of first_row_to_display. */
10677 xassert (first_row_to_display->y < yb);
10678 init_to_row_start (&it, w, first_row_to_display);
10679
10680 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
10681 - start_vpos);
10682 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
10683 - nrows_scrolled);
10684 it.current_y = (first_row_to_display->y - first_reusable_row->y
10685 + WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
10686
10687 /* Display lines beginning with first_row_to_display in the
10688 desired matrix. Set last_text_row to the last row displayed
10689 that displays text. */
10690 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
10691 if (pt_row == NULL)
10692 w->cursor.vpos = -1;
10693 last_text_row = NULL;
10694 while (it.current_y < it.last_visible_y && !fonts_changed_p)
10695 if (display_line (&it))
10696 last_text_row = it.glyph_row - 1;
10697
10698 /* Give up If point isn't in a row displayed or reused. */
10699 if (w->cursor.vpos < 0)
10700 {
10701 clear_glyph_matrix (w->desired_matrix);
10702 return 0;
10703 }
10704
10705 /* If point is in a reused row, adjust y and vpos of the cursor
10706 position. */
10707 if (pt_row)
10708 {
10709 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
10710 w->current_matrix);
10711 w->cursor.y -= first_reusable_row->y;
10712 }
10713
10714 /* Scroll the display. */
10715 run.current_y = first_reusable_row->y;
10716 run.desired_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10717 run.height = it.last_visible_y - run.current_y;
10718 dy = run.current_y - run.desired_y;
10719
10720 if (run.height)
10721 {
10722 struct frame *f = XFRAME (WINDOW_FRAME (w));
10723 update_begin (f);
10724 rif->update_window_begin_hook (w);
10725 rif->clear_mouse_face (w);
10726 rif->scroll_run_hook (w, &run);
10727 rif->update_window_end_hook (w, 0, 0);
10728 update_end (f);
10729 }
10730
10731 /* Adjust Y positions of reused rows. */
10732 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
10733 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10734 max_y = it.last_visible_y;
10735 for (row = first_reusable_row; row < first_row_to_display; ++row)
10736 {
10737 row->y -= dy;
10738 if (row->y < min_y)
10739 row->visible_height = row->height - (min_y - row->y);
10740 else if (row->y + row->height > max_y)
10741 row->visible_height
10742 = row->height - (row->y + row->height - max_y);
10743 else
10744 row->visible_height = row->height;
10745 }
10746
10747 /* Scroll the current matrix. */
10748 xassert (nrows_scrolled > 0);
10749 rotate_matrix (w->current_matrix,
10750 start_vpos,
10751 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10752 -nrows_scrolled);
10753
10754 /* Disable rows not reused. */
10755 for (row -= nrows_scrolled; row < bottom_row; ++row)
10756 row->enabled_p = 0;
10757
10758 /* Adjust window end. A null value of last_text_row means that
10759 the window end is in reused rows which in turn means that
10760 only its vpos can have changed. */
10761 if (last_text_row)
10762 {
10763 w->window_end_bytepos
10764 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10765 w->window_end_pos
10766 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10767 w->window_end_vpos
10768 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10769 }
10770 else
10771 {
10772 w->window_end_vpos
10773 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
10774 }
10775
10776 w->window_end_valid = Qnil;
10777 w->desired_matrix->no_scrolling_p = 1;
10778
10779 #if GLYPH_DEBUG
10780 debug_method_add (w, "try_window_reusing_current_matrix 2");
10781 #endif
10782 return 1;
10783 }
10784
10785 return 0;
10786 }
10787
10788
10789 \f
10790 /************************************************************************
10791 Window redisplay reusing current matrix when buffer has changed
10792 ************************************************************************/
10793
10794 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
10795 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
10796 int *, int *));
10797 static struct glyph_row *
10798 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
10799 struct glyph_row *));
10800
10801
10802 /* Return the last row in MATRIX displaying text. If row START is
10803 non-null, start searching with that row. IT gives the dimensions
10804 of the display. Value is null if matrix is empty; otherwise it is
10805 a pointer to the row found. */
10806
10807 static struct glyph_row *
10808 find_last_row_displaying_text (matrix, it, start)
10809 struct glyph_matrix *matrix;
10810 struct it *it;
10811 struct glyph_row *start;
10812 {
10813 struct glyph_row *row, *row_found;
10814
10815 /* Set row_found to the last row in IT->w's current matrix
10816 displaying text. The loop looks funny but think of partially
10817 visible lines. */
10818 row_found = NULL;
10819 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
10820 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10821 {
10822 xassert (row->enabled_p);
10823 row_found = row;
10824 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
10825 break;
10826 ++row;
10827 }
10828
10829 return row_found;
10830 }
10831
10832
10833 /* Return the last row in the current matrix of W that is not affected
10834 by changes at the start of current_buffer that occurred since the
10835 last time W was redisplayed. Value is null if no such row exists.
10836
10837 The global variable beg_unchanged has to contain the number of
10838 bytes unchanged at the start of current_buffer. BEG +
10839 beg_unchanged is the buffer position of the first changed byte in
10840 current_buffer. Characters at positions < BEG + beg_unchanged are
10841 at the same buffer positions as they were when the current matrix
10842 was built. */
10843
10844 static struct glyph_row *
10845 find_last_unchanged_at_beg_row (w)
10846 struct window *w;
10847 {
10848 int first_changed_pos = BEG + BEG_UNCHANGED;
10849 struct glyph_row *row;
10850 struct glyph_row *row_found = NULL;
10851 int yb = window_text_bottom_y (w);
10852
10853 /* Find the last row displaying unchanged text. */
10854 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10855 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
10856 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
10857 {
10858 if (/* If row ends before first_changed_pos, it is unchanged,
10859 except in some case. */
10860 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
10861 /* When row ends in ZV and we write at ZV it is not
10862 unchanged. */
10863 && !row->ends_at_zv_p
10864 /* When first_changed_pos is the end of a continued line,
10865 row is not unchanged because it may be no longer
10866 continued. */
10867 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
10868 && row->continued_p))
10869 row_found = row;
10870
10871 /* Stop if last visible row. */
10872 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
10873 break;
10874
10875 ++row;
10876 }
10877
10878 return row_found;
10879 }
10880
10881
10882 /* Find the first glyph row in the current matrix of W that is not
10883 affected by changes at the end of current_buffer since the last
10884 time the window was redisplayed. Return in *DELTA the number of
10885 chars by which buffer positions in unchanged text at the end of
10886 current_buffer must be adjusted. Return in *DELTA_BYTES the
10887 corresponding number of bytes. Value is null if no such row
10888 exists, i.e. all rows are affected by changes. */
10889
10890 static struct glyph_row *
10891 find_first_unchanged_at_end_row (w, delta, delta_bytes)
10892 struct window *w;
10893 int *delta, *delta_bytes;
10894 {
10895 struct glyph_row *row;
10896 struct glyph_row *row_found = NULL;
10897
10898 *delta = *delta_bytes = 0;
10899
10900 /* Display must not have been paused, otherwise the current matrix
10901 is not up to date. */
10902 if (NILP (w->window_end_valid))
10903 abort ();
10904
10905 /* A value of window_end_pos >= END_UNCHANGED means that the window
10906 end is in the range of changed text. If so, there is no
10907 unchanged row at the end of W's current matrix. */
10908 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
10909 return NULL;
10910
10911 /* Set row to the last row in W's current matrix displaying text. */
10912 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
10913
10914 /* If matrix is entirely empty, no unchanged row exists. */
10915 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10916 {
10917 /* The value of row is the last glyph row in the matrix having a
10918 meaningful buffer position in it. The end position of row
10919 corresponds to window_end_pos. This allows us to translate
10920 buffer positions in the current matrix to current buffer
10921 positions for characters not in changed text. */
10922 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
10923 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
10924 int last_unchanged_pos, last_unchanged_pos_old;
10925 struct glyph_row *first_text_row
10926 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10927
10928 *delta = Z - Z_old;
10929 *delta_bytes = Z_BYTE - Z_BYTE_old;
10930
10931 /* Set last_unchanged_pos to the buffer position of the last
10932 character in the buffer that has not been changed. Z is the
10933 index + 1 of the last byte in current_buffer, i.e. by
10934 subtracting end_unchanged we get the index of the last
10935 unchanged character, and we have to add BEG to get its buffer
10936 position. */
10937 last_unchanged_pos = Z - END_UNCHANGED + BEG;
10938 last_unchanged_pos_old = last_unchanged_pos - *delta;
10939
10940 /* Search backward from ROW for a row displaying a line that
10941 starts at a minimum position >= last_unchanged_pos_old. */
10942 for (; row > first_text_row; --row)
10943 {
10944 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
10945 abort ();
10946
10947 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
10948 row_found = row;
10949 }
10950 }
10951
10952 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
10953 abort ();
10954
10955 return row_found;
10956 }
10957
10958
10959 /* Make sure that glyph rows in the current matrix of window W
10960 reference the same glyph memory as corresponding rows in the
10961 frame's frame matrix. This function is called after scrolling W's
10962 current matrix on a terminal frame in try_window_id and
10963 try_window_reusing_current_matrix. */
10964
10965 static void
10966 sync_frame_with_window_matrix_rows (w)
10967 struct window *w;
10968 {
10969 struct frame *f = XFRAME (w->frame);
10970 struct glyph_row *window_row, *window_row_end, *frame_row;
10971
10972 /* Preconditions: W must be a leaf window and full-width. Its frame
10973 must have a frame matrix. */
10974 xassert (NILP (w->hchild) && NILP (w->vchild));
10975 xassert (WINDOW_FULL_WIDTH_P (w));
10976 xassert (!FRAME_WINDOW_P (f));
10977
10978 /* If W is a full-width window, glyph pointers in W's current matrix
10979 have, by definition, to be the same as glyph pointers in the
10980 corresponding frame matrix. */
10981 window_row = w->current_matrix->rows;
10982 window_row_end = window_row + w->current_matrix->nrows;
10983 frame_row = f->current_matrix->rows + XFASTINT (w->top);
10984 while (window_row < window_row_end)
10985 {
10986 int area;
10987
10988 for (area = LEFT_MARGIN_AREA; area <= LAST_AREA; ++area)
10989 frame_row->glyphs[area] = window_row->glyphs[area];
10990
10991 /* Disable frame rows whose corresponding window rows have
10992 been disabled in try_window_id. */
10993 if (!window_row->enabled_p)
10994 frame_row->enabled_p = 0;
10995
10996 ++window_row, ++frame_row;
10997 }
10998 }
10999
11000
11001 /* Find the glyph row in window W containing CHARPOS. Consider all
11002 rows between START and END (not inclusive). END null means search
11003 all rows to the end of the display area of W. Value is the row
11004 containing CHARPOS or null. */
11005
11006 static struct glyph_row *
11007 row_containing_pos (w, charpos, start, end)
11008 struct window *w;
11009 int charpos;
11010 struct glyph_row *start, *end;
11011 {
11012 struct glyph_row *row = start;
11013 int last_y;
11014
11015 /* If we happen to start on a header-line, skip that. */
11016 if (row->mode_line_p)
11017 ++row;
11018
11019 if ((end && row >= end) || !row->enabled_p)
11020 return NULL;
11021
11022 last_y = window_text_bottom_y (w);
11023
11024 while ((end == NULL || row < end)
11025 && (MATRIX_ROW_END_CHARPOS (row) < charpos
11026 /* The end position of a row equals the start
11027 position of the next row. If CHARPOS is there, we
11028 would rather display it in the next line, except
11029 when this line ends in ZV. */
11030 || (MATRIX_ROW_END_CHARPOS (row) == charpos
11031 && (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
11032 || !row->ends_at_zv_p)))
11033 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
11034 ++row;
11035
11036 /* Give up if CHARPOS not found. */
11037 if ((end && row >= end)
11038 || charpos < MATRIX_ROW_START_CHARPOS (row)
11039 || charpos > MATRIX_ROW_END_CHARPOS (row))
11040 row = NULL;
11041
11042 return row;
11043 }
11044
11045
11046 /* Try to redisplay window W by reusing its existing display. W's
11047 current matrix must be up to date when this function is called,
11048 i.e. window_end_valid must not be nil.
11049
11050 Value is
11051
11052 1 if display has been updated
11053 0 if otherwise unsuccessful
11054 -1 if redisplay with same window start is known not to succeed
11055
11056 The following steps are performed:
11057
11058 1. Find the last row in the current matrix of W that is not
11059 affected by changes at the start of current_buffer. If no such row
11060 is found, give up.
11061
11062 2. Find the first row in W's current matrix that is not affected by
11063 changes at the end of current_buffer. Maybe there is no such row.
11064
11065 3. Display lines beginning with the row + 1 found in step 1 to the
11066 row found in step 2 or, if step 2 didn't find a row, to the end of
11067 the window.
11068
11069 4. If cursor is not known to appear on the window, give up.
11070
11071 5. If display stopped at the row found in step 2, scroll the
11072 display and current matrix as needed.
11073
11074 6. Maybe display some lines at the end of W, if we must. This can
11075 happen under various circumstances, like a partially visible line
11076 becoming fully visible, or because newly displayed lines are displayed
11077 in smaller font sizes.
11078
11079 7. Update W's window end information. */
11080
11081 /* Check that window end is what we expect it to be. */
11082
11083 static int
11084 try_window_id (w)
11085 struct window *w;
11086 {
11087 struct frame *f = XFRAME (w->frame);
11088 struct glyph_matrix *current_matrix = w->current_matrix;
11089 struct glyph_matrix *desired_matrix = w->desired_matrix;
11090 struct glyph_row *last_unchanged_at_beg_row;
11091 struct glyph_row *first_unchanged_at_end_row;
11092 struct glyph_row *row;
11093 struct glyph_row *bottom_row;
11094 int bottom_vpos;
11095 struct it it;
11096 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
11097 struct text_pos start_pos;
11098 struct run run;
11099 int first_unchanged_at_end_vpos = 0;
11100 struct glyph_row *last_text_row, *last_text_row_at_end;
11101 struct text_pos start;
11102
11103 SET_TEXT_POS_FROM_MARKER (start, w->start);
11104
11105 /* Check pre-conditions. Window end must be valid, otherwise
11106 the current matrix would not be up to date. */
11107 xassert (!NILP (w->window_end_valid));
11108 xassert (FRAME_WINDOW_P (XFRAME (w->frame))
11109 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)));
11110
11111 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
11112 only if buffer has really changed. The reason is that the gap is
11113 initially at Z for freshly visited files. The code below would
11114 set end_unchanged to 0 in that case. */
11115 if (MODIFF > SAVE_MODIFF
11116 /* This seems to happen sometimes after saving a buffer. */
11117 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
11118 {
11119 if (GPT - BEG < BEG_UNCHANGED)
11120 BEG_UNCHANGED = GPT - BEG;
11121 if (Z - GPT < END_UNCHANGED)
11122 END_UNCHANGED = Z - GPT;
11123 }
11124
11125 /* If window starts after a line end, and the last change is in
11126 front of that newline, then changes don't affect the display.
11127 This case happens with stealth-fontification. Note that although
11128 the display is unchanged, glyph positions in the matrix have to
11129 be adjusted, of course. */
11130 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
11131 if (CHARPOS (start) > BEGV
11132 && Z - END_UNCHANGED < CHARPOS (start) - 1
11133 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n'
11134 && PT < MATRIX_ROW_END_CHARPOS (row))
11135 {
11136 struct glyph_row *r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
11137 int delta = CHARPOS (start) - MATRIX_ROW_START_CHARPOS (r0);
11138
11139 if (delta)
11140 {
11141 struct glyph_row *r1 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11142 int delta_bytes = BYTEPOS (start) - MATRIX_ROW_START_BYTEPOS (r0);
11143
11144 increment_matrix_positions (w->current_matrix,
11145 MATRIX_ROW_VPOS (r0, current_matrix),
11146 MATRIX_ROW_VPOS (r1, current_matrix),
11147 delta, delta_bytes);
11148 }
11149
11150 #if 0 /* If changes are all in front of the window start, the
11151 distance of the last displayed glyph from Z hasn't
11152 changed. */
11153 w->window_end_pos
11154 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
11155 w->window_end_bytepos
11156 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11157 #endif
11158
11159 row = row_containing_pos (w, PT, r0, NULL);
11160 if (row == NULL)
11161 return 0;
11162
11163 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11164 return 1;
11165 }
11166
11167 /* Return quickly if changes are all below what is displayed in the
11168 window, and if PT is in the window. */
11169 if (BEG_UNCHANGED > MATRIX_ROW_END_CHARPOS (row)
11170 && PT < MATRIX_ROW_END_CHARPOS (row))
11171 {
11172 /* We have to update window end positions because the buffer's
11173 size has changed. */
11174 w->window_end_pos
11175 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
11176 w->window_end_bytepos
11177 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11178
11179 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11180 row = row_containing_pos (w, PT, row, NULL);
11181 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11182 return 2;
11183 }
11184
11185 /* Check that window start agrees with the start of the first glyph
11186 row in its current matrix. Check this after we know the window
11187 start is not in changed text, otherwise positions would not be
11188 comparable. */
11189 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11190 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
11191 return 0;
11192
11193 /* Compute the position at which we have to start displaying new
11194 lines. Some of the lines at the top of the window might be
11195 reusable because they are not displaying changed text. Find the
11196 last row in W's current matrix not affected by changes at the
11197 start of current_buffer. Value is null if changes start in the
11198 first line of window. */
11199 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
11200 if (last_unchanged_at_beg_row)
11201 {
11202 /* Avoid starting to display in the moddle of a character, a TAB
11203 for instance. This is easier than to set up the iterator
11204 exactly, and it's not a frequent case, so the additional
11205 effort wouldn't really pay off. */
11206 while (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
11207 && last_unchanged_at_beg_row > w->current_matrix->rows)
11208 --last_unchanged_at_beg_row;
11209
11210 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
11211 return 0;
11212
11213 init_to_row_end (&it, w, last_unchanged_at_beg_row);
11214 start_pos = it.current.pos;
11215
11216 /* Start displaying new lines in the desired matrix at the same
11217 vpos we would use in the current matrix, i.e. below
11218 last_unchanged_at_beg_row. */
11219 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
11220 current_matrix);
11221 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11222 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
11223
11224 xassert (it.hpos == 0 && it.current_x == 0);
11225 }
11226 else
11227 {
11228 /* There are no reusable lines at the start of the window.
11229 Start displaying in the first line. */
11230 start_display (&it, w, start);
11231 start_pos = it.current.pos;
11232 }
11233
11234 /* Find the first row that is not affected by changes at the end of
11235 the buffer. Value will be null if there is no unchanged row, in
11236 which case we must redisplay to the end of the window. delta
11237 will be set to the value by which buffer positions beginning with
11238 first_unchanged_at_end_row have to be adjusted due to text
11239 changes. */
11240 first_unchanged_at_end_row
11241 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
11242 IF_DEBUG (debug_delta = delta);
11243 IF_DEBUG (debug_delta_bytes = delta_bytes);
11244
11245 /* Set stop_pos to the buffer position up to which we will have to
11246 display new lines. If first_unchanged_at_end_row != NULL, this
11247 is the buffer position of the start of the line displayed in that
11248 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
11249 that we don't stop at a buffer position. */
11250 stop_pos = 0;
11251 if (first_unchanged_at_end_row)
11252 {
11253 xassert (last_unchanged_at_beg_row == NULL
11254 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
11255
11256 /* If this is a continuation line, move forward to the next one
11257 that isn't. Changes in lines above affect this line.
11258 Caution: this may move first_unchanged_at_end_row to a row
11259 not displaying text. */
11260 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
11261 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11262 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11263 < it.last_visible_y))
11264 ++first_unchanged_at_end_row;
11265
11266 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11267 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11268 >= it.last_visible_y))
11269 first_unchanged_at_end_row = NULL;
11270 else
11271 {
11272 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
11273 + delta);
11274 first_unchanged_at_end_vpos
11275 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
11276 xassert (stop_pos >= Z - END_UNCHANGED);
11277 }
11278 }
11279 else if (last_unchanged_at_beg_row == NULL)
11280 return 0;
11281
11282
11283 #if GLYPH_DEBUG
11284
11285 /* Either there is no unchanged row at the end, or the one we have
11286 now displays text. This is a necessary condition for the window
11287 end pos calculation at the end of this function. */
11288 xassert (first_unchanged_at_end_row == NULL
11289 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
11290
11291 debug_last_unchanged_at_beg_vpos
11292 = (last_unchanged_at_beg_row
11293 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
11294 : -1);
11295 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
11296
11297 #endif /* GLYPH_DEBUG != 0 */
11298
11299
11300 /* Display new lines. Set last_text_row to the last new line
11301 displayed which has text on it, i.e. might end up as being the
11302 line where the window_end_vpos is. */
11303 w->cursor.vpos = -1;
11304 last_text_row = NULL;
11305 overlay_arrow_seen = 0;
11306 while (it.current_y < it.last_visible_y
11307 && !fonts_changed_p
11308 && (first_unchanged_at_end_row == NULL
11309 || IT_CHARPOS (it) < stop_pos))
11310 {
11311 if (display_line (&it))
11312 last_text_row = it.glyph_row - 1;
11313 }
11314
11315 if (fonts_changed_p)
11316 return -1;
11317
11318
11319 /* Compute differences in buffer positions, y-positions etc. for
11320 lines reused at the bottom of the window. Compute what we can
11321 scroll. */
11322 if (first_unchanged_at_end_row
11323 /* No lines reused because we displayed everything up to the
11324 bottom of the window. */
11325 && it.current_y < it.last_visible_y)
11326 {
11327 dvpos = (it.vpos
11328 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
11329 current_matrix));
11330 dy = it.current_y - first_unchanged_at_end_row->y;
11331 run.current_y = first_unchanged_at_end_row->y;
11332 run.desired_y = run.current_y + dy;
11333 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
11334 }
11335 else
11336 {
11337 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
11338 first_unchanged_at_end_row = NULL;
11339 }
11340 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
11341
11342
11343 /* Find the cursor if not already found. We have to decide whether
11344 PT will appear on this window (it sometimes doesn't, but this is
11345 not a very frequent case.) This decision has to be made before
11346 the current matrix is altered. A value of cursor.vpos < 0 means
11347 that PT is either in one of the lines beginning at
11348 first_unchanged_at_end_row or below the window. Don't care for
11349 lines that might be displayed later at the window end; as
11350 mentioned, this is not a frequent case. */
11351 if (w->cursor.vpos < 0)
11352 {
11353 /* Cursor in unchanged rows at the top? */
11354 if (PT < CHARPOS (start_pos)
11355 && last_unchanged_at_beg_row)
11356 {
11357 row = row_containing_pos (w, PT,
11358 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
11359 last_unchanged_at_beg_row + 1);
11360 if (row)
11361 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11362 }
11363
11364 /* Start from first_unchanged_at_end_row looking for PT. */
11365 else if (first_unchanged_at_end_row)
11366 {
11367 row = row_containing_pos (w, PT - delta,
11368 first_unchanged_at_end_row, NULL);
11369 if (row)
11370 set_cursor_from_row (w, row, w->current_matrix, delta,
11371 delta_bytes, dy, dvpos);
11372 }
11373
11374 /* Give up if cursor was not found. */
11375 if (w->cursor.vpos < 0)
11376 {
11377 clear_glyph_matrix (w->desired_matrix);
11378 return -1;
11379 }
11380 }
11381
11382 /* Don't let the cursor end in the scroll margins. */
11383 {
11384 int this_scroll_margin, cursor_height;
11385
11386 this_scroll_margin = max (0, scroll_margin);
11387 this_scroll_margin = min (this_scroll_margin,
11388 XFASTINT (w->height) / 4);
11389 this_scroll_margin *= CANON_Y_UNIT (it.f);
11390 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
11391
11392 if ((w->cursor.y < this_scroll_margin
11393 && CHARPOS (start) > BEGV)
11394 /* Don't take scroll margin into account at the bottom because
11395 old redisplay didn't do it either. */
11396 || w->cursor.y + cursor_height > it.last_visible_y)
11397 {
11398 w->cursor.vpos = -1;
11399 clear_glyph_matrix (w->desired_matrix);
11400 return -1;
11401 }
11402 }
11403
11404 /* Scroll the display. Do it before changing the current matrix so
11405 that xterm.c doesn't get confused about where the cursor glyph is
11406 found. */
11407 if (dy && run.height)
11408 {
11409 update_begin (f);
11410
11411 if (FRAME_WINDOW_P (f))
11412 {
11413 rif->update_window_begin_hook (w);
11414 rif->clear_mouse_face (w);
11415 rif->scroll_run_hook (w, &run);
11416 rif->update_window_end_hook (w, 0, 0);
11417 }
11418 else
11419 {
11420 /* Terminal frame. In this case, dvpos gives the number of
11421 lines to scroll by; dvpos < 0 means scroll up. */
11422 int first_unchanged_at_end_vpos
11423 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
11424 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
11425 int end = XFASTINT (w->top) + window_internal_height (w);
11426
11427 /* Perform the operation on the screen. */
11428 if (dvpos > 0)
11429 {
11430 /* Scroll last_unchanged_at_beg_row to the end of the
11431 window down dvpos lines. */
11432 set_terminal_window (end);
11433
11434 /* On dumb terminals delete dvpos lines at the end
11435 before inserting dvpos empty lines. */
11436 if (!scroll_region_ok)
11437 ins_del_lines (end - dvpos, -dvpos);
11438
11439 /* Insert dvpos empty lines in front of
11440 last_unchanged_at_beg_row. */
11441 ins_del_lines (from, dvpos);
11442 }
11443 else if (dvpos < 0)
11444 {
11445 /* Scroll up last_unchanged_at_beg_vpos to the end of
11446 the window to last_unchanged_at_beg_vpos - |dvpos|. */
11447 set_terminal_window (end);
11448
11449 /* Delete dvpos lines in front of
11450 last_unchanged_at_beg_vpos. ins_del_lines will set
11451 the cursor to the given vpos and emit |dvpos| delete
11452 line sequences. */
11453 ins_del_lines (from + dvpos, dvpos);
11454
11455 /* On a dumb terminal insert dvpos empty lines at the
11456 end. */
11457 if (!scroll_region_ok)
11458 ins_del_lines (end + dvpos, -dvpos);
11459 }
11460
11461 set_terminal_window (0);
11462 }
11463
11464 update_end (f);
11465 }
11466
11467 /* Shift reused rows of the current matrix to the right position.
11468 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
11469 text. */
11470 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11471 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
11472 if (dvpos < 0)
11473 {
11474 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
11475 bottom_vpos, dvpos);
11476 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
11477 bottom_vpos, 0);
11478 }
11479 else if (dvpos > 0)
11480 {
11481 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
11482 bottom_vpos, dvpos);
11483 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
11484 first_unchanged_at_end_vpos + dvpos, 0);
11485 }
11486
11487 /* For frame-based redisplay, make sure that current frame and window
11488 matrix are in sync with respect to glyph memory. */
11489 if (!FRAME_WINDOW_P (f))
11490 sync_frame_with_window_matrix_rows (w);
11491
11492 /* Adjust buffer positions in reused rows. */
11493 if (delta)
11494 increment_matrix_positions (current_matrix,
11495 first_unchanged_at_end_vpos + dvpos,
11496 bottom_vpos, delta, delta_bytes);
11497
11498 /* Adjust Y positions. */
11499 if (dy)
11500 shift_glyph_matrix (w, current_matrix,
11501 first_unchanged_at_end_vpos + dvpos,
11502 bottom_vpos, dy);
11503
11504 if (first_unchanged_at_end_row)
11505 first_unchanged_at_end_row += dvpos;
11506
11507 /* If scrolling up, there may be some lines to display at the end of
11508 the window. */
11509 last_text_row_at_end = NULL;
11510 if (dy < 0)
11511 {
11512 /* Set last_row to the glyph row in the current matrix where the
11513 window end line is found. It has been moved up or down in
11514 the matrix by dvpos. */
11515 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
11516 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
11517
11518 /* If last_row is the window end line, it should display text. */
11519 xassert (last_row->displays_text_p);
11520
11521 /* If window end line was partially visible before, begin
11522 displaying at that line. Otherwise begin displaying with the
11523 line following it. */
11524 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
11525 {
11526 init_to_row_start (&it, w, last_row);
11527 it.vpos = last_vpos;
11528 it.current_y = last_row->y;
11529 }
11530 else
11531 {
11532 init_to_row_end (&it, w, last_row);
11533 it.vpos = 1 + last_vpos;
11534 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
11535 ++last_row;
11536 }
11537
11538 /* We may start in a continuation line. If so, we have to get
11539 the right continuation_lines_width and current_x. */
11540 it.continuation_lines_width = last_row->continuation_lines_width;
11541 it.hpos = it.current_x = 0;
11542
11543 /* Display the rest of the lines at the window end. */
11544 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11545 while (it.current_y < it.last_visible_y
11546 && !fonts_changed_p)
11547 {
11548 /* Is it always sure that the display agrees with lines in
11549 the current matrix? I don't think so, so we mark rows
11550 displayed invalid in the current matrix by setting their
11551 enabled_p flag to zero. */
11552 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
11553 if (display_line (&it))
11554 last_text_row_at_end = it.glyph_row - 1;
11555 }
11556 }
11557
11558 /* Update window_end_pos and window_end_vpos. */
11559 if (first_unchanged_at_end_row
11560 && first_unchanged_at_end_row->y < it.last_visible_y
11561 && !last_text_row_at_end)
11562 {
11563 /* Window end line if one of the preserved rows from the current
11564 matrix. Set row to the last row displaying text in current
11565 matrix starting at first_unchanged_at_end_row, after
11566 scrolling. */
11567 xassert (first_unchanged_at_end_row->displays_text_p);
11568 row = find_last_row_displaying_text (w->current_matrix, &it,
11569 first_unchanged_at_end_row);
11570 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
11571
11572 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
11573 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11574 w->window_end_vpos
11575 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
11576 }
11577 else if (last_text_row_at_end)
11578 {
11579 w->window_end_pos
11580 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
11581 w->window_end_bytepos
11582 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
11583 w->window_end_vpos
11584 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
11585 }
11586 else if (last_text_row)
11587 {
11588 /* We have displayed either to the end of the window or at the
11589 end of the window, i.e. the last row with text is to be found
11590 in the desired matrix. */
11591 w->window_end_pos
11592 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
11593 w->window_end_bytepos
11594 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
11595 w->window_end_vpos
11596 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
11597 }
11598 else if (first_unchanged_at_end_row == NULL
11599 && last_text_row == NULL
11600 && last_text_row_at_end == NULL)
11601 {
11602 /* Displayed to end of window, but no line containing text was
11603 displayed. Lines were deleted at the end of the window. */
11604 int vpos;
11605 int header_line_p = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
11606
11607 for (vpos = XFASTINT (w->window_end_vpos); vpos > 0; --vpos)
11608 if ((w->desired_matrix->rows[vpos + header_line_p].enabled_p
11609 && w->desired_matrix->rows[vpos + header_line_p].displays_text_p)
11610 || (!w->desired_matrix->rows[vpos + header_line_p].enabled_p
11611 && w->current_matrix->rows[vpos + header_line_p].displays_text_p))
11612 break;
11613
11614 w->window_end_vpos = make_number (vpos);
11615 }
11616 else
11617 abort ();
11618
11619 #if 0 /* This leads to problems, for instance when the cursor is
11620 at ZV, and the cursor line displays no text. */
11621 /* Disable rows below what's displayed in the window. This makes
11622 debugging easier. */
11623 enable_glyph_matrix_rows (current_matrix,
11624 XFASTINT (w->window_end_vpos) + 1,
11625 bottom_vpos, 0);
11626 #endif
11627
11628 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
11629 debug_end_vpos = XFASTINT (w->window_end_vpos));
11630
11631 /* Record that display has not been completed. */
11632 w->window_end_valid = Qnil;
11633 w->desired_matrix->no_scrolling_p = 1;
11634 return 3;
11635 }
11636
11637
11638 \f
11639 /***********************************************************************
11640 More debugging support
11641 ***********************************************************************/
11642
11643 #if GLYPH_DEBUG
11644
11645 void dump_glyph_row P_ ((struct glyph_matrix *, int, int));
11646 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
11647 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
11648
11649
11650 /* Dump the contents of glyph matrix MATRIX on stderr.
11651
11652 GLYPHS 0 means don't show glyph contents.
11653 GLYPHS 1 means show glyphs in short form
11654 GLYPHS > 1 means show glyphs in long form. */
11655
11656 void
11657 dump_glyph_matrix (matrix, glyphs)
11658 struct glyph_matrix *matrix;
11659 int glyphs;
11660 {
11661 int i;
11662 for (i = 0; i < matrix->nrows; ++i)
11663 dump_glyph_row (matrix, i, glyphs);
11664 }
11665
11666
11667 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
11668 the glyph row and area where the glyph comes from. */
11669
11670 void
11671 dump_glyph (row, glyph, area)
11672 struct glyph_row *row;
11673 struct glyph *glyph;
11674 int area;
11675 {
11676 if (glyph->type == CHAR_GLYPH)
11677 {
11678 fprintf (stderr,
11679 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11680 glyph - row->glyphs[TEXT_AREA],
11681 'C',
11682 glyph->charpos,
11683 (BUFFERP (glyph->object)
11684 ? 'B'
11685 : (STRINGP (glyph->object)
11686 ? 'S'
11687 : '-')),
11688 glyph->pixel_width,
11689 glyph->u.ch,
11690 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
11691 ? glyph->u.ch
11692 : '.'),
11693 glyph->face_id,
11694 glyph->left_box_line_p,
11695 glyph->right_box_line_p);
11696 }
11697 else if (glyph->type == STRETCH_GLYPH)
11698 {
11699 fprintf (stderr,
11700 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11701 glyph - row->glyphs[TEXT_AREA],
11702 'S',
11703 glyph->charpos,
11704 (BUFFERP (glyph->object)
11705 ? 'B'
11706 : (STRINGP (glyph->object)
11707 ? 'S'
11708 : '-')),
11709 glyph->pixel_width,
11710 0,
11711 '.',
11712 glyph->face_id,
11713 glyph->left_box_line_p,
11714 glyph->right_box_line_p);
11715 }
11716 else if (glyph->type == IMAGE_GLYPH)
11717 {
11718 fprintf (stderr,
11719 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11720 glyph - row->glyphs[TEXT_AREA],
11721 'I',
11722 glyph->charpos,
11723 (BUFFERP (glyph->object)
11724 ? 'B'
11725 : (STRINGP (glyph->object)
11726 ? 'S'
11727 : '-')),
11728 glyph->pixel_width,
11729 glyph->u.img_id,
11730 '.',
11731 glyph->face_id,
11732 glyph->left_box_line_p,
11733 glyph->right_box_line_p);
11734 }
11735 }
11736
11737
11738 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
11739 GLYPHS 0 means don't show glyph contents.
11740 GLYPHS 1 means show glyphs in short form
11741 GLYPHS > 1 means show glyphs in long form. */
11742
11743 void
11744 dump_glyph_row (matrix, vpos, glyphs)
11745 struct glyph_matrix *matrix;
11746 int vpos, glyphs;
11747 {
11748 struct glyph_row *row;
11749
11750 if (vpos < 0 || vpos >= matrix->nrows)
11751 return;
11752
11753 row = MATRIX_ROW (matrix, vpos);
11754
11755 if (glyphs != 1)
11756 {
11757 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
11758 fprintf (stderr, "=======================================================================\n");
11759
11760 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d\
11761 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
11762 row - matrix->rows,
11763 MATRIX_ROW_START_CHARPOS (row),
11764 MATRIX_ROW_END_CHARPOS (row),
11765 row->used[TEXT_AREA],
11766 row->contains_overlapping_glyphs_p,
11767 row->enabled_p,
11768 row->inverse_p,
11769 row->truncated_on_left_p,
11770 row->truncated_on_right_p,
11771 row->overlay_arrow_p,
11772 row->continued_p,
11773 MATRIX_ROW_CONTINUATION_LINE_P (row),
11774 row->displays_text_p,
11775 row->ends_at_zv_p,
11776 row->fill_line_p,
11777 row->ends_in_middle_of_char_p,
11778 row->starts_in_middle_of_char_p,
11779 row->mouse_face_p,
11780 row->x,
11781 row->y,
11782 row->pixel_width,
11783 row->height,
11784 row->visible_height,
11785 row->ascent,
11786 row->phys_ascent);
11787 fprintf (stderr, "%9d %5d\n", row->start.overlay_string_index,
11788 row->end.overlay_string_index);
11789 fprintf (stderr, "%9d %5d\n",
11790 CHARPOS (row->start.string_pos),
11791 CHARPOS (row->end.string_pos));
11792 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
11793 row->end.dpvec_index);
11794 }
11795
11796 if (glyphs > 1)
11797 {
11798 int area;
11799
11800 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
11801 {
11802 struct glyph *glyph = row->glyphs[area];
11803 struct glyph *glyph_end = glyph + row->used[area];
11804
11805 /* Glyph for a line end in text. */
11806 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
11807 ++glyph_end;
11808
11809 if (glyph < glyph_end)
11810 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
11811
11812 for (; glyph < glyph_end; ++glyph)
11813 dump_glyph (row, glyph, area);
11814 }
11815 }
11816 else if (glyphs == 1)
11817 {
11818 int area;
11819
11820 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
11821 {
11822 char *s = (char *) alloca (row->used[area] + 1);
11823 int i;
11824
11825 for (i = 0; i < row->used[area]; ++i)
11826 {
11827 struct glyph *glyph = row->glyphs[area] + i;
11828 if (glyph->type == CHAR_GLYPH
11829 && glyph->u.ch < 0x80
11830 && glyph->u.ch >= ' ')
11831 s[i] = glyph->u.ch;
11832 else
11833 s[i] = '.';
11834 }
11835
11836 s[i] = '\0';
11837 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
11838 }
11839 }
11840 }
11841
11842
11843 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
11844 Sdump_glyph_matrix, 0, 1, "p",
11845 "Dump the current matrix of the selected window to stderr.\n\
11846 Shows contents of glyph row structures. With non-nil\n\
11847 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show\n\
11848 glyphs in short form, otherwise show glyphs in long form.")
11849 (glyphs)
11850 Lisp_Object glyphs;
11851 {
11852 struct window *w = XWINDOW (selected_window);
11853 struct buffer *buffer = XBUFFER (w->buffer);
11854
11855 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
11856 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
11857 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
11858 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
11859 fprintf (stderr, "=============================================\n");
11860 dump_glyph_matrix (w->current_matrix,
11861 NILP (glyphs) ? 0 : XINT (glyphs));
11862 return Qnil;
11863 }
11864
11865
11866 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
11867 "Dump glyph row ROW to stderr.\n\
11868 GLYPH 0 means don't dump glyphs.\n\
11869 GLYPH 1 means dump glyphs in short form.\n\
11870 GLYPH > 1 or omitted means dump glyphs in long form.")
11871 (row, glyphs)
11872 Lisp_Object row, glyphs;
11873 {
11874 CHECK_NUMBER (row, 0);
11875 dump_glyph_row (XWINDOW (selected_window)->current_matrix,
11876 XINT (row),
11877 INTEGERP (glyphs) ? XINT (glyphs) : 2);
11878 return Qnil;
11879 }
11880
11881
11882 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
11883 "Dump glyph row ROW of the tool-bar of the current frame to stderr.\n\
11884 GLYPH 0 means don't dump glyphs.\n\
11885 GLYPH 1 means dump glyphs in short form.\n\
11886 GLYPH > 1 or omitted means dump glyphs in long form.")
11887 (row, glyphs)
11888 Lisp_Object row, glyphs;
11889 {
11890 struct frame *sf = SELECTED_FRAME ();
11891 struct glyph_matrix *m = (XWINDOW (sf->tool_bar_window)->current_matrix);
11892 dump_glyph_row (m, XINT (row), INTEGERP (glyphs) ? XINT (glyphs) : 2);
11893 return Qnil;
11894 }
11895
11896
11897 DEFUN ("trace-redisplay-toggle", Ftrace_redisplay_toggle,
11898 Strace_redisplay_toggle, 0, 0, "",
11899 "Toggle tracing of redisplay.")
11900 ()
11901 {
11902 trace_redisplay_p = !trace_redisplay_p;
11903 return Qnil;
11904 }
11905
11906
11907 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, 1, "",
11908 "Print STRING to stderr.")
11909 (string)
11910 Lisp_Object string;
11911 {
11912 CHECK_STRING (string, 0);
11913 fprintf (stderr, "%s", XSTRING (string)->data);
11914 return Qnil;
11915 }
11916
11917 #endif /* GLYPH_DEBUG */
11918
11919
11920 \f
11921 /***********************************************************************
11922 Building Desired Matrix Rows
11923 ***********************************************************************/
11924
11925 /* Return a temporary glyph row holding the glyphs of an overlay
11926 arrow. Only used for non-window-redisplay windows. */
11927
11928 static struct glyph_row *
11929 get_overlay_arrow_glyph_row (w)
11930 struct window *w;
11931 {
11932 struct frame *f = XFRAME (WINDOW_FRAME (w));
11933 struct buffer *buffer = XBUFFER (w->buffer);
11934 struct buffer *old = current_buffer;
11935 unsigned char *arrow_string = XSTRING (Voverlay_arrow_string)->data;
11936 int arrow_len = XSTRING (Voverlay_arrow_string)->size;
11937 unsigned char *arrow_end = arrow_string + arrow_len;
11938 unsigned char *p;
11939 struct it it;
11940 int multibyte_p;
11941 int n_glyphs_before;
11942
11943 set_buffer_temp (buffer);
11944 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
11945 it.glyph_row->used[TEXT_AREA] = 0;
11946 SET_TEXT_POS (it.position, 0, 0);
11947
11948 multibyte_p = !NILP (buffer->enable_multibyte_characters);
11949 p = arrow_string;
11950 while (p < arrow_end)
11951 {
11952 Lisp_Object face, ilisp;
11953
11954 /* Get the next character. */
11955 if (multibyte_p)
11956 it.c = string_char_and_length (p, arrow_len, &it.len);
11957 else
11958 it.c = *p, it.len = 1;
11959 p += it.len;
11960
11961 /* Get its face. */
11962 ilisp = make_number (p - arrow_string);
11963 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
11964 it.face_id = compute_char_face (f, it.c, face);
11965
11966 /* Compute its width, get its glyphs. */
11967 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
11968 SET_TEXT_POS (it.position, -1, -1);
11969 PRODUCE_GLYPHS (&it);
11970
11971 /* If this character doesn't fit any more in the line, we have
11972 to remove some glyphs. */
11973 if (it.current_x > it.last_visible_x)
11974 {
11975 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
11976 break;
11977 }
11978 }
11979
11980 set_buffer_temp (old);
11981 return it.glyph_row;
11982 }
11983
11984
11985 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
11986 glyphs are only inserted for terminal frames since we can't really
11987 win with truncation glyphs when partially visible glyphs are
11988 involved. Which glyphs to insert is determined by
11989 produce_special_glyphs. */
11990
11991 static void
11992 insert_left_trunc_glyphs (it)
11993 struct it *it;
11994 {
11995 struct it truncate_it;
11996 struct glyph *from, *end, *to, *toend;
11997
11998 xassert (!FRAME_WINDOW_P (it->f));
11999
12000 /* Get the truncation glyphs. */
12001 truncate_it = *it;
12002 truncate_it.current_x = 0;
12003 truncate_it.face_id = DEFAULT_FACE_ID;
12004 truncate_it.glyph_row = &scratch_glyph_row;
12005 truncate_it.glyph_row->used[TEXT_AREA] = 0;
12006 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
12007 truncate_it.object = make_number (0);
12008 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
12009
12010 /* Overwrite glyphs from IT with truncation glyphs. */
12011 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
12012 end = from + truncate_it.glyph_row->used[TEXT_AREA];
12013 to = it->glyph_row->glyphs[TEXT_AREA];
12014 toend = to + it->glyph_row->used[TEXT_AREA];
12015
12016 while (from < end)
12017 *to++ = *from++;
12018
12019 /* There may be padding glyphs left over. Overwrite them too. */
12020 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
12021 {
12022 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
12023 while (from < end)
12024 *to++ = *from++;
12025 }
12026
12027 if (to > toend)
12028 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
12029 }
12030
12031
12032 /* Compute the pixel height and width of IT->glyph_row.
12033
12034 Most of the time, ascent and height of a display line will be equal
12035 to the max_ascent and max_height values of the display iterator
12036 structure. This is not the case if
12037
12038 1. We hit ZV without displaying anything. In this case, max_ascent
12039 and max_height will be zero.
12040
12041 2. We have some glyphs that don't contribute to the line height.
12042 (The glyph row flag contributes_to_line_height_p is for future
12043 pixmap extensions).
12044
12045 The first case is easily covered by using default values because in
12046 these cases, the line height does not really matter, except that it
12047 must not be zero. */
12048
12049 static void
12050 compute_line_metrics (it)
12051 struct it *it;
12052 {
12053 struct glyph_row *row = it->glyph_row;
12054 int area, i;
12055
12056 if (FRAME_WINDOW_P (it->f))
12057 {
12058 int i, header_line_height;
12059
12060 /* The line may consist of one space only, that was added to
12061 place the cursor on it. If so, the row's height hasn't been
12062 computed yet. */
12063 if (row->height == 0)
12064 {
12065 if (it->max_ascent + it->max_descent == 0)
12066 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
12067 row->ascent = it->max_ascent;
12068 row->height = it->max_ascent + it->max_descent;
12069 row->phys_ascent = it->max_phys_ascent;
12070 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
12071 }
12072
12073 /* Compute the width of this line. */
12074 row->pixel_width = row->x;
12075 for (i = 0; i < row->used[TEXT_AREA]; ++i)
12076 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
12077
12078 xassert (row->pixel_width >= 0);
12079 xassert (row->ascent >= 0 && row->height > 0);
12080
12081 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
12082 || MATRIX_ROW_OVERLAPS_PRED_P (row));
12083
12084 /* If first line's physical ascent is larger than its logical
12085 ascent, use the physical ascent, and make the row taller.
12086 This makes accented characters fully visible. */
12087 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
12088 && row->phys_ascent > row->ascent)
12089 {
12090 row->height += row->phys_ascent - row->ascent;
12091 row->ascent = row->phys_ascent;
12092 }
12093
12094 /* Compute how much of the line is visible. */
12095 row->visible_height = row->height;
12096
12097 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (it->w);
12098 if (row->y < header_line_height)
12099 row->visible_height -= header_line_height - row->y;
12100 else
12101 {
12102 int max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
12103 if (row->y + row->height > max_y)
12104 row->visible_height -= row->y + row->height - max_y;
12105 }
12106 }
12107 else
12108 {
12109 row->pixel_width = row->used[TEXT_AREA];
12110 row->ascent = row->phys_ascent = 0;
12111 row->height = row->phys_height = row->visible_height = 1;
12112 }
12113
12114 /* Compute a hash code for this row. */
12115 row->hash = 0;
12116 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12117 for (i = 0; i < row->used[area]; ++i)
12118 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
12119 + row->glyphs[area][i].u.val
12120 + row->glyphs[area][i].face_id
12121 + row->glyphs[area][i].padding_p
12122 + (row->glyphs[area][i].type << 2));
12123
12124 it->max_ascent = it->max_descent = 0;
12125 it->max_phys_ascent = it->max_phys_descent = 0;
12126 }
12127
12128
12129 /* Append one space to the glyph row of iterator IT if doing a
12130 window-based redisplay. DEFAULT_FACE_P non-zero means let the
12131 space have the default face, otherwise let it have the same face as
12132 IT->face_id. Value is non-zero if a space was added.
12133
12134 This function is called to make sure that there is always one glyph
12135 at the end of a glyph row that the cursor can be set on under
12136 window-systems. (If there weren't such a glyph we would not know
12137 how wide and tall a box cursor should be displayed).
12138
12139 At the same time this space let's a nicely handle clearing to the
12140 end of the line if the row ends in italic text. */
12141
12142 static int
12143 append_space (it, default_face_p)
12144 struct it *it;
12145 int default_face_p;
12146 {
12147 if (FRAME_WINDOW_P (it->f))
12148 {
12149 int n = it->glyph_row->used[TEXT_AREA];
12150
12151 if (it->glyph_row->glyphs[TEXT_AREA] + n
12152 < it->glyph_row->glyphs[1 + TEXT_AREA])
12153 {
12154 /* Save some values that must not be changed.
12155 Must save IT->c and IT->len because otherwise
12156 ITERATOR_AT_END_P wouldn't work anymore after
12157 append_space has been called. */
12158 enum display_element_type saved_what = it->what;
12159 int saved_c = it->c, saved_len = it->len;
12160 int saved_x = it->current_x;
12161 int saved_face_id = it->face_id;
12162 struct text_pos saved_pos;
12163 Lisp_Object saved_object;
12164 struct face *face;
12165
12166 saved_object = it->object;
12167 saved_pos = it->position;
12168
12169 it->what = IT_CHARACTER;
12170 bzero (&it->position, sizeof it->position);
12171 it->object = make_number (0);
12172 it->c = ' ';
12173 it->len = 1;
12174
12175 if (default_face_p)
12176 it->face_id = DEFAULT_FACE_ID;
12177 else if (it->face_before_selective_p)
12178 it->face_id = it->saved_face_id;
12179 face = FACE_FROM_ID (it->f, it->face_id);
12180 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
12181
12182 PRODUCE_GLYPHS (it);
12183
12184 it->current_x = saved_x;
12185 it->object = saved_object;
12186 it->position = saved_pos;
12187 it->what = saved_what;
12188 it->face_id = saved_face_id;
12189 it->len = saved_len;
12190 it->c = saved_c;
12191 return 1;
12192 }
12193 }
12194
12195 return 0;
12196 }
12197
12198
12199 /* Extend the face of the last glyph in the text area of IT->glyph_row
12200 to the end of the display line. Called from display_line.
12201 If the glyph row is empty, add a space glyph to it so that we
12202 know the face to draw. Set the glyph row flag fill_line_p. */
12203
12204 static void
12205 extend_face_to_end_of_line (it)
12206 struct it *it;
12207 {
12208 struct face *face;
12209 struct frame *f = it->f;
12210
12211 /* If line is already filled, do nothing. */
12212 if (it->current_x >= it->last_visible_x)
12213 return;
12214
12215 /* Face extension extends the background and box of IT->face_id
12216 to the end of the line. If the background equals the background
12217 of the frame, we don't have to do anything. */
12218 if (it->face_before_selective_p)
12219 face = FACE_FROM_ID (it->f, it->saved_face_id);
12220 else
12221 face = FACE_FROM_ID (f, it->face_id);
12222
12223 if (FRAME_WINDOW_P (f)
12224 && face->box == FACE_NO_BOX
12225 && face->background == FRAME_BACKGROUND_PIXEL (f)
12226 && !face->stipple)
12227 return;
12228
12229 /* Set the glyph row flag indicating that the face of the last glyph
12230 in the text area has to be drawn to the end of the text area. */
12231 it->glyph_row->fill_line_p = 1;
12232
12233 /* If current character of IT is not ASCII, make sure we have the
12234 ASCII face. This will be automatically undone the next time
12235 get_next_display_element returns a multibyte character. Note
12236 that the character will always be single byte in unibyte text. */
12237 if (!SINGLE_BYTE_CHAR_P (it->c))
12238 {
12239 it->face_id = FACE_FOR_CHAR (f, face, 0);
12240 }
12241
12242 if (FRAME_WINDOW_P (f))
12243 {
12244 /* If the row is empty, add a space with the current face of IT,
12245 so that we know which face to draw. */
12246 if (it->glyph_row->used[TEXT_AREA] == 0)
12247 {
12248 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
12249 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
12250 it->glyph_row->used[TEXT_AREA] = 1;
12251 }
12252 }
12253 else
12254 {
12255 /* Save some values that must not be changed. */
12256 int saved_x = it->current_x;
12257 struct text_pos saved_pos;
12258 Lisp_Object saved_object;
12259 enum display_element_type saved_what = it->what;
12260 int saved_face_id = it->face_id;
12261
12262 saved_object = it->object;
12263 saved_pos = it->position;
12264
12265 it->what = IT_CHARACTER;
12266 bzero (&it->position, sizeof it->position);
12267 it->object = make_number (0);
12268 it->c = ' ';
12269 it->len = 1;
12270 it->face_id = face->id;
12271
12272 PRODUCE_GLYPHS (it);
12273
12274 while (it->current_x <= it->last_visible_x)
12275 PRODUCE_GLYPHS (it);
12276
12277 /* Don't count these blanks really. It would let us insert a left
12278 truncation glyph below and make us set the cursor on them, maybe. */
12279 it->current_x = saved_x;
12280 it->object = saved_object;
12281 it->position = saved_pos;
12282 it->what = saved_what;
12283 it->face_id = saved_face_id;
12284 }
12285 }
12286
12287
12288 /* Value is non-zero if text starting at CHARPOS in current_buffer is
12289 trailing whitespace. */
12290
12291 static int
12292 trailing_whitespace_p (charpos)
12293 int charpos;
12294 {
12295 int bytepos = CHAR_TO_BYTE (charpos);
12296 int c = 0;
12297
12298 while (bytepos < ZV_BYTE
12299 && (c = FETCH_CHAR (bytepos),
12300 c == ' ' || c == '\t'))
12301 ++bytepos;
12302
12303 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
12304 {
12305 if (bytepos != PT_BYTE)
12306 return 1;
12307 }
12308 return 0;
12309 }
12310
12311
12312 /* Highlight trailing whitespace, if any, in ROW. */
12313
12314 void
12315 highlight_trailing_whitespace (f, row)
12316 struct frame *f;
12317 struct glyph_row *row;
12318 {
12319 int used = row->used[TEXT_AREA];
12320
12321 if (used)
12322 {
12323 struct glyph *start = row->glyphs[TEXT_AREA];
12324 struct glyph *glyph = start + used - 1;
12325
12326 /* Skip over glyphs inserted to display the cursor at the
12327 end of a line, for extending the face of the last glyph
12328 to the end of the line on terminals, and for truncation
12329 and continuation glyphs. */
12330 while (glyph >= start
12331 && glyph->type == CHAR_GLYPH
12332 && INTEGERP (glyph->object))
12333 --glyph;
12334
12335 /* If last glyph is a space or stretch, and it's trailing
12336 whitespace, set the face of all trailing whitespace glyphs in
12337 IT->glyph_row to `trailing-whitespace'. */
12338 if (glyph >= start
12339 && BUFFERP (glyph->object)
12340 && (glyph->type == STRETCH_GLYPH
12341 || (glyph->type == CHAR_GLYPH
12342 && glyph->u.ch == ' '))
12343 && trailing_whitespace_p (glyph->charpos))
12344 {
12345 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
12346
12347 while (glyph >= start
12348 && BUFFERP (glyph->object)
12349 && (glyph->type == STRETCH_GLYPH
12350 || (glyph->type == CHAR_GLYPH
12351 && glyph->u.ch == ' ')))
12352 (glyph--)->face_id = face_id;
12353 }
12354 }
12355 }
12356
12357
12358 /* Value is non-zero if glyph row ROW in window W should be
12359 used to hold the cursor. */
12360
12361 static int
12362 cursor_row_p (w, row)
12363 struct window *w;
12364 struct glyph_row *row;
12365 {
12366 int cursor_row_p = 1;
12367
12368 if (PT == MATRIX_ROW_END_CHARPOS (row))
12369 {
12370 /* If the row ends with a newline from a string, we don't want
12371 the cursor there (if the row is continued it doesn't end in a
12372 newline). */
12373 if (CHARPOS (row->end.string_pos) >= 0
12374 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12375 cursor_row_p = row->continued_p;
12376
12377 /* If the row ends at ZV, display the cursor at the end of that
12378 row instead of at the start of the row below. */
12379 else if (row->ends_at_zv_p)
12380 cursor_row_p = 1;
12381 else
12382 cursor_row_p = 0;
12383 }
12384
12385 return cursor_row_p;
12386 }
12387
12388
12389 /* Construct the glyph row IT->glyph_row in the desired matrix of
12390 IT->w from text at the current position of IT. See dispextern.h
12391 for an overview of struct it. Value is non-zero if
12392 IT->glyph_row displays text, as opposed to a line displaying ZV
12393 only. */
12394
12395 static int
12396 display_line (it)
12397 struct it *it;
12398 {
12399 struct glyph_row *row = it->glyph_row;
12400
12401 /* We always start displaying at hpos zero even if hscrolled. */
12402 xassert (it->hpos == 0 && it->current_x == 0);
12403
12404 /* We must not display in a row that's not a text row. */
12405 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
12406 < it->w->desired_matrix->nrows);
12407
12408 /* Is IT->w showing the region? */
12409 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
12410
12411 /* Clear the result glyph row and enable it. */
12412 prepare_desired_row (row);
12413
12414 row->y = it->current_y;
12415 row->start = it->current;
12416 row->continuation_lines_width = it->continuation_lines_width;
12417 row->displays_text_p = 1;
12418 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
12419 it->starts_in_middle_of_char_p = 0;
12420
12421 /* Arrange the overlays nicely for our purposes. Usually, we call
12422 display_line on only one line at a time, in which case this
12423 can't really hurt too much, or we call it on lines which appear
12424 one after another in the buffer, in which case all calls to
12425 recenter_overlay_lists but the first will be pretty cheap. */
12426 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
12427
12428 /* Move over display elements that are not visible because we are
12429 hscrolled. This may stop at an x-position < IT->first_visible_x
12430 if the first glyph is partially visible or if we hit a line end. */
12431 if (it->current_x < it->first_visible_x)
12432 move_it_in_display_line_to (it, ZV, it->first_visible_x,
12433 MOVE_TO_POS | MOVE_TO_X);
12434
12435 /* Get the initial row height. This is either the height of the
12436 text hscrolled, if there is any, or zero. */
12437 row->ascent = it->max_ascent;
12438 row->height = it->max_ascent + it->max_descent;
12439 row->phys_ascent = it->max_phys_ascent;
12440 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
12441
12442 /* Loop generating characters. The loop is left with IT on the next
12443 character to display. */
12444 while (1)
12445 {
12446 int n_glyphs_before, hpos_before, x_before;
12447 int x, i, nglyphs;
12448 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
12449
12450 /* Retrieve the next thing to display. Value is zero if end of
12451 buffer reached. */
12452 if (!get_next_display_element (it))
12453 {
12454 /* Maybe add a space at the end of this line that is used to
12455 display the cursor there under X. Set the charpos of the
12456 first glyph of blank lines not corresponding to any text
12457 to -1. */
12458 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
12459 || row->used[TEXT_AREA] == 0)
12460 {
12461 row->glyphs[TEXT_AREA]->charpos = -1;
12462 row->displays_text_p = 0;
12463
12464 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines))
12465 row->indicate_empty_line_p = 1;
12466 }
12467
12468 it->continuation_lines_width = 0;
12469 row->ends_at_zv_p = 1;
12470 break;
12471 }
12472
12473 /* Now, get the metrics of what we want to display. This also
12474 generates glyphs in `row' (which is IT->glyph_row). */
12475 n_glyphs_before = row->used[TEXT_AREA];
12476 x = it->current_x;
12477
12478 /* Remember the line height so far in case the next element doesn't
12479 fit on the line. */
12480 if (!it->truncate_lines_p)
12481 {
12482 ascent = it->max_ascent;
12483 descent = it->max_descent;
12484 phys_ascent = it->max_phys_ascent;
12485 phys_descent = it->max_phys_descent;
12486 }
12487
12488 PRODUCE_GLYPHS (it);
12489
12490 /* If this display element was in marginal areas, continue with
12491 the next one. */
12492 if (it->area != TEXT_AREA)
12493 {
12494 row->ascent = max (row->ascent, it->max_ascent);
12495 row->height = max (row->height, it->max_ascent + it->max_descent);
12496 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12497 row->phys_height = max (row->phys_height,
12498 it->max_phys_ascent + it->max_phys_descent);
12499 set_iterator_to_next (it, 1);
12500 continue;
12501 }
12502
12503 /* Does the display element fit on the line? If we truncate
12504 lines, we should draw past the right edge of the window. If
12505 we don't truncate, we want to stop so that we can display the
12506 continuation glyph before the right margin. If lines are
12507 continued, there are two possible strategies for characters
12508 resulting in more than 1 glyph (e.g. tabs): Display as many
12509 glyphs as possible in this line and leave the rest for the
12510 continuation line, or display the whole element in the next
12511 line. Original redisplay did the former, so we do it also. */
12512 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
12513 hpos_before = it->hpos;
12514 x_before = x;
12515
12516 if (/* Not a newline. */
12517 nglyphs > 0
12518 /* Glyphs produced fit entirely in the line. */
12519 && it->current_x < it->last_visible_x)
12520 {
12521 it->hpos += nglyphs;
12522 row->ascent = max (row->ascent, it->max_ascent);
12523 row->height = max (row->height, it->max_ascent + it->max_descent);
12524 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12525 row->phys_height = max (row->phys_height,
12526 it->max_phys_ascent + it->max_phys_descent);
12527 if (it->current_x - it->pixel_width < it->first_visible_x)
12528 row->x = x - it->first_visible_x;
12529 }
12530 else
12531 {
12532 int new_x;
12533 struct glyph *glyph;
12534
12535 for (i = 0; i < nglyphs; ++i, x = new_x)
12536 {
12537 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12538 new_x = x + glyph->pixel_width;
12539
12540 if (/* Lines are continued. */
12541 !it->truncate_lines_p
12542 && (/* Glyph doesn't fit on the line. */
12543 new_x > it->last_visible_x
12544 /* Or it fits exactly on a window system frame. */
12545 || (new_x == it->last_visible_x
12546 && FRAME_WINDOW_P (it->f))))
12547 {
12548 /* End of a continued line. */
12549
12550 if (it->hpos == 0
12551 || (new_x == it->last_visible_x
12552 && FRAME_WINDOW_P (it->f)))
12553 {
12554 /* Current glyph is the only one on the line or
12555 fits exactly on the line. We must continue
12556 the line because we can't draw the cursor
12557 after the glyph. */
12558 row->continued_p = 1;
12559 it->current_x = new_x;
12560 it->continuation_lines_width += new_x;
12561 ++it->hpos;
12562 if (i == nglyphs - 1)
12563 set_iterator_to_next (it, 1);
12564 }
12565 else if (CHAR_GLYPH_PADDING_P (*glyph)
12566 && !FRAME_WINDOW_P (it->f))
12567 {
12568 /* A padding glyph that doesn't fit on this line.
12569 This means the whole character doesn't fit
12570 on the line. */
12571 row->used[TEXT_AREA] = n_glyphs_before;
12572
12573 /* Fill the rest of the row with continuation
12574 glyphs like in 20.x. */
12575 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
12576 < row->glyphs[1 + TEXT_AREA])
12577 produce_special_glyphs (it, IT_CONTINUATION);
12578
12579 row->continued_p = 1;
12580 it->current_x = x_before;
12581 it->continuation_lines_width += x_before;
12582
12583 /* Restore the height to what it was before the
12584 element not fitting on the line. */
12585 it->max_ascent = ascent;
12586 it->max_descent = descent;
12587 it->max_phys_ascent = phys_ascent;
12588 it->max_phys_descent = phys_descent;
12589 }
12590 else
12591 {
12592 /* Display element draws past the right edge of
12593 the window. Restore positions to values
12594 before the element. The next line starts
12595 with current_x before the glyph that could
12596 not be displayed, so that TAB works right. */
12597 row->used[TEXT_AREA] = n_glyphs_before + i;
12598
12599 /* Display continuation glyphs. */
12600 if (!FRAME_WINDOW_P (it->f))
12601 produce_special_glyphs (it, IT_CONTINUATION);
12602 row->continued_p = 1;
12603
12604 it->current_x = x;
12605 it->continuation_lines_width += x;
12606 if (nglyphs > 1 && i > 0)
12607 {
12608 row->ends_in_middle_of_char_p = 1;
12609 it->starts_in_middle_of_char_p = 1;
12610 }
12611
12612 /* Restore the height to what it was before the
12613 element not fitting on the line. */
12614 it->max_ascent = ascent;
12615 it->max_descent = descent;
12616 it->max_phys_ascent = phys_ascent;
12617 it->max_phys_descent = phys_descent;
12618 }
12619
12620 break;
12621 }
12622 else if (new_x > it->first_visible_x)
12623 {
12624 /* Increment number of glyphs actually displayed. */
12625 ++it->hpos;
12626
12627 if (x < it->first_visible_x)
12628 /* Glyph is partially visible, i.e. row starts at
12629 negative X position. */
12630 row->x = x - it->first_visible_x;
12631 }
12632 else
12633 {
12634 /* Glyph is completely off the left margin of the
12635 window. This should not happen because of the
12636 move_it_in_display_line at the start of
12637 this function. */
12638 abort ();
12639 }
12640 }
12641
12642 row->ascent = max (row->ascent, it->max_ascent);
12643 row->height = max (row->height, it->max_ascent + it->max_descent);
12644 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12645 row->phys_height = max (row->phys_height,
12646 it->max_phys_ascent + it->max_phys_descent);
12647
12648 /* End of this display line if row is continued. */
12649 if (row->continued_p)
12650 break;
12651 }
12652
12653 /* Is this a line end? If yes, we're also done, after making
12654 sure that a non-default face is extended up to the right
12655 margin of the window. */
12656 if (ITERATOR_AT_END_OF_LINE_P (it))
12657 {
12658 int used_before = row->used[TEXT_AREA];
12659
12660 /* Add a space at the end of the line that is used to
12661 display the cursor there. */
12662 append_space (it, 0);
12663
12664 /* Extend the face to the end of the line. */
12665 extend_face_to_end_of_line (it);
12666
12667 /* Make sure we have the position. */
12668 if (used_before == 0)
12669 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
12670
12671 /* Consume the line end. This skips over invisible lines. */
12672 set_iterator_to_next (it, 1);
12673 it->continuation_lines_width = 0;
12674 break;
12675 }
12676
12677 /* Proceed with next display element. Note that this skips
12678 over lines invisible because of selective display. */
12679 set_iterator_to_next (it, 1);
12680
12681 /* If we truncate lines, we are done when the last displayed
12682 glyphs reach past the right margin of the window. */
12683 if (it->truncate_lines_p
12684 && (FRAME_WINDOW_P (it->f)
12685 ? (it->current_x >= it->last_visible_x)
12686 : (it->current_x > it->last_visible_x)))
12687 {
12688 /* Maybe add truncation glyphs. */
12689 if (!FRAME_WINDOW_P (it->f))
12690 {
12691 int i, n;
12692
12693 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
12694 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
12695 break;
12696
12697 for (n = row->used[TEXT_AREA]; i < n; ++i)
12698 {
12699 row->used[TEXT_AREA] = i;
12700 produce_special_glyphs (it, IT_TRUNCATION);
12701 }
12702 }
12703
12704 row->truncated_on_right_p = 1;
12705 it->continuation_lines_width = 0;
12706 reseat_at_next_visible_line_start (it, 0);
12707 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
12708 it->hpos = hpos_before;
12709 it->current_x = x_before;
12710 break;
12711 }
12712 }
12713
12714 /* If line is not empty and hscrolled, maybe insert truncation glyphs
12715 at the left window margin. */
12716 if (it->first_visible_x
12717 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
12718 {
12719 if (!FRAME_WINDOW_P (it->f))
12720 insert_left_trunc_glyphs (it);
12721 row->truncated_on_left_p = 1;
12722 }
12723
12724 /* If the start of this line is the overlay arrow-position, then
12725 mark this glyph row as the one containing the overlay arrow.
12726 This is clearly a mess with variable size fonts. It would be
12727 better to let it be displayed like cursors under X. */
12728 if (MARKERP (Voverlay_arrow_position)
12729 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
12730 && (MATRIX_ROW_START_CHARPOS (row)
12731 == marker_position (Voverlay_arrow_position))
12732 && STRINGP (Voverlay_arrow_string)
12733 && ! overlay_arrow_seen)
12734 {
12735 /* Overlay arrow in window redisplay is a bitmap. */
12736 if (!FRAME_WINDOW_P (it->f))
12737 {
12738 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
12739 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
12740 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
12741 struct glyph *p = row->glyphs[TEXT_AREA];
12742 struct glyph *p2, *end;
12743
12744 /* Copy the arrow glyphs. */
12745 while (glyph < arrow_end)
12746 *p++ = *glyph++;
12747
12748 /* Throw away padding glyphs. */
12749 p2 = p;
12750 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
12751 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
12752 ++p2;
12753 if (p2 > p)
12754 {
12755 while (p2 < end)
12756 *p++ = *p2++;
12757 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
12758 }
12759 }
12760
12761 overlay_arrow_seen = 1;
12762 row->overlay_arrow_p = 1;
12763 }
12764
12765 /* Compute pixel dimensions of this line. */
12766 compute_line_metrics (it);
12767
12768 /* Remember the position at which this line ends. */
12769 row->end = it->current;
12770
12771 /* Maybe set the cursor. */
12772 if (it->w->cursor.vpos < 0
12773 && PT >= MATRIX_ROW_START_CHARPOS (row)
12774 && PT <= MATRIX_ROW_END_CHARPOS (row)
12775 && cursor_row_p (it->w, row))
12776 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
12777
12778 /* Highlight trailing whitespace. */
12779 if (!NILP (Vshow_trailing_whitespace))
12780 highlight_trailing_whitespace (it->f, it->glyph_row);
12781
12782 /* Prepare for the next line. This line starts horizontally at (X
12783 HPOS) = (0 0). Vertical positions are incremented. As a
12784 convenience for the caller, IT->glyph_row is set to the next
12785 row to be used. */
12786 it->current_x = it->hpos = 0;
12787 it->current_y += row->height;
12788 ++it->vpos;
12789 ++it->glyph_row;
12790 return row->displays_text_p;
12791 }
12792
12793
12794 \f
12795 /***********************************************************************
12796 Menu Bar
12797 ***********************************************************************/
12798
12799 /* Redisplay the menu bar in the frame for window W.
12800
12801 The menu bar of X frames that don't have X toolkit support is
12802 displayed in a special window W->frame->menu_bar_window.
12803
12804 The menu bar of terminal frames is treated specially as far as
12805 glyph matrices are concerned. Menu bar lines are not part of
12806 windows, so the update is done directly on the frame matrix rows
12807 for the menu bar. */
12808
12809 static void
12810 display_menu_bar (w)
12811 struct window *w;
12812 {
12813 struct frame *f = XFRAME (WINDOW_FRAME (w));
12814 struct it it;
12815 Lisp_Object items;
12816 int i;
12817
12818 /* Don't do all this for graphical frames. */
12819 #ifdef HAVE_NTGUI
12820 if (!NILP (Vwindow_system))
12821 return;
12822 #endif
12823 #ifdef USE_X_TOOLKIT
12824 if (FRAME_X_P (f))
12825 return;
12826 #endif
12827 #ifdef macintosh
12828 if (FRAME_MAC_P (f))
12829 return;
12830 #endif
12831
12832 #ifdef USE_X_TOOLKIT
12833 xassert (!FRAME_WINDOW_P (f));
12834 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
12835 it.first_visible_x = 0;
12836 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
12837 #else /* not USE_X_TOOLKIT */
12838 if (FRAME_WINDOW_P (f))
12839 {
12840 /* Menu bar lines are displayed in the desired matrix of the
12841 dummy window menu_bar_window. */
12842 struct window *menu_w;
12843 xassert (WINDOWP (f->menu_bar_window));
12844 menu_w = XWINDOW (f->menu_bar_window);
12845 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
12846 MENU_FACE_ID);
12847 it.first_visible_x = 0;
12848 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
12849 }
12850 else
12851 {
12852 /* This is a TTY frame, i.e. character hpos/vpos are used as
12853 pixel x/y. */
12854 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
12855 MENU_FACE_ID);
12856 it.first_visible_x = 0;
12857 it.last_visible_x = FRAME_WIDTH (f);
12858 }
12859 #endif /* not USE_X_TOOLKIT */
12860
12861 if (! mode_line_inverse_video)
12862 /* Force the menu-bar to be displayed in the default face. */
12863 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
12864
12865 /* Clear all rows of the menu bar. */
12866 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
12867 {
12868 struct glyph_row *row = it.glyph_row + i;
12869 clear_glyph_row (row);
12870 row->enabled_p = 1;
12871 row->full_width_p = 1;
12872 }
12873
12874 /* Display all items of the menu bar. */
12875 items = FRAME_MENU_BAR_ITEMS (it.f);
12876 for (i = 0; i < XVECTOR (items)->size; i += 4)
12877 {
12878 Lisp_Object string;
12879
12880 /* Stop at nil string. */
12881 string = AREF (items, i + 1);
12882 if (NILP (string))
12883 break;
12884
12885 /* Remember where item was displayed. */
12886 AREF (items, i + 3) = make_number (it.hpos);
12887
12888 /* Display the item, pad with one space. */
12889 if (it.current_x < it.last_visible_x)
12890 display_string (NULL, string, Qnil, 0, 0, &it,
12891 XSTRING (string)->size + 1, 0, 0, -1);
12892 }
12893
12894 /* Fill out the line with spaces. */
12895 if (it.current_x < it.last_visible_x)
12896 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
12897
12898 /* Compute the total height of the lines. */
12899 compute_line_metrics (&it);
12900 }
12901
12902
12903 \f
12904 /***********************************************************************
12905 Mode Line
12906 ***********************************************************************/
12907
12908 /* Redisplay mode lines in the window tree whose root is WINDOW. If
12909 FORCE is non-zero, redisplay mode lines unconditionally.
12910 Otherwise, redisplay only mode lines that are garbaged. Value is
12911 the number of windows whose mode lines were redisplayed. */
12912
12913 static int
12914 redisplay_mode_lines (window, force)
12915 Lisp_Object window;
12916 int force;
12917 {
12918 int nwindows = 0;
12919
12920 while (!NILP (window))
12921 {
12922 struct window *w = XWINDOW (window);
12923
12924 if (WINDOWP (w->hchild))
12925 nwindows += redisplay_mode_lines (w->hchild, force);
12926 else if (WINDOWP (w->vchild))
12927 nwindows += redisplay_mode_lines (w->vchild, force);
12928 else if (force
12929 || FRAME_GARBAGED_P (XFRAME (w->frame))
12930 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
12931 {
12932 Lisp_Object old_selected_frame;
12933 struct text_pos lpoint;
12934 struct buffer *old = current_buffer;
12935
12936 /* Set the window's buffer for the mode line display. */
12937 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12938 set_buffer_internal_1 (XBUFFER (w->buffer));
12939
12940 /* Point refers normally to the selected window. For any
12941 other window, set up appropriate value. */
12942 if (!EQ (window, selected_window))
12943 {
12944 struct text_pos pt;
12945
12946 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
12947 if (CHARPOS (pt) < BEGV)
12948 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
12949 else if (CHARPOS (pt) > (ZV - 1))
12950 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
12951 else
12952 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
12953 }
12954
12955 /* Temporarily set up the selected frame. */
12956 old_selected_frame = selected_frame;
12957 selected_frame = w->frame;
12958
12959 /* Display mode lines. */
12960 clear_glyph_matrix (w->desired_matrix);
12961 if (display_mode_lines (w))
12962 {
12963 ++nwindows;
12964 w->must_be_updated_p = 1;
12965 }
12966
12967 /* Restore old settings. */
12968 selected_frame = old_selected_frame;
12969 set_buffer_internal_1 (old);
12970 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
12971 }
12972
12973 window = w->next;
12974 }
12975
12976 return nwindows;
12977 }
12978
12979
12980 /* Display the mode and/or top line of window W. Value is the number
12981 of mode lines displayed. */
12982
12983 static int
12984 display_mode_lines (w)
12985 struct window *w;
12986 {
12987 int n = 0;
12988
12989 /* These will be set while the mode line specs are processed. */
12990 line_number_displayed = 0;
12991 w->column_number_displayed = Qnil;
12992
12993 if (WINDOW_WANTS_MODELINE_P (w))
12994 {
12995 display_mode_line (w, MODE_LINE_FACE_ID,
12996 current_buffer->mode_line_format);
12997 ++n;
12998 }
12999
13000 if (WINDOW_WANTS_HEADER_LINE_P (w))
13001 {
13002 display_mode_line (w, HEADER_LINE_FACE_ID,
13003 current_buffer->header_line_format);
13004 ++n;
13005 }
13006
13007 return n;
13008 }
13009
13010
13011 /* Display mode or top line of window W. FACE_ID specifies which line
13012 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
13013 FORMAT is the mode line format to display. Value is the pixel
13014 height of the mode line displayed. */
13015
13016 static int
13017 display_mode_line (w, face_id, format)
13018 struct window *w;
13019 enum face_id face_id;
13020 Lisp_Object format;
13021 {
13022 struct it it;
13023 struct face *face;
13024
13025 init_iterator (&it, w, -1, -1, NULL, face_id);
13026 prepare_desired_row (it.glyph_row);
13027
13028 if (! mode_line_inverse_video)
13029 /* Force the mode-line to be displayed in the default face. */
13030 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
13031
13032 /* Temporarily make frame's keyboard the current kboard so that
13033 kboard-local variables in the mode_line_format will get the right
13034 values. */
13035 push_frame_kboard (it.f);
13036 display_mode_element (&it, 0, 0, 0, format);
13037 pop_frame_kboard ();
13038
13039 /* Fill up with spaces. */
13040 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
13041
13042 compute_line_metrics (&it);
13043 it.glyph_row->full_width_p = 1;
13044 it.glyph_row->mode_line_p = 1;
13045 it.glyph_row->inverse_p = 0;
13046 it.glyph_row->continued_p = 0;
13047 it.glyph_row->truncated_on_left_p = 0;
13048 it.glyph_row->truncated_on_right_p = 0;
13049
13050 /* Make a 3D mode-line have a shadow at its right end. */
13051 face = FACE_FROM_ID (it.f, face_id);
13052 extend_face_to_end_of_line (&it);
13053 if (face->box != FACE_NO_BOX)
13054 {
13055 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
13056 + it.glyph_row->used[TEXT_AREA] - 1);
13057 last->right_box_line_p = 1;
13058 }
13059
13060 return it.glyph_row->height;
13061 }
13062
13063
13064 /* Contribute ELT to the mode line for window IT->w. How it
13065 translates into text depends on its data type.
13066
13067 IT describes the display environment in which we display, as usual.
13068
13069 DEPTH is the depth in recursion. It is used to prevent
13070 infinite recursion here.
13071
13072 FIELD_WIDTH is the number of characters the display of ELT should
13073 occupy in the mode line, and PRECISION is the maximum number of
13074 characters to display from ELT's representation. See
13075 display_string for details. *
13076
13077 Returns the hpos of the end of the text generated by ELT. */
13078
13079 static int
13080 display_mode_element (it, depth, field_width, precision, elt)
13081 struct it *it;
13082 int depth;
13083 int field_width, precision;
13084 Lisp_Object elt;
13085 {
13086 int n = 0, field, prec;
13087
13088 tail_recurse:
13089 if (depth > 10)
13090 goto invalid;
13091
13092 depth++;
13093
13094 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
13095 {
13096 case Lisp_String:
13097 {
13098 /* A string: output it and check for %-constructs within it. */
13099 unsigned char c;
13100 unsigned char *this = XSTRING (elt)->data;
13101 unsigned char *lisp_string = this;
13102
13103 while ((precision <= 0 || n < precision)
13104 && *this
13105 && (frame_title_ptr
13106 || it->current_x < it->last_visible_x))
13107 {
13108 unsigned char *last = this;
13109
13110 /* Advance to end of string or next format specifier. */
13111 while ((c = *this++) != '\0' && c != '%')
13112 ;
13113
13114 if (this - 1 != last)
13115 {
13116 /* Output to end of string or up to '%'. Field width
13117 is length of string. Don't output more than
13118 PRECISION allows us. */
13119 --this;
13120 prec = strwidth (last, this - last);
13121 if (precision > 0 && prec > precision - n)
13122 prec = precision - n;
13123
13124 if (frame_title_ptr)
13125 n += store_frame_title (last, 0, prec);
13126 else
13127 n += display_string (NULL, elt, Qnil, 0, last - lisp_string,
13128 it, 0, prec, 0, -1);
13129 }
13130 else /* c == '%' */
13131 {
13132 unsigned char *percent_position = this;
13133
13134 /* Get the specified minimum width. Zero means
13135 don't pad. */
13136 field = 0;
13137 while ((c = *this++) >= '0' && c <= '9')
13138 field = field * 10 + c - '0';
13139
13140 /* Don't pad beyond the total padding allowed. */
13141 if (field_width - n > 0 && field > field_width - n)
13142 field = field_width - n;
13143
13144 /* Note that either PRECISION <= 0 or N < PRECISION. */
13145 prec = precision - n;
13146
13147 if (c == 'M')
13148 n += display_mode_element (it, depth, field, prec,
13149 Vglobal_mode_string);
13150 else if (c != 0)
13151 {
13152 unsigned char *spec
13153 = decode_mode_spec (it->w, c, field, prec);
13154
13155 if (frame_title_ptr)
13156 n += store_frame_title (spec, field, prec);
13157 else
13158 {
13159 int nglyphs_before
13160 = it->glyph_row->used[TEXT_AREA];
13161 int charpos
13162 = percent_position - XSTRING (elt)->data;
13163 int nwritten
13164 = display_string (spec, Qnil, elt, charpos, 0, it,
13165 field, prec, 0, -1);
13166
13167 /* Assign to the glyphs written above the
13168 string where the `%x' came from, position
13169 of the `%'. */
13170 if (nwritten > 0)
13171 {
13172 struct glyph *glyph
13173 = (it->glyph_row->glyphs[TEXT_AREA]
13174 + nglyphs_before);
13175 int i;
13176
13177 for (i = 0; i < nwritten; ++i)
13178 {
13179 glyph[i].object = elt;
13180 glyph[i].charpos = charpos;
13181 }
13182
13183 n += nwritten;
13184 }
13185 }
13186 }
13187 }
13188 }
13189 }
13190 break;
13191
13192 case Lisp_Symbol:
13193 /* A symbol: process the value of the symbol recursively
13194 as if it appeared here directly. Avoid error if symbol void.
13195 Special case: if value of symbol is a string, output the string
13196 literally. */
13197 {
13198 register Lisp_Object tem;
13199 tem = Fboundp (elt);
13200 if (!NILP (tem))
13201 {
13202 tem = Fsymbol_value (elt);
13203 /* If value is a string, output that string literally:
13204 don't check for % within it. */
13205 if (STRINGP (tem))
13206 {
13207 prec = precision - n;
13208 if (frame_title_ptr)
13209 n += store_frame_title (XSTRING (tem)->data, -1, prec);
13210 else
13211 n += display_string (NULL, tem, Qnil, 0, 0, it,
13212 0, prec, 0, -1);
13213 }
13214 else if (!EQ (tem, elt))
13215 {
13216 /* Give up right away for nil or t. */
13217 elt = tem;
13218 goto tail_recurse;
13219 }
13220 }
13221 }
13222 break;
13223
13224 case Lisp_Cons:
13225 {
13226 register Lisp_Object car, tem;
13227
13228 /* A cons cell: three distinct cases.
13229 If first element is a string or a cons, process all the elements
13230 and effectively concatenate them.
13231 If first element is a negative number, truncate displaying cdr to
13232 at most that many characters. If positive, pad (with spaces)
13233 to at least that many characters.
13234 If first element is a symbol, process the cadr or caddr recursively
13235 according to whether the symbol's value is non-nil or nil. */
13236 car = XCAR (elt);
13237 if (EQ (car, QCeval) && CONSP (XCDR (elt)))
13238 {
13239 /* An element of the form (:eval FORM) means evaluate FORM
13240 and use the result as mode line elements. */
13241 struct gcpro gcpro1;
13242 Lisp_Object spec;
13243
13244 spec = safe_eval (XCAR (XCDR (elt)));
13245 GCPRO1 (spec);
13246 n += display_mode_element (it, depth, field_width - n,
13247 precision - n, spec);
13248 UNGCPRO;
13249 }
13250 else if (SYMBOLP (car))
13251 {
13252 tem = Fboundp (car);
13253 elt = XCDR (elt);
13254 if (!CONSP (elt))
13255 goto invalid;
13256 /* elt is now the cdr, and we know it is a cons cell.
13257 Use its car if CAR has a non-nil value. */
13258 if (!NILP (tem))
13259 {
13260 tem = Fsymbol_value (car);
13261 if (!NILP (tem))
13262 {
13263 elt = XCAR (elt);
13264 goto tail_recurse;
13265 }
13266 }
13267 /* Symbol's value is nil (or symbol is unbound)
13268 Get the cddr of the original list
13269 and if possible find the caddr and use that. */
13270 elt = XCDR (elt);
13271 if (NILP (elt))
13272 break;
13273 else if (!CONSP (elt))
13274 goto invalid;
13275 elt = XCAR (elt);
13276 goto tail_recurse;
13277 }
13278 else if (INTEGERP (car))
13279 {
13280 register int lim = XINT (car);
13281 elt = XCDR (elt);
13282 if (lim < 0)
13283 {
13284 /* Negative int means reduce maximum width. */
13285 if (precision <= 0)
13286 precision = -lim;
13287 else
13288 precision = min (precision, -lim);
13289 }
13290 else if (lim > 0)
13291 {
13292 /* Padding specified. Don't let it be more than
13293 current maximum. */
13294 if (precision > 0)
13295 lim = min (precision, lim);
13296
13297 /* If that's more padding than already wanted, queue it.
13298 But don't reduce padding already specified even if
13299 that is beyond the current truncation point. */
13300 field_width = max (lim, field_width);
13301 }
13302 goto tail_recurse;
13303 }
13304 else if (STRINGP (car) || CONSP (car))
13305 {
13306 register int limit = 50;
13307 /* Limit is to protect against circular lists. */
13308 while (CONSP (elt)
13309 && --limit > 0
13310 && (precision <= 0 || n < precision))
13311 {
13312 n += display_mode_element (it, depth, field_width - n,
13313 precision - n, XCAR (elt));
13314 elt = XCDR (elt);
13315 }
13316 }
13317 }
13318 break;
13319
13320 default:
13321 invalid:
13322 if (frame_title_ptr)
13323 n += store_frame_title ("*invalid*", 0, precision - n);
13324 else
13325 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
13326 precision - n, 0, 0);
13327 return n;
13328 }
13329
13330 /* Pad to FIELD_WIDTH. */
13331 if (field_width > 0 && n < field_width)
13332 {
13333 if (frame_title_ptr)
13334 n += store_frame_title ("", field_width - n, 0);
13335 else
13336 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
13337 0, 0, 0);
13338 }
13339
13340 return n;
13341 }
13342
13343
13344 /* Write a null-terminated, right justified decimal representation of
13345 the positive integer D to BUF using a minimal field width WIDTH. */
13346
13347 static void
13348 pint2str (buf, width, d)
13349 register char *buf;
13350 register int width;
13351 register int d;
13352 {
13353 register char *p = buf;
13354
13355 if (d <= 0)
13356 *p++ = '0';
13357 else
13358 {
13359 while (d > 0)
13360 {
13361 *p++ = d % 10 + '0';
13362 d /= 10;
13363 }
13364 }
13365
13366 for (width -= (int) (p - buf); width > 0; --width)
13367 *p++ = ' ';
13368 *p-- = '\0';
13369 while (p > buf)
13370 {
13371 d = *buf;
13372 *buf++ = *p;
13373 *p-- = d;
13374 }
13375 }
13376
13377 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
13378 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
13379 type of CODING_SYSTEM. Return updated pointer into BUF. */
13380
13381 static unsigned char invalid_eol_type[] = "(*invalid*)";
13382
13383 static char *
13384 decode_mode_spec_coding (coding_system, buf, eol_flag)
13385 Lisp_Object coding_system;
13386 register char *buf;
13387 int eol_flag;
13388 {
13389 Lisp_Object val;
13390 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
13391 unsigned char *eol_str;
13392 int eol_str_len;
13393 /* The EOL conversion we are using. */
13394 Lisp_Object eoltype;
13395
13396 val = Fget (coding_system, Qcoding_system);
13397 eoltype = Qnil;
13398
13399 if (!VECTORP (val)) /* Not yet decided. */
13400 {
13401 if (multibyte)
13402 *buf++ = '-';
13403 if (eol_flag)
13404 eoltype = eol_mnemonic_undecided;
13405 /* Don't mention EOL conversion if it isn't decided. */
13406 }
13407 else
13408 {
13409 Lisp_Object eolvalue;
13410
13411 eolvalue = Fget (coding_system, Qeol_type);
13412
13413 if (multibyte)
13414 *buf++ = XFASTINT (AREF (val, 1));
13415
13416 if (eol_flag)
13417 {
13418 /* The EOL conversion that is normal on this system. */
13419
13420 if (NILP (eolvalue)) /* Not yet decided. */
13421 eoltype = eol_mnemonic_undecided;
13422 else if (VECTORP (eolvalue)) /* Not yet decided. */
13423 eoltype = eol_mnemonic_undecided;
13424 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
13425 eoltype = (XFASTINT (eolvalue) == 0
13426 ? eol_mnemonic_unix
13427 : (XFASTINT (eolvalue) == 1
13428 ? eol_mnemonic_dos : eol_mnemonic_mac));
13429 }
13430 }
13431
13432 if (eol_flag)
13433 {
13434 /* Mention the EOL conversion if it is not the usual one. */
13435 if (STRINGP (eoltype))
13436 {
13437 eol_str = XSTRING (eoltype)->data;
13438 eol_str_len = XSTRING (eoltype)->size;
13439 }
13440 else if (INTEGERP (eoltype)
13441 && CHAR_VALID_P (XINT (eoltype), 0))
13442 {
13443 eol_str = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
13444 eol_str_len = CHAR_STRING (XINT (eoltype), eol_str);
13445 }
13446 else
13447 {
13448 eol_str = invalid_eol_type;
13449 eol_str_len = sizeof (invalid_eol_type) - 1;
13450 }
13451 bcopy (eol_str, buf, eol_str_len);
13452 buf += eol_str_len;
13453 }
13454
13455 return buf;
13456 }
13457
13458 /* Return a string for the output of a mode line %-spec for window W,
13459 generated by character C. PRECISION >= 0 means don't return a
13460 string longer than that value. FIELD_WIDTH > 0 means pad the
13461 string returned with spaces to that value. */
13462
13463 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
13464
13465 static char *
13466 decode_mode_spec (w, c, field_width, precision)
13467 struct window *w;
13468 register int c;
13469 int field_width, precision;
13470 {
13471 Lisp_Object obj;
13472 struct frame *f = XFRAME (WINDOW_FRAME (w));
13473 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
13474 struct buffer *b = XBUFFER (w->buffer);
13475
13476 obj = Qnil;
13477
13478 switch (c)
13479 {
13480 case '*':
13481 if (!NILP (b->read_only))
13482 return "%";
13483 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
13484 return "*";
13485 return "-";
13486
13487 case '+':
13488 /* This differs from %* only for a modified read-only buffer. */
13489 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
13490 return "*";
13491 if (!NILP (b->read_only))
13492 return "%";
13493 return "-";
13494
13495 case '&':
13496 /* This differs from %* in ignoring read-only-ness. */
13497 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
13498 return "*";
13499 return "-";
13500
13501 case '%':
13502 return "%";
13503
13504 case '[':
13505 {
13506 int i;
13507 char *p;
13508
13509 if (command_loop_level > 5)
13510 return "[[[... ";
13511 p = decode_mode_spec_buf;
13512 for (i = 0; i < command_loop_level; i++)
13513 *p++ = '[';
13514 *p = 0;
13515 return decode_mode_spec_buf;
13516 }
13517
13518 case ']':
13519 {
13520 int i;
13521 char *p;
13522
13523 if (command_loop_level > 5)
13524 return " ...]]]";
13525 p = decode_mode_spec_buf;
13526 for (i = 0; i < command_loop_level; i++)
13527 *p++ = ']';
13528 *p = 0;
13529 return decode_mode_spec_buf;
13530 }
13531
13532 case '-':
13533 {
13534 register int i;
13535
13536 /* Let lots_of_dashes be a string of infinite length. */
13537 if (field_width <= 0
13538 || field_width > sizeof (lots_of_dashes))
13539 {
13540 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
13541 decode_mode_spec_buf[i] = '-';
13542 decode_mode_spec_buf[i] = '\0';
13543 return decode_mode_spec_buf;
13544 }
13545 else
13546 return lots_of_dashes;
13547 }
13548
13549 case 'b':
13550 obj = b->name;
13551 break;
13552
13553 case 'c':
13554 {
13555 int col = current_column ();
13556 w->column_number_displayed = make_number (col);
13557 pint2str (decode_mode_spec_buf, field_width, col);
13558 return decode_mode_spec_buf;
13559 }
13560
13561 case 'F':
13562 /* %F displays the frame name. */
13563 if (!NILP (f->title))
13564 return (char *) XSTRING (f->title)->data;
13565 if (f->explicit_name || ! FRAME_WINDOW_P (f))
13566 return (char *) XSTRING (f->name)->data;
13567 return "Emacs";
13568
13569 case 'f':
13570 obj = b->filename;
13571 break;
13572
13573 case 'l':
13574 {
13575 int startpos = XMARKER (w->start)->charpos;
13576 int startpos_byte = marker_byte_position (w->start);
13577 int line, linepos, linepos_byte, topline;
13578 int nlines, junk;
13579 int height = XFASTINT (w->height);
13580
13581 /* If we decided that this buffer isn't suitable for line numbers,
13582 don't forget that too fast. */
13583 if (EQ (w->base_line_pos, w->buffer))
13584 goto no_value;
13585 /* But do forget it, if the window shows a different buffer now. */
13586 else if (BUFFERP (w->base_line_pos))
13587 w->base_line_pos = Qnil;
13588
13589 /* If the buffer is very big, don't waste time. */
13590 if (INTEGERP (Vline_number_display_limit)
13591 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
13592 {
13593 w->base_line_pos = Qnil;
13594 w->base_line_number = Qnil;
13595 goto no_value;
13596 }
13597
13598 if (!NILP (w->base_line_number)
13599 && !NILP (w->base_line_pos)
13600 && XFASTINT (w->base_line_pos) <= startpos)
13601 {
13602 line = XFASTINT (w->base_line_number);
13603 linepos = XFASTINT (w->base_line_pos);
13604 linepos_byte = buf_charpos_to_bytepos (b, linepos);
13605 }
13606 else
13607 {
13608 line = 1;
13609 linepos = BUF_BEGV (b);
13610 linepos_byte = BUF_BEGV_BYTE (b);
13611 }
13612
13613 /* Count lines from base line to window start position. */
13614 nlines = display_count_lines (linepos, linepos_byte,
13615 startpos_byte,
13616 startpos, &junk);
13617
13618 topline = nlines + line;
13619
13620 /* Determine a new base line, if the old one is too close
13621 or too far away, or if we did not have one.
13622 "Too close" means it's plausible a scroll-down would
13623 go back past it. */
13624 if (startpos == BUF_BEGV (b))
13625 {
13626 w->base_line_number = make_number (topline);
13627 w->base_line_pos = make_number (BUF_BEGV (b));
13628 }
13629 else if (nlines < height + 25 || nlines > height * 3 + 50
13630 || linepos == BUF_BEGV (b))
13631 {
13632 int limit = BUF_BEGV (b);
13633 int limit_byte = BUF_BEGV_BYTE (b);
13634 int position;
13635 int distance = (height * 2 + 30) * line_number_display_limit_width;
13636
13637 if (startpos - distance > limit)
13638 {
13639 limit = startpos - distance;
13640 limit_byte = CHAR_TO_BYTE (limit);
13641 }
13642
13643 nlines = display_count_lines (startpos, startpos_byte,
13644 limit_byte,
13645 - (height * 2 + 30),
13646 &position);
13647 /* If we couldn't find the lines we wanted within
13648 line_number_display_limit_width chars per line,
13649 give up on line numbers for this window. */
13650 if (position == limit_byte && limit == startpos - distance)
13651 {
13652 w->base_line_pos = w->buffer;
13653 w->base_line_number = Qnil;
13654 goto no_value;
13655 }
13656
13657 w->base_line_number = make_number (topline - nlines);
13658 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
13659 }
13660
13661 /* Now count lines from the start pos to point. */
13662 nlines = display_count_lines (startpos, startpos_byte,
13663 PT_BYTE, PT, &junk);
13664
13665 /* Record that we did display the line number. */
13666 line_number_displayed = 1;
13667
13668 /* Make the string to show. */
13669 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
13670 return decode_mode_spec_buf;
13671 no_value:
13672 {
13673 char* p = decode_mode_spec_buf;
13674 int pad = field_width - 2;
13675 while (pad-- > 0)
13676 *p++ = ' ';
13677 *p++ = '?';
13678 *p++ = '?';
13679 *p = '\0';
13680 return decode_mode_spec_buf;
13681 }
13682 }
13683 break;
13684
13685 case 'm':
13686 obj = b->mode_name;
13687 break;
13688
13689 case 'n':
13690 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
13691 return " Narrow";
13692 break;
13693
13694 case 'p':
13695 {
13696 int pos = marker_position (w->start);
13697 int total = BUF_ZV (b) - BUF_BEGV (b);
13698
13699 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
13700 {
13701 if (pos <= BUF_BEGV (b))
13702 return "All";
13703 else
13704 return "Bottom";
13705 }
13706 else if (pos <= BUF_BEGV (b))
13707 return "Top";
13708 else
13709 {
13710 if (total > 1000000)
13711 /* Do it differently for a large value, to avoid overflow. */
13712 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
13713 else
13714 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
13715 /* We can't normally display a 3-digit number,
13716 so get us a 2-digit number that is close. */
13717 if (total == 100)
13718 total = 99;
13719 sprintf (decode_mode_spec_buf, "%2d%%", total);
13720 return decode_mode_spec_buf;
13721 }
13722 }
13723
13724 /* Display percentage of size above the bottom of the screen. */
13725 case 'P':
13726 {
13727 int toppos = marker_position (w->start);
13728 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
13729 int total = BUF_ZV (b) - BUF_BEGV (b);
13730
13731 if (botpos >= BUF_ZV (b))
13732 {
13733 if (toppos <= BUF_BEGV (b))
13734 return "All";
13735 else
13736 return "Bottom";
13737 }
13738 else
13739 {
13740 if (total > 1000000)
13741 /* Do it differently for a large value, to avoid overflow. */
13742 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
13743 else
13744 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
13745 /* We can't normally display a 3-digit number,
13746 so get us a 2-digit number that is close. */
13747 if (total == 100)
13748 total = 99;
13749 if (toppos <= BUF_BEGV (b))
13750 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
13751 else
13752 sprintf (decode_mode_spec_buf, "%2d%%", total);
13753 return decode_mode_spec_buf;
13754 }
13755 }
13756
13757 case 's':
13758 /* status of process */
13759 obj = Fget_buffer_process (w->buffer);
13760 if (NILP (obj))
13761 return "no process";
13762 #ifdef subprocesses
13763 obj = Fsymbol_name (Fprocess_status (obj));
13764 #endif
13765 break;
13766
13767 case 't': /* indicate TEXT or BINARY */
13768 #ifdef MODE_LINE_BINARY_TEXT
13769 return MODE_LINE_BINARY_TEXT (b);
13770 #else
13771 return "T";
13772 #endif
13773
13774 case 'z':
13775 /* coding-system (not including end-of-line format) */
13776 case 'Z':
13777 /* coding-system (including end-of-line type) */
13778 {
13779 int eol_flag = (c == 'Z');
13780 char *p = decode_mode_spec_buf;
13781
13782 if (! FRAME_WINDOW_P (f))
13783 {
13784 /* No need to mention EOL here--the terminal never needs
13785 to do EOL conversion. */
13786 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
13787 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
13788 }
13789 p = decode_mode_spec_coding (b->buffer_file_coding_system,
13790 p, eol_flag);
13791
13792 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
13793 #ifdef subprocesses
13794 obj = Fget_buffer_process (Fcurrent_buffer ());
13795 if (PROCESSP (obj))
13796 {
13797 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
13798 p, eol_flag);
13799 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
13800 p, eol_flag);
13801 }
13802 #endif /* subprocesses */
13803 #endif /* 0 */
13804 *p = 0;
13805 return decode_mode_spec_buf;
13806 }
13807 }
13808
13809 if (STRINGP (obj))
13810 return (char *) XSTRING (obj)->data;
13811 else
13812 return "";
13813 }
13814
13815
13816 /* Count up to COUNT lines starting from START / START_BYTE.
13817 But don't go beyond LIMIT_BYTE.
13818 Return the number of lines thus found (always nonnegative).
13819
13820 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
13821
13822 static int
13823 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
13824 int start, start_byte, limit_byte, count;
13825 int *byte_pos_ptr;
13826 {
13827 register unsigned char *cursor;
13828 unsigned char *base;
13829
13830 register int ceiling;
13831 register unsigned char *ceiling_addr;
13832 int orig_count = count;
13833
13834 /* If we are not in selective display mode,
13835 check only for newlines. */
13836 int selective_display = (!NILP (current_buffer->selective_display)
13837 && !INTEGERP (current_buffer->selective_display));
13838
13839 if (count > 0)
13840 {
13841 while (start_byte < limit_byte)
13842 {
13843 ceiling = BUFFER_CEILING_OF (start_byte);
13844 ceiling = min (limit_byte - 1, ceiling);
13845 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
13846 base = (cursor = BYTE_POS_ADDR (start_byte));
13847 while (1)
13848 {
13849 if (selective_display)
13850 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
13851 ;
13852 else
13853 while (*cursor != '\n' && ++cursor != ceiling_addr)
13854 ;
13855
13856 if (cursor != ceiling_addr)
13857 {
13858 if (--count == 0)
13859 {
13860 start_byte += cursor - base + 1;
13861 *byte_pos_ptr = start_byte;
13862 return orig_count;
13863 }
13864 else
13865 if (++cursor == ceiling_addr)
13866 break;
13867 }
13868 else
13869 break;
13870 }
13871 start_byte += cursor - base;
13872 }
13873 }
13874 else
13875 {
13876 while (start_byte > limit_byte)
13877 {
13878 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
13879 ceiling = max (limit_byte, ceiling);
13880 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
13881 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
13882 while (1)
13883 {
13884 if (selective_display)
13885 while (--cursor != ceiling_addr
13886 && *cursor != '\n' && *cursor != 015)
13887 ;
13888 else
13889 while (--cursor != ceiling_addr && *cursor != '\n')
13890 ;
13891
13892 if (cursor != ceiling_addr)
13893 {
13894 if (++count == 0)
13895 {
13896 start_byte += cursor - base + 1;
13897 *byte_pos_ptr = start_byte;
13898 /* When scanning backwards, we should
13899 not count the newline posterior to which we stop. */
13900 return - orig_count - 1;
13901 }
13902 }
13903 else
13904 break;
13905 }
13906 /* Here we add 1 to compensate for the last decrement
13907 of CURSOR, which took it past the valid range. */
13908 start_byte += cursor - base + 1;
13909 }
13910 }
13911
13912 *byte_pos_ptr = limit_byte;
13913
13914 if (count < 0)
13915 return - orig_count + count;
13916 return orig_count - count;
13917
13918 }
13919
13920
13921 \f
13922 /***********************************************************************
13923 Displaying strings
13924 ***********************************************************************/
13925
13926 /* Display a NUL-terminated string, starting with index START.
13927
13928 If STRING is non-null, display that C string. Otherwise, the Lisp
13929 string LISP_STRING is displayed.
13930
13931 If FACE_STRING is not nil, FACE_STRING_POS is a position in
13932 FACE_STRING. Display STRING or LISP_STRING with the face at
13933 FACE_STRING_POS in FACE_STRING:
13934
13935 Display the string in the environment given by IT, but use the
13936 standard display table, temporarily.
13937
13938 FIELD_WIDTH is the minimum number of output glyphs to produce.
13939 If STRING has fewer characters than FIELD_WIDTH, pad to the right
13940 with spaces. If STRING has more characters, more than FIELD_WIDTH
13941 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
13942
13943 PRECISION is the maximum number of characters to output from
13944 STRING. PRECISION < 0 means don't truncate the string.
13945
13946 This is roughly equivalent to printf format specifiers:
13947
13948 FIELD_WIDTH PRECISION PRINTF
13949 ----------------------------------------
13950 -1 -1 %s
13951 -1 10 %.10s
13952 10 -1 %10s
13953 20 10 %20.10s
13954
13955 MULTIBYTE zero means do not display multibyte chars, > 0 means do
13956 display them, and < 0 means obey the current buffer's value of
13957 enable_multibyte_characters.
13958
13959 Value is the number of glyphs produced. */
13960
13961 static int
13962 display_string (string, lisp_string, face_string, face_string_pos,
13963 start, it, field_width, precision, max_x, multibyte)
13964 unsigned char *string;
13965 Lisp_Object lisp_string;
13966 Lisp_Object face_string;
13967 int face_string_pos;
13968 int start;
13969 struct it *it;
13970 int field_width, precision, max_x;
13971 int multibyte;
13972 {
13973 int hpos_at_start = it->hpos;
13974 int saved_face_id = it->face_id;
13975 struct glyph_row *row = it->glyph_row;
13976
13977 /* Initialize the iterator IT for iteration over STRING beginning
13978 with index START. */
13979 reseat_to_string (it, string, lisp_string, start,
13980 precision, field_width, multibyte);
13981
13982 /* If displaying STRING, set up the face of the iterator
13983 from LISP_STRING, if that's given. */
13984 if (STRINGP (face_string))
13985 {
13986 int endptr;
13987 struct face *face;
13988
13989 it->face_id
13990 = face_at_string_position (it->w, face_string, face_string_pos,
13991 0, it->region_beg_charpos,
13992 it->region_end_charpos,
13993 &endptr, it->base_face_id, 0);
13994 face = FACE_FROM_ID (it->f, it->face_id);
13995 it->face_box_p = face->box != FACE_NO_BOX;
13996 }
13997
13998 /* Set max_x to the maximum allowed X position. Don't let it go
13999 beyond the right edge of the window. */
14000 if (max_x <= 0)
14001 max_x = it->last_visible_x;
14002 else
14003 max_x = min (max_x, it->last_visible_x);
14004
14005 /* Skip over display elements that are not visible. because IT->w is
14006 hscrolled. */
14007 if (it->current_x < it->first_visible_x)
14008 move_it_in_display_line_to (it, 100000, it->first_visible_x,
14009 MOVE_TO_POS | MOVE_TO_X);
14010
14011 row->ascent = it->max_ascent;
14012 row->height = it->max_ascent + it->max_descent;
14013 row->phys_ascent = it->max_phys_ascent;
14014 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14015
14016 /* This condition is for the case that we are called with current_x
14017 past last_visible_x. */
14018 while (it->current_x < max_x)
14019 {
14020 int x_before, x, n_glyphs_before, i, nglyphs;
14021
14022 /* Get the next display element. */
14023 if (!get_next_display_element (it))
14024 break;
14025
14026 /* Produce glyphs. */
14027 x_before = it->current_x;
14028 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
14029 PRODUCE_GLYPHS (it);
14030
14031 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
14032 i = 0;
14033 x = x_before;
14034 while (i < nglyphs)
14035 {
14036 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
14037
14038 if (!it->truncate_lines_p
14039 && x + glyph->pixel_width > max_x)
14040 {
14041 /* End of continued line or max_x reached. */
14042 if (CHAR_GLYPH_PADDING_P (*glyph))
14043 {
14044 /* A wide character is unbreakable. */
14045 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
14046 it->current_x = x_before;
14047 }
14048 else
14049 {
14050 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
14051 it->current_x = x;
14052 }
14053 break;
14054 }
14055 else if (x + glyph->pixel_width > it->first_visible_x)
14056 {
14057 /* Glyph is at least partially visible. */
14058 ++it->hpos;
14059 if (x < it->first_visible_x)
14060 it->glyph_row->x = x - it->first_visible_x;
14061 }
14062 else
14063 {
14064 /* Glyph is off the left margin of the display area.
14065 Should not happen. */
14066 abort ();
14067 }
14068
14069 row->ascent = max (row->ascent, it->max_ascent);
14070 row->height = max (row->height, it->max_ascent + it->max_descent);
14071 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14072 row->phys_height = max (row->phys_height,
14073 it->max_phys_ascent + it->max_phys_descent);
14074 x += glyph->pixel_width;
14075 ++i;
14076 }
14077
14078 /* Stop if max_x reached. */
14079 if (i < nglyphs)
14080 break;
14081
14082 /* Stop at line ends. */
14083 if (ITERATOR_AT_END_OF_LINE_P (it))
14084 {
14085 it->continuation_lines_width = 0;
14086 break;
14087 }
14088
14089 set_iterator_to_next (it, 1);
14090
14091 /* Stop if truncating at the right edge. */
14092 if (it->truncate_lines_p
14093 && it->current_x >= it->last_visible_x)
14094 {
14095 /* Add truncation mark, but don't do it if the line is
14096 truncated at a padding space. */
14097 if (IT_CHARPOS (*it) < it->string_nchars)
14098 {
14099 if (!FRAME_WINDOW_P (it->f))
14100 {
14101 int i, n;
14102
14103 if (it->current_x > it->last_visible_x)
14104 {
14105 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
14106 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
14107 break;
14108 for (n = row->used[TEXT_AREA]; i < n; ++i)
14109 {
14110 row->used[TEXT_AREA] = i;
14111 produce_special_glyphs (it, IT_TRUNCATION);
14112 }
14113 }
14114 produce_special_glyphs (it, IT_TRUNCATION);
14115 }
14116 it->glyph_row->truncated_on_right_p = 1;
14117 }
14118 break;
14119 }
14120 }
14121
14122 /* Maybe insert a truncation at the left. */
14123 if (it->first_visible_x
14124 && IT_CHARPOS (*it) > 0)
14125 {
14126 if (!FRAME_WINDOW_P (it->f))
14127 insert_left_trunc_glyphs (it);
14128 it->glyph_row->truncated_on_left_p = 1;
14129 }
14130
14131 it->face_id = saved_face_id;
14132
14133 /* Value is number of columns displayed. */
14134 return it->hpos - hpos_at_start;
14135 }
14136
14137
14138 \f
14139 /* This is like a combination of memq and assq. Return 1 if PROPVAL
14140 appears as an element of LIST or as the car of an element of LIST.
14141 If PROPVAL is a list, compare each element against LIST in that
14142 way, and return 1 if any element of PROPVAL is found in LIST.
14143 Otherwise return 0. This function cannot quit. */
14144
14145 int
14146 invisible_p (propval, list)
14147 register Lisp_Object propval;
14148 Lisp_Object list;
14149 {
14150 register Lisp_Object tail, proptail;
14151
14152 for (tail = list; CONSP (tail); tail = XCDR (tail))
14153 {
14154 register Lisp_Object tem;
14155 tem = XCAR (tail);
14156 if (EQ (propval, tem))
14157 return 1;
14158 if (CONSP (tem) && EQ (propval, XCAR (tem)))
14159 return 1;
14160 }
14161
14162 if (CONSP (propval))
14163 {
14164 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
14165 {
14166 Lisp_Object propelt;
14167 propelt = XCAR (proptail);
14168 for (tail = list; CONSP (tail); tail = XCDR (tail))
14169 {
14170 register Lisp_Object tem;
14171 tem = XCAR (tail);
14172 if (EQ (propelt, tem))
14173 return 1;
14174 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
14175 return 1;
14176 }
14177 }
14178 }
14179
14180 return 0;
14181 }
14182
14183
14184 /* Return 1 if PROPVAL appears as the car of an element of LIST and
14185 the cdr of that element is non-nil. If PROPVAL is a list, check
14186 each element of PROPVAL in that way, and the first time some
14187 element is found, return 1 if the cdr of that element is non-nil.
14188 Otherwise return 0. This function cannot quit. */
14189
14190 int
14191 invisible_ellipsis_p (propval, list)
14192 register Lisp_Object propval;
14193 Lisp_Object list;
14194 {
14195 register Lisp_Object tail, proptail;
14196
14197 for (tail = list; CONSP (tail); tail = XCDR (tail))
14198 {
14199 register Lisp_Object tem;
14200 tem = XCAR (tail);
14201 if (CONSP (tem) && EQ (propval, XCAR (tem)))
14202 return ! NILP (XCDR (tem));
14203 }
14204
14205 if (CONSP (propval))
14206 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
14207 {
14208 Lisp_Object propelt;
14209 propelt = XCAR (proptail);
14210 for (tail = list; CONSP (tail); tail = XCDR (tail))
14211 {
14212 register Lisp_Object tem;
14213 tem = XCAR (tail);
14214 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
14215 return ! NILP (XCDR (tem));
14216 }
14217 }
14218
14219 return 0;
14220 }
14221
14222
14223 \f
14224 /***********************************************************************
14225 Initialization
14226 ***********************************************************************/
14227
14228 void
14229 syms_of_xdisp ()
14230 {
14231 Vwith_echo_area_save_vector = Qnil;
14232 staticpro (&Vwith_echo_area_save_vector);
14233
14234 Vmessage_stack = Qnil;
14235 staticpro (&Vmessage_stack);
14236
14237 Qinhibit_redisplay = intern ("inhibit-redisplay");
14238 staticpro (&Qinhibit_redisplay);
14239
14240 #if GLYPH_DEBUG
14241 defsubr (&Sdump_glyph_matrix);
14242 defsubr (&Sdump_glyph_row);
14243 defsubr (&Sdump_tool_bar_row);
14244 defsubr (&Strace_redisplay_toggle);
14245 defsubr (&Strace_to_stderr);
14246 #endif
14247 #ifdef HAVE_WINDOW_SYSTEM
14248 defsubr (&Stool_bar_lines_needed);
14249 #endif
14250
14251 staticpro (&Qmenu_bar_update_hook);
14252 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
14253
14254 staticpro (&Qoverriding_terminal_local_map);
14255 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
14256
14257 staticpro (&Qoverriding_local_map);
14258 Qoverriding_local_map = intern ("overriding-local-map");
14259
14260 staticpro (&Qwindow_scroll_functions);
14261 Qwindow_scroll_functions = intern ("window-scroll-functions");
14262
14263 staticpro (&Qredisplay_end_trigger_functions);
14264 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
14265
14266 staticpro (&Qinhibit_point_motion_hooks);
14267 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
14268
14269 QCdata = intern (":data");
14270 staticpro (&QCdata);
14271 Qdisplay = intern ("display");
14272 staticpro (&Qdisplay);
14273 Qspace_width = intern ("space-width");
14274 staticpro (&Qspace_width);
14275 Qraise = intern ("raise");
14276 staticpro (&Qraise);
14277 Qspace = intern ("space");
14278 staticpro (&Qspace);
14279 Qmargin = intern ("margin");
14280 staticpro (&Qmargin);
14281 Qleft_margin = intern ("left-margin");
14282 staticpro (&Qleft_margin);
14283 Qright_margin = intern ("right-margin");
14284 staticpro (&Qright_margin);
14285 Qalign_to = intern ("align-to");
14286 staticpro (&Qalign_to);
14287 QCalign_to = intern (":align-to");
14288 staticpro (&QCalign_to);
14289 Qrelative_width = intern ("relative-width");
14290 staticpro (&Qrelative_width);
14291 QCrelative_width = intern (":relative-width");
14292 staticpro (&QCrelative_width);
14293 QCrelative_height = intern (":relative-height");
14294 staticpro (&QCrelative_height);
14295 QCeval = intern (":eval");
14296 staticpro (&QCeval);
14297 Qwhen = intern ("when");
14298 staticpro (&Qwhen);
14299 QCfile = intern (":file");
14300 staticpro (&QCfile);
14301 Qfontified = intern ("fontified");
14302 staticpro (&Qfontified);
14303 Qfontification_functions = intern ("fontification-functions");
14304 staticpro (&Qfontification_functions);
14305 Qtrailing_whitespace = intern ("trailing-whitespace");
14306 staticpro (&Qtrailing_whitespace);
14307 Qimage = intern ("image");
14308 staticpro (&Qimage);
14309 Qmessage_truncate_lines = intern ("message-truncate-lines");
14310 staticpro (&Qmessage_truncate_lines);
14311 Qgrow_only = intern ("grow-only");
14312 staticpro (&Qgrow_only);
14313 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
14314 staticpro (&Qinhibit_menubar_update);
14315 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
14316 staticpro (&Qinhibit_eval_during_redisplay);
14317
14318 last_arrow_position = Qnil;
14319 last_arrow_string = Qnil;
14320 staticpro (&last_arrow_position);
14321 staticpro (&last_arrow_string);
14322
14323 echo_buffer[0] = echo_buffer[1] = Qnil;
14324 staticpro (&echo_buffer[0]);
14325 staticpro (&echo_buffer[1]);
14326
14327 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
14328 staticpro (&echo_area_buffer[0]);
14329 staticpro (&echo_area_buffer[1]);
14330
14331 Vmessages_buffer_name = build_string ("*Messages*");
14332 staticpro (&Vmessages_buffer_name);
14333
14334 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
14335 "Non-nil means highlight trailing whitespace.\n\
14336 The face used for trailing whitespace is `trailing-whitespace'.");
14337 Vshow_trailing_whitespace = Qnil;
14338
14339 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
14340 "Non-nil means don't actually do any redisplay.\n\
14341 This is used for internal purposes.");
14342 Vinhibit_redisplay = Qnil;
14343
14344 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
14345 "String (or mode line construct) included (normally) in `mode-line-format'.");
14346 Vglobal_mode_string = Qnil;
14347
14348 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
14349 "Marker for where to display an arrow on top of the buffer text.\n\
14350 This must be the beginning of a line in order to work.\n\
14351 See also `overlay-arrow-string'.");
14352 Voverlay_arrow_position = Qnil;
14353
14354 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
14355 "String to display as an arrow. See also `overlay-arrow-position'.");
14356 Voverlay_arrow_string = Qnil;
14357
14358 DEFVAR_INT ("scroll-step", &scroll_step,
14359 "*The number of lines to try scrolling a window by when point moves out.\n\
14360 If that fails to bring point back on frame, point is centered instead.\n\
14361 If this is zero, point is always centered after it moves off frame.\n\
14362 If you want scrolling to always be a line at a time, you should set\n\
14363 `scroll-conservatively' to a large value rather than set this to 1.");
14364
14365 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
14366 "*Scroll up to this many lines, to bring point back on screen.\n\
14367 A value of zero means to scroll the text to center point vertically\n\
14368 in the window.");
14369 scroll_conservatively = 0;
14370
14371 DEFVAR_INT ("scroll-margin", &scroll_margin,
14372 "*Number of lines of margin at the top and bottom of a window.\n\
14373 Recenter the window whenever point gets within this many lines\n\
14374 of the top or bottom of the window.");
14375 scroll_margin = 0;
14376
14377 #if GLYPH_DEBUG
14378 DEFVAR_INT ("debug-end-pos", &debug_end_pos, "Don't ask");
14379 #endif
14380
14381 DEFVAR_BOOL ("truncate-partial-width-windows",
14382 &truncate_partial_width_windows,
14383 "*Non-nil means truncate lines in all windows less than full frame wide.");
14384 truncate_partial_width_windows = 1;
14385
14386 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
14387 "nil means display the mode-line/header-line/menu-bar in the default face.\n\
14388 Any other value means to use the appropriate face, `mode-line',\n\
14389 `header-line', or `menu' respectively.\n\
14390 \n\
14391 This variable is deprecated; please change the above faces instead.");
14392 mode_line_inverse_video = 1;
14393
14394 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
14395 "*Maximum buffer size for which line number should be displayed.\n\
14396 If the buffer is bigger than this, the line number does not appear\n\
14397 in the mode line. A value of nil means no limit.");
14398 Vline_number_display_limit = Qnil;
14399
14400 DEFVAR_INT ("line-number-display-limit-width",
14401 &line_number_display_limit_width,
14402 "*Maximum line width (in characters) for line number display.\n\
14403 If the average length of the lines near point is bigger than this, then the\n\
14404 line number may be omitted from the mode line.");
14405 line_number_display_limit_width = 200;
14406
14407 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
14408 "*Non-nil means highlight region even in nonselected windows.");
14409 highlight_nonselected_windows = 0;
14410
14411 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
14412 "Non-nil if more than one frame is visible on this display.\n\
14413 Minibuffer-only frames don't count, but iconified frames do.\n\
14414 This variable is not guaranteed to be accurate except while processing\n\
14415 `frame-title-format' and `icon-title-format'.");
14416
14417 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
14418 "Template for displaying the title bar of visible frames.\n\
14419 \(Assuming the window manager supports this feature.)\n\
14420 This variable has the same structure as `mode-line-format' (which see),\n\
14421 and is used only on frames for which no explicit name has been set\n\
14422 \(see `modify-frame-parameters').");
14423 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
14424 "Template for displaying the title bar of an iconified frame.\n\
14425 \(Assuming the window manager supports this feature.)\n\
14426 This variable has the same structure as `mode-line-format' (which see),\n\
14427 and is used only on frames for which no explicit name has been set\n\
14428 \(see `modify-frame-parameters').");
14429 Vicon_title_format
14430 = Vframe_title_format
14431 = Fcons (intern ("multiple-frames"),
14432 Fcons (build_string ("%b"),
14433 Fcons (Fcons (build_string (""),
14434 Fcons (intern ("invocation-name"),
14435 Fcons (build_string ("@"),
14436 Fcons (intern ("system-name"),
14437 Qnil)))),
14438 Qnil)));
14439
14440 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
14441 "Maximum number of lines to keep in the message log buffer.\n\
14442 If nil, disable message logging. If t, log messages but don't truncate\n\
14443 the buffer when it becomes large.");
14444 Vmessage_log_max = make_number (50);
14445
14446 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
14447 "Functions called before redisplay, if window sizes have changed.\n\
14448 The value should be a list of functions that take one argument.\n\
14449 Just before redisplay, for each frame, if any of its windows have changed\n\
14450 size since the last redisplay, or have been split or deleted,\n\
14451 all the functions in the list are called, with the frame as argument.");
14452 Vwindow_size_change_functions = Qnil;
14453
14454 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
14455 "List of Functions to call before redisplaying a window with scrolling.\n\
14456 Each function is called with two arguments, the window\n\
14457 and its new display-start position. Note that the value of `window-end'\n\
14458 is not valid when these functions are called.");
14459 Vwindow_scroll_functions = Qnil;
14460
14461 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
14462 "*Non-nil means automatically resize tool-bars.\n\
14463 This increases a tool-bar's height if not all tool-bar items are visible.\n\
14464 It decreases a tool-bar's height when it would display blank lines\n\
14465 otherwise.");
14466 auto_resize_tool_bars_p = 1;
14467
14468 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
14469 "*Non-nil means raise tool-bar buttons when the mouse moves over them.");
14470 auto_raise_tool_bar_buttons_p = 1;
14471
14472 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
14473 "*Margin around tool-bar buttons in pixels.\n\
14474 If an integer, use that for both horizontal and vertical margins.\n\
14475 Otherwise, value should be a pair of integers `(HORZ : VERT)' with\n\
14476 HORZ specifying the horizontal margin, and VERT specifying the\n\
14477 vertical margin.");
14478 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
14479
14480 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
14481 "Relief thickness of tool-bar buttons.");
14482 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
14483
14484 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
14485 "List of functions to call to fontify regions of text.\n\
14486 Each function is called with one argument POS. Functions must\n\
14487 fontify a region starting at POS in the current buffer, and give\n\
14488 fontified regions the property `fontified'.\n\
14489 This variable automatically becomes buffer-local when set.");
14490 Vfontification_functions = Qnil;
14491 Fmake_variable_buffer_local (Qfontification_functions);
14492
14493 DEFVAR_BOOL ("unibyte-display-via-language-environment",
14494 &unibyte_display_via_language_environment,
14495 "*Non-nil means display unibyte text according to language environment.\n\
14496 Specifically this means that unibyte non-ASCII characters\n\
14497 are displayed by converting them to the equivalent multibyte characters\n\
14498 according to the current language environment. As a result, they are\n\
14499 displayed according to the current fontset.");
14500 unibyte_display_via_language_environment = 0;
14501
14502 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
14503 "*Maximum height for resizing mini-windows.\n\
14504 If a float, it specifies a fraction of the mini-window frame's height.\n\
14505 If an integer, it specifies a number of lines.");
14506 Vmax_mini_window_height = make_float (0.25);
14507
14508 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
14509 "*How to resize mini-windows.\n\
14510 A value of nil means don't automatically resize mini-windows.\n\
14511 A value of t means resize them to fit the text displayed in them.\n\
14512 A value of `grow-only', the default, means let mini-windows grow\n\
14513 only, until their display becomes empty, at which point the windows\n\
14514 go back to their normal size.");
14515 Vresize_mini_windows = Qgrow_only;
14516
14517 DEFVAR_BOOL ("cursor-in-non-selected-windows",
14518 &cursor_in_non_selected_windows,
14519 "*Non-nil means display a hollow cursor in non-selected windows.\n\
14520 Nil means don't display a cursor there.");
14521 cursor_in_non_selected_windows = 1;
14522
14523 DEFVAR_BOOL ("automatic-hscrolling", &automatic_hscrolling_p,
14524 "*Non-nil means scroll the display automatically to make point visible.");
14525 automatic_hscrolling_p = 1;
14526
14527 DEFVAR_LISP ("image-types", &Vimage_types,
14528 "List of supported image types.\n\
14529 Each element of the list is a symbol for a supported image type.");
14530 Vimage_types = Qnil;
14531
14532 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
14533 "If non-nil, messages are truncated instead of resizing the echo area.\n\
14534 Bind this around calls to `message' to let it take effect.");
14535 message_truncate_lines = 0;
14536
14537 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
14538 "Normal hook run for clicks on menu bar, before displaying a submenu.\n\
14539 Can be used to update submenus whose contents should vary.");
14540 Vmenu_bar_update_hook = Qnil;
14541
14542 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
14543 "Non-nil means don't update menu bars. Internal use only.");
14544 inhibit_menubar_update = 0;
14545
14546 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
14547 "Non-nil means don't eval Lisp during redisplay.");
14548 inhibit_eval_during_redisplay = 0;
14549 }
14550
14551
14552 /* Initialize this module when Emacs starts. */
14553
14554 void
14555 init_xdisp ()
14556 {
14557 Lisp_Object root_window;
14558 struct window *mini_w;
14559
14560 current_header_line_height = current_mode_line_height = -1;
14561
14562 CHARPOS (this_line_start_pos) = 0;
14563
14564 mini_w = XWINDOW (minibuf_window);
14565 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
14566
14567 if (!noninteractive)
14568 {
14569 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
14570 int i;
14571
14572 XWINDOW (root_window)->top = make_number (FRAME_TOP_MARGIN (f));
14573 set_window_height (root_window,
14574 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
14575 0);
14576 mini_w->top = make_number (FRAME_HEIGHT (f) - 1);
14577 set_window_height (minibuf_window, 1, 0);
14578
14579 XWINDOW (root_window)->width = make_number (FRAME_WIDTH (f));
14580 mini_w->width = make_number (FRAME_WIDTH (f));
14581
14582 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
14583 scratch_glyph_row.glyphs[TEXT_AREA + 1]
14584 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
14585
14586 /* The default ellipsis glyphs `...'. */
14587 for (i = 0; i < 3; ++i)
14588 default_invis_vector[i] = make_number ('.');
14589 }
14590
14591 #ifdef HAVE_WINDOW_SYSTEM
14592 {
14593 /* Allocate the buffer for frame titles. */
14594 int size = 100;
14595 frame_title_buf = (char *) xmalloc (size);
14596 frame_title_buf_end = frame_title_buf + size;
14597 frame_title_ptr = NULL;
14598 }
14599 #endif /* HAVE_WINDOW_SYSTEM */
14600
14601 help_echo_showing_p = 0;
14602 }
14603
14604