Fix typo in comment.
[bpt/emacs.git] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 86, 87, 88, 93, 94, 95, 97, 98, 99, 2000, 2001, 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 /* How to blink the default frame cursor off. */
541 Lisp_Object Vblink_cursor_alist;
542
543 /* A scratch glyph row with contents used for generating truncation
544 glyphs. Also used in direct_output_for_insert. */
545
546 #define MAX_SCRATCH_GLYPHS 100
547 struct glyph_row scratch_glyph_row;
548 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
549
550 /* Ascent and height of the last line processed by move_it_to. */
551
552 static int last_max_ascent, last_height;
553
554 /* Non-zero if there's a help-echo in the echo area. */
555
556 int help_echo_showing_p;
557
558 /* If >= 0, computed, exact values of mode-line and header-line height
559 to use in the macros CURRENT_MODE_LINE_HEIGHT and
560 CURRENT_HEADER_LINE_HEIGHT. */
561
562 int current_mode_line_height, current_header_line_height;
563
564 /* The maximum distance to look ahead for text properties. Values
565 that are too small let us call compute_char_face and similar
566 functions too often which is expensive. Values that are too large
567 let us call compute_char_face and alike too often because we
568 might not be interested in text properties that far away. */
569
570 #define TEXT_PROP_DISTANCE_LIMIT 100
571
572 #if GLYPH_DEBUG
573
574 /* Variables to turn off display optimizations from Lisp. */
575
576 int inhibit_try_window_id, inhibit_try_window_reusing;
577 int inhibit_try_cursor_movement;
578
579 /* Non-zero means print traces of redisplay if compiled with
580 GLYPH_DEBUG != 0. */
581
582 int trace_redisplay_p;
583
584 #endif /* GLYPH_DEBUG */
585
586 #ifdef DEBUG_TRACE_MOVE
587 /* Non-zero means trace with TRACE_MOVE to stderr. */
588 int trace_move;
589
590 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
591 #else
592 #define TRACE_MOVE(x) (void) 0
593 #endif
594
595 /* Non-zero means automatically scroll windows horizontally to make
596 point visible. */
597
598 int automatic_hscrolling_p;
599
600 /* How close to the margin can point get before the window is scrolled
601 horizontally. */
602 EMACS_INT hscroll_margin;
603
604 /* How much to scroll horizontally when point is inside the above margin. */
605 Lisp_Object Vhscroll_step;
606
607 /* A list of symbols, one for each supported image type. */
608
609 Lisp_Object Vimage_types;
610
611 /* The variable `resize-mini-windows'. If nil, don't resize
612 mini-windows. If t, always resize them to fit the text they
613 display. If `grow-only', let mini-windows grow only until they
614 become empty. */
615
616 Lisp_Object Vresize_mini_windows;
617
618 /* Buffer being redisplayed -- for redisplay_window_error. */
619
620 struct buffer *displayed_buffer;
621
622 /* Value returned from text property handlers (see below). */
623
624 enum prop_handled
625 {
626 HANDLED_NORMALLY,
627 HANDLED_RECOMPUTE_PROPS,
628 HANDLED_OVERLAY_STRING_CONSUMED,
629 HANDLED_RETURN
630 };
631
632 /* A description of text properties that redisplay is interested
633 in. */
634
635 struct props
636 {
637 /* The name of the property. */
638 Lisp_Object *name;
639
640 /* A unique index for the property. */
641 enum prop_idx idx;
642
643 /* A handler function called to set up iterator IT from the property
644 at IT's current position. Value is used to steer handle_stop. */
645 enum prop_handled (*handler) P_ ((struct it *it));
646 };
647
648 static enum prop_handled handle_face_prop P_ ((struct it *));
649 static enum prop_handled handle_invisible_prop P_ ((struct it *));
650 static enum prop_handled handle_display_prop P_ ((struct it *));
651 static enum prop_handled handle_composition_prop P_ ((struct it *));
652 static enum prop_handled handle_overlay_change P_ ((struct it *));
653 static enum prop_handled handle_fontified_prop P_ ((struct it *));
654
655 /* Properties handled by iterators. */
656
657 static struct props it_props[] =
658 {
659 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
660 /* Handle `face' before `display' because some sub-properties of
661 `display' need to know the face. */
662 {&Qface, FACE_PROP_IDX, handle_face_prop},
663 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
664 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
665 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
666 {NULL, 0, NULL}
667 };
668
669 /* Value is the position described by X. If X is a marker, value is
670 the marker_position of X. Otherwise, value is X. */
671
672 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
673
674 /* Enumeration returned by some move_it_.* functions internally. */
675
676 enum move_it_result
677 {
678 /* Not used. Undefined value. */
679 MOVE_UNDEFINED,
680
681 /* Move ended at the requested buffer position or ZV. */
682 MOVE_POS_MATCH_OR_ZV,
683
684 /* Move ended at the requested X pixel position. */
685 MOVE_X_REACHED,
686
687 /* Move within a line ended at the end of a line that must be
688 continued. */
689 MOVE_LINE_CONTINUED,
690
691 /* Move within a line ended at the end of a line that would
692 be displayed truncated. */
693 MOVE_LINE_TRUNCATED,
694
695 /* Move within a line ended at a line end. */
696 MOVE_NEWLINE_OR_CR
697 };
698
699 /* This counter is used to clear the face cache every once in a while
700 in redisplay_internal. It is incremented for each redisplay.
701 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
702 cleared. */
703
704 #define CLEAR_FACE_CACHE_COUNT 500
705 static int clear_face_cache_count;
706
707 /* Record the previous terminal frame we displayed. */
708
709 static struct frame *previous_terminal_frame;
710
711 /* Non-zero while redisplay_internal is in progress. */
712
713 int redisplaying_p;
714
715 /* Non-zero means don't free realized faces. Bound while freeing
716 realized faces is dangerous because glyph matrices might still
717 reference them. */
718
719 int inhibit_free_realized_faces;
720 Lisp_Object Qinhibit_free_realized_faces;
721
722 \f
723 /* Function prototypes. */
724
725 static void setup_for_ellipsis P_ ((struct it *));
726 static void mark_window_display_accurate_1 P_ ((struct window *, int));
727 static int single_display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
728 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
729 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
730 static int redisplay_mode_lines P_ ((Lisp_Object, int));
731 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
732
733 #if 0
734 static int invisible_text_between_p P_ ((struct it *, int, int));
735 #endif
736
737 static int next_element_from_ellipsis P_ ((struct it *));
738 static void pint2str P_ ((char *, int, int));
739 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
740 struct text_pos));
741 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
742 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
743 static void store_frame_title_char P_ ((char));
744 static int store_frame_title P_ ((const unsigned char *, int, int));
745 static void x_consider_frame_title P_ ((Lisp_Object));
746 static void handle_stop P_ ((struct it *));
747 static int tool_bar_lines_needed P_ ((struct frame *));
748 static int single_display_prop_intangible_p P_ ((Lisp_Object));
749 static void ensure_echo_area_buffers P_ ((void));
750 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
751 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
752 static int with_echo_area_buffer P_ ((struct window *, int,
753 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
754 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
755 static void clear_garbaged_frames P_ ((void));
756 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
757 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
758 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
759 static int display_echo_area P_ ((struct window *));
760 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
761 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
762 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
763 static int string_char_and_length P_ ((const unsigned char *, int, int *));
764 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
765 struct text_pos));
766 static int compute_window_start_on_continuation_line P_ ((struct window *));
767 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
768 static void insert_left_trunc_glyphs P_ ((struct it *));
769 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
770 static void extend_face_to_end_of_line P_ ((struct it *));
771 static int append_space P_ ((struct it *, int));
772 static int make_cursor_line_fully_visible P_ ((struct window *));
773 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int));
774 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
775 static int trailing_whitespace_p P_ ((int));
776 static int message_log_check_duplicate P_ ((int, int, int, int));
777 static void push_it P_ ((struct it *));
778 static void pop_it P_ ((struct it *));
779 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
780 static void redisplay_internal P_ ((int));
781 static int echo_area_display P_ ((int));
782 static void redisplay_windows P_ ((Lisp_Object));
783 static void redisplay_window P_ ((Lisp_Object, int));
784 static Lisp_Object redisplay_window_error ();
785 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
786 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
787 static void update_menu_bar P_ ((struct frame *, int));
788 static int try_window_reusing_current_matrix P_ ((struct window *));
789 static int try_window_id P_ ((struct window *));
790 static int display_line P_ ((struct it *));
791 static int display_mode_lines P_ ((struct window *));
792 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
793 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
794 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
795 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
796 static void display_menu_bar P_ ((struct window *));
797 static int display_count_lines P_ ((int, int, int, int, int *));
798 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
799 int, int, struct it *, int, int, int, int));
800 static void compute_line_metrics P_ ((struct it *));
801 static void run_redisplay_end_trigger_hook P_ ((struct it *));
802 static int get_overlay_strings P_ ((struct it *, int));
803 static void next_overlay_string P_ ((struct it *));
804 static void reseat P_ ((struct it *, struct text_pos, int));
805 static void reseat_1 P_ ((struct it *, struct text_pos, int));
806 static void back_to_previous_visible_line_start P_ ((struct it *));
807 static void reseat_at_previous_visible_line_start P_ ((struct it *));
808 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
809 static int next_element_from_display_vector P_ ((struct it *));
810 static int next_element_from_string P_ ((struct it *));
811 static int next_element_from_c_string P_ ((struct it *));
812 static int next_element_from_buffer P_ ((struct it *));
813 static int next_element_from_composition P_ ((struct it *));
814 static int next_element_from_image P_ ((struct it *));
815 static int next_element_from_stretch P_ ((struct it *));
816 static void load_overlay_strings P_ ((struct it *, int));
817 static int init_from_display_pos P_ ((struct it *, struct window *,
818 struct display_pos *));
819 static void reseat_to_string P_ ((struct it *, unsigned char *,
820 Lisp_Object, int, int, int, int));
821 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
822 int, int, int));
823 void move_it_vertically_backward P_ ((struct it *, int));
824 static void init_to_row_start P_ ((struct it *, struct window *,
825 struct glyph_row *));
826 static int init_to_row_end P_ ((struct it *, struct window *,
827 struct glyph_row *));
828 static void back_to_previous_line_start P_ ((struct it *));
829 static int forward_to_next_line_start P_ ((struct it *, int *));
830 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
831 Lisp_Object, int));
832 static struct text_pos string_pos P_ ((int, Lisp_Object));
833 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
834 static int number_of_chars P_ ((unsigned char *, int));
835 static void compute_stop_pos P_ ((struct it *));
836 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
837 Lisp_Object));
838 static int face_before_or_after_it_pos P_ ((struct it *, int));
839 static int next_overlay_change P_ ((int));
840 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
841 Lisp_Object, struct text_pos *,
842 int));
843 static int underlying_face_id P_ ((struct it *));
844 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
845 struct window *));
846
847 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
848 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
849
850 #ifdef HAVE_WINDOW_SYSTEM
851
852 static void update_tool_bar P_ ((struct frame *, int));
853 static void build_desired_tool_bar_string P_ ((struct frame *f));
854 static int redisplay_tool_bar P_ ((struct frame *));
855 static void display_tool_bar_line P_ ((struct it *));
856
857 #endif /* HAVE_WINDOW_SYSTEM */
858
859 \f
860 /***********************************************************************
861 Window display dimensions
862 ***********************************************************************/
863
864 /* Return the window-relative maximum y + 1 for glyph rows displaying
865 text in window W. This is the height of W minus the height of a
866 mode line, if any. */
867
868 INLINE int
869 window_text_bottom_y (w)
870 struct window *w;
871 {
872 struct frame *f = XFRAME (w->frame);
873 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
874
875 if (WINDOW_WANTS_MODELINE_P (w))
876 height -= CURRENT_MODE_LINE_HEIGHT (w);
877 return height;
878 }
879
880
881 /* Return the pixel width of display area AREA of window W. AREA < 0
882 means return the total width of W, not including fringes to
883 the left and right of the window. */
884
885 INLINE int
886 window_box_width (w, area)
887 struct window *w;
888 int area;
889 {
890 struct frame *f = XFRAME (w->frame);
891 int width = XFASTINT (w->width);
892
893 if (!w->pseudo_window_p)
894 {
895 width -= FRAME_SCROLL_BAR_WIDTH (f) + FRAME_FRINGE_COLS (f);
896
897 if (area == TEXT_AREA)
898 {
899 if (INTEGERP (w->left_margin_width))
900 width -= XFASTINT (w->left_margin_width);
901 if (INTEGERP (w->right_margin_width))
902 width -= XFASTINT (w->right_margin_width);
903 }
904 else if (area == LEFT_MARGIN_AREA)
905 width = (INTEGERP (w->left_margin_width)
906 ? XFASTINT (w->left_margin_width) : 0);
907 else if (area == RIGHT_MARGIN_AREA)
908 width = (INTEGERP (w->right_margin_width)
909 ? XFASTINT (w->right_margin_width) : 0);
910 }
911
912 return width * CANON_X_UNIT (f);
913 }
914
915
916 /* Return the pixel height of the display area of window W, not
917 including mode lines of W, if any. */
918
919 INLINE int
920 window_box_height (w)
921 struct window *w;
922 {
923 struct frame *f = XFRAME (w->frame);
924 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
925
926 xassert (height >= 0);
927
928 /* Note: the code below that determines the mode-line/header-line
929 height is essentially the same as that contained in the macro
930 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
931 the appropriate glyph row has its `mode_line_p' flag set,
932 and if it doesn't, uses estimate_mode_line_height instead. */
933
934 if (WINDOW_WANTS_MODELINE_P (w))
935 {
936 struct glyph_row *ml_row
937 = (w->current_matrix && w->current_matrix->rows
938 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
939 : 0);
940 if (ml_row && ml_row->mode_line_p)
941 height -= ml_row->height;
942 else
943 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
944 }
945
946 if (WINDOW_WANTS_HEADER_LINE_P (w))
947 {
948 struct glyph_row *hl_row
949 = (w->current_matrix && w->current_matrix->rows
950 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
951 : 0);
952 if (hl_row && hl_row->mode_line_p)
953 height -= hl_row->height;
954 else
955 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
956 }
957
958 /* With a very small font and a mode-line that's taller than
959 default, we might end up with a negative height. */
960 return max (0, height);
961 }
962
963
964 /* Return the frame-relative coordinate of the left edge of display
965 area AREA of window W. AREA < 0 means return the left edge of the
966 whole window, to the right of the left fringe of W. */
967
968 INLINE int
969 window_box_left (w, area)
970 struct window *w;
971 int area;
972 {
973 struct frame *f = XFRAME (w->frame);
974 int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
975
976 if (!w->pseudo_window_p)
977 {
978 x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
979 + FRAME_LEFT_FRINGE_WIDTH (f));
980
981 if (area == TEXT_AREA)
982 x += window_box_width (w, LEFT_MARGIN_AREA);
983 else if (area == RIGHT_MARGIN_AREA)
984 x += (window_box_width (w, LEFT_MARGIN_AREA)
985 + window_box_width (w, TEXT_AREA));
986 }
987
988 return x;
989 }
990
991
992 /* Return the frame-relative coordinate of the right edge of display
993 area AREA of window W. AREA < 0 means return the left edge of the
994 whole window, to the left of the right fringe of W. */
995
996 INLINE int
997 window_box_right (w, area)
998 struct window *w;
999 int area;
1000 {
1001 return window_box_left (w, area) + window_box_width (w, area);
1002 }
1003
1004
1005 /* Get the bounding box of the display area AREA of window W, without
1006 mode lines, in frame-relative coordinates. AREA < 0 means the
1007 whole window, not including the left and right fringes of
1008 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1009 coordinates of the upper-left corner of the box. Return in
1010 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1011
1012 INLINE void
1013 window_box (w, area, box_x, box_y, box_width, box_height)
1014 struct window *w;
1015 int area;
1016 int *box_x, *box_y, *box_width, *box_height;
1017 {
1018 struct frame *f = XFRAME (w->frame);
1019
1020 *box_width = window_box_width (w, area);
1021 *box_height = window_box_height (w);
1022 *box_x = window_box_left (w, area);
1023 *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
1024 + XFASTINT (w->top) * CANON_Y_UNIT (f));
1025 if (WINDOW_WANTS_HEADER_LINE_P (w))
1026 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1027 }
1028
1029
1030 /* Get the bounding box of the display area AREA of window W, without
1031 mode lines. AREA < 0 means the whole window, not including the
1032 left and right fringe of the window. Return in *TOP_LEFT_X
1033 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1034 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1035 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1036 box. */
1037
1038 INLINE void
1039 window_box_edges (w, area, top_left_x, top_left_y,
1040 bottom_right_x, bottom_right_y)
1041 struct window *w;
1042 int area;
1043 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1044 {
1045 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1046 bottom_right_y);
1047 *bottom_right_x += *top_left_x;
1048 *bottom_right_y += *top_left_y;
1049 }
1050
1051
1052 \f
1053 /***********************************************************************
1054 Utilities
1055 ***********************************************************************/
1056
1057 /* Return the bottom y-position of the line the iterator IT is in.
1058 This can modify IT's settings. */
1059
1060 int
1061 line_bottom_y (it)
1062 struct it *it;
1063 {
1064 int line_height = it->max_ascent + it->max_descent;
1065 int line_top_y = it->current_y;
1066
1067 if (line_height == 0)
1068 {
1069 if (last_height)
1070 line_height = last_height;
1071 else if (IT_CHARPOS (*it) < ZV)
1072 {
1073 move_it_by_lines (it, 1, 1);
1074 line_height = (it->max_ascent || it->max_descent
1075 ? it->max_ascent + it->max_descent
1076 : last_height);
1077 }
1078 else
1079 {
1080 struct glyph_row *row = it->glyph_row;
1081
1082 /* Use the default character height. */
1083 it->glyph_row = NULL;
1084 it->what = IT_CHARACTER;
1085 it->c = ' ';
1086 it->len = 1;
1087 PRODUCE_GLYPHS (it);
1088 line_height = it->ascent + it->descent;
1089 it->glyph_row = row;
1090 }
1091 }
1092
1093 return line_top_y + line_height;
1094 }
1095
1096
1097 /* Return 1 if position CHARPOS is visible in window W. Set *FULLY to
1098 1 if POS is visible and the line containing POS is fully visible.
1099 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1100 and header-lines heights. */
1101
1102 int
1103 pos_visible_p (w, charpos, fully, exact_mode_line_heights_p)
1104 struct window *w;
1105 int charpos, *fully, exact_mode_line_heights_p;
1106 {
1107 struct it it;
1108 struct text_pos top;
1109 int visible_p;
1110 struct buffer *old_buffer = NULL;
1111
1112 if (XBUFFER (w->buffer) != current_buffer)
1113 {
1114 old_buffer = current_buffer;
1115 set_buffer_internal_1 (XBUFFER (w->buffer));
1116 }
1117
1118 *fully = visible_p = 0;
1119 SET_TEXT_POS_FROM_MARKER (top, w->start);
1120
1121 /* Compute exact mode line heights, if requested. */
1122 if (exact_mode_line_heights_p)
1123 {
1124 if (WINDOW_WANTS_MODELINE_P (w))
1125 current_mode_line_height
1126 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1127 current_buffer->mode_line_format);
1128
1129 if (WINDOW_WANTS_HEADER_LINE_P (w))
1130 current_header_line_height
1131 = display_mode_line (w, HEADER_LINE_FACE_ID,
1132 current_buffer->header_line_format);
1133 }
1134
1135 start_display (&it, w, top);
1136 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
1137 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
1138
1139 /* Note that we may overshoot because of invisible text. */
1140 if (IT_CHARPOS (it) >= charpos)
1141 {
1142 int top_y = it.current_y;
1143 int bottom_y = line_bottom_y (&it);
1144 int window_top_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
1145
1146 if (top_y < window_top_y)
1147 visible_p = bottom_y > window_top_y;
1148 else if (top_y < it.last_visible_y)
1149 {
1150 visible_p = 1;
1151 *fully = bottom_y <= it.last_visible_y;
1152 }
1153 }
1154 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1155 {
1156 move_it_by_lines (&it, 1, 0);
1157 if (charpos < IT_CHARPOS (it))
1158 {
1159 visible_p = 1;
1160 *fully = 0;
1161 }
1162 }
1163
1164 if (old_buffer)
1165 set_buffer_internal_1 (old_buffer);
1166
1167 current_header_line_height = current_mode_line_height = -1;
1168 return visible_p;
1169 }
1170
1171
1172 /* Return the next character from STR which is MAXLEN bytes long.
1173 Return in *LEN the length of the character. This is like
1174 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1175 we find one, we return a `?', but with the length of the invalid
1176 character. */
1177
1178 static INLINE int
1179 string_char_and_length (str, maxlen, len)
1180 const unsigned char *str;
1181 int maxlen, *len;
1182 {
1183 int c;
1184
1185 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1186 if (!CHAR_VALID_P (c, 1))
1187 /* We may not change the length here because other places in Emacs
1188 don't use this function, i.e. they silently accept invalid
1189 characters. */
1190 c = '?';
1191
1192 return c;
1193 }
1194
1195
1196
1197 /* Given a position POS containing a valid character and byte position
1198 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1199
1200 static struct text_pos
1201 string_pos_nchars_ahead (pos, string, nchars)
1202 struct text_pos pos;
1203 Lisp_Object string;
1204 int nchars;
1205 {
1206 xassert (STRINGP (string) && nchars >= 0);
1207
1208 if (STRING_MULTIBYTE (string))
1209 {
1210 int rest = SBYTES (string) - BYTEPOS (pos);
1211 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1212 int len;
1213
1214 while (nchars--)
1215 {
1216 string_char_and_length (p, rest, &len);
1217 p += len, rest -= len;
1218 xassert (rest >= 0);
1219 CHARPOS (pos) += 1;
1220 BYTEPOS (pos) += len;
1221 }
1222 }
1223 else
1224 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1225
1226 return pos;
1227 }
1228
1229
1230 /* Value is the text position, i.e. character and byte position,
1231 for character position CHARPOS in STRING. */
1232
1233 static INLINE struct text_pos
1234 string_pos (charpos, string)
1235 int charpos;
1236 Lisp_Object string;
1237 {
1238 struct text_pos pos;
1239 xassert (STRINGP (string));
1240 xassert (charpos >= 0);
1241 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1242 return pos;
1243 }
1244
1245
1246 /* Value is a text position, i.e. character and byte position, for
1247 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1248 means recognize multibyte characters. */
1249
1250 static struct text_pos
1251 c_string_pos (charpos, s, multibyte_p)
1252 int charpos;
1253 unsigned char *s;
1254 int multibyte_p;
1255 {
1256 struct text_pos pos;
1257
1258 xassert (s != NULL);
1259 xassert (charpos >= 0);
1260
1261 if (multibyte_p)
1262 {
1263 int rest = strlen (s), len;
1264
1265 SET_TEXT_POS (pos, 0, 0);
1266 while (charpos--)
1267 {
1268 string_char_and_length (s, rest, &len);
1269 s += len, rest -= len;
1270 xassert (rest >= 0);
1271 CHARPOS (pos) += 1;
1272 BYTEPOS (pos) += len;
1273 }
1274 }
1275 else
1276 SET_TEXT_POS (pos, charpos, charpos);
1277
1278 return pos;
1279 }
1280
1281
1282 /* Value is the number of characters in C string S. MULTIBYTE_P
1283 non-zero means recognize multibyte characters. */
1284
1285 static int
1286 number_of_chars (s, multibyte_p)
1287 unsigned char *s;
1288 int multibyte_p;
1289 {
1290 int nchars;
1291
1292 if (multibyte_p)
1293 {
1294 int rest = strlen (s), len;
1295 unsigned char *p = (unsigned char *) s;
1296
1297 for (nchars = 0; rest > 0; ++nchars)
1298 {
1299 string_char_and_length (p, rest, &len);
1300 rest -= len, p += len;
1301 }
1302 }
1303 else
1304 nchars = strlen (s);
1305
1306 return nchars;
1307 }
1308
1309
1310 /* Compute byte position NEWPOS->bytepos corresponding to
1311 NEWPOS->charpos. POS is a known position in string STRING.
1312 NEWPOS->charpos must be >= POS.charpos. */
1313
1314 static void
1315 compute_string_pos (newpos, pos, string)
1316 struct text_pos *newpos, pos;
1317 Lisp_Object string;
1318 {
1319 xassert (STRINGP (string));
1320 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1321
1322 if (STRING_MULTIBYTE (string))
1323 *newpos = string_pos_nchars_ahead (pos, string,
1324 CHARPOS (*newpos) - CHARPOS (pos));
1325 else
1326 BYTEPOS (*newpos) = CHARPOS (*newpos);
1327 }
1328
1329
1330 \f
1331 /***********************************************************************
1332 Lisp form evaluation
1333 ***********************************************************************/
1334
1335 /* Error handler for safe_eval and safe_call. */
1336
1337 static Lisp_Object
1338 safe_eval_handler (arg)
1339 Lisp_Object arg;
1340 {
1341 add_to_log ("Error during redisplay: %s", arg, Qnil);
1342 return Qnil;
1343 }
1344
1345
1346 /* Evaluate SEXPR and return the result, or nil if something went
1347 wrong. Prevent redisplay during the evaluation. */
1348
1349 Lisp_Object
1350 safe_eval (sexpr)
1351 Lisp_Object sexpr;
1352 {
1353 Lisp_Object val;
1354
1355 if (inhibit_eval_during_redisplay)
1356 val = Qnil;
1357 else
1358 {
1359 int count = SPECPDL_INDEX ();
1360 struct gcpro gcpro1;
1361
1362 GCPRO1 (sexpr);
1363 specbind (Qinhibit_redisplay, Qt);
1364 /* Use Qt to ensure debugger does not run,
1365 so there is no possibility of wanting to redisplay. */
1366 val = internal_condition_case_1 (Feval, sexpr, Qt,
1367 safe_eval_handler);
1368 UNGCPRO;
1369 val = unbind_to (count, val);
1370 }
1371
1372 return val;
1373 }
1374
1375
1376 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1377 Return the result, or nil if something went wrong. Prevent
1378 redisplay during the evaluation. */
1379
1380 Lisp_Object
1381 safe_call (nargs, args)
1382 int nargs;
1383 Lisp_Object *args;
1384 {
1385 Lisp_Object val;
1386
1387 if (inhibit_eval_during_redisplay)
1388 val = Qnil;
1389 else
1390 {
1391 int count = SPECPDL_INDEX ();
1392 struct gcpro gcpro1;
1393
1394 GCPRO1 (args[0]);
1395 gcpro1.nvars = nargs;
1396 specbind (Qinhibit_redisplay, Qt);
1397 /* Use Qt to ensure debugger does not run,
1398 so there is no possibility of wanting to redisplay. */
1399 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
1400 safe_eval_handler);
1401 UNGCPRO;
1402 val = unbind_to (count, val);
1403 }
1404
1405 return val;
1406 }
1407
1408
1409 /* Call function FN with one argument ARG.
1410 Return the result, or nil if something went wrong. */
1411
1412 Lisp_Object
1413 safe_call1 (fn, arg)
1414 Lisp_Object fn, arg;
1415 {
1416 Lisp_Object args[2];
1417 args[0] = fn;
1418 args[1] = arg;
1419 return safe_call (2, args);
1420 }
1421
1422
1423 \f
1424 /***********************************************************************
1425 Debugging
1426 ***********************************************************************/
1427
1428 #if 0
1429
1430 /* Define CHECK_IT to perform sanity checks on iterators.
1431 This is for debugging. It is too slow to do unconditionally. */
1432
1433 static void
1434 check_it (it)
1435 struct it *it;
1436 {
1437 if (it->method == next_element_from_string)
1438 {
1439 xassert (STRINGP (it->string));
1440 xassert (IT_STRING_CHARPOS (*it) >= 0);
1441 }
1442 else if (it->method == next_element_from_buffer)
1443 {
1444 /* Check that character and byte positions agree. */
1445 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1446 }
1447
1448 if (it->dpvec)
1449 xassert (it->current.dpvec_index >= 0);
1450 else
1451 xassert (it->current.dpvec_index < 0);
1452 }
1453
1454 #define CHECK_IT(IT) check_it ((IT))
1455
1456 #else /* not 0 */
1457
1458 #define CHECK_IT(IT) (void) 0
1459
1460 #endif /* not 0 */
1461
1462
1463 #if GLYPH_DEBUG
1464
1465 /* Check that the window end of window W is what we expect it
1466 to be---the last row in the current matrix displaying text. */
1467
1468 static void
1469 check_window_end (w)
1470 struct window *w;
1471 {
1472 if (!MINI_WINDOW_P (w)
1473 && !NILP (w->window_end_valid))
1474 {
1475 struct glyph_row *row;
1476 xassert ((row = MATRIX_ROW (w->current_matrix,
1477 XFASTINT (w->window_end_vpos)),
1478 !row->enabled_p
1479 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1480 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1481 }
1482 }
1483
1484 #define CHECK_WINDOW_END(W) check_window_end ((W))
1485
1486 #else /* not GLYPH_DEBUG */
1487
1488 #define CHECK_WINDOW_END(W) (void) 0
1489
1490 #endif /* not GLYPH_DEBUG */
1491
1492
1493 \f
1494 /***********************************************************************
1495 Iterator initialization
1496 ***********************************************************************/
1497
1498 /* Initialize IT for displaying current_buffer in window W, starting
1499 at character position CHARPOS. CHARPOS < 0 means that no buffer
1500 position is specified which is useful when the iterator is assigned
1501 a position later. BYTEPOS is the byte position corresponding to
1502 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
1503
1504 If ROW is not null, calls to produce_glyphs with IT as parameter
1505 will produce glyphs in that row.
1506
1507 BASE_FACE_ID is the id of a base face to use. It must be one of
1508 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
1509 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
1510 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
1511
1512 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
1513 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
1514 will be initialized to use the corresponding mode line glyph row of
1515 the desired matrix of W. */
1516
1517 void
1518 init_iterator (it, w, charpos, bytepos, row, base_face_id)
1519 struct it *it;
1520 struct window *w;
1521 int charpos, bytepos;
1522 struct glyph_row *row;
1523 enum face_id base_face_id;
1524 {
1525 int highlight_region_p;
1526
1527 /* Some precondition checks. */
1528 xassert (w != NULL && it != NULL);
1529 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
1530 && charpos <= ZV));
1531
1532 /* If face attributes have been changed since the last redisplay,
1533 free realized faces now because they depend on face definitions
1534 that might have changed. Don't free faces while there might be
1535 desired matrices pending which reference these faces. */
1536 if (face_change_count && !inhibit_free_realized_faces)
1537 {
1538 face_change_count = 0;
1539 free_all_realized_faces (Qnil);
1540 }
1541
1542 /* Use one of the mode line rows of W's desired matrix if
1543 appropriate. */
1544 if (row == NULL)
1545 {
1546 if (base_face_id == MODE_LINE_FACE_ID
1547 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
1548 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
1549 else if (base_face_id == HEADER_LINE_FACE_ID)
1550 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
1551 }
1552
1553 /* Clear IT. */
1554 bzero (it, sizeof *it);
1555 it->current.overlay_string_index = -1;
1556 it->current.dpvec_index = -1;
1557 it->base_face_id = base_face_id;
1558
1559 /* The window in which we iterate over current_buffer: */
1560 XSETWINDOW (it->window, w);
1561 it->w = w;
1562 it->f = XFRAME (w->frame);
1563
1564 /* Extra space between lines (on window systems only). */
1565 if (base_face_id == DEFAULT_FACE_ID
1566 && FRAME_WINDOW_P (it->f))
1567 {
1568 if (NATNUMP (current_buffer->extra_line_spacing))
1569 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
1570 else if (it->f->extra_line_spacing > 0)
1571 it->extra_line_spacing = it->f->extra_line_spacing;
1572 }
1573
1574 /* If realized faces have been removed, e.g. because of face
1575 attribute changes of named faces, recompute them. When running
1576 in batch mode, the face cache of Vterminal_frame is null. If
1577 we happen to get called, make a dummy face cache. */
1578 if (
1579 #ifndef WINDOWSNT
1580 noninteractive &&
1581 #endif
1582 FRAME_FACE_CACHE (it->f) == NULL)
1583 init_frame_faces (it->f);
1584 if (FRAME_FACE_CACHE (it->f)->used == 0)
1585 recompute_basic_faces (it->f);
1586
1587 /* Current value of the `space-width', and 'height' properties. */
1588 it->space_width = Qnil;
1589 it->font_height = Qnil;
1590
1591 /* Are control characters displayed as `^C'? */
1592 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
1593
1594 /* -1 means everything between a CR and the following line end
1595 is invisible. >0 means lines indented more than this value are
1596 invisible. */
1597 it->selective = (INTEGERP (current_buffer->selective_display)
1598 ? XFASTINT (current_buffer->selective_display)
1599 : (!NILP (current_buffer->selective_display)
1600 ? -1 : 0));
1601 it->selective_display_ellipsis_p
1602 = !NILP (current_buffer->selective_display_ellipses);
1603
1604 /* Display table to use. */
1605 it->dp = window_display_table (w);
1606
1607 /* Are multibyte characters enabled in current_buffer? */
1608 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1609
1610 /* Non-zero if we should highlight the region. */
1611 highlight_region_p
1612 = (!NILP (Vtransient_mark_mode)
1613 && !NILP (current_buffer->mark_active)
1614 && XMARKER (current_buffer->mark)->buffer != 0);
1615
1616 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
1617 start and end of a visible region in window IT->w. Set both to
1618 -1 to indicate no region. */
1619 if (highlight_region_p
1620 /* Maybe highlight only in selected window. */
1621 && (/* Either show region everywhere. */
1622 highlight_nonselected_windows
1623 /* Or show region in the selected window. */
1624 || w == XWINDOW (selected_window)
1625 /* Or show the region if we are in the mini-buffer and W is
1626 the window the mini-buffer refers to. */
1627 || (MINI_WINDOW_P (XWINDOW (selected_window))
1628 && WINDOWP (minibuf_selected_window)
1629 && w == XWINDOW (minibuf_selected_window))))
1630 {
1631 int charpos = marker_position (current_buffer->mark);
1632 it->region_beg_charpos = min (PT, charpos);
1633 it->region_end_charpos = max (PT, charpos);
1634 }
1635 else
1636 it->region_beg_charpos = it->region_end_charpos = -1;
1637
1638 /* Get the position at which the redisplay_end_trigger hook should
1639 be run, if it is to be run at all. */
1640 if (MARKERP (w->redisplay_end_trigger)
1641 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
1642 it->redisplay_end_trigger_charpos
1643 = marker_position (w->redisplay_end_trigger);
1644 else if (INTEGERP (w->redisplay_end_trigger))
1645 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
1646
1647 /* Correct bogus values of tab_width. */
1648 it->tab_width = XINT (current_buffer->tab_width);
1649 if (it->tab_width <= 0 || it->tab_width > 1000)
1650 it->tab_width = 8;
1651
1652 /* Are lines in the display truncated? */
1653 it->truncate_lines_p
1654 = (base_face_id != DEFAULT_FACE_ID
1655 || XINT (it->w->hscroll)
1656 || (truncate_partial_width_windows
1657 && !WINDOW_FULL_WIDTH_P (it->w))
1658 || !NILP (current_buffer->truncate_lines));
1659
1660 /* Get dimensions of truncation and continuation glyphs. These are
1661 displayed as fringe bitmaps under X, so we don't need them for such
1662 frames. */
1663 if (!FRAME_WINDOW_P (it->f))
1664 {
1665 if (it->truncate_lines_p)
1666 {
1667 /* We will need the truncation glyph. */
1668 xassert (it->glyph_row == NULL);
1669 produce_special_glyphs (it, IT_TRUNCATION);
1670 it->truncation_pixel_width = it->pixel_width;
1671 }
1672 else
1673 {
1674 /* We will need the continuation glyph. */
1675 xassert (it->glyph_row == NULL);
1676 produce_special_glyphs (it, IT_CONTINUATION);
1677 it->continuation_pixel_width = it->pixel_width;
1678 }
1679
1680 /* Reset these values to zero because the produce_special_glyphs
1681 above has changed them. */
1682 it->pixel_width = it->ascent = it->descent = 0;
1683 it->phys_ascent = it->phys_descent = 0;
1684 }
1685
1686 /* Set this after getting the dimensions of truncation and
1687 continuation glyphs, so that we don't produce glyphs when calling
1688 produce_special_glyphs, above. */
1689 it->glyph_row = row;
1690 it->area = TEXT_AREA;
1691
1692 /* Get the dimensions of the display area. The display area
1693 consists of the visible window area plus a horizontally scrolled
1694 part to the left of the window. All x-values are relative to the
1695 start of this total display area. */
1696 if (base_face_id != DEFAULT_FACE_ID)
1697 {
1698 /* Mode lines, menu bar in terminal frames. */
1699 it->first_visible_x = 0;
1700 it->last_visible_x = XFASTINT (w->width) * CANON_X_UNIT (it->f);
1701 }
1702 else
1703 {
1704 it->first_visible_x
1705 = XFASTINT (it->w->hscroll) * CANON_X_UNIT (it->f);
1706 it->last_visible_x = (it->first_visible_x
1707 + window_box_width (w, TEXT_AREA));
1708
1709 /* If we truncate lines, leave room for the truncator glyph(s) at
1710 the right margin. Otherwise, leave room for the continuation
1711 glyph(s). Truncation and continuation glyphs are not inserted
1712 for window-based redisplay. */
1713 if (!FRAME_WINDOW_P (it->f))
1714 {
1715 if (it->truncate_lines_p)
1716 it->last_visible_x -= it->truncation_pixel_width;
1717 else
1718 it->last_visible_x -= it->continuation_pixel_width;
1719 }
1720
1721 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
1722 it->current_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w) + w->vscroll;
1723 }
1724
1725 /* Leave room for a border glyph. */
1726 if (!FRAME_WINDOW_P (it->f)
1727 && !WINDOW_RIGHTMOST_P (it->w))
1728 it->last_visible_x -= 1;
1729
1730 it->last_visible_y = window_text_bottom_y (w);
1731
1732 /* For mode lines and alike, arrange for the first glyph having a
1733 left box line if the face specifies a box. */
1734 if (base_face_id != DEFAULT_FACE_ID)
1735 {
1736 struct face *face;
1737
1738 it->face_id = base_face_id;
1739
1740 /* If we have a boxed mode line, make the first character appear
1741 with a left box line. */
1742 face = FACE_FROM_ID (it->f, base_face_id);
1743 if (face->box != FACE_NO_BOX)
1744 it->start_of_box_run_p = 1;
1745 }
1746
1747 /* If a buffer position was specified, set the iterator there,
1748 getting overlays and face properties from that position. */
1749 if (charpos >= BUF_BEG (current_buffer))
1750 {
1751 it->end_charpos = ZV;
1752 it->face_id = -1;
1753 IT_CHARPOS (*it) = charpos;
1754
1755 /* Compute byte position if not specified. */
1756 if (bytepos < charpos)
1757 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
1758 else
1759 IT_BYTEPOS (*it) = bytepos;
1760
1761 /* Compute faces etc. */
1762 reseat (it, it->current.pos, 1);
1763 }
1764
1765 CHECK_IT (it);
1766 }
1767
1768
1769 /* Initialize IT for the display of window W with window start POS. */
1770
1771 void
1772 start_display (it, w, pos)
1773 struct it *it;
1774 struct window *w;
1775 struct text_pos pos;
1776 {
1777 struct glyph_row *row;
1778 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
1779
1780 row = w->desired_matrix->rows + first_vpos;
1781 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
1782
1783 if (!it->truncate_lines_p)
1784 {
1785 int start_at_line_beg_p;
1786 int first_y = it->current_y;
1787
1788 /* If window start is not at a line start, skip forward to POS to
1789 get the correct continuation lines width. */
1790 start_at_line_beg_p = (CHARPOS (pos) == BEGV
1791 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
1792 if (!start_at_line_beg_p)
1793 {
1794 reseat_at_previous_visible_line_start (it);
1795 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
1796
1797 /* If lines are continued, this line may end in the middle
1798 of a multi-glyph character (e.g. a control character
1799 displayed as \003, or in the middle of an overlay
1800 string). In this case move_it_to above will not have
1801 taken us to the start of the continuation line but to the
1802 end of the continued line. */
1803 if (it->current_x > 0)
1804 {
1805 if (it->current.dpvec_index >= 0
1806 || it->current.overlay_string_index >= 0)
1807 {
1808 set_iterator_to_next (it, 1);
1809 move_it_in_display_line_to (it, -1, -1, 0);
1810 }
1811
1812 it->continuation_lines_width += it->current_x;
1813 }
1814
1815 /* We're starting a new display line, not affected by the
1816 height of the continued line, so clear the appropriate
1817 fields in the iterator structure. */
1818 it->max_ascent = it->max_descent = 0;
1819 it->max_phys_ascent = it->max_phys_descent = 0;
1820
1821 it->current_y = first_y;
1822 it->vpos = 0;
1823 it->current_x = it->hpos = 0;
1824 }
1825 }
1826
1827 #if 0 /* Don't assert the following because start_display is sometimes
1828 called intentionally with a window start that is not at a
1829 line start. Please leave this code in as a comment. */
1830
1831 /* Window start should be on a line start, now. */
1832 xassert (it->continuation_lines_width
1833 || IT_CHARPOS (it) == BEGV
1834 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
1835 #endif /* 0 */
1836 }
1837
1838
1839 /* Return 1 if POS is a position in ellipses displayed for invisible
1840 text. W is the window we display, for text property lookup. */
1841
1842 static int
1843 in_ellipses_for_invisible_text_p (pos, w)
1844 struct display_pos *pos;
1845 struct window *w;
1846 {
1847 Lisp_Object prop, window;
1848 int ellipses_p = 0;
1849 int charpos = CHARPOS (pos->pos);
1850
1851 /* If POS specifies a position in a display vector, this might
1852 be for an ellipsis displayed for invisible text. We won't
1853 get the iterator set up for delivering that ellipsis unless
1854 we make sure that it gets aware of the invisible text. */
1855 if (pos->dpvec_index >= 0
1856 && pos->overlay_string_index < 0
1857 && CHARPOS (pos->string_pos) < 0
1858 && charpos > BEGV
1859 && (XSETWINDOW (window, w),
1860 prop = Fget_char_property (make_number (charpos),
1861 Qinvisible, window),
1862 !TEXT_PROP_MEANS_INVISIBLE (prop)))
1863 {
1864 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
1865 window);
1866 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
1867 }
1868
1869 return ellipses_p;
1870 }
1871
1872
1873 /* Initialize IT for stepping through current_buffer in window W,
1874 starting at position POS that includes overlay string and display
1875 vector/ control character translation position information. Value
1876 is zero if there are overlay strings with newlines at POS. */
1877
1878 static int
1879 init_from_display_pos (it, w, pos)
1880 struct it *it;
1881 struct window *w;
1882 struct display_pos *pos;
1883 {
1884 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
1885 int i, overlay_strings_with_newlines = 0;
1886
1887 /* If POS specifies a position in a display vector, this might
1888 be for an ellipsis displayed for invisible text. We won't
1889 get the iterator set up for delivering that ellipsis unless
1890 we make sure that it gets aware of the invisible text. */
1891 if (in_ellipses_for_invisible_text_p (pos, w))
1892 {
1893 --charpos;
1894 bytepos = 0;
1895 }
1896
1897 /* Keep in mind: the call to reseat in init_iterator skips invisible
1898 text, so we might end up at a position different from POS. This
1899 is only a problem when POS is a row start after a newline and an
1900 overlay starts there with an after-string, and the overlay has an
1901 invisible property. Since we don't skip invisible text in
1902 display_line and elsewhere immediately after consuming the
1903 newline before the row start, such a POS will not be in a string,
1904 but the call to init_iterator below will move us to the
1905 after-string. */
1906 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
1907
1908 for (i = 0; i < it->n_overlay_strings; ++i)
1909 {
1910 const char *s = SDATA (it->overlay_strings[i]);
1911 const char *e = s + SBYTES (it->overlay_strings[i]);
1912
1913 while (s < e && *s != '\n')
1914 ++s;
1915
1916 if (s < e)
1917 {
1918 overlay_strings_with_newlines = 1;
1919 break;
1920 }
1921 }
1922
1923 /* If position is within an overlay string, set up IT to the right
1924 overlay string. */
1925 if (pos->overlay_string_index >= 0)
1926 {
1927 int relative_index;
1928
1929 /* If the first overlay string happens to have a `display'
1930 property for an image, the iterator will be set up for that
1931 image, and we have to undo that setup first before we can
1932 correct the overlay string index. */
1933 if (it->method == next_element_from_image)
1934 pop_it (it);
1935
1936 /* We already have the first chunk of overlay strings in
1937 IT->overlay_strings. Load more until the one for
1938 pos->overlay_string_index is in IT->overlay_strings. */
1939 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
1940 {
1941 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
1942 it->current.overlay_string_index = 0;
1943 while (n--)
1944 {
1945 load_overlay_strings (it, 0);
1946 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
1947 }
1948 }
1949
1950 it->current.overlay_string_index = pos->overlay_string_index;
1951 relative_index = (it->current.overlay_string_index
1952 % OVERLAY_STRING_CHUNK_SIZE);
1953 it->string = it->overlay_strings[relative_index];
1954 xassert (STRINGP (it->string));
1955 it->current.string_pos = pos->string_pos;
1956 it->method = next_element_from_string;
1957 }
1958
1959 #if 0 /* This is bogus because POS not having an overlay string
1960 position does not mean it's after the string. Example: A
1961 line starting with a before-string and initialization of IT
1962 to the previous row's end position. */
1963 else if (it->current.overlay_string_index >= 0)
1964 {
1965 /* If POS says we're already after an overlay string ending at
1966 POS, make sure to pop the iterator because it will be in
1967 front of that overlay string. When POS is ZV, we've thereby
1968 also ``processed'' overlay strings at ZV. */
1969 while (it->sp)
1970 pop_it (it);
1971 it->current.overlay_string_index = -1;
1972 it->method = next_element_from_buffer;
1973 if (CHARPOS (pos->pos) == ZV)
1974 it->overlay_strings_at_end_processed_p = 1;
1975 }
1976 #endif /* 0 */
1977
1978 if (CHARPOS (pos->string_pos) >= 0)
1979 {
1980 /* Recorded position is not in an overlay string, but in another
1981 string. This can only be a string from a `display' property.
1982 IT should already be filled with that string. */
1983 it->current.string_pos = pos->string_pos;
1984 xassert (STRINGP (it->string));
1985 }
1986
1987 /* Restore position in display vector translations, control
1988 character translations or ellipses. */
1989 if (pos->dpvec_index >= 0)
1990 {
1991 if (it->dpvec == NULL)
1992 get_next_display_element (it);
1993 xassert (it->dpvec && it->current.dpvec_index == 0);
1994 it->current.dpvec_index = pos->dpvec_index;
1995 }
1996
1997 CHECK_IT (it);
1998 return !overlay_strings_with_newlines;
1999 }
2000
2001
2002 /* Initialize IT for stepping through current_buffer in window W
2003 starting at ROW->start. */
2004
2005 static void
2006 init_to_row_start (it, w, row)
2007 struct it *it;
2008 struct window *w;
2009 struct glyph_row *row;
2010 {
2011 init_from_display_pos (it, w, &row->start);
2012 it->continuation_lines_width = row->continuation_lines_width;
2013 CHECK_IT (it);
2014 }
2015
2016
2017 /* Initialize IT for stepping through current_buffer in window W
2018 starting in the line following ROW, i.e. starting at ROW->end.
2019 Value is zero if there are overlay strings with newlines at ROW's
2020 end position. */
2021
2022 static int
2023 init_to_row_end (it, w, row)
2024 struct it *it;
2025 struct window *w;
2026 struct glyph_row *row;
2027 {
2028 int success = 0;
2029
2030 if (init_from_display_pos (it, w, &row->end))
2031 {
2032 if (row->continued_p)
2033 it->continuation_lines_width
2034 = row->continuation_lines_width + row->pixel_width;
2035 CHECK_IT (it);
2036 success = 1;
2037 }
2038
2039 return success;
2040 }
2041
2042
2043
2044 \f
2045 /***********************************************************************
2046 Text properties
2047 ***********************************************************************/
2048
2049 /* Called when IT reaches IT->stop_charpos. Handle text property and
2050 overlay changes. Set IT->stop_charpos to the next position where
2051 to stop. */
2052
2053 static void
2054 handle_stop (it)
2055 struct it *it;
2056 {
2057 enum prop_handled handled;
2058 int handle_overlay_change_p = 1;
2059 struct props *p;
2060
2061 it->dpvec = NULL;
2062 it->current.dpvec_index = -1;
2063
2064 do
2065 {
2066 handled = HANDLED_NORMALLY;
2067
2068 /* Call text property handlers. */
2069 for (p = it_props; p->handler; ++p)
2070 {
2071 handled = p->handler (it);
2072
2073 if (handled == HANDLED_RECOMPUTE_PROPS)
2074 break;
2075 else if (handled == HANDLED_RETURN)
2076 return;
2077 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2078 handle_overlay_change_p = 0;
2079 }
2080
2081 if (handled != HANDLED_RECOMPUTE_PROPS)
2082 {
2083 /* Don't check for overlay strings below when set to deliver
2084 characters from a display vector. */
2085 if (it->method == next_element_from_display_vector)
2086 handle_overlay_change_p = 0;
2087
2088 /* Handle overlay changes. */
2089 if (handle_overlay_change_p)
2090 handled = handle_overlay_change (it);
2091
2092 /* Determine where to stop next. */
2093 if (handled == HANDLED_NORMALLY)
2094 compute_stop_pos (it);
2095 }
2096 }
2097 while (handled == HANDLED_RECOMPUTE_PROPS);
2098 }
2099
2100
2101 /* Compute IT->stop_charpos from text property and overlay change
2102 information for IT's current position. */
2103
2104 static void
2105 compute_stop_pos (it)
2106 struct it *it;
2107 {
2108 register INTERVAL iv, next_iv;
2109 Lisp_Object object, limit, position;
2110
2111 /* If nowhere else, stop at the end. */
2112 it->stop_charpos = it->end_charpos;
2113
2114 if (STRINGP (it->string))
2115 {
2116 /* Strings are usually short, so don't limit the search for
2117 properties. */
2118 object = it->string;
2119 limit = Qnil;
2120 position = make_number (IT_STRING_CHARPOS (*it));
2121 }
2122 else
2123 {
2124 int charpos;
2125
2126 /* If next overlay change is in front of the current stop pos
2127 (which is IT->end_charpos), stop there. Note: value of
2128 next_overlay_change is point-max if no overlay change
2129 follows. */
2130 charpos = next_overlay_change (IT_CHARPOS (*it));
2131 if (charpos < it->stop_charpos)
2132 it->stop_charpos = charpos;
2133
2134 /* If showing the region, we have to stop at the region
2135 start or end because the face might change there. */
2136 if (it->region_beg_charpos > 0)
2137 {
2138 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2139 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2140 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2141 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2142 }
2143
2144 /* Set up variables for computing the stop position from text
2145 property changes. */
2146 XSETBUFFER (object, current_buffer);
2147 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2148 position = make_number (IT_CHARPOS (*it));
2149
2150 }
2151
2152 /* Get the interval containing IT's position. Value is a null
2153 interval if there isn't such an interval. */
2154 iv = validate_interval_range (object, &position, &position, 0);
2155 if (!NULL_INTERVAL_P (iv))
2156 {
2157 Lisp_Object values_here[LAST_PROP_IDX];
2158 struct props *p;
2159
2160 /* Get properties here. */
2161 for (p = it_props; p->handler; ++p)
2162 values_here[p->idx] = textget (iv->plist, *p->name);
2163
2164 /* Look for an interval following iv that has different
2165 properties. */
2166 for (next_iv = next_interval (iv);
2167 (!NULL_INTERVAL_P (next_iv)
2168 && (NILP (limit)
2169 || XFASTINT (limit) > next_iv->position));
2170 next_iv = next_interval (next_iv))
2171 {
2172 for (p = it_props; p->handler; ++p)
2173 {
2174 Lisp_Object new_value;
2175
2176 new_value = textget (next_iv->plist, *p->name);
2177 if (!EQ (values_here[p->idx], new_value))
2178 break;
2179 }
2180
2181 if (p->handler)
2182 break;
2183 }
2184
2185 if (!NULL_INTERVAL_P (next_iv))
2186 {
2187 if (INTEGERP (limit)
2188 && next_iv->position >= XFASTINT (limit))
2189 /* No text property change up to limit. */
2190 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2191 else
2192 /* Text properties change in next_iv. */
2193 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2194 }
2195 }
2196
2197 xassert (STRINGP (it->string)
2198 || (it->stop_charpos >= BEGV
2199 && it->stop_charpos >= IT_CHARPOS (*it)));
2200 }
2201
2202
2203 /* Return the position of the next overlay change after POS in
2204 current_buffer. Value is point-max if no overlay change
2205 follows. This is like `next-overlay-change' but doesn't use
2206 xmalloc. */
2207
2208 static int
2209 next_overlay_change (pos)
2210 int pos;
2211 {
2212 int noverlays;
2213 int endpos;
2214 Lisp_Object *overlays;
2215 int len;
2216 int i;
2217
2218 /* Get all overlays at the given position. */
2219 len = 10;
2220 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2221 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2222 if (noverlays > len)
2223 {
2224 len = noverlays;
2225 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2226 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2227 }
2228
2229 /* If any of these overlays ends before endpos,
2230 use its ending point instead. */
2231 for (i = 0; i < noverlays; ++i)
2232 {
2233 Lisp_Object oend;
2234 int oendpos;
2235
2236 oend = OVERLAY_END (overlays[i]);
2237 oendpos = OVERLAY_POSITION (oend);
2238 endpos = min (endpos, oendpos);
2239 }
2240
2241 return endpos;
2242 }
2243
2244
2245 \f
2246 /***********************************************************************
2247 Fontification
2248 ***********************************************************************/
2249
2250 /* Handle changes in the `fontified' property of the current buffer by
2251 calling hook functions from Qfontification_functions to fontify
2252 regions of text. */
2253
2254 static enum prop_handled
2255 handle_fontified_prop (it)
2256 struct it *it;
2257 {
2258 Lisp_Object prop, pos;
2259 enum prop_handled handled = HANDLED_NORMALLY;
2260
2261 /* Get the value of the `fontified' property at IT's current buffer
2262 position. (The `fontified' property doesn't have a special
2263 meaning in strings.) If the value is nil, call functions from
2264 Qfontification_functions. */
2265 if (!STRINGP (it->string)
2266 && it->s == NULL
2267 && !NILP (Vfontification_functions)
2268 && !NILP (Vrun_hooks)
2269 && (pos = make_number (IT_CHARPOS (*it)),
2270 prop = Fget_char_property (pos, Qfontified, Qnil),
2271 NILP (prop)))
2272 {
2273 int count = SPECPDL_INDEX ();
2274 Lisp_Object val;
2275
2276 val = Vfontification_functions;
2277 specbind (Qfontification_functions, Qnil);
2278
2279 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2280 safe_call1 (val, pos);
2281 else
2282 {
2283 Lisp_Object globals, fn;
2284 struct gcpro gcpro1, gcpro2;
2285
2286 globals = Qnil;
2287 GCPRO2 (val, globals);
2288
2289 for (; CONSP (val); val = XCDR (val))
2290 {
2291 fn = XCAR (val);
2292
2293 if (EQ (fn, Qt))
2294 {
2295 /* A value of t indicates this hook has a local
2296 binding; it means to run the global binding too.
2297 In a global value, t should not occur. If it
2298 does, we must ignore it to avoid an endless
2299 loop. */
2300 for (globals = Fdefault_value (Qfontification_functions);
2301 CONSP (globals);
2302 globals = XCDR (globals))
2303 {
2304 fn = XCAR (globals);
2305 if (!EQ (fn, Qt))
2306 safe_call1 (fn, pos);
2307 }
2308 }
2309 else
2310 safe_call1 (fn, pos);
2311 }
2312
2313 UNGCPRO;
2314 }
2315
2316 unbind_to (count, Qnil);
2317
2318 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2319 something. This avoids an endless loop if they failed to
2320 fontify the text for which reason ever. */
2321 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2322 handled = HANDLED_RECOMPUTE_PROPS;
2323 }
2324
2325 return handled;
2326 }
2327
2328
2329 \f
2330 /***********************************************************************
2331 Faces
2332 ***********************************************************************/
2333
2334 /* Set up iterator IT from face properties at its current position.
2335 Called from handle_stop. */
2336
2337 static enum prop_handled
2338 handle_face_prop (it)
2339 struct it *it;
2340 {
2341 int new_face_id, next_stop;
2342
2343 if (!STRINGP (it->string))
2344 {
2345 new_face_id
2346 = face_at_buffer_position (it->w,
2347 IT_CHARPOS (*it),
2348 it->region_beg_charpos,
2349 it->region_end_charpos,
2350 &next_stop,
2351 (IT_CHARPOS (*it)
2352 + TEXT_PROP_DISTANCE_LIMIT),
2353 0);
2354
2355 /* Is this a start of a run of characters with box face?
2356 Caveat: this can be called for a freshly initialized
2357 iterator; face_id is -1 in this case. We know that the new
2358 face will not change until limit, i.e. if the new face has a
2359 box, all characters up to limit will have one. But, as
2360 usual, we don't know whether limit is really the end. */
2361 if (new_face_id != it->face_id)
2362 {
2363 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2364
2365 /* If new face has a box but old face has not, this is
2366 the start of a run of characters with box, i.e. it has
2367 a shadow on the left side. The value of face_id of the
2368 iterator will be -1 if this is the initial call that gets
2369 the face. In this case, we have to look in front of IT's
2370 position and see whether there is a face != new_face_id. */
2371 it->start_of_box_run_p
2372 = (new_face->box != FACE_NO_BOX
2373 && (it->face_id >= 0
2374 || IT_CHARPOS (*it) == BEG
2375 || new_face_id != face_before_it_pos (it)));
2376 it->face_box_p = new_face->box != FACE_NO_BOX;
2377 }
2378 }
2379 else
2380 {
2381 int base_face_id, bufpos;
2382
2383 if (it->current.overlay_string_index >= 0)
2384 bufpos = IT_CHARPOS (*it);
2385 else
2386 bufpos = 0;
2387
2388 /* For strings from a buffer, i.e. overlay strings or strings
2389 from a `display' property, use the face at IT's current
2390 buffer position as the base face to merge with, so that
2391 overlay strings appear in the same face as surrounding
2392 text, unless they specify their own faces. */
2393 base_face_id = underlying_face_id (it);
2394
2395 new_face_id = face_at_string_position (it->w,
2396 it->string,
2397 IT_STRING_CHARPOS (*it),
2398 bufpos,
2399 it->region_beg_charpos,
2400 it->region_end_charpos,
2401 &next_stop,
2402 base_face_id, 0);
2403
2404 #if 0 /* This shouldn't be neccessary. Let's check it. */
2405 /* If IT is used to display a mode line we would really like to
2406 use the mode line face instead of the frame's default face. */
2407 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2408 && new_face_id == DEFAULT_FACE_ID)
2409 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
2410 #endif
2411
2412 /* Is this a start of a run of characters with box? Caveat:
2413 this can be called for a freshly allocated iterator; face_id
2414 is -1 is this case. We know that the new face will not
2415 change until the next check pos, i.e. if the new face has a
2416 box, all characters up to that position will have a
2417 box. But, as usual, we don't know whether that position
2418 is really the end. */
2419 if (new_face_id != it->face_id)
2420 {
2421 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2422 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2423
2424 /* If new face has a box but old face hasn't, this is the
2425 start of a run of characters with box, i.e. it has a
2426 shadow on the left side. */
2427 it->start_of_box_run_p
2428 = new_face->box && (old_face == NULL || !old_face->box);
2429 it->face_box_p = new_face->box != FACE_NO_BOX;
2430 }
2431 }
2432
2433 it->face_id = new_face_id;
2434 return HANDLED_NORMALLY;
2435 }
2436
2437
2438 /* Return the ID of the face ``underlying'' IT's current position,
2439 which is in a string. If the iterator is associated with a
2440 buffer, return the face at IT's current buffer position.
2441 Otherwise, use the iterator's base_face_id. */
2442
2443 static int
2444 underlying_face_id (it)
2445 struct it *it;
2446 {
2447 int face_id = it->base_face_id, i;
2448
2449 xassert (STRINGP (it->string));
2450
2451 for (i = it->sp - 1; i >= 0; --i)
2452 if (NILP (it->stack[i].string))
2453 face_id = it->stack[i].face_id;
2454
2455 return face_id;
2456 }
2457
2458
2459 /* Compute the face one character before or after the current position
2460 of IT. BEFORE_P non-zero means get the face in front of IT's
2461 position. Value is the id of the face. */
2462
2463 static int
2464 face_before_or_after_it_pos (it, before_p)
2465 struct it *it;
2466 int before_p;
2467 {
2468 int face_id, limit;
2469 int next_check_charpos;
2470 struct text_pos pos;
2471
2472 xassert (it->s == NULL);
2473
2474 if (STRINGP (it->string))
2475 {
2476 int bufpos, base_face_id;
2477
2478 /* No face change past the end of the string (for the case
2479 we are padding with spaces). No face change before the
2480 string start. */
2481 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
2482 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2483 return it->face_id;
2484
2485 /* Set pos to the position before or after IT's current position. */
2486 if (before_p)
2487 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2488 else
2489 /* For composition, we must check the character after the
2490 composition. */
2491 pos = (it->what == IT_COMPOSITION
2492 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
2493 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
2494
2495 if (it->current.overlay_string_index >= 0)
2496 bufpos = IT_CHARPOS (*it);
2497 else
2498 bufpos = 0;
2499
2500 base_face_id = underlying_face_id (it);
2501
2502 /* Get the face for ASCII, or unibyte. */
2503 face_id = face_at_string_position (it->w,
2504 it->string,
2505 CHARPOS (pos),
2506 bufpos,
2507 it->region_beg_charpos,
2508 it->region_end_charpos,
2509 &next_check_charpos,
2510 base_face_id, 0);
2511
2512 /* Correct the face for charsets different from ASCII. Do it
2513 for the multibyte case only. The face returned above is
2514 suitable for unibyte text if IT->string is unibyte. */
2515 if (STRING_MULTIBYTE (it->string))
2516 {
2517 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
2518 int rest = SBYTES (it->string) - BYTEPOS (pos);
2519 int c, len;
2520 struct face *face = FACE_FROM_ID (it->f, face_id);
2521
2522 c = string_char_and_length (p, rest, &len);
2523 face_id = FACE_FOR_CHAR (it->f, face, c);
2524 }
2525 }
2526 else
2527 {
2528 if ((IT_CHARPOS (*it) >= ZV && !before_p)
2529 || (IT_CHARPOS (*it) <= BEGV && before_p))
2530 return it->face_id;
2531
2532 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
2533 pos = it->current.pos;
2534
2535 if (before_p)
2536 DEC_TEXT_POS (pos, it->multibyte_p);
2537 else
2538 {
2539 if (it->what == IT_COMPOSITION)
2540 /* For composition, we must check the position after the
2541 composition. */
2542 pos.charpos += it->cmp_len, pos.bytepos += it->len;
2543 else
2544 INC_TEXT_POS (pos, it->multibyte_p);
2545 }
2546
2547 /* Determine face for CHARSET_ASCII, or unibyte. */
2548 face_id = face_at_buffer_position (it->w,
2549 CHARPOS (pos),
2550 it->region_beg_charpos,
2551 it->region_end_charpos,
2552 &next_check_charpos,
2553 limit, 0);
2554
2555 /* Correct the face for charsets different from ASCII. Do it
2556 for the multibyte case only. The face returned above is
2557 suitable for unibyte text if current_buffer is unibyte. */
2558 if (it->multibyte_p)
2559 {
2560 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
2561 struct face *face = FACE_FROM_ID (it->f, face_id);
2562 face_id = FACE_FOR_CHAR (it->f, face, c);
2563 }
2564 }
2565
2566 return face_id;
2567 }
2568
2569
2570 \f
2571 /***********************************************************************
2572 Invisible text
2573 ***********************************************************************/
2574
2575 /* Set up iterator IT from invisible properties at its current
2576 position. Called from handle_stop. */
2577
2578 static enum prop_handled
2579 handle_invisible_prop (it)
2580 struct it *it;
2581 {
2582 enum prop_handled handled = HANDLED_NORMALLY;
2583
2584 if (STRINGP (it->string))
2585 {
2586 extern Lisp_Object Qinvisible;
2587 Lisp_Object prop, end_charpos, limit, charpos;
2588
2589 /* Get the value of the invisible text property at the
2590 current position. Value will be nil if there is no such
2591 property. */
2592 charpos = make_number (IT_STRING_CHARPOS (*it));
2593 prop = Fget_text_property (charpos, Qinvisible, it->string);
2594
2595 if (!NILP (prop)
2596 && IT_STRING_CHARPOS (*it) < it->end_charpos)
2597 {
2598 handled = HANDLED_RECOMPUTE_PROPS;
2599
2600 /* Get the position at which the next change of the
2601 invisible text property can be found in IT->string.
2602 Value will be nil if the property value is the same for
2603 all the rest of IT->string. */
2604 XSETINT (limit, SCHARS (it->string));
2605 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2606 it->string, limit);
2607
2608 /* Text at current position is invisible. The next
2609 change in the property is at position end_charpos.
2610 Move IT's current position to that position. */
2611 if (INTEGERP (end_charpos)
2612 && XFASTINT (end_charpos) < XFASTINT (limit))
2613 {
2614 struct text_pos old;
2615 old = it->current.string_pos;
2616 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
2617 compute_string_pos (&it->current.string_pos, old, it->string);
2618 }
2619 else
2620 {
2621 /* The rest of the string is invisible. If this is an
2622 overlay string, proceed with the next overlay string
2623 or whatever comes and return a character from there. */
2624 if (it->current.overlay_string_index >= 0)
2625 {
2626 next_overlay_string (it);
2627 /* Don't check for overlay strings when we just
2628 finished processing them. */
2629 handled = HANDLED_OVERLAY_STRING_CONSUMED;
2630 }
2631 else
2632 {
2633 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
2634 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
2635 }
2636 }
2637 }
2638 }
2639 else
2640 {
2641 int invis_p, newpos, next_stop, start_charpos;
2642 Lisp_Object pos, prop, overlay;
2643
2644 /* First of all, is there invisible text at this position? */
2645 start_charpos = IT_CHARPOS (*it);
2646 pos = make_number (IT_CHARPOS (*it));
2647 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
2648 &overlay);
2649 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
2650
2651 /* If we are on invisible text, skip over it. */
2652 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
2653 {
2654 /* Record whether we have to display an ellipsis for the
2655 invisible text. */
2656 int display_ellipsis_p = invis_p == 2;
2657
2658 handled = HANDLED_RECOMPUTE_PROPS;
2659
2660 /* Loop skipping over invisible text. The loop is left at
2661 ZV or with IT on the first char being visible again. */
2662 do
2663 {
2664 /* Try to skip some invisible text. Return value is the
2665 position reached which can be equal to IT's position
2666 if there is nothing invisible here. This skips both
2667 over invisible text properties and overlays with
2668 invisible property. */
2669 newpos = skip_invisible (IT_CHARPOS (*it),
2670 &next_stop, ZV, it->window);
2671
2672 /* If we skipped nothing at all we weren't at invisible
2673 text in the first place. If everything to the end of
2674 the buffer was skipped, end the loop. */
2675 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
2676 invis_p = 0;
2677 else
2678 {
2679 /* We skipped some characters but not necessarily
2680 all there are. Check if we ended up on visible
2681 text. Fget_char_property returns the property of
2682 the char before the given position, i.e. if we
2683 get invis_p = 0, this means that the char at
2684 newpos is visible. */
2685 pos = make_number (newpos);
2686 prop = Fget_char_property (pos, Qinvisible, it->window);
2687 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
2688 }
2689
2690 /* If we ended up on invisible text, proceed to
2691 skip starting with next_stop. */
2692 if (invis_p)
2693 IT_CHARPOS (*it) = next_stop;
2694 }
2695 while (invis_p);
2696
2697 /* The position newpos is now either ZV or on visible text. */
2698 IT_CHARPOS (*it) = newpos;
2699 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2700
2701 /* If there are before-strings at the start of invisible
2702 text, and the text is invisible because of a text
2703 property, arrange to show before-strings because 20.x did
2704 it that way. (If the text is invisible because of an
2705 overlay property instead of a text property, this is
2706 already handled in the overlay code.) */
2707 if (NILP (overlay)
2708 && get_overlay_strings (it, start_charpos))
2709 {
2710 handled = HANDLED_RECOMPUTE_PROPS;
2711 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
2712 }
2713 else if (display_ellipsis_p)
2714 setup_for_ellipsis (it);
2715 }
2716 }
2717
2718 return handled;
2719 }
2720
2721
2722 /* Make iterator IT return `...' next. */
2723
2724 static void
2725 setup_for_ellipsis (it)
2726 struct it *it;
2727 {
2728 if (it->dp
2729 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
2730 {
2731 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
2732 it->dpvec = v->contents;
2733 it->dpend = v->contents + v->size;
2734 }
2735 else
2736 {
2737 /* Default `...'. */
2738 it->dpvec = default_invis_vector;
2739 it->dpend = default_invis_vector + 3;
2740 }
2741
2742 /* The ellipsis display does not replace the display of the
2743 character at the new position. Indicate this by setting
2744 IT->dpvec_char_len to zero. */
2745 it->dpvec_char_len = 0;
2746
2747 it->current.dpvec_index = 0;
2748 it->method = next_element_from_display_vector;
2749 }
2750
2751
2752 \f
2753 /***********************************************************************
2754 'display' property
2755 ***********************************************************************/
2756
2757 /* Set up iterator IT from `display' property at its current position.
2758 Called from handle_stop. */
2759
2760 static enum prop_handled
2761 handle_display_prop (it)
2762 struct it *it;
2763 {
2764 Lisp_Object prop, object;
2765 struct text_pos *position;
2766 int display_replaced_p = 0;
2767
2768 if (STRINGP (it->string))
2769 {
2770 object = it->string;
2771 position = &it->current.string_pos;
2772 }
2773 else
2774 {
2775 object = it->w->buffer;
2776 position = &it->current.pos;
2777 }
2778
2779 /* Reset those iterator values set from display property values. */
2780 it->font_height = Qnil;
2781 it->space_width = Qnil;
2782 it->voffset = 0;
2783
2784 /* We don't support recursive `display' properties, i.e. string
2785 values that have a string `display' property, that have a string
2786 `display' property etc. */
2787 if (!it->string_from_display_prop_p)
2788 it->area = TEXT_AREA;
2789
2790 prop = Fget_char_property (make_number (position->charpos),
2791 Qdisplay, object);
2792 if (NILP (prop))
2793 return HANDLED_NORMALLY;
2794
2795 if (CONSP (prop)
2796 /* Simple properties. */
2797 && !EQ (XCAR (prop), Qimage)
2798 && !EQ (XCAR (prop), Qspace)
2799 && !EQ (XCAR (prop), Qwhen)
2800 && !EQ (XCAR (prop), Qspace_width)
2801 && !EQ (XCAR (prop), Qheight)
2802 && !EQ (XCAR (prop), Qraise)
2803 /* Marginal area specifications. */
2804 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
2805 && !NILP (XCAR (prop)))
2806 {
2807 for (; CONSP (prop); prop = XCDR (prop))
2808 {
2809 if (handle_single_display_prop (it, XCAR (prop), object,
2810 position, display_replaced_p))
2811 display_replaced_p = 1;
2812 }
2813 }
2814 else if (VECTORP (prop))
2815 {
2816 int i;
2817 for (i = 0; i < ASIZE (prop); ++i)
2818 if (handle_single_display_prop (it, AREF (prop, i), object,
2819 position, display_replaced_p))
2820 display_replaced_p = 1;
2821 }
2822 else
2823 {
2824 if (handle_single_display_prop (it, prop, object, position, 0))
2825 display_replaced_p = 1;
2826 }
2827
2828 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
2829 }
2830
2831
2832 /* Value is the position of the end of the `display' property starting
2833 at START_POS in OBJECT. */
2834
2835 static struct text_pos
2836 display_prop_end (it, object, start_pos)
2837 struct it *it;
2838 Lisp_Object object;
2839 struct text_pos start_pos;
2840 {
2841 Lisp_Object end;
2842 struct text_pos end_pos;
2843
2844 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
2845 Qdisplay, object, Qnil);
2846 CHARPOS (end_pos) = XFASTINT (end);
2847 if (STRINGP (object))
2848 compute_string_pos (&end_pos, start_pos, it->string);
2849 else
2850 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
2851
2852 return end_pos;
2853 }
2854
2855
2856 /* Set up IT from a single `display' sub-property value PROP. OBJECT
2857 is the object in which the `display' property was found. *POSITION
2858 is the position at which it was found. DISPLAY_REPLACED_P non-zero
2859 means that we previously saw a display sub-property which already
2860 replaced text display with something else, for example an image;
2861 ignore such properties after the first one has been processed.
2862
2863 If PROP is a `space' or `image' sub-property, set *POSITION to the
2864 end position of the `display' property.
2865
2866 Value is non-zero if something was found which replaces the display
2867 of buffer or string text. */
2868
2869 static int
2870 handle_single_display_prop (it, prop, object, position,
2871 display_replaced_before_p)
2872 struct it *it;
2873 Lisp_Object prop;
2874 Lisp_Object object;
2875 struct text_pos *position;
2876 int display_replaced_before_p;
2877 {
2878 Lisp_Object value;
2879 int replaces_text_display_p = 0;
2880 Lisp_Object form;
2881
2882 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
2883 evaluated. If the result is nil, VALUE is ignored. */
2884 form = Qt;
2885 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2886 {
2887 prop = XCDR (prop);
2888 if (!CONSP (prop))
2889 return 0;
2890 form = XCAR (prop);
2891 prop = XCDR (prop);
2892 }
2893
2894 if (!NILP (form) && !EQ (form, Qt))
2895 {
2896 int count = SPECPDL_INDEX ();
2897 struct gcpro gcpro1;
2898
2899 /* Bind `object' to the object having the `display' property, a
2900 buffer or string. Bind `position' to the position in the
2901 object where the property was found, and `buffer-position'
2902 to the current position in the buffer. */
2903 specbind (Qobject, object);
2904 specbind (Qposition, make_number (CHARPOS (*position)));
2905 specbind (Qbuffer_position,
2906 make_number (STRINGP (object)
2907 ? IT_CHARPOS (*it) : CHARPOS (*position)));
2908 GCPRO1 (form);
2909 form = safe_eval (form);
2910 UNGCPRO;
2911 unbind_to (count, Qnil);
2912 }
2913
2914 if (NILP (form))
2915 return 0;
2916
2917 if (CONSP (prop)
2918 && EQ (XCAR (prop), Qheight)
2919 && CONSP (XCDR (prop)))
2920 {
2921 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2922 return 0;
2923
2924 /* `(height HEIGHT)'. */
2925 it->font_height = XCAR (XCDR (prop));
2926 if (!NILP (it->font_height))
2927 {
2928 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2929 int new_height = -1;
2930
2931 if (CONSP (it->font_height)
2932 && (EQ (XCAR (it->font_height), Qplus)
2933 || EQ (XCAR (it->font_height), Qminus))
2934 && CONSP (XCDR (it->font_height))
2935 && INTEGERP (XCAR (XCDR (it->font_height))))
2936 {
2937 /* `(+ N)' or `(- N)' where N is an integer. */
2938 int steps = XINT (XCAR (XCDR (it->font_height)));
2939 if (EQ (XCAR (it->font_height), Qplus))
2940 steps = - steps;
2941 it->face_id = smaller_face (it->f, it->face_id, steps);
2942 }
2943 else if (FUNCTIONP (it->font_height))
2944 {
2945 /* Call function with current height as argument.
2946 Value is the new height. */
2947 Lisp_Object height;
2948 height = safe_call1 (it->font_height,
2949 face->lface[LFACE_HEIGHT_INDEX]);
2950 if (NUMBERP (height))
2951 new_height = XFLOATINT (height);
2952 }
2953 else if (NUMBERP (it->font_height))
2954 {
2955 /* Value is a multiple of the canonical char height. */
2956 struct face *face;
2957
2958 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
2959 new_height = (XFLOATINT (it->font_height)
2960 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
2961 }
2962 else
2963 {
2964 /* Evaluate IT->font_height with `height' bound to the
2965 current specified height to get the new height. */
2966 Lisp_Object value;
2967 int count = SPECPDL_INDEX ();
2968
2969 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
2970 value = safe_eval (it->font_height);
2971 unbind_to (count, Qnil);
2972
2973 if (NUMBERP (value))
2974 new_height = XFLOATINT (value);
2975 }
2976
2977 if (new_height > 0)
2978 it->face_id = face_with_height (it->f, it->face_id, new_height);
2979 }
2980 }
2981 else if (CONSP (prop)
2982 && EQ (XCAR (prop), Qspace_width)
2983 && CONSP (XCDR (prop)))
2984 {
2985 /* `(space_width WIDTH)'. */
2986 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2987 return 0;
2988
2989 value = XCAR (XCDR (prop));
2990 if (NUMBERP (value) && XFLOATINT (value) > 0)
2991 it->space_width = value;
2992 }
2993 else if (CONSP (prop)
2994 && EQ (XCAR (prop), Qraise)
2995 && CONSP (XCDR (prop)))
2996 {
2997 /* `(raise FACTOR)'. */
2998 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2999 return 0;
3000
3001 #ifdef HAVE_WINDOW_SYSTEM
3002 value = XCAR (XCDR (prop));
3003 if (NUMBERP (value))
3004 {
3005 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3006 it->voffset = - (XFLOATINT (value)
3007 * (FONT_HEIGHT (face->font)));
3008 }
3009 #endif /* HAVE_WINDOW_SYSTEM */
3010 }
3011 else if (!it->string_from_display_prop_p)
3012 {
3013 /* `((margin left-margin) VALUE)' or `((margin right-margin)
3014 VALUE) or `((margin nil) VALUE)' or VALUE. */
3015 Lisp_Object location, value;
3016 struct text_pos start_pos;
3017 int valid_p;
3018
3019 /* Characters having this form of property are not displayed, so
3020 we have to find the end of the property. */
3021 start_pos = *position;
3022 *position = display_prop_end (it, object, start_pos);
3023 value = Qnil;
3024
3025 /* Let's stop at the new position and assume that all
3026 text properties change there. */
3027 it->stop_charpos = position->charpos;
3028
3029 location = Qunbound;
3030 if (CONSP (prop) && CONSP (XCAR (prop)))
3031 {
3032 Lisp_Object tem;
3033
3034 value = XCDR (prop);
3035 if (CONSP (value))
3036 value = XCAR (value);
3037
3038 tem = XCAR (prop);
3039 if (EQ (XCAR (tem), Qmargin)
3040 && (tem = XCDR (tem),
3041 tem = CONSP (tem) ? XCAR (tem) : Qnil,
3042 (NILP (tem)
3043 || EQ (tem, Qleft_margin)
3044 || EQ (tem, Qright_margin))))
3045 location = tem;
3046 }
3047
3048 if (EQ (location, Qunbound))
3049 {
3050 location = Qnil;
3051 value = prop;
3052 }
3053
3054 #ifdef HAVE_WINDOW_SYSTEM
3055 if (FRAME_TERMCAP_P (it->f))
3056 valid_p = STRINGP (value);
3057 else
3058 valid_p = (STRINGP (value)
3059 || (CONSP (value) && EQ (XCAR (value), Qspace))
3060 || valid_image_p (value));
3061 #else /* not HAVE_WINDOW_SYSTEM */
3062 valid_p = STRINGP (value);
3063 #endif /* not HAVE_WINDOW_SYSTEM */
3064
3065 if ((EQ (location, Qleft_margin)
3066 || EQ (location, Qright_margin)
3067 || NILP (location))
3068 && valid_p
3069 && !display_replaced_before_p)
3070 {
3071 replaces_text_display_p = 1;
3072
3073 /* Save current settings of IT so that we can restore them
3074 when we are finished with the glyph property value. */
3075 push_it (it);
3076
3077 if (NILP (location))
3078 it->area = TEXT_AREA;
3079 else if (EQ (location, Qleft_margin))
3080 it->area = LEFT_MARGIN_AREA;
3081 else
3082 it->area = RIGHT_MARGIN_AREA;
3083
3084 if (STRINGP (value))
3085 {
3086 it->string = value;
3087 it->multibyte_p = STRING_MULTIBYTE (it->string);
3088 it->current.overlay_string_index = -1;
3089 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3090 it->end_charpos = it->string_nchars = SCHARS (it->string);
3091 it->method = next_element_from_string;
3092 it->stop_charpos = 0;
3093 it->string_from_display_prop_p = 1;
3094 /* Say that we haven't consumed the characters with
3095 `display' property yet. The call to pop_it in
3096 set_iterator_to_next will clean this up. */
3097 *position = start_pos;
3098 }
3099 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3100 {
3101 it->method = next_element_from_stretch;
3102 it->object = value;
3103 it->current.pos = it->position = start_pos;
3104 }
3105 #ifdef HAVE_WINDOW_SYSTEM
3106 else
3107 {
3108 it->what = IT_IMAGE;
3109 it->image_id = lookup_image (it->f, value);
3110 it->position = start_pos;
3111 it->object = NILP (object) ? it->w->buffer : object;
3112 it->method = next_element_from_image;
3113
3114 /* Say that we haven't consumed the characters with
3115 `display' property yet. The call to pop_it in
3116 set_iterator_to_next will clean this up. */
3117 *position = start_pos;
3118 }
3119 #endif /* HAVE_WINDOW_SYSTEM */
3120 }
3121 else
3122 /* Invalid property or property not supported. Restore
3123 the position to what it was before. */
3124 *position = start_pos;
3125 }
3126
3127 return replaces_text_display_p;
3128 }
3129
3130
3131 /* Check if PROP is a display sub-property value whose text should be
3132 treated as intangible. */
3133
3134 static int
3135 single_display_prop_intangible_p (prop)
3136 Lisp_Object prop;
3137 {
3138 /* Skip over `when FORM'. */
3139 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3140 {
3141 prop = XCDR (prop);
3142 if (!CONSP (prop))
3143 return 0;
3144 prop = XCDR (prop);
3145 }
3146
3147 if (!CONSP (prop))
3148 return 0;
3149
3150 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3151 we don't need to treat text as intangible. */
3152 if (EQ (XCAR (prop), Qmargin))
3153 {
3154 prop = XCDR (prop);
3155 if (!CONSP (prop))
3156 return 0;
3157
3158 prop = XCDR (prop);
3159 if (!CONSP (prop)
3160 || EQ (XCAR (prop), Qleft_margin)
3161 || EQ (XCAR (prop), Qright_margin))
3162 return 0;
3163 }
3164
3165 return CONSP (prop) && EQ (XCAR (prop), Qimage);
3166 }
3167
3168
3169 /* Check if PROP is a display property value whose text should be
3170 treated as intangible. */
3171
3172 int
3173 display_prop_intangible_p (prop)
3174 Lisp_Object prop;
3175 {
3176 if (CONSP (prop)
3177 && CONSP (XCAR (prop))
3178 && !EQ (Qmargin, XCAR (XCAR (prop))))
3179 {
3180 /* A list of sub-properties. */
3181 while (CONSP (prop))
3182 {
3183 if (single_display_prop_intangible_p (XCAR (prop)))
3184 return 1;
3185 prop = XCDR (prop);
3186 }
3187 }
3188 else if (VECTORP (prop))
3189 {
3190 /* A vector of sub-properties. */
3191 int i;
3192 for (i = 0; i < ASIZE (prop); ++i)
3193 if (single_display_prop_intangible_p (AREF (prop, i)))
3194 return 1;
3195 }
3196 else
3197 return single_display_prop_intangible_p (prop);
3198
3199 return 0;
3200 }
3201
3202
3203 /* Return 1 if PROP is a display sub-property value containing STRING. */
3204
3205 static int
3206 single_display_prop_string_p (prop, string)
3207 Lisp_Object prop, string;
3208 {
3209 if (EQ (string, prop))
3210 return 1;
3211
3212 /* Skip over `when FORM'. */
3213 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3214 {
3215 prop = XCDR (prop);
3216 if (!CONSP (prop))
3217 return 0;
3218 prop = XCDR (prop);
3219 }
3220
3221 if (CONSP (prop))
3222 /* Skip over `margin LOCATION'. */
3223 if (EQ (XCAR (prop), Qmargin))
3224 {
3225 prop = XCDR (prop);
3226 if (!CONSP (prop))
3227 return 0;
3228
3229 prop = XCDR (prop);
3230 if (!CONSP (prop))
3231 return 0;
3232 }
3233
3234 return CONSP (prop) && EQ (XCAR (prop), string);
3235 }
3236
3237
3238 /* Return 1 if STRING appears in the `display' property PROP. */
3239
3240 static int
3241 display_prop_string_p (prop, string)
3242 Lisp_Object prop, string;
3243 {
3244 if (CONSP (prop)
3245 && CONSP (XCAR (prop))
3246 && !EQ (Qmargin, XCAR (XCAR (prop))))
3247 {
3248 /* A list of sub-properties. */
3249 while (CONSP (prop))
3250 {
3251 if (single_display_prop_string_p (XCAR (prop), string))
3252 return 1;
3253 prop = XCDR (prop);
3254 }
3255 }
3256 else if (VECTORP (prop))
3257 {
3258 /* A vector of sub-properties. */
3259 int i;
3260 for (i = 0; i < ASIZE (prop); ++i)
3261 if (single_display_prop_string_p (AREF (prop, i), string))
3262 return 1;
3263 }
3264 else
3265 return single_display_prop_string_p (prop, string);
3266
3267 return 0;
3268 }
3269
3270
3271 /* Determine from which buffer position in W's buffer STRING comes
3272 from. AROUND_CHARPOS is an approximate position where it could
3273 be from. Value is the buffer position or 0 if it couldn't be
3274 determined.
3275
3276 W's buffer must be current.
3277
3278 This function is necessary because we don't record buffer positions
3279 in glyphs generated from strings (to keep struct glyph small).
3280 This function may only use code that doesn't eval because it is
3281 called asynchronously from note_mouse_highlight. */
3282
3283 int
3284 string_buffer_position (w, string, around_charpos)
3285 struct window *w;
3286 Lisp_Object string;
3287 int around_charpos;
3288 {
3289 Lisp_Object limit, prop, pos;
3290 const int MAX_DISTANCE = 1000;
3291 int found = 0;
3292
3293 pos = make_number (around_charpos);
3294 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
3295 while (!found && !EQ (pos, limit))
3296 {
3297 prop = Fget_char_property (pos, Qdisplay, Qnil);
3298 if (!NILP (prop) && display_prop_string_p (prop, string))
3299 found = 1;
3300 else
3301 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
3302 }
3303
3304 if (!found)
3305 {
3306 pos = make_number (around_charpos);
3307 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
3308 while (!found && !EQ (pos, limit))
3309 {
3310 prop = Fget_char_property (pos, Qdisplay, Qnil);
3311 if (!NILP (prop) && display_prop_string_p (prop, string))
3312 found = 1;
3313 else
3314 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
3315 limit);
3316 }
3317 }
3318
3319 return found ? XINT (pos) : 0;
3320 }
3321
3322
3323 \f
3324 /***********************************************************************
3325 `composition' property
3326 ***********************************************************************/
3327
3328 /* Set up iterator IT from `composition' property at its current
3329 position. Called from handle_stop. */
3330
3331 static enum prop_handled
3332 handle_composition_prop (it)
3333 struct it *it;
3334 {
3335 Lisp_Object prop, string;
3336 int pos, pos_byte, end;
3337 enum prop_handled handled = HANDLED_NORMALLY;
3338
3339 if (STRINGP (it->string))
3340 {
3341 pos = IT_STRING_CHARPOS (*it);
3342 pos_byte = IT_STRING_BYTEPOS (*it);
3343 string = it->string;
3344 }
3345 else
3346 {
3347 pos = IT_CHARPOS (*it);
3348 pos_byte = IT_BYTEPOS (*it);
3349 string = Qnil;
3350 }
3351
3352 /* If there's a valid composition and point is not inside of the
3353 composition (in the case that the composition is from the current
3354 buffer), draw a glyph composed from the composition components. */
3355 if (find_composition (pos, -1, &pos, &end, &prop, string)
3356 && COMPOSITION_VALID_P (pos, end, prop)
3357 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
3358 {
3359 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
3360
3361 if (id >= 0)
3362 {
3363 it->method = next_element_from_composition;
3364 it->cmp_id = id;
3365 it->cmp_len = COMPOSITION_LENGTH (prop);
3366 /* For a terminal, draw only the first character of the
3367 components. */
3368 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
3369 it->len = (STRINGP (it->string)
3370 ? string_char_to_byte (it->string, end)
3371 : CHAR_TO_BYTE (end)) - pos_byte;
3372 it->stop_charpos = end;
3373 handled = HANDLED_RETURN;
3374 }
3375 }
3376
3377 return handled;
3378 }
3379
3380
3381 \f
3382 /***********************************************************************
3383 Overlay strings
3384 ***********************************************************************/
3385
3386 /* The following structure is used to record overlay strings for
3387 later sorting in load_overlay_strings. */
3388
3389 struct overlay_entry
3390 {
3391 Lisp_Object overlay;
3392 Lisp_Object string;
3393 int priority;
3394 int after_string_p;
3395 };
3396
3397
3398 /* Set up iterator IT from overlay strings at its current position.
3399 Called from handle_stop. */
3400
3401 static enum prop_handled
3402 handle_overlay_change (it)
3403 struct it *it;
3404 {
3405 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
3406 return HANDLED_RECOMPUTE_PROPS;
3407 else
3408 return HANDLED_NORMALLY;
3409 }
3410
3411
3412 /* Set up the next overlay string for delivery by IT, if there is an
3413 overlay string to deliver. Called by set_iterator_to_next when the
3414 end of the current overlay string is reached. If there are more
3415 overlay strings to display, IT->string and
3416 IT->current.overlay_string_index are set appropriately here.
3417 Otherwise IT->string is set to nil. */
3418
3419 static void
3420 next_overlay_string (it)
3421 struct it *it;
3422 {
3423 ++it->current.overlay_string_index;
3424 if (it->current.overlay_string_index == it->n_overlay_strings)
3425 {
3426 /* No more overlay strings. Restore IT's settings to what
3427 they were before overlay strings were processed, and
3428 continue to deliver from current_buffer. */
3429 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
3430
3431 pop_it (it);
3432 xassert (it->stop_charpos >= BEGV
3433 && it->stop_charpos <= it->end_charpos);
3434 it->string = Qnil;
3435 it->current.overlay_string_index = -1;
3436 SET_TEXT_POS (it->current.string_pos, -1, -1);
3437 it->n_overlay_strings = 0;
3438 it->method = next_element_from_buffer;
3439
3440 /* If we're at the end of the buffer, record that we have
3441 processed the overlay strings there already, so that
3442 next_element_from_buffer doesn't try it again. */
3443 if (IT_CHARPOS (*it) >= it->end_charpos)
3444 it->overlay_strings_at_end_processed_p = 1;
3445
3446 /* If we have to display `...' for invisible text, set
3447 the iterator up for that. */
3448 if (display_ellipsis_p)
3449 setup_for_ellipsis (it);
3450 }
3451 else
3452 {
3453 /* There are more overlay strings to process. If
3454 IT->current.overlay_string_index has advanced to a position
3455 where we must load IT->overlay_strings with more strings, do
3456 it. */
3457 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
3458
3459 if (it->current.overlay_string_index && i == 0)
3460 load_overlay_strings (it, 0);
3461
3462 /* Initialize IT to deliver display elements from the overlay
3463 string. */
3464 it->string = it->overlay_strings[i];
3465 it->multibyte_p = STRING_MULTIBYTE (it->string);
3466 SET_TEXT_POS (it->current.string_pos, 0, 0);
3467 it->method = next_element_from_string;
3468 it->stop_charpos = 0;
3469 }
3470
3471 CHECK_IT (it);
3472 }
3473
3474
3475 /* Compare two overlay_entry structures E1 and E2. Used as a
3476 comparison function for qsort in load_overlay_strings. Overlay
3477 strings for the same position are sorted so that
3478
3479 1. All after-strings come in front of before-strings, except
3480 when they come from the same overlay.
3481
3482 2. Within after-strings, strings are sorted so that overlay strings
3483 from overlays with higher priorities come first.
3484
3485 2. Within before-strings, strings are sorted so that overlay
3486 strings from overlays with higher priorities come last.
3487
3488 Value is analogous to strcmp. */
3489
3490
3491 static int
3492 compare_overlay_entries (e1, e2)
3493 void *e1, *e2;
3494 {
3495 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
3496 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
3497 int result;
3498
3499 if (entry1->after_string_p != entry2->after_string_p)
3500 {
3501 /* Let after-strings appear in front of before-strings if
3502 they come from different overlays. */
3503 if (EQ (entry1->overlay, entry2->overlay))
3504 result = entry1->after_string_p ? 1 : -1;
3505 else
3506 result = entry1->after_string_p ? -1 : 1;
3507 }
3508 else if (entry1->after_string_p)
3509 /* After-strings sorted in order of decreasing priority. */
3510 result = entry2->priority - entry1->priority;
3511 else
3512 /* Before-strings sorted in order of increasing priority. */
3513 result = entry1->priority - entry2->priority;
3514
3515 return result;
3516 }
3517
3518
3519 /* Load the vector IT->overlay_strings with overlay strings from IT's
3520 current buffer position, or from CHARPOS if that is > 0. Set
3521 IT->n_overlays to the total number of overlay strings found.
3522
3523 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
3524 a time. On entry into load_overlay_strings,
3525 IT->current.overlay_string_index gives the number of overlay
3526 strings that have already been loaded by previous calls to this
3527 function.
3528
3529 IT->add_overlay_start contains an additional overlay start
3530 position to consider for taking overlay strings from, if non-zero.
3531 This position comes into play when the overlay has an `invisible'
3532 property, and both before and after-strings. When we've skipped to
3533 the end of the overlay, because of its `invisible' property, we
3534 nevertheless want its before-string to appear.
3535 IT->add_overlay_start will contain the overlay start position
3536 in this case.
3537
3538 Overlay strings are sorted so that after-string strings come in
3539 front of before-string strings. Within before and after-strings,
3540 strings are sorted by overlay priority. See also function
3541 compare_overlay_entries. */
3542
3543 static void
3544 load_overlay_strings (it, charpos)
3545 struct it *it;
3546 int charpos;
3547 {
3548 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
3549 Lisp_Object ov, overlay, window, str, invisible;
3550 int start, end;
3551 int size = 20;
3552 int n = 0, i, j, invis_p;
3553 struct overlay_entry *entries
3554 = (struct overlay_entry *) alloca (size * sizeof *entries);
3555
3556 if (charpos <= 0)
3557 charpos = IT_CHARPOS (*it);
3558
3559 /* Append the overlay string STRING of overlay OVERLAY to vector
3560 `entries' which has size `size' and currently contains `n'
3561 elements. AFTER_P non-zero means STRING is an after-string of
3562 OVERLAY. */
3563 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
3564 do \
3565 { \
3566 Lisp_Object priority; \
3567 \
3568 if (n == size) \
3569 { \
3570 int new_size = 2 * size; \
3571 struct overlay_entry *old = entries; \
3572 entries = \
3573 (struct overlay_entry *) alloca (new_size \
3574 * sizeof *entries); \
3575 bcopy (old, entries, size * sizeof *entries); \
3576 size = new_size; \
3577 } \
3578 \
3579 entries[n].string = (STRING); \
3580 entries[n].overlay = (OVERLAY); \
3581 priority = Foverlay_get ((OVERLAY), Qpriority); \
3582 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
3583 entries[n].after_string_p = (AFTER_P); \
3584 ++n; \
3585 } \
3586 while (0)
3587
3588 /* Process overlay before the overlay center. */
3589 for (ov = current_buffer->overlays_before; CONSP (ov); ov = XCDR (ov))
3590 {
3591 overlay = XCAR (ov);
3592 xassert (OVERLAYP (overlay));
3593 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3594 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3595
3596 if (end < charpos)
3597 break;
3598
3599 /* Skip this overlay if it doesn't start or end at IT's current
3600 position. */
3601 if (end != charpos && start != charpos)
3602 continue;
3603
3604 /* Skip this overlay if it doesn't apply to IT->w. */
3605 window = Foverlay_get (overlay, Qwindow);
3606 if (WINDOWP (window) && XWINDOW (window) != it->w)
3607 continue;
3608
3609 /* If the text ``under'' the overlay is invisible, both before-
3610 and after-strings from this overlay are visible; start and
3611 end position are indistinguishable. */
3612 invisible = Foverlay_get (overlay, Qinvisible);
3613 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3614
3615 /* If overlay has a non-empty before-string, record it. */
3616 if ((start == charpos || (end == charpos && invis_p))
3617 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3618 && SCHARS (str))
3619 RECORD_OVERLAY_STRING (overlay, str, 0);
3620
3621 /* If overlay has a non-empty after-string, record it. */
3622 if ((end == charpos || (start == charpos && invis_p))
3623 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3624 && SCHARS (str))
3625 RECORD_OVERLAY_STRING (overlay, str, 1);
3626 }
3627
3628 /* Process overlays after the overlay center. */
3629 for (ov = current_buffer->overlays_after; CONSP (ov); ov = XCDR (ov))
3630 {
3631 overlay = XCAR (ov);
3632 xassert (OVERLAYP (overlay));
3633 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3634 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3635
3636 if (start > charpos)
3637 break;
3638
3639 /* Skip this overlay if it doesn't start or end at IT's current
3640 position. */
3641 if (end != charpos && start != charpos)
3642 continue;
3643
3644 /* Skip this overlay if it doesn't apply to IT->w. */
3645 window = Foverlay_get (overlay, Qwindow);
3646 if (WINDOWP (window) && XWINDOW (window) != it->w)
3647 continue;
3648
3649 /* If the text ``under'' the overlay is invisible, it has a zero
3650 dimension, and both before- and after-strings apply. */
3651 invisible = Foverlay_get (overlay, Qinvisible);
3652 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3653
3654 /* If overlay has a non-empty before-string, record it. */
3655 if ((start == charpos || (end == charpos && invis_p))
3656 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3657 && SCHARS (str))
3658 RECORD_OVERLAY_STRING (overlay, str, 0);
3659
3660 /* If overlay has a non-empty after-string, record it. */
3661 if ((end == charpos || (start == charpos && invis_p))
3662 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3663 && SCHARS (str))
3664 RECORD_OVERLAY_STRING (overlay, str, 1);
3665 }
3666
3667 #undef RECORD_OVERLAY_STRING
3668
3669 /* Sort entries. */
3670 if (n > 1)
3671 qsort (entries, n, sizeof *entries, compare_overlay_entries);
3672
3673 /* Record the total number of strings to process. */
3674 it->n_overlay_strings = n;
3675
3676 /* IT->current.overlay_string_index is the number of overlay strings
3677 that have already been consumed by IT. Copy some of the
3678 remaining overlay strings to IT->overlay_strings. */
3679 i = 0;
3680 j = it->current.overlay_string_index;
3681 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
3682 it->overlay_strings[i++] = entries[j++].string;
3683
3684 CHECK_IT (it);
3685 }
3686
3687
3688 /* Get the first chunk of overlay strings at IT's current buffer
3689 position, or at CHARPOS if that is > 0. Value is non-zero if at
3690 least one overlay string was found. */
3691
3692 static int
3693 get_overlay_strings (it, charpos)
3694 struct it *it;
3695 int charpos;
3696 {
3697 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
3698 process. This fills IT->overlay_strings with strings, and sets
3699 IT->n_overlay_strings to the total number of strings to process.
3700 IT->pos.overlay_string_index has to be set temporarily to zero
3701 because load_overlay_strings needs this; it must be set to -1
3702 when no overlay strings are found because a zero value would
3703 indicate a position in the first overlay string. */
3704 it->current.overlay_string_index = 0;
3705 load_overlay_strings (it, charpos);
3706
3707 /* If we found overlay strings, set up IT to deliver display
3708 elements from the first one. Otherwise set up IT to deliver
3709 from current_buffer. */
3710 if (it->n_overlay_strings)
3711 {
3712 /* Make sure we know settings in current_buffer, so that we can
3713 restore meaningful values when we're done with the overlay
3714 strings. */
3715 compute_stop_pos (it);
3716 xassert (it->face_id >= 0);
3717
3718 /* Save IT's settings. They are restored after all overlay
3719 strings have been processed. */
3720 xassert (it->sp == 0);
3721 push_it (it);
3722
3723 /* Set up IT to deliver display elements from the first overlay
3724 string. */
3725 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3726 it->string = it->overlay_strings[0];
3727 it->stop_charpos = 0;
3728 xassert (STRINGP (it->string));
3729 it->end_charpos = SCHARS (it->string);
3730 it->multibyte_p = STRING_MULTIBYTE (it->string);
3731 it->method = next_element_from_string;
3732 }
3733 else
3734 {
3735 it->string = Qnil;
3736 it->current.overlay_string_index = -1;
3737 it->method = next_element_from_buffer;
3738 }
3739
3740 CHECK_IT (it);
3741
3742 /* Value is non-zero if we found at least one overlay string. */
3743 return STRINGP (it->string);
3744 }
3745
3746
3747 \f
3748 /***********************************************************************
3749 Saving and restoring state
3750 ***********************************************************************/
3751
3752 /* Save current settings of IT on IT->stack. Called, for example,
3753 before setting up IT for an overlay string, to be able to restore
3754 IT's settings to what they were after the overlay string has been
3755 processed. */
3756
3757 static void
3758 push_it (it)
3759 struct it *it;
3760 {
3761 struct iterator_stack_entry *p;
3762
3763 xassert (it->sp < 2);
3764 p = it->stack + it->sp;
3765
3766 p->stop_charpos = it->stop_charpos;
3767 xassert (it->face_id >= 0);
3768 p->face_id = it->face_id;
3769 p->string = it->string;
3770 p->pos = it->current;
3771 p->end_charpos = it->end_charpos;
3772 p->string_nchars = it->string_nchars;
3773 p->area = it->area;
3774 p->multibyte_p = it->multibyte_p;
3775 p->space_width = it->space_width;
3776 p->font_height = it->font_height;
3777 p->voffset = it->voffset;
3778 p->string_from_display_prop_p = it->string_from_display_prop_p;
3779 p->display_ellipsis_p = 0;
3780 ++it->sp;
3781 }
3782
3783
3784 /* Restore IT's settings from IT->stack. Called, for example, when no
3785 more overlay strings must be processed, and we return to delivering
3786 display elements from a buffer, or when the end of a string from a
3787 `display' property is reached and we return to delivering display
3788 elements from an overlay string, or from a buffer. */
3789
3790 static void
3791 pop_it (it)
3792 struct it *it;
3793 {
3794 struct iterator_stack_entry *p;
3795
3796 xassert (it->sp > 0);
3797 --it->sp;
3798 p = it->stack + it->sp;
3799 it->stop_charpos = p->stop_charpos;
3800 it->face_id = p->face_id;
3801 it->string = p->string;
3802 it->current = p->pos;
3803 it->end_charpos = p->end_charpos;
3804 it->string_nchars = p->string_nchars;
3805 it->area = p->area;
3806 it->multibyte_p = p->multibyte_p;
3807 it->space_width = p->space_width;
3808 it->font_height = p->font_height;
3809 it->voffset = p->voffset;
3810 it->string_from_display_prop_p = p->string_from_display_prop_p;
3811 }
3812
3813
3814 \f
3815 /***********************************************************************
3816 Moving over lines
3817 ***********************************************************************/
3818
3819 /* Set IT's current position to the previous line start. */
3820
3821 static void
3822 back_to_previous_line_start (it)
3823 struct it *it;
3824 {
3825 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
3826 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3827 }
3828
3829
3830 /* Move IT to the next line start.
3831
3832 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
3833 we skipped over part of the text (as opposed to moving the iterator
3834 continuously over the text). Otherwise, don't change the value
3835 of *SKIPPED_P.
3836
3837 Newlines may come from buffer text, overlay strings, or strings
3838 displayed via the `display' property. That's the reason we can't
3839 simply use find_next_newline_no_quit.
3840
3841 Note that this function may not skip over invisible text that is so
3842 because of text properties and immediately follows a newline. If
3843 it would, function reseat_at_next_visible_line_start, when called
3844 from set_iterator_to_next, would effectively make invisible
3845 characters following a newline part of the wrong glyph row, which
3846 leads to wrong cursor motion. */
3847
3848 static int
3849 forward_to_next_line_start (it, skipped_p)
3850 struct it *it;
3851 int *skipped_p;
3852 {
3853 int old_selective, newline_found_p, n;
3854 const int MAX_NEWLINE_DISTANCE = 500;
3855
3856 /* If already on a newline, just consume it to avoid unintended
3857 skipping over invisible text below. */
3858 if (it->what == IT_CHARACTER
3859 && it->c == '\n'
3860 && CHARPOS (it->position) == IT_CHARPOS (*it))
3861 {
3862 set_iterator_to_next (it, 0);
3863 it->c = 0;
3864 return 1;
3865 }
3866
3867 /* Don't handle selective display in the following. It's (a)
3868 unnecessary because it's done by the caller, and (b) leads to an
3869 infinite recursion because next_element_from_ellipsis indirectly
3870 calls this function. */
3871 old_selective = it->selective;
3872 it->selective = 0;
3873
3874 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
3875 from buffer text. */
3876 for (n = newline_found_p = 0;
3877 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
3878 n += STRINGP (it->string) ? 0 : 1)
3879 {
3880 if (!get_next_display_element (it))
3881 return 0;
3882 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
3883 set_iterator_to_next (it, 0);
3884 }
3885
3886 /* If we didn't find a newline near enough, see if we can use a
3887 short-cut. */
3888 if (!newline_found_p)
3889 {
3890 int start = IT_CHARPOS (*it);
3891 int limit = find_next_newline_no_quit (start, 1);
3892 Lisp_Object pos;
3893
3894 xassert (!STRINGP (it->string));
3895
3896 /* If there isn't any `display' property in sight, and no
3897 overlays, we can just use the position of the newline in
3898 buffer text. */
3899 if (it->stop_charpos >= limit
3900 || ((pos = Fnext_single_property_change (make_number (start),
3901 Qdisplay,
3902 Qnil, make_number (limit)),
3903 NILP (pos))
3904 && next_overlay_change (start) == ZV))
3905 {
3906 IT_CHARPOS (*it) = limit;
3907 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
3908 *skipped_p = newline_found_p = 1;
3909 }
3910 else
3911 {
3912 while (get_next_display_element (it)
3913 && !newline_found_p)
3914 {
3915 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
3916 set_iterator_to_next (it, 0);
3917 }
3918 }
3919 }
3920
3921 it->selective = old_selective;
3922 return newline_found_p;
3923 }
3924
3925
3926 /* Set IT's current position to the previous visible line start. Skip
3927 invisible text that is so either due to text properties or due to
3928 selective display. Caution: this does not change IT->current_x and
3929 IT->hpos. */
3930
3931 static void
3932 back_to_previous_visible_line_start (it)
3933 struct it *it;
3934 {
3935 int visible_p = 0;
3936
3937 /* Go back one newline if not on BEGV already. */
3938 if (IT_CHARPOS (*it) > BEGV)
3939 back_to_previous_line_start (it);
3940
3941 /* Move over lines that are invisible because of selective display
3942 or text properties. */
3943 while (IT_CHARPOS (*it) > BEGV
3944 && !visible_p)
3945 {
3946 visible_p = 1;
3947
3948 /* If selective > 0, then lines indented more than that values
3949 are invisible. */
3950 if (it->selective > 0
3951 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3952 (double) it->selective)) /* iftc */
3953 visible_p = 0;
3954 else
3955 {
3956 Lisp_Object prop;
3957
3958 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
3959 Qinvisible, it->window);
3960 if (TEXT_PROP_MEANS_INVISIBLE (prop))
3961 visible_p = 0;
3962 }
3963
3964 /* Back one more newline if the current one is invisible. */
3965 if (!visible_p)
3966 back_to_previous_line_start (it);
3967 }
3968
3969 xassert (IT_CHARPOS (*it) >= BEGV);
3970 xassert (IT_CHARPOS (*it) == BEGV
3971 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3972 CHECK_IT (it);
3973 }
3974
3975
3976 /* Reseat iterator IT at the previous visible line start. Skip
3977 invisible text that is so either due to text properties or due to
3978 selective display. At the end, update IT's overlay information,
3979 face information etc. */
3980
3981 static void
3982 reseat_at_previous_visible_line_start (it)
3983 struct it *it;
3984 {
3985 back_to_previous_visible_line_start (it);
3986 reseat (it, it->current.pos, 1);
3987 CHECK_IT (it);
3988 }
3989
3990
3991 /* Reseat iterator IT on the next visible line start in the current
3992 buffer. ON_NEWLINE_P non-zero means position IT on the newline
3993 preceding the line start. Skip over invisible text that is so
3994 because of selective display. Compute faces, overlays etc at the
3995 new position. Note that this function does not skip over text that
3996 is invisible because of text properties. */
3997
3998 static void
3999 reseat_at_next_visible_line_start (it, on_newline_p)
4000 struct it *it;
4001 int on_newline_p;
4002 {
4003 int newline_found_p, skipped_p = 0;
4004
4005 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4006
4007 /* Skip over lines that are invisible because they are indented
4008 more than the value of IT->selective. */
4009 if (it->selective > 0)
4010 while (IT_CHARPOS (*it) < ZV
4011 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4012 (double) it->selective)) /* iftc */
4013 {
4014 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4015 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4016 }
4017
4018 /* Position on the newline if that's what's requested. */
4019 if (on_newline_p && newline_found_p)
4020 {
4021 if (STRINGP (it->string))
4022 {
4023 if (IT_STRING_CHARPOS (*it) > 0)
4024 {
4025 --IT_STRING_CHARPOS (*it);
4026 --IT_STRING_BYTEPOS (*it);
4027 }
4028 }
4029 else if (IT_CHARPOS (*it) > BEGV)
4030 {
4031 --IT_CHARPOS (*it);
4032 --IT_BYTEPOS (*it);
4033 reseat (it, it->current.pos, 0);
4034 }
4035 }
4036 else if (skipped_p)
4037 reseat (it, it->current.pos, 0);
4038
4039 CHECK_IT (it);
4040 }
4041
4042
4043 \f
4044 /***********************************************************************
4045 Changing an iterator's position
4046 ***********************************************************************/
4047
4048 /* Change IT's current position to POS in current_buffer. If FORCE_P
4049 is non-zero, always check for text properties at the new position.
4050 Otherwise, text properties are only looked up if POS >=
4051 IT->check_charpos of a property. */
4052
4053 static void
4054 reseat (it, pos, force_p)
4055 struct it *it;
4056 struct text_pos pos;
4057 int force_p;
4058 {
4059 int original_pos = IT_CHARPOS (*it);
4060
4061 reseat_1 (it, pos, 0);
4062
4063 /* Determine where to check text properties. Avoid doing it
4064 where possible because text property lookup is very expensive. */
4065 if (force_p
4066 || CHARPOS (pos) > it->stop_charpos
4067 || CHARPOS (pos) < original_pos)
4068 handle_stop (it);
4069
4070 CHECK_IT (it);
4071 }
4072
4073
4074 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4075 IT->stop_pos to POS, also. */
4076
4077 static void
4078 reseat_1 (it, pos, set_stop_p)
4079 struct it *it;
4080 struct text_pos pos;
4081 int set_stop_p;
4082 {
4083 /* Don't call this function when scanning a C string. */
4084 xassert (it->s == NULL);
4085
4086 /* POS must be a reasonable value. */
4087 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4088
4089 it->current.pos = it->position = pos;
4090 XSETBUFFER (it->object, current_buffer);
4091 it->end_charpos = ZV;
4092 it->dpvec = NULL;
4093 it->current.dpvec_index = -1;
4094 it->current.overlay_string_index = -1;
4095 IT_STRING_CHARPOS (*it) = -1;
4096 IT_STRING_BYTEPOS (*it) = -1;
4097 it->string = Qnil;
4098 it->method = next_element_from_buffer;
4099 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
4100 it->sp = 0;
4101 it->face_before_selective_p = 0;
4102
4103 if (set_stop_p)
4104 it->stop_charpos = CHARPOS (pos);
4105 }
4106
4107
4108 /* Set up IT for displaying a string, starting at CHARPOS in window W.
4109 If S is non-null, it is a C string to iterate over. Otherwise,
4110 STRING gives a Lisp string to iterate over.
4111
4112 If PRECISION > 0, don't return more then PRECISION number of
4113 characters from the string.
4114
4115 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4116 characters have been returned. FIELD_WIDTH < 0 means an infinite
4117 field width.
4118
4119 MULTIBYTE = 0 means disable processing of multibyte characters,
4120 MULTIBYTE > 0 means enable it,
4121 MULTIBYTE < 0 means use IT->multibyte_p.
4122
4123 IT must be initialized via a prior call to init_iterator before
4124 calling this function. */
4125
4126 static void
4127 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4128 struct it *it;
4129 unsigned char *s;
4130 Lisp_Object string;
4131 int charpos;
4132 int precision, field_width, multibyte;
4133 {
4134 /* No region in strings. */
4135 it->region_beg_charpos = it->region_end_charpos = -1;
4136
4137 /* No text property checks performed by default, but see below. */
4138 it->stop_charpos = -1;
4139
4140 /* Set iterator position and end position. */
4141 bzero (&it->current, sizeof it->current);
4142 it->current.overlay_string_index = -1;
4143 it->current.dpvec_index = -1;
4144 xassert (charpos >= 0);
4145
4146 /* If STRING is specified, use its multibyteness, otherwise use the
4147 setting of MULTIBYTE, if specified. */
4148 if (multibyte >= 0)
4149 it->multibyte_p = multibyte > 0;
4150
4151 if (s == NULL)
4152 {
4153 xassert (STRINGP (string));
4154 it->string = string;
4155 it->s = NULL;
4156 it->end_charpos = it->string_nchars = SCHARS (string);
4157 it->method = next_element_from_string;
4158 it->current.string_pos = string_pos (charpos, string);
4159 }
4160 else
4161 {
4162 it->s = s;
4163 it->string = Qnil;
4164
4165 /* Note that we use IT->current.pos, not it->current.string_pos,
4166 for displaying C strings. */
4167 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4168 if (it->multibyte_p)
4169 {
4170 it->current.pos = c_string_pos (charpos, s, 1);
4171 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4172 }
4173 else
4174 {
4175 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4176 it->end_charpos = it->string_nchars = strlen (s);
4177 }
4178
4179 it->method = next_element_from_c_string;
4180 }
4181
4182 /* PRECISION > 0 means don't return more than PRECISION characters
4183 from the string. */
4184 if (precision > 0 && it->end_charpos - charpos > precision)
4185 it->end_charpos = it->string_nchars = charpos + precision;
4186
4187 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4188 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4189 FIELD_WIDTH < 0 means infinite field width. This is useful for
4190 padding with `-' at the end of a mode line. */
4191 if (field_width < 0)
4192 field_width = INFINITY;
4193 if (field_width > it->end_charpos - charpos)
4194 it->end_charpos = charpos + field_width;
4195
4196 /* Use the standard display table for displaying strings. */
4197 if (DISP_TABLE_P (Vstandard_display_table))
4198 it->dp = XCHAR_TABLE (Vstandard_display_table);
4199
4200 it->stop_charpos = charpos;
4201 CHECK_IT (it);
4202 }
4203
4204
4205 \f
4206 /***********************************************************************
4207 Iteration
4208 ***********************************************************************/
4209
4210 /* Load IT's display element fields with information about the next
4211 display element from the current position of IT. Value is zero if
4212 end of buffer (or C string) is reached. */
4213
4214 int
4215 get_next_display_element (it)
4216 struct it *it;
4217 {
4218 /* Non-zero means that we found a display element. Zero means that
4219 we hit the end of what we iterate over. Performance note: the
4220 function pointer `method' used here turns out to be faster than
4221 using a sequence of if-statements. */
4222 int success_p = (*it->method) (it);
4223
4224 if (it->what == IT_CHARACTER)
4225 {
4226 /* Map via display table or translate control characters.
4227 IT->c, IT->len etc. have been set to the next character by
4228 the function call above. If we have a display table, and it
4229 contains an entry for IT->c, translate it. Don't do this if
4230 IT->c itself comes from a display table, otherwise we could
4231 end up in an infinite recursion. (An alternative could be to
4232 count the recursion depth of this function and signal an
4233 error when a certain maximum depth is reached.) Is it worth
4234 it? */
4235 if (success_p && it->dpvec == NULL)
4236 {
4237 Lisp_Object dv;
4238
4239 if (it->dp
4240 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
4241 VECTORP (dv)))
4242 {
4243 struct Lisp_Vector *v = XVECTOR (dv);
4244
4245 /* Return the first character from the display table
4246 entry, if not empty. If empty, don't display the
4247 current character. */
4248 if (v->size)
4249 {
4250 it->dpvec_char_len = it->len;
4251 it->dpvec = v->contents;
4252 it->dpend = v->contents + v->size;
4253 it->current.dpvec_index = 0;
4254 it->method = next_element_from_display_vector;
4255 success_p = get_next_display_element (it);
4256 }
4257 else
4258 {
4259 set_iterator_to_next (it, 0);
4260 success_p = get_next_display_element (it);
4261 }
4262 }
4263
4264 /* Translate control characters into `\003' or `^C' form.
4265 Control characters coming from a display table entry are
4266 currently not translated because we use IT->dpvec to hold
4267 the translation. This could easily be changed but I
4268 don't believe that it is worth doing.
4269
4270 If it->multibyte_p is nonzero, eight-bit characters and
4271 non-printable multibyte characters are also translated to
4272 octal form.
4273
4274 If it->multibyte_p is zero, eight-bit characters that
4275 don't have corresponding multibyte char code are also
4276 translated to octal form. */
4277 else if ((it->c < ' '
4278 && (it->area != TEXT_AREA
4279 || (it->c != '\n' && it->c != '\t')))
4280 || (it->multibyte_p
4281 ? ((it->c >= 127
4282 && it->len == 1)
4283 || !CHAR_PRINTABLE_P (it->c))
4284 : (it->c >= 127
4285 && it->c == unibyte_char_to_multibyte (it->c))))
4286 {
4287 /* IT->c is a control character which must be displayed
4288 either as '\003' or as `^C' where the '\\' and '^'
4289 can be defined in the display table. Fill
4290 IT->ctl_chars with glyphs for what we have to
4291 display. Then, set IT->dpvec to these glyphs. */
4292 GLYPH g;
4293
4294 if (it->c < 128 && it->ctl_arrow_p)
4295 {
4296 /* Set IT->ctl_chars[0] to the glyph for `^'. */
4297 if (it->dp
4298 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
4299 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
4300 g = XINT (DISP_CTRL_GLYPH (it->dp));
4301 else
4302 g = FAST_MAKE_GLYPH ('^', 0);
4303 XSETINT (it->ctl_chars[0], g);
4304
4305 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
4306 XSETINT (it->ctl_chars[1], g);
4307
4308 /* Set up IT->dpvec and return first character from it. */
4309 it->dpvec_char_len = it->len;
4310 it->dpvec = it->ctl_chars;
4311 it->dpend = it->dpvec + 2;
4312 it->current.dpvec_index = 0;
4313 it->method = next_element_from_display_vector;
4314 get_next_display_element (it);
4315 }
4316 else
4317 {
4318 unsigned char str[MAX_MULTIBYTE_LENGTH];
4319 int len;
4320 int i;
4321 GLYPH escape_glyph;
4322
4323 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
4324 if (it->dp
4325 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
4326 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
4327 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
4328 else
4329 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
4330
4331 if (SINGLE_BYTE_CHAR_P (it->c))
4332 str[0] = it->c, len = 1;
4333 else
4334 {
4335 len = CHAR_STRING_NO_SIGNAL (it->c, str);
4336 if (len < 0)
4337 {
4338 /* It's an invalid character, which
4339 shouldn't happen actually, but due to
4340 bugs it may happen. Let's print the char
4341 as is, there's not much meaningful we can
4342 do with it. */
4343 str[0] = it->c;
4344 str[1] = it->c >> 8;
4345 str[2] = it->c >> 16;
4346 str[3] = it->c >> 24;
4347 len = 4;
4348 }
4349 }
4350
4351 for (i = 0; i < len; i++)
4352 {
4353 XSETINT (it->ctl_chars[i * 4], escape_glyph);
4354 /* Insert three more glyphs into IT->ctl_chars for
4355 the octal display of the character. */
4356 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
4357 XSETINT (it->ctl_chars[i * 4 + 1], g);
4358 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
4359 XSETINT (it->ctl_chars[i * 4 + 2], g);
4360 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
4361 XSETINT (it->ctl_chars[i * 4 + 3], g);
4362 }
4363
4364 /* Set up IT->dpvec and return the first character
4365 from it. */
4366 it->dpvec_char_len = it->len;
4367 it->dpvec = it->ctl_chars;
4368 it->dpend = it->dpvec + len * 4;
4369 it->current.dpvec_index = 0;
4370 it->method = next_element_from_display_vector;
4371 get_next_display_element (it);
4372 }
4373 }
4374 }
4375
4376 /* Adjust face id for a multibyte character. There are no
4377 multibyte character in unibyte text. */
4378 if (it->multibyte_p
4379 && success_p
4380 && FRAME_WINDOW_P (it->f))
4381 {
4382 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4383 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
4384 }
4385 }
4386
4387 /* Is this character the last one of a run of characters with
4388 box? If yes, set IT->end_of_box_run_p to 1. */
4389 if (it->face_box_p
4390 && it->s == NULL)
4391 {
4392 int face_id;
4393 struct face *face;
4394
4395 it->end_of_box_run_p
4396 = ((face_id = face_after_it_pos (it),
4397 face_id != it->face_id)
4398 && (face = FACE_FROM_ID (it->f, face_id),
4399 face->box == FACE_NO_BOX));
4400 }
4401
4402 /* Value is 0 if end of buffer or string reached. */
4403 return success_p;
4404 }
4405
4406
4407 /* Move IT to the next display element.
4408
4409 RESEAT_P non-zero means if called on a newline in buffer text,
4410 skip to the next visible line start.
4411
4412 Functions get_next_display_element and set_iterator_to_next are
4413 separate because I find this arrangement easier to handle than a
4414 get_next_display_element function that also increments IT's
4415 position. The way it is we can first look at an iterator's current
4416 display element, decide whether it fits on a line, and if it does,
4417 increment the iterator position. The other way around we probably
4418 would either need a flag indicating whether the iterator has to be
4419 incremented the next time, or we would have to implement a
4420 decrement position function which would not be easy to write. */
4421
4422 void
4423 set_iterator_to_next (it, reseat_p)
4424 struct it *it;
4425 int reseat_p;
4426 {
4427 /* Reset flags indicating start and end of a sequence of characters
4428 with box. Reset them at the start of this function because
4429 moving the iterator to a new position might set them. */
4430 it->start_of_box_run_p = it->end_of_box_run_p = 0;
4431
4432 if (it->method == next_element_from_buffer)
4433 {
4434 /* The current display element of IT is a character from
4435 current_buffer. Advance in the buffer, and maybe skip over
4436 invisible lines that are so because of selective display. */
4437 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
4438 reseat_at_next_visible_line_start (it, 0);
4439 else
4440 {
4441 xassert (it->len != 0);
4442 IT_BYTEPOS (*it) += it->len;
4443 IT_CHARPOS (*it) += 1;
4444 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
4445 }
4446 }
4447 else if (it->method == next_element_from_composition)
4448 {
4449 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
4450 if (STRINGP (it->string))
4451 {
4452 IT_STRING_BYTEPOS (*it) += it->len;
4453 IT_STRING_CHARPOS (*it) += it->cmp_len;
4454 it->method = next_element_from_string;
4455 goto consider_string_end;
4456 }
4457 else
4458 {
4459 IT_BYTEPOS (*it) += it->len;
4460 IT_CHARPOS (*it) += it->cmp_len;
4461 it->method = next_element_from_buffer;
4462 }
4463 }
4464 else if (it->method == next_element_from_c_string)
4465 {
4466 /* Current display element of IT is from a C string. */
4467 IT_BYTEPOS (*it) += it->len;
4468 IT_CHARPOS (*it) += 1;
4469 }
4470 else if (it->method == next_element_from_display_vector)
4471 {
4472 /* Current display element of IT is from a display table entry.
4473 Advance in the display table definition. Reset it to null if
4474 end reached, and continue with characters from buffers/
4475 strings. */
4476 ++it->current.dpvec_index;
4477
4478 /* Restore face of the iterator to what they were before the
4479 display vector entry (these entries may contain faces). */
4480 it->face_id = it->saved_face_id;
4481
4482 if (it->dpvec + it->current.dpvec_index == it->dpend)
4483 {
4484 if (it->s)
4485 it->method = next_element_from_c_string;
4486 else if (STRINGP (it->string))
4487 it->method = next_element_from_string;
4488 else
4489 it->method = next_element_from_buffer;
4490
4491 it->dpvec = NULL;
4492 it->current.dpvec_index = -1;
4493
4494 /* Skip over characters which were displayed via IT->dpvec. */
4495 if (it->dpvec_char_len < 0)
4496 reseat_at_next_visible_line_start (it, 1);
4497 else if (it->dpvec_char_len > 0)
4498 {
4499 it->len = it->dpvec_char_len;
4500 set_iterator_to_next (it, reseat_p);
4501 }
4502 }
4503 }
4504 else if (it->method == next_element_from_string)
4505 {
4506 /* Current display element is a character from a Lisp string. */
4507 xassert (it->s == NULL && STRINGP (it->string));
4508 IT_STRING_BYTEPOS (*it) += it->len;
4509 IT_STRING_CHARPOS (*it) += 1;
4510
4511 consider_string_end:
4512
4513 if (it->current.overlay_string_index >= 0)
4514 {
4515 /* IT->string is an overlay string. Advance to the
4516 next, if there is one. */
4517 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
4518 next_overlay_string (it);
4519 }
4520 else
4521 {
4522 /* IT->string is not an overlay string. If we reached
4523 its end, and there is something on IT->stack, proceed
4524 with what is on the stack. This can be either another
4525 string, this time an overlay string, or a buffer. */
4526 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
4527 && it->sp > 0)
4528 {
4529 pop_it (it);
4530 if (!STRINGP (it->string))
4531 it->method = next_element_from_buffer;
4532 else
4533 goto consider_string_end;
4534 }
4535 }
4536 }
4537 else if (it->method == next_element_from_image
4538 || it->method == next_element_from_stretch)
4539 {
4540 /* The position etc with which we have to proceed are on
4541 the stack. The position may be at the end of a string,
4542 if the `display' property takes up the whole string. */
4543 pop_it (it);
4544 it->image_id = 0;
4545 if (STRINGP (it->string))
4546 {
4547 it->method = next_element_from_string;
4548 goto consider_string_end;
4549 }
4550 else
4551 it->method = next_element_from_buffer;
4552 }
4553 else
4554 /* There are no other methods defined, so this should be a bug. */
4555 abort ();
4556
4557 xassert (it->method != next_element_from_string
4558 || (STRINGP (it->string)
4559 && IT_STRING_CHARPOS (*it) >= 0));
4560 }
4561
4562
4563 /* Load IT's display element fields with information about the next
4564 display element which comes from a display table entry or from the
4565 result of translating a control character to one of the forms `^C'
4566 or `\003'. IT->dpvec holds the glyphs to return as characters. */
4567
4568 static int
4569 next_element_from_display_vector (it)
4570 struct it *it;
4571 {
4572 /* Precondition. */
4573 xassert (it->dpvec && it->current.dpvec_index >= 0);
4574
4575 /* Remember the current face id in case glyphs specify faces.
4576 IT's face is restored in set_iterator_to_next. */
4577 it->saved_face_id = it->face_id;
4578
4579 if (INTEGERP (*it->dpvec)
4580 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
4581 {
4582 int lface_id;
4583 GLYPH g;
4584
4585 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
4586 it->c = FAST_GLYPH_CHAR (g);
4587 it->len = CHAR_BYTES (it->c);
4588
4589 /* The entry may contain a face id to use. Such a face id is
4590 the id of a Lisp face, not a realized face. A face id of
4591 zero means no face is specified. */
4592 lface_id = FAST_GLYPH_FACE (g);
4593 if (lface_id)
4594 {
4595 /* The function returns -1 if lface_id is invalid. */
4596 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
4597 if (face_id >= 0)
4598 it->face_id = face_id;
4599 }
4600 }
4601 else
4602 /* Display table entry is invalid. Return a space. */
4603 it->c = ' ', it->len = 1;
4604
4605 /* Don't change position and object of the iterator here. They are
4606 still the values of the character that had this display table
4607 entry or was translated, and that's what we want. */
4608 it->what = IT_CHARACTER;
4609 return 1;
4610 }
4611
4612
4613 /* Load IT with the next display element from Lisp string IT->string.
4614 IT->current.string_pos is the current position within the string.
4615 If IT->current.overlay_string_index >= 0, the Lisp string is an
4616 overlay string. */
4617
4618 static int
4619 next_element_from_string (it)
4620 struct it *it;
4621 {
4622 struct text_pos position;
4623
4624 xassert (STRINGP (it->string));
4625 xassert (IT_STRING_CHARPOS (*it) >= 0);
4626 position = it->current.string_pos;
4627
4628 /* Time to check for invisible text? */
4629 if (IT_STRING_CHARPOS (*it) < it->end_charpos
4630 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
4631 {
4632 handle_stop (it);
4633
4634 /* Since a handler may have changed IT->method, we must
4635 recurse here. */
4636 return get_next_display_element (it);
4637 }
4638
4639 if (it->current.overlay_string_index >= 0)
4640 {
4641 /* Get the next character from an overlay string. In overlay
4642 strings, There is no field width or padding with spaces to
4643 do. */
4644 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
4645 {
4646 it->what = IT_EOB;
4647 return 0;
4648 }
4649 else if (STRING_MULTIBYTE (it->string))
4650 {
4651 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
4652 const unsigned char *s = (SDATA (it->string)
4653 + IT_STRING_BYTEPOS (*it));
4654 it->c = string_char_and_length (s, remaining, &it->len);
4655 }
4656 else
4657 {
4658 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
4659 it->len = 1;
4660 }
4661 }
4662 else
4663 {
4664 /* Get the next character from a Lisp string that is not an
4665 overlay string. Such strings come from the mode line, for
4666 example. We may have to pad with spaces, or truncate the
4667 string. See also next_element_from_c_string. */
4668 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
4669 {
4670 it->what = IT_EOB;
4671 return 0;
4672 }
4673 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
4674 {
4675 /* Pad with spaces. */
4676 it->c = ' ', it->len = 1;
4677 CHARPOS (position) = BYTEPOS (position) = -1;
4678 }
4679 else if (STRING_MULTIBYTE (it->string))
4680 {
4681 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
4682 const unsigned char *s = (SDATA (it->string)
4683 + IT_STRING_BYTEPOS (*it));
4684 it->c = string_char_and_length (s, maxlen, &it->len);
4685 }
4686 else
4687 {
4688 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
4689 it->len = 1;
4690 }
4691 }
4692
4693 /* Record what we have and where it came from. Note that we store a
4694 buffer position in IT->position although it could arguably be a
4695 string position. */
4696 it->what = IT_CHARACTER;
4697 it->object = it->string;
4698 it->position = position;
4699 return 1;
4700 }
4701
4702
4703 /* Load IT with next display element from C string IT->s.
4704 IT->string_nchars is the maximum number of characters to return
4705 from the string. IT->end_charpos may be greater than
4706 IT->string_nchars when this function is called, in which case we
4707 may have to return padding spaces. Value is zero if end of string
4708 reached, including padding spaces. */
4709
4710 static int
4711 next_element_from_c_string (it)
4712 struct it *it;
4713 {
4714 int success_p = 1;
4715
4716 xassert (it->s);
4717 it->what = IT_CHARACTER;
4718 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
4719 it->object = Qnil;
4720
4721 /* IT's position can be greater IT->string_nchars in case a field
4722 width or precision has been specified when the iterator was
4723 initialized. */
4724 if (IT_CHARPOS (*it) >= it->end_charpos)
4725 {
4726 /* End of the game. */
4727 it->what = IT_EOB;
4728 success_p = 0;
4729 }
4730 else if (IT_CHARPOS (*it) >= it->string_nchars)
4731 {
4732 /* Pad with spaces. */
4733 it->c = ' ', it->len = 1;
4734 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
4735 }
4736 else if (it->multibyte_p)
4737 {
4738 /* Implementation note: The calls to strlen apparently aren't a
4739 performance problem because there is no noticeable performance
4740 difference between Emacs running in unibyte or multibyte mode. */
4741 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
4742 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
4743 maxlen, &it->len);
4744 }
4745 else
4746 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
4747
4748 return success_p;
4749 }
4750
4751
4752 /* Set up IT to return characters from an ellipsis, if appropriate.
4753 The definition of the ellipsis glyphs may come from a display table
4754 entry. This function Fills IT with the first glyph from the
4755 ellipsis if an ellipsis is to be displayed. */
4756
4757 static int
4758 next_element_from_ellipsis (it)
4759 struct it *it;
4760 {
4761 if (it->selective_display_ellipsis_p)
4762 {
4763 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4764 {
4765 /* Use the display table definition for `...'. Invalid glyphs
4766 will be handled by the method returning elements from dpvec. */
4767 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4768 it->dpvec_char_len = it->len;
4769 it->dpvec = v->contents;
4770 it->dpend = v->contents + v->size;
4771 it->current.dpvec_index = 0;
4772 it->method = next_element_from_display_vector;
4773 }
4774 else
4775 {
4776 /* Use default `...' which is stored in default_invis_vector. */
4777 it->dpvec_char_len = it->len;
4778 it->dpvec = default_invis_vector;
4779 it->dpend = default_invis_vector + 3;
4780 it->current.dpvec_index = 0;
4781 it->method = next_element_from_display_vector;
4782 }
4783 }
4784 else
4785 {
4786 /* The face at the current position may be different from the
4787 face we find after the invisible text. Remember what it
4788 was in IT->saved_face_id, and signal that it's there by
4789 setting face_before_selective_p. */
4790 it->saved_face_id = it->face_id;
4791 it->method = next_element_from_buffer;
4792 reseat_at_next_visible_line_start (it, 1);
4793 it->face_before_selective_p = 1;
4794 }
4795
4796 return get_next_display_element (it);
4797 }
4798
4799
4800 /* Deliver an image display element. The iterator IT is already
4801 filled with image information (done in handle_display_prop). Value
4802 is always 1. */
4803
4804
4805 static int
4806 next_element_from_image (it)
4807 struct it *it;
4808 {
4809 it->what = IT_IMAGE;
4810 return 1;
4811 }
4812
4813
4814 /* Fill iterator IT with next display element from a stretch glyph
4815 property. IT->object is the value of the text property. Value is
4816 always 1. */
4817
4818 static int
4819 next_element_from_stretch (it)
4820 struct it *it;
4821 {
4822 it->what = IT_STRETCH;
4823 return 1;
4824 }
4825
4826
4827 /* Load IT with the next display element from current_buffer. Value
4828 is zero if end of buffer reached. IT->stop_charpos is the next
4829 position at which to stop and check for text properties or buffer
4830 end. */
4831
4832 static int
4833 next_element_from_buffer (it)
4834 struct it *it;
4835 {
4836 int success_p = 1;
4837
4838 /* Check this assumption, otherwise, we would never enter the
4839 if-statement, below. */
4840 xassert (IT_CHARPOS (*it) >= BEGV
4841 && IT_CHARPOS (*it) <= it->stop_charpos);
4842
4843 if (IT_CHARPOS (*it) >= it->stop_charpos)
4844 {
4845 if (IT_CHARPOS (*it) >= it->end_charpos)
4846 {
4847 int overlay_strings_follow_p;
4848
4849 /* End of the game, except when overlay strings follow that
4850 haven't been returned yet. */
4851 if (it->overlay_strings_at_end_processed_p)
4852 overlay_strings_follow_p = 0;
4853 else
4854 {
4855 it->overlay_strings_at_end_processed_p = 1;
4856 overlay_strings_follow_p = get_overlay_strings (it, 0);
4857 }
4858
4859 if (overlay_strings_follow_p)
4860 success_p = get_next_display_element (it);
4861 else
4862 {
4863 it->what = IT_EOB;
4864 it->position = it->current.pos;
4865 success_p = 0;
4866 }
4867 }
4868 else
4869 {
4870 handle_stop (it);
4871 return get_next_display_element (it);
4872 }
4873 }
4874 else
4875 {
4876 /* No face changes, overlays etc. in sight, so just return a
4877 character from current_buffer. */
4878 unsigned char *p;
4879
4880 /* Maybe run the redisplay end trigger hook. Performance note:
4881 This doesn't seem to cost measurable time. */
4882 if (it->redisplay_end_trigger_charpos
4883 && it->glyph_row
4884 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
4885 run_redisplay_end_trigger_hook (it);
4886
4887 /* Get the next character, maybe multibyte. */
4888 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
4889 if (it->multibyte_p && !ASCII_BYTE_P (*p))
4890 {
4891 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
4892 - IT_BYTEPOS (*it));
4893 it->c = string_char_and_length (p, maxlen, &it->len);
4894 }
4895 else
4896 it->c = *p, it->len = 1;
4897
4898 /* Record what we have and where it came from. */
4899 it->what = IT_CHARACTER;;
4900 it->object = it->w->buffer;
4901 it->position = it->current.pos;
4902
4903 /* Normally we return the character found above, except when we
4904 really want to return an ellipsis for selective display. */
4905 if (it->selective)
4906 {
4907 if (it->c == '\n')
4908 {
4909 /* A value of selective > 0 means hide lines indented more
4910 than that number of columns. */
4911 if (it->selective > 0
4912 && IT_CHARPOS (*it) + 1 < ZV
4913 && indented_beyond_p (IT_CHARPOS (*it) + 1,
4914 IT_BYTEPOS (*it) + 1,
4915 (double) it->selective)) /* iftc */
4916 {
4917 success_p = next_element_from_ellipsis (it);
4918 it->dpvec_char_len = -1;
4919 }
4920 }
4921 else if (it->c == '\r' && it->selective == -1)
4922 {
4923 /* A value of selective == -1 means that everything from the
4924 CR to the end of the line is invisible, with maybe an
4925 ellipsis displayed for it. */
4926 success_p = next_element_from_ellipsis (it);
4927 it->dpvec_char_len = -1;
4928 }
4929 }
4930 }
4931
4932 /* Value is zero if end of buffer reached. */
4933 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
4934 return success_p;
4935 }
4936
4937
4938 /* Run the redisplay end trigger hook for IT. */
4939
4940 static void
4941 run_redisplay_end_trigger_hook (it)
4942 struct it *it;
4943 {
4944 Lisp_Object args[3];
4945
4946 /* IT->glyph_row should be non-null, i.e. we should be actually
4947 displaying something, or otherwise we should not run the hook. */
4948 xassert (it->glyph_row);
4949
4950 /* Set up hook arguments. */
4951 args[0] = Qredisplay_end_trigger_functions;
4952 args[1] = it->window;
4953 XSETINT (args[2], it->redisplay_end_trigger_charpos);
4954 it->redisplay_end_trigger_charpos = 0;
4955
4956 /* Since we are *trying* to run these functions, don't try to run
4957 them again, even if they get an error. */
4958 it->w->redisplay_end_trigger = Qnil;
4959 Frun_hook_with_args (3, args);
4960
4961 /* Notice if it changed the face of the character we are on. */
4962 handle_face_prop (it);
4963 }
4964
4965
4966 /* Deliver a composition display element. The iterator IT is already
4967 filled with composition information (done in
4968 handle_composition_prop). Value is always 1. */
4969
4970 static int
4971 next_element_from_composition (it)
4972 struct it *it;
4973 {
4974 it->what = IT_COMPOSITION;
4975 it->position = (STRINGP (it->string)
4976 ? it->current.string_pos
4977 : it->current.pos);
4978 return 1;
4979 }
4980
4981
4982 \f
4983 /***********************************************************************
4984 Moving an iterator without producing glyphs
4985 ***********************************************************************/
4986
4987 /* Move iterator IT to a specified buffer or X position within one
4988 line on the display without producing glyphs.
4989
4990 OP should be a bit mask including some or all of these bits:
4991 MOVE_TO_X: Stop on reaching x-position TO_X.
4992 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
4993 Regardless of OP's value, stop in reaching the end of the display line.
4994
4995 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
4996 This means, in particular, that TO_X includes window's horizontal
4997 scroll amount.
4998
4999 The return value has several possible values that
5000 say what condition caused the scan to stop:
5001
5002 MOVE_POS_MATCH_OR_ZV
5003 - when TO_POS or ZV was reached.
5004
5005 MOVE_X_REACHED
5006 -when TO_X was reached before TO_POS or ZV were reached.
5007
5008 MOVE_LINE_CONTINUED
5009 - when we reached the end of the display area and the line must
5010 be continued.
5011
5012 MOVE_LINE_TRUNCATED
5013 - when we reached the end of the display area and the line is
5014 truncated.
5015
5016 MOVE_NEWLINE_OR_CR
5017 - when we stopped at a line end, i.e. a newline or a CR and selective
5018 display is on. */
5019
5020 static enum move_it_result
5021 move_it_in_display_line_to (it, to_charpos, to_x, op)
5022 struct it *it;
5023 int to_charpos, to_x, op;
5024 {
5025 enum move_it_result result = MOVE_UNDEFINED;
5026 struct glyph_row *saved_glyph_row;
5027
5028 /* Don't produce glyphs in produce_glyphs. */
5029 saved_glyph_row = it->glyph_row;
5030 it->glyph_row = NULL;
5031
5032 while (1)
5033 {
5034 int x, i, ascent = 0, descent = 0;
5035
5036 /* Stop when ZV or TO_CHARPOS reached. */
5037 if (!get_next_display_element (it)
5038 || ((op & MOVE_TO_POS) != 0
5039 && BUFFERP (it->object)
5040 && IT_CHARPOS (*it) >= to_charpos))
5041 {
5042 result = MOVE_POS_MATCH_OR_ZV;
5043 break;
5044 }
5045
5046 /* The call to produce_glyphs will get the metrics of the
5047 display element IT is loaded with. We record in x the
5048 x-position before this display element in case it does not
5049 fit on the line. */
5050 x = it->current_x;
5051
5052 /* Remember the line height so far in case the next element doesn't
5053 fit on the line. */
5054 if (!it->truncate_lines_p)
5055 {
5056 ascent = it->max_ascent;
5057 descent = it->max_descent;
5058 }
5059
5060 PRODUCE_GLYPHS (it);
5061
5062 if (it->area != TEXT_AREA)
5063 {
5064 set_iterator_to_next (it, 1);
5065 continue;
5066 }
5067
5068 /* The number of glyphs we get back in IT->nglyphs will normally
5069 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
5070 character on a terminal frame, or (iii) a line end. For the
5071 second case, IT->nglyphs - 1 padding glyphs will be present
5072 (on X frames, there is only one glyph produced for a
5073 composite character.
5074
5075 The behavior implemented below means, for continuation lines,
5076 that as many spaces of a TAB as fit on the current line are
5077 displayed there. For terminal frames, as many glyphs of a
5078 multi-glyph character are displayed in the current line, too.
5079 This is what the old redisplay code did, and we keep it that
5080 way. Under X, the whole shape of a complex character must
5081 fit on the line or it will be completely displayed in the
5082 next line.
5083
5084 Note that both for tabs and padding glyphs, all glyphs have
5085 the same width. */
5086 if (it->nglyphs)
5087 {
5088 /* More than one glyph or glyph doesn't fit on line. All
5089 glyphs have the same width. */
5090 int single_glyph_width = it->pixel_width / it->nglyphs;
5091 int new_x;
5092
5093 for (i = 0; i < it->nglyphs; ++i, x = new_x)
5094 {
5095 new_x = x + single_glyph_width;
5096
5097 /* We want to leave anything reaching TO_X to the caller. */
5098 if ((op & MOVE_TO_X) && new_x > to_x)
5099 {
5100 it->current_x = x;
5101 result = MOVE_X_REACHED;
5102 break;
5103 }
5104 else if (/* Lines are continued. */
5105 !it->truncate_lines_p
5106 && (/* And glyph doesn't fit on the line. */
5107 new_x > it->last_visible_x
5108 /* Or it fits exactly and we're on a window
5109 system frame. */
5110 || (new_x == it->last_visible_x
5111 && FRAME_WINDOW_P (it->f))))
5112 {
5113 if (/* IT->hpos == 0 means the very first glyph
5114 doesn't fit on the line, e.g. a wide image. */
5115 it->hpos == 0
5116 || (new_x == it->last_visible_x
5117 && FRAME_WINDOW_P (it->f)))
5118 {
5119 ++it->hpos;
5120 it->current_x = new_x;
5121 if (i == it->nglyphs - 1)
5122 set_iterator_to_next (it, 1);
5123 }
5124 else
5125 {
5126 it->current_x = x;
5127 it->max_ascent = ascent;
5128 it->max_descent = descent;
5129 }
5130
5131 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
5132 IT_CHARPOS (*it)));
5133 result = MOVE_LINE_CONTINUED;
5134 break;
5135 }
5136 else if (new_x > it->first_visible_x)
5137 {
5138 /* Glyph is visible. Increment number of glyphs that
5139 would be displayed. */
5140 ++it->hpos;
5141 }
5142 else
5143 {
5144 /* Glyph is completely off the left margin of the display
5145 area. Nothing to do. */
5146 }
5147 }
5148
5149 if (result != MOVE_UNDEFINED)
5150 break;
5151 }
5152 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
5153 {
5154 /* Stop when TO_X specified and reached. This check is
5155 necessary here because of lines consisting of a line end,
5156 only. The line end will not produce any glyphs and we
5157 would never get MOVE_X_REACHED. */
5158 xassert (it->nglyphs == 0);
5159 result = MOVE_X_REACHED;
5160 break;
5161 }
5162
5163 /* Is this a line end? If yes, we're done. */
5164 if (ITERATOR_AT_END_OF_LINE_P (it))
5165 {
5166 result = MOVE_NEWLINE_OR_CR;
5167 break;
5168 }
5169
5170 /* The current display element has been consumed. Advance
5171 to the next. */
5172 set_iterator_to_next (it, 1);
5173
5174 /* Stop if lines are truncated and IT's current x-position is
5175 past the right edge of the window now. */
5176 if (it->truncate_lines_p
5177 && it->current_x >= it->last_visible_x)
5178 {
5179 result = MOVE_LINE_TRUNCATED;
5180 break;
5181 }
5182 }
5183
5184 /* Restore the iterator settings altered at the beginning of this
5185 function. */
5186 it->glyph_row = saved_glyph_row;
5187 return result;
5188 }
5189
5190
5191 /* Move IT forward until it satisfies one or more of the criteria in
5192 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
5193
5194 OP is a bit-mask that specifies where to stop, and in particular,
5195 which of those four position arguments makes a difference. See the
5196 description of enum move_operation_enum.
5197
5198 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
5199 screen line, this function will set IT to the next position >
5200 TO_CHARPOS. */
5201
5202 void
5203 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
5204 struct it *it;
5205 int to_charpos, to_x, to_y, to_vpos;
5206 int op;
5207 {
5208 enum move_it_result skip, skip2 = MOVE_X_REACHED;
5209 int line_height;
5210 int reached = 0;
5211
5212 for (;;)
5213 {
5214 if (op & MOVE_TO_VPOS)
5215 {
5216 /* If no TO_CHARPOS and no TO_X specified, stop at the
5217 start of the line TO_VPOS. */
5218 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
5219 {
5220 if (it->vpos == to_vpos)
5221 {
5222 reached = 1;
5223 break;
5224 }
5225 else
5226 skip = move_it_in_display_line_to (it, -1, -1, 0);
5227 }
5228 else
5229 {
5230 /* TO_VPOS >= 0 means stop at TO_X in the line at
5231 TO_VPOS, or at TO_POS, whichever comes first. */
5232 if (it->vpos == to_vpos)
5233 {
5234 reached = 2;
5235 break;
5236 }
5237
5238 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
5239
5240 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
5241 {
5242 reached = 3;
5243 break;
5244 }
5245 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
5246 {
5247 /* We have reached TO_X but not in the line we want. */
5248 skip = move_it_in_display_line_to (it, to_charpos,
5249 -1, MOVE_TO_POS);
5250 if (skip == MOVE_POS_MATCH_OR_ZV)
5251 {
5252 reached = 4;
5253 break;
5254 }
5255 }
5256 }
5257 }
5258 else if (op & MOVE_TO_Y)
5259 {
5260 struct it it_backup;
5261
5262 /* TO_Y specified means stop at TO_X in the line containing
5263 TO_Y---or at TO_CHARPOS if this is reached first. The
5264 problem is that we can't really tell whether the line
5265 contains TO_Y before we have completely scanned it, and
5266 this may skip past TO_X. What we do is to first scan to
5267 TO_X.
5268
5269 If TO_X is not specified, use a TO_X of zero. The reason
5270 is to make the outcome of this function more predictable.
5271 If we didn't use TO_X == 0, we would stop at the end of
5272 the line which is probably not what a caller would expect
5273 to happen. */
5274 skip = move_it_in_display_line_to (it, to_charpos,
5275 ((op & MOVE_TO_X)
5276 ? to_x : 0),
5277 (MOVE_TO_X
5278 | (op & MOVE_TO_POS)));
5279
5280 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
5281 if (skip == MOVE_POS_MATCH_OR_ZV)
5282 {
5283 reached = 5;
5284 break;
5285 }
5286
5287 /* If TO_X was reached, we would like to know whether TO_Y
5288 is in the line. This can only be said if we know the
5289 total line height which requires us to scan the rest of
5290 the line. */
5291 if (skip == MOVE_X_REACHED)
5292 {
5293 it_backup = *it;
5294 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
5295 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
5296 op & MOVE_TO_POS);
5297 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
5298 }
5299
5300 /* Now, decide whether TO_Y is in this line. */
5301 line_height = it->max_ascent + it->max_descent;
5302 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
5303
5304 if (to_y >= it->current_y
5305 && to_y < it->current_y + line_height)
5306 {
5307 if (skip == MOVE_X_REACHED)
5308 /* If TO_Y is in this line and TO_X was reached above,
5309 we scanned too far. We have to restore IT's settings
5310 to the ones before skipping. */
5311 *it = it_backup;
5312 reached = 6;
5313 }
5314 else if (skip == MOVE_X_REACHED)
5315 {
5316 skip = skip2;
5317 if (skip == MOVE_POS_MATCH_OR_ZV)
5318 reached = 7;
5319 }
5320
5321 if (reached)
5322 break;
5323 }
5324 else
5325 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
5326
5327 switch (skip)
5328 {
5329 case MOVE_POS_MATCH_OR_ZV:
5330 reached = 8;
5331 goto out;
5332
5333 case MOVE_NEWLINE_OR_CR:
5334 set_iterator_to_next (it, 1);
5335 it->continuation_lines_width = 0;
5336 break;
5337
5338 case MOVE_LINE_TRUNCATED:
5339 it->continuation_lines_width = 0;
5340 reseat_at_next_visible_line_start (it, 0);
5341 if ((op & MOVE_TO_POS) != 0
5342 && IT_CHARPOS (*it) > to_charpos)
5343 {
5344 reached = 9;
5345 goto out;
5346 }
5347 break;
5348
5349 case MOVE_LINE_CONTINUED:
5350 it->continuation_lines_width += it->current_x;
5351 break;
5352
5353 default:
5354 abort ();
5355 }
5356
5357 /* Reset/increment for the next run. */
5358 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
5359 it->current_x = it->hpos = 0;
5360 it->current_y += it->max_ascent + it->max_descent;
5361 ++it->vpos;
5362 last_height = it->max_ascent + it->max_descent;
5363 last_max_ascent = it->max_ascent;
5364 it->max_ascent = it->max_descent = 0;
5365 }
5366
5367 out:
5368
5369 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
5370 }
5371
5372
5373 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
5374
5375 If DY > 0, move IT backward at least that many pixels. DY = 0
5376 means move IT backward to the preceding line start or BEGV. This
5377 function may move over more than DY pixels if IT->current_y - DY
5378 ends up in the middle of a line; in this case IT->current_y will be
5379 set to the top of the line moved to. */
5380
5381 void
5382 move_it_vertically_backward (it, dy)
5383 struct it *it;
5384 int dy;
5385 {
5386 int nlines, h;
5387 struct it it2, it3;
5388 int start_pos = IT_CHARPOS (*it);
5389
5390 xassert (dy >= 0);
5391
5392 /* Estimate how many newlines we must move back. */
5393 nlines = max (1, dy / CANON_Y_UNIT (it->f));
5394
5395 /* Set the iterator's position that many lines back. */
5396 while (nlines-- && IT_CHARPOS (*it) > BEGV)
5397 back_to_previous_visible_line_start (it);
5398
5399 /* Reseat the iterator here. When moving backward, we don't want
5400 reseat to skip forward over invisible text, set up the iterator
5401 to deliver from overlay strings at the new position etc. So,
5402 use reseat_1 here. */
5403 reseat_1 (it, it->current.pos, 1);
5404
5405 /* We are now surely at a line start. */
5406 it->current_x = it->hpos = 0;
5407
5408 /* Move forward and see what y-distance we moved. First move to the
5409 start of the next line so that we get its height. We need this
5410 height to be able to tell whether we reached the specified
5411 y-distance. */
5412 it2 = *it;
5413 it2.max_ascent = it2.max_descent = 0;
5414 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
5415 MOVE_TO_POS | MOVE_TO_VPOS);
5416 xassert (IT_CHARPOS (*it) >= BEGV);
5417 it3 = it2;
5418
5419 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
5420 xassert (IT_CHARPOS (*it) >= BEGV);
5421 h = it2.current_y - it->current_y;
5422 nlines = it2.vpos - it->vpos;
5423
5424 /* Correct IT's y and vpos position. */
5425 it->vpos -= nlines;
5426 it->current_y -= h;
5427
5428 if (dy == 0)
5429 {
5430 /* DY == 0 means move to the start of the screen line. The
5431 value of nlines is > 0 if continuation lines were involved. */
5432 if (nlines > 0)
5433 move_it_by_lines (it, nlines, 1);
5434 xassert (IT_CHARPOS (*it) <= start_pos);
5435 }
5436 else if (nlines)
5437 {
5438 /* The y-position we try to reach. Note that h has been
5439 subtracted in front of the if-statement. */
5440 int target_y = it->current_y + h - dy;
5441 int y0 = it3.current_y;
5442 int y1 = line_bottom_y (&it3);
5443 int line_height = y1 - y0;
5444
5445 /* If we did not reach target_y, try to move further backward if
5446 we can. If we moved too far backward, try to move forward. */
5447 if (target_y < it->current_y
5448 /* This is heuristic. In a window that's 3 lines high, with
5449 a line height of 13 pixels each, recentering with point
5450 on the bottom line will try to move -39/2 = 19 pixels
5451 backward. Try to avoid moving into the first line. */
5452 && it->current_y - target_y > line_height / 3 * 2
5453 && IT_CHARPOS (*it) > BEGV)
5454 {
5455 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
5456 target_y - it->current_y));
5457 move_it_vertically (it, target_y - it->current_y);
5458 xassert (IT_CHARPOS (*it) >= BEGV);
5459 }
5460 else if (target_y >= it->current_y + line_height
5461 && IT_CHARPOS (*it) < ZV)
5462 {
5463 /* Should move forward by at least one line, maybe more.
5464
5465 Note: Calling move_it_by_lines can be expensive on
5466 terminal frames, where compute_motion is used (via
5467 vmotion) to do the job, when there are very long lines
5468 and truncate-lines is nil. That's the reason for
5469 treating terminal frames specially here. */
5470
5471 if (!FRAME_WINDOW_P (it->f))
5472 move_it_vertically (it, target_y - (it->current_y + line_height));
5473 else
5474 {
5475 do
5476 {
5477 move_it_by_lines (it, 1, 1);
5478 }
5479 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
5480 }
5481
5482 xassert (IT_CHARPOS (*it) >= BEGV);
5483 }
5484 }
5485 }
5486
5487
5488 /* Move IT by a specified amount of pixel lines DY. DY negative means
5489 move backwards. DY = 0 means move to start of screen line. At the
5490 end, IT will be on the start of a screen line. */
5491
5492 void
5493 move_it_vertically (it, dy)
5494 struct it *it;
5495 int dy;
5496 {
5497 if (dy <= 0)
5498 move_it_vertically_backward (it, -dy);
5499 else if (dy > 0)
5500 {
5501 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
5502 move_it_to (it, ZV, -1, it->current_y + dy, -1,
5503 MOVE_TO_POS | MOVE_TO_Y);
5504 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
5505
5506 /* If buffer ends in ZV without a newline, move to the start of
5507 the line to satisfy the post-condition. */
5508 if (IT_CHARPOS (*it) == ZV
5509 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
5510 move_it_by_lines (it, 0, 0);
5511 }
5512 }
5513
5514
5515 /* Move iterator IT past the end of the text line it is in. */
5516
5517 void
5518 move_it_past_eol (it)
5519 struct it *it;
5520 {
5521 enum move_it_result rc;
5522
5523 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
5524 if (rc == MOVE_NEWLINE_OR_CR)
5525 set_iterator_to_next (it, 0);
5526 }
5527
5528
5529 #if 0 /* Currently not used. */
5530
5531 /* Return non-zero if some text between buffer positions START_CHARPOS
5532 and END_CHARPOS is invisible. IT->window is the window for text
5533 property lookup. */
5534
5535 static int
5536 invisible_text_between_p (it, start_charpos, end_charpos)
5537 struct it *it;
5538 int start_charpos, end_charpos;
5539 {
5540 Lisp_Object prop, limit;
5541 int invisible_found_p;
5542
5543 xassert (it != NULL && start_charpos <= end_charpos);
5544
5545 /* Is text at START invisible? */
5546 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
5547 it->window);
5548 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5549 invisible_found_p = 1;
5550 else
5551 {
5552 limit = Fnext_single_char_property_change (make_number (start_charpos),
5553 Qinvisible, Qnil,
5554 make_number (end_charpos));
5555 invisible_found_p = XFASTINT (limit) < end_charpos;
5556 }
5557
5558 return invisible_found_p;
5559 }
5560
5561 #endif /* 0 */
5562
5563
5564 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
5565 negative means move up. DVPOS == 0 means move to the start of the
5566 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
5567 NEED_Y_P is zero, IT->current_y will be left unchanged.
5568
5569 Further optimization ideas: If we would know that IT->f doesn't use
5570 a face with proportional font, we could be faster for
5571 truncate-lines nil. */
5572
5573 void
5574 move_it_by_lines (it, dvpos, need_y_p)
5575 struct it *it;
5576 int dvpos, need_y_p;
5577 {
5578 struct position pos;
5579
5580 if (!FRAME_WINDOW_P (it->f))
5581 {
5582 struct text_pos textpos;
5583
5584 /* We can use vmotion on frames without proportional fonts. */
5585 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
5586 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
5587 reseat (it, textpos, 1);
5588 it->vpos += pos.vpos;
5589 it->current_y += pos.vpos;
5590 }
5591 else if (dvpos == 0)
5592 {
5593 /* DVPOS == 0 means move to the start of the screen line. */
5594 move_it_vertically_backward (it, 0);
5595 xassert (it->current_x == 0 && it->hpos == 0);
5596 }
5597 else if (dvpos > 0)
5598 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
5599 else
5600 {
5601 struct it it2;
5602 int start_charpos, i;
5603
5604 /* Start at the beginning of the screen line containing IT's
5605 position. */
5606 move_it_vertically_backward (it, 0);
5607
5608 /* Go back -DVPOS visible lines and reseat the iterator there. */
5609 start_charpos = IT_CHARPOS (*it);
5610 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
5611 back_to_previous_visible_line_start (it);
5612 reseat (it, it->current.pos, 1);
5613 it->current_x = it->hpos = 0;
5614
5615 /* Above call may have moved too far if continuation lines
5616 are involved. Scan forward and see if it did. */
5617 it2 = *it;
5618 it2.vpos = it2.current_y = 0;
5619 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
5620 it->vpos -= it2.vpos;
5621 it->current_y -= it2.current_y;
5622 it->current_x = it->hpos = 0;
5623
5624 /* If we moved too far, move IT some lines forward. */
5625 if (it2.vpos > -dvpos)
5626 {
5627 int delta = it2.vpos + dvpos;
5628 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
5629 }
5630 }
5631 }
5632
5633
5634 \f
5635 /***********************************************************************
5636 Messages
5637 ***********************************************************************/
5638
5639
5640 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
5641 to *Messages*. */
5642
5643 void
5644 add_to_log (format, arg1, arg2)
5645 char *format;
5646 Lisp_Object arg1, arg2;
5647 {
5648 Lisp_Object args[3];
5649 Lisp_Object msg, fmt;
5650 char *buffer;
5651 int len;
5652 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5653
5654 /* Do nothing if called asynchronously. Inserting text into
5655 a buffer may call after-change-functions and alike and
5656 that would means running Lisp asynchronously. */
5657 if (handling_signal)
5658 return;
5659
5660 fmt = msg = Qnil;
5661 GCPRO4 (fmt, msg, arg1, arg2);
5662
5663 args[0] = fmt = build_string (format);
5664 args[1] = arg1;
5665 args[2] = arg2;
5666 msg = Fformat (3, args);
5667
5668 len = SBYTES (msg) + 1;
5669 buffer = (char *) alloca (len);
5670 bcopy (SDATA (msg), buffer, len);
5671
5672 message_dolog (buffer, len - 1, 1, 0);
5673 UNGCPRO;
5674 }
5675
5676
5677 /* Output a newline in the *Messages* buffer if "needs" one. */
5678
5679 void
5680 message_log_maybe_newline ()
5681 {
5682 if (message_log_need_newline)
5683 message_dolog ("", 0, 1, 0);
5684 }
5685
5686
5687 /* Add a string M of length NBYTES to the message log, optionally
5688 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
5689 nonzero, means interpret the contents of M as multibyte. This
5690 function calls low-level routines in order to bypass text property
5691 hooks, etc. which might not be safe to run. */
5692
5693 void
5694 message_dolog (m, nbytes, nlflag, multibyte)
5695 const char *m;
5696 int nbytes, nlflag, multibyte;
5697 {
5698 if (!NILP (Vmemory_full))
5699 return;
5700
5701 if (!NILP (Vmessage_log_max))
5702 {
5703 struct buffer *oldbuf;
5704 Lisp_Object oldpoint, oldbegv, oldzv;
5705 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5706 int point_at_end = 0;
5707 int zv_at_end = 0;
5708 Lisp_Object old_deactivate_mark, tem;
5709 struct gcpro gcpro1;
5710
5711 old_deactivate_mark = Vdeactivate_mark;
5712 oldbuf = current_buffer;
5713 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
5714 current_buffer->undo_list = Qt;
5715
5716 oldpoint = message_dolog_marker1;
5717 set_marker_restricted (oldpoint, make_number (PT), Qnil);
5718 oldbegv = message_dolog_marker2;
5719 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
5720 oldzv = message_dolog_marker3;
5721 set_marker_restricted (oldzv, make_number (ZV), Qnil);
5722 GCPRO1 (old_deactivate_mark);
5723
5724 if (PT == Z)
5725 point_at_end = 1;
5726 if (ZV == Z)
5727 zv_at_end = 1;
5728
5729 BEGV = BEG;
5730 BEGV_BYTE = BEG_BYTE;
5731 ZV = Z;
5732 ZV_BYTE = Z_BYTE;
5733 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5734
5735 /* Insert the string--maybe converting multibyte to single byte
5736 or vice versa, so that all the text fits the buffer. */
5737 if (multibyte
5738 && NILP (current_buffer->enable_multibyte_characters))
5739 {
5740 int i, c, char_bytes;
5741 unsigned char work[1];
5742
5743 /* Convert a multibyte string to single-byte
5744 for the *Message* buffer. */
5745 for (i = 0; i < nbytes; i += nbytes)
5746 {
5747 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
5748 work[0] = (SINGLE_BYTE_CHAR_P (c)
5749 ? c
5750 : multibyte_char_to_unibyte (c, Qnil));
5751 insert_1_both (work, 1, 1, 1, 0, 0);
5752 }
5753 }
5754 else if (! multibyte
5755 && ! NILP (current_buffer->enable_multibyte_characters))
5756 {
5757 int i, c, char_bytes;
5758 unsigned char *msg = (unsigned char *) m;
5759 unsigned char str[MAX_MULTIBYTE_LENGTH];
5760 /* Convert a single-byte string to multibyte
5761 for the *Message* buffer. */
5762 for (i = 0; i < nbytes; i++)
5763 {
5764 c = unibyte_char_to_multibyte (msg[i]);
5765 char_bytes = CHAR_STRING (c, str);
5766 insert_1_both (str, 1, char_bytes, 1, 0, 0);
5767 }
5768 }
5769 else if (nbytes)
5770 insert_1 (m, nbytes, 1, 0, 0);
5771
5772 if (nlflag)
5773 {
5774 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
5775 insert_1 ("\n", 1, 1, 0, 0);
5776
5777 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
5778 this_bol = PT;
5779 this_bol_byte = PT_BYTE;
5780
5781 /* See if this line duplicates the previous one.
5782 If so, combine duplicates. */
5783 if (this_bol > BEG)
5784 {
5785 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
5786 prev_bol = PT;
5787 prev_bol_byte = PT_BYTE;
5788
5789 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
5790 this_bol, this_bol_byte);
5791 if (dup)
5792 {
5793 del_range_both (prev_bol, prev_bol_byte,
5794 this_bol, this_bol_byte, 0);
5795 if (dup > 1)
5796 {
5797 char dupstr[40];
5798 int duplen;
5799
5800 /* If you change this format, don't forget to also
5801 change message_log_check_duplicate. */
5802 sprintf (dupstr, " [%d times]", dup);
5803 duplen = strlen (dupstr);
5804 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
5805 insert_1 (dupstr, duplen, 1, 0, 1);
5806 }
5807 }
5808 }
5809
5810 /* If we have more than the desired maximum number of lines
5811 in the *Messages* buffer now, delete the oldest ones.
5812 This is safe because we don't have undo in this buffer. */
5813
5814 if (NATNUMP (Vmessage_log_max))
5815 {
5816 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
5817 -XFASTINT (Vmessage_log_max) - 1, 0);
5818 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
5819 }
5820 }
5821 BEGV = XMARKER (oldbegv)->charpos;
5822 BEGV_BYTE = marker_byte_position (oldbegv);
5823
5824 if (zv_at_end)
5825 {
5826 ZV = Z;
5827 ZV_BYTE = Z_BYTE;
5828 }
5829 else
5830 {
5831 ZV = XMARKER (oldzv)->charpos;
5832 ZV_BYTE = marker_byte_position (oldzv);
5833 }
5834
5835 if (point_at_end)
5836 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5837 else
5838 /* We can't do Fgoto_char (oldpoint) because it will run some
5839 Lisp code. */
5840 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
5841 XMARKER (oldpoint)->bytepos);
5842
5843 UNGCPRO;
5844 unchain_marker (oldpoint);
5845 unchain_marker (oldbegv);
5846 unchain_marker (oldzv);
5847
5848 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
5849 set_buffer_internal (oldbuf);
5850 if (NILP (tem))
5851 windows_or_buffers_changed = old_windows_or_buffers_changed;
5852 message_log_need_newline = !nlflag;
5853 Vdeactivate_mark = old_deactivate_mark;
5854 }
5855 }
5856
5857
5858 /* We are at the end of the buffer after just having inserted a newline.
5859 (Note: We depend on the fact we won't be crossing the gap.)
5860 Check to see if the most recent message looks a lot like the previous one.
5861 Return 0 if different, 1 if the new one should just replace it, or a
5862 value N > 1 if we should also append " [N times]". */
5863
5864 static int
5865 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
5866 int prev_bol, this_bol;
5867 int prev_bol_byte, this_bol_byte;
5868 {
5869 int i;
5870 int len = Z_BYTE - 1 - this_bol_byte;
5871 int seen_dots = 0;
5872 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
5873 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
5874
5875 for (i = 0; i < len; i++)
5876 {
5877 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
5878 seen_dots = 1;
5879 if (p1[i] != p2[i])
5880 return seen_dots;
5881 }
5882 p1 += len;
5883 if (*p1 == '\n')
5884 return 2;
5885 if (*p1++ == ' ' && *p1++ == '[')
5886 {
5887 int n = 0;
5888 while (*p1 >= '0' && *p1 <= '9')
5889 n = n * 10 + *p1++ - '0';
5890 if (strncmp (p1, " times]\n", 8) == 0)
5891 return n+1;
5892 }
5893 return 0;
5894 }
5895
5896
5897 /* Display an echo area message M with a specified length of NBYTES
5898 bytes. The string may include null characters. If M is 0, clear
5899 out any existing message, and let the mini-buffer text show
5900 through.
5901
5902 The buffer M must continue to exist until after the echo area gets
5903 cleared or some other message gets displayed there. This means do
5904 not pass text that is stored in a Lisp string; do not pass text in
5905 a buffer that was alloca'd. */
5906
5907 void
5908 message2 (m, nbytes, multibyte)
5909 const char *m;
5910 int nbytes;
5911 int multibyte;
5912 {
5913 /* First flush out any partial line written with print. */
5914 message_log_maybe_newline ();
5915 if (m)
5916 message_dolog (m, nbytes, 1, multibyte);
5917 message2_nolog (m, nbytes, multibyte);
5918 }
5919
5920
5921 /* The non-logging counterpart of message2. */
5922
5923 void
5924 message2_nolog (m, nbytes, multibyte)
5925 const char *m;
5926 int nbytes;
5927 {
5928 struct frame *sf = SELECTED_FRAME ();
5929 message_enable_multibyte = multibyte;
5930
5931 if (noninteractive)
5932 {
5933 if (noninteractive_need_newline)
5934 putc ('\n', stderr);
5935 noninteractive_need_newline = 0;
5936 if (m)
5937 fwrite (m, nbytes, 1, stderr);
5938 if (cursor_in_echo_area == 0)
5939 fprintf (stderr, "\n");
5940 fflush (stderr);
5941 }
5942 /* A null message buffer means that the frame hasn't really been
5943 initialized yet. Error messages get reported properly by
5944 cmd_error, so this must be just an informative message; toss it. */
5945 else if (INTERACTIVE
5946 && sf->glyphs_initialized_p
5947 && FRAME_MESSAGE_BUF (sf))
5948 {
5949 Lisp_Object mini_window;
5950 struct frame *f;
5951
5952 /* Get the frame containing the mini-buffer
5953 that the selected frame is using. */
5954 mini_window = FRAME_MINIBUF_WINDOW (sf);
5955 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5956
5957 FRAME_SAMPLE_VISIBILITY (f);
5958 if (FRAME_VISIBLE_P (sf)
5959 && ! FRAME_VISIBLE_P (f))
5960 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
5961
5962 if (m)
5963 {
5964 set_message (m, Qnil, nbytes, multibyte);
5965 if (minibuffer_auto_raise)
5966 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5967 }
5968 else
5969 clear_message (1, 1);
5970
5971 do_pending_window_change (0);
5972 echo_area_display (1);
5973 do_pending_window_change (0);
5974 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5975 (*frame_up_to_date_hook) (f);
5976 }
5977 }
5978
5979
5980 /* Display an echo area message M with a specified length of NBYTES
5981 bytes. The string may include null characters. If M is not a
5982 string, clear out any existing message, and let the mini-buffer
5983 text show through. */
5984
5985 void
5986 message3 (m, nbytes, multibyte)
5987 Lisp_Object m;
5988 int nbytes;
5989 int multibyte;
5990 {
5991 struct gcpro gcpro1;
5992
5993 GCPRO1 (m);
5994
5995 /* First flush out any partial line written with print. */
5996 message_log_maybe_newline ();
5997 if (STRINGP (m))
5998 message_dolog (SDATA (m), nbytes, 1, multibyte);
5999 message3_nolog (m, nbytes, multibyte);
6000
6001 UNGCPRO;
6002 }
6003
6004
6005 /* The non-logging version of message3. */
6006
6007 void
6008 message3_nolog (m, nbytes, multibyte)
6009 Lisp_Object m;
6010 int nbytes, multibyte;
6011 {
6012 struct frame *sf = SELECTED_FRAME ();
6013 message_enable_multibyte = multibyte;
6014
6015 if (noninteractive)
6016 {
6017 if (noninteractive_need_newline)
6018 putc ('\n', stderr);
6019 noninteractive_need_newline = 0;
6020 if (STRINGP (m))
6021 fwrite (SDATA (m), nbytes, 1, stderr);
6022 if (cursor_in_echo_area == 0)
6023 fprintf (stderr, "\n");
6024 fflush (stderr);
6025 }
6026 /* A null message buffer means that the frame hasn't really been
6027 initialized yet. Error messages get reported properly by
6028 cmd_error, so this must be just an informative message; toss it. */
6029 else if (INTERACTIVE
6030 && sf->glyphs_initialized_p
6031 && FRAME_MESSAGE_BUF (sf))
6032 {
6033 Lisp_Object mini_window;
6034 Lisp_Object frame;
6035 struct frame *f;
6036
6037 /* Get the frame containing the mini-buffer
6038 that the selected frame is using. */
6039 mini_window = FRAME_MINIBUF_WINDOW (sf);
6040 frame = XWINDOW (mini_window)->frame;
6041 f = XFRAME (frame);
6042
6043 FRAME_SAMPLE_VISIBILITY (f);
6044 if (FRAME_VISIBLE_P (sf)
6045 && !FRAME_VISIBLE_P (f))
6046 Fmake_frame_visible (frame);
6047
6048 if (STRINGP (m) && SCHARS (m) > 0)
6049 {
6050 set_message (NULL, m, nbytes, multibyte);
6051 if (minibuffer_auto_raise)
6052 Fraise_frame (frame);
6053 }
6054 else
6055 clear_message (1, 1);
6056
6057 do_pending_window_change (0);
6058 echo_area_display (1);
6059 do_pending_window_change (0);
6060 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6061 (*frame_up_to_date_hook) (f);
6062 }
6063 }
6064
6065
6066 /* Display a null-terminated echo area message M. If M is 0, clear
6067 out any existing message, and let the mini-buffer text show through.
6068
6069 The buffer M must continue to exist until after the echo area gets
6070 cleared or some other message gets displayed there. Do not pass
6071 text that is stored in a Lisp string. Do not pass text in a buffer
6072 that was alloca'd. */
6073
6074 void
6075 message1 (m)
6076 char *m;
6077 {
6078 message2 (m, (m ? strlen (m) : 0), 0);
6079 }
6080
6081
6082 /* The non-logging counterpart of message1. */
6083
6084 void
6085 message1_nolog (m)
6086 char *m;
6087 {
6088 message2_nolog (m, (m ? strlen (m) : 0), 0);
6089 }
6090
6091 /* Display a message M which contains a single %s
6092 which gets replaced with STRING. */
6093
6094 void
6095 message_with_string (m, string, log)
6096 char *m;
6097 Lisp_Object string;
6098 int log;
6099 {
6100 CHECK_STRING (string);
6101
6102 if (noninteractive)
6103 {
6104 if (m)
6105 {
6106 if (noninteractive_need_newline)
6107 putc ('\n', stderr);
6108 noninteractive_need_newline = 0;
6109 fprintf (stderr, m, SDATA (string));
6110 if (cursor_in_echo_area == 0)
6111 fprintf (stderr, "\n");
6112 fflush (stderr);
6113 }
6114 }
6115 else if (INTERACTIVE)
6116 {
6117 /* The frame whose minibuffer we're going to display the message on.
6118 It may be larger than the selected frame, so we need
6119 to use its buffer, not the selected frame's buffer. */
6120 Lisp_Object mini_window;
6121 struct frame *f, *sf = SELECTED_FRAME ();
6122
6123 /* Get the frame containing the minibuffer
6124 that the selected frame is using. */
6125 mini_window = FRAME_MINIBUF_WINDOW (sf);
6126 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6127
6128 /* A null message buffer means that the frame hasn't really been
6129 initialized yet. Error messages get reported properly by
6130 cmd_error, so this must be just an informative message; toss it. */
6131 if (FRAME_MESSAGE_BUF (f))
6132 {
6133 Lisp_Object args[2], message;
6134 struct gcpro gcpro1, gcpro2;
6135
6136 args[0] = build_string (m);
6137 args[1] = message = string;
6138 GCPRO2 (args[0], message);
6139 gcpro1.nvars = 2;
6140
6141 message = Fformat (2, args);
6142
6143 if (log)
6144 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
6145 else
6146 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
6147
6148 UNGCPRO;
6149
6150 /* Print should start at the beginning of the message
6151 buffer next time. */
6152 message_buf_print = 0;
6153 }
6154 }
6155 }
6156
6157
6158 /* Dump an informative message to the minibuf. If M is 0, clear out
6159 any existing message, and let the mini-buffer text show through. */
6160
6161 /* VARARGS 1 */
6162 void
6163 message (m, a1, a2, a3)
6164 char *m;
6165 EMACS_INT a1, a2, a3;
6166 {
6167 if (noninteractive)
6168 {
6169 if (m)
6170 {
6171 if (noninteractive_need_newline)
6172 putc ('\n', stderr);
6173 noninteractive_need_newline = 0;
6174 fprintf (stderr, m, a1, a2, a3);
6175 if (cursor_in_echo_area == 0)
6176 fprintf (stderr, "\n");
6177 fflush (stderr);
6178 }
6179 }
6180 else if (INTERACTIVE)
6181 {
6182 /* The frame whose mini-buffer we're going to display the message
6183 on. It may be larger than the selected frame, so we need to
6184 use its buffer, not the selected frame's buffer. */
6185 Lisp_Object mini_window;
6186 struct frame *f, *sf = SELECTED_FRAME ();
6187
6188 /* Get the frame containing the mini-buffer
6189 that the selected frame is using. */
6190 mini_window = FRAME_MINIBUF_WINDOW (sf);
6191 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6192
6193 /* A null message buffer means that the frame hasn't really been
6194 initialized yet. Error messages get reported properly by
6195 cmd_error, so this must be just an informative message; toss
6196 it. */
6197 if (FRAME_MESSAGE_BUF (f))
6198 {
6199 if (m)
6200 {
6201 int len;
6202 #ifdef NO_ARG_ARRAY
6203 char *a[3];
6204 a[0] = (char *) a1;
6205 a[1] = (char *) a2;
6206 a[2] = (char *) a3;
6207
6208 len = doprnt (FRAME_MESSAGE_BUF (f),
6209 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6210 #else
6211 len = doprnt (FRAME_MESSAGE_BUF (f),
6212 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
6213 (char **) &a1);
6214 #endif /* NO_ARG_ARRAY */
6215
6216 message2 (FRAME_MESSAGE_BUF (f), len, 0);
6217 }
6218 else
6219 message1 (0);
6220
6221 /* Print should start at the beginning of the message
6222 buffer next time. */
6223 message_buf_print = 0;
6224 }
6225 }
6226 }
6227
6228
6229 /* The non-logging version of message. */
6230
6231 void
6232 message_nolog (m, a1, a2, a3)
6233 char *m;
6234 EMACS_INT a1, a2, a3;
6235 {
6236 Lisp_Object old_log_max;
6237 old_log_max = Vmessage_log_max;
6238 Vmessage_log_max = Qnil;
6239 message (m, a1, a2, a3);
6240 Vmessage_log_max = old_log_max;
6241 }
6242
6243
6244 /* Display the current message in the current mini-buffer. This is
6245 only called from error handlers in process.c, and is not time
6246 critical. */
6247
6248 void
6249 update_echo_area ()
6250 {
6251 if (!NILP (echo_area_buffer[0]))
6252 {
6253 Lisp_Object string;
6254 string = Fcurrent_message ();
6255 message3 (string, SBYTES (string),
6256 !NILP (current_buffer->enable_multibyte_characters));
6257 }
6258 }
6259
6260
6261 /* Make sure echo area buffers in `echo_buffers' are live.
6262 If they aren't, make new ones. */
6263
6264 static void
6265 ensure_echo_area_buffers ()
6266 {
6267 int i;
6268
6269 for (i = 0; i < 2; ++i)
6270 if (!BUFFERP (echo_buffer[i])
6271 || NILP (XBUFFER (echo_buffer[i])->name))
6272 {
6273 char name[30];
6274 Lisp_Object old_buffer;
6275 int j;
6276
6277 old_buffer = echo_buffer[i];
6278 sprintf (name, " *Echo Area %d*", i);
6279 echo_buffer[i] = Fget_buffer_create (build_string (name));
6280 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
6281
6282 for (j = 0; j < 2; ++j)
6283 if (EQ (old_buffer, echo_area_buffer[j]))
6284 echo_area_buffer[j] = echo_buffer[i];
6285 }
6286 }
6287
6288
6289 /* Call FN with args A1..A4 with either the current or last displayed
6290 echo_area_buffer as current buffer.
6291
6292 WHICH zero means use the current message buffer
6293 echo_area_buffer[0]. If that is nil, choose a suitable buffer
6294 from echo_buffer[] and clear it.
6295
6296 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
6297 suitable buffer from echo_buffer[] and clear it.
6298
6299 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
6300 that the current message becomes the last displayed one, make
6301 choose a suitable buffer for echo_area_buffer[0], and clear it.
6302
6303 Value is what FN returns. */
6304
6305 static int
6306 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
6307 struct window *w;
6308 int which;
6309 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
6310 EMACS_INT a1;
6311 Lisp_Object a2;
6312 EMACS_INT a3, a4;
6313 {
6314 Lisp_Object buffer;
6315 int this_one, the_other, clear_buffer_p, rc;
6316 int count = SPECPDL_INDEX ();
6317
6318 /* If buffers aren't live, make new ones. */
6319 ensure_echo_area_buffers ();
6320
6321 clear_buffer_p = 0;
6322
6323 if (which == 0)
6324 this_one = 0, the_other = 1;
6325 else if (which > 0)
6326 this_one = 1, the_other = 0;
6327 else
6328 {
6329 this_one = 0, the_other = 1;
6330 clear_buffer_p = 1;
6331
6332 /* We need a fresh one in case the current echo buffer equals
6333 the one containing the last displayed echo area message. */
6334 if (!NILP (echo_area_buffer[this_one])
6335 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
6336 echo_area_buffer[this_one] = Qnil;
6337 }
6338
6339 /* Choose a suitable buffer from echo_buffer[] is we don't
6340 have one. */
6341 if (NILP (echo_area_buffer[this_one]))
6342 {
6343 echo_area_buffer[this_one]
6344 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
6345 ? echo_buffer[the_other]
6346 : echo_buffer[this_one]);
6347 clear_buffer_p = 1;
6348 }
6349
6350 buffer = echo_area_buffer[this_one];
6351
6352 /* Don't get confused by reusing the buffer used for echoing
6353 for a different purpose. */
6354 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
6355 cancel_echoing ();
6356
6357 record_unwind_protect (unwind_with_echo_area_buffer,
6358 with_echo_area_buffer_unwind_data (w));
6359
6360 /* Make the echo area buffer current. Note that for display
6361 purposes, it is not necessary that the displayed window's buffer
6362 == current_buffer, except for text property lookup. So, let's
6363 only set that buffer temporarily here without doing a full
6364 Fset_window_buffer. We must also change w->pointm, though,
6365 because otherwise an assertions in unshow_buffer fails, and Emacs
6366 aborts. */
6367 set_buffer_internal_1 (XBUFFER (buffer));
6368 if (w)
6369 {
6370 w->buffer = buffer;
6371 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
6372 }
6373
6374 current_buffer->undo_list = Qt;
6375 current_buffer->read_only = Qnil;
6376 specbind (Qinhibit_read_only, Qt);
6377 specbind (Qinhibit_modification_hooks, Qt);
6378
6379 if (clear_buffer_p && Z > BEG)
6380 del_range (BEG, Z);
6381
6382 xassert (BEGV >= BEG);
6383 xassert (ZV <= Z && ZV >= BEGV);
6384
6385 rc = fn (a1, a2, a3, a4);
6386
6387 xassert (BEGV >= BEG);
6388 xassert (ZV <= Z && ZV >= BEGV);
6389
6390 unbind_to (count, Qnil);
6391 return rc;
6392 }
6393
6394
6395 /* Save state that should be preserved around the call to the function
6396 FN called in with_echo_area_buffer. */
6397
6398 static Lisp_Object
6399 with_echo_area_buffer_unwind_data (w)
6400 struct window *w;
6401 {
6402 int i = 0;
6403 Lisp_Object vector;
6404
6405 /* Reduce consing by keeping one vector in
6406 Vwith_echo_area_save_vector. */
6407 vector = Vwith_echo_area_save_vector;
6408 Vwith_echo_area_save_vector = Qnil;
6409
6410 if (NILP (vector))
6411 vector = Fmake_vector (make_number (7), Qnil);
6412
6413 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
6414 AREF (vector, i) = Vdeactivate_mark, ++i;
6415 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
6416
6417 if (w)
6418 {
6419 XSETWINDOW (AREF (vector, i), w); ++i;
6420 AREF (vector, i) = w->buffer; ++i;
6421 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
6422 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
6423 }
6424 else
6425 {
6426 int end = i + 4;
6427 for (; i < end; ++i)
6428 AREF (vector, i) = Qnil;
6429 }
6430
6431 xassert (i == ASIZE (vector));
6432 return vector;
6433 }
6434
6435
6436 /* Restore global state from VECTOR which was created by
6437 with_echo_area_buffer_unwind_data. */
6438
6439 static Lisp_Object
6440 unwind_with_echo_area_buffer (vector)
6441 Lisp_Object vector;
6442 {
6443 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
6444 Vdeactivate_mark = AREF (vector, 1);
6445 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
6446
6447 if (WINDOWP (AREF (vector, 3)))
6448 {
6449 struct window *w;
6450 Lisp_Object buffer, charpos, bytepos;
6451
6452 w = XWINDOW (AREF (vector, 3));
6453 buffer = AREF (vector, 4);
6454 charpos = AREF (vector, 5);
6455 bytepos = AREF (vector, 6);
6456
6457 w->buffer = buffer;
6458 set_marker_both (w->pointm, buffer,
6459 XFASTINT (charpos), XFASTINT (bytepos));
6460 }
6461
6462 Vwith_echo_area_save_vector = vector;
6463 return Qnil;
6464 }
6465
6466
6467 /* Set up the echo area for use by print functions. MULTIBYTE_P
6468 non-zero means we will print multibyte. */
6469
6470 void
6471 setup_echo_area_for_printing (multibyte_p)
6472 int multibyte_p;
6473 {
6474 ensure_echo_area_buffers ();
6475
6476 if (!message_buf_print)
6477 {
6478 /* A message has been output since the last time we printed.
6479 Choose a fresh echo area buffer. */
6480 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6481 echo_area_buffer[0] = echo_buffer[1];
6482 else
6483 echo_area_buffer[0] = echo_buffer[0];
6484
6485 /* Switch to that buffer and clear it. */
6486 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6487 current_buffer->truncate_lines = Qnil;
6488
6489 if (Z > BEG)
6490 {
6491 int count = SPECPDL_INDEX ();
6492 specbind (Qinhibit_read_only, Qt);
6493 /* Note that undo recording is always disabled. */
6494 del_range (BEG, Z);
6495 unbind_to (count, Qnil);
6496 }
6497 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6498
6499 /* Set up the buffer for the multibyteness we need. */
6500 if (multibyte_p
6501 != !NILP (current_buffer->enable_multibyte_characters))
6502 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
6503
6504 /* Raise the frame containing the echo area. */
6505 if (minibuffer_auto_raise)
6506 {
6507 struct frame *sf = SELECTED_FRAME ();
6508 Lisp_Object mini_window;
6509 mini_window = FRAME_MINIBUF_WINDOW (sf);
6510 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6511 }
6512
6513 message_log_maybe_newline ();
6514 message_buf_print = 1;
6515 }
6516 else
6517 {
6518 if (NILP (echo_area_buffer[0]))
6519 {
6520 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6521 echo_area_buffer[0] = echo_buffer[1];
6522 else
6523 echo_area_buffer[0] = echo_buffer[0];
6524 }
6525
6526 if (current_buffer != XBUFFER (echo_area_buffer[0]))
6527 {
6528 /* Someone switched buffers between print requests. */
6529 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6530 current_buffer->truncate_lines = Qnil;
6531 }
6532 }
6533 }
6534
6535
6536 /* Display an echo area message in window W. Value is non-zero if W's
6537 height is changed. If display_last_displayed_message_p is
6538 non-zero, display the message that was last displayed, otherwise
6539 display the current message. */
6540
6541 static int
6542 display_echo_area (w)
6543 struct window *w;
6544 {
6545 int i, no_message_p, window_height_changed_p, count;
6546
6547 /* Temporarily disable garbage collections while displaying the echo
6548 area. This is done because a GC can print a message itself.
6549 That message would modify the echo area buffer's contents while a
6550 redisplay of the buffer is going on, and seriously confuse
6551 redisplay. */
6552 count = inhibit_garbage_collection ();
6553
6554 /* If there is no message, we must call display_echo_area_1
6555 nevertheless because it resizes the window. But we will have to
6556 reset the echo_area_buffer in question to nil at the end because
6557 with_echo_area_buffer will sets it to an empty buffer. */
6558 i = display_last_displayed_message_p ? 1 : 0;
6559 no_message_p = NILP (echo_area_buffer[i]);
6560
6561 window_height_changed_p
6562 = with_echo_area_buffer (w, display_last_displayed_message_p,
6563 display_echo_area_1,
6564 (EMACS_INT) w, Qnil, 0, 0);
6565
6566 if (no_message_p)
6567 echo_area_buffer[i] = Qnil;
6568
6569 unbind_to (count, Qnil);
6570 return window_height_changed_p;
6571 }
6572
6573
6574 /* Helper for display_echo_area. Display the current buffer which
6575 contains the current echo area message in window W, a mini-window,
6576 a pointer to which is passed in A1. A2..A4 are currently not used.
6577 Change the height of W so that all of the message is displayed.
6578 Value is non-zero if height of W was changed. */
6579
6580 static int
6581 display_echo_area_1 (a1, a2, a3, a4)
6582 EMACS_INT a1;
6583 Lisp_Object a2;
6584 EMACS_INT a3, a4;
6585 {
6586 struct window *w = (struct window *) a1;
6587 Lisp_Object window;
6588 struct text_pos start;
6589 int window_height_changed_p = 0;
6590
6591 /* Do this before displaying, so that we have a large enough glyph
6592 matrix for the display. */
6593 window_height_changed_p = resize_mini_window (w, 0);
6594
6595 /* Display. */
6596 clear_glyph_matrix (w->desired_matrix);
6597 XSETWINDOW (window, w);
6598 SET_TEXT_POS (start, BEG, BEG_BYTE);
6599 try_window (window, start);
6600
6601 return window_height_changed_p;
6602 }
6603
6604
6605 /* Resize the echo area window to exactly the size needed for the
6606 currently displayed message, if there is one. If a mini-buffer
6607 is active, don't shrink it. */
6608
6609 void
6610 resize_echo_area_exactly ()
6611 {
6612 if (BUFFERP (echo_area_buffer[0])
6613 && WINDOWP (echo_area_window))
6614 {
6615 struct window *w = XWINDOW (echo_area_window);
6616 int resized_p;
6617 Lisp_Object resize_exactly;
6618
6619 if (minibuf_level == 0)
6620 resize_exactly = Qt;
6621 else
6622 resize_exactly = Qnil;
6623
6624 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
6625 (EMACS_INT) w, resize_exactly, 0, 0);
6626 if (resized_p)
6627 {
6628 ++windows_or_buffers_changed;
6629 ++update_mode_lines;
6630 redisplay_internal (0);
6631 }
6632 }
6633 }
6634
6635
6636 /* Callback function for with_echo_area_buffer, when used from
6637 resize_echo_area_exactly. A1 contains a pointer to the window to
6638 resize, EXACTLY non-nil means resize the mini-window exactly to the
6639 size of the text displayed. A3 and A4 are not used. Value is what
6640 resize_mini_window returns. */
6641
6642 static int
6643 resize_mini_window_1 (a1, exactly, a3, a4)
6644 EMACS_INT a1;
6645 Lisp_Object exactly;
6646 EMACS_INT a3, a4;
6647 {
6648 return resize_mini_window ((struct window *) a1, !NILP (exactly));
6649 }
6650
6651
6652 /* Resize mini-window W to fit the size of its contents. EXACT:P
6653 means size the window exactly to the size needed. Otherwise, it's
6654 only enlarged until W's buffer is empty. Value is non-zero if
6655 the window height has been changed. */
6656
6657 int
6658 resize_mini_window (w, exact_p)
6659 struct window *w;
6660 int exact_p;
6661 {
6662 struct frame *f = XFRAME (w->frame);
6663 int window_height_changed_p = 0;
6664
6665 xassert (MINI_WINDOW_P (w));
6666
6667 /* Don't resize windows while redisplaying a window; it would
6668 confuse redisplay functions when the size of the window they are
6669 displaying changes from under them. Such a resizing can happen,
6670 for instance, when which-func prints a long message while
6671 we are running fontification-functions. We're running these
6672 functions with safe_call which binds inhibit-redisplay to t. */
6673 if (!NILP (Vinhibit_redisplay))
6674 return 0;
6675
6676 /* Nil means don't try to resize. */
6677 if (NILP (Vresize_mini_windows)
6678 || (FRAME_X_P (f) && f->output_data.x == NULL))
6679 return 0;
6680
6681 if (!FRAME_MINIBUF_ONLY_P (f))
6682 {
6683 struct it it;
6684 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
6685 int total_height = XFASTINT (root->height) + XFASTINT (w->height);
6686 int height, max_height;
6687 int unit = CANON_Y_UNIT (f);
6688 struct text_pos start;
6689 struct buffer *old_current_buffer = NULL;
6690
6691 if (current_buffer != XBUFFER (w->buffer))
6692 {
6693 old_current_buffer = current_buffer;
6694 set_buffer_internal (XBUFFER (w->buffer));
6695 }
6696
6697 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
6698
6699 /* Compute the max. number of lines specified by the user. */
6700 if (FLOATP (Vmax_mini_window_height))
6701 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_HEIGHT (f);
6702 else if (INTEGERP (Vmax_mini_window_height))
6703 max_height = XINT (Vmax_mini_window_height);
6704 else
6705 max_height = total_height / 4;
6706
6707 /* Correct that max. height if it's bogus. */
6708 max_height = max (1, max_height);
6709 max_height = min (total_height, max_height);
6710
6711 /* Find out the height of the text in the window. */
6712 if (it.truncate_lines_p)
6713 height = 1;
6714 else
6715 {
6716 last_height = 0;
6717 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
6718 if (it.max_ascent == 0 && it.max_descent == 0)
6719 height = it.current_y + last_height;
6720 else
6721 height = it.current_y + it.max_ascent + it.max_descent;
6722 height -= it.extra_line_spacing;
6723 height = (height + unit - 1) / unit;
6724 }
6725
6726 /* Compute a suitable window start. */
6727 if (height > max_height)
6728 {
6729 height = max_height;
6730 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
6731 move_it_vertically_backward (&it, (height - 1) * unit);
6732 start = it.current.pos;
6733 }
6734 else
6735 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
6736 SET_MARKER_FROM_TEXT_POS (w->start, start);
6737
6738 if (EQ (Vresize_mini_windows, Qgrow_only))
6739 {
6740 /* Let it grow only, until we display an empty message, in which
6741 case the window shrinks again. */
6742 if (height > XFASTINT (w->height))
6743 {
6744 int old_height = XFASTINT (w->height);
6745 freeze_window_starts (f, 1);
6746 grow_mini_window (w, height - XFASTINT (w->height));
6747 window_height_changed_p = XFASTINT (w->height) != old_height;
6748 }
6749 else if (height < XFASTINT (w->height)
6750 && (exact_p || BEGV == ZV))
6751 {
6752 int old_height = XFASTINT (w->height);
6753 freeze_window_starts (f, 0);
6754 shrink_mini_window (w);
6755 window_height_changed_p = XFASTINT (w->height) != old_height;
6756 }
6757 }
6758 else
6759 {
6760 /* Always resize to exact size needed. */
6761 if (height > XFASTINT (w->height))
6762 {
6763 int old_height = XFASTINT (w->height);
6764 freeze_window_starts (f, 1);
6765 grow_mini_window (w, height - XFASTINT (w->height));
6766 window_height_changed_p = XFASTINT (w->height) != old_height;
6767 }
6768 else if (height < XFASTINT (w->height))
6769 {
6770 int old_height = XFASTINT (w->height);
6771 freeze_window_starts (f, 0);
6772 shrink_mini_window (w);
6773
6774 if (height)
6775 {
6776 freeze_window_starts (f, 1);
6777 grow_mini_window (w, height - XFASTINT (w->height));
6778 }
6779
6780 window_height_changed_p = XFASTINT (w->height) != old_height;
6781 }
6782 }
6783
6784 if (old_current_buffer)
6785 set_buffer_internal (old_current_buffer);
6786 }
6787
6788 return window_height_changed_p;
6789 }
6790
6791
6792 /* Value is the current message, a string, or nil if there is no
6793 current message. */
6794
6795 Lisp_Object
6796 current_message ()
6797 {
6798 Lisp_Object msg;
6799
6800 if (NILP (echo_area_buffer[0]))
6801 msg = Qnil;
6802 else
6803 {
6804 with_echo_area_buffer (0, 0, current_message_1,
6805 (EMACS_INT) &msg, Qnil, 0, 0);
6806 if (NILP (msg))
6807 echo_area_buffer[0] = Qnil;
6808 }
6809
6810 return msg;
6811 }
6812
6813
6814 static int
6815 current_message_1 (a1, a2, a3, a4)
6816 EMACS_INT a1;
6817 Lisp_Object a2;
6818 EMACS_INT a3, a4;
6819 {
6820 Lisp_Object *msg = (Lisp_Object *) a1;
6821
6822 if (Z > BEG)
6823 *msg = make_buffer_string (BEG, Z, 1);
6824 else
6825 *msg = Qnil;
6826 return 0;
6827 }
6828
6829
6830 /* Push the current message on Vmessage_stack for later restauration
6831 by restore_message. Value is non-zero if the current message isn't
6832 empty. This is a relatively infrequent operation, so it's not
6833 worth optimizing. */
6834
6835 int
6836 push_message ()
6837 {
6838 Lisp_Object msg;
6839 msg = current_message ();
6840 Vmessage_stack = Fcons (msg, Vmessage_stack);
6841 return STRINGP (msg);
6842 }
6843
6844
6845 /* Restore message display from the top of Vmessage_stack. */
6846
6847 void
6848 restore_message ()
6849 {
6850 Lisp_Object msg;
6851
6852 xassert (CONSP (Vmessage_stack));
6853 msg = XCAR (Vmessage_stack);
6854 if (STRINGP (msg))
6855 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
6856 else
6857 message3_nolog (msg, 0, 0);
6858 }
6859
6860
6861 /* Handler for record_unwind_protect calling pop_message. */
6862
6863 Lisp_Object
6864 pop_message_unwind (dummy)
6865 Lisp_Object dummy;
6866 {
6867 pop_message ();
6868 return Qnil;
6869 }
6870
6871 /* Pop the top-most entry off Vmessage_stack. */
6872
6873 void
6874 pop_message ()
6875 {
6876 xassert (CONSP (Vmessage_stack));
6877 Vmessage_stack = XCDR (Vmessage_stack);
6878 }
6879
6880
6881 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
6882 exits. If the stack is not empty, we have a missing pop_message
6883 somewhere. */
6884
6885 void
6886 check_message_stack ()
6887 {
6888 if (!NILP (Vmessage_stack))
6889 abort ();
6890 }
6891
6892
6893 /* Truncate to NCHARS what will be displayed in the echo area the next
6894 time we display it---but don't redisplay it now. */
6895
6896 void
6897 truncate_echo_area (nchars)
6898 int nchars;
6899 {
6900 if (nchars == 0)
6901 echo_area_buffer[0] = Qnil;
6902 /* A null message buffer means that the frame hasn't really been
6903 initialized yet. Error messages get reported properly by
6904 cmd_error, so this must be just an informative message; toss it. */
6905 else if (!noninteractive
6906 && INTERACTIVE
6907 && !NILP (echo_area_buffer[0]))
6908 {
6909 struct frame *sf = SELECTED_FRAME ();
6910 if (FRAME_MESSAGE_BUF (sf))
6911 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
6912 }
6913 }
6914
6915
6916 /* Helper function for truncate_echo_area. Truncate the current
6917 message to at most NCHARS characters. */
6918
6919 static int
6920 truncate_message_1 (nchars, a2, a3, a4)
6921 EMACS_INT nchars;
6922 Lisp_Object a2;
6923 EMACS_INT a3, a4;
6924 {
6925 if (BEG + nchars < Z)
6926 del_range (BEG + nchars, Z);
6927 if (Z == BEG)
6928 echo_area_buffer[0] = Qnil;
6929 return 0;
6930 }
6931
6932
6933 /* Set the current message to a substring of S or STRING.
6934
6935 If STRING is a Lisp string, set the message to the first NBYTES
6936 bytes from STRING. NBYTES zero means use the whole string. If
6937 STRING is multibyte, the message will be displayed multibyte.
6938
6939 If S is not null, set the message to the first LEN bytes of S. LEN
6940 zero means use the whole string. MULTIBYTE_P non-zero means S is
6941 multibyte. Display the message multibyte in that case. */
6942
6943 void
6944 set_message (s, string, nbytes, multibyte_p)
6945 const char *s;
6946 Lisp_Object string;
6947 int nbytes;
6948 {
6949 message_enable_multibyte
6950 = ((s && multibyte_p)
6951 || (STRINGP (string) && STRING_MULTIBYTE (string)));
6952
6953 with_echo_area_buffer (0, -1, set_message_1,
6954 (EMACS_INT) s, string, nbytes, multibyte_p);
6955 message_buf_print = 0;
6956 help_echo_showing_p = 0;
6957 }
6958
6959
6960 /* Helper function for set_message. Arguments have the same meaning
6961 as there, with A1 corresponding to S and A2 corresponding to STRING
6962 This function is called with the echo area buffer being
6963 current. */
6964
6965 static int
6966 set_message_1 (a1, a2, nbytes, multibyte_p)
6967 EMACS_INT a1;
6968 Lisp_Object a2;
6969 EMACS_INT nbytes, multibyte_p;
6970 {
6971 const char *s = (const char *) a1;
6972 Lisp_Object string = a2;
6973
6974 xassert (BEG == Z);
6975
6976 /* Change multibyteness of the echo buffer appropriately. */
6977 if (message_enable_multibyte
6978 != !NILP (current_buffer->enable_multibyte_characters))
6979 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
6980
6981 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
6982
6983 /* Insert new message at BEG. */
6984 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6985
6986 if (STRINGP (string))
6987 {
6988 int nchars;
6989
6990 if (nbytes == 0)
6991 nbytes = SBYTES (string);
6992 nchars = string_byte_to_char (string, nbytes);
6993
6994 /* This function takes care of single/multibyte conversion. We
6995 just have to ensure that the echo area buffer has the right
6996 setting of enable_multibyte_characters. */
6997 insert_from_string (string, 0, 0, nchars, nbytes, 1);
6998 }
6999 else if (s)
7000 {
7001 if (nbytes == 0)
7002 nbytes = strlen (s);
7003
7004 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
7005 {
7006 /* Convert from multi-byte to single-byte. */
7007 int i, c, n;
7008 unsigned char work[1];
7009
7010 /* Convert a multibyte string to single-byte. */
7011 for (i = 0; i < nbytes; i += n)
7012 {
7013 c = string_char_and_length (s + i, nbytes - i, &n);
7014 work[0] = (SINGLE_BYTE_CHAR_P (c)
7015 ? c
7016 : multibyte_char_to_unibyte (c, Qnil));
7017 insert_1_both (work, 1, 1, 1, 0, 0);
7018 }
7019 }
7020 else if (!multibyte_p
7021 && !NILP (current_buffer->enable_multibyte_characters))
7022 {
7023 /* Convert from single-byte to multi-byte. */
7024 int i, c, n;
7025 const unsigned char *msg = (const unsigned char *) s;
7026 unsigned char str[MAX_MULTIBYTE_LENGTH];
7027
7028 /* Convert a single-byte string to multibyte. */
7029 for (i = 0; i < nbytes; i++)
7030 {
7031 c = unibyte_char_to_multibyte (msg[i]);
7032 n = CHAR_STRING (c, str);
7033 insert_1_both (str, 1, n, 1, 0, 0);
7034 }
7035 }
7036 else
7037 insert_1 (s, nbytes, 1, 0, 0);
7038 }
7039
7040 return 0;
7041 }
7042
7043
7044 /* Clear messages. CURRENT_P non-zero means clear the current
7045 message. LAST_DISPLAYED_P non-zero means clear the message
7046 last displayed. */
7047
7048 void
7049 clear_message (current_p, last_displayed_p)
7050 int current_p, last_displayed_p;
7051 {
7052 if (current_p)
7053 {
7054 echo_area_buffer[0] = Qnil;
7055 message_cleared_p = 1;
7056 }
7057
7058 if (last_displayed_p)
7059 echo_area_buffer[1] = Qnil;
7060
7061 message_buf_print = 0;
7062 }
7063
7064 /* Clear garbaged frames.
7065
7066 This function is used where the old redisplay called
7067 redraw_garbaged_frames which in turn called redraw_frame which in
7068 turn called clear_frame. The call to clear_frame was a source of
7069 flickering. I believe a clear_frame is not necessary. It should
7070 suffice in the new redisplay to invalidate all current matrices,
7071 and ensure a complete redisplay of all windows. */
7072
7073 static void
7074 clear_garbaged_frames ()
7075 {
7076 if (frame_garbaged)
7077 {
7078 Lisp_Object tail, frame;
7079 int changed_count = 0;
7080
7081 FOR_EACH_FRAME (tail, frame)
7082 {
7083 struct frame *f = XFRAME (frame);
7084
7085 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
7086 {
7087 if (f->resized_p)
7088 Fredraw_frame (frame);
7089 clear_current_matrices (f);
7090 changed_count++;
7091 f->garbaged = 0;
7092 f->resized_p = 0;
7093 }
7094 }
7095
7096 frame_garbaged = 0;
7097 if (changed_count)
7098 ++windows_or_buffers_changed;
7099 }
7100 }
7101
7102
7103 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
7104 is non-zero update selected_frame. Value is non-zero if the
7105 mini-windows height has been changed. */
7106
7107 static int
7108 echo_area_display (update_frame_p)
7109 int update_frame_p;
7110 {
7111 Lisp_Object mini_window;
7112 struct window *w;
7113 struct frame *f;
7114 int window_height_changed_p = 0;
7115 struct frame *sf = SELECTED_FRAME ();
7116
7117 mini_window = FRAME_MINIBUF_WINDOW (sf);
7118 w = XWINDOW (mini_window);
7119 f = XFRAME (WINDOW_FRAME (w));
7120
7121 /* Don't display if frame is invisible or not yet initialized. */
7122 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
7123 return 0;
7124
7125 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
7126 #ifndef MAC_OS8
7127 #ifdef HAVE_WINDOW_SYSTEM
7128 /* When Emacs starts, selected_frame may be a visible terminal
7129 frame, even if we run under a window system. If we let this
7130 through, a message would be displayed on the terminal. */
7131 if (EQ (selected_frame, Vterminal_frame)
7132 && !NILP (Vwindow_system))
7133 return 0;
7134 #endif /* HAVE_WINDOW_SYSTEM */
7135 #endif
7136
7137 /* Redraw garbaged frames. */
7138 if (frame_garbaged)
7139 clear_garbaged_frames ();
7140
7141 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
7142 {
7143 echo_area_window = mini_window;
7144 window_height_changed_p = display_echo_area (w);
7145 w->must_be_updated_p = 1;
7146
7147 /* Update the display, unless called from redisplay_internal.
7148 Also don't update the screen during redisplay itself. The
7149 update will happen at the end of redisplay, and an update
7150 here could cause confusion. */
7151 if (update_frame_p && !redisplaying_p)
7152 {
7153 int n = 0;
7154
7155 /* If the display update has been interrupted by pending
7156 input, update mode lines in the frame. Due to the
7157 pending input, it might have been that redisplay hasn't
7158 been called, so that mode lines above the echo area are
7159 garbaged. This looks odd, so we prevent it here. */
7160 if (!display_completed)
7161 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
7162
7163 if (window_height_changed_p
7164 /* Don't do this if Emacs is shutting down. Redisplay
7165 needs to run hooks. */
7166 && !NILP (Vrun_hooks))
7167 {
7168 /* Must update other windows. Likewise as in other
7169 cases, don't let this update be interrupted by
7170 pending input. */
7171 int count = SPECPDL_INDEX ();
7172 specbind (Qredisplay_dont_pause, Qt);
7173 windows_or_buffers_changed = 1;
7174 redisplay_internal (0);
7175 unbind_to (count, Qnil);
7176 }
7177 else if (FRAME_WINDOW_P (f) && n == 0)
7178 {
7179 /* Window configuration is the same as before.
7180 Can do with a display update of the echo area,
7181 unless we displayed some mode lines. */
7182 update_single_window (w, 1);
7183 rif->flush_display (f);
7184 }
7185 else
7186 update_frame (f, 1, 1);
7187
7188 /* If cursor is in the echo area, make sure that the next
7189 redisplay displays the minibuffer, so that the cursor will
7190 be replaced with what the minibuffer wants. */
7191 if (cursor_in_echo_area)
7192 ++windows_or_buffers_changed;
7193 }
7194 }
7195 else if (!EQ (mini_window, selected_window))
7196 windows_or_buffers_changed++;
7197
7198 /* Last displayed message is now the current message. */
7199 echo_area_buffer[1] = echo_area_buffer[0];
7200
7201 /* Prevent redisplay optimization in redisplay_internal by resetting
7202 this_line_start_pos. This is done because the mini-buffer now
7203 displays the message instead of its buffer text. */
7204 if (EQ (mini_window, selected_window))
7205 CHARPOS (this_line_start_pos) = 0;
7206
7207 return window_height_changed_p;
7208 }
7209
7210
7211 \f
7212 /***********************************************************************
7213 Frame Titles
7214 ***********************************************************************/
7215
7216
7217 /* The frame title buffering code is also used by Fformat_mode_line.
7218 So it is not conditioned by HAVE_WINDOW_SYSTEM. */
7219
7220 /* A buffer for constructing frame titles in it; allocated from the
7221 heap in init_xdisp and resized as needed in store_frame_title_char. */
7222
7223 static char *frame_title_buf;
7224
7225 /* The buffer's end, and a current output position in it. */
7226
7227 static char *frame_title_buf_end;
7228 static char *frame_title_ptr;
7229
7230
7231 /* Store a single character C for the frame title in frame_title_buf.
7232 Re-allocate frame_title_buf if necessary. */
7233
7234 static void
7235 store_frame_title_char (c)
7236 char c;
7237 {
7238 /* If output position has reached the end of the allocated buffer,
7239 double the buffer's size. */
7240 if (frame_title_ptr == frame_title_buf_end)
7241 {
7242 int len = frame_title_ptr - frame_title_buf;
7243 int new_size = 2 * len * sizeof *frame_title_buf;
7244 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
7245 frame_title_buf_end = frame_title_buf + new_size;
7246 frame_title_ptr = frame_title_buf + len;
7247 }
7248
7249 *frame_title_ptr++ = c;
7250 }
7251
7252
7253 /* Store part of a frame title in frame_title_buf, beginning at
7254 frame_title_ptr. STR is the string to store. Do not copy
7255 characters that yield more columns than PRECISION; PRECISION <= 0
7256 means copy the whole string. Pad with spaces until FIELD_WIDTH
7257 number of characters have been copied; FIELD_WIDTH <= 0 means don't
7258 pad. Called from display_mode_element when it is used to build a
7259 frame title. */
7260
7261 static int
7262 store_frame_title (str, field_width, precision)
7263 const unsigned char *str;
7264 int field_width, precision;
7265 {
7266 int n = 0;
7267 int dummy, nbytes;
7268
7269 /* Copy at most PRECISION chars from STR. */
7270 nbytes = strlen (str);
7271 n+= c_string_width (str, nbytes, precision, &dummy, &nbytes);
7272 while (nbytes--)
7273 store_frame_title_char (*str++);
7274
7275 /* Fill up with spaces until FIELD_WIDTH reached. */
7276 while (field_width > 0
7277 && n < field_width)
7278 {
7279 store_frame_title_char (' ');
7280 ++n;
7281 }
7282
7283 return n;
7284 }
7285
7286 #ifdef HAVE_WINDOW_SYSTEM
7287
7288 /* Set the title of FRAME, if it has changed. The title format is
7289 Vicon_title_format if FRAME is iconified, otherwise it is
7290 frame_title_format. */
7291
7292 static void
7293 x_consider_frame_title (frame)
7294 Lisp_Object frame;
7295 {
7296 struct frame *f = XFRAME (frame);
7297
7298 if (FRAME_WINDOW_P (f)
7299 || FRAME_MINIBUF_ONLY_P (f)
7300 || f->explicit_name)
7301 {
7302 /* Do we have more than one visible frame on this X display? */
7303 Lisp_Object tail;
7304 Lisp_Object fmt;
7305 struct buffer *obuf;
7306 int len;
7307 struct it it;
7308
7309 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
7310 {
7311 Lisp_Object other_frame = XCAR (tail);
7312 struct frame *tf = XFRAME (other_frame);
7313
7314 if (tf != f
7315 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
7316 && !FRAME_MINIBUF_ONLY_P (tf)
7317 && !EQ (other_frame, tip_frame)
7318 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
7319 break;
7320 }
7321
7322 /* Set global variable indicating that multiple frames exist. */
7323 multiple_frames = CONSP (tail);
7324
7325 /* Switch to the buffer of selected window of the frame. Set up
7326 frame_title_ptr so that display_mode_element will output into it;
7327 then display the title. */
7328 obuf = current_buffer;
7329 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
7330 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
7331 frame_title_ptr = frame_title_buf;
7332 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
7333 NULL, DEFAULT_FACE_ID);
7334 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
7335 len = frame_title_ptr - frame_title_buf;
7336 frame_title_ptr = NULL;
7337 set_buffer_internal_1 (obuf);
7338
7339 /* Set the title only if it's changed. This avoids consing in
7340 the common case where it hasn't. (If it turns out that we've
7341 already wasted too much time by walking through the list with
7342 display_mode_element, then we might need to optimize at a
7343 higher level than this.) */
7344 if (! STRINGP (f->name)
7345 || SBYTES (f->name) != len
7346 || bcmp (frame_title_buf, SDATA (f->name), len) != 0)
7347 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
7348 }
7349 }
7350
7351 #endif /* not HAVE_WINDOW_SYSTEM */
7352
7353
7354
7355 \f
7356 /***********************************************************************
7357 Menu Bars
7358 ***********************************************************************/
7359
7360
7361 /* Prepare for redisplay by updating menu-bar item lists when
7362 appropriate. This can call eval. */
7363
7364 void
7365 prepare_menu_bars ()
7366 {
7367 int all_windows;
7368 struct gcpro gcpro1, gcpro2;
7369 struct frame *f;
7370 Lisp_Object tooltip_frame;
7371
7372 #ifdef HAVE_WINDOW_SYSTEM
7373 tooltip_frame = tip_frame;
7374 #else
7375 tooltip_frame = Qnil;
7376 #endif
7377
7378 /* Update all frame titles based on their buffer names, etc. We do
7379 this before the menu bars so that the buffer-menu will show the
7380 up-to-date frame titles. */
7381 #ifdef HAVE_WINDOW_SYSTEM
7382 if (windows_or_buffers_changed || update_mode_lines)
7383 {
7384 Lisp_Object tail, frame;
7385
7386 FOR_EACH_FRAME (tail, frame)
7387 {
7388 f = XFRAME (frame);
7389 if (!EQ (frame, tooltip_frame)
7390 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
7391 x_consider_frame_title (frame);
7392 }
7393 }
7394 #endif /* HAVE_WINDOW_SYSTEM */
7395
7396 /* Update the menu bar item lists, if appropriate. This has to be
7397 done before any actual redisplay or generation of display lines. */
7398 all_windows = (update_mode_lines
7399 || buffer_shared > 1
7400 || windows_or_buffers_changed);
7401 if (all_windows)
7402 {
7403 Lisp_Object tail, frame;
7404 int count = SPECPDL_INDEX ();
7405
7406 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7407
7408 FOR_EACH_FRAME (tail, frame)
7409 {
7410 f = XFRAME (frame);
7411
7412 /* Ignore tooltip frame. */
7413 if (EQ (frame, tooltip_frame))
7414 continue;
7415
7416 /* If a window on this frame changed size, report that to
7417 the user and clear the size-change flag. */
7418 if (FRAME_WINDOW_SIZES_CHANGED (f))
7419 {
7420 Lisp_Object functions;
7421
7422 /* Clear flag first in case we get an error below. */
7423 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
7424 functions = Vwindow_size_change_functions;
7425 GCPRO2 (tail, functions);
7426
7427 while (CONSP (functions))
7428 {
7429 call1 (XCAR (functions), frame);
7430 functions = XCDR (functions);
7431 }
7432 UNGCPRO;
7433 }
7434
7435 GCPRO1 (tail);
7436 update_menu_bar (f, 0);
7437 #ifdef HAVE_WINDOW_SYSTEM
7438 update_tool_bar (f, 0);
7439 #endif
7440 UNGCPRO;
7441 }
7442
7443 unbind_to (count, Qnil);
7444 }
7445 else
7446 {
7447 struct frame *sf = SELECTED_FRAME ();
7448 update_menu_bar (sf, 1);
7449 #ifdef HAVE_WINDOW_SYSTEM
7450 update_tool_bar (sf, 1);
7451 #endif
7452 }
7453
7454 /* Motif needs this. See comment in xmenu.c. Turn it off when
7455 pending_menu_activation is not defined. */
7456 #ifdef USE_X_TOOLKIT
7457 pending_menu_activation = 0;
7458 #endif
7459 }
7460
7461
7462 /* Update the menu bar item list for frame F. This has to be done
7463 before we start to fill in any display lines, because it can call
7464 eval.
7465
7466 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
7467
7468 static void
7469 update_menu_bar (f, save_match_data)
7470 struct frame *f;
7471 int save_match_data;
7472 {
7473 Lisp_Object window;
7474 register struct window *w;
7475
7476 /* If called recursively during a menu update, do nothing. This can
7477 happen when, for instance, an activate-menubar-hook causes a
7478 redisplay. */
7479 if (inhibit_menubar_update)
7480 return;
7481
7482 window = FRAME_SELECTED_WINDOW (f);
7483 w = XWINDOW (window);
7484
7485 #if 0 /* The if statement below this if statement used to include the
7486 condition !NILP (w->update_mode_line), rather than using
7487 update_mode_lines directly, and this if statement may have
7488 been added to make that condition work. Now the if
7489 statement below matches its comment, this isn't needed. */
7490 if (update_mode_lines)
7491 w->update_mode_line = Qt;
7492 #endif
7493
7494 if (FRAME_WINDOW_P (f)
7495 ?
7496 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS)
7497 FRAME_EXTERNAL_MENU_BAR (f)
7498 #else
7499 FRAME_MENU_BAR_LINES (f) > 0
7500 #endif
7501 : FRAME_MENU_BAR_LINES (f) > 0)
7502 {
7503 /* If the user has switched buffers or windows, we need to
7504 recompute to reflect the new bindings. But we'll
7505 recompute when update_mode_lines is set too; that means
7506 that people can use force-mode-line-update to request
7507 that the menu bar be recomputed. The adverse effect on
7508 the rest of the redisplay algorithm is about the same as
7509 windows_or_buffers_changed anyway. */
7510 if (windows_or_buffers_changed
7511 /* This used to test w->update_mode_line, but we believe
7512 there is no need to recompute the menu in that case. */
7513 || update_mode_lines
7514 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7515 < BUF_MODIFF (XBUFFER (w->buffer)))
7516 != !NILP (w->last_had_star))
7517 || ((!NILP (Vtransient_mark_mode)
7518 && !NILP (XBUFFER (w->buffer)->mark_active))
7519 != !NILP (w->region_showing)))
7520 {
7521 struct buffer *prev = current_buffer;
7522 int count = SPECPDL_INDEX ();
7523
7524 specbind (Qinhibit_menubar_update, Qt);
7525
7526 set_buffer_internal_1 (XBUFFER (w->buffer));
7527 if (save_match_data)
7528 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7529 if (NILP (Voverriding_local_map_menu_flag))
7530 {
7531 specbind (Qoverriding_terminal_local_map, Qnil);
7532 specbind (Qoverriding_local_map, Qnil);
7533 }
7534
7535 /* Run the Lucid hook. */
7536 safe_run_hooks (Qactivate_menubar_hook);
7537
7538 /* If it has changed current-menubar from previous value,
7539 really recompute the menu-bar from the value. */
7540 if (! NILP (Vlucid_menu_bar_dirty_flag))
7541 call0 (Qrecompute_lucid_menubar);
7542
7543 safe_run_hooks (Qmenu_bar_update_hook);
7544 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
7545
7546 /* Redisplay the menu bar in case we changed it. */
7547 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS)
7548 if (FRAME_WINDOW_P (f)
7549 #if defined (MAC_OS)
7550 /* All frames on Mac OS share the same menubar. So only the
7551 selected frame should be allowed to set it. */
7552 && f == SELECTED_FRAME ()
7553 #endif
7554 )
7555 set_frame_menubar (f, 0, 0);
7556 else
7557 /* On a terminal screen, the menu bar is an ordinary screen
7558 line, and this makes it get updated. */
7559 w->update_mode_line = Qt;
7560 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7561 /* In the non-toolkit version, the menu bar is an ordinary screen
7562 line, and this makes it get updated. */
7563 w->update_mode_line = Qt;
7564 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7565
7566 unbind_to (count, Qnil);
7567 set_buffer_internal_1 (prev);
7568 }
7569 }
7570 }
7571
7572
7573 \f
7574 /***********************************************************************
7575 Tool-bars
7576 ***********************************************************************/
7577
7578 #ifdef HAVE_WINDOW_SYSTEM
7579
7580 /* Update the tool-bar item list for frame F. This has to be done
7581 before we start to fill in any display lines. Called from
7582 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
7583 and restore it here. */
7584
7585 static void
7586 update_tool_bar (f, save_match_data)
7587 struct frame *f;
7588 int save_match_data;
7589 {
7590 if (WINDOWP (f->tool_bar_window)
7591 && XFASTINT (XWINDOW (f->tool_bar_window)->height) > 0)
7592 {
7593 Lisp_Object window;
7594 struct window *w;
7595
7596 window = FRAME_SELECTED_WINDOW (f);
7597 w = XWINDOW (window);
7598
7599 /* If the user has switched buffers or windows, we need to
7600 recompute to reflect the new bindings. But we'll
7601 recompute when update_mode_lines is set too; that means
7602 that people can use force-mode-line-update to request
7603 that the menu bar be recomputed. The adverse effect on
7604 the rest of the redisplay algorithm is about the same as
7605 windows_or_buffers_changed anyway. */
7606 if (windows_or_buffers_changed
7607 || !NILP (w->update_mode_line)
7608 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7609 < BUF_MODIFF (XBUFFER (w->buffer)))
7610 != !NILP (w->last_had_star))
7611 || ((!NILP (Vtransient_mark_mode)
7612 && !NILP (XBUFFER (w->buffer)->mark_active))
7613 != !NILP (w->region_showing)))
7614 {
7615 struct buffer *prev = current_buffer;
7616 int count = SPECPDL_INDEX ();
7617
7618 /* Set current_buffer to the buffer of the selected
7619 window of the frame, so that we get the right local
7620 keymaps. */
7621 set_buffer_internal_1 (XBUFFER (w->buffer));
7622
7623 /* Save match data, if we must. */
7624 if (save_match_data)
7625 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7626
7627 /* Make sure that we don't accidentally use bogus keymaps. */
7628 if (NILP (Voverriding_local_map_menu_flag))
7629 {
7630 specbind (Qoverriding_terminal_local_map, Qnil);
7631 specbind (Qoverriding_local_map, Qnil);
7632 }
7633
7634 /* Build desired tool-bar items from keymaps. */
7635 f->tool_bar_items
7636 = tool_bar_items (f->tool_bar_items, &f->n_tool_bar_items);
7637
7638 /* Redisplay the tool-bar in case we changed it. */
7639 w->update_mode_line = Qt;
7640
7641 unbind_to (count, Qnil);
7642 set_buffer_internal_1 (prev);
7643 }
7644 }
7645 }
7646
7647
7648 /* Set F->desired_tool_bar_string to a Lisp string representing frame
7649 F's desired tool-bar contents. F->tool_bar_items must have
7650 been set up previously by calling prepare_menu_bars. */
7651
7652 static void
7653 build_desired_tool_bar_string (f)
7654 struct frame *f;
7655 {
7656 int i, size, size_needed;
7657 struct gcpro gcpro1, gcpro2, gcpro3;
7658 Lisp_Object image, plist, props;
7659
7660 image = plist = props = Qnil;
7661 GCPRO3 (image, plist, props);
7662
7663 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
7664 Otherwise, make a new string. */
7665
7666 /* The size of the string we might be able to reuse. */
7667 size = (STRINGP (f->desired_tool_bar_string)
7668 ? SCHARS (f->desired_tool_bar_string)
7669 : 0);
7670
7671 /* We need one space in the string for each image. */
7672 size_needed = f->n_tool_bar_items;
7673
7674 /* Reuse f->desired_tool_bar_string, if possible. */
7675 if (size < size_needed || NILP (f->desired_tool_bar_string))
7676 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
7677 make_number (' '));
7678 else
7679 {
7680 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
7681 Fremove_text_properties (make_number (0), make_number (size),
7682 props, f->desired_tool_bar_string);
7683 }
7684
7685 /* Put a `display' property on the string for the images to display,
7686 put a `menu_item' property on tool-bar items with a value that
7687 is the index of the item in F's tool-bar item vector. */
7688 for (i = 0; i < f->n_tool_bar_items; ++i)
7689 {
7690 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
7691
7692 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
7693 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
7694 int hmargin, vmargin, relief, idx, end;
7695 extern Lisp_Object QCrelief, QCmargin, QCconversion, Qimage;
7696
7697 /* If image is a vector, choose the image according to the
7698 button state. */
7699 image = PROP (TOOL_BAR_ITEM_IMAGES);
7700 if (VECTORP (image))
7701 {
7702 if (enabled_p)
7703 idx = (selected_p
7704 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
7705 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
7706 else
7707 idx = (selected_p
7708 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
7709 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
7710
7711 xassert (ASIZE (image) >= idx);
7712 image = AREF (image, idx);
7713 }
7714 else
7715 idx = -1;
7716
7717 /* Ignore invalid image specifications. */
7718 if (!valid_image_p (image))
7719 continue;
7720
7721 /* Display the tool-bar button pressed, or depressed. */
7722 plist = Fcopy_sequence (XCDR (image));
7723
7724 /* Compute margin and relief to draw. */
7725 relief = (tool_bar_button_relief >= 0
7726 ? tool_bar_button_relief
7727 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
7728 hmargin = vmargin = relief;
7729
7730 if (INTEGERP (Vtool_bar_button_margin)
7731 && XINT (Vtool_bar_button_margin) > 0)
7732 {
7733 hmargin += XFASTINT (Vtool_bar_button_margin);
7734 vmargin += XFASTINT (Vtool_bar_button_margin);
7735 }
7736 else if (CONSP (Vtool_bar_button_margin))
7737 {
7738 if (INTEGERP (XCAR (Vtool_bar_button_margin))
7739 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
7740 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
7741
7742 if (INTEGERP (XCDR (Vtool_bar_button_margin))
7743 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
7744 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
7745 }
7746
7747 if (auto_raise_tool_bar_buttons_p)
7748 {
7749 /* Add a `:relief' property to the image spec if the item is
7750 selected. */
7751 if (selected_p)
7752 {
7753 plist = Fplist_put (plist, QCrelief, make_number (-relief));
7754 hmargin -= relief;
7755 vmargin -= relief;
7756 }
7757 }
7758 else
7759 {
7760 /* If image is selected, display it pressed, i.e. with a
7761 negative relief. If it's not selected, display it with a
7762 raised relief. */
7763 plist = Fplist_put (plist, QCrelief,
7764 (selected_p
7765 ? make_number (-relief)
7766 : make_number (relief)));
7767 hmargin -= relief;
7768 vmargin -= relief;
7769 }
7770
7771 /* Put a margin around the image. */
7772 if (hmargin || vmargin)
7773 {
7774 if (hmargin == vmargin)
7775 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
7776 else
7777 plist = Fplist_put (plist, QCmargin,
7778 Fcons (make_number (hmargin),
7779 make_number (vmargin)));
7780 }
7781
7782 /* If button is not enabled, and we don't have special images
7783 for the disabled state, make the image appear disabled by
7784 applying an appropriate algorithm to it. */
7785 if (!enabled_p && idx < 0)
7786 plist = Fplist_put (plist, QCconversion, Qdisabled);
7787
7788 /* Put a `display' text property on the string for the image to
7789 display. Put a `menu-item' property on the string that gives
7790 the start of this item's properties in the tool-bar items
7791 vector. */
7792 image = Fcons (Qimage, plist);
7793 props = list4 (Qdisplay, image,
7794 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
7795
7796 /* Let the last image hide all remaining spaces in the tool bar
7797 string. The string can be longer than needed when we reuse a
7798 previous string. */
7799 if (i + 1 == f->n_tool_bar_items)
7800 end = SCHARS (f->desired_tool_bar_string);
7801 else
7802 end = i + 1;
7803 Fadd_text_properties (make_number (i), make_number (end),
7804 props, f->desired_tool_bar_string);
7805 #undef PROP
7806 }
7807
7808 UNGCPRO;
7809 }
7810
7811
7812 /* Display one line of the tool-bar of frame IT->f. */
7813
7814 static void
7815 display_tool_bar_line (it)
7816 struct it *it;
7817 {
7818 struct glyph_row *row = it->glyph_row;
7819 int max_x = it->last_visible_x;
7820 struct glyph *last;
7821
7822 prepare_desired_row (row);
7823 row->y = it->current_y;
7824
7825 /* Note that this isn't made use of if the face hasn't a box,
7826 so there's no need to check the face here. */
7827 it->start_of_box_run_p = 1;
7828
7829 while (it->current_x < max_x)
7830 {
7831 int x_before, x, n_glyphs_before, i, nglyphs;
7832
7833 /* Get the next display element. */
7834 if (!get_next_display_element (it))
7835 break;
7836
7837 /* Produce glyphs. */
7838 x_before = it->current_x;
7839 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
7840 PRODUCE_GLYPHS (it);
7841
7842 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
7843 i = 0;
7844 x = x_before;
7845 while (i < nglyphs)
7846 {
7847 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
7848
7849 if (x + glyph->pixel_width > max_x)
7850 {
7851 /* Glyph doesn't fit on line. */
7852 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
7853 it->current_x = x;
7854 goto out;
7855 }
7856
7857 ++it->hpos;
7858 x += glyph->pixel_width;
7859 ++i;
7860 }
7861
7862 /* Stop at line ends. */
7863 if (ITERATOR_AT_END_OF_LINE_P (it))
7864 break;
7865
7866 set_iterator_to_next (it, 1);
7867 }
7868
7869 out:;
7870
7871 row->displays_text_p = row->used[TEXT_AREA] != 0;
7872 extend_face_to_end_of_line (it);
7873 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
7874 last->right_box_line_p = 1;
7875 if (last == row->glyphs[TEXT_AREA])
7876 last->left_box_line_p = 1;
7877 compute_line_metrics (it);
7878
7879 /* If line is empty, make it occupy the rest of the tool-bar. */
7880 if (!row->displays_text_p)
7881 {
7882 row->height = row->phys_height = it->last_visible_y - row->y;
7883 row->ascent = row->phys_ascent = 0;
7884 }
7885
7886 row->full_width_p = 1;
7887 row->continued_p = 0;
7888 row->truncated_on_left_p = 0;
7889 row->truncated_on_right_p = 0;
7890
7891 it->current_x = it->hpos = 0;
7892 it->current_y += row->height;
7893 ++it->vpos;
7894 ++it->glyph_row;
7895 }
7896
7897
7898 /* Value is the number of screen lines needed to make all tool-bar
7899 items of frame F visible. */
7900
7901 static int
7902 tool_bar_lines_needed (f)
7903 struct frame *f;
7904 {
7905 struct window *w = XWINDOW (f->tool_bar_window);
7906 struct it it;
7907
7908 /* Initialize an iterator for iteration over
7909 F->desired_tool_bar_string in the tool-bar window of frame F. */
7910 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7911 it.first_visible_x = 0;
7912 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7913 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7914
7915 while (!ITERATOR_AT_END_P (&it))
7916 {
7917 it.glyph_row = w->desired_matrix->rows;
7918 clear_glyph_row (it.glyph_row);
7919 display_tool_bar_line (&it);
7920 }
7921
7922 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
7923 }
7924
7925
7926 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
7927 0, 1, 0,
7928 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
7929 (frame)
7930 Lisp_Object frame;
7931 {
7932 struct frame *f;
7933 struct window *w;
7934 int nlines = 0;
7935
7936 if (NILP (frame))
7937 frame = selected_frame;
7938 else
7939 CHECK_FRAME (frame);
7940 f = XFRAME (frame);
7941
7942 if (WINDOWP (f->tool_bar_window)
7943 || (w = XWINDOW (f->tool_bar_window),
7944 XFASTINT (w->height) > 0))
7945 {
7946 update_tool_bar (f, 1);
7947 if (f->n_tool_bar_items)
7948 {
7949 build_desired_tool_bar_string (f);
7950 nlines = tool_bar_lines_needed (f);
7951 }
7952 }
7953
7954 return make_number (nlines);
7955 }
7956
7957
7958 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
7959 height should be changed. */
7960
7961 static int
7962 redisplay_tool_bar (f)
7963 struct frame *f;
7964 {
7965 struct window *w;
7966 struct it it;
7967 struct glyph_row *row;
7968 int change_height_p = 0;
7969
7970 /* If frame hasn't a tool-bar window or if it is zero-height, don't
7971 do anything. This means you must start with tool-bar-lines
7972 non-zero to get the auto-sizing effect. Or in other words, you
7973 can turn off tool-bars by specifying tool-bar-lines zero. */
7974 if (!WINDOWP (f->tool_bar_window)
7975 || (w = XWINDOW (f->tool_bar_window),
7976 XFASTINT (w->height) == 0))
7977 return 0;
7978
7979 /* Set up an iterator for the tool-bar window. */
7980 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7981 it.first_visible_x = 0;
7982 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7983 row = it.glyph_row;
7984
7985 /* Build a string that represents the contents of the tool-bar. */
7986 build_desired_tool_bar_string (f);
7987 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7988
7989 /* Display as many lines as needed to display all tool-bar items. */
7990 while (it.current_y < it.last_visible_y)
7991 display_tool_bar_line (&it);
7992
7993 /* It doesn't make much sense to try scrolling in the tool-bar
7994 window, so don't do it. */
7995 w->desired_matrix->no_scrolling_p = 1;
7996 w->must_be_updated_p = 1;
7997
7998 if (auto_resize_tool_bars_p)
7999 {
8000 int nlines;
8001
8002 /* If we couldn't display everything, change the tool-bar's
8003 height. */
8004 if (IT_STRING_CHARPOS (it) < it.end_charpos)
8005 change_height_p = 1;
8006
8007 /* If there are blank lines at the end, except for a partially
8008 visible blank line at the end that is smaller than
8009 CANON_Y_UNIT, change the tool-bar's height. */
8010 row = it.glyph_row - 1;
8011 if (!row->displays_text_p
8012 && row->height >= CANON_Y_UNIT (f))
8013 change_height_p = 1;
8014
8015 /* If row displays tool-bar items, but is partially visible,
8016 change the tool-bar's height. */
8017 if (row->displays_text_p
8018 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
8019 change_height_p = 1;
8020
8021 /* Resize windows as needed by changing the `tool-bar-lines'
8022 frame parameter. */
8023 if (change_height_p
8024 && (nlines = tool_bar_lines_needed (f),
8025 nlines != XFASTINT (w->height)))
8026 {
8027 extern Lisp_Object Qtool_bar_lines;
8028 Lisp_Object frame;
8029 int old_height = XFASTINT (w->height);
8030
8031 XSETFRAME (frame, f);
8032 clear_glyph_matrix (w->desired_matrix);
8033 Fmodify_frame_parameters (frame,
8034 Fcons (Fcons (Qtool_bar_lines,
8035 make_number (nlines)),
8036 Qnil));
8037 if (XFASTINT (w->height) != old_height)
8038 fonts_changed_p = 1;
8039 }
8040 }
8041
8042 return change_height_p;
8043 }
8044
8045
8046 /* Get information about the tool-bar item which is displayed in GLYPH
8047 on frame F. Return in *PROP_IDX the index where tool-bar item
8048 properties start in F->tool_bar_items. Value is zero if
8049 GLYPH doesn't display a tool-bar item. */
8050
8051 int
8052 tool_bar_item_info (f, glyph, prop_idx)
8053 struct frame *f;
8054 struct glyph *glyph;
8055 int *prop_idx;
8056 {
8057 Lisp_Object prop;
8058 int success_p;
8059 int charpos;
8060
8061 /* This function can be called asynchronously, which means we must
8062 exclude any possibility that Fget_text_property signals an
8063 error. */
8064 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
8065 charpos = max (0, charpos);
8066
8067 /* Get the text property `menu-item' at pos. The value of that
8068 property is the start index of this item's properties in
8069 F->tool_bar_items. */
8070 prop = Fget_text_property (make_number (charpos),
8071 Qmenu_item, f->current_tool_bar_string);
8072 if (INTEGERP (prop))
8073 {
8074 *prop_idx = XINT (prop);
8075 success_p = 1;
8076 }
8077 else
8078 success_p = 0;
8079
8080 return success_p;
8081 }
8082
8083 #endif /* HAVE_WINDOW_SYSTEM */
8084
8085
8086 \f
8087 /************************************************************************
8088 Horizontal scrolling
8089 ************************************************************************/
8090
8091 static int hscroll_window_tree P_ ((Lisp_Object));
8092 static int hscroll_windows P_ ((Lisp_Object));
8093
8094 /* For all leaf windows in the window tree rooted at WINDOW, set their
8095 hscroll value so that PT is (i) visible in the window, and (ii) so
8096 that it is not within a certain margin at the window's left and
8097 right border. Value is non-zero if any window's hscroll has been
8098 changed. */
8099
8100 static int
8101 hscroll_window_tree (window)
8102 Lisp_Object window;
8103 {
8104 int hscrolled_p = 0;
8105 int hscroll_relative_p = FLOATP (Vhscroll_step);
8106 int hscroll_step_abs = 0;
8107 double hscroll_step_rel = 0;
8108
8109 if (hscroll_relative_p)
8110 {
8111 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
8112 if (hscroll_step_rel < 0)
8113 {
8114 hscroll_relative_p = 0;
8115 hscroll_step_abs = 0;
8116 }
8117 }
8118 else if (INTEGERP (Vhscroll_step))
8119 {
8120 hscroll_step_abs = XINT (Vhscroll_step);
8121 if (hscroll_step_abs < 0)
8122 hscroll_step_abs = 0;
8123 }
8124 else
8125 hscroll_step_abs = 0;
8126
8127 while (WINDOWP (window))
8128 {
8129 struct window *w = XWINDOW (window);
8130
8131 if (WINDOWP (w->hchild))
8132 hscrolled_p |= hscroll_window_tree (w->hchild);
8133 else if (WINDOWP (w->vchild))
8134 hscrolled_p |= hscroll_window_tree (w->vchild);
8135 else if (w->cursor.vpos >= 0)
8136 {
8137 int h_margin, text_area_x, text_area_y;
8138 int text_area_width, text_area_height;
8139 struct glyph_row *current_cursor_row
8140 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
8141 struct glyph_row *desired_cursor_row
8142 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
8143 struct glyph_row *cursor_row
8144 = (desired_cursor_row->enabled_p
8145 ? desired_cursor_row
8146 : current_cursor_row);
8147
8148 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
8149 &text_area_width, &text_area_height);
8150
8151 /* Scroll when cursor is inside this scroll margin. */
8152 h_margin = hscroll_margin * CANON_X_UNIT (XFRAME (w->frame));
8153
8154 if ((XFASTINT (w->hscroll)
8155 && w->cursor.x <= h_margin)
8156 || (cursor_row->enabled_p
8157 && cursor_row->truncated_on_right_p
8158 && (w->cursor.x >= text_area_width - h_margin)))
8159 {
8160 struct it it;
8161 int hscroll;
8162 struct buffer *saved_current_buffer;
8163 int pt;
8164 int wanted_x;
8165
8166 /* Find point in a display of infinite width. */
8167 saved_current_buffer = current_buffer;
8168 current_buffer = XBUFFER (w->buffer);
8169
8170 if (w == XWINDOW (selected_window))
8171 pt = BUF_PT (current_buffer);
8172 else
8173 {
8174 pt = marker_position (w->pointm);
8175 pt = max (BEGV, pt);
8176 pt = min (ZV, pt);
8177 }
8178
8179 /* Move iterator to pt starting at cursor_row->start in
8180 a line with infinite width. */
8181 init_to_row_start (&it, w, cursor_row);
8182 it.last_visible_x = INFINITY;
8183 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
8184 current_buffer = saved_current_buffer;
8185
8186 /* Position cursor in window. */
8187 if (!hscroll_relative_p && hscroll_step_abs == 0)
8188 hscroll = max (0, it.current_x - text_area_width / 2)
8189 / CANON_X_UNIT (it.f);
8190 else if (w->cursor.x >= text_area_width - h_margin)
8191 {
8192 if (hscroll_relative_p)
8193 wanted_x = text_area_width * (1 - hscroll_step_rel)
8194 - h_margin;
8195 else
8196 wanted_x = text_area_width
8197 - hscroll_step_abs * CANON_X_UNIT (it.f)
8198 - h_margin;
8199 hscroll
8200 = max (0, it.current_x - wanted_x) / CANON_X_UNIT (it.f);
8201 }
8202 else
8203 {
8204 if (hscroll_relative_p)
8205 wanted_x = text_area_width * hscroll_step_rel
8206 + h_margin;
8207 else
8208 wanted_x = hscroll_step_abs * CANON_X_UNIT (it.f)
8209 + h_margin;
8210 hscroll
8211 = max (0, it.current_x - wanted_x) / CANON_X_UNIT (it.f);
8212 }
8213 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
8214
8215 /* Don't call Fset_window_hscroll if value hasn't
8216 changed because it will prevent redisplay
8217 optimizations. */
8218 if (XFASTINT (w->hscroll) != hscroll)
8219 {
8220 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
8221 w->hscroll = make_number (hscroll);
8222 hscrolled_p = 1;
8223 }
8224 }
8225 }
8226
8227 window = w->next;
8228 }
8229
8230 /* Value is non-zero if hscroll of any leaf window has been changed. */
8231 return hscrolled_p;
8232 }
8233
8234
8235 /* Set hscroll so that cursor is visible and not inside horizontal
8236 scroll margins for all windows in the tree rooted at WINDOW. See
8237 also hscroll_window_tree above. Value is non-zero if any window's
8238 hscroll has been changed. If it has, desired matrices on the frame
8239 of WINDOW are cleared. */
8240
8241 static int
8242 hscroll_windows (window)
8243 Lisp_Object window;
8244 {
8245 int hscrolled_p;
8246
8247 if (automatic_hscrolling_p)
8248 {
8249 hscrolled_p = hscroll_window_tree (window);
8250 if (hscrolled_p)
8251 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
8252 }
8253 else
8254 hscrolled_p = 0;
8255 return hscrolled_p;
8256 }
8257
8258
8259 \f
8260 /************************************************************************
8261 Redisplay
8262 ************************************************************************/
8263
8264 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
8265 to a non-zero value. This is sometimes handy to have in a debugger
8266 session. */
8267
8268 #if GLYPH_DEBUG
8269
8270 /* First and last unchanged row for try_window_id. */
8271
8272 int debug_first_unchanged_at_end_vpos;
8273 int debug_last_unchanged_at_beg_vpos;
8274
8275 /* Delta vpos and y. */
8276
8277 int debug_dvpos, debug_dy;
8278
8279 /* Delta in characters and bytes for try_window_id. */
8280
8281 int debug_delta, debug_delta_bytes;
8282
8283 /* Values of window_end_pos and window_end_vpos at the end of
8284 try_window_id. */
8285
8286 EMACS_INT debug_end_pos, debug_end_vpos;
8287
8288 /* Append a string to W->desired_matrix->method. FMT is a printf
8289 format string. A1...A9 are a supplement for a variable-length
8290 argument list. If trace_redisplay_p is non-zero also printf the
8291 resulting string to stderr. */
8292
8293 static void
8294 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
8295 struct window *w;
8296 char *fmt;
8297 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
8298 {
8299 char buffer[512];
8300 char *method = w->desired_matrix->method;
8301 int len = strlen (method);
8302 int size = sizeof w->desired_matrix->method;
8303 int remaining = size - len - 1;
8304
8305 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
8306 if (len && remaining)
8307 {
8308 method[len] = '|';
8309 --remaining, ++len;
8310 }
8311
8312 strncpy (method + len, buffer, remaining);
8313
8314 if (trace_redisplay_p)
8315 fprintf (stderr, "%p (%s): %s\n",
8316 w,
8317 ((BUFFERP (w->buffer)
8318 && STRINGP (XBUFFER (w->buffer)->name))
8319 ? (char *) SDATA (XBUFFER (w->buffer)->name)
8320 : "no buffer"),
8321 buffer);
8322 }
8323
8324 #endif /* GLYPH_DEBUG */
8325
8326
8327 /* Value is non-zero if all changes in window W, which displays
8328 current_buffer, are in the text between START and END. START is a
8329 buffer position, END is given as a distance from Z. Used in
8330 redisplay_internal for display optimization. */
8331
8332 static INLINE int
8333 text_outside_line_unchanged_p (w, start, end)
8334 struct window *w;
8335 int start, end;
8336 {
8337 int unchanged_p = 1;
8338
8339 /* If text or overlays have changed, see where. */
8340 if (XFASTINT (w->last_modified) < MODIFF
8341 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8342 {
8343 /* Gap in the line? */
8344 if (GPT < start || Z - GPT < end)
8345 unchanged_p = 0;
8346
8347 /* Changes start in front of the line, or end after it? */
8348 if (unchanged_p
8349 && (BEG_UNCHANGED < start - 1
8350 || END_UNCHANGED < end))
8351 unchanged_p = 0;
8352
8353 /* If selective display, can't optimize if changes start at the
8354 beginning of the line. */
8355 if (unchanged_p
8356 && INTEGERP (current_buffer->selective_display)
8357 && XINT (current_buffer->selective_display) > 0
8358 && (BEG_UNCHANGED < start || GPT <= start))
8359 unchanged_p = 0;
8360
8361 /* If there are overlays at the start or end of the line, these
8362 may have overlay strings with newlines in them. A change at
8363 START, for instance, may actually concern the display of such
8364 overlay strings as well, and they are displayed on different
8365 lines. So, quickly rule out this case. (For the future, it
8366 might be desirable to implement something more telling than
8367 just BEG/END_UNCHANGED.) */
8368 if (unchanged_p)
8369 {
8370 if (BEG + BEG_UNCHANGED == start
8371 && overlay_touches_p (start))
8372 unchanged_p = 0;
8373 if (END_UNCHANGED == end
8374 && overlay_touches_p (Z - end))
8375 unchanged_p = 0;
8376 }
8377 }
8378
8379 return unchanged_p;
8380 }
8381
8382
8383 /* Do a frame update, taking possible shortcuts into account. This is
8384 the main external entry point for redisplay.
8385
8386 If the last redisplay displayed an echo area message and that message
8387 is no longer requested, we clear the echo area or bring back the
8388 mini-buffer if that is in use. */
8389
8390 void
8391 redisplay ()
8392 {
8393 redisplay_internal (0);
8394 }
8395
8396
8397 /* Return 1 if point moved out of or into a composition. Otherwise
8398 return 0. PREV_BUF and PREV_PT are the last point buffer and
8399 position. BUF and PT are the current point buffer and position. */
8400
8401 int
8402 check_point_in_composition (prev_buf, prev_pt, buf, pt)
8403 struct buffer *prev_buf, *buf;
8404 int prev_pt, pt;
8405 {
8406 int start, end;
8407 Lisp_Object prop;
8408 Lisp_Object buffer;
8409
8410 XSETBUFFER (buffer, buf);
8411 /* Check a composition at the last point if point moved within the
8412 same buffer. */
8413 if (prev_buf == buf)
8414 {
8415 if (prev_pt == pt)
8416 /* Point didn't move. */
8417 return 0;
8418
8419 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
8420 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
8421 && COMPOSITION_VALID_P (start, end, prop)
8422 && start < prev_pt && end > prev_pt)
8423 /* The last point was within the composition. Return 1 iff
8424 point moved out of the composition. */
8425 return (pt <= start || pt >= end);
8426 }
8427
8428 /* Check a composition at the current point. */
8429 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
8430 && find_composition (pt, -1, &start, &end, &prop, buffer)
8431 && COMPOSITION_VALID_P (start, end, prop)
8432 && start < pt && end > pt);
8433 }
8434
8435
8436 /* Reconsider the setting of B->clip_changed which is displayed
8437 in window W. */
8438
8439 static INLINE void
8440 reconsider_clip_changes (w, b)
8441 struct window *w;
8442 struct buffer *b;
8443 {
8444 if (b->clip_changed
8445 && !NILP (w->window_end_valid)
8446 && w->current_matrix->buffer == b
8447 && w->current_matrix->zv == BUF_ZV (b)
8448 && w->current_matrix->begv == BUF_BEGV (b))
8449 b->clip_changed = 0;
8450
8451 /* If display wasn't paused, and W is not a tool bar window, see if
8452 point has been moved into or out of a composition. In that case,
8453 we set b->clip_changed to 1 to force updating the screen. If
8454 b->clip_changed has already been set to 1, we can skip this
8455 check. */
8456 if (!b->clip_changed
8457 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
8458 {
8459 int pt;
8460
8461 if (w == XWINDOW (selected_window))
8462 pt = BUF_PT (current_buffer);
8463 else
8464 pt = marker_position (w->pointm);
8465
8466 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
8467 || pt != XINT (w->last_point))
8468 && check_point_in_composition (w->current_matrix->buffer,
8469 XINT (w->last_point),
8470 XBUFFER (w->buffer), pt))
8471 b->clip_changed = 1;
8472 }
8473 }
8474 \f
8475 #define STOP_POLLING \
8476 do { if (! polling_stopped_here) stop_polling (); \
8477 polling_stopped_here = 1; } while (0)
8478
8479 #define RESUME_POLLING \
8480 do { if (polling_stopped_here) start_polling (); \
8481 polling_stopped_here = 0; } while (0)
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 int polling_stopped_here = 0;
8503
8504 /* Non-zero means redisplay has to consider all windows on all
8505 frames. Zero means, only selected_window is considered. */
8506 int consider_all_windows_p;
8507
8508 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
8509
8510 /* No redisplay if running in batch mode or frame is not yet fully
8511 initialized, or redisplay is explicitly turned off by setting
8512 Vinhibit_redisplay. */
8513 if (noninteractive
8514 || !NILP (Vinhibit_redisplay)
8515 || !f->glyphs_initialized_p)
8516 return;
8517
8518 /* The flag redisplay_performed_directly_p is set by
8519 direct_output_for_insert when it already did the whole screen
8520 update necessary. */
8521 if (redisplay_performed_directly_p)
8522 {
8523 redisplay_performed_directly_p = 0;
8524 if (!hscroll_windows (selected_window))
8525 return;
8526 }
8527
8528 #ifdef USE_X_TOOLKIT
8529 if (popup_activated ())
8530 return;
8531 #endif
8532
8533 /* I don't think this happens but let's be paranoid. */
8534 if (redisplaying_p)
8535 return;
8536
8537 /* Record a function that resets redisplaying_p to its old value
8538 when we leave this function. */
8539 count = SPECPDL_INDEX ();
8540 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
8541 ++redisplaying_p;
8542 specbind (Qinhibit_free_realized_faces, Qnil);
8543
8544 retry:
8545 pause = 0;
8546 reconsider_clip_changes (w, current_buffer);
8547
8548 /* If new fonts have been loaded that make a glyph matrix adjustment
8549 necessary, do it. */
8550 if (fonts_changed_p)
8551 {
8552 adjust_glyphs (NULL);
8553 ++windows_or_buffers_changed;
8554 fonts_changed_p = 0;
8555 }
8556
8557 /* If face_change_count is non-zero, init_iterator will free all
8558 realized faces, which includes the faces referenced from current
8559 matrices. So, we can't reuse current matrices in this case. */
8560 if (face_change_count)
8561 ++windows_or_buffers_changed;
8562
8563 if (! FRAME_WINDOW_P (sf)
8564 && previous_terminal_frame != sf)
8565 {
8566 /* Since frames on an ASCII terminal share the same display
8567 area, displaying a different frame means redisplay the whole
8568 thing. */
8569 windows_or_buffers_changed++;
8570 SET_FRAME_GARBAGED (sf);
8571 XSETFRAME (Vterminal_frame, sf);
8572 }
8573 previous_terminal_frame = sf;
8574
8575 /* Set the visible flags for all frames. Do this before checking
8576 for resized or garbaged frames; they want to know if their frames
8577 are visible. See the comment in frame.h for
8578 FRAME_SAMPLE_VISIBILITY. */
8579 {
8580 Lisp_Object tail, frame;
8581
8582 number_of_visible_frames = 0;
8583
8584 FOR_EACH_FRAME (tail, frame)
8585 {
8586 struct frame *f = XFRAME (frame);
8587
8588 FRAME_SAMPLE_VISIBILITY (f);
8589 if (FRAME_VISIBLE_P (f))
8590 ++number_of_visible_frames;
8591 clear_desired_matrices (f);
8592 }
8593 }
8594
8595 /* Notice any pending interrupt request to change frame size. */
8596 do_pending_window_change (1);
8597
8598 /* Clear frames marked as garbaged. */
8599 if (frame_garbaged)
8600 clear_garbaged_frames ();
8601
8602 /* Build menubar and tool-bar items. */
8603 prepare_menu_bars ();
8604
8605 if (windows_or_buffers_changed)
8606 update_mode_lines++;
8607
8608 /* Detect case that we need to write or remove a star in the mode line. */
8609 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
8610 {
8611 w->update_mode_line = Qt;
8612 if (buffer_shared > 1)
8613 update_mode_lines++;
8614 }
8615
8616 /* If %c is in the mode line, update it if needed. */
8617 if (!NILP (w->column_number_displayed)
8618 /* This alternative quickly identifies a common case
8619 where no change is needed. */
8620 && !(PT == XFASTINT (w->last_point)
8621 && XFASTINT (w->last_modified) >= MODIFF
8622 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
8623 && (XFASTINT (w->column_number_displayed)
8624 != (int) current_column ())) /* iftc */
8625 w->update_mode_line = Qt;
8626
8627 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
8628
8629 /* The variable buffer_shared is set in redisplay_window and
8630 indicates that we redisplay a buffer in different windows. See
8631 there. */
8632 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
8633 || cursor_type_changed);
8634
8635 /* If specs for an arrow have changed, do thorough redisplay
8636 to ensure we remove any arrow that should no longer exist. */
8637 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
8638 || ! EQ (Voverlay_arrow_string, last_arrow_string))
8639 consider_all_windows_p = windows_or_buffers_changed = 1;
8640
8641 /* Normally the message* functions will have already displayed and
8642 updated the echo area, but the frame may have been trashed, or
8643 the update may have been preempted, so display the echo area
8644 again here. Checking message_cleared_p captures the case that
8645 the echo area should be cleared. */
8646 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
8647 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
8648 || (message_cleared_p
8649 && minibuf_level == 0
8650 /* If the mini-window is currently selected, this means the
8651 echo-area doesn't show through. */
8652 && !MINI_WINDOW_P (XWINDOW (selected_window))))
8653 {
8654 int window_height_changed_p = echo_area_display (0);
8655 must_finish = 1;
8656
8657 /* If we don't display the current message, don't clear the
8658 message_cleared_p flag, because, if we did, we wouldn't clear
8659 the echo area in the next redisplay which doesn't preserve
8660 the echo area. */
8661 if (!display_last_displayed_message_p)
8662 message_cleared_p = 0;
8663
8664 if (fonts_changed_p)
8665 goto retry;
8666 else if (window_height_changed_p)
8667 {
8668 consider_all_windows_p = 1;
8669 ++update_mode_lines;
8670 ++windows_or_buffers_changed;
8671
8672 /* If window configuration was changed, frames may have been
8673 marked garbaged. Clear them or we will experience
8674 surprises wrt scrolling. */
8675 if (frame_garbaged)
8676 clear_garbaged_frames ();
8677 }
8678 }
8679 else if (EQ (selected_window, minibuf_window)
8680 && (current_buffer->clip_changed
8681 || XFASTINT (w->last_modified) < MODIFF
8682 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8683 && resize_mini_window (w, 0))
8684 {
8685 /* Resized active mini-window to fit the size of what it is
8686 showing if its contents might have changed. */
8687 must_finish = 1;
8688 consider_all_windows_p = 1;
8689 ++windows_or_buffers_changed;
8690 ++update_mode_lines;
8691
8692 /* If window configuration was changed, frames may have been
8693 marked garbaged. Clear them or we will experience
8694 surprises wrt scrolling. */
8695 if (frame_garbaged)
8696 clear_garbaged_frames ();
8697 }
8698
8699
8700 /* If showing the region, and mark has changed, we must redisplay
8701 the whole window. The assignment to this_line_start_pos prevents
8702 the optimization directly below this if-statement. */
8703 if (((!NILP (Vtransient_mark_mode)
8704 && !NILP (XBUFFER (w->buffer)->mark_active))
8705 != !NILP (w->region_showing))
8706 || (!NILP (w->region_showing)
8707 && !EQ (w->region_showing,
8708 Fmarker_position (XBUFFER (w->buffer)->mark))))
8709 CHARPOS (this_line_start_pos) = 0;
8710
8711 /* Optimize the case that only the line containing the cursor in the
8712 selected window has changed. Variables starting with this_ are
8713 set in display_line and record information about the line
8714 containing the cursor. */
8715 tlbufpos = this_line_start_pos;
8716 tlendpos = this_line_end_pos;
8717 if (!consider_all_windows_p
8718 && CHARPOS (tlbufpos) > 0
8719 && NILP (w->update_mode_line)
8720 && !current_buffer->clip_changed
8721 && !current_buffer->prevent_redisplay_optimizations_p
8722 && FRAME_VISIBLE_P (XFRAME (w->frame))
8723 && !FRAME_OBSCURED_P (XFRAME (w->frame))
8724 /* Make sure recorded data applies to current buffer, etc. */
8725 && this_line_buffer == current_buffer
8726 && current_buffer == XBUFFER (w->buffer)
8727 && NILP (w->force_start)
8728 && NILP (w->optional_new_start)
8729 /* Point must be on the line that we have info recorded about. */
8730 && PT >= CHARPOS (tlbufpos)
8731 && PT <= Z - CHARPOS (tlendpos)
8732 /* All text outside that line, including its final newline,
8733 must be unchanged */
8734 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
8735 CHARPOS (tlendpos)))
8736 {
8737 if (CHARPOS (tlbufpos) > BEGV
8738 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
8739 && (CHARPOS (tlbufpos) == ZV
8740 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
8741 /* Former continuation line has disappeared by becoming empty */
8742 goto cancel;
8743 else if (XFASTINT (w->last_modified) < MODIFF
8744 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
8745 || MINI_WINDOW_P (w))
8746 {
8747 /* We have to handle the case of continuation around a
8748 wide-column character (See the comment in indent.c around
8749 line 885).
8750
8751 For instance, in the following case:
8752
8753 -------- Insert --------
8754 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
8755 J_I_ ==> J_I_ `^^' are cursors.
8756 ^^ ^^
8757 -------- --------
8758
8759 As we have to redraw the line above, we should goto cancel. */
8760
8761 struct it it;
8762 int line_height_before = this_line_pixel_height;
8763
8764 /* Note that start_display will handle the case that the
8765 line starting at tlbufpos is a continuation lines. */
8766 start_display (&it, w, tlbufpos);
8767
8768 /* Implementation note: It this still necessary? */
8769 if (it.current_x != this_line_start_x)
8770 goto cancel;
8771
8772 TRACE ((stderr, "trying display optimization 1\n"));
8773 w->cursor.vpos = -1;
8774 overlay_arrow_seen = 0;
8775 it.vpos = this_line_vpos;
8776 it.current_y = this_line_y;
8777 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
8778 display_line (&it);
8779
8780 /* If line contains point, is not continued,
8781 and ends at same distance from eob as before, we win */
8782 if (w->cursor.vpos >= 0
8783 /* Line is not continued, otherwise this_line_start_pos
8784 would have been set to 0 in display_line. */
8785 && CHARPOS (this_line_start_pos)
8786 /* Line ends as before. */
8787 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
8788 /* Line has same height as before. Otherwise other lines
8789 would have to be shifted up or down. */
8790 && this_line_pixel_height == line_height_before)
8791 {
8792 /* If this is not the window's last line, we must adjust
8793 the charstarts of the lines below. */
8794 if (it.current_y < it.last_visible_y)
8795 {
8796 struct glyph_row *row
8797 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
8798 int delta, delta_bytes;
8799
8800 if (Z - CHARPOS (tlendpos) == ZV)
8801 {
8802 /* This line ends at end of (accessible part of)
8803 buffer. There is no newline to count. */
8804 delta = (Z
8805 - CHARPOS (tlendpos)
8806 - MATRIX_ROW_START_CHARPOS (row));
8807 delta_bytes = (Z_BYTE
8808 - BYTEPOS (tlendpos)
8809 - MATRIX_ROW_START_BYTEPOS (row));
8810 }
8811 else
8812 {
8813 /* This line ends in a newline. Must take
8814 account of the newline and the rest of the
8815 text that follows. */
8816 delta = (Z
8817 - CHARPOS (tlendpos)
8818 - MATRIX_ROW_START_CHARPOS (row));
8819 delta_bytes = (Z_BYTE
8820 - BYTEPOS (tlendpos)
8821 - MATRIX_ROW_START_BYTEPOS (row));
8822 }
8823
8824 increment_matrix_positions (w->current_matrix,
8825 this_line_vpos + 1,
8826 w->current_matrix->nrows,
8827 delta, delta_bytes);
8828 }
8829
8830 /* If this row displays text now but previously didn't,
8831 or vice versa, w->window_end_vpos may have to be
8832 adjusted. */
8833 if ((it.glyph_row - 1)->displays_text_p)
8834 {
8835 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
8836 XSETINT (w->window_end_vpos, this_line_vpos);
8837 }
8838 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
8839 && this_line_vpos > 0)
8840 XSETINT (w->window_end_vpos, this_line_vpos - 1);
8841 w->window_end_valid = Qnil;
8842
8843 /* Update hint: No need to try to scroll in update_window. */
8844 w->desired_matrix->no_scrolling_p = 1;
8845
8846 #if GLYPH_DEBUG
8847 *w->desired_matrix->method = 0;
8848 debug_method_add (w, "optimization 1");
8849 #endif
8850 goto update;
8851 }
8852 else
8853 goto cancel;
8854 }
8855 else if (/* Cursor position hasn't changed. */
8856 PT == XFASTINT (w->last_point)
8857 /* Make sure the cursor was last displayed
8858 in this window. Otherwise we have to reposition it. */
8859 && 0 <= w->cursor.vpos
8860 && XINT (w->height) > w->cursor.vpos)
8861 {
8862 if (!must_finish)
8863 {
8864 do_pending_window_change (1);
8865
8866 /* We used to always goto end_of_redisplay here, but this
8867 isn't enough if we have a blinking cursor. */
8868 if (w->cursor_off_p == w->last_cursor_off_p)
8869 goto end_of_redisplay;
8870 }
8871 goto update;
8872 }
8873 /* If highlighting the region, or if the cursor is in the echo area,
8874 then we can't just move the cursor. */
8875 else if (! (!NILP (Vtransient_mark_mode)
8876 && !NILP (current_buffer->mark_active))
8877 && (EQ (selected_window, current_buffer->last_selected_window)
8878 || highlight_nonselected_windows)
8879 && NILP (w->region_showing)
8880 && NILP (Vshow_trailing_whitespace)
8881 && !cursor_in_echo_area)
8882 {
8883 struct it it;
8884 struct glyph_row *row;
8885
8886 /* Skip from tlbufpos to PT and see where it is. Note that
8887 PT may be in invisible text. If so, we will end at the
8888 next visible position. */
8889 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
8890 NULL, DEFAULT_FACE_ID);
8891 it.current_x = this_line_start_x;
8892 it.current_y = this_line_y;
8893 it.vpos = this_line_vpos;
8894
8895 /* The call to move_it_to stops in front of PT, but
8896 moves over before-strings. */
8897 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
8898
8899 if (it.vpos == this_line_vpos
8900 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
8901 row->enabled_p))
8902 {
8903 xassert (this_line_vpos == it.vpos);
8904 xassert (this_line_y == it.current_y);
8905 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
8906 #if GLYPH_DEBUG
8907 *w->desired_matrix->method = 0;
8908 debug_method_add (w, "optimization 3");
8909 #endif
8910 goto update;
8911 }
8912 else
8913 goto cancel;
8914 }
8915
8916 cancel:
8917 /* Text changed drastically or point moved off of line. */
8918 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
8919 }
8920
8921 CHARPOS (this_line_start_pos) = 0;
8922 consider_all_windows_p |= buffer_shared > 1;
8923 ++clear_face_cache_count;
8924
8925
8926 /* Build desired matrices, and update the display. If
8927 consider_all_windows_p is non-zero, do it for all windows on all
8928 frames. Otherwise do it for selected_window, only. */
8929
8930 if (consider_all_windows_p)
8931 {
8932 Lisp_Object tail, frame;
8933 int i, n = 0, size = 50;
8934 struct frame **updated
8935 = (struct frame **) alloca (size * sizeof *updated);
8936
8937 /* Clear the face cache eventually. */
8938 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
8939 {
8940 clear_face_cache (0);
8941 clear_face_cache_count = 0;
8942 }
8943
8944 /* Recompute # windows showing selected buffer. This will be
8945 incremented each time such a window is displayed. */
8946 buffer_shared = 0;
8947
8948 FOR_EACH_FRAME (tail, frame)
8949 {
8950 struct frame *f = XFRAME (frame);
8951
8952 if (FRAME_WINDOW_P (f) || f == sf)
8953 {
8954 #ifdef HAVE_WINDOW_SYSTEM
8955 if (clear_face_cache_count % 50 == 0
8956 && FRAME_WINDOW_P (f))
8957 clear_image_cache (f, 0);
8958 #endif /* HAVE_WINDOW_SYSTEM */
8959
8960 /* Mark all the scroll bars to be removed; we'll redeem
8961 the ones we want when we redisplay their windows. */
8962 if (condemn_scroll_bars_hook)
8963 condemn_scroll_bars_hook (f);
8964
8965 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8966 redisplay_windows (FRAME_ROOT_WINDOW (f));
8967
8968 /* Any scroll bars which redisplay_windows should have
8969 nuked should now go away. */
8970 if (judge_scroll_bars_hook)
8971 judge_scroll_bars_hook (f);
8972
8973 /* If fonts changed, display again. */
8974 /* ??? rms: I suspect it is a mistake to jump all the way
8975 back to retry here. It should just retry this frame. */
8976 if (fonts_changed_p)
8977 goto retry;
8978
8979 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8980 {
8981 /* See if we have to hscroll. */
8982 if (hscroll_windows (f->root_window))
8983 goto retry;
8984
8985 /* Prevent various kinds of signals during display
8986 update. stdio is not robust about handling
8987 signals, which can cause an apparent I/O
8988 error. */
8989 if (interrupt_input)
8990 unrequest_sigio ();
8991 STOP_POLLING;
8992
8993 /* Update the display. */
8994 set_window_update_flags (XWINDOW (f->root_window), 1);
8995 pause |= update_frame (f, 0, 0);
8996 if (pause)
8997 break;
8998
8999 if (n == size)
9000 {
9001 int nbytes = size * sizeof *updated;
9002 struct frame **p = (struct frame **) alloca (2 * nbytes);
9003 bcopy (updated, p, nbytes);
9004 size *= 2;
9005 }
9006
9007 updated[n++] = f;
9008 }
9009 }
9010 }
9011
9012 /* Do the mark_window_display_accurate after all windows have
9013 been redisplayed because this call resets flags in buffers
9014 which are needed for proper redisplay. */
9015 for (i = 0; i < n; ++i)
9016 {
9017 struct frame *f = updated[i];
9018 mark_window_display_accurate (f->root_window, 1);
9019 if (frame_up_to_date_hook)
9020 frame_up_to_date_hook (f);
9021 }
9022 }
9023 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
9024 {
9025 Lisp_Object mini_window;
9026 struct frame *mini_frame;
9027
9028 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
9029 /* Use list_of_error, not Qerror, so that
9030 we catch only errors and don't run the debugger. */
9031 internal_condition_case_1 (redisplay_window_1, selected_window,
9032 list_of_error,
9033 redisplay_window_error);
9034
9035 /* Compare desired and current matrices, perform output. */
9036
9037 update:
9038 /* If fonts changed, display again. */
9039 if (fonts_changed_p)
9040 goto retry;
9041
9042 /* Prevent various kinds of signals during display update.
9043 stdio is not robust about handling signals,
9044 which can cause an apparent I/O error. */
9045 if (interrupt_input)
9046 unrequest_sigio ();
9047 STOP_POLLING;
9048
9049 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
9050 {
9051 if (hscroll_windows (selected_window))
9052 goto retry;
9053
9054 XWINDOW (selected_window)->must_be_updated_p = 1;
9055 pause = update_frame (sf, 0, 0);
9056 }
9057
9058 /* We may have called echo_area_display at the top of this
9059 function. If the echo area is on another frame, that may
9060 have put text on a frame other than the selected one, so the
9061 above call to update_frame would not have caught it. Catch
9062 it here. */
9063 mini_window = FRAME_MINIBUF_WINDOW (sf);
9064 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9065
9066 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
9067 {
9068 XWINDOW (mini_window)->must_be_updated_p = 1;
9069 pause |= update_frame (mini_frame, 0, 0);
9070 if (!pause && hscroll_windows (mini_window))
9071 goto retry;
9072 }
9073 }
9074
9075 /* If display was paused because of pending input, make sure we do a
9076 thorough update the next time. */
9077 if (pause)
9078 {
9079 /* Prevent the optimization at the beginning of
9080 redisplay_internal that tries a single-line update of the
9081 line containing the cursor in the selected window. */
9082 CHARPOS (this_line_start_pos) = 0;
9083
9084 /* Let the overlay arrow be updated the next time. */
9085 if (!NILP (last_arrow_position))
9086 {
9087 last_arrow_position = Qt;
9088 last_arrow_string = Qt;
9089 }
9090
9091 /* If we pause after scrolling, some rows in the current
9092 matrices of some windows are not valid. */
9093 if (!WINDOW_FULL_WIDTH_P (w)
9094 && !FRAME_WINDOW_P (XFRAME (w->frame)))
9095 update_mode_lines = 1;
9096 }
9097 else
9098 {
9099 if (!consider_all_windows_p)
9100 {
9101 /* This has already been done above if
9102 consider_all_windows_p is set. */
9103 mark_window_display_accurate_1 (w, 1);
9104
9105 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
9106 last_arrow_string = Voverlay_arrow_string;
9107
9108 if (frame_up_to_date_hook != 0)
9109 frame_up_to_date_hook (sf);
9110 }
9111
9112 update_mode_lines = 0;
9113 windows_or_buffers_changed = 0;
9114 cursor_type_changed = 0;
9115 }
9116
9117 /* Start SIGIO interrupts coming again. Having them off during the
9118 code above makes it less likely one will discard output, but not
9119 impossible, since there might be stuff in the system buffer here.
9120 But it is much hairier to try to do anything about that. */
9121 if (interrupt_input)
9122 request_sigio ();
9123 RESUME_POLLING;
9124
9125 /* If a frame has become visible which was not before, redisplay
9126 again, so that we display it. Expose events for such a frame
9127 (which it gets when becoming visible) don't call the parts of
9128 redisplay constructing glyphs, so simply exposing a frame won't
9129 display anything in this case. So, we have to display these
9130 frames here explicitly. */
9131 if (!pause)
9132 {
9133 Lisp_Object tail, frame;
9134 int new_count = 0;
9135
9136 FOR_EACH_FRAME (tail, frame)
9137 {
9138 int this_is_visible = 0;
9139
9140 if (XFRAME (frame)->visible)
9141 this_is_visible = 1;
9142 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
9143 if (XFRAME (frame)->visible)
9144 this_is_visible = 1;
9145
9146 if (this_is_visible)
9147 new_count++;
9148 }
9149
9150 if (new_count != number_of_visible_frames)
9151 windows_or_buffers_changed++;
9152 }
9153
9154 /* Change frame size now if a change is pending. */
9155 do_pending_window_change (1);
9156
9157 /* If we just did a pending size change, or have additional
9158 visible frames, redisplay again. */
9159 if (windows_or_buffers_changed && !pause)
9160 goto retry;
9161
9162 end_of_redisplay:
9163 unbind_to (count, Qnil);
9164 RESUME_POLLING;
9165 }
9166
9167
9168 /* Redisplay, but leave alone any recent echo area message unless
9169 another message has been requested in its place.
9170
9171 This is useful in situations where you need to redisplay but no
9172 user action has occurred, making it inappropriate for the message
9173 area to be cleared. See tracking_off and
9174 wait_reading_process_input for examples of these situations.
9175
9176 FROM_WHERE is an integer saying from where this function was
9177 called. This is useful for debugging. */
9178
9179 void
9180 redisplay_preserve_echo_area (from_where)
9181 int from_where;
9182 {
9183 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
9184
9185 if (!NILP (echo_area_buffer[1]))
9186 {
9187 /* We have a previously displayed message, but no current
9188 message. Redisplay the previous message. */
9189 display_last_displayed_message_p = 1;
9190 redisplay_internal (1);
9191 display_last_displayed_message_p = 0;
9192 }
9193 else
9194 redisplay_internal (1);
9195 }
9196
9197
9198 /* Function registered with record_unwind_protect in
9199 redisplay_internal. Reset redisplaying_p to the value it had
9200 before redisplay_internal was called, and clear
9201 prevent_freeing_realized_faces_p. */
9202
9203 static Lisp_Object
9204 unwind_redisplay (old_redisplaying_p)
9205 Lisp_Object old_redisplaying_p;
9206 {
9207 redisplaying_p = XFASTINT (old_redisplaying_p);
9208 return Qnil;
9209 }
9210
9211
9212 /* Mark the display of window W as accurate or inaccurate. If
9213 ACCURATE_P is non-zero mark display of W as accurate. If
9214 ACCURATE_P is zero, arrange for W to be redisplayed the next time
9215 redisplay_internal is called. */
9216
9217 static void
9218 mark_window_display_accurate_1 (w, accurate_p)
9219 struct window *w;
9220 int accurate_p;
9221 {
9222 if (BUFFERP (w->buffer))
9223 {
9224 struct buffer *b = XBUFFER (w->buffer);
9225
9226 w->last_modified
9227 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
9228 w->last_overlay_modified
9229 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
9230 w->last_had_star
9231 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
9232
9233 if (accurate_p)
9234 {
9235 b->clip_changed = 0;
9236 b->prevent_redisplay_optimizations_p = 0;
9237
9238 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
9239 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
9240 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
9241 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
9242
9243 w->current_matrix->buffer = b;
9244 w->current_matrix->begv = BUF_BEGV (b);
9245 w->current_matrix->zv = BUF_ZV (b);
9246
9247 w->last_cursor = w->cursor;
9248 w->last_cursor_off_p = w->cursor_off_p;
9249
9250 if (w == XWINDOW (selected_window))
9251 w->last_point = make_number (BUF_PT (b));
9252 else
9253 w->last_point = make_number (XMARKER (w->pointm)->charpos);
9254 }
9255 }
9256
9257 if (accurate_p)
9258 {
9259 w->window_end_valid = w->buffer;
9260 #if 0 /* This is incorrect with variable-height lines. */
9261 xassert (XINT (w->window_end_vpos)
9262 < (XINT (w->height)
9263 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
9264 #endif
9265 w->update_mode_line = Qnil;
9266 }
9267 }
9268
9269
9270 /* Mark the display of windows in the window tree rooted at WINDOW as
9271 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
9272 windows as accurate. If ACCURATE_P is zero, arrange for windows to
9273 be redisplayed the next time redisplay_internal is called. */
9274
9275 void
9276 mark_window_display_accurate (window, accurate_p)
9277 Lisp_Object window;
9278 int accurate_p;
9279 {
9280 struct window *w;
9281
9282 for (; !NILP (window); window = w->next)
9283 {
9284 w = XWINDOW (window);
9285 mark_window_display_accurate_1 (w, accurate_p);
9286
9287 if (!NILP (w->vchild))
9288 mark_window_display_accurate (w->vchild, accurate_p);
9289 if (!NILP (w->hchild))
9290 mark_window_display_accurate (w->hchild, accurate_p);
9291 }
9292
9293 if (accurate_p)
9294 {
9295 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
9296 last_arrow_string = Voverlay_arrow_string;
9297 }
9298 else
9299 {
9300 /* Force a thorough redisplay the next time by setting
9301 last_arrow_position and last_arrow_string to t, which is
9302 unequal to any useful value of Voverlay_arrow_... */
9303 last_arrow_position = Qt;
9304 last_arrow_string = Qt;
9305 }
9306 }
9307
9308
9309 /* Return value in display table DP (Lisp_Char_Table *) for character
9310 C. Since a display table doesn't have any parent, we don't have to
9311 follow parent. Do not call this function directly but use the
9312 macro DISP_CHAR_VECTOR. */
9313
9314 Lisp_Object
9315 disp_char_vector (dp, c)
9316 struct Lisp_Char_Table *dp;
9317 int c;
9318 {
9319 int code[4], i;
9320 Lisp_Object val;
9321
9322 if (SINGLE_BYTE_CHAR_P (c))
9323 return (dp->contents[c]);
9324
9325 SPLIT_CHAR (c, code[0], code[1], code[2]);
9326 if (code[1] < 32)
9327 code[1] = -1;
9328 else if (code[2] < 32)
9329 code[2] = -1;
9330
9331 /* Here, the possible range of code[0] (== charset ID) is
9332 128..max_charset. Since the top level char table contains data
9333 for multibyte characters after 256th element, we must increment
9334 code[0] by 128 to get a correct index. */
9335 code[0] += 128;
9336 code[3] = -1; /* anchor */
9337
9338 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
9339 {
9340 val = dp->contents[code[i]];
9341 if (!SUB_CHAR_TABLE_P (val))
9342 return (NILP (val) ? dp->defalt : val);
9343 }
9344
9345 /* Here, val is a sub char table. We return the default value of
9346 it. */
9347 return (dp->defalt);
9348 }
9349
9350
9351 \f
9352 /***********************************************************************
9353 Window Redisplay
9354 ***********************************************************************/
9355
9356 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
9357
9358 static void
9359 redisplay_windows (window)
9360 Lisp_Object window;
9361 {
9362 while (!NILP (window))
9363 {
9364 struct window *w = XWINDOW (window);
9365
9366 if (!NILP (w->hchild))
9367 redisplay_windows (w->hchild);
9368 else if (!NILP (w->vchild))
9369 redisplay_windows (w->vchild);
9370 else
9371 {
9372 displayed_buffer = XBUFFER (w->buffer);
9373 /* Use list_of_error, not Qerror, so that
9374 we catch only errors and don't run the debugger. */
9375 internal_condition_case_1 (redisplay_window_0, window,
9376 list_of_error,
9377 redisplay_window_error);
9378 }
9379
9380 window = w->next;
9381 }
9382 }
9383
9384 static Lisp_Object
9385 redisplay_window_error ()
9386 {
9387 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
9388 return Qnil;
9389 }
9390
9391 static Lisp_Object
9392 redisplay_window_0 (window)
9393 Lisp_Object window;
9394 {
9395 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
9396 redisplay_window (window, 0);
9397 return Qnil;
9398 }
9399
9400 static Lisp_Object
9401 redisplay_window_1 (window)
9402 Lisp_Object window;
9403 {
9404 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
9405 redisplay_window (window, 1);
9406 return Qnil;
9407 }
9408 \f
9409 /* Set cursor position of W. PT is assumed to be displayed in ROW.
9410 DELTA is the number of bytes by which positions recorded in ROW
9411 differ from current buffer positions. */
9412
9413 void
9414 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
9415 struct window *w;
9416 struct glyph_row *row;
9417 struct glyph_matrix *matrix;
9418 int delta, delta_bytes, dy, dvpos;
9419 {
9420 struct glyph *glyph = row->glyphs[TEXT_AREA];
9421 struct glyph *end = glyph + row->used[TEXT_AREA];
9422 int x = row->x;
9423 int pt_old = PT - delta;
9424
9425 /* Skip over glyphs not having an object at the start of the row.
9426 These are special glyphs like truncation marks on terminal
9427 frames. */
9428 if (row->displays_text_p)
9429 while (glyph < end
9430 && INTEGERP (glyph->object)
9431 && glyph->charpos < 0)
9432 {
9433 x += glyph->pixel_width;
9434 ++glyph;
9435 }
9436
9437 while (glyph < end
9438 && !INTEGERP (glyph->object)
9439 && (!BUFFERP (glyph->object)
9440 || glyph->charpos < pt_old))
9441 {
9442 x += glyph->pixel_width;
9443 ++glyph;
9444 }
9445
9446 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
9447 w->cursor.x = x;
9448 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
9449 w->cursor.y = row->y + dy;
9450
9451 if (w == XWINDOW (selected_window))
9452 {
9453 if (!row->continued_p
9454 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
9455 && row->x == 0)
9456 {
9457 this_line_buffer = XBUFFER (w->buffer);
9458
9459 CHARPOS (this_line_start_pos)
9460 = MATRIX_ROW_START_CHARPOS (row) + delta;
9461 BYTEPOS (this_line_start_pos)
9462 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
9463
9464 CHARPOS (this_line_end_pos)
9465 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
9466 BYTEPOS (this_line_end_pos)
9467 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
9468
9469 this_line_y = w->cursor.y;
9470 this_line_pixel_height = row->height;
9471 this_line_vpos = w->cursor.vpos;
9472 this_line_start_x = row->x;
9473 }
9474 else
9475 CHARPOS (this_line_start_pos) = 0;
9476 }
9477 }
9478
9479
9480 /* Run window scroll functions, if any, for WINDOW with new window
9481 start STARTP. Sets the window start of WINDOW to that position.
9482
9483 We assume that the window's buffer is really current. */
9484
9485 static INLINE struct text_pos
9486 run_window_scroll_functions (window, startp)
9487 Lisp_Object window;
9488 struct text_pos startp;
9489 {
9490 struct window *w = XWINDOW (window);
9491 SET_MARKER_FROM_TEXT_POS (w->start, startp);
9492
9493 if (current_buffer != XBUFFER (w->buffer))
9494 abort ();
9495
9496 if (!NILP (Vwindow_scroll_functions))
9497 {
9498 run_hook_with_args_2 (Qwindow_scroll_functions, window,
9499 make_number (CHARPOS (startp)));
9500 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9501 /* In case the hook functions switch buffers. */
9502 if (current_buffer != XBUFFER (w->buffer))
9503 set_buffer_internal_1 (XBUFFER (w->buffer));
9504 }
9505
9506 return startp;
9507 }
9508
9509
9510 /* Make sure the line containing the cursor is fully visible.
9511 A value of 1 means there is nothing to be done.
9512 (Either the line is fully visible, or it cannot be made so,
9513 or we cannot tell.)
9514 A value of 0 means the caller should do scrolling
9515 as if point had gone off the screen. */
9516
9517 static int
9518 make_cursor_line_fully_visible (w)
9519 struct window *w;
9520 {
9521 struct glyph_matrix *matrix;
9522 struct glyph_row *row;
9523 int window_height;
9524
9525 /* It's not always possible to find the cursor, e.g, when a window
9526 is full of overlay strings. Don't do anything in that case. */
9527 if (w->cursor.vpos < 0)
9528 return 1;
9529
9530 matrix = w->desired_matrix;
9531 row = MATRIX_ROW (matrix, w->cursor.vpos);
9532
9533 /* If the cursor row is not partially visible, there's nothing to do. */
9534 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
9535 return 1;
9536
9537 /* If the row the cursor is in is taller than the window's height,
9538 it's not clear what to do, so do nothing. */
9539 window_height = window_box_height (w);
9540 if (row->height >= window_height)
9541 return 1;
9542
9543 return 0;
9544
9545 #if 0
9546 /* This code used to try to scroll the window just enough to make
9547 the line visible. It returned 0 to say that the caller should
9548 allocate larger glyph matrices. */
9549
9550 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
9551 {
9552 int dy = row->height - row->visible_height;
9553 w->vscroll = 0;
9554 w->cursor.y += dy;
9555 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9556 }
9557 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
9558 {
9559 int dy = - (row->height - row->visible_height);
9560 w->vscroll = dy;
9561 w->cursor.y += dy;
9562 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9563 }
9564
9565 /* When we change the cursor y-position of the selected window,
9566 change this_line_y as well so that the display optimization for
9567 the cursor line of the selected window in redisplay_internal uses
9568 the correct y-position. */
9569 if (w == XWINDOW (selected_window))
9570 this_line_y = w->cursor.y;
9571
9572 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
9573 redisplay with larger matrices. */
9574 if (matrix->nrows < required_matrix_height (w))
9575 {
9576 fonts_changed_p = 1;
9577 return 0;
9578 }
9579
9580 return 1;
9581 #endif /* 0 */
9582 }
9583
9584
9585 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
9586 non-zero means only WINDOW is redisplayed in redisplay_internal.
9587 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
9588 in redisplay_window to bring a partially visible line into view in
9589 the case that only the cursor has moved.
9590
9591 Value is
9592
9593 1 if scrolling succeeded
9594
9595 0 if scrolling didn't find point.
9596
9597 -1 if new fonts have been loaded so that we must interrupt
9598 redisplay, adjust glyph matrices, and try again. */
9599
9600 enum
9601 {
9602 SCROLLING_SUCCESS,
9603 SCROLLING_FAILED,
9604 SCROLLING_NEED_LARGER_MATRICES
9605 };
9606
9607 static int
9608 try_scrolling (window, just_this_one_p, scroll_conservatively,
9609 scroll_step, temp_scroll_step)
9610 Lisp_Object window;
9611 int just_this_one_p;
9612 EMACS_INT scroll_conservatively, scroll_step;
9613 int temp_scroll_step;
9614 {
9615 struct window *w = XWINDOW (window);
9616 struct frame *f = XFRAME (w->frame);
9617 struct text_pos scroll_margin_pos;
9618 struct text_pos pos;
9619 struct text_pos startp;
9620 struct it it;
9621 Lisp_Object window_end;
9622 int this_scroll_margin;
9623 int dy = 0;
9624 int scroll_max;
9625 int rc;
9626 int amount_to_scroll = 0;
9627 Lisp_Object aggressive;
9628 int height;
9629
9630 #if GLYPH_DEBUG
9631 debug_method_add (w, "try_scrolling");
9632 #endif
9633
9634 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9635
9636 /* Compute scroll margin height in pixels. We scroll when point is
9637 within this distance from the top or bottom of the window. */
9638 if (scroll_margin > 0)
9639 {
9640 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
9641 this_scroll_margin *= CANON_Y_UNIT (f);
9642 }
9643 else
9644 this_scroll_margin = 0;
9645
9646 /* Compute how much we should try to scroll maximally to bring point
9647 into view. */
9648 if (scroll_step || scroll_conservatively || temp_scroll_step)
9649 scroll_max = max (scroll_step,
9650 max (scroll_conservatively, temp_scroll_step));
9651 else if (NUMBERP (current_buffer->scroll_down_aggressively)
9652 || NUMBERP (current_buffer->scroll_up_aggressively))
9653 /* We're trying to scroll because of aggressive scrolling
9654 but no scroll_step is set. Choose an arbitrary one. Maybe
9655 there should be a variable for this. */
9656 scroll_max = 10;
9657 else
9658 scroll_max = 0;
9659 scroll_max *= CANON_Y_UNIT (f);
9660
9661 /* Decide whether we have to scroll down. Start at the window end
9662 and move this_scroll_margin up to find the position of the scroll
9663 margin. */
9664 window_end = Fwindow_end (window, Qt);
9665 CHARPOS (scroll_margin_pos) = XINT (window_end);
9666 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
9667 if (this_scroll_margin)
9668 {
9669 start_display (&it, w, scroll_margin_pos);
9670 move_it_vertically (&it, - this_scroll_margin);
9671 scroll_margin_pos = it.current.pos;
9672 }
9673
9674 if (PT >= CHARPOS (scroll_margin_pos))
9675 {
9676 int y0;
9677
9678 too_near_end:
9679 /* Point is in the scroll margin at the bottom of the window, or
9680 below. Compute a new window start that makes point visible. */
9681
9682 /* Compute the distance from the scroll margin to PT.
9683 Give up if the distance is greater than scroll_max. */
9684 start_display (&it, w, scroll_margin_pos);
9685 y0 = it.current_y;
9686 move_it_to (&it, PT, 0, it.last_visible_y, -1,
9687 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9688
9689 /* To make point visible, we have to move the window start
9690 down so that the line the cursor is in is visible, which
9691 means we have to add in the height of the cursor line. */
9692 dy = line_bottom_y (&it) - y0;
9693
9694 if (dy > scroll_max)
9695 return SCROLLING_FAILED;
9696
9697 /* Move the window start down. If scrolling conservatively,
9698 move it just enough down to make point visible. If
9699 scroll_step is set, move it down by scroll_step. */
9700 start_display (&it, w, startp);
9701
9702 if (scroll_conservatively)
9703 amount_to_scroll
9704 = max (max (dy, CANON_Y_UNIT (f)),
9705 CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
9706 else if (scroll_step || temp_scroll_step)
9707 amount_to_scroll = scroll_max;
9708 else
9709 {
9710 aggressive = current_buffer->scroll_up_aggressively;
9711 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
9712 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
9713 if (NUMBERP (aggressive))
9714 amount_to_scroll = XFLOATINT (aggressive) * height;
9715 }
9716
9717 if (amount_to_scroll <= 0)
9718 return SCROLLING_FAILED;
9719
9720 move_it_vertically (&it, amount_to_scroll);
9721 startp = it.current.pos;
9722 }
9723 else
9724 {
9725 /* See if point is inside the scroll margin at the top of the
9726 window. */
9727 scroll_margin_pos = startp;
9728 if (this_scroll_margin)
9729 {
9730 start_display (&it, w, startp);
9731 move_it_vertically (&it, this_scroll_margin);
9732 scroll_margin_pos = it.current.pos;
9733 }
9734
9735 if (PT < CHARPOS (scroll_margin_pos))
9736 {
9737 /* Point is in the scroll margin at the top of the window or
9738 above what is displayed in the window. */
9739 int y0;
9740
9741 /* Compute the vertical distance from PT to the scroll
9742 margin position. Give up if distance is greater than
9743 scroll_max. */
9744 SET_TEXT_POS (pos, PT, PT_BYTE);
9745 start_display (&it, w, pos);
9746 y0 = it.current_y;
9747 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
9748 it.last_visible_y, -1,
9749 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9750 dy = it.current_y - y0;
9751 if (dy > scroll_max)
9752 return SCROLLING_FAILED;
9753
9754 /* Compute new window start. */
9755 start_display (&it, w, startp);
9756
9757 if (scroll_conservatively)
9758 amount_to_scroll =
9759 max (dy, CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
9760 else if (scroll_step || temp_scroll_step)
9761 amount_to_scroll = scroll_max;
9762 else
9763 {
9764 aggressive = current_buffer->scroll_down_aggressively;
9765 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
9766 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
9767 if (NUMBERP (aggressive))
9768 amount_to_scroll = XFLOATINT (aggressive) * height;
9769 }
9770
9771 if (amount_to_scroll <= 0)
9772 return SCROLLING_FAILED;
9773
9774 move_it_vertically (&it, - amount_to_scroll);
9775 startp = it.current.pos;
9776 }
9777 }
9778
9779 /* Run window scroll functions. */
9780 startp = run_window_scroll_functions (window, startp);
9781
9782 /* Display the window. Give up if new fonts are loaded, or if point
9783 doesn't appear. */
9784 if (!try_window (window, startp))
9785 rc = SCROLLING_NEED_LARGER_MATRICES;
9786 else if (w->cursor.vpos < 0)
9787 {
9788 clear_glyph_matrix (w->desired_matrix);
9789 rc = SCROLLING_FAILED;
9790 }
9791 else
9792 {
9793 /* Maybe forget recorded base line for line number display. */
9794 if (!just_this_one_p
9795 || current_buffer->clip_changed
9796 || BEG_UNCHANGED < CHARPOS (startp))
9797 w->base_line_number = Qnil;
9798
9799 /* If cursor ends up on a partially visible line,
9800 treat that as being off the bottom of the screen. */
9801 if (! make_cursor_line_fully_visible (w))
9802 {
9803 clear_glyph_matrix (w->desired_matrix);
9804 goto too_near_end;
9805 }
9806 rc = SCROLLING_SUCCESS;
9807 }
9808
9809 return rc;
9810 }
9811
9812
9813 /* Compute a suitable window start for window W if display of W starts
9814 on a continuation line. Value is non-zero if a new window start
9815 was computed.
9816
9817 The new window start will be computed, based on W's width, starting
9818 from the start of the continued line. It is the start of the
9819 screen line with the minimum distance from the old start W->start. */
9820
9821 static int
9822 compute_window_start_on_continuation_line (w)
9823 struct window *w;
9824 {
9825 struct text_pos pos, start_pos;
9826 int window_start_changed_p = 0;
9827
9828 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
9829
9830 /* If window start is on a continuation line... Window start may be
9831 < BEGV in case there's invisible text at the start of the
9832 buffer (M-x rmail, for example). */
9833 if (CHARPOS (start_pos) > BEGV
9834 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
9835 {
9836 struct it it;
9837 struct glyph_row *row;
9838
9839 /* Handle the case that the window start is out of range. */
9840 if (CHARPOS (start_pos) < BEGV)
9841 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
9842 else if (CHARPOS (start_pos) > ZV)
9843 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
9844
9845 /* Find the start of the continued line. This should be fast
9846 because scan_buffer is fast (newline cache). */
9847 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
9848 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
9849 row, DEFAULT_FACE_ID);
9850 reseat_at_previous_visible_line_start (&it);
9851
9852 /* If the line start is "too far" away from the window start,
9853 say it takes too much time to compute a new window start. */
9854 if (CHARPOS (start_pos) - IT_CHARPOS (it)
9855 < XFASTINT (w->height) * XFASTINT (w->width))
9856 {
9857 int min_distance, distance;
9858
9859 /* Move forward by display lines to find the new window
9860 start. If window width was enlarged, the new start can
9861 be expected to be > the old start. If window width was
9862 decreased, the new window start will be < the old start.
9863 So, we're looking for the display line start with the
9864 minimum distance from the old window start. */
9865 pos = it.current.pos;
9866 min_distance = INFINITY;
9867 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
9868 distance < min_distance)
9869 {
9870 min_distance = distance;
9871 pos = it.current.pos;
9872 move_it_by_lines (&it, 1, 0);
9873 }
9874
9875 /* Set the window start there. */
9876 SET_MARKER_FROM_TEXT_POS (w->start, pos);
9877 window_start_changed_p = 1;
9878 }
9879 }
9880
9881 return window_start_changed_p;
9882 }
9883
9884
9885 /* Try cursor movement in case text has not changed in window WINDOW,
9886 with window start STARTP. Value is
9887
9888 CURSOR_MOVEMENT_SUCCESS if successful
9889
9890 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
9891
9892 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
9893 display. *SCROLL_STEP is set to 1, under certain circumstances, if
9894 we want to scroll as if scroll-step were set to 1. See the code.
9895
9896 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
9897 which case we have to abort this redisplay, and adjust matrices
9898 first. */
9899
9900 enum
9901 {
9902 CURSOR_MOVEMENT_SUCCESS,
9903 CURSOR_MOVEMENT_CANNOT_BE_USED,
9904 CURSOR_MOVEMENT_MUST_SCROLL,
9905 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
9906 };
9907
9908 static int
9909 try_cursor_movement (window, startp, scroll_step)
9910 Lisp_Object window;
9911 struct text_pos startp;
9912 int *scroll_step;
9913 {
9914 struct window *w = XWINDOW (window);
9915 struct frame *f = XFRAME (w->frame);
9916 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
9917
9918 #if GLYPH_DEBUG
9919 if (inhibit_try_cursor_movement)
9920 return rc;
9921 #endif
9922
9923 /* Handle case where text has not changed, only point, and it has
9924 not moved off the frame. */
9925 if (/* Point may be in this window. */
9926 PT >= CHARPOS (startp)
9927 /* Selective display hasn't changed. */
9928 && !current_buffer->clip_changed
9929 /* Function force-mode-line-update is used to force a thorough
9930 redisplay. It sets either windows_or_buffers_changed or
9931 update_mode_lines. So don't take a shortcut here for these
9932 cases. */
9933 && !update_mode_lines
9934 && !windows_or_buffers_changed
9935 && !cursor_type_changed
9936 /* Can't use this case if highlighting a region. When a
9937 region exists, cursor movement has to do more than just
9938 set the cursor. */
9939 && !(!NILP (Vtransient_mark_mode)
9940 && !NILP (current_buffer->mark_active))
9941 && NILP (w->region_showing)
9942 && NILP (Vshow_trailing_whitespace)
9943 /* Right after splitting windows, last_point may be nil. */
9944 && INTEGERP (w->last_point)
9945 /* This code is not used for mini-buffer for the sake of the case
9946 of redisplaying to replace an echo area message; since in
9947 that case the mini-buffer contents per se are usually
9948 unchanged. This code is of no real use in the mini-buffer
9949 since the handling of this_line_start_pos, etc., in redisplay
9950 handles the same cases. */
9951 && !EQ (window, minibuf_window)
9952 /* When splitting windows or for new windows, it happens that
9953 redisplay is called with a nil window_end_vpos or one being
9954 larger than the window. This should really be fixed in
9955 window.c. I don't have this on my list, now, so we do
9956 approximately the same as the old redisplay code. --gerd. */
9957 && INTEGERP (w->window_end_vpos)
9958 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
9959 && (FRAME_WINDOW_P (f)
9960 || !MARKERP (Voverlay_arrow_position)
9961 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
9962 {
9963 int this_scroll_margin;
9964 struct glyph_row *row = NULL;
9965
9966 #if GLYPH_DEBUG
9967 debug_method_add (w, "cursor movement");
9968 #endif
9969
9970 /* Scroll if point within this distance from the top or bottom
9971 of the window. This is a pixel value. */
9972 this_scroll_margin = max (0, scroll_margin);
9973 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
9974 this_scroll_margin *= CANON_Y_UNIT (f);
9975
9976 /* Start with the row the cursor was displayed during the last
9977 not paused redisplay. Give up if that row is not valid. */
9978 if (w->last_cursor.vpos < 0
9979 || w->last_cursor.vpos >= w->current_matrix->nrows)
9980 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9981 else
9982 {
9983 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
9984 if (row->mode_line_p)
9985 ++row;
9986 if (!row->enabled_p)
9987 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9988 }
9989
9990 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
9991 {
9992 int scroll_p = 0;
9993 int last_y = window_text_bottom_y (w) - this_scroll_margin;
9994
9995 if (PT > XFASTINT (w->last_point))
9996 {
9997 /* Point has moved forward. */
9998 while (MATRIX_ROW_END_CHARPOS (row) < PT
9999 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
10000 {
10001 xassert (row->enabled_p);
10002 ++row;
10003 }
10004
10005 /* The end position of a row equals the start position
10006 of the next row. If PT is there, we would rather
10007 display it in the next line. */
10008 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
10009 && MATRIX_ROW_END_CHARPOS (row) == PT
10010 && !cursor_row_p (w, row))
10011 ++row;
10012
10013 /* If within the scroll margin, scroll. Note that
10014 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
10015 the next line would be drawn, and that
10016 this_scroll_margin can be zero. */
10017 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
10018 || PT > MATRIX_ROW_END_CHARPOS (row)
10019 /* Line is completely visible last line in window
10020 and PT is to be set in the next line. */
10021 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
10022 && PT == MATRIX_ROW_END_CHARPOS (row)
10023 && !row->ends_at_zv_p
10024 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
10025 scroll_p = 1;
10026 }
10027 else if (PT < XFASTINT (w->last_point))
10028 {
10029 /* Cursor has to be moved backward. Note that PT >=
10030 CHARPOS (startp) because of the outer
10031 if-statement. */
10032 while (!row->mode_line_p
10033 && (MATRIX_ROW_START_CHARPOS (row) > PT
10034 || (MATRIX_ROW_START_CHARPOS (row) == PT
10035 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
10036 && (row->y > this_scroll_margin
10037 || CHARPOS (startp) == BEGV))
10038 {
10039 xassert (row->enabled_p);
10040 --row;
10041 }
10042
10043 /* Consider the following case: Window starts at BEGV,
10044 there is invisible, intangible text at BEGV, so that
10045 display starts at some point START > BEGV. It can
10046 happen that we are called with PT somewhere between
10047 BEGV and START. Try to handle that case. */
10048 if (row < w->current_matrix->rows
10049 || row->mode_line_p)
10050 {
10051 row = w->current_matrix->rows;
10052 if (row->mode_line_p)
10053 ++row;
10054 }
10055
10056 /* Due to newlines in overlay strings, we may have to
10057 skip forward over overlay strings. */
10058 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
10059 && MATRIX_ROW_END_CHARPOS (row) == PT
10060 && !cursor_row_p (w, row))
10061 ++row;
10062
10063 /* If within the scroll margin, scroll. */
10064 if (row->y < this_scroll_margin
10065 && CHARPOS (startp) != BEGV)
10066 scroll_p = 1;
10067 }
10068
10069 if (PT < MATRIX_ROW_START_CHARPOS (row)
10070 || PT > MATRIX_ROW_END_CHARPOS (row))
10071 {
10072 /* if PT is not in the glyph row, give up. */
10073 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10074 }
10075 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
10076 {
10077 if (PT == MATRIX_ROW_END_CHARPOS (row)
10078 && !row->ends_at_zv_p
10079 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
10080 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10081 else if (row->height > window_box_height (w))
10082 {
10083 /* If we end up in a partially visible line, let's
10084 make it fully visible, except when it's taller
10085 than the window, in which case we can't do much
10086 about it. */
10087 *scroll_step = 1;
10088 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10089 }
10090 else
10091 {
10092 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10093 try_window (window, startp);
10094 if (!make_cursor_line_fully_visible (w))
10095 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10096 else
10097 rc = CURSOR_MOVEMENT_SUCCESS;
10098 }
10099 }
10100 else if (scroll_p)
10101 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10102 else
10103 {
10104 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10105 rc = CURSOR_MOVEMENT_SUCCESS;
10106 }
10107 }
10108 }
10109
10110 return rc;
10111 }
10112
10113
10114 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
10115 selected_window is redisplayed.
10116
10117 We can return without actually redisplaying the window if
10118 fonts_changed_p is nonzero. In that case, redisplay_internal will
10119 retry. */
10120
10121 static void
10122 redisplay_window (window, just_this_one_p)
10123 Lisp_Object window;
10124 int just_this_one_p;
10125 {
10126 struct window *w = XWINDOW (window);
10127 struct frame *f = XFRAME (w->frame);
10128 struct buffer *buffer = XBUFFER (w->buffer);
10129 struct buffer *old = current_buffer;
10130 struct text_pos lpoint, opoint, startp;
10131 int update_mode_line;
10132 int tem;
10133 struct it it;
10134 /* Record it now because it's overwritten. */
10135 int current_matrix_up_to_date_p = 0;
10136 /* This is less strict than current_matrix_up_to_date_p.
10137 It indictes that the buffer contents and narrowing are unchanged. */
10138 int buffer_unchanged_p = 0;
10139 int temp_scroll_step = 0;
10140 int count = SPECPDL_INDEX ();
10141 int rc;
10142 int centering_position;
10143
10144 SET_TEXT_POS (lpoint, PT, PT_BYTE);
10145 opoint = lpoint;
10146
10147 /* W must be a leaf window here. */
10148 xassert (!NILP (w->buffer));
10149 #if GLYPH_DEBUG
10150 *w->desired_matrix->method = 0;
10151 #endif
10152
10153 specbind (Qinhibit_point_motion_hooks, Qt);
10154
10155 reconsider_clip_changes (w, buffer);
10156
10157 /* Has the mode line to be updated? */
10158 update_mode_line = (!NILP (w->update_mode_line)
10159 || update_mode_lines
10160 || buffer->clip_changed
10161 || buffer->prevent_redisplay_optimizations_p);
10162
10163 if (MINI_WINDOW_P (w))
10164 {
10165 if (w == XWINDOW (echo_area_window)
10166 && !NILP (echo_area_buffer[0]))
10167 {
10168 if (update_mode_line)
10169 /* We may have to update a tty frame's menu bar or a
10170 tool-bar. Example `M-x C-h C-h C-g'. */
10171 goto finish_menu_bars;
10172 else
10173 /* We've already displayed the echo area glyphs in this window. */
10174 goto finish_scroll_bars;
10175 }
10176 else if (w != XWINDOW (minibuf_window))
10177 {
10178 /* W is a mini-buffer window, but it's not the currently
10179 active one, so clear it. */
10180 int yb = window_text_bottom_y (w);
10181 struct glyph_row *row;
10182 int y;
10183
10184 for (y = 0, row = w->desired_matrix->rows;
10185 y < yb;
10186 y += row->height, ++row)
10187 blank_row (w, row, y);
10188 goto finish_scroll_bars;
10189 }
10190
10191 clear_glyph_matrix (w->desired_matrix);
10192 }
10193
10194 /* Otherwise set up data on this window; select its buffer and point
10195 value. */
10196 /* Really select the buffer, for the sake of buffer-local
10197 variables. */
10198 set_buffer_internal_1 (XBUFFER (w->buffer));
10199 SET_TEXT_POS (opoint, PT, PT_BYTE);
10200
10201 current_matrix_up_to_date_p
10202 = (!NILP (w->window_end_valid)
10203 && !current_buffer->clip_changed
10204 && !current_buffer->prevent_redisplay_optimizations_p
10205 && XFASTINT (w->last_modified) >= MODIFF
10206 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
10207
10208 buffer_unchanged_p
10209 = (!NILP (w->window_end_valid)
10210 && !current_buffer->clip_changed
10211 && XFASTINT (w->last_modified) >= MODIFF
10212 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
10213
10214 /* When windows_or_buffers_changed is non-zero, we can't rely on
10215 the window end being valid, so set it to nil there. */
10216 if (windows_or_buffers_changed)
10217 {
10218 /* If window starts on a continuation line, maybe adjust the
10219 window start in case the window's width changed. */
10220 if (XMARKER (w->start)->buffer == current_buffer)
10221 compute_window_start_on_continuation_line (w);
10222
10223 w->window_end_valid = Qnil;
10224 }
10225
10226 /* Some sanity checks. */
10227 CHECK_WINDOW_END (w);
10228 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
10229 abort ();
10230 if (BYTEPOS (opoint) < CHARPOS (opoint))
10231 abort ();
10232
10233 /* If %c is in mode line, update it if needed. */
10234 if (!NILP (w->column_number_displayed)
10235 /* This alternative quickly identifies a common case
10236 where no change is needed. */
10237 && !(PT == XFASTINT (w->last_point)
10238 && XFASTINT (w->last_modified) >= MODIFF
10239 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
10240 && (XFASTINT (w->column_number_displayed)
10241 != (int) current_column ())) /* iftc */
10242 update_mode_line = 1;
10243
10244 /* Count number of windows showing the selected buffer. An indirect
10245 buffer counts as its base buffer. */
10246 if (!just_this_one_p)
10247 {
10248 struct buffer *current_base, *window_base;
10249 current_base = current_buffer;
10250 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
10251 if (current_base->base_buffer)
10252 current_base = current_base->base_buffer;
10253 if (window_base->base_buffer)
10254 window_base = window_base->base_buffer;
10255 if (current_base == window_base)
10256 buffer_shared++;
10257 }
10258
10259 /* Point refers normally to the selected window. For any other
10260 window, set up appropriate value. */
10261 if (!EQ (window, selected_window))
10262 {
10263 int new_pt = XMARKER (w->pointm)->charpos;
10264 int new_pt_byte = marker_byte_position (w->pointm);
10265 if (new_pt < BEGV)
10266 {
10267 new_pt = BEGV;
10268 new_pt_byte = BEGV_BYTE;
10269 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
10270 }
10271 else if (new_pt > (ZV - 1))
10272 {
10273 new_pt = ZV;
10274 new_pt_byte = ZV_BYTE;
10275 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
10276 }
10277
10278 /* We don't use SET_PT so that the point-motion hooks don't run. */
10279 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
10280 }
10281
10282 /* If any of the character widths specified in the display table
10283 have changed, invalidate the width run cache. It's true that
10284 this may be a bit late to catch such changes, but the rest of
10285 redisplay goes (non-fatally) haywire when the display table is
10286 changed, so why should we worry about doing any better? */
10287 if (current_buffer->width_run_cache)
10288 {
10289 struct Lisp_Char_Table *disptab = buffer_display_table ();
10290
10291 if (! disptab_matches_widthtab (disptab,
10292 XVECTOR (current_buffer->width_table)))
10293 {
10294 invalidate_region_cache (current_buffer,
10295 current_buffer->width_run_cache,
10296 BEG, Z);
10297 recompute_width_table (current_buffer, disptab);
10298 }
10299 }
10300
10301 /* If window-start is screwed up, choose a new one. */
10302 if (XMARKER (w->start)->buffer != current_buffer)
10303 goto recenter;
10304
10305 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10306
10307 /* If someone specified a new starting point but did not insist,
10308 check whether it can be used. */
10309 if (!NILP (w->optional_new_start)
10310 && CHARPOS (startp) >= BEGV
10311 && CHARPOS (startp) <= ZV)
10312 {
10313 w->optional_new_start = Qnil;
10314 start_display (&it, w, startp);
10315 move_it_to (&it, PT, 0, it.last_visible_y, -1,
10316 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
10317 if (IT_CHARPOS (it) == PT)
10318 w->force_start = Qt;
10319 }
10320
10321 /* Handle case where place to start displaying has been specified,
10322 unless the specified location is outside the accessible range. */
10323 if (!NILP (w->force_start)
10324 || w->frozen_window_start_p)
10325 {
10326 /* We set this later on if we have to adjust point. */
10327 int new_vpos = -1;
10328
10329 w->force_start = Qnil;
10330 w->vscroll = 0;
10331 w->window_end_valid = Qnil;
10332
10333 /* Forget any recorded base line for line number display. */
10334 if (!buffer_unchanged_p)
10335 w->base_line_number = Qnil;
10336
10337 /* Redisplay the mode line. Select the buffer properly for that.
10338 Also, run the hook window-scroll-functions
10339 because we have scrolled. */
10340 /* Note, we do this after clearing force_start because
10341 if there's an error, it is better to forget about force_start
10342 than to get into an infinite loop calling the hook functions
10343 and having them get more errors. */
10344 if (!update_mode_line
10345 || ! NILP (Vwindow_scroll_functions))
10346 {
10347 update_mode_line = 1;
10348 w->update_mode_line = Qt;
10349 startp = run_window_scroll_functions (window, startp);
10350 }
10351
10352 w->last_modified = make_number (0);
10353 w->last_overlay_modified = make_number (0);
10354 if (CHARPOS (startp) < BEGV)
10355 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
10356 else if (CHARPOS (startp) > ZV)
10357 SET_TEXT_POS (startp, ZV, ZV_BYTE);
10358
10359 /* Redisplay, then check if cursor has been set during the
10360 redisplay. Give up if new fonts were loaded. */
10361 if (!try_window (window, startp))
10362 {
10363 w->force_start = Qt;
10364 clear_glyph_matrix (w->desired_matrix);
10365 goto need_larger_matrices;
10366 }
10367
10368 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
10369 {
10370 /* If point does not appear, try to move point so it does
10371 appear. The desired matrix has been built above, so we
10372 can use it here. */
10373 new_vpos = window_box_height (w) / 2;
10374 }
10375
10376 if (!make_cursor_line_fully_visible (w))
10377 {
10378 /* Point does appear, but on a line partly visible at end of window.
10379 Move it back to a fully-visible line. */
10380 new_vpos = window_box_height (w);
10381 }
10382
10383 /* If we need to move point for either of the above reasons,
10384 now actually do it. */
10385 if (new_vpos >= 0)
10386 {
10387 struct glyph_row *row;
10388
10389 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
10390 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
10391 ++row;
10392
10393 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
10394 MATRIX_ROW_START_BYTEPOS (row));
10395
10396 if (w != XWINDOW (selected_window))
10397 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
10398 else if (current_buffer == old)
10399 SET_TEXT_POS (lpoint, PT, PT_BYTE);
10400
10401 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
10402
10403 /* If we are highlighting the region, then we just changed
10404 the region, so redisplay to show it. */
10405 if (!NILP (Vtransient_mark_mode)
10406 && !NILP (current_buffer->mark_active))
10407 {
10408 clear_glyph_matrix (w->desired_matrix);
10409 if (!try_window (window, startp))
10410 goto need_larger_matrices;
10411 }
10412 }
10413
10414 #if GLYPH_DEBUG
10415 debug_method_add (w, "forced window start");
10416 #endif
10417 goto done;
10418 }
10419
10420 /* Handle case where text has not changed, only point, and it has
10421 not moved off the frame, and we are not retrying after hscroll.
10422 (current_matrix_up_to_date_p is nonzero when retrying.) */
10423 if (current_matrix_up_to_date_p
10424 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
10425 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
10426 {
10427 switch (rc)
10428 {
10429 case CURSOR_MOVEMENT_SUCCESS:
10430 goto done;
10431
10432 #if 0 /* try_cursor_movement never returns this value. */
10433 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
10434 goto need_larger_matrices;
10435 #endif
10436
10437 case CURSOR_MOVEMENT_MUST_SCROLL:
10438 goto try_to_scroll;
10439
10440 default:
10441 abort ();
10442 }
10443 }
10444 /* If current starting point was originally the beginning of a line
10445 but no longer is, find a new starting point. */
10446 else if (!NILP (w->start_at_line_beg)
10447 && !(CHARPOS (startp) <= BEGV
10448 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
10449 {
10450 #if GLYPH_DEBUG
10451 debug_method_add (w, "recenter 1");
10452 #endif
10453 goto recenter;
10454 }
10455
10456 /* Try scrolling with try_window_id. Value is > 0 if update has
10457 been done, it is -1 if we know that the same window start will
10458 not work. It is 0 if unsuccessful for some other reason. */
10459 else if ((tem = try_window_id (w)) != 0)
10460 {
10461 #if GLYPH_DEBUG
10462 debug_method_add (w, "try_window_id %d", tem);
10463 #endif
10464
10465 if (fonts_changed_p)
10466 goto need_larger_matrices;
10467 if (tem > 0)
10468 goto done;
10469
10470 /* Otherwise try_window_id has returned -1 which means that we
10471 don't want the alternative below this comment to execute. */
10472 }
10473 else if (CHARPOS (startp) >= BEGV
10474 && CHARPOS (startp) <= ZV
10475 && PT >= CHARPOS (startp)
10476 && (CHARPOS (startp) < ZV
10477 /* Avoid starting at end of buffer. */
10478 || CHARPOS (startp) == BEGV
10479 || (XFASTINT (w->last_modified) >= MODIFF
10480 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
10481 {
10482 #if GLYPH_DEBUG
10483 debug_method_add (w, "same window start");
10484 #endif
10485
10486 /* Try to redisplay starting at same place as before.
10487 If point has not moved off frame, accept the results. */
10488 if (!current_matrix_up_to_date_p
10489 /* Don't use try_window_reusing_current_matrix in this case
10490 because a window scroll function can have changed the
10491 buffer. */
10492 || !NILP (Vwindow_scroll_functions)
10493 || MINI_WINDOW_P (w)
10494 || !try_window_reusing_current_matrix (w))
10495 {
10496 IF_DEBUG (debug_method_add (w, "1"));
10497 try_window (window, startp);
10498 }
10499
10500 if (fonts_changed_p)
10501 goto need_larger_matrices;
10502
10503 if (w->cursor.vpos >= 0)
10504 {
10505 if (!just_this_one_p
10506 || current_buffer->clip_changed
10507 || BEG_UNCHANGED < CHARPOS (startp))
10508 /* Forget any recorded base line for line number display. */
10509 w->base_line_number = Qnil;
10510
10511 if (!make_cursor_line_fully_visible (w))
10512 clear_glyph_matrix (w->desired_matrix);
10513 /* Drop through and scroll. */
10514 else
10515 goto done;
10516 }
10517 else
10518 clear_glyph_matrix (w->desired_matrix);
10519 }
10520
10521 try_to_scroll:
10522
10523 w->last_modified = make_number (0);
10524 w->last_overlay_modified = make_number (0);
10525
10526 /* Redisplay the mode line. Select the buffer properly for that. */
10527 if (!update_mode_line)
10528 {
10529 update_mode_line = 1;
10530 w->update_mode_line = Qt;
10531 }
10532
10533 /* Try to scroll by specified few lines. */
10534 if ((scroll_conservatively
10535 || scroll_step
10536 || temp_scroll_step
10537 || NUMBERP (current_buffer->scroll_up_aggressively)
10538 || NUMBERP (current_buffer->scroll_down_aggressively))
10539 && !current_buffer->clip_changed
10540 && CHARPOS (startp) >= BEGV
10541 && CHARPOS (startp) <= ZV)
10542 {
10543 /* The function returns -1 if new fonts were loaded, 1 if
10544 successful, 0 if not successful. */
10545 int rc = try_scrolling (window, just_this_one_p,
10546 scroll_conservatively,
10547 scroll_step,
10548 temp_scroll_step);
10549 switch (rc)
10550 {
10551 case SCROLLING_SUCCESS:
10552 goto done;
10553
10554 case SCROLLING_NEED_LARGER_MATRICES:
10555 goto need_larger_matrices;
10556
10557 case SCROLLING_FAILED:
10558 break;
10559
10560 default:
10561 abort ();
10562 }
10563 }
10564
10565 /* Finally, just choose place to start which centers point */
10566
10567 recenter:
10568 centering_position = window_box_height (w) / 2;
10569
10570 point_at_top:
10571 /* Jump here with centering_position already set to 0. */
10572
10573 #if GLYPH_DEBUG
10574 debug_method_add (w, "recenter");
10575 #endif
10576
10577 /* w->vscroll = 0; */
10578
10579 /* Forget any previously recorded base line for line number display. */
10580 if (!buffer_unchanged_p)
10581 w->base_line_number = Qnil;
10582
10583 /* Move backward half the height of the window. */
10584 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10585 it.current_y = it.last_visible_y;
10586 move_it_vertically_backward (&it, centering_position);
10587 xassert (IT_CHARPOS (it) >= BEGV);
10588
10589 /* The function move_it_vertically_backward may move over more
10590 than the specified y-distance. If it->w is small, e.g. a
10591 mini-buffer window, we may end up in front of the window's
10592 display area. Start displaying at the start of the line
10593 containing PT in this case. */
10594 if (it.current_y <= 0)
10595 {
10596 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10597 move_it_vertically (&it, 0);
10598 xassert (IT_CHARPOS (it) <= PT);
10599 it.current_y = 0;
10600 }
10601
10602 it.current_x = it.hpos = 0;
10603
10604 /* Set startp here explicitly in case that helps avoid an infinite loop
10605 in case the window-scroll-functions functions get errors. */
10606 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
10607
10608 /* Run scroll hooks. */
10609 startp = run_window_scroll_functions (window, it.current.pos);
10610
10611 /* Redisplay the window. */
10612 if (!current_matrix_up_to_date_p
10613 || windows_or_buffers_changed
10614 || cursor_type_changed
10615 /* Don't use try_window_reusing_current_matrix in this case
10616 because it can have changed the buffer. */
10617 || !NILP (Vwindow_scroll_functions)
10618 || !just_this_one_p
10619 || MINI_WINDOW_P (w)
10620 || !try_window_reusing_current_matrix (w))
10621 try_window (window, startp);
10622
10623 /* If new fonts have been loaded (due to fontsets), give up. We
10624 have to start a new redisplay since we need to re-adjust glyph
10625 matrices. */
10626 if (fonts_changed_p)
10627 goto need_larger_matrices;
10628
10629 /* If cursor did not appear assume that the middle of the window is
10630 in the first line of the window. Do it again with the next line.
10631 (Imagine a window of height 100, displaying two lines of height
10632 60. Moving back 50 from it->last_visible_y will end in the first
10633 line.) */
10634 if (w->cursor.vpos < 0)
10635 {
10636 if (!NILP (w->window_end_valid)
10637 && PT >= Z - XFASTINT (w->window_end_pos))
10638 {
10639 clear_glyph_matrix (w->desired_matrix);
10640 move_it_by_lines (&it, 1, 0);
10641 try_window (window, it.current.pos);
10642 }
10643 else if (PT < IT_CHARPOS (it))
10644 {
10645 clear_glyph_matrix (w->desired_matrix);
10646 move_it_by_lines (&it, -1, 0);
10647 try_window (window, it.current.pos);
10648 }
10649 else
10650 {
10651 /* Not much we can do about it. */
10652 }
10653 }
10654
10655 /* Consider the following case: Window starts at BEGV, there is
10656 invisible, intangible text at BEGV, so that display starts at
10657 some point START > BEGV. It can happen that we are called with
10658 PT somewhere between BEGV and START. Try to handle that case. */
10659 if (w->cursor.vpos < 0)
10660 {
10661 struct glyph_row *row = w->current_matrix->rows;
10662 if (row->mode_line_p)
10663 ++row;
10664 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10665 }
10666
10667 if (!make_cursor_line_fully_visible (w))
10668 {
10669 /* If centering point failed to make the whole line visible,
10670 put point at the top instead. That has to make the whole line
10671 visible, if it can be done. */
10672 centering_position = 0;
10673 goto point_at_top;
10674 }
10675
10676 done:
10677
10678 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10679 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
10680 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
10681 ? Qt : Qnil);
10682
10683 /* Display the mode line, if we must. */
10684 if ((update_mode_line
10685 /* If window not full width, must redo its mode line
10686 if (a) the window to its side is being redone and
10687 (b) we do a frame-based redisplay. This is a consequence
10688 of how inverted lines are drawn in frame-based redisplay. */
10689 || (!just_this_one_p
10690 && !FRAME_WINDOW_P (f)
10691 && !WINDOW_FULL_WIDTH_P (w))
10692 /* Line number to display. */
10693 || INTEGERP (w->base_line_pos)
10694 /* Column number is displayed and different from the one displayed. */
10695 || (!NILP (w->column_number_displayed)
10696 && (XFASTINT (w->column_number_displayed)
10697 != (int) current_column ()))) /* iftc */
10698 /* This means that the window has a mode line. */
10699 && (WINDOW_WANTS_MODELINE_P (w)
10700 || WINDOW_WANTS_HEADER_LINE_P (w)))
10701 {
10702 display_mode_lines (w);
10703
10704 /* If mode line height has changed, arrange for a thorough
10705 immediate redisplay using the correct mode line height. */
10706 if (WINDOW_WANTS_MODELINE_P (w)
10707 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
10708 {
10709 fonts_changed_p = 1;
10710 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
10711 = DESIRED_MODE_LINE_HEIGHT (w);
10712 }
10713
10714 /* If top line height has changed, arrange for a thorough
10715 immediate redisplay using the correct mode line height. */
10716 if (WINDOW_WANTS_HEADER_LINE_P (w)
10717 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
10718 {
10719 fonts_changed_p = 1;
10720 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
10721 = DESIRED_HEADER_LINE_HEIGHT (w);
10722 }
10723
10724 if (fonts_changed_p)
10725 goto need_larger_matrices;
10726 }
10727
10728 if (!line_number_displayed
10729 && !BUFFERP (w->base_line_pos))
10730 {
10731 w->base_line_pos = Qnil;
10732 w->base_line_number = Qnil;
10733 }
10734
10735 finish_menu_bars:
10736
10737 /* When we reach a frame's selected window, redo the frame's menu bar. */
10738 if (update_mode_line
10739 && EQ (FRAME_SELECTED_WINDOW (f), window))
10740 {
10741 int redisplay_menu_p = 0;
10742
10743 if (FRAME_WINDOW_P (f))
10744 {
10745 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS)
10746 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
10747 #else
10748 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10749 #endif
10750 }
10751 else
10752 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10753
10754 if (redisplay_menu_p)
10755 display_menu_bar (w);
10756
10757 #ifdef HAVE_WINDOW_SYSTEM
10758 if (WINDOWP (f->tool_bar_window)
10759 && (FRAME_TOOL_BAR_LINES (f) > 0
10760 || auto_resize_tool_bars_p))
10761 redisplay_tool_bar (f);
10762 #endif
10763 }
10764
10765 /* We go to this label, with fonts_changed_p nonzero,
10766 if it is necessary to try again using larger glyph matrices.
10767 We have to redeem the scroll bar even in this case,
10768 because the loop in redisplay_internal expects that. */
10769 need_larger_matrices:
10770 ;
10771 finish_scroll_bars:
10772
10773 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
10774 {
10775 int start, end, whole;
10776
10777 /* Calculate the start and end positions for the current window.
10778 At some point, it would be nice to choose between scrollbars
10779 which reflect the whole buffer size, with special markers
10780 indicating narrowing, and scrollbars which reflect only the
10781 visible region.
10782
10783 Note that mini-buffers sometimes aren't displaying any text. */
10784 if (!MINI_WINDOW_P (w)
10785 || (w == XWINDOW (minibuf_window)
10786 && NILP (echo_area_buffer[0])))
10787 {
10788 whole = ZV - BEGV;
10789 start = marker_position (w->start) - BEGV;
10790 /* I don't think this is guaranteed to be right. For the
10791 moment, we'll pretend it is. */
10792 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
10793
10794 if (end < start)
10795 end = start;
10796 if (whole < (end - start))
10797 whole = end - start;
10798 }
10799 else
10800 start = end = whole = 0;
10801
10802 /* Indicate what this scroll bar ought to be displaying now. */
10803 set_vertical_scroll_bar_hook (w, end - start, whole, start);
10804
10805 /* Note that we actually used the scroll bar attached to this
10806 window, so it shouldn't be deleted at the end of redisplay. */
10807 redeem_scroll_bar_hook (w);
10808 }
10809
10810 /* Restore current_buffer and value of point in it. */
10811 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
10812 set_buffer_internal_1 (old);
10813 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
10814
10815 unbind_to (count, Qnil);
10816 }
10817
10818
10819 /* Build the complete desired matrix of WINDOW with a window start
10820 buffer position POS. Value is non-zero if successful. It is zero
10821 if fonts were loaded during redisplay which makes re-adjusting
10822 glyph matrices necessary. */
10823
10824 int
10825 try_window (window, pos)
10826 Lisp_Object window;
10827 struct text_pos pos;
10828 {
10829 struct window *w = XWINDOW (window);
10830 struct it it;
10831 struct glyph_row *last_text_row = NULL;
10832
10833 /* Make POS the new window start. */
10834 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
10835
10836 /* Mark cursor position as unknown. No overlay arrow seen. */
10837 w->cursor.vpos = -1;
10838 overlay_arrow_seen = 0;
10839
10840 /* Initialize iterator and info to start at POS. */
10841 start_display (&it, w, pos);
10842
10843 /* Display all lines of W. */
10844 while (it.current_y < it.last_visible_y)
10845 {
10846 if (display_line (&it))
10847 last_text_row = it.glyph_row - 1;
10848 if (fonts_changed_p)
10849 return 0;
10850 }
10851
10852 /* If bottom moved off end of frame, change mode line percentage. */
10853 if (XFASTINT (w->window_end_pos) <= 0
10854 && Z != IT_CHARPOS (it))
10855 w->update_mode_line = Qt;
10856
10857 /* Set window_end_pos to the offset of the last character displayed
10858 on the window from the end of current_buffer. Set
10859 window_end_vpos to its row number. */
10860 if (last_text_row)
10861 {
10862 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
10863 w->window_end_bytepos
10864 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10865 w->window_end_pos
10866 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10867 w->window_end_vpos
10868 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10869 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
10870 ->displays_text_p);
10871 }
10872 else
10873 {
10874 w->window_end_bytepos = 0;
10875 w->window_end_pos = w->window_end_vpos = make_number (0);
10876 }
10877
10878 /* But that is not valid info until redisplay finishes. */
10879 w->window_end_valid = Qnil;
10880 return 1;
10881 }
10882
10883
10884 \f
10885 /************************************************************************
10886 Window redisplay reusing current matrix when buffer has not changed
10887 ************************************************************************/
10888
10889 /* Try redisplay of window W showing an unchanged buffer with a
10890 different window start than the last time it was displayed by
10891 reusing its current matrix. Value is non-zero if successful.
10892 W->start is the new window start. */
10893
10894 static int
10895 try_window_reusing_current_matrix (w)
10896 struct window *w;
10897 {
10898 struct frame *f = XFRAME (w->frame);
10899 struct glyph_row *row, *bottom_row;
10900 struct it it;
10901 struct run run;
10902 struct text_pos start, new_start;
10903 int nrows_scrolled, i;
10904 struct glyph_row *last_text_row;
10905 struct glyph_row *last_reused_text_row;
10906 struct glyph_row *start_row;
10907 int start_vpos, min_y, max_y;
10908
10909 #if GLYPH_DEBUG
10910 if (inhibit_try_window_reusing)
10911 return 0;
10912 #endif
10913
10914 if (/* This function doesn't handle terminal frames. */
10915 !FRAME_WINDOW_P (f)
10916 /* Don't try to reuse the display if windows have been split
10917 or such. */
10918 || windows_or_buffers_changed
10919 || cursor_type_changed)
10920 return 0;
10921
10922 /* Can't do this if region may have changed. */
10923 if ((!NILP (Vtransient_mark_mode)
10924 && !NILP (current_buffer->mark_active))
10925 || !NILP (w->region_showing)
10926 || !NILP (Vshow_trailing_whitespace))
10927 return 0;
10928
10929 /* If top-line visibility has changed, give up. */
10930 if (WINDOW_WANTS_HEADER_LINE_P (w)
10931 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
10932 return 0;
10933
10934 /* Give up if old or new display is scrolled vertically. We could
10935 make this function handle this, but right now it doesn't. */
10936 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10937 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
10938 return 0;
10939
10940 /* The variable new_start now holds the new window start. The old
10941 start `start' can be determined from the current matrix. */
10942 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
10943 start = start_row->start.pos;
10944 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
10945
10946 /* Clear the desired matrix for the display below. */
10947 clear_glyph_matrix (w->desired_matrix);
10948
10949 if (CHARPOS (new_start) <= CHARPOS (start))
10950 {
10951 int first_row_y;
10952
10953 /* Don't use this method if the display starts with an ellipsis
10954 displayed for invisible text. It's not easy to handle that case
10955 below, and it's certainly not worth the effort since this is
10956 not a frequent case. */
10957 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
10958 return 0;
10959
10960 IF_DEBUG (debug_method_add (w, "twu1"));
10961
10962 /* Display up to a row that can be reused. The variable
10963 last_text_row is set to the last row displayed that displays
10964 text. Note that it.vpos == 0 if or if not there is a
10965 header-line; it's not the same as the MATRIX_ROW_VPOS! */
10966 start_display (&it, w, new_start);
10967 first_row_y = it.current_y;
10968 w->cursor.vpos = -1;
10969 last_text_row = last_reused_text_row = NULL;
10970
10971 while (it.current_y < it.last_visible_y
10972 && IT_CHARPOS (it) < CHARPOS (start)
10973 && !fonts_changed_p)
10974 if (display_line (&it))
10975 last_text_row = it.glyph_row - 1;
10976
10977 /* A value of current_y < last_visible_y means that we stopped
10978 at the previous window start, which in turn means that we
10979 have at least one reusable row. */
10980 if (it.current_y < it.last_visible_y)
10981 {
10982 /* IT.vpos always starts from 0; it counts text lines. */
10983 nrows_scrolled = it.vpos;
10984
10985 /* Find PT if not already found in the lines displayed. */
10986 if (w->cursor.vpos < 0)
10987 {
10988 int dy = it.current_y - first_row_y;
10989
10990 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10991 row = row_containing_pos (w, PT, row, NULL, dy);
10992 if (row)
10993 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
10994 dy, nrows_scrolled);
10995 else
10996 {
10997 clear_glyph_matrix (w->desired_matrix);
10998 return 0;
10999 }
11000 }
11001
11002 /* Scroll the display. Do it before the current matrix is
11003 changed. The problem here is that update has not yet
11004 run, i.e. part of the current matrix is not up to date.
11005 scroll_run_hook will clear the cursor, and use the
11006 current matrix to get the height of the row the cursor is
11007 in. */
11008 run.current_y = first_row_y;
11009 run.desired_y = it.current_y;
11010 run.height = it.last_visible_y - it.current_y;
11011
11012 if (run.height > 0 && run.current_y != run.desired_y)
11013 {
11014 update_begin (f);
11015 rif->update_window_begin_hook (w);
11016 rif->clear_mouse_face (w);
11017 rif->scroll_run_hook (w, &run);
11018 rif->update_window_end_hook (w, 0, 0);
11019 update_end (f);
11020 }
11021
11022 /* Shift current matrix down by nrows_scrolled lines. */
11023 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
11024 rotate_matrix (w->current_matrix,
11025 start_vpos,
11026 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
11027 nrows_scrolled);
11028
11029 /* Disable lines that must be updated. */
11030 for (i = 0; i < it.vpos; ++i)
11031 (start_row + i)->enabled_p = 0;
11032
11033 /* Re-compute Y positions. */
11034 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
11035 max_y = it.last_visible_y;
11036 for (row = start_row + nrows_scrolled;
11037 row < bottom_row;
11038 ++row)
11039 {
11040 row->y = it.current_y;
11041 row->visible_height = row->height;
11042
11043 if (row->y < min_y)
11044 row->visible_height -= min_y - row->y;
11045 if (row->y + row->height > max_y)
11046 row->visible_height -= row->y + row->height - max_y;
11047
11048 it.current_y += row->height;
11049
11050 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
11051 last_reused_text_row = row;
11052 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
11053 break;
11054 }
11055
11056 /* Disable lines in the current matrix which are now
11057 below the window. */
11058 for (++row; row < bottom_row; ++row)
11059 row->enabled_p = 0;
11060 }
11061
11062 /* Update window_end_pos etc.; last_reused_text_row is the last
11063 reused row from the current matrix containing text, if any.
11064 The value of last_text_row is the last displayed line
11065 containing text. */
11066 if (last_reused_text_row)
11067 {
11068 w->window_end_bytepos
11069 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
11070 w->window_end_pos
11071 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
11072 w->window_end_vpos
11073 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
11074 w->current_matrix));
11075 }
11076 else if (last_text_row)
11077 {
11078 w->window_end_bytepos
11079 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
11080 w->window_end_pos
11081 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
11082 w->window_end_vpos
11083 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
11084 }
11085 else
11086 {
11087 /* This window must be completely empty. */
11088 w->window_end_bytepos = 0;
11089 w->window_end_pos = w->window_end_vpos = make_number (0);
11090 }
11091 w->window_end_valid = Qnil;
11092
11093 /* Update hint: don't try scrolling again in update_window. */
11094 w->desired_matrix->no_scrolling_p = 1;
11095
11096 #if GLYPH_DEBUG
11097 debug_method_add (w, "try_window_reusing_current_matrix 1");
11098 #endif
11099 return 1;
11100 }
11101 else if (CHARPOS (new_start) > CHARPOS (start))
11102 {
11103 struct glyph_row *pt_row, *row;
11104 struct glyph_row *first_reusable_row;
11105 struct glyph_row *first_row_to_display;
11106 int dy;
11107 int yb = window_text_bottom_y (w);
11108
11109 /* Find the row starting at new_start, if there is one. Don't
11110 reuse a partially visible line at the end. */
11111 first_reusable_row = start_row;
11112 while (first_reusable_row->enabled_p
11113 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
11114 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
11115 < CHARPOS (new_start)))
11116 ++first_reusable_row;
11117
11118 /* Give up if there is no row to reuse. */
11119 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
11120 || !first_reusable_row->enabled_p
11121 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
11122 != CHARPOS (new_start)))
11123 return 0;
11124
11125 /* We can reuse fully visible rows beginning with
11126 first_reusable_row to the end of the window. Set
11127 first_row_to_display to the first row that cannot be reused.
11128 Set pt_row to the row containing point, if there is any. */
11129 pt_row = NULL;
11130 for (first_row_to_display = first_reusable_row;
11131 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
11132 ++first_row_to_display)
11133 {
11134 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
11135 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
11136 pt_row = first_row_to_display;
11137 }
11138
11139 /* Start displaying at the start of first_row_to_display. */
11140 xassert (first_row_to_display->y < yb);
11141 init_to_row_start (&it, w, first_row_to_display);
11142
11143 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
11144 - start_vpos);
11145 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
11146 - nrows_scrolled);
11147 it.current_y = (first_row_to_display->y - first_reusable_row->y
11148 + WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
11149
11150 /* Display lines beginning with first_row_to_display in the
11151 desired matrix. Set last_text_row to the last row displayed
11152 that displays text. */
11153 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
11154 if (pt_row == NULL)
11155 w->cursor.vpos = -1;
11156 last_text_row = NULL;
11157 while (it.current_y < it.last_visible_y && !fonts_changed_p)
11158 if (display_line (&it))
11159 last_text_row = it.glyph_row - 1;
11160
11161 /* Give up If point isn't in a row displayed or reused. */
11162 if (w->cursor.vpos < 0)
11163 {
11164 clear_glyph_matrix (w->desired_matrix);
11165 return 0;
11166 }
11167
11168 /* If point is in a reused row, adjust y and vpos of the cursor
11169 position. */
11170 if (pt_row)
11171 {
11172 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
11173 w->current_matrix);
11174 w->cursor.y -= first_reusable_row->y;
11175 }
11176
11177 /* Scroll the display. */
11178 run.current_y = first_reusable_row->y;
11179 run.desired_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
11180 run.height = it.last_visible_y - run.current_y;
11181 dy = run.current_y - run.desired_y;
11182
11183 if (run.height)
11184 {
11185 struct frame *f = XFRAME (WINDOW_FRAME (w));
11186 update_begin (f);
11187 rif->update_window_begin_hook (w);
11188 rif->clear_mouse_face (w);
11189 rif->scroll_run_hook (w, &run);
11190 rif->update_window_end_hook (w, 0, 0);
11191 update_end (f);
11192 }
11193
11194 /* Adjust Y positions of reused rows. */
11195 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
11196 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
11197 max_y = it.last_visible_y;
11198 for (row = first_reusable_row; row < first_row_to_display; ++row)
11199 {
11200 row->y -= dy;
11201 row->visible_height = row->height;
11202 if (row->y < min_y)
11203 row->visible_height -= min_y - row->y;
11204 if (row->y + row->height > max_y)
11205 row->visible_height -= row->y + row->height - max_y;
11206 }
11207
11208 /* Scroll the current matrix. */
11209 xassert (nrows_scrolled > 0);
11210 rotate_matrix (w->current_matrix,
11211 start_vpos,
11212 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
11213 -nrows_scrolled);
11214
11215 /* Disable rows not reused. */
11216 for (row -= nrows_scrolled; row < bottom_row; ++row)
11217 row->enabled_p = 0;
11218
11219 /* Adjust window end. A null value of last_text_row means that
11220 the window end is in reused rows which in turn means that
11221 only its vpos can have changed. */
11222 if (last_text_row)
11223 {
11224 w->window_end_bytepos
11225 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
11226 w->window_end_pos
11227 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
11228 w->window_end_vpos
11229 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
11230 }
11231 else
11232 {
11233 w->window_end_vpos
11234 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
11235 }
11236
11237 w->window_end_valid = Qnil;
11238 w->desired_matrix->no_scrolling_p = 1;
11239
11240 #if GLYPH_DEBUG
11241 debug_method_add (w, "try_window_reusing_current_matrix 2");
11242 #endif
11243 return 1;
11244 }
11245
11246 return 0;
11247 }
11248
11249
11250 \f
11251 /************************************************************************
11252 Window redisplay reusing current matrix when buffer has changed
11253 ************************************************************************/
11254
11255 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
11256 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
11257 int *, int *));
11258 static struct glyph_row *
11259 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
11260 struct glyph_row *));
11261
11262
11263 /* Return the last row in MATRIX displaying text. If row START is
11264 non-null, start searching with that row. IT gives the dimensions
11265 of the display. Value is null if matrix is empty; otherwise it is
11266 a pointer to the row found. */
11267
11268 static struct glyph_row *
11269 find_last_row_displaying_text (matrix, it, start)
11270 struct glyph_matrix *matrix;
11271 struct it *it;
11272 struct glyph_row *start;
11273 {
11274 struct glyph_row *row, *row_found;
11275
11276 /* Set row_found to the last row in IT->w's current matrix
11277 displaying text. The loop looks funny but think of partially
11278 visible lines. */
11279 row_found = NULL;
11280 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
11281 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
11282 {
11283 xassert (row->enabled_p);
11284 row_found = row;
11285 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
11286 break;
11287 ++row;
11288 }
11289
11290 return row_found;
11291 }
11292
11293
11294 /* Return the last row in the current matrix of W that is not affected
11295 by changes at the start of current_buffer that occurred since W's
11296 current matrix was built. Value is null if no such row exists.
11297
11298 BEG_UNCHANGED us the number of characters unchanged at the start of
11299 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
11300 first changed character in current_buffer. Characters at positions <
11301 BEG + BEG_UNCHANGED are at the same buffer positions as they were
11302 when the current matrix was built. */
11303
11304 static struct glyph_row *
11305 find_last_unchanged_at_beg_row (w)
11306 struct window *w;
11307 {
11308 int first_changed_pos = BEG + BEG_UNCHANGED;
11309 struct glyph_row *row;
11310 struct glyph_row *row_found = NULL;
11311 int yb = window_text_bottom_y (w);
11312
11313 /* Find the last row displaying unchanged text. */
11314 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11315 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
11316 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
11317 {
11318 if (/* If row ends before first_changed_pos, it is unchanged,
11319 except in some case. */
11320 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
11321 /* When row ends in ZV and we write at ZV it is not
11322 unchanged. */
11323 && !row->ends_at_zv_p
11324 /* When first_changed_pos is the end of a continued line,
11325 row is not unchanged because it may be no longer
11326 continued. */
11327 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
11328 && row->continued_p))
11329 row_found = row;
11330
11331 /* Stop if last visible row. */
11332 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
11333 break;
11334
11335 ++row;
11336 }
11337
11338 return row_found;
11339 }
11340
11341
11342 /* Find the first glyph row in the current matrix of W that is not
11343 affected by changes at the end of current_buffer since the
11344 time W's current matrix was built.
11345
11346 Return in *DELTA the number of chars by which buffer positions in
11347 unchanged text at the end of current_buffer must be adjusted.
11348
11349 Return in *DELTA_BYTES the corresponding number of bytes.
11350
11351 Value is null if no such row exists, i.e. all rows are affected by
11352 changes. */
11353
11354 static struct glyph_row *
11355 find_first_unchanged_at_end_row (w, delta, delta_bytes)
11356 struct window *w;
11357 int *delta, *delta_bytes;
11358 {
11359 struct glyph_row *row;
11360 struct glyph_row *row_found = NULL;
11361
11362 *delta = *delta_bytes = 0;
11363
11364 /* Display must not have been paused, otherwise the current matrix
11365 is not up to date. */
11366 if (NILP (w->window_end_valid))
11367 abort ();
11368
11369 /* A value of window_end_pos >= END_UNCHANGED means that the window
11370 end is in the range of changed text. If so, there is no
11371 unchanged row at the end of W's current matrix. */
11372 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
11373 return NULL;
11374
11375 /* Set row to the last row in W's current matrix displaying text. */
11376 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
11377
11378 /* If matrix is entirely empty, no unchanged row exists. */
11379 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
11380 {
11381 /* The value of row is the last glyph row in the matrix having a
11382 meaningful buffer position in it. The end position of row
11383 corresponds to window_end_pos. This allows us to translate
11384 buffer positions in the current matrix to current buffer
11385 positions for characters not in changed text. */
11386 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
11387 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
11388 int last_unchanged_pos, last_unchanged_pos_old;
11389 struct glyph_row *first_text_row
11390 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11391
11392 *delta = Z - Z_old;
11393 *delta_bytes = Z_BYTE - Z_BYTE_old;
11394
11395 /* Set last_unchanged_pos to the buffer position of the last
11396 character in the buffer that has not been changed. Z is the
11397 index + 1 of the last character in current_buffer, i.e. by
11398 subtracting END_UNCHANGED we get the index of the last
11399 unchanged character, and we have to add BEG to get its buffer
11400 position. */
11401 last_unchanged_pos = Z - END_UNCHANGED + BEG;
11402 last_unchanged_pos_old = last_unchanged_pos - *delta;
11403
11404 /* Search backward from ROW for a row displaying a line that
11405 starts at a minimum position >= last_unchanged_pos_old. */
11406 for (; row > first_text_row; --row)
11407 {
11408 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
11409 abort ();
11410
11411 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
11412 row_found = row;
11413 }
11414 }
11415
11416 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
11417 abort ();
11418
11419 return row_found;
11420 }
11421
11422
11423 /* Make sure that glyph rows in the current matrix of window W
11424 reference the same glyph memory as corresponding rows in the
11425 frame's frame matrix. This function is called after scrolling W's
11426 current matrix on a terminal frame in try_window_id and
11427 try_window_reusing_current_matrix. */
11428
11429 static void
11430 sync_frame_with_window_matrix_rows (w)
11431 struct window *w;
11432 {
11433 struct frame *f = XFRAME (w->frame);
11434 struct glyph_row *window_row, *window_row_end, *frame_row;
11435
11436 /* Preconditions: W must be a leaf window and full-width. Its frame
11437 must have a frame matrix. */
11438 xassert (NILP (w->hchild) && NILP (w->vchild));
11439 xassert (WINDOW_FULL_WIDTH_P (w));
11440 xassert (!FRAME_WINDOW_P (f));
11441
11442 /* If W is a full-width window, glyph pointers in W's current matrix
11443 have, by definition, to be the same as glyph pointers in the
11444 corresponding frame matrix. Note that frame matrices have no
11445 marginal areas (see build_frame_matrix). */
11446 window_row = w->current_matrix->rows;
11447 window_row_end = window_row + w->current_matrix->nrows;
11448 frame_row = f->current_matrix->rows + XFASTINT (w->top);
11449 while (window_row < window_row_end)
11450 {
11451 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
11452 struct glyph *end = window_row->glyphs[LAST_AREA];
11453
11454 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
11455 frame_row->glyphs[TEXT_AREA] = start;
11456 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
11457 frame_row->glyphs[LAST_AREA] = end;
11458
11459 /* Disable frame rows whose corresponding window rows have
11460 been disabled in try_window_id. */
11461 if (!window_row->enabled_p)
11462 frame_row->enabled_p = 0;
11463
11464 ++window_row, ++frame_row;
11465 }
11466 }
11467
11468
11469 /* Find the glyph row in window W containing CHARPOS. Consider all
11470 rows between START and END (not inclusive). END null means search
11471 all rows to the end of the display area of W. Value is the row
11472 containing CHARPOS or null. */
11473
11474 struct glyph_row *
11475 row_containing_pos (w, charpos, start, end, dy)
11476 struct window *w;
11477 int charpos;
11478 struct glyph_row *start, *end;
11479 int dy;
11480 {
11481 struct glyph_row *row = start;
11482 int last_y;
11483
11484 /* If we happen to start on a header-line, skip that. */
11485 if (row->mode_line_p)
11486 ++row;
11487
11488 if ((end && row >= end) || !row->enabled_p)
11489 return NULL;
11490
11491 last_y = window_text_bottom_y (w) - dy;
11492
11493 while ((end == NULL || row < end)
11494 && MATRIX_ROW_BOTTOM_Y (row) < last_y
11495 && (MATRIX_ROW_END_CHARPOS (row) < charpos
11496 || (MATRIX_ROW_END_CHARPOS (row) == charpos
11497 /* The end position of a row equals the start
11498 position of the next row. If CHARPOS is there, we
11499 would rather display it in the next line, except
11500 when this line ends in ZV. */
11501 && !row->ends_at_zv_p
11502 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))
11503 ++row;
11504
11505 /* Give up if CHARPOS not found. */
11506 if ((end && row >= end)
11507 || charpos < MATRIX_ROW_START_CHARPOS (row)
11508 || charpos > MATRIX_ROW_END_CHARPOS (row))
11509 row = NULL;
11510
11511 return row;
11512 }
11513
11514
11515 /* Try to redisplay window W by reusing its existing display. W's
11516 current matrix must be up to date when this function is called,
11517 i.e. window_end_valid must not be nil.
11518
11519 Value is
11520
11521 1 if display has been updated
11522 0 if otherwise unsuccessful
11523 -1 if redisplay with same window start is known not to succeed
11524
11525 The following steps are performed:
11526
11527 1. Find the last row in the current matrix of W that is not
11528 affected by changes at the start of current_buffer. If no such row
11529 is found, give up.
11530
11531 2. Find the first row in W's current matrix that is not affected by
11532 changes at the end of current_buffer. Maybe there is no such row.
11533
11534 3. Display lines beginning with the row + 1 found in step 1 to the
11535 row found in step 2 or, if step 2 didn't find a row, to the end of
11536 the window.
11537
11538 4. If cursor is not known to appear on the window, give up.
11539
11540 5. If display stopped at the row found in step 2, scroll the
11541 display and current matrix as needed.
11542
11543 6. Maybe display some lines at the end of W, if we must. This can
11544 happen under various circumstances, like a partially visible line
11545 becoming fully visible, or because newly displayed lines are displayed
11546 in smaller font sizes.
11547
11548 7. Update W's window end information. */
11549
11550 static int
11551 try_window_id (w)
11552 struct window *w;
11553 {
11554 struct frame *f = XFRAME (w->frame);
11555 struct glyph_matrix *current_matrix = w->current_matrix;
11556 struct glyph_matrix *desired_matrix = w->desired_matrix;
11557 struct glyph_row *last_unchanged_at_beg_row;
11558 struct glyph_row *first_unchanged_at_end_row;
11559 struct glyph_row *row;
11560 struct glyph_row *bottom_row;
11561 int bottom_vpos;
11562 struct it it;
11563 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
11564 struct text_pos start_pos;
11565 struct run run;
11566 int first_unchanged_at_end_vpos = 0;
11567 struct glyph_row *last_text_row, *last_text_row_at_end;
11568 struct text_pos start;
11569 int first_changed_charpos, last_changed_charpos;
11570
11571 #if GLYPH_DEBUG
11572 if (inhibit_try_window_id)
11573 return 0;
11574 #endif
11575
11576 /* This is handy for debugging. */
11577 #if 0
11578 #define GIVE_UP(X) \
11579 do { \
11580 fprintf (stderr, "try_window_id give up %d\n", (X)); \
11581 return 0; \
11582 } while (0)
11583 #else
11584 #define GIVE_UP(X) return 0
11585 #endif
11586
11587 SET_TEXT_POS_FROM_MARKER (start, w->start);
11588
11589 /* Don't use this for mini-windows because these can show
11590 messages and mini-buffers, and we don't handle that here. */
11591 if (MINI_WINDOW_P (w))
11592 GIVE_UP (1);
11593
11594 /* This flag is used to prevent redisplay optimizations. */
11595 if (windows_or_buffers_changed || cursor_type_changed)
11596 GIVE_UP (2);
11597
11598 /* Verify that narrowing has not changed.
11599 Also verify that we were not told to prevent redisplay optimizations.
11600 It would be nice to further
11601 reduce the number of cases where this prevents try_window_id. */
11602 if (current_buffer->clip_changed
11603 || current_buffer->prevent_redisplay_optimizations_p)
11604 GIVE_UP (3);
11605
11606 /* Window must either use window-based redisplay or be full width. */
11607 if (!FRAME_WINDOW_P (f)
11608 && (!line_ins_del_ok
11609 || !WINDOW_FULL_WIDTH_P (w)))
11610 GIVE_UP (4);
11611
11612 /* Give up if point is not known NOT to appear in W. */
11613 if (PT < CHARPOS (start))
11614 GIVE_UP (5);
11615
11616 /* Another way to prevent redisplay optimizations. */
11617 if (XFASTINT (w->last_modified) == 0)
11618 GIVE_UP (6);
11619
11620 /* Verify that window is not hscrolled. */
11621 if (XFASTINT (w->hscroll) != 0)
11622 GIVE_UP (7);
11623
11624 /* Verify that display wasn't paused. */
11625 if (NILP (w->window_end_valid))
11626 GIVE_UP (8);
11627
11628 /* Can't use this if highlighting a region because a cursor movement
11629 will do more than just set the cursor. */
11630 if (!NILP (Vtransient_mark_mode)
11631 && !NILP (current_buffer->mark_active))
11632 GIVE_UP (9);
11633
11634 /* Likewise if highlighting trailing whitespace. */
11635 if (!NILP (Vshow_trailing_whitespace))
11636 GIVE_UP (11);
11637
11638 /* Likewise if showing a region. */
11639 if (!NILP (w->region_showing))
11640 GIVE_UP (10);
11641
11642 /* Can use this if overlay arrow position and or string have changed. */
11643 if (!EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
11644 || !EQ (last_arrow_string, Voverlay_arrow_string))
11645 GIVE_UP (12);
11646
11647
11648 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
11649 only if buffer has really changed. The reason is that the gap is
11650 initially at Z for freshly visited files. The code below would
11651 set end_unchanged to 0 in that case. */
11652 if (MODIFF > SAVE_MODIFF
11653 /* This seems to happen sometimes after saving a buffer. */
11654 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
11655 {
11656 if (GPT - BEG < BEG_UNCHANGED)
11657 BEG_UNCHANGED = GPT - BEG;
11658 if (Z - GPT < END_UNCHANGED)
11659 END_UNCHANGED = Z - GPT;
11660 }
11661
11662 /* The position of the first and last character that has been changed. */
11663 first_changed_charpos = BEG + BEG_UNCHANGED;
11664 last_changed_charpos = Z - END_UNCHANGED;
11665
11666 /* If window starts after a line end, and the last change is in
11667 front of that newline, then changes don't affect the display.
11668 This case happens with stealth-fontification. Note that although
11669 the display is unchanged, glyph positions in the matrix have to
11670 be adjusted, of course. */
11671 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
11672 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
11673 && ((last_changed_charpos < CHARPOS (start)
11674 && CHARPOS (start) == BEGV)
11675 || (last_changed_charpos < CHARPOS (start) - 1
11676 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
11677 {
11678 int Z_old, delta, Z_BYTE_old, delta_bytes;
11679 struct glyph_row *r0;
11680
11681 /* Compute how many chars/bytes have been added to or removed
11682 from the buffer. */
11683 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
11684 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
11685 delta = Z - Z_old;
11686 delta_bytes = Z_BYTE - Z_BYTE_old;
11687
11688 /* Give up if PT is not in the window. Note that it already has
11689 been checked at the start of try_window_id that PT is not in
11690 front of the window start. */
11691 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
11692 GIVE_UP (13);
11693
11694 /* If window start is unchanged, we can reuse the whole matrix
11695 as is, after adjusting glyph positions. No need to compute
11696 the window end again, since its offset from Z hasn't changed. */
11697 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
11698 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
11699 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes)
11700 {
11701 /* Adjust positions in the glyph matrix. */
11702 if (delta || delta_bytes)
11703 {
11704 struct glyph_row *r1
11705 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11706 increment_matrix_positions (w->current_matrix,
11707 MATRIX_ROW_VPOS (r0, current_matrix),
11708 MATRIX_ROW_VPOS (r1, current_matrix),
11709 delta, delta_bytes);
11710 }
11711
11712 /* Set the cursor. */
11713 row = row_containing_pos (w, PT, r0, NULL, 0);
11714 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
11715 return 1;
11716 }
11717 }
11718
11719 /* Handle the case that changes are all below what is displayed in
11720 the window, and that PT is in the window. This shortcut cannot
11721 be taken if ZV is visible in the window, and text has been added
11722 there that is visible in the window. */
11723 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
11724 /* ZV is not visible in the window, or there are no
11725 changes at ZV, actually. */
11726 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
11727 || first_changed_charpos == last_changed_charpos))
11728 {
11729 struct glyph_row *r0;
11730
11731 /* Give up if PT is not in the window. Note that it already has
11732 been checked at the start of try_window_id that PT is not in
11733 front of the window start. */
11734 if (PT >= MATRIX_ROW_END_CHARPOS (row))
11735 GIVE_UP (14);
11736
11737 /* If window start is unchanged, we can reuse the whole matrix
11738 as is, without changing glyph positions since no text has
11739 been added/removed in front of the window end. */
11740 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
11741 if (TEXT_POS_EQUAL_P (start, r0->start.pos))
11742 {
11743 /* We have to compute the window end anew since text
11744 can have been added/removed after it. */
11745 w->window_end_pos
11746 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
11747 w->window_end_bytepos
11748 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11749
11750 /* Set the cursor. */
11751 row = row_containing_pos (w, PT, r0, NULL, 0);
11752 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
11753 return 2;
11754 }
11755 }
11756
11757 /* Give up if window start is in the changed area.
11758
11759 The condition used to read
11760
11761 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
11762
11763 but why that was tested escapes me at the moment. */
11764 if (CHARPOS (start) >= first_changed_charpos
11765 && CHARPOS (start) <= last_changed_charpos)
11766 GIVE_UP (15);
11767
11768 /* Check that window start agrees with the start of the first glyph
11769 row in its current matrix. Check this after we know the window
11770 start is not in changed text, otherwise positions would not be
11771 comparable. */
11772 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
11773 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
11774 GIVE_UP (16);
11775
11776 /* Give up if the window ends in strings. Overlay strings
11777 at the end are difficult to handle, so don't try. */
11778 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
11779 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
11780 GIVE_UP (20);
11781
11782 /* Compute the position at which we have to start displaying new
11783 lines. Some of the lines at the top of the window might be
11784 reusable because they are not displaying changed text. Find the
11785 last row in W's current matrix not affected by changes at the
11786 start of current_buffer. Value is null if changes start in the
11787 first line of window. */
11788 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
11789 if (last_unchanged_at_beg_row)
11790 {
11791 /* Avoid starting to display in the moddle of a character, a TAB
11792 for instance. This is easier than to set up the iterator
11793 exactly, and it's not a frequent case, so the additional
11794 effort wouldn't really pay off. */
11795 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
11796 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
11797 && last_unchanged_at_beg_row > w->current_matrix->rows)
11798 --last_unchanged_at_beg_row;
11799
11800 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
11801 GIVE_UP (17);
11802
11803 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
11804 GIVE_UP (18);
11805 start_pos = it.current.pos;
11806
11807 /* Start displaying new lines in the desired matrix at the same
11808 vpos we would use in the current matrix, i.e. below
11809 last_unchanged_at_beg_row. */
11810 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
11811 current_matrix);
11812 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11813 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
11814
11815 xassert (it.hpos == 0 && it.current_x == 0);
11816 }
11817 else
11818 {
11819 /* There are no reusable lines at the start of the window.
11820 Start displaying in the first line. */
11821 start_display (&it, w, start);
11822 start_pos = it.current.pos;
11823 }
11824
11825 /* Find the first row that is not affected by changes at the end of
11826 the buffer. Value will be null if there is no unchanged row, in
11827 which case we must redisplay to the end of the window. delta
11828 will be set to the value by which buffer positions beginning with
11829 first_unchanged_at_end_row have to be adjusted due to text
11830 changes. */
11831 first_unchanged_at_end_row
11832 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
11833 IF_DEBUG (debug_delta = delta);
11834 IF_DEBUG (debug_delta_bytes = delta_bytes);
11835
11836 /* Set stop_pos to the buffer position up to which we will have to
11837 display new lines. If first_unchanged_at_end_row != NULL, this
11838 is the buffer position of the start of the line displayed in that
11839 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
11840 that we don't stop at a buffer position. */
11841 stop_pos = 0;
11842 if (first_unchanged_at_end_row)
11843 {
11844 xassert (last_unchanged_at_beg_row == NULL
11845 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
11846
11847 /* If this is a continuation line, move forward to the next one
11848 that isn't. Changes in lines above affect this line.
11849 Caution: this may move first_unchanged_at_end_row to a row
11850 not displaying text. */
11851 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
11852 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11853 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11854 < it.last_visible_y))
11855 ++first_unchanged_at_end_row;
11856
11857 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11858 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11859 >= it.last_visible_y))
11860 first_unchanged_at_end_row = NULL;
11861 else
11862 {
11863 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
11864 + delta);
11865 first_unchanged_at_end_vpos
11866 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
11867 xassert (stop_pos >= Z - END_UNCHANGED);
11868 }
11869 }
11870 else if (last_unchanged_at_beg_row == NULL)
11871 GIVE_UP (19);
11872
11873
11874 #if GLYPH_DEBUG
11875
11876 /* Either there is no unchanged row at the end, or the one we have
11877 now displays text. This is a necessary condition for the window
11878 end pos calculation at the end of this function. */
11879 xassert (first_unchanged_at_end_row == NULL
11880 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
11881
11882 debug_last_unchanged_at_beg_vpos
11883 = (last_unchanged_at_beg_row
11884 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
11885 : -1);
11886 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
11887
11888 #endif /* GLYPH_DEBUG != 0 */
11889
11890
11891 /* Display new lines. Set last_text_row to the last new line
11892 displayed which has text on it, i.e. might end up as being the
11893 line where the window_end_vpos is. */
11894 w->cursor.vpos = -1;
11895 last_text_row = NULL;
11896 overlay_arrow_seen = 0;
11897 while (it.current_y < it.last_visible_y
11898 && !fonts_changed_p
11899 && (first_unchanged_at_end_row == NULL
11900 || IT_CHARPOS (it) < stop_pos))
11901 {
11902 if (display_line (&it))
11903 last_text_row = it.glyph_row - 1;
11904 }
11905
11906 if (fonts_changed_p)
11907 return -1;
11908
11909
11910 /* Compute differences in buffer positions, y-positions etc. for
11911 lines reused at the bottom of the window. Compute what we can
11912 scroll. */
11913 if (first_unchanged_at_end_row
11914 /* No lines reused because we displayed everything up to the
11915 bottom of the window. */
11916 && it.current_y < it.last_visible_y)
11917 {
11918 dvpos = (it.vpos
11919 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
11920 current_matrix));
11921 dy = it.current_y - first_unchanged_at_end_row->y;
11922 run.current_y = first_unchanged_at_end_row->y;
11923 run.desired_y = run.current_y + dy;
11924 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
11925 }
11926 else
11927 {
11928 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
11929 first_unchanged_at_end_row = NULL;
11930 }
11931 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
11932
11933
11934 /* Find the cursor if not already found. We have to decide whether
11935 PT will appear on this window (it sometimes doesn't, but this is
11936 not a very frequent case.) This decision has to be made before
11937 the current matrix is altered. A value of cursor.vpos < 0 means
11938 that PT is either in one of the lines beginning at
11939 first_unchanged_at_end_row or below the window. Don't care for
11940 lines that might be displayed later at the window end; as
11941 mentioned, this is not a frequent case. */
11942 if (w->cursor.vpos < 0)
11943 {
11944 /* Cursor in unchanged rows at the top? */
11945 if (PT < CHARPOS (start_pos)
11946 && last_unchanged_at_beg_row)
11947 {
11948 row = row_containing_pos (w, PT,
11949 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
11950 last_unchanged_at_beg_row + 1, 0);
11951 if (row)
11952 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11953 }
11954
11955 /* Start from first_unchanged_at_end_row looking for PT. */
11956 else if (first_unchanged_at_end_row)
11957 {
11958 row = row_containing_pos (w, PT - delta,
11959 first_unchanged_at_end_row, NULL, 0);
11960 if (row)
11961 set_cursor_from_row (w, row, w->current_matrix, delta,
11962 delta_bytes, dy, dvpos);
11963 }
11964
11965 /* Give up if cursor was not found. */
11966 if (w->cursor.vpos < 0)
11967 {
11968 clear_glyph_matrix (w->desired_matrix);
11969 return -1;
11970 }
11971 }
11972
11973 /* Don't let the cursor end in the scroll margins. */
11974 {
11975 int this_scroll_margin, cursor_height;
11976
11977 this_scroll_margin = max (0, scroll_margin);
11978 this_scroll_margin = min (this_scroll_margin,
11979 XFASTINT (w->height) / 4);
11980 this_scroll_margin *= CANON_Y_UNIT (it.f);
11981 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
11982
11983 if ((w->cursor.y < this_scroll_margin
11984 && CHARPOS (start) > BEGV)
11985 /* Don't take scroll margin into account at the bottom because
11986 old redisplay didn't do it either. */
11987 || w->cursor.y + cursor_height > it.last_visible_y)
11988 {
11989 w->cursor.vpos = -1;
11990 clear_glyph_matrix (w->desired_matrix);
11991 return -1;
11992 }
11993 }
11994
11995 /* Scroll the display. Do it before changing the current matrix so
11996 that xterm.c doesn't get confused about where the cursor glyph is
11997 found. */
11998 if (dy && run.height)
11999 {
12000 update_begin (f);
12001
12002 if (FRAME_WINDOW_P (f))
12003 {
12004 rif->update_window_begin_hook (w);
12005 rif->clear_mouse_face (w);
12006 rif->scroll_run_hook (w, &run);
12007 rif->update_window_end_hook (w, 0, 0);
12008 }
12009 else
12010 {
12011 /* Terminal frame. In this case, dvpos gives the number of
12012 lines to scroll by; dvpos < 0 means scroll up. */
12013 int first_unchanged_at_end_vpos
12014 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
12015 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
12016 int end = (XFASTINT (w->top)
12017 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
12018 + window_internal_height (w));
12019
12020 /* Perform the operation on the screen. */
12021 if (dvpos > 0)
12022 {
12023 /* Scroll last_unchanged_at_beg_row to the end of the
12024 window down dvpos lines. */
12025 set_terminal_window (end);
12026
12027 /* On dumb terminals delete dvpos lines at the end
12028 before inserting dvpos empty lines. */
12029 if (!scroll_region_ok)
12030 ins_del_lines (end - dvpos, -dvpos);
12031
12032 /* Insert dvpos empty lines in front of
12033 last_unchanged_at_beg_row. */
12034 ins_del_lines (from, dvpos);
12035 }
12036 else if (dvpos < 0)
12037 {
12038 /* Scroll up last_unchanged_at_beg_vpos to the end of
12039 the window to last_unchanged_at_beg_vpos - |dvpos|. */
12040 set_terminal_window (end);
12041
12042 /* Delete dvpos lines in front of
12043 last_unchanged_at_beg_vpos. ins_del_lines will set
12044 the cursor to the given vpos and emit |dvpos| delete
12045 line sequences. */
12046 ins_del_lines (from + dvpos, dvpos);
12047
12048 /* On a dumb terminal insert dvpos empty lines at the
12049 end. */
12050 if (!scroll_region_ok)
12051 ins_del_lines (end + dvpos, -dvpos);
12052 }
12053
12054 set_terminal_window (0);
12055 }
12056
12057 update_end (f);
12058 }
12059
12060 /* Shift reused rows of the current matrix to the right position.
12061 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
12062 text. */
12063 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
12064 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
12065 if (dvpos < 0)
12066 {
12067 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
12068 bottom_vpos, dvpos);
12069 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
12070 bottom_vpos, 0);
12071 }
12072 else if (dvpos > 0)
12073 {
12074 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
12075 bottom_vpos, dvpos);
12076 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
12077 first_unchanged_at_end_vpos + dvpos, 0);
12078 }
12079
12080 /* For frame-based redisplay, make sure that current frame and window
12081 matrix are in sync with respect to glyph memory. */
12082 if (!FRAME_WINDOW_P (f))
12083 sync_frame_with_window_matrix_rows (w);
12084
12085 /* Adjust buffer positions in reused rows. */
12086 if (delta)
12087 increment_matrix_positions (current_matrix,
12088 first_unchanged_at_end_vpos + dvpos,
12089 bottom_vpos, delta, delta_bytes);
12090
12091 /* Adjust Y positions. */
12092 if (dy)
12093 shift_glyph_matrix (w, current_matrix,
12094 first_unchanged_at_end_vpos + dvpos,
12095 bottom_vpos, dy);
12096
12097 if (first_unchanged_at_end_row)
12098 first_unchanged_at_end_row += dvpos;
12099
12100 /* If scrolling up, there may be some lines to display at the end of
12101 the window. */
12102 last_text_row_at_end = NULL;
12103 if (dy < 0)
12104 {
12105 /* Scrolling up can leave for example a partially visible line
12106 at the end of the window to be redisplayed. */
12107 /* Set last_row to the glyph row in the current matrix where the
12108 window end line is found. It has been moved up or down in
12109 the matrix by dvpos. */
12110 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
12111 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
12112
12113 /* If last_row is the window end line, it should display text. */
12114 xassert (last_row->displays_text_p);
12115
12116 /* If window end line was partially visible before, begin
12117 displaying at that line. Otherwise begin displaying with the
12118 line following it. */
12119 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
12120 {
12121 init_to_row_start (&it, w, last_row);
12122 it.vpos = last_vpos;
12123 it.current_y = last_row->y;
12124 }
12125 else
12126 {
12127 init_to_row_end (&it, w, last_row);
12128 it.vpos = 1 + last_vpos;
12129 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
12130 ++last_row;
12131 }
12132
12133 /* We may start in a continuation line. If so, we have to
12134 get the right continuation_lines_width and current_x. */
12135 it.continuation_lines_width = last_row->continuation_lines_width;
12136 it.hpos = it.current_x = 0;
12137
12138 /* Display the rest of the lines at the window end. */
12139 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
12140 while (it.current_y < it.last_visible_y
12141 && !fonts_changed_p)
12142 {
12143 /* Is it always sure that the display agrees with lines in
12144 the current matrix? I don't think so, so we mark rows
12145 displayed invalid in the current matrix by setting their
12146 enabled_p flag to zero. */
12147 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
12148 if (display_line (&it))
12149 last_text_row_at_end = it.glyph_row - 1;
12150 }
12151 }
12152
12153 /* Update window_end_pos and window_end_vpos. */
12154 if (first_unchanged_at_end_row
12155 && first_unchanged_at_end_row->y < it.last_visible_y
12156 && !last_text_row_at_end)
12157 {
12158 /* Window end line if one of the preserved rows from the current
12159 matrix. Set row to the last row displaying text in current
12160 matrix starting at first_unchanged_at_end_row, after
12161 scrolling. */
12162 xassert (first_unchanged_at_end_row->displays_text_p);
12163 row = find_last_row_displaying_text (w->current_matrix, &it,
12164 first_unchanged_at_end_row);
12165 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
12166
12167 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
12168 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
12169 w->window_end_vpos
12170 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
12171 xassert (w->window_end_bytepos >= 0);
12172 IF_DEBUG (debug_method_add (w, "A"));
12173 }
12174 else if (last_text_row_at_end)
12175 {
12176 w->window_end_pos
12177 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
12178 w->window_end_bytepos
12179 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
12180 w->window_end_vpos
12181 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
12182 xassert (w->window_end_bytepos >= 0);
12183 IF_DEBUG (debug_method_add (w, "B"));
12184 }
12185 else if (last_text_row)
12186 {
12187 /* We have displayed either to the end of the window or at the
12188 end of the window, i.e. the last row with text is to be found
12189 in the desired matrix. */
12190 w->window_end_pos
12191 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12192 w->window_end_bytepos
12193 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12194 w->window_end_vpos
12195 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
12196 xassert (w->window_end_bytepos >= 0);
12197 }
12198 else if (first_unchanged_at_end_row == NULL
12199 && last_text_row == NULL
12200 && last_text_row_at_end == NULL)
12201 {
12202 /* Displayed to end of window, but no line containing text was
12203 displayed. Lines were deleted at the end of the window. */
12204 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
12205 int vpos = XFASTINT (w->window_end_vpos);
12206 struct glyph_row *current_row = current_matrix->rows + vpos;
12207 struct glyph_row *desired_row = desired_matrix->rows + vpos;
12208
12209 for (row = NULL;
12210 row == NULL && vpos >= first_vpos;
12211 --vpos, --current_row, --desired_row)
12212 {
12213 if (desired_row->enabled_p)
12214 {
12215 if (desired_row->displays_text_p)
12216 row = desired_row;
12217 }
12218 else if (current_row->displays_text_p)
12219 row = current_row;
12220 }
12221
12222 xassert (row != NULL);
12223 w->window_end_vpos = make_number (vpos + 1);
12224 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
12225 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
12226 xassert (w->window_end_bytepos >= 0);
12227 IF_DEBUG (debug_method_add (w, "C"));
12228 }
12229 else
12230 abort ();
12231
12232 #if 0 /* This leads to problems, for instance when the cursor is
12233 at ZV, and the cursor line displays no text. */
12234 /* Disable rows below what's displayed in the window. This makes
12235 debugging easier. */
12236 enable_glyph_matrix_rows (current_matrix,
12237 XFASTINT (w->window_end_vpos) + 1,
12238 bottom_vpos, 0);
12239 #endif
12240
12241 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
12242 debug_end_vpos = XFASTINT (w->window_end_vpos));
12243
12244 /* Record that display has not been completed. */
12245 w->window_end_valid = Qnil;
12246 w->desired_matrix->no_scrolling_p = 1;
12247 return 3;
12248
12249 #undef GIVE_UP
12250 }
12251
12252
12253 \f
12254 /***********************************************************************
12255 More debugging support
12256 ***********************************************************************/
12257
12258 #if GLYPH_DEBUG
12259
12260 void dump_glyph_row P_ ((struct glyph_row *, int, int));
12261 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
12262 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
12263
12264
12265 /* Dump the contents of glyph matrix MATRIX on stderr.
12266
12267 GLYPHS 0 means don't show glyph contents.
12268 GLYPHS 1 means show glyphs in short form
12269 GLYPHS > 1 means show glyphs in long form. */
12270
12271 void
12272 dump_glyph_matrix (matrix, glyphs)
12273 struct glyph_matrix *matrix;
12274 int glyphs;
12275 {
12276 int i;
12277 for (i = 0; i < matrix->nrows; ++i)
12278 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
12279 }
12280
12281
12282 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
12283 the glyph row and area where the glyph comes from. */
12284
12285 void
12286 dump_glyph (row, glyph, area)
12287 struct glyph_row *row;
12288 struct glyph *glyph;
12289 int area;
12290 {
12291 if (glyph->type == CHAR_GLYPH)
12292 {
12293 fprintf (stderr,
12294 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12295 glyph - row->glyphs[TEXT_AREA],
12296 'C',
12297 glyph->charpos,
12298 (BUFFERP (glyph->object)
12299 ? 'B'
12300 : (STRINGP (glyph->object)
12301 ? 'S'
12302 : '-')),
12303 glyph->pixel_width,
12304 glyph->u.ch,
12305 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
12306 ? glyph->u.ch
12307 : '.'),
12308 glyph->face_id,
12309 glyph->left_box_line_p,
12310 glyph->right_box_line_p);
12311 }
12312 else if (glyph->type == STRETCH_GLYPH)
12313 {
12314 fprintf (stderr,
12315 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12316 glyph - row->glyphs[TEXT_AREA],
12317 'S',
12318 glyph->charpos,
12319 (BUFFERP (glyph->object)
12320 ? 'B'
12321 : (STRINGP (glyph->object)
12322 ? 'S'
12323 : '-')),
12324 glyph->pixel_width,
12325 0,
12326 '.',
12327 glyph->face_id,
12328 glyph->left_box_line_p,
12329 glyph->right_box_line_p);
12330 }
12331 else if (glyph->type == IMAGE_GLYPH)
12332 {
12333 fprintf (stderr,
12334 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12335 glyph - row->glyphs[TEXT_AREA],
12336 'I',
12337 glyph->charpos,
12338 (BUFFERP (glyph->object)
12339 ? 'B'
12340 : (STRINGP (glyph->object)
12341 ? 'S'
12342 : '-')),
12343 glyph->pixel_width,
12344 glyph->u.img_id,
12345 '.',
12346 glyph->face_id,
12347 glyph->left_box_line_p,
12348 glyph->right_box_line_p);
12349 }
12350 }
12351
12352
12353 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
12354 GLYPHS 0 means don't show glyph contents.
12355 GLYPHS 1 means show glyphs in short form
12356 GLYPHS > 1 means show glyphs in long form. */
12357
12358 void
12359 dump_glyph_row (row, vpos, glyphs)
12360 struct glyph_row *row;
12361 int vpos, glyphs;
12362 {
12363 if (glyphs != 1)
12364 {
12365 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
12366 fprintf (stderr, "=======================================================================\n");
12367
12368 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d\
12369 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
12370 vpos,
12371 MATRIX_ROW_START_CHARPOS (row),
12372 MATRIX_ROW_END_CHARPOS (row),
12373 row->used[TEXT_AREA],
12374 row->contains_overlapping_glyphs_p,
12375 row->enabled_p,
12376 row->truncated_on_left_p,
12377 row->truncated_on_right_p,
12378 row->overlay_arrow_p,
12379 row->continued_p,
12380 MATRIX_ROW_CONTINUATION_LINE_P (row),
12381 row->displays_text_p,
12382 row->ends_at_zv_p,
12383 row->fill_line_p,
12384 row->ends_in_middle_of_char_p,
12385 row->starts_in_middle_of_char_p,
12386 row->mouse_face_p,
12387 row->x,
12388 row->y,
12389 row->pixel_width,
12390 row->height,
12391 row->visible_height,
12392 row->ascent,
12393 row->phys_ascent);
12394 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
12395 row->end.overlay_string_index,
12396 row->continuation_lines_width);
12397 fprintf (stderr, "%9d %5d\n",
12398 CHARPOS (row->start.string_pos),
12399 CHARPOS (row->end.string_pos));
12400 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
12401 row->end.dpvec_index);
12402 }
12403
12404 if (glyphs > 1)
12405 {
12406 int area;
12407
12408 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12409 {
12410 struct glyph *glyph = row->glyphs[area];
12411 struct glyph *glyph_end = glyph + row->used[area];
12412
12413 /* Glyph for a line end in text. */
12414 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
12415 ++glyph_end;
12416
12417 if (glyph < glyph_end)
12418 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
12419
12420 for (; glyph < glyph_end; ++glyph)
12421 dump_glyph (row, glyph, area);
12422 }
12423 }
12424 else if (glyphs == 1)
12425 {
12426 int area;
12427
12428 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12429 {
12430 char *s = (char *) alloca (row->used[area] + 1);
12431 int i;
12432
12433 for (i = 0; i < row->used[area]; ++i)
12434 {
12435 struct glyph *glyph = row->glyphs[area] + i;
12436 if (glyph->type == CHAR_GLYPH
12437 && glyph->u.ch < 0x80
12438 && glyph->u.ch >= ' ')
12439 s[i] = glyph->u.ch;
12440 else
12441 s[i] = '.';
12442 }
12443
12444 s[i] = '\0';
12445 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
12446 }
12447 }
12448 }
12449
12450
12451 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
12452 Sdump_glyph_matrix, 0, 1, "p",
12453 doc: /* Dump the current matrix of the selected window to stderr.
12454 Shows contents of glyph row structures. With non-nil
12455 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
12456 glyphs in short form, otherwise show glyphs in long form. */)
12457 (glyphs)
12458 Lisp_Object glyphs;
12459 {
12460 struct window *w = XWINDOW (selected_window);
12461 struct buffer *buffer = XBUFFER (w->buffer);
12462
12463 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
12464 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
12465 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
12466 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
12467 fprintf (stderr, "=============================================\n");
12468 dump_glyph_matrix (w->current_matrix,
12469 NILP (glyphs) ? 0 : XINT (glyphs));
12470 return Qnil;
12471 }
12472
12473
12474 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
12475 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
12476 ()
12477 {
12478 struct frame *f = XFRAME (selected_frame);
12479 dump_glyph_matrix (f->current_matrix, 1);
12480 return Qnil;
12481 }
12482
12483
12484 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
12485 doc: /* Dump glyph row ROW to stderr.
12486 GLYPH 0 means don't dump glyphs.
12487 GLYPH 1 means dump glyphs in short form.
12488 GLYPH > 1 or omitted means dump glyphs in long form. */)
12489 (row, glyphs)
12490 Lisp_Object row, glyphs;
12491 {
12492 struct glyph_matrix *matrix;
12493 int vpos;
12494
12495 CHECK_NUMBER (row);
12496 matrix = XWINDOW (selected_window)->current_matrix;
12497 vpos = XINT (row);
12498 if (vpos >= 0 && vpos < matrix->nrows)
12499 dump_glyph_row (MATRIX_ROW (matrix, vpos),
12500 vpos,
12501 INTEGERP (glyphs) ? XINT (glyphs) : 2);
12502 return Qnil;
12503 }
12504
12505
12506 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
12507 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
12508 GLYPH 0 means don't dump glyphs.
12509 GLYPH 1 means dump glyphs in short form.
12510 GLYPH > 1 or omitted means dump glyphs in long form. */)
12511 (row, glyphs)
12512 Lisp_Object row, glyphs;
12513 {
12514 struct frame *sf = SELECTED_FRAME ();
12515 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
12516 int vpos;
12517
12518 CHECK_NUMBER (row);
12519 vpos = XINT (row);
12520 if (vpos >= 0 && vpos < m->nrows)
12521 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
12522 INTEGERP (glyphs) ? XINT (glyphs) : 2);
12523 return Qnil;
12524 }
12525
12526
12527 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
12528 doc: /* Toggle tracing of redisplay.
12529 With ARG, turn tracing on if and only if ARG is positive. */)
12530 (arg)
12531 Lisp_Object arg;
12532 {
12533 if (NILP (arg))
12534 trace_redisplay_p = !trace_redisplay_p;
12535 else
12536 {
12537 arg = Fprefix_numeric_value (arg);
12538 trace_redisplay_p = XINT (arg) > 0;
12539 }
12540
12541 return Qnil;
12542 }
12543
12544
12545 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
12546 doc: /* Like `format', but print result to stderr.
12547 usage: (trace-to-stderr STRING &rest OBJECTS) */)
12548 (nargs, args)
12549 int nargs;
12550 Lisp_Object *args;
12551 {
12552 Lisp_Object s = Fformat (nargs, args);
12553 fprintf (stderr, "%s", SDATA (s));
12554 return Qnil;
12555 }
12556
12557 #endif /* GLYPH_DEBUG */
12558
12559
12560 \f
12561 /***********************************************************************
12562 Building Desired Matrix Rows
12563 ***********************************************************************/
12564
12565 /* Return a temporary glyph row holding the glyphs of an overlay
12566 arrow. Only used for non-window-redisplay windows. */
12567
12568 static struct glyph_row *
12569 get_overlay_arrow_glyph_row (w)
12570 struct window *w;
12571 {
12572 struct frame *f = XFRAME (WINDOW_FRAME (w));
12573 struct buffer *buffer = XBUFFER (w->buffer);
12574 struct buffer *old = current_buffer;
12575 const unsigned char *arrow_string = SDATA (Voverlay_arrow_string);
12576 int arrow_len = SCHARS (Voverlay_arrow_string);
12577 const unsigned char *arrow_end = arrow_string + arrow_len;
12578 const unsigned char *p;
12579 struct it it;
12580 int multibyte_p;
12581 int n_glyphs_before;
12582
12583 set_buffer_temp (buffer);
12584 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
12585 it.glyph_row->used[TEXT_AREA] = 0;
12586 SET_TEXT_POS (it.position, 0, 0);
12587
12588 multibyte_p = !NILP (buffer->enable_multibyte_characters);
12589 p = arrow_string;
12590 while (p < arrow_end)
12591 {
12592 Lisp_Object face, ilisp;
12593
12594 /* Get the next character. */
12595 if (multibyte_p)
12596 it.c = string_char_and_length (p, arrow_len, &it.len);
12597 else
12598 it.c = *p, it.len = 1;
12599 p += it.len;
12600
12601 /* Get its face. */
12602 ilisp = make_number (p - arrow_string);
12603 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
12604 it.face_id = compute_char_face (f, it.c, face);
12605
12606 /* Compute its width, get its glyphs. */
12607 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
12608 SET_TEXT_POS (it.position, -1, -1);
12609 PRODUCE_GLYPHS (&it);
12610
12611 /* If this character doesn't fit any more in the line, we have
12612 to remove some glyphs. */
12613 if (it.current_x > it.last_visible_x)
12614 {
12615 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
12616 break;
12617 }
12618 }
12619
12620 set_buffer_temp (old);
12621 return it.glyph_row;
12622 }
12623
12624
12625 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
12626 glyphs are only inserted for terminal frames since we can't really
12627 win with truncation glyphs when partially visible glyphs are
12628 involved. Which glyphs to insert is determined by
12629 produce_special_glyphs. */
12630
12631 static void
12632 insert_left_trunc_glyphs (it)
12633 struct it *it;
12634 {
12635 struct it truncate_it;
12636 struct glyph *from, *end, *to, *toend;
12637
12638 xassert (!FRAME_WINDOW_P (it->f));
12639
12640 /* Get the truncation glyphs. */
12641 truncate_it = *it;
12642 truncate_it.current_x = 0;
12643 truncate_it.face_id = DEFAULT_FACE_ID;
12644 truncate_it.glyph_row = &scratch_glyph_row;
12645 truncate_it.glyph_row->used[TEXT_AREA] = 0;
12646 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
12647 truncate_it.object = make_number (0);
12648 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
12649
12650 /* Overwrite glyphs from IT with truncation glyphs. */
12651 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
12652 end = from + truncate_it.glyph_row->used[TEXT_AREA];
12653 to = it->glyph_row->glyphs[TEXT_AREA];
12654 toend = to + it->glyph_row->used[TEXT_AREA];
12655
12656 while (from < end)
12657 *to++ = *from++;
12658
12659 /* There may be padding glyphs left over. Overwrite them too. */
12660 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
12661 {
12662 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
12663 while (from < end)
12664 *to++ = *from++;
12665 }
12666
12667 if (to > toend)
12668 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
12669 }
12670
12671
12672 /* Compute the pixel height and width of IT->glyph_row.
12673
12674 Most of the time, ascent and height of a display line will be equal
12675 to the max_ascent and max_height values of the display iterator
12676 structure. This is not the case if
12677
12678 1. We hit ZV without displaying anything. In this case, max_ascent
12679 and max_height will be zero.
12680
12681 2. We have some glyphs that don't contribute to the line height.
12682 (The glyph row flag contributes_to_line_height_p is for future
12683 pixmap extensions).
12684
12685 The first case is easily covered by using default values because in
12686 these cases, the line height does not really matter, except that it
12687 must not be zero. */
12688
12689 static void
12690 compute_line_metrics (it)
12691 struct it *it;
12692 {
12693 struct glyph_row *row = it->glyph_row;
12694 int area, i;
12695
12696 if (FRAME_WINDOW_P (it->f))
12697 {
12698 int i, min_y, max_y;
12699
12700 /* The line may consist of one space only, that was added to
12701 place the cursor on it. If so, the row's height hasn't been
12702 computed yet. */
12703 if (row->height == 0)
12704 {
12705 if (it->max_ascent + it->max_descent == 0)
12706 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
12707 row->ascent = it->max_ascent;
12708 row->height = it->max_ascent + it->max_descent;
12709 row->phys_ascent = it->max_phys_ascent;
12710 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
12711 }
12712
12713 /* Compute the width of this line. */
12714 row->pixel_width = row->x;
12715 for (i = 0; i < row->used[TEXT_AREA]; ++i)
12716 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
12717
12718 xassert (row->pixel_width >= 0);
12719 xassert (row->ascent >= 0 && row->height > 0);
12720
12721 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
12722 || MATRIX_ROW_OVERLAPS_PRED_P (row));
12723
12724 /* If first line's physical ascent is larger than its logical
12725 ascent, use the physical ascent, and make the row taller.
12726 This makes accented characters fully visible. */
12727 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
12728 && row->phys_ascent > row->ascent)
12729 {
12730 row->height += row->phys_ascent - row->ascent;
12731 row->ascent = row->phys_ascent;
12732 }
12733
12734 /* Compute how much of the line is visible. */
12735 row->visible_height = row->height;
12736
12737 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (it->w);
12738 max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
12739
12740 if (row->y < min_y)
12741 row->visible_height -= min_y - row->y;
12742 if (row->y + row->height > max_y)
12743 row->visible_height -= row->y + row->height - max_y;
12744 }
12745 else
12746 {
12747 row->pixel_width = row->used[TEXT_AREA];
12748 if (row->continued_p)
12749 row->pixel_width -= it->continuation_pixel_width;
12750 else if (row->truncated_on_right_p)
12751 row->pixel_width -= it->truncation_pixel_width;
12752 row->ascent = row->phys_ascent = 0;
12753 row->height = row->phys_height = row->visible_height = 1;
12754 }
12755
12756 /* Compute a hash code for this row. */
12757 row->hash = 0;
12758 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12759 for (i = 0; i < row->used[area]; ++i)
12760 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
12761 + row->glyphs[area][i].u.val
12762 + row->glyphs[area][i].face_id
12763 + row->glyphs[area][i].padding_p
12764 + (row->glyphs[area][i].type << 2));
12765
12766 it->max_ascent = it->max_descent = 0;
12767 it->max_phys_ascent = it->max_phys_descent = 0;
12768 }
12769
12770
12771 /* Append one space to the glyph row of iterator IT if doing a
12772 window-based redisplay. DEFAULT_FACE_P non-zero means let the
12773 space have the default face, otherwise let it have the same face as
12774 IT->face_id. Value is non-zero if a space was added.
12775
12776 This function is called to make sure that there is always one glyph
12777 at the end of a glyph row that the cursor can be set on under
12778 window-systems. (If there weren't such a glyph we would not know
12779 how wide and tall a box cursor should be displayed).
12780
12781 At the same time this space let's a nicely handle clearing to the
12782 end of the line if the row ends in italic text. */
12783
12784 static int
12785 append_space (it, default_face_p)
12786 struct it *it;
12787 int default_face_p;
12788 {
12789 if (FRAME_WINDOW_P (it->f))
12790 {
12791 int n = it->glyph_row->used[TEXT_AREA];
12792
12793 if (it->glyph_row->glyphs[TEXT_AREA] + n
12794 < it->glyph_row->glyphs[1 + TEXT_AREA])
12795 {
12796 /* Save some values that must not be changed.
12797 Must save IT->c and IT->len because otherwise
12798 ITERATOR_AT_END_P wouldn't work anymore after
12799 append_space has been called. */
12800 enum display_element_type saved_what = it->what;
12801 int saved_c = it->c, saved_len = it->len;
12802 int saved_x = it->current_x;
12803 int saved_face_id = it->face_id;
12804 struct text_pos saved_pos;
12805 Lisp_Object saved_object;
12806 struct face *face;
12807
12808 saved_object = it->object;
12809 saved_pos = it->position;
12810
12811 it->what = IT_CHARACTER;
12812 bzero (&it->position, sizeof it->position);
12813 it->object = make_number (0);
12814 it->c = ' ';
12815 it->len = 1;
12816
12817 if (default_face_p)
12818 it->face_id = DEFAULT_FACE_ID;
12819 else if (it->face_before_selective_p)
12820 it->face_id = it->saved_face_id;
12821 face = FACE_FROM_ID (it->f, it->face_id);
12822 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
12823
12824 PRODUCE_GLYPHS (it);
12825
12826 it->current_x = saved_x;
12827 it->object = saved_object;
12828 it->position = saved_pos;
12829 it->what = saved_what;
12830 it->face_id = saved_face_id;
12831 it->len = saved_len;
12832 it->c = saved_c;
12833 return 1;
12834 }
12835 }
12836
12837 return 0;
12838 }
12839
12840
12841 /* Extend the face of the last glyph in the text area of IT->glyph_row
12842 to the end of the display line. Called from display_line.
12843 If the glyph row is empty, add a space glyph to it so that we
12844 know the face to draw. Set the glyph row flag fill_line_p. */
12845
12846 static void
12847 extend_face_to_end_of_line (it)
12848 struct it *it;
12849 {
12850 struct face *face;
12851 struct frame *f = it->f;
12852
12853 /* If line is already filled, do nothing. */
12854 if (it->current_x >= it->last_visible_x)
12855 return;
12856
12857 /* Face extension extends the background and box of IT->face_id
12858 to the end of the line. If the background equals the background
12859 of the frame, we don't have to do anything. */
12860 if (it->face_before_selective_p)
12861 face = FACE_FROM_ID (it->f, it->saved_face_id);
12862 else
12863 face = FACE_FROM_ID (f, it->face_id);
12864
12865 if (FRAME_WINDOW_P (f)
12866 && face->box == FACE_NO_BOX
12867 && face->background == FRAME_BACKGROUND_PIXEL (f)
12868 && !face->stipple)
12869 return;
12870
12871 /* Set the glyph row flag indicating that the face of the last glyph
12872 in the text area has to be drawn to the end of the text area. */
12873 it->glyph_row->fill_line_p = 1;
12874
12875 /* If current character of IT is not ASCII, make sure we have the
12876 ASCII face. This will be automatically undone the next time
12877 get_next_display_element returns a multibyte character. Note
12878 that the character will always be single byte in unibyte text. */
12879 if (!SINGLE_BYTE_CHAR_P (it->c))
12880 {
12881 it->face_id = FACE_FOR_CHAR (f, face, 0);
12882 }
12883
12884 if (FRAME_WINDOW_P (f))
12885 {
12886 /* If the row is empty, add a space with the current face of IT,
12887 so that we know which face to draw. */
12888 if (it->glyph_row->used[TEXT_AREA] == 0)
12889 {
12890 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
12891 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
12892 it->glyph_row->used[TEXT_AREA] = 1;
12893 }
12894 }
12895 else
12896 {
12897 /* Save some values that must not be changed. */
12898 int saved_x = it->current_x;
12899 struct text_pos saved_pos;
12900 Lisp_Object saved_object;
12901 enum display_element_type saved_what = it->what;
12902 int saved_face_id = it->face_id;
12903
12904 saved_object = it->object;
12905 saved_pos = it->position;
12906
12907 it->what = IT_CHARACTER;
12908 bzero (&it->position, sizeof it->position);
12909 it->object = make_number (0);
12910 it->c = ' ';
12911 it->len = 1;
12912 it->face_id = face->id;
12913
12914 PRODUCE_GLYPHS (it);
12915
12916 while (it->current_x <= it->last_visible_x)
12917 PRODUCE_GLYPHS (it);
12918
12919 /* Don't count these blanks really. It would let us insert a left
12920 truncation glyph below and make us set the cursor on them, maybe. */
12921 it->current_x = saved_x;
12922 it->object = saved_object;
12923 it->position = saved_pos;
12924 it->what = saved_what;
12925 it->face_id = saved_face_id;
12926 }
12927 }
12928
12929
12930 /* Value is non-zero if text starting at CHARPOS in current_buffer is
12931 trailing whitespace. */
12932
12933 static int
12934 trailing_whitespace_p (charpos)
12935 int charpos;
12936 {
12937 int bytepos = CHAR_TO_BYTE (charpos);
12938 int c = 0;
12939
12940 while (bytepos < ZV_BYTE
12941 && (c = FETCH_CHAR (bytepos),
12942 c == ' ' || c == '\t'))
12943 ++bytepos;
12944
12945 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
12946 {
12947 if (bytepos != PT_BYTE)
12948 return 1;
12949 }
12950 return 0;
12951 }
12952
12953
12954 /* Highlight trailing whitespace, if any, in ROW. */
12955
12956 void
12957 highlight_trailing_whitespace (f, row)
12958 struct frame *f;
12959 struct glyph_row *row;
12960 {
12961 int used = row->used[TEXT_AREA];
12962
12963 if (used)
12964 {
12965 struct glyph *start = row->glyphs[TEXT_AREA];
12966 struct glyph *glyph = start + used - 1;
12967
12968 /* Skip over glyphs inserted to display the cursor at the
12969 end of a line, for extending the face of the last glyph
12970 to the end of the line on terminals, and for truncation
12971 and continuation glyphs. */
12972 while (glyph >= start
12973 && glyph->type == CHAR_GLYPH
12974 && INTEGERP (glyph->object))
12975 --glyph;
12976
12977 /* If last glyph is a space or stretch, and it's trailing
12978 whitespace, set the face of all trailing whitespace glyphs in
12979 IT->glyph_row to `trailing-whitespace'. */
12980 if (glyph >= start
12981 && BUFFERP (glyph->object)
12982 && (glyph->type == STRETCH_GLYPH
12983 || (glyph->type == CHAR_GLYPH
12984 && glyph->u.ch == ' '))
12985 && trailing_whitespace_p (glyph->charpos))
12986 {
12987 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
12988
12989 while (glyph >= start
12990 && BUFFERP (glyph->object)
12991 && (glyph->type == STRETCH_GLYPH
12992 || (glyph->type == CHAR_GLYPH
12993 && glyph->u.ch == ' ')))
12994 (glyph--)->face_id = face_id;
12995 }
12996 }
12997 }
12998
12999
13000 /* Value is non-zero if glyph row ROW in window W should be
13001 used to hold the cursor. */
13002
13003 static int
13004 cursor_row_p (w, row)
13005 struct window *w;
13006 struct glyph_row *row;
13007 {
13008 int cursor_row_p = 1;
13009
13010 if (PT == MATRIX_ROW_END_CHARPOS (row))
13011 {
13012 /* If the row ends with a newline from a string, we don't want
13013 the cursor there (if the row is continued it doesn't end in a
13014 newline). */
13015 if (CHARPOS (row->end.string_pos) >= 0
13016 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
13017 cursor_row_p = row->continued_p;
13018
13019 /* If the row ends at ZV, display the cursor at the end of that
13020 row instead of at the start of the row below. */
13021 else if (row->ends_at_zv_p)
13022 cursor_row_p = 1;
13023 else
13024 cursor_row_p = 0;
13025 }
13026
13027 return cursor_row_p;
13028 }
13029
13030
13031 /* Construct the glyph row IT->glyph_row in the desired matrix of
13032 IT->w from text at the current position of IT. See dispextern.h
13033 for an overview of struct it. Value is non-zero if
13034 IT->glyph_row displays text, as opposed to a line displaying ZV
13035 only. */
13036
13037 static int
13038 display_line (it)
13039 struct it *it;
13040 {
13041 struct glyph_row *row = it->glyph_row;
13042
13043 /* We always start displaying at hpos zero even if hscrolled. */
13044 xassert (it->hpos == 0 && it->current_x == 0);
13045
13046 /* We must not display in a row that's not a text row. */
13047 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
13048 < it->w->desired_matrix->nrows);
13049
13050 /* Is IT->w showing the region? */
13051 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
13052
13053 /* Clear the result glyph row and enable it. */
13054 prepare_desired_row (row);
13055
13056 row->y = it->current_y;
13057 row->start = it->current;
13058 row->continuation_lines_width = it->continuation_lines_width;
13059 row->displays_text_p = 1;
13060 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
13061 it->starts_in_middle_of_char_p = 0;
13062
13063 /* Arrange the overlays nicely for our purposes. Usually, we call
13064 display_line on only one line at a time, in which case this
13065 can't really hurt too much, or we call it on lines which appear
13066 one after another in the buffer, in which case all calls to
13067 recenter_overlay_lists but the first will be pretty cheap. */
13068 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
13069
13070 /* Move over display elements that are not visible because we are
13071 hscrolled. This may stop at an x-position < IT->first_visible_x
13072 if the first glyph is partially visible or if we hit a line end. */
13073 if (it->current_x < it->first_visible_x)
13074 move_it_in_display_line_to (it, ZV, it->first_visible_x,
13075 MOVE_TO_POS | MOVE_TO_X);
13076
13077 /* Get the initial row height. This is either the height of the
13078 text hscrolled, if there is any, or zero. */
13079 row->ascent = it->max_ascent;
13080 row->height = it->max_ascent + it->max_descent;
13081 row->phys_ascent = it->max_phys_ascent;
13082 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
13083
13084 /* Loop generating characters. The loop is left with IT on the next
13085 character to display. */
13086 while (1)
13087 {
13088 int n_glyphs_before, hpos_before, x_before;
13089 int x, i, nglyphs;
13090 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
13091
13092 /* Retrieve the next thing to display. Value is zero if end of
13093 buffer reached. */
13094 if (!get_next_display_element (it))
13095 {
13096 /* Maybe add a space at the end of this line that is used to
13097 display the cursor there under X. Set the charpos of the
13098 first glyph of blank lines not corresponding to any text
13099 to -1. */
13100 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
13101 || row->used[TEXT_AREA] == 0)
13102 {
13103 row->glyphs[TEXT_AREA]->charpos = -1;
13104 row->displays_text_p = 0;
13105
13106 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
13107 && (!MINI_WINDOW_P (it->w)
13108 || (minibuf_level && EQ (it->window, minibuf_window))))
13109 row->indicate_empty_line_p = 1;
13110 }
13111
13112 it->continuation_lines_width = 0;
13113 row->ends_at_zv_p = 1;
13114 break;
13115 }
13116
13117 /* Now, get the metrics of what we want to display. This also
13118 generates glyphs in `row' (which is IT->glyph_row). */
13119 n_glyphs_before = row->used[TEXT_AREA];
13120 x = it->current_x;
13121
13122 /* Remember the line height so far in case the next element doesn't
13123 fit on the line. */
13124 if (!it->truncate_lines_p)
13125 {
13126 ascent = it->max_ascent;
13127 descent = it->max_descent;
13128 phys_ascent = it->max_phys_ascent;
13129 phys_descent = it->max_phys_descent;
13130 }
13131
13132 PRODUCE_GLYPHS (it);
13133
13134 /* If this display element was in marginal areas, continue with
13135 the next one. */
13136 if (it->area != TEXT_AREA)
13137 {
13138 row->ascent = max (row->ascent, it->max_ascent);
13139 row->height = max (row->height, it->max_ascent + it->max_descent);
13140 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13141 row->phys_height = max (row->phys_height,
13142 it->max_phys_ascent + it->max_phys_descent);
13143 set_iterator_to_next (it, 1);
13144 continue;
13145 }
13146
13147 /* Does the display element fit on the line? If we truncate
13148 lines, we should draw past the right edge of the window. If
13149 we don't truncate, we want to stop so that we can display the
13150 continuation glyph before the right margin. If lines are
13151 continued, there are two possible strategies for characters
13152 resulting in more than 1 glyph (e.g. tabs): Display as many
13153 glyphs as possible in this line and leave the rest for the
13154 continuation line, or display the whole element in the next
13155 line. Original redisplay did the former, so we do it also. */
13156 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
13157 hpos_before = it->hpos;
13158 x_before = x;
13159
13160 if (/* Not a newline. */
13161 nglyphs > 0
13162 /* Glyphs produced fit entirely in the line. */
13163 && it->current_x < it->last_visible_x)
13164 {
13165 it->hpos += nglyphs;
13166 row->ascent = max (row->ascent, it->max_ascent);
13167 row->height = max (row->height, it->max_ascent + it->max_descent);
13168 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13169 row->phys_height = max (row->phys_height,
13170 it->max_phys_ascent + it->max_phys_descent);
13171 if (it->current_x - it->pixel_width < it->first_visible_x)
13172 row->x = x - it->first_visible_x;
13173 }
13174 else
13175 {
13176 int new_x;
13177 struct glyph *glyph;
13178
13179 for (i = 0; i < nglyphs; ++i, x = new_x)
13180 {
13181 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
13182 new_x = x + glyph->pixel_width;
13183
13184 if (/* Lines are continued. */
13185 !it->truncate_lines_p
13186 && (/* Glyph doesn't fit on the line. */
13187 new_x > it->last_visible_x
13188 /* Or it fits exactly on a window system frame. */
13189 || (new_x == it->last_visible_x
13190 && FRAME_WINDOW_P (it->f))))
13191 {
13192 /* End of a continued line. */
13193
13194 if (it->hpos == 0
13195 || (new_x == it->last_visible_x
13196 && FRAME_WINDOW_P (it->f)))
13197 {
13198 /* Current glyph is the only one on the line or
13199 fits exactly on the line. We must continue
13200 the line because we can't draw the cursor
13201 after the glyph. */
13202 row->continued_p = 1;
13203 it->current_x = new_x;
13204 it->continuation_lines_width += new_x;
13205 ++it->hpos;
13206 if (i == nglyphs - 1)
13207 set_iterator_to_next (it, 1);
13208 }
13209 else if (CHAR_GLYPH_PADDING_P (*glyph)
13210 && !FRAME_WINDOW_P (it->f))
13211 {
13212 /* A padding glyph that doesn't fit on this line.
13213 This means the whole character doesn't fit
13214 on the line. */
13215 row->used[TEXT_AREA] = n_glyphs_before;
13216
13217 /* Fill the rest of the row with continuation
13218 glyphs like in 20.x. */
13219 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
13220 < row->glyphs[1 + TEXT_AREA])
13221 produce_special_glyphs (it, IT_CONTINUATION);
13222
13223 row->continued_p = 1;
13224 it->current_x = x_before;
13225 it->continuation_lines_width += x_before;
13226
13227 /* Restore the height to what it was before the
13228 element not fitting on the line. */
13229 it->max_ascent = ascent;
13230 it->max_descent = descent;
13231 it->max_phys_ascent = phys_ascent;
13232 it->max_phys_descent = phys_descent;
13233 }
13234 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
13235 {
13236 /* A TAB that extends past the right edge of the
13237 window. This produces a single glyph on
13238 window system frames. We leave the glyph in
13239 this row and let it fill the row, but don't
13240 consume the TAB. */
13241 it->continuation_lines_width += it->last_visible_x;
13242 row->ends_in_middle_of_char_p = 1;
13243 row->continued_p = 1;
13244 glyph->pixel_width = it->last_visible_x - x;
13245 it->starts_in_middle_of_char_p = 1;
13246 }
13247 else
13248 {
13249 /* Something other than a TAB that draws past
13250 the right edge of the window. Restore
13251 positions to values before the element. */
13252 row->used[TEXT_AREA] = n_glyphs_before + i;
13253
13254 /* Display continuation glyphs. */
13255 if (!FRAME_WINDOW_P (it->f))
13256 produce_special_glyphs (it, IT_CONTINUATION);
13257 row->continued_p = 1;
13258
13259 it->continuation_lines_width += x;
13260
13261 if (nglyphs > 1 && i > 0)
13262 {
13263 row->ends_in_middle_of_char_p = 1;
13264 it->starts_in_middle_of_char_p = 1;
13265 }
13266
13267 /* Restore the height to what it was before the
13268 element not fitting on the line. */
13269 it->max_ascent = ascent;
13270 it->max_descent = descent;
13271 it->max_phys_ascent = phys_ascent;
13272 it->max_phys_descent = phys_descent;
13273 }
13274
13275 break;
13276 }
13277 else if (new_x > it->first_visible_x)
13278 {
13279 /* Increment number of glyphs actually displayed. */
13280 ++it->hpos;
13281
13282 if (x < it->first_visible_x)
13283 /* Glyph is partially visible, i.e. row starts at
13284 negative X position. */
13285 row->x = x - it->first_visible_x;
13286 }
13287 else
13288 {
13289 /* Glyph is completely off the left margin of the
13290 window. This should not happen because of the
13291 move_it_in_display_line at the start of this
13292 function, unless the text display area of the
13293 window is empty. */
13294 xassert (it->first_visible_x <= it->last_visible_x);
13295 }
13296 }
13297
13298 row->ascent = max (row->ascent, it->max_ascent);
13299 row->height = max (row->height, it->max_ascent + it->max_descent);
13300 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13301 row->phys_height = max (row->phys_height,
13302 it->max_phys_ascent + it->max_phys_descent);
13303
13304 /* End of this display line if row is continued. */
13305 if (row->continued_p)
13306 break;
13307 }
13308
13309 /* Is this a line end? If yes, we're also done, after making
13310 sure that a non-default face is extended up to the right
13311 margin of the window. */
13312 if (ITERATOR_AT_END_OF_LINE_P (it))
13313 {
13314 int used_before = row->used[TEXT_AREA];
13315
13316 row->ends_in_newline_from_string_p = STRINGP (it->object);
13317
13318 /* Add a space at the end of the line that is used to
13319 display the cursor there. */
13320 append_space (it, 0);
13321
13322 /* Extend the face to the end of the line. */
13323 extend_face_to_end_of_line (it);
13324
13325 /* Make sure we have the position. */
13326 if (used_before == 0)
13327 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
13328
13329 /* Consume the line end. This skips over invisible lines. */
13330 set_iterator_to_next (it, 1);
13331 it->continuation_lines_width = 0;
13332 break;
13333 }
13334
13335 /* Proceed with next display element. Note that this skips
13336 over lines invisible because of selective display. */
13337 set_iterator_to_next (it, 1);
13338
13339 /* If we truncate lines, we are done when the last displayed
13340 glyphs reach past the right margin of the window. */
13341 if (it->truncate_lines_p
13342 && (FRAME_WINDOW_P (it->f)
13343 ? (it->current_x >= it->last_visible_x)
13344 : (it->current_x > it->last_visible_x)))
13345 {
13346 /* Maybe add truncation glyphs. */
13347 if (!FRAME_WINDOW_P (it->f))
13348 {
13349 int i, n;
13350
13351 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
13352 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
13353 break;
13354
13355 for (n = row->used[TEXT_AREA]; i < n; ++i)
13356 {
13357 row->used[TEXT_AREA] = i;
13358 produce_special_glyphs (it, IT_TRUNCATION);
13359 }
13360 }
13361
13362 row->truncated_on_right_p = 1;
13363 it->continuation_lines_width = 0;
13364 reseat_at_next_visible_line_start (it, 0);
13365 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
13366 it->hpos = hpos_before;
13367 it->current_x = x_before;
13368 break;
13369 }
13370 }
13371
13372 /* If line is not empty and hscrolled, maybe insert truncation glyphs
13373 at the left window margin. */
13374 if (it->first_visible_x
13375 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
13376 {
13377 if (!FRAME_WINDOW_P (it->f))
13378 insert_left_trunc_glyphs (it);
13379 row->truncated_on_left_p = 1;
13380 }
13381
13382 /* If the start of this line is the overlay arrow-position, then
13383 mark this glyph row as the one containing the overlay arrow.
13384 This is clearly a mess with variable size fonts. It would be
13385 better to let it be displayed like cursors under X. */
13386 if (MARKERP (Voverlay_arrow_position)
13387 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
13388 && (MATRIX_ROW_START_CHARPOS (row)
13389 == marker_position (Voverlay_arrow_position))
13390 && STRINGP (Voverlay_arrow_string)
13391 && ! overlay_arrow_seen)
13392 {
13393 /* Overlay arrow in window redisplay is a fringe bitmap. */
13394 if (!FRAME_WINDOW_P (it->f))
13395 {
13396 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
13397 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
13398 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
13399 struct glyph *p = row->glyphs[TEXT_AREA];
13400 struct glyph *p2, *end;
13401
13402 /* Copy the arrow glyphs. */
13403 while (glyph < arrow_end)
13404 *p++ = *glyph++;
13405
13406 /* Throw away padding glyphs. */
13407 p2 = p;
13408 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
13409 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
13410 ++p2;
13411 if (p2 > p)
13412 {
13413 while (p2 < end)
13414 *p++ = *p2++;
13415 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
13416 }
13417 }
13418
13419 overlay_arrow_seen = 1;
13420 row->overlay_arrow_p = 1;
13421 }
13422
13423 /* Compute pixel dimensions of this line. */
13424 compute_line_metrics (it);
13425
13426 /* Remember the position at which this line ends. */
13427 row->end = it->current;
13428
13429 /* Maybe set the cursor. */
13430 if (it->w->cursor.vpos < 0
13431 && PT >= MATRIX_ROW_START_CHARPOS (row)
13432 && PT <= MATRIX_ROW_END_CHARPOS (row)
13433 && cursor_row_p (it->w, row))
13434 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
13435
13436 /* Highlight trailing whitespace. */
13437 if (!NILP (Vshow_trailing_whitespace))
13438 highlight_trailing_whitespace (it->f, it->glyph_row);
13439
13440 /* Prepare for the next line. This line starts horizontally at (X
13441 HPOS) = (0 0). Vertical positions are incremented. As a
13442 convenience for the caller, IT->glyph_row is set to the next
13443 row to be used. */
13444 it->current_x = it->hpos = 0;
13445 it->current_y += row->height;
13446 ++it->vpos;
13447 ++it->glyph_row;
13448 return row->displays_text_p;
13449 }
13450
13451
13452 \f
13453 /***********************************************************************
13454 Menu Bar
13455 ***********************************************************************/
13456
13457 /* Redisplay the menu bar in the frame for window W.
13458
13459 The menu bar of X frames that don't have X toolkit support is
13460 displayed in a special window W->frame->menu_bar_window.
13461
13462 The menu bar of terminal frames is treated specially as far as
13463 glyph matrices are concerned. Menu bar lines are not part of
13464 windows, so the update is done directly on the frame matrix rows
13465 for the menu bar. */
13466
13467 static void
13468 display_menu_bar (w)
13469 struct window *w;
13470 {
13471 struct frame *f = XFRAME (WINDOW_FRAME (w));
13472 struct it it;
13473 Lisp_Object items;
13474 int i;
13475
13476 /* Don't do all this for graphical frames. */
13477 #ifdef HAVE_NTGUI
13478 if (!NILP (Vwindow_system))
13479 return;
13480 #endif
13481 #ifdef USE_X_TOOLKIT
13482 if (FRAME_X_P (f))
13483 return;
13484 #endif
13485 #ifdef MAC_OS
13486 if (FRAME_MAC_P (f))
13487 return;
13488 #endif
13489
13490 #ifdef USE_X_TOOLKIT
13491 xassert (!FRAME_WINDOW_P (f));
13492 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
13493 it.first_visible_x = 0;
13494 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
13495 #else /* not USE_X_TOOLKIT */
13496 if (FRAME_WINDOW_P (f))
13497 {
13498 /* Menu bar lines are displayed in the desired matrix of the
13499 dummy window menu_bar_window. */
13500 struct window *menu_w;
13501 xassert (WINDOWP (f->menu_bar_window));
13502 menu_w = XWINDOW (f->menu_bar_window);
13503 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
13504 MENU_FACE_ID);
13505 it.first_visible_x = 0;
13506 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
13507 }
13508 else
13509 {
13510 /* This is a TTY frame, i.e. character hpos/vpos are used as
13511 pixel x/y. */
13512 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
13513 MENU_FACE_ID);
13514 it.first_visible_x = 0;
13515 it.last_visible_x = FRAME_WIDTH (f);
13516 }
13517 #endif /* not USE_X_TOOLKIT */
13518
13519 if (! mode_line_inverse_video)
13520 /* Force the menu-bar to be displayed in the default face. */
13521 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
13522
13523 /* Clear all rows of the menu bar. */
13524 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
13525 {
13526 struct glyph_row *row = it.glyph_row + i;
13527 clear_glyph_row (row);
13528 row->enabled_p = 1;
13529 row->full_width_p = 1;
13530 }
13531
13532 /* Display all items of the menu bar. */
13533 items = FRAME_MENU_BAR_ITEMS (it.f);
13534 for (i = 0; i < XVECTOR (items)->size; i += 4)
13535 {
13536 Lisp_Object string;
13537
13538 /* Stop at nil string. */
13539 string = AREF (items, i + 1);
13540 if (NILP (string))
13541 break;
13542
13543 /* Remember where item was displayed. */
13544 AREF (items, i + 3) = make_number (it.hpos);
13545
13546 /* Display the item, pad with one space. */
13547 if (it.current_x < it.last_visible_x)
13548 display_string (NULL, string, Qnil, 0, 0, &it,
13549 SCHARS (string) + 1, 0, 0, -1);
13550 }
13551
13552 /* Fill out the line with spaces. */
13553 if (it.current_x < it.last_visible_x)
13554 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
13555
13556 /* Compute the total height of the lines. */
13557 compute_line_metrics (&it);
13558 }
13559
13560
13561 \f
13562 /***********************************************************************
13563 Mode Line
13564 ***********************************************************************/
13565
13566 /* Redisplay mode lines in the window tree whose root is WINDOW. If
13567 FORCE is non-zero, redisplay mode lines unconditionally.
13568 Otherwise, redisplay only mode lines that are garbaged. Value is
13569 the number of windows whose mode lines were redisplayed. */
13570
13571 static int
13572 redisplay_mode_lines (window, force)
13573 Lisp_Object window;
13574 int force;
13575 {
13576 int nwindows = 0;
13577
13578 while (!NILP (window))
13579 {
13580 struct window *w = XWINDOW (window);
13581
13582 if (WINDOWP (w->hchild))
13583 nwindows += redisplay_mode_lines (w->hchild, force);
13584 else if (WINDOWP (w->vchild))
13585 nwindows += redisplay_mode_lines (w->vchild, force);
13586 else if (force
13587 || FRAME_GARBAGED_P (XFRAME (w->frame))
13588 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
13589 {
13590 struct text_pos lpoint;
13591 struct buffer *old = current_buffer;
13592
13593 /* Set the window's buffer for the mode line display. */
13594 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13595 set_buffer_internal_1 (XBUFFER (w->buffer));
13596
13597 /* Point refers normally to the selected window. For any
13598 other window, set up appropriate value. */
13599 if (!EQ (window, selected_window))
13600 {
13601 struct text_pos pt;
13602
13603 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
13604 if (CHARPOS (pt) < BEGV)
13605 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
13606 else if (CHARPOS (pt) > (ZV - 1))
13607 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
13608 else
13609 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
13610 }
13611
13612 /* Display mode lines. */
13613 clear_glyph_matrix (w->desired_matrix);
13614 if (display_mode_lines (w))
13615 {
13616 ++nwindows;
13617 w->must_be_updated_p = 1;
13618 }
13619
13620 /* Restore old settings. */
13621 set_buffer_internal_1 (old);
13622 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13623 }
13624
13625 window = w->next;
13626 }
13627
13628 return nwindows;
13629 }
13630
13631
13632 /* Display the mode and/or top line of window W. Value is the number
13633 of mode lines displayed. */
13634
13635 static int
13636 display_mode_lines (w)
13637 struct window *w;
13638 {
13639 Lisp_Object old_selected_window, old_selected_frame;
13640 int n = 0;
13641
13642 old_selected_frame = selected_frame;
13643 selected_frame = w->frame;
13644 old_selected_window = selected_window;
13645 XSETWINDOW (selected_window, w);
13646
13647 /* These will be set while the mode line specs are processed. */
13648 line_number_displayed = 0;
13649 w->column_number_displayed = Qnil;
13650
13651 if (WINDOW_WANTS_MODELINE_P (w))
13652 {
13653 struct window *sel_w = XWINDOW (old_selected_window);
13654
13655 /* Select mode line face based on the real selected window. */
13656 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
13657 current_buffer->mode_line_format);
13658 ++n;
13659 }
13660
13661 if (WINDOW_WANTS_HEADER_LINE_P (w))
13662 {
13663 display_mode_line (w, HEADER_LINE_FACE_ID,
13664 current_buffer->header_line_format);
13665 ++n;
13666 }
13667
13668 selected_frame = old_selected_frame;
13669 selected_window = old_selected_window;
13670 return n;
13671 }
13672
13673
13674 /* Display mode or top line of window W. FACE_ID specifies which line
13675 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
13676 FORMAT is the mode line format to display. Value is the pixel
13677 height of the mode line displayed. */
13678
13679 static int
13680 display_mode_line (w, face_id, format)
13681 struct window *w;
13682 enum face_id face_id;
13683 Lisp_Object format;
13684 {
13685 struct it it;
13686 struct face *face;
13687
13688 init_iterator (&it, w, -1, -1, NULL, face_id);
13689 prepare_desired_row (it.glyph_row);
13690
13691 if (! mode_line_inverse_video)
13692 /* Force the mode-line to be displayed in the default face. */
13693 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
13694
13695 /* Temporarily make frame's keyboard the current kboard so that
13696 kboard-local variables in the mode_line_format will get the right
13697 values. */
13698 push_frame_kboard (it.f);
13699 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
13700 pop_frame_kboard ();
13701
13702 /* Fill up with spaces. */
13703 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
13704
13705 compute_line_metrics (&it);
13706 it.glyph_row->full_width_p = 1;
13707 it.glyph_row->mode_line_p = 1;
13708 it.glyph_row->continued_p = 0;
13709 it.glyph_row->truncated_on_left_p = 0;
13710 it.glyph_row->truncated_on_right_p = 0;
13711
13712 /* Make a 3D mode-line have a shadow at its right end. */
13713 face = FACE_FROM_ID (it.f, face_id);
13714 extend_face_to_end_of_line (&it);
13715 if (face->box != FACE_NO_BOX)
13716 {
13717 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
13718 + it.glyph_row->used[TEXT_AREA] - 1);
13719 last->right_box_line_p = 1;
13720 }
13721
13722 return it.glyph_row->height;
13723 }
13724
13725 /* Alist that caches the results of :propertize.
13726 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
13727 Lisp_Object mode_line_proptrans_alist;
13728
13729 /* List of strings making up the mode-line. */
13730 Lisp_Object mode_line_string_list;
13731
13732 /* Base face property when building propertized mode line string. */
13733 static Lisp_Object mode_line_string_face;
13734 static Lisp_Object mode_line_string_face_prop;
13735
13736
13737 /* Contribute ELT to the mode line for window IT->w. How it
13738 translates into text depends on its data type.
13739
13740 IT describes the display environment in which we display, as usual.
13741
13742 DEPTH is the depth in recursion. It is used to prevent
13743 infinite recursion here.
13744
13745 FIELD_WIDTH is the number of characters the display of ELT should
13746 occupy in the mode line, and PRECISION is the maximum number of
13747 characters to display from ELT's representation. See
13748 display_string for details.
13749
13750 Returns the hpos of the end of the text generated by ELT.
13751
13752 PROPS is a property list to add to any string we encounter.
13753
13754 If RISKY is nonzero, remove (disregard) any properties in any string
13755 we encounter, and ignore :eval and :propertize.
13756
13757 If the global variable `frame_title_ptr' is non-NULL, then the output
13758 is passed to `store_frame_title' instead of `display_string'. */
13759
13760 static int
13761 display_mode_element (it, depth, field_width, precision, elt, props, risky)
13762 struct it *it;
13763 int depth;
13764 int field_width, precision;
13765 Lisp_Object elt, props;
13766 int risky;
13767 {
13768 int n = 0, field, prec;
13769 int literal = 0;
13770
13771 tail_recurse:
13772 if (depth > 10)
13773 goto invalid;
13774
13775 depth++;
13776
13777 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
13778 {
13779 case Lisp_String:
13780 {
13781 /* A string: output it and check for %-constructs within it. */
13782 unsigned char c;
13783 const unsigned char *this, *lisp_string;
13784
13785 if (!NILP (props) || risky)
13786 {
13787 Lisp_Object oprops, aelt;
13788 oprops = Ftext_properties_at (make_number (0), elt);
13789
13790 if (NILP (Fequal (props, oprops)) || risky)
13791 {
13792 /* If the starting string has properties,
13793 merge the specified ones onto the existing ones. */
13794 if (! NILP (oprops) && !risky)
13795 {
13796 Lisp_Object tem;
13797
13798 oprops = Fcopy_sequence (oprops);
13799 tem = props;
13800 while (CONSP (tem))
13801 {
13802 oprops = Fplist_put (oprops, XCAR (tem),
13803 XCAR (XCDR (tem)));
13804 tem = XCDR (XCDR (tem));
13805 }
13806 props = oprops;
13807 }
13808
13809 aelt = Fassoc (elt, mode_line_proptrans_alist);
13810 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
13811 {
13812 mode_line_proptrans_alist
13813 = Fcons (aelt, Fdelq (aelt, mode_line_proptrans_alist));
13814 elt = XCAR (aelt);
13815 }
13816 else
13817 {
13818 Lisp_Object tem;
13819
13820 elt = Fcopy_sequence (elt);
13821 Fset_text_properties (make_number (0), Flength (elt),
13822 props, elt);
13823 /* Add this item to mode_line_proptrans_alist. */
13824 mode_line_proptrans_alist
13825 = Fcons (Fcons (elt, props),
13826 mode_line_proptrans_alist);
13827 /* Truncate mode_line_proptrans_alist
13828 to at most 50 elements. */
13829 tem = Fnthcdr (make_number (50),
13830 mode_line_proptrans_alist);
13831 if (! NILP (tem))
13832 XSETCDR (tem, Qnil);
13833 }
13834 }
13835 }
13836
13837 this = SDATA (elt);
13838 lisp_string = this;
13839
13840 if (literal)
13841 {
13842 prec = precision - n;
13843 if (frame_title_ptr)
13844 n += store_frame_title (SDATA (elt), -1, prec);
13845 else if (!NILP (mode_line_string_list))
13846 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
13847 else
13848 n += display_string (NULL, elt, Qnil, 0, 0, it,
13849 0, prec, 0, STRING_MULTIBYTE (elt));
13850
13851 break;
13852 }
13853
13854 while ((precision <= 0 || n < precision)
13855 && *this
13856 && (frame_title_ptr
13857 || !NILP (mode_line_string_list)
13858 || it->current_x < it->last_visible_x))
13859 {
13860 const unsigned char *last = this;
13861
13862 /* Advance to end of string or next format specifier. */
13863 while ((c = *this++) != '\0' && c != '%')
13864 ;
13865
13866 if (this - 1 != last)
13867 {
13868 /* Output to end of string or up to '%'. Field width
13869 is length of string. Don't output more than
13870 PRECISION allows us. */
13871 --this;
13872
13873 prec = chars_in_text (last, this - last);
13874 if (precision > 0 && prec > precision - n)
13875 prec = precision - n;
13876
13877 if (frame_title_ptr)
13878 n += store_frame_title (last, 0, prec);
13879 else if (!NILP (mode_line_string_list))
13880 {
13881 int bytepos = last - lisp_string;
13882 int charpos = string_byte_to_char (elt, bytepos);
13883 n += store_mode_line_string (NULL,
13884 Fsubstring (elt, make_number (charpos),
13885 make_number (charpos + prec)),
13886 0, 0, 0, Qnil);
13887 }
13888 else
13889 {
13890 int bytepos = last - lisp_string;
13891 int charpos = string_byte_to_char (elt, bytepos);
13892 n += display_string (NULL, elt, Qnil, 0, charpos,
13893 it, 0, prec, 0,
13894 STRING_MULTIBYTE (elt));
13895 }
13896 }
13897 else /* c == '%' */
13898 {
13899 const unsigned char *percent_position = this;
13900
13901 /* Get the specified minimum width. Zero means
13902 don't pad. */
13903 field = 0;
13904 while ((c = *this++) >= '0' && c <= '9')
13905 field = field * 10 + c - '0';
13906
13907 /* Don't pad beyond the total padding allowed. */
13908 if (field_width - n > 0 && field > field_width - n)
13909 field = field_width - n;
13910
13911 /* Note that either PRECISION <= 0 or N < PRECISION. */
13912 prec = precision - n;
13913
13914 if (c == 'M')
13915 n += display_mode_element (it, depth, field, prec,
13916 Vglobal_mode_string, props,
13917 risky);
13918 else if (c != 0)
13919 {
13920 int multibyte;
13921 int bytepos, charpos;
13922 unsigned char *spec;
13923
13924 bytepos = percent_position - lisp_string;
13925 charpos = (STRING_MULTIBYTE (elt)
13926 ? string_byte_to_char (elt, bytepos)
13927 : bytepos);
13928
13929 spec
13930 = decode_mode_spec (it->w, c, field, prec, &multibyte);
13931
13932 if (frame_title_ptr)
13933 n += store_frame_title (spec, field, prec);
13934 else if (!NILP (mode_line_string_list))
13935 {
13936 int len = strlen (spec);
13937 Lisp_Object tem = make_string (spec, len);
13938 props = Ftext_properties_at (make_number (charpos), elt);
13939 /* Should only keep face property in props */
13940 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
13941 }
13942 else
13943 {
13944 int nglyphs_before, nwritten;
13945
13946 nglyphs_before = it->glyph_row->used[TEXT_AREA];
13947 nwritten = display_string (spec, Qnil, elt,
13948 charpos, 0, it,
13949 field, prec, 0,
13950 multibyte);
13951
13952 /* Assign to the glyphs written above the
13953 string where the `%x' came from, position
13954 of the `%'. */
13955 if (nwritten > 0)
13956 {
13957 struct glyph *glyph
13958 = (it->glyph_row->glyphs[TEXT_AREA]
13959 + nglyphs_before);
13960 int i;
13961
13962 for (i = 0; i < nwritten; ++i)
13963 {
13964 glyph[i].object = elt;
13965 glyph[i].charpos = charpos;
13966 }
13967
13968 n += nwritten;
13969 }
13970 }
13971 }
13972 else /* c == 0 */
13973 break;
13974 }
13975 }
13976 }
13977 break;
13978
13979 case Lisp_Symbol:
13980 /* A symbol: process the value of the symbol recursively
13981 as if it appeared here directly. Avoid error if symbol void.
13982 Special case: if value of symbol is a string, output the string
13983 literally. */
13984 {
13985 register Lisp_Object tem;
13986
13987 /* If the variable is not marked as risky to set
13988 then its contents are risky to use. */
13989 if (NILP (Fget (elt, Qrisky_local_variable)))
13990 risky = 1;
13991
13992 tem = Fboundp (elt);
13993 if (!NILP (tem))
13994 {
13995 tem = Fsymbol_value (elt);
13996 /* If value is a string, output that string literally:
13997 don't check for % within it. */
13998 if (STRINGP (tem))
13999 literal = 1;
14000
14001 if (!EQ (tem, elt))
14002 {
14003 /* Give up right away for nil or t. */
14004 elt = tem;
14005 goto tail_recurse;
14006 }
14007 }
14008 }
14009 break;
14010
14011 case Lisp_Cons:
14012 {
14013 register Lisp_Object car, tem;
14014
14015 /* A cons cell: five distinct cases.
14016 If first element is :eval or :propertize, do something special.
14017 If first element is a string or a cons, process all the elements
14018 and effectively concatenate them.
14019 If first element is a negative number, truncate displaying cdr to
14020 at most that many characters. If positive, pad (with spaces)
14021 to at least that many characters.
14022 If first element is a symbol, process the cadr or caddr recursively
14023 according to whether the symbol's value is non-nil or nil. */
14024 car = XCAR (elt);
14025 if (EQ (car, QCeval))
14026 {
14027 /* An element of the form (:eval FORM) means evaluate FORM
14028 and use the result as mode line elements. */
14029
14030 if (risky)
14031 break;
14032
14033 if (CONSP (XCDR (elt)))
14034 {
14035 Lisp_Object spec;
14036 spec = safe_eval (XCAR (XCDR (elt)));
14037 n += display_mode_element (it, depth, field_width - n,
14038 precision - n, spec, props,
14039 risky);
14040 }
14041 }
14042 else if (EQ (car, QCpropertize))
14043 {
14044 /* An element of the form (:propertize ELT PROPS...)
14045 means display ELT but applying properties PROPS. */
14046
14047 if (risky)
14048 break;
14049
14050 if (CONSP (XCDR (elt)))
14051 n += display_mode_element (it, depth, field_width - n,
14052 precision - n, XCAR (XCDR (elt)),
14053 XCDR (XCDR (elt)), risky);
14054 }
14055 else if (SYMBOLP (car))
14056 {
14057 tem = Fboundp (car);
14058 elt = XCDR (elt);
14059 if (!CONSP (elt))
14060 goto invalid;
14061 /* elt is now the cdr, and we know it is a cons cell.
14062 Use its car if CAR has a non-nil value. */
14063 if (!NILP (tem))
14064 {
14065 tem = Fsymbol_value (car);
14066 if (!NILP (tem))
14067 {
14068 elt = XCAR (elt);
14069 goto tail_recurse;
14070 }
14071 }
14072 /* Symbol's value is nil (or symbol is unbound)
14073 Get the cddr of the original list
14074 and if possible find the caddr and use that. */
14075 elt = XCDR (elt);
14076 if (NILP (elt))
14077 break;
14078 else if (!CONSP (elt))
14079 goto invalid;
14080 elt = XCAR (elt);
14081 goto tail_recurse;
14082 }
14083 else if (INTEGERP (car))
14084 {
14085 register int lim = XINT (car);
14086 elt = XCDR (elt);
14087 if (lim < 0)
14088 {
14089 /* Negative int means reduce maximum width. */
14090 if (precision <= 0)
14091 precision = -lim;
14092 else
14093 precision = min (precision, -lim);
14094 }
14095 else if (lim > 0)
14096 {
14097 /* Padding specified. Don't let it be more than
14098 current maximum. */
14099 if (precision > 0)
14100 lim = min (precision, lim);
14101
14102 /* If that's more padding than already wanted, queue it.
14103 But don't reduce padding already specified even if
14104 that is beyond the current truncation point. */
14105 field_width = max (lim, field_width);
14106 }
14107 goto tail_recurse;
14108 }
14109 else if (STRINGP (car) || CONSP (car))
14110 {
14111 register int limit = 50;
14112 /* Limit is to protect against circular lists. */
14113 while (CONSP (elt)
14114 && --limit > 0
14115 && (precision <= 0 || n < precision))
14116 {
14117 n += display_mode_element (it, depth, field_width - n,
14118 precision - n, XCAR (elt),
14119 props, risky);
14120 elt = XCDR (elt);
14121 }
14122 }
14123 }
14124 break;
14125
14126 default:
14127 invalid:
14128 if (frame_title_ptr)
14129 n += store_frame_title ("*invalid*", 0, precision - n);
14130 else if (!NILP (mode_line_string_list))
14131 n += store_mode_line_string ("*invalid*", Qnil, 0, 0, precision - n, Qnil);
14132 else
14133 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
14134 precision - n, 0, 0);
14135 return n;
14136 }
14137
14138 /* Pad to FIELD_WIDTH. */
14139 if (field_width > 0 && n < field_width)
14140 {
14141 if (frame_title_ptr)
14142 n += store_frame_title ("", field_width - n, 0);
14143 else if (!NILP (mode_line_string_list))
14144 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
14145 else
14146 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
14147 0, 0, 0);
14148 }
14149
14150 return n;
14151 }
14152
14153 /* Store a mode-line string element in mode_line_string_list.
14154
14155 If STRING is non-null, display that C string. Otherwise, the Lisp
14156 string LISP_STRING is displayed.
14157
14158 FIELD_WIDTH is the minimum number of output glyphs to produce.
14159 If STRING has fewer characters than FIELD_WIDTH, pad to the right
14160 with spaces. FIELD_WIDTH <= 0 means don't pad.
14161
14162 PRECISION is the maximum number of characters to output from
14163 STRING. PRECISION <= 0 means don't truncate the string.
14164
14165 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
14166 properties to the string.
14167
14168 PROPS are the properties to add to the string.
14169 The mode_line_string_face face property is always added to the string.
14170 */
14171
14172 static int store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
14173 char *string;
14174 Lisp_Object lisp_string;
14175 int copy_string;
14176 int field_width;
14177 int precision;
14178 Lisp_Object props;
14179 {
14180 int len;
14181 int n = 0;
14182
14183 if (string != NULL)
14184 {
14185 len = strlen (string);
14186 if (precision > 0 && len > precision)
14187 len = precision;
14188 lisp_string = make_string (string, len);
14189 if (NILP (props))
14190 props = mode_line_string_face_prop;
14191 else if (!NILP (mode_line_string_face))
14192 {
14193 Lisp_Object face = Fplist_get (props, Qface);
14194 props = Fcopy_sequence (props);
14195 if (NILP (face))
14196 face = mode_line_string_face;
14197 else
14198 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
14199 props = Fplist_put (props, Qface, face);
14200 }
14201 Fadd_text_properties (make_number (0), make_number (len),
14202 props, lisp_string);
14203 }
14204 else
14205 {
14206 len = XFASTINT (Flength (lisp_string));
14207 if (precision > 0 && len > precision)
14208 {
14209 len = precision;
14210 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
14211 precision = -1;
14212 }
14213 if (!NILP (mode_line_string_face))
14214 {
14215 Lisp_Object face;
14216 if (NILP (props))
14217 props = Ftext_properties_at (make_number (0), lisp_string);
14218 face = Fplist_get (props, Qface);
14219 if (NILP (face))
14220 face = mode_line_string_face;
14221 else
14222 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
14223 props = Fcons (Qface, Fcons (face, Qnil));
14224 if (copy_string)
14225 lisp_string = Fcopy_sequence (lisp_string);
14226 }
14227 if (!NILP (props))
14228 Fadd_text_properties (make_number (0), make_number (len),
14229 props, lisp_string);
14230 }
14231
14232 if (len > 0)
14233 {
14234 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
14235 n += len;
14236 }
14237
14238 if (field_width > len)
14239 {
14240 field_width -= len;
14241 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
14242 if (!NILP (props))
14243 Fadd_text_properties (make_number (0), make_number (field_width),
14244 props, lisp_string);
14245 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
14246 n += field_width;
14247 }
14248
14249 return n;
14250 }
14251
14252
14253 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
14254 0, 3, 0,
14255 doc: /* Return the mode-line of selected window as a string.
14256 First optional arg FORMAT specifies a different format string (see
14257 `mode-line-format' for details) to use. If FORMAT is t, return
14258 the buffer's header-line. Second optional arg WINDOW specifies a
14259 different window to use as the context for the formatting.
14260 If third optional arg NO-PROPS is non-nil, string is not propertized. */)
14261 (format, window, no_props)
14262 Lisp_Object format, window, no_props;
14263 {
14264 struct it it;
14265 int len;
14266 struct window *w;
14267 struct buffer *old_buffer = NULL;
14268 enum face_id face_id = DEFAULT_FACE_ID;
14269
14270 if (NILP (window))
14271 window = selected_window;
14272 CHECK_WINDOW (window);
14273 w = XWINDOW (window);
14274 CHECK_BUFFER (w->buffer);
14275
14276 if (XBUFFER (w->buffer) != current_buffer)
14277 {
14278 old_buffer = current_buffer;
14279 set_buffer_internal_1 (XBUFFER (w->buffer));
14280 }
14281
14282 if (NILP (format) || EQ (format, Qt))
14283 {
14284 face_id = NILP (format)
14285 ? CURRENT_MODE_LINE_FACE_ID (w) :
14286 HEADER_LINE_FACE_ID;
14287 format = NILP (format)
14288 ? current_buffer->mode_line_format
14289 : current_buffer->header_line_format;
14290 }
14291
14292 init_iterator (&it, w, -1, -1, NULL, face_id);
14293
14294 if (NILP (no_props))
14295 {
14296 mode_line_string_face =
14297 (face_id == MODE_LINE_FACE_ID ? Qmode_line :
14298 face_id == MODE_LINE_INACTIVE_FACE_ID ? Qmode_line_inactive :
14299 face_id == HEADER_LINE_FACE_ID ? Qheader_line : Qnil);
14300
14301 mode_line_string_face_prop =
14302 NILP (mode_line_string_face) ? Qnil :
14303 Fcons (Qface, Fcons (mode_line_string_face, Qnil));
14304
14305 /* We need a dummy last element in mode_line_string_list to
14306 indicate we are building the propertized mode-line string.
14307 Using mode_line_string_face_prop here GC protects it. */
14308 mode_line_string_list =
14309 Fcons (mode_line_string_face_prop, Qnil);
14310 frame_title_ptr = NULL;
14311 }
14312 else
14313 {
14314 mode_line_string_face_prop = Qnil;
14315 mode_line_string_list = Qnil;
14316 frame_title_ptr = frame_title_buf;
14317 }
14318
14319 push_frame_kboard (it.f);
14320 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
14321 pop_frame_kboard ();
14322
14323 if (old_buffer)
14324 set_buffer_internal_1 (old_buffer);
14325
14326 if (NILP (no_props))
14327 {
14328 Lisp_Object str;
14329 mode_line_string_list = Fnreverse (mode_line_string_list);
14330 str = Fmapconcat (intern ("identity"), XCDR (mode_line_string_list),
14331 make_string ("", 0));
14332 mode_line_string_face_prop = Qnil;
14333 mode_line_string_list = Qnil;
14334 return str;
14335 }
14336
14337 len = frame_title_ptr - frame_title_buf;
14338 if (len > 0 && frame_title_ptr[-1] == '-')
14339 {
14340 /* Mode lines typically ends with numerous dashes; reduce to two dashes. */
14341 while (frame_title_ptr > frame_title_buf && *--frame_title_ptr == '-')
14342 ;
14343 frame_title_ptr += 3; /* restore last non-dash + two dashes */
14344 if (len > frame_title_ptr - frame_title_buf)
14345 len = frame_title_ptr - frame_title_buf;
14346 }
14347
14348 frame_title_ptr = NULL;
14349 return make_string (frame_title_buf, len);
14350 }
14351
14352 /* Write a null-terminated, right justified decimal representation of
14353 the positive integer D to BUF using a minimal field width WIDTH. */
14354
14355 static void
14356 pint2str (buf, width, d)
14357 register char *buf;
14358 register int width;
14359 register int d;
14360 {
14361 register char *p = buf;
14362
14363 if (d <= 0)
14364 *p++ = '0';
14365 else
14366 {
14367 while (d > 0)
14368 {
14369 *p++ = d % 10 + '0';
14370 d /= 10;
14371 }
14372 }
14373
14374 for (width -= (int) (p - buf); width > 0; --width)
14375 *p++ = ' ';
14376 *p-- = '\0';
14377 while (p > buf)
14378 {
14379 d = *buf;
14380 *buf++ = *p;
14381 *p-- = d;
14382 }
14383 }
14384
14385 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
14386 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
14387 type of CODING_SYSTEM. Return updated pointer into BUF. */
14388
14389 static unsigned char invalid_eol_type[] = "(*invalid*)";
14390
14391 static char *
14392 decode_mode_spec_coding (coding_system, buf, eol_flag)
14393 Lisp_Object coding_system;
14394 register char *buf;
14395 int eol_flag;
14396 {
14397 Lisp_Object val;
14398 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
14399 const unsigned char *eol_str;
14400 int eol_str_len;
14401 /* The EOL conversion we are using. */
14402 Lisp_Object eoltype;
14403
14404 val = Fget (coding_system, Qcoding_system);
14405 eoltype = Qnil;
14406
14407 if (!VECTORP (val)) /* Not yet decided. */
14408 {
14409 if (multibyte)
14410 *buf++ = '-';
14411 if (eol_flag)
14412 eoltype = eol_mnemonic_undecided;
14413 /* Don't mention EOL conversion if it isn't decided. */
14414 }
14415 else
14416 {
14417 Lisp_Object eolvalue;
14418
14419 eolvalue = Fget (coding_system, Qeol_type);
14420
14421 if (multibyte)
14422 *buf++ = XFASTINT (AREF (val, 1));
14423
14424 if (eol_flag)
14425 {
14426 /* The EOL conversion that is normal on this system. */
14427
14428 if (NILP (eolvalue)) /* Not yet decided. */
14429 eoltype = eol_mnemonic_undecided;
14430 else if (VECTORP (eolvalue)) /* Not yet decided. */
14431 eoltype = eol_mnemonic_undecided;
14432 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
14433 eoltype = (XFASTINT (eolvalue) == 0
14434 ? eol_mnemonic_unix
14435 : (XFASTINT (eolvalue) == 1
14436 ? eol_mnemonic_dos : eol_mnemonic_mac));
14437 }
14438 }
14439
14440 if (eol_flag)
14441 {
14442 /* Mention the EOL conversion if it is not the usual one. */
14443 if (STRINGP (eoltype))
14444 {
14445 eol_str = SDATA (eoltype);
14446 eol_str_len = SBYTES (eoltype);
14447 }
14448 else if (INTEGERP (eoltype)
14449 && CHAR_VALID_P (XINT (eoltype), 0))
14450 {
14451 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
14452 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
14453 eol_str = tmp;
14454 }
14455 else
14456 {
14457 eol_str = invalid_eol_type;
14458 eol_str_len = sizeof (invalid_eol_type) - 1;
14459 }
14460 bcopy (eol_str, buf, eol_str_len);
14461 buf += eol_str_len;
14462 }
14463
14464 return buf;
14465 }
14466
14467 /* Return a string for the output of a mode line %-spec for window W,
14468 generated by character C. PRECISION >= 0 means don't return a
14469 string longer than that value. FIELD_WIDTH > 0 means pad the
14470 string returned with spaces to that value. Return 1 in *MULTIBYTE
14471 if the result is multibyte text. */
14472
14473 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
14474
14475 static char *
14476 decode_mode_spec (w, c, field_width, precision, multibyte)
14477 struct window *w;
14478 register int c;
14479 int field_width, precision;
14480 int *multibyte;
14481 {
14482 Lisp_Object obj;
14483 struct frame *f = XFRAME (WINDOW_FRAME (w));
14484 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
14485 struct buffer *b = XBUFFER (w->buffer);
14486
14487 obj = Qnil;
14488 *multibyte = 0;
14489
14490 switch (c)
14491 {
14492 case '*':
14493 if (!NILP (b->read_only))
14494 return "%";
14495 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
14496 return "*";
14497 return "-";
14498
14499 case '+':
14500 /* This differs from %* only for a modified read-only buffer. */
14501 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
14502 return "*";
14503 if (!NILP (b->read_only))
14504 return "%";
14505 return "-";
14506
14507 case '&':
14508 /* This differs from %* in ignoring read-only-ness. */
14509 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
14510 return "*";
14511 return "-";
14512
14513 case '%':
14514 return "%";
14515
14516 case '[':
14517 {
14518 int i;
14519 char *p;
14520
14521 if (command_loop_level > 5)
14522 return "[[[... ";
14523 p = decode_mode_spec_buf;
14524 for (i = 0; i < command_loop_level; i++)
14525 *p++ = '[';
14526 *p = 0;
14527 return decode_mode_spec_buf;
14528 }
14529
14530 case ']':
14531 {
14532 int i;
14533 char *p;
14534
14535 if (command_loop_level > 5)
14536 return " ...]]]";
14537 p = decode_mode_spec_buf;
14538 for (i = 0; i < command_loop_level; i++)
14539 *p++ = ']';
14540 *p = 0;
14541 return decode_mode_spec_buf;
14542 }
14543
14544 case '-':
14545 {
14546 register int i;
14547
14548 /* Let lots_of_dashes be a string of infinite length. */
14549 if (!NILP (mode_line_string_list))
14550 return "--";
14551 if (field_width <= 0
14552 || field_width > sizeof (lots_of_dashes))
14553 {
14554 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
14555 decode_mode_spec_buf[i] = '-';
14556 decode_mode_spec_buf[i] = '\0';
14557 return decode_mode_spec_buf;
14558 }
14559 else
14560 return lots_of_dashes;
14561 }
14562
14563 case 'b':
14564 obj = b->name;
14565 break;
14566
14567 case 'c':
14568 {
14569 int col = (int) current_column (); /* iftc */
14570 w->column_number_displayed = make_number (col);
14571 pint2str (decode_mode_spec_buf, field_width, col);
14572 return decode_mode_spec_buf;
14573 }
14574
14575 case 'F':
14576 /* %F displays the frame name. */
14577 if (!NILP (f->title))
14578 return (char *) SDATA (f->title);
14579 if (f->explicit_name || ! FRAME_WINDOW_P (f))
14580 return (char *) SDATA (f->name);
14581 return "Emacs";
14582
14583 case 'f':
14584 obj = b->filename;
14585 break;
14586
14587 case 'l':
14588 {
14589 int startpos = XMARKER (w->start)->charpos;
14590 int startpos_byte = marker_byte_position (w->start);
14591 int line, linepos, linepos_byte, topline;
14592 int nlines, junk;
14593 int height = XFASTINT (w->height);
14594
14595 /* If we decided that this buffer isn't suitable for line numbers,
14596 don't forget that too fast. */
14597 if (EQ (w->base_line_pos, w->buffer))
14598 goto no_value;
14599 /* But do forget it, if the window shows a different buffer now. */
14600 else if (BUFFERP (w->base_line_pos))
14601 w->base_line_pos = Qnil;
14602
14603 /* If the buffer is very big, don't waste time. */
14604 if (INTEGERP (Vline_number_display_limit)
14605 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
14606 {
14607 w->base_line_pos = Qnil;
14608 w->base_line_number = Qnil;
14609 goto no_value;
14610 }
14611
14612 if (!NILP (w->base_line_number)
14613 && !NILP (w->base_line_pos)
14614 && XFASTINT (w->base_line_pos) <= startpos)
14615 {
14616 line = XFASTINT (w->base_line_number);
14617 linepos = XFASTINT (w->base_line_pos);
14618 linepos_byte = buf_charpos_to_bytepos (b, linepos);
14619 }
14620 else
14621 {
14622 line = 1;
14623 linepos = BUF_BEGV (b);
14624 linepos_byte = BUF_BEGV_BYTE (b);
14625 }
14626
14627 /* Count lines from base line to window start position. */
14628 nlines = display_count_lines (linepos, linepos_byte,
14629 startpos_byte,
14630 startpos, &junk);
14631
14632 topline = nlines + line;
14633
14634 /* Determine a new base line, if the old one is too close
14635 or too far away, or if we did not have one.
14636 "Too close" means it's plausible a scroll-down would
14637 go back past it. */
14638 if (startpos == BUF_BEGV (b))
14639 {
14640 w->base_line_number = make_number (topline);
14641 w->base_line_pos = make_number (BUF_BEGV (b));
14642 }
14643 else if (nlines < height + 25 || nlines > height * 3 + 50
14644 || linepos == BUF_BEGV (b))
14645 {
14646 int limit = BUF_BEGV (b);
14647 int limit_byte = BUF_BEGV_BYTE (b);
14648 int position;
14649 int distance = (height * 2 + 30) * line_number_display_limit_width;
14650
14651 if (startpos - distance > limit)
14652 {
14653 limit = startpos - distance;
14654 limit_byte = CHAR_TO_BYTE (limit);
14655 }
14656
14657 nlines = display_count_lines (startpos, startpos_byte,
14658 limit_byte,
14659 - (height * 2 + 30),
14660 &position);
14661 /* If we couldn't find the lines we wanted within
14662 line_number_display_limit_width chars per line,
14663 give up on line numbers for this window. */
14664 if (position == limit_byte && limit == startpos - distance)
14665 {
14666 w->base_line_pos = w->buffer;
14667 w->base_line_number = Qnil;
14668 goto no_value;
14669 }
14670
14671 w->base_line_number = make_number (topline - nlines);
14672 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
14673 }
14674
14675 /* Now count lines from the start pos to point. */
14676 nlines = display_count_lines (startpos, startpos_byte,
14677 PT_BYTE, PT, &junk);
14678
14679 /* Record that we did display the line number. */
14680 line_number_displayed = 1;
14681
14682 /* Make the string to show. */
14683 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
14684 return decode_mode_spec_buf;
14685 no_value:
14686 {
14687 char* p = decode_mode_spec_buf;
14688 int pad = field_width - 2;
14689 while (pad-- > 0)
14690 *p++ = ' ';
14691 *p++ = '?';
14692 *p++ = '?';
14693 *p = '\0';
14694 return decode_mode_spec_buf;
14695 }
14696 }
14697 break;
14698
14699 case 'm':
14700 obj = b->mode_name;
14701 break;
14702
14703 case 'n':
14704 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
14705 return " Narrow";
14706 break;
14707
14708 case 'p':
14709 {
14710 int pos = marker_position (w->start);
14711 int total = BUF_ZV (b) - BUF_BEGV (b);
14712
14713 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
14714 {
14715 if (pos <= BUF_BEGV (b))
14716 return "All";
14717 else
14718 return "Bottom";
14719 }
14720 else if (pos <= BUF_BEGV (b))
14721 return "Top";
14722 else
14723 {
14724 if (total > 1000000)
14725 /* Do it differently for a large value, to avoid overflow. */
14726 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
14727 else
14728 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
14729 /* We can't normally display a 3-digit number,
14730 so get us a 2-digit number that is close. */
14731 if (total == 100)
14732 total = 99;
14733 sprintf (decode_mode_spec_buf, "%2d%%", total);
14734 return decode_mode_spec_buf;
14735 }
14736 }
14737
14738 /* Display percentage of size above the bottom of the screen. */
14739 case 'P':
14740 {
14741 int toppos = marker_position (w->start);
14742 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
14743 int total = BUF_ZV (b) - BUF_BEGV (b);
14744
14745 if (botpos >= BUF_ZV (b))
14746 {
14747 if (toppos <= BUF_BEGV (b))
14748 return "All";
14749 else
14750 return "Bottom";
14751 }
14752 else
14753 {
14754 if (total > 1000000)
14755 /* Do it differently for a large value, to avoid overflow. */
14756 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
14757 else
14758 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
14759 /* We can't normally display a 3-digit number,
14760 so get us a 2-digit number that is close. */
14761 if (total == 100)
14762 total = 99;
14763 if (toppos <= BUF_BEGV (b))
14764 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
14765 else
14766 sprintf (decode_mode_spec_buf, "%2d%%", total);
14767 return decode_mode_spec_buf;
14768 }
14769 }
14770
14771 case 's':
14772 /* status of process */
14773 obj = Fget_buffer_process (w->buffer);
14774 if (NILP (obj))
14775 return "no process";
14776 #ifdef subprocesses
14777 obj = Fsymbol_name (Fprocess_status (obj));
14778 #endif
14779 break;
14780
14781 case 't': /* indicate TEXT or BINARY */
14782 #ifdef MODE_LINE_BINARY_TEXT
14783 return MODE_LINE_BINARY_TEXT (b);
14784 #else
14785 return "T";
14786 #endif
14787
14788 case 'z':
14789 /* coding-system (not including end-of-line format) */
14790 case 'Z':
14791 /* coding-system (including end-of-line type) */
14792 {
14793 int eol_flag = (c == 'Z');
14794 char *p = decode_mode_spec_buf;
14795
14796 if (! FRAME_WINDOW_P (f))
14797 {
14798 /* No need to mention EOL here--the terminal never needs
14799 to do EOL conversion. */
14800 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
14801 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
14802 }
14803 p = decode_mode_spec_coding (b->buffer_file_coding_system,
14804 p, eol_flag);
14805
14806 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
14807 #ifdef subprocesses
14808 obj = Fget_buffer_process (Fcurrent_buffer ());
14809 if (PROCESSP (obj))
14810 {
14811 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
14812 p, eol_flag);
14813 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
14814 p, eol_flag);
14815 }
14816 #endif /* subprocesses */
14817 #endif /* 0 */
14818 *p = 0;
14819 return decode_mode_spec_buf;
14820 }
14821 }
14822
14823 if (STRINGP (obj))
14824 {
14825 *multibyte = STRING_MULTIBYTE (obj);
14826 return (char *) SDATA (obj);
14827 }
14828 else
14829 return "";
14830 }
14831
14832
14833 /* Count up to COUNT lines starting from START / START_BYTE.
14834 But don't go beyond LIMIT_BYTE.
14835 Return the number of lines thus found (always nonnegative).
14836
14837 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
14838
14839 static int
14840 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
14841 int start, start_byte, limit_byte, count;
14842 int *byte_pos_ptr;
14843 {
14844 register unsigned char *cursor;
14845 unsigned char *base;
14846
14847 register int ceiling;
14848 register unsigned char *ceiling_addr;
14849 int orig_count = count;
14850
14851 /* If we are not in selective display mode,
14852 check only for newlines. */
14853 int selective_display = (!NILP (current_buffer->selective_display)
14854 && !INTEGERP (current_buffer->selective_display));
14855
14856 if (count > 0)
14857 {
14858 while (start_byte < limit_byte)
14859 {
14860 ceiling = BUFFER_CEILING_OF (start_byte);
14861 ceiling = min (limit_byte - 1, ceiling);
14862 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
14863 base = (cursor = BYTE_POS_ADDR (start_byte));
14864 while (1)
14865 {
14866 if (selective_display)
14867 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
14868 ;
14869 else
14870 while (*cursor != '\n' && ++cursor != ceiling_addr)
14871 ;
14872
14873 if (cursor != ceiling_addr)
14874 {
14875 if (--count == 0)
14876 {
14877 start_byte += cursor - base + 1;
14878 *byte_pos_ptr = start_byte;
14879 return orig_count;
14880 }
14881 else
14882 if (++cursor == ceiling_addr)
14883 break;
14884 }
14885 else
14886 break;
14887 }
14888 start_byte += cursor - base;
14889 }
14890 }
14891 else
14892 {
14893 while (start_byte > limit_byte)
14894 {
14895 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
14896 ceiling = max (limit_byte, ceiling);
14897 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
14898 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
14899 while (1)
14900 {
14901 if (selective_display)
14902 while (--cursor != ceiling_addr
14903 && *cursor != '\n' && *cursor != 015)
14904 ;
14905 else
14906 while (--cursor != ceiling_addr && *cursor != '\n')
14907 ;
14908
14909 if (cursor != ceiling_addr)
14910 {
14911 if (++count == 0)
14912 {
14913 start_byte += cursor - base + 1;
14914 *byte_pos_ptr = start_byte;
14915 /* When scanning backwards, we should
14916 not count the newline posterior to which we stop. */
14917 return - orig_count - 1;
14918 }
14919 }
14920 else
14921 break;
14922 }
14923 /* Here we add 1 to compensate for the last decrement
14924 of CURSOR, which took it past the valid range. */
14925 start_byte += cursor - base + 1;
14926 }
14927 }
14928
14929 *byte_pos_ptr = limit_byte;
14930
14931 if (count < 0)
14932 return - orig_count + count;
14933 return orig_count - count;
14934
14935 }
14936
14937
14938 \f
14939 /***********************************************************************
14940 Displaying strings
14941 ***********************************************************************/
14942
14943 /* Display a NUL-terminated string, starting with index START.
14944
14945 If STRING is non-null, display that C string. Otherwise, the Lisp
14946 string LISP_STRING is displayed.
14947
14948 If FACE_STRING is not nil, FACE_STRING_POS is a position in
14949 FACE_STRING. Display STRING or LISP_STRING with the face at
14950 FACE_STRING_POS in FACE_STRING:
14951
14952 Display the string in the environment given by IT, but use the
14953 standard display table, temporarily.
14954
14955 FIELD_WIDTH is the minimum number of output glyphs to produce.
14956 If STRING has fewer characters than FIELD_WIDTH, pad to the right
14957 with spaces. If STRING has more characters, more than FIELD_WIDTH
14958 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
14959
14960 PRECISION is the maximum number of characters to output from
14961 STRING. PRECISION < 0 means don't truncate the string.
14962
14963 This is roughly equivalent to printf format specifiers:
14964
14965 FIELD_WIDTH PRECISION PRINTF
14966 ----------------------------------------
14967 -1 -1 %s
14968 -1 10 %.10s
14969 10 -1 %10s
14970 20 10 %20.10s
14971
14972 MULTIBYTE zero means do not display multibyte chars, > 0 means do
14973 display them, and < 0 means obey the current buffer's value of
14974 enable_multibyte_characters.
14975
14976 Value is the number of glyphs produced. */
14977
14978 static int
14979 display_string (string, lisp_string, face_string, face_string_pos,
14980 start, it, field_width, precision, max_x, multibyte)
14981 unsigned char *string;
14982 Lisp_Object lisp_string;
14983 Lisp_Object face_string;
14984 int face_string_pos;
14985 int start;
14986 struct it *it;
14987 int field_width, precision, max_x;
14988 int multibyte;
14989 {
14990 int hpos_at_start = it->hpos;
14991 int saved_face_id = it->face_id;
14992 struct glyph_row *row = it->glyph_row;
14993
14994 /* Initialize the iterator IT for iteration over STRING beginning
14995 with index START. */
14996 reseat_to_string (it, string, lisp_string, start,
14997 precision, field_width, multibyte);
14998
14999 /* If displaying STRING, set up the face of the iterator
15000 from LISP_STRING, if that's given. */
15001 if (STRINGP (face_string))
15002 {
15003 int endptr;
15004 struct face *face;
15005
15006 it->face_id
15007 = face_at_string_position (it->w, face_string, face_string_pos,
15008 0, it->region_beg_charpos,
15009 it->region_end_charpos,
15010 &endptr, it->base_face_id, 0);
15011 face = FACE_FROM_ID (it->f, it->face_id);
15012 it->face_box_p = face->box != FACE_NO_BOX;
15013 }
15014
15015 /* Set max_x to the maximum allowed X position. Don't let it go
15016 beyond the right edge of the window. */
15017 if (max_x <= 0)
15018 max_x = it->last_visible_x;
15019 else
15020 max_x = min (max_x, it->last_visible_x);
15021
15022 /* Skip over display elements that are not visible. because IT->w is
15023 hscrolled. */
15024 if (it->current_x < it->first_visible_x)
15025 move_it_in_display_line_to (it, 100000, it->first_visible_x,
15026 MOVE_TO_POS | MOVE_TO_X);
15027
15028 row->ascent = it->max_ascent;
15029 row->height = it->max_ascent + it->max_descent;
15030 row->phys_ascent = it->max_phys_ascent;
15031 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15032
15033 /* This condition is for the case that we are called with current_x
15034 past last_visible_x. */
15035 while (it->current_x < max_x)
15036 {
15037 int x_before, x, n_glyphs_before, i, nglyphs;
15038
15039 /* Get the next display element. */
15040 if (!get_next_display_element (it))
15041 break;
15042
15043 /* Produce glyphs. */
15044 x_before = it->current_x;
15045 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
15046 PRODUCE_GLYPHS (it);
15047
15048 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
15049 i = 0;
15050 x = x_before;
15051 while (i < nglyphs)
15052 {
15053 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
15054
15055 if (!it->truncate_lines_p
15056 && x + glyph->pixel_width > max_x)
15057 {
15058 /* End of continued line or max_x reached. */
15059 if (CHAR_GLYPH_PADDING_P (*glyph))
15060 {
15061 /* A wide character is unbreakable. */
15062 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
15063 it->current_x = x_before;
15064 }
15065 else
15066 {
15067 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
15068 it->current_x = x;
15069 }
15070 break;
15071 }
15072 else if (x + glyph->pixel_width > it->first_visible_x)
15073 {
15074 /* Glyph is at least partially visible. */
15075 ++it->hpos;
15076 if (x < it->first_visible_x)
15077 it->glyph_row->x = x - it->first_visible_x;
15078 }
15079 else
15080 {
15081 /* Glyph is off the left margin of the display area.
15082 Should not happen. */
15083 abort ();
15084 }
15085
15086 row->ascent = max (row->ascent, it->max_ascent);
15087 row->height = max (row->height, it->max_ascent + it->max_descent);
15088 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15089 row->phys_height = max (row->phys_height,
15090 it->max_phys_ascent + it->max_phys_descent);
15091 x += glyph->pixel_width;
15092 ++i;
15093 }
15094
15095 /* Stop if max_x reached. */
15096 if (i < nglyphs)
15097 break;
15098
15099 /* Stop at line ends. */
15100 if (ITERATOR_AT_END_OF_LINE_P (it))
15101 {
15102 it->continuation_lines_width = 0;
15103 break;
15104 }
15105
15106 set_iterator_to_next (it, 1);
15107
15108 /* Stop if truncating at the right edge. */
15109 if (it->truncate_lines_p
15110 && it->current_x >= it->last_visible_x)
15111 {
15112 /* Add truncation mark, but don't do it if the line is
15113 truncated at a padding space. */
15114 if (IT_CHARPOS (*it) < it->string_nchars)
15115 {
15116 if (!FRAME_WINDOW_P (it->f))
15117 {
15118 int i, n;
15119
15120 if (it->current_x > it->last_visible_x)
15121 {
15122 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
15123 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
15124 break;
15125 for (n = row->used[TEXT_AREA]; i < n; ++i)
15126 {
15127 row->used[TEXT_AREA] = i;
15128 produce_special_glyphs (it, IT_TRUNCATION);
15129 }
15130 }
15131 produce_special_glyphs (it, IT_TRUNCATION);
15132 }
15133 it->glyph_row->truncated_on_right_p = 1;
15134 }
15135 break;
15136 }
15137 }
15138
15139 /* Maybe insert a truncation at the left. */
15140 if (it->first_visible_x
15141 && IT_CHARPOS (*it) > 0)
15142 {
15143 if (!FRAME_WINDOW_P (it->f))
15144 insert_left_trunc_glyphs (it);
15145 it->glyph_row->truncated_on_left_p = 1;
15146 }
15147
15148 it->face_id = saved_face_id;
15149
15150 /* Value is number of columns displayed. */
15151 return it->hpos - hpos_at_start;
15152 }
15153
15154
15155 \f
15156 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
15157 appears as an element of LIST or as the car of an element of LIST.
15158 If PROPVAL is a list, compare each element against LIST in that
15159 way, and return 1/2 if any element of PROPVAL is found in LIST.
15160 Otherwise return 0. This function cannot quit.
15161 The return value is 2 if the text is invisible but with an ellipsis
15162 and 1 if it's invisible and without an ellipsis. */
15163
15164 int
15165 invisible_p (propval, list)
15166 register Lisp_Object propval;
15167 Lisp_Object list;
15168 {
15169 register Lisp_Object tail, proptail;
15170
15171 for (tail = list; CONSP (tail); tail = XCDR (tail))
15172 {
15173 register Lisp_Object tem;
15174 tem = XCAR (tail);
15175 if (EQ (propval, tem))
15176 return 1;
15177 if (CONSP (tem) && EQ (propval, XCAR (tem)))
15178 return NILP (XCDR (tem)) ? 1 : 2;
15179 }
15180
15181 if (CONSP (propval))
15182 {
15183 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
15184 {
15185 Lisp_Object propelt;
15186 propelt = XCAR (proptail);
15187 for (tail = list; CONSP (tail); tail = XCDR (tail))
15188 {
15189 register Lisp_Object tem;
15190 tem = XCAR (tail);
15191 if (EQ (propelt, tem))
15192 return 1;
15193 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
15194 return NILP (XCDR (tem)) ? 1 : 2;
15195 }
15196 }
15197 }
15198
15199 return 0;
15200 }
15201
15202 \f
15203 /***********************************************************************
15204 Cursor types
15205 ***********************************************************************/
15206
15207 /* Value is the internal representation of the specified cursor type
15208 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
15209 of the bar cursor. */
15210
15211 enum text_cursor_kinds
15212 get_specified_cursor_type (arg, width)
15213 Lisp_Object arg;
15214 int *width;
15215 {
15216 enum text_cursor_kinds type;
15217
15218 if (NILP (arg))
15219 return NO_CURSOR;
15220
15221 if (EQ (arg, Qbox))
15222 return FILLED_BOX_CURSOR;
15223
15224 if (EQ (arg, Qhollow))
15225 return HOLLOW_BOX_CURSOR;
15226
15227 if (EQ (arg, Qbar))
15228 {
15229 *width = 2;
15230 return BAR_CURSOR;
15231 }
15232
15233 if (CONSP (arg)
15234 && EQ (XCAR (arg), Qbar)
15235 && INTEGERP (XCDR (arg))
15236 && XINT (XCDR (arg)) >= 0)
15237 {
15238 *width = XINT (XCDR (arg));
15239 return BAR_CURSOR;
15240 }
15241
15242 if (EQ (arg, Qhbar))
15243 {
15244 *width = 2;
15245 return HBAR_CURSOR;
15246 }
15247
15248 if (CONSP (arg)
15249 && EQ (XCAR (arg), Qhbar)
15250 && INTEGERP (XCDR (arg))
15251 && XINT (XCDR (arg)) >= 0)
15252 {
15253 *width = XINT (XCDR (arg));
15254 return HBAR_CURSOR;
15255 }
15256
15257 /* Treat anything unknown as "hollow box cursor".
15258 It was bad to signal an error; people have trouble fixing
15259 .Xdefaults with Emacs, when it has something bad in it. */
15260 type = HOLLOW_BOX_CURSOR;
15261
15262 return type;
15263 }
15264
15265 /* Set the default cursor types for specified frame. */
15266 void
15267 set_frame_cursor_types (f, arg)
15268 struct frame *f;
15269 Lisp_Object arg;
15270 {
15271 int width;
15272 Lisp_Object tem;
15273
15274 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
15275 FRAME_CURSOR_WIDTH (f) = width;
15276
15277 /* By default, set up the blink-off state depending on the on-state. */
15278
15279 tem = Fassoc (arg, Vblink_cursor_alist);
15280 if (!NILP (tem))
15281 {
15282 FRAME_BLINK_OFF_CURSOR (f)
15283 = get_specified_cursor_type (XCDR (tem), &width);
15284 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
15285 }
15286 else
15287 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
15288 }
15289
15290
15291 /* Return the cursor we want to be displayed. In a mini-buffer
15292 window, we want the cursor only to appear if we are reading input
15293 from this window. For the selected window, we want the cursor type
15294 given by the frame parameter or buffer local setting of
15295 cursor-type. If explicitly marked off, draw no cursor. In all
15296 other cases, we want a hollow box cursor. */
15297
15298 enum text_cursor_kinds
15299 get_window_cursor_type (w, width)
15300 struct window *w;
15301 int *width;
15302 {
15303 struct frame *f = XFRAME (w->frame);
15304 struct buffer *b = XBUFFER (w->buffer);
15305 int cursor_type = DEFAULT_CURSOR;
15306 Lisp_Object alt_cursor;
15307 int non_selected = 0;
15308
15309 /* Echo area */
15310 if (cursor_in_echo_area
15311 && FRAME_HAS_MINIBUF_P (f)
15312 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
15313 {
15314 if (w == XWINDOW (echo_area_window))
15315 {
15316 *width = FRAME_CURSOR_WIDTH (f);
15317 return FRAME_DESIRED_CURSOR (f);
15318 }
15319
15320 non_selected = 1;
15321 }
15322
15323 /* Nonselected window or nonselected frame. */
15324 else if (w != XWINDOW (f->selected_window)
15325 #ifdef HAVE_WINDOW_SYSTEM
15326 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
15327 #endif
15328 )
15329 {
15330 if (MINI_WINDOW_P (w) && minibuf_level == 0)
15331 return NO_CURSOR;
15332
15333 non_selected = 1;
15334 }
15335
15336 /* Never display a cursor in a window in which cursor-type is nil. */
15337 if (NILP (b->cursor_type))
15338 return NO_CURSOR;
15339
15340 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
15341 if (non_selected)
15342 {
15343 alt_cursor = Fbuffer_local_value (Qcursor_in_non_selected_windows, w->buffer);
15344 return get_specified_cursor_type (alt_cursor, width);
15345 }
15346
15347 /* Get the normal cursor type for this window. */
15348 if (EQ (b->cursor_type, Qt))
15349 {
15350 cursor_type = FRAME_DESIRED_CURSOR (f);
15351 *width = FRAME_CURSOR_WIDTH (f);
15352 }
15353 else
15354 cursor_type = get_specified_cursor_type (b->cursor_type, width);
15355
15356 /* Use normal cursor if not blinked off. */
15357 if (!w->cursor_off_p)
15358 return cursor_type;
15359
15360 /* Cursor is blinked off, so determine how to "toggle" it. */
15361
15362 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
15363 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
15364 return get_specified_cursor_type (XCDR (alt_cursor), width);
15365
15366 /* Then see if frame has specified a specific blink off cursor type. */
15367 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
15368 {
15369 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
15370 return FRAME_BLINK_OFF_CURSOR (f);
15371 }
15372
15373 /* Finally perform built-in cursor blinking:
15374 filled box <-> hollow box
15375 wide [h]bar <-> narrow [h]bar
15376 narrow [h]bar <-> no cursor
15377 other type <-> no cursor */
15378
15379 if (cursor_type == FILLED_BOX_CURSOR)
15380 return HOLLOW_BOX_CURSOR;
15381
15382 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
15383 {
15384 *width = 1;
15385 return cursor_type;
15386 }
15387
15388 return NO_CURSOR;
15389 }
15390
15391 \f
15392 /***********************************************************************
15393 Initialization
15394 ***********************************************************************/
15395
15396 void
15397 syms_of_xdisp ()
15398 {
15399 Vwith_echo_area_save_vector = Qnil;
15400 staticpro (&Vwith_echo_area_save_vector);
15401
15402 Vmessage_stack = Qnil;
15403 staticpro (&Vmessage_stack);
15404
15405 Qinhibit_redisplay = intern ("inhibit-redisplay");
15406 staticpro (&Qinhibit_redisplay);
15407
15408 message_dolog_marker1 = Fmake_marker ();
15409 staticpro (&message_dolog_marker1);
15410 message_dolog_marker2 = Fmake_marker ();
15411 staticpro (&message_dolog_marker2);
15412 message_dolog_marker3 = Fmake_marker ();
15413 staticpro (&message_dolog_marker3);
15414
15415 #if GLYPH_DEBUG
15416 defsubr (&Sdump_frame_glyph_matrix);
15417 defsubr (&Sdump_glyph_matrix);
15418 defsubr (&Sdump_glyph_row);
15419 defsubr (&Sdump_tool_bar_row);
15420 defsubr (&Strace_redisplay);
15421 defsubr (&Strace_to_stderr);
15422 #endif
15423 #ifdef HAVE_WINDOW_SYSTEM
15424 defsubr (&Stool_bar_lines_needed);
15425 #endif
15426 defsubr (&Sformat_mode_line);
15427
15428 staticpro (&Qmenu_bar_update_hook);
15429 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
15430
15431 staticpro (&Qoverriding_terminal_local_map);
15432 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
15433
15434 staticpro (&Qoverriding_local_map);
15435 Qoverriding_local_map = intern ("overriding-local-map");
15436
15437 staticpro (&Qwindow_scroll_functions);
15438 Qwindow_scroll_functions = intern ("window-scroll-functions");
15439
15440 staticpro (&Qredisplay_end_trigger_functions);
15441 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
15442
15443 staticpro (&Qinhibit_point_motion_hooks);
15444 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
15445
15446 QCdata = intern (":data");
15447 staticpro (&QCdata);
15448 Qdisplay = intern ("display");
15449 staticpro (&Qdisplay);
15450 Qspace_width = intern ("space-width");
15451 staticpro (&Qspace_width);
15452 Qraise = intern ("raise");
15453 staticpro (&Qraise);
15454 Qspace = intern ("space");
15455 staticpro (&Qspace);
15456 Qmargin = intern ("margin");
15457 staticpro (&Qmargin);
15458 Qleft_margin = intern ("left-margin");
15459 staticpro (&Qleft_margin);
15460 Qright_margin = intern ("right-margin");
15461 staticpro (&Qright_margin);
15462 Qalign_to = intern ("align-to");
15463 staticpro (&Qalign_to);
15464 QCalign_to = intern (":align-to");
15465 staticpro (&QCalign_to);
15466 Qrelative_width = intern ("relative-width");
15467 staticpro (&Qrelative_width);
15468 QCrelative_width = intern (":relative-width");
15469 staticpro (&QCrelative_width);
15470 QCrelative_height = intern (":relative-height");
15471 staticpro (&QCrelative_height);
15472 QCeval = intern (":eval");
15473 staticpro (&QCeval);
15474 QCpropertize = intern (":propertize");
15475 staticpro (&QCpropertize);
15476 Qwhen = intern ("when");
15477 staticpro (&Qwhen);
15478 QCfile = intern (":file");
15479 staticpro (&QCfile);
15480 Qfontified = intern ("fontified");
15481 staticpro (&Qfontified);
15482 Qfontification_functions = intern ("fontification-functions");
15483 staticpro (&Qfontification_functions);
15484 Qtrailing_whitespace = intern ("trailing-whitespace");
15485 staticpro (&Qtrailing_whitespace);
15486 Qimage = intern ("image");
15487 staticpro (&Qimage);
15488 Qmessage_truncate_lines = intern ("message-truncate-lines");
15489 staticpro (&Qmessage_truncate_lines);
15490 Qcursor_in_non_selected_windows = intern ("cursor-in-non-selected-windows");
15491 staticpro (&Qcursor_in_non_selected_windows);
15492 Qgrow_only = intern ("grow-only");
15493 staticpro (&Qgrow_only);
15494 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
15495 staticpro (&Qinhibit_menubar_update);
15496 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
15497 staticpro (&Qinhibit_eval_during_redisplay);
15498 Qposition = intern ("position");
15499 staticpro (&Qposition);
15500 Qbuffer_position = intern ("buffer-position");
15501 staticpro (&Qbuffer_position);
15502 Qobject = intern ("object");
15503 staticpro (&Qobject);
15504 Qbar = intern ("bar");
15505 staticpro (&Qbar);
15506 Qhbar = intern ("hbar");
15507 staticpro (&Qhbar);
15508 Qbox = intern ("box");
15509 staticpro (&Qbox);
15510 Qhollow = intern ("hollow");
15511 staticpro (&Qhollow);
15512 Qrisky_local_variable = intern ("risky-local-variable");
15513 staticpro (&Qrisky_local_variable);
15514 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
15515 staticpro (&Qinhibit_free_realized_faces);
15516
15517 list_of_error = Fcons (intern ("error"), Qnil);
15518 staticpro (&list_of_error);
15519
15520 last_arrow_position = Qnil;
15521 last_arrow_string = Qnil;
15522 staticpro (&last_arrow_position);
15523 staticpro (&last_arrow_string);
15524
15525 echo_buffer[0] = echo_buffer[1] = Qnil;
15526 staticpro (&echo_buffer[0]);
15527 staticpro (&echo_buffer[1]);
15528
15529 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
15530 staticpro (&echo_area_buffer[0]);
15531 staticpro (&echo_area_buffer[1]);
15532
15533 Vmessages_buffer_name = build_string ("*Messages*");
15534 staticpro (&Vmessages_buffer_name);
15535
15536 mode_line_proptrans_alist = Qnil;
15537 staticpro (&mode_line_proptrans_alist);
15538
15539 mode_line_string_list = Qnil;
15540 staticpro (&mode_line_string_list);
15541
15542 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
15543 doc: /* Non-nil means highlight trailing whitespace.
15544 The face used for trailing whitespace is `trailing-whitespace'. */);
15545 Vshow_trailing_whitespace = Qnil;
15546
15547 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
15548 doc: /* Non-nil means don't actually do any redisplay.
15549 This is used for internal purposes. */);
15550 Vinhibit_redisplay = Qnil;
15551
15552 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
15553 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
15554 Vglobal_mode_string = Qnil;
15555
15556 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
15557 doc: /* Marker for where to display an arrow on top of the buffer text.
15558 This must be the beginning of a line in order to work.
15559 See also `overlay-arrow-string'. */);
15560 Voverlay_arrow_position = Qnil;
15561
15562 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
15563 doc: /* String to display as an arrow. See also `overlay-arrow-position'. */);
15564 Voverlay_arrow_string = Qnil;
15565
15566 DEFVAR_INT ("scroll-step", &scroll_step,
15567 doc: /* *The number of lines to try scrolling a window by when point moves out.
15568 If that fails to bring point back on frame, point is centered instead.
15569 If this is zero, point is always centered after it moves off frame.
15570 If you want scrolling to always be a line at a time, you should set
15571 `scroll-conservatively' to a large value rather than set this to 1. */);
15572
15573 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
15574 doc: /* *Scroll up to this many lines, to bring point back on screen.
15575 A value of zero means to scroll the text to center point vertically
15576 in the window. */);
15577 scroll_conservatively = 0;
15578
15579 DEFVAR_INT ("scroll-margin", &scroll_margin,
15580 doc: /* *Number of lines of margin at the top and bottom of a window.
15581 Recenter the window whenever point gets within this many lines
15582 of the top or bottom of the window. */);
15583 scroll_margin = 0;
15584
15585 #if GLYPH_DEBUG
15586 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
15587 #endif
15588
15589 DEFVAR_BOOL ("truncate-partial-width-windows",
15590 &truncate_partial_width_windows,
15591 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
15592 truncate_partial_width_windows = 1;
15593
15594 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
15595 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
15596 Any other value means to use the appropriate face, `mode-line',
15597 `header-line', or `menu' respectively. */);
15598 mode_line_inverse_video = 1;
15599
15600 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
15601 doc: /* *Maximum buffer size for which line number should be displayed.
15602 If the buffer is bigger than this, the line number does not appear
15603 in the mode line. A value of nil means no limit. */);
15604 Vline_number_display_limit = Qnil;
15605
15606 DEFVAR_INT ("line-number-display-limit-width",
15607 &line_number_display_limit_width,
15608 doc: /* *Maximum line width (in characters) for line number display.
15609 If the average length of the lines near point is bigger than this, then the
15610 line number may be omitted from the mode line. */);
15611 line_number_display_limit_width = 200;
15612
15613 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
15614 doc: /* *Non-nil means highlight region even in nonselected windows. */);
15615 highlight_nonselected_windows = 0;
15616
15617 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
15618 doc: /* Non-nil if more than one frame is visible on this display.
15619 Minibuffer-only frames don't count, but iconified frames do.
15620 This variable is not guaranteed to be accurate except while processing
15621 `frame-title-format' and `icon-title-format'. */);
15622
15623 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
15624 doc: /* Template for displaying the title bar of visible frames.
15625 \(Assuming the window manager supports this feature.)
15626 This variable has the same structure as `mode-line-format' (which see),
15627 and is used only on frames for which no explicit name has been set
15628 \(see `modify-frame-parameters'). */);
15629 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
15630 doc: /* Template for displaying the title bar of an iconified frame.
15631 \(Assuming the window manager supports this feature.)
15632 This variable has the same structure as `mode-line-format' (which see),
15633 and is used only on frames for which no explicit name has been set
15634 \(see `modify-frame-parameters'). */);
15635 Vicon_title_format
15636 = Vframe_title_format
15637 = Fcons (intern ("multiple-frames"),
15638 Fcons (build_string ("%b"),
15639 Fcons (Fcons (empty_string,
15640 Fcons (intern ("invocation-name"),
15641 Fcons (build_string ("@"),
15642 Fcons (intern ("system-name"),
15643 Qnil)))),
15644 Qnil)));
15645
15646 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
15647 doc: /* Maximum number of lines to keep in the message log buffer.
15648 If nil, disable message logging. If t, log messages but don't truncate
15649 the buffer when it becomes large. */);
15650 Vmessage_log_max = make_number (50);
15651
15652 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
15653 doc: /* Functions called before redisplay, if window sizes have changed.
15654 The value should be a list of functions that take one argument.
15655 Just before redisplay, for each frame, if any of its windows have changed
15656 size since the last redisplay, or have been split or deleted,
15657 all the functions in the list are called, with the frame as argument. */);
15658 Vwindow_size_change_functions = Qnil;
15659
15660 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
15661 doc: /* List of Functions to call before redisplaying a window with scrolling.
15662 Each function is called with two arguments, the window
15663 and its new display-start position. Note that the value of `window-end'
15664 is not valid when these functions are called. */);
15665 Vwindow_scroll_functions = Qnil;
15666
15667 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
15668 doc: /* *Non-nil means automatically resize tool-bars.
15669 This increases a tool-bar's height if not all tool-bar items are visible.
15670 It decreases a tool-bar's height when it would display blank lines
15671 otherwise. */);
15672 auto_resize_tool_bars_p = 1;
15673
15674 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
15675 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
15676 auto_raise_tool_bar_buttons_p = 1;
15677
15678 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
15679 doc: /* *Margin around tool-bar buttons in pixels.
15680 If an integer, use that for both horizontal and vertical margins.
15681 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
15682 HORZ specifying the horizontal margin, and VERT specifying the
15683 vertical margin. */);
15684 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
15685
15686 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
15687 doc: /* *Relief thickness of tool-bar buttons. */);
15688 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
15689
15690 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
15691 doc: /* List of functions to call to fontify regions of text.
15692 Each function is called with one argument POS. Functions must
15693 fontify a region starting at POS in the current buffer, and give
15694 fontified regions the property `fontified'. */);
15695 Vfontification_functions = Qnil;
15696 Fmake_variable_buffer_local (Qfontification_functions);
15697
15698 DEFVAR_BOOL ("unibyte-display-via-language-environment",
15699 &unibyte_display_via_language_environment,
15700 doc: /* *Non-nil means display unibyte text according to language environment.
15701 Specifically this means that unibyte non-ASCII characters
15702 are displayed by converting them to the equivalent multibyte characters
15703 according to the current language environment. As a result, they are
15704 displayed according to the current fontset. */);
15705 unibyte_display_via_language_environment = 0;
15706
15707 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
15708 doc: /* *Maximum height for resizing mini-windows.
15709 If a float, it specifies a fraction of the mini-window frame's height.
15710 If an integer, it specifies a number of lines. */);
15711 Vmax_mini_window_height = make_float (0.25);
15712
15713 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
15714 doc: /* *How to resize mini-windows.
15715 A value of nil means don't automatically resize mini-windows.
15716 A value of t means resize them to fit the text displayed in them.
15717 A value of `grow-only', the default, means let mini-windows grow
15718 only, until their display becomes empty, at which point the windows
15719 go back to their normal size. */);
15720 Vresize_mini_windows = Qgrow_only;
15721
15722 DEFVAR_LISP ("cursor-in-non-selected-windows",
15723 &Vcursor_in_non_selected_windows,
15724 doc: /* *Cursor type to display in non-selected windows.
15725 t means to use hollow box cursor. See `cursor-type' for other values. */);
15726 Vcursor_in_non_selected_windows = Qt;
15727
15728 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
15729 doc: /* Alist specifying how to blink the cursor off.
15730 Each element has the form (ON-STATE . OFF-STATE). Whenever the
15731 `cursor-type' frame-parameter or variable equals ON-STATE,
15732 comparing using `equal', Emacs uses OFF-STATE to specify
15733 how to blink it off. */);
15734 Vblink_cursor_alist = Qnil;
15735
15736 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
15737 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
15738 automatic_hscrolling_p = 1;
15739
15740 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
15741 doc: /* *How many columns away from the window edge point is allowed to get
15742 before automatic hscrolling will horizontally scroll the window. */);
15743 hscroll_margin = 5;
15744
15745 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
15746 doc: /* *How many columns to scroll the window when point gets too close to the edge.
15747 When point is less than `automatic-hscroll-margin' columns from the window
15748 edge, automatic hscrolling will scroll the window by the amount of columns
15749 determined by this variable. If its value is a positive integer, scroll that
15750 many columns. If it's a positive floating-point number, it specifies the
15751 fraction of the window's width to scroll. If it's nil or zero, point will be
15752 centered horizontally after the scroll. Any other value, including negative
15753 numbers, are treated as if the value were zero.
15754
15755 Automatic hscrolling always moves point outside the scroll margin, so if
15756 point was more than scroll step columns inside the margin, the window will
15757 scroll more than the value given by the scroll step.
15758
15759 Note that the lower bound for automatic hscrolling specified by `scroll-left'
15760 and `scroll-right' overrides this variable's effect. */);
15761 Vhscroll_step = make_number (0);
15762
15763 DEFVAR_LISP ("image-types", &Vimage_types,
15764 doc: /* List of supported image types.
15765 Each element of the list is a symbol for a supported image type. */);
15766 Vimage_types = Qnil;
15767
15768 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
15769 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
15770 Bind this around calls to `message' to let it take effect. */);
15771 message_truncate_lines = 0;
15772
15773 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
15774 doc: /* Normal hook run for clicks on menu bar, before displaying a submenu.
15775 Can be used to update submenus whose contents should vary. */);
15776 Vmenu_bar_update_hook = Qnil;
15777
15778 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
15779 doc: /* Non-nil means don't update menu bars. Internal use only. */);
15780 inhibit_menubar_update = 0;
15781
15782 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
15783 doc: /* Non-nil means don't eval Lisp during redisplay. */);
15784 inhibit_eval_during_redisplay = 0;
15785
15786 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
15787 doc: /* Non-nil means don't free realized faces. Internal use only. */);
15788 inhibit_free_realized_faces = 0;
15789
15790 #if GLYPH_DEBUG
15791 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
15792 doc: /* Inhibit try_window_id display optimization. */);
15793 inhibit_try_window_id = 0;
15794
15795 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
15796 doc: /* Inhibit try_window_reusing display optimization. */);
15797 inhibit_try_window_reusing = 0;
15798
15799 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
15800 doc: /* Inhibit try_cursor_movement display optimization. */);
15801 inhibit_try_cursor_movement = 0;
15802 #endif /* GLYPH_DEBUG */
15803 }
15804
15805
15806 /* Initialize this module when Emacs starts. */
15807
15808 void
15809 init_xdisp ()
15810 {
15811 Lisp_Object root_window;
15812 struct window *mini_w;
15813
15814 current_header_line_height = current_mode_line_height = -1;
15815
15816 CHARPOS (this_line_start_pos) = 0;
15817
15818 mini_w = XWINDOW (minibuf_window);
15819 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
15820
15821 if (!noninteractive)
15822 {
15823 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
15824 int i;
15825
15826 XWINDOW (root_window)->top = make_number (FRAME_TOP_MARGIN (f));
15827 set_window_height (root_window,
15828 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
15829 0);
15830 mini_w->top = make_number (FRAME_HEIGHT (f) - 1);
15831 set_window_height (minibuf_window, 1, 0);
15832
15833 XWINDOW (root_window)->width = make_number (FRAME_WIDTH (f));
15834 mini_w->width = make_number (FRAME_WIDTH (f));
15835
15836 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
15837 scratch_glyph_row.glyphs[TEXT_AREA + 1]
15838 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
15839
15840 /* The default ellipsis glyphs `...'. */
15841 for (i = 0; i < 3; ++i)
15842 default_invis_vector[i] = make_number ('.');
15843 }
15844
15845 {
15846 /* Allocate the buffer for frame titles.
15847 Also used for `format-mode-line'. */
15848 int size = 100;
15849 frame_title_buf = (char *) xmalloc (size);
15850 frame_title_buf_end = frame_title_buf + size;
15851 frame_title_ptr = NULL;
15852 }
15853
15854 help_echo_showing_p = 0;
15855 }
15856
15857