]> code.delx.au - gnu-emacs/blob - src/xdisp.c
(Qbar, Qhbar, Qbox, Qhollow, Vblink_cursor_alist):
[gnu-emacs] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 86, 87, 88, 93, 94, 95, 97, 98, 99, 2000, 2001, 2002
3 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
23
24 Redisplay.
25
26 Emacs separates the task of updating the display from code
27 modifying global state, e.g. buffer text. This way functions
28 operating on buffers don't also have to be concerned with updating
29 the display.
30
31 Updating the display is triggered by the Lisp interpreter when it
32 decides it's time to do it. This is done either automatically for
33 you as part of the interpreter's command loop or as the result of
34 calling Lisp functions like `sit-for'. The C function `redisplay'
35 in xdisp.c is the only entry into the inner redisplay code. (Or,
36 let's say almost---see the description of direct update
37 operations, below.)
38
39 The following diagram shows how redisplay code is invoked. As you
40 can see, Lisp calls redisplay and vice versa. Under window systems
41 like X, some portions of the redisplay code are also called
42 asynchronously during mouse movement or expose events. It is very
43 important that these code parts do NOT use the C library (malloc,
44 free) because many C libraries under Unix are not reentrant. They
45 may also NOT call functions of the Lisp interpreter which could
46 change the interpreter's state. If you don't follow these rules,
47 you will encounter bugs which are very hard to explain.
48
49 (Direct functions, see below)
50 direct_output_for_insert,
51 direct_forward_char (dispnew.c)
52 +---------------------------------+
53 | |
54 | V
55 +--------------+ redisplay +----------------+
56 | Lisp machine |---------------->| Redisplay code |<--+
57 +--------------+ (xdisp.c) +----------------+ |
58 ^ | |
59 +----------------------------------+ |
60 Don't use this path when called |
61 asynchronously! |
62 |
63 expose_window (asynchronous) |
64 |
65 X expose events -----+
66
67 What does redisplay? Obviously, it has to figure out somehow what
68 has been changed since the last time the display has been updated,
69 and to make these changes visible. Preferably it would do that in
70 a moderately intelligent way, i.e. fast.
71
72 Changes in buffer text can be deduced from window and buffer
73 structures, and from some global variables like `beg_unchanged' and
74 `end_unchanged'. The contents of the display are additionally
75 recorded in a `glyph matrix', a two-dimensional matrix of glyph
76 structures. Each row in such a matrix corresponds to a line on the
77 display, and each glyph in a row corresponds to a column displaying
78 a character, an image, or what else. This matrix is called the
79 `current glyph matrix' or `current matrix' in redisplay
80 terminology.
81
82 For buffer parts that have been changed since the last update, a
83 second glyph matrix is constructed, the so called `desired glyph
84 matrix' or short `desired matrix'. Current and desired matrix are
85 then compared to find a cheap way to update the display, e.g. by
86 reusing part of the display by scrolling lines.
87
88
89 Direct operations.
90
91 You will find a lot of redisplay optimizations when you start
92 looking at the innards of redisplay. The overall goal of all these
93 optimizations is to make redisplay fast because it is done
94 frequently.
95
96 Two optimizations are not found in xdisp.c. These are the direct
97 operations mentioned above. As the name suggests they follow a
98 different principle than the rest of redisplay. Instead of
99 building a desired matrix and then comparing it with the current
100 display, they perform their actions directly on the display and on
101 the current matrix.
102
103 One direct operation updates the display after one character has
104 been entered. The other one moves the cursor by one position
105 forward or backward. You find these functions under the names
106 `direct_output_for_insert' and `direct_output_forward_char' in
107 dispnew.c.
108
109
110 Desired matrices.
111
112 Desired matrices are always built per Emacs window. The function
113 `display_line' is the central function to look at if you are
114 interested. It constructs one row in a desired matrix given an
115 iterator structure containing both a buffer position and a
116 description of the environment in which the text is to be
117 displayed. But this is too early, read on.
118
119 Characters and pixmaps displayed for a range of buffer text depend
120 on various settings of buffers and windows, on overlays and text
121 properties, on display tables, on selective display. The good news
122 is that all this hairy stuff is hidden behind a small set of
123 interface functions taking a iterator structure (struct it)
124 argument.
125
126 Iteration over things to be displayed is then simple. It is
127 started by initializing an iterator with a call to init_iterator.
128 Calls to get_next_display_element fill the iterator structure with
129 relevant information about the next thing to display. Calls to
130 set_iterator_to_next move the iterator to the next thing.
131
132 Besides this, an iterator also contains information about the
133 display environment in which glyphs for display elements are to be
134 produced. It has fields for the width and height of the display,
135 the information whether long lines are truncated or continued, a
136 current X and Y position, and lots of other stuff you can better
137 see in dispextern.h.
138
139 Glyphs in a desired matrix are normally constructed in a loop
140 calling get_next_display_element and then produce_glyphs. The call
141 to produce_glyphs will fill the iterator structure with pixel
142 information about the element being displayed and at the same time
143 produce glyphs for it. If the display element fits on the line
144 being displayed, set_iterator_to_next is called next, otherwise the
145 glyphs produced are discarded.
146
147
148 Frame matrices.
149
150 That just couldn't be all, could it? What about terminal types not
151 supporting operations on sub-windows of the screen? To update the
152 display on such a terminal, window-based glyph matrices are not
153 well suited. To be able to reuse part of the display (scrolling
154 lines up and down), we must instead have a view of the whole
155 screen. This is what `frame matrices' are for. They are a trick.
156
157 Frames on terminals like above have a glyph pool. Windows on such
158 a frame sub-allocate their glyph memory from their frame's glyph
159 pool. The frame itself is given its own glyph matrices. By
160 coincidence---or maybe something else---rows in window glyph
161 matrices are slices of corresponding rows in frame matrices. Thus
162 writing to window matrices implicitly updates a frame matrix which
163 provides us with the view of the whole screen that we originally
164 wanted to have without having to move many bytes around. To be
165 honest, there is a little bit more done, but not much more. If you
166 plan to extend that code, take a look at dispnew.c. The function
167 build_frame_matrix is a good starting point. */
168
169 #include <config.h>
170 #include <stdio.h>
171
172 #include "lisp.h"
173 #include "keyboard.h"
174 #include "frame.h"
175 #include "window.h"
176 #include "termchar.h"
177 #include "dispextern.h"
178 #include "buffer.h"
179 #include "charset.h"
180 #include "indent.h"
181 #include "commands.h"
182 #include "macros.h"
183 #include "disptab.h"
184 #include "termhooks.h"
185 #include "intervals.h"
186 #include "coding.h"
187 #include "process.h"
188 #include "region-cache.h"
189 #include "fontset.h"
190
191 #ifdef HAVE_X_WINDOWS
192 #include "xterm.h"
193 #endif
194 #ifdef WINDOWSNT
195 #include "w32term.h"
196 #endif
197 #ifdef MAC_OS
198 #include "macterm.h"
199 #endif
200
201 #define INFINITY 10000000
202
203 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS)
204 extern void set_frame_menubar P_ ((struct frame *f, int, int));
205 extern int pending_menu_activation;
206 #endif
207
208 extern int interrupt_input;
209 extern int command_loop_level;
210
211 extern int minibuffer_auto_raise;
212
213 extern Lisp_Object Qface;
214 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
215
216 extern Lisp_Object Voverriding_local_map;
217 extern Lisp_Object Voverriding_local_map_menu_flag;
218 extern Lisp_Object Qmenu_item;
219
220 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
221 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
222 Lisp_Object Qredisplay_end_trigger_functions;
223 Lisp_Object Qinhibit_point_motion_hooks;
224 Lisp_Object QCeval, Qwhen, QCfile, QCdata, QCpropertize;
225 Lisp_Object Qfontified;
226 Lisp_Object Qgrow_only;
227 Lisp_Object Qinhibit_eval_during_redisplay;
228 Lisp_Object Qbuffer_position, Qposition, Qobject;
229
230 /* 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 /* Specifies the desired cursor-type to use to show the blinking
541 cursor off state and cursor shown in non-selected windows.
542 t means to use the default. */
543
544 Lisp_Object Valternate_cursor_type;
545 Lisp_Object Qalternate_cursor_type;
546
547 /* How to blink the default frame cursor off. */
548 Lisp_Object Vblink_cursor_alist;
549
550 /* A scratch glyph row with contents used for generating truncation
551 glyphs. Also used in direct_output_for_insert. */
552
553 #define MAX_SCRATCH_GLYPHS 100
554 struct glyph_row scratch_glyph_row;
555 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
556
557 /* Ascent and height of the last line processed by move_it_to. */
558
559 static int last_max_ascent, last_height;
560
561 /* Non-zero if there's a help-echo in the echo area. */
562
563 int help_echo_showing_p;
564
565 /* If >= 0, computed, exact values of mode-line and header-line height
566 to use in the macros CURRENT_MODE_LINE_HEIGHT and
567 CURRENT_HEADER_LINE_HEIGHT. */
568
569 int current_mode_line_height, current_header_line_height;
570
571 /* The maximum distance to look ahead for text properties. Values
572 that are too small let us call compute_char_face and similar
573 functions too often which is expensive. Values that are too large
574 let us call compute_char_face and alike too often because we
575 might not be interested in text properties that far away. */
576
577 #define TEXT_PROP_DISTANCE_LIMIT 100
578
579 #if GLYPH_DEBUG
580
581 /* Variables to turn off display optimizations from Lisp. */
582
583 int inhibit_try_window_id, inhibit_try_window_reusing;
584 int inhibit_try_cursor_movement;
585
586 /* Non-zero means print traces of redisplay if compiled with
587 GLYPH_DEBUG != 0. */
588
589 int trace_redisplay_p;
590
591 #endif /* GLYPH_DEBUG */
592
593 #ifdef DEBUG_TRACE_MOVE
594 /* Non-zero means trace with TRACE_MOVE to stderr. */
595 int trace_move;
596
597 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
598 #else
599 #define TRACE_MOVE(x) (void) 0
600 #endif
601
602 /* Non-zero means automatically scroll windows horizontally to make
603 point visible. */
604
605 int automatic_hscrolling_p;
606
607 /* How close to the margin can point get before the window is scrolled
608 horizontally. */
609 EMACS_INT hscroll_margin;
610
611 /* How much to scroll horizontally when point is inside the above margin. */
612 Lisp_Object Vhscroll_step;
613
614 /* A list of symbols, one for each supported image type. */
615
616 Lisp_Object Vimage_types;
617
618 /* The variable `resize-mini-windows'. If nil, don't resize
619 mini-windows. If t, always resize them to fit the text they
620 display. If `grow-only', let mini-windows grow only until they
621 become empty. */
622
623 Lisp_Object Vresize_mini_windows;
624
625 /* Buffer being redisplayed -- for redisplay_window_error. */
626
627 struct buffer *displayed_buffer;
628
629 /* Value returned from text property handlers (see below). */
630
631 enum prop_handled
632 {
633 HANDLED_NORMALLY,
634 HANDLED_RECOMPUTE_PROPS,
635 HANDLED_OVERLAY_STRING_CONSUMED,
636 HANDLED_RETURN
637 };
638
639 /* A description of text properties that redisplay is interested
640 in. */
641
642 struct props
643 {
644 /* The name of the property. */
645 Lisp_Object *name;
646
647 /* A unique index for the property. */
648 enum prop_idx idx;
649
650 /* A handler function called to set up iterator IT from the property
651 at IT's current position. Value is used to steer handle_stop. */
652 enum prop_handled (*handler) P_ ((struct it *it));
653 };
654
655 static enum prop_handled handle_face_prop P_ ((struct it *));
656 static enum prop_handled handle_invisible_prop P_ ((struct it *));
657 static enum prop_handled handle_display_prop P_ ((struct it *));
658 static enum prop_handled handle_composition_prop P_ ((struct it *));
659 static enum prop_handled handle_overlay_change P_ ((struct it *));
660 static enum prop_handled handle_fontified_prop P_ ((struct it *));
661
662 /* Properties handled by iterators. */
663
664 static struct props it_props[] =
665 {
666 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
667 /* Handle `face' before `display' because some sub-properties of
668 `display' need to know the face. */
669 {&Qface, FACE_PROP_IDX, handle_face_prop},
670 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
671 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
672 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
673 {NULL, 0, NULL}
674 };
675
676 /* Value is the position described by X. If X is a marker, value is
677 the marker_position of X. Otherwise, value is X. */
678
679 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
680
681 /* Enumeration returned by some move_it_.* functions internally. */
682
683 enum move_it_result
684 {
685 /* Not used. Undefined value. */
686 MOVE_UNDEFINED,
687
688 /* Move ended at the requested buffer position or ZV. */
689 MOVE_POS_MATCH_OR_ZV,
690
691 /* Move ended at the requested X pixel position. */
692 MOVE_X_REACHED,
693
694 /* Move within a line ended at the end of a line that must be
695 continued. */
696 MOVE_LINE_CONTINUED,
697
698 /* Move within a line ended at the end of a line that would
699 be displayed truncated. */
700 MOVE_LINE_TRUNCATED,
701
702 /* Move within a line ended at a line end. */
703 MOVE_NEWLINE_OR_CR
704 };
705
706 /* This counter is used to clear the face cache every once in a while
707 in redisplay_internal. It is incremented for each redisplay.
708 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
709 cleared. */
710
711 #define CLEAR_FACE_CACHE_COUNT 500
712 static int clear_face_cache_count;
713
714 /* Record the previous terminal frame we displayed. */
715
716 static struct frame *previous_terminal_frame;
717
718 /* Non-zero while redisplay_internal is in progress. */
719
720 int redisplaying_p;
721
722 /* Non-zero means don't free realized faces. Bound while freeing
723 realized faces is dangerous because glyph matrices might still
724 reference them. */
725
726 int inhibit_free_realized_faces;
727 Lisp_Object Qinhibit_free_realized_faces;
728
729 \f
730 /* Function prototypes. */
731
732 static void setup_for_ellipsis P_ ((struct it *));
733 static void mark_window_display_accurate_1 P_ ((struct window *, int));
734 static int single_display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
735 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
736 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
737 static int redisplay_mode_lines P_ ((Lisp_Object, int));
738 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
739
740 #if 0
741 static int invisible_text_between_p P_ ((struct it *, int, int));
742 #endif
743
744 static int next_element_from_ellipsis P_ ((struct it *));
745 static void pint2str P_ ((char *, int, int));
746 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
747 struct text_pos));
748 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
749 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
750 static void store_frame_title_char P_ ((char));
751 static int store_frame_title P_ ((const unsigned char *, int, int));
752 static void x_consider_frame_title P_ ((Lisp_Object));
753 static void handle_stop P_ ((struct it *));
754 static int tool_bar_lines_needed P_ ((struct frame *));
755 static int single_display_prop_intangible_p P_ ((Lisp_Object));
756 static void ensure_echo_area_buffers P_ ((void));
757 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
758 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
759 static int with_echo_area_buffer P_ ((struct window *, int,
760 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
761 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
762 static void clear_garbaged_frames P_ ((void));
763 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
764 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
765 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
766 static int display_echo_area P_ ((struct window *));
767 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
768 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
769 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
770 static int string_char_and_length P_ ((const unsigned char *, int, int *));
771 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
772 struct text_pos));
773 static int compute_window_start_on_continuation_line P_ ((struct window *));
774 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
775 static void insert_left_trunc_glyphs P_ ((struct it *));
776 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
777 static void extend_face_to_end_of_line P_ ((struct it *));
778 static int append_space P_ ((struct it *, int));
779 static int make_cursor_line_fully_visible P_ ((struct window *));
780 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int));
781 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
782 static int trailing_whitespace_p P_ ((int));
783 static int message_log_check_duplicate P_ ((int, int, int, int));
784 static void push_it P_ ((struct it *));
785 static void pop_it P_ ((struct it *));
786 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
787 static void redisplay_internal P_ ((int));
788 static int echo_area_display P_ ((int));
789 static void redisplay_windows P_ ((Lisp_Object));
790 static void redisplay_window P_ ((Lisp_Object, int));
791 static Lisp_Object redisplay_window_error ();
792 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
793 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
794 static void update_menu_bar P_ ((struct frame *, int));
795 static int try_window_reusing_current_matrix P_ ((struct window *));
796 static int try_window_id P_ ((struct window *));
797 static int display_line P_ ((struct it *));
798 static int display_mode_lines P_ ((struct window *));
799 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
800 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
801 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
802 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
803 static void display_menu_bar P_ ((struct window *));
804 static int display_count_lines P_ ((int, int, int, int, int *));
805 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
806 int, int, struct it *, int, int, int, int));
807 static void compute_line_metrics P_ ((struct it *));
808 static void run_redisplay_end_trigger_hook P_ ((struct it *));
809 static int get_overlay_strings P_ ((struct it *, int));
810 static void next_overlay_string P_ ((struct it *));
811 static void reseat P_ ((struct it *, struct text_pos, int));
812 static void reseat_1 P_ ((struct it *, struct text_pos, int));
813 static void back_to_previous_visible_line_start P_ ((struct it *));
814 static void reseat_at_previous_visible_line_start P_ ((struct it *));
815 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
816 static int next_element_from_display_vector P_ ((struct it *));
817 static int next_element_from_string P_ ((struct it *));
818 static int next_element_from_c_string P_ ((struct it *));
819 static int next_element_from_buffer P_ ((struct it *));
820 static int next_element_from_composition P_ ((struct it *));
821 static int next_element_from_image P_ ((struct it *));
822 static int next_element_from_stretch P_ ((struct it *));
823 static void load_overlay_strings P_ ((struct it *, int));
824 static int init_from_display_pos P_ ((struct it *, struct window *,
825 struct display_pos *));
826 static void reseat_to_string P_ ((struct it *, unsigned char *,
827 Lisp_Object, int, int, int, int));
828 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
829 int, int, int));
830 void move_it_vertically_backward P_ ((struct it *, int));
831 static void init_to_row_start P_ ((struct it *, struct window *,
832 struct glyph_row *));
833 static int init_to_row_end P_ ((struct it *, struct window *,
834 struct glyph_row *));
835 static void back_to_previous_line_start P_ ((struct it *));
836 static int forward_to_next_line_start P_ ((struct it *, int *));
837 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
838 Lisp_Object, int));
839 static struct text_pos string_pos P_ ((int, Lisp_Object));
840 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
841 static int number_of_chars P_ ((unsigned char *, int));
842 static void compute_stop_pos P_ ((struct it *));
843 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
844 Lisp_Object));
845 static int face_before_or_after_it_pos P_ ((struct it *, int));
846 static int next_overlay_change P_ ((int));
847 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
848 Lisp_Object, struct text_pos *,
849 int));
850 static int underlying_face_id P_ ((struct it *));
851 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
852 struct window *));
853
854 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
855 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
856
857 #ifdef HAVE_WINDOW_SYSTEM
858
859 static void update_tool_bar P_ ((struct frame *, int));
860 static void build_desired_tool_bar_string P_ ((struct frame *f));
861 static int redisplay_tool_bar P_ ((struct frame *));
862 static void display_tool_bar_line P_ ((struct it *));
863
864 #endif /* HAVE_WINDOW_SYSTEM */
865
866 \f
867 /***********************************************************************
868 Window display dimensions
869 ***********************************************************************/
870
871 /* Return the window-relative maximum y + 1 for glyph rows displaying
872 text in window W. This is the height of W minus the height of a
873 mode line, if any. */
874
875 INLINE int
876 window_text_bottom_y (w)
877 struct window *w;
878 {
879 struct frame *f = XFRAME (w->frame);
880 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
881
882 if (WINDOW_WANTS_MODELINE_P (w))
883 height -= CURRENT_MODE_LINE_HEIGHT (w);
884 return height;
885 }
886
887
888 /* Return the pixel width of display area AREA of window W. AREA < 0
889 means return the total width of W, not including fringes to
890 the left and right of the window. */
891
892 INLINE int
893 window_box_width (w, area)
894 struct window *w;
895 int area;
896 {
897 struct frame *f = XFRAME (w->frame);
898 int width = XFASTINT (w->width);
899
900 if (!w->pseudo_window_p)
901 {
902 width -= FRAME_SCROLL_BAR_WIDTH (f) + FRAME_FRINGE_COLS (f);
903
904 if (area == TEXT_AREA)
905 {
906 if (INTEGERP (w->left_margin_width))
907 width -= XFASTINT (w->left_margin_width);
908 if (INTEGERP (w->right_margin_width))
909 width -= XFASTINT (w->right_margin_width);
910 }
911 else if (area == LEFT_MARGIN_AREA)
912 width = (INTEGERP (w->left_margin_width)
913 ? XFASTINT (w->left_margin_width) : 0);
914 else if (area == RIGHT_MARGIN_AREA)
915 width = (INTEGERP (w->right_margin_width)
916 ? XFASTINT (w->right_margin_width) : 0);
917 }
918
919 return width * CANON_X_UNIT (f);
920 }
921
922
923 /* Return the pixel height of the display area of window W, not
924 including mode lines of W, if any. */
925
926 INLINE int
927 window_box_height (w)
928 struct window *w;
929 {
930 struct frame *f = XFRAME (w->frame);
931 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
932
933 xassert (height >= 0);
934
935 /* Note: the code below that determines the mode-line/header-line
936 height is essentially the same as that contained in the macro
937 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
938 the appropriate glyph row has its `mode_line_p' flag set,
939 and if it doesn't, uses estimate_mode_line_height instead. */
940
941 if (WINDOW_WANTS_MODELINE_P (w))
942 {
943 struct glyph_row *ml_row
944 = (w->current_matrix && w->current_matrix->rows
945 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
946 : 0);
947 if (ml_row && ml_row->mode_line_p)
948 height -= ml_row->height;
949 else
950 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
951 }
952
953 if (WINDOW_WANTS_HEADER_LINE_P (w))
954 {
955 struct glyph_row *hl_row
956 = (w->current_matrix && w->current_matrix->rows
957 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
958 : 0);
959 if (hl_row && hl_row->mode_line_p)
960 height -= hl_row->height;
961 else
962 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
963 }
964
965 /* With a very small font and a mode-line that's taller than
966 default, we might end up with a negative height. */
967 return max (0, height);
968 }
969
970
971 /* Return the frame-relative coordinate of the left edge of display
972 area AREA of window W. AREA < 0 means return the left edge of the
973 whole window, to the right of the left fringe of W. */
974
975 INLINE int
976 window_box_left (w, area)
977 struct window *w;
978 int area;
979 {
980 struct frame *f = XFRAME (w->frame);
981 int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
982
983 if (!w->pseudo_window_p)
984 {
985 x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
986 + FRAME_LEFT_FRINGE_WIDTH (f));
987
988 if (area == TEXT_AREA)
989 x += window_box_width (w, LEFT_MARGIN_AREA);
990 else if (area == RIGHT_MARGIN_AREA)
991 x += (window_box_width (w, LEFT_MARGIN_AREA)
992 + window_box_width (w, TEXT_AREA));
993 }
994
995 return x;
996 }
997
998
999 /* Return the frame-relative coordinate of the right edge of display
1000 area AREA of window W. AREA < 0 means return the left edge of the
1001 whole window, to the left of the right fringe of W. */
1002
1003 INLINE int
1004 window_box_right (w, area)
1005 struct window *w;
1006 int area;
1007 {
1008 return window_box_left (w, area) + window_box_width (w, area);
1009 }
1010
1011
1012 /* Get the bounding box of the display area AREA of window W, without
1013 mode lines, in frame-relative coordinates. AREA < 0 means the
1014 whole window, not including the left and right fringes of
1015 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1016 coordinates of the upper-left corner of the box. Return in
1017 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1018
1019 INLINE void
1020 window_box (w, area, box_x, box_y, box_width, box_height)
1021 struct window *w;
1022 int area;
1023 int *box_x, *box_y, *box_width, *box_height;
1024 {
1025 struct frame *f = XFRAME (w->frame);
1026
1027 *box_width = window_box_width (w, area);
1028 *box_height = window_box_height (w);
1029 *box_x = window_box_left (w, area);
1030 *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
1031 + XFASTINT (w->top) * CANON_Y_UNIT (f));
1032 if (WINDOW_WANTS_HEADER_LINE_P (w))
1033 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1034 }
1035
1036
1037 /* Get the bounding box of the display area AREA of window W, without
1038 mode lines. AREA < 0 means the whole window, not including the
1039 left and right fringe of the window. Return in *TOP_LEFT_X
1040 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1041 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1042 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1043 box. */
1044
1045 INLINE void
1046 window_box_edges (w, area, top_left_x, top_left_y,
1047 bottom_right_x, bottom_right_y)
1048 struct window *w;
1049 int area;
1050 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1051 {
1052 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1053 bottom_right_y);
1054 *bottom_right_x += *top_left_x;
1055 *bottom_right_y += *top_left_y;
1056 }
1057
1058
1059 \f
1060 /***********************************************************************
1061 Utilities
1062 ***********************************************************************/
1063
1064 /* Return the bottom y-position of the line the iterator IT is in.
1065 This can modify IT's settings. */
1066
1067 int
1068 line_bottom_y (it)
1069 struct it *it;
1070 {
1071 int line_height = it->max_ascent + it->max_descent;
1072 int line_top_y = it->current_y;
1073
1074 if (line_height == 0)
1075 {
1076 if (last_height)
1077 line_height = last_height;
1078 else if (IT_CHARPOS (*it) < ZV)
1079 {
1080 move_it_by_lines (it, 1, 1);
1081 line_height = (it->max_ascent || it->max_descent
1082 ? it->max_ascent + it->max_descent
1083 : last_height);
1084 }
1085 else
1086 {
1087 struct glyph_row *row = it->glyph_row;
1088
1089 /* Use the default character height. */
1090 it->glyph_row = NULL;
1091 it->what = IT_CHARACTER;
1092 it->c = ' ';
1093 it->len = 1;
1094 PRODUCE_GLYPHS (it);
1095 line_height = it->ascent + it->descent;
1096 it->glyph_row = row;
1097 }
1098 }
1099
1100 return line_top_y + line_height;
1101 }
1102
1103
1104 /* Return 1 if position CHARPOS is visible in window W. Set *FULLY to
1105 1 if POS is visible and the line containing POS is fully visible.
1106 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1107 and header-lines heights. */
1108
1109 int
1110 pos_visible_p (w, charpos, fully, exact_mode_line_heights_p)
1111 struct window *w;
1112 int charpos, *fully, exact_mode_line_heights_p;
1113 {
1114 struct it it;
1115 struct text_pos top;
1116 int visible_p;
1117 struct buffer *old_buffer = NULL;
1118
1119 if (XBUFFER (w->buffer) != current_buffer)
1120 {
1121 old_buffer = current_buffer;
1122 set_buffer_internal_1 (XBUFFER (w->buffer));
1123 }
1124
1125 *fully = visible_p = 0;
1126 SET_TEXT_POS_FROM_MARKER (top, w->start);
1127
1128 /* Compute exact mode line heights, if requested. */
1129 if (exact_mode_line_heights_p)
1130 {
1131 if (WINDOW_WANTS_MODELINE_P (w))
1132 current_mode_line_height
1133 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1134 current_buffer->mode_line_format);
1135
1136 if (WINDOW_WANTS_HEADER_LINE_P (w))
1137 current_header_line_height
1138 = display_mode_line (w, HEADER_LINE_FACE_ID,
1139 current_buffer->header_line_format);
1140 }
1141
1142 start_display (&it, w, top);
1143 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
1144 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
1145
1146 /* Note that we may overshoot because of invisible text. */
1147 if (IT_CHARPOS (it) >= charpos)
1148 {
1149 int top_y = it.current_y;
1150 int bottom_y = line_bottom_y (&it);
1151 int window_top_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
1152
1153 if (top_y < window_top_y)
1154 visible_p = bottom_y > window_top_y;
1155 else if (top_y < it.last_visible_y)
1156 {
1157 visible_p = 1;
1158 *fully = bottom_y <= it.last_visible_y;
1159 }
1160 }
1161 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1162 {
1163 move_it_by_lines (&it, 1, 0);
1164 if (charpos < IT_CHARPOS (it))
1165 {
1166 visible_p = 1;
1167 *fully = 0;
1168 }
1169 }
1170
1171 if (old_buffer)
1172 set_buffer_internal_1 (old_buffer);
1173
1174 current_header_line_height = current_mode_line_height = -1;
1175 return visible_p;
1176 }
1177
1178
1179 /* Return the next character from STR which is MAXLEN bytes long.
1180 Return in *LEN the length of the character. This is like
1181 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1182 we find one, we return a `?', but with the length of the invalid
1183 character. */
1184
1185 static INLINE int
1186 string_char_and_length (str, maxlen, len)
1187 const unsigned char *str;
1188 int maxlen, *len;
1189 {
1190 int c;
1191
1192 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1193 if (!CHAR_VALID_P (c, 1))
1194 /* We may not change the length here because other places in Emacs
1195 don't use this function, i.e. they silently accept invalid
1196 characters. */
1197 c = '?';
1198
1199 return c;
1200 }
1201
1202
1203
1204 /* Given a position POS containing a valid character and byte position
1205 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1206
1207 static struct text_pos
1208 string_pos_nchars_ahead (pos, string, nchars)
1209 struct text_pos pos;
1210 Lisp_Object string;
1211 int nchars;
1212 {
1213 xassert (STRINGP (string) && nchars >= 0);
1214
1215 if (STRING_MULTIBYTE (string))
1216 {
1217 int rest = SBYTES (string) - BYTEPOS (pos);
1218 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1219 int len;
1220
1221 while (nchars--)
1222 {
1223 string_char_and_length (p, rest, &len);
1224 p += len, rest -= len;
1225 xassert (rest >= 0);
1226 CHARPOS (pos) += 1;
1227 BYTEPOS (pos) += len;
1228 }
1229 }
1230 else
1231 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1232
1233 return pos;
1234 }
1235
1236
1237 /* Value is the text position, i.e. character and byte position,
1238 for character position CHARPOS in STRING. */
1239
1240 static INLINE struct text_pos
1241 string_pos (charpos, string)
1242 int charpos;
1243 Lisp_Object string;
1244 {
1245 struct text_pos pos;
1246 xassert (STRINGP (string));
1247 xassert (charpos >= 0);
1248 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1249 return pos;
1250 }
1251
1252
1253 /* Value is a text position, i.e. character and byte position, for
1254 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1255 means recognize multibyte characters. */
1256
1257 static struct text_pos
1258 c_string_pos (charpos, s, multibyte_p)
1259 int charpos;
1260 unsigned char *s;
1261 int multibyte_p;
1262 {
1263 struct text_pos pos;
1264
1265 xassert (s != NULL);
1266 xassert (charpos >= 0);
1267
1268 if (multibyte_p)
1269 {
1270 int rest = strlen (s), len;
1271
1272 SET_TEXT_POS (pos, 0, 0);
1273 while (charpos--)
1274 {
1275 string_char_and_length (s, rest, &len);
1276 s += len, rest -= len;
1277 xassert (rest >= 0);
1278 CHARPOS (pos) += 1;
1279 BYTEPOS (pos) += len;
1280 }
1281 }
1282 else
1283 SET_TEXT_POS (pos, charpos, charpos);
1284
1285 return pos;
1286 }
1287
1288
1289 /* Value is the number of characters in C string S. MULTIBYTE_P
1290 non-zero means recognize multibyte characters. */
1291
1292 static int
1293 number_of_chars (s, multibyte_p)
1294 unsigned char *s;
1295 int multibyte_p;
1296 {
1297 int nchars;
1298
1299 if (multibyte_p)
1300 {
1301 int rest = strlen (s), len;
1302 unsigned char *p = (unsigned char *) s;
1303
1304 for (nchars = 0; rest > 0; ++nchars)
1305 {
1306 string_char_and_length (p, rest, &len);
1307 rest -= len, p += len;
1308 }
1309 }
1310 else
1311 nchars = strlen (s);
1312
1313 return nchars;
1314 }
1315
1316
1317 /* Compute byte position NEWPOS->bytepos corresponding to
1318 NEWPOS->charpos. POS is a known position in string STRING.
1319 NEWPOS->charpos must be >= POS.charpos. */
1320
1321 static void
1322 compute_string_pos (newpos, pos, string)
1323 struct text_pos *newpos, pos;
1324 Lisp_Object string;
1325 {
1326 xassert (STRINGP (string));
1327 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1328
1329 if (STRING_MULTIBYTE (string))
1330 *newpos = string_pos_nchars_ahead (pos, string,
1331 CHARPOS (*newpos) - CHARPOS (pos));
1332 else
1333 BYTEPOS (*newpos) = CHARPOS (*newpos);
1334 }
1335
1336
1337 \f
1338 /***********************************************************************
1339 Lisp form evaluation
1340 ***********************************************************************/
1341
1342 /* Error handler for safe_eval and safe_call. */
1343
1344 static Lisp_Object
1345 safe_eval_handler (arg)
1346 Lisp_Object arg;
1347 {
1348 add_to_log ("Error during redisplay: %s", arg, Qnil);
1349 return Qnil;
1350 }
1351
1352
1353 /* Evaluate SEXPR and return the result, or nil if something went
1354 wrong. Prevent redisplay during the evaluation. */
1355
1356 Lisp_Object
1357 safe_eval (sexpr)
1358 Lisp_Object sexpr;
1359 {
1360 Lisp_Object val;
1361
1362 if (inhibit_eval_during_redisplay)
1363 val = Qnil;
1364 else
1365 {
1366 int count = SPECPDL_INDEX ();
1367 struct gcpro gcpro1;
1368
1369 GCPRO1 (sexpr);
1370 specbind (Qinhibit_redisplay, Qt);
1371 /* Use Qt to ensure debugger does not run,
1372 so there is no possibility of wanting to redisplay. */
1373 val = internal_condition_case_1 (Feval, sexpr, Qt,
1374 safe_eval_handler);
1375 UNGCPRO;
1376 val = unbind_to (count, val);
1377 }
1378
1379 return val;
1380 }
1381
1382
1383 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1384 Return the result, or nil if something went wrong. Prevent
1385 redisplay during the evaluation. */
1386
1387 Lisp_Object
1388 safe_call (nargs, args)
1389 int nargs;
1390 Lisp_Object *args;
1391 {
1392 Lisp_Object val;
1393
1394 if (inhibit_eval_during_redisplay)
1395 val = Qnil;
1396 else
1397 {
1398 int count = SPECPDL_INDEX ();
1399 struct gcpro gcpro1;
1400
1401 GCPRO1 (args[0]);
1402 gcpro1.nvars = nargs;
1403 specbind (Qinhibit_redisplay, Qt);
1404 /* Use Qt to ensure debugger does not run,
1405 so there is no possibility of wanting to redisplay. */
1406 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
1407 safe_eval_handler);
1408 UNGCPRO;
1409 val = unbind_to (count, val);
1410 }
1411
1412 return val;
1413 }
1414
1415
1416 /* Call function FN with one argument ARG.
1417 Return the result, or nil if something went wrong. */
1418
1419 Lisp_Object
1420 safe_call1 (fn, arg)
1421 Lisp_Object fn, arg;
1422 {
1423 Lisp_Object args[2];
1424 args[0] = fn;
1425 args[1] = arg;
1426 return safe_call (2, args);
1427 }
1428
1429
1430 \f
1431 /***********************************************************************
1432 Debugging
1433 ***********************************************************************/
1434
1435 #if 0
1436
1437 /* Define CHECK_IT to perform sanity checks on iterators.
1438 This is for debugging. It is too slow to do unconditionally. */
1439
1440 static void
1441 check_it (it)
1442 struct it *it;
1443 {
1444 if (it->method == next_element_from_string)
1445 {
1446 xassert (STRINGP (it->string));
1447 xassert (IT_STRING_CHARPOS (*it) >= 0);
1448 }
1449 else if (it->method == next_element_from_buffer)
1450 {
1451 /* Check that character and byte positions agree. */
1452 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1453 }
1454
1455 if (it->dpvec)
1456 xassert (it->current.dpvec_index >= 0);
1457 else
1458 xassert (it->current.dpvec_index < 0);
1459 }
1460
1461 #define CHECK_IT(IT) check_it ((IT))
1462
1463 #else /* not 0 */
1464
1465 #define CHECK_IT(IT) (void) 0
1466
1467 #endif /* not 0 */
1468
1469
1470 #if GLYPH_DEBUG
1471
1472 /* Check that the window end of window W is what we expect it
1473 to be---the last row in the current matrix displaying text. */
1474
1475 static void
1476 check_window_end (w)
1477 struct window *w;
1478 {
1479 if (!MINI_WINDOW_P (w)
1480 && !NILP (w->window_end_valid))
1481 {
1482 struct glyph_row *row;
1483 xassert ((row = MATRIX_ROW (w->current_matrix,
1484 XFASTINT (w->window_end_vpos)),
1485 !row->enabled_p
1486 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1487 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1488 }
1489 }
1490
1491 #define CHECK_WINDOW_END(W) check_window_end ((W))
1492
1493 #else /* not GLYPH_DEBUG */
1494
1495 #define CHECK_WINDOW_END(W) (void) 0
1496
1497 #endif /* not GLYPH_DEBUG */
1498
1499
1500 \f
1501 /***********************************************************************
1502 Iterator initialization
1503 ***********************************************************************/
1504
1505 /* Initialize IT for displaying current_buffer in window W, starting
1506 at character position CHARPOS. CHARPOS < 0 means that no buffer
1507 position is specified which is useful when the iterator is assigned
1508 a position later. BYTEPOS is the byte position corresponding to
1509 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
1510
1511 If ROW is not null, calls to produce_glyphs with IT as parameter
1512 will produce glyphs in that row.
1513
1514 BASE_FACE_ID is the id of a base face to use. It must be one of
1515 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
1516 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
1517 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
1518
1519 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
1520 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
1521 will be initialized to use the corresponding mode line glyph row of
1522 the desired matrix of W. */
1523
1524 void
1525 init_iterator (it, w, charpos, bytepos, row, base_face_id)
1526 struct it *it;
1527 struct window *w;
1528 int charpos, bytepos;
1529 struct glyph_row *row;
1530 enum face_id base_face_id;
1531 {
1532 int highlight_region_p;
1533
1534 /* Some precondition checks. */
1535 xassert (w != NULL && it != NULL);
1536 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
1537 && charpos <= ZV));
1538
1539 /* If face attributes have been changed since the last redisplay,
1540 free realized faces now because they depend on face definitions
1541 that might have changed. Don't free faces while there might be
1542 desired matrices pending which reference these faces. */
1543 if (face_change_count && !inhibit_free_realized_faces)
1544 {
1545 face_change_count = 0;
1546 free_all_realized_faces (Qnil);
1547 }
1548
1549 /* Use one of the mode line rows of W's desired matrix if
1550 appropriate. */
1551 if (row == NULL)
1552 {
1553 if (base_face_id == MODE_LINE_FACE_ID
1554 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
1555 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
1556 else if (base_face_id == HEADER_LINE_FACE_ID)
1557 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
1558 }
1559
1560 /* Clear IT. */
1561 bzero (it, sizeof *it);
1562 it->current.overlay_string_index = -1;
1563 it->current.dpvec_index = -1;
1564 it->base_face_id = base_face_id;
1565
1566 /* The window in which we iterate over current_buffer: */
1567 XSETWINDOW (it->window, w);
1568 it->w = w;
1569 it->f = XFRAME (w->frame);
1570
1571 /* Extra space between lines (on window systems only). */
1572 if (base_face_id == DEFAULT_FACE_ID
1573 && FRAME_WINDOW_P (it->f))
1574 {
1575 if (NATNUMP (current_buffer->extra_line_spacing))
1576 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
1577 else if (it->f->extra_line_spacing > 0)
1578 it->extra_line_spacing = it->f->extra_line_spacing;
1579 }
1580
1581 /* If realized faces have been removed, e.g. because of face
1582 attribute changes of named faces, recompute them. When running
1583 in batch mode, the face cache of Vterminal_frame is null. If
1584 we happen to get called, make a dummy face cache. */
1585 if (
1586 #ifndef WINDOWSNT
1587 noninteractive &&
1588 #endif
1589 FRAME_FACE_CACHE (it->f) == NULL)
1590 init_frame_faces (it->f);
1591 if (FRAME_FACE_CACHE (it->f)->used == 0)
1592 recompute_basic_faces (it->f);
1593
1594 /* Current value of the `space-width', and 'height' properties. */
1595 it->space_width = Qnil;
1596 it->font_height = Qnil;
1597
1598 /* Are control characters displayed as `^C'? */
1599 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
1600
1601 /* -1 means everything between a CR and the following line end
1602 is invisible. >0 means lines indented more than this value are
1603 invisible. */
1604 it->selective = (INTEGERP (current_buffer->selective_display)
1605 ? XFASTINT (current_buffer->selective_display)
1606 : (!NILP (current_buffer->selective_display)
1607 ? -1 : 0));
1608 it->selective_display_ellipsis_p
1609 = !NILP (current_buffer->selective_display_ellipses);
1610
1611 /* Display table to use. */
1612 it->dp = window_display_table (w);
1613
1614 /* Are multibyte characters enabled in current_buffer? */
1615 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1616
1617 /* Non-zero if we should highlight the region. */
1618 highlight_region_p
1619 = (!NILP (Vtransient_mark_mode)
1620 && !NILP (current_buffer->mark_active)
1621 && XMARKER (current_buffer->mark)->buffer != 0);
1622
1623 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
1624 start and end of a visible region in window IT->w. Set both to
1625 -1 to indicate no region. */
1626 if (highlight_region_p
1627 /* Maybe highlight only in selected window. */
1628 && (/* Either show region everywhere. */
1629 highlight_nonselected_windows
1630 /* Or show region in the selected window. */
1631 || w == XWINDOW (selected_window)
1632 /* Or show the region if we are in the mini-buffer and W is
1633 the window the mini-buffer refers to. */
1634 || (MINI_WINDOW_P (XWINDOW (selected_window))
1635 && WINDOWP (minibuf_selected_window)
1636 && w == XWINDOW (minibuf_selected_window))))
1637 {
1638 int charpos = marker_position (current_buffer->mark);
1639 it->region_beg_charpos = min (PT, charpos);
1640 it->region_end_charpos = max (PT, charpos);
1641 }
1642 else
1643 it->region_beg_charpos = it->region_end_charpos = -1;
1644
1645 /* Get the position at which the redisplay_end_trigger hook should
1646 be run, if it is to be run at all. */
1647 if (MARKERP (w->redisplay_end_trigger)
1648 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
1649 it->redisplay_end_trigger_charpos
1650 = marker_position (w->redisplay_end_trigger);
1651 else if (INTEGERP (w->redisplay_end_trigger))
1652 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
1653
1654 /* Correct bogus values of tab_width. */
1655 it->tab_width = XINT (current_buffer->tab_width);
1656 if (it->tab_width <= 0 || it->tab_width > 1000)
1657 it->tab_width = 8;
1658
1659 /* Are lines in the display truncated? */
1660 it->truncate_lines_p
1661 = (base_face_id != DEFAULT_FACE_ID
1662 || XINT (it->w->hscroll)
1663 || (truncate_partial_width_windows
1664 && !WINDOW_FULL_WIDTH_P (it->w))
1665 || !NILP (current_buffer->truncate_lines));
1666
1667 /* Get dimensions of truncation and continuation glyphs. These are
1668 displayed as fringe bitmaps under X, so we don't need them for such
1669 frames. */
1670 if (!FRAME_WINDOW_P (it->f))
1671 {
1672 if (it->truncate_lines_p)
1673 {
1674 /* We will need the truncation glyph. */
1675 xassert (it->glyph_row == NULL);
1676 produce_special_glyphs (it, IT_TRUNCATION);
1677 it->truncation_pixel_width = it->pixel_width;
1678 }
1679 else
1680 {
1681 /* We will need the continuation glyph. */
1682 xassert (it->glyph_row == NULL);
1683 produce_special_glyphs (it, IT_CONTINUATION);
1684 it->continuation_pixel_width = it->pixel_width;
1685 }
1686
1687 /* Reset these values to zero because the produce_special_glyphs
1688 above has changed them. */
1689 it->pixel_width = it->ascent = it->descent = 0;
1690 it->phys_ascent = it->phys_descent = 0;
1691 }
1692
1693 /* Set this after getting the dimensions of truncation and
1694 continuation glyphs, so that we don't produce glyphs when calling
1695 produce_special_glyphs, above. */
1696 it->glyph_row = row;
1697 it->area = TEXT_AREA;
1698
1699 /* Get the dimensions of the display area. The display area
1700 consists of the visible window area plus a horizontally scrolled
1701 part to the left of the window. All x-values are relative to the
1702 start of this total display area. */
1703 if (base_face_id != DEFAULT_FACE_ID)
1704 {
1705 /* Mode lines, menu bar in terminal frames. */
1706 it->first_visible_x = 0;
1707 it->last_visible_x = XFASTINT (w->width) * CANON_X_UNIT (it->f);
1708 }
1709 else
1710 {
1711 it->first_visible_x
1712 = XFASTINT (it->w->hscroll) * CANON_X_UNIT (it->f);
1713 it->last_visible_x = (it->first_visible_x
1714 + window_box_width (w, TEXT_AREA));
1715
1716 /* If we truncate lines, leave room for the truncator glyph(s) at
1717 the right margin. Otherwise, leave room for the continuation
1718 glyph(s). Truncation and continuation glyphs are not inserted
1719 for window-based redisplay. */
1720 if (!FRAME_WINDOW_P (it->f))
1721 {
1722 if (it->truncate_lines_p)
1723 it->last_visible_x -= it->truncation_pixel_width;
1724 else
1725 it->last_visible_x -= it->continuation_pixel_width;
1726 }
1727
1728 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
1729 it->current_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w) + w->vscroll;
1730 }
1731
1732 /* Leave room for a border glyph. */
1733 if (!FRAME_WINDOW_P (it->f)
1734 && !WINDOW_RIGHTMOST_P (it->w))
1735 it->last_visible_x -= 1;
1736
1737 it->last_visible_y = window_text_bottom_y (w);
1738
1739 /* For mode lines and alike, arrange for the first glyph having a
1740 left box line if the face specifies a box. */
1741 if (base_face_id != DEFAULT_FACE_ID)
1742 {
1743 struct face *face;
1744
1745 it->face_id = base_face_id;
1746
1747 /* If we have a boxed mode line, make the first character appear
1748 with a left box line. */
1749 face = FACE_FROM_ID (it->f, base_face_id);
1750 if (face->box != FACE_NO_BOX)
1751 it->start_of_box_run_p = 1;
1752 }
1753
1754 /* If a buffer position was specified, set the iterator there,
1755 getting overlays and face properties from that position. */
1756 if (charpos >= BUF_BEG (current_buffer))
1757 {
1758 it->end_charpos = ZV;
1759 it->face_id = -1;
1760 IT_CHARPOS (*it) = charpos;
1761
1762 /* Compute byte position if not specified. */
1763 if (bytepos < charpos)
1764 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
1765 else
1766 IT_BYTEPOS (*it) = bytepos;
1767
1768 /* Compute faces etc. */
1769 reseat (it, it->current.pos, 1);
1770 }
1771
1772 CHECK_IT (it);
1773 }
1774
1775
1776 /* Initialize IT for the display of window W with window start POS. */
1777
1778 void
1779 start_display (it, w, pos)
1780 struct it *it;
1781 struct window *w;
1782 struct text_pos pos;
1783 {
1784 struct glyph_row *row;
1785 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
1786
1787 row = w->desired_matrix->rows + first_vpos;
1788 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
1789
1790 if (!it->truncate_lines_p)
1791 {
1792 int start_at_line_beg_p;
1793 int first_y = it->current_y;
1794
1795 /* If window start is not at a line start, skip forward to POS to
1796 get the correct continuation lines width. */
1797 start_at_line_beg_p = (CHARPOS (pos) == BEGV
1798 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
1799 if (!start_at_line_beg_p)
1800 {
1801 reseat_at_previous_visible_line_start (it);
1802 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
1803
1804 /* If lines are continued, this line may end in the middle
1805 of a multi-glyph character (e.g. a control character
1806 displayed as \003, or in the middle of an overlay
1807 string). In this case move_it_to above will not have
1808 taken us to the start of the continuation line but to the
1809 end of the continued line. */
1810 if (it->current_x > 0)
1811 {
1812 if (it->current.dpvec_index >= 0
1813 || it->current.overlay_string_index >= 0)
1814 {
1815 set_iterator_to_next (it, 1);
1816 move_it_in_display_line_to (it, -1, -1, 0);
1817 }
1818
1819 it->continuation_lines_width += it->current_x;
1820 }
1821
1822 /* We're starting a new display line, not affected by the
1823 height of the continued line, so clear the appropriate
1824 fields in the iterator structure. */
1825 it->max_ascent = it->max_descent = 0;
1826 it->max_phys_ascent = it->max_phys_descent = 0;
1827
1828 it->current_y = first_y;
1829 it->vpos = 0;
1830 it->current_x = it->hpos = 0;
1831 }
1832 }
1833
1834 #if 0 /* Don't assert the following because start_display is sometimes
1835 called intentionally with a window start that is not at a
1836 line start. Please leave this code in as a comment. */
1837
1838 /* Window start should be on a line start, now. */
1839 xassert (it->continuation_lines_width
1840 || IT_CHARPOS (it) == BEGV
1841 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
1842 #endif /* 0 */
1843 }
1844
1845
1846 /* Return 1 if POS is a position in ellipses displayed for invisible
1847 text. W is the window we display, for text property lookup. */
1848
1849 static int
1850 in_ellipses_for_invisible_text_p (pos, w)
1851 struct display_pos *pos;
1852 struct window *w;
1853 {
1854 Lisp_Object prop, window;
1855 int ellipses_p = 0;
1856 int charpos = CHARPOS (pos->pos);
1857
1858 /* If POS specifies a position in a display vector, this might
1859 be for an ellipsis displayed for invisible text. We won't
1860 get the iterator set up for delivering that ellipsis unless
1861 we make sure that it gets aware of the invisible text. */
1862 if (pos->dpvec_index >= 0
1863 && pos->overlay_string_index < 0
1864 && CHARPOS (pos->string_pos) < 0
1865 && charpos > BEGV
1866 && (XSETWINDOW (window, w),
1867 prop = Fget_char_property (make_number (charpos),
1868 Qinvisible, window),
1869 !TEXT_PROP_MEANS_INVISIBLE (prop)))
1870 {
1871 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
1872 window);
1873 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
1874 }
1875
1876 return ellipses_p;
1877 }
1878
1879
1880 /* Initialize IT for stepping through current_buffer in window W,
1881 starting at position POS that includes overlay string and display
1882 vector/ control character translation position information. Value
1883 is zero if there are overlay strings with newlines at POS. */
1884
1885 static int
1886 init_from_display_pos (it, w, pos)
1887 struct it *it;
1888 struct window *w;
1889 struct display_pos *pos;
1890 {
1891 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
1892 int i, overlay_strings_with_newlines = 0;
1893
1894 /* If POS specifies a position in a display vector, this might
1895 be for an ellipsis displayed for invisible text. We won't
1896 get the iterator set up for delivering that ellipsis unless
1897 we make sure that it gets aware of the invisible text. */
1898 if (in_ellipses_for_invisible_text_p (pos, w))
1899 {
1900 --charpos;
1901 bytepos = 0;
1902 }
1903
1904 /* Keep in mind: the call to reseat in init_iterator skips invisible
1905 text, so we might end up at a position different from POS. This
1906 is only a problem when POS is a row start after a newline and an
1907 overlay starts there with an after-string, and the overlay has an
1908 invisible property. Since we don't skip invisible text in
1909 display_line and elsewhere immediately after consuming the
1910 newline before the row start, such a POS will not be in a string,
1911 but the call to init_iterator below will move us to the
1912 after-string. */
1913 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
1914
1915 for (i = 0; i < it->n_overlay_strings; ++i)
1916 {
1917 const char *s = SDATA (it->overlay_strings[i]);
1918 const char *e = s + SBYTES (it->overlay_strings[i]);
1919
1920 while (s < e && *s != '\n')
1921 ++s;
1922
1923 if (s < e)
1924 {
1925 overlay_strings_with_newlines = 1;
1926 break;
1927 }
1928 }
1929
1930 /* If position is within an overlay string, set up IT to the right
1931 overlay string. */
1932 if (pos->overlay_string_index >= 0)
1933 {
1934 int relative_index;
1935
1936 /* If the first overlay string happens to have a `display'
1937 property for an image, the iterator will be set up for that
1938 image, and we have to undo that setup first before we can
1939 correct the overlay string index. */
1940 if (it->method == next_element_from_image)
1941 pop_it (it);
1942
1943 /* We already have the first chunk of overlay strings in
1944 IT->overlay_strings. Load more until the one for
1945 pos->overlay_string_index is in IT->overlay_strings. */
1946 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
1947 {
1948 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
1949 it->current.overlay_string_index = 0;
1950 while (n--)
1951 {
1952 load_overlay_strings (it, 0);
1953 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
1954 }
1955 }
1956
1957 it->current.overlay_string_index = pos->overlay_string_index;
1958 relative_index = (it->current.overlay_string_index
1959 % OVERLAY_STRING_CHUNK_SIZE);
1960 it->string = it->overlay_strings[relative_index];
1961 xassert (STRINGP (it->string));
1962 it->current.string_pos = pos->string_pos;
1963 it->method = next_element_from_string;
1964 }
1965
1966 #if 0 /* This is bogus because POS not having an overlay string
1967 position does not mean it's after the string. Example: A
1968 line starting with a before-string and initialization of IT
1969 to the previous row's end position. */
1970 else if (it->current.overlay_string_index >= 0)
1971 {
1972 /* If POS says we're already after an overlay string ending at
1973 POS, make sure to pop the iterator because it will be in
1974 front of that overlay string. When POS is ZV, we've thereby
1975 also ``processed'' overlay strings at ZV. */
1976 while (it->sp)
1977 pop_it (it);
1978 it->current.overlay_string_index = -1;
1979 it->method = next_element_from_buffer;
1980 if (CHARPOS (pos->pos) == ZV)
1981 it->overlay_strings_at_end_processed_p = 1;
1982 }
1983 #endif /* 0 */
1984
1985 if (CHARPOS (pos->string_pos) >= 0)
1986 {
1987 /* Recorded position is not in an overlay string, but in another
1988 string. This can only be a string from a `display' property.
1989 IT should already be filled with that string. */
1990 it->current.string_pos = pos->string_pos;
1991 xassert (STRINGP (it->string));
1992 }
1993
1994 /* Restore position in display vector translations, control
1995 character translations or ellipses. */
1996 if (pos->dpvec_index >= 0)
1997 {
1998 if (it->dpvec == NULL)
1999 get_next_display_element (it);
2000 xassert (it->dpvec && it->current.dpvec_index == 0);
2001 it->current.dpvec_index = pos->dpvec_index;
2002 }
2003
2004 CHECK_IT (it);
2005 return !overlay_strings_with_newlines;
2006 }
2007
2008
2009 /* Initialize IT for stepping through current_buffer in window W
2010 starting at ROW->start. */
2011
2012 static void
2013 init_to_row_start (it, w, row)
2014 struct it *it;
2015 struct window *w;
2016 struct glyph_row *row;
2017 {
2018 init_from_display_pos (it, w, &row->start);
2019 it->continuation_lines_width = row->continuation_lines_width;
2020 CHECK_IT (it);
2021 }
2022
2023
2024 /* Initialize IT for stepping through current_buffer in window W
2025 starting in the line following ROW, i.e. starting at ROW->end.
2026 Value is zero if there are overlay strings with newlines at ROW's
2027 end position. */
2028
2029 static int
2030 init_to_row_end (it, w, row)
2031 struct it *it;
2032 struct window *w;
2033 struct glyph_row *row;
2034 {
2035 int success = 0;
2036
2037 if (init_from_display_pos (it, w, &row->end))
2038 {
2039 if (row->continued_p)
2040 it->continuation_lines_width
2041 = row->continuation_lines_width + row->pixel_width;
2042 CHECK_IT (it);
2043 success = 1;
2044 }
2045
2046 return success;
2047 }
2048
2049
2050
2051 \f
2052 /***********************************************************************
2053 Text properties
2054 ***********************************************************************/
2055
2056 /* Called when IT reaches IT->stop_charpos. Handle text property and
2057 overlay changes. Set IT->stop_charpos to the next position where
2058 to stop. */
2059
2060 static void
2061 handle_stop (it)
2062 struct it *it;
2063 {
2064 enum prop_handled handled;
2065 int handle_overlay_change_p = 1;
2066 struct props *p;
2067
2068 it->dpvec = NULL;
2069 it->current.dpvec_index = -1;
2070
2071 do
2072 {
2073 handled = HANDLED_NORMALLY;
2074
2075 /* Call text property handlers. */
2076 for (p = it_props; p->handler; ++p)
2077 {
2078 handled = p->handler (it);
2079
2080 if (handled == HANDLED_RECOMPUTE_PROPS)
2081 break;
2082 else if (handled == HANDLED_RETURN)
2083 return;
2084 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2085 handle_overlay_change_p = 0;
2086 }
2087
2088 if (handled != HANDLED_RECOMPUTE_PROPS)
2089 {
2090 /* Don't check for overlay strings below when set to deliver
2091 characters from a display vector. */
2092 if (it->method == next_element_from_display_vector)
2093 handle_overlay_change_p = 0;
2094
2095 /* Handle overlay changes. */
2096 if (handle_overlay_change_p)
2097 handled = handle_overlay_change (it);
2098
2099 /* Determine where to stop next. */
2100 if (handled == HANDLED_NORMALLY)
2101 compute_stop_pos (it);
2102 }
2103 }
2104 while (handled == HANDLED_RECOMPUTE_PROPS);
2105 }
2106
2107
2108 /* Compute IT->stop_charpos from text property and overlay change
2109 information for IT's current position. */
2110
2111 static void
2112 compute_stop_pos (it)
2113 struct it *it;
2114 {
2115 register INTERVAL iv, next_iv;
2116 Lisp_Object object, limit, position;
2117
2118 /* If nowhere else, stop at the end. */
2119 it->stop_charpos = it->end_charpos;
2120
2121 if (STRINGP (it->string))
2122 {
2123 /* Strings are usually short, so don't limit the search for
2124 properties. */
2125 object = it->string;
2126 limit = Qnil;
2127 position = make_number (IT_STRING_CHARPOS (*it));
2128 }
2129 else
2130 {
2131 int charpos;
2132
2133 /* If next overlay change is in front of the current stop pos
2134 (which is IT->end_charpos), stop there. Note: value of
2135 next_overlay_change is point-max if no overlay change
2136 follows. */
2137 charpos = next_overlay_change (IT_CHARPOS (*it));
2138 if (charpos < it->stop_charpos)
2139 it->stop_charpos = charpos;
2140
2141 /* If showing the region, we have to stop at the region
2142 start or end because the face might change there. */
2143 if (it->region_beg_charpos > 0)
2144 {
2145 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2146 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2147 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2148 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2149 }
2150
2151 /* Set up variables for computing the stop position from text
2152 property changes. */
2153 XSETBUFFER (object, current_buffer);
2154 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2155 position = make_number (IT_CHARPOS (*it));
2156
2157 }
2158
2159 /* Get the interval containing IT's position. Value is a null
2160 interval if there isn't such an interval. */
2161 iv = validate_interval_range (object, &position, &position, 0);
2162 if (!NULL_INTERVAL_P (iv))
2163 {
2164 Lisp_Object values_here[LAST_PROP_IDX];
2165 struct props *p;
2166
2167 /* Get properties here. */
2168 for (p = it_props; p->handler; ++p)
2169 values_here[p->idx] = textget (iv->plist, *p->name);
2170
2171 /* Look for an interval following iv that has different
2172 properties. */
2173 for (next_iv = next_interval (iv);
2174 (!NULL_INTERVAL_P (next_iv)
2175 && (NILP (limit)
2176 || XFASTINT (limit) > next_iv->position));
2177 next_iv = next_interval (next_iv))
2178 {
2179 for (p = it_props; p->handler; ++p)
2180 {
2181 Lisp_Object new_value;
2182
2183 new_value = textget (next_iv->plist, *p->name);
2184 if (!EQ (values_here[p->idx], new_value))
2185 break;
2186 }
2187
2188 if (p->handler)
2189 break;
2190 }
2191
2192 if (!NULL_INTERVAL_P (next_iv))
2193 {
2194 if (INTEGERP (limit)
2195 && next_iv->position >= XFASTINT (limit))
2196 /* No text property change up to limit. */
2197 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2198 else
2199 /* Text properties change in next_iv. */
2200 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2201 }
2202 }
2203
2204 xassert (STRINGP (it->string)
2205 || (it->stop_charpos >= BEGV
2206 && it->stop_charpos >= IT_CHARPOS (*it)));
2207 }
2208
2209
2210 /* Return the position of the next overlay change after POS in
2211 current_buffer. Value is point-max if no overlay change
2212 follows. This is like `next-overlay-change' but doesn't use
2213 xmalloc. */
2214
2215 static int
2216 next_overlay_change (pos)
2217 int pos;
2218 {
2219 int noverlays;
2220 int endpos;
2221 Lisp_Object *overlays;
2222 int len;
2223 int i;
2224
2225 /* Get all overlays at the given position. */
2226 len = 10;
2227 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2228 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2229 if (noverlays > len)
2230 {
2231 len = noverlays;
2232 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2233 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2234 }
2235
2236 /* If any of these overlays ends before endpos,
2237 use its ending point instead. */
2238 for (i = 0; i < noverlays; ++i)
2239 {
2240 Lisp_Object oend;
2241 int oendpos;
2242
2243 oend = OVERLAY_END (overlays[i]);
2244 oendpos = OVERLAY_POSITION (oend);
2245 endpos = min (endpos, oendpos);
2246 }
2247
2248 return endpos;
2249 }
2250
2251
2252 \f
2253 /***********************************************************************
2254 Fontification
2255 ***********************************************************************/
2256
2257 /* Handle changes in the `fontified' property of the current buffer by
2258 calling hook functions from Qfontification_functions to fontify
2259 regions of text. */
2260
2261 static enum prop_handled
2262 handle_fontified_prop (it)
2263 struct it *it;
2264 {
2265 Lisp_Object prop, pos;
2266 enum prop_handled handled = HANDLED_NORMALLY;
2267
2268 /* Get the value of the `fontified' property at IT's current buffer
2269 position. (The `fontified' property doesn't have a special
2270 meaning in strings.) If the value is nil, call functions from
2271 Qfontification_functions. */
2272 if (!STRINGP (it->string)
2273 && it->s == NULL
2274 && !NILP (Vfontification_functions)
2275 && !NILP (Vrun_hooks)
2276 && (pos = make_number (IT_CHARPOS (*it)),
2277 prop = Fget_char_property (pos, Qfontified, Qnil),
2278 NILP (prop)))
2279 {
2280 int count = SPECPDL_INDEX ();
2281 Lisp_Object val;
2282
2283 val = Vfontification_functions;
2284 specbind (Qfontification_functions, Qnil);
2285
2286 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2287 safe_call1 (val, pos);
2288 else
2289 {
2290 Lisp_Object globals, fn;
2291 struct gcpro gcpro1, gcpro2;
2292
2293 globals = Qnil;
2294 GCPRO2 (val, globals);
2295
2296 for (; CONSP (val); val = XCDR (val))
2297 {
2298 fn = XCAR (val);
2299
2300 if (EQ (fn, Qt))
2301 {
2302 /* A value of t indicates this hook has a local
2303 binding; it means to run the global binding too.
2304 In a global value, t should not occur. If it
2305 does, we must ignore it to avoid an endless
2306 loop. */
2307 for (globals = Fdefault_value (Qfontification_functions);
2308 CONSP (globals);
2309 globals = XCDR (globals))
2310 {
2311 fn = XCAR (globals);
2312 if (!EQ (fn, Qt))
2313 safe_call1 (fn, pos);
2314 }
2315 }
2316 else
2317 safe_call1 (fn, pos);
2318 }
2319
2320 UNGCPRO;
2321 }
2322
2323 unbind_to (count, Qnil);
2324
2325 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2326 something. This avoids an endless loop if they failed to
2327 fontify the text for which reason ever. */
2328 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2329 handled = HANDLED_RECOMPUTE_PROPS;
2330 }
2331
2332 return handled;
2333 }
2334
2335
2336 \f
2337 /***********************************************************************
2338 Faces
2339 ***********************************************************************/
2340
2341 /* Set up iterator IT from face properties at its current position.
2342 Called from handle_stop. */
2343
2344 static enum prop_handled
2345 handle_face_prop (it)
2346 struct it *it;
2347 {
2348 int new_face_id, next_stop;
2349
2350 if (!STRINGP (it->string))
2351 {
2352 new_face_id
2353 = face_at_buffer_position (it->w,
2354 IT_CHARPOS (*it),
2355 it->region_beg_charpos,
2356 it->region_end_charpos,
2357 &next_stop,
2358 (IT_CHARPOS (*it)
2359 + TEXT_PROP_DISTANCE_LIMIT),
2360 0);
2361
2362 /* Is this a start of a run of characters with box face?
2363 Caveat: this can be called for a freshly initialized
2364 iterator; face_id is -1 in this case. We know that the new
2365 face will not change until limit, i.e. if the new face has a
2366 box, all characters up to limit will have one. But, as
2367 usual, we don't know whether limit is really the end. */
2368 if (new_face_id != it->face_id)
2369 {
2370 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2371
2372 /* If new face has a box but old face has not, this is
2373 the start of a run of characters with box, i.e. it has
2374 a shadow on the left side. The value of face_id of the
2375 iterator will be -1 if this is the initial call that gets
2376 the face. In this case, we have to look in front of IT's
2377 position and see whether there is a face != new_face_id. */
2378 it->start_of_box_run_p
2379 = (new_face->box != FACE_NO_BOX
2380 && (it->face_id >= 0
2381 || IT_CHARPOS (*it) == BEG
2382 || new_face_id != face_before_it_pos (it)));
2383 it->face_box_p = new_face->box != FACE_NO_BOX;
2384 }
2385 }
2386 else
2387 {
2388 int base_face_id, bufpos;
2389
2390 if (it->current.overlay_string_index >= 0)
2391 bufpos = IT_CHARPOS (*it);
2392 else
2393 bufpos = 0;
2394
2395 /* For strings from a buffer, i.e. overlay strings or strings
2396 from a `display' property, use the face at IT's current
2397 buffer position as the base face to merge with, so that
2398 overlay strings appear in the same face as surrounding
2399 text, unless they specify their own faces. */
2400 base_face_id = underlying_face_id (it);
2401
2402 new_face_id = face_at_string_position (it->w,
2403 it->string,
2404 IT_STRING_CHARPOS (*it),
2405 bufpos,
2406 it->region_beg_charpos,
2407 it->region_end_charpos,
2408 &next_stop,
2409 base_face_id, 0);
2410
2411 #if 0 /* This shouldn't be neccessary. Let's check it. */
2412 /* If IT is used to display a mode line we would really like to
2413 use the mode line face instead of the frame's default face. */
2414 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2415 && new_face_id == DEFAULT_FACE_ID)
2416 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
2417 #endif
2418
2419 /* Is this a start of a run of characters with box? Caveat:
2420 this can be called for a freshly allocated iterator; face_id
2421 is -1 is this case. We know that the new face will not
2422 change until the next check pos, i.e. if the new face has a
2423 box, all characters up to that position will have a
2424 box. But, as usual, we don't know whether that position
2425 is really the end. */
2426 if (new_face_id != it->face_id)
2427 {
2428 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2429 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2430
2431 /* If new face has a box but old face hasn't, this is the
2432 start of a run of characters with box, i.e. it has a
2433 shadow on the left side. */
2434 it->start_of_box_run_p
2435 = new_face->box && (old_face == NULL || !old_face->box);
2436 it->face_box_p = new_face->box != FACE_NO_BOX;
2437 }
2438 }
2439
2440 it->face_id = new_face_id;
2441 return HANDLED_NORMALLY;
2442 }
2443
2444
2445 /* Return the ID of the face ``underlying'' IT's current position,
2446 which is in a string. If the iterator is associated with a
2447 buffer, return the face at IT's current buffer position.
2448 Otherwise, use the iterator's base_face_id. */
2449
2450 static int
2451 underlying_face_id (it)
2452 struct it *it;
2453 {
2454 int face_id = it->base_face_id, i;
2455
2456 xassert (STRINGP (it->string));
2457
2458 for (i = it->sp - 1; i >= 0; --i)
2459 if (NILP (it->stack[i].string))
2460 face_id = it->stack[i].face_id;
2461
2462 return face_id;
2463 }
2464
2465
2466 /* Compute the face one character before or after the current position
2467 of IT. BEFORE_P non-zero means get the face in front of IT's
2468 position. Value is the id of the face. */
2469
2470 static int
2471 face_before_or_after_it_pos (it, before_p)
2472 struct it *it;
2473 int before_p;
2474 {
2475 int face_id, limit;
2476 int next_check_charpos;
2477 struct text_pos pos;
2478
2479 xassert (it->s == NULL);
2480
2481 if (STRINGP (it->string))
2482 {
2483 int bufpos, base_face_id;
2484
2485 /* No face change past the end of the string (for the case
2486 we are padding with spaces). No face change before the
2487 string start. */
2488 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
2489 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2490 return it->face_id;
2491
2492 /* Set pos to the position before or after IT's current position. */
2493 if (before_p)
2494 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2495 else
2496 /* For composition, we must check the character after the
2497 composition. */
2498 pos = (it->what == IT_COMPOSITION
2499 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
2500 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
2501
2502 if (it->current.overlay_string_index >= 0)
2503 bufpos = IT_CHARPOS (*it);
2504 else
2505 bufpos = 0;
2506
2507 base_face_id = underlying_face_id (it);
2508
2509 /* Get the face for ASCII, or unibyte. */
2510 face_id = face_at_string_position (it->w,
2511 it->string,
2512 CHARPOS (pos),
2513 bufpos,
2514 it->region_beg_charpos,
2515 it->region_end_charpos,
2516 &next_check_charpos,
2517 base_face_id, 0);
2518
2519 /* Correct the face for charsets different from ASCII. Do it
2520 for the multibyte case only. The face returned above is
2521 suitable for unibyte text if IT->string is unibyte. */
2522 if (STRING_MULTIBYTE (it->string))
2523 {
2524 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
2525 int rest = SBYTES (it->string) - BYTEPOS (pos);
2526 int c, len;
2527 struct face *face = FACE_FROM_ID (it->f, face_id);
2528
2529 c = string_char_and_length (p, rest, &len);
2530 face_id = FACE_FOR_CHAR (it->f, face, c);
2531 }
2532 }
2533 else
2534 {
2535 if ((IT_CHARPOS (*it) >= ZV && !before_p)
2536 || (IT_CHARPOS (*it) <= BEGV && before_p))
2537 return it->face_id;
2538
2539 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
2540 pos = it->current.pos;
2541
2542 if (before_p)
2543 DEC_TEXT_POS (pos, it->multibyte_p);
2544 else
2545 {
2546 if (it->what == IT_COMPOSITION)
2547 /* For composition, we must check the position after the
2548 composition. */
2549 pos.charpos += it->cmp_len, pos.bytepos += it->len;
2550 else
2551 INC_TEXT_POS (pos, it->multibyte_p);
2552 }
2553
2554 /* Determine face for CHARSET_ASCII, or unibyte. */
2555 face_id = face_at_buffer_position (it->w,
2556 CHARPOS (pos),
2557 it->region_beg_charpos,
2558 it->region_end_charpos,
2559 &next_check_charpos,
2560 limit, 0);
2561
2562 /* Correct the face for charsets different from ASCII. Do it
2563 for the multibyte case only. The face returned above is
2564 suitable for unibyte text if current_buffer is unibyte. */
2565 if (it->multibyte_p)
2566 {
2567 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
2568 struct face *face = FACE_FROM_ID (it->f, face_id);
2569 face_id = FACE_FOR_CHAR (it->f, face, c);
2570 }
2571 }
2572
2573 return face_id;
2574 }
2575
2576
2577 \f
2578 /***********************************************************************
2579 Invisible text
2580 ***********************************************************************/
2581
2582 /* Set up iterator IT from invisible properties at its current
2583 position. Called from handle_stop. */
2584
2585 static enum prop_handled
2586 handle_invisible_prop (it)
2587 struct it *it;
2588 {
2589 enum prop_handled handled = HANDLED_NORMALLY;
2590
2591 if (STRINGP (it->string))
2592 {
2593 extern Lisp_Object Qinvisible;
2594 Lisp_Object prop, end_charpos, limit, charpos;
2595
2596 /* Get the value of the invisible text property at the
2597 current position. Value will be nil if there is no such
2598 property. */
2599 charpos = make_number (IT_STRING_CHARPOS (*it));
2600 prop = Fget_text_property (charpos, Qinvisible, it->string);
2601
2602 if (!NILP (prop)
2603 && IT_STRING_CHARPOS (*it) < it->end_charpos)
2604 {
2605 handled = HANDLED_RECOMPUTE_PROPS;
2606
2607 /* Get the position at which the next change of the
2608 invisible text property can be found in IT->string.
2609 Value will be nil if the property value is the same for
2610 all the rest of IT->string. */
2611 XSETINT (limit, SCHARS (it->string));
2612 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2613 it->string, limit);
2614
2615 /* Text at current position is invisible. The next
2616 change in the property is at position end_charpos.
2617 Move IT's current position to that position. */
2618 if (INTEGERP (end_charpos)
2619 && XFASTINT (end_charpos) < XFASTINT (limit))
2620 {
2621 struct text_pos old;
2622 old = it->current.string_pos;
2623 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
2624 compute_string_pos (&it->current.string_pos, old, it->string);
2625 }
2626 else
2627 {
2628 /* The rest of the string is invisible. If this is an
2629 overlay string, proceed with the next overlay string
2630 or whatever comes and return a character from there. */
2631 if (it->current.overlay_string_index >= 0)
2632 {
2633 next_overlay_string (it);
2634 /* Don't check for overlay strings when we just
2635 finished processing them. */
2636 handled = HANDLED_OVERLAY_STRING_CONSUMED;
2637 }
2638 else
2639 {
2640 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
2641 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
2642 }
2643 }
2644 }
2645 }
2646 else
2647 {
2648 int invis_p, newpos, next_stop, start_charpos;
2649 Lisp_Object pos, prop, overlay;
2650
2651 /* First of all, is there invisible text at this position? */
2652 start_charpos = IT_CHARPOS (*it);
2653 pos = make_number (IT_CHARPOS (*it));
2654 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
2655 &overlay);
2656 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
2657
2658 /* If we are on invisible text, skip over it. */
2659 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
2660 {
2661 /* Record whether we have to display an ellipsis for the
2662 invisible text. */
2663 int display_ellipsis_p = invis_p == 2;
2664
2665 handled = HANDLED_RECOMPUTE_PROPS;
2666
2667 /* Loop skipping over invisible text. The loop is left at
2668 ZV or with IT on the first char being visible again. */
2669 do
2670 {
2671 /* Try to skip some invisible text. Return value is the
2672 position reached which can be equal to IT's position
2673 if there is nothing invisible here. This skips both
2674 over invisible text properties and overlays with
2675 invisible property. */
2676 newpos = skip_invisible (IT_CHARPOS (*it),
2677 &next_stop, ZV, it->window);
2678
2679 /* If we skipped nothing at all we weren't at invisible
2680 text in the first place. If everything to the end of
2681 the buffer was skipped, end the loop. */
2682 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
2683 invis_p = 0;
2684 else
2685 {
2686 /* We skipped some characters but not necessarily
2687 all there are. Check if we ended up on visible
2688 text. Fget_char_property returns the property of
2689 the char before the given position, i.e. if we
2690 get invis_p = 0, this means that the char at
2691 newpos is visible. */
2692 pos = make_number (newpos);
2693 prop = Fget_char_property (pos, Qinvisible, it->window);
2694 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
2695 }
2696
2697 /* If we ended up on invisible text, proceed to
2698 skip starting with next_stop. */
2699 if (invis_p)
2700 IT_CHARPOS (*it) = next_stop;
2701 }
2702 while (invis_p);
2703
2704 /* The position newpos is now either ZV or on visible text. */
2705 IT_CHARPOS (*it) = newpos;
2706 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2707
2708 /* If there are before-strings at the start of invisible
2709 text, and the text is invisible because of a text
2710 property, arrange to show before-strings because 20.x did
2711 it that way. (If the text is invisible because of an
2712 overlay property instead of a text property, this is
2713 already handled in the overlay code.) */
2714 if (NILP (overlay)
2715 && get_overlay_strings (it, start_charpos))
2716 {
2717 handled = HANDLED_RECOMPUTE_PROPS;
2718 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
2719 }
2720 else if (display_ellipsis_p)
2721 setup_for_ellipsis (it);
2722 }
2723 }
2724
2725 return handled;
2726 }
2727
2728
2729 /* Make iterator IT return `...' next. */
2730
2731 static void
2732 setup_for_ellipsis (it)
2733 struct it *it;
2734 {
2735 if (it->dp
2736 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
2737 {
2738 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
2739 it->dpvec = v->contents;
2740 it->dpend = v->contents + v->size;
2741 }
2742 else
2743 {
2744 /* Default `...'. */
2745 it->dpvec = default_invis_vector;
2746 it->dpend = default_invis_vector + 3;
2747 }
2748
2749 /* The ellipsis display does not replace the display of the
2750 character at the new position. Indicate this by setting
2751 IT->dpvec_char_len to zero. */
2752 it->dpvec_char_len = 0;
2753
2754 it->current.dpvec_index = 0;
2755 it->method = next_element_from_display_vector;
2756 }
2757
2758
2759 \f
2760 /***********************************************************************
2761 'display' property
2762 ***********************************************************************/
2763
2764 /* Set up iterator IT from `display' property at its current position.
2765 Called from handle_stop. */
2766
2767 static enum prop_handled
2768 handle_display_prop (it)
2769 struct it *it;
2770 {
2771 Lisp_Object prop, object;
2772 struct text_pos *position;
2773 int display_replaced_p = 0;
2774
2775 if (STRINGP (it->string))
2776 {
2777 object = it->string;
2778 position = &it->current.string_pos;
2779 }
2780 else
2781 {
2782 object = it->w->buffer;
2783 position = &it->current.pos;
2784 }
2785
2786 /* Reset those iterator values set from display property values. */
2787 it->font_height = Qnil;
2788 it->space_width = Qnil;
2789 it->voffset = 0;
2790
2791 /* We don't support recursive `display' properties, i.e. string
2792 values that have a string `display' property, that have a string
2793 `display' property etc. */
2794 if (!it->string_from_display_prop_p)
2795 it->area = TEXT_AREA;
2796
2797 prop = Fget_char_property (make_number (position->charpos),
2798 Qdisplay, object);
2799 if (NILP (prop))
2800 return HANDLED_NORMALLY;
2801
2802 if (CONSP (prop)
2803 /* Simple properties. */
2804 && !EQ (XCAR (prop), Qimage)
2805 && !EQ (XCAR (prop), Qspace)
2806 && !EQ (XCAR (prop), Qwhen)
2807 && !EQ (XCAR (prop), Qspace_width)
2808 && !EQ (XCAR (prop), Qheight)
2809 && !EQ (XCAR (prop), Qraise)
2810 /* Marginal area specifications. */
2811 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
2812 && !NILP (XCAR (prop)))
2813 {
2814 for (; CONSP (prop); prop = XCDR (prop))
2815 {
2816 if (handle_single_display_prop (it, XCAR (prop), object,
2817 position, display_replaced_p))
2818 display_replaced_p = 1;
2819 }
2820 }
2821 else if (VECTORP (prop))
2822 {
2823 int i;
2824 for (i = 0; i < ASIZE (prop); ++i)
2825 if (handle_single_display_prop (it, AREF (prop, i), object,
2826 position, display_replaced_p))
2827 display_replaced_p = 1;
2828 }
2829 else
2830 {
2831 if (handle_single_display_prop (it, prop, object, position, 0))
2832 display_replaced_p = 1;
2833 }
2834
2835 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
2836 }
2837
2838
2839 /* Value is the position of the end of the `display' property starting
2840 at START_POS in OBJECT. */
2841
2842 static struct text_pos
2843 display_prop_end (it, object, start_pos)
2844 struct it *it;
2845 Lisp_Object object;
2846 struct text_pos start_pos;
2847 {
2848 Lisp_Object end;
2849 struct text_pos end_pos;
2850
2851 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
2852 Qdisplay, object, Qnil);
2853 CHARPOS (end_pos) = XFASTINT (end);
2854 if (STRINGP (object))
2855 compute_string_pos (&end_pos, start_pos, it->string);
2856 else
2857 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
2858
2859 return end_pos;
2860 }
2861
2862
2863 /* Set up IT from a single `display' sub-property value PROP. OBJECT
2864 is the object in which the `display' property was found. *POSITION
2865 is the position at which it was found. DISPLAY_REPLACED_P non-zero
2866 means that we previously saw a display sub-property which already
2867 replaced text display with something else, for example an image;
2868 ignore such properties after the first one has been processed.
2869
2870 If PROP is a `space' or `image' sub-property, set *POSITION to the
2871 end position of the `display' property.
2872
2873 Value is non-zero if something was found which replaces the display
2874 of buffer or string text. */
2875
2876 static int
2877 handle_single_display_prop (it, prop, object, position,
2878 display_replaced_before_p)
2879 struct it *it;
2880 Lisp_Object prop;
2881 Lisp_Object object;
2882 struct text_pos *position;
2883 int display_replaced_before_p;
2884 {
2885 Lisp_Object value;
2886 int replaces_text_display_p = 0;
2887 Lisp_Object form;
2888
2889 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
2890 evaluated. If the result is nil, VALUE is ignored. */
2891 form = Qt;
2892 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2893 {
2894 prop = XCDR (prop);
2895 if (!CONSP (prop))
2896 return 0;
2897 form = XCAR (prop);
2898 prop = XCDR (prop);
2899 }
2900
2901 if (!NILP (form) && !EQ (form, Qt))
2902 {
2903 int count = SPECPDL_INDEX ();
2904 struct gcpro gcpro1;
2905
2906 /* Bind `object' to the object having the `display' property, a
2907 buffer or string. Bind `position' to the position in the
2908 object where the property was found, and `buffer-position'
2909 to the current position in the buffer. */
2910 specbind (Qobject, object);
2911 specbind (Qposition, make_number (CHARPOS (*position)));
2912 specbind (Qbuffer_position,
2913 make_number (STRINGP (object)
2914 ? IT_CHARPOS (*it) : CHARPOS (*position)));
2915 GCPRO1 (form);
2916 form = safe_eval (form);
2917 UNGCPRO;
2918 unbind_to (count, Qnil);
2919 }
2920
2921 if (NILP (form))
2922 return 0;
2923
2924 if (CONSP (prop)
2925 && EQ (XCAR (prop), Qheight)
2926 && CONSP (XCDR (prop)))
2927 {
2928 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2929 return 0;
2930
2931 /* `(height HEIGHT)'. */
2932 it->font_height = XCAR (XCDR (prop));
2933 if (!NILP (it->font_height))
2934 {
2935 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2936 int new_height = -1;
2937
2938 if (CONSP (it->font_height)
2939 && (EQ (XCAR (it->font_height), Qplus)
2940 || EQ (XCAR (it->font_height), Qminus))
2941 && CONSP (XCDR (it->font_height))
2942 && INTEGERP (XCAR (XCDR (it->font_height))))
2943 {
2944 /* `(+ N)' or `(- N)' where N is an integer. */
2945 int steps = XINT (XCAR (XCDR (it->font_height)));
2946 if (EQ (XCAR (it->font_height), Qplus))
2947 steps = - steps;
2948 it->face_id = smaller_face (it->f, it->face_id, steps);
2949 }
2950 else if (FUNCTIONP (it->font_height))
2951 {
2952 /* Call function with current height as argument.
2953 Value is the new height. */
2954 Lisp_Object height;
2955 height = safe_call1 (it->font_height,
2956 face->lface[LFACE_HEIGHT_INDEX]);
2957 if (NUMBERP (height))
2958 new_height = XFLOATINT (height);
2959 }
2960 else if (NUMBERP (it->font_height))
2961 {
2962 /* Value is a multiple of the canonical char height. */
2963 struct face *face;
2964
2965 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
2966 new_height = (XFLOATINT (it->font_height)
2967 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
2968 }
2969 else
2970 {
2971 /* Evaluate IT->font_height with `height' bound to the
2972 current specified height to get the new height. */
2973 Lisp_Object value;
2974 int count = SPECPDL_INDEX ();
2975
2976 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
2977 value = safe_eval (it->font_height);
2978 unbind_to (count, Qnil);
2979
2980 if (NUMBERP (value))
2981 new_height = XFLOATINT (value);
2982 }
2983
2984 if (new_height > 0)
2985 it->face_id = face_with_height (it->f, it->face_id, new_height);
2986 }
2987 }
2988 else if (CONSP (prop)
2989 && EQ (XCAR (prop), Qspace_width)
2990 && CONSP (XCDR (prop)))
2991 {
2992 /* `(space_width WIDTH)'. */
2993 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2994 return 0;
2995
2996 value = XCAR (XCDR (prop));
2997 if (NUMBERP (value) && XFLOATINT (value) > 0)
2998 it->space_width = value;
2999 }
3000 else if (CONSP (prop)
3001 && EQ (XCAR (prop), Qraise)
3002 && CONSP (XCDR (prop)))
3003 {
3004 /* `(raise FACTOR)'. */
3005 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3006 return 0;
3007
3008 #ifdef HAVE_WINDOW_SYSTEM
3009 value = XCAR (XCDR (prop));
3010 if (NUMBERP (value))
3011 {
3012 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3013 it->voffset = - (XFLOATINT (value)
3014 * (FONT_HEIGHT (face->font)));
3015 }
3016 #endif /* HAVE_WINDOW_SYSTEM */
3017 }
3018 else if (!it->string_from_display_prop_p)
3019 {
3020 /* `((margin left-margin) VALUE)' or `((margin right-margin)
3021 VALUE) or `((margin nil) VALUE)' or VALUE. */
3022 Lisp_Object location, value;
3023 struct text_pos start_pos;
3024 int valid_p;
3025
3026 /* Characters having this form of property are not displayed, so
3027 we have to find the end of the property. */
3028 start_pos = *position;
3029 *position = display_prop_end (it, object, start_pos);
3030 value = Qnil;
3031
3032 /* Let's stop at the new position and assume that all
3033 text properties change there. */
3034 it->stop_charpos = position->charpos;
3035
3036 location = Qunbound;
3037 if (CONSP (prop) && CONSP (XCAR (prop)))
3038 {
3039 Lisp_Object tem;
3040
3041 value = XCDR (prop);
3042 if (CONSP (value))
3043 value = XCAR (value);
3044
3045 tem = XCAR (prop);
3046 if (EQ (XCAR (tem), Qmargin)
3047 && (tem = XCDR (tem),
3048 tem = CONSP (tem) ? XCAR (tem) : Qnil,
3049 (NILP (tem)
3050 || EQ (tem, Qleft_margin)
3051 || EQ (tem, Qright_margin))))
3052 location = tem;
3053 }
3054
3055 if (EQ (location, Qunbound))
3056 {
3057 location = Qnil;
3058 value = prop;
3059 }
3060
3061 #ifdef HAVE_WINDOW_SYSTEM
3062 if (FRAME_TERMCAP_P (it->f))
3063 valid_p = STRINGP (value);
3064 else
3065 valid_p = (STRINGP (value)
3066 || (CONSP (value) && EQ (XCAR (value), Qspace))
3067 || valid_image_p (value));
3068 #else /* not HAVE_WINDOW_SYSTEM */
3069 valid_p = STRINGP (value);
3070 #endif /* not HAVE_WINDOW_SYSTEM */
3071
3072 if ((EQ (location, Qleft_margin)
3073 || EQ (location, Qright_margin)
3074 || NILP (location))
3075 && valid_p
3076 && !display_replaced_before_p)
3077 {
3078 replaces_text_display_p = 1;
3079
3080 /* Save current settings of IT so that we can restore them
3081 when we are finished with the glyph property value. */
3082 push_it (it);
3083
3084 if (NILP (location))
3085 it->area = TEXT_AREA;
3086 else if (EQ (location, Qleft_margin))
3087 it->area = LEFT_MARGIN_AREA;
3088 else
3089 it->area = RIGHT_MARGIN_AREA;
3090
3091 if (STRINGP (value))
3092 {
3093 it->string = value;
3094 it->multibyte_p = STRING_MULTIBYTE (it->string);
3095 it->current.overlay_string_index = -1;
3096 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3097 it->end_charpos = it->string_nchars = SCHARS (it->string);
3098 it->method = next_element_from_string;
3099 it->stop_charpos = 0;
3100 it->string_from_display_prop_p = 1;
3101 /* Say that we haven't consumed the characters with
3102 `display' property yet. The call to pop_it in
3103 set_iterator_to_next will clean this up. */
3104 *position = start_pos;
3105 }
3106 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3107 {
3108 it->method = next_element_from_stretch;
3109 it->object = value;
3110 it->current.pos = it->position = start_pos;
3111 }
3112 #ifdef HAVE_WINDOW_SYSTEM
3113 else
3114 {
3115 it->what = IT_IMAGE;
3116 it->image_id = lookup_image (it->f, value);
3117 it->position = start_pos;
3118 it->object = NILP (object) ? it->w->buffer : object;
3119 it->method = next_element_from_image;
3120
3121 /* Say that we haven't consumed the characters with
3122 `display' property yet. The call to pop_it in
3123 set_iterator_to_next will clean this up. */
3124 *position = start_pos;
3125 }
3126 #endif /* HAVE_WINDOW_SYSTEM */
3127 }
3128 else
3129 /* Invalid property or property not supported. Restore
3130 the position to what it was before. */
3131 *position = start_pos;
3132 }
3133
3134 return replaces_text_display_p;
3135 }
3136
3137
3138 /* Check if PROP is a display sub-property value whose text should be
3139 treated as intangible. */
3140
3141 static int
3142 single_display_prop_intangible_p (prop)
3143 Lisp_Object prop;
3144 {
3145 /* Skip over `when FORM'. */
3146 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3147 {
3148 prop = XCDR (prop);
3149 if (!CONSP (prop))
3150 return 0;
3151 prop = XCDR (prop);
3152 }
3153
3154 if (!CONSP (prop))
3155 return 0;
3156
3157 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3158 we don't need to treat text as intangible. */
3159 if (EQ (XCAR (prop), Qmargin))
3160 {
3161 prop = XCDR (prop);
3162 if (!CONSP (prop))
3163 return 0;
3164
3165 prop = XCDR (prop);
3166 if (!CONSP (prop)
3167 || EQ (XCAR (prop), Qleft_margin)
3168 || EQ (XCAR (prop), Qright_margin))
3169 return 0;
3170 }
3171
3172 return CONSP (prop) && EQ (XCAR (prop), Qimage);
3173 }
3174
3175
3176 /* Check if PROP is a display property value whose text should be
3177 treated as intangible. */
3178
3179 int
3180 display_prop_intangible_p (prop)
3181 Lisp_Object prop;
3182 {
3183 if (CONSP (prop)
3184 && CONSP (XCAR (prop))
3185 && !EQ (Qmargin, XCAR (XCAR (prop))))
3186 {
3187 /* A list of sub-properties. */
3188 while (CONSP (prop))
3189 {
3190 if (single_display_prop_intangible_p (XCAR (prop)))
3191 return 1;
3192 prop = XCDR (prop);
3193 }
3194 }
3195 else if (VECTORP (prop))
3196 {
3197 /* A vector of sub-properties. */
3198 int i;
3199 for (i = 0; i < ASIZE (prop); ++i)
3200 if (single_display_prop_intangible_p (AREF (prop, i)))
3201 return 1;
3202 }
3203 else
3204 return single_display_prop_intangible_p (prop);
3205
3206 return 0;
3207 }
3208
3209
3210 /* Return 1 if PROP is a display sub-property value containing STRING. */
3211
3212 static int
3213 single_display_prop_string_p (prop, string)
3214 Lisp_Object prop, string;
3215 {
3216 if (EQ (string, prop))
3217 return 1;
3218
3219 /* Skip over `when FORM'. */
3220 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3221 {
3222 prop = XCDR (prop);
3223 if (!CONSP (prop))
3224 return 0;
3225 prop = XCDR (prop);
3226 }
3227
3228 if (CONSP (prop))
3229 /* Skip over `margin LOCATION'. */
3230 if (EQ (XCAR (prop), Qmargin))
3231 {
3232 prop = XCDR (prop);
3233 if (!CONSP (prop))
3234 return 0;
3235
3236 prop = XCDR (prop);
3237 if (!CONSP (prop))
3238 return 0;
3239 }
3240
3241 return CONSP (prop) && EQ (XCAR (prop), string);
3242 }
3243
3244
3245 /* Return 1 if STRING appears in the `display' property PROP. */
3246
3247 static int
3248 display_prop_string_p (prop, string)
3249 Lisp_Object prop, string;
3250 {
3251 if (CONSP (prop)
3252 && CONSP (XCAR (prop))
3253 && !EQ (Qmargin, XCAR (XCAR (prop))))
3254 {
3255 /* A list of sub-properties. */
3256 while (CONSP (prop))
3257 {
3258 if (single_display_prop_string_p (XCAR (prop), string))
3259 return 1;
3260 prop = XCDR (prop);
3261 }
3262 }
3263 else if (VECTORP (prop))
3264 {
3265 /* A vector of sub-properties. */
3266 int i;
3267 for (i = 0; i < ASIZE (prop); ++i)
3268 if (single_display_prop_string_p (AREF (prop, i), string))
3269 return 1;
3270 }
3271 else
3272 return single_display_prop_string_p (prop, string);
3273
3274 return 0;
3275 }
3276
3277
3278 /* Determine from which buffer position in W's buffer STRING comes
3279 from. AROUND_CHARPOS is an approximate position where it could
3280 be from. Value is the buffer position or 0 if it couldn't be
3281 determined.
3282
3283 W's buffer must be current.
3284
3285 This function is necessary because we don't record buffer positions
3286 in glyphs generated from strings (to keep struct glyph small).
3287 This function may only use code that doesn't eval because it is
3288 called asynchronously from note_mouse_highlight. */
3289
3290 int
3291 string_buffer_position (w, string, around_charpos)
3292 struct window *w;
3293 Lisp_Object string;
3294 int around_charpos;
3295 {
3296 Lisp_Object limit, prop, pos;
3297 const int MAX_DISTANCE = 1000;
3298 int found = 0;
3299
3300 pos = make_number (around_charpos);
3301 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
3302 while (!found && !EQ (pos, limit))
3303 {
3304 prop = Fget_char_property (pos, Qdisplay, Qnil);
3305 if (!NILP (prop) && display_prop_string_p (prop, string))
3306 found = 1;
3307 else
3308 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
3309 }
3310
3311 if (!found)
3312 {
3313 pos = make_number (around_charpos);
3314 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
3315 while (!found && !EQ (pos, limit))
3316 {
3317 prop = Fget_char_property (pos, Qdisplay, Qnil);
3318 if (!NILP (prop) && display_prop_string_p (prop, string))
3319 found = 1;
3320 else
3321 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
3322 limit);
3323 }
3324 }
3325
3326 return found ? XINT (pos) : 0;
3327 }
3328
3329
3330 \f
3331 /***********************************************************************
3332 `composition' property
3333 ***********************************************************************/
3334
3335 /* Set up iterator IT from `composition' property at its current
3336 position. Called from handle_stop. */
3337
3338 static enum prop_handled
3339 handle_composition_prop (it)
3340 struct it *it;
3341 {
3342 Lisp_Object prop, string;
3343 int pos, pos_byte, end;
3344 enum prop_handled handled = HANDLED_NORMALLY;
3345
3346 if (STRINGP (it->string))
3347 {
3348 pos = IT_STRING_CHARPOS (*it);
3349 pos_byte = IT_STRING_BYTEPOS (*it);
3350 string = it->string;
3351 }
3352 else
3353 {
3354 pos = IT_CHARPOS (*it);
3355 pos_byte = IT_BYTEPOS (*it);
3356 string = Qnil;
3357 }
3358
3359 /* If there's a valid composition and point is not inside of the
3360 composition (in the case that the composition is from the current
3361 buffer), draw a glyph composed from the composition components. */
3362 if (find_composition (pos, -1, &pos, &end, &prop, string)
3363 && COMPOSITION_VALID_P (pos, end, prop)
3364 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
3365 {
3366 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
3367
3368 if (id >= 0)
3369 {
3370 it->method = next_element_from_composition;
3371 it->cmp_id = id;
3372 it->cmp_len = COMPOSITION_LENGTH (prop);
3373 /* For a terminal, draw only the first character of the
3374 components. */
3375 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
3376 it->len = (STRINGP (it->string)
3377 ? string_char_to_byte (it->string, end)
3378 : CHAR_TO_BYTE (end)) - pos_byte;
3379 it->stop_charpos = end;
3380 handled = HANDLED_RETURN;
3381 }
3382 }
3383
3384 return handled;
3385 }
3386
3387
3388 \f
3389 /***********************************************************************
3390 Overlay strings
3391 ***********************************************************************/
3392
3393 /* The following structure is used to record overlay strings for
3394 later sorting in load_overlay_strings. */
3395
3396 struct overlay_entry
3397 {
3398 Lisp_Object overlay;
3399 Lisp_Object string;
3400 int priority;
3401 int after_string_p;
3402 };
3403
3404
3405 /* Set up iterator IT from overlay strings at its current position.
3406 Called from handle_stop. */
3407
3408 static enum prop_handled
3409 handle_overlay_change (it)
3410 struct it *it;
3411 {
3412 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
3413 return HANDLED_RECOMPUTE_PROPS;
3414 else
3415 return HANDLED_NORMALLY;
3416 }
3417
3418
3419 /* Set up the next overlay string for delivery by IT, if there is an
3420 overlay string to deliver. Called by set_iterator_to_next when the
3421 end of the current overlay string is reached. If there are more
3422 overlay strings to display, IT->string and
3423 IT->current.overlay_string_index are set appropriately here.
3424 Otherwise IT->string is set to nil. */
3425
3426 static void
3427 next_overlay_string (it)
3428 struct it *it;
3429 {
3430 ++it->current.overlay_string_index;
3431 if (it->current.overlay_string_index == it->n_overlay_strings)
3432 {
3433 /* No more overlay strings. Restore IT's settings to what
3434 they were before overlay strings were processed, and
3435 continue to deliver from current_buffer. */
3436 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
3437
3438 pop_it (it);
3439 xassert (it->stop_charpos >= BEGV
3440 && it->stop_charpos <= it->end_charpos);
3441 it->string = Qnil;
3442 it->current.overlay_string_index = -1;
3443 SET_TEXT_POS (it->current.string_pos, -1, -1);
3444 it->n_overlay_strings = 0;
3445 it->method = next_element_from_buffer;
3446
3447 /* If we're at the end of the buffer, record that we have
3448 processed the overlay strings there already, so that
3449 next_element_from_buffer doesn't try it again. */
3450 if (IT_CHARPOS (*it) >= it->end_charpos)
3451 it->overlay_strings_at_end_processed_p = 1;
3452
3453 /* If we have to display `...' for invisible text, set
3454 the iterator up for that. */
3455 if (display_ellipsis_p)
3456 setup_for_ellipsis (it);
3457 }
3458 else
3459 {
3460 /* There are more overlay strings to process. If
3461 IT->current.overlay_string_index has advanced to a position
3462 where we must load IT->overlay_strings with more strings, do
3463 it. */
3464 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
3465
3466 if (it->current.overlay_string_index && i == 0)
3467 load_overlay_strings (it, 0);
3468
3469 /* Initialize IT to deliver display elements from the overlay
3470 string. */
3471 it->string = it->overlay_strings[i];
3472 it->multibyte_p = STRING_MULTIBYTE (it->string);
3473 SET_TEXT_POS (it->current.string_pos, 0, 0);
3474 it->method = next_element_from_string;
3475 it->stop_charpos = 0;
3476 }
3477
3478 CHECK_IT (it);
3479 }
3480
3481
3482 /* Compare two overlay_entry structures E1 and E2. Used as a
3483 comparison function for qsort in load_overlay_strings. Overlay
3484 strings for the same position are sorted so that
3485
3486 1. All after-strings come in front of before-strings, except
3487 when they come from the same overlay.
3488
3489 2. Within after-strings, strings are sorted so that overlay strings
3490 from overlays with higher priorities come first.
3491
3492 2. Within before-strings, strings are sorted so that overlay
3493 strings from overlays with higher priorities come last.
3494
3495 Value is analogous to strcmp. */
3496
3497
3498 static int
3499 compare_overlay_entries (e1, e2)
3500 void *e1, *e2;
3501 {
3502 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
3503 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
3504 int result;
3505
3506 if (entry1->after_string_p != entry2->after_string_p)
3507 {
3508 /* Let after-strings appear in front of before-strings if
3509 they come from different overlays. */
3510 if (EQ (entry1->overlay, entry2->overlay))
3511 result = entry1->after_string_p ? 1 : -1;
3512 else
3513 result = entry1->after_string_p ? -1 : 1;
3514 }
3515 else if (entry1->after_string_p)
3516 /* After-strings sorted in order of decreasing priority. */
3517 result = entry2->priority - entry1->priority;
3518 else
3519 /* Before-strings sorted in order of increasing priority. */
3520 result = entry1->priority - entry2->priority;
3521
3522 return result;
3523 }
3524
3525
3526 /* Load the vector IT->overlay_strings with overlay strings from IT's
3527 current buffer position, or from CHARPOS if that is > 0. Set
3528 IT->n_overlays to the total number of overlay strings found.
3529
3530 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
3531 a time. On entry into load_overlay_strings,
3532 IT->current.overlay_string_index gives the number of overlay
3533 strings that have already been loaded by previous calls to this
3534 function.
3535
3536 IT->add_overlay_start contains an additional overlay start
3537 position to consider for taking overlay strings from, if non-zero.
3538 This position comes into play when the overlay has an `invisible'
3539 property, and both before and after-strings. When we've skipped to
3540 the end of the overlay, because of its `invisible' property, we
3541 nevertheless want its before-string to appear.
3542 IT->add_overlay_start will contain the overlay start position
3543 in this case.
3544
3545 Overlay strings are sorted so that after-string strings come in
3546 front of before-string strings. Within before and after-strings,
3547 strings are sorted by overlay priority. See also function
3548 compare_overlay_entries. */
3549
3550 static void
3551 load_overlay_strings (it, charpos)
3552 struct it *it;
3553 int charpos;
3554 {
3555 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
3556 Lisp_Object ov, overlay, window, str, invisible;
3557 int start, end;
3558 int size = 20;
3559 int n = 0, i, j, invis_p;
3560 struct overlay_entry *entries
3561 = (struct overlay_entry *) alloca (size * sizeof *entries);
3562
3563 if (charpos <= 0)
3564 charpos = IT_CHARPOS (*it);
3565
3566 /* Append the overlay string STRING of overlay OVERLAY to vector
3567 `entries' which has size `size' and currently contains `n'
3568 elements. AFTER_P non-zero means STRING is an after-string of
3569 OVERLAY. */
3570 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
3571 do \
3572 { \
3573 Lisp_Object priority; \
3574 \
3575 if (n == size) \
3576 { \
3577 int new_size = 2 * size; \
3578 struct overlay_entry *old = entries; \
3579 entries = \
3580 (struct overlay_entry *) alloca (new_size \
3581 * sizeof *entries); \
3582 bcopy (old, entries, size * sizeof *entries); \
3583 size = new_size; \
3584 } \
3585 \
3586 entries[n].string = (STRING); \
3587 entries[n].overlay = (OVERLAY); \
3588 priority = Foverlay_get ((OVERLAY), Qpriority); \
3589 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
3590 entries[n].after_string_p = (AFTER_P); \
3591 ++n; \
3592 } \
3593 while (0)
3594
3595 /* Process overlay before the overlay center. */
3596 for (ov = current_buffer->overlays_before; CONSP (ov); ov = XCDR (ov))
3597 {
3598 overlay = XCAR (ov);
3599 xassert (OVERLAYP (overlay));
3600 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3601 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3602
3603 if (end < charpos)
3604 break;
3605
3606 /* Skip this overlay if it doesn't start or end at IT's current
3607 position. */
3608 if (end != charpos && start != charpos)
3609 continue;
3610
3611 /* Skip this overlay if it doesn't apply to IT->w. */
3612 window = Foverlay_get (overlay, Qwindow);
3613 if (WINDOWP (window) && XWINDOW (window) != it->w)
3614 continue;
3615
3616 /* If the text ``under'' the overlay is invisible, both before-
3617 and after-strings from this overlay are visible; start and
3618 end position are indistinguishable. */
3619 invisible = Foverlay_get (overlay, Qinvisible);
3620 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3621
3622 /* If overlay has a non-empty before-string, record it. */
3623 if ((start == charpos || (end == charpos && invis_p))
3624 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3625 && SCHARS (str))
3626 RECORD_OVERLAY_STRING (overlay, str, 0);
3627
3628 /* If overlay has a non-empty after-string, record it. */
3629 if ((end == charpos || (start == charpos && invis_p))
3630 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3631 && SCHARS (str))
3632 RECORD_OVERLAY_STRING (overlay, str, 1);
3633 }
3634
3635 /* Process overlays after the overlay center. */
3636 for (ov = current_buffer->overlays_after; CONSP (ov); ov = XCDR (ov))
3637 {
3638 overlay = XCAR (ov);
3639 xassert (OVERLAYP (overlay));
3640 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3641 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3642
3643 if (start > charpos)
3644 break;
3645
3646 /* Skip this overlay if it doesn't start or end at IT's current
3647 position. */
3648 if (end != charpos && start != charpos)
3649 continue;
3650
3651 /* Skip this overlay if it doesn't apply to IT->w. */
3652 window = Foverlay_get (overlay, Qwindow);
3653 if (WINDOWP (window) && XWINDOW (window) != it->w)
3654 continue;
3655
3656 /* If the text ``under'' the overlay is invisible, it has a zero
3657 dimension, and both before- and after-strings apply. */
3658 invisible = Foverlay_get (overlay, Qinvisible);
3659 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3660
3661 /* If overlay has a non-empty before-string, record it. */
3662 if ((start == charpos || (end == charpos && invis_p))
3663 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3664 && SCHARS (str))
3665 RECORD_OVERLAY_STRING (overlay, str, 0);
3666
3667 /* If overlay has a non-empty after-string, record it. */
3668 if ((end == charpos || (start == charpos && invis_p))
3669 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3670 && SCHARS (str))
3671 RECORD_OVERLAY_STRING (overlay, str, 1);
3672 }
3673
3674 #undef RECORD_OVERLAY_STRING
3675
3676 /* Sort entries. */
3677 if (n > 1)
3678 qsort (entries, n, sizeof *entries, compare_overlay_entries);
3679
3680 /* Record the total number of strings to process. */
3681 it->n_overlay_strings = n;
3682
3683 /* IT->current.overlay_string_index is the number of overlay strings
3684 that have already been consumed by IT. Copy some of the
3685 remaining overlay strings to IT->overlay_strings. */
3686 i = 0;
3687 j = it->current.overlay_string_index;
3688 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
3689 it->overlay_strings[i++] = entries[j++].string;
3690
3691 CHECK_IT (it);
3692 }
3693
3694
3695 /* Get the first chunk of overlay strings at IT's current buffer
3696 position, or at CHARPOS if that is > 0. Value is non-zero if at
3697 least one overlay string was found. */
3698
3699 static int
3700 get_overlay_strings (it, charpos)
3701 struct it *it;
3702 int charpos;
3703 {
3704 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
3705 process. This fills IT->overlay_strings with strings, and sets
3706 IT->n_overlay_strings to the total number of strings to process.
3707 IT->pos.overlay_string_index has to be set temporarily to zero
3708 because load_overlay_strings needs this; it must be set to -1
3709 when no overlay strings are found because a zero value would
3710 indicate a position in the first overlay string. */
3711 it->current.overlay_string_index = 0;
3712 load_overlay_strings (it, charpos);
3713
3714 /* If we found overlay strings, set up IT to deliver display
3715 elements from the first one. Otherwise set up IT to deliver
3716 from current_buffer. */
3717 if (it->n_overlay_strings)
3718 {
3719 /* Make sure we know settings in current_buffer, so that we can
3720 restore meaningful values when we're done with the overlay
3721 strings. */
3722 compute_stop_pos (it);
3723 xassert (it->face_id >= 0);
3724
3725 /* Save IT's settings. They are restored after all overlay
3726 strings have been processed. */
3727 xassert (it->sp == 0);
3728 push_it (it);
3729
3730 /* Set up IT to deliver display elements from the first overlay
3731 string. */
3732 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3733 it->string = it->overlay_strings[0];
3734 it->stop_charpos = 0;
3735 xassert (STRINGP (it->string));
3736 it->end_charpos = SCHARS (it->string);
3737 it->multibyte_p = STRING_MULTIBYTE (it->string);
3738 it->method = next_element_from_string;
3739 }
3740 else
3741 {
3742 it->string = Qnil;
3743 it->current.overlay_string_index = -1;
3744 it->method = next_element_from_buffer;
3745 }
3746
3747 CHECK_IT (it);
3748
3749 /* Value is non-zero if we found at least one overlay string. */
3750 return STRINGP (it->string);
3751 }
3752
3753
3754 \f
3755 /***********************************************************************
3756 Saving and restoring state
3757 ***********************************************************************/
3758
3759 /* Save current settings of IT on IT->stack. Called, for example,
3760 before setting up IT for an overlay string, to be able to restore
3761 IT's settings to what they were after the overlay string has been
3762 processed. */
3763
3764 static void
3765 push_it (it)
3766 struct it *it;
3767 {
3768 struct iterator_stack_entry *p;
3769
3770 xassert (it->sp < 2);
3771 p = it->stack + it->sp;
3772
3773 p->stop_charpos = it->stop_charpos;
3774 xassert (it->face_id >= 0);
3775 p->face_id = it->face_id;
3776 p->string = it->string;
3777 p->pos = it->current;
3778 p->end_charpos = it->end_charpos;
3779 p->string_nchars = it->string_nchars;
3780 p->area = it->area;
3781 p->multibyte_p = it->multibyte_p;
3782 p->space_width = it->space_width;
3783 p->font_height = it->font_height;
3784 p->voffset = it->voffset;
3785 p->string_from_display_prop_p = it->string_from_display_prop_p;
3786 p->display_ellipsis_p = 0;
3787 ++it->sp;
3788 }
3789
3790
3791 /* Restore IT's settings from IT->stack. Called, for example, when no
3792 more overlay strings must be processed, and we return to delivering
3793 display elements from a buffer, or when the end of a string from a
3794 `display' property is reached and we return to delivering display
3795 elements from an overlay string, or from a buffer. */
3796
3797 static void
3798 pop_it (it)
3799 struct it *it;
3800 {
3801 struct iterator_stack_entry *p;
3802
3803 xassert (it->sp > 0);
3804 --it->sp;
3805 p = it->stack + it->sp;
3806 it->stop_charpos = p->stop_charpos;
3807 it->face_id = p->face_id;
3808 it->string = p->string;
3809 it->current = p->pos;
3810 it->end_charpos = p->end_charpos;
3811 it->string_nchars = p->string_nchars;
3812 it->area = p->area;
3813 it->multibyte_p = p->multibyte_p;
3814 it->space_width = p->space_width;
3815 it->font_height = p->font_height;
3816 it->voffset = p->voffset;
3817 it->string_from_display_prop_p = p->string_from_display_prop_p;
3818 }
3819
3820
3821 \f
3822 /***********************************************************************
3823 Moving over lines
3824 ***********************************************************************/
3825
3826 /* Set IT's current position to the previous line start. */
3827
3828 static void
3829 back_to_previous_line_start (it)
3830 struct it *it;
3831 {
3832 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
3833 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3834 }
3835
3836
3837 /* Move IT to the next line start.
3838
3839 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
3840 we skipped over part of the text (as opposed to moving the iterator
3841 continuously over the text). Otherwise, don't change the value
3842 of *SKIPPED_P.
3843
3844 Newlines may come from buffer text, overlay strings, or strings
3845 displayed via the `display' property. That's the reason we can't
3846 simply use find_next_newline_no_quit.
3847
3848 Note that this function may not skip over invisible text that is so
3849 because of text properties and immediately follows a newline. If
3850 it would, function reseat_at_next_visible_line_start, when called
3851 from set_iterator_to_next, would effectively make invisible
3852 characters following a newline part of the wrong glyph row, which
3853 leads to wrong cursor motion. */
3854
3855 static int
3856 forward_to_next_line_start (it, skipped_p)
3857 struct it *it;
3858 int *skipped_p;
3859 {
3860 int old_selective, newline_found_p, n;
3861 const int MAX_NEWLINE_DISTANCE = 500;
3862
3863 /* If already on a newline, just consume it to avoid unintended
3864 skipping over invisible text below. */
3865 if (it->what == IT_CHARACTER
3866 && it->c == '\n'
3867 && CHARPOS (it->position) == IT_CHARPOS (*it))
3868 {
3869 set_iterator_to_next (it, 0);
3870 it->c = 0;
3871 return 1;
3872 }
3873
3874 /* Don't handle selective display in the following. It's (a)
3875 unnecessary because it's done by the caller, and (b) leads to an
3876 infinite recursion because next_element_from_ellipsis indirectly
3877 calls this function. */
3878 old_selective = it->selective;
3879 it->selective = 0;
3880
3881 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
3882 from buffer text. */
3883 for (n = newline_found_p = 0;
3884 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
3885 n += STRINGP (it->string) ? 0 : 1)
3886 {
3887 if (!get_next_display_element (it))
3888 return 0;
3889 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
3890 set_iterator_to_next (it, 0);
3891 }
3892
3893 /* If we didn't find a newline near enough, see if we can use a
3894 short-cut. */
3895 if (!newline_found_p)
3896 {
3897 int start = IT_CHARPOS (*it);
3898 int limit = find_next_newline_no_quit (start, 1);
3899 Lisp_Object pos;
3900
3901 xassert (!STRINGP (it->string));
3902
3903 /* If there isn't any `display' property in sight, and no
3904 overlays, we can just use the position of the newline in
3905 buffer text. */
3906 if (it->stop_charpos >= limit
3907 || ((pos = Fnext_single_property_change (make_number (start),
3908 Qdisplay,
3909 Qnil, make_number (limit)),
3910 NILP (pos))
3911 && next_overlay_change (start) == ZV))
3912 {
3913 IT_CHARPOS (*it) = limit;
3914 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
3915 *skipped_p = newline_found_p = 1;
3916 }
3917 else
3918 {
3919 while (get_next_display_element (it)
3920 && !newline_found_p)
3921 {
3922 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
3923 set_iterator_to_next (it, 0);
3924 }
3925 }
3926 }
3927
3928 it->selective = old_selective;
3929 return newline_found_p;
3930 }
3931
3932
3933 /* Set IT's current position to the previous visible line start. Skip
3934 invisible text that is so either due to text properties or due to
3935 selective display. Caution: this does not change IT->current_x and
3936 IT->hpos. */
3937
3938 static void
3939 back_to_previous_visible_line_start (it)
3940 struct it *it;
3941 {
3942 int visible_p = 0;
3943
3944 /* Go back one newline if not on BEGV already. */
3945 if (IT_CHARPOS (*it) > BEGV)
3946 back_to_previous_line_start (it);
3947
3948 /* Move over lines that are invisible because of selective display
3949 or text properties. */
3950 while (IT_CHARPOS (*it) > BEGV
3951 && !visible_p)
3952 {
3953 visible_p = 1;
3954
3955 /* If selective > 0, then lines indented more than that values
3956 are invisible. */
3957 if (it->selective > 0
3958 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3959 (double) it->selective)) /* iftc */
3960 visible_p = 0;
3961 else
3962 {
3963 Lisp_Object prop;
3964
3965 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
3966 Qinvisible, it->window);
3967 if (TEXT_PROP_MEANS_INVISIBLE (prop))
3968 visible_p = 0;
3969 }
3970
3971 /* Back one more newline if the current one is invisible. */
3972 if (!visible_p)
3973 back_to_previous_line_start (it);
3974 }
3975
3976 xassert (IT_CHARPOS (*it) >= BEGV);
3977 xassert (IT_CHARPOS (*it) == BEGV
3978 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3979 CHECK_IT (it);
3980 }
3981
3982
3983 /* Reseat iterator IT at the previous visible line start. Skip
3984 invisible text that is so either due to text properties or due to
3985 selective display. At the end, update IT's overlay information,
3986 face information etc. */
3987
3988 static void
3989 reseat_at_previous_visible_line_start (it)
3990 struct it *it;
3991 {
3992 back_to_previous_visible_line_start (it);
3993 reseat (it, it->current.pos, 1);
3994 CHECK_IT (it);
3995 }
3996
3997
3998 /* Reseat iterator IT on the next visible line start in the current
3999 buffer. ON_NEWLINE_P non-zero means position IT on the newline
4000 preceding the line start. Skip over invisible text that is so
4001 because of selective display. Compute faces, overlays etc at the
4002 new position. Note that this function does not skip over text that
4003 is invisible because of text properties. */
4004
4005 static void
4006 reseat_at_next_visible_line_start (it, on_newline_p)
4007 struct it *it;
4008 int on_newline_p;
4009 {
4010 int newline_found_p, skipped_p = 0;
4011
4012 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4013
4014 /* Skip over lines that are invisible because they are indented
4015 more than the value of IT->selective. */
4016 if (it->selective > 0)
4017 while (IT_CHARPOS (*it) < ZV
4018 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4019 (double) it->selective)) /* iftc */
4020 {
4021 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4022 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4023 }
4024
4025 /* Position on the newline if that's what's requested. */
4026 if (on_newline_p && newline_found_p)
4027 {
4028 if (STRINGP (it->string))
4029 {
4030 if (IT_STRING_CHARPOS (*it) > 0)
4031 {
4032 --IT_STRING_CHARPOS (*it);
4033 --IT_STRING_BYTEPOS (*it);
4034 }
4035 }
4036 else if (IT_CHARPOS (*it) > BEGV)
4037 {
4038 --IT_CHARPOS (*it);
4039 --IT_BYTEPOS (*it);
4040 reseat (it, it->current.pos, 0);
4041 }
4042 }
4043 else if (skipped_p)
4044 reseat (it, it->current.pos, 0);
4045
4046 CHECK_IT (it);
4047 }
4048
4049
4050 \f
4051 /***********************************************************************
4052 Changing an iterator's position
4053 ***********************************************************************/
4054
4055 /* Change IT's current position to POS in current_buffer. If FORCE_P
4056 is non-zero, always check for text properties at the new position.
4057 Otherwise, text properties are only looked up if POS >=
4058 IT->check_charpos of a property. */
4059
4060 static void
4061 reseat (it, pos, force_p)
4062 struct it *it;
4063 struct text_pos pos;
4064 int force_p;
4065 {
4066 int original_pos = IT_CHARPOS (*it);
4067
4068 reseat_1 (it, pos, 0);
4069
4070 /* Determine where to check text properties. Avoid doing it
4071 where possible because text property lookup is very expensive. */
4072 if (force_p
4073 || CHARPOS (pos) > it->stop_charpos
4074 || CHARPOS (pos) < original_pos)
4075 handle_stop (it);
4076
4077 CHECK_IT (it);
4078 }
4079
4080
4081 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4082 IT->stop_pos to POS, also. */
4083
4084 static void
4085 reseat_1 (it, pos, set_stop_p)
4086 struct it *it;
4087 struct text_pos pos;
4088 int set_stop_p;
4089 {
4090 /* Don't call this function when scanning a C string. */
4091 xassert (it->s == NULL);
4092
4093 /* POS must be a reasonable value. */
4094 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4095
4096 it->current.pos = it->position = pos;
4097 XSETBUFFER (it->object, current_buffer);
4098 it->end_charpos = ZV;
4099 it->dpvec = NULL;
4100 it->current.dpvec_index = -1;
4101 it->current.overlay_string_index = -1;
4102 IT_STRING_CHARPOS (*it) = -1;
4103 IT_STRING_BYTEPOS (*it) = -1;
4104 it->string = Qnil;
4105 it->method = next_element_from_buffer;
4106 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
4107 it->sp = 0;
4108 it->face_before_selective_p = 0;
4109
4110 if (set_stop_p)
4111 it->stop_charpos = CHARPOS (pos);
4112 }
4113
4114
4115 /* Set up IT for displaying a string, starting at CHARPOS in window W.
4116 If S is non-null, it is a C string to iterate over. Otherwise,
4117 STRING gives a Lisp string to iterate over.
4118
4119 If PRECISION > 0, don't return more then PRECISION number of
4120 characters from the string.
4121
4122 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4123 characters have been returned. FIELD_WIDTH < 0 means an infinite
4124 field width.
4125
4126 MULTIBYTE = 0 means disable processing of multibyte characters,
4127 MULTIBYTE > 0 means enable it,
4128 MULTIBYTE < 0 means use IT->multibyte_p.
4129
4130 IT must be initialized via a prior call to init_iterator before
4131 calling this function. */
4132
4133 static void
4134 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4135 struct it *it;
4136 unsigned char *s;
4137 Lisp_Object string;
4138 int charpos;
4139 int precision, field_width, multibyte;
4140 {
4141 /* No region in strings. */
4142 it->region_beg_charpos = it->region_end_charpos = -1;
4143
4144 /* No text property checks performed by default, but see below. */
4145 it->stop_charpos = -1;
4146
4147 /* Set iterator position and end position. */
4148 bzero (&it->current, sizeof it->current);
4149 it->current.overlay_string_index = -1;
4150 it->current.dpvec_index = -1;
4151 xassert (charpos >= 0);
4152
4153 /* If STRING is specified, use its multibyteness, otherwise use the
4154 setting of MULTIBYTE, if specified. */
4155 if (multibyte >= 0)
4156 it->multibyte_p = multibyte > 0;
4157
4158 if (s == NULL)
4159 {
4160 xassert (STRINGP (string));
4161 it->string = string;
4162 it->s = NULL;
4163 it->end_charpos = it->string_nchars = SCHARS (string);
4164 it->method = next_element_from_string;
4165 it->current.string_pos = string_pos (charpos, string);
4166 }
4167 else
4168 {
4169 it->s = s;
4170 it->string = Qnil;
4171
4172 /* Note that we use IT->current.pos, not it->current.string_pos,
4173 for displaying C strings. */
4174 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4175 if (it->multibyte_p)
4176 {
4177 it->current.pos = c_string_pos (charpos, s, 1);
4178 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4179 }
4180 else
4181 {
4182 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4183 it->end_charpos = it->string_nchars = strlen (s);
4184 }
4185
4186 it->method = next_element_from_c_string;
4187 }
4188
4189 /* PRECISION > 0 means don't return more than PRECISION characters
4190 from the string. */
4191 if (precision > 0 && it->end_charpos - charpos > precision)
4192 it->end_charpos = it->string_nchars = charpos + precision;
4193
4194 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4195 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4196 FIELD_WIDTH < 0 means infinite field width. This is useful for
4197 padding with `-' at the end of a mode line. */
4198 if (field_width < 0)
4199 field_width = INFINITY;
4200 if (field_width > it->end_charpos - charpos)
4201 it->end_charpos = charpos + field_width;
4202
4203 /* Use the standard display table for displaying strings. */
4204 if (DISP_TABLE_P (Vstandard_display_table))
4205 it->dp = XCHAR_TABLE (Vstandard_display_table);
4206
4207 it->stop_charpos = charpos;
4208 CHECK_IT (it);
4209 }
4210
4211
4212 \f
4213 /***********************************************************************
4214 Iteration
4215 ***********************************************************************/
4216
4217 /* Load IT's display element fields with information about the next
4218 display element from the current position of IT. Value is zero if
4219 end of buffer (or C string) is reached. */
4220
4221 int
4222 get_next_display_element (it)
4223 struct it *it;
4224 {
4225 /* Non-zero means that we found an display element. Zero means that
4226 we hit the end of what we iterate over. Performance note: the
4227 function pointer `method' used here turns out to be faster than
4228 using a sequence of if-statements. */
4229 int success_p = (*it->method) (it);
4230
4231 if (it->what == IT_CHARACTER)
4232 {
4233 /* Map via display table or translate control characters.
4234 IT->c, IT->len etc. have been set to the next character by
4235 the function call above. If we have a display table, and it
4236 contains an entry for IT->c, translate it. Don't do this if
4237 IT->c itself comes from a display table, otherwise we could
4238 end up in an infinite recursion. (An alternative could be to
4239 count the recursion depth of this function and signal an
4240 error when a certain maximum depth is reached.) Is it worth
4241 it? */
4242 if (success_p && it->dpvec == NULL)
4243 {
4244 Lisp_Object dv;
4245
4246 if (it->dp
4247 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
4248 VECTORP (dv)))
4249 {
4250 struct Lisp_Vector *v = XVECTOR (dv);
4251
4252 /* Return the first character from the display table
4253 entry, if not empty. If empty, don't display the
4254 current character. */
4255 if (v->size)
4256 {
4257 it->dpvec_char_len = it->len;
4258 it->dpvec = v->contents;
4259 it->dpend = v->contents + v->size;
4260 it->current.dpvec_index = 0;
4261 it->method = next_element_from_display_vector;
4262 success_p = get_next_display_element (it);
4263 }
4264 else
4265 {
4266 set_iterator_to_next (it, 0);
4267 success_p = get_next_display_element (it);
4268 }
4269 }
4270
4271 /* Translate control characters into `\003' or `^C' form.
4272 Control characters coming from a display table entry are
4273 currently not translated because we use IT->dpvec to hold
4274 the translation. This could easily be changed but I
4275 don't believe that it is worth doing.
4276
4277 If it->multibyte_p is nonzero, eight-bit characters and
4278 non-printable multibyte characters are also translated to
4279 octal form.
4280
4281 If it->multibyte_p is zero, eight-bit characters that
4282 don't have corresponding multibyte char code are also
4283 translated to octal form. */
4284 else if (((it->c < ' ' || it->c == 127)
4285 && (it->area != TEXT_AREA
4286 || (it->c != '\n' && it->c != '\t')))
4287 || (it->multibyte_p
4288 ? ((it->c >= 127
4289 && it->len == 1)
4290 || !CHAR_PRINTABLE_P (it->c))
4291 : (it->c >= 128
4292 && it->c == unibyte_char_to_multibyte (it->c))))
4293 {
4294 /* IT->c is a control character which must be displayed
4295 either as '\003' or as `^C' where the '\\' and '^'
4296 can be defined in the display table. Fill
4297 IT->ctl_chars with glyphs for what we have to
4298 display. Then, set IT->dpvec to these glyphs. */
4299 GLYPH g;
4300
4301 if (it->c < 128 && it->ctl_arrow_p)
4302 {
4303 /* Set IT->ctl_chars[0] to the glyph for `^'. */
4304 if (it->dp
4305 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
4306 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
4307 g = XINT (DISP_CTRL_GLYPH (it->dp));
4308 else
4309 g = FAST_MAKE_GLYPH ('^', 0);
4310 XSETINT (it->ctl_chars[0], g);
4311
4312 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
4313 XSETINT (it->ctl_chars[1], g);
4314
4315 /* Set up IT->dpvec and return first character from it. */
4316 it->dpvec_char_len = it->len;
4317 it->dpvec = it->ctl_chars;
4318 it->dpend = it->dpvec + 2;
4319 it->current.dpvec_index = 0;
4320 it->method = next_element_from_display_vector;
4321 get_next_display_element (it);
4322 }
4323 else
4324 {
4325 unsigned char str[MAX_MULTIBYTE_LENGTH];
4326 int len;
4327 int i;
4328 GLYPH escape_glyph;
4329
4330 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
4331 if (it->dp
4332 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
4333 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
4334 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
4335 else
4336 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
4337
4338 if (SINGLE_BYTE_CHAR_P (it->c))
4339 str[0] = it->c, len = 1;
4340 else
4341 {
4342 len = CHAR_STRING_NO_SIGNAL (it->c, str);
4343 if (len < 0)
4344 {
4345 /* It's an invalid character, which
4346 shouldn't happen actually, but due to
4347 bugs it may happen. Let's print the char
4348 as is, there's not much meaningful we can
4349 do with it. */
4350 str[0] = it->c;
4351 str[1] = it->c >> 8;
4352 str[2] = it->c >> 16;
4353 str[3] = it->c >> 24;
4354 len = 4;
4355 }
4356 }
4357
4358 for (i = 0; i < len; i++)
4359 {
4360 XSETINT (it->ctl_chars[i * 4], escape_glyph);
4361 /* Insert three more glyphs into IT->ctl_chars for
4362 the octal display of the character. */
4363 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
4364 XSETINT (it->ctl_chars[i * 4 + 1], g);
4365 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
4366 XSETINT (it->ctl_chars[i * 4 + 2], g);
4367 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
4368 XSETINT (it->ctl_chars[i * 4 + 3], g);
4369 }
4370
4371 /* Set up IT->dpvec and return the first character
4372 from it. */
4373 it->dpvec_char_len = it->len;
4374 it->dpvec = it->ctl_chars;
4375 it->dpend = it->dpvec + len * 4;
4376 it->current.dpvec_index = 0;
4377 it->method = next_element_from_display_vector;
4378 get_next_display_element (it);
4379 }
4380 }
4381 }
4382
4383 /* Adjust face id for a multibyte character. There are no
4384 multibyte character in unibyte text. */
4385 if (it->multibyte_p
4386 && success_p
4387 && FRAME_WINDOW_P (it->f))
4388 {
4389 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4390 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
4391 }
4392 }
4393
4394 /* Is this character the last one of a run of characters with
4395 box? If yes, set IT->end_of_box_run_p to 1. */
4396 if (it->face_box_p
4397 && it->s == NULL)
4398 {
4399 int face_id;
4400 struct face *face;
4401
4402 it->end_of_box_run_p
4403 = ((face_id = face_after_it_pos (it),
4404 face_id != it->face_id)
4405 && (face = FACE_FROM_ID (it->f, face_id),
4406 face->box == FACE_NO_BOX));
4407 }
4408
4409 /* Value is 0 if end of buffer or string reached. */
4410 return success_p;
4411 }
4412
4413
4414 /* Move IT to the next display element.
4415
4416 RESEAT_P non-zero means if called on a newline in buffer text,
4417 skip to the next visible line start.
4418
4419 Functions get_next_display_element and set_iterator_to_next are
4420 separate because I find this arrangement easier to handle than a
4421 get_next_display_element function that also increments IT's
4422 position. The way it is we can first look at an iterator's current
4423 display element, decide whether it fits on a line, and if it does,
4424 increment the iterator position. The other way around we probably
4425 would either need a flag indicating whether the iterator has to be
4426 incremented the next time, or we would have to implement a
4427 decrement position function which would not be easy to write. */
4428
4429 void
4430 set_iterator_to_next (it, reseat_p)
4431 struct it *it;
4432 int reseat_p;
4433 {
4434 /* Reset flags indicating start and end of a sequence of characters
4435 with box. Reset them at the start of this function because
4436 moving the iterator to a new position might set them. */
4437 it->start_of_box_run_p = it->end_of_box_run_p = 0;
4438
4439 if (it->method == next_element_from_buffer)
4440 {
4441 /* The current display element of IT is a character from
4442 current_buffer. Advance in the buffer, and maybe skip over
4443 invisible lines that are so because of selective display. */
4444 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
4445 reseat_at_next_visible_line_start (it, 0);
4446 else
4447 {
4448 xassert (it->len != 0);
4449 IT_BYTEPOS (*it) += it->len;
4450 IT_CHARPOS (*it) += 1;
4451 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
4452 }
4453 }
4454 else if (it->method == next_element_from_composition)
4455 {
4456 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
4457 if (STRINGP (it->string))
4458 {
4459 IT_STRING_BYTEPOS (*it) += it->len;
4460 IT_STRING_CHARPOS (*it) += it->cmp_len;
4461 it->method = next_element_from_string;
4462 goto consider_string_end;
4463 }
4464 else
4465 {
4466 IT_BYTEPOS (*it) += it->len;
4467 IT_CHARPOS (*it) += it->cmp_len;
4468 it->method = next_element_from_buffer;
4469 }
4470 }
4471 else if (it->method == next_element_from_c_string)
4472 {
4473 /* Current display element of IT is from a C string. */
4474 IT_BYTEPOS (*it) += it->len;
4475 IT_CHARPOS (*it) += 1;
4476 }
4477 else if (it->method == next_element_from_display_vector)
4478 {
4479 /* Current display element of IT is from a display table entry.
4480 Advance in the display table definition. Reset it to null if
4481 end reached, and continue with characters from buffers/
4482 strings. */
4483 ++it->current.dpvec_index;
4484
4485 /* Restore face of the iterator to what they were before the
4486 display vector entry (these entries may contain faces). */
4487 it->face_id = it->saved_face_id;
4488
4489 if (it->dpvec + it->current.dpvec_index == it->dpend)
4490 {
4491 if (it->s)
4492 it->method = next_element_from_c_string;
4493 else if (STRINGP (it->string))
4494 it->method = next_element_from_string;
4495 else
4496 it->method = next_element_from_buffer;
4497
4498 it->dpvec = NULL;
4499 it->current.dpvec_index = -1;
4500
4501 /* Skip over characters which were displayed via IT->dpvec. */
4502 if (it->dpvec_char_len < 0)
4503 reseat_at_next_visible_line_start (it, 1);
4504 else if (it->dpvec_char_len > 0)
4505 {
4506 it->len = it->dpvec_char_len;
4507 set_iterator_to_next (it, reseat_p);
4508 }
4509 }
4510 }
4511 else if (it->method == next_element_from_string)
4512 {
4513 /* Current display element is a character from a Lisp string. */
4514 xassert (it->s == NULL && STRINGP (it->string));
4515 IT_STRING_BYTEPOS (*it) += it->len;
4516 IT_STRING_CHARPOS (*it) += 1;
4517
4518 consider_string_end:
4519
4520 if (it->current.overlay_string_index >= 0)
4521 {
4522 /* IT->string is an overlay string. Advance to the
4523 next, if there is one. */
4524 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
4525 next_overlay_string (it);
4526 }
4527 else
4528 {
4529 /* IT->string is not an overlay string. If we reached
4530 its end, and there is something on IT->stack, proceed
4531 with what is on the stack. This can be either another
4532 string, this time an overlay string, or a buffer. */
4533 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
4534 && it->sp > 0)
4535 {
4536 pop_it (it);
4537 if (!STRINGP (it->string))
4538 it->method = next_element_from_buffer;
4539 else
4540 goto consider_string_end;
4541 }
4542 }
4543 }
4544 else if (it->method == next_element_from_image
4545 || it->method == next_element_from_stretch)
4546 {
4547 /* The position etc with which we have to proceed are on
4548 the stack. The position may be at the end of a string,
4549 if the `display' property takes up the whole string. */
4550 pop_it (it);
4551 it->image_id = 0;
4552 if (STRINGP (it->string))
4553 {
4554 it->method = next_element_from_string;
4555 goto consider_string_end;
4556 }
4557 else
4558 it->method = next_element_from_buffer;
4559 }
4560 else
4561 /* There are no other methods defined, so this should be a bug. */
4562 abort ();
4563
4564 xassert (it->method != next_element_from_string
4565 || (STRINGP (it->string)
4566 && IT_STRING_CHARPOS (*it) >= 0));
4567 }
4568
4569
4570 /* Load IT's display element fields with information about the next
4571 display element which comes from a display table entry or from the
4572 result of translating a control character to one of the forms `^C'
4573 or `\003'. IT->dpvec holds the glyphs to return as characters. */
4574
4575 static int
4576 next_element_from_display_vector (it)
4577 struct it *it;
4578 {
4579 /* Precondition. */
4580 xassert (it->dpvec && it->current.dpvec_index >= 0);
4581
4582 /* Remember the current face id in case glyphs specify faces.
4583 IT's face is restored in set_iterator_to_next. */
4584 it->saved_face_id = it->face_id;
4585
4586 if (INTEGERP (*it->dpvec)
4587 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
4588 {
4589 int lface_id;
4590 GLYPH g;
4591
4592 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
4593 it->c = FAST_GLYPH_CHAR (g);
4594 it->len = CHAR_BYTES (it->c);
4595
4596 /* The entry may contain a face id to use. Such a face id is
4597 the id of a Lisp face, not a realized face. A face id of
4598 zero means no face is specified. */
4599 lface_id = FAST_GLYPH_FACE (g);
4600 if (lface_id)
4601 {
4602 /* The function returns -1 if lface_id is invalid. */
4603 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
4604 if (face_id >= 0)
4605 it->face_id = face_id;
4606 }
4607 }
4608 else
4609 /* Display table entry is invalid. Return a space. */
4610 it->c = ' ', it->len = 1;
4611
4612 /* Don't change position and object of the iterator here. They are
4613 still the values of the character that had this display table
4614 entry or was translated, and that's what we want. */
4615 it->what = IT_CHARACTER;
4616 return 1;
4617 }
4618
4619
4620 /* Load IT with the next display element from Lisp string IT->string.
4621 IT->current.string_pos is the current position within the string.
4622 If IT->current.overlay_string_index >= 0, the Lisp string is an
4623 overlay string. */
4624
4625 static int
4626 next_element_from_string (it)
4627 struct it *it;
4628 {
4629 struct text_pos position;
4630
4631 xassert (STRINGP (it->string));
4632 xassert (IT_STRING_CHARPOS (*it) >= 0);
4633 position = it->current.string_pos;
4634
4635 /* Time to check for invisible text? */
4636 if (IT_STRING_CHARPOS (*it) < it->end_charpos
4637 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
4638 {
4639 handle_stop (it);
4640
4641 /* Since a handler may have changed IT->method, we must
4642 recurse here. */
4643 return get_next_display_element (it);
4644 }
4645
4646 if (it->current.overlay_string_index >= 0)
4647 {
4648 /* Get the next character from an overlay string. In overlay
4649 strings, There is no field width or padding with spaces to
4650 do. */
4651 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
4652 {
4653 it->what = IT_EOB;
4654 return 0;
4655 }
4656 else if (STRING_MULTIBYTE (it->string))
4657 {
4658 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
4659 const unsigned char *s = (SDATA (it->string)
4660 + IT_STRING_BYTEPOS (*it));
4661 it->c = string_char_and_length (s, remaining, &it->len);
4662 }
4663 else
4664 {
4665 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
4666 it->len = 1;
4667 }
4668 }
4669 else
4670 {
4671 /* Get the next character from a Lisp string that is not an
4672 overlay string. Such strings come from the mode line, for
4673 example. We may have to pad with spaces, or truncate the
4674 string. See also next_element_from_c_string. */
4675 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
4676 {
4677 it->what = IT_EOB;
4678 return 0;
4679 }
4680 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
4681 {
4682 /* Pad with spaces. */
4683 it->c = ' ', it->len = 1;
4684 CHARPOS (position) = BYTEPOS (position) = -1;
4685 }
4686 else if (STRING_MULTIBYTE (it->string))
4687 {
4688 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
4689 const unsigned char *s = (SDATA (it->string)
4690 + IT_STRING_BYTEPOS (*it));
4691 it->c = string_char_and_length (s, maxlen, &it->len);
4692 }
4693 else
4694 {
4695 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
4696 it->len = 1;
4697 }
4698 }
4699
4700 /* Record what we have and where it came from. Note that we store a
4701 buffer position in IT->position although it could arguably be a
4702 string position. */
4703 it->what = IT_CHARACTER;
4704 it->object = it->string;
4705 it->position = position;
4706 return 1;
4707 }
4708
4709
4710 /* Load IT with next display element from C string IT->s.
4711 IT->string_nchars is the maximum number of characters to return
4712 from the string. IT->end_charpos may be greater than
4713 IT->string_nchars when this function is called, in which case we
4714 may have to return padding spaces. Value is zero if end of string
4715 reached, including padding spaces. */
4716
4717 static int
4718 next_element_from_c_string (it)
4719 struct it *it;
4720 {
4721 int success_p = 1;
4722
4723 xassert (it->s);
4724 it->what = IT_CHARACTER;
4725 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
4726 it->object = Qnil;
4727
4728 /* IT's position can be greater IT->string_nchars in case a field
4729 width or precision has been specified when the iterator was
4730 initialized. */
4731 if (IT_CHARPOS (*it) >= it->end_charpos)
4732 {
4733 /* End of the game. */
4734 it->what = IT_EOB;
4735 success_p = 0;
4736 }
4737 else if (IT_CHARPOS (*it) >= it->string_nchars)
4738 {
4739 /* Pad with spaces. */
4740 it->c = ' ', it->len = 1;
4741 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
4742 }
4743 else if (it->multibyte_p)
4744 {
4745 /* Implementation note: The calls to strlen apparently aren't a
4746 performance problem because there is no noticeable performance
4747 difference between Emacs running in unibyte or multibyte mode. */
4748 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
4749 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
4750 maxlen, &it->len);
4751 }
4752 else
4753 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
4754
4755 return success_p;
4756 }
4757
4758
4759 /* Set up IT to return characters from an ellipsis, if appropriate.
4760 The definition of the ellipsis glyphs may come from a display table
4761 entry. This function Fills IT with the first glyph from the
4762 ellipsis if an ellipsis is to be displayed. */
4763
4764 static int
4765 next_element_from_ellipsis (it)
4766 struct it *it;
4767 {
4768 if (it->selective_display_ellipsis_p)
4769 {
4770 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4771 {
4772 /* Use the display table definition for `...'. Invalid glyphs
4773 will be handled by the method returning elements from dpvec. */
4774 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4775 it->dpvec_char_len = it->len;
4776 it->dpvec = v->contents;
4777 it->dpend = v->contents + v->size;
4778 it->current.dpvec_index = 0;
4779 it->method = next_element_from_display_vector;
4780 }
4781 else
4782 {
4783 /* Use default `...' which is stored in default_invis_vector. */
4784 it->dpvec_char_len = it->len;
4785 it->dpvec = default_invis_vector;
4786 it->dpend = default_invis_vector + 3;
4787 it->current.dpvec_index = 0;
4788 it->method = next_element_from_display_vector;
4789 }
4790 }
4791 else
4792 {
4793 /* The face at the current position may be different from the
4794 face we find after the invisible text. Remember what it
4795 was in IT->saved_face_id, and signal that it's there by
4796 setting face_before_selective_p. */
4797 it->saved_face_id = it->face_id;
4798 it->method = next_element_from_buffer;
4799 reseat_at_next_visible_line_start (it, 1);
4800 it->face_before_selective_p = 1;
4801 }
4802
4803 return get_next_display_element (it);
4804 }
4805
4806
4807 /* Deliver an image display element. The iterator IT is already
4808 filled with image information (done in handle_display_prop). Value
4809 is always 1. */
4810
4811
4812 static int
4813 next_element_from_image (it)
4814 struct it *it;
4815 {
4816 it->what = IT_IMAGE;
4817 return 1;
4818 }
4819
4820
4821 /* Fill iterator IT with next display element from a stretch glyph
4822 property. IT->object is the value of the text property. Value is
4823 always 1. */
4824
4825 static int
4826 next_element_from_stretch (it)
4827 struct it *it;
4828 {
4829 it->what = IT_STRETCH;
4830 return 1;
4831 }
4832
4833
4834 /* Load IT with the next display element from current_buffer. Value
4835 is zero if end of buffer reached. IT->stop_charpos is the next
4836 position at which to stop and check for text properties or buffer
4837 end. */
4838
4839 static int
4840 next_element_from_buffer (it)
4841 struct it *it;
4842 {
4843 int success_p = 1;
4844
4845 /* Check this assumption, otherwise, we would never enter the
4846 if-statement, below. */
4847 xassert (IT_CHARPOS (*it) >= BEGV
4848 && IT_CHARPOS (*it) <= it->stop_charpos);
4849
4850 if (IT_CHARPOS (*it) >= it->stop_charpos)
4851 {
4852 if (IT_CHARPOS (*it) >= it->end_charpos)
4853 {
4854 int overlay_strings_follow_p;
4855
4856 /* End of the game, except when overlay strings follow that
4857 haven't been returned yet. */
4858 if (it->overlay_strings_at_end_processed_p)
4859 overlay_strings_follow_p = 0;
4860 else
4861 {
4862 it->overlay_strings_at_end_processed_p = 1;
4863 overlay_strings_follow_p = get_overlay_strings (it, 0);
4864 }
4865
4866 if (overlay_strings_follow_p)
4867 success_p = get_next_display_element (it);
4868 else
4869 {
4870 it->what = IT_EOB;
4871 it->position = it->current.pos;
4872 success_p = 0;
4873 }
4874 }
4875 else
4876 {
4877 handle_stop (it);
4878 return get_next_display_element (it);
4879 }
4880 }
4881 else
4882 {
4883 /* No face changes, overlays etc. in sight, so just return a
4884 character from current_buffer. */
4885 unsigned char *p;
4886
4887 /* Maybe run the redisplay end trigger hook. Performance note:
4888 This doesn't seem to cost measurable time. */
4889 if (it->redisplay_end_trigger_charpos
4890 && it->glyph_row
4891 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
4892 run_redisplay_end_trigger_hook (it);
4893
4894 /* Get the next character, maybe multibyte. */
4895 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
4896 if (it->multibyte_p && !ASCII_BYTE_P (*p))
4897 {
4898 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
4899 - IT_BYTEPOS (*it));
4900 it->c = string_char_and_length (p, maxlen, &it->len);
4901 }
4902 else
4903 it->c = *p, it->len = 1;
4904
4905 /* Record what we have and where it came from. */
4906 it->what = IT_CHARACTER;;
4907 it->object = it->w->buffer;
4908 it->position = it->current.pos;
4909
4910 /* Normally we return the character found above, except when we
4911 really want to return an ellipsis for selective display. */
4912 if (it->selective)
4913 {
4914 if (it->c == '\n')
4915 {
4916 /* A value of selective > 0 means hide lines indented more
4917 than that number of columns. */
4918 if (it->selective > 0
4919 && IT_CHARPOS (*it) + 1 < ZV
4920 && indented_beyond_p (IT_CHARPOS (*it) + 1,
4921 IT_BYTEPOS (*it) + 1,
4922 (double) it->selective)) /* iftc */
4923 {
4924 success_p = next_element_from_ellipsis (it);
4925 it->dpvec_char_len = -1;
4926 }
4927 }
4928 else if (it->c == '\r' && it->selective == -1)
4929 {
4930 /* A value of selective == -1 means that everything from the
4931 CR to the end of the line is invisible, with maybe an
4932 ellipsis displayed for it. */
4933 success_p = next_element_from_ellipsis (it);
4934 it->dpvec_char_len = -1;
4935 }
4936 }
4937 }
4938
4939 /* Value is zero if end of buffer reached. */
4940 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
4941 return success_p;
4942 }
4943
4944
4945 /* Run the redisplay end trigger hook for IT. */
4946
4947 static void
4948 run_redisplay_end_trigger_hook (it)
4949 struct it *it;
4950 {
4951 Lisp_Object args[3];
4952
4953 /* IT->glyph_row should be non-null, i.e. we should be actually
4954 displaying something, or otherwise we should not run the hook. */
4955 xassert (it->glyph_row);
4956
4957 /* Set up hook arguments. */
4958 args[0] = Qredisplay_end_trigger_functions;
4959 args[1] = it->window;
4960 XSETINT (args[2], it->redisplay_end_trigger_charpos);
4961 it->redisplay_end_trigger_charpos = 0;
4962
4963 /* Since we are *trying* to run these functions, don't try to run
4964 them again, even if they get an error. */
4965 it->w->redisplay_end_trigger = Qnil;
4966 Frun_hook_with_args (3, args);
4967
4968 /* Notice if it changed the face of the character we are on. */
4969 handle_face_prop (it);
4970 }
4971
4972
4973 /* Deliver a composition display element. The iterator IT is already
4974 filled with composition information (done in
4975 handle_composition_prop). Value is always 1. */
4976
4977 static int
4978 next_element_from_composition (it)
4979 struct it *it;
4980 {
4981 it->what = IT_COMPOSITION;
4982 it->position = (STRINGP (it->string)
4983 ? it->current.string_pos
4984 : it->current.pos);
4985 return 1;
4986 }
4987
4988
4989 \f
4990 /***********************************************************************
4991 Moving an iterator without producing glyphs
4992 ***********************************************************************/
4993
4994 /* Move iterator IT to a specified buffer or X position within one
4995 line on the display without producing glyphs.
4996
4997 OP should be a bit mask including some or all of these bits:
4998 MOVE_TO_X: Stop on reaching x-position TO_X.
4999 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
5000 Regardless of OP's value, stop in reaching the end of the display line.
5001
5002 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
5003 This means, in particular, that TO_X includes window's horizontal
5004 scroll amount.
5005
5006 The return value has several possible values that
5007 say what condition caused the scan to stop:
5008
5009 MOVE_POS_MATCH_OR_ZV
5010 - when TO_POS or ZV was reached.
5011
5012 MOVE_X_REACHED
5013 -when TO_X was reached before TO_POS or ZV were reached.
5014
5015 MOVE_LINE_CONTINUED
5016 - when we reached the end of the display area and the line must
5017 be continued.
5018
5019 MOVE_LINE_TRUNCATED
5020 - when we reached the end of the display area and the line is
5021 truncated.
5022
5023 MOVE_NEWLINE_OR_CR
5024 - when we stopped at a line end, i.e. a newline or a CR and selective
5025 display is on. */
5026
5027 static enum move_it_result
5028 move_it_in_display_line_to (it, to_charpos, to_x, op)
5029 struct it *it;
5030 int to_charpos, to_x, op;
5031 {
5032 enum move_it_result result = MOVE_UNDEFINED;
5033 struct glyph_row *saved_glyph_row;
5034
5035 /* Don't produce glyphs in produce_glyphs. */
5036 saved_glyph_row = it->glyph_row;
5037 it->glyph_row = NULL;
5038
5039 while (1)
5040 {
5041 int x, i, ascent = 0, descent = 0;
5042
5043 /* Stop when ZV or TO_CHARPOS reached. */
5044 if (!get_next_display_element (it)
5045 || ((op & MOVE_TO_POS) != 0
5046 && BUFFERP (it->object)
5047 && IT_CHARPOS (*it) >= to_charpos))
5048 {
5049 result = MOVE_POS_MATCH_OR_ZV;
5050 break;
5051 }
5052
5053 /* The call to produce_glyphs will get the metrics of the
5054 display element IT is loaded with. We record in x the
5055 x-position before this display element in case it does not
5056 fit on the line. */
5057 x = it->current_x;
5058
5059 /* Remember the line height so far in case the next element doesn't
5060 fit on the line. */
5061 if (!it->truncate_lines_p)
5062 {
5063 ascent = it->max_ascent;
5064 descent = it->max_descent;
5065 }
5066
5067 PRODUCE_GLYPHS (it);
5068
5069 if (it->area != TEXT_AREA)
5070 {
5071 set_iterator_to_next (it, 1);
5072 continue;
5073 }
5074
5075 /* The number of glyphs we get back in IT->nglyphs will normally
5076 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
5077 character on a terminal frame, or (iii) a line end. For the
5078 second case, IT->nglyphs - 1 padding glyphs will be present
5079 (on X frames, there is only one glyph produced for a
5080 composite character.
5081
5082 The behavior implemented below means, for continuation lines,
5083 that as many spaces of a TAB as fit on the current line are
5084 displayed there. For terminal frames, as many glyphs of a
5085 multi-glyph character are displayed in the current line, too.
5086 This is what the old redisplay code did, and we keep it that
5087 way. Under X, the whole shape of a complex character must
5088 fit on the line or it will be completely displayed in the
5089 next line.
5090
5091 Note that both for tabs and padding glyphs, all glyphs have
5092 the same width. */
5093 if (it->nglyphs)
5094 {
5095 /* More than one glyph or glyph doesn't fit on line. All
5096 glyphs have the same width. */
5097 int single_glyph_width = it->pixel_width / it->nglyphs;
5098 int new_x;
5099
5100 for (i = 0; i < it->nglyphs; ++i, x = new_x)
5101 {
5102 new_x = x + single_glyph_width;
5103
5104 /* We want to leave anything reaching TO_X to the caller. */
5105 if ((op & MOVE_TO_X) && new_x > to_x)
5106 {
5107 it->current_x = x;
5108 result = MOVE_X_REACHED;
5109 break;
5110 }
5111 else if (/* Lines are continued. */
5112 !it->truncate_lines_p
5113 && (/* And glyph doesn't fit on the line. */
5114 new_x > it->last_visible_x
5115 /* Or it fits exactly and we're on a window
5116 system frame. */
5117 || (new_x == it->last_visible_x
5118 && FRAME_WINDOW_P (it->f))))
5119 {
5120 if (/* IT->hpos == 0 means the very first glyph
5121 doesn't fit on the line, e.g. a wide image. */
5122 it->hpos == 0
5123 || (new_x == it->last_visible_x
5124 && FRAME_WINDOW_P (it->f)))
5125 {
5126 ++it->hpos;
5127 it->current_x = new_x;
5128 if (i == it->nglyphs - 1)
5129 set_iterator_to_next (it, 1);
5130 }
5131 else
5132 {
5133 it->current_x = x;
5134 it->max_ascent = ascent;
5135 it->max_descent = descent;
5136 }
5137
5138 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
5139 IT_CHARPOS (*it)));
5140 result = MOVE_LINE_CONTINUED;
5141 break;
5142 }
5143 else if (new_x > it->first_visible_x)
5144 {
5145 /* Glyph is visible. Increment number of glyphs that
5146 would be displayed. */
5147 ++it->hpos;
5148 }
5149 else
5150 {
5151 /* Glyph is completely off the left margin of the display
5152 area. Nothing to do. */
5153 }
5154 }
5155
5156 if (result != MOVE_UNDEFINED)
5157 break;
5158 }
5159 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
5160 {
5161 /* Stop when TO_X specified and reached. This check is
5162 necessary here because of lines consisting of a line end,
5163 only. The line end will not produce any glyphs and we
5164 would never get MOVE_X_REACHED. */
5165 xassert (it->nglyphs == 0);
5166 result = MOVE_X_REACHED;
5167 break;
5168 }
5169
5170 /* Is this a line end? If yes, we're done. */
5171 if (ITERATOR_AT_END_OF_LINE_P (it))
5172 {
5173 result = MOVE_NEWLINE_OR_CR;
5174 break;
5175 }
5176
5177 /* The current display element has been consumed. Advance
5178 to the next. */
5179 set_iterator_to_next (it, 1);
5180
5181 /* Stop if lines are truncated and IT's current x-position is
5182 past the right edge of the window now. */
5183 if (it->truncate_lines_p
5184 && it->current_x >= it->last_visible_x)
5185 {
5186 result = MOVE_LINE_TRUNCATED;
5187 break;
5188 }
5189 }
5190
5191 /* Restore the iterator settings altered at the beginning of this
5192 function. */
5193 it->glyph_row = saved_glyph_row;
5194 return result;
5195 }
5196
5197
5198 /* Move IT forward until it satisfies one or more of the criteria in
5199 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
5200
5201 OP is a bit-mask that specifies where to stop, and in particular,
5202 which of those four position arguments makes a difference. See the
5203 description of enum move_operation_enum.
5204
5205 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
5206 screen line, this function will set IT to the next position >
5207 TO_CHARPOS. */
5208
5209 void
5210 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
5211 struct it *it;
5212 int to_charpos, to_x, to_y, to_vpos;
5213 int op;
5214 {
5215 enum move_it_result skip, skip2 = MOVE_X_REACHED;
5216 int line_height;
5217 int reached = 0;
5218
5219 for (;;)
5220 {
5221 if (op & MOVE_TO_VPOS)
5222 {
5223 /* If no TO_CHARPOS and no TO_X specified, stop at the
5224 start of the line TO_VPOS. */
5225 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
5226 {
5227 if (it->vpos == to_vpos)
5228 {
5229 reached = 1;
5230 break;
5231 }
5232 else
5233 skip = move_it_in_display_line_to (it, -1, -1, 0);
5234 }
5235 else
5236 {
5237 /* TO_VPOS >= 0 means stop at TO_X in the line at
5238 TO_VPOS, or at TO_POS, whichever comes first. */
5239 if (it->vpos == to_vpos)
5240 {
5241 reached = 2;
5242 break;
5243 }
5244
5245 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
5246
5247 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
5248 {
5249 reached = 3;
5250 break;
5251 }
5252 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
5253 {
5254 /* We have reached TO_X but not in the line we want. */
5255 skip = move_it_in_display_line_to (it, to_charpos,
5256 -1, MOVE_TO_POS);
5257 if (skip == MOVE_POS_MATCH_OR_ZV)
5258 {
5259 reached = 4;
5260 break;
5261 }
5262 }
5263 }
5264 }
5265 else if (op & MOVE_TO_Y)
5266 {
5267 struct it it_backup;
5268
5269 /* TO_Y specified means stop at TO_X in the line containing
5270 TO_Y---or at TO_CHARPOS if this is reached first. The
5271 problem is that we can't really tell whether the line
5272 contains TO_Y before we have completely scanned it, and
5273 this may skip past TO_X. What we do is to first scan to
5274 TO_X.
5275
5276 If TO_X is not specified, use a TO_X of zero. The reason
5277 is to make the outcome of this function more predictable.
5278 If we didn't use TO_X == 0, we would stop at the end of
5279 the line which is probably not what a caller would expect
5280 to happen. */
5281 skip = move_it_in_display_line_to (it, to_charpos,
5282 ((op & MOVE_TO_X)
5283 ? to_x : 0),
5284 (MOVE_TO_X
5285 | (op & MOVE_TO_POS)));
5286
5287 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
5288 if (skip == MOVE_POS_MATCH_OR_ZV)
5289 {
5290 reached = 5;
5291 break;
5292 }
5293
5294 /* If TO_X was reached, we would like to know whether TO_Y
5295 is in the line. This can only be said if we know the
5296 total line height which requires us to scan the rest of
5297 the line. */
5298 if (skip == MOVE_X_REACHED)
5299 {
5300 it_backup = *it;
5301 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
5302 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
5303 op & MOVE_TO_POS);
5304 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
5305 }
5306
5307 /* Now, decide whether TO_Y is in this line. */
5308 line_height = it->max_ascent + it->max_descent;
5309 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
5310
5311 if (to_y >= it->current_y
5312 && to_y < it->current_y + line_height)
5313 {
5314 if (skip == MOVE_X_REACHED)
5315 /* If TO_Y is in this line and TO_X was reached above,
5316 we scanned too far. We have to restore IT's settings
5317 to the ones before skipping. */
5318 *it = it_backup;
5319 reached = 6;
5320 }
5321 else if (skip == MOVE_X_REACHED)
5322 {
5323 skip = skip2;
5324 if (skip == MOVE_POS_MATCH_OR_ZV)
5325 reached = 7;
5326 }
5327
5328 if (reached)
5329 break;
5330 }
5331 else
5332 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
5333
5334 switch (skip)
5335 {
5336 case MOVE_POS_MATCH_OR_ZV:
5337 reached = 8;
5338 goto out;
5339
5340 case MOVE_NEWLINE_OR_CR:
5341 set_iterator_to_next (it, 1);
5342 it->continuation_lines_width = 0;
5343 break;
5344
5345 case MOVE_LINE_TRUNCATED:
5346 it->continuation_lines_width = 0;
5347 reseat_at_next_visible_line_start (it, 0);
5348 if ((op & MOVE_TO_POS) != 0
5349 && IT_CHARPOS (*it) > to_charpos)
5350 {
5351 reached = 9;
5352 goto out;
5353 }
5354 break;
5355
5356 case MOVE_LINE_CONTINUED:
5357 it->continuation_lines_width += it->current_x;
5358 break;
5359
5360 default:
5361 abort ();
5362 }
5363
5364 /* Reset/increment for the next run. */
5365 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
5366 it->current_x = it->hpos = 0;
5367 it->current_y += it->max_ascent + it->max_descent;
5368 ++it->vpos;
5369 last_height = it->max_ascent + it->max_descent;
5370 last_max_ascent = it->max_ascent;
5371 it->max_ascent = it->max_descent = 0;
5372 }
5373
5374 out:
5375
5376 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
5377 }
5378
5379
5380 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
5381
5382 If DY > 0, move IT backward at least that many pixels. DY = 0
5383 means move IT backward to the preceding line start or BEGV. This
5384 function may move over more than DY pixels if IT->current_y - DY
5385 ends up in the middle of a line; in this case IT->current_y will be
5386 set to the top of the line moved to. */
5387
5388 void
5389 move_it_vertically_backward (it, dy)
5390 struct it *it;
5391 int dy;
5392 {
5393 int nlines, h;
5394 struct it it2, it3;
5395 int start_pos = IT_CHARPOS (*it);
5396
5397 xassert (dy >= 0);
5398
5399 /* Estimate how many newlines we must move back. */
5400 nlines = max (1, dy / CANON_Y_UNIT (it->f));
5401
5402 /* Set the iterator's position that many lines back. */
5403 while (nlines-- && IT_CHARPOS (*it) > BEGV)
5404 back_to_previous_visible_line_start (it);
5405
5406 /* Reseat the iterator here. When moving backward, we don't want
5407 reseat to skip forward over invisible text, set up the iterator
5408 to deliver from overlay strings at the new position etc. So,
5409 use reseat_1 here. */
5410 reseat_1 (it, it->current.pos, 1);
5411
5412 /* We are now surely at a line start. */
5413 it->current_x = it->hpos = 0;
5414
5415 /* Move forward and see what y-distance we moved. First move to the
5416 start of the next line so that we get its height. We need this
5417 height to be able to tell whether we reached the specified
5418 y-distance. */
5419 it2 = *it;
5420 it2.max_ascent = it2.max_descent = 0;
5421 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
5422 MOVE_TO_POS | MOVE_TO_VPOS);
5423 xassert (IT_CHARPOS (*it) >= BEGV);
5424 it3 = it2;
5425
5426 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
5427 xassert (IT_CHARPOS (*it) >= BEGV);
5428 h = it2.current_y - it->current_y;
5429 nlines = it2.vpos - it->vpos;
5430
5431 /* Correct IT's y and vpos position. */
5432 it->vpos -= nlines;
5433 it->current_y -= h;
5434
5435 if (dy == 0)
5436 {
5437 /* DY == 0 means move to the start of the screen line. The
5438 value of nlines is > 0 if continuation lines were involved. */
5439 if (nlines > 0)
5440 move_it_by_lines (it, nlines, 1);
5441 xassert (IT_CHARPOS (*it) <= start_pos);
5442 }
5443 else if (nlines)
5444 {
5445 /* The y-position we try to reach. Note that h has been
5446 subtracted in front of the if-statement. */
5447 int target_y = it->current_y + h - dy;
5448 int y0 = it3.current_y;
5449 int y1 = line_bottom_y (&it3);
5450 int line_height = y1 - y0;
5451
5452 /* If we did not reach target_y, try to move further backward if
5453 we can. If we moved too far backward, try to move forward. */
5454 if (target_y < it->current_y
5455 /* This is heuristic. In a window that's 3 lines high, with
5456 a line height of 13 pixels each, recentering with point
5457 on the bottom line will try to move -39/2 = 19 pixels
5458 backward. Try to avoid moving into the first line. */
5459 && it->current_y - target_y > line_height / 3 * 2
5460 && IT_CHARPOS (*it) > BEGV)
5461 {
5462 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
5463 target_y - it->current_y));
5464 move_it_vertically (it, target_y - it->current_y);
5465 xassert (IT_CHARPOS (*it) >= BEGV);
5466 }
5467 else if (target_y >= it->current_y + line_height
5468 && IT_CHARPOS (*it) < ZV)
5469 {
5470 /* Should move forward by at least one line, maybe more.
5471
5472 Note: Calling move_it_by_lines can be expensive on
5473 terminal frames, where compute_motion is used (via
5474 vmotion) to do the job, when there are very long lines
5475 and truncate-lines is nil. That's the reason for
5476 treating terminal frames specially here. */
5477
5478 if (!FRAME_WINDOW_P (it->f))
5479 move_it_vertically (it, target_y - (it->current_y + line_height));
5480 else
5481 {
5482 do
5483 {
5484 move_it_by_lines (it, 1, 1);
5485 }
5486 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
5487 }
5488
5489 xassert (IT_CHARPOS (*it) >= BEGV);
5490 }
5491 }
5492 }
5493
5494
5495 /* Move IT by a specified amount of pixel lines DY. DY negative means
5496 move backwards. DY = 0 means move to start of screen line. At the
5497 end, IT will be on the start of a screen line. */
5498
5499 void
5500 move_it_vertically (it, dy)
5501 struct it *it;
5502 int dy;
5503 {
5504 if (dy <= 0)
5505 move_it_vertically_backward (it, -dy);
5506 else if (dy > 0)
5507 {
5508 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
5509 move_it_to (it, ZV, -1, it->current_y + dy, -1,
5510 MOVE_TO_POS | MOVE_TO_Y);
5511 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
5512
5513 /* If buffer ends in ZV without a newline, move to the start of
5514 the line to satisfy the post-condition. */
5515 if (IT_CHARPOS (*it) == ZV
5516 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
5517 move_it_by_lines (it, 0, 0);
5518 }
5519 }
5520
5521
5522 /* Move iterator IT past the end of the text line it is in. */
5523
5524 void
5525 move_it_past_eol (it)
5526 struct it *it;
5527 {
5528 enum move_it_result rc;
5529
5530 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
5531 if (rc == MOVE_NEWLINE_OR_CR)
5532 set_iterator_to_next (it, 0);
5533 }
5534
5535
5536 #if 0 /* Currently not used. */
5537
5538 /* Return non-zero if some text between buffer positions START_CHARPOS
5539 and END_CHARPOS is invisible. IT->window is the window for text
5540 property lookup. */
5541
5542 static int
5543 invisible_text_between_p (it, start_charpos, end_charpos)
5544 struct it *it;
5545 int start_charpos, end_charpos;
5546 {
5547 Lisp_Object prop, limit;
5548 int invisible_found_p;
5549
5550 xassert (it != NULL && start_charpos <= end_charpos);
5551
5552 /* Is text at START invisible? */
5553 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
5554 it->window);
5555 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5556 invisible_found_p = 1;
5557 else
5558 {
5559 limit = Fnext_single_char_property_change (make_number (start_charpos),
5560 Qinvisible, Qnil,
5561 make_number (end_charpos));
5562 invisible_found_p = XFASTINT (limit) < end_charpos;
5563 }
5564
5565 return invisible_found_p;
5566 }
5567
5568 #endif /* 0 */
5569
5570
5571 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
5572 negative means move up. DVPOS == 0 means move to the start of the
5573 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
5574 NEED_Y_P is zero, IT->current_y will be left unchanged.
5575
5576 Further optimization ideas: If we would know that IT->f doesn't use
5577 a face with proportional font, we could be faster for
5578 truncate-lines nil. */
5579
5580 void
5581 move_it_by_lines (it, dvpos, need_y_p)
5582 struct it *it;
5583 int dvpos, need_y_p;
5584 {
5585 struct position pos;
5586
5587 if (!FRAME_WINDOW_P (it->f))
5588 {
5589 struct text_pos textpos;
5590
5591 /* We can use vmotion on frames without proportional fonts. */
5592 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
5593 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
5594 reseat (it, textpos, 1);
5595 it->vpos += pos.vpos;
5596 it->current_y += pos.vpos;
5597 }
5598 else if (dvpos == 0)
5599 {
5600 /* DVPOS == 0 means move to the start of the screen line. */
5601 move_it_vertically_backward (it, 0);
5602 xassert (it->current_x == 0 && it->hpos == 0);
5603 }
5604 else if (dvpos > 0)
5605 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
5606 else
5607 {
5608 struct it it2;
5609 int start_charpos, i;
5610
5611 /* Start at the beginning of the screen line containing IT's
5612 position. */
5613 move_it_vertically_backward (it, 0);
5614
5615 /* Go back -DVPOS visible lines and reseat the iterator there. */
5616 start_charpos = IT_CHARPOS (*it);
5617 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
5618 back_to_previous_visible_line_start (it);
5619 reseat (it, it->current.pos, 1);
5620 it->current_x = it->hpos = 0;
5621
5622 /* Above call may have moved too far if continuation lines
5623 are involved. Scan forward and see if it did. */
5624 it2 = *it;
5625 it2.vpos = it2.current_y = 0;
5626 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
5627 it->vpos -= it2.vpos;
5628 it->current_y -= it2.current_y;
5629 it->current_x = it->hpos = 0;
5630
5631 /* If we moved too far, move IT some lines forward. */
5632 if (it2.vpos > -dvpos)
5633 {
5634 int delta = it2.vpos + dvpos;
5635 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
5636 }
5637 }
5638 }
5639
5640
5641 \f
5642 /***********************************************************************
5643 Messages
5644 ***********************************************************************/
5645
5646
5647 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
5648 to *Messages*. */
5649
5650 void
5651 add_to_log (format, arg1, arg2)
5652 char *format;
5653 Lisp_Object arg1, arg2;
5654 {
5655 Lisp_Object args[3];
5656 Lisp_Object msg, fmt;
5657 char *buffer;
5658 int len;
5659 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5660
5661 /* Do nothing if called asynchronously. Inserting text into
5662 a buffer may call after-change-functions and alike and
5663 that would means running Lisp asynchronously. */
5664 if (handling_signal)
5665 return;
5666
5667 fmt = msg = Qnil;
5668 GCPRO4 (fmt, msg, arg1, arg2);
5669
5670 args[0] = fmt = build_string (format);
5671 args[1] = arg1;
5672 args[2] = arg2;
5673 msg = Fformat (3, args);
5674
5675 len = SBYTES (msg) + 1;
5676 buffer = (char *) alloca (len);
5677 bcopy (SDATA (msg), buffer, len);
5678
5679 message_dolog (buffer, len - 1, 1, 0);
5680 UNGCPRO;
5681 }
5682
5683
5684 /* Output a newline in the *Messages* buffer if "needs" one. */
5685
5686 void
5687 message_log_maybe_newline ()
5688 {
5689 if (message_log_need_newline)
5690 message_dolog ("", 0, 1, 0);
5691 }
5692
5693
5694 /* Add a string M of length NBYTES to the message log, optionally
5695 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
5696 nonzero, means interpret the contents of M as multibyte. This
5697 function calls low-level routines in order to bypass text property
5698 hooks, etc. which might not be safe to run. */
5699
5700 void
5701 message_dolog (m, nbytes, nlflag, multibyte)
5702 const char *m;
5703 int nbytes, nlflag, multibyte;
5704 {
5705 if (!NILP (Vmemory_full))
5706 return;
5707
5708 if (!NILP (Vmessage_log_max))
5709 {
5710 struct buffer *oldbuf;
5711 Lisp_Object oldpoint, oldbegv, oldzv;
5712 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5713 int point_at_end = 0;
5714 int zv_at_end = 0;
5715 Lisp_Object old_deactivate_mark, tem;
5716 struct gcpro gcpro1;
5717
5718 old_deactivate_mark = Vdeactivate_mark;
5719 oldbuf = current_buffer;
5720 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
5721 current_buffer->undo_list = Qt;
5722
5723 oldpoint = message_dolog_marker1;
5724 set_marker_restricted (oldpoint, make_number (PT), Qnil);
5725 oldbegv = message_dolog_marker2;
5726 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
5727 oldzv = message_dolog_marker3;
5728 set_marker_restricted (oldzv, make_number (ZV), Qnil);
5729 GCPRO1 (old_deactivate_mark);
5730
5731 if (PT == Z)
5732 point_at_end = 1;
5733 if (ZV == Z)
5734 zv_at_end = 1;
5735
5736 BEGV = BEG;
5737 BEGV_BYTE = BEG_BYTE;
5738 ZV = Z;
5739 ZV_BYTE = Z_BYTE;
5740 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5741
5742 /* Insert the string--maybe converting multibyte to single byte
5743 or vice versa, so that all the text fits the buffer. */
5744 if (multibyte
5745 && NILP (current_buffer->enable_multibyte_characters))
5746 {
5747 int i, c, char_bytes;
5748 unsigned char work[1];
5749
5750 /* Convert a multibyte string to single-byte
5751 for the *Message* buffer. */
5752 for (i = 0; i < nbytes; i += nbytes)
5753 {
5754 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
5755 work[0] = (SINGLE_BYTE_CHAR_P (c)
5756 ? c
5757 : multibyte_char_to_unibyte (c, Qnil));
5758 insert_1_both (work, 1, 1, 1, 0, 0);
5759 }
5760 }
5761 else if (! multibyte
5762 && ! NILP (current_buffer->enable_multibyte_characters))
5763 {
5764 int i, c, char_bytes;
5765 unsigned char *msg = (unsigned char *) m;
5766 unsigned char str[MAX_MULTIBYTE_LENGTH];
5767 /* Convert a single-byte string to multibyte
5768 for the *Message* buffer. */
5769 for (i = 0; i < nbytes; i++)
5770 {
5771 c = unibyte_char_to_multibyte (msg[i]);
5772 char_bytes = CHAR_STRING (c, str);
5773 insert_1_both (str, 1, char_bytes, 1, 0, 0);
5774 }
5775 }
5776 else if (nbytes)
5777 insert_1 (m, nbytes, 1, 0, 0);
5778
5779 if (nlflag)
5780 {
5781 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
5782 insert_1 ("\n", 1, 1, 0, 0);
5783
5784 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
5785 this_bol = PT;
5786 this_bol_byte = PT_BYTE;
5787
5788 /* See if this line duplicates the previous one.
5789 If so, combine duplicates. */
5790 if (this_bol > BEG)
5791 {
5792 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
5793 prev_bol = PT;
5794 prev_bol_byte = PT_BYTE;
5795
5796 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
5797 this_bol, this_bol_byte);
5798 if (dup)
5799 {
5800 del_range_both (prev_bol, prev_bol_byte,
5801 this_bol, this_bol_byte, 0);
5802 if (dup > 1)
5803 {
5804 char dupstr[40];
5805 int duplen;
5806
5807 /* If you change this format, don't forget to also
5808 change message_log_check_duplicate. */
5809 sprintf (dupstr, " [%d times]", dup);
5810 duplen = strlen (dupstr);
5811 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
5812 insert_1 (dupstr, duplen, 1, 0, 1);
5813 }
5814 }
5815 }
5816
5817 /* If we have more than the desired maximum number of lines
5818 in the *Messages* buffer now, delete the oldest ones.
5819 This is safe because we don't have undo in this buffer. */
5820
5821 if (NATNUMP (Vmessage_log_max))
5822 {
5823 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
5824 -XFASTINT (Vmessage_log_max) - 1, 0);
5825 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
5826 }
5827 }
5828 BEGV = XMARKER (oldbegv)->charpos;
5829 BEGV_BYTE = marker_byte_position (oldbegv);
5830
5831 if (zv_at_end)
5832 {
5833 ZV = Z;
5834 ZV_BYTE = Z_BYTE;
5835 }
5836 else
5837 {
5838 ZV = XMARKER (oldzv)->charpos;
5839 ZV_BYTE = marker_byte_position (oldzv);
5840 }
5841
5842 if (point_at_end)
5843 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5844 else
5845 /* We can't do Fgoto_char (oldpoint) because it will run some
5846 Lisp code. */
5847 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
5848 XMARKER (oldpoint)->bytepos);
5849
5850 UNGCPRO;
5851 unchain_marker (oldpoint);
5852 unchain_marker (oldbegv);
5853 unchain_marker (oldzv);
5854
5855 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
5856 set_buffer_internal (oldbuf);
5857 if (NILP (tem))
5858 windows_or_buffers_changed = old_windows_or_buffers_changed;
5859 message_log_need_newline = !nlflag;
5860 Vdeactivate_mark = old_deactivate_mark;
5861 }
5862 }
5863
5864
5865 /* We are at the end of the buffer after just having inserted a newline.
5866 (Note: We depend on the fact we won't be crossing the gap.)
5867 Check to see if the most recent message looks a lot like the previous one.
5868 Return 0 if different, 1 if the new one should just replace it, or a
5869 value N > 1 if we should also append " [N times]". */
5870
5871 static int
5872 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
5873 int prev_bol, this_bol;
5874 int prev_bol_byte, this_bol_byte;
5875 {
5876 int i;
5877 int len = Z_BYTE - 1 - this_bol_byte;
5878 int seen_dots = 0;
5879 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
5880 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
5881
5882 for (i = 0; i < len; i++)
5883 {
5884 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
5885 seen_dots = 1;
5886 if (p1[i] != p2[i])
5887 return seen_dots;
5888 }
5889 p1 += len;
5890 if (*p1 == '\n')
5891 return 2;
5892 if (*p1++ == ' ' && *p1++ == '[')
5893 {
5894 int n = 0;
5895 while (*p1 >= '0' && *p1 <= '9')
5896 n = n * 10 + *p1++ - '0';
5897 if (strncmp (p1, " times]\n", 8) == 0)
5898 return n+1;
5899 }
5900 return 0;
5901 }
5902
5903
5904 /* Display an echo area message M with a specified length of NBYTES
5905 bytes. The string may include null characters. If M is 0, clear
5906 out any existing message, and let the mini-buffer text show
5907 through.
5908
5909 The buffer M must continue to exist until after the echo area gets
5910 cleared or some other message gets displayed there. This means do
5911 not pass text that is stored in a Lisp string; do not pass text in
5912 a buffer that was alloca'd. */
5913
5914 void
5915 message2 (m, nbytes, multibyte)
5916 const char *m;
5917 int nbytes;
5918 int multibyte;
5919 {
5920 /* First flush out any partial line written with print. */
5921 message_log_maybe_newline ();
5922 if (m)
5923 message_dolog (m, nbytes, 1, multibyte);
5924 message2_nolog (m, nbytes, multibyte);
5925 }
5926
5927
5928 /* The non-logging counterpart of message2. */
5929
5930 void
5931 message2_nolog (m, nbytes, multibyte)
5932 const char *m;
5933 int nbytes;
5934 {
5935 struct frame *sf = SELECTED_FRAME ();
5936 message_enable_multibyte = multibyte;
5937
5938 if (noninteractive)
5939 {
5940 if (noninteractive_need_newline)
5941 putc ('\n', stderr);
5942 noninteractive_need_newline = 0;
5943 if (m)
5944 fwrite (m, nbytes, 1, stderr);
5945 if (cursor_in_echo_area == 0)
5946 fprintf (stderr, "\n");
5947 fflush (stderr);
5948 }
5949 /* A null message buffer means that the frame hasn't really been
5950 initialized yet. Error messages get reported properly by
5951 cmd_error, so this must be just an informative message; toss it. */
5952 else if (INTERACTIVE
5953 && sf->glyphs_initialized_p
5954 && FRAME_MESSAGE_BUF (sf))
5955 {
5956 Lisp_Object mini_window;
5957 struct frame *f;
5958
5959 /* Get the frame containing the mini-buffer
5960 that the selected frame is using. */
5961 mini_window = FRAME_MINIBUF_WINDOW (sf);
5962 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5963
5964 FRAME_SAMPLE_VISIBILITY (f);
5965 if (FRAME_VISIBLE_P (sf)
5966 && ! FRAME_VISIBLE_P (f))
5967 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
5968
5969 if (m)
5970 {
5971 set_message (m, Qnil, nbytes, multibyte);
5972 if (minibuffer_auto_raise)
5973 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5974 }
5975 else
5976 clear_message (1, 1);
5977
5978 do_pending_window_change (0);
5979 echo_area_display (1);
5980 do_pending_window_change (0);
5981 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5982 (*frame_up_to_date_hook) (f);
5983 }
5984 }
5985
5986
5987 /* Display an echo area message M with a specified length of NBYTES
5988 bytes. The string may include null characters. If M is not a
5989 string, clear out any existing message, and let the mini-buffer
5990 text show through. */
5991
5992 void
5993 message3 (m, nbytes, multibyte)
5994 Lisp_Object m;
5995 int nbytes;
5996 int multibyte;
5997 {
5998 struct gcpro gcpro1;
5999
6000 GCPRO1 (m);
6001
6002 /* First flush out any partial line written with print. */
6003 message_log_maybe_newline ();
6004 if (STRINGP (m))
6005 message_dolog (SDATA (m), nbytes, 1, multibyte);
6006 message3_nolog (m, nbytes, multibyte);
6007
6008 UNGCPRO;
6009 }
6010
6011
6012 /* The non-logging version of message3. */
6013
6014 void
6015 message3_nolog (m, nbytes, multibyte)
6016 Lisp_Object m;
6017 int nbytes, multibyte;
6018 {
6019 struct frame *sf = SELECTED_FRAME ();
6020 message_enable_multibyte = multibyte;
6021
6022 if (noninteractive)
6023 {
6024 if (noninteractive_need_newline)
6025 putc ('\n', stderr);
6026 noninteractive_need_newline = 0;
6027 if (STRINGP (m))
6028 fwrite (SDATA (m), nbytes, 1, stderr);
6029 if (cursor_in_echo_area == 0)
6030 fprintf (stderr, "\n");
6031 fflush (stderr);
6032 }
6033 /* A null message buffer means that the frame hasn't really been
6034 initialized yet. Error messages get reported properly by
6035 cmd_error, so this must be just an informative message; toss it. */
6036 else if (INTERACTIVE
6037 && sf->glyphs_initialized_p
6038 && FRAME_MESSAGE_BUF (sf))
6039 {
6040 Lisp_Object mini_window;
6041 Lisp_Object frame;
6042 struct frame *f;
6043
6044 /* Get the frame containing the mini-buffer
6045 that the selected frame is using. */
6046 mini_window = FRAME_MINIBUF_WINDOW (sf);
6047 frame = XWINDOW (mini_window)->frame;
6048 f = XFRAME (frame);
6049
6050 FRAME_SAMPLE_VISIBILITY (f);
6051 if (FRAME_VISIBLE_P (sf)
6052 && !FRAME_VISIBLE_P (f))
6053 Fmake_frame_visible (frame);
6054
6055 if (STRINGP (m) && SCHARS (m) > 0)
6056 {
6057 set_message (NULL, m, nbytes, multibyte);
6058 if (minibuffer_auto_raise)
6059 Fraise_frame (frame);
6060 }
6061 else
6062 clear_message (1, 1);
6063
6064 do_pending_window_change (0);
6065 echo_area_display (1);
6066 do_pending_window_change (0);
6067 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6068 (*frame_up_to_date_hook) (f);
6069 }
6070 }
6071
6072
6073 /* Display a null-terminated echo area message M. If M is 0, clear
6074 out any existing message, and let the mini-buffer text show through.
6075
6076 The buffer M must continue to exist until after the echo area gets
6077 cleared or some other message gets displayed there. Do not pass
6078 text that is stored in a Lisp string. Do not pass text in a buffer
6079 that was alloca'd. */
6080
6081 void
6082 message1 (m)
6083 char *m;
6084 {
6085 message2 (m, (m ? strlen (m) : 0), 0);
6086 }
6087
6088
6089 /* The non-logging counterpart of message1. */
6090
6091 void
6092 message1_nolog (m)
6093 char *m;
6094 {
6095 message2_nolog (m, (m ? strlen (m) : 0), 0);
6096 }
6097
6098 /* Display a message M which contains a single %s
6099 which gets replaced with STRING. */
6100
6101 void
6102 message_with_string (m, string, log)
6103 char *m;
6104 Lisp_Object string;
6105 int log;
6106 {
6107 CHECK_STRING (string);
6108
6109 if (noninteractive)
6110 {
6111 if (m)
6112 {
6113 if (noninteractive_need_newline)
6114 putc ('\n', stderr);
6115 noninteractive_need_newline = 0;
6116 fprintf (stderr, m, SDATA (string));
6117 if (cursor_in_echo_area == 0)
6118 fprintf (stderr, "\n");
6119 fflush (stderr);
6120 }
6121 }
6122 else if (INTERACTIVE)
6123 {
6124 /* The frame whose minibuffer we're going to display the message on.
6125 It may be larger than the selected frame, so we need
6126 to use its buffer, not the selected frame's buffer. */
6127 Lisp_Object mini_window;
6128 struct frame *f, *sf = SELECTED_FRAME ();
6129
6130 /* Get the frame containing the minibuffer
6131 that the selected frame is using. */
6132 mini_window = FRAME_MINIBUF_WINDOW (sf);
6133 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6134
6135 /* A null message buffer means that the frame hasn't really been
6136 initialized yet. Error messages get reported properly by
6137 cmd_error, so this must be just an informative message; toss it. */
6138 if (FRAME_MESSAGE_BUF (f))
6139 {
6140 Lisp_Object args[2], message;
6141 struct gcpro gcpro1, gcpro2;
6142
6143 args[0] = build_string (m);
6144 args[1] = message = string;
6145 GCPRO2 (args[0], message);
6146 gcpro1.nvars = 2;
6147
6148 message = Fformat (2, args);
6149
6150 if (log)
6151 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
6152 else
6153 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
6154
6155 UNGCPRO;
6156
6157 /* Print should start at the beginning of the message
6158 buffer next time. */
6159 message_buf_print = 0;
6160 }
6161 }
6162 }
6163
6164
6165 /* Dump an informative message to the minibuf. If M is 0, clear out
6166 any existing message, and let the mini-buffer text show through. */
6167
6168 /* VARARGS 1 */
6169 void
6170 message (m, a1, a2, a3)
6171 char *m;
6172 EMACS_INT a1, a2, a3;
6173 {
6174 if (noninteractive)
6175 {
6176 if (m)
6177 {
6178 if (noninteractive_need_newline)
6179 putc ('\n', stderr);
6180 noninteractive_need_newline = 0;
6181 fprintf (stderr, m, a1, a2, a3);
6182 if (cursor_in_echo_area == 0)
6183 fprintf (stderr, "\n");
6184 fflush (stderr);
6185 }
6186 }
6187 else if (INTERACTIVE)
6188 {
6189 /* The frame whose mini-buffer we're going to display the message
6190 on. It may be larger than the selected frame, so we need to
6191 use its buffer, not the selected frame's buffer. */
6192 Lisp_Object mini_window;
6193 struct frame *f, *sf = SELECTED_FRAME ();
6194
6195 /* Get the frame containing the mini-buffer
6196 that the selected frame is using. */
6197 mini_window = FRAME_MINIBUF_WINDOW (sf);
6198 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6199
6200 /* A null message buffer means that the frame hasn't really been
6201 initialized yet. Error messages get reported properly by
6202 cmd_error, so this must be just an informative message; toss
6203 it. */
6204 if (FRAME_MESSAGE_BUF (f))
6205 {
6206 if (m)
6207 {
6208 int len;
6209 #ifdef NO_ARG_ARRAY
6210 char *a[3];
6211 a[0] = (char *) a1;
6212 a[1] = (char *) a2;
6213 a[2] = (char *) a3;
6214
6215 len = doprnt (FRAME_MESSAGE_BUF (f),
6216 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6217 #else
6218 len = doprnt (FRAME_MESSAGE_BUF (f),
6219 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
6220 (char **) &a1);
6221 #endif /* NO_ARG_ARRAY */
6222
6223 message2 (FRAME_MESSAGE_BUF (f), len, 0);
6224 }
6225 else
6226 message1 (0);
6227
6228 /* Print should start at the beginning of the message
6229 buffer next time. */
6230 message_buf_print = 0;
6231 }
6232 }
6233 }
6234
6235
6236 /* The non-logging version of message. */
6237
6238 void
6239 message_nolog (m, a1, a2, a3)
6240 char *m;
6241 EMACS_INT a1, a2, a3;
6242 {
6243 Lisp_Object old_log_max;
6244 old_log_max = Vmessage_log_max;
6245 Vmessage_log_max = Qnil;
6246 message (m, a1, a2, a3);
6247 Vmessage_log_max = old_log_max;
6248 }
6249
6250
6251 /* Display the current message in the current mini-buffer. This is
6252 only called from error handlers in process.c, and is not time
6253 critical. */
6254
6255 void
6256 update_echo_area ()
6257 {
6258 if (!NILP (echo_area_buffer[0]))
6259 {
6260 Lisp_Object string;
6261 string = Fcurrent_message ();
6262 message3 (string, SBYTES (string),
6263 !NILP (current_buffer->enable_multibyte_characters));
6264 }
6265 }
6266
6267
6268 /* Make sure echo area buffers in `echo_buffers' are live.
6269 If they aren't, make new ones. */
6270
6271 static void
6272 ensure_echo_area_buffers ()
6273 {
6274 int i;
6275
6276 for (i = 0; i < 2; ++i)
6277 if (!BUFFERP (echo_buffer[i])
6278 || NILP (XBUFFER (echo_buffer[i])->name))
6279 {
6280 char name[30];
6281 Lisp_Object old_buffer;
6282 int j;
6283
6284 old_buffer = echo_buffer[i];
6285 sprintf (name, " *Echo Area %d*", i);
6286 echo_buffer[i] = Fget_buffer_create (build_string (name));
6287 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
6288
6289 for (j = 0; j < 2; ++j)
6290 if (EQ (old_buffer, echo_area_buffer[j]))
6291 echo_area_buffer[j] = echo_buffer[i];
6292 }
6293 }
6294
6295
6296 /* Call FN with args A1..A4 with either the current or last displayed
6297 echo_area_buffer as current buffer.
6298
6299 WHICH zero means use the current message buffer
6300 echo_area_buffer[0]. If that is nil, choose a suitable buffer
6301 from echo_buffer[] and clear it.
6302
6303 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
6304 suitable buffer from echo_buffer[] and clear it.
6305
6306 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
6307 that the current message becomes the last displayed one, make
6308 choose a suitable buffer for echo_area_buffer[0], and clear it.
6309
6310 Value is what FN returns. */
6311
6312 static int
6313 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
6314 struct window *w;
6315 int which;
6316 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
6317 EMACS_INT a1;
6318 Lisp_Object a2;
6319 EMACS_INT a3, a4;
6320 {
6321 Lisp_Object buffer;
6322 int this_one, the_other, clear_buffer_p, rc;
6323 int count = SPECPDL_INDEX ();
6324
6325 /* If buffers aren't live, make new ones. */
6326 ensure_echo_area_buffers ();
6327
6328 clear_buffer_p = 0;
6329
6330 if (which == 0)
6331 this_one = 0, the_other = 1;
6332 else if (which > 0)
6333 this_one = 1, the_other = 0;
6334 else
6335 {
6336 this_one = 0, the_other = 1;
6337 clear_buffer_p = 1;
6338
6339 /* We need a fresh one in case the current echo buffer equals
6340 the one containing the last displayed echo area message. */
6341 if (!NILP (echo_area_buffer[this_one])
6342 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
6343 echo_area_buffer[this_one] = Qnil;
6344 }
6345
6346 /* Choose a suitable buffer from echo_buffer[] is we don't
6347 have one. */
6348 if (NILP (echo_area_buffer[this_one]))
6349 {
6350 echo_area_buffer[this_one]
6351 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
6352 ? echo_buffer[the_other]
6353 : echo_buffer[this_one]);
6354 clear_buffer_p = 1;
6355 }
6356
6357 buffer = echo_area_buffer[this_one];
6358
6359 /* Don't get confused by reusing the buffer used for echoing
6360 for a different purpose. */
6361 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
6362 cancel_echoing ();
6363
6364 record_unwind_protect (unwind_with_echo_area_buffer,
6365 with_echo_area_buffer_unwind_data (w));
6366
6367 /* Make the echo area buffer current. Note that for display
6368 purposes, it is not necessary that the displayed window's buffer
6369 == current_buffer, except for text property lookup. So, let's
6370 only set that buffer temporarily here without doing a full
6371 Fset_window_buffer. We must also change w->pointm, though,
6372 because otherwise an assertions in unshow_buffer fails, and Emacs
6373 aborts. */
6374 set_buffer_internal_1 (XBUFFER (buffer));
6375 if (w)
6376 {
6377 w->buffer = buffer;
6378 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
6379 }
6380
6381 current_buffer->undo_list = Qt;
6382 current_buffer->read_only = Qnil;
6383 specbind (Qinhibit_read_only, Qt);
6384 specbind (Qinhibit_modification_hooks, Qt);
6385
6386 if (clear_buffer_p && Z > BEG)
6387 del_range (BEG, Z);
6388
6389 xassert (BEGV >= BEG);
6390 xassert (ZV <= Z && ZV >= BEGV);
6391
6392 rc = fn (a1, a2, a3, a4);
6393
6394 xassert (BEGV >= BEG);
6395 xassert (ZV <= Z && ZV >= BEGV);
6396
6397 unbind_to (count, Qnil);
6398 return rc;
6399 }
6400
6401
6402 /* Save state that should be preserved around the call to the function
6403 FN called in with_echo_area_buffer. */
6404
6405 static Lisp_Object
6406 with_echo_area_buffer_unwind_data (w)
6407 struct window *w;
6408 {
6409 int i = 0;
6410 Lisp_Object vector;
6411
6412 /* Reduce consing by keeping one vector in
6413 Vwith_echo_area_save_vector. */
6414 vector = Vwith_echo_area_save_vector;
6415 Vwith_echo_area_save_vector = Qnil;
6416
6417 if (NILP (vector))
6418 vector = Fmake_vector (make_number (7), Qnil);
6419
6420 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
6421 AREF (vector, i) = Vdeactivate_mark, ++i;
6422 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
6423
6424 if (w)
6425 {
6426 XSETWINDOW (AREF (vector, i), w); ++i;
6427 AREF (vector, i) = w->buffer; ++i;
6428 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
6429 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
6430 }
6431 else
6432 {
6433 int end = i + 4;
6434 for (; i < end; ++i)
6435 AREF (vector, i) = Qnil;
6436 }
6437
6438 xassert (i == ASIZE (vector));
6439 return vector;
6440 }
6441
6442
6443 /* Restore global state from VECTOR which was created by
6444 with_echo_area_buffer_unwind_data. */
6445
6446 static Lisp_Object
6447 unwind_with_echo_area_buffer (vector)
6448 Lisp_Object vector;
6449 {
6450 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
6451 Vdeactivate_mark = AREF (vector, 1);
6452 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
6453
6454 if (WINDOWP (AREF (vector, 3)))
6455 {
6456 struct window *w;
6457 Lisp_Object buffer, charpos, bytepos;
6458
6459 w = XWINDOW (AREF (vector, 3));
6460 buffer = AREF (vector, 4);
6461 charpos = AREF (vector, 5);
6462 bytepos = AREF (vector, 6);
6463
6464 w->buffer = buffer;
6465 set_marker_both (w->pointm, buffer,
6466 XFASTINT (charpos), XFASTINT (bytepos));
6467 }
6468
6469 Vwith_echo_area_save_vector = vector;
6470 return Qnil;
6471 }
6472
6473
6474 /* Set up the echo area for use by print functions. MULTIBYTE_P
6475 non-zero means we will print multibyte. */
6476
6477 void
6478 setup_echo_area_for_printing (multibyte_p)
6479 int multibyte_p;
6480 {
6481 ensure_echo_area_buffers ();
6482
6483 if (!message_buf_print)
6484 {
6485 /* A message has been output since the last time we printed.
6486 Choose a fresh echo area buffer. */
6487 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6488 echo_area_buffer[0] = echo_buffer[1];
6489 else
6490 echo_area_buffer[0] = echo_buffer[0];
6491
6492 /* Switch to that buffer and clear it. */
6493 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6494 current_buffer->truncate_lines = Qnil;
6495
6496 if (Z > BEG)
6497 {
6498 int count = SPECPDL_INDEX ();
6499 specbind (Qinhibit_read_only, Qt);
6500 /* Note that undo recording is always disabled. */
6501 del_range (BEG, Z);
6502 unbind_to (count, Qnil);
6503 }
6504 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6505
6506 /* Set up the buffer for the multibyteness we need. */
6507 if (multibyte_p
6508 != !NILP (current_buffer->enable_multibyte_characters))
6509 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
6510
6511 /* Raise the frame containing the echo area. */
6512 if (minibuffer_auto_raise)
6513 {
6514 struct frame *sf = SELECTED_FRAME ();
6515 Lisp_Object mini_window;
6516 mini_window = FRAME_MINIBUF_WINDOW (sf);
6517 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6518 }
6519
6520 message_log_maybe_newline ();
6521 message_buf_print = 1;
6522 }
6523 else
6524 {
6525 if (NILP (echo_area_buffer[0]))
6526 {
6527 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6528 echo_area_buffer[0] = echo_buffer[1];
6529 else
6530 echo_area_buffer[0] = echo_buffer[0];
6531 }
6532
6533 if (current_buffer != XBUFFER (echo_area_buffer[0]))
6534 {
6535 /* Someone switched buffers between print requests. */
6536 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6537 current_buffer->truncate_lines = Qnil;
6538 }
6539 }
6540 }
6541
6542
6543 /* Display an echo area message in window W. Value is non-zero if W's
6544 height is changed. If display_last_displayed_message_p is
6545 non-zero, display the message that was last displayed, otherwise
6546 display the current message. */
6547
6548 static int
6549 display_echo_area (w)
6550 struct window *w;
6551 {
6552 int i, no_message_p, window_height_changed_p, count;
6553
6554 /* Temporarily disable garbage collections while displaying the echo
6555 area. This is done because a GC can print a message itself.
6556 That message would modify the echo area buffer's contents while a
6557 redisplay of the buffer is going on, and seriously confuse
6558 redisplay. */
6559 count = inhibit_garbage_collection ();
6560
6561 /* If there is no message, we must call display_echo_area_1
6562 nevertheless because it resizes the window. But we will have to
6563 reset the echo_area_buffer in question to nil at the end because
6564 with_echo_area_buffer will sets it to an empty buffer. */
6565 i = display_last_displayed_message_p ? 1 : 0;
6566 no_message_p = NILP (echo_area_buffer[i]);
6567
6568 window_height_changed_p
6569 = with_echo_area_buffer (w, display_last_displayed_message_p,
6570 display_echo_area_1,
6571 (EMACS_INT) w, Qnil, 0, 0);
6572
6573 if (no_message_p)
6574 echo_area_buffer[i] = Qnil;
6575
6576 unbind_to (count, Qnil);
6577 return window_height_changed_p;
6578 }
6579
6580
6581 /* Helper for display_echo_area. Display the current buffer which
6582 contains the current echo area message in window W, a mini-window,
6583 a pointer to which is passed in A1. A2..A4 are currently not used.
6584 Change the height of W so that all of the message is displayed.
6585 Value is non-zero if height of W was changed. */
6586
6587 static int
6588 display_echo_area_1 (a1, a2, a3, a4)
6589 EMACS_INT a1;
6590 Lisp_Object a2;
6591 EMACS_INT a3, a4;
6592 {
6593 struct window *w = (struct window *) a1;
6594 Lisp_Object window;
6595 struct text_pos start;
6596 int window_height_changed_p = 0;
6597
6598 /* Do this before displaying, so that we have a large enough glyph
6599 matrix for the display. */
6600 window_height_changed_p = resize_mini_window (w, 0);
6601
6602 /* Display. */
6603 clear_glyph_matrix (w->desired_matrix);
6604 XSETWINDOW (window, w);
6605 SET_TEXT_POS (start, BEG, BEG_BYTE);
6606 try_window (window, start);
6607
6608 return window_height_changed_p;
6609 }
6610
6611
6612 /* Resize the echo area window to exactly the size needed for the
6613 currently displayed message, if there is one. If a mini-buffer
6614 is active, don't shrink it. */
6615
6616 void
6617 resize_echo_area_exactly ()
6618 {
6619 if (BUFFERP (echo_area_buffer[0])
6620 && WINDOWP (echo_area_window))
6621 {
6622 struct window *w = XWINDOW (echo_area_window);
6623 int resized_p;
6624 Lisp_Object resize_exactly;
6625
6626 if (minibuf_level == 0)
6627 resize_exactly = Qt;
6628 else
6629 resize_exactly = Qnil;
6630
6631 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
6632 (EMACS_INT) w, resize_exactly, 0, 0);
6633 if (resized_p)
6634 {
6635 ++windows_or_buffers_changed;
6636 ++update_mode_lines;
6637 redisplay_internal (0);
6638 }
6639 }
6640 }
6641
6642
6643 /* Callback function for with_echo_area_buffer, when used from
6644 resize_echo_area_exactly. A1 contains a pointer to the window to
6645 resize, EXACTLY non-nil means resize the mini-window exactly to the
6646 size of the text displayed. A3 and A4 are not used. Value is what
6647 resize_mini_window returns. */
6648
6649 static int
6650 resize_mini_window_1 (a1, exactly, a3, a4)
6651 EMACS_INT a1;
6652 Lisp_Object exactly;
6653 EMACS_INT a3, a4;
6654 {
6655 return resize_mini_window ((struct window *) a1, !NILP (exactly));
6656 }
6657
6658
6659 /* Resize mini-window W to fit the size of its contents. EXACT:P
6660 means size the window exactly to the size needed. Otherwise, it's
6661 only enlarged until W's buffer is empty. Value is non-zero if
6662 the window height has been changed. */
6663
6664 int
6665 resize_mini_window (w, exact_p)
6666 struct window *w;
6667 int exact_p;
6668 {
6669 struct frame *f = XFRAME (w->frame);
6670 int window_height_changed_p = 0;
6671
6672 xassert (MINI_WINDOW_P (w));
6673
6674 /* Don't resize windows while redisplaying a window; it would
6675 confuse redisplay functions when the size of the window they are
6676 displaying changes from under them. Such a resizing can happen,
6677 for instance, when which-func prints a long message while
6678 we are running fontification-functions. We're running these
6679 functions with safe_call which binds inhibit-redisplay to t. */
6680 if (!NILP (Vinhibit_redisplay))
6681 return 0;
6682
6683 /* Nil means don't try to resize. */
6684 if (NILP (Vresize_mini_windows)
6685 || (FRAME_X_P (f) && f->output_data.x == NULL))
6686 return 0;
6687
6688 if (!FRAME_MINIBUF_ONLY_P (f))
6689 {
6690 struct it it;
6691 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
6692 int total_height = XFASTINT (root->height) + XFASTINT (w->height);
6693 int height, max_height;
6694 int unit = CANON_Y_UNIT (f);
6695 struct text_pos start;
6696 struct buffer *old_current_buffer = NULL;
6697
6698 if (current_buffer != XBUFFER (w->buffer))
6699 {
6700 old_current_buffer = current_buffer;
6701 set_buffer_internal (XBUFFER (w->buffer));
6702 }
6703
6704 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
6705
6706 /* Compute the max. number of lines specified by the user. */
6707 if (FLOATP (Vmax_mini_window_height))
6708 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_HEIGHT (f);
6709 else if (INTEGERP (Vmax_mini_window_height))
6710 max_height = XINT (Vmax_mini_window_height);
6711 else
6712 max_height = total_height / 4;
6713
6714 /* Correct that max. height if it's bogus. */
6715 max_height = max (1, max_height);
6716 max_height = min (total_height, max_height);
6717
6718 /* Find out the height of the text in the window. */
6719 if (it.truncate_lines_p)
6720 height = 1;
6721 else
6722 {
6723 last_height = 0;
6724 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
6725 if (it.max_ascent == 0 && it.max_descent == 0)
6726 height = it.current_y + last_height;
6727 else
6728 height = it.current_y + it.max_ascent + it.max_descent;
6729 height -= it.extra_line_spacing;
6730 height = (height + unit - 1) / unit;
6731 }
6732
6733 /* Compute a suitable window start. */
6734 if (height > max_height)
6735 {
6736 height = max_height;
6737 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
6738 move_it_vertically_backward (&it, (height - 1) * unit);
6739 start = it.current.pos;
6740 }
6741 else
6742 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
6743 SET_MARKER_FROM_TEXT_POS (w->start, start);
6744
6745 if (EQ (Vresize_mini_windows, Qgrow_only))
6746 {
6747 /* Let it grow only, until we display an empty message, in which
6748 case the window shrinks again. */
6749 if (height > XFASTINT (w->height))
6750 {
6751 int old_height = XFASTINT (w->height);
6752 freeze_window_starts (f, 1);
6753 grow_mini_window (w, height - XFASTINT (w->height));
6754 window_height_changed_p = XFASTINT (w->height) != old_height;
6755 }
6756 else if (height < XFASTINT (w->height)
6757 && (exact_p || BEGV == ZV))
6758 {
6759 int old_height = XFASTINT (w->height);
6760 freeze_window_starts (f, 0);
6761 shrink_mini_window (w);
6762 window_height_changed_p = XFASTINT (w->height) != old_height;
6763 }
6764 }
6765 else
6766 {
6767 /* Always resize to exact size needed. */
6768 if (height > XFASTINT (w->height))
6769 {
6770 int old_height = XFASTINT (w->height);
6771 freeze_window_starts (f, 1);
6772 grow_mini_window (w, height - XFASTINT (w->height));
6773 window_height_changed_p = XFASTINT (w->height) != old_height;
6774 }
6775 else if (height < XFASTINT (w->height))
6776 {
6777 int old_height = XFASTINT (w->height);
6778 freeze_window_starts (f, 0);
6779 shrink_mini_window (w);
6780
6781 if (height)
6782 {
6783 freeze_window_starts (f, 1);
6784 grow_mini_window (w, height - XFASTINT (w->height));
6785 }
6786
6787 window_height_changed_p = XFASTINT (w->height) != old_height;
6788 }
6789 }
6790
6791 if (old_current_buffer)
6792 set_buffer_internal (old_current_buffer);
6793 }
6794
6795 return window_height_changed_p;
6796 }
6797
6798
6799 /* Value is the current message, a string, or nil if there is no
6800 current message. */
6801
6802 Lisp_Object
6803 current_message ()
6804 {
6805 Lisp_Object msg;
6806
6807 if (NILP (echo_area_buffer[0]))
6808 msg = Qnil;
6809 else
6810 {
6811 with_echo_area_buffer (0, 0, current_message_1,
6812 (EMACS_INT) &msg, Qnil, 0, 0);
6813 if (NILP (msg))
6814 echo_area_buffer[0] = Qnil;
6815 }
6816
6817 return msg;
6818 }
6819
6820
6821 static int
6822 current_message_1 (a1, a2, a3, a4)
6823 EMACS_INT a1;
6824 Lisp_Object a2;
6825 EMACS_INT a3, a4;
6826 {
6827 Lisp_Object *msg = (Lisp_Object *) a1;
6828
6829 if (Z > BEG)
6830 *msg = make_buffer_string (BEG, Z, 1);
6831 else
6832 *msg = Qnil;
6833 return 0;
6834 }
6835
6836
6837 /* Push the current message on Vmessage_stack for later restauration
6838 by restore_message. Value is non-zero if the current message isn't
6839 empty. This is a relatively infrequent operation, so it's not
6840 worth optimizing. */
6841
6842 int
6843 push_message ()
6844 {
6845 Lisp_Object msg;
6846 msg = current_message ();
6847 Vmessage_stack = Fcons (msg, Vmessage_stack);
6848 return STRINGP (msg);
6849 }
6850
6851
6852 /* Handler for record_unwind_protect calling pop_message. */
6853
6854 Lisp_Object
6855 push_message_unwind (dummy)
6856 Lisp_Object dummy;
6857 {
6858 pop_message ();
6859 return Qnil;
6860 }
6861
6862
6863 /* Restore message display from the top of Vmessage_stack. */
6864
6865 void
6866 restore_message ()
6867 {
6868 Lisp_Object msg;
6869
6870 xassert (CONSP (Vmessage_stack));
6871 msg = XCAR (Vmessage_stack);
6872 if (STRINGP (msg))
6873 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
6874 else
6875 message3_nolog (msg, 0, 0);
6876 }
6877
6878
6879 /* Pop the top-most entry off Vmessage_stack. */
6880
6881 void
6882 pop_message ()
6883 {
6884 xassert (CONSP (Vmessage_stack));
6885 Vmessage_stack = XCDR (Vmessage_stack);
6886 }
6887
6888
6889 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
6890 exits. If the stack is not empty, we have a missing pop_message
6891 somewhere. */
6892
6893 void
6894 check_message_stack ()
6895 {
6896 if (!NILP (Vmessage_stack))
6897 abort ();
6898 }
6899
6900
6901 /* Truncate to NCHARS what will be displayed in the echo area the next
6902 time we display it---but don't redisplay it now. */
6903
6904 void
6905 truncate_echo_area (nchars)
6906 int nchars;
6907 {
6908 if (nchars == 0)
6909 echo_area_buffer[0] = Qnil;
6910 /* A null message buffer means that the frame hasn't really been
6911 initialized yet. Error messages get reported properly by
6912 cmd_error, so this must be just an informative message; toss it. */
6913 else if (!noninteractive
6914 && INTERACTIVE
6915 && !NILP (echo_area_buffer[0]))
6916 {
6917 struct frame *sf = SELECTED_FRAME ();
6918 if (FRAME_MESSAGE_BUF (sf))
6919 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
6920 }
6921 }
6922
6923
6924 /* Helper function for truncate_echo_area. Truncate the current
6925 message to at most NCHARS characters. */
6926
6927 static int
6928 truncate_message_1 (nchars, a2, a3, a4)
6929 EMACS_INT nchars;
6930 Lisp_Object a2;
6931 EMACS_INT a3, a4;
6932 {
6933 if (BEG + nchars < Z)
6934 del_range (BEG + nchars, Z);
6935 if (Z == BEG)
6936 echo_area_buffer[0] = Qnil;
6937 return 0;
6938 }
6939
6940
6941 /* Set the current message to a substring of S or STRING.
6942
6943 If STRING is a Lisp string, set the message to the first NBYTES
6944 bytes from STRING. NBYTES zero means use the whole string. If
6945 STRING is multibyte, the message will be displayed multibyte.
6946
6947 If S is not null, set the message to the first LEN bytes of S. LEN
6948 zero means use the whole string. MULTIBYTE_P non-zero means S is
6949 multibyte. Display the message multibyte in that case. */
6950
6951 void
6952 set_message (s, string, nbytes, multibyte_p)
6953 const char *s;
6954 Lisp_Object string;
6955 int nbytes;
6956 {
6957 message_enable_multibyte
6958 = ((s && multibyte_p)
6959 || (STRINGP (string) && STRING_MULTIBYTE (string)));
6960
6961 with_echo_area_buffer (0, -1, set_message_1,
6962 (EMACS_INT) s, string, nbytes, multibyte_p);
6963 message_buf_print = 0;
6964 help_echo_showing_p = 0;
6965 }
6966
6967
6968 /* Helper function for set_message. Arguments have the same meaning
6969 as there, with A1 corresponding to S and A2 corresponding to STRING
6970 This function is called with the echo area buffer being
6971 current. */
6972
6973 static int
6974 set_message_1 (a1, a2, nbytes, multibyte_p)
6975 EMACS_INT a1;
6976 Lisp_Object a2;
6977 EMACS_INT nbytes, multibyte_p;
6978 {
6979 const char *s = (const char *) a1;
6980 Lisp_Object string = a2;
6981
6982 xassert (BEG == Z);
6983
6984 /* Change multibyteness of the echo buffer appropriately. */
6985 if (message_enable_multibyte
6986 != !NILP (current_buffer->enable_multibyte_characters))
6987 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
6988
6989 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
6990
6991 /* Insert new message at BEG. */
6992 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6993
6994 if (STRINGP (string))
6995 {
6996 int nchars;
6997
6998 if (nbytes == 0)
6999 nbytes = SBYTES (string);
7000 nchars = string_byte_to_char (string, nbytes);
7001
7002 /* This function takes care of single/multibyte conversion. We
7003 just have to ensure that the echo area buffer has the right
7004 setting of enable_multibyte_characters. */
7005 insert_from_string (string, 0, 0, nchars, nbytes, 1);
7006 }
7007 else if (s)
7008 {
7009 if (nbytes == 0)
7010 nbytes = strlen (s);
7011
7012 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
7013 {
7014 /* Convert from multi-byte to single-byte. */
7015 int i, c, n;
7016 unsigned char work[1];
7017
7018 /* Convert a multibyte string to single-byte. */
7019 for (i = 0; i < nbytes; i += n)
7020 {
7021 c = string_char_and_length (s + i, nbytes - i, &n);
7022 work[0] = (SINGLE_BYTE_CHAR_P (c)
7023 ? c
7024 : multibyte_char_to_unibyte (c, Qnil));
7025 insert_1_both (work, 1, 1, 1, 0, 0);
7026 }
7027 }
7028 else if (!multibyte_p
7029 && !NILP (current_buffer->enable_multibyte_characters))
7030 {
7031 /* Convert from single-byte to multi-byte. */
7032 int i, c, n;
7033 const unsigned char *msg = (const unsigned char *) s;
7034 unsigned char str[MAX_MULTIBYTE_LENGTH];
7035
7036 /* Convert a single-byte string to multibyte. */
7037 for (i = 0; i < nbytes; i++)
7038 {
7039 c = unibyte_char_to_multibyte (msg[i]);
7040 n = CHAR_STRING (c, str);
7041 insert_1_both (str, 1, n, 1, 0, 0);
7042 }
7043 }
7044 else
7045 insert_1 (s, nbytes, 1, 0, 0);
7046 }
7047
7048 return 0;
7049 }
7050
7051
7052 /* Clear messages. CURRENT_P non-zero means clear the current
7053 message. LAST_DISPLAYED_P non-zero means clear the message
7054 last displayed. */
7055
7056 void
7057 clear_message (current_p, last_displayed_p)
7058 int current_p, last_displayed_p;
7059 {
7060 if (current_p)
7061 {
7062 echo_area_buffer[0] = Qnil;
7063 message_cleared_p = 1;
7064 }
7065
7066 if (last_displayed_p)
7067 echo_area_buffer[1] = Qnil;
7068
7069 message_buf_print = 0;
7070 }
7071
7072 /* Clear garbaged frames.
7073
7074 This function is used where the old redisplay called
7075 redraw_garbaged_frames which in turn called redraw_frame which in
7076 turn called clear_frame. The call to clear_frame was a source of
7077 flickering. I believe a clear_frame is not necessary. It should
7078 suffice in the new redisplay to invalidate all current matrices,
7079 and ensure a complete redisplay of all windows. */
7080
7081 static void
7082 clear_garbaged_frames ()
7083 {
7084 if (frame_garbaged)
7085 {
7086 Lisp_Object tail, frame;
7087 int changed_count = 0;
7088
7089 FOR_EACH_FRAME (tail, frame)
7090 {
7091 struct frame *f = XFRAME (frame);
7092
7093 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
7094 {
7095 if (f->resized_p)
7096 Fredraw_frame (frame);
7097 clear_current_matrices (f);
7098 changed_count++;
7099 f->garbaged = 0;
7100 f->resized_p = 0;
7101 }
7102 }
7103
7104 frame_garbaged = 0;
7105 if (changed_count)
7106 ++windows_or_buffers_changed;
7107 }
7108 }
7109
7110
7111 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
7112 is non-zero update selected_frame. Value is non-zero if the
7113 mini-windows height has been changed. */
7114
7115 static int
7116 echo_area_display (update_frame_p)
7117 int update_frame_p;
7118 {
7119 Lisp_Object mini_window;
7120 struct window *w;
7121 struct frame *f;
7122 int window_height_changed_p = 0;
7123 struct frame *sf = SELECTED_FRAME ();
7124
7125 mini_window = FRAME_MINIBUF_WINDOW (sf);
7126 w = XWINDOW (mini_window);
7127 f = XFRAME (WINDOW_FRAME (w));
7128
7129 /* Don't display if frame is invisible or not yet initialized. */
7130 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
7131 return 0;
7132
7133 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
7134 #ifndef MAC_OS8
7135 #ifdef HAVE_WINDOW_SYSTEM
7136 /* When Emacs starts, selected_frame may be a visible terminal
7137 frame, even if we run under a window system. If we let this
7138 through, a message would be displayed on the terminal. */
7139 if (EQ (selected_frame, Vterminal_frame)
7140 && !NILP (Vwindow_system))
7141 return 0;
7142 #endif /* HAVE_WINDOW_SYSTEM */
7143 #endif
7144
7145 /* Redraw garbaged frames. */
7146 if (frame_garbaged)
7147 clear_garbaged_frames ();
7148
7149 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
7150 {
7151 echo_area_window = mini_window;
7152 window_height_changed_p = display_echo_area (w);
7153 w->must_be_updated_p = 1;
7154
7155 /* Update the display, unless called from redisplay_internal.
7156 Also don't update the screen during redisplay itself. The
7157 update will happen at the end of redisplay, and an update
7158 here could cause confusion. */
7159 if (update_frame_p && !redisplaying_p)
7160 {
7161 int n = 0;
7162
7163 /* If the display update has been interrupted by pending
7164 input, update mode lines in the frame. Due to the
7165 pending input, it might have been that redisplay hasn't
7166 been called, so that mode lines above the echo area are
7167 garbaged. This looks odd, so we prevent it here. */
7168 if (!display_completed)
7169 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
7170
7171 if (window_height_changed_p
7172 /* Don't do this if Emacs is shutting down. Redisplay
7173 needs to run hooks. */
7174 && !NILP (Vrun_hooks))
7175 {
7176 /* Must update other windows. Likewise as in other
7177 cases, don't let this update be interrupted by
7178 pending input. */
7179 int count = SPECPDL_INDEX ();
7180 specbind (Qredisplay_dont_pause, Qt);
7181 windows_or_buffers_changed = 1;
7182 redisplay_internal (0);
7183 unbind_to (count, Qnil);
7184 }
7185 else if (FRAME_WINDOW_P (f) && n == 0)
7186 {
7187 /* Window configuration is the same as before.
7188 Can do with a display update of the echo area,
7189 unless we displayed some mode lines. */
7190 update_single_window (w, 1);
7191 rif->flush_display (f);
7192 }
7193 else
7194 update_frame (f, 1, 1);
7195
7196 /* If cursor is in the echo area, make sure that the next
7197 redisplay displays the minibuffer, so that the cursor will
7198 be replaced with what the minibuffer wants. */
7199 if (cursor_in_echo_area)
7200 ++windows_or_buffers_changed;
7201 }
7202 }
7203 else if (!EQ (mini_window, selected_window))
7204 windows_or_buffers_changed++;
7205
7206 /* Last displayed message is now the current message. */
7207 echo_area_buffer[1] = echo_area_buffer[0];
7208
7209 /* Prevent redisplay optimization in redisplay_internal by resetting
7210 this_line_start_pos. This is done because the mini-buffer now
7211 displays the message instead of its buffer text. */
7212 if (EQ (mini_window, selected_window))
7213 CHARPOS (this_line_start_pos) = 0;
7214
7215 return window_height_changed_p;
7216 }
7217
7218
7219 \f
7220 /***********************************************************************
7221 Frame Titles
7222 ***********************************************************************/
7223
7224
7225 /* The frame title buffering code is also used by Fformat_mode_line.
7226 So it is not conditioned by HAVE_WINDOW_SYSTEM. */
7227
7228 /* A buffer for constructing frame titles in it; allocated from the
7229 heap in init_xdisp and resized as needed in store_frame_title_char. */
7230
7231 static char *frame_title_buf;
7232
7233 /* The buffer's end, and a current output position in it. */
7234
7235 static char *frame_title_buf_end;
7236 static char *frame_title_ptr;
7237
7238
7239 /* Store a single character C for the frame title in frame_title_buf.
7240 Re-allocate frame_title_buf if necessary. */
7241
7242 static void
7243 store_frame_title_char (c)
7244 char c;
7245 {
7246 /* If output position has reached the end of the allocated buffer,
7247 double the buffer's size. */
7248 if (frame_title_ptr == frame_title_buf_end)
7249 {
7250 int len = frame_title_ptr - frame_title_buf;
7251 int new_size = 2 * len * sizeof *frame_title_buf;
7252 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
7253 frame_title_buf_end = frame_title_buf + new_size;
7254 frame_title_ptr = frame_title_buf + len;
7255 }
7256
7257 *frame_title_ptr++ = c;
7258 }
7259
7260
7261 /* Store part of a frame title in frame_title_buf, beginning at
7262 frame_title_ptr. STR is the string to store. Do not copy
7263 characters that yield more columns than PRECISION; PRECISION <= 0
7264 means copy the whole string. Pad with spaces until FIELD_WIDTH
7265 number of characters have been copied; FIELD_WIDTH <= 0 means don't
7266 pad. Called from display_mode_element when it is used to build a
7267 frame title. */
7268
7269 static int
7270 store_frame_title (str, field_width, precision)
7271 const unsigned char *str;
7272 int field_width, precision;
7273 {
7274 int n = 0;
7275 int dummy, nbytes;
7276
7277 /* Copy at most PRECISION chars from STR. */
7278 nbytes = strlen (str);
7279 n+= c_string_width (str, nbytes, precision, &dummy, &nbytes);
7280 while (nbytes--)
7281 store_frame_title_char (*str++);
7282
7283 /* Fill up with spaces until FIELD_WIDTH reached. */
7284 while (field_width > 0
7285 && n < field_width)
7286 {
7287 store_frame_title_char (' ');
7288 ++n;
7289 }
7290
7291 return n;
7292 }
7293
7294 #ifdef HAVE_WINDOW_SYSTEM
7295
7296 /* Set the title of FRAME, if it has changed. The title format is
7297 Vicon_title_format if FRAME is iconified, otherwise it is
7298 frame_title_format. */
7299
7300 static void
7301 x_consider_frame_title (frame)
7302 Lisp_Object frame;
7303 {
7304 struct frame *f = XFRAME (frame);
7305
7306 if (FRAME_WINDOW_P (f)
7307 || FRAME_MINIBUF_ONLY_P (f)
7308 || f->explicit_name)
7309 {
7310 /* Do we have more than one visible frame on this X display? */
7311 Lisp_Object tail;
7312 Lisp_Object fmt;
7313 struct buffer *obuf;
7314 int len;
7315 struct it it;
7316
7317 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
7318 {
7319 Lisp_Object other_frame = XCAR (tail);
7320 struct frame *tf = XFRAME (other_frame);
7321
7322 if (tf != f
7323 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
7324 && !FRAME_MINIBUF_ONLY_P (tf)
7325 && !EQ (other_frame, tip_frame)
7326 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
7327 break;
7328 }
7329
7330 /* Set global variable indicating that multiple frames exist. */
7331 multiple_frames = CONSP (tail);
7332
7333 /* Switch to the buffer of selected window of the frame. Set up
7334 frame_title_ptr so that display_mode_element will output into it;
7335 then display the title. */
7336 obuf = current_buffer;
7337 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
7338 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
7339 frame_title_ptr = frame_title_buf;
7340 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
7341 NULL, DEFAULT_FACE_ID);
7342 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
7343 len = frame_title_ptr - frame_title_buf;
7344 frame_title_ptr = NULL;
7345 set_buffer_internal_1 (obuf);
7346
7347 /* Set the title only if it's changed. This avoids consing in
7348 the common case where it hasn't. (If it turns out that we've
7349 already wasted too much time by walking through the list with
7350 display_mode_element, then we might need to optimize at a
7351 higher level than this.) */
7352 if (! STRINGP (f->name)
7353 || SBYTES (f->name) != len
7354 || bcmp (frame_title_buf, SDATA (f->name), len) != 0)
7355 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
7356 }
7357 }
7358
7359 #endif /* not HAVE_WINDOW_SYSTEM */
7360
7361
7362
7363 \f
7364 /***********************************************************************
7365 Menu Bars
7366 ***********************************************************************/
7367
7368
7369 /* Prepare for redisplay by updating menu-bar item lists when
7370 appropriate. This can call eval. */
7371
7372 void
7373 prepare_menu_bars ()
7374 {
7375 int all_windows;
7376 struct gcpro gcpro1, gcpro2;
7377 struct frame *f;
7378 Lisp_Object tooltip_frame;
7379
7380 #ifdef HAVE_WINDOW_SYSTEM
7381 tooltip_frame = tip_frame;
7382 #else
7383 tooltip_frame = Qnil;
7384 #endif
7385
7386 /* Update all frame titles based on their buffer names, etc. We do
7387 this before the menu bars so that the buffer-menu will show the
7388 up-to-date frame titles. */
7389 #ifdef HAVE_WINDOW_SYSTEM
7390 if (windows_or_buffers_changed || update_mode_lines)
7391 {
7392 Lisp_Object tail, frame;
7393
7394 FOR_EACH_FRAME (tail, frame)
7395 {
7396 f = XFRAME (frame);
7397 if (!EQ (frame, tooltip_frame)
7398 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
7399 x_consider_frame_title (frame);
7400 }
7401 }
7402 #endif /* HAVE_WINDOW_SYSTEM */
7403
7404 /* Update the menu bar item lists, if appropriate. This has to be
7405 done before any actual redisplay or generation of display lines. */
7406 all_windows = (update_mode_lines
7407 || buffer_shared > 1
7408 || windows_or_buffers_changed);
7409 if (all_windows)
7410 {
7411 Lisp_Object tail, frame;
7412 int count = SPECPDL_INDEX ();
7413
7414 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7415
7416 FOR_EACH_FRAME (tail, frame)
7417 {
7418 f = XFRAME (frame);
7419
7420 /* Ignore tooltip frame. */
7421 if (EQ (frame, tooltip_frame))
7422 continue;
7423
7424 /* If a window on this frame changed size, report that to
7425 the user and clear the size-change flag. */
7426 if (FRAME_WINDOW_SIZES_CHANGED (f))
7427 {
7428 Lisp_Object functions;
7429
7430 /* Clear flag first in case we get an error below. */
7431 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
7432 functions = Vwindow_size_change_functions;
7433 GCPRO2 (tail, functions);
7434
7435 while (CONSP (functions))
7436 {
7437 call1 (XCAR (functions), frame);
7438 functions = XCDR (functions);
7439 }
7440 UNGCPRO;
7441 }
7442
7443 GCPRO1 (tail);
7444 update_menu_bar (f, 0);
7445 #ifdef HAVE_WINDOW_SYSTEM
7446 update_tool_bar (f, 0);
7447 #endif
7448 UNGCPRO;
7449 }
7450
7451 unbind_to (count, Qnil);
7452 }
7453 else
7454 {
7455 struct frame *sf = SELECTED_FRAME ();
7456 update_menu_bar (sf, 1);
7457 #ifdef HAVE_WINDOW_SYSTEM
7458 update_tool_bar (sf, 1);
7459 #endif
7460 }
7461
7462 /* Motif needs this. See comment in xmenu.c. Turn it off when
7463 pending_menu_activation is not defined. */
7464 #ifdef USE_X_TOOLKIT
7465 pending_menu_activation = 0;
7466 #endif
7467 }
7468
7469
7470 /* Update the menu bar item list for frame F. This has to be done
7471 before we start to fill in any display lines, because it can call
7472 eval.
7473
7474 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
7475
7476 static void
7477 update_menu_bar (f, save_match_data)
7478 struct frame *f;
7479 int save_match_data;
7480 {
7481 Lisp_Object window;
7482 register struct window *w;
7483
7484 /* If called recursively during a menu update, do nothing. This can
7485 happen when, for instance, an activate-menubar-hook causes a
7486 redisplay. */
7487 if (inhibit_menubar_update)
7488 return;
7489
7490 window = FRAME_SELECTED_WINDOW (f);
7491 w = XWINDOW (window);
7492
7493 #if 0 /* The if statement below this if statement used to include the
7494 condition !NILP (w->update_mode_line), rather than using
7495 update_mode_lines directly, and this if statement may have
7496 been added to make that condition work. Now the if
7497 statement below matches its comment, this isn't needed. */
7498 if (update_mode_lines)
7499 w->update_mode_line = Qt;
7500 #endif
7501
7502 if (FRAME_WINDOW_P (f)
7503 ?
7504 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS)
7505 FRAME_EXTERNAL_MENU_BAR (f)
7506 #else
7507 FRAME_MENU_BAR_LINES (f) > 0
7508 #endif
7509 : FRAME_MENU_BAR_LINES (f) > 0)
7510 {
7511 /* If the user has switched buffers or windows, we need to
7512 recompute to reflect the new bindings. But we'll
7513 recompute when update_mode_lines is set too; that means
7514 that people can use force-mode-line-update to request
7515 that the menu bar be recomputed. The adverse effect on
7516 the rest of the redisplay algorithm is about the same as
7517 windows_or_buffers_changed anyway. */
7518 if (windows_or_buffers_changed
7519 /* This used to test w->update_mode_line, but we believe
7520 there is no need to recompute the menu in that case. */
7521 || update_mode_lines
7522 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7523 < BUF_MODIFF (XBUFFER (w->buffer)))
7524 != !NILP (w->last_had_star))
7525 || ((!NILP (Vtransient_mark_mode)
7526 && !NILP (XBUFFER (w->buffer)->mark_active))
7527 != !NILP (w->region_showing)))
7528 {
7529 struct buffer *prev = current_buffer;
7530 int count = SPECPDL_INDEX ();
7531
7532 specbind (Qinhibit_menubar_update, Qt);
7533
7534 set_buffer_internal_1 (XBUFFER (w->buffer));
7535 if (save_match_data)
7536 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7537 if (NILP (Voverriding_local_map_menu_flag))
7538 {
7539 specbind (Qoverriding_terminal_local_map, Qnil);
7540 specbind (Qoverriding_local_map, Qnil);
7541 }
7542
7543 /* Run the Lucid hook. */
7544 safe_run_hooks (Qactivate_menubar_hook);
7545
7546 /* If it has changed current-menubar from previous value,
7547 really recompute the menu-bar from the value. */
7548 if (! NILP (Vlucid_menu_bar_dirty_flag))
7549 call0 (Qrecompute_lucid_menubar);
7550
7551 safe_run_hooks (Qmenu_bar_update_hook);
7552 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
7553
7554 /* Redisplay the menu bar in case we changed it. */
7555 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS)
7556 if (FRAME_WINDOW_P (f)
7557 #if defined (MAC_OS)
7558 /* All frames on Mac OS share the same menubar. So only the
7559 selected frame should be allowed to set it. */
7560 && f == SELECTED_FRAME ()
7561 #endif
7562 )
7563 set_frame_menubar (f, 0, 0);
7564 else
7565 /* On a terminal screen, the menu bar is an ordinary screen
7566 line, and this makes it get updated. */
7567 w->update_mode_line = Qt;
7568 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7569 /* In the non-toolkit version, the menu bar is an ordinary screen
7570 line, and this makes it get updated. */
7571 w->update_mode_line = Qt;
7572 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7573
7574 unbind_to (count, Qnil);
7575 set_buffer_internal_1 (prev);
7576 }
7577 }
7578 }
7579
7580
7581 \f
7582 /***********************************************************************
7583 Tool-bars
7584 ***********************************************************************/
7585
7586 #ifdef HAVE_WINDOW_SYSTEM
7587
7588 /* Update the tool-bar item list for frame F. This has to be done
7589 before we start to fill in any display lines. Called from
7590 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
7591 and restore it here. */
7592
7593 static void
7594 update_tool_bar (f, save_match_data)
7595 struct frame *f;
7596 int save_match_data;
7597 {
7598 if (WINDOWP (f->tool_bar_window)
7599 && XFASTINT (XWINDOW (f->tool_bar_window)->height) > 0)
7600 {
7601 Lisp_Object window;
7602 struct window *w;
7603
7604 window = FRAME_SELECTED_WINDOW (f);
7605 w = XWINDOW (window);
7606
7607 /* If the user has switched buffers or windows, we need to
7608 recompute to reflect the new bindings. But we'll
7609 recompute when update_mode_lines is set too; that means
7610 that people can use force-mode-line-update to request
7611 that the menu bar be recomputed. The adverse effect on
7612 the rest of the redisplay algorithm is about the same as
7613 windows_or_buffers_changed anyway. */
7614 if (windows_or_buffers_changed
7615 || !NILP (w->update_mode_line)
7616 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7617 < BUF_MODIFF (XBUFFER (w->buffer)))
7618 != !NILP (w->last_had_star))
7619 || ((!NILP (Vtransient_mark_mode)
7620 && !NILP (XBUFFER (w->buffer)->mark_active))
7621 != !NILP (w->region_showing)))
7622 {
7623 struct buffer *prev = current_buffer;
7624 int count = SPECPDL_INDEX ();
7625
7626 /* Set current_buffer to the buffer of the selected
7627 window of the frame, so that we get the right local
7628 keymaps. */
7629 set_buffer_internal_1 (XBUFFER (w->buffer));
7630
7631 /* Save match data, if we must. */
7632 if (save_match_data)
7633 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7634
7635 /* Make sure that we don't accidentally use bogus keymaps. */
7636 if (NILP (Voverriding_local_map_menu_flag))
7637 {
7638 specbind (Qoverriding_terminal_local_map, Qnil);
7639 specbind (Qoverriding_local_map, Qnil);
7640 }
7641
7642 /* Build desired tool-bar items from keymaps. */
7643 f->tool_bar_items
7644 = tool_bar_items (f->tool_bar_items, &f->n_tool_bar_items);
7645
7646 /* Redisplay the tool-bar in case we changed it. */
7647 w->update_mode_line = Qt;
7648
7649 unbind_to (count, Qnil);
7650 set_buffer_internal_1 (prev);
7651 }
7652 }
7653 }
7654
7655
7656 /* Set F->desired_tool_bar_string to a Lisp string representing frame
7657 F's desired tool-bar contents. F->tool_bar_items must have
7658 been set up previously by calling prepare_menu_bars. */
7659
7660 static void
7661 build_desired_tool_bar_string (f)
7662 struct frame *f;
7663 {
7664 int i, size, size_needed;
7665 struct gcpro gcpro1, gcpro2, gcpro3;
7666 Lisp_Object image, plist, props;
7667
7668 image = plist = props = Qnil;
7669 GCPRO3 (image, plist, props);
7670
7671 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
7672 Otherwise, make a new string. */
7673
7674 /* The size of the string we might be able to reuse. */
7675 size = (STRINGP (f->desired_tool_bar_string)
7676 ? SCHARS (f->desired_tool_bar_string)
7677 : 0);
7678
7679 /* We need one space in the string for each image. */
7680 size_needed = f->n_tool_bar_items;
7681
7682 /* Reuse f->desired_tool_bar_string, if possible. */
7683 if (size < size_needed || NILP (f->desired_tool_bar_string))
7684 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
7685 make_number (' '));
7686 else
7687 {
7688 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
7689 Fremove_text_properties (make_number (0), make_number (size),
7690 props, f->desired_tool_bar_string);
7691 }
7692
7693 /* Put a `display' property on the string for the images to display,
7694 put a `menu_item' property on tool-bar items with a value that
7695 is the index of the item in F's tool-bar item vector. */
7696 for (i = 0; i < f->n_tool_bar_items; ++i)
7697 {
7698 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
7699
7700 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
7701 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
7702 int hmargin, vmargin, relief, idx, end;
7703 extern Lisp_Object QCrelief, QCmargin, QCconversion, Qimage;
7704
7705 /* If image is a vector, choose the image according to the
7706 button state. */
7707 image = PROP (TOOL_BAR_ITEM_IMAGES);
7708 if (VECTORP (image))
7709 {
7710 if (enabled_p)
7711 idx = (selected_p
7712 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
7713 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
7714 else
7715 idx = (selected_p
7716 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
7717 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
7718
7719 xassert (ASIZE (image) >= idx);
7720 image = AREF (image, idx);
7721 }
7722 else
7723 idx = -1;
7724
7725 /* Ignore invalid image specifications. */
7726 if (!valid_image_p (image))
7727 continue;
7728
7729 /* Display the tool-bar button pressed, or depressed. */
7730 plist = Fcopy_sequence (XCDR (image));
7731
7732 /* Compute margin and relief to draw. */
7733 relief = (tool_bar_button_relief >= 0
7734 ? tool_bar_button_relief
7735 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
7736 hmargin = vmargin = relief;
7737
7738 if (INTEGERP (Vtool_bar_button_margin)
7739 && XINT (Vtool_bar_button_margin) > 0)
7740 {
7741 hmargin += XFASTINT (Vtool_bar_button_margin);
7742 vmargin += XFASTINT (Vtool_bar_button_margin);
7743 }
7744 else if (CONSP (Vtool_bar_button_margin))
7745 {
7746 if (INTEGERP (XCAR (Vtool_bar_button_margin))
7747 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
7748 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
7749
7750 if (INTEGERP (XCDR (Vtool_bar_button_margin))
7751 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
7752 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
7753 }
7754
7755 if (auto_raise_tool_bar_buttons_p)
7756 {
7757 /* Add a `:relief' property to the image spec if the item is
7758 selected. */
7759 if (selected_p)
7760 {
7761 plist = Fplist_put (plist, QCrelief, make_number (-relief));
7762 hmargin -= relief;
7763 vmargin -= relief;
7764 }
7765 }
7766 else
7767 {
7768 /* If image is selected, display it pressed, i.e. with a
7769 negative relief. If it's not selected, display it with a
7770 raised relief. */
7771 plist = Fplist_put (plist, QCrelief,
7772 (selected_p
7773 ? make_number (-relief)
7774 : make_number (relief)));
7775 hmargin -= relief;
7776 vmargin -= relief;
7777 }
7778
7779 /* Put a margin around the image. */
7780 if (hmargin || vmargin)
7781 {
7782 if (hmargin == vmargin)
7783 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
7784 else
7785 plist = Fplist_put (plist, QCmargin,
7786 Fcons (make_number (hmargin),
7787 make_number (vmargin)));
7788 }
7789
7790 /* If button is not enabled, and we don't have special images
7791 for the disabled state, make the image appear disabled by
7792 applying an appropriate algorithm to it. */
7793 if (!enabled_p && idx < 0)
7794 plist = Fplist_put (plist, QCconversion, Qdisabled);
7795
7796 /* Put a `display' text property on the string for the image to
7797 display. Put a `menu-item' property on the string that gives
7798 the start of this item's properties in the tool-bar items
7799 vector. */
7800 image = Fcons (Qimage, plist);
7801 props = list4 (Qdisplay, image,
7802 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
7803
7804 /* Let the last image hide all remaining spaces in the tool bar
7805 string. The string can be longer than needed when we reuse a
7806 previous string. */
7807 if (i + 1 == f->n_tool_bar_items)
7808 end = SCHARS (f->desired_tool_bar_string);
7809 else
7810 end = i + 1;
7811 Fadd_text_properties (make_number (i), make_number (end),
7812 props, f->desired_tool_bar_string);
7813 #undef PROP
7814 }
7815
7816 UNGCPRO;
7817 }
7818
7819
7820 /* Display one line of the tool-bar of frame IT->f. */
7821
7822 static void
7823 display_tool_bar_line (it)
7824 struct it *it;
7825 {
7826 struct glyph_row *row = it->glyph_row;
7827 int max_x = it->last_visible_x;
7828 struct glyph *last;
7829
7830 prepare_desired_row (row);
7831 row->y = it->current_y;
7832
7833 /* Note that this isn't made use of if the face hasn't a box,
7834 so there's no need to check the face here. */
7835 it->start_of_box_run_p = 1;
7836
7837 while (it->current_x < max_x)
7838 {
7839 int x_before, x, n_glyphs_before, i, nglyphs;
7840
7841 /* Get the next display element. */
7842 if (!get_next_display_element (it))
7843 break;
7844
7845 /* Produce glyphs. */
7846 x_before = it->current_x;
7847 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
7848 PRODUCE_GLYPHS (it);
7849
7850 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
7851 i = 0;
7852 x = x_before;
7853 while (i < nglyphs)
7854 {
7855 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
7856
7857 if (x + glyph->pixel_width > max_x)
7858 {
7859 /* Glyph doesn't fit on line. */
7860 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
7861 it->current_x = x;
7862 goto out;
7863 }
7864
7865 ++it->hpos;
7866 x += glyph->pixel_width;
7867 ++i;
7868 }
7869
7870 /* Stop at line ends. */
7871 if (ITERATOR_AT_END_OF_LINE_P (it))
7872 break;
7873
7874 set_iterator_to_next (it, 1);
7875 }
7876
7877 out:;
7878
7879 row->displays_text_p = row->used[TEXT_AREA] != 0;
7880 extend_face_to_end_of_line (it);
7881 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
7882 last->right_box_line_p = 1;
7883 if (last == row->glyphs[TEXT_AREA])
7884 last->left_box_line_p = 1;
7885 compute_line_metrics (it);
7886
7887 /* If line is empty, make it occupy the rest of the tool-bar. */
7888 if (!row->displays_text_p)
7889 {
7890 row->height = row->phys_height = it->last_visible_y - row->y;
7891 row->ascent = row->phys_ascent = 0;
7892 }
7893
7894 row->full_width_p = 1;
7895 row->continued_p = 0;
7896 row->truncated_on_left_p = 0;
7897 row->truncated_on_right_p = 0;
7898
7899 it->current_x = it->hpos = 0;
7900 it->current_y += row->height;
7901 ++it->vpos;
7902 ++it->glyph_row;
7903 }
7904
7905
7906 /* Value is the number of screen lines needed to make all tool-bar
7907 items of frame F visible. */
7908
7909 static int
7910 tool_bar_lines_needed (f)
7911 struct frame *f;
7912 {
7913 struct window *w = XWINDOW (f->tool_bar_window);
7914 struct it it;
7915
7916 /* Initialize an iterator for iteration over
7917 F->desired_tool_bar_string in the tool-bar window of frame F. */
7918 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7919 it.first_visible_x = 0;
7920 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7921 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7922
7923 while (!ITERATOR_AT_END_P (&it))
7924 {
7925 it.glyph_row = w->desired_matrix->rows;
7926 clear_glyph_row (it.glyph_row);
7927 display_tool_bar_line (&it);
7928 }
7929
7930 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
7931 }
7932
7933
7934 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
7935 0, 1, 0,
7936 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
7937 (frame)
7938 Lisp_Object frame;
7939 {
7940 struct frame *f;
7941 struct window *w;
7942 int nlines = 0;
7943
7944 if (NILP (frame))
7945 frame = selected_frame;
7946 else
7947 CHECK_FRAME (frame);
7948 f = XFRAME (frame);
7949
7950 if (WINDOWP (f->tool_bar_window)
7951 || (w = XWINDOW (f->tool_bar_window),
7952 XFASTINT (w->height) > 0))
7953 {
7954 update_tool_bar (f, 1);
7955 if (f->n_tool_bar_items)
7956 {
7957 build_desired_tool_bar_string (f);
7958 nlines = tool_bar_lines_needed (f);
7959 }
7960 }
7961
7962 return make_number (nlines);
7963 }
7964
7965
7966 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
7967 height should be changed. */
7968
7969 static int
7970 redisplay_tool_bar (f)
7971 struct frame *f;
7972 {
7973 struct window *w;
7974 struct it it;
7975 struct glyph_row *row;
7976 int change_height_p = 0;
7977
7978 /* If frame hasn't a tool-bar window or if it is zero-height, don't
7979 do anything. This means you must start with tool-bar-lines
7980 non-zero to get the auto-sizing effect. Or in other words, you
7981 can turn off tool-bars by specifying tool-bar-lines zero. */
7982 if (!WINDOWP (f->tool_bar_window)
7983 || (w = XWINDOW (f->tool_bar_window),
7984 XFASTINT (w->height) == 0))
7985 return 0;
7986
7987 /* Set up an iterator for the tool-bar window. */
7988 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7989 it.first_visible_x = 0;
7990 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7991 row = it.glyph_row;
7992
7993 /* Build a string that represents the contents of the tool-bar. */
7994 build_desired_tool_bar_string (f);
7995 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7996
7997 /* Display as many lines as needed to display all tool-bar items. */
7998 while (it.current_y < it.last_visible_y)
7999 display_tool_bar_line (&it);
8000
8001 /* It doesn't make much sense to try scrolling in the tool-bar
8002 window, so don't do it. */
8003 w->desired_matrix->no_scrolling_p = 1;
8004 w->must_be_updated_p = 1;
8005
8006 if (auto_resize_tool_bars_p)
8007 {
8008 int nlines;
8009
8010 /* If we couldn't display everything, change the tool-bar's
8011 height. */
8012 if (IT_STRING_CHARPOS (it) < it.end_charpos)
8013 change_height_p = 1;
8014
8015 /* If there are blank lines at the end, except for a partially
8016 visible blank line at the end that is smaller than
8017 CANON_Y_UNIT, change the tool-bar's height. */
8018 row = it.glyph_row - 1;
8019 if (!row->displays_text_p
8020 && row->height >= CANON_Y_UNIT (f))
8021 change_height_p = 1;
8022
8023 /* If row displays tool-bar items, but is partially visible,
8024 change the tool-bar's height. */
8025 if (row->displays_text_p
8026 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
8027 change_height_p = 1;
8028
8029 /* Resize windows as needed by changing the `tool-bar-lines'
8030 frame parameter. */
8031 if (change_height_p
8032 && (nlines = tool_bar_lines_needed (f),
8033 nlines != XFASTINT (w->height)))
8034 {
8035 extern Lisp_Object Qtool_bar_lines;
8036 Lisp_Object frame;
8037 int old_height = XFASTINT (w->height);
8038
8039 XSETFRAME (frame, f);
8040 clear_glyph_matrix (w->desired_matrix);
8041 Fmodify_frame_parameters (frame,
8042 Fcons (Fcons (Qtool_bar_lines,
8043 make_number (nlines)),
8044 Qnil));
8045 if (XFASTINT (w->height) != old_height)
8046 fonts_changed_p = 1;
8047 }
8048 }
8049
8050 return change_height_p;
8051 }
8052
8053
8054 /* Get information about the tool-bar item which is displayed in GLYPH
8055 on frame F. Return in *PROP_IDX the index where tool-bar item
8056 properties start in F->tool_bar_items. Value is zero if
8057 GLYPH doesn't display a tool-bar item. */
8058
8059 int
8060 tool_bar_item_info (f, glyph, prop_idx)
8061 struct frame *f;
8062 struct glyph *glyph;
8063 int *prop_idx;
8064 {
8065 Lisp_Object prop;
8066 int success_p;
8067 int charpos;
8068
8069 /* This function can be called asynchronously, which means we must
8070 exclude any possibility that Fget_text_property signals an
8071 error. */
8072 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
8073 charpos = max (0, charpos);
8074
8075 /* Get the text property `menu-item' at pos. The value of that
8076 property is the start index of this item's properties in
8077 F->tool_bar_items. */
8078 prop = Fget_text_property (make_number (charpos),
8079 Qmenu_item, f->current_tool_bar_string);
8080 if (INTEGERP (prop))
8081 {
8082 *prop_idx = XINT (prop);
8083 success_p = 1;
8084 }
8085 else
8086 success_p = 0;
8087
8088 return success_p;
8089 }
8090
8091 #endif /* HAVE_WINDOW_SYSTEM */
8092
8093
8094 \f
8095 /************************************************************************
8096 Horizontal scrolling
8097 ************************************************************************/
8098
8099 static int hscroll_window_tree P_ ((Lisp_Object));
8100 static int hscroll_windows P_ ((Lisp_Object));
8101
8102 /* For all leaf windows in the window tree rooted at WINDOW, set their
8103 hscroll value so that PT is (i) visible in the window, and (ii) so
8104 that it is not within a certain margin at the window's left and
8105 right border. Value is non-zero if any window's hscroll has been
8106 changed. */
8107
8108 static int
8109 hscroll_window_tree (window)
8110 Lisp_Object window;
8111 {
8112 int hscrolled_p = 0;
8113 int hscroll_relative_p = FLOATP (Vhscroll_step);
8114 int hscroll_step_abs = 0;
8115 double hscroll_step_rel = 0;
8116
8117 if (hscroll_relative_p)
8118 {
8119 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
8120 if (hscroll_step_rel < 0)
8121 {
8122 hscroll_relative_p = 0;
8123 hscroll_step_abs = 0;
8124 }
8125 }
8126 else if (INTEGERP (Vhscroll_step))
8127 {
8128 hscroll_step_abs = XINT (Vhscroll_step);
8129 if (hscroll_step_abs < 0)
8130 hscroll_step_abs = 0;
8131 }
8132 else
8133 hscroll_step_abs = 0;
8134
8135 while (WINDOWP (window))
8136 {
8137 struct window *w = XWINDOW (window);
8138
8139 if (WINDOWP (w->hchild))
8140 hscrolled_p |= hscroll_window_tree (w->hchild);
8141 else if (WINDOWP (w->vchild))
8142 hscrolled_p |= hscroll_window_tree (w->vchild);
8143 else if (w->cursor.vpos >= 0)
8144 {
8145 int h_margin, text_area_x, text_area_y;
8146 int text_area_width, text_area_height;
8147 struct glyph_row *current_cursor_row
8148 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
8149 struct glyph_row *desired_cursor_row
8150 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
8151 struct glyph_row *cursor_row
8152 = (desired_cursor_row->enabled_p
8153 ? desired_cursor_row
8154 : current_cursor_row);
8155
8156 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
8157 &text_area_width, &text_area_height);
8158
8159 /* Scroll when cursor is inside this scroll margin. */
8160 h_margin = hscroll_margin * CANON_X_UNIT (XFRAME (w->frame));
8161
8162 if ((XFASTINT (w->hscroll)
8163 && w->cursor.x <= h_margin)
8164 || (cursor_row->enabled_p
8165 && cursor_row->truncated_on_right_p
8166 && (w->cursor.x >= text_area_width - h_margin)))
8167 {
8168 struct it it;
8169 int hscroll;
8170 struct buffer *saved_current_buffer;
8171 int pt;
8172 int wanted_x;
8173
8174 /* Find point in a display of infinite width. */
8175 saved_current_buffer = current_buffer;
8176 current_buffer = XBUFFER (w->buffer);
8177
8178 if (w == XWINDOW (selected_window))
8179 pt = BUF_PT (current_buffer);
8180 else
8181 {
8182 pt = marker_position (w->pointm);
8183 pt = max (BEGV, pt);
8184 pt = min (ZV, pt);
8185 }
8186
8187 /* Move iterator to pt starting at cursor_row->start in
8188 a line with infinite width. */
8189 init_to_row_start (&it, w, cursor_row);
8190 it.last_visible_x = INFINITY;
8191 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
8192 current_buffer = saved_current_buffer;
8193
8194 /* Position cursor in window. */
8195 if (!hscroll_relative_p && hscroll_step_abs == 0)
8196 hscroll = max (0, it.current_x - text_area_width / 2)
8197 / CANON_X_UNIT (it.f);
8198 else if (w->cursor.x >= text_area_width - h_margin)
8199 {
8200 if (hscroll_relative_p)
8201 wanted_x = text_area_width * (1 - hscroll_step_rel)
8202 - h_margin;
8203 else
8204 wanted_x = text_area_width
8205 - hscroll_step_abs * CANON_X_UNIT (it.f)
8206 - h_margin;
8207 hscroll
8208 = max (0, it.current_x - wanted_x) / CANON_X_UNIT (it.f);
8209 }
8210 else
8211 {
8212 if (hscroll_relative_p)
8213 wanted_x = text_area_width * hscroll_step_rel
8214 + h_margin;
8215 else
8216 wanted_x = hscroll_step_abs * CANON_X_UNIT (it.f)
8217 + h_margin;
8218 hscroll
8219 = max (0, it.current_x - wanted_x) / CANON_X_UNIT (it.f);
8220 }
8221 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
8222
8223 /* Don't call Fset_window_hscroll if value hasn't
8224 changed because it will prevent redisplay
8225 optimizations. */
8226 if (XFASTINT (w->hscroll) != hscroll)
8227 {
8228 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
8229 w->hscroll = make_number (hscroll);
8230 hscrolled_p = 1;
8231 }
8232 }
8233 }
8234
8235 window = w->next;
8236 }
8237
8238 /* Value is non-zero if hscroll of any leaf window has been changed. */
8239 return hscrolled_p;
8240 }
8241
8242
8243 /* Set hscroll so that cursor is visible and not inside horizontal
8244 scroll margins for all windows in the tree rooted at WINDOW. See
8245 also hscroll_window_tree above. Value is non-zero if any window's
8246 hscroll has been changed. If it has, desired matrices on the frame
8247 of WINDOW are cleared. */
8248
8249 static int
8250 hscroll_windows (window)
8251 Lisp_Object window;
8252 {
8253 int hscrolled_p;
8254
8255 if (automatic_hscrolling_p)
8256 {
8257 hscrolled_p = hscroll_window_tree (window);
8258 if (hscrolled_p)
8259 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
8260 }
8261 else
8262 hscrolled_p = 0;
8263 return hscrolled_p;
8264 }
8265
8266
8267 \f
8268 /************************************************************************
8269 Redisplay
8270 ************************************************************************/
8271
8272 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
8273 to a non-zero value. This is sometimes handy to have in a debugger
8274 session. */
8275
8276 #if GLYPH_DEBUG
8277
8278 /* First and last unchanged row for try_window_id. */
8279
8280 int debug_first_unchanged_at_end_vpos;
8281 int debug_last_unchanged_at_beg_vpos;
8282
8283 /* Delta vpos and y. */
8284
8285 int debug_dvpos, debug_dy;
8286
8287 /* Delta in characters and bytes for try_window_id. */
8288
8289 int debug_delta, debug_delta_bytes;
8290
8291 /* Values of window_end_pos and window_end_vpos at the end of
8292 try_window_id. */
8293
8294 EMACS_INT debug_end_pos, debug_end_vpos;
8295
8296 /* Append a string to W->desired_matrix->method. FMT is a printf
8297 format string. A1...A9 are a supplement for a variable-length
8298 argument list. If trace_redisplay_p is non-zero also printf the
8299 resulting string to stderr. */
8300
8301 static void
8302 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
8303 struct window *w;
8304 char *fmt;
8305 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
8306 {
8307 char buffer[512];
8308 char *method = w->desired_matrix->method;
8309 int len = strlen (method);
8310 int size = sizeof w->desired_matrix->method;
8311 int remaining = size - len - 1;
8312
8313 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
8314 if (len && remaining)
8315 {
8316 method[len] = '|';
8317 --remaining, ++len;
8318 }
8319
8320 strncpy (method + len, buffer, remaining);
8321
8322 if (trace_redisplay_p)
8323 fprintf (stderr, "%p (%s): %s\n",
8324 w,
8325 ((BUFFERP (w->buffer)
8326 && STRINGP (XBUFFER (w->buffer)->name))
8327 ? (char *) SDATA (XBUFFER (w->buffer)->name)
8328 : "no buffer"),
8329 buffer);
8330 }
8331
8332 #endif /* GLYPH_DEBUG */
8333
8334
8335 /* Value is non-zero if all changes in window W, which displays
8336 current_buffer, are in the text between START and END. START is a
8337 buffer position, END is given as a distance from Z. Used in
8338 redisplay_internal for display optimization. */
8339
8340 static INLINE int
8341 text_outside_line_unchanged_p (w, start, end)
8342 struct window *w;
8343 int start, end;
8344 {
8345 int unchanged_p = 1;
8346
8347 /* If text or overlays have changed, see where. */
8348 if (XFASTINT (w->last_modified) < MODIFF
8349 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8350 {
8351 /* Gap in the line? */
8352 if (GPT < start || Z - GPT < end)
8353 unchanged_p = 0;
8354
8355 /* Changes start in front of the line, or end after it? */
8356 if (unchanged_p
8357 && (BEG_UNCHANGED < start - 1
8358 || END_UNCHANGED < end))
8359 unchanged_p = 0;
8360
8361 /* If selective display, can't optimize if changes start at the
8362 beginning of the line. */
8363 if (unchanged_p
8364 && INTEGERP (current_buffer->selective_display)
8365 && XINT (current_buffer->selective_display) > 0
8366 && (BEG_UNCHANGED < start || GPT <= start))
8367 unchanged_p = 0;
8368
8369 /* If there are overlays at the start or end of the line, these
8370 may have overlay strings with newlines in them. A change at
8371 START, for instance, may actually concern the display of such
8372 overlay strings as well, and they are displayed on different
8373 lines. So, quickly rule out this case. (For the future, it
8374 might be desirable to implement something more telling than
8375 just BEG/END_UNCHANGED.) */
8376 if (unchanged_p)
8377 {
8378 if (BEG + BEG_UNCHANGED == start
8379 && overlay_touches_p (start))
8380 unchanged_p = 0;
8381 if (END_UNCHANGED == end
8382 && overlay_touches_p (Z - end))
8383 unchanged_p = 0;
8384 }
8385 }
8386
8387 return unchanged_p;
8388 }
8389
8390
8391 /* Do a frame update, taking possible shortcuts into account. This is
8392 the main external entry point for redisplay.
8393
8394 If the last redisplay displayed an echo area message and that message
8395 is no longer requested, we clear the echo area or bring back the
8396 mini-buffer if that is in use. */
8397
8398 void
8399 redisplay ()
8400 {
8401 redisplay_internal (0);
8402 }
8403
8404
8405 /* Return 1 if point moved out of or into a composition. Otherwise
8406 return 0. PREV_BUF and PREV_PT are the last point buffer and
8407 position. BUF and PT are the current point buffer and position. */
8408
8409 int
8410 check_point_in_composition (prev_buf, prev_pt, buf, pt)
8411 struct buffer *prev_buf, *buf;
8412 int prev_pt, pt;
8413 {
8414 int start, end;
8415 Lisp_Object prop;
8416 Lisp_Object buffer;
8417
8418 XSETBUFFER (buffer, buf);
8419 /* Check a composition at the last point if point moved within the
8420 same buffer. */
8421 if (prev_buf == buf)
8422 {
8423 if (prev_pt == pt)
8424 /* Point didn't move. */
8425 return 0;
8426
8427 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
8428 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
8429 && COMPOSITION_VALID_P (start, end, prop)
8430 && start < prev_pt && end > prev_pt)
8431 /* The last point was within the composition. Return 1 iff
8432 point moved out of the composition. */
8433 return (pt <= start || pt >= end);
8434 }
8435
8436 /* Check a composition at the current point. */
8437 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
8438 && find_composition (pt, -1, &start, &end, &prop, buffer)
8439 && COMPOSITION_VALID_P (start, end, prop)
8440 && start < pt && end > pt);
8441 }
8442
8443
8444 /* Reconsider the setting of B->clip_changed which is displayed
8445 in window W. */
8446
8447 static INLINE void
8448 reconsider_clip_changes (w, b)
8449 struct window *w;
8450 struct buffer *b;
8451 {
8452 if (b->clip_changed
8453 && !NILP (w->window_end_valid)
8454 && w->current_matrix->buffer == b
8455 && w->current_matrix->zv == BUF_ZV (b)
8456 && w->current_matrix->begv == BUF_BEGV (b))
8457 b->clip_changed = 0;
8458
8459 /* If display wasn't paused, and W is not a tool bar window, see if
8460 point has been moved into or out of a composition. In that case,
8461 we set b->clip_changed to 1 to force updating the screen. If
8462 b->clip_changed has already been set to 1, we can skip this
8463 check. */
8464 if (!b->clip_changed
8465 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
8466 {
8467 int pt;
8468
8469 if (w == XWINDOW (selected_window))
8470 pt = BUF_PT (current_buffer);
8471 else
8472 pt = marker_position (w->pointm);
8473
8474 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
8475 || pt != XINT (w->last_point))
8476 && check_point_in_composition (w->current_matrix->buffer,
8477 XINT (w->last_point),
8478 XBUFFER (w->buffer), pt))
8479 b->clip_changed = 1;
8480 }
8481 }
8482
8483
8484 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
8485 response to any user action; therefore, we should preserve the echo
8486 area. (Actually, our caller does that job.) Perhaps in the future
8487 avoid recentering windows if it is not necessary; currently that
8488 causes some problems. */
8489
8490 static void
8491 redisplay_internal (preserve_echo_area)
8492 int preserve_echo_area;
8493 {
8494 struct window *w = XWINDOW (selected_window);
8495 struct frame *f = XFRAME (w->frame);
8496 int pause;
8497 int must_finish = 0;
8498 struct text_pos tlbufpos, tlendpos;
8499 int number_of_visible_frames;
8500 int count;
8501 struct frame *sf = SELECTED_FRAME ();
8502
8503 /* Non-zero means redisplay has to consider all windows on all
8504 frames. Zero means, only selected_window is considered. */
8505 int consider_all_windows_p;
8506
8507 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
8508
8509 /* No redisplay if running in batch mode or frame is not yet fully
8510 initialized, or redisplay is explicitly turned off by setting
8511 Vinhibit_redisplay. */
8512 if (noninteractive
8513 || !NILP (Vinhibit_redisplay)
8514 || !f->glyphs_initialized_p)
8515 return;
8516
8517 /* The flag redisplay_performed_directly_p is set by
8518 direct_output_for_insert when it already did the whole screen
8519 update necessary. */
8520 if (redisplay_performed_directly_p)
8521 {
8522 redisplay_performed_directly_p = 0;
8523 if (!hscroll_windows (selected_window))
8524 return;
8525 }
8526
8527 #ifdef USE_X_TOOLKIT
8528 if (popup_activated ())
8529 return;
8530 #endif
8531
8532 /* I don't think this happens but let's be paranoid. */
8533 if (redisplaying_p)
8534 return;
8535
8536 /* Record a function that resets redisplaying_p to its old value
8537 when we leave this function. */
8538 count = SPECPDL_INDEX ();
8539 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
8540 ++redisplaying_p;
8541 specbind (Qinhibit_free_realized_faces, Qnil);
8542
8543 retry:
8544 pause = 0;
8545 reconsider_clip_changes (w, current_buffer);
8546
8547 /* If new fonts have been loaded that make a glyph matrix adjustment
8548 necessary, do it. */
8549 if (fonts_changed_p)
8550 {
8551 adjust_glyphs (NULL);
8552 ++windows_or_buffers_changed;
8553 fonts_changed_p = 0;
8554 }
8555
8556 /* If face_change_count is non-zero, init_iterator will free all
8557 realized faces, which includes the faces referenced from current
8558 matrices. So, we can't reuse current matrices in this case. */
8559 if (face_change_count)
8560 ++windows_or_buffers_changed;
8561
8562 if (! FRAME_WINDOW_P (sf)
8563 && previous_terminal_frame != sf)
8564 {
8565 /* Since frames on an ASCII terminal share the same display
8566 area, displaying a different frame means redisplay the whole
8567 thing. */
8568 windows_or_buffers_changed++;
8569 SET_FRAME_GARBAGED (sf);
8570 XSETFRAME (Vterminal_frame, sf);
8571 }
8572 previous_terminal_frame = sf;
8573
8574 /* Set the visible flags for all frames. Do this before checking
8575 for resized or garbaged frames; they want to know if their frames
8576 are visible. See the comment in frame.h for
8577 FRAME_SAMPLE_VISIBILITY. */
8578 {
8579 Lisp_Object tail, frame;
8580
8581 number_of_visible_frames = 0;
8582
8583 FOR_EACH_FRAME (tail, frame)
8584 {
8585 struct frame *f = XFRAME (frame);
8586
8587 FRAME_SAMPLE_VISIBILITY (f);
8588 if (FRAME_VISIBLE_P (f))
8589 ++number_of_visible_frames;
8590 clear_desired_matrices (f);
8591 }
8592 }
8593
8594 /* Notice any pending interrupt request to change frame size. */
8595 do_pending_window_change (1);
8596
8597 /* Clear frames marked as garbaged. */
8598 if (frame_garbaged)
8599 clear_garbaged_frames ();
8600
8601 /* Build menubar and tool-bar items. */
8602 prepare_menu_bars ();
8603
8604 if (windows_or_buffers_changed)
8605 update_mode_lines++;
8606
8607 /* Detect case that we need to write or remove a star in the mode line. */
8608 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
8609 {
8610 w->update_mode_line = Qt;
8611 if (buffer_shared > 1)
8612 update_mode_lines++;
8613 }
8614
8615 /* If %c is in the mode line, update it if needed. */
8616 if (!NILP (w->column_number_displayed)
8617 /* This alternative quickly identifies a common case
8618 where no change is needed. */
8619 && !(PT == XFASTINT (w->last_point)
8620 && XFASTINT (w->last_modified) >= MODIFF
8621 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
8622 && (XFASTINT (w->column_number_displayed)
8623 != (int) current_column ())) /* iftc */
8624 w->update_mode_line = Qt;
8625
8626 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
8627
8628 /* The variable buffer_shared is set in redisplay_window and
8629 indicates that we redisplay a buffer in different windows. See
8630 there. */
8631 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
8632 || cursor_type_changed);
8633
8634 /* If specs for an arrow have changed, do thorough redisplay
8635 to ensure we remove any arrow that should no longer exist. */
8636 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
8637 || ! EQ (Voverlay_arrow_string, last_arrow_string))
8638 consider_all_windows_p = windows_or_buffers_changed = 1;
8639
8640 /* Normally the message* functions will have already displayed and
8641 updated the echo area, but the frame may have been trashed, or
8642 the update may have been preempted, so display the echo area
8643 again here. Checking message_cleared_p captures the case that
8644 the echo area should be cleared. */
8645 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
8646 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
8647 || (message_cleared_p
8648 && minibuf_level == 0
8649 /* If the mini-window is currently selected, this means the
8650 echo-area doesn't show through. */
8651 && !MINI_WINDOW_P (XWINDOW (selected_window))))
8652 {
8653 int window_height_changed_p = echo_area_display (0);
8654 must_finish = 1;
8655
8656 /* If we don't display the current message, don't clear the
8657 message_cleared_p flag, because, if we did, we wouldn't clear
8658 the echo area in the next redisplay which doesn't preserve
8659 the echo area. */
8660 if (!display_last_displayed_message_p)
8661 message_cleared_p = 0;
8662
8663 if (fonts_changed_p)
8664 goto retry;
8665 else if (window_height_changed_p)
8666 {
8667 consider_all_windows_p = 1;
8668 ++update_mode_lines;
8669 ++windows_or_buffers_changed;
8670
8671 /* If window configuration was changed, frames may have been
8672 marked garbaged. Clear them or we will experience
8673 surprises wrt scrolling. */
8674 if (frame_garbaged)
8675 clear_garbaged_frames ();
8676 }
8677 }
8678 else if (EQ (selected_window, minibuf_window)
8679 && (current_buffer->clip_changed
8680 || XFASTINT (w->last_modified) < MODIFF
8681 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8682 && resize_mini_window (w, 0))
8683 {
8684 /* Resized active mini-window to fit the size of what it is
8685 showing if its contents might have changed. */
8686 must_finish = 1;
8687 consider_all_windows_p = 1;
8688 ++windows_or_buffers_changed;
8689 ++update_mode_lines;
8690
8691 /* If window configuration was changed, frames may have been
8692 marked garbaged. Clear them or we will experience
8693 surprises wrt scrolling. */
8694 if (frame_garbaged)
8695 clear_garbaged_frames ();
8696 }
8697
8698
8699 /* If showing the region, and mark has changed, we must redisplay
8700 the whole window. The assignment to this_line_start_pos prevents
8701 the optimization directly below this if-statement. */
8702 if (((!NILP (Vtransient_mark_mode)
8703 && !NILP (XBUFFER (w->buffer)->mark_active))
8704 != !NILP (w->region_showing))
8705 || (!NILP (w->region_showing)
8706 && !EQ (w->region_showing,
8707 Fmarker_position (XBUFFER (w->buffer)->mark))))
8708 CHARPOS (this_line_start_pos) = 0;
8709
8710 /* Optimize the case that only the line containing the cursor in the
8711 selected window has changed. Variables starting with this_ are
8712 set in display_line and record information about the line
8713 containing the cursor. */
8714 tlbufpos = this_line_start_pos;
8715 tlendpos = this_line_end_pos;
8716 if (!consider_all_windows_p
8717 && CHARPOS (tlbufpos) > 0
8718 && NILP (w->update_mode_line)
8719 && !current_buffer->clip_changed
8720 && !current_buffer->prevent_redisplay_optimizations_p
8721 && FRAME_VISIBLE_P (XFRAME (w->frame))
8722 && !FRAME_OBSCURED_P (XFRAME (w->frame))
8723 /* Make sure recorded data applies to current buffer, etc. */
8724 && this_line_buffer == current_buffer
8725 && current_buffer == XBUFFER (w->buffer)
8726 && NILP (w->force_start)
8727 && NILP (w->optional_new_start)
8728 /* Point must be on the line that we have info recorded about. */
8729 && PT >= CHARPOS (tlbufpos)
8730 && PT <= Z - CHARPOS (tlendpos)
8731 /* All text outside that line, including its final newline,
8732 must be unchanged */
8733 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
8734 CHARPOS (tlendpos)))
8735 {
8736 if (CHARPOS (tlbufpos) > BEGV
8737 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
8738 && (CHARPOS (tlbufpos) == ZV
8739 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
8740 /* Former continuation line has disappeared by becoming empty */
8741 goto cancel;
8742 else if (XFASTINT (w->last_modified) < MODIFF
8743 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
8744 || MINI_WINDOW_P (w))
8745 {
8746 /* We have to handle the case of continuation around a
8747 wide-column character (See the comment in indent.c around
8748 line 885).
8749
8750 For instance, in the following case:
8751
8752 -------- Insert --------
8753 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
8754 J_I_ ==> J_I_ `^^' are cursors.
8755 ^^ ^^
8756 -------- --------
8757
8758 As we have to redraw the line above, we should goto cancel. */
8759
8760 struct it it;
8761 int line_height_before = this_line_pixel_height;
8762
8763 /* Note that start_display will handle the case that the
8764 line starting at tlbufpos is a continuation lines. */
8765 start_display (&it, w, tlbufpos);
8766
8767 /* Implementation note: It this still necessary? */
8768 if (it.current_x != this_line_start_x)
8769 goto cancel;
8770
8771 TRACE ((stderr, "trying display optimization 1\n"));
8772 w->cursor.vpos = -1;
8773 overlay_arrow_seen = 0;
8774 it.vpos = this_line_vpos;
8775 it.current_y = this_line_y;
8776 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
8777 display_line (&it);
8778
8779 /* If line contains point, is not continued,
8780 and ends at same distance from eob as before, we win */
8781 if (w->cursor.vpos >= 0
8782 /* Line is not continued, otherwise this_line_start_pos
8783 would have been set to 0 in display_line. */
8784 && CHARPOS (this_line_start_pos)
8785 /* Line ends as before. */
8786 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
8787 /* Line has same height as before. Otherwise other lines
8788 would have to be shifted up or down. */
8789 && this_line_pixel_height == line_height_before)
8790 {
8791 /* If this is not the window's last line, we must adjust
8792 the charstarts of the lines below. */
8793 if (it.current_y < it.last_visible_y)
8794 {
8795 struct glyph_row *row
8796 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
8797 int delta, delta_bytes;
8798
8799 if (Z - CHARPOS (tlendpos) == ZV)
8800 {
8801 /* This line ends at end of (accessible part of)
8802 buffer. There is no newline to count. */
8803 delta = (Z
8804 - CHARPOS (tlendpos)
8805 - MATRIX_ROW_START_CHARPOS (row));
8806 delta_bytes = (Z_BYTE
8807 - BYTEPOS (tlendpos)
8808 - MATRIX_ROW_START_BYTEPOS (row));
8809 }
8810 else
8811 {
8812 /* This line ends in a newline. Must take
8813 account of the newline and the rest of the
8814 text that follows. */
8815 delta = (Z
8816 - CHARPOS (tlendpos)
8817 - MATRIX_ROW_START_CHARPOS (row));
8818 delta_bytes = (Z_BYTE
8819 - BYTEPOS (tlendpos)
8820 - MATRIX_ROW_START_BYTEPOS (row));
8821 }
8822
8823 increment_matrix_positions (w->current_matrix,
8824 this_line_vpos + 1,
8825 w->current_matrix->nrows,
8826 delta, delta_bytes);
8827 }
8828
8829 /* If this row displays text now but previously didn't,
8830 or vice versa, w->window_end_vpos may have to be
8831 adjusted. */
8832 if ((it.glyph_row - 1)->displays_text_p)
8833 {
8834 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
8835 XSETINT (w->window_end_vpos, this_line_vpos);
8836 }
8837 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
8838 && this_line_vpos > 0)
8839 XSETINT (w->window_end_vpos, this_line_vpos - 1);
8840 w->window_end_valid = Qnil;
8841
8842 /* Update hint: No need to try to scroll in update_window. */
8843 w->desired_matrix->no_scrolling_p = 1;
8844
8845 #if GLYPH_DEBUG
8846 *w->desired_matrix->method = 0;
8847 debug_method_add (w, "optimization 1");
8848 #endif
8849 goto update;
8850 }
8851 else
8852 goto cancel;
8853 }
8854 else if (/* Cursor position hasn't changed. */
8855 PT == XFASTINT (w->last_point)
8856 /* Make sure the cursor was last displayed
8857 in this window. Otherwise we have to reposition it. */
8858 && 0 <= w->cursor.vpos
8859 && XINT (w->height) > w->cursor.vpos)
8860 {
8861 if (!must_finish)
8862 {
8863 do_pending_window_change (1);
8864
8865 /* We used to always goto end_of_redisplay here, but this
8866 isn't enough if we have a blinking cursor. */
8867 if (w->cursor_off_p == w->last_cursor_off_p)
8868 goto end_of_redisplay;
8869 }
8870 goto update;
8871 }
8872 /* If highlighting the region, or if the cursor is in the echo area,
8873 then we can't just move the cursor. */
8874 else if (! (!NILP (Vtransient_mark_mode)
8875 && !NILP (current_buffer->mark_active))
8876 && (EQ (selected_window, current_buffer->last_selected_window)
8877 || highlight_nonselected_windows)
8878 && NILP (w->region_showing)
8879 && NILP (Vshow_trailing_whitespace)
8880 && !cursor_in_echo_area)
8881 {
8882 struct it it;
8883 struct glyph_row *row;
8884
8885 /* Skip from tlbufpos to PT and see where it is. Note that
8886 PT may be in invisible text. If so, we will end at the
8887 next visible position. */
8888 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
8889 NULL, DEFAULT_FACE_ID);
8890 it.current_x = this_line_start_x;
8891 it.current_y = this_line_y;
8892 it.vpos = this_line_vpos;
8893
8894 /* The call to move_it_to stops in front of PT, but
8895 moves over before-strings. */
8896 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
8897
8898 if (it.vpos == this_line_vpos
8899 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
8900 row->enabled_p))
8901 {
8902 xassert (this_line_vpos == it.vpos);
8903 xassert (this_line_y == it.current_y);
8904 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
8905 #if GLYPH_DEBUG
8906 *w->desired_matrix->method = 0;
8907 debug_method_add (w, "optimization 3");
8908 #endif
8909 goto update;
8910 }
8911 else
8912 goto cancel;
8913 }
8914
8915 cancel:
8916 /* Text changed drastically or point moved off of line. */
8917 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
8918 }
8919
8920 CHARPOS (this_line_start_pos) = 0;
8921 consider_all_windows_p |= buffer_shared > 1;
8922 ++clear_face_cache_count;
8923
8924
8925 /* Build desired matrices, and update the display. If
8926 consider_all_windows_p is non-zero, do it for all windows on all
8927 frames. Otherwise do it for selected_window, only. */
8928
8929 if (consider_all_windows_p)
8930 {
8931 Lisp_Object tail, frame;
8932 int i, n = 0, size = 50;
8933 struct frame **updated
8934 = (struct frame **) alloca (size * sizeof *updated);
8935
8936 /* Clear the face cache eventually. */
8937 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
8938 {
8939 clear_face_cache (0);
8940 clear_face_cache_count = 0;
8941 }
8942
8943 /* Recompute # windows showing selected buffer. This will be
8944 incremented each time such a window is displayed. */
8945 buffer_shared = 0;
8946
8947 FOR_EACH_FRAME (tail, frame)
8948 {
8949 struct frame *f = XFRAME (frame);
8950
8951 if (FRAME_WINDOW_P (f) || f == sf)
8952 {
8953 #ifdef HAVE_WINDOW_SYSTEM
8954 if (clear_face_cache_count % 50 == 0
8955 && FRAME_WINDOW_P (f))
8956 clear_image_cache (f, 0);
8957 #endif /* HAVE_WINDOW_SYSTEM */
8958
8959 /* Mark all the scroll bars to be removed; we'll redeem
8960 the ones we want when we redisplay their windows. */
8961 if (condemn_scroll_bars_hook)
8962 condemn_scroll_bars_hook (f);
8963
8964 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8965 redisplay_windows (FRAME_ROOT_WINDOW (f));
8966
8967 /* Any scroll bars which redisplay_windows should have
8968 nuked should now go away. */
8969 if (judge_scroll_bars_hook)
8970 judge_scroll_bars_hook (f);
8971
8972 /* If fonts changed, display again. */
8973 if (fonts_changed_p)
8974 goto retry;
8975
8976 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8977 {
8978 /* See if we have to hscroll. */
8979 if (hscroll_windows (f->root_window))
8980 goto retry;
8981
8982 /* Prevent various kinds of signals during display
8983 update. stdio is not robust about handling
8984 signals, which can cause an apparent I/O
8985 error. */
8986 if (interrupt_input)
8987 unrequest_sigio ();
8988 stop_polling ();
8989
8990 /* Update the display. */
8991 set_window_update_flags (XWINDOW (f->root_window), 1);
8992 pause |= update_frame (f, 0, 0);
8993 if (pause)
8994 break;
8995
8996 if (n == size)
8997 {
8998 int nbytes = size * sizeof *updated;
8999 struct frame **p = (struct frame **) alloca (2 * nbytes);
9000 bcopy (updated, p, nbytes);
9001 size *= 2;
9002 }
9003
9004 updated[n++] = f;
9005 }
9006 }
9007 }
9008
9009 /* Do the mark_window_display_accurate after all windows have
9010 been redisplayed because this call resets flags in buffers
9011 which are needed for proper redisplay. */
9012 for (i = 0; i < n; ++i)
9013 {
9014 struct frame *f = updated[i];
9015 mark_window_display_accurate (f->root_window, 1);
9016 if (frame_up_to_date_hook)
9017 frame_up_to_date_hook (f);
9018 }
9019 }
9020 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
9021 {
9022 Lisp_Object mini_window;
9023 struct frame *mini_frame;
9024
9025 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
9026 /* Use list_of_error, not Qerror, so that
9027 we catch only errors and don't run the debugger. */
9028 internal_condition_case_1 (redisplay_window_1, selected_window,
9029 list_of_error,
9030 redisplay_window_error);
9031
9032 /* Compare desired and current matrices, perform output. */
9033
9034 update:
9035 /* If fonts changed, display again. */
9036 if (fonts_changed_p)
9037 goto retry;
9038
9039 /* Prevent various kinds of signals during display update.
9040 stdio is not robust about handling signals,
9041 which can cause an apparent I/O error. */
9042 if (interrupt_input)
9043 unrequest_sigio ();
9044 stop_polling ();
9045
9046 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
9047 {
9048 if (hscroll_windows (selected_window))
9049 goto retry;
9050
9051 XWINDOW (selected_window)->must_be_updated_p = 1;
9052 pause = update_frame (sf, 0, 0);
9053 }
9054
9055 /* We may have called echo_area_display at the top of this
9056 function. If the echo area is on another frame, that may
9057 have put text on a frame other than the selected one, so the
9058 above call to update_frame would not have caught it. Catch
9059 it here. */
9060 mini_window = FRAME_MINIBUF_WINDOW (sf);
9061 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9062
9063 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
9064 {
9065 XWINDOW (mini_window)->must_be_updated_p = 1;
9066 pause |= update_frame (mini_frame, 0, 0);
9067 if (!pause && hscroll_windows (mini_window))
9068 goto retry;
9069 }
9070 }
9071
9072 /* If display was paused because of pending input, make sure we do a
9073 thorough update the next time. */
9074 if (pause)
9075 {
9076 /* Prevent the optimization at the beginning of
9077 redisplay_internal that tries a single-line update of the
9078 line containing the cursor in the selected window. */
9079 CHARPOS (this_line_start_pos) = 0;
9080
9081 /* Let the overlay arrow be updated the next time. */
9082 if (!NILP (last_arrow_position))
9083 {
9084 last_arrow_position = Qt;
9085 last_arrow_string = Qt;
9086 }
9087
9088 /* If we pause after scrolling, some rows in the current
9089 matrices of some windows are not valid. */
9090 if (!WINDOW_FULL_WIDTH_P (w)
9091 && !FRAME_WINDOW_P (XFRAME (w->frame)))
9092 update_mode_lines = 1;
9093 }
9094 else
9095 {
9096 if (!consider_all_windows_p)
9097 {
9098 /* This has already been done above if
9099 consider_all_windows_p is set. */
9100 mark_window_display_accurate_1 (w, 1);
9101
9102 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
9103 last_arrow_string = Voverlay_arrow_string;
9104
9105 if (frame_up_to_date_hook != 0)
9106 frame_up_to_date_hook (sf);
9107 }
9108
9109 update_mode_lines = 0;
9110 windows_or_buffers_changed = 0;
9111 cursor_type_changed = 0;
9112 }
9113
9114 /* Start SIGIO interrupts coming again. Having them off during the
9115 code above makes it less likely one will discard output, but not
9116 impossible, since there might be stuff in the system buffer here.
9117 But it is much hairier to try to do anything about that. */
9118 if (interrupt_input)
9119 request_sigio ();
9120 start_polling ();
9121
9122 /* If a frame has become visible which was not before, redisplay
9123 again, so that we display it. Expose events for such a frame
9124 (which it gets when becoming visible) don't call the parts of
9125 redisplay constructing glyphs, so simply exposing a frame won't
9126 display anything in this case. So, we have to display these
9127 frames here explicitly. */
9128 if (!pause)
9129 {
9130 Lisp_Object tail, frame;
9131 int new_count = 0;
9132
9133 FOR_EACH_FRAME (tail, frame)
9134 {
9135 int this_is_visible = 0;
9136
9137 if (XFRAME (frame)->visible)
9138 this_is_visible = 1;
9139 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
9140 if (XFRAME (frame)->visible)
9141 this_is_visible = 1;
9142
9143 if (this_is_visible)
9144 new_count++;
9145 }
9146
9147 if (new_count != number_of_visible_frames)
9148 windows_or_buffers_changed++;
9149 }
9150
9151 /* Change frame size now if a change is pending. */
9152 do_pending_window_change (1);
9153
9154 /* If we just did a pending size change, or have additional
9155 visible frames, redisplay again. */
9156 if (windows_or_buffers_changed && !pause)
9157 goto retry;
9158
9159 end_of_redisplay:
9160 unbind_to (count, Qnil);
9161 }
9162
9163
9164 /* Redisplay, but leave alone any recent echo area message unless
9165 another message has been requested in its place.
9166
9167 This is useful in situations where you need to redisplay but no
9168 user action has occurred, making it inappropriate for the message
9169 area to be cleared. See tracking_off and
9170 wait_reading_process_input for examples of these situations.
9171
9172 FROM_WHERE is an integer saying from where this function was
9173 called. This is useful for debugging. */
9174
9175 void
9176 redisplay_preserve_echo_area (from_where)
9177 int from_where;
9178 {
9179 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
9180
9181 if (!NILP (echo_area_buffer[1]))
9182 {
9183 /* We have a previously displayed message, but no current
9184 message. Redisplay the previous message. */
9185 display_last_displayed_message_p = 1;
9186 redisplay_internal (1);
9187 display_last_displayed_message_p = 0;
9188 }
9189 else
9190 redisplay_internal (1);
9191 }
9192
9193
9194 /* Function registered with record_unwind_protect in
9195 redisplay_internal. Reset redisplaying_p to the value it had
9196 before redisplay_internal was called, and clear
9197 prevent_freeing_realized_faces_p. */
9198
9199 static Lisp_Object
9200 unwind_redisplay (old_redisplaying_p)
9201 Lisp_Object old_redisplaying_p;
9202 {
9203 redisplaying_p = XFASTINT (old_redisplaying_p);
9204 return Qnil;
9205 }
9206
9207
9208 /* Mark the display of window W as accurate or inaccurate. If
9209 ACCURATE_P is non-zero mark display of W as accurate. If
9210 ACCURATE_P is zero, arrange for W to be redisplayed the next time
9211 redisplay_internal is called. */
9212
9213 static void
9214 mark_window_display_accurate_1 (w, accurate_p)
9215 struct window *w;
9216 int accurate_p;
9217 {
9218 if (BUFFERP (w->buffer))
9219 {
9220 struct buffer *b = XBUFFER (w->buffer);
9221
9222 w->last_modified
9223 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
9224 w->last_overlay_modified
9225 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
9226 w->last_had_star
9227 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
9228
9229 if (accurate_p)
9230 {
9231 b->clip_changed = 0;
9232 b->prevent_redisplay_optimizations_p = 0;
9233
9234 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
9235 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
9236 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
9237 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
9238
9239 w->current_matrix->buffer = b;
9240 w->current_matrix->begv = BUF_BEGV (b);
9241 w->current_matrix->zv = BUF_ZV (b);
9242
9243 w->last_cursor = w->cursor;
9244 w->last_cursor_off_p = w->cursor_off_p;
9245
9246 if (w == XWINDOW (selected_window))
9247 w->last_point = make_number (BUF_PT (b));
9248 else
9249 w->last_point = make_number (XMARKER (w->pointm)->charpos);
9250 }
9251 }
9252
9253 if (accurate_p)
9254 {
9255 w->window_end_valid = w->buffer;
9256 #if 0 /* This is incorrect with variable-height lines. */
9257 xassert (XINT (w->window_end_vpos)
9258 < (XINT (w->height)
9259 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
9260 #endif
9261 w->update_mode_line = Qnil;
9262 }
9263 }
9264
9265
9266 /* Mark the display of windows in the window tree rooted at WINDOW as
9267 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
9268 windows as accurate. If ACCURATE_P is zero, arrange for windows to
9269 be redisplayed the next time redisplay_internal is called. */
9270
9271 void
9272 mark_window_display_accurate (window, accurate_p)
9273 Lisp_Object window;
9274 int accurate_p;
9275 {
9276 struct window *w;
9277
9278 for (; !NILP (window); window = w->next)
9279 {
9280 w = XWINDOW (window);
9281 mark_window_display_accurate_1 (w, accurate_p);
9282
9283 if (!NILP (w->vchild))
9284 mark_window_display_accurate (w->vchild, accurate_p);
9285 if (!NILP (w->hchild))
9286 mark_window_display_accurate (w->hchild, accurate_p);
9287 }
9288
9289 if (accurate_p)
9290 {
9291 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
9292 last_arrow_string = Voverlay_arrow_string;
9293 }
9294 else
9295 {
9296 /* Force a thorough redisplay the next time by setting
9297 last_arrow_position and last_arrow_string to t, which is
9298 unequal to any useful value of Voverlay_arrow_... */
9299 last_arrow_position = Qt;
9300 last_arrow_string = Qt;
9301 }
9302 }
9303
9304
9305 /* Return value in display table DP (Lisp_Char_Table *) for character
9306 C. Since a display table doesn't have any parent, we don't have to
9307 follow parent. Do not call this function directly but use the
9308 macro DISP_CHAR_VECTOR. */
9309
9310 Lisp_Object
9311 disp_char_vector (dp, c)
9312 struct Lisp_Char_Table *dp;
9313 int c;
9314 {
9315 int code[4], i;
9316 Lisp_Object val;
9317
9318 if (SINGLE_BYTE_CHAR_P (c))
9319 return (dp->contents[c]);
9320
9321 SPLIT_CHAR (c, code[0], code[1], code[2]);
9322 if (code[1] < 32)
9323 code[1] = -1;
9324 else if (code[2] < 32)
9325 code[2] = -1;
9326
9327 /* Here, the possible range of code[0] (== charset ID) is
9328 128..max_charset. Since the top level char table contains data
9329 for multibyte characters after 256th element, we must increment
9330 code[0] by 128 to get a correct index. */
9331 code[0] += 128;
9332 code[3] = -1; /* anchor */
9333
9334 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
9335 {
9336 val = dp->contents[code[i]];
9337 if (!SUB_CHAR_TABLE_P (val))
9338 return (NILP (val) ? dp->defalt : val);
9339 }
9340
9341 /* Here, val is a sub char table. We return the default value of
9342 it. */
9343 return (dp->defalt);
9344 }
9345
9346
9347 \f
9348 /***********************************************************************
9349 Window Redisplay
9350 ***********************************************************************/
9351
9352 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
9353
9354 static void
9355 redisplay_windows (window)
9356 Lisp_Object window;
9357 {
9358 while (!NILP (window))
9359 {
9360 struct window *w = XWINDOW (window);
9361
9362 if (!NILP (w->hchild))
9363 redisplay_windows (w->hchild);
9364 else if (!NILP (w->vchild))
9365 redisplay_windows (w->vchild);
9366 else
9367 {
9368 displayed_buffer = XBUFFER (w->buffer);
9369 /* Use list_of_error, not Qerror, so that
9370 we catch only errors and don't run the debugger. */
9371 internal_condition_case_1 (redisplay_window_0, window,
9372 list_of_error,
9373 redisplay_window_error);
9374 }
9375
9376 window = w->next;
9377 }
9378 }
9379
9380 static Lisp_Object
9381 redisplay_window_error ()
9382 {
9383 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
9384 return Qnil;
9385 }
9386
9387 static Lisp_Object
9388 redisplay_window_0 (window)
9389 Lisp_Object window;
9390 {
9391 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
9392 redisplay_window (window, 0);
9393 return Qnil;
9394 }
9395
9396 static Lisp_Object
9397 redisplay_window_1 (window)
9398 Lisp_Object window;
9399 {
9400 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
9401 redisplay_window (window, 1);
9402 return Qnil;
9403 }
9404 \f
9405 /* Set cursor position of W. PT is assumed to be displayed in ROW.
9406 DELTA is the number of bytes by which positions recorded in ROW
9407 differ from current buffer positions. */
9408
9409 void
9410 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
9411 struct window *w;
9412 struct glyph_row *row;
9413 struct glyph_matrix *matrix;
9414 int delta, delta_bytes, dy, dvpos;
9415 {
9416 struct glyph *glyph = row->glyphs[TEXT_AREA];
9417 struct glyph *end = glyph + row->used[TEXT_AREA];
9418 int x = row->x;
9419 int pt_old = PT - delta;
9420
9421 /* Skip over glyphs not having an object at the start of the row.
9422 These are special glyphs like truncation marks on terminal
9423 frames. */
9424 if (row->displays_text_p)
9425 while (glyph < end
9426 && INTEGERP (glyph->object)
9427 && glyph->charpos < 0)
9428 {
9429 x += glyph->pixel_width;
9430 ++glyph;
9431 }
9432
9433 while (glyph < end
9434 && !INTEGERP (glyph->object)
9435 && (!BUFFERP (glyph->object)
9436 || glyph->charpos < pt_old))
9437 {
9438 x += glyph->pixel_width;
9439 ++glyph;
9440 }
9441
9442 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
9443 w->cursor.x = x;
9444 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
9445 w->cursor.y = row->y + dy;
9446
9447 if (w == XWINDOW (selected_window))
9448 {
9449 if (!row->continued_p
9450 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
9451 && row->x == 0)
9452 {
9453 this_line_buffer = XBUFFER (w->buffer);
9454
9455 CHARPOS (this_line_start_pos)
9456 = MATRIX_ROW_START_CHARPOS (row) + delta;
9457 BYTEPOS (this_line_start_pos)
9458 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
9459
9460 CHARPOS (this_line_end_pos)
9461 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
9462 BYTEPOS (this_line_end_pos)
9463 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
9464
9465 this_line_y = w->cursor.y;
9466 this_line_pixel_height = row->height;
9467 this_line_vpos = w->cursor.vpos;
9468 this_line_start_x = row->x;
9469 }
9470 else
9471 CHARPOS (this_line_start_pos) = 0;
9472 }
9473 }
9474
9475
9476 /* Run window scroll functions, if any, for WINDOW with new window
9477 start STARTP. Sets the window start of WINDOW to that position.
9478
9479 We assume that the window's buffer is really current. */
9480
9481 static INLINE struct text_pos
9482 run_window_scroll_functions (window, startp)
9483 Lisp_Object window;
9484 struct text_pos startp;
9485 {
9486 struct window *w = XWINDOW (window);
9487 SET_MARKER_FROM_TEXT_POS (w->start, startp);
9488
9489 if (current_buffer != XBUFFER (w->buffer))
9490 abort ();
9491
9492 if (!NILP (Vwindow_scroll_functions))
9493 {
9494 run_hook_with_args_2 (Qwindow_scroll_functions, window,
9495 make_number (CHARPOS (startp)));
9496 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9497 /* In case the hook functions switch buffers. */
9498 if (current_buffer != XBUFFER (w->buffer))
9499 set_buffer_internal_1 (XBUFFER (w->buffer));
9500 }
9501
9502 return startp;
9503 }
9504
9505
9506 /* Make sure the line containing the cursor is fully visible.
9507 A value of 1 means there is nothing to be done.
9508 (Either the line is fully visible, or it cannot be made so,
9509 or we cannot tell.)
9510 A value of 0 means the caller should do scrolling
9511 as if point had gone off the screen. */
9512
9513 static int
9514 make_cursor_line_fully_visible (w)
9515 struct window *w;
9516 {
9517 struct glyph_matrix *matrix;
9518 struct glyph_row *row;
9519 int window_height;
9520
9521 /* It's not always possible to find the cursor, e.g, when a window
9522 is full of overlay strings. Don't do anything in that case. */
9523 if (w->cursor.vpos < 0)
9524 return 1;
9525
9526 matrix = w->desired_matrix;
9527 row = MATRIX_ROW (matrix, w->cursor.vpos);
9528
9529 /* If the cursor row is not partially visible, there's nothing to do. */
9530 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
9531 return 1;
9532
9533 /* If the row the cursor is in is taller than the window's height,
9534 it's not clear what to do, so do nothing. */
9535 window_height = window_box_height (w);
9536 if (row->height >= window_height)
9537 return 1;
9538
9539 return 0;
9540
9541 #if 0
9542 /* This code used to try to scroll the window just enough to make
9543 the line visible. It returned 0 to say that the caller should
9544 allocate larger glyph matrices. */
9545
9546 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
9547 {
9548 int dy = row->height - row->visible_height;
9549 w->vscroll = 0;
9550 w->cursor.y += dy;
9551 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9552 }
9553 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
9554 {
9555 int dy = - (row->height - row->visible_height);
9556 w->vscroll = dy;
9557 w->cursor.y += dy;
9558 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9559 }
9560
9561 /* When we change the cursor y-position of the selected window,
9562 change this_line_y as well so that the display optimization for
9563 the cursor line of the selected window in redisplay_internal uses
9564 the correct y-position. */
9565 if (w == XWINDOW (selected_window))
9566 this_line_y = w->cursor.y;
9567
9568 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
9569 redisplay with larger matrices. */
9570 if (matrix->nrows < required_matrix_height (w))
9571 {
9572 fonts_changed_p = 1;
9573 return 0;
9574 }
9575
9576 return 1;
9577 #endif /* 0 */
9578 }
9579
9580
9581 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
9582 non-zero means only WINDOW is redisplayed in redisplay_internal.
9583 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
9584 in redisplay_window to bring a partially visible line into view in
9585 the case that only the cursor has moved.
9586
9587 Value is
9588
9589 1 if scrolling succeeded
9590
9591 0 if scrolling didn't find point.
9592
9593 -1 if new fonts have been loaded so that we must interrupt
9594 redisplay, adjust glyph matrices, and try again. */
9595
9596 enum
9597 {
9598 SCROLLING_SUCCESS,
9599 SCROLLING_FAILED,
9600 SCROLLING_NEED_LARGER_MATRICES
9601 };
9602
9603 static int
9604 try_scrolling (window, just_this_one_p, scroll_conservatively,
9605 scroll_step, temp_scroll_step)
9606 Lisp_Object window;
9607 int just_this_one_p;
9608 EMACS_INT scroll_conservatively, scroll_step;
9609 int temp_scroll_step;
9610 {
9611 struct window *w = XWINDOW (window);
9612 struct frame *f = XFRAME (w->frame);
9613 struct text_pos scroll_margin_pos;
9614 struct text_pos pos;
9615 struct text_pos startp;
9616 struct it it;
9617 Lisp_Object window_end;
9618 int this_scroll_margin;
9619 int dy = 0;
9620 int scroll_max;
9621 int rc;
9622 int amount_to_scroll = 0;
9623 Lisp_Object aggressive;
9624 int height;
9625
9626 #if GLYPH_DEBUG
9627 debug_method_add (w, "try_scrolling");
9628 #endif
9629
9630 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9631
9632 /* Compute scroll margin height in pixels. We scroll when point is
9633 within this distance from the top or bottom of the window. */
9634 if (scroll_margin > 0)
9635 {
9636 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
9637 this_scroll_margin *= CANON_Y_UNIT (f);
9638 }
9639 else
9640 this_scroll_margin = 0;
9641
9642 /* Compute how much we should try to scroll maximally to bring point
9643 into view. */
9644 if (scroll_step || scroll_conservatively || temp_scroll_step)
9645 scroll_max = max (scroll_step,
9646 max (scroll_conservatively, temp_scroll_step));
9647 else if (NUMBERP (current_buffer->scroll_down_aggressively)
9648 || NUMBERP (current_buffer->scroll_up_aggressively))
9649 /* We're trying to scroll because of aggressive scrolling
9650 but no scroll_step is set. Choose an arbitrary one. Maybe
9651 there should be a variable for this. */
9652 scroll_max = 10;
9653 else
9654 scroll_max = 0;
9655 scroll_max *= CANON_Y_UNIT (f);
9656
9657 /* Decide whether we have to scroll down. Start at the window end
9658 and move this_scroll_margin up to find the position of the scroll
9659 margin. */
9660 window_end = Fwindow_end (window, Qt);
9661 CHARPOS (scroll_margin_pos) = XINT (window_end);
9662 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
9663 if (this_scroll_margin)
9664 {
9665 start_display (&it, w, scroll_margin_pos);
9666 move_it_vertically (&it, - this_scroll_margin);
9667 scroll_margin_pos = it.current.pos;
9668 }
9669
9670 if (PT >= CHARPOS (scroll_margin_pos))
9671 {
9672 int y0;
9673
9674 too_near_end:
9675 /* Point is in the scroll margin at the bottom of the window, or
9676 below. Compute a new window start that makes point visible. */
9677
9678 /* Compute the distance from the scroll margin to PT.
9679 Give up if the distance is greater than scroll_max. */
9680 start_display (&it, w, scroll_margin_pos);
9681 y0 = it.current_y;
9682 move_it_to (&it, PT, 0, it.last_visible_y, -1,
9683 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9684
9685 /* To make point visible, we have to move the window start
9686 down so that the line the cursor is in is visible, which
9687 means we have to add in the height of the cursor line. */
9688 dy = line_bottom_y (&it) - y0;
9689
9690 if (dy > scroll_max)
9691 return SCROLLING_FAILED;
9692
9693 /* Move the window start down. If scrolling conservatively,
9694 move it just enough down to make point visible. If
9695 scroll_step is set, move it down by scroll_step. */
9696 start_display (&it, w, startp);
9697
9698 if (scroll_conservatively)
9699 amount_to_scroll
9700 = max (max (dy, CANON_Y_UNIT (f)),
9701 CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
9702 else if (scroll_step || temp_scroll_step)
9703 amount_to_scroll = scroll_max;
9704 else
9705 {
9706 aggressive = current_buffer->scroll_up_aggressively;
9707 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
9708 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
9709 if (NUMBERP (aggressive))
9710 amount_to_scroll = XFLOATINT (aggressive) * height;
9711 }
9712
9713 if (amount_to_scroll <= 0)
9714 return SCROLLING_FAILED;
9715
9716 move_it_vertically (&it, amount_to_scroll);
9717 startp = it.current.pos;
9718 }
9719 else
9720 {
9721 /* See if point is inside the scroll margin at the top of the
9722 window. */
9723 scroll_margin_pos = startp;
9724 if (this_scroll_margin)
9725 {
9726 start_display (&it, w, startp);
9727 move_it_vertically (&it, this_scroll_margin);
9728 scroll_margin_pos = it.current.pos;
9729 }
9730
9731 if (PT < CHARPOS (scroll_margin_pos))
9732 {
9733 /* Point is in the scroll margin at the top of the window or
9734 above what is displayed in the window. */
9735 int y0;
9736
9737 /* Compute the vertical distance from PT to the scroll
9738 margin position. Give up if distance is greater than
9739 scroll_max. */
9740 SET_TEXT_POS (pos, PT, PT_BYTE);
9741 start_display (&it, w, pos);
9742 y0 = it.current_y;
9743 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
9744 it.last_visible_y, -1,
9745 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9746 dy = it.current_y - y0;
9747 if (dy > scroll_max)
9748 return SCROLLING_FAILED;
9749
9750 /* Compute new window start. */
9751 start_display (&it, w, startp);
9752
9753 if (scroll_conservatively)
9754 amount_to_scroll =
9755 max (dy, CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
9756 else if (scroll_step || temp_scroll_step)
9757 amount_to_scroll = scroll_max;
9758 else
9759 {
9760 aggressive = current_buffer->scroll_down_aggressively;
9761 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
9762 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
9763 if (NUMBERP (aggressive))
9764 amount_to_scroll = XFLOATINT (aggressive) * height;
9765 }
9766
9767 if (amount_to_scroll <= 0)
9768 return SCROLLING_FAILED;
9769
9770 move_it_vertically (&it, - amount_to_scroll);
9771 startp = it.current.pos;
9772 }
9773 }
9774
9775 /* Run window scroll functions. */
9776 startp = run_window_scroll_functions (window, startp);
9777
9778 /* Display the window. Give up if new fonts are loaded, or if point
9779 doesn't appear. */
9780 if (!try_window (window, startp))
9781 rc = SCROLLING_NEED_LARGER_MATRICES;
9782 else if (w->cursor.vpos < 0)
9783 {
9784 clear_glyph_matrix (w->desired_matrix);
9785 rc = SCROLLING_FAILED;
9786 }
9787 else
9788 {
9789 /* Maybe forget recorded base line for line number display. */
9790 if (!just_this_one_p
9791 || current_buffer->clip_changed
9792 || BEG_UNCHANGED < CHARPOS (startp))
9793 w->base_line_number = Qnil;
9794
9795 /* If cursor ends up on a partially visible line,
9796 treat that as being off the bottom of the screen. */
9797 if (! make_cursor_line_fully_visible (w))
9798 goto too_near_end;
9799 rc = SCROLLING_SUCCESS;
9800 }
9801
9802 return rc;
9803 }
9804
9805
9806 /* Compute a suitable window start for window W if display of W starts
9807 on a continuation line. Value is non-zero if a new window start
9808 was computed.
9809
9810 The new window start will be computed, based on W's width, starting
9811 from the start of the continued line. It is the start of the
9812 screen line with the minimum distance from the old start W->start. */
9813
9814 static int
9815 compute_window_start_on_continuation_line (w)
9816 struct window *w;
9817 {
9818 struct text_pos pos, start_pos;
9819 int window_start_changed_p = 0;
9820
9821 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
9822
9823 /* If window start is on a continuation line... Window start may be
9824 < BEGV in case there's invisible text at the start of the
9825 buffer (M-x rmail, for example). */
9826 if (CHARPOS (start_pos) > BEGV
9827 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
9828 {
9829 struct it it;
9830 struct glyph_row *row;
9831
9832 /* Handle the case that the window start is out of range. */
9833 if (CHARPOS (start_pos) < BEGV)
9834 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
9835 else if (CHARPOS (start_pos) > ZV)
9836 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
9837
9838 /* Find the start of the continued line. This should be fast
9839 because scan_buffer is fast (newline cache). */
9840 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
9841 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
9842 row, DEFAULT_FACE_ID);
9843 reseat_at_previous_visible_line_start (&it);
9844
9845 /* If the line start is "too far" away from the window start,
9846 say it takes too much time to compute a new window start. */
9847 if (CHARPOS (start_pos) - IT_CHARPOS (it)
9848 < XFASTINT (w->height) * XFASTINT (w->width))
9849 {
9850 int min_distance, distance;
9851
9852 /* Move forward by display lines to find the new window
9853 start. If window width was enlarged, the new start can
9854 be expected to be > the old start. If window width was
9855 decreased, the new window start will be < the old start.
9856 So, we're looking for the display line start with the
9857 minimum distance from the old window start. */
9858 pos = it.current.pos;
9859 min_distance = INFINITY;
9860 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
9861 distance < min_distance)
9862 {
9863 min_distance = distance;
9864 pos = it.current.pos;
9865 move_it_by_lines (&it, 1, 0);
9866 }
9867
9868 /* Set the window start there. */
9869 SET_MARKER_FROM_TEXT_POS (w->start, pos);
9870 window_start_changed_p = 1;
9871 }
9872 }
9873
9874 return window_start_changed_p;
9875 }
9876
9877
9878 /* Try cursor movement in case text has not changed in window WINDOW,
9879 with window start STARTP. Value is
9880
9881 CURSOR_MOVEMENT_SUCCESS if successful
9882
9883 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
9884
9885 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
9886 display. *SCROLL_STEP is set to 1, under certain circumstances, if
9887 we want to scroll as if scroll-step were set to 1. See the code.
9888
9889 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
9890 which case we have to abort this redisplay, and adjust matrices
9891 first. */
9892
9893 enum
9894 {
9895 CURSOR_MOVEMENT_SUCCESS,
9896 CURSOR_MOVEMENT_CANNOT_BE_USED,
9897 CURSOR_MOVEMENT_MUST_SCROLL,
9898 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
9899 };
9900
9901 static int
9902 try_cursor_movement (window, startp, scroll_step)
9903 Lisp_Object window;
9904 struct text_pos startp;
9905 int *scroll_step;
9906 {
9907 struct window *w = XWINDOW (window);
9908 struct frame *f = XFRAME (w->frame);
9909 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
9910
9911 #if GLYPH_DEBUG
9912 if (inhibit_try_cursor_movement)
9913 return rc;
9914 #endif
9915
9916 /* Handle case where text has not changed, only point, and it has
9917 not moved off the frame. */
9918 if (/* Point may be in this window. */
9919 PT >= CHARPOS (startp)
9920 /* Selective display hasn't changed. */
9921 && !current_buffer->clip_changed
9922 /* Function force-mode-line-update is used to force a thorough
9923 redisplay. It sets either windows_or_buffers_changed or
9924 update_mode_lines. So don't take a shortcut here for these
9925 cases. */
9926 && !update_mode_lines
9927 && !windows_or_buffers_changed
9928 && !cursor_type_changed
9929 /* Can't use this case if highlighting a region. When a
9930 region exists, cursor movement has to do more than just
9931 set the cursor. */
9932 && !(!NILP (Vtransient_mark_mode)
9933 && !NILP (current_buffer->mark_active))
9934 && NILP (w->region_showing)
9935 && NILP (Vshow_trailing_whitespace)
9936 /* Right after splitting windows, last_point may be nil. */
9937 && INTEGERP (w->last_point)
9938 /* This code is not used for mini-buffer for the sake of the case
9939 of redisplaying to replace an echo area message; since in
9940 that case the mini-buffer contents per se are usually
9941 unchanged. This code is of no real use in the mini-buffer
9942 since the handling of this_line_start_pos, etc., in redisplay
9943 handles the same cases. */
9944 && !EQ (window, minibuf_window)
9945 /* When splitting windows or for new windows, it happens that
9946 redisplay is called with a nil window_end_vpos or one being
9947 larger than the window. This should really be fixed in
9948 window.c. I don't have this on my list, now, so we do
9949 approximately the same as the old redisplay code. --gerd. */
9950 && INTEGERP (w->window_end_vpos)
9951 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
9952 && (FRAME_WINDOW_P (f)
9953 || !MARKERP (Voverlay_arrow_position)
9954 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
9955 {
9956 int this_scroll_margin;
9957 struct glyph_row *row = NULL;
9958
9959 #if GLYPH_DEBUG
9960 debug_method_add (w, "cursor movement");
9961 #endif
9962
9963 /* Scroll if point within this distance from the top or bottom
9964 of the window. This is a pixel value. */
9965 this_scroll_margin = max (0, scroll_margin);
9966 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
9967 this_scroll_margin *= CANON_Y_UNIT (f);
9968
9969 /* Start with the row the cursor was displayed during the last
9970 not paused redisplay. Give up if that row is not valid. */
9971 if (w->last_cursor.vpos < 0
9972 || w->last_cursor.vpos >= w->current_matrix->nrows)
9973 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9974 else
9975 {
9976 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
9977 if (row->mode_line_p)
9978 ++row;
9979 if (!row->enabled_p)
9980 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9981 }
9982
9983 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
9984 {
9985 int scroll_p = 0;
9986 int last_y = window_text_bottom_y (w) - this_scroll_margin;
9987
9988 if (PT > XFASTINT (w->last_point))
9989 {
9990 /* Point has moved forward. */
9991 while (MATRIX_ROW_END_CHARPOS (row) < PT
9992 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
9993 {
9994 xassert (row->enabled_p);
9995 ++row;
9996 }
9997
9998 /* The end position of a row equals the start position
9999 of the next row. If PT is there, we would rather
10000 display it in the next line. */
10001 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
10002 && MATRIX_ROW_END_CHARPOS (row) == PT
10003 && !cursor_row_p (w, row))
10004 ++row;
10005
10006 /* If within the scroll margin, scroll. Note that
10007 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
10008 the next line would be drawn, and that
10009 this_scroll_margin can be zero. */
10010 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
10011 || PT > MATRIX_ROW_END_CHARPOS (row)
10012 /* Line is completely visible last line in window
10013 and PT is to be set in the next line. */
10014 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
10015 && PT == MATRIX_ROW_END_CHARPOS (row)
10016 && !row->ends_at_zv_p
10017 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
10018 scroll_p = 1;
10019 }
10020 else if (PT < XFASTINT (w->last_point))
10021 {
10022 /* Cursor has to be moved backward. Note that PT >=
10023 CHARPOS (startp) because of the outer
10024 if-statement. */
10025 while (!row->mode_line_p
10026 && (MATRIX_ROW_START_CHARPOS (row) > PT
10027 || (MATRIX_ROW_START_CHARPOS (row) == PT
10028 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
10029 && (row->y > this_scroll_margin
10030 || CHARPOS (startp) == BEGV))
10031 {
10032 xassert (row->enabled_p);
10033 --row;
10034 }
10035
10036 /* Consider the following case: Window starts at BEGV,
10037 there is invisible, intangible text at BEGV, so that
10038 display starts at some point START > BEGV. It can
10039 happen that we are called with PT somewhere between
10040 BEGV and START. Try to handle that case. */
10041 if (row < w->current_matrix->rows
10042 || row->mode_line_p)
10043 {
10044 row = w->current_matrix->rows;
10045 if (row->mode_line_p)
10046 ++row;
10047 }
10048
10049 /* Due to newlines in overlay strings, we may have to
10050 skip forward over overlay strings. */
10051 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
10052 && MATRIX_ROW_END_CHARPOS (row) == PT
10053 && !cursor_row_p (w, row))
10054 ++row;
10055
10056 /* If within the scroll margin, scroll. */
10057 if (row->y < this_scroll_margin
10058 && CHARPOS (startp) != BEGV)
10059 scroll_p = 1;
10060 }
10061
10062 if (PT < MATRIX_ROW_START_CHARPOS (row)
10063 || PT > MATRIX_ROW_END_CHARPOS (row))
10064 {
10065 /* if PT is not in the glyph row, give up. */
10066 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10067 }
10068 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
10069 {
10070 if (PT == MATRIX_ROW_END_CHARPOS (row)
10071 && !row->ends_at_zv_p
10072 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
10073 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10074 else if (row->height > window_box_height (w))
10075 {
10076 /* If we end up in a partially visible line, let's
10077 make it fully visible, except when it's taller
10078 than the window, in which case we can't do much
10079 about it. */
10080 *scroll_step = 1;
10081 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10082 }
10083 else
10084 {
10085 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10086 try_window (window, startp);
10087 if (!make_cursor_line_fully_visible (w))
10088 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10089 else
10090 rc = CURSOR_MOVEMENT_SUCCESS;
10091 }
10092 }
10093 else if (scroll_p)
10094 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10095 else
10096 {
10097 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10098 rc = CURSOR_MOVEMENT_SUCCESS;
10099 }
10100 }
10101 }
10102
10103 return rc;
10104 }
10105
10106
10107 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
10108 selected_window is redisplayed. */
10109
10110 static void
10111 redisplay_window (window, just_this_one_p)
10112 Lisp_Object window;
10113 int just_this_one_p;
10114 {
10115 struct window *w = XWINDOW (window);
10116 struct frame *f = XFRAME (w->frame);
10117 struct buffer *buffer = XBUFFER (w->buffer);
10118 struct buffer *old = current_buffer;
10119 struct text_pos lpoint, opoint, startp;
10120 int update_mode_line;
10121 int tem;
10122 struct it it;
10123 /* Record it now because it's overwritten. */
10124 int current_matrix_up_to_date_p = 0;
10125 /* This is less strict than current_matrix_up_to_date_p.
10126 It indictes that the buffer contents and narrowing are unchanged. */
10127 int buffer_unchanged_p = 0;
10128 int temp_scroll_step = 0;
10129 int count = SPECPDL_INDEX ();
10130 int rc;
10131 int centering_position;
10132
10133 SET_TEXT_POS (lpoint, PT, PT_BYTE);
10134 opoint = lpoint;
10135
10136 /* W must be a leaf window here. */
10137 xassert (!NILP (w->buffer));
10138 #if GLYPH_DEBUG
10139 *w->desired_matrix->method = 0;
10140 #endif
10141
10142 specbind (Qinhibit_point_motion_hooks, Qt);
10143
10144 reconsider_clip_changes (w, buffer);
10145
10146 /* Has the mode line to be updated? */
10147 update_mode_line = (!NILP (w->update_mode_line)
10148 || update_mode_lines
10149 || buffer->clip_changed
10150 || buffer->prevent_redisplay_optimizations_p);
10151
10152 if (MINI_WINDOW_P (w))
10153 {
10154 if (w == XWINDOW (echo_area_window)
10155 && !NILP (echo_area_buffer[0]))
10156 {
10157 if (update_mode_line)
10158 /* We may have to update a tty frame's menu bar or a
10159 tool-bar. Example `M-x C-h C-h C-g'. */
10160 goto finish_menu_bars;
10161 else
10162 /* We've already displayed the echo area glyphs in this window. */
10163 goto finish_scroll_bars;
10164 }
10165 else if (w != XWINDOW (minibuf_window))
10166 {
10167 /* W is a mini-buffer window, but it's not the currently
10168 active one, so clear it. */
10169 int yb = window_text_bottom_y (w);
10170 struct glyph_row *row;
10171 int y;
10172
10173 for (y = 0, row = w->desired_matrix->rows;
10174 y < yb;
10175 y += row->height, ++row)
10176 blank_row (w, row, y);
10177 goto finish_scroll_bars;
10178 }
10179
10180 clear_glyph_matrix (w->desired_matrix);
10181 }
10182
10183 /* Otherwise set up data on this window; select its buffer and point
10184 value. */
10185 /* Really select the buffer, for the sake of buffer-local
10186 variables. */
10187 set_buffer_internal_1 (XBUFFER (w->buffer));
10188 SET_TEXT_POS (opoint, PT, PT_BYTE);
10189
10190 current_matrix_up_to_date_p
10191 = (!NILP (w->window_end_valid)
10192 && !current_buffer->clip_changed
10193 && !current_buffer->prevent_redisplay_optimizations_p
10194 && XFASTINT (w->last_modified) >= MODIFF
10195 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
10196
10197 buffer_unchanged_p
10198 = (!NILP (w->window_end_valid)
10199 && !current_buffer->clip_changed
10200 && XFASTINT (w->last_modified) >= MODIFF
10201 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
10202
10203 /* When windows_or_buffers_changed is non-zero, we can't rely on
10204 the window end being valid, so set it to nil there. */
10205 if (windows_or_buffers_changed)
10206 {
10207 /* If window starts on a continuation line, maybe adjust the
10208 window start in case the window's width changed. */
10209 if (XMARKER (w->start)->buffer == current_buffer)
10210 compute_window_start_on_continuation_line (w);
10211
10212 w->window_end_valid = Qnil;
10213 }
10214
10215 /* Some sanity checks. */
10216 CHECK_WINDOW_END (w);
10217 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
10218 abort ();
10219 if (BYTEPOS (opoint) < CHARPOS (opoint))
10220 abort ();
10221
10222 /* If %c is in mode line, update it if needed. */
10223 if (!NILP (w->column_number_displayed)
10224 /* This alternative quickly identifies a common case
10225 where no change is needed. */
10226 && !(PT == XFASTINT (w->last_point)
10227 && XFASTINT (w->last_modified) >= MODIFF
10228 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
10229 && (XFASTINT (w->column_number_displayed)
10230 != (int) current_column ())) /* iftc */
10231 update_mode_line = 1;
10232
10233 /* Count number of windows showing the selected buffer. An indirect
10234 buffer counts as its base buffer. */
10235 if (!just_this_one_p)
10236 {
10237 struct buffer *current_base, *window_base;
10238 current_base = current_buffer;
10239 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
10240 if (current_base->base_buffer)
10241 current_base = current_base->base_buffer;
10242 if (window_base->base_buffer)
10243 window_base = window_base->base_buffer;
10244 if (current_base == window_base)
10245 buffer_shared++;
10246 }
10247
10248 /* Point refers normally to the selected window. For any other
10249 window, set up appropriate value. */
10250 if (!EQ (window, selected_window))
10251 {
10252 int new_pt = XMARKER (w->pointm)->charpos;
10253 int new_pt_byte = marker_byte_position (w->pointm);
10254 if (new_pt < BEGV)
10255 {
10256 new_pt = BEGV;
10257 new_pt_byte = BEGV_BYTE;
10258 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
10259 }
10260 else if (new_pt > (ZV - 1))
10261 {
10262 new_pt = ZV;
10263 new_pt_byte = ZV_BYTE;
10264 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
10265 }
10266
10267 /* We don't use SET_PT so that the point-motion hooks don't run. */
10268 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
10269 }
10270
10271 /* If any of the character widths specified in the display table
10272 have changed, invalidate the width run cache. It's true that
10273 this may be a bit late to catch such changes, but the rest of
10274 redisplay goes (non-fatally) haywire when the display table is
10275 changed, so why should we worry about doing any better? */
10276 if (current_buffer->width_run_cache)
10277 {
10278 struct Lisp_Char_Table *disptab = buffer_display_table ();
10279
10280 if (! disptab_matches_widthtab (disptab,
10281 XVECTOR (current_buffer->width_table)))
10282 {
10283 invalidate_region_cache (current_buffer,
10284 current_buffer->width_run_cache,
10285 BEG, Z);
10286 recompute_width_table (current_buffer, disptab);
10287 }
10288 }
10289
10290 /* If window-start is screwed up, choose a new one. */
10291 if (XMARKER (w->start)->buffer != current_buffer)
10292 goto recenter;
10293
10294 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10295
10296 /* If someone specified a new starting point but did not insist,
10297 check whether it can be used. */
10298 if (!NILP (w->optional_new_start)
10299 && CHARPOS (startp) >= BEGV
10300 && CHARPOS (startp) <= ZV)
10301 {
10302 w->optional_new_start = Qnil;
10303 start_display (&it, w, startp);
10304 move_it_to (&it, PT, 0, it.last_visible_y, -1,
10305 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
10306 if (IT_CHARPOS (it) == PT)
10307 w->force_start = Qt;
10308 }
10309
10310 /* Handle case where place to start displaying has been specified,
10311 unless the specified location is outside the accessible range. */
10312 if (!NILP (w->force_start)
10313 || w->frozen_window_start_p)
10314 {
10315 w->force_start = Qnil;
10316 w->vscroll = 0;
10317 w->window_end_valid = Qnil;
10318
10319 /* Forget any recorded base line for line number display. */
10320 if (!buffer_unchanged_p)
10321 w->base_line_number = Qnil;
10322
10323 /* Redisplay the mode line. Select the buffer properly for that.
10324 Also, run the hook window-scroll-functions
10325 because we have scrolled. */
10326 /* Note, we do this after clearing force_start because
10327 if there's an error, it is better to forget about force_start
10328 than to get into an infinite loop calling the hook functions
10329 and having them get more errors. */
10330 if (!update_mode_line
10331 || ! NILP (Vwindow_scroll_functions))
10332 {
10333 update_mode_line = 1;
10334 w->update_mode_line = Qt;
10335 startp = run_window_scroll_functions (window, startp);
10336 }
10337
10338 w->last_modified = make_number (0);
10339 w->last_overlay_modified = make_number (0);
10340 if (CHARPOS (startp) < BEGV)
10341 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
10342 else if (CHARPOS (startp) > ZV)
10343 SET_TEXT_POS (startp, ZV, ZV_BYTE);
10344
10345 /* Redisplay, then check if cursor has been set during the
10346 redisplay. Give up if new fonts were loaded. */
10347 if (!try_window (window, startp))
10348 {
10349 w->force_start = Qt;
10350 clear_glyph_matrix (w->desired_matrix);
10351 goto finish_scroll_bars;
10352 }
10353
10354 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
10355 {
10356 /* If point does not appear, try to move point so it does
10357 appear. The desired matrix has been built above, so we
10358 can use it here. */
10359 int window_height;
10360 struct glyph_row *row;
10361
10362 window_height = window_box_height (w) / 2;
10363 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
10364 while (MATRIX_ROW_BOTTOM_Y (row) < window_height)
10365 ++row;
10366
10367 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
10368 MATRIX_ROW_START_BYTEPOS (row));
10369
10370 if (w != XWINDOW (selected_window))
10371 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
10372 else if (current_buffer == old)
10373 SET_TEXT_POS (lpoint, PT, PT_BYTE);
10374
10375 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
10376
10377 /* If we are highlighting the region, then we just changed
10378 the region, so redisplay to show it. */
10379 if (!NILP (Vtransient_mark_mode)
10380 && !NILP (current_buffer->mark_active))
10381 {
10382 clear_glyph_matrix (w->desired_matrix);
10383 if (!try_window (window, startp))
10384 goto need_larger_matrices;
10385 }
10386 }
10387
10388 if (!make_cursor_line_fully_visible (w))
10389 {
10390 /* CVS rev. 1.761 had changed this to ``goto try_to_scroll''.
10391
10392 The intention of the fix -- AFAIU -- was to ensure that
10393 the cursor didn't end up on a partially visible last (or
10394 first?) line when scrolling.
10395
10396
10397 But that change causes havoc when scrolling backwards and
10398 a partially visible first (or last?) line is present when
10399 we reach the top of the buffer. In effect, the text
10400 already in the window is repeated (each line is appended
10401 to the same or another lines in the window)...
10402
10403 I changed it back to ``goto need_larger_matrices'' which
10404 in effect mean that we don't go through `try_scrolling'
10405 when the cursor is already at the first line of the buffer,
10406 and there is really only a few pixels [rather than lines]
10407 to scroll backwards. I guess move_it_by_lines etc. really
10408 isn't the right device for doing that, ref. the code in
10409 make_cursor_line_fully_visible which was also disabled by
10410 CVS rev. 1.761.
10411
10412 But how do we know that we are already on the top line of
10413 the window showing the first line in the buffer, so that
10414 scrolling really wont help here?
10415
10416 I cannot find a simple fix for this (I tried various
10417 approaches), but I prefer to an occasional partial line
10418 rather than the visual messup, so I reverted this part of
10419 the fix.
10420
10421 Someone will need to look into this when time allows.
10422
10423 -- 2002-08-22, Kim F. Storm */
10424
10425 goto need_larger_matrices;
10426 }
10427 #if GLYPH_DEBUG
10428 debug_method_add (w, "forced window start");
10429 #endif
10430 goto done;
10431 }
10432
10433 /* Handle case where text has not changed, only point, and it has
10434 not moved off the frame, and we are not retrying after hscroll.
10435 (current_matrix_up_to_date_p is nonzero when retrying.) */
10436 if (current_matrix_up_to_date_p
10437 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
10438 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
10439 {
10440 switch (rc)
10441 {
10442 case CURSOR_MOVEMENT_SUCCESS:
10443 goto done;
10444
10445 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
10446 goto need_larger_matrices;
10447
10448 case CURSOR_MOVEMENT_MUST_SCROLL:
10449 goto try_to_scroll;
10450
10451 default:
10452 abort ();
10453 }
10454 }
10455 /* If current starting point was originally the beginning of a line
10456 but no longer is, find a new starting point. */
10457 else if (!NILP (w->start_at_line_beg)
10458 && !(CHARPOS (startp) <= BEGV
10459 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
10460 {
10461 #if GLYPH_DEBUG
10462 debug_method_add (w, "recenter 1");
10463 #endif
10464 goto recenter;
10465 }
10466
10467 /* Try scrolling with try_window_id. Value is > 0 if update has
10468 been done, it is -1 if we know that the same window start will
10469 not work. It is 0 if unsuccessful for some other reason. */
10470 else if ((tem = try_window_id (w)) != 0)
10471 {
10472 #if GLYPH_DEBUG
10473 debug_method_add (w, "try_window_id %d", tem);
10474 #endif
10475
10476 if (fonts_changed_p)
10477 goto need_larger_matrices;
10478 if (tem > 0)
10479 goto done;
10480
10481 /* Otherwise try_window_id has returned -1 which means that we
10482 don't want the alternative below this comment to execute. */
10483 }
10484 else if (CHARPOS (startp) >= BEGV
10485 && CHARPOS (startp) <= ZV
10486 && PT >= CHARPOS (startp)
10487 && (CHARPOS (startp) < ZV
10488 /* Avoid starting at end of buffer. */
10489 || CHARPOS (startp) == BEGV
10490 || (XFASTINT (w->last_modified) >= MODIFF
10491 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
10492 {
10493 #if GLYPH_DEBUG
10494 debug_method_add (w, "same window start");
10495 #endif
10496
10497 /* Try to redisplay starting at same place as before.
10498 If point has not moved off frame, accept the results. */
10499 if (!current_matrix_up_to_date_p
10500 /* Don't use try_window_reusing_current_matrix in this case
10501 because a window scroll function can have changed the
10502 buffer. */
10503 || !NILP (Vwindow_scroll_functions)
10504 || MINI_WINDOW_P (w)
10505 || !try_window_reusing_current_matrix (w))
10506 {
10507 IF_DEBUG (debug_method_add (w, "1"));
10508 try_window (window, startp);
10509 }
10510
10511 if (fonts_changed_p)
10512 goto need_larger_matrices;
10513
10514 if (w->cursor.vpos >= 0)
10515 {
10516 if (!just_this_one_p
10517 || current_buffer->clip_changed
10518 || BEG_UNCHANGED < CHARPOS (startp))
10519 /* Forget any recorded base line for line number display. */
10520 w->base_line_number = Qnil;
10521
10522 if (!make_cursor_line_fully_visible (w))
10523 /* Drop through and scroll. */
10524 ;
10525 goto done;
10526 }
10527 else
10528 clear_glyph_matrix (w->desired_matrix);
10529 }
10530
10531 try_to_scroll:
10532
10533 w->last_modified = make_number (0);
10534 w->last_overlay_modified = make_number (0);
10535
10536 /* Redisplay the mode line. Select the buffer properly for that. */
10537 if (!update_mode_line)
10538 {
10539 update_mode_line = 1;
10540 w->update_mode_line = Qt;
10541 }
10542
10543 /* Try to scroll by specified few lines. */
10544 if ((scroll_conservatively
10545 || scroll_step
10546 || temp_scroll_step
10547 || NUMBERP (current_buffer->scroll_up_aggressively)
10548 || NUMBERP (current_buffer->scroll_down_aggressively))
10549 && !current_buffer->clip_changed
10550 && CHARPOS (startp) >= BEGV
10551 && CHARPOS (startp) <= ZV)
10552 {
10553 /* The function returns -1 if new fonts were loaded, 1 if
10554 successful, 0 if not successful. */
10555 int rc = try_scrolling (window, just_this_one_p,
10556 scroll_conservatively,
10557 scroll_step,
10558 temp_scroll_step);
10559 switch (rc)
10560 {
10561 case SCROLLING_SUCCESS:
10562 goto done;
10563
10564 case SCROLLING_NEED_LARGER_MATRICES:
10565 goto need_larger_matrices;
10566
10567 case SCROLLING_FAILED:
10568 break;
10569
10570 default:
10571 abort ();
10572 }
10573 }
10574
10575 /* Finally, just choose place to start which centers point */
10576
10577 recenter:
10578 centering_position = window_box_height (w) / 2;
10579
10580 point_at_top:
10581 /* Jump here with centering_position already set to 0. */
10582
10583 #if GLYPH_DEBUG
10584 debug_method_add (w, "recenter");
10585 #endif
10586
10587 /* w->vscroll = 0; */
10588
10589 /* Forget any previously recorded base line for line number display. */
10590 if (!buffer_unchanged_p)
10591 w->base_line_number = Qnil;
10592
10593 /* Move backward half the height of the window. */
10594 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10595 it.current_y = it.last_visible_y;
10596 move_it_vertically_backward (&it, centering_position);
10597 xassert (IT_CHARPOS (it) >= BEGV);
10598
10599 /* The function move_it_vertically_backward may move over more
10600 than the specified y-distance. If it->w is small, e.g. a
10601 mini-buffer window, we may end up in front of the window's
10602 display area. Start displaying at the start of the line
10603 containing PT in this case. */
10604 if (it.current_y <= 0)
10605 {
10606 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10607 move_it_vertically (&it, 0);
10608 xassert (IT_CHARPOS (it) <= PT);
10609 it.current_y = 0;
10610 }
10611
10612 it.current_x = it.hpos = 0;
10613
10614 /* Set startp here explicitly in case that helps avoid an infinite loop
10615 in case the window-scroll-functions functions get errors. */
10616 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
10617
10618 /* Run scroll hooks. */
10619 startp = run_window_scroll_functions (window, it.current.pos);
10620
10621 /* Redisplay the window. */
10622 if (!current_matrix_up_to_date_p
10623 || windows_or_buffers_changed
10624 || cursor_type_changed
10625 /* Don't use try_window_reusing_current_matrix in this case
10626 because it can have changed the buffer. */
10627 || !NILP (Vwindow_scroll_functions)
10628 || !just_this_one_p
10629 || MINI_WINDOW_P (w)
10630 || !try_window_reusing_current_matrix (w))
10631 try_window (window, startp);
10632
10633 /* If new fonts have been loaded (due to fontsets), give up. We
10634 have to start a new redisplay since we need to re-adjust glyph
10635 matrices. */
10636 if (fonts_changed_p)
10637 goto need_larger_matrices;
10638
10639 /* If cursor did not appear assume that the middle of the window is
10640 in the first line of the window. Do it again with the next line.
10641 (Imagine a window of height 100, displaying two lines of height
10642 60. Moving back 50 from it->last_visible_y will end in the first
10643 line.) */
10644 if (w->cursor.vpos < 0)
10645 {
10646 if (!NILP (w->window_end_valid)
10647 && PT >= Z - XFASTINT (w->window_end_pos))
10648 {
10649 clear_glyph_matrix (w->desired_matrix);
10650 move_it_by_lines (&it, 1, 0);
10651 try_window (window, it.current.pos);
10652 }
10653 else if (PT < IT_CHARPOS (it))
10654 {
10655 clear_glyph_matrix (w->desired_matrix);
10656 move_it_by_lines (&it, -1, 0);
10657 try_window (window, it.current.pos);
10658 }
10659 else
10660 {
10661 /* Not much we can do about it. */
10662 }
10663 }
10664
10665 /* Consider the following case: Window starts at BEGV, there is
10666 invisible, intangible text at BEGV, so that display starts at
10667 some point START > BEGV. It can happen that we are called with
10668 PT somewhere between BEGV and START. Try to handle that case. */
10669 if (w->cursor.vpos < 0)
10670 {
10671 struct glyph_row *row = w->current_matrix->rows;
10672 if (row->mode_line_p)
10673 ++row;
10674 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10675 }
10676
10677 if (!make_cursor_line_fully_visible (w))
10678 {
10679 /* If centering point failed to make the whole line visible,
10680 put point at the top instead. That has to make the whole line
10681 visible, if it can be done. */
10682 centering_position = 0;
10683 goto point_at_top;
10684 }
10685
10686 done:
10687
10688 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10689 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
10690 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
10691 ? Qt : Qnil);
10692
10693 /* Display the mode line, if we must. */
10694 if ((update_mode_line
10695 /* If window not full width, must redo its mode line
10696 if (a) the window to its side is being redone and
10697 (b) we do a frame-based redisplay. This is a consequence
10698 of how inverted lines are drawn in frame-based redisplay. */
10699 || (!just_this_one_p
10700 && !FRAME_WINDOW_P (f)
10701 && !WINDOW_FULL_WIDTH_P (w))
10702 /* Line number to display. */
10703 || INTEGERP (w->base_line_pos)
10704 /* Column number is displayed and different from the one displayed. */
10705 || (!NILP (w->column_number_displayed)
10706 && (XFASTINT (w->column_number_displayed)
10707 != (int) current_column ()))) /* iftc */
10708 /* This means that the window has a mode line. */
10709 && (WINDOW_WANTS_MODELINE_P (w)
10710 || WINDOW_WANTS_HEADER_LINE_P (w)))
10711 {
10712 display_mode_lines (w);
10713
10714 /* If mode line height has changed, arrange for a thorough
10715 immediate redisplay using the correct mode line height. */
10716 if (WINDOW_WANTS_MODELINE_P (w)
10717 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
10718 {
10719 fonts_changed_p = 1;
10720 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
10721 = DESIRED_MODE_LINE_HEIGHT (w);
10722 }
10723
10724 /* If top line height has changed, arrange for a thorough
10725 immediate redisplay using the correct mode line height. */
10726 if (WINDOW_WANTS_HEADER_LINE_P (w)
10727 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
10728 {
10729 fonts_changed_p = 1;
10730 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
10731 = DESIRED_HEADER_LINE_HEIGHT (w);
10732 }
10733
10734 if (fonts_changed_p)
10735 goto need_larger_matrices;
10736 }
10737
10738 if (!line_number_displayed
10739 && !BUFFERP (w->base_line_pos))
10740 {
10741 w->base_line_pos = Qnil;
10742 w->base_line_number = Qnil;
10743 }
10744
10745 finish_menu_bars:
10746
10747 /* When we reach a frame's selected window, redo the frame's menu bar. */
10748 if (update_mode_line
10749 && EQ (FRAME_SELECTED_WINDOW (f), window))
10750 {
10751 int redisplay_menu_p = 0;
10752
10753 if (FRAME_WINDOW_P (f))
10754 {
10755 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS)
10756 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
10757 #else
10758 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10759 #endif
10760 }
10761 else
10762 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10763
10764 if (redisplay_menu_p)
10765 display_menu_bar (w);
10766
10767 #ifdef HAVE_WINDOW_SYSTEM
10768 if (WINDOWP (f->tool_bar_window)
10769 && (FRAME_TOOL_BAR_LINES (f) > 0
10770 || auto_resize_tool_bars_p))
10771 redisplay_tool_bar (f);
10772 #endif
10773 }
10774
10775 need_larger_matrices:
10776 ;
10777 finish_scroll_bars:
10778
10779 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
10780 {
10781 int start, end, whole;
10782
10783 /* Calculate the start and end positions for the current window.
10784 At some point, it would be nice to choose between scrollbars
10785 which reflect the whole buffer size, with special markers
10786 indicating narrowing, and scrollbars which reflect only the
10787 visible region.
10788
10789 Note that mini-buffers sometimes aren't displaying any text. */
10790 if (!MINI_WINDOW_P (w)
10791 || (w == XWINDOW (minibuf_window)
10792 && NILP (echo_area_buffer[0])))
10793 {
10794 whole = ZV - BEGV;
10795 start = marker_position (w->start) - BEGV;
10796 /* I don't think this is guaranteed to be right. For the
10797 moment, we'll pretend it is. */
10798 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
10799
10800 if (end < start)
10801 end = start;
10802 if (whole < (end - start))
10803 whole = end - start;
10804 }
10805 else
10806 start = end = whole = 0;
10807
10808 /* Indicate what this scroll bar ought to be displaying now. */
10809 set_vertical_scroll_bar_hook (w, end - start, whole, start);
10810
10811 /* Note that we actually used the scroll bar attached to this
10812 window, so it shouldn't be deleted at the end of redisplay. */
10813 redeem_scroll_bar_hook (w);
10814 }
10815
10816 /* Restore current_buffer and value of point in it. */
10817 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
10818 set_buffer_internal_1 (old);
10819 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
10820
10821 unbind_to (count, Qnil);
10822 }
10823
10824
10825 /* Build the complete desired matrix of WINDOW with a window start
10826 buffer position POS. Value is non-zero if successful. It is zero
10827 if fonts were loaded during redisplay which makes re-adjusting
10828 glyph matrices necessary. */
10829
10830 int
10831 try_window (window, pos)
10832 Lisp_Object window;
10833 struct text_pos pos;
10834 {
10835 struct window *w = XWINDOW (window);
10836 struct it it;
10837 struct glyph_row *last_text_row = NULL;
10838
10839 /* Make POS the new window start. */
10840 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
10841
10842 /* Mark cursor position as unknown. No overlay arrow seen. */
10843 w->cursor.vpos = -1;
10844 overlay_arrow_seen = 0;
10845
10846 /* Initialize iterator and info to start at POS. */
10847 start_display (&it, w, pos);
10848
10849 /* Display all lines of W. */
10850 while (it.current_y < it.last_visible_y)
10851 {
10852 if (display_line (&it))
10853 last_text_row = it.glyph_row - 1;
10854 if (fonts_changed_p)
10855 return 0;
10856 }
10857
10858 /* If bottom moved off end of frame, change mode line percentage. */
10859 if (XFASTINT (w->window_end_pos) <= 0
10860 && Z != IT_CHARPOS (it))
10861 w->update_mode_line = Qt;
10862
10863 /* Set window_end_pos to the offset of the last character displayed
10864 on the window from the end of current_buffer. Set
10865 window_end_vpos to its row number. */
10866 if (last_text_row)
10867 {
10868 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
10869 w->window_end_bytepos
10870 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10871 w->window_end_pos
10872 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10873 w->window_end_vpos
10874 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10875 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
10876 ->displays_text_p);
10877 }
10878 else
10879 {
10880 w->window_end_bytepos = 0;
10881 w->window_end_pos = w->window_end_vpos = make_number (0);
10882 }
10883
10884 /* But that is not valid info until redisplay finishes. */
10885 w->window_end_valid = Qnil;
10886 return 1;
10887 }
10888
10889
10890 \f
10891 /************************************************************************
10892 Window redisplay reusing current matrix when buffer has not changed
10893 ************************************************************************/
10894
10895 /* Try redisplay of window W showing an unchanged buffer with a
10896 different window start than the last time it was displayed by
10897 reusing its current matrix. Value is non-zero if successful.
10898 W->start is the new window start. */
10899
10900 static int
10901 try_window_reusing_current_matrix (w)
10902 struct window *w;
10903 {
10904 struct frame *f = XFRAME (w->frame);
10905 struct glyph_row *row, *bottom_row;
10906 struct it it;
10907 struct run run;
10908 struct text_pos start, new_start;
10909 int nrows_scrolled, i;
10910 struct glyph_row *last_text_row;
10911 struct glyph_row *last_reused_text_row;
10912 struct glyph_row *start_row;
10913 int start_vpos, min_y, max_y;
10914
10915 #if GLYPH_DEBUG
10916 if (inhibit_try_window_reusing)
10917 return 0;
10918 #endif
10919
10920 if (/* This function doesn't handle terminal frames. */
10921 !FRAME_WINDOW_P (f)
10922 /* Don't try to reuse the display if windows have been split
10923 or such. */
10924 || windows_or_buffers_changed
10925 || cursor_type_changed)
10926 return 0;
10927
10928 /* Can't do this if region may have changed. */
10929 if ((!NILP (Vtransient_mark_mode)
10930 && !NILP (current_buffer->mark_active))
10931 || !NILP (w->region_showing)
10932 || !NILP (Vshow_trailing_whitespace))
10933 return 0;
10934
10935 /* If top-line visibility has changed, give up. */
10936 if (WINDOW_WANTS_HEADER_LINE_P (w)
10937 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
10938 return 0;
10939
10940 /* Give up if old or new display is scrolled vertically. We could
10941 make this function handle this, but right now it doesn't. */
10942 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10943 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
10944 return 0;
10945
10946 /* The variable new_start now holds the new window start. The old
10947 start `start' can be determined from the current matrix. */
10948 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
10949 start = start_row->start.pos;
10950 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
10951
10952 /* Clear the desired matrix for the display below. */
10953 clear_glyph_matrix (w->desired_matrix);
10954
10955 if (CHARPOS (new_start) <= CHARPOS (start))
10956 {
10957 int first_row_y;
10958
10959 /* Don't use this method if the display starts with an ellipsis
10960 displayed for invisible text. It's not easy to handle that case
10961 below, and it's certainly not worth the effort since this is
10962 not a frequent case. */
10963 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
10964 return 0;
10965
10966 IF_DEBUG (debug_method_add (w, "twu1"));
10967
10968 /* Display up to a row that can be reused. The variable
10969 last_text_row is set to the last row displayed that displays
10970 text. Note that it.vpos == 0 if or if not there is a
10971 header-line; it's not the same as the MATRIX_ROW_VPOS! */
10972 start_display (&it, w, new_start);
10973 first_row_y = it.current_y;
10974 w->cursor.vpos = -1;
10975 last_text_row = last_reused_text_row = NULL;
10976
10977 while (it.current_y < it.last_visible_y
10978 && IT_CHARPOS (it) < CHARPOS (start)
10979 && !fonts_changed_p)
10980 if (display_line (&it))
10981 last_text_row = it.glyph_row - 1;
10982
10983 /* A value of current_y < last_visible_y means that we stopped
10984 at the previous window start, which in turn means that we
10985 have at least one reusable row. */
10986 if (it.current_y < it.last_visible_y)
10987 {
10988 /* IT.vpos always starts from 0; it counts text lines. */
10989 nrows_scrolled = it.vpos;
10990
10991 /* Find PT if not already found in the lines displayed. */
10992 if (w->cursor.vpos < 0)
10993 {
10994 int dy = it.current_y - first_row_y;
10995
10996 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10997 row = row_containing_pos (w, PT, row, NULL, dy);
10998 if (row)
10999 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
11000 dy, nrows_scrolled);
11001 else
11002 {
11003 clear_glyph_matrix (w->desired_matrix);
11004 return 0;
11005 }
11006 }
11007
11008 /* Scroll the display. Do it before the current matrix is
11009 changed. The problem here is that update has not yet
11010 run, i.e. part of the current matrix is not up to date.
11011 scroll_run_hook will clear the cursor, and use the
11012 current matrix to get the height of the row the cursor is
11013 in. */
11014 run.current_y = first_row_y;
11015 run.desired_y = it.current_y;
11016 run.height = it.last_visible_y - it.current_y;
11017
11018 if (run.height > 0 && run.current_y != run.desired_y)
11019 {
11020 update_begin (f);
11021 rif->update_window_begin_hook (w);
11022 rif->clear_mouse_face (w);
11023 rif->scroll_run_hook (w, &run);
11024 rif->update_window_end_hook (w, 0, 0);
11025 update_end (f);
11026 }
11027
11028 /* Shift current matrix down by nrows_scrolled lines. */
11029 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
11030 rotate_matrix (w->current_matrix,
11031 start_vpos,
11032 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
11033 nrows_scrolled);
11034
11035 /* Disable lines that must be updated. */
11036 for (i = 0; i < it.vpos; ++i)
11037 (start_row + i)->enabled_p = 0;
11038
11039 /* Re-compute Y positions. */
11040 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
11041 max_y = it.last_visible_y;
11042 for (row = start_row + nrows_scrolled;
11043 row < bottom_row;
11044 ++row)
11045 {
11046 row->y = it.current_y;
11047 row->visible_height = row->height;
11048
11049 if (row->y < min_y)
11050 row->visible_height -= min_y - row->y;
11051 if (row->y + row->height > max_y)
11052 row->visible_height -= row->y + row->height - max_y;
11053
11054 it.current_y += row->height;
11055
11056 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
11057 last_reused_text_row = row;
11058 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
11059 break;
11060 }
11061
11062 /* Disable lines in the current matrix which are now
11063 below the window. */
11064 for (++row; row < bottom_row; ++row)
11065 row->enabled_p = 0;
11066 }
11067
11068 /* Update window_end_pos etc.; last_reused_text_row is the last
11069 reused row from the current matrix containing text, if any.
11070 The value of last_text_row is the last displayed line
11071 containing text. */
11072 if (last_reused_text_row)
11073 {
11074 w->window_end_bytepos
11075 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
11076 w->window_end_pos
11077 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
11078 w->window_end_vpos
11079 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
11080 w->current_matrix));
11081 }
11082 else if (last_text_row)
11083 {
11084 w->window_end_bytepos
11085 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
11086 w->window_end_pos
11087 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
11088 w->window_end_vpos
11089 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
11090 }
11091 else
11092 {
11093 /* This window must be completely empty. */
11094 w->window_end_bytepos = 0;
11095 w->window_end_pos = w->window_end_vpos = make_number (0);
11096 }
11097 w->window_end_valid = Qnil;
11098
11099 /* Update hint: don't try scrolling again in update_window. */
11100 w->desired_matrix->no_scrolling_p = 1;
11101
11102 #if GLYPH_DEBUG
11103 debug_method_add (w, "try_window_reusing_current_matrix 1");
11104 #endif
11105 return 1;
11106 }
11107 else if (CHARPOS (new_start) > CHARPOS (start))
11108 {
11109 struct glyph_row *pt_row, *row;
11110 struct glyph_row *first_reusable_row;
11111 struct glyph_row *first_row_to_display;
11112 int dy;
11113 int yb = window_text_bottom_y (w);
11114
11115 /* Find the row starting at new_start, if there is one. Don't
11116 reuse a partially visible line at the end. */
11117 first_reusable_row = start_row;
11118 while (first_reusable_row->enabled_p
11119 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
11120 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
11121 < CHARPOS (new_start)))
11122 ++first_reusable_row;
11123
11124 /* Give up if there is no row to reuse. */
11125 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
11126 || !first_reusable_row->enabled_p
11127 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
11128 != CHARPOS (new_start)))
11129 return 0;
11130
11131 /* We can reuse fully visible rows beginning with
11132 first_reusable_row to the end of the window. Set
11133 first_row_to_display to the first row that cannot be reused.
11134 Set pt_row to the row containing point, if there is any. */
11135 pt_row = NULL;
11136 for (first_row_to_display = first_reusable_row;
11137 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
11138 ++first_row_to_display)
11139 {
11140 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
11141 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
11142 pt_row = first_row_to_display;
11143 }
11144
11145 /* Start displaying at the start of first_row_to_display. */
11146 xassert (first_row_to_display->y < yb);
11147 init_to_row_start (&it, w, first_row_to_display);
11148
11149 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
11150 - start_vpos);
11151 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
11152 - nrows_scrolled);
11153 it.current_y = (first_row_to_display->y - first_reusable_row->y
11154 + WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
11155
11156 /* Display lines beginning with first_row_to_display in the
11157 desired matrix. Set last_text_row to the last row displayed
11158 that displays text. */
11159 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
11160 if (pt_row == NULL)
11161 w->cursor.vpos = -1;
11162 last_text_row = NULL;
11163 while (it.current_y < it.last_visible_y && !fonts_changed_p)
11164 if (display_line (&it))
11165 last_text_row = it.glyph_row - 1;
11166
11167 /* Give up If point isn't in a row displayed or reused. */
11168 if (w->cursor.vpos < 0)
11169 {
11170 clear_glyph_matrix (w->desired_matrix);
11171 return 0;
11172 }
11173
11174 /* If point is in a reused row, adjust y and vpos of the cursor
11175 position. */
11176 if (pt_row)
11177 {
11178 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
11179 w->current_matrix);
11180 w->cursor.y -= first_reusable_row->y;
11181 }
11182
11183 /* Scroll the display. */
11184 run.current_y = first_reusable_row->y;
11185 run.desired_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
11186 run.height = it.last_visible_y - run.current_y;
11187 dy = run.current_y - run.desired_y;
11188
11189 if (run.height)
11190 {
11191 struct frame *f = XFRAME (WINDOW_FRAME (w));
11192 update_begin (f);
11193 rif->update_window_begin_hook (w);
11194 rif->clear_mouse_face (w);
11195 rif->scroll_run_hook (w, &run);
11196 rif->update_window_end_hook (w, 0, 0);
11197 update_end (f);
11198 }
11199
11200 /* Adjust Y positions of reused rows. */
11201 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
11202 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
11203 max_y = it.last_visible_y;
11204 for (row = first_reusable_row; row < first_row_to_display; ++row)
11205 {
11206 row->y -= dy;
11207 row->visible_height = row->height;
11208 if (row->y < min_y)
11209 row->visible_height -= min_y - row->y;
11210 if (row->y + row->height > max_y)
11211 row->visible_height -= row->y + row->height - max_y;
11212 }
11213
11214 /* Scroll the current matrix. */
11215 xassert (nrows_scrolled > 0);
11216 rotate_matrix (w->current_matrix,
11217 start_vpos,
11218 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
11219 -nrows_scrolled);
11220
11221 /* Disable rows not reused. */
11222 for (row -= nrows_scrolled; row < bottom_row; ++row)
11223 row->enabled_p = 0;
11224
11225 /* Adjust window end. A null value of last_text_row means that
11226 the window end is in reused rows which in turn means that
11227 only its vpos can have changed. */
11228 if (last_text_row)
11229 {
11230 w->window_end_bytepos
11231 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
11232 w->window_end_pos
11233 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
11234 w->window_end_vpos
11235 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
11236 }
11237 else
11238 {
11239 w->window_end_vpos
11240 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
11241 }
11242
11243 w->window_end_valid = Qnil;
11244 w->desired_matrix->no_scrolling_p = 1;
11245
11246 #if GLYPH_DEBUG
11247 debug_method_add (w, "try_window_reusing_current_matrix 2");
11248 #endif
11249 return 1;
11250 }
11251
11252 return 0;
11253 }
11254
11255
11256 \f
11257 /************************************************************************
11258 Window redisplay reusing current matrix when buffer has changed
11259 ************************************************************************/
11260
11261 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
11262 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
11263 int *, int *));
11264 static struct glyph_row *
11265 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
11266 struct glyph_row *));
11267
11268
11269 /* Return the last row in MATRIX displaying text. If row START is
11270 non-null, start searching with that row. IT gives the dimensions
11271 of the display. Value is null if matrix is empty; otherwise it is
11272 a pointer to the row found. */
11273
11274 static struct glyph_row *
11275 find_last_row_displaying_text (matrix, it, start)
11276 struct glyph_matrix *matrix;
11277 struct it *it;
11278 struct glyph_row *start;
11279 {
11280 struct glyph_row *row, *row_found;
11281
11282 /* Set row_found to the last row in IT->w's current matrix
11283 displaying text. The loop looks funny but think of partially
11284 visible lines. */
11285 row_found = NULL;
11286 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
11287 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
11288 {
11289 xassert (row->enabled_p);
11290 row_found = row;
11291 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
11292 break;
11293 ++row;
11294 }
11295
11296 return row_found;
11297 }
11298
11299
11300 /* Return the last row in the current matrix of W that is not affected
11301 by changes at the start of current_buffer that occurred since W's
11302 current matrix was built. Value is null if no such row exists.
11303
11304 BEG_UNCHANGED us the number of characters unchanged at the start of
11305 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
11306 first changed character in current_buffer. Characters at positions <
11307 BEG + BEG_UNCHANGED are at the same buffer positions as they were
11308 when the current matrix was built. */
11309
11310 static struct glyph_row *
11311 find_last_unchanged_at_beg_row (w)
11312 struct window *w;
11313 {
11314 int first_changed_pos = BEG + BEG_UNCHANGED;
11315 struct glyph_row *row;
11316 struct glyph_row *row_found = NULL;
11317 int yb = window_text_bottom_y (w);
11318
11319 /* Find the last row displaying unchanged text. */
11320 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11321 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
11322 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
11323 {
11324 if (/* If row ends before first_changed_pos, it is unchanged,
11325 except in some case. */
11326 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
11327 /* When row ends in ZV and we write at ZV it is not
11328 unchanged. */
11329 && !row->ends_at_zv_p
11330 /* When first_changed_pos is the end of a continued line,
11331 row is not unchanged because it may be no longer
11332 continued. */
11333 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
11334 && row->continued_p))
11335 row_found = row;
11336
11337 /* Stop if last visible row. */
11338 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
11339 break;
11340
11341 ++row;
11342 }
11343
11344 return row_found;
11345 }
11346
11347
11348 /* Find the first glyph row in the current matrix of W that is not
11349 affected by changes at the end of current_buffer since the
11350 time W's current matrix was built.
11351
11352 Return in *DELTA the number of chars by which buffer positions in
11353 unchanged text at the end of current_buffer must be adjusted.
11354
11355 Return in *DELTA_BYTES the corresponding number of bytes.
11356
11357 Value is null if no such row exists, i.e. all rows are affected by
11358 changes. */
11359
11360 static struct glyph_row *
11361 find_first_unchanged_at_end_row (w, delta, delta_bytes)
11362 struct window *w;
11363 int *delta, *delta_bytes;
11364 {
11365 struct glyph_row *row;
11366 struct glyph_row *row_found = NULL;
11367
11368 *delta = *delta_bytes = 0;
11369
11370 /* Display must not have been paused, otherwise the current matrix
11371 is not up to date. */
11372 if (NILP (w->window_end_valid))
11373 abort ();
11374
11375 /* A value of window_end_pos >= END_UNCHANGED means that the window
11376 end is in the range of changed text. If so, there is no
11377 unchanged row at the end of W's current matrix. */
11378 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
11379 return NULL;
11380
11381 /* Set row to the last row in W's current matrix displaying text. */
11382 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
11383
11384 /* If matrix is entirely empty, no unchanged row exists. */
11385 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
11386 {
11387 /* The value of row is the last glyph row in the matrix having a
11388 meaningful buffer position in it. The end position of row
11389 corresponds to window_end_pos. This allows us to translate
11390 buffer positions in the current matrix to current buffer
11391 positions for characters not in changed text. */
11392 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
11393 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
11394 int last_unchanged_pos, last_unchanged_pos_old;
11395 struct glyph_row *first_text_row
11396 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11397
11398 *delta = Z - Z_old;
11399 *delta_bytes = Z_BYTE - Z_BYTE_old;
11400
11401 /* Set last_unchanged_pos to the buffer position of the last
11402 character in the buffer that has not been changed. Z is the
11403 index + 1 of the last character in current_buffer, i.e. by
11404 subtracting END_UNCHANGED we get the index of the last
11405 unchanged character, and we have to add BEG to get its buffer
11406 position. */
11407 last_unchanged_pos = Z - END_UNCHANGED + BEG;
11408 last_unchanged_pos_old = last_unchanged_pos - *delta;
11409
11410 /* Search backward from ROW for a row displaying a line that
11411 starts at a minimum position >= last_unchanged_pos_old. */
11412 for (; row > first_text_row; --row)
11413 {
11414 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
11415 abort ();
11416
11417 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
11418 row_found = row;
11419 }
11420 }
11421
11422 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
11423 abort ();
11424
11425 return row_found;
11426 }
11427
11428
11429 /* Make sure that glyph rows in the current matrix of window W
11430 reference the same glyph memory as corresponding rows in the
11431 frame's frame matrix. This function is called after scrolling W's
11432 current matrix on a terminal frame in try_window_id and
11433 try_window_reusing_current_matrix. */
11434
11435 static void
11436 sync_frame_with_window_matrix_rows (w)
11437 struct window *w;
11438 {
11439 struct frame *f = XFRAME (w->frame);
11440 struct glyph_row *window_row, *window_row_end, *frame_row;
11441
11442 /* Preconditions: W must be a leaf window and full-width. Its frame
11443 must have a frame matrix. */
11444 xassert (NILP (w->hchild) && NILP (w->vchild));
11445 xassert (WINDOW_FULL_WIDTH_P (w));
11446 xassert (!FRAME_WINDOW_P (f));
11447
11448 /* If W is a full-width window, glyph pointers in W's current matrix
11449 have, by definition, to be the same as glyph pointers in the
11450 corresponding frame matrix. Note that frame matrices have no
11451 marginal areas (see build_frame_matrix). */
11452 window_row = w->current_matrix->rows;
11453 window_row_end = window_row + w->current_matrix->nrows;
11454 frame_row = f->current_matrix->rows + XFASTINT (w->top);
11455 while (window_row < window_row_end)
11456 {
11457 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
11458 struct glyph *end = window_row->glyphs[LAST_AREA];
11459
11460 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
11461 frame_row->glyphs[TEXT_AREA] = start;
11462 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
11463 frame_row->glyphs[LAST_AREA] = end;
11464
11465 /* Disable frame rows whose corresponding window rows have
11466 been disabled in try_window_id. */
11467 if (!window_row->enabled_p)
11468 frame_row->enabled_p = 0;
11469
11470 ++window_row, ++frame_row;
11471 }
11472 }
11473
11474
11475 /* Find the glyph row in window W containing CHARPOS. Consider all
11476 rows between START and END (not inclusive). END null means search
11477 all rows to the end of the display area of W. Value is the row
11478 containing CHARPOS or null. */
11479
11480 struct glyph_row *
11481 row_containing_pos (w, charpos, start, end, dy)
11482 struct window *w;
11483 int charpos;
11484 struct glyph_row *start, *end;
11485 int dy;
11486 {
11487 struct glyph_row *row = start;
11488 int last_y;
11489
11490 /* If we happen to start on a header-line, skip that. */
11491 if (row->mode_line_p)
11492 ++row;
11493
11494 if ((end && row >= end) || !row->enabled_p)
11495 return NULL;
11496
11497 last_y = window_text_bottom_y (w) - dy;
11498
11499 while ((end == NULL || row < end)
11500 && MATRIX_ROW_BOTTOM_Y (row) < last_y
11501 && (MATRIX_ROW_END_CHARPOS (row) < charpos
11502 || (MATRIX_ROW_END_CHARPOS (row) == charpos
11503 /* The end position of a row equals the start
11504 position of the next row. If CHARPOS is there, we
11505 would rather display it in the next line, except
11506 when this line ends in ZV. */
11507 && !row->ends_at_zv_p
11508 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))
11509 ++row;
11510
11511 /* Give up if CHARPOS not found. */
11512 if ((end && row >= end)
11513 || charpos < MATRIX_ROW_START_CHARPOS (row)
11514 || charpos > MATRIX_ROW_END_CHARPOS (row))
11515 row = NULL;
11516
11517 return row;
11518 }
11519
11520
11521 /* Try to redisplay window W by reusing its existing display. W's
11522 current matrix must be up to date when this function is called,
11523 i.e. window_end_valid must not be nil.
11524
11525 Value is
11526
11527 1 if display has been updated
11528 0 if otherwise unsuccessful
11529 -1 if redisplay with same window start is known not to succeed
11530
11531 The following steps are performed:
11532
11533 1. Find the last row in the current matrix of W that is not
11534 affected by changes at the start of current_buffer. If no such row
11535 is found, give up.
11536
11537 2. Find the first row in W's current matrix that is not affected by
11538 changes at the end of current_buffer. Maybe there is no such row.
11539
11540 3. Display lines beginning with the row + 1 found in step 1 to the
11541 row found in step 2 or, if step 2 didn't find a row, to the end of
11542 the window.
11543
11544 4. If cursor is not known to appear on the window, give up.
11545
11546 5. If display stopped at the row found in step 2, scroll the
11547 display and current matrix as needed.
11548
11549 6. Maybe display some lines at the end of W, if we must. This can
11550 happen under various circumstances, like a partially visible line
11551 becoming fully visible, or because newly displayed lines are displayed
11552 in smaller font sizes.
11553
11554 7. Update W's window end information. */
11555
11556 static int
11557 try_window_id (w)
11558 struct window *w;
11559 {
11560 struct frame *f = XFRAME (w->frame);
11561 struct glyph_matrix *current_matrix = w->current_matrix;
11562 struct glyph_matrix *desired_matrix = w->desired_matrix;
11563 struct glyph_row *last_unchanged_at_beg_row;
11564 struct glyph_row *first_unchanged_at_end_row;
11565 struct glyph_row *row;
11566 struct glyph_row *bottom_row;
11567 int bottom_vpos;
11568 struct it it;
11569 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
11570 struct text_pos start_pos;
11571 struct run run;
11572 int first_unchanged_at_end_vpos = 0;
11573 struct glyph_row *last_text_row, *last_text_row_at_end;
11574 struct text_pos start;
11575 int first_changed_charpos, last_changed_charpos;
11576
11577 #if GLYPH_DEBUG
11578 if (inhibit_try_window_id)
11579 return 0;
11580 #endif
11581
11582 /* This is handy for debugging. */
11583 #if 0
11584 #define GIVE_UP(X) \
11585 do { \
11586 fprintf (stderr, "try_window_id give up %d\n", (X)); \
11587 return 0; \
11588 } while (0)
11589 #else
11590 #define GIVE_UP(X) return 0
11591 #endif
11592
11593 SET_TEXT_POS_FROM_MARKER (start, w->start);
11594
11595 /* Don't use this for mini-windows because these can show
11596 messages and mini-buffers, and we don't handle that here. */
11597 if (MINI_WINDOW_P (w))
11598 GIVE_UP (1);
11599
11600 /* This flag is used to prevent redisplay optimizations. */
11601 if (windows_or_buffers_changed || cursor_type_changed)
11602 GIVE_UP (2);
11603
11604 /* Verify that narrowing has not changed.
11605 Also verify that we were not told to prevent redisplay optimizations.
11606 It would be nice to further
11607 reduce the number of cases where this prevents try_window_id. */
11608 if (current_buffer->clip_changed
11609 || current_buffer->prevent_redisplay_optimizations_p)
11610 GIVE_UP (3);
11611
11612 /* Window must either use window-based redisplay or be full width. */
11613 if (!FRAME_WINDOW_P (f)
11614 && (!line_ins_del_ok
11615 || !WINDOW_FULL_WIDTH_P (w)))
11616 GIVE_UP (4);
11617
11618 /* Give up if point is not known NOT to appear in W. */
11619 if (PT < CHARPOS (start))
11620 GIVE_UP (5);
11621
11622 /* Another way to prevent redisplay optimizations. */
11623 if (XFASTINT (w->last_modified) == 0)
11624 GIVE_UP (6);
11625
11626 /* Verify that window is not hscrolled. */
11627 if (XFASTINT (w->hscroll) != 0)
11628 GIVE_UP (7);
11629
11630 /* Verify that display wasn't paused. */
11631 if (NILP (w->window_end_valid))
11632 GIVE_UP (8);
11633
11634 /* Can't use this if highlighting a region because a cursor movement
11635 will do more than just set the cursor. */
11636 if (!NILP (Vtransient_mark_mode)
11637 && !NILP (current_buffer->mark_active))
11638 GIVE_UP (9);
11639
11640 /* Likewise if highlighting trailing whitespace. */
11641 if (!NILP (Vshow_trailing_whitespace))
11642 GIVE_UP (11);
11643
11644 /* Likewise if showing a region. */
11645 if (!NILP (w->region_showing))
11646 GIVE_UP (10);
11647
11648 /* Can use this if overlay arrow position and or string have changed. */
11649 if (!EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
11650 || !EQ (last_arrow_string, Voverlay_arrow_string))
11651 GIVE_UP (12);
11652
11653
11654 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
11655 only if buffer has really changed. The reason is that the gap is
11656 initially at Z for freshly visited files. The code below would
11657 set end_unchanged to 0 in that case. */
11658 if (MODIFF > SAVE_MODIFF
11659 /* This seems to happen sometimes after saving a buffer. */
11660 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
11661 {
11662 if (GPT - BEG < BEG_UNCHANGED)
11663 BEG_UNCHANGED = GPT - BEG;
11664 if (Z - GPT < END_UNCHANGED)
11665 END_UNCHANGED = Z - GPT;
11666 }
11667
11668 /* The position of the first and last character that has been changed. */
11669 first_changed_charpos = BEG + BEG_UNCHANGED;
11670 last_changed_charpos = Z - END_UNCHANGED;
11671
11672 /* If window starts after a line end, and the last change is in
11673 front of that newline, then changes don't affect the display.
11674 This case happens with stealth-fontification. Note that although
11675 the display is unchanged, glyph positions in the matrix have to
11676 be adjusted, of course. */
11677 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
11678 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
11679 && ((last_changed_charpos < CHARPOS (start)
11680 && CHARPOS (start) == BEGV)
11681 || (last_changed_charpos < CHARPOS (start) - 1
11682 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
11683 {
11684 int Z_old, delta, Z_BYTE_old, delta_bytes;
11685 struct glyph_row *r0;
11686
11687 /* Compute how many chars/bytes have been added to or removed
11688 from the buffer. */
11689 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
11690 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
11691 delta = Z - Z_old;
11692 delta_bytes = Z_BYTE - Z_BYTE_old;
11693
11694 /* Give up if PT is not in the window. Note that it already has
11695 been checked at the start of try_window_id that PT is not in
11696 front of the window start. */
11697 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
11698 GIVE_UP (13);
11699
11700 /* If window start is unchanged, we can reuse the whole matrix
11701 as is, after adjusting glyph positions. No need to compute
11702 the window end again, since its offset from Z hasn't changed. */
11703 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
11704 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
11705 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes)
11706 {
11707 /* Adjust positions in the glyph matrix. */
11708 if (delta || delta_bytes)
11709 {
11710 struct glyph_row *r1
11711 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11712 increment_matrix_positions (w->current_matrix,
11713 MATRIX_ROW_VPOS (r0, current_matrix),
11714 MATRIX_ROW_VPOS (r1, current_matrix),
11715 delta, delta_bytes);
11716 }
11717
11718 /* Set the cursor. */
11719 row = row_containing_pos (w, PT, r0, NULL, 0);
11720 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
11721 return 1;
11722 }
11723 }
11724
11725 /* Handle the case that changes are all below what is displayed in
11726 the window, and that PT is in the window. This shortcut cannot
11727 be taken if ZV is visible in the window, and text has been added
11728 there that is visible in the window. */
11729 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
11730 /* ZV is not visible in the window, or there are no
11731 changes at ZV, actually. */
11732 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
11733 || first_changed_charpos == last_changed_charpos))
11734 {
11735 struct glyph_row *r0;
11736
11737 /* Give up if PT is not in the window. Note that it already has
11738 been checked at the start of try_window_id that PT is not in
11739 front of the window start. */
11740 if (PT >= MATRIX_ROW_END_CHARPOS (row))
11741 GIVE_UP (14);
11742
11743 /* If window start is unchanged, we can reuse the whole matrix
11744 as is, without changing glyph positions since no text has
11745 been added/removed in front of the window end. */
11746 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
11747 if (TEXT_POS_EQUAL_P (start, r0->start.pos))
11748 {
11749 /* We have to compute the window end anew since text
11750 can have been added/removed after it. */
11751 w->window_end_pos
11752 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
11753 w->window_end_bytepos
11754 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11755
11756 /* Set the cursor. */
11757 row = row_containing_pos (w, PT, r0, NULL, 0);
11758 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
11759 return 2;
11760 }
11761 }
11762
11763 /* Give up if window start is in the changed area.
11764
11765 The condition used to read
11766
11767 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
11768
11769 but why that was tested escapes me at the moment. */
11770 if (CHARPOS (start) >= first_changed_charpos
11771 && CHARPOS (start) <= last_changed_charpos)
11772 GIVE_UP (15);
11773
11774 /* Check that window start agrees with the start of the first glyph
11775 row in its current matrix. Check this after we know the window
11776 start is not in changed text, otherwise positions would not be
11777 comparable. */
11778 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
11779 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
11780 GIVE_UP (16);
11781
11782 /* Give up if the window ends in strings. Overlay strings
11783 at the end are difficult to handle, so don't try. */
11784 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
11785 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
11786 GIVE_UP (20);
11787
11788 /* Compute the position at which we have to start displaying new
11789 lines. Some of the lines at the top of the window might be
11790 reusable because they are not displaying changed text. Find the
11791 last row in W's current matrix not affected by changes at the
11792 start of current_buffer. Value is null if changes start in the
11793 first line of window. */
11794 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
11795 if (last_unchanged_at_beg_row)
11796 {
11797 /* Avoid starting to display in the moddle of a character, a TAB
11798 for instance. This is easier than to set up the iterator
11799 exactly, and it's not a frequent case, so the additional
11800 effort wouldn't really pay off. */
11801 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
11802 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
11803 && last_unchanged_at_beg_row > w->current_matrix->rows)
11804 --last_unchanged_at_beg_row;
11805
11806 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
11807 GIVE_UP (17);
11808
11809 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
11810 GIVE_UP (18);
11811 start_pos = it.current.pos;
11812
11813 /* Start displaying new lines in the desired matrix at the same
11814 vpos we would use in the current matrix, i.e. below
11815 last_unchanged_at_beg_row. */
11816 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
11817 current_matrix);
11818 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11819 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
11820
11821 xassert (it.hpos == 0 && it.current_x == 0);
11822 }
11823 else
11824 {
11825 /* There are no reusable lines at the start of the window.
11826 Start displaying in the first line. */
11827 start_display (&it, w, start);
11828 start_pos = it.current.pos;
11829 }
11830
11831 /* Find the first row that is not affected by changes at the end of
11832 the buffer. Value will be null if there is no unchanged row, in
11833 which case we must redisplay to the end of the window. delta
11834 will be set to the value by which buffer positions beginning with
11835 first_unchanged_at_end_row have to be adjusted due to text
11836 changes. */
11837 first_unchanged_at_end_row
11838 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
11839 IF_DEBUG (debug_delta = delta);
11840 IF_DEBUG (debug_delta_bytes = delta_bytes);
11841
11842 /* Set stop_pos to the buffer position up to which we will have to
11843 display new lines. If first_unchanged_at_end_row != NULL, this
11844 is the buffer position of the start of the line displayed in that
11845 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
11846 that we don't stop at a buffer position. */
11847 stop_pos = 0;
11848 if (first_unchanged_at_end_row)
11849 {
11850 xassert (last_unchanged_at_beg_row == NULL
11851 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
11852
11853 /* If this is a continuation line, move forward to the next one
11854 that isn't. Changes in lines above affect this line.
11855 Caution: this may move first_unchanged_at_end_row to a row
11856 not displaying text. */
11857 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
11858 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11859 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11860 < it.last_visible_y))
11861 ++first_unchanged_at_end_row;
11862
11863 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11864 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11865 >= it.last_visible_y))
11866 first_unchanged_at_end_row = NULL;
11867 else
11868 {
11869 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
11870 + delta);
11871 first_unchanged_at_end_vpos
11872 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
11873 xassert (stop_pos >= Z - END_UNCHANGED);
11874 }
11875 }
11876 else if (last_unchanged_at_beg_row == NULL)
11877 GIVE_UP (19);
11878
11879
11880 #if GLYPH_DEBUG
11881
11882 /* Either there is no unchanged row at the end, or the one we have
11883 now displays text. This is a necessary condition for the window
11884 end pos calculation at the end of this function. */
11885 xassert (first_unchanged_at_end_row == NULL
11886 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
11887
11888 debug_last_unchanged_at_beg_vpos
11889 = (last_unchanged_at_beg_row
11890 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
11891 : -1);
11892 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
11893
11894 #endif /* GLYPH_DEBUG != 0 */
11895
11896
11897 /* Display new lines. Set last_text_row to the last new line
11898 displayed which has text on it, i.e. might end up as being the
11899 line where the window_end_vpos is. */
11900 w->cursor.vpos = -1;
11901 last_text_row = NULL;
11902 overlay_arrow_seen = 0;
11903 while (it.current_y < it.last_visible_y
11904 && !fonts_changed_p
11905 && (first_unchanged_at_end_row == NULL
11906 || IT_CHARPOS (it) < stop_pos))
11907 {
11908 if (display_line (&it))
11909 last_text_row = it.glyph_row - 1;
11910 }
11911
11912 if (fonts_changed_p)
11913 return -1;
11914
11915
11916 /* Compute differences in buffer positions, y-positions etc. for
11917 lines reused at the bottom of the window. Compute what we can
11918 scroll. */
11919 if (first_unchanged_at_end_row
11920 /* No lines reused because we displayed everything up to the
11921 bottom of the window. */
11922 && it.current_y < it.last_visible_y)
11923 {
11924 dvpos = (it.vpos
11925 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
11926 current_matrix));
11927 dy = it.current_y - first_unchanged_at_end_row->y;
11928 run.current_y = first_unchanged_at_end_row->y;
11929 run.desired_y = run.current_y + dy;
11930 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
11931 }
11932 else
11933 {
11934 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
11935 first_unchanged_at_end_row = NULL;
11936 }
11937 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
11938
11939
11940 /* Find the cursor if not already found. We have to decide whether
11941 PT will appear on this window (it sometimes doesn't, but this is
11942 not a very frequent case.) This decision has to be made before
11943 the current matrix is altered. A value of cursor.vpos < 0 means
11944 that PT is either in one of the lines beginning at
11945 first_unchanged_at_end_row or below the window. Don't care for
11946 lines that might be displayed later at the window end; as
11947 mentioned, this is not a frequent case. */
11948 if (w->cursor.vpos < 0)
11949 {
11950 /* Cursor in unchanged rows at the top? */
11951 if (PT < CHARPOS (start_pos)
11952 && last_unchanged_at_beg_row)
11953 {
11954 row = row_containing_pos (w, PT,
11955 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
11956 last_unchanged_at_beg_row + 1, 0);
11957 if (row)
11958 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11959 }
11960
11961 /* Start from first_unchanged_at_end_row looking for PT. */
11962 else if (first_unchanged_at_end_row)
11963 {
11964 row = row_containing_pos (w, PT - delta,
11965 first_unchanged_at_end_row, NULL, 0);
11966 if (row)
11967 set_cursor_from_row (w, row, w->current_matrix, delta,
11968 delta_bytes, dy, dvpos);
11969 }
11970
11971 /* Give up if cursor was not found. */
11972 if (w->cursor.vpos < 0)
11973 {
11974 clear_glyph_matrix (w->desired_matrix);
11975 return -1;
11976 }
11977 }
11978
11979 /* Don't let the cursor end in the scroll margins. */
11980 {
11981 int this_scroll_margin, cursor_height;
11982
11983 this_scroll_margin = max (0, scroll_margin);
11984 this_scroll_margin = min (this_scroll_margin,
11985 XFASTINT (w->height) / 4);
11986 this_scroll_margin *= CANON_Y_UNIT (it.f);
11987 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
11988
11989 if ((w->cursor.y < this_scroll_margin
11990 && CHARPOS (start) > BEGV)
11991 /* Don't take scroll margin into account at the bottom because
11992 old redisplay didn't do it either. */
11993 || w->cursor.y + cursor_height > it.last_visible_y)
11994 {
11995 w->cursor.vpos = -1;
11996 clear_glyph_matrix (w->desired_matrix);
11997 return -1;
11998 }
11999 }
12000
12001 /* Scroll the display. Do it before changing the current matrix so
12002 that xterm.c doesn't get confused about where the cursor glyph is
12003 found. */
12004 if (dy && run.height)
12005 {
12006 update_begin (f);
12007
12008 if (FRAME_WINDOW_P (f))
12009 {
12010 rif->update_window_begin_hook (w);
12011 rif->clear_mouse_face (w);
12012 rif->scroll_run_hook (w, &run);
12013 rif->update_window_end_hook (w, 0, 0);
12014 }
12015 else
12016 {
12017 /* Terminal frame. In this case, dvpos gives the number of
12018 lines to scroll by; dvpos < 0 means scroll up. */
12019 int first_unchanged_at_end_vpos
12020 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
12021 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
12022 int end = (XFASTINT (w->top)
12023 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
12024 + window_internal_height (w));
12025
12026 /* Perform the operation on the screen. */
12027 if (dvpos > 0)
12028 {
12029 /* Scroll last_unchanged_at_beg_row to the end of the
12030 window down dvpos lines. */
12031 set_terminal_window (end);
12032
12033 /* On dumb terminals delete dvpos lines at the end
12034 before inserting dvpos empty lines. */
12035 if (!scroll_region_ok)
12036 ins_del_lines (end - dvpos, -dvpos);
12037
12038 /* Insert dvpos empty lines in front of
12039 last_unchanged_at_beg_row. */
12040 ins_del_lines (from, dvpos);
12041 }
12042 else if (dvpos < 0)
12043 {
12044 /* Scroll up last_unchanged_at_beg_vpos to the end of
12045 the window to last_unchanged_at_beg_vpos - |dvpos|. */
12046 set_terminal_window (end);
12047
12048 /* Delete dvpos lines in front of
12049 last_unchanged_at_beg_vpos. ins_del_lines will set
12050 the cursor to the given vpos and emit |dvpos| delete
12051 line sequences. */
12052 ins_del_lines (from + dvpos, dvpos);
12053
12054 /* On a dumb terminal insert dvpos empty lines at the
12055 end. */
12056 if (!scroll_region_ok)
12057 ins_del_lines (end + dvpos, -dvpos);
12058 }
12059
12060 set_terminal_window (0);
12061 }
12062
12063 update_end (f);
12064 }
12065
12066 /* Shift reused rows of the current matrix to the right position.
12067 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
12068 text. */
12069 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
12070 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
12071 if (dvpos < 0)
12072 {
12073 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
12074 bottom_vpos, dvpos);
12075 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
12076 bottom_vpos, 0);
12077 }
12078 else if (dvpos > 0)
12079 {
12080 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
12081 bottom_vpos, dvpos);
12082 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
12083 first_unchanged_at_end_vpos + dvpos, 0);
12084 }
12085
12086 /* For frame-based redisplay, make sure that current frame and window
12087 matrix are in sync with respect to glyph memory. */
12088 if (!FRAME_WINDOW_P (f))
12089 sync_frame_with_window_matrix_rows (w);
12090
12091 /* Adjust buffer positions in reused rows. */
12092 if (delta)
12093 increment_matrix_positions (current_matrix,
12094 first_unchanged_at_end_vpos + dvpos,
12095 bottom_vpos, delta, delta_bytes);
12096
12097 /* Adjust Y positions. */
12098 if (dy)
12099 shift_glyph_matrix (w, current_matrix,
12100 first_unchanged_at_end_vpos + dvpos,
12101 bottom_vpos, dy);
12102
12103 if (first_unchanged_at_end_row)
12104 first_unchanged_at_end_row += dvpos;
12105
12106 /* If scrolling up, there may be some lines to display at the end of
12107 the window. */
12108 last_text_row_at_end = NULL;
12109 if (dy < 0)
12110 {
12111 /* Scrolling up can leave for example a partially visible line
12112 at the end of the window to be redisplayed. */
12113 /* Set last_row to the glyph row in the current matrix where the
12114 window end line is found. It has been moved up or down in
12115 the matrix by dvpos. */
12116 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
12117 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
12118
12119 /* If last_row is the window end line, it should display text. */
12120 xassert (last_row->displays_text_p);
12121
12122 /* If window end line was partially visible before, begin
12123 displaying at that line. Otherwise begin displaying with the
12124 line following it. */
12125 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
12126 {
12127 init_to_row_start (&it, w, last_row);
12128 it.vpos = last_vpos;
12129 it.current_y = last_row->y;
12130 }
12131 else
12132 {
12133 init_to_row_end (&it, w, last_row);
12134 it.vpos = 1 + last_vpos;
12135 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
12136 ++last_row;
12137 }
12138
12139 /* We may start in a continuation line. If so, we have to
12140 get the right continuation_lines_width and current_x. */
12141 it.continuation_lines_width = last_row->continuation_lines_width;
12142 it.hpos = it.current_x = 0;
12143
12144 /* Display the rest of the lines at the window end. */
12145 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
12146 while (it.current_y < it.last_visible_y
12147 && !fonts_changed_p)
12148 {
12149 /* Is it always sure that the display agrees with lines in
12150 the current matrix? I don't think so, so we mark rows
12151 displayed invalid in the current matrix by setting their
12152 enabled_p flag to zero. */
12153 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
12154 if (display_line (&it))
12155 last_text_row_at_end = it.glyph_row - 1;
12156 }
12157 }
12158
12159 /* Update window_end_pos and window_end_vpos. */
12160 if (first_unchanged_at_end_row
12161 && first_unchanged_at_end_row->y < it.last_visible_y
12162 && !last_text_row_at_end)
12163 {
12164 /* Window end line if one of the preserved rows from the current
12165 matrix. Set row to the last row displaying text in current
12166 matrix starting at first_unchanged_at_end_row, after
12167 scrolling. */
12168 xassert (first_unchanged_at_end_row->displays_text_p);
12169 row = find_last_row_displaying_text (w->current_matrix, &it,
12170 first_unchanged_at_end_row);
12171 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
12172
12173 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
12174 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
12175 w->window_end_vpos
12176 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
12177 xassert (w->window_end_bytepos >= 0);
12178 IF_DEBUG (debug_method_add (w, "A"));
12179 }
12180 else if (last_text_row_at_end)
12181 {
12182 w->window_end_pos
12183 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
12184 w->window_end_bytepos
12185 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
12186 w->window_end_vpos
12187 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
12188 xassert (w->window_end_bytepos >= 0);
12189 IF_DEBUG (debug_method_add (w, "B"));
12190 }
12191 else if (last_text_row)
12192 {
12193 /* We have displayed either to the end of the window or at the
12194 end of the window, i.e. the last row with text is to be found
12195 in the desired matrix. */
12196 w->window_end_pos
12197 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12198 w->window_end_bytepos
12199 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12200 w->window_end_vpos
12201 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
12202 xassert (w->window_end_bytepos >= 0);
12203 }
12204 else if (first_unchanged_at_end_row == NULL
12205 && last_text_row == NULL
12206 && last_text_row_at_end == NULL)
12207 {
12208 /* Displayed to end of window, but no line containing text was
12209 displayed. Lines were deleted at the end of the window. */
12210 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
12211 int vpos = XFASTINT (w->window_end_vpos);
12212 struct glyph_row *current_row = current_matrix->rows + vpos;
12213 struct glyph_row *desired_row = desired_matrix->rows + vpos;
12214
12215 for (row = NULL;
12216 row == NULL && vpos >= first_vpos;
12217 --vpos, --current_row, --desired_row)
12218 {
12219 if (desired_row->enabled_p)
12220 {
12221 if (desired_row->displays_text_p)
12222 row = desired_row;
12223 }
12224 else if (current_row->displays_text_p)
12225 row = current_row;
12226 }
12227
12228 xassert (row != NULL);
12229 w->window_end_vpos = make_number (vpos + 1);
12230 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
12231 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
12232 xassert (w->window_end_bytepos >= 0);
12233 IF_DEBUG (debug_method_add (w, "C"));
12234 }
12235 else
12236 abort ();
12237
12238 #if 0 /* This leads to problems, for instance when the cursor is
12239 at ZV, and the cursor line displays no text. */
12240 /* Disable rows below what's displayed in the window. This makes
12241 debugging easier. */
12242 enable_glyph_matrix_rows (current_matrix,
12243 XFASTINT (w->window_end_vpos) + 1,
12244 bottom_vpos, 0);
12245 #endif
12246
12247 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
12248 debug_end_vpos = XFASTINT (w->window_end_vpos));
12249
12250 /* Record that display has not been completed. */
12251 w->window_end_valid = Qnil;
12252 w->desired_matrix->no_scrolling_p = 1;
12253 return 3;
12254
12255 #undef GIVE_UP
12256 }
12257
12258
12259 \f
12260 /***********************************************************************
12261 More debugging support
12262 ***********************************************************************/
12263
12264 #if GLYPH_DEBUG
12265
12266 void dump_glyph_row P_ ((struct glyph_row *, int, int));
12267 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
12268 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
12269
12270
12271 /* Dump the contents of glyph matrix MATRIX on stderr.
12272
12273 GLYPHS 0 means don't show glyph contents.
12274 GLYPHS 1 means show glyphs in short form
12275 GLYPHS > 1 means show glyphs in long form. */
12276
12277 void
12278 dump_glyph_matrix (matrix, glyphs)
12279 struct glyph_matrix *matrix;
12280 int glyphs;
12281 {
12282 int i;
12283 for (i = 0; i < matrix->nrows; ++i)
12284 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
12285 }
12286
12287
12288 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
12289 the glyph row and area where the glyph comes from. */
12290
12291 void
12292 dump_glyph (row, glyph, area)
12293 struct glyph_row *row;
12294 struct glyph *glyph;
12295 int area;
12296 {
12297 if (glyph->type == CHAR_GLYPH)
12298 {
12299 fprintf (stderr,
12300 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12301 glyph - row->glyphs[TEXT_AREA],
12302 'C',
12303 glyph->charpos,
12304 (BUFFERP (glyph->object)
12305 ? 'B'
12306 : (STRINGP (glyph->object)
12307 ? 'S'
12308 : '-')),
12309 glyph->pixel_width,
12310 glyph->u.ch,
12311 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
12312 ? glyph->u.ch
12313 : '.'),
12314 glyph->face_id,
12315 glyph->left_box_line_p,
12316 glyph->right_box_line_p);
12317 }
12318 else if (glyph->type == STRETCH_GLYPH)
12319 {
12320 fprintf (stderr,
12321 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12322 glyph - row->glyphs[TEXT_AREA],
12323 'S',
12324 glyph->charpos,
12325 (BUFFERP (glyph->object)
12326 ? 'B'
12327 : (STRINGP (glyph->object)
12328 ? 'S'
12329 : '-')),
12330 glyph->pixel_width,
12331 0,
12332 '.',
12333 glyph->face_id,
12334 glyph->left_box_line_p,
12335 glyph->right_box_line_p);
12336 }
12337 else if (glyph->type == IMAGE_GLYPH)
12338 {
12339 fprintf (stderr,
12340 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12341 glyph - row->glyphs[TEXT_AREA],
12342 'I',
12343 glyph->charpos,
12344 (BUFFERP (glyph->object)
12345 ? 'B'
12346 : (STRINGP (glyph->object)
12347 ? 'S'
12348 : '-')),
12349 glyph->pixel_width,
12350 glyph->u.img_id,
12351 '.',
12352 glyph->face_id,
12353 glyph->left_box_line_p,
12354 glyph->right_box_line_p);
12355 }
12356 }
12357
12358
12359 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
12360 GLYPHS 0 means don't show glyph contents.
12361 GLYPHS 1 means show glyphs in short form
12362 GLYPHS > 1 means show glyphs in long form. */
12363
12364 void
12365 dump_glyph_row (row, vpos, glyphs)
12366 struct glyph_row *row;
12367 int vpos, glyphs;
12368 {
12369 if (glyphs != 1)
12370 {
12371 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
12372 fprintf (stderr, "=======================================================================\n");
12373
12374 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d\
12375 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
12376 vpos,
12377 MATRIX_ROW_START_CHARPOS (row),
12378 MATRIX_ROW_END_CHARPOS (row),
12379 row->used[TEXT_AREA],
12380 row->contains_overlapping_glyphs_p,
12381 row->enabled_p,
12382 row->truncated_on_left_p,
12383 row->truncated_on_right_p,
12384 row->overlay_arrow_p,
12385 row->continued_p,
12386 MATRIX_ROW_CONTINUATION_LINE_P (row),
12387 row->displays_text_p,
12388 row->ends_at_zv_p,
12389 row->fill_line_p,
12390 row->ends_in_middle_of_char_p,
12391 row->starts_in_middle_of_char_p,
12392 row->mouse_face_p,
12393 row->x,
12394 row->y,
12395 row->pixel_width,
12396 row->height,
12397 row->visible_height,
12398 row->ascent,
12399 row->phys_ascent);
12400 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
12401 row->end.overlay_string_index,
12402 row->continuation_lines_width);
12403 fprintf (stderr, "%9d %5d\n",
12404 CHARPOS (row->start.string_pos),
12405 CHARPOS (row->end.string_pos));
12406 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
12407 row->end.dpvec_index);
12408 }
12409
12410 if (glyphs > 1)
12411 {
12412 int area;
12413
12414 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12415 {
12416 struct glyph *glyph = row->glyphs[area];
12417 struct glyph *glyph_end = glyph + row->used[area];
12418
12419 /* Glyph for a line end in text. */
12420 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
12421 ++glyph_end;
12422
12423 if (glyph < glyph_end)
12424 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
12425
12426 for (; glyph < glyph_end; ++glyph)
12427 dump_glyph (row, glyph, area);
12428 }
12429 }
12430 else if (glyphs == 1)
12431 {
12432 int area;
12433
12434 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12435 {
12436 char *s = (char *) alloca (row->used[area] + 1);
12437 int i;
12438
12439 for (i = 0; i < row->used[area]; ++i)
12440 {
12441 struct glyph *glyph = row->glyphs[area] + i;
12442 if (glyph->type == CHAR_GLYPH
12443 && glyph->u.ch < 0x80
12444 && glyph->u.ch >= ' ')
12445 s[i] = glyph->u.ch;
12446 else
12447 s[i] = '.';
12448 }
12449
12450 s[i] = '\0';
12451 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
12452 }
12453 }
12454 }
12455
12456
12457 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
12458 Sdump_glyph_matrix, 0, 1, "p",
12459 doc: /* Dump the current matrix of the selected window to stderr.
12460 Shows contents of glyph row structures. With non-nil
12461 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
12462 glyphs in short form, otherwise show glyphs in long form. */)
12463 (glyphs)
12464 Lisp_Object glyphs;
12465 {
12466 struct window *w = XWINDOW (selected_window);
12467 struct buffer *buffer = XBUFFER (w->buffer);
12468
12469 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
12470 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
12471 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
12472 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
12473 fprintf (stderr, "=============================================\n");
12474 dump_glyph_matrix (w->current_matrix,
12475 NILP (glyphs) ? 0 : XINT (glyphs));
12476 return Qnil;
12477 }
12478
12479
12480 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
12481 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
12482 ()
12483 {
12484 struct frame *f = XFRAME (selected_frame);
12485 dump_glyph_matrix (f->current_matrix, 1);
12486 return Qnil;
12487 }
12488
12489
12490 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
12491 doc: /* Dump glyph row ROW to stderr.
12492 GLYPH 0 means don't dump glyphs.
12493 GLYPH 1 means dump glyphs in short form.
12494 GLYPH > 1 or omitted means dump glyphs in long form. */)
12495 (row, glyphs)
12496 Lisp_Object row, glyphs;
12497 {
12498 struct glyph_matrix *matrix;
12499 int vpos;
12500
12501 CHECK_NUMBER (row);
12502 matrix = XWINDOW (selected_window)->current_matrix;
12503 vpos = XINT (row);
12504 if (vpos >= 0 && vpos < matrix->nrows)
12505 dump_glyph_row (MATRIX_ROW (matrix, vpos),
12506 vpos,
12507 INTEGERP (glyphs) ? XINT (glyphs) : 2);
12508 return Qnil;
12509 }
12510
12511
12512 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
12513 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
12514 GLYPH 0 means don't dump glyphs.
12515 GLYPH 1 means dump glyphs in short form.
12516 GLYPH > 1 or omitted means dump glyphs in long form. */)
12517 (row, glyphs)
12518 Lisp_Object row, glyphs;
12519 {
12520 struct frame *sf = SELECTED_FRAME ();
12521 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
12522 int vpos;
12523
12524 CHECK_NUMBER (row);
12525 vpos = XINT (row);
12526 if (vpos >= 0 && vpos < m->nrows)
12527 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
12528 INTEGERP (glyphs) ? XINT (glyphs) : 2);
12529 return Qnil;
12530 }
12531
12532
12533 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
12534 doc: /* Toggle tracing of redisplay.
12535 With ARG, turn tracing on if and only if ARG is positive. */)
12536 (arg)
12537 Lisp_Object arg;
12538 {
12539 if (NILP (arg))
12540 trace_redisplay_p = !trace_redisplay_p;
12541 else
12542 {
12543 arg = Fprefix_numeric_value (arg);
12544 trace_redisplay_p = XINT (arg) > 0;
12545 }
12546
12547 return Qnil;
12548 }
12549
12550
12551 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
12552 doc: /* Like `format', but print result to stderr.
12553 usage: (trace-to-stderr STRING &rest OBJECTS) */)
12554 (nargs, args)
12555 int nargs;
12556 Lisp_Object *args;
12557 {
12558 Lisp_Object s = Fformat (nargs, args);
12559 fprintf (stderr, "%s", SDATA (s));
12560 return Qnil;
12561 }
12562
12563 #endif /* GLYPH_DEBUG */
12564
12565
12566 \f
12567 /***********************************************************************
12568 Building Desired Matrix Rows
12569 ***********************************************************************/
12570
12571 /* Return a temporary glyph row holding the glyphs of an overlay
12572 arrow. Only used for non-window-redisplay windows. */
12573
12574 static struct glyph_row *
12575 get_overlay_arrow_glyph_row (w)
12576 struct window *w;
12577 {
12578 struct frame *f = XFRAME (WINDOW_FRAME (w));
12579 struct buffer *buffer = XBUFFER (w->buffer);
12580 struct buffer *old = current_buffer;
12581 const unsigned char *arrow_string = SDATA (Voverlay_arrow_string);
12582 int arrow_len = SCHARS (Voverlay_arrow_string);
12583 const unsigned char *arrow_end = arrow_string + arrow_len;
12584 const unsigned char *p;
12585 struct it it;
12586 int multibyte_p;
12587 int n_glyphs_before;
12588
12589 set_buffer_temp (buffer);
12590 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
12591 it.glyph_row->used[TEXT_AREA] = 0;
12592 SET_TEXT_POS (it.position, 0, 0);
12593
12594 multibyte_p = !NILP (buffer->enable_multibyte_characters);
12595 p = arrow_string;
12596 while (p < arrow_end)
12597 {
12598 Lisp_Object face, ilisp;
12599
12600 /* Get the next character. */
12601 if (multibyte_p)
12602 it.c = string_char_and_length (p, arrow_len, &it.len);
12603 else
12604 it.c = *p, it.len = 1;
12605 p += it.len;
12606
12607 /* Get its face. */
12608 ilisp = make_number (p - arrow_string);
12609 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
12610 it.face_id = compute_char_face (f, it.c, face);
12611
12612 /* Compute its width, get its glyphs. */
12613 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
12614 SET_TEXT_POS (it.position, -1, -1);
12615 PRODUCE_GLYPHS (&it);
12616
12617 /* If this character doesn't fit any more in the line, we have
12618 to remove some glyphs. */
12619 if (it.current_x > it.last_visible_x)
12620 {
12621 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
12622 break;
12623 }
12624 }
12625
12626 set_buffer_temp (old);
12627 return it.glyph_row;
12628 }
12629
12630
12631 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
12632 glyphs are only inserted for terminal frames since we can't really
12633 win with truncation glyphs when partially visible glyphs are
12634 involved. Which glyphs to insert is determined by
12635 produce_special_glyphs. */
12636
12637 static void
12638 insert_left_trunc_glyphs (it)
12639 struct it *it;
12640 {
12641 struct it truncate_it;
12642 struct glyph *from, *end, *to, *toend;
12643
12644 xassert (!FRAME_WINDOW_P (it->f));
12645
12646 /* Get the truncation glyphs. */
12647 truncate_it = *it;
12648 truncate_it.current_x = 0;
12649 truncate_it.face_id = DEFAULT_FACE_ID;
12650 truncate_it.glyph_row = &scratch_glyph_row;
12651 truncate_it.glyph_row->used[TEXT_AREA] = 0;
12652 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
12653 truncate_it.object = make_number (0);
12654 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
12655
12656 /* Overwrite glyphs from IT with truncation glyphs. */
12657 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
12658 end = from + truncate_it.glyph_row->used[TEXT_AREA];
12659 to = it->glyph_row->glyphs[TEXT_AREA];
12660 toend = to + it->glyph_row->used[TEXT_AREA];
12661
12662 while (from < end)
12663 *to++ = *from++;
12664
12665 /* There may be padding glyphs left over. Overwrite them too. */
12666 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
12667 {
12668 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
12669 while (from < end)
12670 *to++ = *from++;
12671 }
12672
12673 if (to > toend)
12674 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
12675 }
12676
12677
12678 /* Compute the pixel height and width of IT->glyph_row.
12679
12680 Most of the time, ascent and height of a display line will be equal
12681 to the max_ascent and max_height values of the display iterator
12682 structure. This is not the case if
12683
12684 1. We hit ZV without displaying anything. In this case, max_ascent
12685 and max_height will be zero.
12686
12687 2. We have some glyphs that don't contribute to the line height.
12688 (The glyph row flag contributes_to_line_height_p is for future
12689 pixmap extensions).
12690
12691 The first case is easily covered by using default values because in
12692 these cases, the line height does not really matter, except that it
12693 must not be zero. */
12694
12695 static void
12696 compute_line_metrics (it)
12697 struct it *it;
12698 {
12699 struct glyph_row *row = it->glyph_row;
12700 int area, i;
12701
12702 if (FRAME_WINDOW_P (it->f))
12703 {
12704 int i, min_y, max_y;
12705
12706 /* The line may consist of one space only, that was added to
12707 place the cursor on it. If so, the row's height hasn't been
12708 computed yet. */
12709 if (row->height == 0)
12710 {
12711 if (it->max_ascent + it->max_descent == 0)
12712 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
12713 row->ascent = it->max_ascent;
12714 row->height = it->max_ascent + it->max_descent;
12715 row->phys_ascent = it->max_phys_ascent;
12716 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
12717 }
12718
12719 /* Compute the width of this line. */
12720 row->pixel_width = row->x;
12721 for (i = 0; i < row->used[TEXT_AREA]; ++i)
12722 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
12723
12724 xassert (row->pixel_width >= 0);
12725 xassert (row->ascent >= 0 && row->height > 0);
12726
12727 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
12728 || MATRIX_ROW_OVERLAPS_PRED_P (row));
12729
12730 /* If first line's physical ascent is larger than its logical
12731 ascent, use the physical ascent, and make the row taller.
12732 This makes accented characters fully visible. */
12733 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
12734 && row->phys_ascent > row->ascent)
12735 {
12736 row->height += row->phys_ascent - row->ascent;
12737 row->ascent = row->phys_ascent;
12738 }
12739
12740 /* Compute how much of the line is visible. */
12741 row->visible_height = row->height;
12742
12743 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (it->w);
12744 max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
12745
12746 if (row->y < min_y)
12747 row->visible_height -= min_y - row->y;
12748 if (row->y + row->height > max_y)
12749 row->visible_height -= row->y + row->height - max_y;
12750 }
12751 else
12752 {
12753 row->pixel_width = row->used[TEXT_AREA];
12754 if (row->continued_p)
12755 row->pixel_width -= it->continuation_pixel_width;
12756 else if (row->truncated_on_right_p)
12757 row->pixel_width -= it->truncation_pixel_width;
12758 row->ascent = row->phys_ascent = 0;
12759 row->height = row->phys_height = row->visible_height = 1;
12760 }
12761
12762 /* Compute a hash code for this row. */
12763 row->hash = 0;
12764 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12765 for (i = 0; i < row->used[area]; ++i)
12766 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
12767 + row->glyphs[area][i].u.val
12768 + row->glyphs[area][i].face_id
12769 + row->glyphs[area][i].padding_p
12770 + (row->glyphs[area][i].type << 2));
12771
12772 it->max_ascent = it->max_descent = 0;
12773 it->max_phys_ascent = it->max_phys_descent = 0;
12774 }
12775
12776
12777 /* Append one space to the glyph row of iterator IT if doing a
12778 window-based redisplay. DEFAULT_FACE_P non-zero means let the
12779 space have the default face, otherwise let it have the same face as
12780 IT->face_id. Value is non-zero if a space was added.
12781
12782 This function is called to make sure that there is always one glyph
12783 at the end of a glyph row that the cursor can be set on under
12784 window-systems. (If there weren't such a glyph we would not know
12785 how wide and tall a box cursor should be displayed).
12786
12787 At the same time this space let's a nicely handle clearing to the
12788 end of the line if the row ends in italic text. */
12789
12790 static int
12791 append_space (it, default_face_p)
12792 struct it *it;
12793 int default_face_p;
12794 {
12795 if (FRAME_WINDOW_P (it->f))
12796 {
12797 int n = it->glyph_row->used[TEXT_AREA];
12798
12799 if (it->glyph_row->glyphs[TEXT_AREA] + n
12800 < it->glyph_row->glyphs[1 + TEXT_AREA])
12801 {
12802 /* Save some values that must not be changed.
12803 Must save IT->c and IT->len because otherwise
12804 ITERATOR_AT_END_P wouldn't work anymore after
12805 append_space has been called. */
12806 enum display_element_type saved_what = it->what;
12807 int saved_c = it->c, saved_len = it->len;
12808 int saved_x = it->current_x;
12809 int saved_face_id = it->face_id;
12810 struct text_pos saved_pos;
12811 Lisp_Object saved_object;
12812 struct face *face;
12813
12814 saved_object = it->object;
12815 saved_pos = it->position;
12816
12817 it->what = IT_CHARACTER;
12818 bzero (&it->position, sizeof it->position);
12819 it->object = make_number (0);
12820 it->c = ' ';
12821 it->len = 1;
12822
12823 if (default_face_p)
12824 it->face_id = DEFAULT_FACE_ID;
12825 else if (it->face_before_selective_p)
12826 it->face_id = it->saved_face_id;
12827 face = FACE_FROM_ID (it->f, it->face_id);
12828 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
12829
12830 PRODUCE_GLYPHS (it);
12831
12832 it->current_x = saved_x;
12833 it->object = saved_object;
12834 it->position = saved_pos;
12835 it->what = saved_what;
12836 it->face_id = saved_face_id;
12837 it->len = saved_len;
12838 it->c = saved_c;
12839 return 1;
12840 }
12841 }
12842
12843 return 0;
12844 }
12845
12846
12847 /* Extend the face of the last glyph in the text area of IT->glyph_row
12848 to the end of the display line. Called from display_line.
12849 If the glyph row is empty, add a space glyph to it so that we
12850 know the face to draw. Set the glyph row flag fill_line_p. */
12851
12852 static void
12853 extend_face_to_end_of_line (it)
12854 struct it *it;
12855 {
12856 struct face *face;
12857 struct frame *f = it->f;
12858
12859 /* If line is already filled, do nothing. */
12860 if (it->current_x >= it->last_visible_x)
12861 return;
12862
12863 /* Face extension extends the background and box of IT->face_id
12864 to the end of the line. If the background equals the background
12865 of the frame, we don't have to do anything. */
12866 if (it->face_before_selective_p)
12867 face = FACE_FROM_ID (it->f, it->saved_face_id);
12868 else
12869 face = FACE_FROM_ID (f, it->face_id);
12870
12871 if (FRAME_WINDOW_P (f)
12872 && face->box == FACE_NO_BOX
12873 && face->background == FRAME_BACKGROUND_PIXEL (f)
12874 && !face->stipple)
12875 return;
12876
12877 /* Set the glyph row flag indicating that the face of the last glyph
12878 in the text area has to be drawn to the end of the text area. */
12879 it->glyph_row->fill_line_p = 1;
12880
12881 /* If current character of IT is not ASCII, make sure we have the
12882 ASCII face. This will be automatically undone the next time
12883 get_next_display_element returns a multibyte character. Note
12884 that the character will always be single byte in unibyte text. */
12885 if (!SINGLE_BYTE_CHAR_P (it->c))
12886 {
12887 it->face_id = FACE_FOR_CHAR (f, face, 0);
12888 }
12889
12890 if (FRAME_WINDOW_P (f))
12891 {
12892 /* If the row is empty, add a space with the current face of IT,
12893 so that we know which face to draw. */
12894 if (it->glyph_row->used[TEXT_AREA] == 0)
12895 {
12896 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
12897 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
12898 it->glyph_row->used[TEXT_AREA] = 1;
12899 }
12900 }
12901 else
12902 {
12903 /* Save some values that must not be changed. */
12904 int saved_x = it->current_x;
12905 struct text_pos saved_pos;
12906 Lisp_Object saved_object;
12907 enum display_element_type saved_what = it->what;
12908 int saved_face_id = it->face_id;
12909
12910 saved_object = it->object;
12911 saved_pos = it->position;
12912
12913 it->what = IT_CHARACTER;
12914 bzero (&it->position, sizeof it->position);
12915 it->object = make_number (0);
12916 it->c = ' ';
12917 it->len = 1;
12918 it->face_id = face->id;
12919
12920 PRODUCE_GLYPHS (it);
12921
12922 while (it->current_x <= it->last_visible_x)
12923 PRODUCE_GLYPHS (it);
12924
12925 /* Don't count these blanks really. It would let us insert a left
12926 truncation glyph below and make us set the cursor on them, maybe. */
12927 it->current_x = saved_x;
12928 it->object = saved_object;
12929 it->position = saved_pos;
12930 it->what = saved_what;
12931 it->face_id = saved_face_id;
12932 }
12933 }
12934
12935
12936 /* Value is non-zero if text starting at CHARPOS in current_buffer is
12937 trailing whitespace. */
12938
12939 static int
12940 trailing_whitespace_p (charpos)
12941 int charpos;
12942 {
12943 int bytepos = CHAR_TO_BYTE (charpos);
12944 int c = 0;
12945
12946 while (bytepos < ZV_BYTE
12947 && (c = FETCH_CHAR (bytepos),
12948 c == ' ' || c == '\t'))
12949 ++bytepos;
12950
12951 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
12952 {
12953 if (bytepos != PT_BYTE)
12954 return 1;
12955 }
12956 return 0;
12957 }
12958
12959
12960 /* Highlight trailing whitespace, if any, in ROW. */
12961
12962 void
12963 highlight_trailing_whitespace (f, row)
12964 struct frame *f;
12965 struct glyph_row *row;
12966 {
12967 int used = row->used[TEXT_AREA];
12968
12969 if (used)
12970 {
12971 struct glyph *start = row->glyphs[TEXT_AREA];
12972 struct glyph *glyph = start + used - 1;
12973
12974 /* Skip over glyphs inserted to display the cursor at the
12975 end of a line, for extending the face of the last glyph
12976 to the end of the line on terminals, and for truncation
12977 and continuation glyphs. */
12978 while (glyph >= start
12979 && glyph->type == CHAR_GLYPH
12980 && INTEGERP (glyph->object))
12981 --glyph;
12982
12983 /* If last glyph is a space or stretch, and it's trailing
12984 whitespace, set the face of all trailing whitespace glyphs in
12985 IT->glyph_row to `trailing-whitespace'. */
12986 if (glyph >= start
12987 && BUFFERP (glyph->object)
12988 && (glyph->type == STRETCH_GLYPH
12989 || (glyph->type == CHAR_GLYPH
12990 && glyph->u.ch == ' '))
12991 && trailing_whitespace_p (glyph->charpos))
12992 {
12993 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
12994
12995 while (glyph >= start
12996 && BUFFERP (glyph->object)
12997 && (glyph->type == STRETCH_GLYPH
12998 || (glyph->type == CHAR_GLYPH
12999 && glyph->u.ch == ' ')))
13000 (glyph--)->face_id = face_id;
13001 }
13002 }
13003 }
13004
13005
13006 /* Value is non-zero if glyph row ROW in window W should be
13007 used to hold the cursor. */
13008
13009 static int
13010 cursor_row_p (w, row)
13011 struct window *w;
13012 struct glyph_row *row;
13013 {
13014 int cursor_row_p = 1;
13015
13016 if (PT == MATRIX_ROW_END_CHARPOS (row))
13017 {
13018 /* If the row ends with a newline from a string, we don't want
13019 the cursor there (if the row is continued it doesn't end in a
13020 newline). */
13021 if (CHARPOS (row->end.string_pos) >= 0
13022 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
13023 cursor_row_p = row->continued_p;
13024
13025 /* If the row ends at ZV, display the cursor at the end of that
13026 row instead of at the start of the row below. */
13027 else if (row->ends_at_zv_p)
13028 cursor_row_p = 1;
13029 else
13030 cursor_row_p = 0;
13031 }
13032
13033 return cursor_row_p;
13034 }
13035
13036
13037 /* Construct the glyph row IT->glyph_row in the desired matrix of
13038 IT->w from text at the current position of IT. See dispextern.h
13039 for an overview of struct it. Value is non-zero if
13040 IT->glyph_row displays text, as opposed to a line displaying ZV
13041 only. */
13042
13043 static int
13044 display_line (it)
13045 struct it *it;
13046 {
13047 struct glyph_row *row = it->glyph_row;
13048
13049 /* We always start displaying at hpos zero even if hscrolled. */
13050 xassert (it->hpos == 0 && it->current_x == 0);
13051
13052 /* We must not display in a row that's not a text row. */
13053 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
13054 < it->w->desired_matrix->nrows);
13055
13056 /* Is IT->w showing the region? */
13057 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
13058
13059 /* Clear the result glyph row and enable it. */
13060 prepare_desired_row (row);
13061
13062 row->y = it->current_y;
13063 row->start = it->current;
13064 row->continuation_lines_width = it->continuation_lines_width;
13065 row->displays_text_p = 1;
13066 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
13067 it->starts_in_middle_of_char_p = 0;
13068
13069 /* Arrange the overlays nicely for our purposes. Usually, we call
13070 display_line on only one line at a time, in which case this
13071 can't really hurt too much, or we call it on lines which appear
13072 one after another in the buffer, in which case all calls to
13073 recenter_overlay_lists but the first will be pretty cheap. */
13074 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
13075
13076 /* Move over display elements that are not visible because we are
13077 hscrolled. This may stop at an x-position < IT->first_visible_x
13078 if the first glyph is partially visible or if we hit a line end. */
13079 if (it->current_x < it->first_visible_x)
13080 move_it_in_display_line_to (it, ZV, it->first_visible_x,
13081 MOVE_TO_POS | MOVE_TO_X);
13082
13083 /* Get the initial row height. This is either the height of the
13084 text hscrolled, if there is any, or zero. */
13085 row->ascent = it->max_ascent;
13086 row->height = it->max_ascent + it->max_descent;
13087 row->phys_ascent = it->max_phys_ascent;
13088 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
13089
13090 /* Loop generating characters. The loop is left with IT on the next
13091 character to display. */
13092 while (1)
13093 {
13094 int n_glyphs_before, hpos_before, x_before;
13095 int x, i, nglyphs;
13096 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
13097
13098 /* Retrieve the next thing to display. Value is zero if end of
13099 buffer reached. */
13100 if (!get_next_display_element (it))
13101 {
13102 /* Maybe add a space at the end of this line that is used to
13103 display the cursor there under X. Set the charpos of the
13104 first glyph of blank lines not corresponding to any text
13105 to -1. */
13106 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
13107 || row->used[TEXT_AREA] == 0)
13108 {
13109 row->glyphs[TEXT_AREA]->charpos = -1;
13110 row->displays_text_p = 0;
13111
13112 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
13113 && (!MINI_WINDOW_P (it->w)
13114 || (minibuf_level && EQ (it->window, minibuf_window))))
13115 row->indicate_empty_line_p = 1;
13116 }
13117
13118 it->continuation_lines_width = 0;
13119 row->ends_at_zv_p = 1;
13120 break;
13121 }
13122
13123 /* Now, get the metrics of what we want to display. This also
13124 generates glyphs in `row' (which is IT->glyph_row). */
13125 n_glyphs_before = row->used[TEXT_AREA];
13126 x = it->current_x;
13127
13128 /* Remember the line height so far in case the next element doesn't
13129 fit on the line. */
13130 if (!it->truncate_lines_p)
13131 {
13132 ascent = it->max_ascent;
13133 descent = it->max_descent;
13134 phys_ascent = it->max_phys_ascent;
13135 phys_descent = it->max_phys_descent;
13136 }
13137
13138 PRODUCE_GLYPHS (it);
13139
13140 /* If this display element was in marginal areas, continue with
13141 the next one. */
13142 if (it->area != TEXT_AREA)
13143 {
13144 row->ascent = max (row->ascent, it->max_ascent);
13145 row->height = max (row->height, it->max_ascent + it->max_descent);
13146 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13147 row->phys_height = max (row->phys_height,
13148 it->max_phys_ascent + it->max_phys_descent);
13149 set_iterator_to_next (it, 1);
13150 continue;
13151 }
13152
13153 /* Does the display element fit on the line? If we truncate
13154 lines, we should draw past the right edge of the window. If
13155 we don't truncate, we want to stop so that we can display the
13156 continuation glyph before the right margin. If lines are
13157 continued, there are two possible strategies for characters
13158 resulting in more than 1 glyph (e.g. tabs): Display as many
13159 glyphs as possible in this line and leave the rest for the
13160 continuation line, or display the whole element in the next
13161 line. Original redisplay did the former, so we do it also. */
13162 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
13163 hpos_before = it->hpos;
13164 x_before = x;
13165
13166 if (/* Not a newline. */
13167 nglyphs > 0
13168 /* Glyphs produced fit entirely in the line. */
13169 && it->current_x < it->last_visible_x)
13170 {
13171 it->hpos += nglyphs;
13172 row->ascent = max (row->ascent, it->max_ascent);
13173 row->height = max (row->height, it->max_ascent + it->max_descent);
13174 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13175 row->phys_height = max (row->phys_height,
13176 it->max_phys_ascent + it->max_phys_descent);
13177 if (it->current_x - it->pixel_width < it->first_visible_x)
13178 row->x = x - it->first_visible_x;
13179 }
13180 else
13181 {
13182 int new_x;
13183 struct glyph *glyph;
13184
13185 for (i = 0; i < nglyphs; ++i, x = new_x)
13186 {
13187 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
13188 new_x = x + glyph->pixel_width;
13189
13190 if (/* Lines are continued. */
13191 !it->truncate_lines_p
13192 && (/* Glyph doesn't fit on the line. */
13193 new_x > it->last_visible_x
13194 /* Or it fits exactly on a window system frame. */
13195 || (new_x == it->last_visible_x
13196 && FRAME_WINDOW_P (it->f))))
13197 {
13198 /* End of a continued line. */
13199
13200 if (it->hpos == 0
13201 || (new_x == it->last_visible_x
13202 && FRAME_WINDOW_P (it->f)))
13203 {
13204 /* Current glyph is the only one on the line or
13205 fits exactly on the line. We must continue
13206 the line because we can't draw the cursor
13207 after the glyph. */
13208 row->continued_p = 1;
13209 it->current_x = new_x;
13210 it->continuation_lines_width += new_x;
13211 ++it->hpos;
13212 if (i == nglyphs - 1)
13213 set_iterator_to_next (it, 1);
13214 }
13215 else if (CHAR_GLYPH_PADDING_P (*glyph)
13216 && !FRAME_WINDOW_P (it->f))
13217 {
13218 /* A padding glyph that doesn't fit on this line.
13219 This means the whole character doesn't fit
13220 on the line. */
13221 row->used[TEXT_AREA] = n_glyphs_before;
13222
13223 /* Fill the rest of the row with continuation
13224 glyphs like in 20.x. */
13225 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
13226 < row->glyphs[1 + TEXT_AREA])
13227 produce_special_glyphs (it, IT_CONTINUATION);
13228
13229 row->continued_p = 1;
13230 it->current_x = x_before;
13231 it->continuation_lines_width += x_before;
13232
13233 /* Restore the height to what it was before the
13234 element not fitting on the line. */
13235 it->max_ascent = ascent;
13236 it->max_descent = descent;
13237 it->max_phys_ascent = phys_ascent;
13238 it->max_phys_descent = phys_descent;
13239 }
13240 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
13241 {
13242 /* A TAB that extends past the right edge of the
13243 window. This produces a single glyph on
13244 window system frames. We leave the glyph in
13245 this row and let it fill the row, but don't
13246 consume the TAB. */
13247 it->continuation_lines_width += it->last_visible_x;
13248 row->ends_in_middle_of_char_p = 1;
13249 row->continued_p = 1;
13250 glyph->pixel_width = it->last_visible_x - x;
13251 it->starts_in_middle_of_char_p = 1;
13252 }
13253 else
13254 {
13255 /* Something other than a TAB that draws past
13256 the right edge of the window. Restore
13257 positions to values before the element. */
13258 row->used[TEXT_AREA] = n_glyphs_before + i;
13259
13260 /* Display continuation glyphs. */
13261 if (!FRAME_WINDOW_P (it->f))
13262 produce_special_glyphs (it, IT_CONTINUATION);
13263 row->continued_p = 1;
13264
13265 it->continuation_lines_width += x;
13266
13267 if (nglyphs > 1 && i > 0)
13268 {
13269 row->ends_in_middle_of_char_p = 1;
13270 it->starts_in_middle_of_char_p = 1;
13271 }
13272
13273 /* Restore the height to what it was before the
13274 element not fitting on the line. */
13275 it->max_ascent = ascent;
13276 it->max_descent = descent;
13277 it->max_phys_ascent = phys_ascent;
13278 it->max_phys_descent = phys_descent;
13279 }
13280
13281 break;
13282 }
13283 else if (new_x > it->first_visible_x)
13284 {
13285 /* Increment number of glyphs actually displayed. */
13286 ++it->hpos;
13287
13288 if (x < it->first_visible_x)
13289 /* Glyph is partially visible, i.e. row starts at
13290 negative X position. */
13291 row->x = x - it->first_visible_x;
13292 }
13293 else
13294 {
13295 /* Glyph is completely off the left margin of the
13296 window. This should not happen because of the
13297 move_it_in_display_line at the start of this
13298 function, unless the text display area of the
13299 window is empty. */
13300 xassert (it->first_visible_x <= it->last_visible_x);
13301 }
13302 }
13303
13304 row->ascent = max (row->ascent, it->max_ascent);
13305 row->height = max (row->height, it->max_ascent + it->max_descent);
13306 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13307 row->phys_height = max (row->phys_height,
13308 it->max_phys_ascent + it->max_phys_descent);
13309
13310 /* End of this display line if row is continued. */
13311 if (row->continued_p)
13312 break;
13313 }
13314
13315 /* Is this a line end? If yes, we're also done, after making
13316 sure that a non-default face is extended up to the right
13317 margin of the window. */
13318 if (ITERATOR_AT_END_OF_LINE_P (it))
13319 {
13320 int used_before = row->used[TEXT_AREA];
13321
13322 row->ends_in_newline_from_string_p = STRINGP (it->object);
13323
13324 /* Add a space at the end of the line that is used to
13325 display the cursor there. */
13326 append_space (it, 0);
13327
13328 /* Extend the face to the end of the line. */
13329 extend_face_to_end_of_line (it);
13330
13331 /* Make sure we have the position. */
13332 if (used_before == 0)
13333 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
13334
13335 /* Consume the line end. This skips over invisible lines. */
13336 set_iterator_to_next (it, 1);
13337 it->continuation_lines_width = 0;
13338 break;
13339 }
13340
13341 /* Proceed with next display element. Note that this skips
13342 over lines invisible because of selective display. */
13343 set_iterator_to_next (it, 1);
13344
13345 /* If we truncate lines, we are done when the last displayed
13346 glyphs reach past the right margin of the window. */
13347 if (it->truncate_lines_p
13348 && (FRAME_WINDOW_P (it->f)
13349 ? (it->current_x >= it->last_visible_x)
13350 : (it->current_x > it->last_visible_x)))
13351 {
13352 /* Maybe add truncation glyphs. */
13353 if (!FRAME_WINDOW_P (it->f))
13354 {
13355 int i, n;
13356
13357 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
13358 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
13359 break;
13360
13361 for (n = row->used[TEXT_AREA]; i < n; ++i)
13362 {
13363 row->used[TEXT_AREA] = i;
13364 produce_special_glyphs (it, IT_TRUNCATION);
13365 }
13366 }
13367
13368 row->truncated_on_right_p = 1;
13369 it->continuation_lines_width = 0;
13370 reseat_at_next_visible_line_start (it, 0);
13371 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
13372 it->hpos = hpos_before;
13373 it->current_x = x_before;
13374 break;
13375 }
13376 }
13377
13378 /* If line is not empty and hscrolled, maybe insert truncation glyphs
13379 at the left window margin. */
13380 if (it->first_visible_x
13381 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
13382 {
13383 if (!FRAME_WINDOW_P (it->f))
13384 insert_left_trunc_glyphs (it);
13385 row->truncated_on_left_p = 1;
13386 }
13387
13388 /* If the start of this line is the overlay arrow-position, then
13389 mark this glyph row as the one containing the overlay arrow.
13390 This is clearly a mess with variable size fonts. It would be
13391 better to let it be displayed like cursors under X. */
13392 if (MARKERP (Voverlay_arrow_position)
13393 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
13394 && (MATRIX_ROW_START_CHARPOS (row)
13395 == marker_position (Voverlay_arrow_position))
13396 && STRINGP (Voverlay_arrow_string)
13397 && ! overlay_arrow_seen)
13398 {
13399 /* Overlay arrow in window redisplay is a fringe bitmap. */
13400 if (!FRAME_WINDOW_P (it->f))
13401 {
13402 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
13403 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
13404 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
13405 struct glyph *p = row->glyphs[TEXT_AREA];
13406 struct glyph *p2, *end;
13407
13408 /* Copy the arrow glyphs. */
13409 while (glyph < arrow_end)
13410 *p++ = *glyph++;
13411
13412 /* Throw away padding glyphs. */
13413 p2 = p;
13414 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
13415 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
13416 ++p2;
13417 if (p2 > p)
13418 {
13419 while (p2 < end)
13420 *p++ = *p2++;
13421 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
13422 }
13423 }
13424
13425 overlay_arrow_seen = 1;
13426 row->overlay_arrow_p = 1;
13427 }
13428
13429 /* Compute pixel dimensions of this line. */
13430 compute_line_metrics (it);
13431
13432 /* Remember the position at which this line ends. */
13433 row->end = it->current;
13434
13435 /* Maybe set the cursor. */
13436 if (it->w->cursor.vpos < 0
13437 && PT >= MATRIX_ROW_START_CHARPOS (row)
13438 && PT <= MATRIX_ROW_END_CHARPOS (row)
13439 && cursor_row_p (it->w, row))
13440 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
13441
13442 /* Highlight trailing whitespace. */
13443 if (!NILP (Vshow_trailing_whitespace))
13444 highlight_trailing_whitespace (it->f, it->glyph_row);
13445
13446 /* Prepare for the next line. This line starts horizontally at (X
13447 HPOS) = (0 0). Vertical positions are incremented. As a
13448 convenience for the caller, IT->glyph_row is set to the next
13449 row to be used. */
13450 it->current_x = it->hpos = 0;
13451 it->current_y += row->height;
13452 ++it->vpos;
13453 ++it->glyph_row;
13454 return row->displays_text_p;
13455 }
13456
13457
13458 \f
13459 /***********************************************************************
13460 Menu Bar
13461 ***********************************************************************/
13462
13463 /* Redisplay the menu bar in the frame for window W.
13464
13465 The menu bar of X frames that don't have X toolkit support is
13466 displayed in a special window W->frame->menu_bar_window.
13467
13468 The menu bar of terminal frames is treated specially as far as
13469 glyph matrices are concerned. Menu bar lines are not part of
13470 windows, so the update is done directly on the frame matrix rows
13471 for the menu bar. */
13472
13473 static void
13474 display_menu_bar (w)
13475 struct window *w;
13476 {
13477 struct frame *f = XFRAME (WINDOW_FRAME (w));
13478 struct it it;
13479 Lisp_Object items;
13480 int i;
13481
13482 /* Don't do all this for graphical frames. */
13483 #ifdef HAVE_NTGUI
13484 if (!NILP (Vwindow_system))
13485 return;
13486 #endif
13487 #ifdef USE_X_TOOLKIT
13488 if (FRAME_X_P (f))
13489 return;
13490 #endif
13491 #ifdef MAC_OS
13492 if (FRAME_MAC_P (f))
13493 return;
13494 #endif
13495
13496 #ifdef USE_X_TOOLKIT
13497 xassert (!FRAME_WINDOW_P (f));
13498 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
13499 it.first_visible_x = 0;
13500 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
13501 #else /* not USE_X_TOOLKIT */
13502 if (FRAME_WINDOW_P (f))
13503 {
13504 /* Menu bar lines are displayed in the desired matrix of the
13505 dummy window menu_bar_window. */
13506 struct window *menu_w;
13507 xassert (WINDOWP (f->menu_bar_window));
13508 menu_w = XWINDOW (f->menu_bar_window);
13509 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
13510 MENU_FACE_ID);
13511 it.first_visible_x = 0;
13512 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
13513 }
13514 else
13515 {
13516 /* This is a TTY frame, i.e. character hpos/vpos are used as
13517 pixel x/y. */
13518 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
13519 MENU_FACE_ID);
13520 it.first_visible_x = 0;
13521 it.last_visible_x = FRAME_WIDTH (f);
13522 }
13523 #endif /* not USE_X_TOOLKIT */
13524
13525 if (! mode_line_inverse_video)
13526 /* Force the menu-bar to be displayed in the default face. */
13527 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
13528
13529 /* Clear all rows of the menu bar. */
13530 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
13531 {
13532 struct glyph_row *row = it.glyph_row + i;
13533 clear_glyph_row (row);
13534 row->enabled_p = 1;
13535 row->full_width_p = 1;
13536 }
13537
13538 /* Display all items of the menu bar. */
13539 items = FRAME_MENU_BAR_ITEMS (it.f);
13540 for (i = 0; i < XVECTOR (items)->size; i += 4)
13541 {
13542 Lisp_Object string;
13543
13544 /* Stop at nil string. */
13545 string = AREF (items, i + 1);
13546 if (NILP (string))
13547 break;
13548
13549 /* Remember where item was displayed. */
13550 AREF (items, i + 3) = make_number (it.hpos);
13551
13552 /* Display the item, pad with one space. */
13553 if (it.current_x < it.last_visible_x)
13554 display_string (NULL, string, Qnil, 0, 0, &it,
13555 SCHARS (string) + 1, 0, 0, -1);
13556 }
13557
13558 /* Fill out the line with spaces. */
13559 if (it.current_x < it.last_visible_x)
13560 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
13561
13562 /* Compute the total height of the lines. */
13563 compute_line_metrics (&it);
13564 }
13565
13566
13567 \f
13568 /***********************************************************************
13569 Mode Line
13570 ***********************************************************************/
13571
13572 /* Redisplay mode lines in the window tree whose root is WINDOW. If
13573 FORCE is non-zero, redisplay mode lines unconditionally.
13574 Otherwise, redisplay only mode lines that are garbaged. Value is
13575 the number of windows whose mode lines were redisplayed. */
13576
13577 static int
13578 redisplay_mode_lines (window, force)
13579 Lisp_Object window;
13580 int force;
13581 {
13582 int nwindows = 0;
13583
13584 while (!NILP (window))
13585 {
13586 struct window *w = XWINDOW (window);
13587
13588 if (WINDOWP (w->hchild))
13589 nwindows += redisplay_mode_lines (w->hchild, force);
13590 else if (WINDOWP (w->vchild))
13591 nwindows += redisplay_mode_lines (w->vchild, force);
13592 else if (force
13593 || FRAME_GARBAGED_P (XFRAME (w->frame))
13594 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
13595 {
13596 struct text_pos lpoint;
13597 struct buffer *old = current_buffer;
13598
13599 /* Set the window's buffer for the mode line display. */
13600 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13601 set_buffer_internal_1 (XBUFFER (w->buffer));
13602
13603 /* Point refers normally to the selected window. For any
13604 other window, set up appropriate value. */
13605 if (!EQ (window, selected_window))
13606 {
13607 struct text_pos pt;
13608
13609 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
13610 if (CHARPOS (pt) < BEGV)
13611 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
13612 else if (CHARPOS (pt) > (ZV - 1))
13613 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
13614 else
13615 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
13616 }
13617
13618 /* Display mode lines. */
13619 clear_glyph_matrix (w->desired_matrix);
13620 if (display_mode_lines (w))
13621 {
13622 ++nwindows;
13623 w->must_be_updated_p = 1;
13624 }
13625
13626 /* Restore old settings. */
13627 set_buffer_internal_1 (old);
13628 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13629 }
13630
13631 window = w->next;
13632 }
13633
13634 return nwindows;
13635 }
13636
13637
13638 /* Display the mode and/or top line of window W. Value is the number
13639 of mode lines displayed. */
13640
13641 static int
13642 display_mode_lines (w)
13643 struct window *w;
13644 {
13645 Lisp_Object old_selected_window, old_selected_frame;
13646 int n = 0;
13647
13648 old_selected_frame = selected_frame;
13649 selected_frame = w->frame;
13650 old_selected_window = selected_window;
13651 XSETWINDOW (selected_window, w);
13652
13653 /* These will be set while the mode line specs are processed. */
13654 line_number_displayed = 0;
13655 w->column_number_displayed = Qnil;
13656
13657 if (WINDOW_WANTS_MODELINE_P (w))
13658 {
13659 struct window *sel_w = XWINDOW (old_selected_window);
13660
13661 /* Select mode line face based on the real selected window. */
13662 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
13663 current_buffer->mode_line_format);
13664 ++n;
13665 }
13666
13667 if (WINDOW_WANTS_HEADER_LINE_P (w))
13668 {
13669 display_mode_line (w, HEADER_LINE_FACE_ID,
13670 current_buffer->header_line_format);
13671 ++n;
13672 }
13673
13674 selected_frame = old_selected_frame;
13675 selected_window = old_selected_window;
13676 return n;
13677 }
13678
13679
13680 /* Display mode or top line of window W. FACE_ID specifies which line
13681 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
13682 FORMAT is the mode line format to display. Value is the pixel
13683 height of the mode line displayed. */
13684
13685 static int
13686 display_mode_line (w, face_id, format)
13687 struct window *w;
13688 enum face_id face_id;
13689 Lisp_Object format;
13690 {
13691 struct it it;
13692 struct face *face;
13693
13694 init_iterator (&it, w, -1, -1, NULL, face_id);
13695 prepare_desired_row (it.glyph_row);
13696
13697 if (! mode_line_inverse_video)
13698 /* Force the mode-line to be displayed in the default face. */
13699 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
13700
13701 /* Temporarily make frame's keyboard the current kboard so that
13702 kboard-local variables in the mode_line_format will get the right
13703 values. */
13704 push_frame_kboard (it.f);
13705 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
13706 pop_frame_kboard ();
13707
13708 /* Fill up with spaces. */
13709 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
13710
13711 compute_line_metrics (&it);
13712 it.glyph_row->full_width_p = 1;
13713 it.glyph_row->mode_line_p = 1;
13714 it.glyph_row->continued_p = 0;
13715 it.glyph_row->truncated_on_left_p = 0;
13716 it.glyph_row->truncated_on_right_p = 0;
13717
13718 /* Make a 3D mode-line have a shadow at its right end. */
13719 face = FACE_FROM_ID (it.f, face_id);
13720 extend_face_to_end_of_line (&it);
13721 if (face->box != FACE_NO_BOX)
13722 {
13723 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
13724 + it.glyph_row->used[TEXT_AREA] - 1);
13725 last->right_box_line_p = 1;
13726 }
13727
13728 return it.glyph_row->height;
13729 }
13730
13731 /* Alist that caches the results of :propertize.
13732 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
13733 Lisp_Object mode_line_proptrans_alist;
13734
13735 /* List of strings making up the mode-line. */
13736 Lisp_Object mode_line_string_list;
13737
13738 /* Base face property when building propertized mode line string. */
13739 static Lisp_Object mode_line_string_face;
13740 static Lisp_Object mode_line_string_face_prop;
13741
13742
13743 /* Contribute ELT to the mode line for window IT->w. How it
13744 translates into text depends on its data type.
13745
13746 IT describes the display environment in which we display, as usual.
13747
13748 DEPTH is the depth in recursion. It is used to prevent
13749 infinite recursion here.
13750
13751 FIELD_WIDTH is the number of characters the display of ELT should
13752 occupy in the mode line, and PRECISION is the maximum number of
13753 characters to display from ELT's representation. See
13754 display_string for details.
13755
13756 Returns the hpos of the end of the text generated by ELT.
13757
13758 PROPS is a property list to add to any string we encounter.
13759
13760 If RISKY is nonzero, remove (disregard) any properties in any string
13761 we encounter, and ignore :eval and :propertize.
13762
13763 If the global variable `frame_title_ptr' is non-NULL, then the output
13764 is passed to `store_frame_title' instead of `display_string'. */
13765
13766 static int
13767 display_mode_element (it, depth, field_width, precision, elt, props, risky)
13768 struct it *it;
13769 int depth;
13770 int field_width, precision;
13771 Lisp_Object elt, props;
13772 int risky;
13773 {
13774 int n = 0, field, prec;
13775 int literal = 0;
13776
13777 tail_recurse:
13778 if (depth > 10)
13779 goto invalid;
13780
13781 depth++;
13782
13783 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
13784 {
13785 case Lisp_String:
13786 {
13787 /* A string: output it and check for %-constructs within it. */
13788 unsigned char c;
13789 const unsigned char *this, *lisp_string;
13790
13791 if (!NILP (props) || risky)
13792 {
13793 Lisp_Object oprops, aelt;
13794 oprops = Ftext_properties_at (make_number (0), elt);
13795
13796 if (NILP (Fequal (props, oprops)) || risky)
13797 {
13798 /* If the starting string has properties,
13799 merge the specified ones onto the existing ones. */
13800 if (! NILP (oprops) && !risky)
13801 {
13802 Lisp_Object tem;
13803
13804 oprops = Fcopy_sequence (oprops);
13805 tem = props;
13806 while (CONSP (tem))
13807 {
13808 oprops = Fplist_put (oprops, XCAR (tem),
13809 XCAR (XCDR (tem)));
13810 tem = XCDR (XCDR (tem));
13811 }
13812 props = oprops;
13813 }
13814
13815 aelt = Fassoc (elt, mode_line_proptrans_alist);
13816 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
13817 {
13818 mode_line_proptrans_alist
13819 = Fcons (aelt, Fdelq (aelt, mode_line_proptrans_alist));
13820 elt = XCAR (aelt);
13821 }
13822 else
13823 {
13824 Lisp_Object tem;
13825
13826 elt = Fcopy_sequence (elt);
13827 Fset_text_properties (make_number (0), Flength (elt),
13828 props, elt);
13829 /* Add this item to mode_line_proptrans_alist. */
13830 mode_line_proptrans_alist
13831 = Fcons (Fcons (elt, props),
13832 mode_line_proptrans_alist);
13833 /* Truncate mode_line_proptrans_alist
13834 to at most 50 elements. */
13835 tem = Fnthcdr (make_number (50),
13836 mode_line_proptrans_alist);
13837 if (! NILP (tem))
13838 XSETCDR (tem, Qnil);
13839 }
13840 }
13841 }
13842
13843 this = SDATA (elt);
13844 lisp_string = this;
13845
13846 if (literal)
13847 {
13848 prec = precision - n;
13849 if (frame_title_ptr)
13850 n += store_frame_title (SDATA (elt), -1, prec);
13851 else if (!NILP (mode_line_string_list))
13852 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
13853 else
13854 n += display_string (NULL, elt, Qnil, 0, 0, it,
13855 0, prec, 0, STRING_MULTIBYTE (elt));
13856
13857 break;
13858 }
13859
13860 while ((precision <= 0 || n < precision)
13861 && *this
13862 && (frame_title_ptr
13863 || !NILP (mode_line_string_list)
13864 || it->current_x < it->last_visible_x))
13865 {
13866 const unsigned char *last = this;
13867
13868 /* Advance to end of string or next format specifier. */
13869 while ((c = *this++) != '\0' && c != '%')
13870 ;
13871
13872 if (this - 1 != last)
13873 {
13874 /* Output to end of string or up to '%'. Field width
13875 is length of string. Don't output more than
13876 PRECISION allows us. */
13877 --this;
13878
13879 prec = chars_in_text (last, this - last);
13880 if (precision > 0 && prec > precision - n)
13881 prec = precision - n;
13882
13883 if (frame_title_ptr)
13884 n += store_frame_title (last, 0, prec);
13885 else if (!NILP (mode_line_string_list))
13886 {
13887 int bytepos = last - lisp_string;
13888 int charpos = string_byte_to_char (elt, bytepos);
13889 n += store_mode_line_string (NULL,
13890 Fsubstring (elt, make_number (charpos),
13891 make_number (charpos + prec)),
13892 0, 0, 0, Qnil);
13893 }
13894 else
13895 {
13896 int bytepos = last - lisp_string;
13897 int charpos = string_byte_to_char (elt, bytepos);
13898 n += display_string (NULL, elt, Qnil, 0, charpos,
13899 it, 0, prec, 0,
13900 STRING_MULTIBYTE (elt));
13901 }
13902 }
13903 else /* c == '%' */
13904 {
13905 const unsigned char *percent_position = this;
13906
13907 /* Get the specified minimum width. Zero means
13908 don't pad. */
13909 field = 0;
13910 while ((c = *this++) >= '0' && c <= '9')
13911 field = field * 10 + c - '0';
13912
13913 /* Don't pad beyond the total padding allowed. */
13914 if (field_width - n > 0 && field > field_width - n)
13915 field = field_width - n;
13916
13917 /* Note that either PRECISION <= 0 or N < PRECISION. */
13918 prec = precision - n;
13919
13920 if (c == 'M')
13921 n += display_mode_element (it, depth, field, prec,
13922 Vglobal_mode_string, props,
13923 risky);
13924 else if (c != 0)
13925 {
13926 int multibyte;
13927 int bytepos, charpos;
13928 unsigned char *spec;
13929
13930 bytepos = percent_position - lisp_string;
13931 charpos = (STRING_MULTIBYTE (elt)
13932 ? string_byte_to_char (elt, bytepos)
13933 : bytepos);
13934
13935 spec
13936 = decode_mode_spec (it->w, c, field, prec, &multibyte);
13937
13938 if (frame_title_ptr)
13939 n += store_frame_title (spec, field, prec);
13940 else if (!NILP (mode_line_string_list))
13941 {
13942 int len = strlen (spec);
13943 Lisp_Object tem = make_string (spec, len);
13944 props = Ftext_properties_at (make_number (charpos), elt);
13945 /* Should only keep face property in props */
13946 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
13947 }
13948 else
13949 {
13950 int nglyphs_before, nwritten;
13951
13952 nglyphs_before = it->glyph_row->used[TEXT_AREA];
13953 nwritten = display_string (spec, Qnil, elt,
13954 charpos, 0, it,
13955 field, prec, 0,
13956 multibyte);
13957
13958 /* Assign to the glyphs written above the
13959 string where the `%x' came from, position
13960 of the `%'. */
13961 if (nwritten > 0)
13962 {
13963 struct glyph *glyph
13964 = (it->glyph_row->glyphs[TEXT_AREA]
13965 + nglyphs_before);
13966 int i;
13967
13968 for (i = 0; i < nwritten; ++i)
13969 {
13970 glyph[i].object = elt;
13971 glyph[i].charpos = charpos;
13972 }
13973
13974 n += nwritten;
13975 }
13976 }
13977 }
13978 else /* c == 0 */
13979 break;
13980 }
13981 }
13982 }
13983 break;
13984
13985 case Lisp_Symbol:
13986 /* A symbol: process the value of the symbol recursively
13987 as if it appeared here directly. Avoid error if symbol void.
13988 Special case: if value of symbol is a string, output the string
13989 literally. */
13990 {
13991 register Lisp_Object tem;
13992
13993 /* If the variable is not marked as risky to set
13994 then its contents are risky to use. */
13995 if (NILP (Fget (elt, Qrisky_local_variable)))
13996 risky = 1;
13997
13998 tem = Fboundp (elt);
13999 if (!NILP (tem))
14000 {
14001 tem = Fsymbol_value (elt);
14002 /* If value is a string, output that string literally:
14003 don't check for % within it. */
14004 if (STRINGP (tem))
14005 literal = 1;
14006
14007 if (!EQ (tem, elt))
14008 {
14009 /* Give up right away for nil or t. */
14010 elt = tem;
14011 goto tail_recurse;
14012 }
14013 }
14014 }
14015 break;
14016
14017 case Lisp_Cons:
14018 {
14019 register Lisp_Object car, tem;
14020
14021 /* A cons cell: five distinct cases.
14022 If first element is :eval or :propertize, do something special.
14023 If first element is a string or a cons, process all the elements
14024 and effectively concatenate them.
14025 If first element is a negative number, truncate displaying cdr to
14026 at most that many characters. If positive, pad (with spaces)
14027 to at least that many characters.
14028 If first element is a symbol, process the cadr or caddr recursively
14029 according to whether the symbol's value is non-nil or nil. */
14030 car = XCAR (elt);
14031 if (EQ (car, QCeval))
14032 {
14033 /* An element of the form (:eval FORM) means evaluate FORM
14034 and use the result as mode line elements. */
14035
14036 if (risky)
14037 break;
14038
14039 if (CONSP (XCDR (elt)))
14040 {
14041 Lisp_Object spec;
14042 spec = safe_eval (XCAR (XCDR (elt)));
14043 n += display_mode_element (it, depth, field_width - n,
14044 precision - n, spec, props,
14045 risky);
14046 }
14047 }
14048 else if (EQ (car, QCpropertize))
14049 {
14050 /* An element of the form (:propertize ELT PROPS...)
14051 means display ELT but applying properties PROPS. */
14052
14053 if (risky)
14054 break;
14055
14056 if (CONSP (XCDR (elt)))
14057 n += display_mode_element (it, depth, field_width - n,
14058 precision - n, XCAR (XCDR (elt)),
14059 XCDR (XCDR (elt)), risky);
14060 }
14061 else if (SYMBOLP (car))
14062 {
14063 tem = Fboundp (car);
14064 elt = XCDR (elt);
14065 if (!CONSP (elt))
14066 goto invalid;
14067 /* elt is now the cdr, and we know it is a cons cell.
14068 Use its car if CAR has a non-nil value. */
14069 if (!NILP (tem))
14070 {
14071 tem = Fsymbol_value (car);
14072 if (!NILP (tem))
14073 {
14074 elt = XCAR (elt);
14075 goto tail_recurse;
14076 }
14077 }
14078 /* Symbol's value is nil (or symbol is unbound)
14079 Get the cddr of the original list
14080 and if possible find the caddr and use that. */
14081 elt = XCDR (elt);
14082 if (NILP (elt))
14083 break;
14084 else if (!CONSP (elt))
14085 goto invalid;
14086 elt = XCAR (elt);
14087 goto tail_recurse;
14088 }
14089 else if (INTEGERP (car))
14090 {
14091 register int lim = XINT (car);
14092 elt = XCDR (elt);
14093 if (lim < 0)
14094 {
14095 /* Negative int means reduce maximum width. */
14096 if (precision <= 0)
14097 precision = -lim;
14098 else
14099 precision = min (precision, -lim);
14100 }
14101 else if (lim > 0)
14102 {
14103 /* Padding specified. Don't let it be more than
14104 current maximum. */
14105 if (precision > 0)
14106 lim = min (precision, lim);
14107
14108 /* If that's more padding than already wanted, queue it.
14109 But don't reduce padding already specified even if
14110 that is beyond the current truncation point. */
14111 field_width = max (lim, field_width);
14112 }
14113 goto tail_recurse;
14114 }
14115 else if (STRINGP (car) || CONSP (car))
14116 {
14117 register int limit = 50;
14118 /* Limit is to protect against circular lists. */
14119 while (CONSP (elt)
14120 && --limit > 0
14121 && (precision <= 0 || n < precision))
14122 {
14123 n += display_mode_element (it, depth, field_width - n,
14124 precision - n, XCAR (elt),
14125 props, risky);
14126 elt = XCDR (elt);
14127 }
14128 }
14129 }
14130 break;
14131
14132 default:
14133 invalid:
14134 if (frame_title_ptr)
14135 n += store_frame_title ("*invalid*", 0, precision - n);
14136 else if (!NILP (mode_line_string_list))
14137 n += store_mode_line_string ("*invalid*", Qnil, 0, 0, precision - n, Qnil);
14138 else
14139 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
14140 precision - n, 0, 0);
14141 return n;
14142 }
14143
14144 /* Pad to FIELD_WIDTH. */
14145 if (field_width > 0 && n < field_width)
14146 {
14147 if (frame_title_ptr)
14148 n += store_frame_title ("", field_width - n, 0);
14149 else if (!NILP (mode_line_string_list))
14150 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
14151 else
14152 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
14153 0, 0, 0);
14154 }
14155
14156 return n;
14157 }
14158
14159 /* Store a mode-line string element in mode_line_string_list.
14160
14161 If STRING is non-null, display that C string. Otherwise, the Lisp
14162 string LISP_STRING is displayed.
14163
14164 FIELD_WIDTH is the minimum number of output glyphs to produce.
14165 If STRING has fewer characters than FIELD_WIDTH, pad to the right
14166 with spaces. FIELD_WIDTH <= 0 means don't pad.
14167
14168 PRECISION is the maximum number of characters to output from
14169 STRING. PRECISION <= 0 means don't truncate the string.
14170
14171 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
14172 properties to the string.
14173
14174 PROPS are the properties to add to the string.
14175 The mode_line_string_face face property is always added to the string.
14176 */
14177
14178 static int store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
14179 char *string;
14180 Lisp_Object lisp_string;
14181 int copy_string;
14182 int field_width;
14183 int precision;
14184 Lisp_Object props;
14185 {
14186 int len;
14187 int n = 0;
14188
14189 if (string != NULL)
14190 {
14191 len = strlen (string);
14192 if (precision > 0 && len > precision)
14193 len = precision;
14194 lisp_string = make_string (string, len);
14195 if (NILP (props))
14196 props = mode_line_string_face_prop;
14197 else if (!NILP (mode_line_string_face))
14198 {
14199 Lisp_Object face = Fplist_get (props, Qface);
14200 props = Fcopy_sequence (props);
14201 if (NILP (face))
14202 face = mode_line_string_face;
14203 else
14204 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
14205 props = Fplist_put (props, Qface, face);
14206 }
14207 Fadd_text_properties (make_number (0), make_number (len),
14208 props, lisp_string);
14209 }
14210 else
14211 {
14212 len = XFASTINT (Flength (lisp_string));
14213 if (precision > 0 && len > precision)
14214 {
14215 len = precision;
14216 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
14217 precision = -1;
14218 }
14219 if (!NILP (mode_line_string_face))
14220 {
14221 Lisp_Object face;
14222 if (NILP (props))
14223 props = Ftext_properties_at (make_number (0), lisp_string);
14224 face = Fplist_get (props, Qface);
14225 if (NILP (face))
14226 face = mode_line_string_face;
14227 else
14228 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
14229 props = Fcons (Qface, Fcons (face, Qnil));
14230 if (copy_string)
14231 lisp_string = Fcopy_sequence (lisp_string);
14232 }
14233 if (!NILP (props))
14234 Fadd_text_properties (make_number (0), make_number (len),
14235 props, lisp_string);
14236 }
14237
14238 if (len > 0)
14239 {
14240 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
14241 n += len;
14242 }
14243
14244 if (field_width > len)
14245 {
14246 field_width -= len;
14247 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
14248 if (!NILP (props))
14249 Fadd_text_properties (make_number (0), make_number (field_width),
14250 props, lisp_string);
14251 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
14252 n += field_width;
14253 }
14254
14255 return n;
14256 }
14257
14258
14259 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
14260 0, 3, 0,
14261 doc: /* Return the mode-line of selected window as a string.
14262 First optional arg FORMAT specifies a different format string (see
14263 `mode-line-format' for details) to use. If FORMAT is t, return
14264 the buffer's header-line. Second optional arg WINDOW specifies a
14265 different window to use as the context for the formatting.
14266 If third optional arg NO-PROPS is non-nil, string is not propertized. */)
14267 (format, window, no_props)
14268 Lisp_Object format, window, no_props;
14269 {
14270 struct it it;
14271 int len;
14272 struct window *w;
14273 struct buffer *old_buffer = NULL;
14274 enum face_id face_id = DEFAULT_FACE_ID;
14275
14276 if (NILP (window))
14277 window = selected_window;
14278 CHECK_WINDOW (window);
14279 w = XWINDOW (window);
14280 CHECK_BUFFER (w->buffer);
14281
14282 if (XBUFFER (w->buffer) != current_buffer)
14283 {
14284 old_buffer = current_buffer;
14285 set_buffer_internal_1 (XBUFFER (w->buffer));
14286 }
14287
14288 if (NILP (format) || EQ (format, Qt))
14289 {
14290 face_id = NILP (format)
14291 ? CURRENT_MODE_LINE_FACE_ID (w) :
14292 HEADER_LINE_FACE_ID;
14293 format = NILP (format)
14294 ? current_buffer->mode_line_format
14295 : current_buffer->header_line_format;
14296 }
14297
14298 init_iterator (&it, w, -1, -1, NULL, face_id);
14299
14300 if (NILP (no_props))
14301 {
14302 mode_line_string_face =
14303 (face_id == MODE_LINE_FACE_ID ? Qmode_line :
14304 face_id == MODE_LINE_INACTIVE_FACE_ID ? Qmode_line_inactive :
14305 face_id == HEADER_LINE_FACE_ID ? Qheader_line : Qnil);
14306
14307 mode_line_string_face_prop =
14308 NILP (mode_line_string_face) ? Qnil :
14309 Fcons (Qface, Fcons (mode_line_string_face, Qnil));
14310
14311 /* We need a dummy last element in mode_line_string_list to
14312 indicate we are building the propertized mode-line string.
14313 Using mode_line_string_face_prop here GC protects it. */
14314 mode_line_string_list =
14315 Fcons (mode_line_string_face_prop, Qnil);
14316 frame_title_ptr = NULL;
14317 }
14318 else
14319 {
14320 mode_line_string_face_prop = Qnil;
14321 mode_line_string_list = Qnil;
14322 frame_title_ptr = frame_title_buf;
14323 }
14324
14325 push_frame_kboard (it.f);
14326 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
14327 pop_frame_kboard ();
14328
14329 if (old_buffer)
14330 set_buffer_internal_1 (old_buffer);
14331
14332 if (NILP (no_props))
14333 {
14334 Lisp_Object str;
14335 mode_line_string_list = Fnreverse (mode_line_string_list);
14336 str = Fmapconcat (intern ("identity"), XCDR (mode_line_string_list),
14337 make_string ("", 0));
14338 mode_line_string_face_prop = Qnil;
14339 mode_line_string_list = Qnil;
14340 return str;
14341 }
14342
14343 len = frame_title_ptr - frame_title_buf;
14344 if (len > 0 && frame_title_ptr[-1] == '-')
14345 {
14346 /* Mode lines typically ends with numerous dashes; reduce to two dashes. */
14347 while (frame_title_ptr > frame_title_buf && *--frame_title_ptr == '-')
14348 ;
14349 frame_title_ptr += 3; /* restore last non-dash + two dashes */
14350 if (len > frame_title_ptr - frame_title_buf)
14351 len = frame_title_ptr - frame_title_buf;
14352 }
14353
14354 frame_title_ptr = NULL;
14355 return make_string (frame_title_buf, len);
14356 }
14357
14358 /* Write a null-terminated, right justified decimal representation of
14359 the positive integer D to BUF using a minimal field width WIDTH. */
14360
14361 static void
14362 pint2str (buf, width, d)
14363 register char *buf;
14364 register int width;
14365 register int d;
14366 {
14367 register char *p = buf;
14368
14369 if (d <= 0)
14370 *p++ = '0';
14371 else
14372 {
14373 while (d > 0)
14374 {
14375 *p++ = d % 10 + '0';
14376 d /= 10;
14377 }
14378 }
14379
14380 for (width -= (int) (p - buf); width > 0; --width)
14381 *p++ = ' ';
14382 *p-- = '\0';
14383 while (p > buf)
14384 {
14385 d = *buf;
14386 *buf++ = *p;
14387 *p-- = d;
14388 }
14389 }
14390
14391 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
14392 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
14393 type of CODING_SYSTEM. Return updated pointer into BUF. */
14394
14395 static unsigned char invalid_eol_type[] = "(*invalid*)";
14396
14397 static char *
14398 decode_mode_spec_coding (coding_system, buf, eol_flag)
14399 Lisp_Object coding_system;
14400 register char *buf;
14401 int eol_flag;
14402 {
14403 Lisp_Object val;
14404 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
14405 const unsigned char *eol_str;
14406 int eol_str_len;
14407 /* The EOL conversion we are using. */
14408 Lisp_Object eoltype;
14409
14410 val = Fget (coding_system, Qcoding_system);
14411 eoltype = Qnil;
14412
14413 if (!VECTORP (val)) /* Not yet decided. */
14414 {
14415 if (multibyte)
14416 *buf++ = '-';
14417 if (eol_flag)
14418 eoltype = eol_mnemonic_undecided;
14419 /* Don't mention EOL conversion if it isn't decided. */
14420 }
14421 else
14422 {
14423 Lisp_Object eolvalue;
14424
14425 eolvalue = Fget (coding_system, Qeol_type);
14426
14427 if (multibyte)
14428 *buf++ = XFASTINT (AREF (val, 1));
14429
14430 if (eol_flag)
14431 {
14432 /* The EOL conversion that is normal on this system. */
14433
14434 if (NILP (eolvalue)) /* Not yet decided. */
14435 eoltype = eol_mnemonic_undecided;
14436 else if (VECTORP (eolvalue)) /* Not yet decided. */
14437 eoltype = eol_mnemonic_undecided;
14438 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
14439 eoltype = (XFASTINT (eolvalue) == 0
14440 ? eol_mnemonic_unix
14441 : (XFASTINT (eolvalue) == 1
14442 ? eol_mnemonic_dos : eol_mnemonic_mac));
14443 }
14444 }
14445
14446 if (eol_flag)
14447 {
14448 /* Mention the EOL conversion if it is not the usual one. */
14449 if (STRINGP (eoltype))
14450 {
14451 eol_str = SDATA (eoltype);
14452 eol_str_len = SBYTES (eoltype);
14453 }
14454 else if (INTEGERP (eoltype)
14455 && CHAR_VALID_P (XINT (eoltype), 0))
14456 {
14457 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
14458 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
14459 eol_str = tmp;
14460 }
14461 else
14462 {
14463 eol_str = invalid_eol_type;
14464 eol_str_len = sizeof (invalid_eol_type) - 1;
14465 }
14466 bcopy (eol_str, buf, eol_str_len);
14467 buf += eol_str_len;
14468 }
14469
14470 return buf;
14471 }
14472
14473 /* Return a string for the output of a mode line %-spec for window W,
14474 generated by character C. PRECISION >= 0 means don't return a
14475 string longer than that value. FIELD_WIDTH > 0 means pad the
14476 string returned with spaces to that value. Return 1 in *MULTIBYTE
14477 if the result is multibyte text. */
14478
14479 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
14480
14481 static char *
14482 decode_mode_spec (w, c, field_width, precision, multibyte)
14483 struct window *w;
14484 register int c;
14485 int field_width, precision;
14486 int *multibyte;
14487 {
14488 Lisp_Object obj;
14489 struct frame *f = XFRAME (WINDOW_FRAME (w));
14490 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
14491 struct buffer *b = XBUFFER (w->buffer);
14492
14493 obj = Qnil;
14494 *multibyte = 0;
14495
14496 switch (c)
14497 {
14498 case '*':
14499 if (!NILP (b->read_only))
14500 return "%";
14501 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
14502 return "*";
14503 return "-";
14504
14505 case '+':
14506 /* This differs from %* only for a modified read-only buffer. */
14507 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
14508 return "*";
14509 if (!NILP (b->read_only))
14510 return "%";
14511 return "-";
14512
14513 case '&':
14514 /* This differs from %* in ignoring read-only-ness. */
14515 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
14516 return "*";
14517 return "-";
14518
14519 case '%':
14520 return "%";
14521
14522 case '[':
14523 {
14524 int i;
14525 char *p;
14526
14527 if (command_loop_level > 5)
14528 return "[[[... ";
14529 p = decode_mode_spec_buf;
14530 for (i = 0; i < command_loop_level; i++)
14531 *p++ = '[';
14532 *p = 0;
14533 return decode_mode_spec_buf;
14534 }
14535
14536 case ']':
14537 {
14538 int i;
14539 char *p;
14540
14541 if (command_loop_level > 5)
14542 return " ...]]]";
14543 p = decode_mode_spec_buf;
14544 for (i = 0; i < command_loop_level; i++)
14545 *p++ = ']';
14546 *p = 0;
14547 return decode_mode_spec_buf;
14548 }
14549
14550 case '-':
14551 {
14552 register int i;
14553
14554 /* Let lots_of_dashes be a string of infinite length. */
14555 if (!NILP (mode_line_string_list))
14556 return "--";
14557 if (field_width <= 0
14558 || field_width > sizeof (lots_of_dashes))
14559 {
14560 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
14561 decode_mode_spec_buf[i] = '-';
14562 decode_mode_spec_buf[i] = '\0';
14563 return decode_mode_spec_buf;
14564 }
14565 else
14566 return lots_of_dashes;
14567 }
14568
14569 case 'b':
14570 obj = b->name;
14571 break;
14572
14573 case 'c':
14574 {
14575 int col = (int) current_column (); /* iftc */
14576 w->column_number_displayed = make_number (col);
14577 pint2str (decode_mode_spec_buf, field_width, col);
14578 return decode_mode_spec_buf;
14579 }
14580
14581 case 'F':
14582 /* %F displays the frame name. */
14583 if (!NILP (f->title))
14584 return (char *) SDATA (f->title);
14585 if (f->explicit_name || ! FRAME_WINDOW_P (f))
14586 return (char *) SDATA (f->name);
14587 return "Emacs";
14588
14589 case 'f':
14590 obj = b->filename;
14591 break;
14592
14593 case 'l':
14594 {
14595 int startpos = XMARKER (w->start)->charpos;
14596 int startpos_byte = marker_byte_position (w->start);
14597 int line, linepos, linepos_byte, topline;
14598 int nlines, junk;
14599 int height = XFASTINT (w->height);
14600
14601 /* If we decided that this buffer isn't suitable for line numbers,
14602 don't forget that too fast. */
14603 if (EQ (w->base_line_pos, w->buffer))
14604 goto no_value;
14605 /* But do forget it, if the window shows a different buffer now. */
14606 else if (BUFFERP (w->base_line_pos))
14607 w->base_line_pos = Qnil;
14608
14609 /* If the buffer is very big, don't waste time. */
14610 if (INTEGERP (Vline_number_display_limit)
14611 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
14612 {
14613 w->base_line_pos = Qnil;
14614 w->base_line_number = Qnil;
14615 goto no_value;
14616 }
14617
14618 if (!NILP (w->base_line_number)
14619 && !NILP (w->base_line_pos)
14620 && XFASTINT (w->base_line_pos) <= startpos)
14621 {
14622 line = XFASTINT (w->base_line_number);
14623 linepos = XFASTINT (w->base_line_pos);
14624 linepos_byte = buf_charpos_to_bytepos (b, linepos);
14625 }
14626 else
14627 {
14628 line = 1;
14629 linepos = BUF_BEGV (b);
14630 linepos_byte = BUF_BEGV_BYTE (b);
14631 }
14632
14633 /* Count lines from base line to window start position. */
14634 nlines = display_count_lines (linepos, linepos_byte,
14635 startpos_byte,
14636 startpos, &junk);
14637
14638 topline = nlines + line;
14639
14640 /* Determine a new base line, if the old one is too close
14641 or too far away, or if we did not have one.
14642 "Too close" means it's plausible a scroll-down would
14643 go back past it. */
14644 if (startpos == BUF_BEGV (b))
14645 {
14646 w->base_line_number = make_number (topline);
14647 w->base_line_pos = make_number (BUF_BEGV (b));
14648 }
14649 else if (nlines < height + 25 || nlines > height * 3 + 50
14650 || linepos == BUF_BEGV (b))
14651 {
14652 int limit = BUF_BEGV (b);
14653 int limit_byte = BUF_BEGV_BYTE (b);
14654 int position;
14655 int distance = (height * 2 + 30) * line_number_display_limit_width;
14656
14657 if (startpos - distance > limit)
14658 {
14659 limit = startpos - distance;
14660 limit_byte = CHAR_TO_BYTE (limit);
14661 }
14662
14663 nlines = display_count_lines (startpos, startpos_byte,
14664 limit_byte,
14665 - (height * 2 + 30),
14666 &position);
14667 /* If we couldn't find the lines we wanted within
14668 line_number_display_limit_width chars per line,
14669 give up on line numbers for this window. */
14670 if (position == limit_byte && limit == startpos - distance)
14671 {
14672 w->base_line_pos = w->buffer;
14673 w->base_line_number = Qnil;
14674 goto no_value;
14675 }
14676
14677 w->base_line_number = make_number (topline - nlines);
14678 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
14679 }
14680
14681 /* Now count lines from the start pos to point. */
14682 nlines = display_count_lines (startpos, startpos_byte,
14683 PT_BYTE, PT, &junk);
14684
14685 /* Record that we did display the line number. */
14686 line_number_displayed = 1;
14687
14688 /* Make the string to show. */
14689 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
14690 return decode_mode_spec_buf;
14691 no_value:
14692 {
14693 char* p = decode_mode_spec_buf;
14694 int pad = field_width - 2;
14695 while (pad-- > 0)
14696 *p++ = ' ';
14697 *p++ = '?';
14698 *p++ = '?';
14699 *p = '\0';
14700 return decode_mode_spec_buf;
14701 }
14702 }
14703 break;
14704
14705 case 'm':
14706 obj = b->mode_name;
14707 break;
14708
14709 case 'n':
14710 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
14711 return " Narrow";
14712 break;
14713
14714 case 'p':
14715 {
14716 int pos = marker_position (w->start);
14717 int total = BUF_ZV (b) - BUF_BEGV (b);
14718
14719 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
14720 {
14721 if (pos <= BUF_BEGV (b))
14722 return "All";
14723 else
14724 return "Bottom";
14725 }
14726 else if (pos <= BUF_BEGV (b))
14727 return "Top";
14728 else
14729 {
14730 if (total > 1000000)
14731 /* Do it differently for a large value, to avoid overflow. */
14732 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
14733 else
14734 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
14735 /* We can't normally display a 3-digit number,
14736 so get us a 2-digit number that is close. */
14737 if (total == 100)
14738 total = 99;
14739 sprintf (decode_mode_spec_buf, "%2d%%", total);
14740 return decode_mode_spec_buf;
14741 }
14742 }
14743
14744 /* Display percentage of size above the bottom of the screen. */
14745 case 'P':
14746 {
14747 int toppos = marker_position (w->start);
14748 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
14749 int total = BUF_ZV (b) - BUF_BEGV (b);
14750
14751 if (botpos >= BUF_ZV (b))
14752 {
14753 if (toppos <= BUF_BEGV (b))
14754 return "All";
14755 else
14756 return "Bottom";
14757 }
14758 else
14759 {
14760 if (total > 1000000)
14761 /* Do it differently for a large value, to avoid overflow. */
14762 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
14763 else
14764 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
14765 /* We can't normally display a 3-digit number,
14766 so get us a 2-digit number that is close. */
14767 if (total == 100)
14768 total = 99;
14769 if (toppos <= BUF_BEGV (b))
14770 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
14771 else
14772 sprintf (decode_mode_spec_buf, "%2d%%", total);
14773 return decode_mode_spec_buf;
14774 }
14775 }
14776
14777 case 's':
14778 /* status of process */
14779 obj = Fget_buffer_process (w->buffer);
14780 if (NILP (obj))
14781 return "no process";
14782 #ifdef subprocesses
14783 obj = Fsymbol_name (Fprocess_status (obj));
14784 #endif
14785 break;
14786
14787 case 't': /* indicate TEXT or BINARY */
14788 #ifdef MODE_LINE_BINARY_TEXT
14789 return MODE_LINE_BINARY_TEXT (b);
14790 #else
14791 return "T";
14792 #endif
14793
14794 case 'z':
14795 /* coding-system (not including end-of-line format) */
14796 case 'Z':
14797 /* coding-system (including end-of-line type) */
14798 {
14799 int eol_flag = (c == 'Z');
14800 char *p = decode_mode_spec_buf;
14801
14802 if (! FRAME_WINDOW_P (f))
14803 {
14804 /* No need to mention EOL here--the terminal never needs
14805 to do EOL conversion. */
14806 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
14807 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
14808 }
14809 p = decode_mode_spec_coding (b->buffer_file_coding_system,
14810 p, eol_flag);
14811
14812 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
14813 #ifdef subprocesses
14814 obj = Fget_buffer_process (Fcurrent_buffer ());
14815 if (PROCESSP (obj))
14816 {
14817 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
14818 p, eol_flag);
14819 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
14820 p, eol_flag);
14821 }
14822 #endif /* subprocesses */
14823 #endif /* 0 */
14824 *p = 0;
14825 return decode_mode_spec_buf;
14826 }
14827 }
14828
14829 if (STRINGP (obj))
14830 {
14831 *multibyte = STRING_MULTIBYTE (obj);
14832 return (char *) SDATA (obj);
14833 }
14834 else
14835 return "";
14836 }
14837
14838
14839 /* Count up to COUNT lines starting from START / START_BYTE.
14840 But don't go beyond LIMIT_BYTE.
14841 Return the number of lines thus found (always nonnegative).
14842
14843 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
14844
14845 static int
14846 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
14847 int start, start_byte, limit_byte, count;
14848 int *byte_pos_ptr;
14849 {
14850 register unsigned char *cursor;
14851 unsigned char *base;
14852
14853 register int ceiling;
14854 register unsigned char *ceiling_addr;
14855 int orig_count = count;
14856
14857 /* If we are not in selective display mode,
14858 check only for newlines. */
14859 int selective_display = (!NILP (current_buffer->selective_display)
14860 && !INTEGERP (current_buffer->selective_display));
14861
14862 if (count > 0)
14863 {
14864 while (start_byte < limit_byte)
14865 {
14866 ceiling = BUFFER_CEILING_OF (start_byte);
14867 ceiling = min (limit_byte - 1, ceiling);
14868 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
14869 base = (cursor = BYTE_POS_ADDR (start_byte));
14870 while (1)
14871 {
14872 if (selective_display)
14873 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
14874 ;
14875 else
14876 while (*cursor != '\n' && ++cursor != ceiling_addr)
14877 ;
14878
14879 if (cursor != ceiling_addr)
14880 {
14881 if (--count == 0)
14882 {
14883 start_byte += cursor - base + 1;
14884 *byte_pos_ptr = start_byte;
14885 return orig_count;
14886 }
14887 else
14888 if (++cursor == ceiling_addr)
14889 break;
14890 }
14891 else
14892 break;
14893 }
14894 start_byte += cursor - base;
14895 }
14896 }
14897 else
14898 {
14899 while (start_byte > limit_byte)
14900 {
14901 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
14902 ceiling = max (limit_byte, ceiling);
14903 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
14904 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
14905 while (1)
14906 {
14907 if (selective_display)
14908 while (--cursor != ceiling_addr
14909 && *cursor != '\n' && *cursor != 015)
14910 ;
14911 else
14912 while (--cursor != ceiling_addr && *cursor != '\n')
14913 ;
14914
14915 if (cursor != ceiling_addr)
14916 {
14917 if (++count == 0)
14918 {
14919 start_byte += cursor - base + 1;
14920 *byte_pos_ptr = start_byte;
14921 /* When scanning backwards, we should
14922 not count the newline posterior to which we stop. */
14923 return - orig_count - 1;
14924 }
14925 }
14926 else
14927 break;
14928 }
14929 /* Here we add 1 to compensate for the last decrement
14930 of CURSOR, which took it past the valid range. */
14931 start_byte += cursor - base + 1;
14932 }
14933 }
14934
14935 *byte_pos_ptr = limit_byte;
14936
14937 if (count < 0)
14938 return - orig_count + count;
14939 return orig_count - count;
14940
14941 }
14942
14943
14944 \f
14945 /***********************************************************************
14946 Displaying strings
14947 ***********************************************************************/
14948
14949 /* Display a NUL-terminated string, starting with index START.
14950
14951 If STRING is non-null, display that C string. Otherwise, the Lisp
14952 string LISP_STRING is displayed.
14953
14954 If FACE_STRING is not nil, FACE_STRING_POS is a position in
14955 FACE_STRING. Display STRING or LISP_STRING with the face at
14956 FACE_STRING_POS in FACE_STRING:
14957
14958 Display the string in the environment given by IT, but use the
14959 standard display table, temporarily.
14960
14961 FIELD_WIDTH is the minimum number of output glyphs to produce.
14962 If STRING has fewer characters than FIELD_WIDTH, pad to the right
14963 with spaces. If STRING has more characters, more than FIELD_WIDTH
14964 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
14965
14966 PRECISION is the maximum number of characters to output from
14967 STRING. PRECISION < 0 means don't truncate the string.
14968
14969 This is roughly equivalent to printf format specifiers:
14970
14971 FIELD_WIDTH PRECISION PRINTF
14972 ----------------------------------------
14973 -1 -1 %s
14974 -1 10 %.10s
14975 10 -1 %10s
14976 20 10 %20.10s
14977
14978 MULTIBYTE zero means do not display multibyte chars, > 0 means do
14979 display them, and < 0 means obey the current buffer's value of
14980 enable_multibyte_characters.
14981
14982 Value is the number of glyphs produced. */
14983
14984 static int
14985 display_string (string, lisp_string, face_string, face_string_pos,
14986 start, it, field_width, precision, max_x, multibyte)
14987 unsigned char *string;
14988 Lisp_Object lisp_string;
14989 Lisp_Object face_string;
14990 int face_string_pos;
14991 int start;
14992 struct it *it;
14993 int field_width, precision, max_x;
14994 int multibyte;
14995 {
14996 int hpos_at_start = it->hpos;
14997 int saved_face_id = it->face_id;
14998 struct glyph_row *row = it->glyph_row;
14999
15000 /* Initialize the iterator IT for iteration over STRING beginning
15001 with index START. */
15002 reseat_to_string (it, string, lisp_string, start,
15003 precision, field_width, multibyte);
15004
15005 /* If displaying STRING, set up the face of the iterator
15006 from LISP_STRING, if that's given. */
15007 if (STRINGP (face_string))
15008 {
15009 int endptr;
15010 struct face *face;
15011
15012 it->face_id
15013 = face_at_string_position (it->w, face_string, face_string_pos,
15014 0, it->region_beg_charpos,
15015 it->region_end_charpos,
15016 &endptr, it->base_face_id, 0);
15017 face = FACE_FROM_ID (it->f, it->face_id);
15018 it->face_box_p = face->box != FACE_NO_BOX;
15019 }
15020
15021 /* Set max_x to the maximum allowed X position. Don't let it go
15022 beyond the right edge of the window. */
15023 if (max_x <= 0)
15024 max_x = it->last_visible_x;
15025 else
15026 max_x = min (max_x, it->last_visible_x);
15027
15028 /* Skip over display elements that are not visible. because IT->w is
15029 hscrolled. */
15030 if (it->current_x < it->first_visible_x)
15031 move_it_in_display_line_to (it, 100000, it->first_visible_x,
15032 MOVE_TO_POS | MOVE_TO_X);
15033
15034 row->ascent = it->max_ascent;
15035 row->height = it->max_ascent + it->max_descent;
15036 row->phys_ascent = it->max_phys_ascent;
15037 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15038
15039 /* This condition is for the case that we are called with current_x
15040 past last_visible_x. */
15041 while (it->current_x < max_x)
15042 {
15043 int x_before, x, n_glyphs_before, i, nglyphs;
15044
15045 /* Get the next display element. */
15046 if (!get_next_display_element (it))
15047 break;
15048
15049 /* Produce glyphs. */
15050 x_before = it->current_x;
15051 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
15052 PRODUCE_GLYPHS (it);
15053
15054 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
15055 i = 0;
15056 x = x_before;
15057 while (i < nglyphs)
15058 {
15059 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
15060
15061 if (!it->truncate_lines_p
15062 && x + glyph->pixel_width > max_x)
15063 {
15064 /* End of continued line or max_x reached. */
15065 if (CHAR_GLYPH_PADDING_P (*glyph))
15066 {
15067 /* A wide character is unbreakable. */
15068 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
15069 it->current_x = x_before;
15070 }
15071 else
15072 {
15073 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
15074 it->current_x = x;
15075 }
15076 break;
15077 }
15078 else if (x + glyph->pixel_width > it->first_visible_x)
15079 {
15080 /* Glyph is at least partially visible. */
15081 ++it->hpos;
15082 if (x < it->first_visible_x)
15083 it->glyph_row->x = x - it->first_visible_x;
15084 }
15085 else
15086 {
15087 /* Glyph is off the left margin of the display area.
15088 Should not happen. */
15089 abort ();
15090 }
15091
15092 row->ascent = max (row->ascent, it->max_ascent);
15093 row->height = max (row->height, it->max_ascent + it->max_descent);
15094 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15095 row->phys_height = max (row->phys_height,
15096 it->max_phys_ascent + it->max_phys_descent);
15097 x += glyph->pixel_width;
15098 ++i;
15099 }
15100
15101 /* Stop if max_x reached. */
15102 if (i < nglyphs)
15103 break;
15104
15105 /* Stop at line ends. */
15106 if (ITERATOR_AT_END_OF_LINE_P (it))
15107 {
15108 it->continuation_lines_width = 0;
15109 break;
15110 }
15111
15112 set_iterator_to_next (it, 1);
15113
15114 /* Stop if truncating at the right edge. */
15115 if (it->truncate_lines_p
15116 && it->current_x >= it->last_visible_x)
15117 {
15118 /* Add truncation mark, but don't do it if the line is
15119 truncated at a padding space. */
15120 if (IT_CHARPOS (*it) < it->string_nchars)
15121 {
15122 if (!FRAME_WINDOW_P (it->f))
15123 {
15124 int i, n;
15125
15126 if (it->current_x > it->last_visible_x)
15127 {
15128 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
15129 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
15130 break;
15131 for (n = row->used[TEXT_AREA]; i < n; ++i)
15132 {
15133 row->used[TEXT_AREA] = i;
15134 produce_special_glyphs (it, IT_TRUNCATION);
15135 }
15136 }
15137 produce_special_glyphs (it, IT_TRUNCATION);
15138 }
15139 it->glyph_row->truncated_on_right_p = 1;
15140 }
15141 break;
15142 }
15143 }
15144
15145 /* Maybe insert a truncation at the left. */
15146 if (it->first_visible_x
15147 && IT_CHARPOS (*it) > 0)
15148 {
15149 if (!FRAME_WINDOW_P (it->f))
15150 insert_left_trunc_glyphs (it);
15151 it->glyph_row->truncated_on_left_p = 1;
15152 }
15153
15154 it->face_id = saved_face_id;
15155
15156 /* Value is number of columns displayed. */
15157 return it->hpos - hpos_at_start;
15158 }
15159
15160
15161 \f
15162 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
15163 appears as an element of LIST or as the car of an element of LIST.
15164 If PROPVAL is a list, compare each element against LIST in that
15165 way, and return 1/2 if any element of PROPVAL is found in LIST.
15166 Otherwise return 0. This function cannot quit.
15167 The return value is 2 if the text is invisible but with an ellipsis
15168 and 1 if it's invisible and without an ellipsis. */
15169
15170 int
15171 invisible_p (propval, list)
15172 register Lisp_Object propval;
15173 Lisp_Object list;
15174 {
15175 register Lisp_Object tail, proptail;
15176
15177 for (tail = list; CONSP (tail); tail = XCDR (tail))
15178 {
15179 register Lisp_Object tem;
15180 tem = XCAR (tail);
15181 if (EQ (propval, tem))
15182 return 1;
15183 if (CONSP (tem) && EQ (propval, XCAR (tem)))
15184 return NILP (XCDR (tem)) ? 1 : 2;
15185 }
15186
15187 if (CONSP (propval))
15188 {
15189 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
15190 {
15191 Lisp_Object propelt;
15192 propelt = XCAR (proptail);
15193 for (tail = list; CONSP (tail); tail = XCDR (tail))
15194 {
15195 register Lisp_Object tem;
15196 tem = XCAR (tail);
15197 if (EQ (propelt, tem))
15198 return 1;
15199 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
15200 return NILP (XCDR (tem)) ? 1 : 2;
15201 }
15202 }
15203 }
15204
15205 return 0;
15206 }
15207
15208 \f
15209 /***********************************************************************
15210 Cursor types
15211 ***********************************************************************/
15212
15213 /* Value is the internal representation of the specified cursor type
15214 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
15215 of the bar cursor. */
15216
15217 enum text_cursor_kinds
15218 get_specified_cursor_type (arg, width)
15219 Lisp_Object arg;
15220 int *width;
15221 {
15222 enum text_cursor_kinds type;
15223
15224 if (NILP (arg))
15225 return NO_CURSOR;
15226
15227 if (EQ (arg, Qbox))
15228 return FILLED_BOX_CURSOR;
15229
15230 if (EQ (arg, Qhollow))
15231 return HOLLOW_BOX_CURSOR;
15232
15233 if (EQ (arg, Qbar))
15234 {
15235 *width = 2;
15236 return BAR_CURSOR;
15237 }
15238
15239 if (CONSP (arg)
15240 && EQ (XCAR (arg), Qbar)
15241 && INTEGERP (XCDR (arg))
15242 && XINT (XCDR (arg)) >= 0)
15243 {
15244 *width = XINT (XCDR (arg));
15245 return BAR_CURSOR;
15246 }
15247
15248 if (EQ (arg, Qhbar))
15249 {
15250 *width = 2;
15251 return HBAR_CURSOR;
15252 }
15253
15254 if (CONSP (arg)
15255 && EQ (XCAR (arg), Qhbar)
15256 && INTEGERP (XCDR (arg))
15257 && XINT (XCDR (arg)) >= 0)
15258 {
15259 *width = XINT (XCDR (arg));
15260 return HBAR_CURSOR;
15261 }
15262
15263 /* Treat anything unknown as "hollow box cursor".
15264 It was bad to signal an error; people have trouble fixing
15265 .Xdefaults with Emacs, when it has something bad in it. */
15266 type = HOLLOW_BOX_CURSOR;
15267
15268 return type;
15269 }
15270
15271 /* Set the default cursor types for specified frame. */
15272 void
15273 set_frame_cursor_types (f, arg)
15274 struct frame *f;
15275 Lisp_Object arg;
15276 {
15277 int width;
15278 Lisp_Object tem;
15279
15280 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
15281 FRAME_CURSOR_WIDTH (f) = width;
15282
15283 /* By default, set up the blink-off state depending on the on-state. */
15284
15285 tem = Fassoc (arg, Vblink_cursor_alist);
15286 if (!NILP (tem))
15287 {
15288 FRAME_BLINK_OFF_CURSOR (f)
15289 = get_specified_cursor_type (XCDR (tem), &width);
15290 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
15291 }
15292 else
15293 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
15294 }
15295
15296
15297 /* Return the cursor we want to be displayed. In a mini-buffer
15298 window, we want the cursor only to appear if we are reading input
15299 from this window. For the selected window, we want the cursor type
15300 given by the frame parameter or buffer local setting of
15301 cursor-type. If explicitly marked off, draw no cursor. In all
15302 other cases, we want a hollow box cursor. */
15303
15304 enum text_cursor_kinds
15305 get_window_cursor_type (w, width)
15306 struct window *w;
15307 int *width;
15308 {
15309 struct frame *f = XFRAME (w->frame);
15310 struct buffer *b = XBUFFER (w->buffer);
15311 int cursor_type = DEFAULT_CURSOR;
15312 Lisp_Object alt_cursor;
15313 int non_selected = 0;
15314
15315 /* Echo area */
15316 if (cursor_in_echo_area
15317 && FRAME_HAS_MINIBUF_P (f)
15318 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
15319 {
15320 if (w == XWINDOW (echo_area_window))
15321 {
15322 *width = FRAME_CURSOR_WIDTH (f);
15323 return FRAME_DESIRED_CURSOR (f);
15324 }
15325
15326 non_selected = 1;
15327 }
15328
15329 /* Nonselected window or nonselected frame. */
15330 else if (f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
15331 || w != XWINDOW (f->selected_window))
15332 {
15333 if (MINI_WINDOW_P (w) && minibuf_level == 0)
15334 return NO_CURSOR;
15335
15336 non_selected = 1;
15337 }
15338
15339 /* Never display a cursor in a window in which cursor-type is nil. */
15340 if (NILP (b->cursor_type))
15341 return NO_CURSOR;
15342
15343 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
15344 if (non_selected)
15345 {
15346 alt_cursor = Fbuffer_local_value (Qcursor_in_non_selected_windows, w->buffer);
15347 return get_specified_cursor_type (alt_cursor, width);
15348 }
15349
15350 /* Get the normal cursor type for this window. */
15351 if (EQ (b->cursor_type, Qt))
15352 {
15353 cursor_type = FRAME_DESIRED_CURSOR (f);
15354 *width = FRAME_CURSOR_WIDTH (f);
15355 }
15356 else
15357 cursor_type = get_specified_cursor_type (b->cursor_type, width);
15358
15359 /* Use normal cursor if not blinked off. */
15360 if (!w->cursor_off_p)
15361 return cursor_type;
15362
15363 /* Cursor is blinked off, so determine how to "toggle" it. */
15364
15365 /* First try to use alternate-cursor-type, unless it is t. */
15366 alt_cursor = Fbuffer_local_value (Qalternate_cursor_type, w->buffer);
15367 if (!EQ (alt_cursor, Qt))
15368 return get_specified_cursor_type (alt_cursor, width);
15369
15370 /* Then unless buffer's cursor-type is t (use default),
15371 look for an entry matching normal cursor in blink-cursor-alist. */
15372 if (!EQ (b->cursor_type, Qt) &&
15373 (alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
15374 return get_specified_cursor_type (XCDR (alt_cursor), width);
15375
15376 /* Then see if frame has specified a specific blink off cursor type. */
15377 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
15378 {
15379 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
15380 return FRAME_BLINK_OFF_CURSOR (f);
15381 }
15382
15383 /* Finally perform built-in cursor blinking:
15384 filled box <-> hollow box
15385 wide [h]bar <-> narrow [h]bar
15386 narrow [h]bar <-> no cursor
15387 other type <-> no cursor */
15388
15389 if (cursor_type == FILLED_BOX_CURSOR)
15390 return HOLLOW_BOX_CURSOR;
15391
15392 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
15393 {
15394 *width = 1;
15395 return cursor_type;
15396 }
15397
15398 return NO_CURSOR;
15399 }
15400
15401 \f
15402 /***********************************************************************
15403 Initialization
15404 ***********************************************************************/
15405
15406 void
15407 syms_of_xdisp ()
15408 {
15409 Vwith_echo_area_save_vector = Qnil;
15410 staticpro (&Vwith_echo_area_save_vector);
15411
15412 Vmessage_stack = Qnil;
15413 staticpro (&Vmessage_stack);
15414
15415 Qinhibit_redisplay = intern ("inhibit-redisplay");
15416 staticpro (&Qinhibit_redisplay);
15417
15418 message_dolog_marker1 = Fmake_marker ();
15419 staticpro (&message_dolog_marker1);
15420 message_dolog_marker2 = Fmake_marker ();
15421 staticpro (&message_dolog_marker2);
15422 message_dolog_marker3 = Fmake_marker ();
15423 staticpro (&message_dolog_marker3);
15424
15425 #if GLYPH_DEBUG
15426 defsubr (&Sdump_frame_glyph_matrix);
15427 defsubr (&Sdump_glyph_matrix);
15428 defsubr (&Sdump_glyph_row);
15429 defsubr (&Sdump_tool_bar_row);
15430 defsubr (&Strace_redisplay);
15431 defsubr (&Strace_to_stderr);
15432 #endif
15433 #ifdef HAVE_WINDOW_SYSTEM
15434 defsubr (&Stool_bar_lines_needed);
15435 #endif
15436 defsubr (&Sformat_mode_line);
15437
15438 staticpro (&Qmenu_bar_update_hook);
15439 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
15440
15441 staticpro (&Qoverriding_terminal_local_map);
15442 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
15443
15444 staticpro (&Qoverriding_local_map);
15445 Qoverriding_local_map = intern ("overriding-local-map");
15446
15447 staticpro (&Qwindow_scroll_functions);
15448 Qwindow_scroll_functions = intern ("window-scroll-functions");
15449
15450 staticpro (&Qredisplay_end_trigger_functions);
15451 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
15452
15453 staticpro (&Qinhibit_point_motion_hooks);
15454 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
15455
15456 QCdata = intern (":data");
15457 staticpro (&QCdata);
15458 Qdisplay = intern ("display");
15459 staticpro (&Qdisplay);
15460 Qspace_width = intern ("space-width");
15461 staticpro (&Qspace_width);
15462 Qraise = intern ("raise");
15463 staticpro (&Qraise);
15464 Qspace = intern ("space");
15465 staticpro (&Qspace);
15466 Qmargin = intern ("margin");
15467 staticpro (&Qmargin);
15468 Qleft_margin = intern ("left-margin");
15469 staticpro (&Qleft_margin);
15470 Qright_margin = intern ("right-margin");
15471 staticpro (&Qright_margin);
15472 Qalign_to = intern ("align-to");
15473 staticpro (&Qalign_to);
15474 QCalign_to = intern (":align-to");
15475 staticpro (&QCalign_to);
15476 Qrelative_width = intern ("relative-width");
15477 staticpro (&Qrelative_width);
15478 QCrelative_width = intern (":relative-width");
15479 staticpro (&QCrelative_width);
15480 QCrelative_height = intern (":relative-height");
15481 staticpro (&QCrelative_height);
15482 QCeval = intern (":eval");
15483 staticpro (&QCeval);
15484 QCpropertize = intern (":propertize");
15485 staticpro (&QCpropertize);
15486 Qwhen = intern ("when");
15487 staticpro (&Qwhen);
15488 QCfile = intern (":file");
15489 staticpro (&QCfile);
15490 Qfontified = intern ("fontified");
15491 staticpro (&Qfontified);
15492 Qfontification_functions = intern ("fontification-functions");
15493 staticpro (&Qfontification_functions);
15494 Qtrailing_whitespace = intern ("trailing-whitespace");
15495 staticpro (&Qtrailing_whitespace);
15496 Qimage = intern ("image");
15497 staticpro (&Qimage);
15498 Qmessage_truncate_lines = intern ("message-truncate-lines");
15499 staticpro (&Qmessage_truncate_lines);
15500 Qcursor_in_non_selected_windows = intern ("cursor-in-non-selected-windows");
15501 staticpro (&Qcursor_in_non_selected_windows);
15502 Qalternate_cursor_type = intern ("alternate-cursor-type");
15503 staticpro (&Qalternate_cursor_type);
15504 Qgrow_only = intern ("grow-only");
15505 staticpro (&Qgrow_only);
15506 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
15507 staticpro (&Qinhibit_menubar_update);
15508 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
15509 staticpro (&Qinhibit_eval_during_redisplay);
15510 Qposition = intern ("position");
15511 staticpro (&Qposition);
15512 Qbuffer_position = intern ("buffer-position");
15513 staticpro (&Qbuffer_position);
15514 Qobject = intern ("object");
15515 staticpro (&Qobject);
15516 Qbar = intern ("bar");
15517 staticpro (&Qbar);
15518 Qhbar = intern ("hbar");
15519 staticpro (&Qhbar);
15520 Qbox = intern ("box");
15521 staticpro (&Qbox);
15522 Qhollow = intern ("hollow");
15523 staticpro (&Qhollow);
15524 Qrisky_local_variable = intern ("risky-local-variable");
15525 staticpro (&Qrisky_local_variable);
15526 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
15527 staticpro (&Qinhibit_free_realized_faces);
15528
15529 list_of_error = Fcons (intern ("error"), Qnil);
15530 staticpro (&list_of_error);
15531
15532 last_arrow_position = Qnil;
15533 last_arrow_string = Qnil;
15534 staticpro (&last_arrow_position);
15535 staticpro (&last_arrow_string);
15536
15537 echo_buffer[0] = echo_buffer[1] = Qnil;
15538 staticpro (&echo_buffer[0]);
15539 staticpro (&echo_buffer[1]);
15540
15541 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
15542 staticpro (&echo_area_buffer[0]);
15543 staticpro (&echo_area_buffer[1]);
15544
15545 Vmessages_buffer_name = build_string ("*Messages*");
15546 staticpro (&Vmessages_buffer_name);
15547
15548 mode_line_proptrans_alist = Qnil;
15549 staticpro (&mode_line_proptrans_alist);
15550
15551 mode_line_string_list = Qnil;
15552 staticpro (&mode_line_string_list);
15553
15554 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
15555 doc: /* Non-nil means highlight trailing whitespace.
15556 The face used for trailing whitespace is `trailing-whitespace'. */);
15557 Vshow_trailing_whitespace = Qnil;
15558
15559 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
15560 doc: /* Non-nil means don't actually do any redisplay.
15561 This is used for internal purposes. */);
15562 Vinhibit_redisplay = Qnil;
15563
15564 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
15565 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
15566 Vglobal_mode_string = Qnil;
15567
15568 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
15569 doc: /* Marker for where to display an arrow on top of the buffer text.
15570 This must be the beginning of a line in order to work.
15571 See also `overlay-arrow-string'. */);
15572 Voverlay_arrow_position = Qnil;
15573
15574 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
15575 doc: /* String to display as an arrow. See also `overlay-arrow-position'. */);
15576 Voverlay_arrow_string = Qnil;
15577
15578 DEFVAR_INT ("scroll-step", &scroll_step,
15579 doc: /* *The number of lines to try scrolling a window by when point moves out.
15580 If that fails to bring point back on frame, point is centered instead.
15581 If this is zero, point is always centered after it moves off frame.
15582 If you want scrolling to always be a line at a time, you should set
15583 `scroll-conservatively' to a large value rather than set this to 1. */);
15584
15585 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
15586 doc: /* *Scroll up to this many lines, to bring point back on screen.
15587 A value of zero means to scroll the text to center point vertically
15588 in the window. */);
15589 scroll_conservatively = 0;
15590
15591 DEFVAR_INT ("scroll-margin", &scroll_margin,
15592 doc: /* *Number of lines of margin at the top and bottom of a window.
15593 Recenter the window whenever point gets within this many lines
15594 of the top or bottom of the window. */);
15595 scroll_margin = 0;
15596
15597 #if GLYPH_DEBUG
15598 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
15599 #endif
15600
15601 DEFVAR_BOOL ("truncate-partial-width-windows",
15602 &truncate_partial_width_windows,
15603 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
15604 truncate_partial_width_windows = 1;
15605
15606 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
15607 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
15608 Any other value means to use the appropriate face, `mode-line',
15609 `header-line', or `menu' respectively. */);
15610 mode_line_inverse_video = 1;
15611
15612 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
15613 doc: /* *Maximum buffer size for which line number should be displayed.
15614 If the buffer is bigger than this, the line number does not appear
15615 in the mode line. A value of nil means no limit. */);
15616 Vline_number_display_limit = Qnil;
15617
15618 DEFVAR_INT ("line-number-display-limit-width",
15619 &line_number_display_limit_width,
15620 doc: /* *Maximum line width (in characters) for line number display.
15621 If the average length of the lines near point is bigger than this, then the
15622 line number may be omitted from the mode line. */);
15623 line_number_display_limit_width = 200;
15624
15625 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
15626 doc: /* *Non-nil means highlight region even in nonselected windows. */);
15627 highlight_nonselected_windows = 0;
15628
15629 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
15630 doc: /* Non-nil if more than one frame is visible on this display.
15631 Minibuffer-only frames don't count, but iconified frames do.
15632 This variable is not guaranteed to be accurate except while processing
15633 `frame-title-format' and `icon-title-format'. */);
15634
15635 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
15636 doc: /* Template for displaying the title bar of visible frames.
15637 \(Assuming the window manager supports this feature.)
15638 This variable has the same structure as `mode-line-format' (which see),
15639 and is used only on frames for which no explicit name has been set
15640 \(see `modify-frame-parameters'). */);
15641 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
15642 doc: /* Template for displaying the title bar of an iconified frame.
15643 \(Assuming the window manager supports this feature.)
15644 This variable has the same structure as `mode-line-format' (which see),
15645 and is used only on frames for which no explicit name has been set
15646 \(see `modify-frame-parameters'). */);
15647 Vicon_title_format
15648 = Vframe_title_format
15649 = Fcons (intern ("multiple-frames"),
15650 Fcons (build_string ("%b"),
15651 Fcons (Fcons (empty_string,
15652 Fcons (intern ("invocation-name"),
15653 Fcons (build_string ("@"),
15654 Fcons (intern ("system-name"),
15655 Qnil)))),
15656 Qnil)));
15657
15658 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
15659 doc: /* Maximum number of lines to keep in the message log buffer.
15660 If nil, disable message logging. If t, log messages but don't truncate
15661 the buffer when it becomes large. */);
15662 Vmessage_log_max = make_number (50);
15663
15664 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
15665 doc: /* Functions called before redisplay, if window sizes have changed.
15666 The value should be a list of functions that take one argument.
15667 Just before redisplay, for each frame, if any of its windows have changed
15668 size since the last redisplay, or have been split or deleted,
15669 all the functions in the list are called, with the frame as argument. */);
15670 Vwindow_size_change_functions = Qnil;
15671
15672 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
15673 doc: /* List of Functions to call before redisplaying a window with scrolling.
15674 Each function is called with two arguments, the window
15675 and its new display-start position. Note that the value of `window-end'
15676 is not valid when these functions are called. */);
15677 Vwindow_scroll_functions = Qnil;
15678
15679 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
15680 doc: /* *Non-nil means automatically resize tool-bars.
15681 This increases a tool-bar's height if not all tool-bar items are visible.
15682 It decreases a tool-bar's height when it would display blank lines
15683 otherwise. */);
15684 auto_resize_tool_bars_p = 1;
15685
15686 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
15687 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
15688 auto_raise_tool_bar_buttons_p = 1;
15689
15690 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
15691 doc: /* *Margin around tool-bar buttons in pixels.
15692 If an integer, use that for both horizontal and vertical margins.
15693 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
15694 HORZ specifying the horizontal margin, and VERT specifying the
15695 vertical margin. */);
15696 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
15697
15698 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
15699 doc: /* *Relief thickness of tool-bar buttons. */);
15700 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
15701
15702 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
15703 doc: /* List of functions to call to fontify regions of text.
15704 Each function is called with one argument POS. Functions must
15705 fontify a region starting at POS in the current buffer, and give
15706 fontified regions the property `fontified'. */);
15707 Vfontification_functions = Qnil;
15708 Fmake_variable_buffer_local (Qfontification_functions);
15709
15710 DEFVAR_BOOL ("unibyte-display-via-language-environment",
15711 &unibyte_display_via_language_environment,
15712 doc: /* *Non-nil means display unibyte text according to language environment.
15713 Specifically this means that unibyte non-ASCII characters
15714 are displayed by converting them to the equivalent multibyte characters
15715 according to the current language environment. As a result, they are
15716 displayed according to the current fontset. */);
15717 unibyte_display_via_language_environment = 0;
15718
15719 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
15720 doc: /* *Maximum height for resizing mini-windows.
15721 If a float, it specifies a fraction of the mini-window frame's height.
15722 If an integer, it specifies a number of lines. */);
15723 Vmax_mini_window_height = make_float (0.25);
15724
15725 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
15726 doc: /* *How to resize mini-windows.
15727 A value of nil means don't automatically resize mini-windows.
15728 A value of t means resize them to fit the text displayed in them.
15729 A value of `grow-only', the default, means let mini-windows grow
15730 only, until their display becomes empty, at which point the windows
15731 go back to their normal size. */);
15732 Vresize_mini_windows = Qgrow_only;
15733
15734 DEFVAR_LISP ("cursor-in-non-selected-windows",
15735 &Vcursor_in_non_selected_windows,
15736 doc: /* *Cursor type to display in non-selected windows.
15737 t means to use hollow box cursor. See `cursor-type' for other values. */);
15738 Vcursor_in_non_selected_windows = Qt;
15739
15740 DEFVAR_LISP ("alternate-cursor-type", &Valternate_cursor_type,
15741 doc: /* *Cursor type displayed in the blinking cursor off state.
15742 t means to use default. See `cursor-type' for other values. */);
15743 Valternate_cursor_type = 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