]> code.delx.au - gnu-emacs/blob - src/xdisp.c
comment last change
[gnu-emacs] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 86, 87, 88, 93, 94, 95, 97, 98, 99, 2000, 2001, 2002
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 MAC_OS
198 #include "macterm.h"
199 #endif
200
201 #define INFINITY 10000000
202
203 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS)
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 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
215
216 extern Lisp_Object Voverriding_local_map;
217 extern Lisp_Object Voverriding_local_map_menu_flag;
218 extern Lisp_Object Qmenu_item;
219
220 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
221 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
222 Lisp_Object Qredisplay_end_trigger_functions;
223 Lisp_Object Qinhibit_point_motion_hooks;
224 Lisp_Object QCeval, Qwhen, QCfile, QCdata, QCpropertize;
225 Lisp_Object Qfontified;
226 Lisp_Object Qgrow_only;
227 Lisp_Object Qinhibit_eval_during_redisplay;
228 Lisp_Object Qbuffer_position, Qposition, Qobject;
229
230 Lisp_Object Qrisky_local_variable;
231
232 /* Holds the list (error). */
233 Lisp_Object list_of_error;
234
235 /* Functions called to fontify regions of text. */
236
237 Lisp_Object Vfontification_functions;
238 Lisp_Object Qfontification_functions;
239
240 /* Non-zero means draw tool bar buttons raised when the mouse moves
241 over them. */
242
243 int auto_raise_tool_bar_buttons_p;
244
245 /* Margin around tool bar buttons in pixels. */
246
247 Lisp_Object Vtool_bar_button_margin;
248
249 /* Thickness of shadow to draw around tool bar buttons. */
250
251 EMACS_INT tool_bar_button_relief;
252
253 /* Non-zero means automatically resize tool-bars so that all tool-bar
254 items are visible, and no blank lines remain. */
255
256 int auto_resize_tool_bars_p;
257
258 /* Non-nil means don't actually do any redisplay. */
259
260 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
261
262 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
263
264 int inhibit_eval_during_redisplay;
265
266 /* Names of text properties relevant for redisplay. */
267
268 Lisp_Object Qdisplay, Qrelative_width, Qalign_to;
269 extern Lisp_Object Qface, Qinvisible, Qwidth;
270
271 /* Symbols used in text property values. */
272
273 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
274 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
275 Lisp_Object Qmargin;
276 extern Lisp_Object Qheight;
277
278 /* Non-nil means highlight trailing whitespace. */
279
280 Lisp_Object Vshow_trailing_whitespace;
281
282 /* Name of the face used to highlight trailing whitespace. */
283
284 Lisp_Object Qtrailing_whitespace;
285
286 /* The symbol `image' which is the car of the lists used to represent
287 images in Lisp. */
288
289 Lisp_Object Qimage;
290
291 /* Non-zero means print newline to stdout before next mini-buffer
292 message. */
293
294 int noninteractive_need_newline;
295
296 /* Non-zero means print newline to message log before next message. */
297
298 static int message_log_need_newline;
299
300 /* Three markers that message_dolog uses.
301 It could allocate them itself, but that causes trouble
302 in handling memory-full errors. */
303 static Lisp_Object message_dolog_marker1;
304 static Lisp_Object message_dolog_marker2;
305 static Lisp_Object message_dolog_marker3;
306 \f
307 /* The buffer position of the first character appearing entirely or
308 partially on the line of the selected window which contains the
309 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
310 redisplay optimization in redisplay_internal. */
311
312 static struct text_pos this_line_start_pos;
313
314 /* Number of characters past the end of the line above, including the
315 terminating newline. */
316
317 static struct text_pos this_line_end_pos;
318
319 /* The vertical positions and the height of this line. */
320
321 static int this_line_vpos;
322 static int this_line_y;
323 static int this_line_pixel_height;
324
325 /* X position at which this display line starts. Usually zero;
326 negative if first character is partially visible. */
327
328 static int this_line_start_x;
329
330 /* Buffer that this_line_.* variables are referring to. */
331
332 static struct buffer *this_line_buffer;
333
334 /* Nonzero means truncate lines in all windows less wide than the
335 frame. */
336
337 int truncate_partial_width_windows;
338
339 /* A flag to control how to display unibyte 8-bit character. */
340
341 int unibyte_display_via_language_environment;
342
343 /* Nonzero means we have more than one non-mini-buffer-only frame.
344 Not guaranteed to be accurate except while parsing
345 frame-title-format. */
346
347 int multiple_frames;
348
349 Lisp_Object Vglobal_mode_string;
350
351 /* Marker for where to display an arrow on top of the buffer text. */
352
353 Lisp_Object Voverlay_arrow_position;
354
355 /* String to display for the arrow. Only used on terminal frames. */
356
357 Lisp_Object Voverlay_arrow_string;
358
359 /* Values of those variables at last redisplay. However, if
360 Voverlay_arrow_position is a marker, last_arrow_position is its
361 numerical position. */
362
363 static Lisp_Object last_arrow_position, last_arrow_string;
364
365 /* Like mode-line-format, but for the title bar on a visible frame. */
366
367 Lisp_Object Vframe_title_format;
368
369 /* Like mode-line-format, but for the title bar on an iconified frame. */
370
371 Lisp_Object Vicon_title_format;
372
373 /* List of functions to call when a window's size changes. These
374 functions get one arg, a frame on which one or more windows' sizes
375 have changed. */
376
377 static Lisp_Object Vwindow_size_change_functions;
378
379 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
380
381 /* Nonzero if overlay arrow has been displayed once in this window. */
382
383 static int overlay_arrow_seen;
384
385 /* Nonzero means highlight the region even in nonselected windows. */
386
387 int highlight_nonselected_windows;
388
389 /* If cursor motion alone moves point off frame, try scrolling this
390 many lines up or down if that will bring it back. */
391
392 static EMACS_INT scroll_step;
393
394 /* Nonzero means scroll just far enough to bring point back on the
395 screen, when appropriate. */
396
397 static EMACS_INT scroll_conservatively;
398
399 /* Recenter the window whenever point gets within this many lines of
400 the top or bottom of the window. This value is translated into a
401 pixel value by multiplying it with CANON_Y_UNIT, which means that
402 there is really a fixed pixel height scroll margin. */
403
404 EMACS_INT scroll_margin;
405
406 /* Number of windows showing the buffer of the selected window (or
407 another buffer with the same base buffer). keyboard.c refers to
408 this. */
409
410 int buffer_shared;
411
412 /* Vector containing glyphs for an ellipsis `...'. */
413
414 static Lisp_Object default_invis_vector[3];
415
416 /* Zero means display the mode-line/header-line/menu-bar in the default face
417 (this slightly odd definition is for compatibility with previous versions
418 of emacs), non-zero means display them using their respective faces.
419
420 This variable is deprecated. */
421
422 int mode_line_inverse_video;
423
424 /* Prompt to display in front of the mini-buffer contents. */
425
426 Lisp_Object minibuf_prompt;
427
428 /* Width of current mini-buffer prompt. Only set after display_line
429 of the line that contains the prompt. */
430
431 int minibuf_prompt_width;
432
433 /* This is the window where the echo area message was displayed. It
434 is always a mini-buffer window, but it may not be the same window
435 currently active as a mini-buffer. */
436
437 Lisp_Object echo_area_window;
438
439 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
440 pushes the current message and the value of
441 message_enable_multibyte on the stack, the function restore_message
442 pops the stack and displays MESSAGE again. */
443
444 Lisp_Object Vmessage_stack;
445
446 /* Nonzero means multibyte characters were enabled when the echo area
447 message was specified. */
448
449 int message_enable_multibyte;
450
451 /* Nonzero if we should redraw the mode lines on the next redisplay. */
452
453 int update_mode_lines;
454
455 /* Nonzero if window sizes or contents have changed since last
456 redisplay that finished. */
457
458 int windows_or_buffers_changed;
459
460 /* Nonzero means a frame's cursor type has been changed. */
461
462 int cursor_type_changed;
463
464 /* Nonzero after display_mode_line if %l was used and it displayed a
465 line number. */
466
467 int line_number_displayed;
468
469 /* Maximum buffer size for which to display line numbers. */
470
471 Lisp_Object Vline_number_display_limit;
472
473 /* Line width to consider when repositioning for line number display. */
474
475 static EMACS_INT line_number_display_limit_width;
476
477 /* Number of lines to keep in the message log buffer. t means
478 infinite. nil means don't log at all. */
479
480 Lisp_Object Vmessage_log_max;
481
482 /* The name of the *Messages* buffer, a string. */
483
484 static Lisp_Object Vmessages_buffer_name;
485
486 /* Current, index 0, and last displayed echo area message. Either
487 buffers from echo_buffers, or nil to indicate no message. */
488
489 Lisp_Object echo_area_buffer[2];
490
491 /* The buffers referenced from echo_area_buffer. */
492
493 static Lisp_Object echo_buffer[2];
494
495 /* A vector saved used in with_area_buffer to reduce consing. */
496
497 static Lisp_Object Vwith_echo_area_save_vector;
498
499 /* Non-zero means display_echo_area should display the last echo area
500 message again. Set by redisplay_preserve_echo_area. */
501
502 static int display_last_displayed_message_p;
503
504 /* Nonzero if echo area is being used by print; zero if being used by
505 message. */
506
507 int message_buf_print;
508
509 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
510
511 Lisp_Object Qinhibit_menubar_update;
512 int inhibit_menubar_update;
513
514 /* Maximum height for resizing mini-windows. Either a float
515 specifying a fraction of the available height, or an integer
516 specifying a number of lines. */
517
518 Lisp_Object Vmax_mini_window_height;
519
520 /* Non-zero means messages should be displayed with truncated
521 lines instead of being continued. */
522
523 int message_truncate_lines;
524 Lisp_Object Qmessage_truncate_lines;
525
526 /* Set to 1 in clear_message to make redisplay_internal aware
527 of an emptied echo area. */
528
529 static int message_cleared_p;
530
531 /* Non-zero means we want a hollow cursor in windows that are not
532 selected. Zero means there's no cursor in such windows. */
533
534 int cursor_in_non_selected_windows;
535 Lisp_Object Qcursor_in_non_selected_windows;
536
537 /* A scratch glyph row with contents used for generating truncation
538 glyphs. Also used in direct_output_for_insert. */
539
540 #define MAX_SCRATCH_GLYPHS 100
541 struct glyph_row scratch_glyph_row;
542 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
543
544 /* Ascent and height of the last line processed by move_it_to. */
545
546 static int last_max_ascent, last_height;
547
548 /* Non-zero if there's a help-echo in the echo area. */
549
550 int help_echo_showing_p;
551
552 /* If >= 0, computed, exact values of mode-line and header-line height
553 to use in the macros CURRENT_MODE_LINE_HEIGHT and
554 CURRENT_HEADER_LINE_HEIGHT. */
555
556 int current_mode_line_height, current_header_line_height;
557
558 /* The maximum distance to look ahead for text properties. Values
559 that are too small let us call compute_char_face and similar
560 functions too often which is expensive. Values that are too large
561 let us call compute_char_face and alike too often because we
562 might not be interested in text properties that far away. */
563
564 #define TEXT_PROP_DISTANCE_LIMIT 100
565
566 #if GLYPH_DEBUG
567
568 /* Variables to turn off display optimizations from Lisp. */
569
570 int inhibit_try_window_id, inhibit_try_window_reusing;
571 int inhibit_try_cursor_movement;
572
573 /* Non-zero means print traces of redisplay if compiled with
574 GLYPH_DEBUG != 0. */
575
576 int trace_redisplay_p;
577
578 #endif /* GLYPH_DEBUG */
579
580 #ifdef DEBUG_TRACE_MOVE
581 /* Non-zero means trace with TRACE_MOVE to stderr. */
582 int trace_move;
583
584 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
585 #else
586 #define TRACE_MOVE(x) (void) 0
587 #endif
588
589 /* Non-zero means automatically scroll windows horizontally to make
590 point visible. */
591
592 int automatic_hscrolling_p;
593
594 /* How close to the margin can point get before the window is scrolled
595 horizontally. */
596 EMACS_INT hscroll_margin;
597
598 /* How much to scroll horizontally when point is inside the above margin. */
599 Lisp_Object Vhscroll_step;
600
601 /* A list of symbols, one for each supported image type. */
602
603 Lisp_Object Vimage_types;
604
605 /* The variable `resize-mini-windows'. If nil, don't resize
606 mini-windows. If t, always resize them to fit the text they
607 display. If `grow-only', let mini-windows grow only until they
608 become empty. */
609
610 Lisp_Object Vresize_mini_windows;
611
612 /* Buffer being redisplayed -- for redisplay_window_error. */
613
614 struct buffer *displayed_buffer;
615
616 /* Value returned from text property handlers (see below). */
617
618 enum prop_handled
619 {
620 HANDLED_NORMALLY,
621 HANDLED_RECOMPUTE_PROPS,
622 HANDLED_OVERLAY_STRING_CONSUMED,
623 HANDLED_RETURN
624 };
625
626 /* A description of text properties that redisplay is interested
627 in. */
628
629 struct props
630 {
631 /* The name of the property. */
632 Lisp_Object *name;
633
634 /* A unique index for the property. */
635 enum prop_idx idx;
636
637 /* A handler function called to set up iterator IT from the property
638 at IT's current position. Value is used to steer handle_stop. */
639 enum prop_handled (*handler) P_ ((struct it *it));
640 };
641
642 static enum prop_handled handle_face_prop P_ ((struct it *));
643 static enum prop_handled handle_invisible_prop P_ ((struct it *));
644 static enum prop_handled handle_display_prop P_ ((struct it *));
645 static enum prop_handled handle_composition_prop P_ ((struct it *));
646 static enum prop_handled handle_overlay_change P_ ((struct it *));
647 static enum prop_handled handle_fontified_prop P_ ((struct it *));
648
649 /* Properties handled by iterators. */
650
651 static struct props it_props[] =
652 {
653 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
654 /* Handle `face' before `display' because some sub-properties of
655 `display' need to know the face. */
656 {&Qface, FACE_PROP_IDX, handle_face_prop},
657 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
658 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
659 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
660 {NULL, 0, NULL}
661 };
662
663 /* Value is the position described by X. If X is a marker, value is
664 the marker_position of X. Otherwise, value is X. */
665
666 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
667
668 /* Enumeration returned by some move_it_.* functions internally. */
669
670 enum move_it_result
671 {
672 /* Not used. Undefined value. */
673 MOVE_UNDEFINED,
674
675 /* Move ended at the requested buffer position or ZV. */
676 MOVE_POS_MATCH_OR_ZV,
677
678 /* Move ended at the requested X pixel position. */
679 MOVE_X_REACHED,
680
681 /* Move within a line ended at the end of a line that must be
682 continued. */
683 MOVE_LINE_CONTINUED,
684
685 /* Move within a line ended at the end of a line that would
686 be displayed truncated. */
687 MOVE_LINE_TRUNCATED,
688
689 /* Move within a line ended at a line end. */
690 MOVE_NEWLINE_OR_CR
691 };
692
693
694 \f
695 /* Function prototypes. */
696
697 static void setup_for_ellipsis P_ ((struct it *));
698 static void mark_window_display_accurate_1 P_ ((struct window *, int));
699 static int single_display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
700 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
701 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
702 static int redisplay_mode_lines P_ ((Lisp_Object, int));
703 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
704
705 #if 0
706 static int invisible_text_between_p P_ ((struct it *, int, int));
707 #endif
708
709 static int next_element_from_ellipsis P_ ((struct it *));
710 static void pint2str P_ ((char *, int, int));
711 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
712 struct text_pos));
713 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
714 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
715 static void store_frame_title_char P_ ((char));
716 static int store_frame_title P_ ((const unsigned char *, int, int));
717 static void x_consider_frame_title P_ ((Lisp_Object));
718 static void handle_stop P_ ((struct it *));
719 static int tool_bar_lines_needed P_ ((struct frame *));
720 static int single_display_prop_intangible_p P_ ((Lisp_Object));
721 static void ensure_echo_area_buffers P_ ((void));
722 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
723 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
724 static int with_echo_area_buffer P_ ((struct window *, int,
725 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
726 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
727 static void clear_garbaged_frames P_ ((void));
728 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
729 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
730 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
731 static int display_echo_area P_ ((struct window *));
732 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
733 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
734 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
735 static int string_char_and_length P_ ((const unsigned char *, int, int *));
736 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
737 struct text_pos));
738 static int compute_window_start_on_continuation_line P_ ((struct window *));
739 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
740 static void insert_left_trunc_glyphs P_ ((struct it *));
741 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
742 static void extend_face_to_end_of_line P_ ((struct it *));
743 static int append_space P_ ((struct it *, int));
744 static int make_cursor_line_fully_visible P_ ((struct window *));
745 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int));
746 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
747 static int trailing_whitespace_p P_ ((int));
748 static int message_log_check_duplicate P_ ((int, int, int, int));
749 static void push_it P_ ((struct it *));
750 static void pop_it P_ ((struct it *));
751 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
752 static void redisplay_internal P_ ((int));
753 static int echo_area_display P_ ((int));
754 static void redisplay_windows P_ ((Lisp_Object));
755 static void redisplay_window P_ ((Lisp_Object, int));
756 static Lisp_Object redisplay_window_error ();
757 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
758 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
759 static void update_menu_bar P_ ((struct frame *, int));
760 static int try_window_reusing_current_matrix P_ ((struct window *));
761 static int try_window_id P_ ((struct window *));
762 static int display_line P_ ((struct it *));
763 static int display_mode_lines P_ ((struct window *));
764 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
765 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
766 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
767 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
768 static void display_menu_bar P_ ((struct window *));
769 static int display_count_lines P_ ((int, int, int, int, int *));
770 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
771 int, int, struct it *, int, int, int, int));
772 static void compute_line_metrics P_ ((struct it *));
773 static void run_redisplay_end_trigger_hook P_ ((struct it *));
774 static int get_overlay_strings P_ ((struct it *, int));
775 static void next_overlay_string P_ ((struct it *));
776 static void reseat P_ ((struct it *, struct text_pos, int));
777 static void reseat_1 P_ ((struct it *, struct text_pos, int));
778 static void back_to_previous_visible_line_start P_ ((struct it *));
779 static void reseat_at_previous_visible_line_start P_ ((struct it *));
780 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
781 static int next_element_from_display_vector P_ ((struct it *));
782 static int next_element_from_string P_ ((struct it *));
783 static int next_element_from_c_string P_ ((struct it *));
784 static int next_element_from_buffer P_ ((struct it *));
785 static int next_element_from_composition P_ ((struct it *));
786 static int next_element_from_image P_ ((struct it *));
787 static int next_element_from_stretch P_ ((struct it *));
788 static void load_overlay_strings P_ ((struct it *, int));
789 static int init_from_display_pos P_ ((struct it *, struct window *,
790 struct display_pos *));
791 static void reseat_to_string P_ ((struct it *, unsigned char *,
792 Lisp_Object, int, int, int, int));
793 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
794 int, int, int));
795 void move_it_vertically_backward P_ ((struct it *, int));
796 static void init_to_row_start P_ ((struct it *, struct window *,
797 struct glyph_row *));
798 static int init_to_row_end P_ ((struct it *, struct window *,
799 struct glyph_row *));
800 static void back_to_previous_line_start P_ ((struct it *));
801 static int forward_to_next_line_start P_ ((struct it *, int *));
802 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
803 Lisp_Object, int));
804 static struct text_pos string_pos P_ ((int, Lisp_Object));
805 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
806 static int number_of_chars P_ ((unsigned char *, int));
807 static void compute_stop_pos P_ ((struct it *));
808 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
809 Lisp_Object));
810 static int face_before_or_after_it_pos P_ ((struct it *, int));
811 static int next_overlay_change P_ ((int));
812 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
813 Lisp_Object, struct text_pos *,
814 int));
815 static int underlying_face_id P_ ((struct it *));
816 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
817 struct window *));
818
819 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
820 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
821
822 #ifdef HAVE_WINDOW_SYSTEM
823
824 static void update_tool_bar P_ ((struct frame *, int));
825 static void build_desired_tool_bar_string P_ ((struct frame *f));
826 static int redisplay_tool_bar P_ ((struct frame *));
827 static void display_tool_bar_line P_ ((struct it *));
828
829 #endif /* HAVE_WINDOW_SYSTEM */
830
831 \f
832 /***********************************************************************
833 Window display dimensions
834 ***********************************************************************/
835
836 /* Return the window-relative maximum y + 1 for glyph rows displaying
837 text in window W. This is the height of W minus the height of a
838 mode line, if any. */
839
840 INLINE int
841 window_text_bottom_y (w)
842 struct window *w;
843 {
844 struct frame *f = XFRAME (w->frame);
845 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
846
847 if (WINDOW_WANTS_MODELINE_P (w))
848 height -= CURRENT_MODE_LINE_HEIGHT (w);
849 return height;
850 }
851
852
853 /* Return the pixel width of display area AREA of window W. AREA < 0
854 means return the total width of W, not including fringes to
855 the left and right of the window. */
856
857 INLINE int
858 window_box_width (w, area)
859 struct window *w;
860 int area;
861 {
862 struct frame *f = XFRAME (w->frame);
863 int width = XFASTINT (w->width);
864
865 if (!w->pseudo_window_p)
866 {
867 width -= FRAME_SCROLL_BAR_WIDTH (f) + FRAME_FRINGE_COLS (f);
868
869 if (area == TEXT_AREA)
870 {
871 if (INTEGERP (w->left_margin_width))
872 width -= XFASTINT (w->left_margin_width);
873 if (INTEGERP (w->right_margin_width))
874 width -= XFASTINT (w->right_margin_width);
875 }
876 else if (area == LEFT_MARGIN_AREA)
877 width = (INTEGERP (w->left_margin_width)
878 ? XFASTINT (w->left_margin_width) : 0);
879 else if (area == RIGHT_MARGIN_AREA)
880 width = (INTEGERP (w->right_margin_width)
881 ? XFASTINT (w->right_margin_width) : 0);
882 }
883
884 return width * CANON_X_UNIT (f);
885 }
886
887
888 /* Return the pixel height of the display area of window W, not
889 including mode lines of W, if any. */
890
891 INLINE int
892 window_box_height (w)
893 struct window *w;
894 {
895 struct frame *f = XFRAME (w->frame);
896 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
897
898 xassert (height >= 0);
899
900 /* Note: the code below that determines the mode-line/header-line
901 height is essentially the same as that contained in the macro
902 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
903 the appropriate glyph row has its `mode_line_p' flag set,
904 and if it doesn't, uses estimate_mode_line_height instead. */
905
906 if (WINDOW_WANTS_MODELINE_P (w))
907 {
908 struct glyph_row *ml_row
909 = (w->current_matrix && w->current_matrix->rows
910 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
911 : 0);
912 if (ml_row && ml_row->mode_line_p)
913 height -= ml_row->height;
914 else
915 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
916 }
917
918 if (WINDOW_WANTS_HEADER_LINE_P (w))
919 {
920 struct glyph_row *hl_row
921 = (w->current_matrix && w->current_matrix->rows
922 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
923 : 0);
924 if (hl_row && hl_row->mode_line_p)
925 height -= hl_row->height;
926 else
927 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
928 }
929
930 /* With a very small font and a mode-line that's taller than
931 default, we might end up with a negative height. */
932 return max (0, height);
933 }
934
935
936 /* Return the frame-relative coordinate of the left edge of display
937 area AREA of window W. AREA < 0 means return the left edge of the
938 whole window, to the right of the left fringe of W. */
939
940 INLINE int
941 window_box_left (w, area)
942 struct window *w;
943 int area;
944 {
945 struct frame *f = XFRAME (w->frame);
946 int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
947
948 if (!w->pseudo_window_p)
949 {
950 x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
951 + FRAME_LEFT_FRINGE_WIDTH (f));
952
953 if (area == TEXT_AREA)
954 x += window_box_width (w, LEFT_MARGIN_AREA);
955 else if (area == RIGHT_MARGIN_AREA)
956 x += (window_box_width (w, LEFT_MARGIN_AREA)
957 + window_box_width (w, TEXT_AREA));
958 }
959
960 return x;
961 }
962
963
964 /* Return the frame-relative coordinate of the right edge of display
965 area AREA of window W. AREA < 0 means return the left edge of the
966 whole window, to the left of the right fringe of W. */
967
968 INLINE int
969 window_box_right (w, area)
970 struct window *w;
971 int area;
972 {
973 return window_box_left (w, area) + window_box_width (w, area);
974 }
975
976
977 /* Get the bounding box of the display area AREA of window W, without
978 mode lines, in frame-relative coordinates. AREA < 0 means the
979 whole window, not including the left and right fringes of
980 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
981 coordinates of the upper-left corner of the box. Return in
982 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
983
984 INLINE void
985 window_box (w, area, box_x, box_y, box_width, box_height)
986 struct window *w;
987 int area;
988 int *box_x, *box_y, *box_width, *box_height;
989 {
990 struct frame *f = XFRAME (w->frame);
991
992 *box_width = window_box_width (w, area);
993 *box_height = window_box_height (w);
994 *box_x = window_box_left (w, area);
995 *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
996 + XFASTINT (w->top) * CANON_Y_UNIT (f));
997 if (WINDOW_WANTS_HEADER_LINE_P (w))
998 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
999 }
1000
1001
1002 /* Get the bounding box of the display area AREA of window W, without
1003 mode lines. AREA < 0 means the whole window, not including the
1004 left and right fringe of the window. Return in *TOP_LEFT_X
1005 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1006 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1007 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1008 box. */
1009
1010 INLINE void
1011 window_box_edges (w, area, top_left_x, top_left_y,
1012 bottom_right_x, bottom_right_y)
1013 struct window *w;
1014 int area;
1015 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1016 {
1017 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1018 bottom_right_y);
1019 *bottom_right_x += *top_left_x;
1020 *bottom_right_y += *top_left_y;
1021 }
1022
1023
1024 \f
1025 /***********************************************************************
1026 Utilities
1027 ***********************************************************************/
1028
1029 /* Return the bottom y-position of the line the iterator IT is in.
1030 This can modify IT's settings. */
1031
1032 int
1033 line_bottom_y (it)
1034 struct it *it;
1035 {
1036 int line_height = it->max_ascent + it->max_descent;
1037 int line_top_y = it->current_y;
1038
1039 if (line_height == 0)
1040 {
1041 if (last_height)
1042 line_height = last_height;
1043 else if (IT_CHARPOS (*it) < ZV)
1044 {
1045 move_it_by_lines (it, 1, 1);
1046 line_height = (it->max_ascent || it->max_descent
1047 ? it->max_ascent + it->max_descent
1048 : last_height);
1049 }
1050 else
1051 {
1052 struct glyph_row *row = it->glyph_row;
1053
1054 /* Use the default character height. */
1055 it->glyph_row = NULL;
1056 it->what = IT_CHARACTER;
1057 it->c = ' ';
1058 it->len = 1;
1059 PRODUCE_GLYPHS (it);
1060 line_height = it->ascent + it->descent;
1061 it->glyph_row = row;
1062 }
1063 }
1064
1065 return line_top_y + line_height;
1066 }
1067
1068
1069 /* Return 1 if position CHARPOS is visible in window W. Set *FULLY to
1070 1 if POS is visible and the line containing POS is fully visible.
1071 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1072 and header-lines heights. */
1073
1074 int
1075 pos_visible_p (w, charpos, fully, exact_mode_line_heights_p)
1076 struct window *w;
1077 int charpos, *fully, exact_mode_line_heights_p;
1078 {
1079 struct it it;
1080 struct text_pos top;
1081 int visible_p;
1082 struct buffer *old_buffer = NULL;
1083
1084 if (XBUFFER (w->buffer) != current_buffer)
1085 {
1086 old_buffer = current_buffer;
1087 set_buffer_internal_1 (XBUFFER (w->buffer));
1088 }
1089
1090 *fully = visible_p = 0;
1091 SET_TEXT_POS_FROM_MARKER (top, w->start);
1092
1093 /* Compute exact mode line heights, if requested. */
1094 if (exact_mode_line_heights_p)
1095 {
1096 if (WINDOW_WANTS_MODELINE_P (w))
1097 current_mode_line_height
1098 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1099 current_buffer->mode_line_format);
1100
1101 if (WINDOW_WANTS_HEADER_LINE_P (w))
1102 current_header_line_height
1103 = display_mode_line (w, HEADER_LINE_FACE_ID,
1104 current_buffer->header_line_format);
1105 }
1106
1107 start_display (&it, w, top);
1108 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
1109 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
1110
1111 /* Note that we may overshoot because of invisible text. */
1112 if (IT_CHARPOS (it) >= charpos)
1113 {
1114 int top_y = it.current_y;
1115 int bottom_y = line_bottom_y (&it);
1116 int window_top_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
1117
1118 if (top_y < window_top_y)
1119 visible_p = bottom_y > window_top_y;
1120 else if (top_y < it.last_visible_y)
1121 {
1122 visible_p = 1;
1123 *fully = bottom_y <= it.last_visible_y;
1124 }
1125 }
1126 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1127 {
1128 move_it_by_lines (&it, 1, 0);
1129 if (charpos < IT_CHARPOS (it))
1130 {
1131 visible_p = 1;
1132 *fully = 0;
1133 }
1134 }
1135
1136 if (old_buffer)
1137 set_buffer_internal_1 (old_buffer);
1138
1139 current_header_line_height = current_mode_line_height = -1;
1140 return visible_p;
1141 }
1142
1143
1144 /* Return the next character from STR which is MAXLEN bytes long.
1145 Return in *LEN the length of the character. This is like
1146 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1147 we find one, we return a `?', but with the length of the invalid
1148 character. */
1149
1150 static INLINE int
1151 string_char_and_length (str, maxlen, len)
1152 const unsigned char *str;
1153 int maxlen, *len;
1154 {
1155 int c;
1156
1157 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1158 if (!CHAR_VALID_P (c, 1))
1159 /* We may not change the length here because other places in Emacs
1160 don't use this function, i.e. they silently accept invalid
1161 characters. */
1162 c = '?';
1163
1164 return c;
1165 }
1166
1167
1168
1169 /* Given a position POS containing a valid character and byte position
1170 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1171
1172 static struct text_pos
1173 string_pos_nchars_ahead (pos, string, nchars)
1174 struct text_pos pos;
1175 Lisp_Object string;
1176 int nchars;
1177 {
1178 xassert (STRINGP (string) && nchars >= 0);
1179
1180 if (STRING_MULTIBYTE (string))
1181 {
1182 int rest = SBYTES (string) - BYTEPOS (pos);
1183 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1184 int len;
1185
1186 while (nchars--)
1187 {
1188 string_char_and_length (p, rest, &len);
1189 p += len, rest -= len;
1190 xassert (rest >= 0);
1191 CHARPOS (pos) += 1;
1192 BYTEPOS (pos) += len;
1193 }
1194 }
1195 else
1196 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1197
1198 return pos;
1199 }
1200
1201
1202 /* Value is the text position, i.e. character and byte position,
1203 for character position CHARPOS in STRING. */
1204
1205 static INLINE struct text_pos
1206 string_pos (charpos, string)
1207 int charpos;
1208 Lisp_Object string;
1209 {
1210 struct text_pos pos;
1211 xassert (STRINGP (string));
1212 xassert (charpos >= 0);
1213 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1214 return pos;
1215 }
1216
1217
1218 /* Value is a text position, i.e. character and byte position, for
1219 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1220 means recognize multibyte characters. */
1221
1222 static struct text_pos
1223 c_string_pos (charpos, s, multibyte_p)
1224 int charpos;
1225 unsigned char *s;
1226 int multibyte_p;
1227 {
1228 struct text_pos pos;
1229
1230 xassert (s != NULL);
1231 xassert (charpos >= 0);
1232
1233 if (multibyte_p)
1234 {
1235 int rest = strlen (s), len;
1236
1237 SET_TEXT_POS (pos, 0, 0);
1238 while (charpos--)
1239 {
1240 string_char_and_length (s, rest, &len);
1241 s += len, rest -= len;
1242 xassert (rest >= 0);
1243 CHARPOS (pos) += 1;
1244 BYTEPOS (pos) += len;
1245 }
1246 }
1247 else
1248 SET_TEXT_POS (pos, charpos, charpos);
1249
1250 return pos;
1251 }
1252
1253
1254 /* Value is the number of characters in C string S. MULTIBYTE_P
1255 non-zero means recognize multibyte characters. */
1256
1257 static int
1258 number_of_chars (s, multibyte_p)
1259 unsigned char *s;
1260 int multibyte_p;
1261 {
1262 int nchars;
1263
1264 if (multibyte_p)
1265 {
1266 int rest = strlen (s), len;
1267 unsigned char *p = (unsigned char *) s;
1268
1269 for (nchars = 0; rest > 0; ++nchars)
1270 {
1271 string_char_and_length (p, rest, &len);
1272 rest -= len, p += len;
1273 }
1274 }
1275 else
1276 nchars = strlen (s);
1277
1278 return nchars;
1279 }
1280
1281
1282 /* Compute byte position NEWPOS->bytepos corresponding to
1283 NEWPOS->charpos. POS is a known position in string STRING.
1284 NEWPOS->charpos must be >= POS.charpos. */
1285
1286 static void
1287 compute_string_pos (newpos, pos, string)
1288 struct text_pos *newpos, pos;
1289 Lisp_Object string;
1290 {
1291 xassert (STRINGP (string));
1292 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1293
1294 if (STRING_MULTIBYTE (string))
1295 *newpos = string_pos_nchars_ahead (pos, string,
1296 CHARPOS (*newpos) - CHARPOS (pos));
1297 else
1298 BYTEPOS (*newpos) = CHARPOS (*newpos);
1299 }
1300
1301
1302 \f
1303 /***********************************************************************
1304 Lisp form evaluation
1305 ***********************************************************************/
1306
1307 /* Error handler for safe_eval and safe_call. */
1308
1309 static Lisp_Object
1310 safe_eval_handler (arg)
1311 Lisp_Object arg;
1312 {
1313 add_to_log ("Error during redisplay: %s", arg, Qnil);
1314 return Qnil;
1315 }
1316
1317
1318 /* Evaluate SEXPR and return the result, or nil if something went
1319 wrong. Prevent redisplay during the evaluation. */
1320
1321 Lisp_Object
1322 safe_eval (sexpr)
1323 Lisp_Object sexpr;
1324 {
1325 Lisp_Object val;
1326
1327 if (inhibit_eval_during_redisplay)
1328 val = Qnil;
1329 else
1330 {
1331 int count = SPECPDL_INDEX ();
1332 struct gcpro gcpro1;
1333
1334 GCPRO1 (sexpr);
1335 specbind (Qinhibit_redisplay, Qt);
1336 /* Use Qt to ensure debugger does not run,
1337 so there is no possibility of wanting to redisplay. */
1338 val = internal_condition_case_1 (Feval, sexpr, Qt,
1339 safe_eval_handler);
1340 UNGCPRO;
1341 val = unbind_to (count, val);
1342 }
1343
1344 return val;
1345 }
1346
1347
1348 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1349 Return the result, or nil if something went wrong. Prevent
1350 redisplay during the evaluation. */
1351
1352 Lisp_Object
1353 safe_call (nargs, args)
1354 int nargs;
1355 Lisp_Object *args;
1356 {
1357 Lisp_Object val;
1358
1359 if (inhibit_eval_during_redisplay)
1360 val = Qnil;
1361 else
1362 {
1363 int count = SPECPDL_INDEX ();
1364 struct gcpro gcpro1;
1365
1366 GCPRO1 (args[0]);
1367 gcpro1.nvars = nargs;
1368 specbind (Qinhibit_redisplay, Qt);
1369 /* Use Qt to ensure debugger does not run,
1370 so there is no possibility of wanting to redisplay. */
1371 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
1372 safe_eval_handler);
1373 UNGCPRO;
1374 val = unbind_to (count, val);
1375 }
1376
1377 return val;
1378 }
1379
1380
1381 /* Call function FN with one argument ARG.
1382 Return the result, or nil if something went wrong. */
1383
1384 Lisp_Object
1385 safe_call1 (fn, arg)
1386 Lisp_Object fn, arg;
1387 {
1388 Lisp_Object args[2];
1389 args[0] = fn;
1390 args[1] = arg;
1391 return safe_call (2, args);
1392 }
1393
1394
1395 \f
1396 /***********************************************************************
1397 Debugging
1398 ***********************************************************************/
1399
1400 #if 0
1401
1402 /* Define CHECK_IT to perform sanity checks on iterators.
1403 This is for debugging. It is too slow to do unconditionally. */
1404
1405 static void
1406 check_it (it)
1407 struct it *it;
1408 {
1409 if (it->method == next_element_from_string)
1410 {
1411 xassert (STRINGP (it->string));
1412 xassert (IT_STRING_CHARPOS (*it) >= 0);
1413 }
1414 else if (it->method == next_element_from_buffer)
1415 {
1416 /* Check that character and byte positions agree. */
1417 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1418 }
1419
1420 if (it->dpvec)
1421 xassert (it->current.dpvec_index >= 0);
1422 else
1423 xassert (it->current.dpvec_index < 0);
1424 }
1425
1426 #define CHECK_IT(IT) check_it ((IT))
1427
1428 #else /* not 0 */
1429
1430 #define CHECK_IT(IT) (void) 0
1431
1432 #endif /* not 0 */
1433
1434
1435 #if GLYPH_DEBUG
1436
1437 /* Check that the window end of window W is what we expect it
1438 to be---the last row in the current matrix displaying text. */
1439
1440 static void
1441 check_window_end (w)
1442 struct window *w;
1443 {
1444 if (!MINI_WINDOW_P (w)
1445 && !NILP (w->window_end_valid))
1446 {
1447 struct glyph_row *row;
1448 xassert ((row = MATRIX_ROW (w->current_matrix,
1449 XFASTINT (w->window_end_vpos)),
1450 !row->enabled_p
1451 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1452 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1453 }
1454 }
1455
1456 #define CHECK_WINDOW_END(W) check_window_end ((W))
1457
1458 #else /* not GLYPH_DEBUG */
1459
1460 #define CHECK_WINDOW_END(W) (void) 0
1461
1462 #endif /* not GLYPH_DEBUG */
1463
1464
1465 \f
1466 /***********************************************************************
1467 Iterator initialization
1468 ***********************************************************************/
1469
1470 /* Initialize IT for displaying current_buffer in window W, starting
1471 at character position CHARPOS. CHARPOS < 0 means that no buffer
1472 position is specified which is useful when the iterator is assigned
1473 a position later. BYTEPOS is the byte position corresponding to
1474 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
1475
1476 If ROW is not null, calls to produce_glyphs with IT as parameter
1477 will produce glyphs in that row.
1478
1479 BASE_FACE_ID is the id of a base face to use. It must be one of
1480 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
1481 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
1482 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
1483
1484 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
1485 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
1486 will be initialized to use the corresponding mode line glyph row of
1487 the desired matrix of W. */
1488
1489 void
1490 init_iterator (it, w, charpos, bytepos, row, base_face_id)
1491 struct it *it;
1492 struct window *w;
1493 int charpos, bytepos;
1494 struct glyph_row *row;
1495 enum face_id base_face_id;
1496 {
1497 int highlight_region_p;
1498
1499 /* Some precondition checks. */
1500 xassert (w != NULL && it != NULL);
1501 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
1502 && charpos <= ZV));
1503
1504 /* If face attributes have been changed since the last redisplay,
1505 free realized faces now because they depend on face definitions
1506 that might have changed. */
1507 if (face_change_count)
1508 {
1509 face_change_count = 0;
1510 free_all_realized_faces (Qnil);
1511 }
1512
1513 /* Use one of the mode line rows of W's desired matrix if
1514 appropriate. */
1515 if (row == NULL)
1516 {
1517 if (base_face_id == MODE_LINE_FACE_ID
1518 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
1519 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
1520 else if (base_face_id == HEADER_LINE_FACE_ID)
1521 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
1522 }
1523
1524 /* Clear IT. */
1525 bzero (it, sizeof *it);
1526 it->current.overlay_string_index = -1;
1527 it->current.dpvec_index = -1;
1528 it->base_face_id = base_face_id;
1529
1530 /* The window in which we iterate over current_buffer: */
1531 XSETWINDOW (it->window, w);
1532 it->w = w;
1533 it->f = XFRAME (w->frame);
1534
1535 /* Extra space between lines (on window systems only). */
1536 if (base_face_id == DEFAULT_FACE_ID
1537 && FRAME_WINDOW_P (it->f))
1538 {
1539 if (NATNUMP (current_buffer->extra_line_spacing))
1540 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
1541 else if (it->f->extra_line_spacing > 0)
1542 it->extra_line_spacing = it->f->extra_line_spacing;
1543 }
1544
1545 /* If realized faces have been removed, e.g. because of face
1546 attribute changes of named faces, recompute them. When running
1547 in batch mode, the face cache of Vterminal_frame is null. If
1548 we happen to get called, make a dummy face cache. */
1549 if (
1550 #ifndef WINDOWSNT
1551 noninteractive &&
1552 #endif
1553 FRAME_FACE_CACHE (it->f) == NULL)
1554 init_frame_faces (it->f);
1555 if (FRAME_FACE_CACHE (it->f)->used == 0)
1556 recompute_basic_faces (it->f);
1557
1558 /* Current value of the `space-width', and 'height' properties. */
1559 it->space_width = Qnil;
1560 it->font_height = Qnil;
1561
1562 /* Are control characters displayed as `^C'? */
1563 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
1564
1565 /* -1 means everything between a CR and the following line end
1566 is invisible. >0 means lines indented more than this value are
1567 invisible. */
1568 it->selective = (INTEGERP (current_buffer->selective_display)
1569 ? XFASTINT (current_buffer->selective_display)
1570 : (!NILP (current_buffer->selective_display)
1571 ? -1 : 0));
1572 it->selective_display_ellipsis_p
1573 = !NILP (current_buffer->selective_display_ellipses);
1574
1575 /* Display table to use. */
1576 it->dp = window_display_table (w);
1577
1578 /* Are multibyte characters enabled in current_buffer? */
1579 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1580
1581 /* Non-zero if we should highlight the region. */
1582 highlight_region_p
1583 = (!NILP (Vtransient_mark_mode)
1584 && !NILP (current_buffer->mark_active)
1585 && XMARKER (current_buffer->mark)->buffer != 0);
1586
1587 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
1588 start and end of a visible region in window IT->w. Set both to
1589 -1 to indicate no region. */
1590 if (highlight_region_p
1591 /* Maybe highlight only in selected window. */
1592 && (/* Either show region everywhere. */
1593 highlight_nonselected_windows
1594 /* Or show region in the selected window. */
1595 || w == XWINDOW (selected_window)
1596 /* Or show the region if we are in the mini-buffer and W is
1597 the window the mini-buffer refers to. */
1598 || (MINI_WINDOW_P (XWINDOW (selected_window))
1599 && WINDOWP (minibuf_selected_window)
1600 && w == XWINDOW (minibuf_selected_window))))
1601 {
1602 int charpos = marker_position (current_buffer->mark);
1603 it->region_beg_charpos = min (PT, charpos);
1604 it->region_end_charpos = max (PT, charpos);
1605 }
1606 else
1607 it->region_beg_charpos = it->region_end_charpos = -1;
1608
1609 /* Get the position at which the redisplay_end_trigger hook should
1610 be run, if it is to be run at all. */
1611 if (MARKERP (w->redisplay_end_trigger)
1612 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
1613 it->redisplay_end_trigger_charpos
1614 = marker_position (w->redisplay_end_trigger);
1615 else if (INTEGERP (w->redisplay_end_trigger))
1616 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
1617
1618 /* Correct bogus values of tab_width. */
1619 it->tab_width = XINT (current_buffer->tab_width);
1620 if (it->tab_width <= 0 || it->tab_width > 1000)
1621 it->tab_width = 8;
1622
1623 /* Are lines in the display truncated? */
1624 it->truncate_lines_p
1625 = (base_face_id != DEFAULT_FACE_ID
1626 || XINT (it->w->hscroll)
1627 || (truncate_partial_width_windows
1628 && !WINDOW_FULL_WIDTH_P (it->w))
1629 || !NILP (current_buffer->truncate_lines));
1630
1631 /* Get dimensions of truncation and continuation glyphs. These are
1632 displayed as fringe bitmaps under X, so we don't need them for such
1633 frames. */
1634 if (!FRAME_WINDOW_P (it->f))
1635 {
1636 if (it->truncate_lines_p)
1637 {
1638 /* We will need the truncation glyph. */
1639 xassert (it->glyph_row == NULL);
1640 produce_special_glyphs (it, IT_TRUNCATION);
1641 it->truncation_pixel_width = it->pixel_width;
1642 }
1643 else
1644 {
1645 /* We will need the continuation glyph. */
1646 xassert (it->glyph_row == NULL);
1647 produce_special_glyphs (it, IT_CONTINUATION);
1648 it->continuation_pixel_width = it->pixel_width;
1649 }
1650
1651 /* Reset these values to zero because the produce_special_glyphs
1652 above has changed them. */
1653 it->pixel_width = it->ascent = it->descent = 0;
1654 it->phys_ascent = it->phys_descent = 0;
1655 }
1656
1657 /* Set this after getting the dimensions of truncation and
1658 continuation glyphs, so that we don't produce glyphs when calling
1659 produce_special_glyphs, above. */
1660 it->glyph_row = row;
1661 it->area = TEXT_AREA;
1662
1663 /* Get the dimensions of the display area. The display area
1664 consists of the visible window area plus a horizontally scrolled
1665 part to the left of the window. All x-values are relative to the
1666 start of this total display area. */
1667 if (base_face_id != DEFAULT_FACE_ID)
1668 {
1669 /* Mode lines, menu bar in terminal frames. */
1670 it->first_visible_x = 0;
1671 it->last_visible_x = XFASTINT (w->width) * CANON_X_UNIT (it->f);
1672 }
1673 else
1674 {
1675 it->first_visible_x
1676 = XFASTINT (it->w->hscroll) * CANON_X_UNIT (it->f);
1677 it->last_visible_x = (it->first_visible_x
1678 + window_box_width (w, TEXT_AREA));
1679
1680 /* If we truncate lines, leave room for the truncator glyph(s) at
1681 the right margin. Otherwise, leave room for the continuation
1682 glyph(s). Truncation and continuation glyphs are not inserted
1683 for window-based redisplay. */
1684 if (!FRAME_WINDOW_P (it->f))
1685 {
1686 if (it->truncate_lines_p)
1687 it->last_visible_x -= it->truncation_pixel_width;
1688 else
1689 it->last_visible_x -= it->continuation_pixel_width;
1690 }
1691
1692 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
1693 it->current_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w) + w->vscroll;
1694 }
1695
1696 /* Leave room for a border glyph. */
1697 if (!FRAME_WINDOW_P (it->f)
1698 && !WINDOW_RIGHTMOST_P (it->w))
1699 it->last_visible_x -= 1;
1700
1701 it->last_visible_y = window_text_bottom_y (w);
1702
1703 /* For mode lines and alike, arrange for the first glyph having a
1704 left box line if the face specifies a box. */
1705 if (base_face_id != DEFAULT_FACE_ID)
1706 {
1707 struct face *face;
1708
1709 it->face_id = base_face_id;
1710
1711 /* If we have a boxed mode line, make the first character appear
1712 with a left box line. */
1713 face = FACE_FROM_ID (it->f, base_face_id);
1714 if (face->box != FACE_NO_BOX)
1715 it->start_of_box_run_p = 1;
1716 }
1717
1718 /* If a buffer position was specified, set the iterator there,
1719 getting overlays and face properties from that position. */
1720 if (charpos >= BUF_BEG (current_buffer))
1721 {
1722 it->end_charpos = ZV;
1723 it->face_id = -1;
1724 IT_CHARPOS (*it) = charpos;
1725
1726 /* Compute byte position if not specified. */
1727 if (bytepos < charpos)
1728 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
1729 else
1730 IT_BYTEPOS (*it) = bytepos;
1731
1732 /* Compute faces etc. */
1733 reseat (it, it->current.pos, 1);
1734 }
1735
1736 CHECK_IT (it);
1737 }
1738
1739
1740 /* Initialize IT for the display of window W with window start POS. */
1741
1742 void
1743 start_display (it, w, pos)
1744 struct it *it;
1745 struct window *w;
1746 struct text_pos pos;
1747 {
1748 struct glyph_row *row;
1749 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
1750
1751 row = w->desired_matrix->rows + first_vpos;
1752 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
1753
1754 if (!it->truncate_lines_p)
1755 {
1756 int start_at_line_beg_p;
1757 int first_y = it->current_y;
1758
1759 /* If window start is not at a line start, skip forward to POS to
1760 get the correct continuation lines width. */
1761 start_at_line_beg_p = (CHARPOS (pos) == BEGV
1762 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
1763 if (!start_at_line_beg_p)
1764 {
1765 reseat_at_previous_visible_line_start (it);
1766 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
1767
1768 /* If lines are continued, this line may end in the middle
1769 of a multi-glyph character (e.g. a control character
1770 displayed as \003, or in the middle of an overlay
1771 string). In this case move_it_to above will not have
1772 taken us to the start of the continuation line but to the
1773 end of the continued line. */
1774 if (it->current_x > 0)
1775 {
1776 if (it->current.dpvec_index >= 0
1777 || it->current.overlay_string_index >= 0)
1778 {
1779 set_iterator_to_next (it, 1);
1780 move_it_in_display_line_to (it, -1, -1, 0);
1781 }
1782
1783 it->continuation_lines_width += it->current_x;
1784 }
1785
1786 /* We're starting a new display line, not affected by the
1787 height of the continued line, so clear the appropriate
1788 fields in the iterator structure. */
1789 it->max_ascent = it->max_descent = 0;
1790 it->max_phys_ascent = it->max_phys_descent = 0;
1791
1792 it->current_y = first_y;
1793 it->vpos = 0;
1794 it->current_x = it->hpos = 0;
1795 }
1796 }
1797
1798 #if 0 /* Don't assert the following because start_display is sometimes
1799 called intentionally with a window start that is not at a
1800 line start. Please leave this code in as a comment. */
1801
1802 /* Window start should be on a line start, now. */
1803 xassert (it->continuation_lines_width
1804 || IT_CHARPOS (it) == BEGV
1805 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
1806 #endif /* 0 */
1807 }
1808
1809
1810 /* Return 1 if POS is a position in ellipses displayed for invisible
1811 text. W is the window we display, for text property lookup. */
1812
1813 static int
1814 in_ellipses_for_invisible_text_p (pos, w)
1815 struct display_pos *pos;
1816 struct window *w;
1817 {
1818 Lisp_Object prop, window;
1819 int ellipses_p = 0;
1820 int charpos = CHARPOS (pos->pos);
1821
1822 /* If POS specifies a position in a display vector, this might
1823 be for an ellipsis displayed for invisible text. We won't
1824 get the iterator set up for delivering that ellipsis unless
1825 we make sure that it gets aware of the invisible text. */
1826 if (pos->dpvec_index >= 0
1827 && pos->overlay_string_index < 0
1828 && CHARPOS (pos->string_pos) < 0
1829 && charpos > BEGV
1830 && (XSETWINDOW (window, w),
1831 prop = Fget_char_property (make_number (charpos),
1832 Qinvisible, window),
1833 !TEXT_PROP_MEANS_INVISIBLE (prop)))
1834 {
1835 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
1836 window);
1837 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
1838 }
1839
1840 return ellipses_p;
1841 }
1842
1843
1844 /* Initialize IT for stepping through current_buffer in window W,
1845 starting at position POS that includes overlay string and display
1846 vector/ control character translation position information. Value
1847 is zero if there are overlay strings with newlines at POS. */
1848
1849 static int
1850 init_from_display_pos (it, w, pos)
1851 struct it *it;
1852 struct window *w;
1853 struct display_pos *pos;
1854 {
1855 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
1856 int i, overlay_strings_with_newlines = 0;
1857
1858 /* If POS specifies a position in a display vector, this might
1859 be for an ellipsis displayed for invisible text. We won't
1860 get the iterator set up for delivering that ellipsis unless
1861 we make sure that it gets aware of the invisible text. */
1862 if (in_ellipses_for_invisible_text_p (pos, w))
1863 {
1864 --charpos;
1865 bytepos = 0;
1866 }
1867
1868 /* Keep in mind: the call to reseat in init_iterator skips invisible
1869 text, so we might end up at a position different from POS. This
1870 is only a problem when POS is a row start after a newline and an
1871 overlay starts there with an after-string, and the overlay has an
1872 invisible property. Since we don't skip invisible text in
1873 display_line and elsewhere immediately after consuming the
1874 newline before the row start, such a POS will not be in a string,
1875 but the call to init_iterator below will move us to the
1876 after-string. */
1877 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
1878
1879 for (i = 0; i < it->n_overlay_strings; ++i)
1880 {
1881 const char *s = SDATA (it->overlay_strings[i]);
1882 const char *e = s + SBYTES (it->overlay_strings[i]);
1883
1884 while (s < e && *s != '\n')
1885 ++s;
1886
1887 if (s < e)
1888 {
1889 overlay_strings_with_newlines = 1;
1890 break;
1891 }
1892 }
1893
1894 /* If position is within an overlay string, set up IT to the right
1895 overlay string. */
1896 if (pos->overlay_string_index >= 0)
1897 {
1898 int relative_index;
1899
1900 /* If the first overlay string happens to have a `display'
1901 property for an image, the iterator will be set up for that
1902 image, and we have to undo that setup first before we can
1903 correct the overlay string index. */
1904 if (it->method == next_element_from_image)
1905 pop_it (it);
1906
1907 /* We already have the first chunk of overlay strings in
1908 IT->overlay_strings. Load more until the one for
1909 pos->overlay_string_index is in IT->overlay_strings. */
1910 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
1911 {
1912 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
1913 it->current.overlay_string_index = 0;
1914 while (n--)
1915 {
1916 load_overlay_strings (it, 0);
1917 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
1918 }
1919 }
1920
1921 it->current.overlay_string_index = pos->overlay_string_index;
1922 relative_index = (it->current.overlay_string_index
1923 % OVERLAY_STRING_CHUNK_SIZE);
1924 it->string = it->overlay_strings[relative_index];
1925 xassert (STRINGP (it->string));
1926 it->current.string_pos = pos->string_pos;
1927 it->method = next_element_from_string;
1928 }
1929
1930 #if 0 /* This is bogus because POS not having an overlay string
1931 position does not mean it's after the string. Example: A
1932 line starting with a before-string and initialization of IT
1933 to the previous row's end position. */
1934 else if (it->current.overlay_string_index >= 0)
1935 {
1936 /* If POS says we're already after an overlay string ending at
1937 POS, make sure to pop the iterator because it will be in
1938 front of that overlay string. When POS is ZV, we've thereby
1939 also ``processed'' overlay strings at ZV. */
1940 while (it->sp)
1941 pop_it (it);
1942 it->current.overlay_string_index = -1;
1943 it->method = next_element_from_buffer;
1944 if (CHARPOS (pos->pos) == ZV)
1945 it->overlay_strings_at_end_processed_p = 1;
1946 }
1947 #endif /* 0 */
1948
1949 if (CHARPOS (pos->string_pos) >= 0)
1950 {
1951 /* Recorded position is not in an overlay string, but in another
1952 string. This can only be a string from a `display' property.
1953 IT should already be filled with that string. */
1954 it->current.string_pos = pos->string_pos;
1955 xassert (STRINGP (it->string));
1956 }
1957
1958 /* Restore position in display vector translations, control
1959 character translations or ellipses. */
1960 if (pos->dpvec_index >= 0)
1961 {
1962 if (it->dpvec == NULL)
1963 get_next_display_element (it);
1964 xassert (it->dpvec && it->current.dpvec_index == 0);
1965 it->current.dpvec_index = pos->dpvec_index;
1966 }
1967
1968 CHECK_IT (it);
1969 return !overlay_strings_with_newlines;
1970 }
1971
1972
1973 /* Initialize IT for stepping through current_buffer in window W
1974 starting at ROW->start. */
1975
1976 static void
1977 init_to_row_start (it, w, row)
1978 struct it *it;
1979 struct window *w;
1980 struct glyph_row *row;
1981 {
1982 init_from_display_pos (it, w, &row->start);
1983 it->continuation_lines_width = row->continuation_lines_width;
1984 CHECK_IT (it);
1985 }
1986
1987
1988 /* Initialize IT for stepping through current_buffer in window W
1989 starting in the line following ROW, i.e. starting at ROW->end.
1990 Value is zero if there are overlay strings with newlines at ROW's
1991 end position. */
1992
1993 static int
1994 init_to_row_end (it, w, row)
1995 struct it *it;
1996 struct window *w;
1997 struct glyph_row *row;
1998 {
1999 int success = 0;
2000
2001 if (init_from_display_pos (it, w, &row->end))
2002 {
2003 if (row->continued_p)
2004 it->continuation_lines_width
2005 = row->continuation_lines_width + row->pixel_width;
2006 CHECK_IT (it);
2007 success = 1;
2008 }
2009
2010 return success;
2011 }
2012
2013
2014
2015 \f
2016 /***********************************************************************
2017 Text properties
2018 ***********************************************************************/
2019
2020 /* Called when IT reaches IT->stop_charpos. Handle text property and
2021 overlay changes. Set IT->stop_charpos to the next position where
2022 to stop. */
2023
2024 static void
2025 handle_stop (it)
2026 struct it *it;
2027 {
2028 enum prop_handled handled;
2029 int handle_overlay_change_p = 1;
2030 struct props *p;
2031
2032 it->dpvec = NULL;
2033 it->current.dpvec_index = -1;
2034
2035 do
2036 {
2037 handled = HANDLED_NORMALLY;
2038
2039 /* Call text property handlers. */
2040 for (p = it_props; p->handler; ++p)
2041 {
2042 handled = p->handler (it);
2043
2044 if (handled == HANDLED_RECOMPUTE_PROPS)
2045 break;
2046 else if (handled == HANDLED_RETURN)
2047 return;
2048 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2049 handle_overlay_change_p = 0;
2050 }
2051
2052 if (handled != HANDLED_RECOMPUTE_PROPS)
2053 {
2054 /* Don't check for overlay strings below when set to deliver
2055 characters from a display vector. */
2056 if (it->method == next_element_from_display_vector)
2057 handle_overlay_change_p = 0;
2058
2059 /* Handle overlay changes. */
2060 if (handle_overlay_change_p)
2061 handled = handle_overlay_change (it);
2062
2063 /* Determine where to stop next. */
2064 if (handled == HANDLED_NORMALLY)
2065 compute_stop_pos (it);
2066 }
2067 }
2068 while (handled == HANDLED_RECOMPUTE_PROPS);
2069 }
2070
2071
2072 /* Compute IT->stop_charpos from text property and overlay change
2073 information for IT's current position. */
2074
2075 static void
2076 compute_stop_pos (it)
2077 struct it *it;
2078 {
2079 register INTERVAL iv, next_iv;
2080 Lisp_Object object, limit, position;
2081
2082 /* If nowhere else, stop at the end. */
2083 it->stop_charpos = it->end_charpos;
2084
2085 if (STRINGP (it->string))
2086 {
2087 /* Strings are usually short, so don't limit the search for
2088 properties. */
2089 object = it->string;
2090 limit = Qnil;
2091 position = make_number (IT_STRING_CHARPOS (*it));
2092 }
2093 else
2094 {
2095 int charpos;
2096
2097 /* If next overlay change is in front of the current stop pos
2098 (which is IT->end_charpos), stop there. Note: value of
2099 next_overlay_change is point-max if no overlay change
2100 follows. */
2101 charpos = next_overlay_change (IT_CHARPOS (*it));
2102 if (charpos < it->stop_charpos)
2103 it->stop_charpos = charpos;
2104
2105 /* If showing the region, we have to stop at the region
2106 start or end because the face might change there. */
2107 if (it->region_beg_charpos > 0)
2108 {
2109 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2110 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2111 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2112 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2113 }
2114
2115 /* Set up variables for computing the stop position from text
2116 property changes. */
2117 XSETBUFFER (object, current_buffer);
2118 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2119 position = make_number (IT_CHARPOS (*it));
2120
2121 }
2122
2123 /* Get the interval containing IT's position. Value is a null
2124 interval if there isn't such an interval. */
2125 iv = validate_interval_range (object, &position, &position, 0);
2126 if (!NULL_INTERVAL_P (iv))
2127 {
2128 Lisp_Object values_here[LAST_PROP_IDX];
2129 struct props *p;
2130
2131 /* Get properties here. */
2132 for (p = it_props; p->handler; ++p)
2133 values_here[p->idx] = textget (iv->plist, *p->name);
2134
2135 /* Look for an interval following iv that has different
2136 properties. */
2137 for (next_iv = next_interval (iv);
2138 (!NULL_INTERVAL_P (next_iv)
2139 && (NILP (limit)
2140 || XFASTINT (limit) > next_iv->position));
2141 next_iv = next_interval (next_iv))
2142 {
2143 for (p = it_props; p->handler; ++p)
2144 {
2145 Lisp_Object new_value;
2146
2147 new_value = textget (next_iv->plist, *p->name);
2148 if (!EQ (values_here[p->idx], new_value))
2149 break;
2150 }
2151
2152 if (p->handler)
2153 break;
2154 }
2155
2156 if (!NULL_INTERVAL_P (next_iv))
2157 {
2158 if (INTEGERP (limit)
2159 && next_iv->position >= XFASTINT (limit))
2160 /* No text property change up to limit. */
2161 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2162 else
2163 /* Text properties change in next_iv. */
2164 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2165 }
2166 }
2167
2168 xassert (STRINGP (it->string)
2169 || (it->stop_charpos >= BEGV
2170 && it->stop_charpos >= IT_CHARPOS (*it)));
2171 }
2172
2173
2174 /* Return the position of the next overlay change after POS in
2175 current_buffer. Value is point-max if no overlay change
2176 follows. This is like `next-overlay-change' but doesn't use
2177 xmalloc. */
2178
2179 static int
2180 next_overlay_change (pos)
2181 int pos;
2182 {
2183 int noverlays;
2184 int endpos;
2185 Lisp_Object *overlays;
2186 int len;
2187 int i;
2188
2189 /* Get all overlays at the given position. */
2190 len = 10;
2191 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2192 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2193 if (noverlays > len)
2194 {
2195 len = noverlays;
2196 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2197 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2198 }
2199
2200 /* If any of these overlays ends before endpos,
2201 use its ending point instead. */
2202 for (i = 0; i < noverlays; ++i)
2203 {
2204 Lisp_Object oend;
2205 int oendpos;
2206
2207 oend = OVERLAY_END (overlays[i]);
2208 oendpos = OVERLAY_POSITION (oend);
2209 endpos = min (endpos, oendpos);
2210 }
2211
2212 return endpos;
2213 }
2214
2215
2216 \f
2217 /***********************************************************************
2218 Fontification
2219 ***********************************************************************/
2220
2221 /* Handle changes in the `fontified' property of the current buffer by
2222 calling hook functions from Qfontification_functions to fontify
2223 regions of text. */
2224
2225 static enum prop_handled
2226 handle_fontified_prop (it)
2227 struct it *it;
2228 {
2229 Lisp_Object prop, pos;
2230 enum prop_handled handled = HANDLED_NORMALLY;
2231
2232 /* Get the value of the `fontified' property at IT's current buffer
2233 position. (The `fontified' property doesn't have a special
2234 meaning in strings.) If the value is nil, call functions from
2235 Qfontification_functions. */
2236 if (!STRINGP (it->string)
2237 && it->s == NULL
2238 && !NILP (Vfontification_functions)
2239 && !NILP (Vrun_hooks)
2240 && (pos = make_number (IT_CHARPOS (*it)),
2241 prop = Fget_char_property (pos, Qfontified, Qnil),
2242 NILP (prop)))
2243 {
2244 int count = SPECPDL_INDEX ();
2245 Lisp_Object val;
2246
2247 val = Vfontification_functions;
2248 specbind (Qfontification_functions, Qnil);
2249
2250 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2251 safe_call1 (val, pos);
2252 else
2253 {
2254 Lisp_Object globals, fn;
2255 struct gcpro gcpro1, gcpro2;
2256
2257 globals = Qnil;
2258 GCPRO2 (val, globals);
2259
2260 for (; CONSP (val); val = XCDR (val))
2261 {
2262 fn = XCAR (val);
2263
2264 if (EQ (fn, Qt))
2265 {
2266 /* A value of t indicates this hook has a local
2267 binding; it means to run the global binding too.
2268 In a global value, t should not occur. If it
2269 does, we must ignore it to avoid an endless
2270 loop. */
2271 for (globals = Fdefault_value (Qfontification_functions);
2272 CONSP (globals);
2273 globals = XCDR (globals))
2274 {
2275 fn = XCAR (globals);
2276 if (!EQ (fn, Qt))
2277 safe_call1 (fn, pos);
2278 }
2279 }
2280 else
2281 safe_call1 (fn, pos);
2282 }
2283
2284 UNGCPRO;
2285 }
2286
2287 unbind_to (count, Qnil);
2288
2289 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2290 something. This avoids an endless loop if they failed to
2291 fontify the text for which reason ever. */
2292 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2293 handled = HANDLED_RECOMPUTE_PROPS;
2294 }
2295
2296 return handled;
2297 }
2298
2299
2300 \f
2301 /***********************************************************************
2302 Faces
2303 ***********************************************************************/
2304
2305 /* Set up iterator IT from face properties at its current position.
2306 Called from handle_stop. */
2307
2308 static enum prop_handled
2309 handle_face_prop (it)
2310 struct it *it;
2311 {
2312 int new_face_id, next_stop;
2313
2314 if (!STRINGP (it->string))
2315 {
2316 new_face_id
2317 = face_at_buffer_position (it->w,
2318 IT_CHARPOS (*it),
2319 it->region_beg_charpos,
2320 it->region_end_charpos,
2321 &next_stop,
2322 (IT_CHARPOS (*it)
2323 + TEXT_PROP_DISTANCE_LIMIT),
2324 0);
2325
2326 /* Is this a start of a run of characters with box face?
2327 Caveat: this can be called for a freshly initialized
2328 iterator; face_id is -1 in this case. We know that the new
2329 face will not change until limit, i.e. if the new face has a
2330 box, all characters up to limit will have one. But, as
2331 usual, we don't know whether limit is really the end. */
2332 if (new_face_id != it->face_id)
2333 {
2334 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2335
2336 /* If new face has a box but old face has not, this is
2337 the start of a run of characters with box, i.e. it has
2338 a shadow on the left side. The value of face_id of the
2339 iterator will be -1 if this is the initial call that gets
2340 the face. In this case, we have to look in front of IT's
2341 position and see whether there is a face != new_face_id. */
2342 it->start_of_box_run_p
2343 = (new_face->box != FACE_NO_BOX
2344 && (it->face_id >= 0
2345 || IT_CHARPOS (*it) == BEG
2346 || new_face_id != face_before_it_pos (it)));
2347 it->face_box_p = new_face->box != FACE_NO_BOX;
2348 }
2349 }
2350 else
2351 {
2352 int base_face_id, bufpos;
2353
2354 if (it->current.overlay_string_index >= 0)
2355 bufpos = IT_CHARPOS (*it);
2356 else
2357 bufpos = 0;
2358
2359 /* For strings from a buffer, i.e. overlay strings or strings
2360 from a `display' property, use the face at IT's current
2361 buffer position as the base face to merge with, so that
2362 overlay strings appear in the same face as surrounding
2363 text, unless they specify their own faces. */
2364 base_face_id = underlying_face_id (it);
2365
2366 new_face_id = face_at_string_position (it->w,
2367 it->string,
2368 IT_STRING_CHARPOS (*it),
2369 bufpos,
2370 it->region_beg_charpos,
2371 it->region_end_charpos,
2372 &next_stop,
2373 base_face_id, 0);
2374
2375 #if 0 /* This shouldn't be neccessary. Let's check it. */
2376 /* If IT is used to display a mode line we would really like to
2377 use the mode line face instead of the frame's default face. */
2378 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2379 && new_face_id == DEFAULT_FACE_ID)
2380 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
2381 #endif
2382
2383 /* Is this a start of a run of characters with box? Caveat:
2384 this can be called for a freshly allocated iterator; face_id
2385 is -1 is this case. We know that the new face will not
2386 change until the next check pos, i.e. if the new face has a
2387 box, all characters up to that position will have a
2388 box. But, as usual, we don't know whether that position
2389 is really the end. */
2390 if (new_face_id != it->face_id)
2391 {
2392 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2393 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2394
2395 /* If new face has a box but old face hasn't, this is the
2396 start of a run of characters with box, i.e. it has a
2397 shadow on the left side. */
2398 it->start_of_box_run_p
2399 = new_face->box && (old_face == NULL || !old_face->box);
2400 it->face_box_p = new_face->box != FACE_NO_BOX;
2401 }
2402 }
2403
2404 it->face_id = new_face_id;
2405 return HANDLED_NORMALLY;
2406 }
2407
2408
2409 /* Return the ID of the face ``underlying'' IT's current position,
2410 which is in a string. If the iterator is associated with a
2411 buffer, return the face at IT's current buffer position.
2412 Otherwise, use the iterator's base_face_id. */
2413
2414 static int
2415 underlying_face_id (it)
2416 struct it *it;
2417 {
2418 int face_id = it->base_face_id, i;
2419
2420 xassert (STRINGP (it->string));
2421
2422 for (i = it->sp - 1; i >= 0; --i)
2423 if (NILP (it->stack[i].string))
2424 face_id = it->stack[i].face_id;
2425
2426 return face_id;
2427 }
2428
2429
2430 /* Compute the face one character before or after the current position
2431 of IT. BEFORE_P non-zero means get the face in front of IT's
2432 position. Value is the id of the face. */
2433
2434 static int
2435 face_before_or_after_it_pos (it, before_p)
2436 struct it *it;
2437 int before_p;
2438 {
2439 int face_id, limit;
2440 int next_check_charpos;
2441 struct text_pos pos;
2442
2443 xassert (it->s == NULL);
2444
2445 if (STRINGP (it->string))
2446 {
2447 int bufpos, base_face_id;
2448
2449 /* No face change past the end of the string (for the case
2450 we are padding with spaces). No face change before the
2451 string start. */
2452 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
2453 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2454 return it->face_id;
2455
2456 /* Set pos to the position before or after IT's current position. */
2457 if (before_p)
2458 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2459 else
2460 /* For composition, we must check the character after the
2461 composition. */
2462 pos = (it->what == IT_COMPOSITION
2463 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
2464 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
2465
2466 if (it->current.overlay_string_index >= 0)
2467 bufpos = IT_CHARPOS (*it);
2468 else
2469 bufpos = 0;
2470
2471 base_face_id = underlying_face_id (it);
2472
2473 /* Get the face for ASCII, or unibyte. */
2474 face_id = face_at_string_position (it->w,
2475 it->string,
2476 CHARPOS (pos),
2477 bufpos,
2478 it->region_beg_charpos,
2479 it->region_end_charpos,
2480 &next_check_charpos,
2481 base_face_id, 0);
2482
2483 /* Correct the face for charsets different from ASCII. Do it
2484 for the multibyte case only. The face returned above is
2485 suitable for unibyte text if IT->string is unibyte. */
2486 if (STRING_MULTIBYTE (it->string))
2487 {
2488 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
2489 int rest = SBYTES (it->string) - BYTEPOS (pos);
2490 int c, len;
2491 struct face *face = FACE_FROM_ID (it->f, face_id);
2492
2493 c = string_char_and_length (p, rest, &len);
2494 face_id = FACE_FOR_CHAR (it->f, face, c);
2495 }
2496 }
2497 else
2498 {
2499 if ((IT_CHARPOS (*it) >= ZV && !before_p)
2500 || (IT_CHARPOS (*it) <= BEGV && before_p))
2501 return it->face_id;
2502
2503 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
2504 pos = it->current.pos;
2505
2506 if (before_p)
2507 DEC_TEXT_POS (pos, it->multibyte_p);
2508 else
2509 {
2510 if (it->what == IT_COMPOSITION)
2511 /* For composition, we must check the position after the
2512 composition. */
2513 pos.charpos += it->cmp_len, pos.bytepos += it->len;
2514 else
2515 INC_TEXT_POS (pos, it->multibyte_p);
2516 }
2517
2518 /* Determine face for CHARSET_ASCII, or unibyte. */
2519 face_id = face_at_buffer_position (it->w,
2520 CHARPOS (pos),
2521 it->region_beg_charpos,
2522 it->region_end_charpos,
2523 &next_check_charpos,
2524 limit, 0);
2525
2526 /* Correct the face for charsets different from ASCII. Do it
2527 for the multibyte case only. The face returned above is
2528 suitable for unibyte text if current_buffer is unibyte. */
2529 if (it->multibyte_p)
2530 {
2531 int c = FETCH_MULTIBYTE_CHAR (CHARPOS (pos));
2532 struct face *face = FACE_FROM_ID (it->f, face_id);
2533 face_id = FACE_FOR_CHAR (it->f, face, c);
2534 }
2535 }
2536
2537 return face_id;
2538 }
2539
2540
2541 \f
2542 /***********************************************************************
2543 Invisible text
2544 ***********************************************************************/
2545
2546 /* Set up iterator IT from invisible properties at its current
2547 position. Called from handle_stop. */
2548
2549 static enum prop_handled
2550 handle_invisible_prop (it)
2551 struct it *it;
2552 {
2553 enum prop_handled handled = HANDLED_NORMALLY;
2554
2555 if (STRINGP (it->string))
2556 {
2557 extern Lisp_Object Qinvisible;
2558 Lisp_Object prop, end_charpos, limit, charpos;
2559
2560 /* Get the value of the invisible text property at the
2561 current position. Value will be nil if there is no such
2562 property. */
2563 charpos = make_number (IT_STRING_CHARPOS (*it));
2564 prop = Fget_text_property (charpos, Qinvisible, it->string);
2565
2566 if (!NILP (prop)
2567 && IT_STRING_CHARPOS (*it) < it->end_charpos)
2568 {
2569 handled = HANDLED_RECOMPUTE_PROPS;
2570
2571 /* Get the position at which the next change of the
2572 invisible text property can be found in IT->string.
2573 Value will be nil if the property value is the same for
2574 all the rest of IT->string. */
2575 XSETINT (limit, SCHARS (it->string));
2576 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2577 it->string, limit);
2578
2579 /* Text at current position is invisible. The next
2580 change in the property is at position end_charpos.
2581 Move IT's current position to that position. */
2582 if (INTEGERP (end_charpos)
2583 && XFASTINT (end_charpos) < XFASTINT (limit))
2584 {
2585 struct text_pos old;
2586 old = it->current.string_pos;
2587 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
2588 compute_string_pos (&it->current.string_pos, old, it->string);
2589 }
2590 else
2591 {
2592 /* The rest of the string is invisible. If this is an
2593 overlay string, proceed with the next overlay string
2594 or whatever comes and return a character from there. */
2595 if (it->current.overlay_string_index >= 0)
2596 {
2597 next_overlay_string (it);
2598 /* Don't check for overlay strings when we just
2599 finished processing them. */
2600 handled = HANDLED_OVERLAY_STRING_CONSUMED;
2601 }
2602 else
2603 {
2604 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
2605 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
2606 }
2607 }
2608 }
2609 }
2610 else
2611 {
2612 int invis_p, newpos, next_stop, start_charpos;
2613 Lisp_Object pos, prop, overlay;
2614
2615 /* First of all, is there invisible text at this position? */
2616 start_charpos = IT_CHARPOS (*it);
2617 pos = make_number (IT_CHARPOS (*it));
2618 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
2619 &overlay);
2620 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
2621
2622 /* If we are on invisible text, skip over it. */
2623 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
2624 {
2625 /* Record whether we have to display an ellipsis for the
2626 invisible text. */
2627 int display_ellipsis_p = invis_p == 2;
2628
2629 handled = HANDLED_RECOMPUTE_PROPS;
2630
2631 /* Loop skipping over invisible text. The loop is left at
2632 ZV or with IT on the first char being visible again. */
2633 do
2634 {
2635 /* Try to skip some invisible text. Return value is the
2636 position reached which can be equal to IT's position
2637 if there is nothing invisible here. This skips both
2638 over invisible text properties and overlays with
2639 invisible property. */
2640 newpos = skip_invisible (IT_CHARPOS (*it),
2641 &next_stop, ZV, it->window);
2642
2643 /* If we skipped nothing at all we weren't at invisible
2644 text in the first place. If everything to the end of
2645 the buffer was skipped, end the loop. */
2646 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
2647 invis_p = 0;
2648 else
2649 {
2650 /* We skipped some characters but not necessarily
2651 all there are. Check if we ended up on visible
2652 text. Fget_char_property returns the property of
2653 the char before the given position, i.e. if we
2654 get invis_p = 0, this means that the char at
2655 newpos is visible. */
2656 pos = make_number (newpos);
2657 prop = Fget_char_property (pos, Qinvisible, it->window);
2658 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
2659 }
2660
2661 /* If we ended up on invisible text, proceed to
2662 skip starting with next_stop. */
2663 if (invis_p)
2664 IT_CHARPOS (*it) = next_stop;
2665 }
2666 while (invis_p);
2667
2668 /* The position newpos is now either ZV or on visible text. */
2669 IT_CHARPOS (*it) = newpos;
2670 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2671
2672 /* If there are before-strings at the start of invisible
2673 text, and the text is invisible because of a text
2674 property, arrange to show before-strings because 20.x did
2675 it that way. (If the text is invisible because of an
2676 overlay property instead of a text property, this is
2677 already handled in the overlay code.) */
2678 if (NILP (overlay)
2679 && get_overlay_strings (it, start_charpos))
2680 {
2681 handled = HANDLED_RECOMPUTE_PROPS;
2682 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
2683 }
2684 else if (display_ellipsis_p)
2685 setup_for_ellipsis (it);
2686 }
2687 }
2688
2689 return handled;
2690 }
2691
2692
2693 /* Make iterator IT return `...' next. */
2694
2695 static void
2696 setup_for_ellipsis (it)
2697 struct it *it;
2698 {
2699 if (it->dp
2700 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
2701 {
2702 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
2703 it->dpvec = v->contents;
2704 it->dpend = v->contents + v->size;
2705 }
2706 else
2707 {
2708 /* Default `...'. */
2709 it->dpvec = default_invis_vector;
2710 it->dpend = default_invis_vector + 3;
2711 }
2712
2713 /* The ellipsis display does not replace the display of the
2714 character at the new position. Indicate this by setting
2715 IT->dpvec_char_len to zero. */
2716 it->dpvec_char_len = 0;
2717
2718 it->current.dpvec_index = 0;
2719 it->method = next_element_from_display_vector;
2720 }
2721
2722
2723 \f
2724 /***********************************************************************
2725 'display' property
2726 ***********************************************************************/
2727
2728 /* Set up iterator IT from `display' property at its current position.
2729 Called from handle_stop. */
2730
2731 static enum prop_handled
2732 handle_display_prop (it)
2733 struct it *it;
2734 {
2735 Lisp_Object prop, object;
2736 struct text_pos *position;
2737 int display_replaced_p = 0;
2738
2739 if (STRINGP (it->string))
2740 {
2741 object = it->string;
2742 position = &it->current.string_pos;
2743 }
2744 else
2745 {
2746 object = it->w->buffer;
2747 position = &it->current.pos;
2748 }
2749
2750 /* Reset those iterator values set from display property values. */
2751 it->font_height = Qnil;
2752 it->space_width = Qnil;
2753 it->voffset = 0;
2754
2755 /* We don't support recursive `display' properties, i.e. string
2756 values that have a string `display' property, that have a string
2757 `display' property etc. */
2758 if (!it->string_from_display_prop_p)
2759 it->area = TEXT_AREA;
2760
2761 prop = Fget_char_property (make_number (position->charpos),
2762 Qdisplay, object);
2763 if (NILP (prop))
2764 return HANDLED_NORMALLY;
2765
2766 if (CONSP (prop)
2767 /* Simple properties. */
2768 && !EQ (XCAR (prop), Qimage)
2769 && !EQ (XCAR (prop), Qspace)
2770 && !EQ (XCAR (prop), Qwhen)
2771 && !EQ (XCAR (prop), Qspace_width)
2772 && !EQ (XCAR (prop), Qheight)
2773 && !EQ (XCAR (prop), Qraise)
2774 /* Marginal area specifications. */
2775 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
2776 && !NILP (XCAR (prop)))
2777 {
2778 for (; CONSP (prop); prop = XCDR (prop))
2779 {
2780 if (handle_single_display_prop (it, XCAR (prop), object,
2781 position, display_replaced_p))
2782 display_replaced_p = 1;
2783 }
2784 }
2785 else if (VECTORP (prop))
2786 {
2787 int i;
2788 for (i = 0; i < ASIZE (prop); ++i)
2789 if (handle_single_display_prop (it, AREF (prop, i), object,
2790 position, display_replaced_p))
2791 display_replaced_p = 1;
2792 }
2793 else
2794 {
2795 if (handle_single_display_prop (it, prop, object, position, 0))
2796 display_replaced_p = 1;
2797 }
2798
2799 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
2800 }
2801
2802
2803 /* Value is the position of the end of the `display' property starting
2804 at START_POS in OBJECT. */
2805
2806 static struct text_pos
2807 display_prop_end (it, object, start_pos)
2808 struct it *it;
2809 Lisp_Object object;
2810 struct text_pos start_pos;
2811 {
2812 Lisp_Object end;
2813 struct text_pos end_pos;
2814
2815 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
2816 Qdisplay, object, Qnil);
2817 CHARPOS (end_pos) = XFASTINT (end);
2818 if (STRINGP (object))
2819 compute_string_pos (&end_pos, start_pos, it->string);
2820 else
2821 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
2822
2823 return end_pos;
2824 }
2825
2826
2827 /* Set up IT from a single `display' sub-property value PROP. OBJECT
2828 is the object in which the `display' property was found. *POSITION
2829 is the position at which it was found. DISPLAY_REPLACED_P non-zero
2830 means that we previously saw a display sub-property which already
2831 replaced text display with something else, for example an image;
2832 ignore such properties after the first one has been processed.
2833
2834 If PROP is a `space' or `image' sub-property, set *POSITION to the
2835 end position of the `display' property.
2836
2837 Value is non-zero if something was found which replaces the display
2838 of buffer or string text. */
2839
2840 static int
2841 handle_single_display_prop (it, prop, object, position,
2842 display_replaced_before_p)
2843 struct it *it;
2844 Lisp_Object prop;
2845 Lisp_Object object;
2846 struct text_pos *position;
2847 int display_replaced_before_p;
2848 {
2849 Lisp_Object value;
2850 int replaces_text_display_p = 0;
2851 Lisp_Object form;
2852
2853 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
2854 evaluated. If the result is nil, VALUE is ignored. */
2855 form = Qt;
2856 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2857 {
2858 prop = XCDR (prop);
2859 if (!CONSP (prop))
2860 return 0;
2861 form = XCAR (prop);
2862 prop = XCDR (prop);
2863 }
2864
2865 if (!NILP (form) && !EQ (form, Qt))
2866 {
2867 int count = SPECPDL_INDEX ();
2868 struct gcpro gcpro1;
2869
2870 /* Bind `object' to the object having the `display' property, a
2871 buffer or string. Bind `position' to the position in the
2872 object where the property was found, and `buffer-position'
2873 to the current position in the buffer. */
2874 specbind (Qobject, object);
2875 specbind (Qposition, make_number (CHARPOS (*position)));
2876 specbind (Qbuffer_position,
2877 make_number (STRINGP (object)
2878 ? IT_CHARPOS (*it) : CHARPOS (*position)));
2879 GCPRO1 (form);
2880 form = safe_eval (form);
2881 UNGCPRO;
2882 unbind_to (count, Qnil);
2883 }
2884
2885 if (NILP (form))
2886 return 0;
2887
2888 if (CONSP (prop)
2889 && EQ (XCAR (prop), Qheight)
2890 && CONSP (XCDR (prop)))
2891 {
2892 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2893 return 0;
2894
2895 /* `(height HEIGHT)'. */
2896 it->font_height = XCAR (XCDR (prop));
2897 if (!NILP (it->font_height))
2898 {
2899 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2900 int new_height = -1;
2901
2902 if (CONSP (it->font_height)
2903 && (EQ (XCAR (it->font_height), Qplus)
2904 || EQ (XCAR (it->font_height), Qminus))
2905 && CONSP (XCDR (it->font_height))
2906 && INTEGERP (XCAR (XCDR (it->font_height))))
2907 {
2908 /* `(+ N)' or `(- N)' where N is an integer. */
2909 int steps = XINT (XCAR (XCDR (it->font_height)));
2910 if (EQ (XCAR (it->font_height), Qplus))
2911 steps = - steps;
2912 it->face_id = smaller_face (it->f, it->face_id, steps);
2913 }
2914 else if (FUNCTIONP (it->font_height))
2915 {
2916 /* Call function with current height as argument.
2917 Value is the new height. */
2918 Lisp_Object height;
2919 height = safe_call1 (it->font_height,
2920 face->lface[LFACE_HEIGHT_INDEX]);
2921 if (NUMBERP (height))
2922 new_height = XFLOATINT (height);
2923 }
2924 else if (NUMBERP (it->font_height))
2925 {
2926 /* Value is a multiple of the canonical char height. */
2927 struct face *face;
2928
2929 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
2930 new_height = (XFLOATINT (it->font_height)
2931 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
2932 }
2933 else
2934 {
2935 /* Evaluate IT->font_height with `height' bound to the
2936 current specified height to get the new height. */
2937 Lisp_Object value;
2938 int count = SPECPDL_INDEX ();
2939
2940 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
2941 value = safe_eval (it->font_height);
2942 unbind_to (count, Qnil);
2943
2944 if (NUMBERP (value))
2945 new_height = XFLOATINT (value);
2946 }
2947
2948 if (new_height > 0)
2949 it->face_id = face_with_height (it->f, it->face_id, new_height);
2950 }
2951 }
2952 else if (CONSP (prop)
2953 && EQ (XCAR (prop), Qspace_width)
2954 && CONSP (XCDR (prop)))
2955 {
2956 /* `(space_width WIDTH)'. */
2957 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2958 return 0;
2959
2960 value = XCAR (XCDR (prop));
2961 if (NUMBERP (value) && XFLOATINT (value) > 0)
2962 it->space_width = value;
2963 }
2964 else if (CONSP (prop)
2965 && EQ (XCAR (prop), Qraise)
2966 && CONSP (XCDR (prop)))
2967 {
2968 /* `(raise FACTOR)'. */
2969 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2970 return 0;
2971
2972 #ifdef HAVE_WINDOW_SYSTEM
2973 value = XCAR (XCDR (prop));
2974 if (NUMBERP (value))
2975 {
2976 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2977 it->voffset = - (XFLOATINT (value)
2978 * (FONT_HEIGHT (face->font)));
2979 }
2980 #endif /* HAVE_WINDOW_SYSTEM */
2981 }
2982 else if (!it->string_from_display_prop_p)
2983 {
2984 /* `((margin left-margin) VALUE)' or `((margin right-margin)
2985 VALUE) or `((margin nil) VALUE)' or VALUE. */
2986 Lisp_Object location, value;
2987 struct text_pos start_pos;
2988 int valid_p;
2989
2990 /* Characters having this form of property are not displayed, so
2991 we have to find the end of the property. */
2992 start_pos = *position;
2993 *position = display_prop_end (it, object, start_pos);
2994 value = Qnil;
2995
2996 /* Let's stop at the new position and assume that all
2997 text properties change there. */
2998 it->stop_charpos = position->charpos;
2999
3000 location = Qunbound;
3001 if (CONSP (prop) && CONSP (XCAR (prop)))
3002 {
3003 Lisp_Object tem;
3004
3005 value = XCDR (prop);
3006 if (CONSP (value))
3007 value = XCAR (value);
3008
3009 tem = XCAR (prop);
3010 if (EQ (XCAR (tem), Qmargin)
3011 && (tem = XCDR (tem),
3012 tem = CONSP (tem) ? XCAR (tem) : Qnil,
3013 (NILP (tem)
3014 || EQ (tem, Qleft_margin)
3015 || EQ (tem, Qright_margin))))
3016 location = tem;
3017 }
3018
3019 if (EQ (location, Qunbound))
3020 {
3021 location = Qnil;
3022 value = prop;
3023 }
3024
3025 #ifdef HAVE_WINDOW_SYSTEM
3026 if (FRAME_TERMCAP_P (it->f))
3027 valid_p = STRINGP (value);
3028 else
3029 valid_p = (STRINGP (value)
3030 || (CONSP (value) && EQ (XCAR (value), Qspace))
3031 || valid_image_p (value));
3032 #else /* not HAVE_WINDOW_SYSTEM */
3033 valid_p = STRINGP (value);
3034 #endif /* not HAVE_WINDOW_SYSTEM */
3035
3036 if ((EQ (location, Qleft_margin)
3037 || EQ (location, Qright_margin)
3038 || NILP (location))
3039 && valid_p
3040 && !display_replaced_before_p)
3041 {
3042 replaces_text_display_p = 1;
3043
3044 /* Save current settings of IT so that we can restore them
3045 when we are finished with the glyph property value. */
3046 push_it (it);
3047
3048 if (NILP (location))
3049 it->area = TEXT_AREA;
3050 else if (EQ (location, Qleft_margin))
3051 it->area = LEFT_MARGIN_AREA;
3052 else
3053 it->area = RIGHT_MARGIN_AREA;
3054
3055 if (STRINGP (value))
3056 {
3057 it->string = value;
3058 it->multibyte_p = STRING_MULTIBYTE (it->string);
3059 it->current.overlay_string_index = -1;
3060 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3061 it->end_charpos = it->string_nchars = SCHARS (it->string);
3062 it->method = next_element_from_string;
3063 it->stop_charpos = 0;
3064 it->string_from_display_prop_p = 1;
3065 /* Say that we haven't consumed the characters with
3066 `display' property yet. The call to pop_it in
3067 set_iterator_to_next will clean this up. */
3068 *position = start_pos;
3069 }
3070 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3071 {
3072 it->method = next_element_from_stretch;
3073 it->object = value;
3074 it->current.pos = it->position = start_pos;
3075 }
3076 #ifdef HAVE_WINDOW_SYSTEM
3077 else
3078 {
3079 it->what = IT_IMAGE;
3080 it->image_id = lookup_image (it->f, value);
3081 it->position = start_pos;
3082 it->object = NILP (object) ? it->w->buffer : object;
3083 it->method = next_element_from_image;
3084
3085 /* Say that we haven't consumed the characters with
3086 `display' property yet. The call to pop_it in
3087 set_iterator_to_next will clean this up. */
3088 *position = start_pos;
3089 }
3090 #endif /* HAVE_WINDOW_SYSTEM */
3091 }
3092 else
3093 /* Invalid property or property not supported. Restore
3094 the position to what it was before. */
3095 *position = start_pos;
3096 }
3097
3098 return replaces_text_display_p;
3099 }
3100
3101
3102 /* Check if PROP is a display sub-property value whose text should be
3103 treated as intangible. */
3104
3105 static int
3106 single_display_prop_intangible_p (prop)
3107 Lisp_Object prop;
3108 {
3109 /* Skip over `when FORM'. */
3110 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3111 {
3112 prop = XCDR (prop);
3113 if (!CONSP (prop))
3114 return 0;
3115 prop = XCDR (prop);
3116 }
3117
3118 if (!CONSP (prop))
3119 return 0;
3120
3121 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3122 we don't need to treat text as intangible. */
3123 if (EQ (XCAR (prop), Qmargin))
3124 {
3125 prop = XCDR (prop);
3126 if (!CONSP (prop))
3127 return 0;
3128
3129 prop = XCDR (prop);
3130 if (!CONSP (prop)
3131 || EQ (XCAR (prop), Qleft_margin)
3132 || EQ (XCAR (prop), Qright_margin))
3133 return 0;
3134 }
3135
3136 return CONSP (prop) && EQ (XCAR (prop), Qimage);
3137 }
3138
3139
3140 /* Check if PROP is a display property value whose text should be
3141 treated as intangible. */
3142
3143 int
3144 display_prop_intangible_p (prop)
3145 Lisp_Object prop;
3146 {
3147 if (CONSP (prop)
3148 && CONSP (XCAR (prop))
3149 && !EQ (Qmargin, XCAR (XCAR (prop))))
3150 {
3151 /* A list of sub-properties. */
3152 while (CONSP (prop))
3153 {
3154 if (single_display_prop_intangible_p (XCAR (prop)))
3155 return 1;
3156 prop = XCDR (prop);
3157 }
3158 }
3159 else if (VECTORP (prop))
3160 {
3161 /* A vector of sub-properties. */
3162 int i;
3163 for (i = 0; i < ASIZE (prop); ++i)
3164 if (single_display_prop_intangible_p (AREF (prop, i)))
3165 return 1;
3166 }
3167 else
3168 return single_display_prop_intangible_p (prop);
3169
3170 return 0;
3171 }
3172
3173
3174 /* Return 1 if PROP is a display sub-property value containing STRING. */
3175
3176 static int
3177 single_display_prop_string_p (prop, string)
3178 Lisp_Object prop, string;
3179 {
3180 if (EQ (string, prop))
3181 return 1;
3182
3183 /* Skip over `when FORM'. */
3184 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3185 {
3186 prop = XCDR (prop);
3187 if (!CONSP (prop))
3188 return 0;
3189 prop = XCDR (prop);
3190 }
3191
3192 if (CONSP (prop))
3193 /* Skip over `margin LOCATION'. */
3194 if (EQ (XCAR (prop), Qmargin))
3195 {
3196 prop = XCDR (prop);
3197 if (!CONSP (prop))
3198 return 0;
3199
3200 prop = XCDR (prop);
3201 if (!CONSP (prop))
3202 return 0;
3203 }
3204
3205 return CONSP (prop) && EQ (XCAR (prop), string);
3206 }
3207
3208
3209 /* Return 1 if STRING appears in the `display' property PROP. */
3210
3211 static int
3212 display_prop_string_p (prop, string)
3213 Lisp_Object prop, string;
3214 {
3215 if (CONSP (prop)
3216 && CONSP (XCAR (prop))
3217 && !EQ (Qmargin, XCAR (XCAR (prop))))
3218 {
3219 /* A list of sub-properties. */
3220 while (CONSP (prop))
3221 {
3222 if (single_display_prop_string_p (XCAR (prop), string))
3223 return 1;
3224 prop = XCDR (prop);
3225 }
3226 }
3227 else if (VECTORP (prop))
3228 {
3229 /* A vector of sub-properties. */
3230 int i;
3231 for (i = 0; i < ASIZE (prop); ++i)
3232 if (single_display_prop_string_p (AREF (prop, i), string))
3233 return 1;
3234 }
3235 else
3236 return single_display_prop_string_p (prop, string);
3237
3238 return 0;
3239 }
3240
3241
3242 /* Determine from which buffer position in W's buffer STRING comes
3243 from. AROUND_CHARPOS is an approximate position where it could
3244 be from. Value is the buffer position or 0 if it couldn't be
3245 determined.
3246
3247 W's buffer must be current.
3248
3249 This function is necessary because we don't record buffer positions
3250 in glyphs generated from strings (to keep struct glyph small).
3251 This function may only use code that doesn't eval because it is
3252 called asynchronously from note_mouse_highlight. */
3253
3254 int
3255 string_buffer_position (w, string, around_charpos)
3256 struct window *w;
3257 Lisp_Object string;
3258 int around_charpos;
3259 {
3260 Lisp_Object limit, prop, pos;
3261 const int MAX_DISTANCE = 1000;
3262 int found = 0;
3263
3264 pos = make_number (around_charpos);
3265 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
3266 while (!found && !EQ (pos, limit))
3267 {
3268 prop = Fget_char_property (pos, Qdisplay, Qnil);
3269 if (!NILP (prop) && display_prop_string_p (prop, string))
3270 found = 1;
3271 else
3272 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
3273 }
3274
3275 if (!found)
3276 {
3277 pos = make_number (around_charpos);
3278 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
3279 while (!found && !EQ (pos, limit))
3280 {
3281 prop = Fget_char_property (pos, Qdisplay, Qnil);
3282 if (!NILP (prop) && display_prop_string_p (prop, string))
3283 found = 1;
3284 else
3285 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
3286 limit);
3287 }
3288 }
3289
3290 return found ? XINT (pos) : 0;
3291 }
3292
3293
3294 \f
3295 /***********************************************************************
3296 `composition' property
3297 ***********************************************************************/
3298
3299 /* Set up iterator IT from `composition' property at its current
3300 position. Called from handle_stop. */
3301
3302 static enum prop_handled
3303 handle_composition_prop (it)
3304 struct it *it;
3305 {
3306 Lisp_Object prop, string;
3307 int pos, pos_byte, end;
3308 enum prop_handled handled = HANDLED_NORMALLY;
3309
3310 if (STRINGP (it->string))
3311 {
3312 pos = IT_STRING_CHARPOS (*it);
3313 pos_byte = IT_STRING_BYTEPOS (*it);
3314 string = it->string;
3315 }
3316 else
3317 {
3318 pos = IT_CHARPOS (*it);
3319 pos_byte = IT_BYTEPOS (*it);
3320 string = Qnil;
3321 }
3322
3323 /* If there's a valid composition and point is not inside of the
3324 composition (in the case that the composition is from the current
3325 buffer), draw a glyph composed from the composition components. */
3326 if (find_composition (pos, -1, &pos, &end, &prop, string)
3327 && COMPOSITION_VALID_P (pos, end, prop)
3328 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
3329 {
3330 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
3331
3332 if (id >= 0)
3333 {
3334 it->method = next_element_from_composition;
3335 it->cmp_id = id;
3336 it->cmp_len = COMPOSITION_LENGTH (prop);
3337 /* For a terminal, draw only the first character of the
3338 components. */
3339 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
3340 it->len = (STRINGP (it->string)
3341 ? string_char_to_byte (it->string, end)
3342 : CHAR_TO_BYTE (end)) - pos_byte;
3343 it->stop_charpos = end;
3344 handled = HANDLED_RETURN;
3345 }
3346 }
3347
3348 return handled;
3349 }
3350
3351
3352 \f
3353 /***********************************************************************
3354 Overlay strings
3355 ***********************************************************************/
3356
3357 /* The following structure is used to record overlay strings for
3358 later sorting in load_overlay_strings. */
3359
3360 struct overlay_entry
3361 {
3362 Lisp_Object overlay;
3363 Lisp_Object string;
3364 int priority;
3365 int after_string_p;
3366 };
3367
3368
3369 /* Set up iterator IT from overlay strings at its current position.
3370 Called from handle_stop. */
3371
3372 static enum prop_handled
3373 handle_overlay_change (it)
3374 struct it *it;
3375 {
3376 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
3377 return HANDLED_RECOMPUTE_PROPS;
3378 else
3379 return HANDLED_NORMALLY;
3380 }
3381
3382
3383 /* Set up the next overlay string for delivery by IT, if there is an
3384 overlay string to deliver. Called by set_iterator_to_next when the
3385 end of the current overlay string is reached. If there are more
3386 overlay strings to display, IT->string and
3387 IT->current.overlay_string_index are set appropriately here.
3388 Otherwise IT->string is set to nil. */
3389
3390 static void
3391 next_overlay_string (it)
3392 struct it *it;
3393 {
3394 ++it->current.overlay_string_index;
3395 if (it->current.overlay_string_index == it->n_overlay_strings)
3396 {
3397 /* No more overlay strings. Restore IT's settings to what
3398 they were before overlay strings were processed, and
3399 continue to deliver from current_buffer. */
3400 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
3401
3402 pop_it (it);
3403 xassert (it->stop_charpos >= BEGV
3404 && it->stop_charpos <= it->end_charpos);
3405 it->string = Qnil;
3406 it->current.overlay_string_index = -1;
3407 SET_TEXT_POS (it->current.string_pos, -1, -1);
3408 it->n_overlay_strings = 0;
3409 it->method = next_element_from_buffer;
3410
3411 /* If we're at the end of the buffer, record that we have
3412 processed the overlay strings there already, so that
3413 next_element_from_buffer doesn't try it again. */
3414 if (IT_CHARPOS (*it) >= it->end_charpos)
3415 it->overlay_strings_at_end_processed_p = 1;
3416
3417 /* If we have to display `...' for invisible text, set
3418 the iterator up for that. */
3419 if (display_ellipsis_p)
3420 setup_for_ellipsis (it);
3421 }
3422 else
3423 {
3424 /* There are more overlay strings to process. If
3425 IT->current.overlay_string_index has advanced to a position
3426 where we must load IT->overlay_strings with more strings, do
3427 it. */
3428 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
3429
3430 if (it->current.overlay_string_index && i == 0)
3431 load_overlay_strings (it, 0);
3432
3433 /* Initialize IT to deliver display elements from the overlay
3434 string. */
3435 it->string = it->overlay_strings[i];
3436 it->multibyte_p = STRING_MULTIBYTE (it->string);
3437 SET_TEXT_POS (it->current.string_pos, 0, 0);
3438 it->method = next_element_from_string;
3439 it->stop_charpos = 0;
3440 }
3441
3442 CHECK_IT (it);
3443 }
3444
3445
3446 /* Compare two overlay_entry structures E1 and E2. Used as a
3447 comparison function for qsort in load_overlay_strings. Overlay
3448 strings for the same position are sorted so that
3449
3450 1. All after-strings come in front of before-strings, except
3451 when they come from the same overlay.
3452
3453 2. Within after-strings, strings are sorted so that overlay strings
3454 from overlays with higher priorities come first.
3455
3456 2. Within before-strings, strings are sorted so that overlay
3457 strings from overlays with higher priorities come last.
3458
3459 Value is analogous to strcmp. */
3460
3461
3462 static int
3463 compare_overlay_entries (e1, e2)
3464 void *e1, *e2;
3465 {
3466 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
3467 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
3468 int result;
3469
3470 if (entry1->after_string_p != entry2->after_string_p)
3471 {
3472 /* Let after-strings appear in front of before-strings if
3473 they come from different overlays. */
3474 if (EQ (entry1->overlay, entry2->overlay))
3475 result = entry1->after_string_p ? 1 : -1;
3476 else
3477 result = entry1->after_string_p ? -1 : 1;
3478 }
3479 else if (entry1->after_string_p)
3480 /* After-strings sorted in order of decreasing priority. */
3481 result = entry2->priority - entry1->priority;
3482 else
3483 /* Before-strings sorted in order of increasing priority. */
3484 result = entry1->priority - entry2->priority;
3485
3486 return result;
3487 }
3488
3489
3490 /* Load the vector IT->overlay_strings with overlay strings from IT's
3491 current buffer position, or from CHARPOS if that is > 0. Set
3492 IT->n_overlays to the total number of overlay strings found.
3493
3494 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
3495 a time. On entry into load_overlay_strings,
3496 IT->current.overlay_string_index gives the number of overlay
3497 strings that have already been loaded by previous calls to this
3498 function.
3499
3500 IT->add_overlay_start contains an additional overlay start
3501 position to consider for taking overlay strings from, if non-zero.
3502 This position comes into play when the overlay has an `invisible'
3503 property, and both before and after-strings. When we've skipped to
3504 the end of the overlay, because of its `invisible' property, we
3505 nevertheless want its before-string to appear.
3506 IT->add_overlay_start will contain the overlay start position
3507 in this case.
3508
3509 Overlay strings are sorted so that after-string strings come in
3510 front of before-string strings. Within before and after-strings,
3511 strings are sorted by overlay priority. See also function
3512 compare_overlay_entries. */
3513
3514 static void
3515 load_overlay_strings (it, charpos)
3516 struct it *it;
3517 int charpos;
3518 {
3519 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
3520 Lisp_Object ov, overlay, window, str, invisible;
3521 int start, end;
3522 int size = 20;
3523 int n = 0, i, j, invis_p;
3524 struct overlay_entry *entries
3525 = (struct overlay_entry *) alloca (size * sizeof *entries);
3526
3527 if (charpos <= 0)
3528 charpos = IT_CHARPOS (*it);
3529
3530 /* Append the overlay string STRING of overlay OVERLAY to vector
3531 `entries' which has size `size' and currently contains `n'
3532 elements. AFTER_P non-zero means STRING is an after-string of
3533 OVERLAY. */
3534 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
3535 do \
3536 { \
3537 Lisp_Object priority; \
3538 \
3539 if (n == size) \
3540 { \
3541 int new_size = 2 * size; \
3542 struct overlay_entry *old = entries; \
3543 entries = \
3544 (struct overlay_entry *) alloca (new_size \
3545 * sizeof *entries); \
3546 bcopy (old, entries, size * sizeof *entries); \
3547 size = new_size; \
3548 } \
3549 \
3550 entries[n].string = (STRING); \
3551 entries[n].overlay = (OVERLAY); \
3552 priority = Foverlay_get ((OVERLAY), Qpriority); \
3553 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
3554 entries[n].after_string_p = (AFTER_P); \
3555 ++n; \
3556 } \
3557 while (0)
3558
3559 /* Process overlay before the overlay center. */
3560 for (ov = current_buffer->overlays_before; CONSP (ov); ov = XCDR (ov))
3561 {
3562 overlay = XCAR (ov);
3563 xassert (OVERLAYP (overlay));
3564 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3565 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3566
3567 if (end < charpos)
3568 break;
3569
3570 /* Skip this overlay if it doesn't start or end at IT's current
3571 position. */
3572 if (end != charpos && start != charpos)
3573 continue;
3574
3575 /* Skip this overlay if it doesn't apply to IT->w. */
3576 window = Foverlay_get (overlay, Qwindow);
3577 if (WINDOWP (window) && XWINDOW (window) != it->w)
3578 continue;
3579
3580 /* If the text ``under'' the overlay is invisible, both before-
3581 and after-strings from this overlay are visible; start and
3582 end position are indistinguishable. */
3583 invisible = Foverlay_get (overlay, Qinvisible);
3584 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3585
3586 /* If overlay has a non-empty before-string, record it. */
3587 if ((start == charpos || (end == charpos && invis_p))
3588 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3589 && SCHARS (str))
3590 RECORD_OVERLAY_STRING (overlay, str, 0);
3591
3592 /* If overlay has a non-empty after-string, record it. */
3593 if ((end == charpos || (start == charpos && invis_p))
3594 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3595 && SCHARS (str))
3596 RECORD_OVERLAY_STRING (overlay, str, 1);
3597 }
3598
3599 /* Process overlays after the overlay center. */
3600 for (ov = current_buffer->overlays_after; CONSP (ov); ov = XCDR (ov))
3601 {
3602 overlay = XCAR (ov);
3603 xassert (OVERLAYP (overlay));
3604 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3605 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3606
3607 if (start > charpos)
3608 break;
3609
3610 /* Skip this overlay if it doesn't start or end at IT's current
3611 position. */
3612 if (end != charpos && start != charpos)
3613 continue;
3614
3615 /* Skip this overlay if it doesn't apply to IT->w. */
3616 window = Foverlay_get (overlay, Qwindow);
3617 if (WINDOWP (window) && XWINDOW (window) != it->w)
3618 continue;
3619
3620 /* If the text ``under'' the overlay is invisible, it has a zero
3621 dimension, and both before- and after-strings apply. */
3622 invisible = Foverlay_get (overlay, Qinvisible);
3623 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3624
3625 /* If overlay has a non-empty before-string, record it. */
3626 if ((start == charpos || (end == charpos && invis_p))
3627 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3628 && SCHARS (str))
3629 RECORD_OVERLAY_STRING (overlay, str, 0);
3630
3631 /* If overlay has a non-empty after-string, record it. */
3632 if ((end == charpos || (start == charpos && invis_p))
3633 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3634 && SCHARS (str))
3635 RECORD_OVERLAY_STRING (overlay, str, 1);
3636 }
3637
3638 #undef RECORD_OVERLAY_STRING
3639
3640 /* Sort entries. */
3641 if (n > 1)
3642 qsort (entries, n, sizeof *entries, compare_overlay_entries);
3643
3644 /* Record the total number of strings to process. */
3645 it->n_overlay_strings = n;
3646
3647 /* IT->current.overlay_string_index is the number of overlay strings
3648 that have already been consumed by IT. Copy some of the
3649 remaining overlay strings to IT->overlay_strings. */
3650 i = 0;
3651 j = it->current.overlay_string_index;
3652 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
3653 it->overlay_strings[i++] = entries[j++].string;
3654
3655 CHECK_IT (it);
3656 }
3657
3658
3659 /* Get the first chunk of overlay strings at IT's current buffer
3660 position, or at CHARPOS if that is > 0. Value is non-zero if at
3661 least one overlay string was found. */
3662
3663 static int
3664 get_overlay_strings (it, charpos)
3665 struct it *it;
3666 int charpos;
3667 {
3668 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
3669 process. This fills IT->overlay_strings with strings, and sets
3670 IT->n_overlay_strings to the total number of strings to process.
3671 IT->pos.overlay_string_index has to be set temporarily to zero
3672 because load_overlay_strings needs this; it must be set to -1
3673 when no overlay strings are found because a zero value would
3674 indicate a position in the first overlay string. */
3675 it->current.overlay_string_index = 0;
3676 load_overlay_strings (it, charpos);
3677
3678 /* If we found overlay strings, set up IT to deliver display
3679 elements from the first one. Otherwise set up IT to deliver
3680 from current_buffer. */
3681 if (it->n_overlay_strings)
3682 {
3683 /* Make sure we know settings in current_buffer, so that we can
3684 restore meaningful values when we're done with the overlay
3685 strings. */
3686 compute_stop_pos (it);
3687 xassert (it->face_id >= 0);
3688
3689 /* Save IT's settings. They are restored after all overlay
3690 strings have been processed. */
3691 xassert (it->sp == 0);
3692 push_it (it);
3693
3694 /* Set up IT to deliver display elements from the first overlay
3695 string. */
3696 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3697 it->string = it->overlay_strings[0];
3698 it->stop_charpos = 0;
3699 xassert (STRINGP (it->string));
3700 it->end_charpos = SCHARS (it->string);
3701 it->multibyte_p = STRING_MULTIBYTE (it->string);
3702 it->method = next_element_from_string;
3703 }
3704 else
3705 {
3706 it->string = Qnil;
3707 it->current.overlay_string_index = -1;
3708 it->method = next_element_from_buffer;
3709 }
3710
3711 CHECK_IT (it);
3712
3713 /* Value is non-zero if we found at least one overlay string. */
3714 return STRINGP (it->string);
3715 }
3716
3717
3718 \f
3719 /***********************************************************************
3720 Saving and restoring state
3721 ***********************************************************************/
3722
3723 /* Save current settings of IT on IT->stack. Called, for example,
3724 before setting up IT for an overlay string, to be able to restore
3725 IT's settings to what they were after the overlay string has been
3726 processed. */
3727
3728 static void
3729 push_it (it)
3730 struct it *it;
3731 {
3732 struct iterator_stack_entry *p;
3733
3734 xassert (it->sp < 2);
3735 p = it->stack + it->sp;
3736
3737 p->stop_charpos = it->stop_charpos;
3738 xassert (it->face_id >= 0);
3739 p->face_id = it->face_id;
3740 p->string = it->string;
3741 p->pos = it->current;
3742 p->end_charpos = it->end_charpos;
3743 p->string_nchars = it->string_nchars;
3744 p->area = it->area;
3745 p->multibyte_p = it->multibyte_p;
3746 p->space_width = it->space_width;
3747 p->font_height = it->font_height;
3748 p->voffset = it->voffset;
3749 p->string_from_display_prop_p = it->string_from_display_prop_p;
3750 p->display_ellipsis_p = 0;
3751 ++it->sp;
3752 }
3753
3754
3755 /* Restore IT's settings from IT->stack. Called, for example, when no
3756 more overlay strings must be processed, and we return to delivering
3757 display elements from a buffer, or when the end of a string from a
3758 `display' property is reached and we return to delivering display
3759 elements from an overlay string, or from a buffer. */
3760
3761 static void
3762 pop_it (it)
3763 struct it *it;
3764 {
3765 struct iterator_stack_entry *p;
3766
3767 xassert (it->sp > 0);
3768 --it->sp;
3769 p = it->stack + it->sp;
3770 it->stop_charpos = p->stop_charpos;
3771 it->face_id = p->face_id;
3772 it->string = p->string;
3773 it->current = p->pos;
3774 it->end_charpos = p->end_charpos;
3775 it->string_nchars = p->string_nchars;
3776 it->area = p->area;
3777 it->multibyte_p = p->multibyte_p;
3778 it->space_width = p->space_width;
3779 it->font_height = p->font_height;
3780 it->voffset = p->voffset;
3781 it->string_from_display_prop_p = p->string_from_display_prop_p;
3782 }
3783
3784
3785 \f
3786 /***********************************************************************
3787 Moving over lines
3788 ***********************************************************************/
3789
3790 /* Set IT's current position to the previous line start. */
3791
3792 static void
3793 back_to_previous_line_start (it)
3794 struct it *it;
3795 {
3796 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
3797 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3798 }
3799
3800
3801 /* Move IT to the next line start.
3802
3803 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
3804 we skipped over part of the text (as opposed to moving the iterator
3805 continuously over the text). Otherwise, don't change the value
3806 of *SKIPPED_P.
3807
3808 Newlines may come from buffer text, overlay strings, or strings
3809 displayed via the `display' property. That's the reason we can't
3810 simply use find_next_newline_no_quit.
3811
3812 Note that this function may not skip over invisible text that is so
3813 because of text properties and immediately follows a newline. If
3814 it would, function reseat_at_next_visible_line_start, when called
3815 from set_iterator_to_next, would effectively make invisible
3816 characters following a newline part of the wrong glyph row, which
3817 leads to wrong cursor motion. */
3818
3819 static int
3820 forward_to_next_line_start (it, skipped_p)
3821 struct it *it;
3822 int *skipped_p;
3823 {
3824 int old_selective, newline_found_p, n;
3825 const int MAX_NEWLINE_DISTANCE = 500;
3826
3827 /* If already on a newline, just consume it to avoid unintended
3828 skipping over invisible text below. */
3829 if (it->what == IT_CHARACTER
3830 && it->c == '\n'
3831 && CHARPOS (it->position) == IT_CHARPOS (*it))
3832 {
3833 set_iterator_to_next (it, 0);
3834 it->c = 0;
3835 return 1;
3836 }
3837
3838 /* Don't handle selective display in the following. It's (a)
3839 unnecessary because it's done by the caller, and (b) leads to an
3840 infinite recursion because next_element_from_ellipsis indirectly
3841 calls this function. */
3842 old_selective = it->selective;
3843 it->selective = 0;
3844
3845 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
3846 from buffer text. */
3847 for (n = newline_found_p = 0;
3848 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
3849 n += STRINGP (it->string) ? 0 : 1)
3850 {
3851 if (!get_next_display_element (it))
3852 break;
3853 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
3854 set_iterator_to_next (it, 0);
3855 }
3856
3857 /* If we didn't find a newline near enough, see if we can use a
3858 short-cut. */
3859 if (n == MAX_NEWLINE_DISTANCE)
3860 {
3861 int start = IT_CHARPOS (*it);
3862 int limit = find_next_newline_no_quit (start, 1);
3863 Lisp_Object pos;
3864
3865 xassert (!STRINGP (it->string));
3866
3867 /* If there isn't any `display' property in sight, and no
3868 overlays, we can just use the position of the newline in
3869 buffer text. */
3870 if (it->stop_charpos >= limit
3871 || ((pos = Fnext_single_property_change (make_number (start),
3872 Qdisplay,
3873 Qnil, make_number (limit)),
3874 NILP (pos))
3875 && next_overlay_change (start) == ZV))
3876 {
3877 IT_CHARPOS (*it) = limit;
3878 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
3879 *skipped_p = newline_found_p = 1;
3880 }
3881 else
3882 {
3883 while (get_next_display_element (it)
3884 && !newline_found_p)
3885 {
3886 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
3887 set_iterator_to_next (it, 0);
3888 }
3889 }
3890 }
3891
3892 it->selective = old_selective;
3893 return newline_found_p;
3894 }
3895
3896
3897 /* Set IT's current position to the previous visible line start. Skip
3898 invisible text that is so either due to text properties or due to
3899 selective display. Caution: this does not change IT->current_x and
3900 IT->hpos. */
3901
3902 static void
3903 back_to_previous_visible_line_start (it)
3904 struct it *it;
3905 {
3906 int visible_p = 0;
3907
3908 /* Go back one newline if not on BEGV already. */
3909 if (IT_CHARPOS (*it) > BEGV)
3910 back_to_previous_line_start (it);
3911
3912 /* Move over lines that are invisible because of selective display
3913 or text properties. */
3914 while (IT_CHARPOS (*it) > BEGV
3915 && !visible_p)
3916 {
3917 visible_p = 1;
3918
3919 /* If selective > 0, then lines indented more than that values
3920 are invisible. */
3921 if (it->selective > 0
3922 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3923 (double) it->selective)) /* iftc */
3924 visible_p = 0;
3925 else
3926 {
3927 Lisp_Object prop;
3928
3929 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
3930 Qinvisible, it->window);
3931 if (TEXT_PROP_MEANS_INVISIBLE (prop))
3932 visible_p = 0;
3933 }
3934
3935 /* Back one more newline if the current one is invisible. */
3936 if (!visible_p)
3937 back_to_previous_line_start (it);
3938 }
3939
3940 xassert (IT_CHARPOS (*it) >= BEGV);
3941 xassert (IT_CHARPOS (*it) == BEGV
3942 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3943 CHECK_IT (it);
3944 }
3945
3946
3947 /* Reseat iterator IT at the previous visible line start. Skip
3948 invisible text that is so either due to text properties or due to
3949 selective display. At the end, update IT's overlay information,
3950 face information etc. */
3951
3952 static void
3953 reseat_at_previous_visible_line_start (it)
3954 struct it *it;
3955 {
3956 back_to_previous_visible_line_start (it);
3957 reseat (it, it->current.pos, 1);
3958 CHECK_IT (it);
3959 }
3960
3961
3962 /* Reseat iterator IT on the next visible line start in the current
3963 buffer. ON_NEWLINE_P non-zero means position IT on the newline
3964 preceding the line start. Skip over invisible text that is so
3965 because of selective display. Compute faces, overlays etc at the
3966 new position. Note that this function does not skip over text that
3967 is invisible because of text properties. */
3968
3969 static void
3970 reseat_at_next_visible_line_start (it, on_newline_p)
3971 struct it *it;
3972 int on_newline_p;
3973 {
3974 int newline_found_p, skipped_p = 0;
3975
3976 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3977
3978 /* Skip over lines that are invisible because they are indented
3979 more than the value of IT->selective. */
3980 if (it->selective > 0)
3981 while (IT_CHARPOS (*it) < ZV
3982 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3983 (double) it->selective)) /* iftc */
3984 {
3985 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3986 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3987 }
3988
3989 /* Position on the newline if that's what's requested. */
3990 if (on_newline_p && newline_found_p)
3991 {
3992 if (STRINGP (it->string))
3993 {
3994 if (IT_STRING_CHARPOS (*it) > 0)
3995 {
3996 --IT_STRING_CHARPOS (*it);
3997 --IT_STRING_BYTEPOS (*it);
3998 }
3999 }
4000 else if (IT_CHARPOS (*it) > BEGV)
4001 {
4002 --IT_CHARPOS (*it);
4003 --IT_BYTEPOS (*it);
4004 reseat (it, it->current.pos, 0);
4005 }
4006 }
4007 else if (skipped_p)
4008 reseat (it, it->current.pos, 0);
4009
4010 CHECK_IT (it);
4011 }
4012
4013
4014 \f
4015 /***********************************************************************
4016 Changing an iterator's position
4017 ***********************************************************************/
4018
4019 /* Change IT's current position to POS in current_buffer. If FORCE_P
4020 is non-zero, always check for text properties at the new position.
4021 Otherwise, text properties are only looked up if POS >=
4022 IT->check_charpos of a property. */
4023
4024 static void
4025 reseat (it, pos, force_p)
4026 struct it *it;
4027 struct text_pos pos;
4028 int force_p;
4029 {
4030 int original_pos = IT_CHARPOS (*it);
4031
4032 reseat_1 (it, pos, 0);
4033
4034 /* Determine where to check text properties. Avoid doing it
4035 where possible because text property lookup is very expensive. */
4036 if (force_p
4037 || CHARPOS (pos) > it->stop_charpos
4038 || CHARPOS (pos) < original_pos)
4039 handle_stop (it);
4040
4041 CHECK_IT (it);
4042 }
4043
4044
4045 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4046 IT->stop_pos to POS, also. */
4047
4048 static void
4049 reseat_1 (it, pos, set_stop_p)
4050 struct it *it;
4051 struct text_pos pos;
4052 int set_stop_p;
4053 {
4054 /* Don't call this function when scanning a C string. */
4055 xassert (it->s == NULL);
4056
4057 /* POS must be a reasonable value. */
4058 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4059
4060 it->current.pos = it->position = pos;
4061 XSETBUFFER (it->object, current_buffer);
4062 it->end_charpos = ZV;
4063 it->dpvec = NULL;
4064 it->current.dpvec_index = -1;
4065 it->current.overlay_string_index = -1;
4066 IT_STRING_CHARPOS (*it) = -1;
4067 IT_STRING_BYTEPOS (*it) = -1;
4068 it->string = Qnil;
4069 it->method = next_element_from_buffer;
4070 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
4071 it->sp = 0;
4072 it->face_before_selective_p = 0;
4073
4074 if (set_stop_p)
4075 it->stop_charpos = CHARPOS (pos);
4076 }
4077
4078
4079 /* Set up IT for displaying a string, starting at CHARPOS in window W.
4080 If S is non-null, it is a C string to iterate over. Otherwise,
4081 STRING gives a Lisp string to iterate over.
4082
4083 If PRECISION > 0, don't return more then PRECISION number of
4084 characters from the string.
4085
4086 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4087 characters have been returned. FIELD_WIDTH < 0 means an infinite
4088 field width.
4089
4090 MULTIBYTE = 0 means disable processing of multibyte characters,
4091 MULTIBYTE > 0 means enable it,
4092 MULTIBYTE < 0 means use IT->multibyte_p.
4093
4094 IT must be initialized via a prior call to init_iterator before
4095 calling this function. */
4096
4097 static void
4098 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4099 struct it *it;
4100 unsigned char *s;
4101 Lisp_Object string;
4102 int charpos;
4103 int precision, field_width, multibyte;
4104 {
4105 /* No region in strings. */
4106 it->region_beg_charpos = it->region_end_charpos = -1;
4107
4108 /* No text property checks performed by default, but see below. */
4109 it->stop_charpos = -1;
4110
4111 /* Set iterator position and end position. */
4112 bzero (&it->current, sizeof it->current);
4113 it->current.overlay_string_index = -1;
4114 it->current.dpvec_index = -1;
4115 xassert (charpos >= 0);
4116
4117 /* If STRING is specified, use its multibyteness, otherwise use the
4118 setting of MULTIBYTE, if specified. */
4119 if (multibyte >= 0)
4120 it->multibyte_p = multibyte > 0;
4121
4122 if (s == NULL)
4123 {
4124 xassert (STRINGP (string));
4125 it->string = string;
4126 it->s = NULL;
4127 it->end_charpos = it->string_nchars = SCHARS (string);
4128 it->method = next_element_from_string;
4129 it->current.string_pos = string_pos (charpos, string);
4130 }
4131 else
4132 {
4133 it->s = s;
4134 it->string = Qnil;
4135
4136 /* Note that we use IT->current.pos, not it->current.string_pos,
4137 for displaying C strings. */
4138 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4139 if (it->multibyte_p)
4140 {
4141 it->current.pos = c_string_pos (charpos, s, 1);
4142 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4143 }
4144 else
4145 {
4146 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4147 it->end_charpos = it->string_nchars = strlen (s);
4148 }
4149
4150 it->method = next_element_from_c_string;
4151 }
4152
4153 /* PRECISION > 0 means don't return more than PRECISION characters
4154 from the string. */
4155 if (precision > 0 && it->end_charpos - charpos > precision)
4156 it->end_charpos = it->string_nchars = charpos + precision;
4157
4158 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4159 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4160 FIELD_WIDTH < 0 means infinite field width. This is useful for
4161 padding with `-' at the end of a mode line. */
4162 if (field_width < 0)
4163 field_width = INFINITY;
4164 if (field_width > it->end_charpos - charpos)
4165 it->end_charpos = charpos + field_width;
4166
4167 /* Use the standard display table for displaying strings. */
4168 if (DISP_TABLE_P (Vstandard_display_table))
4169 it->dp = XCHAR_TABLE (Vstandard_display_table);
4170
4171 it->stop_charpos = charpos;
4172 CHECK_IT (it);
4173 }
4174
4175
4176 \f
4177 /***********************************************************************
4178 Iteration
4179 ***********************************************************************/
4180
4181 /* Load IT's display element fields with information about the next
4182 display element from the current position of IT. Value is zero if
4183 end of buffer (or C string) is reached. */
4184
4185 int
4186 get_next_display_element (it)
4187 struct it *it;
4188 {
4189 /* Non-zero means that we found an display element. Zero means that
4190 we hit the end of what we iterate over. Performance note: the
4191 function pointer `method' used here turns out to be faster than
4192 using a sequence of if-statements. */
4193 int success_p = (*it->method) (it);
4194
4195 if (it->what == IT_CHARACTER)
4196 {
4197 /* Map via display table or translate control characters.
4198 IT->c, IT->len etc. have been set to the next character by
4199 the function call above. If we have a display table, and it
4200 contains an entry for IT->c, translate it. Don't do this if
4201 IT->c itself comes from a display table, otherwise we could
4202 end up in an infinite recursion. (An alternative could be to
4203 count the recursion depth of this function and signal an
4204 error when a certain maximum depth is reached.) Is it worth
4205 it? */
4206 if (success_p && it->dpvec == NULL)
4207 {
4208 Lisp_Object dv;
4209
4210 if (it->dp
4211 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
4212 VECTORP (dv)))
4213 {
4214 struct Lisp_Vector *v = XVECTOR (dv);
4215
4216 /* Return the first character from the display table
4217 entry, if not empty. If empty, don't display the
4218 current character. */
4219 if (v->size)
4220 {
4221 it->dpvec_char_len = it->len;
4222 it->dpvec = v->contents;
4223 it->dpend = v->contents + v->size;
4224 it->current.dpvec_index = 0;
4225 it->method = next_element_from_display_vector;
4226 success_p = get_next_display_element (it);
4227 }
4228 else
4229 {
4230 set_iterator_to_next (it, 0);
4231 success_p = get_next_display_element (it);
4232 }
4233 }
4234
4235 /* Translate control characters into `\003' or `^C' form.
4236 Control characters coming from a display table entry are
4237 currently not translated because we use IT->dpvec to hold
4238 the translation. This could easily be changed but I
4239 don't believe that it is worth doing.
4240
4241 Non-printable multibyte characters are also translated
4242 octal form. */
4243 else if ((it->c < ' '
4244 && (it->area != TEXT_AREA
4245 || (it->c != '\n' && it->c != '\t')))
4246 || (it->c >= 127
4247 && it->len == 1)
4248 || !CHAR_PRINTABLE_P (it->c))
4249 {
4250 /* IT->c is a control character which must be displayed
4251 either as '\003' or as `^C' where the '\\' and '^'
4252 can be defined in the display table. Fill
4253 IT->ctl_chars with glyphs for what we have to
4254 display. Then, set IT->dpvec to these glyphs. */
4255 GLYPH g;
4256
4257 if (it->c < 128 && it->ctl_arrow_p)
4258 {
4259 /* Set IT->ctl_chars[0] to the glyph for `^'. */
4260 if (it->dp
4261 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
4262 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
4263 g = XINT (DISP_CTRL_GLYPH (it->dp));
4264 else
4265 g = FAST_MAKE_GLYPH ('^', 0);
4266 XSETINT (it->ctl_chars[0], g);
4267
4268 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
4269 XSETINT (it->ctl_chars[1], g);
4270
4271 /* Set up IT->dpvec and return first character from it. */
4272 it->dpvec_char_len = it->len;
4273 it->dpvec = it->ctl_chars;
4274 it->dpend = it->dpvec + 2;
4275 it->current.dpvec_index = 0;
4276 it->method = next_element_from_display_vector;
4277 get_next_display_element (it);
4278 }
4279 else
4280 {
4281 unsigned char str[MAX_MULTIBYTE_LENGTH];
4282 int len;
4283 int i;
4284 GLYPH escape_glyph;
4285
4286 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
4287 if (it->dp
4288 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
4289 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
4290 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
4291 else
4292 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
4293
4294 if (SINGLE_BYTE_CHAR_P (it->c))
4295 str[0] = it->c, len = 1;
4296 else
4297 {
4298 len = CHAR_STRING_NO_SIGNAL (it->c, str);
4299 if (len < 0)
4300 {
4301 /* It's an invalid character, which
4302 shouldn't happen actually, but due to
4303 bugs it may happen. Let's print the char
4304 as is, there's not much meaningful we can
4305 do with it. */
4306 str[0] = it->c;
4307 str[1] = it->c >> 8;
4308 str[2] = it->c >> 16;
4309 str[3] = it->c >> 24;
4310 len = 4;
4311 }
4312 }
4313
4314 for (i = 0; i < len; i++)
4315 {
4316 XSETINT (it->ctl_chars[i * 4], escape_glyph);
4317 /* Insert three more glyphs into IT->ctl_chars for
4318 the octal display of the character. */
4319 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
4320 XSETINT (it->ctl_chars[i * 4 + 1], g);
4321 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
4322 XSETINT (it->ctl_chars[i * 4 + 2], g);
4323 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
4324 XSETINT (it->ctl_chars[i * 4 + 3], g);
4325 }
4326
4327 /* Set up IT->dpvec and return the first character
4328 from it. */
4329 it->dpvec_char_len = it->len;
4330 it->dpvec = it->ctl_chars;
4331 it->dpend = it->dpvec + len * 4;
4332 it->current.dpvec_index = 0;
4333 it->method = next_element_from_display_vector;
4334 get_next_display_element (it);
4335 }
4336 }
4337 }
4338
4339 /* Adjust face id for a multibyte character. There are no
4340 multibyte character in unibyte text. */
4341 if (it->multibyte_p
4342 && success_p
4343 && FRAME_WINDOW_P (it->f))
4344 {
4345 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4346 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
4347 }
4348 }
4349
4350 /* Is this character the last one of a run of characters with
4351 box? If yes, set IT->end_of_box_run_p to 1. */
4352 if (it->face_box_p
4353 && it->s == NULL)
4354 {
4355 int face_id;
4356 struct face *face;
4357
4358 it->end_of_box_run_p
4359 = ((face_id = face_after_it_pos (it),
4360 face_id != it->face_id)
4361 && (face = FACE_FROM_ID (it->f, face_id),
4362 face->box == FACE_NO_BOX));
4363 }
4364
4365 /* Value is 0 if end of buffer or string reached. */
4366 return success_p;
4367 }
4368
4369
4370 /* Move IT to the next display element.
4371
4372 RESEAT_P non-zero means if called on a newline in buffer text,
4373 skip to the next visible line start.
4374
4375 Functions get_next_display_element and set_iterator_to_next are
4376 separate because I find this arrangement easier to handle than a
4377 get_next_display_element function that also increments IT's
4378 position. The way it is we can first look at an iterator's current
4379 display element, decide whether it fits on a line, and if it does,
4380 increment the iterator position. The other way around we probably
4381 would either need a flag indicating whether the iterator has to be
4382 incremented the next time, or we would have to implement a
4383 decrement position function which would not be easy to write. */
4384
4385 void
4386 set_iterator_to_next (it, reseat_p)
4387 struct it *it;
4388 int reseat_p;
4389 {
4390 /* Reset flags indicating start and end of a sequence of characters
4391 with box. Reset them at the start of this function because
4392 moving the iterator to a new position might set them. */
4393 it->start_of_box_run_p = it->end_of_box_run_p = 0;
4394
4395 if (it->method == next_element_from_buffer)
4396 {
4397 /* The current display element of IT is a character from
4398 current_buffer. Advance in the buffer, and maybe skip over
4399 invisible lines that are so because of selective display. */
4400 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
4401 reseat_at_next_visible_line_start (it, 0);
4402 else
4403 {
4404 xassert (it->len != 0);
4405 IT_BYTEPOS (*it) += it->len;
4406 IT_CHARPOS (*it) += 1;
4407 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
4408 }
4409 }
4410 else if (it->method == next_element_from_composition)
4411 {
4412 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
4413 if (STRINGP (it->string))
4414 {
4415 IT_STRING_BYTEPOS (*it) += it->len;
4416 IT_STRING_CHARPOS (*it) += it->cmp_len;
4417 it->method = next_element_from_string;
4418 goto consider_string_end;
4419 }
4420 else
4421 {
4422 IT_BYTEPOS (*it) += it->len;
4423 IT_CHARPOS (*it) += it->cmp_len;
4424 it->method = next_element_from_buffer;
4425 }
4426 }
4427 else if (it->method == next_element_from_c_string)
4428 {
4429 /* Current display element of IT is from a C string. */
4430 IT_BYTEPOS (*it) += it->len;
4431 IT_CHARPOS (*it) += 1;
4432 }
4433 else if (it->method == next_element_from_display_vector)
4434 {
4435 /* Current display element of IT is from a display table entry.
4436 Advance in the display table definition. Reset it to null if
4437 end reached, and continue with characters from buffers/
4438 strings. */
4439 ++it->current.dpvec_index;
4440
4441 /* Restore face of the iterator to what they were before the
4442 display vector entry (these entries may contain faces). */
4443 it->face_id = it->saved_face_id;
4444
4445 if (it->dpvec + it->current.dpvec_index == it->dpend)
4446 {
4447 if (it->s)
4448 it->method = next_element_from_c_string;
4449 else if (STRINGP (it->string))
4450 it->method = next_element_from_string;
4451 else
4452 it->method = next_element_from_buffer;
4453
4454 it->dpvec = NULL;
4455 it->current.dpvec_index = -1;
4456
4457 /* Skip over characters which were displayed via IT->dpvec. */
4458 if (it->dpvec_char_len < 0)
4459 reseat_at_next_visible_line_start (it, 1);
4460 else if (it->dpvec_char_len > 0)
4461 {
4462 it->len = it->dpvec_char_len;
4463 set_iterator_to_next (it, reseat_p);
4464 }
4465 }
4466 }
4467 else if (it->method == next_element_from_string)
4468 {
4469 /* Current display element is a character from a Lisp string. */
4470 xassert (it->s == NULL && STRINGP (it->string));
4471 IT_STRING_BYTEPOS (*it) += it->len;
4472 IT_STRING_CHARPOS (*it) += 1;
4473
4474 consider_string_end:
4475
4476 if (it->current.overlay_string_index >= 0)
4477 {
4478 /* IT->string is an overlay string. Advance to the
4479 next, if there is one. */
4480 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
4481 next_overlay_string (it);
4482 }
4483 else
4484 {
4485 /* IT->string is not an overlay string. If we reached
4486 its end, and there is something on IT->stack, proceed
4487 with what is on the stack. This can be either another
4488 string, this time an overlay string, or a buffer. */
4489 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
4490 && it->sp > 0)
4491 {
4492 pop_it (it);
4493 if (!STRINGP (it->string))
4494 it->method = next_element_from_buffer;
4495 else
4496 goto consider_string_end;
4497 }
4498 }
4499 }
4500 else if (it->method == next_element_from_image
4501 || it->method == next_element_from_stretch)
4502 {
4503 /* The position etc with which we have to proceed are on
4504 the stack. The position may be at the end of a string,
4505 if the `display' property takes up the whole string. */
4506 pop_it (it);
4507 it->image_id = 0;
4508 if (STRINGP (it->string))
4509 {
4510 it->method = next_element_from_string;
4511 goto consider_string_end;
4512 }
4513 else
4514 it->method = next_element_from_buffer;
4515 }
4516 else
4517 /* There are no other methods defined, so this should be a bug. */
4518 abort ();
4519
4520 xassert (it->method != next_element_from_string
4521 || (STRINGP (it->string)
4522 && IT_STRING_CHARPOS (*it) >= 0));
4523 }
4524
4525
4526 /* Load IT's display element fields with information about the next
4527 display element which comes from a display table entry or from the
4528 result of translating a control character to one of the forms `^C'
4529 or `\003'. IT->dpvec holds the glyphs to return as characters. */
4530
4531 static int
4532 next_element_from_display_vector (it)
4533 struct it *it;
4534 {
4535 /* Precondition. */
4536 xassert (it->dpvec && it->current.dpvec_index >= 0);
4537
4538 /* Remember the current face id in case glyphs specify faces.
4539 IT's face is restored in set_iterator_to_next. */
4540 it->saved_face_id = it->face_id;
4541
4542 if (INTEGERP (*it->dpvec)
4543 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
4544 {
4545 int lface_id;
4546 GLYPH g;
4547
4548 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
4549 it->c = FAST_GLYPH_CHAR (g);
4550 it->len = CHAR_BYTES (it->c);
4551
4552 /* The entry may contain a face id to use. Such a face id is
4553 the id of a Lisp face, not a realized face. A face id of
4554 zero means no face is specified. */
4555 lface_id = FAST_GLYPH_FACE (g);
4556 if (lface_id)
4557 {
4558 /* The function returns -1 if lface_id is invalid. */
4559 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
4560 if (face_id >= 0)
4561 it->face_id = face_id;
4562 }
4563 }
4564 else
4565 /* Display table entry is invalid. Return a space. */
4566 it->c = ' ', it->len = 1;
4567
4568 /* Don't change position and object of the iterator here. They are
4569 still the values of the character that had this display table
4570 entry or was translated, and that's what we want. */
4571 it->what = IT_CHARACTER;
4572 return 1;
4573 }
4574
4575
4576 /* Load IT with the next display element from Lisp string IT->string.
4577 IT->current.string_pos is the current position within the string.
4578 If IT->current.overlay_string_index >= 0, the Lisp string is an
4579 overlay string. */
4580
4581 static int
4582 next_element_from_string (it)
4583 struct it *it;
4584 {
4585 struct text_pos position;
4586
4587 xassert (STRINGP (it->string));
4588 xassert (IT_STRING_CHARPOS (*it) >= 0);
4589 position = it->current.string_pos;
4590
4591 /* Time to check for invisible text? */
4592 if (IT_STRING_CHARPOS (*it) < it->end_charpos
4593 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
4594 {
4595 handle_stop (it);
4596
4597 /* Since a handler may have changed IT->method, we must
4598 recurse here. */
4599 return get_next_display_element (it);
4600 }
4601
4602 if (it->current.overlay_string_index >= 0)
4603 {
4604 /* Get the next character from an overlay string. In overlay
4605 strings, There is no field width or padding with spaces to
4606 do. */
4607 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
4608 {
4609 it->what = IT_EOB;
4610 return 0;
4611 }
4612 else if (STRING_MULTIBYTE (it->string))
4613 {
4614 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
4615 const unsigned char *s = (SDATA (it->string)
4616 + IT_STRING_BYTEPOS (*it));
4617 it->c = string_char_and_length (s, remaining, &it->len);
4618 }
4619 else
4620 {
4621 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
4622 it->len = 1;
4623 }
4624 }
4625 else
4626 {
4627 /* Get the next character from a Lisp string that is not an
4628 overlay string. Such strings come from the mode line, for
4629 example. We may have to pad with spaces, or truncate the
4630 string. See also next_element_from_c_string. */
4631 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
4632 {
4633 it->what = IT_EOB;
4634 return 0;
4635 }
4636 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
4637 {
4638 /* Pad with spaces. */
4639 it->c = ' ', it->len = 1;
4640 CHARPOS (position) = BYTEPOS (position) = -1;
4641 }
4642 else if (STRING_MULTIBYTE (it->string))
4643 {
4644 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
4645 const unsigned char *s = (SDATA (it->string)
4646 + IT_STRING_BYTEPOS (*it));
4647 it->c = string_char_and_length (s, maxlen, &it->len);
4648 }
4649 else
4650 {
4651 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
4652 it->len = 1;
4653 }
4654 }
4655
4656 /* Record what we have and where it came from. Note that we store a
4657 buffer position in IT->position although it could arguably be a
4658 string position. */
4659 it->what = IT_CHARACTER;
4660 it->object = it->string;
4661 it->position = position;
4662 return 1;
4663 }
4664
4665
4666 /* Load IT with next display element from C string IT->s.
4667 IT->string_nchars is the maximum number of characters to return
4668 from the string. IT->end_charpos may be greater than
4669 IT->string_nchars when this function is called, in which case we
4670 may have to return padding spaces. Value is zero if end of string
4671 reached, including padding spaces. */
4672
4673 static int
4674 next_element_from_c_string (it)
4675 struct it *it;
4676 {
4677 int success_p = 1;
4678
4679 xassert (it->s);
4680 it->what = IT_CHARACTER;
4681 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
4682 it->object = Qnil;
4683
4684 /* IT's position can be greater IT->string_nchars in case a field
4685 width or precision has been specified when the iterator was
4686 initialized. */
4687 if (IT_CHARPOS (*it) >= it->end_charpos)
4688 {
4689 /* End of the game. */
4690 it->what = IT_EOB;
4691 success_p = 0;
4692 }
4693 else if (IT_CHARPOS (*it) >= it->string_nchars)
4694 {
4695 /* Pad with spaces. */
4696 it->c = ' ', it->len = 1;
4697 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
4698 }
4699 else if (it->multibyte_p)
4700 {
4701 /* Implementation note: The calls to strlen apparently aren't a
4702 performance problem because there is no noticeable performance
4703 difference between Emacs running in unibyte or multibyte mode. */
4704 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
4705 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
4706 maxlen, &it->len);
4707 }
4708 else
4709 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
4710
4711 return success_p;
4712 }
4713
4714
4715 /* Set up IT to return characters from an ellipsis, if appropriate.
4716 The definition of the ellipsis glyphs may come from a display table
4717 entry. This function Fills IT with the first glyph from the
4718 ellipsis if an ellipsis is to be displayed. */
4719
4720 static int
4721 next_element_from_ellipsis (it)
4722 struct it *it;
4723 {
4724 if (it->selective_display_ellipsis_p)
4725 {
4726 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4727 {
4728 /* Use the display table definition for `...'. Invalid glyphs
4729 will be handled by the method returning elements from dpvec. */
4730 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4731 it->dpvec_char_len = it->len;
4732 it->dpvec = v->contents;
4733 it->dpend = v->contents + v->size;
4734 it->current.dpvec_index = 0;
4735 it->method = next_element_from_display_vector;
4736 }
4737 else
4738 {
4739 /* Use default `...' which is stored in default_invis_vector. */
4740 it->dpvec_char_len = it->len;
4741 it->dpvec = default_invis_vector;
4742 it->dpend = default_invis_vector + 3;
4743 it->current.dpvec_index = 0;
4744 it->method = next_element_from_display_vector;
4745 }
4746 }
4747 else
4748 {
4749 /* The face at the current position may be different from the
4750 face we find after the invisible text. Remember what it
4751 was in IT->saved_face_id, and signal that it's there by
4752 setting face_before_selective_p. */
4753 it->saved_face_id = it->face_id;
4754 it->method = next_element_from_buffer;
4755 reseat_at_next_visible_line_start (it, 1);
4756 it->face_before_selective_p = 1;
4757 }
4758
4759 return get_next_display_element (it);
4760 }
4761
4762
4763 /* Deliver an image display element. The iterator IT is already
4764 filled with image information (done in handle_display_prop). Value
4765 is always 1. */
4766
4767
4768 static int
4769 next_element_from_image (it)
4770 struct it *it;
4771 {
4772 it->what = IT_IMAGE;
4773 return 1;
4774 }
4775
4776
4777 /* Fill iterator IT with next display element from a stretch glyph
4778 property. IT->object is the value of the text property. Value is
4779 always 1. */
4780
4781 static int
4782 next_element_from_stretch (it)
4783 struct it *it;
4784 {
4785 it->what = IT_STRETCH;
4786 return 1;
4787 }
4788
4789
4790 /* Load IT with the next display element from current_buffer. Value
4791 is zero if end of buffer reached. IT->stop_charpos is the next
4792 position at which to stop and check for text properties or buffer
4793 end. */
4794
4795 static int
4796 next_element_from_buffer (it)
4797 struct it *it;
4798 {
4799 int success_p = 1;
4800
4801 /* Check this assumption, otherwise, we would never enter the
4802 if-statement, below. */
4803 xassert (IT_CHARPOS (*it) >= BEGV
4804 && IT_CHARPOS (*it) <= it->stop_charpos);
4805
4806 if (IT_CHARPOS (*it) >= it->stop_charpos)
4807 {
4808 if (IT_CHARPOS (*it) >= it->end_charpos)
4809 {
4810 int overlay_strings_follow_p;
4811
4812 /* End of the game, except when overlay strings follow that
4813 haven't been returned yet. */
4814 if (it->overlay_strings_at_end_processed_p)
4815 overlay_strings_follow_p = 0;
4816 else
4817 {
4818 it->overlay_strings_at_end_processed_p = 1;
4819 overlay_strings_follow_p = get_overlay_strings (it, 0);
4820 }
4821
4822 if (overlay_strings_follow_p)
4823 success_p = get_next_display_element (it);
4824 else
4825 {
4826 it->what = IT_EOB;
4827 it->position = it->current.pos;
4828 success_p = 0;
4829 }
4830 }
4831 else
4832 {
4833 handle_stop (it);
4834 return get_next_display_element (it);
4835 }
4836 }
4837 else
4838 {
4839 /* No face changes, overlays etc. in sight, so just return a
4840 character from current_buffer. */
4841 unsigned char *p;
4842
4843 /* Maybe run the redisplay end trigger hook. Performance note:
4844 This doesn't seem to cost measurable time. */
4845 if (it->redisplay_end_trigger_charpos
4846 && it->glyph_row
4847 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
4848 run_redisplay_end_trigger_hook (it);
4849
4850 /* Get the next character, maybe multibyte. */
4851 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
4852 if (it->multibyte_p && !ASCII_BYTE_P (*p))
4853 {
4854 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
4855 - IT_BYTEPOS (*it));
4856 it->c = string_char_and_length (p, maxlen, &it->len);
4857 }
4858 else
4859 it->c = *p, it->len = 1;
4860
4861 /* Record what we have and where it came from. */
4862 it->what = IT_CHARACTER;;
4863 it->object = it->w->buffer;
4864 it->position = it->current.pos;
4865
4866 /* Normally we return the character found above, except when we
4867 really want to return an ellipsis for selective display. */
4868 if (it->selective)
4869 {
4870 if (it->c == '\n')
4871 {
4872 /* A value of selective > 0 means hide lines indented more
4873 than that number of columns. */
4874 if (it->selective > 0
4875 && IT_CHARPOS (*it) + 1 < ZV
4876 && indented_beyond_p (IT_CHARPOS (*it) + 1,
4877 IT_BYTEPOS (*it) + 1,
4878 (double) it->selective)) /* iftc */
4879 {
4880 success_p = next_element_from_ellipsis (it);
4881 it->dpvec_char_len = -1;
4882 }
4883 }
4884 else if (it->c == '\r' && it->selective == -1)
4885 {
4886 /* A value of selective == -1 means that everything from the
4887 CR to the end of the line is invisible, with maybe an
4888 ellipsis displayed for it. */
4889 success_p = next_element_from_ellipsis (it);
4890 it->dpvec_char_len = -1;
4891 }
4892 }
4893 }
4894
4895 /* Value is zero if end of buffer reached. */
4896 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
4897 return success_p;
4898 }
4899
4900
4901 /* Run the redisplay end trigger hook for IT. */
4902
4903 static void
4904 run_redisplay_end_trigger_hook (it)
4905 struct it *it;
4906 {
4907 Lisp_Object args[3];
4908
4909 /* IT->glyph_row should be non-null, i.e. we should be actually
4910 displaying something, or otherwise we should not run the hook. */
4911 xassert (it->glyph_row);
4912
4913 /* Set up hook arguments. */
4914 args[0] = Qredisplay_end_trigger_functions;
4915 args[1] = it->window;
4916 XSETINT (args[2], it->redisplay_end_trigger_charpos);
4917 it->redisplay_end_trigger_charpos = 0;
4918
4919 /* Since we are *trying* to run these functions, don't try to run
4920 them again, even if they get an error. */
4921 it->w->redisplay_end_trigger = Qnil;
4922 Frun_hook_with_args (3, args);
4923
4924 /* Notice if it changed the face of the character we are on. */
4925 handle_face_prop (it);
4926 }
4927
4928
4929 /* Deliver a composition display element. The iterator IT is already
4930 filled with composition information (done in
4931 handle_composition_prop). Value is always 1. */
4932
4933 static int
4934 next_element_from_composition (it)
4935 struct it *it;
4936 {
4937 it->what = IT_COMPOSITION;
4938 it->position = (STRINGP (it->string)
4939 ? it->current.string_pos
4940 : it->current.pos);
4941 return 1;
4942 }
4943
4944
4945 \f
4946 /***********************************************************************
4947 Moving an iterator without producing glyphs
4948 ***********************************************************************/
4949
4950 /* Move iterator IT to a specified buffer or X position within one
4951 line on the display without producing glyphs.
4952
4953 OP should be a bit mask including some or all of these bits:
4954 MOVE_TO_X: Stop on reaching x-position TO_X.
4955 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
4956 Regardless of OP's value, stop in reaching the end of the display line.
4957
4958 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
4959 This means, in particular, that TO_X includes window's horizontal
4960 scroll amount.
4961
4962 The return value has several possible values that
4963 say what condition caused the scan to stop:
4964
4965 MOVE_POS_MATCH_OR_ZV
4966 - when TO_POS or ZV was reached.
4967
4968 MOVE_X_REACHED
4969 -when TO_X was reached before TO_POS or ZV were reached.
4970
4971 MOVE_LINE_CONTINUED
4972 - when we reached the end of the display area and the line must
4973 be continued.
4974
4975 MOVE_LINE_TRUNCATED
4976 - when we reached the end of the display area and the line is
4977 truncated.
4978
4979 MOVE_NEWLINE_OR_CR
4980 - when we stopped at a line end, i.e. a newline or a CR and selective
4981 display is on. */
4982
4983 static enum move_it_result
4984 move_it_in_display_line_to (it, to_charpos, to_x, op)
4985 struct it *it;
4986 int to_charpos, to_x, op;
4987 {
4988 enum move_it_result result = MOVE_UNDEFINED;
4989 struct glyph_row *saved_glyph_row;
4990
4991 /* Don't produce glyphs in produce_glyphs. */
4992 saved_glyph_row = it->glyph_row;
4993 it->glyph_row = NULL;
4994
4995 while (1)
4996 {
4997 int x, i, ascent = 0, descent = 0;
4998
4999 /* Stop when ZV or TO_CHARPOS reached. */
5000 if (!get_next_display_element (it)
5001 || ((op & MOVE_TO_POS) != 0
5002 && BUFFERP (it->object)
5003 && IT_CHARPOS (*it) >= to_charpos))
5004 {
5005 result = MOVE_POS_MATCH_OR_ZV;
5006 break;
5007 }
5008
5009 /* The call to produce_glyphs will get the metrics of the
5010 display element IT is loaded with. We record in x the
5011 x-position before this display element in case it does not
5012 fit on the line. */
5013 x = it->current_x;
5014
5015 /* Remember the line height so far in case the next element doesn't
5016 fit on the line. */
5017 if (!it->truncate_lines_p)
5018 {
5019 ascent = it->max_ascent;
5020 descent = it->max_descent;
5021 }
5022
5023 PRODUCE_GLYPHS (it);
5024
5025 if (it->area != TEXT_AREA)
5026 {
5027 set_iterator_to_next (it, 1);
5028 continue;
5029 }
5030
5031 /* The number of glyphs we get back in IT->nglyphs will normally
5032 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
5033 character on a terminal frame, or (iii) a line end. For the
5034 second case, IT->nglyphs - 1 padding glyphs will be present
5035 (on X frames, there is only one glyph produced for a
5036 composite character.
5037
5038 The behavior implemented below means, for continuation lines,
5039 that as many spaces of a TAB as fit on the current line are
5040 displayed there. For terminal frames, as many glyphs of a
5041 multi-glyph character are displayed in the current line, too.
5042 This is what the old redisplay code did, and we keep it that
5043 way. Under X, the whole shape of a complex character must
5044 fit on the line or it will be completely displayed in the
5045 next line.
5046
5047 Note that both for tabs and padding glyphs, all glyphs have
5048 the same width. */
5049 if (it->nglyphs)
5050 {
5051 /* More than one glyph or glyph doesn't fit on line. All
5052 glyphs have the same width. */
5053 int single_glyph_width = it->pixel_width / it->nglyphs;
5054 int new_x;
5055
5056 for (i = 0; i < it->nglyphs; ++i, x = new_x)
5057 {
5058 new_x = x + single_glyph_width;
5059
5060 /* We want to leave anything reaching TO_X to the caller. */
5061 if ((op & MOVE_TO_X) && new_x > to_x)
5062 {
5063 it->current_x = x;
5064 result = MOVE_X_REACHED;
5065 break;
5066 }
5067 else if (/* Lines are continued. */
5068 !it->truncate_lines_p
5069 && (/* And glyph doesn't fit on the line. */
5070 new_x > it->last_visible_x
5071 /* Or it fits exactly and we're on a window
5072 system frame. */
5073 || (new_x == it->last_visible_x
5074 && FRAME_WINDOW_P (it->f))))
5075 {
5076 if (/* IT->hpos == 0 means the very first glyph
5077 doesn't fit on the line, e.g. a wide image. */
5078 it->hpos == 0
5079 || (new_x == it->last_visible_x
5080 && FRAME_WINDOW_P (it->f)))
5081 {
5082 ++it->hpos;
5083 it->current_x = new_x;
5084 if (i == it->nglyphs - 1)
5085 set_iterator_to_next (it, 1);
5086 }
5087 else
5088 {
5089 it->current_x = x;
5090 it->max_ascent = ascent;
5091 it->max_descent = descent;
5092 }
5093
5094 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
5095 IT_CHARPOS (*it)));
5096 result = MOVE_LINE_CONTINUED;
5097 break;
5098 }
5099 else if (new_x > it->first_visible_x)
5100 {
5101 /* Glyph is visible. Increment number of glyphs that
5102 would be displayed. */
5103 ++it->hpos;
5104 }
5105 else
5106 {
5107 /* Glyph is completely off the left margin of the display
5108 area. Nothing to do. */
5109 }
5110 }
5111
5112 if (result != MOVE_UNDEFINED)
5113 break;
5114 }
5115 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
5116 {
5117 /* Stop when TO_X specified and reached. This check is
5118 necessary here because of lines consisting of a line end,
5119 only. The line end will not produce any glyphs and we
5120 would never get MOVE_X_REACHED. */
5121 xassert (it->nglyphs == 0);
5122 result = MOVE_X_REACHED;
5123 break;
5124 }
5125
5126 /* Is this a line end? If yes, we're done. */
5127 if (ITERATOR_AT_END_OF_LINE_P (it))
5128 {
5129 result = MOVE_NEWLINE_OR_CR;
5130 break;
5131 }
5132
5133 /* The current display element has been consumed. Advance
5134 to the next. */
5135 set_iterator_to_next (it, 1);
5136
5137 /* Stop if lines are truncated and IT's current x-position is
5138 past the right edge of the window now. */
5139 if (it->truncate_lines_p
5140 && it->current_x >= it->last_visible_x)
5141 {
5142 result = MOVE_LINE_TRUNCATED;
5143 break;
5144 }
5145 }
5146
5147 /* Restore the iterator settings altered at the beginning of this
5148 function. */
5149 it->glyph_row = saved_glyph_row;
5150 return result;
5151 }
5152
5153
5154 /* Move IT forward until it satisfies one or more of the criteria in
5155 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
5156
5157 OP is a bit-mask that specifies where to stop, and in particular,
5158 which of those four position arguments makes a difference. See the
5159 description of enum move_operation_enum.
5160
5161 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
5162 screen line, this function will set IT to the next position >
5163 TO_CHARPOS. */
5164
5165 void
5166 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
5167 struct it *it;
5168 int to_charpos, to_x, to_y, to_vpos;
5169 int op;
5170 {
5171 enum move_it_result skip, skip2 = MOVE_X_REACHED;
5172 int line_height;
5173 int reached = 0;
5174
5175 for (;;)
5176 {
5177 if (op & MOVE_TO_VPOS)
5178 {
5179 /* If no TO_CHARPOS and no TO_X specified, stop at the
5180 start of the line TO_VPOS. */
5181 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
5182 {
5183 if (it->vpos == to_vpos)
5184 {
5185 reached = 1;
5186 break;
5187 }
5188 else
5189 skip = move_it_in_display_line_to (it, -1, -1, 0);
5190 }
5191 else
5192 {
5193 /* TO_VPOS >= 0 means stop at TO_X in the line at
5194 TO_VPOS, or at TO_POS, whichever comes first. */
5195 if (it->vpos == to_vpos)
5196 {
5197 reached = 2;
5198 break;
5199 }
5200
5201 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
5202
5203 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
5204 {
5205 reached = 3;
5206 break;
5207 }
5208 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
5209 {
5210 /* We have reached TO_X but not in the line we want. */
5211 skip = move_it_in_display_line_to (it, to_charpos,
5212 -1, MOVE_TO_POS);
5213 if (skip == MOVE_POS_MATCH_OR_ZV)
5214 {
5215 reached = 4;
5216 break;
5217 }
5218 }
5219 }
5220 }
5221 else if (op & MOVE_TO_Y)
5222 {
5223 struct it it_backup;
5224
5225 /* TO_Y specified means stop at TO_X in the line containing
5226 TO_Y---or at TO_CHARPOS if this is reached first. The
5227 problem is that we can't really tell whether the line
5228 contains TO_Y before we have completely scanned it, and
5229 this may skip past TO_X. What we do is to first scan to
5230 TO_X.
5231
5232 If TO_X is not specified, use a TO_X of zero. The reason
5233 is to make the outcome of this function more predictable.
5234 If we didn't use TO_X == 0, we would stop at the end of
5235 the line which is probably not what a caller would expect
5236 to happen. */
5237 skip = move_it_in_display_line_to (it, to_charpos,
5238 ((op & MOVE_TO_X)
5239 ? to_x : 0),
5240 (MOVE_TO_X
5241 | (op & MOVE_TO_POS)));
5242
5243 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
5244 if (skip == MOVE_POS_MATCH_OR_ZV)
5245 {
5246 reached = 5;
5247 break;
5248 }
5249
5250 /* If TO_X was reached, we would like to know whether TO_Y
5251 is in the line. This can only be said if we know the
5252 total line height which requires us to scan the rest of
5253 the line. */
5254 if (skip == MOVE_X_REACHED)
5255 {
5256 it_backup = *it;
5257 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
5258 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
5259 op & MOVE_TO_POS);
5260 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
5261 }
5262
5263 /* Now, decide whether TO_Y is in this line. */
5264 line_height = it->max_ascent + it->max_descent;
5265 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
5266
5267 if (to_y >= it->current_y
5268 && to_y < it->current_y + line_height)
5269 {
5270 if (skip == MOVE_X_REACHED)
5271 /* If TO_Y is in this line and TO_X was reached above,
5272 we scanned too far. We have to restore IT's settings
5273 to the ones before skipping. */
5274 *it = it_backup;
5275 reached = 6;
5276 }
5277 else if (skip == MOVE_X_REACHED)
5278 {
5279 skip = skip2;
5280 if (skip == MOVE_POS_MATCH_OR_ZV)
5281 reached = 7;
5282 }
5283
5284 if (reached)
5285 break;
5286 }
5287 else
5288 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
5289
5290 switch (skip)
5291 {
5292 case MOVE_POS_MATCH_OR_ZV:
5293 reached = 8;
5294 goto out;
5295
5296 case MOVE_NEWLINE_OR_CR:
5297 set_iterator_to_next (it, 1);
5298 it->continuation_lines_width = 0;
5299 break;
5300
5301 case MOVE_LINE_TRUNCATED:
5302 it->continuation_lines_width = 0;
5303 reseat_at_next_visible_line_start (it, 0);
5304 if ((op & MOVE_TO_POS) != 0
5305 && IT_CHARPOS (*it) > to_charpos)
5306 {
5307 reached = 9;
5308 goto out;
5309 }
5310 break;
5311
5312 case MOVE_LINE_CONTINUED:
5313 it->continuation_lines_width += it->current_x;
5314 break;
5315
5316 default:
5317 abort ();
5318 }
5319
5320 /* Reset/increment for the next run. */
5321 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
5322 it->current_x = it->hpos = 0;
5323 it->current_y += it->max_ascent + it->max_descent;
5324 ++it->vpos;
5325 last_height = it->max_ascent + it->max_descent;
5326 last_max_ascent = it->max_ascent;
5327 it->max_ascent = it->max_descent = 0;
5328 }
5329
5330 out:
5331
5332 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
5333 }
5334
5335
5336 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
5337
5338 If DY > 0, move IT backward at least that many pixels. DY = 0
5339 means move IT backward to the preceding line start or BEGV. This
5340 function may move over more than DY pixels if IT->current_y - DY
5341 ends up in the middle of a line; in this case IT->current_y will be
5342 set to the top of the line moved to. */
5343
5344 void
5345 move_it_vertically_backward (it, dy)
5346 struct it *it;
5347 int dy;
5348 {
5349 int nlines, h;
5350 struct it it2, it3;
5351 int start_pos = IT_CHARPOS (*it);
5352
5353 xassert (dy >= 0);
5354
5355 /* Estimate how many newlines we must move back. */
5356 nlines = max (1, dy / CANON_Y_UNIT (it->f));
5357
5358 /* Set the iterator's position that many lines back. */
5359 while (nlines-- && IT_CHARPOS (*it) > BEGV)
5360 back_to_previous_visible_line_start (it);
5361
5362 /* Reseat the iterator here. When moving backward, we don't want
5363 reseat to skip forward over invisible text, set up the iterator
5364 to deliver from overlay strings at the new position etc. So,
5365 use reseat_1 here. */
5366 reseat_1 (it, it->current.pos, 1);
5367
5368 /* We are now surely at a line start. */
5369 it->current_x = it->hpos = 0;
5370
5371 /* Move forward and see what y-distance we moved. First move to the
5372 start of the next line so that we get its height. We need this
5373 height to be able to tell whether we reached the specified
5374 y-distance. */
5375 it2 = *it;
5376 it2.max_ascent = it2.max_descent = 0;
5377 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
5378 MOVE_TO_POS | MOVE_TO_VPOS);
5379 xassert (IT_CHARPOS (*it) >= BEGV);
5380 it3 = it2;
5381
5382 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
5383 xassert (IT_CHARPOS (*it) >= BEGV);
5384 h = it2.current_y - it->current_y;
5385 nlines = it2.vpos - it->vpos;
5386
5387 /* Correct IT's y and vpos position. */
5388 it->vpos -= nlines;
5389 it->current_y -= h;
5390
5391 if (dy == 0)
5392 {
5393 /* DY == 0 means move to the start of the screen line. The
5394 value of nlines is > 0 if continuation lines were involved. */
5395 if (nlines > 0)
5396 move_it_by_lines (it, nlines, 1);
5397 xassert (IT_CHARPOS (*it) <= start_pos);
5398 }
5399 else if (nlines)
5400 {
5401 /* The y-position we try to reach. Note that h has been
5402 subtracted in front of the if-statement. */
5403 int target_y = it->current_y + h - dy;
5404 int y0 = it3.current_y;
5405 int y1 = line_bottom_y (&it3);
5406 int line_height = y1 - y0;
5407
5408 /* If we did not reach target_y, try to move further backward if
5409 we can. If we moved too far backward, try to move forward. */
5410 if (target_y < it->current_y
5411 /* This is heuristic. In a window that's 3 lines high, with
5412 a line height of 13 pixels each, recentering with point
5413 on the bottom line will try to move -39/2 = 19 pixels
5414 backward. Try to avoid moving into the first line. */
5415 && it->current_y - target_y > line_height / 3 * 2
5416 && IT_CHARPOS (*it) > BEGV)
5417 {
5418 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
5419 target_y - it->current_y));
5420 move_it_vertically (it, target_y - it->current_y);
5421 xassert (IT_CHARPOS (*it) >= BEGV);
5422 }
5423 else if (target_y >= it->current_y + line_height
5424 && IT_CHARPOS (*it) < ZV)
5425 {
5426 /* Should move forward by at least one line, maybe more.
5427
5428 Note: Calling move_it_by_lines can be expensive on
5429 terminal frames, where compute_motion is used (via
5430 vmotion) to do the job, when there are very long lines
5431 and truncate-lines is nil. That's the reason for
5432 treating terminal frames specially here. */
5433
5434 if (!FRAME_WINDOW_P (it->f))
5435 move_it_vertically (it, target_y - (it->current_y + line_height));
5436 else
5437 {
5438 do
5439 {
5440 move_it_by_lines (it, 1, 1);
5441 }
5442 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
5443 }
5444
5445 xassert (IT_CHARPOS (*it) >= BEGV);
5446 }
5447 }
5448 }
5449
5450
5451 /* Move IT by a specified amount of pixel lines DY. DY negative means
5452 move backwards. DY = 0 means move to start of screen line. At the
5453 end, IT will be on the start of a screen line. */
5454
5455 void
5456 move_it_vertically (it, dy)
5457 struct it *it;
5458 int dy;
5459 {
5460 if (dy <= 0)
5461 move_it_vertically_backward (it, -dy);
5462 else if (dy > 0)
5463 {
5464 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
5465 move_it_to (it, ZV, -1, it->current_y + dy, -1,
5466 MOVE_TO_POS | MOVE_TO_Y);
5467 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
5468
5469 /* If buffer ends in ZV without a newline, move to the start of
5470 the line to satisfy the post-condition. */
5471 if (IT_CHARPOS (*it) == ZV
5472 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
5473 move_it_by_lines (it, 0, 0);
5474 }
5475 }
5476
5477
5478 /* Move iterator IT past the end of the text line it is in. */
5479
5480 void
5481 move_it_past_eol (it)
5482 struct it *it;
5483 {
5484 enum move_it_result rc;
5485
5486 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
5487 if (rc == MOVE_NEWLINE_OR_CR)
5488 set_iterator_to_next (it, 0);
5489 }
5490
5491
5492 #if 0 /* Currently not used. */
5493
5494 /* Return non-zero if some text between buffer positions START_CHARPOS
5495 and END_CHARPOS is invisible. IT->window is the window for text
5496 property lookup. */
5497
5498 static int
5499 invisible_text_between_p (it, start_charpos, end_charpos)
5500 struct it *it;
5501 int start_charpos, end_charpos;
5502 {
5503 Lisp_Object prop, limit;
5504 int invisible_found_p;
5505
5506 xassert (it != NULL && start_charpos <= end_charpos);
5507
5508 /* Is text at START invisible? */
5509 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
5510 it->window);
5511 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5512 invisible_found_p = 1;
5513 else
5514 {
5515 limit = Fnext_single_char_property_change (make_number (start_charpos),
5516 Qinvisible, Qnil,
5517 make_number (end_charpos));
5518 invisible_found_p = XFASTINT (limit) < end_charpos;
5519 }
5520
5521 return invisible_found_p;
5522 }
5523
5524 #endif /* 0 */
5525
5526
5527 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
5528 negative means move up. DVPOS == 0 means move to the start of the
5529 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
5530 NEED_Y_P is zero, IT->current_y will be left unchanged.
5531
5532 Further optimization ideas: If we would know that IT->f doesn't use
5533 a face with proportional font, we could be faster for
5534 truncate-lines nil. */
5535
5536 void
5537 move_it_by_lines (it, dvpos, need_y_p)
5538 struct it *it;
5539 int dvpos, need_y_p;
5540 {
5541 struct position pos;
5542
5543 if (!FRAME_WINDOW_P (it->f))
5544 {
5545 struct text_pos textpos;
5546
5547 /* We can use vmotion on frames without proportional fonts. */
5548 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
5549 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
5550 reseat (it, textpos, 1);
5551 it->vpos += pos.vpos;
5552 it->current_y += pos.vpos;
5553 }
5554 else if (dvpos == 0)
5555 {
5556 /* DVPOS == 0 means move to the start of the screen line. */
5557 move_it_vertically_backward (it, 0);
5558 xassert (it->current_x == 0 && it->hpos == 0);
5559 }
5560 else if (dvpos > 0)
5561 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
5562 else
5563 {
5564 struct it it2;
5565 int start_charpos, i;
5566
5567 /* Start at the beginning of the screen line containing IT's
5568 position. */
5569 move_it_vertically_backward (it, 0);
5570
5571 /* Go back -DVPOS visible lines and reseat the iterator there. */
5572 start_charpos = IT_CHARPOS (*it);
5573 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
5574 back_to_previous_visible_line_start (it);
5575 reseat (it, it->current.pos, 1);
5576 it->current_x = it->hpos = 0;
5577
5578 /* Above call may have moved too far if continuation lines
5579 are involved. Scan forward and see if it did. */
5580 it2 = *it;
5581 it2.vpos = it2.current_y = 0;
5582 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
5583 it->vpos -= it2.vpos;
5584 it->current_y -= it2.current_y;
5585 it->current_x = it->hpos = 0;
5586
5587 /* If we moved too far, move IT some lines forward. */
5588 if (it2.vpos > -dvpos)
5589 {
5590 int delta = it2.vpos + dvpos;
5591 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
5592 }
5593 }
5594 }
5595
5596
5597 \f
5598 /***********************************************************************
5599 Messages
5600 ***********************************************************************/
5601
5602
5603 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
5604 to *Messages*. */
5605
5606 void
5607 add_to_log (format, arg1, arg2)
5608 char *format;
5609 Lisp_Object arg1, arg2;
5610 {
5611 Lisp_Object args[3];
5612 Lisp_Object msg, fmt;
5613 char *buffer;
5614 int len;
5615 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5616
5617 /* Do nothing if called asynchronously. Inserting text into
5618 a buffer may call after-change-functions and alike and
5619 that would means running Lisp asynchronously. */
5620 if (handling_signal)
5621 return;
5622
5623 fmt = msg = Qnil;
5624 GCPRO4 (fmt, msg, arg1, arg2);
5625
5626 args[0] = fmt = build_string (format);
5627 args[1] = arg1;
5628 args[2] = arg2;
5629 msg = Fformat (3, args);
5630
5631 len = SBYTES (msg) + 1;
5632 buffer = (char *) alloca (len);
5633 bcopy (SDATA (msg), buffer, len);
5634
5635 message_dolog (buffer, len - 1, 1, 0);
5636 UNGCPRO;
5637 }
5638
5639
5640 /* Output a newline in the *Messages* buffer if "needs" one. */
5641
5642 void
5643 message_log_maybe_newline ()
5644 {
5645 if (message_log_need_newline)
5646 message_dolog ("", 0, 1, 0);
5647 }
5648
5649
5650 /* Add a string M of length NBYTES to the message log, optionally
5651 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
5652 nonzero, means interpret the contents of M as multibyte. This
5653 function calls low-level routines in order to bypass text property
5654 hooks, etc. which might not be safe to run. */
5655
5656 void
5657 message_dolog (m, nbytes, nlflag, multibyte)
5658 const char *m;
5659 int nbytes, nlflag, multibyte;
5660 {
5661 if (!NILP (Vmemory_full))
5662 return;
5663
5664 if (!NILP (Vmessage_log_max))
5665 {
5666 struct buffer *oldbuf;
5667 Lisp_Object oldpoint, oldbegv, oldzv;
5668 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5669 int point_at_end = 0;
5670 int zv_at_end = 0;
5671 Lisp_Object old_deactivate_mark, tem;
5672 struct gcpro gcpro1;
5673
5674 old_deactivate_mark = Vdeactivate_mark;
5675 oldbuf = current_buffer;
5676 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
5677 current_buffer->undo_list = Qt;
5678
5679 oldpoint = message_dolog_marker1;
5680 set_marker_restricted (oldpoint, make_number (PT), Qnil);
5681 oldbegv = message_dolog_marker2;
5682 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
5683 oldzv = message_dolog_marker3;
5684 set_marker_restricted (oldzv, make_number (ZV), Qnil);
5685 GCPRO1 (old_deactivate_mark);
5686
5687 if (PT == Z)
5688 point_at_end = 1;
5689 if (ZV == Z)
5690 zv_at_end = 1;
5691
5692 BEGV = BEG;
5693 BEGV_BYTE = BEG_BYTE;
5694 ZV = Z;
5695 ZV_BYTE = Z_BYTE;
5696 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5697
5698 /* Insert the string--maybe converting multibyte to single byte
5699 or vice versa, so that all the text fits the buffer. */
5700 if (multibyte
5701 && NILP (current_buffer->enable_multibyte_characters))
5702 {
5703 int i, c, char_bytes;
5704 unsigned char work[1];
5705
5706 /* Convert a multibyte string to single-byte
5707 for the *Message* buffer. */
5708 for (i = 0; i < nbytes; i += nbytes)
5709 {
5710 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
5711 work[0] = (SINGLE_BYTE_CHAR_P (c)
5712 ? c
5713 : multibyte_char_to_unibyte (c, Qnil));
5714 insert_1_both (work, 1, 1, 1, 0, 0);
5715 }
5716 }
5717 else if (! multibyte
5718 && ! NILP (current_buffer->enable_multibyte_characters))
5719 {
5720 int i, c, char_bytes;
5721 unsigned char *msg = (unsigned char *) m;
5722 unsigned char str[MAX_MULTIBYTE_LENGTH];
5723 /* Convert a single-byte string to multibyte
5724 for the *Message* buffer. */
5725 for (i = 0; i < nbytes; i++)
5726 {
5727 c = unibyte_char_to_multibyte (msg[i]);
5728 char_bytes = CHAR_STRING (c, str);
5729 insert_1_both (str, 1, char_bytes, 1, 0, 0);
5730 }
5731 }
5732 else if (nbytes)
5733 insert_1 (m, nbytes, 1, 0, 0);
5734
5735 if (nlflag)
5736 {
5737 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
5738 insert_1 ("\n", 1, 1, 0, 0);
5739
5740 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
5741 this_bol = PT;
5742 this_bol_byte = PT_BYTE;
5743
5744 /* See if this line duplicates the previous one.
5745 If so, combine duplicates. */
5746 if (this_bol > BEG)
5747 {
5748 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
5749 prev_bol = PT;
5750 prev_bol_byte = PT_BYTE;
5751
5752 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
5753 this_bol, this_bol_byte);
5754 if (dup)
5755 {
5756 del_range_both (prev_bol, prev_bol_byte,
5757 this_bol, this_bol_byte, 0);
5758 if (dup > 1)
5759 {
5760 char dupstr[40];
5761 int duplen;
5762
5763 /* If you change this format, don't forget to also
5764 change message_log_check_duplicate. */
5765 sprintf (dupstr, " [%d times]", dup);
5766 duplen = strlen (dupstr);
5767 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
5768 insert_1 (dupstr, duplen, 1, 0, 1);
5769 }
5770 }
5771 }
5772
5773 /* If we have more than the desired maximum number of lines
5774 in the *Messages* buffer now, delete the oldest ones.
5775 This is safe because we don't have undo in this buffer. */
5776
5777 if (NATNUMP (Vmessage_log_max))
5778 {
5779 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
5780 -XFASTINT (Vmessage_log_max) - 1, 0);
5781 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
5782 }
5783 }
5784 BEGV = XMARKER (oldbegv)->charpos;
5785 BEGV_BYTE = marker_byte_position (oldbegv);
5786
5787 if (zv_at_end)
5788 {
5789 ZV = Z;
5790 ZV_BYTE = Z_BYTE;
5791 }
5792 else
5793 {
5794 ZV = XMARKER (oldzv)->charpos;
5795 ZV_BYTE = marker_byte_position (oldzv);
5796 }
5797
5798 if (point_at_end)
5799 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5800 else
5801 /* We can't do Fgoto_char (oldpoint) because it will run some
5802 Lisp code. */
5803 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
5804 XMARKER (oldpoint)->bytepos);
5805
5806 UNGCPRO;
5807 unchain_marker (oldpoint);
5808 unchain_marker (oldbegv);
5809 unchain_marker (oldzv);
5810
5811 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
5812 set_buffer_internal (oldbuf);
5813 if (NILP (tem))
5814 windows_or_buffers_changed = old_windows_or_buffers_changed;
5815 message_log_need_newline = !nlflag;
5816 Vdeactivate_mark = old_deactivate_mark;
5817 }
5818 }
5819
5820
5821 /* We are at the end of the buffer after just having inserted a newline.
5822 (Note: We depend on the fact we won't be crossing the gap.)
5823 Check to see if the most recent message looks a lot like the previous one.
5824 Return 0 if different, 1 if the new one should just replace it, or a
5825 value N > 1 if we should also append " [N times]". */
5826
5827 static int
5828 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
5829 int prev_bol, this_bol;
5830 int prev_bol_byte, this_bol_byte;
5831 {
5832 int i;
5833 int len = Z_BYTE - 1 - this_bol_byte;
5834 int seen_dots = 0;
5835 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
5836 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
5837
5838 for (i = 0; i < len; i++)
5839 {
5840 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
5841 seen_dots = 1;
5842 if (p1[i] != p2[i])
5843 return seen_dots;
5844 }
5845 p1 += len;
5846 if (*p1 == '\n')
5847 return 2;
5848 if (*p1++ == ' ' && *p1++ == '[')
5849 {
5850 int n = 0;
5851 while (*p1 >= '0' && *p1 <= '9')
5852 n = n * 10 + *p1++ - '0';
5853 if (strncmp (p1, " times]\n", 8) == 0)
5854 return n+1;
5855 }
5856 return 0;
5857 }
5858
5859
5860 /* Display an echo area message M with a specified length of NBYTES
5861 bytes. The string may include null characters. If M is 0, clear
5862 out any existing message, and let the mini-buffer text show
5863 through.
5864
5865 The buffer M must continue to exist until after the echo area gets
5866 cleared or some other message gets displayed there. This means do
5867 not pass text that is stored in a Lisp string; do not pass text in
5868 a buffer that was alloca'd. */
5869
5870 void
5871 message2 (m, nbytes, multibyte)
5872 const char *m;
5873 int nbytes;
5874 int multibyte;
5875 {
5876 /* First flush out any partial line written with print. */
5877 message_log_maybe_newline ();
5878 if (m)
5879 message_dolog (m, nbytes, 1, multibyte);
5880 message2_nolog (m, nbytes, multibyte);
5881 }
5882
5883
5884 /* The non-logging counterpart of message2. */
5885
5886 void
5887 message2_nolog (m, nbytes, multibyte)
5888 const char *m;
5889 int nbytes;
5890 {
5891 struct frame *sf = SELECTED_FRAME ();
5892 message_enable_multibyte = multibyte;
5893
5894 if (noninteractive)
5895 {
5896 if (noninteractive_need_newline)
5897 putc ('\n', stderr);
5898 noninteractive_need_newline = 0;
5899 if (m)
5900 fwrite (m, nbytes, 1, stderr);
5901 if (cursor_in_echo_area == 0)
5902 fprintf (stderr, "\n");
5903 fflush (stderr);
5904 }
5905 /* A null message buffer means that the frame hasn't really been
5906 initialized yet. Error messages get reported properly by
5907 cmd_error, so this must be just an informative message; toss it. */
5908 else if (INTERACTIVE
5909 && sf->glyphs_initialized_p
5910 && FRAME_MESSAGE_BUF (sf))
5911 {
5912 Lisp_Object mini_window;
5913 struct frame *f;
5914
5915 /* Get the frame containing the mini-buffer
5916 that the selected frame is using. */
5917 mini_window = FRAME_MINIBUF_WINDOW (sf);
5918 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5919
5920 FRAME_SAMPLE_VISIBILITY (f);
5921 if (FRAME_VISIBLE_P (sf)
5922 && ! FRAME_VISIBLE_P (f))
5923 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
5924
5925 if (m)
5926 {
5927 set_message (m, Qnil, nbytes, multibyte);
5928 if (minibuffer_auto_raise)
5929 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5930 }
5931 else
5932 clear_message (1, 1);
5933
5934 do_pending_window_change (0);
5935 echo_area_display (1);
5936 do_pending_window_change (0);
5937 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5938 (*frame_up_to_date_hook) (f);
5939 }
5940 }
5941
5942
5943 /* Display an echo area message M with a specified length of NBYTES
5944 bytes. The string may include null characters. If M is not a
5945 string, clear out any existing message, and let the mini-buffer
5946 text show through. */
5947
5948 void
5949 message3 (m, nbytes, multibyte)
5950 Lisp_Object m;
5951 int nbytes;
5952 int multibyte;
5953 {
5954 struct gcpro gcpro1;
5955
5956 GCPRO1 (m);
5957
5958 /* First flush out any partial line written with print. */
5959 message_log_maybe_newline ();
5960 if (STRINGP (m))
5961 message_dolog (SDATA (m), nbytes, 1, multibyte);
5962 message3_nolog (m, nbytes, multibyte);
5963
5964 UNGCPRO;
5965 }
5966
5967
5968 /* The non-logging version of message3. */
5969
5970 void
5971 message3_nolog (m, nbytes, multibyte)
5972 Lisp_Object m;
5973 int nbytes, multibyte;
5974 {
5975 struct frame *sf = SELECTED_FRAME ();
5976 message_enable_multibyte = multibyte;
5977
5978 if (noninteractive)
5979 {
5980 if (noninteractive_need_newline)
5981 putc ('\n', stderr);
5982 noninteractive_need_newline = 0;
5983 if (STRINGP (m))
5984 fwrite (SDATA (m), nbytes, 1, stderr);
5985 if (cursor_in_echo_area == 0)
5986 fprintf (stderr, "\n");
5987 fflush (stderr);
5988 }
5989 /* A null message buffer means that the frame hasn't really been
5990 initialized yet. Error messages get reported properly by
5991 cmd_error, so this must be just an informative message; toss it. */
5992 else if (INTERACTIVE
5993 && sf->glyphs_initialized_p
5994 && FRAME_MESSAGE_BUF (sf))
5995 {
5996 Lisp_Object mini_window;
5997 Lisp_Object frame;
5998 struct frame *f;
5999
6000 /* Get the frame containing the mini-buffer
6001 that the selected frame is using. */
6002 mini_window = FRAME_MINIBUF_WINDOW (sf);
6003 frame = XWINDOW (mini_window)->frame;
6004 f = XFRAME (frame);
6005
6006 FRAME_SAMPLE_VISIBILITY (f);
6007 if (FRAME_VISIBLE_P (sf)
6008 && !FRAME_VISIBLE_P (f))
6009 Fmake_frame_visible (frame);
6010
6011 if (STRINGP (m) && SCHARS (m) > 0)
6012 {
6013 set_message (NULL, m, nbytes, multibyte);
6014 if (minibuffer_auto_raise)
6015 Fraise_frame (frame);
6016 }
6017 else
6018 clear_message (1, 1);
6019
6020 do_pending_window_change (0);
6021 echo_area_display (1);
6022 do_pending_window_change (0);
6023 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6024 (*frame_up_to_date_hook) (f);
6025 }
6026 }
6027
6028
6029 /* Display a null-terminated echo area message M. If M is 0, clear
6030 out any existing message, and let the mini-buffer text show through.
6031
6032 The buffer M must continue to exist until after the echo area gets
6033 cleared or some other message gets displayed there. Do not pass
6034 text that is stored in a Lisp string. Do not pass text in a buffer
6035 that was alloca'd. */
6036
6037 void
6038 message1 (m)
6039 char *m;
6040 {
6041 message2 (m, (m ? strlen (m) : 0), 0);
6042 }
6043
6044
6045 /* The non-logging counterpart of message1. */
6046
6047 void
6048 message1_nolog (m)
6049 char *m;
6050 {
6051 message2_nolog (m, (m ? strlen (m) : 0), 0);
6052 }
6053
6054 /* Display a message M which contains a single %s
6055 which gets replaced with STRING. */
6056
6057 void
6058 message_with_string (m, string, log)
6059 char *m;
6060 Lisp_Object string;
6061 int log;
6062 {
6063 CHECK_STRING (string);
6064
6065 if (noninteractive)
6066 {
6067 if (m)
6068 {
6069 if (noninteractive_need_newline)
6070 putc ('\n', stderr);
6071 noninteractive_need_newline = 0;
6072 fprintf (stderr, m, SDATA (string));
6073 if (cursor_in_echo_area == 0)
6074 fprintf (stderr, "\n");
6075 fflush (stderr);
6076 }
6077 }
6078 else if (INTERACTIVE)
6079 {
6080 /* The frame whose minibuffer we're going to display the message on.
6081 It may be larger than the selected frame, so we need
6082 to use its buffer, not the selected frame's buffer. */
6083 Lisp_Object mini_window;
6084 struct frame *f, *sf = SELECTED_FRAME ();
6085
6086 /* Get the frame containing the minibuffer
6087 that the selected frame is using. */
6088 mini_window = FRAME_MINIBUF_WINDOW (sf);
6089 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6090
6091 /* A null message buffer means that the frame hasn't really been
6092 initialized yet. Error messages get reported properly by
6093 cmd_error, so this must be just an informative message; toss it. */
6094 if (FRAME_MESSAGE_BUF (f))
6095 {
6096 Lisp_Object args[2], message;
6097 struct gcpro gcpro1, gcpro2;
6098
6099 args[0] = build_string (m);
6100 args[1] = message = string;
6101 GCPRO2 (args[0], message);
6102 gcpro1.nvars = 2;
6103
6104 message = Fformat (2, args);
6105
6106 if (log)
6107 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
6108 else
6109 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
6110
6111 UNGCPRO;
6112
6113 /* Print should start at the beginning of the message
6114 buffer next time. */
6115 message_buf_print = 0;
6116 }
6117 }
6118 }
6119
6120
6121 /* Dump an informative message to the minibuf. If M is 0, clear out
6122 any existing message, and let the mini-buffer text show through. */
6123
6124 /* VARARGS 1 */
6125 void
6126 message (m, a1, a2, a3)
6127 char *m;
6128 EMACS_INT a1, a2, a3;
6129 {
6130 if (noninteractive)
6131 {
6132 if (m)
6133 {
6134 if (noninteractive_need_newline)
6135 putc ('\n', stderr);
6136 noninteractive_need_newline = 0;
6137 fprintf (stderr, m, a1, a2, a3);
6138 if (cursor_in_echo_area == 0)
6139 fprintf (stderr, "\n");
6140 fflush (stderr);
6141 }
6142 }
6143 else if (INTERACTIVE)
6144 {
6145 /* The frame whose mini-buffer we're going to display the message
6146 on. It may be larger than the selected frame, so we need to
6147 use its buffer, not the selected frame's buffer. */
6148 Lisp_Object mini_window;
6149 struct frame *f, *sf = SELECTED_FRAME ();
6150
6151 /* Get the frame containing the mini-buffer
6152 that the selected frame is using. */
6153 mini_window = FRAME_MINIBUF_WINDOW (sf);
6154 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6155
6156 /* A null message buffer means that the frame hasn't really been
6157 initialized yet. Error messages get reported properly by
6158 cmd_error, so this must be just an informative message; toss
6159 it. */
6160 if (FRAME_MESSAGE_BUF (f))
6161 {
6162 if (m)
6163 {
6164 int len;
6165 #ifdef NO_ARG_ARRAY
6166 char *a[3];
6167 a[0] = (char *) a1;
6168 a[1] = (char *) a2;
6169 a[2] = (char *) a3;
6170
6171 len = doprnt (FRAME_MESSAGE_BUF (f),
6172 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6173 #else
6174 len = doprnt (FRAME_MESSAGE_BUF (f),
6175 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
6176 (char **) &a1);
6177 #endif /* NO_ARG_ARRAY */
6178
6179 message2 (FRAME_MESSAGE_BUF (f), len, 0);
6180 }
6181 else
6182 message1 (0);
6183
6184 /* Print should start at the beginning of the message
6185 buffer next time. */
6186 message_buf_print = 0;
6187 }
6188 }
6189 }
6190
6191
6192 /* The non-logging version of message. */
6193
6194 void
6195 message_nolog (m, a1, a2, a3)
6196 char *m;
6197 EMACS_INT a1, a2, a3;
6198 {
6199 Lisp_Object old_log_max;
6200 old_log_max = Vmessage_log_max;
6201 Vmessage_log_max = Qnil;
6202 message (m, a1, a2, a3);
6203 Vmessage_log_max = old_log_max;
6204 }
6205
6206
6207 /* Display the current message in the current mini-buffer. This is
6208 only called from error handlers in process.c, and is not time
6209 critical. */
6210
6211 void
6212 update_echo_area ()
6213 {
6214 if (!NILP (echo_area_buffer[0]))
6215 {
6216 Lisp_Object string;
6217 string = Fcurrent_message ();
6218 message3 (string, SBYTES (string),
6219 !NILP (current_buffer->enable_multibyte_characters));
6220 }
6221 }
6222
6223
6224 /* Make sure echo area buffers in `echo_buffers' are live.
6225 If they aren't, make new ones. */
6226
6227 static void
6228 ensure_echo_area_buffers ()
6229 {
6230 int i;
6231
6232 for (i = 0; i < 2; ++i)
6233 if (!BUFFERP (echo_buffer[i])
6234 || NILP (XBUFFER (echo_buffer[i])->name))
6235 {
6236 char name[30];
6237 Lisp_Object old_buffer;
6238 int j;
6239
6240 old_buffer = echo_buffer[i];
6241 sprintf (name, " *Echo Area %d*", i);
6242 echo_buffer[i] = Fget_buffer_create (build_string (name));
6243 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
6244
6245 for (j = 0; j < 2; ++j)
6246 if (EQ (old_buffer, echo_area_buffer[j]))
6247 echo_area_buffer[j] = echo_buffer[i];
6248 }
6249 }
6250
6251
6252 /* Call FN with args A1..A4 with either the current or last displayed
6253 echo_area_buffer as current buffer.
6254
6255 WHICH zero means use the current message buffer
6256 echo_area_buffer[0]. If that is nil, choose a suitable buffer
6257 from echo_buffer[] and clear it.
6258
6259 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
6260 suitable buffer from echo_buffer[] and clear it.
6261
6262 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
6263 that the current message becomes the last displayed one, make
6264 choose a suitable buffer for echo_area_buffer[0], and clear it.
6265
6266 Value is what FN returns. */
6267
6268 static int
6269 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
6270 struct window *w;
6271 int which;
6272 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
6273 EMACS_INT a1;
6274 Lisp_Object a2;
6275 EMACS_INT a3, a4;
6276 {
6277 Lisp_Object buffer;
6278 int this_one, the_other, clear_buffer_p, rc;
6279 int count = SPECPDL_INDEX ();
6280
6281 /* If buffers aren't live, make new ones. */
6282 ensure_echo_area_buffers ();
6283
6284 clear_buffer_p = 0;
6285
6286 if (which == 0)
6287 this_one = 0, the_other = 1;
6288 else if (which > 0)
6289 this_one = 1, the_other = 0;
6290 else
6291 {
6292 this_one = 0, the_other = 1;
6293 clear_buffer_p = 1;
6294
6295 /* We need a fresh one in case the current echo buffer equals
6296 the one containing the last displayed echo area message. */
6297 if (!NILP (echo_area_buffer[this_one])
6298 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
6299 echo_area_buffer[this_one] = Qnil;
6300 }
6301
6302 /* Choose a suitable buffer from echo_buffer[] is we don't
6303 have one. */
6304 if (NILP (echo_area_buffer[this_one]))
6305 {
6306 echo_area_buffer[this_one]
6307 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
6308 ? echo_buffer[the_other]
6309 : echo_buffer[this_one]);
6310 clear_buffer_p = 1;
6311 }
6312
6313 buffer = echo_area_buffer[this_one];
6314
6315 /* Don't get confused by reusing the buffer used for echoing
6316 for a different purpose. */
6317 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
6318 cancel_echoing ();
6319
6320 record_unwind_protect (unwind_with_echo_area_buffer,
6321 with_echo_area_buffer_unwind_data (w));
6322
6323 /* Make the echo area buffer current. Note that for display
6324 purposes, it is not necessary that the displayed window's buffer
6325 == current_buffer, except for text property lookup. So, let's
6326 only set that buffer temporarily here without doing a full
6327 Fset_window_buffer. We must also change w->pointm, though,
6328 because otherwise an assertions in unshow_buffer fails, and Emacs
6329 aborts. */
6330 set_buffer_internal_1 (XBUFFER (buffer));
6331 if (w)
6332 {
6333 w->buffer = buffer;
6334 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
6335 }
6336
6337 current_buffer->undo_list = Qt;
6338 current_buffer->read_only = Qnil;
6339 specbind (Qinhibit_read_only, Qt);
6340 specbind (Qinhibit_modification_hooks, Qt);
6341
6342 if (clear_buffer_p && Z > BEG)
6343 del_range (BEG, Z);
6344
6345 xassert (BEGV >= BEG);
6346 xassert (ZV <= Z && ZV >= BEGV);
6347
6348 rc = fn (a1, a2, a3, a4);
6349
6350 xassert (BEGV >= BEG);
6351 xassert (ZV <= Z && ZV >= BEGV);
6352
6353 unbind_to (count, Qnil);
6354 return rc;
6355 }
6356
6357
6358 /* Save state that should be preserved around the call to the function
6359 FN called in with_echo_area_buffer. */
6360
6361 static Lisp_Object
6362 with_echo_area_buffer_unwind_data (w)
6363 struct window *w;
6364 {
6365 int i = 0;
6366 Lisp_Object vector;
6367
6368 /* Reduce consing by keeping one vector in
6369 Vwith_echo_area_save_vector. */
6370 vector = Vwith_echo_area_save_vector;
6371 Vwith_echo_area_save_vector = Qnil;
6372
6373 if (NILP (vector))
6374 vector = Fmake_vector (make_number (7), Qnil);
6375
6376 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
6377 AREF (vector, i) = Vdeactivate_mark, ++i;
6378 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
6379
6380 if (w)
6381 {
6382 XSETWINDOW (AREF (vector, i), w); ++i;
6383 AREF (vector, i) = w->buffer; ++i;
6384 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
6385 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
6386 }
6387 else
6388 {
6389 int end = i + 4;
6390 for (; i < end; ++i)
6391 AREF (vector, i) = Qnil;
6392 }
6393
6394 xassert (i == ASIZE (vector));
6395 return vector;
6396 }
6397
6398
6399 /* Restore global state from VECTOR which was created by
6400 with_echo_area_buffer_unwind_data. */
6401
6402 static Lisp_Object
6403 unwind_with_echo_area_buffer (vector)
6404 Lisp_Object vector;
6405 {
6406 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
6407 Vdeactivate_mark = AREF (vector, 1);
6408 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
6409
6410 if (WINDOWP (AREF (vector, 3)))
6411 {
6412 struct window *w;
6413 Lisp_Object buffer, charpos, bytepos;
6414
6415 w = XWINDOW (AREF (vector, 3));
6416 buffer = AREF (vector, 4);
6417 charpos = AREF (vector, 5);
6418 bytepos = AREF (vector, 6);
6419
6420 w->buffer = buffer;
6421 set_marker_both (w->pointm, buffer,
6422 XFASTINT (charpos), XFASTINT (bytepos));
6423 }
6424
6425 Vwith_echo_area_save_vector = vector;
6426 return Qnil;
6427 }
6428
6429
6430 /* Set up the echo area for use by print functions. MULTIBYTE_P
6431 non-zero means we will print multibyte. */
6432
6433 void
6434 setup_echo_area_for_printing (multibyte_p)
6435 int multibyte_p;
6436 {
6437 ensure_echo_area_buffers ();
6438
6439 if (!message_buf_print)
6440 {
6441 /* A message has been output since the last time we printed.
6442 Choose a fresh echo area buffer. */
6443 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6444 echo_area_buffer[0] = echo_buffer[1];
6445 else
6446 echo_area_buffer[0] = echo_buffer[0];
6447
6448 /* Switch to that buffer and clear it. */
6449 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6450 current_buffer->truncate_lines = Qnil;
6451
6452 if (Z > BEG)
6453 {
6454 int count = SPECPDL_INDEX ();
6455 specbind (Qinhibit_read_only, Qt);
6456 /* Note that undo recording is always disabled. */
6457 del_range (BEG, Z);
6458 unbind_to (count, Qnil);
6459 }
6460 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6461
6462 /* Set up the buffer for the multibyteness we need. */
6463 if (multibyte_p
6464 != !NILP (current_buffer->enable_multibyte_characters))
6465 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
6466
6467 /* Raise the frame containing the echo area. */
6468 if (minibuffer_auto_raise)
6469 {
6470 struct frame *sf = SELECTED_FRAME ();
6471 Lisp_Object mini_window;
6472 mini_window = FRAME_MINIBUF_WINDOW (sf);
6473 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6474 }
6475
6476 message_log_maybe_newline ();
6477 message_buf_print = 1;
6478 }
6479 else
6480 {
6481 if (NILP (echo_area_buffer[0]))
6482 {
6483 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6484 echo_area_buffer[0] = echo_buffer[1];
6485 else
6486 echo_area_buffer[0] = echo_buffer[0];
6487 }
6488
6489 if (current_buffer != XBUFFER (echo_area_buffer[0]))
6490 {
6491 /* Someone switched buffers between print requests. */
6492 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6493 current_buffer->truncate_lines = Qnil;
6494 }
6495 }
6496 }
6497
6498
6499 /* Display an echo area message in window W. Value is non-zero if W's
6500 height is changed. If display_last_displayed_message_p is
6501 non-zero, display the message that was last displayed, otherwise
6502 display the current message. */
6503
6504 static int
6505 display_echo_area (w)
6506 struct window *w;
6507 {
6508 int i, no_message_p, window_height_changed_p, count;
6509
6510 /* Temporarily disable garbage collections while displaying the echo
6511 area. This is done because a GC can print a message itself.
6512 That message would modify the echo area buffer's contents while a
6513 redisplay of the buffer is going on, and seriously confuse
6514 redisplay. */
6515 count = inhibit_garbage_collection ();
6516
6517 /* If there is no message, we must call display_echo_area_1
6518 nevertheless because it resizes the window. But we will have to
6519 reset the echo_area_buffer in question to nil at the end because
6520 with_echo_area_buffer will sets it to an empty buffer. */
6521 i = display_last_displayed_message_p ? 1 : 0;
6522 no_message_p = NILP (echo_area_buffer[i]);
6523
6524 window_height_changed_p
6525 = with_echo_area_buffer (w, display_last_displayed_message_p,
6526 display_echo_area_1,
6527 (EMACS_INT) w, Qnil, 0, 0);
6528
6529 if (no_message_p)
6530 echo_area_buffer[i] = Qnil;
6531
6532 unbind_to (count, Qnil);
6533 return window_height_changed_p;
6534 }
6535
6536
6537 /* Helper for display_echo_area. Display the current buffer which
6538 contains the current echo area message in window W, a mini-window,
6539 a pointer to which is passed in A1. A2..A4 are currently not used.
6540 Change the height of W so that all of the message is displayed.
6541 Value is non-zero if height of W was changed. */
6542
6543 static int
6544 display_echo_area_1 (a1, a2, a3, a4)
6545 EMACS_INT a1;
6546 Lisp_Object a2;
6547 EMACS_INT a3, a4;
6548 {
6549 struct window *w = (struct window *) a1;
6550 Lisp_Object window;
6551 struct text_pos start;
6552 int window_height_changed_p = 0;
6553
6554 /* Do this before displaying, so that we have a large enough glyph
6555 matrix for the display. */
6556 window_height_changed_p = resize_mini_window (w, 0);
6557
6558 /* Display. */
6559 clear_glyph_matrix (w->desired_matrix);
6560 XSETWINDOW (window, w);
6561 SET_TEXT_POS (start, BEG, BEG_BYTE);
6562 try_window (window, start);
6563
6564 return window_height_changed_p;
6565 }
6566
6567
6568 /* Resize the echo area window to exactly the size needed for the
6569 currently displayed message, if there is one. If a mini-buffer
6570 is active, don't shrink it. */
6571
6572 void
6573 resize_echo_area_exactly ()
6574 {
6575 if (BUFFERP (echo_area_buffer[0])
6576 && WINDOWP (echo_area_window))
6577 {
6578 struct window *w = XWINDOW (echo_area_window);
6579 int resized_p;
6580 Lisp_Object resize_exactly;
6581
6582 if (minibuf_level == 0)
6583 resize_exactly = Qt;
6584 else
6585 resize_exactly = Qnil;
6586
6587 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
6588 (EMACS_INT) w, resize_exactly, 0, 0);
6589 if (resized_p)
6590 {
6591 ++windows_or_buffers_changed;
6592 ++update_mode_lines;
6593 redisplay_internal (0);
6594 }
6595 }
6596 }
6597
6598
6599 /* Callback function for with_echo_area_buffer, when used from
6600 resize_echo_area_exactly. A1 contains a pointer to the window to
6601 resize, EXACTLY non-nil means resize the mini-window exactly to the
6602 size of the text displayed. A3 and A4 are not used. Value is what
6603 resize_mini_window returns. */
6604
6605 static int
6606 resize_mini_window_1 (a1, exactly, a3, a4)
6607 EMACS_INT a1;
6608 Lisp_Object exactly;
6609 EMACS_INT a3, a4;
6610 {
6611 return resize_mini_window ((struct window *) a1, !NILP (exactly));
6612 }
6613
6614
6615 /* Resize mini-window W to fit the size of its contents. EXACT:P
6616 means size the window exactly to the size needed. Otherwise, it's
6617 only enlarged until W's buffer is empty. Value is non-zero if
6618 the window height has been changed. */
6619
6620 int
6621 resize_mini_window (w, exact_p)
6622 struct window *w;
6623 int exact_p;
6624 {
6625 struct frame *f = XFRAME (w->frame);
6626 int window_height_changed_p = 0;
6627
6628 xassert (MINI_WINDOW_P (w));
6629
6630 /* Don't resize windows while redisplaying a window; it would
6631 confuse redisplay functions when the size of the window they are
6632 displaying changes from under them. Such a resizing can happen,
6633 for instance, when which-func prints a long message while
6634 we are running fontification-functions. We're running these
6635 functions with safe_call which binds inhibit-redisplay to t. */
6636 if (!NILP (Vinhibit_redisplay))
6637 return 0;
6638
6639 /* Nil means don't try to resize. */
6640 if (NILP (Vresize_mini_windows)
6641 || (FRAME_X_P (f) && f->output_data.x == NULL))
6642 return 0;
6643
6644 if (!FRAME_MINIBUF_ONLY_P (f))
6645 {
6646 struct it it;
6647 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
6648 int total_height = XFASTINT (root->height) + XFASTINT (w->height);
6649 int height, max_height;
6650 int unit = CANON_Y_UNIT (f);
6651 struct text_pos start;
6652 struct buffer *old_current_buffer = NULL;
6653
6654 if (current_buffer != XBUFFER (w->buffer))
6655 {
6656 old_current_buffer = current_buffer;
6657 set_buffer_internal (XBUFFER (w->buffer));
6658 }
6659
6660 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
6661
6662 /* Compute the max. number of lines specified by the user. */
6663 if (FLOATP (Vmax_mini_window_height))
6664 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_HEIGHT (f);
6665 else if (INTEGERP (Vmax_mini_window_height))
6666 max_height = XINT (Vmax_mini_window_height);
6667 else
6668 max_height = total_height / 4;
6669
6670 /* Correct that max. height if it's bogus. */
6671 max_height = max (1, max_height);
6672 max_height = min (total_height, max_height);
6673
6674 /* Find out the height of the text in the window. */
6675 if (it.truncate_lines_p)
6676 height = 1;
6677 else
6678 {
6679 last_height = 0;
6680 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
6681 if (it.max_ascent == 0 && it.max_descent == 0)
6682 height = it.current_y + last_height;
6683 else
6684 height = it.current_y + it.max_ascent + it.max_descent;
6685 height -= it.extra_line_spacing;
6686 height = (height + unit - 1) / unit;
6687 }
6688
6689 /* Compute a suitable window start. */
6690 if (height > max_height)
6691 {
6692 height = max_height;
6693 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
6694 move_it_vertically_backward (&it, (height - 1) * unit);
6695 start = it.current.pos;
6696 }
6697 else
6698 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
6699 SET_MARKER_FROM_TEXT_POS (w->start, start);
6700
6701 if (EQ (Vresize_mini_windows, Qgrow_only))
6702 {
6703 /* Let it grow only, until we display an empty message, in which
6704 case the window shrinks again. */
6705 if (height > XFASTINT (w->height))
6706 {
6707 int old_height = XFASTINT (w->height);
6708 freeze_window_starts (f, 1);
6709 grow_mini_window (w, height - XFASTINT (w->height));
6710 window_height_changed_p = XFASTINT (w->height) != old_height;
6711 }
6712 else if (height < XFASTINT (w->height)
6713 && (exact_p || BEGV == ZV))
6714 {
6715 int old_height = XFASTINT (w->height);
6716 freeze_window_starts (f, 0);
6717 shrink_mini_window (w);
6718 window_height_changed_p = XFASTINT (w->height) != old_height;
6719 }
6720 }
6721 else
6722 {
6723 /* Always resize to exact size needed. */
6724 if (height > XFASTINT (w->height))
6725 {
6726 int old_height = XFASTINT (w->height);
6727 freeze_window_starts (f, 1);
6728 grow_mini_window (w, height - XFASTINT (w->height));
6729 window_height_changed_p = XFASTINT (w->height) != old_height;
6730 }
6731 else if (height < XFASTINT (w->height))
6732 {
6733 int old_height = XFASTINT (w->height);
6734 freeze_window_starts (f, 0);
6735 shrink_mini_window (w);
6736
6737 if (height)
6738 {
6739 freeze_window_starts (f, 1);
6740 grow_mini_window (w, height - XFASTINT (w->height));
6741 }
6742
6743 window_height_changed_p = XFASTINT (w->height) != old_height;
6744 }
6745 }
6746
6747 if (old_current_buffer)
6748 set_buffer_internal (old_current_buffer);
6749 }
6750
6751 return window_height_changed_p;
6752 }
6753
6754
6755 /* Value is the current message, a string, or nil if there is no
6756 current message. */
6757
6758 Lisp_Object
6759 current_message ()
6760 {
6761 Lisp_Object msg;
6762
6763 if (NILP (echo_area_buffer[0]))
6764 msg = Qnil;
6765 else
6766 {
6767 with_echo_area_buffer (0, 0, current_message_1,
6768 (EMACS_INT) &msg, Qnil, 0, 0);
6769 if (NILP (msg))
6770 echo_area_buffer[0] = Qnil;
6771 }
6772
6773 return msg;
6774 }
6775
6776
6777 static int
6778 current_message_1 (a1, a2, a3, a4)
6779 EMACS_INT a1;
6780 Lisp_Object a2;
6781 EMACS_INT a3, a4;
6782 {
6783 Lisp_Object *msg = (Lisp_Object *) a1;
6784
6785 if (Z > BEG)
6786 *msg = make_buffer_string (BEG, Z, 1);
6787 else
6788 *msg = Qnil;
6789 return 0;
6790 }
6791
6792
6793 /* Push the current message on Vmessage_stack for later restauration
6794 by restore_message. Value is non-zero if the current message isn't
6795 empty. This is a relatively infrequent operation, so it's not
6796 worth optimizing. */
6797
6798 int
6799 push_message ()
6800 {
6801 Lisp_Object msg;
6802 msg = current_message ();
6803 Vmessage_stack = Fcons (msg, Vmessage_stack);
6804 return STRINGP (msg);
6805 }
6806
6807
6808 /* Handler for record_unwind_protect calling pop_message. */
6809
6810 Lisp_Object
6811 push_message_unwind (dummy)
6812 Lisp_Object dummy;
6813 {
6814 pop_message ();
6815 return Qnil;
6816 }
6817
6818
6819 /* Restore message display from the top of Vmessage_stack. */
6820
6821 void
6822 restore_message ()
6823 {
6824 Lisp_Object msg;
6825
6826 xassert (CONSP (Vmessage_stack));
6827 msg = XCAR (Vmessage_stack);
6828 if (STRINGP (msg))
6829 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
6830 else
6831 message3_nolog (msg, 0, 0);
6832 }
6833
6834
6835 /* Pop the top-most entry off Vmessage_stack. */
6836
6837 void
6838 pop_message ()
6839 {
6840 xassert (CONSP (Vmessage_stack));
6841 Vmessage_stack = XCDR (Vmessage_stack);
6842 }
6843
6844
6845 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
6846 exits. If the stack is not empty, we have a missing pop_message
6847 somewhere. */
6848
6849 void
6850 check_message_stack ()
6851 {
6852 if (!NILP (Vmessage_stack))
6853 abort ();
6854 }
6855
6856
6857 /* Truncate to NCHARS what will be displayed in the echo area the next
6858 time we display it---but don't redisplay it now. */
6859
6860 void
6861 truncate_echo_area (nchars)
6862 int nchars;
6863 {
6864 if (nchars == 0)
6865 echo_area_buffer[0] = Qnil;
6866 /* A null message buffer means that the frame hasn't really been
6867 initialized yet. Error messages get reported properly by
6868 cmd_error, so this must be just an informative message; toss it. */
6869 else if (!noninteractive
6870 && INTERACTIVE
6871 && !NILP (echo_area_buffer[0]))
6872 {
6873 struct frame *sf = SELECTED_FRAME ();
6874 if (FRAME_MESSAGE_BUF (sf))
6875 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
6876 }
6877 }
6878
6879
6880 /* Helper function for truncate_echo_area. Truncate the current
6881 message to at most NCHARS characters. */
6882
6883 static int
6884 truncate_message_1 (nchars, a2, a3, a4)
6885 EMACS_INT nchars;
6886 Lisp_Object a2;
6887 EMACS_INT a3, a4;
6888 {
6889 if (BEG + nchars < Z)
6890 del_range (BEG + nchars, Z);
6891 if (Z == BEG)
6892 echo_area_buffer[0] = Qnil;
6893 return 0;
6894 }
6895
6896
6897 /* Set the current message to a substring of S or STRING.
6898
6899 If STRING is a Lisp string, set the message to the first NBYTES
6900 bytes from STRING. NBYTES zero means use the whole string. If
6901 STRING is multibyte, the message will be displayed multibyte.
6902
6903 If S is not null, set the message to the first LEN bytes of S. LEN
6904 zero means use the whole string. MULTIBYTE_P non-zero means S is
6905 multibyte. Display the message multibyte in that case. */
6906
6907 void
6908 set_message (s, string, nbytes, multibyte_p)
6909 const char *s;
6910 Lisp_Object string;
6911 int nbytes;
6912 {
6913 message_enable_multibyte
6914 = ((s && multibyte_p)
6915 || (STRINGP (string) && STRING_MULTIBYTE (string)));
6916
6917 with_echo_area_buffer (0, -1, set_message_1,
6918 (EMACS_INT) s, string, nbytes, multibyte_p);
6919 message_buf_print = 0;
6920 help_echo_showing_p = 0;
6921 }
6922
6923
6924 /* Helper function for set_message. Arguments have the same meaning
6925 as there, with A1 corresponding to S and A2 corresponding to STRING
6926 This function is called with the echo area buffer being
6927 current. */
6928
6929 static int
6930 set_message_1 (a1, a2, nbytes, multibyte_p)
6931 EMACS_INT a1;
6932 Lisp_Object a2;
6933 EMACS_INT nbytes, multibyte_p;
6934 {
6935 const char *s = (const char *) a1;
6936 Lisp_Object string = a2;
6937
6938 xassert (BEG == Z);
6939
6940 /* Change multibyteness of the echo buffer appropriately. */
6941 if (message_enable_multibyte
6942 != !NILP (current_buffer->enable_multibyte_characters))
6943 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
6944
6945 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
6946
6947 /* Insert new message at BEG. */
6948 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6949
6950 if (STRINGP (string))
6951 {
6952 int nchars;
6953
6954 if (nbytes == 0)
6955 nbytes = SBYTES (string);
6956 nchars = string_byte_to_char (string, nbytes);
6957
6958 /* This function takes care of single/multibyte conversion. We
6959 just have to ensure that the echo area buffer has the right
6960 setting of enable_multibyte_characters. */
6961 insert_from_string (string, 0, 0, nchars, nbytes, 1);
6962 }
6963 else if (s)
6964 {
6965 if (nbytes == 0)
6966 nbytes = strlen (s);
6967
6968 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
6969 {
6970 /* Convert from multi-byte to single-byte. */
6971 int i, c, n;
6972 unsigned char work[1];
6973
6974 /* Convert a multibyte string to single-byte. */
6975 for (i = 0; i < nbytes; i += n)
6976 {
6977 c = string_char_and_length (s + i, nbytes - i, &n);
6978 work[0] = (SINGLE_BYTE_CHAR_P (c)
6979 ? c
6980 : multibyte_char_to_unibyte (c, Qnil));
6981 insert_1_both (work, 1, 1, 1, 0, 0);
6982 }
6983 }
6984 else if (!multibyte_p
6985 && !NILP (current_buffer->enable_multibyte_characters))
6986 {
6987 /* Convert from single-byte to multi-byte. */
6988 int i, c, n;
6989 const unsigned char *msg = (const unsigned char *) s;
6990 unsigned char str[MAX_MULTIBYTE_LENGTH];
6991
6992 /* Convert a single-byte string to multibyte. */
6993 for (i = 0; i < nbytes; i++)
6994 {
6995 c = unibyte_char_to_multibyte (msg[i]);
6996 n = CHAR_STRING (c, str);
6997 insert_1_both (str, 1, n, 1, 0, 0);
6998 }
6999 }
7000 else
7001 insert_1 (s, nbytes, 1, 0, 0);
7002 }
7003
7004 return 0;
7005 }
7006
7007
7008 /* Clear messages. CURRENT_P non-zero means clear the current
7009 message. LAST_DISPLAYED_P non-zero means clear the message
7010 last displayed. */
7011
7012 void
7013 clear_message (current_p, last_displayed_p)
7014 int current_p, last_displayed_p;
7015 {
7016 if (current_p)
7017 {
7018 echo_area_buffer[0] = Qnil;
7019 message_cleared_p = 1;
7020 }
7021
7022 if (last_displayed_p)
7023 echo_area_buffer[1] = Qnil;
7024
7025 message_buf_print = 0;
7026 }
7027
7028 /* Clear garbaged frames.
7029
7030 This function is used where the old redisplay called
7031 redraw_garbaged_frames which in turn called redraw_frame which in
7032 turn called clear_frame. The call to clear_frame was a source of
7033 flickering. I believe a clear_frame is not necessary. It should
7034 suffice in the new redisplay to invalidate all current matrices,
7035 and ensure a complete redisplay of all windows. */
7036
7037 static void
7038 clear_garbaged_frames ()
7039 {
7040 if (frame_garbaged)
7041 {
7042 Lisp_Object tail, frame;
7043 int changed_count = 0;
7044
7045 FOR_EACH_FRAME (tail, frame)
7046 {
7047 struct frame *f = XFRAME (frame);
7048
7049 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
7050 {
7051 if (f->resized_p)
7052 Fredraw_frame (frame);
7053 clear_current_matrices (f);
7054 changed_count++;
7055 f->garbaged = 0;
7056 f->resized_p = 0;
7057 }
7058 }
7059
7060 frame_garbaged = 0;
7061 if (changed_count)
7062 ++windows_or_buffers_changed;
7063 }
7064 }
7065
7066
7067 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
7068 is non-zero update selected_frame. Value is non-zero if the
7069 mini-windows height has been changed. */
7070
7071 static int
7072 echo_area_display (update_frame_p)
7073 int update_frame_p;
7074 {
7075 Lisp_Object mini_window;
7076 struct window *w;
7077 struct frame *f;
7078 int window_height_changed_p = 0;
7079 struct frame *sf = SELECTED_FRAME ();
7080
7081 mini_window = FRAME_MINIBUF_WINDOW (sf);
7082 w = XWINDOW (mini_window);
7083 f = XFRAME (WINDOW_FRAME (w));
7084
7085 /* Don't display if frame is invisible or not yet initialized. */
7086 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
7087 return 0;
7088
7089 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
7090 #ifndef MAC_OS8
7091 #ifdef HAVE_WINDOW_SYSTEM
7092 /* When Emacs starts, selected_frame may be a visible terminal
7093 frame, even if we run under a window system. If we let this
7094 through, a message would be displayed on the terminal. */
7095 if (EQ (selected_frame, Vterminal_frame)
7096 && !NILP (Vwindow_system))
7097 return 0;
7098 #endif /* HAVE_WINDOW_SYSTEM */
7099 #endif
7100
7101 /* Redraw garbaged frames. */
7102 if (frame_garbaged)
7103 clear_garbaged_frames ();
7104
7105 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
7106 {
7107 echo_area_window = mini_window;
7108 window_height_changed_p = display_echo_area (w);
7109 w->must_be_updated_p = 1;
7110
7111 /* Update the display, unless called from redisplay_internal.
7112 Also don't update the screen during redisplay itself. The
7113 update will happen at the end of redisplay, and an update
7114 here could cause confusion. */
7115 if (update_frame_p && !redisplaying_p)
7116 {
7117 int n = 0;
7118
7119 /* If the display update has been interrupted by pending
7120 input, update mode lines in the frame. Due to the
7121 pending input, it might have been that redisplay hasn't
7122 been called, so that mode lines above the echo area are
7123 garbaged. This looks odd, so we prevent it here. */
7124 if (!display_completed)
7125 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
7126
7127 if (window_height_changed_p
7128 /* Don't do this if Emacs is shutting down. Redisplay
7129 needs to run hooks. */
7130 && !NILP (Vrun_hooks))
7131 {
7132 /* Must update other windows. Likewise as in other
7133 cases, don't let this update be interrupted by
7134 pending input. */
7135 int count = SPECPDL_INDEX ();
7136 specbind (Qredisplay_dont_pause, Qt);
7137 windows_or_buffers_changed = 1;
7138 redisplay_internal (0);
7139 unbind_to (count, Qnil);
7140 }
7141 else if (FRAME_WINDOW_P (f) && n == 0)
7142 {
7143 /* Window configuration is the same as before.
7144 Can do with a display update of the echo area,
7145 unless we displayed some mode lines. */
7146 update_single_window (w, 1);
7147 rif->flush_display (f);
7148 }
7149 else
7150 update_frame (f, 1, 1);
7151
7152 /* If cursor is in the echo area, make sure that the next
7153 redisplay displays the minibuffer, so that the cursor will
7154 be replaced with what the minibuffer wants. */
7155 if (cursor_in_echo_area)
7156 ++windows_or_buffers_changed;
7157 }
7158 }
7159 else if (!EQ (mini_window, selected_window))
7160 windows_or_buffers_changed++;
7161
7162 /* Last displayed message is now the current message. */
7163 echo_area_buffer[1] = echo_area_buffer[0];
7164
7165 /* Prevent redisplay optimization in redisplay_internal by resetting
7166 this_line_start_pos. This is done because the mini-buffer now
7167 displays the message instead of its buffer text. */
7168 if (EQ (mini_window, selected_window))
7169 CHARPOS (this_line_start_pos) = 0;
7170
7171 return window_height_changed_p;
7172 }
7173
7174
7175 \f
7176 /***********************************************************************
7177 Frame Titles
7178 ***********************************************************************/
7179
7180
7181 /* The frame title buffering code is also used by Fformat_mode_line.
7182 So it is not conditioned by HAVE_WINDOW_SYSTEM. */
7183
7184 /* A buffer for constructing frame titles in it; allocated from the
7185 heap in init_xdisp and resized as needed in store_frame_title_char. */
7186
7187 static char *frame_title_buf;
7188
7189 /* The buffer's end, and a current output position in it. */
7190
7191 static char *frame_title_buf_end;
7192 static char *frame_title_ptr;
7193
7194
7195 /* Store a single character C for the frame title in frame_title_buf.
7196 Re-allocate frame_title_buf if necessary. */
7197
7198 static void
7199 store_frame_title_char (c)
7200 char c;
7201 {
7202 /* If output position has reached the end of the allocated buffer,
7203 double the buffer's size. */
7204 if (frame_title_ptr == frame_title_buf_end)
7205 {
7206 int len = frame_title_ptr - frame_title_buf;
7207 int new_size = 2 * len * sizeof *frame_title_buf;
7208 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
7209 frame_title_buf_end = frame_title_buf + new_size;
7210 frame_title_ptr = frame_title_buf + len;
7211 }
7212
7213 *frame_title_ptr++ = c;
7214 }
7215
7216
7217 /* Store part of a frame title in frame_title_buf, beginning at
7218 frame_title_ptr. STR is the string to store. Do not copy
7219 characters that yield more columns than PRECISION; PRECISION <= 0
7220 means copy the whole string. Pad with spaces until FIELD_WIDTH
7221 number of characters have been copied; FIELD_WIDTH <= 0 means don't
7222 pad. Called from display_mode_element when it is used to build a
7223 frame title. */
7224
7225 static int
7226 store_frame_title (str, field_width, precision)
7227 const unsigned char *str;
7228 int field_width, precision;
7229 {
7230 int n = 0;
7231 int dummy, nbytes;
7232
7233 /* Copy at most PRECISION chars from STR. */
7234 nbytes = strlen (str);
7235 n+= c_string_width (str, nbytes, precision, &dummy, &nbytes);
7236 while (nbytes--)
7237 store_frame_title_char (*str++);
7238
7239 /* Fill up with spaces until FIELD_WIDTH reached. */
7240 while (field_width > 0
7241 && n < field_width)
7242 {
7243 store_frame_title_char (' ');
7244 ++n;
7245 }
7246
7247 return n;
7248 }
7249
7250 #ifdef HAVE_WINDOW_SYSTEM
7251
7252 /* Set the title of FRAME, if it has changed. The title format is
7253 Vicon_title_format if FRAME is iconified, otherwise it is
7254 frame_title_format. */
7255
7256 static void
7257 x_consider_frame_title (frame)
7258 Lisp_Object frame;
7259 {
7260 struct frame *f = XFRAME (frame);
7261
7262 if (FRAME_WINDOW_P (f)
7263 || FRAME_MINIBUF_ONLY_P (f)
7264 || f->explicit_name)
7265 {
7266 /* Do we have more than one visible frame on this X display? */
7267 Lisp_Object tail;
7268 Lisp_Object fmt;
7269 struct buffer *obuf;
7270 int len;
7271 struct it it;
7272
7273 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
7274 {
7275 Lisp_Object other_frame = XCAR (tail);
7276 struct frame *tf = XFRAME (other_frame);
7277
7278 if (tf != f
7279 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
7280 && !FRAME_MINIBUF_ONLY_P (tf)
7281 && !EQ (other_frame, tip_frame)
7282 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
7283 break;
7284 }
7285
7286 /* Set global variable indicating that multiple frames exist. */
7287 multiple_frames = CONSP (tail);
7288
7289 /* Switch to the buffer of selected window of the frame. Set up
7290 frame_title_ptr so that display_mode_element will output into it;
7291 then display the title. */
7292 obuf = current_buffer;
7293 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
7294 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
7295 frame_title_ptr = frame_title_buf;
7296 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
7297 NULL, DEFAULT_FACE_ID);
7298 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
7299 len = frame_title_ptr - frame_title_buf;
7300 frame_title_ptr = NULL;
7301 set_buffer_internal_1 (obuf);
7302
7303 /* Set the title only if it's changed. This avoids consing in
7304 the common case where it hasn't. (If it turns out that we've
7305 already wasted too much time by walking through the list with
7306 display_mode_element, then we might need to optimize at a
7307 higher level than this.) */
7308 if (! STRINGP (f->name)
7309 || SBYTES (f->name) != len
7310 || bcmp (frame_title_buf, SDATA (f->name), len) != 0)
7311 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
7312 }
7313 }
7314
7315 #endif /* not HAVE_WINDOW_SYSTEM */
7316
7317
7318
7319 \f
7320 /***********************************************************************
7321 Menu Bars
7322 ***********************************************************************/
7323
7324
7325 /* Prepare for redisplay by updating menu-bar item lists when
7326 appropriate. This can call eval. */
7327
7328 void
7329 prepare_menu_bars ()
7330 {
7331 int all_windows;
7332 struct gcpro gcpro1, gcpro2;
7333 struct frame *f;
7334 Lisp_Object tooltip_frame;
7335
7336 #ifdef HAVE_WINDOW_SYSTEM
7337 tooltip_frame = tip_frame;
7338 #else
7339 tooltip_frame = Qnil;
7340 #endif
7341
7342 /* Update all frame titles based on their buffer names, etc. We do
7343 this before the menu bars so that the buffer-menu will show the
7344 up-to-date frame titles. */
7345 #ifdef HAVE_WINDOW_SYSTEM
7346 if (windows_or_buffers_changed || update_mode_lines)
7347 {
7348 Lisp_Object tail, frame;
7349
7350 FOR_EACH_FRAME (tail, frame)
7351 {
7352 f = XFRAME (frame);
7353 if (!EQ (frame, tooltip_frame)
7354 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
7355 x_consider_frame_title (frame);
7356 }
7357 }
7358 #endif /* HAVE_WINDOW_SYSTEM */
7359
7360 /* Update the menu bar item lists, if appropriate. This has to be
7361 done before any actual redisplay or generation of display lines. */
7362 all_windows = (update_mode_lines
7363 || buffer_shared > 1
7364 || windows_or_buffers_changed);
7365 if (all_windows)
7366 {
7367 Lisp_Object tail, frame;
7368 int count = SPECPDL_INDEX ();
7369
7370 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7371
7372 FOR_EACH_FRAME (tail, frame)
7373 {
7374 f = XFRAME (frame);
7375
7376 /* Ignore tooltip frame. */
7377 if (EQ (frame, tooltip_frame))
7378 continue;
7379
7380 /* If a window on this frame changed size, report that to
7381 the user and clear the size-change flag. */
7382 if (FRAME_WINDOW_SIZES_CHANGED (f))
7383 {
7384 Lisp_Object functions;
7385
7386 /* Clear flag first in case we get an error below. */
7387 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
7388 functions = Vwindow_size_change_functions;
7389 GCPRO2 (tail, functions);
7390
7391 while (CONSP (functions))
7392 {
7393 call1 (XCAR (functions), frame);
7394 functions = XCDR (functions);
7395 }
7396 UNGCPRO;
7397 }
7398
7399 GCPRO1 (tail);
7400 update_menu_bar (f, 0);
7401 #ifdef HAVE_WINDOW_SYSTEM
7402 update_tool_bar (f, 0);
7403 #endif
7404 UNGCPRO;
7405 }
7406
7407 unbind_to (count, Qnil);
7408 }
7409 else
7410 {
7411 struct frame *sf = SELECTED_FRAME ();
7412 update_menu_bar (sf, 1);
7413 #ifdef HAVE_WINDOW_SYSTEM
7414 update_tool_bar (sf, 1);
7415 #endif
7416 }
7417
7418 /* Motif needs this. See comment in xmenu.c. Turn it off when
7419 pending_menu_activation is not defined. */
7420 #ifdef USE_X_TOOLKIT
7421 pending_menu_activation = 0;
7422 #endif
7423 }
7424
7425
7426 /* Update the menu bar item list for frame F. This has to be done
7427 before we start to fill in any display lines, because it can call
7428 eval.
7429
7430 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
7431
7432 static void
7433 update_menu_bar (f, save_match_data)
7434 struct frame *f;
7435 int save_match_data;
7436 {
7437 Lisp_Object window;
7438 register struct window *w;
7439
7440 /* If called recursively during a menu update, do nothing. This can
7441 happen when, for instance, an activate-menubar-hook causes a
7442 redisplay. */
7443 if (inhibit_menubar_update)
7444 return;
7445
7446 window = FRAME_SELECTED_WINDOW (f);
7447 w = XWINDOW (window);
7448
7449 #if 0 /* The if statement below this if statement used to include the
7450 condition !NILP (w->update_mode_line), rather than using
7451 update_mode_lines directly, and this if statement may have
7452 been added to make that condition work. Now the if
7453 statement below matches its comment, this isn't needed. */
7454 if (update_mode_lines)
7455 w->update_mode_line = Qt;
7456 #endif
7457
7458 if (FRAME_WINDOW_P (f)
7459 ?
7460 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS)
7461 FRAME_EXTERNAL_MENU_BAR (f)
7462 #else
7463 FRAME_MENU_BAR_LINES (f) > 0
7464 #endif
7465 : FRAME_MENU_BAR_LINES (f) > 0)
7466 {
7467 /* If the user has switched buffers or windows, we need to
7468 recompute to reflect the new bindings. But we'll
7469 recompute when update_mode_lines is set too; that means
7470 that people can use force-mode-line-update to request
7471 that the menu bar be recomputed. The adverse effect on
7472 the rest of the redisplay algorithm is about the same as
7473 windows_or_buffers_changed anyway. */
7474 if (windows_or_buffers_changed
7475 /* This used to test w->update_mode_line, but we believe
7476 there is no need to recompute the menu in that case. */
7477 || update_mode_lines
7478 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7479 < BUF_MODIFF (XBUFFER (w->buffer)))
7480 != !NILP (w->last_had_star))
7481 || ((!NILP (Vtransient_mark_mode)
7482 && !NILP (XBUFFER (w->buffer)->mark_active))
7483 != !NILP (w->region_showing)))
7484 {
7485 struct buffer *prev = current_buffer;
7486 int count = SPECPDL_INDEX ();
7487
7488 specbind (Qinhibit_menubar_update, Qt);
7489
7490 set_buffer_internal_1 (XBUFFER (w->buffer));
7491 if (save_match_data)
7492 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7493 if (NILP (Voverriding_local_map_menu_flag))
7494 {
7495 specbind (Qoverriding_terminal_local_map, Qnil);
7496 specbind (Qoverriding_local_map, Qnil);
7497 }
7498
7499 /* Run the Lucid hook. */
7500 safe_run_hooks (Qactivate_menubar_hook);
7501
7502 /* If it has changed current-menubar from previous value,
7503 really recompute the menu-bar from the value. */
7504 if (! NILP (Vlucid_menu_bar_dirty_flag))
7505 call0 (Qrecompute_lucid_menubar);
7506
7507 safe_run_hooks (Qmenu_bar_update_hook);
7508 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
7509
7510 /* Redisplay the menu bar in case we changed it. */
7511 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS)
7512 if (FRAME_WINDOW_P (f)
7513 #if defined (MAC_OS)
7514 /* All frames on Mac OS share the same menubar. So only the
7515 selected frame should be allowed to set it. */
7516 && f == SELECTED_FRAME ()
7517 #endif
7518 )
7519 set_frame_menubar (f, 0, 0);
7520 else
7521 /* On a terminal screen, the menu bar is an ordinary screen
7522 line, and this makes it get updated. */
7523 w->update_mode_line = Qt;
7524 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7525 /* In the non-toolkit version, the menu bar is an ordinary screen
7526 line, and this makes it get updated. */
7527 w->update_mode_line = Qt;
7528 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7529
7530 unbind_to (count, Qnil);
7531 set_buffer_internal_1 (prev);
7532 }
7533 }
7534 }
7535
7536
7537 \f
7538 /***********************************************************************
7539 Tool-bars
7540 ***********************************************************************/
7541
7542 #ifdef HAVE_WINDOW_SYSTEM
7543
7544 /* Update the tool-bar item list for frame F. This has to be done
7545 before we start to fill in any display lines. Called from
7546 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
7547 and restore it here. */
7548
7549 static void
7550 update_tool_bar (f, save_match_data)
7551 struct frame *f;
7552 int save_match_data;
7553 {
7554 if (WINDOWP (f->tool_bar_window)
7555 && XFASTINT (XWINDOW (f->tool_bar_window)->height) > 0)
7556 {
7557 Lisp_Object window;
7558 struct window *w;
7559
7560 window = FRAME_SELECTED_WINDOW (f);
7561 w = XWINDOW (window);
7562
7563 /* If the user has switched buffers or windows, we need to
7564 recompute to reflect the new bindings. But we'll
7565 recompute when update_mode_lines is set too; that means
7566 that people can use force-mode-line-update to request
7567 that the menu bar be recomputed. The adverse effect on
7568 the rest of the redisplay algorithm is about the same as
7569 windows_or_buffers_changed anyway. */
7570 if (windows_or_buffers_changed
7571 || !NILP (w->update_mode_line)
7572 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7573 < BUF_MODIFF (XBUFFER (w->buffer)))
7574 != !NILP (w->last_had_star))
7575 || ((!NILP (Vtransient_mark_mode)
7576 && !NILP (XBUFFER (w->buffer)->mark_active))
7577 != !NILP (w->region_showing)))
7578 {
7579 struct buffer *prev = current_buffer;
7580 int count = SPECPDL_INDEX ();
7581
7582 /* Set current_buffer to the buffer of the selected
7583 window of the frame, so that we get the right local
7584 keymaps. */
7585 set_buffer_internal_1 (XBUFFER (w->buffer));
7586
7587 /* Save match data, if we must. */
7588 if (save_match_data)
7589 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7590
7591 /* Make sure that we don't accidentally use bogus keymaps. */
7592 if (NILP (Voverriding_local_map_menu_flag))
7593 {
7594 specbind (Qoverriding_terminal_local_map, Qnil);
7595 specbind (Qoverriding_local_map, Qnil);
7596 }
7597
7598 /* Build desired tool-bar items from keymaps. */
7599 f->tool_bar_items
7600 = tool_bar_items (f->tool_bar_items, &f->n_tool_bar_items);
7601
7602 /* Redisplay the tool-bar in case we changed it. */
7603 w->update_mode_line = Qt;
7604
7605 unbind_to (count, Qnil);
7606 set_buffer_internal_1 (prev);
7607 }
7608 }
7609 }
7610
7611
7612 /* Set F->desired_tool_bar_string to a Lisp string representing frame
7613 F's desired tool-bar contents. F->tool_bar_items must have
7614 been set up previously by calling prepare_menu_bars. */
7615
7616 static void
7617 build_desired_tool_bar_string (f)
7618 struct frame *f;
7619 {
7620 int i, size, size_needed;
7621 struct gcpro gcpro1, gcpro2, gcpro3;
7622 Lisp_Object image, plist, props;
7623
7624 image = plist = props = Qnil;
7625 GCPRO3 (image, plist, props);
7626
7627 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
7628 Otherwise, make a new string. */
7629
7630 /* The size of the string we might be able to reuse. */
7631 size = (STRINGP (f->desired_tool_bar_string)
7632 ? SCHARS (f->desired_tool_bar_string)
7633 : 0);
7634
7635 /* We need one space in the string for each image. */
7636 size_needed = f->n_tool_bar_items;
7637
7638 /* Reuse f->desired_tool_bar_string, if possible. */
7639 if (size < size_needed || NILP (f->desired_tool_bar_string))
7640 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
7641 make_number (' '));
7642 else
7643 {
7644 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
7645 Fremove_text_properties (make_number (0), make_number (size),
7646 props, f->desired_tool_bar_string);
7647 }
7648
7649 /* Put a `display' property on the string for the images to display,
7650 put a `menu_item' property on tool-bar items with a value that
7651 is the index of the item in F's tool-bar item vector. */
7652 for (i = 0; i < f->n_tool_bar_items; ++i)
7653 {
7654 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
7655
7656 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
7657 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
7658 int hmargin, vmargin, relief, idx, end;
7659 extern Lisp_Object QCrelief, QCmargin, QCconversion, Qimage;
7660
7661 /* If image is a vector, choose the image according to the
7662 button state. */
7663 image = PROP (TOOL_BAR_ITEM_IMAGES);
7664 if (VECTORP (image))
7665 {
7666 if (enabled_p)
7667 idx = (selected_p
7668 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
7669 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
7670 else
7671 idx = (selected_p
7672 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
7673 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
7674
7675 xassert (ASIZE (image) >= idx);
7676 image = AREF (image, idx);
7677 }
7678 else
7679 idx = -1;
7680
7681 /* Ignore invalid image specifications. */
7682 if (!valid_image_p (image))
7683 continue;
7684
7685 /* Display the tool-bar button pressed, or depressed. */
7686 plist = Fcopy_sequence (XCDR (image));
7687
7688 /* Compute margin and relief to draw. */
7689 relief = (tool_bar_button_relief >= 0
7690 ? tool_bar_button_relief
7691 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
7692 hmargin = vmargin = relief;
7693
7694 if (INTEGERP (Vtool_bar_button_margin)
7695 && XINT (Vtool_bar_button_margin) > 0)
7696 {
7697 hmargin += XFASTINT (Vtool_bar_button_margin);
7698 vmargin += XFASTINT (Vtool_bar_button_margin);
7699 }
7700 else if (CONSP (Vtool_bar_button_margin))
7701 {
7702 if (INTEGERP (XCAR (Vtool_bar_button_margin))
7703 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
7704 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
7705
7706 if (INTEGERP (XCDR (Vtool_bar_button_margin))
7707 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
7708 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
7709 }
7710
7711 if (auto_raise_tool_bar_buttons_p)
7712 {
7713 /* Add a `:relief' property to the image spec if the item is
7714 selected. */
7715 if (selected_p)
7716 {
7717 plist = Fplist_put (plist, QCrelief, make_number (-relief));
7718 hmargin -= relief;
7719 vmargin -= relief;
7720 }
7721 }
7722 else
7723 {
7724 /* If image is selected, display it pressed, i.e. with a
7725 negative relief. If it's not selected, display it with a
7726 raised relief. */
7727 plist = Fplist_put (plist, QCrelief,
7728 (selected_p
7729 ? make_number (-relief)
7730 : make_number (relief)));
7731 hmargin -= relief;
7732 vmargin -= relief;
7733 }
7734
7735 /* Put a margin around the image. */
7736 if (hmargin || vmargin)
7737 {
7738 if (hmargin == vmargin)
7739 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
7740 else
7741 plist = Fplist_put (plist, QCmargin,
7742 Fcons (make_number (hmargin),
7743 make_number (vmargin)));
7744 }
7745
7746 /* If button is not enabled, and we don't have special images
7747 for the disabled state, make the image appear disabled by
7748 applying an appropriate algorithm to it. */
7749 if (!enabled_p && idx < 0)
7750 plist = Fplist_put (plist, QCconversion, Qdisabled);
7751
7752 /* Put a `display' text property on the string for the image to
7753 display. Put a `menu-item' property on the string that gives
7754 the start of this item's properties in the tool-bar items
7755 vector. */
7756 image = Fcons (Qimage, plist);
7757 props = list4 (Qdisplay, image,
7758 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
7759
7760 /* Let the last image hide all remaining spaces in the tool bar
7761 string. The string can be longer than needed when we reuse a
7762 previous string. */
7763 if (i + 1 == f->n_tool_bar_items)
7764 end = SCHARS (f->desired_tool_bar_string);
7765 else
7766 end = i + 1;
7767 Fadd_text_properties (make_number (i), make_number (end),
7768 props, f->desired_tool_bar_string);
7769 #undef PROP
7770 }
7771
7772 UNGCPRO;
7773 }
7774
7775
7776 /* Display one line of the tool-bar of frame IT->f. */
7777
7778 static void
7779 display_tool_bar_line (it)
7780 struct it *it;
7781 {
7782 struct glyph_row *row = it->glyph_row;
7783 int max_x = it->last_visible_x;
7784 struct glyph *last;
7785
7786 prepare_desired_row (row);
7787 row->y = it->current_y;
7788
7789 /* Note that this isn't made use of if the face hasn't a box,
7790 so there's no need to check the face here. */
7791 it->start_of_box_run_p = 1;
7792
7793 while (it->current_x < max_x)
7794 {
7795 int x_before, x, n_glyphs_before, i, nglyphs;
7796
7797 /* Get the next display element. */
7798 if (!get_next_display_element (it))
7799 break;
7800
7801 /* Produce glyphs. */
7802 x_before = it->current_x;
7803 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
7804 PRODUCE_GLYPHS (it);
7805
7806 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
7807 i = 0;
7808 x = x_before;
7809 while (i < nglyphs)
7810 {
7811 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
7812
7813 if (x + glyph->pixel_width > max_x)
7814 {
7815 /* Glyph doesn't fit on line. */
7816 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
7817 it->current_x = x;
7818 goto out;
7819 }
7820
7821 ++it->hpos;
7822 x += glyph->pixel_width;
7823 ++i;
7824 }
7825
7826 /* Stop at line ends. */
7827 if (ITERATOR_AT_END_OF_LINE_P (it))
7828 break;
7829
7830 set_iterator_to_next (it, 1);
7831 }
7832
7833 out:;
7834
7835 row->displays_text_p = row->used[TEXT_AREA] != 0;
7836 extend_face_to_end_of_line (it);
7837 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
7838 last->right_box_line_p = 1;
7839 if (last == row->glyphs[TEXT_AREA])
7840 last->left_box_line_p = 1;
7841 compute_line_metrics (it);
7842
7843 /* If line is empty, make it occupy the rest of the tool-bar. */
7844 if (!row->displays_text_p)
7845 {
7846 row->height = row->phys_height = it->last_visible_y - row->y;
7847 row->ascent = row->phys_ascent = 0;
7848 }
7849
7850 row->full_width_p = 1;
7851 row->continued_p = 0;
7852 row->truncated_on_left_p = 0;
7853 row->truncated_on_right_p = 0;
7854
7855 it->current_x = it->hpos = 0;
7856 it->current_y += row->height;
7857 ++it->vpos;
7858 ++it->glyph_row;
7859 }
7860
7861
7862 /* Value is the number of screen lines needed to make all tool-bar
7863 items of frame F visible. */
7864
7865 static int
7866 tool_bar_lines_needed (f)
7867 struct frame *f;
7868 {
7869 struct window *w = XWINDOW (f->tool_bar_window);
7870 struct it it;
7871
7872 /* Initialize an iterator for iteration over
7873 F->desired_tool_bar_string in the tool-bar window of frame F. */
7874 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7875 it.first_visible_x = 0;
7876 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7877 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7878
7879 while (!ITERATOR_AT_END_P (&it))
7880 {
7881 it.glyph_row = w->desired_matrix->rows;
7882 clear_glyph_row (it.glyph_row);
7883 display_tool_bar_line (&it);
7884 }
7885
7886 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
7887 }
7888
7889
7890 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
7891 0, 1, 0,
7892 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
7893 (frame)
7894 Lisp_Object frame;
7895 {
7896 struct frame *f;
7897 struct window *w;
7898 int nlines = 0;
7899
7900 if (NILP (frame))
7901 frame = selected_frame;
7902 else
7903 CHECK_FRAME (frame);
7904 f = XFRAME (frame);
7905
7906 if (WINDOWP (f->tool_bar_window)
7907 || (w = XWINDOW (f->tool_bar_window),
7908 XFASTINT (w->height) > 0))
7909 {
7910 update_tool_bar (f, 1);
7911 if (f->n_tool_bar_items)
7912 {
7913 build_desired_tool_bar_string (f);
7914 nlines = tool_bar_lines_needed (f);
7915 }
7916 }
7917
7918 return make_number (nlines);
7919 }
7920
7921
7922 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
7923 height should be changed. */
7924
7925 static int
7926 redisplay_tool_bar (f)
7927 struct frame *f;
7928 {
7929 struct window *w;
7930 struct it it;
7931 struct glyph_row *row;
7932 int change_height_p = 0;
7933
7934 /* If frame hasn't a tool-bar window or if it is zero-height, don't
7935 do anything. This means you must start with tool-bar-lines
7936 non-zero to get the auto-sizing effect. Or in other words, you
7937 can turn off tool-bars by specifying tool-bar-lines zero. */
7938 if (!WINDOWP (f->tool_bar_window)
7939 || (w = XWINDOW (f->tool_bar_window),
7940 XFASTINT (w->height) == 0))
7941 return 0;
7942
7943 /* Set up an iterator for the tool-bar window. */
7944 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7945 it.first_visible_x = 0;
7946 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7947 row = it.glyph_row;
7948
7949 /* Build a string that represents the contents of the tool-bar. */
7950 build_desired_tool_bar_string (f);
7951 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7952
7953 /* Display as many lines as needed to display all tool-bar items. */
7954 while (it.current_y < it.last_visible_y)
7955 display_tool_bar_line (&it);
7956
7957 /* It doesn't make much sense to try scrolling in the tool-bar
7958 window, so don't do it. */
7959 w->desired_matrix->no_scrolling_p = 1;
7960 w->must_be_updated_p = 1;
7961
7962 if (auto_resize_tool_bars_p)
7963 {
7964 int nlines;
7965
7966 /* If we couldn't display everything, change the tool-bar's
7967 height. */
7968 if (IT_STRING_CHARPOS (it) < it.end_charpos)
7969 change_height_p = 1;
7970
7971 /* If there are blank lines at the end, except for a partially
7972 visible blank line at the end that is smaller than
7973 CANON_Y_UNIT, change the tool-bar's height. */
7974 row = it.glyph_row - 1;
7975 if (!row->displays_text_p
7976 && row->height >= CANON_Y_UNIT (f))
7977 change_height_p = 1;
7978
7979 /* If row displays tool-bar items, but is partially visible,
7980 change the tool-bar's height. */
7981 if (row->displays_text_p
7982 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
7983 change_height_p = 1;
7984
7985 /* Resize windows as needed by changing the `tool-bar-lines'
7986 frame parameter. */
7987 if (change_height_p
7988 && (nlines = tool_bar_lines_needed (f),
7989 nlines != XFASTINT (w->height)))
7990 {
7991 extern Lisp_Object Qtool_bar_lines;
7992 Lisp_Object frame;
7993 int old_height = XFASTINT (w->height);
7994
7995 XSETFRAME (frame, f);
7996 clear_glyph_matrix (w->desired_matrix);
7997 Fmodify_frame_parameters (frame,
7998 Fcons (Fcons (Qtool_bar_lines,
7999 make_number (nlines)),
8000 Qnil));
8001 if (XFASTINT (w->height) != old_height)
8002 fonts_changed_p = 1;
8003 }
8004 }
8005
8006 return change_height_p;
8007 }
8008
8009
8010 /* Get information about the tool-bar item which is displayed in GLYPH
8011 on frame F. Return in *PROP_IDX the index where tool-bar item
8012 properties start in F->tool_bar_items. Value is zero if
8013 GLYPH doesn't display a tool-bar item. */
8014
8015 int
8016 tool_bar_item_info (f, glyph, prop_idx)
8017 struct frame *f;
8018 struct glyph *glyph;
8019 int *prop_idx;
8020 {
8021 Lisp_Object prop;
8022 int success_p;
8023 int charpos;
8024
8025 /* This function can be called asynchronously, which means we must
8026 exclude any possibility that Fget_text_property signals an
8027 error. */
8028 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
8029 charpos = max (0, charpos);
8030
8031 /* Get the text property `menu-item' at pos. The value of that
8032 property is the start index of this item's properties in
8033 F->tool_bar_items. */
8034 prop = Fget_text_property (make_number (charpos),
8035 Qmenu_item, f->current_tool_bar_string);
8036 if (INTEGERP (prop))
8037 {
8038 *prop_idx = XINT (prop);
8039 success_p = 1;
8040 }
8041 else
8042 success_p = 0;
8043
8044 return success_p;
8045 }
8046
8047 #endif /* HAVE_WINDOW_SYSTEM */
8048
8049
8050 \f
8051 /************************************************************************
8052 Horizontal scrolling
8053 ************************************************************************/
8054
8055 static int hscroll_window_tree P_ ((Lisp_Object));
8056 static int hscroll_windows P_ ((Lisp_Object));
8057
8058 /* For all leaf windows in the window tree rooted at WINDOW, set their
8059 hscroll value so that PT is (i) visible in the window, and (ii) so
8060 that it is not within a certain margin at the window's left and
8061 right border. Value is non-zero if any window's hscroll has been
8062 changed. */
8063
8064 static int
8065 hscroll_window_tree (window)
8066 Lisp_Object window;
8067 {
8068 int hscrolled_p = 0;
8069 int hscroll_relative_p = FLOATP (Vhscroll_step);
8070 int hscroll_step_abs = 0;
8071 double hscroll_step_rel = 0;
8072
8073 if (hscroll_relative_p)
8074 {
8075 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
8076 if (hscroll_step_rel < 0)
8077 {
8078 hscroll_relative_p = 0;
8079 hscroll_step_abs = 0;
8080 }
8081 }
8082 else if (INTEGERP (Vhscroll_step))
8083 {
8084 hscroll_step_abs = XINT (Vhscroll_step);
8085 if (hscroll_step_abs < 0)
8086 hscroll_step_abs = 0;
8087 }
8088 else
8089 hscroll_step_abs = 0;
8090
8091 while (WINDOWP (window))
8092 {
8093 struct window *w = XWINDOW (window);
8094
8095 if (WINDOWP (w->hchild))
8096 hscrolled_p |= hscroll_window_tree (w->hchild);
8097 else if (WINDOWP (w->vchild))
8098 hscrolled_p |= hscroll_window_tree (w->vchild);
8099 else if (w->cursor.vpos >= 0)
8100 {
8101 int h_margin, text_area_x, text_area_y;
8102 int text_area_width, text_area_height;
8103 struct glyph_row *current_cursor_row
8104 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
8105 struct glyph_row *desired_cursor_row
8106 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
8107 struct glyph_row *cursor_row
8108 = (desired_cursor_row->enabled_p
8109 ? desired_cursor_row
8110 : current_cursor_row);
8111
8112 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
8113 &text_area_width, &text_area_height);
8114
8115 /* Scroll when cursor is inside this scroll margin. */
8116 h_margin = hscroll_margin * CANON_X_UNIT (XFRAME (w->frame));
8117
8118 if ((XFASTINT (w->hscroll)
8119 && w->cursor.x <= h_margin)
8120 || (cursor_row->enabled_p
8121 && cursor_row->truncated_on_right_p
8122 && (w->cursor.x >= text_area_width - h_margin)))
8123 {
8124 struct it it;
8125 int hscroll;
8126 struct buffer *saved_current_buffer;
8127 int pt;
8128 int wanted_x;
8129
8130 /* Find point in a display of infinite width. */
8131 saved_current_buffer = current_buffer;
8132 current_buffer = XBUFFER (w->buffer);
8133
8134 if (w == XWINDOW (selected_window))
8135 pt = BUF_PT (current_buffer);
8136 else
8137 {
8138 pt = marker_position (w->pointm);
8139 pt = max (BEGV, pt);
8140 pt = min (ZV, pt);
8141 }
8142
8143 /* Move iterator to pt starting at cursor_row->start in
8144 a line with infinite width. */
8145 init_to_row_start (&it, w, cursor_row);
8146 it.last_visible_x = INFINITY;
8147 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
8148 current_buffer = saved_current_buffer;
8149
8150 /* Position cursor in window. */
8151 if (!hscroll_relative_p && hscroll_step_abs == 0)
8152 hscroll = max (0, it.current_x - text_area_width / 2)
8153 / CANON_X_UNIT (it.f);
8154 else if (w->cursor.x >= text_area_width - h_margin)
8155 {
8156 if (hscroll_relative_p)
8157 wanted_x = text_area_width * (1 - hscroll_step_rel)
8158 - h_margin;
8159 else
8160 wanted_x = text_area_width
8161 - hscroll_step_abs * CANON_X_UNIT (it.f)
8162 - h_margin;
8163 hscroll
8164 = max (0, it.current_x - wanted_x) / CANON_X_UNIT (it.f);
8165 }
8166 else
8167 {
8168 if (hscroll_relative_p)
8169 wanted_x = text_area_width * hscroll_step_rel
8170 + h_margin;
8171 else
8172 wanted_x = hscroll_step_abs * CANON_X_UNIT (it.f)
8173 + h_margin;
8174 hscroll
8175 = max (0, it.current_x - wanted_x) / CANON_X_UNIT (it.f);
8176 }
8177 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
8178
8179 /* Don't call Fset_window_hscroll if value hasn't
8180 changed because it will prevent redisplay
8181 optimizations. */
8182 if (XFASTINT (w->hscroll) != hscroll)
8183 {
8184 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
8185 w->hscroll = make_number (hscroll);
8186 hscrolled_p = 1;
8187 }
8188 }
8189 }
8190
8191 window = w->next;
8192 }
8193
8194 /* Value is non-zero if hscroll of any leaf window has been changed. */
8195 return hscrolled_p;
8196 }
8197
8198
8199 /* Set hscroll so that cursor is visible and not inside horizontal
8200 scroll margins for all windows in the tree rooted at WINDOW. See
8201 also hscroll_window_tree above. Value is non-zero if any window's
8202 hscroll has been changed. If it has, desired matrices on the frame
8203 of WINDOW are cleared. */
8204
8205 static int
8206 hscroll_windows (window)
8207 Lisp_Object window;
8208 {
8209 int hscrolled_p;
8210
8211 if (automatic_hscrolling_p)
8212 {
8213 hscrolled_p = hscroll_window_tree (window);
8214 if (hscrolled_p)
8215 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
8216 }
8217 else
8218 hscrolled_p = 0;
8219 return hscrolled_p;
8220 }
8221
8222
8223 \f
8224 /************************************************************************
8225 Redisplay
8226 ************************************************************************/
8227
8228 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
8229 to a non-zero value. This is sometimes handy to have in a debugger
8230 session. */
8231
8232 #if GLYPH_DEBUG
8233
8234 /* First and last unchanged row for try_window_id. */
8235
8236 int debug_first_unchanged_at_end_vpos;
8237 int debug_last_unchanged_at_beg_vpos;
8238
8239 /* Delta vpos and y. */
8240
8241 int debug_dvpos, debug_dy;
8242
8243 /* Delta in characters and bytes for try_window_id. */
8244
8245 int debug_delta, debug_delta_bytes;
8246
8247 /* Values of window_end_pos and window_end_vpos at the end of
8248 try_window_id. */
8249
8250 EMACS_INT debug_end_pos, debug_end_vpos;
8251
8252 /* Append a string to W->desired_matrix->method. FMT is a printf
8253 format string. A1...A9 are a supplement for a variable-length
8254 argument list. If trace_redisplay_p is non-zero also printf the
8255 resulting string to stderr. */
8256
8257 static void
8258 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
8259 struct window *w;
8260 char *fmt;
8261 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
8262 {
8263 char buffer[512];
8264 char *method = w->desired_matrix->method;
8265 int len = strlen (method);
8266 int size = sizeof w->desired_matrix->method;
8267 int remaining = size - len - 1;
8268
8269 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
8270 if (len && remaining)
8271 {
8272 method[len] = '|';
8273 --remaining, ++len;
8274 }
8275
8276 strncpy (method + len, buffer, remaining);
8277
8278 if (trace_redisplay_p)
8279 fprintf (stderr, "%p (%s): %s\n",
8280 w,
8281 ((BUFFERP (w->buffer)
8282 && STRINGP (XBUFFER (w->buffer)->name))
8283 ? (char *) SDATA (XBUFFER (w->buffer)->name)
8284 : "no buffer"),
8285 buffer);
8286 }
8287
8288 #endif /* GLYPH_DEBUG */
8289
8290
8291 /* This counter is used to clear the face cache every once in a while
8292 in redisplay_internal. It is incremented for each redisplay.
8293 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
8294 cleared. */
8295
8296 #define CLEAR_FACE_CACHE_COUNT 500
8297 static int clear_face_cache_count;
8298
8299 /* Record the previous terminal frame we displayed. */
8300
8301 static struct frame *previous_terminal_frame;
8302
8303 /* Non-zero while redisplay_internal is in progress. */
8304
8305 int redisplaying_p;
8306
8307
8308 /* Value is non-zero if all changes in window W, which displays
8309 current_buffer, are in the text between START and END. START is a
8310 buffer position, END is given as a distance from Z. Used in
8311 redisplay_internal for display optimization. */
8312
8313 static INLINE int
8314 text_outside_line_unchanged_p (w, start, end)
8315 struct window *w;
8316 int start, end;
8317 {
8318 int unchanged_p = 1;
8319
8320 /* If text or overlays have changed, see where. */
8321 if (XFASTINT (w->last_modified) < MODIFF
8322 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8323 {
8324 /* Gap in the line? */
8325 if (GPT < start || Z - GPT < end)
8326 unchanged_p = 0;
8327
8328 /* Changes start in front of the line, or end after it? */
8329 if (unchanged_p
8330 && (BEG_UNCHANGED < start - 1
8331 || END_UNCHANGED < end))
8332 unchanged_p = 0;
8333
8334 /* If selective display, can't optimize if changes start at the
8335 beginning of the line. */
8336 if (unchanged_p
8337 && INTEGERP (current_buffer->selective_display)
8338 && XINT (current_buffer->selective_display) > 0
8339 && (BEG_UNCHANGED < start || GPT <= start))
8340 unchanged_p = 0;
8341
8342 /* If there are overlays at the start or end of the line, these
8343 may have overlay strings with newlines in them. A change at
8344 START, for instance, may actually concern the display of such
8345 overlay strings as well, and they are displayed on different
8346 lines. So, quickly rule out this case. (For the future, it
8347 might be desirable to implement something more telling than
8348 just BEG/END_UNCHANGED.) */
8349 if (unchanged_p)
8350 {
8351 if (BEG + BEG_UNCHANGED == start
8352 && overlay_touches_p (start))
8353 unchanged_p = 0;
8354 if (END_UNCHANGED == end
8355 && overlay_touches_p (Z - end))
8356 unchanged_p = 0;
8357 }
8358 }
8359
8360 return unchanged_p;
8361 }
8362
8363
8364 /* Do a frame update, taking possible shortcuts into account. This is
8365 the main external entry point for redisplay.
8366
8367 If the last redisplay displayed an echo area message and that message
8368 is no longer requested, we clear the echo area or bring back the
8369 mini-buffer if that is in use. */
8370
8371 void
8372 redisplay ()
8373 {
8374 redisplay_internal (0);
8375 }
8376
8377
8378 /* Return 1 if point moved out of or into a composition. Otherwise
8379 return 0. PREV_BUF and PREV_PT are the last point buffer and
8380 position. BUF and PT are the current point buffer and position. */
8381
8382 int
8383 check_point_in_composition (prev_buf, prev_pt, buf, pt)
8384 struct buffer *prev_buf, *buf;
8385 int prev_pt, pt;
8386 {
8387 int start, end;
8388 Lisp_Object prop;
8389 Lisp_Object buffer;
8390
8391 XSETBUFFER (buffer, buf);
8392 /* Check a composition at the last point if point moved within the
8393 same buffer. */
8394 if (prev_buf == buf)
8395 {
8396 if (prev_pt == pt)
8397 /* Point didn't move. */
8398 return 0;
8399
8400 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
8401 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
8402 && COMPOSITION_VALID_P (start, end, prop)
8403 && start < prev_pt && end > prev_pt)
8404 /* The last point was within the composition. Return 1 iff
8405 point moved out of the composition. */
8406 return (pt <= start || pt >= end);
8407 }
8408
8409 /* Check a composition at the current point. */
8410 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
8411 && find_composition (pt, -1, &start, &end, &prop, buffer)
8412 && COMPOSITION_VALID_P (start, end, prop)
8413 && start < pt && end > pt);
8414 }
8415
8416
8417 /* Reconsider the setting of B->clip_changed which is displayed
8418 in window W. */
8419
8420 static INLINE void
8421 reconsider_clip_changes (w, b)
8422 struct window *w;
8423 struct buffer *b;
8424 {
8425 if (b->clip_changed
8426 && !NILP (w->window_end_valid)
8427 && w->current_matrix->buffer == b
8428 && w->current_matrix->zv == BUF_ZV (b)
8429 && w->current_matrix->begv == BUF_BEGV (b))
8430 b->clip_changed = 0;
8431
8432 /* If display wasn't paused, and W is not a tool bar window, see if
8433 point has been moved into or out of a composition. In that case,
8434 we set b->clip_changed to 1 to force updating the screen. If
8435 b->clip_changed has already been set to 1, we can skip this
8436 check. */
8437 if (!b->clip_changed
8438 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
8439 {
8440 int pt;
8441
8442 if (w == XWINDOW (selected_window))
8443 pt = BUF_PT (current_buffer);
8444 else
8445 pt = marker_position (w->pointm);
8446
8447 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
8448 || pt != XINT (w->last_point))
8449 && check_point_in_composition (w->current_matrix->buffer,
8450 XINT (w->last_point),
8451 XBUFFER (w->buffer), pt))
8452 b->clip_changed = 1;
8453 }
8454 }
8455
8456
8457 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
8458 response to any user action; therefore, we should preserve the echo
8459 area. (Actually, our caller does that job.) Perhaps in the future
8460 avoid recentering windows if it is not necessary; currently that
8461 causes some problems. */
8462
8463 static void
8464 redisplay_internal (preserve_echo_area)
8465 int preserve_echo_area;
8466 {
8467 struct window *w = XWINDOW (selected_window);
8468 struct frame *f = XFRAME (w->frame);
8469 int pause;
8470 int must_finish = 0;
8471 struct text_pos tlbufpos, tlendpos;
8472 int number_of_visible_frames;
8473 int count;
8474 struct frame *sf = SELECTED_FRAME ();
8475
8476 /* Non-zero means redisplay has to consider all windows on all
8477 frames. Zero means, only selected_window is considered. */
8478 int consider_all_windows_p;
8479
8480 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
8481
8482 /* No redisplay if running in batch mode or frame is not yet fully
8483 initialized, or redisplay is explicitly turned off by setting
8484 Vinhibit_redisplay. */
8485 if (noninteractive
8486 || !NILP (Vinhibit_redisplay)
8487 || !f->glyphs_initialized_p)
8488 return;
8489
8490 /* The flag redisplay_performed_directly_p is set by
8491 direct_output_for_insert when it already did the whole screen
8492 update necessary. */
8493 if (redisplay_performed_directly_p)
8494 {
8495 redisplay_performed_directly_p = 0;
8496 if (!hscroll_windows (selected_window))
8497 return;
8498 }
8499
8500 #ifdef USE_X_TOOLKIT
8501 if (popup_activated ())
8502 return;
8503 #endif
8504
8505 /* I don't think this happens but let's be paranoid. */
8506 if (redisplaying_p)
8507 return;
8508
8509 /* Record a function that resets redisplaying_p to its old value
8510 when we leave this function. */
8511 count = SPECPDL_INDEX ();
8512 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
8513 ++redisplaying_p;
8514
8515 retry:
8516 pause = 0;
8517 reconsider_clip_changes (w, current_buffer);
8518
8519 /* If new fonts have been loaded that make a glyph matrix adjustment
8520 necessary, do it. */
8521 if (fonts_changed_p)
8522 {
8523 adjust_glyphs (NULL);
8524 ++windows_or_buffers_changed;
8525 fonts_changed_p = 0;
8526 }
8527
8528 /* If face_change_count is non-zero, init_iterator will free all
8529 realized faces, which includes the faces referenced from current
8530 matrices. So, we can't reuse current matrices in this case. */
8531 if (face_change_count)
8532 ++windows_or_buffers_changed;
8533
8534 if (! FRAME_WINDOW_P (sf)
8535 && previous_terminal_frame != sf)
8536 {
8537 /* Since frames on an ASCII terminal share the same display
8538 area, displaying a different frame means redisplay the whole
8539 thing. */
8540 windows_or_buffers_changed++;
8541 SET_FRAME_GARBAGED (sf);
8542 XSETFRAME (Vterminal_frame, sf);
8543 }
8544 previous_terminal_frame = sf;
8545
8546 /* Set the visible flags for all frames. Do this before checking
8547 for resized or garbaged frames; they want to know if their frames
8548 are visible. See the comment in frame.h for
8549 FRAME_SAMPLE_VISIBILITY. */
8550 {
8551 Lisp_Object tail, frame;
8552
8553 number_of_visible_frames = 0;
8554
8555 FOR_EACH_FRAME (tail, frame)
8556 {
8557 struct frame *f = XFRAME (frame);
8558
8559 FRAME_SAMPLE_VISIBILITY (f);
8560 if (FRAME_VISIBLE_P (f))
8561 ++number_of_visible_frames;
8562 clear_desired_matrices (f);
8563 }
8564 }
8565
8566 /* Notice any pending interrupt request to change frame size. */
8567 do_pending_window_change (1);
8568
8569 /* Clear frames marked as garbaged. */
8570 if (frame_garbaged)
8571 clear_garbaged_frames ();
8572
8573 /* Build menubar and tool-bar items. */
8574 prepare_menu_bars ();
8575
8576 if (windows_or_buffers_changed)
8577 update_mode_lines++;
8578
8579 /* Detect case that we need to write or remove a star in the mode line. */
8580 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
8581 {
8582 w->update_mode_line = Qt;
8583 if (buffer_shared > 1)
8584 update_mode_lines++;
8585 }
8586
8587 /* If %c is in the mode line, update it if needed. */
8588 if (!NILP (w->column_number_displayed)
8589 /* This alternative quickly identifies a common case
8590 where no change is needed. */
8591 && !(PT == XFASTINT (w->last_point)
8592 && XFASTINT (w->last_modified) >= MODIFF
8593 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
8594 && (XFASTINT (w->column_number_displayed)
8595 != (int) current_column ())) /* iftc */
8596 w->update_mode_line = Qt;
8597
8598 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
8599
8600 /* The variable buffer_shared is set in redisplay_window and
8601 indicates that we redisplay a buffer in different windows. See
8602 there. */
8603 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
8604 || cursor_type_changed);
8605
8606 /* If specs for an arrow have changed, do thorough redisplay
8607 to ensure we remove any arrow that should no longer exist. */
8608 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
8609 || ! EQ (Voverlay_arrow_string, last_arrow_string))
8610 consider_all_windows_p = windows_or_buffers_changed = 1;
8611
8612 /* Normally the message* functions will have already displayed and
8613 updated the echo area, but the frame may have been trashed, or
8614 the update may have been preempted, so display the echo area
8615 again here. Checking message_cleared_p captures the case that
8616 the echo area should be cleared. */
8617 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
8618 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
8619 || (message_cleared_p
8620 && minibuf_level == 0
8621 /* If the mini-window is currently selected, this means the
8622 echo-area doesn't show through. */
8623 && !MINI_WINDOW_P (XWINDOW (selected_window))))
8624 {
8625 int window_height_changed_p = echo_area_display (0);
8626 must_finish = 1;
8627
8628 /* If we don't display the current message, don't clear the
8629 message_cleared_p flag, because, if we did, we wouldn't clear
8630 the echo area in the next redisplay which doesn't preserve
8631 the echo area. */
8632 if (!display_last_displayed_message_p)
8633 message_cleared_p = 0;
8634
8635 if (fonts_changed_p)
8636 goto retry;
8637 else if (window_height_changed_p)
8638 {
8639 consider_all_windows_p = 1;
8640 ++update_mode_lines;
8641 ++windows_or_buffers_changed;
8642
8643 /* If window configuration was changed, frames may have been
8644 marked garbaged. Clear them or we will experience
8645 surprises wrt scrolling. */
8646 if (frame_garbaged)
8647 clear_garbaged_frames ();
8648 }
8649 }
8650 else if (EQ (selected_window, minibuf_window)
8651 && (current_buffer->clip_changed
8652 || XFASTINT (w->last_modified) < MODIFF
8653 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8654 && resize_mini_window (w, 0))
8655 {
8656 /* Resized active mini-window to fit the size of what it is
8657 showing if its contents might have changed. */
8658 must_finish = 1;
8659 consider_all_windows_p = 1;
8660 ++windows_or_buffers_changed;
8661 ++update_mode_lines;
8662
8663 /* If window configuration was changed, frames may have been
8664 marked garbaged. Clear them or we will experience
8665 surprises wrt scrolling. */
8666 if (frame_garbaged)
8667 clear_garbaged_frames ();
8668 }
8669
8670
8671 /* If showing the region, and mark has changed, we must redisplay
8672 the whole window. The assignment to this_line_start_pos prevents
8673 the optimization directly below this if-statement. */
8674 if (((!NILP (Vtransient_mark_mode)
8675 && !NILP (XBUFFER (w->buffer)->mark_active))
8676 != !NILP (w->region_showing))
8677 || (!NILP (w->region_showing)
8678 && !EQ (w->region_showing,
8679 Fmarker_position (XBUFFER (w->buffer)->mark))))
8680 CHARPOS (this_line_start_pos) = 0;
8681
8682 /* Optimize the case that only the line containing the cursor in the
8683 selected window has changed. Variables starting with this_ are
8684 set in display_line and record information about the line
8685 containing the cursor. */
8686 tlbufpos = this_line_start_pos;
8687 tlendpos = this_line_end_pos;
8688 if (!consider_all_windows_p
8689 && CHARPOS (tlbufpos) > 0
8690 && NILP (w->update_mode_line)
8691 && !current_buffer->clip_changed
8692 && !current_buffer->prevent_redisplay_optimizations_p
8693 && FRAME_VISIBLE_P (XFRAME (w->frame))
8694 && !FRAME_OBSCURED_P (XFRAME (w->frame))
8695 /* Make sure recorded data applies to current buffer, etc. */
8696 && this_line_buffer == current_buffer
8697 && current_buffer == XBUFFER (w->buffer)
8698 && NILP (w->force_start)
8699 && NILP (w->optional_new_start)
8700 /* Point must be on the line that we have info recorded about. */
8701 && PT >= CHARPOS (tlbufpos)
8702 && PT <= Z - CHARPOS (tlendpos)
8703 /* All text outside that line, including its final newline,
8704 must be unchanged */
8705 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
8706 CHARPOS (tlendpos)))
8707 {
8708 if (CHARPOS (tlbufpos) > BEGV
8709 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
8710 && (CHARPOS (tlbufpos) == ZV
8711 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
8712 /* Former continuation line has disappeared by becoming empty */
8713 goto cancel;
8714 else if (XFASTINT (w->last_modified) < MODIFF
8715 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
8716 || MINI_WINDOW_P (w))
8717 {
8718 /* We have to handle the case of continuation around a
8719 wide-column character (See the comment in indent.c around
8720 line 885).
8721
8722 For instance, in the following case:
8723
8724 -------- Insert --------
8725 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
8726 J_I_ ==> J_I_ `^^' are cursors.
8727 ^^ ^^
8728 -------- --------
8729
8730 As we have to redraw the line above, we should goto cancel. */
8731
8732 struct it it;
8733 int line_height_before = this_line_pixel_height;
8734
8735 /* Note that start_display will handle the case that the
8736 line starting at tlbufpos is a continuation lines. */
8737 start_display (&it, w, tlbufpos);
8738
8739 /* Implementation note: It this still necessary? */
8740 if (it.current_x != this_line_start_x)
8741 goto cancel;
8742
8743 TRACE ((stderr, "trying display optimization 1\n"));
8744 w->cursor.vpos = -1;
8745 overlay_arrow_seen = 0;
8746 it.vpos = this_line_vpos;
8747 it.current_y = this_line_y;
8748 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
8749 display_line (&it);
8750
8751 /* If line contains point, is not continued,
8752 and ends at same distance from eob as before, we win */
8753 if (w->cursor.vpos >= 0
8754 /* Line is not continued, otherwise this_line_start_pos
8755 would have been set to 0 in display_line. */
8756 && CHARPOS (this_line_start_pos)
8757 /* Line ends as before. */
8758 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
8759 /* Line has same height as before. Otherwise other lines
8760 would have to be shifted up or down. */
8761 && this_line_pixel_height == line_height_before)
8762 {
8763 /* If this is not the window's last line, we must adjust
8764 the charstarts of the lines below. */
8765 if (it.current_y < it.last_visible_y)
8766 {
8767 struct glyph_row *row
8768 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
8769 int delta, delta_bytes;
8770
8771 if (Z - CHARPOS (tlendpos) == ZV)
8772 {
8773 /* This line ends at end of (accessible part of)
8774 buffer. There is no newline to count. */
8775 delta = (Z
8776 - CHARPOS (tlendpos)
8777 - MATRIX_ROW_START_CHARPOS (row));
8778 delta_bytes = (Z_BYTE
8779 - BYTEPOS (tlendpos)
8780 - MATRIX_ROW_START_BYTEPOS (row));
8781 }
8782 else
8783 {
8784 /* This line ends in a newline. Must take
8785 account of the newline and the rest of the
8786 text that follows. */
8787 delta = (Z
8788 - CHARPOS (tlendpos)
8789 - MATRIX_ROW_START_CHARPOS (row));
8790 delta_bytes = (Z_BYTE
8791 - BYTEPOS (tlendpos)
8792 - MATRIX_ROW_START_BYTEPOS (row));
8793 }
8794
8795 increment_matrix_positions (w->current_matrix,
8796 this_line_vpos + 1,
8797 w->current_matrix->nrows,
8798 delta, delta_bytes);
8799 }
8800
8801 /* If this row displays text now but previously didn't,
8802 or vice versa, w->window_end_vpos may have to be
8803 adjusted. */
8804 if ((it.glyph_row - 1)->displays_text_p)
8805 {
8806 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
8807 XSETINT (w->window_end_vpos, this_line_vpos);
8808 }
8809 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
8810 && this_line_vpos > 0)
8811 XSETINT (w->window_end_vpos, this_line_vpos - 1);
8812 w->window_end_valid = Qnil;
8813
8814 /* Update hint: No need to try to scroll in update_window. */
8815 w->desired_matrix->no_scrolling_p = 1;
8816
8817 #if GLYPH_DEBUG
8818 *w->desired_matrix->method = 0;
8819 debug_method_add (w, "optimization 1");
8820 #endif
8821 goto update;
8822 }
8823 else
8824 goto cancel;
8825 }
8826 else if (/* Cursor position hasn't changed. */
8827 PT == XFASTINT (w->last_point)
8828 /* Make sure the cursor was last displayed
8829 in this window. Otherwise we have to reposition it. */
8830 && 0 <= w->cursor.vpos
8831 && XINT (w->height) > w->cursor.vpos)
8832 {
8833 if (!must_finish)
8834 {
8835 do_pending_window_change (1);
8836
8837 /* We used to always goto end_of_redisplay here, but this
8838 isn't enough if we have a blinking cursor. */
8839 if (w->cursor_off_p == w->last_cursor_off_p)
8840 goto end_of_redisplay;
8841 }
8842 goto update;
8843 }
8844 /* If highlighting the region, or if the cursor is in the echo area,
8845 then we can't just move the cursor. */
8846 else if (! (!NILP (Vtransient_mark_mode)
8847 && !NILP (current_buffer->mark_active))
8848 && (EQ (selected_window, current_buffer->last_selected_window)
8849 || highlight_nonselected_windows)
8850 && NILP (w->region_showing)
8851 && NILP (Vshow_trailing_whitespace)
8852 && !cursor_in_echo_area)
8853 {
8854 struct it it;
8855 struct glyph_row *row;
8856
8857 /* Skip from tlbufpos to PT and see where it is. Note that
8858 PT may be in invisible text. If so, we will end at the
8859 next visible position. */
8860 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
8861 NULL, DEFAULT_FACE_ID);
8862 it.current_x = this_line_start_x;
8863 it.current_y = this_line_y;
8864 it.vpos = this_line_vpos;
8865
8866 /* The call to move_it_to stops in front of PT, but
8867 moves over before-strings. */
8868 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
8869
8870 if (it.vpos == this_line_vpos
8871 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
8872 row->enabled_p))
8873 {
8874 xassert (this_line_vpos == it.vpos);
8875 xassert (this_line_y == it.current_y);
8876 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
8877 #if GLYPH_DEBUG
8878 *w->desired_matrix->method = 0;
8879 debug_method_add (w, "optimization 3");
8880 #endif
8881 goto update;
8882 }
8883 else
8884 goto cancel;
8885 }
8886
8887 cancel:
8888 /* Text changed drastically or point moved off of line. */
8889 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
8890 }
8891
8892 CHARPOS (this_line_start_pos) = 0;
8893 consider_all_windows_p |= buffer_shared > 1;
8894 ++clear_face_cache_count;
8895
8896
8897 /* Build desired matrices, and update the display. If
8898 consider_all_windows_p is non-zero, do it for all windows on all
8899 frames. Otherwise do it for selected_window, only. */
8900
8901 if (consider_all_windows_p)
8902 {
8903 Lisp_Object tail, frame;
8904 int i, n = 0, size = 50;
8905 struct frame **updated
8906 = (struct frame **) alloca (size * sizeof *updated);
8907
8908 /* Clear the face cache eventually. */
8909 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
8910 {
8911 clear_face_cache (0);
8912 clear_face_cache_count = 0;
8913 }
8914
8915 /* Recompute # windows showing selected buffer. This will be
8916 incremented each time such a window is displayed. */
8917 buffer_shared = 0;
8918
8919 FOR_EACH_FRAME (tail, frame)
8920 {
8921 struct frame *f = XFRAME (frame);
8922
8923 if (FRAME_WINDOW_P (f) || f == sf)
8924 {
8925 #ifdef HAVE_WINDOW_SYSTEM
8926 if (clear_face_cache_count % 50 == 0
8927 && FRAME_WINDOW_P (f))
8928 clear_image_cache (f, 0);
8929 #endif /* HAVE_WINDOW_SYSTEM */
8930
8931 /* Mark all the scroll bars to be removed; we'll redeem
8932 the ones we want when we redisplay their windows. */
8933 if (condemn_scroll_bars_hook)
8934 condemn_scroll_bars_hook (f);
8935
8936 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8937 redisplay_windows (FRAME_ROOT_WINDOW (f));
8938
8939 /* Any scroll bars which redisplay_windows should have
8940 nuked should now go away. */
8941 if (judge_scroll_bars_hook)
8942 judge_scroll_bars_hook (f);
8943
8944 /* If fonts changed, display again. */
8945 if (fonts_changed_p)
8946 goto retry;
8947
8948 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8949 {
8950 /* See if we have to hscroll. */
8951 if (hscroll_windows (f->root_window))
8952 goto retry;
8953
8954 /* Prevent various kinds of signals during display
8955 update. stdio is not robust about handling
8956 signals, which can cause an apparent I/O
8957 error. */
8958 if (interrupt_input)
8959 unrequest_sigio ();
8960 stop_polling ();
8961
8962 /* Update the display. */
8963 set_window_update_flags (XWINDOW (f->root_window), 1);
8964 pause |= update_frame (f, 0, 0);
8965 if (pause)
8966 break;
8967
8968 if (n == size)
8969 {
8970 int nbytes = size * sizeof *updated;
8971 struct frame **p = (struct frame **) alloca (2 * nbytes);
8972 bcopy (updated, p, nbytes);
8973 size *= 2;
8974 }
8975
8976 updated[n++] = f;
8977 }
8978 }
8979 }
8980
8981 /* Do the mark_window_display_accurate after all windows have
8982 been redisplayed because this call resets flags in buffers
8983 which are needed for proper redisplay. */
8984 for (i = 0; i < n; ++i)
8985 {
8986 struct frame *f = updated[i];
8987 mark_window_display_accurate (f->root_window, 1);
8988 if (frame_up_to_date_hook)
8989 frame_up_to_date_hook (f);
8990 }
8991 }
8992 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8993 {
8994 Lisp_Object mini_window;
8995 struct frame *mini_frame;
8996
8997 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
8998 /* Use list_of_error, not Qerror, so that
8999 we catch only errors and don't run the debugger. */
9000 internal_condition_case_1 (redisplay_window_1, selected_window,
9001 list_of_error,
9002 redisplay_window_error);
9003
9004 /* Compare desired and current matrices, perform output. */
9005 update:
9006
9007 /* If fonts changed, display again. */
9008 if (fonts_changed_p)
9009 goto retry;
9010
9011 /* Prevent various kinds of signals during display update.
9012 stdio is not robust about handling signals,
9013 which can cause an apparent I/O error. */
9014 if (interrupt_input)
9015 unrequest_sigio ();
9016 stop_polling ();
9017
9018 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
9019 {
9020 if (hscroll_windows (selected_window))
9021 goto retry;
9022
9023 XWINDOW (selected_window)->must_be_updated_p = 1;
9024 pause = update_frame (sf, 0, 0);
9025 }
9026
9027 /* We may have called echo_area_display at the top of this
9028 function. If the echo area is on another frame, that may
9029 have put text on a frame other than the selected one, so the
9030 above call to update_frame would not have caught it. Catch
9031 it here. */
9032 mini_window = FRAME_MINIBUF_WINDOW (sf);
9033 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9034
9035 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
9036 {
9037 XWINDOW (mini_window)->must_be_updated_p = 1;
9038 pause |= update_frame (mini_frame, 0, 0);
9039 if (!pause && hscroll_windows (mini_window))
9040 goto retry;
9041 }
9042 }
9043
9044 /* If display was paused because of pending input, make sure we do a
9045 thorough update the next time. */
9046 if (pause)
9047 {
9048 /* Prevent the optimization at the beginning of
9049 redisplay_internal that tries a single-line update of the
9050 line containing the cursor in the selected window. */
9051 CHARPOS (this_line_start_pos) = 0;
9052
9053 /* Let the overlay arrow be updated the next time. */
9054 if (!NILP (last_arrow_position))
9055 {
9056 last_arrow_position = Qt;
9057 last_arrow_string = Qt;
9058 }
9059
9060 /* If we pause after scrolling, some rows in the current
9061 matrices of some windows are not valid. */
9062 if (!WINDOW_FULL_WIDTH_P (w)
9063 && !FRAME_WINDOW_P (XFRAME (w->frame)))
9064 update_mode_lines = 1;
9065 }
9066 else
9067 {
9068 if (!consider_all_windows_p)
9069 {
9070 /* This has already been done above if
9071 consider_all_windows_p is set. */
9072 mark_window_display_accurate_1 (w, 1);
9073
9074 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
9075 last_arrow_string = Voverlay_arrow_string;
9076
9077 if (frame_up_to_date_hook != 0)
9078 frame_up_to_date_hook (sf);
9079 }
9080
9081 update_mode_lines = 0;
9082 windows_or_buffers_changed = 0;
9083 cursor_type_changed = 0;
9084 }
9085
9086 /* Start SIGIO interrupts coming again. Having them off during the
9087 code above makes it less likely one will discard output, but not
9088 impossible, since there might be stuff in the system buffer here.
9089 But it is much hairier to try to do anything about that. */
9090 if (interrupt_input)
9091 request_sigio ();
9092 start_polling ();
9093
9094 /* If a frame has become visible which was not before, redisplay
9095 again, so that we display it. Expose events for such a frame
9096 (which it gets when becoming visible) don't call the parts of
9097 redisplay constructing glyphs, so simply exposing a frame won't
9098 display anything in this case. So, we have to display these
9099 frames here explicitly. */
9100 if (!pause)
9101 {
9102 Lisp_Object tail, frame;
9103 int new_count = 0;
9104
9105 FOR_EACH_FRAME (tail, frame)
9106 {
9107 int this_is_visible = 0;
9108
9109 if (XFRAME (frame)->visible)
9110 this_is_visible = 1;
9111 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
9112 if (XFRAME (frame)->visible)
9113 this_is_visible = 1;
9114
9115 if (this_is_visible)
9116 new_count++;
9117 }
9118
9119 if (new_count != number_of_visible_frames)
9120 windows_or_buffers_changed++;
9121 }
9122
9123 /* Change frame size now if a change is pending. */
9124 do_pending_window_change (1);
9125
9126 /* If we just did a pending size change, or have additional
9127 visible frames, redisplay again. */
9128 if (windows_or_buffers_changed && !pause)
9129 goto retry;
9130
9131 end_of_redisplay:;
9132
9133 unbind_to (count, Qnil);
9134 }
9135
9136
9137 /* Redisplay, but leave alone any recent echo area message unless
9138 another message has been requested in its place.
9139
9140 This is useful in situations where you need to redisplay but no
9141 user action has occurred, making it inappropriate for the message
9142 area to be cleared. See tracking_off and
9143 wait_reading_process_input for examples of these situations.
9144
9145 FROM_WHERE is an integer saying from where this function was
9146 called. This is useful for debugging. */
9147
9148 void
9149 redisplay_preserve_echo_area (from_where)
9150 int from_where;
9151 {
9152 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
9153
9154 if (!NILP (echo_area_buffer[1]))
9155 {
9156 /* We have a previously displayed message, but no current
9157 message. Redisplay the previous message. */
9158 display_last_displayed_message_p = 1;
9159 redisplay_internal (1);
9160 display_last_displayed_message_p = 0;
9161 }
9162 else
9163 redisplay_internal (1);
9164 }
9165
9166
9167 /* Function registered with record_unwind_protect in
9168 redisplay_internal. Clears the flag indicating that a redisplay is
9169 in progress. */
9170
9171 static Lisp_Object
9172 unwind_redisplay (old_redisplaying_p)
9173 Lisp_Object old_redisplaying_p;
9174 {
9175 redisplaying_p = XFASTINT (old_redisplaying_p);
9176 return Qnil;
9177 }
9178
9179
9180 /* Mark the display of window W as accurate or inaccurate. If
9181 ACCURATE_P is non-zero mark display of W as accurate. If
9182 ACCURATE_P is zero, arrange for W to be redisplayed the next time
9183 redisplay_internal is called. */
9184
9185 static void
9186 mark_window_display_accurate_1 (w, accurate_p)
9187 struct window *w;
9188 int accurate_p;
9189 {
9190 if (BUFFERP (w->buffer))
9191 {
9192 struct buffer *b = XBUFFER (w->buffer);
9193
9194 w->last_modified
9195 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
9196 w->last_overlay_modified
9197 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
9198 w->last_had_star
9199 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
9200
9201 if (accurate_p)
9202 {
9203 b->clip_changed = 0;
9204 b->prevent_redisplay_optimizations_p = 0;
9205
9206 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
9207 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
9208 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
9209 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
9210
9211 w->current_matrix->buffer = b;
9212 w->current_matrix->begv = BUF_BEGV (b);
9213 w->current_matrix->zv = BUF_ZV (b);
9214
9215 w->last_cursor = w->cursor;
9216 w->last_cursor_off_p = w->cursor_off_p;
9217
9218 if (w == XWINDOW (selected_window))
9219 w->last_point = make_number (BUF_PT (b));
9220 else
9221 w->last_point = make_number (XMARKER (w->pointm)->charpos);
9222 }
9223 }
9224
9225 if (accurate_p)
9226 {
9227 w->window_end_valid = w->buffer;
9228 #if 0 /* This is incorrect with variable-height lines. */
9229 xassert (XINT (w->window_end_vpos)
9230 < (XINT (w->height)
9231 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
9232 #endif
9233 w->update_mode_line = Qnil;
9234 }
9235 }
9236
9237
9238 /* Mark the display of windows in the window tree rooted at WINDOW as
9239 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
9240 windows as accurate. If ACCURATE_P is zero, arrange for windows to
9241 be redisplayed the next time redisplay_internal is called. */
9242
9243 void
9244 mark_window_display_accurate (window, accurate_p)
9245 Lisp_Object window;
9246 int accurate_p;
9247 {
9248 struct window *w;
9249
9250 for (; !NILP (window); window = w->next)
9251 {
9252 w = XWINDOW (window);
9253 mark_window_display_accurate_1 (w, accurate_p);
9254
9255 if (!NILP (w->vchild))
9256 mark_window_display_accurate (w->vchild, accurate_p);
9257 if (!NILP (w->hchild))
9258 mark_window_display_accurate (w->hchild, accurate_p);
9259 }
9260
9261 if (accurate_p)
9262 {
9263 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
9264 last_arrow_string = Voverlay_arrow_string;
9265 }
9266 else
9267 {
9268 /* Force a thorough redisplay the next time by setting
9269 last_arrow_position and last_arrow_string to t, which is
9270 unequal to any useful value of Voverlay_arrow_... */
9271 last_arrow_position = Qt;
9272 last_arrow_string = Qt;
9273 }
9274 }
9275
9276
9277 /* Return value in display table DP (Lisp_Char_Table *) for character
9278 C. Since a display table doesn't have any parent, we don't have to
9279 follow parent. Do not call this function directly but use the
9280 macro DISP_CHAR_VECTOR. */
9281
9282 Lisp_Object
9283 disp_char_vector (dp, c)
9284 struct Lisp_Char_Table *dp;
9285 int c;
9286 {
9287 int code[4], i;
9288 Lisp_Object val;
9289
9290 if (SINGLE_BYTE_CHAR_P (c))
9291 return (dp->contents[c]);
9292
9293 SPLIT_CHAR (c, code[0], code[1], code[2]);
9294 if (code[1] < 32)
9295 code[1] = -1;
9296 else if (code[2] < 32)
9297 code[2] = -1;
9298
9299 /* Here, the possible range of code[0] (== charset ID) is
9300 128..max_charset. Since the top level char table contains data
9301 for multibyte characters after 256th element, we must increment
9302 code[0] by 128 to get a correct index. */
9303 code[0] += 128;
9304 code[3] = -1; /* anchor */
9305
9306 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
9307 {
9308 val = dp->contents[code[i]];
9309 if (!SUB_CHAR_TABLE_P (val))
9310 return (NILP (val) ? dp->defalt : val);
9311 }
9312
9313 /* Here, val is a sub char table. We return the default value of
9314 it. */
9315 return (dp->defalt);
9316 }
9317
9318
9319 \f
9320 /***********************************************************************
9321 Window Redisplay
9322 ***********************************************************************/
9323
9324 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
9325
9326 static void
9327 redisplay_windows (window)
9328 Lisp_Object window;
9329 {
9330 while (!NILP (window))
9331 {
9332 struct window *w = XWINDOW (window);
9333
9334 if (!NILP (w->hchild))
9335 redisplay_windows (w->hchild);
9336 else if (!NILP (w->vchild))
9337 redisplay_windows (w->vchild);
9338 else
9339 {
9340 displayed_buffer = XBUFFER (w->buffer);
9341 /* Use list_of_error, not Qerror, so that
9342 we catch only errors and don't run the debugger. */
9343 internal_condition_case_1 (redisplay_window_0, window,
9344 list_of_error,
9345 redisplay_window_error);
9346 }
9347
9348 window = w->next;
9349 }
9350 }
9351
9352 static Lisp_Object
9353 redisplay_window_error ()
9354 {
9355 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
9356 return Qnil;
9357 }
9358
9359 static Lisp_Object
9360 redisplay_window_0 (window)
9361 Lisp_Object window;
9362 {
9363 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
9364 redisplay_window (window, 0);
9365 return Qnil;
9366 }
9367
9368 static Lisp_Object
9369 redisplay_window_1 (window)
9370 Lisp_Object window;
9371 {
9372 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
9373 redisplay_window (window, 1);
9374 return Qnil;
9375 }
9376 \f
9377 /* Set cursor position of W. PT is assumed to be displayed in ROW.
9378 DELTA is the number of bytes by which positions recorded in ROW
9379 differ from current buffer positions. */
9380
9381 void
9382 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
9383 struct window *w;
9384 struct glyph_row *row;
9385 struct glyph_matrix *matrix;
9386 int delta, delta_bytes, dy, dvpos;
9387 {
9388 struct glyph *glyph = row->glyphs[TEXT_AREA];
9389 struct glyph *end = glyph + row->used[TEXT_AREA];
9390 int x = row->x;
9391 int pt_old = PT - delta;
9392
9393 /* Skip over glyphs not having an object at the start of the row.
9394 These are special glyphs like truncation marks on terminal
9395 frames. */
9396 if (row->displays_text_p)
9397 while (glyph < end
9398 && INTEGERP (glyph->object)
9399 && glyph->charpos < 0)
9400 {
9401 x += glyph->pixel_width;
9402 ++glyph;
9403 }
9404
9405 while (glyph < end
9406 && !INTEGERP (glyph->object)
9407 && (!BUFFERP (glyph->object)
9408 || glyph->charpos < pt_old))
9409 {
9410 x += glyph->pixel_width;
9411 ++glyph;
9412 }
9413
9414 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
9415 w->cursor.x = x;
9416 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
9417 w->cursor.y = row->y + dy;
9418
9419 if (w == XWINDOW (selected_window))
9420 {
9421 if (!row->continued_p
9422 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
9423 && row->x == 0)
9424 {
9425 this_line_buffer = XBUFFER (w->buffer);
9426
9427 CHARPOS (this_line_start_pos)
9428 = MATRIX_ROW_START_CHARPOS (row) + delta;
9429 BYTEPOS (this_line_start_pos)
9430 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
9431
9432 CHARPOS (this_line_end_pos)
9433 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
9434 BYTEPOS (this_line_end_pos)
9435 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
9436
9437 this_line_y = w->cursor.y;
9438 this_line_pixel_height = row->height;
9439 this_line_vpos = w->cursor.vpos;
9440 this_line_start_x = row->x;
9441 }
9442 else
9443 CHARPOS (this_line_start_pos) = 0;
9444 }
9445 }
9446
9447
9448 /* Run window scroll functions, if any, for WINDOW with new window
9449 start STARTP. Sets the window start of WINDOW to that position.
9450
9451 We assume that the window's buffer is really current. */
9452
9453 static INLINE struct text_pos
9454 run_window_scroll_functions (window, startp)
9455 Lisp_Object window;
9456 struct text_pos startp;
9457 {
9458 struct window *w = XWINDOW (window);
9459 SET_MARKER_FROM_TEXT_POS (w->start, startp);
9460
9461 if (current_buffer != XBUFFER (w->buffer))
9462 abort ();
9463
9464 if (!NILP (Vwindow_scroll_functions))
9465 {
9466 run_hook_with_args_2 (Qwindow_scroll_functions, window,
9467 make_number (CHARPOS (startp)));
9468 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9469 /* In case the hook functions switch buffers. */
9470 if (current_buffer != XBUFFER (w->buffer))
9471 set_buffer_internal_1 (XBUFFER (w->buffer));
9472 }
9473
9474 return startp;
9475 }
9476
9477
9478 /* Make sure the line containing the cursor is fully visible.
9479 A value of 1 means there is nothing to be done.
9480 (Either the line is fully visible, or it cannot be made so,
9481 or we cannot tell.)
9482 A value of 0 means the caller should do scrolling
9483 as if point had gone off the screen. */
9484
9485 static int
9486 make_cursor_line_fully_visible (w)
9487 struct window *w;
9488 {
9489 struct glyph_matrix *matrix;
9490 struct glyph_row *row;
9491 int window_height;
9492
9493 /* It's not always possible to find the cursor, e.g, when a window
9494 is full of overlay strings. Don't do anything in that case. */
9495 if (w->cursor.vpos < 0)
9496 return 1;
9497
9498 matrix = w->desired_matrix;
9499 row = MATRIX_ROW (matrix, w->cursor.vpos);
9500
9501 /* If the cursor row is not partially visible, there's nothing to do. */
9502 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
9503 return 1;
9504
9505 /* If the row the cursor is in is taller than the window's height,
9506 it's not clear what to do, so do nothing. */
9507 window_height = window_box_height (w);
9508 if (row->height >= window_height)
9509 return 1;
9510
9511 return 0;
9512
9513 #if 0
9514 /* This code used to try to scroll the window just enough to make
9515 the line visible. It returned 0 to say that the caller should
9516 allocate larger glyph matrices. */
9517
9518 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
9519 {
9520 int dy = row->height - row->visible_height;
9521 w->vscroll = 0;
9522 w->cursor.y += dy;
9523 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9524 }
9525 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
9526 {
9527 int dy = - (row->height - row->visible_height);
9528 w->vscroll = dy;
9529 w->cursor.y += dy;
9530 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9531 }
9532
9533 /* When we change the cursor y-position of the selected window,
9534 change this_line_y as well so that the display optimization for
9535 the cursor line of the selected window in redisplay_internal uses
9536 the correct y-position. */
9537 if (w == XWINDOW (selected_window))
9538 this_line_y = w->cursor.y;
9539
9540 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
9541 redisplay with larger matrices. */
9542 if (matrix->nrows < required_matrix_height (w))
9543 {
9544 fonts_changed_p = 1;
9545 return 0;
9546 }
9547
9548 return 1;
9549 #endif /* 0 */
9550 }
9551
9552
9553 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
9554 non-zero means only WINDOW is redisplayed in redisplay_internal.
9555 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
9556 in redisplay_window to bring a partially visible line into view in
9557 the case that only the cursor has moved.
9558
9559 Value is
9560
9561 1 if scrolling succeeded
9562
9563 0 if scrolling didn't find point.
9564
9565 -1 if new fonts have been loaded so that we must interrupt
9566 redisplay, adjust glyph matrices, and try again. */
9567
9568 enum
9569 {
9570 SCROLLING_SUCCESS,
9571 SCROLLING_FAILED,
9572 SCROLLING_NEED_LARGER_MATRICES
9573 };
9574
9575 static int
9576 try_scrolling (window, just_this_one_p, scroll_conservatively,
9577 scroll_step, temp_scroll_step)
9578 Lisp_Object window;
9579 int just_this_one_p;
9580 EMACS_INT scroll_conservatively, scroll_step;
9581 int temp_scroll_step;
9582 {
9583 struct window *w = XWINDOW (window);
9584 struct frame *f = XFRAME (w->frame);
9585 struct text_pos scroll_margin_pos;
9586 struct text_pos pos;
9587 struct text_pos startp;
9588 struct it it;
9589 Lisp_Object window_end;
9590 int this_scroll_margin;
9591 int dy = 0;
9592 int scroll_max;
9593 int rc;
9594 int amount_to_scroll = 0;
9595 Lisp_Object aggressive;
9596 int height;
9597
9598 #if GLYPH_DEBUG
9599 debug_method_add (w, "try_scrolling");
9600 #endif
9601
9602 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9603
9604 /* Compute scroll margin height in pixels. We scroll when point is
9605 within this distance from the top or bottom of the window. */
9606 if (scroll_margin > 0)
9607 {
9608 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
9609 this_scroll_margin *= CANON_Y_UNIT (f);
9610 }
9611 else
9612 this_scroll_margin = 0;
9613
9614 /* Compute how much we should try to scroll maximally to bring point
9615 into view. */
9616 if (scroll_step || scroll_conservatively || temp_scroll_step)
9617 scroll_max = max (scroll_step,
9618 max (scroll_conservatively, temp_scroll_step));
9619 else if (NUMBERP (current_buffer->scroll_down_aggressively)
9620 || NUMBERP (current_buffer->scroll_up_aggressively))
9621 /* We're trying to scroll because of aggressive scrolling
9622 but no scroll_step is set. Choose an arbitrary one. Maybe
9623 there should be a variable for this. */
9624 scroll_max = 10;
9625 else
9626 scroll_max = 0;
9627 scroll_max *= CANON_Y_UNIT (f);
9628
9629 /* Decide whether we have to scroll down. Start at the window end
9630 and move this_scroll_margin up to find the position of the scroll
9631 margin. */
9632 window_end = Fwindow_end (window, Qt);
9633 CHARPOS (scroll_margin_pos) = XINT (window_end);
9634 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
9635 if (this_scroll_margin)
9636 {
9637 start_display (&it, w, scroll_margin_pos);
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 int y0;
9645
9646 too_near_end:
9647 /* Point is in the scroll margin at the bottom of the window, or
9648 below. Compute a new window start that makes point visible. */
9649
9650 /* Compute the distance from the scroll margin to PT.
9651 Give up if the distance is greater than scroll_max. */
9652 start_display (&it, w, scroll_margin_pos);
9653 y0 = it.current_y;
9654 move_it_to (&it, PT, 0, it.last_visible_y, -1,
9655 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9656
9657 /* To make point visible, we have to move the window start
9658 down so that the line the cursor is in is visible, which
9659 means we have to add in the height of the cursor line. */
9660 dy = line_bottom_y (&it) - y0;
9661
9662 if (dy > scroll_max)
9663 return SCROLLING_FAILED;
9664
9665 /* Move the window start down. If scrolling conservatively,
9666 move it just enough down to make point visible. If
9667 scroll_step is set, move it down by scroll_step. */
9668 start_display (&it, w, startp);
9669
9670 if (scroll_conservatively)
9671 amount_to_scroll
9672 = max (max (dy, CANON_Y_UNIT (f)),
9673 CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
9674 else if (scroll_step || temp_scroll_step)
9675 amount_to_scroll = scroll_max;
9676 else
9677 {
9678 aggressive = current_buffer->scroll_up_aggressively;
9679 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
9680 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
9681 if (NUMBERP (aggressive))
9682 amount_to_scroll = XFLOATINT (aggressive) * height;
9683 }
9684
9685 if (amount_to_scroll <= 0)
9686 return SCROLLING_FAILED;
9687
9688 move_it_vertically (&it, amount_to_scroll);
9689 startp = it.current.pos;
9690 }
9691 else
9692 {
9693 /* See if point is inside the scroll margin at the top of the
9694 window. */
9695 scroll_margin_pos = startp;
9696 if (this_scroll_margin)
9697 {
9698 start_display (&it, w, startp);
9699 move_it_vertically (&it, this_scroll_margin);
9700 scroll_margin_pos = it.current.pos;
9701 }
9702
9703 if (PT < CHARPOS (scroll_margin_pos))
9704 {
9705 /* Point is in the scroll margin at the top of the window or
9706 above what is displayed in the window. */
9707 int y0;
9708
9709 /* Compute the vertical distance from PT to the scroll
9710 margin position. Give up if distance is greater than
9711 scroll_max. */
9712 SET_TEXT_POS (pos, PT, PT_BYTE);
9713 start_display (&it, w, pos);
9714 y0 = it.current_y;
9715 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
9716 it.last_visible_y, -1,
9717 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9718 dy = it.current_y - y0;
9719 if (dy > scroll_max)
9720 return SCROLLING_FAILED;
9721
9722 /* Compute new window start. */
9723 start_display (&it, w, startp);
9724
9725 if (scroll_conservatively)
9726 amount_to_scroll =
9727 max (dy, CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
9728 else if (scroll_step || temp_scroll_step)
9729 amount_to_scroll = scroll_max;
9730 else
9731 {
9732 aggressive = current_buffer->scroll_down_aggressively;
9733 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
9734 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
9735 if (NUMBERP (aggressive))
9736 amount_to_scroll = XFLOATINT (aggressive) * height;
9737 }
9738
9739 if (amount_to_scroll <= 0)
9740 return SCROLLING_FAILED;
9741
9742 move_it_vertically (&it, - amount_to_scroll);
9743 startp = it.current.pos;
9744 }
9745 }
9746
9747 /* Run window scroll functions. */
9748 startp = run_window_scroll_functions (window, startp);
9749
9750 /* Display the window. Give up if new fonts are loaded, or if point
9751 doesn't appear. */
9752 if (!try_window (window, startp))
9753 rc = SCROLLING_NEED_LARGER_MATRICES;
9754 else if (w->cursor.vpos < 0)
9755 {
9756 clear_glyph_matrix (w->desired_matrix);
9757 rc = SCROLLING_FAILED;
9758 }
9759 else
9760 {
9761 /* Maybe forget recorded base line for line number display. */
9762 if (!just_this_one_p
9763 || current_buffer->clip_changed
9764 || BEG_UNCHANGED < CHARPOS (startp))
9765 w->base_line_number = Qnil;
9766
9767 /* If cursor ends up on a partially visible line,
9768 treat that as being off the bottom of the screen. */
9769 if (! make_cursor_line_fully_visible (w))
9770 goto too_near_end;
9771 rc = SCROLLING_SUCCESS;
9772 }
9773
9774 return rc;
9775 }
9776
9777
9778 /* Compute a suitable window start for window W if display of W starts
9779 on a continuation line. Value is non-zero if a new window start
9780 was computed.
9781
9782 The new window start will be computed, based on W's width, starting
9783 from the start of the continued line. It is the start of the
9784 screen line with the minimum distance from the old start W->start. */
9785
9786 static int
9787 compute_window_start_on_continuation_line (w)
9788 struct window *w;
9789 {
9790 struct text_pos pos, start_pos;
9791 int window_start_changed_p = 0;
9792
9793 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
9794
9795 /* If window start is on a continuation line... Window start may be
9796 < BEGV in case there's invisible text at the start of the
9797 buffer (M-x rmail, for example). */
9798 if (CHARPOS (start_pos) > BEGV
9799 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
9800 {
9801 struct it it;
9802 struct glyph_row *row;
9803
9804 /* Handle the case that the window start is out of range. */
9805 if (CHARPOS (start_pos) < BEGV)
9806 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
9807 else if (CHARPOS (start_pos) > ZV)
9808 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
9809
9810 /* Find the start of the continued line. This should be fast
9811 because scan_buffer is fast (newline cache). */
9812 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
9813 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
9814 row, DEFAULT_FACE_ID);
9815 reseat_at_previous_visible_line_start (&it);
9816
9817 /* If the line start is "too far" away from the window start,
9818 say it takes too much time to compute a new window start. */
9819 if (CHARPOS (start_pos) - IT_CHARPOS (it)
9820 < XFASTINT (w->height) * XFASTINT (w->width))
9821 {
9822 int min_distance, distance;
9823
9824 /* Move forward by display lines to find the new window
9825 start. If window width was enlarged, the new start can
9826 be expected to be > the old start. If window width was
9827 decreased, the new window start will be < the old start.
9828 So, we're looking for the display line start with the
9829 minimum distance from the old window start. */
9830 pos = it.current.pos;
9831 min_distance = INFINITY;
9832 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
9833 distance < min_distance)
9834 {
9835 min_distance = distance;
9836 pos = it.current.pos;
9837 move_it_by_lines (&it, 1, 0);
9838 }
9839
9840 /* Set the window start there. */
9841 SET_MARKER_FROM_TEXT_POS (w->start, pos);
9842 window_start_changed_p = 1;
9843 }
9844 }
9845
9846 return window_start_changed_p;
9847 }
9848
9849
9850 /* Try cursor movement in case text has not changed in window WINDOW,
9851 with window start STARTP. Value is
9852
9853 CURSOR_MOVEMENT_SUCCESS if successful
9854
9855 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
9856
9857 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
9858 display. *SCROLL_STEP is set to 1, under certain circumstances, if
9859 we want to scroll as if scroll-step were set to 1. See the code.
9860
9861 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
9862 which case we have to abort this redisplay, and adjust matrices
9863 first. */
9864
9865 enum
9866 {
9867 CURSOR_MOVEMENT_SUCCESS,
9868 CURSOR_MOVEMENT_CANNOT_BE_USED,
9869 CURSOR_MOVEMENT_MUST_SCROLL,
9870 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
9871 };
9872
9873 static int
9874 try_cursor_movement (window, startp, scroll_step)
9875 Lisp_Object window;
9876 struct text_pos startp;
9877 int *scroll_step;
9878 {
9879 struct window *w = XWINDOW (window);
9880 struct frame *f = XFRAME (w->frame);
9881 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
9882
9883 #if GLYPH_DEBUG
9884 if (inhibit_try_cursor_movement)
9885 return rc;
9886 #endif
9887
9888 /* Handle case where text has not changed, only point, and it has
9889 not moved off the frame. */
9890 if (/* Point may be in this window. */
9891 PT >= CHARPOS (startp)
9892 /* Selective display hasn't changed. */
9893 && !current_buffer->clip_changed
9894 /* Function force-mode-line-update is used to force a thorough
9895 redisplay. It sets either windows_or_buffers_changed or
9896 update_mode_lines. So don't take a shortcut here for these
9897 cases. */
9898 && !update_mode_lines
9899 && !windows_or_buffers_changed
9900 && !cursor_type_changed
9901 /* Can't use this case if highlighting a region. When a
9902 region exists, cursor movement has to do more than just
9903 set the cursor. */
9904 && !(!NILP (Vtransient_mark_mode)
9905 && !NILP (current_buffer->mark_active))
9906 && NILP (w->region_showing)
9907 && NILP (Vshow_trailing_whitespace)
9908 /* Right after splitting windows, last_point may be nil. */
9909 && INTEGERP (w->last_point)
9910 /* This code is not used for mini-buffer for the sake of the case
9911 of redisplaying to replace an echo area message; since in
9912 that case the mini-buffer contents per se are usually
9913 unchanged. This code is of no real use in the mini-buffer
9914 since the handling of this_line_start_pos, etc., in redisplay
9915 handles the same cases. */
9916 && !EQ (window, minibuf_window)
9917 /* When splitting windows or for new windows, it happens that
9918 redisplay is called with a nil window_end_vpos or one being
9919 larger than the window. This should really be fixed in
9920 window.c. I don't have this on my list, now, so we do
9921 approximately the same as the old redisplay code. --gerd. */
9922 && INTEGERP (w->window_end_vpos)
9923 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
9924 && (FRAME_WINDOW_P (f)
9925 || !MARKERP (Voverlay_arrow_position)
9926 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
9927 {
9928 int this_scroll_margin;
9929 struct glyph_row *row = NULL;
9930
9931 #if GLYPH_DEBUG
9932 debug_method_add (w, "cursor movement");
9933 #endif
9934
9935 /* Scroll if point within this distance from the top or bottom
9936 of the window. This is a pixel value. */
9937 this_scroll_margin = max (0, scroll_margin);
9938 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
9939 this_scroll_margin *= CANON_Y_UNIT (f);
9940
9941 /* Start with the row the cursor was displayed during the last
9942 not paused redisplay. Give up if that row is not valid. */
9943 if (w->last_cursor.vpos < 0
9944 || w->last_cursor.vpos >= w->current_matrix->nrows)
9945 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9946 else
9947 {
9948 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
9949 if (row->mode_line_p)
9950 ++row;
9951 if (!row->enabled_p)
9952 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9953 }
9954
9955 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
9956 {
9957 int scroll_p = 0;
9958 int last_y = window_text_bottom_y (w) - this_scroll_margin;
9959
9960 if (PT > XFASTINT (w->last_point))
9961 {
9962 /* Point has moved forward. */
9963 while (MATRIX_ROW_END_CHARPOS (row) < PT
9964 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
9965 {
9966 xassert (row->enabled_p);
9967 ++row;
9968 }
9969
9970 /* The end position of a row equals the start position
9971 of the next row. If PT is there, we would rather
9972 display it in the next line. */
9973 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9974 && MATRIX_ROW_END_CHARPOS (row) == PT
9975 && !cursor_row_p (w, row))
9976 ++row;
9977
9978 /* If within the scroll margin, scroll. Note that
9979 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
9980 the next line would be drawn, and that
9981 this_scroll_margin can be zero. */
9982 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
9983 || PT > MATRIX_ROW_END_CHARPOS (row)
9984 /* Line is completely visible last line in window
9985 and PT is to be set in the next line. */
9986 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
9987 && PT == MATRIX_ROW_END_CHARPOS (row)
9988 && !row->ends_at_zv_p
9989 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
9990 scroll_p = 1;
9991 }
9992 else if (PT < XFASTINT (w->last_point))
9993 {
9994 /* Cursor has to be moved backward. Note that PT >=
9995 CHARPOS (startp) because of the outer
9996 if-statement. */
9997 while (!row->mode_line_p
9998 && (MATRIX_ROW_START_CHARPOS (row) > PT
9999 || (MATRIX_ROW_START_CHARPOS (row) == PT
10000 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
10001 && (row->y > this_scroll_margin
10002 || CHARPOS (startp) == BEGV))
10003 {
10004 xassert (row->enabled_p);
10005 --row;
10006 }
10007
10008 /* Consider the following case: Window starts at BEGV,
10009 there is invisible, intangible text at BEGV, so that
10010 display starts at some point START > BEGV. It can
10011 happen that we are called with PT somewhere between
10012 BEGV and START. Try to handle that case. */
10013 if (row < w->current_matrix->rows
10014 || row->mode_line_p)
10015 {
10016 row = w->current_matrix->rows;
10017 if (row->mode_line_p)
10018 ++row;
10019 }
10020
10021 /* Due to newlines in overlay strings, we may have to
10022 skip forward over overlay strings. */
10023 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
10024 && MATRIX_ROW_END_CHARPOS (row) == PT
10025 && !cursor_row_p (w, row))
10026 ++row;
10027
10028 /* If within the scroll margin, scroll. */
10029 if (row->y < this_scroll_margin
10030 && CHARPOS (startp) != BEGV)
10031 scroll_p = 1;
10032 }
10033
10034 if (PT < MATRIX_ROW_START_CHARPOS (row)
10035 || PT > MATRIX_ROW_END_CHARPOS (row))
10036 {
10037 /* if PT is not in the glyph row, give up. */
10038 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10039 }
10040 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
10041 {
10042 if (PT == MATRIX_ROW_END_CHARPOS (row)
10043 && !row->ends_at_zv_p
10044 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
10045 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10046 else if (row->height > window_box_height (w))
10047 {
10048 /* If we end up in a partially visible line, let's
10049 make it fully visible, except when it's taller
10050 than the window, in which case we can't do much
10051 about it. */
10052 *scroll_step = 1;
10053 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10054 }
10055 else
10056 {
10057 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10058 try_window (window, startp);
10059 if (!make_cursor_line_fully_visible (w))
10060 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10061 else
10062 rc = CURSOR_MOVEMENT_SUCCESS;
10063 }
10064 }
10065 else if (scroll_p)
10066 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10067 else
10068 {
10069 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10070 rc = CURSOR_MOVEMENT_SUCCESS;
10071 }
10072 }
10073 }
10074
10075 return rc;
10076 }
10077
10078
10079 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
10080 selected_window is redisplayed. */
10081
10082 static void
10083 redisplay_window (window, just_this_one_p)
10084 Lisp_Object window;
10085 int just_this_one_p;
10086 {
10087 struct window *w = XWINDOW (window);
10088 struct frame *f = XFRAME (w->frame);
10089 struct buffer *buffer = XBUFFER (w->buffer);
10090 struct buffer *old = current_buffer;
10091 struct text_pos lpoint, opoint, startp;
10092 int update_mode_line;
10093 int tem;
10094 struct it it;
10095 /* Record it now because it's overwritten. */
10096 int current_matrix_up_to_date_p = 0;
10097 /* This is less strict than current_matrix_up_to_date_p.
10098 It indictes that the buffer contents and narrowing are unchanged. */
10099 int buffer_unchanged_p = 0;
10100 int temp_scroll_step = 0;
10101 int count = SPECPDL_INDEX ();
10102 int rc;
10103 int centering_position;
10104
10105 SET_TEXT_POS (lpoint, PT, PT_BYTE);
10106 opoint = lpoint;
10107
10108 /* W must be a leaf window here. */
10109 xassert (!NILP (w->buffer));
10110 #if GLYPH_DEBUG
10111 *w->desired_matrix->method = 0;
10112 #endif
10113
10114 specbind (Qinhibit_point_motion_hooks, Qt);
10115
10116 reconsider_clip_changes (w, buffer);
10117
10118 /* Has the mode line to be updated? */
10119 update_mode_line = (!NILP (w->update_mode_line)
10120 || update_mode_lines
10121 || buffer->clip_changed
10122 || buffer->prevent_redisplay_optimizations_p);
10123
10124 if (MINI_WINDOW_P (w))
10125 {
10126 if (w == XWINDOW (echo_area_window)
10127 && !NILP (echo_area_buffer[0]))
10128 {
10129 if (update_mode_line)
10130 /* We may have to update a tty frame's menu bar or a
10131 tool-bar. Example `M-x C-h C-h C-g'. */
10132 goto finish_menu_bars;
10133 else
10134 /* We've already displayed the echo area glyphs in this window. */
10135 goto finish_scroll_bars;
10136 }
10137 else if (w != XWINDOW (minibuf_window))
10138 {
10139 /* W is a mini-buffer window, but it's not the currently
10140 active one, so clear it. */
10141 int yb = window_text_bottom_y (w);
10142 struct glyph_row *row;
10143 int y;
10144
10145 for (y = 0, row = w->desired_matrix->rows;
10146 y < yb;
10147 y += row->height, ++row)
10148 blank_row (w, row, y);
10149 goto finish_scroll_bars;
10150 }
10151
10152 clear_glyph_matrix (w->desired_matrix);
10153 }
10154
10155 /* Otherwise set up data on this window; select its buffer and point
10156 value. */
10157 /* Really select the buffer, for the sake of buffer-local
10158 variables. */
10159 set_buffer_internal_1 (XBUFFER (w->buffer));
10160 SET_TEXT_POS (opoint, PT, PT_BYTE);
10161
10162 current_matrix_up_to_date_p
10163 = (!NILP (w->window_end_valid)
10164 && !current_buffer->clip_changed
10165 && !current_buffer->prevent_redisplay_optimizations_p
10166 && XFASTINT (w->last_modified) >= MODIFF
10167 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
10168
10169 buffer_unchanged_p
10170 = (!NILP (w->window_end_valid)
10171 && !current_buffer->clip_changed
10172 && END_UNCHANGED + BEG_UNCHANGED >= Z - BEG
10173 && XFASTINT (w->last_modified) >= MODIFF
10174 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
10175
10176 /* When windows_or_buffers_changed is non-zero, we can't rely on
10177 the window end being valid, so set it to nil there. */
10178 if (windows_or_buffers_changed)
10179 {
10180 /* If window starts on a continuation line, maybe adjust the
10181 window start in case the window's width changed. */
10182 if (XMARKER (w->start)->buffer == current_buffer)
10183 compute_window_start_on_continuation_line (w);
10184
10185 w->window_end_valid = Qnil;
10186 }
10187
10188 /* Some sanity checks. */
10189 CHECK_WINDOW_END (w);
10190 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
10191 abort ();
10192 if (BYTEPOS (opoint) < CHARPOS (opoint))
10193 abort ();
10194
10195 /* If %c is in mode line, update it if needed. */
10196 if (!NILP (w->column_number_displayed)
10197 /* This alternative quickly identifies a common case
10198 where no change is needed. */
10199 && !(PT == XFASTINT (w->last_point)
10200 && XFASTINT (w->last_modified) >= MODIFF
10201 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
10202 && (XFASTINT (w->column_number_displayed)
10203 != (int) current_column ())) /* iftc */
10204 update_mode_line = 1;
10205
10206 /* Count number of windows showing the selected buffer. An indirect
10207 buffer counts as its base buffer. */
10208 if (!just_this_one_p)
10209 {
10210 struct buffer *current_base, *window_base;
10211 current_base = current_buffer;
10212 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
10213 if (current_base->base_buffer)
10214 current_base = current_base->base_buffer;
10215 if (window_base->base_buffer)
10216 window_base = window_base->base_buffer;
10217 if (current_base == window_base)
10218 buffer_shared++;
10219 }
10220
10221 /* Point refers normally to the selected window. For any other
10222 window, set up appropriate value. */
10223 if (!EQ (window, selected_window))
10224 {
10225 int new_pt = XMARKER (w->pointm)->charpos;
10226 int new_pt_byte = marker_byte_position (w->pointm);
10227 if (new_pt < BEGV)
10228 {
10229 new_pt = BEGV;
10230 new_pt_byte = BEGV_BYTE;
10231 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
10232 }
10233 else if (new_pt > (ZV - 1))
10234 {
10235 new_pt = ZV;
10236 new_pt_byte = ZV_BYTE;
10237 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
10238 }
10239
10240 /* We don't use SET_PT so that the point-motion hooks don't run. */
10241 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
10242 }
10243
10244 /* If any of the character widths specified in the display table
10245 have changed, invalidate the width run cache. It's true that
10246 this may be a bit late to catch such changes, but the rest of
10247 redisplay goes (non-fatally) haywire when the display table is
10248 changed, so why should we worry about doing any better? */
10249 if (current_buffer->width_run_cache)
10250 {
10251 struct Lisp_Char_Table *disptab = buffer_display_table ();
10252
10253 if (! disptab_matches_widthtab (disptab,
10254 XVECTOR (current_buffer->width_table)))
10255 {
10256 invalidate_region_cache (current_buffer,
10257 current_buffer->width_run_cache,
10258 BEG, Z);
10259 recompute_width_table (current_buffer, disptab);
10260 }
10261 }
10262
10263 /* If window-start is screwed up, choose a new one. */
10264 if (XMARKER (w->start)->buffer != current_buffer)
10265 goto recenter;
10266
10267 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10268
10269 /* If someone specified a new starting point but did not insist,
10270 check whether it can be used. */
10271 if (!NILP (w->optional_new_start)
10272 && CHARPOS (startp) >= BEGV
10273 && CHARPOS (startp) <= ZV)
10274 {
10275 w->optional_new_start = Qnil;
10276 start_display (&it, w, startp);
10277 move_it_to (&it, PT, 0, it.last_visible_y, -1,
10278 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
10279 if (IT_CHARPOS (it) == PT)
10280 w->force_start = Qt;
10281 }
10282
10283 /* Handle case where place to start displaying has been specified,
10284 unless the specified location is outside the accessible range. */
10285 if (!NILP (w->force_start)
10286 || w->frozen_window_start_p)
10287 {
10288 w->force_start = Qnil;
10289 w->vscroll = 0;
10290 w->window_end_valid = Qnil;
10291
10292 /* Forget any recorded base line for line number display. */
10293 if (!buffer_unchanged_p)
10294 w->base_line_number = Qnil;
10295
10296 /* Redisplay the mode line. Select the buffer properly for that.
10297 Also, run the hook window-scroll-functions
10298 because we have scrolled. */
10299 /* Note, we do this after clearing force_start because
10300 if there's an error, it is better to forget about force_start
10301 than to get into an infinite loop calling the hook functions
10302 and having them get more errors. */
10303 if (!update_mode_line
10304 || ! NILP (Vwindow_scroll_functions))
10305 {
10306 update_mode_line = 1;
10307 w->update_mode_line = Qt;
10308 startp = run_window_scroll_functions (window, startp);
10309 }
10310
10311 w->last_modified = make_number (0);
10312 w->last_overlay_modified = make_number (0);
10313 if (CHARPOS (startp) < BEGV)
10314 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
10315 else if (CHARPOS (startp) > ZV)
10316 SET_TEXT_POS (startp, ZV, ZV_BYTE);
10317
10318 /* Redisplay, then check if cursor has been set during the
10319 redisplay. Give up if new fonts were loaded. */
10320 if (!try_window (window, startp))
10321 {
10322 w->force_start = Qt;
10323 clear_glyph_matrix (w->desired_matrix);
10324 goto finish_scroll_bars;
10325 }
10326
10327 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
10328 {
10329 /* If point does not appear, try to move point so it does
10330 appear. The desired matrix has been built above, so we
10331 can use it here. */
10332 int window_height;
10333 struct glyph_row *row;
10334
10335 window_height = window_box_height (w) / 2;
10336 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
10337 while (MATRIX_ROW_BOTTOM_Y (row) < window_height)
10338 ++row;
10339
10340 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
10341 MATRIX_ROW_START_BYTEPOS (row));
10342
10343 if (w != XWINDOW (selected_window))
10344 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
10345 else if (current_buffer == old)
10346 SET_TEXT_POS (lpoint, PT, PT_BYTE);
10347
10348 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
10349
10350 /* If we are highlighting the region, then we just changed
10351 the region, so redisplay to show it. */
10352 if (!NILP (Vtransient_mark_mode)
10353 && !NILP (current_buffer->mark_active))
10354 {
10355 clear_glyph_matrix (w->desired_matrix);
10356 if (!try_window (window, startp))
10357 goto need_larger_matrices;
10358 }
10359 }
10360
10361 if (!make_cursor_line_fully_visible (w))
10362 goto try_to_scroll;
10363 #if GLYPH_DEBUG
10364 debug_method_add (w, "forced window start");
10365 #endif
10366 goto done;
10367 }
10368
10369 /* Handle case where text has not changed, only point, and it has
10370 not moved off the frame. */
10371 if (buffer_unchanged_p
10372 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
10373 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
10374 {
10375 switch (rc)
10376 {
10377 case CURSOR_MOVEMENT_SUCCESS:
10378 goto done;
10379
10380 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
10381 goto need_larger_matrices;
10382
10383 case CURSOR_MOVEMENT_MUST_SCROLL:
10384 goto try_to_scroll;
10385
10386 default:
10387 abort ();
10388 }
10389 }
10390 /* If current starting point was originally the beginning of a line
10391 but no longer is, find a new starting point. */
10392 else if (!NILP (w->start_at_line_beg)
10393 && !(CHARPOS (startp) <= BEGV
10394 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
10395 {
10396 #if GLYPH_DEBUG
10397 debug_method_add (w, "recenter 1");
10398 #endif
10399 goto recenter;
10400 }
10401
10402 /* Try scrolling with try_window_id. Value is > 0 if update has
10403 been done, it is -1 if we know that the same window start will
10404 not work. It is 0 if unsuccessful for some other reason. */
10405 else if ((tem = try_window_id (w)) != 0)
10406 {
10407 #if GLYPH_DEBUG
10408 debug_method_add (w, "try_window_id %d", tem);
10409 #endif
10410
10411 if (fonts_changed_p)
10412 goto need_larger_matrices;
10413 if (tem > 0)
10414 goto done;
10415
10416 /* Otherwise try_window_id has returned -1 which means that we
10417 don't want the alternative below this comment to execute. */
10418 }
10419 else if (CHARPOS (startp) >= BEGV
10420 && CHARPOS (startp) <= ZV
10421 && PT >= CHARPOS (startp)
10422 && (CHARPOS (startp) < ZV
10423 /* Avoid starting at end of buffer. */
10424 || CHARPOS (startp) == BEGV
10425 || (XFASTINT (w->last_modified) >= MODIFF
10426 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
10427 {
10428 #if GLYPH_DEBUG
10429 debug_method_add (w, "same window start");
10430 #endif
10431
10432 /* Try to redisplay starting at same place as before.
10433 If point has not moved off frame, accept the results. */
10434 if (!current_matrix_up_to_date_p
10435 /* Don't use try_window_reusing_current_matrix in this case
10436 because a window scroll function can have changed the
10437 buffer. */
10438 || !NILP (Vwindow_scroll_functions)
10439 || MINI_WINDOW_P (w)
10440 || !try_window_reusing_current_matrix (w))
10441 {
10442 IF_DEBUG (debug_method_add (w, "1"));
10443 try_window (window, startp);
10444 }
10445
10446 if (fonts_changed_p)
10447 goto need_larger_matrices;
10448
10449 if (w->cursor.vpos >= 0)
10450 {
10451 if (!just_this_one_p
10452 || current_buffer->clip_changed
10453 || BEG_UNCHANGED < CHARPOS (startp))
10454 /* Forget any recorded base line for line number display. */
10455 w->base_line_number = Qnil;
10456
10457 if (!make_cursor_line_fully_visible (w))
10458 /* Drop through and scroll. */
10459 ;
10460 goto done;
10461 }
10462 else
10463 clear_glyph_matrix (w->desired_matrix);
10464 }
10465
10466 try_to_scroll:
10467
10468 w->last_modified = make_number (0);
10469 w->last_overlay_modified = make_number (0);
10470
10471 /* Redisplay the mode line. Select the buffer properly for that. */
10472 if (!update_mode_line)
10473 {
10474 update_mode_line = 1;
10475 w->update_mode_line = Qt;
10476 }
10477
10478 /* Try to scroll by specified few lines. */
10479 if ((scroll_conservatively
10480 || scroll_step
10481 || temp_scroll_step
10482 || NUMBERP (current_buffer->scroll_up_aggressively)
10483 || NUMBERP (current_buffer->scroll_down_aggressively))
10484 && !current_buffer->clip_changed
10485 && CHARPOS (startp) >= BEGV
10486 && CHARPOS (startp) <= ZV)
10487 {
10488 /* The function returns -1 if new fonts were loaded, 1 if
10489 successful, 0 if not successful. */
10490 int rc = try_scrolling (window, just_this_one_p,
10491 scroll_conservatively,
10492 scroll_step,
10493 temp_scroll_step);
10494 switch (rc)
10495 {
10496 case SCROLLING_SUCCESS:
10497 goto done;
10498
10499 case SCROLLING_NEED_LARGER_MATRICES:
10500 goto need_larger_matrices;
10501
10502 case SCROLLING_FAILED:
10503 break;
10504
10505 default:
10506 abort ();
10507 }
10508 }
10509
10510 /* Finally, just choose place to start which centers point */
10511
10512 recenter:
10513 centering_position = window_box_height (w) / 2;
10514
10515 point_at_top:
10516 /* Jump here with centering_position already set to 0. */
10517
10518 #if GLYPH_DEBUG
10519 debug_method_add (w, "recenter");
10520 #endif
10521
10522 /* w->vscroll = 0; */
10523
10524 /* Forget any previously recorded base line for line number display. */
10525 if (!buffer_unchanged_p)
10526 w->base_line_number = Qnil;
10527
10528 /* Move backward half the height of the window. */
10529 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10530 it.current_y = it.last_visible_y;
10531 move_it_vertically_backward (&it, centering_position);
10532 xassert (IT_CHARPOS (it) >= BEGV);
10533
10534 /* The function move_it_vertically_backward may move over more
10535 than the specified y-distance. If it->w is small, e.g. a
10536 mini-buffer window, we may end up in front of the window's
10537 display area. Start displaying at the start of the line
10538 containing PT in this case. */
10539 if (it.current_y <= 0)
10540 {
10541 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10542 move_it_vertically (&it, 0);
10543 xassert (IT_CHARPOS (it) <= PT);
10544 it.current_y = 0;
10545 }
10546
10547 it.current_x = it.hpos = 0;
10548
10549 /* Set startp here explicitly in case that helps avoid an infinite loop
10550 in case the window-scroll-functions functions get errors. */
10551 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
10552
10553 /* Run scroll hooks. */
10554 startp = run_window_scroll_functions (window, it.current.pos);
10555
10556 /* Redisplay the window. */
10557 if (!current_matrix_up_to_date_p
10558 || windows_or_buffers_changed
10559 || cursor_type_changed
10560 /* Don't use try_window_reusing_current_matrix in this case
10561 because it can have changed the buffer. */
10562 || !NILP (Vwindow_scroll_functions)
10563 || !just_this_one_p
10564 || MINI_WINDOW_P (w)
10565 || !try_window_reusing_current_matrix (w))
10566 try_window (window, startp);
10567
10568 /* If new fonts have been loaded (due to fontsets), give up. We
10569 have to start a new redisplay since we need to re-adjust glyph
10570 matrices. */
10571 if (fonts_changed_p)
10572 goto need_larger_matrices;
10573
10574 /* If cursor did not appear assume that the middle of the window is
10575 in the first line of the window. Do it again with the next line.
10576 (Imagine a window of height 100, displaying two lines of height
10577 60. Moving back 50 from it->last_visible_y will end in the first
10578 line.) */
10579 if (w->cursor.vpos < 0)
10580 {
10581 if (!NILP (w->window_end_valid)
10582 && PT >= Z - XFASTINT (w->window_end_pos))
10583 {
10584 clear_glyph_matrix (w->desired_matrix);
10585 move_it_by_lines (&it, 1, 0);
10586 try_window (window, it.current.pos);
10587 }
10588 else if (PT < IT_CHARPOS (it))
10589 {
10590 clear_glyph_matrix (w->desired_matrix);
10591 move_it_by_lines (&it, -1, 0);
10592 try_window (window, it.current.pos);
10593 }
10594 else
10595 {
10596 /* Not much we can do about it. */
10597 }
10598 }
10599
10600 /* Consider the following case: Window starts at BEGV, there is
10601 invisible, intangible text at BEGV, so that display starts at
10602 some point START > BEGV. It can happen that we are called with
10603 PT somewhere between BEGV and START. Try to handle that case. */
10604 if (w->cursor.vpos < 0)
10605 {
10606 struct glyph_row *row = w->current_matrix->rows;
10607 if (row->mode_line_p)
10608 ++row;
10609 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10610 }
10611
10612 if (!make_cursor_line_fully_visible (w))
10613 {
10614 /* If centering point failed to make the whole line visible,
10615 put point at the top instead. That has to make the whole line
10616 visible, if it can be done. */
10617 centering_position = 0;
10618 goto point_at_top;
10619 }
10620
10621 done:
10622
10623 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10624 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
10625 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
10626 ? Qt : Qnil);
10627
10628 /* Display the mode line, if we must. */
10629 if ((update_mode_line
10630 /* If window not full width, must redo its mode line
10631 if (a) the window to its side is being redone and
10632 (b) we do a frame-based redisplay. This is a consequence
10633 of how inverted lines are drawn in frame-based redisplay. */
10634 || (!just_this_one_p
10635 && !FRAME_WINDOW_P (f)
10636 && !WINDOW_FULL_WIDTH_P (w))
10637 /* Line number to display. */
10638 || INTEGERP (w->base_line_pos)
10639 /* Column number is displayed and different from the one displayed. */
10640 || (!NILP (w->column_number_displayed)
10641 && (XFASTINT (w->column_number_displayed)
10642 != (int) current_column ()))) /* iftc */
10643 /* This means that the window has a mode line. */
10644 && (WINDOW_WANTS_MODELINE_P (w)
10645 || WINDOW_WANTS_HEADER_LINE_P (w)))
10646 {
10647 display_mode_lines (w);
10648
10649 /* If mode line height has changed, arrange for a thorough
10650 immediate redisplay using the correct mode line height. */
10651 if (WINDOW_WANTS_MODELINE_P (w)
10652 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
10653 {
10654 fonts_changed_p = 1;
10655 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
10656 = DESIRED_MODE_LINE_HEIGHT (w);
10657 }
10658
10659 /* If top line height has changed, arrange for a thorough
10660 immediate redisplay using the correct mode line height. */
10661 if (WINDOW_WANTS_HEADER_LINE_P (w)
10662 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
10663 {
10664 fonts_changed_p = 1;
10665 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
10666 = DESIRED_HEADER_LINE_HEIGHT (w);
10667 }
10668
10669 if (fonts_changed_p)
10670 goto need_larger_matrices;
10671 }
10672
10673 if (!line_number_displayed
10674 && !BUFFERP (w->base_line_pos))
10675 {
10676 w->base_line_pos = Qnil;
10677 w->base_line_number = Qnil;
10678 }
10679
10680 finish_menu_bars:
10681
10682 /* When we reach a frame's selected window, redo the frame's menu bar. */
10683 if (update_mode_line
10684 && EQ (FRAME_SELECTED_WINDOW (f), window))
10685 {
10686 int redisplay_menu_p = 0;
10687
10688 if (FRAME_WINDOW_P (f))
10689 {
10690 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS)
10691 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
10692 #else
10693 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10694 #endif
10695 }
10696 else
10697 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10698
10699 if (redisplay_menu_p)
10700 display_menu_bar (w);
10701
10702 #ifdef HAVE_WINDOW_SYSTEM
10703 if (WINDOWP (f->tool_bar_window)
10704 && (FRAME_TOOL_BAR_LINES (f) > 0
10705 || auto_resize_tool_bars_p))
10706 redisplay_tool_bar (f);
10707 #endif
10708 }
10709
10710 need_larger_matrices:
10711 ;
10712 finish_scroll_bars:
10713
10714 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
10715 {
10716 int start, end, whole;
10717
10718 /* Calculate the start and end positions for the current window.
10719 At some point, it would be nice to choose between scrollbars
10720 which reflect the whole buffer size, with special markers
10721 indicating narrowing, and scrollbars which reflect only the
10722 visible region.
10723
10724 Note that mini-buffers sometimes aren't displaying any text. */
10725 if (!MINI_WINDOW_P (w)
10726 || (w == XWINDOW (minibuf_window)
10727 && NILP (echo_area_buffer[0])))
10728 {
10729 whole = ZV - BEGV;
10730 start = marker_position (w->start) - BEGV;
10731 /* I don't think this is guaranteed to be right. For the
10732 moment, we'll pretend it is. */
10733 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
10734
10735 if (end < start)
10736 end = start;
10737 if (whole < (end - start))
10738 whole = end - start;
10739 }
10740 else
10741 start = end = whole = 0;
10742
10743 /* Indicate what this scroll bar ought to be displaying now. */
10744 set_vertical_scroll_bar_hook (w, end - start, whole, start);
10745
10746 /* Note that we actually used the scroll bar attached to this
10747 window, so it shouldn't be deleted at the end of redisplay. */
10748 redeem_scroll_bar_hook (w);
10749 }
10750
10751 /* Restore current_buffer and value of point in it. */
10752 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
10753 set_buffer_internal_1 (old);
10754 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
10755
10756 unbind_to (count, Qnil);
10757 }
10758
10759
10760 /* Build the complete desired matrix of WINDOW with a window start
10761 buffer position POS. Value is non-zero if successful. It is zero
10762 if fonts were loaded during redisplay which makes re-adjusting
10763 glyph matrices necessary. */
10764
10765 int
10766 try_window (window, pos)
10767 Lisp_Object window;
10768 struct text_pos pos;
10769 {
10770 struct window *w = XWINDOW (window);
10771 struct it it;
10772 struct glyph_row *last_text_row = NULL;
10773
10774 /* Make POS the new window start. */
10775 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
10776
10777 /* Mark cursor position as unknown. No overlay arrow seen. */
10778 w->cursor.vpos = -1;
10779 overlay_arrow_seen = 0;
10780
10781 /* Initialize iterator and info to start at POS. */
10782 start_display (&it, w, pos);
10783
10784 /* Display all lines of W. */
10785 while (it.current_y < it.last_visible_y)
10786 {
10787 if (display_line (&it))
10788 last_text_row = it.glyph_row - 1;
10789 if (fonts_changed_p)
10790 return 0;
10791 }
10792
10793 /* If bottom moved off end of frame, change mode line percentage. */
10794 if (XFASTINT (w->window_end_pos) <= 0
10795 && Z != IT_CHARPOS (it))
10796 w->update_mode_line = Qt;
10797
10798 /* Set window_end_pos to the offset of the last character displayed
10799 on the window from the end of current_buffer. Set
10800 window_end_vpos to its row number. */
10801 if (last_text_row)
10802 {
10803 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
10804 w->window_end_bytepos
10805 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10806 w->window_end_pos
10807 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10808 w->window_end_vpos
10809 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10810 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
10811 ->displays_text_p);
10812 }
10813 else
10814 {
10815 w->window_end_bytepos = 0;
10816 w->window_end_pos = w->window_end_vpos = make_number (0);
10817 }
10818
10819 /* But that is not valid info until redisplay finishes. */
10820 w->window_end_valid = Qnil;
10821 return 1;
10822 }
10823
10824
10825 \f
10826 /************************************************************************
10827 Window redisplay reusing current matrix when buffer has not changed
10828 ************************************************************************/
10829
10830 /* Try redisplay of window W showing an unchanged buffer with a
10831 different window start than the last time it was displayed by
10832 reusing its current matrix. Value is non-zero if successful.
10833 W->start is the new window start. */
10834
10835 static int
10836 try_window_reusing_current_matrix (w)
10837 struct window *w;
10838 {
10839 struct frame *f = XFRAME (w->frame);
10840 struct glyph_row *row, *bottom_row;
10841 struct it it;
10842 struct run run;
10843 struct text_pos start, new_start;
10844 int nrows_scrolled, i;
10845 struct glyph_row *last_text_row;
10846 struct glyph_row *last_reused_text_row;
10847 struct glyph_row *start_row;
10848 int start_vpos, min_y, max_y;
10849
10850 #if GLYPH_DEBUG
10851 if (inhibit_try_window_reusing)
10852 return 0;
10853 #endif
10854
10855 if (/* This function doesn't handle terminal frames. */
10856 !FRAME_WINDOW_P (f)
10857 /* Don't try to reuse the display if windows have been split
10858 or such. */
10859 || windows_or_buffers_changed
10860 || cursor_type_changed)
10861 return 0;
10862
10863 /* Can't do this if region may have changed. */
10864 if ((!NILP (Vtransient_mark_mode)
10865 && !NILP (current_buffer->mark_active))
10866 || !NILP (w->region_showing)
10867 || !NILP (Vshow_trailing_whitespace))
10868 return 0;
10869
10870 /* If top-line visibility has changed, give up. */
10871 if (WINDOW_WANTS_HEADER_LINE_P (w)
10872 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
10873 return 0;
10874
10875 /* Give up if old or new display is scrolled vertically. We could
10876 make this function handle this, but right now it doesn't. */
10877 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10878 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
10879 return 0;
10880
10881 /* The variable new_start now holds the new window start. The old
10882 start `start' can be determined from the current matrix. */
10883 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
10884 start = start_row->start.pos;
10885 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
10886
10887 /* Clear the desired matrix for the display below. */
10888 clear_glyph_matrix (w->desired_matrix);
10889
10890 if (CHARPOS (new_start) <= CHARPOS (start))
10891 {
10892 int first_row_y;
10893
10894 /* Don't use this method if the display starts with an ellipsis
10895 displayed for invisible text. It's not easy to handle that case
10896 below, and it's certainly not worth the effort since this is
10897 not a frequent case. */
10898 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
10899 return 0;
10900
10901 IF_DEBUG (debug_method_add (w, "twu1"));
10902
10903 /* Display up to a row that can be reused. The variable
10904 last_text_row is set to the last row displayed that displays
10905 text. Note that it.vpos == 0 if or if not there is a
10906 header-line; it's not the same as the MATRIX_ROW_VPOS! */
10907 start_display (&it, w, new_start);
10908 first_row_y = it.current_y;
10909 w->cursor.vpos = -1;
10910 last_text_row = last_reused_text_row = NULL;
10911
10912 while (it.current_y < it.last_visible_y
10913 && IT_CHARPOS (it) < CHARPOS (start)
10914 && !fonts_changed_p)
10915 if (display_line (&it))
10916 last_text_row = it.glyph_row - 1;
10917
10918 /* A value of current_y < last_visible_y means that we stopped
10919 at the previous window start, which in turn means that we
10920 have at least one reusable row. */
10921 if (it.current_y < it.last_visible_y)
10922 {
10923 /* IT.vpos always starts from 0; it counts text lines. */
10924 nrows_scrolled = it.vpos;
10925
10926 /* Find PT if not already found in the lines displayed. */
10927 if (w->cursor.vpos < 0)
10928 {
10929 int dy = it.current_y - first_row_y;
10930
10931 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10932 row = row_containing_pos (w, PT, row, NULL, dy);
10933 if (row)
10934 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
10935 dy, nrows_scrolled);
10936 else
10937 {
10938 clear_glyph_matrix (w->desired_matrix);
10939 return 0;
10940 }
10941 }
10942
10943 /* Scroll the display. Do it before the current matrix is
10944 changed. The problem here is that update has not yet
10945 run, i.e. part of the current matrix is not up to date.
10946 scroll_run_hook will clear the cursor, and use the
10947 current matrix to get the height of the row the cursor is
10948 in. */
10949 run.current_y = first_row_y;
10950 run.desired_y = it.current_y;
10951 run.height = it.last_visible_y - it.current_y;
10952
10953 if (run.height > 0 && run.current_y != run.desired_y)
10954 {
10955 update_begin (f);
10956 rif->update_window_begin_hook (w);
10957 rif->clear_mouse_face (w);
10958 rif->scroll_run_hook (w, &run);
10959 rif->update_window_end_hook (w, 0, 0);
10960 update_end (f);
10961 }
10962
10963 /* Shift current matrix down by nrows_scrolled lines. */
10964 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
10965 rotate_matrix (w->current_matrix,
10966 start_vpos,
10967 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10968 nrows_scrolled);
10969
10970 /* Disable lines that must be updated. */
10971 for (i = 0; i < it.vpos; ++i)
10972 (start_row + i)->enabled_p = 0;
10973
10974 /* Re-compute Y positions. */
10975 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10976 max_y = it.last_visible_y;
10977 for (row = start_row + nrows_scrolled;
10978 row < bottom_row;
10979 ++row)
10980 {
10981 row->y = it.current_y;
10982 row->visible_height = row->height;
10983
10984 if (row->y < min_y)
10985 row->visible_height -= min_y - row->y;
10986 if (row->y + row->height > max_y)
10987 row->visible_height -= row->y + row->height - max_y;
10988
10989 it.current_y += row->height;
10990
10991 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10992 last_reused_text_row = row;
10993 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
10994 break;
10995 }
10996
10997 /* Disable lines in the current matrix which are now
10998 below the window. */
10999 for (++row; row < bottom_row; ++row)
11000 row->enabled_p = 0;
11001 }
11002
11003 /* Update window_end_pos etc.; last_reused_text_row is the last
11004 reused row from the current matrix containing text, if any.
11005 The value of last_text_row is the last displayed line
11006 containing text. */
11007 if (last_reused_text_row)
11008 {
11009 w->window_end_bytepos
11010 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
11011 w->window_end_pos
11012 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
11013 w->window_end_vpos
11014 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
11015 w->current_matrix));
11016 }
11017 else if (last_text_row)
11018 {
11019 w->window_end_bytepos
11020 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
11021 w->window_end_pos
11022 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
11023 w->window_end_vpos
11024 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
11025 }
11026 else
11027 {
11028 /* This window must be completely empty. */
11029 w->window_end_bytepos = 0;
11030 w->window_end_pos = w->window_end_vpos = make_number (0);
11031 }
11032 w->window_end_valid = Qnil;
11033
11034 /* Update hint: don't try scrolling again in update_window. */
11035 w->desired_matrix->no_scrolling_p = 1;
11036
11037 #if GLYPH_DEBUG
11038 debug_method_add (w, "try_window_reusing_current_matrix 1");
11039 #endif
11040 return 1;
11041 }
11042 else if (CHARPOS (new_start) > CHARPOS (start))
11043 {
11044 struct glyph_row *pt_row, *row;
11045 struct glyph_row *first_reusable_row;
11046 struct glyph_row *first_row_to_display;
11047 int dy;
11048 int yb = window_text_bottom_y (w);
11049
11050 /* Find the row starting at new_start, if there is one. Don't
11051 reuse a partially visible line at the end. */
11052 first_reusable_row = start_row;
11053 while (first_reusable_row->enabled_p
11054 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
11055 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
11056 < CHARPOS (new_start)))
11057 ++first_reusable_row;
11058
11059 /* Give up if there is no row to reuse. */
11060 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
11061 || !first_reusable_row->enabled_p
11062 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
11063 != CHARPOS (new_start)))
11064 return 0;
11065
11066 /* We can reuse fully visible rows beginning with
11067 first_reusable_row to the end of the window. Set
11068 first_row_to_display to the first row that cannot be reused.
11069 Set pt_row to the row containing point, if there is any. */
11070 pt_row = NULL;
11071 for (first_row_to_display = first_reusable_row;
11072 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
11073 ++first_row_to_display)
11074 {
11075 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
11076 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
11077 pt_row = first_row_to_display;
11078 }
11079
11080 /* Start displaying at the start of first_row_to_display. */
11081 xassert (first_row_to_display->y < yb);
11082 init_to_row_start (&it, w, first_row_to_display);
11083
11084 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
11085 - start_vpos);
11086 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
11087 - nrows_scrolled);
11088 it.current_y = (first_row_to_display->y - first_reusable_row->y
11089 + WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
11090
11091 /* Display lines beginning with first_row_to_display in the
11092 desired matrix. Set last_text_row to the last row displayed
11093 that displays text. */
11094 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
11095 if (pt_row == NULL)
11096 w->cursor.vpos = -1;
11097 last_text_row = NULL;
11098 while (it.current_y < it.last_visible_y && !fonts_changed_p)
11099 if (display_line (&it))
11100 last_text_row = it.glyph_row - 1;
11101
11102 /* Give up If point isn't in a row displayed or reused. */
11103 if (w->cursor.vpos < 0)
11104 {
11105 clear_glyph_matrix (w->desired_matrix);
11106 return 0;
11107 }
11108
11109 /* If point is in a reused row, adjust y and vpos of the cursor
11110 position. */
11111 if (pt_row)
11112 {
11113 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
11114 w->current_matrix);
11115 w->cursor.y -= first_reusable_row->y;
11116 }
11117
11118 /* Scroll the display. */
11119 run.current_y = first_reusable_row->y;
11120 run.desired_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
11121 run.height = it.last_visible_y - run.current_y;
11122 dy = run.current_y - run.desired_y;
11123
11124 if (run.height)
11125 {
11126 struct frame *f = XFRAME (WINDOW_FRAME (w));
11127 update_begin (f);
11128 rif->update_window_begin_hook (w);
11129 rif->clear_mouse_face (w);
11130 rif->scroll_run_hook (w, &run);
11131 rif->update_window_end_hook (w, 0, 0);
11132 update_end (f);
11133 }
11134
11135 /* Adjust Y positions of reused rows. */
11136 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
11137 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
11138 max_y = it.last_visible_y;
11139 for (row = first_reusable_row; row < first_row_to_display; ++row)
11140 {
11141 row->y -= dy;
11142 row->visible_height = row->height;
11143 if (row->y < min_y)
11144 row->visible_height -= min_y - row->y;
11145 if (row->y + row->height > max_y)
11146 row->visible_height -= row->y + row->height - max_y;
11147 }
11148
11149 /* Scroll the current matrix. */
11150 xassert (nrows_scrolled > 0);
11151 rotate_matrix (w->current_matrix,
11152 start_vpos,
11153 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
11154 -nrows_scrolled);
11155
11156 /* Disable rows not reused. */
11157 for (row -= nrows_scrolled; row < bottom_row; ++row)
11158 row->enabled_p = 0;
11159
11160 /* Adjust window end. A null value of last_text_row means that
11161 the window end is in reused rows which in turn means that
11162 only its vpos can have changed. */
11163 if (last_text_row)
11164 {
11165 w->window_end_bytepos
11166 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
11167 w->window_end_pos
11168 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
11169 w->window_end_vpos
11170 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
11171 }
11172 else
11173 {
11174 w->window_end_vpos
11175 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
11176 }
11177
11178 w->window_end_valid = Qnil;
11179 w->desired_matrix->no_scrolling_p = 1;
11180
11181 #if GLYPH_DEBUG
11182 debug_method_add (w, "try_window_reusing_current_matrix 2");
11183 #endif
11184 return 1;
11185 }
11186
11187 return 0;
11188 }
11189
11190
11191 \f
11192 /************************************************************************
11193 Window redisplay reusing current matrix when buffer has changed
11194 ************************************************************************/
11195
11196 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
11197 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
11198 int *, int *));
11199 static struct glyph_row *
11200 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
11201 struct glyph_row *));
11202
11203
11204 /* Return the last row in MATRIX displaying text. If row START is
11205 non-null, start searching with that row. IT gives the dimensions
11206 of the display. Value is null if matrix is empty; otherwise it is
11207 a pointer to the row found. */
11208
11209 static struct glyph_row *
11210 find_last_row_displaying_text (matrix, it, start)
11211 struct glyph_matrix *matrix;
11212 struct it *it;
11213 struct glyph_row *start;
11214 {
11215 struct glyph_row *row, *row_found;
11216
11217 /* Set row_found to the last row in IT->w's current matrix
11218 displaying text. The loop looks funny but think of partially
11219 visible lines. */
11220 row_found = NULL;
11221 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
11222 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
11223 {
11224 xassert (row->enabled_p);
11225 row_found = row;
11226 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
11227 break;
11228 ++row;
11229 }
11230
11231 return row_found;
11232 }
11233
11234
11235 /* Return the last row in the current matrix of W that is not affected
11236 by changes at the start of current_buffer that occurred since W's
11237 current matrix was built. Value is null if no such row exists.
11238
11239 BEG_UNCHANGED us the number of characters unchanged at the start of
11240 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
11241 first changed character in current_buffer. Characters at positions <
11242 BEG + BEG_UNCHANGED are at the same buffer positions as they were
11243 when the current matrix was built. */
11244
11245 static struct glyph_row *
11246 find_last_unchanged_at_beg_row (w)
11247 struct window *w;
11248 {
11249 int first_changed_pos = BEG + BEG_UNCHANGED;
11250 struct glyph_row *row;
11251 struct glyph_row *row_found = NULL;
11252 int yb = window_text_bottom_y (w);
11253
11254 /* Find the last row displaying unchanged text. */
11255 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11256 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
11257 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
11258 {
11259 if (/* If row ends before first_changed_pos, it is unchanged,
11260 except in some case. */
11261 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
11262 /* When row ends in ZV and we write at ZV it is not
11263 unchanged. */
11264 && !row->ends_at_zv_p
11265 /* When first_changed_pos is the end of a continued line,
11266 row is not unchanged because it may be no longer
11267 continued. */
11268 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
11269 && row->continued_p))
11270 row_found = row;
11271
11272 /* Stop if last visible row. */
11273 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
11274 break;
11275
11276 ++row;
11277 }
11278
11279 return row_found;
11280 }
11281
11282
11283 /* Find the first glyph row in the current matrix of W that is not
11284 affected by changes at the end of current_buffer since the
11285 time W's current matrix was built.
11286
11287 Return in *DELTA the number of chars by which buffer positions in
11288 unchanged text at the end of current_buffer must be adjusted.
11289
11290 Return in *DELTA_BYTES the corresponding number of bytes.
11291
11292 Value is null if no such row exists, i.e. all rows are affected by
11293 changes. */
11294
11295 static struct glyph_row *
11296 find_first_unchanged_at_end_row (w, delta, delta_bytes)
11297 struct window *w;
11298 int *delta, *delta_bytes;
11299 {
11300 struct glyph_row *row;
11301 struct glyph_row *row_found = NULL;
11302
11303 *delta = *delta_bytes = 0;
11304
11305 /* Display must not have been paused, otherwise the current matrix
11306 is not up to date. */
11307 if (NILP (w->window_end_valid))
11308 abort ();
11309
11310 /* A value of window_end_pos >= END_UNCHANGED means that the window
11311 end is in the range of changed text. If so, there is no
11312 unchanged row at the end of W's current matrix. */
11313 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
11314 return NULL;
11315
11316 /* Set row to the last row in W's current matrix displaying text. */
11317 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
11318
11319 /* If matrix is entirely empty, no unchanged row exists. */
11320 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
11321 {
11322 /* The value of row is the last glyph row in the matrix having a
11323 meaningful buffer position in it. The end position of row
11324 corresponds to window_end_pos. This allows us to translate
11325 buffer positions in the current matrix to current buffer
11326 positions for characters not in changed text. */
11327 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
11328 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
11329 int last_unchanged_pos, last_unchanged_pos_old;
11330 struct glyph_row *first_text_row
11331 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11332
11333 *delta = Z - Z_old;
11334 *delta_bytes = Z_BYTE - Z_BYTE_old;
11335
11336 /* Set last_unchanged_pos to the buffer position of the last
11337 character in the buffer that has not been changed. Z is the
11338 index + 1 of the last character in current_buffer, i.e. by
11339 subtracting END_UNCHANGED we get the index of the last
11340 unchanged character, and we have to add BEG to get its buffer
11341 position. */
11342 last_unchanged_pos = Z - END_UNCHANGED + BEG;
11343 last_unchanged_pos_old = last_unchanged_pos - *delta;
11344
11345 /* Search backward from ROW for a row displaying a line that
11346 starts at a minimum position >= last_unchanged_pos_old. */
11347 for (; row > first_text_row; --row)
11348 {
11349 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
11350 abort ();
11351
11352 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
11353 row_found = row;
11354 }
11355 }
11356
11357 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
11358 abort ();
11359
11360 return row_found;
11361 }
11362
11363
11364 /* Make sure that glyph rows in the current matrix of window W
11365 reference the same glyph memory as corresponding rows in the
11366 frame's frame matrix. This function is called after scrolling W's
11367 current matrix on a terminal frame in try_window_id and
11368 try_window_reusing_current_matrix. */
11369
11370 static void
11371 sync_frame_with_window_matrix_rows (w)
11372 struct window *w;
11373 {
11374 struct frame *f = XFRAME (w->frame);
11375 struct glyph_row *window_row, *window_row_end, *frame_row;
11376
11377 /* Preconditions: W must be a leaf window and full-width. Its frame
11378 must have a frame matrix. */
11379 xassert (NILP (w->hchild) && NILP (w->vchild));
11380 xassert (WINDOW_FULL_WIDTH_P (w));
11381 xassert (!FRAME_WINDOW_P (f));
11382
11383 /* If W is a full-width window, glyph pointers in W's current matrix
11384 have, by definition, to be the same as glyph pointers in the
11385 corresponding frame matrix. Note that frame matrices have no
11386 marginal areas (see build_frame_matrix). */
11387 window_row = w->current_matrix->rows;
11388 window_row_end = window_row + w->current_matrix->nrows;
11389 frame_row = f->current_matrix->rows + XFASTINT (w->top);
11390 while (window_row < window_row_end)
11391 {
11392 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
11393 struct glyph *end = window_row->glyphs[LAST_AREA];
11394
11395 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
11396 frame_row->glyphs[TEXT_AREA] = start;
11397 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
11398 frame_row->glyphs[LAST_AREA] = end;
11399
11400 /* Disable frame rows whose corresponding window rows have
11401 been disabled in try_window_id. */
11402 if (!window_row->enabled_p)
11403 frame_row->enabled_p = 0;
11404
11405 ++window_row, ++frame_row;
11406 }
11407 }
11408
11409
11410 /* Find the glyph row in window W containing CHARPOS. Consider all
11411 rows between START and END (not inclusive). END null means search
11412 all rows to the end of the display area of W. Value is the row
11413 containing CHARPOS or null. */
11414
11415 struct glyph_row *
11416 row_containing_pos (w, charpos, start, end, dy)
11417 struct window *w;
11418 int charpos;
11419 struct glyph_row *start, *end;
11420 int dy;
11421 {
11422 struct glyph_row *row = start;
11423 int last_y;
11424
11425 /* If we happen to start on a header-line, skip that. */
11426 if (row->mode_line_p)
11427 ++row;
11428
11429 if ((end && row >= end) || !row->enabled_p)
11430 return NULL;
11431
11432 last_y = window_text_bottom_y (w) - dy;
11433
11434 while ((end == NULL || row < end)
11435 && MATRIX_ROW_BOTTOM_Y (row) < last_y
11436 && (MATRIX_ROW_END_CHARPOS (row) < charpos
11437 || (MATRIX_ROW_END_CHARPOS (row) == charpos
11438 /* The end position of a row equals the start
11439 position of the next row. If CHARPOS is there, we
11440 would rather display it in the next line, except
11441 when this line ends in ZV. */
11442 && !row->ends_at_zv_p
11443 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))
11444 ++row;
11445
11446 /* Give up if CHARPOS not found. */
11447 if ((end && row >= end)
11448 || charpos < MATRIX_ROW_START_CHARPOS (row)
11449 || charpos > MATRIX_ROW_END_CHARPOS (row))
11450 row = NULL;
11451
11452 return row;
11453 }
11454
11455
11456 /* Try to redisplay window W by reusing its existing display. W's
11457 current matrix must be up to date when this function is called,
11458 i.e. window_end_valid must not be nil.
11459
11460 Value is
11461
11462 1 if display has been updated
11463 0 if otherwise unsuccessful
11464 -1 if redisplay with same window start is known not to succeed
11465
11466 The following steps are performed:
11467
11468 1. Find the last row in the current matrix of W that is not
11469 affected by changes at the start of current_buffer. If no such row
11470 is found, give up.
11471
11472 2. Find the first row in W's current matrix that is not affected by
11473 changes at the end of current_buffer. Maybe there is no such row.
11474
11475 3. Display lines beginning with the row + 1 found in step 1 to the
11476 row found in step 2 or, if step 2 didn't find a row, to the end of
11477 the window.
11478
11479 4. If cursor is not known to appear on the window, give up.
11480
11481 5. If display stopped at the row found in step 2, scroll the
11482 display and current matrix as needed.
11483
11484 6. Maybe display some lines at the end of W, if we must. This can
11485 happen under various circumstances, like a partially visible line
11486 becoming fully visible, or because newly displayed lines are displayed
11487 in smaller font sizes.
11488
11489 7. Update W's window end information. */
11490
11491 static int
11492 try_window_id (w)
11493 struct window *w;
11494 {
11495 struct frame *f = XFRAME (w->frame);
11496 struct glyph_matrix *current_matrix = w->current_matrix;
11497 struct glyph_matrix *desired_matrix = w->desired_matrix;
11498 struct glyph_row *last_unchanged_at_beg_row;
11499 struct glyph_row *first_unchanged_at_end_row;
11500 struct glyph_row *row;
11501 struct glyph_row *bottom_row;
11502 int bottom_vpos;
11503 struct it it;
11504 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
11505 struct text_pos start_pos;
11506 struct run run;
11507 int first_unchanged_at_end_vpos = 0;
11508 struct glyph_row *last_text_row, *last_text_row_at_end;
11509 struct text_pos start;
11510 int first_changed_charpos, last_changed_charpos;
11511
11512 #if GLYPH_DEBUG
11513 if (inhibit_try_window_id)
11514 return 0;
11515 #endif
11516
11517 /* This is handy for debugging. */
11518 #if 0
11519 #define GIVE_UP(X) \
11520 do { \
11521 fprintf (stderr, "try_window_id give up %d\n", (X)); \
11522 return 0; \
11523 } while (0)
11524 #else
11525 #define GIVE_UP(X) return 0
11526 #endif
11527
11528 SET_TEXT_POS_FROM_MARKER (start, w->start);
11529
11530 /* Don't use this for mini-windows because these can show
11531 messages and mini-buffers, and we don't handle that here. */
11532 if (MINI_WINDOW_P (w))
11533 GIVE_UP (1);
11534
11535 /* This flag is used to prevent redisplay optimizations. */
11536 if (windows_or_buffers_changed || cursor_type_changed)
11537 GIVE_UP (2);
11538
11539 /* Verify that narrowing has not changed.
11540 Also verify that we were not told to prevent redisplay optimizations.
11541 It would be nice to further
11542 reduce the number of cases where this prevents try_window_id. */
11543 if (current_buffer->clip_changed
11544 || current_buffer->prevent_redisplay_optimizations_p)
11545 GIVE_UP (3);
11546
11547 /* Window must either use window-based redisplay or be full width. */
11548 if (!FRAME_WINDOW_P (f)
11549 && (!line_ins_del_ok
11550 || !WINDOW_FULL_WIDTH_P (w)))
11551 GIVE_UP (4);
11552
11553 /* Give up if point is not known NOT to appear in W. */
11554 if (PT < CHARPOS (start))
11555 GIVE_UP (5);
11556
11557 /* Another way to prevent redisplay optimizations. */
11558 if (XFASTINT (w->last_modified) == 0)
11559 GIVE_UP (6);
11560
11561 /* Verify that window is not hscrolled. */
11562 if (XFASTINT (w->hscroll) != 0)
11563 GIVE_UP (7);
11564
11565 /* Verify that display wasn't paused. */
11566 if (NILP (w->window_end_valid))
11567 GIVE_UP (8);
11568
11569 /* Can't use this if highlighting a region because a cursor movement
11570 will do more than just set the cursor. */
11571 if (!NILP (Vtransient_mark_mode)
11572 && !NILP (current_buffer->mark_active))
11573 GIVE_UP (9);
11574
11575 /* Likewise if highlighting trailing whitespace. */
11576 if (!NILP (Vshow_trailing_whitespace))
11577 GIVE_UP (11);
11578
11579 /* Likewise if showing a region. */
11580 if (!NILP (w->region_showing))
11581 GIVE_UP (10);
11582
11583 /* Can use this if overlay arrow position and or string have changed. */
11584 if (!EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
11585 || !EQ (last_arrow_string, Voverlay_arrow_string))
11586 GIVE_UP (12);
11587
11588
11589 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
11590 only if buffer has really changed. The reason is that the gap is
11591 initially at Z for freshly visited files. The code below would
11592 set end_unchanged to 0 in that case. */
11593 if (MODIFF > SAVE_MODIFF
11594 /* This seems to happen sometimes after saving a buffer. */
11595 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
11596 {
11597 if (GPT - BEG < BEG_UNCHANGED)
11598 BEG_UNCHANGED = GPT - BEG;
11599 if (Z - GPT < END_UNCHANGED)
11600 END_UNCHANGED = Z - GPT;
11601 }
11602
11603 /* The position of the first and last character that has been changed. */
11604 first_changed_charpos = BEG + BEG_UNCHANGED;
11605 last_changed_charpos = Z - END_UNCHANGED;
11606
11607 /* If window starts after a line end, and the last change is in
11608 front of that newline, then changes don't affect the display.
11609 This case happens with stealth-fontification. Note that although
11610 the display is unchanged, glyph positions in the matrix have to
11611 be adjusted, of course. */
11612 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
11613 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
11614 && ((last_changed_charpos < CHARPOS (start)
11615 && CHARPOS (start) == BEGV)
11616 || (last_changed_charpos < CHARPOS (start) - 1
11617 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
11618 {
11619 int Z_old, delta, Z_BYTE_old, delta_bytes;
11620 struct glyph_row *r0;
11621
11622 /* Compute how many chars/bytes have been added to or removed
11623 from the buffer. */
11624 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
11625 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
11626 delta = Z - Z_old;
11627 delta_bytes = Z_BYTE - Z_BYTE_old;
11628
11629 /* Give up if PT is not in the window. Note that it already has
11630 been checked at the start of try_window_id that PT is not in
11631 front of the window start. */
11632 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
11633 GIVE_UP (13);
11634
11635 /* If window start is unchanged, we can reuse the whole matrix
11636 as is, after adjusting glyph positions. No need to compute
11637 the window end again, since its offset from Z hasn't changed. */
11638 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
11639 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
11640 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes)
11641 {
11642 /* Adjust positions in the glyph matrix. */
11643 if (delta || delta_bytes)
11644 {
11645 struct glyph_row *r1
11646 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11647 increment_matrix_positions (w->current_matrix,
11648 MATRIX_ROW_VPOS (r0, current_matrix),
11649 MATRIX_ROW_VPOS (r1, current_matrix),
11650 delta, delta_bytes);
11651 }
11652
11653 /* Set the cursor. */
11654 row = row_containing_pos (w, PT, r0, NULL, 0);
11655 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
11656 return 1;
11657 }
11658 }
11659
11660 /* Handle the case that changes are all below what is displayed in
11661 the window, and that PT is in the window. This shortcut cannot
11662 be taken if ZV is visible in the window, and text has been added
11663 there that is visible in the window. */
11664 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
11665 /* ZV is not visible in the window, or there are no
11666 changes at ZV, actually. */
11667 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
11668 || first_changed_charpos == last_changed_charpos))
11669 {
11670 struct glyph_row *r0;
11671
11672 /* Give up if PT is not in the window. Note that it already has
11673 been checked at the start of try_window_id that PT is not in
11674 front of the window start. */
11675 if (PT >= MATRIX_ROW_END_CHARPOS (row))
11676 GIVE_UP (14);
11677
11678 /* If window start is unchanged, we can reuse the whole matrix
11679 as is, without changing glyph positions since no text has
11680 been added/removed in front of the window end. */
11681 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
11682 if (TEXT_POS_EQUAL_P (start, r0->start.pos))
11683 {
11684 /* We have to compute the window end anew since text
11685 can have been added/removed after it. */
11686 w->window_end_pos
11687 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
11688 w->window_end_bytepos
11689 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11690
11691 /* Set the cursor. */
11692 row = row_containing_pos (w, PT, r0, NULL, 0);
11693 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
11694 return 2;
11695 }
11696 }
11697
11698 /* Give up if window start is in the changed area.
11699
11700 The condition used to read
11701
11702 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
11703
11704 but why that was tested escapes me at the moment. */
11705 if (CHARPOS (start) >= first_changed_charpos
11706 && CHARPOS (start) <= last_changed_charpos)
11707 GIVE_UP (15);
11708
11709 /* Check that window start agrees with the start of the first glyph
11710 row in its current matrix. Check this after we know the window
11711 start is not in changed text, otherwise positions would not be
11712 comparable. */
11713 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
11714 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
11715 GIVE_UP (16);
11716
11717 /* Give up if the window ends in strings. Overlay strings
11718 at the end are difficult to handle, so don't try. */
11719 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
11720 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
11721 GIVE_UP (20);
11722
11723 /* Compute the position at which we have to start displaying new
11724 lines. Some of the lines at the top of the window might be
11725 reusable because they are not displaying changed text. Find the
11726 last row in W's current matrix not affected by changes at the
11727 start of current_buffer. Value is null if changes start in the
11728 first line of window. */
11729 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
11730 if (last_unchanged_at_beg_row)
11731 {
11732 /* Avoid starting to display in the moddle of a character, a TAB
11733 for instance. This is easier than to set up the iterator
11734 exactly, and it's not a frequent case, so the additional
11735 effort wouldn't really pay off. */
11736 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
11737 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
11738 && last_unchanged_at_beg_row > w->current_matrix->rows)
11739 --last_unchanged_at_beg_row;
11740
11741 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
11742 GIVE_UP (17);
11743
11744 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
11745 GIVE_UP (18);
11746 start_pos = it.current.pos;
11747
11748 /* Start displaying new lines in the desired matrix at the same
11749 vpos we would use in the current matrix, i.e. below
11750 last_unchanged_at_beg_row. */
11751 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
11752 current_matrix);
11753 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11754 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
11755
11756 xassert (it.hpos == 0 && it.current_x == 0);
11757 }
11758 else
11759 {
11760 /* There are no reusable lines at the start of the window.
11761 Start displaying in the first line. */
11762 start_display (&it, w, start);
11763 start_pos = it.current.pos;
11764 }
11765
11766 /* Find the first row that is not affected by changes at the end of
11767 the buffer. Value will be null if there is no unchanged row, in
11768 which case we must redisplay to the end of the window. delta
11769 will be set to the value by which buffer positions beginning with
11770 first_unchanged_at_end_row have to be adjusted due to text
11771 changes. */
11772 first_unchanged_at_end_row
11773 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
11774 IF_DEBUG (debug_delta = delta);
11775 IF_DEBUG (debug_delta_bytes = delta_bytes);
11776
11777 /* Set stop_pos to the buffer position up to which we will have to
11778 display new lines. If first_unchanged_at_end_row != NULL, this
11779 is the buffer position of the start of the line displayed in that
11780 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
11781 that we don't stop at a buffer position. */
11782 stop_pos = 0;
11783 if (first_unchanged_at_end_row)
11784 {
11785 xassert (last_unchanged_at_beg_row == NULL
11786 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
11787
11788 /* If this is a continuation line, move forward to the next one
11789 that isn't. Changes in lines above affect this line.
11790 Caution: this may move first_unchanged_at_end_row to a row
11791 not displaying text. */
11792 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
11793 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11794 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11795 < it.last_visible_y))
11796 ++first_unchanged_at_end_row;
11797
11798 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11799 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11800 >= it.last_visible_y))
11801 first_unchanged_at_end_row = NULL;
11802 else
11803 {
11804 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
11805 + delta);
11806 first_unchanged_at_end_vpos
11807 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
11808 xassert (stop_pos >= Z - END_UNCHANGED);
11809 }
11810 }
11811 else if (last_unchanged_at_beg_row == NULL)
11812 GIVE_UP (19);
11813
11814
11815 #if GLYPH_DEBUG
11816
11817 /* Either there is no unchanged row at the end, or the one we have
11818 now displays text. This is a necessary condition for the window
11819 end pos calculation at the end of this function. */
11820 xassert (first_unchanged_at_end_row == NULL
11821 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
11822
11823 debug_last_unchanged_at_beg_vpos
11824 = (last_unchanged_at_beg_row
11825 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
11826 : -1);
11827 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
11828
11829 #endif /* GLYPH_DEBUG != 0 */
11830
11831
11832 /* Display new lines. Set last_text_row to the last new line
11833 displayed which has text on it, i.e. might end up as being the
11834 line where the window_end_vpos is. */
11835 w->cursor.vpos = -1;
11836 last_text_row = NULL;
11837 overlay_arrow_seen = 0;
11838 while (it.current_y < it.last_visible_y
11839 && !fonts_changed_p
11840 && (first_unchanged_at_end_row == NULL
11841 || IT_CHARPOS (it) < stop_pos))
11842 {
11843 if (display_line (&it))
11844 last_text_row = it.glyph_row - 1;
11845 }
11846
11847 if (fonts_changed_p)
11848 return -1;
11849
11850
11851 /* Compute differences in buffer positions, y-positions etc. for
11852 lines reused at the bottom of the window. Compute what we can
11853 scroll. */
11854 if (first_unchanged_at_end_row
11855 /* No lines reused because we displayed everything up to the
11856 bottom of the window. */
11857 && it.current_y < it.last_visible_y)
11858 {
11859 dvpos = (it.vpos
11860 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
11861 current_matrix));
11862 dy = it.current_y - first_unchanged_at_end_row->y;
11863 run.current_y = first_unchanged_at_end_row->y;
11864 run.desired_y = run.current_y + dy;
11865 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
11866 }
11867 else
11868 {
11869 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
11870 first_unchanged_at_end_row = NULL;
11871 }
11872 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
11873
11874
11875 /* Find the cursor if not already found. We have to decide whether
11876 PT will appear on this window (it sometimes doesn't, but this is
11877 not a very frequent case.) This decision has to be made before
11878 the current matrix is altered. A value of cursor.vpos < 0 means
11879 that PT is either in one of the lines beginning at
11880 first_unchanged_at_end_row or below the window. Don't care for
11881 lines that might be displayed later at the window end; as
11882 mentioned, this is not a frequent case. */
11883 if (w->cursor.vpos < 0)
11884 {
11885 /* Cursor in unchanged rows at the top? */
11886 if (PT < CHARPOS (start_pos)
11887 && last_unchanged_at_beg_row)
11888 {
11889 row = row_containing_pos (w, PT,
11890 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
11891 last_unchanged_at_beg_row + 1, 0);
11892 if (row)
11893 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11894 }
11895
11896 /* Start from first_unchanged_at_end_row looking for PT. */
11897 else if (first_unchanged_at_end_row)
11898 {
11899 row = row_containing_pos (w, PT - delta,
11900 first_unchanged_at_end_row, NULL, 0);
11901 if (row)
11902 set_cursor_from_row (w, row, w->current_matrix, delta,
11903 delta_bytes, dy, dvpos);
11904 }
11905
11906 /* Give up if cursor was not found. */
11907 if (w->cursor.vpos < 0)
11908 {
11909 clear_glyph_matrix (w->desired_matrix);
11910 return -1;
11911 }
11912 }
11913
11914 /* Don't let the cursor end in the scroll margins. */
11915 {
11916 int this_scroll_margin, cursor_height;
11917
11918 this_scroll_margin = max (0, scroll_margin);
11919 this_scroll_margin = min (this_scroll_margin,
11920 XFASTINT (w->height) / 4);
11921 this_scroll_margin *= CANON_Y_UNIT (it.f);
11922 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
11923
11924 if ((w->cursor.y < this_scroll_margin
11925 && CHARPOS (start) > BEGV)
11926 /* Don't take scroll margin into account at the bottom because
11927 old redisplay didn't do it either. */
11928 || w->cursor.y + cursor_height > it.last_visible_y)
11929 {
11930 w->cursor.vpos = -1;
11931 clear_glyph_matrix (w->desired_matrix);
11932 return -1;
11933 }
11934 }
11935
11936 /* Scroll the display. Do it before changing the current matrix so
11937 that xterm.c doesn't get confused about where the cursor glyph is
11938 found. */
11939 if (dy && run.height)
11940 {
11941 update_begin (f);
11942
11943 if (FRAME_WINDOW_P (f))
11944 {
11945 rif->update_window_begin_hook (w);
11946 rif->clear_mouse_face (w);
11947 rif->scroll_run_hook (w, &run);
11948 rif->update_window_end_hook (w, 0, 0);
11949 }
11950 else
11951 {
11952 /* Terminal frame. In this case, dvpos gives the number of
11953 lines to scroll by; dvpos < 0 means scroll up. */
11954 int first_unchanged_at_end_vpos
11955 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
11956 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
11957 int end = (XFASTINT (w->top)
11958 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
11959 + window_internal_height (w));
11960
11961 /* Perform the operation on the screen. */
11962 if (dvpos > 0)
11963 {
11964 /* Scroll last_unchanged_at_beg_row to the end of the
11965 window down dvpos lines. */
11966 set_terminal_window (end);
11967
11968 /* On dumb terminals delete dvpos lines at the end
11969 before inserting dvpos empty lines. */
11970 if (!scroll_region_ok)
11971 ins_del_lines (end - dvpos, -dvpos);
11972
11973 /* Insert dvpos empty lines in front of
11974 last_unchanged_at_beg_row. */
11975 ins_del_lines (from, dvpos);
11976 }
11977 else if (dvpos < 0)
11978 {
11979 /* Scroll up last_unchanged_at_beg_vpos to the end of
11980 the window to last_unchanged_at_beg_vpos - |dvpos|. */
11981 set_terminal_window (end);
11982
11983 /* Delete dvpos lines in front of
11984 last_unchanged_at_beg_vpos. ins_del_lines will set
11985 the cursor to the given vpos and emit |dvpos| delete
11986 line sequences. */
11987 ins_del_lines (from + dvpos, dvpos);
11988
11989 /* On a dumb terminal insert dvpos empty lines at the
11990 end. */
11991 if (!scroll_region_ok)
11992 ins_del_lines (end + dvpos, -dvpos);
11993 }
11994
11995 set_terminal_window (0);
11996 }
11997
11998 update_end (f);
11999 }
12000
12001 /* Shift reused rows of the current matrix to the right position.
12002 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
12003 text. */
12004 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
12005 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
12006 if (dvpos < 0)
12007 {
12008 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
12009 bottom_vpos, dvpos);
12010 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
12011 bottom_vpos, 0);
12012 }
12013 else if (dvpos > 0)
12014 {
12015 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
12016 bottom_vpos, dvpos);
12017 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
12018 first_unchanged_at_end_vpos + dvpos, 0);
12019 }
12020
12021 /* For frame-based redisplay, make sure that current frame and window
12022 matrix are in sync with respect to glyph memory. */
12023 if (!FRAME_WINDOW_P (f))
12024 sync_frame_with_window_matrix_rows (w);
12025
12026 /* Adjust buffer positions in reused rows. */
12027 if (delta)
12028 increment_matrix_positions (current_matrix,
12029 first_unchanged_at_end_vpos + dvpos,
12030 bottom_vpos, delta, delta_bytes);
12031
12032 /* Adjust Y positions. */
12033 if (dy)
12034 shift_glyph_matrix (w, current_matrix,
12035 first_unchanged_at_end_vpos + dvpos,
12036 bottom_vpos, dy);
12037
12038 if (first_unchanged_at_end_row)
12039 first_unchanged_at_end_row += dvpos;
12040
12041 /* If scrolling up, there may be some lines to display at the end of
12042 the window. */
12043 last_text_row_at_end = NULL;
12044 if (dy < 0)
12045 {
12046 /* Scrolling up can leave for example a partially visible line
12047 at the end of the window to be redisplayed. */
12048 /* Set last_row to the glyph row in the current matrix where the
12049 window end line is found. It has been moved up or down in
12050 the matrix by dvpos. */
12051 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
12052 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
12053
12054 /* If last_row is the window end line, it should display text. */
12055 xassert (last_row->displays_text_p);
12056
12057 /* If window end line was partially visible before, begin
12058 displaying at that line. Otherwise begin displaying with the
12059 line following it. */
12060 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
12061 {
12062 init_to_row_start (&it, w, last_row);
12063 it.vpos = last_vpos;
12064 it.current_y = last_row->y;
12065 }
12066 else
12067 {
12068 init_to_row_end (&it, w, last_row);
12069 it.vpos = 1 + last_vpos;
12070 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
12071 ++last_row;
12072 }
12073
12074 /* We may start in a continuation line. If so, we have to
12075 get the right continuation_lines_width and current_x. */
12076 it.continuation_lines_width = last_row->continuation_lines_width;
12077 it.hpos = it.current_x = 0;
12078
12079 /* Display the rest of the lines at the window end. */
12080 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
12081 while (it.current_y < it.last_visible_y
12082 && !fonts_changed_p)
12083 {
12084 /* Is it always sure that the display agrees with lines in
12085 the current matrix? I don't think so, so we mark rows
12086 displayed invalid in the current matrix by setting their
12087 enabled_p flag to zero. */
12088 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
12089 if (display_line (&it))
12090 last_text_row_at_end = it.glyph_row - 1;
12091 }
12092 }
12093
12094 /* Update window_end_pos and window_end_vpos. */
12095 if (first_unchanged_at_end_row
12096 && first_unchanged_at_end_row->y < it.last_visible_y
12097 && !last_text_row_at_end)
12098 {
12099 /* Window end line if one of the preserved rows from the current
12100 matrix. Set row to the last row displaying text in current
12101 matrix starting at first_unchanged_at_end_row, after
12102 scrolling. */
12103 xassert (first_unchanged_at_end_row->displays_text_p);
12104 row = find_last_row_displaying_text (w->current_matrix, &it,
12105 first_unchanged_at_end_row);
12106 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
12107
12108 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
12109 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
12110 w->window_end_vpos
12111 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
12112 xassert (w->window_end_bytepos >= 0);
12113 IF_DEBUG (debug_method_add (w, "A"));
12114 }
12115 else if (last_text_row_at_end)
12116 {
12117 w->window_end_pos
12118 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
12119 w->window_end_bytepos
12120 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
12121 w->window_end_vpos
12122 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
12123 xassert (w->window_end_bytepos >= 0);
12124 IF_DEBUG (debug_method_add (w, "B"));
12125 }
12126 else if (last_text_row)
12127 {
12128 /* We have displayed either to the end of the window or at the
12129 end of the window, i.e. the last row with text is to be found
12130 in the desired matrix. */
12131 w->window_end_pos
12132 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12133 w->window_end_bytepos
12134 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12135 w->window_end_vpos
12136 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
12137 xassert (w->window_end_bytepos >= 0);
12138 }
12139 else if (first_unchanged_at_end_row == NULL
12140 && last_text_row == NULL
12141 && last_text_row_at_end == NULL)
12142 {
12143 /* Displayed to end of window, but no line containing text was
12144 displayed. Lines were deleted at the end of the window. */
12145 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
12146 int vpos = XFASTINT (w->window_end_vpos);
12147 struct glyph_row *current_row = current_matrix->rows + vpos;
12148 struct glyph_row *desired_row = desired_matrix->rows + vpos;
12149
12150 for (row = NULL;
12151 row == NULL && vpos >= first_vpos;
12152 --vpos, --current_row, --desired_row)
12153 {
12154 if (desired_row->enabled_p)
12155 {
12156 if (desired_row->displays_text_p)
12157 row = desired_row;
12158 }
12159 else if (current_row->displays_text_p)
12160 row = current_row;
12161 }
12162
12163 xassert (row != NULL);
12164 w->window_end_vpos = make_number (vpos + 1);
12165 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
12166 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
12167 xassert (w->window_end_bytepos >= 0);
12168 IF_DEBUG (debug_method_add (w, "C"));
12169 }
12170 else
12171 abort ();
12172
12173 #if 0 /* This leads to problems, for instance when the cursor is
12174 at ZV, and the cursor line displays no text. */
12175 /* Disable rows below what's displayed in the window. This makes
12176 debugging easier. */
12177 enable_glyph_matrix_rows (current_matrix,
12178 XFASTINT (w->window_end_vpos) + 1,
12179 bottom_vpos, 0);
12180 #endif
12181
12182 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
12183 debug_end_vpos = XFASTINT (w->window_end_vpos));
12184
12185 /* Record that display has not been completed. */
12186 w->window_end_valid = Qnil;
12187 w->desired_matrix->no_scrolling_p = 1;
12188 return 3;
12189
12190 #undef GIVE_UP
12191 }
12192
12193
12194 \f
12195 /***********************************************************************
12196 More debugging support
12197 ***********************************************************************/
12198
12199 #if GLYPH_DEBUG
12200
12201 void dump_glyph_row P_ ((struct glyph_row *, int, int));
12202 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
12203 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
12204
12205
12206 /* Dump the contents of glyph matrix MATRIX on stderr.
12207
12208 GLYPHS 0 means don't show glyph contents.
12209 GLYPHS 1 means show glyphs in short form
12210 GLYPHS > 1 means show glyphs in long form. */
12211
12212 void
12213 dump_glyph_matrix (matrix, glyphs)
12214 struct glyph_matrix *matrix;
12215 int glyphs;
12216 {
12217 int i;
12218 for (i = 0; i < matrix->nrows; ++i)
12219 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
12220 }
12221
12222
12223 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
12224 the glyph row and area where the glyph comes from. */
12225
12226 void
12227 dump_glyph (row, glyph, area)
12228 struct glyph_row *row;
12229 struct glyph *glyph;
12230 int area;
12231 {
12232 if (glyph->type == CHAR_GLYPH)
12233 {
12234 fprintf (stderr,
12235 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12236 glyph - row->glyphs[TEXT_AREA],
12237 'C',
12238 glyph->charpos,
12239 (BUFFERP (glyph->object)
12240 ? 'B'
12241 : (STRINGP (glyph->object)
12242 ? 'S'
12243 : '-')),
12244 glyph->pixel_width,
12245 glyph->u.ch,
12246 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
12247 ? glyph->u.ch
12248 : '.'),
12249 glyph->face_id,
12250 glyph->left_box_line_p,
12251 glyph->right_box_line_p);
12252 }
12253 else if (glyph->type == STRETCH_GLYPH)
12254 {
12255 fprintf (stderr,
12256 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12257 glyph - row->glyphs[TEXT_AREA],
12258 'S',
12259 glyph->charpos,
12260 (BUFFERP (glyph->object)
12261 ? 'B'
12262 : (STRINGP (glyph->object)
12263 ? 'S'
12264 : '-')),
12265 glyph->pixel_width,
12266 0,
12267 '.',
12268 glyph->face_id,
12269 glyph->left_box_line_p,
12270 glyph->right_box_line_p);
12271 }
12272 else if (glyph->type == IMAGE_GLYPH)
12273 {
12274 fprintf (stderr,
12275 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12276 glyph - row->glyphs[TEXT_AREA],
12277 'I',
12278 glyph->charpos,
12279 (BUFFERP (glyph->object)
12280 ? 'B'
12281 : (STRINGP (glyph->object)
12282 ? 'S'
12283 : '-')),
12284 glyph->pixel_width,
12285 glyph->u.img_id,
12286 '.',
12287 glyph->face_id,
12288 glyph->left_box_line_p,
12289 glyph->right_box_line_p);
12290 }
12291 }
12292
12293
12294 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
12295 GLYPHS 0 means don't show glyph contents.
12296 GLYPHS 1 means show glyphs in short form
12297 GLYPHS > 1 means show glyphs in long form. */
12298
12299 void
12300 dump_glyph_row (row, vpos, glyphs)
12301 struct glyph_row *row;
12302 int vpos, glyphs;
12303 {
12304 if (glyphs != 1)
12305 {
12306 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
12307 fprintf (stderr, "=======================================================================\n");
12308
12309 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d\
12310 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
12311 vpos,
12312 MATRIX_ROW_START_CHARPOS (row),
12313 MATRIX_ROW_END_CHARPOS (row),
12314 row->used[TEXT_AREA],
12315 row->contains_overlapping_glyphs_p,
12316 row->enabled_p,
12317 row->truncated_on_left_p,
12318 row->truncated_on_right_p,
12319 row->overlay_arrow_p,
12320 row->continued_p,
12321 MATRIX_ROW_CONTINUATION_LINE_P (row),
12322 row->displays_text_p,
12323 row->ends_at_zv_p,
12324 row->fill_line_p,
12325 row->ends_in_middle_of_char_p,
12326 row->starts_in_middle_of_char_p,
12327 row->mouse_face_p,
12328 row->x,
12329 row->y,
12330 row->pixel_width,
12331 row->height,
12332 row->visible_height,
12333 row->ascent,
12334 row->phys_ascent);
12335 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
12336 row->end.overlay_string_index,
12337 row->continuation_lines_width);
12338 fprintf (stderr, "%9d %5d\n",
12339 CHARPOS (row->start.string_pos),
12340 CHARPOS (row->end.string_pos));
12341 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
12342 row->end.dpvec_index);
12343 }
12344
12345 if (glyphs > 1)
12346 {
12347 int area;
12348
12349 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12350 {
12351 struct glyph *glyph = row->glyphs[area];
12352 struct glyph *glyph_end = glyph + row->used[area];
12353
12354 /* Glyph for a line end in text. */
12355 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
12356 ++glyph_end;
12357
12358 if (glyph < glyph_end)
12359 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
12360
12361 for (; glyph < glyph_end; ++glyph)
12362 dump_glyph (row, glyph, area);
12363 }
12364 }
12365 else if (glyphs == 1)
12366 {
12367 int area;
12368
12369 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12370 {
12371 char *s = (char *) alloca (row->used[area] + 1);
12372 int i;
12373
12374 for (i = 0; i < row->used[area]; ++i)
12375 {
12376 struct glyph *glyph = row->glyphs[area] + i;
12377 if (glyph->type == CHAR_GLYPH
12378 && glyph->u.ch < 0x80
12379 && glyph->u.ch >= ' ')
12380 s[i] = glyph->u.ch;
12381 else
12382 s[i] = '.';
12383 }
12384
12385 s[i] = '\0';
12386 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
12387 }
12388 }
12389 }
12390
12391
12392 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
12393 Sdump_glyph_matrix, 0, 1, "p",
12394 doc: /* Dump the current matrix of the selected window to stderr.
12395 Shows contents of glyph row structures. With non-nil
12396 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
12397 glyphs in short form, otherwise show glyphs in long form. */)
12398 (glyphs)
12399 Lisp_Object glyphs;
12400 {
12401 struct window *w = XWINDOW (selected_window);
12402 struct buffer *buffer = XBUFFER (w->buffer);
12403
12404 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
12405 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
12406 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
12407 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
12408 fprintf (stderr, "=============================================\n");
12409 dump_glyph_matrix (w->current_matrix,
12410 NILP (glyphs) ? 0 : XINT (glyphs));
12411 return Qnil;
12412 }
12413
12414
12415 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
12416 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
12417 ()
12418 {
12419 struct frame *f = XFRAME (selected_frame);
12420 dump_glyph_matrix (f->current_matrix, 1);
12421 return Qnil;
12422 }
12423
12424
12425 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
12426 doc: /* Dump glyph row ROW to stderr.
12427 GLYPH 0 means don't dump glyphs.
12428 GLYPH 1 means dump glyphs in short form.
12429 GLYPH > 1 or omitted means dump glyphs in long form. */)
12430 (row, glyphs)
12431 Lisp_Object row, glyphs;
12432 {
12433 struct glyph_matrix *matrix;
12434 int vpos;
12435
12436 CHECK_NUMBER (row);
12437 matrix = XWINDOW (selected_window)->current_matrix;
12438 vpos = XINT (row);
12439 if (vpos >= 0 && vpos < matrix->nrows)
12440 dump_glyph_row (MATRIX_ROW (matrix, vpos),
12441 vpos,
12442 INTEGERP (glyphs) ? XINT (glyphs) : 2);
12443 return Qnil;
12444 }
12445
12446
12447 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
12448 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
12449 GLYPH 0 means don't dump glyphs.
12450 GLYPH 1 means dump glyphs in short form.
12451 GLYPH > 1 or omitted means dump glyphs in long form. */)
12452 (row, glyphs)
12453 Lisp_Object row, glyphs;
12454 {
12455 struct frame *sf = SELECTED_FRAME ();
12456 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
12457 int vpos;
12458
12459 CHECK_NUMBER (row);
12460 vpos = XINT (row);
12461 if (vpos >= 0 && vpos < m->nrows)
12462 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
12463 INTEGERP (glyphs) ? XINT (glyphs) : 2);
12464 return Qnil;
12465 }
12466
12467
12468 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
12469 doc: /* Toggle tracing of redisplay.
12470 With ARG, turn tracing on if and only if ARG is positive. */)
12471 (arg)
12472 Lisp_Object arg;
12473 {
12474 if (NILP (arg))
12475 trace_redisplay_p = !trace_redisplay_p;
12476 else
12477 {
12478 arg = Fprefix_numeric_value (arg);
12479 trace_redisplay_p = XINT (arg) > 0;
12480 }
12481
12482 return Qnil;
12483 }
12484
12485
12486 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
12487 doc: /* Like `format', but print result to stderr.
12488 usage: (trace-to-stderr STRING &rest OBJECTS) */)
12489 (nargs, args)
12490 int nargs;
12491 Lisp_Object *args;
12492 {
12493 Lisp_Object s = Fformat (nargs, args);
12494 fprintf (stderr, "%s", SDATA (s));
12495 return Qnil;
12496 }
12497
12498 #endif /* GLYPH_DEBUG */
12499
12500
12501 \f
12502 /***********************************************************************
12503 Building Desired Matrix Rows
12504 ***********************************************************************/
12505
12506 /* Return a temporary glyph row holding the glyphs of an overlay
12507 arrow. Only used for non-window-redisplay windows. */
12508
12509 static struct glyph_row *
12510 get_overlay_arrow_glyph_row (w)
12511 struct window *w;
12512 {
12513 struct frame *f = XFRAME (WINDOW_FRAME (w));
12514 struct buffer *buffer = XBUFFER (w->buffer);
12515 struct buffer *old = current_buffer;
12516 const unsigned char *arrow_string = SDATA (Voverlay_arrow_string);
12517 int arrow_len = SCHARS (Voverlay_arrow_string);
12518 const unsigned char *arrow_end = arrow_string + arrow_len;
12519 const unsigned char *p;
12520 struct it it;
12521 int multibyte_p;
12522 int n_glyphs_before;
12523
12524 set_buffer_temp (buffer);
12525 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
12526 it.glyph_row->used[TEXT_AREA] = 0;
12527 SET_TEXT_POS (it.position, 0, 0);
12528
12529 multibyte_p = !NILP (buffer->enable_multibyte_characters);
12530 p = arrow_string;
12531 while (p < arrow_end)
12532 {
12533 Lisp_Object face, ilisp;
12534
12535 /* Get the next character. */
12536 if (multibyte_p)
12537 it.c = string_char_and_length (p, arrow_len, &it.len);
12538 else
12539 it.c = *p, it.len = 1;
12540 p += it.len;
12541
12542 /* Get its face. */
12543 ilisp = make_number (p - arrow_string);
12544 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
12545 it.face_id = compute_char_face (f, it.c, face);
12546
12547 /* Compute its width, get its glyphs. */
12548 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
12549 SET_TEXT_POS (it.position, -1, -1);
12550 PRODUCE_GLYPHS (&it);
12551
12552 /* If this character doesn't fit any more in the line, we have
12553 to remove some glyphs. */
12554 if (it.current_x > it.last_visible_x)
12555 {
12556 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
12557 break;
12558 }
12559 }
12560
12561 set_buffer_temp (old);
12562 return it.glyph_row;
12563 }
12564
12565
12566 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
12567 glyphs are only inserted for terminal frames since we can't really
12568 win with truncation glyphs when partially visible glyphs are
12569 involved. Which glyphs to insert is determined by
12570 produce_special_glyphs. */
12571
12572 static void
12573 insert_left_trunc_glyphs (it)
12574 struct it *it;
12575 {
12576 struct it truncate_it;
12577 struct glyph *from, *end, *to, *toend;
12578
12579 xassert (!FRAME_WINDOW_P (it->f));
12580
12581 /* Get the truncation glyphs. */
12582 truncate_it = *it;
12583 truncate_it.current_x = 0;
12584 truncate_it.face_id = DEFAULT_FACE_ID;
12585 truncate_it.glyph_row = &scratch_glyph_row;
12586 truncate_it.glyph_row->used[TEXT_AREA] = 0;
12587 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
12588 truncate_it.object = make_number (0);
12589 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
12590
12591 /* Overwrite glyphs from IT with truncation glyphs. */
12592 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
12593 end = from + truncate_it.glyph_row->used[TEXT_AREA];
12594 to = it->glyph_row->glyphs[TEXT_AREA];
12595 toend = to + it->glyph_row->used[TEXT_AREA];
12596
12597 while (from < end)
12598 *to++ = *from++;
12599
12600 /* There may be padding glyphs left over. Overwrite them too. */
12601 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
12602 {
12603 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
12604 while (from < end)
12605 *to++ = *from++;
12606 }
12607
12608 if (to > toend)
12609 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
12610 }
12611
12612
12613 /* Compute the pixel height and width of IT->glyph_row.
12614
12615 Most of the time, ascent and height of a display line will be equal
12616 to the max_ascent and max_height values of the display iterator
12617 structure. This is not the case if
12618
12619 1. We hit ZV without displaying anything. In this case, max_ascent
12620 and max_height will be zero.
12621
12622 2. We have some glyphs that don't contribute to the line height.
12623 (The glyph row flag contributes_to_line_height_p is for future
12624 pixmap extensions).
12625
12626 The first case is easily covered by using default values because in
12627 these cases, the line height does not really matter, except that it
12628 must not be zero. */
12629
12630 static void
12631 compute_line_metrics (it)
12632 struct it *it;
12633 {
12634 struct glyph_row *row = it->glyph_row;
12635 int area, i;
12636
12637 if (FRAME_WINDOW_P (it->f))
12638 {
12639 int i, min_y, max_y;
12640
12641 /* The line may consist of one space only, that was added to
12642 place the cursor on it. If so, the row's height hasn't been
12643 computed yet. */
12644 if (row->height == 0)
12645 {
12646 if (it->max_ascent + it->max_descent == 0)
12647 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
12648 row->ascent = it->max_ascent;
12649 row->height = it->max_ascent + it->max_descent;
12650 row->phys_ascent = it->max_phys_ascent;
12651 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
12652 }
12653
12654 /* Compute the width of this line. */
12655 row->pixel_width = row->x;
12656 for (i = 0; i < row->used[TEXT_AREA]; ++i)
12657 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
12658
12659 xassert (row->pixel_width >= 0);
12660 xassert (row->ascent >= 0 && row->height > 0);
12661
12662 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
12663 || MATRIX_ROW_OVERLAPS_PRED_P (row));
12664
12665 /* If first line's physical ascent is larger than its logical
12666 ascent, use the physical ascent, and make the row taller.
12667 This makes accented characters fully visible. */
12668 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
12669 && row->phys_ascent > row->ascent)
12670 {
12671 row->height += row->phys_ascent - row->ascent;
12672 row->ascent = row->phys_ascent;
12673 }
12674
12675 /* Compute how much of the line is visible. */
12676 row->visible_height = row->height;
12677
12678 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (it->w);
12679 max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
12680
12681 if (row->y < min_y)
12682 row->visible_height -= min_y - row->y;
12683 if (row->y + row->height > max_y)
12684 row->visible_height -= row->y + row->height - max_y;
12685 }
12686 else
12687 {
12688 row->pixel_width = row->used[TEXT_AREA];
12689 if (row->continued_p)
12690 row->pixel_width -= it->continuation_pixel_width;
12691 else if (row->truncated_on_right_p)
12692 row->pixel_width -= it->truncation_pixel_width;
12693 row->ascent = row->phys_ascent = 0;
12694 row->height = row->phys_height = row->visible_height = 1;
12695 }
12696
12697 /* Compute a hash code for this row. */
12698 row->hash = 0;
12699 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12700 for (i = 0; i < row->used[area]; ++i)
12701 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
12702 + row->glyphs[area][i].u.val
12703 + row->glyphs[area][i].face_id
12704 + row->glyphs[area][i].padding_p
12705 + (row->glyphs[area][i].type << 2));
12706
12707 it->max_ascent = it->max_descent = 0;
12708 it->max_phys_ascent = it->max_phys_descent = 0;
12709 }
12710
12711
12712 /* Append one space to the glyph row of iterator IT if doing a
12713 window-based redisplay. DEFAULT_FACE_P non-zero means let the
12714 space have the default face, otherwise let it have the same face as
12715 IT->face_id. Value is non-zero if a space was added.
12716
12717 This function is called to make sure that there is always one glyph
12718 at the end of a glyph row that the cursor can be set on under
12719 window-systems. (If there weren't such a glyph we would not know
12720 how wide and tall a box cursor should be displayed).
12721
12722 At the same time this space let's a nicely handle clearing to the
12723 end of the line if the row ends in italic text. */
12724
12725 static int
12726 append_space (it, default_face_p)
12727 struct it *it;
12728 int default_face_p;
12729 {
12730 if (FRAME_WINDOW_P (it->f))
12731 {
12732 int n = it->glyph_row->used[TEXT_AREA];
12733
12734 if (it->glyph_row->glyphs[TEXT_AREA] + n
12735 < it->glyph_row->glyphs[1 + TEXT_AREA])
12736 {
12737 /* Save some values that must not be changed.
12738 Must save IT->c and IT->len because otherwise
12739 ITERATOR_AT_END_P wouldn't work anymore after
12740 append_space has been called. */
12741 enum display_element_type saved_what = it->what;
12742 int saved_c = it->c, saved_len = it->len;
12743 int saved_x = it->current_x;
12744 int saved_face_id = it->face_id;
12745 struct text_pos saved_pos;
12746 Lisp_Object saved_object;
12747 struct face *face;
12748
12749 saved_object = it->object;
12750 saved_pos = it->position;
12751
12752 it->what = IT_CHARACTER;
12753 bzero (&it->position, sizeof it->position);
12754 it->object = make_number (0);
12755 it->c = ' ';
12756 it->len = 1;
12757
12758 if (default_face_p)
12759 it->face_id = DEFAULT_FACE_ID;
12760 else if (it->face_before_selective_p)
12761 it->face_id = it->saved_face_id;
12762 face = FACE_FROM_ID (it->f, it->face_id);
12763 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
12764
12765 PRODUCE_GLYPHS (it);
12766
12767 it->current_x = saved_x;
12768 it->object = saved_object;
12769 it->position = saved_pos;
12770 it->what = saved_what;
12771 it->face_id = saved_face_id;
12772 it->len = saved_len;
12773 it->c = saved_c;
12774 return 1;
12775 }
12776 }
12777
12778 return 0;
12779 }
12780
12781
12782 /* Extend the face of the last glyph in the text area of IT->glyph_row
12783 to the end of the display line. Called from display_line.
12784 If the glyph row is empty, add a space glyph to it so that we
12785 know the face to draw. Set the glyph row flag fill_line_p. */
12786
12787 static void
12788 extend_face_to_end_of_line (it)
12789 struct it *it;
12790 {
12791 struct face *face;
12792 struct frame *f = it->f;
12793
12794 /* If line is already filled, do nothing. */
12795 if (it->current_x >= it->last_visible_x)
12796 return;
12797
12798 /* Face extension extends the background and box of IT->face_id
12799 to the end of the line. If the background equals the background
12800 of the frame, we don't have to do anything. */
12801 if (it->face_before_selective_p)
12802 face = FACE_FROM_ID (it->f, it->saved_face_id);
12803 else
12804 face = FACE_FROM_ID (f, it->face_id);
12805
12806 if (FRAME_WINDOW_P (f)
12807 && face->box == FACE_NO_BOX
12808 && face->background == FRAME_BACKGROUND_PIXEL (f)
12809 && !face->stipple)
12810 return;
12811
12812 /* Set the glyph row flag indicating that the face of the last glyph
12813 in the text area has to be drawn to the end of the text area. */
12814 it->glyph_row->fill_line_p = 1;
12815
12816 /* If current character of IT is not ASCII, make sure we have the
12817 ASCII face. This will be automatically undone the next time
12818 get_next_display_element returns a multibyte character. Note
12819 that the character will always be single byte in unibyte text. */
12820 if (!SINGLE_BYTE_CHAR_P (it->c))
12821 {
12822 it->face_id = FACE_FOR_CHAR (f, face, 0);
12823 }
12824
12825 if (FRAME_WINDOW_P (f))
12826 {
12827 /* If the row is empty, add a space with the current face of IT,
12828 so that we know which face to draw. */
12829 if (it->glyph_row->used[TEXT_AREA] == 0)
12830 {
12831 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
12832 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
12833 it->glyph_row->used[TEXT_AREA] = 1;
12834 }
12835 }
12836 else
12837 {
12838 /* Save some values that must not be changed. */
12839 int saved_x = it->current_x;
12840 struct text_pos saved_pos;
12841 Lisp_Object saved_object;
12842 enum display_element_type saved_what = it->what;
12843 int saved_face_id = it->face_id;
12844
12845 saved_object = it->object;
12846 saved_pos = it->position;
12847
12848 it->what = IT_CHARACTER;
12849 bzero (&it->position, sizeof it->position);
12850 it->object = make_number (0);
12851 it->c = ' ';
12852 it->len = 1;
12853 it->face_id = face->id;
12854
12855 PRODUCE_GLYPHS (it);
12856
12857 while (it->current_x <= it->last_visible_x)
12858 PRODUCE_GLYPHS (it);
12859
12860 /* Don't count these blanks really. It would let us insert a left
12861 truncation glyph below and make us set the cursor on them, maybe. */
12862 it->current_x = saved_x;
12863 it->object = saved_object;
12864 it->position = saved_pos;
12865 it->what = saved_what;
12866 it->face_id = saved_face_id;
12867 }
12868 }
12869
12870
12871 /* Value is non-zero if text starting at CHARPOS in current_buffer is
12872 trailing whitespace. */
12873
12874 static int
12875 trailing_whitespace_p (charpos)
12876 int charpos;
12877 {
12878 int bytepos = CHAR_TO_BYTE (charpos);
12879 int c = 0;
12880
12881 while (bytepos < ZV_BYTE
12882 && (c = FETCH_CHAR (bytepos),
12883 c == ' ' || c == '\t'))
12884 ++bytepos;
12885
12886 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
12887 {
12888 if (bytepos != PT_BYTE)
12889 return 1;
12890 }
12891 return 0;
12892 }
12893
12894
12895 /* Highlight trailing whitespace, if any, in ROW. */
12896
12897 void
12898 highlight_trailing_whitespace (f, row)
12899 struct frame *f;
12900 struct glyph_row *row;
12901 {
12902 int used = row->used[TEXT_AREA];
12903
12904 if (used)
12905 {
12906 struct glyph *start = row->glyphs[TEXT_AREA];
12907 struct glyph *glyph = start + used - 1;
12908
12909 /* Skip over glyphs inserted to display the cursor at the
12910 end of a line, for extending the face of the last glyph
12911 to the end of the line on terminals, and for truncation
12912 and continuation glyphs. */
12913 while (glyph >= start
12914 && glyph->type == CHAR_GLYPH
12915 && INTEGERP (glyph->object))
12916 --glyph;
12917
12918 /* If last glyph is a space or stretch, and it's trailing
12919 whitespace, set the face of all trailing whitespace glyphs in
12920 IT->glyph_row to `trailing-whitespace'. */
12921 if (glyph >= start
12922 && BUFFERP (glyph->object)
12923 && (glyph->type == STRETCH_GLYPH
12924 || (glyph->type == CHAR_GLYPH
12925 && glyph->u.ch == ' '))
12926 && trailing_whitespace_p (glyph->charpos))
12927 {
12928 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
12929
12930 while (glyph >= start
12931 && BUFFERP (glyph->object)
12932 && (glyph->type == STRETCH_GLYPH
12933 || (glyph->type == CHAR_GLYPH
12934 && glyph->u.ch == ' ')))
12935 (glyph--)->face_id = face_id;
12936 }
12937 }
12938 }
12939
12940
12941 /* Value is non-zero if glyph row ROW in window W should be
12942 used to hold the cursor. */
12943
12944 static int
12945 cursor_row_p (w, row)
12946 struct window *w;
12947 struct glyph_row *row;
12948 {
12949 int cursor_row_p = 1;
12950
12951 if (PT == MATRIX_ROW_END_CHARPOS (row))
12952 {
12953 /* If the row ends with a newline from a string, we don't want
12954 the cursor there (if the row is continued it doesn't end in a
12955 newline). */
12956 if (CHARPOS (row->end.string_pos) >= 0
12957 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12958 cursor_row_p = row->continued_p;
12959
12960 /* If the row ends at ZV, display the cursor at the end of that
12961 row instead of at the start of the row below. */
12962 else if (row->ends_at_zv_p)
12963 cursor_row_p = 1;
12964 else
12965 cursor_row_p = 0;
12966 }
12967
12968 return cursor_row_p;
12969 }
12970
12971
12972 /* Construct the glyph row IT->glyph_row in the desired matrix of
12973 IT->w from text at the current position of IT. See dispextern.h
12974 for an overview of struct it. Value is non-zero if
12975 IT->glyph_row displays text, as opposed to a line displaying ZV
12976 only. */
12977
12978 static int
12979 display_line (it)
12980 struct it *it;
12981 {
12982 struct glyph_row *row = it->glyph_row;
12983
12984 /* We always start displaying at hpos zero even if hscrolled. */
12985 xassert (it->hpos == 0 && it->current_x == 0);
12986
12987 /* We must not display in a row that's not a text row. */
12988 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
12989 < it->w->desired_matrix->nrows);
12990
12991 /* Is IT->w showing the region? */
12992 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
12993
12994 /* Clear the result glyph row and enable it. */
12995 prepare_desired_row (row);
12996
12997 row->y = it->current_y;
12998 row->start = it->current;
12999 row->continuation_lines_width = it->continuation_lines_width;
13000 row->displays_text_p = 1;
13001 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
13002 it->starts_in_middle_of_char_p = 0;
13003
13004 /* Arrange the overlays nicely for our purposes. Usually, we call
13005 display_line on only one line at a time, in which case this
13006 can't really hurt too much, or we call it on lines which appear
13007 one after another in the buffer, in which case all calls to
13008 recenter_overlay_lists but the first will be pretty cheap. */
13009 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
13010
13011 /* Move over display elements that are not visible because we are
13012 hscrolled. This may stop at an x-position < IT->first_visible_x
13013 if the first glyph is partially visible or if we hit a line end. */
13014 if (it->current_x < it->first_visible_x)
13015 move_it_in_display_line_to (it, ZV, it->first_visible_x,
13016 MOVE_TO_POS | MOVE_TO_X);
13017
13018 /* Get the initial row height. This is either the height of the
13019 text hscrolled, if there is any, or zero. */
13020 row->ascent = it->max_ascent;
13021 row->height = it->max_ascent + it->max_descent;
13022 row->phys_ascent = it->max_phys_ascent;
13023 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
13024
13025 /* Loop generating characters. The loop is left with IT on the next
13026 character to display. */
13027 while (1)
13028 {
13029 int n_glyphs_before, hpos_before, x_before;
13030 int x, i, nglyphs;
13031 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
13032
13033 /* Retrieve the next thing to display. Value is zero if end of
13034 buffer reached. */
13035 if (!get_next_display_element (it))
13036 {
13037 /* Maybe add a space at the end of this line that is used to
13038 display the cursor there under X. Set the charpos of the
13039 first glyph of blank lines not corresponding to any text
13040 to -1. */
13041 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
13042 || row->used[TEXT_AREA] == 0)
13043 {
13044 row->glyphs[TEXT_AREA]->charpos = -1;
13045 row->displays_text_p = 0;
13046
13047 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
13048 && (!MINI_WINDOW_P (it->w)
13049 || (minibuf_level && EQ (it->window, minibuf_window))))
13050 row->indicate_empty_line_p = 1;
13051 }
13052
13053 it->continuation_lines_width = 0;
13054 row->ends_at_zv_p = 1;
13055 break;
13056 }
13057
13058 /* Now, get the metrics of what we want to display. This also
13059 generates glyphs in `row' (which is IT->glyph_row). */
13060 n_glyphs_before = row->used[TEXT_AREA];
13061 x = it->current_x;
13062
13063 /* Remember the line height so far in case the next element doesn't
13064 fit on the line. */
13065 if (!it->truncate_lines_p)
13066 {
13067 ascent = it->max_ascent;
13068 descent = it->max_descent;
13069 phys_ascent = it->max_phys_ascent;
13070 phys_descent = it->max_phys_descent;
13071 }
13072
13073 PRODUCE_GLYPHS (it);
13074
13075 /* If this display element was in marginal areas, continue with
13076 the next one. */
13077 if (it->area != TEXT_AREA)
13078 {
13079 row->ascent = max (row->ascent, it->max_ascent);
13080 row->height = max (row->height, it->max_ascent + it->max_descent);
13081 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13082 row->phys_height = max (row->phys_height,
13083 it->max_phys_ascent + it->max_phys_descent);
13084 set_iterator_to_next (it, 1);
13085 continue;
13086 }
13087
13088 /* Does the display element fit on the line? If we truncate
13089 lines, we should draw past the right edge of the window. If
13090 we don't truncate, we want to stop so that we can display the
13091 continuation glyph before the right margin. If lines are
13092 continued, there are two possible strategies for characters
13093 resulting in more than 1 glyph (e.g. tabs): Display as many
13094 glyphs as possible in this line and leave the rest for the
13095 continuation line, or display the whole element in the next
13096 line. Original redisplay did the former, so we do it also. */
13097 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
13098 hpos_before = it->hpos;
13099 x_before = x;
13100
13101 if (/* Not a newline. */
13102 nglyphs > 0
13103 /* Glyphs produced fit entirely in the line. */
13104 && it->current_x < it->last_visible_x)
13105 {
13106 it->hpos += nglyphs;
13107 row->ascent = max (row->ascent, it->max_ascent);
13108 row->height = max (row->height, it->max_ascent + it->max_descent);
13109 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13110 row->phys_height = max (row->phys_height,
13111 it->max_phys_ascent + it->max_phys_descent);
13112 if (it->current_x - it->pixel_width < it->first_visible_x)
13113 row->x = x - it->first_visible_x;
13114 }
13115 else
13116 {
13117 int new_x;
13118 struct glyph *glyph;
13119
13120 for (i = 0; i < nglyphs; ++i, x = new_x)
13121 {
13122 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
13123 new_x = x + glyph->pixel_width;
13124
13125 if (/* Lines are continued. */
13126 !it->truncate_lines_p
13127 && (/* Glyph doesn't fit on the line. */
13128 new_x > it->last_visible_x
13129 /* Or it fits exactly on a window system frame. */
13130 || (new_x == it->last_visible_x
13131 && FRAME_WINDOW_P (it->f))))
13132 {
13133 /* End of a continued line. */
13134
13135 if (it->hpos == 0
13136 || (new_x == it->last_visible_x
13137 && FRAME_WINDOW_P (it->f)))
13138 {
13139 /* Current glyph is the only one on the line or
13140 fits exactly on the line. We must continue
13141 the line because we can't draw the cursor
13142 after the glyph. */
13143 row->continued_p = 1;
13144 it->current_x = new_x;
13145 it->continuation_lines_width += new_x;
13146 ++it->hpos;
13147 if (i == nglyphs - 1)
13148 set_iterator_to_next (it, 1);
13149 }
13150 else if (CHAR_GLYPH_PADDING_P (*glyph)
13151 && !FRAME_WINDOW_P (it->f))
13152 {
13153 /* A padding glyph that doesn't fit on this line.
13154 This means the whole character doesn't fit
13155 on the line. */
13156 row->used[TEXT_AREA] = n_glyphs_before;
13157
13158 /* Fill the rest of the row with continuation
13159 glyphs like in 20.x. */
13160 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
13161 < row->glyphs[1 + TEXT_AREA])
13162 produce_special_glyphs (it, IT_CONTINUATION);
13163
13164 row->continued_p = 1;
13165 it->current_x = x_before;
13166 it->continuation_lines_width += x_before;
13167
13168 /* Restore the height to what it was before the
13169 element not fitting on the line. */
13170 it->max_ascent = ascent;
13171 it->max_descent = descent;
13172 it->max_phys_ascent = phys_ascent;
13173 it->max_phys_descent = phys_descent;
13174 }
13175 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
13176 {
13177 /* A TAB that extends past the right edge of the
13178 window. This produces a single glyph on
13179 window system frames. We leave the glyph in
13180 this row and let it fill the row, but don't
13181 consume the TAB. */
13182 it->continuation_lines_width += it->last_visible_x;
13183 row->ends_in_middle_of_char_p = 1;
13184 row->continued_p = 1;
13185 glyph->pixel_width = it->last_visible_x - x;
13186 it->starts_in_middle_of_char_p = 1;
13187 }
13188 else
13189 {
13190 /* Something other than a TAB that draws past
13191 the right edge of the window. Restore
13192 positions to values before the element. */
13193 row->used[TEXT_AREA] = n_glyphs_before + i;
13194
13195 /* Display continuation glyphs. */
13196 if (!FRAME_WINDOW_P (it->f))
13197 produce_special_glyphs (it, IT_CONTINUATION);
13198 row->continued_p = 1;
13199
13200 it->continuation_lines_width += x;
13201
13202 if (nglyphs > 1 && i > 0)
13203 {
13204 row->ends_in_middle_of_char_p = 1;
13205 it->starts_in_middle_of_char_p = 1;
13206 }
13207
13208 /* Restore the height to what it was before the
13209 element not fitting on the line. */
13210 it->max_ascent = ascent;
13211 it->max_descent = descent;
13212 it->max_phys_ascent = phys_ascent;
13213 it->max_phys_descent = phys_descent;
13214 }
13215
13216 break;
13217 }
13218 else if (new_x > it->first_visible_x)
13219 {
13220 /* Increment number of glyphs actually displayed. */
13221 ++it->hpos;
13222
13223 if (x < it->first_visible_x)
13224 /* Glyph is partially visible, i.e. row starts at
13225 negative X position. */
13226 row->x = x - it->first_visible_x;
13227 }
13228 else
13229 {
13230 /* Glyph is completely off the left margin of the
13231 window. This should not happen because of the
13232 move_it_in_display_line at the start of
13233 this function. */
13234 abort ();
13235 }
13236 }
13237
13238 row->ascent = max (row->ascent, it->max_ascent);
13239 row->height = max (row->height, it->max_ascent + it->max_descent);
13240 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13241 row->phys_height = max (row->phys_height,
13242 it->max_phys_ascent + it->max_phys_descent);
13243
13244 /* End of this display line if row is continued. */
13245 if (row->continued_p)
13246 break;
13247 }
13248
13249 /* Is this a line end? If yes, we're also done, after making
13250 sure that a non-default face is extended up to the right
13251 margin of the window. */
13252 if (ITERATOR_AT_END_OF_LINE_P (it))
13253 {
13254 int used_before = row->used[TEXT_AREA];
13255
13256 row->ends_in_newline_from_string_p = STRINGP (it->object);
13257
13258 /* Add a space at the end of the line that is used to
13259 display the cursor there. */
13260 append_space (it, 0);
13261
13262 /* Extend the face to the end of the line. */
13263 extend_face_to_end_of_line (it);
13264
13265 /* Make sure we have the position. */
13266 if (used_before == 0)
13267 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
13268
13269 /* Consume the line end. This skips over invisible lines. */
13270 set_iterator_to_next (it, 1);
13271 it->continuation_lines_width = 0;
13272 break;
13273 }
13274
13275 /* Proceed with next display element. Note that this skips
13276 over lines invisible because of selective display. */
13277 set_iterator_to_next (it, 1);
13278
13279 /* If we truncate lines, we are done when the last displayed
13280 glyphs reach past the right margin of the window. */
13281 if (it->truncate_lines_p
13282 && (FRAME_WINDOW_P (it->f)
13283 ? (it->current_x >= it->last_visible_x)
13284 : (it->current_x > it->last_visible_x)))
13285 {
13286 /* Maybe add truncation glyphs. */
13287 if (!FRAME_WINDOW_P (it->f))
13288 {
13289 int i, n;
13290
13291 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
13292 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
13293 break;
13294
13295 for (n = row->used[TEXT_AREA]; i < n; ++i)
13296 {
13297 row->used[TEXT_AREA] = i;
13298 produce_special_glyphs (it, IT_TRUNCATION);
13299 }
13300 }
13301
13302 row->truncated_on_right_p = 1;
13303 it->continuation_lines_width = 0;
13304 reseat_at_next_visible_line_start (it, 0);
13305 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
13306 it->hpos = hpos_before;
13307 it->current_x = x_before;
13308 break;
13309 }
13310 }
13311
13312 /* If line is not empty and hscrolled, maybe insert truncation glyphs
13313 at the left window margin. */
13314 if (it->first_visible_x
13315 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
13316 {
13317 if (!FRAME_WINDOW_P (it->f))
13318 insert_left_trunc_glyphs (it);
13319 row->truncated_on_left_p = 1;
13320 }
13321
13322 /* If the start of this line is the overlay arrow-position, then
13323 mark this glyph row as the one containing the overlay arrow.
13324 This is clearly a mess with variable size fonts. It would be
13325 better to let it be displayed like cursors under X. */
13326 if (MARKERP (Voverlay_arrow_position)
13327 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
13328 && (MATRIX_ROW_START_CHARPOS (row)
13329 == marker_position (Voverlay_arrow_position))
13330 && STRINGP (Voverlay_arrow_string)
13331 && ! overlay_arrow_seen)
13332 {
13333 /* Overlay arrow in window redisplay is a fringe bitmap. */
13334 if (!FRAME_WINDOW_P (it->f))
13335 {
13336 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
13337 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
13338 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
13339 struct glyph *p = row->glyphs[TEXT_AREA];
13340 struct glyph *p2, *end;
13341
13342 /* Copy the arrow glyphs. */
13343 while (glyph < arrow_end)
13344 *p++ = *glyph++;
13345
13346 /* Throw away padding glyphs. */
13347 p2 = p;
13348 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
13349 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
13350 ++p2;
13351 if (p2 > p)
13352 {
13353 while (p2 < end)
13354 *p++ = *p2++;
13355 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
13356 }
13357 }
13358
13359 overlay_arrow_seen = 1;
13360 row->overlay_arrow_p = 1;
13361 }
13362
13363 /* Compute pixel dimensions of this line. */
13364 compute_line_metrics (it);
13365
13366 /* Remember the position at which this line ends. */
13367 row->end = it->current;
13368
13369 /* Maybe set the cursor. */
13370 if (it->w->cursor.vpos < 0
13371 && PT >= MATRIX_ROW_START_CHARPOS (row)
13372 && PT <= MATRIX_ROW_END_CHARPOS (row)
13373 && cursor_row_p (it->w, row))
13374 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
13375
13376 /* Highlight trailing whitespace. */
13377 if (!NILP (Vshow_trailing_whitespace))
13378 highlight_trailing_whitespace (it->f, it->glyph_row);
13379
13380 /* Prepare for the next line. This line starts horizontally at (X
13381 HPOS) = (0 0). Vertical positions are incremented. As a
13382 convenience for the caller, IT->glyph_row is set to the next
13383 row to be used. */
13384 it->current_x = it->hpos = 0;
13385 it->current_y += row->height;
13386 ++it->vpos;
13387 ++it->glyph_row;
13388 return row->displays_text_p;
13389 }
13390
13391
13392 \f
13393 /***********************************************************************
13394 Menu Bar
13395 ***********************************************************************/
13396
13397 /* Redisplay the menu bar in the frame for window W.
13398
13399 The menu bar of X frames that don't have X toolkit support is
13400 displayed in a special window W->frame->menu_bar_window.
13401
13402 The menu bar of terminal frames is treated specially as far as
13403 glyph matrices are concerned. Menu bar lines are not part of
13404 windows, so the update is done directly on the frame matrix rows
13405 for the menu bar. */
13406
13407 static void
13408 display_menu_bar (w)
13409 struct window *w;
13410 {
13411 struct frame *f = XFRAME (WINDOW_FRAME (w));
13412 struct it it;
13413 Lisp_Object items;
13414 int i;
13415
13416 /* Don't do all this for graphical frames. */
13417 #ifdef HAVE_NTGUI
13418 if (!NILP (Vwindow_system))
13419 return;
13420 #endif
13421 #ifdef USE_X_TOOLKIT
13422 if (FRAME_X_P (f))
13423 return;
13424 #endif
13425 #ifdef MAC_OS
13426 if (FRAME_MAC_P (f))
13427 return;
13428 #endif
13429
13430 #ifdef USE_X_TOOLKIT
13431 xassert (!FRAME_WINDOW_P (f));
13432 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
13433 it.first_visible_x = 0;
13434 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
13435 #else /* not USE_X_TOOLKIT */
13436 if (FRAME_WINDOW_P (f))
13437 {
13438 /* Menu bar lines are displayed in the desired matrix of the
13439 dummy window menu_bar_window. */
13440 struct window *menu_w;
13441 xassert (WINDOWP (f->menu_bar_window));
13442 menu_w = XWINDOW (f->menu_bar_window);
13443 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
13444 MENU_FACE_ID);
13445 it.first_visible_x = 0;
13446 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
13447 }
13448 else
13449 {
13450 /* This is a TTY frame, i.e. character hpos/vpos are used as
13451 pixel x/y. */
13452 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
13453 MENU_FACE_ID);
13454 it.first_visible_x = 0;
13455 it.last_visible_x = FRAME_WIDTH (f);
13456 }
13457 #endif /* not USE_X_TOOLKIT */
13458
13459 if (! mode_line_inverse_video)
13460 /* Force the menu-bar to be displayed in the default face. */
13461 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
13462
13463 /* Clear all rows of the menu bar. */
13464 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
13465 {
13466 struct glyph_row *row = it.glyph_row + i;
13467 clear_glyph_row (row);
13468 row->enabled_p = 1;
13469 row->full_width_p = 1;
13470 }
13471
13472 /* Display all items of the menu bar. */
13473 items = FRAME_MENU_BAR_ITEMS (it.f);
13474 for (i = 0; i < XVECTOR (items)->size; i += 4)
13475 {
13476 Lisp_Object string;
13477
13478 /* Stop at nil string. */
13479 string = AREF (items, i + 1);
13480 if (NILP (string))
13481 break;
13482
13483 /* Remember where item was displayed. */
13484 AREF (items, i + 3) = make_number (it.hpos);
13485
13486 /* Display the item, pad with one space. */
13487 if (it.current_x < it.last_visible_x)
13488 display_string (NULL, string, Qnil, 0, 0, &it,
13489 SCHARS (string) + 1, 0, 0, -1);
13490 }
13491
13492 /* Fill out the line with spaces. */
13493 if (it.current_x < it.last_visible_x)
13494 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
13495
13496 /* Compute the total height of the lines. */
13497 compute_line_metrics (&it);
13498 }
13499
13500
13501 \f
13502 /***********************************************************************
13503 Mode Line
13504 ***********************************************************************/
13505
13506 /* Redisplay mode lines in the window tree whose root is WINDOW. If
13507 FORCE is non-zero, redisplay mode lines unconditionally.
13508 Otherwise, redisplay only mode lines that are garbaged. Value is
13509 the number of windows whose mode lines were redisplayed. */
13510
13511 static int
13512 redisplay_mode_lines (window, force)
13513 Lisp_Object window;
13514 int force;
13515 {
13516 int nwindows = 0;
13517
13518 while (!NILP (window))
13519 {
13520 struct window *w = XWINDOW (window);
13521
13522 if (WINDOWP (w->hchild))
13523 nwindows += redisplay_mode_lines (w->hchild, force);
13524 else if (WINDOWP (w->vchild))
13525 nwindows += redisplay_mode_lines (w->vchild, force);
13526 else if (force
13527 || FRAME_GARBAGED_P (XFRAME (w->frame))
13528 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
13529 {
13530 struct text_pos lpoint;
13531 struct buffer *old = current_buffer;
13532
13533 /* Set the window's buffer for the mode line display. */
13534 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13535 set_buffer_internal_1 (XBUFFER (w->buffer));
13536
13537 /* Point refers normally to the selected window. For any
13538 other window, set up appropriate value. */
13539 if (!EQ (window, selected_window))
13540 {
13541 struct text_pos pt;
13542
13543 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
13544 if (CHARPOS (pt) < BEGV)
13545 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
13546 else if (CHARPOS (pt) > (ZV - 1))
13547 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
13548 else
13549 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
13550 }
13551
13552 /* Display mode lines. */
13553 clear_glyph_matrix (w->desired_matrix);
13554 if (display_mode_lines (w))
13555 {
13556 ++nwindows;
13557 w->must_be_updated_p = 1;
13558 }
13559
13560 /* Restore old settings. */
13561 set_buffer_internal_1 (old);
13562 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13563 }
13564
13565 window = w->next;
13566 }
13567
13568 return nwindows;
13569 }
13570
13571
13572 /* Display the mode and/or top line of window W. Value is the number
13573 of mode lines displayed. */
13574
13575 static int
13576 display_mode_lines (w)
13577 struct window *w;
13578 {
13579 Lisp_Object old_selected_window, old_selected_frame;
13580 int n = 0;
13581
13582 old_selected_frame = selected_frame;
13583 selected_frame = w->frame;
13584 old_selected_window = selected_window;
13585 XSETWINDOW (selected_window, w);
13586
13587 /* These will be set while the mode line specs are processed. */
13588 line_number_displayed = 0;
13589 w->column_number_displayed = Qnil;
13590
13591 if (WINDOW_WANTS_MODELINE_P (w))
13592 {
13593 struct window *sel_w = XWINDOW (old_selected_window);
13594
13595 /* Select mode line face based on the real selected window. */
13596 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
13597 current_buffer->mode_line_format);
13598 ++n;
13599 }
13600
13601 if (WINDOW_WANTS_HEADER_LINE_P (w))
13602 {
13603 display_mode_line (w, HEADER_LINE_FACE_ID,
13604 current_buffer->header_line_format);
13605 ++n;
13606 }
13607
13608 selected_frame = old_selected_frame;
13609 selected_window = old_selected_window;
13610 return n;
13611 }
13612
13613
13614 /* Display mode or top line of window W. FACE_ID specifies which line
13615 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
13616 FORMAT is the mode line format to display. Value is the pixel
13617 height of the mode line displayed. */
13618
13619 static int
13620 display_mode_line (w, face_id, format)
13621 struct window *w;
13622 enum face_id face_id;
13623 Lisp_Object format;
13624 {
13625 struct it it;
13626 struct face *face;
13627
13628 init_iterator (&it, w, -1, -1, NULL, face_id);
13629 prepare_desired_row (it.glyph_row);
13630
13631 if (! mode_line_inverse_video)
13632 /* Force the mode-line to be displayed in the default face. */
13633 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
13634
13635 /* Temporarily make frame's keyboard the current kboard so that
13636 kboard-local variables in the mode_line_format will get the right
13637 values. */
13638 push_frame_kboard (it.f);
13639 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
13640 pop_frame_kboard ();
13641
13642 /* Fill up with spaces. */
13643 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
13644
13645 compute_line_metrics (&it);
13646 it.glyph_row->full_width_p = 1;
13647 it.glyph_row->mode_line_p = 1;
13648 it.glyph_row->continued_p = 0;
13649 it.glyph_row->truncated_on_left_p = 0;
13650 it.glyph_row->truncated_on_right_p = 0;
13651
13652 /* Make a 3D mode-line have a shadow at its right end. */
13653 face = FACE_FROM_ID (it.f, face_id);
13654 extend_face_to_end_of_line (&it);
13655 if (face->box != FACE_NO_BOX)
13656 {
13657 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
13658 + it.glyph_row->used[TEXT_AREA] - 1);
13659 last->right_box_line_p = 1;
13660 }
13661
13662 return it.glyph_row->height;
13663 }
13664
13665 /* Alist that caches the results of :propertize.
13666 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
13667 Lisp_Object mode_line_proptrans_alist;
13668
13669 /* List of strings making up the mode-line. */
13670 Lisp_Object mode_line_string_list;
13671
13672 /* Base face property when building propertized mode line string. */
13673 static Lisp_Object mode_line_string_face;
13674 static Lisp_Object mode_line_string_face_prop;
13675
13676
13677 /* Contribute ELT to the mode line for window IT->w. How it
13678 translates into text depends on its data type.
13679
13680 IT describes the display environment in which we display, as usual.
13681
13682 DEPTH is the depth in recursion. It is used to prevent
13683 infinite recursion here.
13684
13685 FIELD_WIDTH is the number of characters the display of ELT should
13686 occupy in the mode line, and PRECISION is the maximum number of
13687 characters to display from ELT's representation. See
13688 display_string for details.
13689
13690 Returns the hpos of the end of the text generated by ELT.
13691
13692 PROPS is a property list to add to any string we encounter.
13693
13694 If RISKY is nonzero, remove (disregard) any properties in any string
13695 we encounter, and ignore :eval and :propertize.
13696
13697 If the global variable `frame_title_ptr' is non-NULL, then the output
13698 is passed to `store_frame_title' instead of `display_string'. */
13699
13700 static int
13701 display_mode_element (it, depth, field_width, precision, elt, props, risky)
13702 struct it *it;
13703 int depth;
13704 int field_width, precision;
13705 Lisp_Object elt, props;
13706 int risky;
13707 {
13708 int n = 0, field, prec;
13709 int literal = 0;
13710
13711 tail_recurse:
13712 if (depth > 10)
13713 goto invalid;
13714
13715 depth++;
13716
13717 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
13718 {
13719 case Lisp_String:
13720 {
13721 /* A string: output it and check for %-constructs within it. */
13722 unsigned char c;
13723 const unsigned char *this, *lisp_string;
13724
13725 if (!NILP (props) || risky)
13726 {
13727 Lisp_Object oprops, aelt;
13728 oprops = Ftext_properties_at (make_number (0), elt);
13729
13730 if (NILP (Fequal (props, oprops)) || risky)
13731 {
13732 /* If the starting string has properties,
13733 merge the specified ones onto the existing ones. */
13734 if (! NILP (oprops) && !risky)
13735 {
13736 Lisp_Object tem;
13737
13738 oprops = Fcopy_sequence (oprops);
13739 tem = props;
13740 while (CONSP (tem))
13741 {
13742 oprops = Fplist_put (oprops, XCAR (tem),
13743 XCAR (XCDR (tem)));
13744 tem = XCDR (XCDR (tem));
13745 }
13746 props = oprops;
13747 }
13748
13749 aelt = Fassoc (elt, mode_line_proptrans_alist);
13750 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
13751 {
13752 mode_line_proptrans_alist
13753 = Fcons (aelt, Fdelq (aelt, mode_line_proptrans_alist));
13754 elt = XCAR (aelt);
13755 }
13756 else
13757 {
13758 Lisp_Object tem;
13759
13760 elt = Fcopy_sequence (elt);
13761 Fset_text_properties (make_number (0), Flength (elt),
13762 props, elt);
13763 /* Add this item to mode_line_proptrans_alist. */
13764 mode_line_proptrans_alist
13765 = Fcons (Fcons (elt, props),
13766 mode_line_proptrans_alist);
13767 /* Truncate mode_line_proptrans_alist
13768 to at most 50 elements. */
13769 tem = Fnthcdr (make_number (50),
13770 mode_line_proptrans_alist);
13771 if (! NILP (tem))
13772 XSETCDR (tem, Qnil);
13773 }
13774 }
13775 }
13776
13777 this = SDATA (elt);
13778 lisp_string = this;
13779
13780 if (literal)
13781 {
13782 prec = precision - n;
13783 if (frame_title_ptr)
13784 n += store_frame_title (SDATA (elt), -1, prec);
13785 else if (!NILP (mode_line_string_list))
13786 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
13787 else
13788 n += display_string (NULL, elt, Qnil, 0, 0, it,
13789 0, prec, 0, STRING_MULTIBYTE (elt));
13790
13791 break;
13792 }
13793
13794 while ((precision <= 0 || n < precision)
13795 && *this
13796 && (frame_title_ptr
13797 || !NILP (mode_line_string_list)
13798 || it->current_x < it->last_visible_x))
13799 {
13800 const unsigned char *last = this;
13801
13802 /* Advance to end of string or next format specifier. */
13803 while ((c = *this++) != '\0' && c != '%')
13804 ;
13805
13806 if (this - 1 != last)
13807 {
13808 /* Output to end of string or up to '%'. Field width
13809 is length of string. Don't output more than
13810 PRECISION allows us. */
13811 --this;
13812
13813 prec = chars_in_text (last, this - last);
13814 if (precision > 0 && prec > precision - n)
13815 prec = precision - n;
13816
13817 if (frame_title_ptr)
13818 n += store_frame_title (last, 0, prec);
13819 else if (!NILP (mode_line_string_list))
13820 {
13821 int bytepos = last - lisp_string;
13822 int charpos = string_byte_to_char (elt, bytepos);
13823 n += store_mode_line_string (NULL,
13824 Fsubstring (elt, make_number (charpos),
13825 make_number (charpos + prec)),
13826 0, 0, 0, Qnil);
13827 }
13828 else
13829 {
13830 int bytepos = last - lisp_string;
13831 int charpos = string_byte_to_char (elt, bytepos);
13832 n += display_string (NULL, elt, Qnil, 0, charpos,
13833 it, 0, prec, 0,
13834 STRING_MULTIBYTE (elt));
13835 }
13836 }
13837 else /* c == '%' */
13838 {
13839 const unsigned char *percent_position = this;
13840
13841 /* Get the specified minimum width. Zero means
13842 don't pad. */
13843 field = 0;
13844 while ((c = *this++) >= '0' && c <= '9')
13845 field = field * 10 + c - '0';
13846
13847 /* Don't pad beyond the total padding allowed. */
13848 if (field_width - n > 0 && field > field_width - n)
13849 field = field_width - n;
13850
13851 /* Note that either PRECISION <= 0 or N < PRECISION. */
13852 prec = precision - n;
13853
13854 if (c == 'M')
13855 n += display_mode_element (it, depth, field, prec,
13856 Vglobal_mode_string, props,
13857 risky);
13858 else if (c != 0)
13859 {
13860 int multibyte;
13861 int bytepos, charpos;
13862 unsigned char *spec;
13863
13864 bytepos = percent_position - lisp_string;
13865 charpos = (STRING_MULTIBYTE (elt)
13866 ? string_byte_to_char (elt, bytepos)
13867 : bytepos);
13868
13869 spec
13870 = decode_mode_spec (it->w, c, field, prec, &multibyte);
13871
13872 if (frame_title_ptr)
13873 n += store_frame_title (spec, field, prec);
13874 else if (!NILP (mode_line_string_list))
13875 {
13876 int len = strlen (spec);
13877 Lisp_Object tem = make_string (spec, len);
13878 props = Ftext_properties_at (make_number (charpos), elt);
13879 /* Should only keep face property in props */
13880 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
13881 }
13882 else
13883 {
13884 int nglyphs_before, nwritten;
13885
13886 nglyphs_before = it->glyph_row->used[TEXT_AREA];
13887 nwritten = display_string (spec, Qnil, elt,
13888 charpos, 0, it,
13889 field, prec, 0,
13890 multibyte);
13891
13892 /* Assign to the glyphs written above the
13893 string where the `%x' came from, position
13894 of the `%'. */
13895 if (nwritten > 0)
13896 {
13897 struct glyph *glyph
13898 = (it->glyph_row->glyphs[TEXT_AREA]
13899 + nglyphs_before);
13900 int i;
13901
13902 for (i = 0; i < nwritten; ++i)
13903 {
13904 glyph[i].object = elt;
13905 glyph[i].charpos = charpos;
13906 }
13907
13908 n += nwritten;
13909 }
13910 }
13911 }
13912 else /* c == 0 */
13913 break;
13914 }
13915 }
13916 }
13917 break;
13918
13919 case Lisp_Symbol:
13920 /* A symbol: process the value of the symbol recursively
13921 as if it appeared here directly. Avoid error if symbol void.
13922 Special case: if value of symbol is a string, output the string
13923 literally. */
13924 {
13925 register Lisp_Object tem;
13926
13927 /* If the variable is not marked as risky to set
13928 then its contents are risky to use. */
13929 if (NILP (Fget (elt, Qrisky_local_variable)))
13930 risky = 1;
13931
13932 tem = Fboundp (elt);
13933 if (!NILP (tem))
13934 {
13935 tem = Fsymbol_value (elt);
13936 /* If value is a string, output that string literally:
13937 don't check for % within it. */
13938 if (STRINGP (tem))
13939 literal = 1;
13940
13941 if (!EQ (tem, elt))
13942 {
13943 /* Give up right away for nil or t. */
13944 elt = tem;
13945 goto tail_recurse;
13946 }
13947 }
13948 }
13949 break;
13950
13951 case Lisp_Cons:
13952 {
13953 register Lisp_Object car, tem;
13954
13955 /* A cons cell: five distinct cases.
13956 If first element is :eval or :propertize, do something special.
13957 If first element is a string or a cons, process all the elements
13958 and effectively concatenate them.
13959 If first element is a negative number, truncate displaying cdr to
13960 at most that many characters. If positive, pad (with spaces)
13961 to at least that many characters.
13962 If first element is a symbol, process the cadr or caddr recursively
13963 according to whether the symbol's value is non-nil or nil. */
13964 car = XCAR (elt);
13965 if (EQ (car, QCeval))
13966 {
13967 /* An element of the form (:eval FORM) means evaluate FORM
13968 and use the result as mode line elements. */
13969
13970 if (risky)
13971 break;
13972
13973 if (CONSP (XCDR (elt)))
13974 {
13975 Lisp_Object spec;
13976 spec = safe_eval (XCAR (XCDR (elt)));
13977 n += display_mode_element (it, depth, field_width - n,
13978 precision - n, spec, props,
13979 risky);
13980 }
13981 }
13982 else if (EQ (car, QCpropertize))
13983 {
13984 /* An element of the form (:propertize ELT PROPS...)
13985 means display ELT but applying properties PROPS. */
13986
13987 if (risky)
13988 break;
13989
13990 if (CONSP (XCDR (elt)))
13991 n += display_mode_element (it, depth, field_width - n,
13992 precision - n, XCAR (XCDR (elt)),
13993 XCDR (XCDR (elt)), risky);
13994 }
13995 else if (SYMBOLP (car))
13996 {
13997 tem = Fboundp (car);
13998 elt = XCDR (elt);
13999 if (!CONSP (elt))
14000 goto invalid;
14001 /* elt is now the cdr, and we know it is a cons cell.
14002 Use its car if CAR has a non-nil value. */
14003 if (!NILP (tem))
14004 {
14005 tem = Fsymbol_value (car);
14006 if (!NILP (tem))
14007 {
14008 elt = XCAR (elt);
14009 goto tail_recurse;
14010 }
14011 }
14012 /* Symbol's value is nil (or symbol is unbound)
14013 Get the cddr of the original list
14014 and if possible find the caddr and use that. */
14015 elt = XCDR (elt);
14016 if (NILP (elt))
14017 break;
14018 else if (!CONSP (elt))
14019 goto invalid;
14020 elt = XCAR (elt);
14021 goto tail_recurse;
14022 }
14023 else if (INTEGERP (car))
14024 {
14025 register int lim = XINT (car);
14026 elt = XCDR (elt);
14027 if (lim < 0)
14028 {
14029 /* Negative int means reduce maximum width. */
14030 if (precision <= 0)
14031 precision = -lim;
14032 else
14033 precision = min (precision, -lim);
14034 }
14035 else if (lim > 0)
14036 {
14037 /* Padding specified. Don't let it be more than
14038 current maximum. */
14039 if (precision > 0)
14040 lim = min (precision, lim);
14041
14042 /* If that's more padding than already wanted, queue it.
14043 But don't reduce padding already specified even if
14044 that is beyond the current truncation point. */
14045 field_width = max (lim, field_width);
14046 }
14047 goto tail_recurse;
14048 }
14049 else if (STRINGP (car) || CONSP (car))
14050 {
14051 register int limit = 50;
14052 /* Limit is to protect against circular lists. */
14053 while (CONSP (elt)
14054 && --limit > 0
14055 && (precision <= 0 || n < precision))
14056 {
14057 n += display_mode_element (it, depth, field_width - n,
14058 precision - n, XCAR (elt),
14059 props, risky);
14060 elt = XCDR (elt);
14061 }
14062 }
14063 }
14064 break;
14065
14066 default:
14067 invalid:
14068 if (frame_title_ptr)
14069 n += store_frame_title ("*invalid*", 0, precision - n);
14070 else if (!NILP (mode_line_string_list))
14071 n += store_mode_line_string ("*invalid*", Qnil, 0, 0, precision - n, Qnil);
14072 else
14073 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
14074 precision - n, 0, 0);
14075 return n;
14076 }
14077
14078 /* Pad to FIELD_WIDTH. */
14079 if (field_width > 0 && n < field_width)
14080 {
14081 if (frame_title_ptr)
14082 n += store_frame_title ("", field_width - n, 0);
14083 else if (!NILP (mode_line_string_list))
14084 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
14085 else
14086 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
14087 0, 0, 0);
14088 }
14089
14090 return n;
14091 }
14092
14093 /* Store a mode-line string element in mode_line_string_list.
14094
14095 If STRING is non-null, display that C string. Otherwise, the Lisp
14096 string LISP_STRING is displayed.
14097
14098 FIELD_WIDTH is the minimum number of output glyphs to produce.
14099 If STRING has fewer characters than FIELD_WIDTH, pad to the right
14100 with spaces. FIELD_WIDTH <= 0 means don't pad.
14101
14102 PRECISION is the maximum number of characters to output from
14103 STRING. PRECISION <= 0 means don't truncate the string.
14104
14105 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
14106 properties to the string.
14107
14108 PROPS are the properties to add to the string.
14109 The mode_line_string_face face property is always added to the string.
14110 */
14111
14112 static int store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
14113 char *string;
14114 Lisp_Object lisp_string;
14115 int copy_string;
14116 int field_width;
14117 int precision;
14118 Lisp_Object props;
14119 {
14120 int len;
14121 int n = 0;
14122
14123 if (string != NULL)
14124 {
14125 len = strlen (string);
14126 if (precision > 0 && len > precision)
14127 len = precision;
14128 lisp_string = make_string (string, len);
14129 if (NILP (props))
14130 props = mode_line_string_face_prop;
14131 else if (!NILP (mode_line_string_face))
14132 {
14133 Lisp_Object face = Fplist_get (props, Qface);
14134 props = Fcopy_sequence (props);
14135 if (NILP (face))
14136 face = mode_line_string_face;
14137 else
14138 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
14139 props = Fplist_put (props, Qface, face);
14140 }
14141 Fadd_text_properties (make_number (0), make_number (len),
14142 props, lisp_string);
14143 }
14144 else
14145 {
14146 len = XFASTINT (Flength (lisp_string));
14147 if (precision > 0 && len > precision)
14148 {
14149 len = precision;
14150 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
14151 precision = -1;
14152 }
14153 if (!NILP (mode_line_string_face))
14154 {
14155 Lisp_Object face;
14156 if (NILP (props))
14157 props = Ftext_properties_at (make_number (0), lisp_string);
14158 face = Fplist_get (props, Qface);
14159 if (NILP (face))
14160 face = mode_line_string_face;
14161 else
14162 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
14163 props = Fcons (Qface, Fcons (face, Qnil));
14164 if (copy_string)
14165 lisp_string = Fcopy_sequence (lisp_string);
14166 }
14167 if (!NILP (props))
14168 Fadd_text_properties (make_number (0), make_number (len),
14169 props, lisp_string);
14170 }
14171
14172 if (len > 0)
14173 {
14174 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
14175 n += len;
14176 }
14177
14178 if (field_width > len)
14179 {
14180 field_width -= len;
14181 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
14182 if (!NILP (props))
14183 Fadd_text_properties (make_number (0), make_number (field_width),
14184 props, lisp_string);
14185 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
14186 n += field_width;
14187 }
14188
14189 return n;
14190 }
14191
14192
14193 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
14194 0, 3, 0,
14195 doc: /* Return the mode-line of selected window as a string.
14196 First optional arg FORMAT specifies a different format string (see
14197 `mode-line-format' for details) to use. If FORMAT is t, return
14198 the buffer's header-line. Second optional arg WINDOW specifies a
14199 different window to use as the context for the formatting.
14200 If third optional arg NO-PROPS is non-nil, string is not propertized. */)
14201 (format, window, no_props)
14202 Lisp_Object format, window, no_props;
14203 {
14204 struct it it;
14205 int len;
14206 struct window *w;
14207 struct buffer *old_buffer = NULL;
14208 enum face_id face_id = DEFAULT_FACE_ID;
14209
14210 if (NILP (window))
14211 window = selected_window;
14212 CHECK_WINDOW (window);
14213 w = XWINDOW (window);
14214 CHECK_BUFFER (w->buffer);
14215
14216 if (XBUFFER (w->buffer) != current_buffer)
14217 {
14218 old_buffer = current_buffer;
14219 set_buffer_internal_1 (XBUFFER (w->buffer));
14220 }
14221
14222 if (NILP (format) || EQ (format, Qt))
14223 {
14224 face_id = NILP (format)
14225 ? CURRENT_MODE_LINE_FACE_ID (w) :
14226 HEADER_LINE_FACE_ID;
14227 format = NILP (format)
14228 ? current_buffer->mode_line_format
14229 : current_buffer->header_line_format;
14230 }
14231
14232 init_iterator (&it, w, -1, -1, NULL, face_id);
14233
14234 if (NILP (no_props))
14235 {
14236 mode_line_string_face =
14237 (face_id == MODE_LINE_FACE_ID ? Qmode_line :
14238 face_id == MODE_LINE_INACTIVE_FACE_ID ? Qmode_line_inactive :
14239 face_id == HEADER_LINE_FACE_ID ? Qheader_line : Qnil);
14240
14241 mode_line_string_face_prop =
14242 NILP (mode_line_string_face) ? Qnil :
14243 Fcons (Qface, Fcons (mode_line_string_face, Qnil));
14244
14245 /* We need a dummy last element in mode_line_string_list to
14246 indicate we are building the propertized mode-line string.
14247 Using mode_line_string_face_prop here GC protects it. */
14248 mode_line_string_list =
14249 Fcons (mode_line_string_face_prop, Qnil);
14250 frame_title_ptr = NULL;
14251 }
14252 else
14253 {
14254 mode_line_string_face_prop = Qnil;
14255 mode_line_string_list = Qnil;
14256 frame_title_ptr = frame_title_buf;
14257 }
14258
14259 push_frame_kboard (it.f);
14260 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
14261 pop_frame_kboard ();
14262
14263 if (old_buffer)
14264 set_buffer_internal_1 (old_buffer);
14265
14266 if (NILP (no_props))
14267 {
14268 Lisp_Object str;
14269 mode_line_string_list = Fnreverse (mode_line_string_list);
14270 str = Fmapconcat (intern ("identity"), XCDR (mode_line_string_list),
14271 make_string ("", 0));
14272 mode_line_string_face_prop = Qnil;
14273 mode_line_string_list = Qnil;
14274 return str;
14275 }
14276
14277 len = frame_title_ptr - frame_title_buf;
14278 if (len > 0 && frame_title_ptr[-1] == '-')
14279 {
14280 /* Mode lines typically ends with numerous dashes; reduce to two dashes. */
14281 while (frame_title_ptr > frame_title_buf && *--frame_title_ptr == '-')
14282 ;
14283 frame_title_ptr += 3; /* restore last non-dash + two dashes */
14284 if (len > frame_title_ptr - frame_title_buf)
14285 len = frame_title_ptr - frame_title_buf;
14286 }
14287
14288 frame_title_ptr = NULL;
14289 return make_string (frame_title_buf, len);
14290 }
14291
14292 /* Write a null-terminated, right justified decimal representation of
14293 the positive integer D to BUF using a minimal field width WIDTH. */
14294
14295 static void
14296 pint2str (buf, width, d)
14297 register char *buf;
14298 register int width;
14299 register int d;
14300 {
14301 register char *p = buf;
14302
14303 if (d <= 0)
14304 *p++ = '0';
14305 else
14306 {
14307 while (d > 0)
14308 {
14309 *p++ = d % 10 + '0';
14310 d /= 10;
14311 }
14312 }
14313
14314 for (width -= (int) (p - buf); width > 0; --width)
14315 *p++ = ' ';
14316 *p-- = '\0';
14317 while (p > buf)
14318 {
14319 d = *buf;
14320 *buf++ = *p;
14321 *p-- = d;
14322 }
14323 }
14324
14325 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
14326 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
14327 type of CODING_SYSTEM. Return updated pointer into BUF. */
14328
14329 static unsigned char invalid_eol_type[] = "(*invalid*)";
14330
14331 static char *
14332 decode_mode_spec_coding (coding_system, buf, eol_flag)
14333 Lisp_Object coding_system;
14334 register char *buf;
14335 int eol_flag;
14336 {
14337 Lisp_Object val;
14338 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
14339 const unsigned char *eol_str;
14340 int eol_str_len;
14341 /* The EOL conversion we are using. */
14342 Lisp_Object eoltype;
14343
14344 val = Fget (coding_system, Qcoding_system);
14345 eoltype = Qnil;
14346
14347 if (!VECTORP (val)) /* Not yet decided. */
14348 {
14349 if (multibyte)
14350 *buf++ = '-';
14351 if (eol_flag)
14352 eoltype = eol_mnemonic_undecided;
14353 /* Don't mention EOL conversion if it isn't decided. */
14354 }
14355 else
14356 {
14357 Lisp_Object eolvalue;
14358
14359 eolvalue = Fget (coding_system, Qeol_type);
14360
14361 if (multibyte)
14362 *buf++ = XFASTINT (AREF (val, 1));
14363
14364 if (eol_flag)
14365 {
14366 /* The EOL conversion that is normal on this system. */
14367
14368 if (NILP (eolvalue)) /* Not yet decided. */
14369 eoltype = eol_mnemonic_undecided;
14370 else if (VECTORP (eolvalue)) /* Not yet decided. */
14371 eoltype = eol_mnemonic_undecided;
14372 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
14373 eoltype = (XFASTINT (eolvalue) == 0
14374 ? eol_mnemonic_unix
14375 : (XFASTINT (eolvalue) == 1
14376 ? eol_mnemonic_dos : eol_mnemonic_mac));
14377 }
14378 }
14379
14380 if (eol_flag)
14381 {
14382 /* Mention the EOL conversion if it is not the usual one. */
14383 if (STRINGP (eoltype))
14384 {
14385 eol_str = SDATA (eoltype);
14386 eol_str_len = SBYTES (eoltype);
14387 }
14388 else if (INTEGERP (eoltype)
14389 && CHAR_VALID_P (XINT (eoltype), 0))
14390 {
14391 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
14392 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
14393 eol_str = tmp;
14394 }
14395 else
14396 {
14397 eol_str = invalid_eol_type;
14398 eol_str_len = sizeof (invalid_eol_type) - 1;
14399 }
14400 bcopy (eol_str, buf, eol_str_len);
14401 buf += eol_str_len;
14402 }
14403
14404 return buf;
14405 }
14406
14407 /* Return a string for the output of a mode line %-spec for window W,
14408 generated by character C. PRECISION >= 0 means don't return a
14409 string longer than that value. FIELD_WIDTH > 0 means pad the
14410 string returned with spaces to that value. Return 1 in *MULTIBYTE
14411 if the result is multibyte text. */
14412
14413 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
14414
14415 static char *
14416 decode_mode_spec (w, c, field_width, precision, multibyte)
14417 struct window *w;
14418 register int c;
14419 int field_width, precision;
14420 int *multibyte;
14421 {
14422 Lisp_Object obj;
14423 struct frame *f = XFRAME (WINDOW_FRAME (w));
14424 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
14425 struct buffer *b = XBUFFER (w->buffer);
14426
14427 obj = Qnil;
14428 *multibyte = 0;
14429
14430 switch (c)
14431 {
14432 case '*':
14433 if (!NILP (b->read_only))
14434 return "%";
14435 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
14436 return "*";
14437 return "-";
14438
14439 case '+':
14440 /* This differs from %* only for a modified read-only buffer. */
14441 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
14442 return "*";
14443 if (!NILP (b->read_only))
14444 return "%";
14445 return "-";
14446
14447 case '&':
14448 /* This differs from %* in ignoring read-only-ness. */
14449 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
14450 return "*";
14451 return "-";
14452
14453 case '%':
14454 return "%";
14455
14456 case '[':
14457 {
14458 int i;
14459 char *p;
14460
14461 if (command_loop_level > 5)
14462 return "[[[... ";
14463 p = decode_mode_spec_buf;
14464 for (i = 0; i < command_loop_level; i++)
14465 *p++ = '[';
14466 *p = 0;
14467 return decode_mode_spec_buf;
14468 }
14469
14470 case ']':
14471 {
14472 int i;
14473 char *p;
14474
14475 if (command_loop_level > 5)
14476 return " ...]]]";
14477 p = decode_mode_spec_buf;
14478 for (i = 0; i < command_loop_level; i++)
14479 *p++ = ']';
14480 *p = 0;
14481 return decode_mode_spec_buf;
14482 }
14483
14484 case '-':
14485 {
14486 register int i;
14487
14488 /* Let lots_of_dashes be a string of infinite length. */
14489 if (!NILP (mode_line_string_list))
14490 return "--";
14491 if (field_width <= 0
14492 || field_width > sizeof (lots_of_dashes))
14493 {
14494 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
14495 decode_mode_spec_buf[i] = '-';
14496 decode_mode_spec_buf[i] = '\0';
14497 return decode_mode_spec_buf;
14498 }
14499 else
14500 return lots_of_dashes;
14501 }
14502
14503 case 'b':
14504 obj = b->name;
14505 break;
14506
14507 case 'c':
14508 {
14509 int col = (int) current_column (); /* iftc */
14510 w->column_number_displayed = make_number (col);
14511 pint2str (decode_mode_spec_buf, field_width, col);
14512 return decode_mode_spec_buf;
14513 }
14514
14515 case 'F':
14516 /* %F displays the frame name. */
14517 if (!NILP (f->title))
14518 return (char *) SDATA (f->title);
14519 if (f->explicit_name || ! FRAME_WINDOW_P (f))
14520 return (char *) SDATA (f->name);
14521 return "Emacs";
14522
14523 case 'f':
14524 obj = b->filename;
14525 break;
14526
14527 case 'l':
14528 {
14529 int startpos = XMARKER (w->start)->charpos;
14530 int startpos_byte = marker_byte_position (w->start);
14531 int line, linepos, linepos_byte, topline;
14532 int nlines, junk;
14533 int height = XFASTINT (w->height);
14534
14535 /* If we decided that this buffer isn't suitable for line numbers,
14536 don't forget that too fast. */
14537 if (EQ (w->base_line_pos, w->buffer))
14538 goto no_value;
14539 /* But do forget it, if the window shows a different buffer now. */
14540 else if (BUFFERP (w->base_line_pos))
14541 w->base_line_pos = Qnil;
14542
14543 /* If the buffer is very big, don't waste time. */
14544 if (INTEGERP (Vline_number_display_limit)
14545 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
14546 {
14547 w->base_line_pos = Qnil;
14548 w->base_line_number = Qnil;
14549 goto no_value;
14550 }
14551
14552 if (!NILP (w->base_line_number)
14553 && !NILP (w->base_line_pos)
14554 && XFASTINT (w->base_line_pos) <= startpos)
14555 {
14556 line = XFASTINT (w->base_line_number);
14557 linepos = XFASTINT (w->base_line_pos);
14558 linepos_byte = buf_charpos_to_bytepos (b, linepos);
14559 }
14560 else
14561 {
14562 line = 1;
14563 linepos = BUF_BEGV (b);
14564 linepos_byte = BUF_BEGV_BYTE (b);
14565 }
14566
14567 /* Count lines from base line to window start position. */
14568 nlines = display_count_lines (linepos, linepos_byte,
14569 startpos_byte,
14570 startpos, &junk);
14571
14572 topline = nlines + line;
14573
14574 /* Determine a new base line, if the old one is too close
14575 or too far away, or if we did not have one.
14576 "Too close" means it's plausible a scroll-down would
14577 go back past it. */
14578 if (startpos == BUF_BEGV (b))
14579 {
14580 w->base_line_number = make_number (topline);
14581 w->base_line_pos = make_number (BUF_BEGV (b));
14582 }
14583 else if (nlines < height + 25 || nlines > height * 3 + 50
14584 || linepos == BUF_BEGV (b))
14585 {
14586 int limit = BUF_BEGV (b);
14587 int limit_byte = BUF_BEGV_BYTE (b);
14588 int position;
14589 int distance = (height * 2 + 30) * line_number_display_limit_width;
14590
14591 if (startpos - distance > limit)
14592 {
14593 limit = startpos - distance;
14594 limit_byte = CHAR_TO_BYTE (limit);
14595 }
14596
14597 nlines = display_count_lines (startpos, startpos_byte,
14598 limit_byte,
14599 - (height * 2 + 30),
14600 &position);
14601 /* If we couldn't find the lines we wanted within
14602 line_number_display_limit_width chars per line,
14603 give up on line numbers for this window. */
14604 if (position == limit_byte && limit == startpos - distance)
14605 {
14606 w->base_line_pos = w->buffer;
14607 w->base_line_number = Qnil;
14608 goto no_value;
14609 }
14610
14611 w->base_line_number = make_number (topline - nlines);
14612 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
14613 }
14614
14615 /* Now count lines from the start pos to point. */
14616 nlines = display_count_lines (startpos, startpos_byte,
14617 PT_BYTE, PT, &junk);
14618
14619 /* Record that we did display the line number. */
14620 line_number_displayed = 1;
14621
14622 /* Make the string to show. */
14623 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
14624 return decode_mode_spec_buf;
14625 no_value:
14626 {
14627 char* p = decode_mode_spec_buf;
14628 int pad = field_width - 2;
14629 while (pad-- > 0)
14630 *p++ = ' ';
14631 *p++ = '?';
14632 *p++ = '?';
14633 *p = '\0';
14634 return decode_mode_spec_buf;
14635 }
14636 }
14637 break;
14638
14639 case 'm':
14640 obj = b->mode_name;
14641 break;
14642
14643 case 'n':
14644 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
14645 return " Narrow";
14646 break;
14647
14648 case 'p':
14649 {
14650 int pos = marker_position (w->start);
14651 int total = BUF_ZV (b) - BUF_BEGV (b);
14652
14653 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
14654 {
14655 if (pos <= BUF_BEGV (b))
14656 return "All";
14657 else
14658 return "Bottom";
14659 }
14660 else if (pos <= BUF_BEGV (b))
14661 return "Top";
14662 else
14663 {
14664 if (total > 1000000)
14665 /* Do it differently for a large value, to avoid overflow. */
14666 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
14667 else
14668 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
14669 /* We can't normally display a 3-digit number,
14670 so get us a 2-digit number that is close. */
14671 if (total == 100)
14672 total = 99;
14673 sprintf (decode_mode_spec_buf, "%2d%%", total);
14674 return decode_mode_spec_buf;
14675 }
14676 }
14677
14678 /* Display percentage of size above the bottom of the screen. */
14679 case 'P':
14680 {
14681 int toppos = marker_position (w->start);
14682 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
14683 int total = BUF_ZV (b) - BUF_BEGV (b);
14684
14685 if (botpos >= BUF_ZV (b))
14686 {
14687 if (toppos <= BUF_BEGV (b))
14688 return "All";
14689 else
14690 return "Bottom";
14691 }
14692 else
14693 {
14694 if (total > 1000000)
14695 /* Do it differently for a large value, to avoid overflow. */
14696 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
14697 else
14698 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
14699 /* We can't normally display a 3-digit number,
14700 so get us a 2-digit number that is close. */
14701 if (total == 100)
14702 total = 99;
14703 if (toppos <= BUF_BEGV (b))
14704 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
14705 else
14706 sprintf (decode_mode_spec_buf, "%2d%%", total);
14707 return decode_mode_spec_buf;
14708 }
14709 }
14710
14711 case 's':
14712 /* status of process */
14713 obj = Fget_buffer_process (w->buffer);
14714 if (NILP (obj))
14715 return "no process";
14716 #ifdef subprocesses
14717 obj = Fsymbol_name (Fprocess_status (obj));
14718 #endif
14719 break;
14720
14721 case 't': /* indicate TEXT or BINARY */
14722 #ifdef MODE_LINE_BINARY_TEXT
14723 return MODE_LINE_BINARY_TEXT (b);
14724 #else
14725 return "T";
14726 #endif
14727
14728 case 'z':
14729 /* coding-system (not including end-of-line format) */
14730 case 'Z':
14731 /* coding-system (including end-of-line type) */
14732 {
14733 int eol_flag = (c == 'Z');
14734 char *p = decode_mode_spec_buf;
14735
14736 if (! FRAME_WINDOW_P (f))
14737 {
14738 /* No need to mention EOL here--the terminal never needs
14739 to do EOL conversion. */
14740 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
14741 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
14742 }
14743 p = decode_mode_spec_coding (b->buffer_file_coding_system,
14744 p, eol_flag);
14745
14746 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
14747 #ifdef subprocesses
14748 obj = Fget_buffer_process (Fcurrent_buffer ());
14749 if (PROCESSP (obj))
14750 {
14751 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
14752 p, eol_flag);
14753 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
14754 p, eol_flag);
14755 }
14756 #endif /* subprocesses */
14757 #endif /* 0 */
14758 *p = 0;
14759 return decode_mode_spec_buf;
14760 }
14761 }
14762
14763 if (STRINGP (obj))
14764 {
14765 *multibyte = STRING_MULTIBYTE (obj);
14766 return (char *) SDATA (obj);
14767 }
14768 else
14769 return "";
14770 }
14771
14772
14773 /* Count up to COUNT lines starting from START / START_BYTE.
14774 But don't go beyond LIMIT_BYTE.
14775 Return the number of lines thus found (always nonnegative).
14776
14777 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
14778
14779 static int
14780 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
14781 int start, start_byte, limit_byte, count;
14782 int *byte_pos_ptr;
14783 {
14784 register unsigned char *cursor;
14785 unsigned char *base;
14786
14787 register int ceiling;
14788 register unsigned char *ceiling_addr;
14789 int orig_count = count;
14790
14791 /* If we are not in selective display mode,
14792 check only for newlines. */
14793 int selective_display = (!NILP (current_buffer->selective_display)
14794 && !INTEGERP (current_buffer->selective_display));
14795
14796 if (count > 0)
14797 {
14798 while (start_byte < limit_byte)
14799 {
14800 ceiling = BUFFER_CEILING_OF (start_byte);
14801 ceiling = min (limit_byte - 1, ceiling);
14802 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
14803 base = (cursor = BYTE_POS_ADDR (start_byte));
14804 while (1)
14805 {
14806 if (selective_display)
14807 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
14808 ;
14809 else
14810 while (*cursor != '\n' && ++cursor != ceiling_addr)
14811 ;
14812
14813 if (cursor != ceiling_addr)
14814 {
14815 if (--count == 0)
14816 {
14817 start_byte += cursor - base + 1;
14818 *byte_pos_ptr = start_byte;
14819 return orig_count;
14820 }
14821 else
14822 if (++cursor == ceiling_addr)
14823 break;
14824 }
14825 else
14826 break;
14827 }
14828 start_byte += cursor - base;
14829 }
14830 }
14831 else
14832 {
14833 while (start_byte > limit_byte)
14834 {
14835 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
14836 ceiling = max (limit_byte, ceiling);
14837 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
14838 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
14839 while (1)
14840 {
14841 if (selective_display)
14842 while (--cursor != ceiling_addr
14843 && *cursor != '\n' && *cursor != 015)
14844 ;
14845 else
14846 while (--cursor != ceiling_addr && *cursor != '\n')
14847 ;
14848
14849 if (cursor != ceiling_addr)
14850 {
14851 if (++count == 0)
14852 {
14853 start_byte += cursor - base + 1;
14854 *byte_pos_ptr = start_byte;
14855 /* When scanning backwards, we should
14856 not count the newline posterior to which we stop. */
14857 return - orig_count - 1;
14858 }
14859 }
14860 else
14861 break;
14862 }
14863 /* Here we add 1 to compensate for the last decrement
14864 of CURSOR, which took it past the valid range. */
14865 start_byte += cursor - base + 1;
14866 }
14867 }
14868
14869 *byte_pos_ptr = limit_byte;
14870
14871 if (count < 0)
14872 return - orig_count + count;
14873 return orig_count - count;
14874
14875 }
14876
14877
14878 \f
14879 /***********************************************************************
14880 Displaying strings
14881 ***********************************************************************/
14882
14883 /* Display a NUL-terminated string, starting with index START.
14884
14885 If STRING is non-null, display that C string. Otherwise, the Lisp
14886 string LISP_STRING is displayed.
14887
14888 If FACE_STRING is not nil, FACE_STRING_POS is a position in
14889 FACE_STRING. Display STRING or LISP_STRING with the face at
14890 FACE_STRING_POS in FACE_STRING:
14891
14892 Display the string in the environment given by IT, but use the
14893 standard display table, temporarily.
14894
14895 FIELD_WIDTH is the minimum number of output glyphs to produce.
14896 If STRING has fewer characters than FIELD_WIDTH, pad to the right
14897 with spaces. If STRING has more characters, more than FIELD_WIDTH
14898 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
14899
14900 PRECISION is the maximum number of characters to output from
14901 STRING. PRECISION < 0 means don't truncate the string.
14902
14903 This is roughly equivalent to printf format specifiers:
14904
14905 FIELD_WIDTH PRECISION PRINTF
14906 ----------------------------------------
14907 -1 -1 %s
14908 -1 10 %.10s
14909 10 -1 %10s
14910 20 10 %20.10s
14911
14912 MULTIBYTE zero means do not display multibyte chars, > 0 means do
14913 display them, and < 0 means obey the current buffer's value of
14914 enable_multibyte_characters.
14915
14916 Value is the number of glyphs produced. */
14917
14918 static int
14919 display_string (string, lisp_string, face_string, face_string_pos,
14920 start, it, field_width, precision, max_x, multibyte)
14921 unsigned char *string;
14922 Lisp_Object lisp_string;
14923 Lisp_Object face_string;
14924 int face_string_pos;
14925 int start;
14926 struct it *it;
14927 int field_width, precision, max_x;
14928 int multibyte;
14929 {
14930 int hpos_at_start = it->hpos;
14931 int saved_face_id = it->face_id;
14932 struct glyph_row *row = it->glyph_row;
14933
14934 /* Initialize the iterator IT for iteration over STRING beginning
14935 with index START. */
14936 reseat_to_string (it, string, lisp_string, start,
14937 precision, field_width, multibyte);
14938
14939 /* If displaying STRING, set up the face of the iterator
14940 from LISP_STRING, if that's given. */
14941 if (STRINGP (face_string))
14942 {
14943 int endptr;
14944 struct face *face;
14945
14946 it->face_id
14947 = face_at_string_position (it->w, face_string, face_string_pos,
14948 0, it->region_beg_charpos,
14949 it->region_end_charpos,
14950 &endptr, it->base_face_id, 0);
14951 face = FACE_FROM_ID (it->f, it->face_id);
14952 it->face_box_p = face->box != FACE_NO_BOX;
14953 }
14954
14955 /* Set max_x to the maximum allowed X position. Don't let it go
14956 beyond the right edge of the window. */
14957 if (max_x <= 0)
14958 max_x = it->last_visible_x;
14959 else
14960 max_x = min (max_x, it->last_visible_x);
14961
14962 /* Skip over display elements that are not visible. because IT->w is
14963 hscrolled. */
14964 if (it->current_x < it->first_visible_x)
14965 move_it_in_display_line_to (it, 100000, it->first_visible_x,
14966 MOVE_TO_POS | MOVE_TO_X);
14967
14968 row->ascent = it->max_ascent;
14969 row->height = it->max_ascent + it->max_descent;
14970 row->phys_ascent = it->max_phys_ascent;
14971 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14972
14973 /* This condition is for the case that we are called with current_x
14974 past last_visible_x. */
14975 while (it->current_x < max_x)
14976 {
14977 int x_before, x, n_glyphs_before, i, nglyphs;
14978
14979 /* Get the next display element. */
14980 if (!get_next_display_element (it))
14981 break;
14982
14983 /* Produce glyphs. */
14984 x_before = it->current_x;
14985 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
14986 PRODUCE_GLYPHS (it);
14987
14988 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
14989 i = 0;
14990 x = x_before;
14991 while (i < nglyphs)
14992 {
14993 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
14994
14995 if (!it->truncate_lines_p
14996 && x + glyph->pixel_width > max_x)
14997 {
14998 /* End of continued line or max_x reached. */
14999 if (CHAR_GLYPH_PADDING_P (*glyph))
15000 {
15001 /* A wide character is unbreakable. */
15002 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
15003 it->current_x = x_before;
15004 }
15005 else
15006 {
15007 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
15008 it->current_x = x;
15009 }
15010 break;
15011 }
15012 else if (x + glyph->pixel_width > it->first_visible_x)
15013 {
15014 /* Glyph is at least partially visible. */
15015 ++it->hpos;
15016 if (x < it->first_visible_x)
15017 it->glyph_row->x = x - it->first_visible_x;
15018 }
15019 else
15020 {
15021 /* Glyph is off the left margin of the display area.
15022 Should not happen. */
15023 abort ();
15024 }
15025
15026 row->ascent = max (row->ascent, it->max_ascent);
15027 row->height = max (row->height, it->max_ascent + it->max_descent);
15028 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15029 row->phys_height = max (row->phys_height,
15030 it->max_phys_ascent + it->max_phys_descent);
15031 x += glyph->pixel_width;
15032 ++i;
15033 }
15034
15035 /* Stop if max_x reached. */
15036 if (i < nglyphs)
15037 break;
15038
15039 /* Stop at line ends. */
15040 if (ITERATOR_AT_END_OF_LINE_P (it))
15041 {
15042 it->continuation_lines_width = 0;
15043 break;
15044 }
15045
15046 set_iterator_to_next (it, 1);
15047
15048 /* Stop if truncating at the right edge. */
15049 if (it->truncate_lines_p
15050 && it->current_x >= it->last_visible_x)
15051 {
15052 /* Add truncation mark, but don't do it if the line is
15053 truncated at a padding space. */
15054 if (IT_CHARPOS (*it) < it->string_nchars)
15055 {
15056 if (!FRAME_WINDOW_P (it->f))
15057 {
15058 int i, n;
15059
15060 if (it->current_x > it->last_visible_x)
15061 {
15062 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
15063 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
15064 break;
15065 for (n = row->used[TEXT_AREA]; i < n; ++i)
15066 {
15067 row->used[TEXT_AREA] = i;
15068 produce_special_glyphs (it, IT_TRUNCATION);
15069 }
15070 }
15071 produce_special_glyphs (it, IT_TRUNCATION);
15072 }
15073 it->glyph_row->truncated_on_right_p = 1;
15074 }
15075 break;
15076 }
15077 }
15078
15079 /* Maybe insert a truncation at the left. */
15080 if (it->first_visible_x
15081 && IT_CHARPOS (*it) > 0)
15082 {
15083 if (!FRAME_WINDOW_P (it->f))
15084 insert_left_trunc_glyphs (it);
15085 it->glyph_row->truncated_on_left_p = 1;
15086 }
15087
15088 it->face_id = saved_face_id;
15089
15090 /* Value is number of columns displayed. */
15091 return it->hpos - hpos_at_start;
15092 }
15093
15094
15095 \f
15096 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
15097 appears as an element of LIST or as the car of an element of LIST.
15098 If PROPVAL is a list, compare each element against LIST in that
15099 way, and return 1/2 if any element of PROPVAL is found in LIST.
15100 Otherwise return 0. This function cannot quit.
15101 The return value is 2 if the text is invisible but with an ellipsis
15102 and 1 if it's invisible and without an ellipsis. */
15103
15104 int
15105 invisible_p (propval, list)
15106 register Lisp_Object propval;
15107 Lisp_Object list;
15108 {
15109 register Lisp_Object tail, proptail;
15110
15111 for (tail = list; CONSP (tail); tail = XCDR (tail))
15112 {
15113 register Lisp_Object tem;
15114 tem = XCAR (tail);
15115 if (EQ (propval, tem))
15116 return 1;
15117 if (CONSP (tem) && EQ (propval, XCAR (tem)))
15118 return NILP (XCDR (tem)) ? 1 : 2;
15119 }
15120
15121 if (CONSP (propval))
15122 {
15123 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
15124 {
15125 Lisp_Object propelt;
15126 propelt = XCAR (proptail);
15127 for (tail = list; CONSP (tail); tail = XCDR (tail))
15128 {
15129 register Lisp_Object tem;
15130 tem = XCAR (tail);
15131 if (EQ (propelt, tem))
15132 return 1;
15133 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
15134 return NILP (XCDR (tem)) ? 1 : 2;
15135 }
15136 }
15137 }
15138
15139 return 0;
15140 }
15141
15142 \f
15143 /***********************************************************************
15144 Initialization
15145 ***********************************************************************/
15146
15147 void
15148 syms_of_xdisp ()
15149 {
15150 Vwith_echo_area_save_vector = Qnil;
15151 staticpro (&Vwith_echo_area_save_vector);
15152
15153 Vmessage_stack = Qnil;
15154 staticpro (&Vmessage_stack);
15155
15156 Qinhibit_redisplay = intern ("inhibit-redisplay");
15157 staticpro (&Qinhibit_redisplay);
15158
15159 message_dolog_marker1 = Fmake_marker ();
15160 staticpro (&message_dolog_marker1);
15161 message_dolog_marker2 = Fmake_marker ();
15162 staticpro (&message_dolog_marker2);
15163 message_dolog_marker3 = Fmake_marker ();
15164 staticpro (&message_dolog_marker3);
15165
15166 #if GLYPH_DEBUG
15167 defsubr (&Sdump_frame_glyph_matrix);
15168 defsubr (&Sdump_glyph_matrix);
15169 defsubr (&Sdump_glyph_row);
15170 defsubr (&Sdump_tool_bar_row);
15171 defsubr (&Strace_redisplay);
15172 defsubr (&Strace_to_stderr);
15173 #endif
15174 #ifdef HAVE_WINDOW_SYSTEM
15175 defsubr (&Stool_bar_lines_needed);
15176 #endif
15177 defsubr (&Sformat_mode_line);
15178
15179 staticpro (&Qmenu_bar_update_hook);
15180 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
15181
15182 staticpro (&Qoverriding_terminal_local_map);
15183 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
15184
15185 staticpro (&Qoverriding_local_map);
15186 Qoverriding_local_map = intern ("overriding-local-map");
15187
15188 staticpro (&Qwindow_scroll_functions);
15189 Qwindow_scroll_functions = intern ("window-scroll-functions");
15190
15191 staticpro (&Qredisplay_end_trigger_functions);
15192 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
15193
15194 staticpro (&Qinhibit_point_motion_hooks);
15195 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
15196
15197 QCdata = intern (":data");
15198 staticpro (&QCdata);
15199 Qdisplay = intern ("display");
15200 staticpro (&Qdisplay);
15201 Qspace_width = intern ("space-width");
15202 staticpro (&Qspace_width);
15203 Qraise = intern ("raise");
15204 staticpro (&Qraise);
15205 Qspace = intern ("space");
15206 staticpro (&Qspace);
15207 Qmargin = intern ("margin");
15208 staticpro (&Qmargin);
15209 Qleft_margin = intern ("left-margin");
15210 staticpro (&Qleft_margin);
15211 Qright_margin = intern ("right-margin");
15212 staticpro (&Qright_margin);
15213 Qalign_to = intern ("align-to");
15214 staticpro (&Qalign_to);
15215 QCalign_to = intern (":align-to");
15216 staticpro (&QCalign_to);
15217 Qrelative_width = intern ("relative-width");
15218 staticpro (&Qrelative_width);
15219 QCrelative_width = intern (":relative-width");
15220 staticpro (&QCrelative_width);
15221 QCrelative_height = intern (":relative-height");
15222 staticpro (&QCrelative_height);
15223 QCeval = intern (":eval");
15224 staticpro (&QCeval);
15225 QCpropertize = intern (":propertize");
15226 staticpro (&QCpropertize);
15227 Qwhen = intern ("when");
15228 staticpro (&Qwhen);
15229 QCfile = intern (":file");
15230 staticpro (&QCfile);
15231 Qfontified = intern ("fontified");
15232 staticpro (&Qfontified);
15233 Qfontification_functions = intern ("fontification-functions");
15234 staticpro (&Qfontification_functions);
15235 Qtrailing_whitespace = intern ("trailing-whitespace");
15236 staticpro (&Qtrailing_whitespace);
15237 Qimage = intern ("image");
15238 staticpro (&Qimage);
15239 Qmessage_truncate_lines = intern ("message-truncate-lines");
15240 staticpro (&Qmessage_truncate_lines);
15241 Qcursor_in_non_selected_windows = intern ("cursor-in-non-selected-windows");
15242 staticpro (&Qcursor_in_non_selected_windows);
15243 Qgrow_only = intern ("grow-only");
15244 staticpro (&Qgrow_only);
15245 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
15246 staticpro (&Qinhibit_menubar_update);
15247 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
15248 staticpro (&Qinhibit_eval_during_redisplay);
15249 Qposition = intern ("position");
15250 staticpro (&Qposition);
15251 Qbuffer_position = intern ("buffer-position");
15252 staticpro (&Qbuffer_position);
15253 Qobject = intern ("object");
15254 staticpro (&Qobject);
15255 Qrisky_local_variable = intern ("risky-local-variable");
15256 staticpro (&Qrisky_local_variable);
15257
15258 list_of_error = Fcons (intern ("error"), Qnil);
15259 staticpro (&list_of_error);
15260
15261 last_arrow_position = Qnil;
15262 last_arrow_string = Qnil;
15263 staticpro (&last_arrow_position);
15264 staticpro (&last_arrow_string);
15265
15266 echo_buffer[0] = echo_buffer[1] = Qnil;
15267 staticpro (&echo_buffer[0]);
15268 staticpro (&echo_buffer[1]);
15269
15270 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
15271 staticpro (&echo_area_buffer[0]);
15272 staticpro (&echo_area_buffer[1]);
15273
15274 Vmessages_buffer_name = build_string ("*Messages*");
15275 staticpro (&Vmessages_buffer_name);
15276
15277 mode_line_proptrans_alist = Qnil;
15278 staticpro (&mode_line_proptrans_alist);
15279
15280 mode_line_string_list = Qnil;
15281 staticpro (&mode_line_string_list);
15282
15283 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
15284 doc: /* Non-nil means highlight trailing whitespace.
15285 The face used for trailing whitespace is `trailing-whitespace'. */);
15286 Vshow_trailing_whitespace = Qnil;
15287
15288 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
15289 doc: /* Non-nil means don't actually do any redisplay.
15290 This is used for internal purposes. */);
15291 Vinhibit_redisplay = Qnil;
15292
15293 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
15294 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
15295 Vglobal_mode_string = Qnil;
15296
15297 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
15298 doc: /* Marker for where to display an arrow on top of the buffer text.
15299 This must be the beginning of a line in order to work.
15300 See also `overlay-arrow-string'. */);
15301 Voverlay_arrow_position = Qnil;
15302
15303 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
15304 doc: /* String to display as an arrow. See also `overlay-arrow-position'. */);
15305 Voverlay_arrow_string = Qnil;
15306
15307 DEFVAR_INT ("scroll-step", &scroll_step,
15308 doc: /* *The number of lines to try scrolling a window by when point moves out.
15309 If that fails to bring point back on frame, point is centered instead.
15310 If this is zero, point is always centered after it moves off frame.
15311 If you want scrolling to always be a line at a time, you should set
15312 `scroll-conservatively' to a large value rather than set this to 1. */);
15313
15314 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
15315 doc: /* *Scroll up to this many lines, to bring point back on screen.
15316 A value of zero means to scroll the text to center point vertically
15317 in the window. */);
15318 scroll_conservatively = 0;
15319
15320 DEFVAR_INT ("scroll-margin", &scroll_margin,
15321 doc: /* *Number of lines of margin at the top and bottom of a window.
15322 Recenter the window whenever point gets within this many lines
15323 of the top or bottom of the window. */);
15324 scroll_margin = 0;
15325
15326 #if GLYPH_DEBUG
15327 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
15328 #endif
15329
15330 DEFVAR_BOOL ("truncate-partial-width-windows",
15331 &truncate_partial_width_windows,
15332 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
15333 truncate_partial_width_windows = 1;
15334
15335 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
15336 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
15337 Any other value means to use the appropriate face, `mode-line',
15338 `header-line', or `menu' respectively. */);
15339 mode_line_inverse_video = 1;
15340
15341 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
15342 doc: /* *Maximum buffer size for which line number should be displayed.
15343 If the buffer is bigger than this, the line number does not appear
15344 in the mode line. A value of nil means no limit. */);
15345 Vline_number_display_limit = Qnil;
15346
15347 DEFVAR_INT ("line-number-display-limit-width",
15348 &line_number_display_limit_width,
15349 doc: /* *Maximum line width (in characters) for line number display.
15350 If the average length of the lines near point is bigger than this, then the
15351 line number may be omitted from the mode line. */);
15352 line_number_display_limit_width = 200;
15353
15354 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
15355 doc: /* *Non-nil means highlight region even in nonselected windows. */);
15356 highlight_nonselected_windows = 0;
15357
15358 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
15359 doc: /* Non-nil if more than one frame is visible on this display.
15360 Minibuffer-only frames don't count, but iconified frames do.
15361 This variable is not guaranteed to be accurate except while processing
15362 `frame-title-format' and `icon-title-format'. */);
15363
15364 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
15365 doc: /* Template for displaying the title bar of visible frames.
15366 \(Assuming the window manager supports this feature.)
15367 This variable has the same structure as `mode-line-format' (which see),
15368 and is used only on frames for which no explicit name has been set
15369 \(see `modify-frame-parameters'). */);
15370 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
15371 doc: /* Template for displaying the title bar of an iconified frame.
15372 \(Assuming the window manager supports this feature.)
15373 This variable has the same structure as `mode-line-format' (which see),
15374 and is used only on frames for which no explicit name has been set
15375 \(see `modify-frame-parameters'). */);
15376 Vicon_title_format
15377 = Vframe_title_format
15378 = Fcons (intern ("multiple-frames"),
15379 Fcons (build_string ("%b"),
15380 Fcons (Fcons (empty_string,
15381 Fcons (intern ("invocation-name"),
15382 Fcons (build_string ("@"),
15383 Fcons (intern ("system-name"),
15384 Qnil)))),
15385 Qnil)));
15386
15387 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
15388 doc: /* Maximum number of lines to keep in the message log buffer.
15389 If nil, disable message logging. If t, log messages but don't truncate
15390 the buffer when it becomes large. */);
15391 Vmessage_log_max = make_number (50);
15392
15393 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
15394 doc: /* Functions called before redisplay, if window sizes have changed.
15395 The value should be a list of functions that take one argument.
15396 Just before redisplay, for each frame, if any of its windows have changed
15397 size since the last redisplay, or have been split or deleted,
15398 all the functions in the list are called, with the frame as argument. */);
15399 Vwindow_size_change_functions = Qnil;
15400
15401 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
15402 doc: /* List of Functions to call before redisplaying a window with scrolling.
15403 Each function is called with two arguments, the window
15404 and its new display-start position. Note that the value of `window-end'
15405 is not valid when these functions are called. */);
15406 Vwindow_scroll_functions = Qnil;
15407
15408 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
15409 doc: /* *Non-nil means automatically resize tool-bars.
15410 This increases a tool-bar's height if not all tool-bar items are visible.
15411 It decreases a tool-bar's height when it would display blank lines
15412 otherwise. */);
15413 auto_resize_tool_bars_p = 1;
15414
15415 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
15416 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
15417 auto_raise_tool_bar_buttons_p = 1;
15418
15419 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
15420 doc: /* *Margin around tool-bar buttons in pixels.
15421 If an integer, use that for both horizontal and vertical margins.
15422 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
15423 HORZ specifying the horizontal margin, and VERT specifying the
15424 vertical margin. */);
15425 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
15426
15427 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
15428 doc: /* *Relief thickness of tool-bar buttons. */);
15429 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
15430
15431 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
15432 doc: /* List of functions to call to fontify regions of text.
15433 Each function is called with one argument POS. Functions must
15434 fontify a region starting at POS in the current buffer, and give
15435 fontified regions the property `fontified'. */);
15436 Vfontification_functions = Qnil;
15437 Fmake_variable_buffer_local (Qfontification_functions);
15438
15439 DEFVAR_BOOL ("unibyte-display-via-language-environment",
15440 &unibyte_display_via_language_environment,
15441 doc: /* *Non-nil means display unibyte text according to language environment.
15442 Specifically this means that unibyte non-ASCII characters
15443 are displayed by converting them to the equivalent multibyte characters
15444 according to the current language environment. As a result, they are
15445 displayed according to the current fontset. */);
15446 unibyte_display_via_language_environment = 0;
15447
15448 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
15449 doc: /* *Maximum height for resizing mini-windows.
15450 If a float, it specifies a fraction of the mini-window frame's height.
15451 If an integer, it specifies a number of lines. */);
15452 Vmax_mini_window_height = make_float (0.25);
15453
15454 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
15455 doc: /* *How to resize mini-windows.
15456 A value of nil means don't automatically resize mini-windows.
15457 A value of t means resize them to fit the text displayed in them.
15458 A value of `grow-only', the default, means let mini-windows grow
15459 only, until their display becomes empty, at which point the windows
15460 go back to their normal size. */);
15461 Vresize_mini_windows = Qgrow_only;
15462
15463 DEFVAR_BOOL ("cursor-in-non-selected-windows",
15464 &cursor_in_non_selected_windows,
15465 doc: /* *Non-nil means display a hollow cursor in non-selected windows.
15466 nil means don't display a cursor there. */);
15467 cursor_in_non_selected_windows = 1;
15468
15469 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
15470 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
15471 automatic_hscrolling_p = 1;
15472
15473 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
15474 doc: /* *How many columns away from the window edge point is allowed to get
15475 before automatic hscrolling will horizontally scroll the window. */);
15476 hscroll_margin = 5;
15477
15478 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
15479 doc: /* *How many columns to scroll the window when point gets too close to the edge.
15480 When point is less than `automatic-hscroll-margin' columns from the window
15481 edge, automatic hscrolling will scroll the window by the amount of columns
15482 determined by this variable. If its value is a positive integer, scroll that
15483 many columns. If it's a positive floating-point number, it specifies the
15484 fraction of the window's width to scroll. If it's nil or zero, point will be
15485 centered horizontally after the scroll. Any other value, including negative
15486 numbers, are treated as if the value were zero.
15487
15488 Automatic hscrolling always moves point outside the scroll margin, so if
15489 point was more than scroll step columns inside the margin, the window will
15490 scroll more than the value given by the scroll step.
15491
15492 Note that the lower bound for automatic hscrolling specified by `scroll-left'
15493 and `scroll-right' overrides this variable's effect. */);
15494 Vhscroll_step = make_number (0);
15495
15496 DEFVAR_LISP ("image-types", &Vimage_types,
15497 doc: /* List of supported image types.
15498 Each element of the list is a symbol for a supported image type. */);
15499 Vimage_types = Qnil;
15500
15501 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
15502 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
15503 Bind this around calls to `message' to let it take effect. */);
15504 message_truncate_lines = 0;
15505
15506 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
15507 doc: /* Normal hook run for clicks on menu bar, before displaying a submenu.
15508 Can be used to update submenus whose contents should vary. */);
15509 Vmenu_bar_update_hook = Qnil;
15510
15511 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
15512 doc: /* Non-nil means don't update menu bars. Internal use only. */);
15513 inhibit_menubar_update = 0;
15514
15515 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
15516 doc: /* Non-nil means don't eval Lisp during redisplay. */);
15517 inhibit_eval_during_redisplay = 0;
15518
15519 #if GLYPH_DEBUG
15520 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
15521 doc: /* Inhibit try_window_id display optimization. */);
15522 inhibit_try_window_id = 0;
15523
15524 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
15525 doc: /* Inhibit try_window_reusing display optimization. */);
15526 inhibit_try_window_reusing = 0;
15527
15528 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
15529 doc: /* Inhibit try_cursor_movement display optimization. */);
15530 inhibit_try_cursor_movement = 0;
15531 #endif /* GLYPH_DEBUG */
15532 }
15533
15534
15535 /* Initialize this module when Emacs starts. */
15536
15537 void
15538 init_xdisp ()
15539 {
15540 Lisp_Object root_window;
15541 struct window *mini_w;
15542
15543 current_header_line_height = current_mode_line_height = -1;
15544
15545 CHARPOS (this_line_start_pos) = 0;
15546
15547 mini_w = XWINDOW (minibuf_window);
15548 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
15549
15550 if (!noninteractive)
15551 {
15552 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
15553 int i;
15554
15555 XWINDOW (root_window)->top = make_number (FRAME_TOP_MARGIN (f));
15556 set_window_height (root_window,
15557 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
15558 0);
15559 mini_w->top = make_number (FRAME_HEIGHT (f) - 1);
15560 set_window_height (minibuf_window, 1, 0);
15561
15562 XWINDOW (root_window)->width = make_number (FRAME_WIDTH (f));
15563 mini_w->width = make_number (FRAME_WIDTH (f));
15564
15565 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
15566 scratch_glyph_row.glyphs[TEXT_AREA + 1]
15567 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
15568
15569 /* The default ellipsis glyphs `...'. */
15570 for (i = 0; i < 3; ++i)
15571 default_invis_vector[i] = make_number ('.');
15572 }
15573
15574 {
15575 /* Allocate the buffer for frame titles.
15576 Also used for `format-mode-line'. */
15577 int size = 100;
15578 frame_title_buf = (char *) xmalloc (size);
15579 frame_title_buf_end = frame_title_buf + size;
15580 frame_title_ptr = NULL;
15581 }
15582
15583 help_echo_showing_p = 0;
15584 }
15585
15586