Change defvar_int def and vars to use EMACS_INT instead of just int.
[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 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 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 displayed is then simple. It is
127 started by initializing an iterator with a call to init_iterator.
128 Calls to get_next_display_element fill the iterator structure with
129 relevant information about the next thing to display. Calls to
130 set_iterator_to_next move the iterator to the next thing.
131
132 Besides this, an iterator also contains information about the
133 display environment in which glyphs for display elements are to be
134 produced. It has fields for the width and height of the display,
135 the information whether long lines are truncated or continued, a
136 current X and Y position, and lots of other stuff you can better
137 see in dispextern.h.
138
139 Glyphs in a desired matrix are normally constructed in a loop
140 calling get_next_display_element and then produce_glyphs. The call
141 to produce_glyphs will fill the iterator structure with pixel
142 information about the element being displayed and at the same time
143 produce glyphs for it. If the display element fits on the line
144 being displayed, set_iterator_to_next is called next, otherwise the
145 glyphs produced are discarded.
146
147
148 Frame matrices.
149
150 That just couldn't be all, could it? What about terminal types not
151 supporting operations on sub-windows of the screen? To update the
152 display on such a terminal, window-based glyph matrices are not
153 well suited. To be able to reuse part of the display (scrolling
154 lines up and down), we must instead have a view of the whole
155 screen. This is what `frame matrices' are for. They are a trick.
156
157 Frames on terminals like above have a glyph pool. Windows on such
158 a frame sub-allocate their glyph memory from their frame's glyph
159 pool. The frame itself is given its own glyph matrices. By
160 coincidence---or maybe something else---rows in window glyph
161 matrices are slices of corresponding rows in frame matrices. Thus
162 writing to window matrices implicitly updates a frame matrix which
163 provides us with the view of the whole screen that we originally
164 wanted to have without having to move many bytes around. To be
165 honest, there is a little bit more done, but not much more. If you
166 plan to extend that code, take a look at dispnew.c. The function
167 build_frame_matrix is a good starting point. */
168
169 #include <config.h>
170 #include <stdio.h>
171
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 INFINITY 10000000
202
203 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
204 extern void set_frame_menubar P_ ((struct frame *f, int, int));
205 extern int pending_menu_activation;
206 #endif
207
208 extern int interrupt_input;
209 extern int command_loop_level;
210
211 extern int minibuffer_auto_raise;
212
213 extern Lisp_Object Qface;
214
215 extern Lisp_Object Voverriding_local_map;
216 extern Lisp_Object Voverriding_local_map_menu_flag;
217 extern Lisp_Object Qmenu_item;
218
219 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
220 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
221 Lisp_Object Qredisplay_end_trigger_functions;
222 Lisp_Object Qinhibit_point_motion_hooks;
223 Lisp_Object QCeval, Qwhen, QCfile, QCdata, QCpropertize;
224 Lisp_Object Qfontified;
225 Lisp_Object Qgrow_only;
226 Lisp_Object Qinhibit_eval_during_redisplay;
227 Lisp_Object Qbuffer_position, Qposition, Qobject;
228
229 /* Functions called to fontify regions of text. */
230
231 Lisp_Object Vfontification_functions;
232 Lisp_Object Qfontification_functions;
233
234 /* Non-zero means draw tool bar buttons raised when the mouse moves
235 over them. */
236
237 int auto_raise_tool_bar_buttons_p;
238
239 /* Margin around tool bar buttons in pixels. */
240
241 Lisp_Object Vtool_bar_button_margin;
242
243 /* Thickness of shadow to draw around tool bar buttons. */
244
245 EMACS_INT tool_bar_button_relief;
246
247 /* Non-zero means automatically resize tool-bars so that all tool-bar
248 items are visible, and no blank lines remain. */
249
250 int auto_resize_tool_bars_p;
251
252 /* Non-nil means don't actually do any redisplay. */
253
254 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
255
256 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
257
258 int inhibit_eval_during_redisplay;
259
260 /* Names of text properties relevant for redisplay. */
261
262 Lisp_Object Qdisplay, Qrelative_width, Qalign_to;
263 extern Lisp_Object Qface, Qinvisible, Qwidth;
264
265 /* Symbols used in text property values. */
266
267 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
268 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
269 Lisp_Object Qmargin;
270 extern Lisp_Object Qheight;
271
272 /* Non-nil means highlight trailing whitespace. */
273
274 Lisp_Object Vshow_trailing_whitespace;
275
276 /* Name of the face used to highlight trailing whitespace. */
277
278 Lisp_Object Qtrailing_whitespace;
279
280 /* The symbol `image' which is the car of the lists used to represent
281 images in Lisp. */
282
283 Lisp_Object Qimage;
284
285 /* Non-zero means print newline to stdout before next mini-buffer
286 message. */
287
288 int noninteractive_need_newline;
289
290 /* Non-zero means print newline to message log before next message. */
291
292 static int message_log_need_newline;
293
294 /* Three markers that message_dolog uses.
295 It could allocate them itself, but that causes trouble
296 in handling memory-full errors. */
297 static Lisp_Object message_dolog_marker1;
298 static Lisp_Object message_dolog_marker2;
299 static Lisp_Object message_dolog_marker3;
300 \f
301 /* The buffer position of the first character appearing entirely or
302 partially on the line of the selected window which contains the
303 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
304 redisplay optimization in redisplay_internal. */
305
306 static struct text_pos this_line_start_pos;
307
308 /* Number of characters past the end of the line above, including the
309 terminating newline. */
310
311 static struct text_pos this_line_end_pos;
312
313 /* The vertical positions and the height of this line. */
314
315 static int this_line_vpos;
316 static int this_line_y;
317 static int this_line_pixel_height;
318
319 /* X position at which this display line starts. Usually zero;
320 negative if first character is partially visible. */
321
322 static int this_line_start_x;
323
324 /* Buffer that this_line_.* variables are referring to. */
325
326 static struct buffer *this_line_buffer;
327
328 /* Nonzero means truncate lines in all windows less wide than the
329 frame. */
330
331 int truncate_partial_width_windows;
332
333 /* A flag to control how to display unibyte 8-bit character. */
334
335 int unibyte_display_via_language_environment;
336
337 /* Nonzero means we have more than one non-mini-buffer-only frame.
338 Not guaranteed to be accurate except while parsing
339 frame-title-format. */
340
341 int multiple_frames;
342
343 Lisp_Object Vglobal_mode_string;
344
345 /* Marker for where to display an arrow on top of the buffer text. */
346
347 Lisp_Object Voverlay_arrow_position;
348
349 /* String to display for the arrow. Only used on terminal frames. */
350
351 Lisp_Object Voverlay_arrow_string;
352
353 /* Values of those variables at last redisplay. However, if
354 Voverlay_arrow_position is a marker, last_arrow_position is its
355 numerical position. */
356
357 static Lisp_Object last_arrow_position, last_arrow_string;
358
359 /* Like mode-line-format, but for the title bar on a visible frame. */
360
361 Lisp_Object Vframe_title_format;
362
363 /* Like mode-line-format, but for the title bar on an iconified frame. */
364
365 Lisp_Object Vicon_title_format;
366
367 /* List of functions to call when a window's size changes. These
368 functions get one arg, a frame on which one or more windows' sizes
369 have changed. */
370
371 static Lisp_Object Vwindow_size_change_functions;
372
373 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
374
375 /* Nonzero if overlay arrow has been displayed once in this window. */
376
377 static int overlay_arrow_seen;
378
379 /* Nonzero means highlight the region even in nonselected windows. */
380
381 int highlight_nonselected_windows;
382
383 /* If cursor motion alone moves point off frame, try scrolling this
384 many lines up or down if that will bring it back. */
385
386 static EMACS_INT scroll_step;
387
388 /* Nonzero means scroll just far enough to bring point back on the
389 screen, when appropriate. */
390
391 static EMACS_INT scroll_conservatively;
392
393 /* Recenter the window whenever point gets within this many lines of
394 the top or bottom of the window. This value is translated into a
395 pixel value by multiplying it with CANON_Y_UNIT, which means that
396 there is really a fixed pixel height scroll margin. */
397
398 EMACS_INT scroll_margin;
399
400 /* Number of windows showing the buffer of the selected window (or
401 another buffer with the same base buffer). keyboard.c refers to
402 this. */
403
404 int buffer_shared;
405
406 /* Vector containing glyphs for an ellipsis `...'. */
407
408 static Lisp_Object default_invis_vector[3];
409
410 /* Zero means display the mode-line/header-line/menu-bar in the default face
411 (this slightly odd definition is for compatibility with previous versions
412 of emacs), non-zero means display them using their respective faces.
413
414 This variable is deprecated. */
415
416 int mode_line_inverse_video;
417
418 /* Prompt to display in front of the mini-buffer contents. */
419
420 Lisp_Object minibuf_prompt;
421
422 /* Width of current mini-buffer prompt. Only set after display_line
423 of the line that contains the prompt. */
424
425 int minibuf_prompt_width;
426 int minibuf_prompt_pixel_width;
427
428 /* This is the window where the echo area message was displayed. It
429 is always a mini-buffer window, but it may not be the same window
430 currently active as a mini-buffer. */
431
432 Lisp_Object echo_area_window;
433
434 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
435 pushes the current message and the value of
436 message_enable_multibyte on the stack, the function restore_message
437 pops the stack and displays MESSAGE again. */
438
439 Lisp_Object Vmessage_stack;
440
441 /* Nonzero means multibyte characters were enabled when the echo area
442 message was specified. */
443
444 int message_enable_multibyte;
445
446 /* Nonzero if we should redraw the mode lines on the next redisplay. */
447
448 int update_mode_lines;
449
450 /* Nonzero if window sizes or contents have changed since last
451 redisplay that finished. */
452
453 int windows_or_buffers_changed;
454
455 /* Nonzero after display_mode_line if %l was used and it displayed a
456 line number. */
457
458 int line_number_displayed;
459
460 /* Maximum buffer size for which to display line numbers. */
461
462 Lisp_Object Vline_number_display_limit;
463
464 /* Line width to consider when repositioning for line number display. */
465
466 static EMACS_INT line_number_display_limit_width;
467
468 /* Number of lines to keep in the message log buffer. t means
469 infinite. nil means don't log at all. */
470
471 Lisp_Object Vmessage_log_max;
472
473 /* The name of the *Messages* buffer, a string. */
474
475 static Lisp_Object Vmessages_buffer_name;
476
477 /* Current, index 0, and last displayed echo area message. Either
478 buffers from echo_buffers, or nil to indicate no message. */
479
480 Lisp_Object echo_area_buffer[2];
481
482 /* The buffers referenced from echo_area_buffer. */
483
484 static Lisp_Object echo_buffer[2];
485
486 /* A vector saved used in with_area_buffer to reduce consing. */
487
488 static Lisp_Object Vwith_echo_area_save_vector;
489
490 /* Non-zero means display_echo_area should display the last echo area
491 message again. Set by redisplay_preserve_echo_area. */
492
493 static int display_last_displayed_message_p;
494
495 /* Nonzero if echo area is being used by print; zero if being used by
496 message. */
497
498 int message_buf_print;
499
500 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
501
502 Lisp_Object Qinhibit_menubar_update;
503 int inhibit_menubar_update;
504
505 /* Maximum height for resizing mini-windows. Either a float
506 specifying a fraction of the available height, or an integer
507 specifying a number of lines. */
508
509 Lisp_Object Vmax_mini_window_height;
510
511 /* Non-zero means messages should be displayed with truncated
512 lines instead of being continued. */
513
514 int message_truncate_lines;
515 Lisp_Object Qmessage_truncate_lines;
516
517 /* Set to 1 in clear_message to make redisplay_internal aware
518 of an emptied echo area. */
519
520 static int message_cleared_p;
521
522 /* Non-zero means we want a hollow cursor in windows that are not
523 selected. Zero means there's no cursor in such windows. */
524
525 int cursor_in_non_selected_windows;
526 Lisp_Object Qcursor_in_non_selected_windows;
527
528 /* A scratch glyph row with contents used for generating truncation
529 glyphs. Also used in direct_output_for_insert. */
530
531 #define MAX_SCRATCH_GLYPHS 100
532 struct glyph_row scratch_glyph_row;
533 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
534
535 /* Ascent and height of the last line processed by move_it_to. */
536
537 static int last_max_ascent, last_height;
538
539 /* Non-zero if there's a help-echo in the echo area. */
540
541 int help_echo_showing_p;
542
543 /* If >= 0, computed, exact values of mode-line and header-line height
544 to use in the macros CURRENT_MODE_LINE_HEIGHT and
545 CURRENT_HEADER_LINE_HEIGHT. */
546
547 int current_mode_line_height, current_header_line_height;
548
549 /* The maximum distance to look ahead for text properties. Values
550 that are too small let us call compute_char_face and similar
551 functions too often which is expensive. Values that are too large
552 let us call compute_char_face and alike too often because we
553 might not be interested in text properties that far away. */
554
555 #define TEXT_PROP_DISTANCE_LIMIT 100
556
557 #if GLYPH_DEBUG
558
559 /* Variables to turn off display optimizations from Lisp. */
560
561 int inhibit_try_window_id, inhibit_try_window_reusing;
562 int inhibit_try_cursor_movement;
563
564 /* Non-zero means print traces of redisplay if compiled with
565 GLYPH_DEBUG != 0. */
566
567 int trace_redisplay_p;
568
569 #endif /* GLYPH_DEBUG */
570
571 #ifdef DEBUG_TRACE_MOVE
572 /* Non-zero means trace with TRACE_MOVE to stderr. */
573 int trace_move;
574
575 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
576 #else
577 #define TRACE_MOVE(x) (void) 0
578 #endif
579
580 /* Non-zero means automatically scroll windows horizontally to make
581 point visible. */
582
583 int automatic_hscrolling_p;
584
585 /* How close to the margin can point get before the window is scrolled
586 horizontally. */
587 int hscroll_margin;
588
589 /* How much to scroll horizontally when point is inside the above margin. */
590 Lisp_Object Vhscroll_step;
591
592 /* A list of symbols, one for each supported image type. */
593
594 Lisp_Object Vimage_types;
595
596 /* The variable `resize-mini-windows'. If nil, don't resize
597 mini-windows. If t, always resize them to fit the text they
598 display. If `grow-only', let mini-windows grow only until they
599 become empty. */
600
601 Lisp_Object Vresize_mini_windows;
602
603 /* Buffer being redisplayed -- for redisplay_window_error. */
604
605 struct buffer *displayed_buffer;
606
607 /* Value returned from text property handlers (see below). */
608
609 enum prop_handled
610 {
611 HANDLED_NORMALLY,
612 HANDLED_RECOMPUTE_PROPS,
613 HANDLED_OVERLAY_STRING_CONSUMED,
614 HANDLED_RETURN
615 };
616
617 /* A description of text properties that redisplay is interested
618 in. */
619
620 struct props
621 {
622 /* The name of the property. */
623 Lisp_Object *name;
624
625 /* A unique index for the property. */
626 enum prop_idx idx;
627
628 /* A handler function called to set up iterator IT from the property
629 at IT's current position. Value is used to steer handle_stop. */
630 enum prop_handled (*handler) P_ ((struct it *it));
631 };
632
633 static enum prop_handled handle_face_prop P_ ((struct it *));
634 static enum prop_handled handle_invisible_prop P_ ((struct it *));
635 static enum prop_handled handle_display_prop P_ ((struct it *));
636 static enum prop_handled handle_composition_prop P_ ((struct it *));
637 static enum prop_handled handle_overlay_change P_ ((struct it *));
638 static enum prop_handled handle_fontified_prop P_ ((struct it *));
639
640 /* Properties handled by iterators. */
641
642 static struct props it_props[] =
643 {
644 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
645 /* Handle `face' before `display' because some sub-properties of
646 `display' need to know the face. */
647 {&Qface, FACE_PROP_IDX, handle_face_prop},
648 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
649 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
650 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
651 {NULL, 0, NULL}
652 };
653
654 /* Value is the position described by X. If X is a marker, value is
655 the marker_position of X. Otherwise, value is X. */
656
657 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
658
659 /* Enumeration returned by some move_it_.* functions internally. */
660
661 enum move_it_result
662 {
663 /* Not used. Undefined value. */
664 MOVE_UNDEFINED,
665
666 /* Move ended at the requested buffer position or ZV. */
667 MOVE_POS_MATCH_OR_ZV,
668
669 /* Move ended at the requested X pixel position. */
670 MOVE_X_REACHED,
671
672 /* Move within a line ended at the end of a line that must be
673 continued. */
674 MOVE_LINE_CONTINUED,
675
676 /* Move within a line ended at the end of a line that would
677 be displayed truncated. */
678 MOVE_LINE_TRUNCATED,
679
680 /* Move within a line ended at a line end. */
681 MOVE_NEWLINE_OR_CR
682 };
683
684
685 \f
686 /* Function prototypes. */
687
688 static void setup_for_ellipsis P_ ((struct it *));
689 static void mark_window_display_accurate_1 P_ ((struct window *, int));
690 static int single_display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
691 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
692 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
693 static int redisplay_mode_lines P_ ((Lisp_Object, int));
694 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
695
696 #if 0
697 static int invisible_text_between_p P_ ((struct it *, int, int));
698 #endif
699
700 static int next_element_from_ellipsis P_ ((struct it *));
701 static void pint2str P_ ((char *, int, int));
702 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
703 struct text_pos));
704 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
705 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
706 static void store_frame_title_char P_ ((char));
707 static int store_frame_title P_ ((unsigned char *, int, int));
708 static void x_consider_frame_title P_ ((Lisp_Object));
709 static void handle_stop P_ ((struct it *));
710 static int tool_bar_lines_needed P_ ((struct frame *));
711 static int single_display_prop_intangible_p P_ ((Lisp_Object));
712 static void ensure_echo_area_buffers P_ ((void));
713 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
714 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
715 static int with_echo_area_buffer P_ ((struct window *, int,
716 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
717 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
718 static void clear_garbaged_frames P_ ((void));
719 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
720 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
721 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
722 static int display_echo_area P_ ((struct window *));
723 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
724 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
725 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
726 static int string_char_and_length P_ ((unsigned char *, int, int *));
727 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
728 struct text_pos));
729 static int compute_window_start_on_continuation_line P_ ((struct window *));
730 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
731 static void insert_left_trunc_glyphs P_ ((struct it *));
732 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
733 static void extend_face_to_end_of_line P_ ((struct it *));
734 static int append_space P_ ((struct it *, int));
735 static int make_cursor_line_fully_visible P_ ((struct window *));
736 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int));
737 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
738 static int trailing_whitespace_p P_ ((int));
739 static int message_log_check_duplicate P_ ((int, int, int, int));
740 static void push_it P_ ((struct it *));
741 static void pop_it P_ ((struct it *));
742 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
743 static void redisplay_internal P_ ((int));
744 static int echo_area_display P_ ((int));
745 static void redisplay_windows P_ ((Lisp_Object));
746 static void redisplay_window P_ ((Lisp_Object, int));
747 static Lisp_Object redisplay_window_error ();
748 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
749 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
750 static void update_menu_bar P_ ((struct frame *, int));
751 static int try_window_reusing_current_matrix P_ ((struct window *));
752 static int try_window_id P_ ((struct window *));
753 static int display_line P_ ((struct it *));
754 static int display_mode_lines P_ ((struct window *));
755 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
756 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object));
757 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
758 static void display_menu_bar P_ ((struct window *));
759 static int display_count_lines P_ ((int, int, int, int, int *));
760 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
761 int, int, struct it *, int, int, int, int));
762 static void compute_line_metrics P_ ((struct it *));
763 static void run_redisplay_end_trigger_hook P_ ((struct it *));
764 static int get_overlay_strings P_ ((struct it *, int));
765 static void next_overlay_string P_ ((struct it *));
766 static void reseat P_ ((struct it *, struct text_pos, int));
767 static void reseat_1 P_ ((struct it *, struct text_pos, int));
768 static void back_to_previous_visible_line_start P_ ((struct it *));
769 static void reseat_at_previous_visible_line_start P_ ((struct it *));
770 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
771 static int next_element_from_display_vector P_ ((struct it *));
772 static int next_element_from_string P_ ((struct it *));
773 static int next_element_from_c_string P_ ((struct it *));
774 static int next_element_from_buffer P_ ((struct it *));
775 static int next_element_from_composition P_ ((struct it *));
776 static int next_element_from_image P_ ((struct it *));
777 static int next_element_from_stretch P_ ((struct it *));
778 static void load_overlay_strings P_ ((struct it *, int));
779 static int init_from_display_pos P_ ((struct it *, struct window *,
780 struct display_pos *));
781 static void reseat_to_string P_ ((struct it *, unsigned char *,
782 Lisp_Object, int, int, int, int));
783 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
784 int, int, int));
785 void move_it_vertically_backward P_ ((struct it *, int));
786 static void init_to_row_start P_ ((struct it *, struct window *,
787 struct glyph_row *));
788 static int init_to_row_end P_ ((struct it *, struct window *,
789 struct glyph_row *));
790 static void back_to_previous_line_start P_ ((struct it *));
791 static int forward_to_next_line_start P_ ((struct it *, int *));
792 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
793 Lisp_Object, int));
794 static struct text_pos string_pos P_ ((int, Lisp_Object));
795 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
796 static int number_of_chars P_ ((unsigned char *, int));
797 static void compute_stop_pos P_ ((struct it *));
798 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
799 Lisp_Object));
800 static int face_before_or_after_it_pos P_ ((struct it *, int));
801 static int next_overlay_change P_ ((int));
802 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
803 Lisp_Object, struct text_pos *,
804 int));
805 static int underlying_face_id P_ ((struct it *));
806 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
807 struct window *));
808
809 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
810 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
811
812 #ifdef HAVE_WINDOW_SYSTEM
813
814 static void update_tool_bar P_ ((struct frame *, int));
815 static void build_desired_tool_bar_string P_ ((struct frame *f));
816 static int redisplay_tool_bar P_ ((struct frame *));
817 static void display_tool_bar_line P_ ((struct it *));
818
819 #endif /* HAVE_WINDOW_SYSTEM */
820
821 \f
822 /***********************************************************************
823 Window display dimensions
824 ***********************************************************************/
825
826 /* Return the window-relative maximum y + 1 for glyph rows displaying
827 text in window W. This is the height of W minus the height of a
828 mode line, if any. */
829
830 INLINE int
831 window_text_bottom_y (w)
832 struct window *w;
833 {
834 struct frame *f = XFRAME (w->frame);
835 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
836
837 if (WINDOW_WANTS_MODELINE_P (w))
838 height -= CURRENT_MODE_LINE_HEIGHT (w);
839 return height;
840 }
841
842
843 /* Return the pixel width of display area AREA of window W. AREA < 0
844 means return the total width of W, not including fringes to
845 the left and right of the window. */
846
847 INLINE int
848 window_box_width (w, area)
849 struct window *w;
850 int area;
851 {
852 struct frame *f = XFRAME (w->frame);
853 int width = XFASTINT (w->width);
854
855 if (!w->pseudo_window_p)
856 {
857 width -= FRAME_SCROLL_BAR_WIDTH (f) + FRAME_FRINGE_COLS (f);
858
859 if (area == TEXT_AREA)
860 {
861 if (INTEGERP (w->left_margin_width))
862 width -= XFASTINT (w->left_margin_width);
863 if (INTEGERP (w->right_margin_width))
864 width -= XFASTINT (w->right_margin_width);
865 }
866 else if (area == LEFT_MARGIN_AREA)
867 width = (INTEGERP (w->left_margin_width)
868 ? XFASTINT (w->left_margin_width) : 0);
869 else if (area == RIGHT_MARGIN_AREA)
870 width = (INTEGERP (w->right_margin_width)
871 ? XFASTINT (w->right_margin_width) : 0);
872 }
873
874 return width * CANON_X_UNIT (f);
875 }
876
877
878 /* Return the pixel height of the display area of window W, not
879 including mode lines of W, if any. */
880
881 INLINE int
882 window_box_height (w)
883 struct window *w;
884 {
885 struct frame *f = XFRAME (w->frame);
886 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
887
888 xassert (height >= 0);
889
890 /* Note: the code below that determines the mode-line/header-line
891 height is essentially the same as that contained in the macro
892 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
893 the appropriate glyph row has its `mode_line_p' flag set,
894 and if it doesn't, uses estimate_mode_line_height instead. */
895
896 if (WINDOW_WANTS_MODELINE_P (w))
897 {
898 struct glyph_row *ml_row
899 = (w->current_matrix && w->current_matrix->rows
900 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
901 : 0);
902 if (ml_row && ml_row->mode_line_p)
903 height -= ml_row->height;
904 else
905 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
906 }
907
908 if (WINDOW_WANTS_HEADER_LINE_P (w))
909 {
910 struct glyph_row *hl_row
911 = (w->current_matrix && w->current_matrix->rows
912 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
913 : 0);
914 if (hl_row && hl_row->mode_line_p)
915 height -= hl_row->height;
916 else
917 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
918 }
919
920 /* With a very small font and a mode-line that's taller than
921 default, we might end up with a negative height. */
922 return max (0, height);
923 }
924
925
926 /* Return the frame-relative coordinate of the left edge of display
927 area AREA of window W. AREA < 0 means return the left edge of the
928 whole window, to the right of the left fringe of W. */
929
930 INLINE int
931 window_box_left (w, area)
932 struct window *w;
933 int area;
934 {
935 struct frame *f = XFRAME (w->frame);
936 int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
937
938 if (!w->pseudo_window_p)
939 {
940 x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
941 + FRAME_LEFT_FRINGE_WIDTH (f));
942
943 if (area == TEXT_AREA)
944 x += window_box_width (w, LEFT_MARGIN_AREA);
945 else if (area == RIGHT_MARGIN_AREA)
946 x += (window_box_width (w, LEFT_MARGIN_AREA)
947 + window_box_width (w, TEXT_AREA));
948 }
949
950 return x;
951 }
952
953
954 /* Return the frame-relative coordinate of the right edge of display
955 area AREA of window W. AREA < 0 means return the left edge of the
956 whole window, to the left of the right fringe of W. */
957
958 INLINE int
959 window_box_right (w, area)
960 struct window *w;
961 int area;
962 {
963 return window_box_left (w, area) + window_box_width (w, area);
964 }
965
966
967 /* Get the bounding box of the display area AREA of window W, without
968 mode lines, in frame-relative coordinates. AREA < 0 means the
969 whole window, not including the left and right fringes of
970 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
971 coordinates of the upper-left corner of the box. Return in
972 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
973
974 INLINE void
975 window_box (w, area, box_x, box_y, box_width, box_height)
976 struct window *w;
977 int area;
978 int *box_x, *box_y, *box_width, *box_height;
979 {
980 struct frame *f = XFRAME (w->frame);
981
982 *box_width = window_box_width (w, area);
983 *box_height = window_box_height (w);
984 *box_x = window_box_left (w, area);
985 *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
986 + XFASTINT (w->top) * CANON_Y_UNIT (f));
987 if (WINDOW_WANTS_HEADER_LINE_P (w))
988 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
989 }
990
991
992 /* Get the bounding box of the display area AREA of window W, without
993 mode lines. AREA < 0 means the whole window, not including the
994 left and right fringe of the window. Return in *TOP_LEFT_X
995 and TOP_LEFT_Y the frame-relative pixel coordinates of the
996 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
997 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
998 box. */
999
1000 INLINE void
1001 window_box_edges (w, area, top_left_x, top_left_y,
1002 bottom_right_x, bottom_right_y)
1003 struct window *w;
1004 int area;
1005 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1006 {
1007 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1008 bottom_right_y);
1009 *bottom_right_x += *top_left_x;
1010 *bottom_right_y += *top_left_y;
1011 }
1012
1013
1014 \f
1015 /***********************************************************************
1016 Utilities
1017 ***********************************************************************/
1018
1019 /* Return the bottom y-position of the line the iterator IT is in.
1020 This can modify IT's settings. */
1021
1022 int
1023 line_bottom_y (it)
1024 struct it *it;
1025 {
1026 int line_height = it->max_ascent + it->max_descent;
1027 int line_top_y = it->current_y;
1028
1029 if (line_height == 0)
1030 {
1031 if (last_height)
1032 line_height = last_height;
1033 else if (IT_CHARPOS (*it) < ZV)
1034 {
1035 move_it_by_lines (it, 1, 1);
1036 line_height = (it->max_ascent || it->max_descent
1037 ? it->max_ascent + it->max_descent
1038 : last_height);
1039 }
1040 else
1041 {
1042 struct glyph_row *row = it->glyph_row;
1043
1044 /* Use the default character height. */
1045 it->glyph_row = NULL;
1046 it->what = IT_CHARACTER;
1047 it->c = ' ';
1048 it->len = 1;
1049 PRODUCE_GLYPHS (it);
1050 line_height = it->ascent + it->descent;
1051 it->glyph_row = row;
1052 }
1053 }
1054
1055 return line_top_y + line_height;
1056 }
1057
1058
1059 /* Return 1 if position CHARPOS is visible in window W. Set *FULLY to
1060 1 if POS is visible and the line containing POS is fully visible.
1061 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1062 and header-lines heights. */
1063
1064 int
1065 pos_visible_p (w, charpos, fully, exact_mode_line_heights_p)
1066 struct window *w;
1067 int charpos, *fully, exact_mode_line_heights_p;
1068 {
1069 struct it it;
1070 struct text_pos top;
1071 int visible_p;
1072 struct buffer *old_buffer = NULL;
1073
1074 if (XBUFFER (w->buffer) != current_buffer)
1075 {
1076 old_buffer = current_buffer;
1077 set_buffer_internal_1 (XBUFFER (w->buffer));
1078 }
1079
1080 *fully = visible_p = 0;
1081 SET_TEXT_POS_FROM_MARKER (top, w->start);
1082
1083 /* Compute exact mode line heights, if requested. */
1084 if (exact_mode_line_heights_p)
1085 {
1086 if (WINDOW_WANTS_MODELINE_P (w))
1087 current_mode_line_height
1088 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1089 current_buffer->mode_line_format);
1090
1091 if (WINDOW_WANTS_HEADER_LINE_P (w))
1092 current_header_line_height
1093 = display_mode_line (w, HEADER_LINE_FACE_ID,
1094 current_buffer->header_line_format);
1095 }
1096
1097 start_display (&it, w, top);
1098 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
1099 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
1100
1101 /* Note that we may overshoot because of invisible text. */
1102 if (IT_CHARPOS (it) >= charpos)
1103 {
1104 int top_y = it.current_y;
1105 int bottom_y = line_bottom_y (&it);
1106 int window_top_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
1107
1108 if (top_y < window_top_y)
1109 visible_p = bottom_y > window_top_y;
1110 else if (top_y < it.last_visible_y)
1111 {
1112 visible_p = 1;
1113 *fully = bottom_y <= it.last_visible_y;
1114 }
1115 }
1116 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1117 {
1118 move_it_by_lines (&it, 1, 0);
1119 if (charpos < IT_CHARPOS (it))
1120 {
1121 visible_p = 1;
1122 *fully = 0;
1123 }
1124 }
1125
1126 if (old_buffer)
1127 set_buffer_internal_1 (old_buffer);
1128
1129 current_header_line_height = current_mode_line_height = -1;
1130 return visible_p;
1131 }
1132
1133
1134 /* Return the next character from STR which is MAXLEN bytes long.
1135 Return in *LEN the length of the character. This is like
1136 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1137 we find one, we return a `?', but with the length of the invalid
1138 character. */
1139
1140 static INLINE int
1141 string_char_and_length (str, maxlen, len)
1142 unsigned char *str;
1143 int maxlen, *len;
1144 {
1145 int c;
1146
1147 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1148 if (!CHAR_VALID_P (c, 1))
1149 /* We may not change the length here because other places in Emacs
1150 don't use this function, i.e. they silently accept invalid
1151 characters. */
1152 c = '?';
1153
1154 return c;
1155 }
1156
1157
1158
1159 /* Given a position POS containing a valid character and byte position
1160 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1161
1162 static struct text_pos
1163 string_pos_nchars_ahead (pos, string, nchars)
1164 struct text_pos pos;
1165 Lisp_Object string;
1166 int nchars;
1167 {
1168 xassert (STRINGP (string) && nchars >= 0);
1169
1170 if (STRING_MULTIBYTE (string))
1171 {
1172 int rest = STRING_BYTES (XSTRING (string)) - BYTEPOS (pos);
1173 unsigned char *p = XSTRING (string)->data + BYTEPOS (pos);
1174 int len;
1175
1176 while (nchars--)
1177 {
1178 string_char_and_length (p, rest, &len);
1179 p += len, rest -= len;
1180 xassert (rest >= 0);
1181 CHARPOS (pos) += 1;
1182 BYTEPOS (pos) += len;
1183 }
1184 }
1185 else
1186 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1187
1188 return pos;
1189 }
1190
1191
1192 /* Value is the text position, i.e. character and byte position,
1193 for character position CHARPOS in STRING. */
1194
1195 static INLINE struct text_pos
1196 string_pos (charpos, string)
1197 int charpos;
1198 Lisp_Object string;
1199 {
1200 struct text_pos pos;
1201 xassert (STRINGP (string));
1202 xassert (charpos >= 0);
1203 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1204 return pos;
1205 }
1206
1207
1208 /* Value is a text position, i.e. character and byte position, for
1209 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1210 means recognize multibyte characters. */
1211
1212 static struct text_pos
1213 c_string_pos (charpos, s, multibyte_p)
1214 int charpos;
1215 unsigned char *s;
1216 int multibyte_p;
1217 {
1218 struct text_pos pos;
1219
1220 xassert (s != NULL);
1221 xassert (charpos >= 0);
1222
1223 if (multibyte_p)
1224 {
1225 int rest = strlen (s), len;
1226
1227 SET_TEXT_POS (pos, 0, 0);
1228 while (charpos--)
1229 {
1230 string_char_and_length (s, rest, &len);
1231 s += len, rest -= len;
1232 xassert (rest >= 0);
1233 CHARPOS (pos) += 1;
1234 BYTEPOS (pos) += len;
1235 }
1236 }
1237 else
1238 SET_TEXT_POS (pos, charpos, charpos);
1239
1240 return pos;
1241 }
1242
1243
1244 /* Value is the number of characters in C string S. MULTIBYTE_P
1245 non-zero means recognize multibyte characters. */
1246
1247 static int
1248 number_of_chars (s, multibyte_p)
1249 unsigned char *s;
1250 int multibyte_p;
1251 {
1252 int nchars;
1253
1254 if (multibyte_p)
1255 {
1256 int rest = strlen (s), len;
1257 unsigned char *p = (unsigned char *) s;
1258
1259 for (nchars = 0; rest > 0; ++nchars)
1260 {
1261 string_char_and_length (p, rest, &len);
1262 rest -= len, p += len;
1263 }
1264 }
1265 else
1266 nchars = strlen (s);
1267
1268 return nchars;
1269 }
1270
1271
1272 /* Compute byte position NEWPOS->bytepos corresponding to
1273 NEWPOS->charpos. POS is a known position in string STRING.
1274 NEWPOS->charpos must be >= POS.charpos. */
1275
1276 static void
1277 compute_string_pos (newpos, pos, string)
1278 struct text_pos *newpos, pos;
1279 Lisp_Object string;
1280 {
1281 xassert (STRINGP (string));
1282 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1283
1284 if (STRING_MULTIBYTE (string))
1285 *newpos = string_pos_nchars_ahead (pos, string,
1286 CHARPOS (*newpos) - CHARPOS (pos));
1287 else
1288 BYTEPOS (*newpos) = CHARPOS (*newpos);
1289 }
1290
1291
1292 \f
1293 /***********************************************************************
1294 Lisp form evaluation
1295 ***********************************************************************/
1296
1297 /* Error handler for safe_eval and safe_call. */
1298
1299 static Lisp_Object
1300 safe_eval_handler (arg)
1301 Lisp_Object arg;
1302 {
1303 add_to_log ("Error during redisplay: %s", arg, Qnil);
1304 return Qnil;
1305 }
1306
1307
1308 /* Evaluate SEXPR and return the result, or nil if something went
1309 wrong. Prevent redisplay during the evaluation. */
1310
1311 Lisp_Object
1312 safe_eval (sexpr)
1313 Lisp_Object sexpr;
1314 {
1315 Lisp_Object val;
1316
1317 if (inhibit_eval_during_redisplay)
1318 val = Qnil;
1319 else
1320 {
1321 int count = BINDING_STACK_SIZE ();
1322 struct gcpro gcpro1;
1323
1324 GCPRO1 (sexpr);
1325 specbind (Qinhibit_redisplay, Qt);
1326 val = internal_condition_case_1 (Feval, sexpr, Qerror,
1327 safe_eval_handler);
1328 UNGCPRO;
1329 val = unbind_to (count, val);
1330 }
1331
1332 return val;
1333 }
1334
1335
1336 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1337 Return the result, or nil if something went wrong. Prevent
1338 redisplay during the evaluation. */
1339
1340 Lisp_Object
1341 safe_call (nargs, args)
1342 int nargs;
1343 Lisp_Object *args;
1344 {
1345 Lisp_Object val;
1346
1347 if (inhibit_eval_during_redisplay)
1348 val = Qnil;
1349 else
1350 {
1351 int count = BINDING_STACK_SIZE ();
1352 struct gcpro gcpro1;
1353
1354 GCPRO1 (args[0]);
1355 gcpro1.nvars = nargs;
1356 specbind (Qinhibit_redisplay, Qt);
1357 val = internal_condition_case_2 (Ffuncall, nargs, args, Qerror,
1358 safe_eval_handler);
1359 UNGCPRO;
1360 val = unbind_to (count, val);
1361 }
1362
1363 return val;
1364 }
1365
1366
1367 /* Call function FN with one argument ARG.
1368 Return the result, or nil if something went wrong. */
1369
1370 Lisp_Object
1371 safe_call1 (fn, arg)
1372 Lisp_Object fn, arg;
1373 {
1374 Lisp_Object args[2];
1375 args[0] = fn;
1376 args[1] = arg;
1377 return safe_call (2, args);
1378 }
1379
1380
1381 \f
1382 /***********************************************************************
1383 Debugging
1384 ***********************************************************************/
1385
1386 #if 0
1387
1388 /* Define CHECK_IT to perform sanity checks on iterators.
1389 This is for debugging. It is too slow to do unconditionally. */
1390
1391 static void
1392 check_it (it)
1393 struct it *it;
1394 {
1395 if (it->method == next_element_from_string)
1396 {
1397 xassert (STRINGP (it->string));
1398 xassert (IT_STRING_CHARPOS (*it) >= 0);
1399 }
1400 else if (it->method == next_element_from_buffer)
1401 {
1402 /* Check that character and byte positions agree. */
1403 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1404 }
1405
1406 if (it->dpvec)
1407 xassert (it->current.dpvec_index >= 0);
1408 else
1409 xassert (it->current.dpvec_index < 0);
1410 }
1411
1412 #define CHECK_IT(IT) check_it ((IT))
1413
1414 #else /* not 0 */
1415
1416 #define CHECK_IT(IT) (void) 0
1417
1418 #endif /* not 0 */
1419
1420
1421 #if GLYPH_DEBUG
1422
1423 /* Check that the window end of window W is what we expect it
1424 to be---the last row in the current matrix displaying text. */
1425
1426 static void
1427 check_window_end (w)
1428 struct window *w;
1429 {
1430 if (!MINI_WINDOW_P (w)
1431 && !NILP (w->window_end_valid))
1432 {
1433 struct glyph_row *row;
1434 xassert ((row = MATRIX_ROW (w->current_matrix,
1435 XFASTINT (w->window_end_vpos)),
1436 !row->enabled_p
1437 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1438 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1439 }
1440 }
1441
1442 #define CHECK_WINDOW_END(W) check_window_end ((W))
1443
1444 #else /* not GLYPH_DEBUG */
1445
1446 #define CHECK_WINDOW_END(W) (void) 0
1447
1448 #endif /* not GLYPH_DEBUG */
1449
1450
1451 \f
1452 /***********************************************************************
1453 Iterator initialization
1454 ***********************************************************************/
1455
1456 /* Initialize IT for displaying current_buffer in window W, starting
1457 at character position CHARPOS. CHARPOS < 0 means that no buffer
1458 position is specified which is useful when the iterator is assigned
1459 a position later. BYTEPOS is the byte position corresponding to
1460 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
1461
1462 If ROW is not null, calls to produce_glyphs with IT as parameter
1463 will produce glyphs in that row.
1464
1465 BASE_FACE_ID is the id of a base face to use. It must be one of
1466 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
1467 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
1468 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
1469
1470 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
1471 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
1472 will be initialized to use the corresponding mode line glyph row of
1473 the desired matrix of W. */
1474
1475 void
1476 init_iterator (it, w, charpos, bytepos, row, base_face_id)
1477 struct it *it;
1478 struct window *w;
1479 int charpos, bytepos;
1480 struct glyph_row *row;
1481 enum face_id base_face_id;
1482 {
1483 int highlight_region_p;
1484
1485 /* Some precondition checks. */
1486 xassert (w != NULL && it != NULL);
1487 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
1488 && charpos <= ZV));
1489
1490 /* If face attributes have been changed since the last redisplay,
1491 free realized faces now because they depend on face definitions
1492 that might have changed. */
1493 if (face_change_count)
1494 {
1495 face_change_count = 0;
1496 free_all_realized_faces (Qnil);
1497 }
1498
1499 /* Use one of the mode line rows of W's desired matrix if
1500 appropriate. */
1501 if (row == NULL)
1502 {
1503 if (base_face_id == MODE_LINE_FACE_ID
1504 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
1505 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
1506 else if (base_face_id == HEADER_LINE_FACE_ID)
1507 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
1508 }
1509
1510 /* Clear IT. */
1511 bzero (it, sizeof *it);
1512 it->current.overlay_string_index = -1;
1513 it->current.dpvec_index = -1;
1514 it->base_face_id = base_face_id;
1515
1516 /* The window in which we iterate over current_buffer: */
1517 XSETWINDOW (it->window, w);
1518 it->w = w;
1519 it->f = XFRAME (w->frame);
1520
1521 /* Extra space between lines (on window systems only). */
1522 if (base_face_id == DEFAULT_FACE_ID
1523 && FRAME_WINDOW_P (it->f))
1524 {
1525 if (NATNUMP (current_buffer->extra_line_spacing))
1526 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
1527 else if (it->f->extra_line_spacing > 0)
1528 it->extra_line_spacing = it->f->extra_line_spacing;
1529 }
1530
1531 /* If realized faces have been removed, e.g. because of face
1532 attribute changes of named faces, recompute them. When running
1533 in batch mode, the face cache of Vterminal_frame is null. If
1534 we happen to get called, make a dummy face cache. */
1535 if (
1536 #ifndef WINDOWSNT
1537 noninteractive &&
1538 #endif
1539 FRAME_FACE_CACHE (it->f) == NULL)
1540 init_frame_faces (it->f);
1541 if (FRAME_FACE_CACHE (it->f)->used == 0)
1542 recompute_basic_faces (it->f);
1543
1544 /* Current value of the `space-width', and 'height' properties. */
1545 it->space_width = Qnil;
1546 it->font_height = Qnil;
1547
1548 /* Are control characters displayed as `^C'? */
1549 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
1550
1551 /* -1 means everything between a CR and the following line end
1552 is invisible. >0 means lines indented more than this value are
1553 invisible. */
1554 it->selective = (INTEGERP (current_buffer->selective_display)
1555 ? XFASTINT (current_buffer->selective_display)
1556 : (!NILP (current_buffer->selective_display)
1557 ? -1 : 0));
1558 it->selective_display_ellipsis_p
1559 = !NILP (current_buffer->selective_display_ellipses);
1560
1561 /* Display table to use. */
1562 it->dp = window_display_table (w);
1563
1564 /* Are multibyte characters enabled in current_buffer? */
1565 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1566
1567 /* Non-zero if we should highlight the region. */
1568 highlight_region_p
1569 = (!NILP (Vtransient_mark_mode)
1570 && !NILP (current_buffer->mark_active)
1571 && XMARKER (current_buffer->mark)->buffer != 0);
1572
1573 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
1574 start and end of a visible region in window IT->w. Set both to
1575 -1 to indicate no region. */
1576 if (highlight_region_p
1577 /* Maybe highlight only in selected window. */
1578 && (/* Either show region everywhere. */
1579 highlight_nonselected_windows
1580 /* Or show region in the selected window. */
1581 || w == XWINDOW (selected_window)
1582 /* Or show the region if we are in the mini-buffer and W is
1583 the window the mini-buffer refers to. */
1584 || (MINI_WINDOW_P (XWINDOW (selected_window))
1585 && WINDOWP (minibuf_selected_window)
1586 && w == XWINDOW (minibuf_selected_window))))
1587 {
1588 int charpos = marker_position (current_buffer->mark);
1589 it->region_beg_charpos = min (PT, charpos);
1590 it->region_end_charpos = max (PT, charpos);
1591 }
1592 else
1593 it->region_beg_charpos = it->region_end_charpos = -1;
1594
1595 /* Get the position at which the redisplay_end_trigger hook should
1596 be run, if it is to be run at all. */
1597 if (MARKERP (w->redisplay_end_trigger)
1598 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
1599 it->redisplay_end_trigger_charpos
1600 = marker_position (w->redisplay_end_trigger);
1601 else if (INTEGERP (w->redisplay_end_trigger))
1602 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
1603
1604 /* Correct bogus values of tab_width. */
1605 it->tab_width = XINT (current_buffer->tab_width);
1606 if (it->tab_width <= 0 || it->tab_width > 1000)
1607 it->tab_width = 8;
1608
1609 /* Are lines in the display truncated? */
1610 it->truncate_lines_p
1611 = (base_face_id != DEFAULT_FACE_ID
1612 || XINT (it->w->hscroll)
1613 || (truncate_partial_width_windows
1614 && !WINDOW_FULL_WIDTH_P (it->w))
1615 || !NILP (current_buffer->truncate_lines));
1616
1617 /* Get dimensions of truncation and continuation glyphs. These are
1618 displayed as fringe bitmaps under X, so we don't need them for such
1619 frames. */
1620 if (!FRAME_WINDOW_P (it->f))
1621 {
1622 if (it->truncate_lines_p)
1623 {
1624 /* We will need the truncation glyph. */
1625 xassert (it->glyph_row == NULL);
1626 produce_special_glyphs (it, IT_TRUNCATION);
1627 it->truncation_pixel_width = it->pixel_width;
1628 }
1629 else
1630 {
1631 /* We will need the continuation glyph. */
1632 xassert (it->glyph_row == NULL);
1633 produce_special_glyphs (it, IT_CONTINUATION);
1634 it->continuation_pixel_width = it->pixel_width;
1635 }
1636
1637 /* Reset these values to zero because the produce_special_glyphs
1638 above has changed them. */
1639 it->pixel_width = it->ascent = it->descent = 0;
1640 it->phys_ascent = it->phys_descent = 0;
1641 }
1642
1643 /* Set this after getting the dimensions of truncation and
1644 continuation glyphs, so that we don't produce glyphs when calling
1645 produce_special_glyphs, above. */
1646 it->glyph_row = row;
1647 it->area = TEXT_AREA;
1648
1649 /* Get the dimensions of the display area. The display area
1650 consists of the visible window area plus a horizontally scrolled
1651 part to the left of the window. All x-values are relative to the
1652 start of this total display area. */
1653 if (base_face_id != DEFAULT_FACE_ID)
1654 {
1655 /* Mode lines, menu bar in terminal frames. */
1656 it->first_visible_x = 0;
1657 it->last_visible_x = XFASTINT (w->width) * CANON_X_UNIT (it->f);
1658 }
1659 else
1660 {
1661 it->first_visible_x
1662 = XFASTINT (it->w->hscroll) * CANON_X_UNIT (it->f);
1663 it->last_visible_x = (it->first_visible_x
1664 + window_box_width (w, TEXT_AREA));
1665
1666 /* If we truncate lines, leave room for the truncator glyph(s) at
1667 the right margin. Otherwise, leave room for the continuation
1668 glyph(s). Truncation and continuation glyphs are not inserted
1669 for window-based redisplay. */
1670 if (!FRAME_WINDOW_P (it->f))
1671 {
1672 if (it->truncate_lines_p)
1673 it->last_visible_x -= it->truncation_pixel_width;
1674 else
1675 it->last_visible_x -= it->continuation_pixel_width;
1676 }
1677
1678 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
1679 it->current_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w) + w->vscroll;
1680 }
1681
1682 /* Leave room for a border glyph. */
1683 if (!FRAME_WINDOW_P (it->f)
1684 && !WINDOW_RIGHTMOST_P (it->w))
1685 it->last_visible_x -= 1;
1686
1687 it->last_visible_y = window_text_bottom_y (w);
1688
1689 /* For mode lines and alike, arrange for the first glyph having a
1690 left box line if the face specifies a box. */
1691 if (base_face_id != DEFAULT_FACE_ID)
1692 {
1693 struct face *face;
1694
1695 it->face_id = base_face_id;
1696
1697 /* If we have a boxed mode line, make the first character appear
1698 with a left box line. */
1699 face = FACE_FROM_ID (it->f, base_face_id);
1700 if (face->box != FACE_NO_BOX)
1701 it->start_of_box_run_p = 1;
1702 }
1703
1704 /* If a buffer position was specified, set the iterator there,
1705 getting overlays and face properties from that position. */
1706 if (charpos >= BUF_BEG (current_buffer))
1707 {
1708 it->end_charpos = ZV;
1709 it->face_id = -1;
1710 IT_CHARPOS (*it) = charpos;
1711
1712 /* Compute byte position if not specified. */
1713 if (bytepos < charpos)
1714 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
1715 else
1716 IT_BYTEPOS (*it) = bytepos;
1717
1718 /* Compute faces etc. */
1719 reseat (it, it->current.pos, 1);
1720 }
1721
1722 CHECK_IT (it);
1723 }
1724
1725
1726 /* Initialize IT for the display of window W with window start POS. */
1727
1728 void
1729 start_display (it, w, pos)
1730 struct it *it;
1731 struct window *w;
1732 struct text_pos pos;
1733 {
1734 struct glyph_row *row;
1735 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
1736
1737 row = w->desired_matrix->rows + first_vpos;
1738 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
1739
1740 if (!it->truncate_lines_p)
1741 {
1742 int start_at_line_beg_p;
1743 int first_y = it->current_y;
1744
1745 /* If window start is not at a line start, skip forward to POS to
1746 get the correct continuation lines width. */
1747 start_at_line_beg_p = (CHARPOS (pos) == BEGV
1748 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
1749 if (!start_at_line_beg_p)
1750 {
1751 reseat_at_previous_visible_line_start (it);
1752 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
1753
1754 /* If lines are continued, this line may end in the middle
1755 of a multi-glyph character (e.g. a control character
1756 displayed as \003, or in the middle of an overlay
1757 string). In this case move_it_to above will not have
1758 taken us to the start of the continuation line but to the
1759 end of the continued line. */
1760 if (it->current_x > 0)
1761 {
1762 if (it->current.dpvec_index >= 0
1763 || it->current.overlay_string_index >= 0)
1764 {
1765 set_iterator_to_next (it, 1);
1766 move_it_in_display_line_to (it, -1, -1, 0);
1767 }
1768
1769 it->continuation_lines_width += it->current_x;
1770 }
1771
1772 /* We're starting a new display line, not affected by the
1773 height of the continued line, so clear the appropriate
1774 fields in the iterator structure. */
1775 it->max_ascent = it->max_descent = 0;
1776 it->max_phys_ascent = it->max_phys_descent = 0;
1777
1778 it->current_y = first_y;
1779 it->vpos = 0;
1780 it->current_x = it->hpos = 0;
1781 }
1782 }
1783
1784 #if 0 /* Don't assert the following because start_display is sometimes
1785 called intentionally with a window start that is not at a
1786 line start. Please leave this code in as a comment. */
1787
1788 /* Window start should be on a line start, now. */
1789 xassert (it->continuation_lines_width
1790 || IT_CHARPOS (it) == BEGV
1791 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
1792 #endif /* 0 */
1793 }
1794
1795
1796 /* Return 1 if POS is a position in ellipses displayed for invisible
1797 text. W is the window we display, for text property lookup. */
1798
1799 static int
1800 in_ellipses_for_invisible_text_p (pos, w)
1801 struct display_pos *pos;
1802 struct window *w;
1803 {
1804 Lisp_Object prop, window;
1805 int ellipses_p = 0;
1806 int charpos = CHARPOS (pos->pos);
1807
1808 /* If POS specifies a position in a display vector, this might
1809 be for an ellipsis displayed for invisible text. We won't
1810 get the iterator set up for delivering that ellipsis unless
1811 we make sure that it gets aware of the invisible text. */
1812 if (pos->dpvec_index >= 0
1813 && pos->overlay_string_index < 0
1814 && CHARPOS (pos->string_pos) < 0
1815 && charpos > BEGV
1816 && (XSETWINDOW (window, w),
1817 prop = Fget_char_property (make_number (charpos),
1818 Qinvisible, window),
1819 !TEXT_PROP_MEANS_INVISIBLE (prop)))
1820 {
1821 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
1822 window);
1823 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
1824 }
1825
1826 return ellipses_p;
1827 }
1828
1829
1830 /* Initialize IT for stepping through current_buffer in window W,
1831 starting at position POS that includes overlay string and display
1832 vector/ control character translation position information. Value
1833 is zero if there are overlay strings with newlines at POS. */
1834
1835 static int
1836 init_from_display_pos (it, w, pos)
1837 struct it *it;
1838 struct window *w;
1839 struct display_pos *pos;
1840 {
1841 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
1842 int i, overlay_strings_with_newlines = 0;
1843
1844 /* If POS specifies a position in a display vector, this might
1845 be for an ellipsis displayed for invisible text. We won't
1846 get the iterator set up for delivering that ellipsis unless
1847 we make sure that it gets aware of the invisible text. */
1848 if (in_ellipses_for_invisible_text_p (pos, w))
1849 {
1850 --charpos;
1851 bytepos = 0;
1852 }
1853
1854 /* Keep in mind: the call to reseat in init_iterator skips invisible
1855 text, so we might end up at a position different from POS. This
1856 is only a problem when POS is a row start after a newline and an
1857 overlay starts there with an after-string, and the overlay has an
1858 invisible property. Since we don't skip invisible text in
1859 display_line and elsewhere immediately after consuming the
1860 newline before the row start, such a POS will not be in a string,
1861 but the call to init_iterator below will move us to the
1862 after-string. */
1863 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
1864
1865 for (i = 0; i < it->n_overlay_strings; ++i)
1866 {
1867 char *s = XSTRING (it->overlay_strings[i])->data;
1868 char *e = s + STRING_BYTES (XSTRING (it->overlay_strings[i]));
1869
1870 while (s < e && *s != '\n')
1871 ++s;
1872
1873 if (s < e)
1874 {
1875 overlay_strings_with_newlines = 1;
1876 break;
1877 }
1878 }
1879
1880 /* If position is within an overlay string, set up IT to the right
1881 overlay string. */
1882 if (pos->overlay_string_index >= 0)
1883 {
1884 int relative_index;
1885
1886 /* If the first overlay string happens to have a `display'
1887 property for an image, the iterator will be set up for that
1888 image, and we have to undo that setup first before we can
1889 correct the overlay string index. */
1890 if (it->method == next_element_from_image)
1891 pop_it (it);
1892
1893 /* We already have the first chunk of overlay strings in
1894 IT->overlay_strings. Load more until the one for
1895 pos->overlay_string_index is in IT->overlay_strings. */
1896 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
1897 {
1898 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
1899 it->current.overlay_string_index = 0;
1900 while (n--)
1901 {
1902 load_overlay_strings (it, 0);
1903 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
1904 }
1905 }
1906
1907 it->current.overlay_string_index = pos->overlay_string_index;
1908 relative_index = (it->current.overlay_string_index
1909 % OVERLAY_STRING_CHUNK_SIZE);
1910 it->string = it->overlay_strings[relative_index];
1911 xassert (STRINGP (it->string));
1912 it->current.string_pos = pos->string_pos;
1913 it->method = next_element_from_string;
1914 }
1915
1916 #if 0 /* This is bogus because POS not having an overlay string
1917 position does not mean it's after the string. Example: A
1918 line starting with a before-string and initialization of IT
1919 to the previous row's end position. */
1920 else if (it->current.overlay_string_index >= 0)
1921 {
1922 /* If POS says we're already after an overlay string ending at
1923 POS, make sure to pop the iterator because it will be in
1924 front of that overlay string. When POS is ZV, we've thereby
1925 also ``processed'' overlay strings at ZV. */
1926 while (it->sp)
1927 pop_it (it);
1928 it->current.overlay_string_index = -1;
1929 it->method = next_element_from_buffer;
1930 if (CHARPOS (pos->pos) == ZV)
1931 it->overlay_strings_at_end_processed_p = 1;
1932 }
1933 #endif /* 0 */
1934
1935 if (CHARPOS (pos->string_pos) >= 0)
1936 {
1937 /* Recorded position is not in an overlay string, but in another
1938 string. This can only be a string from a `display' property.
1939 IT should already be filled with that string. */
1940 it->current.string_pos = pos->string_pos;
1941 xassert (STRINGP (it->string));
1942 }
1943
1944 /* Restore position in display vector translations, control
1945 character translations or ellipses. */
1946 if (pos->dpvec_index >= 0)
1947 {
1948 if (it->dpvec == NULL)
1949 get_next_display_element (it);
1950 xassert (it->dpvec && it->current.dpvec_index == 0);
1951 it->current.dpvec_index = pos->dpvec_index;
1952 }
1953
1954 CHECK_IT (it);
1955 return !overlay_strings_with_newlines;
1956 }
1957
1958
1959 /* Initialize IT for stepping through current_buffer in window W
1960 starting at ROW->start. */
1961
1962 static void
1963 init_to_row_start (it, w, row)
1964 struct it *it;
1965 struct window *w;
1966 struct glyph_row *row;
1967 {
1968 init_from_display_pos (it, w, &row->start);
1969 it->continuation_lines_width = row->continuation_lines_width;
1970 CHECK_IT (it);
1971 }
1972
1973
1974 /* Initialize IT for stepping through current_buffer in window W
1975 starting in the line following ROW, i.e. starting at ROW->end.
1976 Value is zero if there are overlay strings with newlines at ROW's
1977 end position. */
1978
1979 static int
1980 init_to_row_end (it, w, row)
1981 struct it *it;
1982 struct window *w;
1983 struct glyph_row *row;
1984 {
1985 int success = 0;
1986
1987 if (init_from_display_pos (it, w, &row->end))
1988 {
1989 if (row->continued_p)
1990 it->continuation_lines_width
1991 = row->continuation_lines_width + row->pixel_width;
1992 CHECK_IT (it);
1993 success = 1;
1994 }
1995
1996 return success;
1997 }
1998
1999
2000
2001 \f
2002 /***********************************************************************
2003 Text properties
2004 ***********************************************************************/
2005
2006 /* Called when IT reaches IT->stop_charpos. Handle text property and
2007 overlay changes. Set IT->stop_charpos to the next position where
2008 to stop. */
2009
2010 static void
2011 handle_stop (it)
2012 struct it *it;
2013 {
2014 enum prop_handled handled;
2015 int handle_overlay_change_p = 1;
2016 struct props *p;
2017
2018 it->dpvec = NULL;
2019 it->current.dpvec_index = -1;
2020
2021 do
2022 {
2023 handled = HANDLED_NORMALLY;
2024
2025 /* Call text property handlers. */
2026 for (p = it_props; p->handler; ++p)
2027 {
2028 handled = p->handler (it);
2029
2030 if (handled == HANDLED_RECOMPUTE_PROPS)
2031 break;
2032 else if (handled == HANDLED_RETURN)
2033 return;
2034 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2035 handle_overlay_change_p = 0;
2036 }
2037
2038 if (handled != HANDLED_RECOMPUTE_PROPS)
2039 {
2040 /* Don't check for overlay strings below when set to deliver
2041 characters from a display vector. */
2042 if (it->method == next_element_from_display_vector)
2043 handle_overlay_change_p = 0;
2044
2045 /* Handle overlay changes. */
2046 if (handle_overlay_change_p)
2047 handled = handle_overlay_change (it);
2048
2049 /* Determine where to stop next. */
2050 if (handled == HANDLED_NORMALLY)
2051 compute_stop_pos (it);
2052 }
2053 }
2054 while (handled == HANDLED_RECOMPUTE_PROPS);
2055 }
2056
2057
2058 /* Compute IT->stop_charpos from text property and overlay change
2059 information for IT's current position. */
2060
2061 static void
2062 compute_stop_pos (it)
2063 struct it *it;
2064 {
2065 register INTERVAL iv, next_iv;
2066 Lisp_Object object, limit, position;
2067
2068 /* If nowhere else, stop at the end. */
2069 it->stop_charpos = it->end_charpos;
2070
2071 if (STRINGP (it->string))
2072 {
2073 /* Strings are usually short, so don't limit the search for
2074 properties. */
2075 object = it->string;
2076 limit = Qnil;
2077 position = make_number (IT_STRING_CHARPOS (*it));
2078 }
2079 else
2080 {
2081 int charpos;
2082
2083 /* If next overlay change is in front of the current stop pos
2084 (which is IT->end_charpos), stop there. Note: value of
2085 next_overlay_change is point-max if no overlay change
2086 follows. */
2087 charpos = next_overlay_change (IT_CHARPOS (*it));
2088 if (charpos < it->stop_charpos)
2089 it->stop_charpos = charpos;
2090
2091 /* If showing the region, we have to stop at the region
2092 start or end because the face might change there. */
2093 if (it->region_beg_charpos > 0)
2094 {
2095 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2096 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2097 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2098 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2099 }
2100
2101 /* Set up variables for computing the stop position from text
2102 property changes. */
2103 XSETBUFFER (object, current_buffer);
2104 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2105 position = make_number (IT_CHARPOS (*it));
2106
2107 }
2108
2109 /* Get the interval containing IT's position. Value is a null
2110 interval if there isn't such an interval. */
2111 iv = validate_interval_range (object, &position, &position, 0);
2112 if (!NULL_INTERVAL_P (iv))
2113 {
2114 Lisp_Object values_here[LAST_PROP_IDX];
2115 struct props *p;
2116
2117 /* Get properties here. */
2118 for (p = it_props; p->handler; ++p)
2119 values_here[p->idx] = textget (iv->plist, *p->name);
2120
2121 /* Look for an interval following iv that has different
2122 properties. */
2123 for (next_iv = next_interval (iv);
2124 (!NULL_INTERVAL_P (next_iv)
2125 && (NILP (limit)
2126 || XFASTINT (limit) > next_iv->position));
2127 next_iv = next_interval (next_iv))
2128 {
2129 for (p = it_props; p->handler; ++p)
2130 {
2131 Lisp_Object new_value;
2132
2133 new_value = textget (next_iv->plist, *p->name);
2134 if (!EQ (values_here[p->idx], new_value))
2135 break;
2136 }
2137
2138 if (p->handler)
2139 break;
2140 }
2141
2142 if (!NULL_INTERVAL_P (next_iv))
2143 {
2144 if (INTEGERP (limit)
2145 && next_iv->position >= XFASTINT (limit))
2146 /* No text property change up to limit. */
2147 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2148 else
2149 /* Text properties change in next_iv. */
2150 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2151 }
2152 }
2153
2154 xassert (STRINGP (it->string)
2155 || (it->stop_charpos >= BEGV
2156 && it->stop_charpos >= IT_CHARPOS (*it)));
2157 }
2158
2159
2160 /* Return the position of the next overlay change after POS in
2161 current_buffer. Value is point-max if no overlay change
2162 follows. This is like `next-overlay-change' but doesn't use
2163 xmalloc. */
2164
2165 static int
2166 next_overlay_change (pos)
2167 int pos;
2168 {
2169 int noverlays;
2170 int endpos;
2171 Lisp_Object *overlays;
2172 int len;
2173 int i;
2174
2175 /* Get all overlays at the given position. */
2176 len = 10;
2177 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2178 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2179 if (noverlays > len)
2180 {
2181 len = noverlays;
2182 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2183 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2184 }
2185
2186 /* If any of these overlays ends before endpos,
2187 use its ending point instead. */
2188 for (i = 0; i < noverlays; ++i)
2189 {
2190 Lisp_Object oend;
2191 int oendpos;
2192
2193 oend = OVERLAY_END (overlays[i]);
2194 oendpos = OVERLAY_POSITION (oend);
2195 endpos = min (endpos, oendpos);
2196 }
2197
2198 return endpos;
2199 }
2200
2201
2202 \f
2203 /***********************************************************************
2204 Fontification
2205 ***********************************************************************/
2206
2207 /* Handle changes in the `fontified' property of the current buffer by
2208 calling hook functions from Qfontification_functions to fontify
2209 regions of text. */
2210
2211 static enum prop_handled
2212 handle_fontified_prop (it)
2213 struct it *it;
2214 {
2215 Lisp_Object prop, pos;
2216 enum prop_handled handled = HANDLED_NORMALLY;
2217
2218 /* Get the value of the `fontified' property at IT's current buffer
2219 position. (The `fontified' property doesn't have a special
2220 meaning in strings.) If the value is nil, call functions from
2221 Qfontification_functions. */
2222 if (!STRINGP (it->string)
2223 && it->s == NULL
2224 && !NILP (Vfontification_functions)
2225 && !NILP (Vrun_hooks)
2226 && (pos = make_number (IT_CHARPOS (*it)),
2227 prop = Fget_char_property (pos, Qfontified, Qnil),
2228 NILP (prop)))
2229 {
2230 int count = BINDING_STACK_SIZE ();
2231 Lisp_Object val;
2232
2233 val = Vfontification_functions;
2234 specbind (Qfontification_functions, Qnil);
2235
2236 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2237 safe_call1 (val, pos);
2238 else
2239 {
2240 Lisp_Object globals, fn;
2241 struct gcpro gcpro1, gcpro2;
2242
2243 globals = Qnil;
2244 GCPRO2 (val, globals);
2245
2246 for (; CONSP (val); val = XCDR (val))
2247 {
2248 fn = XCAR (val);
2249
2250 if (EQ (fn, Qt))
2251 {
2252 /* A value of t indicates this hook has a local
2253 binding; it means to run the global binding too.
2254 In a global value, t should not occur. If it
2255 does, we must ignore it to avoid an endless
2256 loop. */
2257 for (globals = Fdefault_value (Qfontification_functions);
2258 CONSP (globals);
2259 globals = XCDR (globals))
2260 {
2261 fn = XCAR (globals);
2262 if (!EQ (fn, Qt))
2263 safe_call1 (fn, pos);
2264 }
2265 }
2266 else
2267 safe_call1 (fn, pos);
2268 }
2269
2270 UNGCPRO;
2271 }
2272
2273 unbind_to (count, Qnil);
2274
2275 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2276 something. This avoids an endless loop if they failed to
2277 fontify the text for which reason ever. */
2278 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2279 handled = HANDLED_RECOMPUTE_PROPS;
2280 }
2281
2282 return handled;
2283 }
2284
2285
2286 \f
2287 /***********************************************************************
2288 Faces
2289 ***********************************************************************/
2290
2291 /* Set up iterator IT from face properties at its current position.
2292 Called from handle_stop. */
2293
2294 static enum prop_handled
2295 handle_face_prop (it)
2296 struct it *it;
2297 {
2298 int new_face_id, next_stop;
2299
2300 if (!STRINGP (it->string))
2301 {
2302 new_face_id
2303 = face_at_buffer_position (it->w,
2304 IT_CHARPOS (*it),
2305 it->region_beg_charpos,
2306 it->region_end_charpos,
2307 &next_stop,
2308 (IT_CHARPOS (*it)
2309 + TEXT_PROP_DISTANCE_LIMIT),
2310 0);
2311
2312 /* Is this a start of a run of characters with box face?
2313 Caveat: this can be called for a freshly initialized
2314 iterator; face_id is -1 in this case. We know that the new
2315 face will not change until limit, i.e. if the new face has a
2316 box, all characters up to limit will have one. But, as
2317 usual, we don't know whether limit is really the end. */
2318 if (new_face_id != it->face_id)
2319 {
2320 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2321
2322 /* If new face has a box but old face has not, this is
2323 the start of a run of characters with box, i.e. it has
2324 a shadow on the left side. The value of face_id of the
2325 iterator will be -1 if this is the initial call that gets
2326 the face. In this case, we have to look in front of IT's
2327 position and see whether there is a face != new_face_id. */
2328 it->start_of_box_run_p
2329 = (new_face->box != FACE_NO_BOX
2330 && (it->face_id >= 0
2331 || IT_CHARPOS (*it) == BEG
2332 || new_face_id != face_before_it_pos (it)));
2333 it->face_box_p = new_face->box != FACE_NO_BOX;
2334 }
2335 }
2336 else
2337 {
2338 int base_face_id, bufpos;
2339
2340 if (it->current.overlay_string_index >= 0)
2341 bufpos = IT_CHARPOS (*it);
2342 else
2343 bufpos = 0;
2344
2345 /* For strings from a buffer, i.e. overlay strings or strings
2346 from a `display' property, use the face at IT's current
2347 buffer position as the base face to merge with, so that
2348 overlay strings appear in the same face as surrounding
2349 text, unless they specify their own faces. */
2350 base_face_id = underlying_face_id (it);
2351
2352 new_face_id = face_at_string_position (it->w,
2353 it->string,
2354 IT_STRING_CHARPOS (*it),
2355 bufpos,
2356 it->region_beg_charpos,
2357 it->region_end_charpos,
2358 &next_stop,
2359 base_face_id, 0);
2360
2361 #if 0 /* This shouldn't be neccessary. Let's check it. */
2362 /* If IT is used to display a mode line we would really like to
2363 use the mode line face instead of the frame's default face. */
2364 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2365 && new_face_id == DEFAULT_FACE_ID)
2366 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
2367 #endif
2368
2369 /* Is this a start of a run of characters with box? Caveat:
2370 this can be called for a freshly allocated iterator; face_id
2371 is -1 is this case. We know that the new face will not
2372 change until the next check pos, i.e. if the new face has a
2373 box, all characters up to that position will have a
2374 box. But, as usual, we don't know whether that position
2375 is really the end. */
2376 if (new_face_id != it->face_id)
2377 {
2378 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2379 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2380
2381 /* If new face has a box but old face hasn't, this is the
2382 start of a run of characters with box, i.e. it has a
2383 shadow on the left side. */
2384 it->start_of_box_run_p
2385 = new_face->box && (old_face == NULL || !old_face->box);
2386 it->face_box_p = new_face->box != FACE_NO_BOX;
2387 }
2388 }
2389
2390 it->face_id = new_face_id;
2391 return HANDLED_NORMALLY;
2392 }
2393
2394
2395 /* Return the ID of the face ``underlying'' IT's current position,
2396 which is in a string. If the iterator is associated with a
2397 buffer, return the face at IT's current buffer position.
2398 Otherwise, use the iterator's base_face_id. */
2399
2400 static int
2401 underlying_face_id (it)
2402 struct it *it;
2403 {
2404 int face_id = it->base_face_id, i;
2405
2406 xassert (STRINGP (it->string));
2407
2408 for (i = it->sp - 1; i >= 0; --i)
2409 if (NILP (it->stack[i].string))
2410 face_id = it->stack[i].face_id;
2411
2412 return face_id;
2413 }
2414
2415
2416 /* Compute the face one character before or after the current position
2417 of IT. BEFORE_P non-zero means get the face in front of IT's
2418 position. Value is the id of the face. */
2419
2420 static int
2421 face_before_or_after_it_pos (it, before_p)
2422 struct it *it;
2423 int before_p;
2424 {
2425 int face_id, limit;
2426 int next_check_charpos;
2427 struct text_pos pos;
2428
2429 xassert (it->s == NULL);
2430
2431 if (STRINGP (it->string))
2432 {
2433 int bufpos, base_face_id;
2434
2435 /* No face change past the end of the string (for the case
2436 we are padding with spaces). No face change before the
2437 string start. */
2438 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size
2439 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2440 return it->face_id;
2441
2442 /* Set pos to the position before or after IT's current position. */
2443 if (before_p)
2444 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2445 else
2446 /* For composition, we must check the character after the
2447 composition. */
2448 pos = (it->what == IT_COMPOSITION
2449 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
2450 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
2451
2452 if (it->current.overlay_string_index >= 0)
2453 bufpos = IT_CHARPOS (*it);
2454 else
2455 bufpos = 0;
2456
2457 base_face_id = underlying_face_id (it);
2458
2459 /* Get the face for ASCII, or unibyte. */
2460 face_id = face_at_string_position (it->w,
2461 it->string,
2462 CHARPOS (pos),
2463 bufpos,
2464 it->region_beg_charpos,
2465 it->region_end_charpos,
2466 &next_check_charpos,
2467 base_face_id, 0);
2468
2469 /* Correct the face for charsets different from ASCII. Do it
2470 for the multibyte case only. The face returned above is
2471 suitable for unibyte text if IT->string is unibyte. */
2472 if (STRING_MULTIBYTE (it->string))
2473 {
2474 unsigned char *p = XSTRING (it->string)->data + BYTEPOS (pos);
2475 int rest = STRING_BYTES (XSTRING (it->string)) - BYTEPOS (pos);
2476 int c, len;
2477 struct face *face = FACE_FROM_ID (it->f, face_id);
2478
2479 c = string_char_and_length (p, rest, &len);
2480 face_id = FACE_FOR_CHAR (it->f, face, c);
2481 }
2482 }
2483 else
2484 {
2485 if ((IT_CHARPOS (*it) >= ZV && !before_p)
2486 || (IT_CHARPOS (*it) <= BEGV && before_p))
2487 return it->face_id;
2488
2489 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
2490 pos = it->current.pos;
2491
2492 if (before_p)
2493 DEC_TEXT_POS (pos, it->multibyte_p);
2494 else
2495 {
2496 if (it->what == IT_COMPOSITION)
2497 /* For composition, we must check the position after the
2498 composition. */
2499 pos.charpos += it->cmp_len, pos.bytepos += it->len;
2500 else
2501 INC_TEXT_POS (pos, it->multibyte_p);
2502 }
2503
2504 /* Determine face for CHARSET_ASCII, or unibyte. */
2505 face_id = face_at_buffer_position (it->w,
2506 CHARPOS (pos),
2507 it->region_beg_charpos,
2508 it->region_end_charpos,
2509 &next_check_charpos,
2510 limit, 0);
2511
2512 /* Correct the face for charsets different from ASCII. Do it
2513 for the multibyte case only. The face returned above is
2514 suitable for unibyte text if current_buffer is unibyte. */
2515 if (it->multibyte_p)
2516 {
2517 int c = FETCH_MULTIBYTE_CHAR (CHARPOS (pos));
2518 struct face *face = FACE_FROM_ID (it->f, face_id);
2519 face_id = FACE_FOR_CHAR (it->f, face, c);
2520 }
2521 }
2522
2523 return face_id;
2524 }
2525
2526
2527 \f
2528 /***********************************************************************
2529 Invisible text
2530 ***********************************************************************/
2531
2532 /* Set up iterator IT from invisible properties at its current
2533 position. Called from handle_stop. */
2534
2535 static enum prop_handled
2536 handle_invisible_prop (it)
2537 struct it *it;
2538 {
2539 enum prop_handled handled = HANDLED_NORMALLY;
2540
2541 if (STRINGP (it->string))
2542 {
2543 extern Lisp_Object Qinvisible;
2544 Lisp_Object prop, end_charpos, limit, charpos;
2545
2546 /* Get the value of the invisible text property at the
2547 current position. Value will be nil if there is no such
2548 property. */
2549 charpos = make_number (IT_STRING_CHARPOS (*it));
2550 prop = Fget_text_property (charpos, Qinvisible, it->string);
2551
2552 if (!NILP (prop)
2553 && IT_STRING_CHARPOS (*it) < it->end_charpos)
2554 {
2555 handled = HANDLED_RECOMPUTE_PROPS;
2556
2557 /* Get the position at which the next change of the
2558 invisible text property can be found in IT->string.
2559 Value will be nil if the property value is the same for
2560 all the rest of IT->string. */
2561 XSETINT (limit, XSTRING (it->string)->size);
2562 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2563 it->string, limit);
2564
2565 /* Text at current position is invisible. The next
2566 change in the property is at position end_charpos.
2567 Move IT's current position to that position. */
2568 if (INTEGERP (end_charpos)
2569 && XFASTINT (end_charpos) < XFASTINT (limit))
2570 {
2571 struct text_pos old;
2572 old = it->current.string_pos;
2573 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
2574 compute_string_pos (&it->current.string_pos, old, it->string);
2575 }
2576 else
2577 {
2578 /* The rest of the string is invisible. If this is an
2579 overlay string, proceed with the next overlay string
2580 or whatever comes and return a character from there. */
2581 if (it->current.overlay_string_index >= 0)
2582 {
2583 next_overlay_string (it);
2584 /* Don't check for overlay strings when we just
2585 finished processing them. */
2586 handled = HANDLED_OVERLAY_STRING_CONSUMED;
2587 }
2588 else
2589 {
2590 struct Lisp_String *s = XSTRING (it->string);
2591 IT_STRING_CHARPOS (*it) = s->size;
2592 IT_STRING_BYTEPOS (*it) = STRING_BYTES (s);
2593 }
2594 }
2595 }
2596 }
2597 else
2598 {
2599 int invis_p, newpos, next_stop, start_charpos;
2600 Lisp_Object pos, prop, overlay;
2601
2602 /* First of all, is there invisible text at this position? */
2603 start_charpos = IT_CHARPOS (*it);
2604 pos = make_number (IT_CHARPOS (*it));
2605 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
2606 &overlay);
2607 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
2608
2609 /* If we are on invisible text, skip over it. */
2610 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
2611 {
2612 /* Record whether we have to display an ellipsis for the
2613 invisible text. */
2614 int display_ellipsis_p = invis_p == 2;
2615
2616 handled = HANDLED_RECOMPUTE_PROPS;
2617
2618 /* Loop skipping over invisible text. The loop is left at
2619 ZV or with IT on the first char being visible again. */
2620 do
2621 {
2622 /* Try to skip some invisible text. Return value is the
2623 position reached which can be equal to IT's position
2624 if there is nothing invisible here. This skips both
2625 over invisible text properties and overlays with
2626 invisible property. */
2627 newpos = skip_invisible (IT_CHARPOS (*it),
2628 &next_stop, ZV, it->window);
2629
2630 /* If we skipped nothing at all we weren't at invisible
2631 text in the first place. If everything to the end of
2632 the buffer was skipped, end the loop. */
2633 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
2634 invis_p = 0;
2635 else
2636 {
2637 /* We skipped some characters but not necessarily
2638 all there are. Check if we ended up on visible
2639 text. Fget_char_property returns the property of
2640 the char before the given position, i.e. if we
2641 get invis_p = 0, this means that the char at
2642 newpos is visible. */
2643 pos = make_number (newpos);
2644 prop = Fget_char_property (pos, Qinvisible, it->window);
2645 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
2646 }
2647
2648 /* If we ended up on invisible text, proceed to
2649 skip starting with next_stop. */
2650 if (invis_p)
2651 IT_CHARPOS (*it) = next_stop;
2652 }
2653 while (invis_p);
2654
2655 /* The position newpos is now either ZV or on visible text. */
2656 IT_CHARPOS (*it) = newpos;
2657 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2658
2659 /* If there are before-strings at the start of invisible
2660 text, and the text is invisible because of a text
2661 property, arrange to show before-strings because 20.x did
2662 it that way. (If the text is invisible because of an
2663 overlay property instead of a text property, this is
2664 already handled in the overlay code.) */
2665 if (NILP (overlay)
2666 && get_overlay_strings (it, start_charpos))
2667 {
2668 handled = HANDLED_RECOMPUTE_PROPS;
2669 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
2670 }
2671 else if (display_ellipsis_p)
2672 setup_for_ellipsis (it);
2673 }
2674 }
2675
2676 return handled;
2677 }
2678
2679
2680 /* Make iterator IT return `...' next. */
2681
2682 static void
2683 setup_for_ellipsis (it)
2684 struct it *it;
2685 {
2686 if (it->dp
2687 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
2688 {
2689 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
2690 it->dpvec = v->contents;
2691 it->dpend = v->contents + v->size;
2692 }
2693 else
2694 {
2695 /* Default `...'. */
2696 it->dpvec = default_invis_vector;
2697 it->dpend = default_invis_vector + 3;
2698 }
2699
2700 /* The ellipsis display does not replace the display of the
2701 character at the new position. Indicate this by setting
2702 IT->dpvec_char_len to zero. */
2703 it->dpvec_char_len = 0;
2704
2705 it->current.dpvec_index = 0;
2706 it->method = next_element_from_display_vector;
2707 }
2708
2709
2710 \f
2711 /***********************************************************************
2712 'display' property
2713 ***********************************************************************/
2714
2715 /* Set up iterator IT from `display' property at its current position.
2716 Called from handle_stop. */
2717
2718 static enum prop_handled
2719 handle_display_prop (it)
2720 struct it *it;
2721 {
2722 Lisp_Object prop, object;
2723 struct text_pos *position;
2724 int display_replaced_p = 0;
2725
2726 if (STRINGP (it->string))
2727 {
2728 object = it->string;
2729 position = &it->current.string_pos;
2730 }
2731 else
2732 {
2733 object = it->w->buffer;
2734 position = &it->current.pos;
2735 }
2736
2737 /* Reset those iterator values set from display property values. */
2738 it->font_height = Qnil;
2739 it->space_width = Qnil;
2740 it->voffset = 0;
2741
2742 /* We don't support recursive `display' properties, i.e. string
2743 values that have a string `display' property, that have a string
2744 `display' property etc. */
2745 if (!it->string_from_display_prop_p)
2746 it->area = TEXT_AREA;
2747
2748 prop = Fget_char_property (make_number (position->charpos),
2749 Qdisplay, object);
2750 if (NILP (prop))
2751 return HANDLED_NORMALLY;
2752
2753 if (CONSP (prop)
2754 /* Simple properties. */
2755 && !EQ (XCAR (prop), Qimage)
2756 && !EQ (XCAR (prop), Qspace)
2757 && !EQ (XCAR (prop), Qwhen)
2758 && !EQ (XCAR (prop), Qspace_width)
2759 && !EQ (XCAR (prop), Qheight)
2760 && !EQ (XCAR (prop), Qraise)
2761 /* Marginal area specifications. */
2762 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
2763 && !NILP (XCAR (prop)))
2764 {
2765 for (; CONSP (prop); prop = XCDR (prop))
2766 {
2767 if (handle_single_display_prop (it, XCAR (prop), object,
2768 position, display_replaced_p))
2769 display_replaced_p = 1;
2770 }
2771 }
2772 else if (VECTORP (prop))
2773 {
2774 int i;
2775 for (i = 0; i < ASIZE (prop); ++i)
2776 if (handle_single_display_prop (it, AREF (prop, i), object,
2777 position, display_replaced_p))
2778 display_replaced_p = 1;
2779 }
2780 else
2781 {
2782 if (handle_single_display_prop (it, prop, object, position, 0))
2783 display_replaced_p = 1;
2784 }
2785
2786 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
2787 }
2788
2789
2790 /* Value is the position of the end of the `display' property starting
2791 at START_POS in OBJECT. */
2792
2793 static struct text_pos
2794 display_prop_end (it, object, start_pos)
2795 struct it *it;
2796 Lisp_Object object;
2797 struct text_pos start_pos;
2798 {
2799 Lisp_Object end;
2800 struct text_pos end_pos;
2801
2802 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
2803 Qdisplay, object, Qnil);
2804 CHARPOS (end_pos) = XFASTINT (end);
2805 if (STRINGP (object))
2806 compute_string_pos (&end_pos, start_pos, it->string);
2807 else
2808 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
2809
2810 return end_pos;
2811 }
2812
2813
2814 /* Set up IT from a single `display' sub-property value PROP. OBJECT
2815 is the object in which the `display' property was found. *POSITION
2816 is the position at which it was found. DISPLAY_REPLACED_P non-zero
2817 means that we previously saw a display sub-property which already
2818 replaced text display with something else, for example an image;
2819 ignore such properties after the first one has been processed.
2820
2821 If PROP is a `space' or `image' sub-property, set *POSITION to the
2822 end position of the `display' property.
2823
2824 Value is non-zero if something was found which replaces the display
2825 of buffer or string text. */
2826
2827 static int
2828 handle_single_display_prop (it, prop, object, position,
2829 display_replaced_before_p)
2830 struct it *it;
2831 Lisp_Object prop;
2832 Lisp_Object object;
2833 struct text_pos *position;
2834 int display_replaced_before_p;
2835 {
2836 Lisp_Object value;
2837 int replaces_text_display_p = 0;
2838 Lisp_Object form;
2839
2840 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
2841 evaluated. If the result is nil, VALUE is ignored. */
2842 form = Qt;
2843 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2844 {
2845 prop = XCDR (prop);
2846 if (!CONSP (prop))
2847 return 0;
2848 form = XCAR (prop);
2849 prop = XCDR (prop);
2850 }
2851
2852 if (!NILP (form) && !EQ (form, Qt))
2853 {
2854 int count = BINDING_STACK_SIZE ();
2855 struct gcpro gcpro1;
2856
2857 /* Bind `object' to the object having the `display' property, a
2858 buffer or string. Bind `position' to the position in the
2859 object where the property was found, and `buffer-position'
2860 to the current position in the buffer. */
2861 specbind (Qobject, object);
2862 specbind (Qposition, make_number (CHARPOS (*position)));
2863 specbind (Qbuffer_position,
2864 make_number (STRINGP (object)
2865 ? IT_CHARPOS (*it) : CHARPOS (*position)));
2866 GCPRO1 (form);
2867 form = safe_eval (form);
2868 UNGCPRO;
2869 unbind_to (count, Qnil);
2870 }
2871
2872 if (NILP (form))
2873 return 0;
2874
2875 if (CONSP (prop)
2876 && EQ (XCAR (prop), Qheight)
2877 && CONSP (XCDR (prop)))
2878 {
2879 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2880 return 0;
2881
2882 /* `(height HEIGHT)'. */
2883 it->font_height = XCAR (XCDR (prop));
2884 if (!NILP (it->font_height))
2885 {
2886 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2887 int new_height = -1;
2888
2889 if (CONSP (it->font_height)
2890 && (EQ (XCAR (it->font_height), Qplus)
2891 || EQ (XCAR (it->font_height), Qminus))
2892 && CONSP (XCDR (it->font_height))
2893 && INTEGERP (XCAR (XCDR (it->font_height))))
2894 {
2895 /* `(+ N)' or `(- N)' where N is an integer. */
2896 int steps = XINT (XCAR (XCDR (it->font_height)));
2897 if (EQ (XCAR (it->font_height), Qplus))
2898 steps = - steps;
2899 it->face_id = smaller_face (it->f, it->face_id, steps);
2900 }
2901 else if (FUNCTIONP (it->font_height))
2902 {
2903 /* Call function with current height as argument.
2904 Value is the new height. */
2905 Lisp_Object height;
2906 height = safe_call1 (it->font_height,
2907 face->lface[LFACE_HEIGHT_INDEX]);
2908 if (NUMBERP (height))
2909 new_height = XFLOATINT (height);
2910 }
2911 else if (NUMBERP (it->font_height))
2912 {
2913 /* Value is a multiple of the canonical char height. */
2914 struct face *face;
2915
2916 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
2917 new_height = (XFLOATINT (it->font_height)
2918 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
2919 }
2920 else
2921 {
2922 /* Evaluate IT->font_height with `height' bound to the
2923 current specified height to get the new height. */
2924 Lisp_Object value;
2925 int count = BINDING_STACK_SIZE ();
2926
2927 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
2928 value = safe_eval (it->font_height);
2929 unbind_to (count, Qnil);
2930
2931 if (NUMBERP (value))
2932 new_height = XFLOATINT (value);
2933 }
2934
2935 if (new_height > 0)
2936 it->face_id = face_with_height (it->f, it->face_id, new_height);
2937 }
2938 }
2939 else if (CONSP (prop)
2940 && EQ (XCAR (prop), Qspace_width)
2941 && CONSP (XCDR (prop)))
2942 {
2943 /* `(space_width WIDTH)'. */
2944 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2945 return 0;
2946
2947 value = XCAR (XCDR (prop));
2948 if (NUMBERP (value) && XFLOATINT (value) > 0)
2949 it->space_width = value;
2950 }
2951 else if (CONSP (prop)
2952 && EQ (XCAR (prop), Qraise)
2953 && CONSP (XCDR (prop)))
2954 {
2955 /* `(raise FACTOR)'. */
2956 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2957 return 0;
2958
2959 #ifdef HAVE_WINDOW_SYSTEM
2960 value = XCAR (XCDR (prop));
2961 if (NUMBERP (value))
2962 {
2963 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2964 it->voffset = - (XFLOATINT (value)
2965 * (FONT_HEIGHT (face->font)));
2966 }
2967 #endif /* HAVE_WINDOW_SYSTEM */
2968 }
2969 else if (!it->string_from_display_prop_p)
2970 {
2971 /* `((margin left-margin) VALUE)' or `((margin right-margin)
2972 VALUE) or `((margin nil) VALUE)' or VALUE. */
2973 Lisp_Object location, value;
2974 struct text_pos start_pos;
2975 int valid_p;
2976
2977 /* Characters having this form of property are not displayed, so
2978 we have to find the end of the property. */
2979 start_pos = *position;
2980 *position = display_prop_end (it, object, start_pos);
2981 value = Qnil;
2982
2983 /* Let's stop at the new position and assume that all
2984 text properties change there. */
2985 it->stop_charpos = position->charpos;
2986
2987 location = Qunbound;
2988 if (CONSP (prop) && CONSP (XCAR (prop)))
2989 {
2990 Lisp_Object tem;
2991
2992 value = XCDR (prop);
2993 if (CONSP (value))
2994 value = XCAR (value);
2995
2996 tem = XCAR (prop);
2997 if (EQ (XCAR (tem), Qmargin)
2998 && (tem = XCDR (tem),
2999 tem = CONSP (tem) ? XCAR (tem) : Qnil,
3000 (NILP (tem)
3001 || EQ (tem, Qleft_margin)
3002 || EQ (tem, Qright_margin))))
3003 location = tem;
3004 }
3005
3006 if (EQ (location, Qunbound))
3007 {
3008 location = Qnil;
3009 value = prop;
3010 }
3011
3012 #ifdef HAVE_WINDOW_SYSTEM
3013 if (FRAME_TERMCAP_P (it->f))
3014 valid_p = STRINGP (value);
3015 else
3016 valid_p = (STRINGP (value)
3017 || (CONSP (value) && EQ (XCAR (value), Qspace))
3018 || valid_image_p (value));
3019 #else /* not HAVE_WINDOW_SYSTEM */
3020 valid_p = STRINGP (value);
3021 #endif /* not HAVE_WINDOW_SYSTEM */
3022
3023 if ((EQ (location, Qleft_margin)
3024 || EQ (location, Qright_margin)
3025 || NILP (location))
3026 && valid_p
3027 && !display_replaced_before_p)
3028 {
3029 replaces_text_display_p = 1;
3030
3031 /* Save current settings of IT so that we can restore them
3032 when we are finished with the glyph property value. */
3033 push_it (it);
3034
3035 if (NILP (location))
3036 it->area = TEXT_AREA;
3037 else if (EQ (location, Qleft_margin))
3038 it->area = LEFT_MARGIN_AREA;
3039 else
3040 it->area = RIGHT_MARGIN_AREA;
3041
3042 if (STRINGP (value))
3043 {
3044 it->string = value;
3045 it->multibyte_p = STRING_MULTIBYTE (it->string);
3046 it->current.overlay_string_index = -1;
3047 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3048 it->end_charpos = it->string_nchars
3049 = XSTRING (it->string)->size;
3050 it->method = next_element_from_string;
3051 it->stop_charpos = 0;
3052 it->string_from_display_prop_p = 1;
3053 /* Say that we haven't consumed the characters with
3054 `display' property yet. The call to pop_it in
3055 set_iterator_to_next will clean this up. */
3056 *position = start_pos;
3057 }
3058 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3059 {
3060 it->method = next_element_from_stretch;
3061 it->object = value;
3062 it->current.pos = it->position = start_pos;
3063 }
3064 #ifdef HAVE_WINDOW_SYSTEM
3065 else
3066 {
3067 it->what = IT_IMAGE;
3068 it->image_id = lookup_image (it->f, value);
3069 it->position = start_pos;
3070 it->object = NILP (object) ? it->w->buffer : object;
3071 it->method = next_element_from_image;
3072
3073 /* Say that we haven't consumed the characters with
3074 `display' property yet. The call to pop_it in
3075 set_iterator_to_next will clean this up. */
3076 *position = start_pos;
3077 }
3078 #endif /* HAVE_WINDOW_SYSTEM */
3079 }
3080 else
3081 /* Invalid property or property not supported. Restore
3082 the position to what it was before. */
3083 *position = start_pos;
3084 }
3085
3086 return replaces_text_display_p;
3087 }
3088
3089
3090 /* Check if PROP is a display sub-property value whose text should be
3091 treated as intangible. */
3092
3093 static int
3094 single_display_prop_intangible_p (prop)
3095 Lisp_Object prop;
3096 {
3097 /* Skip over `when FORM'. */
3098 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3099 {
3100 prop = XCDR (prop);
3101 if (!CONSP (prop))
3102 return 0;
3103 prop = XCDR (prop);
3104 }
3105
3106 if (!CONSP (prop))
3107 return 0;
3108
3109 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3110 we don't need to treat text as intangible. */
3111 if (EQ (XCAR (prop), Qmargin))
3112 {
3113 prop = XCDR (prop);
3114 if (!CONSP (prop))
3115 return 0;
3116
3117 prop = XCDR (prop);
3118 if (!CONSP (prop)
3119 || EQ (XCAR (prop), Qleft_margin)
3120 || EQ (XCAR (prop), Qright_margin))
3121 return 0;
3122 }
3123
3124 return CONSP (prop) && EQ (XCAR (prop), Qimage);
3125 }
3126
3127
3128 /* Check if PROP is a display property value whose text should be
3129 treated as intangible. */
3130
3131 int
3132 display_prop_intangible_p (prop)
3133 Lisp_Object prop;
3134 {
3135 if (CONSP (prop)
3136 && CONSP (XCAR (prop))
3137 && !EQ (Qmargin, XCAR (XCAR (prop))))
3138 {
3139 /* A list of sub-properties. */
3140 while (CONSP (prop))
3141 {
3142 if (single_display_prop_intangible_p (XCAR (prop)))
3143 return 1;
3144 prop = XCDR (prop);
3145 }
3146 }
3147 else if (VECTORP (prop))
3148 {
3149 /* A vector of sub-properties. */
3150 int i;
3151 for (i = 0; i < ASIZE (prop); ++i)
3152 if (single_display_prop_intangible_p (AREF (prop, i)))
3153 return 1;
3154 }
3155 else
3156 return single_display_prop_intangible_p (prop);
3157
3158 return 0;
3159 }
3160
3161
3162 /* Return 1 if PROP is a display sub-property value containing STRING. */
3163
3164 static int
3165 single_display_prop_string_p (prop, string)
3166 Lisp_Object prop, string;
3167 {
3168 if (EQ (string, prop))
3169 return 1;
3170
3171 /* Skip over `when FORM'. */
3172 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3173 {
3174 prop = XCDR (prop);
3175 if (!CONSP (prop))
3176 return 0;
3177 prop = XCDR (prop);
3178 }
3179
3180 if (CONSP (prop))
3181 /* Skip over `margin LOCATION'. */
3182 if (EQ (XCAR (prop), Qmargin))
3183 {
3184 prop = XCDR (prop);
3185 if (!CONSP (prop))
3186 return 0;
3187
3188 prop = XCDR (prop);
3189 if (!CONSP (prop))
3190 return 0;
3191 }
3192
3193 return CONSP (prop) && EQ (XCAR (prop), string);
3194 }
3195
3196
3197 /* Return 1 if STRING appears in the `display' property PROP. */
3198
3199 static int
3200 display_prop_string_p (prop, string)
3201 Lisp_Object prop, string;
3202 {
3203 if (CONSP (prop)
3204 && CONSP (XCAR (prop))
3205 && !EQ (Qmargin, XCAR (XCAR (prop))))
3206 {
3207 /* A list of sub-properties. */
3208 while (CONSP (prop))
3209 {
3210 if (single_display_prop_string_p (XCAR (prop), string))
3211 return 1;
3212 prop = XCDR (prop);
3213 }
3214 }
3215 else if (VECTORP (prop))
3216 {
3217 /* A vector of sub-properties. */
3218 int i;
3219 for (i = 0; i < ASIZE (prop); ++i)
3220 if (single_display_prop_string_p (AREF (prop, i), string))
3221 return 1;
3222 }
3223 else
3224 return single_display_prop_string_p (prop, string);
3225
3226 return 0;
3227 }
3228
3229
3230 /* Determine from which buffer position in W's buffer STRING comes
3231 from. AROUND_CHARPOS is an approximate position where it could
3232 be from. Value is the buffer position or 0 if it couldn't be
3233 determined.
3234
3235 W's buffer must be current.
3236
3237 This function is necessary because we don't record buffer positions
3238 in glyphs generated from strings (to keep struct glyph small).
3239 This function may only use code that doesn't eval because it is
3240 called asynchronously from note_mouse_highlight. */
3241
3242 int
3243 string_buffer_position (w, string, around_charpos)
3244 struct window *w;
3245 Lisp_Object string;
3246 int around_charpos;
3247 {
3248 Lisp_Object limit, prop, pos;
3249 const int MAX_DISTANCE = 1000;
3250 int found = 0;
3251
3252 pos = make_number (around_charpos);
3253 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
3254 while (!found && !EQ (pos, limit))
3255 {
3256 prop = Fget_char_property (pos, Qdisplay, Qnil);
3257 if (!NILP (prop) && display_prop_string_p (prop, string))
3258 found = 1;
3259 else
3260 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
3261 }
3262
3263 if (!found)
3264 {
3265 pos = make_number (around_charpos);
3266 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
3267 while (!found && !EQ (pos, limit))
3268 {
3269 prop = Fget_char_property (pos, Qdisplay, Qnil);
3270 if (!NILP (prop) && display_prop_string_p (prop, string))
3271 found = 1;
3272 else
3273 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
3274 limit);
3275 }
3276 }
3277
3278 return found ? XINT (pos) : 0;
3279 }
3280
3281
3282 \f
3283 /***********************************************************************
3284 `composition' property
3285 ***********************************************************************/
3286
3287 /* Set up iterator IT from `composition' property at its current
3288 position. Called from handle_stop. */
3289
3290 static enum prop_handled
3291 handle_composition_prop (it)
3292 struct it *it;
3293 {
3294 Lisp_Object prop, string;
3295 int pos, pos_byte, end;
3296 enum prop_handled handled = HANDLED_NORMALLY;
3297
3298 if (STRINGP (it->string))
3299 {
3300 pos = IT_STRING_CHARPOS (*it);
3301 pos_byte = IT_STRING_BYTEPOS (*it);
3302 string = it->string;
3303 }
3304 else
3305 {
3306 pos = IT_CHARPOS (*it);
3307 pos_byte = IT_BYTEPOS (*it);
3308 string = Qnil;
3309 }
3310
3311 /* If there's a valid composition and point is not inside of the
3312 composition (in the case that the composition is from the current
3313 buffer), draw a glyph composed from the composition components. */
3314 if (find_composition (pos, -1, &pos, &end, &prop, string)
3315 && COMPOSITION_VALID_P (pos, end, prop)
3316 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
3317 {
3318 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
3319
3320 if (id >= 0)
3321 {
3322 it->method = next_element_from_composition;
3323 it->cmp_id = id;
3324 it->cmp_len = COMPOSITION_LENGTH (prop);
3325 /* For a terminal, draw only the first character of the
3326 components. */
3327 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
3328 it->len = (STRINGP (it->string)
3329 ? string_char_to_byte (it->string, end)
3330 : CHAR_TO_BYTE (end)) - pos_byte;
3331 it->stop_charpos = end;
3332 handled = HANDLED_RETURN;
3333 }
3334 }
3335
3336 return handled;
3337 }
3338
3339
3340 \f
3341 /***********************************************************************
3342 Overlay strings
3343 ***********************************************************************/
3344
3345 /* The following structure is used to record overlay strings for
3346 later sorting in load_overlay_strings. */
3347
3348 struct overlay_entry
3349 {
3350 Lisp_Object overlay;
3351 Lisp_Object string;
3352 int priority;
3353 int after_string_p;
3354 };
3355
3356
3357 /* Set up iterator IT from overlay strings at its current position.
3358 Called from handle_stop. */
3359
3360 static enum prop_handled
3361 handle_overlay_change (it)
3362 struct it *it;
3363 {
3364 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
3365 return HANDLED_RECOMPUTE_PROPS;
3366 else
3367 return HANDLED_NORMALLY;
3368 }
3369
3370
3371 /* Set up the next overlay string for delivery by IT, if there is an
3372 overlay string to deliver. Called by set_iterator_to_next when the
3373 end of the current overlay string is reached. If there are more
3374 overlay strings to display, IT->string and
3375 IT->current.overlay_string_index are set appropriately here.
3376 Otherwise IT->string is set to nil. */
3377
3378 static void
3379 next_overlay_string (it)
3380 struct it *it;
3381 {
3382 ++it->current.overlay_string_index;
3383 if (it->current.overlay_string_index == it->n_overlay_strings)
3384 {
3385 /* No more overlay strings. Restore IT's settings to what
3386 they were before overlay strings were processed, and
3387 continue to deliver from current_buffer. */
3388 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
3389
3390 pop_it (it);
3391 xassert (it->stop_charpos >= BEGV
3392 && it->stop_charpos <= it->end_charpos);
3393 it->string = Qnil;
3394 it->current.overlay_string_index = -1;
3395 SET_TEXT_POS (it->current.string_pos, -1, -1);
3396 it->n_overlay_strings = 0;
3397 it->method = next_element_from_buffer;
3398
3399 /* If we're at the end of the buffer, record that we have
3400 processed the overlay strings there already, so that
3401 next_element_from_buffer doesn't try it again. */
3402 if (IT_CHARPOS (*it) >= it->end_charpos)
3403 it->overlay_strings_at_end_processed_p = 1;
3404
3405 /* If we have to display `...' for invisible text, set
3406 the iterator up for that. */
3407 if (display_ellipsis_p)
3408 setup_for_ellipsis (it);
3409 }
3410 else
3411 {
3412 /* There are more overlay strings to process. If
3413 IT->current.overlay_string_index has advanced to a position
3414 where we must load IT->overlay_strings with more strings, do
3415 it. */
3416 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
3417
3418 if (it->current.overlay_string_index && i == 0)
3419 load_overlay_strings (it, 0);
3420
3421 /* Initialize IT to deliver display elements from the overlay
3422 string. */
3423 it->string = it->overlay_strings[i];
3424 it->multibyte_p = STRING_MULTIBYTE (it->string);
3425 SET_TEXT_POS (it->current.string_pos, 0, 0);
3426 it->method = next_element_from_string;
3427 it->stop_charpos = 0;
3428 }
3429
3430 CHECK_IT (it);
3431 }
3432
3433
3434 /* Compare two overlay_entry structures E1 and E2. Used as a
3435 comparison function for qsort in load_overlay_strings. Overlay
3436 strings for the same position are sorted so that
3437
3438 1. All after-strings come in front of before-strings, except
3439 when they come from the same overlay.
3440
3441 2. Within after-strings, strings are sorted so that overlay strings
3442 from overlays with higher priorities come first.
3443
3444 2. Within before-strings, strings are sorted so that overlay
3445 strings from overlays with higher priorities come last.
3446
3447 Value is analogous to strcmp. */
3448
3449
3450 static int
3451 compare_overlay_entries (e1, e2)
3452 void *e1, *e2;
3453 {
3454 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
3455 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
3456 int result;
3457
3458 if (entry1->after_string_p != entry2->after_string_p)
3459 {
3460 /* Let after-strings appear in front of before-strings if
3461 they come from different overlays. */
3462 if (EQ (entry1->overlay, entry2->overlay))
3463 result = entry1->after_string_p ? 1 : -1;
3464 else
3465 result = entry1->after_string_p ? -1 : 1;
3466 }
3467 else if (entry1->after_string_p)
3468 /* After-strings sorted in order of decreasing priority. */
3469 result = entry2->priority - entry1->priority;
3470 else
3471 /* Before-strings sorted in order of increasing priority. */
3472 result = entry1->priority - entry2->priority;
3473
3474 return result;
3475 }
3476
3477
3478 /* Load the vector IT->overlay_strings with overlay strings from IT's
3479 current buffer position, or from CHARPOS if that is > 0. Set
3480 IT->n_overlays to the total number of overlay strings found.
3481
3482 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
3483 a time. On entry into load_overlay_strings,
3484 IT->current.overlay_string_index gives the number of overlay
3485 strings that have already been loaded by previous calls to this
3486 function.
3487
3488 IT->add_overlay_start contains an additional overlay start
3489 position to consider for taking overlay strings from, if non-zero.
3490 This position comes into play when the overlay has an `invisible'
3491 property, and both before and after-strings. When we've skipped to
3492 the end of the overlay, because of its `invisible' property, we
3493 nevertheless want its before-string to appear.
3494 IT->add_overlay_start will contain the overlay start position
3495 in this case.
3496
3497 Overlay strings are sorted so that after-string strings come in
3498 front of before-string strings. Within before and after-strings,
3499 strings are sorted by overlay priority. See also function
3500 compare_overlay_entries. */
3501
3502 static void
3503 load_overlay_strings (it, charpos)
3504 struct it *it;
3505 int charpos;
3506 {
3507 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
3508 Lisp_Object ov, overlay, window, str, invisible;
3509 int start, end;
3510 int size = 20;
3511 int n = 0, i, j, invis_p;
3512 struct overlay_entry *entries
3513 = (struct overlay_entry *) alloca (size * sizeof *entries);
3514
3515 if (charpos <= 0)
3516 charpos = IT_CHARPOS (*it);
3517
3518 /* Append the overlay string STRING of overlay OVERLAY to vector
3519 `entries' which has size `size' and currently contains `n'
3520 elements. AFTER_P non-zero means STRING is an after-string of
3521 OVERLAY. */
3522 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
3523 do \
3524 { \
3525 Lisp_Object priority; \
3526 \
3527 if (n == size) \
3528 { \
3529 int new_size = 2 * size; \
3530 struct overlay_entry *old = entries; \
3531 entries = \
3532 (struct overlay_entry *) alloca (new_size \
3533 * sizeof *entries); \
3534 bcopy (old, entries, size * sizeof *entries); \
3535 size = new_size; \
3536 } \
3537 \
3538 entries[n].string = (STRING); \
3539 entries[n].overlay = (OVERLAY); \
3540 priority = Foverlay_get ((OVERLAY), Qpriority); \
3541 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
3542 entries[n].after_string_p = (AFTER_P); \
3543 ++n; \
3544 } \
3545 while (0)
3546
3547 /* Process overlay before the overlay center. */
3548 for (ov = current_buffer->overlays_before; CONSP (ov); ov = XCDR (ov))
3549 {
3550 overlay = XCAR (ov);
3551 xassert (OVERLAYP (overlay));
3552 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3553 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3554
3555 if (end < charpos)
3556 break;
3557
3558 /* Skip this overlay if it doesn't start or end at IT's current
3559 position. */
3560 if (end != charpos && start != charpos)
3561 continue;
3562
3563 /* Skip this overlay if it doesn't apply to IT->w. */
3564 window = Foverlay_get (overlay, Qwindow);
3565 if (WINDOWP (window) && XWINDOW (window) != it->w)
3566 continue;
3567
3568 /* If the text ``under'' the overlay is invisible, both before-
3569 and after-strings from this overlay are visible; start and
3570 end position are indistinguishable. */
3571 invisible = Foverlay_get (overlay, Qinvisible);
3572 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3573
3574 /* If overlay has a non-empty before-string, record it. */
3575 if ((start == charpos || (end == charpos && invis_p))
3576 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3577 && XSTRING (str)->size)
3578 RECORD_OVERLAY_STRING (overlay, str, 0);
3579
3580 /* If overlay has a non-empty after-string, record it. */
3581 if ((end == charpos || (start == charpos && invis_p))
3582 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3583 && XSTRING (str)->size)
3584 RECORD_OVERLAY_STRING (overlay, str, 1);
3585 }
3586
3587 /* Process overlays after the overlay center. */
3588 for (ov = current_buffer->overlays_after; CONSP (ov); ov = XCDR (ov))
3589 {
3590 overlay = XCAR (ov);
3591 xassert (OVERLAYP (overlay));
3592 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3593 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3594
3595 if (start > charpos)
3596 break;
3597
3598 /* Skip this overlay if it doesn't start or end at IT's current
3599 position. */
3600 if (end != charpos && start != charpos)
3601 continue;
3602
3603 /* Skip this overlay if it doesn't apply to IT->w. */
3604 window = Foverlay_get (overlay, Qwindow);
3605 if (WINDOWP (window) && XWINDOW (window) != it->w)
3606 continue;
3607
3608 /* If the text ``under'' the overlay is invisible, it has a zero
3609 dimension, and both before- and after-strings apply. */
3610 invisible = Foverlay_get (overlay, Qinvisible);
3611 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3612
3613 /* If overlay has a non-empty before-string, record it. */
3614 if ((start == charpos || (end == charpos && invis_p))
3615 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3616 && XSTRING (str)->size)
3617 RECORD_OVERLAY_STRING (overlay, str, 0);
3618
3619 /* If overlay has a non-empty after-string, record it. */
3620 if ((end == charpos || (start == charpos && invis_p))
3621 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3622 && XSTRING (str)->size)
3623 RECORD_OVERLAY_STRING (overlay, str, 1);
3624 }
3625
3626 #undef RECORD_OVERLAY_STRING
3627
3628 /* Sort entries. */
3629 if (n > 1)
3630 qsort (entries, n, sizeof *entries, compare_overlay_entries);
3631
3632 /* Record the total number of strings to process. */
3633 it->n_overlay_strings = n;
3634
3635 /* IT->current.overlay_string_index is the number of overlay strings
3636 that have already been consumed by IT. Copy some of the
3637 remaining overlay strings to IT->overlay_strings. */
3638 i = 0;
3639 j = it->current.overlay_string_index;
3640 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
3641 it->overlay_strings[i++] = entries[j++].string;
3642
3643 CHECK_IT (it);
3644 }
3645
3646
3647 /* Get the first chunk of overlay strings at IT's current buffer
3648 position, or at CHARPOS if that is > 0. Value is non-zero if at
3649 least one overlay string was found. */
3650
3651 static int
3652 get_overlay_strings (it, charpos)
3653 struct it *it;
3654 int charpos;
3655 {
3656 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
3657 process. This fills IT->overlay_strings with strings, and sets
3658 IT->n_overlay_strings to the total number of strings to process.
3659 IT->pos.overlay_string_index has to be set temporarily to zero
3660 because load_overlay_strings needs this; it must be set to -1
3661 when no overlay strings are found because a zero value would
3662 indicate a position in the first overlay string. */
3663 it->current.overlay_string_index = 0;
3664 load_overlay_strings (it, charpos);
3665
3666 /* If we found overlay strings, set up IT to deliver display
3667 elements from the first one. Otherwise set up IT to deliver
3668 from current_buffer. */
3669 if (it->n_overlay_strings)
3670 {
3671 /* Make sure we know settings in current_buffer, so that we can
3672 restore meaningful values when we're done with the overlay
3673 strings. */
3674 compute_stop_pos (it);
3675 xassert (it->face_id >= 0);
3676
3677 /* Save IT's settings. They are restored after all overlay
3678 strings have been processed. */
3679 xassert (it->sp == 0);
3680 push_it (it);
3681
3682 /* Set up IT to deliver display elements from the first overlay
3683 string. */
3684 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3685 it->string = it->overlay_strings[0];
3686 it->stop_charpos = 0;
3687 it->end_charpos = XSTRING (it->string)->size;
3688 it->multibyte_p = STRING_MULTIBYTE (it->string);
3689 xassert (STRINGP (it->string));
3690 it->method = next_element_from_string;
3691 }
3692 else
3693 {
3694 it->string = Qnil;
3695 it->current.overlay_string_index = -1;
3696 it->method = next_element_from_buffer;
3697 }
3698
3699 CHECK_IT (it);
3700
3701 /* Value is non-zero if we found at least one overlay string. */
3702 return STRINGP (it->string);
3703 }
3704
3705
3706 \f
3707 /***********************************************************************
3708 Saving and restoring state
3709 ***********************************************************************/
3710
3711 /* Save current settings of IT on IT->stack. Called, for example,
3712 before setting up IT for an overlay string, to be able to restore
3713 IT's settings to what they were after the overlay string has been
3714 processed. */
3715
3716 static void
3717 push_it (it)
3718 struct it *it;
3719 {
3720 struct iterator_stack_entry *p;
3721
3722 xassert (it->sp < 2);
3723 p = it->stack + it->sp;
3724
3725 p->stop_charpos = it->stop_charpos;
3726 xassert (it->face_id >= 0);
3727 p->face_id = it->face_id;
3728 p->string = it->string;
3729 p->pos = it->current;
3730 p->end_charpos = it->end_charpos;
3731 p->string_nchars = it->string_nchars;
3732 p->area = it->area;
3733 p->multibyte_p = it->multibyte_p;
3734 p->space_width = it->space_width;
3735 p->font_height = it->font_height;
3736 p->voffset = it->voffset;
3737 p->string_from_display_prop_p = it->string_from_display_prop_p;
3738 p->display_ellipsis_p = 0;
3739 ++it->sp;
3740 }
3741
3742
3743 /* Restore IT's settings from IT->stack. Called, for example, when no
3744 more overlay strings must be processed, and we return to delivering
3745 display elements from a buffer, or when the end of a string from a
3746 `display' property is reached and we return to delivering display
3747 elements from an overlay string, or from a buffer. */
3748
3749 static void
3750 pop_it (it)
3751 struct it *it;
3752 {
3753 struct iterator_stack_entry *p;
3754
3755 xassert (it->sp > 0);
3756 --it->sp;
3757 p = it->stack + it->sp;
3758 it->stop_charpos = p->stop_charpos;
3759 it->face_id = p->face_id;
3760 it->string = p->string;
3761 it->current = p->pos;
3762 it->end_charpos = p->end_charpos;
3763 it->string_nchars = p->string_nchars;
3764 it->area = p->area;
3765 it->multibyte_p = p->multibyte_p;
3766 it->space_width = p->space_width;
3767 it->font_height = p->font_height;
3768 it->voffset = p->voffset;
3769 it->string_from_display_prop_p = p->string_from_display_prop_p;
3770 }
3771
3772
3773 \f
3774 /***********************************************************************
3775 Moving over lines
3776 ***********************************************************************/
3777
3778 /* Set IT's current position to the previous line start. */
3779
3780 static void
3781 back_to_previous_line_start (it)
3782 struct it *it;
3783 {
3784 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
3785 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3786 }
3787
3788
3789 /* Move IT to the next line start.
3790
3791 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
3792 we skipped over part of the text (as opposed to moving the iterator
3793 continuously over the text). Otherwise, don't change the value
3794 of *SKIPPED_P.
3795
3796 Newlines may come from buffer text, overlay strings, or strings
3797 displayed via the `display' property. That's the reason we can't
3798 simply use find_next_newline_no_quit.
3799
3800 Note that this function may not skip over invisible text that is so
3801 because of text properties and immediately follows a newline. If
3802 it would, function reseat_at_next_visible_line_start, when called
3803 from set_iterator_to_next, would effectively make invisible
3804 characters following a newline part of the wrong glyph row, which
3805 leads to wrong cursor motion. */
3806
3807 static int
3808 forward_to_next_line_start (it, skipped_p)
3809 struct it *it;
3810 int *skipped_p;
3811 {
3812 int old_selective, newline_found_p, n;
3813 const int MAX_NEWLINE_DISTANCE = 500;
3814
3815 /* If already on a newline, just consume it to avoid unintended
3816 skipping over invisible text below. */
3817 if (it->what == IT_CHARACTER
3818 && it->c == '\n'
3819 && CHARPOS (it->position) == IT_CHARPOS (*it))
3820 {
3821 set_iterator_to_next (it, 0);
3822 it->c = 0;
3823 return 1;
3824 }
3825
3826 /* Don't handle selective display in the following. It's (a)
3827 unnecessary because it's done by the caller, and (b) leads to an
3828 infinite recursion because next_element_from_ellipsis indirectly
3829 calls this function. */
3830 old_selective = it->selective;
3831 it->selective = 0;
3832
3833 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
3834 from buffer text. */
3835 for (n = newline_found_p = 0;
3836 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
3837 n += STRINGP (it->string) ? 0 : 1)
3838 {
3839 if (!get_next_display_element (it))
3840 break;
3841 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
3842 set_iterator_to_next (it, 0);
3843 }
3844
3845 /* If we didn't find a newline near enough, see if we can use a
3846 short-cut. */
3847 if (n == MAX_NEWLINE_DISTANCE)
3848 {
3849 int start = IT_CHARPOS (*it);
3850 int limit = find_next_newline_no_quit (start, 1);
3851 Lisp_Object pos;
3852
3853 xassert (!STRINGP (it->string));
3854
3855 /* If there isn't any `display' property in sight, and no
3856 overlays, we can just use the position of the newline in
3857 buffer text. */
3858 if (it->stop_charpos >= limit
3859 || ((pos = Fnext_single_property_change (make_number (start),
3860 Qdisplay,
3861 Qnil, make_number (limit)),
3862 NILP (pos))
3863 && next_overlay_change (start) == ZV))
3864 {
3865 IT_CHARPOS (*it) = limit;
3866 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
3867 *skipped_p = newline_found_p = 1;
3868 }
3869 else
3870 {
3871 while (get_next_display_element (it)
3872 && !newline_found_p)
3873 {
3874 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
3875 set_iterator_to_next (it, 0);
3876 }
3877 }
3878 }
3879
3880 it->selective = old_selective;
3881 return newline_found_p;
3882 }
3883
3884
3885 /* Set IT's current position to the previous visible line start. Skip
3886 invisible text that is so either due to text properties or due to
3887 selective display. Caution: this does not change IT->current_x and
3888 IT->hpos. */
3889
3890 static void
3891 back_to_previous_visible_line_start (it)
3892 struct it *it;
3893 {
3894 int visible_p = 0;
3895
3896 /* Go back one newline if not on BEGV already. */
3897 if (IT_CHARPOS (*it) > BEGV)
3898 back_to_previous_line_start (it);
3899
3900 /* Move over lines that are invisible because of selective display
3901 or text properties. */
3902 while (IT_CHARPOS (*it) > BEGV
3903 && !visible_p)
3904 {
3905 visible_p = 1;
3906
3907 /* If selective > 0, then lines indented more than that values
3908 are invisible. */
3909 if (it->selective > 0
3910 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3911 it->selective))
3912 visible_p = 0;
3913 else
3914 {
3915 Lisp_Object prop;
3916
3917 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
3918 Qinvisible, it->window);
3919 if (TEXT_PROP_MEANS_INVISIBLE (prop))
3920 visible_p = 0;
3921 }
3922
3923 /* Back one more newline if the current one is invisible. */
3924 if (!visible_p)
3925 back_to_previous_line_start (it);
3926 }
3927
3928 xassert (IT_CHARPOS (*it) >= BEGV);
3929 xassert (IT_CHARPOS (*it) == BEGV
3930 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3931 CHECK_IT (it);
3932 }
3933
3934
3935 /* Reseat iterator IT at the previous visible line start. Skip
3936 invisible text that is so either due to text properties or due to
3937 selective display. At the end, update IT's overlay information,
3938 face information etc. */
3939
3940 static void
3941 reseat_at_previous_visible_line_start (it)
3942 struct it *it;
3943 {
3944 back_to_previous_visible_line_start (it);
3945 reseat (it, it->current.pos, 1);
3946 CHECK_IT (it);
3947 }
3948
3949
3950 /* Reseat iterator IT on the next visible line start in the current
3951 buffer. ON_NEWLINE_P non-zero means position IT on the newline
3952 preceding the line start. Skip over invisible text that is so
3953 because of selective display. Compute faces, overlays etc at the
3954 new position. Note that this function does not skip over text that
3955 is invisible because of text properties. */
3956
3957 static void
3958 reseat_at_next_visible_line_start (it, on_newline_p)
3959 struct it *it;
3960 int on_newline_p;
3961 {
3962 int newline_found_p, skipped_p = 0;
3963
3964 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3965
3966 /* Skip over lines that are invisible because they are indented
3967 more than the value of IT->selective. */
3968 if (it->selective > 0)
3969 while (IT_CHARPOS (*it) < ZV
3970 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3971 it->selective))
3972 {
3973 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3974 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3975 }
3976
3977 /* Position on the newline if that's what's requested. */
3978 if (on_newline_p && newline_found_p)
3979 {
3980 if (STRINGP (it->string))
3981 {
3982 if (IT_STRING_CHARPOS (*it) > 0)
3983 {
3984 --IT_STRING_CHARPOS (*it);
3985 --IT_STRING_BYTEPOS (*it);
3986 }
3987 }
3988 else if (IT_CHARPOS (*it) > BEGV)
3989 {
3990 --IT_CHARPOS (*it);
3991 --IT_BYTEPOS (*it);
3992 reseat (it, it->current.pos, 0);
3993 }
3994 }
3995 else if (skipped_p)
3996 reseat (it, it->current.pos, 0);
3997
3998 CHECK_IT (it);
3999 }
4000
4001
4002 \f
4003 /***********************************************************************
4004 Changing an iterator's position
4005 ***********************************************************************/
4006
4007 /* Change IT's current position to POS in current_buffer. If FORCE_P
4008 is non-zero, always check for text properties at the new position.
4009 Otherwise, text properties are only looked up if POS >=
4010 IT->check_charpos of a property. */
4011
4012 static void
4013 reseat (it, pos, force_p)
4014 struct it *it;
4015 struct text_pos pos;
4016 int force_p;
4017 {
4018 int original_pos = IT_CHARPOS (*it);
4019
4020 reseat_1 (it, pos, 0);
4021
4022 /* Determine where to check text properties. Avoid doing it
4023 where possible because text property lookup is very expensive. */
4024 if (force_p
4025 || CHARPOS (pos) > it->stop_charpos
4026 || CHARPOS (pos) < original_pos)
4027 handle_stop (it);
4028
4029 CHECK_IT (it);
4030 }
4031
4032
4033 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4034 IT->stop_pos to POS, also. */
4035
4036 static void
4037 reseat_1 (it, pos, set_stop_p)
4038 struct it *it;
4039 struct text_pos pos;
4040 int set_stop_p;
4041 {
4042 /* Don't call this function when scanning a C string. */
4043 xassert (it->s == NULL);
4044
4045 /* POS must be a reasonable value. */
4046 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4047
4048 it->current.pos = it->position = pos;
4049 XSETBUFFER (it->object, current_buffer);
4050 it->end_charpos = ZV;
4051 it->dpvec = NULL;
4052 it->current.dpvec_index = -1;
4053 it->current.overlay_string_index = -1;
4054 IT_STRING_CHARPOS (*it) = -1;
4055 IT_STRING_BYTEPOS (*it) = -1;
4056 it->string = Qnil;
4057 it->method = next_element_from_buffer;
4058 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
4059 it->sp = 0;
4060 it->face_before_selective_p = 0;
4061
4062 if (set_stop_p)
4063 it->stop_charpos = CHARPOS (pos);
4064 }
4065
4066
4067 /* Set up IT for displaying a string, starting at CHARPOS in window W.
4068 If S is non-null, it is a C string to iterate over. Otherwise,
4069 STRING gives a Lisp string to iterate over.
4070
4071 If PRECISION > 0, don't return more then PRECISION number of
4072 characters from the string.
4073
4074 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4075 characters have been returned. FIELD_WIDTH < 0 means an infinite
4076 field width.
4077
4078 MULTIBYTE = 0 means disable processing of multibyte characters,
4079 MULTIBYTE > 0 means enable it,
4080 MULTIBYTE < 0 means use IT->multibyte_p.
4081
4082 IT must be initialized via a prior call to init_iterator before
4083 calling this function. */
4084
4085 static void
4086 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4087 struct it *it;
4088 unsigned char *s;
4089 Lisp_Object string;
4090 int charpos;
4091 int precision, field_width, multibyte;
4092 {
4093 /* No region in strings. */
4094 it->region_beg_charpos = it->region_end_charpos = -1;
4095
4096 /* No text property checks performed by default, but see below. */
4097 it->stop_charpos = -1;
4098
4099 /* Set iterator position and end position. */
4100 bzero (&it->current, sizeof it->current);
4101 it->current.overlay_string_index = -1;
4102 it->current.dpvec_index = -1;
4103 xassert (charpos >= 0);
4104
4105 /* If STRING is specified, use its multibyteness, otherwise use the
4106 setting of MULTIBYTE, if specified. */
4107 if (multibyte >= 0)
4108 it->multibyte_p = multibyte > 0;
4109
4110 if (s == NULL)
4111 {
4112 xassert (STRINGP (string));
4113 it->string = string;
4114 it->s = NULL;
4115 it->end_charpos = it->string_nchars = XSTRING (string)->size;
4116 it->method = next_element_from_string;
4117 it->current.string_pos = string_pos (charpos, string);
4118 }
4119 else
4120 {
4121 it->s = s;
4122 it->string = Qnil;
4123
4124 /* Note that we use IT->current.pos, not it->current.string_pos,
4125 for displaying C strings. */
4126 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4127 if (it->multibyte_p)
4128 {
4129 it->current.pos = c_string_pos (charpos, s, 1);
4130 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4131 }
4132 else
4133 {
4134 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4135 it->end_charpos = it->string_nchars = strlen (s);
4136 }
4137
4138 it->method = next_element_from_c_string;
4139 }
4140
4141 /* PRECISION > 0 means don't return more than PRECISION characters
4142 from the string. */
4143 if (precision > 0 && it->end_charpos - charpos > precision)
4144 it->end_charpos = it->string_nchars = charpos + precision;
4145
4146 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4147 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4148 FIELD_WIDTH < 0 means infinite field width. This is useful for
4149 padding with `-' at the end of a mode line. */
4150 if (field_width < 0)
4151 field_width = INFINITY;
4152 if (field_width > it->end_charpos - charpos)
4153 it->end_charpos = charpos + field_width;
4154
4155 /* Use the standard display table for displaying strings. */
4156 if (DISP_TABLE_P (Vstandard_display_table))
4157 it->dp = XCHAR_TABLE (Vstandard_display_table);
4158
4159 it->stop_charpos = charpos;
4160 CHECK_IT (it);
4161 }
4162
4163
4164 \f
4165 /***********************************************************************
4166 Iteration
4167 ***********************************************************************/
4168
4169 /* Load IT's display element fields with information about the next
4170 display element from the current position of IT. Value is zero if
4171 end of buffer (or C string) is reached. */
4172
4173 int
4174 get_next_display_element (it)
4175 struct it *it;
4176 {
4177 /* Non-zero means that we found an display element. Zero means that
4178 we hit the end of what we iterate over. Performance note: the
4179 function pointer `method' used here turns out to be faster than
4180 using a sequence of if-statements. */
4181 int success_p = (*it->method) (it);
4182
4183 if (it->what == IT_CHARACTER)
4184 {
4185 /* Map via display table or translate control characters.
4186 IT->c, IT->len etc. have been set to the next character by
4187 the function call above. If we have a display table, and it
4188 contains an entry for IT->c, translate it. Don't do this if
4189 IT->c itself comes from a display table, otherwise we could
4190 end up in an infinite recursion. (An alternative could be to
4191 count the recursion depth of this function and signal an
4192 error when a certain maximum depth is reached.) Is it worth
4193 it? */
4194 if (success_p && it->dpvec == NULL)
4195 {
4196 Lisp_Object dv;
4197
4198 if (it->dp
4199 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
4200 VECTORP (dv)))
4201 {
4202 struct Lisp_Vector *v = XVECTOR (dv);
4203
4204 /* Return the first character from the display table
4205 entry, if not empty. If empty, don't display the
4206 current character. */
4207 if (v->size)
4208 {
4209 it->dpvec_char_len = it->len;
4210 it->dpvec = v->contents;
4211 it->dpend = v->contents + v->size;
4212 it->current.dpvec_index = 0;
4213 it->method = next_element_from_display_vector;
4214 success_p = get_next_display_element (it);
4215 }
4216 else
4217 {
4218 set_iterator_to_next (it, 0);
4219 success_p = get_next_display_element (it);
4220 }
4221 }
4222
4223 /* Translate control characters into `\003' or `^C' form.
4224 Control characters coming from a display table entry are
4225 currently not translated because we use IT->dpvec to hold
4226 the translation. This could easily be changed but I
4227 don't believe that it is worth doing.
4228
4229 Non-printable multibyte characters are also translated
4230 octal form. */
4231 else if ((it->c < ' '
4232 && (it->area != TEXT_AREA
4233 || (it->c != '\n' && it->c != '\t')))
4234 || (it->c >= 127
4235 && it->len == 1)
4236 || !CHAR_PRINTABLE_P (it->c))
4237 {
4238 /* IT->c is a control character which must be displayed
4239 either as '\003' or as `^C' where the '\\' and '^'
4240 can be defined in the display table. Fill
4241 IT->ctl_chars with glyphs for what we have to
4242 display. Then, set IT->dpvec to these glyphs. */
4243 GLYPH g;
4244
4245 if (it->c < 128 && it->ctl_arrow_p)
4246 {
4247 /* Set IT->ctl_chars[0] to the glyph for `^'. */
4248 if (it->dp
4249 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
4250 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
4251 g = XINT (DISP_CTRL_GLYPH (it->dp));
4252 else
4253 g = FAST_MAKE_GLYPH ('^', 0);
4254 XSETINT (it->ctl_chars[0], g);
4255
4256 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
4257 XSETINT (it->ctl_chars[1], g);
4258
4259 /* Set up IT->dpvec and return first character from it. */
4260 it->dpvec_char_len = it->len;
4261 it->dpvec = it->ctl_chars;
4262 it->dpend = it->dpvec + 2;
4263 it->current.dpvec_index = 0;
4264 it->method = next_element_from_display_vector;
4265 get_next_display_element (it);
4266 }
4267 else
4268 {
4269 unsigned char str[MAX_MULTIBYTE_LENGTH];
4270 int len;
4271 int i;
4272 GLYPH escape_glyph;
4273
4274 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
4275 if (it->dp
4276 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
4277 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
4278 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
4279 else
4280 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
4281
4282 if (SINGLE_BYTE_CHAR_P (it->c))
4283 str[0] = it->c, len = 1;
4284 else
4285 {
4286 len = CHAR_STRING_NO_SIGNAL (it->c, str);
4287 if (len < 0)
4288 {
4289 /* It's an invalid character, which
4290 shouldn't happen actually, but due to
4291 bugs it may happen. Let's print the char
4292 as is, there's not much meaningful we can
4293 do with it. */
4294 str[0] = it->c;
4295 str[1] = it->c >> 8;
4296 str[2] = it->c >> 16;
4297 str[3] = it->c >> 24;
4298 len = 4;
4299 }
4300 }
4301
4302 for (i = 0; i < len; i++)
4303 {
4304 XSETINT (it->ctl_chars[i * 4], escape_glyph);
4305 /* Insert three more glyphs into IT->ctl_chars for
4306 the octal display of the character. */
4307 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
4308 XSETINT (it->ctl_chars[i * 4 + 1], g);
4309 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
4310 XSETINT (it->ctl_chars[i * 4 + 2], g);
4311 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
4312 XSETINT (it->ctl_chars[i * 4 + 3], g);
4313 }
4314
4315 /* Set up IT->dpvec and return the first character
4316 from it. */
4317 it->dpvec_char_len = it->len;
4318 it->dpvec = it->ctl_chars;
4319 it->dpend = it->dpvec + len * 4;
4320 it->current.dpvec_index = 0;
4321 it->method = next_element_from_display_vector;
4322 get_next_display_element (it);
4323 }
4324 }
4325 }
4326
4327 /* Adjust face id for a multibyte character. There are no
4328 multibyte character in unibyte text. */
4329 if (it->multibyte_p
4330 && success_p
4331 && FRAME_WINDOW_P (it->f))
4332 {
4333 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4334 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
4335 }
4336 }
4337
4338 /* Is this character the last one of a run of characters with
4339 box? If yes, set IT->end_of_box_run_p to 1. */
4340 if (it->face_box_p
4341 && it->s == NULL)
4342 {
4343 int face_id;
4344 struct face *face;
4345
4346 it->end_of_box_run_p
4347 = ((face_id = face_after_it_pos (it),
4348 face_id != it->face_id)
4349 && (face = FACE_FROM_ID (it->f, face_id),
4350 face->box == FACE_NO_BOX));
4351 }
4352
4353 /* Value is 0 if end of buffer or string reached. */
4354 return success_p;
4355 }
4356
4357
4358 /* Move IT to the next display element.
4359
4360 RESEAT_P non-zero means if called on a newline in buffer text,
4361 skip to the next visible line start.
4362
4363 Functions get_next_display_element and set_iterator_to_next are
4364 separate because I find this arrangement easier to handle than a
4365 get_next_display_element function that also increments IT's
4366 position. The way it is we can first look at an iterator's current
4367 display element, decide whether it fits on a line, and if it does,
4368 increment the iterator position. The other way around we probably
4369 would either need a flag indicating whether the iterator has to be
4370 incremented the next time, or we would have to implement a
4371 decrement position function which would not be easy to write. */
4372
4373 void
4374 set_iterator_to_next (it, reseat_p)
4375 struct it *it;
4376 int reseat_p;
4377 {
4378 /* Reset flags indicating start and end of a sequence of characters
4379 with box. Reset them at the start of this function because
4380 moving the iterator to a new position might set them. */
4381 it->start_of_box_run_p = it->end_of_box_run_p = 0;
4382
4383 if (it->method == next_element_from_buffer)
4384 {
4385 /* The current display element of IT is a character from
4386 current_buffer. Advance in the buffer, and maybe skip over
4387 invisible lines that are so because of selective display. */
4388 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
4389 reseat_at_next_visible_line_start (it, 0);
4390 else
4391 {
4392 xassert (it->len != 0);
4393 IT_BYTEPOS (*it) += it->len;
4394 IT_CHARPOS (*it) += 1;
4395 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
4396 }
4397 }
4398 else if (it->method == next_element_from_composition)
4399 {
4400 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
4401 if (STRINGP (it->string))
4402 {
4403 IT_STRING_BYTEPOS (*it) += it->len;
4404 IT_STRING_CHARPOS (*it) += it->cmp_len;
4405 it->method = next_element_from_string;
4406 goto consider_string_end;
4407 }
4408 else
4409 {
4410 IT_BYTEPOS (*it) += it->len;
4411 IT_CHARPOS (*it) += it->cmp_len;
4412 it->method = next_element_from_buffer;
4413 }
4414 }
4415 else if (it->method == next_element_from_c_string)
4416 {
4417 /* Current display element of IT is from a C string. */
4418 IT_BYTEPOS (*it) += it->len;
4419 IT_CHARPOS (*it) += 1;
4420 }
4421 else if (it->method == next_element_from_display_vector)
4422 {
4423 /* Current display element of IT is from a display table entry.
4424 Advance in the display table definition. Reset it to null if
4425 end reached, and continue with characters from buffers/
4426 strings. */
4427 ++it->current.dpvec_index;
4428
4429 /* Restore face of the iterator to what they were before the
4430 display vector entry (these entries may contain faces). */
4431 it->face_id = it->saved_face_id;
4432
4433 if (it->dpvec + it->current.dpvec_index == it->dpend)
4434 {
4435 if (it->s)
4436 it->method = next_element_from_c_string;
4437 else if (STRINGP (it->string))
4438 it->method = next_element_from_string;
4439 else
4440 it->method = next_element_from_buffer;
4441
4442 it->dpvec = NULL;
4443 it->current.dpvec_index = -1;
4444
4445 /* Skip over characters which were displayed via IT->dpvec. */
4446 if (it->dpvec_char_len < 0)
4447 reseat_at_next_visible_line_start (it, 1);
4448 else if (it->dpvec_char_len > 0)
4449 {
4450 it->len = it->dpvec_char_len;
4451 set_iterator_to_next (it, reseat_p);
4452 }
4453 }
4454 }
4455 else if (it->method == next_element_from_string)
4456 {
4457 /* Current display element is a character from a Lisp string. */
4458 xassert (it->s == NULL && STRINGP (it->string));
4459 IT_STRING_BYTEPOS (*it) += it->len;
4460 IT_STRING_CHARPOS (*it) += 1;
4461
4462 consider_string_end:
4463
4464 if (it->current.overlay_string_index >= 0)
4465 {
4466 /* IT->string is an overlay string. Advance to the
4467 next, if there is one. */
4468 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
4469 next_overlay_string (it);
4470 }
4471 else
4472 {
4473 /* IT->string is not an overlay string. If we reached
4474 its end, and there is something on IT->stack, proceed
4475 with what is on the stack. This can be either another
4476 string, this time an overlay string, or a buffer. */
4477 if (IT_STRING_CHARPOS (*it) == XSTRING (it->string)->size
4478 && it->sp > 0)
4479 {
4480 pop_it (it);
4481 if (!STRINGP (it->string))
4482 it->method = next_element_from_buffer;
4483 else
4484 goto consider_string_end;
4485 }
4486 }
4487 }
4488 else if (it->method == next_element_from_image
4489 || it->method == next_element_from_stretch)
4490 {
4491 /* The position etc with which we have to proceed are on
4492 the stack. The position may be at the end of a string,
4493 if the `display' property takes up the whole string. */
4494 pop_it (it);
4495 it->image_id = 0;
4496 if (STRINGP (it->string))
4497 {
4498 it->method = next_element_from_string;
4499 goto consider_string_end;
4500 }
4501 else
4502 it->method = next_element_from_buffer;
4503 }
4504 else
4505 /* There are no other methods defined, so this should be a bug. */
4506 abort ();
4507
4508 xassert (it->method != next_element_from_string
4509 || (STRINGP (it->string)
4510 && IT_STRING_CHARPOS (*it) >= 0));
4511 }
4512
4513
4514 /* Load IT's display element fields with information about the next
4515 display element which comes from a display table entry or from the
4516 result of translating a control character to one of the forms `^C'
4517 or `\003'. IT->dpvec holds the glyphs to return as characters. */
4518
4519 static int
4520 next_element_from_display_vector (it)
4521 struct it *it;
4522 {
4523 /* Precondition. */
4524 xassert (it->dpvec && it->current.dpvec_index >= 0);
4525
4526 /* Remember the current face id in case glyphs specify faces.
4527 IT's face is restored in set_iterator_to_next. */
4528 it->saved_face_id = it->face_id;
4529
4530 if (INTEGERP (*it->dpvec)
4531 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
4532 {
4533 int lface_id;
4534 GLYPH g;
4535
4536 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
4537 it->c = FAST_GLYPH_CHAR (g);
4538 it->len = CHAR_BYTES (it->c);
4539
4540 /* The entry may contain a face id to use. Such a face id is
4541 the id of a Lisp face, not a realized face. A face id of
4542 zero means no face is specified. */
4543 lface_id = FAST_GLYPH_FACE (g);
4544 if (lface_id)
4545 {
4546 /* The function returns -1 if lface_id is invalid. */
4547 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
4548 if (face_id >= 0)
4549 it->face_id = face_id;
4550 }
4551 }
4552 else
4553 /* Display table entry is invalid. Return a space. */
4554 it->c = ' ', it->len = 1;
4555
4556 /* Don't change position and object of the iterator here. They are
4557 still the values of the character that had this display table
4558 entry or was translated, and that's what we want. */
4559 it->what = IT_CHARACTER;
4560 return 1;
4561 }
4562
4563
4564 /* Load IT with the next display element from Lisp string IT->string.
4565 IT->current.string_pos is the current position within the string.
4566 If IT->current.overlay_string_index >= 0, the Lisp string is an
4567 overlay string. */
4568
4569 static int
4570 next_element_from_string (it)
4571 struct it *it;
4572 {
4573 struct text_pos position;
4574
4575 xassert (STRINGP (it->string));
4576 xassert (IT_STRING_CHARPOS (*it) >= 0);
4577 position = it->current.string_pos;
4578
4579 /* Time to check for invisible text? */
4580 if (IT_STRING_CHARPOS (*it) < it->end_charpos
4581 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
4582 {
4583 handle_stop (it);
4584
4585 /* Since a handler may have changed IT->method, we must
4586 recurse here. */
4587 return get_next_display_element (it);
4588 }
4589
4590 if (it->current.overlay_string_index >= 0)
4591 {
4592 /* Get the next character from an overlay string. In overlay
4593 strings, There is no field width or padding with spaces to
4594 do. */
4595 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
4596 {
4597 it->what = IT_EOB;
4598 return 0;
4599 }
4600 else if (STRING_MULTIBYTE (it->string))
4601 {
4602 int remaining = (STRING_BYTES (XSTRING (it->string))
4603 - IT_STRING_BYTEPOS (*it));
4604 unsigned char *s = (XSTRING (it->string)->data
4605 + IT_STRING_BYTEPOS (*it));
4606 it->c = string_char_and_length (s, remaining, &it->len);
4607 }
4608 else
4609 {
4610 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
4611 it->len = 1;
4612 }
4613 }
4614 else
4615 {
4616 /* Get the next character from a Lisp string that is not an
4617 overlay string. Such strings come from the mode line, for
4618 example. We may have to pad with spaces, or truncate the
4619 string. See also next_element_from_c_string. */
4620 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
4621 {
4622 it->what = IT_EOB;
4623 return 0;
4624 }
4625 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
4626 {
4627 /* Pad with spaces. */
4628 it->c = ' ', it->len = 1;
4629 CHARPOS (position) = BYTEPOS (position) = -1;
4630 }
4631 else if (STRING_MULTIBYTE (it->string))
4632 {
4633 int maxlen = (STRING_BYTES (XSTRING (it->string))
4634 - IT_STRING_BYTEPOS (*it));
4635 unsigned char *s = (XSTRING (it->string)->data
4636 + IT_STRING_BYTEPOS (*it));
4637 it->c = string_char_and_length (s, maxlen, &it->len);
4638 }
4639 else
4640 {
4641 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
4642 it->len = 1;
4643 }
4644 }
4645
4646 /* Record what we have and where it came from. Note that we store a
4647 buffer position in IT->position although it could arguably be a
4648 string position. */
4649 it->what = IT_CHARACTER;
4650 it->object = it->string;
4651 it->position = position;
4652 return 1;
4653 }
4654
4655
4656 /* Load IT with next display element from C string IT->s.
4657 IT->string_nchars is the maximum number of characters to return
4658 from the string. IT->end_charpos may be greater than
4659 IT->string_nchars when this function is called, in which case we
4660 may have to return padding spaces. Value is zero if end of string
4661 reached, including padding spaces. */
4662
4663 static int
4664 next_element_from_c_string (it)
4665 struct it *it;
4666 {
4667 int success_p = 1;
4668
4669 xassert (it->s);
4670 it->what = IT_CHARACTER;
4671 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
4672 it->object = Qnil;
4673
4674 /* IT's position can be greater IT->string_nchars in case a field
4675 width or precision has been specified when the iterator was
4676 initialized. */
4677 if (IT_CHARPOS (*it) >= it->end_charpos)
4678 {
4679 /* End of the game. */
4680 it->what = IT_EOB;
4681 success_p = 0;
4682 }
4683 else if (IT_CHARPOS (*it) >= it->string_nchars)
4684 {
4685 /* Pad with spaces. */
4686 it->c = ' ', it->len = 1;
4687 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
4688 }
4689 else if (it->multibyte_p)
4690 {
4691 /* Implementation note: The calls to strlen apparently aren't a
4692 performance problem because there is no noticeable performance
4693 difference between Emacs running in unibyte or multibyte mode. */
4694 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
4695 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
4696 maxlen, &it->len);
4697 }
4698 else
4699 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
4700
4701 return success_p;
4702 }
4703
4704
4705 /* Set up IT to return characters from an ellipsis, if appropriate.
4706 The definition of the ellipsis glyphs may come from a display table
4707 entry. This function Fills IT with the first glyph from the
4708 ellipsis if an ellipsis is to be displayed. */
4709
4710 static int
4711 next_element_from_ellipsis (it)
4712 struct it *it;
4713 {
4714 if (it->selective_display_ellipsis_p)
4715 {
4716 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4717 {
4718 /* Use the display table definition for `...'. Invalid glyphs
4719 will be handled by the method returning elements from dpvec. */
4720 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4721 it->dpvec_char_len = it->len;
4722 it->dpvec = v->contents;
4723 it->dpend = v->contents + v->size;
4724 it->current.dpvec_index = 0;
4725 it->method = next_element_from_display_vector;
4726 }
4727 else
4728 {
4729 /* Use default `...' which is stored in default_invis_vector. */
4730 it->dpvec_char_len = it->len;
4731 it->dpvec = default_invis_vector;
4732 it->dpend = default_invis_vector + 3;
4733 it->current.dpvec_index = 0;
4734 it->method = next_element_from_display_vector;
4735 }
4736 }
4737 else
4738 {
4739 /* The face at the current position may be different from the
4740 face we find after the invisible text. Remember what it
4741 was in IT->saved_face_id, and signal that it's there by
4742 setting face_before_selective_p. */
4743 it->saved_face_id = it->face_id;
4744 it->method = next_element_from_buffer;
4745 reseat_at_next_visible_line_start (it, 1);
4746 it->face_before_selective_p = 1;
4747 }
4748
4749 return get_next_display_element (it);
4750 }
4751
4752
4753 /* Deliver an image display element. The iterator IT is already
4754 filled with image information (done in handle_display_prop). Value
4755 is always 1. */
4756
4757
4758 static int
4759 next_element_from_image (it)
4760 struct it *it;
4761 {
4762 it->what = IT_IMAGE;
4763 return 1;
4764 }
4765
4766
4767 /* Fill iterator IT with next display element from a stretch glyph
4768 property. IT->object is the value of the text property. Value is
4769 always 1. */
4770
4771 static int
4772 next_element_from_stretch (it)
4773 struct it *it;
4774 {
4775 it->what = IT_STRETCH;
4776 return 1;
4777 }
4778
4779
4780 /* Load IT with the next display element from current_buffer. Value
4781 is zero if end of buffer reached. IT->stop_charpos is the next
4782 position at which to stop and check for text properties or buffer
4783 end. */
4784
4785 static int
4786 next_element_from_buffer (it)
4787 struct it *it;
4788 {
4789 int success_p = 1;
4790
4791 /* Check this assumption, otherwise, we would never enter the
4792 if-statement, below. */
4793 xassert (IT_CHARPOS (*it) >= BEGV
4794 && IT_CHARPOS (*it) <= it->stop_charpos);
4795
4796 if (IT_CHARPOS (*it) >= it->stop_charpos)
4797 {
4798 if (IT_CHARPOS (*it) >= it->end_charpos)
4799 {
4800 int overlay_strings_follow_p;
4801
4802 /* End of the game, except when overlay strings follow that
4803 haven't been returned yet. */
4804 if (it->overlay_strings_at_end_processed_p)
4805 overlay_strings_follow_p = 0;
4806 else
4807 {
4808 it->overlay_strings_at_end_processed_p = 1;
4809 overlay_strings_follow_p = get_overlay_strings (it, 0);
4810 }
4811
4812 if (overlay_strings_follow_p)
4813 success_p = get_next_display_element (it);
4814 else
4815 {
4816 it->what = IT_EOB;
4817 it->position = it->current.pos;
4818 success_p = 0;
4819 }
4820 }
4821 else
4822 {
4823 handle_stop (it);
4824 return get_next_display_element (it);
4825 }
4826 }
4827 else
4828 {
4829 /* No face changes, overlays etc. in sight, so just return a
4830 character from current_buffer. */
4831 unsigned char *p;
4832
4833 /* Maybe run the redisplay end trigger hook. Performance note:
4834 This doesn't seem to cost measurable time. */
4835 if (it->redisplay_end_trigger_charpos
4836 && it->glyph_row
4837 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
4838 run_redisplay_end_trigger_hook (it);
4839
4840 /* Get the next character, maybe multibyte. */
4841 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
4842 if (it->multibyte_p && !ASCII_BYTE_P (*p))
4843 {
4844 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
4845 - IT_BYTEPOS (*it));
4846 it->c = string_char_and_length (p, maxlen, &it->len);
4847 }
4848 else
4849 it->c = *p, it->len = 1;
4850
4851 /* Record what we have and where it came from. */
4852 it->what = IT_CHARACTER;;
4853 it->object = it->w->buffer;
4854 it->position = it->current.pos;
4855
4856 /* Normally we return the character found above, except when we
4857 really want to return an ellipsis for selective display. */
4858 if (it->selective)
4859 {
4860 if (it->c == '\n')
4861 {
4862 /* A value of selective > 0 means hide lines indented more
4863 than that number of columns. */
4864 if (it->selective > 0
4865 && IT_CHARPOS (*it) + 1 < ZV
4866 && indented_beyond_p (IT_CHARPOS (*it) + 1,
4867 IT_BYTEPOS (*it) + 1,
4868 it->selective))
4869 {
4870 success_p = next_element_from_ellipsis (it);
4871 it->dpvec_char_len = -1;
4872 }
4873 }
4874 else if (it->c == '\r' && it->selective == -1)
4875 {
4876 /* A value of selective == -1 means that everything from the
4877 CR to the end of the line is invisible, with maybe an
4878 ellipsis displayed for it. */
4879 success_p = next_element_from_ellipsis (it);
4880 it->dpvec_char_len = -1;
4881 }
4882 }
4883 }
4884
4885 /* Value is zero if end of buffer reached. */
4886 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
4887 return success_p;
4888 }
4889
4890
4891 /* Run the redisplay end trigger hook for IT. */
4892
4893 static void
4894 run_redisplay_end_trigger_hook (it)
4895 struct it *it;
4896 {
4897 Lisp_Object args[3];
4898
4899 /* IT->glyph_row should be non-null, i.e. we should be actually
4900 displaying something, or otherwise we should not run the hook. */
4901 xassert (it->glyph_row);
4902
4903 /* Set up hook arguments. */
4904 args[0] = Qredisplay_end_trigger_functions;
4905 args[1] = it->window;
4906 XSETINT (args[2], it->redisplay_end_trigger_charpos);
4907 it->redisplay_end_trigger_charpos = 0;
4908
4909 /* Since we are *trying* to run these functions, don't try to run
4910 them again, even if they get an error. */
4911 it->w->redisplay_end_trigger = Qnil;
4912 Frun_hook_with_args (3, args);
4913
4914 /* Notice if it changed the face of the character we are on. */
4915 handle_face_prop (it);
4916 }
4917
4918
4919 /* Deliver a composition display element. The iterator IT is already
4920 filled with composition information (done in
4921 handle_composition_prop). Value is always 1. */
4922
4923 static int
4924 next_element_from_composition (it)
4925 struct it *it;
4926 {
4927 it->what = IT_COMPOSITION;
4928 it->position = (STRINGP (it->string)
4929 ? it->current.string_pos
4930 : it->current.pos);
4931 return 1;
4932 }
4933
4934
4935 \f
4936 /***********************************************************************
4937 Moving an iterator without producing glyphs
4938 ***********************************************************************/
4939
4940 /* Move iterator IT to a specified buffer or X position within one
4941 line on the display without producing glyphs.
4942
4943 Begin to skip at IT's current position. Skip to TO_CHARPOS or TO_X
4944 whichever is reached first.
4945
4946 TO_CHARPOS <= 0 means no TO_CHARPOS is specified.
4947
4948 TO_X < 0 means that no TO_X is specified. TO_X is normally a value
4949 0 <= TO_X <= IT->last_visible_x. This means in particular, that
4950 TO_X includes the amount by which a window is horizontally
4951 scrolled.
4952
4953 Value is
4954
4955 MOVE_POS_MATCH_OR_ZV
4956 - when TO_POS or ZV was reached.
4957
4958 MOVE_X_REACHED
4959 -when TO_X was reached before TO_POS or ZV were reached.
4960
4961 MOVE_LINE_CONTINUED
4962 - when we reached the end of the display area and the line must
4963 be continued.
4964
4965 MOVE_LINE_TRUNCATED
4966 - when we reached the end of the display area and the line is
4967 truncated.
4968
4969 MOVE_NEWLINE_OR_CR
4970 - when we stopped at a line end, i.e. a newline or a CR and selective
4971 display is on. */
4972
4973 static enum move_it_result
4974 move_it_in_display_line_to (it, to_charpos, to_x, op)
4975 struct it *it;
4976 int to_charpos, to_x, op;
4977 {
4978 enum move_it_result result = MOVE_UNDEFINED;
4979 struct glyph_row *saved_glyph_row;
4980
4981 /* Don't produce glyphs in produce_glyphs. */
4982 saved_glyph_row = it->glyph_row;
4983 it->glyph_row = NULL;
4984
4985 while (1)
4986 {
4987 int x, i, ascent = 0, descent = 0;
4988
4989 /* Stop when ZV or TO_CHARPOS reached. */
4990 if (!get_next_display_element (it)
4991 || ((op & MOVE_TO_POS) != 0
4992 && BUFFERP (it->object)
4993 && IT_CHARPOS (*it) >= to_charpos))
4994 {
4995 result = MOVE_POS_MATCH_OR_ZV;
4996 break;
4997 }
4998
4999 /* The call to produce_glyphs will get the metrics of the
5000 display element IT is loaded with. We record in x the
5001 x-position before this display element in case it does not
5002 fit on the line. */
5003 x = it->current_x;
5004
5005 /* Remember the line height so far in case the next element doesn't
5006 fit on the line. */
5007 if (!it->truncate_lines_p)
5008 {
5009 ascent = it->max_ascent;
5010 descent = it->max_descent;
5011 }
5012
5013 PRODUCE_GLYPHS (it);
5014
5015 if (it->area != TEXT_AREA)
5016 {
5017 set_iterator_to_next (it, 1);
5018 continue;
5019 }
5020
5021 /* The number of glyphs we get back in IT->nglyphs will normally
5022 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
5023 character on a terminal frame, or (iii) a line end. For the
5024 second case, IT->nglyphs - 1 padding glyphs will be present
5025 (on X frames, there is only one glyph produced for a
5026 composite character.
5027
5028 The behavior implemented below means, for continuation lines,
5029 that as many spaces of a TAB as fit on the current line are
5030 displayed there. For terminal frames, as many glyphs of a
5031 multi-glyph character are displayed in the current line, too.
5032 This is what the old redisplay code did, and we keep it that
5033 way. Under X, the whole shape of a complex character must
5034 fit on the line or it will be completely displayed in the
5035 next line.
5036
5037 Note that both for tabs and padding glyphs, all glyphs have
5038 the same width. */
5039 if (it->nglyphs)
5040 {
5041 /* More than one glyph or glyph doesn't fit on line. All
5042 glyphs have the same width. */
5043 int single_glyph_width = it->pixel_width / it->nglyphs;
5044 int new_x;
5045
5046 for (i = 0; i < it->nglyphs; ++i, x = new_x)
5047 {
5048 new_x = x + single_glyph_width;
5049
5050 /* We want to leave anything reaching TO_X to the caller. */
5051 if ((op & MOVE_TO_X) && new_x > to_x)
5052 {
5053 it->current_x = x;
5054 result = MOVE_X_REACHED;
5055 break;
5056 }
5057 else if (/* Lines are continued. */
5058 !it->truncate_lines_p
5059 && (/* And glyph doesn't fit on the line. */
5060 new_x > it->last_visible_x
5061 /* Or it fits exactly and we're on a window
5062 system frame. */
5063 || (new_x == it->last_visible_x
5064 && FRAME_WINDOW_P (it->f))))
5065 {
5066 if (/* IT->hpos == 0 means the very first glyph
5067 doesn't fit on the line, e.g. a wide image. */
5068 it->hpos == 0
5069 || (new_x == it->last_visible_x
5070 && FRAME_WINDOW_P (it->f)))
5071 {
5072 ++it->hpos;
5073 it->current_x = new_x;
5074 if (i == it->nglyphs - 1)
5075 set_iterator_to_next (it, 1);
5076 }
5077 else
5078 {
5079 it->current_x = x;
5080 it->max_ascent = ascent;
5081 it->max_descent = descent;
5082 }
5083
5084 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
5085 IT_CHARPOS (*it)));
5086 result = MOVE_LINE_CONTINUED;
5087 break;
5088 }
5089 else if (new_x > it->first_visible_x)
5090 {
5091 /* Glyph is visible. Increment number of glyphs that
5092 would be displayed. */
5093 ++it->hpos;
5094 }
5095 else
5096 {
5097 /* Glyph is completely off the left margin of the display
5098 area. Nothing to do. */
5099 }
5100 }
5101
5102 if (result != MOVE_UNDEFINED)
5103 break;
5104 }
5105 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
5106 {
5107 /* Stop when TO_X specified and reached. This check is
5108 necessary here because of lines consisting of a line end,
5109 only. The line end will not produce any glyphs and we
5110 would never get MOVE_X_REACHED. */
5111 xassert (it->nglyphs == 0);
5112 result = MOVE_X_REACHED;
5113 break;
5114 }
5115
5116 /* Is this a line end? If yes, we're done. */
5117 if (ITERATOR_AT_END_OF_LINE_P (it))
5118 {
5119 result = MOVE_NEWLINE_OR_CR;
5120 break;
5121 }
5122
5123 /* The current display element has been consumed. Advance
5124 to the next. */
5125 set_iterator_to_next (it, 1);
5126
5127 /* Stop if lines are truncated and IT's current x-position is
5128 past the right edge of the window now. */
5129 if (it->truncate_lines_p
5130 && it->current_x >= it->last_visible_x)
5131 {
5132 result = MOVE_LINE_TRUNCATED;
5133 break;
5134 }
5135 }
5136
5137 /* Restore the iterator settings altered at the beginning of this
5138 function. */
5139 it->glyph_row = saved_glyph_row;
5140 return result;
5141 }
5142
5143
5144 /* Move IT forward to a specified buffer position TO_CHARPOS, TO_X,
5145 TO_Y, TO_VPOS. OP is a bit-mask that specifies where to stop. See
5146 the description of enum move_operation_enum.
5147
5148 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
5149 screen line, this function will set IT to the next position >
5150 TO_CHARPOS. */
5151
5152 void
5153 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
5154 struct it *it;
5155 int to_charpos, to_x, to_y, to_vpos;
5156 int op;
5157 {
5158 enum move_it_result skip, skip2 = MOVE_X_REACHED;
5159 int line_height;
5160 int reached = 0;
5161
5162 for (;;)
5163 {
5164 if (op & MOVE_TO_VPOS)
5165 {
5166 /* If no TO_CHARPOS and no TO_X specified, stop at the
5167 start of the line TO_VPOS. */
5168 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
5169 {
5170 if (it->vpos == to_vpos)
5171 {
5172 reached = 1;
5173 break;
5174 }
5175 else
5176 skip = move_it_in_display_line_to (it, -1, -1, 0);
5177 }
5178 else
5179 {
5180 /* TO_VPOS >= 0 means stop at TO_X in the line at
5181 TO_VPOS, or at TO_POS, whichever comes first. */
5182 if (it->vpos == to_vpos)
5183 {
5184 reached = 2;
5185 break;
5186 }
5187
5188 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
5189
5190 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
5191 {
5192 reached = 3;
5193 break;
5194 }
5195 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
5196 {
5197 /* We have reached TO_X but not in the line we want. */
5198 skip = move_it_in_display_line_to (it, to_charpos,
5199 -1, MOVE_TO_POS);
5200 if (skip == MOVE_POS_MATCH_OR_ZV)
5201 {
5202 reached = 4;
5203 break;
5204 }
5205 }
5206 }
5207 }
5208 else if (op & MOVE_TO_Y)
5209 {
5210 struct it it_backup;
5211
5212 /* TO_Y specified means stop at TO_X in the line containing
5213 TO_Y---or at TO_CHARPOS if this is reached first. The
5214 problem is that we can't really tell whether the line
5215 contains TO_Y before we have completely scanned it, and
5216 this may skip past TO_X. What we do is to first scan to
5217 TO_X.
5218
5219 If TO_X is not specified, use a TO_X of zero. The reason
5220 is to make the outcome of this function more predictable.
5221 If we didn't use TO_X == 0, we would stop at the end of
5222 the line which is probably not what a caller would expect
5223 to happen. */
5224 skip = move_it_in_display_line_to (it, to_charpos,
5225 ((op & MOVE_TO_X)
5226 ? to_x : 0),
5227 (MOVE_TO_X
5228 | (op & MOVE_TO_POS)));
5229
5230 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
5231 if (skip == MOVE_POS_MATCH_OR_ZV)
5232 {
5233 reached = 5;
5234 break;
5235 }
5236
5237 /* If TO_X was reached, we would like to know whether TO_Y
5238 is in the line. This can only be said if we know the
5239 total line height which requires us to scan the rest of
5240 the line. */
5241 if (skip == MOVE_X_REACHED)
5242 {
5243 it_backup = *it;
5244 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
5245 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
5246 op & MOVE_TO_POS);
5247 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
5248 }
5249
5250 /* Now, decide whether TO_Y is in this line. */
5251 line_height = it->max_ascent + it->max_descent;
5252 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
5253
5254 if (to_y >= it->current_y
5255 && to_y < it->current_y + line_height)
5256 {
5257 if (skip == MOVE_X_REACHED)
5258 /* If TO_Y is in this line and TO_X was reached above,
5259 we scanned too far. We have to restore IT's settings
5260 to the ones before skipping. */
5261 *it = it_backup;
5262 reached = 6;
5263 }
5264 else if (skip == MOVE_X_REACHED)
5265 {
5266 skip = skip2;
5267 if (skip == MOVE_POS_MATCH_OR_ZV)
5268 reached = 7;
5269 }
5270
5271 if (reached)
5272 break;
5273 }
5274 else
5275 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
5276
5277 switch (skip)
5278 {
5279 case MOVE_POS_MATCH_OR_ZV:
5280 reached = 8;
5281 goto out;
5282
5283 case MOVE_NEWLINE_OR_CR:
5284 set_iterator_to_next (it, 1);
5285 it->continuation_lines_width = 0;
5286 break;
5287
5288 case MOVE_LINE_TRUNCATED:
5289 it->continuation_lines_width = 0;
5290 reseat_at_next_visible_line_start (it, 0);
5291 if ((op & MOVE_TO_POS) != 0
5292 && IT_CHARPOS (*it) > to_charpos)
5293 {
5294 reached = 9;
5295 goto out;
5296 }
5297 break;
5298
5299 case MOVE_LINE_CONTINUED:
5300 it->continuation_lines_width += it->current_x;
5301 break;
5302
5303 default:
5304 abort ();
5305 }
5306
5307 /* Reset/increment for the next run. */
5308 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
5309 it->current_x = it->hpos = 0;
5310 it->current_y += it->max_ascent + it->max_descent;
5311 ++it->vpos;
5312 last_height = it->max_ascent + it->max_descent;
5313 last_max_ascent = it->max_ascent;
5314 it->max_ascent = it->max_descent = 0;
5315 }
5316
5317 out:
5318
5319 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
5320 }
5321
5322
5323 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
5324
5325 If DY > 0, move IT backward at least that many pixels. DY = 0
5326 means move IT backward to the preceding line start or BEGV. This
5327 function may move over more than DY pixels if IT->current_y - DY
5328 ends up in the middle of a line; in this case IT->current_y will be
5329 set to the top of the line moved to. */
5330
5331 void
5332 move_it_vertically_backward (it, dy)
5333 struct it *it;
5334 int dy;
5335 {
5336 int nlines, h;
5337 struct it it2, it3;
5338 int start_pos = IT_CHARPOS (*it);
5339
5340 xassert (dy >= 0);
5341
5342 /* Estimate how many newlines we must move back. */
5343 nlines = max (1, dy / CANON_Y_UNIT (it->f));
5344
5345 /* Set the iterator's position that many lines back. */
5346 while (nlines-- && IT_CHARPOS (*it) > BEGV)
5347 back_to_previous_visible_line_start (it);
5348
5349 /* Reseat the iterator here. When moving backward, we don't want
5350 reseat to skip forward over invisible text, set up the iterator
5351 to deliver from overlay strings at the new position etc. So,
5352 use reseat_1 here. */
5353 reseat_1 (it, it->current.pos, 1);
5354
5355 /* We are now surely at a line start. */
5356 it->current_x = it->hpos = 0;
5357
5358 /* Move forward and see what y-distance we moved. First move to the
5359 start of the next line so that we get its height. We need this
5360 height to be able to tell whether we reached the specified
5361 y-distance. */
5362 it2 = *it;
5363 it2.max_ascent = it2.max_descent = 0;
5364 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
5365 MOVE_TO_POS | MOVE_TO_VPOS);
5366 xassert (IT_CHARPOS (*it) >= BEGV);
5367 it3 = it2;
5368
5369 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
5370 xassert (IT_CHARPOS (*it) >= BEGV);
5371 h = it2.current_y - it->current_y;
5372 nlines = it2.vpos - it->vpos;
5373
5374 /* Correct IT's y and vpos position. */
5375 it->vpos -= nlines;
5376 it->current_y -= h;
5377
5378 if (dy == 0)
5379 {
5380 /* DY == 0 means move to the start of the screen line. The
5381 value of nlines is > 0 if continuation lines were involved. */
5382 if (nlines > 0)
5383 move_it_by_lines (it, nlines, 1);
5384 xassert (IT_CHARPOS (*it) <= start_pos);
5385 }
5386 else if (nlines)
5387 {
5388 /* The y-position we try to reach. Note that h has been
5389 subtracted in front of the if-statement. */
5390 int target_y = it->current_y + h - dy;
5391 int y0 = it3.current_y;
5392 int y1 = line_bottom_y (&it3);
5393 int line_height = y1 - y0;
5394
5395 /* If we did not reach target_y, try to move further backward if
5396 we can. If we moved too far backward, try to move forward. */
5397 if (target_y < it->current_y
5398 /* This is heuristic. In a window that's 3 lines high, with
5399 a line height of 13 pixels each, recentering with point
5400 on the bottom line will try to move -39/2 = 19 pixels
5401 backward. Try to avoid moving into the first line. */
5402 && it->current_y - target_y > line_height / 3 * 2
5403 && IT_CHARPOS (*it) > BEGV)
5404 {
5405 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
5406 target_y - it->current_y));
5407 move_it_vertically (it, target_y - it->current_y);
5408 xassert (IT_CHARPOS (*it) >= BEGV);
5409 }
5410 else if (target_y >= it->current_y + line_height
5411 && IT_CHARPOS (*it) < ZV)
5412 {
5413 /* Should move forward by at least one line, maybe more. */
5414 do
5415 {
5416 move_it_by_lines (it, 1, 1);
5417 }
5418 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
5419
5420 xassert (IT_CHARPOS (*it) >= BEGV);
5421 }
5422 }
5423 }
5424
5425
5426 /* Move IT by a specified amount of pixel lines DY. DY negative means
5427 move backwards. DY = 0 means move to start of screen line. At the
5428 end, IT will be on the start of a screen line. */
5429
5430 void
5431 move_it_vertically (it, dy)
5432 struct it *it;
5433 int dy;
5434 {
5435 if (dy <= 0)
5436 move_it_vertically_backward (it, -dy);
5437 else if (dy > 0)
5438 {
5439 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
5440 move_it_to (it, ZV, -1, it->current_y + dy, -1,
5441 MOVE_TO_POS | MOVE_TO_Y);
5442 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
5443
5444 /* If buffer ends in ZV without a newline, move to the start of
5445 the line to satisfy the post-condition. */
5446 if (IT_CHARPOS (*it) == ZV
5447 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
5448 move_it_by_lines (it, 0, 0);
5449 }
5450 }
5451
5452
5453 /* Move iterator IT past the end of the text line it is in. */
5454
5455 void
5456 move_it_past_eol (it)
5457 struct it *it;
5458 {
5459 enum move_it_result rc;
5460
5461 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
5462 if (rc == MOVE_NEWLINE_OR_CR)
5463 set_iterator_to_next (it, 0);
5464 }
5465
5466
5467 #if 0 /* Currently not used. */
5468
5469 /* Return non-zero if some text between buffer positions START_CHARPOS
5470 and END_CHARPOS is invisible. IT->window is the window for text
5471 property lookup. */
5472
5473 static int
5474 invisible_text_between_p (it, start_charpos, end_charpos)
5475 struct it *it;
5476 int start_charpos, end_charpos;
5477 {
5478 Lisp_Object prop, limit;
5479 int invisible_found_p;
5480
5481 xassert (it != NULL && start_charpos <= end_charpos);
5482
5483 /* Is text at START invisible? */
5484 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
5485 it->window);
5486 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5487 invisible_found_p = 1;
5488 else
5489 {
5490 limit = Fnext_single_char_property_change (make_number (start_charpos),
5491 Qinvisible, Qnil,
5492 make_number (end_charpos));
5493 invisible_found_p = XFASTINT (limit) < end_charpos;
5494 }
5495
5496 return invisible_found_p;
5497 }
5498
5499 #endif /* 0 */
5500
5501
5502 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
5503 negative means move up. DVPOS == 0 means move to the start of the
5504 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
5505 NEED_Y_P is zero, IT->current_y will be left unchanged.
5506
5507 Further optimization ideas: If we would know that IT->f doesn't use
5508 a face with proportional font, we could be faster for
5509 truncate-lines nil. */
5510
5511 void
5512 move_it_by_lines (it, dvpos, need_y_p)
5513 struct it *it;
5514 int dvpos, need_y_p;
5515 {
5516 struct position pos;
5517
5518 if (!FRAME_WINDOW_P (it->f))
5519 {
5520 struct text_pos textpos;
5521
5522 /* We can use vmotion on frames without proportional fonts. */
5523 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
5524 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
5525 reseat (it, textpos, 1);
5526 it->vpos += pos.vpos;
5527 it->current_y += pos.vpos;
5528 }
5529 else if (dvpos == 0)
5530 {
5531 /* DVPOS == 0 means move to the start of the screen line. */
5532 move_it_vertically_backward (it, 0);
5533 xassert (it->current_x == 0 && it->hpos == 0);
5534 }
5535 else if (dvpos > 0)
5536 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
5537 else
5538 {
5539 struct it it2;
5540 int start_charpos, i;
5541
5542 /* Start at the beginning of the screen line containing IT's
5543 position. */
5544 move_it_vertically_backward (it, 0);
5545
5546 /* Go back -DVPOS visible lines and reseat the iterator there. */
5547 start_charpos = IT_CHARPOS (*it);
5548 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
5549 back_to_previous_visible_line_start (it);
5550 reseat (it, it->current.pos, 1);
5551 it->current_x = it->hpos = 0;
5552
5553 /* Above call may have moved too far if continuation lines
5554 are involved. Scan forward and see if it did. */
5555 it2 = *it;
5556 it2.vpos = it2.current_y = 0;
5557 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
5558 it->vpos -= it2.vpos;
5559 it->current_y -= it2.current_y;
5560 it->current_x = it->hpos = 0;
5561
5562 /* If we moved too far, move IT some lines forward. */
5563 if (it2.vpos > -dvpos)
5564 {
5565 int delta = it2.vpos + dvpos;
5566 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
5567 }
5568 }
5569 }
5570
5571
5572 \f
5573 /***********************************************************************
5574 Messages
5575 ***********************************************************************/
5576
5577
5578 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
5579 to *Messages*. */
5580
5581 void
5582 add_to_log (format, arg1, arg2)
5583 char *format;
5584 Lisp_Object arg1, arg2;
5585 {
5586 Lisp_Object args[3];
5587 Lisp_Object msg, fmt;
5588 char *buffer;
5589 int len;
5590 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5591
5592 /* Do nothing if called asynchronously. Inserting text into
5593 a buffer may call after-change-functions and alike and
5594 that would means running Lisp asynchronously. */
5595 if (handling_signal)
5596 return;
5597
5598 fmt = msg = Qnil;
5599 GCPRO4 (fmt, msg, arg1, arg2);
5600
5601 args[0] = fmt = build_string (format);
5602 args[1] = arg1;
5603 args[2] = arg2;
5604 msg = Fformat (3, args);
5605
5606 len = STRING_BYTES (XSTRING (msg)) + 1;
5607 buffer = (char *) alloca (len);
5608 bcopy (XSTRING (msg)->data, buffer, len);
5609
5610 message_dolog (buffer, len - 1, 1, 0);
5611 UNGCPRO;
5612 }
5613
5614
5615 /* Output a newline in the *Messages* buffer if "needs" one. */
5616
5617 void
5618 message_log_maybe_newline ()
5619 {
5620 if (message_log_need_newline)
5621 message_dolog ("", 0, 1, 0);
5622 }
5623
5624
5625 /* Add a string M of length NBYTES to the message log, optionally
5626 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
5627 nonzero, means interpret the contents of M as multibyte. This
5628 function calls low-level routines in order to bypass text property
5629 hooks, etc. which might not be safe to run. */
5630
5631 void
5632 message_dolog (m, nbytes, nlflag, multibyte)
5633 char *m;
5634 int nbytes, nlflag, multibyte;
5635 {
5636 if (!NILP (Vmessage_log_max))
5637 {
5638 struct buffer *oldbuf;
5639 Lisp_Object oldpoint, oldbegv, oldzv;
5640 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5641 int point_at_end = 0;
5642 int zv_at_end = 0;
5643 Lisp_Object old_deactivate_mark, tem;
5644 struct gcpro gcpro1;
5645
5646 old_deactivate_mark = Vdeactivate_mark;
5647 oldbuf = current_buffer;
5648 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
5649 current_buffer->undo_list = Qt;
5650
5651 oldpoint = message_dolog_marker1;
5652 set_marker_restricted (oldpoint, make_number (PT), Qnil);
5653 oldbegv = message_dolog_marker2;
5654 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
5655 oldzv = message_dolog_marker3;
5656 set_marker_restricted (oldzv, make_number (ZV), Qnil);
5657 GCPRO1 (old_deactivate_mark);
5658
5659 if (PT == Z)
5660 point_at_end = 1;
5661 if (ZV == Z)
5662 zv_at_end = 1;
5663
5664 BEGV = BEG;
5665 BEGV_BYTE = BEG_BYTE;
5666 ZV = Z;
5667 ZV_BYTE = Z_BYTE;
5668 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5669
5670 /* Insert the string--maybe converting multibyte to single byte
5671 or vice versa, so that all the text fits the buffer. */
5672 if (multibyte
5673 && NILP (current_buffer->enable_multibyte_characters))
5674 {
5675 int i, c, char_bytes;
5676 unsigned char work[1];
5677
5678 /* Convert a multibyte string to single-byte
5679 for the *Message* buffer. */
5680 for (i = 0; i < nbytes; i += nbytes)
5681 {
5682 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
5683 work[0] = (SINGLE_BYTE_CHAR_P (c)
5684 ? c
5685 : multibyte_char_to_unibyte (c, Qnil));
5686 insert_1_both (work, 1, 1, 1, 0, 0);
5687 }
5688 }
5689 else if (! multibyte
5690 && ! NILP (current_buffer->enable_multibyte_characters))
5691 {
5692 int i, c, char_bytes;
5693 unsigned char *msg = (unsigned char *) m;
5694 unsigned char str[MAX_MULTIBYTE_LENGTH];
5695 /* Convert a single-byte string to multibyte
5696 for the *Message* buffer. */
5697 for (i = 0; i < nbytes; i++)
5698 {
5699 c = unibyte_char_to_multibyte (msg[i]);
5700 char_bytes = CHAR_STRING (c, str);
5701 insert_1_both (str, 1, char_bytes, 1, 0, 0);
5702 }
5703 }
5704 else if (nbytes)
5705 insert_1 (m, nbytes, 1, 0, 0);
5706
5707 if (nlflag)
5708 {
5709 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
5710 insert_1 ("\n", 1, 1, 0, 0);
5711
5712 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
5713 this_bol = PT;
5714 this_bol_byte = PT_BYTE;
5715
5716 /* See if this line duplicates the previous one.
5717 If so, combine duplicates. */
5718 if (this_bol > BEG)
5719 {
5720 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
5721 prev_bol = PT;
5722 prev_bol_byte = PT_BYTE;
5723
5724 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
5725 this_bol, this_bol_byte);
5726 if (dup)
5727 {
5728 del_range_both (prev_bol, prev_bol_byte,
5729 this_bol, this_bol_byte, 0);
5730 if (dup > 1)
5731 {
5732 char dupstr[40];
5733 int duplen;
5734
5735 /* If you change this format, don't forget to also
5736 change message_log_check_duplicate. */
5737 sprintf (dupstr, " [%d times]", dup);
5738 duplen = strlen (dupstr);
5739 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
5740 insert_1 (dupstr, duplen, 1, 0, 1);
5741 }
5742 }
5743 }
5744
5745 /* If we have more than the desired maximum number of lines
5746 in the *Messages* buffer now, delete the oldest ones.
5747 This is safe because we don't have undo in this buffer. */
5748
5749 if (NATNUMP (Vmessage_log_max))
5750 {
5751 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
5752 -XFASTINT (Vmessage_log_max) - 1, 0);
5753 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
5754 }
5755 }
5756 BEGV = XMARKER (oldbegv)->charpos;
5757 BEGV_BYTE = marker_byte_position (oldbegv);
5758
5759 if (zv_at_end)
5760 {
5761 ZV = Z;
5762 ZV_BYTE = Z_BYTE;
5763 }
5764 else
5765 {
5766 ZV = XMARKER (oldzv)->charpos;
5767 ZV_BYTE = marker_byte_position (oldzv);
5768 }
5769
5770 if (point_at_end)
5771 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5772 else
5773 /* We can't do Fgoto_char (oldpoint) because it will run some
5774 Lisp code. */
5775 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
5776 XMARKER (oldpoint)->bytepos);
5777
5778 UNGCPRO;
5779 unchain_marker (oldpoint);
5780 unchain_marker (oldbegv);
5781 unchain_marker (oldzv);
5782
5783 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
5784 set_buffer_internal (oldbuf);
5785 if (NILP (tem))
5786 windows_or_buffers_changed = old_windows_or_buffers_changed;
5787 message_log_need_newline = !nlflag;
5788 Vdeactivate_mark = old_deactivate_mark;
5789 }
5790 }
5791
5792
5793 /* We are at the end of the buffer after just having inserted a newline.
5794 (Note: We depend on the fact we won't be crossing the gap.)
5795 Check to see if the most recent message looks a lot like the previous one.
5796 Return 0 if different, 1 if the new one should just replace it, or a
5797 value N > 1 if we should also append " [N times]". */
5798
5799 static int
5800 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
5801 int prev_bol, this_bol;
5802 int prev_bol_byte, this_bol_byte;
5803 {
5804 int i;
5805 int len = Z_BYTE - 1 - this_bol_byte;
5806 int seen_dots = 0;
5807 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
5808 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
5809
5810 for (i = 0; i < len; i++)
5811 {
5812 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
5813 seen_dots = 1;
5814 if (p1[i] != p2[i])
5815 return seen_dots;
5816 }
5817 p1 += len;
5818 if (*p1 == '\n')
5819 return 2;
5820 if (*p1++ == ' ' && *p1++ == '[')
5821 {
5822 int n = 0;
5823 while (*p1 >= '0' && *p1 <= '9')
5824 n = n * 10 + *p1++ - '0';
5825 if (strncmp (p1, " times]\n", 8) == 0)
5826 return n+1;
5827 }
5828 return 0;
5829 }
5830
5831
5832 /* Display an echo area message M with a specified length of NBYTES
5833 bytes. The string may include null characters. If M is 0, clear
5834 out any existing message, and let the mini-buffer text show
5835 through.
5836
5837 The buffer M must continue to exist until after the echo area gets
5838 cleared or some other message gets displayed there. This means do
5839 not pass text that is stored in a Lisp string; do not pass text in
5840 a buffer that was alloca'd. */
5841
5842 void
5843 message2 (m, nbytes, multibyte)
5844 char *m;
5845 int nbytes;
5846 int multibyte;
5847 {
5848 /* First flush out any partial line written with print. */
5849 message_log_maybe_newline ();
5850 if (m)
5851 message_dolog (m, nbytes, 1, multibyte);
5852 message2_nolog (m, nbytes, multibyte);
5853 }
5854
5855
5856 /* The non-logging counterpart of message2. */
5857
5858 void
5859 message2_nolog (m, nbytes, multibyte)
5860 char *m;
5861 int nbytes;
5862 {
5863 struct frame *sf = SELECTED_FRAME ();
5864 message_enable_multibyte = multibyte;
5865
5866 if (noninteractive)
5867 {
5868 if (noninteractive_need_newline)
5869 putc ('\n', stderr);
5870 noninteractive_need_newline = 0;
5871 if (m)
5872 fwrite (m, nbytes, 1, stderr);
5873 if (cursor_in_echo_area == 0)
5874 fprintf (stderr, "\n");
5875 fflush (stderr);
5876 }
5877 /* A null message buffer means that the frame hasn't really been
5878 initialized yet. Error messages get reported properly by
5879 cmd_error, so this must be just an informative message; toss it. */
5880 else if (INTERACTIVE
5881 && sf->glyphs_initialized_p
5882 && FRAME_MESSAGE_BUF (sf))
5883 {
5884 Lisp_Object mini_window;
5885 struct frame *f;
5886
5887 /* Get the frame containing the mini-buffer
5888 that the selected frame is using. */
5889 mini_window = FRAME_MINIBUF_WINDOW (sf);
5890 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5891
5892 FRAME_SAMPLE_VISIBILITY (f);
5893 if (FRAME_VISIBLE_P (sf)
5894 && ! FRAME_VISIBLE_P (f))
5895 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
5896
5897 if (m)
5898 {
5899 set_message (m, Qnil, nbytes, multibyte);
5900 if (minibuffer_auto_raise)
5901 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5902 }
5903 else
5904 clear_message (1, 1);
5905
5906 do_pending_window_change (0);
5907 echo_area_display (1);
5908 do_pending_window_change (0);
5909 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5910 (*frame_up_to_date_hook) (f);
5911 }
5912 }
5913
5914
5915 /* Display an echo area message M with a specified length of NBYTES
5916 bytes. The string may include null characters. If M is not a
5917 string, clear out any existing message, and let the mini-buffer
5918 text show through. */
5919
5920 void
5921 message3 (m, nbytes, multibyte)
5922 Lisp_Object m;
5923 int nbytes;
5924 int multibyte;
5925 {
5926 struct gcpro gcpro1;
5927
5928 GCPRO1 (m);
5929
5930 /* First flush out any partial line written with print. */
5931 message_log_maybe_newline ();
5932 if (STRINGP (m))
5933 message_dolog (XSTRING (m)->data, nbytes, 1, multibyte);
5934 message3_nolog (m, nbytes, multibyte);
5935
5936 UNGCPRO;
5937 }
5938
5939
5940 /* The non-logging version of message3. */
5941
5942 void
5943 message3_nolog (m, nbytes, multibyte)
5944 Lisp_Object m;
5945 int nbytes, multibyte;
5946 {
5947 struct frame *sf = SELECTED_FRAME ();
5948 message_enable_multibyte = multibyte;
5949
5950 if (noninteractive)
5951 {
5952 if (noninteractive_need_newline)
5953 putc ('\n', stderr);
5954 noninteractive_need_newline = 0;
5955 if (STRINGP (m))
5956 fwrite (XSTRING (m)->data, nbytes, 1, stderr);
5957 if (cursor_in_echo_area == 0)
5958 fprintf (stderr, "\n");
5959 fflush (stderr);
5960 }
5961 /* A null message buffer means that the frame hasn't really been
5962 initialized yet. Error messages get reported properly by
5963 cmd_error, so this must be just an informative message; toss it. */
5964 else if (INTERACTIVE
5965 && sf->glyphs_initialized_p
5966 && FRAME_MESSAGE_BUF (sf))
5967 {
5968 Lisp_Object mini_window;
5969 Lisp_Object frame;
5970 struct frame *f;
5971
5972 /* Get the frame containing the mini-buffer
5973 that the selected frame is using. */
5974 mini_window = FRAME_MINIBUF_WINDOW (sf);
5975 frame = XWINDOW (mini_window)->frame;
5976 f = XFRAME (frame);
5977
5978 FRAME_SAMPLE_VISIBILITY (f);
5979 if (FRAME_VISIBLE_P (sf)
5980 && !FRAME_VISIBLE_P (f))
5981 Fmake_frame_visible (frame);
5982
5983 if (STRINGP (m) && XSTRING (m)->size)
5984 {
5985 set_message (NULL, m, nbytes, multibyte);
5986 if (minibuffer_auto_raise)
5987 Fraise_frame (frame);
5988 }
5989 else
5990 clear_message (1, 1);
5991
5992 do_pending_window_change (0);
5993 echo_area_display (1);
5994 do_pending_window_change (0);
5995 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5996 (*frame_up_to_date_hook) (f);
5997 }
5998 }
5999
6000
6001 /* Display a null-terminated echo area message M. If M is 0, clear
6002 out any existing message, and let the mini-buffer text show through.
6003
6004 The buffer M must continue to exist until after the echo area gets
6005 cleared or some other message gets displayed there. Do not pass
6006 text that is stored in a Lisp string. Do not pass text in a buffer
6007 that was alloca'd. */
6008
6009 void
6010 message1 (m)
6011 char *m;
6012 {
6013 message2 (m, (m ? strlen (m) : 0), 0);
6014 }
6015
6016
6017 /* The non-logging counterpart of message1. */
6018
6019 void
6020 message1_nolog (m)
6021 char *m;
6022 {
6023 message2_nolog (m, (m ? strlen (m) : 0), 0);
6024 }
6025
6026 /* Display a message M which contains a single %s
6027 which gets replaced with STRING. */
6028
6029 void
6030 message_with_string (m, string, log)
6031 char *m;
6032 Lisp_Object string;
6033 int log;
6034 {
6035 if (noninteractive)
6036 {
6037 if (m)
6038 {
6039 if (noninteractive_need_newline)
6040 putc ('\n', stderr);
6041 noninteractive_need_newline = 0;
6042 fprintf (stderr, m, XSTRING (string)->data);
6043 if (cursor_in_echo_area == 0)
6044 fprintf (stderr, "\n");
6045 fflush (stderr);
6046 }
6047 }
6048 else if (INTERACTIVE)
6049 {
6050 /* The frame whose minibuffer we're going to display the message on.
6051 It may be larger than the selected frame, so we need
6052 to use its buffer, not the selected frame's buffer. */
6053 Lisp_Object mini_window;
6054 struct frame *f, *sf = SELECTED_FRAME ();
6055
6056 /* Get the frame containing the minibuffer
6057 that the selected frame is using. */
6058 mini_window = FRAME_MINIBUF_WINDOW (sf);
6059 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6060
6061 /* A null message buffer means that the frame hasn't really been
6062 initialized yet. Error messages get reported properly by
6063 cmd_error, so this must be just an informative message; toss it. */
6064 if (FRAME_MESSAGE_BUF (f))
6065 {
6066 int len;
6067 char *a[1];
6068 a[0] = (char *) XSTRING (string)->data;
6069
6070 len = doprnt (FRAME_MESSAGE_BUF (f),
6071 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6072
6073 if (log)
6074 message2 (FRAME_MESSAGE_BUF (f), len,
6075 STRING_MULTIBYTE (string));
6076 else
6077 message2_nolog (FRAME_MESSAGE_BUF (f), len,
6078 STRING_MULTIBYTE (string));
6079
6080 /* Print should start at the beginning of the message
6081 buffer next time. */
6082 message_buf_print = 0;
6083 }
6084 }
6085 }
6086
6087
6088 /* Dump an informative message to the minibuf. If M is 0, clear out
6089 any existing message, and let the mini-buffer text show through. */
6090
6091 /* VARARGS 1 */
6092 void
6093 message (m, a1, a2, a3)
6094 char *m;
6095 EMACS_INT a1, a2, a3;
6096 {
6097 if (noninteractive)
6098 {
6099 if (m)
6100 {
6101 if (noninteractive_need_newline)
6102 putc ('\n', stderr);
6103 noninteractive_need_newline = 0;
6104 fprintf (stderr, m, a1, a2, a3);
6105 if (cursor_in_echo_area == 0)
6106 fprintf (stderr, "\n");
6107 fflush (stderr);
6108 }
6109 }
6110 else if (INTERACTIVE)
6111 {
6112 /* The frame whose mini-buffer we're going to display the message
6113 on. It may be larger than the selected frame, so we need to
6114 use its buffer, not the selected frame's buffer. */
6115 Lisp_Object mini_window;
6116 struct frame *f, *sf = SELECTED_FRAME ();
6117
6118 /* Get the frame containing the mini-buffer
6119 that the selected frame is using. */
6120 mini_window = FRAME_MINIBUF_WINDOW (sf);
6121 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6122
6123 /* A null message buffer means that the frame hasn't really been
6124 initialized yet. Error messages get reported properly by
6125 cmd_error, so this must be just an informative message; toss
6126 it. */
6127 if (FRAME_MESSAGE_BUF (f))
6128 {
6129 if (m)
6130 {
6131 int len;
6132 #ifdef NO_ARG_ARRAY
6133 char *a[3];
6134 a[0] = (char *) a1;
6135 a[1] = (char *) a2;
6136 a[2] = (char *) a3;
6137
6138 len = doprnt (FRAME_MESSAGE_BUF (f),
6139 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6140 #else
6141 len = doprnt (FRAME_MESSAGE_BUF (f),
6142 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
6143 (char **) &a1);
6144 #endif /* NO_ARG_ARRAY */
6145
6146 message2 (FRAME_MESSAGE_BUF (f), len, 0);
6147 }
6148 else
6149 message1 (0);
6150
6151 /* Print should start at the beginning of the message
6152 buffer next time. */
6153 message_buf_print = 0;
6154 }
6155 }
6156 }
6157
6158
6159 /* The non-logging version of message. */
6160
6161 void
6162 message_nolog (m, a1, a2, a3)
6163 char *m;
6164 EMACS_INT a1, a2, a3;
6165 {
6166 Lisp_Object old_log_max;
6167 old_log_max = Vmessage_log_max;
6168 Vmessage_log_max = Qnil;
6169 message (m, a1, a2, a3);
6170 Vmessage_log_max = old_log_max;
6171 }
6172
6173
6174 /* Display the current message in the current mini-buffer. This is
6175 only called from error handlers in process.c, and is not time
6176 critical. */
6177
6178 void
6179 update_echo_area ()
6180 {
6181 if (!NILP (echo_area_buffer[0]))
6182 {
6183 Lisp_Object string;
6184 string = Fcurrent_message ();
6185 message3 (string, XSTRING (string)->size,
6186 !NILP (current_buffer->enable_multibyte_characters));
6187 }
6188 }
6189
6190
6191 /* Make sure echo area buffers in echo_buffers[] are life. If they
6192 aren't, make new ones. */
6193
6194 static void
6195 ensure_echo_area_buffers ()
6196 {
6197 int i;
6198
6199 for (i = 0; i < 2; ++i)
6200 if (!BUFFERP (echo_buffer[i])
6201 || NILP (XBUFFER (echo_buffer[i])->name))
6202 {
6203 char name[30];
6204 Lisp_Object old_buffer;
6205 int j;
6206
6207 old_buffer = echo_buffer[i];
6208 sprintf (name, " *Echo Area %d*", i);
6209 echo_buffer[i] = Fget_buffer_create (build_string (name));
6210 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
6211
6212 for (j = 0; j < 2; ++j)
6213 if (EQ (old_buffer, echo_area_buffer[j]))
6214 echo_area_buffer[j] = echo_buffer[i];
6215 }
6216 }
6217
6218
6219 /* Call FN with args A1..A4 with either the current or last displayed
6220 echo_area_buffer as current buffer.
6221
6222 WHICH zero means use the current message buffer
6223 echo_area_buffer[0]. If that is nil, choose a suitable buffer
6224 from echo_buffer[] and clear it.
6225
6226 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
6227 suitable buffer from echo_buffer[] and clear it.
6228
6229 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
6230 that the current message becomes the last displayed one, make
6231 choose a suitable buffer for echo_area_buffer[0], and clear it.
6232
6233 Value is what FN returns. */
6234
6235 static int
6236 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
6237 struct window *w;
6238 int which;
6239 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
6240 EMACS_INT a1;
6241 Lisp_Object a2;
6242 EMACS_INT a3, a4;
6243 {
6244 Lisp_Object buffer;
6245 int this_one, the_other, clear_buffer_p, rc;
6246 int count = BINDING_STACK_SIZE ();
6247
6248 /* If buffers aren't life, make new ones. */
6249 ensure_echo_area_buffers ();
6250
6251 clear_buffer_p = 0;
6252
6253 if (which == 0)
6254 this_one = 0, the_other = 1;
6255 else if (which > 0)
6256 this_one = 1, the_other = 0;
6257 else
6258 {
6259 this_one = 0, the_other = 1;
6260 clear_buffer_p = 1;
6261
6262 /* We need a fresh one in case the current echo buffer equals
6263 the one containing the last displayed echo area message. */
6264 if (!NILP (echo_area_buffer[this_one])
6265 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
6266 echo_area_buffer[this_one] = Qnil;
6267 }
6268
6269 /* Choose a suitable buffer from echo_buffer[] is we don't
6270 have one. */
6271 if (NILP (echo_area_buffer[this_one]))
6272 {
6273 echo_area_buffer[this_one]
6274 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
6275 ? echo_buffer[the_other]
6276 : echo_buffer[this_one]);
6277 clear_buffer_p = 1;
6278 }
6279
6280 buffer = echo_area_buffer[this_one];
6281
6282 /* Don't get confused by reusing the buffer used for echoing
6283 for a different purpose. */
6284 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
6285 cancel_echoing ();
6286
6287 record_unwind_protect (unwind_with_echo_area_buffer,
6288 with_echo_area_buffer_unwind_data (w));
6289
6290 /* Make the echo area buffer current. Note that for display
6291 purposes, it is not necessary that the displayed window's buffer
6292 == current_buffer, except for text property lookup. So, let's
6293 only set that buffer temporarily here without doing a full
6294 Fset_window_buffer. We must also change w->pointm, though,
6295 because otherwise an assertions in unshow_buffer fails, and Emacs
6296 aborts. */
6297 set_buffer_internal_1 (XBUFFER (buffer));
6298 if (w)
6299 {
6300 w->buffer = buffer;
6301 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
6302 }
6303
6304 current_buffer->undo_list = Qt;
6305 current_buffer->read_only = Qnil;
6306 specbind (Qinhibit_read_only, Qt);
6307 specbind (Qinhibit_modification_hooks, Qt);
6308
6309 if (clear_buffer_p && Z > BEG)
6310 del_range (BEG, Z);
6311
6312 xassert (BEGV >= BEG);
6313 xassert (ZV <= Z && ZV >= BEGV);
6314
6315 rc = fn (a1, a2, a3, a4);
6316
6317 xassert (BEGV >= BEG);
6318 xassert (ZV <= Z && ZV >= BEGV);
6319
6320 unbind_to (count, Qnil);
6321 return rc;
6322 }
6323
6324
6325 /* Save state that should be preserved around the call to the function
6326 FN called in with_echo_area_buffer. */
6327
6328 static Lisp_Object
6329 with_echo_area_buffer_unwind_data (w)
6330 struct window *w;
6331 {
6332 int i = 0;
6333 Lisp_Object vector;
6334
6335 /* Reduce consing by keeping one vector in
6336 Vwith_echo_area_save_vector. */
6337 vector = Vwith_echo_area_save_vector;
6338 Vwith_echo_area_save_vector = Qnil;
6339
6340 if (NILP (vector))
6341 vector = Fmake_vector (make_number (7), Qnil);
6342
6343 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
6344 AREF (vector, i) = Vdeactivate_mark, ++i;
6345 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
6346
6347 if (w)
6348 {
6349 XSETWINDOW (AREF (vector, i), w); ++i;
6350 AREF (vector, i) = w->buffer; ++i;
6351 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
6352 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
6353 }
6354 else
6355 {
6356 int end = i + 4;
6357 for (; i < end; ++i)
6358 AREF (vector, i) = Qnil;
6359 }
6360
6361 xassert (i == ASIZE (vector));
6362 return vector;
6363 }
6364
6365
6366 /* Restore global state from VECTOR which was created by
6367 with_echo_area_buffer_unwind_data. */
6368
6369 static Lisp_Object
6370 unwind_with_echo_area_buffer (vector)
6371 Lisp_Object vector;
6372 {
6373 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
6374 Vdeactivate_mark = AREF (vector, 1);
6375 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
6376
6377 if (WINDOWP (AREF (vector, 3)))
6378 {
6379 struct window *w;
6380 Lisp_Object buffer, charpos, bytepos;
6381
6382 w = XWINDOW (AREF (vector, 3));
6383 buffer = AREF (vector, 4);
6384 charpos = AREF (vector, 5);
6385 bytepos = AREF (vector, 6);
6386
6387 w->buffer = buffer;
6388 set_marker_both (w->pointm, buffer,
6389 XFASTINT (charpos), XFASTINT (bytepos));
6390 }
6391
6392 Vwith_echo_area_save_vector = vector;
6393 return Qnil;
6394 }
6395
6396
6397 /* Set up the echo area for use by print functions. MULTIBYTE_P
6398 non-zero means we will print multibyte. */
6399
6400 void
6401 setup_echo_area_for_printing (multibyte_p)
6402 int multibyte_p;
6403 {
6404 ensure_echo_area_buffers ();
6405
6406 if (!message_buf_print)
6407 {
6408 /* A message has been output since the last time we printed.
6409 Choose a fresh echo area buffer. */
6410 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6411 echo_area_buffer[0] = echo_buffer[1];
6412 else
6413 echo_area_buffer[0] = echo_buffer[0];
6414
6415 /* Switch to that buffer and clear it. */
6416 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6417 current_buffer->truncate_lines = Qnil;
6418
6419 if (Z > BEG)
6420 {
6421 int count = BINDING_STACK_SIZE ();
6422 specbind (Qinhibit_read_only, Qt);
6423 del_range (BEG, Z);
6424 unbind_to (count, Qnil);
6425 }
6426 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6427
6428 /* Set up the buffer for the multibyteness we need. */
6429 if (multibyte_p
6430 != !NILP (current_buffer->enable_multibyte_characters))
6431 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
6432
6433 /* Raise the frame containing the echo area. */
6434 if (minibuffer_auto_raise)
6435 {
6436 struct frame *sf = SELECTED_FRAME ();
6437 Lisp_Object mini_window;
6438 mini_window = FRAME_MINIBUF_WINDOW (sf);
6439 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6440 }
6441
6442 message_log_maybe_newline ();
6443 message_buf_print = 1;
6444 }
6445 else
6446 {
6447 if (NILP (echo_area_buffer[0]))
6448 {
6449 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6450 echo_area_buffer[0] = echo_buffer[1];
6451 else
6452 echo_area_buffer[0] = echo_buffer[0];
6453 }
6454
6455 if (current_buffer != XBUFFER (echo_area_buffer[0]))
6456 {
6457 /* Someone switched buffers between print requests. */
6458 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6459 current_buffer->truncate_lines = Qnil;
6460 }
6461 }
6462 }
6463
6464
6465 /* Display an echo area message in window W. Value is non-zero if W's
6466 height is changed. If display_last_displayed_message_p is
6467 non-zero, display the message that was last displayed, otherwise
6468 display the current message. */
6469
6470 static int
6471 display_echo_area (w)
6472 struct window *w;
6473 {
6474 int i, no_message_p, window_height_changed_p, count;
6475
6476 /* Temporarily disable garbage collections while displaying the echo
6477 area. This is done because a GC can print a message itself.
6478 That message would modify the echo area buffer's contents while a
6479 redisplay of the buffer is going on, and seriously confuse
6480 redisplay. */
6481 count = inhibit_garbage_collection ();
6482
6483 /* If there is no message, we must call display_echo_area_1
6484 nevertheless because it resizes the window. But we will have to
6485 reset the echo_area_buffer in question to nil at the end because
6486 with_echo_area_buffer will sets it to an empty buffer. */
6487 i = display_last_displayed_message_p ? 1 : 0;
6488 no_message_p = NILP (echo_area_buffer[i]);
6489
6490 window_height_changed_p
6491 = with_echo_area_buffer (w, display_last_displayed_message_p,
6492 display_echo_area_1,
6493 (EMACS_INT) w, Qnil, 0, 0);
6494
6495 if (no_message_p)
6496 echo_area_buffer[i] = Qnil;
6497
6498 unbind_to (count, Qnil);
6499 return window_height_changed_p;
6500 }
6501
6502
6503 /* Helper for display_echo_area. Display the current buffer which
6504 contains the current echo area message in window W, a mini-window,
6505 a pointer to which is passed in A1. A2..A4 are currently not used.
6506 Change the height of W so that all of the message is displayed.
6507 Value is non-zero if height of W was changed. */
6508
6509 static int
6510 display_echo_area_1 (a1, a2, a3, a4)
6511 EMACS_INT a1;
6512 Lisp_Object a2;
6513 EMACS_INT a3, a4;
6514 {
6515 struct window *w = (struct window *) a1;
6516 Lisp_Object window;
6517 struct text_pos start;
6518 int window_height_changed_p = 0;
6519
6520 /* Do this before displaying, so that we have a large enough glyph
6521 matrix for the display. */
6522 window_height_changed_p = resize_mini_window (w, 0);
6523
6524 /* Display. */
6525 clear_glyph_matrix (w->desired_matrix);
6526 XSETWINDOW (window, w);
6527 SET_TEXT_POS (start, BEG, BEG_BYTE);
6528 try_window (window, start);
6529
6530 return window_height_changed_p;
6531 }
6532
6533
6534 /* Resize the echo area window to exactly the size needed for the
6535 currently displayed message, if there is one. If a mini-buffer
6536 is active, don't shrink it. */
6537
6538 void
6539 resize_echo_area_exactly ()
6540 {
6541 if (BUFFERP (echo_area_buffer[0])
6542 && WINDOWP (echo_area_window))
6543 {
6544 struct window *w = XWINDOW (echo_area_window);
6545 int resized_p;
6546 Lisp_Object resize_exactly;
6547
6548 if (minibuf_level == 0)
6549 resize_exactly = Qt;
6550 else
6551 resize_exactly = Qnil;
6552
6553 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
6554 (EMACS_INT) w, resize_exactly, 0, 0);
6555 if (resized_p)
6556 {
6557 ++windows_or_buffers_changed;
6558 ++update_mode_lines;
6559 redisplay_internal (0);
6560 }
6561 }
6562 }
6563
6564
6565 /* Callback function for with_echo_area_buffer, when used from
6566 resize_echo_area_exactly. A1 contains a pointer to the window to
6567 resize, EXACTLY non-nil means resize the mini-window exactly to the
6568 size of the text displayed. A3 and A4 are not used. Value is what
6569 resize_mini_window returns. */
6570
6571 static int
6572 resize_mini_window_1 (a1, exactly, a3, a4)
6573 EMACS_INT a1;
6574 Lisp_Object exactly;
6575 EMACS_INT a3, a4;
6576 {
6577 return resize_mini_window ((struct window *) a1, !NILP (exactly));
6578 }
6579
6580
6581 /* Resize mini-window W to fit the size of its contents. EXACT:P
6582 means size the window exactly to the size needed. Otherwise, it's
6583 only enlarged until W's buffer is empty. Value is non-zero if
6584 the window height has been changed. */
6585
6586 int
6587 resize_mini_window (w, exact_p)
6588 struct window *w;
6589 int exact_p;
6590 {
6591 struct frame *f = XFRAME (w->frame);
6592 int window_height_changed_p = 0;
6593
6594 xassert (MINI_WINDOW_P (w));
6595
6596 /* Don't resize windows while redisplaying a window; it would
6597 confuse redisplay functions when the size of the window they are
6598 displaying changes from under them. Such a resizing can happen,
6599 for instance, when which-func prints a long message while
6600 we are running fontification-functions. We're running these
6601 functions with safe_call which binds inhibit-redisplay to t. */
6602 if (!NILP (Vinhibit_redisplay))
6603 return 0;
6604
6605 /* Nil means don't try to resize. */
6606 if (NILP (Vresize_mini_windows)
6607 || (FRAME_X_P (f) && f->output_data.x == NULL))
6608 return 0;
6609
6610 if (!FRAME_MINIBUF_ONLY_P (f))
6611 {
6612 struct it it;
6613 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
6614 int total_height = XFASTINT (root->height) + XFASTINT (w->height);
6615 int height, max_height;
6616 int unit = CANON_Y_UNIT (f);
6617 struct text_pos start;
6618 struct buffer *old_current_buffer = NULL;
6619
6620 if (current_buffer != XBUFFER (w->buffer))
6621 {
6622 old_current_buffer = current_buffer;
6623 set_buffer_internal (XBUFFER (w->buffer));
6624 }
6625
6626 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
6627
6628 /* Compute the max. number of lines specified by the user. */
6629 if (FLOATP (Vmax_mini_window_height))
6630 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_HEIGHT (f);
6631 else if (INTEGERP (Vmax_mini_window_height))
6632 max_height = XINT (Vmax_mini_window_height);
6633 else
6634 max_height = total_height / 4;
6635
6636 /* Correct that max. height if it's bogus. */
6637 max_height = max (1, max_height);
6638 max_height = min (total_height, max_height);
6639
6640 /* Find out the height of the text in the window. */
6641 if (it.truncate_lines_p)
6642 height = 1;
6643 else
6644 {
6645 last_height = 0;
6646 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
6647 if (it.max_ascent == 0 && it.max_descent == 0)
6648 height = it.current_y + last_height;
6649 else
6650 height = it.current_y + it.max_ascent + it.max_descent;
6651 height -= it.extra_line_spacing;
6652 height = (height + unit - 1) / unit;
6653 }
6654
6655 /* Compute a suitable window start. */
6656 if (height > max_height)
6657 {
6658 height = max_height;
6659 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
6660 move_it_vertically_backward (&it, (height - 1) * unit);
6661 start = it.current.pos;
6662 }
6663 else
6664 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
6665 SET_MARKER_FROM_TEXT_POS (w->start, start);
6666
6667 if (EQ (Vresize_mini_windows, Qgrow_only))
6668 {
6669 /* Let it grow only, until we display an empty message, in which
6670 case the window shrinks again. */
6671 if (height > XFASTINT (w->height))
6672 {
6673 int old_height = XFASTINT (w->height);
6674 freeze_window_starts (f, 1);
6675 grow_mini_window (w, height - XFASTINT (w->height));
6676 window_height_changed_p = XFASTINT (w->height) != old_height;
6677 }
6678 else if (height < XFASTINT (w->height)
6679 && (exact_p || BEGV == ZV))
6680 {
6681 int old_height = XFASTINT (w->height);
6682 freeze_window_starts (f, 0);
6683 shrink_mini_window (w);
6684 window_height_changed_p = XFASTINT (w->height) != old_height;
6685 }
6686 }
6687 else
6688 {
6689 /* Always resize to exact size needed. */
6690 if (height > XFASTINT (w->height))
6691 {
6692 int old_height = XFASTINT (w->height);
6693 freeze_window_starts (f, 1);
6694 grow_mini_window (w, height - XFASTINT (w->height));
6695 window_height_changed_p = XFASTINT (w->height) != old_height;
6696 }
6697 else if (height < XFASTINT (w->height))
6698 {
6699 int old_height = XFASTINT (w->height);
6700 freeze_window_starts (f, 0);
6701 shrink_mini_window (w);
6702
6703 if (height)
6704 {
6705 freeze_window_starts (f, 1);
6706 grow_mini_window (w, height - XFASTINT (w->height));
6707 }
6708
6709 window_height_changed_p = XFASTINT (w->height) != old_height;
6710 }
6711 }
6712
6713 if (old_current_buffer)
6714 set_buffer_internal (old_current_buffer);
6715 }
6716
6717 return window_height_changed_p;
6718 }
6719
6720
6721 /* Value is the current message, a string, or nil if there is no
6722 current message. */
6723
6724 Lisp_Object
6725 current_message ()
6726 {
6727 Lisp_Object msg;
6728
6729 if (NILP (echo_area_buffer[0]))
6730 msg = Qnil;
6731 else
6732 {
6733 with_echo_area_buffer (0, 0, current_message_1,
6734 (EMACS_INT) &msg, Qnil, 0, 0);
6735 if (NILP (msg))
6736 echo_area_buffer[0] = Qnil;
6737 }
6738
6739 return msg;
6740 }
6741
6742
6743 static int
6744 current_message_1 (a1, a2, a3, a4)
6745 EMACS_INT a1;
6746 Lisp_Object a2;
6747 EMACS_INT a3, a4;
6748 {
6749 Lisp_Object *msg = (Lisp_Object *) a1;
6750
6751 if (Z > BEG)
6752 *msg = make_buffer_string (BEG, Z, 1);
6753 else
6754 *msg = Qnil;
6755 return 0;
6756 }
6757
6758
6759 /* Push the current message on Vmessage_stack for later restauration
6760 by restore_message. Value is non-zero if the current message isn't
6761 empty. This is a relatively infrequent operation, so it's not
6762 worth optimizing. */
6763
6764 int
6765 push_message ()
6766 {
6767 Lisp_Object msg;
6768 msg = current_message ();
6769 Vmessage_stack = Fcons (msg, Vmessage_stack);
6770 return STRINGP (msg);
6771 }
6772
6773
6774 /* Handler for record_unwind_protect calling pop_message. */
6775
6776 Lisp_Object
6777 push_message_unwind (dummy)
6778 Lisp_Object dummy;
6779 {
6780 pop_message ();
6781 return Qnil;
6782 }
6783
6784
6785 /* Restore message display from the top of Vmessage_stack. */
6786
6787 void
6788 restore_message ()
6789 {
6790 Lisp_Object msg;
6791
6792 xassert (CONSP (Vmessage_stack));
6793 msg = XCAR (Vmessage_stack);
6794 if (STRINGP (msg))
6795 message3_nolog (msg, STRING_BYTES (XSTRING (msg)), STRING_MULTIBYTE (msg));
6796 else
6797 message3_nolog (msg, 0, 0);
6798 }
6799
6800
6801 /* Pop the top-most entry off Vmessage_stack. */
6802
6803 void
6804 pop_message ()
6805 {
6806 xassert (CONSP (Vmessage_stack));
6807 Vmessage_stack = XCDR (Vmessage_stack);
6808 }
6809
6810
6811 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
6812 exits. If the stack is not empty, we have a missing pop_message
6813 somewhere. */
6814
6815 void
6816 check_message_stack ()
6817 {
6818 if (!NILP (Vmessage_stack))
6819 abort ();
6820 }
6821
6822
6823 /* Truncate to NCHARS what will be displayed in the echo area the next
6824 time we display it---but don't redisplay it now. */
6825
6826 void
6827 truncate_echo_area (nchars)
6828 int nchars;
6829 {
6830 if (nchars == 0)
6831 echo_area_buffer[0] = Qnil;
6832 /* A null message buffer means that the frame hasn't really been
6833 initialized yet. Error messages get reported properly by
6834 cmd_error, so this must be just an informative message; toss it. */
6835 else if (!noninteractive
6836 && INTERACTIVE
6837 && !NILP (echo_area_buffer[0]))
6838 {
6839 struct frame *sf = SELECTED_FRAME ();
6840 if (FRAME_MESSAGE_BUF (sf))
6841 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
6842 }
6843 }
6844
6845
6846 /* Helper function for truncate_echo_area. Truncate the current
6847 message to at most NCHARS characters. */
6848
6849 static int
6850 truncate_message_1 (nchars, a2, a3, a4)
6851 EMACS_INT nchars;
6852 Lisp_Object a2;
6853 EMACS_INT a3, a4;
6854 {
6855 if (BEG + nchars < Z)
6856 del_range (BEG + nchars, Z);
6857 if (Z == BEG)
6858 echo_area_buffer[0] = Qnil;
6859 return 0;
6860 }
6861
6862
6863 /* Set the current message to a substring of S or STRING.
6864
6865 If STRING is a Lisp string, set the message to the first NBYTES
6866 bytes from STRING. NBYTES zero means use the whole string. If
6867 STRING is multibyte, the message will be displayed multibyte.
6868
6869 If S is not null, set the message to the first LEN bytes of S. LEN
6870 zero means use the whole string. MULTIBYTE_P non-zero means S is
6871 multibyte. Display the message multibyte in that case. */
6872
6873 void
6874 set_message (s, string, nbytes, multibyte_p)
6875 char *s;
6876 Lisp_Object string;
6877 int nbytes;
6878 {
6879 message_enable_multibyte
6880 = ((s && multibyte_p)
6881 || (STRINGP (string) && STRING_MULTIBYTE (string)));
6882
6883 with_echo_area_buffer (0, -1, set_message_1,
6884 (EMACS_INT) s, string, nbytes, multibyte_p);
6885 message_buf_print = 0;
6886 help_echo_showing_p = 0;
6887 }
6888
6889
6890 /* Helper function for set_message. Arguments have the same meaning
6891 as there, with A1 corresponding to S and A2 corresponding to STRING
6892 This function is called with the echo area buffer being
6893 current. */
6894
6895 static int
6896 set_message_1 (a1, a2, nbytes, multibyte_p)
6897 EMACS_INT a1;
6898 Lisp_Object a2;
6899 EMACS_INT nbytes, multibyte_p;
6900 {
6901 char *s = (char *) a1;
6902 Lisp_Object string = a2;
6903
6904 xassert (BEG == Z);
6905
6906 /* Change multibyteness of the echo buffer appropriately. */
6907 if (message_enable_multibyte
6908 != !NILP (current_buffer->enable_multibyte_characters))
6909 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
6910
6911 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
6912
6913 /* Insert new message at BEG. */
6914 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6915
6916 if (STRINGP (string))
6917 {
6918 int nchars;
6919
6920 if (nbytes == 0)
6921 nbytes = XSTRING (string)->size_byte;
6922 nchars = string_byte_to_char (string, nbytes);
6923
6924 /* This function takes care of single/multibyte conversion. We
6925 just have to ensure that the echo area buffer has the right
6926 setting of enable_multibyte_characters. */
6927 insert_from_string (string, 0, 0, nchars, nbytes, 1);
6928 }
6929 else if (s)
6930 {
6931 if (nbytes == 0)
6932 nbytes = strlen (s);
6933
6934 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
6935 {
6936 /* Convert from multi-byte to single-byte. */
6937 int i, c, n;
6938 unsigned char work[1];
6939
6940 /* Convert a multibyte string to single-byte. */
6941 for (i = 0; i < nbytes; i += n)
6942 {
6943 c = string_char_and_length (s + i, nbytes - i, &n);
6944 work[0] = (SINGLE_BYTE_CHAR_P (c)
6945 ? c
6946 : multibyte_char_to_unibyte (c, Qnil));
6947 insert_1_both (work, 1, 1, 1, 0, 0);
6948 }
6949 }
6950 else if (!multibyte_p
6951 && !NILP (current_buffer->enable_multibyte_characters))
6952 {
6953 /* Convert from single-byte to multi-byte. */
6954 int i, c, n;
6955 unsigned char *msg = (unsigned char *) s;
6956 unsigned char str[MAX_MULTIBYTE_LENGTH];
6957
6958 /* Convert a single-byte string to multibyte. */
6959 for (i = 0; i < nbytes; i++)
6960 {
6961 c = unibyte_char_to_multibyte (msg[i]);
6962 n = CHAR_STRING (c, str);
6963 insert_1_both (str, 1, n, 1, 0, 0);
6964 }
6965 }
6966 else
6967 insert_1 (s, nbytes, 1, 0, 0);
6968 }
6969
6970 return 0;
6971 }
6972
6973
6974 /* Clear messages. CURRENT_P non-zero means clear the current
6975 message. LAST_DISPLAYED_P non-zero means clear the message
6976 last displayed. */
6977
6978 void
6979 clear_message (current_p, last_displayed_p)
6980 int current_p, last_displayed_p;
6981 {
6982 if (current_p)
6983 {
6984 echo_area_buffer[0] = Qnil;
6985 message_cleared_p = 1;
6986 }
6987
6988 if (last_displayed_p)
6989 echo_area_buffer[1] = Qnil;
6990
6991 message_buf_print = 0;
6992 }
6993
6994 /* Clear garbaged frames.
6995
6996 This function is used where the old redisplay called
6997 redraw_garbaged_frames which in turn called redraw_frame which in
6998 turn called clear_frame. The call to clear_frame was a source of
6999 flickering. I believe a clear_frame is not necessary. It should
7000 suffice in the new redisplay to invalidate all current matrices,
7001 and ensure a complete redisplay of all windows. */
7002
7003 static void
7004 clear_garbaged_frames ()
7005 {
7006 if (frame_garbaged)
7007 {
7008 Lisp_Object tail, frame;
7009
7010 FOR_EACH_FRAME (tail, frame)
7011 {
7012 struct frame *f = XFRAME (frame);
7013
7014 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
7015 {
7016 if (f->resized_p)
7017 Fredraw_frame (frame);
7018 clear_current_matrices (f);
7019 f->garbaged = 0;
7020 f->resized_p = 0;
7021 }
7022 }
7023
7024 frame_garbaged = 0;
7025 ++windows_or_buffers_changed;
7026 }
7027 }
7028
7029
7030 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
7031 is non-zero update selected_frame. Value is non-zero if the
7032 mini-windows height has been changed. */
7033
7034 static int
7035 echo_area_display (update_frame_p)
7036 int update_frame_p;
7037 {
7038 Lisp_Object mini_window;
7039 struct window *w;
7040 struct frame *f;
7041 int window_height_changed_p = 0;
7042 struct frame *sf = SELECTED_FRAME ();
7043
7044 mini_window = FRAME_MINIBUF_WINDOW (sf);
7045 w = XWINDOW (mini_window);
7046 f = XFRAME (WINDOW_FRAME (w));
7047
7048 /* Don't display if frame is invisible or not yet initialized. */
7049 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
7050 return 0;
7051
7052 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
7053 #ifndef macintosh
7054 #ifdef HAVE_WINDOW_SYSTEM
7055 /* When Emacs starts, selected_frame may be a visible terminal
7056 frame, even if we run under a window system. If we let this
7057 through, a message would be displayed on the terminal. */
7058 if (EQ (selected_frame, Vterminal_frame)
7059 && !NILP (Vwindow_system))
7060 return 0;
7061 #endif /* HAVE_WINDOW_SYSTEM */
7062 #endif
7063
7064 /* Redraw garbaged frames. */
7065 if (frame_garbaged)
7066 clear_garbaged_frames ();
7067
7068 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
7069 {
7070 echo_area_window = mini_window;
7071 window_height_changed_p = display_echo_area (w);
7072 w->must_be_updated_p = 1;
7073
7074 /* Update the display, unless called from redisplay_internal.
7075 Also don't update the screen during redisplay itself. The
7076 update will happen at the end of redisplay, and an update
7077 here could cause confusion. */
7078 if (update_frame_p && !redisplaying_p)
7079 {
7080 int n = 0;
7081
7082 /* If the display update has been interrupted by pending
7083 input, update mode lines in the frame. Due to the
7084 pending input, it might have been that redisplay hasn't
7085 been called, so that mode lines above the echo area are
7086 garbaged. This looks odd, so we prevent it here. */
7087 if (!display_completed)
7088 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
7089
7090 if (window_height_changed_p
7091 /* Don't do this if Emacs is shutting down. Redisplay
7092 needs to run hooks. */
7093 && !NILP (Vrun_hooks))
7094 {
7095 /* Must update other windows. Likewise as in other
7096 cases, don't let this update be interrupted by
7097 pending input. */
7098 int count = BINDING_STACK_SIZE ();
7099 specbind (Qredisplay_dont_pause, Qt);
7100 windows_or_buffers_changed = 1;
7101 redisplay_internal (0);
7102 unbind_to (count, Qnil);
7103 }
7104 else if (FRAME_WINDOW_P (f) && n == 0)
7105 {
7106 /* Window configuration is the same as before.
7107 Can do with a display update of the echo area,
7108 unless we displayed some mode lines. */
7109 update_single_window (w, 1);
7110 rif->flush_display (f);
7111 }
7112 else
7113 update_frame (f, 1, 1);
7114
7115 /* If cursor is in the echo area, make sure that the next
7116 redisplay displays the minibuffer, so that the cursor will
7117 be replaced with what the minibuffer wants. */
7118 if (cursor_in_echo_area)
7119 ++windows_or_buffers_changed;
7120 }
7121 }
7122 else if (!EQ (mini_window, selected_window))
7123 windows_or_buffers_changed++;
7124
7125 /* Last displayed message is now the current message. */
7126 echo_area_buffer[1] = echo_area_buffer[0];
7127
7128 /* Prevent redisplay optimization in redisplay_internal by resetting
7129 this_line_start_pos. This is done because the mini-buffer now
7130 displays the message instead of its buffer text. */
7131 if (EQ (mini_window, selected_window))
7132 CHARPOS (this_line_start_pos) = 0;
7133
7134 return window_height_changed_p;
7135 }
7136
7137
7138 \f
7139 /***********************************************************************
7140 Frame Titles
7141 ***********************************************************************/
7142
7143
7144 #ifdef HAVE_WINDOW_SYSTEM
7145
7146 /* A buffer for constructing frame titles in it; allocated from the
7147 heap in init_xdisp and resized as needed in store_frame_title_char. */
7148
7149 static char *frame_title_buf;
7150
7151 /* The buffer's end, and a current output position in it. */
7152
7153 static char *frame_title_buf_end;
7154 static char *frame_title_ptr;
7155
7156
7157 /* Store a single character C for the frame title in frame_title_buf.
7158 Re-allocate frame_title_buf if necessary. */
7159
7160 static void
7161 store_frame_title_char (c)
7162 char c;
7163 {
7164 /* If output position has reached the end of the allocated buffer,
7165 double the buffer's size. */
7166 if (frame_title_ptr == frame_title_buf_end)
7167 {
7168 int len = frame_title_ptr - frame_title_buf;
7169 int new_size = 2 * len * sizeof *frame_title_buf;
7170 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
7171 frame_title_buf_end = frame_title_buf + new_size;
7172 frame_title_ptr = frame_title_buf + len;
7173 }
7174
7175 *frame_title_ptr++ = c;
7176 }
7177
7178
7179 /* Store part of a frame title in frame_title_buf, beginning at
7180 frame_title_ptr. STR is the string to store. Do not copy
7181 characters that yield more columns than PRECISION; PRECISION <= 0
7182 means copy the whole string. Pad with spaces until FIELD_WIDTH
7183 number of characters have been copied; FIELD_WIDTH <= 0 means don't
7184 pad. Called from display_mode_element when it is used to build a
7185 frame title. */
7186
7187 static int
7188 store_frame_title (str, field_width, precision)
7189 unsigned char *str;
7190 int field_width, precision;
7191 {
7192 int n = 0;
7193 int dummy, nbytes;
7194
7195 /* Copy at most PRECISION chars from STR. */
7196 nbytes = strlen (str);
7197 n+= c_string_width (str, nbytes, precision, &dummy, &nbytes);
7198 while (nbytes--)
7199 store_frame_title_char (*str++);
7200
7201 /* Fill up with spaces until FIELD_WIDTH reached. */
7202 while (field_width > 0
7203 && n < field_width)
7204 {
7205 store_frame_title_char (' ');
7206 ++n;
7207 }
7208
7209 return n;
7210 }
7211
7212
7213 /* Set the title of FRAME, if it has changed. The title format is
7214 Vicon_title_format if FRAME is iconified, otherwise it is
7215 frame_title_format. */
7216
7217 static void
7218 x_consider_frame_title (frame)
7219 Lisp_Object frame;
7220 {
7221 struct frame *f = XFRAME (frame);
7222
7223 if (FRAME_WINDOW_P (f)
7224 || FRAME_MINIBUF_ONLY_P (f)
7225 || f->explicit_name)
7226 {
7227 /* Do we have more than one visible frame on this X display? */
7228 Lisp_Object tail;
7229 Lisp_Object fmt;
7230 struct buffer *obuf;
7231 int len;
7232 struct it it;
7233
7234 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
7235 {
7236 Lisp_Object other_frame = XCAR (tail);
7237 struct frame *tf = XFRAME (other_frame);
7238
7239 if (tf != f
7240 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
7241 && !FRAME_MINIBUF_ONLY_P (tf)
7242 && !EQ (other_frame, tip_frame)
7243 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
7244 break;
7245 }
7246
7247 /* Set global variable indicating that multiple frames exist. */
7248 multiple_frames = CONSP (tail);
7249
7250 /* Switch to the buffer of selected window of the frame. Set up
7251 frame_title_ptr so that display_mode_element will output into it;
7252 then display the title. */
7253 obuf = current_buffer;
7254 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
7255 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
7256 frame_title_ptr = frame_title_buf;
7257 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
7258 NULL, DEFAULT_FACE_ID);
7259 display_mode_element (&it, 0, -1, -1, fmt, Qnil);
7260 len = frame_title_ptr - frame_title_buf;
7261 frame_title_ptr = NULL;
7262 set_buffer_internal_1 (obuf);
7263
7264 /* Set the title only if it's changed. This avoids consing in
7265 the common case where it hasn't. (If it turns out that we've
7266 already wasted too much time by walking through the list with
7267 display_mode_element, then we might need to optimize at a
7268 higher level than this.) */
7269 if (! STRINGP (f->name)
7270 || STRING_BYTES (XSTRING (f->name)) != len
7271 || bcmp (frame_title_buf, XSTRING (f->name)->data, len) != 0)
7272 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
7273 }
7274 }
7275
7276 #else /* not HAVE_WINDOW_SYSTEM */
7277
7278 #define frame_title_ptr ((char *)0)
7279 #define store_frame_title(str, mincol, maxcol) 0
7280
7281 #endif /* not HAVE_WINDOW_SYSTEM */
7282
7283
7284
7285 \f
7286 /***********************************************************************
7287 Menu Bars
7288 ***********************************************************************/
7289
7290
7291 /* Prepare for redisplay by updating menu-bar item lists when
7292 appropriate. This can call eval. */
7293
7294 void
7295 prepare_menu_bars ()
7296 {
7297 int all_windows;
7298 struct gcpro gcpro1, gcpro2;
7299 struct frame *f;
7300 Lisp_Object tooltip_frame;
7301
7302 #ifdef HAVE_WINDOW_SYSTEM
7303 tooltip_frame = tip_frame;
7304 #else
7305 tooltip_frame = Qnil;
7306 #endif
7307
7308 /* Update all frame titles based on their buffer names, etc. We do
7309 this before the menu bars so that the buffer-menu will show the
7310 up-to-date frame titles. */
7311 #ifdef HAVE_WINDOW_SYSTEM
7312 if (windows_or_buffers_changed || update_mode_lines)
7313 {
7314 Lisp_Object tail, frame;
7315
7316 FOR_EACH_FRAME (tail, frame)
7317 {
7318 f = XFRAME (frame);
7319 if (!EQ (frame, tooltip_frame)
7320 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
7321 x_consider_frame_title (frame);
7322 }
7323 }
7324 #endif /* HAVE_WINDOW_SYSTEM */
7325
7326 /* Update the menu bar item lists, if appropriate. This has to be
7327 done before any actual redisplay or generation of display lines. */
7328 all_windows = (update_mode_lines
7329 || buffer_shared > 1
7330 || windows_or_buffers_changed);
7331 if (all_windows)
7332 {
7333 Lisp_Object tail, frame;
7334 int count = BINDING_STACK_SIZE ();
7335
7336 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7337
7338 FOR_EACH_FRAME (tail, frame)
7339 {
7340 f = XFRAME (frame);
7341
7342 /* Ignore tooltip frame. */
7343 if (EQ (frame, tooltip_frame))
7344 continue;
7345
7346 /* If a window on this frame changed size, report that to
7347 the user and clear the size-change flag. */
7348 if (FRAME_WINDOW_SIZES_CHANGED (f))
7349 {
7350 Lisp_Object functions;
7351
7352 /* Clear flag first in case we get an error below. */
7353 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
7354 functions = Vwindow_size_change_functions;
7355 GCPRO2 (tail, functions);
7356
7357 while (CONSP (functions))
7358 {
7359 call1 (XCAR (functions), frame);
7360 functions = XCDR (functions);
7361 }
7362 UNGCPRO;
7363 }
7364
7365 GCPRO1 (tail);
7366 update_menu_bar (f, 0);
7367 #ifdef HAVE_WINDOW_SYSTEM
7368 update_tool_bar (f, 0);
7369 #endif
7370 UNGCPRO;
7371 }
7372
7373 unbind_to (count, Qnil);
7374 }
7375 else
7376 {
7377 struct frame *sf = SELECTED_FRAME ();
7378 update_menu_bar (sf, 1);
7379 #ifdef HAVE_WINDOW_SYSTEM
7380 update_tool_bar (sf, 1);
7381 #endif
7382 }
7383
7384 /* Motif needs this. See comment in xmenu.c. Turn it off when
7385 pending_menu_activation is not defined. */
7386 #ifdef USE_X_TOOLKIT
7387 pending_menu_activation = 0;
7388 #endif
7389 }
7390
7391
7392 /* Update the menu bar item list for frame F. This has to be done
7393 before we start to fill in any display lines, because it can call
7394 eval.
7395
7396 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
7397
7398 static void
7399 update_menu_bar (f, save_match_data)
7400 struct frame *f;
7401 int save_match_data;
7402 {
7403 Lisp_Object window;
7404 register struct window *w;
7405
7406 /* If called recursively during a menu update, do nothing. This can
7407 happen when, for instance, an activate-menubar-hook causes a
7408 redisplay. */
7409 if (inhibit_menubar_update)
7410 return;
7411
7412 window = FRAME_SELECTED_WINDOW (f);
7413 w = XWINDOW (window);
7414
7415 if (update_mode_lines)
7416 w->update_mode_line = Qt;
7417
7418 if (FRAME_WINDOW_P (f)
7419 ?
7420 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
7421 FRAME_EXTERNAL_MENU_BAR (f)
7422 #else
7423 FRAME_MENU_BAR_LINES (f) > 0
7424 #endif
7425 : FRAME_MENU_BAR_LINES (f) > 0)
7426 {
7427 /* If the user has switched buffers or windows, we need to
7428 recompute to reflect the new bindings. But we'll
7429 recompute when update_mode_lines is set too; that means
7430 that people can use force-mode-line-update to request
7431 that the menu bar be recomputed. The adverse effect on
7432 the rest of the redisplay algorithm is about the same as
7433 windows_or_buffers_changed anyway. */
7434 if (windows_or_buffers_changed
7435 || !NILP (w->update_mode_line)
7436 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7437 < BUF_MODIFF (XBUFFER (w->buffer)))
7438 != !NILP (w->last_had_star))
7439 || ((!NILP (Vtransient_mark_mode)
7440 && !NILP (XBUFFER (w->buffer)->mark_active))
7441 != !NILP (w->region_showing)))
7442 {
7443 struct buffer *prev = current_buffer;
7444 int count = BINDING_STACK_SIZE ();
7445
7446 specbind (Qinhibit_menubar_update, Qt);
7447
7448 set_buffer_internal_1 (XBUFFER (w->buffer));
7449 if (save_match_data)
7450 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7451 if (NILP (Voverriding_local_map_menu_flag))
7452 {
7453 specbind (Qoverriding_terminal_local_map, Qnil);
7454 specbind (Qoverriding_local_map, Qnil);
7455 }
7456
7457 /* Run the Lucid hook. */
7458 safe_run_hooks (Qactivate_menubar_hook);
7459
7460 /* If it has changed current-menubar from previous value,
7461 really recompute the menu-bar from the value. */
7462 if (! NILP (Vlucid_menu_bar_dirty_flag))
7463 call0 (Qrecompute_lucid_menubar);
7464
7465 safe_run_hooks (Qmenu_bar_update_hook);
7466 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
7467
7468 /* Redisplay the menu bar in case we changed it. */
7469 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
7470 if (FRAME_WINDOW_P (f)
7471 #if defined (macintosh)
7472 /* All frames on Mac OS share the same menubar. So only the
7473 selected frame should be allowed to set it. */
7474 && f == SELECTED_FRAME ()
7475 #endif
7476 )
7477 set_frame_menubar (f, 0, 0);
7478 else
7479 /* On a terminal screen, the menu bar is an ordinary screen
7480 line, and this makes it get updated. */
7481 w->update_mode_line = Qt;
7482 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7483 /* In the non-toolkit version, the menu bar is an ordinary screen
7484 line, and this makes it get updated. */
7485 w->update_mode_line = Qt;
7486 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7487
7488 unbind_to (count, Qnil);
7489 set_buffer_internal_1 (prev);
7490 }
7491 }
7492 }
7493
7494
7495 \f
7496 /***********************************************************************
7497 Tool-bars
7498 ***********************************************************************/
7499
7500 #ifdef HAVE_WINDOW_SYSTEM
7501
7502 /* Update the tool-bar item list for frame F. This has to be done
7503 before we start to fill in any display lines. Called from
7504 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
7505 and restore it here. */
7506
7507 static void
7508 update_tool_bar (f, save_match_data)
7509 struct frame *f;
7510 int save_match_data;
7511 {
7512 if (WINDOWP (f->tool_bar_window)
7513 && XFASTINT (XWINDOW (f->tool_bar_window)->height) > 0)
7514 {
7515 Lisp_Object window;
7516 struct window *w;
7517
7518 window = FRAME_SELECTED_WINDOW (f);
7519 w = XWINDOW (window);
7520
7521 /* If the user has switched buffers or windows, we need to
7522 recompute to reflect the new bindings. But we'll
7523 recompute when update_mode_lines is set too; that means
7524 that people can use force-mode-line-update to request
7525 that the menu bar be recomputed. The adverse effect on
7526 the rest of the redisplay algorithm is about the same as
7527 windows_or_buffers_changed anyway. */
7528 if (windows_or_buffers_changed
7529 || !NILP (w->update_mode_line)
7530 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7531 < BUF_MODIFF (XBUFFER (w->buffer)))
7532 != !NILP (w->last_had_star))
7533 || ((!NILP (Vtransient_mark_mode)
7534 && !NILP (XBUFFER (w->buffer)->mark_active))
7535 != !NILP (w->region_showing)))
7536 {
7537 struct buffer *prev = current_buffer;
7538 int count = BINDING_STACK_SIZE ();
7539
7540 /* Set current_buffer to the buffer of the selected
7541 window of the frame, so that we get the right local
7542 keymaps. */
7543 set_buffer_internal_1 (XBUFFER (w->buffer));
7544
7545 /* Save match data, if we must. */
7546 if (save_match_data)
7547 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7548
7549 /* Make sure that we don't accidentally use bogus keymaps. */
7550 if (NILP (Voverriding_local_map_menu_flag))
7551 {
7552 specbind (Qoverriding_terminal_local_map, Qnil);
7553 specbind (Qoverriding_local_map, Qnil);
7554 }
7555
7556 /* Build desired tool-bar items from keymaps. */
7557 f->tool_bar_items
7558 = tool_bar_items (f->tool_bar_items, &f->n_tool_bar_items);
7559
7560 /* Redisplay the tool-bar in case we changed it. */
7561 w->update_mode_line = Qt;
7562
7563 unbind_to (count, Qnil);
7564 set_buffer_internal_1 (prev);
7565 }
7566 }
7567 }
7568
7569
7570 /* Set F->desired_tool_bar_string to a Lisp string representing frame
7571 F's desired tool-bar contents. F->tool_bar_items must have
7572 been set up previously by calling prepare_menu_bars. */
7573
7574 static void
7575 build_desired_tool_bar_string (f)
7576 struct frame *f;
7577 {
7578 int i, size, size_needed;
7579 struct gcpro gcpro1, gcpro2, gcpro3;
7580 Lisp_Object image, plist, props;
7581
7582 image = plist = props = Qnil;
7583 GCPRO3 (image, plist, props);
7584
7585 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
7586 Otherwise, make a new string. */
7587
7588 /* The size of the string we might be able to reuse. */
7589 size = (STRINGP (f->desired_tool_bar_string)
7590 ? XSTRING (f->desired_tool_bar_string)->size
7591 : 0);
7592
7593 /* We need one space in the string for each image. */
7594 size_needed = f->n_tool_bar_items;
7595
7596 /* Reuse f->desired_tool_bar_string, if possible. */
7597 if (size < size_needed || NILP (f->desired_tool_bar_string))
7598 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
7599 make_number (' '));
7600 else
7601 {
7602 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
7603 Fremove_text_properties (make_number (0), make_number (size),
7604 props, f->desired_tool_bar_string);
7605 }
7606
7607 /* Put a `display' property on the string for the images to display,
7608 put a `menu_item' property on tool-bar items with a value that
7609 is the index of the item in F's tool-bar item vector. */
7610 for (i = 0; i < f->n_tool_bar_items; ++i)
7611 {
7612 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
7613
7614 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
7615 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
7616 int hmargin, vmargin, relief, idx, end;
7617 extern Lisp_Object QCrelief, QCmargin, QCconversion, Qimage;
7618
7619 /* If image is a vector, choose the image according to the
7620 button state. */
7621 image = PROP (TOOL_BAR_ITEM_IMAGES);
7622 if (VECTORP (image))
7623 {
7624 if (enabled_p)
7625 idx = (selected_p
7626 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
7627 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
7628 else
7629 idx = (selected_p
7630 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
7631 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
7632
7633 xassert (ASIZE (image) >= idx);
7634 image = AREF (image, idx);
7635 }
7636 else
7637 idx = -1;
7638
7639 /* Ignore invalid image specifications. */
7640 if (!valid_image_p (image))
7641 continue;
7642
7643 /* Display the tool-bar button pressed, or depressed. */
7644 plist = Fcopy_sequence (XCDR (image));
7645
7646 /* Compute margin and relief to draw. */
7647 relief = (tool_bar_button_relief >= 0
7648 ? tool_bar_button_relief
7649 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
7650 hmargin = vmargin = relief;
7651
7652 if (INTEGERP (Vtool_bar_button_margin)
7653 && XINT (Vtool_bar_button_margin) > 0)
7654 {
7655 hmargin += XFASTINT (Vtool_bar_button_margin);
7656 vmargin += XFASTINT (Vtool_bar_button_margin);
7657 }
7658 else if (CONSP (Vtool_bar_button_margin))
7659 {
7660 if (INTEGERP (XCAR (Vtool_bar_button_margin))
7661 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
7662 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
7663
7664 if (INTEGERP (XCDR (Vtool_bar_button_margin))
7665 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
7666 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
7667 }
7668
7669 if (auto_raise_tool_bar_buttons_p)
7670 {
7671 /* Add a `:relief' property to the image spec if the item is
7672 selected. */
7673 if (selected_p)
7674 {
7675 plist = Fplist_put (plist, QCrelief, make_number (-relief));
7676 hmargin -= relief;
7677 vmargin -= relief;
7678 }
7679 }
7680 else
7681 {
7682 /* If image is selected, display it pressed, i.e. with a
7683 negative relief. If it's not selected, display it with a
7684 raised relief. */
7685 plist = Fplist_put (plist, QCrelief,
7686 (selected_p
7687 ? make_number (-relief)
7688 : make_number (relief)));
7689 hmargin -= relief;
7690 vmargin -= relief;
7691 }
7692
7693 /* Put a margin around the image. */
7694 if (hmargin || vmargin)
7695 {
7696 if (hmargin == vmargin)
7697 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
7698 else
7699 plist = Fplist_put (plist, QCmargin,
7700 Fcons (make_number (hmargin),
7701 make_number (vmargin)));
7702 }
7703
7704 /* If button is not enabled, and we don't have special images
7705 for the disabled state, make the image appear disabled by
7706 applying an appropriate algorithm to it. */
7707 if (!enabled_p && idx < 0)
7708 plist = Fplist_put (plist, QCconversion, Qdisabled);
7709
7710 /* Put a `display' text property on the string for the image to
7711 display. Put a `menu-item' property on the string that gives
7712 the start of this item's properties in the tool-bar items
7713 vector. */
7714 image = Fcons (Qimage, plist);
7715 props = list4 (Qdisplay, image,
7716 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
7717
7718 /* Let the last image hide all remaining spaces in the tool bar
7719 string. The string can be longer than needed when we reuse a
7720 previous string. */
7721 if (i + 1 == f->n_tool_bar_items)
7722 end = XSTRING (f->desired_tool_bar_string)->size;
7723 else
7724 end = i + 1;
7725 Fadd_text_properties (make_number (i), make_number (end),
7726 props, f->desired_tool_bar_string);
7727 #undef PROP
7728 }
7729
7730 UNGCPRO;
7731 }
7732
7733
7734 /* Display one line of the tool-bar of frame IT->f. */
7735
7736 static void
7737 display_tool_bar_line (it)
7738 struct it *it;
7739 {
7740 struct glyph_row *row = it->glyph_row;
7741 int max_x = it->last_visible_x;
7742 struct glyph *last;
7743
7744 prepare_desired_row (row);
7745 row->y = it->current_y;
7746
7747 /* Note that this isn't made use of if the face hasn't a box,
7748 so there's no need to check the face here. */
7749 it->start_of_box_run_p = 1;
7750
7751 while (it->current_x < max_x)
7752 {
7753 int x_before, x, n_glyphs_before, i, nglyphs;
7754
7755 /* Get the next display element. */
7756 if (!get_next_display_element (it))
7757 break;
7758
7759 /* Produce glyphs. */
7760 x_before = it->current_x;
7761 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
7762 PRODUCE_GLYPHS (it);
7763
7764 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
7765 i = 0;
7766 x = x_before;
7767 while (i < nglyphs)
7768 {
7769 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
7770
7771 if (x + glyph->pixel_width > max_x)
7772 {
7773 /* Glyph doesn't fit on line. */
7774 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
7775 it->current_x = x;
7776 goto out;
7777 }
7778
7779 ++it->hpos;
7780 x += glyph->pixel_width;
7781 ++i;
7782 }
7783
7784 /* Stop at line ends. */
7785 if (ITERATOR_AT_END_OF_LINE_P (it))
7786 break;
7787
7788 set_iterator_to_next (it, 1);
7789 }
7790
7791 out:;
7792
7793 row->displays_text_p = row->used[TEXT_AREA] != 0;
7794 extend_face_to_end_of_line (it);
7795 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
7796 last->right_box_line_p = 1;
7797 if (last == row->glyphs[TEXT_AREA])
7798 last->left_box_line_p = 1;
7799 compute_line_metrics (it);
7800
7801 /* If line is empty, make it occupy the rest of the tool-bar. */
7802 if (!row->displays_text_p)
7803 {
7804 row->height = row->phys_height = it->last_visible_y - row->y;
7805 row->ascent = row->phys_ascent = 0;
7806 }
7807
7808 row->full_width_p = 1;
7809 row->continued_p = 0;
7810 row->truncated_on_left_p = 0;
7811 row->truncated_on_right_p = 0;
7812
7813 it->current_x = it->hpos = 0;
7814 it->current_y += row->height;
7815 ++it->vpos;
7816 ++it->glyph_row;
7817 }
7818
7819
7820 /* Value is the number of screen lines needed to make all tool-bar
7821 items of frame F visible. */
7822
7823 static int
7824 tool_bar_lines_needed (f)
7825 struct frame *f;
7826 {
7827 struct window *w = XWINDOW (f->tool_bar_window);
7828 struct it it;
7829
7830 /* Initialize an iterator for iteration over
7831 F->desired_tool_bar_string in the tool-bar window of frame F. */
7832 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7833 it.first_visible_x = 0;
7834 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7835 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7836
7837 while (!ITERATOR_AT_END_P (&it))
7838 {
7839 it.glyph_row = w->desired_matrix->rows;
7840 clear_glyph_row (it.glyph_row);
7841 display_tool_bar_line (&it);
7842 }
7843
7844 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
7845 }
7846
7847
7848 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
7849 0, 1, 0,
7850 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
7851 (frame)
7852 Lisp_Object frame;
7853 {
7854 struct frame *f;
7855 struct window *w;
7856 int nlines = 0;
7857
7858 if (NILP (frame))
7859 frame = selected_frame;
7860 else
7861 CHECK_FRAME (frame);
7862 f = XFRAME (frame);
7863
7864 if (WINDOWP (f->tool_bar_window)
7865 || (w = XWINDOW (f->tool_bar_window),
7866 XFASTINT (w->height) > 0))
7867 {
7868 update_tool_bar (f, 1);
7869 if (f->n_tool_bar_items)
7870 {
7871 build_desired_tool_bar_string (f);
7872 nlines = tool_bar_lines_needed (f);
7873 }
7874 }
7875
7876 return make_number (nlines);
7877 }
7878
7879
7880 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
7881 height should be changed. */
7882
7883 static int
7884 redisplay_tool_bar (f)
7885 struct frame *f;
7886 {
7887 struct window *w;
7888 struct it it;
7889 struct glyph_row *row;
7890 int change_height_p = 0;
7891
7892 /* If frame hasn't a tool-bar window or if it is zero-height, don't
7893 do anything. This means you must start with tool-bar-lines
7894 non-zero to get the auto-sizing effect. Or in other words, you
7895 can turn off tool-bars by specifying tool-bar-lines zero. */
7896 if (!WINDOWP (f->tool_bar_window)
7897 || (w = XWINDOW (f->tool_bar_window),
7898 XFASTINT (w->height) == 0))
7899 return 0;
7900
7901 /* Set up an iterator for the tool-bar window. */
7902 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7903 it.first_visible_x = 0;
7904 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7905 row = it.glyph_row;
7906
7907 /* Build a string that represents the contents of the tool-bar. */
7908 build_desired_tool_bar_string (f);
7909 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7910
7911 /* Display as many lines as needed to display all tool-bar items. */
7912 while (it.current_y < it.last_visible_y)
7913 display_tool_bar_line (&it);
7914
7915 /* It doesn't make much sense to try scrolling in the tool-bar
7916 window, so don't do it. */
7917 w->desired_matrix->no_scrolling_p = 1;
7918 w->must_be_updated_p = 1;
7919
7920 if (auto_resize_tool_bars_p)
7921 {
7922 int nlines;
7923
7924 /* If we couldn't display everything, change the tool-bar's
7925 height. */
7926 if (IT_STRING_CHARPOS (it) < it.end_charpos)
7927 change_height_p = 1;
7928
7929 /* If there are blank lines at the end, except for a partially
7930 visible blank line at the end that is smaller than
7931 CANON_Y_UNIT, change the tool-bar's height. */
7932 row = it.glyph_row - 1;
7933 if (!row->displays_text_p
7934 && row->height >= CANON_Y_UNIT (f))
7935 change_height_p = 1;
7936
7937 /* If row displays tool-bar items, but is partially visible,
7938 change the tool-bar's height. */
7939 if (row->displays_text_p
7940 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
7941 change_height_p = 1;
7942
7943 /* Resize windows as needed by changing the `tool-bar-lines'
7944 frame parameter. */
7945 if (change_height_p
7946 && (nlines = tool_bar_lines_needed (f),
7947 nlines != XFASTINT (w->height)))
7948 {
7949 extern Lisp_Object Qtool_bar_lines;
7950 Lisp_Object frame;
7951 int old_height = XFASTINT (w->height);
7952
7953 XSETFRAME (frame, f);
7954 clear_glyph_matrix (w->desired_matrix);
7955 Fmodify_frame_parameters (frame,
7956 Fcons (Fcons (Qtool_bar_lines,
7957 make_number (nlines)),
7958 Qnil));
7959 if (XFASTINT (w->height) != old_height)
7960 fonts_changed_p = 1;
7961 }
7962 }
7963
7964 return change_height_p;
7965 }
7966
7967
7968 /* Get information about the tool-bar item which is displayed in GLYPH
7969 on frame F. Return in *PROP_IDX the index where tool-bar item
7970 properties start in F->tool_bar_items. Value is zero if
7971 GLYPH doesn't display a tool-bar item. */
7972
7973 int
7974 tool_bar_item_info (f, glyph, prop_idx)
7975 struct frame *f;
7976 struct glyph *glyph;
7977 int *prop_idx;
7978 {
7979 Lisp_Object prop;
7980 int success_p;
7981 int charpos;
7982
7983 /* This function can be called asynchronously, which means we must
7984 exclude any possibility that Fget_text_property signals an
7985 error. */
7986 charpos = min (XSTRING (f->current_tool_bar_string)->size, glyph->charpos);
7987 charpos = max (0, charpos);
7988
7989 /* Get the text property `menu-item' at pos. The value of that
7990 property is the start index of this item's properties in
7991 F->tool_bar_items. */
7992 prop = Fget_text_property (make_number (charpos),
7993 Qmenu_item, f->current_tool_bar_string);
7994 if (INTEGERP (prop))
7995 {
7996 *prop_idx = XINT (prop);
7997 success_p = 1;
7998 }
7999 else
8000 success_p = 0;
8001
8002 return success_p;
8003 }
8004
8005 #endif /* HAVE_WINDOW_SYSTEM */
8006
8007
8008 \f
8009 /************************************************************************
8010 Horizontal scrolling
8011 ************************************************************************/
8012
8013 static int hscroll_window_tree P_ ((Lisp_Object));
8014 static int hscroll_windows P_ ((Lisp_Object));
8015
8016 /* For all leaf windows in the window tree rooted at WINDOW, set their
8017 hscroll value so that PT is (i) visible in the window, and (ii) so
8018 that it is not within a certain margin at the window's left and
8019 right border. Value is non-zero if any window's hscroll has been
8020 changed. */
8021
8022 static int
8023 hscroll_window_tree (window)
8024 Lisp_Object window;
8025 {
8026 int hscrolled_p = 0;
8027 int hscroll_relative_p = FLOATP (Vhscroll_step);
8028 int hscroll_step_abs = 0;
8029 double hscroll_step_rel = 0;
8030
8031 if (hscroll_relative_p)
8032 {
8033 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
8034 if (hscroll_step_rel < 0)
8035 {
8036 hscroll_relative_p = 0;
8037 hscroll_step_abs = 0;
8038 }
8039 }
8040 else if (INTEGERP (Vhscroll_step))
8041 {
8042 hscroll_step_abs = XINT (Vhscroll_step);
8043 if (hscroll_step_abs < 0)
8044 hscroll_step_abs = 0;
8045 }
8046 else
8047 hscroll_step_abs = 0;
8048
8049 while (WINDOWP (window))
8050 {
8051 struct window *w = XWINDOW (window);
8052
8053 if (WINDOWP (w->hchild))
8054 hscrolled_p |= hscroll_window_tree (w->hchild);
8055 else if (WINDOWP (w->vchild))
8056 hscrolled_p |= hscroll_window_tree (w->vchild);
8057 else if (w->cursor.vpos >= 0)
8058 {
8059 int h_margin, text_area_x, text_area_y;
8060 int text_area_width, text_area_height;
8061 struct glyph_row *current_cursor_row
8062 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
8063 struct glyph_row *desired_cursor_row
8064 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
8065 struct glyph_row *cursor_row
8066 = (desired_cursor_row->enabled_p
8067 ? desired_cursor_row
8068 : current_cursor_row);
8069
8070 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
8071 &text_area_width, &text_area_height);
8072
8073 /* Scroll when cursor is inside this scroll margin. */
8074 h_margin = hscroll_margin * CANON_X_UNIT (XFRAME (w->frame));
8075
8076 if ((XFASTINT (w->hscroll)
8077 && w->cursor.x <= h_margin)
8078 || (cursor_row->enabled_p
8079 && cursor_row->truncated_on_right_p
8080 && (w->cursor.x >= text_area_width - h_margin)))
8081 {
8082 struct it it;
8083 int hscroll;
8084 struct buffer *saved_current_buffer;
8085 int pt;
8086 int wanted_x;
8087
8088 /* Find point in a display of infinite width. */
8089 saved_current_buffer = current_buffer;
8090 current_buffer = XBUFFER (w->buffer);
8091
8092 if (w == XWINDOW (selected_window))
8093 pt = BUF_PT (current_buffer);
8094 else
8095 {
8096 pt = marker_position (w->pointm);
8097 pt = max (BEGV, pt);
8098 pt = min (ZV, pt);
8099 }
8100
8101 /* Move iterator to pt starting at cursor_row->start in
8102 a line with infinite width. */
8103 init_to_row_start (&it, w, cursor_row);
8104 it.last_visible_x = INFINITY;
8105 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
8106 current_buffer = saved_current_buffer;
8107
8108 /* Position cursor in window. */
8109 if (!hscroll_relative_p && hscroll_step_abs == 0)
8110 hscroll = max (0, it.current_x - text_area_width / 2)
8111 / CANON_X_UNIT (it.f);
8112 else if (w->cursor.x >= text_area_width - h_margin)
8113 {
8114 if (hscroll_relative_p)
8115 wanted_x = text_area_width * (1 - hscroll_step_rel)
8116 - h_margin;
8117 else
8118 wanted_x = text_area_width
8119 - hscroll_step_abs * CANON_X_UNIT (it.f)
8120 - h_margin;
8121 hscroll
8122 = max (0, it.current_x - wanted_x) / CANON_X_UNIT (it.f);
8123 }
8124 else
8125 {
8126 if (hscroll_relative_p)
8127 wanted_x = text_area_width * hscroll_step_rel
8128 + h_margin;
8129 else
8130 wanted_x = hscroll_step_abs * CANON_X_UNIT (it.f)
8131 + h_margin;
8132 hscroll
8133 = max (0, it.current_x - wanted_x) / CANON_X_UNIT (it.f);
8134 }
8135 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
8136
8137 /* Don't call Fset_window_hscroll if value hasn't
8138 changed because it will prevent redisplay
8139 optimizations. */
8140 if (XFASTINT (w->hscroll) != hscroll)
8141 {
8142 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
8143 w->hscroll = make_number (hscroll);
8144 hscrolled_p = 1;
8145 }
8146 }
8147 }
8148
8149 window = w->next;
8150 }
8151
8152 /* Value is non-zero if hscroll of any leaf window has been changed. */
8153 return hscrolled_p;
8154 }
8155
8156
8157 /* Set hscroll so that cursor is visible and not inside horizontal
8158 scroll margins for all windows in the tree rooted at WINDOW. See
8159 also hscroll_window_tree above. Value is non-zero if any window's
8160 hscroll has been changed. If it has, desired matrices on the frame
8161 of WINDOW are cleared. */
8162
8163 static int
8164 hscroll_windows (window)
8165 Lisp_Object window;
8166 {
8167 int hscrolled_p;
8168
8169 if (automatic_hscrolling_p)
8170 {
8171 hscrolled_p = hscroll_window_tree (window);
8172 if (hscrolled_p)
8173 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
8174 }
8175 else
8176 hscrolled_p = 0;
8177 return hscrolled_p;
8178 }
8179
8180
8181 \f
8182 /************************************************************************
8183 Redisplay
8184 ************************************************************************/
8185
8186 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
8187 to a non-zero value. This is sometimes handy to have in a debugger
8188 session. */
8189
8190 #if GLYPH_DEBUG
8191
8192 /* First and last unchanged row for try_window_id. */
8193
8194 int debug_first_unchanged_at_end_vpos;
8195 int debug_last_unchanged_at_beg_vpos;
8196
8197 /* Delta vpos and y. */
8198
8199 int debug_dvpos, debug_dy;
8200
8201 /* Delta in characters and bytes for try_window_id. */
8202
8203 int debug_delta, debug_delta_bytes;
8204
8205 /* Values of window_end_pos and window_end_vpos at the end of
8206 try_window_id. */
8207
8208 EMACS_INT debug_end_pos, debug_end_vpos;
8209
8210 /* Append a string to W->desired_matrix->method. FMT is a printf
8211 format string. A1...A9 are a supplement for a variable-length
8212 argument list. If trace_redisplay_p is non-zero also printf the
8213 resulting string to stderr. */
8214
8215 static void
8216 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
8217 struct window *w;
8218 char *fmt;
8219 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
8220 {
8221 char buffer[512];
8222 char *method = w->desired_matrix->method;
8223 int len = strlen (method);
8224 int size = sizeof w->desired_matrix->method;
8225 int remaining = size - len - 1;
8226
8227 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
8228 if (len && remaining)
8229 {
8230 method[len] = '|';
8231 --remaining, ++len;
8232 }
8233
8234 strncpy (method + len, buffer, remaining);
8235
8236 if (trace_redisplay_p)
8237 fprintf (stderr, "%p (%s): %s\n",
8238 w,
8239 ((BUFFERP (w->buffer)
8240 && STRINGP (XBUFFER (w->buffer)->name))
8241 ? (char *) XSTRING (XBUFFER (w->buffer)->name)->data
8242 : "no buffer"),
8243 buffer);
8244 }
8245
8246 #endif /* GLYPH_DEBUG */
8247
8248
8249 /* This counter is used to clear the face cache every once in a while
8250 in redisplay_internal. It is incremented for each redisplay.
8251 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
8252 cleared. */
8253
8254 #define CLEAR_FACE_CACHE_COUNT 500
8255 static int clear_face_cache_count;
8256
8257 /* Record the previous terminal frame we displayed. */
8258
8259 static struct frame *previous_terminal_frame;
8260
8261 /* Non-zero while redisplay_internal is in progress. */
8262
8263 int redisplaying_p;
8264
8265
8266 /* Value is non-zero if all changes in window W, which displays
8267 current_buffer, are in the text between START and END. START is a
8268 buffer position, END is given as a distance from Z. Used in
8269 redisplay_internal for display optimization. */
8270
8271 static INLINE int
8272 text_outside_line_unchanged_p (w, start, end)
8273 struct window *w;
8274 int start, end;
8275 {
8276 int unchanged_p = 1;
8277
8278 /* If text or overlays have changed, see where. */
8279 if (XFASTINT (w->last_modified) < MODIFF
8280 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8281 {
8282 /* Gap in the line? */
8283 if (GPT < start || Z - GPT < end)
8284 unchanged_p = 0;
8285
8286 /* Changes start in front of the line, or end after it? */
8287 if (unchanged_p
8288 && (BEG_UNCHANGED < start - 1
8289 || END_UNCHANGED < end))
8290 unchanged_p = 0;
8291
8292 /* If selective display, can't optimize if changes start at the
8293 beginning of the line. */
8294 if (unchanged_p
8295 && INTEGERP (current_buffer->selective_display)
8296 && XINT (current_buffer->selective_display) > 0
8297 && (BEG_UNCHANGED < start || GPT <= start))
8298 unchanged_p = 0;
8299
8300 /* If there are overlays at the start or end of the line, these
8301 may have overlay strings with newlines in them. A change at
8302 START, for instance, may actually concern the display of such
8303 overlay strings as well, and they are displayed on different
8304 lines. So, quickly rule out this case. (For the future, it
8305 might be desirable to implement something more telling than
8306 just BEG/END_UNCHANGED.) */
8307 if (unchanged_p)
8308 {
8309 if (BEG + BEG_UNCHANGED == start
8310 && overlay_touches_p (start))
8311 unchanged_p = 0;
8312 if (END_UNCHANGED == end
8313 && overlay_touches_p (Z - end))
8314 unchanged_p = 0;
8315 }
8316 }
8317
8318 return unchanged_p;
8319 }
8320
8321
8322 /* Do a frame update, taking possible shortcuts into account. This is
8323 the main external entry point for redisplay.
8324
8325 If the last redisplay displayed an echo area message and that message
8326 is no longer requested, we clear the echo area or bring back the
8327 mini-buffer if that is in use. */
8328
8329 void
8330 redisplay ()
8331 {
8332 redisplay_internal (0);
8333 }
8334
8335
8336 /* Return 1 if point moved out of or into a composition. Otherwise
8337 return 0. PREV_BUF and PREV_PT are the last point buffer and
8338 position. BUF and PT are the current point buffer and position. */
8339
8340 int
8341 check_point_in_composition (prev_buf, prev_pt, buf, pt)
8342 struct buffer *prev_buf, *buf;
8343 int prev_pt, pt;
8344 {
8345 int start, end;
8346 Lisp_Object prop;
8347 Lisp_Object buffer;
8348
8349 XSETBUFFER (buffer, buf);
8350 /* Check a composition at the last point if point moved within the
8351 same buffer. */
8352 if (prev_buf == buf)
8353 {
8354 if (prev_pt == pt)
8355 /* Point didn't move. */
8356 return 0;
8357
8358 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
8359 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
8360 && COMPOSITION_VALID_P (start, end, prop)
8361 && start < prev_pt && end > prev_pt)
8362 /* The last point was within the composition. Return 1 iff
8363 point moved out of the composition. */
8364 return (pt <= start || pt >= end);
8365 }
8366
8367 /* Check a composition at the current point. */
8368 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
8369 && find_composition (pt, -1, &start, &end, &prop, buffer)
8370 && COMPOSITION_VALID_P (start, end, prop)
8371 && start < pt && end > pt);
8372 }
8373
8374
8375 /* Reconsider the setting of B->clip_changed which is displayed
8376 in window W. */
8377
8378 static INLINE void
8379 reconsider_clip_changes (w, b)
8380 struct window *w;
8381 struct buffer *b;
8382 {
8383 if (b->prevent_redisplay_optimizations_p)
8384 b->clip_changed = 1;
8385 else if (b->clip_changed
8386 && !NILP (w->window_end_valid)
8387 && w->current_matrix->buffer == b
8388 && w->current_matrix->zv == BUF_ZV (b)
8389 && w->current_matrix->begv == BUF_BEGV (b))
8390 b->clip_changed = 0;
8391
8392 /* If display wasn't paused, and W is not a tool bar window, see if
8393 point has been moved into or out of a composition. In that case,
8394 we set b->clip_changed to 1 to force updating the screen. If
8395 b->clip_changed has already been set to 1, we can skip this
8396 check. */
8397 if (!b->clip_changed
8398 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
8399 {
8400 int pt;
8401
8402 if (w == XWINDOW (selected_window))
8403 pt = BUF_PT (current_buffer);
8404 else
8405 pt = marker_position (w->pointm);
8406
8407 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
8408 || pt != XINT (w->last_point))
8409 && check_point_in_composition (w->current_matrix->buffer,
8410 XINT (w->last_point),
8411 XBUFFER (w->buffer), pt))
8412 b->clip_changed = 1;
8413 }
8414 }
8415
8416
8417 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
8418 response to any user action; therefore, we should preserve the echo
8419 area. (Actually, our caller does that job.) Perhaps in the future
8420 avoid recentering windows if it is not necessary; currently that
8421 causes some problems. */
8422
8423 static void
8424 redisplay_internal (preserve_echo_area)
8425 int preserve_echo_area;
8426 {
8427 struct window *w = XWINDOW (selected_window);
8428 struct frame *f = XFRAME (w->frame);
8429 int pause;
8430 int must_finish = 0;
8431 struct text_pos tlbufpos, tlendpos;
8432 int number_of_visible_frames;
8433 int count;
8434 struct frame *sf = SELECTED_FRAME ();
8435
8436 /* Non-zero means redisplay has to consider all windows on all
8437 frames. Zero means, only selected_window is considered. */
8438 int consider_all_windows_p;
8439
8440 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
8441
8442 /* No redisplay if running in batch mode or frame is not yet fully
8443 initialized, or redisplay is explicitly turned off by setting
8444 Vinhibit_redisplay. */
8445 if (noninteractive
8446 || !NILP (Vinhibit_redisplay)
8447 || !f->glyphs_initialized_p)
8448 return;
8449
8450 /* The flag redisplay_performed_directly_p is set by
8451 direct_output_for_insert when it already did the whole screen
8452 update necessary. */
8453 if (redisplay_performed_directly_p)
8454 {
8455 redisplay_performed_directly_p = 0;
8456 if (!hscroll_windows (selected_window))
8457 return;
8458 }
8459
8460 #ifdef USE_X_TOOLKIT
8461 if (popup_activated ())
8462 return;
8463 #endif
8464
8465 /* I don't think this happens but let's be paranoid. */
8466 if (redisplaying_p)
8467 return;
8468
8469 /* Record a function that resets redisplaying_p to its old value
8470 when we leave this function. */
8471 count = BINDING_STACK_SIZE ();
8472 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
8473 ++redisplaying_p;
8474
8475 retry:
8476 pause = 0;
8477 reconsider_clip_changes (w, current_buffer);
8478
8479 /* If new fonts have been loaded that make a glyph matrix adjustment
8480 necessary, do it. */
8481 if (fonts_changed_p)
8482 {
8483 adjust_glyphs (NULL);
8484 ++windows_or_buffers_changed;
8485 fonts_changed_p = 0;
8486 }
8487
8488 /* If face_change_count is non-zero, init_iterator will free all
8489 realized faces, which includes the faces referenced from current
8490 matrices. So, we can't reuse current matrices in this case. */
8491 if (face_change_count)
8492 ++windows_or_buffers_changed;
8493
8494 if (! FRAME_WINDOW_P (sf)
8495 && previous_terminal_frame != sf)
8496 {
8497 /* Since frames on an ASCII terminal share the same display
8498 area, displaying a different frame means redisplay the whole
8499 thing. */
8500 windows_or_buffers_changed++;
8501 SET_FRAME_GARBAGED (sf);
8502 XSETFRAME (Vterminal_frame, sf);
8503 }
8504 previous_terminal_frame = sf;
8505
8506 /* Set the visible flags for all frames. Do this before checking
8507 for resized or garbaged frames; they want to know if their frames
8508 are visible. See the comment in frame.h for
8509 FRAME_SAMPLE_VISIBILITY. */
8510 {
8511 Lisp_Object tail, frame;
8512
8513 number_of_visible_frames = 0;
8514
8515 FOR_EACH_FRAME (tail, frame)
8516 {
8517 struct frame *f = XFRAME (frame);
8518
8519 FRAME_SAMPLE_VISIBILITY (f);
8520 if (FRAME_VISIBLE_P (f))
8521 ++number_of_visible_frames;
8522 clear_desired_matrices (f);
8523 }
8524 }
8525
8526 /* Notice any pending interrupt request to change frame size. */
8527 do_pending_window_change (1);
8528
8529 /* Clear frames marked as garbaged. */
8530 if (frame_garbaged)
8531 clear_garbaged_frames ();
8532
8533 /* Build menubar and tool-bar items. */
8534 prepare_menu_bars ();
8535
8536 if (windows_or_buffers_changed)
8537 update_mode_lines++;
8538
8539 /* Detect case that we need to write or remove a star in the mode line. */
8540 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
8541 {
8542 w->update_mode_line = Qt;
8543 if (buffer_shared > 1)
8544 update_mode_lines++;
8545 }
8546
8547 /* If %c is in the mode line, update it if needed. */
8548 if (!NILP (w->column_number_displayed)
8549 /* This alternative quickly identifies a common case
8550 where no change is needed. */
8551 && !(PT == XFASTINT (w->last_point)
8552 && XFASTINT (w->last_modified) >= MODIFF
8553 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
8554 && XFASTINT (w->column_number_displayed) != current_column ())
8555 w->update_mode_line = Qt;
8556
8557 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
8558
8559 /* The variable buffer_shared is set in redisplay_window and
8560 indicates that we redisplay a buffer in different windows. See
8561 there. */
8562 consider_all_windows_p = update_mode_lines || buffer_shared > 1;
8563
8564 /* If specs for an arrow have changed, do thorough redisplay
8565 to ensure we remove any arrow that should no longer exist. */
8566 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
8567 || ! EQ (Voverlay_arrow_string, last_arrow_string))
8568 consider_all_windows_p = windows_or_buffers_changed = 1;
8569
8570 /* Normally the message* functions will have already displayed and
8571 updated the echo area, but the frame may have been trashed, or
8572 the update may have been preempted, so display the echo area
8573 again here. Checking message_cleared_p captures the case that
8574 the echo area should be cleared. */
8575 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
8576 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
8577 || (message_cleared_p
8578 && minibuf_level == 0
8579 /* If the mini-window is currently selected, this means the
8580 echo-area doesn't show through. */
8581 && !MINI_WINDOW_P (XWINDOW (selected_window))))
8582 {
8583 int window_height_changed_p = echo_area_display (0);
8584 must_finish = 1;
8585
8586 /* If we don't display the current message, don't clear the
8587 message_cleared_p flag, because, if we did, we wouldn't clear
8588 the echo area in the next redisplay which doesn't preserve
8589 the echo area. */
8590 if (!display_last_displayed_message_p)
8591 message_cleared_p = 0;
8592
8593 if (fonts_changed_p)
8594 goto retry;
8595 else if (window_height_changed_p)
8596 {
8597 consider_all_windows_p = 1;
8598 ++update_mode_lines;
8599 ++windows_or_buffers_changed;
8600
8601 /* If window configuration was changed, frames may have been
8602 marked garbaged. Clear them or we will experience
8603 surprises wrt scrolling. */
8604 if (frame_garbaged)
8605 clear_garbaged_frames ();
8606 }
8607 }
8608 else if (EQ (selected_window, minibuf_window)
8609 && (current_buffer->clip_changed
8610 || XFASTINT (w->last_modified) < MODIFF
8611 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8612 && resize_mini_window (w, 0))
8613 {
8614 /* Resized active mini-window to fit the size of what it is
8615 showing if its contents might have changed. */
8616 must_finish = 1;
8617 consider_all_windows_p = 1;
8618 ++windows_or_buffers_changed;
8619 ++update_mode_lines;
8620
8621 /* If window configuration was changed, frames may have been
8622 marked garbaged. Clear them or we will experience
8623 surprises wrt scrolling. */
8624 if (frame_garbaged)
8625 clear_garbaged_frames ();
8626 }
8627
8628
8629 /* If showing the region, and mark has changed, we must redisplay
8630 the whole window. The assignment to this_line_start_pos prevents
8631 the optimization directly below this if-statement. */
8632 if (((!NILP (Vtransient_mark_mode)
8633 && !NILP (XBUFFER (w->buffer)->mark_active))
8634 != !NILP (w->region_showing))
8635 || (!NILP (w->region_showing)
8636 && !EQ (w->region_showing,
8637 Fmarker_position (XBUFFER (w->buffer)->mark))))
8638 CHARPOS (this_line_start_pos) = 0;
8639
8640 /* Optimize the case that only the line containing the cursor in the
8641 selected window has changed. Variables starting with this_ are
8642 set in display_line and record information about the line
8643 containing the cursor. */
8644 tlbufpos = this_line_start_pos;
8645 tlendpos = this_line_end_pos;
8646 if (!consider_all_windows_p
8647 && CHARPOS (tlbufpos) > 0
8648 && NILP (w->update_mode_line)
8649 && !current_buffer->clip_changed
8650 && FRAME_VISIBLE_P (XFRAME (w->frame))
8651 && !FRAME_OBSCURED_P (XFRAME (w->frame))
8652 /* Make sure recorded data applies to current buffer, etc. */
8653 && this_line_buffer == current_buffer
8654 && current_buffer == XBUFFER (w->buffer)
8655 && NILP (w->force_start)
8656 /* Point must be on the line that we have info recorded about. */
8657 && PT >= CHARPOS (tlbufpos)
8658 && PT <= Z - CHARPOS (tlendpos)
8659 /* All text outside that line, including its final newline,
8660 must be unchanged */
8661 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
8662 CHARPOS (tlendpos)))
8663 {
8664 if (CHARPOS (tlbufpos) > BEGV
8665 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
8666 && (CHARPOS (tlbufpos) == ZV
8667 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
8668 /* Former continuation line has disappeared by becoming empty */
8669 goto cancel;
8670 else if (XFASTINT (w->last_modified) < MODIFF
8671 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
8672 || MINI_WINDOW_P (w))
8673 {
8674 /* We have to handle the case of continuation around a
8675 wide-column character (See the comment in indent.c around
8676 line 885).
8677
8678 For instance, in the following case:
8679
8680 -------- Insert --------
8681 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
8682 J_I_ ==> J_I_ `^^' are cursors.
8683 ^^ ^^
8684 -------- --------
8685
8686 As we have to redraw the line above, we should goto cancel. */
8687
8688 struct it it;
8689 int line_height_before = this_line_pixel_height;
8690
8691 /* Note that start_display will handle the case that the
8692 line starting at tlbufpos is a continuation lines. */
8693 start_display (&it, w, tlbufpos);
8694
8695 /* Implementation note: It this still necessary? */
8696 if (it.current_x != this_line_start_x)
8697 goto cancel;
8698
8699 TRACE ((stderr, "trying display optimization 1\n"));
8700 w->cursor.vpos = -1;
8701 overlay_arrow_seen = 0;
8702 it.vpos = this_line_vpos;
8703 it.current_y = this_line_y;
8704 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
8705 display_line (&it);
8706
8707 /* If line contains point, is not continued,
8708 and ends at same distance from eob as before, we win */
8709 if (w->cursor.vpos >= 0
8710 /* Line is not continued, otherwise this_line_start_pos
8711 would have been set to 0 in display_line. */
8712 && CHARPOS (this_line_start_pos)
8713 /* Line ends as before. */
8714 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
8715 /* Line has same height as before. Otherwise other lines
8716 would have to be shifted up or down. */
8717 && this_line_pixel_height == line_height_before)
8718 {
8719 /* If this is not the window's last line, we must adjust
8720 the charstarts of the lines below. */
8721 if (it.current_y < it.last_visible_y)
8722 {
8723 struct glyph_row *row
8724 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
8725 int delta, delta_bytes;
8726
8727 if (Z - CHARPOS (tlendpos) == ZV)
8728 {
8729 /* This line ends at end of (accessible part of)
8730 buffer. There is no newline to count. */
8731 delta = (Z
8732 - CHARPOS (tlendpos)
8733 - MATRIX_ROW_START_CHARPOS (row));
8734 delta_bytes = (Z_BYTE
8735 - BYTEPOS (tlendpos)
8736 - MATRIX_ROW_START_BYTEPOS (row));
8737 }
8738 else
8739 {
8740 /* This line ends in a newline. Must take
8741 account of the newline and the rest of the
8742 text that follows. */
8743 delta = (Z
8744 - CHARPOS (tlendpos)
8745 - MATRIX_ROW_START_CHARPOS (row));
8746 delta_bytes = (Z_BYTE
8747 - BYTEPOS (tlendpos)
8748 - MATRIX_ROW_START_BYTEPOS (row));
8749 }
8750
8751 increment_matrix_positions (w->current_matrix,
8752 this_line_vpos + 1,
8753 w->current_matrix->nrows,
8754 delta, delta_bytes);
8755 }
8756
8757 /* If this row displays text now but previously didn't,
8758 or vice versa, w->window_end_vpos may have to be
8759 adjusted. */
8760 if ((it.glyph_row - 1)->displays_text_p)
8761 {
8762 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
8763 XSETINT (w->window_end_vpos, this_line_vpos);
8764 }
8765 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
8766 && this_line_vpos > 0)
8767 XSETINT (w->window_end_vpos, this_line_vpos - 1);
8768 w->window_end_valid = Qnil;
8769
8770 /* Update hint: No need to try to scroll in update_window. */
8771 w->desired_matrix->no_scrolling_p = 1;
8772
8773 #if GLYPH_DEBUG
8774 *w->desired_matrix->method = 0;
8775 debug_method_add (w, "optimization 1");
8776 #endif
8777 goto update;
8778 }
8779 else
8780 goto cancel;
8781 }
8782 else if (/* Cursor position hasn't changed. */
8783 PT == XFASTINT (w->last_point)
8784 /* Make sure the cursor was last displayed
8785 in this window. Otherwise we have to reposition it. */
8786 && 0 <= w->cursor.vpos
8787 && XINT (w->height) > w->cursor.vpos)
8788 {
8789 if (!must_finish)
8790 {
8791 do_pending_window_change (1);
8792
8793 /* We used to always goto end_of_redisplay here, but this
8794 isn't enough if we have a blinking cursor. */
8795 if (w->cursor_off_p == w->last_cursor_off_p)
8796 goto end_of_redisplay;
8797 }
8798 goto update;
8799 }
8800 /* If highlighting the region, or if the cursor is in the echo area,
8801 then we can't just move the cursor. */
8802 else if (! (!NILP (Vtransient_mark_mode)
8803 && !NILP (current_buffer->mark_active))
8804 && (EQ (selected_window, current_buffer->last_selected_window)
8805 || highlight_nonselected_windows)
8806 && NILP (w->region_showing)
8807 && NILP (Vshow_trailing_whitespace)
8808 && !cursor_in_echo_area)
8809 {
8810 struct it it;
8811 struct glyph_row *row;
8812
8813 /* Skip from tlbufpos to PT and see where it is. Note that
8814 PT may be in invisible text. If so, we will end at the
8815 next visible position. */
8816 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
8817 NULL, DEFAULT_FACE_ID);
8818 it.current_x = this_line_start_x;
8819 it.current_y = this_line_y;
8820 it.vpos = this_line_vpos;
8821
8822 /* The call to move_it_to stops in front of PT, but
8823 moves over before-strings. */
8824 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
8825
8826 if (it.vpos == this_line_vpos
8827 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
8828 row->enabled_p))
8829 {
8830 xassert (this_line_vpos == it.vpos);
8831 xassert (this_line_y == it.current_y);
8832 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
8833 #if GLYPH_DEBUG
8834 *w->desired_matrix->method = 0;
8835 debug_method_add (w, "optimization 3");
8836 #endif
8837 goto update;
8838 }
8839 else
8840 goto cancel;
8841 }
8842
8843 cancel:
8844 /* Text changed drastically or point moved off of line. */
8845 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
8846 }
8847
8848 CHARPOS (this_line_start_pos) = 0;
8849 consider_all_windows_p |= buffer_shared > 1;
8850 ++clear_face_cache_count;
8851
8852
8853 /* Build desired matrices, and update the display. If
8854 consider_all_windows_p is non-zero, do it for all windows on all
8855 frames. Otherwise do it for selected_window, only. */
8856
8857 if (consider_all_windows_p)
8858 {
8859 Lisp_Object tail, frame;
8860 int i, n = 0, size = 50;
8861 struct frame **updated
8862 = (struct frame **) alloca (size * sizeof *updated);
8863
8864 /* Clear the face cache eventually. */
8865 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
8866 {
8867 clear_face_cache (0);
8868 clear_face_cache_count = 0;
8869 }
8870
8871 /* Recompute # windows showing selected buffer. This will be
8872 incremented each time such a window is displayed. */
8873 buffer_shared = 0;
8874
8875 FOR_EACH_FRAME (tail, frame)
8876 {
8877 struct frame *f = XFRAME (frame);
8878
8879 if (FRAME_WINDOW_P (f) || f == sf)
8880 {
8881 #ifdef HAVE_WINDOW_SYSTEM
8882 if (clear_face_cache_count % 50 == 0
8883 && FRAME_WINDOW_P (f))
8884 clear_image_cache (f, 0);
8885 #endif /* HAVE_WINDOW_SYSTEM */
8886
8887 /* Mark all the scroll bars to be removed; we'll redeem
8888 the ones we want when we redisplay their windows. */
8889 if (condemn_scroll_bars_hook)
8890 condemn_scroll_bars_hook (f);
8891
8892 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8893 redisplay_windows (FRAME_ROOT_WINDOW (f));
8894
8895 /* Any scroll bars which redisplay_windows should have
8896 nuked should now go away. */
8897 if (judge_scroll_bars_hook)
8898 judge_scroll_bars_hook (f);
8899
8900 /* If fonts changed, display again. */
8901 if (fonts_changed_p)
8902 goto retry;
8903
8904 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8905 {
8906 /* See if we have to hscroll. */
8907 if (hscroll_windows (f->root_window))
8908 goto retry;
8909
8910 /* Prevent various kinds of signals during display
8911 update. stdio is not robust about handling
8912 signals, which can cause an apparent I/O
8913 error. */
8914 if (interrupt_input)
8915 unrequest_sigio ();
8916 stop_polling ();
8917
8918 /* Update the display. */
8919 set_window_update_flags (XWINDOW (f->root_window), 1);
8920 pause |= update_frame (f, 0, 0);
8921 if (pause)
8922 break;
8923
8924 if (n == size)
8925 {
8926 int nbytes = size * sizeof *updated;
8927 struct frame **p = (struct frame **) alloca (2 * nbytes);
8928 bcopy (updated, p, nbytes);
8929 size *= 2;
8930 }
8931
8932 updated[n++] = f;
8933 }
8934 }
8935 }
8936
8937 /* Do the mark_window_display_accurate after all windows have
8938 been redisplayed because this call resets flags in buffers
8939 which are needed for proper redisplay. */
8940 for (i = 0; i < n; ++i)
8941 {
8942 struct frame *f = updated[i];
8943 mark_window_display_accurate (f->root_window, 1);
8944 if (frame_up_to_date_hook)
8945 frame_up_to_date_hook (f);
8946 }
8947 }
8948 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8949 {
8950 Lisp_Object mini_window;
8951 struct frame *mini_frame;
8952
8953 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
8954 internal_condition_case_1 (redisplay_window_1, selected_window, Qerror,
8955 redisplay_window_error);
8956
8957 /* Compare desired and current matrices, perform output. */
8958 update:
8959
8960 /* If fonts changed, display again. */
8961 if (fonts_changed_p)
8962 goto retry;
8963
8964 /* Prevent various kinds of signals during display update.
8965 stdio is not robust about handling signals,
8966 which can cause an apparent I/O error. */
8967 if (interrupt_input)
8968 unrequest_sigio ();
8969 stop_polling ();
8970
8971 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8972 {
8973 if (hscroll_windows (selected_window))
8974 goto retry;
8975
8976 XWINDOW (selected_window)->must_be_updated_p = 1;
8977 pause = update_frame (sf, 0, 0);
8978 }
8979
8980 /* We may have called echo_area_display at the top of this
8981 function. If the echo area is on another frame, that may
8982 have put text on a frame other than the selected one, so the
8983 above call to update_frame would not have caught it. Catch
8984 it here. */
8985 mini_window = FRAME_MINIBUF_WINDOW (sf);
8986 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8987
8988 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
8989 {
8990 XWINDOW (mini_window)->must_be_updated_p = 1;
8991 pause |= update_frame (mini_frame, 0, 0);
8992 if (!pause && hscroll_windows (mini_window))
8993 goto retry;
8994 }
8995 }
8996
8997 /* If display was paused because of pending input, make sure we do a
8998 thorough update the next time. */
8999 if (pause)
9000 {
9001 /* Prevent the optimization at the beginning of
9002 redisplay_internal that tries a single-line update of the
9003 line containing the cursor in the selected window. */
9004 CHARPOS (this_line_start_pos) = 0;
9005
9006 /* Let the overlay arrow be updated the next time. */
9007 if (!NILP (last_arrow_position))
9008 {
9009 last_arrow_position = Qt;
9010 last_arrow_string = Qt;
9011 }
9012
9013 /* If we pause after scrolling, some rows in the current
9014 matrices of some windows are not valid. */
9015 if (!WINDOW_FULL_WIDTH_P (w)
9016 && !FRAME_WINDOW_P (XFRAME (w->frame)))
9017 update_mode_lines = 1;
9018 }
9019 else
9020 {
9021 if (!consider_all_windows_p)
9022 {
9023 /* This has already been done above if
9024 consider_all_windows_p is set. */
9025 mark_window_display_accurate_1 (w, 1);
9026
9027 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
9028 last_arrow_string = Voverlay_arrow_string;
9029
9030 if (frame_up_to_date_hook != 0)
9031 frame_up_to_date_hook (sf);
9032 }
9033
9034 update_mode_lines = 0;
9035 windows_or_buffers_changed = 0;
9036 }
9037
9038 /* Start SIGIO interrupts coming again. Having them off during the
9039 code above makes it less likely one will discard output, but not
9040 impossible, since there might be stuff in the system buffer here.
9041 But it is much hairier to try to do anything about that. */
9042 if (interrupt_input)
9043 request_sigio ();
9044 start_polling ();
9045
9046 /* If a frame has become visible which was not before, redisplay
9047 again, so that we display it. Expose events for such a frame
9048 (which it gets when becoming visible) don't call the parts of
9049 redisplay constructing glyphs, so simply exposing a frame won't
9050 display anything in this case. So, we have to display these
9051 frames here explicitly. */
9052 if (!pause)
9053 {
9054 Lisp_Object tail, frame;
9055 int new_count = 0;
9056
9057 FOR_EACH_FRAME (tail, frame)
9058 {
9059 int this_is_visible = 0;
9060
9061 if (XFRAME (frame)->visible)
9062 this_is_visible = 1;
9063 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
9064 if (XFRAME (frame)->visible)
9065 this_is_visible = 1;
9066
9067 if (this_is_visible)
9068 new_count++;
9069 }
9070
9071 if (new_count != number_of_visible_frames)
9072 windows_or_buffers_changed++;
9073 }
9074
9075 /* Change frame size now if a change is pending. */
9076 do_pending_window_change (1);
9077
9078 /* If we just did a pending size change, or have additional
9079 visible frames, redisplay again. */
9080 if (windows_or_buffers_changed && !pause)
9081 goto retry;
9082
9083 end_of_redisplay:;
9084
9085 unbind_to (count, Qnil);
9086 }
9087
9088
9089 /* Redisplay, but leave alone any recent echo area message unless
9090 another message has been requested in its place.
9091
9092 This is useful in situations where you need to redisplay but no
9093 user action has occurred, making it inappropriate for the message
9094 area to be cleared. See tracking_off and
9095 wait_reading_process_input for examples of these situations.
9096
9097 FROM_WHERE is an integer saying from where this function was
9098 called. This is useful for debugging. */
9099
9100 void
9101 redisplay_preserve_echo_area (from_where)
9102 int from_where;
9103 {
9104 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
9105
9106 if (!NILP (echo_area_buffer[1]))
9107 {
9108 /* We have a previously displayed message, but no current
9109 message. Redisplay the previous message. */
9110 display_last_displayed_message_p = 1;
9111 redisplay_internal (1);
9112 display_last_displayed_message_p = 0;
9113 }
9114 else
9115 redisplay_internal (1);
9116 }
9117
9118
9119 /* Function registered with record_unwind_protect in
9120 redisplay_internal. Clears the flag indicating that a redisplay is
9121 in progress. */
9122
9123 static Lisp_Object
9124 unwind_redisplay (old_redisplaying_p)
9125 Lisp_Object old_redisplaying_p;
9126 {
9127 redisplaying_p = XFASTINT (old_redisplaying_p);
9128 return Qnil;
9129 }
9130
9131
9132 /* Mark the display of window W as accurate or inaccurate. If
9133 ACCURATE_P is non-zero mark display of W as accurate. If
9134 ACCURATE_P is zero, arrange for W to be redisplayed the next time
9135 redisplay_internal is called. */
9136
9137 static void
9138 mark_window_display_accurate_1 (w, accurate_p)
9139 struct window *w;
9140 int accurate_p;
9141 {
9142 if (BUFFERP (w->buffer))
9143 {
9144 struct buffer *b = XBUFFER (w->buffer);
9145
9146 w->last_modified
9147 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
9148 w->last_overlay_modified
9149 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
9150 w->last_had_star
9151 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
9152
9153 if (accurate_p)
9154 {
9155 b->clip_changed = 0;
9156 b->prevent_redisplay_optimizations_p = 0;
9157
9158 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
9159 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
9160 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
9161 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
9162
9163 w->current_matrix->buffer = b;
9164 w->current_matrix->begv = BUF_BEGV (b);
9165 w->current_matrix->zv = BUF_ZV (b);
9166
9167 w->last_cursor = w->cursor;
9168 w->last_cursor_off_p = w->cursor_off_p;
9169
9170 if (w == XWINDOW (selected_window))
9171 w->last_point = make_number (BUF_PT (b));
9172 else
9173 w->last_point = make_number (XMARKER (w->pointm)->charpos);
9174 }
9175 }
9176
9177 if (accurate_p)
9178 {
9179 w->window_end_valid = w->buffer;
9180 #if 0 /* This is incorrect with variable-height lines. */
9181 xassert (XINT (w->window_end_vpos)
9182 < (XINT (w->height)
9183 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
9184 #endif
9185 w->update_mode_line = Qnil;
9186 }
9187 }
9188
9189
9190 /* Mark the display of windows in the window tree rooted at WINDOW as
9191 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
9192 windows as accurate. If ACCURATE_P is zero, arrange for windows to
9193 be redisplayed the next time redisplay_internal is called. */
9194
9195 void
9196 mark_window_display_accurate (window, accurate_p)
9197 Lisp_Object window;
9198 int accurate_p;
9199 {
9200 struct window *w;
9201
9202 for (; !NILP (window); window = w->next)
9203 {
9204 w = XWINDOW (window);
9205 mark_window_display_accurate_1 (w, accurate_p);
9206
9207 if (!NILP (w->vchild))
9208 mark_window_display_accurate (w->vchild, accurate_p);
9209 if (!NILP (w->hchild))
9210 mark_window_display_accurate (w->hchild, accurate_p);
9211 }
9212
9213 if (accurate_p)
9214 {
9215 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
9216 last_arrow_string = Voverlay_arrow_string;
9217 }
9218 else
9219 {
9220 /* Force a thorough redisplay the next time by setting
9221 last_arrow_position and last_arrow_string to t, which is
9222 unequal to any useful value of Voverlay_arrow_... */
9223 last_arrow_position = Qt;
9224 last_arrow_string = Qt;
9225 }
9226 }
9227
9228
9229 /* Return value in display table DP (Lisp_Char_Table *) for character
9230 C. Since a display table doesn't have any parent, we don't have to
9231 follow parent. Do not call this function directly but use the
9232 macro DISP_CHAR_VECTOR. */
9233
9234 Lisp_Object
9235 disp_char_vector (dp, c)
9236 struct Lisp_Char_Table *dp;
9237 int c;
9238 {
9239 int code[4], i;
9240 Lisp_Object val;
9241
9242 if (SINGLE_BYTE_CHAR_P (c))
9243 return (dp->contents[c]);
9244
9245 SPLIT_CHAR (c, code[0], code[1], code[2]);
9246 if (code[1] < 32)
9247 code[1] = -1;
9248 else if (code[2] < 32)
9249 code[2] = -1;
9250
9251 /* Here, the possible range of code[0] (== charset ID) is
9252 128..max_charset. Since the top level char table contains data
9253 for multibyte characters after 256th element, we must increment
9254 code[0] by 128 to get a correct index. */
9255 code[0] += 128;
9256 code[3] = -1; /* anchor */
9257
9258 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
9259 {
9260 val = dp->contents[code[i]];
9261 if (!SUB_CHAR_TABLE_P (val))
9262 return (NILP (val) ? dp->defalt : val);
9263 }
9264
9265 /* Here, val is a sub char table. We return the default value of
9266 it. */
9267 return (dp->defalt);
9268 }
9269
9270
9271 \f
9272 /***********************************************************************
9273 Window Redisplay
9274 ***********************************************************************/
9275
9276 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
9277
9278 static void
9279 redisplay_windows (window)
9280 Lisp_Object window;
9281 {
9282 while (!NILP (window))
9283 {
9284 struct window *w = XWINDOW (window);
9285
9286 if (!NILP (w->hchild))
9287 redisplay_windows (w->hchild);
9288 else if (!NILP (w->vchild))
9289 redisplay_windows (w->vchild);
9290 else
9291 {
9292 displayed_buffer = XBUFFER (w->buffer);
9293 internal_condition_case_1 (redisplay_window_0, window, Qerror,
9294 redisplay_window_error);
9295 }
9296
9297 window = w->next;
9298 }
9299 }
9300
9301 static Lisp_Object
9302 redisplay_window_error ()
9303 {
9304 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
9305 return Qnil;
9306 }
9307
9308 static Lisp_Object
9309 redisplay_window_0 (window)
9310 Lisp_Object window;
9311 {
9312 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
9313 redisplay_window (window, 0);
9314 return Qnil;
9315 }
9316
9317 static Lisp_Object
9318 redisplay_window_1 (window)
9319 Lisp_Object window;
9320 {
9321 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
9322 redisplay_window (window, 1);
9323 return Qnil;
9324 }
9325 \f
9326 /* Set cursor position of W. PT is assumed to be displayed in ROW.
9327 DELTA is the number of bytes by which positions recorded in ROW
9328 differ from current buffer positions. */
9329
9330 void
9331 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
9332 struct window *w;
9333 struct glyph_row *row;
9334 struct glyph_matrix *matrix;
9335 int delta, delta_bytes, dy, dvpos;
9336 {
9337 struct glyph *glyph = row->glyphs[TEXT_AREA];
9338 struct glyph *end = glyph + row->used[TEXT_AREA];
9339 int x = row->x;
9340 int pt_old = PT - delta;
9341
9342 /* Skip over glyphs not having an object at the start of the row.
9343 These are special glyphs like truncation marks on terminal
9344 frames. */
9345 if (row->displays_text_p)
9346 while (glyph < end
9347 && INTEGERP (glyph->object)
9348 && glyph->charpos < 0)
9349 {
9350 x += glyph->pixel_width;
9351 ++glyph;
9352 }
9353
9354 while (glyph < end
9355 && !INTEGERP (glyph->object)
9356 && (!BUFFERP (glyph->object)
9357 || glyph->charpos < pt_old))
9358 {
9359 x += glyph->pixel_width;
9360 ++glyph;
9361 }
9362
9363 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
9364 w->cursor.x = x;
9365 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
9366 w->cursor.y = row->y + dy;
9367
9368 if (w == XWINDOW (selected_window))
9369 {
9370 if (!row->continued_p
9371 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
9372 && row->x == 0)
9373 {
9374 this_line_buffer = XBUFFER (w->buffer);
9375
9376 CHARPOS (this_line_start_pos)
9377 = MATRIX_ROW_START_CHARPOS (row) + delta;
9378 BYTEPOS (this_line_start_pos)
9379 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
9380
9381 CHARPOS (this_line_end_pos)
9382 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
9383 BYTEPOS (this_line_end_pos)
9384 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
9385
9386 this_line_y = w->cursor.y;
9387 this_line_pixel_height = row->height;
9388 this_line_vpos = w->cursor.vpos;
9389 this_line_start_x = row->x;
9390 }
9391 else
9392 CHARPOS (this_line_start_pos) = 0;
9393 }
9394 }
9395
9396
9397 /* Run window scroll functions, if any, for WINDOW with new window
9398 start STARTP. Sets the window start of WINDOW to that position.
9399
9400 We assume that the window's buffer is really current. */
9401
9402 static INLINE struct text_pos
9403 run_window_scroll_functions (window, startp)
9404 Lisp_Object window;
9405 struct text_pos startp;
9406 {
9407 struct window *w = XWINDOW (window);
9408 SET_MARKER_FROM_TEXT_POS (w->start, startp);
9409
9410 if (current_buffer != XBUFFER (w->buffer))
9411 abort ();
9412
9413 if (!NILP (Vwindow_scroll_functions))
9414 {
9415 run_hook_with_args_2 (Qwindow_scroll_functions, window,
9416 make_number (CHARPOS (startp)));
9417 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9418 /* In case the hook functions switch buffers. */
9419 if (current_buffer != XBUFFER (w->buffer))
9420 set_buffer_internal_1 (XBUFFER (w->buffer));
9421 }
9422
9423 return startp;
9424 }
9425
9426
9427 /* Modify the desired matrix of window W and W->vscroll so that the
9428 line containing the cursor is fully visible. If this requires
9429 larger matrices than are allocated, set fonts_changed_p and return
9430 0. */
9431
9432 static int
9433 make_cursor_line_fully_visible (w)
9434 struct window *w;
9435 {
9436 struct glyph_matrix *matrix;
9437 struct glyph_row *row;
9438 int window_height;
9439
9440 /* It's not always possible to find the cursor, e.g, when a window
9441 is full of overlay strings. Don't do anything in that case. */
9442 if (w->cursor.vpos < 0)
9443 return 1;
9444
9445 matrix = w->desired_matrix;
9446 row = MATRIX_ROW (matrix, w->cursor.vpos);
9447
9448 /* If the cursor row is not partially visible, there's nothing
9449 to do. */
9450 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
9451 return 1;
9452
9453 /* If the row the cursor is in is taller than the window's height,
9454 it's not clear what to do, so do nothing. */
9455 window_height = window_box_height (w);
9456 if (row->height >= window_height)
9457 return 1;
9458
9459 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
9460 {
9461 int dy = row->height - row->visible_height;
9462 w->vscroll = 0;
9463 w->cursor.y += dy;
9464 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9465 }
9466 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
9467 {
9468 int dy = - (row->height - row->visible_height);
9469 w->vscroll = dy;
9470 w->cursor.y += dy;
9471 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9472 }
9473
9474 /* When we change the cursor y-position of the selected window,
9475 change this_line_y as well so that the display optimization for
9476 the cursor line of the selected window in redisplay_internal uses
9477 the correct y-position. */
9478 if (w == XWINDOW (selected_window))
9479 this_line_y = w->cursor.y;
9480
9481 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
9482 redisplay with larger matrices. */
9483 if (matrix->nrows < required_matrix_height (w))
9484 {
9485 fonts_changed_p = 1;
9486 return 0;
9487 }
9488
9489 return 1;
9490 }
9491
9492
9493 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
9494 non-zero means only WINDOW is redisplayed in redisplay_internal.
9495 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
9496 in redisplay_window to bring a partially visible line into view in
9497 the case that only the cursor has moved.
9498
9499 Value is
9500
9501 1 if scrolling succeeded
9502
9503 0 if scrolling didn't find point.
9504
9505 -1 if new fonts have been loaded so that we must interrupt
9506 redisplay, adjust glyph matrices, and try again. */
9507
9508 enum
9509 {
9510 SCROLLING_SUCCESS,
9511 SCROLLING_FAILED,
9512 SCROLLING_NEED_LARGER_MATRICES
9513 };
9514
9515 static int
9516 try_scrolling (window, just_this_one_p, scroll_conservatively,
9517 scroll_step, temp_scroll_step)
9518 Lisp_Object window;
9519 int just_this_one_p;
9520 EMACS_INT scroll_conservatively, scroll_step;
9521 int temp_scroll_step;
9522 {
9523 struct window *w = XWINDOW (window);
9524 struct frame *f = XFRAME (w->frame);
9525 struct text_pos scroll_margin_pos;
9526 struct text_pos pos;
9527 struct text_pos startp;
9528 struct it it;
9529 Lisp_Object window_end;
9530 int this_scroll_margin;
9531 int dy = 0;
9532 int scroll_max;
9533 int rc;
9534 int amount_to_scroll = 0;
9535 Lisp_Object aggressive;
9536 int height;
9537
9538 #if GLYPH_DEBUG
9539 debug_method_add (w, "try_scrolling");
9540 #endif
9541
9542 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9543
9544 /* Compute scroll margin height in pixels. We scroll when point is
9545 within this distance from the top or bottom of the window. */
9546 if (scroll_margin > 0)
9547 {
9548 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
9549 this_scroll_margin *= CANON_Y_UNIT (f);
9550 }
9551 else
9552 this_scroll_margin = 0;
9553
9554 /* Compute how much we should try to scroll maximally to bring point
9555 into view. */
9556 if (scroll_step || scroll_conservatively || temp_scroll_step)
9557 scroll_max = max (scroll_step,
9558 max (scroll_conservatively, temp_scroll_step));
9559 else if (NUMBERP (current_buffer->scroll_down_aggressively)
9560 || NUMBERP (current_buffer->scroll_up_aggressively))
9561 /* We're trying to scroll because of aggressive scrolling
9562 but no scroll_step is set. Choose an arbitrary one. Maybe
9563 there should be a variable for this. */
9564 scroll_max = 10;
9565 else
9566 scroll_max = 0;
9567 scroll_max *= CANON_Y_UNIT (f);
9568
9569 /* Decide whether we have to scroll down. Start at the window end
9570 and move this_scroll_margin up to find the position of the scroll
9571 margin. */
9572 window_end = Fwindow_end (window, Qt);
9573 CHARPOS (scroll_margin_pos) = XINT (window_end);
9574 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
9575 if (this_scroll_margin)
9576 {
9577 start_display (&it, w, scroll_margin_pos);
9578 move_it_vertically (&it, - this_scroll_margin);
9579 scroll_margin_pos = it.current.pos;
9580 }
9581
9582 if (PT >= CHARPOS (scroll_margin_pos))
9583 {
9584 int y0;
9585
9586 /* Point is in the scroll margin at the bottom of the window, or
9587 below. Compute a new window start that makes point visible. */
9588
9589 /* Compute the distance from the scroll margin to PT.
9590 Give up if the distance is greater than scroll_max. */
9591 start_display (&it, w, scroll_margin_pos);
9592 y0 = it.current_y;
9593 move_it_to (&it, PT, 0, it.last_visible_y, -1,
9594 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9595
9596 /* To make point visible, we have to move the window start
9597 down so that the line the cursor is in is visible, which
9598 means we have to add in the height of the cursor line. */
9599 dy = line_bottom_y (&it) - y0;
9600
9601 if (dy > scroll_max)
9602 return SCROLLING_FAILED;
9603
9604 /* Move the window start down. If scrolling conservatively,
9605 move it just enough down to make point visible. If
9606 scroll_step is set, move it down by scroll_step. */
9607 start_display (&it, w, startp);
9608
9609 if (scroll_conservatively)
9610 amount_to_scroll
9611 = max (max (dy, CANON_Y_UNIT (f)),
9612 CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
9613 else if (scroll_step || temp_scroll_step)
9614 amount_to_scroll = scroll_max;
9615 else
9616 {
9617 aggressive = current_buffer->scroll_up_aggressively;
9618 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
9619 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
9620 if (NUMBERP (aggressive))
9621 amount_to_scroll = XFLOATINT (aggressive) * height;
9622 }
9623
9624 if (amount_to_scroll <= 0)
9625 return SCROLLING_FAILED;
9626
9627 move_it_vertically (&it, amount_to_scroll);
9628 startp = it.current.pos;
9629 }
9630 else
9631 {
9632 /* See if point is inside the scroll margin at the top of the
9633 window. */
9634 scroll_margin_pos = startp;
9635 if (this_scroll_margin)
9636 {
9637 start_display (&it, w, startp);
9638 move_it_vertically (&it, this_scroll_margin);
9639 scroll_margin_pos = it.current.pos;
9640 }
9641
9642 if (PT < CHARPOS (scroll_margin_pos))
9643 {
9644 /* Point is in the scroll margin at the top of the window or
9645 above what is displayed in the window. */
9646 int y0;
9647
9648 /* Compute the vertical distance from PT to the scroll
9649 margin position. Give up if distance is greater than
9650 scroll_max. */
9651 SET_TEXT_POS (pos, PT, PT_BYTE);
9652 start_display (&it, w, pos);
9653 y0 = it.current_y;
9654 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
9655 it.last_visible_y, -1,
9656 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9657 dy = it.current_y - y0;
9658 if (dy > scroll_max)
9659 return SCROLLING_FAILED;
9660
9661 /* Compute new window start. */
9662 start_display (&it, w, startp);
9663
9664 if (scroll_conservatively)
9665 amount_to_scroll =
9666 max (dy, CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
9667 else if (scroll_step || temp_scroll_step)
9668 amount_to_scroll = scroll_max;
9669 else
9670 {
9671 aggressive = current_buffer->scroll_down_aggressively;
9672 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
9673 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
9674 if (NUMBERP (aggressive))
9675 amount_to_scroll = XFLOATINT (aggressive) * height;
9676 }
9677
9678 if (amount_to_scroll <= 0)
9679 return SCROLLING_FAILED;
9680
9681 move_it_vertically (&it, - amount_to_scroll);
9682 startp = it.current.pos;
9683 }
9684 }
9685
9686 /* Run window scroll functions. */
9687 startp = run_window_scroll_functions (window, startp);
9688
9689 /* Display the window. Give up if new fonts are loaded, or if point
9690 doesn't appear. */
9691 if (!try_window (window, startp))
9692 rc = SCROLLING_NEED_LARGER_MATRICES;
9693 else if (w->cursor.vpos < 0)
9694 {
9695 clear_glyph_matrix (w->desired_matrix);
9696 rc = SCROLLING_FAILED;
9697 }
9698 else
9699 {
9700 /* Maybe forget recorded base line for line number display. */
9701 if (!just_this_one_p
9702 || current_buffer->clip_changed
9703 || BEG_UNCHANGED < CHARPOS (startp))
9704 w->base_line_number = Qnil;
9705
9706 /* If cursor ends up on a partially visible line, shift display
9707 lines up or down. If that fails because we need larger
9708 matrices, give up. */
9709 if (!make_cursor_line_fully_visible (w))
9710 rc = SCROLLING_NEED_LARGER_MATRICES;
9711 else
9712 rc = SCROLLING_SUCCESS;
9713 }
9714
9715 return rc;
9716 }
9717
9718
9719 /* Compute a suitable window start for window W if display of W starts
9720 on a continuation line. Value is non-zero if a new window start
9721 was computed.
9722
9723 The new window start will be computed, based on W's width, starting
9724 from the start of the continued line. It is the start of the
9725 screen line with the minimum distance from the old start W->start. */
9726
9727 static int
9728 compute_window_start_on_continuation_line (w)
9729 struct window *w;
9730 {
9731 struct text_pos pos, start_pos;
9732 int window_start_changed_p = 0;
9733
9734 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
9735
9736 /* If window start is on a continuation line... Window start may be
9737 < BEGV in case there's invisible text at the start of the
9738 buffer (M-x rmail, for example). */
9739 if (CHARPOS (start_pos) > BEGV
9740 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
9741 {
9742 struct it it;
9743 struct glyph_row *row;
9744
9745 /* Handle the case that the window start is out of range. */
9746 if (CHARPOS (start_pos) < BEGV)
9747 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
9748 else if (CHARPOS (start_pos) > ZV)
9749 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
9750
9751 /* Find the start of the continued line. This should be fast
9752 because scan_buffer is fast (newline cache). */
9753 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
9754 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
9755 row, DEFAULT_FACE_ID);
9756 reseat_at_previous_visible_line_start (&it);
9757
9758 /* If the line start is "too far" away from the window start,
9759 say it takes too much time to compute a new window start. */
9760 if (CHARPOS (start_pos) - IT_CHARPOS (it)
9761 < XFASTINT (w->height) * XFASTINT (w->width))
9762 {
9763 int min_distance, distance;
9764
9765 /* Move forward by display lines to find the new window
9766 start. If window width was enlarged, the new start can
9767 be expected to be > the old start. If window width was
9768 decreased, the new window start will be < the old start.
9769 So, we're looking for the display line start with the
9770 minimum distance from the old window start. */
9771 pos = it.current.pos;
9772 min_distance = INFINITY;
9773 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
9774 distance < min_distance)
9775 {
9776 min_distance = distance;
9777 pos = it.current.pos;
9778 move_it_by_lines (&it, 1, 0);
9779 }
9780
9781 /* Set the window start there. */
9782 SET_MARKER_FROM_TEXT_POS (w->start, pos);
9783 window_start_changed_p = 1;
9784 }
9785 }
9786
9787 return window_start_changed_p;
9788 }
9789
9790
9791 /* Try cursor movement in case text has not changes in window WINDOW,
9792 with window start STARTP. Value is
9793
9794 CURSOR_MOVEMENT_SUCCESS if successful
9795
9796 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
9797
9798 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
9799 display. *SCROLL_STEP is set to 1, under certain circumstances, if
9800 we want to scroll as if scroll-step were set to 1. See the code.
9801
9802 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
9803 which case we have to abort this redisplay, and adjust matrices
9804 first. */
9805
9806 enum
9807 {
9808 CURSOR_MOVEMENT_SUCCESS,
9809 CURSOR_MOVEMENT_CANNOT_BE_USED,
9810 CURSOR_MOVEMENT_MUST_SCROLL,
9811 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
9812 };
9813
9814 static int
9815 try_cursor_movement (window, startp, scroll_step)
9816 Lisp_Object window;
9817 struct text_pos startp;
9818 int *scroll_step;
9819 {
9820 struct window *w = XWINDOW (window);
9821 struct frame *f = XFRAME (w->frame);
9822 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
9823
9824 #if GLYPH_DEBUG
9825 if (inhibit_try_cursor_movement)
9826 return rc;
9827 #endif
9828
9829 /* Handle case where text has not changed, only point, and it has
9830 not moved off the frame. */
9831 if (/* Point may be in this window. */
9832 PT >= CHARPOS (startp)
9833 /* Selective display hasn't changed. */
9834 && !current_buffer->clip_changed
9835 /* Function force-mode-line-update is used to force a thorough
9836 redisplay. It sets either windows_or_buffers_changed or
9837 update_mode_lines. So don't take a shortcut here for these
9838 cases. */
9839 && !update_mode_lines
9840 && !windows_or_buffers_changed
9841 /* Can't use this case if highlighting a region. When a
9842 region exists, cursor movement has to do more than just
9843 set the cursor. */
9844 && !(!NILP (Vtransient_mark_mode)
9845 && !NILP (current_buffer->mark_active))
9846 && NILP (w->region_showing)
9847 && NILP (Vshow_trailing_whitespace)
9848 /* Right after splitting windows, last_point may be nil. */
9849 && INTEGERP (w->last_point)
9850 /* This code is not used for mini-buffer for the sake of the case
9851 of redisplaying to replace an echo area message; since in
9852 that case the mini-buffer contents per se are usually
9853 unchanged. This code is of no real use in the mini-buffer
9854 since the handling of this_line_start_pos, etc., in redisplay
9855 handles the same cases. */
9856 && !EQ (window, minibuf_window)
9857 /* When splitting windows or for new windows, it happens that
9858 redisplay is called with a nil window_end_vpos or one being
9859 larger than the window. This should really be fixed in
9860 window.c. I don't have this on my list, now, so we do
9861 approximately the same as the old redisplay code. --gerd. */
9862 && INTEGERP (w->window_end_vpos)
9863 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
9864 && (FRAME_WINDOW_P (f)
9865 || !MARKERP (Voverlay_arrow_position)
9866 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
9867 {
9868 int this_scroll_margin;
9869 struct glyph_row *row = NULL;
9870
9871 #if GLYPH_DEBUG
9872 debug_method_add (w, "cursor movement");
9873 #endif
9874
9875 /* Scroll if point within this distance from the top or bottom
9876 of the window. This is a pixel value. */
9877 this_scroll_margin = max (0, scroll_margin);
9878 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
9879 this_scroll_margin *= CANON_Y_UNIT (f);
9880
9881 /* Start with the row the cursor was displayed during the last
9882 not paused redisplay. Give up if that row is not valid. */
9883 if (w->last_cursor.vpos < 0
9884 || w->last_cursor.vpos >= w->current_matrix->nrows)
9885 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9886 else
9887 {
9888 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
9889 if (row->mode_line_p)
9890 ++row;
9891 if (!row->enabled_p)
9892 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9893 }
9894
9895 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
9896 {
9897 int scroll_p = 0;
9898 int last_y = window_text_bottom_y (w) - this_scroll_margin;
9899
9900 if (PT > XFASTINT (w->last_point))
9901 {
9902 /* Point has moved forward. */
9903 while (MATRIX_ROW_END_CHARPOS (row) < PT
9904 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
9905 {
9906 xassert (row->enabled_p);
9907 ++row;
9908 }
9909
9910 /* The end position of a row equals the start position
9911 of the next row. If PT is there, we would rather
9912 display it in the next line. */
9913 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9914 && MATRIX_ROW_END_CHARPOS (row) == PT
9915 && !cursor_row_p (w, row))
9916 ++row;
9917
9918 /* If within the scroll margin, scroll. Note that
9919 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
9920 the next line would be drawn, and that
9921 this_scroll_margin can be zero. */
9922 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
9923 || PT > MATRIX_ROW_END_CHARPOS (row)
9924 /* Line is completely visible last line in window
9925 and PT is to be set in the next line. */
9926 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
9927 && PT == MATRIX_ROW_END_CHARPOS (row)
9928 && !row->ends_at_zv_p
9929 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
9930 scroll_p = 1;
9931 }
9932 else if (PT < XFASTINT (w->last_point))
9933 {
9934 /* Cursor has to be moved backward. Note that PT >=
9935 CHARPOS (startp) because of the outer
9936 if-statement. */
9937 while (!row->mode_line_p
9938 && (MATRIX_ROW_START_CHARPOS (row) > PT
9939 || (MATRIX_ROW_START_CHARPOS (row) == PT
9940 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
9941 && (row->y > this_scroll_margin
9942 || CHARPOS (startp) == BEGV))
9943 {
9944 xassert (row->enabled_p);
9945 --row;
9946 }
9947
9948 /* Consider the following case: Window starts at BEGV,
9949 there is invisible, intangible text at BEGV, so that
9950 display starts at some point START > BEGV. It can
9951 happen that we are called with PT somewhere between
9952 BEGV and START. Try to handle that case. */
9953 if (row < w->current_matrix->rows
9954 || row->mode_line_p)
9955 {
9956 row = w->current_matrix->rows;
9957 if (row->mode_line_p)
9958 ++row;
9959 }
9960
9961 /* Due to newlines in overlay strings, we may have to
9962 skip forward over overlay strings. */
9963 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9964 && MATRIX_ROW_END_CHARPOS (row) == PT
9965 && !cursor_row_p (w, row))
9966 ++row;
9967
9968 /* If within the scroll margin, scroll. */
9969 if (row->y < this_scroll_margin
9970 && CHARPOS (startp) != BEGV)
9971 scroll_p = 1;
9972 }
9973
9974 if (PT < MATRIX_ROW_START_CHARPOS (row)
9975 || PT > MATRIX_ROW_END_CHARPOS (row))
9976 {
9977 /* if PT is not in the glyph row, give up. */
9978 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9979 }
9980 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
9981 {
9982 if (PT == MATRIX_ROW_END_CHARPOS (row)
9983 && !row->ends_at_zv_p
9984 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
9985 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9986 else if (row->height > window_box_height (w))
9987 {
9988 /* If we end up in a partially visible line, let's
9989 make it fully visible, except when it's taller
9990 than the window, in which case we can't do much
9991 about it. */
9992 *scroll_step = 1;
9993 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9994 }
9995 else
9996 {
9997 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9998 try_window (window, startp);
9999 if (!make_cursor_line_fully_visible (w))
10000 rc = CURSOR_MOVEMENT_NEED_LARGER_MATRICES;
10001 else
10002 rc = CURSOR_MOVEMENT_SUCCESS;
10003 }
10004 }
10005 else if (scroll_p)
10006 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10007 else
10008 {
10009 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10010 rc = CURSOR_MOVEMENT_SUCCESS;
10011 }
10012 }
10013 }
10014
10015 return rc;
10016 }
10017
10018
10019 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
10020 selected_window is redisplayed. */
10021
10022 static void
10023 redisplay_window (window, just_this_one_p)
10024 Lisp_Object window;
10025 int just_this_one_p;
10026 {
10027 struct window *w = XWINDOW (window);
10028 struct frame *f = XFRAME (w->frame);
10029 struct buffer *buffer = XBUFFER (w->buffer);
10030 struct buffer *old = current_buffer;
10031 struct text_pos lpoint, opoint, startp;
10032 int update_mode_line;
10033 int tem;
10034 struct it it;
10035 /* Record it now because it's overwritten. */
10036 int current_matrix_up_to_date_p = 0;
10037 int temp_scroll_step = 0;
10038 int count = BINDING_STACK_SIZE ();
10039 int rc;
10040
10041 SET_TEXT_POS (lpoint, PT, PT_BYTE);
10042 opoint = lpoint;
10043
10044 /* W must be a leaf window here. */
10045 xassert (!NILP (w->buffer));
10046 #if GLYPH_DEBUG
10047 *w->desired_matrix->method = 0;
10048 #endif
10049
10050 specbind (Qinhibit_point_motion_hooks, Qt);
10051
10052 reconsider_clip_changes (w, buffer);
10053
10054 /* Has the mode line to be updated? */
10055 update_mode_line = (!NILP (w->update_mode_line)
10056 || update_mode_lines
10057 || buffer->clip_changed);
10058
10059 if (MINI_WINDOW_P (w))
10060 {
10061 if (w == XWINDOW (echo_area_window)
10062 && !NILP (echo_area_buffer[0]))
10063 {
10064 if (update_mode_line)
10065 /* We may have to update a tty frame's menu bar or a
10066 tool-bar. Example `M-x C-h C-h C-g'. */
10067 goto finish_menu_bars;
10068 else
10069 /* We've already displayed the echo area glyphs in this window. */
10070 goto finish_scroll_bars;
10071 }
10072 else if (w != XWINDOW (minibuf_window))
10073 {
10074 /* W is a mini-buffer window, but it's not the currently
10075 active one, so clear it. */
10076 int yb = window_text_bottom_y (w);
10077 struct glyph_row *row;
10078 int y;
10079
10080 for (y = 0, row = w->desired_matrix->rows;
10081 y < yb;
10082 y += row->height, ++row)
10083 blank_row (w, row, y);
10084 goto finish_scroll_bars;
10085 }
10086
10087 clear_glyph_matrix (w->desired_matrix);
10088 }
10089
10090 /* Otherwise set up data on this window; select its buffer and point
10091 value. */
10092 /* Really select the buffer, for the sake of buffer-local
10093 variables. */
10094 set_buffer_internal_1 (XBUFFER (w->buffer));
10095 SET_TEXT_POS (opoint, PT, PT_BYTE);
10096
10097 current_matrix_up_to_date_p
10098 = (!NILP (w->window_end_valid)
10099 && !current_buffer->clip_changed
10100 && XFASTINT (w->last_modified) >= MODIFF
10101 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
10102
10103 /* When windows_or_buffers_changed is non-zero, we can't rely on
10104 the window end being valid, so set it to nil there. */
10105 if (windows_or_buffers_changed)
10106 {
10107 /* If window starts on a continuation line, maybe adjust the
10108 window start in case the window's width changed. */
10109 if (XMARKER (w->start)->buffer == current_buffer)
10110 compute_window_start_on_continuation_line (w);
10111
10112 w->window_end_valid = Qnil;
10113 }
10114
10115 /* Some sanity checks. */
10116 CHECK_WINDOW_END (w);
10117 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
10118 abort ();
10119 if (BYTEPOS (opoint) < CHARPOS (opoint))
10120 abort ();
10121
10122 /* If %c is in mode line, update it if needed. */
10123 if (!NILP (w->column_number_displayed)
10124 /* This alternative quickly identifies a common case
10125 where no change is needed. */
10126 && !(PT == XFASTINT (w->last_point)
10127 && XFASTINT (w->last_modified) >= MODIFF
10128 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
10129 && XFASTINT (w->column_number_displayed) != current_column ())
10130 update_mode_line = 1;
10131
10132 /* Count number of windows showing the selected buffer. An indirect
10133 buffer counts as its base buffer. */
10134 if (!just_this_one_p)
10135 {
10136 struct buffer *current_base, *window_base;
10137 current_base = current_buffer;
10138 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
10139 if (current_base->base_buffer)
10140 current_base = current_base->base_buffer;
10141 if (window_base->base_buffer)
10142 window_base = window_base->base_buffer;
10143 if (current_base == window_base)
10144 buffer_shared++;
10145 }
10146
10147 /* Point refers normally to the selected window. For any other
10148 window, set up appropriate value. */
10149 if (!EQ (window, selected_window))
10150 {
10151 int new_pt = XMARKER (w->pointm)->charpos;
10152 int new_pt_byte = marker_byte_position (w->pointm);
10153 if (new_pt < BEGV)
10154 {
10155 new_pt = BEGV;
10156 new_pt_byte = BEGV_BYTE;
10157 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
10158 }
10159 else if (new_pt > (ZV - 1))
10160 {
10161 new_pt = ZV;
10162 new_pt_byte = ZV_BYTE;
10163 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
10164 }
10165
10166 /* We don't use SET_PT so that the point-motion hooks don't run. */
10167 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
10168 }
10169
10170 /* If any of the character widths specified in the display table
10171 have changed, invalidate the width run cache. It's true that
10172 this may be a bit late to catch such changes, but the rest of
10173 redisplay goes (non-fatally) haywire when the display table is
10174 changed, so why should we worry about doing any better? */
10175 if (current_buffer->width_run_cache)
10176 {
10177 struct Lisp_Char_Table *disptab = buffer_display_table ();
10178
10179 if (! disptab_matches_widthtab (disptab,
10180 XVECTOR (current_buffer->width_table)))
10181 {
10182 invalidate_region_cache (current_buffer,
10183 current_buffer->width_run_cache,
10184 BEG, Z);
10185 recompute_width_table (current_buffer, disptab);
10186 }
10187 }
10188
10189 /* If window-start is screwed up, choose a new one. */
10190 if (XMARKER (w->start)->buffer != current_buffer)
10191 goto recenter;
10192
10193 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10194
10195 /* If someone specified a new starting point but did not insist,
10196 check whether it can be used. */
10197 if (!NILP (w->optional_new_start)
10198 && CHARPOS (startp) >= BEGV
10199 && CHARPOS (startp) <= ZV)
10200 {
10201 w->optional_new_start = Qnil;
10202 start_display (&it, w, startp);
10203 move_it_to (&it, PT, 0, it.last_visible_y, -1,
10204 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
10205 if (IT_CHARPOS (it) == PT)
10206 w->force_start = Qt;
10207 }
10208
10209 /* Handle case where place to start displaying has been specified,
10210 unless the specified location is outside the accessible range. */
10211 if (!NILP (w->force_start)
10212 || w->frozen_window_start_p)
10213 {
10214 w->force_start = Qnil;
10215 w->vscroll = 0;
10216 w->window_end_valid = Qnil;
10217
10218 /* Forget any recorded base line for line number display. */
10219 if (!current_matrix_up_to_date_p
10220 || current_buffer->clip_changed)
10221 w->base_line_number = Qnil;
10222
10223 /* Redisplay the mode line. Select the buffer properly for that.
10224 Also, run the hook window-scroll-functions
10225 because we have scrolled. */
10226 /* Note, we do this after clearing force_start because
10227 if there's an error, it is better to forget about force_start
10228 than to get into an infinite loop calling the hook functions
10229 and having them get more errors. */
10230 if (!update_mode_line
10231 || ! NILP (Vwindow_scroll_functions))
10232 {
10233 update_mode_line = 1;
10234 w->update_mode_line = Qt;
10235 startp = run_window_scroll_functions (window, startp);
10236 }
10237
10238 w->last_modified = make_number (0);
10239 w->last_overlay_modified = make_number (0);
10240 if (CHARPOS (startp) < BEGV)
10241 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
10242 else if (CHARPOS (startp) > ZV)
10243 SET_TEXT_POS (startp, ZV, ZV_BYTE);
10244
10245 /* Redisplay, then check if cursor has been set during the
10246 redisplay. Give up if new fonts were loaded. */
10247 if (!try_window (window, startp))
10248 {
10249 w->force_start = Qt;
10250 clear_glyph_matrix (w->desired_matrix);
10251 goto finish_scroll_bars;
10252 }
10253
10254 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
10255 {
10256 /* If point does not appear, try to move point so it does
10257 appear. The desired matrix has been built above, so we
10258 can use it here. */
10259 int window_height;
10260 struct glyph_row *row;
10261
10262 window_height = window_box_height (w) / 2;
10263 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
10264 while (MATRIX_ROW_BOTTOM_Y (row) < window_height)
10265 ++row;
10266
10267 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
10268 MATRIX_ROW_START_BYTEPOS (row));
10269
10270 if (w != XWINDOW (selected_window))
10271 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
10272 else if (current_buffer == old)
10273 SET_TEXT_POS (lpoint, PT, PT_BYTE);
10274
10275 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
10276
10277 /* If we are highlighting the region, then we just changed
10278 the region, so redisplay to show it. */
10279 if (!NILP (Vtransient_mark_mode)
10280 && !NILP (current_buffer->mark_active))
10281 {
10282 clear_glyph_matrix (w->desired_matrix);
10283 if (!try_window (window, startp))
10284 goto need_larger_matrices;
10285 }
10286 }
10287
10288 if (!make_cursor_line_fully_visible (w))
10289 goto need_larger_matrices;
10290 #if GLYPH_DEBUG
10291 debug_method_add (w, "forced window start");
10292 #endif
10293 goto done;
10294 }
10295
10296 /* Handle case where text has not changed, only point, and it has
10297 not moved off the frame. */
10298 if (current_matrix_up_to_date_p
10299 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
10300 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
10301 {
10302 switch (rc)
10303 {
10304 case CURSOR_MOVEMENT_SUCCESS:
10305 goto done;
10306
10307 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
10308 goto need_larger_matrices;
10309
10310 case CURSOR_MOVEMENT_MUST_SCROLL:
10311 goto try_to_scroll;
10312
10313 default:
10314 abort ();
10315 }
10316 }
10317 /* If current starting point was originally the beginning of a line
10318 but no longer is, find a new starting point. */
10319 else if (!NILP (w->start_at_line_beg)
10320 && !(CHARPOS (startp) <= BEGV
10321 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
10322 {
10323 #if GLYPH_DEBUG
10324 debug_method_add (w, "recenter 1");
10325 #endif
10326 goto recenter;
10327 }
10328
10329 /* Try scrolling with try_window_id. Value is > 0 if update has
10330 been done, it is -1 if we know that the same window start will
10331 not work. It is 0 if unsuccessful for some other reason. */
10332 else if ((tem = try_window_id (w)) != 0)
10333 {
10334 #if GLYPH_DEBUG
10335 debug_method_add (w, "try_window_id %d", tem);
10336 #endif
10337
10338 if (fonts_changed_p)
10339 goto need_larger_matrices;
10340 if (tem > 0)
10341 goto done;
10342
10343 /* Otherwise try_window_id has returned -1 which means that we
10344 don't want the alternative below this comment to execute. */
10345 }
10346 else if (CHARPOS (startp) >= BEGV
10347 && CHARPOS (startp) <= ZV
10348 && PT >= CHARPOS (startp)
10349 && (CHARPOS (startp) < ZV
10350 /* Avoid starting at end of buffer. */
10351 || CHARPOS (startp) == BEGV
10352 || (XFASTINT (w->last_modified) >= MODIFF
10353 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
10354 {
10355 #if GLYPH_DEBUG
10356 debug_method_add (w, "same window start");
10357 #endif
10358
10359 /* Try to redisplay starting at same place as before.
10360 If point has not moved off frame, accept the results. */
10361 if (!current_matrix_up_to_date_p
10362 /* Don't use try_window_reusing_current_matrix in this case
10363 because a window scroll function can have changed the
10364 buffer. */
10365 || !NILP (Vwindow_scroll_functions)
10366 || MINI_WINDOW_P (w)
10367 || !try_window_reusing_current_matrix (w))
10368 {
10369 IF_DEBUG (debug_method_add (w, "1"));
10370 try_window (window, startp);
10371 }
10372
10373 if (fonts_changed_p)
10374 goto need_larger_matrices;
10375
10376 if (w->cursor.vpos >= 0)
10377 {
10378 if (!just_this_one_p
10379 || current_buffer->clip_changed
10380 || BEG_UNCHANGED < CHARPOS (startp))
10381 /* Forget any recorded base line for line number display. */
10382 w->base_line_number = Qnil;
10383
10384 if (!make_cursor_line_fully_visible (w))
10385 goto need_larger_matrices;
10386 goto done;
10387 }
10388 else
10389 clear_glyph_matrix (w->desired_matrix);
10390 }
10391
10392 try_to_scroll:
10393
10394 w->last_modified = make_number (0);
10395 w->last_overlay_modified = make_number (0);
10396
10397 /* Redisplay the mode line. Select the buffer properly for that. */
10398 if (!update_mode_line)
10399 {
10400 update_mode_line = 1;
10401 w->update_mode_line = Qt;
10402 }
10403
10404 /* Try to scroll by specified few lines. */
10405 if ((scroll_conservatively
10406 || scroll_step
10407 || temp_scroll_step
10408 || NUMBERP (current_buffer->scroll_up_aggressively)
10409 || NUMBERP (current_buffer->scroll_down_aggressively))
10410 && !current_buffer->clip_changed
10411 && CHARPOS (startp) >= BEGV
10412 && CHARPOS (startp) <= ZV)
10413 {
10414 /* The function returns -1 if new fonts were loaded, 1 if
10415 successful, 0 if not successful. */
10416 int rc = try_scrolling (window, just_this_one_p,
10417 scroll_conservatively,
10418 scroll_step,
10419 temp_scroll_step);
10420 switch (rc)
10421 {
10422 case SCROLLING_SUCCESS:
10423 goto done;
10424
10425 case SCROLLING_NEED_LARGER_MATRICES:
10426 goto need_larger_matrices;
10427
10428 case SCROLLING_FAILED:
10429 break;
10430
10431 default:
10432 abort ();
10433 }
10434 }
10435
10436 /* Finally, just choose place to start which centers point */
10437
10438 recenter:
10439
10440 #if GLYPH_DEBUG
10441 debug_method_add (w, "recenter");
10442 #endif
10443
10444 /* w->vscroll = 0; */
10445
10446 /* Forget any previously recorded base line for line number display. */
10447 if (!current_matrix_up_to_date_p
10448 || current_buffer->clip_changed)
10449 w->base_line_number = Qnil;
10450
10451 /* Move backward half the height of the window. */
10452 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10453 it.current_y = it.last_visible_y;
10454 move_it_vertically_backward (&it, window_box_height (w) / 2);
10455 xassert (IT_CHARPOS (it) >= BEGV);
10456
10457 /* The function move_it_vertically_backward may move over more
10458 than the specified y-distance. If it->w is small, e.g. a
10459 mini-buffer window, we may end up in front of the window's
10460 display area. Start displaying at the start of the line
10461 containing PT in this case. */
10462 if (it.current_y <= 0)
10463 {
10464 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10465 move_it_vertically (&it, 0);
10466 xassert (IT_CHARPOS (it) <= PT);
10467 it.current_y = 0;
10468 }
10469
10470 it.current_x = it.hpos = 0;
10471
10472 /* Set startp here explicitly in case that helps avoid an infinite loop
10473 in case the window-scroll-functions functions get errors. */
10474 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
10475
10476 /* Run scroll hooks. */
10477 startp = run_window_scroll_functions (window, it.current.pos);
10478
10479 /* Redisplay the window. */
10480 if (!current_matrix_up_to_date_p
10481 || windows_or_buffers_changed
10482 /* Don't use try_window_reusing_current_matrix in this case
10483 because it can have changed the buffer. */
10484 || !NILP (Vwindow_scroll_functions)
10485 || !just_this_one_p
10486 || MINI_WINDOW_P (w)
10487 || !try_window_reusing_current_matrix (w))
10488 try_window (window, startp);
10489
10490 /* If new fonts have been loaded (due to fontsets), give up. We
10491 have to start a new redisplay since we need to re-adjust glyph
10492 matrices. */
10493 if (fonts_changed_p)
10494 goto need_larger_matrices;
10495
10496 /* If cursor did not appear assume that the middle of the window is
10497 in the first line of the window. Do it again with the next line.
10498 (Imagine a window of height 100, displaying two lines of height
10499 60. Moving back 50 from it->last_visible_y will end in the first
10500 line.) */
10501 if (w->cursor.vpos < 0)
10502 {
10503 if (!NILP (w->window_end_valid)
10504 && PT >= Z - XFASTINT (w->window_end_pos))
10505 {
10506 clear_glyph_matrix (w->desired_matrix);
10507 move_it_by_lines (&it, 1, 0);
10508 try_window (window, it.current.pos);
10509 }
10510 else if (PT < IT_CHARPOS (it))
10511 {
10512 clear_glyph_matrix (w->desired_matrix);
10513 move_it_by_lines (&it, -1, 0);
10514 try_window (window, it.current.pos);
10515 }
10516 else
10517 {
10518 /* Not much we can do about it. */
10519 }
10520 }
10521
10522 /* Consider the following case: Window starts at BEGV, there is
10523 invisible, intangible text at BEGV, so that display starts at
10524 some point START > BEGV. It can happen that we are called with
10525 PT somewhere between BEGV and START. Try to handle that case. */
10526 if (w->cursor.vpos < 0)
10527 {
10528 struct glyph_row *row = w->current_matrix->rows;
10529 if (row->mode_line_p)
10530 ++row;
10531 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10532 }
10533
10534 if (!make_cursor_line_fully_visible (w))
10535 goto need_larger_matrices;
10536
10537 done:
10538
10539 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10540 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
10541 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
10542 ? Qt : Qnil);
10543
10544 /* Display the mode line, if we must. */
10545 if ((update_mode_line
10546 /* If window not full width, must redo its mode line
10547 if (a) the window to its side is being redone and
10548 (b) we do a frame-based redisplay. This is a consequence
10549 of how inverted lines are drawn in frame-based redisplay. */
10550 || (!just_this_one_p
10551 && !FRAME_WINDOW_P (f)
10552 && !WINDOW_FULL_WIDTH_P (w))
10553 /* Line number to display. */
10554 || INTEGERP (w->base_line_pos)
10555 /* Column number is displayed and different from the one displayed. */
10556 || (!NILP (w->column_number_displayed)
10557 && XFASTINT (w->column_number_displayed) != current_column ()))
10558 /* This means that the window has a mode line. */
10559 && (WINDOW_WANTS_MODELINE_P (w)
10560 || WINDOW_WANTS_HEADER_LINE_P (w)))
10561 {
10562 display_mode_lines (w);
10563
10564 /* If mode line height has changed, arrange for a thorough
10565 immediate redisplay using the correct mode line height. */
10566 if (WINDOW_WANTS_MODELINE_P (w)
10567 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
10568 {
10569 fonts_changed_p = 1;
10570 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
10571 = DESIRED_MODE_LINE_HEIGHT (w);
10572 }
10573
10574 /* If top line height has changed, arrange for a thorough
10575 immediate redisplay using the correct mode line height. */
10576 if (WINDOW_WANTS_HEADER_LINE_P (w)
10577 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
10578 {
10579 fonts_changed_p = 1;
10580 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
10581 = DESIRED_HEADER_LINE_HEIGHT (w);
10582 }
10583
10584 if (fonts_changed_p)
10585 goto need_larger_matrices;
10586 }
10587
10588 if (!line_number_displayed
10589 && !BUFFERP (w->base_line_pos))
10590 {
10591 w->base_line_pos = Qnil;
10592 w->base_line_number = Qnil;
10593 }
10594
10595 finish_menu_bars:
10596
10597 /* When we reach a frame's selected window, redo the frame's menu bar. */
10598 if (update_mode_line
10599 && EQ (FRAME_SELECTED_WINDOW (f), window))
10600 {
10601 int redisplay_menu_p = 0;
10602
10603 if (FRAME_WINDOW_P (f))
10604 {
10605 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
10606 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
10607 #else
10608 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10609 #endif
10610 }
10611 else
10612 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10613
10614 if (redisplay_menu_p)
10615 display_menu_bar (w);
10616
10617 #ifdef HAVE_WINDOW_SYSTEM
10618 if (WINDOWP (f->tool_bar_window)
10619 && (FRAME_TOOL_BAR_LINES (f) > 0
10620 || auto_resize_tool_bars_p))
10621 redisplay_tool_bar (f);
10622 #endif
10623 }
10624
10625 need_larger_matrices:
10626 ;
10627 finish_scroll_bars:
10628
10629 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
10630 {
10631 int start, end, whole;
10632
10633 /* Calculate the start and end positions for the current window.
10634 At some point, it would be nice to choose between scrollbars
10635 which reflect the whole buffer size, with special markers
10636 indicating narrowing, and scrollbars which reflect only the
10637 visible region.
10638
10639 Note that mini-buffers sometimes aren't displaying any text. */
10640 if (!MINI_WINDOW_P (w)
10641 || (w == XWINDOW (minibuf_window)
10642 && NILP (echo_area_buffer[0])))
10643 {
10644 whole = ZV - BEGV;
10645 start = marker_position (w->start) - BEGV;
10646 /* I don't think this is guaranteed to be right. For the
10647 moment, we'll pretend it is. */
10648 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
10649
10650 if (end < start)
10651 end = start;
10652 if (whole < (end - start))
10653 whole = end - start;
10654 }
10655 else
10656 start = end = whole = 0;
10657
10658 /* Indicate what this scroll bar ought to be displaying now. */
10659 set_vertical_scroll_bar_hook (w, end - start, whole, start);
10660
10661 /* Note that we actually used the scroll bar attached to this
10662 window, so it shouldn't be deleted at the end of redisplay. */
10663 redeem_scroll_bar_hook (w);
10664 }
10665
10666 /* Restore current_buffer and value of point in it. */
10667 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
10668 set_buffer_internal_1 (old);
10669 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
10670
10671 unbind_to (count, Qnil);
10672 }
10673
10674
10675 /* Build the complete desired matrix of WINDOW with a window start
10676 buffer position POS. Value is non-zero if successful. It is zero
10677 if fonts were loaded during redisplay which makes re-adjusting
10678 glyph matrices necessary. */
10679
10680 int
10681 try_window (window, pos)
10682 Lisp_Object window;
10683 struct text_pos pos;
10684 {
10685 struct window *w = XWINDOW (window);
10686 struct it it;
10687 struct glyph_row *last_text_row = NULL;
10688
10689 /* Make POS the new window start. */
10690 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
10691
10692 /* Mark cursor position as unknown. No overlay arrow seen. */
10693 w->cursor.vpos = -1;
10694 overlay_arrow_seen = 0;
10695
10696 /* Initialize iterator and info to start at POS. */
10697 start_display (&it, w, pos);
10698
10699 /* Display all lines of W. */
10700 while (it.current_y < it.last_visible_y)
10701 {
10702 if (display_line (&it))
10703 last_text_row = it.glyph_row - 1;
10704 if (fonts_changed_p)
10705 return 0;
10706 }
10707
10708 /* If bottom moved off end of frame, change mode line percentage. */
10709 if (XFASTINT (w->window_end_pos) <= 0
10710 && Z != IT_CHARPOS (it))
10711 w->update_mode_line = Qt;
10712
10713 /* Set window_end_pos to the offset of the last character displayed
10714 on the window from the end of current_buffer. Set
10715 window_end_vpos to its row number. */
10716 if (last_text_row)
10717 {
10718 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
10719 w->window_end_bytepos
10720 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10721 w->window_end_pos
10722 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10723 w->window_end_vpos
10724 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10725 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
10726 ->displays_text_p);
10727 }
10728 else
10729 {
10730 w->window_end_bytepos = 0;
10731 w->window_end_pos = w->window_end_vpos = make_number (0);
10732 }
10733
10734 /* But that is not valid info until redisplay finishes. */
10735 w->window_end_valid = Qnil;
10736 return 1;
10737 }
10738
10739
10740 \f
10741 /************************************************************************
10742 Window redisplay reusing current matrix when buffer has not changed
10743 ************************************************************************/
10744
10745 /* Try redisplay of window W showing an unchanged buffer with a
10746 different window start than the last time it was displayed by
10747 reusing its current matrix. Value is non-zero if successful.
10748 W->start is the new window start. */
10749
10750 static int
10751 try_window_reusing_current_matrix (w)
10752 struct window *w;
10753 {
10754 struct frame *f = XFRAME (w->frame);
10755 struct glyph_row *row, *bottom_row;
10756 struct it it;
10757 struct run run;
10758 struct text_pos start, new_start;
10759 int nrows_scrolled, i;
10760 struct glyph_row *last_text_row;
10761 struct glyph_row *last_reused_text_row;
10762 struct glyph_row *start_row;
10763 int start_vpos, min_y, max_y;
10764
10765 #if GLYPH_DEBUG
10766 if (inhibit_try_window_reusing)
10767 return 0;
10768 #endif
10769
10770 if (/* This function doesn't handle terminal frames. */
10771 !FRAME_WINDOW_P (f)
10772 /* Don't try to reuse the display if windows have been split
10773 or such. */
10774 || windows_or_buffers_changed)
10775 return 0;
10776
10777 /* Can't do this if region may have changed. */
10778 if ((!NILP (Vtransient_mark_mode)
10779 && !NILP (current_buffer->mark_active))
10780 || !NILP (w->region_showing)
10781 || !NILP (Vshow_trailing_whitespace))
10782 return 0;
10783
10784 /* If top-line visibility has changed, give up. */
10785 if (WINDOW_WANTS_HEADER_LINE_P (w)
10786 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
10787 return 0;
10788
10789 /* Give up if old or new display is scrolled vertically. We could
10790 make this function handle this, but right now it doesn't. */
10791 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10792 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
10793 return 0;
10794
10795 /* The variable new_start now holds the new window start. The old
10796 start `start' can be determined from the current matrix. */
10797 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
10798 start = start_row->start.pos;
10799 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
10800
10801 /* Clear the desired matrix for the display below. */
10802 clear_glyph_matrix (w->desired_matrix);
10803
10804 if (CHARPOS (new_start) <= CHARPOS (start))
10805 {
10806 int first_row_y;
10807
10808 /* Don't use this method if the display starts with an ellipsis
10809 displayed for invisible text. It's not easy to handle that case
10810 below, and it's certainly not worth the effort since this is
10811 not a frequent case. */
10812 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
10813 return 0;
10814
10815 IF_DEBUG (debug_method_add (w, "twu1"));
10816
10817 /* Display up to a row that can be reused. The variable
10818 last_text_row is set to the last row displayed that displays
10819 text. Note that it.vpos == 0 if or if not there is a
10820 header-line; it's not the same as the MATRIX_ROW_VPOS! */
10821 start_display (&it, w, new_start);
10822 first_row_y = it.current_y;
10823 w->cursor.vpos = -1;
10824 last_text_row = last_reused_text_row = NULL;
10825
10826 while (it.current_y < it.last_visible_y
10827 && IT_CHARPOS (it) < CHARPOS (start)
10828 && !fonts_changed_p)
10829 if (display_line (&it))
10830 last_text_row = it.glyph_row - 1;
10831
10832 /* A value of current_y < last_visible_y means that we stopped
10833 at the previous window start, which in turn means that we
10834 have at least one reusable row. */
10835 if (it.current_y < it.last_visible_y)
10836 {
10837 /* IT.vpos always starts from 0; it counts text lines. */
10838 nrows_scrolled = it.vpos;
10839
10840 /* Find PT if not already found in the lines displayed. */
10841 if (w->cursor.vpos < 0)
10842 {
10843 int dy = it.current_y - first_row_y;
10844
10845 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10846 row = row_containing_pos (w, PT, row, NULL, dy);
10847 if (row)
10848 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
10849 dy, nrows_scrolled);
10850 else
10851 {
10852 clear_glyph_matrix (w->desired_matrix);
10853 return 0;
10854 }
10855 }
10856
10857 /* Scroll the display. Do it before the current matrix is
10858 changed. The problem here is that update has not yet
10859 run, i.e. part of the current matrix is not up to date.
10860 scroll_run_hook will clear the cursor, and use the
10861 current matrix to get the height of the row the cursor is
10862 in. */
10863 run.current_y = first_row_y;
10864 run.desired_y = it.current_y;
10865 run.height = it.last_visible_y - it.current_y;
10866
10867 if (run.height > 0 && run.current_y != run.desired_y)
10868 {
10869 update_begin (f);
10870 rif->update_window_begin_hook (w);
10871 rif->clear_mouse_face (w);
10872 rif->scroll_run_hook (w, &run);
10873 rif->update_window_end_hook (w, 0, 0);
10874 update_end (f);
10875 }
10876
10877 /* Shift current matrix down by nrows_scrolled lines. */
10878 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
10879 rotate_matrix (w->current_matrix,
10880 start_vpos,
10881 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10882 nrows_scrolled);
10883
10884 /* Disable lines that must be updated. */
10885 for (i = 0; i < it.vpos; ++i)
10886 (start_row + i)->enabled_p = 0;
10887
10888 /* Re-compute Y positions. */
10889 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10890 max_y = it.last_visible_y;
10891 for (row = start_row + nrows_scrolled;
10892 row < bottom_row;
10893 ++row)
10894 {
10895 row->y = it.current_y;
10896 row->visible_height = row->height;
10897
10898 if (row->y < min_y)
10899 row->visible_height -= min_y - row->y;
10900 if (row->y + row->height > max_y)
10901 row->visible_height -= row->y + row->height - max_y;
10902
10903 it.current_y += row->height;
10904
10905 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10906 last_reused_text_row = row;
10907 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
10908 break;
10909 }
10910
10911 /* Disable lines in the current matrix which are now
10912 below the window. */
10913 for (++row; row < bottom_row; ++row)
10914 row->enabled_p = 0;
10915 }
10916
10917 /* Update window_end_pos etc.; last_reused_text_row is the last
10918 reused row from the current matrix containing text, if any.
10919 The value of last_text_row is the last displayed line
10920 containing text. */
10921 if (last_reused_text_row)
10922 {
10923 w->window_end_bytepos
10924 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
10925 w->window_end_pos
10926 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
10927 w->window_end_vpos
10928 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
10929 w->current_matrix));
10930 }
10931 else if (last_text_row)
10932 {
10933 w->window_end_bytepos
10934 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10935 w->window_end_pos
10936 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10937 w->window_end_vpos
10938 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10939 }
10940 else
10941 {
10942 /* This window must be completely empty. */
10943 w->window_end_bytepos = 0;
10944 w->window_end_pos = w->window_end_vpos = make_number (0);
10945 }
10946 w->window_end_valid = Qnil;
10947
10948 /* Update hint: don't try scrolling again in update_window. */
10949 w->desired_matrix->no_scrolling_p = 1;
10950
10951 #if GLYPH_DEBUG
10952 debug_method_add (w, "try_window_reusing_current_matrix 1");
10953 #endif
10954 return 1;
10955 }
10956 else if (CHARPOS (new_start) > CHARPOS (start))
10957 {
10958 struct glyph_row *pt_row, *row;
10959 struct glyph_row *first_reusable_row;
10960 struct glyph_row *first_row_to_display;
10961 int dy;
10962 int yb = window_text_bottom_y (w);
10963
10964 /* Find the row starting at new_start, if there is one. Don't
10965 reuse a partially visible line at the end. */
10966 first_reusable_row = start_row;
10967 while (first_reusable_row->enabled_p
10968 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
10969 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
10970 < CHARPOS (new_start)))
10971 ++first_reusable_row;
10972
10973 /* Give up if there is no row to reuse. */
10974 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
10975 || !first_reusable_row->enabled_p
10976 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
10977 != CHARPOS (new_start)))
10978 return 0;
10979
10980 /* We can reuse fully visible rows beginning with
10981 first_reusable_row to the end of the window. Set
10982 first_row_to_display to the first row that cannot be reused.
10983 Set pt_row to the row containing point, if there is any. */
10984 pt_row = NULL;
10985 for (first_row_to_display = first_reusable_row;
10986 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
10987 ++first_row_to_display)
10988 {
10989 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
10990 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
10991 pt_row = first_row_to_display;
10992 }
10993
10994 /* Start displaying at the start of first_row_to_display. */
10995 xassert (first_row_to_display->y < yb);
10996 init_to_row_start (&it, w, first_row_to_display);
10997
10998 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
10999 - start_vpos);
11000 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
11001 - nrows_scrolled);
11002 it.current_y = (first_row_to_display->y - first_reusable_row->y
11003 + WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
11004
11005 /* Display lines beginning with first_row_to_display in the
11006 desired matrix. Set last_text_row to the last row displayed
11007 that displays text. */
11008 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
11009 if (pt_row == NULL)
11010 w->cursor.vpos = -1;
11011 last_text_row = NULL;
11012 while (it.current_y < it.last_visible_y && !fonts_changed_p)
11013 if (display_line (&it))
11014 last_text_row = it.glyph_row - 1;
11015
11016 /* Give up If point isn't in a row displayed or reused. */
11017 if (w->cursor.vpos < 0)
11018 {
11019 clear_glyph_matrix (w->desired_matrix);
11020 return 0;
11021 }
11022
11023 /* If point is in a reused row, adjust y and vpos of the cursor
11024 position. */
11025 if (pt_row)
11026 {
11027 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
11028 w->current_matrix);
11029 w->cursor.y -= first_reusable_row->y;
11030 }
11031
11032 /* Scroll the display. */
11033 run.current_y = first_reusable_row->y;
11034 run.desired_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
11035 run.height = it.last_visible_y - run.current_y;
11036 dy = run.current_y - run.desired_y;
11037
11038 if (run.height)
11039 {
11040 struct frame *f = XFRAME (WINDOW_FRAME (w));
11041 update_begin (f);
11042 rif->update_window_begin_hook (w);
11043 rif->clear_mouse_face (w);
11044 rif->scroll_run_hook (w, &run);
11045 rif->update_window_end_hook (w, 0, 0);
11046 update_end (f);
11047 }
11048
11049 /* Adjust Y positions of reused rows. */
11050 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
11051 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
11052 max_y = it.last_visible_y;
11053 for (row = first_reusable_row; row < first_row_to_display; ++row)
11054 {
11055 row->y -= dy;
11056 row->visible_height = row->height;
11057 if (row->y < min_y)
11058 row->visible_height -= min_y - row->y;
11059 if (row->y + row->height > max_y)
11060 row->visible_height -= row->y + row->height - max_y;
11061 }
11062
11063 /* Scroll the current matrix. */
11064 xassert (nrows_scrolled > 0);
11065 rotate_matrix (w->current_matrix,
11066 start_vpos,
11067 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
11068 -nrows_scrolled);
11069
11070 /* Disable rows not reused. */
11071 for (row -= nrows_scrolled; row < bottom_row; ++row)
11072 row->enabled_p = 0;
11073
11074 /* Adjust window end. A null value of last_text_row means that
11075 the window end is in reused rows which in turn means that
11076 only its vpos can have changed. */
11077 if (last_text_row)
11078 {
11079 w->window_end_bytepos
11080 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
11081 w->window_end_pos
11082 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
11083 w->window_end_vpos
11084 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
11085 }
11086 else
11087 {
11088 w->window_end_vpos
11089 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
11090 }
11091
11092 w->window_end_valid = Qnil;
11093 w->desired_matrix->no_scrolling_p = 1;
11094
11095 #if GLYPH_DEBUG
11096 debug_method_add (w, "try_window_reusing_current_matrix 2");
11097 #endif
11098 return 1;
11099 }
11100
11101 return 0;
11102 }
11103
11104
11105 \f
11106 /************************************************************************
11107 Window redisplay reusing current matrix when buffer has changed
11108 ************************************************************************/
11109
11110 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
11111 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
11112 int *, int *));
11113 static struct glyph_row *
11114 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
11115 struct glyph_row *));
11116
11117
11118 /* Return the last row in MATRIX displaying text. If row START is
11119 non-null, start searching with that row. IT gives the dimensions
11120 of the display. Value is null if matrix is empty; otherwise it is
11121 a pointer to the row found. */
11122
11123 static struct glyph_row *
11124 find_last_row_displaying_text (matrix, it, start)
11125 struct glyph_matrix *matrix;
11126 struct it *it;
11127 struct glyph_row *start;
11128 {
11129 struct glyph_row *row, *row_found;
11130
11131 /* Set row_found to the last row in IT->w's current matrix
11132 displaying text. The loop looks funny but think of partially
11133 visible lines. */
11134 row_found = NULL;
11135 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
11136 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
11137 {
11138 xassert (row->enabled_p);
11139 row_found = row;
11140 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
11141 break;
11142 ++row;
11143 }
11144
11145 return row_found;
11146 }
11147
11148
11149 /* Return the last row in the current matrix of W that is not affected
11150 by changes at the start of current_buffer that occurred since W's
11151 current matrix was built. Value is null if no such row exists.
11152
11153 BEG_UNCHANGED us the number of characters unchanged at the start of
11154 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
11155 first changed character in current_buffer. Characters at positions <
11156 BEG + BEG_UNCHANGED are at the same buffer positions as they were
11157 when the current matrix was built. */
11158
11159 static struct glyph_row *
11160 find_last_unchanged_at_beg_row (w)
11161 struct window *w;
11162 {
11163 int first_changed_pos = BEG + BEG_UNCHANGED;
11164 struct glyph_row *row;
11165 struct glyph_row *row_found = NULL;
11166 int yb = window_text_bottom_y (w);
11167
11168 /* Find the last row displaying unchanged text. */
11169 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11170 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
11171 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
11172 {
11173 if (/* If row ends before first_changed_pos, it is unchanged,
11174 except in some case. */
11175 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
11176 /* When row ends in ZV and we write at ZV it is not
11177 unchanged. */
11178 && !row->ends_at_zv_p
11179 /* When first_changed_pos is the end of a continued line,
11180 row is not unchanged because it may be no longer
11181 continued. */
11182 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
11183 && row->continued_p))
11184 row_found = row;
11185
11186 /* Stop if last visible row. */
11187 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
11188 break;
11189
11190 ++row;
11191 }
11192
11193 return row_found;
11194 }
11195
11196
11197 /* Find the first glyph row in the current matrix of W that is not
11198 affected by changes at the end of current_buffer since the
11199 time W's current matrix was built.
11200
11201 Return in *DELTA the number of chars by which buffer positions in
11202 unchanged text at the end of current_buffer must be adjusted.
11203
11204 Return in *DELTA_BYTES the corresponding number of bytes.
11205
11206 Value is null if no such row exists, i.e. all rows are affected by
11207 changes. */
11208
11209 static struct glyph_row *
11210 find_first_unchanged_at_end_row (w, delta, delta_bytes)
11211 struct window *w;
11212 int *delta, *delta_bytes;
11213 {
11214 struct glyph_row *row;
11215 struct glyph_row *row_found = NULL;
11216
11217 *delta = *delta_bytes = 0;
11218
11219 /* Display must not have been paused, otherwise the current matrix
11220 is not up to date. */
11221 if (NILP (w->window_end_valid))
11222 abort ();
11223
11224 /* A value of window_end_pos >= END_UNCHANGED means that the window
11225 end is in the range of changed text. If so, there is no
11226 unchanged row at the end of W's current matrix. */
11227 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
11228 return NULL;
11229
11230 /* Set row to the last row in W's current matrix displaying text. */
11231 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
11232
11233 /* If matrix is entirely empty, no unchanged row exists. */
11234 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
11235 {
11236 /* The value of row is the last glyph row in the matrix having a
11237 meaningful buffer position in it. The end position of row
11238 corresponds to window_end_pos. This allows us to translate
11239 buffer positions in the current matrix to current buffer
11240 positions for characters not in changed text. */
11241 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
11242 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
11243 int last_unchanged_pos, last_unchanged_pos_old;
11244 struct glyph_row *first_text_row
11245 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11246
11247 *delta = Z - Z_old;
11248 *delta_bytes = Z_BYTE - Z_BYTE_old;
11249
11250 /* Set last_unchanged_pos to the buffer position of the last
11251 character in the buffer that has not been changed. Z is the
11252 index + 1 of the last character in current_buffer, i.e. by
11253 subtracting END_UNCHANGED we get the index of the last
11254 unchanged character, and we have to add BEG to get its buffer
11255 position. */
11256 last_unchanged_pos = Z - END_UNCHANGED + BEG;
11257 last_unchanged_pos_old = last_unchanged_pos - *delta;
11258
11259 /* Search backward from ROW for a row displaying a line that
11260 starts at a minimum position >= last_unchanged_pos_old. */
11261 for (; row > first_text_row; --row)
11262 {
11263 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
11264 abort ();
11265
11266 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
11267 row_found = row;
11268 }
11269 }
11270
11271 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
11272 abort ();
11273
11274 return row_found;
11275 }
11276
11277
11278 /* Make sure that glyph rows in the current matrix of window W
11279 reference the same glyph memory as corresponding rows in the
11280 frame's frame matrix. This function is called after scrolling W's
11281 current matrix on a terminal frame in try_window_id and
11282 try_window_reusing_current_matrix. */
11283
11284 static void
11285 sync_frame_with_window_matrix_rows (w)
11286 struct window *w;
11287 {
11288 struct frame *f = XFRAME (w->frame);
11289 struct glyph_row *window_row, *window_row_end, *frame_row;
11290
11291 /* Preconditions: W must be a leaf window and full-width. Its frame
11292 must have a frame matrix. */
11293 xassert (NILP (w->hchild) && NILP (w->vchild));
11294 xassert (WINDOW_FULL_WIDTH_P (w));
11295 xassert (!FRAME_WINDOW_P (f));
11296
11297 /* If W is a full-width window, glyph pointers in W's current matrix
11298 have, by definition, to be the same as glyph pointers in the
11299 corresponding frame matrix. */
11300 window_row = w->current_matrix->rows;
11301 window_row_end = window_row + w->current_matrix->nrows;
11302 frame_row = f->current_matrix->rows + XFASTINT (w->top);
11303 while (window_row < window_row_end)
11304 {
11305 int area;
11306
11307 for (area = LEFT_MARGIN_AREA; area <= LAST_AREA; ++area)
11308 frame_row->glyphs[area] = window_row->glyphs[area];
11309
11310 /* Disable frame rows whose corresponding window rows have
11311 been disabled in try_window_id. */
11312 if (!window_row->enabled_p)
11313 frame_row->enabled_p = 0;
11314
11315 ++window_row, ++frame_row;
11316 }
11317 }
11318
11319
11320 /* Find the glyph row in window W containing CHARPOS. Consider all
11321 rows between START and END (not inclusive). END null means search
11322 all rows to the end of the display area of W. Value is the row
11323 containing CHARPOS or null. */
11324
11325 struct glyph_row *
11326 row_containing_pos (w, charpos, start, end, dy)
11327 struct window *w;
11328 int charpos;
11329 struct glyph_row *start, *end;
11330 int dy;
11331 {
11332 struct glyph_row *row = start;
11333 int last_y;
11334
11335 /* If we happen to start on a header-line, skip that. */
11336 if (row->mode_line_p)
11337 ++row;
11338
11339 if ((end && row >= end) || !row->enabled_p)
11340 return NULL;
11341
11342 last_y = window_text_bottom_y (w) - dy;
11343
11344 while ((end == NULL || row < end)
11345 && MATRIX_ROW_BOTTOM_Y (row) < last_y
11346 && (MATRIX_ROW_END_CHARPOS (row) < charpos
11347 || (MATRIX_ROW_END_CHARPOS (row) == charpos
11348 /* The end position of a row equals the start
11349 position of the next row. If CHARPOS is there, we
11350 would rather display it in the next line, except
11351 when this line ends in ZV. */
11352 && !row->ends_at_zv_p
11353 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))
11354 ++row;
11355
11356 /* Give up if CHARPOS not found. */
11357 if ((end && row >= end)
11358 || charpos < MATRIX_ROW_START_CHARPOS (row)
11359 || charpos > MATRIX_ROW_END_CHARPOS (row))
11360 row = NULL;
11361
11362 return row;
11363 }
11364
11365
11366 /* Try to redisplay window W by reusing its existing display. W's
11367 current matrix must be up to date when this function is called,
11368 i.e. window_end_valid must not be nil.
11369
11370 Value is
11371
11372 1 if display has been updated
11373 0 if otherwise unsuccessful
11374 -1 if redisplay with same window start is known not to succeed
11375
11376 The following steps are performed:
11377
11378 1. Find the last row in the current matrix of W that is not
11379 affected by changes at the start of current_buffer. If no such row
11380 is found, give up.
11381
11382 2. Find the first row in W's current matrix that is not affected by
11383 changes at the end of current_buffer. Maybe there is no such row.
11384
11385 3. Display lines beginning with the row + 1 found in step 1 to the
11386 row found in step 2 or, if step 2 didn't find a row, to the end of
11387 the window.
11388
11389 4. If cursor is not known to appear on the window, give up.
11390
11391 5. If display stopped at the row found in step 2, scroll the
11392 display and current matrix as needed.
11393
11394 6. Maybe display some lines at the end of W, if we must. This can
11395 happen under various circumstances, like a partially visible line
11396 becoming fully visible, or because newly displayed lines are displayed
11397 in smaller font sizes.
11398
11399 7. Update W's window end information. */
11400
11401 static int
11402 try_window_id (w)
11403 struct window *w;
11404 {
11405 struct frame *f = XFRAME (w->frame);
11406 struct glyph_matrix *current_matrix = w->current_matrix;
11407 struct glyph_matrix *desired_matrix = w->desired_matrix;
11408 struct glyph_row *last_unchanged_at_beg_row;
11409 struct glyph_row *first_unchanged_at_end_row;
11410 struct glyph_row *row;
11411 struct glyph_row *bottom_row;
11412 int bottom_vpos;
11413 struct it it;
11414 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
11415 struct text_pos start_pos;
11416 struct run run;
11417 int first_unchanged_at_end_vpos = 0;
11418 struct glyph_row *last_text_row, *last_text_row_at_end;
11419 struct text_pos start;
11420 int first_changed_charpos, last_changed_charpos;
11421
11422 #if GLYPH_DEBUG
11423 if (inhibit_try_window_id)
11424 return 0;
11425 #endif
11426
11427 /* This is handy for debugging. */
11428 #if 0
11429 #define GIVE_UP(X) \
11430 do { \
11431 fprintf (stderr, "try_window_id give up %d\n", (X)); \
11432 return 0; \
11433 } while (0)
11434 #else
11435 #define GIVE_UP(X) return 0
11436 #endif
11437
11438 SET_TEXT_POS_FROM_MARKER (start, w->start);
11439
11440 /* Don't use this for mini-windows because these can show
11441 messages and mini-buffers, and we don't handle that here. */
11442 if (MINI_WINDOW_P (w))
11443 GIVE_UP (1);
11444
11445 /* This flag is used to prevent redisplay optimizations. */
11446 if (windows_or_buffers_changed)
11447 GIVE_UP (2);
11448
11449 /* Verify that narrowing has not changed. This flag is also set to prevent
11450 redisplay optimizations. It would be nice to further
11451 reduce the number of cases where this prevents try_window_id. */
11452 if (current_buffer->clip_changed)
11453 GIVE_UP (3);
11454
11455 /* Window must either use window-based redisplay or be full width. */
11456 if (!FRAME_WINDOW_P (f)
11457 && (!line_ins_del_ok
11458 || !WINDOW_FULL_WIDTH_P (w)))
11459 GIVE_UP (4);
11460
11461 /* Give up if point is not known NOT to appear in W. */
11462 if (PT < CHARPOS (start))
11463 GIVE_UP (5);
11464
11465 /* Another way to prevent redisplay optimizations. */
11466 if (XFASTINT (w->last_modified) == 0)
11467 GIVE_UP (6);
11468
11469 /* Verify that window is not hscrolled. */
11470 if (XFASTINT (w->hscroll) != 0)
11471 GIVE_UP (7);
11472
11473 /* Verify that display wasn't paused. */
11474 if (NILP (w->window_end_valid))
11475 GIVE_UP (8);
11476
11477 /* Can't use this if highlighting a region because a cursor movement
11478 will do more than just set the cursor. */
11479 if (!NILP (Vtransient_mark_mode)
11480 && !NILP (current_buffer->mark_active))
11481 GIVE_UP (9);
11482
11483 /* Likewise if highlighting trailing whitespace. */
11484 if (!NILP (Vshow_trailing_whitespace))
11485 GIVE_UP (11);
11486
11487 /* Likewise if showing a region. */
11488 if (!NILP (w->region_showing))
11489 GIVE_UP (10);
11490
11491 /* Can use this if overlay arrow position and or string have changed. */
11492 if (!EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
11493 || !EQ (last_arrow_string, Voverlay_arrow_string))
11494 GIVE_UP (12);
11495
11496
11497 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
11498 only if buffer has really changed. The reason is that the gap is
11499 initially at Z for freshly visited files. The code below would
11500 set end_unchanged to 0 in that case. */
11501 if (MODIFF > SAVE_MODIFF
11502 /* This seems to happen sometimes after saving a buffer. */
11503 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
11504 {
11505 if (GPT - BEG < BEG_UNCHANGED)
11506 BEG_UNCHANGED = GPT - BEG;
11507 if (Z - GPT < END_UNCHANGED)
11508 END_UNCHANGED = Z - GPT;
11509 }
11510
11511 /* The position of the first and last character that has been changed. */
11512 first_changed_charpos = BEG + BEG_UNCHANGED;
11513 last_changed_charpos = Z - END_UNCHANGED;
11514
11515 /* If window starts after a line end, and the last change is in
11516 front of that newline, then changes don't affect the display.
11517 This case happens with stealth-fontification. Note that although
11518 the display is unchanged, glyph positions in the matrix have to
11519 be adjusted, of course. */
11520 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
11521 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
11522 && ((last_changed_charpos < CHARPOS (start)
11523 && CHARPOS (start) == BEGV)
11524 || (last_changed_charpos < CHARPOS (start) - 1
11525 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
11526 {
11527 int Z_old, delta, Z_BYTE_old, delta_bytes;
11528 struct glyph_row *r0;
11529
11530 /* Compute how many chars/bytes have been added to or removed
11531 from the buffer. */
11532 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
11533 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
11534 delta = Z - Z_old;
11535 delta_bytes = Z_BYTE - Z_BYTE_old;
11536
11537 /* Give up if PT is not in the window. Note that it already has
11538 been checked at the start of try_window_id that PT is not in
11539 front of the window start. */
11540 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
11541 GIVE_UP (13);
11542
11543 /* If window start is unchanged, we can reuse the whole matrix
11544 as is, after adjusting glyph positions. No need to compute
11545 the window end again, since its offset from Z hasn't changed. */
11546 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
11547 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
11548 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes)
11549 {
11550 /* Adjust positions in the glyph matrix. */
11551 if (delta || delta_bytes)
11552 {
11553 struct glyph_row *r1
11554 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11555 increment_matrix_positions (w->current_matrix,
11556 MATRIX_ROW_VPOS (r0, current_matrix),
11557 MATRIX_ROW_VPOS (r1, current_matrix),
11558 delta, delta_bytes);
11559 }
11560
11561 /* Set the cursor. */
11562 row = row_containing_pos (w, PT, r0, NULL, 0);
11563 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
11564 return 1;
11565 }
11566 }
11567
11568 /* Handle the case that changes are all below what is displayed in
11569 the window, and that PT is in the window. This shortcut cannot
11570 be taken if ZV is visible in the window, and text has been added
11571 there that is visible in the window. */
11572 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
11573 /* ZV is not visible in the window, or there are no
11574 changes at ZV, actually. */
11575 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
11576 || first_changed_charpos == last_changed_charpos))
11577 {
11578 struct glyph_row *r0;
11579
11580 /* Give up if PT is not in the window. Note that it already has
11581 been checked at the start of try_window_id that PT is not in
11582 front of the window start. */
11583 if (PT >= MATRIX_ROW_END_CHARPOS (row))
11584 GIVE_UP (14);
11585
11586 /* If window start is unchanged, we can reuse the whole matrix
11587 as is, without changing glyph positions since no text has
11588 been added/removed in front of the window end. */
11589 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
11590 if (TEXT_POS_EQUAL_P (start, r0->start.pos))
11591 {
11592 /* We have to compute the window end anew since text
11593 can have been added/removed after it. */
11594 w->window_end_pos
11595 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
11596 w->window_end_bytepos
11597 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11598
11599 /* Set the cursor. */
11600 row = row_containing_pos (w, PT, r0, NULL, 0);
11601 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
11602 return 2;
11603 }
11604 }
11605
11606 /* Give up if window start is in the changed area.
11607
11608 The condition used to read
11609
11610 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
11611
11612 but why that was tested escapes me at the moment. */
11613 if (CHARPOS (start) >= first_changed_charpos
11614 && CHARPOS (start) <= last_changed_charpos)
11615 GIVE_UP (15);
11616
11617 /* Check that window start agrees with the start of the first glyph
11618 row in its current matrix. Check this after we know the window
11619 start is not in changed text, otherwise positions would not be
11620 comparable. */
11621 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
11622 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
11623 GIVE_UP (16);
11624
11625 /* Give up if the window ends in strings. Overlay strings
11626 at the end are difficult to handle, so don't try. */
11627 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
11628 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
11629 GIVE_UP (20);
11630
11631 /* Compute the position at which we have to start displaying new
11632 lines. Some of the lines at the top of the window might be
11633 reusable because they are not displaying changed text. Find the
11634 last row in W's current matrix not affected by changes at the
11635 start of current_buffer. Value is null if changes start in the
11636 first line of window. */
11637 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
11638 if (last_unchanged_at_beg_row)
11639 {
11640 /* Avoid starting to display in the moddle of a character, a TAB
11641 for instance. This is easier than to set up the iterator
11642 exactly, and it's not a frequent case, so the additional
11643 effort wouldn't really pay off. */
11644 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
11645 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
11646 && last_unchanged_at_beg_row > w->current_matrix->rows)
11647 --last_unchanged_at_beg_row;
11648
11649 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
11650 GIVE_UP (17);
11651
11652 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
11653 GIVE_UP (18);
11654 start_pos = it.current.pos;
11655
11656 /* Start displaying new lines in the desired matrix at the same
11657 vpos we would use in the current matrix, i.e. below
11658 last_unchanged_at_beg_row. */
11659 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
11660 current_matrix);
11661 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11662 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
11663
11664 xassert (it.hpos == 0 && it.current_x == 0);
11665 }
11666 else
11667 {
11668 /* There are no reusable lines at the start of the window.
11669 Start displaying in the first line. */
11670 start_display (&it, w, start);
11671 start_pos = it.current.pos;
11672 }
11673
11674 /* Find the first row that is not affected by changes at the end of
11675 the buffer. Value will be null if there is no unchanged row, in
11676 which case we must redisplay to the end of the window. delta
11677 will be set to the value by which buffer positions beginning with
11678 first_unchanged_at_end_row have to be adjusted due to text
11679 changes. */
11680 first_unchanged_at_end_row
11681 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
11682 IF_DEBUG (debug_delta = delta);
11683 IF_DEBUG (debug_delta_bytes = delta_bytes);
11684
11685 /* Set stop_pos to the buffer position up to which we will have to
11686 display new lines. If first_unchanged_at_end_row != NULL, this
11687 is the buffer position of the start of the line displayed in that
11688 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
11689 that we don't stop at a buffer position. */
11690 stop_pos = 0;
11691 if (first_unchanged_at_end_row)
11692 {
11693 xassert (last_unchanged_at_beg_row == NULL
11694 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
11695
11696 /* If this is a continuation line, move forward to the next one
11697 that isn't. Changes in lines above affect this line.
11698 Caution: this may move first_unchanged_at_end_row to a row
11699 not displaying text. */
11700 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
11701 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11702 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11703 < it.last_visible_y))
11704 ++first_unchanged_at_end_row;
11705
11706 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11707 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11708 >= it.last_visible_y))
11709 first_unchanged_at_end_row = NULL;
11710 else
11711 {
11712 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
11713 + delta);
11714 first_unchanged_at_end_vpos
11715 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
11716 xassert (stop_pos >= Z - END_UNCHANGED);
11717 }
11718 }
11719 else if (last_unchanged_at_beg_row == NULL)
11720 GIVE_UP (19);
11721
11722
11723 #if GLYPH_DEBUG
11724
11725 /* Either there is no unchanged row at the end, or the one we have
11726 now displays text. This is a necessary condition for the window
11727 end pos calculation at the end of this function. */
11728 xassert (first_unchanged_at_end_row == NULL
11729 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
11730
11731 debug_last_unchanged_at_beg_vpos
11732 = (last_unchanged_at_beg_row
11733 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
11734 : -1);
11735 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
11736
11737 #endif /* GLYPH_DEBUG != 0 */
11738
11739
11740 /* Display new lines. Set last_text_row to the last new line
11741 displayed which has text on it, i.e. might end up as being the
11742 line where the window_end_vpos is. */
11743 w->cursor.vpos = -1;
11744 last_text_row = NULL;
11745 overlay_arrow_seen = 0;
11746 while (it.current_y < it.last_visible_y
11747 && !fonts_changed_p
11748 && (first_unchanged_at_end_row == NULL
11749 || IT_CHARPOS (it) < stop_pos))
11750 {
11751 if (display_line (&it))
11752 last_text_row = it.glyph_row - 1;
11753 }
11754
11755 if (fonts_changed_p)
11756 return -1;
11757
11758
11759 /* Compute differences in buffer positions, y-positions etc. for
11760 lines reused at the bottom of the window. Compute what we can
11761 scroll. */
11762 if (first_unchanged_at_end_row
11763 /* No lines reused because we displayed everything up to the
11764 bottom of the window. */
11765 && it.current_y < it.last_visible_y)
11766 {
11767 dvpos = (it.vpos
11768 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
11769 current_matrix));
11770 dy = it.current_y - first_unchanged_at_end_row->y;
11771 run.current_y = first_unchanged_at_end_row->y;
11772 run.desired_y = run.current_y + dy;
11773 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
11774 }
11775 else
11776 {
11777 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
11778 first_unchanged_at_end_row = NULL;
11779 }
11780 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
11781
11782
11783 /* Find the cursor if not already found. We have to decide whether
11784 PT will appear on this window (it sometimes doesn't, but this is
11785 not a very frequent case.) This decision has to be made before
11786 the current matrix is altered. A value of cursor.vpos < 0 means
11787 that PT is either in one of the lines beginning at
11788 first_unchanged_at_end_row or below the window. Don't care for
11789 lines that might be displayed later at the window end; as
11790 mentioned, this is not a frequent case. */
11791 if (w->cursor.vpos < 0)
11792 {
11793 /* Cursor in unchanged rows at the top? */
11794 if (PT < CHARPOS (start_pos)
11795 && last_unchanged_at_beg_row)
11796 {
11797 row = row_containing_pos (w, PT,
11798 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
11799 last_unchanged_at_beg_row + 1, 0);
11800 if (row)
11801 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11802 }
11803
11804 /* Start from first_unchanged_at_end_row looking for PT. */
11805 else if (first_unchanged_at_end_row)
11806 {
11807 row = row_containing_pos (w, PT - delta,
11808 first_unchanged_at_end_row, NULL, 0);
11809 if (row)
11810 set_cursor_from_row (w, row, w->current_matrix, delta,
11811 delta_bytes, dy, dvpos);
11812 }
11813
11814 /* Give up if cursor was not found. */
11815 if (w->cursor.vpos < 0)
11816 {
11817 clear_glyph_matrix (w->desired_matrix);
11818 return -1;
11819 }
11820 }
11821
11822 /* Don't let the cursor end in the scroll margins. */
11823 {
11824 int this_scroll_margin, cursor_height;
11825
11826 this_scroll_margin = max (0, scroll_margin);
11827 this_scroll_margin = min (this_scroll_margin,
11828 XFASTINT (w->height) / 4);
11829 this_scroll_margin *= CANON_Y_UNIT (it.f);
11830 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
11831
11832 if ((w->cursor.y < this_scroll_margin
11833 && CHARPOS (start) > BEGV)
11834 /* Don't take scroll margin into account at the bottom because
11835 old redisplay didn't do it either. */
11836 || w->cursor.y + cursor_height > it.last_visible_y)
11837 {
11838 w->cursor.vpos = -1;
11839 clear_glyph_matrix (w->desired_matrix);
11840 return -1;
11841 }
11842 }
11843
11844 /* Scroll the display. Do it before changing the current matrix so
11845 that xterm.c doesn't get confused about where the cursor glyph is
11846 found. */
11847 if (dy && run.height)
11848 {
11849 update_begin (f);
11850
11851 if (FRAME_WINDOW_P (f))
11852 {
11853 rif->update_window_begin_hook (w);
11854 rif->clear_mouse_face (w);
11855 rif->scroll_run_hook (w, &run);
11856 rif->update_window_end_hook (w, 0, 0);
11857 }
11858 else
11859 {
11860 /* Terminal frame. In this case, dvpos gives the number of
11861 lines to scroll by; dvpos < 0 means scroll up. */
11862 int first_unchanged_at_end_vpos
11863 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
11864 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
11865 int end = (XFASTINT (w->top)
11866 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
11867 + window_internal_height (w));
11868
11869 /* Perform the operation on the screen. */
11870 if (dvpos > 0)
11871 {
11872 /* Scroll last_unchanged_at_beg_row to the end of the
11873 window down dvpos lines. */
11874 set_terminal_window (end);
11875
11876 /* On dumb terminals delete dvpos lines at the end
11877 before inserting dvpos empty lines. */
11878 if (!scroll_region_ok)
11879 ins_del_lines (end - dvpos, -dvpos);
11880
11881 /* Insert dvpos empty lines in front of
11882 last_unchanged_at_beg_row. */
11883 ins_del_lines (from, dvpos);
11884 }
11885 else if (dvpos < 0)
11886 {
11887 /* Scroll up last_unchanged_at_beg_vpos to the end of
11888 the window to last_unchanged_at_beg_vpos - |dvpos|. */
11889 set_terminal_window (end);
11890
11891 /* Delete dvpos lines in front of
11892 last_unchanged_at_beg_vpos. ins_del_lines will set
11893 the cursor to the given vpos and emit |dvpos| delete
11894 line sequences. */
11895 ins_del_lines (from + dvpos, dvpos);
11896
11897 /* On a dumb terminal insert dvpos empty lines at the
11898 end. */
11899 if (!scroll_region_ok)
11900 ins_del_lines (end + dvpos, -dvpos);
11901 }
11902
11903 set_terminal_window (0);
11904 }
11905
11906 update_end (f);
11907 }
11908
11909 /* Shift reused rows of the current matrix to the right position.
11910 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
11911 text. */
11912 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11913 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
11914 if (dvpos < 0)
11915 {
11916 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
11917 bottom_vpos, dvpos);
11918 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
11919 bottom_vpos, 0);
11920 }
11921 else if (dvpos > 0)
11922 {
11923 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
11924 bottom_vpos, dvpos);
11925 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
11926 first_unchanged_at_end_vpos + dvpos, 0);
11927 }
11928
11929 /* For frame-based redisplay, make sure that current frame and window
11930 matrix are in sync with respect to glyph memory. */
11931 if (!FRAME_WINDOW_P (f))
11932 sync_frame_with_window_matrix_rows (w);
11933
11934 /* Adjust buffer positions in reused rows. */
11935 if (delta)
11936 increment_matrix_positions (current_matrix,
11937 first_unchanged_at_end_vpos + dvpos,
11938 bottom_vpos, delta, delta_bytes);
11939
11940 /* Adjust Y positions. */
11941 if (dy)
11942 shift_glyph_matrix (w, current_matrix,
11943 first_unchanged_at_end_vpos + dvpos,
11944 bottom_vpos, dy);
11945
11946 if (first_unchanged_at_end_row)
11947 first_unchanged_at_end_row += dvpos;
11948
11949 /* If scrolling up, there may be some lines to display at the end of
11950 the window. */
11951 last_text_row_at_end = NULL;
11952 if (dy < 0)
11953 {
11954 /* Scrolling up can leave for example a partially visible line
11955 at the end of the window to be redisplayed. */
11956 /* Set last_row to the glyph row in the current matrix where the
11957 window end line is found. It has been moved up or down in
11958 the matrix by dvpos. */
11959 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
11960 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
11961
11962 /* If last_row is the window end line, it should display text. */
11963 xassert (last_row->displays_text_p);
11964
11965 /* If window end line was partially visible before, begin
11966 displaying at that line. Otherwise begin displaying with the
11967 line following it. */
11968 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
11969 {
11970 init_to_row_start (&it, w, last_row);
11971 it.vpos = last_vpos;
11972 it.current_y = last_row->y;
11973 }
11974 else
11975 {
11976 init_to_row_end (&it, w, last_row);
11977 it.vpos = 1 + last_vpos;
11978 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
11979 ++last_row;
11980 }
11981
11982 /* We may start in a continuation line. If so, we have to
11983 get the right continuation_lines_width and current_x. */
11984 it.continuation_lines_width = last_row->continuation_lines_width;
11985 it.hpos = it.current_x = 0;
11986
11987 /* Display the rest of the lines at the window end. */
11988 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11989 while (it.current_y < it.last_visible_y
11990 && !fonts_changed_p)
11991 {
11992 /* Is it always sure that the display agrees with lines in
11993 the current matrix? I don't think so, so we mark rows
11994 displayed invalid in the current matrix by setting their
11995 enabled_p flag to zero. */
11996 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
11997 if (display_line (&it))
11998 last_text_row_at_end = it.glyph_row - 1;
11999 }
12000 }
12001
12002 /* Update window_end_pos and window_end_vpos. */
12003 if (first_unchanged_at_end_row
12004 && first_unchanged_at_end_row->y < it.last_visible_y
12005 && !last_text_row_at_end)
12006 {
12007 /* Window end line if one of the preserved rows from the current
12008 matrix. Set row to the last row displaying text in current
12009 matrix starting at first_unchanged_at_end_row, after
12010 scrolling. */
12011 xassert (first_unchanged_at_end_row->displays_text_p);
12012 row = find_last_row_displaying_text (w->current_matrix, &it,
12013 first_unchanged_at_end_row);
12014 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
12015
12016 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
12017 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
12018 w->window_end_vpos
12019 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
12020 xassert (w->window_end_bytepos >= 0);
12021 IF_DEBUG (debug_method_add (w, "A"));
12022 }
12023 else if (last_text_row_at_end)
12024 {
12025 w->window_end_pos
12026 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
12027 w->window_end_bytepos
12028 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
12029 w->window_end_vpos
12030 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
12031 xassert (w->window_end_bytepos >= 0);
12032 IF_DEBUG (debug_method_add (w, "B"));
12033 }
12034 else if (last_text_row)
12035 {
12036 /* We have displayed either to the end of the window or at the
12037 end of the window, i.e. the last row with text is to be found
12038 in the desired matrix. */
12039 w->window_end_pos
12040 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12041 w->window_end_bytepos
12042 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12043 w->window_end_vpos
12044 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
12045 xassert (w->window_end_bytepos >= 0);
12046 }
12047 else if (first_unchanged_at_end_row == NULL
12048 && last_text_row == NULL
12049 && last_text_row_at_end == NULL)
12050 {
12051 /* Displayed to end of window, but no line containing text was
12052 displayed. Lines were deleted at the end of the window. */
12053 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
12054 int vpos = XFASTINT (w->window_end_vpos);
12055 struct glyph_row *current_row = current_matrix->rows + vpos;
12056 struct glyph_row *desired_row = desired_matrix->rows + vpos;
12057
12058 for (row = NULL;
12059 row == NULL && vpos >= first_vpos;
12060 --vpos, --current_row, --desired_row)
12061 {
12062 if (desired_row->enabled_p)
12063 {
12064 if (desired_row->displays_text_p)
12065 row = desired_row;
12066 }
12067 else if (current_row->displays_text_p)
12068 row = current_row;
12069 }
12070
12071 xassert (row != NULL);
12072 w->window_end_vpos = make_number (vpos + 1);
12073 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
12074 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
12075 xassert (w->window_end_bytepos >= 0);
12076 IF_DEBUG (debug_method_add (w, "C"));
12077 }
12078 else
12079 abort ();
12080
12081 #if 0 /* This leads to problems, for instance when the cursor is
12082 at ZV, and the cursor line displays no text. */
12083 /* Disable rows below what's displayed in the window. This makes
12084 debugging easier. */
12085 enable_glyph_matrix_rows (current_matrix,
12086 XFASTINT (w->window_end_vpos) + 1,
12087 bottom_vpos, 0);
12088 #endif
12089
12090 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
12091 debug_end_vpos = XFASTINT (w->window_end_vpos));
12092
12093 /* Record that display has not been completed. */
12094 w->window_end_valid = Qnil;
12095 w->desired_matrix->no_scrolling_p = 1;
12096 return 3;
12097
12098 #undef GIVE_UP
12099 }
12100
12101
12102 \f
12103 /***********************************************************************
12104 More debugging support
12105 ***********************************************************************/
12106
12107 #if GLYPH_DEBUG
12108
12109 void dump_glyph_row P_ ((struct glyph_row *, int, int));
12110 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
12111 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
12112
12113
12114 /* Dump the contents of glyph matrix MATRIX on stderr.
12115
12116 GLYPHS 0 means don't show glyph contents.
12117 GLYPHS 1 means show glyphs in short form
12118 GLYPHS > 1 means show glyphs in long form. */
12119
12120 void
12121 dump_glyph_matrix (matrix, glyphs)
12122 struct glyph_matrix *matrix;
12123 int glyphs;
12124 {
12125 int i;
12126 for (i = 0; i < matrix->nrows; ++i)
12127 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
12128 }
12129
12130
12131 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
12132 the glyph row and area where the glyph comes from. */
12133
12134 void
12135 dump_glyph (row, glyph, area)
12136 struct glyph_row *row;
12137 struct glyph *glyph;
12138 int area;
12139 {
12140 if (glyph->type == CHAR_GLYPH)
12141 {
12142 fprintf (stderr,
12143 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12144 glyph - row->glyphs[TEXT_AREA],
12145 'C',
12146 glyph->charpos,
12147 (BUFFERP (glyph->object)
12148 ? 'B'
12149 : (STRINGP (glyph->object)
12150 ? 'S'
12151 : '-')),
12152 glyph->pixel_width,
12153 glyph->u.ch,
12154 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
12155 ? glyph->u.ch
12156 : '.'),
12157 glyph->face_id,
12158 glyph->left_box_line_p,
12159 glyph->right_box_line_p);
12160 }
12161 else if (glyph->type == STRETCH_GLYPH)
12162 {
12163 fprintf (stderr,
12164 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12165 glyph - row->glyphs[TEXT_AREA],
12166 'S',
12167 glyph->charpos,
12168 (BUFFERP (glyph->object)
12169 ? 'B'
12170 : (STRINGP (glyph->object)
12171 ? 'S'
12172 : '-')),
12173 glyph->pixel_width,
12174 0,
12175 '.',
12176 glyph->face_id,
12177 glyph->left_box_line_p,
12178 glyph->right_box_line_p);
12179 }
12180 else if (glyph->type == IMAGE_GLYPH)
12181 {
12182 fprintf (stderr,
12183 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12184 glyph - row->glyphs[TEXT_AREA],
12185 'I',
12186 glyph->charpos,
12187 (BUFFERP (glyph->object)
12188 ? 'B'
12189 : (STRINGP (glyph->object)
12190 ? 'S'
12191 : '-')),
12192 glyph->pixel_width,
12193 glyph->u.img_id,
12194 '.',
12195 glyph->face_id,
12196 glyph->left_box_line_p,
12197 glyph->right_box_line_p);
12198 }
12199 }
12200
12201
12202 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
12203 GLYPHS 0 means don't show glyph contents.
12204 GLYPHS 1 means show glyphs in short form
12205 GLYPHS > 1 means show glyphs in long form. */
12206
12207 void
12208 dump_glyph_row (row, vpos, glyphs)
12209 struct glyph_row *row;
12210 int vpos, glyphs;
12211 {
12212 if (glyphs != 1)
12213 {
12214 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
12215 fprintf (stderr, "=======================================================================\n");
12216
12217 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d\
12218 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
12219 vpos,
12220 MATRIX_ROW_START_CHARPOS (row),
12221 MATRIX_ROW_END_CHARPOS (row),
12222 row->used[TEXT_AREA],
12223 row->contains_overlapping_glyphs_p,
12224 row->enabled_p,
12225 row->truncated_on_left_p,
12226 row->truncated_on_right_p,
12227 row->overlay_arrow_p,
12228 row->continued_p,
12229 MATRIX_ROW_CONTINUATION_LINE_P (row),
12230 row->displays_text_p,
12231 row->ends_at_zv_p,
12232 row->fill_line_p,
12233 row->ends_in_middle_of_char_p,
12234 row->starts_in_middle_of_char_p,
12235 row->mouse_face_p,
12236 row->x,
12237 row->y,
12238 row->pixel_width,
12239 row->height,
12240 row->visible_height,
12241 row->ascent,
12242 row->phys_ascent);
12243 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
12244 row->end.overlay_string_index,
12245 row->continuation_lines_width);
12246 fprintf (stderr, "%9d %5d\n",
12247 CHARPOS (row->start.string_pos),
12248 CHARPOS (row->end.string_pos));
12249 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
12250 row->end.dpvec_index);
12251 }
12252
12253 if (glyphs > 1)
12254 {
12255 int area;
12256
12257 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12258 {
12259 struct glyph *glyph = row->glyphs[area];
12260 struct glyph *glyph_end = glyph + row->used[area];
12261
12262 /* Glyph for a line end in text. */
12263 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
12264 ++glyph_end;
12265
12266 if (glyph < glyph_end)
12267 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
12268
12269 for (; glyph < glyph_end; ++glyph)
12270 dump_glyph (row, glyph, area);
12271 }
12272 }
12273 else if (glyphs == 1)
12274 {
12275 int area;
12276
12277 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12278 {
12279 char *s = (char *) alloca (row->used[area] + 1);
12280 int i;
12281
12282 for (i = 0; i < row->used[area]; ++i)
12283 {
12284 struct glyph *glyph = row->glyphs[area] + i;
12285 if (glyph->type == CHAR_GLYPH
12286 && glyph->u.ch < 0x80
12287 && glyph->u.ch >= ' ')
12288 s[i] = glyph->u.ch;
12289 else
12290 s[i] = '.';
12291 }
12292
12293 s[i] = '\0';
12294 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
12295 }
12296 }
12297 }
12298
12299
12300 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
12301 Sdump_glyph_matrix, 0, 1, "p",
12302 doc: /* Dump the current matrix of the selected window to stderr.
12303 Shows contents of glyph row structures. With non-nil
12304 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
12305 glyphs in short form, otherwise show glyphs in long form. */)
12306 (glyphs)
12307 Lisp_Object glyphs;
12308 {
12309 struct window *w = XWINDOW (selected_window);
12310 struct buffer *buffer = XBUFFER (w->buffer);
12311
12312 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
12313 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
12314 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
12315 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
12316 fprintf (stderr, "=============================================\n");
12317 dump_glyph_matrix (w->current_matrix,
12318 NILP (glyphs) ? 0 : XINT (glyphs));
12319 return Qnil;
12320 }
12321
12322
12323 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
12324 doc: /* Dump glyph row ROW to stderr.
12325 GLYPH 0 means don't dump glyphs.
12326 GLYPH 1 means dump glyphs in short form.
12327 GLYPH > 1 or omitted means dump glyphs in long form. */)
12328 (row, glyphs)
12329 Lisp_Object row, glyphs;
12330 {
12331 struct glyph_matrix *matrix;
12332 int vpos;
12333
12334 CHECK_NUMBER (row);
12335 matrix = XWINDOW (selected_window)->current_matrix;
12336 vpos = XINT (row);
12337 if (vpos >= 0 && vpos < matrix->nrows)
12338 dump_glyph_row (MATRIX_ROW (matrix, vpos),
12339 vpos,
12340 INTEGERP (glyphs) ? XINT (glyphs) : 2);
12341 return Qnil;
12342 }
12343
12344
12345 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
12346 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
12347 GLYPH 0 means don't dump glyphs.
12348 GLYPH 1 means dump glyphs in short form.
12349 GLYPH > 1 or omitted means dump glyphs in long form. */)
12350 (row, glyphs)
12351 Lisp_Object row, glyphs;
12352 {
12353 struct frame *sf = SELECTED_FRAME ();
12354 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
12355 int vpos;
12356
12357 CHECK_NUMBER (row);
12358 vpos = XINT (row);
12359 if (vpos >= 0 && vpos < m->nrows)
12360 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
12361 INTEGERP (glyphs) ? XINT (glyphs) : 2);
12362 return Qnil;
12363 }
12364
12365
12366 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
12367 doc: /* Toggle tracing of redisplay.
12368 With ARG, turn tracing on if and only if ARG is positive. */)
12369 (arg)
12370 Lisp_Object arg;
12371 {
12372 if (NILP (arg))
12373 trace_redisplay_p = !trace_redisplay_p;
12374 else
12375 {
12376 arg = Fprefix_numeric_value (arg);
12377 trace_redisplay_p = XINT (arg) > 0;
12378 }
12379
12380 return Qnil;
12381 }
12382
12383
12384 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
12385 doc: /* Like `format', but print result to stderr. */)
12386 (nargs, args)
12387 int nargs;
12388 Lisp_Object *args;
12389 {
12390 Lisp_Object s = Fformat (nargs, args);
12391 fprintf (stderr, "%s", XSTRING (s)->data);
12392 return Qnil;
12393 }
12394
12395 #endif /* GLYPH_DEBUG */
12396
12397
12398 \f
12399 /***********************************************************************
12400 Building Desired Matrix Rows
12401 ***********************************************************************/
12402
12403 /* Return a temporary glyph row holding the glyphs of an overlay
12404 arrow. Only used for non-window-redisplay windows. */
12405
12406 static struct glyph_row *
12407 get_overlay_arrow_glyph_row (w)
12408 struct window *w;
12409 {
12410 struct frame *f = XFRAME (WINDOW_FRAME (w));
12411 struct buffer *buffer = XBUFFER (w->buffer);
12412 struct buffer *old = current_buffer;
12413 unsigned char *arrow_string = XSTRING (Voverlay_arrow_string)->data;
12414 int arrow_len = XSTRING (Voverlay_arrow_string)->size;
12415 unsigned char *arrow_end = arrow_string + arrow_len;
12416 unsigned char *p;
12417 struct it it;
12418 int multibyte_p;
12419 int n_glyphs_before;
12420
12421 set_buffer_temp (buffer);
12422 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
12423 it.glyph_row->used[TEXT_AREA] = 0;
12424 SET_TEXT_POS (it.position, 0, 0);
12425
12426 multibyte_p = !NILP (buffer->enable_multibyte_characters);
12427 p = arrow_string;
12428 while (p < arrow_end)
12429 {
12430 Lisp_Object face, ilisp;
12431
12432 /* Get the next character. */
12433 if (multibyte_p)
12434 it.c = string_char_and_length (p, arrow_len, &it.len);
12435 else
12436 it.c = *p, it.len = 1;
12437 p += it.len;
12438
12439 /* Get its face. */
12440 ilisp = make_number (p - arrow_string);
12441 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
12442 it.face_id = compute_char_face (f, it.c, face);
12443
12444 /* Compute its width, get its glyphs. */
12445 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
12446 SET_TEXT_POS (it.position, -1, -1);
12447 PRODUCE_GLYPHS (&it);
12448
12449 /* If this character doesn't fit any more in the line, we have
12450 to remove some glyphs. */
12451 if (it.current_x > it.last_visible_x)
12452 {
12453 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
12454 break;
12455 }
12456 }
12457
12458 set_buffer_temp (old);
12459 return it.glyph_row;
12460 }
12461
12462
12463 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
12464 glyphs are only inserted for terminal frames since we can't really
12465 win with truncation glyphs when partially visible glyphs are
12466 involved. Which glyphs to insert is determined by
12467 produce_special_glyphs. */
12468
12469 static void
12470 insert_left_trunc_glyphs (it)
12471 struct it *it;
12472 {
12473 struct it truncate_it;
12474 struct glyph *from, *end, *to, *toend;
12475
12476 xassert (!FRAME_WINDOW_P (it->f));
12477
12478 /* Get the truncation glyphs. */
12479 truncate_it = *it;
12480 truncate_it.current_x = 0;
12481 truncate_it.face_id = DEFAULT_FACE_ID;
12482 truncate_it.glyph_row = &scratch_glyph_row;
12483 truncate_it.glyph_row->used[TEXT_AREA] = 0;
12484 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
12485 truncate_it.object = make_number (0);
12486 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
12487
12488 /* Overwrite glyphs from IT with truncation glyphs. */
12489 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
12490 end = from + truncate_it.glyph_row->used[TEXT_AREA];
12491 to = it->glyph_row->glyphs[TEXT_AREA];
12492 toend = to + it->glyph_row->used[TEXT_AREA];
12493
12494 while (from < end)
12495 *to++ = *from++;
12496
12497 /* There may be padding glyphs left over. Overwrite them too. */
12498 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
12499 {
12500 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
12501 while (from < end)
12502 *to++ = *from++;
12503 }
12504
12505 if (to > toend)
12506 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
12507 }
12508
12509
12510 /* Compute the pixel height and width of IT->glyph_row.
12511
12512 Most of the time, ascent and height of a display line will be equal
12513 to the max_ascent and max_height values of the display iterator
12514 structure. This is not the case if
12515
12516 1. We hit ZV without displaying anything. In this case, max_ascent
12517 and max_height will be zero.
12518
12519 2. We have some glyphs that don't contribute to the line height.
12520 (The glyph row flag contributes_to_line_height_p is for future
12521 pixmap extensions).
12522
12523 The first case is easily covered by using default values because in
12524 these cases, the line height does not really matter, except that it
12525 must not be zero. */
12526
12527 static void
12528 compute_line_metrics (it)
12529 struct it *it;
12530 {
12531 struct glyph_row *row = it->glyph_row;
12532 int area, i;
12533
12534 if (FRAME_WINDOW_P (it->f))
12535 {
12536 int i, min_y, max_y;
12537
12538 /* The line may consist of one space only, that was added to
12539 place the cursor on it. If so, the row's height hasn't been
12540 computed yet. */
12541 if (row->height == 0)
12542 {
12543 if (it->max_ascent + it->max_descent == 0)
12544 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
12545 row->ascent = it->max_ascent;
12546 row->height = it->max_ascent + it->max_descent;
12547 row->phys_ascent = it->max_phys_ascent;
12548 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
12549 }
12550
12551 /* Compute the width of this line. */
12552 row->pixel_width = row->x;
12553 for (i = 0; i < row->used[TEXT_AREA]; ++i)
12554 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
12555
12556 xassert (row->pixel_width >= 0);
12557 xassert (row->ascent >= 0 && row->height > 0);
12558
12559 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
12560 || MATRIX_ROW_OVERLAPS_PRED_P (row));
12561
12562 /* If first line's physical ascent is larger than its logical
12563 ascent, use the physical ascent, and make the row taller.
12564 This makes accented characters fully visible. */
12565 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
12566 && row->phys_ascent > row->ascent)
12567 {
12568 row->height += row->phys_ascent - row->ascent;
12569 row->ascent = row->phys_ascent;
12570 }
12571
12572 /* Compute how much of the line is visible. */
12573 row->visible_height = row->height;
12574
12575 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (it->w);
12576 max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
12577
12578 if (row->y < min_y)
12579 row->visible_height -= min_y - row->y;
12580 if (row->y + row->height > max_y)
12581 row->visible_height -= row->y + row->height - max_y;
12582 }
12583 else
12584 {
12585 row->pixel_width = row->used[TEXT_AREA];
12586 if (row->continued_p)
12587 row->pixel_width -= it->continuation_pixel_width;
12588 else if (row->truncated_on_right_p)
12589 row->pixel_width -= it->truncation_pixel_width;
12590 row->ascent = row->phys_ascent = 0;
12591 row->height = row->phys_height = row->visible_height = 1;
12592 }
12593
12594 /* Compute a hash code for this row. */
12595 row->hash = 0;
12596 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12597 for (i = 0; i < row->used[area]; ++i)
12598 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
12599 + row->glyphs[area][i].u.val
12600 + row->glyphs[area][i].face_id
12601 + row->glyphs[area][i].padding_p
12602 + (row->glyphs[area][i].type << 2));
12603
12604 it->max_ascent = it->max_descent = 0;
12605 it->max_phys_ascent = it->max_phys_descent = 0;
12606 }
12607
12608
12609 /* Append one space to the glyph row of iterator IT if doing a
12610 window-based redisplay. DEFAULT_FACE_P non-zero means let the
12611 space have the default face, otherwise let it have the same face as
12612 IT->face_id. Value is non-zero if a space was added.
12613
12614 This function is called to make sure that there is always one glyph
12615 at the end of a glyph row that the cursor can be set on under
12616 window-systems. (If there weren't such a glyph we would not know
12617 how wide and tall a box cursor should be displayed).
12618
12619 At the same time this space let's a nicely handle clearing to the
12620 end of the line if the row ends in italic text. */
12621
12622 static int
12623 append_space (it, default_face_p)
12624 struct it *it;
12625 int default_face_p;
12626 {
12627 if (FRAME_WINDOW_P (it->f))
12628 {
12629 int n = it->glyph_row->used[TEXT_AREA];
12630
12631 if (it->glyph_row->glyphs[TEXT_AREA] + n
12632 < it->glyph_row->glyphs[1 + TEXT_AREA])
12633 {
12634 /* Save some values that must not be changed.
12635 Must save IT->c and IT->len because otherwise
12636 ITERATOR_AT_END_P wouldn't work anymore after
12637 append_space has been called. */
12638 enum display_element_type saved_what = it->what;
12639 int saved_c = it->c, saved_len = it->len;
12640 int saved_x = it->current_x;
12641 int saved_face_id = it->face_id;
12642 struct text_pos saved_pos;
12643 Lisp_Object saved_object;
12644 struct face *face;
12645
12646 saved_object = it->object;
12647 saved_pos = it->position;
12648
12649 it->what = IT_CHARACTER;
12650 bzero (&it->position, sizeof it->position);
12651 it->object = make_number (0);
12652 it->c = ' ';
12653 it->len = 1;
12654
12655 if (default_face_p)
12656 it->face_id = DEFAULT_FACE_ID;
12657 else if (it->face_before_selective_p)
12658 it->face_id = it->saved_face_id;
12659 face = FACE_FROM_ID (it->f, it->face_id);
12660 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
12661
12662 PRODUCE_GLYPHS (it);
12663
12664 it->current_x = saved_x;
12665 it->object = saved_object;
12666 it->position = saved_pos;
12667 it->what = saved_what;
12668 it->face_id = saved_face_id;
12669 it->len = saved_len;
12670 it->c = saved_c;
12671 return 1;
12672 }
12673 }
12674
12675 return 0;
12676 }
12677
12678
12679 /* Extend the face of the last glyph in the text area of IT->glyph_row
12680 to the end of the display line. Called from display_line.
12681 If the glyph row is empty, add a space glyph to it so that we
12682 know the face to draw. Set the glyph row flag fill_line_p. */
12683
12684 static void
12685 extend_face_to_end_of_line (it)
12686 struct it *it;
12687 {
12688 struct face *face;
12689 struct frame *f = it->f;
12690
12691 /* If line is already filled, do nothing. */
12692 if (it->current_x >= it->last_visible_x)
12693 return;
12694
12695 /* Face extension extends the background and box of IT->face_id
12696 to the end of the line. If the background equals the background
12697 of the frame, we don't have to do anything. */
12698 if (it->face_before_selective_p)
12699 face = FACE_FROM_ID (it->f, it->saved_face_id);
12700 else
12701 face = FACE_FROM_ID (f, it->face_id);
12702
12703 if (FRAME_WINDOW_P (f)
12704 && face->box == FACE_NO_BOX
12705 && face->background == FRAME_BACKGROUND_PIXEL (f)
12706 && !face->stipple)
12707 return;
12708
12709 /* Set the glyph row flag indicating that the face of the last glyph
12710 in the text area has to be drawn to the end of the text area. */
12711 it->glyph_row->fill_line_p = 1;
12712
12713 /* If current character of IT is not ASCII, make sure we have the
12714 ASCII face. This will be automatically undone the next time
12715 get_next_display_element returns a multibyte character. Note
12716 that the character will always be single byte in unibyte text. */
12717 if (!SINGLE_BYTE_CHAR_P (it->c))
12718 {
12719 it->face_id = FACE_FOR_CHAR (f, face, 0);
12720 }
12721
12722 if (FRAME_WINDOW_P (f))
12723 {
12724 /* If the row is empty, add a space with the current face of IT,
12725 so that we know which face to draw. */
12726 if (it->glyph_row->used[TEXT_AREA] == 0)
12727 {
12728 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
12729 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
12730 it->glyph_row->used[TEXT_AREA] = 1;
12731 }
12732 }
12733 else
12734 {
12735 /* Save some values that must not be changed. */
12736 int saved_x = it->current_x;
12737 struct text_pos saved_pos;
12738 Lisp_Object saved_object;
12739 enum display_element_type saved_what = it->what;
12740 int saved_face_id = it->face_id;
12741
12742 saved_object = it->object;
12743 saved_pos = it->position;
12744
12745 it->what = IT_CHARACTER;
12746 bzero (&it->position, sizeof it->position);
12747 it->object = make_number (0);
12748 it->c = ' ';
12749 it->len = 1;
12750 it->face_id = face->id;
12751
12752 PRODUCE_GLYPHS (it);
12753
12754 while (it->current_x <= it->last_visible_x)
12755 PRODUCE_GLYPHS (it);
12756
12757 /* Don't count these blanks really. It would let us insert a left
12758 truncation glyph below and make us set the cursor on them, maybe. */
12759 it->current_x = saved_x;
12760 it->object = saved_object;
12761 it->position = saved_pos;
12762 it->what = saved_what;
12763 it->face_id = saved_face_id;
12764 }
12765 }
12766
12767
12768 /* Value is non-zero if text starting at CHARPOS in current_buffer is
12769 trailing whitespace. */
12770
12771 static int
12772 trailing_whitespace_p (charpos)
12773 int charpos;
12774 {
12775 int bytepos = CHAR_TO_BYTE (charpos);
12776 int c = 0;
12777
12778 while (bytepos < ZV_BYTE
12779 && (c = FETCH_CHAR (bytepos),
12780 c == ' ' || c == '\t'))
12781 ++bytepos;
12782
12783 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
12784 {
12785 if (bytepos != PT_BYTE)
12786 return 1;
12787 }
12788 return 0;
12789 }
12790
12791
12792 /* Highlight trailing whitespace, if any, in ROW. */
12793
12794 void
12795 highlight_trailing_whitespace (f, row)
12796 struct frame *f;
12797 struct glyph_row *row;
12798 {
12799 int used = row->used[TEXT_AREA];
12800
12801 if (used)
12802 {
12803 struct glyph *start = row->glyphs[TEXT_AREA];
12804 struct glyph *glyph = start + used - 1;
12805
12806 /* Skip over glyphs inserted to display the cursor at the
12807 end of a line, for extending the face of the last glyph
12808 to the end of the line on terminals, and for truncation
12809 and continuation glyphs. */
12810 while (glyph >= start
12811 && glyph->type == CHAR_GLYPH
12812 && INTEGERP (glyph->object))
12813 --glyph;
12814
12815 /* If last glyph is a space or stretch, and it's trailing
12816 whitespace, set the face of all trailing whitespace glyphs in
12817 IT->glyph_row to `trailing-whitespace'. */
12818 if (glyph >= start
12819 && BUFFERP (glyph->object)
12820 && (glyph->type == STRETCH_GLYPH
12821 || (glyph->type == CHAR_GLYPH
12822 && glyph->u.ch == ' '))
12823 && trailing_whitespace_p (glyph->charpos))
12824 {
12825 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
12826
12827 while (glyph >= start
12828 && BUFFERP (glyph->object)
12829 && (glyph->type == STRETCH_GLYPH
12830 || (glyph->type == CHAR_GLYPH
12831 && glyph->u.ch == ' ')))
12832 (glyph--)->face_id = face_id;
12833 }
12834 }
12835 }
12836
12837
12838 /* Value is non-zero if glyph row ROW in window W should be
12839 used to hold the cursor. */
12840
12841 static int
12842 cursor_row_p (w, row)
12843 struct window *w;
12844 struct glyph_row *row;
12845 {
12846 int cursor_row_p = 1;
12847
12848 if (PT == MATRIX_ROW_END_CHARPOS (row))
12849 {
12850 /* If the row ends with a newline from a string, we don't want
12851 the cursor there (if the row is continued it doesn't end in a
12852 newline). */
12853 if (CHARPOS (row->end.string_pos) >= 0
12854 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12855 cursor_row_p = row->continued_p;
12856
12857 /* If the row ends at ZV, display the cursor at the end of that
12858 row instead of at the start of the row below. */
12859 else if (row->ends_at_zv_p)
12860 cursor_row_p = 1;
12861 else
12862 cursor_row_p = 0;
12863 }
12864
12865 return cursor_row_p;
12866 }
12867
12868
12869 /* Construct the glyph row IT->glyph_row in the desired matrix of
12870 IT->w from text at the current position of IT. See dispextern.h
12871 for an overview of struct it. Value is non-zero if
12872 IT->glyph_row displays text, as opposed to a line displaying ZV
12873 only. */
12874
12875 static int
12876 display_line (it)
12877 struct it *it;
12878 {
12879 struct glyph_row *row = it->glyph_row;
12880
12881 /* We always start displaying at hpos zero even if hscrolled. */
12882 xassert (it->hpos == 0 && it->current_x == 0);
12883
12884 /* We must not display in a row that's not a text row. */
12885 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
12886 < it->w->desired_matrix->nrows);
12887
12888 /* Is IT->w showing the region? */
12889 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
12890
12891 /* Clear the result glyph row and enable it. */
12892 prepare_desired_row (row);
12893
12894 row->y = it->current_y;
12895 row->start = it->current;
12896 row->continuation_lines_width = it->continuation_lines_width;
12897 row->displays_text_p = 1;
12898 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
12899 it->starts_in_middle_of_char_p = 0;
12900
12901 /* Arrange the overlays nicely for our purposes. Usually, we call
12902 display_line on only one line at a time, in which case this
12903 can't really hurt too much, or we call it on lines which appear
12904 one after another in the buffer, in which case all calls to
12905 recenter_overlay_lists but the first will be pretty cheap. */
12906 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
12907
12908 /* Move over display elements that are not visible because we are
12909 hscrolled. This may stop at an x-position < IT->first_visible_x
12910 if the first glyph is partially visible or if we hit a line end. */
12911 if (it->current_x < it->first_visible_x)
12912 move_it_in_display_line_to (it, ZV, it->first_visible_x,
12913 MOVE_TO_POS | MOVE_TO_X);
12914
12915 /* Get the initial row height. This is either the height of the
12916 text hscrolled, if there is any, or zero. */
12917 row->ascent = it->max_ascent;
12918 row->height = it->max_ascent + it->max_descent;
12919 row->phys_ascent = it->max_phys_ascent;
12920 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
12921
12922 /* Loop generating characters. The loop is left with IT on the next
12923 character to display. */
12924 while (1)
12925 {
12926 int n_glyphs_before, hpos_before, x_before;
12927 int x, i, nglyphs;
12928 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
12929
12930 /* Retrieve the next thing to display. Value is zero if end of
12931 buffer reached. */
12932 if (!get_next_display_element (it))
12933 {
12934 /* Maybe add a space at the end of this line that is used to
12935 display the cursor there under X. Set the charpos of the
12936 first glyph of blank lines not corresponding to any text
12937 to -1. */
12938 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
12939 || row->used[TEXT_AREA] == 0)
12940 {
12941 row->glyphs[TEXT_AREA]->charpos = -1;
12942 row->displays_text_p = 0;
12943
12944 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
12945 && (!MINI_WINDOW_P (it->w)
12946 || (minibuf_level && EQ (it->window, minibuf_window))))
12947 row->indicate_empty_line_p = 1;
12948 }
12949
12950 it->continuation_lines_width = 0;
12951 row->ends_at_zv_p = 1;
12952 break;
12953 }
12954
12955 /* Now, get the metrics of what we want to display. This also
12956 generates glyphs in `row' (which is IT->glyph_row). */
12957 n_glyphs_before = row->used[TEXT_AREA];
12958 x = it->current_x;
12959
12960 /* Remember the line height so far in case the next element doesn't
12961 fit on the line. */
12962 if (!it->truncate_lines_p)
12963 {
12964 ascent = it->max_ascent;
12965 descent = it->max_descent;
12966 phys_ascent = it->max_phys_ascent;
12967 phys_descent = it->max_phys_descent;
12968 }
12969
12970 PRODUCE_GLYPHS (it);
12971
12972 /* If this display element was in marginal areas, continue with
12973 the next one. */
12974 if (it->area != TEXT_AREA)
12975 {
12976 row->ascent = max (row->ascent, it->max_ascent);
12977 row->height = max (row->height, it->max_ascent + it->max_descent);
12978 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12979 row->phys_height = max (row->phys_height,
12980 it->max_phys_ascent + it->max_phys_descent);
12981 set_iterator_to_next (it, 1);
12982 continue;
12983 }
12984
12985 /* Does the display element fit on the line? If we truncate
12986 lines, we should draw past the right edge of the window. If
12987 we don't truncate, we want to stop so that we can display the
12988 continuation glyph before the right margin. If lines are
12989 continued, there are two possible strategies for characters
12990 resulting in more than 1 glyph (e.g. tabs): Display as many
12991 glyphs as possible in this line and leave the rest for the
12992 continuation line, or display the whole element in the next
12993 line. Original redisplay did the former, so we do it also. */
12994 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
12995 hpos_before = it->hpos;
12996 x_before = x;
12997
12998 if (/* Not a newline. */
12999 nglyphs > 0
13000 /* Glyphs produced fit entirely in the line. */
13001 && it->current_x < it->last_visible_x)
13002 {
13003 it->hpos += nglyphs;
13004 row->ascent = max (row->ascent, it->max_ascent);
13005 row->height = max (row->height, it->max_ascent + it->max_descent);
13006 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13007 row->phys_height = max (row->phys_height,
13008 it->max_phys_ascent + it->max_phys_descent);
13009 if (it->current_x - it->pixel_width < it->first_visible_x)
13010 row->x = x - it->first_visible_x;
13011 }
13012 else
13013 {
13014 int new_x;
13015 struct glyph *glyph;
13016
13017 for (i = 0; i < nglyphs; ++i, x = new_x)
13018 {
13019 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
13020 new_x = x + glyph->pixel_width;
13021
13022 if (/* Lines are continued. */
13023 !it->truncate_lines_p
13024 && (/* Glyph doesn't fit on the line. */
13025 new_x > it->last_visible_x
13026 /* Or it fits exactly on a window system frame. */
13027 || (new_x == it->last_visible_x
13028 && FRAME_WINDOW_P (it->f))))
13029 {
13030 /* End of a continued line. */
13031
13032 if (it->hpos == 0
13033 || (new_x == it->last_visible_x
13034 && FRAME_WINDOW_P (it->f)))
13035 {
13036 /* Current glyph is the only one on the line or
13037 fits exactly on the line. We must continue
13038 the line because we can't draw the cursor
13039 after the glyph. */
13040 row->continued_p = 1;
13041 it->current_x = new_x;
13042 it->continuation_lines_width += new_x;
13043 ++it->hpos;
13044 if (i == nglyphs - 1)
13045 set_iterator_to_next (it, 1);
13046 }
13047 else if (CHAR_GLYPH_PADDING_P (*glyph)
13048 && !FRAME_WINDOW_P (it->f))
13049 {
13050 /* A padding glyph that doesn't fit on this line.
13051 This means the whole character doesn't fit
13052 on the line. */
13053 row->used[TEXT_AREA] = n_glyphs_before;
13054
13055 /* Fill the rest of the row with continuation
13056 glyphs like in 20.x. */
13057 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
13058 < row->glyphs[1 + TEXT_AREA])
13059 produce_special_glyphs (it, IT_CONTINUATION);
13060
13061 row->continued_p = 1;
13062 it->current_x = x_before;
13063 it->continuation_lines_width += x_before;
13064
13065 /* Restore the height to what it was before the
13066 element not fitting on the line. */
13067 it->max_ascent = ascent;
13068 it->max_descent = descent;
13069 it->max_phys_ascent = phys_ascent;
13070 it->max_phys_descent = phys_descent;
13071 }
13072 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
13073 {
13074 /* A TAB that extends past the right edge of the
13075 window. This produces a single glyph on
13076 window system frames. We leave the glyph in
13077 this row and let it fill the row, but don't
13078 consume the TAB. */
13079 it->continuation_lines_width += it->last_visible_x;
13080 row->ends_in_middle_of_char_p = 1;
13081 row->continued_p = 1;
13082 glyph->pixel_width = it->last_visible_x - x;
13083 it->starts_in_middle_of_char_p = 1;
13084 }
13085 else
13086 {
13087 /* Something other than a TAB that draws past
13088 the right edge of the window. Restore
13089 positions to values before the element. */
13090 row->used[TEXT_AREA] = n_glyphs_before + i;
13091
13092 /* Display continuation glyphs. */
13093 if (!FRAME_WINDOW_P (it->f))
13094 produce_special_glyphs (it, IT_CONTINUATION);
13095 row->continued_p = 1;
13096
13097 it->continuation_lines_width += x;
13098
13099 if (nglyphs > 1 && i > 0)
13100 {
13101 row->ends_in_middle_of_char_p = 1;
13102 it->starts_in_middle_of_char_p = 1;
13103 }
13104
13105 /* Restore the height to what it was before the
13106 element not fitting on the line. */
13107 it->max_ascent = ascent;
13108 it->max_descent = descent;
13109 it->max_phys_ascent = phys_ascent;
13110 it->max_phys_descent = phys_descent;
13111 }
13112
13113 break;
13114 }
13115 else if (new_x > it->first_visible_x)
13116 {
13117 /* Increment number of glyphs actually displayed. */
13118 ++it->hpos;
13119
13120 if (x < it->first_visible_x)
13121 /* Glyph is partially visible, i.e. row starts at
13122 negative X position. */
13123 row->x = x - it->first_visible_x;
13124 }
13125 else
13126 {
13127 /* Glyph is completely off the left margin of the
13128 window. This should not happen because of the
13129 move_it_in_display_line at the start of
13130 this function. */
13131 abort ();
13132 }
13133 }
13134
13135 row->ascent = max (row->ascent, it->max_ascent);
13136 row->height = max (row->height, it->max_ascent + it->max_descent);
13137 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13138 row->phys_height = max (row->phys_height,
13139 it->max_phys_ascent + it->max_phys_descent);
13140
13141 /* End of this display line if row is continued. */
13142 if (row->continued_p)
13143 break;
13144 }
13145
13146 /* Is this a line end? If yes, we're also done, after making
13147 sure that a non-default face is extended up to the right
13148 margin of the window. */
13149 if (ITERATOR_AT_END_OF_LINE_P (it))
13150 {
13151 int used_before = row->used[TEXT_AREA];
13152
13153 row->ends_in_newline_from_string_p = STRINGP (it->object);
13154
13155 /* Add a space at the end of the line that is used to
13156 display the cursor there. */
13157 append_space (it, 0);
13158
13159 /* Extend the face to the end of the line. */
13160 extend_face_to_end_of_line (it);
13161
13162 /* Make sure we have the position. */
13163 if (used_before == 0)
13164 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
13165
13166 /* Consume the line end. This skips over invisible lines. */
13167 set_iterator_to_next (it, 1);
13168 it->continuation_lines_width = 0;
13169 break;
13170 }
13171
13172 /* Proceed with next display element. Note that this skips
13173 over lines invisible because of selective display. */
13174 set_iterator_to_next (it, 1);
13175
13176 /* If we truncate lines, we are done when the last displayed
13177 glyphs reach past the right margin of the window. */
13178 if (it->truncate_lines_p
13179 && (FRAME_WINDOW_P (it->f)
13180 ? (it->current_x >= it->last_visible_x)
13181 : (it->current_x > it->last_visible_x)))
13182 {
13183 /* Maybe add truncation glyphs. */
13184 if (!FRAME_WINDOW_P (it->f))
13185 {
13186 int i, n;
13187
13188 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
13189 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
13190 break;
13191
13192 for (n = row->used[TEXT_AREA]; i < n; ++i)
13193 {
13194 row->used[TEXT_AREA] = i;
13195 produce_special_glyphs (it, IT_TRUNCATION);
13196 }
13197 }
13198
13199 row->truncated_on_right_p = 1;
13200 it->continuation_lines_width = 0;
13201 reseat_at_next_visible_line_start (it, 0);
13202 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
13203 it->hpos = hpos_before;
13204 it->current_x = x_before;
13205 break;
13206 }
13207 }
13208
13209 /* If line is not empty and hscrolled, maybe insert truncation glyphs
13210 at the left window margin. */
13211 if (it->first_visible_x
13212 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
13213 {
13214 if (!FRAME_WINDOW_P (it->f))
13215 insert_left_trunc_glyphs (it);
13216 row->truncated_on_left_p = 1;
13217 }
13218
13219 /* If the start of this line is the overlay arrow-position, then
13220 mark this glyph row as the one containing the overlay arrow.
13221 This is clearly a mess with variable size fonts. It would be
13222 better to let it be displayed like cursors under X. */
13223 if (MARKERP (Voverlay_arrow_position)
13224 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
13225 && (MATRIX_ROW_START_CHARPOS (row)
13226 == marker_position (Voverlay_arrow_position))
13227 && STRINGP (Voverlay_arrow_string)
13228 && ! overlay_arrow_seen)
13229 {
13230 /* Overlay arrow in window redisplay is a fringe bitmap. */
13231 if (!FRAME_WINDOW_P (it->f))
13232 {
13233 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
13234 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
13235 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
13236 struct glyph *p = row->glyphs[TEXT_AREA];
13237 struct glyph *p2, *end;
13238
13239 /* Copy the arrow glyphs. */
13240 while (glyph < arrow_end)
13241 *p++ = *glyph++;
13242
13243 /* Throw away padding glyphs. */
13244 p2 = p;
13245 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
13246 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
13247 ++p2;
13248 if (p2 > p)
13249 {
13250 while (p2 < end)
13251 *p++ = *p2++;
13252 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
13253 }
13254 }
13255
13256 overlay_arrow_seen = 1;
13257 row->overlay_arrow_p = 1;
13258 }
13259
13260 /* Compute pixel dimensions of this line. */
13261 compute_line_metrics (it);
13262
13263 /* Remember the position at which this line ends. */
13264 row->end = it->current;
13265
13266 /* Maybe set the cursor. */
13267 if (it->w->cursor.vpos < 0
13268 && PT >= MATRIX_ROW_START_CHARPOS (row)
13269 && PT <= MATRIX_ROW_END_CHARPOS (row)
13270 && cursor_row_p (it->w, row))
13271 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
13272
13273 /* Highlight trailing whitespace. */
13274 if (!NILP (Vshow_trailing_whitespace))
13275 highlight_trailing_whitespace (it->f, it->glyph_row);
13276
13277 /* Prepare for the next line. This line starts horizontally at (X
13278 HPOS) = (0 0). Vertical positions are incremented. As a
13279 convenience for the caller, IT->glyph_row is set to the next
13280 row to be used. */
13281 it->current_x = it->hpos = 0;
13282 it->current_y += row->height;
13283 ++it->vpos;
13284 ++it->glyph_row;
13285 return row->displays_text_p;
13286 }
13287
13288
13289 \f
13290 /***********************************************************************
13291 Menu Bar
13292 ***********************************************************************/
13293
13294 /* Redisplay the menu bar in the frame for window W.
13295
13296 The menu bar of X frames that don't have X toolkit support is
13297 displayed in a special window W->frame->menu_bar_window.
13298
13299 The menu bar of terminal frames is treated specially as far as
13300 glyph matrices are concerned. Menu bar lines are not part of
13301 windows, so the update is done directly on the frame matrix rows
13302 for the menu bar. */
13303
13304 static void
13305 display_menu_bar (w)
13306 struct window *w;
13307 {
13308 struct frame *f = XFRAME (WINDOW_FRAME (w));
13309 struct it it;
13310 Lisp_Object items;
13311 int i;
13312
13313 /* Don't do all this for graphical frames. */
13314 #ifdef HAVE_NTGUI
13315 if (!NILP (Vwindow_system))
13316 return;
13317 #endif
13318 #ifdef USE_X_TOOLKIT
13319 if (FRAME_X_P (f))
13320 return;
13321 #endif
13322 #ifdef macintosh
13323 if (FRAME_MAC_P (f))
13324 return;
13325 #endif
13326
13327 #ifdef USE_X_TOOLKIT
13328 xassert (!FRAME_WINDOW_P (f));
13329 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
13330 it.first_visible_x = 0;
13331 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
13332 #else /* not USE_X_TOOLKIT */
13333 if (FRAME_WINDOW_P (f))
13334 {
13335 /* Menu bar lines are displayed in the desired matrix of the
13336 dummy window menu_bar_window. */
13337 struct window *menu_w;
13338 xassert (WINDOWP (f->menu_bar_window));
13339 menu_w = XWINDOW (f->menu_bar_window);
13340 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
13341 MENU_FACE_ID);
13342 it.first_visible_x = 0;
13343 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
13344 }
13345 else
13346 {
13347 /* This is a TTY frame, i.e. character hpos/vpos are used as
13348 pixel x/y. */
13349 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
13350 MENU_FACE_ID);
13351 it.first_visible_x = 0;
13352 it.last_visible_x = FRAME_WIDTH (f);
13353 }
13354 #endif /* not USE_X_TOOLKIT */
13355
13356 if (! mode_line_inverse_video)
13357 /* Force the menu-bar to be displayed in the default face. */
13358 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
13359
13360 /* Clear all rows of the menu bar. */
13361 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
13362 {
13363 struct glyph_row *row = it.glyph_row + i;
13364 clear_glyph_row (row);
13365 row->enabled_p = 1;
13366 row->full_width_p = 1;
13367 }
13368
13369 /* Display all items of the menu bar. */
13370 items = FRAME_MENU_BAR_ITEMS (it.f);
13371 for (i = 0; i < XVECTOR (items)->size; i += 4)
13372 {
13373 Lisp_Object string;
13374
13375 /* Stop at nil string. */
13376 string = AREF (items, i + 1);
13377 if (NILP (string))
13378 break;
13379
13380 /* Remember where item was displayed. */
13381 AREF (items, i + 3) = make_number (it.hpos);
13382
13383 /* Display the item, pad with one space. */
13384 if (it.current_x < it.last_visible_x)
13385 display_string (NULL, string, Qnil, 0, 0, &it,
13386 XSTRING (string)->size + 1, 0, 0, -1);
13387 }
13388
13389 /* Fill out the line with spaces. */
13390 if (it.current_x < it.last_visible_x)
13391 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
13392
13393 /* Compute the total height of the lines. */
13394 compute_line_metrics (&it);
13395 }
13396
13397
13398 \f
13399 /***********************************************************************
13400 Mode Line
13401 ***********************************************************************/
13402
13403 /* Redisplay mode lines in the window tree whose root is WINDOW. If
13404 FORCE is non-zero, redisplay mode lines unconditionally.
13405 Otherwise, redisplay only mode lines that are garbaged. Value is
13406 the number of windows whose mode lines were redisplayed. */
13407
13408 static int
13409 redisplay_mode_lines (window, force)
13410 Lisp_Object window;
13411 int force;
13412 {
13413 int nwindows = 0;
13414
13415 while (!NILP (window))
13416 {
13417 struct window *w = XWINDOW (window);
13418
13419 if (WINDOWP (w->hchild))
13420 nwindows += redisplay_mode_lines (w->hchild, force);
13421 else if (WINDOWP (w->vchild))
13422 nwindows += redisplay_mode_lines (w->vchild, force);
13423 else if (force
13424 || FRAME_GARBAGED_P (XFRAME (w->frame))
13425 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
13426 {
13427 struct text_pos lpoint;
13428 struct buffer *old = current_buffer;
13429
13430 /* Set the window's buffer for the mode line display. */
13431 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13432 set_buffer_internal_1 (XBUFFER (w->buffer));
13433
13434 /* Point refers normally to the selected window. For any
13435 other window, set up appropriate value. */
13436 if (!EQ (window, selected_window))
13437 {
13438 struct text_pos pt;
13439
13440 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
13441 if (CHARPOS (pt) < BEGV)
13442 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
13443 else if (CHARPOS (pt) > (ZV - 1))
13444 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
13445 else
13446 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
13447 }
13448
13449 /* Display mode lines. */
13450 clear_glyph_matrix (w->desired_matrix);
13451 if (display_mode_lines (w))
13452 {
13453 ++nwindows;
13454 w->must_be_updated_p = 1;
13455 }
13456
13457 /* Restore old settings. */
13458 set_buffer_internal_1 (old);
13459 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13460 }
13461
13462 window = w->next;
13463 }
13464
13465 return nwindows;
13466 }
13467
13468
13469 /* Display the mode and/or top line of window W. Value is the number
13470 of mode lines displayed. */
13471
13472 static int
13473 display_mode_lines (w)
13474 struct window *w;
13475 {
13476 Lisp_Object old_selected_window, old_selected_frame;
13477 int n = 0;
13478
13479 old_selected_frame = selected_frame;
13480 selected_frame = w->frame;
13481 old_selected_window = selected_window;
13482 XSETWINDOW (selected_window, w);
13483
13484 /* These will be set while the mode line specs are processed. */
13485 line_number_displayed = 0;
13486 w->column_number_displayed = Qnil;
13487
13488 if (WINDOW_WANTS_MODELINE_P (w))
13489 {
13490 struct window *sel_w = XWINDOW (old_selected_window);
13491
13492 /* Select mode line face based on the real selected window. */
13493 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
13494 current_buffer->mode_line_format);
13495 ++n;
13496 }
13497
13498 if (WINDOW_WANTS_HEADER_LINE_P (w))
13499 {
13500 display_mode_line (w, HEADER_LINE_FACE_ID,
13501 current_buffer->header_line_format);
13502 ++n;
13503 }
13504
13505 selected_frame = old_selected_frame;
13506 selected_window = old_selected_window;
13507 return n;
13508 }
13509
13510
13511 /* Display mode or top line of window W. FACE_ID specifies which line
13512 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
13513 FORMAT is the mode line format to display. Value is the pixel
13514 height of the mode line displayed. */
13515
13516 static int
13517 display_mode_line (w, face_id, format)
13518 struct window *w;
13519 enum face_id face_id;
13520 Lisp_Object format;
13521 {
13522 struct it it;
13523 struct face *face;
13524
13525 init_iterator (&it, w, -1, -1, NULL, face_id);
13526 prepare_desired_row (it.glyph_row);
13527
13528 if (! mode_line_inverse_video)
13529 /* Force the mode-line to be displayed in the default face. */
13530 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
13531
13532 /* Temporarily make frame's keyboard the current kboard so that
13533 kboard-local variables in the mode_line_format will get the right
13534 values. */
13535 push_frame_kboard (it.f);
13536 display_mode_element (&it, 0, 0, 0, format, Qnil);
13537 pop_frame_kboard ();
13538
13539 /* Fill up with spaces. */
13540 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
13541
13542 compute_line_metrics (&it);
13543 it.glyph_row->full_width_p = 1;
13544 it.glyph_row->mode_line_p = 1;
13545 it.glyph_row->continued_p = 0;
13546 it.glyph_row->truncated_on_left_p = 0;
13547 it.glyph_row->truncated_on_right_p = 0;
13548
13549 /* Make a 3D mode-line have a shadow at its right end. */
13550 face = FACE_FROM_ID (it.f, face_id);
13551 extend_face_to_end_of_line (&it);
13552 if (face->box != FACE_NO_BOX)
13553 {
13554 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
13555 + it.glyph_row->used[TEXT_AREA] - 1);
13556 last->right_box_line_p = 1;
13557 }
13558
13559 return it.glyph_row->height;
13560 }
13561
13562 /* Alist that caches the results of :propertize.
13563 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
13564 Lisp_Object mode_line_proptrans_alist;
13565
13566 /* Contribute ELT to the mode line for window IT->w. How it
13567 translates into text depends on its data type.
13568
13569 IT describes the display environment in which we display, as usual.
13570
13571 DEPTH is the depth in recursion. It is used to prevent
13572 infinite recursion here.
13573
13574 FIELD_WIDTH is the number of characters the display of ELT should
13575 occupy in the mode line, and PRECISION is the maximum number of
13576 characters to display from ELT's representation. See
13577 display_string for details.
13578
13579 Returns the hpos of the end of the text generated by ELT. */
13580
13581 static int
13582 display_mode_element (it, depth, field_width, precision, elt, props)
13583 struct it *it;
13584 int depth;
13585 int field_width, precision;
13586 Lisp_Object elt, props;
13587 {
13588 int n = 0, field, prec;
13589 int literal = 0;
13590
13591 tail_recurse:
13592 if (depth > 10)
13593 goto invalid;
13594
13595 depth++;
13596
13597 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
13598 {
13599 case Lisp_String:
13600 {
13601 /* A string: output it and check for %-constructs within it. */
13602 unsigned char c;
13603 unsigned char *this, *lisp_string;
13604
13605 if (!NILP (props))
13606 {
13607 Lisp_Object oprops, aelt;
13608 oprops = Ftext_properties_at (make_number (0), elt);
13609 if (NILP (Fequal (props, oprops)))
13610 {
13611 /* If the starting string has properties,
13612 merge the specified ones onto the existing ones. */
13613 if (! NILP (oprops))
13614 {
13615 Lisp_Object tem;
13616
13617 oprops = Fcopy_sequence (oprops);
13618 tem = props;
13619 while (CONSP (tem))
13620 {
13621 oprops = Fplist_put (oprops, XCAR (tem),
13622 XCAR (XCDR (tem)));
13623 tem = XCDR (XCDR (tem));
13624 }
13625 props = oprops;
13626 }
13627
13628 aelt = Fassoc (elt, mode_line_proptrans_alist);
13629 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
13630 elt = XCAR (aelt);
13631 else
13632 {
13633 elt = Fcopy_sequence (elt);
13634 Fset_text_properties (0, Flength (elt), props, elt);
13635 mode_line_proptrans_alist
13636 = Fcons (Fcons (elt, props),
13637 mode_line_proptrans_alist);
13638 }
13639 }
13640 }
13641
13642 this = XSTRING (elt)->data;
13643 lisp_string = this;
13644
13645 if (literal)
13646 {
13647 prec = precision - n;
13648 if (frame_title_ptr)
13649 n += store_frame_title (XSTRING (elt)->data, -1, prec);
13650 else
13651 n += display_string (NULL, elt, Qnil, 0, 0, it,
13652 0, prec, 0, STRING_MULTIBYTE (elt));
13653
13654 break;
13655 }
13656
13657 while ((precision <= 0 || n < precision)
13658 && *this
13659 && (frame_title_ptr
13660 || it->current_x < it->last_visible_x))
13661 {
13662 unsigned char *last = this;
13663
13664 /* Advance to end of string or next format specifier. */
13665 while ((c = *this++) != '\0' && c != '%')
13666 ;
13667
13668 if (this - 1 != last)
13669 {
13670 /* Output to end of string or up to '%'. Field width
13671 is length of string. Don't output more than
13672 PRECISION allows us. */
13673 --this;
13674
13675 prec = chars_in_text (last, this - last);
13676 if (precision > 0 && prec > precision - n)
13677 prec = precision - n;
13678
13679 if (frame_title_ptr)
13680 n += store_frame_title (last, 0, prec);
13681 else
13682 {
13683 int bytepos = last - lisp_string;
13684 int charpos = string_byte_to_char (elt, bytepos);
13685 n += display_string (NULL, elt, Qnil, 0, charpos,
13686 it, 0, prec, 0,
13687 STRING_MULTIBYTE (elt));
13688 }
13689 }
13690 else /* c == '%' */
13691 {
13692 unsigned char *percent_position = this;
13693
13694 /* Get the specified minimum width. Zero means
13695 don't pad. */
13696 field = 0;
13697 while ((c = *this++) >= '0' && c <= '9')
13698 field = field * 10 + c - '0';
13699
13700 /* Don't pad beyond the total padding allowed. */
13701 if (field_width - n > 0 && field > field_width - n)
13702 field = field_width - n;
13703
13704 /* Note that either PRECISION <= 0 or N < PRECISION. */
13705 prec = precision - n;
13706
13707 if (c == 'M')
13708 n += display_mode_element (it, depth, field, prec,
13709 Vglobal_mode_string, props);
13710 else if (c != 0)
13711 {
13712 int multibyte;
13713 int bytepos, charpos;
13714 unsigned char *spec;
13715
13716 bytepos = percent_position - lisp_string;
13717 charpos = (STRING_MULTIBYTE (elt)
13718 ? string_byte_to_char (elt, bytepos)
13719 : bytepos);
13720
13721 spec
13722 = decode_mode_spec (it->w, c, field, prec, &multibyte);
13723
13724 if (frame_title_ptr)
13725 n += store_frame_title (spec, field, prec);
13726 else
13727 {
13728 int nglyphs_before, nwritten;
13729
13730 nglyphs_before = it->glyph_row->used[TEXT_AREA];
13731 nwritten = display_string (spec, Qnil, elt,
13732 charpos, 0, it,
13733 field, prec, 0,
13734 multibyte);
13735
13736 /* Assign to the glyphs written above the
13737 string where the `%x' came from, position
13738 of the `%'. */
13739 if (nwritten > 0)
13740 {
13741 struct glyph *glyph
13742 = (it->glyph_row->glyphs[TEXT_AREA]
13743 + nglyphs_before);
13744 int i;
13745
13746 for (i = 0; i < nwritten; ++i)
13747 {
13748 glyph[i].object = elt;
13749 glyph[i].charpos = charpos;
13750 }
13751
13752 n += nwritten;
13753 }
13754 }
13755 }
13756 else /* c == 0 */
13757 break;
13758 }
13759 }
13760 }
13761 break;
13762
13763 case Lisp_Symbol:
13764 /* A symbol: process the value of the symbol recursively
13765 as if it appeared here directly. Avoid error if symbol void.
13766 Special case: if value of symbol is a string, output the string
13767 literally. */
13768 {
13769 register Lisp_Object tem;
13770 tem = Fboundp (elt);
13771 if (!NILP (tem))
13772 {
13773 tem = Fsymbol_value (elt);
13774 /* If value is a string, output that string literally:
13775 don't check for % within it. */
13776 if (STRINGP (tem))
13777 literal = 1;
13778
13779 if (!EQ (tem, elt))
13780 {
13781 /* Give up right away for nil or t. */
13782 elt = tem;
13783 goto tail_recurse;
13784 }
13785 }
13786 }
13787 break;
13788
13789 case Lisp_Cons:
13790 {
13791 register Lisp_Object car, tem;
13792
13793 /* A cons cell: five distinct cases.
13794 If first element is :eval or :propertize, do something special.
13795 If first element is a string or a cons, process all the elements
13796 and effectively concatenate them.
13797 If first element is a negative number, truncate displaying cdr to
13798 at most that many characters. If positive, pad (with spaces)
13799 to at least that many characters.
13800 If first element is a symbol, process the cadr or caddr recursively
13801 according to whether the symbol's value is non-nil or nil. */
13802 car = XCAR (elt);
13803 if (EQ (car, QCeval))
13804 {
13805 /* An element of the form (:eval FORM) means evaluate FORM
13806 and use the result as mode line elements. */
13807
13808 if (CONSP (XCDR (elt)))
13809 {
13810 Lisp_Object spec;
13811 spec = safe_eval (XCAR (XCDR (elt)));
13812 n += display_mode_element (it, depth, field_width - n,
13813 precision - n, spec, props);
13814 }
13815 }
13816 else if (EQ (car, QCpropertize))
13817 {
13818 if (CONSP (XCDR (elt)))
13819 {
13820 /* An element of the form (:propertize ELT PROPS...)
13821 means display ELT but applying properties PROPS. */
13822 n += display_mode_element (it, depth, field_width - n,
13823 precision - n, XCAR (XCDR (elt)),
13824 XCDR (XCDR (elt)));
13825 }
13826 }
13827 else if (SYMBOLP (car))
13828 {
13829 tem = Fboundp (car);
13830 elt = XCDR (elt);
13831 if (!CONSP (elt))
13832 goto invalid;
13833 /* elt is now the cdr, and we know it is a cons cell.
13834 Use its car if CAR has a non-nil value. */
13835 if (!NILP (tem))
13836 {
13837 tem = Fsymbol_value (car);
13838 if (!NILP (tem))
13839 {
13840 elt = XCAR (elt);
13841 goto tail_recurse;
13842 }
13843 }
13844 /* Symbol's value is nil (or symbol is unbound)
13845 Get the cddr of the original list
13846 and if possible find the caddr and use that. */
13847 elt = XCDR (elt);
13848 if (NILP (elt))
13849 break;
13850 else if (!CONSP (elt))
13851 goto invalid;
13852 elt = XCAR (elt);
13853 goto tail_recurse;
13854 }
13855 else if (INTEGERP (car))
13856 {
13857 register int lim = XINT (car);
13858 elt = XCDR (elt);
13859 if (lim < 0)
13860 {
13861 /* Negative int means reduce maximum width. */
13862 if (precision <= 0)
13863 precision = -lim;
13864 else
13865 precision = min (precision, -lim);
13866 }
13867 else if (lim > 0)
13868 {
13869 /* Padding specified. Don't let it be more than
13870 current maximum. */
13871 if (precision > 0)
13872 lim = min (precision, lim);
13873
13874 /* If that's more padding than already wanted, queue it.
13875 But don't reduce padding already specified even if
13876 that is beyond the current truncation point. */
13877 field_width = max (lim, field_width);
13878 }
13879 goto tail_recurse;
13880 }
13881 else if (STRINGP (car) || CONSP (car))
13882 {
13883 register int limit = 50;
13884 /* Limit is to protect against circular lists. */
13885 while (CONSP (elt)
13886 && --limit > 0
13887 && (precision <= 0 || n < precision))
13888 {
13889 n += display_mode_element (it, depth, field_width - n,
13890 precision - n, XCAR (elt), props);
13891 elt = XCDR (elt);
13892 }
13893 }
13894 }
13895 break;
13896
13897 default:
13898 invalid:
13899 if (frame_title_ptr)
13900 n += store_frame_title ("*invalid*", 0, precision - n);
13901 else
13902 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
13903 precision - n, 0, 0);
13904 return n;
13905 }
13906
13907 /* Pad to FIELD_WIDTH. */
13908 if (field_width > 0 && n < field_width)
13909 {
13910 if (frame_title_ptr)
13911 n += store_frame_title ("", field_width - n, 0);
13912 else
13913 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
13914 0, 0, 0);
13915 }
13916
13917 return n;
13918 }
13919
13920
13921 /* Write a null-terminated, right justified decimal representation of
13922 the positive integer D to BUF using a minimal field width WIDTH. */
13923
13924 static void
13925 pint2str (buf, width, d)
13926 register char *buf;
13927 register int width;
13928 register int d;
13929 {
13930 register char *p = buf;
13931
13932 if (d <= 0)
13933 *p++ = '0';
13934 else
13935 {
13936 while (d > 0)
13937 {
13938 *p++ = d % 10 + '0';
13939 d /= 10;
13940 }
13941 }
13942
13943 for (width -= (int) (p - buf); width > 0; --width)
13944 *p++ = ' ';
13945 *p-- = '\0';
13946 while (p > buf)
13947 {
13948 d = *buf;
13949 *buf++ = *p;
13950 *p-- = d;
13951 }
13952 }
13953
13954 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
13955 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
13956 type of CODING_SYSTEM. Return updated pointer into BUF. */
13957
13958 static unsigned char invalid_eol_type[] = "(*invalid*)";
13959
13960 static char *
13961 decode_mode_spec_coding (coding_system, buf, eol_flag)
13962 Lisp_Object coding_system;
13963 register char *buf;
13964 int eol_flag;
13965 {
13966 Lisp_Object val;
13967 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
13968 unsigned char *eol_str;
13969 int eol_str_len;
13970 /* The EOL conversion we are using. */
13971 Lisp_Object eoltype;
13972
13973 val = Fget (coding_system, Qcoding_system);
13974 eoltype = Qnil;
13975
13976 if (!VECTORP (val)) /* Not yet decided. */
13977 {
13978 if (multibyte)
13979 *buf++ = '-';
13980 if (eol_flag)
13981 eoltype = eol_mnemonic_undecided;
13982 /* Don't mention EOL conversion if it isn't decided. */
13983 }
13984 else
13985 {
13986 Lisp_Object eolvalue;
13987
13988 eolvalue = Fget (coding_system, Qeol_type);
13989
13990 if (multibyte)
13991 *buf++ = XFASTINT (AREF (val, 1));
13992
13993 if (eol_flag)
13994 {
13995 /* The EOL conversion that is normal on this system. */
13996
13997 if (NILP (eolvalue)) /* Not yet decided. */
13998 eoltype = eol_mnemonic_undecided;
13999 else if (VECTORP (eolvalue)) /* Not yet decided. */
14000 eoltype = eol_mnemonic_undecided;
14001 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
14002 eoltype = (XFASTINT (eolvalue) == 0
14003 ? eol_mnemonic_unix
14004 : (XFASTINT (eolvalue) == 1
14005 ? eol_mnemonic_dos : eol_mnemonic_mac));
14006 }
14007 }
14008
14009 if (eol_flag)
14010 {
14011 /* Mention the EOL conversion if it is not the usual one. */
14012 if (STRINGP (eoltype))
14013 {
14014 eol_str = XSTRING (eoltype)->data;
14015 eol_str_len = XSTRING (eoltype)->size;
14016 }
14017 else if (INTEGERP (eoltype)
14018 && CHAR_VALID_P (XINT (eoltype), 0))
14019 {
14020 eol_str = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
14021 eol_str_len = CHAR_STRING (XINT (eoltype), eol_str);
14022 }
14023 else
14024 {
14025 eol_str = invalid_eol_type;
14026 eol_str_len = sizeof (invalid_eol_type) - 1;
14027 }
14028 bcopy (eol_str, buf, eol_str_len);
14029 buf += eol_str_len;
14030 }
14031
14032 return buf;
14033 }
14034
14035 /* Return a string for the output of a mode line %-spec for window W,
14036 generated by character C. PRECISION >= 0 means don't return a
14037 string longer than that value. FIELD_WIDTH > 0 means pad the
14038 string returned with spaces to that value. Return 1 in *MULTIBYTE
14039 if the result is multibyte text. */
14040
14041 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
14042
14043 static char *
14044 decode_mode_spec (w, c, field_width, precision, multibyte)
14045 struct window *w;
14046 register int c;
14047 int field_width, precision;
14048 int *multibyte;
14049 {
14050 Lisp_Object obj;
14051 struct frame *f = XFRAME (WINDOW_FRAME (w));
14052 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
14053 struct buffer *b = XBUFFER (w->buffer);
14054
14055 obj = Qnil;
14056 *multibyte = 0;
14057
14058 switch (c)
14059 {
14060 case '*':
14061 if (!NILP (b->read_only))
14062 return "%";
14063 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
14064 return "*";
14065 return "-";
14066
14067 case '+':
14068 /* This differs from %* only for a modified read-only buffer. */
14069 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
14070 return "*";
14071 if (!NILP (b->read_only))
14072 return "%";
14073 return "-";
14074
14075 case '&':
14076 /* This differs from %* in ignoring read-only-ness. */
14077 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
14078 return "*";
14079 return "-";
14080
14081 case '%':
14082 return "%";
14083
14084 case '[':
14085 {
14086 int i;
14087 char *p;
14088
14089 if (command_loop_level > 5)
14090 return "[[[... ";
14091 p = decode_mode_spec_buf;
14092 for (i = 0; i < command_loop_level; i++)
14093 *p++ = '[';
14094 *p = 0;
14095 return decode_mode_spec_buf;
14096 }
14097
14098 case ']':
14099 {
14100 int i;
14101 char *p;
14102
14103 if (command_loop_level > 5)
14104 return " ...]]]";
14105 p = decode_mode_spec_buf;
14106 for (i = 0; i < command_loop_level; i++)
14107 *p++ = ']';
14108 *p = 0;
14109 return decode_mode_spec_buf;
14110 }
14111
14112 case '-':
14113 {
14114 register int i;
14115
14116 /* Let lots_of_dashes be a string of infinite length. */
14117 if (field_width <= 0
14118 || field_width > sizeof (lots_of_dashes))
14119 {
14120 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
14121 decode_mode_spec_buf[i] = '-';
14122 decode_mode_spec_buf[i] = '\0';
14123 return decode_mode_spec_buf;
14124 }
14125 else
14126 return lots_of_dashes;
14127 }
14128
14129 case 'b':
14130 obj = b->name;
14131 break;
14132
14133 case 'c':
14134 {
14135 int col = current_column ();
14136 w->column_number_displayed = make_number (col);
14137 pint2str (decode_mode_spec_buf, field_width, col);
14138 return decode_mode_spec_buf;
14139 }
14140
14141 case 'F':
14142 /* %F displays the frame name. */
14143 if (!NILP (f->title))
14144 return (char *) XSTRING (f->title)->data;
14145 if (f->explicit_name || ! FRAME_WINDOW_P (f))
14146 return (char *) XSTRING (f->name)->data;
14147 return "Emacs";
14148
14149 case 'f':
14150 obj = b->filename;
14151 break;
14152
14153 case 'l':
14154 {
14155 int startpos = XMARKER (w->start)->charpos;
14156 int startpos_byte = marker_byte_position (w->start);
14157 int line, linepos, linepos_byte, topline;
14158 int nlines, junk;
14159 int height = XFASTINT (w->height);
14160
14161 /* If we decided that this buffer isn't suitable for line numbers,
14162 don't forget that too fast. */
14163 if (EQ (w->base_line_pos, w->buffer))
14164 goto no_value;
14165 /* But do forget it, if the window shows a different buffer now. */
14166 else if (BUFFERP (w->base_line_pos))
14167 w->base_line_pos = Qnil;
14168
14169 /* If the buffer is very big, don't waste time. */
14170 if (INTEGERP (Vline_number_display_limit)
14171 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
14172 {
14173 w->base_line_pos = Qnil;
14174 w->base_line_number = Qnil;
14175 goto no_value;
14176 }
14177
14178 if (!NILP (w->base_line_number)
14179 && !NILP (w->base_line_pos)
14180 && XFASTINT (w->base_line_pos) <= startpos)
14181 {
14182 line = XFASTINT (w->base_line_number);
14183 linepos = XFASTINT (w->base_line_pos);
14184 linepos_byte = buf_charpos_to_bytepos (b, linepos);
14185 }
14186 else
14187 {
14188 line = 1;
14189 linepos = BUF_BEGV (b);
14190 linepos_byte = BUF_BEGV_BYTE (b);
14191 }
14192
14193 /* Count lines from base line to window start position. */
14194 nlines = display_count_lines (linepos, linepos_byte,
14195 startpos_byte,
14196 startpos, &junk);
14197
14198 topline = nlines + line;
14199
14200 /* Determine a new base line, if the old one is too close
14201 or too far away, or if we did not have one.
14202 "Too close" means it's plausible a scroll-down would
14203 go back past it. */
14204 if (startpos == BUF_BEGV (b))
14205 {
14206 w->base_line_number = make_number (topline);
14207 w->base_line_pos = make_number (BUF_BEGV (b));
14208 }
14209 else if (nlines < height + 25 || nlines > height * 3 + 50
14210 || linepos == BUF_BEGV (b))
14211 {
14212 int limit = BUF_BEGV (b);
14213 int limit_byte = BUF_BEGV_BYTE (b);
14214 int position;
14215 int distance = (height * 2 + 30) * line_number_display_limit_width;
14216
14217 if (startpos - distance > limit)
14218 {
14219 limit = startpos - distance;
14220 limit_byte = CHAR_TO_BYTE (limit);
14221 }
14222
14223 nlines = display_count_lines (startpos, startpos_byte,
14224 limit_byte,
14225 - (height * 2 + 30),
14226 &position);
14227 /* If we couldn't find the lines we wanted within
14228 line_number_display_limit_width chars per line,
14229 give up on line numbers for this window. */
14230 if (position == limit_byte && limit == startpos - distance)
14231 {
14232 w->base_line_pos = w->buffer;
14233 w->base_line_number = Qnil;
14234 goto no_value;
14235 }
14236
14237 w->base_line_number = make_number (topline - nlines);
14238 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
14239 }
14240
14241 /* Now count lines from the start pos to point. */
14242 nlines = display_count_lines (startpos, startpos_byte,
14243 PT_BYTE, PT, &junk);
14244
14245 /* Record that we did display the line number. */
14246 line_number_displayed = 1;
14247
14248 /* Make the string to show. */
14249 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
14250 return decode_mode_spec_buf;
14251 no_value:
14252 {
14253 char* p = decode_mode_spec_buf;
14254 int pad = field_width - 2;
14255 while (pad-- > 0)
14256 *p++ = ' ';
14257 *p++ = '?';
14258 *p++ = '?';
14259 *p = '\0';
14260 return decode_mode_spec_buf;
14261 }
14262 }
14263 break;
14264
14265 case 'm':
14266 obj = b->mode_name;
14267 break;
14268
14269 case 'n':
14270 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
14271 return " Narrow";
14272 break;
14273
14274 case 'p':
14275 {
14276 int pos = marker_position (w->start);
14277 int total = BUF_ZV (b) - BUF_BEGV (b);
14278
14279 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
14280 {
14281 if (pos <= BUF_BEGV (b))
14282 return "All";
14283 else
14284 return "Bottom";
14285 }
14286 else if (pos <= BUF_BEGV (b))
14287 return "Top";
14288 else
14289 {
14290 if (total > 1000000)
14291 /* Do it differently for a large value, to avoid overflow. */
14292 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
14293 else
14294 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
14295 /* We can't normally display a 3-digit number,
14296 so get us a 2-digit number that is close. */
14297 if (total == 100)
14298 total = 99;
14299 sprintf (decode_mode_spec_buf, "%2d%%", total);
14300 return decode_mode_spec_buf;
14301 }
14302 }
14303
14304 /* Display percentage of size above the bottom of the screen. */
14305 case 'P':
14306 {
14307 int toppos = marker_position (w->start);
14308 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
14309 int total = BUF_ZV (b) - BUF_BEGV (b);
14310
14311 if (botpos >= BUF_ZV (b))
14312 {
14313 if (toppos <= BUF_BEGV (b))
14314 return "All";
14315 else
14316 return "Bottom";
14317 }
14318 else
14319 {
14320 if (total > 1000000)
14321 /* Do it differently for a large value, to avoid overflow. */
14322 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
14323 else
14324 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
14325 /* We can't normally display a 3-digit number,
14326 so get us a 2-digit number that is close. */
14327 if (total == 100)
14328 total = 99;
14329 if (toppos <= BUF_BEGV (b))
14330 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
14331 else
14332 sprintf (decode_mode_spec_buf, "%2d%%", total);
14333 return decode_mode_spec_buf;
14334 }
14335 }
14336
14337 case 's':
14338 /* status of process */
14339 obj = Fget_buffer_process (w->buffer);
14340 if (NILP (obj))
14341 return "no process";
14342 #ifdef subprocesses
14343 obj = Fsymbol_name (Fprocess_status (obj));
14344 #endif
14345 break;
14346
14347 case 't': /* indicate TEXT or BINARY */
14348 #ifdef MODE_LINE_BINARY_TEXT
14349 return MODE_LINE_BINARY_TEXT (b);
14350 #else
14351 return "T";
14352 #endif
14353
14354 case 'z':
14355 /* coding-system (not including end-of-line format) */
14356 case 'Z':
14357 /* coding-system (including end-of-line type) */
14358 {
14359 int eol_flag = (c == 'Z');
14360 char *p = decode_mode_spec_buf;
14361
14362 if (! FRAME_WINDOW_P (f))
14363 {
14364 /* No need to mention EOL here--the terminal never needs
14365 to do EOL conversion. */
14366 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
14367 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
14368 }
14369 p = decode_mode_spec_coding (b->buffer_file_coding_system,
14370 p, eol_flag);
14371
14372 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
14373 #ifdef subprocesses
14374 obj = Fget_buffer_process (Fcurrent_buffer ());
14375 if (PROCESSP (obj))
14376 {
14377 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
14378 p, eol_flag);
14379 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
14380 p, eol_flag);
14381 }
14382 #endif /* subprocesses */
14383 #endif /* 0 */
14384 *p = 0;
14385 return decode_mode_spec_buf;
14386 }
14387 }
14388
14389 if (STRINGP (obj))
14390 {
14391 *multibyte = STRING_MULTIBYTE (obj);
14392 return (char *) XSTRING (obj)->data;
14393 }
14394 else
14395 return "";
14396 }
14397
14398
14399 /* Count up to COUNT lines starting from START / START_BYTE.
14400 But don't go beyond LIMIT_BYTE.
14401 Return the number of lines thus found (always nonnegative).
14402
14403 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
14404
14405 static int
14406 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
14407 int start, start_byte, limit_byte, count;
14408 int *byte_pos_ptr;
14409 {
14410 register unsigned char *cursor;
14411 unsigned char *base;
14412
14413 register int ceiling;
14414 register unsigned char *ceiling_addr;
14415 int orig_count = count;
14416
14417 /* If we are not in selective display mode,
14418 check only for newlines. */
14419 int selective_display = (!NILP (current_buffer->selective_display)
14420 && !INTEGERP (current_buffer->selective_display));
14421
14422 if (count > 0)
14423 {
14424 while (start_byte < limit_byte)
14425 {
14426 ceiling = BUFFER_CEILING_OF (start_byte);
14427 ceiling = min (limit_byte - 1, ceiling);
14428 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
14429 base = (cursor = BYTE_POS_ADDR (start_byte));
14430 while (1)
14431 {
14432 if (selective_display)
14433 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
14434 ;
14435 else
14436 while (*cursor != '\n' && ++cursor != ceiling_addr)
14437 ;
14438
14439 if (cursor != ceiling_addr)
14440 {
14441 if (--count == 0)
14442 {
14443 start_byte += cursor - base + 1;
14444 *byte_pos_ptr = start_byte;
14445 return orig_count;
14446 }
14447 else
14448 if (++cursor == ceiling_addr)
14449 break;
14450 }
14451 else
14452 break;
14453 }
14454 start_byte += cursor - base;
14455 }
14456 }
14457 else
14458 {
14459 while (start_byte > limit_byte)
14460 {
14461 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
14462 ceiling = max (limit_byte, ceiling);
14463 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
14464 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
14465 while (1)
14466 {
14467 if (selective_display)
14468 while (--cursor != ceiling_addr
14469 && *cursor != '\n' && *cursor != 015)
14470 ;
14471 else
14472 while (--cursor != ceiling_addr && *cursor != '\n')
14473 ;
14474
14475 if (cursor != ceiling_addr)
14476 {
14477 if (++count == 0)
14478 {
14479 start_byte += cursor - base + 1;
14480 *byte_pos_ptr = start_byte;
14481 /* When scanning backwards, we should
14482 not count the newline posterior to which we stop. */
14483 return - orig_count - 1;
14484 }
14485 }
14486 else
14487 break;
14488 }
14489 /* Here we add 1 to compensate for the last decrement
14490 of CURSOR, which took it past the valid range. */
14491 start_byte += cursor - base + 1;
14492 }
14493 }
14494
14495 *byte_pos_ptr = limit_byte;
14496
14497 if (count < 0)
14498 return - orig_count + count;
14499 return orig_count - count;
14500
14501 }
14502
14503
14504 \f
14505 /***********************************************************************
14506 Displaying strings
14507 ***********************************************************************/
14508
14509 /* Display a NUL-terminated string, starting with index START.
14510
14511 If STRING is non-null, display that C string. Otherwise, the Lisp
14512 string LISP_STRING is displayed.
14513
14514 If FACE_STRING is not nil, FACE_STRING_POS is a position in
14515 FACE_STRING. Display STRING or LISP_STRING with the face at
14516 FACE_STRING_POS in FACE_STRING:
14517
14518 Display the string in the environment given by IT, but use the
14519 standard display table, temporarily.
14520
14521 FIELD_WIDTH is the minimum number of output glyphs to produce.
14522 If STRING has fewer characters than FIELD_WIDTH, pad to the right
14523 with spaces. If STRING has more characters, more than FIELD_WIDTH
14524 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
14525
14526 PRECISION is the maximum number of characters to output from
14527 STRING. PRECISION < 0 means don't truncate the string.
14528
14529 This is roughly equivalent to printf format specifiers:
14530
14531 FIELD_WIDTH PRECISION PRINTF
14532 ----------------------------------------
14533 -1 -1 %s
14534 -1 10 %.10s
14535 10 -1 %10s
14536 20 10 %20.10s
14537
14538 MULTIBYTE zero means do not display multibyte chars, > 0 means do
14539 display them, and < 0 means obey the current buffer's value of
14540 enable_multibyte_characters.
14541
14542 Value is the number of glyphs produced. */
14543
14544 static int
14545 display_string (string, lisp_string, face_string, face_string_pos,
14546 start, it, field_width, precision, max_x, multibyte)
14547 unsigned char *string;
14548 Lisp_Object lisp_string;
14549 Lisp_Object face_string;
14550 int face_string_pos;
14551 int start;
14552 struct it *it;
14553 int field_width, precision, max_x;
14554 int multibyte;
14555 {
14556 int hpos_at_start = it->hpos;
14557 int saved_face_id = it->face_id;
14558 struct glyph_row *row = it->glyph_row;
14559
14560 /* Initialize the iterator IT for iteration over STRING beginning
14561 with index START. */
14562 reseat_to_string (it, string, lisp_string, start,
14563 precision, field_width, multibyte);
14564
14565 /* If displaying STRING, set up the face of the iterator
14566 from LISP_STRING, if that's given. */
14567 if (STRINGP (face_string))
14568 {
14569 int endptr;
14570 struct face *face;
14571
14572 it->face_id
14573 = face_at_string_position (it->w, face_string, face_string_pos,
14574 0, it->region_beg_charpos,
14575 it->region_end_charpos,
14576 &endptr, it->base_face_id, 0);
14577 face = FACE_FROM_ID (it->f, it->face_id);
14578 it->face_box_p = face->box != FACE_NO_BOX;
14579 }
14580
14581 /* Set max_x to the maximum allowed X position. Don't let it go
14582 beyond the right edge of the window. */
14583 if (max_x <= 0)
14584 max_x = it->last_visible_x;
14585 else
14586 max_x = min (max_x, it->last_visible_x);
14587
14588 /* Skip over display elements that are not visible. because IT->w is
14589 hscrolled. */
14590 if (it->current_x < it->first_visible_x)
14591 move_it_in_display_line_to (it, 100000, it->first_visible_x,
14592 MOVE_TO_POS | MOVE_TO_X);
14593
14594 row->ascent = it->max_ascent;
14595 row->height = it->max_ascent + it->max_descent;
14596 row->phys_ascent = it->max_phys_ascent;
14597 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14598
14599 /* This condition is for the case that we are called with current_x
14600 past last_visible_x. */
14601 while (it->current_x < max_x)
14602 {
14603 int x_before, x, n_glyphs_before, i, nglyphs;
14604
14605 /* Get the next display element. */
14606 if (!get_next_display_element (it))
14607 break;
14608
14609 /* Produce glyphs. */
14610 x_before = it->current_x;
14611 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
14612 PRODUCE_GLYPHS (it);
14613
14614 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
14615 i = 0;
14616 x = x_before;
14617 while (i < nglyphs)
14618 {
14619 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
14620
14621 if (!it->truncate_lines_p
14622 && x + glyph->pixel_width > max_x)
14623 {
14624 /* End of continued line or max_x reached. */
14625 if (CHAR_GLYPH_PADDING_P (*glyph))
14626 {
14627 /* A wide character is unbreakable. */
14628 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
14629 it->current_x = x_before;
14630 }
14631 else
14632 {
14633 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
14634 it->current_x = x;
14635 }
14636 break;
14637 }
14638 else if (x + glyph->pixel_width > it->first_visible_x)
14639 {
14640 /* Glyph is at least partially visible. */
14641 ++it->hpos;
14642 if (x < it->first_visible_x)
14643 it->glyph_row->x = x - it->first_visible_x;
14644 }
14645 else
14646 {
14647 /* Glyph is off the left margin of the display area.
14648 Should not happen. */
14649 abort ();
14650 }
14651
14652 row->ascent = max (row->ascent, it->max_ascent);
14653 row->height = max (row->height, it->max_ascent + it->max_descent);
14654 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14655 row->phys_height = max (row->phys_height,
14656 it->max_phys_ascent + it->max_phys_descent);
14657 x += glyph->pixel_width;
14658 ++i;
14659 }
14660
14661 /* Stop if max_x reached. */
14662 if (i < nglyphs)
14663 break;
14664
14665 /* Stop at line ends. */
14666 if (ITERATOR_AT_END_OF_LINE_P (it))
14667 {
14668 it->continuation_lines_width = 0;
14669 break;
14670 }
14671
14672 set_iterator_to_next (it, 1);
14673
14674 /* Stop if truncating at the right edge. */
14675 if (it->truncate_lines_p
14676 && it->current_x >= it->last_visible_x)
14677 {
14678 /* Add truncation mark, but don't do it if the line is
14679 truncated at a padding space. */
14680 if (IT_CHARPOS (*it) < it->string_nchars)
14681 {
14682 if (!FRAME_WINDOW_P (it->f))
14683 {
14684 int i, n;
14685
14686 if (it->current_x > it->last_visible_x)
14687 {
14688 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
14689 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
14690 break;
14691 for (n = row->used[TEXT_AREA]; i < n; ++i)
14692 {
14693 row->used[TEXT_AREA] = i;
14694 produce_special_glyphs (it, IT_TRUNCATION);
14695 }
14696 }
14697 produce_special_glyphs (it, IT_TRUNCATION);
14698 }
14699 it->glyph_row->truncated_on_right_p = 1;
14700 }
14701 break;
14702 }
14703 }
14704
14705 /* Maybe insert a truncation at the left. */
14706 if (it->first_visible_x
14707 && IT_CHARPOS (*it) > 0)
14708 {
14709 if (!FRAME_WINDOW_P (it->f))
14710 insert_left_trunc_glyphs (it);
14711 it->glyph_row->truncated_on_left_p = 1;
14712 }
14713
14714 it->face_id = saved_face_id;
14715
14716 /* Value is number of columns displayed. */
14717 return it->hpos - hpos_at_start;
14718 }
14719
14720
14721 \f
14722 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
14723 appears as an element of LIST or as the car of an element of LIST.
14724 If PROPVAL is a list, compare each element against LIST in that
14725 way, and return 1/2 if any element of PROPVAL is found in LIST.
14726 Otherwise return 0. This function cannot quit.
14727 The return value is 2 if the text is invisible but with an ellipsis
14728 and 1 if it's invisible and without an ellipsis. */
14729
14730 int
14731 invisible_p (propval, list)
14732 register Lisp_Object propval;
14733 Lisp_Object list;
14734 {
14735 register Lisp_Object tail, proptail;
14736
14737 for (tail = list; CONSP (tail); tail = XCDR (tail))
14738 {
14739 register Lisp_Object tem;
14740 tem = XCAR (tail);
14741 if (EQ (propval, tem))
14742 return 1;
14743 if (CONSP (tem) && EQ (propval, XCAR (tem)))
14744 return NILP (XCDR (tem)) ? 1 : 2;
14745 }
14746
14747 if (CONSP (propval))
14748 {
14749 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
14750 {
14751 Lisp_Object propelt;
14752 propelt = XCAR (proptail);
14753 for (tail = list; CONSP (tail); tail = XCDR (tail))
14754 {
14755 register Lisp_Object tem;
14756 tem = XCAR (tail);
14757 if (EQ (propelt, tem))
14758 return 1;
14759 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
14760 return NILP (XCDR (tem)) ? 1 : 2;
14761 }
14762 }
14763 }
14764
14765 return 0;
14766 }
14767
14768 \f
14769 /***********************************************************************
14770 Initialization
14771 ***********************************************************************/
14772
14773 void
14774 syms_of_xdisp ()
14775 {
14776 Vwith_echo_area_save_vector = Qnil;
14777 staticpro (&Vwith_echo_area_save_vector);
14778
14779 Vmessage_stack = Qnil;
14780 staticpro (&Vmessage_stack);
14781
14782 Qinhibit_redisplay = intern ("inhibit-redisplay");
14783 staticpro (&Qinhibit_redisplay);
14784
14785 message_dolog_marker1 = Fmake_marker ();
14786 staticpro (&message_dolog_marker1);
14787 message_dolog_marker2 = Fmake_marker ();
14788 staticpro (&message_dolog_marker2);
14789 message_dolog_marker3 = Fmake_marker ();
14790 staticpro (&message_dolog_marker3);
14791
14792 #if GLYPH_DEBUG
14793 defsubr (&Sdump_glyph_matrix);
14794 defsubr (&Sdump_glyph_row);
14795 defsubr (&Sdump_tool_bar_row);
14796 defsubr (&Strace_redisplay);
14797 defsubr (&Strace_to_stderr);
14798 #endif
14799 #ifdef HAVE_WINDOW_SYSTEM
14800 defsubr (&Stool_bar_lines_needed);
14801 #endif
14802
14803 staticpro (&Qmenu_bar_update_hook);
14804 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
14805
14806 staticpro (&Qoverriding_terminal_local_map);
14807 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
14808
14809 staticpro (&Qoverriding_local_map);
14810 Qoverriding_local_map = intern ("overriding-local-map");
14811
14812 staticpro (&Qwindow_scroll_functions);
14813 Qwindow_scroll_functions = intern ("window-scroll-functions");
14814
14815 staticpro (&Qredisplay_end_trigger_functions);
14816 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
14817
14818 staticpro (&Qinhibit_point_motion_hooks);
14819 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
14820
14821 QCdata = intern (":data");
14822 staticpro (&QCdata);
14823 Qdisplay = intern ("display");
14824 staticpro (&Qdisplay);
14825 Qspace_width = intern ("space-width");
14826 staticpro (&Qspace_width);
14827 Qraise = intern ("raise");
14828 staticpro (&Qraise);
14829 Qspace = intern ("space");
14830 staticpro (&Qspace);
14831 Qmargin = intern ("margin");
14832 staticpro (&Qmargin);
14833 Qleft_margin = intern ("left-margin");
14834 staticpro (&Qleft_margin);
14835 Qright_margin = intern ("right-margin");
14836 staticpro (&Qright_margin);
14837 Qalign_to = intern ("align-to");
14838 staticpro (&Qalign_to);
14839 QCalign_to = intern (":align-to");
14840 staticpro (&QCalign_to);
14841 Qrelative_width = intern ("relative-width");
14842 staticpro (&Qrelative_width);
14843 QCrelative_width = intern (":relative-width");
14844 staticpro (&QCrelative_width);
14845 QCrelative_height = intern (":relative-height");
14846 staticpro (&QCrelative_height);
14847 QCeval = intern (":eval");
14848 staticpro (&QCeval);
14849 QCpropertize = intern (":propertize");
14850 staticpro (&QCpropertize);
14851 Qwhen = intern ("when");
14852 staticpro (&Qwhen);
14853 QCfile = intern (":file");
14854 staticpro (&QCfile);
14855 Qfontified = intern ("fontified");
14856 staticpro (&Qfontified);
14857 Qfontification_functions = intern ("fontification-functions");
14858 staticpro (&Qfontification_functions);
14859 Qtrailing_whitespace = intern ("trailing-whitespace");
14860 staticpro (&Qtrailing_whitespace);
14861 Qimage = intern ("image");
14862 staticpro (&Qimage);
14863 Qmessage_truncate_lines = intern ("message-truncate-lines");
14864 staticpro (&Qmessage_truncate_lines);
14865 Qcursor_in_non_selected_windows = intern ("cursor-in-non-selected-windows");
14866 staticpro (&Qcursor_in_non_selected_windows);
14867 Qgrow_only = intern ("grow-only");
14868 staticpro (&Qgrow_only);
14869 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
14870 staticpro (&Qinhibit_menubar_update);
14871 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
14872 staticpro (&Qinhibit_eval_during_redisplay);
14873 Qposition = intern ("position");
14874 staticpro (&Qposition);
14875 Qbuffer_position = intern ("buffer-position");
14876 staticpro (&Qbuffer_position);
14877 Qobject = intern ("object");
14878 staticpro (&Qobject);
14879
14880 last_arrow_position = Qnil;
14881 last_arrow_string = Qnil;
14882 staticpro (&last_arrow_position);
14883 staticpro (&last_arrow_string);
14884
14885 echo_buffer[0] = echo_buffer[1] = Qnil;
14886 staticpro (&echo_buffer[0]);
14887 staticpro (&echo_buffer[1]);
14888
14889 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
14890 staticpro (&echo_area_buffer[0]);
14891 staticpro (&echo_area_buffer[1]);
14892
14893 Vmessages_buffer_name = build_string ("*Messages*");
14894 staticpro (&Vmessages_buffer_name);
14895
14896 mode_line_proptrans_alist = Qnil;
14897 staticpro (&mode_line_proptrans_alist);
14898
14899 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
14900 doc: /* Non-nil means highlight trailing whitespace.
14901 The face used for trailing whitespace is `trailing-whitespace'. */);
14902 Vshow_trailing_whitespace = Qnil;
14903
14904 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
14905 doc: /* Non-nil means don't actually do any redisplay.
14906 This is used for internal purposes. */);
14907 Vinhibit_redisplay = Qnil;
14908
14909 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
14910 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
14911 Vglobal_mode_string = Qnil;
14912
14913 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
14914 doc: /* Marker for where to display an arrow on top of the buffer text.
14915 This must be the beginning of a line in order to work.
14916 See also `overlay-arrow-string'. */);
14917 Voverlay_arrow_position = Qnil;
14918
14919 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
14920 doc: /* String to display as an arrow. See also `overlay-arrow-position'. */);
14921 Voverlay_arrow_string = Qnil;
14922
14923 DEFVAR_INT ("scroll-step", &scroll_step,
14924 doc: /* *The number of lines to try scrolling a window by when point moves out.
14925 If that fails to bring point back on frame, point is centered instead.
14926 If this is zero, point is always centered after it moves off frame.
14927 If you want scrolling to always be a line at a time, you should set
14928 `scroll-conservatively' to a large value rather than set this to 1. */);
14929
14930 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
14931 doc: /* *Scroll up to this many lines, to bring point back on screen.
14932 A value of zero means to scroll the text to center point vertically
14933 in the window. */);
14934 scroll_conservatively = 0;
14935
14936 DEFVAR_INT ("scroll-margin", &scroll_margin,
14937 doc: /* *Number of lines of margin at the top and bottom of a window.
14938 Recenter the window whenever point gets within this many lines
14939 of the top or bottom of the window. */);
14940 scroll_margin = 0;
14941
14942 #if GLYPH_DEBUG
14943 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
14944 #endif
14945
14946 DEFVAR_BOOL ("truncate-partial-width-windows",
14947 &truncate_partial_width_windows,
14948 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
14949 truncate_partial_width_windows = 1;
14950
14951 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
14952 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
14953 Any other value means to use the appropriate face, `mode-line',
14954 `header-line', or `menu' respectively.
14955
14956 This variable is deprecated; please change the above faces instead. */);
14957 mode_line_inverse_video = 1;
14958
14959 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
14960 doc: /* *Maximum buffer size for which line number should be displayed.
14961 If the buffer is bigger than this, the line number does not appear
14962 in the mode line. A value of nil means no limit. */);
14963 Vline_number_display_limit = Qnil;
14964
14965 DEFVAR_INT ("line-number-display-limit-width",
14966 &line_number_display_limit_width,
14967 doc: /* *Maximum line width (in characters) for line number display.
14968 If the average length of the lines near point is bigger than this, then the
14969 line number may be omitted from the mode line. */);
14970 line_number_display_limit_width = 200;
14971
14972 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
14973 doc: /* *Non-nil means highlight region even in nonselected windows. */);
14974 highlight_nonselected_windows = 0;
14975
14976 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
14977 doc: /* Non-nil if more than one frame is visible on this display.
14978 Minibuffer-only frames don't count, but iconified frames do.
14979 This variable is not guaranteed to be accurate except while processing
14980 `frame-title-format' and `icon-title-format'. */);
14981
14982 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
14983 doc: /* Template for displaying the title bar of visible frames.
14984 \(Assuming the window manager supports this feature.)
14985 This variable has the same structure as `mode-line-format' (which see),
14986 and is used only on frames for which no explicit name has been set
14987 \(see `modify-frame-parameters'). */);
14988 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
14989 doc: /* Template for displaying the title bar of an iconified frame.
14990 \(Assuming the window manager supports this feature.)
14991 This variable has the same structure as `mode-line-format' (which see),
14992 and is used only on frames for which no explicit name has been set
14993 \(see `modify-frame-parameters'). */);
14994 Vicon_title_format
14995 = Vframe_title_format
14996 = Fcons (intern ("multiple-frames"),
14997 Fcons (build_string ("%b"),
14998 Fcons (Fcons (empty_string,
14999 Fcons (intern ("invocation-name"),
15000 Fcons (build_string ("@"),
15001 Fcons (intern ("system-name"),
15002 Qnil)))),
15003 Qnil)));
15004
15005 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
15006 doc: /* Maximum number of lines to keep in the message log buffer.
15007 If nil, disable message logging. If t, log messages but don't truncate
15008 the buffer when it becomes large. */);
15009 Vmessage_log_max = make_number (50);
15010
15011 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
15012 doc: /* Functions called before redisplay, if window sizes have changed.
15013 The value should be a list of functions that take one argument.
15014 Just before redisplay, for each frame, if any of its windows have changed
15015 size since the last redisplay, or have been split or deleted,
15016 all the functions in the list are called, with the frame as argument. */);
15017 Vwindow_size_change_functions = Qnil;
15018
15019 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
15020 doc: /* List of Functions to call before redisplaying a window with scrolling.
15021 Each function is called with two arguments, the window
15022 and its new display-start position. Note that the value of `window-end'
15023 is not valid when these functions are called. */);
15024 Vwindow_scroll_functions = Qnil;
15025
15026 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
15027 doc: /* *Non-nil means automatically resize tool-bars.
15028 This increases a tool-bar's height if not all tool-bar items are visible.
15029 It decreases a tool-bar's height when it would display blank lines
15030 otherwise. */);
15031 auto_resize_tool_bars_p = 1;
15032
15033 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
15034 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
15035 auto_raise_tool_bar_buttons_p = 1;
15036
15037 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
15038 doc: /* *Margin around tool-bar buttons in pixels.
15039 If an integer, use that for both horizontal and vertical margins.
15040 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
15041 HORZ specifying the horizontal margin, and VERT specifying the
15042 vertical margin. */);
15043 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
15044
15045 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
15046 doc: /* *Relief thickness of tool-bar buttons. */);
15047 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
15048
15049 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
15050 doc: /* List of functions to call to fontify regions of text.
15051 Each function is called with one argument POS. Functions must
15052 fontify a region starting at POS in the current buffer, and give
15053 fontified regions the property `fontified'. */);
15054 Vfontification_functions = Qnil;
15055 Fmake_variable_buffer_local (Qfontification_functions);
15056
15057 DEFVAR_BOOL ("unibyte-display-via-language-environment",
15058 &unibyte_display_via_language_environment,
15059 doc: /* *Non-nil means display unibyte text according to language environment.
15060 Specifically this means that unibyte non-ASCII characters
15061 are displayed by converting them to the equivalent multibyte characters
15062 according to the current language environment. As a result, they are
15063 displayed according to the current fontset. */);
15064 unibyte_display_via_language_environment = 0;
15065
15066 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
15067 doc: /* *Maximum height for resizing mini-windows.
15068 If a float, it specifies a fraction of the mini-window frame's height.
15069 If an integer, it specifies a number of lines. */);
15070 Vmax_mini_window_height = make_float (0.25);
15071
15072 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
15073 doc: /* *How to resize mini-windows.
15074 A value of nil means don't automatically resize mini-windows.
15075 A value of t means resize them to fit the text displayed in them.
15076 A value of `grow-only', the default, means let mini-windows grow
15077 only, until their display becomes empty, at which point the windows
15078 go back to their normal size. */);
15079 Vresize_mini_windows = Qgrow_only;
15080
15081 DEFVAR_BOOL ("cursor-in-non-selected-windows",
15082 &cursor_in_non_selected_windows,
15083 doc: /* *Non-nil means display a hollow cursor in non-selected windows.
15084 nil means don't display a cursor there. */);
15085 cursor_in_non_selected_windows = 1;
15086
15087 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
15088 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
15089 automatic_hscrolling_p = 1;
15090
15091 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
15092 doc: /* *How many columns away from the window edge point is allowed to get
15093 before automatic hscrolling will horizontally scroll the window. */);
15094 hscroll_margin = 5;
15095
15096 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
15097 doc: /* *How many columns to scroll the window when point gets too close to the edge.
15098 When point is less than `automatic-hscroll-margin' columns from the window
15099 edge, automatic hscrolling will scroll the window by the amount of columns
15100 determined by this variable. If its value is a positive integer, scroll that
15101 many columns. If it's a positive floating-point number, it specifies the
15102 fraction of the window's width to scroll. If it's nil or zero, point will be
15103 centered horizontally after the scroll. Any other value, including negative
15104 numbers, are treated as if the value were zero.
15105
15106 Automatic hscrolling always moves point outside the scroll margin, so if
15107 point was more than scroll step columns inside the margin, the window will
15108 scroll more than the value given by the scroll step.
15109
15110 Note that the lower bound for automatic hscrolling specified by `scroll-left'
15111 and `scroll-right' overrides this variable's effect. */);
15112 Vhscroll_step = make_number (0);
15113
15114 DEFVAR_LISP ("image-types", &Vimage_types,
15115 doc: /* List of supported image types.
15116 Each element of the list is a symbol for a supported image type. */);
15117 Vimage_types = Qnil;
15118
15119 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
15120 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
15121 Bind this around calls to `message' to let it take effect. */);
15122 message_truncate_lines = 0;
15123
15124 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
15125 doc: /* Normal hook run for clicks on menu bar, before displaying a submenu.
15126 Can be used to update submenus whose contents should vary. */);
15127 Vmenu_bar_update_hook = Qnil;
15128
15129 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
15130 doc: /* Non-nil means don't update menu bars. Internal use only. */);
15131 inhibit_menubar_update = 0;
15132
15133 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
15134 doc: /* Non-nil means don't eval Lisp during redisplay. */);
15135 inhibit_eval_during_redisplay = 0;
15136
15137 #if GLYPH_DEBUG
15138 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
15139 doc: /* Inhibit try_window_id display optimization. */);
15140 inhibit_try_window_id = 0;
15141
15142 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
15143 doc: /* Inhibit try_window_reusing display optimization. */);
15144 inhibit_try_window_reusing = 0;
15145
15146 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
15147 doc: /* Inhibit try_cursor_movement display optimization. */);
15148 inhibit_try_cursor_movement = 0;
15149 #endif /* GLYPH_DEBUG */
15150 }
15151
15152
15153 /* Initialize this module when Emacs starts. */
15154
15155 void
15156 init_xdisp ()
15157 {
15158 Lisp_Object root_window;
15159 struct window *mini_w;
15160
15161 current_header_line_height = current_mode_line_height = -1;
15162
15163 CHARPOS (this_line_start_pos) = 0;
15164
15165 mini_w = XWINDOW (minibuf_window);
15166 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
15167
15168 if (!noninteractive)
15169 {
15170 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
15171 int i;
15172
15173 XWINDOW (root_window)->top = make_number (FRAME_TOP_MARGIN (f));
15174 set_window_height (root_window,
15175 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
15176 0);
15177 mini_w->top = make_number (FRAME_HEIGHT (f) - 1);
15178 set_window_height (minibuf_window, 1, 0);
15179
15180 XWINDOW (root_window)->width = make_number (FRAME_WIDTH (f));
15181 mini_w->width = make_number (FRAME_WIDTH (f));
15182
15183 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
15184 scratch_glyph_row.glyphs[TEXT_AREA + 1]
15185 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
15186
15187 /* The default ellipsis glyphs `...'. */
15188 for (i = 0; i < 3; ++i)
15189 default_invis_vector[i] = make_number ('.');
15190 }
15191
15192 #ifdef HAVE_WINDOW_SYSTEM
15193 {
15194 /* Allocate the buffer for frame titles. */
15195 int size = 100;
15196 frame_title_buf = (char *) xmalloc (size);
15197 frame_title_buf_end = frame_title_buf + size;
15198 frame_title_ptr = NULL;
15199 }
15200 #endif /* HAVE_WINDOW_SYSTEM */
15201
15202 help_echo_showing_p = 0;
15203 }
15204
15205