]> code.delx.au - gnu-emacs/blob - src/xdisp.c
(store_frame_title_char) [PROTOTYPES]: Provide ISO C
[gnu-emacs] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 86, 87, 88, 93, 94, 95, 97, 98, 99, 2000, 2001, 2002
3 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
23
24 Redisplay.
25
26 Emacs separates the task of updating the display from code
27 modifying global state, e.g. buffer text. This way functions
28 operating on buffers don't also have to be concerned with updating
29 the display.
30
31 Updating the display is triggered by the Lisp interpreter when it
32 decides it's time to do it. This is done either automatically for
33 you as part of the interpreter's command loop or as the result of
34 calling Lisp functions like `sit-for'. The C function `redisplay'
35 in xdisp.c is the only entry into the inner redisplay code. (Or,
36 let's say almost---see the description of direct update
37 operations, below.)
38
39 The following diagram shows how redisplay code is invoked. As you
40 can see, Lisp calls redisplay and vice versa. Under window systems
41 like X, some portions of the redisplay code are also called
42 asynchronously during mouse movement or expose events. It is very
43 important that these code parts do NOT use the C library (malloc,
44 free) because many C libraries under Unix are not reentrant. They
45 may also NOT call functions of the Lisp interpreter which could
46 change the interpreter's state. If you don't follow these rules,
47 you will encounter bugs which are very hard to explain.
48
49 (Direct functions, see below)
50 direct_output_for_insert,
51 direct_forward_char (dispnew.c)
52 +---------------------------------+
53 | |
54 | V
55 +--------------+ redisplay +----------------+
56 | Lisp machine |---------------->| Redisplay code |<--+
57 +--------------+ (xdisp.c) +----------------+ |
58 ^ | |
59 +----------------------------------+ |
60 Don't use this path when called |
61 asynchronously! |
62 |
63 expose_window (asynchronous) |
64 |
65 X expose events -----+
66
67 What does redisplay do? 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 an iterator structure (struct it)
124 argument.
125
126 Iteration over things to be displayed is then simple. It is
127 started by initializing an iterator with a call to init_iterator.
128 Calls to get_next_display_element fill the iterator structure with
129 relevant information about the next thing to display. Calls to
130 set_iterator_to_next move the iterator to the next thing.
131
132 Besides this, an iterator also contains information about the
133 display environment in which glyphs for display elements are to be
134 produced. It has fields for the width and height of the display,
135 the information whether long lines are truncated or continued, a
136 current X and Y position, and lots of other stuff you can better
137 see in dispextern.h.
138
139 Glyphs in a desired matrix are normally constructed in a loop
140 calling get_next_display_element and then produce_glyphs. The call
141 to produce_glyphs will fill the iterator structure with pixel
142 information about the element being displayed and at the same time
143 produce glyphs for it. If the display element fits on the line
144 being displayed, set_iterator_to_next is called next, otherwise the
145 glyphs produced are discarded.
146
147
148 Frame matrices.
149
150 That just couldn't be all, could it? What about terminal types not
151 supporting operations on sub-windows of the screen? To update the
152 display on such a terminal, window-based glyph matrices are not
153 well suited. To be able to reuse part of the display (scrolling
154 lines up and down), we must instead have a view of the whole
155 screen. This is what `frame matrices' are for. They are a trick.
156
157 Frames on terminals like above have a glyph pool. Windows on such
158 a frame sub-allocate their glyph memory from their frame's glyph
159 pool. The frame itself is given its own glyph matrices. By
160 coincidence---or maybe something else---rows in window glyph
161 matrices are slices of corresponding rows in frame matrices. Thus
162 writing to window matrices implicitly updates a frame matrix which
163 provides us with the view of the whole screen that we originally
164 wanted to have without having to move many bytes around. To be
165 honest, there is a little bit more done, but not much more. If you
166 plan to extend that code, take a look at dispnew.c. The function
167 build_frame_matrix is a good starting point. */
168
169 #include <config.h>
170 #include <stdio.h>
171
172 #include "lisp.h"
173 #include "keyboard.h"
174 #include "frame.h"
175 #include "window.h"
176 #include "termchar.h"
177 #include "dispextern.h"
178 #include "buffer.h"
179 #include "charset.h"
180 #include "indent.h"
181 #include "commands.h"
182 #include "macros.h"
183 #include "disptab.h"
184 #include "termhooks.h"
185 #include "intervals.h"
186 #include "coding.h"
187 #include "process.h"
188 #include "region-cache.h"
189 #include "fontset.h"
190
191 #ifdef HAVE_X_WINDOWS
192 #include "xterm.h"
193 #endif
194 #ifdef WINDOWSNT
195 #include "w32term.h"
196 #endif
197 #ifdef MAC_OS
198 #include "macterm.h"
199 #endif
200
201 #define INFINITY 10000000
202
203 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS)
204 extern void set_frame_menubar P_ ((struct frame *f, int, int));
205 extern int pending_menu_activation;
206 #endif
207
208 extern int interrupt_input;
209 extern int command_loop_level;
210
211 extern int minibuffer_auto_raise;
212
213 extern Lisp_Object Qface;
214 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
215
216 extern Lisp_Object Voverriding_local_map;
217 extern Lisp_Object Voverriding_local_map_menu_flag;
218 extern Lisp_Object Qmenu_item;
219
220 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
221 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
222 Lisp_Object Qredisplay_end_trigger_functions;
223 Lisp_Object Qinhibit_point_motion_hooks;
224 Lisp_Object QCeval, Qwhen, QCfile, QCdata, QCpropertize;
225 Lisp_Object Qfontified;
226 Lisp_Object Qgrow_only;
227 Lisp_Object Qinhibit_eval_during_redisplay;
228 Lisp_Object Qbuffer_position, Qposition, Qobject;
229
230 /* Cursor shapes */
231 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
232
233 Lisp_Object Qrisky_local_variable;
234
235 /* Holds the list (error). */
236 Lisp_Object list_of_error;
237
238 /* Functions called to fontify regions of text. */
239
240 Lisp_Object Vfontification_functions;
241 Lisp_Object Qfontification_functions;
242
243 /* Non-zero means draw tool bar buttons raised when the mouse moves
244 over them. */
245
246 int auto_raise_tool_bar_buttons_p;
247
248 /* Margin around tool bar buttons in pixels. */
249
250 Lisp_Object Vtool_bar_button_margin;
251
252 /* Thickness of shadow to draw around tool bar buttons. */
253
254 EMACS_INT tool_bar_button_relief;
255
256 /* Non-zero means automatically resize tool-bars so that all tool-bar
257 items are visible, and no blank lines remain. */
258
259 int auto_resize_tool_bars_p;
260
261 /* Non-nil means don't actually do any redisplay. */
262
263 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
264
265 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
266
267 int inhibit_eval_during_redisplay;
268
269 /* Names of text properties relevant for redisplay. */
270
271 Lisp_Object Qdisplay, Qrelative_width, Qalign_to;
272 extern Lisp_Object Qface, Qinvisible, Qwidth;
273
274 /* Symbols used in text property values. */
275
276 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
277 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
278 Lisp_Object Qmargin;
279 extern Lisp_Object Qheight;
280
281 /* Non-nil means highlight trailing whitespace. */
282
283 Lisp_Object Vshow_trailing_whitespace;
284
285 /* Name of the face used to highlight trailing whitespace. */
286
287 Lisp_Object Qtrailing_whitespace;
288
289 /* The symbol `image' which is the car of the lists used to represent
290 images in Lisp. */
291
292 Lisp_Object Qimage;
293
294 /* Non-zero means print newline to stdout before next mini-buffer
295 message. */
296
297 int noninteractive_need_newline;
298
299 /* Non-zero means print newline to message log before next message. */
300
301 static int message_log_need_newline;
302
303 /* Three markers that message_dolog uses.
304 It could allocate them itself, but that causes trouble
305 in handling memory-full errors. */
306 static Lisp_Object message_dolog_marker1;
307 static Lisp_Object message_dolog_marker2;
308 static Lisp_Object message_dolog_marker3;
309 \f
310 /* The buffer position of the first character appearing entirely or
311 partially on the line of the selected window which contains the
312 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
313 redisplay optimization in redisplay_internal. */
314
315 static struct text_pos this_line_start_pos;
316
317 /* Number of characters past the end of the line above, including the
318 terminating newline. */
319
320 static struct text_pos this_line_end_pos;
321
322 /* The vertical positions and the height of this line. */
323
324 static int this_line_vpos;
325 static int this_line_y;
326 static int this_line_pixel_height;
327
328 /* X position at which this display line starts. Usually zero;
329 negative if first character is partially visible. */
330
331 static int this_line_start_x;
332
333 /* Buffer that this_line_.* variables are referring to. */
334
335 static struct buffer *this_line_buffer;
336
337 /* Nonzero means truncate lines in all windows less wide than the
338 frame. */
339
340 int truncate_partial_width_windows;
341
342 /* A flag to control how to display unibyte 8-bit character. */
343
344 int unibyte_display_via_language_environment;
345
346 /* Nonzero means we have more than one non-mini-buffer-only frame.
347 Not guaranteed to be accurate except while parsing
348 frame-title-format. */
349
350 int multiple_frames;
351
352 Lisp_Object Vglobal_mode_string;
353
354 /* Marker for where to display an arrow on top of the buffer text. */
355
356 Lisp_Object Voverlay_arrow_position;
357
358 /* String to display for the arrow. Only used on terminal frames. */
359
360 Lisp_Object Voverlay_arrow_string;
361
362 /* Values of those variables at last redisplay. However, if
363 Voverlay_arrow_position is a marker, last_arrow_position is its
364 numerical position. */
365
366 static Lisp_Object last_arrow_position, last_arrow_string;
367
368 /* Like mode-line-format, but for the title bar on a visible frame. */
369
370 Lisp_Object Vframe_title_format;
371
372 /* Like mode-line-format, but for the title bar on an iconified frame. */
373
374 Lisp_Object Vicon_title_format;
375
376 /* List of functions to call when a window's size changes. These
377 functions get one arg, a frame on which one or more windows' sizes
378 have changed. */
379
380 static Lisp_Object Vwindow_size_change_functions;
381
382 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
383
384 /* Nonzero if overlay arrow has been displayed once in this window. */
385
386 static int overlay_arrow_seen;
387
388 /* Nonzero means highlight the region even in nonselected windows. */
389
390 int highlight_nonselected_windows;
391
392 /* If cursor motion alone moves point off frame, try scrolling this
393 many lines up or down if that will bring it back. */
394
395 static EMACS_INT scroll_step;
396
397 /* Nonzero means scroll just far enough to bring point back on the
398 screen, when appropriate. */
399
400 static EMACS_INT scroll_conservatively;
401
402 /* Recenter the window whenever point gets within this many lines of
403 the top or bottom of the window. This value is translated into a
404 pixel value by multiplying it with CANON_Y_UNIT, which means that
405 there is really a fixed pixel height scroll margin. */
406
407 EMACS_INT scroll_margin;
408
409 /* Number of windows showing the buffer of the selected window (or
410 another buffer with the same base buffer). keyboard.c refers to
411 this. */
412
413 int buffer_shared;
414
415 /* Vector containing glyphs for an ellipsis `...'. */
416
417 static Lisp_Object default_invis_vector[3];
418
419 /* Zero means display the mode-line/header-line/menu-bar in the default face
420 (this slightly odd definition is for compatibility with previous versions
421 of emacs), non-zero means display them using their respective faces.
422
423 This variable is deprecated. */
424
425 int mode_line_inverse_video;
426
427 /* Prompt to display in front of the mini-buffer contents. */
428
429 Lisp_Object minibuf_prompt;
430
431 /* Width of current mini-buffer prompt. Only set after display_line
432 of the line that contains the prompt. */
433
434 int minibuf_prompt_width;
435
436 /* This is the window where the echo area message was displayed. It
437 is always a mini-buffer window, but it may not be the same window
438 currently active as a mini-buffer. */
439
440 Lisp_Object echo_area_window;
441
442 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
443 pushes the current message and the value of
444 message_enable_multibyte on the stack, the function restore_message
445 pops the stack and displays MESSAGE again. */
446
447 Lisp_Object Vmessage_stack;
448
449 /* Nonzero means multibyte characters were enabled when the echo area
450 message was specified. */
451
452 int message_enable_multibyte;
453
454 /* Nonzero if we should redraw the mode lines on the next redisplay. */
455
456 int update_mode_lines;
457
458 /* Nonzero if window sizes or contents have changed since last
459 redisplay that finished. */
460
461 int windows_or_buffers_changed;
462
463 /* Nonzero means a frame's cursor type has been changed. */
464
465 int cursor_type_changed;
466
467 /* Nonzero after display_mode_line if %l was used and it displayed a
468 line number. */
469
470 int line_number_displayed;
471
472 /* Maximum buffer size for which to display line numbers. */
473
474 Lisp_Object Vline_number_display_limit;
475
476 /* Line width to consider when repositioning for line number display. */
477
478 static EMACS_INT line_number_display_limit_width;
479
480 /* Number of lines to keep in the message log buffer. t means
481 infinite. nil means don't log at all. */
482
483 Lisp_Object Vmessage_log_max;
484
485 /* The name of the *Messages* buffer, a string. */
486
487 static Lisp_Object Vmessages_buffer_name;
488
489 /* Current, index 0, and last displayed echo area message. Either
490 buffers from echo_buffers, or nil to indicate no message. */
491
492 Lisp_Object echo_area_buffer[2];
493
494 /* The buffers referenced from echo_area_buffer. */
495
496 static Lisp_Object echo_buffer[2];
497
498 /* A vector saved used in with_area_buffer to reduce consing. */
499
500 static Lisp_Object Vwith_echo_area_save_vector;
501
502 /* Non-zero means display_echo_area should display the last echo area
503 message again. Set by redisplay_preserve_echo_area. */
504
505 static int display_last_displayed_message_p;
506
507 /* Nonzero if echo area is being used by print; zero if being used by
508 message. */
509
510 int message_buf_print;
511
512 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
513
514 Lisp_Object Qinhibit_menubar_update;
515 int inhibit_menubar_update;
516
517 /* Maximum height for resizing mini-windows. Either a float
518 specifying a fraction of the available height, or an integer
519 specifying a number of lines. */
520
521 Lisp_Object Vmax_mini_window_height;
522
523 /* Non-zero means messages should be displayed with truncated
524 lines instead of being continued. */
525
526 int message_truncate_lines;
527 Lisp_Object Qmessage_truncate_lines;
528
529 /* Set to 1 in clear_message to make redisplay_internal aware
530 of an emptied echo area. */
531
532 static int message_cleared_p;
533
534 /* Non-zero means we want a hollow cursor in windows that are not
535 selected. Zero means there's no cursor in such windows. */
536
537 Lisp_Object Vcursor_in_non_selected_windows;
538 Lisp_Object Qcursor_in_non_selected_windows;
539
540 /* How to blink the default frame cursor off. */
541 Lisp_Object Vblink_cursor_alist;
542
543 /* A scratch glyph row with contents used for generating truncation
544 glyphs. Also used in direct_output_for_insert. */
545
546 #define MAX_SCRATCH_GLYPHS 100
547 struct glyph_row scratch_glyph_row;
548 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
549
550 /* Ascent and height of the last line processed by move_it_to. */
551
552 static int last_max_ascent, last_height;
553
554 /* Non-zero if there's a help-echo in the echo area. */
555
556 int help_echo_showing_p;
557
558 /* If >= 0, computed, exact values of mode-line and header-line height
559 to use in the macros CURRENT_MODE_LINE_HEIGHT and
560 CURRENT_HEADER_LINE_HEIGHT. */
561
562 int current_mode_line_height, current_header_line_height;
563
564 /* The maximum distance to look ahead for text properties. Values
565 that are too small let us call compute_char_face and similar
566 functions too often which is expensive. Values that are too large
567 let us call compute_char_face and alike too often because we
568 might not be interested in text properties that far away. */
569
570 #define TEXT_PROP_DISTANCE_LIMIT 100
571
572 #if GLYPH_DEBUG
573
574 /* Variables to turn off display optimizations from Lisp. */
575
576 int inhibit_try_window_id, inhibit_try_window_reusing;
577 int inhibit_try_cursor_movement;
578
579 /* Non-zero means print traces of redisplay if compiled with
580 GLYPH_DEBUG != 0. */
581
582 int trace_redisplay_p;
583
584 #endif /* GLYPH_DEBUG */
585
586 #ifdef DEBUG_TRACE_MOVE
587 /* Non-zero means trace with TRACE_MOVE to stderr. */
588 int trace_move;
589
590 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
591 #else
592 #define TRACE_MOVE(x) (void) 0
593 #endif
594
595 /* Non-zero means automatically scroll windows horizontally to make
596 point visible. */
597
598 int automatic_hscrolling_p;
599
600 /* How close to the margin can point get before the window is scrolled
601 horizontally. */
602 EMACS_INT hscroll_margin;
603
604 /* How much to scroll horizontally when point is inside the above margin. */
605 Lisp_Object Vhscroll_step;
606
607 /* A list of symbols, one for each supported image type. */
608
609 Lisp_Object Vimage_types;
610
611 /* The variable `resize-mini-windows'. If nil, don't resize
612 mini-windows. If t, always resize them to fit the text they
613 display. If `grow-only', let mini-windows grow only until they
614 become empty. */
615
616 Lisp_Object Vresize_mini_windows;
617
618 /* Buffer being redisplayed -- for redisplay_window_error. */
619
620 struct buffer *displayed_buffer;
621
622 /* Value returned from text property handlers (see below). */
623
624 enum prop_handled
625 {
626 HANDLED_NORMALLY,
627 HANDLED_RECOMPUTE_PROPS,
628 HANDLED_OVERLAY_STRING_CONSUMED,
629 HANDLED_RETURN
630 };
631
632 /* A description of text properties that redisplay is interested
633 in. */
634
635 struct props
636 {
637 /* The name of the property. */
638 Lisp_Object *name;
639
640 /* A unique index for the property. */
641 enum prop_idx idx;
642
643 /* A handler function called to set up iterator IT from the property
644 at IT's current position. Value is used to steer handle_stop. */
645 enum prop_handled (*handler) P_ ((struct it *it));
646 };
647
648 static enum prop_handled handle_face_prop P_ ((struct it *));
649 static enum prop_handled handle_invisible_prop P_ ((struct it *));
650 static enum prop_handled handle_display_prop P_ ((struct it *));
651 static enum prop_handled handle_composition_prop P_ ((struct it *));
652 static enum prop_handled handle_overlay_change P_ ((struct it *));
653 static enum prop_handled handle_fontified_prop P_ ((struct it *));
654
655 /* Properties handled by iterators. */
656
657 static struct props it_props[] =
658 {
659 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
660 /* Handle `face' before `display' because some sub-properties of
661 `display' need to know the face. */
662 {&Qface, FACE_PROP_IDX, handle_face_prop},
663 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
664 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
665 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
666 {NULL, 0, NULL}
667 };
668
669 /* Value is the position described by X. If X is a marker, value is
670 the marker_position of X. Otherwise, value is X. */
671
672 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
673
674 /* Enumeration returned by some move_it_.* functions internally. */
675
676 enum move_it_result
677 {
678 /* Not used. Undefined value. */
679 MOVE_UNDEFINED,
680
681 /* Move ended at the requested buffer position or ZV. */
682 MOVE_POS_MATCH_OR_ZV,
683
684 /* Move ended at the requested X pixel position. */
685 MOVE_X_REACHED,
686
687 /* Move within a line ended at the end of a line that must be
688 continued. */
689 MOVE_LINE_CONTINUED,
690
691 /* Move within a line ended at the end of a line that would
692 be displayed truncated. */
693 MOVE_LINE_TRUNCATED,
694
695 /* Move within a line ended at a line end. */
696 MOVE_NEWLINE_OR_CR
697 };
698
699 /* This counter is used to clear the face cache every once in a while
700 in redisplay_internal. It is incremented for each redisplay.
701 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
702 cleared. */
703
704 #define CLEAR_FACE_CACHE_COUNT 500
705 static int clear_face_cache_count;
706
707 /* Record the previous terminal frame we displayed. */
708
709 static struct frame *previous_terminal_frame;
710
711 /* Non-zero while redisplay_internal is in progress. */
712
713 int redisplaying_p;
714
715 /* Non-zero means don't free realized faces. Bound while freeing
716 realized faces is dangerous because glyph matrices might still
717 reference them. */
718
719 int inhibit_free_realized_faces;
720 Lisp_Object Qinhibit_free_realized_faces;
721
722 \f
723 /* Function prototypes. */
724
725 static void setup_for_ellipsis P_ ((struct it *));
726 static void mark_window_display_accurate_1 P_ ((struct window *, int));
727 static int single_display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
728 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
729 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
730 static int redisplay_mode_lines P_ ((Lisp_Object, int));
731 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
732
733 #if 0
734 static int invisible_text_between_p P_ ((struct it *, int, int));
735 #endif
736
737 static int next_element_from_ellipsis P_ ((struct it *));
738 static void pint2str P_ ((char *, int, int));
739 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
740 struct text_pos));
741 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
742 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
743 static void store_frame_title_char P_ ((char));
744 static int store_frame_title P_ ((const unsigned char *, int, int));
745 static void x_consider_frame_title P_ ((Lisp_Object));
746 static void handle_stop P_ ((struct it *));
747 static int tool_bar_lines_needed P_ ((struct frame *));
748 static int single_display_prop_intangible_p P_ ((Lisp_Object));
749 static void ensure_echo_area_buffers P_ ((void));
750 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
751 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
752 static int with_echo_area_buffer P_ ((struct window *, int,
753 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
754 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
755 static void clear_garbaged_frames P_ ((void));
756 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
757 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
758 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
759 static int display_echo_area P_ ((struct window *));
760 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
761 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
762 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
763 static int string_char_and_length P_ ((const unsigned char *, int, int *));
764 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
765 struct text_pos));
766 static int compute_window_start_on_continuation_line P_ ((struct window *));
767 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
768 static void insert_left_trunc_glyphs P_ ((struct it *));
769 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
770 static void extend_face_to_end_of_line P_ ((struct it *));
771 static int append_space P_ ((struct it *, int));
772 static int make_cursor_line_fully_visible P_ ((struct window *));
773 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int));
774 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
775 static int trailing_whitespace_p P_ ((int));
776 static int message_log_check_duplicate P_ ((int, int, int, int));
777 static void push_it P_ ((struct it *));
778 static void pop_it P_ ((struct it *));
779 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
780 static void redisplay_internal P_ ((int));
781 static int echo_area_display P_ ((int));
782 static void redisplay_windows P_ ((Lisp_Object));
783 static void redisplay_window P_ ((Lisp_Object, int));
784 static Lisp_Object redisplay_window_error ();
785 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
786 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
787 static void update_menu_bar P_ ((struct frame *, int));
788 static int try_window_reusing_current_matrix P_ ((struct window *));
789 static int try_window_id P_ ((struct window *));
790 static int display_line P_ ((struct it *));
791 static int display_mode_lines P_ ((struct window *));
792 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
793 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
794 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
795 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
796 static void display_menu_bar P_ ((struct window *));
797 static int display_count_lines P_ ((int, int, int, int, int *));
798 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
799 int, int, struct it *, int, int, int, int));
800 static void compute_line_metrics P_ ((struct it *));
801 static void run_redisplay_end_trigger_hook P_ ((struct it *));
802 static int get_overlay_strings P_ ((struct it *, int));
803 static void next_overlay_string P_ ((struct it *));
804 static void reseat P_ ((struct it *, struct text_pos, int));
805 static void reseat_1 P_ ((struct it *, struct text_pos, int));
806 static void back_to_previous_visible_line_start P_ ((struct it *));
807 static void reseat_at_previous_visible_line_start P_ ((struct it *));
808 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
809 static int next_element_from_display_vector P_ ((struct it *));
810 static int next_element_from_string P_ ((struct it *));
811 static int next_element_from_c_string P_ ((struct it *));
812 static int next_element_from_buffer P_ ((struct it *));
813 static int next_element_from_composition P_ ((struct it *));
814 static int next_element_from_image P_ ((struct it *));
815 static int next_element_from_stretch P_ ((struct it *));
816 static void load_overlay_strings P_ ((struct it *, int));
817 static int init_from_display_pos P_ ((struct it *, struct window *,
818 struct display_pos *));
819 static void reseat_to_string P_ ((struct it *, unsigned char *,
820 Lisp_Object, int, int, int, int));
821 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
822 int, int, int));
823 void move_it_vertically_backward P_ ((struct it *, int));
824 static void init_to_row_start P_ ((struct it *, struct window *,
825 struct glyph_row *));
826 static int init_to_row_end P_ ((struct it *, struct window *,
827 struct glyph_row *));
828 static void back_to_previous_line_start P_ ((struct it *));
829 static int forward_to_next_line_start P_ ((struct it *, int *));
830 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
831 Lisp_Object, int));
832 static struct text_pos string_pos P_ ((int, Lisp_Object));
833 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
834 static int number_of_chars P_ ((unsigned char *, int));
835 static void compute_stop_pos P_ ((struct it *));
836 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
837 Lisp_Object));
838 static int face_before_or_after_it_pos P_ ((struct it *, int));
839 static int next_overlay_change P_ ((int));
840 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
841 Lisp_Object, struct text_pos *,
842 int));
843 static int underlying_face_id P_ ((struct it *));
844 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
845 struct window *));
846
847 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
848 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
849
850 #ifdef HAVE_WINDOW_SYSTEM
851
852 static void update_tool_bar P_ ((struct frame *, int));
853 static void build_desired_tool_bar_string P_ ((struct frame *f));
854 static int redisplay_tool_bar P_ ((struct frame *));
855 static void display_tool_bar_line P_ ((struct it *));
856
857 #endif /* HAVE_WINDOW_SYSTEM */
858
859 \f
860 /***********************************************************************
861 Window display dimensions
862 ***********************************************************************/
863
864 /* Return the window-relative maximum y + 1 for glyph rows displaying
865 text in window W. This is the height of W minus the height of a
866 mode line, if any. */
867
868 INLINE int
869 window_text_bottom_y (w)
870 struct window *w;
871 {
872 struct frame *f = XFRAME (w->frame);
873 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
874
875 if (WINDOW_WANTS_MODELINE_P (w))
876 height -= CURRENT_MODE_LINE_HEIGHT (w);
877 return height;
878 }
879
880
881 /* Return the pixel width of display area AREA of window W. AREA < 0
882 means return the total width of W, not including fringes to
883 the left and right of the window. */
884
885 INLINE int
886 window_box_width (w, area)
887 struct window *w;
888 int area;
889 {
890 struct frame *f = XFRAME (w->frame);
891 int width = XFASTINT (w->width);
892
893 if (!w->pseudo_window_p)
894 {
895 width -= FRAME_SCROLL_BAR_WIDTH (f) + FRAME_FRINGE_COLS (f);
896
897 if (area == TEXT_AREA)
898 {
899 if (INTEGERP (w->left_margin_width))
900 width -= XFASTINT (w->left_margin_width);
901 if (INTEGERP (w->right_margin_width))
902 width -= XFASTINT (w->right_margin_width);
903 }
904 else if (area == LEFT_MARGIN_AREA)
905 width = (INTEGERP (w->left_margin_width)
906 ? XFASTINT (w->left_margin_width) : 0);
907 else if (area == RIGHT_MARGIN_AREA)
908 width = (INTEGERP (w->right_margin_width)
909 ? XFASTINT (w->right_margin_width) : 0);
910 }
911
912 return width * CANON_X_UNIT (f);
913 }
914
915
916 /* Return the pixel height of the display area of window W, not
917 including mode lines of W, if any. */
918
919 INLINE int
920 window_box_height (w)
921 struct window *w;
922 {
923 struct frame *f = XFRAME (w->frame);
924 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
925
926 xassert (height >= 0);
927
928 /* Note: the code below that determines the mode-line/header-line
929 height is essentially the same as that contained in the macro
930 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
931 the appropriate glyph row has its `mode_line_p' flag set,
932 and if it doesn't, uses estimate_mode_line_height instead. */
933
934 if (WINDOW_WANTS_MODELINE_P (w))
935 {
936 struct glyph_row *ml_row
937 = (w->current_matrix && w->current_matrix->rows
938 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
939 : 0);
940 if (ml_row && ml_row->mode_line_p)
941 height -= ml_row->height;
942 else
943 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
944 }
945
946 if (WINDOW_WANTS_HEADER_LINE_P (w))
947 {
948 struct glyph_row *hl_row
949 = (w->current_matrix && w->current_matrix->rows
950 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
951 : 0);
952 if (hl_row && hl_row->mode_line_p)
953 height -= hl_row->height;
954 else
955 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
956 }
957
958 /* With a very small font and a mode-line that's taller than
959 default, we might end up with a negative height. */
960 return max (0, height);
961 }
962
963
964 /* Return the frame-relative coordinate of the left edge of display
965 area AREA of window W. AREA < 0 means return the left edge of the
966 whole window, to the right of the left fringe of W. */
967
968 INLINE int
969 window_box_left (w, area)
970 struct window *w;
971 int area;
972 {
973 struct frame *f = XFRAME (w->frame);
974 int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
975
976 if (!w->pseudo_window_p)
977 {
978 x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
979 + FRAME_LEFT_FRINGE_WIDTH (f));
980
981 if (area == TEXT_AREA)
982 x += window_box_width (w, LEFT_MARGIN_AREA);
983 else if (area == RIGHT_MARGIN_AREA)
984 x += (window_box_width (w, LEFT_MARGIN_AREA)
985 + window_box_width (w, TEXT_AREA));
986 }
987
988 return x;
989 }
990
991
992 /* Return the frame-relative coordinate of the right edge of display
993 area AREA of window W. AREA < 0 means return the left edge of the
994 whole window, to the left of the right fringe of W. */
995
996 INLINE int
997 window_box_right (w, area)
998 struct window *w;
999 int area;
1000 {
1001 return window_box_left (w, area) + window_box_width (w, area);
1002 }
1003
1004
1005 /* Get the bounding box of the display area AREA of window W, without
1006 mode lines, in frame-relative coordinates. AREA < 0 means the
1007 whole window, not including the left and right fringes of
1008 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1009 coordinates of the upper-left corner of the box. Return in
1010 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1011
1012 INLINE void
1013 window_box (w, area, box_x, box_y, box_width, box_height)
1014 struct window *w;
1015 int area;
1016 int *box_x, *box_y, *box_width, *box_height;
1017 {
1018 struct frame *f = XFRAME (w->frame);
1019
1020 *box_width = window_box_width (w, area);
1021 *box_height = window_box_height (w);
1022 *box_x = window_box_left (w, area);
1023 *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
1024 + XFASTINT (w->top) * CANON_Y_UNIT (f));
1025 if (WINDOW_WANTS_HEADER_LINE_P (w))
1026 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1027 }
1028
1029
1030 /* Get the bounding box of the display area AREA of window W, without
1031 mode lines. AREA < 0 means the whole window, not including the
1032 left and right fringe of the window. Return in *TOP_LEFT_X
1033 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1034 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1035 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1036 box. */
1037
1038 INLINE void
1039 window_box_edges (w, area, top_left_x, top_left_y,
1040 bottom_right_x, bottom_right_y)
1041 struct window *w;
1042 int area;
1043 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1044 {
1045 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1046 bottom_right_y);
1047 *bottom_right_x += *top_left_x;
1048 *bottom_right_y += *top_left_y;
1049 }
1050
1051
1052 \f
1053 /***********************************************************************
1054 Utilities
1055 ***********************************************************************/
1056
1057 /* Return the bottom y-position of the line the iterator IT is in.
1058 This can modify IT's settings. */
1059
1060 int
1061 line_bottom_y (it)
1062 struct it *it;
1063 {
1064 int line_height = it->max_ascent + it->max_descent;
1065 int line_top_y = it->current_y;
1066
1067 if (line_height == 0)
1068 {
1069 if (last_height)
1070 line_height = last_height;
1071 else if (IT_CHARPOS (*it) < ZV)
1072 {
1073 move_it_by_lines (it, 1, 1);
1074 line_height = (it->max_ascent || it->max_descent
1075 ? it->max_ascent + it->max_descent
1076 : last_height);
1077 }
1078 else
1079 {
1080 struct glyph_row *row = it->glyph_row;
1081
1082 /* Use the default character height. */
1083 it->glyph_row = NULL;
1084 it->what = IT_CHARACTER;
1085 it->c = ' ';
1086 it->len = 1;
1087 PRODUCE_GLYPHS (it);
1088 line_height = it->ascent + it->descent;
1089 it->glyph_row = row;
1090 }
1091 }
1092
1093 return line_top_y + line_height;
1094 }
1095
1096
1097 /* Return 1 if position CHARPOS is visible in window W. Set *FULLY to
1098 1 if POS is visible and the line containing POS is fully visible.
1099 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1100 and header-lines heights. */
1101
1102 int
1103 pos_visible_p (w, charpos, fully, exact_mode_line_heights_p)
1104 struct window *w;
1105 int charpos, *fully, exact_mode_line_heights_p;
1106 {
1107 struct it it;
1108 struct text_pos top;
1109 int visible_p;
1110 struct buffer *old_buffer = NULL;
1111
1112 if (XBUFFER (w->buffer) != current_buffer)
1113 {
1114 old_buffer = current_buffer;
1115 set_buffer_internal_1 (XBUFFER (w->buffer));
1116 }
1117
1118 *fully = visible_p = 0;
1119 SET_TEXT_POS_FROM_MARKER (top, w->start);
1120
1121 /* Compute exact mode line heights, if requested. */
1122 if (exact_mode_line_heights_p)
1123 {
1124 if (WINDOW_WANTS_MODELINE_P (w))
1125 current_mode_line_height
1126 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1127 current_buffer->mode_line_format);
1128
1129 if (WINDOW_WANTS_HEADER_LINE_P (w))
1130 current_header_line_height
1131 = display_mode_line (w, HEADER_LINE_FACE_ID,
1132 current_buffer->header_line_format);
1133 }
1134
1135 start_display (&it, w, top);
1136 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
1137 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
1138
1139 /* Note that we may overshoot because of invisible text. */
1140 if (IT_CHARPOS (it) >= charpos)
1141 {
1142 int top_y = it.current_y;
1143 int bottom_y = line_bottom_y (&it);
1144 int window_top_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
1145
1146 if (top_y < window_top_y)
1147 visible_p = bottom_y > window_top_y;
1148 else if (top_y < it.last_visible_y)
1149 {
1150 visible_p = 1;
1151 *fully = bottom_y <= it.last_visible_y;
1152 }
1153 }
1154 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1155 {
1156 move_it_by_lines (&it, 1, 0);
1157 if (charpos < IT_CHARPOS (it))
1158 {
1159 visible_p = 1;
1160 *fully = 0;
1161 }
1162 }
1163
1164 if (old_buffer)
1165 set_buffer_internal_1 (old_buffer);
1166
1167 current_header_line_height = current_mode_line_height = -1;
1168 return visible_p;
1169 }
1170
1171
1172 /* Return the next character from STR which is MAXLEN bytes long.
1173 Return in *LEN the length of the character. This is like
1174 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1175 we find one, we return a `?', but with the length of the invalid
1176 character. */
1177
1178 static INLINE int
1179 string_char_and_length (str, maxlen, len)
1180 const unsigned char *str;
1181 int maxlen, *len;
1182 {
1183 int c;
1184
1185 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1186 if (!CHAR_VALID_P (c, 1))
1187 /* We may not change the length here because other places in Emacs
1188 don't use this function, i.e. they silently accept invalid
1189 characters. */
1190 c = '?';
1191
1192 return c;
1193 }
1194
1195
1196
1197 /* Given a position POS containing a valid character and byte position
1198 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1199
1200 static struct text_pos
1201 string_pos_nchars_ahead (pos, string, nchars)
1202 struct text_pos pos;
1203 Lisp_Object string;
1204 int nchars;
1205 {
1206 xassert (STRINGP (string) && nchars >= 0);
1207
1208 if (STRING_MULTIBYTE (string))
1209 {
1210 int rest = SBYTES (string) - BYTEPOS (pos);
1211 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1212 int len;
1213
1214 while (nchars--)
1215 {
1216 string_char_and_length (p, rest, &len);
1217 p += len, rest -= len;
1218 xassert (rest >= 0);
1219 CHARPOS (pos) += 1;
1220 BYTEPOS (pos) += len;
1221 }
1222 }
1223 else
1224 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1225
1226 return pos;
1227 }
1228
1229
1230 /* Value is the text position, i.e. character and byte position,
1231 for character position CHARPOS in STRING. */
1232
1233 static INLINE struct text_pos
1234 string_pos (charpos, string)
1235 int charpos;
1236 Lisp_Object string;
1237 {
1238 struct text_pos pos;
1239 xassert (STRINGP (string));
1240 xassert (charpos >= 0);
1241 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1242 return pos;
1243 }
1244
1245
1246 /* Value is a text position, i.e. character and byte position, for
1247 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1248 means recognize multibyte characters. */
1249
1250 static struct text_pos
1251 c_string_pos (charpos, s, multibyte_p)
1252 int charpos;
1253 unsigned char *s;
1254 int multibyte_p;
1255 {
1256 struct text_pos pos;
1257
1258 xassert (s != NULL);
1259 xassert (charpos >= 0);
1260
1261 if (multibyte_p)
1262 {
1263 int rest = strlen (s), len;
1264
1265 SET_TEXT_POS (pos, 0, 0);
1266 while (charpos--)
1267 {
1268 string_char_and_length (s, rest, &len);
1269 s += len, rest -= len;
1270 xassert (rest >= 0);
1271 CHARPOS (pos) += 1;
1272 BYTEPOS (pos) += len;
1273 }
1274 }
1275 else
1276 SET_TEXT_POS (pos, charpos, charpos);
1277
1278 return pos;
1279 }
1280
1281
1282 /* Value is the number of characters in C string S. MULTIBYTE_P
1283 non-zero means recognize multibyte characters. */
1284
1285 static int
1286 number_of_chars (s, multibyte_p)
1287 unsigned char *s;
1288 int multibyte_p;
1289 {
1290 int nchars;
1291
1292 if (multibyte_p)
1293 {
1294 int rest = strlen (s), len;
1295 unsigned char *p = (unsigned char *) s;
1296
1297 for (nchars = 0; rest > 0; ++nchars)
1298 {
1299 string_char_and_length (p, rest, &len);
1300 rest -= len, p += len;
1301 }
1302 }
1303 else
1304 nchars = strlen (s);
1305
1306 return nchars;
1307 }
1308
1309
1310 /* Compute byte position NEWPOS->bytepos corresponding to
1311 NEWPOS->charpos. POS is a known position in string STRING.
1312 NEWPOS->charpos must be >= POS.charpos. */
1313
1314 static void
1315 compute_string_pos (newpos, pos, string)
1316 struct text_pos *newpos, pos;
1317 Lisp_Object string;
1318 {
1319 xassert (STRINGP (string));
1320 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1321
1322 if (STRING_MULTIBYTE (string))
1323 *newpos = string_pos_nchars_ahead (pos, string,
1324 CHARPOS (*newpos) - CHARPOS (pos));
1325 else
1326 BYTEPOS (*newpos) = CHARPOS (*newpos);
1327 }
1328
1329
1330 \f
1331 /***********************************************************************
1332 Lisp form evaluation
1333 ***********************************************************************/
1334
1335 /* Error handler for safe_eval and safe_call. */
1336
1337 static Lisp_Object
1338 safe_eval_handler (arg)
1339 Lisp_Object arg;
1340 {
1341 add_to_log ("Error during redisplay: %s", arg, Qnil);
1342 return Qnil;
1343 }
1344
1345
1346 /* Evaluate SEXPR and return the result, or nil if something went
1347 wrong. Prevent redisplay during the evaluation. */
1348
1349 Lisp_Object
1350 safe_eval (sexpr)
1351 Lisp_Object sexpr;
1352 {
1353 Lisp_Object val;
1354
1355 if (inhibit_eval_during_redisplay)
1356 val = Qnil;
1357 else
1358 {
1359 int count = SPECPDL_INDEX ();
1360 struct gcpro gcpro1;
1361
1362 GCPRO1 (sexpr);
1363 specbind (Qinhibit_redisplay, Qt);
1364 /* Use Qt to ensure debugger does not run,
1365 so there is no possibility of wanting to redisplay. */
1366 val = internal_condition_case_1 (Feval, sexpr, Qt,
1367 safe_eval_handler);
1368 UNGCPRO;
1369 val = unbind_to (count, val);
1370 }
1371
1372 return val;
1373 }
1374
1375
1376 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1377 Return the result, or nil if something went wrong. Prevent
1378 redisplay during the evaluation. */
1379
1380 Lisp_Object
1381 safe_call (nargs, args)
1382 int nargs;
1383 Lisp_Object *args;
1384 {
1385 Lisp_Object val;
1386
1387 if (inhibit_eval_during_redisplay)
1388 val = Qnil;
1389 else
1390 {
1391 int count = SPECPDL_INDEX ();
1392 struct gcpro gcpro1;
1393
1394 GCPRO1 (args[0]);
1395 gcpro1.nvars = nargs;
1396 specbind (Qinhibit_redisplay, Qt);
1397 /* Use Qt to ensure debugger does not run,
1398 so there is no possibility of wanting to redisplay. */
1399 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
1400 safe_eval_handler);
1401 UNGCPRO;
1402 val = unbind_to (count, val);
1403 }
1404
1405 return val;
1406 }
1407
1408
1409 /* Call function FN with one argument ARG.
1410 Return the result, or nil if something went wrong. */
1411
1412 Lisp_Object
1413 safe_call1 (fn, arg)
1414 Lisp_Object fn, arg;
1415 {
1416 Lisp_Object args[2];
1417 args[0] = fn;
1418 args[1] = arg;
1419 return safe_call (2, args);
1420 }
1421
1422
1423 \f
1424 /***********************************************************************
1425 Debugging
1426 ***********************************************************************/
1427
1428 #if 0
1429
1430 /* Define CHECK_IT to perform sanity checks on iterators.
1431 This is for debugging. It is too slow to do unconditionally. */
1432
1433 static void
1434 check_it (it)
1435 struct it *it;
1436 {
1437 if (it->method == next_element_from_string)
1438 {
1439 xassert (STRINGP (it->string));
1440 xassert (IT_STRING_CHARPOS (*it) >= 0);
1441 }
1442 else if (it->method == next_element_from_buffer)
1443 {
1444 /* Check that character and byte positions agree. */
1445 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1446 }
1447
1448 if (it->dpvec)
1449 xassert (it->current.dpvec_index >= 0);
1450 else
1451 xassert (it->current.dpvec_index < 0);
1452 }
1453
1454 #define CHECK_IT(IT) check_it ((IT))
1455
1456 #else /* not 0 */
1457
1458 #define CHECK_IT(IT) (void) 0
1459
1460 #endif /* not 0 */
1461
1462
1463 #if GLYPH_DEBUG
1464
1465 /* Check that the window end of window W is what we expect it
1466 to be---the last row in the current matrix displaying text. */
1467
1468 static void
1469 check_window_end (w)
1470 struct window *w;
1471 {
1472 if (!MINI_WINDOW_P (w)
1473 && !NILP (w->window_end_valid))
1474 {
1475 struct glyph_row *row;
1476 xassert ((row = MATRIX_ROW (w->current_matrix,
1477 XFASTINT (w->window_end_vpos)),
1478 !row->enabled_p
1479 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1480 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1481 }
1482 }
1483
1484 #define CHECK_WINDOW_END(W) check_window_end ((W))
1485
1486 #else /* not GLYPH_DEBUG */
1487
1488 #define CHECK_WINDOW_END(W) (void) 0
1489
1490 #endif /* not GLYPH_DEBUG */
1491
1492
1493 \f
1494 /***********************************************************************
1495 Iterator initialization
1496 ***********************************************************************/
1497
1498 /* Initialize IT for displaying current_buffer in window W, starting
1499 at character position CHARPOS. CHARPOS < 0 means that no buffer
1500 position is specified which is useful when the iterator is assigned
1501 a position later. BYTEPOS is the byte position corresponding to
1502 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
1503
1504 If ROW is not null, calls to produce_glyphs with IT as parameter
1505 will produce glyphs in that row.
1506
1507 BASE_FACE_ID is the id of a base face to use. It must be one of
1508 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
1509 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
1510 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
1511
1512 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
1513 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
1514 will be initialized to use the corresponding mode line glyph row of
1515 the desired matrix of W. */
1516
1517 void
1518 init_iterator (it, w, charpos, bytepos, row, base_face_id)
1519 struct it *it;
1520 struct window *w;
1521 int charpos, bytepos;
1522 struct glyph_row *row;
1523 enum face_id base_face_id;
1524 {
1525 int highlight_region_p;
1526
1527 /* Some precondition checks. */
1528 xassert (w != NULL && it != NULL);
1529 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
1530 && charpos <= ZV));
1531
1532 /* If face attributes have been changed since the last redisplay,
1533 free realized faces now because they depend on face definitions
1534 that might have changed. Don't free faces while there might be
1535 desired matrices pending which reference these faces. */
1536 if (face_change_count && !inhibit_free_realized_faces)
1537 {
1538 face_change_count = 0;
1539 free_all_realized_faces (Qnil);
1540 }
1541
1542 /* Use one of the mode line rows of W's desired matrix if
1543 appropriate. */
1544 if (row == NULL)
1545 {
1546 if (base_face_id == MODE_LINE_FACE_ID
1547 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
1548 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
1549 else if (base_face_id == HEADER_LINE_FACE_ID)
1550 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
1551 }
1552
1553 /* Clear IT. */
1554 bzero (it, sizeof *it);
1555 it->current.overlay_string_index = -1;
1556 it->current.dpvec_index = -1;
1557 it->base_face_id = base_face_id;
1558
1559 /* The window in which we iterate over current_buffer: */
1560 XSETWINDOW (it->window, w);
1561 it->w = w;
1562 it->f = XFRAME (w->frame);
1563
1564 /* Extra space between lines (on window systems only). */
1565 if (base_face_id == DEFAULT_FACE_ID
1566 && FRAME_WINDOW_P (it->f))
1567 {
1568 if (NATNUMP (current_buffer->extra_line_spacing))
1569 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
1570 else if (it->f->extra_line_spacing > 0)
1571 it->extra_line_spacing = it->f->extra_line_spacing;
1572 }
1573
1574 /* If realized faces have been removed, e.g. because of face
1575 attribute changes of named faces, recompute them. When running
1576 in batch mode, the face cache of Vterminal_frame is null. If
1577 we happen to get called, make a dummy face cache. */
1578 if (
1579 #ifndef WINDOWSNT
1580 noninteractive &&
1581 #endif
1582 FRAME_FACE_CACHE (it->f) == NULL)
1583 init_frame_faces (it->f);
1584 if (FRAME_FACE_CACHE (it->f)->used == 0)
1585 recompute_basic_faces (it->f);
1586
1587 /* Current value of the `space-width', and 'height' properties. */
1588 it->space_width = Qnil;
1589 it->font_height = Qnil;
1590
1591 /* Are control characters displayed as `^C'? */
1592 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
1593
1594 /* -1 means everything between a CR and the following line end
1595 is invisible. >0 means lines indented more than this value are
1596 invisible. */
1597 it->selective = (INTEGERP (current_buffer->selective_display)
1598 ? XFASTINT (current_buffer->selective_display)
1599 : (!NILP (current_buffer->selective_display)
1600 ? -1 : 0));
1601 it->selective_display_ellipsis_p
1602 = !NILP (current_buffer->selective_display_ellipses);
1603
1604 /* Display table to use. */
1605 it->dp = window_display_table (w);
1606
1607 /* Are multibyte characters enabled in current_buffer? */
1608 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1609
1610 /* Non-zero if we should highlight the region. */
1611 highlight_region_p
1612 = (!NILP (Vtransient_mark_mode)
1613 && !NILP (current_buffer->mark_active)
1614 && XMARKER (current_buffer->mark)->buffer != 0);
1615
1616 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
1617 start and end of a visible region in window IT->w. Set both to
1618 -1 to indicate no region. */
1619 if (highlight_region_p
1620 /* Maybe highlight only in selected window. */
1621 && (/* Either show region everywhere. */
1622 highlight_nonselected_windows
1623 /* Or show region in the selected window. */
1624 || w == XWINDOW (selected_window)
1625 /* Or show the region if we are in the mini-buffer and W is
1626 the window the mini-buffer refers to. */
1627 || (MINI_WINDOW_P (XWINDOW (selected_window))
1628 && WINDOWP (minibuf_selected_window)
1629 && w == XWINDOW (minibuf_selected_window))))
1630 {
1631 int charpos = marker_position (current_buffer->mark);
1632 it->region_beg_charpos = min (PT, charpos);
1633 it->region_end_charpos = max (PT, charpos);
1634 }
1635 else
1636 it->region_beg_charpos = it->region_end_charpos = -1;
1637
1638 /* Get the position at which the redisplay_end_trigger hook should
1639 be run, if it is to be run at all. */
1640 if (MARKERP (w->redisplay_end_trigger)
1641 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
1642 it->redisplay_end_trigger_charpos
1643 = marker_position (w->redisplay_end_trigger);
1644 else if (INTEGERP (w->redisplay_end_trigger))
1645 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
1646
1647 /* Correct bogus values of tab_width. */
1648 it->tab_width = XINT (current_buffer->tab_width);
1649 if (it->tab_width <= 0 || it->tab_width > 1000)
1650 it->tab_width = 8;
1651
1652 /* Are lines in the display truncated? */
1653 it->truncate_lines_p
1654 = (base_face_id != DEFAULT_FACE_ID
1655 || XINT (it->w->hscroll)
1656 || (truncate_partial_width_windows
1657 && !WINDOW_FULL_WIDTH_P (it->w))
1658 || !NILP (current_buffer->truncate_lines));
1659
1660 /* Get dimensions of truncation and continuation glyphs. These are
1661 displayed as fringe bitmaps under X, so we don't need them for such
1662 frames. */
1663 if (!FRAME_WINDOW_P (it->f))
1664 {
1665 if (it->truncate_lines_p)
1666 {
1667 /* We will need the truncation glyph. */
1668 xassert (it->glyph_row == NULL);
1669 produce_special_glyphs (it, IT_TRUNCATION);
1670 it->truncation_pixel_width = it->pixel_width;
1671 }
1672 else
1673 {
1674 /* We will need the continuation glyph. */
1675 xassert (it->glyph_row == NULL);
1676 produce_special_glyphs (it, IT_CONTINUATION);
1677 it->continuation_pixel_width = it->pixel_width;
1678 }
1679
1680 /* Reset these values to zero because the produce_special_glyphs
1681 above has changed them. */
1682 it->pixel_width = it->ascent = it->descent = 0;
1683 it->phys_ascent = it->phys_descent = 0;
1684 }
1685
1686 /* Set this after getting the dimensions of truncation and
1687 continuation glyphs, so that we don't produce glyphs when calling
1688 produce_special_glyphs, above. */
1689 it->glyph_row = row;
1690 it->area = TEXT_AREA;
1691
1692 /* Get the dimensions of the display area. The display area
1693 consists of the visible window area plus a horizontally scrolled
1694 part to the left of the window. All x-values are relative to the
1695 start of this total display area. */
1696 if (base_face_id != DEFAULT_FACE_ID)
1697 {
1698 /* Mode lines, menu bar in terminal frames. */
1699 it->first_visible_x = 0;
1700 it->last_visible_x = XFASTINT (w->width) * CANON_X_UNIT (it->f);
1701 }
1702 else
1703 {
1704 it->first_visible_x
1705 = XFASTINT (it->w->hscroll) * CANON_X_UNIT (it->f);
1706 it->last_visible_x = (it->first_visible_x
1707 + window_box_width (w, TEXT_AREA));
1708
1709 /* If we truncate lines, leave room for the truncator glyph(s) at
1710 the right margin. Otherwise, leave room for the continuation
1711 glyph(s). Truncation and continuation glyphs are not inserted
1712 for window-based redisplay. */
1713 if (!FRAME_WINDOW_P (it->f))
1714 {
1715 if (it->truncate_lines_p)
1716 it->last_visible_x -= it->truncation_pixel_width;
1717 else
1718 it->last_visible_x -= it->continuation_pixel_width;
1719 }
1720
1721 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
1722 it->current_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w) + w->vscroll;
1723 }
1724
1725 /* Leave room for a border glyph. */
1726 if (!FRAME_WINDOW_P (it->f)
1727 && !WINDOW_RIGHTMOST_P (it->w))
1728 it->last_visible_x -= 1;
1729
1730 it->last_visible_y = window_text_bottom_y (w);
1731
1732 /* For mode lines and alike, arrange for the first glyph having a
1733 left box line if the face specifies a box. */
1734 if (base_face_id != DEFAULT_FACE_ID)
1735 {
1736 struct face *face;
1737
1738 it->face_id = base_face_id;
1739
1740 /* If we have a boxed mode line, make the first character appear
1741 with a left box line. */
1742 face = FACE_FROM_ID (it->f, base_face_id);
1743 if (face->box != FACE_NO_BOX)
1744 it->start_of_box_run_p = 1;
1745 }
1746
1747 /* If a buffer position was specified, set the iterator there,
1748 getting overlays and face properties from that position. */
1749 if (charpos >= BUF_BEG (current_buffer))
1750 {
1751 it->end_charpos = ZV;
1752 it->face_id = -1;
1753 IT_CHARPOS (*it) = charpos;
1754
1755 /* Compute byte position if not specified. */
1756 if (bytepos < charpos)
1757 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
1758 else
1759 IT_BYTEPOS (*it) = bytepos;
1760
1761 /* Compute faces etc. */
1762 reseat (it, it->current.pos, 1);
1763 }
1764
1765 CHECK_IT (it);
1766 }
1767
1768
1769 /* Initialize IT for the display of window W with window start POS. */
1770
1771 void
1772 start_display (it, w, pos)
1773 struct it *it;
1774 struct window *w;
1775 struct text_pos pos;
1776 {
1777 struct glyph_row *row;
1778 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
1779
1780 row = w->desired_matrix->rows + first_vpos;
1781 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
1782
1783 if (!it->truncate_lines_p)
1784 {
1785 int start_at_line_beg_p;
1786 int first_y = it->current_y;
1787
1788 /* If window start is not at a line start, skip forward to POS to
1789 get the correct continuation lines width. */
1790 start_at_line_beg_p = (CHARPOS (pos) == BEGV
1791 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
1792 if (!start_at_line_beg_p)
1793 {
1794 reseat_at_previous_visible_line_start (it);
1795 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
1796
1797 /* If lines are continued, this line may end in the middle
1798 of a multi-glyph character (e.g. a control character
1799 displayed as \003, or in the middle of an overlay
1800 string). In this case move_it_to above will not have
1801 taken us to the start of the continuation line but to the
1802 end of the continued line. */
1803 if (it->current_x > 0)
1804 {
1805 if (it->current.dpvec_index >= 0
1806 || it->current.overlay_string_index >= 0)
1807 {
1808 set_iterator_to_next (it, 1);
1809 move_it_in_display_line_to (it, -1, -1, 0);
1810 }
1811
1812 it->continuation_lines_width += it->current_x;
1813 }
1814
1815 /* We're starting a new display line, not affected by the
1816 height of the continued line, so clear the appropriate
1817 fields in the iterator structure. */
1818 it->max_ascent = it->max_descent = 0;
1819 it->max_phys_ascent = it->max_phys_descent = 0;
1820
1821 it->current_y = first_y;
1822 it->vpos = 0;
1823 it->current_x = it->hpos = 0;
1824 }
1825 }
1826
1827 #if 0 /* Don't assert the following because start_display is sometimes
1828 called intentionally with a window start that is not at a
1829 line start. Please leave this code in as a comment. */
1830
1831 /* Window start should be on a line start, now. */
1832 xassert (it->continuation_lines_width
1833 || IT_CHARPOS (it) == BEGV
1834 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
1835 #endif /* 0 */
1836 }
1837
1838
1839 /* Return 1 if POS is a position in ellipses displayed for invisible
1840 text. W is the window we display, for text property lookup. */
1841
1842 static int
1843 in_ellipses_for_invisible_text_p (pos, w)
1844 struct display_pos *pos;
1845 struct window *w;
1846 {
1847 Lisp_Object prop, window;
1848 int ellipses_p = 0;
1849 int charpos = CHARPOS (pos->pos);
1850
1851 /* If POS specifies a position in a display vector, this might
1852 be for an ellipsis displayed for invisible text. We won't
1853 get the iterator set up for delivering that ellipsis unless
1854 we make sure that it gets aware of the invisible text. */
1855 if (pos->dpvec_index >= 0
1856 && pos->overlay_string_index < 0
1857 && CHARPOS (pos->string_pos) < 0
1858 && charpos > BEGV
1859 && (XSETWINDOW (window, w),
1860 prop = Fget_char_property (make_number (charpos),
1861 Qinvisible, window),
1862 !TEXT_PROP_MEANS_INVISIBLE (prop)))
1863 {
1864 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
1865 window);
1866 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
1867 }
1868
1869 return ellipses_p;
1870 }
1871
1872
1873 /* Initialize IT for stepping through current_buffer in window W,
1874 starting at position POS that includes overlay string and display
1875 vector/ control character translation position information. Value
1876 is zero if there are overlay strings with newlines at POS. */
1877
1878 static int
1879 init_from_display_pos (it, w, pos)
1880 struct it *it;
1881 struct window *w;
1882 struct display_pos *pos;
1883 {
1884 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
1885 int i, overlay_strings_with_newlines = 0;
1886
1887 /* If POS specifies a position in a display vector, this might
1888 be for an ellipsis displayed for invisible text. We won't
1889 get the iterator set up for delivering that ellipsis unless
1890 we make sure that it gets aware of the invisible text. */
1891 if (in_ellipses_for_invisible_text_p (pos, w))
1892 {
1893 --charpos;
1894 bytepos = 0;
1895 }
1896
1897 /* Keep in mind: the call to reseat in init_iterator skips invisible
1898 text, so we might end up at a position different from POS. This
1899 is only a problem when POS is a row start after a newline and an
1900 overlay starts there with an after-string, and the overlay has an
1901 invisible property. Since we don't skip invisible text in
1902 display_line and elsewhere immediately after consuming the
1903 newline before the row start, such a POS will not be in a string,
1904 but the call to init_iterator below will move us to the
1905 after-string. */
1906 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
1907
1908 for (i = 0; i < it->n_overlay_strings; ++i)
1909 {
1910 const char *s = SDATA (it->overlay_strings[i]);
1911 const char *e = s + SBYTES (it->overlay_strings[i]);
1912
1913 while (s < e && *s != '\n')
1914 ++s;
1915
1916 if (s < e)
1917 {
1918 overlay_strings_with_newlines = 1;
1919 break;
1920 }
1921 }
1922
1923 /* If position is within an overlay string, set up IT to the right
1924 overlay string. */
1925 if (pos->overlay_string_index >= 0)
1926 {
1927 int relative_index;
1928
1929 /* If the first overlay string happens to have a `display'
1930 property for an image, the iterator will be set up for that
1931 image, and we have to undo that setup first before we can
1932 correct the overlay string index. */
1933 if (it->method == next_element_from_image)
1934 pop_it (it);
1935
1936 /* We already have the first chunk of overlay strings in
1937 IT->overlay_strings. Load more until the one for
1938 pos->overlay_string_index is in IT->overlay_strings. */
1939 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
1940 {
1941 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
1942 it->current.overlay_string_index = 0;
1943 while (n--)
1944 {
1945 load_overlay_strings (it, 0);
1946 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
1947 }
1948 }
1949
1950 it->current.overlay_string_index = pos->overlay_string_index;
1951 relative_index = (it->current.overlay_string_index
1952 % OVERLAY_STRING_CHUNK_SIZE);
1953 it->string = it->overlay_strings[relative_index];
1954 xassert (STRINGP (it->string));
1955 it->current.string_pos = pos->string_pos;
1956 it->method = next_element_from_string;
1957 }
1958
1959 #if 0 /* This is bogus because POS not having an overlay string
1960 position does not mean it's after the string. Example: A
1961 line starting with a before-string and initialization of IT
1962 to the previous row's end position. */
1963 else if (it->current.overlay_string_index >= 0)
1964 {
1965 /* If POS says we're already after an overlay string ending at
1966 POS, make sure to pop the iterator because it will be in
1967 front of that overlay string. When POS is ZV, we've thereby
1968 also ``processed'' overlay strings at ZV. */
1969 while (it->sp)
1970 pop_it (it);
1971 it->current.overlay_string_index = -1;
1972 it->method = next_element_from_buffer;
1973 if (CHARPOS (pos->pos) == ZV)
1974 it->overlay_strings_at_end_processed_p = 1;
1975 }
1976 #endif /* 0 */
1977
1978 if (CHARPOS (pos->string_pos) >= 0)
1979 {
1980 /* Recorded position is not in an overlay string, but in another
1981 string. This can only be a string from a `display' property.
1982 IT should already be filled with that string. */
1983 it->current.string_pos = pos->string_pos;
1984 xassert (STRINGP (it->string));
1985 }
1986
1987 /* Restore position in display vector translations, control
1988 character translations or ellipses. */
1989 if (pos->dpvec_index >= 0)
1990 {
1991 if (it->dpvec == NULL)
1992 get_next_display_element (it);
1993 xassert (it->dpvec && it->current.dpvec_index == 0);
1994 it->current.dpvec_index = pos->dpvec_index;
1995 }
1996
1997 CHECK_IT (it);
1998 return !overlay_strings_with_newlines;
1999 }
2000
2001
2002 /* Initialize IT for stepping through current_buffer in window W
2003 starting at ROW->start. */
2004
2005 static void
2006 init_to_row_start (it, w, row)
2007 struct it *it;
2008 struct window *w;
2009 struct glyph_row *row;
2010 {
2011 init_from_display_pos (it, w, &row->start);
2012 it->continuation_lines_width = row->continuation_lines_width;
2013 CHECK_IT (it);
2014 }
2015
2016
2017 /* Initialize IT for stepping through current_buffer in window W
2018 starting in the line following ROW, i.e. starting at ROW->end.
2019 Value is zero if there are overlay strings with newlines at ROW's
2020 end position. */
2021
2022 static int
2023 init_to_row_end (it, w, row)
2024 struct it *it;
2025 struct window *w;
2026 struct glyph_row *row;
2027 {
2028 int success = 0;
2029
2030 if (init_from_display_pos (it, w, &row->end))
2031 {
2032 if (row->continued_p)
2033 it->continuation_lines_width
2034 = row->continuation_lines_width + row->pixel_width;
2035 CHECK_IT (it);
2036 success = 1;
2037 }
2038
2039 return success;
2040 }
2041
2042
2043
2044 \f
2045 /***********************************************************************
2046 Text properties
2047 ***********************************************************************/
2048
2049 /* Called when IT reaches IT->stop_charpos. Handle text property and
2050 overlay changes. Set IT->stop_charpos to the next position where
2051 to stop. */
2052
2053 static void
2054 handle_stop (it)
2055 struct it *it;
2056 {
2057 enum prop_handled handled;
2058 int handle_overlay_change_p = 1;
2059 struct props *p;
2060
2061 it->dpvec = NULL;
2062 it->current.dpvec_index = -1;
2063
2064 do
2065 {
2066 handled = HANDLED_NORMALLY;
2067
2068 /* Call text property handlers. */
2069 for (p = it_props; p->handler; ++p)
2070 {
2071 handled = p->handler (it);
2072
2073 if (handled == HANDLED_RECOMPUTE_PROPS)
2074 break;
2075 else if (handled == HANDLED_RETURN)
2076 return;
2077 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2078 handle_overlay_change_p = 0;
2079 }
2080
2081 if (handled != HANDLED_RECOMPUTE_PROPS)
2082 {
2083 /* Don't check for overlay strings below when set to deliver
2084 characters from a display vector. */
2085 if (it->method == next_element_from_display_vector)
2086 handle_overlay_change_p = 0;
2087
2088 /* Handle overlay changes. */
2089 if (handle_overlay_change_p)
2090 handled = handle_overlay_change (it);
2091
2092 /* Determine where to stop next. */
2093 if (handled == HANDLED_NORMALLY)
2094 compute_stop_pos (it);
2095 }
2096 }
2097 while (handled == HANDLED_RECOMPUTE_PROPS);
2098 }
2099
2100
2101 /* Compute IT->stop_charpos from text property and overlay change
2102 information for IT's current position. */
2103
2104 static void
2105 compute_stop_pos (it)
2106 struct it *it;
2107 {
2108 register INTERVAL iv, next_iv;
2109 Lisp_Object object, limit, position;
2110
2111 /* If nowhere else, stop at the end. */
2112 it->stop_charpos = it->end_charpos;
2113
2114 if (STRINGP (it->string))
2115 {
2116 /* Strings are usually short, so don't limit the search for
2117 properties. */
2118 object = it->string;
2119 limit = Qnil;
2120 position = make_number (IT_STRING_CHARPOS (*it));
2121 }
2122 else
2123 {
2124 int charpos;
2125
2126 /* If next overlay change is in front of the current stop pos
2127 (which is IT->end_charpos), stop there. Note: value of
2128 next_overlay_change is point-max if no overlay change
2129 follows. */
2130 charpos = next_overlay_change (IT_CHARPOS (*it));
2131 if (charpos < it->stop_charpos)
2132 it->stop_charpos = charpos;
2133
2134 /* If showing the region, we have to stop at the region
2135 start or end because the face might change there. */
2136 if (it->region_beg_charpos > 0)
2137 {
2138 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2139 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2140 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2141 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2142 }
2143
2144 /* Set up variables for computing the stop position from text
2145 property changes. */
2146 XSETBUFFER (object, current_buffer);
2147 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2148 position = make_number (IT_CHARPOS (*it));
2149
2150 }
2151
2152 /* Get the interval containing IT's position. Value is a null
2153 interval if there isn't such an interval. */
2154 iv = validate_interval_range (object, &position, &position, 0);
2155 if (!NULL_INTERVAL_P (iv))
2156 {
2157 Lisp_Object values_here[LAST_PROP_IDX];
2158 struct props *p;
2159
2160 /* Get properties here. */
2161 for (p = it_props; p->handler; ++p)
2162 values_here[p->idx] = textget (iv->plist, *p->name);
2163
2164 /* Look for an interval following iv that has different
2165 properties. */
2166 for (next_iv = next_interval (iv);
2167 (!NULL_INTERVAL_P (next_iv)
2168 && (NILP (limit)
2169 || XFASTINT (limit) > next_iv->position));
2170 next_iv = next_interval (next_iv))
2171 {
2172 for (p = it_props; p->handler; ++p)
2173 {
2174 Lisp_Object new_value;
2175
2176 new_value = textget (next_iv->plist, *p->name);
2177 if (!EQ (values_here[p->idx], new_value))
2178 break;
2179 }
2180
2181 if (p->handler)
2182 break;
2183 }
2184
2185 if (!NULL_INTERVAL_P (next_iv))
2186 {
2187 if (INTEGERP (limit)
2188 && next_iv->position >= XFASTINT (limit))
2189 /* No text property change up to limit. */
2190 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2191 else
2192 /* Text properties change in next_iv. */
2193 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2194 }
2195 }
2196
2197 xassert (STRINGP (it->string)
2198 || (it->stop_charpos >= BEGV
2199 && it->stop_charpos >= IT_CHARPOS (*it)));
2200 }
2201
2202
2203 /* Return the position of the next overlay change after POS in
2204 current_buffer. Value is point-max if no overlay change
2205 follows. This is like `next-overlay-change' but doesn't use
2206 xmalloc. */
2207
2208 static int
2209 next_overlay_change (pos)
2210 int pos;
2211 {
2212 int noverlays;
2213 int endpos;
2214 Lisp_Object *overlays;
2215 int len;
2216 int i;
2217
2218 /* Get all overlays at the given position. */
2219 len = 10;
2220 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2221 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2222 if (noverlays > len)
2223 {
2224 len = noverlays;
2225 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2226 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2227 }
2228
2229 /* If any of these overlays ends before endpos,
2230 use its ending point instead. */
2231 for (i = 0; i < noverlays; ++i)
2232 {
2233 Lisp_Object oend;
2234 int oendpos;
2235
2236 oend = OVERLAY_END (overlays[i]);
2237 oendpos = OVERLAY_POSITION (oend);
2238 endpos = min (endpos, oendpos);
2239 }
2240
2241 return endpos;
2242 }
2243
2244
2245 \f
2246 /***********************************************************************
2247 Fontification
2248 ***********************************************************************/
2249
2250 /* Handle changes in the `fontified' property of the current buffer by
2251 calling hook functions from Qfontification_functions to fontify
2252 regions of text. */
2253
2254 static enum prop_handled
2255 handle_fontified_prop (it)
2256 struct it *it;
2257 {
2258 Lisp_Object prop, pos;
2259 enum prop_handled handled = HANDLED_NORMALLY;
2260
2261 /* Get the value of the `fontified' property at IT's current buffer
2262 position. (The `fontified' property doesn't have a special
2263 meaning in strings.) If the value is nil, call functions from
2264 Qfontification_functions. */
2265 if (!STRINGP (it->string)
2266 && it->s == NULL
2267 && !NILP (Vfontification_functions)
2268 && !NILP (Vrun_hooks)
2269 && (pos = make_number (IT_CHARPOS (*it)),
2270 prop = Fget_char_property (pos, Qfontified, Qnil),
2271 NILP (prop)))
2272 {
2273 int count = SPECPDL_INDEX ();
2274 Lisp_Object val;
2275
2276 val = Vfontification_functions;
2277 specbind (Qfontification_functions, Qnil);
2278
2279 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2280 safe_call1 (val, pos);
2281 else
2282 {
2283 Lisp_Object globals, fn;
2284 struct gcpro gcpro1, gcpro2;
2285
2286 globals = Qnil;
2287 GCPRO2 (val, globals);
2288
2289 for (; CONSP (val); val = XCDR (val))
2290 {
2291 fn = XCAR (val);
2292
2293 if (EQ (fn, Qt))
2294 {
2295 /* A value of t indicates this hook has a local
2296 binding; it means to run the global binding too.
2297 In a global value, t should not occur. If it
2298 does, we must ignore it to avoid an endless
2299 loop. */
2300 for (globals = Fdefault_value (Qfontification_functions);
2301 CONSP (globals);
2302 globals = XCDR (globals))
2303 {
2304 fn = XCAR (globals);
2305 if (!EQ (fn, Qt))
2306 safe_call1 (fn, pos);
2307 }
2308 }
2309 else
2310 safe_call1 (fn, pos);
2311 }
2312
2313 UNGCPRO;
2314 }
2315
2316 unbind_to (count, Qnil);
2317
2318 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2319 something. This avoids an endless loop if they failed to
2320 fontify the text for which reason ever. */
2321 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2322 handled = HANDLED_RECOMPUTE_PROPS;
2323 }
2324
2325 return handled;
2326 }
2327
2328
2329 \f
2330 /***********************************************************************
2331 Faces
2332 ***********************************************************************/
2333
2334 /* Set up iterator IT from face properties at its current position.
2335 Called from handle_stop. */
2336
2337 static enum prop_handled
2338 handle_face_prop (it)
2339 struct it *it;
2340 {
2341 int new_face_id, next_stop;
2342
2343 if (!STRINGP (it->string))
2344 {
2345 new_face_id
2346 = face_at_buffer_position (it->w,
2347 IT_CHARPOS (*it),
2348 it->region_beg_charpos,
2349 it->region_end_charpos,
2350 &next_stop,
2351 (IT_CHARPOS (*it)
2352 + TEXT_PROP_DISTANCE_LIMIT),
2353 0);
2354
2355 /* Is this a start of a run of characters with box face?
2356 Caveat: this can be called for a freshly initialized
2357 iterator; face_id is -1 in this case. We know that the new
2358 face will not change until limit, i.e. if the new face has a
2359 box, all characters up to limit will have one. But, as
2360 usual, we don't know whether limit is really the end. */
2361 if (new_face_id != it->face_id)
2362 {
2363 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2364
2365 /* If new face has a box but old face has not, this is
2366 the start of a run of characters with box, i.e. it has
2367 a shadow on the left side. The value of face_id of the
2368 iterator will be -1 if this is the initial call that gets
2369 the face. In this case, we have to look in front of IT's
2370 position and see whether there is a face != new_face_id. */
2371 it->start_of_box_run_p
2372 = (new_face->box != FACE_NO_BOX
2373 && (it->face_id >= 0
2374 || IT_CHARPOS (*it) == BEG
2375 || new_face_id != face_before_it_pos (it)));
2376 it->face_box_p = new_face->box != FACE_NO_BOX;
2377 }
2378 }
2379 else
2380 {
2381 int base_face_id, bufpos;
2382
2383 if (it->current.overlay_string_index >= 0)
2384 bufpos = IT_CHARPOS (*it);
2385 else
2386 bufpos = 0;
2387
2388 /* For strings from a buffer, i.e. overlay strings or strings
2389 from a `display' property, use the face at IT's current
2390 buffer position as the base face to merge with, so that
2391 overlay strings appear in the same face as surrounding
2392 text, unless they specify their own faces. */
2393 base_face_id = underlying_face_id (it);
2394
2395 new_face_id = face_at_string_position (it->w,
2396 it->string,
2397 IT_STRING_CHARPOS (*it),
2398 bufpos,
2399 it->region_beg_charpos,
2400 it->region_end_charpos,
2401 &next_stop,
2402 base_face_id, 0);
2403
2404 #if 0 /* This shouldn't be neccessary. Let's check it. */
2405 /* If IT is used to display a mode line we would really like to
2406 use the mode line face instead of the frame's default face. */
2407 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2408 && new_face_id == DEFAULT_FACE_ID)
2409 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
2410 #endif
2411
2412 /* Is this a start of a run of characters with box? Caveat:
2413 this can be called for a freshly allocated iterator; face_id
2414 is -1 is this case. We know that the new face will not
2415 change until the next check pos, i.e. if the new face has a
2416 box, all characters up to that position will have a
2417 box. But, as usual, we don't know whether that position
2418 is really the end. */
2419 if (new_face_id != it->face_id)
2420 {
2421 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2422 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2423
2424 /* If new face has a box but old face hasn't, this is the
2425 start of a run of characters with box, i.e. it has a
2426 shadow on the left side. */
2427 it->start_of_box_run_p
2428 = new_face->box && (old_face == NULL || !old_face->box);
2429 it->face_box_p = new_face->box != FACE_NO_BOX;
2430 }
2431 }
2432
2433 it->face_id = new_face_id;
2434 return HANDLED_NORMALLY;
2435 }
2436
2437
2438 /* Return the ID of the face ``underlying'' IT's current position,
2439 which is in a string. If the iterator is associated with a
2440 buffer, return the face at IT's current buffer position.
2441 Otherwise, use the iterator's base_face_id. */
2442
2443 static int
2444 underlying_face_id (it)
2445 struct it *it;
2446 {
2447 int face_id = it->base_face_id, i;
2448
2449 xassert (STRINGP (it->string));
2450
2451 for (i = it->sp - 1; i >= 0; --i)
2452 if (NILP (it->stack[i].string))
2453 face_id = it->stack[i].face_id;
2454
2455 return face_id;
2456 }
2457
2458
2459 /* Compute the face one character before or after the current position
2460 of IT. BEFORE_P non-zero means get the face in front of IT's
2461 position. Value is the id of the face. */
2462
2463 static int
2464 face_before_or_after_it_pos (it, before_p)
2465 struct it *it;
2466 int before_p;
2467 {
2468 int face_id, limit;
2469 int next_check_charpos;
2470 struct text_pos pos;
2471
2472 xassert (it->s == NULL);
2473
2474 if (STRINGP (it->string))
2475 {
2476 int bufpos, base_face_id;
2477
2478 /* No face change past the end of the string (for the case
2479 we are padding with spaces). No face change before the
2480 string start. */
2481 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
2482 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2483 return it->face_id;
2484
2485 /* Set pos to the position before or after IT's current position. */
2486 if (before_p)
2487 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2488 else
2489 /* For composition, we must check the character after the
2490 composition. */
2491 pos = (it->what == IT_COMPOSITION
2492 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
2493 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
2494
2495 if (it->current.overlay_string_index >= 0)
2496 bufpos = IT_CHARPOS (*it);
2497 else
2498 bufpos = 0;
2499
2500 base_face_id = underlying_face_id (it);
2501
2502 /* Get the face for ASCII, or unibyte. */
2503 face_id = face_at_string_position (it->w,
2504 it->string,
2505 CHARPOS (pos),
2506 bufpos,
2507 it->region_beg_charpos,
2508 it->region_end_charpos,
2509 &next_check_charpos,
2510 base_face_id, 0);
2511
2512 /* Correct the face for charsets different from ASCII. Do it
2513 for the multibyte case only. The face returned above is
2514 suitable for unibyte text if IT->string is unibyte. */
2515 if (STRING_MULTIBYTE (it->string))
2516 {
2517 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
2518 int rest = SBYTES (it->string) - BYTEPOS (pos);
2519 int c, len;
2520 struct face *face = FACE_FROM_ID (it->f, face_id);
2521
2522 c = string_char_and_length (p, rest, &len);
2523 face_id = FACE_FOR_CHAR (it->f, face, c);
2524 }
2525 }
2526 else
2527 {
2528 if ((IT_CHARPOS (*it) >= ZV && !before_p)
2529 || (IT_CHARPOS (*it) <= BEGV && before_p))
2530 return it->face_id;
2531
2532 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
2533 pos = it->current.pos;
2534
2535 if (before_p)
2536 DEC_TEXT_POS (pos, it->multibyte_p);
2537 else
2538 {
2539 if (it->what == IT_COMPOSITION)
2540 /* For composition, we must check the position after the
2541 composition. */
2542 pos.charpos += it->cmp_len, pos.bytepos += it->len;
2543 else
2544 INC_TEXT_POS (pos, it->multibyte_p);
2545 }
2546
2547 /* Determine face for CHARSET_ASCII, or unibyte. */
2548 face_id = face_at_buffer_position (it->w,
2549 CHARPOS (pos),
2550 it->region_beg_charpos,
2551 it->region_end_charpos,
2552 &next_check_charpos,
2553 limit, 0);
2554
2555 /* Correct the face for charsets different from ASCII. Do it
2556 for the multibyte case only. The face returned above is
2557 suitable for unibyte text if current_buffer is unibyte. */
2558 if (it->multibyte_p)
2559 {
2560 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
2561 struct face *face = FACE_FROM_ID (it->f, face_id);
2562 face_id = FACE_FOR_CHAR (it->f, face, c);
2563 }
2564 }
2565
2566 return face_id;
2567 }
2568
2569
2570 \f
2571 /***********************************************************************
2572 Invisible text
2573 ***********************************************************************/
2574
2575 /* Set up iterator IT from invisible properties at its current
2576 position. Called from handle_stop. */
2577
2578 static enum prop_handled
2579 handle_invisible_prop (it)
2580 struct it *it;
2581 {
2582 enum prop_handled handled = HANDLED_NORMALLY;
2583
2584 if (STRINGP (it->string))
2585 {
2586 extern Lisp_Object Qinvisible;
2587 Lisp_Object prop, end_charpos, limit, charpos;
2588
2589 /* Get the value of the invisible text property at the
2590 current position. Value will be nil if there is no such
2591 property. */
2592 charpos = make_number (IT_STRING_CHARPOS (*it));
2593 prop = Fget_text_property (charpos, Qinvisible, it->string);
2594
2595 if (!NILP (prop)
2596 && IT_STRING_CHARPOS (*it) < it->end_charpos)
2597 {
2598 handled = HANDLED_RECOMPUTE_PROPS;
2599
2600 /* Get the position at which the next change of the
2601 invisible text property can be found in IT->string.
2602 Value will be nil if the property value is the same for
2603 all the rest of IT->string. */
2604 XSETINT (limit, SCHARS (it->string));
2605 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2606 it->string, limit);
2607
2608 /* Text at current position is invisible. The next
2609 change in the property is at position end_charpos.
2610 Move IT's current position to that position. */
2611 if (INTEGERP (end_charpos)
2612 && XFASTINT (end_charpos) < XFASTINT (limit))
2613 {
2614 struct text_pos old;
2615 old = it->current.string_pos;
2616 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
2617 compute_string_pos (&it->current.string_pos, old, it->string);
2618 }
2619 else
2620 {
2621 /* The rest of the string is invisible. If this is an
2622 overlay string, proceed with the next overlay string
2623 or whatever comes and return a character from there. */
2624 if (it->current.overlay_string_index >= 0)
2625 {
2626 next_overlay_string (it);
2627 /* Don't check for overlay strings when we just
2628 finished processing them. */
2629 handled = HANDLED_OVERLAY_STRING_CONSUMED;
2630 }
2631 else
2632 {
2633 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
2634 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
2635 }
2636 }
2637 }
2638 }
2639 else
2640 {
2641 int invis_p, newpos, next_stop, start_charpos;
2642 Lisp_Object pos, prop, overlay;
2643
2644 /* First of all, is there invisible text at this position? */
2645 start_charpos = IT_CHARPOS (*it);
2646 pos = make_number (IT_CHARPOS (*it));
2647 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
2648 &overlay);
2649 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
2650
2651 /* If we are on invisible text, skip over it. */
2652 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
2653 {
2654 /* Record whether we have to display an ellipsis for the
2655 invisible text. */
2656 int display_ellipsis_p = invis_p == 2;
2657
2658 handled = HANDLED_RECOMPUTE_PROPS;
2659
2660 /* Loop skipping over invisible text. The loop is left at
2661 ZV or with IT on the first char being visible again. */
2662 do
2663 {
2664 /* Try to skip some invisible text. Return value is the
2665 position reached which can be equal to IT's position
2666 if there is nothing invisible here. This skips both
2667 over invisible text properties and overlays with
2668 invisible property. */
2669 newpos = skip_invisible (IT_CHARPOS (*it),
2670 &next_stop, ZV, it->window);
2671
2672 /* If we skipped nothing at all we weren't at invisible
2673 text in the first place. If everything to the end of
2674 the buffer was skipped, end the loop. */
2675 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
2676 invis_p = 0;
2677 else
2678 {
2679 /* We skipped some characters but not necessarily
2680 all there are. Check if we ended up on visible
2681 text. Fget_char_property returns the property of
2682 the char before the given position, i.e. if we
2683 get invis_p = 0, this means that the char at
2684 newpos is visible. */
2685 pos = make_number (newpos);
2686 prop = Fget_char_property (pos, Qinvisible, it->window);
2687 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
2688 }
2689
2690 /* If we ended up on invisible text, proceed to
2691 skip starting with next_stop. */
2692 if (invis_p)
2693 IT_CHARPOS (*it) = next_stop;
2694 }
2695 while (invis_p);
2696
2697 /* The position newpos is now either ZV or on visible text. */
2698 IT_CHARPOS (*it) = newpos;
2699 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2700
2701 /* If there are before-strings at the start of invisible
2702 text, and the text is invisible because of a text
2703 property, arrange to show before-strings because 20.x did
2704 it that way. (If the text is invisible because of an
2705 overlay property instead of a text property, this is
2706 already handled in the overlay code.) */
2707 if (NILP (overlay)
2708 && get_overlay_strings (it, start_charpos))
2709 {
2710 handled = HANDLED_RECOMPUTE_PROPS;
2711 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
2712 }
2713 else if (display_ellipsis_p)
2714 setup_for_ellipsis (it);
2715 }
2716 }
2717
2718 return handled;
2719 }
2720
2721
2722 /* Make iterator IT return `...' next. */
2723
2724 static void
2725 setup_for_ellipsis (it)
2726 struct it *it;
2727 {
2728 if (it->dp
2729 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
2730 {
2731 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
2732 it->dpvec = v->contents;
2733 it->dpend = v->contents + v->size;
2734 }
2735 else
2736 {
2737 /* Default `...'. */
2738 it->dpvec = default_invis_vector;
2739 it->dpend = default_invis_vector + 3;
2740 }
2741
2742 /* The ellipsis display does not replace the display of the
2743 character at the new position. Indicate this by setting
2744 IT->dpvec_char_len to zero. */
2745 it->dpvec_char_len = 0;
2746
2747 it->current.dpvec_index = 0;
2748 it->method = next_element_from_display_vector;
2749 }
2750
2751
2752 \f
2753 /***********************************************************************
2754 'display' property
2755 ***********************************************************************/
2756
2757 /* Set up iterator IT from `display' property at its current position.
2758 Called from handle_stop. */
2759
2760 static enum prop_handled
2761 handle_display_prop (it)
2762 struct it *it;
2763 {
2764 Lisp_Object prop, object;
2765 struct text_pos *position;
2766 int display_replaced_p = 0;
2767
2768 if (STRINGP (it->string))
2769 {
2770 object = it->string;
2771 position = &it->current.string_pos;
2772 }
2773 else
2774 {
2775 object = it->w->buffer;
2776 position = &it->current.pos;
2777 }
2778
2779 /* Reset those iterator values set from display property values. */
2780 it->font_height = Qnil;
2781 it->space_width = Qnil;
2782 it->voffset = 0;
2783
2784 /* We don't support recursive `display' properties, i.e. string
2785 values that have a string `display' property, that have a string
2786 `display' property etc. */
2787 if (!it->string_from_display_prop_p)
2788 it->area = TEXT_AREA;
2789
2790 prop = Fget_char_property (make_number (position->charpos),
2791 Qdisplay, object);
2792 if (NILP (prop))
2793 return HANDLED_NORMALLY;
2794
2795 if (CONSP (prop)
2796 /* Simple properties. */
2797 && !EQ (XCAR (prop), Qimage)
2798 && !EQ (XCAR (prop), Qspace)
2799 && !EQ (XCAR (prop), Qwhen)
2800 && !EQ (XCAR (prop), Qspace_width)
2801 && !EQ (XCAR (prop), Qheight)
2802 && !EQ (XCAR (prop), Qraise)
2803 /* Marginal area specifications. */
2804 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
2805 && !NILP (XCAR (prop)))
2806 {
2807 for (; CONSP (prop); prop = XCDR (prop))
2808 {
2809 if (handle_single_display_prop (it, XCAR (prop), object,
2810 position, display_replaced_p))
2811 display_replaced_p = 1;
2812 }
2813 }
2814 else if (VECTORP (prop))
2815 {
2816 int i;
2817 for (i = 0; i < ASIZE (prop); ++i)
2818 if (handle_single_display_prop (it, AREF (prop, i), object,
2819 position, display_replaced_p))
2820 display_replaced_p = 1;
2821 }
2822 else
2823 {
2824 if (handle_single_display_prop (it, prop, object, position, 0))
2825 display_replaced_p = 1;
2826 }
2827
2828 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
2829 }
2830
2831
2832 /* Value is the position of the end of the `display' property starting
2833 at START_POS in OBJECT. */
2834
2835 static struct text_pos
2836 display_prop_end (it, object, start_pos)
2837 struct it *it;
2838 Lisp_Object object;
2839 struct text_pos start_pos;
2840 {
2841 Lisp_Object end;
2842 struct text_pos end_pos;
2843
2844 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
2845 Qdisplay, object, Qnil);
2846 CHARPOS (end_pos) = XFASTINT (end);
2847 if (STRINGP (object))
2848 compute_string_pos (&end_pos, start_pos, it->string);
2849 else
2850 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
2851
2852 return end_pos;
2853 }
2854
2855
2856 /* Set up IT from a single `display' sub-property value PROP. OBJECT
2857 is the object in which the `display' property was found. *POSITION
2858 is the position at which it was found. DISPLAY_REPLACED_P non-zero
2859 means that we previously saw a display sub-property which already
2860 replaced text display with something else, for example an image;
2861 ignore such properties after the first one has been processed.
2862
2863 If PROP is a `space' or `image' sub-property, set *POSITION to the
2864 end position of the `display' property.
2865
2866 Value is non-zero if something was found which replaces the display
2867 of buffer or string text. */
2868
2869 static int
2870 handle_single_display_prop (it, prop, object, position,
2871 display_replaced_before_p)
2872 struct it *it;
2873 Lisp_Object prop;
2874 Lisp_Object object;
2875 struct text_pos *position;
2876 int display_replaced_before_p;
2877 {
2878 Lisp_Object value;
2879 int replaces_text_display_p = 0;
2880 Lisp_Object form;
2881
2882 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
2883 evaluated. If the result is nil, VALUE is ignored. */
2884 form = Qt;
2885 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2886 {
2887 prop = XCDR (prop);
2888 if (!CONSP (prop))
2889 return 0;
2890 form = XCAR (prop);
2891 prop = XCDR (prop);
2892 }
2893
2894 if (!NILP (form) && !EQ (form, Qt))
2895 {
2896 int count = SPECPDL_INDEX ();
2897 struct gcpro gcpro1;
2898
2899 /* Bind `object' to the object having the `display' property, a
2900 buffer or string. Bind `position' to the position in the
2901 object where the property was found, and `buffer-position'
2902 to the current position in the buffer. */
2903 specbind (Qobject, object);
2904 specbind (Qposition, make_number (CHARPOS (*position)));
2905 specbind (Qbuffer_position,
2906 make_number (STRINGP (object)
2907 ? IT_CHARPOS (*it) : CHARPOS (*position)));
2908 GCPRO1 (form);
2909 form = safe_eval (form);
2910 UNGCPRO;
2911 unbind_to (count, Qnil);
2912 }
2913
2914 if (NILP (form))
2915 return 0;
2916
2917 if (CONSP (prop)
2918 && EQ (XCAR (prop), Qheight)
2919 && CONSP (XCDR (prop)))
2920 {
2921 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2922 return 0;
2923
2924 /* `(height HEIGHT)'. */
2925 it->font_height = XCAR (XCDR (prop));
2926 if (!NILP (it->font_height))
2927 {
2928 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2929 int new_height = -1;
2930
2931 if (CONSP (it->font_height)
2932 && (EQ (XCAR (it->font_height), Qplus)
2933 || EQ (XCAR (it->font_height), Qminus))
2934 && CONSP (XCDR (it->font_height))
2935 && INTEGERP (XCAR (XCDR (it->font_height))))
2936 {
2937 /* `(+ N)' or `(- N)' where N is an integer. */
2938 int steps = XINT (XCAR (XCDR (it->font_height)));
2939 if (EQ (XCAR (it->font_height), Qplus))
2940 steps = - steps;
2941 it->face_id = smaller_face (it->f, it->face_id, steps);
2942 }
2943 else if (FUNCTIONP (it->font_height))
2944 {
2945 /* Call function with current height as argument.
2946 Value is the new height. */
2947 Lisp_Object height;
2948 height = safe_call1 (it->font_height,
2949 face->lface[LFACE_HEIGHT_INDEX]);
2950 if (NUMBERP (height))
2951 new_height = XFLOATINT (height);
2952 }
2953 else if (NUMBERP (it->font_height))
2954 {
2955 /* Value is a multiple of the canonical char height. */
2956 struct face *face;
2957
2958 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
2959 new_height = (XFLOATINT (it->font_height)
2960 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
2961 }
2962 else
2963 {
2964 /* Evaluate IT->font_height with `height' bound to the
2965 current specified height to get the new height. */
2966 Lisp_Object value;
2967 int count = SPECPDL_INDEX ();
2968
2969 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
2970 value = safe_eval (it->font_height);
2971 unbind_to (count, Qnil);
2972
2973 if (NUMBERP (value))
2974 new_height = XFLOATINT (value);
2975 }
2976
2977 if (new_height > 0)
2978 it->face_id = face_with_height (it->f, it->face_id, new_height);
2979 }
2980 }
2981 else if (CONSP (prop)
2982 && EQ (XCAR (prop), Qspace_width)
2983 && CONSP (XCDR (prop)))
2984 {
2985 /* `(space_width WIDTH)'. */
2986 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2987 return 0;
2988
2989 value = XCAR (XCDR (prop));
2990 if (NUMBERP (value) && XFLOATINT (value) > 0)
2991 it->space_width = value;
2992 }
2993 else if (CONSP (prop)
2994 && EQ (XCAR (prop), Qraise)
2995 && CONSP (XCDR (prop)))
2996 {
2997 /* `(raise FACTOR)'. */
2998 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2999 return 0;
3000
3001 #ifdef HAVE_WINDOW_SYSTEM
3002 value = XCAR (XCDR (prop));
3003 if (NUMBERP (value))
3004 {
3005 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3006 it->voffset = - (XFLOATINT (value)
3007 * (FONT_HEIGHT (face->font)));
3008 }
3009 #endif /* HAVE_WINDOW_SYSTEM */
3010 }
3011 else if (!it->string_from_display_prop_p)
3012 {
3013 /* `((margin left-margin) VALUE)' or `((margin right-margin)
3014 VALUE) or `((margin nil) VALUE)' or VALUE. */
3015 Lisp_Object location, value;
3016 struct text_pos start_pos;
3017 int valid_p;
3018
3019 /* Characters having this form of property are not displayed, so
3020 we have to find the end of the property. */
3021 start_pos = *position;
3022 *position = display_prop_end (it, object, start_pos);
3023 value = Qnil;
3024
3025 /* Let's stop at the new position and assume that all
3026 text properties change there. */
3027 it->stop_charpos = position->charpos;
3028
3029 location = Qunbound;
3030 if (CONSP (prop) && CONSP (XCAR (prop)))
3031 {
3032 Lisp_Object tem;
3033
3034 value = XCDR (prop);
3035 if (CONSP (value))
3036 value = XCAR (value);
3037
3038 tem = XCAR (prop);
3039 if (EQ (XCAR (tem), Qmargin)
3040 && (tem = XCDR (tem),
3041 tem = CONSP (tem) ? XCAR (tem) : Qnil,
3042 (NILP (tem)
3043 || EQ (tem, Qleft_margin)
3044 || EQ (tem, Qright_margin))))
3045 location = tem;
3046 }
3047
3048 if (EQ (location, Qunbound))
3049 {
3050 location = Qnil;
3051 value = prop;
3052 }
3053
3054 #ifdef HAVE_WINDOW_SYSTEM
3055 if (FRAME_TERMCAP_P (it->f))
3056 valid_p = STRINGP (value);
3057 else
3058 valid_p = (STRINGP (value)
3059 || (CONSP (value) && EQ (XCAR (value), Qspace))
3060 || valid_image_p (value));
3061 #else /* not HAVE_WINDOW_SYSTEM */
3062 valid_p = STRINGP (value);
3063 #endif /* not HAVE_WINDOW_SYSTEM */
3064
3065 if ((EQ (location, Qleft_margin)
3066 || EQ (location, Qright_margin)
3067 || NILP (location))
3068 && valid_p
3069 && !display_replaced_before_p)
3070 {
3071 replaces_text_display_p = 1;
3072
3073 /* Save current settings of IT so that we can restore them
3074 when we are finished with the glyph property value. */
3075 push_it (it);
3076
3077 if (NILP (location))
3078 it->area = TEXT_AREA;
3079 else if (EQ (location, Qleft_margin))
3080 it->area = LEFT_MARGIN_AREA;
3081 else
3082 it->area = RIGHT_MARGIN_AREA;
3083
3084 if (STRINGP (value))
3085 {
3086 it->string = value;
3087 it->multibyte_p = STRING_MULTIBYTE (it->string);
3088 it->current.overlay_string_index = -1;
3089 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3090 it->end_charpos = it->string_nchars = SCHARS (it->string);
3091 it->method = next_element_from_string;
3092 it->stop_charpos = 0;
3093 it->string_from_display_prop_p = 1;
3094 /* Say that we haven't consumed the characters with
3095 `display' property yet. The call to pop_it in
3096 set_iterator_to_next will clean this up. */
3097 *position = start_pos;
3098 }
3099 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3100 {
3101 it->method = next_element_from_stretch;
3102 it->object = value;
3103 it->current.pos = it->position = start_pos;
3104 }
3105 #ifdef HAVE_WINDOW_SYSTEM
3106 else
3107 {
3108 it->what = IT_IMAGE;
3109 it->image_id = lookup_image (it->f, value);
3110 it->position = start_pos;
3111 it->object = NILP (object) ? it->w->buffer : object;
3112 it->method = next_element_from_image;
3113
3114 /* Say that we haven't consumed the characters with
3115 `display' property yet. The call to pop_it in
3116 set_iterator_to_next will clean this up. */
3117 *position = start_pos;
3118 }
3119 #endif /* HAVE_WINDOW_SYSTEM */
3120 }
3121 else
3122 /* Invalid property or property not supported. Restore
3123 the position to what it was before. */
3124 *position = start_pos;
3125 }
3126
3127 return replaces_text_display_p;
3128 }
3129
3130
3131 /* Check if PROP is a display sub-property value whose text should be
3132 treated as intangible. */
3133
3134 static int
3135 single_display_prop_intangible_p (prop)
3136 Lisp_Object prop;
3137 {
3138 /* Skip over `when FORM'. */
3139 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3140 {
3141 prop = XCDR (prop);
3142 if (!CONSP (prop))
3143 return 0;
3144 prop = XCDR (prop);
3145 }
3146
3147 if (STRINGP (prop))
3148 return 1;
3149
3150 if (!CONSP (prop))
3151 return 0;
3152
3153 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3154 we don't need to treat text as intangible. */
3155 if (EQ (XCAR (prop), Qmargin))
3156 {
3157 prop = XCDR (prop);
3158 if (!CONSP (prop))
3159 return 0;
3160
3161 prop = XCDR (prop);
3162 if (!CONSP (prop)
3163 || EQ (XCAR (prop), Qleft_margin)
3164 || EQ (XCAR (prop), Qright_margin))
3165 return 0;
3166 }
3167
3168 return CONSP (prop) && EQ (XCAR (prop), Qimage);
3169 }
3170
3171
3172 /* Check if PROP is a display property value whose text should be
3173 treated as intangible. */
3174
3175 int
3176 display_prop_intangible_p (prop)
3177 Lisp_Object prop;
3178 {
3179 if (CONSP (prop)
3180 && CONSP (XCAR (prop))
3181 && !EQ (Qmargin, XCAR (XCAR (prop))))
3182 {
3183 /* A list of sub-properties. */
3184 while (CONSP (prop))
3185 {
3186 if (single_display_prop_intangible_p (XCAR (prop)))
3187 return 1;
3188 prop = XCDR (prop);
3189 }
3190 }
3191 else if (VECTORP (prop))
3192 {
3193 /* A vector of sub-properties. */
3194 int i;
3195 for (i = 0; i < ASIZE (prop); ++i)
3196 if (single_display_prop_intangible_p (AREF (prop, i)))
3197 return 1;
3198 }
3199 else
3200 return single_display_prop_intangible_p (prop);
3201
3202 return 0;
3203 }
3204
3205
3206 /* Return 1 if PROP is a display sub-property value containing STRING. */
3207
3208 static int
3209 single_display_prop_string_p (prop, string)
3210 Lisp_Object prop, string;
3211 {
3212 if (EQ (string, prop))
3213 return 1;
3214
3215 /* Skip over `when FORM'. */
3216 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3217 {
3218 prop = XCDR (prop);
3219 if (!CONSP (prop))
3220 return 0;
3221 prop = XCDR (prop);
3222 }
3223
3224 if (CONSP (prop))
3225 /* Skip over `margin LOCATION'. */
3226 if (EQ (XCAR (prop), Qmargin))
3227 {
3228 prop = XCDR (prop);
3229 if (!CONSP (prop))
3230 return 0;
3231
3232 prop = XCDR (prop);
3233 if (!CONSP (prop))
3234 return 0;
3235 }
3236
3237 return CONSP (prop) && EQ (XCAR (prop), string);
3238 }
3239
3240
3241 /* Return 1 if STRING appears in the `display' property PROP. */
3242
3243 static int
3244 display_prop_string_p (prop, string)
3245 Lisp_Object prop, string;
3246 {
3247 if (CONSP (prop)
3248 && CONSP (XCAR (prop))
3249 && !EQ (Qmargin, XCAR (XCAR (prop))))
3250 {
3251 /* A list of sub-properties. */
3252 while (CONSP (prop))
3253 {
3254 if (single_display_prop_string_p (XCAR (prop), string))
3255 return 1;
3256 prop = XCDR (prop);
3257 }
3258 }
3259 else if (VECTORP (prop))
3260 {
3261 /* A vector of sub-properties. */
3262 int i;
3263 for (i = 0; i < ASIZE (prop); ++i)
3264 if (single_display_prop_string_p (AREF (prop, i), string))
3265 return 1;
3266 }
3267 else
3268 return single_display_prop_string_p (prop, string);
3269
3270 return 0;
3271 }
3272
3273
3274 /* Determine from which buffer position in W's buffer STRING comes
3275 from. AROUND_CHARPOS is an approximate position where it could
3276 be from. Value is the buffer position or 0 if it couldn't be
3277 determined.
3278
3279 W's buffer must be current.
3280
3281 This function is necessary because we don't record buffer positions
3282 in glyphs generated from strings (to keep struct glyph small).
3283 This function may only use code that doesn't eval because it is
3284 called asynchronously from note_mouse_highlight. */
3285
3286 int
3287 string_buffer_position (w, string, around_charpos)
3288 struct window *w;
3289 Lisp_Object string;
3290 int around_charpos;
3291 {
3292 Lisp_Object limit, prop, pos;
3293 const int MAX_DISTANCE = 1000;
3294 int found = 0;
3295
3296 pos = make_number (around_charpos);
3297 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
3298 while (!found && !EQ (pos, limit))
3299 {
3300 prop = Fget_char_property (pos, Qdisplay, Qnil);
3301 if (!NILP (prop) && display_prop_string_p (prop, string))
3302 found = 1;
3303 else
3304 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
3305 }
3306
3307 if (!found)
3308 {
3309 pos = make_number (around_charpos);
3310 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
3311 while (!found && !EQ (pos, limit))
3312 {
3313 prop = Fget_char_property (pos, Qdisplay, Qnil);
3314 if (!NILP (prop) && display_prop_string_p (prop, string))
3315 found = 1;
3316 else
3317 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
3318 limit);
3319 }
3320 }
3321
3322 return found ? XINT (pos) : 0;
3323 }
3324
3325
3326 \f
3327 /***********************************************************************
3328 `composition' property
3329 ***********************************************************************/
3330
3331 /* Set up iterator IT from `composition' property at its current
3332 position. Called from handle_stop. */
3333
3334 static enum prop_handled
3335 handle_composition_prop (it)
3336 struct it *it;
3337 {
3338 Lisp_Object prop, string;
3339 int pos, pos_byte, end;
3340 enum prop_handled handled = HANDLED_NORMALLY;
3341
3342 if (STRINGP (it->string))
3343 {
3344 pos = IT_STRING_CHARPOS (*it);
3345 pos_byte = IT_STRING_BYTEPOS (*it);
3346 string = it->string;
3347 }
3348 else
3349 {
3350 pos = IT_CHARPOS (*it);
3351 pos_byte = IT_BYTEPOS (*it);
3352 string = Qnil;
3353 }
3354
3355 /* If there's a valid composition and point is not inside of the
3356 composition (in the case that the composition is from the current
3357 buffer), draw a glyph composed from the composition components. */
3358 if (find_composition (pos, -1, &pos, &end, &prop, string)
3359 && COMPOSITION_VALID_P (pos, end, prop)
3360 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
3361 {
3362 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
3363
3364 if (id >= 0)
3365 {
3366 it->method = next_element_from_composition;
3367 it->cmp_id = id;
3368 it->cmp_len = COMPOSITION_LENGTH (prop);
3369 /* For a terminal, draw only the first character of the
3370 components. */
3371 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
3372 it->len = (STRINGP (it->string)
3373 ? string_char_to_byte (it->string, end)
3374 : CHAR_TO_BYTE (end)) - pos_byte;
3375 it->stop_charpos = end;
3376 handled = HANDLED_RETURN;
3377 }
3378 }
3379
3380 return handled;
3381 }
3382
3383
3384 \f
3385 /***********************************************************************
3386 Overlay strings
3387 ***********************************************************************/
3388
3389 /* The following structure is used to record overlay strings for
3390 later sorting in load_overlay_strings. */
3391
3392 struct overlay_entry
3393 {
3394 Lisp_Object overlay;
3395 Lisp_Object string;
3396 int priority;
3397 int after_string_p;
3398 };
3399
3400
3401 /* Set up iterator IT from overlay strings at its current position.
3402 Called from handle_stop. */
3403
3404 static enum prop_handled
3405 handle_overlay_change (it)
3406 struct it *it;
3407 {
3408 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
3409 return HANDLED_RECOMPUTE_PROPS;
3410 else
3411 return HANDLED_NORMALLY;
3412 }
3413
3414
3415 /* Set up the next overlay string for delivery by IT, if there is an
3416 overlay string to deliver. Called by set_iterator_to_next when the
3417 end of the current overlay string is reached. If there are more
3418 overlay strings to display, IT->string and
3419 IT->current.overlay_string_index are set appropriately here.
3420 Otherwise IT->string is set to nil. */
3421
3422 static void
3423 next_overlay_string (it)
3424 struct it *it;
3425 {
3426 ++it->current.overlay_string_index;
3427 if (it->current.overlay_string_index == it->n_overlay_strings)
3428 {
3429 /* No more overlay strings. Restore IT's settings to what
3430 they were before overlay strings were processed, and
3431 continue to deliver from current_buffer. */
3432 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
3433
3434 pop_it (it);
3435 xassert (it->stop_charpos >= BEGV
3436 && it->stop_charpos <= it->end_charpos);
3437 it->string = Qnil;
3438 it->current.overlay_string_index = -1;
3439 SET_TEXT_POS (it->current.string_pos, -1, -1);
3440 it->n_overlay_strings = 0;
3441 it->method = next_element_from_buffer;
3442
3443 /* If we're at the end of the buffer, record that we have
3444 processed the overlay strings there already, so that
3445 next_element_from_buffer doesn't try it again. */
3446 if (IT_CHARPOS (*it) >= it->end_charpos)
3447 it->overlay_strings_at_end_processed_p = 1;
3448
3449 /* If we have to display `...' for invisible text, set
3450 the iterator up for that. */
3451 if (display_ellipsis_p)
3452 setup_for_ellipsis (it);
3453 }
3454 else
3455 {
3456 /* There are more overlay strings to process. If
3457 IT->current.overlay_string_index has advanced to a position
3458 where we must load IT->overlay_strings with more strings, do
3459 it. */
3460 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
3461
3462 if (it->current.overlay_string_index && i == 0)
3463 load_overlay_strings (it, 0);
3464
3465 /* Initialize IT to deliver display elements from the overlay
3466 string. */
3467 it->string = it->overlay_strings[i];
3468 it->multibyte_p = STRING_MULTIBYTE (it->string);
3469 SET_TEXT_POS (it->current.string_pos, 0, 0);
3470 it->method = next_element_from_string;
3471 it->stop_charpos = 0;
3472 }
3473
3474 CHECK_IT (it);
3475 }
3476
3477
3478 /* Compare two overlay_entry structures E1 and E2. Used as a
3479 comparison function for qsort in load_overlay_strings. Overlay
3480 strings for the same position are sorted so that
3481
3482 1. All after-strings come in front of before-strings, except
3483 when they come from the same overlay.
3484
3485 2. Within after-strings, strings are sorted so that overlay strings
3486 from overlays with higher priorities come first.
3487
3488 2. Within before-strings, strings are sorted so that overlay
3489 strings from overlays with higher priorities come last.
3490
3491 Value is analogous to strcmp. */
3492
3493
3494 static int
3495 compare_overlay_entries (e1, e2)
3496 void *e1, *e2;
3497 {
3498 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
3499 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
3500 int result;
3501
3502 if (entry1->after_string_p != entry2->after_string_p)
3503 {
3504 /* Let after-strings appear in front of before-strings if
3505 they come from different overlays. */
3506 if (EQ (entry1->overlay, entry2->overlay))
3507 result = entry1->after_string_p ? 1 : -1;
3508 else
3509 result = entry1->after_string_p ? -1 : 1;
3510 }
3511 else if (entry1->after_string_p)
3512 /* After-strings sorted in order of decreasing priority. */
3513 result = entry2->priority - entry1->priority;
3514 else
3515 /* Before-strings sorted in order of increasing priority. */
3516 result = entry1->priority - entry2->priority;
3517
3518 return result;
3519 }
3520
3521
3522 /* Load the vector IT->overlay_strings with overlay strings from IT's
3523 current buffer position, or from CHARPOS if that is > 0. Set
3524 IT->n_overlays to the total number of overlay strings found.
3525
3526 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
3527 a time. On entry into load_overlay_strings,
3528 IT->current.overlay_string_index gives the number of overlay
3529 strings that have already been loaded by previous calls to this
3530 function.
3531
3532 IT->add_overlay_start contains an additional overlay start
3533 position to consider for taking overlay strings from, if non-zero.
3534 This position comes into play when the overlay has an `invisible'
3535 property, and both before and after-strings. When we've skipped to
3536 the end of the overlay, because of its `invisible' property, we
3537 nevertheless want its before-string to appear.
3538 IT->add_overlay_start will contain the overlay start position
3539 in this case.
3540
3541 Overlay strings are sorted so that after-string strings come in
3542 front of before-string strings. Within before and after-strings,
3543 strings are sorted by overlay priority. See also function
3544 compare_overlay_entries. */
3545
3546 static void
3547 load_overlay_strings (it, charpos)
3548 struct it *it;
3549 int charpos;
3550 {
3551 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
3552 Lisp_Object ov, overlay, window, str, invisible;
3553 int start, end;
3554 int size = 20;
3555 int n = 0, i, j, invis_p;
3556 struct overlay_entry *entries
3557 = (struct overlay_entry *) alloca (size * sizeof *entries);
3558
3559 if (charpos <= 0)
3560 charpos = IT_CHARPOS (*it);
3561
3562 /* Append the overlay string STRING of overlay OVERLAY to vector
3563 `entries' which has size `size' and currently contains `n'
3564 elements. AFTER_P non-zero means STRING is an after-string of
3565 OVERLAY. */
3566 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
3567 do \
3568 { \
3569 Lisp_Object priority; \
3570 \
3571 if (n == size) \
3572 { \
3573 int new_size = 2 * size; \
3574 struct overlay_entry *old = entries; \
3575 entries = \
3576 (struct overlay_entry *) alloca (new_size \
3577 * sizeof *entries); \
3578 bcopy (old, entries, size * sizeof *entries); \
3579 size = new_size; \
3580 } \
3581 \
3582 entries[n].string = (STRING); \
3583 entries[n].overlay = (OVERLAY); \
3584 priority = Foverlay_get ((OVERLAY), Qpriority); \
3585 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
3586 entries[n].after_string_p = (AFTER_P); \
3587 ++n; \
3588 } \
3589 while (0)
3590
3591 /* Process overlay before the overlay center. */
3592 for (ov = current_buffer->overlays_before; CONSP (ov); ov = XCDR (ov))
3593 {
3594 overlay = XCAR (ov);
3595 xassert (OVERLAYP (overlay));
3596 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3597 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3598
3599 if (end < charpos)
3600 break;
3601
3602 /* Skip this overlay if it doesn't start or end at IT's current
3603 position. */
3604 if (end != charpos && start != charpos)
3605 continue;
3606
3607 /* Skip this overlay if it doesn't apply to IT->w. */
3608 window = Foverlay_get (overlay, Qwindow);
3609 if (WINDOWP (window) && XWINDOW (window) != it->w)
3610 continue;
3611
3612 /* If the text ``under'' the overlay is invisible, both before-
3613 and after-strings from this overlay are visible; start and
3614 end position are indistinguishable. */
3615 invisible = Foverlay_get (overlay, Qinvisible);
3616 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3617
3618 /* If overlay has a non-empty before-string, record it. */
3619 if ((start == charpos || (end == charpos && invis_p))
3620 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3621 && SCHARS (str))
3622 RECORD_OVERLAY_STRING (overlay, str, 0);
3623
3624 /* If overlay has a non-empty after-string, record it. */
3625 if ((end == charpos || (start == charpos && invis_p))
3626 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3627 && SCHARS (str))
3628 RECORD_OVERLAY_STRING (overlay, str, 1);
3629 }
3630
3631 /* Process overlays after the overlay center. */
3632 for (ov = current_buffer->overlays_after; CONSP (ov); ov = XCDR (ov))
3633 {
3634 overlay = XCAR (ov);
3635 xassert (OVERLAYP (overlay));
3636 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3637 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3638
3639 if (start > charpos)
3640 break;
3641
3642 /* Skip this overlay if it doesn't start or end at IT's current
3643 position. */
3644 if (end != charpos && start != charpos)
3645 continue;
3646
3647 /* Skip this overlay if it doesn't apply to IT->w. */
3648 window = Foverlay_get (overlay, Qwindow);
3649 if (WINDOWP (window) && XWINDOW (window) != it->w)
3650 continue;
3651
3652 /* If the text ``under'' the overlay is invisible, it has a zero
3653 dimension, and both before- and after-strings apply. */
3654 invisible = Foverlay_get (overlay, Qinvisible);
3655 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3656
3657 /* If overlay has a non-empty before-string, record it. */
3658 if ((start == charpos || (end == charpos && invis_p))
3659 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3660 && SCHARS (str))
3661 RECORD_OVERLAY_STRING (overlay, str, 0);
3662
3663 /* If overlay has a non-empty after-string, record it. */
3664 if ((end == charpos || (start == charpos && invis_p))
3665 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3666 && SCHARS (str))
3667 RECORD_OVERLAY_STRING (overlay, str, 1);
3668 }
3669
3670 #undef RECORD_OVERLAY_STRING
3671
3672 /* Sort entries. */
3673 if (n > 1)
3674 qsort (entries, n, sizeof *entries, compare_overlay_entries);
3675
3676 /* Record the total number of strings to process. */
3677 it->n_overlay_strings = n;
3678
3679 /* IT->current.overlay_string_index is the number of overlay strings
3680 that have already been consumed by IT. Copy some of the
3681 remaining overlay strings to IT->overlay_strings. */
3682 i = 0;
3683 j = it->current.overlay_string_index;
3684 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
3685 it->overlay_strings[i++] = entries[j++].string;
3686
3687 CHECK_IT (it);
3688 }
3689
3690
3691 /* Get the first chunk of overlay strings at IT's current buffer
3692 position, or at CHARPOS if that is > 0. Value is non-zero if at
3693 least one overlay string was found. */
3694
3695 static int
3696 get_overlay_strings (it, charpos)
3697 struct it *it;
3698 int charpos;
3699 {
3700 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
3701 process. This fills IT->overlay_strings with strings, and sets
3702 IT->n_overlay_strings to the total number of strings to process.
3703 IT->pos.overlay_string_index has to be set temporarily to zero
3704 because load_overlay_strings needs this; it must be set to -1
3705 when no overlay strings are found because a zero value would
3706 indicate a position in the first overlay string. */
3707 it->current.overlay_string_index = 0;
3708 load_overlay_strings (it, charpos);
3709
3710 /* If we found overlay strings, set up IT to deliver display
3711 elements from the first one. Otherwise set up IT to deliver
3712 from current_buffer. */
3713 if (it->n_overlay_strings)
3714 {
3715 /* Make sure we know settings in current_buffer, so that we can
3716 restore meaningful values when we're done with the overlay
3717 strings. */
3718 compute_stop_pos (it);
3719 xassert (it->face_id >= 0);
3720
3721 /* Save IT's settings. They are restored after all overlay
3722 strings have been processed. */
3723 xassert (it->sp == 0);
3724 push_it (it);
3725
3726 /* Set up IT to deliver display elements from the first overlay
3727 string. */
3728 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3729 it->string = it->overlay_strings[0];
3730 it->stop_charpos = 0;
3731 xassert (STRINGP (it->string));
3732 it->end_charpos = SCHARS (it->string);
3733 it->multibyte_p = STRING_MULTIBYTE (it->string);
3734 it->method = next_element_from_string;
3735 }
3736 else
3737 {
3738 it->string = Qnil;
3739 it->current.overlay_string_index = -1;
3740 it->method = next_element_from_buffer;
3741 }
3742
3743 CHECK_IT (it);
3744
3745 /* Value is non-zero if we found at least one overlay string. */
3746 return STRINGP (it->string);
3747 }
3748
3749
3750 \f
3751 /***********************************************************************
3752 Saving and restoring state
3753 ***********************************************************************/
3754
3755 /* Save current settings of IT on IT->stack. Called, for example,
3756 before setting up IT for an overlay string, to be able to restore
3757 IT's settings to what they were after the overlay string has been
3758 processed. */
3759
3760 static void
3761 push_it (it)
3762 struct it *it;
3763 {
3764 struct iterator_stack_entry *p;
3765
3766 xassert (it->sp < 2);
3767 p = it->stack + it->sp;
3768
3769 p->stop_charpos = it->stop_charpos;
3770 xassert (it->face_id >= 0);
3771 p->face_id = it->face_id;
3772 p->string = it->string;
3773 p->pos = it->current;
3774 p->end_charpos = it->end_charpos;
3775 p->string_nchars = it->string_nchars;
3776 p->area = it->area;
3777 p->multibyte_p = it->multibyte_p;
3778 p->space_width = it->space_width;
3779 p->font_height = it->font_height;
3780 p->voffset = it->voffset;
3781 p->string_from_display_prop_p = it->string_from_display_prop_p;
3782 p->display_ellipsis_p = 0;
3783 ++it->sp;
3784 }
3785
3786
3787 /* Restore IT's settings from IT->stack. Called, for example, when no
3788 more overlay strings must be processed, and we return to delivering
3789 display elements from a buffer, or when the end of a string from a
3790 `display' property is reached and we return to delivering display
3791 elements from an overlay string, or from a buffer. */
3792
3793 static void
3794 pop_it (it)
3795 struct it *it;
3796 {
3797 struct iterator_stack_entry *p;
3798
3799 xassert (it->sp > 0);
3800 --it->sp;
3801 p = it->stack + it->sp;
3802 it->stop_charpos = p->stop_charpos;
3803 it->face_id = p->face_id;
3804 it->string = p->string;
3805 it->current = p->pos;
3806 it->end_charpos = p->end_charpos;
3807 it->string_nchars = p->string_nchars;
3808 it->area = p->area;
3809 it->multibyte_p = p->multibyte_p;
3810 it->space_width = p->space_width;
3811 it->font_height = p->font_height;
3812 it->voffset = p->voffset;
3813 it->string_from_display_prop_p = p->string_from_display_prop_p;
3814 }
3815
3816
3817 \f
3818 /***********************************************************************
3819 Moving over lines
3820 ***********************************************************************/
3821
3822 /* Set IT's current position to the previous line start. */
3823
3824 static void
3825 back_to_previous_line_start (it)
3826 struct it *it;
3827 {
3828 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
3829 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3830 }
3831
3832
3833 /* Move IT to the next line start.
3834
3835 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
3836 we skipped over part of the text (as opposed to moving the iterator
3837 continuously over the text). Otherwise, don't change the value
3838 of *SKIPPED_P.
3839
3840 Newlines may come from buffer text, overlay strings, or strings
3841 displayed via the `display' property. That's the reason we can't
3842 simply use find_next_newline_no_quit.
3843
3844 Note that this function may not skip over invisible text that is so
3845 because of text properties and immediately follows a newline. If
3846 it would, function reseat_at_next_visible_line_start, when called
3847 from set_iterator_to_next, would effectively make invisible
3848 characters following a newline part of the wrong glyph row, which
3849 leads to wrong cursor motion. */
3850
3851 static int
3852 forward_to_next_line_start (it, skipped_p)
3853 struct it *it;
3854 int *skipped_p;
3855 {
3856 int old_selective, newline_found_p, n;
3857 const int MAX_NEWLINE_DISTANCE = 500;
3858
3859 /* If already on a newline, just consume it to avoid unintended
3860 skipping over invisible text below. */
3861 if (it->what == IT_CHARACTER
3862 && it->c == '\n'
3863 && CHARPOS (it->position) == IT_CHARPOS (*it))
3864 {
3865 set_iterator_to_next (it, 0);
3866 it->c = 0;
3867 return 1;
3868 }
3869
3870 /* Don't handle selective display in the following. It's (a)
3871 unnecessary because it's done by the caller, and (b) leads to an
3872 infinite recursion because next_element_from_ellipsis indirectly
3873 calls this function. */
3874 old_selective = it->selective;
3875 it->selective = 0;
3876
3877 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
3878 from buffer text. */
3879 for (n = newline_found_p = 0;
3880 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
3881 n += STRINGP (it->string) ? 0 : 1)
3882 {
3883 if (!get_next_display_element (it))
3884 return 0;
3885 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
3886 set_iterator_to_next (it, 0);
3887 }
3888
3889 /* If we didn't find a newline near enough, see if we can use a
3890 short-cut. */
3891 if (!newline_found_p)
3892 {
3893 int start = IT_CHARPOS (*it);
3894 int limit = find_next_newline_no_quit (start, 1);
3895 Lisp_Object pos;
3896
3897 xassert (!STRINGP (it->string));
3898
3899 /* If there isn't any `display' property in sight, and no
3900 overlays, we can just use the position of the newline in
3901 buffer text. */
3902 if (it->stop_charpos >= limit
3903 || ((pos = Fnext_single_property_change (make_number (start),
3904 Qdisplay,
3905 Qnil, make_number (limit)),
3906 NILP (pos))
3907 && next_overlay_change (start) == ZV))
3908 {
3909 IT_CHARPOS (*it) = limit;
3910 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
3911 *skipped_p = newline_found_p = 1;
3912 }
3913 else
3914 {
3915 while (get_next_display_element (it)
3916 && !newline_found_p)
3917 {
3918 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
3919 set_iterator_to_next (it, 0);
3920 }
3921 }
3922 }
3923
3924 it->selective = old_selective;
3925 return newline_found_p;
3926 }
3927
3928
3929 /* Set IT's current position to the previous visible line start. Skip
3930 invisible text that is so either due to text properties or due to
3931 selective display. Caution: this does not change IT->current_x and
3932 IT->hpos. */
3933
3934 static void
3935 back_to_previous_visible_line_start (it)
3936 struct it *it;
3937 {
3938 int visible_p = 0;
3939
3940 /* Go back one newline if not on BEGV already. */
3941 if (IT_CHARPOS (*it) > BEGV)
3942 back_to_previous_line_start (it);
3943
3944 /* Move over lines that are invisible because of selective display
3945 or text properties. */
3946 while (IT_CHARPOS (*it) > BEGV
3947 && !visible_p)
3948 {
3949 visible_p = 1;
3950
3951 /* If selective > 0, then lines indented more than that values
3952 are invisible. */
3953 if (it->selective > 0
3954 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3955 (double) it->selective)) /* iftc */
3956 visible_p = 0;
3957 else
3958 {
3959 Lisp_Object prop;
3960
3961 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
3962 Qinvisible, it->window);
3963 if (TEXT_PROP_MEANS_INVISIBLE (prop))
3964 visible_p = 0;
3965 }
3966
3967 /* Back one more newline if the current one is invisible. */
3968 if (!visible_p)
3969 back_to_previous_line_start (it);
3970 }
3971
3972 xassert (IT_CHARPOS (*it) >= BEGV);
3973 xassert (IT_CHARPOS (*it) == BEGV
3974 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3975 CHECK_IT (it);
3976 }
3977
3978
3979 /* Reseat iterator IT at the previous visible line start. Skip
3980 invisible text that is so either due to text properties or due to
3981 selective display. At the end, update IT's overlay information,
3982 face information etc. */
3983
3984 static void
3985 reseat_at_previous_visible_line_start (it)
3986 struct it *it;
3987 {
3988 back_to_previous_visible_line_start (it);
3989 reseat (it, it->current.pos, 1);
3990 CHECK_IT (it);
3991 }
3992
3993
3994 /* Reseat iterator IT on the next visible line start in the current
3995 buffer. ON_NEWLINE_P non-zero means position IT on the newline
3996 preceding the line start. Skip over invisible text that is so
3997 because of selective display. Compute faces, overlays etc at the
3998 new position. Note that this function does not skip over text that
3999 is invisible because of text properties. */
4000
4001 static void
4002 reseat_at_next_visible_line_start (it, on_newline_p)
4003 struct it *it;
4004 int on_newline_p;
4005 {
4006 int newline_found_p, skipped_p = 0;
4007
4008 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4009
4010 /* Skip over lines that are invisible because they are indented
4011 more than the value of IT->selective. */
4012 if (it->selective > 0)
4013 while (IT_CHARPOS (*it) < ZV
4014 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4015 (double) it->selective)) /* iftc */
4016 {
4017 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4018 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4019 }
4020
4021 /* Position on the newline if that's what's requested. */
4022 if (on_newline_p && newline_found_p)
4023 {
4024 if (STRINGP (it->string))
4025 {
4026 if (IT_STRING_CHARPOS (*it) > 0)
4027 {
4028 --IT_STRING_CHARPOS (*it);
4029 --IT_STRING_BYTEPOS (*it);
4030 }
4031 }
4032 else if (IT_CHARPOS (*it) > BEGV)
4033 {
4034 --IT_CHARPOS (*it);
4035 --IT_BYTEPOS (*it);
4036 reseat (it, it->current.pos, 0);
4037 }
4038 }
4039 else if (skipped_p)
4040 reseat (it, it->current.pos, 0);
4041
4042 CHECK_IT (it);
4043 }
4044
4045
4046 \f
4047 /***********************************************************************
4048 Changing an iterator's position
4049 ***********************************************************************/
4050
4051 /* Change IT's current position to POS in current_buffer. If FORCE_P
4052 is non-zero, always check for text properties at the new position.
4053 Otherwise, text properties are only looked up if POS >=
4054 IT->check_charpos of a property. */
4055
4056 static void
4057 reseat (it, pos, force_p)
4058 struct it *it;
4059 struct text_pos pos;
4060 int force_p;
4061 {
4062 int original_pos = IT_CHARPOS (*it);
4063
4064 reseat_1 (it, pos, 0);
4065
4066 /* Determine where to check text properties. Avoid doing it
4067 where possible because text property lookup is very expensive. */
4068 if (force_p
4069 || CHARPOS (pos) > it->stop_charpos
4070 || CHARPOS (pos) < original_pos)
4071 handle_stop (it);
4072
4073 CHECK_IT (it);
4074 }
4075
4076
4077 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4078 IT->stop_pos to POS, also. */
4079
4080 static void
4081 reseat_1 (it, pos, set_stop_p)
4082 struct it *it;
4083 struct text_pos pos;
4084 int set_stop_p;
4085 {
4086 /* Don't call this function when scanning a C string. */
4087 xassert (it->s == NULL);
4088
4089 /* POS must be a reasonable value. */
4090 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4091
4092 it->current.pos = it->position = pos;
4093 XSETBUFFER (it->object, current_buffer);
4094 it->end_charpos = ZV;
4095 it->dpvec = NULL;
4096 it->current.dpvec_index = -1;
4097 it->current.overlay_string_index = -1;
4098 IT_STRING_CHARPOS (*it) = -1;
4099 IT_STRING_BYTEPOS (*it) = -1;
4100 it->string = Qnil;
4101 it->method = next_element_from_buffer;
4102 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
4103 it->sp = 0;
4104 it->face_before_selective_p = 0;
4105
4106 if (set_stop_p)
4107 it->stop_charpos = CHARPOS (pos);
4108 }
4109
4110
4111 /* Set up IT for displaying a string, starting at CHARPOS in window W.
4112 If S is non-null, it is a C string to iterate over. Otherwise,
4113 STRING gives a Lisp string to iterate over.
4114
4115 If PRECISION > 0, don't return more then PRECISION number of
4116 characters from the string.
4117
4118 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4119 characters have been returned. FIELD_WIDTH < 0 means an infinite
4120 field width.
4121
4122 MULTIBYTE = 0 means disable processing of multibyte characters,
4123 MULTIBYTE > 0 means enable it,
4124 MULTIBYTE < 0 means use IT->multibyte_p.
4125
4126 IT must be initialized via a prior call to init_iterator before
4127 calling this function. */
4128
4129 static void
4130 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4131 struct it *it;
4132 unsigned char *s;
4133 Lisp_Object string;
4134 int charpos;
4135 int precision, field_width, multibyte;
4136 {
4137 /* No region in strings. */
4138 it->region_beg_charpos = it->region_end_charpos = -1;
4139
4140 /* No text property checks performed by default, but see below. */
4141 it->stop_charpos = -1;
4142
4143 /* Set iterator position and end position. */
4144 bzero (&it->current, sizeof it->current);
4145 it->current.overlay_string_index = -1;
4146 it->current.dpvec_index = -1;
4147 xassert (charpos >= 0);
4148
4149 /* If STRING is specified, use its multibyteness, otherwise use the
4150 setting of MULTIBYTE, if specified. */
4151 if (multibyte >= 0)
4152 it->multibyte_p = multibyte > 0;
4153
4154 if (s == NULL)
4155 {
4156 xassert (STRINGP (string));
4157 it->string = string;
4158 it->s = NULL;
4159 it->end_charpos = it->string_nchars = SCHARS (string);
4160 it->method = next_element_from_string;
4161 it->current.string_pos = string_pos (charpos, string);
4162 }
4163 else
4164 {
4165 it->s = s;
4166 it->string = Qnil;
4167
4168 /* Note that we use IT->current.pos, not it->current.string_pos,
4169 for displaying C strings. */
4170 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4171 if (it->multibyte_p)
4172 {
4173 it->current.pos = c_string_pos (charpos, s, 1);
4174 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4175 }
4176 else
4177 {
4178 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4179 it->end_charpos = it->string_nchars = strlen (s);
4180 }
4181
4182 it->method = next_element_from_c_string;
4183 }
4184
4185 /* PRECISION > 0 means don't return more than PRECISION characters
4186 from the string. */
4187 if (precision > 0 && it->end_charpos - charpos > precision)
4188 it->end_charpos = it->string_nchars = charpos + precision;
4189
4190 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4191 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4192 FIELD_WIDTH < 0 means infinite field width. This is useful for
4193 padding with `-' at the end of a mode line. */
4194 if (field_width < 0)
4195 field_width = INFINITY;
4196 if (field_width > it->end_charpos - charpos)
4197 it->end_charpos = charpos + field_width;
4198
4199 /* Use the standard display table for displaying strings. */
4200 if (DISP_TABLE_P (Vstandard_display_table))
4201 it->dp = XCHAR_TABLE (Vstandard_display_table);
4202
4203 it->stop_charpos = charpos;
4204 CHECK_IT (it);
4205 }
4206
4207
4208 \f
4209 /***********************************************************************
4210 Iteration
4211 ***********************************************************************/
4212
4213 /* Load IT's display element fields with information about the next
4214 display element from the current position of IT. Value is zero if
4215 end of buffer (or C string) is reached. */
4216
4217 int
4218 get_next_display_element (it)
4219 struct it *it;
4220 {
4221 /* Non-zero means that we found a display element. Zero means that
4222 we hit the end of what we iterate over. Performance note: the
4223 function pointer `method' used here turns out to be faster than
4224 using a sequence of if-statements. */
4225 int success_p = (*it->method) (it);
4226
4227 if (it->what == IT_CHARACTER)
4228 {
4229 /* Map via display table or translate control characters.
4230 IT->c, IT->len etc. have been set to the next character by
4231 the function call above. If we have a display table, and it
4232 contains an entry for IT->c, translate it. Don't do this if
4233 IT->c itself comes from a display table, otherwise we could
4234 end up in an infinite recursion. (An alternative could be to
4235 count the recursion depth of this function and signal an
4236 error when a certain maximum depth is reached.) Is it worth
4237 it? */
4238 if (success_p && it->dpvec == NULL)
4239 {
4240 Lisp_Object dv;
4241
4242 if (it->dp
4243 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
4244 VECTORP (dv)))
4245 {
4246 struct Lisp_Vector *v = XVECTOR (dv);
4247
4248 /* Return the first character from the display table
4249 entry, if not empty. If empty, don't display the
4250 current character. */
4251 if (v->size)
4252 {
4253 it->dpvec_char_len = it->len;
4254 it->dpvec = v->contents;
4255 it->dpend = v->contents + v->size;
4256 it->current.dpvec_index = 0;
4257 it->method = next_element_from_display_vector;
4258 success_p = get_next_display_element (it);
4259 }
4260 else
4261 {
4262 set_iterator_to_next (it, 0);
4263 success_p = get_next_display_element (it);
4264 }
4265 }
4266
4267 /* Translate control characters into `\003' or `^C' form.
4268 Control characters coming from a display table entry are
4269 currently not translated because we use IT->dpvec to hold
4270 the translation. This could easily be changed but I
4271 don't believe that it is worth doing.
4272
4273 If it->multibyte_p is nonzero, eight-bit characters and
4274 non-printable multibyte characters are also translated to
4275 octal form.
4276
4277 If it->multibyte_p is zero, eight-bit characters that
4278 don't have corresponding multibyte char code are also
4279 translated to octal form. */
4280 else if ((it->c < ' '
4281 && (it->area != TEXT_AREA
4282 || (it->c != '\n' && it->c != '\t')))
4283 || (it->multibyte_p
4284 ? ((it->c >= 127
4285 && it->len == 1)
4286 || !CHAR_PRINTABLE_P (it->c))
4287 : (it->c >= 127
4288 && it->c == unibyte_char_to_multibyte (it->c))))
4289 {
4290 /* IT->c is a control character which must be displayed
4291 either as '\003' or as `^C' where the '\\' and '^'
4292 can be defined in the display table. Fill
4293 IT->ctl_chars with glyphs for what we have to
4294 display. Then, set IT->dpvec to these glyphs. */
4295 GLYPH g;
4296
4297 if (it->c < 128 && it->ctl_arrow_p)
4298 {
4299 /* Set IT->ctl_chars[0] to the glyph for `^'. */
4300 if (it->dp
4301 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
4302 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
4303 g = XINT (DISP_CTRL_GLYPH (it->dp));
4304 else
4305 g = FAST_MAKE_GLYPH ('^', 0);
4306 XSETINT (it->ctl_chars[0], g);
4307
4308 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
4309 XSETINT (it->ctl_chars[1], g);
4310
4311 /* Set up IT->dpvec and return first character from it. */
4312 it->dpvec_char_len = it->len;
4313 it->dpvec = it->ctl_chars;
4314 it->dpend = it->dpvec + 2;
4315 it->current.dpvec_index = 0;
4316 it->method = next_element_from_display_vector;
4317 get_next_display_element (it);
4318 }
4319 else
4320 {
4321 unsigned char str[MAX_MULTIBYTE_LENGTH];
4322 int len;
4323 int i;
4324 GLYPH escape_glyph;
4325
4326 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
4327 if (it->dp
4328 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
4329 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
4330 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
4331 else
4332 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
4333
4334 if (SINGLE_BYTE_CHAR_P (it->c))
4335 str[0] = it->c, len = 1;
4336 else
4337 {
4338 len = CHAR_STRING_NO_SIGNAL (it->c, str);
4339 if (len < 0)
4340 {
4341 /* It's an invalid character, which
4342 shouldn't happen actually, but due to
4343 bugs it may happen. Let's print the char
4344 as is, there's not much meaningful we can
4345 do with it. */
4346 str[0] = it->c;
4347 str[1] = it->c >> 8;
4348 str[2] = it->c >> 16;
4349 str[3] = it->c >> 24;
4350 len = 4;
4351 }
4352 }
4353
4354 for (i = 0; i < len; i++)
4355 {
4356 XSETINT (it->ctl_chars[i * 4], escape_glyph);
4357 /* Insert three more glyphs into IT->ctl_chars for
4358 the octal display of the character. */
4359 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
4360 XSETINT (it->ctl_chars[i * 4 + 1], g);
4361 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
4362 XSETINT (it->ctl_chars[i * 4 + 2], g);
4363 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
4364 XSETINT (it->ctl_chars[i * 4 + 3], g);
4365 }
4366
4367 /* Set up IT->dpvec and return the first character
4368 from it. */
4369 it->dpvec_char_len = it->len;
4370 it->dpvec = it->ctl_chars;
4371 it->dpend = it->dpvec + len * 4;
4372 it->current.dpvec_index = 0;
4373 it->method = next_element_from_display_vector;
4374 get_next_display_element (it);
4375 }
4376 }
4377 }
4378
4379 /* Adjust face id for a multibyte character. There are no
4380 multibyte character in unibyte text. */
4381 if (it->multibyte_p
4382 && success_p
4383 && FRAME_WINDOW_P (it->f))
4384 {
4385 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4386 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
4387 }
4388 }
4389
4390 /* Is this character the last one of a run of characters with
4391 box? If yes, set IT->end_of_box_run_p to 1. */
4392 if (it->face_box_p
4393 && it->s == NULL)
4394 {
4395 int face_id;
4396 struct face *face;
4397
4398 it->end_of_box_run_p
4399 = ((face_id = face_after_it_pos (it),
4400 face_id != it->face_id)
4401 && (face = FACE_FROM_ID (it->f, face_id),
4402 face->box == FACE_NO_BOX));
4403 }
4404
4405 /* Value is 0 if end of buffer or string reached. */
4406 return success_p;
4407 }
4408
4409
4410 /* Move IT to the next display element.
4411
4412 RESEAT_P non-zero means if called on a newline in buffer text,
4413 skip to the next visible line start.
4414
4415 Functions get_next_display_element and set_iterator_to_next are
4416 separate because I find this arrangement easier to handle than a
4417 get_next_display_element function that also increments IT's
4418 position. The way it is we can first look at an iterator's current
4419 display element, decide whether it fits on a line, and if it does,
4420 increment the iterator position. The other way around we probably
4421 would either need a flag indicating whether the iterator has to be
4422 incremented the next time, or we would have to implement a
4423 decrement position function which would not be easy to write. */
4424
4425 void
4426 set_iterator_to_next (it, reseat_p)
4427 struct it *it;
4428 int reseat_p;
4429 {
4430 /* Reset flags indicating start and end of a sequence of characters
4431 with box. Reset them at the start of this function because
4432 moving the iterator to a new position might set them. */
4433 it->start_of_box_run_p = it->end_of_box_run_p = 0;
4434
4435 if (it->method == next_element_from_buffer)
4436 {
4437 /* The current display element of IT is a character from
4438 current_buffer. Advance in the buffer, and maybe skip over
4439 invisible lines that are so because of selective display. */
4440 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
4441 reseat_at_next_visible_line_start (it, 0);
4442 else
4443 {
4444 xassert (it->len != 0);
4445 IT_BYTEPOS (*it) += it->len;
4446 IT_CHARPOS (*it) += 1;
4447 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
4448 }
4449 }
4450 else if (it->method == next_element_from_composition)
4451 {
4452 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
4453 if (STRINGP (it->string))
4454 {
4455 IT_STRING_BYTEPOS (*it) += it->len;
4456 IT_STRING_CHARPOS (*it) += it->cmp_len;
4457 it->method = next_element_from_string;
4458 goto consider_string_end;
4459 }
4460 else
4461 {
4462 IT_BYTEPOS (*it) += it->len;
4463 IT_CHARPOS (*it) += it->cmp_len;
4464 it->method = next_element_from_buffer;
4465 }
4466 }
4467 else if (it->method == next_element_from_c_string)
4468 {
4469 /* Current display element of IT is from a C string. */
4470 IT_BYTEPOS (*it) += it->len;
4471 IT_CHARPOS (*it) += 1;
4472 }
4473 else if (it->method == next_element_from_display_vector)
4474 {
4475 /* Current display element of IT is from a display table entry.
4476 Advance in the display table definition. Reset it to null if
4477 end reached, and continue with characters from buffers/
4478 strings. */
4479 ++it->current.dpvec_index;
4480
4481 /* Restore face of the iterator to what they were before the
4482 display vector entry (these entries may contain faces). */
4483 it->face_id = it->saved_face_id;
4484
4485 if (it->dpvec + it->current.dpvec_index == it->dpend)
4486 {
4487 if (it->s)
4488 it->method = next_element_from_c_string;
4489 else if (STRINGP (it->string))
4490 it->method = next_element_from_string;
4491 else
4492 it->method = next_element_from_buffer;
4493
4494 it->dpvec = NULL;
4495 it->current.dpvec_index = -1;
4496
4497 /* Skip over characters which were displayed via IT->dpvec. */
4498 if (it->dpvec_char_len < 0)
4499 reseat_at_next_visible_line_start (it, 1);
4500 else if (it->dpvec_char_len > 0)
4501 {
4502 it->len = it->dpvec_char_len;
4503 set_iterator_to_next (it, reseat_p);
4504 }
4505 }
4506 }
4507 else if (it->method == next_element_from_string)
4508 {
4509 /* Current display element is a character from a Lisp string. */
4510 xassert (it->s == NULL && STRINGP (it->string));
4511 IT_STRING_BYTEPOS (*it) += it->len;
4512 IT_STRING_CHARPOS (*it) += 1;
4513
4514 consider_string_end:
4515
4516 if (it->current.overlay_string_index >= 0)
4517 {
4518 /* IT->string is an overlay string. Advance to the
4519 next, if there is one. */
4520 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
4521 next_overlay_string (it);
4522 }
4523 else
4524 {
4525 /* IT->string is not an overlay string. If we reached
4526 its end, and there is something on IT->stack, proceed
4527 with what is on the stack. This can be either another
4528 string, this time an overlay string, or a buffer. */
4529 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
4530 && it->sp > 0)
4531 {
4532 pop_it (it);
4533 if (!STRINGP (it->string))
4534 it->method = next_element_from_buffer;
4535 else
4536 goto consider_string_end;
4537 }
4538 }
4539 }
4540 else if (it->method == next_element_from_image
4541 || it->method == next_element_from_stretch)
4542 {
4543 /* The position etc with which we have to proceed are on
4544 the stack. The position may be at the end of a string,
4545 if the `display' property takes up the whole string. */
4546 pop_it (it);
4547 it->image_id = 0;
4548 if (STRINGP (it->string))
4549 {
4550 it->method = next_element_from_string;
4551 goto consider_string_end;
4552 }
4553 else
4554 it->method = next_element_from_buffer;
4555 }
4556 else
4557 /* There are no other methods defined, so this should be a bug. */
4558 abort ();
4559
4560 xassert (it->method != next_element_from_string
4561 || (STRINGP (it->string)
4562 && IT_STRING_CHARPOS (*it) >= 0));
4563 }
4564
4565
4566 /* Load IT's display element fields with information about the next
4567 display element which comes from a display table entry or from the
4568 result of translating a control character to one of the forms `^C'
4569 or `\003'. IT->dpvec holds the glyphs to return as characters. */
4570
4571 static int
4572 next_element_from_display_vector (it)
4573 struct it *it;
4574 {
4575 /* Precondition. */
4576 xassert (it->dpvec && it->current.dpvec_index >= 0);
4577
4578 /* Remember the current face id in case glyphs specify faces.
4579 IT's face is restored in set_iterator_to_next. */
4580 it->saved_face_id = it->face_id;
4581
4582 if (INTEGERP (*it->dpvec)
4583 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
4584 {
4585 int lface_id;
4586 GLYPH g;
4587
4588 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
4589 it->c = FAST_GLYPH_CHAR (g);
4590 it->len = CHAR_BYTES (it->c);
4591
4592 /* The entry may contain a face id to use. Such a face id is
4593 the id of a Lisp face, not a realized face. A face id of
4594 zero means no face is specified. */
4595 lface_id = FAST_GLYPH_FACE (g);
4596 if (lface_id)
4597 {
4598 /* The function returns -1 if lface_id is invalid. */
4599 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
4600 if (face_id >= 0)
4601 it->face_id = face_id;
4602 }
4603 }
4604 else
4605 /* Display table entry is invalid. Return a space. */
4606 it->c = ' ', it->len = 1;
4607
4608 /* Don't change position and object of the iterator here. They are
4609 still the values of the character that had this display table
4610 entry or was translated, and that's what we want. */
4611 it->what = IT_CHARACTER;
4612 return 1;
4613 }
4614
4615
4616 /* Load IT with the next display element from Lisp string IT->string.
4617 IT->current.string_pos is the current position within the string.
4618 If IT->current.overlay_string_index >= 0, the Lisp string is an
4619 overlay string. */
4620
4621 static int
4622 next_element_from_string (it)
4623 struct it *it;
4624 {
4625 struct text_pos position;
4626
4627 xassert (STRINGP (it->string));
4628 xassert (IT_STRING_CHARPOS (*it) >= 0);
4629 position = it->current.string_pos;
4630
4631 /* Time to check for invisible text? */
4632 if (IT_STRING_CHARPOS (*it) < it->end_charpos
4633 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
4634 {
4635 handle_stop (it);
4636
4637 /* Since a handler may have changed IT->method, we must
4638 recurse here. */
4639 return get_next_display_element (it);
4640 }
4641
4642 if (it->current.overlay_string_index >= 0)
4643 {
4644 /* Get the next character from an overlay string. In overlay
4645 strings, There is no field width or padding with spaces to
4646 do. */
4647 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
4648 {
4649 it->what = IT_EOB;
4650 return 0;
4651 }
4652 else if (STRING_MULTIBYTE (it->string))
4653 {
4654 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
4655 const unsigned char *s = (SDATA (it->string)
4656 + IT_STRING_BYTEPOS (*it));
4657 it->c = string_char_and_length (s, remaining, &it->len);
4658 }
4659 else
4660 {
4661 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
4662 it->len = 1;
4663 }
4664 }
4665 else
4666 {
4667 /* Get the next character from a Lisp string that is not an
4668 overlay string. Such strings come from the mode line, for
4669 example. We may have to pad with spaces, or truncate the
4670 string. See also next_element_from_c_string. */
4671 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
4672 {
4673 it->what = IT_EOB;
4674 return 0;
4675 }
4676 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
4677 {
4678 /* Pad with spaces. */
4679 it->c = ' ', it->len = 1;
4680 CHARPOS (position) = BYTEPOS (position) = -1;
4681 }
4682 else if (STRING_MULTIBYTE (it->string))
4683 {
4684 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
4685 const unsigned char *s = (SDATA (it->string)
4686 + IT_STRING_BYTEPOS (*it));
4687 it->c = string_char_and_length (s, maxlen, &it->len);
4688 }
4689 else
4690 {
4691 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
4692 it->len = 1;
4693 }
4694 }
4695
4696 /* Record what we have and where it came from. Note that we store a
4697 buffer position in IT->position although it could arguably be a
4698 string position. */
4699 it->what = IT_CHARACTER;
4700 it->object = it->string;
4701 it->position = position;
4702 return 1;
4703 }
4704
4705
4706 /* Load IT with next display element from C string IT->s.
4707 IT->string_nchars is the maximum number of characters to return
4708 from the string. IT->end_charpos may be greater than
4709 IT->string_nchars when this function is called, in which case we
4710 may have to return padding spaces. Value is zero if end of string
4711 reached, including padding spaces. */
4712
4713 static int
4714 next_element_from_c_string (it)
4715 struct it *it;
4716 {
4717 int success_p = 1;
4718
4719 xassert (it->s);
4720 it->what = IT_CHARACTER;
4721 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
4722 it->object = Qnil;
4723
4724 /* IT's position can be greater IT->string_nchars in case a field
4725 width or precision has been specified when the iterator was
4726 initialized. */
4727 if (IT_CHARPOS (*it) >= it->end_charpos)
4728 {
4729 /* End of the game. */
4730 it->what = IT_EOB;
4731 success_p = 0;
4732 }
4733 else if (IT_CHARPOS (*it) >= it->string_nchars)
4734 {
4735 /* Pad with spaces. */
4736 it->c = ' ', it->len = 1;
4737 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
4738 }
4739 else if (it->multibyte_p)
4740 {
4741 /* Implementation note: The calls to strlen apparently aren't a
4742 performance problem because there is no noticeable performance
4743 difference between Emacs running in unibyte or multibyte mode. */
4744 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
4745 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
4746 maxlen, &it->len);
4747 }
4748 else
4749 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
4750
4751 return success_p;
4752 }
4753
4754
4755 /* Set up IT to return characters from an ellipsis, if appropriate.
4756 The definition of the ellipsis glyphs may come from a display table
4757 entry. This function Fills IT with the first glyph from the
4758 ellipsis if an ellipsis is to be displayed. */
4759
4760 static int
4761 next_element_from_ellipsis (it)
4762 struct it *it;
4763 {
4764 if (it->selective_display_ellipsis_p)
4765 {
4766 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4767 {
4768 /* Use the display table definition for `...'. Invalid glyphs
4769 will be handled by the method returning elements from dpvec. */
4770 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4771 it->dpvec_char_len = it->len;
4772 it->dpvec = v->contents;
4773 it->dpend = v->contents + v->size;
4774 it->current.dpvec_index = 0;
4775 it->method = next_element_from_display_vector;
4776 }
4777 else
4778 {
4779 /* Use default `...' which is stored in default_invis_vector. */
4780 it->dpvec_char_len = it->len;
4781 it->dpvec = default_invis_vector;
4782 it->dpend = default_invis_vector + 3;
4783 it->current.dpvec_index = 0;
4784 it->method = next_element_from_display_vector;
4785 }
4786 }
4787 else
4788 {
4789 /* The face at the current position may be different from the
4790 face we find after the invisible text. Remember what it
4791 was in IT->saved_face_id, and signal that it's there by
4792 setting face_before_selective_p. */
4793 it->saved_face_id = it->face_id;
4794 it->method = next_element_from_buffer;
4795 reseat_at_next_visible_line_start (it, 1);
4796 it->face_before_selective_p = 1;
4797 }
4798
4799 return get_next_display_element (it);
4800 }
4801
4802
4803 /* Deliver an image display element. The iterator IT is already
4804 filled with image information (done in handle_display_prop). Value
4805 is always 1. */
4806
4807
4808 static int
4809 next_element_from_image (it)
4810 struct it *it;
4811 {
4812 it->what = IT_IMAGE;
4813 return 1;
4814 }
4815
4816
4817 /* Fill iterator IT with next display element from a stretch glyph
4818 property. IT->object is the value of the text property. Value is
4819 always 1. */
4820
4821 static int
4822 next_element_from_stretch (it)
4823 struct it *it;
4824 {
4825 it->what = IT_STRETCH;
4826 return 1;
4827 }
4828
4829
4830 /* Load IT with the next display element from current_buffer. Value
4831 is zero if end of buffer reached. IT->stop_charpos is the next
4832 position at which to stop and check for text properties or buffer
4833 end. */
4834
4835 static int
4836 next_element_from_buffer (it)
4837 struct it *it;
4838 {
4839 int success_p = 1;
4840
4841 /* Check this assumption, otherwise, we would never enter the
4842 if-statement, below. */
4843 xassert (IT_CHARPOS (*it) >= BEGV
4844 && IT_CHARPOS (*it) <= it->stop_charpos);
4845
4846 if (IT_CHARPOS (*it) >= it->stop_charpos)
4847 {
4848 if (IT_CHARPOS (*it) >= it->end_charpos)
4849 {
4850 int overlay_strings_follow_p;
4851
4852 /* End of the game, except when overlay strings follow that
4853 haven't been returned yet. */
4854 if (it->overlay_strings_at_end_processed_p)
4855 overlay_strings_follow_p = 0;
4856 else
4857 {
4858 it->overlay_strings_at_end_processed_p = 1;
4859 overlay_strings_follow_p = get_overlay_strings (it, 0);
4860 }
4861
4862 if (overlay_strings_follow_p)
4863 success_p = get_next_display_element (it);
4864 else
4865 {
4866 it->what = IT_EOB;
4867 it->position = it->current.pos;
4868 success_p = 0;
4869 }
4870 }
4871 else
4872 {
4873 handle_stop (it);
4874 return get_next_display_element (it);
4875 }
4876 }
4877 else
4878 {
4879 /* No face changes, overlays etc. in sight, so just return a
4880 character from current_buffer. */
4881 unsigned char *p;
4882
4883 /* Maybe run the redisplay end trigger hook. Performance note:
4884 This doesn't seem to cost measurable time. */
4885 if (it->redisplay_end_trigger_charpos
4886 && it->glyph_row
4887 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
4888 run_redisplay_end_trigger_hook (it);
4889
4890 /* Get the next character, maybe multibyte. */
4891 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
4892 if (it->multibyte_p && !ASCII_BYTE_P (*p))
4893 {
4894 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
4895 - IT_BYTEPOS (*it));
4896 it->c = string_char_and_length (p, maxlen, &it->len);
4897 }
4898 else
4899 it->c = *p, it->len = 1;
4900
4901 /* Record what we have and where it came from. */
4902 it->what = IT_CHARACTER;;
4903 it->object = it->w->buffer;
4904 it->position = it->current.pos;
4905
4906 /* Normally we return the character found above, except when we
4907 really want to return an ellipsis for selective display. */
4908 if (it->selective)
4909 {
4910 if (it->c == '\n')
4911 {
4912 /* A value of selective > 0 means hide lines indented more
4913 than that number of columns. */
4914 if (it->selective > 0
4915 && IT_CHARPOS (*it) + 1 < ZV
4916 && indented_beyond_p (IT_CHARPOS (*it) + 1,
4917 IT_BYTEPOS (*it) + 1,
4918 (double) it->selective)) /* iftc */
4919 {
4920 success_p = next_element_from_ellipsis (it);
4921 it->dpvec_char_len = -1;
4922 }
4923 }
4924 else if (it->c == '\r' && it->selective == -1)
4925 {
4926 /* A value of selective == -1 means that everything from the
4927 CR to the end of the line is invisible, with maybe an
4928 ellipsis displayed for it. */
4929 success_p = next_element_from_ellipsis (it);
4930 it->dpvec_char_len = -1;
4931 }
4932 }
4933 }
4934
4935 /* Value is zero if end of buffer reached. */
4936 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
4937 return success_p;
4938 }
4939
4940
4941 /* Run the redisplay end trigger hook for IT. */
4942
4943 static void
4944 run_redisplay_end_trigger_hook (it)
4945 struct it *it;
4946 {
4947 Lisp_Object args[3];
4948
4949 /* IT->glyph_row should be non-null, i.e. we should be actually
4950 displaying something, or otherwise we should not run the hook. */
4951 xassert (it->glyph_row);
4952
4953 /* Set up hook arguments. */
4954 args[0] = Qredisplay_end_trigger_functions;
4955 args[1] = it->window;
4956 XSETINT (args[2], it->redisplay_end_trigger_charpos);
4957 it->redisplay_end_trigger_charpos = 0;
4958
4959 /* Since we are *trying* to run these functions, don't try to run
4960 them again, even if they get an error. */
4961 it->w->redisplay_end_trigger = Qnil;
4962 Frun_hook_with_args (3, args);
4963
4964 /* Notice if it changed the face of the character we are on. */
4965 handle_face_prop (it);
4966 }
4967
4968
4969 /* Deliver a composition display element. The iterator IT is already
4970 filled with composition information (done in
4971 handle_composition_prop). Value is always 1. */
4972
4973 static int
4974 next_element_from_composition (it)
4975 struct it *it;
4976 {
4977 it->what = IT_COMPOSITION;
4978 it->position = (STRINGP (it->string)
4979 ? it->current.string_pos
4980 : it->current.pos);
4981 return 1;
4982 }
4983
4984
4985 \f
4986 /***********************************************************************
4987 Moving an iterator without producing glyphs
4988 ***********************************************************************/
4989
4990 /* Move iterator IT to a specified buffer or X position within one
4991 line on the display without producing glyphs.
4992
4993 OP should be a bit mask including some or all of these bits:
4994 MOVE_TO_X: Stop on reaching x-position TO_X.
4995 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
4996 Regardless of OP's value, stop in reaching the end of the display line.
4997
4998 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
4999 This means, in particular, that TO_X includes window's horizontal
5000 scroll amount.
5001
5002 The return value has several possible values that
5003 say what condition caused the scan to stop:
5004
5005 MOVE_POS_MATCH_OR_ZV
5006 - when TO_POS or ZV was reached.
5007
5008 MOVE_X_REACHED
5009 -when TO_X was reached before TO_POS or ZV were reached.
5010
5011 MOVE_LINE_CONTINUED
5012 - when we reached the end of the display area and the line must
5013 be continued.
5014
5015 MOVE_LINE_TRUNCATED
5016 - when we reached the end of the display area and the line is
5017 truncated.
5018
5019 MOVE_NEWLINE_OR_CR
5020 - when we stopped at a line end, i.e. a newline or a CR and selective
5021 display is on. */
5022
5023 static enum move_it_result
5024 move_it_in_display_line_to (it, to_charpos, to_x, op)
5025 struct it *it;
5026 int to_charpos, to_x, op;
5027 {
5028 enum move_it_result result = MOVE_UNDEFINED;
5029 struct glyph_row *saved_glyph_row;
5030
5031 /* Don't produce glyphs in produce_glyphs. */
5032 saved_glyph_row = it->glyph_row;
5033 it->glyph_row = NULL;
5034
5035 while (1)
5036 {
5037 int x, i, ascent = 0, descent = 0;
5038
5039 /* Stop when ZV or TO_CHARPOS reached. */
5040 if (!get_next_display_element (it)
5041 || ((op & MOVE_TO_POS) != 0
5042 && BUFFERP (it->object)
5043 && IT_CHARPOS (*it) >= to_charpos))
5044 {
5045 result = MOVE_POS_MATCH_OR_ZV;
5046 break;
5047 }
5048
5049 /* The call to produce_glyphs will get the metrics of the
5050 display element IT is loaded with. We record in x the
5051 x-position before this display element in case it does not
5052 fit on the line. */
5053 x = it->current_x;
5054
5055 /* Remember the line height so far in case the next element doesn't
5056 fit on the line. */
5057 if (!it->truncate_lines_p)
5058 {
5059 ascent = it->max_ascent;
5060 descent = it->max_descent;
5061 }
5062
5063 PRODUCE_GLYPHS (it);
5064
5065 if (it->area != TEXT_AREA)
5066 {
5067 set_iterator_to_next (it, 1);
5068 continue;
5069 }
5070
5071 /* The number of glyphs we get back in IT->nglyphs will normally
5072 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
5073 character on a terminal frame, or (iii) a line end. For the
5074 second case, IT->nglyphs - 1 padding glyphs will be present
5075 (on X frames, there is only one glyph produced for a
5076 composite character.
5077
5078 The behavior implemented below means, for continuation lines,
5079 that as many spaces of a TAB as fit on the current line are
5080 displayed there. For terminal frames, as many glyphs of a
5081 multi-glyph character are displayed in the current line, too.
5082 This is what the old redisplay code did, and we keep it that
5083 way. Under X, the whole shape of a complex character must
5084 fit on the line or it will be completely displayed in the
5085 next line.
5086
5087 Note that both for tabs and padding glyphs, all glyphs have
5088 the same width. */
5089 if (it->nglyphs)
5090 {
5091 /* More than one glyph or glyph doesn't fit on line. All
5092 glyphs have the same width. */
5093 int single_glyph_width = it->pixel_width / it->nglyphs;
5094 int new_x;
5095
5096 for (i = 0; i < it->nglyphs; ++i, x = new_x)
5097 {
5098 new_x = x + single_glyph_width;
5099
5100 /* We want to leave anything reaching TO_X to the caller. */
5101 if ((op & MOVE_TO_X) && new_x > to_x)
5102 {
5103 it->current_x = x;
5104 result = MOVE_X_REACHED;
5105 break;
5106 }
5107 else if (/* Lines are continued. */
5108 !it->truncate_lines_p
5109 && (/* And glyph doesn't fit on the line. */
5110 new_x > it->last_visible_x
5111 /* Or it fits exactly and we're on a window
5112 system frame. */
5113 || (new_x == it->last_visible_x
5114 && FRAME_WINDOW_P (it->f))))
5115 {
5116 if (/* IT->hpos == 0 means the very first glyph
5117 doesn't fit on the line, e.g. a wide image. */
5118 it->hpos == 0
5119 || (new_x == it->last_visible_x
5120 && FRAME_WINDOW_P (it->f)))
5121 {
5122 ++it->hpos;
5123 it->current_x = new_x;
5124 if (i == it->nglyphs - 1)
5125 set_iterator_to_next (it, 1);
5126 }
5127 else
5128 {
5129 it->current_x = x;
5130 it->max_ascent = ascent;
5131 it->max_descent = descent;
5132 }
5133
5134 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
5135 IT_CHARPOS (*it)));
5136 result = MOVE_LINE_CONTINUED;
5137 break;
5138 }
5139 else if (new_x > it->first_visible_x)
5140 {
5141 /* Glyph is visible. Increment number of glyphs that
5142 would be displayed. */
5143 ++it->hpos;
5144 }
5145 else
5146 {
5147 /* Glyph is completely off the left margin of the display
5148 area. Nothing to do. */
5149 }
5150 }
5151
5152 if (result != MOVE_UNDEFINED)
5153 break;
5154 }
5155 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
5156 {
5157 /* Stop when TO_X specified and reached. This check is
5158 necessary here because of lines consisting of a line end,
5159 only. The line end will not produce any glyphs and we
5160 would never get MOVE_X_REACHED. */
5161 xassert (it->nglyphs == 0);
5162 result = MOVE_X_REACHED;
5163 break;
5164 }
5165
5166 /* Is this a line end? If yes, we're done. */
5167 if (ITERATOR_AT_END_OF_LINE_P (it))
5168 {
5169 result = MOVE_NEWLINE_OR_CR;
5170 break;
5171 }
5172
5173 /* The current display element has been consumed. Advance
5174 to the next. */
5175 set_iterator_to_next (it, 1);
5176
5177 /* Stop if lines are truncated and IT's current x-position is
5178 past the right edge of the window now. */
5179 if (it->truncate_lines_p
5180 && it->current_x >= it->last_visible_x)
5181 {
5182 result = MOVE_LINE_TRUNCATED;
5183 break;
5184 }
5185 }
5186
5187 /* Restore the iterator settings altered at the beginning of this
5188 function. */
5189 it->glyph_row = saved_glyph_row;
5190 return result;
5191 }
5192
5193
5194 /* Move IT forward until it satisfies one or more of the criteria in
5195 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
5196
5197 OP is a bit-mask that specifies where to stop, and in particular,
5198 which of those four position arguments makes a difference. See the
5199 description of enum move_operation_enum.
5200
5201 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
5202 screen line, this function will set IT to the next position >
5203 TO_CHARPOS. */
5204
5205 void
5206 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
5207 struct it *it;
5208 int to_charpos, to_x, to_y, to_vpos;
5209 int op;
5210 {
5211 enum move_it_result skip, skip2 = MOVE_X_REACHED;
5212 int line_height;
5213 int reached = 0;
5214
5215 for (;;)
5216 {
5217 if (op & MOVE_TO_VPOS)
5218 {
5219 /* If no TO_CHARPOS and no TO_X specified, stop at the
5220 start of the line TO_VPOS. */
5221 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
5222 {
5223 if (it->vpos == to_vpos)
5224 {
5225 reached = 1;
5226 break;
5227 }
5228 else
5229 skip = move_it_in_display_line_to (it, -1, -1, 0);
5230 }
5231 else
5232 {
5233 /* TO_VPOS >= 0 means stop at TO_X in the line at
5234 TO_VPOS, or at TO_POS, whichever comes first. */
5235 if (it->vpos == to_vpos)
5236 {
5237 reached = 2;
5238 break;
5239 }
5240
5241 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
5242
5243 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
5244 {
5245 reached = 3;
5246 break;
5247 }
5248 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
5249 {
5250 /* We have reached TO_X but not in the line we want. */
5251 skip = move_it_in_display_line_to (it, to_charpos,
5252 -1, MOVE_TO_POS);
5253 if (skip == MOVE_POS_MATCH_OR_ZV)
5254 {
5255 reached = 4;
5256 break;
5257 }
5258 }
5259 }
5260 }
5261 else if (op & MOVE_TO_Y)
5262 {
5263 struct it it_backup;
5264
5265 /* TO_Y specified means stop at TO_X in the line containing
5266 TO_Y---or at TO_CHARPOS if this is reached first. The
5267 problem is that we can't really tell whether the line
5268 contains TO_Y before we have completely scanned it, and
5269 this may skip past TO_X. What we do is to first scan to
5270 TO_X.
5271
5272 If TO_X is not specified, use a TO_X of zero. The reason
5273 is to make the outcome of this function more predictable.
5274 If we didn't use TO_X == 0, we would stop at the end of
5275 the line which is probably not what a caller would expect
5276 to happen. */
5277 skip = move_it_in_display_line_to (it, to_charpos,
5278 ((op & MOVE_TO_X)
5279 ? to_x : 0),
5280 (MOVE_TO_X
5281 | (op & MOVE_TO_POS)));
5282
5283 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
5284 if (skip == MOVE_POS_MATCH_OR_ZV)
5285 {
5286 reached = 5;
5287 break;
5288 }
5289
5290 /* If TO_X was reached, we would like to know whether TO_Y
5291 is in the line. This can only be said if we know the
5292 total line height which requires us to scan the rest of
5293 the line. */
5294 if (skip == MOVE_X_REACHED)
5295 {
5296 it_backup = *it;
5297 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
5298 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
5299 op & MOVE_TO_POS);
5300 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
5301 }
5302
5303 /* Now, decide whether TO_Y is in this line. */
5304 line_height = it->max_ascent + it->max_descent;
5305 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
5306
5307 if (to_y >= it->current_y
5308 && to_y < it->current_y + line_height)
5309 {
5310 if (skip == MOVE_X_REACHED)
5311 /* If TO_Y is in this line and TO_X was reached above,
5312 we scanned too far. We have to restore IT's settings
5313 to the ones before skipping. */
5314 *it = it_backup;
5315 reached = 6;
5316 }
5317 else if (skip == MOVE_X_REACHED)
5318 {
5319 skip = skip2;
5320 if (skip == MOVE_POS_MATCH_OR_ZV)
5321 reached = 7;
5322 }
5323
5324 if (reached)
5325 break;
5326 }
5327 else
5328 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
5329
5330 switch (skip)
5331 {
5332 case MOVE_POS_MATCH_OR_ZV:
5333 reached = 8;
5334 goto out;
5335
5336 case MOVE_NEWLINE_OR_CR:
5337 set_iterator_to_next (it, 1);
5338 it->continuation_lines_width = 0;
5339 break;
5340
5341 case MOVE_LINE_TRUNCATED:
5342 it->continuation_lines_width = 0;
5343 reseat_at_next_visible_line_start (it, 0);
5344 if ((op & MOVE_TO_POS) != 0
5345 && IT_CHARPOS (*it) > to_charpos)
5346 {
5347 reached = 9;
5348 goto out;
5349 }
5350 break;
5351
5352 case MOVE_LINE_CONTINUED:
5353 it->continuation_lines_width += it->current_x;
5354 break;
5355
5356 default:
5357 abort ();
5358 }
5359
5360 /* Reset/increment for the next run. */
5361 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
5362 it->current_x = it->hpos = 0;
5363 it->current_y += it->max_ascent + it->max_descent;
5364 ++it->vpos;
5365 last_height = it->max_ascent + it->max_descent;
5366 last_max_ascent = it->max_ascent;
5367 it->max_ascent = it->max_descent = 0;
5368 }
5369
5370 out:
5371
5372 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
5373 }
5374
5375
5376 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
5377
5378 If DY > 0, move IT backward at least that many pixels. DY = 0
5379 means move IT backward to the preceding line start or BEGV. This
5380 function may move over more than DY pixels if IT->current_y - DY
5381 ends up in the middle of a line; in this case IT->current_y will be
5382 set to the top of the line moved to. */
5383
5384 void
5385 move_it_vertically_backward (it, dy)
5386 struct it *it;
5387 int dy;
5388 {
5389 int nlines, h;
5390 struct it it2, it3;
5391 int start_pos = IT_CHARPOS (*it);
5392
5393 xassert (dy >= 0);
5394
5395 /* Estimate how many newlines we must move back. */
5396 nlines = max (1, dy / CANON_Y_UNIT (it->f));
5397
5398 /* Set the iterator's position that many lines back. */
5399 while (nlines-- && IT_CHARPOS (*it) > BEGV)
5400 back_to_previous_visible_line_start (it);
5401
5402 /* Reseat the iterator here. When moving backward, we don't want
5403 reseat to skip forward over invisible text, set up the iterator
5404 to deliver from overlay strings at the new position etc. So,
5405 use reseat_1 here. */
5406 reseat_1 (it, it->current.pos, 1);
5407
5408 /* We are now surely at a line start. */
5409 it->current_x = it->hpos = 0;
5410
5411 /* Move forward and see what y-distance we moved. First move to the
5412 start of the next line so that we get its height. We need this
5413 height to be able to tell whether we reached the specified
5414 y-distance. */
5415 it2 = *it;
5416 it2.max_ascent = it2.max_descent = 0;
5417 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
5418 MOVE_TO_POS | MOVE_TO_VPOS);
5419 xassert (IT_CHARPOS (*it) >= BEGV);
5420 it3 = it2;
5421
5422 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
5423 xassert (IT_CHARPOS (*it) >= BEGV);
5424 h = it2.current_y - it->current_y;
5425 nlines = it2.vpos - it->vpos;
5426
5427 /* Correct IT's y and vpos position. */
5428 it->vpos -= nlines;
5429 it->current_y -= h;
5430
5431 if (dy == 0)
5432 {
5433 /* DY == 0 means move to the start of the screen line. The
5434 value of nlines is > 0 if continuation lines were involved. */
5435 if (nlines > 0)
5436 move_it_by_lines (it, nlines, 1);
5437 xassert (IT_CHARPOS (*it) <= start_pos);
5438 }
5439 else if (nlines)
5440 {
5441 /* The y-position we try to reach. Note that h has been
5442 subtracted in front of the if-statement. */
5443 int target_y = it->current_y + h - dy;
5444 int y0 = it3.current_y;
5445 int y1 = line_bottom_y (&it3);
5446 int line_height = y1 - y0;
5447
5448 /* If we did not reach target_y, try to move further backward if
5449 we can. If we moved too far backward, try to move forward. */
5450 if (target_y < it->current_y
5451 /* This is heuristic. In a window that's 3 lines high, with
5452 a line height of 13 pixels each, recentering with point
5453 on the bottom line will try to move -39/2 = 19 pixels
5454 backward. Try to avoid moving into the first line. */
5455 && it->current_y - target_y > line_height / 3 * 2
5456 && IT_CHARPOS (*it) > BEGV)
5457 {
5458 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
5459 target_y - it->current_y));
5460 move_it_vertically (it, target_y - it->current_y);
5461 xassert (IT_CHARPOS (*it) >= BEGV);
5462 }
5463 else if (target_y >= it->current_y + line_height
5464 && IT_CHARPOS (*it) < ZV)
5465 {
5466 /* Should move forward by at least one line, maybe more.
5467
5468 Note: Calling move_it_by_lines can be expensive on
5469 terminal frames, where compute_motion is used (via
5470 vmotion) to do the job, when there are very long lines
5471 and truncate-lines is nil. That's the reason for
5472 treating terminal frames specially here. */
5473
5474 if (!FRAME_WINDOW_P (it->f))
5475 move_it_vertically (it, target_y - (it->current_y + line_height));
5476 else
5477 {
5478 do
5479 {
5480 move_it_by_lines (it, 1, 1);
5481 }
5482 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
5483 }
5484
5485 xassert (IT_CHARPOS (*it) >= BEGV);
5486 }
5487 }
5488 }
5489
5490
5491 /* Move IT by a specified amount of pixel lines DY. DY negative means
5492 move backwards. DY = 0 means move to start of screen line. At the
5493 end, IT will be on the start of a screen line. */
5494
5495 void
5496 move_it_vertically (it, dy)
5497 struct it *it;
5498 int dy;
5499 {
5500 if (dy <= 0)
5501 move_it_vertically_backward (it, -dy);
5502 else if (dy > 0)
5503 {
5504 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
5505 move_it_to (it, ZV, -1, it->current_y + dy, -1,
5506 MOVE_TO_POS | MOVE_TO_Y);
5507 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
5508
5509 /* If buffer ends in ZV without a newline, move to the start of
5510 the line to satisfy the post-condition. */
5511 if (IT_CHARPOS (*it) == ZV
5512 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
5513 move_it_by_lines (it, 0, 0);
5514 }
5515 }
5516
5517
5518 /* Move iterator IT past the end of the text line it is in. */
5519
5520 void
5521 move_it_past_eol (it)
5522 struct it *it;
5523 {
5524 enum move_it_result rc;
5525
5526 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
5527 if (rc == MOVE_NEWLINE_OR_CR)
5528 set_iterator_to_next (it, 0);
5529 }
5530
5531
5532 #if 0 /* Currently not used. */
5533
5534 /* Return non-zero if some text between buffer positions START_CHARPOS
5535 and END_CHARPOS is invisible. IT->window is the window for text
5536 property lookup. */
5537
5538 static int
5539 invisible_text_between_p (it, start_charpos, end_charpos)
5540 struct it *it;
5541 int start_charpos, end_charpos;
5542 {
5543 Lisp_Object prop, limit;
5544 int invisible_found_p;
5545
5546 xassert (it != NULL && start_charpos <= end_charpos);
5547
5548 /* Is text at START invisible? */
5549 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
5550 it->window);
5551 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5552 invisible_found_p = 1;
5553 else
5554 {
5555 limit = Fnext_single_char_property_change (make_number (start_charpos),
5556 Qinvisible, Qnil,
5557 make_number (end_charpos));
5558 invisible_found_p = XFASTINT (limit) < end_charpos;
5559 }
5560
5561 return invisible_found_p;
5562 }
5563
5564 #endif /* 0 */
5565
5566
5567 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
5568 negative means move up. DVPOS == 0 means move to the start of the
5569 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
5570 NEED_Y_P is zero, IT->current_y will be left unchanged.
5571
5572 Further optimization ideas: If we would know that IT->f doesn't use
5573 a face with proportional font, we could be faster for
5574 truncate-lines nil. */
5575
5576 void
5577 move_it_by_lines (it, dvpos, need_y_p)
5578 struct it *it;
5579 int dvpos, need_y_p;
5580 {
5581 struct position pos;
5582
5583 if (!FRAME_WINDOW_P (it->f))
5584 {
5585 struct text_pos textpos;
5586
5587 /* We can use vmotion on frames without proportional fonts. */
5588 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
5589 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
5590 reseat (it, textpos, 1);
5591 it->vpos += pos.vpos;
5592 it->current_y += pos.vpos;
5593 }
5594 else if (dvpos == 0)
5595 {
5596 /* DVPOS == 0 means move to the start of the screen line. */
5597 move_it_vertically_backward (it, 0);
5598 xassert (it->current_x == 0 && it->hpos == 0);
5599 }
5600 else if (dvpos > 0)
5601 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
5602 else
5603 {
5604 struct it it2;
5605 int start_charpos, i;
5606
5607 /* Start at the beginning of the screen line containing IT's
5608 position. */
5609 move_it_vertically_backward (it, 0);
5610
5611 /* Go back -DVPOS visible lines and reseat the iterator there. */
5612 start_charpos = IT_CHARPOS (*it);
5613 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
5614 back_to_previous_visible_line_start (it);
5615 reseat (it, it->current.pos, 1);
5616 it->current_x = it->hpos = 0;
5617
5618 /* Above call may have moved too far if continuation lines
5619 are involved. Scan forward and see if it did. */
5620 it2 = *it;
5621 it2.vpos = it2.current_y = 0;
5622 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
5623 it->vpos -= it2.vpos;
5624 it->current_y -= it2.current_y;
5625 it->current_x = it->hpos = 0;
5626
5627 /* If we moved too far, move IT some lines forward. */
5628 if (it2.vpos > -dvpos)
5629 {
5630 int delta = it2.vpos + dvpos;
5631 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
5632 }
5633 }
5634 }
5635
5636
5637 \f
5638 /***********************************************************************
5639 Messages
5640 ***********************************************************************/
5641
5642
5643 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
5644 to *Messages*. */
5645
5646 void
5647 add_to_log (format, arg1, arg2)
5648 char *format;
5649 Lisp_Object arg1, arg2;
5650 {
5651 Lisp_Object args[3];
5652 Lisp_Object msg, fmt;
5653 char *buffer;
5654 int len;
5655 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5656
5657 /* Do nothing if called asynchronously. Inserting text into
5658 a buffer may call after-change-functions and alike and
5659 that would means running Lisp asynchronously. */
5660 if (handling_signal)
5661 return;
5662
5663 fmt = msg = Qnil;
5664 GCPRO4 (fmt, msg, arg1, arg2);
5665
5666 args[0] = fmt = build_string (format);
5667 args[1] = arg1;
5668 args[2] = arg2;
5669 msg = Fformat (3, args);
5670
5671 len = SBYTES (msg) + 1;
5672 buffer = (char *) alloca (len);
5673 bcopy (SDATA (msg), buffer, len);
5674
5675 message_dolog (buffer, len - 1, 1, 0);
5676 UNGCPRO;
5677 }
5678
5679
5680 /* Output a newline in the *Messages* buffer if "needs" one. */
5681
5682 void
5683 message_log_maybe_newline ()
5684 {
5685 if (message_log_need_newline)
5686 message_dolog ("", 0, 1, 0);
5687 }
5688
5689
5690 /* Add a string M of length NBYTES to the message log, optionally
5691 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
5692 nonzero, means interpret the contents of M as multibyte. This
5693 function calls low-level routines in order to bypass text property
5694 hooks, etc. which might not be safe to run. */
5695
5696 void
5697 message_dolog (m, nbytes, nlflag, multibyte)
5698 const char *m;
5699 int nbytes, nlflag, multibyte;
5700 {
5701 if (!NILP (Vmemory_full))
5702 return;
5703
5704 if (!NILP (Vmessage_log_max))
5705 {
5706 struct buffer *oldbuf;
5707 Lisp_Object oldpoint, oldbegv, oldzv;
5708 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5709 int point_at_end = 0;
5710 int zv_at_end = 0;
5711 Lisp_Object old_deactivate_mark, tem;
5712 struct gcpro gcpro1;
5713
5714 old_deactivate_mark = Vdeactivate_mark;
5715 oldbuf = current_buffer;
5716 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
5717 current_buffer->undo_list = Qt;
5718
5719 oldpoint = message_dolog_marker1;
5720 set_marker_restricted (oldpoint, make_number (PT), Qnil);
5721 oldbegv = message_dolog_marker2;
5722 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
5723 oldzv = message_dolog_marker3;
5724 set_marker_restricted (oldzv, make_number (ZV), Qnil);
5725 GCPRO1 (old_deactivate_mark);
5726
5727 if (PT == Z)
5728 point_at_end = 1;
5729 if (ZV == Z)
5730 zv_at_end = 1;
5731
5732 BEGV = BEG;
5733 BEGV_BYTE = BEG_BYTE;
5734 ZV = Z;
5735 ZV_BYTE = Z_BYTE;
5736 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5737
5738 /* Insert the string--maybe converting multibyte to single byte
5739 or vice versa, so that all the text fits the buffer. */
5740 if (multibyte
5741 && NILP (current_buffer->enable_multibyte_characters))
5742 {
5743 int i, c, char_bytes;
5744 unsigned char work[1];
5745
5746 /* Convert a multibyte string to single-byte
5747 for the *Message* buffer. */
5748 for (i = 0; i < nbytes; i += nbytes)
5749 {
5750 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
5751 work[0] = (SINGLE_BYTE_CHAR_P (c)
5752 ? c
5753 : multibyte_char_to_unibyte (c, Qnil));
5754 insert_1_both (work, 1, 1, 1, 0, 0);
5755 }
5756 }
5757 else if (! multibyte
5758 && ! NILP (current_buffer->enable_multibyte_characters))
5759 {
5760 int i, c, char_bytes;
5761 unsigned char *msg = (unsigned char *) m;
5762 unsigned char str[MAX_MULTIBYTE_LENGTH];
5763 /* Convert a single-byte string to multibyte
5764 for the *Message* buffer. */
5765 for (i = 0; i < nbytes; i++)
5766 {
5767 c = unibyte_char_to_multibyte (msg[i]);
5768 char_bytes = CHAR_STRING (c, str);
5769 insert_1_both (str, 1, char_bytes, 1, 0, 0);
5770 }
5771 }
5772 else if (nbytes)
5773 insert_1 (m, nbytes, 1, 0, 0);
5774
5775 if (nlflag)
5776 {
5777 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
5778 insert_1 ("\n", 1, 1, 0, 0);
5779
5780 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
5781 this_bol = PT;
5782 this_bol_byte = PT_BYTE;
5783
5784 /* See if this line duplicates the previous one.
5785 If so, combine duplicates. */
5786 if (this_bol > BEG)
5787 {
5788 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
5789 prev_bol = PT;
5790 prev_bol_byte = PT_BYTE;
5791
5792 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
5793 this_bol, this_bol_byte);
5794 if (dup)
5795 {
5796 del_range_both (prev_bol, prev_bol_byte,
5797 this_bol, this_bol_byte, 0);
5798 if (dup > 1)
5799 {
5800 char dupstr[40];
5801 int duplen;
5802
5803 /* If you change this format, don't forget to also
5804 change message_log_check_duplicate. */
5805 sprintf (dupstr, " [%d times]", dup);
5806 duplen = strlen (dupstr);
5807 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
5808 insert_1 (dupstr, duplen, 1, 0, 1);
5809 }
5810 }
5811 }
5812
5813 /* If we have more than the desired maximum number of lines
5814 in the *Messages* buffer now, delete the oldest ones.
5815 This is safe because we don't have undo in this buffer. */
5816
5817 if (NATNUMP (Vmessage_log_max))
5818 {
5819 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
5820 -XFASTINT (Vmessage_log_max) - 1, 0);
5821 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
5822 }
5823 }
5824 BEGV = XMARKER (oldbegv)->charpos;
5825 BEGV_BYTE = marker_byte_position (oldbegv);
5826
5827 if (zv_at_end)
5828 {
5829 ZV = Z;
5830 ZV_BYTE = Z_BYTE;
5831 }
5832 else
5833 {
5834 ZV = XMARKER (oldzv)->charpos;
5835 ZV_BYTE = marker_byte_position (oldzv);
5836 }
5837
5838 if (point_at_end)
5839 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5840 else
5841 /* We can't do Fgoto_char (oldpoint) because it will run some
5842 Lisp code. */
5843 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
5844 XMARKER (oldpoint)->bytepos);
5845
5846 UNGCPRO;
5847 unchain_marker (oldpoint);
5848 unchain_marker (oldbegv);
5849 unchain_marker (oldzv);
5850
5851 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
5852 set_buffer_internal (oldbuf);
5853 if (NILP (tem))
5854 windows_or_buffers_changed = old_windows_or_buffers_changed;
5855 message_log_need_newline = !nlflag;
5856 Vdeactivate_mark = old_deactivate_mark;
5857 }
5858 }
5859
5860
5861 /* We are at the end of the buffer after just having inserted a newline.
5862 (Note: We depend on the fact we won't be crossing the gap.)
5863 Check to see if the most recent message looks a lot like the previous one.
5864 Return 0 if different, 1 if the new one should just replace it, or a
5865 value N > 1 if we should also append " [N times]". */
5866
5867 static int
5868 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
5869 int prev_bol, this_bol;
5870 int prev_bol_byte, this_bol_byte;
5871 {
5872 int i;
5873 int len = Z_BYTE - 1 - this_bol_byte;
5874 int seen_dots = 0;
5875 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
5876 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
5877
5878 for (i = 0; i < len; i++)
5879 {
5880 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
5881 seen_dots = 1;
5882 if (p1[i] != p2[i])
5883 return seen_dots;
5884 }
5885 p1 += len;
5886 if (*p1 == '\n')
5887 return 2;
5888 if (*p1++ == ' ' && *p1++ == '[')
5889 {
5890 int n = 0;
5891 while (*p1 >= '0' && *p1 <= '9')
5892 n = n * 10 + *p1++ - '0';
5893 if (strncmp (p1, " times]\n", 8) == 0)
5894 return n+1;
5895 }
5896 return 0;
5897 }
5898
5899
5900 /* Display an echo area message M with a specified length of NBYTES
5901 bytes. The string may include null characters. If M is 0, clear
5902 out any existing message, and let the mini-buffer text show
5903 through.
5904
5905 The buffer M must continue to exist until after the echo area gets
5906 cleared or some other message gets displayed there. This means do
5907 not pass text that is stored in a Lisp string; do not pass text in
5908 a buffer that was alloca'd. */
5909
5910 void
5911 message2 (m, nbytes, multibyte)
5912 const char *m;
5913 int nbytes;
5914 int multibyte;
5915 {
5916 /* First flush out any partial line written with print. */
5917 message_log_maybe_newline ();
5918 if (m)
5919 message_dolog (m, nbytes, 1, multibyte);
5920 message2_nolog (m, nbytes, multibyte);
5921 }
5922
5923
5924 /* The non-logging counterpart of message2. */
5925
5926 void
5927 message2_nolog (m, nbytes, multibyte)
5928 const char *m;
5929 int nbytes, multibyte;
5930 {
5931 struct frame *sf = SELECTED_FRAME ();
5932 message_enable_multibyte = multibyte;
5933
5934 if (noninteractive)
5935 {
5936 if (noninteractive_need_newline)
5937 putc ('\n', stderr);
5938 noninteractive_need_newline = 0;
5939 if (m)
5940 fwrite (m, nbytes, 1, stderr);
5941 if (cursor_in_echo_area == 0)
5942 fprintf (stderr, "\n");
5943 fflush (stderr);
5944 }
5945 /* A null message buffer means that the frame hasn't really been
5946 initialized yet. Error messages get reported properly by
5947 cmd_error, so this must be just an informative message; toss it. */
5948 else if (INTERACTIVE
5949 && sf->glyphs_initialized_p
5950 && FRAME_MESSAGE_BUF (sf))
5951 {
5952 Lisp_Object mini_window;
5953 struct frame *f;
5954
5955 /* Get the frame containing the mini-buffer
5956 that the selected frame is using. */
5957 mini_window = FRAME_MINIBUF_WINDOW (sf);
5958 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5959
5960 FRAME_SAMPLE_VISIBILITY (f);
5961 if (FRAME_VISIBLE_P (sf)
5962 && ! FRAME_VISIBLE_P (f))
5963 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
5964
5965 if (m)
5966 {
5967 set_message (m, Qnil, nbytes, multibyte);
5968 if (minibuffer_auto_raise)
5969 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5970 }
5971 else
5972 clear_message (1, 1);
5973
5974 do_pending_window_change (0);
5975 echo_area_display (1);
5976 do_pending_window_change (0);
5977 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5978 (*frame_up_to_date_hook) (f);
5979 }
5980 }
5981
5982
5983 /* Display an echo area message M with a specified length of NBYTES
5984 bytes. The string may include null characters. If M is not a
5985 string, clear out any existing message, and let the mini-buffer
5986 text show through. */
5987
5988 void
5989 message3 (m, nbytes, multibyte)
5990 Lisp_Object m;
5991 int nbytes;
5992 int multibyte;
5993 {
5994 struct gcpro gcpro1;
5995
5996 GCPRO1 (m);
5997
5998 /* First flush out any partial line written with print. */
5999 message_log_maybe_newline ();
6000 if (STRINGP (m))
6001 message_dolog (SDATA (m), nbytes, 1, multibyte);
6002 message3_nolog (m, nbytes, multibyte);
6003
6004 UNGCPRO;
6005 }
6006
6007
6008 /* The non-logging version of message3. */
6009
6010 void
6011 message3_nolog (m, nbytes, multibyte)
6012 Lisp_Object m;
6013 int nbytes, multibyte;
6014 {
6015 struct frame *sf = SELECTED_FRAME ();
6016 message_enable_multibyte = multibyte;
6017
6018 if (noninteractive)
6019 {
6020 if (noninteractive_need_newline)
6021 putc ('\n', stderr);
6022 noninteractive_need_newline = 0;
6023 if (STRINGP (m))
6024 fwrite (SDATA (m), nbytes, 1, stderr);
6025 if (cursor_in_echo_area == 0)
6026 fprintf (stderr, "\n");
6027 fflush (stderr);
6028 }
6029 /* A null message buffer means that the frame hasn't really been
6030 initialized yet. Error messages get reported properly by
6031 cmd_error, so this must be just an informative message; toss it. */
6032 else if (INTERACTIVE
6033 && sf->glyphs_initialized_p
6034 && FRAME_MESSAGE_BUF (sf))
6035 {
6036 Lisp_Object mini_window;
6037 Lisp_Object frame;
6038 struct frame *f;
6039
6040 /* Get the frame containing the mini-buffer
6041 that the selected frame is using. */
6042 mini_window = FRAME_MINIBUF_WINDOW (sf);
6043 frame = XWINDOW (mini_window)->frame;
6044 f = XFRAME (frame);
6045
6046 FRAME_SAMPLE_VISIBILITY (f);
6047 if (FRAME_VISIBLE_P (sf)
6048 && !FRAME_VISIBLE_P (f))
6049 Fmake_frame_visible (frame);
6050
6051 if (STRINGP (m) && SCHARS (m) > 0)
6052 {
6053 set_message (NULL, m, nbytes, multibyte);
6054 if (minibuffer_auto_raise)
6055 Fraise_frame (frame);
6056 }
6057 else
6058 clear_message (1, 1);
6059
6060 do_pending_window_change (0);
6061 echo_area_display (1);
6062 do_pending_window_change (0);
6063 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6064 (*frame_up_to_date_hook) (f);
6065 }
6066 }
6067
6068
6069 /* Display a null-terminated echo area message M. If M is 0, clear
6070 out any existing message, and let the mini-buffer text show through.
6071
6072 The buffer M must continue to exist until after the echo area gets
6073 cleared or some other message gets displayed there. Do not pass
6074 text that is stored in a Lisp string. Do not pass text in a buffer
6075 that was alloca'd. */
6076
6077 void
6078 message1 (m)
6079 char *m;
6080 {
6081 message2 (m, (m ? strlen (m) : 0), 0);
6082 }
6083
6084
6085 /* The non-logging counterpart of message1. */
6086
6087 void
6088 message1_nolog (m)
6089 char *m;
6090 {
6091 message2_nolog (m, (m ? strlen (m) : 0), 0);
6092 }
6093
6094 /* Display a message M which contains a single %s
6095 which gets replaced with STRING. */
6096
6097 void
6098 message_with_string (m, string, log)
6099 char *m;
6100 Lisp_Object string;
6101 int log;
6102 {
6103 CHECK_STRING (string);
6104
6105 if (noninteractive)
6106 {
6107 if (m)
6108 {
6109 if (noninteractive_need_newline)
6110 putc ('\n', stderr);
6111 noninteractive_need_newline = 0;
6112 fprintf (stderr, m, SDATA (string));
6113 if (cursor_in_echo_area == 0)
6114 fprintf (stderr, "\n");
6115 fflush (stderr);
6116 }
6117 }
6118 else if (INTERACTIVE)
6119 {
6120 /* The frame whose minibuffer we're going to display the message on.
6121 It may be larger than the selected frame, so we need
6122 to use its buffer, not the selected frame's buffer. */
6123 Lisp_Object mini_window;
6124 struct frame *f, *sf = SELECTED_FRAME ();
6125
6126 /* Get the frame containing the minibuffer
6127 that the selected frame is using. */
6128 mini_window = FRAME_MINIBUF_WINDOW (sf);
6129 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6130
6131 /* A null message buffer means that the frame hasn't really been
6132 initialized yet. Error messages get reported properly by
6133 cmd_error, so this must be just an informative message; toss it. */
6134 if (FRAME_MESSAGE_BUF (f))
6135 {
6136 Lisp_Object args[2], message;
6137 struct gcpro gcpro1, gcpro2;
6138
6139 args[0] = build_string (m);
6140 args[1] = message = string;
6141 GCPRO2 (args[0], message);
6142 gcpro1.nvars = 2;
6143
6144 message = Fformat (2, args);
6145
6146 if (log)
6147 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
6148 else
6149 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
6150
6151 UNGCPRO;
6152
6153 /* Print should start at the beginning of the message
6154 buffer next time. */
6155 message_buf_print = 0;
6156 }
6157 }
6158 }
6159
6160
6161 /* Dump an informative message to the minibuf. If M is 0, clear out
6162 any existing message, and let the mini-buffer text show through. */
6163
6164 /* VARARGS 1 */
6165 void
6166 message (m, a1, a2, a3)
6167 char *m;
6168 EMACS_INT a1, a2, a3;
6169 {
6170 if (noninteractive)
6171 {
6172 if (m)
6173 {
6174 if (noninteractive_need_newline)
6175 putc ('\n', stderr);
6176 noninteractive_need_newline = 0;
6177 fprintf (stderr, m, a1, a2, a3);
6178 if (cursor_in_echo_area == 0)
6179 fprintf (stderr, "\n");
6180 fflush (stderr);
6181 }
6182 }
6183 else if (INTERACTIVE)
6184 {
6185 /* The frame whose mini-buffer we're going to display the message
6186 on. It may be larger than the selected frame, so we need to
6187 use its buffer, not the selected frame's buffer. */
6188 Lisp_Object mini_window;
6189 struct frame *f, *sf = SELECTED_FRAME ();
6190
6191 /* Get the frame containing the mini-buffer
6192 that the selected frame is using. */
6193 mini_window = FRAME_MINIBUF_WINDOW (sf);
6194 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6195
6196 /* A null message buffer means that the frame hasn't really been
6197 initialized yet. Error messages get reported properly by
6198 cmd_error, so this must be just an informative message; toss
6199 it. */
6200 if (FRAME_MESSAGE_BUF (f))
6201 {
6202 if (m)
6203 {
6204 int len;
6205 #ifdef NO_ARG_ARRAY
6206 char *a[3];
6207 a[0] = (char *) a1;
6208 a[1] = (char *) a2;
6209 a[2] = (char *) a3;
6210
6211 len = doprnt (FRAME_MESSAGE_BUF (f),
6212 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6213 #else
6214 len = doprnt (FRAME_MESSAGE_BUF (f),
6215 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
6216 (char **) &a1);
6217 #endif /* NO_ARG_ARRAY */
6218
6219 message2 (FRAME_MESSAGE_BUF (f), len, 0);
6220 }
6221 else
6222 message1 (0);
6223
6224 /* Print should start at the beginning of the message
6225 buffer next time. */
6226 message_buf_print = 0;
6227 }
6228 }
6229 }
6230
6231
6232 /* The non-logging version of message. */
6233
6234 void
6235 message_nolog (m, a1, a2, a3)
6236 char *m;
6237 EMACS_INT a1, a2, a3;
6238 {
6239 Lisp_Object old_log_max;
6240 old_log_max = Vmessage_log_max;
6241 Vmessage_log_max = Qnil;
6242 message (m, a1, a2, a3);
6243 Vmessage_log_max = old_log_max;
6244 }
6245
6246
6247 /* Display the current message in the current mini-buffer. This is
6248 only called from error handlers in process.c, and is not time
6249 critical. */
6250
6251 void
6252 update_echo_area ()
6253 {
6254 if (!NILP (echo_area_buffer[0]))
6255 {
6256 Lisp_Object string;
6257 string = Fcurrent_message ();
6258 message3 (string, SBYTES (string),
6259 !NILP (current_buffer->enable_multibyte_characters));
6260 }
6261 }
6262
6263
6264 /* Make sure echo area buffers in `echo_buffers' are live.
6265 If they aren't, make new ones. */
6266
6267 static void
6268 ensure_echo_area_buffers ()
6269 {
6270 int i;
6271
6272 for (i = 0; i < 2; ++i)
6273 if (!BUFFERP (echo_buffer[i])
6274 || NILP (XBUFFER (echo_buffer[i])->name))
6275 {
6276 char name[30];
6277 Lisp_Object old_buffer;
6278 int j;
6279
6280 old_buffer = echo_buffer[i];
6281 sprintf (name, " *Echo Area %d*", i);
6282 echo_buffer[i] = Fget_buffer_create (build_string (name));
6283 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
6284
6285 for (j = 0; j < 2; ++j)
6286 if (EQ (old_buffer, echo_area_buffer[j]))
6287 echo_area_buffer[j] = echo_buffer[i];
6288 }
6289 }
6290
6291
6292 /* Call FN with args A1..A4 with either the current or last displayed
6293 echo_area_buffer as current buffer.
6294
6295 WHICH zero means use the current message buffer
6296 echo_area_buffer[0]. If that is nil, choose a suitable buffer
6297 from echo_buffer[] and clear it.
6298
6299 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
6300 suitable buffer from echo_buffer[] and clear it.
6301
6302 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
6303 that the current message becomes the last displayed one, make
6304 choose a suitable buffer for echo_area_buffer[0], and clear it.
6305
6306 Value is what FN returns. */
6307
6308 static int
6309 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
6310 struct window *w;
6311 int which;
6312 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
6313 EMACS_INT a1;
6314 Lisp_Object a2;
6315 EMACS_INT a3, a4;
6316 {
6317 Lisp_Object buffer;
6318 int this_one, the_other, clear_buffer_p, rc;
6319 int count = SPECPDL_INDEX ();
6320
6321 /* If buffers aren't live, make new ones. */
6322 ensure_echo_area_buffers ();
6323
6324 clear_buffer_p = 0;
6325
6326 if (which == 0)
6327 this_one = 0, the_other = 1;
6328 else if (which > 0)
6329 this_one = 1, the_other = 0;
6330 else
6331 {
6332 this_one = 0, the_other = 1;
6333 clear_buffer_p = 1;
6334
6335 /* We need a fresh one in case the current echo buffer equals
6336 the one containing the last displayed echo area message. */
6337 if (!NILP (echo_area_buffer[this_one])
6338 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
6339 echo_area_buffer[this_one] = Qnil;
6340 }
6341
6342 /* Choose a suitable buffer from echo_buffer[] is we don't
6343 have one. */
6344 if (NILP (echo_area_buffer[this_one]))
6345 {
6346 echo_area_buffer[this_one]
6347 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
6348 ? echo_buffer[the_other]
6349 : echo_buffer[this_one]);
6350 clear_buffer_p = 1;
6351 }
6352
6353 buffer = echo_area_buffer[this_one];
6354
6355 /* Don't get confused by reusing the buffer used for echoing
6356 for a different purpose. */
6357 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
6358 cancel_echoing ();
6359
6360 record_unwind_protect (unwind_with_echo_area_buffer,
6361 with_echo_area_buffer_unwind_data (w));
6362
6363 /* Make the echo area buffer current. Note that for display
6364 purposes, it is not necessary that the displayed window's buffer
6365 == current_buffer, except for text property lookup. So, let's
6366 only set that buffer temporarily here without doing a full
6367 Fset_window_buffer. We must also change w->pointm, though,
6368 because otherwise an assertions in unshow_buffer fails, and Emacs
6369 aborts. */
6370 set_buffer_internal_1 (XBUFFER (buffer));
6371 if (w)
6372 {
6373 w->buffer = buffer;
6374 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
6375 }
6376
6377 current_buffer->undo_list = Qt;
6378 current_buffer->read_only = Qnil;
6379 specbind (Qinhibit_read_only, Qt);
6380 specbind (Qinhibit_modification_hooks, Qt);
6381
6382 if (clear_buffer_p && Z > BEG)
6383 del_range (BEG, Z);
6384
6385 xassert (BEGV >= BEG);
6386 xassert (ZV <= Z && ZV >= BEGV);
6387
6388 rc = fn (a1, a2, a3, a4);
6389
6390 xassert (BEGV >= BEG);
6391 xassert (ZV <= Z && ZV >= BEGV);
6392
6393 unbind_to (count, Qnil);
6394 return rc;
6395 }
6396
6397
6398 /* Save state that should be preserved around the call to the function
6399 FN called in with_echo_area_buffer. */
6400
6401 static Lisp_Object
6402 with_echo_area_buffer_unwind_data (w)
6403 struct window *w;
6404 {
6405 int i = 0;
6406 Lisp_Object vector;
6407
6408 /* Reduce consing by keeping one vector in
6409 Vwith_echo_area_save_vector. */
6410 vector = Vwith_echo_area_save_vector;
6411 Vwith_echo_area_save_vector = Qnil;
6412
6413 if (NILP (vector))
6414 vector = Fmake_vector (make_number (7), Qnil);
6415
6416 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
6417 AREF (vector, i) = Vdeactivate_mark, ++i;
6418 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
6419
6420 if (w)
6421 {
6422 XSETWINDOW (AREF (vector, i), w); ++i;
6423 AREF (vector, i) = w->buffer; ++i;
6424 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
6425 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
6426 }
6427 else
6428 {
6429 int end = i + 4;
6430 for (; i < end; ++i)
6431 AREF (vector, i) = Qnil;
6432 }
6433
6434 xassert (i == ASIZE (vector));
6435 return vector;
6436 }
6437
6438
6439 /* Restore global state from VECTOR which was created by
6440 with_echo_area_buffer_unwind_data. */
6441
6442 static Lisp_Object
6443 unwind_with_echo_area_buffer (vector)
6444 Lisp_Object vector;
6445 {
6446 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
6447 Vdeactivate_mark = AREF (vector, 1);
6448 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
6449
6450 if (WINDOWP (AREF (vector, 3)))
6451 {
6452 struct window *w;
6453 Lisp_Object buffer, charpos, bytepos;
6454
6455 w = XWINDOW (AREF (vector, 3));
6456 buffer = AREF (vector, 4);
6457 charpos = AREF (vector, 5);
6458 bytepos = AREF (vector, 6);
6459
6460 w->buffer = buffer;
6461 set_marker_both (w->pointm, buffer,
6462 XFASTINT (charpos), XFASTINT (bytepos));
6463 }
6464
6465 Vwith_echo_area_save_vector = vector;
6466 return Qnil;
6467 }
6468
6469
6470 /* Set up the echo area for use by print functions. MULTIBYTE_P
6471 non-zero means we will print multibyte. */
6472
6473 void
6474 setup_echo_area_for_printing (multibyte_p)
6475 int multibyte_p;
6476 {
6477 ensure_echo_area_buffers ();
6478
6479 if (!message_buf_print)
6480 {
6481 /* A message has been output since the last time we printed.
6482 Choose a fresh echo area buffer. */
6483 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6484 echo_area_buffer[0] = echo_buffer[1];
6485 else
6486 echo_area_buffer[0] = echo_buffer[0];
6487
6488 /* Switch to that buffer and clear it. */
6489 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6490 current_buffer->truncate_lines = Qnil;
6491
6492 if (Z > BEG)
6493 {
6494 int count = SPECPDL_INDEX ();
6495 specbind (Qinhibit_read_only, Qt);
6496 /* Note that undo recording is always disabled. */
6497 del_range (BEG, Z);
6498 unbind_to (count, Qnil);
6499 }
6500 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6501
6502 /* Set up the buffer for the multibyteness we need. */
6503 if (multibyte_p
6504 != !NILP (current_buffer->enable_multibyte_characters))
6505 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
6506
6507 /* Raise the frame containing the echo area. */
6508 if (minibuffer_auto_raise)
6509 {
6510 struct frame *sf = SELECTED_FRAME ();
6511 Lisp_Object mini_window;
6512 mini_window = FRAME_MINIBUF_WINDOW (sf);
6513 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6514 }
6515
6516 message_log_maybe_newline ();
6517 message_buf_print = 1;
6518 }
6519 else
6520 {
6521 if (NILP (echo_area_buffer[0]))
6522 {
6523 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6524 echo_area_buffer[0] = echo_buffer[1];
6525 else
6526 echo_area_buffer[0] = echo_buffer[0];
6527 }
6528
6529 if (current_buffer != XBUFFER (echo_area_buffer[0]))
6530 {
6531 /* Someone switched buffers between print requests. */
6532 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6533 current_buffer->truncate_lines = Qnil;
6534 }
6535 }
6536 }
6537
6538
6539 /* Display an echo area message in window W. Value is non-zero if W's
6540 height is changed. If display_last_displayed_message_p is
6541 non-zero, display the message that was last displayed, otherwise
6542 display the current message. */
6543
6544 static int
6545 display_echo_area (w)
6546 struct window *w;
6547 {
6548 int i, no_message_p, window_height_changed_p, count;
6549
6550 /* Temporarily disable garbage collections while displaying the echo
6551 area. This is done because a GC can print a message itself.
6552 That message would modify the echo area buffer's contents while a
6553 redisplay of the buffer is going on, and seriously confuse
6554 redisplay. */
6555 count = inhibit_garbage_collection ();
6556
6557 /* If there is no message, we must call display_echo_area_1
6558 nevertheless because it resizes the window. But we will have to
6559 reset the echo_area_buffer in question to nil at the end because
6560 with_echo_area_buffer will sets it to an empty buffer. */
6561 i = display_last_displayed_message_p ? 1 : 0;
6562 no_message_p = NILP (echo_area_buffer[i]);
6563
6564 window_height_changed_p
6565 = with_echo_area_buffer (w, display_last_displayed_message_p,
6566 display_echo_area_1,
6567 (EMACS_INT) w, Qnil, 0, 0);
6568
6569 if (no_message_p)
6570 echo_area_buffer[i] = Qnil;
6571
6572 unbind_to (count, Qnil);
6573 return window_height_changed_p;
6574 }
6575
6576
6577 /* Helper for display_echo_area. Display the current buffer which
6578 contains the current echo area message in window W, a mini-window,
6579 a pointer to which is passed in A1. A2..A4 are currently not used.
6580 Change the height of W so that all of the message is displayed.
6581 Value is non-zero if height of W was changed. */
6582
6583 static int
6584 display_echo_area_1 (a1, a2, a3, a4)
6585 EMACS_INT a1;
6586 Lisp_Object a2;
6587 EMACS_INT a3, a4;
6588 {
6589 struct window *w = (struct window *) a1;
6590 Lisp_Object window;
6591 struct text_pos start;
6592 int window_height_changed_p = 0;
6593
6594 /* Do this before displaying, so that we have a large enough glyph
6595 matrix for the display. */
6596 window_height_changed_p = resize_mini_window (w, 0);
6597
6598 /* Display. */
6599 clear_glyph_matrix (w->desired_matrix);
6600 XSETWINDOW (window, w);
6601 SET_TEXT_POS (start, BEG, BEG_BYTE);
6602 try_window (window, start);
6603
6604 return window_height_changed_p;
6605 }
6606
6607
6608 /* Resize the echo area window to exactly the size needed for the
6609 currently displayed message, if there is one. If a mini-buffer
6610 is active, don't shrink it. */
6611
6612 void
6613 resize_echo_area_exactly ()
6614 {
6615 if (BUFFERP (echo_area_buffer[0])
6616 && WINDOWP (echo_area_window))
6617 {
6618 struct window *w = XWINDOW (echo_area_window);
6619 int resized_p;
6620 Lisp_Object resize_exactly;
6621
6622 if (minibuf_level == 0)
6623 resize_exactly = Qt;
6624 else
6625 resize_exactly = Qnil;
6626
6627 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
6628 (EMACS_INT) w, resize_exactly, 0, 0);
6629 if (resized_p)
6630 {
6631 ++windows_or_buffers_changed;
6632 ++update_mode_lines;
6633 redisplay_internal (0);
6634 }
6635 }
6636 }
6637
6638
6639 /* Callback function for with_echo_area_buffer, when used from
6640 resize_echo_area_exactly. A1 contains a pointer to the window to
6641 resize, EXACTLY non-nil means resize the mini-window exactly to the
6642 size of the text displayed. A3 and A4 are not used. Value is what
6643 resize_mini_window returns. */
6644
6645 static int
6646 resize_mini_window_1 (a1, exactly, a3, a4)
6647 EMACS_INT a1;
6648 Lisp_Object exactly;
6649 EMACS_INT a3, a4;
6650 {
6651 return resize_mini_window ((struct window *) a1, !NILP (exactly));
6652 }
6653
6654
6655 /* Resize mini-window W to fit the size of its contents. EXACT:P
6656 means size the window exactly to the size needed. Otherwise, it's
6657 only enlarged until W's buffer is empty. Value is non-zero if
6658 the window height has been changed. */
6659
6660 int
6661 resize_mini_window (w, exact_p)
6662 struct window *w;
6663 int exact_p;
6664 {
6665 struct frame *f = XFRAME (w->frame);
6666 int window_height_changed_p = 0;
6667
6668 xassert (MINI_WINDOW_P (w));
6669
6670 /* Don't resize windows while redisplaying a window; it would
6671 confuse redisplay functions when the size of the window they are
6672 displaying changes from under them. Such a resizing can happen,
6673 for instance, when which-func prints a long message while
6674 we are running fontification-functions. We're running these
6675 functions with safe_call which binds inhibit-redisplay to t. */
6676 if (!NILP (Vinhibit_redisplay))
6677 return 0;
6678
6679 /* Nil means don't try to resize. */
6680 if (NILP (Vresize_mini_windows)
6681 || (FRAME_X_P (f) && f->output_data.x == NULL))
6682 return 0;
6683
6684 if (!FRAME_MINIBUF_ONLY_P (f))
6685 {
6686 struct it it;
6687 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
6688 int total_height = XFASTINT (root->height) + XFASTINT (w->height);
6689 int height, max_height;
6690 int unit = CANON_Y_UNIT (f);
6691 struct text_pos start;
6692 struct buffer *old_current_buffer = NULL;
6693
6694 if (current_buffer != XBUFFER (w->buffer))
6695 {
6696 old_current_buffer = current_buffer;
6697 set_buffer_internal (XBUFFER (w->buffer));
6698 }
6699
6700 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
6701
6702 /* Compute the max. number of lines specified by the user. */
6703 if (FLOATP (Vmax_mini_window_height))
6704 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_HEIGHT (f);
6705 else if (INTEGERP (Vmax_mini_window_height))
6706 max_height = XINT (Vmax_mini_window_height);
6707 else
6708 max_height = total_height / 4;
6709
6710 /* Correct that max. height if it's bogus. */
6711 max_height = max (1, max_height);
6712 max_height = min (total_height, max_height);
6713
6714 /* Find out the height of the text in the window. */
6715 if (it.truncate_lines_p)
6716 height = 1;
6717 else
6718 {
6719 last_height = 0;
6720 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
6721 if (it.max_ascent == 0 && it.max_descent == 0)
6722 height = it.current_y + last_height;
6723 else
6724 height = it.current_y + it.max_ascent + it.max_descent;
6725 height -= it.extra_line_spacing;
6726 height = (height + unit - 1) / unit;
6727 }
6728
6729 /* Compute a suitable window start. */
6730 if (height > max_height)
6731 {
6732 height = max_height;
6733 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
6734 move_it_vertically_backward (&it, (height - 1) * unit);
6735 start = it.current.pos;
6736 }
6737 else
6738 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
6739 SET_MARKER_FROM_TEXT_POS (w->start, start);
6740
6741 if (EQ (Vresize_mini_windows, Qgrow_only))
6742 {
6743 /* Let it grow only, until we display an empty message, in which
6744 case the window shrinks again. */
6745 if (height > XFASTINT (w->height))
6746 {
6747 int old_height = XFASTINT (w->height);
6748 freeze_window_starts (f, 1);
6749 grow_mini_window (w, height - XFASTINT (w->height));
6750 window_height_changed_p = XFASTINT (w->height) != old_height;
6751 }
6752 else if (height < XFASTINT (w->height)
6753 && (exact_p || BEGV == ZV))
6754 {
6755 int old_height = XFASTINT (w->height);
6756 freeze_window_starts (f, 0);
6757 shrink_mini_window (w);
6758 window_height_changed_p = XFASTINT (w->height) != old_height;
6759 }
6760 }
6761 else
6762 {
6763 /* Always resize to exact size needed. */
6764 if (height > XFASTINT (w->height))
6765 {
6766 int old_height = XFASTINT (w->height);
6767 freeze_window_starts (f, 1);
6768 grow_mini_window (w, height - XFASTINT (w->height));
6769 window_height_changed_p = XFASTINT (w->height) != old_height;
6770 }
6771 else if (height < XFASTINT (w->height))
6772 {
6773 int old_height = XFASTINT (w->height);
6774 freeze_window_starts (f, 0);
6775 shrink_mini_window (w);
6776
6777 if (height)
6778 {
6779 freeze_window_starts (f, 1);
6780 grow_mini_window (w, height - XFASTINT (w->height));
6781 }
6782
6783 window_height_changed_p = XFASTINT (w->height) != old_height;
6784 }
6785 }
6786
6787 if (old_current_buffer)
6788 set_buffer_internal (old_current_buffer);
6789 }
6790
6791 return window_height_changed_p;
6792 }
6793
6794
6795 /* Value is the current message, a string, or nil if there is no
6796 current message. */
6797
6798 Lisp_Object
6799 current_message ()
6800 {
6801 Lisp_Object msg;
6802
6803 if (NILP (echo_area_buffer[0]))
6804 msg = Qnil;
6805 else
6806 {
6807 with_echo_area_buffer (0, 0, current_message_1,
6808 (EMACS_INT) &msg, Qnil, 0, 0);
6809 if (NILP (msg))
6810 echo_area_buffer[0] = Qnil;
6811 }
6812
6813 return msg;
6814 }
6815
6816
6817 static int
6818 current_message_1 (a1, a2, a3, a4)
6819 EMACS_INT a1;
6820 Lisp_Object a2;
6821 EMACS_INT a3, a4;
6822 {
6823 Lisp_Object *msg = (Lisp_Object *) a1;
6824
6825 if (Z > BEG)
6826 *msg = make_buffer_string (BEG, Z, 1);
6827 else
6828 *msg = Qnil;
6829 return 0;
6830 }
6831
6832
6833 /* Push the current message on Vmessage_stack for later restauration
6834 by restore_message. Value is non-zero if the current message isn't
6835 empty. This is a relatively infrequent operation, so it's not
6836 worth optimizing. */
6837
6838 int
6839 push_message ()
6840 {
6841 Lisp_Object msg;
6842 msg = current_message ();
6843 Vmessage_stack = Fcons (msg, Vmessage_stack);
6844 return STRINGP (msg);
6845 }
6846
6847
6848 /* Restore message display from the top of Vmessage_stack. */
6849
6850 void
6851 restore_message ()
6852 {
6853 Lisp_Object msg;
6854
6855 xassert (CONSP (Vmessage_stack));
6856 msg = XCAR (Vmessage_stack);
6857 if (STRINGP (msg))
6858 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
6859 else
6860 message3_nolog (msg, 0, 0);
6861 }
6862
6863
6864 /* Handler for record_unwind_protect calling pop_message. */
6865
6866 Lisp_Object
6867 pop_message_unwind (dummy)
6868 Lisp_Object dummy;
6869 {
6870 pop_message ();
6871 return Qnil;
6872 }
6873
6874 /* Pop the top-most entry off Vmessage_stack. */
6875
6876 void
6877 pop_message ()
6878 {
6879 xassert (CONSP (Vmessage_stack));
6880 Vmessage_stack = XCDR (Vmessage_stack);
6881 }
6882
6883
6884 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
6885 exits. If the stack is not empty, we have a missing pop_message
6886 somewhere. */
6887
6888 void
6889 check_message_stack ()
6890 {
6891 if (!NILP (Vmessage_stack))
6892 abort ();
6893 }
6894
6895
6896 /* Truncate to NCHARS what will be displayed in the echo area the next
6897 time we display it---but don't redisplay it now. */
6898
6899 void
6900 truncate_echo_area (nchars)
6901 int nchars;
6902 {
6903 if (nchars == 0)
6904 echo_area_buffer[0] = Qnil;
6905 /* A null message buffer means that the frame hasn't really been
6906 initialized yet. Error messages get reported properly by
6907 cmd_error, so this must be just an informative message; toss it. */
6908 else if (!noninteractive
6909 && INTERACTIVE
6910 && !NILP (echo_area_buffer[0]))
6911 {
6912 struct frame *sf = SELECTED_FRAME ();
6913 if (FRAME_MESSAGE_BUF (sf))
6914 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
6915 }
6916 }
6917
6918
6919 /* Helper function for truncate_echo_area. Truncate the current
6920 message to at most NCHARS characters. */
6921
6922 static int
6923 truncate_message_1 (nchars, a2, a3, a4)
6924 EMACS_INT nchars;
6925 Lisp_Object a2;
6926 EMACS_INT a3, a4;
6927 {
6928 if (BEG + nchars < Z)
6929 del_range (BEG + nchars, Z);
6930 if (Z == BEG)
6931 echo_area_buffer[0] = Qnil;
6932 return 0;
6933 }
6934
6935
6936 /* Set the current message to a substring of S or STRING.
6937
6938 If STRING is a Lisp string, set the message to the first NBYTES
6939 bytes from STRING. NBYTES zero means use the whole string. If
6940 STRING is multibyte, the message will be displayed multibyte.
6941
6942 If S is not null, set the message to the first LEN bytes of S. LEN
6943 zero means use the whole string. MULTIBYTE_P non-zero means S is
6944 multibyte. Display the message multibyte in that case. */
6945
6946 void
6947 set_message (s, string, nbytes, multibyte_p)
6948 const char *s;
6949 Lisp_Object string;
6950 int nbytes, multibyte_p;
6951 {
6952 message_enable_multibyte
6953 = ((s && multibyte_p)
6954 || (STRINGP (string) && STRING_MULTIBYTE (string)));
6955
6956 with_echo_area_buffer (0, -1, set_message_1,
6957 (EMACS_INT) s, string, nbytes, multibyte_p);
6958 message_buf_print = 0;
6959 help_echo_showing_p = 0;
6960 }
6961
6962
6963 /* Helper function for set_message. Arguments have the same meaning
6964 as there, with A1 corresponding to S and A2 corresponding to STRING
6965 This function is called with the echo area buffer being
6966 current. */
6967
6968 static int
6969 set_message_1 (a1, a2, nbytes, multibyte_p)
6970 EMACS_INT a1;
6971 Lisp_Object a2;
6972 EMACS_INT nbytes, multibyte_p;
6973 {
6974 const char *s = (const char *) a1;
6975 Lisp_Object string = a2;
6976
6977 xassert (BEG == Z);
6978
6979 /* Change multibyteness of the echo buffer appropriately. */
6980 if (message_enable_multibyte
6981 != !NILP (current_buffer->enable_multibyte_characters))
6982 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
6983
6984 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
6985
6986 /* Insert new message at BEG. */
6987 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6988
6989 if (STRINGP (string))
6990 {
6991 int nchars;
6992
6993 if (nbytes == 0)
6994 nbytes = SBYTES (string);
6995 nchars = string_byte_to_char (string, nbytes);
6996
6997 /* This function takes care of single/multibyte conversion. We
6998 just have to ensure that the echo area buffer has the right
6999 setting of enable_multibyte_characters. */
7000 insert_from_string (string, 0, 0, nchars, nbytes, 1);
7001 }
7002 else if (s)
7003 {
7004 if (nbytes == 0)
7005 nbytes = strlen (s);
7006
7007 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
7008 {
7009 /* Convert from multi-byte to single-byte. */
7010 int i, c, n;
7011 unsigned char work[1];
7012
7013 /* Convert a multibyte string to single-byte. */
7014 for (i = 0; i < nbytes; i += n)
7015 {
7016 c = string_char_and_length (s + i, nbytes - i, &n);
7017 work[0] = (SINGLE_BYTE_CHAR_P (c)
7018 ? c
7019 : multibyte_char_to_unibyte (c, Qnil));
7020 insert_1_both (work, 1, 1, 1, 0, 0);
7021 }
7022 }
7023 else if (!multibyte_p
7024 && !NILP (current_buffer->enable_multibyte_characters))
7025 {
7026 /* Convert from single-byte to multi-byte. */
7027 int i, c, n;
7028 const unsigned char *msg = (const unsigned char *) s;
7029 unsigned char str[MAX_MULTIBYTE_LENGTH];
7030
7031 /* Convert a single-byte string to multibyte. */
7032 for (i = 0; i < nbytes; i++)
7033 {
7034 c = unibyte_char_to_multibyte (msg[i]);
7035 n = CHAR_STRING (c, str);
7036 insert_1_both (str, 1, n, 1, 0, 0);
7037 }
7038 }
7039 else
7040 insert_1 (s, nbytes, 1, 0, 0);
7041 }
7042
7043 return 0;
7044 }
7045
7046
7047 /* Clear messages. CURRENT_P non-zero means clear the current
7048 message. LAST_DISPLAYED_P non-zero means clear the message
7049 last displayed. */
7050
7051 void
7052 clear_message (current_p, last_displayed_p)
7053 int current_p, last_displayed_p;
7054 {
7055 if (current_p)
7056 {
7057 echo_area_buffer[0] = Qnil;
7058 message_cleared_p = 1;
7059 }
7060
7061 if (last_displayed_p)
7062 echo_area_buffer[1] = Qnil;
7063
7064 message_buf_print = 0;
7065 }
7066
7067 /* Clear garbaged frames.
7068
7069 This function is used where the old redisplay called
7070 redraw_garbaged_frames which in turn called redraw_frame which in
7071 turn called clear_frame. The call to clear_frame was a source of
7072 flickering. I believe a clear_frame is not necessary. It should
7073 suffice in the new redisplay to invalidate all current matrices,
7074 and ensure a complete redisplay of all windows. */
7075
7076 static void
7077 clear_garbaged_frames ()
7078 {
7079 if (frame_garbaged)
7080 {
7081 Lisp_Object tail, frame;
7082 int changed_count = 0;
7083
7084 FOR_EACH_FRAME (tail, frame)
7085 {
7086 struct frame *f = XFRAME (frame);
7087
7088 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
7089 {
7090 if (f->resized_p)
7091 Fredraw_frame (frame);
7092 clear_current_matrices (f);
7093 changed_count++;
7094 f->garbaged = 0;
7095 f->resized_p = 0;
7096 }
7097 }
7098
7099 frame_garbaged = 0;
7100 if (changed_count)
7101 ++windows_or_buffers_changed;
7102 }
7103 }
7104
7105
7106 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
7107 is non-zero update selected_frame. Value is non-zero if the
7108 mini-windows height has been changed. */
7109
7110 static int
7111 echo_area_display (update_frame_p)
7112 int update_frame_p;
7113 {
7114 Lisp_Object mini_window;
7115 struct window *w;
7116 struct frame *f;
7117 int window_height_changed_p = 0;
7118 struct frame *sf = SELECTED_FRAME ();
7119
7120 mini_window = FRAME_MINIBUF_WINDOW (sf);
7121 w = XWINDOW (mini_window);
7122 f = XFRAME (WINDOW_FRAME (w));
7123
7124 /* Don't display if frame is invisible or not yet initialized. */
7125 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
7126 return 0;
7127
7128 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
7129 #ifndef MAC_OS8
7130 #ifdef HAVE_WINDOW_SYSTEM
7131 /* When Emacs starts, selected_frame may be a visible terminal
7132 frame, even if we run under a window system. If we let this
7133 through, a message would be displayed on the terminal. */
7134 if (EQ (selected_frame, Vterminal_frame)
7135 && !NILP (Vwindow_system))
7136 return 0;
7137 #endif /* HAVE_WINDOW_SYSTEM */
7138 #endif
7139
7140 /* Redraw garbaged frames. */
7141 if (frame_garbaged)
7142 clear_garbaged_frames ();
7143
7144 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
7145 {
7146 echo_area_window = mini_window;
7147 window_height_changed_p = display_echo_area (w);
7148 w->must_be_updated_p = 1;
7149
7150 /* Update the display, unless called from redisplay_internal.
7151 Also don't update the screen during redisplay itself. The
7152 update will happen at the end of redisplay, and an update
7153 here could cause confusion. */
7154 if (update_frame_p && !redisplaying_p)
7155 {
7156 int n = 0;
7157
7158 /* If the display update has been interrupted by pending
7159 input, update mode lines in the frame. Due to the
7160 pending input, it might have been that redisplay hasn't
7161 been called, so that mode lines above the echo area are
7162 garbaged. This looks odd, so we prevent it here. */
7163 if (!display_completed)
7164 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
7165
7166 if (window_height_changed_p
7167 /* Don't do this if Emacs is shutting down. Redisplay
7168 needs to run hooks. */
7169 && !NILP (Vrun_hooks))
7170 {
7171 /* Must update other windows. Likewise as in other
7172 cases, don't let this update be interrupted by
7173 pending input. */
7174 int count = SPECPDL_INDEX ();
7175 specbind (Qredisplay_dont_pause, Qt);
7176 windows_or_buffers_changed = 1;
7177 redisplay_internal (0);
7178 unbind_to (count, Qnil);
7179 }
7180 else if (FRAME_WINDOW_P (f) && n == 0)
7181 {
7182 /* Window configuration is the same as before.
7183 Can do with a display update of the echo area,
7184 unless we displayed some mode lines. */
7185 update_single_window (w, 1);
7186 rif->flush_display (f);
7187 }
7188 else
7189 update_frame (f, 1, 1);
7190
7191 /* If cursor is in the echo area, make sure that the next
7192 redisplay displays the minibuffer, so that the cursor will
7193 be replaced with what the minibuffer wants. */
7194 if (cursor_in_echo_area)
7195 ++windows_or_buffers_changed;
7196 }
7197 }
7198 else if (!EQ (mini_window, selected_window))
7199 windows_or_buffers_changed++;
7200
7201 /* Last displayed message is now the current message. */
7202 echo_area_buffer[1] = echo_area_buffer[0];
7203
7204 /* Prevent redisplay optimization in redisplay_internal by resetting
7205 this_line_start_pos. This is done because the mini-buffer now
7206 displays the message instead of its buffer text. */
7207 if (EQ (mini_window, selected_window))
7208 CHARPOS (this_line_start_pos) = 0;
7209
7210 return window_height_changed_p;
7211 }
7212
7213
7214 \f
7215 /***********************************************************************
7216 Frame Titles
7217 ***********************************************************************/
7218
7219
7220 /* The frame title buffering code is also used by Fformat_mode_line.
7221 So it is not conditioned by HAVE_WINDOW_SYSTEM. */
7222
7223 /* A buffer for constructing frame titles in it; allocated from the
7224 heap in init_xdisp and resized as needed in store_frame_title_char. */
7225
7226 static char *frame_title_buf;
7227
7228 /* The buffer's end, and a current output position in it. */
7229
7230 static char *frame_title_buf_end;
7231 static char *frame_title_ptr;
7232
7233
7234 /* Store a single character C for the frame title in frame_title_buf.
7235 Re-allocate frame_title_buf if necessary. */
7236
7237 static void
7238 #ifdef PROTOTYPES
7239 store_frame_title_char (char c)
7240 #else
7241 store_frame_title_char (c)
7242 char c;
7243 #endif
7244 {
7245 /* If output position has reached the end of the allocated buffer,
7246 double the buffer's size. */
7247 if (frame_title_ptr == frame_title_buf_end)
7248 {
7249 int len = frame_title_ptr - frame_title_buf;
7250 int new_size = 2 * len * sizeof *frame_title_buf;
7251 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
7252 frame_title_buf_end = frame_title_buf + new_size;
7253 frame_title_ptr = frame_title_buf + len;
7254 }
7255
7256 *frame_title_ptr++ = c;
7257 }
7258
7259
7260 /* Store part of a frame title in frame_title_buf, beginning at
7261 frame_title_ptr. STR is the string to store. Do not copy
7262 characters that yield more columns than PRECISION; PRECISION <= 0
7263 means copy the whole string. Pad with spaces until FIELD_WIDTH
7264 number of characters have been copied; FIELD_WIDTH <= 0 means don't
7265 pad. Called from display_mode_element when it is used to build a
7266 frame title. */
7267
7268 static int
7269 store_frame_title (str, field_width, precision)
7270 const unsigned char *str;
7271 int field_width, precision;
7272 {
7273 int n = 0;
7274 int dummy, nbytes;
7275
7276 /* Copy at most PRECISION chars from STR. */
7277 nbytes = strlen (str);
7278 n+= c_string_width (str, nbytes, precision, &dummy, &nbytes);
7279 while (nbytes--)
7280 store_frame_title_char (*str++);
7281
7282 /* Fill up with spaces until FIELD_WIDTH reached. */
7283 while (field_width > 0
7284 && n < field_width)
7285 {
7286 store_frame_title_char (' ');
7287 ++n;
7288 }
7289
7290 return n;
7291 }
7292
7293 #ifdef HAVE_WINDOW_SYSTEM
7294
7295 /* Set the title of FRAME, if it has changed. The title format is
7296 Vicon_title_format if FRAME is iconified, otherwise it is
7297 frame_title_format. */
7298
7299 static void
7300 x_consider_frame_title (frame)
7301 Lisp_Object frame;
7302 {
7303 struct frame *f = XFRAME (frame);
7304
7305 if (FRAME_WINDOW_P (f)
7306 || FRAME_MINIBUF_ONLY_P (f)
7307 || f->explicit_name)
7308 {
7309 /* Do we have more than one visible frame on this X display? */
7310 Lisp_Object tail;
7311 Lisp_Object fmt;
7312 struct buffer *obuf;
7313 int len;
7314 struct it it;
7315
7316 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
7317 {
7318 Lisp_Object other_frame = XCAR (tail);
7319 struct frame *tf = XFRAME (other_frame);
7320
7321 if (tf != f
7322 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
7323 && !FRAME_MINIBUF_ONLY_P (tf)
7324 && !EQ (other_frame, tip_frame)
7325 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
7326 break;
7327 }
7328
7329 /* Set global variable indicating that multiple frames exist. */
7330 multiple_frames = CONSP (tail);
7331
7332 /* Switch to the buffer of selected window of the frame. Set up
7333 frame_title_ptr so that display_mode_element will output into it;
7334 then display the title. */
7335 obuf = current_buffer;
7336 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
7337 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
7338 frame_title_ptr = frame_title_buf;
7339 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
7340 NULL, DEFAULT_FACE_ID);
7341 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
7342 len = frame_title_ptr - frame_title_buf;
7343 frame_title_ptr = NULL;
7344 set_buffer_internal_1 (obuf);
7345
7346 /* Set the title only if it's changed. This avoids consing in
7347 the common case where it hasn't. (If it turns out that we've
7348 already wasted too much time by walking through the list with
7349 display_mode_element, then we might need to optimize at a
7350 higher level than this.) */
7351 if (! STRINGP (f->name)
7352 || SBYTES (f->name) != len
7353 || bcmp (frame_title_buf, SDATA (f->name), len) != 0)
7354 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
7355 }
7356 }
7357
7358 #endif /* not HAVE_WINDOW_SYSTEM */
7359
7360
7361
7362 \f
7363 /***********************************************************************
7364 Menu Bars
7365 ***********************************************************************/
7366
7367
7368 /* Prepare for redisplay by updating menu-bar item lists when
7369 appropriate. This can call eval. */
7370
7371 void
7372 prepare_menu_bars ()
7373 {
7374 int all_windows;
7375 struct gcpro gcpro1, gcpro2;
7376 struct frame *f;
7377 Lisp_Object tooltip_frame;
7378
7379 #ifdef HAVE_WINDOW_SYSTEM
7380 tooltip_frame = tip_frame;
7381 #else
7382 tooltip_frame = Qnil;
7383 #endif
7384
7385 /* Update all frame titles based on their buffer names, etc. We do
7386 this before the menu bars so that the buffer-menu will show the
7387 up-to-date frame titles. */
7388 #ifdef HAVE_WINDOW_SYSTEM
7389 if (windows_or_buffers_changed || update_mode_lines)
7390 {
7391 Lisp_Object tail, frame;
7392
7393 FOR_EACH_FRAME (tail, frame)
7394 {
7395 f = XFRAME (frame);
7396 if (!EQ (frame, tooltip_frame)
7397 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
7398 x_consider_frame_title (frame);
7399 }
7400 }
7401 #endif /* HAVE_WINDOW_SYSTEM */
7402
7403 /* Update the menu bar item lists, if appropriate. This has to be
7404 done before any actual redisplay or generation of display lines. */
7405 all_windows = (update_mode_lines
7406 || buffer_shared > 1
7407 || windows_or_buffers_changed);
7408 if (all_windows)
7409 {
7410 Lisp_Object tail, frame;
7411 int count = SPECPDL_INDEX ();
7412
7413 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7414
7415 FOR_EACH_FRAME (tail, frame)
7416 {
7417 f = XFRAME (frame);
7418
7419 /* Ignore tooltip frame. */
7420 if (EQ (frame, tooltip_frame))
7421 continue;
7422
7423 /* If a window on this frame changed size, report that to
7424 the user and clear the size-change flag. */
7425 if (FRAME_WINDOW_SIZES_CHANGED (f))
7426 {
7427 Lisp_Object functions;
7428
7429 /* Clear flag first in case we get an error below. */
7430 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
7431 functions = Vwindow_size_change_functions;
7432 GCPRO2 (tail, functions);
7433
7434 while (CONSP (functions))
7435 {
7436 call1 (XCAR (functions), frame);
7437 functions = XCDR (functions);
7438 }
7439 UNGCPRO;
7440 }
7441
7442 GCPRO1 (tail);
7443 update_menu_bar (f, 0);
7444 #ifdef HAVE_WINDOW_SYSTEM
7445 update_tool_bar (f, 0);
7446 #endif
7447 UNGCPRO;
7448 }
7449
7450 unbind_to (count, Qnil);
7451 }
7452 else
7453 {
7454 struct frame *sf = SELECTED_FRAME ();
7455 update_menu_bar (sf, 1);
7456 #ifdef HAVE_WINDOW_SYSTEM
7457 update_tool_bar (sf, 1);
7458 #endif
7459 }
7460
7461 /* Motif needs this. See comment in xmenu.c. Turn it off when
7462 pending_menu_activation is not defined. */
7463 #ifdef USE_X_TOOLKIT
7464 pending_menu_activation = 0;
7465 #endif
7466 }
7467
7468
7469 /* Update the menu bar item list for frame F. This has to be done
7470 before we start to fill in any display lines, because it can call
7471 eval.
7472
7473 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
7474
7475 static void
7476 update_menu_bar (f, save_match_data)
7477 struct frame *f;
7478 int save_match_data;
7479 {
7480 Lisp_Object window;
7481 register struct window *w;
7482
7483 /* If called recursively during a menu update, do nothing. This can
7484 happen when, for instance, an activate-menubar-hook causes a
7485 redisplay. */
7486 if (inhibit_menubar_update)
7487 return;
7488
7489 window = FRAME_SELECTED_WINDOW (f);
7490 w = XWINDOW (window);
7491
7492 #if 0 /* The if statement below this if statement used to include the
7493 condition !NILP (w->update_mode_line), rather than using
7494 update_mode_lines directly, and this if statement may have
7495 been added to make that condition work. Now the if
7496 statement below matches its comment, this isn't needed. */
7497 if (update_mode_lines)
7498 w->update_mode_line = Qt;
7499 #endif
7500
7501 if (FRAME_WINDOW_P (f)
7502 ?
7503 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS)
7504 FRAME_EXTERNAL_MENU_BAR (f)
7505 #else
7506 FRAME_MENU_BAR_LINES (f) > 0
7507 #endif
7508 : FRAME_MENU_BAR_LINES (f) > 0)
7509 {
7510 /* If the user has switched buffers or windows, we need to
7511 recompute to reflect the new bindings. But we'll
7512 recompute when update_mode_lines is set too; that means
7513 that people can use force-mode-line-update to request
7514 that the menu bar be recomputed. The adverse effect on
7515 the rest of the redisplay algorithm is about the same as
7516 windows_or_buffers_changed anyway. */
7517 if (windows_or_buffers_changed
7518 /* This used to test w->update_mode_line, but we believe
7519 there is no need to recompute the menu in that case. */
7520 || update_mode_lines
7521 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7522 < BUF_MODIFF (XBUFFER (w->buffer)))
7523 != !NILP (w->last_had_star))
7524 || ((!NILP (Vtransient_mark_mode)
7525 && !NILP (XBUFFER (w->buffer)->mark_active))
7526 != !NILP (w->region_showing)))
7527 {
7528 struct buffer *prev = current_buffer;
7529 int count = SPECPDL_INDEX ();
7530
7531 specbind (Qinhibit_menubar_update, Qt);
7532
7533 set_buffer_internal_1 (XBUFFER (w->buffer));
7534 if (save_match_data)
7535 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7536 if (NILP (Voverriding_local_map_menu_flag))
7537 {
7538 specbind (Qoverriding_terminal_local_map, Qnil);
7539 specbind (Qoverriding_local_map, Qnil);
7540 }
7541
7542 /* Run the Lucid hook. */
7543 safe_run_hooks (Qactivate_menubar_hook);
7544
7545 /* If it has changed current-menubar from previous value,
7546 really recompute the menu-bar from the value. */
7547 if (! NILP (Vlucid_menu_bar_dirty_flag))
7548 call0 (Qrecompute_lucid_menubar);
7549
7550 safe_run_hooks (Qmenu_bar_update_hook);
7551 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
7552
7553 /* Redisplay the menu bar in case we changed it. */
7554 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS)
7555 if (FRAME_WINDOW_P (f)
7556 #if defined (MAC_OS)
7557 /* All frames on Mac OS share the same menubar. So only the
7558 selected frame should be allowed to set it. */
7559 && f == SELECTED_FRAME ()
7560 #endif
7561 )
7562 set_frame_menubar (f, 0, 0);
7563 else
7564 /* On a terminal screen, the menu bar is an ordinary screen
7565 line, and this makes it get updated. */
7566 w->update_mode_line = Qt;
7567 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7568 /* In the non-toolkit version, the menu bar is an ordinary screen
7569 line, and this makes it get updated. */
7570 w->update_mode_line = Qt;
7571 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7572
7573 unbind_to (count, Qnil);
7574 set_buffer_internal_1 (prev);
7575 }
7576 }
7577 }
7578
7579
7580 \f
7581 /***********************************************************************
7582 Tool-bars
7583 ***********************************************************************/
7584
7585 #ifdef HAVE_WINDOW_SYSTEM
7586
7587 /* Update the tool-bar item list for frame F. This has to be done
7588 before we start to fill in any display lines. Called from
7589 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
7590 and restore it here. */
7591
7592 static void
7593 update_tool_bar (f, save_match_data)
7594 struct frame *f;
7595 int save_match_data;
7596 {
7597 if (WINDOWP (f->tool_bar_window)
7598 && XFASTINT (XWINDOW (f->tool_bar_window)->height) > 0)
7599 {
7600 Lisp_Object window;
7601 struct window *w;
7602
7603 window = FRAME_SELECTED_WINDOW (f);
7604 w = XWINDOW (window);
7605
7606 /* If the user has switched buffers or windows, we need to
7607 recompute to reflect the new bindings. But we'll
7608 recompute when update_mode_lines is set too; that means
7609 that people can use force-mode-line-update to request
7610 that the menu bar be recomputed. The adverse effect on
7611 the rest of the redisplay algorithm is about the same as
7612 windows_or_buffers_changed anyway. */
7613 if (windows_or_buffers_changed
7614 || !NILP (w->update_mode_line)
7615 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7616 < BUF_MODIFF (XBUFFER (w->buffer)))
7617 != !NILP (w->last_had_star))
7618 || ((!NILP (Vtransient_mark_mode)
7619 && !NILP (XBUFFER (w->buffer)->mark_active))
7620 != !NILP (w->region_showing)))
7621 {
7622 struct buffer *prev = current_buffer;
7623 int count = SPECPDL_INDEX ();
7624
7625 /* Set current_buffer to the buffer of the selected
7626 window of the frame, so that we get the right local
7627 keymaps. */
7628 set_buffer_internal_1 (XBUFFER (w->buffer));
7629
7630 /* Save match data, if we must. */
7631 if (save_match_data)
7632 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7633
7634 /* Make sure that we don't accidentally use bogus keymaps. */
7635 if (NILP (Voverriding_local_map_menu_flag))
7636 {
7637 specbind (Qoverriding_terminal_local_map, Qnil);
7638 specbind (Qoverriding_local_map, Qnil);
7639 }
7640
7641 /* Build desired tool-bar items from keymaps. */
7642 f->tool_bar_items
7643 = tool_bar_items (f->tool_bar_items, &f->n_tool_bar_items);
7644
7645 /* Redisplay the tool-bar in case we changed it. */
7646 w->update_mode_line = Qt;
7647
7648 unbind_to (count, Qnil);
7649 set_buffer_internal_1 (prev);
7650 }
7651 }
7652 }
7653
7654
7655 /* Set F->desired_tool_bar_string to a Lisp string representing frame
7656 F's desired tool-bar contents. F->tool_bar_items must have
7657 been set up previously by calling prepare_menu_bars. */
7658
7659 static void
7660 build_desired_tool_bar_string (f)
7661 struct frame *f;
7662 {
7663 int i, size, size_needed;
7664 struct gcpro gcpro1, gcpro2, gcpro3;
7665 Lisp_Object image, plist, props;
7666
7667 image = plist = props = Qnil;
7668 GCPRO3 (image, plist, props);
7669
7670 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
7671 Otherwise, make a new string. */
7672
7673 /* The size of the string we might be able to reuse. */
7674 size = (STRINGP (f->desired_tool_bar_string)
7675 ? SCHARS (f->desired_tool_bar_string)
7676 : 0);
7677
7678 /* We need one space in the string for each image. */
7679 size_needed = f->n_tool_bar_items;
7680
7681 /* Reuse f->desired_tool_bar_string, if possible. */
7682 if (size < size_needed || NILP (f->desired_tool_bar_string))
7683 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
7684 make_number (' '));
7685 else
7686 {
7687 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
7688 Fremove_text_properties (make_number (0), make_number (size),
7689 props, f->desired_tool_bar_string);
7690 }
7691
7692 /* Put a `display' property on the string for the images to display,
7693 put a `menu_item' property on tool-bar items with a value that
7694 is the index of the item in F's tool-bar item vector. */
7695 for (i = 0; i < f->n_tool_bar_items; ++i)
7696 {
7697 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
7698
7699 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
7700 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
7701 int hmargin, vmargin, relief, idx, end;
7702 extern Lisp_Object QCrelief, QCmargin, QCconversion, Qimage;
7703
7704 /* If image is a vector, choose the image according to the
7705 button state. */
7706 image = PROP (TOOL_BAR_ITEM_IMAGES);
7707 if (VECTORP (image))
7708 {
7709 if (enabled_p)
7710 idx = (selected_p
7711 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
7712 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
7713 else
7714 idx = (selected_p
7715 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
7716 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
7717
7718 xassert (ASIZE (image) >= idx);
7719 image = AREF (image, idx);
7720 }
7721 else
7722 idx = -1;
7723
7724 /* Ignore invalid image specifications. */
7725 if (!valid_image_p (image))
7726 continue;
7727
7728 /* Display the tool-bar button pressed, or depressed. */
7729 plist = Fcopy_sequence (XCDR (image));
7730
7731 /* Compute margin and relief to draw. */
7732 relief = (tool_bar_button_relief >= 0
7733 ? tool_bar_button_relief
7734 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
7735 hmargin = vmargin = relief;
7736
7737 if (INTEGERP (Vtool_bar_button_margin)
7738 && XINT (Vtool_bar_button_margin) > 0)
7739 {
7740 hmargin += XFASTINT (Vtool_bar_button_margin);
7741 vmargin += XFASTINT (Vtool_bar_button_margin);
7742 }
7743 else if (CONSP (Vtool_bar_button_margin))
7744 {
7745 if (INTEGERP (XCAR (Vtool_bar_button_margin))
7746 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
7747 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
7748
7749 if (INTEGERP (XCDR (Vtool_bar_button_margin))
7750 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
7751 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
7752 }
7753
7754 if (auto_raise_tool_bar_buttons_p)
7755 {
7756 /* Add a `:relief' property to the image spec if the item is
7757 selected. */
7758 if (selected_p)
7759 {
7760 plist = Fplist_put (plist, QCrelief, make_number (-relief));
7761 hmargin -= relief;
7762 vmargin -= relief;
7763 }
7764 }
7765 else
7766 {
7767 /* If image is selected, display it pressed, i.e. with a
7768 negative relief. If it's not selected, display it with a
7769 raised relief. */
7770 plist = Fplist_put (plist, QCrelief,
7771 (selected_p
7772 ? make_number (-relief)
7773 : make_number (relief)));
7774 hmargin -= relief;
7775 vmargin -= relief;
7776 }
7777
7778 /* Put a margin around the image. */
7779 if (hmargin || vmargin)
7780 {
7781 if (hmargin == vmargin)
7782 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
7783 else
7784 plist = Fplist_put (plist, QCmargin,
7785 Fcons (make_number (hmargin),
7786 make_number (vmargin)));
7787 }
7788
7789 /* If button is not enabled, and we don't have special images
7790 for the disabled state, make the image appear disabled by
7791 applying an appropriate algorithm to it. */
7792 if (!enabled_p && idx < 0)
7793 plist = Fplist_put (plist, QCconversion, Qdisabled);
7794
7795 /* Put a `display' text property on the string for the image to
7796 display. Put a `menu-item' property on the string that gives
7797 the start of this item's properties in the tool-bar items
7798 vector. */
7799 image = Fcons (Qimage, plist);
7800 props = list4 (Qdisplay, image,
7801 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
7802
7803 /* Let the last image hide all remaining spaces in the tool bar
7804 string. The string can be longer than needed when we reuse a
7805 previous string. */
7806 if (i + 1 == f->n_tool_bar_items)
7807 end = SCHARS (f->desired_tool_bar_string);
7808 else
7809 end = i + 1;
7810 Fadd_text_properties (make_number (i), make_number (end),
7811 props, f->desired_tool_bar_string);
7812 #undef PROP
7813 }
7814
7815 UNGCPRO;
7816 }
7817
7818
7819 /* Display one line of the tool-bar of frame IT->f. */
7820
7821 static void
7822 display_tool_bar_line (it)
7823 struct it *it;
7824 {
7825 struct glyph_row *row = it->glyph_row;
7826 int max_x = it->last_visible_x;
7827 struct glyph *last;
7828
7829 prepare_desired_row (row);
7830 row->y = it->current_y;
7831
7832 /* Note that this isn't made use of if the face hasn't a box,
7833 so there's no need to check the face here. */
7834 it->start_of_box_run_p = 1;
7835
7836 while (it->current_x < max_x)
7837 {
7838 int x_before, x, n_glyphs_before, i, nglyphs;
7839
7840 /* Get the next display element. */
7841 if (!get_next_display_element (it))
7842 break;
7843
7844 /* Produce glyphs. */
7845 x_before = it->current_x;
7846 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
7847 PRODUCE_GLYPHS (it);
7848
7849 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
7850 i = 0;
7851 x = x_before;
7852 while (i < nglyphs)
7853 {
7854 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
7855
7856 if (x + glyph->pixel_width > max_x)
7857 {
7858 /* Glyph doesn't fit on line. */
7859 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
7860 it->current_x = x;
7861 goto out;
7862 }
7863
7864 ++it->hpos;
7865 x += glyph->pixel_width;
7866 ++i;
7867 }
7868
7869 /* Stop at line ends. */
7870 if (ITERATOR_AT_END_OF_LINE_P (it))
7871 break;
7872
7873 set_iterator_to_next (it, 1);
7874 }
7875
7876 out:;
7877
7878 row->displays_text_p = row->used[TEXT_AREA] != 0;
7879 extend_face_to_end_of_line (it);
7880 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
7881 last->right_box_line_p = 1;
7882 if (last == row->glyphs[TEXT_AREA])
7883 last->left_box_line_p = 1;
7884 compute_line_metrics (it);
7885
7886 /* If line is empty, make it occupy the rest of the tool-bar. */
7887 if (!row->displays_text_p)
7888 {
7889 row->height = row->phys_height = it->last_visible_y - row->y;
7890 row->ascent = row->phys_ascent = 0;
7891 }
7892
7893 row->full_width_p = 1;
7894 row->continued_p = 0;
7895 row->truncated_on_left_p = 0;
7896 row->truncated_on_right_p = 0;
7897
7898 it->current_x = it->hpos = 0;
7899 it->current_y += row->height;
7900 ++it->vpos;
7901 ++it->glyph_row;
7902 }
7903
7904
7905 /* Value is the number of screen lines needed to make all tool-bar
7906 items of frame F visible. */
7907
7908 static int
7909 tool_bar_lines_needed (f)
7910 struct frame *f;
7911 {
7912 struct window *w = XWINDOW (f->tool_bar_window);
7913 struct it it;
7914
7915 /* Initialize an iterator for iteration over
7916 F->desired_tool_bar_string in the tool-bar window of frame F. */
7917 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7918 it.first_visible_x = 0;
7919 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7920 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7921
7922 while (!ITERATOR_AT_END_P (&it))
7923 {
7924 it.glyph_row = w->desired_matrix->rows;
7925 clear_glyph_row (it.glyph_row);
7926 display_tool_bar_line (&it);
7927 }
7928
7929 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
7930 }
7931
7932
7933 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
7934 0, 1, 0,
7935 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
7936 (frame)
7937 Lisp_Object frame;
7938 {
7939 struct frame *f;
7940 struct window *w;
7941 int nlines = 0;
7942
7943 if (NILP (frame))
7944 frame = selected_frame;
7945 else
7946 CHECK_FRAME (frame);
7947 f = XFRAME (frame);
7948
7949 if (WINDOWP (f->tool_bar_window)
7950 || (w = XWINDOW (f->tool_bar_window),
7951 XFASTINT (w->height) > 0))
7952 {
7953 update_tool_bar (f, 1);
7954 if (f->n_tool_bar_items)
7955 {
7956 build_desired_tool_bar_string (f);
7957 nlines = tool_bar_lines_needed (f);
7958 }
7959 }
7960
7961 return make_number (nlines);
7962 }
7963
7964
7965 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
7966 height should be changed. */
7967
7968 static int
7969 redisplay_tool_bar (f)
7970 struct frame *f;
7971 {
7972 struct window *w;
7973 struct it it;
7974 struct glyph_row *row;
7975 int change_height_p = 0;
7976
7977 /* If frame hasn't a tool-bar window or if it is zero-height, don't
7978 do anything. This means you must start with tool-bar-lines
7979 non-zero to get the auto-sizing effect. Or in other words, you
7980 can turn off tool-bars by specifying tool-bar-lines zero. */
7981 if (!WINDOWP (f->tool_bar_window)
7982 || (w = XWINDOW (f->tool_bar_window),
7983 XFASTINT (w->height) == 0))
7984 return 0;
7985
7986 /* Set up an iterator for the tool-bar window. */
7987 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7988 it.first_visible_x = 0;
7989 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7990 row = it.glyph_row;
7991
7992 /* Build a string that represents the contents of the tool-bar. */
7993 build_desired_tool_bar_string (f);
7994 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7995
7996 /* Display as many lines as needed to display all tool-bar items. */
7997 while (it.current_y < it.last_visible_y)
7998 display_tool_bar_line (&it);
7999
8000 /* It doesn't make much sense to try scrolling in the tool-bar
8001 window, so don't do it. */
8002 w->desired_matrix->no_scrolling_p = 1;
8003 w->must_be_updated_p = 1;
8004
8005 if (auto_resize_tool_bars_p)
8006 {
8007 int nlines;
8008
8009 /* If we couldn't display everything, change the tool-bar's
8010 height. */
8011 if (IT_STRING_CHARPOS (it) < it.end_charpos)
8012 change_height_p = 1;
8013
8014 /* If there are blank lines at the end, except for a partially
8015 visible blank line at the end that is smaller than
8016 CANON_Y_UNIT, change the tool-bar's height. */
8017 row = it.glyph_row - 1;
8018 if (!row->displays_text_p
8019 && row->height >= CANON_Y_UNIT (f))
8020 change_height_p = 1;
8021
8022 /* If row displays tool-bar items, but is partially visible,
8023 change the tool-bar's height. */
8024 if (row->displays_text_p
8025 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
8026 change_height_p = 1;
8027
8028 /* Resize windows as needed by changing the `tool-bar-lines'
8029 frame parameter. */
8030 if (change_height_p
8031 && (nlines = tool_bar_lines_needed (f),
8032 nlines != XFASTINT (w->height)))
8033 {
8034 extern Lisp_Object Qtool_bar_lines;
8035 Lisp_Object frame;
8036 int old_height = XFASTINT (w->height);
8037
8038 XSETFRAME (frame, f);
8039 clear_glyph_matrix (w->desired_matrix);
8040 Fmodify_frame_parameters (frame,
8041 Fcons (Fcons (Qtool_bar_lines,
8042 make_number (nlines)),
8043 Qnil));
8044 if (XFASTINT (w->height) != old_height)
8045 fonts_changed_p = 1;
8046 }
8047 }
8048
8049 return change_height_p;
8050 }
8051
8052
8053 /* Get information about the tool-bar item which is displayed in GLYPH
8054 on frame F. Return in *PROP_IDX the index where tool-bar item
8055 properties start in F->tool_bar_items. Value is zero if
8056 GLYPH doesn't display a tool-bar item. */
8057
8058 int
8059 tool_bar_item_info (f, glyph, prop_idx)
8060 struct frame *f;
8061 struct glyph *glyph;
8062 int *prop_idx;
8063 {
8064 Lisp_Object prop;
8065 int success_p;
8066 int charpos;
8067
8068 /* This function can be called asynchronously, which means we must
8069 exclude any possibility that Fget_text_property signals an
8070 error. */
8071 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
8072 charpos = max (0, charpos);
8073
8074 /* Get the text property `menu-item' at pos. The value of that
8075 property is the start index of this item's properties in
8076 F->tool_bar_items. */
8077 prop = Fget_text_property (make_number (charpos),
8078 Qmenu_item, f->current_tool_bar_string);
8079 if (INTEGERP (prop))
8080 {
8081 *prop_idx = XINT (prop);
8082 success_p = 1;
8083 }
8084 else
8085 success_p = 0;
8086
8087 return success_p;
8088 }
8089
8090 #endif /* HAVE_WINDOW_SYSTEM */
8091
8092
8093 \f
8094 /************************************************************************
8095 Horizontal scrolling
8096 ************************************************************************/
8097
8098 static int hscroll_window_tree P_ ((Lisp_Object));
8099 static int hscroll_windows P_ ((Lisp_Object));
8100
8101 /* For all leaf windows in the window tree rooted at WINDOW, set their
8102 hscroll value so that PT is (i) visible in the window, and (ii) so
8103 that it is not within a certain margin at the window's left and
8104 right border. Value is non-zero if any window's hscroll has been
8105 changed. */
8106
8107 static int
8108 hscroll_window_tree (window)
8109 Lisp_Object window;
8110 {
8111 int hscrolled_p = 0;
8112 int hscroll_relative_p = FLOATP (Vhscroll_step);
8113 int hscroll_step_abs = 0;
8114 double hscroll_step_rel = 0;
8115
8116 if (hscroll_relative_p)
8117 {
8118 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
8119 if (hscroll_step_rel < 0)
8120 {
8121 hscroll_relative_p = 0;
8122 hscroll_step_abs = 0;
8123 }
8124 }
8125 else if (INTEGERP (Vhscroll_step))
8126 {
8127 hscroll_step_abs = XINT (Vhscroll_step);
8128 if (hscroll_step_abs < 0)
8129 hscroll_step_abs = 0;
8130 }
8131 else
8132 hscroll_step_abs = 0;
8133
8134 while (WINDOWP (window))
8135 {
8136 struct window *w = XWINDOW (window);
8137
8138 if (WINDOWP (w->hchild))
8139 hscrolled_p |= hscroll_window_tree (w->hchild);
8140 else if (WINDOWP (w->vchild))
8141 hscrolled_p |= hscroll_window_tree (w->vchild);
8142 else if (w->cursor.vpos >= 0)
8143 {
8144 int h_margin, text_area_x, text_area_y;
8145 int text_area_width, text_area_height;
8146 struct glyph_row *current_cursor_row
8147 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
8148 struct glyph_row *desired_cursor_row
8149 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
8150 struct glyph_row *cursor_row
8151 = (desired_cursor_row->enabled_p
8152 ? desired_cursor_row
8153 : current_cursor_row);
8154
8155 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
8156 &text_area_width, &text_area_height);
8157
8158 /* Scroll when cursor is inside this scroll margin. */
8159 h_margin = hscroll_margin * CANON_X_UNIT (XFRAME (w->frame));
8160
8161 if ((XFASTINT (w->hscroll)
8162 && w->cursor.x <= h_margin)
8163 || (cursor_row->enabled_p
8164 && cursor_row->truncated_on_right_p
8165 && (w->cursor.x >= text_area_width - h_margin)))
8166 {
8167 struct it it;
8168 int hscroll;
8169 struct buffer *saved_current_buffer;
8170 int pt;
8171 int wanted_x;
8172
8173 /* Find point in a display of infinite width. */
8174 saved_current_buffer = current_buffer;
8175 current_buffer = XBUFFER (w->buffer);
8176
8177 if (w == XWINDOW (selected_window))
8178 pt = BUF_PT (current_buffer);
8179 else
8180 {
8181 pt = marker_position (w->pointm);
8182 pt = max (BEGV, pt);
8183 pt = min (ZV, pt);
8184 }
8185
8186 /* Move iterator to pt starting at cursor_row->start in
8187 a line with infinite width. */
8188 init_to_row_start (&it, w, cursor_row);
8189 it.last_visible_x = INFINITY;
8190 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
8191 current_buffer = saved_current_buffer;
8192
8193 /* Position cursor in window. */
8194 if (!hscroll_relative_p && hscroll_step_abs == 0)
8195 hscroll = max (0, it.current_x - text_area_width / 2)
8196 / CANON_X_UNIT (it.f);
8197 else if (w->cursor.x >= text_area_width - h_margin)
8198 {
8199 if (hscroll_relative_p)
8200 wanted_x = text_area_width * (1 - hscroll_step_rel)
8201 - h_margin;
8202 else
8203 wanted_x = text_area_width
8204 - hscroll_step_abs * CANON_X_UNIT (it.f)
8205 - h_margin;
8206 hscroll
8207 = max (0, it.current_x - wanted_x) / CANON_X_UNIT (it.f);
8208 }
8209 else
8210 {
8211 if (hscroll_relative_p)
8212 wanted_x = text_area_width * hscroll_step_rel
8213 + h_margin;
8214 else
8215 wanted_x = hscroll_step_abs * CANON_X_UNIT (it.f)
8216 + h_margin;
8217 hscroll
8218 = max (0, it.current_x - wanted_x) / CANON_X_UNIT (it.f);
8219 }
8220 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
8221
8222 /* Don't call Fset_window_hscroll if value hasn't
8223 changed because it will prevent redisplay
8224 optimizations. */
8225 if (XFASTINT (w->hscroll) != hscroll)
8226 {
8227 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
8228 w->hscroll = make_number (hscroll);
8229 hscrolled_p = 1;
8230 }
8231 }
8232 }
8233
8234 window = w->next;
8235 }
8236
8237 /* Value is non-zero if hscroll of any leaf window has been changed. */
8238 return hscrolled_p;
8239 }
8240
8241
8242 /* Set hscroll so that cursor is visible and not inside horizontal
8243 scroll margins for all windows in the tree rooted at WINDOW. See
8244 also hscroll_window_tree above. Value is non-zero if any window's
8245 hscroll has been changed. If it has, desired matrices on the frame
8246 of WINDOW are cleared. */
8247
8248 static int
8249 hscroll_windows (window)
8250 Lisp_Object window;
8251 {
8252 int hscrolled_p;
8253
8254 if (automatic_hscrolling_p)
8255 {
8256 hscrolled_p = hscroll_window_tree (window);
8257 if (hscrolled_p)
8258 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
8259 }
8260 else
8261 hscrolled_p = 0;
8262 return hscrolled_p;
8263 }
8264
8265
8266 \f
8267 /************************************************************************
8268 Redisplay
8269 ************************************************************************/
8270
8271 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
8272 to a non-zero value. This is sometimes handy to have in a debugger
8273 session. */
8274
8275 #if GLYPH_DEBUG
8276
8277 /* First and last unchanged row for try_window_id. */
8278
8279 int debug_first_unchanged_at_end_vpos;
8280 int debug_last_unchanged_at_beg_vpos;
8281
8282 /* Delta vpos and y. */
8283
8284 int debug_dvpos, debug_dy;
8285
8286 /* Delta in characters and bytes for try_window_id. */
8287
8288 int debug_delta, debug_delta_bytes;
8289
8290 /* Values of window_end_pos and window_end_vpos at the end of
8291 try_window_id. */
8292
8293 EMACS_INT debug_end_pos, debug_end_vpos;
8294
8295 /* Append a string to W->desired_matrix->method. FMT is a printf
8296 format string. A1...A9 are a supplement for a variable-length
8297 argument list. If trace_redisplay_p is non-zero also printf the
8298 resulting string to stderr. */
8299
8300 static void
8301 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
8302 struct window *w;
8303 char *fmt;
8304 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
8305 {
8306 char buffer[512];
8307 char *method = w->desired_matrix->method;
8308 int len = strlen (method);
8309 int size = sizeof w->desired_matrix->method;
8310 int remaining = size - len - 1;
8311
8312 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
8313 if (len && remaining)
8314 {
8315 method[len] = '|';
8316 --remaining, ++len;
8317 }
8318
8319 strncpy (method + len, buffer, remaining);
8320
8321 if (trace_redisplay_p)
8322 fprintf (stderr, "%p (%s): %s\n",
8323 w,
8324 ((BUFFERP (w->buffer)
8325 && STRINGP (XBUFFER (w->buffer)->name))
8326 ? (char *) SDATA (XBUFFER (w->buffer)->name)
8327 : "no buffer"),
8328 buffer);
8329 }
8330
8331 #endif /* GLYPH_DEBUG */
8332
8333
8334 /* Value is non-zero if all changes in window W, which displays
8335 current_buffer, are in the text between START and END. START is a
8336 buffer position, END is given as a distance from Z. Used in
8337 redisplay_internal for display optimization. */
8338
8339 static INLINE int
8340 text_outside_line_unchanged_p (w, start, end)
8341 struct window *w;
8342 int start, end;
8343 {
8344 int unchanged_p = 1;
8345
8346 /* If text or overlays have changed, see where. */
8347 if (XFASTINT (w->last_modified) < MODIFF
8348 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8349 {
8350 /* Gap in the line? */
8351 if (GPT < start || Z - GPT < end)
8352 unchanged_p = 0;
8353
8354 /* Changes start in front of the line, or end after it? */
8355 if (unchanged_p
8356 && (BEG_UNCHANGED < start - 1
8357 || END_UNCHANGED < end))
8358 unchanged_p = 0;
8359
8360 /* If selective display, can't optimize if changes start at the
8361 beginning of the line. */
8362 if (unchanged_p
8363 && INTEGERP (current_buffer->selective_display)
8364 && XINT (current_buffer->selective_display) > 0
8365 && (BEG_UNCHANGED < start || GPT <= start))
8366 unchanged_p = 0;
8367
8368 /* If there are overlays at the start or end of the line, these
8369 may have overlay strings with newlines in them. A change at
8370 START, for instance, may actually concern the display of such
8371 overlay strings as well, and they are displayed on different
8372 lines. So, quickly rule out this case. (For the future, it
8373 might be desirable to implement something more telling than
8374 just BEG/END_UNCHANGED.) */
8375 if (unchanged_p)
8376 {
8377 if (BEG + BEG_UNCHANGED == start
8378 && overlay_touches_p (start))
8379 unchanged_p = 0;
8380 if (END_UNCHANGED == end
8381 && overlay_touches_p (Z - end))
8382 unchanged_p = 0;
8383 }
8384 }
8385
8386 return unchanged_p;
8387 }
8388
8389
8390 /* Do a frame update, taking possible shortcuts into account. This is
8391 the main external entry point for redisplay.
8392
8393 If the last redisplay displayed an echo area message and that message
8394 is no longer requested, we clear the echo area or bring back the
8395 mini-buffer if that is in use. */
8396
8397 void
8398 redisplay ()
8399 {
8400 redisplay_internal (0);
8401 }
8402
8403
8404 /* Return 1 if point moved out of or into a composition. Otherwise
8405 return 0. PREV_BUF and PREV_PT are the last point buffer and
8406 position. BUF and PT are the current point buffer and position. */
8407
8408 int
8409 check_point_in_composition (prev_buf, prev_pt, buf, pt)
8410 struct buffer *prev_buf, *buf;
8411 int prev_pt, pt;
8412 {
8413 int start, end;
8414 Lisp_Object prop;
8415 Lisp_Object buffer;
8416
8417 XSETBUFFER (buffer, buf);
8418 /* Check a composition at the last point if point moved within the
8419 same buffer. */
8420 if (prev_buf == buf)
8421 {
8422 if (prev_pt == pt)
8423 /* Point didn't move. */
8424 return 0;
8425
8426 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
8427 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
8428 && COMPOSITION_VALID_P (start, end, prop)
8429 && start < prev_pt && end > prev_pt)
8430 /* The last point was within the composition. Return 1 iff
8431 point moved out of the composition. */
8432 return (pt <= start || pt >= end);
8433 }
8434
8435 /* Check a composition at the current point. */
8436 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
8437 && find_composition (pt, -1, &start, &end, &prop, buffer)
8438 && COMPOSITION_VALID_P (start, end, prop)
8439 && start < pt && end > pt);
8440 }
8441
8442
8443 /* Reconsider the setting of B->clip_changed which is displayed
8444 in window W. */
8445
8446 static INLINE void
8447 reconsider_clip_changes (w, b)
8448 struct window *w;
8449 struct buffer *b;
8450 {
8451 if (b->clip_changed
8452 && !NILP (w->window_end_valid)
8453 && w->current_matrix->buffer == b
8454 && w->current_matrix->zv == BUF_ZV (b)
8455 && w->current_matrix->begv == BUF_BEGV (b))
8456 b->clip_changed = 0;
8457
8458 /* If display wasn't paused, and W is not a tool bar window, see if
8459 point has been moved into or out of a composition. In that case,
8460 we set b->clip_changed to 1 to force updating the screen. If
8461 b->clip_changed has already been set to 1, we can skip this
8462 check. */
8463 if (!b->clip_changed
8464 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
8465 {
8466 int pt;
8467
8468 if (w == XWINDOW (selected_window))
8469 pt = BUF_PT (current_buffer);
8470 else
8471 pt = marker_position (w->pointm);
8472
8473 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
8474 || pt != XINT (w->last_point))
8475 && check_point_in_composition (w->current_matrix->buffer,
8476 XINT (w->last_point),
8477 XBUFFER (w->buffer), pt))
8478 b->clip_changed = 1;
8479 }
8480 }
8481 \f
8482 #define STOP_POLLING \
8483 do { if (! polling_stopped_here) stop_polling (); \
8484 polling_stopped_here = 1; } while (0)
8485
8486 #define RESUME_POLLING \
8487 do { if (polling_stopped_here) start_polling (); \
8488 polling_stopped_here = 0; } while (0)
8489
8490
8491 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
8492 response to any user action; therefore, we should preserve the echo
8493 area. (Actually, our caller does that job.) Perhaps in the future
8494 avoid recentering windows if it is not necessary; currently that
8495 causes some problems. */
8496
8497 static void
8498 redisplay_internal (preserve_echo_area)
8499 int preserve_echo_area;
8500 {
8501 struct window *w = XWINDOW (selected_window);
8502 struct frame *f = XFRAME (w->frame);
8503 int pause;
8504 int must_finish = 0;
8505 struct text_pos tlbufpos, tlendpos;
8506 int number_of_visible_frames;
8507 int count;
8508 struct frame *sf = SELECTED_FRAME ();
8509 int polling_stopped_here = 0;
8510
8511 /* Non-zero means redisplay has to consider all windows on all
8512 frames. Zero means, only selected_window is considered. */
8513 int consider_all_windows_p;
8514
8515 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
8516
8517 /* No redisplay if running in batch mode or frame is not yet fully
8518 initialized, or redisplay is explicitly turned off by setting
8519 Vinhibit_redisplay. */
8520 if (noninteractive
8521 || !NILP (Vinhibit_redisplay)
8522 || !f->glyphs_initialized_p)
8523 return;
8524
8525 /* The flag redisplay_performed_directly_p is set by
8526 direct_output_for_insert when it already did the whole screen
8527 update necessary. */
8528 if (redisplay_performed_directly_p)
8529 {
8530 redisplay_performed_directly_p = 0;
8531 if (!hscroll_windows (selected_window))
8532 return;
8533 }
8534
8535 #ifdef USE_X_TOOLKIT
8536 if (popup_activated ())
8537 return;
8538 #endif
8539
8540 /* I don't think this happens but let's be paranoid. */
8541 if (redisplaying_p)
8542 return;
8543
8544 /* Record a function that resets redisplaying_p to its old value
8545 when we leave this function. */
8546 count = SPECPDL_INDEX ();
8547 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
8548 ++redisplaying_p;
8549 specbind (Qinhibit_free_realized_faces, Qnil);
8550
8551 retry:
8552 pause = 0;
8553 reconsider_clip_changes (w, current_buffer);
8554
8555 /* If new fonts have been loaded that make a glyph matrix adjustment
8556 necessary, do it. */
8557 if (fonts_changed_p)
8558 {
8559 adjust_glyphs (NULL);
8560 ++windows_or_buffers_changed;
8561 fonts_changed_p = 0;
8562 }
8563
8564 /* If face_change_count is non-zero, init_iterator will free all
8565 realized faces, which includes the faces referenced from current
8566 matrices. So, we can't reuse current matrices in this case. */
8567 if (face_change_count)
8568 ++windows_or_buffers_changed;
8569
8570 if (! FRAME_WINDOW_P (sf)
8571 && previous_terminal_frame != sf)
8572 {
8573 /* Since frames on an ASCII terminal share the same display
8574 area, displaying a different frame means redisplay the whole
8575 thing. */
8576 windows_or_buffers_changed++;
8577 SET_FRAME_GARBAGED (sf);
8578 XSETFRAME (Vterminal_frame, sf);
8579 }
8580 previous_terminal_frame = sf;
8581
8582 /* Set the visible flags for all frames. Do this before checking
8583 for resized or garbaged frames; they want to know if their frames
8584 are visible. See the comment in frame.h for
8585 FRAME_SAMPLE_VISIBILITY. */
8586 {
8587 Lisp_Object tail, frame;
8588
8589 number_of_visible_frames = 0;
8590
8591 FOR_EACH_FRAME (tail, frame)
8592 {
8593 struct frame *f = XFRAME (frame);
8594
8595 FRAME_SAMPLE_VISIBILITY (f);
8596 if (FRAME_VISIBLE_P (f))
8597 ++number_of_visible_frames;
8598 clear_desired_matrices (f);
8599 }
8600 }
8601
8602 /* Notice any pending interrupt request to change frame size. */
8603 do_pending_window_change (1);
8604
8605 /* Clear frames marked as garbaged. */
8606 if (frame_garbaged)
8607 clear_garbaged_frames ();
8608
8609 /* Build menubar and tool-bar items. */
8610 prepare_menu_bars ();
8611
8612 if (windows_or_buffers_changed)
8613 update_mode_lines++;
8614
8615 /* Detect case that we need to write or remove a star in the mode line. */
8616 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
8617 {
8618 w->update_mode_line = Qt;
8619 if (buffer_shared > 1)
8620 update_mode_lines++;
8621 }
8622
8623 /* If %c is in the mode line, update it if needed. */
8624 if (!NILP (w->column_number_displayed)
8625 /* This alternative quickly identifies a common case
8626 where no change is needed. */
8627 && !(PT == XFASTINT (w->last_point)
8628 && XFASTINT (w->last_modified) >= MODIFF
8629 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
8630 && (XFASTINT (w->column_number_displayed)
8631 != (int) current_column ())) /* iftc */
8632 w->update_mode_line = Qt;
8633
8634 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
8635
8636 /* The variable buffer_shared is set in redisplay_window and
8637 indicates that we redisplay a buffer in different windows. See
8638 there. */
8639 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
8640 || cursor_type_changed);
8641
8642 /* If specs for an arrow have changed, do thorough redisplay
8643 to ensure we remove any arrow that should no longer exist. */
8644 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
8645 || ! EQ (Voverlay_arrow_string, last_arrow_string))
8646 consider_all_windows_p = windows_or_buffers_changed = 1;
8647
8648 /* Normally the message* functions will have already displayed and
8649 updated the echo area, but the frame may have been trashed, or
8650 the update may have been preempted, so display the echo area
8651 again here. Checking message_cleared_p captures the case that
8652 the echo area should be cleared. */
8653 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
8654 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
8655 || (message_cleared_p
8656 && minibuf_level == 0
8657 /* If the mini-window is currently selected, this means the
8658 echo-area doesn't show through. */
8659 && !MINI_WINDOW_P (XWINDOW (selected_window))))
8660 {
8661 int window_height_changed_p = echo_area_display (0);
8662 must_finish = 1;
8663
8664 /* If we don't display the current message, don't clear the
8665 message_cleared_p flag, because, if we did, we wouldn't clear
8666 the echo area in the next redisplay which doesn't preserve
8667 the echo area. */
8668 if (!display_last_displayed_message_p)
8669 message_cleared_p = 0;
8670
8671 if (fonts_changed_p)
8672 goto retry;
8673 else if (window_height_changed_p)
8674 {
8675 consider_all_windows_p = 1;
8676 ++update_mode_lines;
8677 ++windows_or_buffers_changed;
8678
8679 /* If window configuration was changed, frames may have been
8680 marked garbaged. Clear them or we will experience
8681 surprises wrt scrolling. */
8682 if (frame_garbaged)
8683 clear_garbaged_frames ();
8684 }
8685 }
8686 else if (EQ (selected_window, minibuf_window)
8687 && (current_buffer->clip_changed
8688 || XFASTINT (w->last_modified) < MODIFF
8689 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8690 && resize_mini_window (w, 0))
8691 {
8692 /* Resized active mini-window to fit the size of what it is
8693 showing if its contents might have changed. */
8694 must_finish = 1;
8695 consider_all_windows_p = 1;
8696 ++windows_or_buffers_changed;
8697 ++update_mode_lines;
8698
8699 /* If window configuration was changed, frames may have been
8700 marked garbaged. Clear them or we will experience
8701 surprises wrt scrolling. */
8702 if (frame_garbaged)
8703 clear_garbaged_frames ();
8704 }
8705
8706
8707 /* If showing the region, and mark has changed, we must redisplay
8708 the whole window. The assignment to this_line_start_pos prevents
8709 the optimization directly below this if-statement. */
8710 if (((!NILP (Vtransient_mark_mode)
8711 && !NILP (XBUFFER (w->buffer)->mark_active))
8712 != !NILP (w->region_showing))
8713 || (!NILP (w->region_showing)
8714 && !EQ (w->region_showing,
8715 Fmarker_position (XBUFFER (w->buffer)->mark))))
8716 CHARPOS (this_line_start_pos) = 0;
8717
8718 /* Optimize the case that only the line containing the cursor in the
8719 selected window has changed. Variables starting with this_ are
8720 set in display_line and record information about the line
8721 containing the cursor. */
8722 tlbufpos = this_line_start_pos;
8723 tlendpos = this_line_end_pos;
8724 if (!consider_all_windows_p
8725 && CHARPOS (tlbufpos) > 0
8726 && NILP (w->update_mode_line)
8727 && !current_buffer->clip_changed
8728 && !current_buffer->prevent_redisplay_optimizations_p
8729 && FRAME_VISIBLE_P (XFRAME (w->frame))
8730 && !FRAME_OBSCURED_P (XFRAME (w->frame))
8731 /* Make sure recorded data applies to current buffer, etc. */
8732 && this_line_buffer == current_buffer
8733 && current_buffer == XBUFFER (w->buffer)
8734 && NILP (w->force_start)
8735 && NILP (w->optional_new_start)
8736 /* Point must be on the line that we have info recorded about. */
8737 && PT >= CHARPOS (tlbufpos)
8738 && PT <= Z - CHARPOS (tlendpos)
8739 /* All text outside that line, including its final newline,
8740 must be unchanged */
8741 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
8742 CHARPOS (tlendpos)))
8743 {
8744 if (CHARPOS (tlbufpos) > BEGV
8745 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
8746 && (CHARPOS (tlbufpos) == ZV
8747 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
8748 /* Former continuation line has disappeared by becoming empty */
8749 goto cancel;
8750 else if (XFASTINT (w->last_modified) < MODIFF
8751 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
8752 || MINI_WINDOW_P (w))
8753 {
8754 /* We have to handle the case of continuation around a
8755 wide-column character (See the comment in indent.c around
8756 line 885).
8757
8758 For instance, in the following case:
8759
8760 -------- Insert --------
8761 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
8762 J_I_ ==> J_I_ `^^' are cursors.
8763 ^^ ^^
8764 -------- --------
8765
8766 As we have to redraw the line above, we should goto cancel. */
8767
8768 struct it it;
8769 int line_height_before = this_line_pixel_height;
8770
8771 /* Note that start_display will handle the case that the
8772 line starting at tlbufpos is a continuation lines. */
8773 start_display (&it, w, tlbufpos);
8774
8775 /* Implementation note: It this still necessary? */
8776 if (it.current_x != this_line_start_x)
8777 goto cancel;
8778
8779 TRACE ((stderr, "trying display optimization 1\n"));
8780 w->cursor.vpos = -1;
8781 overlay_arrow_seen = 0;
8782 it.vpos = this_line_vpos;
8783 it.current_y = this_line_y;
8784 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
8785 display_line (&it);
8786
8787 /* If line contains point, is not continued,
8788 and ends at same distance from eob as before, we win */
8789 if (w->cursor.vpos >= 0
8790 /* Line is not continued, otherwise this_line_start_pos
8791 would have been set to 0 in display_line. */
8792 && CHARPOS (this_line_start_pos)
8793 /* Line ends as before. */
8794 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
8795 /* Line has same height as before. Otherwise other lines
8796 would have to be shifted up or down. */
8797 && this_line_pixel_height == line_height_before)
8798 {
8799 /* If this is not the window's last line, we must adjust
8800 the charstarts of the lines below. */
8801 if (it.current_y < it.last_visible_y)
8802 {
8803 struct glyph_row *row
8804 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
8805 int delta, delta_bytes;
8806
8807 if (Z - CHARPOS (tlendpos) == ZV)
8808 {
8809 /* This line ends at end of (accessible part of)
8810 buffer. There is no newline to count. */
8811 delta = (Z
8812 - CHARPOS (tlendpos)
8813 - MATRIX_ROW_START_CHARPOS (row));
8814 delta_bytes = (Z_BYTE
8815 - BYTEPOS (tlendpos)
8816 - MATRIX_ROW_START_BYTEPOS (row));
8817 }
8818 else
8819 {
8820 /* This line ends in a newline. Must take
8821 account of the newline and the rest of the
8822 text that follows. */
8823 delta = (Z
8824 - CHARPOS (tlendpos)
8825 - MATRIX_ROW_START_CHARPOS (row));
8826 delta_bytes = (Z_BYTE
8827 - BYTEPOS (tlendpos)
8828 - MATRIX_ROW_START_BYTEPOS (row));
8829 }
8830
8831 increment_matrix_positions (w->current_matrix,
8832 this_line_vpos + 1,
8833 w->current_matrix->nrows,
8834 delta, delta_bytes);
8835 }
8836
8837 /* If this row displays text now but previously didn't,
8838 or vice versa, w->window_end_vpos may have to be
8839 adjusted. */
8840 if ((it.glyph_row - 1)->displays_text_p)
8841 {
8842 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
8843 XSETINT (w->window_end_vpos, this_line_vpos);
8844 }
8845 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
8846 && this_line_vpos > 0)
8847 XSETINT (w->window_end_vpos, this_line_vpos - 1);
8848 w->window_end_valid = Qnil;
8849
8850 /* Update hint: No need to try to scroll in update_window. */
8851 w->desired_matrix->no_scrolling_p = 1;
8852
8853 #if GLYPH_DEBUG
8854 *w->desired_matrix->method = 0;
8855 debug_method_add (w, "optimization 1");
8856 #endif
8857 goto update;
8858 }
8859 else
8860 goto cancel;
8861 }
8862 else if (/* Cursor position hasn't changed. */
8863 PT == XFASTINT (w->last_point)
8864 /* Make sure the cursor was last displayed
8865 in this window. Otherwise we have to reposition it. */
8866 && 0 <= w->cursor.vpos
8867 && XINT (w->height) > w->cursor.vpos)
8868 {
8869 if (!must_finish)
8870 {
8871 do_pending_window_change (1);
8872
8873 /* We used to always goto end_of_redisplay here, but this
8874 isn't enough if we have a blinking cursor. */
8875 if (w->cursor_off_p == w->last_cursor_off_p)
8876 goto end_of_redisplay;
8877 }
8878 goto update;
8879 }
8880 /* If highlighting the region, or if the cursor is in the echo area,
8881 then we can't just move the cursor. */
8882 else if (! (!NILP (Vtransient_mark_mode)
8883 && !NILP (current_buffer->mark_active))
8884 && (EQ (selected_window, current_buffer->last_selected_window)
8885 || highlight_nonselected_windows)
8886 && NILP (w->region_showing)
8887 && NILP (Vshow_trailing_whitespace)
8888 && !cursor_in_echo_area)
8889 {
8890 struct it it;
8891 struct glyph_row *row;
8892
8893 /* Skip from tlbufpos to PT and see where it is. Note that
8894 PT may be in invisible text. If so, we will end at the
8895 next visible position. */
8896 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
8897 NULL, DEFAULT_FACE_ID);
8898 it.current_x = this_line_start_x;
8899 it.current_y = this_line_y;
8900 it.vpos = this_line_vpos;
8901
8902 /* The call to move_it_to stops in front of PT, but
8903 moves over before-strings. */
8904 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
8905
8906 if (it.vpos == this_line_vpos
8907 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
8908 row->enabled_p))
8909 {
8910 xassert (this_line_vpos == it.vpos);
8911 xassert (this_line_y == it.current_y);
8912 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
8913 #if GLYPH_DEBUG
8914 *w->desired_matrix->method = 0;
8915 debug_method_add (w, "optimization 3");
8916 #endif
8917 goto update;
8918 }
8919 else
8920 goto cancel;
8921 }
8922
8923 cancel:
8924 /* Text changed drastically or point moved off of line. */
8925 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
8926 }
8927
8928 CHARPOS (this_line_start_pos) = 0;
8929 consider_all_windows_p |= buffer_shared > 1;
8930 ++clear_face_cache_count;
8931
8932
8933 /* Build desired matrices, and update the display. If
8934 consider_all_windows_p is non-zero, do it for all windows on all
8935 frames. Otherwise do it for selected_window, only. */
8936
8937 if (consider_all_windows_p)
8938 {
8939 Lisp_Object tail, frame;
8940 int i, n = 0, size = 50;
8941 struct frame **updated
8942 = (struct frame **) alloca (size * sizeof *updated);
8943
8944 /* Clear the face cache eventually. */
8945 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
8946 {
8947 clear_face_cache (0);
8948 clear_face_cache_count = 0;
8949 }
8950
8951 /* Recompute # windows showing selected buffer. This will be
8952 incremented each time such a window is displayed. */
8953 buffer_shared = 0;
8954
8955 FOR_EACH_FRAME (tail, frame)
8956 {
8957 struct frame *f = XFRAME (frame);
8958
8959 if (FRAME_WINDOW_P (f) || f == sf)
8960 {
8961 #ifdef HAVE_WINDOW_SYSTEM
8962 if (clear_face_cache_count % 50 == 0
8963 && FRAME_WINDOW_P (f))
8964 clear_image_cache (f, 0);
8965 #endif /* HAVE_WINDOW_SYSTEM */
8966
8967 /* Mark all the scroll bars to be removed; we'll redeem
8968 the ones we want when we redisplay their windows. */
8969 if (condemn_scroll_bars_hook)
8970 condemn_scroll_bars_hook (f);
8971
8972 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8973 redisplay_windows (FRAME_ROOT_WINDOW (f));
8974
8975 /* Any scroll bars which redisplay_windows should have
8976 nuked should now go away. */
8977 if (judge_scroll_bars_hook)
8978 judge_scroll_bars_hook (f);
8979
8980 /* If fonts changed, display again. */
8981 /* ??? rms: I suspect it is a mistake to jump all the way
8982 back to retry here. It should just retry this frame. */
8983 if (fonts_changed_p)
8984 goto retry;
8985
8986 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8987 {
8988 /* See if we have to hscroll. */
8989 if (hscroll_windows (f->root_window))
8990 goto retry;
8991
8992 /* Prevent various kinds of signals during display
8993 update. stdio is not robust about handling
8994 signals, which can cause an apparent I/O
8995 error. */
8996 if (interrupt_input)
8997 unrequest_sigio ();
8998 STOP_POLLING;
8999
9000 /* Update the display. */
9001 set_window_update_flags (XWINDOW (f->root_window), 1);
9002 pause |= update_frame (f, 0, 0);
9003 if (pause)
9004 break;
9005
9006 if (n == size)
9007 {
9008 int nbytes = size * sizeof *updated;
9009 struct frame **p = (struct frame **) alloca (2 * nbytes);
9010 bcopy (updated, p, nbytes);
9011 size *= 2;
9012 }
9013
9014 updated[n++] = f;
9015 }
9016 }
9017 }
9018
9019 /* Do the mark_window_display_accurate after all windows have
9020 been redisplayed because this call resets flags in buffers
9021 which are needed for proper redisplay. */
9022 for (i = 0; i < n; ++i)
9023 {
9024 struct frame *f = updated[i];
9025 mark_window_display_accurate (f->root_window, 1);
9026 if (frame_up_to_date_hook)
9027 frame_up_to_date_hook (f);
9028 }
9029 }
9030 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
9031 {
9032 Lisp_Object mini_window;
9033 struct frame *mini_frame;
9034
9035 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
9036 /* Use list_of_error, not Qerror, so that
9037 we catch only errors and don't run the debugger. */
9038 internal_condition_case_1 (redisplay_window_1, selected_window,
9039 list_of_error,
9040 redisplay_window_error);
9041
9042 /* Compare desired and current matrices, perform output. */
9043
9044 update:
9045 /* If fonts changed, display again. */
9046 if (fonts_changed_p)
9047 goto retry;
9048
9049 /* Prevent various kinds of signals during display update.
9050 stdio is not robust about handling signals,
9051 which can cause an apparent I/O error. */
9052 if (interrupt_input)
9053 unrequest_sigio ();
9054 STOP_POLLING;
9055
9056 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
9057 {
9058 if (hscroll_windows (selected_window))
9059 goto retry;
9060
9061 XWINDOW (selected_window)->must_be_updated_p = 1;
9062 pause = update_frame (sf, 0, 0);
9063 }
9064
9065 /* We may have called echo_area_display at the top of this
9066 function. If the echo area is on another frame, that may
9067 have put text on a frame other than the selected one, so the
9068 above call to update_frame would not have caught it. Catch
9069 it here. */
9070 mini_window = FRAME_MINIBUF_WINDOW (sf);
9071 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9072
9073 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
9074 {
9075 XWINDOW (mini_window)->must_be_updated_p = 1;
9076 pause |= update_frame (mini_frame, 0, 0);
9077 if (!pause && hscroll_windows (mini_window))
9078 goto retry;
9079 }
9080 }
9081
9082 /* If display was paused because of pending input, make sure we do a
9083 thorough update the next time. */
9084 if (pause)
9085 {
9086 /* Prevent the optimization at the beginning of
9087 redisplay_internal that tries a single-line update of the
9088 line containing the cursor in the selected window. */
9089 CHARPOS (this_line_start_pos) = 0;
9090
9091 /* Let the overlay arrow be updated the next time. */
9092 if (!NILP (last_arrow_position))
9093 {
9094 last_arrow_position = Qt;
9095 last_arrow_string = Qt;
9096 }
9097
9098 /* If we pause after scrolling, some rows in the current
9099 matrices of some windows are not valid. */
9100 if (!WINDOW_FULL_WIDTH_P (w)
9101 && !FRAME_WINDOW_P (XFRAME (w->frame)))
9102 update_mode_lines = 1;
9103 }
9104 else
9105 {
9106 if (!consider_all_windows_p)
9107 {
9108 /* This has already been done above if
9109 consider_all_windows_p is set. */
9110 mark_window_display_accurate_1 (w, 1);
9111
9112 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
9113 last_arrow_string = Voverlay_arrow_string;
9114
9115 if (frame_up_to_date_hook != 0)
9116 frame_up_to_date_hook (sf);
9117 }
9118
9119 update_mode_lines = 0;
9120 windows_or_buffers_changed = 0;
9121 cursor_type_changed = 0;
9122 }
9123
9124 /* Start SIGIO interrupts coming again. Having them off during the
9125 code above makes it less likely one will discard output, but not
9126 impossible, since there might be stuff in the system buffer here.
9127 But it is much hairier to try to do anything about that. */
9128 if (interrupt_input)
9129 request_sigio ();
9130 RESUME_POLLING;
9131
9132 /* If a frame has become visible which was not before, redisplay
9133 again, so that we display it. Expose events for such a frame
9134 (which it gets when becoming visible) don't call the parts of
9135 redisplay constructing glyphs, so simply exposing a frame won't
9136 display anything in this case. So, we have to display these
9137 frames here explicitly. */
9138 if (!pause)
9139 {
9140 Lisp_Object tail, frame;
9141 int new_count = 0;
9142
9143 FOR_EACH_FRAME (tail, frame)
9144 {
9145 int this_is_visible = 0;
9146
9147 if (XFRAME (frame)->visible)
9148 this_is_visible = 1;
9149 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
9150 if (XFRAME (frame)->visible)
9151 this_is_visible = 1;
9152
9153 if (this_is_visible)
9154 new_count++;
9155 }
9156
9157 if (new_count != number_of_visible_frames)
9158 windows_or_buffers_changed++;
9159 }
9160
9161 /* Change frame size now if a change is pending. */
9162 do_pending_window_change (1);
9163
9164 /* If we just did a pending size change, or have additional
9165 visible frames, redisplay again. */
9166 if (windows_or_buffers_changed && !pause)
9167 goto retry;
9168
9169 end_of_redisplay:
9170 unbind_to (count, Qnil);
9171 RESUME_POLLING;
9172 }
9173
9174
9175 /* Redisplay, but leave alone any recent echo area message unless
9176 another message has been requested in its place.
9177
9178 This is useful in situations where you need to redisplay but no
9179 user action has occurred, making it inappropriate for the message
9180 area to be cleared. See tracking_off and
9181 wait_reading_process_input for examples of these situations.
9182
9183 FROM_WHERE is an integer saying from where this function was
9184 called. This is useful for debugging. */
9185
9186 void
9187 redisplay_preserve_echo_area (from_where)
9188 int from_where;
9189 {
9190 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
9191
9192 if (!NILP (echo_area_buffer[1]))
9193 {
9194 /* We have a previously displayed message, but no current
9195 message. Redisplay the previous message. */
9196 display_last_displayed_message_p = 1;
9197 redisplay_internal (1);
9198 display_last_displayed_message_p = 0;
9199 }
9200 else
9201 redisplay_internal (1);
9202 }
9203
9204
9205 /* Function registered with record_unwind_protect in
9206 redisplay_internal. Reset redisplaying_p to the value it had
9207 before redisplay_internal was called, and clear
9208 prevent_freeing_realized_faces_p. */
9209
9210 static Lisp_Object
9211 unwind_redisplay (old_redisplaying_p)
9212 Lisp_Object old_redisplaying_p;
9213 {
9214 redisplaying_p = XFASTINT (old_redisplaying_p);
9215 return Qnil;
9216 }
9217
9218
9219 /* Mark the display of window W as accurate or inaccurate. If
9220 ACCURATE_P is non-zero mark display of W as accurate. If
9221 ACCURATE_P is zero, arrange for W to be redisplayed the next time
9222 redisplay_internal is called. */
9223
9224 static void
9225 mark_window_display_accurate_1 (w, accurate_p)
9226 struct window *w;
9227 int accurate_p;
9228 {
9229 if (BUFFERP (w->buffer))
9230 {
9231 struct buffer *b = XBUFFER (w->buffer);
9232
9233 w->last_modified
9234 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
9235 w->last_overlay_modified
9236 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
9237 w->last_had_star
9238 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
9239
9240 if (accurate_p)
9241 {
9242 b->clip_changed = 0;
9243 b->prevent_redisplay_optimizations_p = 0;
9244
9245 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
9246 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
9247 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
9248 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
9249
9250 w->current_matrix->buffer = b;
9251 w->current_matrix->begv = BUF_BEGV (b);
9252 w->current_matrix->zv = BUF_ZV (b);
9253
9254 w->last_cursor = w->cursor;
9255 w->last_cursor_off_p = w->cursor_off_p;
9256
9257 if (w == XWINDOW (selected_window))
9258 w->last_point = make_number (BUF_PT (b));
9259 else
9260 w->last_point = make_number (XMARKER (w->pointm)->charpos);
9261 }
9262 }
9263
9264 if (accurate_p)
9265 {
9266 w->window_end_valid = w->buffer;
9267 #if 0 /* This is incorrect with variable-height lines. */
9268 xassert (XINT (w->window_end_vpos)
9269 < (XINT (w->height)
9270 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
9271 #endif
9272 w->update_mode_line = Qnil;
9273 }
9274 }
9275
9276
9277 /* Mark the display of windows in the window tree rooted at WINDOW as
9278 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
9279 windows as accurate. If ACCURATE_P is zero, arrange for windows to
9280 be redisplayed the next time redisplay_internal is called. */
9281
9282 void
9283 mark_window_display_accurate (window, accurate_p)
9284 Lisp_Object window;
9285 int accurate_p;
9286 {
9287 struct window *w;
9288
9289 for (; !NILP (window); window = w->next)
9290 {
9291 w = XWINDOW (window);
9292 mark_window_display_accurate_1 (w, accurate_p);
9293
9294 if (!NILP (w->vchild))
9295 mark_window_display_accurate (w->vchild, accurate_p);
9296 if (!NILP (w->hchild))
9297 mark_window_display_accurate (w->hchild, accurate_p);
9298 }
9299
9300 if (accurate_p)
9301 {
9302 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
9303 last_arrow_string = Voverlay_arrow_string;
9304 }
9305 else
9306 {
9307 /* Force a thorough redisplay the next time by setting
9308 last_arrow_position and last_arrow_string to t, which is
9309 unequal to any useful value of Voverlay_arrow_... */
9310 last_arrow_position = Qt;
9311 last_arrow_string = Qt;
9312 }
9313 }
9314
9315
9316 /* Return value in display table DP (Lisp_Char_Table *) for character
9317 C. Since a display table doesn't have any parent, we don't have to
9318 follow parent. Do not call this function directly but use the
9319 macro DISP_CHAR_VECTOR. */
9320
9321 Lisp_Object
9322 disp_char_vector (dp, c)
9323 struct Lisp_Char_Table *dp;
9324 int c;
9325 {
9326 int code[4], i;
9327 Lisp_Object val;
9328
9329 if (SINGLE_BYTE_CHAR_P (c))
9330 return (dp->contents[c]);
9331
9332 SPLIT_CHAR (c, code[0], code[1], code[2]);
9333 if (code[1] < 32)
9334 code[1] = -1;
9335 else if (code[2] < 32)
9336 code[2] = -1;
9337
9338 /* Here, the possible range of code[0] (== charset ID) is
9339 128..max_charset. Since the top level char table contains data
9340 for multibyte characters after 256th element, we must increment
9341 code[0] by 128 to get a correct index. */
9342 code[0] += 128;
9343 code[3] = -1; /* anchor */
9344
9345 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
9346 {
9347 val = dp->contents[code[i]];
9348 if (!SUB_CHAR_TABLE_P (val))
9349 return (NILP (val) ? dp->defalt : val);
9350 }
9351
9352 /* Here, val is a sub char table. We return the default value of
9353 it. */
9354 return (dp->defalt);
9355 }
9356
9357
9358 \f
9359 /***********************************************************************
9360 Window Redisplay
9361 ***********************************************************************/
9362
9363 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
9364
9365 static void
9366 redisplay_windows (window)
9367 Lisp_Object window;
9368 {
9369 while (!NILP (window))
9370 {
9371 struct window *w = XWINDOW (window);
9372
9373 if (!NILP (w->hchild))
9374 redisplay_windows (w->hchild);
9375 else if (!NILP (w->vchild))
9376 redisplay_windows (w->vchild);
9377 else
9378 {
9379 displayed_buffer = XBUFFER (w->buffer);
9380 /* Use list_of_error, not Qerror, so that
9381 we catch only errors and don't run the debugger. */
9382 internal_condition_case_1 (redisplay_window_0, window,
9383 list_of_error,
9384 redisplay_window_error);
9385 }
9386
9387 window = w->next;
9388 }
9389 }
9390
9391 static Lisp_Object
9392 redisplay_window_error ()
9393 {
9394 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
9395 return Qnil;
9396 }
9397
9398 static Lisp_Object
9399 redisplay_window_0 (window)
9400 Lisp_Object window;
9401 {
9402 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
9403 redisplay_window (window, 0);
9404 return Qnil;
9405 }
9406
9407 static Lisp_Object
9408 redisplay_window_1 (window)
9409 Lisp_Object window;
9410 {
9411 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
9412 redisplay_window (window, 1);
9413 return Qnil;
9414 }
9415 \f
9416 /* Set cursor position of W. PT is assumed to be displayed in ROW.
9417 DELTA is the number of bytes by which positions recorded in ROW
9418 differ from current buffer positions. */
9419
9420 void
9421 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
9422 struct window *w;
9423 struct glyph_row *row;
9424 struct glyph_matrix *matrix;
9425 int delta, delta_bytes, dy, dvpos;
9426 {
9427 struct glyph *glyph = row->glyphs[TEXT_AREA];
9428 struct glyph *end = glyph + row->used[TEXT_AREA];
9429 int x = row->x;
9430 int pt_old = PT - delta;
9431
9432 /* Skip over glyphs not having an object at the start of the row.
9433 These are special glyphs like truncation marks on terminal
9434 frames. */
9435 if (row->displays_text_p)
9436 while (glyph < end
9437 && INTEGERP (glyph->object)
9438 && glyph->charpos < 0)
9439 {
9440 x += glyph->pixel_width;
9441 ++glyph;
9442 }
9443
9444 while (glyph < end
9445 && !INTEGERP (glyph->object)
9446 && (!BUFFERP (glyph->object)
9447 || glyph->charpos < pt_old))
9448 {
9449 x += glyph->pixel_width;
9450 ++glyph;
9451 }
9452
9453 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
9454 w->cursor.x = x;
9455 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
9456 w->cursor.y = row->y + dy;
9457
9458 if (w == XWINDOW (selected_window))
9459 {
9460 if (!row->continued_p
9461 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
9462 && row->x == 0)
9463 {
9464 this_line_buffer = XBUFFER (w->buffer);
9465
9466 CHARPOS (this_line_start_pos)
9467 = MATRIX_ROW_START_CHARPOS (row) + delta;
9468 BYTEPOS (this_line_start_pos)
9469 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
9470
9471 CHARPOS (this_line_end_pos)
9472 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
9473 BYTEPOS (this_line_end_pos)
9474 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
9475
9476 this_line_y = w->cursor.y;
9477 this_line_pixel_height = row->height;
9478 this_line_vpos = w->cursor.vpos;
9479 this_line_start_x = row->x;
9480 }
9481 else
9482 CHARPOS (this_line_start_pos) = 0;
9483 }
9484 }
9485
9486
9487 /* Run window scroll functions, if any, for WINDOW with new window
9488 start STARTP. Sets the window start of WINDOW to that position.
9489
9490 We assume that the window's buffer is really current. */
9491
9492 static INLINE struct text_pos
9493 run_window_scroll_functions (window, startp)
9494 Lisp_Object window;
9495 struct text_pos startp;
9496 {
9497 struct window *w = XWINDOW (window);
9498 SET_MARKER_FROM_TEXT_POS (w->start, startp);
9499
9500 if (current_buffer != XBUFFER (w->buffer))
9501 abort ();
9502
9503 if (!NILP (Vwindow_scroll_functions))
9504 {
9505 run_hook_with_args_2 (Qwindow_scroll_functions, window,
9506 make_number (CHARPOS (startp)));
9507 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9508 /* In case the hook functions switch buffers. */
9509 if (current_buffer != XBUFFER (w->buffer))
9510 set_buffer_internal_1 (XBUFFER (w->buffer));
9511 }
9512
9513 return startp;
9514 }
9515
9516
9517 /* Make sure the line containing the cursor is fully visible.
9518 A value of 1 means there is nothing to be done.
9519 (Either the line is fully visible, or it cannot be made so,
9520 or we cannot tell.)
9521 A value of 0 means the caller should do scrolling
9522 as if point had gone off the screen. */
9523
9524 static int
9525 make_cursor_line_fully_visible (w)
9526 struct window *w;
9527 {
9528 struct glyph_matrix *matrix;
9529 struct glyph_row *row;
9530 int window_height;
9531
9532 /* It's not always possible to find the cursor, e.g, when a window
9533 is full of overlay strings. Don't do anything in that case. */
9534 if (w->cursor.vpos < 0)
9535 return 1;
9536
9537 matrix = w->desired_matrix;
9538 row = MATRIX_ROW (matrix, w->cursor.vpos);
9539
9540 /* If the cursor row is not partially visible, there's nothing to do. */
9541 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
9542 return 1;
9543
9544 /* If the row the cursor is in is taller than the window's height,
9545 it's not clear what to do, so do nothing. */
9546 window_height = window_box_height (w);
9547 if (row->height >= window_height)
9548 return 1;
9549
9550 return 0;
9551
9552 #if 0
9553 /* This code used to try to scroll the window just enough to make
9554 the line visible. It returned 0 to say that the caller should
9555 allocate larger glyph matrices. */
9556
9557 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
9558 {
9559 int dy = row->height - row->visible_height;
9560 w->vscroll = 0;
9561 w->cursor.y += dy;
9562 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9563 }
9564 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
9565 {
9566 int dy = - (row->height - row->visible_height);
9567 w->vscroll = dy;
9568 w->cursor.y += dy;
9569 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9570 }
9571
9572 /* When we change the cursor y-position of the selected window,
9573 change this_line_y as well so that the display optimization for
9574 the cursor line of the selected window in redisplay_internal uses
9575 the correct y-position. */
9576 if (w == XWINDOW (selected_window))
9577 this_line_y = w->cursor.y;
9578
9579 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
9580 redisplay with larger matrices. */
9581 if (matrix->nrows < required_matrix_height (w))
9582 {
9583 fonts_changed_p = 1;
9584 return 0;
9585 }
9586
9587 return 1;
9588 #endif /* 0 */
9589 }
9590
9591
9592 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
9593 non-zero means only WINDOW is redisplayed in redisplay_internal.
9594 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
9595 in redisplay_window to bring a partially visible line into view in
9596 the case that only the cursor has moved.
9597
9598 Value is
9599
9600 1 if scrolling succeeded
9601
9602 0 if scrolling didn't find point.
9603
9604 -1 if new fonts have been loaded so that we must interrupt
9605 redisplay, adjust glyph matrices, and try again. */
9606
9607 enum
9608 {
9609 SCROLLING_SUCCESS,
9610 SCROLLING_FAILED,
9611 SCROLLING_NEED_LARGER_MATRICES
9612 };
9613
9614 static int
9615 try_scrolling (window, just_this_one_p, scroll_conservatively,
9616 scroll_step, temp_scroll_step)
9617 Lisp_Object window;
9618 int just_this_one_p;
9619 EMACS_INT scroll_conservatively, scroll_step;
9620 int temp_scroll_step;
9621 {
9622 struct window *w = XWINDOW (window);
9623 struct frame *f = XFRAME (w->frame);
9624 struct text_pos scroll_margin_pos;
9625 struct text_pos pos;
9626 struct text_pos startp;
9627 struct it it;
9628 Lisp_Object window_end;
9629 int this_scroll_margin;
9630 int dy = 0;
9631 int scroll_max;
9632 int rc;
9633 int amount_to_scroll = 0;
9634 Lisp_Object aggressive;
9635 int height;
9636
9637 #if GLYPH_DEBUG
9638 debug_method_add (w, "try_scrolling");
9639 #endif
9640
9641 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9642
9643 /* Compute scroll margin height in pixels. We scroll when point is
9644 within this distance from the top or bottom of the window. */
9645 if (scroll_margin > 0)
9646 {
9647 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
9648 this_scroll_margin *= CANON_Y_UNIT (f);
9649 }
9650 else
9651 this_scroll_margin = 0;
9652
9653 /* Compute how much we should try to scroll maximally to bring point
9654 into view. */
9655 if (scroll_step || scroll_conservatively || temp_scroll_step)
9656 scroll_max = max (scroll_step,
9657 max (scroll_conservatively, temp_scroll_step));
9658 else if (NUMBERP (current_buffer->scroll_down_aggressively)
9659 || NUMBERP (current_buffer->scroll_up_aggressively))
9660 /* We're trying to scroll because of aggressive scrolling
9661 but no scroll_step is set. Choose an arbitrary one. Maybe
9662 there should be a variable for this. */
9663 scroll_max = 10;
9664 else
9665 scroll_max = 0;
9666 scroll_max *= CANON_Y_UNIT (f);
9667
9668 /* Decide whether we have to scroll down. Start at the window end
9669 and move this_scroll_margin up to find the position of the scroll
9670 margin. */
9671 window_end = Fwindow_end (window, Qt);
9672 CHARPOS (scroll_margin_pos) = XINT (window_end);
9673 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
9674 if (this_scroll_margin)
9675 {
9676 start_display (&it, w, scroll_margin_pos);
9677 move_it_vertically (&it, - this_scroll_margin);
9678 scroll_margin_pos = it.current.pos;
9679 }
9680
9681 if (PT >= CHARPOS (scroll_margin_pos))
9682 {
9683 int y0;
9684
9685 too_near_end:
9686 /* Point is in the scroll margin at the bottom of the window, or
9687 below. Compute a new window start that makes point visible. */
9688
9689 /* Compute the distance from the scroll margin to PT.
9690 Give up if the distance is greater than scroll_max. */
9691 start_display (&it, w, scroll_margin_pos);
9692 y0 = it.current_y;
9693 move_it_to (&it, PT, 0, it.last_visible_y, -1,
9694 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9695
9696 /* To make point visible, we have to move the window start
9697 down so that the line the cursor is in is visible, which
9698 means we have to add in the height of the cursor line. */
9699 dy = line_bottom_y (&it) - y0;
9700
9701 if (dy > scroll_max)
9702 return SCROLLING_FAILED;
9703
9704 /* Move the window start down. If scrolling conservatively,
9705 move it just enough down to make point visible. If
9706 scroll_step is set, move it down by scroll_step. */
9707 start_display (&it, w, startp);
9708
9709 if (scroll_conservatively)
9710 amount_to_scroll
9711 = max (max (dy, CANON_Y_UNIT (f)),
9712 CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
9713 else if (scroll_step || temp_scroll_step)
9714 amount_to_scroll = scroll_max;
9715 else
9716 {
9717 aggressive = current_buffer->scroll_up_aggressively;
9718 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
9719 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
9720 if (NUMBERP (aggressive))
9721 amount_to_scroll = XFLOATINT (aggressive) * height;
9722 }
9723
9724 if (amount_to_scroll <= 0)
9725 return SCROLLING_FAILED;
9726
9727 move_it_vertically (&it, amount_to_scroll);
9728 startp = it.current.pos;
9729 }
9730 else
9731 {
9732 /* See if point is inside the scroll margin at the top of the
9733 window. */
9734 scroll_margin_pos = startp;
9735 if (this_scroll_margin)
9736 {
9737 start_display (&it, w, startp);
9738 move_it_vertically (&it, this_scroll_margin);
9739 scroll_margin_pos = it.current.pos;
9740 }
9741
9742 if (PT < CHARPOS (scroll_margin_pos))
9743 {
9744 /* Point is in the scroll margin at the top of the window or
9745 above what is displayed in the window. */
9746 int y0;
9747
9748 /* Compute the vertical distance from PT to the scroll
9749 margin position. Give up if distance is greater than
9750 scroll_max. */
9751 SET_TEXT_POS (pos, PT, PT_BYTE);
9752 start_display (&it, w, pos);
9753 y0 = it.current_y;
9754 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
9755 it.last_visible_y, -1,
9756 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9757 dy = it.current_y - y0;
9758 if (dy > scroll_max)
9759 return SCROLLING_FAILED;
9760
9761 /* Compute new window start. */
9762 start_display (&it, w, startp);
9763
9764 if (scroll_conservatively)
9765 amount_to_scroll =
9766 max (dy, CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
9767 else if (scroll_step || temp_scroll_step)
9768 amount_to_scroll = scroll_max;
9769 else
9770 {
9771 aggressive = current_buffer->scroll_down_aggressively;
9772 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
9773 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
9774 if (NUMBERP (aggressive))
9775 amount_to_scroll = XFLOATINT (aggressive) * height;
9776 }
9777
9778 if (amount_to_scroll <= 0)
9779 return SCROLLING_FAILED;
9780
9781 move_it_vertically (&it, - amount_to_scroll);
9782 startp = it.current.pos;
9783 }
9784 }
9785
9786 /* Run window scroll functions. */
9787 startp = run_window_scroll_functions (window, startp);
9788
9789 /* Display the window. Give up if new fonts are loaded, or if point
9790 doesn't appear. */
9791 if (!try_window (window, startp))
9792 rc = SCROLLING_NEED_LARGER_MATRICES;
9793 else if (w->cursor.vpos < 0)
9794 {
9795 clear_glyph_matrix (w->desired_matrix);
9796 rc = SCROLLING_FAILED;
9797 }
9798 else
9799 {
9800 /* Maybe forget recorded base line for line number display. */
9801 if (!just_this_one_p
9802 || current_buffer->clip_changed
9803 || BEG_UNCHANGED < CHARPOS (startp))
9804 w->base_line_number = Qnil;
9805
9806 /* If cursor ends up on a partially visible line,
9807 treat that as being off the bottom of the screen. */
9808 if (! make_cursor_line_fully_visible (w))
9809 {
9810 clear_glyph_matrix (w->desired_matrix);
9811 goto too_near_end;
9812 }
9813 rc = SCROLLING_SUCCESS;
9814 }
9815
9816 return rc;
9817 }
9818
9819
9820 /* Compute a suitable window start for window W if display of W starts
9821 on a continuation line. Value is non-zero if a new window start
9822 was computed.
9823
9824 The new window start will be computed, based on W's width, starting
9825 from the start of the continued line. It is the start of the
9826 screen line with the minimum distance from the old start W->start. */
9827
9828 static int
9829 compute_window_start_on_continuation_line (w)
9830 struct window *w;
9831 {
9832 struct text_pos pos, start_pos;
9833 int window_start_changed_p = 0;
9834
9835 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
9836
9837 /* If window start is on a continuation line... Window start may be
9838 < BEGV in case there's invisible text at the start of the
9839 buffer (M-x rmail, for example). */
9840 if (CHARPOS (start_pos) > BEGV
9841 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
9842 {
9843 struct it it;
9844 struct glyph_row *row;
9845
9846 /* Handle the case that the window start is out of range. */
9847 if (CHARPOS (start_pos) < BEGV)
9848 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
9849 else if (CHARPOS (start_pos) > ZV)
9850 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
9851
9852 /* Find the start of the continued line. This should be fast
9853 because scan_buffer is fast (newline cache). */
9854 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
9855 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
9856 row, DEFAULT_FACE_ID);
9857 reseat_at_previous_visible_line_start (&it);
9858
9859 /* If the line start is "too far" away from the window start,
9860 say it takes too much time to compute a new window start. */
9861 if (CHARPOS (start_pos) - IT_CHARPOS (it)
9862 < XFASTINT (w->height) * XFASTINT (w->width))
9863 {
9864 int min_distance, distance;
9865
9866 /* Move forward by display lines to find the new window
9867 start. If window width was enlarged, the new start can
9868 be expected to be > the old start. If window width was
9869 decreased, the new window start will be < the old start.
9870 So, we're looking for the display line start with the
9871 minimum distance from the old window start. */
9872 pos = it.current.pos;
9873 min_distance = INFINITY;
9874 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
9875 distance < min_distance)
9876 {
9877 min_distance = distance;
9878 pos = it.current.pos;
9879 move_it_by_lines (&it, 1, 0);
9880 }
9881
9882 /* Set the window start there. */
9883 SET_MARKER_FROM_TEXT_POS (w->start, pos);
9884 window_start_changed_p = 1;
9885 }
9886 }
9887
9888 return window_start_changed_p;
9889 }
9890
9891
9892 /* Try cursor movement in case text has not changed in window WINDOW,
9893 with window start STARTP. Value is
9894
9895 CURSOR_MOVEMENT_SUCCESS if successful
9896
9897 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
9898
9899 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
9900 display. *SCROLL_STEP is set to 1, under certain circumstances, if
9901 we want to scroll as if scroll-step were set to 1. See the code.
9902
9903 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
9904 which case we have to abort this redisplay, and adjust matrices
9905 first. */
9906
9907 enum
9908 {
9909 CURSOR_MOVEMENT_SUCCESS,
9910 CURSOR_MOVEMENT_CANNOT_BE_USED,
9911 CURSOR_MOVEMENT_MUST_SCROLL,
9912 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
9913 };
9914
9915 static int
9916 try_cursor_movement (window, startp, scroll_step)
9917 Lisp_Object window;
9918 struct text_pos startp;
9919 int *scroll_step;
9920 {
9921 struct window *w = XWINDOW (window);
9922 struct frame *f = XFRAME (w->frame);
9923 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
9924
9925 #if GLYPH_DEBUG
9926 if (inhibit_try_cursor_movement)
9927 return rc;
9928 #endif
9929
9930 /* Handle case where text has not changed, only point, and it has
9931 not moved off the frame. */
9932 if (/* Point may be in this window. */
9933 PT >= CHARPOS (startp)
9934 /* Selective display hasn't changed. */
9935 && !current_buffer->clip_changed
9936 /* Function force-mode-line-update is used to force a thorough
9937 redisplay. It sets either windows_or_buffers_changed or
9938 update_mode_lines. So don't take a shortcut here for these
9939 cases. */
9940 && !update_mode_lines
9941 && !windows_or_buffers_changed
9942 && !cursor_type_changed
9943 /* Can't use this case if highlighting a region. When a
9944 region exists, cursor movement has to do more than just
9945 set the cursor. */
9946 && !(!NILP (Vtransient_mark_mode)
9947 && !NILP (current_buffer->mark_active))
9948 && NILP (w->region_showing)
9949 && NILP (Vshow_trailing_whitespace)
9950 /* Right after splitting windows, last_point may be nil. */
9951 && INTEGERP (w->last_point)
9952 /* This code is not used for mini-buffer for the sake of the case
9953 of redisplaying to replace an echo area message; since in
9954 that case the mini-buffer contents per se are usually
9955 unchanged. This code is of no real use in the mini-buffer
9956 since the handling of this_line_start_pos, etc., in redisplay
9957 handles the same cases. */
9958 && !EQ (window, minibuf_window)
9959 /* When splitting windows or for new windows, it happens that
9960 redisplay is called with a nil window_end_vpos or one being
9961 larger than the window. This should really be fixed in
9962 window.c. I don't have this on my list, now, so we do
9963 approximately the same as the old redisplay code. --gerd. */
9964 && INTEGERP (w->window_end_vpos)
9965 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
9966 && (FRAME_WINDOW_P (f)
9967 || !MARKERP (Voverlay_arrow_position)
9968 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
9969 {
9970 int this_scroll_margin;
9971 struct glyph_row *row = NULL;
9972
9973 #if GLYPH_DEBUG
9974 debug_method_add (w, "cursor movement");
9975 #endif
9976
9977 /* Scroll if point within this distance from the top or bottom
9978 of the window. This is a pixel value. */
9979 this_scroll_margin = max (0, scroll_margin);
9980 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
9981 this_scroll_margin *= CANON_Y_UNIT (f);
9982
9983 /* Start with the row the cursor was displayed during the last
9984 not paused redisplay. Give up if that row is not valid. */
9985 if (w->last_cursor.vpos < 0
9986 || w->last_cursor.vpos >= w->current_matrix->nrows)
9987 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9988 else
9989 {
9990 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
9991 if (row->mode_line_p)
9992 ++row;
9993 if (!row->enabled_p)
9994 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9995 }
9996
9997 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
9998 {
9999 int scroll_p = 0;
10000 int last_y = window_text_bottom_y (w) - this_scroll_margin;
10001
10002 if (PT > XFASTINT (w->last_point))
10003 {
10004 /* Point has moved forward. */
10005 while (MATRIX_ROW_END_CHARPOS (row) < PT
10006 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
10007 {
10008 xassert (row->enabled_p);
10009 ++row;
10010 }
10011
10012 /* The end position of a row equals the start position
10013 of the next row. If PT is there, we would rather
10014 display it in the next line. */
10015 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
10016 && MATRIX_ROW_END_CHARPOS (row) == PT
10017 && !cursor_row_p (w, row))
10018 ++row;
10019
10020 /* If within the scroll margin, scroll. Note that
10021 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
10022 the next line would be drawn, and that
10023 this_scroll_margin can be zero. */
10024 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
10025 || PT > MATRIX_ROW_END_CHARPOS (row)
10026 /* Line is completely visible last line in window
10027 and PT is to be set in the next line. */
10028 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
10029 && PT == MATRIX_ROW_END_CHARPOS (row)
10030 && !row->ends_at_zv_p
10031 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
10032 scroll_p = 1;
10033 }
10034 else if (PT < XFASTINT (w->last_point))
10035 {
10036 /* Cursor has to be moved backward. Note that PT >=
10037 CHARPOS (startp) because of the outer
10038 if-statement. */
10039 while (!row->mode_line_p
10040 && (MATRIX_ROW_START_CHARPOS (row) > PT
10041 || (MATRIX_ROW_START_CHARPOS (row) == PT
10042 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
10043 && (row->y > this_scroll_margin
10044 || CHARPOS (startp) == BEGV))
10045 {
10046 xassert (row->enabled_p);
10047 --row;
10048 }
10049
10050 /* Consider the following case: Window starts at BEGV,
10051 there is invisible, intangible text at BEGV, so that
10052 display starts at some point START > BEGV. It can
10053 happen that we are called with PT somewhere between
10054 BEGV and START. Try to handle that case. */
10055 if (row < w->current_matrix->rows
10056 || row->mode_line_p)
10057 {
10058 row = w->current_matrix->rows;
10059 if (row->mode_line_p)
10060 ++row;
10061 }
10062
10063 /* Due to newlines in overlay strings, we may have to
10064 skip forward over overlay strings. */
10065 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
10066 && MATRIX_ROW_END_CHARPOS (row) == PT
10067 && !cursor_row_p (w, row))
10068 ++row;
10069
10070 /* If within the scroll margin, scroll. */
10071 if (row->y < this_scroll_margin
10072 && CHARPOS (startp) != BEGV)
10073 scroll_p = 1;
10074 }
10075
10076 if (PT < MATRIX_ROW_START_CHARPOS (row)
10077 || PT > MATRIX_ROW_END_CHARPOS (row))
10078 {
10079 /* if PT is not in the glyph row, give up. */
10080 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10081 }
10082 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
10083 {
10084 if (PT == MATRIX_ROW_END_CHARPOS (row)
10085 && !row->ends_at_zv_p
10086 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
10087 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10088 else if (row->height > window_box_height (w))
10089 {
10090 /* If we end up in a partially visible line, let's
10091 make it fully visible, except when it's taller
10092 than the window, in which case we can't do much
10093 about it. */
10094 *scroll_step = 1;
10095 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10096 }
10097 else
10098 {
10099 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10100 try_window (window, startp);
10101 if (!make_cursor_line_fully_visible (w))
10102 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10103 else
10104 rc = CURSOR_MOVEMENT_SUCCESS;
10105 }
10106 }
10107 else if (scroll_p)
10108 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10109 else
10110 {
10111 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10112 rc = CURSOR_MOVEMENT_SUCCESS;
10113 }
10114 }
10115 }
10116
10117 return rc;
10118 }
10119
10120
10121 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
10122 selected_window is redisplayed.
10123
10124 We can return without actually redisplaying the window if
10125 fonts_changed_p is nonzero. In that case, redisplay_internal will
10126 retry. */
10127
10128 static void
10129 redisplay_window (window, just_this_one_p)
10130 Lisp_Object window;
10131 int just_this_one_p;
10132 {
10133 struct window *w = XWINDOW (window);
10134 struct frame *f = XFRAME (w->frame);
10135 struct buffer *buffer = XBUFFER (w->buffer);
10136 struct buffer *old = current_buffer;
10137 struct text_pos lpoint, opoint, startp;
10138 int update_mode_line;
10139 int tem;
10140 struct it it;
10141 /* Record it now because it's overwritten. */
10142 int current_matrix_up_to_date_p = 0;
10143 /* This is less strict than current_matrix_up_to_date_p.
10144 It indictes that the buffer contents and narrowing are unchanged. */
10145 int buffer_unchanged_p = 0;
10146 int temp_scroll_step = 0;
10147 int count = SPECPDL_INDEX ();
10148 int rc;
10149 int centering_position;
10150
10151 SET_TEXT_POS (lpoint, PT, PT_BYTE);
10152 opoint = lpoint;
10153
10154 /* W must be a leaf window here. */
10155 xassert (!NILP (w->buffer));
10156 #if GLYPH_DEBUG
10157 *w->desired_matrix->method = 0;
10158 #endif
10159
10160 specbind (Qinhibit_point_motion_hooks, Qt);
10161
10162 reconsider_clip_changes (w, buffer);
10163
10164 /* Has the mode line to be updated? */
10165 update_mode_line = (!NILP (w->update_mode_line)
10166 || update_mode_lines
10167 || buffer->clip_changed
10168 || buffer->prevent_redisplay_optimizations_p);
10169
10170 if (MINI_WINDOW_P (w))
10171 {
10172 if (w == XWINDOW (echo_area_window)
10173 && !NILP (echo_area_buffer[0]))
10174 {
10175 if (update_mode_line)
10176 /* We may have to update a tty frame's menu bar or a
10177 tool-bar. Example `M-x C-h C-h C-g'. */
10178 goto finish_menu_bars;
10179 else
10180 /* We've already displayed the echo area glyphs in this window. */
10181 goto finish_scroll_bars;
10182 }
10183 else if (w != XWINDOW (minibuf_window))
10184 {
10185 /* W is a mini-buffer window, but it's not the currently
10186 active one, so clear it. */
10187 int yb = window_text_bottom_y (w);
10188 struct glyph_row *row;
10189 int y;
10190
10191 for (y = 0, row = w->desired_matrix->rows;
10192 y < yb;
10193 y += row->height, ++row)
10194 blank_row (w, row, y);
10195 goto finish_scroll_bars;
10196 }
10197
10198 clear_glyph_matrix (w->desired_matrix);
10199 }
10200
10201 /* Otherwise set up data on this window; select its buffer and point
10202 value. */
10203 /* Really select the buffer, for the sake of buffer-local
10204 variables. */
10205 set_buffer_internal_1 (XBUFFER (w->buffer));
10206 SET_TEXT_POS (opoint, PT, PT_BYTE);
10207
10208 current_matrix_up_to_date_p
10209 = (!NILP (w->window_end_valid)
10210 && !current_buffer->clip_changed
10211 && !current_buffer->prevent_redisplay_optimizations_p
10212 && XFASTINT (w->last_modified) >= MODIFF
10213 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
10214
10215 buffer_unchanged_p
10216 = (!NILP (w->window_end_valid)
10217 && !current_buffer->clip_changed
10218 && XFASTINT (w->last_modified) >= MODIFF
10219 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
10220
10221 /* When windows_or_buffers_changed is non-zero, we can't rely on
10222 the window end being valid, so set it to nil there. */
10223 if (windows_or_buffers_changed)
10224 {
10225 /* If window starts on a continuation line, maybe adjust the
10226 window start in case the window's width changed. */
10227 if (XMARKER (w->start)->buffer == current_buffer)
10228 compute_window_start_on_continuation_line (w);
10229
10230 w->window_end_valid = Qnil;
10231 }
10232
10233 /* Some sanity checks. */
10234 CHECK_WINDOW_END (w);
10235 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
10236 abort ();
10237 if (BYTEPOS (opoint) < CHARPOS (opoint))
10238 abort ();
10239
10240 /* If %c is in mode line, update it if needed. */
10241 if (!NILP (w->column_number_displayed)
10242 /* This alternative quickly identifies a common case
10243 where no change is needed. */
10244 && !(PT == XFASTINT (w->last_point)
10245 && XFASTINT (w->last_modified) >= MODIFF
10246 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
10247 && (XFASTINT (w->column_number_displayed)
10248 != (int) current_column ())) /* iftc */
10249 update_mode_line = 1;
10250
10251 /* Count number of windows showing the selected buffer. An indirect
10252 buffer counts as its base buffer. */
10253 if (!just_this_one_p)
10254 {
10255 struct buffer *current_base, *window_base;
10256 current_base = current_buffer;
10257 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
10258 if (current_base->base_buffer)
10259 current_base = current_base->base_buffer;
10260 if (window_base->base_buffer)
10261 window_base = window_base->base_buffer;
10262 if (current_base == window_base)
10263 buffer_shared++;
10264 }
10265
10266 /* Point refers normally to the selected window. For any other
10267 window, set up appropriate value. */
10268 if (!EQ (window, selected_window))
10269 {
10270 int new_pt = XMARKER (w->pointm)->charpos;
10271 int new_pt_byte = marker_byte_position (w->pointm);
10272 if (new_pt < BEGV)
10273 {
10274 new_pt = BEGV;
10275 new_pt_byte = BEGV_BYTE;
10276 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
10277 }
10278 else if (new_pt > (ZV - 1))
10279 {
10280 new_pt = ZV;
10281 new_pt_byte = ZV_BYTE;
10282 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
10283 }
10284
10285 /* We don't use SET_PT so that the point-motion hooks don't run. */
10286 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
10287 }
10288
10289 /* If any of the character widths specified in the display table
10290 have changed, invalidate the width run cache. It's true that
10291 this may be a bit late to catch such changes, but the rest of
10292 redisplay goes (non-fatally) haywire when the display table is
10293 changed, so why should we worry about doing any better? */
10294 if (current_buffer->width_run_cache)
10295 {
10296 struct Lisp_Char_Table *disptab = buffer_display_table ();
10297
10298 if (! disptab_matches_widthtab (disptab,
10299 XVECTOR (current_buffer->width_table)))
10300 {
10301 invalidate_region_cache (current_buffer,
10302 current_buffer->width_run_cache,
10303 BEG, Z);
10304 recompute_width_table (current_buffer, disptab);
10305 }
10306 }
10307
10308 /* If window-start is screwed up, choose a new one. */
10309 if (XMARKER (w->start)->buffer != current_buffer)
10310 goto recenter;
10311
10312 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10313
10314 /* If someone specified a new starting point but did not insist,
10315 check whether it can be used. */
10316 if (!NILP (w->optional_new_start)
10317 && CHARPOS (startp) >= BEGV
10318 && CHARPOS (startp) <= ZV)
10319 {
10320 w->optional_new_start = Qnil;
10321 start_display (&it, w, startp);
10322 move_it_to (&it, PT, 0, it.last_visible_y, -1,
10323 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
10324 if (IT_CHARPOS (it) == PT)
10325 w->force_start = Qt;
10326 }
10327
10328 /* Handle case where place to start displaying has been specified,
10329 unless the specified location is outside the accessible range. */
10330 if (!NILP (w->force_start)
10331 || w->frozen_window_start_p)
10332 {
10333 /* We set this later on if we have to adjust point. */
10334 int new_vpos = -1;
10335
10336 w->force_start = Qnil;
10337 w->vscroll = 0;
10338 w->window_end_valid = Qnil;
10339
10340 /* Forget any recorded base line for line number display. */
10341 if (!buffer_unchanged_p)
10342 w->base_line_number = Qnil;
10343
10344 /* Redisplay the mode line. Select the buffer properly for that.
10345 Also, run the hook window-scroll-functions
10346 because we have scrolled. */
10347 /* Note, we do this after clearing force_start because
10348 if there's an error, it is better to forget about force_start
10349 than to get into an infinite loop calling the hook functions
10350 and having them get more errors. */
10351 if (!update_mode_line
10352 || ! NILP (Vwindow_scroll_functions))
10353 {
10354 update_mode_line = 1;
10355 w->update_mode_line = Qt;
10356 startp = run_window_scroll_functions (window, startp);
10357 }
10358
10359 w->last_modified = make_number (0);
10360 w->last_overlay_modified = make_number (0);
10361 if (CHARPOS (startp) < BEGV)
10362 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
10363 else if (CHARPOS (startp) > ZV)
10364 SET_TEXT_POS (startp, ZV, ZV_BYTE);
10365
10366 /* Redisplay, then check if cursor has been set during the
10367 redisplay. Give up if new fonts were loaded. */
10368 if (!try_window (window, startp))
10369 {
10370 w->force_start = Qt;
10371 clear_glyph_matrix (w->desired_matrix);
10372 goto need_larger_matrices;
10373 }
10374
10375 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
10376 {
10377 /* If point does not appear, try to move point so it does
10378 appear. The desired matrix has been built above, so we
10379 can use it here. */
10380 new_vpos = window_box_height (w) / 2;
10381 }
10382
10383 if (!make_cursor_line_fully_visible (w))
10384 {
10385 /* Point does appear, but on a line partly visible at end of window.
10386 Move it back to a fully-visible line. */
10387 new_vpos = window_box_height (w);
10388 }
10389
10390 /* If we need to move point for either of the above reasons,
10391 now actually do it. */
10392 if (new_vpos >= 0)
10393 {
10394 struct glyph_row *row;
10395
10396 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
10397 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
10398 ++row;
10399
10400 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
10401 MATRIX_ROW_START_BYTEPOS (row));
10402
10403 if (w != XWINDOW (selected_window))
10404 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
10405 else if (current_buffer == old)
10406 SET_TEXT_POS (lpoint, PT, PT_BYTE);
10407
10408 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
10409
10410 /* If we are highlighting the region, then we just changed
10411 the region, so redisplay to show it. */
10412 if (!NILP (Vtransient_mark_mode)
10413 && !NILP (current_buffer->mark_active))
10414 {
10415 clear_glyph_matrix (w->desired_matrix);
10416 if (!try_window (window, startp))
10417 goto need_larger_matrices;
10418 }
10419 }
10420
10421 #if GLYPH_DEBUG
10422 debug_method_add (w, "forced window start");
10423 #endif
10424 goto done;
10425 }
10426
10427 /* Handle case where text has not changed, only point, and it has
10428 not moved off the frame, and we are not retrying after hscroll.
10429 (current_matrix_up_to_date_p is nonzero when retrying.) */
10430 if (current_matrix_up_to_date_p
10431 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
10432 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
10433 {
10434 switch (rc)
10435 {
10436 case CURSOR_MOVEMENT_SUCCESS:
10437 goto done;
10438
10439 #if 0 /* try_cursor_movement never returns this value. */
10440 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
10441 goto need_larger_matrices;
10442 #endif
10443
10444 case CURSOR_MOVEMENT_MUST_SCROLL:
10445 goto try_to_scroll;
10446
10447 default:
10448 abort ();
10449 }
10450 }
10451 /* If current starting point was originally the beginning of a line
10452 but no longer is, find a new starting point. */
10453 else if (!NILP (w->start_at_line_beg)
10454 && !(CHARPOS (startp) <= BEGV
10455 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
10456 {
10457 #if GLYPH_DEBUG
10458 debug_method_add (w, "recenter 1");
10459 #endif
10460 goto recenter;
10461 }
10462
10463 /* Try scrolling with try_window_id. Value is > 0 if update has
10464 been done, it is -1 if we know that the same window start will
10465 not work. It is 0 if unsuccessful for some other reason. */
10466 else if ((tem = try_window_id (w)) != 0)
10467 {
10468 #if GLYPH_DEBUG
10469 debug_method_add (w, "try_window_id %d", tem);
10470 #endif
10471
10472 if (fonts_changed_p)
10473 goto need_larger_matrices;
10474 if (tem > 0)
10475 goto done;
10476
10477 /* Otherwise try_window_id has returned -1 which means that we
10478 don't want the alternative below this comment to execute. */
10479 }
10480 else if (CHARPOS (startp) >= BEGV
10481 && CHARPOS (startp) <= ZV
10482 && PT >= CHARPOS (startp)
10483 && (CHARPOS (startp) < ZV
10484 /* Avoid starting at end of buffer. */
10485 || CHARPOS (startp) == BEGV
10486 || (XFASTINT (w->last_modified) >= MODIFF
10487 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
10488 {
10489 #if GLYPH_DEBUG
10490 debug_method_add (w, "same window start");
10491 #endif
10492
10493 /* Try to redisplay starting at same place as before.
10494 If point has not moved off frame, accept the results. */
10495 if (!current_matrix_up_to_date_p
10496 /* Don't use try_window_reusing_current_matrix in this case
10497 because a window scroll function can have changed the
10498 buffer. */
10499 || !NILP (Vwindow_scroll_functions)
10500 || MINI_WINDOW_P (w)
10501 || !try_window_reusing_current_matrix (w))
10502 {
10503 IF_DEBUG (debug_method_add (w, "1"));
10504 try_window (window, startp);
10505 }
10506
10507 if (fonts_changed_p)
10508 goto need_larger_matrices;
10509
10510 if (w->cursor.vpos >= 0)
10511 {
10512 if (!just_this_one_p
10513 || current_buffer->clip_changed
10514 || BEG_UNCHANGED < CHARPOS (startp))
10515 /* Forget any recorded base line for line number display. */
10516 w->base_line_number = Qnil;
10517
10518 if (!make_cursor_line_fully_visible (w))
10519 clear_glyph_matrix (w->desired_matrix);
10520 /* Drop through and scroll. */
10521 else
10522 goto done;
10523 }
10524 else
10525 clear_glyph_matrix (w->desired_matrix);
10526 }
10527
10528 try_to_scroll:
10529
10530 w->last_modified = make_number (0);
10531 w->last_overlay_modified = make_number (0);
10532
10533 /* Redisplay the mode line. Select the buffer properly for that. */
10534 if (!update_mode_line)
10535 {
10536 update_mode_line = 1;
10537 w->update_mode_line = Qt;
10538 }
10539
10540 /* Try to scroll by specified few lines. */
10541 if ((scroll_conservatively
10542 || scroll_step
10543 || temp_scroll_step
10544 || NUMBERP (current_buffer->scroll_up_aggressively)
10545 || NUMBERP (current_buffer->scroll_down_aggressively))
10546 && !current_buffer->clip_changed
10547 && CHARPOS (startp) >= BEGV
10548 && CHARPOS (startp) <= ZV)
10549 {
10550 /* The function returns -1 if new fonts were loaded, 1 if
10551 successful, 0 if not successful. */
10552 int rc = try_scrolling (window, just_this_one_p,
10553 scroll_conservatively,
10554 scroll_step,
10555 temp_scroll_step);
10556 switch (rc)
10557 {
10558 case SCROLLING_SUCCESS:
10559 goto done;
10560
10561 case SCROLLING_NEED_LARGER_MATRICES:
10562 goto need_larger_matrices;
10563
10564 case SCROLLING_FAILED:
10565 break;
10566
10567 default:
10568 abort ();
10569 }
10570 }
10571
10572 /* Finally, just choose place to start which centers point */
10573
10574 recenter:
10575 centering_position = window_box_height (w) / 2;
10576
10577 point_at_top:
10578 /* Jump here with centering_position already set to 0. */
10579
10580 #if GLYPH_DEBUG
10581 debug_method_add (w, "recenter");
10582 #endif
10583
10584 /* w->vscroll = 0; */
10585
10586 /* Forget any previously recorded base line for line number display. */
10587 if (!buffer_unchanged_p)
10588 w->base_line_number = Qnil;
10589
10590 /* Move backward half the height of the window. */
10591 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10592 it.current_y = it.last_visible_y;
10593 move_it_vertically_backward (&it, centering_position);
10594 xassert (IT_CHARPOS (it) >= BEGV);
10595
10596 /* The function move_it_vertically_backward may move over more
10597 than the specified y-distance. If it->w is small, e.g. a
10598 mini-buffer window, we may end up in front of the window's
10599 display area. Start displaying at the start of the line
10600 containing PT in this case. */
10601 if (it.current_y <= 0)
10602 {
10603 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10604 move_it_vertically (&it, 0);
10605 xassert (IT_CHARPOS (it) <= PT);
10606 it.current_y = 0;
10607 }
10608
10609 it.current_x = it.hpos = 0;
10610
10611 /* Set startp here explicitly in case that helps avoid an infinite loop
10612 in case the window-scroll-functions functions get errors. */
10613 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
10614
10615 /* Run scroll hooks. */
10616 startp = run_window_scroll_functions (window, it.current.pos);
10617
10618 /* Redisplay the window. */
10619 if (!current_matrix_up_to_date_p
10620 || windows_or_buffers_changed
10621 || cursor_type_changed
10622 /* Don't use try_window_reusing_current_matrix in this case
10623 because it can have changed the buffer. */
10624 || !NILP (Vwindow_scroll_functions)
10625 || !just_this_one_p
10626 || MINI_WINDOW_P (w)
10627 || !try_window_reusing_current_matrix (w))
10628 try_window (window, startp);
10629
10630 /* If new fonts have been loaded (due to fontsets), give up. We
10631 have to start a new redisplay since we need to re-adjust glyph
10632 matrices. */
10633 if (fonts_changed_p)
10634 goto need_larger_matrices;
10635
10636 /* If cursor did not appear assume that the middle of the window is
10637 in the first line of the window. Do it again with the next line.
10638 (Imagine a window of height 100, displaying two lines of height
10639 60. Moving back 50 from it->last_visible_y will end in the first
10640 line.) */
10641 if (w->cursor.vpos < 0)
10642 {
10643 if (!NILP (w->window_end_valid)
10644 && PT >= Z - XFASTINT (w->window_end_pos))
10645 {
10646 clear_glyph_matrix (w->desired_matrix);
10647 move_it_by_lines (&it, 1, 0);
10648 try_window (window, it.current.pos);
10649 }
10650 else if (PT < IT_CHARPOS (it))
10651 {
10652 clear_glyph_matrix (w->desired_matrix);
10653 move_it_by_lines (&it, -1, 0);
10654 try_window (window, it.current.pos);
10655 }
10656 else
10657 {
10658 /* Not much we can do about it. */
10659 }
10660 }
10661
10662 /* Consider the following case: Window starts at BEGV, there is
10663 invisible, intangible text at BEGV, so that display starts at
10664 some point START > BEGV. It can happen that we are called with
10665 PT somewhere between BEGV and START. Try to handle that case. */
10666 if (w->cursor.vpos < 0)
10667 {
10668 struct glyph_row *row = w->current_matrix->rows;
10669 if (row->mode_line_p)
10670 ++row;
10671 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10672 }
10673
10674 if (!make_cursor_line_fully_visible (w))
10675 {
10676 /* If centering point failed to make the whole line visible,
10677 put point at the top instead. That has to make the whole line
10678 visible, if it can be done. */
10679 centering_position = 0;
10680 goto point_at_top;
10681 }
10682
10683 done:
10684
10685 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10686 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
10687 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
10688 ? Qt : Qnil);
10689
10690 /* Display the mode line, if we must. */
10691 if ((update_mode_line
10692 /* If window not full width, must redo its mode line
10693 if (a) the window to its side is being redone and
10694 (b) we do a frame-based redisplay. This is a consequence
10695 of how inverted lines are drawn in frame-based redisplay. */
10696 || (!just_this_one_p
10697 && !FRAME_WINDOW_P (f)
10698 && !WINDOW_FULL_WIDTH_P (w))
10699 /* Line number to display. */
10700 || INTEGERP (w->base_line_pos)
10701 /* Column number is displayed and different from the one displayed. */
10702 || (!NILP (w->column_number_displayed)
10703 && (XFASTINT (w->column_number_displayed)
10704 != (int) current_column ()))) /* iftc */
10705 /* This means that the window has a mode line. */
10706 && (WINDOW_WANTS_MODELINE_P (w)
10707 || WINDOW_WANTS_HEADER_LINE_P (w)))
10708 {
10709 display_mode_lines (w);
10710
10711 /* If mode line height has changed, arrange for a thorough
10712 immediate redisplay using the correct mode line height. */
10713 if (WINDOW_WANTS_MODELINE_P (w)
10714 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
10715 {
10716 fonts_changed_p = 1;
10717 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
10718 = DESIRED_MODE_LINE_HEIGHT (w);
10719 }
10720
10721 /* If top line height has changed, arrange for a thorough
10722 immediate redisplay using the correct mode line height. */
10723 if (WINDOW_WANTS_HEADER_LINE_P (w)
10724 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
10725 {
10726 fonts_changed_p = 1;
10727 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
10728 = DESIRED_HEADER_LINE_HEIGHT (w);
10729 }
10730
10731 if (fonts_changed_p)
10732 goto need_larger_matrices;
10733 }
10734
10735 if (!line_number_displayed
10736 && !BUFFERP (w->base_line_pos))
10737 {
10738 w->base_line_pos = Qnil;
10739 w->base_line_number = Qnil;
10740 }
10741
10742 finish_menu_bars:
10743
10744 /* When we reach a frame's selected window, redo the frame's menu bar. */
10745 if (update_mode_line
10746 && EQ (FRAME_SELECTED_WINDOW (f), window))
10747 {
10748 int redisplay_menu_p = 0;
10749
10750 if (FRAME_WINDOW_P (f))
10751 {
10752 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS)
10753 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
10754 #else
10755 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10756 #endif
10757 }
10758 else
10759 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10760
10761 if (redisplay_menu_p)
10762 display_menu_bar (w);
10763
10764 #ifdef HAVE_WINDOW_SYSTEM
10765 if (WINDOWP (f->tool_bar_window)
10766 && (FRAME_TOOL_BAR_LINES (f) > 0
10767 || auto_resize_tool_bars_p))
10768 redisplay_tool_bar (f);
10769 #endif
10770 }
10771
10772 /* We go to this label, with fonts_changed_p nonzero,
10773 if it is necessary to try again using larger glyph matrices.
10774 We have to redeem the scroll bar even in this case,
10775 because the loop in redisplay_internal expects that. */
10776 need_larger_matrices:
10777 ;
10778 finish_scroll_bars:
10779
10780 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
10781 {
10782 int start, end, whole;
10783
10784 /* Calculate the start and end positions for the current window.
10785 At some point, it would be nice to choose between scrollbars
10786 which reflect the whole buffer size, with special markers
10787 indicating narrowing, and scrollbars which reflect only the
10788 visible region.
10789
10790 Note that mini-buffers sometimes aren't displaying any text. */
10791 if (!MINI_WINDOW_P (w)
10792 || (w == XWINDOW (minibuf_window)
10793 && NILP (echo_area_buffer[0])))
10794 {
10795 whole = ZV - BEGV;
10796 start = marker_position (w->start) - BEGV;
10797 /* I don't think this is guaranteed to be right. For the
10798 moment, we'll pretend it is. */
10799 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
10800
10801 if (end < start)
10802 end = start;
10803 if (whole < (end - start))
10804 whole = end - start;
10805 }
10806 else
10807 start = end = whole = 0;
10808
10809 /* Indicate what this scroll bar ought to be displaying now. */
10810 set_vertical_scroll_bar_hook (w, end - start, whole, start);
10811
10812 /* Note that we actually used the scroll bar attached to this
10813 window, so it shouldn't be deleted at the end of redisplay. */
10814 redeem_scroll_bar_hook (w);
10815 }
10816
10817 /* Restore current_buffer and value of point in it. */
10818 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
10819 set_buffer_internal_1 (old);
10820 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
10821
10822 unbind_to (count, Qnil);
10823 }
10824
10825
10826 /* Build the complete desired matrix of WINDOW with a window start
10827 buffer position POS. Value is non-zero if successful. It is zero
10828 if fonts were loaded during redisplay which makes re-adjusting
10829 glyph matrices necessary. */
10830
10831 int
10832 try_window (window, pos)
10833 Lisp_Object window;
10834 struct text_pos pos;
10835 {
10836 struct window *w = XWINDOW (window);
10837 struct it it;
10838 struct glyph_row *last_text_row = NULL;
10839
10840 /* Make POS the new window start. */
10841 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
10842
10843 /* Mark cursor position as unknown. No overlay arrow seen. */
10844 w->cursor.vpos = -1;
10845 overlay_arrow_seen = 0;
10846
10847 /* Initialize iterator and info to start at POS. */
10848 start_display (&it, w, pos);
10849
10850 /* Display all lines of W. */
10851 while (it.current_y < it.last_visible_y)
10852 {
10853 if (display_line (&it))
10854 last_text_row = it.glyph_row - 1;
10855 if (fonts_changed_p)
10856 return 0;
10857 }
10858
10859 /* If bottom moved off end of frame, change mode line percentage. */
10860 if (XFASTINT (w->window_end_pos) <= 0
10861 && Z != IT_CHARPOS (it))
10862 w->update_mode_line = Qt;
10863
10864 /* Set window_end_pos to the offset of the last character displayed
10865 on the window from the end of current_buffer. Set
10866 window_end_vpos to its row number. */
10867 if (last_text_row)
10868 {
10869 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
10870 w->window_end_bytepos
10871 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10872 w->window_end_pos
10873 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10874 w->window_end_vpos
10875 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10876 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
10877 ->displays_text_p);
10878 }
10879 else
10880 {
10881 w->window_end_bytepos = 0;
10882 w->window_end_pos = w->window_end_vpos = make_number (0);
10883 }
10884
10885 /* But that is not valid info until redisplay finishes. */
10886 w->window_end_valid = Qnil;
10887 return 1;
10888 }
10889
10890
10891 \f
10892 /************************************************************************
10893 Window redisplay reusing current matrix when buffer has not changed
10894 ************************************************************************/
10895
10896 /* Try redisplay of window W showing an unchanged buffer with a
10897 different window start than the last time it was displayed by
10898 reusing its current matrix. Value is non-zero if successful.
10899 W->start is the new window start. */
10900
10901 static int
10902 try_window_reusing_current_matrix (w)
10903 struct window *w;
10904 {
10905 struct frame *f = XFRAME (w->frame);
10906 struct glyph_row *row, *bottom_row;
10907 struct it it;
10908 struct run run;
10909 struct text_pos start, new_start;
10910 int nrows_scrolled, i;
10911 struct glyph_row *last_text_row;
10912 struct glyph_row *last_reused_text_row;
10913 struct glyph_row *start_row;
10914 int start_vpos, min_y, max_y;
10915
10916 #if GLYPH_DEBUG
10917 if (inhibit_try_window_reusing)
10918 return 0;
10919 #endif
10920
10921 if (/* This function doesn't handle terminal frames. */
10922 !FRAME_WINDOW_P (f)
10923 /* Don't try to reuse the display if windows have been split
10924 or such. */
10925 || windows_or_buffers_changed
10926 || cursor_type_changed)
10927 return 0;
10928
10929 /* Can't do this if region may have changed. */
10930 if ((!NILP (Vtransient_mark_mode)
10931 && !NILP (current_buffer->mark_active))
10932 || !NILP (w->region_showing)
10933 || !NILP (Vshow_trailing_whitespace))
10934 return 0;
10935
10936 /* If top-line visibility has changed, give up. */
10937 if (WINDOW_WANTS_HEADER_LINE_P (w)
10938 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
10939 return 0;
10940
10941 /* Give up if old or new display is scrolled vertically. We could
10942 make this function handle this, but right now it doesn't. */
10943 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10944 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
10945 return 0;
10946
10947 /* The variable new_start now holds the new window start. The old
10948 start `start' can be determined from the current matrix. */
10949 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
10950 start = start_row->start.pos;
10951 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
10952
10953 /* Clear the desired matrix for the display below. */
10954 clear_glyph_matrix (w->desired_matrix);
10955
10956 if (CHARPOS (new_start) <= CHARPOS (start))
10957 {
10958 int first_row_y;
10959
10960 /* Don't use this method if the display starts with an ellipsis
10961 displayed for invisible text. It's not easy to handle that case
10962 below, and it's certainly not worth the effort since this is
10963 not a frequent case. */
10964 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
10965 return 0;
10966
10967 IF_DEBUG (debug_method_add (w, "twu1"));
10968
10969 /* Display up to a row that can be reused. The variable
10970 last_text_row is set to the last row displayed that displays
10971 text. Note that it.vpos == 0 if or if not there is a
10972 header-line; it's not the same as the MATRIX_ROW_VPOS! */
10973 start_display (&it, w, new_start);
10974 first_row_y = it.current_y;
10975 w->cursor.vpos = -1;
10976 last_text_row = last_reused_text_row = NULL;
10977
10978 while (it.current_y < it.last_visible_y
10979 && IT_CHARPOS (it) < CHARPOS (start)
10980 && !fonts_changed_p)
10981 if (display_line (&it))
10982 last_text_row = it.glyph_row - 1;
10983
10984 /* A value of current_y < last_visible_y means that we stopped
10985 at the previous window start, which in turn means that we
10986 have at least one reusable row. */
10987 if (it.current_y < it.last_visible_y)
10988 {
10989 /* IT.vpos always starts from 0; it counts text lines. */
10990 nrows_scrolled = it.vpos;
10991
10992 /* Find PT if not already found in the lines displayed. */
10993 if (w->cursor.vpos < 0)
10994 {
10995 int dy = it.current_y - first_row_y;
10996
10997 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10998 row = row_containing_pos (w, PT, row, NULL, dy);
10999 if (row)
11000 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
11001 dy, nrows_scrolled);
11002 else
11003 {
11004 clear_glyph_matrix (w->desired_matrix);
11005 return 0;
11006 }
11007 }
11008
11009 /* Scroll the display. Do it before the current matrix is
11010 changed. The problem here is that update has not yet
11011 run, i.e. part of the current matrix is not up to date.
11012 scroll_run_hook will clear the cursor, and use the
11013 current matrix to get the height of the row the cursor is
11014 in. */
11015 run.current_y = first_row_y;
11016 run.desired_y = it.current_y;
11017 run.height = it.last_visible_y - it.current_y;
11018
11019 if (run.height > 0 && run.current_y != run.desired_y)
11020 {
11021 update_begin (f);
11022 rif->update_window_begin_hook (w);
11023 rif->clear_mouse_face (w);
11024 rif->scroll_run_hook (w, &run);
11025 rif->update_window_end_hook (w, 0, 0);
11026 update_end (f);
11027 }
11028
11029 /* Shift current matrix down by nrows_scrolled lines. */
11030 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
11031 rotate_matrix (w->current_matrix,
11032 start_vpos,
11033 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
11034 nrows_scrolled);
11035
11036 /* Disable lines that must be updated. */
11037 for (i = 0; i < it.vpos; ++i)
11038 (start_row + i)->enabled_p = 0;
11039
11040 /* Re-compute Y positions. */
11041 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
11042 max_y = it.last_visible_y;
11043 for (row = start_row + nrows_scrolled;
11044 row < bottom_row;
11045 ++row)
11046 {
11047 row->y = it.current_y;
11048 row->visible_height = row->height;
11049
11050 if (row->y < min_y)
11051 row->visible_height -= min_y - row->y;
11052 if (row->y + row->height > max_y)
11053 row->visible_height -= row->y + row->height - max_y;
11054
11055 it.current_y += row->height;
11056
11057 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
11058 last_reused_text_row = row;
11059 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
11060 break;
11061 }
11062
11063 /* Disable lines in the current matrix which are now
11064 below the window. */
11065 for (++row; row < bottom_row; ++row)
11066 row->enabled_p = 0;
11067 }
11068
11069 /* Update window_end_pos etc.; last_reused_text_row is the last
11070 reused row from the current matrix containing text, if any.
11071 The value of last_text_row is the last displayed line
11072 containing text. */
11073 if (last_reused_text_row)
11074 {
11075 w->window_end_bytepos
11076 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
11077 w->window_end_pos
11078 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
11079 w->window_end_vpos
11080 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
11081 w->current_matrix));
11082 }
11083 else if (last_text_row)
11084 {
11085 w->window_end_bytepos
11086 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
11087 w->window_end_pos
11088 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
11089 w->window_end_vpos
11090 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
11091 }
11092 else
11093 {
11094 /* This window must be completely empty. */
11095 w->window_end_bytepos = 0;
11096 w->window_end_pos = w->window_end_vpos = make_number (0);
11097 }
11098 w->window_end_valid = Qnil;
11099
11100 /* Update hint: don't try scrolling again in update_window. */
11101 w->desired_matrix->no_scrolling_p = 1;
11102
11103 #if GLYPH_DEBUG
11104 debug_method_add (w, "try_window_reusing_current_matrix 1");
11105 #endif
11106 return 1;
11107 }
11108 else if (CHARPOS (new_start) > CHARPOS (start))
11109 {
11110 struct glyph_row *pt_row, *row;
11111 struct glyph_row *first_reusable_row;
11112 struct glyph_row *first_row_to_display;
11113 int dy;
11114 int yb = window_text_bottom_y (w);
11115
11116 /* Find the row starting at new_start, if there is one. Don't
11117 reuse a partially visible line at the end. */
11118 first_reusable_row = start_row;
11119 while (first_reusable_row->enabled_p
11120 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
11121 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
11122 < CHARPOS (new_start)))
11123 ++first_reusable_row;
11124
11125 /* Give up if there is no row to reuse. */
11126 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
11127 || !first_reusable_row->enabled_p
11128 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
11129 != CHARPOS (new_start)))
11130 return 0;
11131
11132 /* We can reuse fully visible rows beginning with
11133 first_reusable_row to the end of the window. Set
11134 first_row_to_display to the first row that cannot be reused.
11135 Set pt_row to the row containing point, if there is any. */
11136 pt_row = NULL;
11137 for (first_row_to_display = first_reusable_row;
11138 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
11139 ++first_row_to_display)
11140 {
11141 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
11142 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
11143 pt_row = first_row_to_display;
11144 }
11145
11146 /* Start displaying at the start of first_row_to_display. */
11147 xassert (first_row_to_display->y < yb);
11148 init_to_row_start (&it, w, first_row_to_display);
11149
11150 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
11151 - start_vpos);
11152 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
11153 - nrows_scrolled);
11154 it.current_y = (first_row_to_display->y - first_reusable_row->y
11155 + WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
11156
11157 /* Display lines beginning with first_row_to_display in the
11158 desired matrix. Set last_text_row to the last row displayed
11159 that displays text. */
11160 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
11161 if (pt_row == NULL)
11162 w->cursor.vpos = -1;
11163 last_text_row = NULL;
11164 while (it.current_y < it.last_visible_y && !fonts_changed_p)
11165 if (display_line (&it))
11166 last_text_row = it.glyph_row - 1;
11167
11168 /* Give up If point isn't in a row displayed or reused. */
11169 if (w->cursor.vpos < 0)
11170 {
11171 clear_glyph_matrix (w->desired_matrix);
11172 return 0;
11173 }
11174
11175 /* If point is in a reused row, adjust y and vpos of the cursor
11176 position. */
11177 if (pt_row)
11178 {
11179 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
11180 w->current_matrix);
11181 w->cursor.y -= first_reusable_row->y;
11182 }
11183
11184 /* Scroll the display. */
11185 run.current_y = first_reusable_row->y;
11186 run.desired_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
11187 run.height = it.last_visible_y - run.current_y;
11188 dy = run.current_y - run.desired_y;
11189
11190 if (run.height)
11191 {
11192 struct frame *f = XFRAME (WINDOW_FRAME (w));
11193 update_begin (f);
11194 rif->update_window_begin_hook (w);
11195 rif->clear_mouse_face (w);
11196 rif->scroll_run_hook (w, &run);
11197 rif->update_window_end_hook (w, 0, 0);
11198 update_end (f);
11199 }
11200
11201 /* Adjust Y positions of reused rows. */
11202 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
11203 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
11204 max_y = it.last_visible_y;
11205 for (row = first_reusable_row; row < first_row_to_display; ++row)
11206 {
11207 row->y -= dy;
11208 row->visible_height = row->height;
11209 if (row->y < min_y)
11210 row->visible_height -= min_y - row->y;
11211 if (row->y + row->height > max_y)
11212 row->visible_height -= row->y + row->height - max_y;
11213 }
11214
11215 /* Scroll the current matrix. */
11216 xassert (nrows_scrolled > 0);
11217 rotate_matrix (w->current_matrix,
11218 start_vpos,
11219 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
11220 -nrows_scrolled);
11221
11222 /* Disable rows not reused. */
11223 for (row -= nrows_scrolled; row < bottom_row; ++row)
11224 row->enabled_p = 0;
11225
11226 /* Adjust window end. A null value of last_text_row means that
11227 the window end is in reused rows which in turn means that
11228 only its vpos can have changed. */
11229 if (last_text_row)
11230 {
11231 w->window_end_bytepos
11232 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
11233 w->window_end_pos
11234 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
11235 w->window_end_vpos
11236 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
11237 }
11238 else
11239 {
11240 w->window_end_vpos
11241 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
11242 }
11243
11244 w->window_end_valid = Qnil;
11245 w->desired_matrix->no_scrolling_p = 1;
11246
11247 #if GLYPH_DEBUG
11248 debug_method_add (w, "try_window_reusing_current_matrix 2");
11249 #endif
11250 return 1;
11251 }
11252
11253 return 0;
11254 }
11255
11256
11257 \f
11258 /************************************************************************
11259 Window redisplay reusing current matrix when buffer has changed
11260 ************************************************************************/
11261
11262 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
11263 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
11264 int *, int *));
11265 static struct glyph_row *
11266 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
11267 struct glyph_row *));
11268
11269
11270 /* Return the last row in MATRIX displaying text. If row START is
11271 non-null, start searching with that row. IT gives the dimensions
11272 of the display. Value is null if matrix is empty; otherwise it is
11273 a pointer to the row found. */
11274
11275 static struct glyph_row *
11276 find_last_row_displaying_text (matrix, it, start)
11277 struct glyph_matrix *matrix;
11278 struct it *it;
11279 struct glyph_row *start;
11280 {
11281 struct glyph_row *row, *row_found;
11282
11283 /* Set row_found to the last row in IT->w's current matrix
11284 displaying text. The loop looks funny but think of partially
11285 visible lines. */
11286 row_found = NULL;
11287 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
11288 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
11289 {
11290 xassert (row->enabled_p);
11291 row_found = row;
11292 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
11293 break;
11294 ++row;
11295 }
11296
11297 return row_found;
11298 }
11299
11300
11301 /* Return the last row in the current matrix of W that is not affected
11302 by changes at the start of current_buffer that occurred since W's
11303 current matrix was built. Value is null if no such row exists.
11304
11305 BEG_UNCHANGED us the number of characters unchanged at the start of
11306 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
11307 first changed character in current_buffer. Characters at positions <
11308 BEG + BEG_UNCHANGED are at the same buffer positions as they were
11309 when the current matrix was built. */
11310
11311 static struct glyph_row *
11312 find_last_unchanged_at_beg_row (w)
11313 struct window *w;
11314 {
11315 int first_changed_pos = BEG + BEG_UNCHANGED;
11316 struct glyph_row *row;
11317 struct glyph_row *row_found = NULL;
11318 int yb = window_text_bottom_y (w);
11319
11320 /* Find the last row displaying unchanged text. */
11321 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11322 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
11323 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
11324 {
11325 if (/* If row ends before first_changed_pos, it is unchanged,
11326 except in some case. */
11327 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
11328 /* When row ends in ZV and we write at ZV it is not
11329 unchanged. */
11330 && !row->ends_at_zv_p
11331 /* When first_changed_pos is the end of a continued line,
11332 row is not unchanged because it may be no longer
11333 continued. */
11334 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
11335 && row->continued_p))
11336 row_found = row;
11337
11338 /* Stop if last visible row. */
11339 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
11340 break;
11341
11342 ++row;
11343 }
11344
11345 return row_found;
11346 }
11347
11348
11349 /* Find the first glyph row in the current matrix of W that is not
11350 affected by changes at the end of current_buffer since the
11351 time W's current matrix was built.
11352
11353 Return in *DELTA the number of chars by which buffer positions in
11354 unchanged text at the end of current_buffer must be adjusted.
11355
11356 Return in *DELTA_BYTES the corresponding number of bytes.
11357
11358 Value is null if no such row exists, i.e. all rows are affected by
11359 changes. */
11360
11361 static struct glyph_row *
11362 find_first_unchanged_at_end_row (w, delta, delta_bytes)
11363 struct window *w;
11364 int *delta, *delta_bytes;
11365 {
11366 struct glyph_row *row;
11367 struct glyph_row *row_found = NULL;
11368
11369 *delta = *delta_bytes = 0;
11370
11371 /* Display must not have been paused, otherwise the current matrix
11372 is not up to date. */
11373 if (NILP (w->window_end_valid))
11374 abort ();
11375
11376 /* A value of window_end_pos >= END_UNCHANGED means that the window
11377 end is in the range of changed text. If so, there is no
11378 unchanged row at the end of W's current matrix. */
11379 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
11380 return NULL;
11381
11382 /* Set row to the last row in W's current matrix displaying text. */
11383 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
11384
11385 /* If matrix is entirely empty, no unchanged row exists. */
11386 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
11387 {
11388 /* The value of row is the last glyph row in the matrix having a
11389 meaningful buffer position in it. The end position of row
11390 corresponds to window_end_pos. This allows us to translate
11391 buffer positions in the current matrix to current buffer
11392 positions for characters not in changed text. */
11393 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
11394 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
11395 int last_unchanged_pos, last_unchanged_pos_old;
11396 struct glyph_row *first_text_row
11397 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11398
11399 *delta = Z - Z_old;
11400 *delta_bytes = Z_BYTE - Z_BYTE_old;
11401
11402 /* Set last_unchanged_pos to the buffer position of the last
11403 character in the buffer that has not been changed. Z is the
11404 index + 1 of the last character in current_buffer, i.e. by
11405 subtracting END_UNCHANGED we get the index of the last
11406 unchanged character, and we have to add BEG to get its buffer
11407 position. */
11408 last_unchanged_pos = Z - END_UNCHANGED + BEG;
11409 last_unchanged_pos_old = last_unchanged_pos - *delta;
11410
11411 /* Search backward from ROW for a row displaying a line that
11412 starts at a minimum position >= last_unchanged_pos_old. */
11413 for (; row > first_text_row; --row)
11414 {
11415 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
11416 abort ();
11417
11418 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
11419 row_found = row;
11420 }
11421 }
11422
11423 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
11424 abort ();
11425
11426 return row_found;
11427 }
11428
11429
11430 /* Make sure that glyph rows in the current matrix of window W
11431 reference the same glyph memory as corresponding rows in the
11432 frame's frame matrix. This function is called after scrolling W's
11433 current matrix on a terminal frame in try_window_id and
11434 try_window_reusing_current_matrix. */
11435
11436 static void
11437 sync_frame_with_window_matrix_rows (w)
11438 struct window *w;
11439 {
11440 struct frame *f = XFRAME (w->frame);
11441 struct glyph_row *window_row, *window_row_end, *frame_row;
11442
11443 /* Preconditions: W must be a leaf window and full-width. Its frame
11444 must have a frame matrix. */
11445 xassert (NILP (w->hchild) && NILP (w->vchild));
11446 xassert (WINDOW_FULL_WIDTH_P (w));
11447 xassert (!FRAME_WINDOW_P (f));
11448
11449 /* If W is a full-width window, glyph pointers in W's current matrix
11450 have, by definition, to be the same as glyph pointers in the
11451 corresponding frame matrix. Note that frame matrices have no
11452 marginal areas (see build_frame_matrix). */
11453 window_row = w->current_matrix->rows;
11454 window_row_end = window_row + w->current_matrix->nrows;
11455 frame_row = f->current_matrix->rows + XFASTINT (w->top);
11456 while (window_row < window_row_end)
11457 {
11458 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
11459 struct glyph *end = window_row->glyphs[LAST_AREA];
11460
11461 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
11462 frame_row->glyphs[TEXT_AREA] = start;
11463 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
11464 frame_row->glyphs[LAST_AREA] = end;
11465
11466 /* Disable frame rows whose corresponding window rows have
11467 been disabled in try_window_id. */
11468 if (!window_row->enabled_p)
11469 frame_row->enabled_p = 0;
11470
11471 ++window_row, ++frame_row;
11472 }
11473 }
11474
11475
11476 /* Find the glyph row in window W containing CHARPOS. Consider all
11477 rows between START and END (not inclusive). END null means search
11478 all rows to the end of the display area of W. Value is the row
11479 containing CHARPOS or null. */
11480
11481 struct glyph_row *
11482 row_containing_pos (w, charpos, start, end, dy)
11483 struct window *w;
11484 int charpos;
11485 struct glyph_row *start, *end;
11486 int dy;
11487 {
11488 struct glyph_row *row = start;
11489 int last_y;
11490
11491 /* If we happen to start on a header-line, skip that. */
11492 if (row->mode_line_p)
11493 ++row;
11494
11495 if ((end && row >= end) || !row->enabled_p)
11496 return NULL;
11497
11498 last_y = window_text_bottom_y (w) - dy;
11499
11500 while ((end == NULL || row < end)
11501 && MATRIX_ROW_BOTTOM_Y (row) < last_y
11502 && (MATRIX_ROW_END_CHARPOS (row) < charpos
11503 || (MATRIX_ROW_END_CHARPOS (row) == charpos
11504 /* The end position of a row equals the start
11505 position of the next row. If CHARPOS is there, we
11506 would rather display it in the next line, except
11507 when this line ends in ZV. */
11508 && !row->ends_at_zv_p
11509 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))
11510 ++row;
11511
11512 /* Give up if CHARPOS not found. */
11513 if ((end && row >= end)
11514 || charpos < MATRIX_ROW_START_CHARPOS (row)
11515 || charpos > MATRIX_ROW_END_CHARPOS (row))
11516 row = NULL;
11517
11518 return row;
11519 }
11520
11521
11522 /* Try to redisplay window W by reusing its existing display. W's
11523 current matrix must be up to date when this function is called,
11524 i.e. window_end_valid must not be nil.
11525
11526 Value is
11527
11528 1 if display has been updated
11529 0 if otherwise unsuccessful
11530 -1 if redisplay with same window start is known not to succeed
11531
11532 The following steps are performed:
11533
11534 1. Find the last row in the current matrix of W that is not
11535 affected by changes at the start of current_buffer. If no such row
11536 is found, give up.
11537
11538 2. Find the first row in W's current matrix that is not affected by
11539 changes at the end of current_buffer. Maybe there is no such row.
11540
11541 3. Display lines beginning with the row + 1 found in step 1 to the
11542 row found in step 2 or, if step 2 didn't find a row, to the end of
11543 the window.
11544
11545 4. If cursor is not known to appear on the window, give up.
11546
11547 5. If display stopped at the row found in step 2, scroll the
11548 display and current matrix as needed.
11549
11550 6. Maybe display some lines at the end of W, if we must. This can
11551 happen under various circumstances, like a partially visible line
11552 becoming fully visible, or because newly displayed lines are displayed
11553 in smaller font sizes.
11554
11555 7. Update W's window end information. */
11556
11557 static int
11558 try_window_id (w)
11559 struct window *w;
11560 {
11561 struct frame *f = XFRAME (w->frame);
11562 struct glyph_matrix *current_matrix = w->current_matrix;
11563 struct glyph_matrix *desired_matrix = w->desired_matrix;
11564 struct glyph_row *last_unchanged_at_beg_row;
11565 struct glyph_row *first_unchanged_at_end_row;
11566 struct glyph_row *row;
11567 struct glyph_row *bottom_row;
11568 int bottom_vpos;
11569 struct it it;
11570 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
11571 struct text_pos start_pos;
11572 struct run run;
11573 int first_unchanged_at_end_vpos = 0;
11574 struct glyph_row *last_text_row, *last_text_row_at_end;
11575 struct text_pos start;
11576 int first_changed_charpos, last_changed_charpos;
11577
11578 #if GLYPH_DEBUG
11579 if (inhibit_try_window_id)
11580 return 0;
11581 #endif
11582
11583 /* This is handy for debugging. */
11584 #if 0
11585 #define GIVE_UP(X) \
11586 do { \
11587 fprintf (stderr, "try_window_id give up %d\n", (X)); \
11588 return 0; \
11589 } while (0)
11590 #else
11591 #define GIVE_UP(X) return 0
11592 #endif
11593
11594 SET_TEXT_POS_FROM_MARKER (start, w->start);
11595
11596 /* Don't use this for mini-windows because these can show
11597 messages and mini-buffers, and we don't handle that here. */
11598 if (MINI_WINDOW_P (w))
11599 GIVE_UP (1);
11600
11601 /* This flag is used to prevent redisplay optimizations. */
11602 if (windows_or_buffers_changed || cursor_type_changed)
11603 GIVE_UP (2);
11604
11605 /* Verify that narrowing has not changed.
11606 Also verify that we were not told to prevent redisplay optimizations.
11607 It would be nice to further
11608 reduce the number of cases where this prevents try_window_id. */
11609 if (current_buffer->clip_changed
11610 || current_buffer->prevent_redisplay_optimizations_p)
11611 GIVE_UP (3);
11612
11613 /* Window must either use window-based redisplay or be full width. */
11614 if (!FRAME_WINDOW_P (f)
11615 && (!line_ins_del_ok
11616 || !WINDOW_FULL_WIDTH_P (w)))
11617 GIVE_UP (4);
11618
11619 /* Give up if point is not known NOT to appear in W. */
11620 if (PT < CHARPOS (start))
11621 GIVE_UP (5);
11622
11623 /* Another way to prevent redisplay optimizations. */
11624 if (XFASTINT (w->last_modified) == 0)
11625 GIVE_UP (6);
11626
11627 /* Verify that window is not hscrolled. */
11628 if (XFASTINT (w->hscroll) != 0)
11629 GIVE_UP (7);
11630
11631 /* Verify that display wasn't paused. */
11632 if (NILP (w->window_end_valid))
11633 GIVE_UP (8);
11634
11635 /* Can't use this if highlighting a region because a cursor movement
11636 will do more than just set the cursor. */
11637 if (!NILP (Vtransient_mark_mode)
11638 && !NILP (current_buffer->mark_active))
11639 GIVE_UP (9);
11640
11641 /* Likewise if highlighting trailing whitespace. */
11642 if (!NILP (Vshow_trailing_whitespace))
11643 GIVE_UP (11);
11644
11645 /* Likewise if showing a region. */
11646 if (!NILP (w->region_showing))
11647 GIVE_UP (10);
11648
11649 /* Can use this if overlay arrow position and or string have changed. */
11650 if (!EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
11651 || !EQ (last_arrow_string, Voverlay_arrow_string))
11652 GIVE_UP (12);
11653
11654
11655 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
11656 only if buffer has really changed. The reason is that the gap is
11657 initially at Z for freshly visited files. The code below would
11658 set end_unchanged to 0 in that case. */
11659 if (MODIFF > SAVE_MODIFF
11660 /* This seems to happen sometimes after saving a buffer. */
11661 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
11662 {
11663 if (GPT - BEG < BEG_UNCHANGED)
11664 BEG_UNCHANGED = GPT - BEG;
11665 if (Z - GPT < END_UNCHANGED)
11666 END_UNCHANGED = Z - GPT;
11667 }
11668
11669 /* The position of the first and last character that has been changed. */
11670 first_changed_charpos = BEG + BEG_UNCHANGED;
11671 last_changed_charpos = Z - END_UNCHANGED;
11672
11673 /* If window starts after a line end, and the last change is in
11674 front of that newline, then changes don't affect the display.
11675 This case happens with stealth-fontification. Note that although
11676 the display is unchanged, glyph positions in the matrix have to
11677 be adjusted, of course. */
11678 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
11679 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
11680 && ((last_changed_charpos < CHARPOS (start)
11681 && CHARPOS (start) == BEGV)
11682 || (last_changed_charpos < CHARPOS (start) - 1
11683 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
11684 {
11685 int Z_old, delta, Z_BYTE_old, delta_bytes;
11686 struct glyph_row *r0;
11687
11688 /* Compute how many chars/bytes have been added to or removed
11689 from the buffer. */
11690 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
11691 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
11692 delta = Z - Z_old;
11693 delta_bytes = Z_BYTE - Z_BYTE_old;
11694
11695 /* Give up if PT is not in the window. Note that it already has
11696 been checked at the start of try_window_id that PT is not in
11697 front of the window start. */
11698 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
11699 GIVE_UP (13);
11700
11701 /* If window start is unchanged, we can reuse the whole matrix
11702 as is, after adjusting glyph positions. No need to compute
11703 the window end again, since its offset from Z hasn't changed. */
11704 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
11705 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
11706 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes)
11707 {
11708 /* Adjust positions in the glyph matrix. */
11709 if (delta || delta_bytes)
11710 {
11711 struct glyph_row *r1
11712 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11713 increment_matrix_positions (w->current_matrix,
11714 MATRIX_ROW_VPOS (r0, current_matrix),
11715 MATRIX_ROW_VPOS (r1, current_matrix),
11716 delta, delta_bytes);
11717 }
11718
11719 /* Set the cursor. */
11720 row = row_containing_pos (w, PT, r0, NULL, 0);
11721 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
11722 return 1;
11723 }
11724 }
11725
11726 /* Handle the case that changes are all below what is displayed in
11727 the window, and that PT is in the window. This shortcut cannot
11728 be taken if ZV is visible in the window, and text has been added
11729 there that is visible in the window. */
11730 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
11731 /* ZV is not visible in the window, or there are no
11732 changes at ZV, actually. */
11733 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
11734 || first_changed_charpos == last_changed_charpos))
11735 {
11736 struct glyph_row *r0;
11737
11738 /* Give up if PT is not in the window. Note that it already has
11739 been checked at the start of try_window_id that PT is not in
11740 front of the window start. */
11741 if (PT >= MATRIX_ROW_END_CHARPOS (row))
11742 GIVE_UP (14);
11743
11744 /* If window start is unchanged, we can reuse the whole matrix
11745 as is, without changing glyph positions since no text has
11746 been added/removed in front of the window end. */
11747 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
11748 if (TEXT_POS_EQUAL_P (start, r0->start.pos))
11749 {
11750 /* We have to compute the window end anew since text
11751 can have been added/removed after it. */
11752 w->window_end_pos
11753 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
11754 w->window_end_bytepos
11755 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11756
11757 /* Set the cursor. */
11758 row = row_containing_pos (w, PT, r0, NULL, 0);
11759 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
11760 return 2;
11761 }
11762 }
11763
11764 /* Give up if window start is in the changed area.
11765
11766 The condition used to read
11767
11768 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
11769
11770 but why that was tested escapes me at the moment. */
11771 if (CHARPOS (start) >= first_changed_charpos
11772 && CHARPOS (start) <= last_changed_charpos)
11773 GIVE_UP (15);
11774
11775 /* Check that window start agrees with the start of the first glyph
11776 row in its current matrix. Check this after we know the window
11777 start is not in changed text, otherwise positions would not be
11778 comparable. */
11779 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
11780 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
11781 GIVE_UP (16);
11782
11783 /* Give up if the window ends in strings. Overlay strings
11784 at the end are difficult to handle, so don't try. */
11785 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
11786 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
11787 GIVE_UP (20);
11788
11789 /* Compute the position at which we have to start displaying new
11790 lines. Some of the lines at the top of the window might be
11791 reusable because they are not displaying changed text. Find the
11792 last row in W's current matrix not affected by changes at the
11793 start of current_buffer. Value is null if changes start in the
11794 first line of window. */
11795 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
11796 if (last_unchanged_at_beg_row)
11797 {
11798 /* Avoid starting to display in the moddle of a character, a TAB
11799 for instance. This is easier than to set up the iterator
11800 exactly, and it's not a frequent case, so the additional
11801 effort wouldn't really pay off. */
11802 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
11803 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
11804 && last_unchanged_at_beg_row > w->current_matrix->rows)
11805 --last_unchanged_at_beg_row;
11806
11807 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
11808 GIVE_UP (17);
11809
11810 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
11811 GIVE_UP (18);
11812 start_pos = it.current.pos;
11813
11814 /* Start displaying new lines in the desired matrix at the same
11815 vpos we would use in the current matrix, i.e. below
11816 last_unchanged_at_beg_row. */
11817 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
11818 current_matrix);
11819 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11820 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
11821
11822 xassert (it.hpos == 0 && it.current_x == 0);
11823 }
11824 else
11825 {
11826 /* There are no reusable lines at the start of the window.
11827 Start displaying in the first line. */
11828 start_display (&it, w, start);
11829 start_pos = it.current.pos;
11830 }
11831
11832 /* Find the first row that is not affected by changes at the end of
11833 the buffer. Value will be null if there is no unchanged row, in
11834 which case we must redisplay to the end of the window. delta
11835 will be set to the value by which buffer positions beginning with
11836 first_unchanged_at_end_row have to be adjusted due to text
11837 changes. */
11838 first_unchanged_at_end_row
11839 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
11840 IF_DEBUG (debug_delta = delta);
11841 IF_DEBUG (debug_delta_bytes = delta_bytes);
11842
11843 /* Set stop_pos to the buffer position up to which we will have to
11844 display new lines. If first_unchanged_at_end_row != NULL, this
11845 is the buffer position of the start of the line displayed in that
11846 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
11847 that we don't stop at a buffer position. */
11848 stop_pos = 0;
11849 if (first_unchanged_at_end_row)
11850 {
11851 xassert (last_unchanged_at_beg_row == NULL
11852 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
11853
11854 /* If this is a continuation line, move forward to the next one
11855 that isn't. Changes in lines above affect this line.
11856 Caution: this may move first_unchanged_at_end_row to a row
11857 not displaying text. */
11858 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
11859 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11860 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11861 < it.last_visible_y))
11862 ++first_unchanged_at_end_row;
11863
11864 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11865 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11866 >= it.last_visible_y))
11867 first_unchanged_at_end_row = NULL;
11868 else
11869 {
11870 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
11871 + delta);
11872 first_unchanged_at_end_vpos
11873 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
11874 xassert (stop_pos >= Z - END_UNCHANGED);
11875 }
11876 }
11877 else if (last_unchanged_at_beg_row == NULL)
11878 GIVE_UP (19);
11879
11880
11881 #if GLYPH_DEBUG
11882
11883 /* Either there is no unchanged row at the end, or the one we have
11884 now displays text. This is a necessary condition for the window
11885 end pos calculation at the end of this function. */
11886 xassert (first_unchanged_at_end_row == NULL
11887 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
11888
11889 debug_last_unchanged_at_beg_vpos
11890 = (last_unchanged_at_beg_row
11891 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
11892 : -1);
11893 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
11894
11895 #endif /* GLYPH_DEBUG != 0 */
11896
11897
11898 /* Display new lines. Set last_text_row to the last new line
11899 displayed which has text on it, i.e. might end up as being the
11900 line where the window_end_vpos is. */
11901 w->cursor.vpos = -1;
11902 last_text_row = NULL;
11903 overlay_arrow_seen = 0;
11904 while (it.current_y < it.last_visible_y
11905 && !fonts_changed_p
11906 && (first_unchanged_at_end_row == NULL
11907 || IT_CHARPOS (it) < stop_pos))
11908 {
11909 if (display_line (&it))
11910 last_text_row = it.glyph_row - 1;
11911 }
11912
11913 if (fonts_changed_p)
11914 return -1;
11915
11916
11917 /* Compute differences in buffer positions, y-positions etc. for
11918 lines reused at the bottom of the window. Compute what we can
11919 scroll. */
11920 if (first_unchanged_at_end_row
11921 /* No lines reused because we displayed everything up to the
11922 bottom of the window. */
11923 && it.current_y < it.last_visible_y)
11924 {
11925 dvpos = (it.vpos
11926 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
11927 current_matrix));
11928 dy = it.current_y - first_unchanged_at_end_row->y;
11929 run.current_y = first_unchanged_at_end_row->y;
11930 run.desired_y = run.current_y + dy;
11931 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
11932 }
11933 else
11934 {
11935 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
11936 first_unchanged_at_end_row = NULL;
11937 }
11938 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
11939
11940
11941 /* Find the cursor if not already found. We have to decide whether
11942 PT will appear on this window (it sometimes doesn't, but this is
11943 not a very frequent case.) This decision has to be made before
11944 the current matrix is altered. A value of cursor.vpos < 0 means
11945 that PT is either in one of the lines beginning at
11946 first_unchanged_at_end_row or below the window. Don't care for
11947 lines that might be displayed later at the window end; as
11948 mentioned, this is not a frequent case. */
11949 if (w->cursor.vpos < 0)
11950 {
11951 /* Cursor in unchanged rows at the top? */
11952 if (PT < CHARPOS (start_pos)
11953 && last_unchanged_at_beg_row)
11954 {
11955 row = row_containing_pos (w, PT,
11956 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
11957 last_unchanged_at_beg_row + 1, 0);
11958 if (row)
11959 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11960 }
11961
11962 /* Start from first_unchanged_at_end_row looking for PT. */
11963 else if (first_unchanged_at_end_row)
11964 {
11965 row = row_containing_pos (w, PT - delta,
11966 first_unchanged_at_end_row, NULL, 0);
11967 if (row)
11968 set_cursor_from_row (w, row, w->current_matrix, delta,
11969 delta_bytes, dy, dvpos);
11970 }
11971
11972 /* Give up if cursor was not found. */
11973 if (w->cursor.vpos < 0)
11974 {
11975 clear_glyph_matrix (w->desired_matrix);
11976 return -1;
11977 }
11978 }
11979
11980 /* Don't let the cursor end in the scroll margins. */
11981 {
11982 int this_scroll_margin, cursor_height;
11983
11984 this_scroll_margin = max (0, scroll_margin);
11985 this_scroll_margin = min (this_scroll_margin,
11986 XFASTINT (w->height) / 4);
11987 this_scroll_margin *= CANON_Y_UNIT (it.f);
11988 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
11989
11990 if ((w->cursor.y < this_scroll_margin
11991 && CHARPOS (start) > BEGV)
11992 /* Don't take scroll margin into account at the bottom because
11993 old redisplay didn't do it either. */
11994 || w->cursor.y + cursor_height > it.last_visible_y)
11995 {
11996 w->cursor.vpos = -1;
11997 clear_glyph_matrix (w->desired_matrix);
11998 return -1;
11999 }
12000 }
12001
12002 /* Scroll the display. Do it before changing the current matrix so
12003 that xterm.c doesn't get confused about where the cursor glyph is
12004 found. */
12005 if (dy && run.height)
12006 {
12007 update_begin (f);
12008
12009 if (FRAME_WINDOW_P (f))
12010 {
12011 rif->update_window_begin_hook (w);
12012 rif->clear_mouse_face (w);
12013 rif->scroll_run_hook (w, &run);
12014 rif->update_window_end_hook (w, 0, 0);
12015 }
12016 else
12017 {
12018 /* Terminal frame. In this case, dvpos gives the number of
12019 lines to scroll by; dvpos < 0 means scroll up. */
12020 int first_unchanged_at_end_vpos
12021 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
12022 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
12023 int end = (XFASTINT (w->top)
12024 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
12025 + window_internal_height (w));
12026
12027 /* Perform the operation on the screen. */
12028 if (dvpos > 0)
12029 {
12030 /* Scroll last_unchanged_at_beg_row to the end of the
12031 window down dvpos lines. */
12032 set_terminal_window (end);
12033
12034 /* On dumb terminals delete dvpos lines at the end
12035 before inserting dvpos empty lines. */
12036 if (!scroll_region_ok)
12037 ins_del_lines (end - dvpos, -dvpos);
12038
12039 /* Insert dvpos empty lines in front of
12040 last_unchanged_at_beg_row. */
12041 ins_del_lines (from, dvpos);
12042 }
12043 else if (dvpos < 0)
12044 {
12045 /* Scroll up last_unchanged_at_beg_vpos to the end of
12046 the window to last_unchanged_at_beg_vpos - |dvpos|. */
12047 set_terminal_window (end);
12048
12049 /* Delete dvpos lines in front of
12050 last_unchanged_at_beg_vpos. ins_del_lines will set
12051 the cursor to the given vpos and emit |dvpos| delete
12052 line sequences. */
12053 ins_del_lines (from + dvpos, dvpos);
12054
12055 /* On a dumb terminal insert dvpos empty lines at the
12056 end. */
12057 if (!scroll_region_ok)
12058 ins_del_lines (end + dvpos, -dvpos);
12059 }
12060
12061 set_terminal_window (0);
12062 }
12063
12064 update_end (f);
12065 }
12066
12067 /* Shift reused rows of the current matrix to the right position.
12068 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
12069 text. */
12070 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
12071 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
12072 if (dvpos < 0)
12073 {
12074 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
12075 bottom_vpos, dvpos);
12076 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
12077 bottom_vpos, 0);
12078 }
12079 else if (dvpos > 0)
12080 {
12081 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
12082 bottom_vpos, dvpos);
12083 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
12084 first_unchanged_at_end_vpos + dvpos, 0);
12085 }
12086
12087 /* For frame-based redisplay, make sure that current frame and window
12088 matrix are in sync with respect to glyph memory. */
12089 if (!FRAME_WINDOW_P (f))
12090 sync_frame_with_window_matrix_rows (w);
12091
12092 /* Adjust buffer positions in reused rows. */
12093 if (delta)
12094 increment_matrix_positions (current_matrix,
12095 first_unchanged_at_end_vpos + dvpos,
12096 bottom_vpos, delta, delta_bytes);
12097
12098 /* Adjust Y positions. */
12099 if (dy)
12100 shift_glyph_matrix (w, current_matrix,
12101 first_unchanged_at_end_vpos + dvpos,
12102 bottom_vpos, dy);
12103
12104 if (first_unchanged_at_end_row)
12105 first_unchanged_at_end_row += dvpos;
12106
12107 /* If scrolling up, there may be some lines to display at the end of
12108 the window. */
12109 last_text_row_at_end = NULL;
12110 if (dy < 0)
12111 {
12112 /* Scrolling up can leave for example a partially visible line
12113 at the end of the window to be redisplayed. */
12114 /* Set last_row to the glyph row in the current matrix where the
12115 window end line is found. It has been moved up or down in
12116 the matrix by dvpos. */
12117 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
12118 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
12119
12120 /* If last_row is the window end line, it should display text. */
12121 xassert (last_row->displays_text_p);
12122
12123 /* If window end line was partially visible before, begin
12124 displaying at that line. Otherwise begin displaying with the
12125 line following it. */
12126 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
12127 {
12128 init_to_row_start (&it, w, last_row);
12129 it.vpos = last_vpos;
12130 it.current_y = last_row->y;
12131 }
12132 else
12133 {
12134 init_to_row_end (&it, w, last_row);
12135 it.vpos = 1 + last_vpos;
12136 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
12137 ++last_row;
12138 }
12139
12140 /* We may start in a continuation line. If so, we have to
12141 get the right continuation_lines_width and current_x. */
12142 it.continuation_lines_width = last_row->continuation_lines_width;
12143 it.hpos = it.current_x = 0;
12144
12145 /* Display the rest of the lines at the window end. */
12146 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
12147 while (it.current_y < it.last_visible_y
12148 && !fonts_changed_p)
12149 {
12150 /* Is it always sure that the display agrees with lines in
12151 the current matrix? I don't think so, so we mark rows
12152 displayed invalid in the current matrix by setting their
12153 enabled_p flag to zero. */
12154 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
12155 if (display_line (&it))
12156 last_text_row_at_end = it.glyph_row - 1;
12157 }
12158 }
12159
12160 /* Update window_end_pos and window_end_vpos. */
12161 if (first_unchanged_at_end_row
12162 && first_unchanged_at_end_row->y < it.last_visible_y
12163 && !last_text_row_at_end)
12164 {
12165 /* Window end line if one of the preserved rows from the current
12166 matrix. Set row to the last row displaying text in current
12167 matrix starting at first_unchanged_at_end_row, after
12168 scrolling. */
12169 xassert (first_unchanged_at_end_row->displays_text_p);
12170 row = find_last_row_displaying_text (w->current_matrix, &it,
12171 first_unchanged_at_end_row);
12172 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
12173
12174 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
12175 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
12176 w->window_end_vpos
12177 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
12178 xassert (w->window_end_bytepos >= 0);
12179 IF_DEBUG (debug_method_add (w, "A"));
12180 }
12181 else if (last_text_row_at_end)
12182 {
12183 w->window_end_pos
12184 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
12185 w->window_end_bytepos
12186 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
12187 w->window_end_vpos
12188 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
12189 xassert (w->window_end_bytepos >= 0);
12190 IF_DEBUG (debug_method_add (w, "B"));
12191 }
12192 else if (last_text_row)
12193 {
12194 /* We have displayed either to the end of the window or at the
12195 end of the window, i.e. the last row with text is to be found
12196 in the desired matrix. */
12197 w->window_end_pos
12198 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12199 w->window_end_bytepos
12200 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12201 w->window_end_vpos
12202 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
12203 xassert (w->window_end_bytepos >= 0);
12204 }
12205 else if (first_unchanged_at_end_row == NULL
12206 && last_text_row == NULL
12207 && last_text_row_at_end == NULL)
12208 {
12209 /* Displayed to end of window, but no line containing text was
12210 displayed. Lines were deleted at the end of the window. */
12211 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
12212 int vpos = XFASTINT (w->window_end_vpos);
12213 struct glyph_row *current_row = current_matrix->rows + vpos;
12214 struct glyph_row *desired_row = desired_matrix->rows + vpos;
12215
12216 for (row = NULL;
12217 row == NULL && vpos >= first_vpos;
12218 --vpos, --current_row, --desired_row)
12219 {
12220 if (desired_row->enabled_p)
12221 {
12222 if (desired_row->displays_text_p)
12223 row = desired_row;
12224 }
12225 else if (current_row->displays_text_p)
12226 row = current_row;
12227 }
12228
12229 xassert (row != NULL);
12230 w->window_end_vpos = make_number (vpos + 1);
12231 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
12232 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
12233 xassert (w->window_end_bytepos >= 0);
12234 IF_DEBUG (debug_method_add (w, "C"));
12235 }
12236 else
12237 abort ();
12238
12239 #if 0 /* This leads to problems, for instance when the cursor is
12240 at ZV, and the cursor line displays no text. */
12241 /* Disable rows below what's displayed in the window. This makes
12242 debugging easier. */
12243 enable_glyph_matrix_rows (current_matrix,
12244 XFASTINT (w->window_end_vpos) + 1,
12245 bottom_vpos, 0);
12246 #endif
12247
12248 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
12249 debug_end_vpos = XFASTINT (w->window_end_vpos));
12250
12251 /* Record that display has not been completed. */
12252 w->window_end_valid = Qnil;
12253 w->desired_matrix->no_scrolling_p = 1;
12254 return 3;
12255
12256 #undef GIVE_UP
12257 }
12258
12259
12260 \f
12261 /***********************************************************************
12262 More debugging support
12263 ***********************************************************************/
12264
12265 #if GLYPH_DEBUG
12266
12267 void dump_glyph_row P_ ((struct glyph_row *, int, int));
12268 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
12269 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
12270
12271
12272 /* Dump the contents of glyph matrix MATRIX on stderr.
12273
12274 GLYPHS 0 means don't show glyph contents.
12275 GLYPHS 1 means show glyphs in short form
12276 GLYPHS > 1 means show glyphs in long form. */
12277
12278 void
12279 dump_glyph_matrix (matrix, glyphs)
12280 struct glyph_matrix *matrix;
12281 int glyphs;
12282 {
12283 int i;
12284 for (i = 0; i < matrix->nrows; ++i)
12285 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
12286 }
12287
12288
12289 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
12290 the glyph row and area where the glyph comes from. */
12291
12292 void
12293 dump_glyph (row, glyph, area)
12294 struct glyph_row *row;
12295 struct glyph *glyph;
12296 int area;
12297 {
12298 if (glyph->type == CHAR_GLYPH)
12299 {
12300 fprintf (stderr,
12301 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12302 glyph - row->glyphs[TEXT_AREA],
12303 'C',
12304 glyph->charpos,
12305 (BUFFERP (glyph->object)
12306 ? 'B'
12307 : (STRINGP (glyph->object)
12308 ? 'S'
12309 : '-')),
12310 glyph->pixel_width,
12311 glyph->u.ch,
12312 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
12313 ? glyph->u.ch
12314 : '.'),
12315 glyph->face_id,
12316 glyph->left_box_line_p,
12317 glyph->right_box_line_p);
12318 }
12319 else if (glyph->type == STRETCH_GLYPH)
12320 {
12321 fprintf (stderr,
12322 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12323 glyph - row->glyphs[TEXT_AREA],
12324 'S',
12325 glyph->charpos,
12326 (BUFFERP (glyph->object)
12327 ? 'B'
12328 : (STRINGP (glyph->object)
12329 ? 'S'
12330 : '-')),
12331 glyph->pixel_width,
12332 0,
12333 '.',
12334 glyph->face_id,
12335 glyph->left_box_line_p,
12336 glyph->right_box_line_p);
12337 }
12338 else if (glyph->type == IMAGE_GLYPH)
12339 {
12340 fprintf (stderr,
12341 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12342 glyph - row->glyphs[TEXT_AREA],
12343 'I',
12344 glyph->charpos,
12345 (BUFFERP (glyph->object)
12346 ? 'B'
12347 : (STRINGP (glyph->object)
12348 ? 'S'
12349 : '-')),
12350 glyph->pixel_width,
12351 glyph->u.img_id,
12352 '.',
12353 glyph->face_id,
12354 glyph->left_box_line_p,
12355 glyph->right_box_line_p);
12356 }
12357 }
12358
12359
12360 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
12361 GLYPHS 0 means don't show glyph contents.
12362 GLYPHS 1 means show glyphs in short form
12363 GLYPHS > 1 means show glyphs in long form. */
12364
12365 void
12366 dump_glyph_row (row, vpos, glyphs)
12367 struct glyph_row *row;
12368 int vpos, glyphs;
12369 {
12370 if (glyphs != 1)
12371 {
12372 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
12373 fprintf (stderr, "=======================================================================\n");
12374
12375 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d\
12376 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
12377 vpos,
12378 MATRIX_ROW_START_CHARPOS (row),
12379 MATRIX_ROW_END_CHARPOS (row),
12380 row->used[TEXT_AREA],
12381 row->contains_overlapping_glyphs_p,
12382 row->enabled_p,
12383 row->truncated_on_left_p,
12384 row->truncated_on_right_p,
12385 row->overlay_arrow_p,
12386 row->continued_p,
12387 MATRIX_ROW_CONTINUATION_LINE_P (row),
12388 row->displays_text_p,
12389 row->ends_at_zv_p,
12390 row->fill_line_p,
12391 row->ends_in_middle_of_char_p,
12392 row->starts_in_middle_of_char_p,
12393 row->mouse_face_p,
12394 row->x,
12395 row->y,
12396 row->pixel_width,
12397 row->height,
12398 row->visible_height,
12399 row->ascent,
12400 row->phys_ascent);
12401 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
12402 row->end.overlay_string_index,
12403 row->continuation_lines_width);
12404 fprintf (stderr, "%9d %5d\n",
12405 CHARPOS (row->start.string_pos),
12406 CHARPOS (row->end.string_pos));
12407 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
12408 row->end.dpvec_index);
12409 }
12410
12411 if (glyphs > 1)
12412 {
12413 int area;
12414
12415 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12416 {
12417 struct glyph *glyph = row->glyphs[area];
12418 struct glyph *glyph_end = glyph + row->used[area];
12419
12420 /* Glyph for a line end in text. */
12421 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
12422 ++glyph_end;
12423
12424 if (glyph < glyph_end)
12425 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
12426
12427 for (; glyph < glyph_end; ++glyph)
12428 dump_glyph (row, glyph, area);
12429 }
12430 }
12431 else if (glyphs == 1)
12432 {
12433 int area;
12434
12435 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12436 {
12437 char *s = (char *) alloca (row->used[area] + 1);
12438 int i;
12439
12440 for (i = 0; i < row->used[area]; ++i)
12441 {
12442 struct glyph *glyph = row->glyphs[area] + i;
12443 if (glyph->type == CHAR_GLYPH
12444 && glyph->u.ch < 0x80
12445 && glyph->u.ch >= ' ')
12446 s[i] = glyph->u.ch;
12447 else
12448 s[i] = '.';
12449 }
12450
12451 s[i] = '\0';
12452 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
12453 }
12454 }
12455 }
12456
12457
12458 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
12459 Sdump_glyph_matrix, 0, 1, "p",
12460 doc: /* Dump the current matrix of the selected window to stderr.
12461 Shows contents of glyph row structures. With non-nil
12462 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
12463 glyphs in short form, otherwise show glyphs in long form. */)
12464 (glyphs)
12465 Lisp_Object glyphs;
12466 {
12467 struct window *w = XWINDOW (selected_window);
12468 struct buffer *buffer = XBUFFER (w->buffer);
12469
12470 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
12471 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
12472 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
12473 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
12474 fprintf (stderr, "=============================================\n");
12475 dump_glyph_matrix (w->current_matrix,
12476 NILP (glyphs) ? 0 : XINT (glyphs));
12477 return Qnil;
12478 }
12479
12480
12481 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
12482 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
12483 ()
12484 {
12485 struct frame *f = XFRAME (selected_frame);
12486 dump_glyph_matrix (f->current_matrix, 1);
12487 return Qnil;
12488 }
12489
12490
12491 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
12492 doc: /* Dump glyph row ROW to stderr.
12493 GLYPH 0 means don't dump glyphs.
12494 GLYPH 1 means dump glyphs in short form.
12495 GLYPH > 1 or omitted means dump glyphs in long form. */)
12496 (row, glyphs)
12497 Lisp_Object row, glyphs;
12498 {
12499 struct glyph_matrix *matrix;
12500 int vpos;
12501
12502 CHECK_NUMBER (row);
12503 matrix = XWINDOW (selected_window)->current_matrix;
12504 vpos = XINT (row);
12505 if (vpos >= 0 && vpos < matrix->nrows)
12506 dump_glyph_row (MATRIX_ROW (matrix, vpos),
12507 vpos,
12508 INTEGERP (glyphs) ? XINT (glyphs) : 2);
12509 return Qnil;
12510 }
12511
12512
12513 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
12514 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
12515 GLYPH 0 means don't dump glyphs.
12516 GLYPH 1 means dump glyphs in short form.
12517 GLYPH > 1 or omitted means dump glyphs in long form. */)
12518 (row, glyphs)
12519 Lisp_Object row, glyphs;
12520 {
12521 struct frame *sf = SELECTED_FRAME ();
12522 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
12523 int vpos;
12524
12525 CHECK_NUMBER (row);
12526 vpos = XINT (row);
12527 if (vpos >= 0 && vpos < m->nrows)
12528 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
12529 INTEGERP (glyphs) ? XINT (glyphs) : 2);
12530 return Qnil;
12531 }
12532
12533
12534 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
12535 doc: /* Toggle tracing of redisplay.
12536 With ARG, turn tracing on if and only if ARG is positive. */)
12537 (arg)
12538 Lisp_Object arg;
12539 {
12540 if (NILP (arg))
12541 trace_redisplay_p = !trace_redisplay_p;
12542 else
12543 {
12544 arg = Fprefix_numeric_value (arg);
12545 trace_redisplay_p = XINT (arg) > 0;
12546 }
12547
12548 return Qnil;
12549 }
12550
12551
12552 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
12553 doc: /* Like `format', but print result to stderr.
12554 usage: (trace-to-stderr STRING &rest OBJECTS) */)
12555 (nargs, args)
12556 int nargs;
12557 Lisp_Object *args;
12558 {
12559 Lisp_Object s = Fformat (nargs, args);
12560 fprintf (stderr, "%s", SDATA (s));
12561 return Qnil;
12562 }
12563
12564 #endif /* GLYPH_DEBUG */
12565
12566
12567 \f
12568 /***********************************************************************
12569 Building Desired Matrix Rows
12570 ***********************************************************************/
12571
12572 /* Return a temporary glyph row holding the glyphs of an overlay
12573 arrow. Only used for non-window-redisplay windows. */
12574
12575 static struct glyph_row *
12576 get_overlay_arrow_glyph_row (w)
12577 struct window *w;
12578 {
12579 struct frame *f = XFRAME (WINDOW_FRAME (w));
12580 struct buffer *buffer = XBUFFER (w->buffer);
12581 struct buffer *old = current_buffer;
12582 const unsigned char *arrow_string = SDATA (Voverlay_arrow_string);
12583 int arrow_len = SCHARS (Voverlay_arrow_string);
12584 const unsigned char *arrow_end = arrow_string + arrow_len;
12585 const unsigned char *p;
12586 struct it it;
12587 int multibyte_p;
12588 int n_glyphs_before;
12589
12590 set_buffer_temp (buffer);
12591 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
12592 it.glyph_row->used[TEXT_AREA] = 0;
12593 SET_TEXT_POS (it.position, 0, 0);
12594
12595 multibyte_p = !NILP (buffer->enable_multibyte_characters);
12596 p = arrow_string;
12597 while (p < arrow_end)
12598 {
12599 Lisp_Object face, ilisp;
12600
12601 /* Get the next character. */
12602 if (multibyte_p)
12603 it.c = string_char_and_length (p, arrow_len, &it.len);
12604 else
12605 it.c = *p, it.len = 1;
12606 p += it.len;
12607
12608 /* Get its face. */
12609 ilisp = make_number (p - arrow_string);
12610 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
12611 it.face_id = compute_char_face (f, it.c, face);
12612
12613 /* Compute its width, get its glyphs. */
12614 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
12615 SET_TEXT_POS (it.position, -1, -1);
12616 PRODUCE_GLYPHS (&it);
12617
12618 /* If this character doesn't fit any more in the line, we have
12619 to remove some glyphs. */
12620 if (it.current_x > it.last_visible_x)
12621 {
12622 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
12623 break;
12624 }
12625 }
12626
12627 set_buffer_temp (old);
12628 return it.glyph_row;
12629 }
12630
12631
12632 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
12633 glyphs are only inserted for terminal frames since we can't really
12634 win with truncation glyphs when partially visible glyphs are
12635 involved. Which glyphs to insert is determined by
12636 produce_special_glyphs. */
12637
12638 static void
12639 insert_left_trunc_glyphs (it)
12640 struct it *it;
12641 {
12642 struct it truncate_it;
12643 struct glyph *from, *end, *to, *toend;
12644
12645 xassert (!FRAME_WINDOW_P (it->f));
12646
12647 /* Get the truncation glyphs. */
12648 truncate_it = *it;
12649 truncate_it.current_x = 0;
12650 truncate_it.face_id = DEFAULT_FACE_ID;
12651 truncate_it.glyph_row = &scratch_glyph_row;
12652 truncate_it.glyph_row->used[TEXT_AREA] = 0;
12653 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
12654 truncate_it.object = make_number (0);
12655 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
12656
12657 /* Overwrite glyphs from IT with truncation glyphs. */
12658 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
12659 end = from + truncate_it.glyph_row->used[TEXT_AREA];
12660 to = it->glyph_row->glyphs[TEXT_AREA];
12661 toend = to + it->glyph_row->used[TEXT_AREA];
12662
12663 while (from < end)
12664 *to++ = *from++;
12665
12666 /* There may be padding glyphs left over. Overwrite them too. */
12667 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
12668 {
12669 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
12670 while (from < end)
12671 *to++ = *from++;
12672 }
12673
12674 if (to > toend)
12675 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
12676 }
12677
12678
12679 /* Compute the pixel height and width of IT->glyph_row.
12680
12681 Most of the time, ascent and height of a display line will be equal
12682 to the max_ascent and max_height values of the display iterator
12683 structure. This is not the case if
12684
12685 1. We hit ZV without displaying anything. In this case, max_ascent
12686 and max_height will be zero.
12687
12688 2. We have some glyphs that don't contribute to the line height.
12689 (The glyph row flag contributes_to_line_height_p is for future
12690 pixmap extensions).
12691
12692 The first case is easily covered by using default values because in
12693 these cases, the line height does not really matter, except that it
12694 must not be zero. */
12695
12696 static void
12697 compute_line_metrics (it)
12698 struct it *it;
12699 {
12700 struct glyph_row *row = it->glyph_row;
12701 int area, i;
12702
12703 if (FRAME_WINDOW_P (it->f))
12704 {
12705 int i, min_y, max_y;
12706
12707 /* The line may consist of one space only, that was added to
12708 place the cursor on it. If so, the row's height hasn't been
12709 computed yet. */
12710 if (row->height == 0)
12711 {
12712 if (it->max_ascent + it->max_descent == 0)
12713 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
12714 row->ascent = it->max_ascent;
12715 row->height = it->max_ascent + it->max_descent;
12716 row->phys_ascent = it->max_phys_ascent;
12717 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
12718 }
12719
12720 /* Compute the width of this line. */
12721 row->pixel_width = row->x;
12722 for (i = 0; i < row->used[TEXT_AREA]; ++i)
12723 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
12724
12725 xassert (row->pixel_width >= 0);
12726 xassert (row->ascent >= 0 && row->height > 0);
12727
12728 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
12729 || MATRIX_ROW_OVERLAPS_PRED_P (row));
12730
12731 /* If first line's physical ascent is larger than its logical
12732 ascent, use the physical ascent, and make the row taller.
12733 This makes accented characters fully visible. */
12734 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
12735 && row->phys_ascent > row->ascent)
12736 {
12737 row->height += row->phys_ascent - row->ascent;
12738 row->ascent = row->phys_ascent;
12739 }
12740
12741 /* Compute how much of the line is visible. */
12742 row->visible_height = row->height;
12743
12744 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (it->w);
12745 max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
12746
12747 if (row->y < min_y)
12748 row->visible_height -= min_y - row->y;
12749 if (row->y + row->height > max_y)
12750 row->visible_height -= row->y + row->height - max_y;
12751 }
12752 else
12753 {
12754 row->pixel_width = row->used[TEXT_AREA];
12755 if (row->continued_p)
12756 row->pixel_width -= it->continuation_pixel_width;
12757 else if (row->truncated_on_right_p)
12758 row->pixel_width -= it->truncation_pixel_width;
12759 row->ascent = row->phys_ascent = 0;
12760 row->height = row->phys_height = row->visible_height = 1;
12761 }
12762
12763 /* Compute a hash code for this row. */
12764 row->hash = 0;
12765 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12766 for (i = 0; i < row->used[area]; ++i)
12767 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
12768 + row->glyphs[area][i].u.val
12769 + row->glyphs[area][i].face_id
12770 + row->glyphs[area][i].padding_p
12771 + (row->glyphs[area][i].type << 2));
12772
12773 it->max_ascent = it->max_descent = 0;
12774 it->max_phys_ascent = it->max_phys_descent = 0;
12775 }
12776
12777
12778 /* Append one space to the glyph row of iterator IT if doing a
12779 window-based redisplay. DEFAULT_FACE_P non-zero means let the
12780 space have the default face, otherwise let it have the same face as
12781 IT->face_id. Value is non-zero if a space was added.
12782
12783 This function is called to make sure that there is always one glyph
12784 at the end of a glyph row that the cursor can be set on under
12785 window-systems. (If there weren't such a glyph we would not know
12786 how wide and tall a box cursor should be displayed).
12787
12788 At the same time this space let's a nicely handle clearing to the
12789 end of the line if the row ends in italic text. */
12790
12791 static int
12792 append_space (it, default_face_p)
12793 struct it *it;
12794 int default_face_p;
12795 {
12796 if (FRAME_WINDOW_P (it->f))
12797 {
12798 int n = it->glyph_row->used[TEXT_AREA];
12799
12800 if (it->glyph_row->glyphs[TEXT_AREA] + n
12801 < it->glyph_row->glyphs[1 + TEXT_AREA])
12802 {
12803 /* Save some values that must not be changed.
12804 Must save IT->c and IT->len because otherwise
12805 ITERATOR_AT_END_P wouldn't work anymore after
12806 append_space has been called. */
12807 enum display_element_type saved_what = it->what;
12808 int saved_c = it->c, saved_len = it->len;
12809 int saved_x = it->current_x;
12810 int saved_face_id = it->face_id;
12811 struct text_pos saved_pos;
12812 Lisp_Object saved_object;
12813 struct face *face;
12814
12815 saved_object = it->object;
12816 saved_pos = it->position;
12817
12818 it->what = IT_CHARACTER;
12819 bzero (&it->position, sizeof it->position);
12820 it->object = make_number (0);
12821 it->c = ' ';
12822 it->len = 1;
12823
12824 if (default_face_p)
12825 it->face_id = DEFAULT_FACE_ID;
12826 else if (it->face_before_selective_p)
12827 it->face_id = it->saved_face_id;
12828 face = FACE_FROM_ID (it->f, it->face_id);
12829 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
12830
12831 PRODUCE_GLYPHS (it);
12832
12833 it->current_x = saved_x;
12834 it->object = saved_object;
12835 it->position = saved_pos;
12836 it->what = saved_what;
12837 it->face_id = saved_face_id;
12838 it->len = saved_len;
12839 it->c = saved_c;
12840 return 1;
12841 }
12842 }
12843
12844 return 0;
12845 }
12846
12847
12848 /* Extend the face of the last glyph in the text area of IT->glyph_row
12849 to the end of the display line. Called from display_line.
12850 If the glyph row is empty, add a space glyph to it so that we
12851 know the face to draw. Set the glyph row flag fill_line_p. */
12852
12853 static void
12854 extend_face_to_end_of_line (it)
12855 struct it *it;
12856 {
12857 struct face *face;
12858 struct frame *f = it->f;
12859
12860 /* If line is already filled, do nothing. */
12861 if (it->current_x >= it->last_visible_x)
12862 return;
12863
12864 /* Face extension extends the background and box of IT->face_id
12865 to the end of the line. If the background equals the background
12866 of the frame, we don't have to do anything. */
12867 if (it->face_before_selective_p)
12868 face = FACE_FROM_ID (it->f, it->saved_face_id);
12869 else
12870 face = FACE_FROM_ID (f, it->face_id);
12871
12872 if (FRAME_WINDOW_P (f)
12873 && face->box == FACE_NO_BOX
12874 && face->background == FRAME_BACKGROUND_PIXEL (f)
12875 && !face->stipple)
12876 return;
12877
12878 /* Set the glyph row flag indicating that the face of the last glyph
12879 in the text area has to be drawn to the end of the text area. */
12880 it->glyph_row->fill_line_p = 1;
12881
12882 /* If current character of IT is not ASCII, make sure we have the
12883 ASCII face. This will be automatically undone the next time
12884 get_next_display_element returns a multibyte character. Note
12885 that the character will always be single byte in unibyte text. */
12886 if (!SINGLE_BYTE_CHAR_P (it->c))
12887 {
12888 it->face_id = FACE_FOR_CHAR (f, face, 0);
12889 }
12890
12891 if (FRAME_WINDOW_P (f))
12892 {
12893 /* If the row is empty, add a space with the current face of IT,
12894 so that we know which face to draw. */
12895 if (it->glyph_row->used[TEXT_AREA] == 0)
12896 {
12897 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
12898 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
12899 it->glyph_row->used[TEXT_AREA] = 1;
12900 }
12901 }
12902 else
12903 {
12904 /* Save some values that must not be changed. */
12905 int saved_x = it->current_x;
12906 struct text_pos saved_pos;
12907 Lisp_Object saved_object;
12908 enum display_element_type saved_what = it->what;
12909 int saved_face_id = it->face_id;
12910
12911 saved_object = it->object;
12912 saved_pos = it->position;
12913
12914 it->what = IT_CHARACTER;
12915 bzero (&it->position, sizeof it->position);
12916 it->object = make_number (0);
12917 it->c = ' ';
12918 it->len = 1;
12919 it->face_id = face->id;
12920
12921 PRODUCE_GLYPHS (it);
12922
12923 while (it->current_x <= it->last_visible_x)
12924 PRODUCE_GLYPHS (it);
12925
12926 /* Don't count these blanks really. It would let us insert a left
12927 truncation glyph below and make us set the cursor on them, maybe. */
12928 it->current_x = saved_x;
12929 it->object = saved_object;
12930 it->position = saved_pos;
12931 it->what = saved_what;
12932 it->face_id = saved_face_id;
12933 }
12934 }
12935
12936
12937 /* Value is non-zero if text starting at CHARPOS in current_buffer is
12938 trailing whitespace. */
12939
12940 static int
12941 trailing_whitespace_p (charpos)
12942 int charpos;
12943 {
12944 int bytepos = CHAR_TO_BYTE (charpos);
12945 int c = 0;
12946
12947 while (bytepos < ZV_BYTE
12948 && (c = FETCH_CHAR (bytepos),
12949 c == ' ' || c == '\t'))
12950 ++bytepos;
12951
12952 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
12953 {
12954 if (bytepos != PT_BYTE)
12955 return 1;
12956 }
12957 return 0;
12958 }
12959
12960
12961 /* Highlight trailing whitespace, if any, in ROW. */
12962
12963 void
12964 highlight_trailing_whitespace (f, row)
12965 struct frame *f;
12966 struct glyph_row *row;
12967 {
12968 int used = row->used[TEXT_AREA];
12969
12970 if (used)
12971 {
12972 struct glyph *start = row->glyphs[TEXT_AREA];
12973 struct glyph *glyph = start + used - 1;
12974
12975 /* Skip over glyphs inserted to display the cursor at the
12976 end of a line, for extending the face of the last glyph
12977 to the end of the line on terminals, and for truncation
12978 and continuation glyphs. */
12979 while (glyph >= start
12980 && glyph->type == CHAR_GLYPH
12981 && INTEGERP (glyph->object))
12982 --glyph;
12983
12984 /* If last glyph is a space or stretch, and it's trailing
12985 whitespace, set the face of all trailing whitespace glyphs in
12986 IT->glyph_row to `trailing-whitespace'. */
12987 if (glyph >= start
12988 && BUFFERP (glyph->object)
12989 && (glyph->type == STRETCH_GLYPH
12990 || (glyph->type == CHAR_GLYPH
12991 && glyph->u.ch == ' '))
12992 && trailing_whitespace_p (glyph->charpos))
12993 {
12994 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
12995
12996 while (glyph >= start
12997 && BUFFERP (glyph->object)
12998 && (glyph->type == STRETCH_GLYPH
12999 || (glyph->type == CHAR_GLYPH
13000 && glyph->u.ch == ' ')))
13001 (glyph--)->face_id = face_id;
13002 }
13003 }
13004 }
13005
13006
13007 /* Value is non-zero if glyph row ROW in window W should be
13008 used to hold the cursor. */
13009
13010 static int
13011 cursor_row_p (w, row)
13012 struct window *w;
13013 struct glyph_row *row;
13014 {
13015 int cursor_row_p = 1;
13016
13017 if (PT == MATRIX_ROW_END_CHARPOS (row))
13018 {
13019 /* If the row ends with a newline from a string, we don't want
13020 the cursor there (if the row is continued it doesn't end in a
13021 newline). */
13022 if (CHARPOS (row->end.string_pos) >= 0
13023 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
13024 cursor_row_p = row->continued_p;
13025
13026 /* If the row ends at ZV, display the cursor at the end of that
13027 row instead of at the start of the row below. */
13028 else if (row->ends_at_zv_p)
13029 cursor_row_p = 1;
13030 else
13031 cursor_row_p = 0;
13032 }
13033
13034 return cursor_row_p;
13035 }
13036
13037
13038 /* Construct the glyph row IT->glyph_row in the desired matrix of
13039 IT->w from text at the current position of IT. See dispextern.h
13040 for an overview of struct it. Value is non-zero if
13041 IT->glyph_row displays text, as opposed to a line displaying ZV
13042 only. */
13043
13044 static int
13045 display_line (it)
13046 struct it *it;
13047 {
13048 struct glyph_row *row = it->glyph_row;
13049
13050 /* We always start displaying at hpos zero even if hscrolled. */
13051 xassert (it->hpos == 0 && it->current_x == 0);
13052
13053 /* We must not display in a row that's not a text row. */
13054 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
13055 < it->w->desired_matrix->nrows);
13056
13057 /* Is IT->w showing the region? */
13058 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
13059
13060 /* Clear the result glyph row and enable it. */
13061 prepare_desired_row (row);
13062
13063 row->y = it->current_y;
13064 row->start = it->current;
13065 row->continuation_lines_width = it->continuation_lines_width;
13066 row->displays_text_p = 1;
13067 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
13068 it->starts_in_middle_of_char_p = 0;
13069
13070 /* Arrange the overlays nicely for our purposes. Usually, we call
13071 display_line on only one line at a time, in which case this
13072 can't really hurt too much, or we call it on lines which appear
13073 one after another in the buffer, in which case all calls to
13074 recenter_overlay_lists but the first will be pretty cheap. */
13075 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
13076
13077 /* Move over display elements that are not visible because we are
13078 hscrolled. This may stop at an x-position < IT->first_visible_x
13079 if the first glyph is partially visible or if we hit a line end. */
13080 if (it->current_x < it->first_visible_x)
13081 move_it_in_display_line_to (it, ZV, it->first_visible_x,
13082 MOVE_TO_POS | MOVE_TO_X);
13083
13084 /* Get the initial row height. This is either the height of the
13085 text hscrolled, if there is any, or zero. */
13086 row->ascent = it->max_ascent;
13087 row->height = it->max_ascent + it->max_descent;
13088 row->phys_ascent = it->max_phys_ascent;
13089 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
13090
13091 /* Loop generating characters. The loop is left with IT on the next
13092 character to display. */
13093 while (1)
13094 {
13095 int n_glyphs_before, hpos_before, x_before;
13096 int x, i, nglyphs;
13097 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
13098
13099 /* Retrieve the next thing to display. Value is zero if end of
13100 buffer reached. */
13101 if (!get_next_display_element (it))
13102 {
13103 /* Maybe add a space at the end of this line that is used to
13104 display the cursor there under X. Set the charpos of the
13105 first glyph of blank lines not corresponding to any text
13106 to -1. */
13107 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
13108 || row->used[TEXT_AREA] == 0)
13109 {
13110 row->glyphs[TEXT_AREA]->charpos = -1;
13111 row->displays_text_p = 0;
13112
13113 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
13114 && (!MINI_WINDOW_P (it->w)
13115 || (minibuf_level && EQ (it->window, minibuf_window))))
13116 row->indicate_empty_line_p = 1;
13117 }
13118
13119 it->continuation_lines_width = 0;
13120 row->ends_at_zv_p = 1;
13121 break;
13122 }
13123
13124 /* Now, get the metrics of what we want to display. This also
13125 generates glyphs in `row' (which is IT->glyph_row). */
13126 n_glyphs_before = row->used[TEXT_AREA];
13127 x = it->current_x;
13128
13129 /* Remember the line height so far in case the next element doesn't
13130 fit on the line. */
13131 if (!it->truncate_lines_p)
13132 {
13133 ascent = it->max_ascent;
13134 descent = it->max_descent;
13135 phys_ascent = it->max_phys_ascent;
13136 phys_descent = it->max_phys_descent;
13137 }
13138
13139 PRODUCE_GLYPHS (it);
13140
13141 /* If this display element was in marginal areas, continue with
13142 the next one. */
13143 if (it->area != TEXT_AREA)
13144 {
13145 row->ascent = max (row->ascent, it->max_ascent);
13146 row->height = max (row->height, it->max_ascent + it->max_descent);
13147 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13148 row->phys_height = max (row->phys_height,
13149 it->max_phys_ascent + it->max_phys_descent);
13150 set_iterator_to_next (it, 1);
13151 continue;
13152 }
13153
13154 /* Does the display element fit on the line? If we truncate
13155 lines, we should draw past the right edge of the window. If
13156 we don't truncate, we want to stop so that we can display the
13157 continuation glyph before the right margin. If lines are
13158 continued, there are two possible strategies for characters
13159 resulting in more than 1 glyph (e.g. tabs): Display as many
13160 glyphs as possible in this line and leave the rest for the
13161 continuation line, or display the whole element in the next
13162 line. Original redisplay did the former, so we do it also. */
13163 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
13164 hpos_before = it->hpos;
13165 x_before = x;
13166
13167 if (/* Not a newline. */
13168 nglyphs > 0
13169 /* Glyphs produced fit entirely in the line. */
13170 && it->current_x < it->last_visible_x)
13171 {
13172 it->hpos += nglyphs;
13173 row->ascent = max (row->ascent, it->max_ascent);
13174 row->height = max (row->height, it->max_ascent + it->max_descent);
13175 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13176 row->phys_height = max (row->phys_height,
13177 it->max_phys_ascent + it->max_phys_descent);
13178 if (it->current_x - it->pixel_width < it->first_visible_x)
13179 row->x = x - it->first_visible_x;
13180 }
13181 else
13182 {
13183 int new_x;
13184 struct glyph *glyph;
13185
13186 for (i = 0; i < nglyphs; ++i, x = new_x)
13187 {
13188 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
13189 new_x = x + glyph->pixel_width;
13190
13191 if (/* Lines are continued. */
13192 !it->truncate_lines_p
13193 && (/* Glyph doesn't fit on the line. */
13194 new_x > it->last_visible_x
13195 /* Or it fits exactly on a window system frame. */
13196 || (new_x == it->last_visible_x
13197 && FRAME_WINDOW_P (it->f))))
13198 {
13199 /* End of a continued line. */
13200
13201 if (it->hpos == 0
13202 || (new_x == it->last_visible_x
13203 && FRAME_WINDOW_P (it->f)))
13204 {
13205 /* Current glyph is the only one on the line or
13206 fits exactly on the line. We must continue
13207 the line because we can't draw the cursor
13208 after the glyph. */
13209 row->continued_p = 1;
13210 it->current_x = new_x;
13211 it->continuation_lines_width += new_x;
13212 ++it->hpos;
13213 if (i == nglyphs - 1)
13214 set_iterator_to_next (it, 1);
13215 }
13216 else if (CHAR_GLYPH_PADDING_P (*glyph)
13217 && !FRAME_WINDOW_P (it->f))
13218 {
13219 /* A padding glyph that doesn't fit on this line.
13220 This means the whole character doesn't fit
13221 on the line. */
13222 row->used[TEXT_AREA] = n_glyphs_before;
13223
13224 /* Fill the rest of the row with continuation
13225 glyphs like in 20.x. */
13226 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
13227 < row->glyphs[1 + TEXT_AREA])
13228 produce_special_glyphs (it, IT_CONTINUATION);
13229
13230 row->continued_p = 1;
13231 it->current_x = x_before;
13232 it->continuation_lines_width += x_before;
13233
13234 /* Restore the height to what it was before the
13235 element not fitting on the line. */
13236 it->max_ascent = ascent;
13237 it->max_descent = descent;
13238 it->max_phys_ascent = phys_ascent;
13239 it->max_phys_descent = phys_descent;
13240 }
13241 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
13242 {
13243 /* A TAB that extends past the right edge of the
13244 window. This produces a single glyph on
13245 window system frames. We leave the glyph in
13246 this row and let it fill the row, but don't
13247 consume the TAB. */
13248 it->continuation_lines_width += it->last_visible_x;
13249 row->ends_in_middle_of_char_p = 1;
13250 row->continued_p = 1;
13251 glyph->pixel_width = it->last_visible_x - x;
13252 it->starts_in_middle_of_char_p = 1;
13253 }
13254 else
13255 {
13256 /* Something other than a TAB that draws past
13257 the right edge of the window. Restore
13258 positions to values before the element. */
13259 row->used[TEXT_AREA] = n_glyphs_before + i;
13260
13261 /* Display continuation glyphs. */
13262 if (!FRAME_WINDOW_P (it->f))
13263 produce_special_glyphs (it, IT_CONTINUATION);
13264 row->continued_p = 1;
13265
13266 it->continuation_lines_width += x;
13267
13268 if (nglyphs > 1 && i > 0)
13269 {
13270 row->ends_in_middle_of_char_p = 1;
13271 it->starts_in_middle_of_char_p = 1;
13272 }
13273
13274 /* Restore the height to what it was before the
13275 element not fitting on the line. */
13276 it->max_ascent = ascent;
13277 it->max_descent = descent;
13278 it->max_phys_ascent = phys_ascent;
13279 it->max_phys_descent = phys_descent;
13280 }
13281
13282 break;
13283 }
13284 else if (new_x > it->first_visible_x)
13285 {
13286 /* Increment number of glyphs actually displayed. */
13287 ++it->hpos;
13288
13289 if (x < it->first_visible_x)
13290 /* Glyph is partially visible, i.e. row starts at
13291 negative X position. */
13292 row->x = x - it->first_visible_x;
13293 }
13294 else
13295 {
13296 /* Glyph is completely off the left margin of the
13297 window. This should not happen because of the
13298 move_it_in_display_line at the start of this
13299 function, unless the text display area of the
13300 window is empty. */
13301 xassert (it->first_visible_x <= it->last_visible_x);
13302 }
13303 }
13304
13305 row->ascent = max (row->ascent, it->max_ascent);
13306 row->height = max (row->height, it->max_ascent + it->max_descent);
13307 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13308 row->phys_height = max (row->phys_height,
13309 it->max_phys_ascent + it->max_phys_descent);
13310
13311 /* End of this display line if row is continued. */
13312 if (row->continued_p)
13313 break;
13314 }
13315
13316 /* Is this a line end? If yes, we're also done, after making
13317 sure that a non-default face is extended up to the right
13318 margin of the window. */
13319 if (ITERATOR_AT_END_OF_LINE_P (it))
13320 {
13321 int used_before = row->used[TEXT_AREA];
13322
13323 row->ends_in_newline_from_string_p = STRINGP (it->object);
13324
13325 /* Add a space at the end of the line that is used to
13326 display the cursor there. */
13327 append_space (it, 0);
13328
13329 /* Extend the face to the end of the line. */
13330 extend_face_to_end_of_line (it);
13331
13332 /* Make sure we have the position. */
13333 if (used_before == 0)
13334 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
13335
13336 /* Consume the line end. This skips over invisible lines. */
13337 set_iterator_to_next (it, 1);
13338 it->continuation_lines_width = 0;
13339 break;
13340 }
13341
13342 /* Proceed with next display element. Note that this skips
13343 over lines invisible because of selective display. */
13344 set_iterator_to_next (it, 1);
13345
13346 /* If we truncate lines, we are done when the last displayed
13347 glyphs reach past the right margin of the window. */
13348 if (it->truncate_lines_p
13349 && (FRAME_WINDOW_P (it->f)
13350 ? (it->current_x >= it->last_visible_x)
13351 : (it->current_x > it->last_visible_x)))
13352 {
13353 /* Maybe add truncation glyphs. */
13354 if (!FRAME_WINDOW_P (it->f))
13355 {
13356 int i, n;
13357
13358 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
13359 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
13360 break;
13361
13362 for (n = row->used[TEXT_AREA]; i < n; ++i)
13363 {
13364 row->used[TEXT_AREA] = i;
13365 produce_special_glyphs (it, IT_TRUNCATION);
13366 }
13367 }
13368
13369 row->truncated_on_right_p = 1;
13370 it->continuation_lines_width = 0;
13371 reseat_at_next_visible_line_start (it, 0);
13372 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
13373 it->hpos = hpos_before;
13374 it->current_x = x_before;
13375 break;
13376 }
13377 }
13378
13379 /* If line is not empty and hscrolled, maybe insert truncation glyphs
13380 at the left window margin. */
13381 if (it->first_visible_x
13382 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
13383 {
13384 if (!FRAME_WINDOW_P (it->f))
13385 insert_left_trunc_glyphs (it);
13386 row->truncated_on_left_p = 1;
13387 }
13388
13389 /* If the start of this line is the overlay arrow-position, then
13390 mark this glyph row as the one containing the overlay arrow.
13391 This is clearly a mess with variable size fonts. It would be
13392 better to let it be displayed like cursors under X. */
13393 if (MARKERP (Voverlay_arrow_position)
13394 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
13395 && (MATRIX_ROW_START_CHARPOS (row)
13396 == marker_position (Voverlay_arrow_position))
13397 && STRINGP (Voverlay_arrow_string)
13398 && ! overlay_arrow_seen)
13399 {
13400 /* Overlay arrow in window redisplay is a fringe bitmap. */
13401 if (!FRAME_WINDOW_P (it->f))
13402 {
13403 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
13404 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
13405 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
13406 struct glyph *p = row->glyphs[TEXT_AREA];
13407 struct glyph *p2, *end;
13408
13409 /* Copy the arrow glyphs. */
13410 while (glyph < arrow_end)
13411 *p++ = *glyph++;
13412
13413 /* Throw away padding glyphs. */
13414 p2 = p;
13415 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
13416 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
13417 ++p2;
13418 if (p2 > p)
13419 {
13420 while (p2 < end)
13421 *p++ = *p2++;
13422 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
13423 }
13424 }
13425
13426 overlay_arrow_seen = 1;
13427 row->overlay_arrow_p = 1;
13428 }
13429
13430 /* Compute pixel dimensions of this line. */
13431 compute_line_metrics (it);
13432
13433 /* Remember the position at which this line ends. */
13434 row->end = it->current;
13435
13436 /* Maybe set the cursor. */
13437 if (it->w->cursor.vpos < 0
13438 && PT >= MATRIX_ROW_START_CHARPOS (row)
13439 && PT <= MATRIX_ROW_END_CHARPOS (row)
13440 && cursor_row_p (it->w, row))
13441 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
13442
13443 /* Highlight trailing whitespace. */
13444 if (!NILP (Vshow_trailing_whitespace))
13445 highlight_trailing_whitespace (it->f, it->glyph_row);
13446
13447 /* Prepare for the next line. This line starts horizontally at (X
13448 HPOS) = (0 0). Vertical positions are incremented. As a
13449 convenience for the caller, IT->glyph_row is set to the next
13450 row to be used. */
13451 it->current_x = it->hpos = 0;
13452 it->current_y += row->height;
13453 ++it->vpos;
13454 ++it->glyph_row;
13455 return row->displays_text_p;
13456 }
13457
13458
13459 \f
13460 /***********************************************************************
13461 Menu Bar
13462 ***********************************************************************/
13463
13464 /* Redisplay the menu bar in the frame for window W.
13465
13466 The menu bar of X frames that don't have X toolkit support is
13467 displayed in a special window W->frame->menu_bar_window.
13468
13469 The menu bar of terminal frames is treated specially as far as
13470 glyph matrices are concerned. Menu bar lines are not part of
13471 windows, so the update is done directly on the frame matrix rows
13472 for the menu bar. */
13473
13474 static void
13475 display_menu_bar (w)
13476 struct window *w;
13477 {
13478 struct frame *f = XFRAME (WINDOW_FRAME (w));
13479 struct it it;
13480 Lisp_Object items;
13481 int i;
13482
13483 /* Don't do all this for graphical frames. */
13484 #ifdef HAVE_NTGUI
13485 if (!NILP (Vwindow_system))
13486 return;
13487 #endif
13488 #ifdef USE_X_TOOLKIT
13489 if (FRAME_X_P (f))
13490 return;
13491 #endif
13492 #ifdef MAC_OS
13493 if (FRAME_MAC_P (f))
13494 return;
13495 #endif
13496
13497 #ifdef USE_X_TOOLKIT
13498 xassert (!FRAME_WINDOW_P (f));
13499 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
13500 it.first_visible_x = 0;
13501 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
13502 #else /* not USE_X_TOOLKIT */
13503 if (FRAME_WINDOW_P (f))
13504 {
13505 /* Menu bar lines are displayed in the desired matrix of the
13506 dummy window menu_bar_window. */
13507 struct window *menu_w;
13508 xassert (WINDOWP (f->menu_bar_window));
13509 menu_w = XWINDOW (f->menu_bar_window);
13510 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
13511 MENU_FACE_ID);
13512 it.first_visible_x = 0;
13513 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
13514 }
13515 else
13516 {
13517 /* This is a TTY frame, i.e. character hpos/vpos are used as
13518 pixel x/y. */
13519 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
13520 MENU_FACE_ID);
13521 it.first_visible_x = 0;
13522 it.last_visible_x = FRAME_WIDTH (f);
13523 }
13524 #endif /* not USE_X_TOOLKIT */
13525
13526 if (! mode_line_inverse_video)
13527 /* Force the menu-bar to be displayed in the default face. */
13528 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
13529
13530 /* Clear all rows of the menu bar. */
13531 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
13532 {
13533 struct glyph_row *row = it.glyph_row + i;
13534 clear_glyph_row (row);
13535 row->enabled_p = 1;
13536 row->full_width_p = 1;
13537 }
13538
13539 /* Display all items of the menu bar. */
13540 items = FRAME_MENU_BAR_ITEMS (it.f);
13541 for (i = 0; i < XVECTOR (items)->size; i += 4)
13542 {
13543 Lisp_Object string;
13544
13545 /* Stop at nil string. */
13546 string = AREF (items, i + 1);
13547 if (NILP (string))
13548 break;
13549
13550 /* Remember where item was displayed. */
13551 AREF (items, i + 3) = make_number (it.hpos);
13552
13553 /* Display the item, pad with one space. */
13554 if (it.current_x < it.last_visible_x)
13555 display_string (NULL, string, Qnil, 0, 0, &it,
13556 SCHARS (string) + 1, 0, 0, -1);
13557 }
13558
13559 /* Fill out the line with spaces. */
13560 if (it.current_x < it.last_visible_x)
13561 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
13562
13563 /* Compute the total height of the lines. */
13564 compute_line_metrics (&it);
13565 }
13566
13567
13568 \f
13569 /***********************************************************************
13570 Mode Line
13571 ***********************************************************************/
13572
13573 /* Redisplay mode lines in the window tree whose root is WINDOW. If
13574 FORCE is non-zero, redisplay mode lines unconditionally.
13575 Otherwise, redisplay only mode lines that are garbaged. Value is
13576 the number of windows whose mode lines were redisplayed. */
13577
13578 static int
13579 redisplay_mode_lines (window, force)
13580 Lisp_Object window;
13581 int force;
13582 {
13583 int nwindows = 0;
13584
13585 while (!NILP (window))
13586 {
13587 struct window *w = XWINDOW (window);
13588
13589 if (WINDOWP (w->hchild))
13590 nwindows += redisplay_mode_lines (w->hchild, force);
13591 else if (WINDOWP (w->vchild))
13592 nwindows += redisplay_mode_lines (w->vchild, force);
13593 else if (force
13594 || FRAME_GARBAGED_P (XFRAME (w->frame))
13595 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
13596 {
13597 struct text_pos lpoint;
13598 struct buffer *old = current_buffer;
13599
13600 /* Set the window's buffer for the mode line display. */
13601 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13602 set_buffer_internal_1 (XBUFFER (w->buffer));
13603
13604 /* Point refers normally to the selected window. For any
13605 other window, set up appropriate value. */
13606 if (!EQ (window, selected_window))
13607 {
13608 struct text_pos pt;
13609
13610 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
13611 if (CHARPOS (pt) < BEGV)
13612 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
13613 else if (CHARPOS (pt) > (ZV - 1))
13614 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
13615 else
13616 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
13617 }
13618
13619 /* Display mode lines. */
13620 clear_glyph_matrix (w->desired_matrix);
13621 if (display_mode_lines (w))
13622 {
13623 ++nwindows;
13624 w->must_be_updated_p = 1;
13625 }
13626
13627 /* Restore old settings. */
13628 set_buffer_internal_1 (old);
13629 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13630 }
13631
13632 window = w->next;
13633 }
13634
13635 return nwindows;
13636 }
13637
13638
13639 /* Display the mode and/or top line of window W. Value is the number
13640 of mode lines displayed. */
13641
13642 static int
13643 display_mode_lines (w)
13644 struct window *w;
13645 {
13646 Lisp_Object old_selected_window, old_selected_frame;
13647 int n = 0;
13648
13649 old_selected_frame = selected_frame;
13650 selected_frame = w->frame;
13651 old_selected_window = selected_window;
13652 XSETWINDOW (selected_window, w);
13653
13654 /* These will be set while the mode line specs are processed. */
13655 line_number_displayed = 0;
13656 w->column_number_displayed = Qnil;
13657
13658 if (WINDOW_WANTS_MODELINE_P (w))
13659 {
13660 struct window *sel_w = XWINDOW (old_selected_window);
13661
13662 /* Select mode line face based on the real selected window. */
13663 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
13664 current_buffer->mode_line_format);
13665 ++n;
13666 }
13667
13668 if (WINDOW_WANTS_HEADER_LINE_P (w))
13669 {
13670 display_mode_line (w, HEADER_LINE_FACE_ID,
13671 current_buffer->header_line_format);
13672 ++n;
13673 }
13674
13675 selected_frame = old_selected_frame;
13676 selected_window = old_selected_window;
13677 return n;
13678 }
13679
13680
13681 /* Display mode or top line of window W. FACE_ID specifies which line
13682 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
13683 FORMAT is the mode line format to display. Value is the pixel
13684 height of the mode line displayed. */
13685
13686 static int
13687 display_mode_line (w, face_id, format)
13688 struct window *w;
13689 enum face_id face_id;
13690 Lisp_Object format;
13691 {
13692 struct it it;
13693 struct face *face;
13694
13695 init_iterator (&it, w, -1, -1, NULL, face_id);
13696 prepare_desired_row (it.glyph_row);
13697
13698 if (! mode_line_inverse_video)
13699 /* Force the mode-line to be displayed in the default face. */
13700 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
13701
13702 /* Temporarily make frame's keyboard the current kboard so that
13703 kboard-local variables in the mode_line_format will get the right
13704 values. */
13705 push_frame_kboard (it.f);
13706 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
13707 pop_frame_kboard ();
13708
13709 /* Fill up with spaces. */
13710 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
13711
13712 compute_line_metrics (&it);
13713 it.glyph_row->full_width_p = 1;
13714 it.glyph_row->mode_line_p = 1;
13715 it.glyph_row->continued_p = 0;
13716 it.glyph_row->truncated_on_left_p = 0;
13717 it.glyph_row->truncated_on_right_p = 0;
13718
13719 /* Make a 3D mode-line have a shadow at its right end. */
13720 face = FACE_FROM_ID (it.f, face_id);
13721 extend_face_to_end_of_line (&it);
13722 if (face->box != FACE_NO_BOX)
13723 {
13724 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
13725 + it.glyph_row->used[TEXT_AREA] - 1);
13726 last->right_box_line_p = 1;
13727 }
13728
13729 return it.glyph_row->height;
13730 }
13731
13732 /* Alist that caches the results of :propertize.
13733 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
13734 Lisp_Object mode_line_proptrans_alist;
13735
13736 /* List of strings making up the mode-line. */
13737 Lisp_Object mode_line_string_list;
13738
13739 /* Base face property when building propertized mode line string. */
13740 static Lisp_Object mode_line_string_face;
13741 static Lisp_Object mode_line_string_face_prop;
13742
13743
13744 /* Contribute ELT to the mode line for window IT->w. How it
13745 translates into text depends on its data type.
13746
13747 IT describes the display environment in which we display, as usual.
13748
13749 DEPTH is the depth in recursion. It is used to prevent
13750 infinite recursion here.
13751
13752 FIELD_WIDTH is the number of characters the display of ELT should
13753 occupy in the mode line, and PRECISION is the maximum number of
13754 characters to display from ELT's representation. See
13755 display_string for details.
13756
13757 Returns the hpos of the end of the text generated by ELT.
13758
13759 PROPS is a property list to add to any string we encounter.
13760
13761 If RISKY is nonzero, remove (disregard) any properties in any string
13762 we encounter, and ignore :eval and :propertize.
13763
13764 If the global variable `frame_title_ptr' is non-NULL, then the output
13765 is passed to `store_frame_title' instead of `display_string'. */
13766
13767 static int
13768 display_mode_element (it, depth, field_width, precision, elt, props, risky)
13769 struct it *it;
13770 int depth;
13771 int field_width, precision;
13772 Lisp_Object elt, props;
13773 int risky;
13774 {
13775 int n = 0, field, prec;
13776 int literal = 0;
13777
13778 tail_recurse:
13779 if (depth > 10)
13780 goto invalid;
13781
13782 depth++;
13783
13784 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
13785 {
13786 case Lisp_String:
13787 {
13788 /* A string: output it and check for %-constructs within it. */
13789 unsigned char c;
13790 const unsigned char *this, *lisp_string;
13791
13792 if (!NILP (props) || risky)
13793 {
13794 Lisp_Object oprops, aelt;
13795 oprops = Ftext_properties_at (make_number (0), elt);
13796
13797 if (NILP (Fequal (props, oprops)) || risky)
13798 {
13799 /* If the starting string has properties,
13800 merge the specified ones onto the existing ones. */
13801 if (! NILP (oprops) && !risky)
13802 {
13803 Lisp_Object tem;
13804
13805 oprops = Fcopy_sequence (oprops);
13806 tem = props;
13807 while (CONSP (tem))
13808 {
13809 oprops = Fplist_put (oprops, XCAR (tem),
13810 XCAR (XCDR (tem)));
13811 tem = XCDR (XCDR (tem));
13812 }
13813 props = oprops;
13814 }
13815
13816 aelt = Fassoc (elt, mode_line_proptrans_alist);
13817 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
13818 {
13819 mode_line_proptrans_alist
13820 = Fcons (aelt, Fdelq (aelt, mode_line_proptrans_alist));
13821 elt = XCAR (aelt);
13822 }
13823 else
13824 {
13825 Lisp_Object tem;
13826
13827 elt = Fcopy_sequence (elt);
13828 Fset_text_properties (make_number (0), Flength (elt),
13829 props, elt);
13830 /* Add this item to mode_line_proptrans_alist. */
13831 mode_line_proptrans_alist
13832 = Fcons (Fcons (elt, props),
13833 mode_line_proptrans_alist);
13834 /* Truncate mode_line_proptrans_alist
13835 to at most 50 elements. */
13836 tem = Fnthcdr (make_number (50),
13837 mode_line_proptrans_alist);
13838 if (! NILP (tem))
13839 XSETCDR (tem, Qnil);
13840 }
13841 }
13842 }
13843
13844 this = SDATA (elt);
13845 lisp_string = this;
13846
13847 if (literal)
13848 {
13849 prec = precision - n;
13850 if (frame_title_ptr)
13851 n += store_frame_title (SDATA (elt), -1, prec);
13852 else if (!NILP (mode_line_string_list))
13853 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
13854 else
13855 n += display_string (NULL, elt, Qnil, 0, 0, it,
13856 0, prec, 0, STRING_MULTIBYTE (elt));
13857
13858 break;
13859 }
13860
13861 while ((precision <= 0 || n < precision)
13862 && *this
13863 && (frame_title_ptr
13864 || !NILP (mode_line_string_list)
13865 || it->current_x < it->last_visible_x))
13866 {
13867 const unsigned char *last = this;
13868
13869 /* Advance to end of string or next format specifier. */
13870 while ((c = *this++) != '\0' && c != '%')
13871 ;
13872
13873 if (this - 1 != last)
13874 {
13875 /* Output to end of string or up to '%'. Field width
13876 is length of string. Don't output more than
13877 PRECISION allows us. */
13878 --this;
13879
13880 prec = chars_in_text (last, this - last);
13881 if (precision > 0 && prec > precision - n)
13882 prec = precision - n;
13883
13884 if (frame_title_ptr)
13885 n += store_frame_title (last, 0, prec);
13886 else if (!NILP (mode_line_string_list))
13887 {
13888 int bytepos = last - lisp_string;
13889 int charpos = string_byte_to_char (elt, bytepos);
13890 n += store_mode_line_string (NULL,
13891 Fsubstring (elt, make_number (charpos),
13892 make_number (charpos + prec)),
13893 0, 0, 0, Qnil);
13894 }
13895 else
13896 {
13897 int bytepos = last - lisp_string;
13898 int charpos = string_byte_to_char (elt, bytepos);
13899 n += display_string (NULL, elt, Qnil, 0, charpos,
13900 it, 0, prec, 0,
13901 STRING_MULTIBYTE (elt));
13902 }
13903 }
13904 else /* c == '%' */
13905 {
13906 const unsigned char *percent_position = this;
13907
13908 /* Get the specified minimum width. Zero means
13909 don't pad. */
13910 field = 0;
13911 while ((c = *this++) >= '0' && c <= '9')
13912 field = field * 10 + c - '0';
13913
13914 /* Don't pad beyond the total padding allowed. */
13915 if (field_width - n > 0 && field > field_width - n)
13916 field = field_width - n;
13917
13918 /* Note that either PRECISION <= 0 or N < PRECISION. */
13919 prec = precision - n;
13920
13921 if (c == 'M')
13922 n += display_mode_element (it, depth, field, prec,
13923 Vglobal_mode_string, props,
13924 risky);
13925 else if (c != 0)
13926 {
13927 int multibyte;
13928 int bytepos, charpos;
13929 unsigned char *spec;
13930
13931 bytepos = percent_position - lisp_string;
13932 charpos = (STRING_MULTIBYTE (elt)
13933 ? string_byte_to_char (elt, bytepos)
13934 : bytepos);
13935
13936 spec
13937 = decode_mode_spec (it->w, c, field, prec, &multibyte);
13938
13939 if (frame_title_ptr)
13940 n += store_frame_title (spec, field, prec);
13941 else if (!NILP (mode_line_string_list))
13942 {
13943 int len = strlen (spec);
13944 Lisp_Object tem = make_string (spec, len);
13945 props = Ftext_properties_at (make_number (charpos), elt);
13946 /* Should only keep face property in props */
13947 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
13948 }
13949 else
13950 {
13951 int nglyphs_before, nwritten;
13952
13953 nglyphs_before = it->glyph_row->used[TEXT_AREA];
13954 nwritten = display_string (spec, Qnil, elt,
13955 charpos, 0, it,
13956 field, prec, 0,
13957 multibyte);
13958
13959 /* Assign to the glyphs written above the
13960 string where the `%x' came from, position
13961 of the `%'. */
13962 if (nwritten > 0)
13963 {
13964 struct glyph *glyph
13965 = (it->glyph_row->glyphs[TEXT_AREA]
13966 + nglyphs_before);
13967 int i;
13968
13969 for (i = 0; i < nwritten; ++i)
13970 {
13971 glyph[i].object = elt;
13972 glyph[i].charpos = charpos;
13973 }
13974
13975 n += nwritten;
13976 }
13977 }
13978 }
13979 else /* c == 0 */
13980 break;
13981 }
13982 }
13983 }
13984 break;
13985
13986 case Lisp_Symbol:
13987 /* A symbol: process the value of the symbol recursively
13988 as if it appeared here directly. Avoid error if symbol void.
13989 Special case: if value of symbol is a string, output the string
13990 literally. */
13991 {
13992 register Lisp_Object tem;
13993
13994 /* If the variable is not marked as risky to set
13995 then its contents are risky to use. */
13996 if (NILP (Fget (elt, Qrisky_local_variable)))
13997 risky = 1;
13998
13999 tem = Fboundp (elt);
14000 if (!NILP (tem))
14001 {
14002 tem = Fsymbol_value (elt);
14003 /* If value is a string, output that string literally:
14004 don't check for % within it. */
14005 if (STRINGP (tem))
14006 literal = 1;
14007
14008 if (!EQ (tem, elt))
14009 {
14010 /* Give up right away for nil or t. */
14011 elt = tem;
14012 goto tail_recurse;
14013 }
14014 }
14015 }
14016 break;
14017
14018 case Lisp_Cons:
14019 {
14020 register Lisp_Object car, tem;
14021
14022 /* A cons cell: five distinct cases.
14023 If first element is :eval or :propertize, do something special.
14024 If first element is a string or a cons, process all the elements
14025 and effectively concatenate them.
14026 If first element is a negative number, truncate displaying cdr to
14027 at most that many characters. If positive, pad (with spaces)
14028 to at least that many characters.
14029 If first element is a symbol, process the cadr or caddr recursively
14030 according to whether the symbol's value is non-nil or nil. */
14031 car = XCAR (elt);
14032 if (EQ (car, QCeval))
14033 {
14034 /* An element of the form (:eval FORM) means evaluate FORM
14035 and use the result as mode line elements. */
14036
14037 if (risky)
14038 break;
14039
14040 if (CONSP (XCDR (elt)))
14041 {
14042 Lisp_Object spec;
14043 spec = safe_eval (XCAR (XCDR (elt)));
14044 n += display_mode_element (it, depth, field_width - n,
14045 precision - n, spec, props,
14046 risky);
14047 }
14048 }
14049 else if (EQ (car, QCpropertize))
14050 {
14051 /* An element of the form (:propertize ELT PROPS...)
14052 means display ELT but applying properties PROPS. */
14053
14054 if (risky)
14055 break;
14056
14057 if (CONSP (XCDR (elt)))
14058 n += display_mode_element (it, depth, field_width - n,
14059 precision - n, XCAR (XCDR (elt)),
14060 XCDR (XCDR (elt)), risky);
14061 }
14062 else if (SYMBOLP (car))
14063 {
14064 tem = Fboundp (car);
14065 elt = XCDR (elt);
14066 if (!CONSP (elt))
14067 goto invalid;
14068 /* elt is now the cdr, and we know it is a cons cell.
14069 Use its car if CAR has a non-nil value. */
14070 if (!NILP (tem))
14071 {
14072 tem = Fsymbol_value (car);
14073 if (!NILP (tem))
14074 {
14075 elt = XCAR (elt);
14076 goto tail_recurse;
14077 }
14078 }
14079 /* Symbol's value is nil (or symbol is unbound)
14080 Get the cddr of the original list
14081 and if possible find the caddr and use that. */
14082 elt = XCDR (elt);
14083 if (NILP (elt))
14084 break;
14085 else if (!CONSP (elt))
14086 goto invalid;
14087 elt = XCAR (elt);
14088 goto tail_recurse;
14089 }
14090 else if (INTEGERP (car))
14091 {
14092 register int lim = XINT (car);
14093 elt = XCDR (elt);
14094 if (lim < 0)
14095 {
14096 /* Negative int means reduce maximum width. */
14097 if (precision <= 0)
14098 precision = -lim;
14099 else
14100 precision = min (precision, -lim);
14101 }
14102 else if (lim > 0)
14103 {
14104 /* Padding specified. Don't let it be more than
14105 current maximum. */
14106 if (precision > 0)
14107 lim = min (precision, lim);
14108
14109 /* If that's more padding than already wanted, queue it.
14110 But don't reduce padding already specified even if
14111 that is beyond the current truncation point. */
14112 field_width = max (lim, field_width);
14113 }
14114 goto tail_recurse;
14115 }
14116 else if (STRINGP (car) || CONSP (car))
14117 {
14118 register int limit = 50;
14119 /* Limit is to protect against circular lists. */
14120 while (CONSP (elt)
14121 && --limit > 0
14122 && (precision <= 0 || n < precision))
14123 {
14124 n += display_mode_element (it, depth, field_width - n,
14125 precision - n, XCAR (elt),
14126 props, risky);
14127 elt = XCDR (elt);
14128 }
14129 }
14130 }
14131 break;
14132
14133 default:
14134 invalid:
14135 if (frame_title_ptr)
14136 n += store_frame_title ("*invalid*", 0, precision - n);
14137 else if (!NILP (mode_line_string_list))
14138 n += store_mode_line_string ("*invalid*", Qnil, 0, 0, precision - n, Qnil);
14139 else
14140 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
14141 precision - n, 0, 0);
14142 return n;
14143 }
14144
14145 /* Pad to FIELD_WIDTH. */
14146 if (field_width > 0 && n < field_width)
14147 {
14148 if (frame_title_ptr)
14149 n += store_frame_title ("", field_width - n, 0);
14150 else if (!NILP (mode_line_string_list))
14151 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
14152 else
14153 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
14154 0, 0, 0);
14155 }
14156
14157 return n;
14158 }
14159
14160 /* Store a mode-line string element in mode_line_string_list.
14161
14162 If STRING is non-null, display that C string. Otherwise, the Lisp
14163 string LISP_STRING is displayed.
14164
14165 FIELD_WIDTH is the minimum number of output glyphs to produce.
14166 If STRING has fewer characters than FIELD_WIDTH, pad to the right
14167 with spaces. FIELD_WIDTH <= 0 means don't pad.
14168
14169 PRECISION is the maximum number of characters to output from
14170 STRING. PRECISION <= 0 means don't truncate the string.
14171
14172 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
14173 properties to the string.
14174
14175 PROPS are the properties to add to the string.
14176 The mode_line_string_face face property is always added to the string.
14177 */
14178
14179 static int store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
14180 char *string;
14181 Lisp_Object lisp_string;
14182 int copy_string;
14183 int field_width;
14184 int precision;
14185 Lisp_Object props;
14186 {
14187 int len;
14188 int n = 0;
14189
14190 if (string != NULL)
14191 {
14192 len = strlen (string);
14193 if (precision > 0 && len > precision)
14194 len = precision;
14195 lisp_string = make_string (string, len);
14196 if (NILP (props))
14197 props = mode_line_string_face_prop;
14198 else if (!NILP (mode_line_string_face))
14199 {
14200 Lisp_Object face = Fplist_get (props, Qface);
14201 props = Fcopy_sequence (props);
14202 if (NILP (face))
14203 face = mode_line_string_face;
14204 else
14205 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
14206 props = Fplist_put (props, Qface, face);
14207 }
14208 Fadd_text_properties (make_number (0), make_number (len),
14209 props, lisp_string);
14210 }
14211 else
14212 {
14213 len = XFASTINT (Flength (lisp_string));
14214 if (precision > 0 && len > precision)
14215 {
14216 len = precision;
14217 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
14218 precision = -1;
14219 }
14220 if (!NILP (mode_line_string_face))
14221 {
14222 Lisp_Object face;
14223 if (NILP (props))
14224 props = Ftext_properties_at (make_number (0), lisp_string);
14225 face = Fplist_get (props, Qface);
14226 if (NILP (face))
14227 face = mode_line_string_face;
14228 else
14229 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
14230 props = Fcons (Qface, Fcons (face, Qnil));
14231 if (copy_string)
14232 lisp_string = Fcopy_sequence (lisp_string);
14233 }
14234 if (!NILP (props))
14235 Fadd_text_properties (make_number (0), make_number (len),
14236 props, lisp_string);
14237 }
14238
14239 if (len > 0)
14240 {
14241 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
14242 n += len;
14243 }
14244
14245 if (field_width > len)
14246 {
14247 field_width -= len;
14248 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
14249 if (!NILP (props))
14250 Fadd_text_properties (make_number (0), make_number (field_width),
14251 props, lisp_string);
14252 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
14253 n += field_width;
14254 }
14255
14256 return n;
14257 }
14258
14259
14260 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
14261 0, 3, 0,
14262 doc: /* Return the mode-line of selected window as a string.
14263 First optional arg FORMAT specifies a different format string (see
14264 `mode-line-format' for details) to use. If FORMAT is t, return
14265 the buffer's header-line. Second optional arg WINDOW specifies a
14266 different window to use as the context for the formatting.
14267 If third optional arg NO-PROPS is non-nil, string is not propertized. */)
14268 (format, window, no_props)
14269 Lisp_Object format, window, no_props;
14270 {
14271 struct it it;
14272 int len;
14273 struct window *w;
14274 struct buffer *old_buffer = NULL;
14275 enum face_id face_id = DEFAULT_FACE_ID;
14276
14277 if (NILP (window))
14278 window = selected_window;
14279 CHECK_WINDOW (window);
14280 w = XWINDOW (window);
14281 CHECK_BUFFER (w->buffer);
14282
14283 if (XBUFFER (w->buffer) != current_buffer)
14284 {
14285 old_buffer = current_buffer;
14286 set_buffer_internal_1 (XBUFFER (w->buffer));
14287 }
14288
14289 if (NILP (format) || EQ (format, Qt))
14290 {
14291 face_id = NILP (format)
14292 ? CURRENT_MODE_LINE_FACE_ID (w) :
14293 HEADER_LINE_FACE_ID;
14294 format = NILP (format)
14295 ? current_buffer->mode_line_format
14296 : current_buffer->header_line_format;
14297 }
14298
14299 init_iterator (&it, w, -1, -1, NULL, face_id);
14300
14301 if (NILP (no_props))
14302 {
14303 mode_line_string_face =
14304 (face_id == MODE_LINE_FACE_ID ? Qmode_line :
14305 face_id == MODE_LINE_INACTIVE_FACE_ID ? Qmode_line_inactive :
14306 face_id == HEADER_LINE_FACE_ID ? Qheader_line : Qnil);
14307
14308 mode_line_string_face_prop =
14309 NILP (mode_line_string_face) ? Qnil :
14310 Fcons (Qface, Fcons (mode_line_string_face, Qnil));
14311
14312 /* We need a dummy last element in mode_line_string_list to
14313 indicate we are building the propertized mode-line string.
14314 Using mode_line_string_face_prop here GC protects it. */
14315 mode_line_string_list =
14316 Fcons (mode_line_string_face_prop, Qnil);
14317 frame_title_ptr = NULL;
14318 }
14319 else
14320 {
14321 mode_line_string_face_prop = Qnil;
14322 mode_line_string_list = Qnil;
14323 frame_title_ptr = frame_title_buf;
14324 }
14325
14326 push_frame_kboard (it.f);
14327 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
14328 pop_frame_kboard ();
14329
14330 if (old_buffer)
14331 set_buffer_internal_1 (old_buffer);
14332
14333 if (NILP (no_props))
14334 {
14335 Lisp_Object str;
14336 mode_line_string_list = Fnreverse (mode_line_string_list);
14337 str = Fmapconcat (intern ("identity"), XCDR (mode_line_string_list),
14338 make_string ("", 0));
14339 mode_line_string_face_prop = Qnil;
14340 mode_line_string_list = Qnil;
14341 return str;
14342 }
14343
14344 len = frame_title_ptr - frame_title_buf;
14345 if (len > 0 && frame_title_ptr[-1] == '-')
14346 {
14347 /* Mode lines typically ends with numerous dashes; reduce to two dashes. */
14348 while (frame_title_ptr > frame_title_buf && *--frame_title_ptr == '-')
14349 ;
14350 frame_title_ptr += 3; /* restore last non-dash + two dashes */
14351 if (len > frame_title_ptr - frame_title_buf)
14352 len = frame_title_ptr - frame_title_buf;
14353 }
14354
14355 frame_title_ptr = NULL;
14356 return make_string (frame_title_buf, len);
14357 }
14358
14359 /* Write a null-terminated, right justified decimal representation of
14360 the positive integer D to BUF using a minimal field width WIDTH. */
14361
14362 static void
14363 pint2str (buf, width, d)
14364 register char *buf;
14365 register int width;
14366 register int d;
14367 {
14368 register char *p = buf;
14369
14370 if (d <= 0)
14371 *p++ = '0';
14372 else
14373 {
14374 while (d > 0)
14375 {
14376 *p++ = d % 10 + '0';
14377 d /= 10;
14378 }
14379 }
14380
14381 for (width -= (int) (p - buf); width > 0; --width)
14382 *p++ = ' ';
14383 *p-- = '\0';
14384 while (p > buf)
14385 {
14386 d = *buf;
14387 *buf++ = *p;
14388 *p-- = d;
14389 }
14390 }
14391
14392 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
14393 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
14394 type of CODING_SYSTEM. Return updated pointer into BUF. */
14395
14396 static unsigned char invalid_eol_type[] = "(*invalid*)";
14397
14398 static char *
14399 decode_mode_spec_coding (coding_system, buf, eol_flag)
14400 Lisp_Object coding_system;
14401 register char *buf;
14402 int eol_flag;
14403 {
14404 Lisp_Object val;
14405 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
14406 const unsigned char *eol_str;
14407 int eol_str_len;
14408 /* The EOL conversion we are using. */
14409 Lisp_Object eoltype;
14410
14411 val = Fget (coding_system, Qcoding_system);
14412 eoltype = Qnil;
14413
14414 if (!VECTORP (val)) /* Not yet decided. */
14415 {
14416 if (multibyte)
14417 *buf++ = '-';
14418 if (eol_flag)
14419 eoltype = eol_mnemonic_undecided;
14420 /* Don't mention EOL conversion if it isn't decided. */
14421 }
14422 else
14423 {
14424 Lisp_Object eolvalue;
14425
14426 eolvalue = Fget (coding_system, Qeol_type);
14427
14428 if (multibyte)
14429 *buf++ = XFASTINT (AREF (val, 1));
14430
14431 if (eol_flag)
14432 {
14433 /* The EOL conversion that is normal on this system. */
14434
14435 if (NILP (eolvalue)) /* Not yet decided. */
14436 eoltype = eol_mnemonic_undecided;
14437 else if (VECTORP (eolvalue)) /* Not yet decided. */
14438 eoltype = eol_mnemonic_undecided;
14439 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
14440 eoltype = (XFASTINT (eolvalue) == 0
14441 ? eol_mnemonic_unix
14442 : (XFASTINT (eolvalue) == 1
14443 ? eol_mnemonic_dos : eol_mnemonic_mac));
14444 }
14445 }
14446
14447 if (eol_flag)
14448 {
14449 /* Mention the EOL conversion if it is not the usual one. */
14450 if (STRINGP (eoltype))
14451 {
14452 eol_str = SDATA (eoltype);
14453 eol_str_len = SBYTES (eoltype);
14454 }
14455 else if (INTEGERP (eoltype)
14456 && CHAR_VALID_P (XINT (eoltype), 0))
14457 {
14458 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
14459 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
14460 eol_str = tmp;
14461 }
14462 else
14463 {
14464 eol_str = invalid_eol_type;
14465 eol_str_len = sizeof (invalid_eol_type) - 1;
14466 }
14467 bcopy (eol_str, buf, eol_str_len);
14468 buf += eol_str_len;
14469 }
14470
14471 return buf;
14472 }
14473
14474 /* Return a string for the output of a mode line %-spec for window W,
14475 generated by character C. PRECISION >= 0 means don't return a
14476 string longer than that value. FIELD_WIDTH > 0 means pad the
14477 string returned with spaces to that value. Return 1 in *MULTIBYTE
14478 if the result is multibyte text. */
14479
14480 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
14481
14482 static char *
14483 decode_mode_spec (w, c, field_width, precision, multibyte)
14484 struct window *w;
14485 register int c;
14486 int field_width, precision;
14487 int *multibyte;
14488 {
14489 Lisp_Object obj;
14490 struct frame *f = XFRAME (WINDOW_FRAME (w));
14491 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
14492 struct buffer *b = XBUFFER (w->buffer);
14493
14494 obj = Qnil;
14495 *multibyte = 0;
14496
14497 switch (c)
14498 {
14499 case '*':
14500 if (!NILP (b->read_only))
14501 return "%";
14502 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
14503 return "*";
14504 return "-";
14505
14506 case '+':
14507 /* This differs from %* only for a modified read-only buffer. */
14508 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
14509 return "*";
14510 if (!NILP (b->read_only))
14511 return "%";
14512 return "-";
14513
14514 case '&':
14515 /* This differs from %* in ignoring read-only-ness. */
14516 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
14517 return "*";
14518 return "-";
14519
14520 case '%':
14521 return "%";
14522
14523 case '[':
14524 {
14525 int i;
14526 char *p;
14527
14528 if (command_loop_level > 5)
14529 return "[[[... ";
14530 p = decode_mode_spec_buf;
14531 for (i = 0; i < command_loop_level; i++)
14532 *p++ = '[';
14533 *p = 0;
14534 return decode_mode_spec_buf;
14535 }
14536
14537 case ']':
14538 {
14539 int i;
14540 char *p;
14541
14542 if (command_loop_level > 5)
14543 return " ...]]]";
14544 p = decode_mode_spec_buf;
14545 for (i = 0; i < command_loop_level; i++)
14546 *p++ = ']';
14547 *p = 0;
14548 return decode_mode_spec_buf;
14549 }
14550
14551 case '-':
14552 {
14553 register int i;
14554
14555 /* Let lots_of_dashes be a string of infinite length. */
14556 if (!NILP (mode_line_string_list))
14557 return "--";
14558 if (field_width <= 0
14559 || field_width > sizeof (lots_of_dashes))
14560 {
14561 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
14562 decode_mode_spec_buf[i] = '-';
14563 decode_mode_spec_buf[i] = '\0';
14564 return decode_mode_spec_buf;
14565 }
14566 else
14567 return lots_of_dashes;
14568 }
14569
14570 case 'b':
14571 obj = b->name;
14572 break;
14573
14574 case 'c':
14575 {
14576 int col = (int) current_column (); /* iftc */
14577 w->column_number_displayed = make_number (col);
14578 pint2str (decode_mode_spec_buf, field_width, col);
14579 return decode_mode_spec_buf;
14580 }
14581
14582 case 'F':
14583 /* %F displays the frame name. */
14584 if (!NILP (f->title))
14585 return (char *) SDATA (f->title);
14586 if (f->explicit_name || ! FRAME_WINDOW_P (f))
14587 return (char *) SDATA (f->name);
14588 return "Emacs";
14589
14590 case 'f':
14591 obj = b->filename;
14592 break;
14593
14594 case 'l':
14595 {
14596 int startpos = XMARKER (w->start)->charpos;
14597 int startpos_byte = marker_byte_position (w->start);
14598 int line, linepos, linepos_byte, topline;
14599 int nlines, junk;
14600 int height = XFASTINT (w->height);
14601
14602 /* If we decided that this buffer isn't suitable for line numbers,
14603 don't forget that too fast. */
14604 if (EQ (w->base_line_pos, w->buffer))
14605 goto no_value;
14606 /* But do forget it, if the window shows a different buffer now. */
14607 else if (BUFFERP (w->base_line_pos))
14608 w->base_line_pos = Qnil;
14609
14610 /* If the buffer is very big, don't waste time. */
14611 if (INTEGERP (Vline_number_display_limit)
14612 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
14613 {
14614 w->base_line_pos = Qnil;
14615 w->base_line_number = Qnil;
14616 goto no_value;
14617 }
14618
14619 if (!NILP (w->base_line_number)
14620 && !NILP (w->base_line_pos)
14621 && XFASTINT (w->base_line_pos) <= startpos)
14622 {
14623 line = XFASTINT (w->base_line_number);
14624 linepos = XFASTINT (w->base_line_pos);
14625 linepos_byte = buf_charpos_to_bytepos (b, linepos);
14626 }
14627 else
14628 {
14629 line = 1;
14630 linepos = BUF_BEGV (b);
14631 linepos_byte = BUF_BEGV_BYTE (b);
14632 }
14633
14634 /* Count lines from base line to window start position. */
14635 nlines = display_count_lines (linepos, linepos_byte,
14636 startpos_byte,
14637 startpos, &junk);
14638
14639 topline = nlines + line;
14640
14641 /* Determine a new base line, if the old one is too close
14642 or too far away, or if we did not have one.
14643 "Too close" means it's plausible a scroll-down would
14644 go back past it. */
14645 if (startpos == BUF_BEGV (b))
14646 {
14647 w->base_line_number = make_number (topline);
14648 w->base_line_pos = make_number (BUF_BEGV (b));
14649 }
14650 else if (nlines < height + 25 || nlines > height * 3 + 50
14651 || linepos == BUF_BEGV (b))
14652 {
14653 int limit = BUF_BEGV (b);
14654 int limit_byte = BUF_BEGV_BYTE (b);
14655 int position;
14656 int distance = (height * 2 + 30) * line_number_display_limit_width;
14657
14658 if (startpos - distance > limit)
14659 {
14660 limit = startpos - distance;
14661 limit_byte = CHAR_TO_BYTE (limit);
14662 }
14663
14664 nlines = display_count_lines (startpos, startpos_byte,
14665 limit_byte,
14666 - (height * 2 + 30),
14667 &position);
14668 /* If we couldn't find the lines we wanted within
14669 line_number_display_limit_width chars per line,
14670 give up on line numbers for this window. */
14671 if (position == limit_byte && limit == startpos - distance)
14672 {
14673 w->base_line_pos = w->buffer;
14674 w->base_line_number = Qnil;
14675 goto no_value;
14676 }
14677
14678 w->base_line_number = make_number (topline - nlines);
14679 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
14680 }
14681
14682 /* Now count lines from the start pos to point. */
14683 nlines = display_count_lines (startpos, startpos_byte,
14684 PT_BYTE, PT, &junk);
14685
14686 /* Record that we did display the line number. */
14687 line_number_displayed = 1;
14688
14689 /* Make the string to show. */
14690 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
14691 return decode_mode_spec_buf;
14692 no_value:
14693 {
14694 char* p = decode_mode_spec_buf;
14695 int pad = field_width - 2;
14696 while (pad-- > 0)
14697 *p++ = ' ';
14698 *p++ = '?';
14699 *p++ = '?';
14700 *p = '\0';
14701 return decode_mode_spec_buf;
14702 }
14703 }
14704 break;
14705
14706 case 'm':
14707 obj = b->mode_name;
14708 break;
14709
14710 case 'n':
14711 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
14712 return " Narrow";
14713 break;
14714
14715 case 'p':
14716 {
14717 int pos = marker_position (w->start);
14718 int total = BUF_ZV (b) - BUF_BEGV (b);
14719
14720 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
14721 {
14722 if (pos <= BUF_BEGV (b))
14723 return "All";
14724 else
14725 return "Bottom";
14726 }
14727 else if (pos <= BUF_BEGV (b))
14728 return "Top";
14729 else
14730 {
14731 if (total > 1000000)
14732 /* Do it differently for a large value, to avoid overflow. */
14733 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
14734 else
14735 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
14736 /* We can't normally display a 3-digit number,
14737 so get us a 2-digit number that is close. */
14738 if (total == 100)
14739 total = 99;
14740 sprintf (decode_mode_spec_buf, "%2d%%", total);
14741 return decode_mode_spec_buf;
14742 }
14743 }
14744
14745 /* Display percentage of size above the bottom of the screen. */
14746 case 'P':
14747 {
14748 int toppos = marker_position (w->start);
14749 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
14750 int total = BUF_ZV (b) - BUF_BEGV (b);
14751
14752 if (botpos >= BUF_ZV (b))
14753 {
14754 if (toppos <= BUF_BEGV (b))
14755 return "All";
14756 else
14757 return "Bottom";
14758 }
14759 else
14760 {
14761 if (total > 1000000)
14762 /* Do it differently for a large value, to avoid overflow. */
14763 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
14764 else
14765 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
14766 /* We can't normally display a 3-digit number,
14767 so get us a 2-digit number that is close. */
14768 if (total == 100)
14769 total = 99;
14770 if (toppos <= BUF_BEGV (b))
14771 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
14772 else
14773 sprintf (decode_mode_spec_buf, "%2d%%", total);
14774 return decode_mode_spec_buf;
14775 }
14776 }
14777
14778 case 's':
14779 /* status of process */
14780 obj = Fget_buffer_process (w->buffer);
14781 if (NILP (obj))
14782 return "no process";
14783 #ifdef subprocesses
14784 obj = Fsymbol_name (Fprocess_status (obj));
14785 #endif
14786 break;
14787
14788 case 't': /* indicate TEXT or BINARY */
14789 #ifdef MODE_LINE_BINARY_TEXT
14790 return MODE_LINE_BINARY_TEXT (b);
14791 #else
14792 return "T";
14793 #endif
14794
14795 case 'z':
14796 /* coding-system (not including end-of-line format) */
14797 case 'Z':
14798 /* coding-system (including end-of-line type) */
14799 {
14800 int eol_flag = (c == 'Z');
14801 char *p = decode_mode_spec_buf;
14802
14803 if (! FRAME_WINDOW_P (f))
14804 {
14805 /* No need to mention EOL here--the terminal never needs
14806 to do EOL conversion. */
14807 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
14808 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
14809 }
14810 p = decode_mode_spec_coding (b->buffer_file_coding_system,
14811 p, eol_flag);
14812
14813 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
14814 #ifdef subprocesses
14815 obj = Fget_buffer_process (Fcurrent_buffer ());
14816 if (PROCESSP (obj))
14817 {
14818 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
14819 p, eol_flag);
14820 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
14821 p, eol_flag);
14822 }
14823 #endif /* subprocesses */
14824 #endif /* 0 */
14825 *p = 0;
14826 return decode_mode_spec_buf;
14827 }
14828 }
14829
14830 if (STRINGP (obj))
14831 {
14832 *multibyte = STRING_MULTIBYTE (obj);
14833 return (char *) SDATA (obj);
14834 }
14835 else
14836 return "";
14837 }
14838
14839
14840 /* Count up to COUNT lines starting from START / START_BYTE.
14841 But don't go beyond LIMIT_BYTE.
14842 Return the number of lines thus found (always nonnegative).
14843
14844 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
14845
14846 static int
14847 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
14848 int start, start_byte, limit_byte, count;
14849 int *byte_pos_ptr;
14850 {
14851 register unsigned char *cursor;
14852 unsigned char *base;
14853
14854 register int ceiling;
14855 register unsigned char *ceiling_addr;
14856 int orig_count = count;
14857
14858 /* If we are not in selective display mode,
14859 check only for newlines. */
14860 int selective_display = (!NILP (current_buffer->selective_display)
14861 && !INTEGERP (current_buffer->selective_display));
14862
14863 if (count > 0)
14864 {
14865 while (start_byte < limit_byte)
14866 {
14867 ceiling = BUFFER_CEILING_OF (start_byte);
14868 ceiling = min (limit_byte - 1, ceiling);
14869 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
14870 base = (cursor = BYTE_POS_ADDR (start_byte));
14871 while (1)
14872 {
14873 if (selective_display)
14874 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
14875 ;
14876 else
14877 while (*cursor != '\n' && ++cursor != ceiling_addr)
14878 ;
14879
14880 if (cursor != ceiling_addr)
14881 {
14882 if (--count == 0)
14883 {
14884 start_byte += cursor - base + 1;
14885 *byte_pos_ptr = start_byte;
14886 return orig_count;
14887 }
14888 else
14889 if (++cursor == ceiling_addr)
14890 break;
14891 }
14892 else
14893 break;
14894 }
14895 start_byte += cursor - base;
14896 }
14897 }
14898 else
14899 {
14900 while (start_byte > limit_byte)
14901 {
14902 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
14903 ceiling = max (limit_byte, ceiling);
14904 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
14905 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
14906 while (1)
14907 {
14908 if (selective_display)
14909 while (--cursor != ceiling_addr
14910 && *cursor != '\n' && *cursor != 015)
14911 ;
14912 else
14913 while (--cursor != ceiling_addr && *cursor != '\n')
14914 ;
14915
14916 if (cursor != ceiling_addr)
14917 {
14918 if (++count == 0)
14919 {
14920 start_byte += cursor - base + 1;
14921 *byte_pos_ptr = start_byte;
14922 /* When scanning backwards, we should
14923 not count the newline posterior to which we stop. */
14924 return - orig_count - 1;
14925 }
14926 }
14927 else
14928 break;
14929 }
14930 /* Here we add 1 to compensate for the last decrement
14931 of CURSOR, which took it past the valid range. */
14932 start_byte += cursor - base + 1;
14933 }
14934 }
14935
14936 *byte_pos_ptr = limit_byte;
14937
14938 if (count < 0)
14939 return - orig_count + count;
14940 return orig_count - count;
14941
14942 }
14943
14944
14945 \f
14946 /***********************************************************************
14947 Displaying strings
14948 ***********************************************************************/
14949
14950 /* Display a NUL-terminated string, starting with index START.
14951
14952 If STRING is non-null, display that C string. Otherwise, the Lisp
14953 string LISP_STRING is displayed.
14954
14955 If FACE_STRING is not nil, FACE_STRING_POS is a position in
14956 FACE_STRING. Display STRING or LISP_STRING with the face at
14957 FACE_STRING_POS in FACE_STRING:
14958
14959 Display the string in the environment given by IT, but use the
14960 standard display table, temporarily.
14961
14962 FIELD_WIDTH is the minimum number of output glyphs to produce.
14963 If STRING has fewer characters than FIELD_WIDTH, pad to the right
14964 with spaces. If STRING has more characters, more than FIELD_WIDTH
14965 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
14966
14967 PRECISION is the maximum number of characters to output from
14968 STRING. PRECISION < 0 means don't truncate the string.
14969
14970 This is roughly equivalent to printf format specifiers:
14971
14972 FIELD_WIDTH PRECISION PRINTF
14973 ----------------------------------------
14974 -1 -1 %s
14975 -1 10 %.10s
14976 10 -1 %10s
14977 20 10 %20.10s
14978
14979 MULTIBYTE zero means do not display multibyte chars, > 0 means do
14980 display them, and < 0 means obey the current buffer's value of
14981 enable_multibyte_characters.
14982
14983 Value is the number of glyphs produced. */
14984
14985 static int
14986 display_string (string, lisp_string, face_string, face_string_pos,
14987 start, it, field_width, precision, max_x, multibyte)
14988 unsigned char *string;
14989 Lisp_Object lisp_string;
14990 Lisp_Object face_string;
14991 int face_string_pos;
14992 int start;
14993 struct it *it;
14994 int field_width, precision, max_x;
14995 int multibyte;
14996 {
14997 int hpos_at_start = it->hpos;
14998 int saved_face_id = it->face_id;
14999 struct glyph_row *row = it->glyph_row;
15000
15001 /* Initialize the iterator IT for iteration over STRING beginning
15002 with index START. */
15003 reseat_to_string (it, string, lisp_string, start,
15004 precision, field_width, multibyte);
15005
15006 /* If displaying STRING, set up the face of the iterator
15007 from LISP_STRING, if that's given. */
15008 if (STRINGP (face_string))
15009 {
15010 int endptr;
15011 struct face *face;
15012
15013 it->face_id
15014 = face_at_string_position (it->w, face_string, face_string_pos,
15015 0, it->region_beg_charpos,
15016 it->region_end_charpos,
15017 &endptr, it->base_face_id, 0);
15018 face = FACE_FROM_ID (it->f, it->face_id);
15019 it->face_box_p = face->box != FACE_NO_BOX;
15020 }
15021
15022 /* Set max_x to the maximum allowed X position. Don't let it go
15023 beyond the right edge of the window. */
15024 if (max_x <= 0)
15025 max_x = it->last_visible_x;
15026 else
15027 max_x = min (max_x, it->last_visible_x);
15028
15029 /* Skip over display elements that are not visible. because IT->w is
15030 hscrolled. */
15031 if (it->current_x < it->first_visible_x)
15032 move_it_in_display_line_to (it, 100000, it->first_visible_x,
15033 MOVE_TO_POS | MOVE_TO_X);
15034
15035 row->ascent = it->max_ascent;
15036 row->height = it->max_ascent + it->max_descent;
15037 row->phys_ascent = it->max_phys_ascent;
15038 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15039
15040 /* This condition is for the case that we are called with current_x
15041 past last_visible_x. */
15042 while (it->current_x < max_x)
15043 {
15044 int x_before, x, n_glyphs_before, i, nglyphs;
15045
15046 /* Get the next display element. */
15047 if (!get_next_display_element (it))
15048 break;
15049
15050 /* Produce glyphs. */
15051 x_before = it->current_x;
15052 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
15053 PRODUCE_GLYPHS (it);
15054
15055 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
15056 i = 0;
15057 x = x_before;
15058 while (i < nglyphs)
15059 {
15060 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
15061
15062 if (!it->truncate_lines_p
15063 && x + glyph->pixel_width > max_x)
15064 {
15065 /* End of continued line or max_x reached. */
15066 if (CHAR_GLYPH_PADDING_P (*glyph))
15067 {
15068 /* A wide character is unbreakable. */
15069 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
15070 it->current_x = x_before;
15071 }
15072 else
15073 {
15074 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
15075 it->current_x = x;
15076 }
15077 break;
15078 }
15079 else if (x + glyph->pixel_width > it->first_visible_x)
15080 {
15081 /* Glyph is at least partially visible. */
15082 ++it->hpos;
15083 if (x < it->first_visible_x)
15084 it->glyph_row->x = x - it->first_visible_x;
15085 }
15086 else
15087 {
15088 /* Glyph is off the left margin of the display area.
15089 Should not happen. */
15090 abort ();
15091 }
15092
15093 row->ascent = max (row->ascent, it->max_ascent);
15094 row->height = max (row->height, it->max_ascent + it->max_descent);
15095 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15096 row->phys_height = max (row->phys_height,
15097 it->max_phys_ascent + it->max_phys_descent);
15098 x += glyph->pixel_width;
15099 ++i;
15100 }
15101
15102 /* Stop if max_x reached. */
15103 if (i < nglyphs)
15104 break;
15105
15106 /* Stop at line ends. */
15107 if (ITERATOR_AT_END_OF_LINE_P (it))
15108 {
15109 it->continuation_lines_width = 0;
15110 break;
15111 }
15112
15113 set_iterator_to_next (it, 1);
15114
15115 /* Stop if truncating at the right edge. */
15116 if (it->truncate_lines_p
15117 && it->current_x >= it->last_visible_x)
15118 {
15119 /* Add truncation mark, but don't do it if the line is
15120 truncated at a padding space. */
15121 if (IT_CHARPOS (*it) < it->string_nchars)
15122 {
15123 if (!FRAME_WINDOW_P (it->f))
15124 {
15125 int i, n;
15126
15127 if (it->current_x > it->last_visible_x)
15128 {
15129 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
15130 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
15131 break;
15132 for (n = row->used[TEXT_AREA]; i < n; ++i)
15133 {
15134 row->used[TEXT_AREA] = i;
15135 produce_special_glyphs (it, IT_TRUNCATION);
15136 }
15137 }
15138 produce_special_glyphs (it, IT_TRUNCATION);
15139 }
15140 it->glyph_row->truncated_on_right_p = 1;
15141 }
15142 break;
15143 }
15144 }
15145
15146 /* Maybe insert a truncation at the left. */
15147 if (it->first_visible_x
15148 && IT_CHARPOS (*it) > 0)
15149 {
15150 if (!FRAME_WINDOW_P (it->f))
15151 insert_left_trunc_glyphs (it);
15152 it->glyph_row->truncated_on_left_p = 1;
15153 }
15154
15155 it->face_id = saved_face_id;
15156
15157 /* Value is number of columns displayed. */
15158 return it->hpos - hpos_at_start;
15159 }
15160
15161
15162 \f
15163 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
15164 appears as an element of LIST or as the car of an element of LIST.
15165 If PROPVAL is a list, compare each element against LIST in that
15166 way, and return 1/2 if any element of PROPVAL is found in LIST.
15167 Otherwise return 0. This function cannot quit.
15168 The return value is 2 if the text is invisible but with an ellipsis
15169 and 1 if it's invisible and without an ellipsis. */
15170
15171 int
15172 invisible_p (propval, list)
15173 register Lisp_Object propval;
15174 Lisp_Object list;
15175 {
15176 register Lisp_Object tail, proptail;
15177
15178 for (tail = list; CONSP (tail); tail = XCDR (tail))
15179 {
15180 register Lisp_Object tem;
15181 tem = XCAR (tail);
15182 if (EQ (propval, tem))
15183 return 1;
15184 if (CONSP (tem) && EQ (propval, XCAR (tem)))
15185 return NILP (XCDR (tem)) ? 1 : 2;
15186 }
15187
15188 if (CONSP (propval))
15189 {
15190 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
15191 {
15192 Lisp_Object propelt;
15193 propelt = XCAR (proptail);
15194 for (tail = list; CONSP (tail); tail = XCDR (tail))
15195 {
15196 register Lisp_Object tem;
15197 tem = XCAR (tail);
15198 if (EQ (propelt, tem))
15199 return 1;
15200 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
15201 return NILP (XCDR (tem)) ? 1 : 2;
15202 }
15203 }
15204 }
15205
15206 return 0;
15207 }
15208
15209 \f
15210 /***********************************************************************
15211 Cursor types
15212 ***********************************************************************/
15213
15214 /* Value is the internal representation of the specified cursor type
15215 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
15216 of the bar cursor. */
15217
15218 enum text_cursor_kinds
15219 get_specified_cursor_type (arg, width)
15220 Lisp_Object arg;
15221 int *width;
15222 {
15223 enum text_cursor_kinds type;
15224
15225 if (NILP (arg))
15226 return NO_CURSOR;
15227
15228 if (EQ (arg, Qbox))
15229 return FILLED_BOX_CURSOR;
15230
15231 if (EQ (arg, Qhollow))
15232 return HOLLOW_BOX_CURSOR;
15233
15234 if (EQ (arg, Qbar))
15235 {
15236 *width = 2;
15237 return BAR_CURSOR;
15238 }
15239
15240 if (CONSP (arg)
15241 && EQ (XCAR (arg), Qbar)
15242 && INTEGERP (XCDR (arg))
15243 && XINT (XCDR (arg)) >= 0)
15244 {
15245 *width = XINT (XCDR (arg));
15246 return BAR_CURSOR;
15247 }
15248
15249 if (EQ (arg, Qhbar))
15250 {
15251 *width = 2;
15252 return HBAR_CURSOR;
15253 }
15254
15255 if (CONSP (arg)
15256 && EQ (XCAR (arg), Qhbar)
15257 && INTEGERP (XCDR (arg))
15258 && XINT (XCDR (arg)) >= 0)
15259 {
15260 *width = XINT (XCDR (arg));
15261 return HBAR_CURSOR;
15262 }
15263
15264 /* Treat anything unknown as "hollow box cursor".
15265 It was bad to signal an error; people have trouble fixing
15266 .Xdefaults with Emacs, when it has something bad in it. */
15267 type = HOLLOW_BOX_CURSOR;
15268
15269 return type;
15270 }
15271
15272 /* Set the default cursor types for specified frame. */
15273 void
15274 set_frame_cursor_types (f, arg)
15275 struct frame *f;
15276 Lisp_Object arg;
15277 {
15278 int width;
15279 Lisp_Object tem;
15280
15281 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
15282 FRAME_CURSOR_WIDTH (f) = width;
15283
15284 /* By default, set up the blink-off state depending on the on-state. */
15285
15286 tem = Fassoc (arg, Vblink_cursor_alist);
15287 if (!NILP (tem))
15288 {
15289 FRAME_BLINK_OFF_CURSOR (f)
15290 = get_specified_cursor_type (XCDR (tem), &width);
15291 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
15292 }
15293 else
15294 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
15295 }
15296
15297
15298 /* Return the cursor we want to be displayed in window W. Return
15299 width of bar/hbar cursor through WIDTH arg. Return with
15300 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
15301 (i.e. if the `system caret' should track this cursor).
15302
15303 In a mini-buffer window, we want the cursor only to appear if we
15304 are reading input from this window. For the selected window, we
15305 want the cursor type given by the frame parameter or buffer local
15306 setting of cursor-type. If explicitly marked off, draw no cursor.
15307 In all other cases, we want a hollow box cursor. */
15308
15309 enum text_cursor_kinds
15310 get_window_cursor_type (w, width, active_cursor)
15311 struct window *w;
15312 int *width;
15313 int *active_cursor;
15314 {
15315 struct frame *f = XFRAME (w->frame);
15316 struct buffer *b = XBUFFER (w->buffer);
15317 int cursor_type = DEFAULT_CURSOR;
15318 Lisp_Object alt_cursor;
15319 int non_selected = 0;
15320
15321 *active_cursor = 1;
15322
15323 /* Echo area */
15324 if (cursor_in_echo_area
15325 && FRAME_HAS_MINIBUF_P (f)
15326 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
15327 {
15328 if (w == XWINDOW (echo_area_window))
15329 {
15330 *width = FRAME_CURSOR_WIDTH (f);
15331 return FRAME_DESIRED_CURSOR (f);
15332 }
15333
15334 *active_cursor = 0;
15335 non_selected = 1;
15336 }
15337
15338 /* Nonselected window or nonselected frame. */
15339 else if (w != XWINDOW (f->selected_window)
15340 #ifdef HAVE_WINDOW_SYSTEM
15341 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
15342 #endif
15343 )
15344 {
15345 *active_cursor = 0;
15346
15347 if (MINI_WINDOW_P (w) && minibuf_level == 0)
15348 return NO_CURSOR;
15349
15350 non_selected = 1;
15351 }
15352
15353 /* Never display a cursor in a window in which cursor-type is nil. */
15354 if (NILP (b->cursor_type))
15355 return NO_CURSOR;
15356
15357 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
15358 if (non_selected)
15359 {
15360 alt_cursor = Fbuffer_local_value (Qcursor_in_non_selected_windows, w->buffer);
15361 return get_specified_cursor_type (alt_cursor, width);
15362 }
15363
15364 /* Get the normal cursor type for this window. */
15365 if (EQ (b->cursor_type, Qt))
15366 {
15367 cursor_type = FRAME_DESIRED_CURSOR (f);
15368 *width = FRAME_CURSOR_WIDTH (f);
15369 }
15370 else
15371 cursor_type = get_specified_cursor_type (b->cursor_type, width);
15372
15373 /* Use normal cursor if not blinked off. */
15374 if (!w->cursor_off_p)
15375 return cursor_type;
15376
15377 /* Cursor is blinked off, so determine how to "toggle" it. */
15378
15379 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
15380 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
15381 return get_specified_cursor_type (XCDR (alt_cursor), width);
15382
15383 /* Then see if frame has specified a specific blink off cursor type. */
15384 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
15385 {
15386 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
15387 return FRAME_BLINK_OFF_CURSOR (f);
15388 }
15389
15390 /* Finally perform built-in cursor blinking:
15391 filled box <-> hollow box
15392 wide [h]bar <-> narrow [h]bar
15393 narrow [h]bar <-> no cursor
15394 other type <-> no cursor */
15395
15396 if (cursor_type == FILLED_BOX_CURSOR)
15397 return HOLLOW_BOX_CURSOR;
15398
15399 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
15400 {
15401 *width = 1;
15402 return cursor_type;
15403 }
15404
15405 return NO_CURSOR;
15406 }
15407
15408 \f
15409 /***********************************************************************
15410 Initialization
15411 ***********************************************************************/
15412
15413 void
15414 syms_of_xdisp ()
15415 {
15416 Vwith_echo_area_save_vector = Qnil;
15417 staticpro (&Vwith_echo_area_save_vector);
15418
15419 Vmessage_stack = Qnil;
15420 staticpro (&Vmessage_stack);
15421
15422 Qinhibit_redisplay = intern ("inhibit-redisplay");
15423 staticpro (&Qinhibit_redisplay);
15424
15425 message_dolog_marker1 = Fmake_marker ();
15426 staticpro (&message_dolog_marker1);
15427 message_dolog_marker2 = Fmake_marker ();
15428 staticpro (&message_dolog_marker2);
15429 message_dolog_marker3 = Fmake_marker ();
15430 staticpro (&message_dolog_marker3);
15431
15432 #if GLYPH_DEBUG
15433 defsubr (&Sdump_frame_glyph_matrix);
15434 defsubr (&Sdump_glyph_matrix);
15435 defsubr (&Sdump_glyph_row);
15436 defsubr (&Sdump_tool_bar_row);
15437 defsubr (&Strace_redisplay);
15438 defsubr (&Strace_to_stderr);
15439 #endif
15440 #ifdef HAVE_WINDOW_SYSTEM
15441 defsubr (&Stool_bar_lines_needed);
15442 #endif
15443 defsubr (&Sformat_mode_line);
15444
15445 staticpro (&Qmenu_bar_update_hook);
15446 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
15447
15448 staticpro (&Qoverriding_terminal_local_map);
15449 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
15450
15451 staticpro (&Qoverriding_local_map);
15452 Qoverriding_local_map = intern ("overriding-local-map");
15453
15454 staticpro (&Qwindow_scroll_functions);
15455 Qwindow_scroll_functions = intern ("window-scroll-functions");
15456
15457 staticpro (&Qredisplay_end_trigger_functions);
15458 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
15459
15460 staticpro (&Qinhibit_point_motion_hooks);
15461 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
15462
15463 QCdata = intern (":data");
15464 staticpro (&QCdata);
15465 Qdisplay = intern ("display");
15466 staticpro (&Qdisplay);
15467 Qspace_width = intern ("space-width");
15468 staticpro (&Qspace_width);
15469 Qraise = intern ("raise");
15470 staticpro (&Qraise);
15471 Qspace = intern ("space");
15472 staticpro (&Qspace);
15473 Qmargin = intern ("margin");
15474 staticpro (&Qmargin);
15475 Qleft_margin = intern ("left-margin");
15476 staticpro (&Qleft_margin);
15477 Qright_margin = intern ("right-margin");
15478 staticpro (&Qright_margin);
15479 Qalign_to = intern ("align-to");
15480 staticpro (&Qalign_to);
15481 QCalign_to = intern (":align-to");
15482 staticpro (&QCalign_to);
15483 Qrelative_width = intern ("relative-width");
15484 staticpro (&Qrelative_width);
15485 QCrelative_width = intern (":relative-width");
15486 staticpro (&QCrelative_width);
15487 QCrelative_height = intern (":relative-height");
15488 staticpro (&QCrelative_height);
15489 QCeval = intern (":eval");
15490 staticpro (&QCeval);
15491 QCpropertize = intern (":propertize");
15492 staticpro (&QCpropertize);
15493 Qwhen = intern ("when");
15494 staticpro (&Qwhen);
15495 QCfile = intern (":file");
15496 staticpro (&QCfile);
15497 Qfontified = intern ("fontified");
15498 staticpro (&Qfontified);
15499 Qfontification_functions = intern ("fontification-functions");
15500 staticpro (&Qfontification_functions);
15501 Qtrailing_whitespace = intern ("trailing-whitespace");
15502 staticpro (&Qtrailing_whitespace);
15503 Qimage = intern ("image");
15504 staticpro (&Qimage);
15505 Qmessage_truncate_lines = intern ("message-truncate-lines");
15506 staticpro (&Qmessage_truncate_lines);
15507 Qcursor_in_non_selected_windows = intern ("cursor-in-non-selected-windows");
15508 staticpro (&Qcursor_in_non_selected_windows);
15509 Qgrow_only = intern ("grow-only");
15510 staticpro (&Qgrow_only);
15511 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
15512 staticpro (&Qinhibit_menubar_update);
15513 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
15514 staticpro (&Qinhibit_eval_during_redisplay);
15515 Qposition = intern ("position");
15516 staticpro (&Qposition);
15517 Qbuffer_position = intern ("buffer-position");
15518 staticpro (&Qbuffer_position);
15519 Qobject = intern ("object");
15520 staticpro (&Qobject);
15521 Qbar = intern ("bar");
15522 staticpro (&Qbar);
15523 Qhbar = intern ("hbar");
15524 staticpro (&Qhbar);
15525 Qbox = intern ("box");
15526 staticpro (&Qbox);
15527 Qhollow = intern ("hollow");
15528 staticpro (&Qhollow);
15529 Qrisky_local_variable = intern ("risky-local-variable");
15530 staticpro (&Qrisky_local_variable);
15531 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
15532 staticpro (&Qinhibit_free_realized_faces);
15533
15534 list_of_error = Fcons (intern ("error"), Qnil);
15535 staticpro (&list_of_error);
15536
15537 last_arrow_position = Qnil;
15538 last_arrow_string = Qnil;
15539 staticpro (&last_arrow_position);
15540 staticpro (&last_arrow_string);
15541
15542 echo_buffer[0] = echo_buffer[1] = Qnil;
15543 staticpro (&echo_buffer[0]);
15544 staticpro (&echo_buffer[1]);
15545
15546 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
15547 staticpro (&echo_area_buffer[0]);
15548 staticpro (&echo_area_buffer[1]);
15549
15550 Vmessages_buffer_name = build_string ("*Messages*");
15551 staticpro (&Vmessages_buffer_name);
15552
15553 mode_line_proptrans_alist = Qnil;
15554 staticpro (&mode_line_proptrans_alist);
15555
15556 mode_line_string_list = Qnil;
15557 staticpro (&mode_line_string_list);
15558
15559 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
15560 doc: /* Non-nil means highlight trailing whitespace.
15561 The face used for trailing whitespace is `trailing-whitespace'. */);
15562 Vshow_trailing_whitespace = Qnil;
15563
15564 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
15565 doc: /* Non-nil means don't actually do any redisplay.
15566 This is used for internal purposes. */);
15567 Vinhibit_redisplay = Qnil;
15568
15569 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
15570 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
15571 Vglobal_mode_string = Qnil;
15572
15573 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
15574 doc: /* Marker for where to display an arrow on top of the buffer text.
15575 This must be the beginning of a line in order to work.
15576 See also `overlay-arrow-string'. */);
15577 Voverlay_arrow_position = Qnil;
15578
15579 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
15580 doc: /* String to display as an arrow. See also `overlay-arrow-position'. */);
15581 Voverlay_arrow_string = Qnil;
15582
15583 DEFVAR_INT ("scroll-step", &scroll_step,
15584 doc: /* *The number of lines to try scrolling a window by when point moves out.
15585 If that fails to bring point back on frame, point is centered instead.
15586 If this is zero, point is always centered after it moves off frame.
15587 If you want scrolling to always be a line at a time, you should set
15588 `scroll-conservatively' to a large value rather than set this to 1. */);
15589
15590 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
15591 doc: /* *Scroll up to this many lines, to bring point back on screen.
15592 A value of zero means to scroll the text to center point vertically
15593 in the window. */);
15594 scroll_conservatively = 0;
15595
15596 DEFVAR_INT ("scroll-margin", &scroll_margin,
15597 doc: /* *Number of lines of margin at the top and bottom of a window.
15598 Recenter the window whenever point gets within this many lines
15599 of the top or bottom of the window. */);
15600 scroll_margin = 0;
15601
15602 #if GLYPH_DEBUG
15603 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
15604 #endif
15605
15606 DEFVAR_BOOL ("truncate-partial-width-windows",
15607 &truncate_partial_width_windows,
15608 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
15609 truncate_partial_width_windows = 1;
15610
15611 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
15612 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
15613 Any other value means to use the appropriate face, `mode-line',
15614 `header-line', or `menu' respectively. */);
15615 mode_line_inverse_video = 1;
15616
15617 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
15618 doc: /* *Maximum buffer size for which line number should be displayed.
15619 If the buffer is bigger than this, the line number does not appear
15620 in the mode line. A value of nil means no limit. */);
15621 Vline_number_display_limit = Qnil;
15622
15623 DEFVAR_INT ("line-number-display-limit-width",
15624 &line_number_display_limit_width,
15625 doc: /* *Maximum line width (in characters) for line number display.
15626 If the average length of the lines near point is bigger than this, then the
15627 line number may be omitted from the mode line. */);
15628 line_number_display_limit_width = 200;
15629
15630 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
15631 doc: /* *Non-nil means highlight region even in nonselected windows. */);
15632 highlight_nonselected_windows = 0;
15633
15634 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
15635 doc: /* Non-nil if more than one frame is visible on this display.
15636 Minibuffer-only frames don't count, but iconified frames do.
15637 This variable is not guaranteed to be accurate except while processing
15638 `frame-title-format' and `icon-title-format'. */);
15639
15640 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
15641 doc: /* Template for displaying the title bar of visible frames.
15642 \(Assuming the window manager supports this feature.)
15643 This variable has the same structure as `mode-line-format' (which see),
15644 and is used only on frames for which no explicit name has been set
15645 \(see `modify-frame-parameters'). */);
15646 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
15647 doc: /* Template for displaying the title bar of an iconified frame.
15648 \(Assuming the window manager supports this feature.)
15649 This variable has the same structure as `mode-line-format' (which see),
15650 and is used only on frames for which no explicit name has been set
15651 \(see `modify-frame-parameters'). */);
15652 Vicon_title_format
15653 = Vframe_title_format
15654 = Fcons (intern ("multiple-frames"),
15655 Fcons (build_string ("%b"),
15656 Fcons (Fcons (empty_string,
15657 Fcons (intern ("invocation-name"),
15658 Fcons (build_string ("@"),
15659 Fcons (intern ("system-name"),
15660 Qnil)))),
15661 Qnil)));
15662
15663 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
15664 doc: /* Maximum number of lines to keep in the message log buffer.
15665 If nil, disable message logging. If t, log messages but don't truncate
15666 the buffer when it becomes large. */);
15667 Vmessage_log_max = make_number (50);
15668
15669 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
15670 doc: /* Functions called before redisplay, if window sizes have changed.
15671 The value should be a list of functions that take one argument.
15672 Just before redisplay, for each frame, if any of its windows have changed
15673 size since the last redisplay, or have been split or deleted,
15674 all the functions in the list are called, with the frame as argument. */);
15675 Vwindow_size_change_functions = Qnil;
15676
15677 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
15678 doc: /* List of Functions to call before redisplaying a window with scrolling.
15679 Each function is called with two arguments, the window
15680 and its new display-start position. Note that the value of `window-end'
15681 is not valid when these functions are called. */);
15682 Vwindow_scroll_functions = Qnil;
15683
15684 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
15685 doc: /* *Non-nil means automatically resize tool-bars.
15686 This increases a tool-bar's height if not all tool-bar items are visible.
15687 It decreases a tool-bar's height when it would display blank lines
15688 otherwise. */);
15689 auto_resize_tool_bars_p = 1;
15690
15691 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
15692 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
15693 auto_raise_tool_bar_buttons_p = 1;
15694
15695 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
15696 doc: /* *Margin around tool-bar buttons in pixels.
15697 If an integer, use that for both horizontal and vertical margins.
15698 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
15699 HORZ specifying the horizontal margin, and VERT specifying the
15700 vertical margin. */);
15701 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
15702
15703 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
15704 doc: /* *Relief thickness of tool-bar buttons. */);
15705 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
15706
15707 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
15708 doc: /* List of functions to call to fontify regions of text.
15709 Each function is called with one argument POS. Functions must
15710 fontify a region starting at POS in the current buffer, and give
15711 fontified regions the property `fontified'. */);
15712 Vfontification_functions = Qnil;
15713 Fmake_variable_buffer_local (Qfontification_functions);
15714
15715 DEFVAR_BOOL ("unibyte-display-via-language-environment",
15716 &unibyte_display_via_language_environment,
15717 doc: /* *Non-nil means display unibyte text according to language environment.
15718 Specifically this means that unibyte non-ASCII characters
15719 are displayed by converting them to the equivalent multibyte characters
15720 according to the current language environment. As a result, they are
15721 displayed according to the current fontset. */);
15722 unibyte_display_via_language_environment = 0;
15723
15724 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
15725 doc: /* *Maximum height for resizing mini-windows.
15726 If a float, it specifies a fraction of the mini-window frame's height.
15727 If an integer, it specifies a number of lines. */);
15728 Vmax_mini_window_height = make_float (0.25);
15729
15730 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
15731 doc: /* *How to resize mini-windows.
15732 A value of nil means don't automatically resize mini-windows.
15733 A value of t means resize them to fit the text displayed in them.
15734 A value of `grow-only', the default, means let mini-windows grow
15735 only, until their display becomes empty, at which point the windows
15736 go back to their normal size. */);
15737 Vresize_mini_windows = Qgrow_only;
15738
15739 DEFVAR_LISP ("cursor-in-non-selected-windows",
15740 &Vcursor_in_non_selected_windows,
15741 doc: /* *Cursor type to display in non-selected windows.
15742 t means to use hollow box cursor. See `cursor-type' for other values. */);
15743 Vcursor_in_non_selected_windows = Qt;
15744
15745 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
15746 doc: /* Alist specifying how to blink the cursor off.
15747 Each element has the form (ON-STATE . OFF-STATE). Whenever the
15748 `cursor-type' frame-parameter or variable equals ON-STATE,
15749 comparing using `equal', Emacs uses OFF-STATE to specify
15750 how to blink it off. */);
15751 Vblink_cursor_alist = Qnil;
15752
15753 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
15754 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
15755 automatic_hscrolling_p = 1;
15756
15757 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
15758 doc: /* *How many columns away from the window edge point is allowed to get
15759 before automatic hscrolling will horizontally scroll the window. */);
15760 hscroll_margin = 5;
15761
15762 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
15763 doc: /* *How many columns to scroll the window when point gets too close to the edge.
15764 When point is less than `automatic-hscroll-margin' columns from the window
15765 edge, automatic hscrolling will scroll the window by the amount of columns
15766 determined by this variable. If its value is a positive integer, scroll that
15767 many columns. If it's a positive floating-point number, it specifies the
15768 fraction of the window's width to scroll. If it's nil or zero, point will be
15769 centered horizontally after the scroll. Any other value, including negative
15770 numbers, are treated as if the value were zero.
15771
15772 Automatic hscrolling always moves point outside the scroll margin, so if
15773 point was more than scroll step columns inside the margin, the window will
15774 scroll more than the value given by the scroll step.
15775
15776 Note that the lower bound for automatic hscrolling specified by `scroll-left'
15777 and `scroll-right' overrides this variable's effect. */);
15778 Vhscroll_step = make_number (0);
15779
15780 DEFVAR_LISP ("image-types", &Vimage_types,
15781 doc: /* List of supported image types.
15782 Each element of the list is a symbol for a supported image type. */);
15783 Vimage_types = Qnil;
15784
15785 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
15786 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
15787 Bind this around calls to `message' to let it take effect. */);
15788 message_truncate_lines = 0;
15789
15790 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
15791 doc: /* Normal hook run for clicks on menu bar, before displaying a submenu.
15792 Can be used to update submenus whose contents should vary. */);
15793 Vmenu_bar_update_hook = Qnil;
15794
15795 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
15796 doc: /* Non-nil means don't update menu bars. Internal use only. */);
15797 inhibit_menubar_update = 0;
15798
15799 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
15800 doc: /* Non-nil means don't eval Lisp during redisplay. */);
15801 inhibit_eval_during_redisplay = 0;
15802
15803 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
15804 doc: /* Non-nil means don't free realized faces. Internal use only. */);
15805 inhibit_free_realized_faces = 0;
15806
15807 #if GLYPH_DEBUG
15808 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
15809 doc: /* Inhibit try_window_id display optimization. */);
15810 inhibit_try_window_id = 0;
15811
15812 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
15813 doc: /* Inhibit try_window_reusing display optimization. */);
15814 inhibit_try_window_reusing = 0;
15815
15816 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
15817 doc: /* Inhibit try_cursor_movement display optimization. */);
15818 inhibit_try_cursor_movement = 0;
15819 #endif /* GLYPH_DEBUG */
15820 }
15821
15822
15823 /* Initialize this module when Emacs starts. */
15824
15825 void
15826 init_xdisp ()
15827 {
15828 Lisp_Object root_window;
15829 struct window *mini_w;
15830
15831 current_header_line_height = current_mode_line_height = -1;
15832
15833 CHARPOS (this_line_start_pos) = 0;
15834
15835 mini_w = XWINDOW (minibuf_window);
15836 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
15837
15838 if (!noninteractive)
15839 {
15840 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
15841 int i;
15842
15843 XWINDOW (root_window)->top = make_number (FRAME_TOP_MARGIN (f));
15844 set_window_height (root_window,
15845 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
15846 0);
15847 mini_w->top = make_number (FRAME_HEIGHT (f) - 1);
15848 set_window_height (minibuf_window, 1, 0);
15849
15850 XWINDOW (root_window)->width = make_number (FRAME_WIDTH (f));
15851 mini_w->width = make_number (FRAME_WIDTH (f));
15852
15853 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
15854 scratch_glyph_row.glyphs[TEXT_AREA + 1]
15855 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
15856
15857 /* The default ellipsis glyphs `...'. */
15858 for (i = 0; i < 3; ++i)
15859 default_invis_vector[i] = make_number ('.');
15860 }
15861
15862 {
15863 /* Allocate the buffer for frame titles.
15864 Also used for `format-mode-line'. */
15865 int size = 100;
15866 frame_title_buf = (char *) xmalloc (size);
15867 frame_title_buf_end = frame_title_buf + size;
15868 frame_title_ptr = NULL;
15869 }
15870
15871 help_echo_showing_p = 0;
15872 }
15873
15874