]> code.delx.au - gnu-emacs/blob - src/xdisp.c
(update_compositions): Fix type error.
[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
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 "character.h"
180 #include "charset.h"
181 #include "indent.h"
182 #include "commands.h"
183 #include "macros.h"
184 #include "disptab.h"
185 #include "termhooks.h"
186 #include "intervals.h"
187 #include "coding.h"
188 #include "process.h"
189 #include "region-cache.h"
190 #include "fontset.h"
191
192 #ifdef HAVE_X_WINDOWS
193 #include "xterm.h"
194 #endif
195 #ifdef WINDOWSNT
196 #include "w32term.h"
197 #endif
198 #ifdef macintosh
199 #include "macterm.h"
200 #endif
201
202 #define INFINITY 10000000
203
204 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
205 extern void set_frame_menubar P_ ((struct frame *f, int, int));
206 extern int pending_menu_activation;
207 #endif
208
209 extern int interrupt_input;
210 extern int command_loop_level;
211
212 extern int minibuffer_auto_raise;
213
214 extern Lisp_Object Qface;
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 /* Functions called to fontify regions of text. */
231
232 Lisp_Object Vfontification_functions;
233 Lisp_Object Qfontification_functions;
234
235 /* Non-zero means draw tool bar buttons raised when the mouse moves
236 over them. */
237
238 int auto_raise_tool_bar_buttons_p;
239
240 /* Margin around tool bar buttons in pixels. */
241
242 Lisp_Object Vtool_bar_button_margin;
243
244 /* Thickness of shadow to draw around tool bar buttons. */
245
246 int tool_bar_button_relief;
247
248 /* Non-zero means automatically resize tool-bars so that all tool-bar
249 items are visible, and no blank lines remain. */
250
251 int auto_resize_tool_bars_p;
252
253 /* Non-nil means don't actually do any redisplay. */
254
255 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
256
257 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
258
259 int inhibit_eval_during_redisplay;
260
261 /* Names of text properties relevant for redisplay. */
262
263 Lisp_Object Qdisplay, Qrelative_width, Qalign_to;
264 extern Lisp_Object Qface, Qinvisible, Qwidth;
265
266 /* Symbols used in text property values. */
267
268 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
269 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
270 Lisp_Object Qmargin;
271 extern Lisp_Object Qheight;
272
273 /* Non-nil means highlight trailing whitespace. */
274
275 Lisp_Object Vshow_trailing_whitespace;
276
277 /* Name of the face used to highlight trailing whitespace. */
278
279 Lisp_Object Qtrailing_whitespace;
280
281 /* The symbol `image' which is the car of the lists used to represent
282 images in Lisp. */
283
284 Lisp_Object Qimage;
285
286 /* Non-zero means print newline to stdout before next mini-buffer
287 message. */
288
289 int noninteractive_need_newline;
290
291 /* Non-zero means print newline to message log before next message. */
292
293 static int message_log_need_newline;
294
295 /* Three markers that message_dolog uses.
296 It could allocate them itself, but that causes trouble
297 in handling memory-full errors. */
298 static Lisp_Object message_dolog_marker1;
299 static Lisp_Object message_dolog_marker2;
300 static Lisp_Object message_dolog_marker3;
301 \f
302 /* The buffer position of the first character appearing entirely or
303 partially on the line of the selected window which contains the
304 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
305 redisplay optimization in redisplay_internal. */
306
307 static struct text_pos this_line_start_pos;
308
309 /* Number of characters past the end of the line above, including the
310 terminating newline. */
311
312 static struct text_pos this_line_end_pos;
313
314 /* The vertical positions and the height of this line. */
315
316 static int this_line_vpos;
317 static int this_line_y;
318 static int this_line_pixel_height;
319
320 /* X position at which this display line starts. Usually zero;
321 negative if first character is partially visible. */
322
323 static int this_line_start_x;
324
325 /* Buffer that this_line_.* variables are referring to. */
326
327 static struct buffer *this_line_buffer;
328
329 /* Nonzero means truncate lines in all windows less wide than the
330 frame. */
331
332 int truncate_partial_width_windows;
333
334 /* A flag to control how to display unibyte 8-bit character. */
335
336 int unibyte_display_via_language_environment;
337
338 /* Nonzero means we have more than one non-mini-buffer-only frame.
339 Not guaranteed to be accurate except while parsing
340 frame-title-format. */
341
342 int multiple_frames;
343
344 Lisp_Object Vglobal_mode_string;
345
346 /* Marker for where to display an arrow on top of the buffer text. */
347
348 Lisp_Object Voverlay_arrow_position;
349
350 /* String to display for the arrow. Only used on terminal frames. */
351
352 Lisp_Object Voverlay_arrow_string;
353
354 /* Values of those variables at last redisplay. However, if
355 Voverlay_arrow_position is a marker, last_arrow_position is its
356 numerical position. */
357
358 static Lisp_Object last_arrow_position, last_arrow_string;
359
360 /* Like mode-line-format, but for the title bar on a visible frame. */
361
362 Lisp_Object Vframe_title_format;
363
364 /* Like mode-line-format, but for the title bar on an iconified frame. */
365
366 Lisp_Object Vicon_title_format;
367
368 /* List of functions to call when a window's size changes. These
369 functions get one arg, a frame on which one or more windows' sizes
370 have changed. */
371
372 static Lisp_Object Vwindow_size_change_functions;
373
374 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
375
376 /* Nonzero if overlay arrow has been displayed once in this window. */
377
378 static int overlay_arrow_seen;
379
380 /* Nonzero means highlight the region even in nonselected windows. */
381
382 int highlight_nonselected_windows;
383
384 /* If cursor motion alone moves point off frame, try scrolling this
385 many lines up or down if that will bring it back. */
386
387 static int scroll_step;
388
389 /* Nonzero means scroll just far enough to bring point back on the
390 screen, when appropriate. */
391
392 static int scroll_conservatively;
393
394 /* Recenter the window whenever point gets within this many lines of
395 the top or bottom of the window. This value is translated into a
396 pixel value by multiplying it with CANON_Y_UNIT, which means that
397 there is really a fixed pixel height scroll margin. */
398
399 int scroll_margin;
400
401 /* Number of windows showing the buffer of the selected window (or
402 another buffer with the same base buffer). keyboard.c refers to
403 this. */
404
405 int buffer_shared;
406
407 /* Vector containing glyphs for an ellipsis `...'. */
408
409 static Lisp_Object default_invis_vector[3];
410
411 /* Zero means display the mode-line/header-line/menu-bar in the default face
412 (this slightly odd definition is for compatibility with previous versions
413 of emacs), non-zero means display them using their respective faces.
414
415 This variable is deprecated. */
416
417 int mode_line_inverse_video;
418
419 /* Prompt to display in front of the mini-buffer contents. */
420
421 Lisp_Object minibuf_prompt;
422
423 /* Width of current mini-buffer prompt. Only set after display_line
424 of the line that contains the prompt. */
425
426 int minibuf_prompt_width;
427 int minibuf_prompt_pixel_width;
428
429 /* This is the window where the echo area message was displayed. It
430 is always a mini-buffer window, but it may not be the same window
431 currently active as a mini-buffer. */
432
433 Lisp_Object echo_area_window;
434
435 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
436 pushes the current message and the value of
437 message_enable_multibyte on the stack, the function restore_message
438 pops the stack and displays MESSAGE again. */
439
440 Lisp_Object Vmessage_stack;
441
442 /* Nonzero means multibyte characters were enabled when the echo area
443 message was specified. */
444
445 int message_enable_multibyte;
446
447 /* Nonzero if we should redraw the mode lines on the next redisplay. */
448
449 int update_mode_lines;
450
451 /* Nonzero if window sizes or contents have changed since last
452 redisplay that finished. */
453
454 int windows_or_buffers_changed;
455
456 /* Nonzero after display_mode_line if %l was used and it displayed a
457 line number. */
458
459 int line_number_displayed;
460
461 /* Maximum buffer size for which to display line numbers. */
462
463 Lisp_Object Vline_number_display_limit;
464
465 /* Line width to consider when repositioning for line number display. */
466
467 static int line_number_display_limit_width;
468
469 /* Number of lines to keep in the message log buffer. t means
470 infinite. nil means don't log at all. */
471
472 Lisp_Object Vmessage_log_max;
473
474 /* The name of the *Messages* buffer, a string. */
475
476 static Lisp_Object Vmessages_buffer_name;
477
478 /* Current, index 0, and last displayed echo area message. Either
479 buffers from echo_buffers, or nil to indicate no message. */
480
481 Lisp_Object echo_area_buffer[2];
482
483 /* The buffers referenced from echo_area_buffer. */
484
485 static Lisp_Object echo_buffer[2];
486
487 /* A vector saved used in with_area_buffer to reduce consing. */
488
489 static Lisp_Object Vwith_echo_area_save_vector;
490
491 /* Non-zero means display_echo_area should display the last echo area
492 message again. Set by redisplay_preserve_echo_area. */
493
494 static int display_last_displayed_message_p;
495
496 /* Nonzero if echo area is being used by print; zero if being used by
497 message. */
498
499 int message_buf_print;
500
501 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
502
503 Lisp_Object Qinhibit_menubar_update;
504 int inhibit_menubar_update;
505
506 /* Maximum height for resizing mini-windows. Either a float
507 specifying a fraction of the available height, or an integer
508 specifying a number of lines. */
509
510 Lisp_Object Vmax_mini_window_height;
511
512 /* Non-zero means messages should be displayed with truncated
513 lines instead of being continued. */
514
515 int message_truncate_lines;
516 Lisp_Object Qmessage_truncate_lines;
517
518 /* Set to 1 in clear_message to make redisplay_internal aware
519 of an emptied echo area. */
520
521 static int message_cleared_p;
522
523 /* Non-zero means we want a hollow cursor in windows that are not
524 selected. Zero means there's no cursor in such windows. */
525
526 int cursor_in_non_selected_windows;
527 Lisp_Object Qcursor_in_non_selected_windows;
528
529 /* A scratch glyph row with contents used for generating truncation
530 glyphs. Also used in direct_output_for_insert. */
531
532 #define MAX_SCRATCH_GLYPHS 100
533 struct glyph_row scratch_glyph_row;
534 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
535
536 /* Ascent and height of the last line processed by move_it_to. */
537
538 static int last_max_ascent, last_height;
539
540 /* Non-zero if there's a help-echo in the echo area. */
541
542 int help_echo_showing_p;
543
544 /* If >= 0, computed, exact values of mode-line and header-line height
545 to use in the macros CURRENT_MODE_LINE_HEIGHT and
546 CURRENT_HEADER_LINE_HEIGHT. */
547
548 int current_mode_line_height, current_header_line_height;
549
550 /* The maximum distance to look ahead for text properties. Values
551 that are too small let us call compute_char_face and similar
552 functions too often which is expensive. Values that are too large
553 let us call compute_char_face and alike too often because we
554 might not be interested in text properties that far away. */
555
556 #define TEXT_PROP_DISTANCE_LIMIT 100
557
558 #if GLYPH_DEBUG
559
560 /* Variables to turn off display optimizations from Lisp. */
561
562 int inhibit_try_window_id, inhibit_try_window_reusing;
563 int inhibit_try_cursor_movement;
564
565 /* Non-zero means print traces of redisplay if compiled with
566 GLYPH_DEBUG != 0. */
567
568 int trace_redisplay_p;
569
570 #endif /* GLYPH_DEBUG */
571
572 #ifdef DEBUG_TRACE_MOVE
573 /* Non-zero means trace with TRACE_MOVE to stderr. */
574 int trace_move;
575
576 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
577 #else
578 #define TRACE_MOVE(x) (void) 0
579 #endif
580
581 /* Non-zero means automatically scroll windows horizontally to make
582 point visible. */
583
584 int automatic_hscrolling_p;
585
586 /* How close to the margin can point get before the window is scrolled
587 horizontally. */
588 int automatic_hscroll_margin;
589
590 /* How much to scroll horizontally when point is inside the above margin. */
591 Lisp_Object Vautomatic_hscroll_step;
592
593 /* A list of symbols, one for each supported image type. */
594
595 Lisp_Object Vimage_types;
596
597 /* The variable `resize-mini-windows'. If nil, don't resize
598 mini-windows. If t, always resize them to fit the text they
599 display. If `grow-only', let mini-windows grow only until they
600 become empty. */
601
602 Lisp_Object Vresize_mini_windows;
603
604 /* Buffer being redisplayed -- for redisplay_window_error. */
605
606 struct buffer *displayed_buffer;
607
608 /* Value returned from text property handlers (see below). */
609
610 enum prop_handled
611 {
612 HANDLED_NORMALLY,
613 HANDLED_RECOMPUTE_PROPS,
614 HANDLED_OVERLAY_STRING_CONSUMED,
615 HANDLED_RETURN
616 };
617
618 /* A description of text properties that redisplay is interested
619 in. */
620
621 struct props
622 {
623 /* The name of the property. */
624 Lisp_Object *name;
625
626 /* A unique index for the property. */
627 enum prop_idx idx;
628
629 /* A handler function called to set up iterator IT from the property
630 at IT's current position. Value is used to steer handle_stop. */
631 enum prop_handled (*handler) P_ ((struct it *it));
632 };
633
634 static enum prop_handled handle_face_prop P_ ((struct it *));
635 static enum prop_handled handle_invisible_prop P_ ((struct it *));
636 static enum prop_handled handle_display_prop P_ ((struct it *));
637 static enum prop_handled handle_composition_prop P_ ((struct it *));
638 static enum prop_handled handle_overlay_change P_ ((struct it *));
639 static enum prop_handled handle_fontified_prop P_ ((struct it *));
640 static enum prop_handled handle_auto_composed_prop P_ ((struct it *));
641
642 /* Properties handled by iterators. */
643
644 static struct props it_props[] =
645 {
646 {&Qauto_composed, AUTO_COMPOSED_PROP_IDX, handle_auto_composed_prop},
647 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
648 /* Handle `face' before `display' because some sub-properties of
649 `display' need to know the face. */
650 {&Qface, FACE_PROP_IDX, handle_face_prop},
651 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
652 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
653 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
654 {NULL, 0, NULL}
655 };
656
657 /* Value is the position described by X. If X is a marker, value is
658 the marker_position of X. Otherwise, value is X. */
659
660 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
661
662 /* Enumeration returned by some move_it_.* functions internally. */
663
664 enum move_it_result
665 {
666 /* Not used. Undefined value. */
667 MOVE_UNDEFINED,
668
669 /* Move ended at the requested buffer position or ZV. */
670 MOVE_POS_MATCH_OR_ZV,
671
672 /* Move ended at the requested X pixel position. */
673 MOVE_X_REACHED,
674
675 /* Move within a line ended at the end of a line that must be
676 continued. */
677 MOVE_LINE_CONTINUED,
678
679 /* Move within a line ended at the end of a line that would
680 be displayed truncated. */
681 MOVE_LINE_TRUNCATED,
682
683 /* Move within a line ended at a line end. */
684 MOVE_NEWLINE_OR_CR
685 };
686
687
688 \f
689 /* Function prototypes. */
690
691 static void setup_for_ellipsis P_ ((struct it *));
692 static void mark_window_display_accurate_1 P_ ((struct window *, int));
693 static int single_display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
694 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
695 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
696 static int redisplay_mode_lines P_ ((Lisp_Object, int));
697 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
698
699 #if 0
700 static int invisible_text_between_p P_ ((struct it *, int, int));
701 #endif
702
703 static int next_element_from_ellipsis P_ ((struct it *));
704 static void pint2str P_ ((char *, int, int));
705 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
706 struct text_pos));
707 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
708 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
709 static void store_frame_title_char P_ ((char));
710 static int store_frame_title P_ ((unsigned char *, int, int));
711 static void x_consider_frame_title P_ ((Lisp_Object));
712 static void handle_stop P_ ((struct it *));
713 static int tool_bar_lines_needed P_ ((struct frame *));
714 static int single_display_prop_intangible_p P_ ((Lisp_Object));
715 static void ensure_echo_area_buffers P_ ((void));
716 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
717 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
718 static int with_echo_area_buffer P_ ((struct window *, int,
719 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
720 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
721 static void clear_garbaged_frames P_ ((void));
722 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
723 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
724 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
725 static int display_echo_area P_ ((struct window *));
726 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
727 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
728 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
729 static int string_char_and_length P_ ((unsigned char *, int, int *));
730 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
731 struct text_pos));
732 static int compute_window_start_on_continuation_line P_ ((struct window *));
733 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
734 static void insert_left_trunc_glyphs P_ ((struct it *));
735 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
736 static void extend_face_to_end_of_line P_ ((struct it *));
737 static int append_space P_ ((struct it *, int));
738 static int make_cursor_line_fully_visible P_ ((struct window *));
739 static int try_scrolling P_ ((Lisp_Object, int, int, int, int));
740 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
741 static int trailing_whitespace_p P_ ((int));
742 static int message_log_check_duplicate P_ ((int, int, int, int));
743 static void push_it P_ ((struct it *));
744 static void pop_it P_ ((struct it *));
745 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
746 static void redisplay_internal P_ ((int));
747 static int echo_area_display P_ ((int));
748 static void redisplay_windows P_ ((Lisp_Object));
749 static void redisplay_window P_ ((Lisp_Object, int));
750 static Lisp_Object redisplay_window_error ();
751 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
752 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
753 static void update_menu_bar P_ ((struct frame *, int));
754 static int try_window_reusing_current_matrix P_ ((struct window *));
755 static int try_window_id P_ ((struct window *));
756 static int display_line P_ ((struct it *));
757 static int display_mode_lines P_ ((struct window *));
758 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
759 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object));
760 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
761 static void display_menu_bar P_ ((struct window *));
762 static int display_count_lines P_ ((int, int, int, int, int *));
763 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
764 int, int, struct it *, int, int, int, int));
765 static void compute_line_metrics P_ ((struct it *));
766 static void run_redisplay_end_trigger_hook P_ ((struct it *));
767 static int get_overlay_strings P_ ((struct it *, int));
768 static void next_overlay_string P_ ((struct it *));
769 static void reseat P_ ((struct it *, struct text_pos, int));
770 static void reseat_1 P_ ((struct it *, struct text_pos, int));
771 static void back_to_previous_visible_line_start P_ ((struct it *));
772 static void reseat_at_previous_visible_line_start P_ ((struct it *));
773 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
774 static int next_element_from_display_vector P_ ((struct it *));
775 static int next_element_from_string P_ ((struct it *));
776 static int next_element_from_c_string P_ ((struct it *));
777 static int next_element_from_buffer P_ ((struct it *));
778 static int next_element_from_composition P_ ((struct it *));
779 static int next_element_from_image P_ ((struct it *));
780 static int next_element_from_stretch P_ ((struct it *));
781 static void load_overlay_strings P_ ((struct it *, int));
782 static int init_from_display_pos P_ ((struct it *, struct window *,
783 struct display_pos *));
784 static void reseat_to_string P_ ((struct it *, unsigned char *,
785 Lisp_Object, int, int, int, int));
786 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
787 int, int, int));
788 void move_it_vertically_backward P_ ((struct it *, int));
789 static void init_to_row_start P_ ((struct it *, struct window *,
790 struct glyph_row *));
791 static int init_to_row_end P_ ((struct it *, struct window *,
792 struct glyph_row *));
793 static void back_to_previous_line_start P_ ((struct it *));
794 static int forward_to_next_line_start P_ ((struct it *, int *));
795 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
796 Lisp_Object, int));
797 static struct text_pos string_pos P_ ((int, Lisp_Object));
798 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
799 static int number_of_chars P_ ((unsigned char *, int));
800 static void compute_stop_pos P_ ((struct it *));
801 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
802 Lisp_Object));
803 static int face_before_or_after_it_pos P_ ((struct it *, int));
804 static int next_overlay_change P_ ((int));
805 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
806 Lisp_Object, struct text_pos *,
807 int));
808 static int underlying_face_id P_ ((struct it *));
809 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
810 struct window *));
811
812 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
813 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
814
815 #ifdef HAVE_WINDOW_SYSTEM
816
817 static void update_tool_bar P_ ((struct frame *, int));
818 static void build_desired_tool_bar_string P_ ((struct frame *f));
819 static int redisplay_tool_bar P_ ((struct frame *));
820 static void display_tool_bar_line P_ ((struct it *));
821
822 #endif /* HAVE_WINDOW_SYSTEM */
823
824 \f
825 /***********************************************************************
826 Window display dimensions
827 ***********************************************************************/
828
829 /* Return the window-relative maximum y + 1 for glyph rows displaying
830 text in window W. This is the height of W minus the height of a
831 mode line, if any. */
832
833 INLINE int
834 window_text_bottom_y (w)
835 struct window *w;
836 {
837 struct frame *f = XFRAME (w->frame);
838 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
839
840 if (WINDOW_WANTS_MODELINE_P (w))
841 height -= CURRENT_MODE_LINE_HEIGHT (w);
842 return height;
843 }
844
845
846 /* Return the pixel width of display area AREA of window W. AREA < 0
847 means return the total width of W, not including fringes to
848 the left and right of the window. */
849
850 INLINE int
851 window_box_width (w, area)
852 struct window *w;
853 int area;
854 {
855 struct frame *f = XFRAME (w->frame);
856 int width = XFASTINT (w->width);
857
858 if (!w->pseudo_window_p)
859 {
860 width -= FRAME_SCROLL_BAR_WIDTH (f) + FRAME_FRINGE_COLS (f);
861
862 if (area == TEXT_AREA)
863 {
864 if (INTEGERP (w->left_margin_width))
865 width -= XFASTINT (w->left_margin_width);
866 if (INTEGERP (w->right_margin_width))
867 width -= XFASTINT (w->right_margin_width);
868 }
869 else if (area == LEFT_MARGIN_AREA)
870 width = (INTEGERP (w->left_margin_width)
871 ? XFASTINT (w->left_margin_width) : 0);
872 else if (area == RIGHT_MARGIN_AREA)
873 width = (INTEGERP (w->right_margin_width)
874 ? XFASTINT (w->right_margin_width) : 0);
875 }
876
877 return width * CANON_X_UNIT (f);
878 }
879
880
881 /* Return the pixel height of the display area of window W, not
882 including mode lines of W, if any. */
883
884 INLINE int
885 window_box_height (w)
886 struct window *w;
887 {
888 struct frame *f = XFRAME (w->frame);
889 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
890
891 xassert (height >= 0);
892
893 /* Note: the code below that determines the mode-line/header-line
894 height is essentially the same as that contained in the macro
895 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
896 the appropriate glyph row has its `mode_line_p' flag set,
897 and if it doesn't, uses estimate_mode_line_height instead. */
898
899 if (WINDOW_WANTS_MODELINE_P (w))
900 {
901 struct glyph_row *ml_row
902 = (w->current_matrix && w->current_matrix->rows
903 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
904 : 0);
905 if (ml_row && ml_row->mode_line_p)
906 height -= ml_row->height;
907 else
908 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
909 }
910
911 if (WINDOW_WANTS_HEADER_LINE_P (w))
912 {
913 struct glyph_row *hl_row
914 = (w->current_matrix && w->current_matrix->rows
915 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
916 : 0);
917 if (hl_row && hl_row->mode_line_p)
918 height -= hl_row->height;
919 else
920 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
921 }
922
923 /* With a very small font and a mode-line that's taller than
924 default, we might end up with a negative height. */
925 return max (0, height);
926 }
927
928
929 /* Return the frame-relative coordinate of the left edge of display
930 area AREA of window W. AREA < 0 means return the left edge of the
931 whole window, to the right of the left fringe of W. */
932
933 INLINE int
934 window_box_left (w, area)
935 struct window *w;
936 int area;
937 {
938 struct frame *f = XFRAME (w->frame);
939 int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
940
941 if (!w->pseudo_window_p)
942 {
943 x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
944 + FRAME_LEFT_FRINGE_WIDTH (f));
945
946 if (area == TEXT_AREA)
947 x += window_box_width (w, LEFT_MARGIN_AREA);
948 else if (area == RIGHT_MARGIN_AREA)
949 x += (window_box_width (w, LEFT_MARGIN_AREA)
950 + window_box_width (w, TEXT_AREA));
951 }
952
953 return x;
954 }
955
956
957 /* Return the frame-relative coordinate of the right edge of display
958 area AREA of window W. AREA < 0 means return the left edge of the
959 whole window, to the left of the right fringe of W. */
960
961 INLINE int
962 window_box_right (w, area)
963 struct window *w;
964 int area;
965 {
966 return window_box_left (w, area) + window_box_width (w, area);
967 }
968
969
970 /* Get the bounding box of the display area AREA of window W, without
971 mode lines, in frame-relative coordinates. AREA < 0 means the
972 whole window, not including the left and right fringes of
973 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
974 coordinates of the upper-left corner of the box. Return in
975 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
976
977 INLINE void
978 window_box (w, area, box_x, box_y, box_width, box_height)
979 struct window *w;
980 int area;
981 int *box_x, *box_y, *box_width, *box_height;
982 {
983 struct frame *f = XFRAME (w->frame);
984
985 *box_width = window_box_width (w, area);
986 *box_height = window_box_height (w);
987 *box_x = window_box_left (w, area);
988 *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
989 + XFASTINT (w->top) * CANON_Y_UNIT (f));
990 if (WINDOW_WANTS_HEADER_LINE_P (w))
991 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
992 }
993
994
995 /* Get the bounding box of the display area AREA of window W, without
996 mode lines. AREA < 0 means the whole window, not including the
997 left and right fringe of the window. Return in *TOP_LEFT_X
998 and TOP_LEFT_Y the frame-relative pixel coordinates of the
999 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1000 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1001 box. */
1002
1003 INLINE void
1004 window_box_edges (w, area, top_left_x, top_left_y,
1005 bottom_right_x, bottom_right_y)
1006 struct window *w;
1007 int area;
1008 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1009 {
1010 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1011 bottom_right_y);
1012 *bottom_right_x += *top_left_x;
1013 *bottom_right_y += *top_left_y;
1014 }
1015
1016
1017 \f
1018 /***********************************************************************
1019 Utilities
1020 ***********************************************************************/
1021
1022 /* Return the bottom y-position of the line the iterator IT is in.
1023 This can modify IT's settings. */
1024
1025 int
1026 line_bottom_y (it)
1027 struct it *it;
1028 {
1029 int line_height = it->max_ascent + it->max_descent;
1030 int line_top_y = it->current_y;
1031
1032 if (line_height == 0)
1033 {
1034 if (last_height)
1035 line_height = last_height;
1036 else if (IT_CHARPOS (*it) < ZV)
1037 {
1038 move_it_by_lines (it, 1, 1);
1039 line_height = (it->max_ascent || it->max_descent
1040 ? it->max_ascent + it->max_descent
1041 : last_height);
1042 }
1043 else
1044 {
1045 struct glyph_row *row = it->glyph_row;
1046
1047 /* Use the default character height. */
1048 it->glyph_row = NULL;
1049 it->what = IT_CHARACTER;
1050 it->c = ' ';
1051 it->len = 1;
1052 PRODUCE_GLYPHS (it);
1053 line_height = it->ascent + it->descent;
1054 it->glyph_row = row;
1055 }
1056 }
1057
1058 return line_top_y + line_height;
1059 }
1060
1061
1062 /* Return 1 if position CHARPOS is visible in window W. Set *FULLY to
1063 1 if POS is visible and the line containing POS is fully visible.
1064 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1065 and header-lines heights. */
1066
1067 int
1068 pos_visible_p (w, charpos, fully, exact_mode_line_heights_p)
1069 struct window *w;
1070 int charpos, *fully, exact_mode_line_heights_p;
1071 {
1072 struct it it;
1073 struct text_pos top;
1074 int visible_p;
1075 struct buffer *old_buffer = NULL;
1076
1077 if (XBUFFER (w->buffer) != current_buffer)
1078 {
1079 old_buffer = current_buffer;
1080 set_buffer_internal_1 (XBUFFER (w->buffer));
1081 }
1082
1083 *fully = visible_p = 0;
1084 SET_TEXT_POS_FROM_MARKER (top, w->start);
1085
1086 /* Compute exact mode line heights, if requested. */
1087 if (exact_mode_line_heights_p)
1088 {
1089 if (WINDOW_WANTS_MODELINE_P (w))
1090 current_mode_line_height
1091 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1092 current_buffer->mode_line_format);
1093
1094 if (WINDOW_WANTS_HEADER_LINE_P (w))
1095 current_header_line_height
1096 = display_mode_line (w, HEADER_LINE_FACE_ID,
1097 current_buffer->header_line_format);
1098 }
1099
1100 start_display (&it, w, top);
1101 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
1102 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
1103
1104 /* Note that we may overshoot because of invisible text. */
1105 if (IT_CHARPOS (it) >= charpos)
1106 {
1107 int top_y = it.current_y;
1108 int bottom_y = line_bottom_y (&it);
1109 int window_top_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
1110
1111 if (top_y < window_top_y)
1112 visible_p = bottom_y > window_top_y;
1113 else if (top_y < it.last_visible_y)
1114 {
1115 visible_p = 1;
1116 *fully = bottom_y <= it.last_visible_y;
1117 }
1118 }
1119 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1120 {
1121 move_it_by_lines (&it, 1, 0);
1122 if (charpos < IT_CHARPOS (it))
1123 {
1124 visible_p = 1;
1125 *fully = 0;
1126 }
1127 }
1128
1129 if (old_buffer)
1130 set_buffer_internal_1 (old_buffer);
1131
1132 current_header_line_height = current_mode_line_height = -1;
1133 return visible_p;
1134 }
1135
1136
1137 /* Return the next character from STR which is MAXLEN bytes long.
1138 Return in *LEN the length of the character. This is like
1139 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1140 we find one, we return a `?', but with the length of the invalid
1141 character. */
1142
1143 static INLINE int
1144 string_char_and_length (str, maxlen, len)
1145 unsigned char *str;
1146 int maxlen, *len;
1147 {
1148 int c;
1149
1150 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1151 if (!CHAR_VALID_P (c, 1))
1152 /* We may not change the length here because other places in Emacs
1153 don't use this function, i.e. they silently accept invalid
1154 characters. */
1155 c = '?';
1156
1157 return c;
1158 }
1159
1160
1161
1162 /* Given a position POS containing a valid character and byte position
1163 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1164
1165 static struct text_pos
1166 string_pos_nchars_ahead (pos, string, nchars)
1167 struct text_pos pos;
1168 Lisp_Object string;
1169 int nchars;
1170 {
1171 xassert (STRINGP (string) && nchars >= 0);
1172
1173 if (STRING_MULTIBYTE (string))
1174 {
1175 int rest = STRING_BYTES (XSTRING (string)) - BYTEPOS (pos);
1176 unsigned char *p = XSTRING (string)->data + BYTEPOS (pos);
1177 int len;
1178
1179 while (nchars--)
1180 {
1181 string_char_and_length (p, rest, &len);
1182 p += len, rest -= len;
1183 xassert (rest >= 0);
1184 CHARPOS (pos) += 1;
1185 BYTEPOS (pos) += len;
1186 }
1187 }
1188 else
1189 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1190
1191 return pos;
1192 }
1193
1194
1195 /* Value is the text position, i.e. character and byte position,
1196 for character position CHARPOS in STRING. */
1197
1198 static INLINE struct text_pos
1199 string_pos (charpos, string)
1200 int charpos;
1201 Lisp_Object string;
1202 {
1203 struct text_pos pos;
1204 xassert (STRINGP (string));
1205 xassert (charpos >= 0);
1206 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1207 return pos;
1208 }
1209
1210
1211 /* Value is a text position, i.e. character and byte position, for
1212 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1213 means recognize multibyte characters. */
1214
1215 static struct text_pos
1216 c_string_pos (charpos, s, multibyte_p)
1217 int charpos;
1218 unsigned char *s;
1219 int multibyte_p;
1220 {
1221 struct text_pos pos;
1222
1223 xassert (s != NULL);
1224 xassert (charpos >= 0);
1225
1226 if (multibyte_p)
1227 {
1228 int rest = strlen (s), len;
1229
1230 SET_TEXT_POS (pos, 0, 0);
1231 while (charpos--)
1232 {
1233 string_char_and_length (s, rest, &len);
1234 s += len, rest -= len;
1235 xassert (rest >= 0);
1236 CHARPOS (pos) += 1;
1237 BYTEPOS (pos) += len;
1238 }
1239 }
1240 else
1241 SET_TEXT_POS (pos, charpos, charpos);
1242
1243 return pos;
1244 }
1245
1246
1247 /* Value is the number of characters in C string S. MULTIBYTE_P
1248 non-zero means recognize multibyte characters. */
1249
1250 static int
1251 number_of_chars (s, multibyte_p)
1252 unsigned char *s;
1253 int multibyte_p;
1254 {
1255 int nchars;
1256
1257 if (multibyte_p)
1258 {
1259 int rest = strlen (s), len;
1260 unsigned char *p = (unsigned char *) s;
1261
1262 for (nchars = 0; rest > 0; ++nchars)
1263 {
1264 string_char_and_length (p, rest, &len);
1265 rest -= len, p += len;
1266 }
1267 }
1268 else
1269 nchars = strlen (s);
1270
1271 return nchars;
1272 }
1273
1274
1275 /* Compute byte position NEWPOS->bytepos corresponding to
1276 NEWPOS->charpos. POS is a known position in string STRING.
1277 NEWPOS->charpos must be >= POS.charpos. */
1278
1279 static void
1280 compute_string_pos (newpos, pos, string)
1281 struct text_pos *newpos, pos;
1282 Lisp_Object string;
1283 {
1284 xassert (STRINGP (string));
1285 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1286
1287 if (STRING_MULTIBYTE (string))
1288 *newpos = string_pos_nchars_ahead (pos, string,
1289 CHARPOS (*newpos) - CHARPOS (pos));
1290 else
1291 BYTEPOS (*newpos) = CHARPOS (*newpos);
1292 }
1293
1294
1295 \f
1296 /***********************************************************************
1297 Lisp form evaluation
1298 ***********************************************************************/
1299
1300 /* Error handler for safe_eval and safe_call. */
1301
1302 static Lisp_Object
1303 safe_eval_handler (arg)
1304 Lisp_Object arg;
1305 {
1306 add_to_log ("Error during redisplay: %s", arg, Qnil);
1307 return Qnil;
1308 }
1309
1310
1311 /* Evaluate SEXPR and return the result, or nil if something went
1312 wrong. Prevent redisplay during the evaluation. */
1313
1314 Lisp_Object
1315 safe_eval (sexpr)
1316 Lisp_Object sexpr;
1317 {
1318 Lisp_Object val;
1319
1320 if (inhibit_eval_during_redisplay)
1321 val = Qnil;
1322 else
1323 {
1324 int count = BINDING_STACK_SIZE ();
1325 struct gcpro gcpro1;
1326
1327 GCPRO1 (sexpr);
1328 specbind (Qinhibit_redisplay, Qt);
1329 val = internal_condition_case_1 (Feval, sexpr, Qerror,
1330 safe_eval_handler);
1331 UNGCPRO;
1332 val = unbind_to (count, val);
1333 }
1334
1335 return val;
1336 }
1337
1338
1339 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1340 Return the result, or nil if something went wrong. Prevent
1341 redisplay during the evaluation. */
1342
1343 Lisp_Object
1344 safe_call (nargs, args)
1345 int nargs;
1346 Lisp_Object *args;
1347 {
1348 Lisp_Object val;
1349
1350 if (inhibit_eval_during_redisplay)
1351 val = Qnil;
1352 else
1353 {
1354 int count = BINDING_STACK_SIZE ();
1355 struct gcpro gcpro1;
1356
1357 GCPRO1 (args[0]);
1358 gcpro1.nvars = nargs;
1359 specbind (Qinhibit_redisplay, Qt);
1360 val = internal_condition_case_2 (Ffuncall, nargs, args, Qerror,
1361 safe_eval_handler);
1362 UNGCPRO;
1363 val = unbind_to (count, val);
1364 }
1365
1366 return val;
1367 }
1368
1369
1370 /* Call function FN with one argument ARG.
1371 Return the result, or nil if something went wrong. */
1372
1373 Lisp_Object
1374 safe_call1 (fn, arg)
1375 Lisp_Object fn, arg;
1376 {
1377 Lisp_Object args[2];
1378 args[0] = fn;
1379 args[1] = arg;
1380 return safe_call (2, args);
1381 }
1382
1383
1384 \f
1385 /***********************************************************************
1386 Debugging
1387 ***********************************************************************/
1388
1389 #if 0
1390
1391 /* Define CHECK_IT to perform sanity checks on iterators.
1392 This is for debugging. It is too slow to do unconditionally. */
1393
1394 static void
1395 check_it (it)
1396 struct it *it;
1397 {
1398 if (it->method == next_element_from_string)
1399 {
1400 xassert (STRINGP (it->string));
1401 xassert (IT_STRING_CHARPOS (*it) >= 0);
1402 }
1403 else if (it->method == next_element_from_buffer)
1404 {
1405 /* Check that character and byte positions agree. */
1406 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1407 }
1408
1409 if (it->dpvec)
1410 xassert (it->current.dpvec_index >= 0);
1411 else
1412 xassert (it->current.dpvec_index < 0);
1413 }
1414
1415 #define CHECK_IT(IT) check_it ((IT))
1416
1417 #else /* not 0 */
1418
1419 #define CHECK_IT(IT) (void) 0
1420
1421 #endif /* not 0 */
1422
1423
1424 #if GLYPH_DEBUG
1425
1426 /* Check that the window end of window W is what we expect it
1427 to be---the last row in the current matrix displaying text. */
1428
1429 static void
1430 check_window_end (w)
1431 struct window *w;
1432 {
1433 if (!MINI_WINDOW_P (w)
1434 && !NILP (w->window_end_valid))
1435 {
1436 struct glyph_row *row;
1437 xassert ((row = MATRIX_ROW (w->current_matrix,
1438 XFASTINT (w->window_end_vpos)),
1439 !row->enabled_p
1440 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1441 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1442 }
1443 }
1444
1445 #define CHECK_WINDOW_END(W) check_window_end ((W))
1446
1447 #else /* not GLYPH_DEBUG */
1448
1449 #define CHECK_WINDOW_END(W) (void) 0
1450
1451 #endif /* not GLYPH_DEBUG */
1452
1453
1454 \f
1455 /***********************************************************************
1456 Iterator initialization
1457 ***********************************************************************/
1458
1459 /* Initialize IT for displaying current_buffer in window W, starting
1460 at character position CHARPOS. CHARPOS < 0 means that no buffer
1461 position is specified which is useful when the iterator is assigned
1462 a position later. BYTEPOS is the byte position corresponding to
1463 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
1464
1465 If ROW is not null, calls to produce_glyphs with IT as parameter
1466 will produce glyphs in that row.
1467
1468 BASE_FACE_ID is the id of a base face to use. It must be one of
1469 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
1470 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
1471 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
1472
1473 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
1474 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
1475 will be initialized to use the corresponding mode line glyph row of
1476 the desired matrix of W. */
1477
1478 void
1479 init_iterator (it, w, charpos, bytepos, row, base_face_id)
1480 struct it *it;
1481 struct window *w;
1482 int charpos, bytepos;
1483 struct glyph_row *row;
1484 enum face_id base_face_id;
1485 {
1486 int highlight_region_p;
1487
1488 /* Some precondition checks. */
1489 xassert (w != NULL && it != NULL);
1490 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
1491 && charpos <= ZV));
1492
1493 /* If face attributes have been changed since the last redisplay,
1494 free realized faces now because they depend on face definitions
1495 that might have changed. */
1496 if (face_change_count)
1497 {
1498 face_change_count = 0;
1499 free_all_realized_faces (Qnil);
1500 }
1501
1502 /* Use one of the mode line rows of W's desired matrix if
1503 appropriate. */
1504 if (row == NULL)
1505 {
1506 if (base_face_id == MODE_LINE_FACE_ID
1507 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
1508 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
1509 else if (base_face_id == HEADER_LINE_FACE_ID)
1510 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
1511 }
1512
1513 /* Clear IT. */
1514 bzero (it, sizeof *it);
1515 it->current.overlay_string_index = -1;
1516 it->current.dpvec_index = -1;
1517 it->base_face_id = base_face_id;
1518
1519 /* The window in which we iterate over current_buffer: */
1520 XSETWINDOW (it->window, w);
1521 it->w = w;
1522 it->f = XFRAME (w->frame);
1523
1524 /* Extra space between lines (on window systems only). */
1525 if (base_face_id == DEFAULT_FACE_ID
1526 && FRAME_WINDOW_P (it->f))
1527 {
1528 if (NATNUMP (current_buffer->extra_line_spacing))
1529 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
1530 else if (it->f->extra_line_spacing > 0)
1531 it->extra_line_spacing = it->f->extra_line_spacing;
1532 }
1533
1534 /* If realized faces have been removed, e.g. because of face
1535 attribute changes of named faces, recompute them. When running
1536 in batch mode, the face cache of Vterminal_frame is null. If
1537 we happen to get called, make a dummy face cache. */
1538 if (
1539 #ifndef WINDOWSNT
1540 noninteractive &&
1541 #endif
1542 FRAME_FACE_CACHE (it->f) == NULL)
1543 init_frame_faces (it->f);
1544 if (FRAME_FACE_CACHE (it->f)->used == 0)
1545 recompute_basic_faces (it->f);
1546
1547 /* Current value of the `space-width', and 'height' properties. */
1548 it->space_width = Qnil;
1549 it->font_height = Qnil;
1550
1551 /* Are control characters displayed as `^C'? */
1552 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
1553
1554 /* -1 means everything between a CR and the following line end
1555 is invisible. >0 means lines indented more than this value are
1556 invisible. */
1557 it->selective = (INTEGERP (current_buffer->selective_display)
1558 ? XFASTINT (current_buffer->selective_display)
1559 : (!NILP (current_buffer->selective_display)
1560 ? -1 : 0));
1561 it->selective_display_ellipsis_p
1562 = !NILP (current_buffer->selective_display_ellipses);
1563
1564 /* Display table to use. */
1565 it->dp = window_display_table (w);
1566
1567 /* Are multibyte characters enabled in current_buffer? */
1568 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1569
1570 /* Non-zero if we should highlight the region. */
1571 highlight_region_p
1572 = (!NILP (Vtransient_mark_mode)
1573 && !NILP (current_buffer->mark_active)
1574 && XMARKER (current_buffer->mark)->buffer != 0);
1575
1576 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
1577 start and end of a visible region in window IT->w. Set both to
1578 -1 to indicate no region. */
1579 if (highlight_region_p
1580 /* Maybe highlight only in selected window. */
1581 && (/* Either show region everywhere. */
1582 highlight_nonselected_windows
1583 /* Or show region in the selected window. */
1584 || w == XWINDOW (selected_window)
1585 /* Or show the region if we are in the mini-buffer and W is
1586 the window the mini-buffer refers to. */
1587 || (MINI_WINDOW_P (XWINDOW (selected_window))
1588 && WINDOWP (Vminibuf_selected_window)
1589 && w == XWINDOW (Vminibuf_selected_window))))
1590 {
1591 int charpos = marker_position (current_buffer->mark);
1592 it->region_beg_charpos = min (PT, charpos);
1593 it->region_end_charpos = max (PT, charpos);
1594 }
1595 else
1596 it->region_beg_charpos = it->region_end_charpos = -1;
1597
1598 /* Get the position at which the redisplay_end_trigger hook should
1599 be run, if it is to be run at all. */
1600 if (MARKERP (w->redisplay_end_trigger)
1601 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
1602 it->redisplay_end_trigger_charpos
1603 = marker_position (w->redisplay_end_trigger);
1604 else if (INTEGERP (w->redisplay_end_trigger))
1605 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
1606
1607 /* Correct bogus values of tab_width. */
1608 it->tab_width = XINT (current_buffer->tab_width);
1609 if (it->tab_width <= 0 || it->tab_width > 1000)
1610 it->tab_width = 8;
1611
1612 /* Are lines in the display truncated? */
1613 it->truncate_lines_p
1614 = (base_face_id != DEFAULT_FACE_ID
1615 || XINT (it->w->hscroll)
1616 || (truncate_partial_width_windows
1617 && !WINDOW_FULL_WIDTH_P (it->w))
1618 || !NILP (current_buffer->truncate_lines));
1619
1620 /* Get dimensions of truncation and continuation glyphs. These are
1621 displayed as fringe bitmaps under X, so we don't need them for such
1622 frames. */
1623 if (!FRAME_WINDOW_P (it->f))
1624 {
1625 if (it->truncate_lines_p)
1626 {
1627 /* We will need the truncation glyph. */
1628 xassert (it->glyph_row == NULL);
1629 produce_special_glyphs (it, IT_TRUNCATION);
1630 it->truncation_pixel_width = it->pixel_width;
1631 }
1632 else
1633 {
1634 /* We will need the continuation glyph. */
1635 xassert (it->glyph_row == NULL);
1636 produce_special_glyphs (it, IT_CONTINUATION);
1637 it->continuation_pixel_width = it->pixel_width;
1638 }
1639
1640 /* Reset these values to zero because the produce_special_glyphs
1641 above has changed them. */
1642 it->pixel_width = it->ascent = it->descent = 0;
1643 it->phys_ascent = it->phys_descent = 0;
1644 }
1645
1646 /* Set this after getting the dimensions of truncation and
1647 continuation glyphs, so that we don't produce glyphs when calling
1648 produce_special_glyphs, above. */
1649 it->glyph_row = row;
1650 it->area = TEXT_AREA;
1651
1652 /* Get the dimensions of the display area. The display area
1653 consists of the visible window area plus a horizontally scrolled
1654 part to the left of the window. All x-values are relative to the
1655 start of this total display area. */
1656 if (base_face_id != DEFAULT_FACE_ID)
1657 {
1658 /* Mode lines, menu bar in terminal frames. */
1659 it->first_visible_x = 0;
1660 it->last_visible_x = XFASTINT (w->width) * CANON_X_UNIT (it->f);
1661 }
1662 else
1663 {
1664 it->first_visible_x
1665 = XFASTINT (it->w->hscroll) * CANON_X_UNIT (it->f);
1666 it->last_visible_x = (it->first_visible_x
1667 + window_box_width (w, TEXT_AREA));
1668
1669 /* If we truncate lines, leave room for the truncator glyph(s) at
1670 the right margin. Otherwise, leave room for the continuation
1671 glyph(s). Truncation and continuation glyphs are not inserted
1672 for window-based redisplay. */
1673 if (!FRAME_WINDOW_P (it->f))
1674 {
1675 if (it->truncate_lines_p)
1676 it->last_visible_x -= it->truncation_pixel_width;
1677 else
1678 it->last_visible_x -= it->continuation_pixel_width;
1679 }
1680
1681 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
1682 it->current_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w) + w->vscroll;
1683 }
1684
1685 /* Leave room for a border glyph. */
1686 if (!FRAME_WINDOW_P (it->f)
1687 && !WINDOW_RIGHTMOST_P (it->w))
1688 it->last_visible_x -= 1;
1689
1690 it->last_visible_y = window_text_bottom_y (w);
1691
1692 /* For mode lines and alike, arrange for the first glyph having a
1693 left box line if the face specifies a box. */
1694 if (base_face_id != DEFAULT_FACE_ID)
1695 {
1696 struct face *face;
1697
1698 it->face_id = base_face_id;
1699
1700 /* If we have a boxed mode line, make the first character appear
1701 with a left box line. */
1702 face = FACE_FROM_ID (it->f, base_face_id);
1703 if (face->box != FACE_NO_BOX)
1704 it->start_of_box_run_p = 1;
1705 }
1706
1707 /* If a buffer position was specified, set the iterator there,
1708 getting overlays and face properties from that position. */
1709 if (charpos >= BUF_BEG (current_buffer))
1710 {
1711 it->end_charpos = ZV;
1712 it->face_id = -1;
1713 IT_CHARPOS (*it) = charpos;
1714
1715 /* Compute byte position if not specified. */
1716 if (bytepos < charpos)
1717 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
1718 else
1719 IT_BYTEPOS (*it) = bytepos;
1720
1721 /* Compute faces etc. */
1722 reseat (it, it->current.pos, 1);
1723 }
1724
1725 CHECK_IT (it);
1726 }
1727
1728
1729 /* Initialize IT for the display of window W with window start POS. */
1730
1731 void
1732 start_display (it, w, pos)
1733 struct it *it;
1734 struct window *w;
1735 struct text_pos pos;
1736 {
1737 struct glyph_row *row;
1738 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
1739
1740 row = w->desired_matrix->rows + first_vpos;
1741 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
1742
1743 if (!it->truncate_lines_p)
1744 {
1745 int start_at_line_beg_p;
1746 int first_y = it->current_y;
1747
1748 /* If window start is not at a line start, skip forward to POS to
1749 get the correct continuation lines width. */
1750 start_at_line_beg_p = (CHARPOS (pos) == BEGV
1751 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
1752 if (!start_at_line_beg_p)
1753 {
1754 reseat_at_previous_visible_line_start (it);
1755 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
1756
1757 /* If lines are continued, this line may end in the middle
1758 of a multi-glyph character (e.g. a control character
1759 displayed as \003, or in the middle of an overlay
1760 string). In this case move_it_to above will not have
1761 taken us to the start of the continuation line but to the
1762 end of the continued line. */
1763 if (it->current_x > 0)
1764 {
1765 if (it->current.dpvec_index >= 0
1766 || it->current.overlay_string_index >= 0)
1767 {
1768 set_iterator_to_next (it, 1);
1769 move_it_in_display_line_to (it, -1, -1, 0);
1770 }
1771
1772 it->continuation_lines_width += it->current_x;
1773 }
1774
1775 /* We're starting a new display line, not affected by the
1776 height of the continued line, so clear the appropriate
1777 fields in the iterator structure. */
1778 it->max_ascent = it->max_descent = 0;
1779 it->max_phys_ascent = it->max_phys_descent = 0;
1780
1781 it->current_y = first_y;
1782 it->vpos = 0;
1783 it->current_x = it->hpos = 0;
1784 }
1785 }
1786
1787 #if 0 /* Don't assert the following because start_display is sometimes
1788 called intentionally with a window start that is not at a
1789 line start. Please leave this code in as a comment. */
1790
1791 /* Window start should be on a line start, now. */
1792 xassert (it->continuation_lines_width
1793 || IT_CHARPOS (it) == BEGV
1794 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
1795 #endif /* 0 */
1796 }
1797
1798
1799 /* Return 1 if POS is a position in ellipses displayed for invisible
1800 text. W is the window we display, for text property lookup. */
1801
1802 static int
1803 in_ellipses_for_invisible_text_p (pos, w)
1804 struct display_pos *pos;
1805 struct window *w;
1806 {
1807 Lisp_Object prop, window;
1808 int ellipses_p = 0;
1809 int charpos = CHARPOS (pos->pos);
1810
1811 /* If POS specifies a position in a display vector, this might
1812 be for an ellipsis displayed for invisible text. We won't
1813 get the iterator set up for delivering that ellipsis unless
1814 we make sure that it gets aware of the invisible text. */
1815 if (pos->dpvec_index >= 0
1816 && pos->overlay_string_index < 0
1817 && CHARPOS (pos->string_pos) < 0
1818 && charpos > BEGV
1819 && (XSETWINDOW (window, w),
1820 prop = Fget_char_property (make_number (charpos),
1821 Qinvisible, window),
1822 !TEXT_PROP_MEANS_INVISIBLE (prop)))
1823 {
1824 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
1825 window);
1826 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
1827 }
1828
1829 return ellipses_p;
1830 }
1831
1832
1833 /* Initialize IT for stepping through current_buffer in window W,
1834 starting at position POS that includes overlay string and display
1835 vector/ control character translation position information. Value
1836 is zero if there are overlay strings with newlines at POS. */
1837
1838 static int
1839 init_from_display_pos (it, w, pos)
1840 struct it *it;
1841 struct window *w;
1842 struct display_pos *pos;
1843 {
1844 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
1845 int i, overlay_strings_with_newlines = 0;
1846
1847 /* If POS specifies a position in a display vector, this might
1848 be for an ellipsis displayed for invisible text. We won't
1849 get the iterator set up for delivering that ellipsis unless
1850 we make sure that it gets aware of the invisible text. */
1851 if (in_ellipses_for_invisible_text_p (pos, w))
1852 {
1853 --charpos;
1854 bytepos = 0;
1855 }
1856
1857 /* Keep in mind: the call to reseat in init_iterator skips invisible
1858 text, so we might end up at a position different from POS. This
1859 is only a problem when POS is a row start after a newline and an
1860 overlay starts there with an after-string, and the overlay has an
1861 invisible property. Since we don't skip invisible text in
1862 display_line and elsewhere immediately after consuming the
1863 newline before the row start, such a POS will not be in a string,
1864 but the call to init_iterator below will move us to the
1865 after-string. */
1866 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
1867
1868 for (i = 0; i < it->n_overlay_strings; ++i)
1869 {
1870 char *s = XSTRING (it->overlay_strings[i])->data;
1871 char *e = s + STRING_BYTES (XSTRING (it->overlay_strings[i]));
1872
1873 while (s < e && *s != '\n')
1874 ++s;
1875
1876 if (s < e)
1877 {
1878 overlay_strings_with_newlines = 1;
1879 break;
1880 }
1881 }
1882
1883 /* If position is within an overlay string, set up IT to the right
1884 overlay string. */
1885 if (pos->overlay_string_index >= 0)
1886 {
1887 int relative_index;
1888
1889 /* If the first overlay string happens to have a `display'
1890 property for an image, the iterator will be set up for that
1891 image, and we have to undo that setup first before we can
1892 correct the overlay string index. */
1893 if (it->method == next_element_from_image)
1894 pop_it (it);
1895
1896 /* We already have the first chunk of overlay strings in
1897 IT->overlay_strings. Load more until the one for
1898 pos->overlay_string_index is in IT->overlay_strings. */
1899 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
1900 {
1901 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
1902 it->current.overlay_string_index = 0;
1903 while (n--)
1904 {
1905 load_overlay_strings (it, 0);
1906 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
1907 }
1908 }
1909
1910 it->current.overlay_string_index = pos->overlay_string_index;
1911 relative_index = (it->current.overlay_string_index
1912 % OVERLAY_STRING_CHUNK_SIZE);
1913 it->string = it->overlay_strings[relative_index];
1914 xassert (STRINGP (it->string));
1915 it->current.string_pos = pos->string_pos;
1916 it->method = next_element_from_string;
1917 }
1918
1919 #if 0 /* This is bogus because POS not having an overlay string
1920 position does not mean it's after the string. Example: A
1921 line starting with a before-string and initialization of IT
1922 to the previous row's end position. */
1923 else if (it->current.overlay_string_index >= 0)
1924 {
1925 /* If POS says we're already after an overlay string ending at
1926 POS, make sure to pop the iterator because it will be in
1927 front of that overlay string. When POS is ZV, we've thereby
1928 also ``processed'' overlay strings at ZV. */
1929 while (it->sp)
1930 pop_it (it);
1931 it->current.overlay_string_index = -1;
1932 it->method = next_element_from_buffer;
1933 if (CHARPOS (pos->pos) == ZV)
1934 it->overlay_strings_at_end_processed_p = 1;
1935 }
1936 #endif /* 0 */
1937
1938 if (CHARPOS (pos->string_pos) >= 0)
1939 {
1940 /* Recorded position is not in an overlay string, but in another
1941 string. This can only be a string from a `display' property.
1942 IT should already be filled with that string. */
1943 it->current.string_pos = pos->string_pos;
1944 xassert (STRINGP (it->string));
1945 }
1946
1947 /* Restore position in display vector translations, control
1948 character translations or ellipses. */
1949 if (pos->dpvec_index >= 0)
1950 {
1951 if (it->dpvec == NULL)
1952 get_next_display_element (it);
1953 xassert (it->dpvec && it->current.dpvec_index == 0);
1954 it->current.dpvec_index = pos->dpvec_index;
1955 }
1956
1957 CHECK_IT (it);
1958 return !overlay_strings_with_newlines;
1959 }
1960
1961
1962 /* Initialize IT for stepping through current_buffer in window W
1963 starting at ROW->start. */
1964
1965 static void
1966 init_to_row_start (it, w, row)
1967 struct it *it;
1968 struct window *w;
1969 struct glyph_row *row;
1970 {
1971 init_from_display_pos (it, w, &row->start);
1972 it->continuation_lines_width = row->continuation_lines_width;
1973 CHECK_IT (it);
1974 }
1975
1976
1977 /* Initialize IT for stepping through current_buffer in window W
1978 starting in the line following ROW, i.e. starting at ROW->end.
1979 Value is zero if there are overlay strings with newlines at ROW's
1980 end position. */
1981
1982 static int
1983 init_to_row_end (it, w, row)
1984 struct it *it;
1985 struct window *w;
1986 struct glyph_row *row;
1987 {
1988 int success = 0;
1989
1990 if (init_from_display_pos (it, w, &row->end))
1991 {
1992 if (row->continued_p)
1993 it->continuation_lines_width
1994 = row->continuation_lines_width + row->pixel_width;
1995 CHECK_IT (it);
1996 success = 1;
1997 }
1998
1999 return success;
2000 }
2001
2002
2003
2004 \f
2005 /***********************************************************************
2006 Text properties
2007 ***********************************************************************/
2008
2009 /* Called when IT reaches IT->stop_charpos. Handle text property and
2010 overlay changes. Set IT->stop_charpos to the next position where
2011 to stop. */
2012
2013 static void
2014 handle_stop (it)
2015 struct it *it;
2016 {
2017 enum prop_handled handled;
2018 int handle_overlay_change_p = 1;
2019 struct props *p;
2020
2021 it->dpvec = NULL;
2022 it->current.dpvec_index = -1;
2023
2024 do
2025 {
2026 handled = HANDLED_NORMALLY;
2027
2028 /* Call text property handlers. */
2029 for (p = it_props; p->handler; ++p)
2030 {
2031 handled = p->handler (it);
2032
2033 if (handled == HANDLED_RECOMPUTE_PROPS)
2034 break;
2035 else if (handled == HANDLED_RETURN)
2036 return;
2037 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2038 handle_overlay_change_p = 0;
2039 }
2040
2041 if (handled != HANDLED_RECOMPUTE_PROPS)
2042 {
2043 /* Don't check for overlay strings below when set to deliver
2044 characters from a display vector. */
2045 if (it->method == next_element_from_display_vector)
2046 handle_overlay_change_p = 0;
2047
2048 /* Handle overlay changes. */
2049 if (handle_overlay_change_p)
2050 handled = handle_overlay_change (it);
2051
2052 /* Determine where to stop next. */
2053 if (handled == HANDLED_NORMALLY)
2054 compute_stop_pos (it);
2055 }
2056 }
2057 while (handled == HANDLED_RECOMPUTE_PROPS);
2058 }
2059
2060
2061 /* Compute IT->stop_charpos from text property and overlay change
2062 information for IT's current position. */
2063
2064 static void
2065 compute_stop_pos (it)
2066 struct it *it;
2067 {
2068 register INTERVAL iv, next_iv;
2069 Lisp_Object object, limit, position;
2070
2071 /* If nowhere else, stop at the end. */
2072 it->stop_charpos = it->end_charpos;
2073
2074 if (STRINGP (it->string))
2075 {
2076 /* Strings are usually short, so don't limit the search for
2077 properties. */
2078 object = it->string;
2079 limit = Qnil;
2080 position = make_number (IT_STRING_CHARPOS (*it));
2081 }
2082 else
2083 {
2084 int charpos;
2085
2086 /* If next overlay change is in front of the current stop pos
2087 (which is IT->end_charpos), stop there. Note: value of
2088 next_overlay_change is point-max if no overlay change
2089 follows. */
2090 charpos = next_overlay_change (IT_CHARPOS (*it));
2091 if (charpos < it->stop_charpos)
2092 it->stop_charpos = charpos;
2093
2094 /* If showing the region, we have to stop at the region
2095 start or end because the face might change there. */
2096 if (it->region_beg_charpos > 0)
2097 {
2098 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2099 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2100 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2101 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2102 }
2103
2104 /* Set up variables for computing the stop position from text
2105 property changes. */
2106 XSETBUFFER (object, current_buffer);
2107 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2108 position = make_number (IT_CHARPOS (*it));
2109
2110 }
2111
2112 /* Get the interval containing IT's position. Value is a null
2113 interval if there isn't such an interval. */
2114 iv = validate_interval_range (object, &position, &position, 0);
2115 if (!NULL_INTERVAL_P (iv))
2116 {
2117 Lisp_Object values_here[LAST_PROP_IDX];
2118 struct props *p;
2119
2120 /* Get properties here. */
2121 for (p = it_props; p->handler; ++p)
2122 values_here[p->idx] = textget (iv->plist, *p->name);
2123
2124 /* Look for an interval following iv that has different
2125 properties. */
2126 for (next_iv = next_interval (iv);
2127 (!NULL_INTERVAL_P (next_iv)
2128 && (NILP (limit)
2129 || XFASTINT (limit) > next_iv->position));
2130 next_iv = next_interval (next_iv))
2131 {
2132 for (p = it_props; p->handler; ++p)
2133 {
2134 Lisp_Object new_value;
2135
2136 new_value = textget (next_iv->plist, *p->name);
2137 if (!EQ (values_here[p->idx], new_value))
2138 break;
2139 }
2140
2141 if (p->handler)
2142 break;
2143 }
2144
2145 if (!NULL_INTERVAL_P (next_iv))
2146 {
2147 if (INTEGERP (limit)
2148 && next_iv->position >= XFASTINT (limit))
2149 /* No text property change up to limit. */
2150 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2151 else
2152 /* Text properties change in next_iv. */
2153 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2154 }
2155 }
2156
2157 xassert (STRINGP (it->string)
2158 || (it->stop_charpos >= BEGV
2159 && it->stop_charpos >= IT_CHARPOS (*it)));
2160 }
2161
2162
2163 /* Return the position of the next overlay change after POS in
2164 current_buffer. Value is point-max if no overlay change
2165 follows. This is like `next-overlay-change' but doesn't use
2166 xmalloc. */
2167
2168 static int
2169 next_overlay_change (pos)
2170 int pos;
2171 {
2172 int noverlays;
2173 int endpos;
2174 Lisp_Object *overlays;
2175 int len;
2176 int i;
2177
2178 /* Get all overlays at the given position. */
2179 len = 10;
2180 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2181 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2182 if (noverlays > len)
2183 {
2184 len = noverlays;
2185 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2186 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2187 }
2188
2189 /* If any of these overlays ends before endpos,
2190 use its ending point instead. */
2191 for (i = 0; i < noverlays; ++i)
2192 {
2193 Lisp_Object oend;
2194 int oendpos;
2195
2196 oend = OVERLAY_END (overlays[i]);
2197 oendpos = OVERLAY_POSITION (oend);
2198 endpos = min (endpos, oendpos);
2199 }
2200
2201 return endpos;
2202 }
2203
2204
2205 \f
2206 /***********************************************************************
2207 Fontification
2208 ***********************************************************************/
2209
2210 /* Handle changes in the `fontified' property of the current buffer by
2211 calling hook functions from Qfontification_functions to fontify
2212 regions of text. */
2213
2214 static enum prop_handled
2215 handle_fontified_prop (it)
2216 struct it *it;
2217 {
2218 Lisp_Object prop, pos;
2219 enum prop_handled handled = HANDLED_NORMALLY;
2220
2221 /* Get the value of the `fontified' property at IT's current buffer
2222 position. (The `fontified' property doesn't have a special
2223 meaning in strings.) If the value is nil, call functions from
2224 Qfontification_functions. */
2225 if (!STRINGP (it->string)
2226 && it->s == NULL
2227 && !NILP (Vfontification_functions)
2228 && !NILP (Vrun_hooks)
2229 && (pos = make_number (IT_CHARPOS (*it)),
2230 prop = Fget_char_property (pos, Qfontified, Qnil),
2231 NILP (prop)))
2232 {
2233 int count = BINDING_STACK_SIZE ();
2234 Lisp_Object val;
2235
2236 val = Vfontification_functions;
2237 specbind (Qfontification_functions, Qnil);
2238
2239 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2240 safe_call1 (val, pos);
2241 else
2242 {
2243 Lisp_Object globals, fn;
2244 struct gcpro gcpro1, gcpro2;
2245
2246 globals = Qnil;
2247 GCPRO2 (val, globals);
2248
2249 for (; CONSP (val); val = XCDR (val))
2250 {
2251 fn = XCAR (val);
2252
2253 if (EQ (fn, Qt))
2254 {
2255 /* A value of t indicates this hook has a local
2256 binding; it means to run the global binding too.
2257 In a global value, t should not occur. If it
2258 does, we must ignore it to avoid an endless
2259 loop. */
2260 for (globals = Fdefault_value (Qfontification_functions);
2261 CONSP (globals);
2262 globals = XCDR (globals))
2263 {
2264 fn = XCAR (globals);
2265 if (!EQ (fn, Qt))
2266 safe_call1 (fn, pos);
2267 }
2268 }
2269 else
2270 safe_call1 (fn, pos);
2271 }
2272
2273 UNGCPRO;
2274 }
2275
2276 unbind_to (count, Qnil);
2277
2278 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2279 something. This avoids an endless loop if they failed to
2280 fontify the text for which reason ever. */
2281 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2282 handled = HANDLED_RECOMPUTE_PROPS;
2283 }
2284
2285 return handled;
2286 }
2287
2288
2289 \f
2290 /***********************************************************************
2291 Faces
2292 ***********************************************************************/
2293
2294 /* Set up iterator IT from face properties at its current position.
2295 Called from handle_stop. */
2296
2297 static enum prop_handled
2298 handle_face_prop (it)
2299 struct it *it;
2300 {
2301 int new_face_id, next_stop;
2302
2303 if (!STRINGP (it->string))
2304 {
2305 new_face_id
2306 = face_at_buffer_position (it->w,
2307 IT_CHARPOS (*it),
2308 it->region_beg_charpos,
2309 it->region_end_charpos,
2310 &next_stop,
2311 (IT_CHARPOS (*it)
2312 + TEXT_PROP_DISTANCE_LIMIT),
2313 0);
2314
2315 /* Is this a start of a run of characters with box face?
2316 Caveat: this can be called for a freshly initialized
2317 iterator; face_id is -1 in this case. We know that the new
2318 face will not change until limit, i.e. if the new face has a
2319 box, all characters up to limit will have one. But, as
2320 usual, we don't know whether limit is really the end. */
2321 if (new_face_id != it->face_id)
2322 {
2323 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2324
2325 /* If new face has a box but old face has not, this is
2326 the start of a run of characters with box, i.e. it has
2327 a shadow on the left side. The value of face_id of the
2328 iterator will be -1 if this is the initial call that gets
2329 the face. In this case, we have to look in front of IT's
2330 position and see whether there is a face != new_face_id. */
2331 it->start_of_box_run_p
2332 = (new_face->box != FACE_NO_BOX
2333 && (it->face_id >= 0
2334 || IT_CHARPOS (*it) == BEG
2335 || new_face_id != face_before_it_pos (it)));
2336 it->face_box_p = new_face->box != FACE_NO_BOX;
2337 }
2338 }
2339 else
2340 {
2341 int base_face_id, bufpos;
2342
2343 if (it->current.overlay_string_index >= 0)
2344 bufpos = IT_CHARPOS (*it);
2345 else
2346 bufpos = 0;
2347
2348 /* For strings from a buffer, i.e. overlay strings or strings
2349 from a `display' property, use the face at IT's current
2350 buffer position as the base face to merge with, so that
2351 overlay strings appear in the same face as surrounding
2352 text, unless they specify their own faces. */
2353 base_face_id = underlying_face_id (it);
2354
2355 new_face_id = face_at_string_position (it->w,
2356 it->string,
2357 IT_STRING_CHARPOS (*it),
2358 bufpos,
2359 it->region_beg_charpos,
2360 it->region_end_charpos,
2361 &next_stop,
2362 base_face_id, 0);
2363
2364 #if 0 /* This shouldn't be neccessary. Let's check it. */
2365 /* If IT is used to display a mode line we would really like to
2366 use the mode line face instead of the frame's default face. */
2367 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2368 && new_face_id == DEFAULT_FACE_ID)
2369 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
2370 #endif
2371
2372 /* Is this a start of a run of characters with box? Caveat:
2373 this can be called for a freshly allocated iterator; face_id
2374 is -1 is this case. We know that the new face will not
2375 change until the next check pos, i.e. if the new face has a
2376 box, all characters up to that position will have a
2377 box. But, as usual, we don't know whether that position
2378 is really the end. */
2379 if (new_face_id != it->face_id)
2380 {
2381 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2382 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2383
2384 /* If new face has a box but old face hasn't, this is the
2385 start of a run of characters with box, i.e. it has a
2386 shadow on the left side. */
2387 it->start_of_box_run_p
2388 = new_face->box && (old_face == NULL || !old_face->box);
2389 it->face_box_p = new_face->box != FACE_NO_BOX;
2390 }
2391 }
2392
2393 it->face_id = new_face_id;
2394 return HANDLED_NORMALLY;
2395 }
2396
2397
2398 /* Return the ID of the face ``underlying'' IT's current position,
2399 which is in a string. If the iterator is associated with a
2400 buffer, return the face at IT's current buffer position.
2401 Otherwise, use the iterator's base_face_id. */
2402
2403 static int
2404 underlying_face_id (it)
2405 struct it *it;
2406 {
2407 int face_id = it->base_face_id, i;
2408
2409 xassert (STRINGP (it->string));
2410
2411 for (i = it->sp - 1; i >= 0; --i)
2412 if (NILP (it->stack[i].string))
2413 face_id = it->stack[i].face_id;
2414
2415 return face_id;
2416 }
2417
2418
2419 /* Compute the face one character before or after the current position
2420 of IT. BEFORE_P non-zero means get the face in front of IT's
2421 position. Value is the id of the face. */
2422
2423 static int
2424 face_before_or_after_it_pos (it, before_p)
2425 struct it *it;
2426 int before_p;
2427 {
2428 int face_id, limit;
2429 int next_check_charpos;
2430 struct text_pos pos;
2431
2432 xassert (it->s == NULL);
2433
2434 if (STRINGP (it->string))
2435 {
2436 int bufpos, base_face_id;
2437
2438 /* No face change past the end of the string (for the case
2439 we are padding with spaces). No face change before the
2440 string start. */
2441 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size
2442 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2443 return it->face_id;
2444
2445 /* Set pos to the position before or after IT's current position. */
2446 if (before_p)
2447 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2448 else
2449 /* For composition, we must check the character after the
2450 composition. */
2451 pos = (it->what == IT_COMPOSITION
2452 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
2453 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
2454
2455 if (it->current.overlay_string_index >= 0)
2456 bufpos = IT_CHARPOS (*it);
2457 else
2458 bufpos = 0;
2459
2460 base_face_id = underlying_face_id (it);
2461
2462 /* Get the face for ASCII, or unibyte. */
2463 face_id = face_at_string_position (it->w,
2464 it->string,
2465 CHARPOS (pos),
2466 bufpos,
2467 it->region_beg_charpos,
2468 it->region_end_charpos,
2469 &next_check_charpos,
2470 base_face_id, 0);
2471
2472 /* Correct the face for charsets different from ASCII. Do it
2473 for the multibyte case only. The face returned above is
2474 suitable for unibyte text if IT->string is unibyte. */
2475 if (STRING_MULTIBYTE (it->string))
2476 {
2477 unsigned char *p = XSTRING (it->string)->data + BYTEPOS (pos);
2478 int rest = STRING_BYTES (XSTRING (it->string)) - BYTEPOS (pos);
2479 int c, len;
2480 struct face *face = FACE_FROM_ID (it->f, face_id);
2481
2482 c = string_char_and_length (p, rest, &len);
2483 face_id = FACE_FOR_CHAR (it->f, face, c);
2484 }
2485 }
2486 else
2487 {
2488 if ((IT_CHARPOS (*it) >= ZV && !before_p)
2489 || (IT_CHARPOS (*it) <= BEGV && before_p))
2490 return it->face_id;
2491
2492 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
2493 pos = it->current.pos;
2494
2495 if (before_p)
2496 DEC_TEXT_POS (pos, it->multibyte_p);
2497 else
2498 {
2499 if (it->what == IT_COMPOSITION)
2500 /* For composition, we must check the position after the
2501 composition. */
2502 pos.charpos += it->cmp_len, pos.bytepos += it->len;
2503 else
2504 INC_TEXT_POS (pos, it->multibyte_p);
2505 }
2506
2507 /* Determine face for CHARSET_ASCII, or unibyte. */
2508 face_id = face_at_buffer_position (it->w,
2509 CHARPOS (pos),
2510 it->region_beg_charpos,
2511 it->region_end_charpos,
2512 &next_check_charpos,
2513 limit, 0);
2514
2515 /* Correct the face for charsets different from ASCII. Do it
2516 for the multibyte case only. The face returned above is
2517 suitable for unibyte text if current_buffer is unibyte. */
2518 if (it->multibyte_p)
2519 {
2520 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
2521 struct face *face = FACE_FROM_ID (it->f, face_id);
2522 face_id = FACE_FOR_CHAR (it->f, face, c);
2523 }
2524 }
2525
2526 return face_id;
2527 }
2528
2529
2530 \f
2531 /***********************************************************************
2532 Invisible text
2533 ***********************************************************************/
2534
2535 /* Set up iterator IT from invisible properties at its current
2536 position. Called from handle_stop. */
2537
2538 static enum prop_handled
2539 handle_invisible_prop (it)
2540 struct it *it;
2541 {
2542 enum prop_handled handled = HANDLED_NORMALLY;
2543
2544 if (STRINGP (it->string))
2545 {
2546 extern Lisp_Object Qinvisible;
2547 Lisp_Object prop, end_charpos, limit, charpos;
2548
2549 /* Get the value of the invisible text property at the
2550 current position. Value will be nil if there is no such
2551 property. */
2552 charpos = make_number (IT_STRING_CHARPOS (*it));
2553 prop = Fget_text_property (charpos, Qinvisible, it->string);
2554
2555 if (!NILP (prop)
2556 && IT_STRING_CHARPOS (*it) < it->end_charpos)
2557 {
2558 handled = HANDLED_RECOMPUTE_PROPS;
2559
2560 /* Get the position at which the next change of the
2561 invisible text property can be found in IT->string.
2562 Value will be nil if the property value is the same for
2563 all the rest of IT->string. */
2564 XSETINT (limit, XSTRING (it->string)->size);
2565 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2566 it->string, limit);
2567
2568 /* Text at current position is invisible. The next
2569 change in the property is at position end_charpos.
2570 Move IT's current position to that position. */
2571 if (INTEGERP (end_charpos)
2572 && XFASTINT (end_charpos) < XFASTINT (limit))
2573 {
2574 struct text_pos old;
2575 old = it->current.string_pos;
2576 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
2577 compute_string_pos (&it->current.string_pos, old, it->string);
2578 }
2579 else
2580 {
2581 /* The rest of the string is invisible. If this is an
2582 overlay string, proceed with the next overlay string
2583 or whatever comes and return a character from there. */
2584 if (it->current.overlay_string_index >= 0)
2585 {
2586 next_overlay_string (it);
2587 /* Don't check for overlay strings when we just
2588 finished processing them. */
2589 handled = HANDLED_OVERLAY_STRING_CONSUMED;
2590 }
2591 else
2592 {
2593 struct Lisp_String *s = XSTRING (it->string);
2594 IT_STRING_CHARPOS (*it) = s->size;
2595 IT_STRING_BYTEPOS (*it) = STRING_BYTES (s);
2596 }
2597 }
2598 }
2599 }
2600 else
2601 {
2602 int invis_p, newpos, next_stop, start_charpos;
2603 Lisp_Object pos, prop, overlay;
2604
2605 /* First of all, is there invisible text at this position? */
2606 start_charpos = IT_CHARPOS (*it);
2607 pos = make_number (IT_CHARPOS (*it));
2608 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
2609 &overlay);
2610 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
2611
2612 /* If we are on invisible text, skip over it. */
2613 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
2614 {
2615 /* Record whether we have to display an ellipsis for the
2616 invisible text. */
2617 int display_ellipsis_p = invis_p == 2;
2618
2619 handled = HANDLED_RECOMPUTE_PROPS;
2620
2621 /* Loop skipping over invisible text. The loop is left at
2622 ZV or with IT on the first char being visible again. */
2623 do
2624 {
2625 /* Try to skip some invisible text. Return value is the
2626 position reached which can be equal to IT's position
2627 if there is nothing invisible here. This skips both
2628 over invisible text properties and overlays with
2629 invisible property. */
2630 newpos = skip_invisible (IT_CHARPOS (*it),
2631 &next_stop, ZV, it->window);
2632
2633 /* If we skipped nothing at all we weren't at invisible
2634 text in the first place. If everything to the end of
2635 the buffer was skipped, end the loop. */
2636 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
2637 invis_p = 0;
2638 else
2639 {
2640 /* We skipped some characters but not necessarily
2641 all there are. Check if we ended up on visible
2642 text. Fget_char_property returns the property of
2643 the char before the given position, i.e. if we
2644 get invis_p = 0, this means that the char at
2645 newpos is visible. */
2646 pos = make_number (newpos);
2647 prop = Fget_char_property (pos, Qinvisible, it->window);
2648 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
2649 }
2650
2651 /* If we ended up on invisible text, proceed to
2652 skip starting with next_stop. */
2653 if (invis_p)
2654 IT_CHARPOS (*it) = next_stop;
2655 }
2656 while (invis_p);
2657
2658 /* The position newpos is now either ZV or on visible text. */
2659 IT_CHARPOS (*it) = newpos;
2660 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2661
2662 /* If there are before-strings at the start of invisible
2663 text, and the text is invisible because of a text
2664 property, arrange to show before-strings because 20.x did
2665 it that way. (If the text is invisible because of an
2666 overlay property instead of a text property, this is
2667 already handled in the overlay code.) */
2668 if (NILP (overlay)
2669 && get_overlay_strings (it, start_charpos))
2670 {
2671 handled = HANDLED_RECOMPUTE_PROPS;
2672 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
2673 }
2674 else if (display_ellipsis_p)
2675 setup_for_ellipsis (it);
2676 }
2677 }
2678
2679 return handled;
2680 }
2681
2682
2683 /* Make iterator IT return `...' next. */
2684
2685 static void
2686 setup_for_ellipsis (it)
2687 struct it *it;
2688 {
2689 if (it->dp
2690 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
2691 {
2692 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
2693 it->dpvec = v->contents;
2694 it->dpend = v->contents + v->size;
2695 }
2696 else
2697 {
2698 /* Default `...'. */
2699 it->dpvec = default_invis_vector;
2700 it->dpend = default_invis_vector + 3;
2701 }
2702
2703 /* The ellipsis display does not replace the display of the
2704 character at the new position. Indicate this by setting
2705 IT->dpvec_char_len to zero. */
2706 it->dpvec_char_len = 0;
2707
2708 it->current.dpvec_index = 0;
2709 it->method = next_element_from_display_vector;
2710 }
2711
2712
2713 \f
2714 /***********************************************************************
2715 'display' property
2716 ***********************************************************************/
2717
2718 /* Set up iterator IT from `display' property at its current position.
2719 Called from handle_stop. */
2720
2721 static enum prop_handled
2722 handle_display_prop (it)
2723 struct it *it;
2724 {
2725 Lisp_Object prop, object;
2726 struct text_pos *position;
2727 int display_replaced_p = 0;
2728
2729 if (STRINGP (it->string))
2730 {
2731 object = it->string;
2732 position = &it->current.string_pos;
2733 }
2734 else
2735 {
2736 object = it->w->buffer;
2737 position = &it->current.pos;
2738 }
2739
2740 /* Reset those iterator values set from display property values. */
2741 it->font_height = Qnil;
2742 it->space_width = Qnil;
2743 it->voffset = 0;
2744
2745 /* We don't support recursive `display' properties, i.e. string
2746 values that have a string `display' property, that have a string
2747 `display' property etc. */
2748 if (!it->string_from_display_prop_p)
2749 it->area = TEXT_AREA;
2750
2751 prop = Fget_char_property (make_number (position->charpos),
2752 Qdisplay, object);
2753 if (NILP (prop))
2754 return HANDLED_NORMALLY;
2755
2756 if (CONSP (prop)
2757 /* Simple properties. */
2758 && !EQ (XCAR (prop), Qimage)
2759 && !EQ (XCAR (prop), Qspace)
2760 && !EQ (XCAR (prop), Qwhen)
2761 && !EQ (XCAR (prop), Qspace_width)
2762 && !EQ (XCAR (prop), Qheight)
2763 && !EQ (XCAR (prop), Qraise)
2764 /* Marginal area specifications. */
2765 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
2766 && !NILP (XCAR (prop)))
2767 {
2768 for (; CONSP (prop); prop = XCDR (prop))
2769 {
2770 if (handle_single_display_prop (it, XCAR (prop), object,
2771 position, display_replaced_p))
2772 display_replaced_p = 1;
2773 }
2774 }
2775 else if (VECTORP (prop))
2776 {
2777 int i;
2778 for (i = 0; i < ASIZE (prop); ++i)
2779 if (handle_single_display_prop (it, AREF (prop, i), object,
2780 position, display_replaced_p))
2781 display_replaced_p = 1;
2782 }
2783 else
2784 {
2785 if (handle_single_display_prop (it, prop, object, position, 0))
2786 display_replaced_p = 1;
2787 }
2788
2789 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
2790 }
2791
2792
2793 /* Value is the position of the end of the `display' property starting
2794 at START_POS in OBJECT. */
2795
2796 static struct text_pos
2797 display_prop_end (it, object, start_pos)
2798 struct it *it;
2799 Lisp_Object object;
2800 struct text_pos start_pos;
2801 {
2802 Lisp_Object end;
2803 struct text_pos end_pos;
2804
2805 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
2806 Qdisplay, object, Qnil);
2807 CHARPOS (end_pos) = XFASTINT (end);
2808 if (STRINGP (object))
2809 compute_string_pos (&end_pos, start_pos, it->string);
2810 else
2811 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
2812
2813 return end_pos;
2814 }
2815
2816
2817 /* Set up IT from a single `display' sub-property value PROP. OBJECT
2818 is the object in which the `display' property was found. *POSITION
2819 is the position at which it was found. DISPLAY_REPLACED_P non-zero
2820 means that we previously saw a display sub-property which already
2821 replaced text display with something else, for example an image;
2822 ignore such properties after the first one has been processed.
2823
2824 If PROP is a `space' or `image' sub-property, set *POSITION to the
2825 end position of the `display' property.
2826
2827 Value is non-zero if something was found which replaces the display
2828 of buffer or string text. */
2829
2830 static int
2831 handle_single_display_prop (it, prop, object, position,
2832 display_replaced_before_p)
2833 struct it *it;
2834 Lisp_Object prop;
2835 Lisp_Object object;
2836 struct text_pos *position;
2837 int display_replaced_before_p;
2838 {
2839 Lisp_Object value;
2840 int replaces_text_display_p = 0;
2841 Lisp_Object form;
2842
2843 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
2844 evaluated. If the result is nil, VALUE is ignored. */
2845 form = Qt;
2846 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2847 {
2848 prop = XCDR (prop);
2849 if (!CONSP (prop))
2850 return 0;
2851 form = XCAR (prop);
2852 prop = XCDR (prop);
2853 }
2854
2855 if (!NILP (form) && !EQ (form, Qt))
2856 {
2857 int count = BINDING_STACK_SIZE ();
2858 struct gcpro gcpro1;
2859
2860 /* Bind `object' to the object having the `display' property, a
2861 buffer or string. Bind `position' to the position in the
2862 object where the property was found, and `buffer-position'
2863 to the current position in the buffer. */
2864 specbind (Qobject, object);
2865 specbind (Qposition, make_number (CHARPOS (*position)));
2866 specbind (Qbuffer_position,
2867 make_number (STRINGP (object)
2868 ? IT_CHARPOS (*it) : CHARPOS (*position)));
2869 GCPRO1 (form);
2870 form = safe_eval (form);
2871 UNGCPRO;
2872 unbind_to (count, Qnil);
2873 }
2874
2875 if (NILP (form))
2876 return 0;
2877
2878 if (CONSP (prop)
2879 && EQ (XCAR (prop), Qheight)
2880 && CONSP (XCDR (prop)))
2881 {
2882 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2883 return 0;
2884
2885 /* `(height HEIGHT)'. */
2886 it->font_height = XCAR (XCDR (prop));
2887 if (!NILP (it->font_height))
2888 {
2889 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2890 int new_height = -1;
2891
2892 if (CONSP (it->font_height)
2893 && (EQ (XCAR (it->font_height), Qplus)
2894 || EQ (XCAR (it->font_height), Qminus))
2895 && CONSP (XCDR (it->font_height))
2896 && INTEGERP (XCAR (XCDR (it->font_height))))
2897 {
2898 /* `(+ N)' or `(- N)' where N is an integer. */
2899 int steps = XINT (XCAR (XCDR (it->font_height)));
2900 if (EQ (XCAR (it->font_height), Qplus))
2901 steps = - steps;
2902 it->face_id = smaller_face (it->f, it->face_id, steps);
2903 }
2904 else if (FUNCTIONP (it->font_height))
2905 {
2906 /* Call function with current height as argument.
2907 Value is the new height. */
2908 Lisp_Object height;
2909 height = safe_call1 (it->font_height,
2910 face->lface[LFACE_HEIGHT_INDEX]);
2911 if (NUMBERP (height))
2912 new_height = XFLOATINT (height);
2913 }
2914 else if (NUMBERP (it->font_height))
2915 {
2916 /* Value is a multiple of the canonical char height. */
2917 struct face *face;
2918
2919 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
2920 new_height = (XFLOATINT (it->font_height)
2921 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
2922 }
2923 else
2924 {
2925 /* Evaluate IT->font_height with `height' bound to the
2926 current specified height to get the new height. */
2927 Lisp_Object value;
2928 int count = BINDING_STACK_SIZE ();
2929
2930 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
2931 value = safe_eval (it->font_height);
2932 unbind_to (count, Qnil);
2933
2934 if (NUMBERP (value))
2935 new_height = XFLOATINT (value);
2936 }
2937
2938 if (new_height > 0)
2939 it->face_id = face_with_height (it->f, it->face_id, new_height);
2940 }
2941 }
2942 else if (CONSP (prop)
2943 && EQ (XCAR (prop), Qspace_width)
2944 && CONSP (XCDR (prop)))
2945 {
2946 /* `(space_width WIDTH)'. */
2947 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2948 return 0;
2949
2950 value = XCAR (XCDR (prop));
2951 if (NUMBERP (value) && XFLOATINT (value) > 0)
2952 it->space_width = value;
2953 }
2954 else if (CONSP (prop)
2955 && EQ (XCAR (prop), Qraise)
2956 && CONSP (XCDR (prop)))
2957 {
2958 /* `(raise FACTOR)'. */
2959 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2960 return 0;
2961
2962 #ifdef HAVE_WINDOW_SYSTEM
2963 value = XCAR (XCDR (prop));
2964 if (NUMBERP (value))
2965 {
2966 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2967 it->voffset = - (XFLOATINT (value)
2968 * (FONT_HEIGHT (face->font)));
2969 }
2970 #endif /* HAVE_WINDOW_SYSTEM */
2971 }
2972 else if (!it->string_from_display_prop_p)
2973 {
2974 /* `((margin left-margin) VALUE)' or `((margin right-margin)
2975 VALUE) or `((margin nil) VALUE)' or VALUE. */
2976 Lisp_Object location, value;
2977 struct text_pos start_pos;
2978 int valid_p;
2979
2980 /* Characters having this form of property are not displayed, so
2981 we have to find the end of the property. */
2982 start_pos = *position;
2983 *position = display_prop_end (it, object, start_pos);
2984 value = Qnil;
2985
2986 /* Let's stop at the new position and assume that all
2987 text properties change there. */
2988 it->stop_charpos = position->charpos;
2989
2990 location = Qunbound;
2991 if (CONSP (prop) && CONSP (XCAR (prop)))
2992 {
2993 Lisp_Object tem;
2994
2995 value = XCDR (prop);
2996 if (CONSP (value))
2997 value = XCAR (value);
2998
2999 tem = XCAR (prop);
3000 if (EQ (XCAR (tem), Qmargin)
3001 && (tem = XCDR (tem),
3002 tem = CONSP (tem) ? XCAR (tem) : Qnil,
3003 (NILP (tem)
3004 || EQ (tem, Qleft_margin)
3005 || EQ (tem, Qright_margin))))
3006 location = tem;
3007 }
3008
3009 if (EQ (location, Qunbound))
3010 {
3011 location = Qnil;
3012 value = prop;
3013 }
3014
3015 #ifdef HAVE_WINDOW_SYSTEM
3016 if (FRAME_TERMCAP_P (it->f))
3017 valid_p = STRINGP (value);
3018 else
3019 valid_p = (STRINGP (value)
3020 || (CONSP (value) && EQ (XCAR (value), Qspace))
3021 || valid_image_p (value));
3022 #else /* not HAVE_WINDOW_SYSTEM */
3023 valid_p = STRINGP (value);
3024 #endif /* not HAVE_WINDOW_SYSTEM */
3025
3026 if ((EQ (location, Qleft_margin)
3027 || EQ (location, Qright_margin)
3028 || NILP (location))
3029 && valid_p
3030 && !display_replaced_before_p)
3031 {
3032 replaces_text_display_p = 1;
3033
3034 /* Save current settings of IT so that we can restore them
3035 when we are finished with the glyph property value. */
3036 push_it (it);
3037
3038 if (NILP (location))
3039 it->area = TEXT_AREA;
3040 else if (EQ (location, Qleft_margin))
3041 it->area = LEFT_MARGIN_AREA;
3042 else
3043 it->area = RIGHT_MARGIN_AREA;
3044
3045 if (STRINGP (value))
3046 {
3047 it->string = value;
3048 it->multibyte_p = STRING_MULTIBYTE (it->string);
3049 it->current.overlay_string_index = -1;
3050 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3051 it->end_charpos = it->string_nchars
3052 = XSTRING (it->string)->size;
3053 it->method = next_element_from_string;
3054 it->stop_charpos = 0;
3055 it->string_from_display_prop_p = 1;
3056 /* Say that we haven't consumed the characters with
3057 `display' property yet. The call to pop_it in
3058 set_iterator_to_next will clean this up. */
3059 *position = start_pos;
3060 }
3061 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3062 {
3063 it->method = next_element_from_stretch;
3064 it->object = value;
3065 it->current.pos = it->position = start_pos;
3066 }
3067 #ifdef HAVE_WINDOW_SYSTEM
3068 else
3069 {
3070 it->what = IT_IMAGE;
3071 it->image_id = lookup_image (it->f, value);
3072 it->position = start_pos;
3073 it->object = NILP (object) ? it->w->buffer : object;
3074 it->method = next_element_from_image;
3075
3076 /* Say that we haven't consumed the characters with
3077 `display' property yet. The call to pop_it in
3078 set_iterator_to_next will clean this up. */
3079 *position = start_pos;
3080 }
3081 #endif /* HAVE_WINDOW_SYSTEM */
3082 }
3083 else
3084 /* Invalid property or property not supported. Restore
3085 the position to what it was before. */
3086 *position = start_pos;
3087 }
3088
3089 return replaces_text_display_p;
3090 }
3091
3092
3093 /* Check if PROP is a display sub-property value whose text should be
3094 treated as intangible. */
3095
3096 static int
3097 single_display_prop_intangible_p (prop)
3098 Lisp_Object prop;
3099 {
3100 /* Skip over `when FORM'. */
3101 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3102 {
3103 prop = XCDR (prop);
3104 if (!CONSP (prop))
3105 return 0;
3106 prop = XCDR (prop);
3107 }
3108
3109 if (!CONSP (prop))
3110 return 0;
3111
3112 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3113 we don't need to treat text as intangible. */
3114 if (EQ (XCAR (prop), Qmargin))
3115 {
3116 prop = XCDR (prop);
3117 if (!CONSP (prop))
3118 return 0;
3119
3120 prop = XCDR (prop);
3121 if (!CONSP (prop)
3122 || EQ (XCAR (prop), Qleft_margin)
3123 || EQ (XCAR (prop), Qright_margin))
3124 return 0;
3125 }
3126
3127 return CONSP (prop) && EQ (XCAR (prop), Qimage);
3128 }
3129
3130
3131 /* Check if PROP is a display property value whose text should be
3132 treated as intangible. */
3133
3134 int
3135 display_prop_intangible_p (prop)
3136 Lisp_Object prop;
3137 {
3138 if (CONSP (prop)
3139 && CONSP (XCAR (prop))
3140 && !EQ (Qmargin, XCAR (XCAR (prop))))
3141 {
3142 /* A list of sub-properties. */
3143 while (CONSP (prop))
3144 {
3145 if (single_display_prop_intangible_p (XCAR (prop)))
3146 return 1;
3147 prop = XCDR (prop);
3148 }
3149 }
3150 else if (VECTORP (prop))
3151 {
3152 /* A vector of sub-properties. */
3153 int i;
3154 for (i = 0; i < ASIZE (prop); ++i)
3155 if (single_display_prop_intangible_p (AREF (prop, i)))
3156 return 1;
3157 }
3158 else
3159 return single_display_prop_intangible_p (prop);
3160
3161 return 0;
3162 }
3163
3164
3165 /* Return 1 if PROP is a display sub-property value containing STRING. */
3166
3167 static int
3168 single_display_prop_string_p (prop, string)
3169 Lisp_Object prop, string;
3170 {
3171 if (EQ (string, prop))
3172 return 1;
3173
3174 /* Skip over `when FORM'. */
3175 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3176 {
3177 prop = XCDR (prop);
3178 if (!CONSP (prop))
3179 return 0;
3180 prop = XCDR (prop);
3181 }
3182
3183 if (CONSP (prop))
3184 /* Skip over `margin LOCATION'. */
3185 if (EQ (XCAR (prop), Qmargin))
3186 {
3187 prop = XCDR (prop);
3188 if (!CONSP (prop))
3189 return 0;
3190
3191 prop = XCDR (prop);
3192 if (!CONSP (prop))
3193 return 0;
3194 }
3195
3196 return CONSP (prop) && EQ (XCAR (prop), string);
3197 }
3198
3199
3200 /* Return 1 if STRING appears in the `display' property PROP. */
3201
3202 static int
3203 display_prop_string_p (prop, string)
3204 Lisp_Object prop, string;
3205 {
3206 if (CONSP (prop)
3207 && CONSP (XCAR (prop))
3208 && !EQ (Qmargin, XCAR (XCAR (prop))))
3209 {
3210 /* A list of sub-properties. */
3211 while (CONSP (prop))
3212 {
3213 if (single_display_prop_string_p (XCAR (prop), string))
3214 return 1;
3215 prop = XCDR (prop);
3216 }
3217 }
3218 else if (VECTORP (prop))
3219 {
3220 /* A vector of sub-properties. */
3221 int i;
3222 for (i = 0; i < ASIZE (prop); ++i)
3223 if (single_display_prop_string_p (AREF (prop, i), string))
3224 return 1;
3225 }
3226 else
3227 return single_display_prop_string_p (prop, string);
3228
3229 return 0;
3230 }
3231
3232
3233 /* Determine from which buffer position in W's buffer STRING comes
3234 from. AROUND_CHARPOS is an approximate position where it could
3235 be from. Value is the buffer position or 0 if it couldn't be
3236 determined.
3237
3238 W's buffer must be current.
3239
3240 This function is necessary because we don't record buffer positions
3241 in glyphs generated from strings (to keep struct glyph small).
3242 This function may only use code that doesn't eval because it is
3243 called asynchronously from note_mouse_highlight. */
3244
3245 int
3246 string_buffer_position (w, string, around_charpos)
3247 struct window *w;
3248 Lisp_Object string;
3249 int around_charpos;
3250 {
3251 Lisp_Object limit, prop, pos;
3252 const int MAX_DISTANCE = 1000;
3253 int found = 0;
3254
3255 pos = make_number (around_charpos);
3256 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
3257 while (!found && !EQ (pos, limit))
3258 {
3259 prop = Fget_char_property (pos, Qdisplay, Qnil);
3260 if (!NILP (prop) && display_prop_string_p (prop, string))
3261 found = 1;
3262 else
3263 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
3264 }
3265
3266 if (!found)
3267 {
3268 pos = make_number (around_charpos);
3269 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
3270 while (!found && !EQ (pos, limit))
3271 {
3272 prop = Fget_char_property (pos, Qdisplay, Qnil);
3273 if (!NILP (prop) && display_prop_string_p (prop, string))
3274 found = 1;
3275 else
3276 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
3277 limit);
3278 }
3279 }
3280
3281 return found ? XINT (pos) : 0;
3282 }
3283
3284
3285 \f
3286 /***********************************************************************
3287 `composition' property
3288 ***********************************************************************/
3289
3290 static enum prop_handled
3291 handle_auto_composed_prop (it)
3292 struct it *it;
3293 {
3294 enum prop_handled handled = HANDLED_NORMALLY;
3295
3296 if (! NILP (Vauto_composition_function))
3297 {
3298 Lisp_Object val;
3299 int pos;
3300
3301 if (STRINGP (it->string))
3302 pos = IT_STRING_CHARPOS (*it);
3303 else
3304 pos = IT_CHARPOS (*it);
3305
3306 val =Fget_char_property (make_number (pos), Qauto_composed, it->string);
3307 if (NILP (val))
3308 {
3309 int count = BINDING_STACK_SIZE ();
3310 Lisp_Object args[3];
3311
3312 args[0] = Vauto_composition_function;
3313 specbind (Qauto_composition_function, Qnil);
3314 args[1] = make_number (pos);
3315 args[2] = it->string;
3316 safe_call (3, args);
3317 unbind_to (count, Qnil);
3318
3319 val = Fget_char_property (args[1], Qauto_composed, it->string);
3320 if (! NILP (val))
3321 handled = HANDLED_RECOMPUTE_PROPS;
3322 }
3323 }
3324
3325 return handled;
3326 }
3327
3328 /* Set up iterator IT from `composition' property at its current
3329 position. Called from handle_stop. */
3330
3331 static enum prop_handled
3332 handle_composition_prop (it)
3333 struct it *it;
3334 {
3335 Lisp_Object prop, string;
3336 int pos, pos_byte, end;
3337 enum prop_handled handled = HANDLED_NORMALLY;
3338
3339 if (STRINGP (it->string))
3340 {
3341 pos = IT_STRING_CHARPOS (*it);
3342 pos_byte = IT_STRING_BYTEPOS (*it);
3343 string = it->string;
3344 }
3345 else
3346 {
3347 pos = IT_CHARPOS (*it);
3348 pos_byte = IT_BYTEPOS (*it);
3349 string = Qnil;
3350 }
3351
3352 /* If there's a valid composition and point is not inside of the
3353 composition (in the case that the composition is from the current
3354 buffer), draw a glyph composed from the composition components. */
3355 if (find_composition (pos, -1, &pos, &end, &prop, string)
3356 && COMPOSITION_VALID_P (pos, end, prop)
3357 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
3358 {
3359 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
3360
3361 if (id >= 0)
3362 {
3363 it->method = next_element_from_composition;
3364 it->cmp_id = id;
3365 it->cmp_len = COMPOSITION_LENGTH (prop);
3366 /* For a terminal, draw only the first character of the
3367 components. */
3368 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
3369 it->len = (STRINGP (it->string)
3370 ? string_char_to_byte (it->string, end)
3371 : CHAR_TO_BYTE (end)) - pos_byte;
3372 it->stop_charpos = end;
3373 handled = HANDLED_RETURN;
3374 }
3375 }
3376
3377 return handled;
3378 }
3379
3380
3381 \f
3382 /***********************************************************************
3383 Overlay strings
3384 ***********************************************************************/
3385
3386 /* The following structure is used to record overlay strings for
3387 later sorting in load_overlay_strings. */
3388
3389 struct overlay_entry
3390 {
3391 Lisp_Object overlay;
3392 Lisp_Object string;
3393 int priority;
3394 int after_string_p;
3395 };
3396
3397
3398 /* Set up iterator IT from overlay strings at its current position.
3399 Called from handle_stop. */
3400
3401 static enum prop_handled
3402 handle_overlay_change (it)
3403 struct it *it;
3404 {
3405 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
3406 return HANDLED_RECOMPUTE_PROPS;
3407 else
3408 return HANDLED_NORMALLY;
3409 }
3410
3411
3412 /* Set up the next overlay string for delivery by IT, if there is an
3413 overlay string to deliver. Called by set_iterator_to_next when the
3414 end of the current overlay string is reached. If there are more
3415 overlay strings to display, IT->string and
3416 IT->current.overlay_string_index are set appropriately here.
3417 Otherwise IT->string is set to nil. */
3418
3419 static void
3420 next_overlay_string (it)
3421 struct it *it;
3422 {
3423 ++it->current.overlay_string_index;
3424 if (it->current.overlay_string_index == it->n_overlay_strings)
3425 {
3426 /* No more overlay strings. Restore IT's settings to what
3427 they were before overlay strings were processed, and
3428 continue to deliver from current_buffer. */
3429 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
3430
3431 pop_it (it);
3432 xassert (it->stop_charpos >= BEGV
3433 && it->stop_charpos <= it->end_charpos);
3434 it->string = Qnil;
3435 it->current.overlay_string_index = -1;
3436 SET_TEXT_POS (it->current.string_pos, -1, -1);
3437 it->n_overlay_strings = 0;
3438 it->method = next_element_from_buffer;
3439
3440 /* If we're at the end of the buffer, record that we have
3441 processed the overlay strings there already, so that
3442 next_element_from_buffer doesn't try it again. */
3443 if (IT_CHARPOS (*it) >= it->end_charpos)
3444 it->overlay_strings_at_end_processed_p = 1;
3445
3446 /* If we have to display `...' for invisible text, set
3447 the iterator up for that. */
3448 if (display_ellipsis_p)
3449 setup_for_ellipsis (it);
3450 }
3451 else
3452 {
3453 /* There are more overlay strings to process. If
3454 IT->current.overlay_string_index has advanced to a position
3455 where we must load IT->overlay_strings with more strings, do
3456 it. */
3457 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
3458
3459 if (it->current.overlay_string_index && i == 0)
3460 load_overlay_strings (it, 0);
3461
3462 /* Initialize IT to deliver display elements from the overlay
3463 string. */
3464 it->string = it->overlay_strings[i];
3465 it->multibyte_p = STRING_MULTIBYTE (it->string);
3466 SET_TEXT_POS (it->current.string_pos, 0, 0);
3467 it->method = next_element_from_string;
3468 it->stop_charpos = 0;
3469 }
3470
3471 CHECK_IT (it);
3472 }
3473
3474
3475 /* Compare two overlay_entry structures E1 and E2. Used as a
3476 comparison function for qsort in load_overlay_strings. Overlay
3477 strings for the same position are sorted so that
3478
3479 1. All after-strings come in front of before-strings, except
3480 when they come from the same overlay.
3481
3482 2. Within after-strings, strings are sorted so that overlay strings
3483 from overlays with higher priorities come first.
3484
3485 2. Within before-strings, strings are sorted so that overlay
3486 strings from overlays with higher priorities come last.
3487
3488 Value is analogous to strcmp. */
3489
3490
3491 static int
3492 compare_overlay_entries (e1, e2)
3493 void *e1, *e2;
3494 {
3495 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
3496 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
3497 int result;
3498
3499 if (entry1->after_string_p != entry2->after_string_p)
3500 {
3501 /* Let after-strings appear in front of before-strings if
3502 they come from different overlays. */
3503 if (EQ (entry1->overlay, entry2->overlay))
3504 result = entry1->after_string_p ? 1 : -1;
3505 else
3506 result = entry1->after_string_p ? -1 : 1;
3507 }
3508 else if (entry1->after_string_p)
3509 /* After-strings sorted in order of decreasing priority. */
3510 result = entry2->priority - entry1->priority;
3511 else
3512 /* Before-strings sorted in order of increasing priority. */
3513 result = entry1->priority - entry2->priority;
3514
3515 return result;
3516 }
3517
3518
3519 /* Load the vector IT->overlay_strings with overlay strings from IT's
3520 current buffer position, or from CHARPOS if that is > 0. Set
3521 IT->n_overlays to the total number of overlay strings found.
3522
3523 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
3524 a time. On entry into load_overlay_strings,
3525 IT->current.overlay_string_index gives the number of overlay
3526 strings that have already been loaded by previous calls to this
3527 function.
3528
3529 IT->add_overlay_start contains an additional overlay start
3530 position to consider for taking overlay strings from, if non-zero.
3531 This position comes into play when the overlay has an `invisible'
3532 property, and both before and after-strings. When we've skipped to
3533 the end of the overlay, because of its `invisible' property, we
3534 nevertheless want its before-string to appear.
3535 IT->add_overlay_start will contain the overlay start position
3536 in this case.
3537
3538 Overlay strings are sorted so that after-string strings come in
3539 front of before-string strings. Within before and after-strings,
3540 strings are sorted by overlay priority. See also function
3541 compare_overlay_entries. */
3542
3543 static void
3544 load_overlay_strings (it, charpos)
3545 struct it *it;
3546 int charpos;
3547 {
3548 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
3549 Lisp_Object ov, overlay, window, str, invisible;
3550 int start, end;
3551 int size = 20;
3552 int n = 0, i, j, invis_p;
3553 struct overlay_entry *entries
3554 = (struct overlay_entry *) alloca (size * sizeof *entries);
3555
3556 if (charpos <= 0)
3557 charpos = IT_CHARPOS (*it);
3558
3559 /* Append the overlay string STRING of overlay OVERLAY to vector
3560 `entries' which has size `size' and currently contains `n'
3561 elements. AFTER_P non-zero means STRING is an after-string of
3562 OVERLAY. */
3563 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
3564 do \
3565 { \
3566 Lisp_Object priority; \
3567 \
3568 if (n == size) \
3569 { \
3570 int new_size = 2 * size; \
3571 struct overlay_entry *old = entries; \
3572 entries = \
3573 (struct overlay_entry *) alloca (new_size \
3574 * sizeof *entries); \
3575 bcopy (old, entries, size * sizeof *entries); \
3576 size = new_size; \
3577 } \
3578 \
3579 entries[n].string = (STRING); \
3580 entries[n].overlay = (OVERLAY); \
3581 priority = Foverlay_get ((OVERLAY), Qpriority); \
3582 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
3583 entries[n].after_string_p = (AFTER_P); \
3584 ++n; \
3585 } \
3586 while (0)
3587
3588 /* Process overlay before the overlay center. */
3589 for (ov = current_buffer->overlays_before; CONSP (ov); ov = XCDR (ov))
3590 {
3591 overlay = XCAR (ov);
3592 xassert (OVERLAYP (overlay));
3593 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3594 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3595
3596 if (end < charpos)
3597 break;
3598
3599 /* Skip this overlay if it doesn't start or end at IT's current
3600 position. */
3601 if (end != charpos && start != charpos)
3602 continue;
3603
3604 /* Skip this overlay if it doesn't apply to IT->w. */
3605 window = Foverlay_get (overlay, Qwindow);
3606 if (WINDOWP (window) && XWINDOW (window) != it->w)
3607 continue;
3608
3609 /* If the text ``under'' the overlay is invisible, both before-
3610 and after-strings from this overlay are visible; start and
3611 end position are indistinguishable. */
3612 invisible = Foverlay_get (overlay, Qinvisible);
3613 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3614
3615 /* If overlay has a non-empty before-string, record it. */
3616 if ((start == charpos || (end == charpos && invis_p))
3617 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3618 && XSTRING (str)->size)
3619 RECORD_OVERLAY_STRING (overlay, str, 0);
3620
3621 /* If overlay has a non-empty after-string, record it. */
3622 if ((end == charpos || (start == charpos && invis_p))
3623 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3624 && XSTRING (str)->size)
3625 RECORD_OVERLAY_STRING (overlay, str, 1);
3626 }
3627
3628 /* Process overlays after the overlay center. */
3629 for (ov = current_buffer->overlays_after; CONSP (ov); ov = XCDR (ov))
3630 {
3631 overlay = XCAR (ov);
3632 xassert (OVERLAYP (overlay));
3633 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3634 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3635
3636 if (start > charpos)
3637 break;
3638
3639 /* Skip this overlay if it doesn't start or end at IT's current
3640 position. */
3641 if (end != charpos && start != charpos)
3642 continue;
3643
3644 /* Skip this overlay if it doesn't apply to IT->w. */
3645 window = Foverlay_get (overlay, Qwindow);
3646 if (WINDOWP (window) && XWINDOW (window) != it->w)
3647 continue;
3648
3649 /* If the text ``under'' the overlay is invisible, it has a zero
3650 dimension, and both before- and after-strings apply. */
3651 invisible = Foverlay_get (overlay, Qinvisible);
3652 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3653
3654 /* If overlay has a non-empty before-string, record it. */
3655 if ((start == charpos || (end == charpos && invis_p))
3656 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3657 && XSTRING (str)->size)
3658 RECORD_OVERLAY_STRING (overlay, str, 0);
3659
3660 /* If overlay has a non-empty after-string, record it. */
3661 if ((end == charpos || (start == charpos && invis_p))
3662 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3663 && XSTRING (str)->size)
3664 RECORD_OVERLAY_STRING (overlay, str, 1);
3665 }
3666
3667 #undef RECORD_OVERLAY_STRING
3668
3669 /* Sort entries. */
3670 if (n > 1)
3671 qsort (entries, n, sizeof *entries, compare_overlay_entries);
3672
3673 /* Record the total number of strings to process. */
3674 it->n_overlay_strings = n;
3675
3676 /* IT->current.overlay_string_index is the number of overlay strings
3677 that have already been consumed by IT. Copy some of the
3678 remaining overlay strings to IT->overlay_strings. */
3679 i = 0;
3680 j = it->current.overlay_string_index;
3681 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
3682 it->overlay_strings[i++] = entries[j++].string;
3683
3684 CHECK_IT (it);
3685 }
3686
3687
3688 /* Get the first chunk of overlay strings at IT's current buffer
3689 position, or at CHARPOS if that is > 0. Value is non-zero if at
3690 least one overlay string was found. */
3691
3692 static int
3693 get_overlay_strings (it, charpos)
3694 struct it *it;
3695 int charpos;
3696 {
3697 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
3698 process. This fills IT->overlay_strings with strings, and sets
3699 IT->n_overlay_strings to the total number of strings to process.
3700 IT->pos.overlay_string_index has to be set temporarily to zero
3701 because load_overlay_strings needs this; it must be set to -1
3702 when no overlay strings are found because a zero value would
3703 indicate a position in the first overlay string. */
3704 it->current.overlay_string_index = 0;
3705 load_overlay_strings (it, charpos);
3706
3707 /* If we found overlay strings, set up IT to deliver display
3708 elements from the first one. Otherwise set up IT to deliver
3709 from current_buffer. */
3710 if (it->n_overlay_strings)
3711 {
3712 /* Make sure we know settings in current_buffer, so that we can
3713 restore meaningful values when we're done with the overlay
3714 strings. */
3715 compute_stop_pos (it);
3716 xassert (it->face_id >= 0);
3717
3718 /* Save IT's settings. They are restored after all overlay
3719 strings have been processed. */
3720 xassert (it->sp == 0);
3721 push_it (it);
3722
3723 /* Set up IT to deliver display elements from the first overlay
3724 string. */
3725 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3726 it->string = it->overlay_strings[0];
3727 it->stop_charpos = 0;
3728 it->end_charpos = XSTRING (it->string)->size;
3729 it->multibyte_p = STRING_MULTIBYTE (it->string);
3730 xassert (STRINGP (it->string));
3731 it->method = next_element_from_string;
3732 }
3733 else
3734 {
3735 it->string = Qnil;
3736 it->current.overlay_string_index = -1;
3737 it->method = next_element_from_buffer;
3738 }
3739
3740 CHECK_IT (it);
3741
3742 /* Value is non-zero if we found at least one overlay string. */
3743 return STRINGP (it->string);
3744 }
3745
3746
3747 \f
3748 /***********************************************************************
3749 Saving and restoring state
3750 ***********************************************************************/
3751
3752 /* Save current settings of IT on IT->stack. Called, for example,
3753 before setting up IT for an overlay string, to be able to restore
3754 IT's settings to what they were after the overlay string has been
3755 processed. */
3756
3757 static void
3758 push_it (it)
3759 struct it *it;
3760 {
3761 struct iterator_stack_entry *p;
3762
3763 xassert (it->sp < 2);
3764 p = it->stack + it->sp;
3765
3766 p->stop_charpos = it->stop_charpos;
3767 xassert (it->face_id >= 0);
3768 p->face_id = it->face_id;
3769 p->string = it->string;
3770 p->pos = it->current;
3771 p->end_charpos = it->end_charpos;
3772 p->string_nchars = it->string_nchars;
3773 p->area = it->area;
3774 p->multibyte_p = it->multibyte_p;
3775 p->space_width = it->space_width;
3776 p->font_height = it->font_height;
3777 p->voffset = it->voffset;
3778 p->string_from_display_prop_p = it->string_from_display_prop_p;
3779 p->display_ellipsis_p = 0;
3780 ++it->sp;
3781 }
3782
3783
3784 /* Restore IT's settings from IT->stack. Called, for example, when no
3785 more overlay strings must be processed, and we return to delivering
3786 display elements from a buffer, or when the end of a string from a
3787 `display' property is reached and we return to delivering display
3788 elements from an overlay string, or from a buffer. */
3789
3790 static void
3791 pop_it (it)
3792 struct it *it;
3793 {
3794 struct iterator_stack_entry *p;
3795
3796 xassert (it->sp > 0);
3797 --it->sp;
3798 p = it->stack + it->sp;
3799 it->stop_charpos = p->stop_charpos;
3800 it->face_id = p->face_id;
3801 it->string = p->string;
3802 it->current = p->pos;
3803 it->end_charpos = p->end_charpos;
3804 it->string_nchars = p->string_nchars;
3805 it->area = p->area;
3806 it->multibyte_p = p->multibyte_p;
3807 it->space_width = p->space_width;
3808 it->font_height = p->font_height;
3809 it->voffset = p->voffset;
3810 it->string_from_display_prop_p = p->string_from_display_prop_p;
3811 }
3812
3813
3814 \f
3815 /***********************************************************************
3816 Moving over lines
3817 ***********************************************************************/
3818
3819 /* Set IT's current position to the previous line start. */
3820
3821 static void
3822 back_to_previous_line_start (it)
3823 struct it *it;
3824 {
3825 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
3826 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3827 }
3828
3829
3830 /* Move IT to the next line start.
3831
3832 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
3833 we skipped over part of the text (as opposed to moving the iterator
3834 continuously over the text). Otherwise, don't change the value
3835 of *SKIPPED_P.
3836
3837 Newlines may come from buffer text, overlay strings, or strings
3838 displayed via the `display' property. That's the reason we can't
3839 simply use find_next_newline_no_quit.
3840
3841 Note that this function may not skip over invisible text that is so
3842 because of text properties and immediately follows a newline. If
3843 it would, function reseat_at_next_visible_line_start, when called
3844 from set_iterator_to_next, would effectively make invisible
3845 characters following a newline part of the wrong glyph row, which
3846 leads to wrong cursor motion. */
3847
3848 static int
3849 forward_to_next_line_start (it, skipped_p)
3850 struct it *it;
3851 int *skipped_p;
3852 {
3853 int old_selective, newline_found_p, n;
3854 const int MAX_NEWLINE_DISTANCE = 500;
3855
3856 /* If already on a newline, just consume it to avoid unintended
3857 skipping over invisible text below. */
3858 if (it->what == IT_CHARACTER
3859 && it->c == '\n'
3860 && CHARPOS (it->position) == IT_CHARPOS (*it))
3861 {
3862 set_iterator_to_next (it, 0);
3863 it->c = 0;
3864 return 1;
3865 }
3866
3867 /* Don't handle selective display in the following. It's (a)
3868 unnecessary because it's done by the caller, and (b) leads to an
3869 infinite recursion because next_element_from_ellipsis indirectly
3870 calls this function. */
3871 old_selective = it->selective;
3872 it->selective = 0;
3873
3874 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
3875 from buffer text. */
3876 for (n = newline_found_p = 0;
3877 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
3878 n += STRINGP (it->string) ? 0 : 1)
3879 {
3880 if (!get_next_display_element (it))
3881 break;
3882 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
3883 set_iterator_to_next (it, 0);
3884 }
3885
3886 /* If we didn't find a newline near enough, see if we can use a
3887 short-cut. */
3888 if (n == MAX_NEWLINE_DISTANCE)
3889 {
3890 int start = IT_CHARPOS (*it);
3891 int limit = find_next_newline_no_quit (start, 1);
3892 Lisp_Object pos;
3893
3894 xassert (!STRINGP (it->string));
3895
3896 /* If there isn't any `display' property in sight, and no
3897 overlays, we can just use the position of the newline in
3898 buffer text. */
3899 if (it->stop_charpos >= limit
3900 || ((pos = Fnext_single_property_change (make_number (start),
3901 Qdisplay,
3902 Qnil, make_number (limit)),
3903 NILP (pos))
3904 && next_overlay_change (start) == ZV))
3905 {
3906 IT_CHARPOS (*it) = limit;
3907 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
3908 *skipped_p = newline_found_p = 1;
3909 }
3910 else
3911 {
3912 while (get_next_display_element (it)
3913 && !newline_found_p)
3914 {
3915 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
3916 set_iterator_to_next (it, 0);
3917 }
3918 }
3919 }
3920
3921 it->selective = old_selective;
3922 return newline_found_p;
3923 }
3924
3925
3926 /* Set IT's current position to the previous visible line start. Skip
3927 invisible text that is so either due to text properties or due to
3928 selective display. Caution: this does not change IT->current_x and
3929 IT->hpos. */
3930
3931 static void
3932 back_to_previous_visible_line_start (it)
3933 struct it *it;
3934 {
3935 int visible_p = 0;
3936
3937 /* Go back one newline if not on BEGV already. */
3938 if (IT_CHARPOS (*it) > BEGV)
3939 back_to_previous_line_start (it);
3940
3941 /* Move over lines that are invisible because of selective display
3942 or text properties. */
3943 while (IT_CHARPOS (*it) > BEGV
3944 && !visible_p)
3945 {
3946 visible_p = 1;
3947
3948 /* If selective > 0, then lines indented more than that values
3949 are invisible. */
3950 if (it->selective > 0
3951 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3952 it->selective))
3953 visible_p = 0;
3954 else
3955 {
3956 Lisp_Object prop;
3957
3958 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
3959 Qinvisible, it->window);
3960 if (TEXT_PROP_MEANS_INVISIBLE (prop))
3961 visible_p = 0;
3962 }
3963
3964 /* Back one more newline if the current one is invisible. */
3965 if (!visible_p)
3966 back_to_previous_line_start (it);
3967 }
3968
3969 xassert (IT_CHARPOS (*it) >= BEGV);
3970 xassert (IT_CHARPOS (*it) == BEGV
3971 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3972 CHECK_IT (it);
3973 }
3974
3975
3976 /* Reseat iterator IT at the previous visible line start. Skip
3977 invisible text that is so either due to text properties or due to
3978 selective display. At the end, update IT's overlay information,
3979 face information etc. */
3980
3981 static void
3982 reseat_at_previous_visible_line_start (it)
3983 struct it *it;
3984 {
3985 back_to_previous_visible_line_start (it);
3986 reseat (it, it->current.pos, 1);
3987 CHECK_IT (it);
3988 }
3989
3990
3991 /* Reseat iterator IT on the next visible line start in the current
3992 buffer. ON_NEWLINE_P non-zero means position IT on the newline
3993 preceding the line start. Skip over invisible text that is so
3994 because of selective display. Compute faces, overlays etc at the
3995 new position. Note that this function does not skip over text that
3996 is invisible because of text properties. */
3997
3998 static void
3999 reseat_at_next_visible_line_start (it, on_newline_p)
4000 struct it *it;
4001 int on_newline_p;
4002 {
4003 int newline_found_p, skipped_p = 0;
4004
4005 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4006
4007 /* Skip over lines that are invisible because they are indented
4008 more than the value of IT->selective. */
4009 if (it->selective > 0)
4010 while (IT_CHARPOS (*it) < ZV
4011 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4012 it->selective))
4013 {
4014 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4015 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4016 }
4017
4018 /* Position on the newline if that's what's requested. */
4019 if (on_newline_p && newline_found_p)
4020 {
4021 if (STRINGP (it->string))
4022 {
4023 if (IT_STRING_CHARPOS (*it) > 0)
4024 {
4025 --IT_STRING_CHARPOS (*it);
4026 --IT_STRING_BYTEPOS (*it);
4027 }
4028 }
4029 else if (IT_CHARPOS (*it) > BEGV)
4030 {
4031 --IT_CHARPOS (*it);
4032 --IT_BYTEPOS (*it);
4033 reseat (it, it->current.pos, 0);
4034 }
4035 }
4036 else if (skipped_p)
4037 reseat (it, it->current.pos, 0);
4038
4039 CHECK_IT (it);
4040 }
4041
4042
4043 \f
4044 /***********************************************************************
4045 Changing an iterator's position
4046 ***********************************************************************/
4047
4048 /* Change IT's current position to POS in current_buffer. If FORCE_P
4049 is non-zero, always check for text properties at the new position.
4050 Otherwise, text properties are only looked up if POS >=
4051 IT->check_charpos of a property. */
4052
4053 static void
4054 reseat (it, pos, force_p)
4055 struct it *it;
4056 struct text_pos pos;
4057 int force_p;
4058 {
4059 int original_pos = IT_CHARPOS (*it);
4060
4061 reseat_1 (it, pos, 0);
4062
4063 /* Determine where to check text properties. Avoid doing it
4064 where possible because text property lookup is very expensive. */
4065 if (force_p
4066 || CHARPOS (pos) > it->stop_charpos
4067 || CHARPOS (pos) < original_pos)
4068 handle_stop (it);
4069
4070 CHECK_IT (it);
4071 }
4072
4073
4074 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4075 IT->stop_pos to POS, also. */
4076
4077 static void
4078 reseat_1 (it, pos, set_stop_p)
4079 struct it *it;
4080 struct text_pos pos;
4081 int set_stop_p;
4082 {
4083 /* Don't call this function when scanning a C string. */
4084 xassert (it->s == NULL);
4085
4086 /* POS must be a reasonable value. */
4087 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4088
4089 it->current.pos = it->position = pos;
4090 XSETBUFFER (it->object, current_buffer);
4091 it->end_charpos = ZV;
4092 it->dpvec = NULL;
4093 it->current.dpvec_index = -1;
4094 it->current.overlay_string_index = -1;
4095 IT_STRING_CHARPOS (*it) = -1;
4096 IT_STRING_BYTEPOS (*it) = -1;
4097 it->string = Qnil;
4098 it->method = next_element_from_buffer;
4099 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
4100 it->sp = 0;
4101 it->face_before_selective_p = 0;
4102
4103 if (set_stop_p)
4104 it->stop_charpos = CHARPOS (pos);
4105 }
4106
4107
4108 /* Set up IT for displaying a string, starting at CHARPOS in window W.
4109 If S is non-null, it is a C string to iterate over. Otherwise,
4110 STRING gives a Lisp string to iterate over.
4111
4112 If PRECISION > 0, don't return more then PRECISION number of
4113 characters from the string.
4114
4115 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4116 characters have been returned. FIELD_WIDTH < 0 means an infinite
4117 field width.
4118
4119 MULTIBYTE = 0 means disable processing of multibyte characters,
4120 MULTIBYTE > 0 means enable it,
4121 MULTIBYTE < 0 means use IT->multibyte_p.
4122
4123 IT must be initialized via a prior call to init_iterator before
4124 calling this function. */
4125
4126 static void
4127 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4128 struct it *it;
4129 unsigned char *s;
4130 Lisp_Object string;
4131 int charpos;
4132 int precision, field_width, multibyte;
4133 {
4134 /* No region in strings. */
4135 it->region_beg_charpos = it->region_end_charpos = -1;
4136
4137 /* No text property checks performed by default, but see below. */
4138 it->stop_charpos = -1;
4139
4140 /* Set iterator position and end position. */
4141 bzero (&it->current, sizeof it->current);
4142 it->current.overlay_string_index = -1;
4143 it->current.dpvec_index = -1;
4144 xassert (charpos >= 0);
4145
4146 /* If STRING is specified, use its multibyteness, otherwise use the
4147 setting of MULTIBYTE, if specified. */
4148 if (multibyte >= 0)
4149 it->multibyte_p = multibyte > 0;
4150
4151 if (s == NULL)
4152 {
4153 xassert (STRINGP (string));
4154 it->string = string;
4155 it->s = NULL;
4156 it->end_charpos = it->string_nchars = XSTRING (string)->size;
4157 it->method = next_element_from_string;
4158 it->current.string_pos = string_pos (charpos, string);
4159 }
4160 else
4161 {
4162 it->s = s;
4163 it->string = Qnil;
4164
4165 /* Note that we use IT->current.pos, not it->current.string_pos,
4166 for displaying C strings. */
4167 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4168 if (it->multibyte_p)
4169 {
4170 it->current.pos = c_string_pos (charpos, s, 1);
4171 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4172 }
4173 else
4174 {
4175 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4176 it->end_charpos = it->string_nchars = strlen (s);
4177 }
4178
4179 it->method = next_element_from_c_string;
4180 }
4181
4182 /* PRECISION > 0 means don't return more than PRECISION characters
4183 from the string. */
4184 if (precision > 0 && it->end_charpos - charpos > precision)
4185 it->end_charpos = it->string_nchars = charpos + precision;
4186
4187 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4188 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4189 FIELD_WIDTH < 0 means infinite field width. This is useful for
4190 padding with `-' at the end of a mode line. */
4191 if (field_width < 0)
4192 field_width = INFINITY;
4193 if (field_width > it->end_charpos - charpos)
4194 it->end_charpos = charpos + field_width;
4195
4196 /* Use the standard display table for displaying strings. */
4197 if (DISP_TABLE_P (Vstandard_display_table))
4198 it->dp = XCHAR_TABLE (Vstandard_display_table);
4199
4200 it->stop_charpos = charpos;
4201 CHECK_IT (it);
4202 }
4203
4204
4205 \f
4206 /***********************************************************************
4207 Iteration
4208 ***********************************************************************/
4209
4210 /* Load IT's display element fields with information about the next
4211 display element from the current position of IT. Value is zero if
4212 end of buffer (or C string) is reached. */
4213
4214 int
4215 get_next_display_element (it)
4216 struct it *it;
4217 {
4218 /* Non-zero means that we found an display element. Zero means that
4219 we hit the end of what we iterate over. Performance note: the
4220 function pointer `method' used here turns out to be faster than
4221 using a sequence of if-statements. */
4222 int success_p = (*it->method) (it);
4223
4224 if (it->what == IT_CHARACTER)
4225 {
4226 /* Map via display table or translate control characters.
4227 IT->c, IT->len etc. have been set to the next character by
4228 the function call above. If we have a display table, and it
4229 contains an entry for IT->c, translate it. Don't do this if
4230 IT->c itself comes from a display table, otherwise we could
4231 end up in an infinite recursion. (An alternative could be to
4232 count the recursion depth of this function and signal an
4233 error when a certain maximum depth is reached.) Is it worth
4234 it? */
4235 if (success_p && it->dpvec == NULL)
4236 {
4237 Lisp_Object dv;
4238
4239 if (it->dp
4240 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
4241 VECTORP (dv)))
4242 {
4243 struct Lisp_Vector *v = XVECTOR (dv);
4244
4245 /* Return the first character from the display table
4246 entry, if not empty. If empty, don't display the
4247 current character. */
4248 if (v->size)
4249 {
4250 it->dpvec_char_len = it->len;
4251 it->dpvec = v->contents;
4252 it->dpend = v->contents + v->size;
4253 it->current.dpvec_index = 0;
4254 it->method = next_element_from_display_vector;
4255 success_p = get_next_display_element (it);
4256 }
4257 else
4258 {
4259 set_iterator_to_next (it, 0);
4260 success_p = get_next_display_element (it);
4261 }
4262 }
4263
4264 /* Translate control characters into `\003' or `^C' form.
4265 Control characters coming from a display table entry are
4266 currently not translated because we use IT->dpvec to hold
4267 the translation. This could easily be changed but I
4268 don't believe that it is worth doing.
4269
4270 Non-printable multibyte characters are also translated
4271 octal form. */
4272 else if ((it->c < ' '
4273 && (it->area != TEXT_AREA
4274 || (it->c != '\n' && it->c != '\t')))
4275 || (it->c != '\n' && it->c != '\t'
4276 && (it->multibyte_p ? !CHAR_PRINTABLE_P (it->c)
4277 : it->c == 127)))
4278 {
4279 /* IT->c is a control character which must be displayed
4280 either as '\003' or as `^C' where the '\\' and '^'
4281 can be defined in the display table. Fill
4282 IT->ctl_chars with glyphs for what we have to
4283 display. Then, set IT->dpvec to these glyphs. */
4284 GLYPH g;
4285
4286 if (it->c < 128 && it->ctl_arrow_p)
4287 {
4288 /* Set IT->ctl_chars[0] to the glyph for `^'. */
4289 if (it->dp
4290 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
4291 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
4292 g = XINT (DISP_CTRL_GLYPH (it->dp));
4293 else
4294 g = FAST_MAKE_GLYPH ('^', 0);
4295 XSETINT (it->ctl_chars[0], g);
4296
4297 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
4298 XSETINT (it->ctl_chars[1], g);
4299
4300 /* Set up IT->dpvec and return first character from it. */
4301 it->dpvec_char_len = it->len;
4302 it->dpvec = it->ctl_chars;
4303 it->dpend = it->dpvec + 2;
4304 it->current.dpvec_index = 0;
4305 it->method = next_element_from_display_vector;
4306 get_next_display_element (it);
4307 }
4308 else
4309 {
4310 unsigned char str[MAX_MULTIBYTE_LENGTH];
4311 int len;
4312 int i;
4313 GLYPH escape_glyph;
4314
4315 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
4316 if (it->dp
4317 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
4318 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
4319 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
4320 else
4321 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
4322
4323 if (CHAR_BYTE8_P (it->c))
4324 {
4325 str[0] = CHAR_TO_BYTE8 (it->c);
4326 len = 1;
4327 }
4328 else if (it->c < 256)
4329 {
4330 str[0] = it->c;
4331 len = 1;
4332 }
4333 else
4334 {
4335 /* It's an invalid character, which
4336 shouldn't happen actually, but due to
4337 bugs it may happen. Let's print the char
4338 as is, there's not much meaningful we can
4339 do with it. */
4340 str[0] = it->c;
4341 str[1] = it->c >> 8;
4342 str[2] = it->c >> 16;
4343 str[3] = it->c >> 24;
4344 len = 4;
4345 }
4346
4347 for (i = 0; i < len; i++)
4348 {
4349 XSETINT (it->ctl_chars[i * 4], escape_glyph);
4350 /* Insert three more glyphs into IT->ctl_chars for
4351 the octal display of the character. */
4352 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
4353 XSETINT (it->ctl_chars[i * 4 + 1], g);
4354 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
4355 XSETINT (it->ctl_chars[i * 4 + 2], g);
4356 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
4357 XSETINT (it->ctl_chars[i * 4 + 3], g);
4358 }
4359
4360 /* Set up IT->dpvec and return the first character
4361 from it. */
4362 it->dpvec_char_len = it->len;
4363 it->dpvec = it->ctl_chars;
4364 it->dpend = it->dpvec + len * 4;
4365 it->current.dpvec_index = 0;
4366 it->method = next_element_from_display_vector;
4367 get_next_display_element (it);
4368 }
4369 }
4370 }
4371
4372 /* Adjust face id for a multibyte character. There are no
4373 multibyte character in unibyte text. */
4374 if (it->multibyte_p
4375 && success_p
4376 && FRAME_WINDOW_P (it->f))
4377 {
4378 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4379 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
4380 }
4381 }
4382
4383 /* Is this character the last one of a run of characters with
4384 box? If yes, set IT->end_of_box_run_p to 1. */
4385 if (it->face_box_p
4386 && it->s == NULL)
4387 {
4388 int face_id;
4389 struct face *face;
4390
4391 it->end_of_box_run_p
4392 = ((face_id = face_after_it_pos (it),
4393 face_id != it->face_id)
4394 && (face = FACE_FROM_ID (it->f, face_id),
4395 face->box == FACE_NO_BOX));
4396 }
4397
4398 /* Value is 0 if end of buffer or string reached. */
4399 return success_p;
4400 }
4401
4402
4403 /* Move IT to the next display element.
4404
4405 RESEAT_P non-zero means if called on a newline in buffer text,
4406 skip to the next visible line start.
4407
4408 Functions get_next_display_element and set_iterator_to_next are
4409 separate because I find this arrangement easier to handle than a
4410 get_next_display_element function that also increments IT's
4411 position. The way it is we can first look at an iterator's current
4412 display element, decide whether it fits on a line, and if it does,
4413 increment the iterator position. The other way around we probably
4414 would either need a flag indicating whether the iterator has to be
4415 incremented the next time, or we would have to implement a
4416 decrement position function which would not be easy to write. */
4417
4418 void
4419 set_iterator_to_next (it, reseat_p)
4420 struct it *it;
4421 int reseat_p;
4422 {
4423 /* Reset flags indicating start and end of a sequence of characters
4424 with box. Reset them at the start of this function because
4425 moving the iterator to a new position might set them. */
4426 it->start_of_box_run_p = it->end_of_box_run_p = 0;
4427
4428 if (it->method == next_element_from_buffer)
4429 {
4430 /* The current display element of IT is a character from
4431 current_buffer. Advance in the buffer, and maybe skip over
4432 invisible lines that are so because of selective display. */
4433 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
4434 reseat_at_next_visible_line_start (it, 0);
4435 else
4436 {
4437 xassert (it->len != 0);
4438 IT_BYTEPOS (*it) += it->len;
4439 IT_CHARPOS (*it) += 1;
4440 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
4441 }
4442 }
4443 else if (it->method == next_element_from_composition)
4444 {
4445 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
4446 if (STRINGP (it->string))
4447 {
4448 IT_STRING_BYTEPOS (*it) += it->len;
4449 IT_STRING_CHARPOS (*it) += it->cmp_len;
4450 it->method = next_element_from_string;
4451 goto consider_string_end;
4452 }
4453 else
4454 {
4455 IT_BYTEPOS (*it) += it->len;
4456 IT_CHARPOS (*it) += it->cmp_len;
4457 it->method = next_element_from_buffer;
4458 }
4459 }
4460 else if (it->method == next_element_from_c_string)
4461 {
4462 /* Current display element of IT is from a C string. */
4463 IT_BYTEPOS (*it) += it->len;
4464 IT_CHARPOS (*it) += 1;
4465 }
4466 else if (it->method == next_element_from_display_vector)
4467 {
4468 /* Current display element of IT is from a display table entry.
4469 Advance in the display table definition. Reset it to null if
4470 end reached, and continue with characters from buffers/
4471 strings. */
4472 ++it->current.dpvec_index;
4473
4474 /* Restore face of the iterator to what they were before the
4475 display vector entry (these entries may contain faces). */
4476 it->face_id = it->saved_face_id;
4477
4478 if (it->dpvec + it->current.dpvec_index == it->dpend)
4479 {
4480 if (it->s)
4481 it->method = next_element_from_c_string;
4482 else if (STRINGP (it->string))
4483 it->method = next_element_from_string;
4484 else
4485 it->method = next_element_from_buffer;
4486
4487 it->dpvec = NULL;
4488 it->current.dpvec_index = -1;
4489
4490 /* Skip over characters which were displayed via IT->dpvec. */
4491 if (it->dpvec_char_len < 0)
4492 reseat_at_next_visible_line_start (it, 1);
4493 else if (it->dpvec_char_len > 0)
4494 {
4495 it->len = it->dpvec_char_len;
4496 set_iterator_to_next (it, reseat_p);
4497 }
4498 }
4499 }
4500 else if (it->method == next_element_from_string)
4501 {
4502 /* Current display element is a character from a Lisp string. */
4503 xassert (it->s == NULL && STRINGP (it->string));
4504 IT_STRING_BYTEPOS (*it) += it->len;
4505 IT_STRING_CHARPOS (*it) += 1;
4506
4507 consider_string_end:
4508
4509 if (it->current.overlay_string_index >= 0)
4510 {
4511 /* IT->string is an overlay string. Advance to the
4512 next, if there is one. */
4513 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
4514 next_overlay_string (it);
4515 }
4516 else
4517 {
4518 /* IT->string is not an overlay string. If we reached
4519 its end, and there is something on IT->stack, proceed
4520 with what is on the stack. This can be either another
4521 string, this time an overlay string, or a buffer. */
4522 if (IT_STRING_CHARPOS (*it) == XSTRING (it->string)->size
4523 && it->sp > 0)
4524 {
4525 pop_it (it);
4526 if (!STRINGP (it->string))
4527 it->method = next_element_from_buffer;
4528 else
4529 goto consider_string_end;
4530 }
4531 }
4532 }
4533 else if (it->method == next_element_from_image
4534 || it->method == next_element_from_stretch)
4535 {
4536 /* The position etc with which we have to proceed are on
4537 the stack. The position may be at the end of a string,
4538 if the `display' property takes up the whole string. */
4539 pop_it (it);
4540 it->image_id = 0;
4541 if (STRINGP (it->string))
4542 {
4543 it->method = next_element_from_string;
4544 goto consider_string_end;
4545 }
4546 else
4547 it->method = next_element_from_buffer;
4548 }
4549 else
4550 /* There are no other methods defined, so this should be a bug. */
4551 abort ();
4552
4553 xassert (it->method != next_element_from_string
4554 || (STRINGP (it->string)
4555 && IT_STRING_CHARPOS (*it) >= 0));
4556 }
4557
4558
4559 /* Load IT's display element fields with information about the next
4560 display element which comes from a display table entry or from the
4561 result of translating a control character to one of the forms `^C'
4562 or `\003'. IT->dpvec holds the glyphs to return as characters. */
4563
4564 static int
4565 next_element_from_display_vector (it)
4566 struct it *it;
4567 {
4568 /* Precondition. */
4569 xassert (it->dpvec && it->current.dpvec_index >= 0);
4570
4571 /* Remember the current face id in case glyphs specify faces.
4572 IT's face is restored in set_iterator_to_next. */
4573 it->saved_face_id = it->face_id;
4574
4575 if (INTEGERP (*it->dpvec)
4576 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
4577 {
4578 int lface_id;
4579 GLYPH g;
4580
4581 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
4582 it->c = FAST_GLYPH_CHAR (g);
4583 it->len = CHAR_BYTES (it->c);
4584
4585 /* The entry may contain a face id to use. Such a face id is
4586 the id of a Lisp face, not a realized face. A face id of
4587 zero means no face is specified. */
4588 lface_id = FAST_GLYPH_FACE (g);
4589 if (lface_id)
4590 {
4591 /* The function returns -1 if lface_id is invalid. */
4592 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
4593 if (face_id >= 0)
4594 it->face_id = face_id;
4595 }
4596 }
4597 else
4598 /* Display table entry is invalid. Return a space. */
4599 it->c = ' ', it->len = 1;
4600
4601 /* Don't change position and object of the iterator here. They are
4602 still the values of the character that had this display table
4603 entry or was translated, and that's what we want. */
4604 it->what = IT_CHARACTER;
4605 return 1;
4606 }
4607
4608
4609 /* Load IT with the next display element from Lisp string IT->string.
4610 IT->current.string_pos is the current position within the string.
4611 If IT->current.overlay_string_index >= 0, the Lisp string is an
4612 overlay string. */
4613
4614 static int
4615 next_element_from_string (it)
4616 struct it *it;
4617 {
4618 struct text_pos position;
4619
4620 xassert (STRINGP (it->string));
4621 xassert (IT_STRING_CHARPOS (*it) >= 0);
4622 position = it->current.string_pos;
4623
4624 /* Time to check for invisible text? */
4625 if (IT_STRING_CHARPOS (*it) < it->end_charpos
4626 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
4627 {
4628 handle_stop (it);
4629
4630 /* Since a handler may have changed IT->method, we must
4631 recurse here. */
4632 return get_next_display_element (it);
4633 }
4634
4635 if (it->current.overlay_string_index >= 0)
4636 {
4637 /* Get the next character from an overlay string. In overlay
4638 strings, There is no field width or padding with spaces to
4639 do. */
4640 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
4641 {
4642 it->what = IT_EOB;
4643 return 0;
4644 }
4645 else if (STRING_MULTIBYTE (it->string))
4646 {
4647 int remaining = (STRING_BYTES (XSTRING (it->string))
4648 - IT_STRING_BYTEPOS (*it));
4649 unsigned char *s = (XSTRING (it->string)->data
4650 + IT_STRING_BYTEPOS (*it));
4651 it->c = string_char_and_length (s, remaining, &it->len);
4652 }
4653 else
4654 {
4655 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
4656 it->len = 1;
4657 }
4658 }
4659 else
4660 {
4661 /* Get the next character from a Lisp string that is not an
4662 overlay string. Such strings come from the mode line, for
4663 example. We may have to pad with spaces, or truncate the
4664 string. See also next_element_from_c_string. */
4665 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
4666 {
4667 it->what = IT_EOB;
4668 return 0;
4669 }
4670 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
4671 {
4672 /* Pad with spaces. */
4673 it->c = ' ', it->len = 1;
4674 CHARPOS (position) = BYTEPOS (position) = -1;
4675 }
4676 else if (STRING_MULTIBYTE (it->string))
4677 {
4678 int maxlen = (STRING_BYTES (XSTRING (it->string))
4679 - IT_STRING_BYTEPOS (*it));
4680 unsigned char *s = (XSTRING (it->string)->data
4681 + IT_STRING_BYTEPOS (*it));
4682 it->c = string_char_and_length (s, maxlen, &it->len);
4683 }
4684 else
4685 {
4686 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
4687 it->len = 1;
4688 }
4689 }
4690
4691 /* Record what we have and where it came from. Note that we store a
4692 buffer position in IT->position although it could arguably be a
4693 string position. */
4694 it->what = IT_CHARACTER;
4695 it->object = it->string;
4696 it->position = position;
4697 return 1;
4698 }
4699
4700
4701 /* Load IT with next display element from C string IT->s.
4702 IT->string_nchars is the maximum number of characters to return
4703 from the string. IT->end_charpos may be greater than
4704 IT->string_nchars when this function is called, in which case we
4705 may have to return padding spaces. Value is zero if end of string
4706 reached, including padding spaces. */
4707
4708 static int
4709 next_element_from_c_string (it)
4710 struct it *it;
4711 {
4712 int success_p = 1;
4713
4714 xassert (it->s);
4715 it->what = IT_CHARACTER;
4716 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
4717 it->object = Qnil;
4718
4719 /* IT's position can be greater IT->string_nchars in case a field
4720 width or precision has been specified when the iterator was
4721 initialized. */
4722 if (IT_CHARPOS (*it) >= it->end_charpos)
4723 {
4724 /* End of the game. */
4725 it->what = IT_EOB;
4726 success_p = 0;
4727 }
4728 else if (IT_CHARPOS (*it) >= it->string_nchars)
4729 {
4730 /* Pad with spaces. */
4731 it->c = ' ', it->len = 1;
4732 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
4733 }
4734 else if (it->multibyte_p)
4735 {
4736 /* Implementation note: The calls to strlen apparently aren't a
4737 performance problem because there is no noticeable performance
4738 difference between Emacs running in unibyte or multibyte mode. */
4739 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
4740 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
4741 maxlen, &it->len);
4742 }
4743 else
4744 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
4745
4746 return success_p;
4747 }
4748
4749
4750 /* Set up IT to return characters from an ellipsis, if appropriate.
4751 The definition of the ellipsis glyphs may come from a display table
4752 entry. This function Fills IT with the first glyph from the
4753 ellipsis if an ellipsis is to be displayed. */
4754
4755 static int
4756 next_element_from_ellipsis (it)
4757 struct it *it;
4758 {
4759 if (it->selective_display_ellipsis_p)
4760 {
4761 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4762 {
4763 /* Use the display table definition for `...'. Invalid glyphs
4764 will be handled by the method returning elements from dpvec. */
4765 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4766 it->dpvec_char_len = it->len;
4767 it->dpvec = v->contents;
4768 it->dpend = v->contents + v->size;
4769 it->current.dpvec_index = 0;
4770 it->method = next_element_from_display_vector;
4771 }
4772 else
4773 {
4774 /* Use default `...' which is stored in default_invis_vector. */
4775 it->dpvec_char_len = it->len;
4776 it->dpvec = default_invis_vector;
4777 it->dpend = default_invis_vector + 3;
4778 it->current.dpvec_index = 0;
4779 it->method = next_element_from_display_vector;
4780 }
4781 }
4782 else
4783 {
4784 /* The face at the current position may be different from the
4785 face we find after the invisible text. Remember what it
4786 was in IT->saved_face_id, and signal that it's there by
4787 setting face_before_selective_p. */
4788 it->saved_face_id = it->face_id;
4789 it->method = next_element_from_buffer;
4790 reseat_at_next_visible_line_start (it, 1);
4791 it->face_before_selective_p = 1;
4792 }
4793
4794 return get_next_display_element (it);
4795 }
4796
4797
4798 /* Deliver an image display element. The iterator IT is already
4799 filled with image information (done in handle_display_prop). Value
4800 is always 1. */
4801
4802
4803 static int
4804 next_element_from_image (it)
4805 struct it *it;
4806 {
4807 it->what = IT_IMAGE;
4808 return 1;
4809 }
4810
4811
4812 /* Fill iterator IT with next display element from a stretch glyph
4813 property. IT->object is the value of the text property. Value is
4814 always 1. */
4815
4816 static int
4817 next_element_from_stretch (it)
4818 struct it *it;
4819 {
4820 it->what = IT_STRETCH;
4821 return 1;
4822 }
4823
4824
4825 /* Load IT with the next display element from current_buffer. Value
4826 is zero if end of buffer reached. IT->stop_charpos is the next
4827 position at which to stop and check for text properties or buffer
4828 end. */
4829
4830 static int
4831 next_element_from_buffer (it)
4832 struct it *it;
4833 {
4834 int success_p = 1;
4835
4836 /* Check this assumption, otherwise, we would never enter the
4837 if-statement, below. */
4838 xassert (IT_CHARPOS (*it) >= BEGV
4839 && IT_CHARPOS (*it) <= it->stop_charpos);
4840
4841 if (IT_CHARPOS (*it) >= it->stop_charpos)
4842 {
4843 if (IT_CHARPOS (*it) >= it->end_charpos)
4844 {
4845 int overlay_strings_follow_p;
4846
4847 /* End of the game, except when overlay strings follow that
4848 haven't been returned yet. */
4849 if (it->overlay_strings_at_end_processed_p)
4850 overlay_strings_follow_p = 0;
4851 else
4852 {
4853 it->overlay_strings_at_end_processed_p = 1;
4854 overlay_strings_follow_p = get_overlay_strings (it, 0);
4855 }
4856
4857 if (overlay_strings_follow_p)
4858 success_p = get_next_display_element (it);
4859 else
4860 {
4861 it->what = IT_EOB;
4862 it->position = it->current.pos;
4863 success_p = 0;
4864 }
4865 }
4866 else
4867 {
4868 handle_stop (it);
4869 return get_next_display_element (it);
4870 }
4871 }
4872 else
4873 {
4874 /* No face changes, overlays etc. in sight, so just return a
4875 character from current_buffer. */
4876 unsigned char *p;
4877
4878 /* Maybe run the redisplay end trigger hook. Performance note:
4879 This doesn't seem to cost measurable time. */
4880 if (it->redisplay_end_trigger_charpos
4881 && it->glyph_row
4882 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
4883 run_redisplay_end_trigger_hook (it);
4884
4885 /* Get the next character, maybe multibyte. */
4886 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
4887 if (it->multibyte_p && !ASCII_BYTE_P (*p))
4888 {
4889 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
4890 - IT_BYTEPOS (*it));
4891 it->c = string_char_and_length (p, maxlen, &it->len);
4892 }
4893 else
4894 it->c = *p, it->len = 1;
4895
4896 /* Record what we have and where it came from. */
4897 it->what = IT_CHARACTER;;
4898 it->object = it->w->buffer;
4899 it->position = it->current.pos;
4900
4901 /* Normally we return the character found above, except when we
4902 really want to return an ellipsis for selective display. */
4903 if (it->selective)
4904 {
4905 if (it->c == '\n')
4906 {
4907 /* A value of selective > 0 means hide lines indented more
4908 than that number of columns. */
4909 if (it->selective > 0
4910 && IT_CHARPOS (*it) + 1 < ZV
4911 && indented_beyond_p (IT_CHARPOS (*it) + 1,
4912 IT_BYTEPOS (*it) + 1,
4913 it->selective))
4914 {
4915 success_p = next_element_from_ellipsis (it);
4916 it->dpvec_char_len = -1;
4917 }
4918 }
4919 else if (it->c == '\r' && it->selective == -1)
4920 {
4921 /* A value of selective == -1 means that everything from the
4922 CR to the end of the line is invisible, with maybe an
4923 ellipsis displayed for it. */
4924 success_p = next_element_from_ellipsis (it);
4925 it->dpvec_char_len = -1;
4926 }
4927 }
4928 }
4929
4930 /* Value is zero if end of buffer reached. */
4931 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
4932 return success_p;
4933 }
4934
4935
4936 /* Run the redisplay end trigger hook for IT. */
4937
4938 static void
4939 run_redisplay_end_trigger_hook (it)
4940 struct it *it;
4941 {
4942 Lisp_Object args[3];
4943
4944 /* IT->glyph_row should be non-null, i.e. we should be actually
4945 displaying something, or otherwise we should not run the hook. */
4946 xassert (it->glyph_row);
4947
4948 /* Set up hook arguments. */
4949 args[0] = Qredisplay_end_trigger_functions;
4950 args[1] = it->window;
4951 XSETINT (args[2], it->redisplay_end_trigger_charpos);
4952 it->redisplay_end_trigger_charpos = 0;
4953
4954 /* Since we are *trying* to run these functions, don't try to run
4955 them again, even if they get an error. */
4956 it->w->redisplay_end_trigger = Qnil;
4957 Frun_hook_with_args (3, args);
4958
4959 /* Notice if it changed the face of the character we are on. */
4960 handle_face_prop (it);
4961 }
4962
4963
4964 /* Deliver a composition display element. The iterator IT is already
4965 filled with composition information (done in
4966 handle_composition_prop). Value is always 1. */
4967
4968 static int
4969 next_element_from_composition (it)
4970 struct it *it;
4971 {
4972 it->what = IT_COMPOSITION;
4973 it->position = (STRINGP (it->string)
4974 ? it->current.string_pos
4975 : it->current.pos);
4976 return 1;
4977 }
4978
4979
4980 \f
4981 /***********************************************************************
4982 Moving an iterator without producing glyphs
4983 ***********************************************************************/
4984
4985 /* Move iterator IT to a specified buffer or X position within one
4986 line on the display without producing glyphs.
4987
4988 Begin to skip at IT's current position. Skip to TO_CHARPOS or TO_X
4989 whichever is reached first.
4990
4991 TO_CHARPOS <= 0 means no TO_CHARPOS is specified.
4992
4993 TO_X < 0 means that no TO_X is specified. TO_X is normally a value
4994 0 <= TO_X <= IT->last_visible_x. This means in particular, that
4995 TO_X includes the amount by which a window is horizontally
4996 scrolled.
4997
4998 Value is
4999
5000 MOVE_POS_MATCH_OR_ZV
5001 - when TO_POS or ZV was reached.
5002
5003 MOVE_X_REACHED
5004 -when TO_X was reached before TO_POS or ZV were reached.
5005
5006 MOVE_LINE_CONTINUED
5007 - when we reached the end of the display area and the line must
5008 be continued.
5009
5010 MOVE_LINE_TRUNCATED
5011 - when we reached the end of the display area and the line is
5012 truncated.
5013
5014 MOVE_NEWLINE_OR_CR
5015 - when we stopped at a line end, i.e. a newline or a CR and selective
5016 display is on. */
5017
5018 static enum move_it_result
5019 move_it_in_display_line_to (it, to_charpos, to_x, op)
5020 struct it *it;
5021 int to_charpos, to_x, op;
5022 {
5023 enum move_it_result result = MOVE_UNDEFINED;
5024 struct glyph_row *saved_glyph_row;
5025
5026 /* Don't produce glyphs in produce_glyphs. */
5027 saved_glyph_row = it->glyph_row;
5028 it->glyph_row = NULL;
5029
5030 while (1)
5031 {
5032 int x, i, ascent = 0, descent = 0;
5033
5034 /* Stop when ZV or TO_CHARPOS reached. */
5035 if (!get_next_display_element (it)
5036 || ((op & MOVE_TO_POS) != 0
5037 && BUFFERP (it->object)
5038 && IT_CHARPOS (*it) >= to_charpos))
5039 {
5040 result = MOVE_POS_MATCH_OR_ZV;
5041 break;
5042 }
5043
5044 /* The call to produce_glyphs will get the metrics of the
5045 display element IT is loaded with. We record in x the
5046 x-position before this display element in case it does not
5047 fit on the line. */
5048 x = it->current_x;
5049
5050 /* Remember the line height so far in case the next element doesn't
5051 fit on the line. */
5052 if (!it->truncate_lines_p)
5053 {
5054 ascent = it->max_ascent;
5055 descent = it->max_descent;
5056 }
5057
5058 PRODUCE_GLYPHS (it);
5059
5060 if (it->area != TEXT_AREA)
5061 {
5062 set_iterator_to_next (it, 1);
5063 continue;
5064 }
5065
5066 /* The number of glyphs we get back in IT->nglyphs will normally
5067 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
5068 character on a terminal frame, or (iii) a line end. For the
5069 second case, IT->nglyphs - 1 padding glyphs will be present
5070 (on X frames, there is only one glyph produced for a
5071 composite character.
5072
5073 The behavior implemented below means, for continuation lines,
5074 that as many spaces of a TAB as fit on the current line are
5075 displayed there. For terminal frames, as many glyphs of a
5076 multi-glyph character are displayed in the current line, too.
5077 This is what the old redisplay code did, and we keep it that
5078 way. Under X, the whole shape of a complex character must
5079 fit on the line or it will be completely displayed in the
5080 next line.
5081
5082 Note that both for tabs and padding glyphs, all glyphs have
5083 the same width. */
5084 if (it->nglyphs)
5085 {
5086 /* More than one glyph or glyph doesn't fit on line. All
5087 glyphs have the same width. */
5088 int single_glyph_width = it->pixel_width / it->nglyphs;
5089 int new_x;
5090
5091 for (i = 0; i < it->nglyphs; ++i, x = new_x)
5092 {
5093 new_x = x + single_glyph_width;
5094
5095 /* We want to leave anything reaching TO_X to the caller. */
5096 if ((op & MOVE_TO_X) && new_x > to_x)
5097 {
5098 it->current_x = x;
5099 result = MOVE_X_REACHED;
5100 break;
5101 }
5102 else if (/* Lines are continued. */
5103 !it->truncate_lines_p
5104 && (/* And glyph doesn't fit on the line. */
5105 new_x > it->last_visible_x
5106 /* Or it fits exactly and we're on a window
5107 system frame. */
5108 || (new_x == it->last_visible_x
5109 && FRAME_WINDOW_P (it->f))))
5110 {
5111 if (/* IT->hpos == 0 means the very first glyph
5112 doesn't fit on the line, e.g. a wide image. */
5113 it->hpos == 0
5114 || (new_x == it->last_visible_x
5115 && FRAME_WINDOW_P (it->f)))
5116 {
5117 ++it->hpos;
5118 it->current_x = new_x;
5119 if (i == it->nglyphs - 1)
5120 set_iterator_to_next (it, 1);
5121 }
5122 else
5123 {
5124 it->current_x = x;
5125 it->max_ascent = ascent;
5126 it->max_descent = descent;
5127 }
5128
5129 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
5130 IT_CHARPOS (*it)));
5131 result = MOVE_LINE_CONTINUED;
5132 break;
5133 }
5134 else if (new_x > it->first_visible_x)
5135 {
5136 /* Glyph is visible. Increment number of glyphs that
5137 would be displayed. */
5138 ++it->hpos;
5139 }
5140 else
5141 {
5142 /* Glyph is completely off the left margin of the display
5143 area. Nothing to do. */
5144 }
5145 }
5146
5147 if (result != MOVE_UNDEFINED)
5148 break;
5149 }
5150 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
5151 {
5152 /* Stop when TO_X specified and reached. This check is
5153 necessary here because of lines consisting of a line end,
5154 only. The line end will not produce any glyphs and we
5155 would never get MOVE_X_REACHED. */
5156 xassert (it->nglyphs == 0);
5157 result = MOVE_X_REACHED;
5158 break;
5159 }
5160
5161 /* Is this a line end? If yes, we're done. */
5162 if (ITERATOR_AT_END_OF_LINE_P (it))
5163 {
5164 result = MOVE_NEWLINE_OR_CR;
5165 break;
5166 }
5167
5168 /* The current display element has been consumed. Advance
5169 to the next. */
5170 set_iterator_to_next (it, 1);
5171
5172 /* Stop if lines are truncated and IT's current x-position is
5173 past the right edge of the window now. */
5174 if (it->truncate_lines_p
5175 && it->current_x >= it->last_visible_x)
5176 {
5177 result = MOVE_LINE_TRUNCATED;
5178 break;
5179 }
5180 }
5181
5182 /* Restore the iterator settings altered at the beginning of this
5183 function. */
5184 it->glyph_row = saved_glyph_row;
5185 return result;
5186 }
5187
5188
5189 /* Move IT forward to a specified buffer position TO_CHARPOS, TO_X,
5190 TO_Y, TO_VPOS. OP is a bit-mask that specifies where to stop. See
5191 the description of enum move_operation_enum.
5192
5193 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
5194 screen line, this function will set IT to the next position >
5195 TO_CHARPOS. */
5196
5197 void
5198 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
5199 struct it *it;
5200 int to_charpos, to_x, to_y, to_vpos;
5201 int op;
5202 {
5203 enum move_it_result skip, skip2 = MOVE_X_REACHED;
5204 int line_height;
5205 int reached = 0;
5206
5207 for (;;)
5208 {
5209 if (op & MOVE_TO_VPOS)
5210 {
5211 /* If no TO_CHARPOS and no TO_X specified, stop at the
5212 start of the line TO_VPOS. */
5213 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
5214 {
5215 if (it->vpos == to_vpos)
5216 {
5217 reached = 1;
5218 break;
5219 }
5220 else
5221 skip = move_it_in_display_line_to (it, -1, -1, 0);
5222 }
5223 else
5224 {
5225 /* TO_VPOS >= 0 means stop at TO_X in the line at
5226 TO_VPOS, or at TO_POS, whichever comes first. */
5227 if (it->vpos == to_vpos)
5228 {
5229 reached = 2;
5230 break;
5231 }
5232
5233 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
5234
5235 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
5236 {
5237 reached = 3;
5238 break;
5239 }
5240 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
5241 {
5242 /* We have reached TO_X but not in the line we want. */
5243 skip = move_it_in_display_line_to (it, to_charpos,
5244 -1, MOVE_TO_POS);
5245 if (skip == MOVE_POS_MATCH_OR_ZV)
5246 {
5247 reached = 4;
5248 break;
5249 }
5250 }
5251 }
5252 }
5253 else if (op & MOVE_TO_Y)
5254 {
5255 struct it it_backup;
5256
5257 /* TO_Y specified means stop at TO_X in the line containing
5258 TO_Y---or at TO_CHARPOS if this is reached first. The
5259 problem is that we can't really tell whether the line
5260 contains TO_Y before we have completely scanned it, and
5261 this may skip past TO_X. What we do is to first scan to
5262 TO_X.
5263
5264 If TO_X is not specified, use a TO_X of zero. The reason
5265 is to make the outcome of this function more predictable.
5266 If we didn't use TO_X == 0, we would stop at the end of
5267 the line which is probably not what a caller would expect
5268 to happen. */
5269 skip = move_it_in_display_line_to (it, to_charpos,
5270 ((op & MOVE_TO_X)
5271 ? to_x : 0),
5272 (MOVE_TO_X
5273 | (op & MOVE_TO_POS)));
5274
5275 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
5276 if (skip == MOVE_POS_MATCH_OR_ZV)
5277 {
5278 reached = 5;
5279 break;
5280 }
5281
5282 /* If TO_X was reached, we would like to know whether TO_Y
5283 is in the line. This can only be said if we know the
5284 total line height which requires us to scan the rest of
5285 the line. */
5286 if (skip == MOVE_X_REACHED)
5287 {
5288 it_backup = *it;
5289 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
5290 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
5291 op & MOVE_TO_POS);
5292 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
5293 }
5294
5295 /* Now, decide whether TO_Y is in this line. */
5296 line_height = it->max_ascent + it->max_descent;
5297 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
5298
5299 if (to_y >= it->current_y
5300 && to_y < it->current_y + line_height)
5301 {
5302 if (skip == MOVE_X_REACHED)
5303 /* If TO_Y is in this line and TO_X was reached above,
5304 we scanned too far. We have to restore IT's settings
5305 to the ones before skipping. */
5306 *it = it_backup;
5307 reached = 6;
5308 }
5309 else if (skip == MOVE_X_REACHED)
5310 {
5311 skip = skip2;
5312 if (skip == MOVE_POS_MATCH_OR_ZV)
5313 reached = 7;
5314 }
5315
5316 if (reached)
5317 break;
5318 }
5319 else
5320 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
5321
5322 switch (skip)
5323 {
5324 case MOVE_POS_MATCH_OR_ZV:
5325 reached = 8;
5326 goto out;
5327
5328 case MOVE_NEWLINE_OR_CR:
5329 set_iterator_to_next (it, 1);
5330 it->continuation_lines_width = 0;
5331 break;
5332
5333 case MOVE_LINE_TRUNCATED:
5334 it->continuation_lines_width = 0;
5335 reseat_at_next_visible_line_start (it, 0);
5336 if ((op & MOVE_TO_POS) != 0
5337 && IT_CHARPOS (*it) > to_charpos)
5338 {
5339 reached = 9;
5340 goto out;
5341 }
5342 break;
5343
5344 case MOVE_LINE_CONTINUED:
5345 it->continuation_lines_width += it->current_x;
5346 break;
5347
5348 default:
5349 abort ();
5350 }
5351
5352 /* Reset/increment for the next run. */
5353 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
5354 it->current_x = it->hpos = 0;
5355 it->current_y += it->max_ascent + it->max_descent;
5356 ++it->vpos;
5357 last_height = it->max_ascent + it->max_descent;
5358 last_max_ascent = it->max_ascent;
5359 it->max_ascent = it->max_descent = 0;
5360 }
5361
5362 out:
5363
5364 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
5365 }
5366
5367
5368 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
5369
5370 If DY > 0, move IT backward at least that many pixels. DY = 0
5371 means move IT backward to the preceding line start or BEGV. This
5372 function may move over more than DY pixels if IT->current_y - DY
5373 ends up in the middle of a line; in this case IT->current_y will be
5374 set to the top of the line moved to. */
5375
5376 void
5377 move_it_vertically_backward (it, dy)
5378 struct it *it;
5379 int dy;
5380 {
5381 int nlines, h;
5382 struct it it2, it3;
5383 int start_pos = IT_CHARPOS (*it);
5384
5385 xassert (dy >= 0);
5386
5387 /* Estimate how many newlines we must move back. */
5388 nlines = max (1, dy / CANON_Y_UNIT (it->f));
5389
5390 /* Set the iterator's position that many lines back. */
5391 while (nlines-- && IT_CHARPOS (*it) > BEGV)
5392 back_to_previous_visible_line_start (it);
5393
5394 /* Reseat the iterator here. When moving backward, we don't want
5395 reseat to skip forward over invisible text, set up the iterator
5396 to deliver from overlay strings at the new position etc. So,
5397 use reseat_1 here. */
5398 reseat_1 (it, it->current.pos, 1);
5399
5400 /* We are now surely at a line start. */
5401 it->current_x = it->hpos = 0;
5402
5403 /* Move forward and see what y-distance we moved. First move to the
5404 start of the next line so that we get its height. We need this
5405 height to be able to tell whether we reached the specified
5406 y-distance. */
5407 it2 = *it;
5408 it2.max_ascent = it2.max_descent = 0;
5409 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
5410 MOVE_TO_POS | MOVE_TO_VPOS);
5411 xassert (IT_CHARPOS (*it) >= BEGV);
5412 it3 = it2;
5413
5414 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
5415 xassert (IT_CHARPOS (*it) >= BEGV);
5416 h = it2.current_y - it->current_y;
5417 nlines = it2.vpos - it->vpos;
5418
5419 /* Correct IT's y and vpos position. */
5420 it->vpos -= nlines;
5421 it->current_y -= h;
5422
5423 if (dy == 0)
5424 {
5425 /* DY == 0 means move to the start of the screen line. The
5426 value of nlines is > 0 if continuation lines were involved. */
5427 if (nlines > 0)
5428 move_it_by_lines (it, nlines, 1);
5429 xassert (IT_CHARPOS (*it) <= start_pos);
5430 }
5431 else if (nlines)
5432 {
5433 /* The y-position we try to reach. Note that h has been
5434 subtracted in front of the if-statement. */
5435 int target_y = it->current_y + h - dy;
5436 int y0 = it3.current_y;
5437 int y1 = line_bottom_y (&it3);
5438 int line_height = y1 - y0;
5439
5440 /* If we did not reach target_y, try to move further backward if
5441 we can. If we moved too far backward, try to move forward. */
5442 if (target_y < it->current_y
5443 /* This is heuristic. In a window that's 3 lines high, with
5444 a line height of 13 pixels each, recentering with point
5445 on the bottom line will try to move -39/2 = 19 pixels
5446 backward. Try to avoid moving into the first line. */
5447 && it->current_y - target_y > line_height / 3 * 2
5448 && IT_CHARPOS (*it) > BEGV)
5449 {
5450 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
5451 target_y - it->current_y));
5452 move_it_vertically (it, target_y - it->current_y);
5453 xassert (IT_CHARPOS (*it) >= BEGV);
5454 }
5455 else if (target_y >= it->current_y + line_height
5456 && IT_CHARPOS (*it) < ZV)
5457 {
5458 /* Should move forward by at least one line, maybe more. */
5459 do
5460 {
5461 move_it_by_lines (it, 1, 1);
5462 }
5463 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
5464
5465 xassert (IT_CHARPOS (*it) >= BEGV);
5466 }
5467 }
5468 }
5469
5470
5471 /* Move IT by a specified amount of pixel lines DY. DY negative means
5472 move backwards. DY = 0 means move to start of screen line. At the
5473 end, IT will be on the start of a screen line. */
5474
5475 void
5476 move_it_vertically (it, dy)
5477 struct it *it;
5478 int dy;
5479 {
5480 if (dy <= 0)
5481 move_it_vertically_backward (it, -dy);
5482 else if (dy > 0)
5483 {
5484 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
5485 move_it_to (it, ZV, -1, it->current_y + dy, -1,
5486 MOVE_TO_POS | MOVE_TO_Y);
5487 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
5488
5489 /* If buffer ends in ZV without a newline, move to the start of
5490 the line to satisfy the post-condition. */
5491 if (IT_CHARPOS (*it) == ZV
5492 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
5493 move_it_by_lines (it, 0, 0);
5494 }
5495 }
5496
5497
5498 /* Move iterator IT past the end of the text line it is in. */
5499
5500 void
5501 move_it_past_eol (it)
5502 struct it *it;
5503 {
5504 enum move_it_result rc;
5505
5506 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
5507 if (rc == MOVE_NEWLINE_OR_CR)
5508 set_iterator_to_next (it, 0);
5509 }
5510
5511
5512 #if 0 /* Currently not used. */
5513
5514 /* Return non-zero if some text between buffer positions START_CHARPOS
5515 and END_CHARPOS is invisible. IT->window is the window for text
5516 property lookup. */
5517
5518 static int
5519 invisible_text_between_p (it, start_charpos, end_charpos)
5520 struct it *it;
5521 int start_charpos, end_charpos;
5522 {
5523 Lisp_Object prop, limit;
5524 int invisible_found_p;
5525
5526 xassert (it != NULL && start_charpos <= end_charpos);
5527
5528 /* Is text at START invisible? */
5529 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
5530 it->window);
5531 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5532 invisible_found_p = 1;
5533 else
5534 {
5535 limit = Fnext_single_char_property_change (make_number (start_charpos),
5536 Qinvisible, Qnil,
5537 make_number (end_charpos));
5538 invisible_found_p = XFASTINT (limit) < end_charpos;
5539 }
5540
5541 return invisible_found_p;
5542 }
5543
5544 #endif /* 0 */
5545
5546
5547 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
5548 negative means move up. DVPOS == 0 means move to the start of the
5549 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
5550 NEED_Y_P is zero, IT->current_y will be left unchanged.
5551
5552 Further optimization ideas: If we would know that IT->f doesn't use
5553 a face with proportional font, we could be faster for
5554 truncate-lines nil. */
5555
5556 void
5557 move_it_by_lines (it, dvpos, need_y_p)
5558 struct it *it;
5559 int dvpos, need_y_p;
5560 {
5561 struct position pos;
5562
5563 if (!FRAME_WINDOW_P (it->f))
5564 {
5565 struct text_pos textpos;
5566
5567 /* We can use vmotion on frames without proportional fonts. */
5568 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
5569 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
5570 reseat (it, textpos, 1);
5571 it->vpos += pos.vpos;
5572 it->current_y += pos.vpos;
5573 }
5574 else if (dvpos == 0)
5575 {
5576 /* DVPOS == 0 means move to the start of the screen line. */
5577 move_it_vertically_backward (it, 0);
5578 xassert (it->current_x == 0 && it->hpos == 0);
5579 }
5580 else if (dvpos > 0)
5581 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
5582 else
5583 {
5584 struct it it2;
5585 int start_charpos, i;
5586
5587 /* Start at the beginning of the screen line containing IT's
5588 position. */
5589 move_it_vertically_backward (it, 0);
5590
5591 /* Go back -DVPOS visible lines and reseat the iterator there. */
5592 start_charpos = IT_CHARPOS (*it);
5593 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
5594 back_to_previous_visible_line_start (it);
5595 reseat (it, it->current.pos, 1);
5596 it->current_x = it->hpos = 0;
5597
5598 /* Above call may have moved too far if continuation lines
5599 are involved. Scan forward and see if it did. */
5600 it2 = *it;
5601 it2.vpos = it2.current_y = 0;
5602 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
5603 it->vpos -= it2.vpos;
5604 it->current_y -= it2.current_y;
5605 it->current_x = it->hpos = 0;
5606
5607 /* If we moved too far, move IT some lines forward. */
5608 if (it2.vpos > -dvpos)
5609 {
5610 int delta = it2.vpos + dvpos;
5611 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
5612 }
5613 }
5614 }
5615
5616
5617 \f
5618 /***********************************************************************
5619 Messages
5620 ***********************************************************************/
5621
5622
5623 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
5624 to *Messages*. */
5625
5626 void
5627 add_to_log (format, arg1, arg2)
5628 char *format;
5629 Lisp_Object arg1, arg2;
5630 {
5631 Lisp_Object args[3];
5632 Lisp_Object msg, fmt;
5633 char *buffer;
5634 int len;
5635 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5636
5637 /* Do nothing if called asynchronously. Inserting text into
5638 a buffer may call after-change-functions and alike and
5639 that would means running Lisp asynchronously. */
5640 if (handling_signal)
5641 return;
5642
5643 fmt = msg = Qnil;
5644 GCPRO4 (fmt, msg, arg1, arg2);
5645
5646 args[0] = fmt = build_string (format);
5647 args[1] = arg1;
5648 args[2] = arg2;
5649 msg = Fformat (3, args);
5650
5651 len = STRING_BYTES (XSTRING (msg)) + 1;
5652 buffer = (char *) alloca (len);
5653 bcopy (XSTRING (msg)->data, buffer, len);
5654
5655 message_dolog (buffer, len - 1, 1, 0);
5656 UNGCPRO;
5657 }
5658
5659
5660 /* Output a newline in the *Messages* buffer if "needs" one. */
5661
5662 void
5663 message_log_maybe_newline ()
5664 {
5665 if (message_log_need_newline)
5666 message_dolog ("", 0, 1, 0);
5667 }
5668
5669
5670 /* Add a string M of length NBYTES to the message log, optionally
5671 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
5672 nonzero, means interpret the contents of M as multibyte. This
5673 function calls low-level routines in order to bypass text property
5674 hooks, etc. which might not be safe to run. */
5675
5676 void
5677 message_dolog (m, nbytes, nlflag, multibyte)
5678 char *m;
5679 int nbytes, nlflag, multibyte;
5680 {
5681 if (!NILP (Vmessage_log_max))
5682 {
5683 struct buffer *oldbuf;
5684 Lisp_Object oldpoint, oldbegv, oldzv;
5685 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5686 int point_at_end = 0;
5687 int zv_at_end = 0;
5688 Lisp_Object old_deactivate_mark, tem;
5689 struct gcpro gcpro1;
5690
5691 old_deactivate_mark = Vdeactivate_mark;
5692 oldbuf = current_buffer;
5693 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
5694 current_buffer->undo_list = Qt;
5695
5696 oldpoint = message_dolog_marker1;
5697 set_marker_restricted (oldpoint, make_number (PT), Qnil);
5698 oldbegv = message_dolog_marker2;
5699 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
5700 oldzv = message_dolog_marker3;
5701 set_marker_restricted (oldzv, make_number (ZV), Qnil);
5702 GCPRO1 (old_deactivate_mark);
5703
5704 if (PT == Z)
5705 point_at_end = 1;
5706 if (ZV == Z)
5707 zv_at_end = 1;
5708
5709 BEGV = BEG;
5710 BEGV_BYTE = BEG_BYTE;
5711 ZV = Z;
5712 ZV_BYTE = Z_BYTE;
5713 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5714
5715 /* Insert the string--maybe converting multibyte to single byte
5716 or vice versa, so that all the text fits the buffer. */
5717 if (multibyte
5718 && NILP (current_buffer->enable_multibyte_characters))
5719 {
5720 int i, c, char_bytes;
5721 unsigned char work[1];
5722
5723 /* Convert a multibyte string to single-byte
5724 for the *Message* buffer. */
5725 for (i = 0; i < nbytes; i += nbytes)
5726 {
5727 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
5728 work[0] = (ASCII_CHAR_P (c)
5729 ? c
5730 : multibyte_char_to_unibyte (c, Qnil));
5731 insert_1_both (work, 1, 1, 1, 0, 0);
5732 }
5733 }
5734 else if (! multibyte
5735 && ! NILP (current_buffer->enable_multibyte_characters))
5736 {
5737 int i, c, char_bytes;
5738 unsigned char *msg = (unsigned char *) m;
5739 unsigned char str[MAX_MULTIBYTE_LENGTH];
5740 /* Convert a single-byte string to multibyte
5741 for the *Message* buffer. */
5742 for (i = 0; i < nbytes; i++)
5743 {
5744 c = unibyte_char_to_multibyte (msg[i]);
5745 char_bytes = CHAR_STRING (c, str);
5746 insert_1_both (str, 1, char_bytes, 1, 0, 0);
5747 }
5748 }
5749 else if (nbytes)
5750 insert_1 (m, nbytes, 1, 0, 0);
5751
5752 if (nlflag)
5753 {
5754 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
5755 insert_1 ("\n", 1, 1, 0, 0);
5756
5757 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
5758 this_bol = PT;
5759 this_bol_byte = PT_BYTE;
5760
5761 /* See if this line duplicates the previous one.
5762 If so, combine duplicates. */
5763 if (this_bol > BEG)
5764 {
5765 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
5766 prev_bol = PT;
5767 prev_bol_byte = PT_BYTE;
5768
5769 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
5770 this_bol, this_bol_byte);
5771 if (dup)
5772 {
5773 del_range_both (prev_bol, prev_bol_byte,
5774 this_bol, this_bol_byte, 0);
5775 if (dup > 1)
5776 {
5777 char dupstr[40];
5778 int duplen;
5779
5780 /* If you change this format, don't forget to also
5781 change message_log_check_duplicate. */
5782 sprintf (dupstr, " [%d times]", dup);
5783 duplen = strlen (dupstr);
5784 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
5785 insert_1 (dupstr, duplen, 1, 0, 1);
5786 }
5787 }
5788 }
5789
5790 /* If we have more than the desired maximum number of lines
5791 in the *Messages* buffer now, delete the oldest ones.
5792 This is safe because we don't have undo in this buffer. */
5793
5794 if (NATNUMP (Vmessage_log_max))
5795 {
5796 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
5797 -XFASTINT (Vmessage_log_max) - 1, 0);
5798 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
5799 }
5800 }
5801 BEGV = XMARKER (oldbegv)->charpos;
5802 BEGV_BYTE = marker_byte_position (oldbegv);
5803
5804 if (zv_at_end)
5805 {
5806 ZV = Z;
5807 ZV_BYTE = Z_BYTE;
5808 }
5809 else
5810 {
5811 ZV = XMARKER (oldzv)->charpos;
5812 ZV_BYTE = marker_byte_position (oldzv);
5813 }
5814
5815 if (point_at_end)
5816 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5817 else
5818 /* We can't do Fgoto_char (oldpoint) because it will run some
5819 Lisp code. */
5820 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
5821 XMARKER (oldpoint)->bytepos);
5822
5823 UNGCPRO;
5824 unchain_marker (oldpoint);
5825 unchain_marker (oldbegv);
5826 unchain_marker (oldzv);
5827
5828 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
5829 set_buffer_internal (oldbuf);
5830 if (NILP (tem))
5831 windows_or_buffers_changed = old_windows_or_buffers_changed;
5832 message_log_need_newline = !nlflag;
5833 Vdeactivate_mark = old_deactivate_mark;
5834 }
5835 }
5836
5837
5838 /* We are at the end of the buffer after just having inserted a newline.
5839 (Note: We depend on the fact we won't be crossing the gap.)
5840 Check to see if the most recent message looks a lot like the previous one.
5841 Return 0 if different, 1 if the new one should just replace it, or a
5842 value N > 1 if we should also append " [N times]". */
5843
5844 static int
5845 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
5846 int prev_bol, this_bol;
5847 int prev_bol_byte, this_bol_byte;
5848 {
5849 int i;
5850 int len = Z_BYTE - 1 - this_bol_byte;
5851 int seen_dots = 0;
5852 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
5853 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
5854
5855 for (i = 0; i < len; i++)
5856 {
5857 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
5858 seen_dots = 1;
5859 if (p1[i] != p2[i])
5860 return seen_dots;
5861 }
5862 p1 += len;
5863 if (*p1 == '\n')
5864 return 2;
5865 if (*p1++ == ' ' && *p1++ == '[')
5866 {
5867 int n = 0;
5868 while (*p1 >= '0' && *p1 <= '9')
5869 n = n * 10 + *p1++ - '0';
5870 if (strncmp (p1, " times]\n", 8) == 0)
5871 return n+1;
5872 }
5873 return 0;
5874 }
5875
5876
5877 /* Display an echo area message M with a specified length of NBYTES
5878 bytes. The string may include null characters. If M is 0, clear
5879 out any existing message, and let the mini-buffer text show
5880 through.
5881
5882 The buffer M must continue to exist until after the echo area gets
5883 cleared or some other message gets displayed there. This means do
5884 not pass text that is stored in a Lisp string; do not pass text in
5885 a buffer that was alloca'd. */
5886
5887 void
5888 message2 (m, nbytes, multibyte)
5889 char *m;
5890 int nbytes;
5891 int multibyte;
5892 {
5893 /* First flush out any partial line written with print. */
5894 message_log_maybe_newline ();
5895 if (m)
5896 message_dolog (m, nbytes, 1, multibyte);
5897 message2_nolog (m, nbytes, multibyte);
5898 }
5899
5900
5901 /* The non-logging counterpart of message2. */
5902
5903 void
5904 message2_nolog (m, nbytes, multibyte)
5905 char *m;
5906 int nbytes, multibyte;
5907 {
5908 struct frame *sf = SELECTED_FRAME ();
5909 message_enable_multibyte = multibyte;
5910
5911 if (noninteractive)
5912 {
5913 if (noninteractive_need_newline)
5914 putc ('\n', stderr);
5915 noninteractive_need_newline = 0;
5916 if (m)
5917 fwrite (m, nbytes, 1, stderr);
5918 if (cursor_in_echo_area == 0)
5919 fprintf (stderr, "\n");
5920 fflush (stderr);
5921 }
5922 /* A null message buffer means that the frame hasn't really been
5923 initialized yet. Error messages get reported properly by
5924 cmd_error, so this must be just an informative message; toss it. */
5925 else if (INTERACTIVE
5926 && sf->glyphs_initialized_p
5927 && FRAME_MESSAGE_BUF (sf))
5928 {
5929 Lisp_Object mini_window;
5930 struct frame *f;
5931
5932 /* Get the frame containing the mini-buffer
5933 that the selected frame is using. */
5934 mini_window = FRAME_MINIBUF_WINDOW (sf);
5935 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5936
5937 FRAME_SAMPLE_VISIBILITY (f);
5938 if (FRAME_VISIBLE_P (sf)
5939 && ! FRAME_VISIBLE_P (f))
5940 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
5941
5942 if (m)
5943 {
5944 set_message (m, Qnil, nbytes, multibyte);
5945 if (minibuffer_auto_raise)
5946 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5947 }
5948 else
5949 clear_message (1, 1);
5950
5951 do_pending_window_change (0);
5952 echo_area_display (1);
5953 do_pending_window_change (0);
5954 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5955 (*frame_up_to_date_hook) (f);
5956 }
5957 }
5958
5959
5960 /* Display an echo area message M with a specified length of NBYTES
5961 bytes. The string may include null characters. If M is not a
5962 string, clear out any existing message, and let the mini-buffer
5963 text show through. */
5964
5965 void
5966 message3 (m, nbytes, multibyte)
5967 Lisp_Object m;
5968 int nbytes;
5969 int multibyte;
5970 {
5971 struct gcpro gcpro1;
5972
5973 GCPRO1 (m);
5974
5975 /* First flush out any partial line written with print. */
5976 message_log_maybe_newline ();
5977 if (STRINGP (m))
5978 message_dolog (XSTRING (m)->data, nbytes, 1, multibyte);
5979 message3_nolog (m, nbytes, multibyte);
5980
5981 UNGCPRO;
5982 }
5983
5984
5985 /* The non-logging version of message3. */
5986
5987 void
5988 message3_nolog (m, nbytes, multibyte)
5989 Lisp_Object m;
5990 int nbytes, multibyte;
5991 {
5992 struct frame *sf = SELECTED_FRAME ();
5993 message_enable_multibyte = multibyte;
5994
5995 if (noninteractive)
5996 {
5997 if (noninteractive_need_newline)
5998 putc ('\n', stderr);
5999 noninteractive_need_newline = 0;
6000 if (STRINGP (m))
6001 fwrite (XSTRING (m)->data, nbytes, 1, stderr);
6002 if (cursor_in_echo_area == 0)
6003 fprintf (stderr, "\n");
6004 fflush (stderr);
6005 }
6006 /* A null message buffer means that the frame hasn't really been
6007 initialized yet. Error messages get reported properly by
6008 cmd_error, so this must be just an informative message; toss it. */
6009 else if (INTERACTIVE
6010 && sf->glyphs_initialized_p
6011 && FRAME_MESSAGE_BUF (sf))
6012 {
6013 Lisp_Object mini_window;
6014 Lisp_Object frame;
6015 struct frame *f;
6016
6017 /* Get the frame containing the mini-buffer
6018 that the selected frame is using. */
6019 mini_window = FRAME_MINIBUF_WINDOW (sf);
6020 frame = XWINDOW (mini_window)->frame;
6021 f = XFRAME (frame);
6022
6023 FRAME_SAMPLE_VISIBILITY (f);
6024 if (FRAME_VISIBLE_P (sf)
6025 && !FRAME_VISIBLE_P (f))
6026 Fmake_frame_visible (frame);
6027
6028 if (STRINGP (m) && XSTRING (m)->size)
6029 {
6030 set_message (NULL, m, nbytes, multibyte);
6031 if (minibuffer_auto_raise)
6032 Fraise_frame (frame);
6033 }
6034 else
6035 clear_message (1, 1);
6036
6037 do_pending_window_change (0);
6038 echo_area_display (1);
6039 do_pending_window_change (0);
6040 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6041 (*frame_up_to_date_hook) (f);
6042 }
6043 }
6044
6045
6046 /* Display a null-terminated echo area message M. If M is 0, clear
6047 out any existing message, and let the mini-buffer text show through.
6048
6049 The buffer M must continue to exist until after the echo area gets
6050 cleared or some other message gets displayed there. Do not pass
6051 text that is stored in a Lisp string. Do not pass text in a buffer
6052 that was alloca'd. */
6053
6054 void
6055 message1 (m)
6056 char *m;
6057 {
6058 message2 (m, (m ? strlen (m) : 0), 0);
6059 }
6060
6061
6062 /* The non-logging counterpart of message1. */
6063
6064 void
6065 message1_nolog (m)
6066 char *m;
6067 {
6068 message2_nolog (m, (m ? strlen (m) : 0), 0);
6069 }
6070
6071 /* Display a message M which contains a single %s
6072 which gets replaced with STRING. */
6073
6074 void
6075 message_with_string (m, string, log)
6076 char *m;
6077 Lisp_Object string;
6078 int log;
6079 {
6080 if (noninteractive)
6081 {
6082 if (m)
6083 {
6084 if (noninteractive_need_newline)
6085 putc ('\n', stderr);
6086 noninteractive_need_newline = 0;
6087 fprintf (stderr, m, XSTRING (string)->data);
6088 if (cursor_in_echo_area == 0)
6089 fprintf (stderr, "\n");
6090 fflush (stderr);
6091 }
6092 }
6093 else if (INTERACTIVE)
6094 {
6095 /* The frame whose minibuffer we're going to display the message on.
6096 It may be larger than the selected frame, so we need
6097 to use its buffer, not the selected frame's buffer. */
6098 Lisp_Object mini_window;
6099 struct frame *f, *sf = SELECTED_FRAME ();
6100
6101 /* Get the frame containing the minibuffer
6102 that the selected frame is using. */
6103 mini_window = FRAME_MINIBUF_WINDOW (sf);
6104 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6105
6106 /* A null message buffer means that the frame hasn't really been
6107 initialized yet. Error messages get reported properly by
6108 cmd_error, so this must be just an informative message; toss it. */
6109 if (FRAME_MESSAGE_BUF (f))
6110 {
6111 int len;
6112 char *a[1];
6113 a[0] = (char *) XSTRING (string)->data;
6114
6115 len = doprnt (FRAME_MESSAGE_BUF (f),
6116 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6117
6118 if (log)
6119 message2 (FRAME_MESSAGE_BUF (f), len,
6120 STRING_MULTIBYTE (string));
6121 else
6122 message2_nolog (FRAME_MESSAGE_BUF (f), len,
6123 STRING_MULTIBYTE (string));
6124
6125 /* Print should start at the beginning of the message
6126 buffer next time. */
6127 message_buf_print = 0;
6128 }
6129 }
6130 }
6131
6132
6133 /* Dump an informative message to the minibuf. If M is 0, clear out
6134 any existing message, and let the mini-buffer text show through. */
6135
6136 /* VARARGS 1 */
6137 void
6138 message (m, a1, a2, a3)
6139 char *m;
6140 EMACS_INT a1, a2, a3;
6141 {
6142 if (noninteractive)
6143 {
6144 if (m)
6145 {
6146 if (noninteractive_need_newline)
6147 putc ('\n', stderr);
6148 noninteractive_need_newline = 0;
6149 fprintf (stderr, m, a1, a2, a3);
6150 if (cursor_in_echo_area == 0)
6151 fprintf (stderr, "\n");
6152 fflush (stderr);
6153 }
6154 }
6155 else if (INTERACTIVE)
6156 {
6157 /* The frame whose mini-buffer we're going to display the message
6158 on. It may be larger than the selected frame, so we need to
6159 use its buffer, not the selected frame's buffer. */
6160 Lisp_Object mini_window;
6161 struct frame *f, *sf = SELECTED_FRAME ();
6162
6163 /* Get the frame containing the mini-buffer
6164 that the selected frame is using. */
6165 mini_window = FRAME_MINIBUF_WINDOW (sf);
6166 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6167
6168 /* A null message buffer means that the frame hasn't really been
6169 initialized yet. Error messages get reported properly by
6170 cmd_error, so this must be just an informative message; toss
6171 it. */
6172 if (FRAME_MESSAGE_BUF (f))
6173 {
6174 if (m)
6175 {
6176 int len;
6177 #ifdef NO_ARG_ARRAY
6178 char *a[3];
6179 a[0] = (char *) a1;
6180 a[1] = (char *) a2;
6181 a[2] = (char *) a3;
6182
6183 len = doprnt (FRAME_MESSAGE_BUF (f),
6184 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6185 #else
6186 len = doprnt (FRAME_MESSAGE_BUF (f),
6187 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
6188 (char **) &a1);
6189 #endif /* NO_ARG_ARRAY */
6190
6191 message2 (FRAME_MESSAGE_BUF (f), len, 0);
6192 }
6193 else
6194 message1 (0);
6195
6196 /* Print should start at the beginning of the message
6197 buffer next time. */
6198 message_buf_print = 0;
6199 }
6200 }
6201 }
6202
6203
6204 /* The non-logging version of message. */
6205
6206 void
6207 message_nolog (m, a1, a2, a3)
6208 char *m;
6209 EMACS_INT a1, a2, a3;
6210 {
6211 Lisp_Object old_log_max;
6212 old_log_max = Vmessage_log_max;
6213 Vmessage_log_max = Qnil;
6214 message (m, a1, a2, a3);
6215 Vmessage_log_max = old_log_max;
6216 }
6217
6218
6219 /* Display the current message in the current mini-buffer. This is
6220 only called from error handlers in process.c, and is not time
6221 critical. */
6222
6223 void
6224 update_echo_area ()
6225 {
6226 if (!NILP (echo_area_buffer[0]))
6227 {
6228 Lisp_Object string;
6229 string = Fcurrent_message ();
6230 message3 (string, XSTRING (string)->size,
6231 !NILP (current_buffer->enable_multibyte_characters));
6232 }
6233 }
6234
6235
6236 /* Make sure echo area buffers in echo_buffers[] are life. If they
6237 aren't, make new ones. */
6238
6239 static void
6240 ensure_echo_area_buffers ()
6241 {
6242 int i;
6243
6244 for (i = 0; i < 2; ++i)
6245 if (!BUFFERP (echo_buffer[i])
6246 || NILP (XBUFFER (echo_buffer[i])->name))
6247 {
6248 char name[30];
6249 Lisp_Object old_buffer;
6250 int j;
6251
6252 old_buffer = echo_buffer[i];
6253 sprintf (name, " *Echo Area %d*", i);
6254 echo_buffer[i] = Fget_buffer_create (build_string (name));
6255 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
6256
6257 for (j = 0; j < 2; ++j)
6258 if (EQ (old_buffer, echo_area_buffer[j]))
6259 echo_area_buffer[j] = echo_buffer[i];
6260 }
6261 }
6262
6263
6264 /* Call FN with args A1..A4 with either the current or last displayed
6265 echo_area_buffer as current buffer.
6266
6267 WHICH zero means use the current message buffer
6268 echo_area_buffer[0]. If that is nil, choose a suitable buffer
6269 from echo_buffer[] and clear it.
6270
6271 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
6272 suitable buffer from echo_buffer[] and clear it.
6273
6274 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
6275 that the current message becomes the last displayed one, make
6276 choose a suitable buffer for echo_area_buffer[0], and clear it.
6277
6278 Value is what FN returns. */
6279
6280 static int
6281 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
6282 struct window *w;
6283 int which;
6284 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
6285 EMACS_INT a1;
6286 Lisp_Object a2;
6287 EMACS_INT a3, a4;
6288 {
6289 Lisp_Object buffer;
6290 int this_one, the_other, clear_buffer_p, rc;
6291 int count = BINDING_STACK_SIZE ();
6292
6293 /* If buffers aren't life, make new ones. */
6294 ensure_echo_area_buffers ();
6295
6296 clear_buffer_p = 0;
6297
6298 if (which == 0)
6299 this_one = 0, the_other = 1;
6300 else if (which > 0)
6301 this_one = 1, the_other = 0;
6302 else
6303 {
6304 this_one = 0, the_other = 1;
6305 clear_buffer_p = 1;
6306
6307 /* We need a fresh one in case the current echo buffer equals
6308 the one containing the last displayed echo area message. */
6309 if (!NILP (echo_area_buffer[this_one])
6310 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
6311 echo_area_buffer[this_one] = Qnil;
6312 }
6313
6314 /* Choose a suitable buffer from echo_buffer[] is we don't
6315 have one. */
6316 if (NILP (echo_area_buffer[this_one]))
6317 {
6318 echo_area_buffer[this_one]
6319 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
6320 ? echo_buffer[the_other]
6321 : echo_buffer[this_one]);
6322 clear_buffer_p = 1;
6323 }
6324
6325 buffer = echo_area_buffer[this_one];
6326
6327 /* Don't get confused by reusing the buffer used for echoing
6328 for a different purpose. */
6329 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
6330 cancel_echoing ();
6331
6332 record_unwind_protect (unwind_with_echo_area_buffer,
6333 with_echo_area_buffer_unwind_data (w));
6334
6335 /* Make the echo area buffer current. Note that for display
6336 purposes, it is not necessary that the displayed window's buffer
6337 == current_buffer, except for text property lookup. So, let's
6338 only set that buffer temporarily here without doing a full
6339 Fset_window_buffer. We must also change w->pointm, though,
6340 because otherwise an assertions in unshow_buffer fails, and Emacs
6341 aborts. */
6342 set_buffer_internal_1 (XBUFFER (buffer));
6343 if (w)
6344 {
6345 w->buffer = buffer;
6346 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
6347 }
6348
6349 current_buffer->undo_list = Qt;
6350 current_buffer->read_only = Qnil;
6351 specbind (Qinhibit_read_only, Qt);
6352 specbind (Qinhibit_modification_hooks, Qt);
6353
6354 if (clear_buffer_p && Z > BEG)
6355 del_range (BEG, Z);
6356
6357 xassert (BEGV >= BEG);
6358 xassert (ZV <= Z && ZV >= BEGV);
6359
6360 rc = fn (a1, a2, a3, a4);
6361
6362 xassert (BEGV >= BEG);
6363 xassert (ZV <= Z && ZV >= BEGV);
6364
6365 unbind_to (count, Qnil);
6366 return rc;
6367 }
6368
6369
6370 /* Save state that should be preserved around the call to the function
6371 FN called in with_echo_area_buffer. */
6372
6373 static Lisp_Object
6374 with_echo_area_buffer_unwind_data (w)
6375 struct window *w;
6376 {
6377 int i = 0;
6378 Lisp_Object vector;
6379
6380 /* Reduce consing by keeping one vector in
6381 Vwith_echo_area_save_vector. */
6382 vector = Vwith_echo_area_save_vector;
6383 Vwith_echo_area_save_vector = Qnil;
6384
6385 if (NILP (vector))
6386 vector = Fmake_vector (make_number (7), Qnil);
6387
6388 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
6389 AREF (vector, i) = Vdeactivate_mark, ++i;
6390 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
6391
6392 if (w)
6393 {
6394 XSETWINDOW (AREF (vector, i), w); ++i;
6395 AREF (vector, i) = w->buffer; ++i;
6396 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
6397 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
6398 }
6399 else
6400 {
6401 int end = i + 4;
6402 for (; i < end; ++i)
6403 AREF (vector, i) = Qnil;
6404 }
6405
6406 xassert (i == ASIZE (vector));
6407 return vector;
6408 }
6409
6410
6411 /* Restore global state from VECTOR which was created by
6412 with_echo_area_buffer_unwind_data. */
6413
6414 static Lisp_Object
6415 unwind_with_echo_area_buffer (vector)
6416 Lisp_Object vector;
6417 {
6418 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
6419 Vdeactivate_mark = AREF (vector, 1);
6420 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
6421
6422 if (WINDOWP (AREF (vector, 3)))
6423 {
6424 struct window *w;
6425 Lisp_Object buffer, charpos, bytepos;
6426
6427 w = XWINDOW (AREF (vector, 3));
6428 buffer = AREF (vector, 4);
6429 charpos = AREF (vector, 5);
6430 bytepos = AREF (vector, 6);
6431
6432 w->buffer = buffer;
6433 set_marker_both (w->pointm, buffer,
6434 XFASTINT (charpos), XFASTINT (bytepos));
6435 }
6436
6437 Vwith_echo_area_save_vector = vector;
6438 return Qnil;
6439 }
6440
6441
6442 /* Set up the echo area for use by print functions. MULTIBYTE_P
6443 non-zero means we will print multibyte. */
6444
6445 void
6446 setup_echo_area_for_printing (multibyte_p)
6447 int multibyte_p;
6448 {
6449 ensure_echo_area_buffers ();
6450
6451 if (!message_buf_print)
6452 {
6453 /* A message has been output since the last time we printed.
6454 Choose a fresh echo area buffer. */
6455 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6456 echo_area_buffer[0] = echo_buffer[1];
6457 else
6458 echo_area_buffer[0] = echo_buffer[0];
6459
6460 /* Switch to that buffer and clear it. */
6461 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6462 current_buffer->truncate_lines = Qnil;
6463
6464 if (Z > BEG)
6465 {
6466 int count = BINDING_STACK_SIZE ();
6467 specbind (Qinhibit_read_only, Qt);
6468 del_range (BEG, Z);
6469 unbind_to (count, Qnil);
6470 }
6471 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6472
6473 /* Set up the buffer for the multibyteness we need. */
6474 if (multibyte_p
6475 != !NILP (current_buffer->enable_multibyte_characters))
6476 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil, Qnil);
6477
6478 /* Raise the frame containing the echo area. */
6479 if (minibuffer_auto_raise)
6480 {
6481 struct frame *sf = SELECTED_FRAME ();
6482 Lisp_Object mini_window;
6483 mini_window = FRAME_MINIBUF_WINDOW (sf);
6484 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6485 }
6486
6487 message_log_maybe_newline ();
6488 message_buf_print = 1;
6489 }
6490 else
6491 {
6492 if (NILP (echo_area_buffer[0]))
6493 {
6494 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6495 echo_area_buffer[0] = echo_buffer[1];
6496 else
6497 echo_area_buffer[0] = echo_buffer[0];
6498 }
6499
6500 if (current_buffer != XBUFFER (echo_area_buffer[0]))
6501 {
6502 /* Someone switched buffers between print requests. */
6503 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6504 current_buffer->truncate_lines = Qnil;
6505 }
6506 }
6507 }
6508
6509
6510 /* Display an echo area message in window W. Value is non-zero if W's
6511 height is changed. If display_last_displayed_message_p is
6512 non-zero, display the message that was last displayed, otherwise
6513 display the current message. */
6514
6515 static int
6516 display_echo_area (w)
6517 struct window *w;
6518 {
6519 int i, no_message_p, window_height_changed_p, count;
6520
6521 /* Temporarily disable garbage collections while displaying the echo
6522 area. This is done because a GC can print a message itself.
6523 That message would modify the echo area buffer's contents while a
6524 redisplay of the buffer is going on, and seriously confuse
6525 redisplay. */
6526 count = inhibit_garbage_collection ();
6527
6528 /* If there is no message, we must call display_echo_area_1
6529 nevertheless because it resizes the window. But we will have to
6530 reset the echo_area_buffer in question to nil at the end because
6531 with_echo_area_buffer will sets it to an empty buffer. */
6532 i = display_last_displayed_message_p ? 1 : 0;
6533 no_message_p = NILP (echo_area_buffer[i]);
6534
6535 window_height_changed_p
6536 = with_echo_area_buffer (w, display_last_displayed_message_p,
6537 display_echo_area_1,
6538 (EMACS_INT) w, Qnil, 0, 0);
6539
6540 if (no_message_p)
6541 echo_area_buffer[i] = Qnil;
6542
6543 unbind_to (count, Qnil);
6544 return window_height_changed_p;
6545 }
6546
6547
6548 /* Helper for display_echo_area. Display the current buffer which
6549 contains the current echo area message in window W, a mini-window,
6550 a pointer to which is passed in A1. A2..A4 are currently not used.
6551 Change the height of W so that all of the message is displayed.
6552 Value is non-zero if height of W was changed. */
6553
6554 static int
6555 display_echo_area_1 (a1, a2, a3, a4)
6556 EMACS_INT a1;
6557 Lisp_Object a2;
6558 EMACS_INT a3, a4;
6559 {
6560 struct window *w = (struct window *) a1;
6561 Lisp_Object window;
6562 struct text_pos start;
6563 int window_height_changed_p = 0;
6564
6565 /* Do this before displaying, so that we have a large enough glyph
6566 matrix for the display. */
6567 window_height_changed_p = resize_mini_window (w, 0);
6568
6569 /* Display. */
6570 clear_glyph_matrix (w->desired_matrix);
6571 XSETWINDOW (window, w);
6572 SET_TEXT_POS (start, BEG, BEG_BYTE);
6573 try_window (window, start);
6574
6575 return window_height_changed_p;
6576 }
6577
6578
6579 /* Resize the echo area window to exactly the size needed for the
6580 currently displayed message, if there is one. If a mini-buffer
6581 is active, don't shrink it. */
6582
6583 void
6584 resize_echo_area_exactly ()
6585 {
6586 if (BUFFERP (echo_area_buffer[0])
6587 && WINDOWP (echo_area_window))
6588 {
6589 struct window *w = XWINDOW (echo_area_window);
6590 int resized_p;
6591 Lisp_Object resize_exactly;
6592
6593 if (minibuf_level == 0)
6594 resize_exactly = Qt;
6595 else
6596 resize_exactly = Qnil;
6597
6598 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
6599 (EMACS_INT) w, resize_exactly, 0, 0);
6600 if (resized_p)
6601 {
6602 ++windows_or_buffers_changed;
6603 ++update_mode_lines;
6604 redisplay_internal (0);
6605 }
6606 }
6607 }
6608
6609
6610 /* Callback function for with_echo_area_buffer, when used from
6611 resize_echo_area_exactly. A1 contains a pointer to the window to
6612 resize, EXACTLY non-nil means resize the mini-window exactly to the
6613 size of the text displayed. A3 and A4 are not used. Value is what
6614 resize_mini_window returns. */
6615
6616 static int
6617 resize_mini_window_1 (a1, exactly, a3, a4)
6618 EMACS_INT a1;
6619 Lisp_Object exactly;
6620 EMACS_INT a3, a4;
6621 {
6622 return resize_mini_window ((struct window *) a1, !NILP (exactly));
6623 }
6624
6625
6626 /* Resize mini-window W to fit the size of its contents. EXACT:P
6627 means size the window exactly to the size needed. Otherwise, it's
6628 only enlarged until W's buffer is empty. Value is non-zero if
6629 the window height has been changed. */
6630
6631 int
6632 resize_mini_window (w, exact_p)
6633 struct window *w;
6634 int exact_p;
6635 {
6636 struct frame *f = XFRAME (w->frame);
6637 int window_height_changed_p = 0;
6638
6639 xassert (MINI_WINDOW_P (w));
6640
6641 /* Don't resize windows while redisplaying a window; it would
6642 confuse redisplay functions when the size of the window they are
6643 displaying changes from under them. Such a resizing can happen,
6644 for instance, when which-func prints a long message while
6645 we are running fontification-functions. We're running these
6646 functions with safe_call which binds inhibit-redisplay to t. */
6647 if (!NILP (Vinhibit_redisplay))
6648 return 0;
6649
6650 /* Nil means don't try to resize. */
6651 if (NILP (Vresize_mini_windows)
6652 || (FRAME_X_P (f) && f->output_data.x == NULL))
6653 return 0;
6654
6655 if (!FRAME_MINIBUF_ONLY_P (f))
6656 {
6657 struct it it;
6658 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
6659 int total_height = XFASTINT (root->height) + XFASTINT (w->height);
6660 int height, max_height;
6661 int unit = CANON_Y_UNIT (f);
6662 struct text_pos start;
6663 struct buffer *old_current_buffer = NULL;
6664
6665 if (current_buffer != XBUFFER (w->buffer))
6666 {
6667 old_current_buffer = current_buffer;
6668 set_buffer_internal (XBUFFER (w->buffer));
6669 }
6670
6671 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
6672
6673 /* Compute the max. number of lines specified by the user. */
6674 if (FLOATP (Vmax_mini_window_height))
6675 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_HEIGHT (f);
6676 else if (INTEGERP (Vmax_mini_window_height))
6677 max_height = XINT (Vmax_mini_window_height);
6678 else
6679 max_height = total_height / 4;
6680
6681 /* Correct that max. height if it's bogus. */
6682 max_height = max (1, max_height);
6683 max_height = min (total_height, max_height);
6684
6685 /* Find out the height of the text in the window. */
6686 if (it.truncate_lines_p)
6687 height = 1;
6688 else
6689 {
6690 last_height = 0;
6691 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
6692 if (it.max_ascent == 0 && it.max_descent == 0)
6693 height = it.current_y + last_height;
6694 else
6695 height = it.current_y + it.max_ascent + it.max_descent;
6696 height -= it.extra_line_spacing;
6697 height = (height + unit - 1) / unit;
6698 }
6699
6700 /* Compute a suitable window start. */
6701 if (height > max_height)
6702 {
6703 height = max_height;
6704 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
6705 move_it_vertically_backward (&it, (height - 1) * unit);
6706 start = it.current.pos;
6707 }
6708 else
6709 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
6710 SET_MARKER_FROM_TEXT_POS (w->start, start);
6711
6712 if (EQ (Vresize_mini_windows, Qgrow_only))
6713 {
6714 /* Let it grow only, until we display an empty message, in which
6715 case the window shrinks again. */
6716 if (height > XFASTINT (w->height))
6717 {
6718 int old_height = XFASTINT (w->height);
6719 freeze_window_starts (f, 1);
6720 grow_mini_window (w, height - XFASTINT (w->height));
6721 window_height_changed_p = XFASTINT (w->height) != old_height;
6722 }
6723 else if (height < XFASTINT (w->height)
6724 && (exact_p || BEGV == ZV))
6725 {
6726 int old_height = XFASTINT (w->height);
6727 freeze_window_starts (f, 0);
6728 shrink_mini_window (w);
6729 window_height_changed_p = XFASTINT (w->height) != old_height;
6730 }
6731 }
6732 else
6733 {
6734 /* Always resize to exact size needed. */
6735 if (height > XFASTINT (w->height))
6736 {
6737 int old_height = XFASTINT (w->height);
6738 freeze_window_starts (f, 1);
6739 grow_mini_window (w, height - XFASTINT (w->height));
6740 window_height_changed_p = XFASTINT (w->height) != old_height;
6741 }
6742 else if (height < XFASTINT (w->height))
6743 {
6744 int old_height = XFASTINT (w->height);
6745 freeze_window_starts (f, 0);
6746 shrink_mini_window (w);
6747
6748 if (height)
6749 {
6750 freeze_window_starts (f, 1);
6751 grow_mini_window (w, height - XFASTINT (w->height));
6752 }
6753
6754 window_height_changed_p = XFASTINT (w->height) != old_height;
6755 }
6756 }
6757
6758 if (old_current_buffer)
6759 set_buffer_internal (old_current_buffer);
6760 }
6761
6762 return window_height_changed_p;
6763 }
6764
6765
6766 /* Value is the current message, a string, or nil if there is no
6767 current message. */
6768
6769 Lisp_Object
6770 current_message ()
6771 {
6772 Lisp_Object msg;
6773
6774 if (NILP (echo_area_buffer[0]))
6775 msg = Qnil;
6776 else
6777 {
6778 with_echo_area_buffer (0, 0, current_message_1,
6779 (EMACS_INT) &msg, Qnil, 0, 0);
6780 if (NILP (msg))
6781 echo_area_buffer[0] = Qnil;
6782 }
6783
6784 return msg;
6785 }
6786
6787
6788 static int
6789 current_message_1 (a1, a2, a3, a4)
6790 EMACS_INT a1;
6791 Lisp_Object a2;
6792 EMACS_INT a3, a4;
6793 {
6794 Lisp_Object *msg = (Lisp_Object *) a1;
6795
6796 if (Z > BEG)
6797 *msg = make_buffer_string (BEG, Z, 1);
6798 else
6799 *msg = Qnil;
6800 return 0;
6801 }
6802
6803
6804 /* Push the current message on Vmessage_stack for later restauration
6805 by restore_message. Value is non-zero if the current message isn't
6806 empty. This is a relatively infrequent operation, so it's not
6807 worth optimizing. */
6808
6809 int
6810 push_message ()
6811 {
6812 Lisp_Object msg;
6813 msg = current_message ();
6814 Vmessage_stack = Fcons (msg, Vmessage_stack);
6815 return STRINGP (msg);
6816 }
6817
6818
6819 /* Handler for record_unwind_protect calling pop_message. */
6820
6821 Lisp_Object
6822 push_message_unwind (dummy)
6823 Lisp_Object dummy;
6824 {
6825 pop_message ();
6826 return Qnil;
6827 }
6828
6829
6830 /* Restore message display from the top of Vmessage_stack. */
6831
6832 void
6833 restore_message ()
6834 {
6835 Lisp_Object msg;
6836
6837 xassert (CONSP (Vmessage_stack));
6838 msg = XCAR (Vmessage_stack);
6839 if (STRINGP (msg))
6840 message3_nolog (msg, STRING_BYTES (XSTRING (msg)), STRING_MULTIBYTE (msg));
6841 else
6842 message3_nolog (msg, 0, 0);
6843 }
6844
6845
6846 /* Pop the top-most entry off Vmessage_stack. */
6847
6848 void
6849 pop_message ()
6850 {
6851 xassert (CONSP (Vmessage_stack));
6852 Vmessage_stack = XCDR (Vmessage_stack);
6853 }
6854
6855
6856 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
6857 exits. If the stack is not empty, we have a missing pop_message
6858 somewhere. */
6859
6860 void
6861 check_message_stack ()
6862 {
6863 if (!NILP (Vmessage_stack))
6864 abort ();
6865 }
6866
6867
6868 /* Truncate to NCHARS what will be displayed in the echo area the next
6869 time we display it---but don't redisplay it now. */
6870
6871 void
6872 truncate_echo_area (nchars)
6873 int nchars;
6874 {
6875 if (nchars == 0)
6876 echo_area_buffer[0] = Qnil;
6877 /* A null message buffer means that the frame hasn't really been
6878 initialized yet. Error messages get reported properly by
6879 cmd_error, so this must be just an informative message; toss it. */
6880 else if (!noninteractive
6881 && INTERACTIVE
6882 && !NILP (echo_area_buffer[0]))
6883 {
6884 struct frame *sf = SELECTED_FRAME ();
6885 if (FRAME_MESSAGE_BUF (sf))
6886 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
6887 }
6888 }
6889
6890
6891 /* Helper function for truncate_echo_area. Truncate the current
6892 message to at most NCHARS characters. */
6893
6894 static int
6895 truncate_message_1 (nchars, a2, a3, a4)
6896 EMACS_INT nchars;
6897 Lisp_Object a2;
6898 EMACS_INT a3, a4;
6899 {
6900 if (BEG + nchars < Z)
6901 del_range (BEG + nchars, Z);
6902 if (Z == BEG)
6903 echo_area_buffer[0] = Qnil;
6904 return 0;
6905 }
6906
6907
6908 /* Set the current message to a substring of S or STRING.
6909
6910 If STRING is a Lisp string, set the message to the first NBYTES
6911 bytes from STRING. NBYTES zero means use the whole string. If
6912 STRING is multibyte, the message will be displayed multibyte.
6913
6914 If S is not null, set the message to the first LEN bytes of S. LEN
6915 zero means use the whole string. MULTIBYTE_P non-zero means S is
6916 multibyte. Display the message multibyte in that case. */
6917
6918 void
6919 set_message (s, string, nbytes, multibyte_p)
6920 char *s;
6921 Lisp_Object string;
6922 int nbytes, multibyte_p;
6923 {
6924 message_enable_multibyte
6925 = ((s && multibyte_p)
6926 || (STRINGP (string) && STRING_MULTIBYTE (string)));
6927
6928 with_echo_area_buffer (0, -1, set_message_1,
6929 (EMACS_INT) s, string, nbytes, multibyte_p);
6930 message_buf_print = 0;
6931 help_echo_showing_p = 0;
6932 }
6933
6934
6935 /* Helper function for set_message. Arguments have the same meaning
6936 as there, with A1 corresponding to S and A2 corresponding to STRING
6937 This function is called with the echo area buffer being
6938 current. */
6939
6940 static int
6941 set_message_1 (a1, a2, nbytes, multibyte_p)
6942 EMACS_INT a1;
6943 Lisp_Object a2;
6944 EMACS_INT nbytes, multibyte_p;
6945 {
6946 char *s = (char *) a1;
6947 Lisp_Object string = a2;
6948
6949 xassert (BEG == Z);
6950
6951 /* Change multibyteness of the echo buffer appropriately. */
6952 if (message_enable_multibyte
6953 != !NILP (current_buffer->enable_multibyte_characters))
6954 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil, Qnil);
6955
6956 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
6957
6958 /* Insert new message at BEG. */
6959 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6960
6961 if (STRINGP (string))
6962 {
6963 int nchars;
6964
6965 if (nbytes == 0)
6966 nbytes = XSTRING (string)->size_byte;
6967 nchars = string_byte_to_char (string, nbytes);
6968
6969 /* This function takes care of single/multibyte conversion. We
6970 just have to ensure that the echo area buffer has the right
6971 setting of enable_multibyte_characters. */
6972 insert_from_string (string, 0, 0, nchars, nbytes, 1);
6973 }
6974 else if (s)
6975 {
6976 if (nbytes == 0)
6977 nbytes = strlen (s);
6978
6979 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
6980 {
6981 /* Convert from multi-byte to single-byte. */
6982 int i, c, n;
6983 unsigned char work[1];
6984
6985 /* Convert a multibyte string to single-byte. */
6986 for (i = 0; i < nbytes; i += n)
6987 {
6988 c = string_char_and_length (s + i, nbytes - i, &n);
6989 work[0] = (ASCII_CHAR_P (c)
6990 ? c
6991 : multibyte_char_to_unibyte (c, Qnil));
6992 insert_1_both (work, 1, 1, 1, 0, 0);
6993 }
6994 }
6995 else if (!multibyte_p
6996 && !NILP (current_buffer->enable_multibyte_characters))
6997 {
6998 /* Convert from single-byte to multi-byte. */
6999 int i, c, n;
7000 unsigned char *msg = (unsigned char *) s;
7001 unsigned char str[MAX_MULTIBYTE_LENGTH];
7002
7003 /* Convert a single-byte string to multibyte. */
7004 for (i = 0; i < nbytes; i++)
7005 {
7006 c = unibyte_char_to_multibyte (msg[i]);
7007 n = CHAR_STRING (c, str);
7008 insert_1_both (str, 1, n, 1, 0, 0);
7009 }
7010 }
7011 else
7012 insert_1 (s, nbytes, 1, 0, 0);
7013 }
7014
7015 return 0;
7016 }
7017
7018
7019 /* Clear messages. CURRENT_P non-zero means clear the current
7020 message. LAST_DISPLAYED_P non-zero means clear the message
7021 last displayed. */
7022
7023 void
7024 clear_message (current_p, last_displayed_p)
7025 int current_p, last_displayed_p;
7026 {
7027 if (current_p)
7028 {
7029 echo_area_buffer[0] = Qnil;
7030 message_cleared_p = 1;
7031 }
7032
7033 if (last_displayed_p)
7034 echo_area_buffer[1] = Qnil;
7035
7036 message_buf_print = 0;
7037 }
7038
7039 /* Clear garbaged frames.
7040
7041 This function is used where the old redisplay called
7042 redraw_garbaged_frames which in turn called redraw_frame which in
7043 turn called clear_frame. The call to clear_frame was a source of
7044 flickering. I believe a clear_frame is not necessary. It should
7045 suffice in the new redisplay to invalidate all current matrices,
7046 and ensure a complete redisplay of all windows. */
7047
7048 static void
7049 clear_garbaged_frames ()
7050 {
7051 if (frame_garbaged)
7052 {
7053 Lisp_Object tail, frame;
7054
7055 FOR_EACH_FRAME (tail, frame)
7056 {
7057 struct frame *f = XFRAME (frame);
7058
7059 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
7060 {
7061 if (f->resized_p)
7062 Fredraw_frame (frame);
7063 clear_current_matrices (f);
7064 f->garbaged = 0;
7065 f->resized_p = 0;
7066 }
7067 }
7068
7069 frame_garbaged = 0;
7070 ++windows_or_buffers_changed;
7071 }
7072 }
7073
7074
7075 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
7076 is non-zero update selected_frame. Value is non-zero if the
7077 mini-windows height has been changed. */
7078
7079 static int
7080 echo_area_display (update_frame_p)
7081 int update_frame_p;
7082 {
7083 Lisp_Object mini_window;
7084 struct window *w;
7085 struct frame *f;
7086 int window_height_changed_p = 0;
7087 struct frame *sf = SELECTED_FRAME ();
7088
7089 mini_window = FRAME_MINIBUF_WINDOW (sf);
7090 w = XWINDOW (mini_window);
7091 f = XFRAME (WINDOW_FRAME (w));
7092
7093 /* Don't display if frame is invisible or not yet initialized. */
7094 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
7095 return 0;
7096
7097 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
7098 #ifndef macintosh
7099 #ifdef HAVE_WINDOW_SYSTEM
7100 /* When Emacs starts, selected_frame may be a visible terminal
7101 frame, even if we run under a window system. If we let this
7102 through, a message would be displayed on the terminal. */
7103 if (EQ (selected_frame, Vterminal_frame)
7104 && !NILP (Vwindow_system))
7105 return 0;
7106 #endif /* HAVE_WINDOW_SYSTEM */
7107 #endif
7108
7109 /* Redraw garbaged frames. */
7110 if (frame_garbaged)
7111 clear_garbaged_frames ();
7112
7113 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
7114 {
7115 echo_area_window = mini_window;
7116 window_height_changed_p = display_echo_area (w);
7117 w->must_be_updated_p = 1;
7118
7119 /* Update the display, unless called from redisplay_internal.
7120 Also don't update the screen during redisplay itself. The
7121 update will happen at the end of redisplay, and an update
7122 here could cause confusion. */
7123 if (update_frame_p && !redisplaying_p)
7124 {
7125 int n = 0;
7126
7127 /* If the display update has been interrupted by pending
7128 input, update mode lines in the frame. Due to the
7129 pending input, it might have been that redisplay hasn't
7130 been called, so that mode lines above the echo area are
7131 garbaged. This looks odd, so we prevent it here. */
7132 if (!display_completed)
7133 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
7134
7135 if (window_height_changed_p
7136 /* Don't do this if Emacs is shutting down. Redisplay
7137 needs to run hooks. */
7138 && !NILP (Vrun_hooks))
7139 {
7140 /* Must update other windows. Likewise as in other
7141 cases, don't let this update be interrupted by
7142 pending input. */
7143 int count = BINDING_STACK_SIZE ();
7144 specbind (Qredisplay_dont_pause, Qt);
7145 windows_or_buffers_changed = 1;
7146 redisplay_internal (0);
7147 unbind_to (count, Qnil);
7148 }
7149 else if (FRAME_WINDOW_P (f) && n == 0)
7150 {
7151 /* Window configuration is the same as before.
7152 Can do with a display update of the echo area,
7153 unless we displayed some mode lines. */
7154 update_single_window (w, 1);
7155 rif->flush_display (f);
7156 }
7157 else
7158 update_frame (f, 1, 1);
7159
7160 /* If cursor is in the echo area, make sure that the next
7161 redisplay displays the minibuffer, so that the cursor will
7162 be replaced with what the minibuffer wants. */
7163 if (cursor_in_echo_area)
7164 ++windows_or_buffers_changed;
7165 }
7166 }
7167 else if (!EQ (mini_window, selected_window))
7168 windows_or_buffers_changed++;
7169
7170 /* Last displayed message is now the current message. */
7171 echo_area_buffer[1] = echo_area_buffer[0];
7172
7173 /* Prevent redisplay optimization in redisplay_internal by resetting
7174 this_line_start_pos. This is done because the mini-buffer now
7175 displays the message instead of its buffer text. */
7176 if (EQ (mini_window, selected_window))
7177 CHARPOS (this_line_start_pos) = 0;
7178
7179 return window_height_changed_p;
7180 }
7181
7182
7183 \f
7184 /***********************************************************************
7185 Frame Titles
7186 ***********************************************************************/
7187
7188
7189 #ifdef HAVE_WINDOW_SYSTEM
7190
7191 /* A buffer for constructing frame titles in it; allocated from the
7192 heap in init_xdisp and resized as needed in store_frame_title_char. */
7193
7194 static char *frame_title_buf;
7195
7196 /* The buffer's end, and a current output position in it. */
7197
7198 static char *frame_title_buf_end;
7199 static char *frame_title_ptr;
7200
7201
7202 /* Store a single character C for the frame title in frame_title_buf.
7203 Re-allocate frame_title_buf if necessary. */
7204
7205 static void
7206 store_frame_title_char (c)
7207 char c;
7208 {
7209 /* If output position has reached the end of the allocated buffer,
7210 double the buffer's size. */
7211 if (frame_title_ptr == frame_title_buf_end)
7212 {
7213 int len = frame_title_ptr - frame_title_buf;
7214 int new_size = 2 * len * sizeof *frame_title_buf;
7215 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
7216 frame_title_buf_end = frame_title_buf + new_size;
7217 frame_title_ptr = frame_title_buf + len;
7218 }
7219
7220 *frame_title_ptr++ = c;
7221 }
7222
7223
7224 /* Store part of a frame title in frame_title_buf, beginning at
7225 frame_title_ptr. STR is the string to store. Do not copy
7226 characters that yield more columns than PRECISION; PRECISION <= 0
7227 means copy the whole string. Pad with spaces until FIELD_WIDTH
7228 number of characters have been copied; FIELD_WIDTH <= 0 means don't
7229 pad. Called from display_mode_element when it is used to build a
7230 frame title. */
7231
7232 static int
7233 store_frame_title (str, field_width, precision)
7234 unsigned char *str;
7235 int field_width, precision;
7236 {
7237 int n = 0;
7238 int dummy, nbytes;
7239
7240 /* Copy at most PRECISION chars from STR. */
7241 nbytes = strlen (str);
7242 n+= c_string_width (str, nbytes, precision, &dummy, &nbytes);
7243 while (nbytes--)
7244 store_frame_title_char (*str++);
7245
7246 /* Fill up with spaces until FIELD_WIDTH reached. */
7247 while (field_width > 0
7248 && n < field_width)
7249 {
7250 store_frame_title_char (' ');
7251 ++n;
7252 }
7253
7254 return n;
7255 }
7256
7257
7258 /* Set the title of FRAME, if it has changed. The title format is
7259 Vicon_title_format if FRAME is iconified, otherwise it is
7260 frame_title_format. */
7261
7262 static void
7263 x_consider_frame_title (frame)
7264 Lisp_Object frame;
7265 {
7266 struct frame *f = XFRAME (frame);
7267
7268 if (FRAME_WINDOW_P (f)
7269 || FRAME_MINIBUF_ONLY_P (f)
7270 || f->explicit_name)
7271 {
7272 /* Do we have more than one visible frame on this X display? */
7273 Lisp_Object tail;
7274 Lisp_Object fmt;
7275 struct buffer *obuf;
7276 int len;
7277 struct it it;
7278
7279 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
7280 {
7281 Lisp_Object other_frame = XCAR (tail);
7282 struct frame *tf = XFRAME (other_frame);
7283
7284 if (tf != f
7285 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
7286 && !FRAME_MINIBUF_ONLY_P (tf)
7287 && !EQ (other_frame, tip_frame)
7288 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
7289 break;
7290 }
7291
7292 /* Set global variable indicating that multiple frames exist. */
7293 multiple_frames = CONSP (tail);
7294
7295 /* Switch to the buffer of selected window of the frame. Set up
7296 frame_title_ptr so that display_mode_element will output into it;
7297 then display the title. */
7298 obuf = current_buffer;
7299 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
7300 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
7301 frame_title_ptr = frame_title_buf;
7302 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
7303 NULL, DEFAULT_FACE_ID);
7304 display_mode_element (&it, 0, -1, -1, fmt, Qnil);
7305 len = frame_title_ptr - frame_title_buf;
7306 frame_title_ptr = NULL;
7307 set_buffer_internal_1 (obuf);
7308
7309 /* Set the title only if it's changed. This avoids consing in
7310 the common case where it hasn't. (If it turns out that we've
7311 already wasted too much time by walking through the list with
7312 display_mode_element, then we might need to optimize at a
7313 higher level than this.) */
7314 if (! STRINGP (f->name)
7315 || STRING_BYTES (XSTRING (f->name)) != len
7316 || bcmp (frame_title_buf, XSTRING (f->name)->data, len) != 0)
7317 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
7318 }
7319 }
7320
7321 #else /* not HAVE_WINDOW_SYSTEM */
7322
7323 #define frame_title_ptr ((char *)0)
7324 #define store_frame_title(str, mincol, maxcol) 0
7325
7326 #endif /* not HAVE_WINDOW_SYSTEM */
7327
7328
7329
7330 \f
7331 /***********************************************************************
7332 Menu Bars
7333 ***********************************************************************/
7334
7335
7336 /* Prepare for redisplay by updating menu-bar item lists when
7337 appropriate. This can call eval. */
7338
7339 void
7340 prepare_menu_bars ()
7341 {
7342 int all_windows;
7343 struct gcpro gcpro1, gcpro2;
7344 struct frame *f;
7345 Lisp_Object tooltip_frame;
7346
7347 #ifdef HAVE_WINDOW_SYSTEM
7348 tooltip_frame = tip_frame;
7349 #else
7350 tooltip_frame = Qnil;
7351 #endif
7352
7353 /* Update all frame titles based on their buffer names, etc. We do
7354 this before the menu bars so that the buffer-menu will show the
7355 up-to-date frame titles. */
7356 #ifdef HAVE_WINDOW_SYSTEM
7357 if (windows_or_buffers_changed || update_mode_lines)
7358 {
7359 Lisp_Object tail, frame;
7360
7361 FOR_EACH_FRAME (tail, frame)
7362 {
7363 f = XFRAME (frame);
7364 if (!EQ (frame, tooltip_frame)
7365 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
7366 x_consider_frame_title (frame);
7367 }
7368 }
7369 #endif /* HAVE_WINDOW_SYSTEM */
7370
7371 /* Update the menu bar item lists, if appropriate. This has to be
7372 done before any actual redisplay or generation of display lines. */
7373 all_windows = (update_mode_lines
7374 || buffer_shared > 1
7375 || windows_or_buffers_changed);
7376 if (all_windows)
7377 {
7378 Lisp_Object tail, frame;
7379 int count = BINDING_STACK_SIZE ();
7380
7381 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7382
7383 FOR_EACH_FRAME (tail, frame)
7384 {
7385 f = XFRAME (frame);
7386
7387 /* Ignore tooltip frame. */
7388 if (EQ (frame, tooltip_frame))
7389 continue;
7390
7391 /* If a window on this frame changed size, report that to
7392 the user and clear the size-change flag. */
7393 if (FRAME_WINDOW_SIZES_CHANGED (f))
7394 {
7395 Lisp_Object functions;
7396
7397 /* Clear flag first in case we get an error below. */
7398 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
7399 functions = Vwindow_size_change_functions;
7400 GCPRO2 (tail, functions);
7401
7402 while (CONSP (functions))
7403 {
7404 call1 (XCAR (functions), frame);
7405 functions = XCDR (functions);
7406 }
7407 UNGCPRO;
7408 }
7409
7410 GCPRO1 (tail);
7411 update_menu_bar (f, 0);
7412 #ifdef HAVE_WINDOW_SYSTEM
7413 update_tool_bar (f, 0);
7414 #endif
7415 UNGCPRO;
7416 }
7417
7418 unbind_to (count, Qnil);
7419 }
7420 else
7421 {
7422 struct frame *sf = SELECTED_FRAME ();
7423 update_menu_bar (sf, 1);
7424 #ifdef HAVE_WINDOW_SYSTEM
7425 update_tool_bar (sf, 1);
7426 #endif
7427 }
7428
7429 /* Motif needs this. See comment in xmenu.c. Turn it off when
7430 pending_menu_activation is not defined. */
7431 #ifdef USE_X_TOOLKIT
7432 pending_menu_activation = 0;
7433 #endif
7434 }
7435
7436
7437 /* Update the menu bar item list for frame F. This has to be done
7438 before we start to fill in any display lines, because it can call
7439 eval.
7440
7441 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
7442
7443 static void
7444 update_menu_bar (f, save_match_data)
7445 struct frame *f;
7446 int save_match_data;
7447 {
7448 Lisp_Object window;
7449 register struct window *w;
7450
7451 /* If called recursively during a menu update, do nothing. This can
7452 happen when, for instance, an activate-menubar-hook causes a
7453 redisplay. */
7454 if (inhibit_menubar_update)
7455 return;
7456
7457 window = FRAME_SELECTED_WINDOW (f);
7458 w = XWINDOW (window);
7459
7460 if (update_mode_lines)
7461 w->update_mode_line = Qt;
7462
7463 if (FRAME_WINDOW_P (f)
7464 ?
7465 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
7466 FRAME_EXTERNAL_MENU_BAR (f)
7467 #else
7468 FRAME_MENU_BAR_LINES (f) > 0
7469 #endif
7470 : FRAME_MENU_BAR_LINES (f) > 0)
7471 {
7472 /* If the user has switched buffers or windows, we need to
7473 recompute to reflect the new bindings. But we'll
7474 recompute when update_mode_lines is set too; that means
7475 that people can use force-mode-line-update to request
7476 that the menu bar be recomputed. The adverse effect on
7477 the rest of the redisplay algorithm is about the same as
7478 windows_or_buffers_changed anyway. */
7479 if (windows_or_buffers_changed
7480 || !NILP (w->update_mode_line)
7481 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7482 < BUF_MODIFF (XBUFFER (w->buffer)))
7483 != !NILP (w->last_had_star))
7484 || ((!NILP (Vtransient_mark_mode)
7485 && !NILP (XBUFFER (w->buffer)->mark_active))
7486 != !NILP (w->region_showing)))
7487 {
7488 struct buffer *prev = current_buffer;
7489 int count = BINDING_STACK_SIZE ();
7490
7491 specbind (Qinhibit_menubar_update, Qt);
7492
7493 set_buffer_internal_1 (XBUFFER (w->buffer));
7494 if (save_match_data)
7495 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7496 if (NILP (Voverriding_local_map_menu_flag))
7497 {
7498 specbind (Qoverriding_terminal_local_map, Qnil);
7499 specbind (Qoverriding_local_map, Qnil);
7500 }
7501
7502 /* Run the Lucid hook. */
7503 safe_run_hooks (Qactivate_menubar_hook);
7504
7505 /* If it has changed current-menubar from previous value,
7506 really recompute the menu-bar from the value. */
7507 if (! NILP (Vlucid_menu_bar_dirty_flag))
7508 call0 (Qrecompute_lucid_menubar);
7509
7510 safe_run_hooks (Qmenu_bar_update_hook);
7511 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
7512
7513 /* Redisplay the menu bar in case we changed it. */
7514 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
7515 if (FRAME_WINDOW_P (f)
7516 #if defined (macintosh)
7517 /* All frames on Mac OS share the same menubar. So only the
7518 selected frame should be allowed to set it. */
7519 && f == SELECTED_FRAME ()
7520 #endif
7521 )
7522 set_frame_menubar (f, 0, 0);
7523 else
7524 /* On a terminal screen, the menu bar is an ordinary screen
7525 line, and this makes it get updated. */
7526 w->update_mode_line = Qt;
7527 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7528 /* In the non-toolkit version, the menu bar is an ordinary screen
7529 line, and this makes it get updated. */
7530 w->update_mode_line = Qt;
7531 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7532
7533 unbind_to (count, Qnil);
7534 set_buffer_internal_1 (prev);
7535 }
7536 }
7537 }
7538
7539
7540 \f
7541 /***********************************************************************
7542 Tool-bars
7543 ***********************************************************************/
7544
7545 #ifdef HAVE_WINDOW_SYSTEM
7546
7547 /* Update the tool-bar item list for frame F. This has to be done
7548 before we start to fill in any display lines. Called from
7549 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
7550 and restore it here. */
7551
7552 static void
7553 update_tool_bar (f, save_match_data)
7554 struct frame *f;
7555 int save_match_data;
7556 {
7557 if (WINDOWP (f->tool_bar_window)
7558 && XFASTINT (XWINDOW (f->tool_bar_window)->height) > 0)
7559 {
7560 Lisp_Object window;
7561 struct window *w;
7562
7563 window = FRAME_SELECTED_WINDOW (f);
7564 w = XWINDOW (window);
7565
7566 /* If the user has switched buffers or windows, we need to
7567 recompute to reflect the new bindings. But we'll
7568 recompute when update_mode_lines is set too; that means
7569 that people can use force-mode-line-update to request
7570 that the menu bar be recomputed. The adverse effect on
7571 the rest of the redisplay algorithm is about the same as
7572 windows_or_buffers_changed anyway. */
7573 if (windows_or_buffers_changed
7574 || !NILP (w->update_mode_line)
7575 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7576 < BUF_MODIFF (XBUFFER (w->buffer)))
7577 != !NILP (w->last_had_star))
7578 || ((!NILP (Vtransient_mark_mode)
7579 && !NILP (XBUFFER (w->buffer)->mark_active))
7580 != !NILP (w->region_showing)))
7581 {
7582 struct buffer *prev = current_buffer;
7583 int count = BINDING_STACK_SIZE ();
7584
7585 /* Set current_buffer to the buffer of the selected
7586 window of the frame, so that we get the right local
7587 keymaps. */
7588 set_buffer_internal_1 (XBUFFER (w->buffer));
7589
7590 /* Save match data, if we must. */
7591 if (save_match_data)
7592 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7593
7594 /* Make sure that we don't accidentally use bogus keymaps. */
7595 if (NILP (Voverriding_local_map_menu_flag))
7596 {
7597 specbind (Qoverriding_terminal_local_map, Qnil);
7598 specbind (Qoverriding_local_map, Qnil);
7599 }
7600
7601 /* Build desired tool-bar items from keymaps. */
7602 f->tool_bar_items
7603 = tool_bar_items (f->tool_bar_items, &f->n_tool_bar_items);
7604
7605 /* Redisplay the tool-bar in case we changed it. */
7606 w->update_mode_line = Qt;
7607
7608 unbind_to (count, Qnil);
7609 set_buffer_internal_1 (prev);
7610 }
7611 }
7612 }
7613
7614
7615 /* Set F->desired_tool_bar_string to a Lisp string representing frame
7616 F's desired tool-bar contents. F->tool_bar_items must have
7617 been set up previously by calling prepare_menu_bars. */
7618
7619 static void
7620 build_desired_tool_bar_string (f)
7621 struct frame *f;
7622 {
7623 int i, size, size_needed;
7624 struct gcpro gcpro1, gcpro2, gcpro3;
7625 Lisp_Object image, plist, props;
7626
7627 image = plist = props = Qnil;
7628 GCPRO3 (image, plist, props);
7629
7630 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
7631 Otherwise, make a new string. */
7632
7633 /* The size of the string we might be able to reuse. */
7634 size = (STRINGP (f->desired_tool_bar_string)
7635 ? XSTRING (f->desired_tool_bar_string)->size
7636 : 0);
7637
7638 /* We need one space in the string for each image. */
7639 size_needed = f->n_tool_bar_items;
7640
7641 /* Reuse f->desired_tool_bar_string, if possible. */
7642 if (size < size_needed || NILP (f->desired_tool_bar_string))
7643 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
7644 make_number (' '));
7645 else
7646 {
7647 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
7648 Fremove_text_properties (make_number (0), make_number (size),
7649 props, f->desired_tool_bar_string);
7650 }
7651
7652 /* Put a `display' property on the string for the images to display,
7653 put a `menu_item' property on tool-bar items with a value that
7654 is the index of the item in F's tool-bar item vector. */
7655 for (i = 0; i < f->n_tool_bar_items; ++i)
7656 {
7657 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
7658
7659 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
7660 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
7661 int hmargin, vmargin, relief, idx, end;
7662 extern Lisp_Object QCrelief, QCmargin, QCconversion, Qimage;
7663
7664 /* If image is a vector, choose the image according to the
7665 button state. */
7666 image = PROP (TOOL_BAR_ITEM_IMAGES);
7667 if (VECTORP (image))
7668 {
7669 if (enabled_p)
7670 idx = (selected_p
7671 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
7672 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
7673 else
7674 idx = (selected_p
7675 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
7676 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
7677
7678 xassert (ASIZE (image) >= idx);
7679 image = AREF (image, idx);
7680 }
7681 else
7682 idx = -1;
7683
7684 /* Ignore invalid image specifications. */
7685 if (!valid_image_p (image))
7686 continue;
7687
7688 /* Display the tool-bar button pressed, or depressed. */
7689 plist = Fcopy_sequence (XCDR (image));
7690
7691 /* Compute margin and relief to draw. */
7692 relief = (tool_bar_button_relief >= 0
7693 ? tool_bar_button_relief
7694 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
7695 hmargin = vmargin = relief;
7696
7697 if (INTEGERP (Vtool_bar_button_margin)
7698 && XINT (Vtool_bar_button_margin) > 0)
7699 {
7700 hmargin += XFASTINT (Vtool_bar_button_margin);
7701 vmargin += XFASTINT (Vtool_bar_button_margin);
7702 }
7703 else if (CONSP (Vtool_bar_button_margin))
7704 {
7705 if (INTEGERP (XCAR (Vtool_bar_button_margin))
7706 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
7707 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
7708
7709 if (INTEGERP (XCDR (Vtool_bar_button_margin))
7710 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
7711 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
7712 }
7713
7714 if (auto_raise_tool_bar_buttons_p)
7715 {
7716 /* Add a `:relief' property to the image spec if the item is
7717 selected. */
7718 if (selected_p)
7719 {
7720 plist = Fplist_put (plist, QCrelief, make_number (-relief));
7721 hmargin -= relief;
7722 vmargin -= relief;
7723 }
7724 }
7725 else
7726 {
7727 /* If image is selected, display it pressed, i.e. with a
7728 negative relief. If it's not selected, display it with a
7729 raised relief. */
7730 plist = Fplist_put (plist, QCrelief,
7731 (selected_p
7732 ? make_number (-relief)
7733 : make_number (relief)));
7734 hmargin -= relief;
7735 vmargin -= relief;
7736 }
7737
7738 /* Put a margin around the image. */
7739 if (hmargin || vmargin)
7740 {
7741 if (hmargin == vmargin)
7742 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
7743 else
7744 plist = Fplist_put (plist, QCmargin,
7745 Fcons (make_number (hmargin),
7746 make_number (vmargin)));
7747 }
7748
7749 /* If button is not enabled, and we don't have special images
7750 for the disabled state, make the image appear disabled by
7751 applying an appropriate algorithm to it. */
7752 if (!enabled_p && idx < 0)
7753 plist = Fplist_put (plist, QCconversion, Qdisabled);
7754
7755 /* Put a `display' text property on the string for the image to
7756 display. Put a `menu-item' property on the string that gives
7757 the start of this item's properties in the tool-bar items
7758 vector. */
7759 image = Fcons (Qimage, plist);
7760 props = list4 (Qdisplay, image,
7761 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
7762
7763 /* Let the last image hide all remaining spaces in the tool bar
7764 string. The string can be longer than needed when we reuse a
7765 previous string. */
7766 if (i + 1 == f->n_tool_bar_items)
7767 end = XSTRING (f->desired_tool_bar_string)->size;
7768 else
7769 end = i + 1;
7770 Fadd_text_properties (make_number (i), make_number (end),
7771 props, f->desired_tool_bar_string);
7772 #undef PROP
7773 }
7774
7775 UNGCPRO;
7776 }
7777
7778
7779 /* Display one line of the tool-bar of frame IT->f. */
7780
7781 static void
7782 display_tool_bar_line (it)
7783 struct it *it;
7784 {
7785 struct glyph_row *row = it->glyph_row;
7786 int max_x = it->last_visible_x;
7787 struct glyph *last;
7788
7789 prepare_desired_row (row);
7790 row->y = it->current_y;
7791
7792 /* Note that this isn't made use of if the face hasn't a box,
7793 so there's no need to check the face here. */
7794 it->start_of_box_run_p = 1;
7795
7796 while (it->current_x < max_x)
7797 {
7798 int x_before, x, n_glyphs_before, i, nglyphs;
7799
7800 /* Get the next display element. */
7801 if (!get_next_display_element (it))
7802 break;
7803
7804 /* Produce glyphs. */
7805 x_before = it->current_x;
7806 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
7807 PRODUCE_GLYPHS (it);
7808
7809 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
7810 i = 0;
7811 x = x_before;
7812 while (i < nglyphs)
7813 {
7814 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
7815
7816 if (x + glyph->pixel_width > max_x)
7817 {
7818 /* Glyph doesn't fit on line. */
7819 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
7820 it->current_x = x;
7821 goto out;
7822 }
7823
7824 ++it->hpos;
7825 x += glyph->pixel_width;
7826 ++i;
7827 }
7828
7829 /* Stop at line ends. */
7830 if (ITERATOR_AT_END_OF_LINE_P (it))
7831 break;
7832
7833 set_iterator_to_next (it, 1);
7834 }
7835
7836 out:;
7837
7838 row->displays_text_p = row->used[TEXT_AREA] != 0;
7839 extend_face_to_end_of_line (it);
7840 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
7841 last->right_box_line_p = 1;
7842 if (last == row->glyphs[TEXT_AREA])
7843 last->left_box_line_p = 1;
7844 compute_line_metrics (it);
7845
7846 /* If line is empty, make it occupy the rest of the tool-bar. */
7847 if (!row->displays_text_p)
7848 {
7849 row->height = row->phys_height = it->last_visible_y - row->y;
7850 row->ascent = row->phys_ascent = 0;
7851 }
7852
7853 row->full_width_p = 1;
7854 row->continued_p = 0;
7855 row->truncated_on_left_p = 0;
7856 row->truncated_on_right_p = 0;
7857
7858 it->current_x = it->hpos = 0;
7859 it->current_y += row->height;
7860 ++it->vpos;
7861 ++it->glyph_row;
7862 }
7863
7864
7865 /* Value is the number of screen lines needed to make all tool-bar
7866 items of frame F visible. */
7867
7868 static int
7869 tool_bar_lines_needed (f)
7870 struct frame *f;
7871 {
7872 struct window *w = XWINDOW (f->tool_bar_window);
7873 struct it it;
7874
7875 /* Initialize an iterator for iteration over
7876 F->desired_tool_bar_string in the tool-bar window of frame F. */
7877 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7878 it.first_visible_x = 0;
7879 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7880 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7881
7882 while (!ITERATOR_AT_END_P (&it))
7883 {
7884 it.glyph_row = w->desired_matrix->rows;
7885 clear_glyph_row (it.glyph_row);
7886 display_tool_bar_line (&it);
7887 }
7888
7889 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
7890 }
7891
7892
7893 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
7894 0, 1, 0,
7895 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
7896 (frame)
7897 Lisp_Object frame;
7898 {
7899 struct frame *f;
7900 struct window *w;
7901 int nlines = 0;
7902
7903 if (NILP (frame))
7904 frame = selected_frame;
7905 else
7906 CHECK_FRAME (frame);
7907 f = XFRAME (frame);
7908
7909 if (WINDOWP (f->tool_bar_window)
7910 || (w = XWINDOW (f->tool_bar_window),
7911 XFASTINT (w->height) > 0))
7912 {
7913 update_tool_bar (f, 1);
7914 if (f->n_tool_bar_items)
7915 {
7916 build_desired_tool_bar_string (f);
7917 nlines = tool_bar_lines_needed (f);
7918 }
7919 }
7920
7921 return make_number (nlines);
7922 }
7923
7924
7925 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
7926 height should be changed. */
7927
7928 static int
7929 redisplay_tool_bar (f)
7930 struct frame *f;
7931 {
7932 struct window *w;
7933 struct it it;
7934 struct glyph_row *row;
7935 int change_height_p = 0;
7936
7937 /* If frame hasn't a tool-bar window or if it is zero-height, don't
7938 do anything. This means you must start with tool-bar-lines
7939 non-zero to get the auto-sizing effect. Or in other words, you
7940 can turn off tool-bars by specifying tool-bar-lines zero. */
7941 if (!WINDOWP (f->tool_bar_window)
7942 || (w = XWINDOW (f->tool_bar_window),
7943 XFASTINT (w->height) == 0))
7944 return 0;
7945
7946 /* Set up an iterator for the tool-bar window. */
7947 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7948 it.first_visible_x = 0;
7949 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7950 row = it.glyph_row;
7951
7952 /* Build a string that represents the contents of the tool-bar. */
7953 build_desired_tool_bar_string (f);
7954 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7955
7956 /* Display as many lines as needed to display all tool-bar items. */
7957 while (it.current_y < it.last_visible_y)
7958 display_tool_bar_line (&it);
7959
7960 /* It doesn't make much sense to try scrolling in the tool-bar
7961 window, so don't do it. */
7962 w->desired_matrix->no_scrolling_p = 1;
7963 w->must_be_updated_p = 1;
7964
7965 if (auto_resize_tool_bars_p)
7966 {
7967 int nlines;
7968
7969 /* If we couldn't display everything, change the tool-bar's
7970 height. */
7971 if (IT_STRING_CHARPOS (it) < it.end_charpos)
7972 change_height_p = 1;
7973
7974 /* If there are blank lines at the end, except for a partially
7975 visible blank line at the end that is smaller than
7976 CANON_Y_UNIT, change the tool-bar's height. */
7977 row = it.glyph_row - 1;
7978 if (!row->displays_text_p
7979 && row->height >= CANON_Y_UNIT (f))
7980 change_height_p = 1;
7981
7982 /* If row displays tool-bar items, but is partially visible,
7983 change the tool-bar's height. */
7984 if (row->displays_text_p
7985 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
7986 change_height_p = 1;
7987
7988 /* Resize windows as needed by changing the `tool-bar-lines'
7989 frame parameter. */
7990 if (change_height_p
7991 && (nlines = tool_bar_lines_needed (f),
7992 nlines != XFASTINT (w->height)))
7993 {
7994 extern Lisp_Object Qtool_bar_lines;
7995 Lisp_Object frame;
7996 int old_height = XFASTINT (w->height);
7997
7998 XSETFRAME (frame, f);
7999 clear_glyph_matrix (w->desired_matrix);
8000 Fmodify_frame_parameters (frame,
8001 Fcons (Fcons (Qtool_bar_lines,
8002 make_number (nlines)),
8003 Qnil));
8004 if (XFASTINT (w->height) != old_height)
8005 fonts_changed_p = 1;
8006 }
8007 }
8008
8009 return change_height_p;
8010 }
8011
8012
8013 /* Get information about the tool-bar item which is displayed in GLYPH
8014 on frame F. Return in *PROP_IDX the index where tool-bar item
8015 properties start in F->tool_bar_items. Value is zero if
8016 GLYPH doesn't display a tool-bar item. */
8017
8018 int
8019 tool_bar_item_info (f, glyph, prop_idx)
8020 struct frame *f;
8021 struct glyph *glyph;
8022 int *prop_idx;
8023 {
8024 Lisp_Object prop;
8025 int success_p;
8026 int charpos;
8027
8028 /* This function can be called asynchronously, which means we must
8029 exclude any possibility that Fget_text_property signals an
8030 error. */
8031 charpos = min (XSTRING (f->current_tool_bar_string)->size, glyph->charpos);
8032 charpos = max (0, charpos);
8033
8034 /* Get the text property `menu-item' at pos. The value of that
8035 property is the start index of this item's properties in
8036 F->tool_bar_items. */
8037 prop = Fget_text_property (make_number (charpos),
8038 Qmenu_item, f->current_tool_bar_string);
8039 if (INTEGERP (prop))
8040 {
8041 *prop_idx = XINT (prop);
8042 success_p = 1;
8043 }
8044 else
8045 success_p = 0;
8046
8047 return success_p;
8048 }
8049
8050 #endif /* HAVE_WINDOW_SYSTEM */
8051
8052
8053 \f
8054 /************************************************************************
8055 Horizontal scrolling
8056 ************************************************************************/
8057
8058 static int hscroll_window_tree P_ ((Lisp_Object));
8059 static int hscroll_windows P_ ((Lisp_Object));
8060
8061 /* For all leaf windows in the window tree rooted at WINDOW, set their
8062 hscroll value so that PT is (i) visible in the window, and (ii) so
8063 that it is not within a certain margin at the window's left and
8064 right border. Value is non-zero if any window's hscroll has been
8065 changed. */
8066
8067 static int
8068 hscroll_window_tree (window)
8069 Lisp_Object window;
8070 {
8071 int hscrolled_p = 0;
8072 int hscroll_relative_p = FLOATP (Vautomatic_hscroll_step);
8073 int hscroll_step_abs = 0;
8074 double hscroll_step_rel = 0;
8075
8076 if (hscroll_relative_p)
8077 {
8078 hscroll_step_rel = XFLOAT_DATA (Vautomatic_hscroll_step);
8079 if (hscroll_step_rel < 0)
8080 {
8081 hscroll_relative_p = 0;
8082 hscroll_step_abs = 0;
8083 }
8084 }
8085 else if (INTEGERP (Vautomatic_hscroll_step))
8086 {
8087 hscroll_step_abs = XINT (Vautomatic_hscroll_step);
8088 if (hscroll_step_abs < 0)
8089 hscroll_step_abs = 0;
8090 }
8091 else
8092 hscroll_step_abs = 0;
8093
8094 while (WINDOWP (window))
8095 {
8096 struct window *w = XWINDOW (window);
8097
8098 if (WINDOWP (w->hchild))
8099 hscrolled_p |= hscroll_window_tree (w->hchild);
8100 else if (WINDOWP (w->vchild))
8101 hscrolled_p |= hscroll_window_tree (w->vchild);
8102 else if (w->cursor.vpos >= 0)
8103 {
8104 int hscroll_margin, text_area_x, text_area_y;
8105 int text_area_width, text_area_height;
8106 struct glyph_row *current_cursor_row
8107 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
8108 struct glyph_row *desired_cursor_row
8109 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
8110 struct glyph_row *cursor_row
8111 = (desired_cursor_row->enabled_p
8112 ? desired_cursor_row
8113 : current_cursor_row);
8114
8115 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
8116 &text_area_width, &text_area_height);
8117
8118 /* Scroll when cursor is inside this scroll margin. */
8119 hscroll_margin
8120 = automatic_hscroll_margin * CANON_X_UNIT (XFRAME (w->frame));
8121
8122 if ((XFASTINT (w->hscroll)
8123 && w->cursor.x <= hscroll_margin)
8124 || (cursor_row->enabled_p
8125 && cursor_row->truncated_on_right_p
8126 && (w->cursor.x >= text_area_width - hscroll_margin)))
8127 {
8128 struct it it;
8129 int hscroll;
8130 struct buffer *saved_current_buffer;
8131 int pt;
8132 int wanted_x;
8133
8134 /* Find point in a display of infinite width. */
8135 saved_current_buffer = current_buffer;
8136 current_buffer = XBUFFER (w->buffer);
8137
8138 if (w == XWINDOW (selected_window))
8139 pt = BUF_PT (current_buffer);
8140 else
8141 {
8142 pt = marker_position (w->pointm);
8143 pt = max (BEGV, pt);
8144 pt = min (ZV, pt);
8145 }
8146
8147 /* Move iterator to pt starting at cursor_row->start in
8148 a line with infinite width. */
8149 init_to_row_start (&it, w, cursor_row);
8150 it.last_visible_x = INFINITY;
8151 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
8152 current_buffer = saved_current_buffer;
8153
8154 /* Position cursor in window. */
8155 if (!hscroll_relative_p && hscroll_step_abs == 0)
8156 hscroll = max (0, it.current_x - text_area_width / 2)
8157 / CANON_X_UNIT (it.f);
8158 else if (w->cursor.x >= text_area_width - hscroll_margin)
8159 {
8160 if (hscroll_relative_p)
8161 wanted_x = text_area_width * (1 - hscroll_step_rel)
8162 - hscroll_margin;
8163 else
8164 wanted_x = text_area_width
8165 - hscroll_step_abs * CANON_X_UNIT (it.f)
8166 - hscroll_margin;
8167 hscroll
8168 = max (0, it.current_x - wanted_x) / CANON_X_UNIT (it.f);
8169 }
8170 else
8171 {
8172 if (hscroll_relative_p)
8173 wanted_x = text_area_width * hscroll_step_rel
8174 + hscroll_margin;
8175 else
8176 wanted_x = hscroll_step_abs * CANON_X_UNIT (it.f)
8177 + hscroll_margin;
8178 hscroll
8179 = max (0, it.current_x - wanted_x) / CANON_X_UNIT (it.f);
8180 }
8181 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
8182
8183 /* Don't call Fset_window_hscroll if value hasn't
8184 changed because it will prevent redisplay
8185 optimizations. */
8186 if (XFASTINT (w->hscroll) != hscroll)
8187 {
8188 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
8189 w->hscroll = make_number (hscroll);
8190 hscrolled_p = 1;
8191 }
8192 }
8193 }
8194
8195 window = w->next;
8196 }
8197
8198 /* Value is non-zero if hscroll of any leaf window has been changed. */
8199 return hscrolled_p;
8200 }
8201
8202
8203 /* Set hscroll so that cursor is visible and not inside horizontal
8204 scroll margins for all windows in the tree rooted at WINDOW. See
8205 also hscroll_window_tree above. Value is non-zero if any window's
8206 hscroll has been changed. If it has, desired matrices on the frame
8207 of WINDOW are cleared. */
8208
8209 static int
8210 hscroll_windows (window)
8211 Lisp_Object window;
8212 {
8213 int hscrolled_p;
8214
8215 if (automatic_hscrolling_p)
8216 {
8217 hscrolled_p = hscroll_window_tree (window);
8218 if (hscrolled_p)
8219 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
8220 }
8221 else
8222 hscrolled_p = 0;
8223 return hscrolled_p;
8224 }
8225
8226
8227 \f
8228 /************************************************************************
8229 Redisplay
8230 ************************************************************************/
8231
8232 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
8233 to a non-zero value. This is sometimes handy to have in a debugger
8234 session. */
8235
8236 #if GLYPH_DEBUG
8237
8238 /* First and last unchanged row for try_window_id. */
8239
8240 int debug_first_unchanged_at_end_vpos;
8241 int debug_last_unchanged_at_beg_vpos;
8242
8243 /* Delta vpos and y. */
8244
8245 int debug_dvpos, debug_dy;
8246
8247 /* Delta in characters and bytes for try_window_id. */
8248
8249 int debug_delta, debug_delta_bytes;
8250
8251 /* Values of window_end_pos and window_end_vpos at the end of
8252 try_window_id. */
8253
8254 int debug_end_pos, debug_end_vpos;
8255
8256 /* Append a string to W->desired_matrix->method. FMT is a printf
8257 format string. A1...A9 are a supplement for a variable-length
8258 argument list. If trace_redisplay_p is non-zero also printf the
8259 resulting string to stderr. */
8260
8261 static void
8262 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
8263 struct window *w;
8264 char *fmt;
8265 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
8266 {
8267 char buffer[512];
8268 char *method = w->desired_matrix->method;
8269 int len = strlen (method);
8270 int size = sizeof w->desired_matrix->method;
8271 int remaining = size - len - 1;
8272
8273 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
8274 if (len && remaining)
8275 {
8276 method[len] = '|';
8277 --remaining, ++len;
8278 }
8279
8280 strncpy (method + len, buffer, remaining);
8281
8282 if (trace_redisplay_p)
8283 fprintf (stderr, "%p (%s): %s\n",
8284 w,
8285 ((BUFFERP (w->buffer)
8286 && STRINGP (XBUFFER (w->buffer)->name))
8287 ? (char *) XSTRING (XBUFFER (w->buffer)->name)->data
8288 : "no buffer"),
8289 buffer);
8290 }
8291
8292 #endif /* GLYPH_DEBUG */
8293
8294
8295 /* This counter is used to clear the face cache every once in a while
8296 in redisplay_internal. It is incremented for each redisplay.
8297 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
8298 cleared. */
8299
8300 #define CLEAR_FACE_CACHE_COUNT 500
8301 static int clear_face_cache_count;
8302
8303 /* Record the previous terminal frame we displayed. */
8304
8305 static struct frame *previous_terminal_frame;
8306
8307 /* Non-zero while redisplay_internal is in progress. */
8308
8309 int redisplaying_p;
8310
8311
8312 /* Value is non-zero if all changes in window W, which displays
8313 current_buffer, are in the text between START and END. START is a
8314 buffer position, END is given as a distance from Z. Used in
8315 redisplay_internal for display optimization. */
8316
8317 static INLINE int
8318 text_outside_line_unchanged_p (w, start, end)
8319 struct window *w;
8320 int start, end;
8321 {
8322 int unchanged_p = 1;
8323
8324 /* If text or overlays have changed, see where. */
8325 if (XFASTINT (w->last_modified) < MODIFF
8326 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8327 {
8328 /* Gap in the line? */
8329 if (GPT < start || Z - GPT < end)
8330 unchanged_p = 0;
8331
8332 /* Changes start in front of the line, or end after it? */
8333 if (unchanged_p
8334 && (BEG_UNCHANGED < start - 1
8335 || END_UNCHANGED < end))
8336 unchanged_p = 0;
8337
8338 /* If selective display, can't optimize if changes start at the
8339 beginning of the line. */
8340 if (unchanged_p
8341 && INTEGERP (current_buffer->selective_display)
8342 && XINT (current_buffer->selective_display) > 0
8343 && (BEG_UNCHANGED < start || GPT <= start))
8344 unchanged_p = 0;
8345
8346 /* If there are overlays at the start or end of the line, these
8347 may have overlay strings with newlines in them. A change at
8348 START, for instance, may actually concern the display of such
8349 overlay strings as well, and they are displayed on different
8350 lines. So, quickly rule out this case. (For the future, it
8351 might be desirable to implement something more telling than
8352 just BEG/END_UNCHANGED.) */
8353 if (unchanged_p)
8354 {
8355 if (BEG + BEG_UNCHANGED == start
8356 && overlay_touches_p (start))
8357 unchanged_p = 0;
8358 if (END_UNCHANGED == end
8359 && overlay_touches_p (Z - end))
8360 unchanged_p = 0;
8361 }
8362 }
8363
8364 return unchanged_p;
8365 }
8366
8367
8368 /* Do a frame update, taking possible shortcuts into account. This is
8369 the main external entry point for redisplay.
8370
8371 If the last redisplay displayed an echo area message and that message
8372 is no longer requested, we clear the echo area or bring back the
8373 mini-buffer if that is in use. */
8374
8375 void
8376 redisplay ()
8377 {
8378 redisplay_internal (0);
8379 }
8380
8381
8382 /* Return 1 if point moved out of or into a composition. Otherwise
8383 return 0. PREV_BUF and PREV_PT are the last point buffer and
8384 position. BUF and PT are the current point buffer and position. */
8385
8386 int
8387 check_point_in_composition (prev_buf, prev_pt, buf, pt)
8388 struct buffer *prev_buf, *buf;
8389 int prev_pt, pt;
8390 {
8391 int start, end;
8392 Lisp_Object prop;
8393 Lisp_Object buffer;
8394
8395 XSETBUFFER (buffer, buf);
8396 /* Check a composition at the last point if point moved within the
8397 same buffer. */
8398 if (prev_buf == buf)
8399 {
8400 if (prev_pt == pt)
8401 /* Point didn't move. */
8402 return 0;
8403
8404 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
8405 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
8406 && COMPOSITION_VALID_P (start, end, prop)
8407 && start < prev_pt && end > prev_pt)
8408 /* The last point was within the composition. Return 1 iff
8409 point moved out of the composition. */
8410 return (pt <= start || pt >= end);
8411 }
8412
8413 /* Check a composition at the current point. */
8414 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
8415 && find_composition (pt, -1, &start, &end, &prop, buffer)
8416 && COMPOSITION_VALID_P (start, end, prop)
8417 && start < pt && end > pt);
8418 }
8419
8420
8421 /* Reconsider the setting of B->clip_changed which is displayed
8422 in window W. */
8423
8424 static INLINE void
8425 reconsider_clip_changes (w, b)
8426 struct window *w;
8427 struct buffer *b;
8428 {
8429 if (b->prevent_redisplay_optimizations_p)
8430 b->clip_changed = 1;
8431 else if (b->clip_changed
8432 && !NILP (w->window_end_valid)
8433 && w->current_matrix->buffer == b
8434 && w->current_matrix->zv == BUF_ZV (b)
8435 && w->current_matrix->begv == BUF_BEGV (b))
8436 b->clip_changed = 0;
8437
8438 /* If display wasn't paused, and W is not a tool bar window, see if
8439 point has been moved into or out of a composition. In that case,
8440 we set b->clip_changed to 1 to force updating the screen. If
8441 b->clip_changed has already been set to 1, we can skip this
8442 check. */
8443 if (!b->clip_changed
8444 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
8445 {
8446 int pt;
8447
8448 if (w == XWINDOW (selected_window))
8449 pt = BUF_PT (current_buffer);
8450 else
8451 pt = marker_position (w->pointm);
8452
8453 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
8454 || pt != XINT (w->last_point))
8455 && check_point_in_composition (w->current_matrix->buffer,
8456 XINT (w->last_point),
8457 XBUFFER (w->buffer), pt))
8458 b->clip_changed = 1;
8459 }
8460 }
8461
8462
8463 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
8464 response to any user action; therefore, we should preserve the echo
8465 area. (Actually, our caller does that job.) Perhaps in the future
8466 avoid recentering windows if it is not necessary; currently that
8467 causes some problems. */
8468
8469 static void
8470 redisplay_internal (preserve_echo_area)
8471 int preserve_echo_area;
8472 {
8473 struct window *w = XWINDOW (selected_window);
8474 struct frame *f = XFRAME (w->frame);
8475 int pause;
8476 int must_finish = 0;
8477 struct text_pos tlbufpos, tlendpos;
8478 int number_of_visible_frames;
8479 int count;
8480 struct frame *sf = SELECTED_FRAME ();
8481
8482 /* Non-zero means redisplay has to consider all windows on all
8483 frames. Zero means, only selected_window is considered. */
8484 int consider_all_windows_p;
8485
8486 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
8487
8488 /* No redisplay if running in batch mode or frame is not yet fully
8489 initialized, or redisplay is explicitly turned off by setting
8490 Vinhibit_redisplay. */
8491 if (noninteractive
8492 || !NILP (Vinhibit_redisplay)
8493 || !f->glyphs_initialized_p)
8494 return;
8495
8496 /* The flag redisplay_performed_directly_p is set by
8497 direct_output_for_insert when it already did the whole screen
8498 update necessary. */
8499 if (redisplay_performed_directly_p)
8500 {
8501 redisplay_performed_directly_p = 0;
8502 if (!hscroll_windows (selected_window))
8503 return;
8504 }
8505
8506 #ifdef USE_X_TOOLKIT
8507 if (popup_activated ())
8508 return;
8509 #endif
8510
8511 /* I don't think this happens but let's be paranoid. */
8512 if (redisplaying_p)
8513 return;
8514
8515 /* Record a function that resets redisplaying_p to its old value
8516 when we leave this function. */
8517 count = BINDING_STACK_SIZE ();
8518 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
8519 ++redisplaying_p;
8520
8521 retry:
8522 pause = 0;
8523 reconsider_clip_changes (w, current_buffer);
8524
8525 /* If new fonts have been loaded that make a glyph matrix adjustment
8526 necessary, do it. */
8527 if (fonts_changed_p)
8528 {
8529 adjust_glyphs (NULL);
8530 ++windows_or_buffers_changed;
8531 fonts_changed_p = 0;
8532 }
8533
8534 /* If face_change_count is non-zero, init_iterator will free all
8535 realized faces, which includes the faces referenced from current
8536 matrices. So, we can't reuse current matrices in this case. */
8537 if (face_change_count)
8538 ++windows_or_buffers_changed;
8539
8540 if (! FRAME_WINDOW_P (sf)
8541 && previous_terminal_frame != sf)
8542 {
8543 /* Since frames on an ASCII terminal share the same display
8544 area, displaying a different frame means redisplay the whole
8545 thing. */
8546 windows_or_buffers_changed++;
8547 SET_FRAME_GARBAGED (sf);
8548 XSETFRAME (Vterminal_frame, sf);
8549 }
8550 previous_terminal_frame = sf;
8551
8552 /* Set the visible flags for all frames. Do this before checking
8553 for resized or garbaged frames; they want to know if their frames
8554 are visible. See the comment in frame.h for
8555 FRAME_SAMPLE_VISIBILITY. */
8556 {
8557 Lisp_Object tail, frame;
8558
8559 number_of_visible_frames = 0;
8560
8561 FOR_EACH_FRAME (tail, frame)
8562 {
8563 struct frame *f = XFRAME (frame);
8564
8565 FRAME_SAMPLE_VISIBILITY (f);
8566 if (FRAME_VISIBLE_P (f))
8567 ++number_of_visible_frames;
8568 clear_desired_matrices (f);
8569 }
8570 }
8571
8572 /* Notice any pending interrupt request to change frame size. */
8573 do_pending_window_change (1);
8574
8575 /* Clear frames marked as garbaged. */
8576 if (frame_garbaged)
8577 clear_garbaged_frames ();
8578
8579 /* Build menubar and tool-bar items. */
8580 prepare_menu_bars ();
8581
8582 if (windows_or_buffers_changed)
8583 update_mode_lines++;
8584
8585 /* Detect case that we need to write or remove a star in the mode line. */
8586 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
8587 {
8588 w->update_mode_line = Qt;
8589 if (buffer_shared > 1)
8590 update_mode_lines++;
8591 }
8592
8593 /* If %c is in the mode line, update it if needed. */
8594 if (!NILP (w->column_number_displayed)
8595 /* This alternative quickly identifies a common case
8596 where no change is needed. */
8597 && !(PT == XFASTINT (w->last_point)
8598 && XFASTINT (w->last_modified) >= MODIFF
8599 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
8600 && XFASTINT (w->column_number_displayed) != current_column ())
8601 w->update_mode_line = Qt;
8602
8603 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
8604
8605 /* The variable buffer_shared is set in redisplay_window and
8606 indicates that we redisplay a buffer in different windows. See
8607 there. */
8608 consider_all_windows_p = update_mode_lines || buffer_shared > 1;
8609
8610 /* If specs for an arrow have changed, do thorough redisplay
8611 to ensure we remove any arrow that should no longer exist. */
8612 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
8613 || ! EQ (Voverlay_arrow_string, last_arrow_string))
8614 consider_all_windows_p = windows_or_buffers_changed = 1;
8615
8616 /* Normally the message* functions will have already displayed and
8617 updated the echo area, but the frame may have been trashed, or
8618 the update may have been preempted, so display the echo area
8619 again here. Checking message_cleared_p captures the case that
8620 the echo area should be cleared. */
8621 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
8622 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
8623 || (message_cleared_p
8624 && minibuf_level == 0
8625 /* If the mini-window is currently selected, this means the
8626 echo-area doesn't show through. */
8627 && !MINI_WINDOW_P (XWINDOW (selected_window))))
8628 {
8629 int window_height_changed_p = echo_area_display (0);
8630 must_finish = 1;
8631
8632 /* If we don't display the current message, don't clear the
8633 message_cleared_p flag, because, if we did, we wouldn't clear
8634 the echo area in the next redisplay which doesn't preserve
8635 the echo area. */
8636 if (!display_last_displayed_message_p)
8637 message_cleared_p = 0;
8638
8639 if (fonts_changed_p)
8640 goto retry;
8641 else if (window_height_changed_p)
8642 {
8643 consider_all_windows_p = 1;
8644 ++update_mode_lines;
8645 ++windows_or_buffers_changed;
8646
8647 /* If window configuration was changed, frames may have been
8648 marked garbaged. Clear them or we will experience
8649 surprises wrt scrolling. */
8650 if (frame_garbaged)
8651 clear_garbaged_frames ();
8652 }
8653 }
8654 else if (EQ (selected_window, minibuf_window)
8655 && (current_buffer->clip_changed
8656 || XFASTINT (w->last_modified) < MODIFF
8657 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8658 && resize_mini_window (w, 0))
8659 {
8660 /* Resized active mini-window to fit the size of what it is
8661 showing if its contents might have changed. */
8662 must_finish = 1;
8663 consider_all_windows_p = 1;
8664 ++windows_or_buffers_changed;
8665 ++update_mode_lines;
8666
8667 /* If window configuration was changed, frames may have been
8668 marked garbaged. Clear them or we will experience
8669 surprises wrt scrolling. */
8670 if (frame_garbaged)
8671 clear_garbaged_frames ();
8672 }
8673
8674
8675 /* If showing the region, and mark has changed, we must redisplay
8676 the whole window. The assignment to this_line_start_pos prevents
8677 the optimization directly below this if-statement. */
8678 if (((!NILP (Vtransient_mark_mode)
8679 && !NILP (XBUFFER (w->buffer)->mark_active))
8680 != !NILP (w->region_showing))
8681 || (!NILP (w->region_showing)
8682 && !EQ (w->region_showing,
8683 Fmarker_position (XBUFFER (w->buffer)->mark))))
8684 CHARPOS (this_line_start_pos) = 0;
8685
8686 /* Optimize the case that only the line containing the cursor in the
8687 selected window has changed. Variables starting with this_ are
8688 set in display_line and record information about the line
8689 containing the cursor. */
8690 tlbufpos = this_line_start_pos;
8691 tlendpos = this_line_end_pos;
8692 if (!consider_all_windows_p
8693 && CHARPOS (tlbufpos) > 0
8694 && NILP (w->update_mode_line)
8695 && !current_buffer->clip_changed
8696 && FRAME_VISIBLE_P (XFRAME (w->frame))
8697 && !FRAME_OBSCURED_P (XFRAME (w->frame))
8698 /* Make sure recorded data applies to current buffer, etc. */
8699 && this_line_buffer == current_buffer
8700 && current_buffer == XBUFFER (w->buffer)
8701 && NILP (w->force_start)
8702 /* Point must be on the line that we have info recorded about. */
8703 && PT >= CHARPOS (tlbufpos)
8704 && PT <= Z - CHARPOS (tlendpos)
8705 /* All text outside that line, including its final newline,
8706 must be unchanged */
8707 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
8708 CHARPOS (tlendpos)))
8709 {
8710 if (CHARPOS (tlbufpos) > BEGV
8711 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
8712 && (CHARPOS (tlbufpos) == ZV
8713 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
8714 /* Former continuation line has disappeared by becoming empty */
8715 goto cancel;
8716 else if (XFASTINT (w->last_modified) < MODIFF
8717 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
8718 || MINI_WINDOW_P (w))
8719 {
8720 /* We have to handle the case of continuation around a
8721 wide-column character (See the comment in indent.c around
8722 line 885).
8723
8724 For instance, in the following case:
8725
8726 -------- Insert --------
8727 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
8728 J_I_ ==> J_I_ `^^' are cursors.
8729 ^^ ^^
8730 -------- --------
8731
8732 As we have to redraw the line above, we should goto cancel. */
8733
8734 struct it it;
8735 int line_height_before = this_line_pixel_height;
8736
8737 /* Note that start_display will handle the case that the
8738 line starting at tlbufpos is a continuation lines. */
8739 start_display (&it, w, tlbufpos);
8740
8741 /* Implementation note: It this still necessary? */
8742 if (it.current_x != this_line_start_x)
8743 goto cancel;
8744
8745 TRACE ((stderr, "trying display optimization 1\n"));
8746 w->cursor.vpos = -1;
8747 overlay_arrow_seen = 0;
8748 it.vpos = this_line_vpos;
8749 it.current_y = this_line_y;
8750 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
8751 display_line (&it);
8752
8753 /* If line contains point, is not continued,
8754 and ends at same distance from eob as before, we win */
8755 if (w->cursor.vpos >= 0
8756 /* Line is not continued, otherwise this_line_start_pos
8757 would have been set to 0 in display_line. */
8758 && CHARPOS (this_line_start_pos)
8759 /* Line ends as before. */
8760 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
8761 /* Line has same height as before. Otherwise other lines
8762 would have to be shifted up or down. */
8763 && this_line_pixel_height == line_height_before)
8764 {
8765 /* If this is not the window's last line, we must adjust
8766 the charstarts of the lines below. */
8767 if (it.current_y < it.last_visible_y)
8768 {
8769 struct glyph_row *row
8770 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
8771 int delta, delta_bytes;
8772
8773 if (Z - CHARPOS (tlendpos) == ZV)
8774 {
8775 /* This line ends at end of (accessible part of)
8776 buffer. There is no newline to count. */
8777 delta = (Z
8778 - CHARPOS (tlendpos)
8779 - MATRIX_ROW_START_CHARPOS (row));
8780 delta_bytes = (Z_BYTE
8781 - BYTEPOS (tlendpos)
8782 - MATRIX_ROW_START_BYTEPOS (row));
8783 }
8784 else
8785 {
8786 /* This line ends in a newline. Must take
8787 account of the newline and the rest of the
8788 text that follows. */
8789 delta = (Z
8790 - CHARPOS (tlendpos)
8791 - MATRIX_ROW_START_CHARPOS (row));
8792 delta_bytes = (Z_BYTE
8793 - BYTEPOS (tlendpos)
8794 - MATRIX_ROW_START_BYTEPOS (row));
8795 }
8796
8797 increment_matrix_positions (w->current_matrix,
8798 this_line_vpos + 1,
8799 w->current_matrix->nrows,
8800 delta, delta_bytes);
8801 }
8802
8803 /* If this row displays text now but previously didn't,
8804 or vice versa, w->window_end_vpos may have to be
8805 adjusted. */
8806 if ((it.glyph_row - 1)->displays_text_p)
8807 {
8808 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
8809 XSETINT (w->window_end_vpos, this_line_vpos);
8810 }
8811 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
8812 && this_line_vpos > 0)
8813 XSETINT (w->window_end_vpos, this_line_vpos - 1);
8814 w->window_end_valid = Qnil;
8815
8816 /* Update hint: No need to try to scroll in update_window. */
8817 w->desired_matrix->no_scrolling_p = 1;
8818
8819 #if GLYPH_DEBUG
8820 *w->desired_matrix->method = 0;
8821 debug_method_add (w, "optimization 1");
8822 #endif
8823 goto update;
8824 }
8825 else
8826 goto cancel;
8827 }
8828 else if (/* Cursor position hasn't changed. */
8829 PT == XFASTINT (w->last_point)
8830 /* Make sure the cursor was last displayed
8831 in this window. Otherwise we have to reposition it. */
8832 && 0 <= w->cursor.vpos
8833 && XINT (w->height) > w->cursor.vpos)
8834 {
8835 if (!must_finish)
8836 {
8837 do_pending_window_change (1);
8838
8839 /* We used to always goto end_of_redisplay here, but this
8840 isn't enough if we have a blinking cursor. */
8841 if (w->cursor_off_p == w->last_cursor_off_p)
8842 goto end_of_redisplay;
8843 }
8844 goto update;
8845 }
8846 /* If highlighting the region, or if the cursor is in the echo area,
8847 then we can't just move the cursor. */
8848 else if (! (!NILP (Vtransient_mark_mode)
8849 && !NILP (current_buffer->mark_active))
8850 && (EQ (selected_window, current_buffer->last_selected_window)
8851 || highlight_nonselected_windows)
8852 && NILP (w->region_showing)
8853 && NILP (Vshow_trailing_whitespace)
8854 && !cursor_in_echo_area)
8855 {
8856 struct it it;
8857 struct glyph_row *row;
8858
8859 /* Skip from tlbufpos to PT and see where it is. Note that
8860 PT may be in invisible text. If so, we will end at the
8861 next visible position. */
8862 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
8863 NULL, DEFAULT_FACE_ID);
8864 it.current_x = this_line_start_x;
8865 it.current_y = this_line_y;
8866 it.vpos = this_line_vpos;
8867
8868 /* The call to move_it_to stops in front of PT, but
8869 moves over before-strings. */
8870 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
8871
8872 if (it.vpos == this_line_vpos
8873 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
8874 row->enabled_p))
8875 {
8876 xassert (this_line_vpos == it.vpos);
8877 xassert (this_line_y == it.current_y);
8878 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
8879 #if GLYPH_DEBUG
8880 *w->desired_matrix->method = 0;
8881 debug_method_add (w, "optimization 3");
8882 #endif
8883 goto update;
8884 }
8885 else
8886 goto cancel;
8887 }
8888
8889 cancel:
8890 /* Text changed drastically or point moved off of line. */
8891 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
8892 }
8893
8894 CHARPOS (this_line_start_pos) = 0;
8895 consider_all_windows_p |= buffer_shared > 1;
8896 ++clear_face_cache_count;
8897
8898
8899 /* Build desired matrices, and update the display. If
8900 consider_all_windows_p is non-zero, do it for all windows on all
8901 frames. Otherwise do it for selected_window, only. */
8902
8903 if (consider_all_windows_p)
8904 {
8905 Lisp_Object tail, frame;
8906 int i, n = 0, size = 50;
8907 struct frame **updated
8908 = (struct frame **) alloca (size * sizeof *updated);
8909
8910 /* Clear the face cache eventually. */
8911 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
8912 {
8913 clear_face_cache (0);
8914 clear_face_cache_count = 0;
8915 }
8916
8917 /* Recompute # windows showing selected buffer. This will be
8918 incremented each time such a window is displayed. */
8919 buffer_shared = 0;
8920
8921 FOR_EACH_FRAME (tail, frame)
8922 {
8923 struct frame *f = XFRAME (frame);
8924
8925 if (FRAME_WINDOW_P (f) || f == sf)
8926 {
8927 if (clear_face_cache_count % 50 == 0
8928 && FRAME_WINDOW_P (f))
8929 clear_image_cache (f, 0);
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 internal_condition_case_1 (redisplay_window_1, selected_window, Qerror,
8999 redisplay_window_error);
9000
9001 /* Compare desired and current matrices, perform output. */
9002 update:
9003
9004 /* If fonts changed, display again. */
9005 if (fonts_changed_p)
9006 goto retry;
9007
9008 /* Prevent various kinds of signals during display update.
9009 stdio is not robust about handling signals,
9010 which can cause an apparent I/O error. */
9011 if (interrupt_input)
9012 unrequest_sigio ();
9013 stop_polling ();
9014
9015 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
9016 {
9017 if (hscroll_windows (selected_window))
9018 goto retry;
9019
9020 XWINDOW (selected_window)->must_be_updated_p = 1;
9021 pause = update_frame (sf, 0, 0);
9022 }
9023
9024 /* We may have called echo_area_display at the top of this
9025 function. If the echo area is on another frame, that may
9026 have put text on a frame other than the selected one, so the
9027 above call to update_frame would not have caught it. Catch
9028 it here. */
9029 mini_window = FRAME_MINIBUF_WINDOW (sf);
9030 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9031
9032 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
9033 {
9034 XWINDOW (mini_window)->must_be_updated_p = 1;
9035 pause |= update_frame (mini_frame, 0, 0);
9036 if (!pause && hscroll_windows (mini_window))
9037 goto retry;
9038 }
9039 }
9040
9041 /* If display was paused because of pending input, make sure we do a
9042 thorough update the next time. */
9043 if (pause)
9044 {
9045 /* Prevent the optimization at the beginning of
9046 redisplay_internal that tries a single-line update of the
9047 line containing the cursor in the selected window. */
9048 CHARPOS (this_line_start_pos) = 0;
9049
9050 /* Let the overlay arrow be updated the next time. */
9051 if (!NILP (last_arrow_position))
9052 {
9053 last_arrow_position = Qt;
9054 last_arrow_string = Qt;
9055 }
9056
9057 /* If we pause after scrolling, some rows in the current
9058 matrices of some windows are not valid. */
9059 if (!WINDOW_FULL_WIDTH_P (w)
9060 && !FRAME_WINDOW_P (XFRAME (w->frame)))
9061 update_mode_lines = 1;
9062 }
9063 else
9064 {
9065 if (!consider_all_windows_p)
9066 {
9067 /* This has already been done above if
9068 consider_all_windows_p is set. */
9069 mark_window_display_accurate_1 (w, 1);
9070
9071 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
9072 last_arrow_string = Voverlay_arrow_string;
9073
9074 if (frame_up_to_date_hook != 0)
9075 frame_up_to_date_hook (sf);
9076 }
9077
9078 update_mode_lines = 0;
9079 windows_or_buffers_changed = 0;
9080 }
9081
9082 /* Start SIGIO interrupts coming again. Having them off during the
9083 code above makes it less likely one will discard output, but not
9084 impossible, since there might be stuff in the system buffer here.
9085 But it is much hairier to try to do anything about that. */
9086 if (interrupt_input)
9087 request_sigio ();
9088 start_polling ();
9089
9090 /* If a frame has become visible which was not before, redisplay
9091 again, so that we display it. Expose events for such a frame
9092 (which it gets when becoming visible) don't call the parts of
9093 redisplay constructing glyphs, so simply exposing a frame won't
9094 display anything in this case. So, we have to display these
9095 frames here explicitly. */
9096 if (!pause)
9097 {
9098 Lisp_Object tail, frame;
9099 int new_count = 0;
9100
9101 FOR_EACH_FRAME (tail, frame)
9102 {
9103 int this_is_visible = 0;
9104
9105 if (XFRAME (frame)->visible)
9106 this_is_visible = 1;
9107 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
9108 if (XFRAME (frame)->visible)
9109 this_is_visible = 1;
9110
9111 if (this_is_visible)
9112 new_count++;
9113 }
9114
9115 if (new_count != number_of_visible_frames)
9116 windows_or_buffers_changed++;
9117 }
9118
9119 /* Change frame size now if a change is pending. */
9120 do_pending_window_change (1);
9121
9122 /* If we just did a pending size change, or have additional
9123 visible frames, redisplay again. */
9124 if (windows_or_buffers_changed && !pause)
9125 goto retry;
9126
9127 end_of_redisplay:;
9128
9129 unbind_to (count, Qnil);
9130 }
9131
9132
9133 /* Redisplay, but leave alone any recent echo area message unless
9134 another message has been requested in its place.
9135
9136 This is useful in situations where you need to redisplay but no
9137 user action has occurred, making it inappropriate for the message
9138 area to be cleared. See tracking_off and
9139 wait_reading_process_input for examples of these situations.
9140
9141 FROM_WHERE is an integer saying from where this function was
9142 called. This is useful for debugging. */
9143
9144 void
9145 redisplay_preserve_echo_area (from_where)
9146 int from_where;
9147 {
9148 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
9149
9150 if (!NILP (echo_area_buffer[1]))
9151 {
9152 /* We have a previously displayed message, but no current
9153 message. Redisplay the previous message. */
9154 display_last_displayed_message_p = 1;
9155 redisplay_internal (1);
9156 display_last_displayed_message_p = 0;
9157 }
9158 else
9159 redisplay_internal (1);
9160 }
9161
9162
9163 /* Function registered with record_unwind_protect in
9164 redisplay_internal. Clears the flag indicating that a redisplay is
9165 in progress. */
9166
9167 static Lisp_Object
9168 unwind_redisplay (old_redisplaying_p)
9169 Lisp_Object old_redisplaying_p;
9170 {
9171 redisplaying_p = XFASTINT (old_redisplaying_p);
9172 return Qnil;
9173 }
9174
9175
9176 /* Mark the display of window W as accurate or inaccurate. If
9177 ACCURATE_P is non-zero mark display of W as accurate. If
9178 ACCURATE_P is zero, arrange for W to be redisplayed the next time
9179 redisplay_internal is called. */
9180
9181 static void
9182 mark_window_display_accurate_1 (w, accurate_p)
9183 struct window *w;
9184 int accurate_p;
9185 {
9186 if (BUFFERP (w->buffer))
9187 {
9188 struct buffer *b = XBUFFER (w->buffer);
9189
9190 w->last_modified
9191 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
9192 w->last_overlay_modified
9193 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
9194 w->last_had_star
9195 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
9196
9197 if (accurate_p)
9198 {
9199 b->clip_changed = 0;
9200 b->prevent_redisplay_optimizations_p = 0;
9201
9202 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
9203 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
9204 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
9205 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
9206
9207 w->current_matrix->buffer = b;
9208 w->current_matrix->begv = BUF_BEGV (b);
9209 w->current_matrix->zv = BUF_ZV (b);
9210
9211 w->last_cursor = w->cursor;
9212 w->last_cursor_off_p = w->cursor_off_p;
9213
9214 if (w == XWINDOW (selected_window))
9215 w->last_point = make_number (BUF_PT (b));
9216 else
9217 w->last_point = make_number (XMARKER (w->pointm)->charpos);
9218 }
9219 }
9220
9221 if (accurate_p)
9222 {
9223 w->window_end_valid = w->buffer;
9224 #if 0 /* This is incorrect with variable-height lines. */
9225 xassert (XINT (w->window_end_vpos)
9226 < (XINT (w->height)
9227 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
9228 #endif
9229 w->update_mode_line = Qnil;
9230 }
9231 }
9232
9233
9234 /* Mark the display of windows in the window tree rooted at WINDOW as
9235 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
9236 windows as accurate. If ACCURATE_P is zero, arrange for windows to
9237 be redisplayed the next time redisplay_internal is called. */
9238
9239 void
9240 mark_window_display_accurate (window, accurate_p)
9241 Lisp_Object window;
9242 int accurate_p;
9243 {
9244 struct window *w;
9245
9246 for (; !NILP (window); window = w->next)
9247 {
9248 w = XWINDOW (window);
9249 mark_window_display_accurate_1 (w, accurate_p);
9250
9251 if (!NILP (w->vchild))
9252 mark_window_display_accurate (w->vchild, accurate_p);
9253 if (!NILP (w->hchild))
9254 mark_window_display_accurate (w->hchild, accurate_p);
9255 }
9256
9257 if (accurate_p)
9258 {
9259 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
9260 last_arrow_string = Voverlay_arrow_string;
9261 }
9262 else
9263 {
9264 /* Force a thorough redisplay the next time by setting
9265 last_arrow_position and last_arrow_string to t, which is
9266 unequal to any useful value of Voverlay_arrow_... */
9267 last_arrow_position = Qt;
9268 last_arrow_string = Qt;
9269 }
9270 }
9271
9272
9273 /* Return value in display table DP (Lisp_Char_Table *) for character
9274 C. Since a display table doesn't have any parent, we don't have to
9275 follow parent. Do not call this function directly but use the
9276 macro DISP_CHAR_VECTOR. */
9277
9278 Lisp_Object
9279 disp_char_vector (dp, c)
9280 struct Lisp_Char_Table *dp;
9281 int c;
9282 {
9283 Lisp_Object val;
9284
9285 if (ASCII_CHAR_P (c))
9286 {
9287 val = dp->ascii;
9288 if (SUB_CHAR_TABLE_P (val))
9289 val = XSUB_CHAR_TABLE (val)->contents[c];
9290 }
9291 else
9292 {
9293 Lisp_Object table;
9294
9295 XSETCHAR_TABLE (table, dp);
9296 val = char_table_ref (table, c);
9297 }
9298 if (NILP (val))
9299 val = dp->defalt;
9300 return val;
9301 }
9302
9303
9304 \f
9305 /***********************************************************************
9306 Window Redisplay
9307 ***********************************************************************/
9308
9309 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
9310
9311 static void
9312 redisplay_windows (window)
9313 Lisp_Object window;
9314 {
9315 while (!NILP (window))
9316 {
9317 struct window *w = XWINDOW (window);
9318
9319 if (!NILP (w->hchild))
9320 redisplay_windows (w->hchild);
9321 else if (!NILP (w->vchild))
9322 redisplay_windows (w->vchild);
9323 else
9324 {
9325 displayed_buffer = XBUFFER (w->buffer);
9326 internal_condition_case_1 (redisplay_window_0, window, Qerror,
9327 redisplay_window_error);
9328 }
9329
9330 window = w->next;
9331 }
9332 }
9333
9334 static Lisp_Object
9335 redisplay_window_error ()
9336 {
9337 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
9338 return Qnil;
9339 }
9340
9341 static Lisp_Object
9342 redisplay_window_0 (window)
9343 Lisp_Object window;
9344 {
9345 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
9346 redisplay_window (window, 0);
9347 return Qnil;
9348 }
9349
9350 static Lisp_Object
9351 redisplay_window_1 (window)
9352 Lisp_Object window;
9353 {
9354 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
9355 redisplay_window (window, 1);
9356 return Qnil;
9357 }
9358 \f
9359 /* Set cursor position of W. PT is assumed to be displayed in ROW.
9360 DELTA is the number of bytes by which positions recorded in ROW
9361 differ from current buffer positions. */
9362
9363 void
9364 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
9365 struct window *w;
9366 struct glyph_row *row;
9367 struct glyph_matrix *matrix;
9368 int delta, delta_bytes, dy, dvpos;
9369 {
9370 struct glyph *glyph = row->glyphs[TEXT_AREA];
9371 struct glyph *end = glyph + row->used[TEXT_AREA];
9372 int x = row->x;
9373 int pt_old = PT - delta;
9374
9375 /* Skip over glyphs not having an object at the start of the row.
9376 These are special glyphs like truncation marks on terminal
9377 frames. */
9378 if (row->displays_text_p)
9379 while (glyph < end
9380 && INTEGERP (glyph->object)
9381 && glyph->charpos < 0)
9382 {
9383 x += glyph->pixel_width;
9384 ++glyph;
9385 }
9386
9387 while (glyph < end
9388 && !INTEGERP (glyph->object)
9389 && (!BUFFERP (glyph->object)
9390 || glyph->charpos < pt_old))
9391 {
9392 x += glyph->pixel_width;
9393 ++glyph;
9394 }
9395
9396 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
9397 w->cursor.x = x;
9398 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
9399 w->cursor.y = row->y + dy;
9400
9401 if (w == XWINDOW (selected_window))
9402 {
9403 if (!row->continued_p
9404 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
9405 && row->x == 0)
9406 {
9407 this_line_buffer = XBUFFER (w->buffer);
9408
9409 CHARPOS (this_line_start_pos)
9410 = MATRIX_ROW_START_CHARPOS (row) + delta;
9411 BYTEPOS (this_line_start_pos)
9412 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
9413
9414 CHARPOS (this_line_end_pos)
9415 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
9416 BYTEPOS (this_line_end_pos)
9417 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
9418
9419 this_line_y = w->cursor.y;
9420 this_line_pixel_height = row->height;
9421 this_line_vpos = w->cursor.vpos;
9422 this_line_start_x = row->x;
9423 }
9424 else
9425 CHARPOS (this_line_start_pos) = 0;
9426 }
9427 }
9428
9429
9430 /* Run window scroll functions, if any, for WINDOW with new window
9431 start STARTP. Sets the window start of WINDOW to that position.
9432
9433 We assume that the window's buffer is really current. */
9434
9435 static INLINE struct text_pos
9436 run_window_scroll_functions (window, startp)
9437 Lisp_Object window;
9438 struct text_pos startp;
9439 {
9440 struct window *w = XWINDOW (window);
9441 SET_MARKER_FROM_TEXT_POS (w->start, startp);
9442
9443 if (current_buffer != XBUFFER (w->buffer))
9444 abort ();
9445
9446 if (!NILP (Vwindow_scroll_functions))
9447 {
9448 run_hook_with_args_2 (Qwindow_scroll_functions, window,
9449 make_number (CHARPOS (startp)));
9450 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9451 /* In case the hook functions switch buffers. */
9452 if (current_buffer != XBUFFER (w->buffer))
9453 set_buffer_internal_1 (XBUFFER (w->buffer));
9454 }
9455
9456 return startp;
9457 }
9458
9459
9460 /* Modify the desired matrix of window W and W->vscroll so that the
9461 line containing the cursor is fully visible. If this requires
9462 larger matrices than are allocated, set fonts_changed_p and return
9463 0. */
9464
9465 static int
9466 make_cursor_line_fully_visible (w)
9467 struct window *w;
9468 {
9469 struct glyph_matrix *matrix;
9470 struct glyph_row *row;
9471 int window_height;
9472
9473 /* It's not always possible to find the cursor, e.g, when a window
9474 is full of overlay strings. Don't do anything in that case. */
9475 if (w->cursor.vpos < 0)
9476 return 1;
9477
9478 matrix = w->desired_matrix;
9479 row = MATRIX_ROW (matrix, w->cursor.vpos);
9480
9481 /* If the cursor row is not partially visible, there's nothing
9482 to do. */
9483 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
9484 return 1;
9485
9486 /* If the row the cursor is in is taller than the window's height,
9487 it's not clear what to do, so do nothing. */
9488 window_height = window_box_height (w);
9489 if (row->height >= window_height)
9490 return 1;
9491
9492 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
9493 {
9494 int dy = row->height - row->visible_height;
9495 w->vscroll = 0;
9496 w->cursor.y += dy;
9497 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9498 }
9499 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
9500 {
9501 int dy = - (row->height - row->visible_height);
9502 w->vscroll = dy;
9503 w->cursor.y += dy;
9504 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9505 }
9506
9507 /* When we change the cursor y-position of the selected window,
9508 change this_line_y as well so that the display optimization for
9509 the cursor line of the selected window in redisplay_internal uses
9510 the correct y-position. */
9511 if (w == XWINDOW (selected_window))
9512 this_line_y = w->cursor.y;
9513
9514 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
9515 redisplay with larger matrices. */
9516 if (matrix->nrows < required_matrix_height (w))
9517 {
9518 fonts_changed_p = 1;
9519 return 0;
9520 }
9521
9522 return 1;
9523 }
9524
9525
9526 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
9527 non-zero means only WINDOW is redisplayed in redisplay_internal.
9528 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
9529 in redisplay_window to bring a partially visible line into view in
9530 the case that only the cursor has moved.
9531
9532 Value is
9533
9534 1 if scrolling succeeded
9535
9536 0 if scrolling didn't find point.
9537
9538 -1 if new fonts have been loaded so that we must interrupt
9539 redisplay, adjust glyph matrices, and try again. */
9540
9541 enum
9542 {
9543 SCROLLING_SUCCESS,
9544 SCROLLING_FAILED,
9545 SCROLLING_NEED_LARGER_MATRICES
9546 };
9547
9548 static int
9549 try_scrolling (window, just_this_one_p, scroll_conservatively,
9550 scroll_step, temp_scroll_step)
9551 Lisp_Object window;
9552 int just_this_one_p;
9553 int scroll_conservatively, scroll_step;
9554 int temp_scroll_step;
9555 {
9556 struct window *w = XWINDOW (window);
9557 struct frame *f = XFRAME (w->frame);
9558 struct text_pos scroll_margin_pos;
9559 struct text_pos pos;
9560 struct text_pos startp;
9561 struct it it;
9562 Lisp_Object window_end;
9563 int this_scroll_margin;
9564 int dy = 0;
9565 int scroll_max;
9566 int rc;
9567 int amount_to_scroll = 0;
9568 Lisp_Object aggressive;
9569 int height;
9570
9571 #if GLYPH_DEBUG
9572 debug_method_add (w, "try_scrolling");
9573 #endif
9574
9575 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9576
9577 /* Compute scroll margin height in pixels. We scroll when point is
9578 within this distance from the top or bottom of the window. */
9579 if (scroll_margin > 0)
9580 {
9581 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
9582 this_scroll_margin *= CANON_Y_UNIT (f);
9583 }
9584 else
9585 this_scroll_margin = 0;
9586
9587 /* Compute how much we should try to scroll maximally to bring point
9588 into view. */
9589 if (scroll_step || scroll_conservatively || temp_scroll_step)
9590 scroll_max = max (scroll_step,
9591 max (scroll_conservatively, temp_scroll_step));
9592 else if (NUMBERP (current_buffer->scroll_down_aggressively)
9593 || NUMBERP (current_buffer->scroll_up_aggressively))
9594 /* We're trying to scroll because of aggressive scrolling
9595 but no scroll_step is set. Choose an arbitrary one. Maybe
9596 there should be a variable for this. */
9597 scroll_max = 10;
9598 else
9599 scroll_max = 0;
9600 scroll_max *= CANON_Y_UNIT (f);
9601
9602 /* Decide whether we have to scroll down. Start at the window end
9603 and move this_scroll_margin up to find the position of the scroll
9604 margin. */
9605 window_end = Fwindow_end (window, Qt);
9606 CHARPOS (scroll_margin_pos) = XINT (window_end);
9607 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
9608 if (this_scroll_margin)
9609 {
9610 start_display (&it, w, scroll_margin_pos);
9611 move_it_vertically (&it, - this_scroll_margin);
9612 scroll_margin_pos = it.current.pos;
9613 }
9614
9615 if (PT >= CHARPOS (scroll_margin_pos))
9616 {
9617 int y0;
9618
9619 /* Point is in the scroll margin at the bottom of the window, or
9620 below. Compute a new window start that makes point visible. */
9621
9622 /* Compute the distance from the scroll margin to PT.
9623 Give up if the distance is greater than scroll_max. */
9624 start_display (&it, w, scroll_margin_pos);
9625 y0 = it.current_y;
9626 move_it_to (&it, PT, 0, it.last_visible_y, -1,
9627 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9628
9629 /* To make point visible, we have to move the window start
9630 down so that the line the cursor is in is visible, which
9631 means we have to add in the height of the cursor line. */
9632 dy = line_bottom_y (&it) - y0;
9633
9634 if (dy > scroll_max)
9635 return SCROLLING_FAILED;
9636
9637 /* Move the window start down. If scrolling conservatively,
9638 move it just enough down to make point visible. If
9639 scroll_step is set, move it down by scroll_step. */
9640 start_display (&it, w, startp);
9641
9642 if (scroll_conservatively)
9643 amount_to_scroll
9644 = max (max (dy, CANON_Y_UNIT (f)),
9645 CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
9646 else if (scroll_step || temp_scroll_step)
9647 amount_to_scroll = scroll_max;
9648 else
9649 {
9650 aggressive = current_buffer->scroll_up_aggressively;
9651 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
9652 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
9653 if (NUMBERP (aggressive))
9654 amount_to_scroll = XFLOATINT (aggressive) * height;
9655 }
9656
9657 if (amount_to_scroll <= 0)
9658 return SCROLLING_FAILED;
9659
9660 move_it_vertically (&it, amount_to_scroll);
9661 startp = it.current.pos;
9662 }
9663 else
9664 {
9665 /* See if point is inside the scroll margin at the top of the
9666 window. */
9667 scroll_margin_pos = startp;
9668 if (this_scroll_margin)
9669 {
9670 start_display (&it, w, startp);
9671 move_it_vertically (&it, this_scroll_margin);
9672 scroll_margin_pos = it.current.pos;
9673 }
9674
9675 if (PT < CHARPOS (scroll_margin_pos))
9676 {
9677 /* Point is in the scroll margin at the top of the window or
9678 above what is displayed in the window. */
9679 int y0;
9680
9681 /* Compute the vertical distance from PT to the scroll
9682 margin position. Give up if distance is greater than
9683 scroll_max. */
9684 SET_TEXT_POS (pos, PT, PT_BYTE);
9685 start_display (&it, w, pos);
9686 y0 = it.current_y;
9687 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
9688 it.last_visible_y, -1,
9689 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9690 dy = it.current_y - y0;
9691 if (dy > scroll_max)
9692 return SCROLLING_FAILED;
9693
9694 /* Compute new window start. */
9695 start_display (&it, w, startp);
9696
9697 if (scroll_conservatively)
9698 amount_to_scroll =
9699 max (dy, CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
9700 else if (scroll_step || temp_scroll_step)
9701 amount_to_scroll = scroll_max;
9702 else
9703 {
9704 aggressive = current_buffer->scroll_down_aggressively;
9705 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
9706 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
9707 if (NUMBERP (aggressive))
9708 amount_to_scroll = XFLOATINT (aggressive) * height;
9709 }
9710
9711 if (amount_to_scroll <= 0)
9712 return SCROLLING_FAILED;
9713
9714 move_it_vertically (&it, - amount_to_scroll);
9715 startp = it.current.pos;
9716 }
9717 }
9718
9719 /* Run window scroll functions. */
9720 startp = run_window_scroll_functions (window, startp);
9721
9722 /* Display the window. Give up if new fonts are loaded, or if point
9723 doesn't appear. */
9724 if (!try_window (window, startp))
9725 rc = SCROLLING_NEED_LARGER_MATRICES;
9726 else if (w->cursor.vpos < 0)
9727 {
9728 clear_glyph_matrix (w->desired_matrix);
9729 rc = SCROLLING_FAILED;
9730 }
9731 else
9732 {
9733 /* Maybe forget recorded base line for line number display. */
9734 if (!just_this_one_p
9735 || current_buffer->clip_changed
9736 || BEG_UNCHANGED < CHARPOS (startp))
9737 w->base_line_number = Qnil;
9738
9739 /* If cursor ends up on a partially visible line, shift display
9740 lines up or down. If that fails because we need larger
9741 matrices, give up. */
9742 if (!make_cursor_line_fully_visible (w))
9743 rc = SCROLLING_NEED_LARGER_MATRICES;
9744 else
9745 rc = SCROLLING_SUCCESS;
9746 }
9747
9748 return rc;
9749 }
9750
9751
9752 /* Compute a suitable window start for window W if display of W starts
9753 on a continuation line. Value is non-zero if a new window start
9754 was computed.
9755
9756 The new window start will be computed, based on W's width, starting
9757 from the start of the continued line. It is the start of the
9758 screen line with the minimum distance from the old start W->start. */
9759
9760 static int
9761 compute_window_start_on_continuation_line (w)
9762 struct window *w;
9763 {
9764 struct text_pos pos, start_pos;
9765 int window_start_changed_p = 0;
9766
9767 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
9768
9769 /* If window start is on a continuation line... Window start may be
9770 < BEGV in case there's invisible text at the start of the
9771 buffer (M-x rmail, for example). */
9772 if (CHARPOS (start_pos) > BEGV
9773 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
9774 {
9775 struct it it;
9776 struct glyph_row *row;
9777
9778 /* Handle the case that the window start is out of range. */
9779 if (CHARPOS (start_pos) < BEGV)
9780 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
9781 else if (CHARPOS (start_pos) > ZV)
9782 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
9783
9784 /* Find the start of the continued line. This should be fast
9785 because scan_buffer is fast (newline cache). */
9786 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
9787 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
9788 row, DEFAULT_FACE_ID);
9789 reseat_at_previous_visible_line_start (&it);
9790
9791 /* If the line start is "too far" away from the window start,
9792 say it takes too much time to compute a new window start. */
9793 if (CHARPOS (start_pos) - IT_CHARPOS (it)
9794 < XFASTINT (w->height) * XFASTINT (w->width))
9795 {
9796 int min_distance, distance;
9797
9798 /* Move forward by display lines to find the new window
9799 start. If window width was enlarged, the new start can
9800 be expected to be > the old start. If window width was
9801 decreased, the new window start will be < the old start.
9802 So, we're looking for the display line start with the
9803 minimum distance from the old window start. */
9804 pos = it.current.pos;
9805 min_distance = INFINITY;
9806 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
9807 distance < min_distance)
9808 {
9809 min_distance = distance;
9810 pos = it.current.pos;
9811 move_it_by_lines (&it, 1, 0);
9812 }
9813
9814 /* Set the window start there. */
9815 SET_MARKER_FROM_TEXT_POS (w->start, pos);
9816 window_start_changed_p = 1;
9817 }
9818 }
9819
9820 return window_start_changed_p;
9821 }
9822
9823
9824 /* Try cursor movement in case text has not changes in window WINDOW,
9825 with window start STARTP. Value is
9826
9827 CURSOR_MOVEMENT_SUCCESS if successful
9828
9829 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
9830
9831 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
9832 display. *SCROLL_STEP is set to 1, under certain circumstances, if
9833 we want to scroll as if scroll-step were set to 1. See the code.
9834
9835 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
9836 which case we have to abort this redisplay, and adjust matrices
9837 first. */
9838
9839 enum
9840 {
9841 CURSOR_MOVEMENT_SUCCESS,
9842 CURSOR_MOVEMENT_CANNOT_BE_USED,
9843 CURSOR_MOVEMENT_MUST_SCROLL,
9844 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
9845 };
9846
9847 static int
9848 try_cursor_movement (window, startp, scroll_step)
9849 Lisp_Object window;
9850 struct text_pos startp;
9851 int *scroll_step;
9852 {
9853 struct window *w = XWINDOW (window);
9854 struct frame *f = XFRAME (w->frame);
9855 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
9856
9857 #if GLYPH_DEBUG
9858 if (inhibit_try_cursor_movement)
9859 return rc;
9860 #endif
9861
9862 /* Handle case where text has not changed, only point, and it has
9863 not moved off the frame. */
9864 if (/* Point may be in this window. */
9865 PT >= CHARPOS (startp)
9866 /* Selective display hasn't changed. */
9867 && !current_buffer->clip_changed
9868 /* Function force-mode-line-update is used to force a thorough
9869 redisplay. It sets either windows_or_buffers_changed or
9870 update_mode_lines. So don't take a shortcut here for these
9871 cases. */
9872 && !update_mode_lines
9873 && !windows_or_buffers_changed
9874 /* Can't use this case if highlighting a region. When a
9875 region exists, cursor movement has to do more than just
9876 set the cursor. */
9877 && !(!NILP (Vtransient_mark_mode)
9878 && !NILP (current_buffer->mark_active))
9879 && NILP (w->region_showing)
9880 && NILP (Vshow_trailing_whitespace)
9881 /* Right after splitting windows, last_point may be nil. */
9882 && INTEGERP (w->last_point)
9883 /* This code is not used for mini-buffer for the sake of the case
9884 of redisplaying to replace an echo area message; since in
9885 that case the mini-buffer contents per se are usually
9886 unchanged. This code is of no real use in the mini-buffer
9887 since the handling of this_line_start_pos, etc., in redisplay
9888 handles the same cases. */
9889 && !EQ (window, minibuf_window)
9890 /* When splitting windows or for new windows, it happens that
9891 redisplay is called with a nil window_end_vpos or one being
9892 larger than the window. This should really be fixed in
9893 window.c. I don't have this on my list, now, so we do
9894 approximately the same as the old redisplay code. --gerd. */
9895 && INTEGERP (w->window_end_vpos)
9896 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
9897 && (FRAME_WINDOW_P (f)
9898 || !MARKERP (Voverlay_arrow_position)
9899 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
9900 {
9901 int this_scroll_margin;
9902 struct glyph_row *row = NULL;
9903
9904 #if GLYPH_DEBUG
9905 debug_method_add (w, "cursor movement");
9906 #endif
9907
9908 /* Scroll if point within this distance from the top or bottom
9909 of the window. This is a pixel value. */
9910 this_scroll_margin = max (0, scroll_margin);
9911 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
9912 this_scroll_margin *= CANON_Y_UNIT (f);
9913
9914 /* Start with the row the cursor was displayed during the last
9915 not paused redisplay. Give up if that row is not valid. */
9916 if (w->last_cursor.vpos < 0
9917 || w->last_cursor.vpos >= w->current_matrix->nrows)
9918 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9919 else
9920 {
9921 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
9922 if (row->mode_line_p)
9923 ++row;
9924 if (!row->enabled_p)
9925 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9926 }
9927
9928 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
9929 {
9930 int scroll_p = 0;
9931 int last_y = window_text_bottom_y (w) - this_scroll_margin;
9932
9933 if (PT > XFASTINT (w->last_point))
9934 {
9935 /* Point has moved forward. */
9936 while (MATRIX_ROW_END_CHARPOS (row) < PT
9937 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
9938 {
9939 xassert (row->enabled_p);
9940 ++row;
9941 }
9942
9943 /* The end position of a row equals the start position
9944 of the next row. If PT is there, we would rather
9945 display it in the next line. */
9946 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9947 && MATRIX_ROW_END_CHARPOS (row) == PT
9948 && !cursor_row_p (w, row))
9949 ++row;
9950
9951 /* If within the scroll margin, scroll. Note that
9952 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
9953 the next line would be drawn, and that
9954 this_scroll_margin can be zero. */
9955 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
9956 || PT > MATRIX_ROW_END_CHARPOS (row)
9957 /* Line is completely visible last line in window
9958 and PT is to be set in the next line. */
9959 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
9960 && PT == MATRIX_ROW_END_CHARPOS (row)
9961 && !row->ends_at_zv_p
9962 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
9963 scroll_p = 1;
9964 }
9965 else if (PT < XFASTINT (w->last_point))
9966 {
9967 /* Cursor has to be moved backward. Note that PT >=
9968 CHARPOS (startp) because of the outer
9969 if-statement. */
9970 while (!row->mode_line_p
9971 && (MATRIX_ROW_START_CHARPOS (row) > PT
9972 || (MATRIX_ROW_START_CHARPOS (row) == PT
9973 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
9974 && (row->y > this_scroll_margin
9975 || CHARPOS (startp) == BEGV))
9976 {
9977 xassert (row->enabled_p);
9978 --row;
9979 }
9980
9981 /* Consider the following case: Window starts at BEGV,
9982 there is invisible, intangible text at BEGV, so that
9983 display starts at some point START > BEGV. It can
9984 happen that we are called with PT somewhere between
9985 BEGV and START. Try to handle that case. */
9986 if (row < w->current_matrix->rows
9987 || row->mode_line_p)
9988 {
9989 row = w->current_matrix->rows;
9990 if (row->mode_line_p)
9991 ++row;
9992 }
9993
9994 /* Due to newlines in overlay strings, we may have to
9995 skip forward over overlay strings. */
9996 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9997 && MATRIX_ROW_END_CHARPOS (row) == PT
9998 && !cursor_row_p (w, row))
9999 ++row;
10000
10001 /* If within the scroll margin, scroll. */
10002 if (row->y < this_scroll_margin
10003 && CHARPOS (startp) != BEGV)
10004 scroll_p = 1;
10005 }
10006
10007 if (PT < MATRIX_ROW_START_CHARPOS (row)
10008 || PT > MATRIX_ROW_END_CHARPOS (row))
10009 {
10010 /* if PT is not in the glyph row, give up. */
10011 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10012 }
10013 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
10014 {
10015 if (PT == MATRIX_ROW_END_CHARPOS (row)
10016 && !row->ends_at_zv_p
10017 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
10018 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10019 else if (row->height > window_box_height (w))
10020 {
10021 /* If we end up in a partially visible line, let's
10022 make it fully visible, except when it's taller
10023 than the window, in which case we can't do much
10024 about it. */
10025 *scroll_step = 1;
10026 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10027 }
10028 else
10029 {
10030 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10031 try_window (window, startp);
10032 if (!make_cursor_line_fully_visible (w))
10033 rc = CURSOR_MOVEMENT_NEED_LARGER_MATRICES;
10034 else
10035 rc = CURSOR_MOVEMENT_SUCCESS;
10036 }
10037 }
10038 else if (scroll_p)
10039 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10040 else
10041 {
10042 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10043 rc = CURSOR_MOVEMENT_SUCCESS;
10044 }
10045 }
10046 }
10047
10048 return rc;
10049 }
10050
10051
10052 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
10053 selected_window is redisplayed. */
10054
10055 static void
10056 redisplay_window (window, just_this_one_p)
10057 Lisp_Object window;
10058 int just_this_one_p;
10059 {
10060 struct window *w = XWINDOW (window);
10061 struct frame *f = XFRAME (w->frame);
10062 struct buffer *buffer = XBUFFER (w->buffer);
10063 struct buffer *old = current_buffer;
10064 struct text_pos lpoint, opoint, startp;
10065 int update_mode_line;
10066 int tem;
10067 struct it it;
10068 /* Record it now because it's overwritten. */
10069 int current_matrix_up_to_date_p = 0;
10070 int temp_scroll_step = 0;
10071 int count = BINDING_STACK_SIZE ();
10072 int rc;
10073
10074 SET_TEXT_POS (lpoint, PT, PT_BYTE);
10075 opoint = lpoint;
10076
10077 /* W must be a leaf window here. */
10078 xassert (!NILP (w->buffer));
10079 #if GLYPH_DEBUG
10080 *w->desired_matrix->method = 0;
10081 #endif
10082
10083 specbind (Qinhibit_point_motion_hooks, Qt);
10084
10085 reconsider_clip_changes (w, buffer);
10086
10087 /* Has the mode line to be updated? */
10088 update_mode_line = (!NILP (w->update_mode_line)
10089 || update_mode_lines
10090 || buffer->clip_changed);
10091
10092 if (MINI_WINDOW_P (w))
10093 {
10094 if (w == XWINDOW (echo_area_window)
10095 && !NILP (echo_area_buffer[0]))
10096 {
10097 if (update_mode_line)
10098 /* We may have to update a tty frame's menu bar or a
10099 tool-bar. Example `M-x C-h C-h C-g'. */
10100 goto finish_menu_bars;
10101 else
10102 /* We've already displayed the echo area glyphs in this window. */
10103 goto finish_scroll_bars;
10104 }
10105 else if (w != XWINDOW (minibuf_window))
10106 {
10107 /* W is a mini-buffer window, but it's not the currently
10108 active one, so clear it. */
10109 int yb = window_text_bottom_y (w);
10110 struct glyph_row *row;
10111 int y;
10112
10113 for (y = 0, row = w->desired_matrix->rows;
10114 y < yb;
10115 y += row->height, ++row)
10116 blank_row (w, row, y);
10117 goto finish_scroll_bars;
10118 }
10119
10120 clear_glyph_matrix (w->desired_matrix);
10121 }
10122
10123 /* Otherwise set up data on this window; select its buffer and point
10124 value. */
10125 /* Really select the buffer, for the sake of buffer-local
10126 variables. */
10127 set_buffer_internal_1 (XBUFFER (w->buffer));
10128 SET_TEXT_POS (opoint, PT, PT_BYTE);
10129
10130 current_matrix_up_to_date_p
10131 = (!NILP (w->window_end_valid)
10132 && !current_buffer->clip_changed
10133 && XFASTINT (w->last_modified) >= MODIFF
10134 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
10135
10136 /* When windows_or_buffers_changed is non-zero, we can't rely on
10137 the window end being valid, so set it to nil there. */
10138 if (windows_or_buffers_changed)
10139 {
10140 /* If window starts on a continuation line, maybe adjust the
10141 window start in case the window's width changed. */
10142 if (XMARKER (w->start)->buffer == current_buffer)
10143 compute_window_start_on_continuation_line (w);
10144
10145 w->window_end_valid = Qnil;
10146 }
10147
10148 /* Some sanity checks. */
10149 CHECK_WINDOW_END (w);
10150 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
10151 abort ();
10152 if (BYTEPOS (opoint) < CHARPOS (opoint))
10153 abort ();
10154
10155 /* If %c is in mode line, update it if needed. */
10156 if (!NILP (w->column_number_displayed)
10157 /* This alternative quickly identifies a common case
10158 where no change is needed. */
10159 && !(PT == XFASTINT (w->last_point)
10160 && XFASTINT (w->last_modified) >= MODIFF
10161 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
10162 && XFASTINT (w->column_number_displayed) != current_column ())
10163 update_mode_line = 1;
10164
10165 /* Count number of windows showing the selected buffer. An indirect
10166 buffer counts as its base buffer. */
10167 if (!just_this_one_p)
10168 {
10169 struct buffer *current_base, *window_base;
10170 current_base = current_buffer;
10171 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
10172 if (current_base->base_buffer)
10173 current_base = current_base->base_buffer;
10174 if (window_base->base_buffer)
10175 window_base = window_base->base_buffer;
10176 if (current_base == window_base)
10177 buffer_shared++;
10178 }
10179
10180 /* Point refers normally to the selected window. For any other
10181 window, set up appropriate value. */
10182 if (!EQ (window, selected_window))
10183 {
10184 int new_pt = XMARKER (w->pointm)->charpos;
10185 int new_pt_byte = marker_byte_position (w->pointm);
10186 if (new_pt < BEGV)
10187 {
10188 new_pt = BEGV;
10189 new_pt_byte = BEGV_BYTE;
10190 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
10191 }
10192 else if (new_pt > (ZV - 1))
10193 {
10194 new_pt = ZV;
10195 new_pt_byte = ZV_BYTE;
10196 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
10197 }
10198
10199 /* We don't use SET_PT so that the point-motion hooks don't run. */
10200 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
10201 }
10202
10203 /* If any of the character widths specified in the display table
10204 have changed, invalidate the width run cache. It's true that
10205 this may be a bit late to catch such changes, but the rest of
10206 redisplay goes (non-fatally) haywire when the display table is
10207 changed, so why should we worry about doing any better? */
10208 if (current_buffer->width_run_cache)
10209 {
10210 struct Lisp_Char_Table *disptab = buffer_display_table ();
10211
10212 if (! disptab_matches_widthtab (disptab,
10213 XVECTOR (current_buffer->width_table)))
10214 {
10215 invalidate_region_cache (current_buffer,
10216 current_buffer->width_run_cache,
10217 BEG, Z);
10218 recompute_width_table (current_buffer, disptab);
10219 }
10220 }
10221
10222 /* If window-start is screwed up, choose a new one. */
10223 if (XMARKER (w->start)->buffer != current_buffer)
10224 goto recenter;
10225
10226 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10227
10228 /* If someone specified a new starting point but did not insist,
10229 check whether it can be used. */
10230 if (!NILP (w->optional_new_start)
10231 && CHARPOS (startp) >= BEGV
10232 && CHARPOS (startp) <= ZV)
10233 {
10234 w->optional_new_start = Qnil;
10235 start_display (&it, w, startp);
10236 move_it_to (&it, PT, 0, it.last_visible_y, -1,
10237 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
10238 if (IT_CHARPOS (it) == PT)
10239 w->force_start = Qt;
10240 }
10241
10242 /* Handle case where place to start displaying has been specified,
10243 unless the specified location is outside the accessible range. */
10244 if (!NILP (w->force_start)
10245 || w->frozen_window_start_p)
10246 {
10247 w->force_start = Qnil;
10248 w->vscroll = 0;
10249 w->window_end_valid = Qnil;
10250
10251 /* Forget any recorded base line for line number display. */
10252 if (!current_matrix_up_to_date_p
10253 || current_buffer->clip_changed)
10254 w->base_line_number = Qnil;
10255
10256 /* Redisplay the mode line. Select the buffer properly for that.
10257 Also, run the hook window-scroll-functions
10258 because we have scrolled. */
10259 /* Note, we do this after clearing force_start because
10260 if there's an error, it is better to forget about force_start
10261 than to get into an infinite loop calling the hook functions
10262 and having them get more errors. */
10263 if (!update_mode_line
10264 || ! NILP (Vwindow_scroll_functions))
10265 {
10266 update_mode_line = 1;
10267 w->update_mode_line = Qt;
10268 startp = run_window_scroll_functions (window, startp);
10269 }
10270
10271 w->last_modified = make_number (0);
10272 w->last_overlay_modified = make_number (0);
10273 if (CHARPOS (startp) < BEGV)
10274 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
10275 else if (CHARPOS (startp) > ZV)
10276 SET_TEXT_POS (startp, ZV, ZV_BYTE);
10277
10278 /* Redisplay, then check if cursor has been set during the
10279 redisplay. Give up if new fonts were loaded. */
10280 if (!try_window (window, startp))
10281 {
10282 w->force_start = Qt;
10283 clear_glyph_matrix (w->desired_matrix);
10284 goto finish_scroll_bars;
10285 }
10286
10287 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
10288 {
10289 /* If point does not appear, try to move point so it does
10290 appear. The desired matrix has been built above, so we
10291 can use it here. */
10292 int window_height;
10293 struct glyph_row *row;
10294
10295 window_height = window_box_height (w) / 2;
10296 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
10297 while (MATRIX_ROW_BOTTOM_Y (row) < window_height)
10298 ++row;
10299
10300 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
10301 MATRIX_ROW_START_BYTEPOS (row));
10302
10303 if (w != XWINDOW (selected_window))
10304 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
10305 else if (current_buffer == old)
10306 SET_TEXT_POS (lpoint, PT, PT_BYTE);
10307
10308 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
10309
10310 /* If we are highlighting the region, then we just changed
10311 the region, so redisplay to show it. */
10312 if (!NILP (Vtransient_mark_mode)
10313 && !NILP (current_buffer->mark_active))
10314 {
10315 clear_glyph_matrix (w->desired_matrix);
10316 if (!try_window (window, startp))
10317 goto need_larger_matrices;
10318 }
10319 }
10320
10321 if (!make_cursor_line_fully_visible (w))
10322 goto need_larger_matrices;
10323 #if GLYPH_DEBUG
10324 debug_method_add (w, "forced window start");
10325 #endif
10326 goto done;
10327 }
10328
10329 /* Handle case where text has not changed, only point, and it has
10330 not moved off the frame. */
10331 if (current_matrix_up_to_date_p
10332 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
10333 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
10334 {
10335 switch (rc)
10336 {
10337 case CURSOR_MOVEMENT_SUCCESS:
10338 goto done;
10339
10340 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
10341 goto need_larger_matrices;
10342
10343 case CURSOR_MOVEMENT_MUST_SCROLL:
10344 goto try_to_scroll;
10345
10346 default:
10347 abort ();
10348 }
10349 }
10350 /* If current starting point was originally the beginning of a line
10351 but no longer is, find a new starting point. */
10352 else if (!NILP (w->start_at_line_beg)
10353 && !(CHARPOS (startp) <= BEGV
10354 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
10355 {
10356 #if GLYPH_DEBUG
10357 debug_method_add (w, "recenter 1");
10358 #endif
10359 goto recenter;
10360 }
10361
10362 /* Try scrolling with try_window_id. Value is > 0 if update has
10363 been done, it is -1 if we know that the same window start will
10364 not work. It is 0 if unsuccessful for some other reason. */
10365 else if ((tem = try_window_id (w)) != 0)
10366 {
10367 #if GLYPH_DEBUG
10368 debug_method_add (w, "try_window_id %d", tem);
10369 #endif
10370
10371 if (fonts_changed_p)
10372 goto need_larger_matrices;
10373 if (tem > 0)
10374 goto done;
10375
10376 /* Otherwise try_window_id has returned -1 which means that we
10377 don't want the alternative below this comment to execute. */
10378 }
10379 else if (CHARPOS (startp) >= BEGV
10380 && CHARPOS (startp) <= ZV
10381 && PT >= CHARPOS (startp)
10382 && (CHARPOS (startp) < ZV
10383 /* Avoid starting at end of buffer. */
10384 || CHARPOS (startp) == BEGV
10385 || (XFASTINT (w->last_modified) >= MODIFF
10386 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
10387 {
10388 #if GLYPH_DEBUG
10389 debug_method_add (w, "same window start");
10390 #endif
10391
10392 /* Try to redisplay starting at same place as before.
10393 If point has not moved off frame, accept the results. */
10394 if (!current_matrix_up_to_date_p
10395 /* Don't use try_window_reusing_current_matrix in this case
10396 because a window scroll function can have changed the
10397 buffer. */
10398 || !NILP (Vwindow_scroll_functions)
10399 || MINI_WINDOW_P (w)
10400 || !try_window_reusing_current_matrix (w))
10401 {
10402 IF_DEBUG (debug_method_add (w, "1"));
10403 try_window (window, startp);
10404 }
10405
10406 if (fonts_changed_p)
10407 goto need_larger_matrices;
10408
10409 if (w->cursor.vpos >= 0)
10410 {
10411 if (!just_this_one_p
10412 || current_buffer->clip_changed
10413 || BEG_UNCHANGED < CHARPOS (startp))
10414 /* Forget any recorded base line for line number display. */
10415 w->base_line_number = Qnil;
10416
10417 if (!make_cursor_line_fully_visible (w))
10418 goto need_larger_matrices;
10419 goto done;
10420 }
10421 else
10422 clear_glyph_matrix (w->desired_matrix);
10423 }
10424
10425 try_to_scroll:
10426
10427 w->last_modified = make_number (0);
10428 w->last_overlay_modified = make_number (0);
10429
10430 /* Redisplay the mode line. Select the buffer properly for that. */
10431 if (!update_mode_line)
10432 {
10433 update_mode_line = 1;
10434 w->update_mode_line = Qt;
10435 }
10436
10437 /* Try to scroll by specified few lines. */
10438 if ((scroll_conservatively
10439 || scroll_step
10440 || temp_scroll_step
10441 || NUMBERP (current_buffer->scroll_up_aggressively)
10442 || NUMBERP (current_buffer->scroll_down_aggressively))
10443 && !current_buffer->clip_changed
10444 && CHARPOS (startp) >= BEGV
10445 && CHARPOS (startp) <= ZV)
10446 {
10447 /* The function returns -1 if new fonts were loaded, 1 if
10448 successful, 0 if not successful. */
10449 int rc = try_scrolling (window, just_this_one_p,
10450 scroll_conservatively,
10451 scroll_step,
10452 temp_scroll_step);
10453 switch (rc)
10454 {
10455 case SCROLLING_SUCCESS:
10456 goto done;
10457
10458 case SCROLLING_NEED_LARGER_MATRICES:
10459 goto need_larger_matrices;
10460
10461 case SCROLLING_FAILED:
10462 break;
10463
10464 default:
10465 abort ();
10466 }
10467 }
10468
10469 /* Finally, just choose place to start which centers point */
10470
10471 recenter:
10472
10473 #if GLYPH_DEBUG
10474 debug_method_add (w, "recenter");
10475 #endif
10476
10477 /* w->vscroll = 0; */
10478
10479 /* Forget any previously recorded base line for line number display. */
10480 if (!current_matrix_up_to_date_p
10481 || current_buffer->clip_changed)
10482 w->base_line_number = Qnil;
10483
10484 /* Move backward half the height of the window. */
10485 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10486 it.current_y = it.last_visible_y;
10487 move_it_vertically_backward (&it, window_box_height (w) / 2);
10488 xassert (IT_CHARPOS (it) >= BEGV);
10489
10490 /* The function move_it_vertically_backward may move over more
10491 than the specified y-distance. If it->w is small, e.g. a
10492 mini-buffer window, we may end up in front of the window's
10493 display area. Start displaying at the start of the line
10494 containing PT in this case. */
10495 if (it.current_y <= 0)
10496 {
10497 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10498 move_it_vertically (&it, 0);
10499 xassert (IT_CHARPOS (it) <= PT);
10500 it.current_y = 0;
10501 }
10502
10503 it.current_x = it.hpos = 0;
10504
10505 /* Set startp here explicitly in case that helps avoid an infinite loop
10506 in case the window-scroll-functions functions get errors. */
10507 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
10508
10509 /* Run scroll hooks. */
10510 startp = run_window_scroll_functions (window, it.current.pos);
10511
10512 /* Redisplay the window. */
10513 if (!current_matrix_up_to_date_p
10514 || windows_or_buffers_changed
10515 /* Don't use try_window_reusing_current_matrix in this case
10516 because it can have changed the buffer. */
10517 || !NILP (Vwindow_scroll_functions)
10518 || !just_this_one_p
10519 || MINI_WINDOW_P (w)
10520 || !try_window_reusing_current_matrix (w))
10521 try_window (window, startp);
10522
10523 /* If new fonts have been loaded (due to fontsets), give up. We
10524 have to start a new redisplay since we need to re-adjust glyph
10525 matrices. */
10526 if (fonts_changed_p)
10527 goto need_larger_matrices;
10528
10529 /* If cursor did not appear assume that the middle of the window is
10530 in the first line of the window. Do it again with the next line.
10531 (Imagine a window of height 100, displaying two lines of height
10532 60. Moving back 50 from it->last_visible_y will end in the first
10533 line.) */
10534 if (w->cursor.vpos < 0)
10535 {
10536 if (!NILP (w->window_end_valid)
10537 && PT >= Z - XFASTINT (w->window_end_pos))
10538 {
10539 clear_glyph_matrix (w->desired_matrix);
10540 move_it_by_lines (&it, 1, 0);
10541 try_window (window, it.current.pos);
10542 }
10543 else if (PT < IT_CHARPOS (it))
10544 {
10545 clear_glyph_matrix (w->desired_matrix);
10546 move_it_by_lines (&it, -1, 0);
10547 try_window (window, it.current.pos);
10548 }
10549 else
10550 {
10551 /* Not much we can do about it. */
10552 }
10553 }
10554
10555 /* Consider the following case: Window starts at BEGV, there is
10556 invisible, intangible text at BEGV, so that display starts at
10557 some point START > BEGV. It can happen that we are called with
10558 PT somewhere between BEGV and START. Try to handle that case. */
10559 if (w->cursor.vpos < 0)
10560 {
10561 struct glyph_row *row = w->current_matrix->rows;
10562 if (row->mode_line_p)
10563 ++row;
10564 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10565 }
10566
10567 if (!make_cursor_line_fully_visible (w))
10568 goto need_larger_matrices;
10569
10570 done:
10571
10572 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10573 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
10574 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
10575 ? Qt : Qnil);
10576
10577 /* Display the mode line, if we must. */
10578 if ((update_mode_line
10579 /* If window not full width, must redo its mode line
10580 if (a) the window to its side is being redone and
10581 (b) we do a frame-based redisplay. This is a consequence
10582 of how inverted lines are drawn in frame-based redisplay. */
10583 || (!just_this_one_p
10584 && !FRAME_WINDOW_P (f)
10585 && !WINDOW_FULL_WIDTH_P (w))
10586 /* Line number to display. */
10587 || INTEGERP (w->base_line_pos)
10588 /* Column number is displayed and different from the one displayed. */
10589 || (!NILP (w->column_number_displayed)
10590 && XFASTINT (w->column_number_displayed) != current_column ()))
10591 /* This means that the window has a mode line. */
10592 && (WINDOW_WANTS_MODELINE_P (w)
10593 || WINDOW_WANTS_HEADER_LINE_P (w)))
10594 {
10595 display_mode_lines (w);
10596
10597 /* If mode line height has changed, arrange for a thorough
10598 immediate redisplay using the correct mode line height. */
10599 if (WINDOW_WANTS_MODELINE_P (w)
10600 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
10601 {
10602 fonts_changed_p = 1;
10603 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
10604 = DESIRED_MODE_LINE_HEIGHT (w);
10605 }
10606
10607 /* If top line height has changed, arrange for a thorough
10608 immediate redisplay using the correct mode line height. */
10609 if (WINDOW_WANTS_HEADER_LINE_P (w)
10610 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
10611 {
10612 fonts_changed_p = 1;
10613 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
10614 = DESIRED_HEADER_LINE_HEIGHT (w);
10615 }
10616
10617 if (fonts_changed_p)
10618 goto need_larger_matrices;
10619 }
10620
10621 if (!line_number_displayed
10622 && !BUFFERP (w->base_line_pos))
10623 {
10624 w->base_line_pos = Qnil;
10625 w->base_line_number = Qnil;
10626 }
10627
10628 finish_menu_bars:
10629
10630 /* When we reach a frame's selected window, redo the frame's menu bar. */
10631 if (update_mode_line
10632 && EQ (FRAME_SELECTED_WINDOW (f), window))
10633 {
10634 int redisplay_menu_p = 0;
10635
10636 if (FRAME_WINDOW_P (f))
10637 {
10638 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
10639 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
10640 #else
10641 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10642 #endif
10643 }
10644 else
10645 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10646
10647 if (redisplay_menu_p)
10648 display_menu_bar (w);
10649
10650 #ifdef HAVE_WINDOW_SYSTEM
10651 if (WINDOWP (f->tool_bar_window)
10652 && (FRAME_TOOL_BAR_LINES (f) > 0
10653 || auto_resize_tool_bars_p))
10654 redisplay_tool_bar (f);
10655 #endif
10656 }
10657
10658 need_larger_matrices:
10659 ;
10660 finish_scroll_bars:
10661
10662 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
10663 {
10664 int start, end, whole;
10665
10666 /* Calculate the start and end positions for the current window.
10667 At some point, it would be nice to choose between scrollbars
10668 which reflect the whole buffer size, with special markers
10669 indicating narrowing, and scrollbars which reflect only the
10670 visible region.
10671
10672 Note that mini-buffers sometimes aren't displaying any text. */
10673 if (!MINI_WINDOW_P (w)
10674 || (w == XWINDOW (minibuf_window)
10675 && NILP (echo_area_buffer[0])))
10676 {
10677 whole = ZV - BEGV;
10678 start = marker_position (w->start) - BEGV;
10679 /* I don't think this is guaranteed to be right. For the
10680 moment, we'll pretend it is. */
10681 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
10682
10683 if (end < start)
10684 end = start;
10685 if (whole < (end - start))
10686 whole = end - start;
10687 }
10688 else
10689 start = end = whole = 0;
10690
10691 /* Indicate what this scroll bar ought to be displaying now. */
10692 set_vertical_scroll_bar_hook (w, end - start, whole, start);
10693
10694 /* Note that we actually used the scroll bar attached to this
10695 window, so it shouldn't be deleted at the end of redisplay. */
10696 redeem_scroll_bar_hook (w);
10697 }
10698
10699 /* Restore current_buffer and value of point in it. */
10700 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
10701 set_buffer_internal_1 (old);
10702 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
10703
10704 unbind_to (count, Qnil);
10705 }
10706
10707
10708 /* Build the complete desired matrix of WINDOW with a window start
10709 buffer position POS. Value is non-zero if successful. It is zero
10710 if fonts were loaded during redisplay which makes re-adjusting
10711 glyph matrices necessary. */
10712
10713 int
10714 try_window (window, pos)
10715 Lisp_Object window;
10716 struct text_pos pos;
10717 {
10718 struct window *w = XWINDOW (window);
10719 struct it it;
10720 struct glyph_row *last_text_row = NULL;
10721
10722 /* Make POS the new window start. */
10723 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
10724
10725 /* Mark cursor position as unknown. No overlay arrow seen. */
10726 w->cursor.vpos = -1;
10727 overlay_arrow_seen = 0;
10728
10729 /* Initialize iterator and info to start at POS. */
10730 start_display (&it, w, pos);
10731
10732 /* Display all lines of W. */
10733 while (it.current_y < it.last_visible_y)
10734 {
10735 if (display_line (&it))
10736 last_text_row = it.glyph_row - 1;
10737 if (fonts_changed_p)
10738 return 0;
10739 }
10740
10741 /* If bottom moved off end of frame, change mode line percentage. */
10742 if (XFASTINT (w->window_end_pos) <= 0
10743 && Z != IT_CHARPOS (it))
10744 w->update_mode_line = Qt;
10745
10746 /* Set window_end_pos to the offset of the last character displayed
10747 on the window from the end of current_buffer. Set
10748 window_end_vpos to its row number. */
10749 if (last_text_row)
10750 {
10751 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
10752 w->window_end_bytepos
10753 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10754 w->window_end_pos
10755 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10756 w->window_end_vpos
10757 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10758 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
10759 ->displays_text_p);
10760 }
10761 else
10762 {
10763 w->window_end_bytepos = 0;
10764 w->window_end_pos = w->window_end_vpos = make_number (0);
10765 }
10766
10767 /* But that is not valid info until redisplay finishes. */
10768 w->window_end_valid = Qnil;
10769 return 1;
10770 }
10771
10772
10773 \f
10774 /************************************************************************
10775 Window redisplay reusing current matrix when buffer has not changed
10776 ************************************************************************/
10777
10778 /* Try redisplay of window W showing an unchanged buffer with a
10779 different window start than the last time it was displayed by
10780 reusing its current matrix. Value is non-zero if successful.
10781 W->start is the new window start. */
10782
10783 static int
10784 try_window_reusing_current_matrix (w)
10785 struct window *w;
10786 {
10787 struct frame *f = XFRAME (w->frame);
10788 struct glyph_row *row, *bottom_row;
10789 struct it it;
10790 struct run run;
10791 struct text_pos start, new_start;
10792 int nrows_scrolled, i;
10793 struct glyph_row *last_text_row;
10794 struct glyph_row *last_reused_text_row;
10795 struct glyph_row *start_row;
10796 int start_vpos, min_y, max_y;
10797
10798 #if GLYPH_DEBUG
10799 if (inhibit_try_window_reusing)
10800 return 0;
10801 #endif
10802
10803 if (/* This function doesn't handle terminal frames. */
10804 !FRAME_WINDOW_P (f)
10805 /* Don't try to reuse the display if windows have been split
10806 or such. */
10807 || windows_or_buffers_changed)
10808 return 0;
10809
10810 /* Can't do this if region may have changed. */
10811 if ((!NILP (Vtransient_mark_mode)
10812 && !NILP (current_buffer->mark_active))
10813 || !NILP (w->region_showing)
10814 || !NILP (Vshow_trailing_whitespace))
10815 return 0;
10816
10817 /* If top-line visibility has changed, give up. */
10818 if (WINDOW_WANTS_HEADER_LINE_P (w)
10819 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
10820 return 0;
10821
10822 /* Give up if old or new display is scrolled vertically. We could
10823 make this function handle this, but right now it doesn't. */
10824 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10825 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
10826 return 0;
10827
10828 /* The variable new_start now holds the new window start. The old
10829 start `start' can be determined from the current matrix. */
10830 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
10831 start = start_row->start.pos;
10832 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
10833
10834 /* Clear the desired matrix for the display below. */
10835 clear_glyph_matrix (w->desired_matrix);
10836
10837 if (CHARPOS (new_start) <= CHARPOS (start))
10838 {
10839 int first_row_y;
10840
10841 /* Don't use this method if the display starts with an ellipsis
10842 displayed for invisible text. It's not easy to handle that case
10843 below, and it's certainly not worth the effort since this is
10844 not a frequent case. */
10845 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
10846 return 0;
10847
10848 IF_DEBUG (debug_method_add (w, "twu1"));
10849
10850 /* Display up to a row that can be reused. The variable
10851 last_text_row is set to the last row displayed that displays
10852 text. Note that it.vpos == 0 if or if not there is a
10853 header-line; it's not the same as the MATRIX_ROW_VPOS! */
10854 start_display (&it, w, new_start);
10855 first_row_y = it.current_y;
10856 w->cursor.vpos = -1;
10857 last_text_row = last_reused_text_row = NULL;
10858
10859 while (it.current_y < it.last_visible_y
10860 && IT_CHARPOS (it) < CHARPOS (start)
10861 && !fonts_changed_p)
10862 if (display_line (&it))
10863 last_text_row = it.glyph_row - 1;
10864
10865 /* A value of current_y < last_visible_y means that we stopped
10866 at the previous window start, which in turn means that we
10867 have at least one reusable row. */
10868 if (it.current_y < it.last_visible_y)
10869 {
10870 /* IT.vpos always starts from 0; it counts text lines. */
10871 nrows_scrolled = it.vpos;
10872
10873 /* Find PT if not already found in the lines displayed. */
10874 if (w->cursor.vpos < 0)
10875 {
10876 int dy = it.current_y - first_row_y;
10877
10878 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10879 row = row_containing_pos (w, PT, row, NULL, dy);
10880 if (row)
10881 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
10882 dy, nrows_scrolled);
10883 else
10884 {
10885 clear_glyph_matrix (w->desired_matrix);
10886 return 0;
10887 }
10888 }
10889
10890 /* Scroll the display. Do it before the current matrix is
10891 changed. The problem here is that update has not yet
10892 run, i.e. part of the current matrix is not up to date.
10893 scroll_run_hook will clear the cursor, and use the
10894 current matrix to get the height of the row the cursor is
10895 in. */
10896 run.current_y = first_row_y;
10897 run.desired_y = it.current_y;
10898 run.height = it.last_visible_y - it.current_y;
10899
10900 if (run.height > 0 && run.current_y != run.desired_y)
10901 {
10902 update_begin (f);
10903 rif->update_window_begin_hook (w);
10904 rif->clear_mouse_face (w);
10905 rif->scroll_run_hook (w, &run);
10906 rif->update_window_end_hook (w, 0, 0);
10907 update_end (f);
10908 }
10909
10910 /* Shift current matrix down by nrows_scrolled lines. */
10911 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
10912 rotate_matrix (w->current_matrix,
10913 start_vpos,
10914 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10915 nrows_scrolled);
10916
10917 /* Disable lines that must be updated. */
10918 for (i = 0; i < it.vpos; ++i)
10919 (start_row + i)->enabled_p = 0;
10920
10921 /* Re-compute Y positions. */
10922 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10923 max_y = it.last_visible_y;
10924 for (row = start_row + nrows_scrolled;
10925 row < bottom_row;
10926 ++row)
10927 {
10928 row->y = it.current_y;
10929 row->visible_height = row->height;
10930
10931 if (row->y < min_y)
10932 row->visible_height -= min_y - row->y;
10933 if (row->y + row->height > max_y)
10934 row->visible_height -= row->y + row->height - max_y;
10935
10936 it.current_y += row->height;
10937
10938 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10939 last_reused_text_row = row;
10940 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
10941 break;
10942 }
10943
10944 /* Disable lines in the current matrix which are now
10945 below the window. */
10946 for (++row; row < bottom_row; ++row)
10947 row->enabled_p = 0;
10948 }
10949
10950 /* Update window_end_pos etc.; last_reused_text_row is the last
10951 reused row from the current matrix containing text, if any.
10952 The value of last_text_row is the last displayed line
10953 containing text. */
10954 if (last_reused_text_row)
10955 {
10956 w->window_end_bytepos
10957 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
10958 w->window_end_pos
10959 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
10960 w->window_end_vpos
10961 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
10962 w->current_matrix));
10963 }
10964 else if (last_text_row)
10965 {
10966 w->window_end_bytepos
10967 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10968 w->window_end_pos
10969 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10970 w->window_end_vpos
10971 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10972 }
10973 else
10974 {
10975 /* This window must be completely empty. */
10976 w->window_end_bytepos = 0;
10977 w->window_end_pos = w->window_end_vpos = make_number (0);
10978 }
10979 w->window_end_valid = Qnil;
10980
10981 /* Update hint: don't try scrolling again in update_window. */
10982 w->desired_matrix->no_scrolling_p = 1;
10983
10984 #if GLYPH_DEBUG
10985 debug_method_add (w, "try_window_reusing_current_matrix 1");
10986 #endif
10987 return 1;
10988 }
10989 else if (CHARPOS (new_start) > CHARPOS (start))
10990 {
10991 struct glyph_row *pt_row, *row;
10992 struct glyph_row *first_reusable_row;
10993 struct glyph_row *first_row_to_display;
10994 int dy;
10995 int yb = window_text_bottom_y (w);
10996
10997 /* Find the row starting at new_start, if there is one. Don't
10998 reuse a partially visible line at the end. */
10999 first_reusable_row = start_row;
11000 while (first_reusable_row->enabled_p
11001 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
11002 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
11003 < CHARPOS (new_start)))
11004 ++first_reusable_row;
11005
11006 /* Give up if there is no row to reuse. */
11007 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
11008 || !first_reusable_row->enabled_p
11009 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
11010 != CHARPOS (new_start)))
11011 return 0;
11012
11013 /* We can reuse fully visible rows beginning with
11014 first_reusable_row to the end of the window. Set
11015 first_row_to_display to the first row that cannot be reused.
11016 Set pt_row to the row containing point, if there is any. */
11017 pt_row = NULL;
11018 for (first_row_to_display = first_reusable_row;
11019 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
11020 ++first_row_to_display)
11021 {
11022 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
11023 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
11024 pt_row = first_row_to_display;
11025 }
11026
11027 /* Start displaying at the start of first_row_to_display. */
11028 xassert (first_row_to_display->y < yb);
11029 init_to_row_start (&it, w, first_row_to_display);
11030
11031 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
11032 - start_vpos);
11033 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
11034 - nrows_scrolled);
11035 it.current_y = (first_row_to_display->y - first_reusable_row->y
11036 + WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
11037
11038 /* Display lines beginning with first_row_to_display in the
11039 desired matrix. Set last_text_row to the last row displayed
11040 that displays text. */
11041 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
11042 if (pt_row == NULL)
11043 w->cursor.vpos = -1;
11044 last_text_row = NULL;
11045 while (it.current_y < it.last_visible_y && !fonts_changed_p)
11046 if (display_line (&it))
11047 last_text_row = it.glyph_row - 1;
11048
11049 /* Give up If point isn't in a row displayed or reused. */
11050 if (w->cursor.vpos < 0)
11051 {
11052 clear_glyph_matrix (w->desired_matrix);
11053 return 0;
11054 }
11055
11056 /* If point is in a reused row, adjust y and vpos of the cursor
11057 position. */
11058 if (pt_row)
11059 {
11060 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
11061 w->current_matrix);
11062 w->cursor.y -= first_reusable_row->y;
11063 }
11064
11065 /* Scroll the display. */
11066 run.current_y = first_reusable_row->y;
11067 run.desired_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
11068 run.height = it.last_visible_y - run.current_y;
11069 dy = run.current_y - run.desired_y;
11070
11071 if (run.height)
11072 {
11073 struct frame *f = XFRAME (WINDOW_FRAME (w));
11074 update_begin (f);
11075 rif->update_window_begin_hook (w);
11076 rif->clear_mouse_face (w);
11077 rif->scroll_run_hook (w, &run);
11078 rif->update_window_end_hook (w, 0, 0);
11079 update_end (f);
11080 }
11081
11082 /* Adjust Y positions of reused rows. */
11083 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
11084 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
11085 max_y = it.last_visible_y;
11086 for (row = first_reusable_row; row < first_row_to_display; ++row)
11087 {
11088 row->y -= dy;
11089 row->visible_height = row->height;
11090 if (row->y < min_y)
11091 row->visible_height -= min_y - row->y;
11092 if (row->y + row->height > max_y)
11093 row->visible_height -= row->y + row->height - max_y;
11094 }
11095
11096 /* Scroll the current matrix. */
11097 xassert (nrows_scrolled > 0);
11098 rotate_matrix (w->current_matrix,
11099 start_vpos,
11100 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
11101 -nrows_scrolled);
11102
11103 /* Disable rows not reused. */
11104 for (row -= nrows_scrolled; row < bottom_row; ++row)
11105 row->enabled_p = 0;
11106
11107 /* Adjust window end. A null value of last_text_row means that
11108 the window end is in reused rows which in turn means that
11109 only its vpos can have changed. */
11110 if (last_text_row)
11111 {
11112 w->window_end_bytepos
11113 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
11114 w->window_end_pos
11115 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
11116 w->window_end_vpos
11117 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
11118 }
11119 else
11120 {
11121 w->window_end_vpos
11122 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
11123 }
11124
11125 w->window_end_valid = Qnil;
11126 w->desired_matrix->no_scrolling_p = 1;
11127
11128 #if GLYPH_DEBUG
11129 debug_method_add (w, "try_window_reusing_current_matrix 2");
11130 #endif
11131 return 1;
11132 }
11133
11134 return 0;
11135 }
11136
11137
11138 \f
11139 /************************************************************************
11140 Window redisplay reusing current matrix when buffer has changed
11141 ************************************************************************/
11142
11143 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
11144 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
11145 int *, int *));
11146 static struct glyph_row *
11147 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
11148 struct glyph_row *));
11149
11150
11151 /* Return the last row in MATRIX displaying text. If row START is
11152 non-null, start searching with that row. IT gives the dimensions
11153 of the display. Value is null if matrix is empty; otherwise it is
11154 a pointer to the row found. */
11155
11156 static struct glyph_row *
11157 find_last_row_displaying_text (matrix, it, start)
11158 struct glyph_matrix *matrix;
11159 struct it *it;
11160 struct glyph_row *start;
11161 {
11162 struct glyph_row *row, *row_found;
11163
11164 /* Set row_found to the last row in IT->w's current matrix
11165 displaying text. The loop looks funny but think of partially
11166 visible lines. */
11167 row_found = NULL;
11168 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
11169 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
11170 {
11171 xassert (row->enabled_p);
11172 row_found = row;
11173 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
11174 break;
11175 ++row;
11176 }
11177
11178 return row_found;
11179 }
11180
11181
11182 /* Return the last row in the current matrix of W that is not affected
11183 by changes at the start of current_buffer that occurred since W's
11184 current matrix was built. Value is null if no such row exists.
11185
11186 BEG_UNCHANGED us the number of characters unchanged at the start of
11187 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
11188 first changed character in current_buffer. Characters at positions <
11189 BEG + BEG_UNCHANGED are at the same buffer positions as they were
11190 when the current matrix was built. */
11191
11192 static struct glyph_row *
11193 find_last_unchanged_at_beg_row (w)
11194 struct window *w;
11195 {
11196 int first_changed_pos = BEG + BEG_UNCHANGED;
11197 struct glyph_row *row;
11198 struct glyph_row *row_found = NULL;
11199 int yb = window_text_bottom_y (w);
11200
11201 /* Find the last row displaying unchanged text. */
11202 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11203 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
11204 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
11205 {
11206 if (/* If row ends before first_changed_pos, it is unchanged,
11207 except in some case. */
11208 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
11209 /* When row ends in ZV and we write at ZV it is not
11210 unchanged. */
11211 && !row->ends_at_zv_p
11212 /* When first_changed_pos is the end of a continued line,
11213 row is not unchanged because it may be no longer
11214 continued. */
11215 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
11216 && row->continued_p))
11217 row_found = row;
11218
11219 /* Stop if last visible row. */
11220 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
11221 break;
11222
11223 ++row;
11224 }
11225
11226 return row_found;
11227 }
11228
11229
11230 /* Find the first glyph row in the current matrix of W that is not
11231 affected by changes at the end of current_buffer since the
11232 time W's current matrix was built.
11233
11234 Return in *DELTA the number of chars by which buffer positions in
11235 unchanged text at the end of current_buffer must be adjusted.
11236
11237 Return in *DELTA_BYTES the corresponding number of bytes.
11238
11239 Value is null if no such row exists, i.e. all rows are affected by
11240 changes. */
11241
11242 static struct glyph_row *
11243 find_first_unchanged_at_end_row (w, delta, delta_bytes)
11244 struct window *w;
11245 int *delta, *delta_bytes;
11246 {
11247 struct glyph_row *row;
11248 struct glyph_row *row_found = NULL;
11249
11250 *delta = *delta_bytes = 0;
11251
11252 /* Display must not have been paused, otherwise the current matrix
11253 is not up to date. */
11254 if (NILP (w->window_end_valid))
11255 abort ();
11256
11257 /* A value of window_end_pos >= END_UNCHANGED means that the window
11258 end is in the range of changed text. If so, there is no
11259 unchanged row at the end of W's current matrix. */
11260 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
11261 return NULL;
11262
11263 /* Set row to the last row in W's current matrix displaying text. */
11264 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
11265
11266 /* If matrix is entirely empty, no unchanged row exists. */
11267 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
11268 {
11269 /* The value of row is the last glyph row in the matrix having a
11270 meaningful buffer position in it. The end position of row
11271 corresponds to window_end_pos. This allows us to translate
11272 buffer positions in the current matrix to current buffer
11273 positions for characters not in changed text. */
11274 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
11275 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
11276 int last_unchanged_pos, last_unchanged_pos_old;
11277 struct glyph_row *first_text_row
11278 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11279
11280 *delta = Z - Z_old;
11281 *delta_bytes = Z_BYTE - Z_BYTE_old;
11282
11283 /* Set last_unchanged_pos to the buffer position of the last
11284 character in the buffer that has not been changed. Z is the
11285 index + 1 of the last character in current_buffer, i.e. by
11286 subtracting END_UNCHANGED we get the index of the last
11287 unchanged character, and we have to add BEG to get its buffer
11288 position. */
11289 last_unchanged_pos = Z - END_UNCHANGED + BEG;
11290 last_unchanged_pos_old = last_unchanged_pos - *delta;
11291
11292 /* Search backward from ROW for a row displaying a line that
11293 starts at a minimum position >= last_unchanged_pos_old. */
11294 for (; row > first_text_row; --row)
11295 {
11296 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
11297 abort ();
11298
11299 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
11300 row_found = row;
11301 }
11302 }
11303
11304 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
11305 abort ();
11306
11307 return row_found;
11308 }
11309
11310
11311 /* Make sure that glyph rows in the current matrix of window W
11312 reference the same glyph memory as corresponding rows in the
11313 frame's frame matrix. This function is called after scrolling W's
11314 current matrix on a terminal frame in try_window_id and
11315 try_window_reusing_current_matrix. */
11316
11317 static void
11318 sync_frame_with_window_matrix_rows (w)
11319 struct window *w;
11320 {
11321 struct frame *f = XFRAME (w->frame);
11322 struct glyph_row *window_row, *window_row_end, *frame_row;
11323
11324 /* Preconditions: W must be a leaf window and full-width. Its frame
11325 must have a frame matrix. */
11326 xassert (NILP (w->hchild) && NILP (w->vchild));
11327 xassert (WINDOW_FULL_WIDTH_P (w));
11328 xassert (!FRAME_WINDOW_P (f));
11329
11330 /* If W is a full-width window, glyph pointers in W's current matrix
11331 have, by definition, to be the same as glyph pointers in the
11332 corresponding frame matrix. */
11333 window_row = w->current_matrix->rows;
11334 window_row_end = window_row + w->current_matrix->nrows;
11335 frame_row = f->current_matrix->rows + XFASTINT (w->top);
11336 while (window_row < window_row_end)
11337 {
11338 int area;
11339
11340 for (area = LEFT_MARGIN_AREA; area <= LAST_AREA; ++area)
11341 frame_row->glyphs[area] = window_row->glyphs[area];
11342
11343 /* Disable frame rows whose corresponding window rows have
11344 been disabled in try_window_id. */
11345 if (!window_row->enabled_p)
11346 frame_row->enabled_p = 0;
11347
11348 ++window_row, ++frame_row;
11349 }
11350 }
11351
11352
11353 /* Find the glyph row in window W containing CHARPOS. Consider all
11354 rows between START and END (not inclusive). END null means search
11355 all rows to the end of the display area of W. Value is the row
11356 containing CHARPOS or null. */
11357
11358 struct glyph_row *
11359 row_containing_pos (w, charpos, start, end, dy)
11360 struct window *w;
11361 int charpos;
11362 struct glyph_row *start, *end;
11363 int dy;
11364 {
11365 struct glyph_row *row = start;
11366 int last_y;
11367
11368 /* If we happen to start on a header-line, skip that. */
11369 if (row->mode_line_p)
11370 ++row;
11371
11372 if ((end && row >= end) || !row->enabled_p)
11373 return NULL;
11374
11375 last_y = window_text_bottom_y (w) - dy;
11376
11377 while ((end == NULL || row < end)
11378 && MATRIX_ROW_BOTTOM_Y (row) < last_y
11379 && (MATRIX_ROW_END_CHARPOS (row) < charpos
11380 || (MATRIX_ROW_END_CHARPOS (row) == charpos
11381 /* The end position of a row equals the start
11382 position of the next row. If CHARPOS is there, we
11383 would rather display it in the next line, except
11384 when this line ends in ZV. */
11385 && !row->ends_at_zv_p
11386 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))
11387 ++row;
11388
11389 /* Give up if CHARPOS not found. */
11390 if ((end && row >= end)
11391 || charpos < MATRIX_ROW_START_CHARPOS (row)
11392 || charpos > MATRIX_ROW_END_CHARPOS (row))
11393 row = NULL;
11394
11395 return row;
11396 }
11397
11398
11399 /* Try to redisplay window W by reusing its existing display. W's
11400 current matrix must be up to date when this function is called,
11401 i.e. window_end_valid must not be nil.
11402
11403 Value is
11404
11405 1 if display has been updated
11406 0 if otherwise unsuccessful
11407 -1 if redisplay with same window start is known not to succeed
11408
11409 The following steps are performed:
11410
11411 1. Find the last row in the current matrix of W that is not
11412 affected by changes at the start of current_buffer. If no such row
11413 is found, give up.
11414
11415 2. Find the first row in W's current matrix that is not affected by
11416 changes at the end of current_buffer. Maybe there is no such row.
11417
11418 3. Display lines beginning with the row + 1 found in step 1 to the
11419 row found in step 2 or, if step 2 didn't find a row, to the end of
11420 the window.
11421
11422 4. If cursor is not known to appear on the window, give up.
11423
11424 5. If display stopped at the row found in step 2, scroll the
11425 display and current matrix as needed.
11426
11427 6. Maybe display some lines at the end of W, if we must. This can
11428 happen under various circumstances, like a partially visible line
11429 becoming fully visible, or because newly displayed lines are displayed
11430 in smaller font sizes.
11431
11432 7. Update W's window end information. */
11433
11434 static int
11435 try_window_id (w)
11436 struct window *w;
11437 {
11438 struct frame *f = XFRAME (w->frame);
11439 struct glyph_matrix *current_matrix = w->current_matrix;
11440 struct glyph_matrix *desired_matrix = w->desired_matrix;
11441 struct glyph_row *last_unchanged_at_beg_row;
11442 struct glyph_row *first_unchanged_at_end_row;
11443 struct glyph_row *row;
11444 struct glyph_row *bottom_row;
11445 int bottom_vpos;
11446 struct it it;
11447 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
11448 struct text_pos start_pos;
11449 struct run run;
11450 int first_unchanged_at_end_vpos = 0;
11451 struct glyph_row *last_text_row, *last_text_row_at_end;
11452 struct text_pos start;
11453 int first_changed_charpos, last_changed_charpos;
11454
11455 #if GLYPH_DEBUG
11456 if (inhibit_try_window_id)
11457 return 0;
11458 #endif
11459
11460 /* This is handy for debugging. */
11461 #if 0
11462 #define GIVE_UP(X) \
11463 do { \
11464 fprintf (stderr, "try_window_id give up %d\n", (X)); \
11465 return 0; \
11466 } while (0)
11467 #else
11468 #define GIVE_UP(X) return 0
11469 #endif
11470
11471 SET_TEXT_POS_FROM_MARKER (start, w->start);
11472
11473 /* Don't use this for mini-windows because these can show
11474 messages and mini-buffers, and we don't handle that here. */
11475 if (MINI_WINDOW_P (w))
11476 GIVE_UP (1);
11477
11478 /* This flag is used to prevent redisplay optimizations. */
11479 if (windows_or_buffers_changed)
11480 GIVE_UP (2);
11481
11482 /* Verify that narrowing has not changed. This flag is also set to prevent
11483 redisplay optimizations. It would be nice to further
11484 reduce the number of cases where this prevents try_window_id. */
11485 if (current_buffer->clip_changed)
11486 GIVE_UP (3);
11487
11488 /* Window must either use window-based redisplay or be full width. */
11489 if (!FRAME_WINDOW_P (f)
11490 && (!line_ins_del_ok
11491 || !WINDOW_FULL_WIDTH_P (w)))
11492 GIVE_UP (4);
11493
11494 /* Give up if point is not known NOT to appear in W. */
11495 if (PT < CHARPOS (start))
11496 GIVE_UP (5);
11497
11498 /* Another way to prevent redisplay optimizations. */
11499 if (XFASTINT (w->last_modified) == 0)
11500 GIVE_UP (6);
11501
11502 /* Verify that window is not hscrolled. */
11503 if (XFASTINT (w->hscroll) != 0)
11504 GIVE_UP (7);
11505
11506 /* Verify that display wasn't paused. */
11507 if (NILP (w->window_end_valid))
11508 GIVE_UP (8);
11509
11510 /* Can't use this if highlighting a region because a cursor movement
11511 will do more than just set the cursor. */
11512 if (!NILP (Vtransient_mark_mode)
11513 && !NILP (current_buffer->mark_active))
11514 GIVE_UP (9);
11515
11516 /* Likewise if highlighting trailing whitespace. */
11517 if (!NILP (Vshow_trailing_whitespace))
11518 GIVE_UP (11);
11519
11520 /* Likewise if showing a region. */
11521 if (!NILP (w->region_showing))
11522 GIVE_UP (10);
11523
11524 /* Can use this if overlay arrow position and or string have changed. */
11525 if (!EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
11526 || !EQ (last_arrow_string, Voverlay_arrow_string))
11527 GIVE_UP (12);
11528
11529
11530 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
11531 only if buffer has really changed. The reason is that the gap is
11532 initially at Z for freshly visited files. The code below would
11533 set end_unchanged to 0 in that case. */
11534 if (MODIFF > SAVE_MODIFF
11535 /* This seems to happen sometimes after saving a buffer. */
11536 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
11537 {
11538 if (GPT - BEG < BEG_UNCHANGED)
11539 BEG_UNCHANGED = GPT - BEG;
11540 if (Z - GPT < END_UNCHANGED)
11541 END_UNCHANGED = Z - GPT;
11542 }
11543
11544 /* The position of the first and last character that has been changed. */
11545 first_changed_charpos = BEG + BEG_UNCHANGED;
11546 last_changed_charpos = Z - END_UNCHANGED;
11547
11548 /* If window starts after a line end, and the last change is in
11549 front of that newline, then changes don't affect the display.
11550 This case happens with stealth-fontification. Note that although
11551 the display is unchanged, glyph positions in the matrix have to
11552 be adjusted, of course. */
11553 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
11554 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
11555 && ((last_changed_charpos < CHARPOS (start)
11556 && CHARPOS (start) == BEGV)
11557 || (last_changed_charpos < CHARPOS (start) - 1
11558 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
11559 {
11560 int Z_old, delta, Z_BYTE_old, delta_bytes;
11561 struct glyph_row *r0;
11562
11563 /* Compute how many chars/bytes have been added to or removed
11564 from the buffer. */
11565 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
11566 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
11567 delta = Z - Z_old;
11568 delta_bytes = Z_BYTE - Z_BYTE_old;
11569
11570 /* Give up if PT is not in the window. Note that it already has
11571 been checked at the start of try_window_id that PT is not in
11572 front of the window start. */
11573 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
11574 GIVE_UP (13);
11575
11576 /* If window start is unchanged, we can reuse the whole matrix
11577 as is, after adjusting glyph positions. No need to compute
11578 the window end again, since its offset from Z hasn't changed. */
11579 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
11580 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
11581 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes)
11582 {
11583 /* Adjust positions in the glyph matrix. */
11584 if (delta || delta_bytes)
11585 {
11586 struct glyph_row *r1
11587 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11588 increment_matrix_positions (w->current_matrix,
11589 MATRIX_ROW_VPOS (r0, current_matrix),
11590 MATRIX_ROW_VPOS (r1, current_matrix),
11591 delta, delta_bytes);
11592 }
11593
11594 /* Set the cursor. */
11595 row = row_containing_pos (w, PT, r0, NULL, 0);
11596 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
11597 return 1;
11598 }
11599 }
11600
11601 /* Handle the case that changes are all below what is displayed in
11602 the window, and that PT is in the window. This shortcut cannot
11603 be taken if ZV is visible in the window, and text has been added
11604 there that is visible in the window. */
11605 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
11606 /* ZV is not visible in the window, or there are no
11607 changes at ZV, actually. */
11608 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
11609 || first_changed_charpos == last_changed_charpos))
11610 {
11611 struct glyph_row *r0;
11612
11613 /* Give up if PT is not in the window. Note that it already has
11614 been checked at the start of try_window_id that PT is not in
11615 front of the window start. */
11616 if (PT >= MATRIX_ROW_END_CHARPOS (row))
11617 GIVE_UP (14);
11618
11619 /* If window start is unchanged, we can reuse the whole matrix
11620 as is, without changing glyph positions since no text has
11621 been added/removed in front of the window end. */
11622 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
11623 if (TEXT_POS_EQUAL_P (start, r0->start.pos))
11624 {
11625 /* We have to compute the window end anew since text
11626 can have been added/removed after it. */
11627 w->window_end_pos
11628 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
11629 w->window_end_bytepos
11630 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11631
11632 /* Set the cursor. */
11633 row = row_containing_pos (w, PT, r0, NULL, 0);
11634 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
11635 return 2;
11636 }
11637 }
11638
11639 /* Give up if window start is in the changed area.
11640
11641 The condition used to read
11642
11643 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
11644
11645 but why that was tested escapes me at the moment. */
11646 if (CHARPOS (start) >= first_changed_charpos
11647 && CHARPOS (start) <= last_changed_charpos)
11648 GIVE_UP (15);
11649
11650 /* Check that window start agrees with the start of the first glyph
11651 row in its current matrix. Check this after we know the window
11652 start is not in changed text, otherwise positions would not be
11653 comparable. */
11654 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
11655 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
11656 GIVE_UP (16);
11657
11658 /* Give up if the window ends in strings. Overlay strings
11659 at the end are difficult to handle, so don't try. */
11660 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
11661 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
11662 GIVE_UP (20);
11663
11664 /* Compute the position at which we have to start displaying new
11665 lines. Some of the lines at the top of the window might be
11666 reusable because they are not displaying changed text. Find the
11667 last row in W's current matrix not affected by changes at the
11668 start of current_buffer. Value is null if changes start in the
11669 first line of window. */
11670 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
11671 if (last_unchanged_at_beg_row)
11672 {
11673 /* Avoid starting to display in the moddle of a character, a TAB
11674 for instance. This is easier than to set up the iterator
11675 exactly, and it's not a frequent case, so the additional
11676 effort wouldn't really pay off. */
11677 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
11678 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
11679 && last_unchanged_at_beg_row > w->current_matrix->rows)
11680 --last_unchanged_at_beg_row;
11681
11682 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
11683 GIVE_UP (17);
11684
11685 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
11686 GIVE_UP (18);
11687 start_pos = it.current.pos;
11688
11689 /* Start displaying new lines in the desired matrix at the same
11690 vpos we would use in the current matrix, i.e. below
11691 last_unchanged_at_beg_row. */
11692 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
11693 current_matrix);
11694 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11695 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
11696
11697 xassert (it.hpos == 0 && it.current_x == 0);
11698 }
11699 else
11700 {
11701 /* There are no reusable lines at the start of the window.
11702 Start displaying in the first line. */
11703 start_display (&it, w, start);
11704 start_pos = it.current.pos;
11705 }
11706
11707 /* Find the first row that is not affected by changes at the end of
11708 the buffer. Value will be null if there is no unchanged row, in
11709 which case we must redisplay to the end of the window. delta
11710 will be set to the value by which buffer positions beginning with
11711 first_unchanged_at_end_row have to be adjusted due to text
11712 changes. */
11713 first_unchanged_at_end_row
11714 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
11715 IF_DEBUG (debug_delta = delta);
11716 IF_DEBUG (debug_delta_bytes = delta_bytes);
11717
11718 /* Set stop_pos to the buffer position up to which we will have to
11719 display new lines. If first_unchanged_at_end_row != NULL, this
11720 is the buffer position of the start of the line displayed in that
11721 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
11722 that we don't stop at a buffer position. */
11723 stop_pos = 0;
11724 if (first_unchanged_at_end_row)
11725 {
11726 xassert (last_unchanged_at_beg_row == NULL
11727 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
11728
11729 /* If this is a continuation line, move forward to the next one
11730 that isn't. Changes in lines above affect this line.
11731 Caution: this may move first_unchanged_at_end_row to a row
11732 not displaying text. */
11733 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
11734 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11735 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11736 < it.last_visible_y))
11737 ++first_unchanged_at_end_row;
11738
11739 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11740 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11741 >= it.last_visible_y))
11742 first_unchanged_at_end_row = NULL;
11743 else
11744 {
11745 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
11746 + delta);
11747 first_unchanged_at_end_vpos
11748 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
11749 xassert (stop_pos >= Z - END_UNCHANGED);
11750 }
11751 }
11752 else if (last_unchanged_at_beg_row == NULL)
11753 GIVE_UP (19);
11754
11755
11756 #if GLYPH_DEBUG
11757
11758 /* Either there is no unchanged row at the end, or the one we have
11759 now displays text. This is a necessary condition for the window
11760 end pos calculation at the end of this function. */
11761 xassert (first_unchanged_at_end_row == NULL
11762 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
11763
11764 debug_last_unchanged_at_beg_vpos
11765 = (last_unchanged_at_beg_row
11766 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
11767 : -1);
11768 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
11769
11770 #endif /* GLYPH_DEBUG != 0 */
11771
11772
11773 /* Display new lines. Set last_text_row to the last new line
11774 displayed which has text on it, i.e. might end up as being the
11775 line where the window_end_vpos is. */
11776 w->cursor.vpos = -1;
11777 last_text_row = NULL;
11778 overlay_arrow_seen = 0;
11779 while (it.current_y < it.last_visible_y
11780 && !fonts_changed_p
11781 && (first_unchanged_at_end_row == NULL
11782 || IT_CHARPOS (it) < stop_pos))
11783 {
11784 if (display_line (&it))
11785 last_text_row = it.glyph_row - 1;
11786 }
11787
11788 if (fonts_changed_p)
11789 return -1;
11790
11791
11792 /* Compute differences in buffer positions, y-positions etc. for
11793 lines reused at the bottom of the window. Compute what we can
11794 scroll. */
11795 if (first_unchanged_at_end_row
11796 /* No lines reused because we displayed everything up to the
11797 bottom of the window. */
11798 && it.current_y < it.last_visible_y)
11799 {
11800 dvpos = (it.vpos
11801 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
11802 current_matrix));
11803 dy = it.current_y - first_unchanged_at_end_row->y;
11804 run.current_y = first_unchanged_at_end_row->y;
11805 run.desired_y = run.current_y + dy;
11806 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
11807 }
11808 else
11809 {
11810 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
11811 first_unchanged_at_end_row = NULL;
11812 }
11813 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
11814
11815
11816 /* Find the cursor if not already found. We have to decide whether
11817 PT will appear on this window (it sometimes doesn't, but this is
11818 not a very frequent case.) This decision has to be made before
11819 the current matrix is altered. A value of cursor.vpos < 0 means
11820 that PT is either in one of the lines beginning at
11821 first_unchanged_at_end_row or below the window. Don't care for
11822 lines that might be displayed later at the window end; as
11823 mentioned, this is not a frequent case. */
11824 if (w->cursor.vpos < 0)
11825 {
11826 /* Cursor in unchanged rows at the top? */
11827 if (PT < CHARPOS (start_pos)
11828 && last_unchanged_at_beg_row)
11829 {
11830 row = row_containing_pos (w, PT,
11831 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
11832 last_unchanged_at_beg_row + 1, 0);
11833 if (row)
11834 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11835 }
11836
11837 /* Start from first_unchanged_at_end_row looking for PT. */
11838 else if (first_unchanged_at_end_row)
11839 {
11840 row = row_containing_pos (w, PT - delta,
11841 first_unchanged_at_end_row, NULL, 0);
11842 if (row)
11843 set_cursor_from_row (w, row, w->current_matrix, delta,
11844 delta_bytes, dy, dvpos);
11845 }
11846
11847 /* Give up if cursor was not found. */
11848 if (w->cursor.vpos < 0)
11849 {
11850 clear_glyph_matrix (w->desired_matrix);
11851 return -1;
11852 }
11853 }
11854
11855 /* Don't let the cursor end in the scroll margins. */
11856 {
11857 int this_scroll_margin, cursor_height;
11858
11859 this_scroll_margin = max (0, scroll_margin);
11860 this_scroll_margin = min (this_scroll_margin,
11861 XFASTINT (w->height) / 4);
11862 this_scroll_margin *= CANON_Y_UNIT (it.f);
11863 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
11864
11865 if ((w->cursor.y < this_scroll_margin
11866 && CHARPOS (start) > BEGV)
11867 /* Don't take scroll margin into account at the bottom because
11868 old redisplay didn't do it either. */
11869 || w->cursor.y + cursor_height > it.last_visible_y)
11870 {
11871 w->cursor.vpos = -1;
11872 clear_glyph_matrix (w->desired_matrix);
11873 return -1;
11874 }
11875 }
11876
11877 /* Scroll the display. Do it before changing the current matrix so
11878 that xterm.c doesn't get confused about where the cursor glyph is
11879 found. */
11880 if (dy && run.height)
11881 {
11882 update_begin (f);
11883
11884 if (FRAME_WINDOW_P (f))
11885 {
11886 rif->update_window_begin_hook (w);
11887 rif->clear_mouse_face (w);
11888 rif->scroll_run_hook (w, &run);
11889 rif->update_window_end_hook (w, 0, 0);
11890 }
11891 else
11892 {
11893 /* Terminal frame. In this case, dvpos gives the number of
11894 lines to scroll by; dvpos < 0 means scroll up. */
11895 int first_unchanged_at_end_vpos
11896 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
11897 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
11898 int end = (XFASTINT (w->top)
11899 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
11900 + window_internal_height (w));
11901
11902 /* Perform the operation on the screen. */
11903 if (dvpos > 0)
11904 {
11905 /* Scroll last_unchanged_at_beg_row to the end of the
11906 window down dvpos lines. */
11907 set_terminal_window (end);
11908
11909 /* On dumb terminals delete dvpos lines at the end
11910 before inserting dvpos empty lines. */
11911 if (!scroll_region_ok)
11912 ins_del_lines (end - dvpos, -dvpos);
11913
11914 /* Insert dvpos empty lines in front of
11915 last_unchanged_at_beg_row. */
11916 ins_del_lines (from, dvpos);
11917 }
11918 else if (dvpos < 0)
11919 {
11920 /* Scroll up last_unchanged_at_beg_vpos to the end of
11921 the window to last_unchanged_at_beg_vpos - |dvpos|. */
11922 set_terminal_window (end);
11923
11924 /* Delete dvpos lines in front of
11925 last_unchanged_at_beg_vpos. ins_del_lines will set
11926 the cursor to the given vpos and emit |dvpos| delete
11927 line sequences. */
11928 ins_del_lines (from + dvpos, dvpos);
11929
11930 /* On a dumb terminal insert dvpos empty lines at the
11931 end. */
11932 if (!scroll_region_ok)
11933 ins_del_lines (end + dvpos, -dvpos);
11934 }
11935
11936 set_terminal_window (0);
11937 }
11938
11939 update_end (f);
11940 }
11941
11942 /* Shift reused rows of the current matrix to the right position.
11943 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
11944 text. */
11945 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11946 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
11947 if (dvpos < 0)
11948 {
11949 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
11950 bottom_vpos, dvpos);
11951 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
11952 bottom_vpos, 0);
11953 }
11954 else if (dvpos > 0)
11955 {
11956 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
11957 bottom_vpos, dvpos);
11958 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
11959 first_unchanged_at_end_vpos + dvpos, 0);
11960 }
11961
11962 /* For frame-based redisplay, make sure that current frame and window
11963 matrix are in sync with respect to glyph memory. */
11964 if (!FRAME_WINDOW_P (f))
11965 sync_frame_with_window_matrix_rows (w);
11966
11967 /* Adjust buffer positions in reused rows. */
11968 if (delta)
11969 increment_matrix_positions (current_matrix,
11970 first_unchanged_at_end_vpos + dvpos,
11971 bottom_vpos, delta, delta_bytes);
11972
11973 /* Adjust Y positions. */
11974 if (dy)
11975 shift_glyph_matrix (w, current_matrix,
11976 first_unchanged_at_end_vpos + dvpos,
11977 bottom_vpos, dy);
11978
11979 if (first_unchanged_at_end_row)
11980 first_unchanged_at_end_row += dvpos;
11981
11982 /* If scrolling up, there may be some lines to display at the end of
11983 the window. */
11984 last_text_row_at_end = NULL;
11985 if (dy < 0)
11986 {
11987 /* Scrolling up can leave for example a partially visible line
11988 at the end of the window to be redisplayed. */
11989 /* Set last_row to the glyph row in the current matrix where the
11990 window end line is found. It has been moved up or down in
11991 the matrix by dvpos. */
11992 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
11993 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
11994
11995 /* If last_row is the window end line, it should display text. */
11996 xassert (last_row->displays_text_p);
11997
11998 /* If window end line was partially visible before, begin
11999 displaying at that line. Otherwise begin displaying with the
12000 line following it. */
12001 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
12002 {
12003 init_to_row_start (&it, w, last_row);
12004 it.vpos = last_vpos;
12005 it.current_y = last_row->y;
12006 }
12007 else
12008 {
12009 init_to_row_end (&it, w, last_row);
12010 it.vpos = 1 + last_vpos;
12011 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
12012 ++last_row;
12013 }
12014
12015 /* We may start in a continuation line. If so, we have to
12016 get the right continuation_lines_width and current_x. */
12017 it.continuation_lines_width = last_row->continuation_lines_width;
12018 it.hpos = it.current_x = 0;
12019
12020 /* Display the rest of the lines at the window end. */
12021 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
12022 while (it.current_y < it.last_visible_y
12023 && !fonts_changed_p)
12024 {
12025 /* Is it always sure that the display agrees with lines in
12026 the current matrix? I don't think so, so we mark rows
12027 displayed invalid in the current matrix by setting their
12028 enabled_p flag to zero. */
12029 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
12030 if (display_line (&it))
12031 last_text_row_at_end = it.glyph_row - 1;
12032 }
12033 }
12034
12035 /* Update window_end_pos and window_end_vpos. */
12036 if (first_unchanged_at_end_row
12037 && first_unchanged_at_end_row->y < it.last_visible_y
12038 && !last_text_row_at_end)
12039 {
12040 /* Window end line if one of the preserved rows from the current
12041 matrix. Set row to the last row displaying text in current
12042 matrix starting at first_unchanged_at_end_row, after
12043 scrolling. */
12044 xassert (first_unchanged_at_end_row->displays_text_p);
12045 row = find_last_row_displaying_text (w->current_matrix, &it,
12046 first_unchanged_at_end_row);
12047 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
12048
12049 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
12050 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
12051 w->window_end_vpos
12052 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
12053 xassert (w->window_end_bytepos >= 0);
12054 IF_DEBUG (debug_method_add (w, "A"));
12055 }
12056 else if (last_text_row_at_end)
12057 {
12058 w->window_end_pos
12059 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
12060 w->window_end_bytepos
12061 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
12062 w->window_end_vpos
12063 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
12064 xassert (w->window_end_bytepos >= 0);
12065 IF_DEBUG (debug_method_add (w, "B"));
12066 }
12067 else if (last_text_row)
12068 {
12069 /* We have displayed either to the end of the window or at the
12070 end of the window, i.e. the last row with text is to be found
12071 in the desired matrix. */
12072 w->window_end_pos
12073 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12074 w->window_end_bytepos
12075 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12076 w->window_end_vpos
12077 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
12078 xassert (w->window_end_bytepos >= 0);
12079 }
12080 else if (first_unchanged_at_end_row == NULL
12081 && last_text_row == NULL
12082 && last_text_row_at_end == NULL)
12083 {
12084 /* Displayed to end of window, but no line containing text was
12085 displayed. Lines were deleted at the end of the window. */
12086 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
12087 int vpos = XFASTINT (w->window_end_vpos);
12088 struct glyph_row *current_row = current_matrix->rows + vpos;
12089 struct glyph_row *desired_row = desired_matrix->rows + vpos;
12090
12091 for (row = NULL;
12092 row == NULL && vpos >= first_vpos;
12093 --vpos, --current_row, --desired_row)
12094 {
12095 if (desired_row->enabled_p)
12096 {
12097 if (desired_row->displays_text_p)
12098 row = desired_row;
12099 }
12100 else if (current_row->displays_text_p)
12101 row = current_row;
12102 }
12103
12104 xassert (row != NULL);
12105 w->window_end_vpos = make_number (vpos + 1);
12106 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
12107 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
12108 xassert (w->window_end_bytepos >= 0);
12109 IF_DEBUG (debug_method_add (w, "C"));
12110 }
12111 else
12112 abort ();
12113
12114 #if 0 /* This leads to problems, for instance when the cursor is
12115 at ZV, and the cursor line displays no text. */
12116 /* Disable rows below what's displayed in the window. This makes
12117 debugging easier. */
12118 enable_glyph_matrix_rows (current_matrix,
12119 XFASTINT (w->window_end_vpos) + 1,
12120 bottom_vpos, 0);
12121 #endif
12122
12123 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
12124 debug_end_vpos = XFASTINT (w->window_end_vpos));
12125
12126 /* Record that display has not been completed. */
12127 w->window_end_valid = Qnil;
12128 w->desired_matrix->no_scrolling_p = 1;
12129 return 3;
12130
12131 #undef GIVE_UP
12132 }
12133
12134
12135 \f
12136 /***********************************************************************
12137 More debugging support
12138 ***********************************************************************/
12139
12140 #if GLYPH_DEBUG
12141
12142 void dump_glyph_row P_ ((struct glyph_row *, int, int));
12143 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
12144 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
12145
12146
12147 /* Dump the contents of glyph matrix MATRIX on stderr.
12148
12149 GLYPHS 0 means don't show glyph contents.
12150 GLYPHS 1 means show glyphs in short form
12151 GLYPHS > 1 means show glyphs in long form. */
12152
12153 void
12154 dump_glyph_matrix (matrix, glyphs)
12155 struct glyph_matrix *matrix;
12156 int glyphs;
12157 {
12158 int i;
12159 for (i = 0; i < matrix->nrows; ++i)
12160 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
12161 }
12162
12163
12164 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
12165 the glyph row and area where the glyph comes from. */
12166
12167 void
12168 dump_glyph (row, glyph, area)
12169 struct glyph_row *row;
12170 struct glyph *glyph;
12171 int area;
12172 {
12173 if (glyph->type == CHAR_GLYPH)
12174 {
12175 fprintf (stderr,
12176 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12177 glyph - row->glyphs[TEXT_AREA],
12178 'C',
12179 glyph->charpos,
12180 (BUFFERP (glyph->object)
12181 ? 'B'
12182 : (STRINGP (glyph->object)
12183 ? 'S'
12184 : '-')),
12185 glyph->pixel_width,
12186 glyph->u.ch,
12187 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
12188 ? glyph->u.ch
12189 : '.'),
12190 glyph->face_id,
12191 glyph->left_box_line_p,
12192 glyph->right_box_line_p);
12193 }
12194 else if (glyph->type == STRETCH_GLYPH)
12195 {
12196 fprintf (stderr,
12197 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12198 glyph - row->glyphs[TEXT_AREA],
12199 'S',
12200 glyph->charpos,
12201 (BUFFERP (glyph->object)
12202 ? 'B'
12203 : (STRINGP (glyph->object)
12204 ? 'S'
12205 : '-')),
12206 glyph->pixel_width,
12207 0,
12208 '.',
12209 glyph->face_id,
12210 glyph->left_box_line_p,
12211 glyph->right_box_line_p);
12212 }
12213 else if (glyph->type == IMAGE_GLYPH)
12214 {
12215 fprintf (stderr,
12216 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12217 glyph - row->glyphs[TEXT_AREA],
12218 'I',
12219 glyph->charpos,
12220 (BUFFERP (glyph->object)
12221 ? 'B'
12222 : (STRINGP (glyph->object)
12223 ? 'S'
12224 : '-')),
12225 glyph->pixel_width,
12226 glyph->u.img_id,
12227 '.',
12228 glyph->face_id,
12229 glyph->left_box_line_p,
12230 glyph->right_box_line_p);
12231 }
12232 }
12233
12234
12235 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
12236 GLYPHS 0 means don't show glyph contents.
12237 GLYPHS 1 means show glyphs in short form
12238 GLYPHS > 1 means show glyphs in long form. */
12239
12240 void
12241 dump_glyph_row (row, vpos, glyphs)
12242 struct glyph_row *row;
12243 int vpos, glyphs;
12244 {
12245 if (glyphs != 1)
12246 {
12247 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
12248 fprintf (stderr, "=======================================================================\n");
12249
12250 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d\
12251 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
12252 vpos,
12253 MATRIX_ROW_START_CHARPOS (row),
12254 MATRIX_ROW_END_CHARPOS (row),
12255 row->used[TEXT_AREA],
12256 row->contains_overlapping_glyphs_p,
12257 row->enabled_p,
12258 row->truncated_on_left_p,
12259 row->truncated_on_right_p,
12260 row->overlay_arrow_p,
12261 row->continued_p,
12262 MATRIX_ROW_CONTINUATION_LINE_P (row),
12263 row->displays_text_p,
12264 row->ends_at_zv_p,
12265 row->fill_line_p,
12266 row->ends_in_middle_of_char_p,
12267 row->starts_in_middle_of_char_p,
12268 row->mouse_face_p,
12269 row->x,
12270 row->y,
12271 row->pixel_width,
12272 row->height,
12273 row->visible_height,
12274 row->ascent,
12275 row->phys_ascent);
12276 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
12277 row->end.overlay_string_index,
12278 row->continuation_lines_width);
12279 fprintf (stderr, "%9d %5d\n",
12280 CHARPOS (row->start.string_pos),
12281 CHARPOS (row->end.string_pos));
12282 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
12283 row->end.dpvec_index);
12284 }
12285
12286 if (glyphs > 1)
12287 {
12288 int area;
12289
12290 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12291 {
12292 struct glyph *glyph = row->glyphs[area];
12293 struct glyph *glyph_end = glyph + row->used[area];
12294
12295 /* Glyph for a line end in text. */
12296 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
12297 ++glyph_end;
12298
12299 if (glyph < glyph_end)
12300 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
12301
12302 for (; glyph < glyph_end; ++glyph)
12303 dump_glyph (row, glyph, area);
12304 }
12305 }
12306 else if (glyphs == 1)
12307 {
12308 int area;
12309
12310 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12311 {
12312 char *s = (char *) alloca (row->used[area] + 1);
12313 int i;
12314
12315 for (i = 0; i < row->used[area]; ++i)
12316 {
12317 struct glyph *glyph = row->glyphs[area] + i;
12318 if (glyph->type == CHAR_GLYPH
12319 && glyph->u.ch < 0x80
12320 && glyph->u.ch >= ' ')
12321 s[i] = glyph->u.ch;
12322 else
12323 s[i] = '.';
12324 }
12325
12326 s[i] = '\0';
12327 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
12328 }
12329 }
12330 }
12331
12332
12333 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
12334 Sdump_glyph_matrix, 0, 1, "p",
12335 doc: /* Dump the current matrix of the selected window to stderr.
12336 Shows contents of glyph row structures. With non-nil
12337 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
12338 glyphs in short form, otherwise show glyphs in long form. */)
12339 (glyphs)
12340 Lisp_Object glyphs;
12341 {
12342 struct window *w = XWINDOW (selected_window);
12343 struct buffer *buffer = XBUFFER (w->buffer);
12344
12345 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
12346 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
12347 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
12348 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
12349 fprintf (stderr, "=============================================\n");
12350 dump_glyph_matrix (w->current_matrix,
12351 NILP (glyphs) ? 0 : XINT (glyphs));
12352 return Qnil;
12353 }
12354
12355
12356 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
12357 doc: /* Dump glyph row ROW to stderr.
12358 GLYPH 0 means don't dump glyphs.
12359 GLYPH 1 means dump glyphs in short form.
12360 GLYPH > 1 or omitted means dump glyphs in long form. */)
12361 (row, glyphs)
12362 Lisp_Object row, glyphs;
12363 {
12364 struct glyph_matrix *matrix;
12365 int vpos;
12366
12367 CHECK_NUMBER (row);
12368 matrix = XWINDOW (selected_window)->current_matrix;
12369 vpos = XINT (row);
12370 if (vpos >= 0 && vpos < matrix->nrows)
12371 dump_glyph_row (MATRIX_ROW (matrix, vpos),
12372 vpos,
12373 INTEGERP (glyphs) ? XINT (glyphs) : 2);
12374 return Qnil;
12375 }
12376
12377
12378 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
12379 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
12380 GLYPH 0 means don't dump glyphs.
12381 GLYPH 1 means dump glyphs in short form.
12382 GLYPH > 1 or omitted means dump glyphs in long form. */)
12383 (row, glyphs)
12384 Lisp_Object row, glyphs;
12385 {
12386 struct frame *sf = SELECTED_FRAME ();
12387 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
12388 int vpos;
12389
12390 CHECK_NUMBER (row);
12391 vpos = XINT (row);
12392 if (vpos >= 0 && vpos < m->nrows)
12393 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
12394 INTEGERP (glyphs) ? XINT (glyphs) : 2);
12395 return Qnil;
12396 }
12397
12398
12399 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
12400 doc: /* Toggle tracing of redisplay.
12401 With ARG, turn tracing on if and only if ARG is positive. */)
12402 (arg)
12403 Lisp_Object arg;
12404 {
12405 if (NILP (arg))
12406 trace_redisplay_p = !trace_redisplay_p;
12407 else
12408 {
12409 arg = Fprefix_numeric_value (arg);
12410 trace_redisplay_p = XINT (arg) > 0;
12411 }
12412
12413 return Qnil;
12414 }
12415
12416
12417 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
12418 doc: /* Like `format', but print result to stderr. */)
12419 (nargs, args)
12420 int nargs;
12421 Lisp_Object *args;
12422 {
12423 Lisp_Object s = Fformat (nargs, args);
12424 fprintf (stderr, "%s", XSTRING (s)->data);
12425 return Qnil;
12426 }
12427
12428 #endif /* GLYPH_DEBUG */
12429
12430
12431 \f
12432 /***********************************************************************
12433 Building Desired Matrix Rows
12434 ***********************************************************************/
12435
12436 /* Return a temporary glyph row holding the glyphs of an overlay
12437 arrow. Only used for non-window-redisplay windows. */
12438
12439 static struct glyph_row *
12440 get_overlay_arrow_glyph_row (w)
12441 struct window *w;
12442 {
12443 struct frame *f = XFRAME (WINDOW_FRAME (w));
12444 struct buffer *buffer = XBUFFER (w->buffer);
12445 struct buffer *old = current_buffer;
12446 unsigned char *arrow_string = XSTRING (Voverlay_arrow_string)->data;
12447 int arrow_len = XSTRING (Voverlay_arrow_string)->size;
12448 unsigned char *arrow_end = arrow_string + arrow_len;
12449 unsigned char *p;
12450 struct it it;
12451 int multibyte_p;
12452 int n_glyphs_before;
12453
12454 set_buffer_temp (buffer);
12455 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
12456 it.glyph_row->used[TEXT_AREA] = 0;
12457 SET_TEXT_POS (it.position, 0, 0);
12458
12459 multibyte_p = !NILP (buffer->enable_multibyte_characters);
12460 p = arrow_string;
12461 while (p < arrow_end)
12462 {
12463 Lisp_Object face, ilisp;
12464
12465 /* Get the next character. */
12466 if (multibyte_p)
12467 it.c = string_char_and_length (p, arrow_len, &it.len);
12468 else
12469 it.c = *p, it.len = 1;
12470 p += it.len;
12471
12472 /* Get its face. */
12473 ilisp = make_number (p - arrow_string);
12474 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
12475 it.face_id = compute_char_face (f, it.c, face);
12476
12477 /* Compute its width, get its glyphs. */
12478 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
12479 SET_TEXT_POS (it.position, -1, -1);
12480 PRODUCE_GLYPHS (&it);
12481
12482 /* If this character doesn't fit any more in the line, we have
12483 to remove some glyphs. */
12484 if (it.current_x > it.last_visible_x)
12485 {
12486 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
12487 break;
12488 }
12489 }
12490
12491 set_buffer_temp (old);
12492 return it.glyph_row;
12493 }
12494
12495
12496 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
12497 glyphs are only inserted for terminal frames since we can't really
12498 win with truncation glyphs when partially visible glyphs are
12499 involved. Which glyphs to insert is determined by
12500 produce_special_glyphs. */
12501
12502 static void
12503 insert_left_trunc_glyphs (it)
12504 struct it *it;
12505 {
12506 struct it truncate_it;
12507 struct glyph *from, *end, *to, *toend;
12508
12509 xassert (!FRAME_WINDOW_P (it->f));
12510
12511 /* Get the truncation glyphs. */
12512 truncate_it = *it;
12513 truncate_it.current_x = 0;
12514 truncate_it.face_id = DEFAULT_FACE_ID;
12515 truncate_it.glyph_row = &scratch_glyph_row;
12516 truncate_it.glyph_row->used[TEXT_AREA] = 0;
12517 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
12518 truncate_it.object = make_number (0);
12519 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
12520
12521 /* Overwrite glyphs from IT with truncation glyphs. */
12522 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
12523 end = from + truncate_it.glyph_row->used[TEXT_AREA];
12524 to = it->glyph_row->glyphs[TEXT_AREA];
12525 toend = to + it->glyph_row->used[TEXT_AREA];
12526
12527 while (from < end)
12528 *to++ = *from++;
12529
12530 /* There may be padding glyphs left over. Overwrite them too. */
12531 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
12532 {
12533 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
12534 while (from < end)
12535 *to++ = *from++;
12536 }
12537
12538 if (to > toend)
12539 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
12540 }
12541
12542
12543 /* Compute the pixel height and width of IT->glyph_row.
12544
12545 Most of the time, ascent and height of a display line will be equal
12546 to the max_ascent and max_height values of the display iterator
12547 structure. This is not the case if
12548
12549 1. We hit ZV without displaying anything. In this case, max_ascent
12550 and max_height will be zero.
12551
12552 2. We have some glyphs that don't contribute to the line height.
12553 (The glyph row flag contributes_to_line_height_p is for future
12554 pixmap extensions).
12555
12556 The first case is easily covered by using default values because in
12557 these cases, the line height does not really matter, except that it
12558 must not be zero. */
12559
12560 static void
12561 compute_line_metrics (it)
12562 struct it *it;
12563 {
12564 struct glyph_row *row = it->glyph_row;
12565 int area, i;
12566
12567 if (FRAME_WINDOW_P (it->f))
12568 {
12569 int i, min_y, max_y;
12570
12571 /* The line may consist of one space only, that was added to
12572 place the cursor on it. If so, the row's height hasn't been
12573 computed yet. */
12574 if (row->height == 0)
12575 {
12576 if (it->max_ascent + it->max_descent == 0)
12577 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
12578 row->ascent = it->max_ascent;
12579 row->height = it->max_ascent + it->max_descent;
12580 row->phys_ascent = it->max_phys_ascent;
12581 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
12582 }
12583
12584 /* Compute the width of this line. */
12585 row->pixel_width = row->x;
12586 for (i = 0; i < row->used[TEXT_AREA]; ++i)
12587 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
12588
12589 xassert (row->pixel_width >= 0);
12590 xassert (row->ascent >= 0 && row->height > 0);
12591
12592 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
12593 || MATRIX_ROW_OVERLAPS_PRED_P (row));
12594
12595 /* If first line's physical ascent is larger than its logical
12596 ascent, use the physical ascent, and make the row taller.
12597 This makes accented characters fully visible. */
12598 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
12599 && row->phys_ascent > row->ascent)
12600 {
12601 row->height += row->phys_ascent - row->ascent;
12602 row->ascent = row->phys_ascent;
12603 }
12604
12605 /* Compute how much of the line is visible. */
12606 row->visible_height = row->height;
12607
12608 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (it->w);
12609 max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
12610
12611 if (row->y < min_y)
12612 row->visible_height -= min_y - row->y;
12613 if (row->y + row->height > max_y)
12614 row->visible_height -= row->y + row->height - max_y;
12615 }
12616 else
12617 {
12618 row->pixel_width = row->used[TEXT_AREA];
12619 if (row->continued_p)
12620 row->pixel_width -= it->continuation_pixel_width;
12621 else if (row->truncated_on_right_p)
12622 row->pixel_width -= it->truncation_pixel_width;
12623 row->ascent = row->phys_ascent = 0;
12624 row->height = row->phys_height = row->visible_height = 1;
12625 }
12626
12627 /* Compute a hash code for this row. */
12628 row->hash = 0;
12629 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12630 for (i = 0; i < row->used[area]; ++i)
12631 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
12632 + row->glyphs[area][i].u.val
12633 + row->glyphs[area][i].face_id
12634 + row->glyphs[area][i].padding_p
12635 + (row->glyphs[area][i].type << 2));
12636
12637 it->max_ascent = it->max_descent = 0;
12638 it->max_phys_ascent = it->max_phys_descent = 0;
12639 }
12640
12641
12642 /* Append one space to the glyph row of iterator IT if doing a
12643 window-based redisplay. DEFAULT_FACE_P non-zero means let the
12644 space have the default face, otherwise let it have the same face as
12645 IT->face_id. Value is non-zero if a space was added.
12646
12647 This function is called to make sure that there is always one glyph
12648 at the end of a glyph row that the cursor can be set on under
12649 window-systems. (If there weren't such a glyph we would not know
12650 how wide and tall a box cursor should be displayed).
12651
12652 At the same time this space let's a nicely handle clearing to the
12653 end of the line if the row ends in italic text. */
12654
12655 static int
12656 append_space (it, default_face_p)
12657 struct it *it;
12658 int default_face_p;
12659 {
12660 if (FRAME_WINDOW_P (it->f))
12661 {
12662 int n = it->glyph_row->used[TEXT_AREA];
12663
12664 if (it->glyph_row->glyphs[TEXT_AREA] + n
12665 < it->glyph_row->glyphs[1 + TEXT_AREA])
12666 {
12667 /* Save some values that must not be changed.
12668 Must save IT->c and IT->len because otherwise
12669 ITERATOR_AT_END_P wouldn't work anymore after
12670 append_space has been called. */
12671 enum display_element_type saved_what = it->what;
12672 int saved_c = it->c, saved_len = it->len;
12673 int saved_x = it->current_x;
12674 int saved_face_id = it->face_id;
12675 struct text_pos saved_pos;
12676 Lisp_Object saved_object;
12677 struct face *face;
12678
12679 saved_object = it->object;
12680 saved_pos = it->position;
12681
12682 it->what = IT_CHARACTER;
12683 bzero (&it->position, sizeof it->position);
12684 it->object = make_number (0);
12685 it->c = ' ';
12686 it->len = 1;
12687
12688 if (default_face_p)
12689 it->face_id = DEFAULT_FACE_ID;
12690 else if (it->face_before_selective_p)
12691 it->face_id = it->saved_face_id;
12692 face = FACE_FROM_ID (it->f, it->face_id);
12693 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
12694
12695 PRODUCE_GLYPHS (it);
12696
12697 it->current_x = saved_x;
12698 it->object = saved_object;
12699 it->position = saved_pos;
12700 it->what = saved_what;
12701 it->face_id = saved_face_id;
12702 it->len = saved_len;
12703 it->c = saved_c;
12704 return 1;
12705 }
12706 }
12707
12708 return 0;
12709 }
12710
12711
12712 /* Extend the face of the last glyph in the text area of IT->glyph_row
12713 to the end of the display line. Called from display_line.
12714 If the glyph row is empty, add a space glyph to it so that we
12715 know the face to draw. Set the glyph row flag fill_line_p. */
12716
12717 static void
12718 extend_face_to_end_of_line (it)
12719 struct it *it;
12720 {
12721 struct face *face;
12722 struct frame *f = it->f;
12723
12724 /* If line is already filled, do nothing. */
12725 if (it->current_x >= it->last_visible_x)
12726 return;
12727
12728 /* Face extension extends the background and box of IT->face_id
12729 to the end of the line. If the background equals the background
12730 of the frame, we don't have to do anything. */
12731 if (it->face_before_selective_p)
12732 face = FACE_FROM_ID (it->f, it->saved_face_id);
12733 else
12734 face = FACE_FROM_ID (f, it->face_id);
12735
12736 if (FRAME_WINDOW_P (f)
12737 && face->box == FACE_NO_BOX
12738 && face->background == FRAME_BACKGROUND_PIXEL (f)
12739 && !face->stipple)
12740 return;
12741
12742 /* Set the glyph row flag indicating that the face of the last glyph
12743 in the text area has to be drawn to the end of the text area. */
12744 it->glyph_row->fill_line_p = 1;
12745
12746 /* If current character of IT is not ASCII, make sure we have the
12747 ASCII face. This will be automatically undone the next time
12748 get_next_display_element returns a multibyte character. Note
12749 that the character will always be single byte in unibyte text. */
12750 if (!ASCII_CHAR_P (it->c))
12751 {
12752 it->face_id = FACE_FOR_CHAR (f, face, 0);
12753 }
12754
12755 if (FRAME_WINDOW_P (f))
12756 {
12757 /* If the row is empty, add a space with the current face of IT,
12758 so that we know which face to draw. */
12759 if (it->glyph_row->used[TEXT_AREA] == 0)
12760 {
12761 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
12762 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
12763 it->glyph_row->used[TEXT_AREA] = 1;
12764 }
12765 }
12766 else
12767 {
12768 /* Save some values that must not be changed. */
12769 int saved_x = it->current_x;
12770 struct text_pos saved_pos;
12771 Lisp_Object saved_object;
12772 enum display_element_type saved_what = it->what;
12773 int saved_face_id = it->face_id;
12774
12775 saved_object = it->object;
12776 saved_pos = it->position;
12777
12778 it->what = IT_CHARACTER;
12779 bzero (&it->position, sizeof it->position);
12780 it->object = make_number (0);
12781 it->c = ' ';
12782 it->len = 1;
12783 it->face_id = face->id;
12784
12785 PRODUCE_GLYPHS (it);
12786
12787 while (it->current_x <= it->last_visible_x)
12788 PRODUCE_GLYPHS (it);
12789
12790 /* Don't count these blanks really. It would let us insert a left
12791 truncation glyph below and make us set the cursor on them, maybe. */
12792 it->current_x = saved_x;
12793 it->object = saved_object;
12794 it->position = saved_pos;
12795 it->what = saved_what;
12796 it->face_id = saved_face_id;
12797 }
12798 }
12799
12800
12801 /* Value is non-zero if text starting at CHARPOS in current_buffer is
12802 trailing whitespace. */
12803
12804 static int
12805 trailing_whitespace_p (charpos)
12806 int charpos;
12807 {
12808 int bytepos = CHAR_TO_BYTE (charpos);
12809 int c = 0;
12810
12811 while (bytepos < ZV_BYTE
12812 && (c = FETCH_CHAR (bytepos),
12813 c == ' ' || c == '\t'))
12814 ++bytepos;
12815
12816 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
12817 {
12818 if (bytepos != PT_BYTE)
12819 return 1;
12820 }
12821 return 0;
12822 }
12823
12824
12825 /* Highlight trailing whitespace, if any, in ROW. */
12826
12827 void
12828 highlight_trailing_whitespace (f, row)
12829 struct frame *f;
12830 struct glyph_row *row;
12831 {
12832 int used = row->used[TEXT_AREA];
12833
12834 if (used)
12835 {
12836 struct glyph *start = row->glyphs[TEXT_AREA];
12837 struct glyph *glyph = start + used - 1;
12838
12839 /* Skip over glyphs inserted to display the cursor at the
12840 end of a line, for extending the face of the last glyph
12841 to the end of the line on terminals, and for truncation
12842 and continuation glyphs. */
12843 while (glyph >= start
12844 && glyph->type == CHAR_GLYPH
12845 && INTEGERP (glyph->object))
12846 --glyph;
12847
12848 /* If last glyph is a space or stretch, and it's trailing
12849 whitespace, set the face of all trailing whitespace glyphs in
12850 IT->glyph_row to `trailing-whitespace'. */
12851 if (glyph >= start
12852 && BUFFERP (glyph->object)
12853 && (glyph->type == STRETCH_GLYPH
12854 || (glyph->type == CHAR_GLYPH
12855 && glyph->u.ch == ' '))
12856 && trailing_whitespace_p (glyph->charpos))
12857 {
12858 int face_id = lookup_named_face (f, Qtrailing_whitespace);
12859
12860 while (glyph >= start
12861 && BUFFERP (glyph->object)
12862 && (glyph->type == STRETCH_GLYPH
12863 || (glyph->type == CHAR_GLYPH
12864 && glyph->u.ch == ' ')))
12865 (glyph--)->face_id = face_id;
12866 }
12867 }
12868 }
12869
12870
12871 /* Value is non-zero if glyph row ROW in window W should be
12872 used to hold the cursor. */
12873
12874 static int
12875 cursor_row_p (w, row)
12876 struct window *w;
12877 struct glyph_row *row;
12878 {
12879 int cursor_row_p = 1;
12880
12881 if (PT == MATRIX_ROW_END_CHARPOS (row))
12882 {
12883 /* If the row ends with a newline from a string, we don't want
12884 the cursor there (if the row is continued it doesn't end in a
12885 newline). */
12886 if (CHARPOS (row->end.string_pos) >= 0
12887 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12888 cursor_row_p = row->continued_p;
12889
12890 /* If the row ends at ZV, display the cursor at the end of that
12891 row instead of at the start of the row below. */
12892 else if (row->ends_at_zv_p)
12893 cursor_row_p = 1;
12894 else
12895 cursor_row_p = 0;
12896 }
12897
12898 return cursor_row_p;
12899 }
12900
12901
12902 /* Construct the glyph row IT->glyph_row in the desired matrix of
12903 IT->w from text at the current position of IT. See dispextern.h
12904 for an overview of struct it. Value is non-zero if
12905 IT->glyph_row displays text, as opposed to a line displaying ZV
12906 only. */
12907
12908 static int
12909 display_line (it)
12910 struct it *it;
12911 {
12912 struct glyph_row *row = it->glyph_row;
12913
12914 /* We always start displaying at hpos zero even if hscrolled. */
12915 xassert (it->hpos == 0 && it->current_x == 0);
12916
12917 /* We must not display in a row that's not a text row. */
12918 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
12919 < it->w->desired_matrix->nrows);
12920
12921 /* Is IT->w showing the region? */
12922 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
12923
12924 /* Clear the result glyph row and enable it. */
12925 prepare_desired_row (row);
12926
12927 row->y = it->current_y;
12928 row->start = it->current;
12929 row->continuation_lines_width = it->continuation_lines_width;
12930 row->displays_text_p = 1;
12931 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
12932 it->starts_in_middle_of_char_p = 0;
12933
12934 /* Arrange the overlays nicely for our purposes. Usually, we call
12935 display_line on only one line at a time, in which case this
12936 can't really hurt too much, or we call it on lines which appear
12937 one after another in the buffer, in which case all calls to
12938 recenter_overlay_lists but the first will be pretty cheap. */
12939 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
12940
12941 /* Move over display elements that are not visible because we are
12942 hscrolled. This may stop at an x-position < IT->first_visible_x
12943 if the first glyph is partially visible or if we hit a line end. */
12944 if (it->current_x < it->first_visible_x)
12945 move_it_in_display_line_to (it, ZV, it->first_visible_x,
12946 MOVE_TO_POS | MOVE_TO_X);
12947
12948 /* Get the initial row height. This is either the height of the
12949 text hscrolled, if there is any, or zero. */
12950 row->ascent = it->max_ascent;
12951 row->height = it->max_ascent + it->max_descent;
12952 row->phys_ascent = it->max_phys_ascent;
12953 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
12954
12955 /* Loop generating characters. The loop is left with IT on the next
12956 character to display. */
12957 while (1)
12958 {
12959 int n_glyphs_before, hpos_before, x_before;
12960 int x, i, nglyphs;
12961 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
12962
12963 /* Retrieve the next thing to display. Value is zero if end of
12964 buffer reached. */
12965 if (!get_next_display_element (it))
12966 {
12967 /* Maybe add a space at the end of this line that is used to
12968 display the cursor there under X. Set the charpos of the
12969 first glyph of blank lines not corresponding to any text
12970 to -1. */
12971 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
12972 || row->used[TEXT_AREA] == 0)
12973 {
12974 row->glyphs[TEXT_AREA]->charpos = -1;
12975 row->displays_text_p = 0;
12976
12977 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
12978 && (!MINI_WINDOW_P (it->w)
12979 || (minibuf_level && EQ (it->window, minibuf_window))))
12980 row->indicate_empty_line_p = 1;
12981 }
12982
12983 it->continuation_lines_width = 0;
12984 row->ends_at_zv_p = 1;
12985 break;
12986 }
12987
12988 /* Now, get the metrics of what we want to display. This also
12989 generates glyphs in `row' (which is IT->glyph_row). */
12990 n_glyphs_before = row->used[TEXT_AREA];
12991 x = it->current_x;
12992
12993 /* Remember the line height so far in case the next element doesn't
12994 fit on the line. */
12995 if (!it->truncate_lines_p)
12996 {
12997 ascent = it->max_ascent;
12998 descent = it->max_descent;
12999 phys_ascent = it->max_phys_ascent;
13000 phys_descent = it->max_phys_descent;
13001 }
13002
13003 PRODUCE_GLYPHS (it);
13004
13005 /* If this display element was in marginal areas, continue with
13006 the next one. */
13007 if (it->area != TEXT_AREA)
13008 {
13009 row->ascent = max (row->ascent, it->max_ascent);
13010 row->height = max (row->height, it->max_ascent + it->max_descent);
13011 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13012 row->phys_height = max (row->phys_height,
13013 it->max_phys_ascent + it->max_phys_descent);
13014 set_iterator_to_next (it, 1);
13015 continue;
13016 }
13017
13018 /* Does the display element fit on the line? If we truncate
13019 lines, we should draw past the right edge of the window. If
13020 we don't truncate, we want to stop so that we can display the
13021 continuation glyph before the right margin. If lines are
13022 continued, there are two possible strategies for characters
13023 resulting in more than 1 glyph (e.g. tabs): Display as many
13024 glyphs as possible in this line and leave the rest for the
13025 continuation line, or display the whole element in the next
13026 line. Original redisplay did the former, so we do it also. */
13027 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
13028 hpos_before = it->hpos;
13029 x_before = x;
13030
13031 if (/* Not a newline. */
13032 nglyphs > 0
13033 /* Glyphs produced fit entirely in the line. */
13034 && it->current_x < it->last_visible_x)
13035 {
13036 it->hpos += nglyphs;
13037 row->ascent = max (row->ascent, it->max_ascent);
13038 row->height = max (row->height, it->max_ascent + it->max_descent);
13039 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13040 row->phys_height = max (row->phys_height,
13041 it->max_phys_ascent + it->max_phys_descent);
13042 if (it->current_x - it->pixel_width < it->first_visible_x)
13043 row->x = x - it->first_visible_x;
13044 }
13045 else
13046 {
13047 int new_x;
13048 struct glyph *glyph;
13049
13050 for (i = 0; i < nglyphs; ++i, x = new_x)
13051 {
13052 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
13053 new_x = x + glyph->pixel_width;
13054
13055 if (/* Lines are continued. */
13056 !it->truncate_lines_p
13057 && (/* Glyph doesn't fit on the line. */
13058 new_x > it->last_visible_x
13059 /* Or it fits exactly on a window system frame. */
13060 || (new_x == it->last_visible_x
13061 && FRAME_WINDOW_P (it->f))))
13062 {
13063 /* End of a continued line. */
13064
13065 if (it->hpos == 0
13066 || (new_x == it->last_visible_x
13067 && FRAME_WINDOW_P (it->f)))
13068 {
13069 /* Current glyph is the only one on the line or
13070 fits exactly on the line. We must continue
13071 the line because we can't draw the cursor
13072 after the glyph. */
13073 row->continued_p = 1;
13074 it->current_x = new_x;
13075 it->continuation_lines_width += new_x;
13076 ++it->hpos;
13077 if (i == nglyphs - 1)
13078 set_iterator_to_next (it, 1);
13079 }
13080 else if (CHAR_GLYPH_PADDING_P (*glyph)
13081 && !FRAME_WINDOW_P (it->f))
13082 {
13083 /* A padding glyph that doesn't fit on this line.
13084 This means the whole character doesn't fit
13085 on the line. */
13086 row->used[TEXT_AREA] = n_glyphs_before;
13087
13088 /* Fill the rest of the row with continuation
13089 glyphs like in 20.x. */
13090 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
13091 < row->glyphs[1 + TEXT_AREA])
13092 produce_special_glyphs (it, IT_CONTINUATION);
13093
13094 row->continued_p = 1;
13095 it->current_x = x_before;
13096 it->continuation_lines_width += x_before;
13097
13098 /* Restore the height to what it was before the
13099 element not fitting on the line. */
13100 it->max_ascent = ascent;
13101 it->max_descent = descent;
13102 it->max_phys_ascent = phys_ascent;
13103 it->max_phys_descent = phys_descent;
13104 }
13105 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
13106 {
13107 /* A TAB that extends past the right edge of the
13108 window. This produces a single glyph on
13109 window system frames. We leave the glyph in
13110 this row and let it fill the row, but don't
13111 consume the TAB. */
13112 it->continuation_lines_width += it->last_visible_x;
13113 row->ends_in_middle_of_char_p = 1;
13114 row->continued_p = 1;
13115 glyph->pixel_width = it->last_visible_x - x;
13116 it->starts_in_middle_of_char_p = 1;
13117 }
13118 else
13119 {
13120 /* Something other than a TAB that draws past
13121 the right edge of the window. Restore
13122 positions to values before the element. */
13123 row->used[TEXT_AREA] = n_glyphs_before + i;
13124
13125 /* Display continuation glyphs. */
13126 if (!FRAME_WINDOW_P (it->f))
13127 produce_special_glyphs (it, IT_CONTINUATION);
13128 row->continued_p = 1;
13129
13130 it->continuation_lines_width += x;
13131
13132 if (nglyphs > 1 && i > 0)
13133 {
13134 row->ends_in_middle_of_char_p = 1;
13135 it->starts_in_middle_of_char_p = 1;
13136 }
13137
13138 /* Restore the height to what it was before the
13139 element not fitting on the line. */
13140 it->max_ascent = ascent;
13141 it->max_descent = descent;
13142 it->max_phys_ascent = phys_ascent;
13143 it->max_phys_descent = phys_descent;
13144 }
13145
13146 break;
13147 }
13148 else if (new_x > it->first_visible_x)
13149 {
13150 /* Increment number of glyphs actually displayed. */
13151 ++it->hpos;
13152
13153 if (x < it->first_visible_x)
13154 /* Glyph is partially visible, i.e. row starts at
13155 negative X position. */
13156 row->x = x - it->first_visible_x;
13157 }
13158 else
13159 {
13160 /* Glyph is completely off the left margin of the
13161 window. This should not happen because of the
13162 move_it_in_display_line at the start of
13163 this function. */
13164 abort ();
13165 }
13166 }
13167
13168 row->ascent = max (row->ascent, it->max_ascent);
13169 row->height = max (row->height, it->max_ascent + it->max_descent);
13170 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13171 row->phys_height = max (row->phys_height,
13172 it->max_phys_ascent + it->max_phys_descent);
13173
13174 /* End of this display line if row is continued. */
13175 if (row->continued_p)
13176 break;
13177 }
13178
13179 /* Is this a line end? If yes, we're also done, after making
13180 sure that a non-default face is extended up to the right
13181 margin of the window. */
13182 if (ITERATOR_AT_END_OF_LINE_P (it))
13183 {
13184 int used_before = row->used[TEXT_AREA];
13185
13186 row->ends_in_newline_from_string_p = STRINGP (it->object);
13187
13188 /* Add a space at the end of the line that is used to
13189 display the cursor there. */
13190 append_space (it, 0);
13191
13192 /* Extend the face to the end of the line. */
13193 extend_face_to_end_of_line (it);
13194
13195 /* Make sure we have the position. */
13196 if (used_before == 0)
13197 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
13198
13199 /* Consume the line end. This skips over invisible lines. */
13200 set_iterator_to_next (it, 1);
13201 it->continuation_lines_width = 0;
13202 break;
13203 }
13204
13205 /* Proceed with next display element. Note that this skips
13206 over lines invisible because of selective display. */
13207 set_iterator_to_next (it, 1);
13208
13209 /* If we truncate lines, we are done when the last displayed
13210 glyphs reach past the right margin of the window. */
13211 if (it->truncate_lines_p
13212 && (FRAME_WINDOW_P (it->f)
13213 ? (it->current_x >= it->last_visible_x)
13214 : (it->current_x > it->last_visible_x)))
13215 {
13216 /* Maybe add truncation glyphs. */
13217 if (!FRAME_WINDOW_P (it->f))
13218 {
13219 int i, n;
13220
13221 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
13222 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
13223 break;
13224
13225 for (n = row->used[TEXT_AREA]; i < n; ++i)
13226 {
13227 row->used[TEXT_AREA] = i;
13228 produce_special_glyphs (it, IT_TRUNCATION);
13229 }
13230 }
13231
13232 row->truncated_on_right_p = 1;
13233 it->continuation_lines_width = 0;
13234 reseat_at_next_visible_line_start (it, 0);
13235 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
13236 it->hpos = hpos_before;
13237 it->current_x = x_before;
13238 break;
13239 }
13240 }
13241
13242 /* If line is not empty and hscrolled, maybe insert truncation glyphs
13243 at the left window margin. */
13244 if (it->first_visible_x
13245 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
13246 {
13247 if (!FRAME_WINDOW_P (it->f))
13248 insert_left_trunc_glyphs (it);
13249 row->truncated_on_left_p = 1;
13250 }
13251
13252 /* If the start of this line is the overlay arrow-position, then
13253 mark this glyph row as the one containing the overlay arrow.
13254 This is clearly a mess with variable size fonts. It would be
13255 better to let it be displayed like cursors under X. */
13256 if (MARKERP (Voverlay_arrow_position)
13257 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
13258 && (MATRIX_ROW_START_CHARPOS (row)
13259 == marker_position (Voverlay_arrow_position))
13260 && STRINGP (Voverlay_arrow_string)
13261 && ! overlay_arrow_seen)
13262 {
13263 /* Overlay arrow in window redisplay is a fringe bitmap. */
13264 if (!FRAME_WINDOW_P (it->f))
13265 {
13266 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
13267 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
13268 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
13269 struct glyph *p = row->glyphs[TEXT_AREA];
13270 struct glyph *p2, *end;
13271
13272 /* Copy the arrow glyphs. */
13273 while (glyph < arrow_end)
13274 *p++ = *glyph++;
13275
13276 /* Throw away padding glyphs. */
13277 p2 = p;
13278 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
13279 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
13280 ++p2;
13281 if (p2 > p)
13282 {
13283 while (p2 < end)
13284 *p++ = *p2++;
13285 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
13286 }
13287 }
13288
13289 overlay_arrow_seen = 1;
13290 row->overlay_arrow_p = 1;
13291 }
13292
13293 /* Compute pixel dimensions of this line. */
13294 compute_line_metrics (it);
13295
13296 /* Remember the position at which this line ends. */
13297 row->end = it->current;
13298
13299 /* Maybe set the cursor. */
13300 if (it->w->cursor.vpos < 0
13301 && PT >= MATRIX_ROW_START_CHARPOS (row)
13302 && PT <= MATRIX_ROW_END_CHARPOS (row)
13303 && cursor_row_p (it->w, row))
13304 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
13305
13306 /* Highlight trailing whitespace. */
13307 if (!NILP (Vshow_trailing_whitespace))
13308 highlight_trailing_whitespace (it->f, it->glyph_row);
13309
13310 /* Prepare for the next line. This line starts horizontally at (X
13311 HPOS) = (0 0). Vertical positions are incremented. As a
13312 convenience for the caller, IT->glyph_row is set to the next
13313 row to be used. */
13314 it->current_x = it->hpos = 0;
13315 it->current_y += row->height;
13316 ++it->vpos;
13317 ++it->glyph_row;
13318 return row->displays_text_p;
13319 }
13320
13321
13322 \f
13323 /***********************************************************************
13324 Menu Bar
13325 ***********************************************************************/
13326
13327 /* Redisplay the menu bar in the frame for window W.
13328
13329 The menu bar of X frames that don't have X toolkit support is
13330 displayed in a special window W->frame->menu_bar_window.
13331
13332 The menu bar of terminal frames is treated specially as far as
13333 glyph matrices are concerned. Menu bar lines are not part of
13334 windows, so the update is done directly on the frame matrix rows
13335 for the menu bar. */
13336
13337 static void
13338 display_menu_bar (w)
13339 struct window *w;
13340 {
13341 struct frame *f = XFRAME (WINDOW_FRAME (w));
13342 struct it it;
13343 Lisp_Object items;
13344 int i;
13345
13346 /* Don't do all this for graphical frames. */
13347 #ifdef HAVE_NTGUI
13348 if (!NILP (Vwindow_system))
13349 return;
13350 #endif
13351 #ifdef USE_X_TOOLKIT
13352 if (FRAME_X_P (f))
13353 return;
13354 #endif
13355 #ifdef macintosh
13356 if (FRAME_MAC_P (f))
13357 return;
13358 #endif
13359
13360 #ifdef USE_X_TOOLKIT
13361 xassert (!FRAME_WINDOW_P (f));
13362 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
13363 it.first_visible_x = 0;
13364 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
13365 #else /* not USE_X_TOOLKIT */
13366 if (FRAME_WINDOW_P (f))
13367 {
13368 /* Menu bar lines are displayed in the desired matrix of the
13369 dummy window menu_bar_window. */
13370 struct window *menu_w;
13371 xassert (WINDOWP (f->menu_bar_window));
13372 menu_w = XWINDOW (f->menu_bar_window);
13373 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
13374 MENU_FACE_ID);
13375 it.first_visible_x = 0;
13376 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
13377 }
13378 else
13379 {
13380 /* This is a TTY frame, i.e. character hpos/vpos are used as
13381 pixel x/y. */
13382 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
13383 MENU_FACE_ID);
13384 it.first_visible_x = 0;
13385 it.last_visible_x = FRAME_WIDTH (f);
13386 }
13387 #endif /* not USE_X_TOOLKIT */
13388
13389 if (! mode_line_inverse_video)
13390 /* Force the menu-bar to be displayed in the default face. */
13391 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
13392
13393 /* Clear all rows of the menu bar. */
13394 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
13395 {
13396 struct glyph_row *row = it.glyph_row + i;
13397 clear_glyph_row (row);
13398 row->enabled_p = 1;
13399 row->full_width_p = 1;
13400 }
13401
13402 /* Display all items of the menu bar. */
13403 items = FRAME_MENU_BAR_ITEMS (it.f);
13404 for (i = 0; i < XVECTOR (items)->size; i += 4)
13405 {
13406 Lisp_Object string;
13407
13408 /* Stop at nil string. */
13409 string = AREF (items, i + 1);
13410 if (NILP (string))
13411 break;
13412
13413 /* Remember where item was displayed. */
13414 AREF (items, i + 3) = make_number (it.hpos);
13415
13416 /* Display the item, pad with one space. */
13417 if (it.current_x < it.last_visible_x)
13418 display_string (NULL, string, Qnil, 0, 0, &it,
13419 XSTRING (string)->size + 1, 0, 0, -1);
13420 }
13421
13422 /* Fill out the line with spaces. */
13423 if (it.current_x < it.last_visible_x)
13424 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
13425
13426 /* Compute the total height of the lines. */
13427 compute_line_metrics (&it);
13428 }
13429
13430
13431 \f
13432 /***********************************************************************
13433 Mode Line
13434 ***********************************************************************/
13435
13436 /* Redisplay mode lines in the window tree whose root is WINDOW. If
13437 FORCE is non-zero, redisplay mode lines unconditionally.
13438 Otherwise, redisplay only mode lines that are garbaged. Value is
13439 the number of windows whose mode lines were redisplayed. */
13440
13441 static int
13442 redisplay_mode_lines (window, force)
13443 Lisp_Object window;
13444 int force;
13445 {
13446 int nwindows = 0;
13447
13448 while (!NILP (window))
13449 {
13450 struct window *w = XWINDOW (window);
13451
13452 if (WINDOWP (w->hchild))
13453 nwindows += redisplay_mode_lines (w->hchild, force);
13454 else if (WINDOWP (w->vchild))
13455 nwindows += redisplay_mode_lines (w->vchild, force);
13456 else if (force
13457 || FRAME_GARBAGED_P (XFRAME (w->frame))
13458 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
13459 {
13460 struct text_pos lpoint;
13461 struct buffer *old = current_buffer;
13462
13463 /* Set the window's buffer for the mode line display. */
13464 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13465 set_buffer_internal_1 (XBUFFER (w->buffer));
13466
13467 /* Point refers normally to the selected window. For any
13468 other window, set up appropriate value. */
13469 if (!EQ (window, selected_window))
13470 {
13471 struct text_pos pt;
13472
13473 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
13474 if (CHARPOS (pt) < BEGV)
13475 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
13476 else if (CHARPOS (pt) > (ZV - 1))
13477 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
13478 else
13479 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
13480 }
13481
13482 /* Display mode lines. */
13483 clear_glyph_matrix (w->desired_matrix);
13484 if (display_mode_lines (w))
13485 {
13486 ++nwindows;
13487 w->must_be_updated_p = 1;
13488 }
13489
13490 /* Restore old settings. */
13491 set_buffer_internal_1 (old);
13492 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13493 }
13494
13495 window = w->next;
13496 }
13497
13498 return nwindows;
13499 }
13500
13501
13502 /* Display the mode and/or top line of window W. Value is the number
13503 of mode lines displayed. */
13504
13505 static int
13506 display_mode_lines (w)
13507 struct window *w;
13508 {
13509 Lisp_Object old_selected_window, old_selected_frame;
13510 int n = 0;
13511
13512 old_selected_frame = selected_frame;
13513 selected_frame = w->frame;
13514 old_selected_window = selected_window;
13515 XSETWINDOW (selected_window, w);
13516
13517 /* These will be set while the mode line specs are processed. */
13518 line_number_displayed = 0;
13519 w->column_number_displayed = Qnil;
13520
13521 if (WINDOW_WANTS_MODELINE_P (w))
13522 {
13523 struct window *sel_w = XWINDOW (old_selected_window);
13524
13525 /* Select mode line face based on the real selected window. */
13526 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
13527 current_buffer->mode_line_format);
13528 ++n;
13529 }
13530
13531 if (WINDOW_WANTS_HEADER_LINE_P (w))
13532 {
13533 display_mode_line (w, HEADER_LINE_FACE_ID,
13534 current_buffer->header_line_format);
13535 ++n;
13536 }
13537
13538 selected_frame = old_selected_frame;
13539 selected_window = old_selected_window;
13540 return n;
13541 }
13542
13543
13544 /* Display mode or top line of window W. FACE_ID specifies which line
13545 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
13546 FORMAT is the mode line format to display. Value is the pixel
13547 height of the mode line displayed. */
13548
13549 static int
13550 display_mode_line (w, face_id, format)
13551 struct window *w;
13552 enum face_id face_id;
13553 Lisp_Object format;
13554 {
13555 struct it it;
13556 struct face *face;
13557
13558 init_iterator (&it, w, -1, -1, NULL, face_id);
13559 prepare_desired_row (it.glyph_row);
13560
13561 if (! mode_line_inverse_video)
13562 /* Force the mode-line to be displayed in the default face. */
13563 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
13564
13565 /* Temporarily make frame's keyboard the current kboard so that
13566 kboard-local variables in the mode_line_format will get the right
13567 values. */
13568 push_frame_kboard (it.f);
13569 display_mode_element (&it, 0, 0, 0, format, Qnil);
13570 pop_frame_kboard ();
13571
13572 /* Fill up with spaces. */
13573 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
13574
13575 compute_line_metrics (&it);
13576 it.glyph_row->full_width_p = 1;
13577 it.glyph_row->mode_line_p = 1;
13578 it.glyph_row->continued_p = 0;
13579 it.glyph_row->truncated_on_left_p = 0;
13580 it.glyph_row->truncated_on_right_p = 0;
13581
13582 /* Make a 3D mode-line have a shadow at its right end. */
13583 face = FACE_FROM_ID (it.f, face_id);
13584 extend_face_to_end_of_line (&it);
13585 if (face->box != FACE_NO_BOX)
13586 {
13587 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
13588 + it.glyph_row->used[TEXT_AREA] - 1);
13589 last->right_box_line_p = 1;
13590 }
13591
13592 return it.glyph_row->height;
13593 }
13594
13595 /* Alist that caches the results of :propertize.
13596 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
13597 Lisp_Object mode_line_proptrans_alist;
13598
13599 /* Contribute ELT to the mode line for window IT->w. How it
13600 translates into text depends on its data type.
13601
13602 IT describes the display environment in which we display, as usual.
13603
13604 DEPTH is the depth in recursion. It is used to prevent
13605 infinite recursion here.
13606
13607 FIELD_WIDTH is the number of characters the display of ELT should
13608 occupy in the mode line, and PRECISION is the maximum number of
13609 characters to display from ELT's representation. See
13610 display_string for details.
13611
13612 Returns the hpos of the end of the text generated by ELT. */
13613
13614 static int
13615 display_mode_element (it, depth, field_width, precision, elt, props)
13616 struct it *it;
13617 int depth;
13618 int field_width, precision;
13619 Lisp_Object elt, props;
13620 {
13621 int n = 0, field, prec;
13622 int literal = 0;
13623
13624 tail_recurse:
13625 if (depth > 10)
13626 goto invalid;
13627
13628 depth++;
13629
13630 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
13631 {
13632 case Lisp_String:
13633 {
13634 /* A string: output it and check for %-constructs within it. */
13635 unsigned char c;
13636 unsigned char *this = XSTRING (elt)->data;
13637 unsigned char *lisp_string = this;
13638
13639 if (!NILP (props))
13640 {
13641 Lisp_Object oprops, aelt;
13642 oprops = Ftext_properties_at (make_number (0), elt);
13643 if (NILP (Fequal (props, oprops)))
13644 {
13645 aelt = Fassoc (elt, mode_line_proptrans_alist);
13646 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
13647 elt = XCAR (aelt);
13648 else
13649 {
13650 elt = Fcopy_sequence (elt);
13651 Fset_text_properties (make_number (0), Flength (elt),
13652 props, elt);
13653 mode_line_proptrans_alist
13654 = Fcons (Fcons (elt, props),
13655 mode_line_proptrans_alist);
13656 }
13657 }
13658 this = XSTRING (elt)->data;
13659 lisp_string = this;
13660 }
13661
13662 if (literal)
13663 {
13664 prec = precision - n;
13665 if (frame_title_ptr)
13666 n += store_frame_title (XSTRING (elt)->data, -1, prec);
13667 else
13668 n += display_string (NULL, elt, Qnil, 0, 0, it,
13669 0, prec, 0, STRING_MULTIBYTE (elt));
13670
13671 break;
13672 }
13673
13674 while ((precision <= 0 || n < precision)
13675 && *this
13676 && (frame_title_ptr
13677 || it->current_x < it->last_visible_x))
13678 {
13679 unsigned char *last = this;
13680
13681 /* Advance to end of string or next format specifier. */
13682 while ((c = *this++) != '\0' && c != '%')
13683 ;
13684
13685 if (this - 1 != last)
13686 {
13687 /* Output to end of string or up to '%'. Field width
13688 is length of string. Don't output more than
13689 PRECISION allows us. */
13690 --this;
13691
13692 prec = chars_in_text (last, this - last);
13693 if (precision > 0 && prec > precision - n)
13694 prec = precision - n;
13695
13696 if (frame_title_ptr)
13697 n += store_frame_title (last, 0, prec);
13698 else
13699 {
13700 int bytepos = last - lisp_string;
13701 int charpos = string_byte_to_char (elt, bytepos);
13702 n += display_string (NULL, elt, Qnil, 0, charpos,
13703 it, 0, prec, 0,
13704 STRING_MULTIBYTE (elt));
13705 }
13706 }
13707 else /* c == '%' */
13708 {
13709 unsigned char *percent_position = this;
13710
13711 /* Get the specified minimum width. Zero means
13712 don't pad. */
13713 field = 0;
13714 while ((c = *this++) >= '0' && c <= '9')
13715 field = field * 10 + c - '0';
13716
13717 /* Don't pad beyond the total padding allowed. */
13718 if (field_width - n > 0 && field > field_width - n)
13719 field = field_width - n;
13720
13721 /* Note that either PRECISION <= 0 or N < PRECISION. */
13722 prec = precision - n;
13723
13724 if (c == 'M')
13725 n += display_mode_element (it, depth, field, prec,
13726 Vglobal_mode_string, props);
13727 else if (c != 0)
13728 {
13729 int multibyte;
13730 unsigned char *spec
13731 = decode_mode_spec (it->w, c, field, prec, &multibyte);
13732
13733 if (frame_title_ptr)
13734 n += store_frame_title (spec, field, prec);
13735 else
13736 {
13737 int nglyphs_before, bytepos, charpos, nwritten;
13738
13739 nglyphs_before = it->glyph_row->used[TEXT_AREA];
13740 bytepos = percent_position - lisp_string;
13741 charpos = (STRING_MULTIBYTE (elt)
13742 ? string_byte_to_char (elt, bytepos)
13743 : bytepos);
13744 nwritten = display_string (spec, Qnil, elt,
13745 charpos, 0, it,
13746 field, prec, 0,
13747 multibyte);
13748
13749 /* Assign to the glyphs written above the
13750 string where the `%x' came from, position
13751 of the `%'. */
13752 if (nwritten > 0)
13753 {
13754 struct glyph *glyph
13755 = (it->glyph_row->glyphs[TEXT_AREA]
13756 + nglyphs_before);
13757 int i;
13758
13759 for (i = 0; i < nwritten; ++i)
13760 {
13761 glyph[i].object = elt;
13762 glyph[i].charpos = charpos;
13763 }
13764
13765 n += nwritten;
13766 }
13767 }
13768 }
13769 else /* c == 0 */
13770 break;
13771 }
13772 }
13773 }
13774 break;
13775
13776 case Lisp_Symbol:
13777 /* A symbol: process the value of the symbol recursively
13778 as if it appeared here directly. Avoid error if symbol void.
13779 Special case: if value of symbol is a string, output the string
13780 literally. */
13781 {
13782 register Lisp_Object tem;
13783 tem = Fboundp (elt);
13784 if (!NILP (tem))
13785 {
13786 tem = Fsymbol_value (elt);
13787 /* If value is a string, output that string literally:
13788 don't check for % within it. */
13789 if (STRINGP (tem))
13790 literal = 1;
13791
13792 if (!EQ (tem, elt))
13793 {
13794 /* Give up right away for nil or t. */
13795 elt = tem;
13796 goto tail_recurse;
13797 }
13798 }
13799 }
13800 break;
13801
13802 case Lisp_Cons:
13803 {
13804 register Lisp_Object car, tem;
13805
13806 /* A cons cell: five distinct cases.
13807 If first element is :eval or :propertize, do something special.
13808 If first element is a string or a cons, process all the elements
13809 and effectively concatenate them.
13810 If first element is a negative number, truncate displaying cdr to
13811 at most that many characters. If positive, pad (with spaces)
13812 to at least that many characters.
13813 If first element is a symbol, process the cadr or caddr recursively
13814 according to whether the symbol's value is non-nil or nil. */
13815 car = XCAR (elt);
13816 if (EQ (car, QCeval))
13817 {
13818 /* An element of the form (:eval FORM) means evaluate FORM
13819 and use the result as mode line elements. */
13820
13821 if (CONSP (XCDR (elt)))
13822 {
13823 Lisp_Object spec;
13824 spec = safe_eval (XCAR (XCDR (elt)));
13825 n += display_mode_element (it, depth, field_width - n,
13826 precision - n, spec, props);
13827 }
13828 }
13829 else if (EQ (car, QCpropertize))
13830 {
13831 if (CONSP (XCDR (elt)))
13832 {
13833 /* An element of the form (:propertize ELT PROPS...)
13834 means display ELT but applying properties PROPS. */
13835 n += display_mode_element (it, depth, field_width - n,
13836 precision - n, XCAR (XCDR (elt)),
13837 XCDR (XCDR (elt)));
13838 }
13839 }
13840 else if (SYMBOLP (car))
13841 {
13842 tem = Fboundp (car);
13843 elt = XCDR (elt);
13844 if (!CONSP (elt))
13845 goto invalid;
13846 /* elt is now the cdr, and we know it is a cons cell.
13847 Use its car if CAR has a non-nil value. */
13848 if (!NILP (tem))
13849 {
13850 tem = Fsymbol_value (car);
13851 if (!NILP (tem))
13852 {
13853 elt = XCAR (elt);
13854 goto tail_recurse;
13855 }
13856 }
13857 /* Symbol's value is nil (or symbol is unbound)
13858 Get the cddr of the original list
13859 and if possible find the caddr and use that. */
13860 elt = XCDR (elt);
13861 if (NILP (elt))
13862 break;
13863 else if (!CONSP (elt))
13864 goto invalid;
13865 elt = XCAR (elt);
13866 goto tail_recurse;
13867 }
13868 else if (INTEGERP (car))
13869 {
13870 register int lim = XINT (car);
13871 elt = XCDR (elt);
13872 if (lim < 0)
13873 {
13874 /* Negative int means reduce maximum width. */
13875 if (precision <= 0)
13876 precision = -lim;
13877 else
13878 precision = min (precision, -lim);
13879 }
13880 else if (lim > 0)
13881 {
13882 /* Padding specified. Don't let it be more than
13883 current maximum. */
13884 if (precision > 0)
13885 lim = min (precision, lim);
13886
13887 /* If that's more padding than already wanted, queue it.
13888 But don't reduce padding already specified even if
13889 that is beyond the current truncation point. */
13890 field_width = max (lim, field_width);
13891 }
13892 goto tail_recurse;
13893 }
13894 else if (STRINGP (car) || CONSP (car))
13895 {
13896 register int limit = 50;
13897 /* Limit is to protect against circular lists. */
13898 while (CONSP (elt)
13899 && --limit > 0
13900 && (precision <= 0 || n < precision))
13901 {
13902 n += display_mode_element (it, depth, field_width - n,
13903 precision - n, XCAR (elt), props);
13904 elt = XCDR (elt);
13905 }
13906 }
13907 }
13908 break;
13909
13910 default:
13911 invalid:
13912 if (frame_title_ptr)
13913 n += store_frame_title ("*invalid*", 0, precision - n);
13914 else
13915 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
13916 precision - n, 0, 0);
13917 return n;
13918 }
13919
13920 /* Pad to FIELD_WIDTH. */
13921 if (field_width > 0 && n < field_width)
13922 {
13923 if (frame_title_ptr)
13924 n += store_frame_title ("", field_width - n, 0);
13925 else
13926 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
13927 0, 0, 0);
13928 }
13929
13930 return n;
13931 }
13932
13933
13934 /* Write a null-terminated, right justified decimal representation of
13935 the positive integer D to BUF using a minimal field width WIDTH. */
13936
13937 static void
13938 pint2str (buf, width, d)
13939 register char *buf;
13940 register int width;
13941 register int d;
13942 {
13943 register char *p = buf;
13944
13945 if (d <= 0)
13946 *p++ = '0';
13947 else
13948 {
13949 while (d > 0)
13950 {
13951 *p++ = d % 10 + '0';
13952 d /= 10;
13953 }
13954 }
13955
13956 for (width -= (int) (p - buf); width > 0; --width)
13957 *p++ = ' ';
13958 *p-- = '\0';
13959 while (p > buf)
13960 {
13961 d = *buf;
13962 *buf++ = *p;
13963 *p-- = d;
13964 }
13965 }
13966
13967 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
13968 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
13969 type of CODING_SYSTEM. Return updated pointer into BUF. */
13970
13971 static unsigned char invalid_eol_type[] = "(*invalid*)";
13972
13973 static char *
13974 decode_mode_spec_coding (coding_system, buf, eol_flag)
13975 Lisp_Object coding_system;
13976 register char *buf;
13977 int eol_flag;
13978 {
13979 Lisp_Object val;
13980 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
13981 unsigned char *eol_str;
13982 int eol_str_len;
13983 /* The EOL conversion we are using. */
13984 Lisp_Object eoltype;
13985
13986 val = CODING_SYSTEM_SPEC (coding_system);
13987 eoltype = Qnil;
13988
13989 if (!VECTORP (val)) /* Not yet decided. */
13990 {
13991 if (multibyte)
13992 *buf++ = '-';
13993 if (eol_flag)
13994 eoltype = eol_mnemonic_undecided;
13995 /* Don't mention EOL conversion if it isn't decided. */
13996 }
13997 else
13998 {
13999 Lisp_Object attrs;
14000 Lisp_Object eolvalue;
14001
14002 attrs = AREF (val, 0);
14003 eolvalue = AREF (val, 2);
14004
14005 if (multibyte)
14006 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
14007
14008 if (eol_flag)
14009 {
14010 /* The EOL conversion that is normal on this system. */
14011
14012 if (NILP (eolvalue)) /* Not yet decided. */
14013 eoltype = eol_mnemonic_undecided;
14014 else if (VECTORP (eolvalue)) /* Not yet decided. */
14015 eoltype = eol_mnemonic_undecided;
14016 else /* eolvalue is Qunix, Qdos, or Qmac. */
14017 eoltype = (EQ (eolvalue, Qunix)
14018 ? eol_mnemonic_unix
14019 : (EQ (eolvalue, Qdos) == 1
14020 ? eol_mnemonic_dos : eol_mnemonic_mac));
14021 }
14022 }
14023
14024 if (eol_flag)
14025 {
14026 /* Mention the EOL conversion if it is not the usual one. */
14027 if (STRINGP (eoltype))
14028 {
14029 eol_str = XSTRING (eoltype)->data;
14030 eol_str_len = XSTRING (eoltype)->size;
14031 }
14032 else if (CHARACTERP (eoltype))
14033 {
14034 eol_str = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
14035 eol_str_len = CHAR_STRING (XINT (eoltype), eol_str);
14036 }
14037 else
14038 {
14039 eol_str = invalid_eol_type;
14040 eol_str_len = sizeof (invalid_eol_type) - 1;
14041 }
14042 bcopy (eol_str, buf, eol_str_len);
14043 buf += eol_str_len;
14044 }
14045
14046 return buf;
14047 }
14048
14049 /* Return a string for the output of a mode line %-spec for window W,
14050 generated by character C. PRECISION >= 0 means don't return a
14051 string longer than that value. FIELD_WIDTH > 0 means pad the
14052 string returned with spaces to that value. Return 1 in *MULTIBYTE
14053 if the result is multibyte text. */
14054
14055 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
14056
14057 static char *
14058 decode_mode_spec (w, c, field_width, precision, multibyte)
14059 struct window *w;
14060 register int c;
14061 int field_width, precision;
14062 int *multibyte;
14063 {
14064 Lisp_Object obj;
14065 struct frame *f = XFRAME (WINDOW_FRAME (w));
14066 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
14067 struct buffer *b = XBUFFER (w->buffer);
14068
14069 obj = Qnil;
14070 *multibyte = 0;
14071
14072 switch (c)
14073 {
14074 case '*':
14075 if (!NILP (b->read_only))
14076 return "%";
14077 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
14078 return "*";
14079 return "-";
14080
14081 case '+':
14082 /* This differs from %* only for a modified read-only buffer. */
14083 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
14084 return "*";
14085 if (!NILP (b->read_only))
14086 return "%";
14087 return "-";
14088
14089 case '&':
14090 /* This differs from %* in ignoring read-only-ness. */
14091 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
14092 return "*";
14093 return "-";
14094
14095 case '%':
14096 return "%";
14097
14098 case '[':
14099 {
14100 int i;
14101 char *p;
14102
14103 if (command_loop_level > 5)
14104 return "[[[... ";
14105 p = decode_mode_spec_buf;
14106 for (i = 0; i < command_loop_level; i++)
14107 *p++ = '[';
14108 *p = 0;
14109 return decode_mode_spec_buf;
14110 }
14111
14112 case ']':
14113 {
14114 int i;
14115 char *p;
14116
14117 if (command_loop_level > 5)
14118 return " ...]]]";
14119 p = decode_mode_spec_buf;
14120 for (i = 0; i < command_loop_level; i++)
14121 *p++ = ']';
14122 *p = 0;
14123 return decode_mode_spec_buf;
14124 }
14125
14126 case '-':
14127 {
14128 register int i;
14129
14130 /* Let lots_of_dashes be a string of infinite length. */
14131 if (field_width <= 0
14132 || field_width > sizeof (lots_of_dashes))
14133 {
14134 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
14135 decode_mode_spec_buf[i] = '-';
14136 decode_mode_spec_buf[i] = '\0';
14137 return decode_mode_spec_buf;
14138 }
14139 else
14140 return lots_of_dashes;
14141 }
14142
14143 case 'b':
14144 obj = b->name;
14145 break;
14146
14147 case 'c':
14148 {
14149 int col = current_column ();
14150 w->column_number_displayed = make_number (col);
14151 pint2str (decode_mode_spec_buf, field_width, col);
14152 return decode_mode_spec_buf;
14153 }
14154
14155 case 'F':
14156 /* %F displays the frame name. */
14157 if (!NILP (f->title))
14158 return (char *) XSTRING (f->title)->data;
14159 if (f->explicit_name || ! FRAME_WINDOW_P (f))
14160 return (char *) XSTRING (f->name)->data;
14161 return "Emacs";
14162
14163 case 'f':
14164 obj = b->filename;
14165 break;
14166
14167 case 'l':
14168 {
14169 int startpos = XMARKER (w->start)->charpos;
14170 int startpos_byte = marker_byte_position (w->start);
14171 int line, linepos, linepos_byte, topline;
14172 int nlines, junk;
14173 int height = XFASTINT (w->height);
14174
14175 /* If we decided that this buffer isn't suitable for line numbers,
14176 don't forget that too fast. */
14177 if (EQ (w->base_line_pos, w->buffer))
14178 goto no_value;
14179 /* But do forget it, if the window shows a different buffer now. */
14180 else if (BUFFERP (w->base_line_pos))
14181 w->base_line_pos = Qnil;
14182
14183 /* If the buffer is very big, don't waste time. */
14184 if (INTEGERP (Vline_number_display_limit)
14185 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
14186 {
14187 w->base_line_pos = Qnil;
14188 w->base_line_number = Qnil;
14189 goto no_value;
14190 }
14191
14192 if (!NILP (w->base_line_number)
14193 && !NILP (w->base_line_pos)
14194 && XFASTINT (w->base_line_pos) <= startpos)
14195 {
14196 line = XFASTINT (w->base_line_number);
14197 linepos = XFASTINT (w->base_line_pos);
14198 linepos_byte = buf_charpos_to_bytepos (b, linepos);
14199 }
14200 else
14201 {
14202 line = 1;
14203 linepos = BUF_BEGV (b);
14204 linepos_byte = BUF_BEGV_BYTE (b);
14205 }
14206
14207 /* Count lines from base line to window start position. */
14208 nlines = display_count_lines (linepos, linepos_byte,
14209 startpos_byte,
14210 startpos, &junk);
14211
14212 topline = nlines + line;
14213
14214 /* Determine a new base line, if the old one is too close
14215 or too far away, or if we did not have one.
14216 "Too close" means it's plausible a scroll-down would
14217 go back past it. */
14218 if (startpos == BUF_BEGV (b))
14219 {
14220 w->base_line_number = make_number (topline);
14221 w->base_line_pos = make_number (BUF_BEGV (b));
14222 }
14223 else if (nlines < height + 25 || nlines > height * 3 + 50
14224 || linepos == BUF_BEGV (b))
14225 {
14226 int limit = BUF_BEGV (b);
14227 int limit_byte = BUF_BEGV_BYTE (b);
14228 int position;
14229 int distance = (height * 2 + 30) * line_number_display_limit_width;
14230
14231 if (startpos - distance > limit)
14232 {
14233 limit = startpos - distance;
14234 limit_byte = CHAR_TO_BYTE (limit);
14235 }
14236
14237 nlines = display_count_lines (startpos, startpos_byte,
14238 limit_byte,
14239 - (height * 2 + 30),
14240 &position);
14241 /* If we couldn't find the lines we wanted within
14242 line_number_display_limit_width chars per line,
14243 give up on line numbers for this window. */
14244 if (position == limit_byte && limit == startpos - distance)
14245 {
14246 w->base_line_pos = w->buffer;
14247 w->base_line_number = Qnil;
14248 goto no_value;
14249 }
14250
14251 w->base_line_number = make_number (topline - nlines);
14252 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
14253 }
14254
14255 /* Now count lines from the start pos to point. */
14256 nlines = display_count_lines (startpos, startpos_byte,
14257 PT_BYTE, PT, &junk);
14258
14259 /* Record that we did display the line number. */
14260 line_number_displayed = 1;
14261
14262 /* Make the string to show. */
14263 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
14264 return decode_mode_spec_buf;
14265 no_value:
14266 {
14267 char* p = decode_mode_spec_buf;
14268 int pad = field_width - 2;
14269 while (pad-- > 0)
14270 *p++ = ' ';
14271 *p++ = '?';
14272 *p++ = '?';
14273 *p = '\0';
14274 return decode_mode_spec_buf;
14275 }
14276 }
14277 break;
14278
14279 case 'm':
14280 obj = b->mode_name;
14281 break;
14282
14283 case 'n':
14284 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
14285 return " Narrow";
14286 break;
14287
14288 case 'p':
14289 {
14290 int pos = marker_position (w->start);
14291 int total = BUF_ZV (b) - BUF_BEGV (b);
14292
14293 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
14294 {
14295 if (pos <= BUF_BEGV (b))
14296 return "All";
14297 else
14298 return "Bottom";
14299 }
14300 else if (pos <= BUF_BEGV (b))
14301 return "Top";
14302 else
14303 {
14304 if (total > 1000000)
14305 /* Do it differently for a large value, to avoid overflow. */
14306 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
14307 else
14308 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
14309 /* We can't normally display a 3-digit number,
14310 so get us a 2-digit number that is close. */
14311 if (total == 100)
14312 total = 99;
14313 sprintf (decode_mode_spec_buf, "%2d%%", total);
14314 return decode_mode_spec_buf;
14315 }
14316 }
14317
14318 /* Display percentage of size above the bottom of the screen. */
14319 case 'P':
14320 {
14321 int toppos = marker_position (w->start);
14322 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
14323 int total = BUF_ZV (b) - BUF_BEGV (b);
14324
14325 if (botpos >= BUF_ZV (b))
14326 {
14327 if (toppos <= BUF_BEGV (b))
14328 return "All";
14329 else
14330 return "Bottom";
14331 }
14332 else
14333 {
14334 if (total > 1000000)
14335 /* Do it differently for a large value, to avoid overflow. */
14336 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
14337 else
14338 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
14339 /* We can't normally display a 3-digit number,
14340 so get us a 2-digit number that is close. */
14341 if (total == 100)
14342 total = 99;
14343 if (toppos <= BUF_BEGV (b))
14344 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
14345 else
14346 sprintf (decode_mode_spec_buf, "%2d%%", total);
14347 return decode_mode_spec_buf;
14348 }
14349 }
14350
14351 case 's':
14352 /* status of process */
14353 obj = Fget_buffer_process (w->buffer);
14354 if (NILP (obj))
14355 return "no process";
14356 #ifdef subprocesses
14357 obj = Fsymbol_name (Fprocess_status (obj));
14358 #endif
14359 break;
14360
14361 case 't': /* indicate TEXT or BINARY */
14362 #ifdef MODE_LINE_BINARY_TEXT
14363 return MODE_LINE_BINARY_TEXT (b);
14364 #else
14365 return "T";
14366 #endif
14367
14368 case 'z':
14369 /* coding-system (not including end-of-line format) */
14370 case 'Z':
14371 /* coding-system (including end-of-line type) */
14372 {
14373 int eol_flag = (c == 'Z');
14374 char *p = decode_mode_spec_buf;
14375
14376 if (! FRAME_WINDOW_P (f))
14377 {
14378 /* No need to mention EOL here--the terminal never needs
14379 to do EOL conversion. */
14380 p = decode_mode_spec_coding (CODING_ID_NAME (keyboard_coding.id),
14381 p, 0);
14382 p = decode_mode_spec_coding (CODING_ID_NAME (terminal_coding.id),
14383 p, 0);
14384 }
14385 p = decode_mode_spec_coding (b->buffer_file_coding_system,
14386 p, eol_flag);
14387
14388 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
14389 #ifdef subprocesses
14390 obj = Fget_buffer_process (Fcurrent_buffer ());
14391 if (PROCESSP (obj))
14392 {
14393 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
14394 p, eol_flag);
14395 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
14396 p, eol_flag);
14397 }
14398 #endif /* subprocesses */
14399 #endif /* 0 */
14400 *p = 0;
14401 return decode_mode_spec_buf;
14402 }
14403 }
14404
14405 if (STRINGP (obj))
14406 {
14407 *multibyte = STRING_MULTIBYTE (obj);
14408 return (char *) XSTRING (obj)->data;
14409 }
14410 else
14411 return "";
14412 }
14413
14414
14415 /* Count up to COUNT lines starting from START / START_BYTE.
14416 But don't go beyond LIMIT_BYTE.
14417 Return the number of lines thus found (always nonnegative).
14418
14419 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
14420
14421 static int
14422 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
14423 int start, start_byte, limit_byte, count;
14424 int *byte_pos_ptr;
14425 {
14426 register unsigned char *cursor;
14427 unsigned char *base;
14428
14429 register int ceiling;
14430 register unsigned char *ceiling_addr;
14431 int orig_count = count;
14432
14433 /* If we are not in selective display mode,
14434 check only for newlines. */
14435 int selective_display = (!NILP (current_buffer->selective_display)
14436 && !INTEGERP (current_buffer->selective_display));
14437
14438 if (count > 0)
14439 {
14440 while (start_byte < limit_byte)
14441 {
14442 ceiling = BUFFER_CEILING_OF (start_byte);
14443 ceiling = min (limit_byte - 1, ceiling);
14444 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
14445 base = (cursor = BYTE_POS_ADDR (start_byte));
14446 while (1)
14447 {
14448 if (selective_display)
14449 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
14450 ;
14451 else
14452 while (*cursor != '\n' && ++cursor != ceiling_addr)
14453 ;
14454
14455 if (cursor != ceiling_addr)
14456 {
14457 if (--count == 0)
14458 {
14459 start_byte += cursor - base + 1;
14460 *byte_pos_ptr = start_byte;
14461 return orig_count;
14462 }
14463 else
14464 if (++cursor == ceiling_addr)
14465 break;
14466 }
14467 else
14468 break;
14469 }
14470 start_byte += cursor - base;
14471 }
14472 }
14473 else
14474 {
14475 while (start_byte > limit_byte)
14476 {
14477 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
14478 ceiling = max (limit_byte, ceiling);
14479 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
14480 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
14481 while (1)
14482 {
14483 if (selective_display)
14484 while (--cursor != ceiling_addr
14485 && *cursor != '\n' && *cursor != 015)
14486 ;
14487 else
14488 while (--cursor != ceiling_addr && *cursor != '\n')
14489 ;
14490
14491 if (cursor != ceiling_addr)
14492 {
14493 if (++count == 0)
14494 {
14495 start_byte += cursor - base + 1;
14496 *byte_pos_ptr = start_byte;
14497 /* When scanning backwards, we should
14498 not count the newline posterior to which we stop. */
14499 return - orig_count - 1;
14500 }
14501 }
14502 else
14503 break;
14504 }
14505 /* Here we add 1 to compensate for the last decrement
14506 of CURSOR, which took it past the valid range. */
14507 start_byte += cursor - base + 1;
14508 }
14509 }
14510
14511 *byte_pos_ptr = limit_byte;
14512
14513 if (count < 0)
14514 return - orig_count + count;
14515 return orig_count - count;
14516
14517 }
14518
14519
14520 \f
14521 /***********************************************************************
14522 Displaying strings
14523 ***********************************************************************/
14524
14525 /* Display a NUL-terminated string, starting with index START.
14526
14527 If STRING is non-null, display that C string. Otherwise, the Lisp
14528 string LISP_STRING is displayed.
14529
14530 If FACE_STRING is not nil, FACE_STRING_POS is a position in
14531 FACE_STRING. Display STRING or LISP_STRING with the face at
14532 FACE_STRING_POS in FACE_STRING:
14533
14534 Display the string in the environment given by IT, but use the
14535 standard display table, temporarily.
14536
14537 FIELD_WIDTH is the minimum number of output glyphs to produce.
14538 If STRING has fewer characters than FIELD_WIDTH, pad to the right
14539 with spaces. If STRING has more characters, more than FIELD_WIDTH
14540 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
14541
14542 PRECISION is the maximum number of characters to output from
14543 STRING. PRECISION < 0 means don't truncate the string.
14544
14545 This is roughly equivalent to printf format specifiers:
14546
14547 FIELD_WIDTH PRECISION PRINTF
14548 ----------------------------------------
14549 -1 -1 %s
14550 -1 10 %.10s
14551 10 -1 %10s
14552 20 10 %20.10s
14553
14554 MULTIBYTE zero means do not display multibyte chars, > 0 means do
14555 display them, and < 0 means obey the current buffer's value of
14556 enable_multibyte_characters.
14557
14558 Value is the number of glyphs produced. */
14559
14560 static int
14561 display_string (string, lisp_string, face_string, face_string_pos,
14562 start, it, field_width, precision, max_x, multibyte)
14563 unsigned char *string;
14564 Lisp_Object lisp_string;
14565 Lisp_Object face_string;
14566 int face_string_pos;
14567 int start;
14568 struct it *it;
14569 int field_width, precision, max_x;
14570 int multibyte;
14571 {
14572 int hpos_at_start = it->hpos;
14573 int saved_face_id = it->face_id;
14574 struct glyph_row *row = it->glyph_row;
14575
14576 /* Initialize the iterator IT for iteration over STRING beginning
14577 with index START. */
14578 reseat_to_string (it, string, lisp_string, start,
14579 precision, field_width, multibyte);
14580
14581 /* If displaying STRING, set up the face of the iterator
14582 from LISP_STRING, if that's given. */
14583 if (STRINGP (face_string))
14584 {
14585 int endptr;
14586 struct face *face;
14587
14588 it->face_id
14589 = face_at_string_position (it->w, face_string, face_string_pos,
14590 0, it->region_beg_charpos,
14591 it->region_end_charpos,
14592 &endptr, it->base_face_id, 0);
14593 face = FACE_FROM_ID (it->f, it->face_id);
14594 it->face_box_p = face->box != FACE_NO_BOX;
14595 }
14596
14597 /* Set max_x to the maximum allowed X position. Don't let it go
14598 beyond the right edge of the window. */
14599 if (max_x <= 0)
14600 max_x = it->last_visible_x;
14601 else
14602 max_x = min (max_x, it->last_visible_x);
14603
14604 /* Skip over display elements that are not visible. because IT->w is
14605 hscrolled. */
14606 if (it->current_x < it->first_visible_x)
14607 move_it_in_display_line_to (it, 100000, it->first_visible_x,
14608 MOVE_TO_POS | MOVE_TO_X);
14609
14610 row->ascent = it->max_ascent;
14611 row->height = it->max_ascent + it->max_descent;
14612 row->phys_ascent = it->max_phys_ascent;
14613 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14614
14615 /* This condition is for the case that we are called with current_x
14616 past last_visible_x. */
14617 while (it->current_x < max_x)
14618 {
14619 int x_before, x, n_glyphs_before, i, nglyphs;
14620
14621 /* Get the next display element. */
14622 if (!get_next_display_element (it))
14623 break;
14624
14625 /* Produce glyphs. */
14626 x_before = it->current_x;
14627 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
14628 PRODUCE_GLYPHS (it);
14629
14630 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
14631 i = 0;
14632 x = x_before;
14633 while (i < nglyphs)
14634 {
14635 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
14636
14637 if (!it->truncate_lines_p
14638 && x + glyph->pixel_width > max_x)
14639 {
14640 /* End of continued line or max_x reached. */
14641 if (CHAR_GLYPH_PADDING_P (*glyph))
14642 {
14643 /* A wide character is unbreakable. */
14644 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
14645 it->current_x = x_before;
14646 }
14647 else
14648 {
14649 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
14650 it->current_x = x;
14651 }
14652 break;
14653 }
14654 else if (x + glyph->pixel_width >= it->first_visible_x)
14655 {
14656 /* Glyph is at least partially visible. */
14657 ++it->hpos;
14658 if (x < it->first_visible_x)
14659 it->glyph_row->x = x - it->first_visible_x;
14660 }
14661 else
14662 {
14663 /* Glyph is off the left margin of the display area.
14664 Should not happen. */
14665 abort ();
14666 }
14667
14668 row->ascent = max (row->ascent, it->max_ascent);
14669 row->height = max (row->height, it->max_ascent + it->max_descent);
14670 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14671 row->phys_height = max (row->phys_height,
14672 it->max_phys_ascent + it->max_phys_descent);
14673 x += glyph->pixel_width;
14674 ++i;
14675 }
14676
14677 /* Stop if max_x reached. */
14678 if (i < nglyphs)
14679 break;
14680
14681 /* Stop at line ends. */
14682 if (ITERATOR_AT_END_OF_LINE_P (it))
14683 {
14684 it->continuation_lines_width = 0;
14685 break;
14686 }
14687
14688 set_iterator_to_next (it, 1);
14689
14690 /* Stop if truncating at the right edge. */
14691 if (it->truncate_lines_p
14692 && it->current_x >= it->last_visible_x)
14693 {
14694 /* Add truncation mark, but don't do it if the line is
14695 truncated at a padding space. */
14696 if (IT_CHARPOS (*it) < it->string_nchars)
14697 {
14698 if (!FRAME_WINDOW_P (it->f))
14699 {
14700 int i, n;
14701
14702 if (it->current_x > it->last_visible_x)
14703 {
14704 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
14705 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
14706 break;
14707 for (n = row->used[TEXT_AREA]; i < n; ++i)
14708 {
14709 row->used[TEXT_AREA] = i;
14710 produce_special_glyphs (it, IT_TRUNCATION);
14711 }
14712 }
14713 produce_special_glyphs (it, IT_TRUNCATION);
14714 }
14715 it->glyph_row->truncated_on_right_p = 1;
14716 }
14717 break;
14718 }
14719 }
14720
14721 /* Maybe insert a truncation at the left. */
14722 if (it->first_visible_x
14723 && IT_CHARPOS (*it) > 0)
14724 {
14725 if (!FRAME_WINDOW_P (it->f))
14726 insert_left_trunc_glyphs (it);
14727 it->glyph_row->truncated_on_left_p = 1;
14728 }
14729
14730 it->face_id = saved_face_id;
14731
14732 /* Value is number of columns displayed. */
14733 return it->hpos - hpos_at_start;
14734 }
14735
14736
14737 \f
14738 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
14739 appears as an element of LIST or as the car of an element of LIST.
14740 If PROPVAL is a list, compare each element against LIST in that
14741 way, and return 1/2 if any element of PROPVAL is found in LIST.
14742 Otherwise return 0. This function cannot quit.
14743 The return value is 2 if the text is invisible but with an ellipsis
14744 and 1 if it's invisible and without an ellipsis. */
14745
14746 int
14747 invisible_p (propval, list)
14748 register Lisp_Object propval;
14749 Lisp_Object list;
14750 {
14751 register Lisp_Object tail, proptail;
14752
14753 for (tail = list; CONSP (tail); tail = XCDR (tail))
14754 {
14755 register Lisp_Object tem;
14756 tem = XCAR (tail);
14757 if (EQ (propval, tem))
14758 return 1;
14759 if (CONSP (tem) && EQ (propval, XCAR (tem)))
14760 return NILP (XCDR (tem)) ? 1 : 2;
14761 }
14762
14763 if (CONSP (propval))
14764 {
14765 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
14766 {
14767 Lisp_Object propelt;
14768 propelt = XCAR (proptail);
14769 for (tail = list; CONSP (tail); tail = XCDR (tail))
14770 {
14771 register Lisp_Object tem;
14772 tem = XCAR (tail);
14773 if (EQ (propelt, tem))
14774 return 1;
14775 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
14776 return NILP (XCDR (tem)) ? 1 : 2;
14777 }
14778 }
14779 }
14780
14781 return 0;
14782 }
14783
14784 \f
14785 /***********************************************************************
14786 Initialization
14787 ***********************************************************************/
14788
14789 void
14790 syms_of_xdisp ()
14791 {
14792 Vwith_echo_area_save_vector = Qnil;
14793 staticpro (&Vwith_echo_area_save_vector);
14794
14795 Vmessage_stack = Qnil;
14796 staticpro (&Vmessage_stack);
14797
14798 Qinhibit_redisplay = intern ("inhibit-redisplay");
14799 staticpro (&Qinhibit_redisplay);
14800
14801 message_dolog_marker1 = Fmake_marker ();
14802 staticpro (&message_dolog_marker1);
14803 message_dolog_marker2 = Fmake_marker ();
14804 staticpro (&message_dolog_marker2);
14805 message_dolog_marker3 = Fmake_marker ();
14806 staticpro (&message_dolog_marker3);
14807
14808 #if GLYPH_DEBUG
14809 defsubr (&Sdump_glyph_matrix);
14810 defsubr (&Sdump_glyph_row);
14811 defsubr (&Sdump_tool_bar_row);
14812 defsubr (&Strace_redisplay);
14813 defsubr (&Strace_to_stderr);
14814 #endif
14815 #ifdef HAVE_WINDOW_SYSTEM
14816 defsubr (&Stool_bar_lines_needed);
14817 #endif
14818
14819 staticpro (&Qmenu_bar_update_hook);
14820 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
14821
14822 staticpro (&Qoverriding_terminal_local_map);
14823 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
14824
14825 staticpro (&Qoverriding_local_map);
14826 Qoverriding_local_map = intern ("overriding-local-map");
14827
14828 staticpro (&Qwindow_scroll_functions);
14829 Qwindow_scroll_functions = intern ("window-scroll-functions");
14830
14831 staticpro (&Qredisplay_end_trigger_functions);
14832 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
14833
14834 staticpro (&Qinhibit_point_motion_hooks);
14835 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
14836
14837 QCdata = intern (":data");
14838 staticpro (&QCdata);
14839 Qdisplay = intern ("display");
14840 staticpro (&Qdisplay);
14841 Qspace_width = intern ("space-width");
14842 staticpro (&Qspace_width);
14843 Qraise = intern ("raise");
14844 staticpro (&Qraise);
14845 Qspace = intern ("space");
14846 staticpro (&Qspace);
14847 Qmargin = intern ("margin");
14848 staticpro (&Qmargin);
14849 Qleft_margin = intern ("left-margin");
14850 staticpro (&Qleft_margin);
14851 Qright_margin = intern ("right-margin");
14852 staticpro (&Qright_margin);
14853 Qalign_to = intern ("align-to");
14854 staticpro (&Qalign_to);
14855 QCalign_to = intern (":align-to");
14856 staticpro (&QCalign_to);
14857 Qrelative_width = intern ("relative-width");
14858 staticpro (&Qrelative_width);
14859 QCrelative_width = intern (":relative-width");
14860 staticpro (&QCrelative_width);
14861 QCrelative_height = intern (":relative-height");
14862 staticpro (&QCrelative_height);
14863 QCeval = intern (":eval");
14864 staticpro (&QCeval);
14865 QCpropertize = intern (":propertize");
14866 staticpro (&QCpropertize);
14867 Qwhen = intern ("when");
14868 staticpro (&Qwhen);
14869 QCfile = intern (":file");
14870 staticpro (&QCfile);
14871 Qfontified = intern ("fontified");
14872 staticpro (&Qfontified);
14873 Qfontification_functions = intern ("fontification-functions");
14874 staticpro (&Qfontification_functions);
14875 Qtrailing_whitespace = intern ("trailing-whitespace");
14876 staticpro (&Qtrailing_whitespace);
14877 Qimage = intern ("image");
14878 staticpro (&Qimage);
14879 Qmessage_truncate_lines = intern ("message-truncate-lines");
14880 staticpro (&Qmessage_truncate_lines);
14881 Qcursor_in_non_selected_windows = intern ("cursor-in-non-selected-windows");
14882 staticpro (&Qcursor_in_non_selected_windows);
14883 Qgrow_only = intern ("grow-only");
14884 staticpro (&Qgrow_only);
14885 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
14886 staticpro (&Qinhibit_menubar_update);
14887 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
14888 staticpro (&Qinhibit_eval_during_redisplay);
14889 Qposition = intern ("position");
14890 staticpro (&Qposition);
14891 Qbuffer_position = intern ("buffer-position");
14892 staticpro (&Qbuffer_position);
14893 Qobject = intern ("object");
14894 staticpro (&Qobject);
14895
14896 last_arrow_position = Qnil;
14897 last_arrow_string = Qnil;
14898 staticpro (&last_arrow_position);
14899 staticpro (&last_arrow_string);
14900
14901 echo_buffer[0] = echo_buffer[1] = Qnil;
14902 staticpro (&echo_buffer[0]);
14903 staticpro (&echo_buffer[1]);
14904
14905 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
14906 staticpro (&echo_area_buffer[0]);
14907 staticpro (&echo_area_buffer[1]);
14908
14909 Vmessages_buffer_name = build_string ("*Messages*");
14910 staticpro (&Vmessages_buffer_name);
14911
14912 mode_line_proptrans_alist = Qnil;
14913 staticpro (&mode_line_proptrans_alist);
14914
14915 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
14916 doc: /* Non-nil means highlight trailing whitespace.
14917 The face used for trailing whitespace is `trailing-whitespace'. */);
14918 Vshow_trailing_whitespace = Qnil;
14919
14920 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
14921 doc: /* Non-nil means don't actually do any redisplay.
14922 This is used for internal purposes. */);
14923 Vinhibit_redisplay = Qnil;
14924
14925 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
14926 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
14927 Vglobal_mode_string = Qnil;
14928
14929 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
14930 doc: /* Marker for where to display an arrow on top of the buffer text.
14931 This must be the beginning of a line in order to work.
14932 See also `overlay-arrow-string'. */);
14933 Voverlay_arrow_position = Qnil;
14934
14935 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
14936 doc: /* String to display as an arrow. See also `overlay-arrow-position'. */);
14937 Voverlay_arrow_string = Qnil;
14938
14939 DEFVAR_INT ("scroll-step", &scroll_step,
14940 doc: /* *The number of lines to try scrolling a window by when point moves out.
14941 If that fails to bring point back on frame, point is centered instead.
14942 If this is zero, point is always centered after it moves off frame.
14943 If you want scrolling to always be a line at a time, you should set
14944 `scroll-conservatively' to a large value rather than set this to 1. */);
14945
14946 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
14947 doc: /* *Scroll up to this many lines, to bring point back on screen.
14948 A value of zero means to scroll the text to center point vertically
14949 in the window. */);
14950 scroll_conservatively = 0;
14951
14952 DEFVAR_INT ("scroll-margin", &scroll_margin,
14953 doc: /* *Number of lines of margin at the top and bottom of a window.
14954 Recenter the window whenever point gets within this many lines
14955 of the top or bottom of the window. */);
14956 scroll_margin = 0;
14957
14958 #if GLYPH_DEBUG
14959 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
14960 #endif
14961
14962 DEFVAR_BOOL ("truncate-partial-width-windows",
14963 &truncate_partial_width_windows,
14964 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
14965 truncate_partial_width_windows = 1;
14966
14967 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
14968 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
14969 Any other value means to use the appropriate face, `mode-line',
14970 `header-line', or `menu' respectively.
14971
14972 This variable is deprecated; please change the above faces instead. */);
14973 mode_line_inverse_video = 1;
14974
14975 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
14976 doc: /* *Maximum buffer size for which line number should be displayed.
14977 If the buffer is bigger than this, the line number does not appear
14978 in the mode line. A value of nil means no limit. */);
14979 Vline_number_display_limit = Qnil;
14980
14981 DEFVAR_INT ("line-number-display-limit-width",
14982 &line_number_display_limit_width,
14983 doc: /* *Maximum line width (in characters) for line number display.
14984 If the average length of the lines near point is bigger than this, then the
14985 line number may be omitted from the mode line. */);
14986 line_number_display_limit_width = 200;
14987
14988 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
14989 doc: /* *Non-nil means highlight region even in nonselected windows. */);
14990 highlight_nonselected_windows = 0;
14991
14992 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
14993 doc: /* Non-nil if more than one frame is visible on this display.
14994 Minibuffer-only frames don't count, but iconified frames do.
14995 This variable is not guaranteed to be accurate except while processing
14996 `frame-title-format' and `icon-title-format'. */);
14997
14998 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
14999 doc: /* Template for displaying the title bar of visible frames.
15000 \(Assuming the window manager supports this feature.)
15001 This variable has the same structure as `mode-line-format' (which see),
15002 and is used only on frames for which no explicit name has been set
15003 \(see `modify-frame-parameters'). */);
15004 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
15005 doc: /* Template for displaying the title bar of an iconified frame.
15006 \(Assuming the window manager supports this feature.)
15007 This variable has the same structure as `mode-line-format' (which see),
15008 and is used only on frames for which no explicit name has been set
15009 \(see `modify-frame-parameters'). */);
15010 Vicon_title_format
15011 = Vframe_title_format
15012 = Fcons (intern ("multiple-frames"),
15013 Fcons (build_string ("%b"),
15014 Fcons (Fcons (empty_string,
15015 Fcons (intern ("invocation-name"),
15016 Fcons (build_string ("@"),
15017 Fcons (intern ("system-name"),
15018 Qnil)))),
15019 Qnil)));
15020
15021 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
15022 doc: /* Maximum number of lines to keep in the message log buffer.
15023 If nil, disable message logging. If t, log messages but don't truncate
15024 the buffer when it becomes large. */);
15025 Vmessage_log_max = make_number (50);
15026
15027 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
15028 doc: /* Functions called before redisplay, if window sizes have changed.
15029 The value should be a list of functions that take one argument.
15030 Just before redisplay, for each frame, if any of its windows have changed
15031 size since the last redisplay, or have been split or deleted,
15032 all the functions in the list are called, with the frame as argument. */);
15033 Vwindow_size_change_functions = Qnil;
15034
15035 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
15036 doc: /* List of Functions to call before redisplaying a window with scrolling.
15037 Each function is called with two arguments, the window
15038 and its new display-start position. Note that the value of `window-end'
15039 is not valid when these functions are called. */);
15040 Vwindow_scroll_functions = Qnil;
15041
15042 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
15043 doc: /* *Non-nil means automatically resize tool-bars.
15044 This increases a tool-bar's height if not all tool-bar items are visible.
15045 It decreases a tool-bar's height when it would display blank lines
15046 otherwise. */);
15047 auto_resize_tool_bars_p = 1;
15048
15049 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
15050 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
15051 auto_raise_tool_bar_buttons_p = 1;
15052
15053 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
15054 doc: /* *Margin around tool-bar buttons in pixels.
15055 If an integer, use that for both horizontal and vertical margins.
15056 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
15057 HORZ specifying the horizontal margin, and VERT specifying the
15058 vertical margin. */);
15059 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
15060
15061 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
15062 doc: /* *Relief thickness of tool-bar buttons. */);
15063 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
15064
15065 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
15066 doc: /* List of functions to call to fontify regions of text.
15067 Each function is called with one argument POS. Functions must
15068 fontify a region starting at POS in the current buffer, and give
15069 fontified regions the property `fontified'. */);
15070 Vfontification_functions = Qnil;
15071 Fmake_variable_buffer_local (Qfontification_functions);
15072
15073 DEFVAR_BOOL ("unibyte-display-via-language-environment",
15074 &unibyte_display_via_language_environment,
15075 doc: /* *Non-nil means display unibyte text according to language environment.
15076 Specifically this means that unibyte non-ASCII characters
15077 are displayed by converting them to the equivalent multibyte characters
15078 according to the current language environment. As a result, they are
15079 displayed according to the current fontset. */);
15080 unibyte_display_via_language_environment = 0;
15081
15082 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
15083 doc: /* *Maximum height for resizing mini-windows.
15084 If a float, it specifies a fraction of the mini-window frame's height.
15085 If an integer, it specifies a number of lines. */);
15086 Vmax_mini_window_height = make_float (0.25);
15087
15088 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
15089 doc: /* *How to resize mini-windows.
15090 A value of nil means don't automatically resize mini-windows.
15091 A value of t means resize them to fit the text displayed in them.
15092 A value of `grow-only', the default, means let mini-windows grow
15093 only, until their display becomes empty, at which point the windows
15094 go back to their normal size. */);
15095 Vresize_mini_windows = Qgrow_only;
15096
15097 DEFVAR_BOOL ("cursor-in-non-selected-windows",
15098 &cursor_in_non_selected_windows,
15099 doc: /* *Non-nil means display a hollow cursor in non-selected windows.
15100 nil means don't display a cursor there. */);
15101 cursor_in_non_selected_windows = 1;
15102
15103 DEFVAR_BOOL ("automatic-hscrolling", &automatic_hscrolling_p,
15104 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
15105 automatic_hscrolling_p = 1;
15106
15107 DEFVAR_INT ("automatic-hscroll-margin", &automatic_hscroll_margin,
15108 doc: /* *How many columns away from the window edge point is allowed to get
15109 before automatic hscrolling will horizontally scroll the window. */);
15110 automatic_hscroll_margin = 5;
15111
15112 DEFVAR_LISP ("automatic-hscroll-step", &Vautomatic_hscroll_step,
15113 doc: /* *How many columns to scroll the window when point gets too close to the edge.
15114 When point is less than `automatic-hscroll-margin' columns from the window
15115 edge, automatic hscrolling will scroll the window by the amount of columns
15116 determined by this variable. If its value is a positive integer, scroll that
15117 many columns. If it's a positive floating-point number, it specifies the
15118 fraction of the window's width to scroll. If it's nil or zero, point will be
15119 centered horizontally after the scroll. Any other value, including negative
15120 numbers, are treated as if the value were zero.
15121
15122 Automatic hscrolling always moves point outside the scroll margin, so if
15123 point was more than scroll step columns inside the margin, the window will
15124 scroll more than the value given by the scroll step.
15125
15126 Note that the lower bound for automatic hscrolling specified by `scroll-left'
15127 and `scroll-right' overrides this variable's effect. */);
15128 Vautomatic_hscroll_step = make_number (0);
15129
15130 DEFVAR_LISP ("image-types", &Vimage_types,
15131 doc: /* List of supported image types.
15132 Each element of the list is a symbol for a supported image type. */);
15133 Vimage_types = Qnil;
15134
15135 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
15136 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
15137 Bind this around calls to `message' to let it take effect. */);
15138 message_truncate_lines = 0;
15139
15140 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
15141 doc: /* Normal hook run for clicks on menu bar, before displaying a submenu.
15142 Can be used to update submenus whose contents should vary. */);
15143 Vmenu_bar_update_hook = Qnil;
15144
15145 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
15146 doc: /* Non-nil means don't update menu bars. Internal use only. */);
15147 inhibit_menubar_update = 0;
15148
15149 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
15150 doc: /* Non-nil means don't eval Lisp during redisplay. */);
15151 inhibit_eval_during_redisplay = 0;
15152
15153 #if GLYPH_DEBUG
15154 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
15155 doc: /* Inhibit try_window_id display optimization. */);
15156 inhibit_try_window_id = 0;
15157
15158 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
15159 doc: /* Inhibit try_window_reusing display optimization. */);
15160 inhibit_try_window_reusing = 0;
15161
15162 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
15163 doc: /* Inhibit try_cursor_movement display optimization. */);
15164 inhibit_try_cursor_movement = 0;
15165 #endif /* GLYPH_DEBUG */
15166 }
15167
15168
15169 /* Initialize this module when Emacs starts. */
15170
15171 void
15172 init_xdisp ()
15173 {
15174 Lisp_Object root_window;
15175 struct window *mini_w;
15176
15177 current_header_line_height = current_mode_line_height = -1;
15178
15179 CHARPOS (this_line_start_pos) = 0;
15180
15181 mini_w = XWINDOW (minibuf_window);
15182 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
15183
15184 if (!noninteractive)
15185 {
15186 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
15187 int i;
15188
15189 XWINDOW (root_window)->top = make_number (FRAME_TOP_MARGIN (f));
15190 set_window_height (root_window,
15191 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
15192 0);
15193 mini_w->top = make_number (FRAME_HEIGHT (f) - 1);
15194 set_window_height (minibuf_window, 1, 0);
15195
15196 XWINDOW (root_window)->width = make_number (FRAME_WIDTH (f));
15197 mini_w->width = make_number (FRAME_WIDTH (f));
15198
15199 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
15200 scratch_glyph_row.glyphs[TEXT_AREA + 1]
15201 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
15202
15203 /* The default ellipsis glyphs `...'. */
15204 for (i = 0; i < 3; ++i)
15205 default_invis_vector[i] = make_number ('.');
15206 }
15207
15208 #ifdef HAVE_WINDOW_SYSTEM
15209 {
15210 /* Allocate the buffer for frame titles. */
15211 int size = 100;
15212 frame_title_buf = (char *) xmalloc (size);
15213 frame_title_buf_end = frame_title_buf + size;
15214 frame_title_ptr = NULL;
15215 }
15216 #endif /* HAVE_WINDOW_SYSTEM */
15217
15218 help_echo_showing_p = 0;
15219 }
15220
15221