]> code.delx.au - gnu-emacs/blob - src/xdisp.c
(try_window_id): Don't call set_cursor_from_row if
[gnu-emacs] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 86, 87, 88, 93, 94, 95, 97, 98, 99, 2000, 2001, 2002
3 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
23
24 Redisplay.
25
26 Emacs separates the task of updating the display from code
27 modifying global state, e.g. buffer text. This way functions
28 operating on buffers don't also have to be concerned with updating
29 the display.
30
31 Updating the display is triggered by the Lisp interpreter when it
32 decides it's time to do it. This is done either automatically for
33 you as part of the interpreter's command loop or as the result of
34 calling Lisp functions like `sit-for'. The C function `redisplay'
35 in xdisp.c is the only entry into the inner redisplay code. (Or,
36 let's say almost---see the description of direct update
37 operations, below.)
38
39 The following diagram shows how redisplay code is invoked. As you
40 can see, Lisp calls redisplay and vice versa. Under window systems
41 like X, some portions of the redisplay code are also called
42 asynchronously during mouse movement or expose events. It is very
43 important that these code parts do NOT use the C library (malloc,
44 free) because many C libraries under Unix are not reentrant. They
45 may also NOT call functions of the Lisp interpreter which could
46 change the interpreter's state. If you don't follow these rules,
47 you will encounter bugs which are very hard to explain.
48
49 (Direct functions, see below)
50 direct_output_for_insert,
51 direct_forward_char (dispnew.c)
52 +---------------------------------+
53 | |
54 | V
55 +--------------+ redisplay +----------------+
56 | Lisp machine |---------------->| Redisplay code |<--+
57 +--------------+ (xdisp.c) +----------------+ |
58 ^ | |
59 +----------------------------------+ |
60 Don't use this path when called |
61 asynchronously! |
62 |
63 expose_window (asynchronous) |
64 |
65 X expose events -----+
66
67 What does redisplay do? Obviously, it has to figure out somehow what
68 has been changed since the last time the display has been updated,
69 and to make these changes visible. Preferably it would do that in
70 a moderately intelligent way, i.e. fast.
71
72 Changes in buffer text can be deduced from window and buffer
73 structures, and from some global variables like `beg_unchanged' and
74 `end_unchanged'. The contents of the display are additionally
75 recorded in a `glyph matrix', a two-dimensional matrix of glyph
76 structures. Each row in such a matrix corresponds to a line on the
77 display, and each glyph in a row corresponds to a column displaying
78 a character, an image, or what else. This matrix is called the
79 `current glyph matrix' or `current matrix' in redisplay
80 terminology.
81
82 For buffer parts that have been changed since the last update, a
83 second glyph matrix is constructed, the so called `desired glyph
84 matrix' or short `desired matrix'. Current and desired matrix are
85 then compared to find a cheap way to update the display, e.g. by
86 reusing part of the display by scrolling lines.
87
88
89 Direct operations.
90
91 You will find a lot of redisplay optimizations when you start
92 looking at the innards of redisplay. The overall goal of all these
93 optimizations is to make redisplay fast because it is done
94 frequently.
95
96 Two optimizations are not found in xdisp.c. These are the direct
97 operations mentioned above. As the name suggests they follow a
98 different principle than the rest of redisplay. Instead of
99 building a desired matrix and then comparing it with the current
100 display, they perform their actions directly on the display and on
101 the current matrix.
102
103 One direct operation updates the display after one character has
104 been entered. The other one moves the cursor by one position
105 forward or backward. You find these functions under the names
106 `direct_output_for_insert' and `direct_output_forward_char' in
107 dispnew.c.
108
109
110 Desired matrices.
111
112 Desired matrices are always built per Emacs window. The function
113 `display_line' is the central function to look at if you are
114 interested. It constructs one row in a desired matrix given an
115 iterator structure containing both a buffer position and a
116 description of the environment in which the text is to be
117 displayed. But this is too early, read on.
118
119 Characters and pixmaps displayed for a range of buffer text depend
120 on various settings of buffers and windows, on overlays and text
121 properties, on display tables, on selective display. The good news
122 is that all this hairy stuff is hidden behind a small set of
123 interface functions taking an iterator structure (struct it)
124 argument.
125
126 Iteration over things to be displayed is then simple. It is
127 started by initializing an iterator with a call to init_iterator.
128 Calls to get_next_display_element fill the iterator structure with
129 relevant information about the next thing to display. Calls to
130 set_iterator_to_next move the iterator to the next thing.
131
132 Besides this, an iterator also contains information about the
133 display environment in which glyphs for display elements are to be
134 produced. It has fields for the width and height of the display,
135 the information whether long lines are truncated or continued, a
136 current X and Y position, and lots of other stuff you can better
137 see in dispextern.h.
138
139 Glyphs in a desired matrix are normally constructed in a loop
140 calling get_next_display_element and then produce_glyphs. The call
141 to produce_glyphs will fill the iterator structure with pixel
142 information about the element being displayed and at the same time
143 produce glyphs for it. If the display element fits on the line
144 being displayed, set_iterator_to_next is called next, otherwise the
145 glyphs produced are discarded.
146
147
148 Frame matrices.
149
150 That just couldn't be all, could it? What about terminal types not
151 supporting operations on sub-windows of the screen? To update the
152 display on such a terminal, window-based glyph matrices are not
153 well suited. To be able to reuse part of the display (scrolling
154 lines up and down), we must instead have a view of the whole
155 screen. This is what `frame matrices' are for. They are a trick.
156
157 Frames on terminals like above have a glyph pool. Windows on such
158 a frame sub-allocate their glyph memory from their frame's glyph
159 pool. The frame itself is given its own glyph matrices. By
160 coincidence---or maybe something else---rows in window glyph
161 matrices are slices of corresponding rows in frame matrices. Thus
162 writing to window matrices implicitly updates a frame matrix which
163 provides us with the view of the whole screen that we originally
164 wanted to have without having to move many bytes around. To be
165 honest, there is a little bit more done, but not much more. If you
166 plan to extend that code, take a look at dispnew.c. The function
167 build_frame_matrix is a good starting point. */
168
169 #include <config.h>
170 #include <stdio.h>
171
172 #include "lisp.h"
173 #include "keyboard.h"
174 #include "frame.h"
175 #include "window.h"
176 #include "termchar.h"
177 #include "dispextern.h"
178 #include "buffer.h"
179 #include "charset.h"
180 #include "indent.h"
181 #include "commands.h"
182 #include "macros.h"
183 #include "disptab.h"
184 #include "termhooks.h"
185 #include "intervals.h"
186 #include "coding.h"
187 #include "process.h"
188 #include "region-cache.h"
189 #include "fontset.h"
190
191 #ifdef HAVE_X_WINDOWS
192 #include "xterm.h"
193 #endif
194 #ifdef WINDOWSNT
195 #include "w32term.h"
196 #endif
197 #ifdef MAC_OS
198 #include "macterm.h"
199 #endif
200
201 #define INFINITY 10000000
202
203 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS)
204 extern void set_frame_menubar P_ ((struct frame *f, int, int));
205 extern int pending_menu_activation;
206 #endif
207
208 extern int interrupt_input;
209 extern int command_loop_level;
210
211 extern int minibuffer_auto_raise;
212
213 extern Lisp_Object Qface;
214 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
215
216 extern Lisp_Object Voverriding_local_map;
217 extern Lisp_Object Voverriding_local_map_menu_flag;
218 extern Lisp_Object Qmenu_item;
219
220 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
221 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
222 Lisp_Object Qredisplay_end_trigger_functions;
223 Lisp_Object Qinhibit_point_motion_hooks;
224 Lisp_Object QCeval, Qwhen, QCfile, QCdata, QCpropertize;
225 Lisp_Object Qfontified;
226 Lisp_Object Qgrow_only;
227 Lisp_Object Qinhibit_eval_during_redisplay;
228 Lisp_Object Qbuffer_position, Qposition, Qobject;
229
230 /* Cursor shapes */
231 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
232
233 Lisp_Object Qrisky_local_variable;
234
235 /* Holds the list (error). */
236 Lisp_Object list_of_error;
237
238 /* Functions called to fontify regions of text. */
239
240 Lisp_Object Vfontification_functions;
241 Lisp_Object Qfontification_functions;
242
243 /* Non-zero means draw tool bar buttons raised when the mouse moves
244 over them. */
245
246 int auto_raise_tool_bar_buttons_p;
247
248 /* Margin around tool bar buttons in pixels. */
249
250 Lisp_Object Vtool_bar_button_margin;
251
252 /* Thickness of shadow to draw around tool bar buttons. */
253
254 EMACS_INT tool_bar_button_relief;
255
256 /* Non-zero means automatically resize tool-bars so that all tool-bar
257 items are visible, and no blank lines remain. */
258
259 int auto_resize_tool_bars_p;
260
261 /* Non-nil means don't actually do any redisplay. */
262
263 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
264
265 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
266
267 int inhibit_eval_during_redisplay;
268
269 /* Names of text properties relevant for redisplay. */
270
271 Lisp_Object Qdisplay, Qrelative_width, Qalign_to;
272 extern Lisp_Object Qface, Qinvisible, Qwidth;
273
274 /* Symbols used in text property values. */
275
276 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
277 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
278 Lisp_Object Qmargin;
279 extern Lisp_Object Qheight;
280
281 /* Non-nil means highlight trailing whitespace. */
282
283 Lisp_Object Vshow_trailing_whitespace;
284
285 /* Name of the face used to highlight trailing whitespace. */
286
287 Lisp_Object Qtrailing_whitespace;
288
289 /* The symbol `image' which is the car of the lists used to represent
290 images in Lisp. */
291
292 Lisp_Object Qimage;
293
294 /* Non-zero means print newline to stdout before next mini-buffer
295 message. */
296
297 int noninteractive_need_newline;
298
299 /* Non-zero means print newline to message log before next message. */
300
301 static int message_log_need_newline;
302
303 /* Three markers that message_dolog uses.
304 It could allocate them itself, but that causes trouble
305 in handling memory-full errors. */
306 static Lisp_Object message_dolog_marker1;
307 static Lisp_Object message_dolog_marker2;
308 static Lisp_Object message_dolog_marker3;
309 \f
310 /* The buffer position of the first character appearing entirely or
311 partially on the line of the selected window which contains the
312 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
313 redisplay optimization in redisplay_internal. */
314
315 static struct text_pos this_line_start_pos;
316
317 /* Number of characters past the end of the line above, including the
318 terminating newline. */
319
320 static struct text_pos this_line_end_pos;
321
322 /* The vertical positions and the height of this line. */
323
324 static int this_line_vpos;
325 static int this_line_y;
326 static int this_line_pixel_height;
327
328 /* X position at which this display line starts. Usually zero;
329 negative if first character is partially visible. */
330
331 static int this_line_start_x;
332
333 /* Buffer that this_line_.* variables are referring to. */
334
335 static struct buffer *this_line_buffer;
336
337 /* Nonzero means truncate lines in all windows less wide than the
338 frame. */
339
340 int truncate_partial_width_windows;
341
342 /* A flag to control how to display unibyte 8-bit character. */
343
344 int unibyte_display_via_language_environment;
345
346 /* Nonzero means we have more than one non-mini-buffer-only frame.
347 Not guaranteed to be accurate except while parsing
348 frame-title-format. */
349
350 int multiple_frames;
351
352 Lisp_Object Vglobal_mode_string;
353
354 /* Marker for where to display an arrow on top of the buffer text. */
355
356 Lisp_Object Voverlay_arrow_position;
357
358 /* String to display for the arrow. Only used on terminal frames. */
359
360 Lisp_Object Voverlay_arrow_string;
361
362 /* Values of those variables at last redisplay. However, if
363 Voverlay_arrow_position is a marker, last_arrow_position is its
364 numerical position. */
365
366 static Lisp_Object last_arrow_position, last_arrow_string;
367
368 /* Like mode-line-format, but for the title bar on a visible frame. */
369
370 Lisp_Object Vframe_title_format;
371
372 /* Like mode-line-format, but for the title bar on an iconified frame. */
373
374 Lisp_Object Vicon_title_format;
375
376 /* List of functions to call when a window's size changes. These
377 functions get one arg, a frame on which one or more windows' sizes
378 have changed. */
379
380 static Lisp_Object Vwindow_size_change_functions;
381
382 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
383
384 /* Nonzero if overlay arrow has been displayed once in this window. */
385
386 static int overlay_arrow_seen;
387
388 /* Nonzero means highlight the region even in nonselected windows. */
389
390 int highlight_nonselected_windows;
391
392 /* If cursor motion alone moves point off frame, try scrolling this
393 many lines up or down if that will bring it back. */
394
395 static EMACS_INT scroll_step;
396
397 /* Nonzero means scroll just far enough to bring point back on the
398 screen, when appropriate. */
399
400 static EMACS_INT scroll_conservatively;
401
402 /* Recenter the window whenever point gets within this many lines of
403 the top or bottom of the window. This value is translated into a
404 pixel value by multiplying it with CANON_Y_UNIT, which means that
405 there is really a fixed pixel height scroll margin. */
406
407 EMACS_INT scroll_margin;
408
409 /* Number of windows showing the buffer of the selected window (or
410 another buffer with the same base buffer). keyboard.c refers to
411 this. */
412
413 int buffer_shared;
414
415 /* Vector containing glyphs for an ellipsis `...'. */
416
417 static Lisp_Object default_invis_vector[3];
418
419 /* Zero means display the mode-line/header-line/menu-bar in the default face
420 (this slightly odd definition is for compatibility with previous versions
421 of emacs), non-zero means display them using their respective faces.
422
423 This variable is deprecated. */
424
425 int mode_line_inverse_video;
426
427 /* Prompt to display in front of the mini-buffer contents. */
428
429 Lisp_Object minibuf_prompt;
430
431 /* Width of current mini-buffer prompt. Only set after display_line
432 of the line that contains the prompt. */
433
434 int minibuf_prompt_width;
435
436 /* This is the window where the echo area message was displayed. It
437 is always a mini-buffer window, but it may not be the same window
438 currently active as a mini-buffer. */
439
440 Lisp_Object echo_area_window;
441
442 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
443 pushes the current message and the value of
444 message_enable_multibyte on the stack, the function restore_message
445 pops the stack and displays MESSAGE again. */
446
447 Lisp_Object Vmessage_stack;
448
449 /* Nonzero means multibyte characters were enabled when the echo area
450 message was specified. */
451
452 int message_enable_multibyte;
453
454 /* Nonzero if we should redraw the mode lines on the next redisplay. */
455
456 int update_mode_lines;
457
458 /* Nonzero if window sizes or contents have changed since last
459 redisplay that finished. */
460
461 int windows_or_buffers_changed;
462
463 /* Nonzero means a frame's cursor type has been changed. */
464
465 int cursor_type_changed;
466
467 /* Nonzero after display_mode_line if %l was used and it displayed a
468 line number. */
469
470 int line_number_displayed;
471
472 /* Maximum buffer size for which to display line numbers. */
473
474 Lisp_Object Vline_number_display_limit;
475
476 /* Line width to consider when repositioning for line number display. */
477
478 static EMACS_INT line_number_display_limit_width;
479
480 /* Number of lines to keep in the message log buffer. t means
481 infinite. nil means don't log at all. */
482
483 Lisp_Object Vmessage_log_max;
484
485 /* The name of the *Messages* buffer, a string. */
486
487 static Lisp_Object Vmessages_buffer_name;
488
489 /* Current, index 0, and last displayed echo area message. Either
490 buffers from echo_buffers, or nil to indicate no message. */
491
492 Lisp_Object echo_area_buffer[2];
493
494 /* The buffers referenced from echo_area_buffer. */
495
496 static Lisp_Object echo_buffer[2];
497
498 /* A vector saved used in with_area_buffer to reduce consing. */
499
500 static Lisp_Object Vwith_echo_area_save_vector;
501
502 /* Non-zero means display_echo_area should display the last echo area
503 message again. Set by redisplay_preserve_echo_area. */
504
505 static int display_last_displayed_message_p;
506
507 /* Nonzero if echo area is being used by print; zero if being used by
508 message. */
509
510 int message_buf_print;
511
512 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
513
514 Lisp_Object Qinhibit_menubar_update;
515 int inhibit_menubar_update;
516
517 /* Maximum height for resizing mini-windows. Either a float
518 specifying a fraction of the available height, or an integer
519 specifying a number of lines. */
520
521 Lisp_Object Vmax_mini_window_height;
522
523 /* Non-zero means messages should be displayed with truncated
524 lines instead of being continued. */
525
526 int message_truncate_lines;
527 Lisp_Object Qmessage_truncate_lines;
528
529 /* Set to 1 in clear_message to make redisplay_internal aware
530 of an emptied echo area. */
531
532 static int message_cleared_p;
533
534 /* Non-zero means we want a hollow cursor in windows that are not
535 selected. Zero means there's no cursor in such windows. */
536
537 Lisp_Object Vcursor_in_non_selected_windows;
538 Lisp_Object Qcursor_in_non_selected_windows;
539
540 /* How to blink the default frame cursor off. */
541 Lisp_Object Vblink_cursor_alist;
542
543 /* A scratch glyph row with contents used for generating truncation
544 glyphs. Also used in direct_output_for_insert. */
545
546 #define MAX_SCRATCH_GLYPHS 100
547 struct glyph_row scratch_glyph_row;
548 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
549
550 /* Ascent and height of the last line processed by move_it_to. */
551
552 static int last_max_ascent, last_height;
553
554 /* Non-zero if there's a help-echo in the echo area. */
555
556 int help_echo_showing_p;
557
558 /* If >= 0, computed, exact values of mode-line and header-line height
559 to use in the macros CURRENT_MODE_LINE_HEIGHT and
560 CURRENT_HEADER_LINE_HEIGHT. */
561
562 int current_mode_line_height, current_header_line_height;
563
564 /* The maximum distance to look ahead for text properties. Values
565 that are too small let us call compute_char_face and similar
566 functions too often which is expensive. Values that are too large
567 let us call compute_char_face and alike too often because we
568 might not be interested in text properties that far away. */
569
570 #define TEXT_PROP_DISTANCE_LIMIT 100
571
572 #if GLYPH_DEBUG
573
574 /* Variables to turn off display optimizations from Lisp. */
575
576 int inhibit_try_window_id, inhibit_try_window_reusing;
577 int inhibit_try_cursor_movement;
578
579 /* Non-zero means print traces of redisplay if compiled with
580 GLYPH_DEBUG != 0. */
581
582 int trace_redisplay_p;
583
584 #endif /* GLYPH_DEBUG */
585
586 #ifdef DEBUG_TRACE_MOVE
587 /* Non-zero means trace with TRACE_MOVE to stderr. */
588 int trace_move;
589
590 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
591 #else
592 #define TRACE_MOVE(x) (void) 0
593 #endif
594
595 /* Non-zero means automatically scroll windows horizontally to make
596 point visible. */
597
598 int automatic_hscrolling_p;
599
600 /* How close to the margin can point get before the window is scrolled
601 horizontally. */
602 EMACS_INT hscroll_margin;
603
604 /* How much to scroll horizontally when point is inside the above margin. */
605 Lisp_Object Vhscroll_step;
606
607 /* A list of symbols, one for each supported image type. */
608
609 Lisp_Object Vimage_types;
610
611 /* The variable `resize-mini-windows'. If nil, don't resize
612 mini-windows. If t, always resize them to fit the text they
613 display. If `grow-only', let mini-windows grow only until they
614 become empty. */
615
616 Lisp_Object Vresize_mini_windows;
617
618 /* Buffer being redisplayed -- for redisplay_window_error. */
619
620 struct buffer *displayed_buffer;
621
622 /* Value returned from text property handlers (see below). */
623
624 enum prop_handled
625 {
626 HANDLED_NORMALLY,
627 HANDLED_RECOMPUTE_PROPS,
628 HANDLED_OVERLAY_STRING_CONSUMED,
629 HANDLED_RETURN
630 };
631
632 /* A description of text properties that redisplay is interested
633 in. */
634
635 struct props
636 {
637 /* The name of the property. */
638 Lisp_Object *name;
639
640 /* A unique index for the property. */
641 enum prop_idx idx;
642
643 /* A handler function called to set up iterator IT from the property
644 at IT's current position. Value is used to steer handle_stop. */
645 enum prop_handled (*handler) P_ ((struct it *it));
646 };
647
648 static enum prop_handled handle_face_prop P_ ((struct it *));
649 static enum prop_handled handle_invisible_prop P_ ((struct it *));
650 static enum prop_handled handle_display_prop P_ ((struct it *));
651 static enum prop_handled handle_composition_prop P_ ((struct it *));
652 static enum prop_handled handle_overlay_change P_ ((struct it *));
653 static enum prop_handled handle_fontified_prop P_ ((struct it *));
654
655 /* Properties handled by iterators. */
656
657 static struct props it_props[] =
658 {
659 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
660 /* Handle `face' before `display' because some sub-properties of
661 `display' need to know the face. */
662 {&Qface, FACE_PROP_IDX, handle_face_prop},
663 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
664 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
665 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
666 {NULL, 0, NULL}
667 };
668
669 /* Value is the position described by X. If X is a marker, value is
670 the marker_position of X. Otherwise, value is X. */
671
672 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
673
674 /* Enumeration returned by some move_it_.* functions internally. */
675
676 enum move_it_result
677 {
678 /* Not used. Undefined value. */
679 MOVE_UNDEFINED,
680
681 /* Move ended at the requested buffer position or ZV. */
682 MOVE_POS_MATCH_OR_ZV,
683
684 /* Move ended at the requested X pixel position. */
685 MOVE_X_REACHED,
686
687 /* Move within a line ended at the end of a line that must be
688 continued. */
689 MOVE_LINE_CONTINUED,
690
691 /* Move within a line ended at the end of a line that would
692 be displayed truncated. */
693 MOVE_LINE_TRUNCATED,
694
695 /* Move within a line ended at a line end. */
696 MOVE_NEWLINE_OR_CR
697 };
698
699 /* This counter is used to clear the face cache every once in a while
700 in redisplay_internal. It is incremented for each redisplay.
701 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
702 cleared. */
703
704 #define CLEAR_FACE_CACHE_COUNT 500
705 static int clear_face_cache_count;
706
707 /* Record the previous terminal frame we displayed. */
708
709 static struct frame *previous_terminal_frame;
710
711 /* Non-zero while redisplay_internal is in progress. */
712
713 int redisplaying_p;
714
715 /* Non-zero means don't free realized faces. Bound while freeing
716 realized faces is dangerous because glyph matrices might still
717 reference them. */
718
719 int inhibit_free_realized_faces;
720 Lisp_Object Qinhibit_free_realized_faces;
721
722 \f
723 /* Function prototypes. */
724
725 static void setup_for_ellipsis P_ ((struct it *));
726 static void mark_window_display_accurate_1 P_ ((struct window *, int));
727 static int single_display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
728 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
729 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
730 static int redisplay_mode_lines P_ ((Lisp_Object, int));
731 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
732
733 #if 0
734 static int invisible_text_between_p P_ ((struct it *, int, int));
735 #endif
736
737 static int next_element_from_ellipsis P_ ((struct it *));
738 static void pint2str P_ ((char *, int, int));
739 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
740 struct text_pos));
741 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
742 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
743 static void store_frame_title_char P_ ((char));
744 static int store_frame_title P_ ((const unsigned char *, int, int));
745 static void x_consider_frame_title P_ ((Lisp_Object));
746 static void handle_stop P_ ((struct it *));
747 static int tool_bar_lines_needed P_ ((struct frame *));
748 static int single_display_prop_intangible_p P_ ((Lisp_Object));
749 static void ensure_echo_area_buffers P_ ((void));
750 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
751 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
752 static int with_echo_area_buffer P_ ((struct window *, int,
753 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
754 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
755 static void clear_garbaged_frames P_ ((void));
756 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
757 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
758 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
759 static int display_echo_area P_ ((struct window *));
760 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
761 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
762 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
763 static int string_char_and_length P_ ((const unsigned char *, int, int *));
764 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
765 struct text_pos));
766 static int compute_window_start_on_continuation_line P_ ((struct window *));
767 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
768 static void insert_left_trunc_glyphs P_ ((struct it *));
769 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
770 static void extend_face_to_end_of_line P_ ((struct it *));
771 static int append_space P_ ((struct it *, int));
772 static int make_cursor_line_fully_visible P_ ((struct window *));
773 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int));
774 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
775 static int trailing_whitespace_p P_ ((int));
776 static int message_log_check_duplicate P_ ((int, int, int, int));
777 static void push_it P_ ((struct it *));
778 static void pop_it P_ ((struct it *));
779 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
780 static void redisplay_internal P_ ((int));
781 static int echo_area_display P_ ((int));
782 static void redisplay_windows P_ ((Lisp_Object));
783 static void redisplay_window P_ ((Lisp_Object, int));
784 static Lisp_Object redisplay_window_error ();
785 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
786 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
787 static void update_menu_bar P_ ((struct frame *, int));
788 static int try_window_reusing_current_matrix P_ ((struct window *));
789 static int try_window_id P_ ((struct window *));
790 static int display_line P_ ((struct it *));
791 static int display_mode_lines P_ ((struct window *));
792 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
793 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
794 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
795 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
796 static void display_menu_bar P_ ((struct window *));
797 static int display_count_lines P_ ((int, int, int, int, int *));
798 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
799 int, int, struct it *, int, int, int, int));
800 static void compute_line_metrics P_ ((struct it *));
801 static void run_redisplay_end_trigger_hook P_ ((struct it *));
802 static int get_overlay_strings P_ ((struct it *, int));
803 static void next_overlay_string P_ ((struct it *));
804 static void reseat P_ ((struct it *, struct text_pos, int));
805 static void reseat_1 P_ ((struct it *, struct text_pos, int));
806 static void back_to_previous_visible_line_start P_ ((struct it *));
807 static void reseat_at_previous_visible_line_start P_ ((struct it *));
808 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
809 static int next_element_from_display_vector P_ ((struct it *));
810 static int next_element_from_string P_ ((struct it *));
811 static int next_element_from_c_string P_ ((struct it *));
812 static int next_element_from_buffer P_ ((struct it *));
813 static int next_element_from_composition P_ ((struct it *));
814 static int next_element_from_image P_ ((struct it *));
815 static int next_element_from_stretch P_ ((struct it *));
816 static void load_overlay_strings P_ ((struct it *, int));
817 static int init_from_display_pos P_ ((struct it *, struct window *,
818 struct display_pos *));
819 static void reseat_to_string P_ ((struct it *, unsigned char *,
820 Lisp_Object, int, int, int, int));
821 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
822 int, int, int));
823 void move_it_vertically_backward P_ ((struct it *, int));
824 static void init_to_row_start P_ ((struct it *, struct window *,
825 struct glyph_row *));
826 static int init_to_row_end P_ ((struct it *, struct window *,
827 struct glyph_row *));
828 static void back_to_previous_line_start P_ ((struct it *));
829 static int forward_to_next_line_start P_ ((struct it *, int *));
830 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
831 Lisp_Object, int));
832 static struct text_pos string_pos P_ ((int, Lisp_Object));
833 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
834 static int number_of_chars P_ ((unsigned char *, int));
835 static void compute_stop_pos P_ ((struct it *));
836 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
837 Lisp_Object));
838 static int face_before_or_after_it_pos P_ ((struct it *, int));
839 static int next_overlay_change P_ ((int));
840 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
841 Lisp_Object, struct text_pos *,
842 int));
843 static int underlying_face_id P_ ((struct it *));
844 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
845 struct window *));
846
847 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
848 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
849
850 #ifdef HAVE_WINDOW_SYSTEM
851
852 static void update_tool_bar P_ ((struct frame *, int));
853 static void build_desired_tool_bar_string P_ ((struct frame *f));
854 static int redisplay_tool_bar P_ ((struct frame *));
855 static void display_tool_bar_line P_ ((struct it *));
856
857 #endif /* HAVE_WINDOW_SYSTEM */
858
859 \f
860 /***********************************************************************
861 Window display dimensions
862 ***********************************************************************/
863
864 /* Return the window-relative maximum y + 1 for glyph rows displaying
865 text in window W. This is the height of W minus the height of a
866 mode line, if any. */
867
868 INLINE int
869 window_text_bottom_y (w)
870 struct window *w;
871 {
872 struct frame *f = XFRAME (w->frame);
873 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
874
875 if (WINDOW_WANTS_MODELINE_P (w))
876 height -= CURRENT_MODE_LINE_HEIGHT (w);
877 return height;
878 }
879
880
881 /* Return the pixel width of display area AREA of window W. AREA < 0
882 means return the total width of W, not including fringes to
883 the left and right of the window. */
884
885 INLINE int
886 window_box_width (w, area)
887 struct window *w;
888 int area;
889 {
890 struct frame *f = XFRAME (w->frame);
891 int width = XFASTINT (w->width);
892
893 if (!w->pseudo_window_p)
894 {
895 width -= FRAME_SCROLL_BAR_WIDTH (f) + FRAME_FRINGE_COLS (f);
896
897 if (area == TEXT_AREA)
898 {
899 if (INTEGERP (w->left_margin_width))
900 width -= XFASTINT (w->left_margin_width);
901 if (INTEGERP (w->right_margin_width))
902 width -= XFASTINT (w->right_margin_width);
903 }
904 else if (area == LEFT_MARGIN_AREA)
905 width = (INTEGERP (w->left_margin_width)
906 ? XFASTINT (w->left_margin_width) : 0);
907 else if (area == RIGHT_MARGIN_AREA)
908 width = (INTEGERP (w->right_margin_width)
909 ? XFASTINT (w->right_margin_width) : 0);
910 }
911
912 return width * CANON_X_UNIT (f);
913 }
914
915
916 /* Return the pixel height of the display area of window W, not
917 including mode lines of W, if any. */
918
919 INLINE int
920 window_box_height (w)
921 struct window *w;
922 {
923 struct frame *f = XFRAME (w->frame);
924 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
925
926 xassert (height >= 0);
927
928 /* Note: the code below that determines the mode-line/header-line
929 height is essentially the same as that contained in the macro
930 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
931 the appropriate glyph row has its `mode_line_p' flag set,
932 and if it doesn't, uses estimate_mode_line_height instead. */
933
934 if (WINDOW_WANTS_MODELINE_P (w))
935 {
936 struct glyph_row *ml_row
937 = (w->current_matrix && w->current_matrix->rows
938 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
939 : 0);
940 if (ml_row && ml_row->mode_line_p)
941 height -= ml_row->height;
942 else
943 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
944 }
945
946 if (WINDOW_WANTS_HEADER_LINE_P (w))
947 {
948 struct glyph_row *hl_row
949 = (w->current_matrix && w->current_matrix->rows
950 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
951 : 0);
952 if (hl_row && hl_row->mode_line_p)
953 height -= hl_row->height;
954 else
955 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
956 }
957
958 /* With a very small font and a mode-line that's taller than
959 default, we might end up with a negative height. */
960 return max (0, height);
961 }
962
963
964 /* Return the frame-relative coordinate of the left edge of display
965 area AREA of window W. AREA < 0 means return the left edge of the
966 whole window, to the right of the left fringe of W. */
967
968 INLINE int
969 window_box_left (w, area)
970 struct window *w;
971 int area;
972 {
973 struct frame *f = XFRAME (w->frame);
974 int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
975
976 if (!w->pseudo_window_p)
977 {
978 x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
979 + FRAME_LEFT_FRINGE_WIDTH (f));
980
981 if (area == TEXT_AREA)
982 x += window_box_width (w, LEFT_MARGIN_AREA);
983 else if (area == RIGHT_MARGIN_AREA)
984 x += (window_box_width (w, LEFT_MARGIN_AREA)
985 + window_box_width (w, TEXT_AREA));
986 }
987
988 return x;
989 }
990
991
992 /* Return the frame-relative coordinate of the right edge of display
993 area AREA of window W. AREA < 0 means return the left edge of the
994 whole window, to the left of the right fringe of W. */
995
996 INLINE int
997 window_box_right (w, area)
998 struct window *w;
999 int area;
1000 {
1001 return window_box_left (w, area) + window_box_width (w, area);
1002 }
1003
1004
1005 /* Get the bounding box of the display area AREA of window W, without
1006 mode lines, in frame-relative coordinates. AREA < 0 means the
1007 whole window, not including the left and right fringes of
1008 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1009 coordinates of the upper-left corner of the box. Return in
1010 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1011
1012 INLINE void
1013 window_box (w, area, box_x, box_y, box_width, box_height)
1014 struct window *w;
1015 int area;
1016 int *box_x, *box_y, *box_width, *box_height;
1017 {
1018 struct frame *f = XFRAME (w->frame);
1019
1020 *box_width = window_box_width (w, area);
1021 *box_height = window_box_height (w);
1022 *box_x = window_box_left (w, area);
1023 *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
1024 + XFASTINT (w->top) * CANON_Y_UNIT (f));
1025 if (WINDOW_WANTS_HEADER_LINE_P (w))
1026 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1027 }
1028
1029
1030 /* Get the bounding box of the display area AREA of window W, without
1031 mode lines. AREA < 0 means the whole window, not including the
1032 left and right fringe of the window. Return in *TOP_LEFT_X
1033 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1034 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1035 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1036 box. */
1037
1038 INLINE void
1039 window_box_edges (w, area, top_left_x, top_left_y,
1040 bottom_right_x, bottom_right_y)
1041 struct window *w;
1042 int area;
1043 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1044 {
1045 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1046 bottom_right_y);
1047 *bottom_right_x += *top_left_x;
1048 *bottom_right_y += *top_left_y;
1049 }
1050
1051
1052 \f
1053 /***********************************************************************
1054 Utilities
1055 ***********************************************************************/
1056
1057 /* Return the bottom y-position of the line the iterator IT is in.
1058 This can modify IT's settings. */
1059
1060 int
1061 line_bottom_y (it)
1062 struct it *it;
1063 {
1064 int line_height = it->max_ascent + it->max_descent;
1065 int line_top_y = it->current_y;
1066
1067 if (line_height == 0)
1068 {
1069 if (last_height)
1070 line_height = last_height;
1071 else if (IT_CHARPOS (*it) < ZV)
1072 {
1073 move_it_by_lines (it, 1, 1);
1074 line_height = (it->max_ascent || it->max_descent
1075 ? it->max_ascent + it->max_descent
1076 : last_height);
1077 }
1078 else
1079 {
1080 struct glyph_row *row = it->glyph_row;
1081
1082 /* Use the default character height. */
1083 it->glyph_row = NULL;
1084 it->what = IT_CHARACTER;
1085 it->c = ' ';
1086 it->len = 1;
1087 PRODUCE_GLYPHS (it);
1088 line_height = it->ascent + it->descent;
1089 it->glyph_row = row;
1090 }
1091 }
1092
1093 return line_top_y + line_height;
1094 }
1095
1096
1097 /* Return 1 if position CHARPOS is visible in window W. Set *FULLY to
1098 1 if POS is visible and the line containing POS is fully visible.
1099 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1100 and header-lines heights. */
1101
1102 int
1103 pos_visible_p (w, charpos, fully, exact_mode_line_heights_p)
1104 struct window *w;
1105 int charpos, *fully, exact_mode_line_heights_p;
1106 {
1107 struct it it;
1108 struct text_pos top;
1109 int visible_p;
1110 struct buffer *old_buffer = NULL;
1111
1112 if (XBUFFER (w->buffer) != current_buffer)
1113 {
1114 old_buffer = current_buffer;
1115 set_buffer_internal_1 (XBUFFER (w->buffer));
1116 }
1117
1118 *fully = visible_p = 0;
1119 SET_TEXT_POS_FROM_MARKER (top, w->start);
1120
1121 /* Compute exact mode line heights, if requested. */
1122 if (exact_mode_line_heights_p)
1123 {
1124 if (WINDOW_WANTS_MODELINE_P (w))
1125 current_mode_line_height
1126 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1127 current_buffer->mode_line_format);
1128
1129 if (WINDOW_WANTS_HEADER_LINE_P (w))
1130 current_header_line_height
1131 = display_mode_line (w, HEADER_LINE_FACE_ID,
1132 current_buffer->header_line_format);
1133 }
1134
1135 start_display (&it, w, top);
1136 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
1137 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
1138
1139 /* Note that we may overshoot because of invisible text. */
1140 if (IT_CHARPOS (it) >= charpos)
1141 {
1142 int top_y = it.current_y;
1143 int bottom_y = line_bottom_y (&it);
1144 int window_top_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
1145
1146 if (top_y < window_top_y)
1147 visible_p = bottom_y > window_top_y;
1148 else if (top_y < it.last_visible_y)
1149 {
1150 visible_p = 1;
1151 *fully = bottom_y <= it.last_visible_y;
1152 }
1153 }
1154 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1155 {
1156 move_it_by_lines (&it, 1, 0);
1157 if (charpos < IT_CHARPOS (it))
1158 {
1159 visible_p = 1;
1160 *fully = 0;
1161 }
1162 }
1163
1164 if (old_buffer)
1165 set_buffer_internal_1 (old_buffer);
1166
1167 current_header_line_height = current_mode_line_height = -1;
1168 return visible_p;
1169 }
1170
1171
1172 /* Return the next character from STR which is MAXLEN bytes long.
1173 Return in *LEN the length of the character. This is like
1174 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1175 we find one, we return a `?', but with the length of the invalid
1176 character. */
1177
1178 static INLINE int
1179 string_char_and_length (str, maxlen, len)
1180 const unsigned char *str;
1181 int maxlen, *len;
1182 {
1183 int c;
1184
1185 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1186 if (!CHAR_VALID_P (c, 1))
1187 /* We may not change the length here because other places in Emacs
1188 don't use this function, i.e. they silently accept invalid
1189 characters. */
1190 c = '?';
1191
1192 return c;
1193 }
1194
1195
1196
1197 /* Given a position POS containing a valid character and byte position
1198 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1199
1200 static struct text_pos
1201 string_pos_nchars_ahead (pos, string, nchars)
1202 struct text_pos pos;
1203 Lisp_Object string;
1204 int nchars;
1205 {
1206 xassert (STRINGP (string) && nchars >= 0);
1207
1208 if (STRING_MULTIBYTE (string))
1209 {
1210 int rest = SBYTES (string) - BYTEPOS (pos);
1211 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1212 int len;
1213
1214 while (nchars--)
1215 {
1216 string_char_and_length (p, rest, &len);
1217 p += len, rest -= len;
1218 xassert (rest >= 0);
1219 CHARPOS (pos) += 1;
1220 BYTEPOS (pos) += len;
1221 }
1222 }
1223 else
1224 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1225
1226 return pos;
1227 }
1228
1229
1230 /* Value is the text position, i.e. character and byte position,
1231 for character position CHARPOS in STRING. */
1232
1233 static INLINE struct text_pos
1234 string_pos (charpos, string)
1235 int charpos;
1236 Lisp_Object string;
1237 {
1238 struct text_pos pos;
1239 xassert (STRINGP (string));
1240 xassert (charpos >= 0);
1241 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1242 return pos;
1243 }
1244
1245
1246 /* Value is a text position, i.e. character and byte position, for
1247 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1248 means recognize multibyte characters. */
1249
1250 static struct text_pos
1251 c_string_pos (charpos, s, multibyte_p)
1252 int charpos;
1253 unsigned char *s;
1254 int multibyte_p;
1255 {
1256 struct text_pos pos;
1257
1258 xassert (s != NULL);
1259 xassert (charpos >= 0);
1260
1261 if (multibyte_p)
1262 {
1263 int rest = strlen (s), len;
1264
1265 SET_TEXT_POS (pos, 0, 0);
1266 while (charpos--)
1267 {
1268 string_char_and_length (s, rest, &len);
1269 s += len, rest -= len;
1270 xassert (rest >= 0);
1271 CHARPOS (pos) += 1;
1272 BYTEPOS (pos) += len;
1273 }
1274 }
1275 else
1276 SET_TEXT_POS (pos, charpos, charpos);
1277
1278 return pos;
1279 }
1280
1281
1282 /* Value is the number of characters in C string S. MULTIBYTE_P
1283 non-zero means recognize multibyte characters. */
1284
1285 static int
1286 number_of_chars (s, multibyte_p)
1287 unsigned char *s;
1288 int multibyte_p;
1289 {
1290 int nchars;
1291
1292 if (multibyte_p)
1293 {
1294 int rest = strlen (s), len;
1295 unsigned char *p = (unsigned char *) s;
1296
1297 for (nchars = 0; rest > 0; ++nchars)
1298 {
1299 string_char_and_length (p, rest, &len);
1300 rest -= len, p += len;
1301 }
1302 }
1303 else
1304 nchars = strlen (s);
1305
1306 return nchars;
1307 }
1308
1309
1310 /* Compute byte position NEWPOS->bytepos corresponding to
1311 NEWPOS->charpos. POS is a known position in string STRING.
1312 NEWPOS->charpos must be >= POS.charpos. */
1313
1314 static void
1315 compute_string_pos (newpos, pos, string)
1316 struct text_pos *newpos, pos;
1317 Lisp_Object string;
1318 {
1319 xassert (STRINGP (string));
1320 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1321
1322 if (STRING_MULTIBYTE (string))
1323 *newpos = string_pos_nchars_ahead (pos, string,
1324 CHARPOS (*newpos) - CHARPOS (pos));
1325 else
1326 BYTEPOS (*newpos) = CHARPOS (*newpos);
1327 }
1328
1329
1330 \f
1331 /***********************************************************************
1332 Lisp form evaluation
1333 ***********************************************************************/
1334
1335 /* Error handler for safe_eval and safe_call. */
1336
1337 static Lisp_Object
1338 safe_eval_handler (arg)
1339 Lisp_Object arg;
1340 {
1341 add_to_log ("Error during redisplay: %s", arg, Qnil);
1342 return Qnil;
1343 }
1344
1345
1346 /* Evaluate SEXPR and return the result, or nil if something went
1347 wrong. Prevent redisplay during the evaluation. */
1348
1349 Lisp_Object
1350 safe_eval (sexpr)
1351 Lisp_Object sexpr;
1352 {
1353 Lisp_Object val;
1354
1355 if (inhibit_eval_during_redisplay)
1356 val = Qnil;
1357 else
1358 {
1359 int count = SPECPDL_INDEX ();
1360 struct gcpro gcpro1;
1361
1362 GCPRO1 (sexpr);
1363 specbind (Qinhibit_redisplay, Qt);
1364 /* Use Qt to ensure debugger does not run,
1365 so there is no possibility of wanting to redisplay. */
1366 val = internal_condition_case_1 (Feval, sexpr, Qt,
1367 safe_eval_handler);
1368 UNGCPRO;
1369 val = unbind_to (count, val);
1370 }
1371
1372 return val;
1373 }
1374
1375
1376 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1377 Return the result, or nil if something went wrong. Prevent
1378 redisplay during the evaluation. */
1379
1380 Lisp_Object
1381 safe_call (nargs, args)
1382 int nargs;
1383 Lisp_Object *args;
1384 {
1385 Lisp_Object val;
1386
1387 if (inhibit_eval_during_redisplay)
1388 val = Qnil;
1389 else
1390 {
1391 int count = SPECPDL_INDEX ();
1392 struct gcpro gcpro1;
1393
1394 GCPRO1 (args[0]);
1395 gcpro1.nvars = nargs;
1396 specbind (Qinhibit_redisplay, Qt);
1397 /* Use Qt to ensure debugger does not run,
1398 so there is no possibility of wanting to redisplay. */
1399 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
1400 safe_eval_handler);
1401 UNGCPRO;
1402 val = unbind_to (count, val);
1403 }
1404
1405 return val;
1406 }
1407
1408
1409 /* Call function FN with one argument ARG.
1410 Return the result, or nil if something went wrong. */
1411
1412 Lisp_Object
1413 safe_call1 (fn, arg)
1414 Lisp_Object fn, arg;
1415 {
1416 Lisp_Object args[2];
1417 args[0] = fn;
1418 args[1] = arg;
1419 return safe_call (2, args);
1420 }
1421
1422
1423 \f
1424 /***********************************************************************
1425 Debugging
1426 ***********************************************************************/
1427
1428 #if 0
1429
1430 /* Define CHECK_IT to perform sanity checks on iterators.
1431 This is for debugging. It is too slow to do unconditionally. */
1432
1433 static void
1434 check_it (it)
1435 struct it *it;
1436 {
1437 if (it->method == next_element_from_string)
1438 {
1439 xassert (STRINGP (it->string));
1440 xassert (IT_STRING_CHARPOS (*it) >= 0);
1441 }
1442 else if (it->method == next_element_from_buffer)
1443 {
1444 /* Check that character and byte positions agree. */
1445 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1446 }
1447
1448 if (it->dpvec)
1449 xassert (it->current.dpvec_index >= 0);
1450 else
1451 xassert (it->current.dpvec_index < 0);
1452 }
1453
1454 #define CHECK_IT(IT) check_it ((IT))
1455
1456 #else /* not 0 */
1457
1458 #define CHECK_IT(IT) (void) 0
1459
1460 #endif /* not 0 */
1461
1462
1463 #if GLYPH_DEBUG
1464
1465 /* Check that the window end of window W is what we expect it
1466 to be---the last row in the current matrix displaying text. */
1467
1468 static void
1469 check_window_end (w)
1470 struct window *w;
1471 {
1472 if (!MINI_WINDOW_P (w)
1473 && !NILP (w->window_end_valid))
1474 {
1475 struct glyph_row *row;
1476 xassert ((row = MATRIX_ROW (w->current_matrix,
1477 XFASTINT (w->window_end_vpos)),
1478 !row->enabled_p
1479 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1480 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1481 }
1482 }
1483
1484 #define CHECK_WINDOW_END(W) check_window_end ((W))
1485
1486 #else /* not GLYPH_DEBUG */
1487
1488 #define CHECK_WINDOW_END(W) (void) 0
1489
1490 #endif /* not GLYPH_DEBUG */
1491
1492
1493 \f
1494 /***********************************************************************
1495 Iterator initialization
1496 ***********************************************************************/
1497
1498 /* Initialize IT for displaying current_buffer in window W, starting
1499 at character position CHARPOS. CHARPOS < 0 means that no buffer
1500 position is specified which is useful when the iterator is assigned
1501 a position later. BYTEPOS is the byte position corresponding to
1502 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
1503
1504 If ROW is not null, calls to produce_glyphs with IT as parameter
1505 will produce glyphs in that row.
1506
1507 BASE_FACE_ID is the id of a base face to use. It must be one of
1508 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
1509 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
1510 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
1511
1512 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
1513 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
1514 will be initialized to use the corresponding mode line glyph row of
1515 the desired matrix of W. */
1516
1517 void
1518 init_iterator (it, w, charpos, bytepos, row, base_face_id)
1519 struct it *it;
1520 struct window *w;
1521 int charpos, bytepos;
1522 struct glyph_row *row;
1523 enum face_id base_face_id;
1524 {
1525 int highlight_region_p;
1526
1527 /* Some precondition checks. */
1528 xassert (w != NULL && it != NULL);
1529 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
1530 && charpos <= ZV));
1531
1532 /* If face attributes have been changed since the last redisplay,
1533 free realized faces now because they depend on face definitions
1534 that might have changed. Don't free faces while there might be
1535 desired matrices pending which reference these faces. */
1536 if (face_change_count && !inhibit_free_realized_faces)
1537 {
1538 face_change_count = 0;
1539 free_all_realized_faces (Qnil);
1540 }
1541
1542 /* Use one of the mode line rows of W's desired matrix if
1543 appropriate. */
1544 if (row == NULL)
1545 {
1546 if (base_face_id == MODE_LINE_FACE_ID
1547 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
1548 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
1549 else if (base_face_id == HEADER_LINE_FACE_ID)
1550 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
1551 }
1552
1553 /* Clear IT. */
1554 bzero (it, sizeof *it);
1555 it->current.overlay_string_index = -1;
1556 it->current.dpvec_index = -1;
1557 it->base_face_id = base_face_id;
1558
1559 /* The window in which we iterate over current_buffer: */
1560 XSETWINDOW (it->window, w);
1561 it->w = w;
1562 it->f = XFRAME (w->frame);
1563
1564 /* Extra space between lines (on window systems only). */
1565 if (base_face_id == DEFAULT_FACE_ID
1566 && FRAME_WINDOW_P (it->f))
1567 {
1568 if (NATNUMP (current_buffer->extra_line_spacing))
1569 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
1570 else if (it->f->extra_line_spacing > 0)
1571 it->extra_line_spacing = it->f->extra_line_spacing;
1572 }
1573
1574 /* If realized faces have been removed, e.g. because of face
1575 attribute changes of named faces, recompute them. When running
1576 in batch mode, the face cache of Vterminal_frame is null. If
1577 we happen to get called, make a dummy face cache. */
1578 if (
1579 #ifndef WINDOWSNT
1580 noninteractive &&
1581 #endif
1582 FRAME_FACE_CACHE (it->f) == NULL)
1583 init_frame_faces (it->f);
1584 if (FRAME_FACE_CACHE (it->f)->used == 0)
1585 recompute_basic_faces (it->f);
1586
1587 /* Current value of the `space-width', and 'height' properties. */
1588 it->space_width = Qnil;
1589 it->font_height = Qnil;
1590
1591 /* Are control characters displayed as `^C'? */
1592 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
1593
1594 /* -1 means everything between a CR and the following line end
1595 is invisible. >0 means lines indented more than this value are
1596 invisible. */
1597 it->selective = (INTEGERP (current_buffer->selective_display)
1598 ? XFASTINT (current_buffer->selective_display)
1599 : (!NILP (current_buffer->selective_display)
1600 ? -1 : 0));
1601 it->selective_display_ellipsis_p
1602 = !NILP (current_buffer->selective_display_ellipses);
1603
1604 /* Display table to use. */
1605 it->dp = window_display_table (w);
1606
1607 /* Are multibyte characters enabled in current_buffer? */
1608 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1609
1610 /* Non-zero if we should highlight the region. */
1611 highlight_region_p
1612 = (!NILP (Vtransient_mark_mode)
1613 && !NILP (current_buffer->mark_active)
1614 && XMARKER (current_buffer->mark)->buffer != 0);
1615
1616 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
1617 start and end of a visible region in window IT->w. Set both to
1618 -1 to indicate no region. */
1619 if (highlight_region_p
1620 /* Maybe highlight only in selected window. */
1621 && (/* Either show region everywhere. */
1622 highlight_nonselected_windows
1623 /* Or show region in the selected window. */
1624 || w == XWINDOW (selected_window)
1625 /* Or show the region if we are in the mini-buffer and W is
1626 the window the mini-buffer refers to. */
1627 || (MINI_WINDOW_P (XWINDOW (selected_window))
1628 && WINDOWP (minibuf_selected_window)
1629 && w == XWINDOW (minibuf_selected_window))))
1630 {
1631 int charpos = marker_position (current_buffer->mark);
1632 it->region_beg_charpos = min (PT, charpos);
1633 it->region_end_charpos = max (PT, charpos);
1634 }
1635 else
1636 it->region_beg_charpos = it->region_end_charpos = -1;
1637
1638 /* Get the position at which the redisplay_end_trigger hook should
1639 be run, if it is to be run at all. */
1640 if (MARKERP (w->redisplay_end_trigger)
1641 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
1642 it->redisplay_end_trigger_charpos
1643 = marker_position (w->redisplay_end_trigger);
1644 else if (INTEGERP (w->redisplay_end_trigger))
1645 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
1646
1647 /* Correct bogus values of tab_width. */
1648 it->tab_width = XINT (current_buffer->tab_width);
1649 if (it->tab_width <= 0 || it->tab_width > 1000)
1650 it->tab_width = 8;
1651
1652 /* Are lines in the display truncated? */
1653 it->truncate_lines_p
1654 = (base_face_id != DEFAULT_FACE_ID
1655 || XINT (it->w->hscroll)
1656 || (truncate_partial_width_windows
1657 && !WINDOW_FULL_WIDTH_P (it->w))
1658 || !NILP (current_buffer->truncate_lines));
1659
1660 /* Get dimensions of truncation and continuation glyphs. These are
1661 displayed as fringe bitmaps under X, so we don't need them for such
1662 frames. */
1663 if (!FRAME_WINDOW_P (it->f))
1664 {
1665 if (it->truncate_lines_p)
1666 {
1667 /* We will need the truncation glyph. */
1668 xassert (it->glyph_row == NULL);
1669 produce_special_glyphs (it, IT_TRUNCATION);
1670 it->truncation_pixel_width = it->pixel_width;
1671 }
1672 else
1673 {
1674 /* We will need the continuation glyph. */
1675 xassert (it->glyph_row == NULL);
1676 produce_special_glyphs (it, IT_CONTINUATION);
1677 it->continuation_pixel_width = it->pixel_width;
1678 }
1679
1680 /* Reset these values to zero because the produce_special_glyphs
1681 above has changed them. */
1682 it->pixel_width = it->ascent = it->descent = 0;
1683 it->phys_ascent = it->phys_descent = 0;
1684 }
1685
1686 /* Set this after getting the dimensions of truncation and
1687 continuation glyphs, so that we don't produce glyphs when calling
1688 produce_special_glyphs, above. */
1689 it->glyph_row = row;
1690 it->area = TEXT_AREA;
1691
1692 /* Get the dimensions of the display area. The display area
1693 consists of the visible window area plus a horizontally scrolled
1694 part to the left of the window. All x-values are relative to the
1695 start of this total display area. */
1696 if (base_face_id != DEFAULT_FACE_ID)
1697 {
1698 /* Mode lines, menu bar in terminal frames. */
1699 it->first_visible_x = 0;
1700 it->last_visible_x = XFASTINT (w->width) * CANON_X_UNIT (it->f);
1701 }
1702 else
1703 {
1704 it->first_visible_x
1705 = XFASTINT (it->w->hscroll) * CANON_X_UNIT (it->f);
1706 it->last_visible_x = (it->first_visible_x
1707 + window_box_width (w, TEXT_AREA));
1708
1709 /* If we truncate lines, leave room for the truncator glyph(s) at
1710 the right margin. Otherwise, leave room for the continuation
1711 glyph(s). Truncation and continuation glyphs are not inserted
1712 for window-based redisplay. */
1713 if (!FRAME_WINDOW_P (it->f))
1714 {
1715 if (it->truncate_lines_p)
1716 it->last_visible_x -= it->truncation_pixel_width;
1717 else
1718 it->last_visible_x -= it->continuation_pixel_width;
1719 }
1720
1721 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
1722 it->current_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w) + w->vscroll;
1723 }
1724
1725 /* Leave room for a border glyph. */
1726 if (!FRAME_WINDOW_P (it->f)
1727 && !WINDOW_RIGHTMOST_P (it->w))
1728 it->last_visible_x -= 1;
1729
1730 it->last_visible_y = window_text_bottom_y (w);
1731
1732 /* For mode lines and alike, arrange for the first glyph having a
1733 left box line if the face specifies a box. */
1734 if (base_face_id != DEFAULT_FACE_ID)
1735 {
1736 struct face *face;
1737
1738 it->face_id = base_face_id;
1739
1740 /* If we have a boxed mode line, make the first character appear
1741 with a left box line. */
1742 face = FACE_FROM_ID (it->f, base_face_id);
1743 if (face->box != FACE_NO_BOX)
1744 it->start_of_box_run_p = 1;
1745 }
1746
1747 /* If a buffer position was specified, set the iterator there,
1748 getting overlays and face properties from that position. */
1749 if (charpos >= BUF_BEG (current_buffer))
1750 {
1751 it->end_charpos = ZV;
1752 it->face_id = -1;
1753 IT_CHARPOS (*it) = charpos;
1754
1755 /* Compute byte position if not specified. */
1756 if (bytepos < charpos)
1757 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
1758 else
1759 IT_BYTEPOS (*it) = bytepos;
1760
1761 /* Compute faces etc. */
1762 reseat (it, it->current.pos, 1);
1763 }
1764
1765 CHECK_IT (it);
1766 }
1767
1768
1769 /* Initialize IT for the display of window W with window start POS. */
1770
1771 void
1772 start_display (it, w, pos)
1773 struct it *it;
1774 struct window *w;
1775 struct text_pos pos;
1776 {
1777 struct glyph_row *row;
1778 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
1779
1780 row = w->desired_matrix->rows + first_vpos;
1781 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
1782
1783 if (!it->truncate_lines_p)
1784 {
1785 int start_at_line_beg_p;
1786 int first_y = it->current_y;
1787
1788 /* If window start is not at a line start, skip forward to POS to
1789 get the correct continuation lines width. */
1790 start_at_line_beg_p = (CHARPOS (pos) == BEGV
1791 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
1792 if (!start_at_line_beg_p)
1793 {
1794 int new_x;
1795
1796 reseat_at_previous_visible_line_start (it);
1797 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
1798
1799 new_x = it->current_x + it->pixel_width;
1800
1801 /* If lines are continued, this line may end in the middle
1802 of a multi-glyph character (e.g. a control character
1803 displayed as \003, or in the middle of an overlay
1804 string). In this case move_it_to above will not have
1805 taken us to the start of the continuation line but to the
1806 end of the continued line. */
1807 if (it->current_x > 0
1808 && !it->truncate_lines_p /* Lines are continued. */
1809 && (/* And glyph doesn't fit on the line. */
1810 new_x > it->last_visible_x
1811 /* Or it fits exactly and we're on a window
1812 system frame. */
1813 || (new_x == it->last_visible_x
1814 && FRAME_WINDOW_P (it->f))))
1815 {
1816 if (it->current.dpvec_index >= 0
1817 || it->current.overlay_string_index >= 0)
1818 {
1819 set_iterator_to_next (it, 1);
1820 move_it_in_display_line_to (it, -1, -1, 0);
1821 }
1822
1823 it->continuation_lines_width += it->current_x;
1824 }
1825
1826 /* We're starting a new display line, not affected by the
1827 height of the continued line, so clear the appropriate
1828 fields in the iterator structure. */
1829 it->max_ascent = it->max_descent = 0;
1830 it->max_phys_ascent = it->max_phys_descent = 0;
1831
1832 it->current_y = first_y;
1833 it->vpos = 0;
1834 it->current_x = it->hpos = 0;
1835 }
1836 }
1837
1838 #if 0 /* Don't assert the following because start_display is sometimes
1839 called intentionally with a window start that is not at a
1840 line start. Please leave this code in as a comment. */
1841
1842 /* Window start should be on a line start, now. */
1843 xassert (it->continuation_lines_width
1844 || IT_CHARPOS (it) == BEGV
1845 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
1846 #endif /* 0 */
1847 }
1848
1849
1850 /* Return 1 if POS is a position in ellipses displayed for invisible
1851 text. W is the window we display, for text property lookup. */
1852
1853 static int
1854 in_ellipses_for_invisible_text_p (pos, w)
1855 struct display_pos *pos;
1856 struct window *w;
1857 {
1858 Lisp_Object prop, window;
1859 int ellipses_p = 0;
1860 int charpos = CHARPOS (pos->pos);
1861
1862 /* If POS specifies a position in a display vector, this might
1863 be for an ellipsis displayed for invisible text. We won't
1864 get the iterator set up for delivering that ellipsis unless
1865 we make sure that it gets aware of the invisible text. */
1866 if (pos->dpvec_index >= 0
1867 && pos->overlay_string_index < 0
1868 && CHARPOS (pos->string_pos) < 0
1869 && charpos > BEGV
1870 && (XSETWINDOW (window, w),
1871 prop = Fget_char_property (make_number (charpos),
1872 Qinvisible, window),
1873 !TEXT_PROP_MEANS_INVISIBLE (prop)))
1874 {
1875 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
1876 window);
1877 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
1878 }
1879
1880 return ellipses_p;
1881 }
1882
1883
1884 /* Initialize IT for stepping through current_buffer in window W,
1885 starting at position POS that includes overlay string and display
1886 vector/ control character translation position information. Value
1887 is zero if there are overlay strings with newlines at POS. */
1888
1889 static int
1890 init_from_display_pos (it, w, pos)
1891 struct it *it;
1892 struct window *w;
1893 struct display_pos *pos;
1894 {
1895 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
1896 int i, overlay_strings_with_newlines = 0;
1897
1898 /* If POS specifies a position in a display vector, this might
1899 be for an ellipsis displayed for invisible text. We won't
1900 get the iterator set up for delivering that ellipsis unless
1901 we make sure that it gets aware of the invisible text. */
1902 if (in_ellipses_for_invisible_text_p (pos, w))
1903 {
1904 --charpos;
1905 bytepos = 0;
1906 }
1907
1908 /* Keep in mind: the call to reseat in init_iterator skips invisible
1909 text, so we might end up at a position different from POS. This
1910 is only a problem when POS is a row start after a newline and an
1911 overlay starts there with an after-string, and the overlay has an
1912 invisible property. Since we don't skip invisible text in
1913 display_line and elsewhere immediately after consuming the
1914 newline before the row start, such a POS will not be in a string,
1915 but the call to init_iterator below will move us to the
1916 after-string. */
1917 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
1918
1919 for (i = 0; i < it->n_overlay_strings; ++i)
1920 {
1921 const char *s = SDATA (it->overlay_strings[i]);
1922 const char *e = s + SBYTES (it->overlay_strings[i]);
1923
1924 while (s < e && *s != '\n')
1925 ++s;
1926
1927 if (s < e)
1928 {
1929 overlay_strings_with_newlines = 1;
1930 break;
1931 }
1932 }
1933
1934 /* If position is within an overlay string, set up IT to the right
1935 overlay string. */
1936 if (pos->overlay_string_index >= 0)
1937 {
1938 int relative_index;
1939
1940 /* If the first overlay string happens to have a `display'
1941 property for an image, the iterator will be set up for that
1942 image, and we have to undo that setup first before we can
1943 correct the overlay string index. */
1944 if (it->method == next_element_from_image)
1945 pop_it (it);
1946
1947 /* We already have the first chunk of overlay strings in
1948 IT->overlay_strings. Load more until the one for
1949 pos->overlay_string_index is in IT->overlay_strings. */
1950 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
1951 {
1952 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
1953 it->current.overlay_string_index = 0;
1954 while (n--)
1955 {
1956 load_overlay_strings (it, 0);
1957 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
1958 }
1959 }
1960
1961 it->current.overlay_string_index = pos->overlay_string_index;
1962 relative_index = (it->current.overlay_string_index
1963 % OVERLAY_STRING_CHUNK_SIZE);
1964 it->string = it->overlay_strings[relative_index];
1965 xassert (STRINGP (it->string));
1966 it->current.string_pos = pos->string_pos;
1967 it->method = next_element_from_string;
1968 }
1969
1970 #if 0 /* This is bogus because POS not having an overlay string
1971 position does not mean it's after the string. Example: A
1972 line starting with a before-string and initialization of IT
1973 to the previous row's end position. */
1974 else if (it->current.overlay_string_index >= 0)
1975 {
1976 /* If POS says we're already after an overlay string ending at
1977 POS, make sure to pop the iterator because it will be in
1978 front of that overlay string. When POS is ZV, we've thereby
1979 also ``processed'' overlay strings at ZV. */
1980 while (it->sp)
1981 pop_it (it);
1982 it->current.overlay_string_index = -1;
1983 it->method = next_element_from_buffer;
1984 if (CHARPOS (pos->pos) == ZV)
1985 it->overlay_strings_at_end_processed_p = 1;
1986 }
1987 #endif /* 0 */
1988
1989 if (CHARPOS (pos->string_pos) >= 0)
1990 {
1991 /* Recorded position is not in an overlay string, but in another
1992 string. This can only be a string from a `display' property.
1993 IT should already be filled with that string. */
1994 it->current.string_pos = pos->string_pos;
1995 xassert (STRINGP (it->string));
1996 }
1997
1998 /* Restore position in display vector translations, control
1999 character translations or ellipses. */
2000 if (pos->dpvec_index >= 0)
2001 {
2002 if (it->dpvec == NULL)
2003 get_next_display_element (it);
2004 xassert (it->dpvec && it->current.dpvec_index == 0);
2005 it->current.dpvec_index = pos->dpvec_index;
2006 }
2007
2008 CHECK_IT (it);
2009 return !overlay_strings_with_newlines;
2010 }
2011
2012
2013 /* Initialize IT for stepping through current_buffer in window W
2014 starting at ROW->start. */
2015
2016 static void
2017 init_to_row_start (it, w, row)
2018 struct it *it;
2019 struct window *w;
2020 struct glyph_row *row;
2021 {
2022 init_from_display_pos (it, w, &row->start);
2023 it->continuation_lines_width = row->continuation_lines_width;
2024 CHECK_IT (it);
2025 }
2026
2027
2028 /* Initialize IT for stepping through current_buffer in window W
2029 starting in the line following ROW, i.e. starting at ROW->end.
2030 Value is zero if there are overlay strings with newlines at ROW's
2031 end position. */
2032
2033 static int
2034 init_to_row_end (it, w, row)
2035 struct it *it;
2036 struct window *w;
2037 struct glyph_row *row;
2038 {
2039 int success = 0;
2040
2041 if (init_from_display_pos (it, w, &row->end))
2042 {
2043 if (row->continued_p)
2044 it->continuation_lines_width
2045 = row->continuation_lines_width + row->pixel_width;
2046 CHECK_IT (it);
2047 success = 1;
2048 }
2049
2050 return success;
2051 }
2052
2053
2054
2055 \f
2056 /***********************************************************************
2057 Text properties
2058 ***********************************************************************/
2059
2060 /* Called when IT reaches IT->stop_charpos. Handle text property and
2061 overlay changes. Set IT->stop_charpos to the next position where
2062 to stop. */
2063
2064 static void
2065 handle_stop (it)
2066 struct it *it;
2067 {
2068 enum prop_handled handled;
2069 int handle_overlay_change_p = 1;
2070 struct props *p;
2071
2072 it->dpvec = NULL;
2073 it->current.dpvec_index = -1;
2074
2075 do
2076 {
2077 handled = HANDLED_NORMALLY;
2078
2079 /* Call text property handlers. */
2080 for (p = it_props; p->handler; ++p)
2081 {
2082 handled = p->handler (it);
2083
2084 if (handled == HANDLED_RECOMPUTE_PROPS)
2085 break;
2086 else if (handled == HANDLED_RETURN)
2087 return;
2088 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2089 handle_overlay_change_p = 0;
2090 }
2091
2092 if (handled != HANDLED_RECOMPUTE_PROPS)
2093 {
2094 /* Don't check for overlay strings below when set to deliver
2095 characters from a display vector. */
2096 if (it->method == next_element_from_display_vector)
2097 handle_overlay_change_p = 0;
2098
2099 /* Handle overlay changes. */
2100 if (handle_overlay_change_p)
2101 handled = handle_overlay_change (it);
2102
2103 /* Determine where to stop next. */
2104 if (handled == HANDLED_NORMALLY)
2105 compute_stop_pos (it);
2106 }
2107 }
2108 while (handled == HANDLED_RECOMPUTE_PROPS);
2109 }
2110
2111
2112 /* Compute IT->stop_charpos from text property and overlay change
2113 information for IT's current position. */
2114
2115 static void
2116 compute_stop_pos (it)
2117 struct it *it;
2118 {
2119 register INTERVAL iv, next_iv;
2120 Lisp_Object object, limit, position;
2121
2122 /* If nowhere else, stop at the end. */
2123 it->stop_charpos = it->end_charpos;
2124
2125 if (STRINGP (it->string))
2126 {
2127 /* Strings are usually short, so don't limit the search for
2128 properties. */
2129 object = it->string;
2130 limit = Qnil;
2131 position = make_number (IT_STRING_CHARPOS (*it));
2132 }
2133 else
2134 {
2135 int charpos;
2136
2137 /* If next overlay change is in front of the current stop pos
2138 (which is IT->end_charpos), stop there. Note: value of
2139 next_overlay_change is point-max if no overlay change
2140 follows. */
2141 charpos = next_overlay_change (IT_CHARPOS (*it));
2142 if (charpos < it->stop_charpos)
2143 it->stop_charpos = charpos;
2144
2145 /* If showing the region, we have to stop at the region
2146 start or end because the face might change there. */
2147 if (it->region_beg_charpos > 0)
2148 {
2149 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2150 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2151 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2152 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2153 }
2154
2155 /* Set up variables for computing the stop position from text
2156 property changes. */
2157 XSETBUFFER (object, current_buffer);
2158 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2159 position = make_number (IT_CHARPOS (*it));
2160
2161 }
2162
2163 /* Get the interval containing IT's position. Value is a null
2164 interval if there isn't such an interval. */
2165 iv = validate_interval_range (object, &position, &position, 0);
2166 if (!NULL_INTERVAL_P (iv))
2167 {
2168 Lisp_Object values_here[LAST_PROP_IDX];
2169 struct props *p;
2170
2171 /* Get properties here. */
2172 for (p = it_props; p->handler; ++p)
2173 values_here[p->idx] = textget (iv->plist, *p->name);
2174
2175 /* Look for an interval following iv that has different
2176 properties. */
2177 for (next_iv = next_interval (iv);
2178 (!NULL_INTERVAL_P (next_iv)
2179 && (NILP (limit)
2180 || XFASTINT (limit) > next_iv->position));
2181 next_iv = next_interval (next_iv))
2182 {
2183 for (p = it_props; p->handler; ++p)
2184 {
2185 Lisp_Object new_value;
2186
2187 new_value = textget (next_iv->plist, *p->name);
2188 if (!EQ (values_here[p->idx], new_value))
2189 break;
2190 }
2191
2192 if (p->handler)
2193 break;
2194 }
2195
2196 if (!NULL_INTERVAL_P (next_iv))
2197 {
2198 if (INTEGERP (limit)
2199 && next_iv->position >= XFASTINT (limit))
2200 /* No text property change up to limit. */
2201 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2202 else
2203 /* Text properties change in next_iv. */
2204 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2205 }
2206 }
2207
2208 xassert (STRINGP (it->string)
2209 || (it->stop_charpos >= BEGV
2210 && it->stop_charpos >= IT_CHARPOS (*it)));
2211 }
2212
2213
2214 /* Return the position of the next overlay change after POS in
2215 current_buffer. Value is point-max if no overlay change
2216 follows. This is like `next-overlay-change' but doesn't use
2217 xmalloc. */
2218
2219 static int
2220 next_overlay_change (pos)
2221 int pos;
2222 {
2223 int noverlays;
2224 int endpos;
2225 Lisp_Object *overlays;
2226 int len;
2227 int i;
2228
2229 /* Get all overlays at the given position. */
2230 len = 10;
2231 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2232 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2233 if (noverlays > len)
2234 {
2235 len = noverlays;
2236 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2237 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2238 }
2239
2240 /* If any of these overlays ends before endpos,
2241 use its ending point instead. */
2242 for (i = 0; i < noverlays; ++i)
2243 {
2244 Lisp_Object oend;
2245 int oendpos;
2246
2247 oend = OVERLAY_END (overlays[i]);
2248 oendpos = OVERLAY_POSITION (oend);
2249 endpos = min (endpos, oendpos);
2250 }
2251
2252 return endpos;
2253 }
2254
2255
2256 \f
2257 /***********************************************************************
2258 Fontification
2259 ***********************************************************************/
2260
2261 /* Handle changes in the `fontified' property of the current buffer by
2262 calling hook functions from Qfontification_functions to fontify
2263 regions of text. */
2264
2265 static enum prop_handled
2266 handle_fontified_prop (it)
2267 struct it *it;
2268 {
2269 Lisp_Object prop, pos;
2270 enum prop_handled handled = HANDLED_NORMALLY;
2271
2272 /* Get the value of the `fontified' property at IT's current buffer
2273 position. (The `fontified' property doesn't have a special
2274 meaning in strings.) If the value is nil, call functions from
2275 Qfontification_functions. */
2276 if (!STRINGP (it->string)
2277 && it->s == NULL
2278 && !NILP (Vfontification_functions)
2279 && !NILP (Vrun_hooks)
2280 && (pos = make_number (IT_CHARPOS (*it)),
2281 prop = Fget_char_property (pos, Qfontified, Qnil),
2282 NILP (prop)))
2283 {
2284 int count = SPECPDL_INDEX ();
2285 Lisp_Object val;
2286
2287 val = Vfontification_functions;
2288 specbind (Qfontification_functions, Qnil);
2289
2290 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2291 safe_call1 (val, pos);
2292 else
2293 {
2294 Lisp_Object globals, fn;
2295 struct gcpro gcpro1, gcpro2;
2296
2297 globals = Qnil;
2298 GCPRO2 (val, globals);
2299
2300 for (; CONSP (val); val = XCDR (val))
2301 {
2302 fn = XCAR (val);
2303
2304 if (EQ (fn, Qt))
2305 {
2306 /* A value of t indicates this hook has a local
2307 binding; it means to run the global binding too.
2308 In a global value, t should not occur. If it
2309 does, we must ignore it to avoid an endless
2310 loop. */
2311 for (globals = Fdefault_value (Qfontification_functions);
2312 CONSP (globals);
2313 globals = XCDR (globals))
2314 {
2315 fn = XCAR (globals);
2316 if (!EQ (fn, Qt))
2317 safe_call1 (fn, pos);
2318 }
2319 }
2320 else
2321 safe_call1 (fn, pos);
2322 }
2323
2324 UNGCPRO;
2325 }
2326
2327 unbind_to (count, Qnil);
2328
2329 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2330 something. This avoids an endless loop if they failed to
2331 fontify the text for which reason ever. */
2332 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2333 handled = HANDLED_RECOMPUTE_PROPS;
2334 }
2335
2336 return handled;
2337 }
2338
2339
2340 \f
2341 /***********************************************************************
2342 Faces
2343 ***********************************************************************/
2344
2345 /* Set up iterator IT from face properties at its current position.
2346 Called from handle_stop. */
2347
2348 static enum prop_handled
2349 handle_face_prop (it)
2350 struct it *it;
2351 {
2352 int new_face_id, next_stop;
2353
2354 if (!STRINGP (it->string))
2355 {
2356 new_face_id
2357 = face_at_buffer_position (it->w,
2358 IT_CHARPOS (*it),
2359 it->region_beg_charpos,
2360 it->region_end_charpos,
2361 &next_stop,
2362 (IT_CHARPOS (*it)
2363 + TEXT_PROP_DISTANCE_LIMIT),
2364 0);
2365
2366 /* Is this a start of a run of characters with box face?
2367 Caveat: this can be called for a freshly initialized
2368 iterator; face_id is -1 in this case. We know that the new
2369 face will not change until limit, i.e. if the new face has a
2370 box, all characters up to limit will have one. But, as
2371 usual, we don't know whether limit is really the end. */
2372 if (new_face_id != it->face_id)
2373 {
2374 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2375
2376 /* If new face has a box but old face has not, this is
2377 the start of a run of characters with box, i.e. it has
2378 a shadow on the left side. The value of face_id of the
2379 iterator will be -1 if this is the initial call that gets
2380 the face. In this case, we have to look in front of IT's
2381 position and see whether there is a face != new_face_id. */
2382 it->start_of_box_run_p
2383 = (new_face->box != FACE_NO_BOX
2384 && (it->face_id >= 0
2385 || IT_CHARPOS (*it) == BEG
2386 || new_face_id != face_before_it_pos (it)));
2387 it->face_box_p = new_face->box != FACE_NO_BOX;
2388 }
2389 }
2390 else
2391 {
2392 int base_face_id, bufpos;
2393
2394 if (it->current.overlay_string_index >= 0)
2395 bufpos = IT_CHARPOS (*it);
2396 else
2397 bufpos = 0;
2398
2399 /* For strings from a buffer, i.e. overlay strings or strings
2400 from a `display' property, use the face at IT's current
2401 buffer position as the base face to merge with, so that
2402 overlay strings appear in the same face as surrounding
2403 text, unless they specify their own faces. */
2404 base_face_id = underlying_face_id (it);
2405
2406 new_face_id = face_at_string_position (it->w,
2407 it->string,
2408 IT_STRING_CHARPOS (*it),
2409 bufpos,
2410 it->region_beg_charpos,
2411 it->region_end_charpos,
2412 &next_stop,
2413 base_face_id, 0);
2414
2415 #if 0 /* This shouldn't be neccessary. Let's check it. */
2416 /* If IT is used to display a mode line we would really like to
2417 use the mode line face instead of the frame's default face. */
2418 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2419 && new_face_id == DEFAULT_FACE_ID)
2420 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
2421 #endif
2422
2423 /* Is this a start of a run of characters with box? Caveat:
2424 this can be called for a freshly allocated iterator; face_id
2425 is -1 is this case. We know that the new face will not
2426 change until the next check pos, i.e. if the new face has a
2427 box, all characters up to that position will have a
2428 box. But, as usual, we don't know whether that position
2429 is really the end. */
2430 if (new_face_id != it->face_id)
2431 {
2432 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2433 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2434
2435 /* If new face has a box but old face hasn't, this is the
2436 start of a run of characters with box, i.e. it has a
2437 shadow on the left side. */
2438 it->start_of_box_run_p
2439 = new_face->box && (old_face == NULL || !old_face->box);
2440 it->face_box_p = new_face->box != FACE_NO_BOX;
2441 }
2442 }
2443
2444 it->face_id = new_face_id;
2445 return HANDLED_NORMALLY;
2446 }
2447
2448
2449 /* Return the ID of the face ``underlying'' IT's current position,
2450 which is in a string. If the iterator is associated with a
2451 buffer, return the face at IT's current buffer position.
2452 Otherwise, use the iterator's base_face_id. */
2453
2454 static int
2455 underlying_face_id (it)
2456 struct it *it;
2457 {
2458 int face_id = it->base_face_id, i;
2459
2460 xassert (STRINGP (it->string));
2461
2462 for (i = it->sp - 1; i >= 0; --i)
2463 if (NILP (it->stack[i].string))
2464 face_id = it->stack[i].face_id;
2465
2466 return face_id;
2467 }
2468
2469
2470 /* Compute the face one character before or after the current position
2471 of IT. BEFORE_P non-zero means get the face in front of IT's
2472 position. Value is the id of the face. */
2473
2474 static int
2475 face_before_or_after_it_pos (it, before_p)
2476 struct it *it;
2477 int before_p;
2478 {
2479 int face_id, limit;
2480 int next_check_charpos;
2481 struct text_pos pos;
2482
2483 xassert (it->s == NULL);
2484
2485 if (STRINGP (it->string))
2486 {
2487 int bufpos, base_face_id;
2488
2489 /* No face change past the end of the string (for the case
2490 we are padding with spaces). No face change before the
2491 string start. */
2492 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
2493 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2494 return it->face_id;
2495
2496 /* Set pos to the position before or after IT's current position. */
2497 if (before_p)
2498 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2499 else
2500 /* For composition, we must check the character after the
2501 composition. */
2502 pos = (it->what == IT_COMPOSITION
2503 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
2504 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
2505
2506 if (it->current.overlay_string_index >= 0)
2507 bufpos = IT_CHARPOS (*it);
2508 else
2509 bufpos = 0;
2510
2511 base_face_id = underlying_face_id (it);
2512
2513 /* Get the face for ASCII, or unibyte. */
2514 face_id = face_at_string_position (it->w,
2515 it->string,
2516 CHARPOS (pos),
2517 bufpos,
2518 it->region_beg_charpos,
2519 it->region_end_charpos,
2520 &next_check_charpos,
2521 base_face_id, 0);
2522
2523 /* Correct the face for charsets different from ASCII. Do it
2524 for the multibyte case only. The face returned above is
2525 suitable for unibyte text if IT->string is unibyte. */
2526 if (STRING_MULTIBYTE (it->string))
2527 {
2528 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
2529 int rest = SBYTES (it->string) - BYTEPOS (pos);
2530 int c, len;
2531 struct face *face = FACE_FROM_ID (it->f, face_id);
2532
2533 c = string_char_and_length (p, rest, &len);
2534 face_id = FACE_FOR_CHAR (it->f, face, c);
2535 }
2536 }
2537 else
2538 {
2539 if ((IT_CHARPOS (*it) >= ZV && !before_p)
2540 || (IT_CHARPOS (*it) <= BEGV && before_p))
2541 return it->face_id;
2542
2543 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
2544 pos = it->current.pos;
2545
2546 if (before_p)
2547 DEC_TEXT_POS (pos, it->multibyte_p);
2548 else
2549 {
2550 if (it->what == IT_COMPOSITION)
2551 /* For composition, we must check the position after the
2552 composition. */
2553 pos.charpos += it->cmp_len, pos.bytepos += it->len;
2554 else
2555 INC_TEXT_POS (pos, it->multibyte_p);
2556 }
2557
2558 /* Determine face for CHARSET_ASCII, or unibyte. */
2559 face_id = face_at_buffer_position (it->w,
2560 CHARPOS (pos),
2561 it->region_beg_charpos,
2562 it->region_end_charpos,
2563 &next_check_charpos,
2564 limit, 0);
2565
2566 /* Correct the face for charsets different from ASCII. Do it
2567 for the multibyte case only. The face returned above is
2568 suitable for unibyte text if current_buffer is unibyte. */
2569 if (it->multibyte_p)
2570 {
2571 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
2572 struct face *face = FACE_FROM_ID (it->f, face_id);
2573 face_id = FACE_FOR_CHAR (it->f, face, c);
2574 }
2575 }
2576
2577 return face_id;
2578 }
2579
2580
2581 \f
2582 /***********************************************************************
2583 Invisible text
2584 ***********************************************************************/
2585
2586 /* Set up iterator IT from invisible properties at its current
2587 position. Called from handle_stop. */
2588
2589 static enum prop_handled
2590 handle_invisible_prop (it)
2591 struct it *it;
2592 {
2593 enum prop_handled handled = HANDLED_NORMALLY;
2594
2595 if (STRINGP (it->string))
2596 {
2597 extern Lisp_Object Qinvisible;
2598 Lisp_Object prop, end_charpos, limit, charpos;
2599
2600 /* Get the value of the invisible text property at the
2601 current position. Value will be nil if there is no such
2602 property. */
2603 charpos = make_number (IT_STRING_CHARPOS (*it));
2604 prop = Fget_text_property (charpos, Qinvisible, it->string);
2605
2606 if (!NILP (prop)
2607 && IT_STRING_CHARPOS (*it) < it->end_charpos)
2608 {
2609 handled = HANDLED_RECOMPUTE_PROPS;
2610
2611 /* Get the position at which the next change of the
2612 invisible text property can be found in IT->string.
2613 Value will be nil if the property value is the same for
2614 all the rest of IT->string. */
2615 XSETINT (limit, SCHARS (it->string));
2616 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2617 it->string, limit);
2618
2619 /* Text at current position is invisible. The next
2620 change in the property is at position end_charpos.
2621 Move IT's current position to that position. */
2622 if (INTEGERP (end_charpos)
2623 && XFASTINT (end_charpos) < XFASTINT (limit))
2624 {
2625 struct text_pos old;
2626 old = it->current.string_pos;
2627 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
2628 compute_string_pos (&it->current.string_pos, old, it->string);
2629 }
2630 else
2631 {
2632 /* The rest of the string is invisible. If this is an
2633 overlay string, proceed with the next overlay string
2634 or whatever comes and return a character from there. */
2635 if (it->current.overlay_string_index >= 0)
2636 {
2637 next_overlay_string (it);
2638 /* Don't check for overlay strings when we just
2639 finished processing them. */
2640 handled = HANDLED_OVERLAY_STRING_CONSUMED;
2641 }
2642 else
2643 {
2644 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
2645 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
2646 }
2647 }
2648 }
2649 }
2650 else
2651 {
2652 int invis_p, newpos, next_stop, start_charpos;
2653 Lisp_Object pos, prop, overlay;
2654
2655 /* First of all, is there invisible text at this position? */
2656 start_charpos = IT_CHARPOS (*it);
2657 pos = make_number (IT_CHARPOS (*it));
2658 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
2659 &overlay);
2660 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
2661
2662 /* If we are on invisible text, skip over it. */
2663 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
2664 {
2665 /* Record whether we have to display an ellipsis for the
2666 invisible text. */
2667 int display_ellipsis_p = invis_p == 2;
2668
2669 handled = HANDLED_RECOMPUTE_PROPS;
2670
2671 /* Loop skipping over invisible text. The loop is left at
2672 ZV or with IT on the first char being visible again. */
2673 do
2674 {
2675 /* Try to skip some invisible text. Return value is the
2676 position reached which can be equal to IT's position
2677 if there is nothing invisible here. This skips both
2678 over invisible text properties and overlays with
2679 invisible property. */
2680 newpos = skip_invisible (IT_CHARPOS (*it),
2681 &next_stop, ZV, it->window);
2682
2683 /* If we skipped nothing at all we weren't at invisible
2684 text in the first place. If everything to the end of
2685 the buffer was skipped, end the loop. */
2686 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
2687 invis_p = 0;
2688 else
2689 {
2690 /* We skipped some characters but not necessarily
2691 all there are. Check if we ended up on visible
2692 text. Fget_char_property returns the property of
2693 the char before the given position, i.e. if we
2694 get invis_p = 0, this means that the char at
2695 newpos is visible. */
2696 pos = make_number (newpos);
2697 prop = Fget_char_property (pos, Qinvisible, it->window);
2698 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
2699 }
2700
2701 /* If we ended up on invisible text, proceed to
2702 skip starting with next_stop. */
2703 if (invis_p)
2704 IT_CHARPOS (*it) = next_stop;
2705 }
2706 while (invis_p);
2707
2708 /* The position newpos is now either ZV or on visible text. */
2709 IT_CHARPOS (*it) = newpos;
2710 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2711
2712 /* If there are before-strings at the start of invisible
2713 text, and the text is invisible because of a text
2714 property, arrange to show before-strings because 20.x did
2715 it that way. (If the text is invisible because of an
2716 overlay property instead of a text property, this is
2717 already handled in the overlay code.) */
2718 if (NILP (overlay)
2719 && get_overlay_strings (it, start_charpos))
2720 {
2721 handled = HANDLED_RECOMPUTE_PROPS;
2722 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
2723 }
2724 else if (display_ellipsis_p)
2725 setup_for_ellipsis (it);
2726 }
2727 }
2728
2729 return handled;
2730 }
2731
2732
2733 /* Make iterator IT return `...' next. */
2734
2735 static void
2736 setup_for_ellipsis (it)
2737 struct it *it;
2738 {
2739 if (it->dp
2740 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
2741 {
2742 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
2743 it->dpvec = v->contents;
2744 it->dpend = v->contents + v->size;
2745 }
2746 else
2747 {
2748 /* Default `...'. */
2749 it->dpvec = default_invis_vector;
2750 it->dpend = default_invis_vector + 3;
2751 }
2752
2753 /* The ellipsis display does not replace the display of the
2754 character at the new position. Indicate this by setting
2755 IT->dpvec_char_len to zero. */
2756 it->dpvec_char_len = 0;
2757
2758 it->current.dpvec_index = 0;
2759 it->method = next_element_from_display_vector;
2760 }
2761
2762
2763 \f
2764 /***********************************************************************
2765 'display' property
2766 ***********************************************************************/
2767
2768 /* Set up iterator IT from `display' property at its current position.
2769 Called from handle_stop. */
2770
2771 static enum prop_handled
2772 handle_display_prop (it)
2773 struct it *it;
2774 {
2775 Lisp_Object prop, object;
2776 struct text_pos *position;
2777 int display_replaced_p = 0;
2778
2779 if (STRINGP (it->string))
2780 {
2781 object = it->string;
2782 position = &it->current.string_pos;
2783 }
2784 else
2785 {
2786 object = it->w->buffer;
2787 position = &it->current.pos;
2788 }
2789
2790 /* Reset those iterator values set from display property values. */
2791 it->font_height = Qnil;
2792 it->space_width = Qnil;
2793 it->voffset = 0;
2794
2795 /* We don't support recursive `display' properties, i.e. string
2796 values that have a string `display' property, that have a string
2797 `display' property etc. */
2798 if (!it->string_from_display_prop_p)
2799 it->area = TEXT_AREA;
2800
2801 prop = Fget_char_property (make_number (position->charpos),
2802 Qdisplay, object);
2803 if (NILP (prop))
2804 return HANDLED_NORMALLY;
2805
2806 if (CONSP (prop)
2807 /* Simple properties. */
2808 && !EQ (XCAR (prop), Qimage)
2809 && !EQ (XCAR (prop), Qspace)
2810 && !EQ (XCAR (prop), Qwhen)
2811 && !EQ (XCAR (prop), Qspace_width)
2812 && !EQ (XCAR (prop), Qheight)
2813 && !EQ (XCAR (prop), Qraise)
2814 /* Marginal area specifications. */
2815 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
2816 && !NILP (XCAR (prop)))
2817 {
2818 for (; CONSP (prop); prop = XCDR (prop))
2819 {
2820 if (handle_single_display_prop (it, XCAR (prop), object,
2821 position, display_replaced_p))
2822 display_replaced_p = 1;
2823 }
2824 }
2825 else if (VECTORP (prop))
2826 {
2827 int i;
2828 for (i = 0; i < ASIZE (prop); ++i)
2829 if (handle_single_display_prop (it, AREF (prop, i), object,
2830 position, display_replaced_p))
2831 display_replaced_p = 1;
2832 }
2833 else
2834 {
2835 if (handle_single_display_prop (it, prop, object, position, 0))
2836 display_replaced_p = 1;
2837 }
2838
2839 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
2840 }
2841
2842
2843 /* Value is the position of the end of the `display' property starting
2844 at START_POS in OBJECT. */
2845
2846 static struct text_pos
2847 display_prop_end (it, object, start_pos)
2848 struct it *it;
2849 Lisp_Object object;
2850 struct text_pos start_pos;
2851 {
2852 Lisp_Object end;
2853 struct text_pos end_pos;
2854
2855 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
2856 Qdisplay, object, Qnil);
2857 CHARPOS (end_pos) = XFASTINT (end);
2858 if (STRINGP (object))
2859 compute_string_pos (&end_pos, start_pos, it->string);
2860 else
2861 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
2862
2863 return end_pos;
2864 }
2865
2866
2867 /* Set up IT from a single `display' sub-property value PROP. OBJECT
2868 is the object in which the `display' property was found. *POSITION
2869 is the position at which it was found. DISPLAY_REPLACED_P non-zero
2870 means that we previously saw a display sub-property which already
2871 replaced text display with something else, for example an image;
2872 ignore such properties after the first one has been processed.
2873
2874 If PROP is a `space' or `image' sub-property, set *POSITION to the
2875 end position of the `display' property.
2876
2877 Value is non-zero if something was found which replaces the display
2878 of buffer or string text. */
2879
2880 static int
2881 handle_single_display_prop (it, prop, object, position,
2882 display_replaced_before_p)
2883 struct it *it;
2884 Lisp_Object prop;
2885 Lisp_Object object;
2886 struct text_pos *position;
2887 int display_replaced_before_p;
2888 {
2889 Lisp_Object value;
2890 int replaces_text_display_p = 0;
2891 Lisp_Object form;
2892
2893 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
2894 evaluated. If the result is nil, VALUE is ignored. */
2895 form = Qt;
2896 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2897 {
2898 prop = XCDR (prop);
2899 if (!CONSP (prop))
2900 return 0;
2901 form = XCAR (prop);
2902 prop = XCDR (prop);
2903 }
2904
2905 if (!NILP (form) && !EQ (form, Qt))
2906 {
2907 int count = SPECPDL_INDEX ();
2908 struct gcpro gcpro1;
2909
2910 /* Bind `object' to the object having the `display' property, a
2911 buffer or string. Bind `position' to the position in the
2912 object where the property was found, and `buffer-position'
2913 to the current position in the buffer. */
2914 specbind (Qobject, object);
2915 specbind (Qposition, make_number (CHARPOS (*position)));
2916 specbind (Qbuffer_position,
2917 make_number (STRINGP (object)
2918 ? IT_CHARPOS (*it) : CHARPOS (*position)));
2919 GCPRO1 (form);
2920 form = safe_eval (form);
2921 UNGCPRO;
2922 unbind_to (count, Qnil);
2923 }
2924
2925 if (NILP (form))
2926 return 0;
2927
2928 if (CONSP (prop)
2929 && EQ (XCAR (prop), Qheight)
2930 && CONSP (XCDR (prop)))
2931 {
2932 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2933 return 0;
2934
2935 /* `(height HEIGHT)'. */
2936 it->font_height = XCAR (XCDR (prop));
2937 if (!NILP (it->font_height))
2938 {
2939 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2940 int new_height = -1;
2941
2942 if (CONSP (it->font_height)
2943 && (EQ (XCAR (it->font_height), Qplus)
2944 || EQ (XCAR (it->font_height), Qminus))
2945 && CONSP (XCDR (it->font_height))
2946 && INTEGERP (XCAR (XCDR (it->font_height))))
2947 {
2948 /* `(+ N)' or `(- N)' where N is an integer. */
2949 int steps = XINT (XCAR (XCDR (it->font_height)));
2950 if (EQ (XCAR (it->font_height), Qplus))
2951 steps = - steps;
2952 it->face_id = smaller_face (it->f, it->face_id, steps);
2953 }
2954 else if (FUNCTIONP (it->font_height))
2955 {
2956 /* Call function with current height as argument.
2957 Value is the new height. */
2958 Lisp_Object height;
2959 height = safe_call1 (it->font_height,
2960 face->lface[LFACE_HEIGHT_INDEX]);
2961 if (NUMBERP (height))
2962 new_height = XFLOATINT (height);
2963 }
2964 else if (NUMBERP (it->font_height))
2965 {
2966 /* Value is a multiple of the canonical char height. */
2967 struct face *face;
2968
2969 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
2970 new_height = (XFLOATINT (it->font_height)
2971 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
2972 }
2973 else
2974 {
2975 /* Evaluate IT->font_height with `height' bound to the
2976 current specified height to get the new height. */
2977 Lisp_Object value;
2978 int count = SPECPDL_INDEX ();
2979
2980 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
2981 value = safe_eval (it->font_height);
2982 unbind_to (count, Qnil);
2983
2984 if (NUMBERP (value))
2985 new_height = XFLOATINT (value);
2986 }
2987
2988 if (new_height > 0)
2989 it->face_id = face_with_height (it->f, it->face_id, new_height);
2990 }
2991 }
2992 else if (CONSP (prop)
2993 && EQ (XCAR (prop), Qspace_width)
2994 && CONSP (XCDR (prop)))
2995 {
2996 /* `(space_width WIDTH)'. */
2997 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2998 return 0;
2999
3000 value = XCAR (XCDR (prop));
3001 if (NUMBERP (value) && XFLOATINT (value) > 0)
3002 it->space_width = value;
3003 }
3004 else if (CONSP (prop)
3005 && EQ (XCAR (prop), Qraise)
3006 && CONSP (XCDR (prop)))
3007 {
3008 /* `(raise FACTOR)'. */
3009 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3010 return 0;
3011
3012 #ifdef HAVE_WINDOW_SYSTEM
3013 value = XCAR (XCDR (prop));
3014 if (NUMBERP (value))
3015 {
3016 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3017 it->voffset = - (XFLOATINT (value)
3018 * (FONT_HEIGHT (face->font)));
3019 }
3020 #endif /* HAVE_WINDOW_SYSTEM */
3021 }
3022 else if (!it->string_from_display_prop_p)
3023 {
3024 /* `((margin left-margin) VALUE)' or `((margin right-margin)
3025 VALUE) or `((margin nil) VALUE)' or VALUE. */
3026 Lisp_Object location, value;
3027 struct text_pos start_pos;
3028 int valid_p;
3029
3030 /* Characters having this form of property are not displayed, so
3031 we have to find the end of the property. */
3032 start_pos = *position;
3033 *position = display_prop_end (it, object, start_pos);
3034 value = Qnil;
3035
3036 /* Let's stop at the new position and assume that all
3037 text properties change there. */
3038 it->stop_charpos = position->charpos;
3039
3040 location = Qunbound;
3041 if (CONSP (prop) && CONSP (XCAR (prop)))
3042 {
3043 Lisp_Object tem;
3044
3045 value = XCDR (prop);
3046 if (CONSP (value))
3047 value = XCAR (value);
3048
3049 tem = XCAR (prop);
3050 if (EQ (XCAR (tem), Qmargin)
3051 && (tem = XCDR (tem),
3052 tem = CONSP (tem) ? XCAR (tem) : Qnil,
3053 (NILP (tem)
3054 || EQ (tem, Qleft_margin)
3055 || EQ (tem, Qright_margin))))
3056 location = tem;
3057 }
3058
3059 if (EQ (location, Qunbound))
3060 {
3061 location = Qnil;
3062 value = prop;
3063 }
3064
3065 #ifdef HAVE_WINDOW_SYSTEM
3066 if (FRAME_TERMCAP_P (it->f))
3067 valid_p = STRINGP (value);
3068 else
3069 valid_p = (STRINGP (value)
3070 || (CONSP (value) && EQ (XCAR (value), Qspace))
3071 || valid_image_p (value));
3072 #else /* not HAVE_WINDOW_SYSTEM */
3073 valid_p = STRINGP (value);
3074 #endif /* not HAVE_WINDOW_SYSTEM */
3075
3076 if ((EQ (location, Qleft_margin)
3077 || EQ (location, Qright_margin)
3078 || NILP (location))
3079 && valid_p
3080 && !display_replaced_before_p)
3081 {
3082 replaces_text_display_p = 1;
3083
3084 /* Save current settings of IT so that we can restore them
3085 when we are finished with the glyph property value. */
3086 push_it (it);
3087
3088 if (NILP (location))
3089 it->area = TEXT_AREA;
3090 else if (EQ (location, Qleft_margin))
3091 it->area = LEFT_MARGIN_AREA;
3092 else
3093 it->area = RIGHT_MARGIN_AREA;
3094
3095 if (STRINGP (value))
3096 {
3097 it->string = value;
3098 it->multibyte_p = STRING_MULTIBYTE (it->string);
3099 it->current.overlay_string_index = -1;
3100 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3101 it->end_charpos = it->string_nchars = SCHARS (it->string);
3102 it->method = next_element_from_string;
3103 it->stop_charpos = 0;
3104 it->string_from_display_prop_p = 1;
3105 /* Say that we haven't consumed the characters with
3106 `display' property yet. The call to pop_it in
3107 set_iterator_to_next will clean this up. */
3108 *position = start_pos;
3109 }
3110 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3111 {
3112 it->method = next_element_from_stretch;
3113 it->object = value;
3114 it->current.pos = it->position = start_pos;
3115 }
3116 #ifdef HAVE_WINDOW_SYSTEM
3117 else
3118 {
3119 it->what = IT_IMAGE;
3120 it->image_id = lookup_image (it->f, value);
3121 it->position = start_pos;
3122 it->object = NILP (object) ? it->w->buffer : object;
3123 it->method = next_element_from_image;
3124
3125 /* Say that we haven't consumed the characters with
3126 `display' property yet. The call to pop_it in
3127 set_iterator_to_next will clean this up. */
3128 *position = start_pos;
3129 }
3130 #endif /* HAVE_WINDOW_SYSTEM */
3131 }
3132 else
3133 /* Invalid property or property not supported. Restore
3134 the position to what it was before. */
3135 *position = start_pos;
3136 }
3137
3138 return replaces_text_display_p;
3139 }
3140
3141
3142 /* Check if PROP is a display sub-property value whose text should be
3143 treated as intangible. */
3144
3145 static int
3146 single_display_prop_intangible_p (prop)
3147 Lisp_Object prop;
3148 {
3149 /* Skip over `when FORM'. */
3150 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3151 {
3152 prop = XCDR (prop);
3153 if (!CONSP (prop))
3154 return 0;
3155 prop = XCDR (prop);
3156 }
3157
3158 if (STRINGP (prop))
3159 return 1;
3160
3161 if (!CONSP (prop))
3162 return 0;
3163
3164 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3165 we don't need to treat text as intangible. */
3166 if (EQ (XCAR (prop), Qmargin))
3167 {
3168 prop = XCDR (prop);
3169 if (!CONSP (prop))
3170 return 0;
3171
3172 prop = XCDR (prop);
3173 if (!CONSP (prop)
3174 || EQ (XCAR (prop), Qleft_margin)
3175 || EQ (XCAR (prop), Qright_margin))
3176 return 0;
3177 }
3178
3179 return CONSP (prop) && EQ (XCAR (prop), Qimage);
3180 }
3181
3182
3183 /* Check if PROP is a display property value whose text should be
3184 treated as intangible. */
3185
3186 int
3187 display_prop_intangible_p (prop)
3188 Lisp_Object prop;
3189 {
3190 if (CONSP (prop)
3191 && CONSP (XCAR (prop))
3192 && !EQ (Qmargin, XCAR (XCAR (prop))))
3193 {
3194 /* A list of sub-properties. */
3195 while (CONSP (prop))
3196 {
3197 if (single_display_prop_intangible_p (XCAR (prop)))
3198 return 1;
3199 prop = XCDR (prop);
3200 }
3201 }
3202 else if (VECTORP (prop))
3203 {
3204 /* A vector of sub-properties. */
3205 int i;
3206 for (i = 0; i < ASIZE (prop); ++i)
3207 if (single_display_prop_intangible_p (AREF (prop, i)))
3208 return 1;
3209 }
3210 else
3211 return single_display_prop_intangible_p (prop);
3212
3213 return 0;
3214 }
3215
3216
3217 /* Return 1 if PROP is a display sub-property value containing STRING. */
3218
3219 static int
3220 single_display_prop_string_p (prop, string)
3221 Lisp_Object prop, string;
3222 {
3223 if (EQ (string, prop))
3224 return 1;
3225
3226 /* Skip over `when FORM'. */
3227 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3228 {
3229 prop = XCDR (prop);
3230 if (!CONSP (prop))
3231 return 0;
3232 prop = XCDR (prop);
3233 }
3234
3235 if (CONSP (prop))
3236 /* Skip over `margin LOCATION'. */
3237 if (EQ (XCAR (prop), Qmargin))
3238 {
3239 prop = XCDR (prop);
3240 if (!CONSP (prop))
3241 return 0;
3242
3243 prop = XCDR (prop);
3244 if (!CONSP (prop))
3245 return 0;
3246 }
3247
3248 return CONSP (prop) && EQ (XCAR (prop), string);
3249 }
3250
3251
3252 /* Return 1 if STRING appears in the `display' property PROP. */
3253
3254 static int
3255 display_prop_string_p (prop, string)
3256 Lisp_Object prop, string;
3257 {
3258 if (CONSP (prop)
3259 && CONSP (XCAR (prop))
3260 && !EQ (Qmargin, XCAR (XCAR (prop))))
3261 {
3262 /* A list of sub-properties. */
3263 while (CONSP (prop))
3264 {
3265 if (single_display_prop_string_p (XCAR (prop), string))
3266 return 1;
3267 prop = XCDR (prop);
3268 }
3269 }
3270 else if (VECTORP (prop))
3271 {
3272 /* A vector of sub-properties. */
3273 int i;
3274 for (i = 0; i < ASIZE (prop); ++i)
3275 if (single_display_prop_string_p (AREF (prop, i), string))
3276 return 1;
3277 }
3278 else
3279 return single_display_prop_string_p (prop, string);
3280
3281 return 0;
3282 }
3283
3284
3285 /* Determine from which buffer position in W's buffer STRING comes
3286 from. AROUND_CHARPOS is an approximate position where it could
3287 be from. Value is the buffer position or 0 if it couldn't be
3288 determined.
3289
3290 W's buffer must be current.
3291
3292 This function is necessary because we don't record buffer positions
3293 in glyphs generated from strings (to keep struct glyph small).
3294 This function may only use code that doesn't eval because it is
3295 called asynchronously from note_mouse_highlight. */
3296
3297 int
3298 string_buffer_position (w, string, around_charpos)
3299 struct window *w;
3300 Lisp_Object string;
3301 int around_charpos;
3302 {
3303 Lisp_Object limit, prop, pos;
3304 const int MAX_DISTANCE = 1000;
3305 int found = 0;
3306
3307 pos = make_number (around_charpos);
3308 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
3309 while (!found && !EQ (pos, limit))
3310 {
3311 prop = Fget_char_property (pos, Qdisplay, Qnil);
3312 if (!NILP (prop) && display_prop_string_p (prop, string))
3313 found = 1;
3314 else
3315 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
3316 }
3317
3318 if (!found)
3319 {
3320 pos = make_number (around_charpos);
3321 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
3322 while (!found && !EQ (pos, limit))
3323 {
3324 prop = Fget_char_property (pos, Qdisplay, Qnil);
3325 if (!NILP (prop) && display_prop_string_p (prop, string))
3326 found = 1;
3327 else
3328 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
3329 limit);
3330 }
3331 }
3332
3333 return found ? XINT (pos) : 0;
3334 }
3335
3336
3337 \f
3338 /***********************************************************************
3339 `composition' property
3340 ***********************************************************************/
3341
3342 /* Set up iterator IT from `composition' property at its current
3343 position. Called from handle_stop. */
3344
3345 static enum prop_handled
3346 handle_composition_prop (it)
3347 struct it *it;
3348 {
3349 Lisp_Object prop, string;
3350 int pos, pos_byte, end;
3351 enum prop_handled handled = HANDLED_NORMALLY;
3352
3353 if (STRINGP (it->string))
3354 {
3355 pos = IT_STRING_CHARPOS (*it);
3356 pos_byte = IT_STRING_BYTEPOS (*it);
3357 string = it->string;
3358 }
3359 else
3360 {
3361 pos = IT_CHARPOS (*it);
3362 pos_byte = IT_BYTEPOS (*it);
3363 string = Qnil;
3364 }
3365
3366 /* If there's a valid composition and point is not inside of the
3367 composition (in the case that the composition is from the current
3368 buffer), draw a glyph composed from the composition components. */
3369 if (find_composition (pos, -1, &pos, &end, &prop, string)
3370 && COMPOSITION_VALID_P (pos, end, prop)
3371 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
3372 {
3373 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
3374
3375 if (id >= 0)
3376 {
3377 it->method = next_element_from_composition;
3378 it->cmp_id = id;
3379 it->cmp_len = COMPOSITION_LENGTH (prop);
3380 /* For a terminal, draw only the first character of the
3381 components. */
3382 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
3383 it->len = (STRINGP (it->string)
3384 ? string_char_to_byte (it->string, end)
3385 : CHAR_TO_BYTE (end)) - pos_byte;
3386 it->stop_charpos = end;
3387 handled = HANDLED_RETURN;
3388 }
3389 }
3390
3391 return handled;
3392 }
3393
3394
3395 \f
3396 /***********************************************************************
3397 Overlay strings
3398 ***********************************************************************/
3399
3400 /* The following structure is used to record overlay strings for
3401 later sorting in load_overlay_strings. */
3402
3403 struct overlay_entry
3404 {
3405 Lisp_Object overlay;
3406 Lisp_Object string;
3407 int priority;
3408 int after_string_p;
3409 };
3410
3411
3412 /* Set up iterator IT from overlay strings at its current position.
3413 Called from handle_stop. */
3414
3415 static enum prop_handled
3416 handle_overlay_change (it)
3417 struct it *it;
3418 {
3419 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
3420 return HANDLED_RECOMPUTE_PROPS;
3421 else
3422 return HANDLED_NORMALLY;
3423 }
3424
3425
3426 /* Set up the next overlay string for delivery by IT, if there is an
3427 overlay string to deliver. Called by set_iterator_to_next when the
3428 end of the current overlay string is reached. If there are more
3429 overlay strings to display, IT->string and
3430 IT->current.overlay_string_index are set appropriately here.
3431 Otherwise IT->string is set to nil. */
3432
3433 static void
3434 next_overlay_string (it)
3435 struct it *it;
3436 {
3437 ++it->current.overlay_string_index;
3438 if (it->current.overlay_string_index == it->n_overlay_strings)
3439 {
3440 /* No more overlay strings. Restore IT's settings to what
3441 they were before overlay strings were processed, and
3442 continue to deliver from current_buffer. */
3443 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
3444
3445 pop_it (it);
3446 xassert (it->stop_charpos >= BEGV
3447 && it->stop_charpos <= it->end_charpos);
3448 it->string = Qnil;
3449 it->current.overlay_string_index = -1;
3450 SET_TEXT_POS (it->current.string_pos, -1, -1);
3451 it->n_overlay_strings = 0;
3452 it->method = next_element_from_buffer;
3453
3454 /* If we're at the end of the buffer, record that we have
3455 processed the overlay strings there already, so that
3456 next_element_from_buffer doesn't try it again. */
3457 if (IT_CHARPOS (*it) >= it->end_charpos)
3458 it->overlay_strings_at_end_processed_p = 1;
3459
3460 /* If we have to display `...' for invisible text, set
3461 the iterator up for that. */
3462 if (display_ellipsis_p)
3463 setup_for_ellipsis (it);
3464 }
3465 else
3466 {
3467 /* There are more overlay strings to process. If
3468 IT->current.overlay_string_index has advanced to a position
3469 where we must load IT->overlay_strings with more strings, do
3470 it. */
3471 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
3472
3473 if (it->current.overlay_string_index && i == 0)
3474 load_overlay_strings (it, 0);
3475
3476 /* Initialize IT to deliver display elements from the overlay
3477 string. */
3478 it->string = it->overlay_strings[i];
3479 it->multibyte_p = STRING_MULTIBYTE (it->string);
3480 SET_TEXT_POS (it->current.string_pos, 0, 0);
3481 it->method = next_element_from_string;
3482 it->stop_charpos = 0;
3483 }
3484
3485 CHECK_IT (it);
3486 }
3487
3488
3489 /* Compare two overlay_entry structures E1 and E2. Used as a
3490 comparison function for qsort in load_overlay_strings. Overlay
3491 strings for the same position are sorted so that
3492
3493 1. All after-strings come in front of before-strings, except
3494 when they come from the same overlay.
3495
3496 2. Within after-strings, strings are sorted so that overlay strings
3497 from overlays with higher priorities come first.
3498
3499 2. Within before-strings, strings are sorted so that overlay
3500 strings from overlays with higher priorities come last.
3501
3502 Value is analogous to strcmp. */
3503
3504
3505 static int
3506 compare_overlay_entries (e1, e2)
3507 void *e1, *e2;
3508 {
3509 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
3510 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
3511 int result;
3512
3513 if (entry1->after_string_p != entry2->after_string_p)
3514 {
3515 /* Let after-strings appear in front of before-strings if
3516 they come from different overlays. */
3517 if (EQ (entry1->overlay, entry2->overlay))
3518 result = entry1->after_string_p ? 1 : -1;
3519 else
3520 result = entry1->after_string_p ? -1 : 1;
3521 }
3522 else if (entry1->after_string_p)
3523 /* After-strings sorted in order of decreasing priority. */
3524 result = entry2->priority - entry1->priority;
3525 else
3526 /* Before-strings sorted in order of increasing priority. */
3527 result = entry1->priority - entry2->priority;
3528
3529 return result;
3530 }
3531
3532
3533 /* Load the vector IT->overlay_strings with overlay strings from IT's
3534 current buffer position, or from CHARPOS if that is > 0. Set
3535 IT->n_overlays to the total number of overlay strings found.
3536
3537 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
3538 a time. On entry into load_overlay_strings,
3539 IT->current.overlay_string_index gives the number of overlay
3540 strings that have already been loaded by previous calls to this
3541 function.
3542
3543 IT->add_overlay_start contains an additional overlay start
3544 position to consider for taking overlay strings from, if non-zero.
3545 This position comes into play when the overlay has an `invisible'
3546 property, and both before and after-strings. When we've skipped to
3547 the end of the overlay, because of its `invisible' property, we
3548 nevertheless want its before-string to appear.
3549 IT->add_overlay_start will contain the overlay start position
3550 in this case.
3551
3552 Overlay strings are sorted so that after-string strings come in
3553 front of before-string strings. Within before and after-strings,
3554 strings are sorted by overlay priority. See also function
3555 compare_overlay_entries. */
3556
3557 static void
3558 load_overlay_strings (it, charpos)
3559 struct it *it;
3560 int charpos;
3561 {
3562 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
3563 Lisp_Object ov, overlay, window, str, invisible;
3564 int start, end;
3565 int size = 20;
3566 int n = 0, i, j, invis_p;
3567 struct overlay_entry *entries
3568 = (struct overlay_entry *) alloca (size * sizeof *entries);
3569
3570 if (charpos <= 0)
3571 charpos = IT_CHARPOS (*it);
3572
3573 /* Append the overlay string STRING of overlay OVERLAY to vector
3574 `entries' which has size `size' and currently contains `n'
3575 elements. AFTER_P non-zero means STRING is an after-string of
3576 OVERLAY. */
3577 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
3578 do \
3579 { \
3580 Lisp_Object priority; \
3581 \
3582 if (n == size) \
3583 { \
3584 int new_size = 2 * size; \
3585 struct overlay_entry *old = entries; \
3586 entries = \
3587 (struct overlay_entry *) alloca (new_size \
3588 * sizeof *entries); \
3589 bcopy (old, entries, size * sizeof *entries); \
3590 size = new_size; \
3591 } \
3592 \
3593 entries[n].string = (STRING); \
3594 entries[n].overlay = (OVERLAY); \
3595 priority = Foverlay_get ((OVERLAY), Qpriority); \
3596 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
3597 entries[n].after_string_p = (AFTER_P); \
3598 ++n; \
3599 } \
3600 while (0)
3601
3602 /* Process overlay before the overlay center. */
3603 for (ov = current_buffer->overlays_before; CONSP (ov); ov = XCDR (ov))
3604 {
3605 overlay = XCAR (ov);
3606 xassert (OVERLAYP (overlay));
3607 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3608 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3609
3610 if (end < charpos)
3611 break;
3612
3613 /* Skip this overlay if it doesn't start or end at IT's current
3614 position. */
3615 if (end != charpos && start != charpos)
3616 continue;
3617
3618 /* Skip this overlay if it doesn't apply to IT->w. */
3619 window = Foverlay_get (overlay, Qwindow);
3620 if (WINDOWP (window) && XWINDOW (window) != it->w)
3621 continue;
3622
3623 /* If the text ``under'' the overlay is invisible, both before-
3624 and after-strings from this overlay are visible; start and
3625 end position are indistinguishable. */
3626 invisible = Foverlay_get (overlay, Qinvisible);
3627 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3628
3629 /* If overlay has a non-empty before-string, record it. */
3630 if ((start == charpos || (end == charpos && invis_p))
3631 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3632 && SCHARS (str))
3633 RECORD_OVERLAY_STRING (overlay, str, 0);
3634
3635 /* If overlay has a non-empty after-string, record it. */
3636 if ((end == charpos || (start == charpos && invis_p))
3637 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3638 && SCHARS (str))
3639 RECORD_OVERLAY_STRING (overlay, str, 1);
3640 }
3641
3642 /* Process overlays after the overlay center. */
3643 for (ov = current_buffer->overlays_after; CONSP (ov); ov = XCDR (ov))
3644 {
3645 overlay = XCAR (ov);
3646 xassert (OVERLAYP (overlay));
3647 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3648 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3649
3650 if (start > charpos)
3651 break;
3652
3653 /* Skip this overlay if it doesn't start or end at IT's current
3654 position. */
3655 if (end != charpos && start != charpos)
3656 continue;
3657
3658 /* Skip this overlay if it doesn't apply to IT->w. */
3659 window = Foverlay_get (overlay, Qwindow);
3660 if (WINDOWP (window) && XWINDOW (window) != it->w)
3661 continue;
3662
3663 /* If the text ``under'' the overlay is invisible, it has a zero
3664 dimension, and both before- and after-strings apply. */
3665 invisible = Foverlay_get (overlay, Qinvisible);
3666 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3667
3668 /* If overlay has a non-empty before-string, record it. */
3669 if ((start == charpos || (end == charpos && invis_p))
3670 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3671 && SCHARS (str))
3672 RECORD_OVERLAY_STRING (overlay, str, 0);
3673
3674 /* If overlay has a non-empty after-string, record it. */
3675 if ((end == charpos || (start == charpos && invis_p))
3676 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3677 && SCHARS (str))
3678 RECORD_OVERLAY_STRING (overlay, str, 1);
3679 }
3680
3681 #undef RECORD_OVERLAY_STRING
3682
3683 /* Sort entries. */
3684 if (n > 1)
3685 qsort (entries, n, sizeof *entries, compare_overlay_entries);
3686
3687 /* Record the total number of strings to process. */
3688 it->n_overlay_strings = n;
3689
3690 /* IT->current.overlay_string_index is the number of overlay strings
3691 that have already been consumed by IT. Copy some of the
3692 remaining overlay strings to IT->overlay_strings. */
3693 i = 0;
3694 j = it->current.overlay_string_index;
3695 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
3696 it->overlay_strings[i++] = entries[j++].string;
3697
3698 CHECK_IT (it);
3699 }
3700
3701
3702 /* Get the first chunk of overlay strings at IT's current buffer
3703 position, or at CHARPOS if that is > 0. Value is non-zero if at
3704 least one overlay string was found. */
3705
3706 static int
3707 get_overlay_strings (it, charpos)
3708 struct it *it;
3709 int charpos;
3710 {
3711 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
3712 process. This fills IT->overlay_strings with strings, and sets
3713 IT->n_overlay_strings to the total number of strings to process.
3714 IT->pos.overlay_string_index has to be set temporarily to zero
3715 because load_overlay_strings needs this; it must be set to -1
3716 when no overlay strings are found because a zero value would
3717 indicate a position in the first overlay string. */
3718 it->current.overlay_string_index = 0;
3719 load_overlay_strings (it, charpos);
3720
3721 /* If we found overlay strings, set up IT to deliver display
3722 elements from the first one. Otherwise set up IT to deliver
3723 from current_buffer. */
3724 if (it->n_overlay_strings)
3725 {
3726 /* Make sure we know settings in current_buffer, so that we can
3727 restore meaningful values when we're done with the overlay
3728 strings. */
3729 compute_stop_pos (it);
3730 xassert (it->face_id >= 0);
3731
3732 /* Save IT's settings. They are restored after all overlay
3733 strings have been processed. */
3734 xassert (it->sp == 0);
3735 push_it (it);
3736
3737 /* Set up IT to deliver display elements from the first overlay
3738 string. */
3739 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3740 it->string = it->overlay_strings[0];
3741 it->stop_charpos = 0;
3742 xassert (STRINGP (it->string));
3743 it->end_charpos = SCHARS (it->string);
3744 it->multibyte_p = STRING_MULTIBYTE (it->string);
3745 it->method = next_element_from_string;
3746 }
3747 else
3748 {
3749 it->string = Qnil;
3750 it->current.overlay_string_index = -1;
3751 it->method = next_element_from_buffer;
3752 }
3753
3754 CHECK_IT (it);
3755
3756 /* Value is non-zero if we found at least one overlay string. */
3757 return STRINGP (it->string);
3758 }
3759
3760
3761 \f
3762 /***********************************************************************
3763 Saving and restoring state
3764 ***********************************************************************/
3765
3766 /* Save current settings of IT on IT->stack. Called, for example,
3767 before setting up IT for an overlay string, to be able to restore
3768 IT's settings to what they were after the overlay string has been
3769 processed. */
3770
3771 static void
3772 push_it (it)
3773 struct it *it;
3774 {
3775 struct iterator_stack_entry *p;
3776
3777 xassert (it->sp < 2);
3778 p = it->stack + it->sp;
3779
3780 p->stop_charpos = it->stop_charpos;
3781 xassert (it->face_id >= 0);
3782 p->face_id = it->face_id;
3783 p->string = it->string;
3784 p->pos = it->current;
3785 p->end_charpos = it->end_charpos;
3786 p->string_nchars = it->string_nchars;
3787 p->area = it->area;
3788 p->multibyte_p = it->multibyte_p;
3789 p->space_width = it->space_width;
3790 p->font_height = it->font_height;
3791 p->voffset = it->voffset;
3792 p->string_from_display_prop_p = it->string_from_display_prop_p;
3793 p->display_ellipsis_p = 0;
3794 ++it->sp;
3795 }
3796
3797
3798 /* Restore IT's settings from IT->stack. Called, for example, when no
3799 more overlay strings must be processed, and we return to delivering
3800 display elements from a buffer, or when the end of a string from a
3801 `display' property is reached and we return to delivering display
3802 elements from an overlay string, or from a buffer. */
3803
3804 static void
3805 pop_it (it)
3806 struct it *it;
3807 {
3808 struct iterator_stack_entry *p;
3809
3810 xassert (it->sp > 0);
3811 --it->sp;
3812 p = it->stack + it->sp;
3813 it->stop_charpos = p->stop_charpos;
3814 it->face_id = p->face_id;
3815 it->string = p->string;
3816 it->current = p->pos;
3817 it->end_charpos = p->end_charpos;
3818 it->string_nchars = p->string_nchars;
3819 it->area = p->area;
3820 it->multibyte_p = p->multibyte_p;
3821 it->space_width = p->space_width;
3822 it->font_height = p->font_height;
3823 it->voffset = p->voffset;
3824 it->string_from_display_prop_p = p->string_from_display_prop_p;
3825 }
3826
3827
3828 \f
3829 /***********************************************************************
3830 Moving over lines
3831 ***********************************************************************/
3832
3833 /* Set IT's current position to the previous line start. */
3834
3835 static void
3836 back_to_previous_line_start (it)
3837 struct it *it;
3838 {
3839 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
3840 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3841 }
3842
3843
3844 /* Move IT to the next line start.
3845
3846 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
3847 we skipped over part of the text (as opposed to moving the iterator
3848 continuously over the text). Otherwise, don't change the value
3849 of *SKIPPED_P.
3850
3851 Newlines may come from buffer text, overlay strings, or strings
3852 displayed via the `display' property. That's the reason we can't
3853 simply use find_next_newline_no_quit.
3854
3855 Note that this function may not skip over invisible text that is so
3856 because of text properties and immediately follows a newline. If
3857 it would, function reseat_at_next_visible_line_start, when called
3858 from set_iterator_to_next, would effectively make invisible
3859 characters following a newline part of the wrong glyph row, which
3860 leads to wrong cursor motion. */
3861
3862 static int
3863 forward_to_next_line_start (it, skipped_p)
3864 struct it *it;
3865 int *skipped_p;
3866 {
3867 int old_selective, newline_found_p, n;
3868 const int MAX_NEWLINE_DISTANCE = 500;
3869
3870 /* If already on a newline, just consume it to avoid unintended
3871 skipping over invisible text below. */
3872 if (it->what == IT_CHARACTER
3873 && it->c == '\n'
3874 && CHARPOS (it->position) == IT_CHARPOS (*it))
3875 {
3876 set_iterator_to_next (it, 0);
3877 it->c = 0;
3878 return 1;
3879 }
3880
3881 /* Don't handle selective display in the following. It's (a)
3882 unnecessary because it's done by the caller, and (b) leads to an
3883 infinite recursion because next_element_from_ellipsis indirectly
3884 calls this function. */
3885 old_selective = it->selective;
3886 it->selective = 0;
3887
3888 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
3889 from buffer text. */
3890 for (n = newline_found_p = 0;
3891 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
3892 n += STRINGP (it->string) ? 0 : 1)
3893 {
3894 if (!get_next_display_element (it))
3895 return 0;
3896 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
3897 set_iterator_to_next (it, 0);
3898 }
3899
3900 /* If we didn't find a newline near enough, see if we can use a
3901 short-cut. */
3902 if (!newline_found_p)
3903 {
3904 int start = IT_CHARPOS (*it);
3905 int limit = find_next_newline_no_quit (start, 1);
3906 Lisp_Object pos;
3907
3908 xassert (!STRINGP (it->string));
3909
3910 /* If there isn't any `display' property in sight, and no
3911 overlays, we can just use the position of the newline in
3912 buffer text. */
3913 if (it->stop_charpos >= limit
3914 || ((pos = Fnext_single_property_change (make_number (start),
3915 Qdisplay,
3916 Qnil, make_number (limit)),
3917 NILP (pos))
3918 && next_overlay_change (start) == ZV))
3919 {
3920 IT_CHARPOS (*it) = limit;
3921 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
3922 *skipped_p = newline_found_p = 1;
3923 }
3924 else
3925 {
3926 while (get_next_display_element (it)
3927 && !newline_found_p)
3928 {
3929 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
3930 set_iterator_to_next (it, 0);
3931 }
3932 }
3933 }
3934
3935 it->selective = old_selective;
3936 return newline_found_p;
3937 }
3938
3939
3940 /* Set IT's current position to the previous visible line start. Skip
3941 invisible text that is so either due to text properties or due to
3942 selective display. Caution: this does not change IT->current_x and
3943 IT->hpos. */
3944
3945 static void
3946 back_to_previous_visible_line_start (it)
3947 struct it *it;
3948 {
3949 int visible_p = 0;
3950
3951 /* Go back one newline if not on BEGV already. */
3952 if (IT_CHARPOS (*it) > BEGV)
3953 back_to_previous_line_start (it);
3954
3955 /* Move over lines that are invisible because of selective display
3956 or text properties. */
3957 while (IT_CHARPOS (*it) > BEGV
3958 && !visible_p)
3959 {
3960 visible_p = 1;
3961
3962 /* If selective > 0, then lines indented more than that values
3963 are invisible. */
3964 if (it->selective > 0
3965 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3966 (double) it->selective)) /* iftc */
3967 visible_p = 0;
3968 else
3969 {
3970 Lisp_Object prop;
3971
3972 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
3973 Qinvisible, it->window);
3974 if (TEXT_PROP_MEANS_INVISIBLE (prop))
3975 visible_p = 0;
3976 }
3977
3978 /* Back one more newline if the current one is invisible. */
3979 if (!visible_p)
3980 back_to_previous_line_start (it);
3981 }
3982
3983 xassert (IT_CHARPOS (*it) >= BEGV);
3984 xassert (IT_CHARPOS (*it) == BEGV
3985 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3986 CHECK_IT (it);
3987 }
3988
3989
3990 /* Reseat iterator IT at the previous visible line start. Skip
3991 invisible text that is so either due to text properties or due to
3992 selective display. At the end, update IT's overlay information,
3993 face information etc. */
3994
3995 static void
3996 reseat_at_previous_visible_line_start (it)
3997 struct it *it;
3998 {
3999 back_to_previous_visible_line_start (it);
4000 reseat (it, it->current.pos, 1);
4001 CHECK_IT (it);
4002 }
4003
4004
4005 /* Reseat iterator IT on the next visible line start in the current
4006 buffer. ON_NEWLINE_P non-zero means position IT on the newline
4007 preceding the line start. Skip over invisible text that is so
4008 because of selective display. Compute faces, overlays etc at the
4009 new position. Note that this function does not skip over text that
4010 is invisible because of text properties. */
4011
4012 static void
4013 reseat_at_next_visible_line_start (it, on_newline_p)
4014 struct it *it;
4015 int on_newline_p;
4016 {
4017 int newline_found_p, skipped_p = 0;
4018
4019 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4020
4021 /* Skip over lines that are invisible because they are indented
4022 more than the value of IT->selective. */
4023 if (it->selective > 0)
4024 while (IT_CHARPOS (*it) < ZV
4025 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4026 (double) it->selective)) /* iftc */
4027 {
4028 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4029 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4030 }
4031
4032 /* Position on the newline if that's what's requested. */
4033 if (on_newline_p && newline_found_p)
4034 {
4035 if (STRINGP (it->string))
4036 {
4037 if (IT_STRING_CHARPOS (*it) > 0)
4038 {
4039 --IT_STRING_CHARPOS (*it);
4040 --IT_STRING_BYTEPOS (*it);
4041 }
4042 }
4043 else if (IT_CHARPOS (*it) > BEGV)
4044 {
4045 --IT_CHARPOS (*it);
4046 --IT_BYTEPOS (*it);
4047 reseat (it, it->current.pos, 0);
4048 }
4049 }
4050 else if (skipped_p)
4051 reseat (it, it->current.pos, 0);
4052
4053 CHECK_IT (it);
4054 }
4055
4056
4057 \f
4058 /***********************************************************************
4059 Changing an iterator's position
4060 ***********************************************************************/
4061
4062 /* Change IT's current position to POS in current_buffer. If FORCE_P
4063 is non-zero, always check for text properties at the new position.
4064 Otherwise, text properties are only looked up if POS >=
4065 IT->check_charpos of a property. */
4066
4067 static void
4068 reseat (it, pos, force_p)
4069 struct it *it;
4070 struct text_pos pos;
4071 int force_p;
4072 {
4073 int original_pos = IT_CHARPOS (*it);
4074
4075 reseat_1 (it, pos, 0);
4076
4077 /* Determine where to check text properties. Avoid doing it
4078 where possible because text property lookup is very expensive. */
4079 if (force_p
4080 || CHARPOS (pos) > it->stop_charpos
4081 || CHARPOS (pos) < original_pos)
4082 handle_stop (it);
4083
4084 CHECK_IT (it);
4085 }
4086
4087
4088 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4089 IT->stop_pos to POS, also. */
4090
4091 static void
4092 reseat_1 (it, pos, set_stop_p)
4093 struct it *it;
4094 struct text_pos pos;
4095 int set_stop_p;
4096 {
4097 /* Don't call this function when scanning a C string. */
4098 xassert (it->s == NULL);
4099
4100 /* POS must be a reasonable value. */
4101 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4102
4103 it->current.pos = it->position = pos;
4104 XSETBUFFER (it->object, current_buffer);
4105 it->end_charpos = ZV;
4106 it->dpvec = NULL;
4107 it->current.dpvec_index = -1;
4108 it->current.overlay_string_index = -1;
4109 IT_STRING_CHARPOS (*it) = -1;
4110 IT_STRING_BYTEPOS (*it) = -1;
4111 it->string = Qnil;
4112 it->method = next_element_from_buffer;
4113 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
4114 it->sp = 0;
4115 it->face_before_selective_p = 0;
4116
4117 if (set_stop_p)
4118 it->stop_charpos = CHARPOS (pos);
4119 }
4120
4121
4122 /* Set up IT for displaying a string, starting at CHARPOS in window W.
4123 If S is non-null, it is a C string to iterate over. Otherwise,
4124 STRING gives a Lisp string to iterate over.
4125
4126 If PRECISION > 0, don't return more then PRECISION number of
4127 characters from the string.
4128
4129 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4130 characters have been returned. FIELD_WIDTH < 0 means an infinite
4131 field width.
4132
4133 MULTIBYTE = 0 means disable processing of multibyte characters,
4134 MULTIBYTE > 0 means enable it,
4135 MULTIBYTE < 0 means use IT->multibyte_p.
4136
4137 IT must be initialized via a prior call to init_iterator before
4138 calling this function. */
4139
4140 static void
4141 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4142 struct it *it;
4143 unsigned char *s;
4144 Lisp_Object string;
4145 int charpos;
4146 int precision, field_width, multibyte;
4147 {
4148 /* No region in strings. */
4149 it->region_beg_charpos = it->region_end_charpos = -1;
4150
4151 /* No text property checks performed by default, but see below. */
4152 it->stop_charpos = -1;
4153
4154 /* Set iterator position and end position. */
4155 bzero (&it->current, sizeof it->current);
4156 it->current.overlay_string_index = -1;
4157 it->current.dpvec_index = -1;
4158 xassert (charpos >= 0);
4159
4160 /* If STRING is specified, use its multibyteness, otherwise use the
4161 setting of MULTIBYTE, if specified. */
4162 if (multibyte >= 0)
4163 it->multibyte_p = multibyte > 0;
4164
4165 if (s == NULL)
4166 {
4167 xassert (STRINGP (string));
4168 it->string = string;
4169 it->s = NULL;
4170 it->end_charpos = it->string_nchars = SCHARS (string);
4171 it->method = next_element_from_string;
4172 it->current.string_pos = string_pos (charpos, string);
4173 }
4174 else
4175 {
4176 it->s = s;
4177 it->string = Qnil;
4178
4179 /* Note that we use IT->current.pos, not it->current.string_pos,
4180 for displaying C strings. */
4181 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4182 if (it->multibyte_p)
4183 {
4184 it->current.pos = c_string_pos (charpos, s, 1);
4185 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4186 }
4187 else
4188 {
4189 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4190 it->end_charpos = it->string_nchars = strlen (s);
4191 }
4192
4193 it->method = next_element_from_c_string;
4194 }
4195
4196 /* PRECISION > 0 means don't return more than PRECISION characters
4197 from the string. */
4198 if (precision > 0 && it->end_charpos - charpos > precision)
4199 it->end_charpos = it->string_nchars = charpos + precision;
4200
4201 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4202 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4203 FIELD_WIDTH < 0 means infinite field width. This is useful for
4204 padding with `-' at the end of a mode line. */
4205 if (field_width < 0)
4206 field_width = INFINITY;
4207 if (field_width > it->end_charpos - charpos)
4208 it->end_charpos = charpos + field_width;
4209
4210 /* Use the standard display table for displaying strings. */
4211 if (DISP_TABLE_P (Vstandard_display_table))
4212 it->dp = XCHAR_TABLE (Vstandard_display_table);
4213
4214 it->stop_charpos = charpos;
4215 CHECK_IT (it);
4216 }
4217
4218
4219 \f
4220 /***********************************************************************
4221 Iteration
4222 ***********************************************************************/
4223
4224 /* Load IT's display element fields with information about the next
4225 display element from the current position of IT. Value is zero if
4226 end of buffer (or C string) is reached. */
4227
4228 int
4229 get_next_display_element (it)
4230 struct it *it;
4231 {
4232 /* Non-zero means that we found a display element. Zero means that
4233 we hit the end of what we iterate over. Performance note: the
4234 function pointer `method' used here turns out to be faster than
4235 using a sequence of if-statements. */
4236 int success_p = (*it->method) (it);
4237
4238 if (it->what == IT_CHARACTER)
4239 {
4240 /* Map via display table or translate control characters.
4241 IT->c, IT->len etc. have been set to the next character by
4242 the function call above. If we have a display table, and it
4243 contains an entry for IT->c, translate it. Don't do this if
4244 IT->c itself comes from a display table, otherwise we could
4245 end up in an infinite recursion. (An alternative could be to
4246 count the recursion depth of this function and signal an
4247 error when a certain maximum depth is reached.) Is it worth
4248 it? */
4249 if (success_p && it->dpvec == NULL)
4250 {
4251 Lisp_Object dv;
4252
4253 if (it->dp
4254 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
4255 VECTORP (dv)))
4256 {
4257 struct Lisp_Vector *v = XVECTOR (dv);
4258
4259 /* Return the first character from the display table
4260 entry, if not empty. If empty, don't display the
4261 current character. */
4262 if (v->size)
4263 {
4264 it->dpvec_char_len = it->len;
4265 it->dpvec = v->contents;
4266 it->dpend = v->contents + v->size;
4267 it->current.dpvec_index = 0;
4268 it->method = next_element_from_display_vector;
4269 success_p = get_next_display_element (it);
4270 }
4271 else
4272 {
4273 set_iterator_to_next (it, 0);
4274 success_p = get_next_display_element (it);
4275 }
4276 }
4277
4278 /* Translate control characters into `\003' or `^C' form.
4279 Control characters coming from a display table entry are
4280 currently not translated because we use IT->dpvec to hold
4281 the translation. This could easily be changed but I
4282 don't believe that it is worth doing.
4283
4284 If it->multibyte_p is nonzero, eight-bit characters and
4285 non-printable multibyte characters are also translated to
4286 octal form.
4287
4288 If it->multibyte_p is zero, eight-bit characters that
4289 don't have corresponding multibyte char code are also
4290 translated to octal form. */
4291 else if ((it->c < ' '
4292 && (it->area != TEXT_AREA
4293 || (it->c != '\n' && it->c != '\t')))
4294 || (it->multibyte_p
4295 ? ((it->c >= 127
4296 && it->len == 1)
4297 || !CHAR_PRINTABLE_P (it->c))
4298 : (it->c >= 127
4299 && it->c == unibyte_char_to_multibyte (it->c))))
4300 {
4301 /* IT->c is a control character which must be displayed
4302 either as '\003' or as `^C' where the '\\' and '^'
4303 can be defined in the display table. Fill
4304 IT->ctl_chars with glyphs for what we have to
4305 display. Then, set IT->dpvec to these glyphs. */
4306 GLYPH g;
4307
4308 if (it->c < 128 && it->ctl_arrow_p)
4309 {
4310 /* Set IT->ctl_chars[0] to the glyph for `^'. */
4311 if (it->dp
4312 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
4313 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
4314 g = XINT (DISP_CTRL_GLYPH (it->dp));
4315 else
4316 g = FAST_MAKE_GLYPH ('^', 0);
4317 XSETINT (it->ctl_chars[0], g);
4318
4319 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
4320 XSETINT (it->ctl_chars[1], g);
4321
4322 /* Set up IT->dpvec and return first character from it. */
4323 it->dpvec_char_len = it->len;
4324 it->dpvec = it->ctl_chars;
4325 it->dpend = it->dpvec + 2;
4326 it->current.dpvec_index = 0;
4327 it->method = next_element_from_display_vector;
4328 get_next_display_element (it);
4329 }
4330 else
4331 {
4332 unsigned char str[MAX_MULTIBYTE_LENGTH];
4333 int len;
4334 int i;
4335 GLYPH escape_glyph;
4336
4337 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
4338 if (it->dp
4339 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
4340 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
4341 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
4342 else
4343 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
4344
4345 if (SINGLE_BYTE_CHAR_P (it->c))
4346 str[0] = it->c, len = 1;
4347 else
4348 {
4349 len = CHAR_STRING_NO_SIGNAL (it->c, str);
4350 if (len < 0)
4351 {
4352 /* It's an invalid character, which
4353 shouldn't happen actually, but due to
4354 bugs it may happen. Let's print the char
4355 as is, there's not much meaningful we can
4356 do with it. */
4357 str[0] = it->c;
4358 str[1] = it->c >> 8;
4359 str[2] = it->c >> 16;
4360 str[3] = it->c >> 24;
4361 len = 4;
4362 }
4363 }
4364
4365 for (i = 0; i < len; i++)
4366 {
4367 XSETINT (it->ctl_chars[i * 4], escape_glyph);
4368 /* Insert three more glyphs into IT->ctl_chars for
4369 the octal display of the character. */
4370 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
4371 XSETINT (it->ctl_chars[i * 4 + 1], g);
4372 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
4373 XSETINT (it->ctl_chars[i * 4 + 2], g);
4374 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
4375 XSETINT (it->ctl_chars[i * 4 + 3], g);
4376 }
4377
4378 /* Set up IT->dpvec and return the first character
4379 from it. */
4380 it->dpvec_char_len = it->len;
4381 it->dpvec = it->ctl_chars;
4382 it->dpend = it->dpvec + len * 4;
4383 it->current.dpvec_index = 0;
4384 it->method = next_element_from_display_vector;
4385 get_next_display_element (it);
4386 }
4387 }
4388 }
4389
4390 /* Adjust face id for a multibyte character. There are no
4391 multibyte character in unibyte text. */
4392 if (it->multibyte_p
4393 && success_p
4394 && FRAME_WINDOW_P (it->f))
4395 {
4396 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4397 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
4398 }
4399 }
4400
4401 /* Is this character the last one of a run of characters with
4402 box? If yes, set IT->end_of_box_run_p to 1. */
4403 if (it->face_box_p
4404 && it->s == NULL)
4405 {
4406 int face_id;
4407 struct face *face;
4408
4409 it->end_of_box_run_p
4410 = ((face_id = face_after_it_pos (it),
4411 face_id != it->face_id)
4412 && (face = FACE_FROM_ID (it->f, face_id),
4413 face->box == FACE_NO_BOX));
4414 }
4415
4416 /* Value is 0 if end of buffer or string reached. */
4417 return success_p;
4418 }
4419
4420
4421 /* Move IT to the next display element.
4422
4423 RESEAT_P non-zero means if called on a newline in buffer text,
4424 skip to the next visible line start.
4425
4426 Functions get_next_display_element and set_iterator_to_next are
4427 separate because I find this arrangement easier to handle than a
4428 get_next_display_element function that also increments IT's
4429 position. The way it is we can first look at an iterator's current
4430 display element, decide whether it fits on a line, and if it does,
4431 increment the iterator position. The other way around we probably
4432 would either need a flag indicating whether the iterator has to be
4433 incremented the next time, or we would have to implement a
4434 decrement position function which would not be easy to write. */
4435
4436 void
4437 set_iterator_to_next (it, reseat_p)
4438 struct it *it;
4439 int reseat_p;
4440 {
4441 /* Reset flags indicating start and end of a sequence of characters
4442 with box. Reset them at the start of this function because
4443 moving the iterator to a new position might set them. */
4444 it->start_of_box_run_p = it->end_of_box_run_p = 0;
4445
4446 if (it->method == next_element_from_buffer)
4447 {
4448 /* The current display element of IT is a character from
4449 current_buffer. Advance in the buffer, and maybe skip over
4450 invisible lines that are so because of selective display. */
4451 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
4452 reseat_at_next_visible_line_start (it, 0);
4453 else
4454 {
4455 xassert (it->len != 0);
4456 IT_BYTEPOS (*it) += it->len;
4457 IT_CHARPOS (*it) += 1;
4458 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
4459 }
4460 }
4461 else if (it->method == next_element_from_composition)
4462 {
4463 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
4464 if (STRINGP (it->string))
4465 {
4466 IT_STRING_BYTEPOS (*it) += it->len;
4467 IT_STRING_CHARPOS (*it) += it->cmp_len;
4468 it->method = next_element_from_string;
4469 goto consider_string_end;
4470 }
4471 else
4472 {
4473 IT_BYTEPOS (*it) += it->len;
4474 IT_CHARPOS (*it) += it->cmp_len;
4475 it->method = next_element_from_buffer;
4476 }
4477 }
4478 else if (it->method == next_element_from_c_string)
4479 {
4480 /* Current display element of IT is from a C string. */
4481 IT_BYTEPOS (*it) += it->len;
4482 IT_CHARPOS (*it) += 1;
4483 }
4484 else if (it->method == next_element_from_display_vector)
4485 {
4486 /* Current display element of IT is from a display table entry.
4487 Advance in the display table definition. Reset it to null if
4488 end reached, and continue with characters from buffers/
4489 strings. */
4490 ++it->current.dpvec_index;
4491
4492 /* Restore face of the iterator to what they were before the
4493 display vector entry (these entries may contain faces). */
4494 it->face_id = it->saved_face_id;
4495
4496 if (it->dpvec + it->current.dpvec_index == it->dpend)
4497 {
4498 if (it->s)
4499 it->method = next_element_from_c_string;
4500 else if (STRINGP (it->string))
4501 it->method = next_element_from_string;
4502 else
4503 it->method = next_element_from_buffer;
4504
4505 it->dpvec = NULL;
4506 it->current.dpvec_index = -1;
4507
4508 /* Skip over characters which were displayed via IT->dpvec. */
4509 if (it->dpvec_char_len < 0)
4510 reseat_at_next_visible_line_start (it, 1);
4511 else if (it->dpvec_char_len > 0)
4512 {
4513 it->len = it->dpvec_char_len;
4514 set_iterator_to_next (it, reseat_p);
4515 }
4516 }
4517 }
4518 else if (it->method == next_element_from_string)
4519 {
4520 /* Current display element is a character from a Lisp string. */
4521 xassert (it->s == NULL && STRINGP (it->string));
4522 IT_STRING_BYTEPOS (*it) += it->len;
4523 IT_STRING_CHARPOS (*it) += 1;
4524
4525 consider_string_end:
4526
4527 if (it->current.overlay_string_index >= 0)
4528 {
4529 /* IT->string is an overlay string. Advance to the
4530 next, if there is one. */
4531 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
4532 next_overlay_string (it);
4533 }
4534 else
4535 {
4536 /* IT->string is not an overlay string. If we reached
4537 its end, and there is something on IT->stack, proceed
4538 with what is on the stack. This can be either another
4539 string, this time an overlay string, or a buffer. */
4540 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
4541 && it->sp > 0)
4542 {
4543 pop_it (it);
4544 if (!STRINGP (it->string))
4545 it->method = next_element_from_buffer;
4546 else
4547 goto consider_string_end;
4548 }
4549 }
4550 }
4551 else if (it->method == next_element_from_image
4552 || it->method == next_element_from_stretch)
4553 {
4554 /* The position etc with which we have to proceed are on
4555 the stack. The position may be at the end of a string,
4556 if the `display' property takes up the whole string. */
4557 pop_it (it);
4558 it->image_id = 0;
4559 if (STRINGP (it->string))
4560 {
4561 it->method = next_element_from_string;
4562 goto consider_string_end;
4563 }
4564 else
4565 it->method = next_element_from_buffer;
4566 }
4567 else
4568 /* There are no other methods defined, so this should be a bug. */
4569 abort ();
4570
4571 xassert (it->method != next_element_from_string
4572 || (STRINGP (it->string)
4573 && IT_STRING_CHARPOS (*it) >= 0));
4574 }
4575
4576
4577 /* Load IT's display element fields with information about the next
4578 display element which comes from a display table entry or from the
4579 result of translating a control character to one of the forms `^C'
4580 or `\003'. IT->dpvec holds the glyphs to return as characters. */
4581
4582 static int
4583 next_element_from_display_vector (it)
4584 struct it *it;
4585 {
4586 /* Precondition. */
4587 xassert (it->dpvec && it->current.dpvec_index >= 0);
4588
4589 /* Remember the current face id in case glyphs specify faces.
4590 IT's face is restored in set_iterator_to_next. */
4591 it->saved_face_id = it->face_id;
4592
4593 if (INTEGERP (*it->dpvec)
4594 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
4595 {
4596 int lface_id;
4597 GLYPH g;
4598
4599 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
4600 it->c = FAST_GLYPH_CHAR (g);
4601 it->len = CHAR_BYTES (it->c);
4602
4603 /* The entry may contain a face id to use. Such a face id is
4604 the id of a Lisp face, not a realized face. A face id of
4605 zero means no face is specified. */
4606 lface_id = FAST_GLYPH_FACE (g);
4607 if (lface_id)
4608 {
4609 /* The function returns -1 if lface_id is invalid. */
4610 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
4611 if (face_id >= 0)
4612 it->face_id = face_id;
4613 }
4614 }
4615 else
4616 /* Display table entry is invalid. Return a space. */
4617 it->c = ' ', it->len = 1;
4618
4619 /* Don't change position and object of the iterator here. They are
4620 still the values of the character that had this display table
4621 entry or was translated, and that's what we want. */
4622 it->what = IT_CHARACTER;
4623 return 1;
4624 }
4625
4626
4627 /* Load IT with the next display element from Lisp string IT->string.
4628 IT->current.string_pos is the current position within the string.
4629 If IT->current.overlay_string_index >= 0, the Lisp string is an
4630 overlay string. */
4631
4632 static int
4633 next_element_from_string (it)
4634 struct it *it;
4635 {
4636 struct text_pos position;
4637
4638 xassert (STRINGP (it->string));
4639 xassert (IT_STRING_CHARPOS (*it) >= 0);
4640 position = it->current.string_pos;
4641
4642 /* Time to check for invisible text? */
4643 if (IT_STRING_CHARPOS (*it) < it->end_charpos
4644 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
4645 {
4646 handle_stop (it);
4647
4648 /* Since a handler may have changed IT->method, we must
4649 recurse here. */
4650 return get_next_display_element (it);
4651 }
4652
4653 if (it->current.overlay_string_index >= 0)
4654 {
4655 /* Get the next character from an overlay string. In overlay
4656 strings, There is no field width or padding with spaces to
4657 do. */
4658 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
4659 {
4660 it->what = IT_EOB;
4661 return 0;
4662 }
4663 else if (STRING_MULTIBYTE (it->string))
4664 {
4665 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
4666 const unsigned char *s = (SDATA (it->string)
4667 + IT_STRING_BYTEPOS (*it));
4668 it->c = string_char_and_length (s, remaining, &it->len);
4669 }
4670 else
4671 {
4672 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
4673 it->len = 1;
4674 }
4675 }
4676 else
4677 {
4678 /* Get the next character from a Lisp string that is not an
4679 overlay string. Such strings come from the mode line, for
4680 example. We may have to pad with spaces, or truncate the
4681 string. See also next_element_from_c_string. */
4682 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
4683 {
4684 it->what = IT_EOB;
4685 return 0;
4686 }
4687 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
4688 {
4689 /* Pad with spaces. */
4690 it->c = ' ', it->len = 1;
4691 CHARPOS (position) = BYTEPOS (position) = -1;
4692 }
4693 else if (STRING_MULTIBYTE (it->string))
4694 {
4695 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
4696 const unsigned char *s = (SDATA (it->string)
4697 + IT_STRING_BYTEPOS (*it));
4698 it->c = string_char_and_length (s, maxlen, &it->len);
4699 }
4700 else
4701 {
4702 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
4703 it->len = 1;
4704 }
4705 }
4706
4707 /* Record what we have and where it came from. Note that we store a
4708 buffer position in IT->position although it could arguably be a
4709 string position. */
4710 it->what = IT_CHARACTER;
4711 it->object = it->string;
4712 it->position = position;
4713 return 1;
4714 }
4715
4716
4717 /* Load IT with next display element from C string IT->s.
4718 IT->string_nchars is the maximum number of characters to return
4719 from the string. IT->end_charpos may be greater than
4720 IT->string_nchars when this function is called, in which case we
4721 may have to return padding spaces. Value is zero if end of string
4722 reached, including padding spaces. */
4723
4724 static int
4725 next_element_from_c_string (it)
4726 struct it *it;
4727 {
4728 int success_p = 1;
4729
4730 xassert (it->s);
4731 it->what = IT_CHARACTER;
4732 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
4733 it->object = Qnil;
4734
4735 /* IT's position can be greater IT->string_nchars in case a field
4736 width or precision has been specified when the iterator was
4737 initialized. */
4738 if (IT_CHARPOS (*it) >= it->end_charpos)
4739 {
4740 /* End of the game. */
4741 it->what = IT_EOB;
4742 success_p = 0;
4743 }
4744 else if (IT_CHARPOS (*it) >= it->string_nchars)
4745 {
4746 /* Pad with spaces. */
4747 it->c = ' ', it->len = 1;
4748 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
4749 }
4750 else if (it->multibyte_p)
4751 {
4752 /* Implementation note: The calls to strlen apparently aren't a
4753 performance problem because there is no noticeable performance
4754 difference between Emacs running in unibyte or multibyte mode. */
4755 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
4756 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
4757 maxlen, &it->len);
4758 }
4759 else
4760 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
4761
4762 return success_p;
4763 }
4764
4765
4766 /* Set up IT to return characters from an ellipsis, if appropriate.
4767 The definition of the ellipsis glyphs may come from a display table
4768 entry. This function Fills IT with the first glyph from the
4769 ellipsis if an ellipsis is to be displayed. */
4770
4771 static int
4772 next_element_from_ellipsis (it)
4773 struct it *it;
4774 {
4775 if (it->selective_display_ellipsis_p)
4776 {
4777 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4778 {
4779 /* Use the display table definition for `...'. Invalid glyphs
4780 will be handled by the method returning elements from dpvec. */
4781 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4782 it->dpvec_char_len = it->len;
4783 it->dpvec = v->contents;
4784 it->dpend = v->contents + v->size;
4785 it->current.dpvec_index = 0;
4786 it->method = next_element_from_display_vector;
4787 }
4788 else
4789 {
4790 /* Use default `...' which is stored in default_invis_vector. */
4791 it->dpvec_char_len = it->len;
4792 it->dpvec = default_invis_vector;
4793 it->dpend = default_invis_vector + 3;
4794 it->current.dpvec_index = 0;
4795 it->method = next_element_from_display_vector;
4796 }
4797 }
4798 else
4799 {
4800 /* The face at the current position may be different from the
4801 face we find after the invisible text. Remember what it
4802 was in IT->saved_face_id, and signal that it's there by
4803 setting face_before_selective_p. */
4804 it->saved_face_id = it->face_id;
4805 it->method = next_element_from_buffer;
4806 reseat_at_next_visible_line_start (it, 1);
4807 it->face_before_selective_p = 1;
4808 }
4809
4810 return get_next_display_element (it);
4811 }
4812
4813
4814 /* Deliver an image display element. The iterator IT is already
4815 filled with image information (done in handle_display_prop). Value
4816 is always 1. */
4817
4818
4819 static int
4820 next_element_from_image (it)
4821 struct it *it;
4822 {
4823 it->what = IT_IMAGE;
4824 return 1;
4825 }
4826
4827
4828 /* Fill iterator IT with next display element from a stretch glyph
4829 property. IT->object is the value of the text property. Value is
4830 always 1. */
4831
4832 static int
4833 next_element_from_stretch (it)
4834 struct it *it;
4835 {
4836 it->what = IT_STRETCH;
4837 return 1;
4838 }
4839
4840
4841 /* Load IT with the next display element from current_buffer. Value
4842 is zero if end of buffer reached. IT->stop_charpos is the next
4843 position at which to stop and check for text properties or buffer
4844 end. */
4845
4846 static int
4847 next_element_from_buffer (it)
4848 struct it *it;
4849 {
4850 int success_p = 1;
4851
4852 /* Check this assumption, otherwise, we would never enter the
4853 if-statement, below. */
4854 xassert (IT_CHARPOS (*it) >= BEGV
4855 && IT_CHARPOS (*it) <= it->stop_charpos);
4856
4857 if (IT_CHARPOS (*it) >= it->stop_charpos)
4858 {
4859 if (IT_CHARPOS (*it) >= it->end_charpos)
4860 {
4861 int overlay_strings_follow_p;
4862
4863 /* End of the game, except when overlay strings follow that
4864 haven't been returned yet. */
4865 if (it->overlay_strings_at_end_processed_p)
4866 overlay_strings_follow_p = 0;
4867 else
4868 {
4869 it->overlay_strings_at_end_processed_p = 1;
4870 overlay_strings_follow_p = get_overlay_strings (it, 0);
4871 }
4872
4873 if (overlay_strings_follow_p)
4874 success_p = get_next_display_element (it);
4875 else
4876 {
4877 it->what = IT_EOB;
4878 it->position = it->current.pos;
4879 success_p = 0;
4880 }
4881 }
4882 else
4883 {
4884 handle_stop (it);
4885 return get_next_display_element (it);
4886 }
4887 }
4888 else
4889 {
4890 /* No face changes, overlays etc. in sight, so just return a
4891 character from current_buffer. */
4892 unsigned char *p;
4893
4894 /* Maybe run the redisplay end trigger hook. Performance note:
4895 This doesn't seem to cost measurable time. */
4896 if (it->redisplay_end_trigger_charpos
4897 && it->glyph_row
4898 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
4899 run_redisplay_end_trigger_hook (it);
4900
4901 /* Get the next character, maybe multibyte. */
4902 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
4903 if (it->multibyte_p && !ASCII_BYTE_P (*p))
4904 {
4905 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
4906 - IT_BYTEPOS (*it));
4907 it->c = string_char_and_length (p, maxlen, &it->len);
4908 }
4909 else
4910 it->c = *p, it->len = 1;
4911
4912 /* Record what we have and where it came from. */
4913 it->what = IT_CHARACTER;;
4914 it->object = it->w->buffer;
4915 it->position = it->current.pos;
4916
4917 /* Normally we return the character found above, except when we
4918 really want to return an ellipsis for selective display. */
4919 if (it->selective)
4920 {
4921 if (it->c == '\n')
4922 {
4923 /* A value of selective > 0 means hide lines indented more
4924 than that number of columns. */
4925 if (it->selective > 0
4926 && IT_CHARPOS (*it) + 1 < ZV
4927 && indented_beyond_p (IT_CHARPOS (*it) + 1,
4928 IT_BYTEPOS (*it) + 1,
4929 (double) it->selective)) /* iftc */
4930 {
4931 success_p = next_element_from_ellipsis (it);
4932 it->dpvec_char_len = -1;
4933 }
4934 }
4935 else if (it->c == '\r' && it->selective == -1)
4936 {
4937 /* A value of selective == -1 means that everything from the
4938 CR to the end of the line is invisible, with maybe an
4939 ellipsis displayed for it. */
4940 success_p = next_element_from_ellipsis (it);
4941 it->dpvec_char_len = -1;
4942 }
4943 }
4944 }
4945
4946 /* Value is zero if end of buffer reached. */
4947 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
4948 return success_p;
4949 }
4950
4951
4952 /* Run the redisplay end trigger hook for IT. */
4953
4954 static void
4955 run_redisplay_end_trigger_hook (it)
4956 struct it *it;
4957 {
4958 Lisp_Object args[3];
4959
4960 /* IT->glyph_row should be non-null, i.e. we should be actually
4961 displaying something, or otherwise we should not run the hook. */
4962 xassert (it->glyph_row);
4963
4964 /* Set up hook arguments. */
4965 args[0] = Qredisplay_end_trigger_functions;
4966 args[1] = it->window;
4967 XSETINT (args[2], it->redisplay_end_trigger_charpos);
4968 it->redisplay_end_trigger_charpos = 0;
4969
4970 /* Since we are *trying* to run these functions, don't try to run
4971 them again, even if they get an error. */
4972 it->w->redisplay_end_trigger = Qnil;
4973 Frun_hook_with_args (3, args);
4974
4975 /* Notice if it changed the face of the character we are on. */
4976 handle_face_prop (it);
4977 }
4978
4979
4980 /* Deliver a composition display element. The iterator IT is already
4981 filled with composition information (done in
4982 handle_composition_prop). Value is always 1. */
4983
4984 static int
4985 next_element_from_composition (it)
4986 struct it *it;
4987 {
4988 it->what = IT_COMPOSITION;
4989 it->position = (STRINGP (it->string)
4990 ? it->current.string_pos
4991 : it->current.pos);
4992 return 1;
4993 }
4994
4995
4996 \f
4997 /***********************************************************************
4998 Moving an iterator without producing glyphs
4999 ***********************************************************************/
5000
5001 /* Move iterator IT to a specified buffer or X position within one
5002 line on the display without producing glyphs.
5003
5004 OP should be a bit mask including some or all of these bits:
5005 MOVE_TO_X: Stop on reaching x-position TO_X.
5006 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
5007 Regardless of OP's value, stop in reaching the end of the display line.
5008
5009 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
5010 This means, in particular, that TO_X includes window's horizontal
5011 scroll amount.
5012
5013 The return value has several possible values that
5014 say what condition caused the scan to stop:
5015
5016 MOVE_POS_MATCH_OR_ZV
5017 - when TO_POS or ZV was reached.
5018
5019 MOVE_X_REACHED
5020 -when TO_X was reached before TO_POS or ZV were reached.
5021
5022 MOVE_LINE_CONTINUED
5023 - when we reached the end of the display area and the line must
5024 be continued.
5025
5026 MOVE_LINE_TRUNCATED
5027 - when we reached the end of the display area and the line is
5028 truncated.
5029
5030 MOVE_NEWLINE_OR_CR
5031 - when we stopped at a line end, i.e. a newline or a CR and selective
5032 display is on. */
5033
5034 static enum move_it_result
5035 move_it_in_display_line_to (it, to_charpos, to_x, op)
5036 struct it *it;
5037 int to_charpos, to_x, op;
5038 {
5039 enum move_it_result result = MOVE_UNDEFINED;
5040 struct glyph_row *saved_glyph_row;
5041
5042 /* Don't produce glyphs in produce_glyphs. */
5043 saved_glyph_row = it->glyph_row;
5044 it->glyph_row = NULL;
5045
5046 while (1)
5047 {
5048 int x, i, ascent = 0, descent = 0;
5049
5050 /* Stop when ZV or TO_CHARPOS reached. */
5051 if (!get_next_display_element (it)
5052 || ((op & MOVE_TO_POS) != 0
5053 && BUFFERP (it->object)
5054 && IT_CHARPOS (*it) >= to_charpos))
5055 {
5056 result = MOVE_POS_MATCH_OR_ZV;
5057 break;
5058 }
5059
5060 /* The call to produce_glyphs will get the metrics of the
5061 display element IT is loaded with. We record in x the
5062 x-position before this display element in case it does not
5063 fit on the line. */
5064 x = it->current_x;
5065
5066 /* Remember the line height so far in case the next element doesn't
5067 fit on the line. */
5068 if (!it->truncate_lines_p)
5069 {
5070 ascent = it->max_ascent;
5071 descent = it->max_descent;
5072 }
5073
5074 PRODUCE_GLYPHS (it);
5075
5076 if (it->area != TEXT_AREA)
5077 {
5078 set_iterator_to_next (it, 1);
5079 continue;
5080 }
5081
5082 /* The number of glyphs we get back in IT->nglyphs will normally
5083 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
5084 character on a terminal frame, or (iii) a line end. For the
5085 second case, IT->nglyphs - 1 padding glyphs will be present
5086 (on X frames, there is only one glyph produced for a
5087 composite character.
5088
5089 The behavior implemented below means, for continuation lines,
5090 that as many spaces of a TAB as fit on the current line are
5091 displayed there. For terminal frames, as many glyphs of a
5092 multi-glyph character are displayed in the current line, too.
5093 This is what the old redisplay code did, and we keep it that
5094 way. Under X, the whole shape of a complex character must
5095 fit on the line or it will be completely displayed in the
5096 next line.
5097
5098 Note that both for tabs and padding glyphs, all glyphs have
5099 the same width. */
5100 if (it->nglyphs)
5101 {
5102 /* More than one glyph or glyph doesn't fit on line. All
5103 glyphs have the same width. */
5104 int single_glyph_width = it->pixel_width / it->nglyphs;
5105 int new_x;
5106
5107 for (i = 0; i < it->nglyphs; ++i, x = new_x)
5108 {
5109 new_x = x + single_glyph_width;
5110
5111 /* We want to leave anything reaching TO_X to the caller. */
5112 if ((op & MOVE_TO_X) && new_x > to_x)
5113 {
5114 it->current_x = x;
5115 result = MOVE_X_REACHED;
5116 break;
5117 }
5118 else if (/* Lines are continued. */
5119 !it->truncate_lines_p
5120 && (/* And glyph doesn't fit on the line. */
5121 new_x > it->last_visible_x
5122 /* Or it fits exactly and we're on a window
5123 system frame. */
5124 || (new_x == it->last_visible_x
5125 && FRAME_WINDOW_P (it->f))))
5126 {
5127 if (/* IT->hpos == 0 means the very first glyph
5128 doesn't fit on the line, e.g. a wide image. */
5129 it->hpos == 0
5130 || (new_x == it->last_visible_x
5131 && FRAME_WINDOW_P (it->f)))
5132 {
5133 ++it->hpos;
5134 it->current_x = new_x;
5135 if (i == it->nglyphs - 1)
5136 set_iterator_to_next (it, 1);
5137 }
5138 else
5139 {
5140 it->current_x = x;
5141 it->max_ascent = ascent;
5142 it->max_descent = descent;
5143 }
5144
5145 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
5146 IT_CHARPOS (*it)));
5147 result = MOVE_LINE_CONTINUED;
5148 break;
5149 }
5150 else if (new_x > it->first_visible_x)
5151 {
5152 /* Glyph is visible. Increment number of glyphs that
5153 would be displayed. */
5154 ++it->hpos;
5155 }
5156 else
5157 {
5158 /* Glyph is completely off the left margin of the display
5159 area. Nothing to do. */
5160 }
5161 }
5162
5163 if (result != MOVE_UNDEFINED)
5164 break;
5165 }
5166 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
5167 {
5168 /* Stop when TO_X specified and reached. This check is
5169 necessary here because of lines consisting of a line end,
5170 only. The line end will not produce any glyphs and we
5171 would never get MOVE_X_REACHED. */
5172 xassert (it->nglyphs == 0);
5173 result = MOVE_X_REACHED;
5174 break;
5175 }
5176
5177 /* Is this a line end? If yes, we're done. */
5178 if (ITERATOR_AT_END_OF_LINE_P (it))
5179 {
5180 result = MOVE_NEWLINE_OR_CR;
5181 break;
5182 }
5183
5184 /* The current display element has been consumed. Advance
5185 to the next. */
5186 set_iterator_to_next (it, 1);
5187
5188 /* Stop if lines are truncated and IT's current x-position is
5189 past the right edge of the window now. */
5190 if (it->truncate_lines_p
5191 && it->current_x >= it->last_visible_x)
5192 {
5193 result = MOVE_LINE_TRUNCATED;
5194 break;
5195 }
5196 }
5197
5198 /* Restore the iterator settings altered at the beginning of this
5199 function. */
5200 it->glyph_row = saved_glyph_row;
5201 return result;
5202 }
5203
5204
5205 /* Move IT forward until it satisfies one or more of the criteria in
5206 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
5207
5208 OP is a bit-mask that specifies where to stop, and in particular,
5209 which of those four position arguments makes a difference. See the
5210 description of enum move_operation_enum.
5211
5212 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
5213 screen line, this function will set IT to the next position >
5214 TO_CHARPOS. */
5215
5216 void
5217 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
5218 struct it *it;
5219 int to_charpos, to_x, to_y, to_vpos;
5220 int op;
5221 {
5222 enum move_it_result skip, skip2 = MOVE_X_REACHED;
5223 int line_height;
5224 int reached = 0;
5225
5226 for (;;)
5227 {
5228 if (op & MOVE_TO_VPOS)
5229 {
5230 /* If no TO_CHARPOS and no TO_X specified, stop at the
5231 start of the line TO_VPOS. */
5232 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
5233 {
5234 if (it->vpos == to_vpos)
5235 {
5236 reached = 1;
5237 break;
5238 }
5239 else
5240 skip = move_it_in_display_line_to (it, -1, -1, 0);
5241 }
5242 else
5243 {
5244 /* TO_VPOS >= 0 means stop at TO_X in the line at
5245 TO_VPOS, or at TO_POS, whichever comes first. */
5246 if (it->vpos == to_vpos)
5247 {
5248 reached = 2;
5249 break;
5250 }
5251
5252 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
5253
5254 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
5255 {
5256 reached = 3;
5257 break;
5258 }
5259 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
5260 {
5261 /* We have reached TO_X but not in the line we want. */
5262 skip = move_it_in_display_line_to (it, to_charpos,
5263 -1, MOVE_TO_POS);
5264 if (skip == MOVE_POS_MATCH_OR_ZV)
5265 {
5266 reached = 4;
5267 break;
5268 }
5269 }
5270 }
5271 }
5272 else if (op & MOVE_TO_Y)
5273 {
5274 struct it it_backup;
5275
5276 /* TO_Y specified means stop at TO_X in the line containing
5277 TO_Y---or at TO_CHARPOS if this is reached first. The
5278 problem is that we can't really tell whether the line
5279 contains TO_Y before we have completely scanned it, and
5280 this may skip past TO_X. What we do is to first scan to
5281 TO_X.
5282
5283 If TO_X is not specified, use a TO_X of zero. The reason
5284 is to make the outcome of this function more predictable.
5285 If we didn't use TO_X == 0, we would stop at the end of
5286 the line which is probably not what a caller would expect
5287 to happen. */
5288 skip = move_it_in_display_line_to (it, to_charpos,
5289 ((op & MOVE_TO_X)
5290 ? to_x : 0),
5291 (MOVE_TO_X
5292 | (op & MOVE_TO_POS)));
5293
5294 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
5295 if (skip == MOVE_POS_MATCH_OR_ZV)
5296 {
5297 reached = 5;
5298 break;
5299 }
5300
5301 /* If TO_X was reached, we would like to know whether TO_Y
5302 is in the line. This can only be said if we know the
5303 total line height which requires us to scan the rest of
5304 the line. */
5305 if (skip == MOVE_X_REACHED)
5306 {
5307 it_backup = *it;
5308 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
5309 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
5310 op & MOVE_TO_POS);
5311 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
5312 }
5313
5314 /* Now, decide whether TO_Y is in this line. */
5315 line_height = it->max_ascent + it->max_descent;
5316 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
5317
5318 if (to_y >= it->current_y
5319 && to_y < it->current_y + line_height)
5320 {
5321 if (skip == MOVE_X_REACHED)
5322 /* If TO_Y is in this line and TO_X was reached above,
5323 we scanned too far. We have to restore IT's settings
5324 to the ones before skipping. */
5325 *it = it_backup;
5326 reached = 6;
5327 }
5328 else if (skip == MOVE_X_REACHED)
5329 {
5330 skip = skip2;
5331 if (skip == MOVE_POS_MATCH_OR_ZV)
5332 reached = 7;
5333 }
5334
5335 if (reached)
5336 break;
5337 }
5338 else
5339 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
5340
5341 switch (skip)
5342 {
5343 case MOVE_POS_MATCH_OR_ZV:
5344 reached = 8;
5345 goto out;
5346
5347 case MOVE_NEWLINE_OR_CR:
5348 set_iterator_to_next (it, 1);
5349 it->continuation_lines_width = 0;
5350 break;
5351
5352 case MOVE_LINE_TRUNCATED:
5353 it->continuation_lines_width = 0;
5354 reseat_at_next_visible_line_start (it, 0);
5355 if ((op & MOVE_TO_POS) != 0
5356 && IT_CHARPOS (*it) > to_charpos)
5357 {
5358 reached = 9;
5359 goto out;
5360 }
5361 break;
5362
5363 case MOVE_LINE_CONTINUED:
5364 it->continuation_lines_width += it->current_x;
5365 break;
5366
5367 default:
5368 abort ();
5369 }
5370
5371 /* Reset/increment for the next run. */
5372 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
5373 it->current_x = it->hpos = 0;
5374 it->current_y += it->max_ascent + it->max_descent;
5375 ++it->vpos;
5376 last_height = it->max_ascent + it->max_descent;
5377 last_max_ascent = it->max_ascent;
5378 it->max_ascent = it->max_descent = 0;
5379 }
5380
5381 out:
5382
5383 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
5384 }
5385
5386
5387 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
5388
5389 If DY > 0, move IT backward at least that many pixels. DY = 0
5390 means move IT backward to the preceding line start or BEGV. This
5391 function may move over more than DY pixels if IT->current_y - DY
5392 ends up in the middle of a line; in this case IT->current_y will be
5393 set to the top of the line moved to. */
5394
5395 void
5396 move_it_vertically_backward (it, dy)
5397 struct it *it;
5398 int dy;
5399 {
5400 int nlines, h;
5401 struct it it2, it3;
5402 int start_pos = IT_CHARPOS (*it);
5403
5404 xassert (dy >= 0);
5405
5406 /* Estimate how many newlines we must move back. */
5407 nlines = max (1, dy / CANON_Y_UNIT (it->f));
5408
5409 /* Set the iterator's position that many lines back. */
5410 while (nlines-- && IT_CHARPOS (*it) > BEGV)
5411 back_to_previous_visible_line_start (it);
5412
5413 /* Reseat the iterator here. When moving backward, we don't want
5414 reseat to skip forward over invisible text, set up the iterator
5415 to deliver from overlay strings at the new position etc. So,
5416 use reseat_1 here. */
5417 reseat_1 (it, it->current.pos, 1);
5418
5419 /* We are now surely at a line start. */
5420 it->current_x = it->hpos = 0;
5421 it->continuation_lines_width = 0;
5422
5423 /* Move forward and see what y-distance we moved. First move to the
5424 start of the next line so that we get its height. We need this
5425 height to be able to tell whether we reached the specified
5426 y-distance. */
5427 it2 = *it;
5428 it2.max_ascent = it2.max_descent = 0;
5429 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
5430 MOVE_TO_POS | MOVE_TO_VPOS);
5431 xassert (IT_CHARPOS (*it) >= BEGV);
5432 it3 = it2;
5433
5434 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
5435 xassert (IT_CHARPOS (*it) >= BEGV);
5436 h = it2.current_y - it->current_y;
5437 nlines = it2.vpos - it->vpos;
5438
5439 /* Correct IT's y and vpos position. */
5440 it->vpos -= nlines;
5441 it->current_y -= h;
5442
5443 if (dy == 0)
5444 {
5445 /* DY == 0 means move to the start of the screen line. The
5446 value of nlines is > 0 if continuation lines were involved. */
5447 if (nlines > 0)
5448 move_it_by_lines (it, nlines, 1);
5449 xassert (IT_CHARPOS (*it) <= start_pos);
5450 }
5451 else if (nlines)
5452 {
5453 /* The y-position we try to reach. Note that h has been
5454 subtracted in front of the if-statement. */
5455 int target_y = it->current_y + h - dy;
5456 int y0 = it3.current_y;
5457 int y1 = line_bottom_y (&it3);
5458 int line_height = y1 - y0;
5459
5460 /* If we did not reach target_y, try to move further backward if
5461 we can. If we moved too far backward, try to move forward. */
5462 if (target_y < it->current_y
5463 /* This is heuristic. In a window that's 3 lines high, with
5464 a line height of 13 pixels each, recentering with point
5465 on the bottom line will try to move -39/2 = 19 pixels
5466 backward. Try to avoid moving into the first line. */
5467 && it->current_y - target_y > line_height / 3 * 2
5468 && IT_CHARPOS (*it) > BEGV)
5469 {
5470 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
5471 target_y - it->current_y));
5472 move_it_vertically (it, target_y - it->current_y);
5473 xassert (IT_CHARPOS (*it) >= BEGV);
5474 }
5475 else if (target_y >= it->current_y + line_height
5476 && IT_CHARPOS (*it) < ZV)
5477 {
5478 /* Should move forward by at least one line, maybe more.
5479
5480 Note: Calling move_it_by_lines can be expensive on
5481 terminal frames, where compute_motion is used (via
5482 vmotion) to do the job, when there are very long lines
5483 and truncate-lines is nil. That's the reason for
5484 treating terminal frames specially here. */
5485
5486 if (!FRAME_WINDOW_P (it->f))
5487 move_it_vertically (it, target_y - (it->current_y + line_height));
5488 else
5489 {
5490 do
5491 {
5492 move_it_by_lines (it, 1, 1);
5493 }
5494 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
5495 }
5496
5497 xassert (IT_CHARPOS (*it) >= BEGV);
5498 }
5499 }
5500 }
5501
5502
5503 /* Move IT by a specified amount of pixel lines DY. DY negative means
5504 move backwards. DY = 0 means move to start of screen line. At the
5505 end, IT will be on the start of a screen line. */
5506
5507 void
5508 move_it_vertically (it, dy)
5509 struct it *it;
5510 int dy;
5511 {
5512 if (dy <= 0)
5513 move_it_vertically_backward (it, -dy);
5514 else if (dy > 0)
5515 {
5516 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
5517 move_it_to (it, ZV, -1, it->current_y + dy, -1,
5518 MOVE_TO_POS | MOVE_TO_Y);
5519 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
5520
5521 /* If buffer ends in ZV without a newline, move to the start of
5522 the line to satisfy the post-condition. */
5523 if (IT_CHARPOS (*it) == ZV
5524 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
5525 move_it_by_lines (it, 0, 0);
5526 }
5527 }
5528
5529
5530 /* Move iterator IT past the end of the text line it is in. */
5531
5532 void
5533 move_it_past_eol (it)
5534 struct it *it;
5535 {
5536 enum move_it_result rc;
5537
5538 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
5539 if (rc == MOVE_NEWLINE_OR_CR)
5540 set_iterator_to_next (it, 0);
5541 }
5542
5543
5544 #if 0 /* Currently not used. */
5545
5546 /* Return non-zero if some text between buffer positions START_CHARPOS
5547 and END_CHARPOS is invisible. IT->window is the window for text
5548 property lookup. */
5549
5550 static int
5551 invisible_text_between_p (it, start_charpos, end_charpos)
5552 struct it *it;
5553 int start_charpos, end_charpos;
5554 {
5555 Lisp_Object prop, limit;
5556 int invisible_found_p;
5557
5558 xassert (it != NULL && start_charpos <= end_charpos);
5559
5560 /* Is text at START invisible? */
5561 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
5562 it->window);
5563 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5564 invisible_found_p = 1;
5565 else
5566 {
5567 limit = Fnext_single_char_property_change (make_number (start_charpos),
5568 Qinvisible, Qnil,
5569 make_number (end_charpos));
5570 invisible_found_p = XFASTINT (limit) < end_charpos;
5571 }
5572
5573 return invisible_found_p;
5574 }
5575
5576 #endif /* 0 */
5577
5578
5579 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
5580 negative means move up. DVPOS == 0 means move to the start of the
5581 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
5582 NEED_Y_P is zero, IT->current_y will be left unchanged.
5583
5584 Further optimization ideas: If we would know that IT->f doesn't use
5585 a face with proportional font, we could be faster for
5586 truncate-lines nil. */
5587
5588 void
5589 move_it_by_lines (it, dvpos, need_y_p)
5590 struct it *it;
5591 int dvpos, need_y_p;
5592 {
5593 struct position pos;
5594
5595 if (!FRAME_WINDOW_P (it->f))
5596 {
5597 struct text_pos textpos;
5598
5599 /* We can use vmotion on frames without proportional fonts. */
5600 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
5601 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
5602 reseat (it, textpos, 1);
5603 it->vpos += pos.vpos;
5604 it->current_y += pos.vpos;
5605 }
5606 else if (dvpos == 0)
5607 {
5608 /* DVPOS == 0 means move to the start of the screen line. */
5609 move_it_vertically_backward (it, 0);
5610 xassert (it->current_x == 0 && it->hpos == 0);
5611 }
5612 else if (dvpos > 0)
5613 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
5614 else
5615 {
5616 struct it it2;
5617 int start_charpos, i;
5618
5619 /* Start at the beginning of the screen line containing IT's
5620 position. */
5621 move_it_vertically_backward (it, 0);
5622
5623 /* Go back -DVPOS visible lines and reseat the iterator there. */
5624 start_charpos = IT_CHARPOS (*it);
5625 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
5626 back_to_previous_visible_line_start (it);
5627 reseat (it, it->current.pos, 1);
5628 it->current_x = it->hpos = 0;
5629
5630 /* Above call may have moved too far if continuation lines
5631 are involved. Scan forward and see if it did. */
5632 it2 = *it;
5633 it2.vpos = it2.current_y = 0;
5634 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
5635 it->vpos -= it2.vpos;
5636 it->current_y -= it2.current_y;
5637 it->current_x = it->hpos = 0;
5638
5639 /* If we moved too far, move IT some lines forward. */
5640 if (it2.vpos > -dvpos)
5641 {
5642 int delta = it2.vpos + dvpos;
5643 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
5644 }
5645 }
5646 }
5647
5648
5649 \f
5650 /***********************************************************************
5651 Messages
5652 ***********************************************************************/
5653
5654
5655 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
5656 to *Messages*. */
5657
5658 void
5659 add_to_log (format, arg1, arg2)
5660 char *format;
5661 Lisp_Object arg1, arg2;
5662 {
5663 Lisp_Object args[3];
5664 Lisp_Object msg, fmt;
5665 char *buffer;
5666 int len;
5667 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5668
5669 /* Do nothing if called asynchronously. Inserting text into
5670 a buffer may call after-change-functions and alike and
5671 that would means running Lisp asynchronously. */
5672 if (handling_signal)
5673 return;
5674
5675 fmt = msg = Qnil;
5676 GCPRO4 (fmt, msg, arg1, arg2);
5677
5678 args[0] = fmt = build_string (format);
5679 args[1] = arg1;
5680 args[2] = arg2;
5681 msg = Fformat (3, args);
5682
5683 len = SBYTES (msg) + 1;
5684 buffer = (char *) alloca (len);
5685 bcopy (SDATA (msg), buffer, len);
5686
5687 message_dolog (buffer, len - 1, 1, 0);
5688 UNGCPRO;
5689 }
5690
5691
5692 /* Output a newline in the *Messages* buffer if "needs" one. */
5693
5694 void
5695 message_log_maybe_newline ()
5696 {
5697 if (message_log_need_newline)
5698 message_dolog ("", 0, 1, 0);
5699 }
5700
5701
5702 /* Add a string M of length NBYTES to the message log, optionally
5703 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
5704 nonzero, means interpret the contents of M as multibyte. This
5705 function calls low-level routines in order to bypass text property
5706 hooks, etc. which might not be safe to run. */
5707
5708 void
5709 message_dolog (m, nbytes, nlflag, multibyte)
5710 const char *m;
5711 int nbytes, nlflag, multibyte;
5712 {
5713 if (!NILP (Vmemory_full))
5714 return;
5715
5716 if (!NILP (Vmessage_log_max))
5717 {
5718 struct buffer *oldbuf;
5719 Lisp_Object oldpoint, oldbegv, oldzv;
5720 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5721 int point_at_end = 0;
5722 int zv_at_end = 0;
5723 Lisp_Object old_deactivate_mark, tem;
5724 struct gcpro gcpro1;
5725
5726 old_deactivate_mark = Vdeactivate_mark;
5727 oldbuf = current_buffer;
5728 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
5729 current_buffer->undo_list = Qt;
5730
5731 oldpoint = message_dolog_marker1;
5732 set_marker_restricted (oldpoint, make_number (PT), Qnil);
5733 oldbegv = message_dolog_marker2;
5734 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
5735 oldzv = message_dolog_marker3;
5736 set_marker_restricted (oldzv, make_number (ZV), Qnil);
5737 GCPRO1 (old_deactivate_mark);
5738
5739 if (PT == Z)
5740 point_at_end = 1;
5741 if (ZV == Z)
5742 zv_at_end = 1;
5743
5744 BEGV = BEG;
5745 BEGV_BYTE = BEG_BYTE;
5746 ZV = Z;
5747 ZV_BYTE = Z_BYTE;
5748 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5749
5750 /* Insert the string--maybe converting multibyte to single byte
5751 or vice versa, so that all the text fits the buffer. */
5752 if (multibyte
5753 && NILP (current_buffer->enable_multibyte_characters))
5754 {
5755 int i, c, char_bytes;
5756 unsigned char work[1];
5757
5758 /* Convert a multibyte string to single-byte
5759 for the *Message* buffer. */
5760 for (i = 0; i < nbytes; i += nbytes)
5761 {
5762 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
5763 work[0] = (SINGLE_BYTE_CHAR_P (c)
5764 ? c
5765 : multibyte_char_to_unibyte (c, Qnil));
5766 insert_1_both (work, 1, 1, 1, 0, 0);
5767 }
5768 }
5769 else if (! multibyte
5770 && ! NILP (current_buffer->enable_multibyte_characters))
5771 {
5772 int i, c, char_bytes;
5773 unsigned char *msg = (unsigned char *) m;
5774 unsigned char str[MAX_MULTIBYTE_LENGTH];
5775 /* Convert a single-byte string to multibyte
5776 for the *Message* buffer. */
5777 for (i = 0; i < nbytes; i++)
5778 {
5779 c = unibyte_char_to_multibyte (msg[i]);
5780 char_bytes = CHAR_STRING (c, str);
5781 insert_1_both (str, 1, char_bytes, 1, 0, 0);
5782 }
5783 }
5784 else if (nbytes)
5785 insert_1 (m, nbytes, 1, 0, 0);
5786
5787 if (nlflag)
5788 {
5789 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
5790 insert_1 ("\n", 1, 1, 0, 0);
5791
5792 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
5793 this_bol = PT;
5794 this_bol_byte = PT_BYTE;
5795
5796 /* See if this line duplicates the previous one.
5797 If so, combine duplicates. */
5798 if (this_bol > BEG)
5799 {
5800 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
5801 prev_bol = PT;
5802 prev_bol_byte = PT_BYTE;
5803
5804 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
5805 this_bol, this_bol_byte);
5806 if (dup)
5807 {
5808 del_range_both (prev_bol, prev_bol_byte,
5809 this_bol, this_bol_byte, 0);
5810 if (dup > 1)
5811 {
5812 char dupstr[40];
5813 int duplen;
5814
5815 /* If you change this format, don't forget to also
5816 change message_log_check_duplicate. */
5817 sprintf (dupstr, " [%d times]", dup);
5818 duplen = strlen (dupstr);
5819 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
5820 insert_1 (dupstr, duplen, 1, 0, 1);
5821 }
5822 }
5823 }
5824
5825 /* If we have more than the desired maximum number of lines
5826 in the *Messages* buffer now, delete the oldest ones.
5827 This is safe because we don't have undo in this buffer. */
5828
5829 if (NATNUMP (Vmessage_log_max))
5830 {
5831 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
5832 -XFASTINT (Vmessage_log_max) - 1, 0);
5833 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
5834 }
5835 }
5836 BEGV = XMARKER (oldbegv)->charpos;
5837 BEGV_BYTE = marker_byte_position (oldbegv);
5838
5839 if (zv_at_end)
5840 {
5841 ZV = Z;
5842 ZV_BYTE = Z_BYTE;
5843 }
5844 else
5845 {
5846 ZV = XMARKER (oldzv)->charpos;
5847 ZV_BYTE = marker_byte_position (oldzv);
5848 }
5849
5850 if (point_at_end)
5851 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5852 else
5853 /* We can't do Fgoto_char (oldpoint) because it will run some
5854 Lisp code. */
5855 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
5856 XMARKER (oldpoint)->bytepos);
5857
5858 UNGCPRO;
5859 unchain_marker (oldpoint);
5860 unchain_marker (oldbegv);
5861 unchain_marker (oldzv);
5862
5863 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
5864 set_buffer_internal (oldbuf);
5865 if (NILP (tem))
5866 windows_or_buffers_changed = old_windows_or_buffers_changed;
5867 message_log_need_newline = !nlflag;
5868 Vdeactivate_mark = old_deactivate_mark;
5869 }
5870 }
5871
5872
5873 /* We are at the end of the buffer after just having inserted a newline.
5874 (Note: We depend on the fact we won't be crossing the gap.)
5875 Check to see if the most recent message looks a lot like the previous one.
5876 Return 0 if different, 1 if the new one should just replace it, or a
5877 value N > 1 if we should also append " [N times]". */
5878
5879 static int
5880 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
5881 int prev_bol, this_bol;
5882 int prev_bol_byte, this_bol_byte;
5883 {
5884 int i;
5885 int len = Z_BYTE - 1 - this_bol_byte;
5886 int seen_dots = 0;
5887 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
5888 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
5889
5890 for (i = 0; i < len; i++)
5891 {
5892 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
5893 seen_dots = 1;
5894 if (p1[i] != p2[i])
5895 return seen_dots;
5896 }
5897 p1 += len;
5898 if (*p1 == '\n')
5899 return 2;
5900 if (*p1++ == ' ' && *p1++ == '[')
5901 {
5902 int n = 0;
5903 while (*p1 >= '0' && *p1 <= '9')
5904 n = n * 10 + *p1++ - '0';
5905 if (strncmp (p1, " times]\n", 8) == 0)
5906 return n+1;
5907 }
5908 return 0;
5909 }
5910
5911
5912 /* Display an echo area message M with a specified length of NBYTES
5913 bytes. The string may include null characters. If M is 0, clear
5914 out any existing message, and let the mini-buffer text show
5915 through.
5916
5917 The buffer M must continue to exist until after the echo area gets
5918 cleared or some other message gets displayed there. This means do
5919 not pass text that is stored in a Lisp string; do not pass text in
5920 a buffer that was alloca'd. */
5921
5922 void
5923 message2 (m, nbytes, multibyte)
5924 const char *m;
5925 int nbytes;
5926 int multibyte;
5927 {
5928 /* First flush out any partial line written with print. */
5929 message_log_maybe_newline ();
5930 if (m)
5931 message_dolog (m, nbytes, 1, multibyte);
5932 message2_nolog (m, nbytes, multibyte);
5933 }
5934
5935
5936 /* The non-logging counterpart of message2. */
5937
5938 void
5939 message2_nolog (m, nbytes, multibyte)
5940 const char *m;
5941 int nbytes, multibyte;
5942 {
5943 struct frame *sf = SELECTED_FRAME ();
5944 message_enable_multibyte = multibyte;
5945
5946 if (noninteractive)
5947 {
5948 if (noninteractive_need_newline)
5949 putc ('\n', stderr);
5950 noninteractive_need_newline = 0;
5951 if (m)
5952 fwrite (m, nbytes, 1, stderr);
5953 if (cursor_in_echo_area == 0)
5954 fprintf (stderr, "\n");
5955 fflush (stderr);
5956 }
5957 /* A null message buffer means that the frame hasn't really been
5958 initialized yet. Error messages get reported properly by
5959 cmd_error, so this must be just an informative message; toss it. */
5960 else if (INTERACTIVE
5961 && sf->glyphs_initialized_p
5962 && FRAME_MESSAGE_BUF (sf))
5963 {
5964 Lisp_Object mini_window;
5965 struct frame *f;
5966
5967 /* Get the frame containing the mini-buffer
5968 that the selected frame is using. */
5969 mini_window = FRAME_MINIBUF_WINDOW (sf);
5970 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5971
5972 FRAME_SAMPLE_VISIBILITY (f);
5973 if (FRAME_VISIBLE_P (sf)
5974 && ! FRAME_VISIBLE_P (f))
5975 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
5976
5977 if (m)
5978 {
5979 set_message (m, Qnil, nbytes, multibyte);
5980 if (minibuffer_auto_raise)
5981 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5982 }
5983 else
5984 clear_message (1, 1);
5985
5986 do_pending_window_change (0);
5987 echo_area_display (1);
5988 do_pending_window_change (0);
5989 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5990 (*frame_up_to_date_hook) (f);
5991 }
5992 }
5993
5994
5995 /* Display an echo area message M with a specified length of NBYTES
5996 bytes. The string may include null characters. If M is not a
5997 string, clear out any existing message, and let the mini-buffer
5998 text show through. */
5999
6000 void
6001 message3 (m, nbytes, multibyte)
6002 Lisp_Object m;
6003 int nbytes;
6004 int multibyte;
6005 {
6006 struct gcpro gcpro1;
6007
6008 GCPRO1 (m);
6009
6010 /* First flush out any partial line written with print. */
6011 message_log_maybe_newline ();
6012 if (STRINGP (m))
6013 message_dolog (SDATA (m), nbytes, 1, multibyte);
6014 message3_nolog (m, nbytes, multibyte);
6015
6016 UNGCPRO;
6017 }
6018
6019
6020 /* The non-logging version of message3. */
6021
6022 void
6023 message3_nolog (m, nbytes, multibyte)
6024 Lisp_Object m;
6025 int nbytes, multibyte;
6026 {
6027 struct frame *sf = SELECTED_FRAME ();
6028 message_enable_multibyte = multibyte;
6029
6030 if (noninteractive)
6031 {
6032 if (noninteractive_need_newline)
6033 putc ('\n', stderr);
6034 noninteractive_need_newline = 0;
6035 if (STRINGP (m))
6036 fwrite (SDATA (m), nbytes, 1, stderr);
6037 if (cursor_in_echo_area == 0)
6038 fprintf (stderr, "\n");
6039 fflush (stderr);
6040 }
6041 /* A null message buffer means that the frame hasn't really been
6042 initialized yet. Error messages get reported properly by
6043 cmd_error, so this must be just an informative message; toss it. */
6044 else if (INTERACTIVE
6045 && sf->glyphs_initialized_p
6046 && FRAME_MESSAGE_BUF (sf))
6047 {
6048 Lisp_Object mini_window;
6049 Lisp_Object frame;
6050 struct frame *f;
6051
6052 /* Get the frame containing the mini-buffer
6053 that the selected frame is using. */
6054 mini_window = FRAME_MINIBUF_WINDOW (sf);
6055 frame = XWINDOW (mini_window)->frame;
6056 f = XFRAME (frame);
6057
6058 FRAME_SAMPLE_VISIBILITY (f);
6059 if (FRAME_VISIBLE_P (sf)
6060 && !FRAME_VISIBLE_P (f))
6061 Fmake_frame_visible (frame);
6062
6063 if (STRINGP (m) && SCHARS (m) > 0)
6064 {
6065 set_message (NULL, m, nbytes, multibyte);
6066 if (minibuffer_auto_raise)
6067 Fraise_frame (frame);
6068 }
6069 else
6070 clear_message (1, 1);
6071
6072 do_pending_window_change (0);
6073 echo_area_display (1);
6074 do_pending_window_change (0);
6075 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6076 (*frame_up_to_date_hook) (f);
6077 }
6078 }
6079
6080
6081 /* Display a null-terminated echo area message M. If M is 0, clear
6082 out any existing message, and let the mini-buffer text show through.
6083
6084 The buffer M must continue to exist until after the echo area gets
6085 cleared or some other message gets displayed there. Do not pass
6086 text that is stored in a Lisp string. Do not pass text in a buffer
6087 that was alloca'd. */
6088
6089 void
6090 message1 (m)
6091 char *m;
6092 {
6093 message2 (m, (m ? strlen (m) : 0), 0);
6094 }
6095
6096
6097 /* The non-logging counterpart of message1. */
6098
6099 void
6100 message1_nolog (m)
6101 char *m;
6102 {
6103 message2_nolog (m, (m ? strlen (m) : 0), 0);
6104 }
6105
6106 /* Display a message M which contains a single %s
6107 which gets replaced with STRING. */
6108
6109 void
6110 message_with_string (m, string, log)
6111 char *m;
6112 Lisp_Object string;
6113 int log;
6114 {
6115 CHECK_STRING (string);
6116
6117 if (noninteractive)
6118 {
6119 if (m)
6120 {
6121 if (noninteractive_need_newline)
6122 putc ('\n', stderr);
6123 noninteractive_need_newline = 0;
6124 fprintf (stderr, m, SDATA (string));
6125 if (cursor_in_echo_area == 0)
6126 fprintf (stderr, "\n");
6127 fflush (stderr);
6128 }
6129 }
6130 else if (INTERACTIVE)
6131 {
6132 /* The frame whose minibuffer we're going to display the message on.
6133 It may be larger than the selected frame, so we need
6134 to use its buffer, not the selected frame's buffer. */
6135 Lisp_Object mini_window;
6136 struct frame *f, *sf = SELECTED_FRAME ();
6137
6138 /* Get the frame containing the minibuffer
6139 that the selected frame is using. */
6140 mini_window = FRAME_MINIBUF_WINDOW (sf);
6141 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6142
6143 /* A null message buffer means that the frame hasn't really been
6144 initialized yet. Error messages get reported properly by
6145 cmd_error, so this must be just an informative message; toss it. */
6146 if (FRAME_MESSAGE_BUF (f))
6147 {
6148 Lisp_Object args[2], message;
6149 struct gcpro gcpro1, gcpro2;
6150
6151 args[0] = build_string (m);
6152 args[1] = message = string;
6153 GCPRO2 (args[0], message);
6154 gcpro1.nvars = 2;
6155
6156 message = Fformat (2, args);
6157
6158 if (log)
6159 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
6160 else
6161 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
6162
6163 UNGCPRO;
6164
6165 /* Print should start at the beginning of the message
6166 buffer next time. */
6167 message_buf_print = 0;
6168 }
6169 }
6170 }
6171
6172
6173 /* Dump an informative message to the minibuf. If M is 0, clear out
6174 any existing message, and let the mini-buffer text show through. */
6175
6176 /* VARARGS 1 */
6177 void
6178 message (m, a1, a2, a3)
6179 char *m;
6180 EMACS_INT a1, a2, a3;
6181 {
6182 if (noninteractive)
6183 {
6184 if (m)
6185 {
6186 if (noninteractive_need_newline)
6187 putc ('\n', stderr);
6188 noninteractive_need_newline = 0;
6189 fprintf (stderr, m, a1, a2, a3);
6190 if (cursor_in_echo_area == 0)
6191 fprintf (stderr, "\n");
6192 fflush (stderr);
6193 }
6194 }
6195 else if (INTERACTIVE)
6196 {
6197 /* The frame whose mini-buffer we're going to display the message
6198 on. It may be larger than the selected frame, so we need to
6199 use its buffer, not the selected frame's buffer. */
6200 Lisp_Object mini_window;
6201 struct frame *f, *sf = SELECTED_FRAME ();
6202
6203 /* Get the frame containing the mini-buffer
6204 that the selected frame is using. */
6205 mini_window = FRAME_MINIBUF_WINDOW (sf);
6206 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6207
6208 /* A null message buffer means that the frame hasn't really been
6209 initialized yet. Error messages get reported properly by
6210 cmd_error, so this must be just an informative message; toss
6211 it. */
6212 if (FRAME_MESSAGE_BUF (f))
6213 {
6214 if (m)
6215 {
6216 int len;
6217 #ifdef NO_ARG_ARRAY
6218 char *a[3];
6219 a[0] = (char *) a1;
6220 a[1] = (char *) a2;
6221 a[2] = (char *) a3;
6222
6223 len = doprnt (FRAME_MESSAGE_BUF (f),
6224 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6225 #else
6226 len = doprnt (FRAME_MESSAGE_BUF (f),
6227 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
6228 (char **) &a1);
6229 #endif /* NO_ARG_ARRAY */
6230
6231 message2 (FRAME_MESSAGE_BUF (f), len, 0);
6232 }
6233 else
6234 message1 (0);
6235
6236 /* Print should start at the beginning of the message
6237 buffer next time. */
6238 message_buf_print = 0;
6239 }
6240 }
6241 }
6242
6243
6244 /* The non-logging version of message. */
6245
6246 void
6247 message_nolog (m, a1, a2, a3)
6248 char *m;
6249 EMACS_INT a1, a2, a3;
6250 {
6251 Lisp_Object old_log_max;
6252 old_log_max = Vmessage_log_max;
6253 Vmessage_log_max = Qnil;
6254 message (m, a1, a2, a3);
6255 Vmessage_log_max = old_log_max;
6256 }
6257
6258
6259 /* Display the current message in the current mini-buffer. This is
6260 only called from error handlers in process.c, and is not time
6261 critical. */
6262
6263 void
6264 update_echo_area ()
6265 {
6266 if (!NILP (echo_area_buffer[0]))
6267 {
6268 Lisp_Object string;
6269 string = Fcurrent_message ();
6270 message3 (string, SBYTES (string),
6271 !NILP (current_buffer->enable_multibyte_characters));
6272 }
6273 }
6274
6275
6276 /* Make sure echo area buffers in `echo_buffers' are live.
6277 If they aren't, make new ones. */
6278
6279 static void
6280 ensure_echo_area_buffers ()
6281 {
6282 int i;
6283
6284 for (i = 0; i < 2; ++i)
6285 if (!BUFFERP (echo_buffer[i])
6286 || NILP (XBUFFER (echo_buffer[i])->name))
6287 {
6288 char name[30];
6289 Lisp_Object old_buffer;
6290 int j;
6291
6292 old_buffer = echo_buffer[i];
6293 sprintf (name, " *Echo Area %d*", i);
6294 echo_buffer[i] = Fget_buffer_create (build_string (name));
6295 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
6296
6297 for (j = 0; j < 2; ++j)
6298 if (EQ (old_buffer, echo_area_buffer[j]))
6299 echo_area_buffer[j] = echo_buffer[i];
6300 }
6301 }
6302
6303
6304 /* Call FN with args A1..A4 with either the current or last displayed
6305 echo_area_buffer as current buffer.
6306
6307 WHICH zero means use the current message buffer
6308 echo_area_buffer[0]. If that is nil, choose a suitable buffer
6309 from echo_buffer[] and clear it.
6310
6311 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
6312 suitable buffer from echo_buffer[] and clear it.
6313
6314 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
6315 that the current message becomes the last displayed one, make
6316 choose a suitable buffer for echo_area_buffer[0], and clear it.
6317
6318 Value is what FN returns. */
6319
6320 static int
6321 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
6322 struct window *w;
6323 int which;
6324 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
6325 EMACS_INT a1;
6326 Lisp_Object a2;
6327 EMACS_INT a3, a4;
6328 {
6329 Lisp_Object buffer;
6330 int this_one, the_other, clear_buffer_p, rc;
6331 int count = SPECPDL_INDEX ();
6332
6333 /* If buffers aren't live, make new ones. */
6334 ensure_echo_area_buffers ();
6335
6336 clear_buffer_p = 0;
6337
6338 if (which == 0)
6339 this_one = 0, the_other = 1;
6340 else if (which > 0)
6341 this_one = 1, the_other = 0;
6342 else
6343 {
6344 this_one = 0, the_other = 1;
6345 clear_buffer_p = 1;
6346
6347 /* We need a fresh one in case the current echo buffer equals
6348 the one containing the last displayed echo area message. */
6349 if (!NILP (echo_area_buffer[this_one])
6350 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
6351 echo_area_buffer[this_one] = Qnil;
6352 }
6353
6354 /* Choose a suitable buffer from echo_buffer[] is we don't
6355 have one. */
6356 if (NILP (echo_area_buffer[this_one]))
6357 {
6358 echo_area_buffer[this_one]
6359 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
6360 ? echo_buffer[the_other]
6361 : echo_buffer[this_one]);
6362 clear_buffer_p = 1;
6363 }
6364
6365 buffer = echo_area_buffer[this_one];
6366
6367 /* Don't get confused by reusing the buffer used for echoing
6368 for a different purpose. */
6369 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
6370 cancel_echoing ();
6371
6372 record_unwind_protect (unwind_with_echo_area_buffer,
6373 with_echo_area_buffer_unwind_data (w));
6374
6375 /* Make the echo area buffer current. Note that for display
6376 purposes, it is not necessary that the displayed window's buffer
6377 == current_buffer, except for text property lookup. So, let's
6378 only set that buffer temporarily here without doing a full
6379 Fset_window_buffer. We must also change w->pointm, though,
6380 because otherwise an assertions in unshow_buffer fails, and Emacs
6381 aborts. */
6382 set_buffer_internal_1 (XBUFFER (buffer));
6383 if (w)
6384 {
6385 w->buffer = buffer;
6386 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
6387 }
6388
6389 current_buffer->undo_list = Qt;
6390 current_buffer->read_only = Qnil;
6391 specbind (Qinhibit_read_only, Qt);
6392 specbind (Qinhibit_modification_hooks, Qt);
6393
6394 if (clear_buffer_p && Z > BEG)
6395 del_range (BEG, Z);
6396
6397 xassert (BEGV >= BEG);
6398 xassert (ZV <= Z && ZV >= BEGV);
6399
6400 rc = fn (a1, a2, a3, a4);
6401
6402 xassert (BEGV >= BEG);
6403 xassert (ZV <= Z && ZV >= BEGV);
6404
6405 unbind_to (count, Qnil);
6406 return rc;
6407 }
6408
6409
6410 /* Save state that should be preserved around the call to the function
6411 FN called in with_echo_area_buffer. */
6412
6413 static Lisp_Object
6414 with_echo_area_buffer_unwind_data (w)
6415 struct window *w;
6416 {
6417 int i = 0;
6418 Lisp_Object vector;
6419
6420 /* Reduce consing by keeping one vector in
6421 Vwith_echo_area_save_vector. */
6422 vector = Vwith_echo_area_save_vector;
6423 Vwith_echo_area_save_vector = Qnil;
6424
6425 if (NILP (vector))
6426 vector = Fmake_vector (make_number (7), Qnil);
6427
6428 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
6429 AREF (vector, i) = Vdeactivate_mark, ++i;
6430 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
6431
6432 if (w)
6433 {
6434 XSETWINDOW (AREF (vector, i), w); ++i;
6435 AREF (vector, i) = w->buffer; ++i;
6436 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
6437 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
6438 }
6439 else
6440 {
6441 int end = i + 4;
6442 for (; i < end; ++i)
6443 AREF (vector, i) = Qnil;
6444 }
6445
6446 xassert (i == ASIZE (vector));
6447 return vector;
6448 }
6449
6450
6451 /* Restore global state from VECTOR which was created by
6452 with_echo_area_buffer_unwind_data. */
6453
6454 static Lisp_Object
6455 unwind_with_echo_area_buffer (vector)
6456 Lisp_Object vector;
6457 {
6458 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
6459 Vdeactivate_mark = AREF (vector, 1);
6460 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
6461
6462 if (WINDOWP (AREF (vector, 3)))
6463 {
6464 struct window *w;
6465 Lisp_Object buffer, charpos, bytepos;
6466
6467 w = XWINDOW (AREF (vector, 3));
6468 buffer = AREF (vector, 4);
6469 charpos = AREF (vector, 5);
6470 bytepos = AREF (vector, 6);
6471
6472 w->buffer = buffer;
6473 set_marker_both (w->pointm, buffer,
6474 XFASTINT (charpos), XFASTINT (bytepos));
6475 }
6476
6477 Vwith_echo_area_save_vector = vector;
6478 return Qnil;
6479 }
6480
6481
6482 /* Set up the echo area for use by print functions. MULTIBYTE_P
6483 non-zero means we will print multibyte. */
6484
6485 void
6486 setup_echo_area_for_printing (multibyte_p)
6487 int multibyte_p;
6488 {
6489 ensure_echo_area_buffers ();
6490
6491 if (!message_buf_print)
6492 {
6493 /* A message has been output since the last time we printed.
6494 Choose a fresh echo area buffer. */
6495 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6496 echo_area_buffer[0] = echo_buffer[1];
6497 else
6498 echo_area_buffer[0] = echo_buffer[0];
6499
6500 /* Switch to that buffer and clear it. */
6501 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6502 current_buffer->truncate_lines = Qnil;
6503
6504 if (Z > BEG)
6505 {
6506 int count = SPECPDL_INDEX ();
6507 specbind (Qinhibit_read_only, Qt);
6508 /* Note that undo recording is always disabled. */
6509 del_range (BEG, Z);
6510 unbind_to (count, Qnil);
6511 }
6512 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6513
6514 /* Set up the buffer for the multibyteness we need. */
6515 if (multibyte_p
6516 != !NILP (current_buffer->enable_multibyte_characters))
6517 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
6518
6519 /* Raise the frame containing the echo area. */
6520 if (minibuffer_auto_raise)
6521 {
6522 struct frame *sf = SELECTED_FRAME ();
6523 Lisp_Object mini_window;
6524 mini_window = FRAME_MINIBUF_WINDOW (sf);
6525 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6526 }
6527
6528 message_log_maybe_newline ();
6529 message_buf_print = 1;
6530 }
6531 else
6532 {
6533 if (NILP (echo_area_buffer[0]))
6534 {
6535 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6536 echo_area_buffer[0] = echo_buffer[1];
6537 else
6538 echo_area_buffer[0] = echo_buffer[0];
6539 }
6540
6541 if (current_buffer != XBUFFER (echo_area_buffer[0]))
6542 {
6543 /* Someone switched buffers between print requests. */
6544 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6545 current_buffer->truncate_lines = Qnil;
6546 }
6547 }
6548 }
6549
6550
6551 /* Display an echo area message in window W. Value is non-zero if W's
6552 height is changed. If display_last_displayed_message_p is
6553 non-zero, display the message that was last displayed, otherwise
6554 display the current message. */
6555
6556 static int
6557 display_echo_area (w)
6558 struct window *w;
6559 {
6560 int i, no_message_p, window_height_changed_p, count;
6561
6562 /* Temporarily disable garbage collections while displaying the echo
6563 area. This is done because a GC can print a message itself.
6564 That message would modify the echo area buffer's contents while a
6565 redisplay of the buffer is going on, and seriously confuse
6566 redisplay. */
6567 count = inhibit_garbage_collection ();
6568
6569 /* If there is no message, we must call display_echo_area_1
6570 nevertheless because it resizes the window. But we will have to
6571 reset the echo_area_buffer in question to nil at the end because
6572 with_echo_area_buffer will sets it to an empty buffer. */
6573 i = display_last_displayed_message_p ? 1 : 0;
6574 no_message_p = NILP (echo_area_buffer[i]);
6575
6576 window_height_changed_p
6577 = with_echo_area_buffer (w, display_last_displayed_message_p,
6578 display_echo_area_1,
6579 (EMACS_INT) w, Qnil, 0, 0);
6580
6581 if (no_message_p)
6582 echo_area_buffer[i] = Qnil;
6583
6584 unbind_to (count, Qnil);
6585 return window_height_changed_p;
6586 }
6587
6588
6589 /* Helper for display_echo_area. Display the current buffer which
6590 contains the current echo area message in window W, a mini-window,
6591 a pointer to which is passed in A1. A2..A4 are currently not used.
6592 Change the height of W so that all of the message is displayed.
6593 Value is non-zero if height of W was changed. */
6594
6595 static int
6596 display_echo_area_1 (a1, a2, a3, a4)
6597 EMACS_INT a1;
6598 Lisp_Object a2;
6599 EMACS_INT a3, a4;
6600 {
6601 struct window *w = (struct window *) a1;
6602 Lisp_Object window;
6603 struct text_pos start;
6604 int window_height_changed_p = 0;
6605
6606 /* Do this before displaying, so that we have a large enough glyph
6607 matrix for the display. */
6608 window_height_changed_p = resize_mini_window (w, 0);
6609
6610 /* Display. */
6611 clear_glyph_matrix (w->desired_matrix);
6612 XSETWINDOW (window, w);
6613 SET_TEXT_POS (start, BEG, BEG_BYTE);
6614 try_window (window, start);
6615
6616 return window_height_changed_p;
6617 }
6618
6619
6620 /* Resize the echo area window to exactly the size needed for the
6621 currently displayed message, if there is one. If a mini-buffer
6622 is active, don't shrink it. */
6623
6624 void
6625 resize_echo_area_exactly ()
6626 {
6627 if (BUFFERP (echo_area_buffer[0])
6628 && WINDOWP (echo_area_window))
6629 {
6630 struct window *w = XWINDOW (echo_area_window);
6631 int resized_p;
6632 Lisp_Object resize_exactly;
6633
6634 if (minibuf_level == 0)
6635 resize_exactly = Qt;
6636 else
6637 resize_exactly = Qnil;
6638
6639 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
6640 (EMACS_INT) w, resize_exactly, 0, 0);
6641 if (resized_p)
6642 {
6643 ++windows_or_buffers_changed;
6644 ++update_mode_lines;
6645 redisplay_internal (0);
6646 }
6647 }
6648 }
6649
6650
6651 /* Callback function for with_echo_area_buffer, when used from
6652 resize_echo_area_exactly. A1 contains a pointer to the window to
6653 resize, EXACTLY non-nil means resize the mini-window exactly to the
6654 size of the text displayed. A3 and A4 are not used. Value is what
6655 resize_mini_window returns. */
6656
6657 static int
6658 resize_mini_window_1 (a1, exactly, a3, a4)
6659 EMACS_INT a1;
6660 Lisp_Object exactly;
6661 EMACS_INT a3, a4;
6662 {
6663 return resize_mini_window ((struct window *) a1, !NILP (exactly));
6664 }
6665
6666
6667 /* Resize mini-window W to fit the size of its contents. EXACT:P
6668 means size the window exactly to the size needed. Otherwise, it's
6669 only enlarged until W's buffer is empty. Value is non-zero if
6670 the window height has been changed. */
6671
6672 int
6673 resize_mini_window (w, exact_p)
6674 struct window *w;
6675 int exact_p;
6676 {
6677 struct frame *f = XFRAME (w->frame);
6678 int window_height_changed_p = 0;
6679
6680 xassert (MINI_WINDOW_P (w));
6681
6682 /* Don't resize windows while redisplaying a window; it would
6683 confuse redisplay functions when the size of the window they are
6684 displaying changes from under them. Such a resizing can happen,
6685 for instance, when which-func prints a long message while
6686 we are running fontification-functions. We're running these
6687 functions with safe_call which binds inhibit-redisplay to t. */
6688 if (!NILP (Vinhibit_redisplay))
6689 return 0;
6690
6691 /* Nil means don't try to resize. */
6692 if (NILP (Vresize_mini_windows)
6693 || (FRAME_X_P (f) && f->output_data.x == NULL))
6694 return 0;
6695
6696 if (!FRAME_MINIBUF_ONLY_P (f))
6697 {
6698 struct it it;
6699 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
6700 int total_height = XFASTINT (root->height) + XFASTINT (w->height);
6701 int height, max_height;
6702 int unit = CANON_Y_UNIT (f);
6703 struct text_pos start;
6704 struct buffer *old_current_buffer = NULL;
6705
6706 if (current_buffer != XBUFFER (w->buffer))
6707 {
6708 old_current_buffer = current_buffer;
6709 set_buffer_internal (XBUFFER (w->buffer));
6710 }
6711
6712 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
6713
6714 /* Compute the max. number of lines specified by the user. */
6715 if (FLOATP (Vmax_mini_window_height))
6716 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_HEIGHT (f);
6717 else if (INTEGERP (Vmax_mini_window_height))
6718 max_height = XINT (Vmax_mini_window_height);
6719 else
6720 max_height = total_height / 4;
6721
6722 /* Correct that max. height if it's bogus. */
6723 max_height = max (1, max_height);
6724 max_height = min (total_height, max_height);
6725
6726 /* Find out the height of the text in the window. */
6727 if (it.truncate_lines_p)
6728 height = 1;
6729 else
6730 {
6731 last_height = 0;
6732 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
6733 if (it.max_ascent == 0 && it.max_descent == 0)
6734 height = it.current_y + last_height;
6735 else
6736 height = it.current_y + it.max_ascent + it.max_descent;
6737 height -= it.extra_line_spacing;
6738 height = (height + unit - 1) / unit;
6739 }
6740
6741 /* Compute a suitable window start. */
6742 if (height > max_height)
6743 {
6744 height = max_height;
6745 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
6746 move_it_vertically_backward (&it, (height - 1) * unit);
6747 start = it.current.pos;
6748 }
6749 else
6750 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
6751 SET_MARKER_FROM_TEXT_POS (w->start, start);
6752
6753 if (EQ (Vresize_mini_windows, Qgrow_only))
6754 {
6755 /* Let it grow only, until we display an empty message, in which
6756 case the window shrinks again. */
6757 if (height > XFASTINT (w->height))
6758 {
6759 int old_height = XFASTINT (w->height);
6760 freeze_window_starts (f, 1);
6761 grow_mini_window (w, height - XFASTINT (w->height));
6762 window_height_changed_p = XFASTINT (w->height) != old_height;
6763 }
6764 else if (height < XFASTINT (w->height)
6765 && (exact_p || BEGV == ZV))
6766 {
6767 int old_height = XFASTINT (w->height);
6768 freeze_window_starts (f, 0);
6769 shrink_mini_window (w);
6770 window_height_changed_p = XFASTINT (w->height) != old_height;
6771 }
6772 }
6773 else
6774 {
6775 /* Always resize to exact size needed. */
6776 if (height > XFASTINT (w->height))
6777 {
6778 int old_height = XFASTINT (w->height);
6779 freeze_window_starts (f, 1);
6780 grow_mini_window (w, height - XFASTINT (w->height));
6781 window_height_changed_p = XFASTINT (w->height) != old_height;
6782 }
6783 else if (height < XFASTINT (w->height))
6784 {
6785 int old_height = XFASTINT (w->height);
6786 freeze_window_starts (f, 0);
6787 shrink_mini_window (w);
6788
6789 if (height)
6790 {
6791 freeze_window_starts (f, 1);
6792 grow_mini_window (w, height - XFASTINT (w->height));
6793 }
6794
6795 window_height_changed_p = XFASTINT (w->height) != old_height;
6796 }
6797 }
6798
6799 if (old_current_buffer)
6800 set_buffer_internal (old_current_buffer);
6801 }
6802
6803 return window_height_changed_p;
6804 }
6805
6806
6807 /* Value is the current message, a string, or nil if there is no
6808 current message. */
6809
6810 Lisp_Object
6811 current_message ()
6812 {
6813 Lisp_Object msg;
6814
6815 if (NILP (echo_area_buffer[0]))
6816 msg = Qnil;
6817 else
6818 {
6819 with_echo_area_buffer (0, 0, current_message_1,
6820 (EMACS_INT) &msg, Qnil, 0, 0);
6821 if (NILP (msg))
6822 echo_area_buffer[0] = Qnil;
6823 }
6824
6825 return msg;
6826 }
6827
6828
6829 static int
6830 current_message_1 (a1, a2, a3, a4)
6831 EMACS_INT a1;
6832 Lisp_Object a2;
6833 EMACS_INT a3, a4;
6834 {
6835 Lisp_Object *msg = (Lisp_Object *) a1;
6836
6837 if (Z > BEG)
6838 *msg = make_buffer_string (BEG, Z, 1);
6839 else
6840 *msg = Qnil;
6841 return 0;
6842 }
6843
6844
6845 /* Push the current message on Vmessage_stack for later restauration
6846 by restore_message. Value is non-zero if the current message isn't
6847 empty. This is a relatively infrequent operation, so it's not
6848 worth optimizing. */
6849
6850 int
6851 push_message ()
6852 {
6853 Lisp_Object msg;
6854 msg = current_message ();
6855 Vmessage_stack = Fcons (msg, Vmessage_stack);
6856 return STRINGP (msg);
6857 }
6858
6859
6860 /* Restore message display from the top of Vmessage_stack. */
6861
6862 void
6863 restore_message ()
6864 {
6865 Lisp_Object msg;
6866
6867 xassert (CONSP (Vmessage_stack));
6868 msg = XCAR (Vmessage_stack);
6869 if (STRINGP (msg))
6870 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
6871 else
6872 message3_nolog (msg, 0, 0);
6873 }
6874
6875
6876 /* Handler for record_unwind_protect calling pop_message. */
6877
6878 Lisp_Object
6879 pop_message_unwind (dummy)
6880 Lisp_Object dummy;
6881 {
6882 pop_message ();
6883 return Qnil;
6884 }
6885
6886 /* Pop the top-most entry off Vmessage_stack. */
6887
6888 void
6889 pop_message ()
6890 {
6891 xassert (CONSP (Vmessage_stack));
6892 Vmessage_stack = XCDR (Vmessage_stack);
6893 }
6894
6895
6896 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
6897 exits. If the stack is not empty, we have a missing pop_message
6898 somewhere. */
6899
6900 void
6901 check_message_stack ()
6902 {
6903 if (!NILP (Vmessage_stack))
6904 abort ();
6905 }
6906
6907
6908 /* Truncate to NCHARS what will be displayed in the echo area the next
6909 time we display it---but don't redisplay it now. */
6910
6911 void
6912 truncate_echo_area (nchars)
6913 int nchars;
6914 {
6915 if (nchars == 0)
6916 echo_area_buffer[0] = Qnil;
6917 /* A null message buffer means that the frame hasn't really been
6918 initialized yet. Error messages get reported properly by
6919 cmd_error, so this must be just an informative message; toss it. */
6920 else if (!noninteractive
6921 && INTERACTIVE
6922 && !NILP (echo_area_buffer[0]))
6923 {
6924 struct frame *sf = SELECTED_FRAME ();
6925 if (FRAME_MESSAGE_BUF (sf))
6926 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
6927 }
6928 }
6929
6930
6931 /* Helper function for truncate_echo_area. Truncate the current
6932 message to at most NCHARS characters. */
6933
6934 static int
6935 truncate_message_1 (nchars, a2, a3, a4)
6936 EMACS_INT nchars;
6937 Lisp_Object a2;
6938 EMACS_INT a3, a4;
6939 {
6940 if (BEG + nchars < Z)
6941 del_range (BEG + nchars, Z);
6942 if (Z == BEG)
6943 echo_area_buffer[0] = Qnil;
6944 return 0;
6945 }
6946
6947
6948 /* Set the current message to a substring of S or STRING.
6949
6950 If STRING is a Lisp string, set the message to the first NBYTES
6951 bytes from STRING. NBYTES zero means use the whole string. If
6952 STRING is multibyte, the message will be displayed multibyte.
6953
6954 If S is not null, set the message to the first LEN bytes of S. LEN
6955 zero means use the whole string. MULTIBYTE_P non-zero means S is
6956 multibyte. Display the message multibyte in that case. */
6957
6958 void
6959 set_message (s, string, nbytes, multibyte_p)
6960 const char *s;
6961 Lisp_Object string;
6962 int nbytes, multibyte_p;
6963 {
6964 message_enable_multibyte
6965 = ((s && multibyte_p)
6966 || (STRINGP (string) && STRING_MULTIBYTE (string)));
6967
6968 with_echo_area_buffer (0, -1, set_message_1,
6969 (EMACS_INT) s, string, nbytes, multibyte_p);
6970 message_buf_print = 0;
6971 help_echo_showing_p = 0;
6972 }
6973
6974
6975 /* Helper function for set_message. Arguments have the same meaning
6976 as there, with A1 corresponding to S and A2 corresponding to STRING
6977 This function is called with the echo area buffer being
6978 current. */
6979
6980 static int
6981 set_message_1 (a1, a2, nbytes, multibyte_p)
6982 EMACS_INT a1;
6983 Lisp_Object a2;
6984 EMACS_INT nbytes, multibyte_p;
6985 {
6986 const char *s = (const char *) a1;
6987 Lisp_Object string = a2;
6988
6989 xassert (BEG == Z);
6990
6991 /* Change multibyteness of the echo buffer appropriately. */
6992 if (message_enable_multibyte
6993 != !NILP (current_buffer->enable_multibyte_characters))
6994 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
6995
6996 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
6997
6998 /* Insert new message at BEG. */
6999 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7000
7001 if (STRINGP (string))
7002 {
7003 int nchars;
7004
7005 if (nbytes == 0)
7006 nbytes = SBYTES (string);
7007 nchars = string_byte_to_char (string, nbytes);
7008
7009 /* This function takes care of single/multibyte conversion. We
7010 just have to ensure that the echo area buffer has the right
7011 setting of enable_multibyte_characters. */
7012 insert_from_string (string, 0, 0, nchars, nbytes, 1);
7013 }
7014 else if (s)
7015 {
7016 if (nbytes == 0)
7017 nbytes = strlen (s);
7018
7019 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
7020 {
7021 /* Convert from multi-byte to single-byte. */
7022 int i, c, n;
7023 unsigned char work[1];
7024
7025 /* Convert a multibyte string to single-byte. */
7026 for (i = 0; i < nbytes; i += n)
7027 {
7028 c = string_char_and_length (s + i, nbytes - i, &n);
7029 work[0] = (SINGLE_BYTE_CHAR_P (c)
7030 ? c
7031 : multibyte_char_to_unibyte (c, Qnil));
7032 insert_1_both (work, 1, 1, 1, 0, 0);
7033 }
7034 }
7035 else if (!multibyte_p
7036 && !NILP (current_buffer->enable_multibyte_characters))
7037 {
7038 /* Convert from single-byte to multi-byte. */
7039 int i, c, n;
7040 const unsigned char *msg = (const unsigned char *) s;
7041 unsigned char str[MAX_MULTIBYTE_LENGTH];
7042
7043 /* Convert a single-byte string to multibyte. */
7044 for (i = 0; i < nbytes; i++)
7045 {
7046 c = unibyte_char_to_multibyte (msg[i]);
7047 n = CHAR_STRING (c, str);
7048 insert_1_both (str, 1, n, 1, 0, 0);
7049 }
7050 }
7051 else
7052 insert_1 (s, nbytes, 1, 0, 0);
7053 }
7054
7055 return 0;
7056 }
7057
7058
7059 /* Clear messages. CURRENT_P non-zero means clear the current
7060 message. LAST_DISPLAYED_P non-zero means clear the message
7061 last displayed. */
7062
7063 void
7064 clear_message (current_p, last_displayed_p)
7065 int current_p, last_displayed_p;
7066 {
7067 if (current_p)
7068 {
7069 echo_area_buffer[0] = Qnil;
7070 message_cleared_p = 1;
7071 }
7072
7073 if (last_displayed_p)
7074 echo_area_buffer[1] = Qnil;
7075
7076 message_buf_print = 0;
7077 }
7078
7079 /* Clear garbaged frames.
7080
7081 This function is used where the old redisplay called
7082 redraw_garbaged_frames which in turn called redraw_frame which in
7083 turn called clear_frame. The call to clear_frame was a source of
7084 flickering. I believe a clear_frame is not necessary. It should
7085 suffice in the new redisplay to invalidate all current matrices,
7086 and ensure a complete redisplay of all windows. */
7087
7088 static void
7089 clear_garbaged_frames ()
7090 {
7091 if (frame_garbaged)
7092 {
7093 Lisp_Object tail, frame;
7094 int changed_count = 0;
7095
7096 FOR_EACH_FRAME (tail, frame)
7097 {
7098 struct frame *f = XFRAME (frame);
7099
7100 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
7101 {
7102 if (f->resized_p)
7103 Fredraw_frame (frame);
7104 clear_current_matrices (f);
7105 changed_count++;
7106 f->garbaged = 0;
7107 f->resized_p = 0;
7108 }
7109 }
7110
7111 frame_garbaged = 0;
7112 if (changed_count)
7113 ++windows_or_buffers_changed;
7114 }
7115 }
7116
7117
7118 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
7119 is non-zero update selected_frame. Value is non-zero if the
7120 mini-windows height has been changed. */
7121
7122 static int
7123 echo_area_display (update_frame_p)
7124 int update_frame_p;
7125 {
7126 Lisp_Object mini_window;
7127 struct window *w;
7128 struct frame *f;
7129 int window_height_changed_p = 0;
7130 struct frame *sf = SELECTED_FRAME ();
7131
7132 mini_window = FRAME_MINIBUF_WINDOW (sf);
7133 w = XWINDOW (mini_window);
7134 f = XFRAME (WINDOW_FRAME (w));
7135
7136 /* Don't display if frame is invisible or not yet initialized. */
7137 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
7138 return 0;
7139
7140 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
7141 #ifndef MAC_OS8
7142 #ifdef HAVE_WINDOW_SYSTEM
7143 /* When Emacs starts, selected_frame may be a visible terminal
7144 frame, even if we run under a window system. If we let this
7145 through, a message would be displayed on the terminal. */
7146 if (EQ (selected_frame, Vterminal_frame)
7147 && !NILP (Vwindow_system))
7148 return 0;
7149 #endif /* HAVE_WINDOW_SYSTEM */
7150 #endif
7151
7152 /* Redraw garbaged frames. */
7153 if (frame_garbaged)
7154 clear_garbaged_frames ();
7155
7156 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
7157 {
7158 echo_area_window = mini_window;
7159 window_height_changed_p = display_echo_area (w);
7160 w->must_be_updated_p = 1;
7161
7162 /* Update the display, unless called from redisplay_internal.
7163 Also don't update the screen during redisplay itself. The
7164 update will happen at the end of redisplay, and an update
7165 here could cause confusion. */
7166 if (update_frame_p && !redisplaying_p)
7167 {
7168 int n = 0;
7169
7170 /* If the display update has been interrupted by pending
7171 input, update mode lines in the frame. Due to the
7172 pending input, it might have been that redisplay hasn't
7173 been called, so that mode lines above the echo area are
7174 garbaged. This looks odd, so we prevent it here. */
7175 if (!display_completed)
7176 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
7177
7178 if (window_height_changed_p
7179 /* Don't do this if Emacs is shutting down. Redisplay
7180 needs to run hooks. */
7181 && !NILP (Vrun_hooks))
7182 {
7183 /* Must update other windows. Likewise as in other
7184 cases, don't let this update be interrupted by
7185 pending input. */
7186 int count = SPECPDL_INDEX ();
7187 specbind (Qredisplay_dont_pause, Qt);
7188 windows_or_buffers_changed = 1;
7189 redisplay_internal (0);
7190 unbind_to (count, Qnil);
7191 }
7192 else if (FRAME_WINDOW_P (f) && n == 0)
7193 {
7194 /* Window configuration is the same as before.
7195 Can do with a display update of the echo area,
7196 unless we displayed some mode lines. */
7197 update_single_window (w, 1);
7198 rif->flush_display (f);
7199 }
7200 else
7201 update_frame (f, 1, 1);
7202
7203 /* If cursor is in the echo area, make sure that the next
7204 redisplay displays the minibuffer, so that the cursor will
7205 be replaced with what the minibuffer wants. */
7206 if (cursor_in_echo_area)
7207 ++windows_or_buffers_changed;
7208 }
7209 }
7210 else if (!EQ (mini_window, selected_window))
7211 windows_or_buffers_changed++;
7212
7213 /* Last displayed message is now the current message. */
7214 echo_area_buffer[1] = echo_area_buffer[0];
7215
7216 /* Prevent redisplay optimization in redisplay_internal by resetting
7217 this_line_start_pos. This is done because the mini-buffer now
7218 displays the message instead of its buffer text. */
7219 if (EQ (mini_window, selected_window))
7220 CHARPOS (this_line_start_pos) = 0;
7221
7222 return window_height_changed_p;
7223 }
7224
7225
7226 \f
7227 /***********************************************************************
7228 Frame Titles
7229 ***********************************************************************/
7230
7231
7232 /* The frame title buffering code is also used by Fformat_mode_line.
7233 So it is not conditioned by HAVE_WINDOW_SYSTEM. */
7234
7235 /* A buffer for constructing frame titles in it; allocated from the
7236 heap in init_xdisp and resized as needed in store_frame_title_char. */
7237
7238 static char *frame_title_buf;
7239
7240 /* The buffer's end, and a current output position in it. */
7241
7242 static char *frame_title_buf_end;
7243 static char *frame_title_ptr;
7244
7245
7246 /* Store a single character C for the frame title in frame_title_buf.
7247 Re-allocate frame_title_buf if necessary. */
7248
7249 static void
7250 #ifdef PROTOTYPES
7251 store_frame_title_char (char c)
7252 #else
7253 store_frame_title_char (c)
7254 char c;
7255 #endif
7256 {
7257 /* If output position has reached the end of the allocated buffer,
7258 double the buffer's size. */
7259 if (frame_title_ptr == frame_title_buf_end)
7260 {
7261 int len = frame_title_ptr - frame_title_buf;
7262 int new_size = 2 * len * sizeof *frame_title_buf;
7263 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
7264 frame_title_buf_end = frame_title_buf + new_size;
7265 frame_title_ptr = frame_title_buf + len;
7266 }
7267
7268 *frame_title_ptr++ = c;
7269 }
7270
7271
7272 /* Store part of a frame title in frame_title_buf, beginning at
7273 frame_title_ptr. STR is the string to store. Do not copy
7274 characters that yield more columns than PRECISION; PRECISION <= 0
7275 means copy the whole string. Pad with spaces until FIELD_WIDTH
7276 number of characters have been copied; FIELD_WIDTH <= 0 means don't
7277 pad. Called from display_mode_element when it is used to build a
7278 frame title. */
7279
7280 static int
7281 store_frame_title (str, field_width, precision)
7282 const unsigned char *str;
7283 int field_width, precision;
7284 {
7285 int n = 0;
7286 int dummy, nbytes;
7287
7288 /* Copy at most PRECISION chars from STR. */
7289 nbytes = strlen (str);
7290 n+= c_string_width (str, nbytes, precision, &dummy, &nbytes);
7291 while (nbytes--)
7292 store_frame_title_char (*str++);
7293
7294 /* Fill up with spaces until FIELD_WIDTH reached. */
7295 while (field_width > 0
7296 && n < field_width)
7297 {
7298 store_frame_title_char (' ');
7299 ++n;
7300 }
7301
7302 return n;
7303 }
7304
7305 #ifdef HAVE_WINDOW_SYSTEM
7306
7307 /* Set the title of FRAME, if it has changed. The title format is
7308 Vicon_title_format if FRAME is iconified, otherwise it is
7309 frame_title_format. */
7310
7311 static void
7312 x_consider_frame_title (frame)
7313 Lisp_Object frame;
7314 {
7315 struct frame *f = XFRAME (frame);
7316
7317 if (FRAME_WINDOW_P (f)
7318 || FRAME_MINIBUF_ONLY_P (f)
7319 || f->explicit_name)
7320 {
7321 /* Do we have more than one visible frame on this X display? */
7322 Lisp_Object tail;
7323 Lisp_Object fmt;
7324 struct buffer *obuf;
7325 int len;
7326 struct it it;
7327
7328 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
7329 {
7330 Lisp_Object other_frame = XCAR (tail);
7331 struct frame *tf = XFRAME (other_frame);
7332
7333 if (tf != f
7334 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
7335 && !FRAME_MINIBUF_ONLY_P (tf)
7336 && !EQ (other_frame, tip_frame)
7337 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
7338 break;
7339 }
7340
7341 /* Set global variable indicating that multiple frames exist. */
7342 multiple_frames = CONSP (tail);
7343
7344 /* Switch to the buffer of selected window of the frame. Set up
7345 frame_title_ptr so that display_mode_element will output into it;
7346 then display the title. */
7347 obuf = current_buffer;
7348 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
7349 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
7350 frame_title_ptr = frame_title_buf;
7351 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
7352 NULL, DEFAULT_FACE_ID);
7353 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
7354 len = frame_title_ptr - frame_title_buf;
7355 frame_title_ptr = NULL;
7356 set_buffer_internal_1 (obuf);
7357
7358 /* Set the title only if it's changed. This avoids consing in
7359 the common case where it hasn't. (If it turns out that we've
7360 already wasted too much time by walking through the list with
7361 display_mode_element, then we might need to optimize at a
7362 higher level than this.) */
7363 if (! STRINGP (f->name)
7364 || SBYTES (f->name) != len
7365 || bcmp (frame_title_buf, SDATA (f->name), len) != 0)
7366 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
7367 }
7368 }
7369
7370 #endif /* not HAVE_WINDOW_SYSTEM */
7371
7372
7373
7374 \f
7375 /***********************************************************************
7376 Menu Bars
7377 ***********************************************************************/
7378
7379
7380 /* Prepare for redisplay by updating menu-bar item lists when
7381 appropriate. This can call eval. */
7382
7383 void
7384 prepare_menu_bars ()
7385 {
7386 int all_windows;
7387 struct gcpro gcpro1, gcpro2;
7388 struct frame *f;
7389 Lisp_Object tooltip_frame;
7390
7391 #ifdef HAVE_WINDOW_SYSTEM
7392 tooltip_frame = tip_frame;
7393 #else
7394 tooltip_frame = Qnil;
7395 #endif
7396
7397 /* Update all frame titles based on their buffer names, etc. We do
7398 this before the menu bars so that the buffer-menu will show the
7399 up-to-date frame titles. */
7400 #ifdef HAVE_WINDOW_SYSTEM
7401 if (windows_or_buffers_changed || update_mode_lines)
7402 {
7403 Lisp_Object tail, frame;
7404
7405 FOR_EACH_FRAME (tail, frame)
7406 {
7407 f = XFRAME (frame);
7408 if (!EQ (frame, tooltip_frame)
7409 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
7410 x_consider_frame_title (frame);
7411 }
7412 }
7413 #endif /* HAVE_WINDOW_SYSTEM */
7414
7415 /* Update the menu bar item lists, if appropriate. This has to be
7416 done before any actual redisplay or generation of display lines. */
7417 all_windows = (update_mode_lines
7418 || buffer_shared > 1
7419 || windows_or_buffers_changed);
7420 if (all_windows)
7421 {
7422 Lisp_Object tail, frame;
7423 int count = SPECPDL_INDEX ();
7424
7425 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7426
7427 FOR_EACH_FRAME (tail, frame)
7428 {
7429 f = XFRAME (frame);
7430
7431 /* Ignore tooltip frame. */
7432 if (EQ (frame, tooltip_frame))
7433 continue;
7434
7435 /* If a window on this frame changed size, report that to
7436 the user and clear the size-change flag. */
7437 if (FRAME_WINDOW_SIZES_CHANGED (f))
7438 {
7439 Lisp_Object functions;
7440
7441 /* Clear flag first in case we get an error below. */
7442 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
7443 functions = Vwindow_size_change_functions;
7444 GCPRO2 (tail, functions);
7445
7446 while (CONSP (functions))
7447 {
7448 call1 (XCAR (functions), frame);
7449 functions = XCDR (functions);
7450 }
7451 UNGCPRO;
7452 }
7453
7454 GCPRO1 (tail);
7455 update_menu_bar (f, 0);
7456 #ifdef HAVE_WINDOW_SYSTEM
7457 update_tool_bar (f, 0);
7458 #endif
7459 UNGCPRO;
7460 }
7461
7462 unbind_to (count, Qnil);
7463 }
7464 else
7465 {
7466 struct frame *sf = SELECTED_FRAME ();
7467 update_menu_bar (sf, 1);
7468 #ifdef HAVE_WINDOW_SYSTEM
7469 update_tool_bar (sf, 1);
7470 #endif
7471 }
7472
7473 /* Motif needs this. See comment in xmenu.c. Turn it off when
7474 pending_menu_activation is not defined. */
7475 #ifdef USE_X_TOOLKIT
7476 pending_menu_activation = 0;
7477 #endif
7478 }
7479
7480
7481 /* Update the menu bar item list for frame F. This has to be done
7482 before we start to fill in any display lines, because it can call
7483 eval.
7484
7485 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
7486
7487 static void
7488 update_menu_bar (f, save_match_data)
7489 struct frame *f;
7490 int save_match_data;
7491 {
7492 Lisp_Object window;
7493 register struct window *w;
7494
7495 /* If called recursively during a menu update, do nothing. This can
7496 happen when, for instance, an activate-menubar-hook causes a
7497 redisplay. */
7498 if (inhibit_menubar_update)
7499 return;
7500
7501 window = FRAME_SELECTED_WINDOW (f);
7502 w = XWINDOW (window);
7503
7504 #if 0 /* The if statement below this if statement used to include the
7505 condition !NILP (w->update_mode_line), rather than using
7506 update_mode_lines directly, and this if statement may have
7507 been added to make that condition work. Now the if
7508 statement below matches its comment, this isn't needed. */
7509 if (update_mode_lines)
7510 w->update_mode_line = Qt;
7511 #endif
7512
7513 if (FRAME_WINDOW_P (f)
7514 ?
7515 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS)
7516 FRAME_EXTERNAL_MENU_BAR (f)
7517 #else
7518 FRAME_MENU_BAR_LINES (f) > 0
7519 #endif
7520 : FRAME_MENU_BAR_LINES (f) > 0)
7521 {
7522 /* If the user has switched buffers or windows, we need to
7523 recompute to reflect the new bindings. But we'll
7524 recompute when update_mode_lines is set too; that means
7525 that people can use force-mode-line-update to request
7526 that the menu bar be recomputed. The adverse effect on
7527 the rest of the redisplay algorithm is about the same as
7528 windows_or_buffers_changed anyway. */
7529 if (windows_or_buffers_changed
7530 /* This used to test w->update_mode_line, but we believe
7531 there is no need to recompute the menu in that case. */
7532 || update_mode_lines
7533 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7534 < BUF_MODIFF (XBUFFER (w->buffer)))
7535 != !NILP (w->last_had_star))
7536 || ((!NILP (Vtransient_mark_mode)
7537 && !NILP (XBUFFER (w->buffer)->mark_active))
7538 != !NILP (w->region_showing)))
7539 {
7540 struct buffer *prev = current_buffer;
7541 int count = SPECPDL_INDEX ();
7542
7543 specbind (Qinhibit_menubar_update, Qt);
7544
7545 set_buffer_internal_1 (XBUFFER (w->buffer));
7546 if (save_match_data)
7547 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7548 if (NILP (Voverriding_local_map_menu_flag))
7549 {
7550 specbind (Qoverriding_terminal_local_map, Qnil);
7551 specbind (Qoverriding_local_map, Qnil);
7552 }
7553
7554 /* Run the Lucid hook. */
7555 safe_run_hooks (Qactivate_menubar_hook);
7556
7557 /* If it has changed current-menubar from previous value,
7558 really recompute the menu-bar from the value. */
7559 if (! NILP (Vlucid_menu_bar_dirty_flag))
7560 call0 (Qrecompute_lucid_menubar);
7561
7562 safe_run_hooks (Qmenu_bar_update_hook);
7563 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
7564
7565 /* Redisplay the menu bar in case we changed it. */
7566 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS)
7567 if (FRAME_WINDOW_P (f)
7568 #if defined (MAC_OS)
7569 /* All frames on Mac OS share the same menubar. So only the
7570 selected frame should be allowed to set it. */
7571 && f == SELECTED_FRAME ()
7572 #endif
7573 )
7574 set_frame_menubar (f, 0, 0);
7575 else
7576 /* On a terminal screen, the menu bar is an ordinary screen
7577 line, and this makes it get updated. */
7578 w->update_mode_line = Qt;
7579 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7580 /* In the non-toolkit version, the menu bar is an ordinary screen
7581 line, and this makes it get updated. */
7582 w->update_mode_line = Qt;
7583 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7584
7585 unbind_to (count, Qnil);
7586 set_buffer_internal_1 (prev);
7587 }
7588 }
7589 }
7590
7591
7592 \f
7593 /***********************************************************************
7594 Tool-bars
7595 ***********************************************************************/
7596
7597 #ifdef HAVE_WINDOW_SYSTEM
7598
7599 /* Update the tool-bar item list for frame F. This has to be done
7600 before we start to fill in any display lines. Called from
7601 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
7602 and restore it here. */
7603
7604 static void
7605 update_tool_bar (f, save_match_data)
7606 struct frame *f;
7607 int save_match_data;
7608 {
7609 if (WINDOWP (f->tool_bar_window)
7610 && XFASTINT (XWINDOW (f->tool_bar_window)->height) > 0)
7611 {
7612 Lisp_Object window;
7613 struct window *w;
7614
7615 window = FRAME_SELECTED_WINDOW (f);
7616 w = XWINDOW (window);
7617
7618 /* If the user has switched buffers or windows, we need to
7619 recompute to reflect the new bindings. But we'll
7620 recompute when update_mode_lines is set too; that means
7621 that people can use force-mode-line-update to request
7622 that the menu bar be recomputed. The adverse effect on
7623 the rest of the redisplay algorithm is about the same as
7624 windows_or_buffers_changed anyway. */
7625 if (windows_or_buffers_changed
7626 || !NILP (w->update_mode_line)
7627 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7628 < BUF_MODIFF (XBUFFER (w->buffer)))
7629 != !NILP (w->last_had_star))
7630 || ((!NILP (Vtransient_mark_mode)
7631 && !NILP (XBUFFER (w->buffer)->mark_active))
7632 != !NILP (w->region_showing)))
7633 {
7634 struct buffer *prev = current_buffer;
7635 int count = SPECPDL_INDEX ();
7636
7637 /* Set current_buffer to the buffer of the selected
7638 window of the frame, so that we get the right local
7639 keymaps. */
7640 set_buffer_internal_1 (XBUFFER (w->buffer));
7641
7642 /* Save match data, if we must. */
7643 if (save_match_data)
7644 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7645
7646 /* Make sure that we don't accidentally use bogus keymaps. */
7647 if (NILP (Voverriding_local_map_menu_flag))
7648 {
7649 specbind (Qoverriding_terminal_local_map, Qnil);
7650 specbind (Qoverriding_local_map, Qnil);
7651 }
7652
7653 /* Build desired tool-bar items from keymaps. */
7654 f->tool_bar_items
7655 = tool_bar_items (f->tool_bar_items, &f->n_tool_bar_items);
7656
7657 /* Redisplay the tool-bar in case we changed it. */
7658 w->update_mode_line = Qt;
7659
7660 unbind_to (count, Qnil);
7661 set_buffer_internal_1 (prev);
7662 }
7663 }
7664 }
7665
7666
7667 /* Set F->desired_tool_bar_string to a Lisp string representing frame
7668 F's desired tool-bar contents. F->tool_bar_items must have
7669 been set up previously by calling prepare_menu_bars. */
7670
7671 static void
7672 build_desired_tool_bar_string (f)
7673 struct frame *f;
7674 {
7675 int i, size, size_needed;
7676 struct gcpro gcpro1, gcpro2, gcpro3;
7677 Lisp_Object image, plist, props;
7678
7679 image = plist = props = Qnil;
7680 GCPRO3 (image, plist, props);
7681
7682 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
7683 Otherwise, make a new string. */
7684
7685 /* The size of the string we might be able to reuse. */
7686 size = (STRINGP (f->desired_tool_bar_string)
7687 ? SCHARS (f->desired_tool_bar_string)
7688 : 0);
7689
7690 /* We need one space in the string for each image. */
7691 size_needed = f->n_tool_bar_items;
7692
7693 /* Reuse f->desired_tool_bar_string, if possible. */
7694 if (size < size_needed || NILP (f->desired_tool_bar_string))
7695 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
7696 make_number (' '));
7697 else
7698 {
7699 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
7700 Fremove_text_properties (make_number (0), make_number (size),
7701 props, f->desired_tool_bar_string);
7702 }
7703
7704 /* Put a `display' property on the string for the images to display,
7705 put a `menu_item' property on tool-bar items with a value that
7706 is the index of the item in F's tool-bar item vector. */
7707 for (i = 0; i < f->n_tool_bar_items; ++i)
7708 {
7709 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
7710
7711 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
7712 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
7713 int hmargin, vmargin, relief, idx, end;
7714 extern Lisp_Object QCrelief, QCmargin, QCconversion, Qimage;
7715
7716 /* If image is a vector, choose the image according to the
7717 button state. */
7718 image = PROP (TOOL_BAR_ITEM_IMAGES);
7719 if (VECTORP (image))
7720 {
7721 if (enabled_p)
7722 idx = (selected_p
7723 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
7724 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
7725 else
7726 idx = (selected_p
7727 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
7728 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
7729
7730 xassert (ASIZE (image) >= idx);
7731 image = AREF (image, idx);
7732 }
7733 else
7734 idx = -1;
7735
7736 /* Ignore invalid image specifications. */
7737 if (!valid_image_p (image))
7738 continue;
7739
7740 /* Display the tool-bar button pressed, or depressed. */
7741 plist = Fcopy_sequence (XCDR (image));
7742
7743 /* Compute margin and relief to draw. */
7744 relief = (tool_bar_button_relief >= 0
7745 ? tool_bar_button_relief
7746 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
7747 hmargin = vmargin = relief;
7748
7749 if (INTEGERP (Vtool_bar_button_margin)
7750 && XINT (Vtool_bar_button_margin) > 0)
7751 {
7752 hmargin += XFASTINT (Vtool_bar_button_margin);
7753 vmargin += XFASTINT (Vtool_bar_button_margin);
7754 }
7755 else if (CONSP (Vtool_bar_button_margin))
7756 {
7757 if (INTEGERP (XCAR (Vtool_bar_button_margin))
7758 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
7759 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
7760
7761 if (INTEGERP (XCDR (Vtool_bar_button_margin))
7762 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
7763 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
7764 }
7765
7766 if (auto_raise_tool_bar_buttons_p)
7767 {
7768 /* Add a `:relief' property to the image spec if the item is
7769 selected. */
7770 if (selected_p)
7771 {
7772 plist = Fplist_put (plist, QCrelief, make_number (-relief));
7773 hmargin -= relief;
7774 vmargin -= relief;
7775 }
7776 }
7777 else
7778 {
7779 /* If image is selected, display it pressed, i.e. with a
7780 negative relief. If it's not selected, display it with a
7781 raised relief. */
7782 plist = Fplist_put (plist, QCrelief,
7783 (selected_p
7784 ? make_number (-relief)
7785 : make_number (relief)));
7786 hmargin -= relief;
7787 vmargin -= relief;
7788 }
7789
7790 /* Put a margin around the image. */
7791 if (hmargin || vmargin)
7792 {
7793 if (hmargin == vmargin)
7794 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
7795 else
7796 plist = Fplist_put (plist, QCmargin,
7797 Fcons (make_number (hmargin),
7798 make_number (vmargin)));
7799 }
7800
7801 /* If button is not enabled, and we don't have special images
7802 for the disabled state, make the image appear disabled by
7803 applying an appropriate algorithm to it. */
7804 if (!enabled_p && idx < 0)
7805 plist = Fplist_put (plist, QCconversion, Qdisabled);
7806
7807 /* Put a `display' text property on the string for the image to
7808 display. Put a `menu-item' property on the string that gives
7809 the start of this item's properties in the tool-bar items
7810 vector. */
7811 image = Fcons (Qimage, plist);
7812 props = list4 (Qdisplay, image,
7813 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
7814
7815 /* Let the last image hide all remaining spaces in the tool bar
7816 string. The string can be longer than needed when we reuse a
7817 previous string. */
7818 if (i + 1 == f->n_tool_bar_items)
7819 end = SCHARS (f->desired_tool_bar_string);
7820 else
7821 end = i + 1;
7822 Fadd_text_properties (make_number (i), make_number (end),
7823 props, f->desired_tool_bar_string);
7824 #undef PROP
7825 }
7826
7827 UNGCPRO;
7828 }
7829
7830
7831 /* Display one line of the tool-bar of frame IT->f. */
7832
7833 static void
7834 display_tool_bar_line (it)
7835 struct it *it;
7836 {
7837 struct glyph_row *row = it->glyph_row;
7838 int max_x = it->last_visible_x;
7839 struct glyph *last;
7840
7841 prepare_desired_row (row);
7842 row->y = it->current_y;
7843
7844 /* Note that this isn't made use of if the face hasn't a box,
7845 so there's no need to check the face here. */
7846 it->start_of_box_run_p = 1;
7847
7848 while (it->current_x < max_x)
7849 {
7850 int x_before, x, n_glyphs_before, i, nglyphs;
7851
7852 /* Get the next display element. */
7853 if (!get_next_display_element (it))
7854 break;
7855
7856 /* Produce glyphs. */
7857 x_before = it->current_x;
7858 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
7859 PRODUCE_GLYPHS (it);
7860
7861 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
7862 i = 0;
7863 x = x_before;
7864 while (i < nglyphs)
7865 {
7866 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
7867
7868 if (x + glyph->pixel_width > max_x)
7869 {
7870 /* Glyph doesn't fit on line. */
7871 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
7872 it->current_x = x;
7873 goto out;
7874 }
7875
7876 ++it->hpos;
7877 x += glyph->pixel_width;
7878 ++i;
7879 }
7880
7881 /* Stop at line ends. */
7882 if (ITERATOR_AT_END_OF_LINE_P (it))
7883 break;
7884
7885 set_iterator_to_next (it, 1);
7886 }
7887
7888 out:;
7889
7890 row->displays_text_p = row->used[TEXT_AREA] != 0;
7891 extend_face_to_end_of_line (it);
7892 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
7893 last->right_box_line_p = 1;
7894 if (last == row->glyphs[TEXT_AREA])
7895 last->left_box_line_p = 1;
7896 compute_line_metrics (it);
7897
7898 /* If line is empty, make it occupy the rest of the tool-bar. */
7899 if (!row->displays_text_p)
7900 {
7901 row->height = row->phys_height = it->last_visible_y - row->y;
7902 row->ascent = row->phys_ascent = 0;
7903 }
7904
7905 row->full_width_p = 1;
7906 row->continued_p = 0;
7907 row->truncated_on_left_p = 0;
7908 row->truncated_on_right_p = 0;
7909
7910 it->current_x = it->hpos = 0;
7911 it->current_y += row->height;
7912 ++it->vpos;
7913 ++it->glyph_row;
7914 }
7915
7916
7917 /* Value is the number of screen lines needed to make all tool-bar
7918 items of frame F visible. */
7919
7920 static int
7921 tool_bar_lines_needed (f)
7922 struct frame *f;
7923 {
7924 struct window *w = XWINDOW (f->tool_bar_window);
7925 struct it it;
7926
7927 /* Initialize an iterator for iteration over
7928 F->desired_tool_bar_string in the tool-bar window of frame F. */
7929 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7930 it.first_visible_x = 0;
7931 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7932 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7933
7934 while (!ITERATOR_AT_END_P (&it))
7935 {
7936 it.glyph_row = w->desired_matrix->rows;
7937 clear_glyph_row (it.glyph_row);
7938 display_tool_bar_line (&it);
7939 }
7940
7941 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
7942 }
7943
7944
7945 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
7946 0, 1, 0,
7947 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
7948 (frame)
7949 Lisp_Object frame;
7950 {
7951 struct frame *f;
7952 struct window *w;
7953 int nlines = 0;
7954
7955 if (NILP (frame))
7956 frame = selected_frame;
7957 else
7958 CHECK_FRAME (frame);
7959 f = XFRAME (frame);
7960
7961 if (WINDOWP (f->tool_bar_window)
7962 || (w = XWINDOW (f->tool_bar_window),
7963 XFASTINT (w->height) > 0))
7964 {
7965 update_tool_bar (f, 1);
7966 if (f->n_tool_bar_items)
7967 {
7968 build_desired_tool_bar_string (f);
7969 nlines = tool_bar_lines_needed (f);
7970 }
7971 }
7972
7973 return make_number (nlines);
7974 }
7975
7976
7977 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
7978 height should be changed. */
7979
7980 static int
7981 redisplay_tool_bar (f)
7982 struct frame *f;
7983 {
7984 struct window *w;
7985 struct it it;
7986 struct glyph_row *row;
7987 int change_height_p = 0;
7988
7989 /* If frame hasn't a tool-bar window or if it is zero-height, don't
7990 do anything. This means you must start with tool-bar-lines
7991 non-zero to get the auto-sizing effect. Or in other words, you
7992 can turn off tool-bars by specifying tool-bar-lines zero. */
7993 if (!WINDOWP (f->tool_bar_window)
7994 || (w = XWINDOW (f->tool_bar_window),
7995 XFASTINT (w->height) == 0))
7996 return 0;
7997
7998 /* Set up an iterator for the tool-bar window. */
7999 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
8000 it.first_visible_x = 0;
8001 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
8002 row = it.glyph_row;
8003
8004 /* Build a string that represents the contents of the tool-bar. */
8005 build_desired_tool_bar_string (f);
8006 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
8007
8008 /* Display as many lines as needed to display all tool-bar items. */
8009 while (it.current_y < it.last_visible_y)
8010 display_tool_bar_line (&it);
8011
8012 /* It doesn't make much sense to try scrolling in the tool-bar
8013 window, so don't do it. */
8014 w->desired_matrix->no_scrolling_p = 1;
8015 w->must_be_updated_p = 1;
8016
8017 if (auto_resize_tool_bars_p)
8018 {
8019 int nlines;
8020
8021 /* If we couldn't display everything, change the tool-bar's
8022 height. */
8023 if (IT_STRING_CHARPOS (it) < it.end_charpos)
8024 change_height_p = 1;
8025
8026 /* If there are blank lines at the end, except for a partially
8027 visible blank line at the end that is smaller than
8028 CANON_Y_UNIT, change the tool-bar's height. */
8029 row = it.glyph_row - 1;
8030 if (!row->displays_text_p
8031 && row->height >= CANON_Y_UNIT (f))
8032 change_height_p = 1;
8033
8034 /* If row displays tool-bar items, but is partially visible,
8035 change the tool-bar's height. */
8036 if (row->displays_text_p
8037 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
8038 change_height_p = 1;
8039
8040 /* Resize windows as needed by changing the `tool-bar-lines'
8041 frame parameter. */
8042 if (change_height_p
8043 && (nlines = tool_bar_lines_needed (f),
8044 nlines != XFASTINT (w->height)))
8045 {
8046 extern Lisp_Object Qtool_bar_lines;
8047 Lisp_Object frame;
8048 int old_height = XFASTINT (w->height);
8049
8050 XSETFRAME (frame, f);
8051 clear_glyph_matrix (w->desired_matrix);
8052 Fmodify_frame_parameters (frame,
8053 Fcons (Fcons (Qtool_bar_lines,
8054 make_number (nlines)),
8055 Qnil));
8056 if (XFASTINT (w->height) != old_height)
8057 fonts_changed_p = 1;
8058 }
8059 }
8060
8061 return change_height_p;
8062 }
8063
8064
8065 /* Get information about the tool-bar item which is displayed in GLYPH
8066 on frame F. Return in *PROP_IDX the index where tool-bar item
8067 properties start in F->tool_bar_items. Value is zero if
8068 GLYPH doesn't display a tool-bar item. */
8069
8070 int
8071 tool_bar_item_info (f, glyph, prop_idx)
8072 struct frame *f;
8073 struct glyph *glyph;
8074 int *prop_idx;
8075 {
8076 Lisp_Object prop;
8077 int success_p;
8078 int charpos;
8079
8080 /* This function can be called asynchronously, which means we must
8081 exclude any possibility that Fget_text_property signals an
8082 error. */
8083 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
8084 charpos = max (0, charpos);
8085
8086 /* Get the text property `menu-item' at pos. The value of that
8087 property is the start index of this item's properties in
8088 F->tool_bar_items. */
8089 prop = Fget_text_property (make_number (charpos),
8090 Qmenu_item, f->current_tool_bar_string);
8091 if (INTEGERP (prop))
8092 {
8093 *prop_idx = XINT (prop);
8094 success_p = 1;
8095 }
8096 else
8097 success_p = 0;
8098
8099 return success_p;
8100 }
8101
8102 #endif /* HAVE_WINDOW_SYSTEM */
8103
8104
8105 \f
8106 /************************************************************************
8107 Horizontal scrolling
8108 ************************************************************************/
8109
8110 static int hscroll_window_tree P_ ((Lisp_Object));
8111 static int hscroll_windows P_ ((Lisp_Object));
8112
8113 /* For all leaf windows in the window tree rooted at WINDOW, set their
8114 hscroll value so that PT is (i) visible in the window, and (ii) so
8115 that it is not within a certain margin at the window's left and
8116 right border. Value is non-zero if any window's hscroll has been
8117 changed. */
8118
8119 static int
8120 hscroll_window_tree (window)
8121 Lisp_Object window;
8122 {
8123 int hscrolled_p = 0;
8124 int hscroll_relative_p = FLOATP (Vhscroll_step);
8125 int hscroll_step_abs = 0;
8126 double hscroll_step_rel = 0;
8127
8128 if (hscroll_relative_p)
8129 {
8130 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
8131 if (hscroll_step_rel < 0)
8132 {
8133 hscroll_relative_p = 0;
8134 hscroll_step_abs = 0;
8135 }
8136 }
8137 else if (INTEGERP (Vhscroll_step))
8138 {
8139 hscroll_step_abs = XINT (Vhscroll_step);
8140 if (hscroll_step_abs < 0)
8141 hscroll_step_abs = 0;
8142 }
8143 else
8144 hscroll_step_abs = 0;
8145
8146 while (WINDOWP (window))
8147 {
8148 struct window *w = XWINDOW (window);
8149
8150 if (WINDOWP (w->hchild))
8151 hscrolled_p |= hscroll_window_tree (w->hchild);
8152 else if (WINDOWP (w->vchild))
8153 hscrolled_p |= hscroll_window_tree (w->vchild);
8154 else if (w->cursor.vpos >= 0)
8155 {
8156 int h_margin, text_area_x, text_area_y;
8157 int text_area_width, text_area_height;
8158 struct glyph_row *current_cursor_row
8159 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
8160 struct glyph_row *desired_cursor_row
8161 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
8162 struct glyph_row *cursor_row
8163 = (desired_cursor_row->enabled_p
8164 ? desired_cursor_row
8165 : current_cursor_row);
8166
8167 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
8168 &text_area_width, &text_area_height);
8169
8170 /* Scroll when cursor is inside this scroll margin. */
8171 h_margin = hscroll_margin * CANON_X_UNIT (XFRAME (w->frame));
8172
8173 if ((XFASTINT (w->hscroll)
8174 && w->cursor.x <= h_margin)
8175 || (cursor_row->enabled_p
8176 && cursor_row->truncated_on_right_p
8177 && (w->cursor.x >= text_area_width - h_margin)))
8178 {
8179 struct it it;
8180 int hscroll;
8181 struct buffer *saved_current_buffer;
8182 int pt;
8183 int wanted_x;
8184
8185 /* Find point in a display of infinite width. */
8186 saved_current_buffer = current_buffer;
8187 current_buffer = XBUFFER (w->buffer);
8188
8189 if (w == XWINDOW (selected_window))
8190 pt = BUF_PT (current_buffer);
8191 else
8192 {
8193 pt = marker_position (w->pointm);
8194 pt = max (BEGV, pt);
8195 pt = min (ZV, pt);
8196 }
8197
8198 /* Move iterator to pt starting at cursor_row->start in
8199 a line with infinite width. */
8200 init_to_row_start (&it, w, cursor_row);
8201 it.last_visible_x = INFINITY;
8202 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
8203 current_buffer = saved_current_buffer;
8204
8205 /* Position cursor in window. */
8206 if (!hscroll_relative_p && hscroll_step_abs == 0)
8207 hscroll = max (0, it.current_x - text_area_width / 2)
8208 / CANON_X_UNIT (it.f);
8209 else if (w->cursor.x >= text_area_width - h_margin)
8210 {
8211 if (hscroll_relative_p)
8212 wanted_x = text_area_width * (1 - hscroll_step_rel)
8213 - h_margin;
8214 else
8215 wanted_x = text_area_width
8216 - hscroll_step_abs * CANON_X_UNIT (it.f)
8217 - h_margin;
8218 hscroll
8219 = max (0, it.current_x - wanted_x) / CANON_X_UNIT (it.f);
8220 }
8221 else
8222 {
8223 if (hscroll_relative_p)
8224 wanted_x = text_area_width * hscroll_step_rel
8225 + h_margin;
8226 else
8227 wanted_x = hscroll_step_abs * CANON_X_UNIT (it.f)
8228 + h_margin;
8229 hscroll
8230 = max (0, it.current_x - wanted_x) / CANON_X_UNIT (it.f);
8231 }
8232 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
8233
8234 /* Don't call Fset_window_hscroll if value hasn't
8235 changed because it will prevent redisplay
8236 optimizations. */
8237 if (XFASTINT (w->hscroll) != hscroll)
8238 {
8239 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
8240 w->hscroll = make_number (hscroll);
8241 hscrolled_p = 1;
8242 }
8243 }
8244 }
8245
8246 window = w->next;
8247 }
8248
8249 /* Value is non-zero if hscroll of any leaf window has been changed. */
8250 return hscrolled_p;
8251 }
8252
8253
8254 /* Set hscroll so that cursor is visible and not inside horizontal
8255 scroll margins for all windows in the tree rooted at WINDOW. See
8256 also hscroll_window_tree above. Value is non-zero if any window's
8257 hscroll has been changed. If it has, desired matrices on the frame
8258 of WINDOW are cleared. */
8259
8260 static int
8261 hscroll_windows (window)
8262 Lisp_Object window;
8263 {
8264 int hscrolled_p;
8265
8266 if (automatic_hscrolling_p)
8267 {
8268 hscrolled_p = hscroll_window_tree (window);
8269 if (hscrolled_p)
8270 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
8271 }
8272 else
8273 hscrolled_p = 0;
8274 return hscrolled_p;
8275 }
8276
8277
8278 \f
8279 /************************************************************************
8280 Redisplay
8281 ************************************************************************/
8282
8283 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
8284 to a non-zero value. This is sometimes handy to have in a debugger
8285 session. */
8286
8287 #if GLYPH_DEBUG
8288
8289 /* First and last unchanged row for try_window_id. */
8290
8291 int debug_first_unchanged_at_end_vpos;
8292 int debug_last_unchanged_at_beg_vpos;
8293
8294 /* Delta vpos and y. */
8295
8296 int debug_dvpos, debug_dy;
8297
8298 /* Delta in characters and bytes for try_window_id. */
8299
8300 int debug_delta, debug_delta_bytes;
8301
8302 /* Values of window_end_pos and window_end_vpos at the end of
8303 try_window_id. */
8304
8305 EMACS_INT debug_end_pos, debug_end_vpos;
8306
8307 /* Append a string to W->desired_matrix->method. FMT is a printf
8308 format string. A1...A9 are a supplement for a variable-length
8309 argument list. If trace_redisplay_p is non-zero also printf the
8310 resulting string to stderr. */
8311
8312 static void
8313 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
8314 struct window *w;
8315 char *fmt;
8316 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
8317 {
8318 char buffer[512];
8319 char *method = w->desired_matrix->method;
8320 int len = strlen (method);
8321 int size = sizeof w->desired_matrix->method;
8322 int remaining = size - len - 1;
8323
8324 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
8325 if (len && remaining)
8326 {
8327 method[len] = '|';
8328 --remaining, ++len;
8329 }
8330
8331 strncpy (method + len, buffer, remaining);
8332
8333 if (trace_redisplay_p)
8334 fprintf (stderr, "%p (%s): %s\n",
8335 w,
8336 ((BUFFERP (w->buffer)
8337 && STRINGP (XBUFFER (w->buffer)->name))
8338 ? (char *) SDATA (XBUFFER (w->buffer)->name)
8339 : "no buffer"),
8340 buffer);
8341 }
8342
8343 #endif /* GLYPH_DEBUG */
8344
8345
8346 /* Value is non-zero if all changes in window W, which displays
8347 current_buffer, are in the text between START and END. START is a
8348 buffer position, END is given as a distance from Z. Used in
8349 redisplay_internal for display optimization. */
8350
8351 static INLINE int
8352 text_outside_line_unchanged_p (w, start, end)
8353 struct window *w;
8354 int start, end;
8355 {
8356 int unchanged_p = 1;
8357
8358 /* If text or overlays have changed, see where. */
8359 if (XFASTINT (w->last_modified) < MODIFF
8360 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8361 {
8362 /* Gap in the line? */
8363 if (GPT < start || Z - GPT < end)
8364 unchanged_p = 0;
8365
8366 /* Changes start in front of the line, or end after it? */
8367 if (unchanged_p
8368 && (BEG_UNCHANGED < start - 1
8369 || END_UNCHANGED < end))
8370 unchanged_p = 0;
8371
8372 /* If selective display, can't optimize if changes start at the
8373 beginning of the line. */
8374 if (unchanged_p
8375 && INTEGERP (current_buffer->selective_display)
8376 && XINT (current_buffer->selective_display) > 0
8377 && (BEG_UNCHANGED < start || GPT <= start))
8378 unchanged_p = 0;
8379
8380 /* If there are overlays at the start or end of the line, these
8381 may have overlay strings with newlines in them. A change at
8382 START, for instance, may actually concern the display of such
8383 overlay strings as well, and they are displayed on different
8384 lines. So, quickly rule out this case. (For the future, it
8385 might be desirable to implement something more telling than
8386 just BEG/END_UNCHANGED.) */
8387 if (unchanged_p)
8388 {
8389 if (BEG + BEG_UNCHANGED == start
8390 && overlay_touches_p (start))
8391 unchanged_p = 0;
8392 if (END_UNCHANGED == end
8393 && overlay_touches_p (Z - end))
8394 unchanged_p = 0;
8395 }
8396 }
8397
8398 return unchanged_p;
8399 }
8400
8401
8402 /* Do a frame update, taking possible shortcuts into account. This is
8403 the main external entry point for redisplay.
8404
8405 If the last redisplay displayed an echo area message and that message
8406 is no longer requested, we clear the echo area or bring back the
8407 mini-buffer if that is in use. */
8408
8409 void
8410 redisplay ()
8411 {
8412 redisplay_internal (0);
8413 }
8414
8415
8416 /* Return 1 if point moved out of or into a composition. Otherwise
8417 return 0. PREV_BUF and PREV_PT are the last point buffer and
8418 position. BUF and PT are the current point buffer and position. */
8419
8420 int
8421 check_point_in_composition (prev_buf, prev_pt, buf, pt)
8422 struct buffer *prev_buf, *buf;
8423 int prev_pt, pt;
8424 {
8425 int start, end;
8426 Lisp_Object prop;
8427 Lisp_Object buffer;
8428
8429 XSETBUFFER (buffer, buf);
8430 /* Check a composition at the last point if point moved within the
8431 same buffer. */
8432 if (prev_buf == buf)
8433 {
8434 if (prev_pt == pt)
8435 /* Point didn't move. */
8436 return 0;
8437
8438 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
8439 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
8440 && COMPOSITION_VALID_P (start, end, prop)
8441 && start < prev_pt && end > prev_pt)
8442 /* The last point was within the composition. Return 1 iff
8443 point moved out of the composition. */
8444 return (pt <= start || pt >= end);
8445 }
8446
8447 /* Check a composition at the current point. */
8448 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
8449 && find_composition (pt, -1, &start, &end, &prop, buffer)
8450 && COMPOSITION_VALID_P (start, end, prop)
8451 && start < pt && end > pt);
8452 }
8453
8454
8455 /* Reconsider the setting of B->clip_changed which is displayed
8456 in window W. */
8457
8458 static INLINE void
8459 reconsider_clip_changes (w, b)
8460 struct window *w;
8461 struct buffer *b;
8462 {
8463 if (b->clip_changed
8464 && !NILP (w->window_end_valid)
8465 && w->current_matrix->buffer == b
8466 && w->current_matrix->zv == BUF_ZV (b)
8467 && w->current_matrix->begv == BUF_BEGV (b))
8468 b->clip_changed = 0;
8469
8470 /* If display wasn't paused, and W is not a tool bar window, see if
8471 point has been moved into or out of a composition. In that case,
8472 we set b->clip_changed to 1 to force updating the screen. If
8473 b->clip_changed has already been set to 1, we can skip this
8474 check. */
8475 if (!b->clip_changed
8476 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
8477 {
8478 int pt;
8479
8480 if (w == XWINDOW (selected_window))
8481 pt = BUF_PT (current_buffer);
8482 else
8483 pt = marker_position (w->pointm);
8484
8485 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
8486 || pt != XINT (w->last_point))
8487 && check_point_in_composition (w->current_matrix->buffer,
8488 XINT (w->last_point),
8489 XBUFFER (w->buffer), pt))
8490 b->clip_changed = 1;
8491 }
8492 }
8493 \f
8494 #define STOP_POLLING \
8495 do { if (! polling_stopped_here) stop_polling (); \
8496 polling_stopped_here = 1; } while (0)
8497
8498 #define RESUME_POLLING \
8499 do { if (polling_stopped_here) start_polling (); \
8500 polling_stopped_here = 0; } while (0)
8501
8502
8503 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
8504 response to any user action; therefore, we should preserve the echo
8505 area. (Actually, our caller does that job.) Perhaps in the future
8506 avoid recentering windows if it is not necessary; currently that
8507 causes some problems. */
8508
8509 static void
8510 redisplay_internal (preserve_echo_area)
8511 int preserve_echo_area;
8512 {
8513 struct window *w = XWINDOW (selected_window);
8514 struct frame *f = XFRAME (w->frame);
8515 int pause;
8516 int must_finish = 0;
8517 struct text_pos tlbufpos, tlendpos;
8518 int number_of_visible_frames;
8519 int count;
8520 struct frame *sf = SELECTED_FRAME ();
8521 int polling_stopped_here = 0;
8522
8523 /* Non-zero means redisplay has to consider all windows on all
8524 frames. Zero means, only selected_window is considered. */
8525 int consider_all_windows_p;
8526
8527 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
8528
8529 /* No redisplay if running in batch mode or frame is not yet fully
8530 initialized, or redisplay is explicitly turned off by setting
8531 Vinhibit_redisplay. */
8532 if (noninteractive
8533 || !NILP (Vinhibit_redisplay)
8534 || !f->glyphs_initialized_p)
8535 return;
8536
8537 /* The flag redisplay_performed_directly_p is set by
8538 direct_output_for_insert when it already did the whole screen
8539 update necessary. */
8540 if (redisplay_performed_directly_p)
8541 {
8542 redisplay_performed_directly_p = 0;
8543 if (!hscroll_windows (selected_window))
8544 return;
8545 }
8546
8547 #ifdef USE_X_TOOLKIT
8548 if (popup_activated ())
8549 return;
8550 #endif
8551
8552 /* I don't think this happens but let's be paranoid. */
8553 if (redisplaying_p)
8554 return;
8555
8556 /* Record a function that resets redisplaying_p to its old value
8557 when we leave this function. */
8558 count = SPECPDL_INDEX ();
8559 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
8560 ++redisplaying_p;
8561 specbind (Qinhibit_free_realized_faces, Qnil);
8562
8563 retry:
8564 pause = 0;
8565 reconsider_clip_changes (w, current_buffer);
8566
8567 /* If new fonts have been loaded that make a glyph matrix adjustment
8568 necessary, do it. */
8569 if (fonts_changed_p)
8570 {
8571 adjust_glyphs (NULL);
8572 ++windows_or_buffers_changed;
8573 fonts_changed_p = 0;
8574 }
8575
8576 /* If face_change_count is non-zero, init_iterator will free all
8577 realized faces, which includes the faces referenced from current
8578 matrices. So, we can't reuse current matrices in this case. */
8579 if (face_change_count)
8580 ++windows_or_buffers_changed;
8581
8582 if (! FRAME_WINDOW_P (sf)
8583 && previous_terminal_frame != sf)
8584 {
8585 /* Since frames on an ASCII terminal share the same display
8586 area, displaying a different frame means redisplay the whole
8587 thing. */
8588 windows_or_buffers_changed++;
8589 SET_FRAME_GARBAGED (sf);
8590 XSETFRAME (Vterminal_frame, sf);
8591 }
8592 previous_terminal_frame = sf;
8593
8594 /* Set the visible flags for all frames. Do this before checking
8595 for resized or garbaged frames; they want to know if their frames
8596 are visible. See the comment in frame.h for
8597 FRAME_SAMPLE_VISIBILITY. */
8598 {
8599 Lisp_Object tail, frame;
8600
8601 number_of_visible_frames = 0;
8602
8603 FOR_EACH_FRAME (tail, frame)
8604 {
8605 struct frame *f = XFRAME (frame);
8606
8607 FRAME_SAMPLE_VISIBILITY (f);
8608 if (FRAME_VISIBLE_P (f))
8609 ++number_of_visible_frames;
8610 clear_desired_matrices (f);
8611 }
8612 }
8613
8614 /* Notice any pending interrupt request to change frame size. */
8615 do_pending_window_change (1);
8616
8617 /* Clear frames marked as garbaged. */
8618 if (frame_garbaged)
8619 clear_garbaged_frames ();
8620
8621 /* Build menubar and tool-bar items. */
8622 prepare_menu_bars ();
8623
8624 if (windows_or_buffers_changed)
8625 update_mode_lines++;
8626
8627 /* Detect case that we need to write or remove a star in the mode line. */
8628 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
8629 {
8630 w->update_mode_line = Qt;
8631 if (buffer_shared > 1)
8632 update_mode_lines++;
8633 }
8634
8635 /* If %c is in the mode line, update it if needed. */
8636 if (!NILP (w->column_number_displayed)
8637 /* This alternative quickly identifies a common case
8638 where no change is needed. */
8639 && !(PT == XFASTINT (w->last_point)
8640 && XFASTINT (w->last_modified) >= MODIFF
8641 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
8642 && (XFASTINT (w->column_number_displayed)
8643 != (int) current_column ())) /* iftc */
8644 w->update_mode_line = Qt;
8645
8646 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
8647
8648 /* The variable buffer_shared is set in redisplay_window and
8649 indicates that we redisplay a buffer in different windows. See
8650 there. */
8651 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
8652 || cursor_type_changed);
8653
8654 /* If specs for an arrow have changed, do thorough redisplay
8655 to ensure we remove any arrow that should no longer exist. */
8656 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
8657 || ! EQ (Voverlay_arrow_string, last_arrow_string))
8658 consider_all_windows_p = windows_or_buffers_changed = 1;
8659
8660 /* Normally the message* functions will have already displayed and
8661 updated the echo area, but the frame may have been trashed, or
8662 the update may have been preempted, so display the echo area
8663 again here. Checking message_cleared_p captures the case that
8664 the echo area should be cleared. */
8665 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
8666 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
8667 || (message_cleared_p
8668 && minibuf_level == 0
8669 /* If the mini-window is currently selected, this means the
8670 echo-area doesn't show through. */
8671 && !MINI_WINDOW_P (XWINDOW (selected_window))))
8672 {
8673 int window_height_changed_p = echo_area_display (0);
8674 must_finish = 1;
8675
8676 /* If we don't display the current message, don't clear the
8677 message_cleared_p flag, because, if we did, we wouldn't clear
8678 the echo area in the next redisplay which doesn't preserve
8679 the echo area. */
8680 if (!display_last_displayed_message_p)
8681 message_cleared_p = 0;
8682
8683 if (fonts_changed_p)
8684 goto retry;
8685 else if (window_height_changed_p)
8686 {
8687 consider_all_windows_p = 1;
8688 ++update_mode_lines;
8689 ++windows_or_buffers_changed;
8690
8691 /* If window configuration was changed, frames may have been
8692 marked garbaged. Clear them or we will experience
8693 surprises wrt scrolling. */
8694 if (frame_garbaged)
8695 clear_garbaged_frames ();
8696 }
8697 }
8698 else if (EQ (selected_window, minibuf_window)
8699 && (current_buffer->clip_changed
8700 || XFASTINT (w->last_modified) < MODIFF
8701 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8702 && resize_mini_window (w, 0))
8703 {
8704 /* Resized active mini-window to fit the size of what it is
8705 showing if its contents might have changed. */
8706 must_finish = 1;
8707 consider_all_windows_p = 1;
8708 ++windows_or_buffers_changed;
8709 ++update_mode_lines;
8710
8711 /* If window configuration was changed, frames may have been
8712 marked garbaged. Clear them or we will experience
8713 surprises wrt scrolling. */
8714 if (frame_garbaged)
8715 clear_garbaged_frames ();
8716 }
8717
8718
8719 /* If showing the region, and mark has changed, we must redisplay
8720 the whole window. The assignment to this_line_start_pos prevents
8721 the optimization directly below this if-statement. */
8722 if (((!NILP (Vtransient_mark_mode)
8723 && !NILP (XBUFFER (w->buffer)->mark_active))
8724 != !NILP (w->region_showing))
8725 || (!NILP (w->region_showing)
8726 && !EQ (w->region_showing,
8727 Fmarker_position (XBUFFER (w->buffer)->mark))))
8728 CHARPOS (this_line_start_pos) = 0;
8729
8730 /* Optimize the case that only the line containing the cursor in the
8731 selected window has changed. Variables starting with this_ are
8732 set in display_line and record information about the line
8733 containing the cursor. */
8734 tlbufpos = this_line_start_pos;
8735 tlendpos = this_line_end_pos;
8736 if (!consider_all_windows_p
8737 && CHARPOS (tlbufpos) > 0
8738 && NILP (w->update_mode_line)
8739 && !current_buffer->clip_changed
8740 && !current_buffer->prevent_redisplay_optimizations_p
8741 && FRAME_VISIBLE_P (XFRAME (w->frame))
8742 && !FRAME_OBSCURED_P (XFRAME (w->frame))
8743 /* Make sure recorded data applies to current buffer, etc. */
8744 && this_line_buffer == current_buffer
8745 && current_buffer == XBUFFER (w->buffer)
8746 && NILP (w->force_start)
8747 && NILP (w->optional_new_start)
8748 /* Point must be on the line that we have info recorded about. */
8749 && PT >= CHARPOS (tlbufpos)
8750 && PT <= Z - CHARPOS (tlendpos)
8751 /* All text outside that line, including its final newline,
8752 must be unchanged */
8753 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
8754 CHARPOS (tlendpos)))
8755 {
8756 if (CHARPOS (tlbufpos) > BEGV
8757 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
8758 && (CHARPOS (tlbufpos) == ZV
8759 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
8760 /* Former continuation line has disappeared by becoming empty */
8761 goto cancel;
8762 else if (XFASTINT (w->last_modified) < MODIFF
8763 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
8764 || MINI_WINDOW_P (w))
8765 {
8766 /* We have to handle the case of continuation around a
8767 wide-column character (See the comment in indent.c around
8768 line 885).
8769
8770 For instance, in the following case:
8771
8772 -------- Insert --------
8773 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
8774 J_I_ ==> J_I_ `^^' are cursors.
8775 ^^ ^^
8776 -------- --------
8777
8778 As we have to redraw the line above, we should goto cancel. */
8779
8780 struct it it;
8781 int line_height_before = this_line_pixel_height;
8782
8783 /* Note that start_display will handle the case that the
8784 line starting at tlbufpos is a continuation lines. */
8785 start_display (&it, w, tlbufpos);
8786
8787 /* Implementation note: It this still necessary? */
8788 if (it.current_x != this_line_start_x)
8789 goto cancel;
8790
8791 TRACE ((stderr, "trying display optimization 1\n"));
8792 w->cursor.vpos = -1;
8793 overlay_arrow_seen = 0;
8794 it.vpos = this_line_vpos;
8795 it.current_y = this_line_y;
8796 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
8797 display_line (&it);
8798
8799 /* If line contains point, is not continued,
8800 and ends at same distance from eob as before, we win */
8801 if (w->cursor.vpos >= 0
8802 /* Line is not continued, otherwise this_line_start_pos
8803 would have been set to 0 in display_line. */
8804 && CHARPOS (this_line_start_pos)
8805 /* Line ends as before. */
8806 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
8807 /* Line has same height as before. Otherwise other lines
8808 would have to be shifted up or down. */
8809 && this_line_pixel_height == line_height_before)
8810 {
8811 /* If this is not the window's last line, we must adjust
8812 the charstarts of the lines below. */
8813 if (it.current_y < it.last_visible_y)
8814 {
8815 struct glyph_row *row
8816 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
8817 int delta, delta_bytes;
8818
8819 if (Z - CHARPOS (tlendpos) == ZV)
8820 {
8821 /* This line ends at end of (accessible part of)
8822 buffer. There is no newline to count. */
8823 delta = (Z
8824 - CHARPOS (tlendpos)
8825 - MATRIX_ROW_START_CHARPOS (row));
8826 delta_bytes = (Z_BYTE
8827 - BYTEPOS (tlendpos)
8828 - MATRIX_ROW_START_BYTEPOS (row));
8829 }
8830 else
8831 {
8832 /* This line ends in a newline. Must take
8833 account of the newline and the rest of the
8834 text that follows. */
8835 delta = (Z
8836 - CHARPOS (tlendpos)
8837 - MATRIX_ROW_START_CHARPOS (row));
8838 delta_bytes = (Z_BYTE
8839 - BYTEPOS (tlendpos)
8840 - MATRIX_ROW_START_BYTEPOS (row));
8841 }
8842
8843 increment_matrix_positions (w->current_matrix,
8844 this_line_vpos + 1,
8845 w->current_matrix->nrows,
8846 delta, delta_bytes);
8847 }
8848
8849 /* If this row displays text now but previously didn't,
8850 or vice versa, w->window_end_vpos may have to be
8851 adjusted. */
8852 if ((it.glyph_row - 1)->displays_text_p)
8853 {
8854 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
8855 XSETINT (w->window_end_vpos, this_line_vpos);
8856 }
8857 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
8858 && this_line_vpos > 0)
8859 XSETINT (w->window_end_vpos, this_line_vpos - 1);
8860 w->window_end_valid = Qnil;
8861
8862 /* Update hint: No need to try to scroll in update_window. */
8863 w->desired_matrix->no_scrolling_p = 1;
8864
8865 #if GLYPH_DEBUG
8866 *w->desired_matrix->method = 0;
8867 debug_method_add (w, "optimization 1");
8868 #endif
8869 goto update;
8870 }
8871 else
8872 goto cancel;
8873 }
8874 else if (/* Cursor position hasn't changed. */
8875 PT == XFASTINT (w->last_point)
8876 /* Make sure the cursor was last displayed
8877 in this window. Otherwise we have to reposition it. */
8878 && 0 <= w->cursor.vpos
8879 && XINT (w->height) > w->cursor.vpos)
8880 {
8881 if (!must_finish)
8882 {
8883 do_pending_window_change (1);
8884
8885 /* We used to always goto end_of_redisplay here, but this
8886 isn't enough if we have a blinking cursor. */
8887 if (w->cursor_off_p == w->last_cursor_off_p)
8888 goto end_of_redisplay;
8889 }
8890 goto update;
8891 }
8892 /* If highlighting the region, or if the cursor is in the echo area,
8893 then we can't just move the cursor. */
8894 else if (! (!NILP (Vtransient_mark_mode)
8895 && !NILP (current_buffer->mark_active))
8896 && (EQ (selected_window, current_buffer->last_selected_window)
8897 || highlight_nonselected_windows)
8898 && NILP (w->region_showing)
8899 && NILP (Vshow_trailing_whitespace)
8900 && !cursor_in_echo_area)
8901 {
8902 struct it it;
8903 struct glyph_row *row;
8904
8905 /* Skip from tlbufpos to PT and see where it is. Note that
8906 PT may be in invisible text. If so, we will end at the
8907 next visible position. */
8908 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
8909 NULL, DEFAULT_FACE_ID);
8910 it.current_x = this_line_start_x;
8911 it.current_y = this_line_y;
8912 it.vpos = this_line_vpos;
8913
8914 /* The call to move_it_to stops in front of PT, but
8915 moves over before-strings. */
8916 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
8917
8918 if (it.vpos == this_line_vpos
8919 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
8920 row->enabled_p))
8921 {
8922 xassert (this_line_vpos == it.vpos);
8923 xassert (this_line_y == it.current_y);
8924 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
8925 #if GLYPH_DEBUG
8926 *w->desired_matrix->method = 0;
8927 debug_method_add (w, "optimization 3");
8928 #endif
8929 goto update;
8930 }
8931 else
8932 goto cancel;
8933 }
8934
8935 cancel:
8936 /* Text changed drastically or point moved off of line. */
8937 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
8938 }
8939
8940 CHARPOS (this_line_start_pos) = 0;
8941 consider_all_windows_p |= buffer_shared > 1;
8942 ++clear_face_cache_count;
8943
8944
8945 /* Build desired matrices, and update the display. If
8946 consider_all_windows_p is non-zero, do it for all windows on all
8947 frames. Otherwise do it for selected_window, only. */
8948
8949 if (consider_all_windows_p)
8950 {
8951 Lisp_Object tail, frame;
8952 int i, n = 0, size = 50;
8953 struct frame **updated
8954 = (struct frame **) alloca (size * sizeof *updated);
8955
8956 /* Clear the face cache eventually. */
8957 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
8958 {
8959 clear_face_cache (0);
8960 clear_face_cache_count = 0;
8961 }
8962
8963 /* Recompute # windows showing selected buffer. This will be
8964 incremented each time such a window is displayed. */
8965 buffer_shared = 0;
8966
8967 FOR_EACH_FRAME (tail, frame)
8968 {
8969 struct frame *f = XFRAME (frame);
8970
8971 if (FRAME_WINDOW_P (f) || f == sf)
8972 {
8973 #ifdef HAVE_WINDOW_SYSTEM
8974 if (clear_face_cache_count % 50 == 0
8975 && FRAME_WINDOW_P (f))
8976 clear_image_cache (f, 0);
8977 #endif /* HAVE_WINDOW_SYSTEM */
8978
8979 /* Mark all the scroll bars to be removed; we'll redeem
8980 the ones we want when we redisplay their windows. */
8981 if (condemn_scroll_bars_hook)
8982 condemn_scroll_bars_hook (f);
8983
8984 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8985 redisplay_windows (FRAME_ROOT_WINDOW (f));
8986
8987 /* Any scroll bars which redisplay_windows should have
8988 nuked should now go away. */
8989 if (judge_scroll_bars_hook)
8990 judge_scroll_bars_hook (f);
8991
8992 /* If fonts changed, display again. */
8993 /* ??? rms: I suspect it is a mistake to jump all the way
8994 back to retry here. It should just retry this frame. */
8995 if (fonts_changed_p)
8996 goto retry;
8997
8998 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8999 {
9000 /* See if we have to hscroll. */
9001 if (hscroll_windows (f->root_window))
9002 goto retry;
9003
9004 /* Prevent various kinds of signals during display
9005 update. stdio is not robust about handling
9006 signals, which can cause an apparent I/O
9007 error. */
9008 if (interrupt_input)
9009 unrequest_sigio ();
9010 STOP_POLLING;
9011
9012 /* Update the display. */
9013 set_window_update_flags (XWINDOW (f->root_window), 1);
9014 pause |= update_frame (f, 0, 0);
9015 if (pause)
9016 break;
9017
9018 if (n == size)
9019 {
9020 int nbytes = size * sizeof *updated;
9021 struct frame **p = (struct frame **) alloca (2 * nbytes);
9022 bcopy (updated, p, nbytes);
9023 size *= 2;
9024 }
9025
9026 updated[n++] = f;
9027 }
9028 }
9029 }
9030
9031 /* Do the mark_window_display_accurate after all windows have
9032 been redisplayed because this call resets flags in buffers
9033 which are needed for proper redisplay. */
9034 for (i = 0; i < n; ++i)
9035 {
9036 struct frame *f = updated[i];
9037 mark_window_display_accurate (f->root_window, 1);
9038 if (frame_up_to_date_hook)
9039 frame_up_to_date_hook (f);
9040 }
9041 }
9042 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
9043 {
9044 Lisp_Object mini_window;
9045 struct frame *mini_frame;
9046
9047 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
9048 /* Use list_of_error, not Qerror, so that
9049 we catch only errors and don't run the debugger. */
9050 internal_condition_case_1 (redisplay_window_1, selected_window,
9051 list_of_error,
9052 redisplay_window_error);
9053
9054 /* Compare desired and current matrices, perform output. */
9055
9056 update:
9057 /* If fonts changed, display again. */
9058 if (fonts_changed_p)
9059 goto retry;
9060
9061 /* Prevent various kinds of signals during display update.
9062 stdio is not robust about handling signals,
9063 which can cause an apparent I/O error. */
9064 if (interrupt_input)
9065 unrequest_sigio ();
9066 STOP_POLLING;
9067
9068 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
9069 {
9070 if (hscroll_windows (selected_window))
9071 goto retry;
9072
9073 XWINDOW (selected_window)->must_be_updated_p = 1;
9074 pause = update_frame (sf, 0, 0);
9075 }
9076
9077 /* We may have called echo_area_display at the top of this
9078 function. If the echo area is on another frame, that may
9079 have put text on a frame other than the selected one, so the
9080 above call to update_frame would not have caught it. Catch
9081 it here. */
9082 mini_window = FRAME_MINIBUF_WINDOW (sf);
9083 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9084
9085 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
9086 {
9087 XWINDOW (mini_window)->must_be_updated_p = 1;
9088 pause |= update_frame (mini_frame, 0, 0);
9089 if (!pause && hscroll_windows (mini_window))
9090 goto retry;
9091 }
9092 }
9093
9094 /* If display was paused because of pending input, make sure we do a
9095 thorough update the next time. */
9096 if (pause)
9097 {
9098 /* Prevent the optimization at the beginning of
9099 redisplay_internal that tries a single-line update of the
9100 line containing the cursor in the selected window. */
9101 CHARPOS (this_line_start_pos) = 0;
9102
9103 /* Let the overlay arrow be updated the next time. */
9104 if (!NILP (last_arrow_position))
9105 {
9106 last_arrow_position = Qt;
9107 last_arrow_string = Qt;
9108 }
9109
9110 /* If we pause after scrolling, some rows in the current
9111 matrices of some windows are not valid. */
9112 if (!WINDOW_FULL_WIDTH_P (w)
9113 && !FRAME_WINDOW_P (XFRAME (w->frame)))
9114 update_mode_lines = 1;
9115 }
9116 else
9117 {
9118 if (!consider_all_windows_p)
9119 {
9120 /* This has already been done above if
9121 consider_all_windows_p is set. */
9122 mark_window_display_accurate_1 (w, 1);
9123
9124 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
9125 last_arrow_string = Voverlay_arrow_string;
9126
9127 if (frame_up_to_date_hook != 0)
9128 frame_up_to_date_hook (sf);
9129 }
9130
9131 update_mode_lines = 0;
9132 windows_or_buffers_changed = 0;
9133 cursor_type_changed = 0;
9134 }
9135
9136 /* Start SIGIO interrupts coming again. Having them off during the
9137 code above makes it less likely one will discard output, but not
9138 impossible, since there might be stuff in the system buffer here.
9139 But it is much hairier to try to do anything about that. */
9140 if (interrupt_input)
9141 request_sigio ();
9142 RESUME_POLLING;
9143
9144 /* If a frame has become visible which was not before, redisplay
9145 again, so that we display it. Expose events for such a frame
9146 (which it gets when becoming visible) don't call the parts of
9147 redisplay constructing glyphs, so simply exposing a frame won't
9148 display anything in this case. So, we have to display these
9149 frames here explicitly. */
9150 if (!pause)
9151 {
9152 Lisp_Object tail, frame;
9153 int new_count = 0;
9154
9155 FOR_EACH_FRAME (tail, frame)
9156 {
9157 int this_is_visible = 0;
9158
9159 if (XFRAME (frame)->visible)
9160 this_is_visible = 1;
9161 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
9162 if (XFRAME (frame)->visible)
9163 this_is_visible = 1;
9164
9165 if (this_is_visible)
9166 new_count++;
9167 }
9168
9169 if (new_count != number_of_visible_frames)
9170 windows_or_buffers_changed++;
9171 }
9172
9173 /* Change frame size now if a change is pending. */
9174 do_pending_window_change (1);
9175
9176 /* If we just did a pending size change, or have additional
9177 visible frames, redisplay again. */
9178 if (windows_or_buffers_changed && !pause)
9179 goto retry;
9180
9181 end_of_redisplay:
9182 unbind_to (count, Qnil);
9183 RESUME_POLLING;
9184 }
9185
9186
9187 /* Redisplay, but leave alone any recent echo area message unless
9188 another message has been requested in its place.
9189
9190 This is useful in situations where you need to redisplay but no
9191 user action has occurred, making it inappropriate for the message
9192 area to be cleared. See tracking_off and
9193 wait_reading_process_input for examples of these situations.
9194
9195 FROM_WHERE is an integer saying from where this function was
9196 called. This is useful for debugging. */
9197
9198 void
9199 redisplay_preserve_echo_area (from_where)
9200 int from_where;
9201 {
9202 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
9203
9204 if (!NILP (echo_area_buffer[1]))
9205 {
9206 /* We have a previously displayed message, but no current
9207 message. Redisplay the previous message. */
9208 display_last_displayed_message_p = 1;
9209 redisplay_internal (1);
9210 display_last_displayed_message_p = 0;
9211 }
9212 else
9213 redisplay_internal (1);
9214 }
9215
9216
9217 /* Function registered with record_unwind_protect in
9218 redisplay_internal. Reset redisplaying_p to the value it had
9219 before redisplay_internal was called, and clear
9220 prevent_freeing_realized_faces_p. */
9221
9222 static Lisp_Object
9223 unwind_redisplay (old_redisplaying_p)
9224 Lisp_Object old_redisplaying_p;
9225 {
9226 redisplaying_p = XFASTINT (old_redisplaying_p);
9227 return Qnil;
9228 }
9229
9230
9231 /* Mark the display of window W as accurate or inaccurate. If
9232 ACCURATE_P is non-zero mark display of W as accurate. If
9233 ACCURATE_P is zero, arrange for W to be redisplayed the next time
9234 redisplay_internal is called. */
9235
9236 static void
9237 mark_window_display_accurate_1 (w, accurate_p)
9238 struct window *w;
9239 int accurate_p;
9240 {
9241 if (BUFFERP (w->buffer))
9242 {
9243 struct buffer *b = XBUFFER (w->buffer);
9244
9245 w->last_modified
9246 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
9247 w->last_overlay_modified
9248 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
9249 w->last_had_star
9250 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
9251
9252 if (accurate_p)
9253 {
9254 b->clip_changed = 0;
9255 b->prevent_redisplay_optimizations_p = 0;
9256
9257 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
9258 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
9259 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
9260 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
9261
9262 w->current_matrix->buffer = b;
9263 w->current_matrix->begv = BUF_BEGV (b);
9264 w->current_matrix->zv = BUF_ZV (b);
9265
9266 w->last_cursor = w->cursor;
9267 w->last_cursor_off_p = w->cursor_off_p;
9268
9269 if (w == XWINDOW (selected_window))
9270 w->last_point = make_number (BUF_PT (b));
9271 else
9272 w->last_point = make_number (XMARKER (w->pointm)->charpos);
9273 }
9274 }
9275
9276 if (accurate_p)
9277 {
9278 w->window_end_valid = w->buffer;
9279 #if 0 /* This is incorrect with variable-height lines. */
9280 xassert (XINT (w->window_end_vpos)
9281 < (XINT (w->height)
9282 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
9283 #endif
9284 w->update_mode_line = Qnil;
9285 }
9286 }
9287
9288
9289 /* Mark the display of windows in the window tree rooted at WINDOW as
9290 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
9291 windows as accurate. If ACCURATE_P is zero, arrange for windows to
9292 be redisplayed the next time redisplay_internal is called. */
9293
9294 void
9295 mark_window_display_accurate (window, accurate_p)
9296 Lisp_Object window;
9297 int accurate_p;
9298 {
9299 struct window *w;
9300
9301 for (; !NILP (window); window = w->next)
9302 {
9303 w = XWINDOW (window);
9304 mark_window_display_accurate_1 (w, accurate_p);
9305
9306 if (!NILP (w->vchild))
9307 mark_window_display_accurate (w->vchild, accurate_p);
9308 if (!NILP (w->hchild))
9309 mark_window_display_accurate (w->hchild, accurate_p);
9310 }
9311
9312 if (accurate_p)
9313 {
9314 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
9315 last_arrow_string = Voverlay_arrow_string;
9316 }
9317 else
9318 {
9319 /* Force a thorough redisplay the next time by setting
9320 last_arrow_position and last_arrow_string to t, which is
9321 unequal to any useful value of Voverlay_arrow_... */
9322 last_arrow_position = Qt;
9323 last_arrow_string = Qt;
9324 }
9325 }
9326
9327
9328 /* Return value in display table DP (Lisp_Char_Table *) for character
9329 C. Since a display table doesn't have any parent, we don't have to
9330 follow parent. Do not call this function directly but use the
9331 macro DISP_CHAR_VECTOR. */
9332
9333 Lisp_Object
9334 disp_char_vector (dp, c)
9335 struct Lisp_Char_Table *dp;
9336 int c;
9337 {
9338 int code[4], i;
9339 Lisp_Object val;
9340
9341 if (SINGLE_BYTE_CHAR_P (c))
9342 return (dp->contents[c]);
9343
9344 SPLIT_CHAR (c, code[0], code[1], code[2]);
9345 if (code[1] < 32)
9346 code[1] = -1;
9347 else if (code[2] < 32)
9348 code[2] = -1;
9349
9350 /* Here, the possible range of code[0] (== charset ID) is
9351 128..max_charset. Since the top level char table contains data
9352 for multibyte characters after 256th element, we must increment
9353 code[0] by 128 to get a correct index. */
9354 code[0] += 128;
9355 code[3] = -1; /* anchor */
9356
9357 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
9358 {
9359 val = dp->contents[code[i]];
9360 if (!SUB_CHAR_TABLE_P (val))
9361 return (NILP (val) ? dp->defalt : val);
9362 }
9363
9364 /* Here, val is a sub char table. We return the default value of
9365 it. */
9366 return (dp->defalt);
9367 }
9368
9369
9370 \f
9371 /***********************************************************************
9372 Window Redisplay
9373 ***********************************************************************/
9374
9375 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
9376
9377 static void
9378 redisplay_windows (window)
9379 Lisp_Object window;
9380 {
9381 while (!NILP (window))
9382 {
9383 struct window *w = XWINDOW (window);
9384
9385 if (!NILP (w->hchild))
9386 redisplay_windows (w->hchild);
9387 else if (!NILP (w->vchild))
9388 redisplay_windows (w->vchild);
9389 else
9390 {
9391 displayed_buffer = XBUFFER (w->buffer);
9392 /* Use list_of_error, not Qerror, so that
9393 we catch only errors and don't run the debugger. */
9394 internal_condition_case_1 (redisplay_window_0, window,
9395 list_of_error,
9396 redisplay_window_error);
9397 }
9398
9399 window = w->next;
9400 }
9401 }
9402
9403 static Lisp_Object
9404 redisplay_window_error ()
9405 {
9406 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
9407 return Qnil;
9408 }
9409
9410 static Lisp_Object
9411 redisplay_window_0 (window)
9412 Lisp_Object window;
9413 {
9414 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
9415 redisplay_window (window, 0);
9416 return Qnil;
9417 }
9418
9419 static Lisp_Object
9420 redisplay_window_1 (window)
9421 Lisp_Object window;
9422 {
9423 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
9424 redisplay_window (window, 1);
9425 return Qnil;
9426 }
9427 \f
9428 /* Set cursor position of W. PT is assumed to be displayed in ROW.
9429 DELTA is the number of bytes by which positions recorded in ROW
9430 differ from current buffer positions. */
9431
9432 void
9433 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
9434 struct window *w;
9435 struct glyph_row *row;
9436 struct glyph_matrix *matrix;
9437 int delta, delta_bytes, dy, dvpos;
9438 {
9439 struct glyph *glyph = row->glyphs[TEXT_AREA];
9440 struct glyph *end = glyph + row->used[TEXT_AREA];
9441 int x = row->x;
9442 int pt_old = PT - delta;
9443
9444 /* Skip over glyphs not having an object at the start of the row.
9445 These are special glyphs like truncation marks on terminal
9446 frames. */
9447 if (row->displays_text_p)
9448 while (glyph < end
9449 && INTEGERP (glyph->object)
9450 && glyph->charpos < 0)
9451 {
9452 x += glyph->pixel_width;
9453 ++glyph;
9454 }
9455
9456 while (glyph < end
9457 && !INTEGERP (glyph->object)
9458 && (!BUFFERP (glyph->object)
9459 || glyph->charpos < pt_old))
9460 {
9461 x += glyph->pixel_width;
9462 ++glyph;
9463 }
9464
9465 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
9466 w->cursor.x = x;
9467 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
9468 w->cursor.y = row->y + dy;
9469
9470 if (w == XWINDOW (selected_window))
9471 {
9472 if (!row->continued_p
9473 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
9474 && row->x == 0)
9475 {
9476 this_line_buffer = XBUFFER (w->buffer);
9477
9478 CHARPOS (this_line_start_pos)
9479 = MATRIX_ROW_START_CHARPOS (row) + delta;
9480 BYTEPOS (this_line_start_pos)
9481 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
9482
9483 CHARPOS (this_line_end_pos)
9484 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
9485 BYTEPOS (this_line_end_pos)
9486 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
9487
9488 this_line_y = w->cursor.y;
9489 this_line_pixel_height = row->height;
9490 this_line_vpos = w->cursor.vpos;
9491 this_line_start_x = row->x;
9492 }
9493 else
9494 CHARPOS (this_line_start_pos) = 0;
9495 }
9496 }
9497
9498
9499 /* Run window scroll functions, if any, for WINDOW with new window
9500 start STARTP. Sets the window start of WINDOW to that position.
9501
9502 We assume that the window's buffer is really current. */
9503
9504 static INLINE struct text_pos
9505 run_window_scroll_functions (window, startp)
9506 Lisp_Object window;
9507 struct text_pos startp;
9508 {
9509 struct window *w = XWINDOW (window);
9510 SET_MARKER_FROM_TEXT_POS (w->start, startp);
9511
9512 if (current_buffer != XBUFFER (w->buffer))
9513 abort ();
9514
9515 if (!NILP (Vwindow_scroll_functions))
9516 {
9517 run_hook_with_args_2 (Qwindow_scroll_functions, window,
9518 make_number (CHARPOS (startp)));
9519 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9520 /* In case the hook functions switch buffers. */
9521 if (current_buffer != XBUFFER (w->buffer))
9522 set_buffer_internal_1 (XBUFFER (w->buffer));
9523 }
9524
9525 return startp;
9526 }
9527
9528
9529 /* Make sure the line containing the cursor is fully visible.
9530 A value of 1 means there is nothing to be done.
9531 (Either the line is fully visible, or it cannot be made so,
9532 or we cannot tell.)
9533 A value of 0 means the caller should do scrolling
9534 as if point had gone off the screen. */
9535
9536 static int
9537 make_cursor_line_fully_visible (w)
9538 struct window *w;
9539 {
9540 struct glyph_matrix *matrix;
9541 struct glyph_row *row;
9542 int window_height;
9543
9544 /* It's not always possible to find the cursor, e.g, when a window
9545 is full of overlay strings. Don't do anything in that case. */
9546 if (w->cursor.vpos < 0)
9547 return 1;
9548
9549 matrix = w->desired_matrix;
9550 row = MATRIX_ROW (matrix, w->cursor.vpos);
9551
9552 /* If the cursor row is not partially visible, there's nothing to do. */
9553 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
9554 return 1;
9555
9556 /* If the row the cursor is in is taller than the window's height,
9557 it's not clear what to do, so do nothing. */
9558 window_height = window_box_height (w);
9559 if (row->height >= window_height)
9560 return 1;
9561
9562 return 0;
9563
9564 #if 0
9565 /* This code used to try to scroll the window just enough to make
9566 the line visible. It returned 0 to say that the caller should
9567 allocate larger glyph matrices. */
9568
9569 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
9570 {
9571 int dy = row->height - row->visible_height;
9572 w->vscroll = 0;
9573 w->cursor.y += dy;
9574 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9575 }
9576 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
9577 {
9578 int dy = - (row->height - row->visible_height);
9579 w->vscroll = dy;
9580 w->cursor.y += dy;
9581 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9582 }
9583
9584 /* When we change the cursor y-position of the selected window,
9585 change this_line_y as well so that the display optimization for
9586 the cursor line of the selected window in redisplay_internal uses
9587 the correct y-position. */
9588 if (w == XWINDOW (selected_window))
9589 this_line_y = w->cursor.y;
9590
9591 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
9592 redisplay with larger matrices. */
9593 if (matrix->nrows < required_matrix_height (w))
9594 {
9595 fonts_changed_p = 1;
9596 return 0;
9597 }
9598
9599 return 1;
9600 #endif /* 0 */
9601 }
9602
9603
9604 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
9605 non-zero means only WINDOW is redisplayed in redisplay_internal.
9606 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
9607 in redisplay_window to bring a partially visible line into view in
9608 the case that only the cursor has moved.
9609
9610 Value is
9611
9612 1 if scrolling succeeded
9613
9614 0 if scrolling didn't find point.
9615
9616 -1 if new fonts have been loaded so that we must interrupt
9617 redisplay, adjust glyph matrices, and try again. */
9618
9619 enum
9620 {
9621 SCROLLING_SUCCESS,
9622 SCROLLING_FAILED,
9623 SCROLLING_NEED_LARGER_MATRICES
9624 };
9625
9626 static int
9627 try_scrolling (window, just_this_one_p, scroll_conservatively,
9628 scroll_step, temp_scroll_step)
9629 Lisp_Object window;
9630 int just_this_one_p;
9631 EMACS_INT scroll_conservatively, scroll_step;
9632 int temp_scroll_step;
9633 {
9634 struct window *w = XWINDOW (window);
9635 struct frame *f = XFRAME (w->frame);
9636 struct text_pos scroll_margin_pos;
9637 struct text_pos pos;
9638 struct text_pos startp;
9639 struct it it;
9640 Lisp_Object window_end;
9641 int this_scroll_margin;
9642 int dy = 0;
9643 int scroll_max;
9644 int rc;
9645 int amount_to_scroll = 0;
9646 Lisp_Object aggressive;
9647 int height;
9648
9649 #if GLYPH_DEBUG
9650 debug_method_add (w, "try_scrolling");
9651 #endif
9652
9653 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9654
9655 /* Compute scroll margin height in pixels. We scroll when point is
9656 within this distance from the top or bottom of the window. */
9657 if (scroll_margin > 0)
9658 {
9659 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
9660 this_scroll_margin *= CANON_Y_UNIT (f);
9661 }
9662 else
9663 this_scroll_margin = 0;
9664
9665 /* Compute how much we should try to scroll maximally to bring point
9666 into view. */
9667 if (scroll_step || scroll_conservatively || temp_scroll_step)
9668 scroll_max = max (scroll_step,
9669 max (scroll_conservatively, temp_scroll_step));
9670 else if (NUMBERP (current_buffer->scroll_down_aggressively)
9671 || NUMBERP (current_buffer->scroll_up_aggressively))
9672 /* We're trying to scroll because of aggressive scrolling
9673 but no scroll_step is set. Choose an arbitrary one. Maybe
9674 there should be a variable for this. */
9675 scroll_max = 10;
9676 else
9677 scroll_max = 0;
9678 scroll_max *= CANON_Y_UNIT (f);
9679
9680 /* Decide whether we have to scroll down. Start at the window end
9681 and move this_scroll_margin up to find the position of the scroll
9682 margin. */
9683 window_end = Fwindow_end (window, Qt);
9684 CHARPOS (scroll_margin_pos) = XINT (window_end);
9685 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
9686 if (this_scroll_margin)
9687 {
9688 start_display (&it, w, scroll_margin_pos);
9689 move_it_vertically (&it, - this_scroll_margin);
9690 scroll_margin_pos = it.current.pos;
9691 }
9692
9693 if (PT >= CHARPOS (scroll_margin_pos))
9694 {
9695 int y0;
9696
9697 too_near_end:
9698 /* Point is in the scroll margin at the bottom of the window, or
9699 below. Compute a new window start that makes point visible. */
9700
9701 /* Compute the distance from the scroll margin to PT.
9702 Give up if the distance is greater than scroll_max. */
9703 start_display (&it, w, scroll_margin_pos);
9704 y0 = it.current_y;
9705 move_it_to (&it, PT, 0, it.last_visible_y, -1,
9706 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9707
9708 /* To make point visible, we have to move the window start
9709 down so that the line the cursor is in is visible, which
9710 means we have to add in the height of the cursor line. */
9711 dy = line_bottom_y (&it) - y0;
9712
9713 if (dy > scroll_max)
9714 return SCROLLING_FAILED;
9715
9716 /* Move the window start down. If scrolling conservatively,
9717 move it just enough down to make point visible. If
9718 scroll_step is set, move it down by scroll_step. */
9719 start_display (&it, w, startp);
9720
9721 if (scroll_conservatively)
9722 amount_to_scroll
9723 = max (max (dy, CANON_Y_UNIT (f)),
9724 CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
9725 else if (scroll_step || temp_scroll_step)
9726 amount_to_scroll = scroll_max;
9727 else
9728 {
9729 aggressive = current_buffer->scroll_up_aggressively;
9730 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
9731 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
9732 if (NUMBERP (aggressive))
9733 amount_to_scroll = XFLOATINT (aggressive) * height;
9734 }
9735
9736 if (amount_to_scroll <= 0)
9737 return SCROLLING_FAILED;
9738
9739 move_it_vertically (&it, amount_to_scroll);
9740 startp = it.current.pos;
9741 }
9742 else
9743 {
9744 /* See if point is inside the scroll margin at the top of the
9745 window. */
9746 scroll_margin_pos = startp;
9747 if (this_scroll_margin)
9748 {
9749 start_display (&it, w, startp);
9750 move_it_vertically (&it, this_scroll_margin);
9751 scroll_margin_pos = it.current.pos;
9752 }
9753
9754 if (PT < CHARPOS (scroll_margin_pos))
9755 {
9756 /* Point is in the scroll margin at the top of the window or
9757 above what is displayed in the window. */
9758 int y0;
9759
9760 /* Compute the vertical distance from PT to the scroll
9761 margin position. Give up if distance is greater than
9762 scroll_max. */
9763 SET_TEXT_POS (pos, PT, PT_BYTE);
9764 start_display (&it, w, pos);
9765 y0 = it.current_y;
9766 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
9767 it.last_visible_y, -1,
9768 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9769 dy = it.current_y - y0;
9770 if (dy > scroll_max)
9771 return SCROLLING_FAILED;
9772
9773 /* Compute new window start. */
9774 start_display (&it, w, startp);
9775
9776 if (scroll_conservatively)
9777 amount_to_scroll =
9778 max (dy, CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
9779 else if (scroll_step || temp_scroll_step)
9780 amount_to_scroll = scroll_max;
9781 else
9782 {
9783 aggressive = current_buffer->scroll_down_aggressively;
9784 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
9785 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
9786 if (NUMBERP (aggressive))
9787 amount_to_scroll = XFLOATINT (aggressive) * height;
9788 }
9789
9790 if (amount_to_scroll <= 0)
9791 return SCROLLING_FAILED;
9792
9793 move_it_vertically (&it, - amount_to_scroll);
9794 startp = it.current.pos;
9795 }
9796 }
9797
9798 /* Run window scroll functions. */
9799 startp = run_window_scroll_functions (window, startp);
9800
9801 /* Display the window. Give up if new fonts are loaded, or if point
9802 doesn't appear. */
9803 if (!try_window (window, startp))
9804 rc = SCROLLING_NEED_LARGER_MATRICES;
9805 else if (w->cursor.vpos < 0)
9806 {
9807 clear_glyph_matrix (w->desired_matrix);
9808 rc = SCROLLING_FAILED;
9809 }
9810 else
9811 {
9812 /* Maybe forget recorded base line for line number display. */
9813 if (!just_this_one_p
9814 || current_buffer->clip_changed
9815 || BEG_UNCHANGED < CHARPOS (startp))
9816 w->base_line_number = Qnil;
9817
9818 /* If cursor ends up on a partially visible line,
9819 treat that as being off the bottom of the screen. */
9820 if (! make_cursor_line_fully_visible (w))
9821 {
9822 clear_glyph_matrix (w->desired_matrix);
9823 goto too_near_end;
9824 }
9825 rc = SCROLLING_SUCCESS;
9826 }
9827
9828 return rc;
9829 }
9830
9831
9832 /* Compute a suitable window start for window W if display of W starts
9833 on a continuation line. Value is non-zero if a new window start
9834 was computed.
9835
9836 The new window start will be computed, based on W's width, starting
9837 from the start of the continued line. It is the start of the
9838 screen line with the minimum distance from the old start W->start. */
9839
9840 static int
9841 compute_window_start_on_continuation_line (w)
9842 struct window *w;
9843 {
9844 struct text_pos pos, start_pos;
9845 int window_start_changed_p = 0;
9846
9847 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
9848
9849 /* If window start is on a continuation line... Window start may be
9850 < BEGV in case there's invisible text at the start of the
9851 buffer (M-x rmail, for example). */
9852 if (CHARPOS (start_pos) > BEGV
9853 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
9854 {
9855 struct it it;
9856 struct glyph_row *row;
9857
9858 /* Handle the case that the window start is out of range. */
9859 if (CHARPOS (start_pos) < BEGV)
9860 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
9861 else if (CHARPOS (start_pos) > ZV)
9862 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
9863
9864 /* Find the start of the continued line. This should be fast
9865 because scan_buffer is fast (newline cache). */
9866 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
9867 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
9868 row, DEFAULT_FACE_ID);
9869 reseat_at_previous_visible_line_start (&it);
9870
9871 /* If the line start is "too far" away from the window start,
9872 say it takes too much time to compute a new window start. */
9873 if (CHARPOS (start_pos) - IT_CHARPOS (it)
9874 < XFASTINT (w->height) * XFASTINT (w->width))
9875 {
9876 int min_distance, distance;
9877
9878 /* Move forward by display lines to find the new window
9879 start. If window width was enlarged, the new start can
9880 be expected to be > the old start. If window width was
9881 decreased, the new window start will be < the old start.
9882 So, we're looking for the display line start with the
9883 minimum distance from the old window start. */
9884 pos = it.current.pos;
9885 min_distance = INFINITY;
9886 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
9887 distance < min_distance)
9888 {
9889 min_distance = distance;
9890 pos = it.current.pos;
9891 move_it_by_lines (&it, 1, 0);
9892 }
9893
9894 /* Set the window start there. */
9895 SET_MARKER_FROM_TEXT_POS (w->start, pos);
9896 window_start_changed_p = 1;
9897 }
9898 }
9899
9900 return window_start_changed_p;
9901 }
9902
9903
9904 /* Try cursor movement in case text has not changed in window WINDOW,
9905 with window start STARTP. Value is
9906
9907 CURSOR_MOVEMENT_SUCCESS if successful
9908
9909 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
9910
9911 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
9912 display. *SCROLL_STEP is set to 1, under certain circumstances, if
9913 we want to scroll as if scroll-step were set to 1. See the code.
9914
9915 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
9916 which case we have to abort this redisplay, and adjust matrices
9917 first. */
9918
9919 enum
9920 {
9921 CURSOR_MOVEMENT_SUCCESS,
9922 CURSOR_MOVEMENT_CANNOT_BE_USED,
9923 CURSOR_MOVEMENT_MUST_SCROLL,
9924 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
9925 };
9926
9927 static int
9928 try_cursor_movement (window, startp, scroll_step)
9929 Lisp_Object window;
9930 struct text_pos startp;
9931 int *scroll_step;
9932 {
9933 struct window *w = XWINDOW (window);
9934 struct frame *f = XFRAME (w->frame);
9935 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
9936
9937 #if GLYPH_DEBUG
9938 if (inhibit_try_cursor_movement)
9939 return rc;
9940 #endif
9941
9942 /* Handle case where text has not changed, only point, and it has
9943 not moved off the frame. */
9944 if (/* Point may be in this window. */
9945 PT >= CHARPOS (startp)
9946 /* Selective display hasn't changed. */
9947 && !current_buffer->clip_changed
9948 /* Function force-mode-line-update is used to force a thorough
9949 redisplay. It sets either windows_or_buffers_changed or
9950 update_mode_lines. So don't take a shortcut here for these
9951 cases. */
9952 && !update_mode_lines
9953 && !windows_or_buffers_changed
9954 && !cursor_type_changed
9955 /* Can't use this case if highlighting a region. When a
9956 region exists, cursor movement has to do more than just
9957 set the cursor. */
9958 && !(!NILP (Vtransient_mark_mode)
9959 && !NILP (current_buffer->mark_active))
9960 && NILP (w->region_showing)
9961 && NILP (Vshow_trailing_whitespace)
9962 /* Right after splitting windows, last_point may be nil. */
9963 && INTEGERP (w->last_point)
9964 /* This code is not used for mini-buffer for the sake of the case
9965 of redisplaying to replace an echo area message; since in
9966 that case the mini-buffer contents per se are usually
9967 unchanged. This code is of no real use in the mini-buffer
9968 since the handling of this_line_start_pos, etc., in redisplay
9969 handles the same cases. */
9970 && !EQ (window, minibuf_window)
9971 /* When splitting windows or for new windows, it happens that
9972 redisplay is called with a nil window_end_vpos or one being
9973 larger than the window. This should really be fixed in
9974 window.c. I don't have this on my list, now, so we do
9975 approximately the same as the old redisplay code. --gerd. */
9976 && INTEGERP (w->window_end_vpos)
9977 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
9978 && (FRAME_WINDOW_P (f)
9979 || !MARKERP (Voverlay_arrow_position)
9980 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
9981 {
9982 int this_scroll_margin;
9983 struct glyph_row *row = NULL;
9984
9985 #if GLYPH_DEBUG
9986 debug_method_add (w, "cursor movement");
9987 #endif
9988
9989 /* Scroll if point within this distance from the top or bottom
9990 of the window. This is a pixel value. */
9991 this_scroll_margin = max (0, scroll_margin);
9992 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
9993 this_scroll_margin *= CANON_Y_UNIT (f);
9994
9995 /* Start with the row the cursor was displayed during the last
9996 not paused redisplay. Give up if that row is not valid. */
9997 if (w->last_cursor.vpos < 0
9998 || w->last_cursor.vpos >= w->current_matrix->nrows)
9999 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10000 else
10001 {
10002 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
10003 if (row->mode_line_p)
10004 ++row;
10005 if (!row->enabled_p)
10006 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10007 }
10008
10009 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
10010 {
10011 int scroll_p = 0;
10012 int last_y = window_text_bottom_y (w) - this_scroll_margin;
10013
10014 if (PT > XFASTINT (w->last_point))
10015 {
10016 /* Point has moved forward. */
10017 while (MATRIX_ROW_END_CHARPOS (row) < PT
10018 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
10019 {
10020 xassert (row->enabled_p);
10021 ++row;
10022 }
10023
10024 /* The end position of a row equals the start position
10025 of the next row. If PT is there, we would rather
10026 display it in the next line. */
10027 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
10028 && MATRIX_ROW_END_CHARPOS (row) == PT
10029 && !cursor_row_p (w, row))
10030 ++row;
10031
10032 /* If within the scroll margin, scroll. Note that
10033 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
10034 the next line would be drawn, and that
10035 this_scroll_margin can be zero. */
10036 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
10037 || PT > MATRIX_ROW_END_CHARPOS (row)
10038 /* Line is completely visible last line in window
10039 and PT is to be set in the next line. */
10040 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
10041 && PT == MATRIX_ROW_END_CHARPOS (row)
10042 && !row->ends_at_zv_p
10043 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
10044 scroll_p = 1;
10045 }
10046 else if (PT < XFASTINT (w->last_point))
10047 {
10048 /* Cursor has to be moved backward. Note that PT >=
10049 CHARPOS (startp) because of the outer
10050 if-statement. */
10051 while (!row->mode_line_p
10052 && (MATRIX_ROW_START_CHARPOS (row) > PT
10053 || (MATRIX_ROW_START_CHARPOS (row) == PT
10054 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
10055 && (row->y > this_scroll_margin
10056 || CHARPOS (startp) == BEGV))
10057 {
10058 xassert (row->enabled_p);
10059 --row;
10060 }
10061
10062 /* Consider the following case: Window starts at BEGV,
10063 there is invisible, intangible text at BEGV, so that
10064 display starts at some point START > BEGV. It can
10065 happen that we are called with PT somewhere between
10066 BEGV and START. Try to handle that case. */
10067 if (row < w->current_matrix->rows
10068 || row->mode_line_p)
10069 {
10070 row = w->current_matrix->rows;
10071 if (row->mode_line_p)
10072 ++row;
10073 }
10074
10075 /* Due to newlines in overlay strings, we may have to
10076 skip forward over overlay strings. */
10077 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
10078 && MATRIX_ROW_END_CHARPOS (row) == PT
10079 && !cursor_row_p (w, row))
10080 ++row;
10081
10082 /* If within the scroll margin, scroll. */
10083 if (row->y < this_scroll_margin
10084 && CHARPOS (startp) != BEGV)
10085 scroll_p = 1;
10086 }
10087
10088 if (PT < MATRIX_ROW_START_CHARPOS (row)
10089 || PT > MATRIX_ROW_END_CHARPOS (row))
10090 {
10091 /* if PT is not in the glyph row, give up. */
10092 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10093 }
10094 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
10095 {
10096 if (PT == MATRIX_ROW_END_CHARPOS (row)
10097 && !row->ends_at_zv_p
10098 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
10099 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10100 else if (row->height > window_box_height (w))
10101 {
10102 /* If we end up in a partially visible line, let's
10103 make it fully visible, except when it's taller
10104 than the window, in which case we can't do much
10105 about it. */
10106 *scroll_step = 1;
10107 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10108 }
10109 else
10110 {
10111 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10112 try_window (window, startp);
10113 if (!make_cursor_line_fully_visible (w))
10114 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10115 else
10116 rc = CURSOR_MOVEMENT_SUCCESS;
10117 }
10118 }
10119 else if (scroll_p)
10120 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10121 else
10122 {
10123 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10124 rc = CURSOR_MOVEMENT_SUCCESS;
10125 }
10126 }
10127 }
10128
10129 return rc;
10130 }
10131
10132
10133 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
10134 selected_window is redisplayed.
10135
10136 We can return without actually redisplaying the window if
10137 fonts_changed_p is nonzero. In that case, redisplay_internal will
10138 retry. */
10139
10140 static void
10141 redisplay_window (window, just_this_one_p)
10142 Lisp_Object window;
10143 int just_this_one_p;
10144 {
10145 struct window *w = XWINDOW (window);
10146 struct frame *f = XFRAME (w->frame);
10147 struct buffer *buffer = XBUFFER (w->buffer);
10148 struct buffer *old = current_buffer;
10149 struct text_pos lpoint, opoint, startp;
10150 int update_mode_line;
10151 int tem;
10152 struct it it;
10153 /* Record it now because it's overwritten. */
10154 int current_matrix_up_to_date_p = 0;
10155 /* This is less strict than current_matrix_up_to_date_p.
10156 It indictes that the buffer contents and narrowing are unchanged. */
10157 int buffer_unchanged_p = 0;
10158 int temp_scroll_step = 0;
10159 int count = SPECPDL_INDEX ();
10160 int rc;
10161 int centering_position;
10162
10163 SET_TEXT_POS (lpoint, PT, PT_BYTE);
10164 opoint = lpoint;
10165
10166 /* W must be a leaf window here. */
10167 xassert (!NILP (w->buffer));
10168 #if GLYPH_DEBUG
10169 *w->desired_matrix->method = 0;
10170 #endif
10171
10172 specbind (Qinhibit_point_motion_hooks, Qt);
10173
10174 reconsider_clip_changes (w, buffer);
10175
10176 /* Has the mode line to be updated? */
10177 update_mode_line = (!NILP (w->update_mode_line)
10178 || update_mode_lines
10179 || buffer->clip_changed
10180 || buffer->prevent_redisplay_optimizations_p);
10181
10182 if (MINI_WINDOW_P (w))
10183 {
10184 if (w == XWINDOW (echo_area_window)
10185 && !NILP (echo_area_buffer[0]))
10186 {
10187 if (update_mode_line)
10188 /* We may have to update a tty frame's menu bar or a
10189 tool-bar. Example `M-x C-h C-h C-g'. */
10190 goto finish_menu_bars;
10191 else
10192 /* We've already displayed the echo area glyphs in this window. */
10193 goto finish_scroll_bars;
10194 }
10195 else if (w != XWINDOW (minibuf_window))
10196 {
10197 /* W is a mini-buffer window, but it's not the currently
10198 active one, so clear it. */
10199 int yb = window_text_bottom_y (w);
10200 struct glyph_row *row;
10201 int y;
10202
10203 for (y = 0, row = w->desired_matrix->rows;
10204 y < yb;
10205 y += row->height, ++row)
10206 blank_row (w, row, y);
10207 goto finish_scroll_bars;
10208 }
10209
10210 clear_glyph_matrix (w->desired_matrix);
10211 }
10212
10213 /* Otherwise set up data on this window; select its buffer and point
10214 value. */
10215 /* Really select the buffer, for the sake of buffer-local
10216 variables. */
10217 set_buffer_internal_1 (XBUFFER (w->buffer));
10218 SET_TEXT_POS (opoint, PT, PT_BYTE);
10219
10220 current_matrix_up_to_date_p
10221 = (!NILP (w->window_end_valid)
10222 && !current_buffer->clip_changed
10223 && !current_buffer->prevent_redisplay_optimizations_p
10224 && XFASTINT (w->last_modified) >= MODIFF
10225 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
10226
10227 buffer_unchanged_p
10228 = (!NILP (w->window_end_valid)
10229 && !current_buffer->clip_changed
10230 && XFASTINT (w->last_modified) >= MODIFF
10231 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
10232
10233 /* When windows_or_buffers_changed is non-zero, we can't rely on
10234 the window end being valid, so set it to nil there. */
10235 if (windows_or_buffers_changed)
10236 {
10237 /* If window starts on a continuation line, maybe adjust the
10238 window start in case the window's width changed. */
10239 if (XMARKER (w->start)->buffer == current_buffer)
10240 compute_window_start_on_continuation_line (w);
10241
10242 w->window_end_valid = Qnil;
10243 }
10244
10245 /* Some sanity checks. */
10246 CHECK_WINDOW_END (w);
10247 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
10248 abort ();
10249 if (BYTEPOS (opoint) < CHARPOS (opoint))
10250 abort ();
10251
10252 /* If %c is in mode line, update it if needed. */
10253 if (!NILP (w->column_number_displayed)
10254 /* This alternative quickly identifies a common case
10255 where no change is needed. */
10256 && !(PT == XFASTINT (w->last_point)
10257 && XFASTINT (w->last_modified) >= MODIFF
10258 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
10259 && (XFASTINT (w->column_number_displayed)
10260 != (int) current_column ())) /* iftc */
10261 update_mode_line = 1;
10262
10263 /* Count number of windows showing the selected buffer. An indirect
10264 buffer counts as its base buffer. */
10265 if (!just_this_one_p)
10266 {
10267 struct buffer *current_base, *window_base;
10268 current_base = current_buffer;
10269 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
10270 if (current_base->base_buffer)
10271 current_base = current_base->base_buffer;
10272 if (window_base->base_buffer)
10273 window_base = window_base->base_buffer;
10274 if (current_base == window_base)
10275 buffer_shared++;
10276 }
10277
10278 /* Point refers normally to the selected window. For any other
10279 window, set up appropriate value. */
10280 if (!EQ (window, selected_window))
10281 {
10282 int new_pt = XMARKER (w->pointm)->charpos;
10283 int new_pt_byte = marker_byte_position (w->pointm);
10284 if (new_pt < BEGV)
10285 {
10286 new_pt = BEGV;
10287 new_pt_byte = BEGV_BYTE;
10288 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
10289 }
10290 else if (new_pt > (ZV - 1))
10291 {
10292 new_pt = ZV;
10293 new_pt_byte = ZV_BYTE;
10294 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
10295 }
10296
10297 /* We don't use SET_PT so that the point-motion hooks don't run. */
10298 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
10299 }
10300
10301 /* If any of the character widths specified in the display table
10302 have changed, invalidate the width run cache. It's true that
10303 this may be a bit late to catch such changes, but the rest of
10304 redisplay goes (non-fatally) haywire when the display table is
10305 changed, so why should we worry about doing any better? */
10306 if (current_buffer->width_run_cache)
10307 {
10308 struct Lisp_Char_Table *disptab = buffer_display_table ();
10309
10310 if (! disptab_matches_widthtab (disptab,
10311 XVECTOR (current_buffer->width_table)))
10312 {
10313 invalidate_region_cache (current_buffer,
10314 current_buffer->width_run_cache,
10315 BEG, Z);
10316 recompute_width_table (current_buffer, disptab);
10317 }
10318 }
10319
10320 /* If window-start is screwed up, choose a new one. */
10321 if (XMARKER (w->start)->buffer != current_buffer)
10322 goto recenter;
10323
10324 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10325
10326 /* If someone specified a new starting point but did not insist,
10327 check whether it can be used. */
10328 if (!NILP (w->optional_new_start)
10329 && CHARPOS (startp) >= BEGV
10330 && CHARPOS (startp) <= ZV)
10331 {
10332 w->optional_new_start = Qnil;
10333 start_display (&it, w, startp);
10334 move_it_to (&it, PT, 0, it.last_visible_y, -1,
10335 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
10336 if (IT_CHARPOS (it) == PT)
10337 w->force_start = Qt;
10338 }
10339
10340 /* Handle case where place to start displaying has been specified,
10341 unless the specified location is outside the accessible range. */
10342 if (!NILP (w->force_start)
10343 || w->frozen_window_start_p)
10344 {
10345 /* We set this later on if we have to adjust point. */
10346 int new_vpos = -1;
10347
10348 w->force_start = Qnil;
10349 w->vscroll = 0;
10350 w->window_end_valid = Qnil;
10351
10352 /* Forget any recorded base line for line number display. */
10353 if (!buffer_unchanged_p)
10354 w->base_line_number = Qnil;
10355
10356 /* Redisplay the mode line. Select the buffer properly for that.
10357 Also, run the hook window-scroll-functions
10358 because we have scrolled. */
10359 /* Note, we do this after clearing force_start because
10360 if there's an error, it is better to forget about force_start
10361 than to get into an infinite loop calling the hook functions
10362 and having them get more errors. */
10363 if (!update_mode_line
10364 || ! NILP (Vwindow_scroll_functions))
10365 {
10366 update_mode_line = 1;
10367 w->update_mode_line = Qt;
10368 startp = run_window_scroll_functions (window, startp);
10369 }
10370
10371 w->last_modified = make_number (0);
10372 w->last_overlay_modified = make_number (0);
10373 if (CHARPOS (startp) < BEGV)
10374 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
10375 else if (CHARPOS (startp) > ZV)
10376 SET_TEXT_POS (startp, ZV, ZV_BYTE);
10377
10378 /* Redisplay, then check if cursor has been set during the
10379 redisplay. Give up if new fonts were loaded. */
10380 if (!try_window (window, startp))
10381 {
10382 w->force_start = Qt;
10383 clear_glyph_matrix (w->desired_matrix);
10384 goto need_larger_matrices;
10385 }
10386
10387 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
10388 {
10389 /* If point does not appear, try to move point so it does
10390 appear. The desired matrix has been built above, so we
10391 can use it here. */
10392 new_vpos = window_box_height (w) / 2;
10393 }
10394
10395 if (!make_cursor_line_fully_visible (w))
10396 {
10397 /* Point does appear, but on a line partly visible at end of window.
10398 Move it back to a fully-visible line. */
10399 new_vpos = window_box_height (w);
10400 }
10401
10402 /* If we need to move point for either of the above reasons,
10403 now actually do it. */
10404 if (new_vpos >= 0)
10405 {
10406 struct glyph_row *row;
10407
10408 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
10409 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
10410 ++row;
10411
10412 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
10413 MATRIX_ROW_START_BYTEPOS (row));
10414
10415 if (w != XWINDOW (selected_window))
10416 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
10417 else if (current_buffer == old)
10418 SET_TEXT_POS (lpoint, PT, PT_BYTE);
10419
10420 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
10421
10422 /* If we are highlighting the region, then we just changed
10423 the region, so redisplay to show it. */
10424 if (!NILP (Vtransient_mark_mode)
10425 && !NILP (current_buffer->mark_active))
10426 {
10427 clear_glyph_matrix (w->desired_matrix);
10428 if (!try_window (window, startp))
10429 goto need_larger_matrices;
10430 }
10431 }
10432
10433 #if GLYPH_DEBUG
10434 debug_method_add (w, "forced window start");
10435 #endif
10436 goto done;
10437 }
10438
10439 /* Handle case where text has not changed, only point, and it has
10440 not moved off the frame, and we are not retrying after hscroll.
10441 (current_matrix_up_to_date_p is nonzero when retrying.) */
10442 if (current_matrix_up_to_date_p
10443 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
10444 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
10445 {
10446 switch (rc)
10447 {
10448 case CURSOR_MOVEMENT_SUCCESS:
10449 goto done;
10450
10451 #if 0 /* try_cursor_movement never returns this value. */
10452 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
10453 goto need_larger_matrices;
10454 #endif
10455
10456 case CURSOR_MOVEMENT_MUST_SCROLL:
10457 goto try_to_scroll;
10458
10459 default:
10460 abort ();
10461 }
10462 }
10463 /* If current starting point was originally the beginning of a line
10464 but no longer is, find a new starting point. */
10465 else if (!NILP (w->start_at_line_beg)
10466 && !(CHARPOS (startp) <= BEGV
10467 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
10468 {
10469 #if GLYPH_DEBUG
10470 debug_method_add (w, "recenter 1");
10471 #endif
10472 goto recenter;
10473 }
10474
10475 /* Try scrolling with try_window_id. Value is > 0 if update has
10476 been done, it is -1 if we know that the same window start will
10477 not work. It is 0 if unsuccessful for some other reason. */
10478 else if ((tem = try_window_id (w)) != 0)
10479 {
10480 #if GLYPH_DEBUG
10481 debug_method_add (w, "try_window_id %d", tem);
10482 #endif
10483
10484 if (fonts_changed_p)
10485 goto need_larger_matrices;
10486 if (tem > 0)
10487 goto done;
10488
10489 /* Otherwise try_window_id has returned -1 which means that we
10490 don't want the alternative below this comment to execute. */
10491 }
10492 else if (CHARPOS (startp) >= BEGV
10493 && CHARPOS (startp) <= ZV
10494 && PT >= CHARPOS (startp)
10495 && (CHARPOS (startp) < ZV
10496 /* Avoid starting at end of buffer. */
10497 || CHARPOS (startp) == BEGV
10498 || (XFASTINT (w->last_modified) >= MODIFF
10499 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
10500 {
10501 #if GLYPH_DEBUG
10502 debug_method_add (w, "same window start");
10503 #endif
10504
10505 /* Try to redisplay starting at same place as before.
10506 If point has not moved off frame, accept the results. */
10507 if (!current_matrix_up_to_date_p
10508 /* Don't use try_window_reusing_current_matrix in this case
10509 because a window scroll function can have changed the
10510 buffer. */
10511 || !NILP (Vwindow_scroll_functions)
10512 || MINI_WINDOW_P (w)
10513 || !try_window_reusing_current_matrix (w))
10514 {
10515 IF_DEBUG (debug_method_add (w, "1"));
10516 try_window (window, startp);
10517 }
10518
10519 if (fonts_changed_p)
10520 goto need_larger_matrices;
10521
10522 if (w->cursor.vpos >= 0)
10523 {
10524 if (!just_this_one_p
10525 || current_buffer->clip_changed
10526 || BEG_UNCHANGED < CHARPOS (startp))
10527 /* Forget any recorded base line for line number display. */
10528 w->base_line_number = Qnil;
10529
10530 if (!make_cursor_line_fully_visible (w))
10531 clear_glyph_matrix (w->desired_matrix);
10532 /* Drop through and scroll. */
10533 else
10534 goto done;
10535 }
10536 else
10537 clear_glyph_matrix (w->desired_matrix);
10538 }
10539
10540 try_to_scroll:
10541
10542 w->last_modified = make_number (0);
10543 w->last_overlay_modified = make_number (0);
10544
10545 /* Redisplay the mode line. Select the buffer properly for that. */
10546 if (!update_mode_line)
10547 {
10548 update_mode_line = 1;
10549 w->update_mode_line = Qt;
10550 }
10551
10552 /* Try to scroll by specified few lines. */
10553 if ((scroll_conservatively
10554 || scroll_step
10555 || temp_scroll_step
10556 || NUMBERP (current_buffer->scroll_up_aggressively)
10557 || NUMBERP (current_buffer->scroll_down_aggressively))
10558 && !current_buffer->clip_changed
10559 && CHARPOS (startp) >= BEGV
10560 && CHARPOS (startp) <= ZV)
10561 {
10562 /* The function returns -1 if new fonts were loaded, 1 if
10563 successful, 0 if not successful. */
10564 int rc = try_scrolling (window, just_this_one_p,
10565 scroll_conservatively,
10566 scroll_step,
10567 temp_scroll_step);
10568 switch (rc)
10569 {
10570 case SCROLLING_SUCCESS:
10571 goto done;
10572
10573 case SCROLLING_NEED_LARGER_MATRICES:
10574 goto need_larger_matrices;
10575
10576 case SCROLLING_FAILED:
10577 break;
10578
10579 default:
10580 abort ();
10581 }
10582 }
10583
10584 /* Finally, just choose place to start which centers point */
10585
10586 recenter:
10587 centering_position = window_box_height (w) / 2;
10588
10589 point_at_top:
10590 /* Jump here with centering_position already set to 0. */
10591
10592 #if GLYPH_DEBUG
10593 debug_method_add (w, "recenter");
10594 #endif
10595
10596 /* w->vscroll = 0; */
10597
10598 /* Forget any previously recorded base line for line number display. */
10599 if (!buffer_unchanged_p)
10600 w->base_line_number = Qnil;
10601
10602 /* Move backward half the height of the window. */
10603 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10604 it.current_y = it.last_visible_y;
10605 move_it_vertically_backward (&it, centering_position);
10606 xassert (IT_CHARPOS (it) >= BEGV);
10607
10608 /* The function move_it_vertically_backward may move over more
10609 than the specified y-distance. If it->w is small, e.g. a
10610 mini-buffer window, we may end up in front of the window's
10611 display area. Start displaying at the start of the line
10612 containing PT in this case. */
10613 if (it.current_y <= 0)
10614 {
10615 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10616 move_it_vertically (&it, 0);
10617 xassert (IT_CHARPOS (it) <= PT);
10618 it.current_y = 0;
10619 }
10620
10621 it.current_x = it.hpos = 0;
10622
10623 /* Set startp here explicitly in case that helps avoid an infinite loop
10624 in case the window-scroll-functions functions get errors. */
10625 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
10626
10627 /* Run scroll hooks. */
10628 startp = run_window_scroll_functions (window, it.current.pos);
10629
10630 /* Redisplay the window. */
10631 if (!current_matrix_up_to_date_p
10632 || windows_or_buffers_changed
10633 || cursor_type_changed
10634 /* Don't use try_window_reusing_current_matrix in this case
10635 because it can have changed the buffer. */
10636 || !NILP (Vwindow_scroll_functions)
10637 || !just_this_one_p
10638 || MINI_WINDOW_P (w)
10639 || !try_window_reusing_current_matrix (w))
10640 try_window (window, startp);
10641
10642 /* If new fonts have been loaded (due to fontsets), give up. We
10643 have to start a new redisplay since we need to re-adjust glyph
10644 matrices. */
10645 if (fonts_changed_p)
10646 goto need_larger_matrices;
10647
10648 /* If cursor did not appear assume that the middle of the window is
10649 in the first line of the window. Do it again with the next line.
10650 (Imagine a window of height 100, displaying two lines of height
10651 60. Moving back 50 from it->last_visible_y will end in the first
10652 line.) */
10653 if (w->cursor.vpos < 0)
10654 {
10655 if (!NILP (w->window_end_valid)
10656 && PT >= Z - XFASTINT (w->window_end_pos))
10657 {
10658 clear_glyph_matrix (w->desired_matrix);
10659 move_it_by_lines (&it, 1, 0);
10660 try_window (window, it.current.pos);
10661 }
10662 else if (PT < IT_CHARPOS (it))
10663 {
10664 clear_glyph_matrix (w->desired_matrix);
10665 move_it_by_lines (&it, -1, 0);
10666 try_window (window, it.current.pos);
10667 }
10668 else
10669 {
10670 /* Not much we can do about it. */
10671 }
10672 }
10673
10674 /* Consider the following case: Window starts at BEGV, there is
10675 invisible, intangible text at BEGV, so that display starts at
10676 some point START > BEGV. It can happen that we are called with
10677 PT somewhere between BEGV and START. Try to handle that case. */
10678 if (w->cursor.vpos < 0)
10679 {
10680 struct glyph_row *row = w->current_matrix->rows;
10681 if (row->mode_line_p)
10682 ++row;
10683 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10684 }
10685
10686 if (!make_cursor_line_fully_visible (w))
10687 {
10688 /* If centering point failed to make the whole line visible,
10689 put point at the top instead. That has to make the whole line
10690 visible, if it can be done. */
10691 centering_position = 0;
10692 goto point_at_top;
10693 }
10694
10695 done:
10696
10697 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10698 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
10699 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
10700 ? Qt : Qnil);
10701
10702 /* Display the mode line, if we must. */
10703 if ((update_mode_line
10704 /* If window not full width, must redo its mode line
10705 if (a) the window to its side is being redone and
10706 (b) we do a frame-based redisplay. This is a consequence
10707 of how inverted lines are drawn in frame-based redisplay. */
10708 || (!just_this_one_p
10709 && !FRAME_WINDOW_P (f)
10710 && !WINDOW_FULL_WIDTH_P (w))
10711 /* Line number to display. */
10712 || INTEGERP (w->base_line_pos)
10713 /* Column number is displayed and different from the one displayed. */
10714 || (!NILP (w->column_number_displayed)
10715 && (XFASTINT (w->column_number_displayed)
10716 != (int) current_column ()))) /* iftc */
10717 /* This means that the window has a mode line. */
10718 && (WINDOW_WANTS_MODELINE_P (w)
10719 || WINDOW_WANTS_HEADER_LINE_P (w)))
10720 {
10721 display_mode_lines (w);
10722
10723 /* If mode line height has changed, arrange for a thorough
10724 immediate redisplay using the correct mode line height. */
10725 if (WINDOW_WANTS_MODELINE_P (w)
10726 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
10727 {
10728 fonts_changed_p = 1;
10729 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
10730 = DESIRED_MODE_LINE_HEIGHT (w);
10731 }
10732
10733 /* If top line height has changed, arrange for a thorough
10734 immediate redisplay using the correct mode line height. */
10735 if (WINDOW_WANTS_HEADER_LINE_P (w)
10736 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
10737 {
10738 fonts_changed_p = 1;
10739 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
10740 = DESIRED_HEADER_LINE_HEIGHT (w);
10741 }
10742
10743 if (fonts_changed_p)
10744 goto need_larger_matrices;
10745 }
10746
10747 if (!line_number_displayed
10748 && !BUFFERP (w->base_line_pos))
10749 {
10750 w->base_line_pos = Qnil;
10751 w->base_line_number = Qnil;
10752 }
10753
10754 finish_menu_bars:
10755
10756 /* When we reach a frame's selected window, redo the frame's menu bar. */
10757 if (update_mode_line
10758 && EQ (FRAME_SELECTED_WINDOW (f), window))
10759 {
10760 int redisplay_menu_p = 0;
10761
10762 if (FRAME_WINDOW_P (f))
10763 {
10764 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS)
10765 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
10766 #else
10767 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10768 #endif
10769 }
10770 else
10771 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10772
10773 if (redisplay_menu_p)
10774 display_menu_bar (w);
10775
10776 #ifdef HAVE_WINDOW_SYSTEM
10777 if (WINDOWP (f->tool_bar_window)
10778 && (FRAME_TOOL_BAR_LINES (f) > 0
10779 || auto_resize_tool_bars_p))
10780 redisplay_tool_bar (f);
10781 #endif
10782 }
10783
10784 /* We go to this label, with fonts_changed_p nonzero,
10785 if it is necessary to try again using larger glyph matrices.
10786 We have to redeem the scroll bar even in this case,
10787 because the loop in redisplay_internal expects that. */
10788 need_larger_matrices:
10789 ;
10790 finish_scroll_bars:
10791
10792 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
10793 {
10794 int start, end, whole;
10795
10796 /* Calculate the start and end positions for the current window.
10797 At some point, it would be nice to choose between scrollbars
10798 which reflect the whole buffer size, with special markers
10799 indicating narrowing, and scrollbars which reflect only the
10800 visible region.
10801
10802 Note that mini-buffers sometimes aren't displaying any text. */
10803 if (!MINI_WINDOW_P (w)
10804 || (w == XWINDOW (minibuf_window)
10805 && NILP (echo_area_buffer[0])))
10806 {
10807 whole = ZV - BEGV;
10808 start = marker_position (w->start) - BEGV;
10809 /* I don't think this is guaranteed to be right. For the
10810 moment, we'll pretend it is. */
10811 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
10812
10813 if (end < start)
10814 end = start;
10815 if (whole < (end - start))
10816 whole = end - start;
10817 }
10818 else
10819 start = end = whole = 0;
10820
10821 /* Indicate what this scroll bar ought to be displaying now. */
10822 set_vertical_scroll_bar_hook (w, end - start, whole, start);
10823
10824 /* Note that we actually used the scroll bar attached to this
10825 window, so it shouldn't be deleted at the end of redisplay. */
10826 redeem_scroll_bar_hook (w);
10827 }
10828
10829 /* Restore current_buffer and value of point in it. */
10830 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
10831 set_buffer_internal_1 (old);
10832 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
10833
10834 unbind_to (count, Qnil);
10835 }
10836
10837
10838 /* Build the complete desired matrix of WINDOW with a window start
10839 buffer position POS. Value is non-zero if successful. It is zero
10840 if fonts were loaded during redisplay which makes re-adjusting
10841 glyph matrices necessary. */
10842
10843 int
10844 try_window (window, pos)
10845 Lisp_Object window;
10846 struct text_pos pos;
10847 {
10848 struct window *w = XWINDOW (window);
10849 struct it it;
10850 struct glyph_row *last_text_row = NULL;
10851
10852 /* Make POS the new window start. */
10853 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
10854
10855 /* Mark cursor position as unknown. No overlay arrow seen. */
10856 w->cursor.vpos = -1;
10857 overlay_arrow_seen = 0;
10858
10859 /* Initialize iterator and info to start at POS. */
10860 start_display (&it, w, pos);
10861
10862 /* Display all lines of W. */
10863 while (it.current_y < it.last_visible_y)
10864 {
10865 if (display_line (&it))
10866 last_text_row = it.glyph_row - 1;
10867 if (fonts_changed_p)
10868 return 0;
10869 }
10870
10871 /* If bottom moved off end of frame, change mode line percentage. */
10872 if (XFASTINT (w->window_end_pos) <= 0
10873 && Z != IT_CHARPOS (it))
10874 w->update_mode_line = Qt;
10875
10876 /* Set window_end_pos to the offset of the last character displayed
10877 on the window from the end of current_buffer. Set
10878 window_end_vpos to its row number. */
10879 if (last_text_row)
10880 {
10881 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
10882 w->window_end_bytepos
10883 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10884 w->window_end_pos
10885 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10886 w->window_end_vpos
10887 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10888 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
10889 ->displays_text_p);
10890 }
10891 else
10892 {
10893 w->window_end_bytepos = 0;
10894 w->window_end_pos = w->window_end_vpos = make_number (0);
10895 }
10896
10897 /* But that is not valid info until redisplay finishes. */
10898 w->window_end_valid = Qnil;
10899 return 1;
10900 }
10901
10902
10903 \f
10904 /************************************************************************
10905 Window redisplay reusing current matrix when buffer has not changed
10906 ************************************************************************/
10907
10908 /* Try redisplay of window W showing an unchanged buffer with a
10909 different window start than the last time it was displayed by
10910 reusing its current matrix. Value is non-zero if successful.
10911 W->start is the new window start. */
10912
10913 static int
10914 try_window_reusing_current_matrix (w)
10915 struct window *w;
10916 {
10917 struct frame *f = XFRAME (w->frame);
10918 struct glyph_row *row, *bottom_row;
10919 struct it it;
10920 struct run run;
10921 struct text_pos start, new_start;
10922 int nrows_scrolled, i;
10923 struct glyph_row *last_text_row;
10924 struct glyph_row *last_reused_text_row;
10925 struct glyph_row *start_row;
10926 int start_vpos, min_y, max_y;
10927
10928 #if GLYPH_DEBUG
10929 if (inhibit_try_window_reusing)
10930 return 0;
10931 #endif
10932
10933 if (/* This function doesn't handle terminal frames. */
10934 !FRAME_WINDOW_P (f)
10935 /* Don't try to reuse the display if windows have been split
10936 or such. */
10937 || windows_or_buffers_changed
10938 || cursor_type_changed)
10939 return 0;
10940
10941 /* Can't do this if region may have changed. */
10942 if ((!NILP (Vtransient_mark_mode)
10943 && !NILP (current_buffer->mark_active))
10944 || !NILP (w->region_showing)
10945 || !NILP (Vshow_trailing_whitespace))
10946 return 0;
10947
10948 /* If top-line visibility has changed, give up. */
10949 if (WINDOW_WANTS_HEADER_LINE_P (w)
10950 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
10951 return 0;
10952
10953 /* Give up if old or new display is scrolled vertically. We could
10954 make this function handle this, but right now it doesn't. */
10955 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10956 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
10957 return 0;
10958
10959 /* The variable new_start now holds the new window start. The old
10960 start `start' can be determined from the current matrix. */
10961 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
10962 start = start_row->start.pos;
10963 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
10964
10965 /* Clear the desired matrix for the display below. */
10966 clear_glyph_matrix (w->desired_matrix);
10967
10968 if (CHARPOS (new_start) <= CHARPOS (start))
10969 {
10970 int first_row_y;
10971
10972 /* Don't use this method if the display starts with an ellipsis
10973 displayed for invisible text. It's not easy to handle that case
10974 below, and it's certainly not worth the effort since this is
10975 not a frequent case. */
10976 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
10977 return 0;
10978
10979 IF_DEBUG (debug_method_add (w, "twu1"));
10980
10981 /* Display up to a row that can be reused. The variable
10982 last_text_row is set to the last row displayed that displays
10983 text. Note that it.vpos == 0 if or if not there is a
10984 header-line; it's not the same as the MATRIX_ROW_VPOS! */
10985 start_display (&it, w, new_start);
10986 first_row_y = it.current_y;
10987 w->cursor.vpos = -1;
10988 last_text_row = last_reused_text_row = NULL;
10989
10990 while (it.current_y < it.last_visible_y
10991 && IT_CHARPOS (it) < CHARPOS (start)
10992 && !fonts_changed_p)
10993 if (display_line (&it))
10994 last_text_row = it.glyph_row - 1;
10995
10996 /* A value of current_y < last_visible_y means that we stopped
10997 at the previous window start, which in turn means that we
10998 have at least one reusable row. */
10999 if (it.current_y < it.last_visible_y)
11000 {
11001 /* IT.vpos always starts from 0; it counts text lines. */
11002 nrows_scrolled = it.vpos;
11003
11004 /* Find PT if not already found in the lines displayed. */
11005 if (w->cursor.vpos < 0)
11006 {
11007 int dy = it.current_y - first_row_y;
11008
11009 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11010 row = row_containing_pos (w, PT, row, NULL, dy);
11011 if (row)
11012 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
11013 dy, nrows_scrolled);
11014 else
11015 {
11016 clear_glyph_matrix (w->desired_matrix);
11017 return 0;
11018 }
11019 }
11020
11021 /* Scroll the display. Do it before the current matrix is
11022 changed. The problem here is that update has not yet
11023 run, i.e. part of the current matrix is not up to date.
11024 scroll_run_hook will clear the cursor, and use the
11025 current matrix to get the height of the row the cursor is
11026 in. */
11027 run.current_y = first_row_y;
11028 run.desired_y = it.current_y;
11029 run.height = it.last_visible_y - it.current_y;
11030
11031 if (run.height > 0 && run.current_y != run.desired_y)
11032 {
11033 update_begin (f);
11034 rif->update_window_begin_hook (w);
11035 rif->clear_mouse_face (w);
11036 rif->scroll_run_hook (w, &run);
11037 rif->update_window_end_hook (w, 0, 0);
11038 update_end (f);
11039 }
11040
11041 /* Shift current matrix down by nrows_scrolled lines. */
11042 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
11043 rotate_matrix (w->current_matrix,
11044 start_vpos,
11045 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
11046 nrows_scrolled);
11047
11048 /* Disable lines that must be updated. */
11049 for (i = 0; i < it.vpos; ++i)
11050 (start_row + i)->enabled_p = 0;
11051
11052 /* Re-compute Y positions. */
11053 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
11054 max_y = it.last_visible_y;
11055 for (row = start_row + nrows_scrolled;
11056 row < bottom_row;
11057 ++row)
11058 {
11059 row->y = it.current_y;
11060 row->visible_height = row->height;
11061
11062 if (row->y < min_y)
11063 row->visible_height -= min_y - row->y;
11064 if (row->y + row->height > max_y)
11065 row->visible_height -= row->y + row->height - max_y;
11066
11067 it.current_y += row->height;
11068
11069 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
11070 last_reused_text_row = row;
11071 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
11072 break;
11073 }
11074
11075 /* Disable lines in the current matrix which are now
11076 below the window. */
11077 for (++row; row < bottom_row; ++row)
11078 row->enabled_p = 0;
11079 }
11080
11081 /* Update window_end_pos etc.; last_reused_text_row is the last
11082 reused row from the current matrix containing text, if any.
11083 The value of last_text_row is the last displayed line
11084 containing text. */
11085 if (last_reused_text_row)
11086 {
11087 w->window_end_bytepos
11088 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
11089 w->window_end_pos
11090 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
11091 w->window_end_vpos
11092 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
11093 w->current_matrix));
11094 }
11095 else if (last_text_row)
11096 {
11097 w->window_end_bytepos
11098 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
11099 w->window_end_pos
11100 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
11101 w->window_end_vpos
11102 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
11103 }
11104 else
11105 {
11106 /* This window must be completely empty. */
11107 w->window_end_bytepos = 0;
11108 w->window_end_pos = w->window_end_vpos = make_number (0);
11109 }
11110 w->window_end_valid = Qnil;
11111
11112 /* Update hint: don't try scrolling again in update_window. */
11113 w->desired_matrix->no_scrolling_p = 1;
11114
11115 #if GLYPH_DEBUG
11116 debug_method_add (w, "try_window_reusing_current_matrix 1");
11117 #endif
11118 return 1;
11119 }
11120 else if (CHARPOS (new_start) > CHARPOS (start))
11121 {
11122 struct glyph_row *pt_row, *row;
11123 struct glyph_row *first_reusable_row;
11124 struct glyph_row *first_row_to_display;
11125 int dy;
11126 int yb = window_text_bottom_y (w);
11127
11128 /* Find the row starting at new_start, if there is one. Don't
11129 reuse a partially visible line at the end. */
11130 first_reusable_row = start_row;
11131 while (first_reusable_row->enabled_p
11132 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
11133 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
11134 < CHARPOS (new_start)))
11135 ++first_reusable_row;
11136
11137 /* Give up if there is no row to reuse. */
11138 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
11139 || !first_reusable_row->enabled_p
11140 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
11141 != CHARPOS (new_start)))
11142 return 0;
11143
11144 /* We can reuse fully visible rows beginning with
11145 first_reusable_row to the end of the window. Set
11146 first_row_to_display to the first row that cannot be reused.
11147 Set pt_row to the row containing point, if there is any. */
11148 pt_row = NULL;
11149 for (first_row_to_display = first_reusable_row;
11150 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
11151 ++first_row_to_display)
11152 {
11153 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
11154 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
11155 pt_row = first_row_to_display;
11156 }
11157
11158 /* Start displaying at the start of first_row_to_display. */
11159 xassert (first_row_to_display->y < yb);
11160 init_to_row_start (&it, w, first_row_to_display);
11161
11162 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
11163 - start_vpos);
11164 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
11165 - nrows_scrolled);
11166 it.current_y = (first_row_to_display->y - first_reusable_row->y
11167 + WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
11168
11169 /* Display lines beginning with first_row_to_display in the
11170 desired matrix. Set last_text_row to the last row displayed
11171 that displays text. */
11172 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
11173 if (pt_row == NULL)
11174 w->cursor.vpos = -1;
11175 last_text_row = NULL;
11176 while (it.current_y < it.last_visible_y && !fonts_changed_p)
11177 if (display_line (&it))
11178 last_text_row = it.glyph_row - 1;
11179
11180 /* Give up If point isn't in a row displayed or reused. */
11181 if (w->cursor.vpos < 0)
11182 {
11183 clear_glyph_matrix (w->desired_matrix);
11184 return 0;
11185 }
11186
11187 /* If point is in a reused row, adjust y and vpos of the cursor
11188 position. */
11189 if (pt_row)
11190 {
11191 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
11192 w->current_matrix);
11193 w->cursor.y -= first_reusable_row->y;
11194 }
11195
11196 /* Scroll the display. */
11197 run.current_y = first_reusable_row->y;
11198 run.desired_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
11199 run.height = it.last_visible_y - run.current_y;
11200 dy = run.current_y - run.desired_y;
11201
11202 if (run.height)
11203 {
11204 struct frame *f = XFRAME (WINDOW_FRAME (w));
11205 update_begin (f);
11206 rif->update_window_begin_hook (w);
11207 rif->clear_mouse_face (w);
11208 rif->scroll_run_hook (w, &run);
11209 rif->update_window_end_hook (w, 0, 0);
11210 update_end (f);
11211 }
11212
11213 /* Adjust Y positions of reused rows. */
11214 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
11215 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
11216 max_y = it.last_visible_y;
11217 for (row = first_reusable_row; row < first_row_to_display; ++row)
11218 {
11219 row->y -= dy;
11220 row->visible_height = row->height;
11221 if (row->y < min_y)
11222 row->visible_height -= min_y - row->y;
11223 if (row->y + row->height > max_y)
11224 row->visible_height -= row->y + row->height - max_y;
11225 }
11226
11227 /* Scroll the current matrix. */
11228 xassert (nrows_scrolled > 0);
11229 rotate_matrix (w->current_matrix,
11230 start_vpos,
11231 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
11232 -nrows_scrolled);
11233
11234 /* Disable rows not reused. */
11235 for (row -= nrows_scrolled; row < bottom_row; ++row)
11236 row->enabled_p = 0;
11237
11238 /* Adjust window end. A null value of last_text_row means that
11239 the window end is in reused rows which in turn means that
11240 only its vpos can have changed. */
11241 if (last_text_row)
11242 {
11243 w->window_end_bytepos
11244 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
11245 w->window_end_pos
11246 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
11247 w->window_end_vpos
11248 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
11249 }
11250 else
11251 {
11252 w->window_end_vpos
11253 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
11254 }
11255
11256 w->window_end_valid = Qnil;
11257 w->desired_matrix->no_scrolling_p = 1;
11258
11259 #if GLYPH_DEBUG
11260 debug_method_add (w, "try_window_reusing_current_matrix 2");
11261 #endif
11262 return 1;
11263 }
11264
11265 return 0;
11266 }
11267
11268
11269 \f
11270 /************************************************************************
11271 Window redisplay reusing current matrix when buffer has changed
11272 ************************************************************************/
11273
11274 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
11275 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
11276 int *, int *));
11277 static struct glyph_row *
11278 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
11279 struct glyph_row *));
11280
11281
11282 /* Return the last row in MATRIX displaying text. If row START is
11283 non-null, start searching with that row. IT gives the dimensions
11284 of the display. Value is null if matrix is empty; otherwise it is
11285 a pointer to the row found. */
11286
11287 static struct glyph_row *
11288 find_last_row_displaying_text (matrix, it, start)
11289 struct glyph_matrix *matrix;
11290 struct it *it;
11291 struct glyph_row *start;
11292 {
11293 struct glyph_row *row, *row_found;
11294
11295 /* Set row_found to the last row in IT->w's current matrix
11296 displaying text. The loop looks funny but think of partially
11297 visible lines. */
11298 row_found = NULL;
11299 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
11300 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
11301 {
11302 xassert (row->enabled_p);
11303 row_found = row;
11304 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
11305 break;
11306 ++row;
11307 }
11308
11309 return row_found;
11310 }
11311
11312
11313 /* Return the last row in the current matrix of W that is not affected
11314 by changes at the start of current_buffer that occurred since W's
11315 current matrix was built. Value is null if no such row exists.
11316
11317 BEG_UNCHANGED us the number of characters unchanged at the start of
11318 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
11319 first changed character in current_buffer. Characters at positions <
11320 BEG + BEG_UNCHANGED are at the same buffer positions as they were
11321 when the current matrix was built. */
11322
11323 static struct glyph_row *
11324 find_last_unchanged_at_beg_row (w)
11325 struct window *w;
11326 {
11327 int first_changed_pos = BEG + BEG_UNCHANGED;
11328 struct glyph_row *row;
11329 struct glyph_row *row_found = NULL;
11330 int yb = window_text_bottom_y (w);
11331
11332 /* Find the last row displaying unchanged text. */
11333 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11334 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
11335 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
11336 {
11337 if (/* If row ends before first_changed_pos, it is unchanged,
11338 except in some case. */
11339 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
11340 /* When row ends in ZV and we write at ZV it is not
11341 unchanged. */
11342 && !row->ends_at_zv_p
11343 /* When first_changed_pos is the end of a continued line,
11344 row is not unchanged because it may be no longer
11345 continued. */
11346 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
11347 && row->continued_p))
11348 row_found = row;
11349
11350 /* Stop if last visible row. */
11351 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
11352 break;
11353
11354 ++row;
11355 }
11356
11357 return row_found;
11358 }
11359
11360
11361 /* Find the first glyph row in the current matrix of W that is not
11362 affected by changes at the end of current_buffer since the
11363 time W's current matrix was built.
11364
11365 Return in *DELTA the number of chars by which buffer positions in
11366 unchanged text at the end of current_buffer must be adjusted.
11367
11368 Return in *DELTA_BYTES the corresponding number of bytes.
11369
11370 Value is null if no such row exists, i.e. all rows are affected by
11371 changes. */
11372
11373 static struct glyph_row *
11374 find_first_unchanged_at_end_row (w, delta, delta_bytes)
11375 struct window *w;
11376 int *delta, *delta_bytes;
11377 {
11378 struct glyph_row *row;
11379 struct glyph_row *row_found = NULL;
11380
11381 *delta = *delta_bytes = 0;
11382
11383 /* Display must not have been paused, otherwise the current matrix
11384 is not up to date. */
11385 if (NILP (w->window_end_valid))
11386 abort ();
11387
11388 /* A value of window_end_pos >= END_UNCHANGED means that the window
11389 end is in the range of changed text. If so, there is no
11390 unchanged row at the end of W's current matrix. */
11391 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
11392 return NULL;
11393
11394 /* Set row to the last row in W's current matrix displaying text. */
11395 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
11396
11397 /* If matrix is entirely empty, no unchanged row exists. */
11398 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
11399 {
11400 /* The value of row is the last glyph row in the matrix having a
11401 meaningful buffer position in it. The end position of row
11402 corresponds to window_end_pos. This allows us to translate
11403 buffer positions in the current matrix to current buffer
11404 positions for characters not in changed text. */
11405 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
11406 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
11407 int last_unchanged_pos, last_unchanged_pos_old;
11408 struct glyph_row *first_text_row
11409 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11410
11411 *delta = Z - Z_old;
11412 *delta_bytes = Z_BYTE - Z_BYTE_old;
11413
11414 /* Set last_unchanged_pos to the buffer position of the last
11415 character in the buffer that has not been changed. Z is the
11416 index + 1 of the last character in current_buffer, i.e. by
11417 subtracting END_UNCHANGED we get the index of the last
11418 unchanged character, and we have to add BEG to get its buffer
11419 position. */
11420 last_unchanged_pos = Z - END_UNCHANGED + BEG;
11421 last_unchanged_pos_old = last_unchanged_pos - *delta;
11422
11423 /* Search backward from ROW for a row displaying a line that
11424 starts at a minimum position >= last_unchanged_pos_old. */
11425 for (; row > first_text_row; --row)
11426 {
11427 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
11428 abort ();
11429
11430 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
11431 row_found = row;
11432 }
11433 }
11434
11435 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
11436 abort ();
11437
11438 return row_found;
11439 }
11440
11441
11442 /* Make sure that glyph rows in the current matrix of window W
11443 reference the same glyph memory as corresponding rows in the
11444 frame's frame matrix. This function is called after scrolling W's
11445 current matrix on a terminal frame in try_window_id and
11446 try_window_reusing_current_matrix. */
11447
11448 static void
11449 sync_frame_with_window_matrix_rows (w)
11450 struct window *w;
11451 {
11452 struct frame *f = XFRAME (w->frame);
11453 struct glyph_row *window_row, *window_row_end, *frame_row;
11454
11455 /* Preconditions: W must be a leaf window and full-width. Its frame
11456 must have a frame matrix. */
11457 xassert (NILP (w->hchild) && NILP (w->vchild));
11458 xassert (WINDOW_FULL_WIDTH_P (w));
11459 xassert (!FRAME_WINDOW_P (f));
11460
11461 /* If W is a full-width window, glyph pointers in W's current matrix
11462 have, by definition, to be the same as glyph pointers in the
11463 corresponding frame matrix. Note that frame matrices have no
11464 marginal areas (see build_frame_matrix). */
11465 window_row = w->current_matrix->rows;
11466 window_row_end = window_row + w->current_matrix->nrows;
11467 frame_row = f->current_matrix->rows + XFASTINT (w->top);
11468 while (window_row < window_row_end)
11469 {
11470 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
11471 struct glyph *end = window_row->glyphs[LAST_AREA];
11472
11473 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
11474 frame_row->glyphs[TEXT_AREA] = start;
11475 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
11476 frame_row->glyphs[LAST_AREA] = end;
11477
11478 /* Disable frame rows whose corresponding window rows have
11479 been disabled in try_window_id. */
11480 if (!window_row->enabled_p)
11481 frame_row->enabled_p = 0;
11482
11483 ++window_row, ++frame_row;
11484 }
11485 }
11486
11487
11488 /* Find the glyph row in window W containing CHARPOS. Consider all
11489 rows between START and END (not inclusive). END null means search
11490 all rows to the end of the display area of W. Value is the row
11491 containing CHARPOS or null. */
11492
11493 struct glyph_row *
11494 row_containing_pos (w, charpos, start, end, dy)
11495 struct window *w;
11496 int charpos;
11497 struct glyph_row *start, *end;
11498 int dy;
11499 {
11500 struct glyph_row *row = start;
11501 int last_y;
11502
11503 /* If we happen to start on a header-line, skip that. */
11504 if (row->mode_line_p)
11505 ++row;
11506
11507 if ((end && row >= end) || !row->enabled_p)
11508 return NULL;
11509
11510 last_y = window_text_bottom_y (w) - dy;
11511
11512 while (1)
11513 {
11514 /* Give up if we have gone too far. */
11515 if (end && row >= end)
11516 return NULL;
11517 if (MATRIX_ROW_BOTTOM_Y (row) >= last_y)
11518 return NULL;
11519
11520 /* If it is in this row, return this row. */
11521 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
11522 || (MATRIX_ROW_END_CHARPOS (row) == charpos
11523 /* The end position of a row equals the start
11524 position of the next row. If CHARPOS is there, we
11525 would rather display it in the next line, except
11526 when this line ends in ZV. */
11527 && !row->ends_at_zv_p
11528 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
11529 && charpos >= MATRIX_ROW_START_CHARPOS (row))
11530 return row;
11531 ++row;
11532 }
11533 }
11534
11535
11536 /* Try to redisplay window W by reusing its existing display. W's
11537 current matrix must be up to date when this function is called,
11538 i.e. window_end_valid must not be nil.
11539
11540 Value is
11541
11542 1 if display has been updated
11543 0 if otherwise unsuccessful
11544 -1 if redisplay with same window start is known not to succeed
11545
11546 The following steps are performed:
11547
11548 1. Find the last row in the current matrix of W that is not
11549 affected by changes at the start of current_buffer. If no such row
11550 is found, give up.
11551
11552 2. Find the first row in W's current matrix that is not affected by
11553 changes at the end of current_buffer. Maybe there is no such row.
11554
11555 3. Display lines beginning with the row + 1 found in step 1 to the
11556 row found in step 2 or, if step 2 didn't find a row, to the end of
11557 the window.
11558
11559 4. If cursor is not known to appear on the window, give up.
11560
11561 5. If display stopped at the row found in step 2, scroll the
11562 display and current matrix as needed.
11563
11564 6. Maybe display some lines at the end of W, if we must. This can
11565 happen under various circumstances, like a partially visible line
11566 becoming fully visible, or because newly displayed lines are displayed
11567 in smaller font sizes.
11568
11569 7. Update W's window end information. */
11570
11571 static int
11572 try_window_id (w)
11573 struct window *w;
11574 {
11575 struct frame *f = XFRAME (w->frame);
11576 struct glyph_matrix *current_matrix = w->current_matrix;
11577 struct glyph_matrix *desired_matrix = w->desired_matrix;
11578 struct glyph_row *last_unchanged_at_beg_row;
11579 struct glyph_row *first_unchanged_at_end_row;
11580 struct glyph_row *row;
11581 struct glyph_row *bottom_row;
11582 int bottom_vpos;
11583 struct it it;
11584 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
11585 struct text_pos start_pos;
11586 struct run run;
11587 int first_unchanged_at_end_vpos = 0;
11588 struct glyph_row *last_text_row, *last_text_row_at_end;
11589 struct text_pos start;
11590 int first_changed_charpos, last_changed_charpos;
11591
11592 #if GLYPH_DEBUG
11593 if (inhibit_try_window_id)
11594 return 0;
11595 #endif
11596
11597 /* This is handy for debugging. */
11598 #if 0
11599 #define GIVE_UP(X) \
11600 do { \
11601 fprintf (stderr, "try_window_id give up %d\n", (X)); \
11602 return 0; \
11603 } while (0)
11604 #else
11605 #define GIVE_UP(X) return 0
11606 #endif
11607
11608 SET_TEXT_POS_FROM_MARKER (start, w->start);
11609
11610 /* Don't use this for mini-windows because these can show
11611 messages and mini-buffers, and we don't handle that here. */
11612 if (MINI_WINDOW_P (w))
11613 GIVE_UP (1);
11614
11615 /* This flag is used to prevent redisplay optimizations. */
11616 if (windows_or_buffers_changed || cursor_type_changed)
11617 GIVE_UP (2);
11618
11619 /* Verify that narrowing has not changed.
11620 Also verify that we were not told to prevent redisplay optimizations.
11621 It would be nice to further
11622 reduce the number of cases where this prevents try_window_id. */
11623 if (current_buffer->clip_changed
11624 || current_buffer->prevent_redisplay_optimizations_p)
11625 GIVE_UP (3);
11626
11627 /* Window must either use window-based redisplay or be full width. */
11628 if (!FRAME_WINDOW_P (f)
11629 && (!line_ins_del_ok
11630 || !WINDOW_FULL_WIDTH_P (w)))
11631 GIVE_UP (4);
11632
11633 /* Give up if point is not known NOT to appear in W. */
11634 if (PT < CHARPOS (start))
11635 GIVE_UP (5);
11636
11637 /* Another way to prevent redisplay optimizations. */
11638 if (XFASTINT (w->last_modified) == 0)
11639 GIVE_UP (6);
11640
11641 /* Verify that window is not hscrolled. */
11642 if (XFASTINT (w->hscroll) != 0)
11643 GIVE_UP (7);
11644
11645 /* Verify that display wasn't paused. */
11646 if (NILP (w->window_end_valid))
11647 GIVE_UP (8);
11648
11649 /* Can't use this if highlighting a region because a cursor movement
11650 will do more than just set the cursor. */
11651 if (!NILP (Vtransient_mark_mode)
11652 && !NILP (current_buffer->mark_active))
11653 GIVE_UP (9);
11654
11655 /* Likewise if highlighting trailing whitespace. */
11656 if (!NILP (Vshow_trailing_whitespace))
11657 GIVE_UP (11);
11658
11659 /* Likewise if showing a region. */
11660 if (!NILP (w->region_showing))
11661 GIVE_UP (10);
11662
11663 /* Can use this if overlay arrow position and or string have changed. */
11664 if (!EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
11665 || !EQ (last_arrow_string, Voverlay_arrow_string))
11666 GIVE_UP (12);
11667
11668
11669 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
11670 only if buffer has really changed. The reason is that the gap is
11671 initially at Z for freshly visited files. The code below would
11672 set end_unchanged to 0 in that case. */
11673 if (MODIFF > SAVE_MODIFF
11674 /* This seems to happen sometimes after saving a buffer. */
11675 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
11676 {
11677 if (GPT - BEG < BEG_UNCHANGED)
11678 BEG_UNCHANGED = GPT - BEG;
11679 if (Z - GPT < END_UNCHANGED)
11680 END_UNCHANGED = Z - GPT;
11681 }
11682
11683 /* The position of the first and last character that has been changed. */
11684 first_changed_charpos = BEG + BEG_UNCHANGED;
11685 last_changed_charpos = Z - END_UNCHANGED;
11686
11687 /* If window starts after a line end, and the last change is in
11688 front of that newline, then changes don't affect the display.
11689 This case happens with stealth-fontification. Note that although
11690 the display is unchanged, glyph positions in the matrix have to
11691 be adjusted, of course. */
11692 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
11693 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
11694 && ((last_changed_charpos < CHARPOS (start)
11695 && CHARPOS (start) == BEGV)
11696 || (last_changed_charpos < CHARPOS (start) - 1
11697 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
11698 {
11699 int Z_old, delta, Z_BYTE_old, delta_bytes;
11700 struct glyph_row *r0;
11701
11702 /* Compute how many chars/bytes have been added to or removed
11703 from the buffer. */
11704 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
11705 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
11706 delta = Z - Z_old;
11707 delta_bytes = Z_BYTE - Z_BYTE_old;
11708
11709 /* Give up if PT is not in the window. Note that it already has
11710 been checked at the start of try_window_id that PT is not in
11711 front of the window start. */
11712 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
11713 GIVE_UP (13);
11714
11715 /* If window start is unchanged, we can reuse the whole matrix
11716 as is, after adjusting glyph positions. No need to compute
11717 the window end again, since its offset from Z hasn't changed. */
11718 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
11719 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
11720 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes)
11721 {
11722 /* Adjust positions in the glyph matrix. */
11723 if (delta || delta_bytes)
11724 {
11725 struct glyph_row *r1
11726 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11727 increment_matrix_positions (w->current_matrix,
11728 MATRIX_ROW_VPOS (r0, current_matrix),
11729 MATRIX_ROW_VPOS (r1, current_matrix),
11730 delta, delta_bytes);
11731 }
11732
11733 /* Set the cursor. */
11734 row = row_containing_pos (w, PT, r0, NULL, 0);
11735 if (row)
11736 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
11737 return 1;
11738 }
11739 }
11740
11741 /* Handle the case that changes are all below what is displayed in
11742 the window, and that PT is in the window. This shortcut cannot
11743 be taken if ZV is visible in the window, and text has been added
11744 there that is visible in the window. */
11745 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
11746 /* ZV is not visible in the window, or there are no
11747 changes at ZV, actually. */
11748 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
11749 || first_changed_charpos == last_changed_charpos))
11750 {
11751 struct glyph_row *r0;
11752
11753 /* Give up if PT is not in the window. Note that it already has
11754 been checked at the start of try_window_id that PT is not in
11755 front of the window start. */
11756 if (PT >= MATRIX_ROW_END_CHARPOS (row))
11757 GIVE_UP (14);
11758
11759 /* If window start is unchanged, we can reuse the whole matrix
11760 as is, without changing glyph positions since no text has
11761 been added/removed in front of the window end. */
11762 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
11763 if (TEXT_POS_EQUAL_P (start, r0->start.pos))
11764 {
11765 /* We have to compute the window end anew since text
11766 can have been added/removed after it. */
11767 w->window_end_pos
11768 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
11769 w->window_end_bytepos
11770 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11771
11772 /* Set the cursor. */
11773 row = row_containing_pos (w, PT, r0, NULL, 0);
11774 if (row)
11775 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
11776 return 2;
11777 }
11778 }
11779
11780 /* Give up if window start is in the changed area.
11781
11782 The condition used to read
11783
11784 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
11785
11786 but why that was tested escapes me at the moment. */
11787 if (CHARPOS (start) >= first_changed_charpos
11788 && CHARPOS (start) <= last_changed_charpos)
11789 GIVE_UP (15);
11790
11791 /* Check that window start agrees with the start of the first glyph
11792 row in its current matrix. Check this after we know the window
11793 start is not in changed text, otherwise positions would not be
11794 comparable. */
11795 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
11796 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
11797 GIVE_UP (16);
11798
11799 /* Give up if the window ends in strings. Overlay strings
11800 at the end are difficult to handle, so don't try. */
11801 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
11802 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
11803 GIVE_UP (20);
11804
11805 /* Compute the position at which we have to start displaying new
11806 lines. Some of the lines at the top of the window might be
11807 reusable because they are not displaying changed text. Find the
11808 last row in W's current matrix not affected by changes at the
11809 start of current_buffer. Value is null if changes start in the
11810 first line of window. */
11811 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
11812 if (last_unchanged_at_beg_row)
11813 {
11814 /* Avoid starting to display in the moddle of a character, a TAB
11815 for instance. This is easier than to set up the iterator
11816 exactly, and it's not a frequent case, so the additional
11817 effort wouldn't really pay off. */
11818 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
11819 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
11820 && last_unchanged_at_beg_row > w->current_matrix->rows)
11821 --last_unchanged_at_beg_row;
11822
11823 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
11824 GIVE_UP (17);
11825
11826 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
11827 GIVE_UP (18);
11828 start_pos = it.current.pos;
11829
11830 /* Start displaying new lines in the desired matrix at the same
11831 vpos we would use in the current matrix, i.e. below
11832 last_unchanged_at_beg_row. */
11833 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
11834 current_matrix);
11835 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11836 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
11837
11838 xassert (it.hpos == 0 && it.current_x == 0);
11839 }
11840 else
11841 {
11842 /* There are no reusable lines at the start of the window.
11843 Start displaying in the first line. */
11844 start_display (&it, w, start);
11845 start_pos = it.current.pos;
11846 }
11847
11848 /* Find the first row that is not affected by changes at the end of
11849 the buffer. Value will be null if there is no unchanged row, in
11850 which case we must redisplay to the end of the window. delta
11851 will be set to the value by which buffer positions beginning with
11852 first_unchanged_at_end_row have to be adjusted due to text
11853 changes. */
11854 first_unchanged_at_end_row
11855 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
11856 IF_DEBUG (debug_delta = delta);
11857 IF_DEBUG (debug_delta_bytes = delta_bytes);
11858
11859 /* Set stop_pos to the buffer position up to which we will have to
11860 display new lines. If first_unchanged_at_end_row != NULL, this
11861 is the buffer position of the start of the line displayed in that
11862 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
11863 that we don't stop at a buffer position. */
11864 stop_pos = 0;
11865 if (first_unchanged_at_end_row)
11866 {
11867 xassert (last_unchanged_at_beg_row == NULL
11868 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
11869
11870 /* If this is a continuation line, move forward to the next one
11871 that isn't. Changes in lines above affect this line.
11872 Caution: this may move first_unchanged_at_end_row to a row
11873 not displaying text. */
11874 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
11875 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11876 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11877 < it.last_visible_y))
11878 ++first_unchanged_at_end_row;
11879
11880 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11881 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11882 >= it.last_visible_y))
11883 first_unchanged_at_end_row = NULL;
11884 else
11885 {
11886 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
11887 + delta);
11888 first_unchanged_at_end_vpos
11889 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
11890 xassert (stop_pos >= Z - END_UNCHANGED);
11891 }
11892 }
11893 else if (last_unchanged_at_beg_row == NULL)
11894 GIVE_UP (19);
11895
11896
11897 #if GLYPH_DEBUG
11898
11899 /* Either there is no unchanged row at the end, or the one we have
11900 now displays text. This is a necessary condition for the window
11901 end pos calculation at the end of this function. */
11902 xassert (first_unchanged_at_end_row == NULL
11903 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
11904
11905 debug_last_unchanged_at_beg_vpos
11906 = (last_unchanged_at_beg_row
11907 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
11908 : -1);
11909 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
11910
11911 #endif /* GLYPH_DEBUG != 0 */
11912
11913
11914 /* Display new lines. Set last_text_row to the last new line
11915 displayed which has text on it, i.e. might end up as being the
11916 line where the window_end_vpos is. */
11917 w->cursor.vpos = -1;
11918 last_text_row = NULL;
11919 overlay_arrow_seen = 0;
11920 while (it.current_y < it.last_visible_y
11921 && !fonts_changed_p
11922 && (first_unchanged_at_end_row == NULL
11923 || IT_CHARPOS (it) < stop_pos))
11924 {
11925 if (display_line (&it))
11926 last_text_row = it.glyph_row - 1;
11927 }
11928
11929 if (fonts_changed_p)
11930 return -1;
11931
11932
11933 /* Compute differences in buffer positions, y-positions etc. for
11934 lines reused at the bottom of the window. Compute what we can
11935 scroll. */
11936 if (first_unchanged_at_end_row
11937 /* No lines reused because we displayed everything up to the
11938 bottom of the window. */
11939 && it.current_y < it.last_visible_y)
11940 {
11941 dvpos = (it.vpos
11942 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
11943 current_matrix));
11944 dy = it.current_y - first_unchanged_at_end_row->y;
11945 run.current_y = first_unchanged_at_end_row->y;
11946 run.desired_y = run.current_y + dy;
11947 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
11948 }
11949 else
11950 {
11951 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
11952 first_unchanged_at_end_row = NULL;
11953 }
11954 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
11955
11956
11957 /* Find the cursor if not already found. We have to decide whether
11958 PT will appear on this window (it sometimes doesn't, but this is
11959 not a very frequent case.) This decision has to be made before
11960 the current matrix is altered. A value of cursor.vpos < 0 means
11961 that PT is either in one of the lines beginning at
11962 first_unchanged_at_end_row or below the window. Don't care for
11963 lines that might be displayed later at the window end; as
11964 mentioned, this is not a frequent case. */
11965 if (w->cursor.vpos < 0)
11966 {
11967 /* Cursor in unchanged rows at the top? */
11968 if (PT < CHARPOS (start_pos)
11969 && last_unchanged_at_beg_row)
11970 {
11971 row = row_containing_pos (w, PT,
11972 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
11973 last_unchanged_at_beg_row + 1, 0);
11974 if (row)
11975 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11976 }
11977
11978 /* Start from first_unchanged_at_end_row looking for PT. */
11979 else if (first_unchanged_at_end_row)
11980 {
11981 row = row_containing_pos (w, PT - delta,
11982 first_unchanged_at_end_row, NULL, 0);
11983 if (row)
11984 set_cursor_from_row (w, row, w->current_matrix, delta,
11985 delta_bytes, dy, dvpos);
11986 }
11987
11988 /* Give up if cursor was not found. */
11989 if (w->cursor.vpos < 0)
11990 {
11991 clear_glyph_matrix (w->desired_matrix);
11992 return -1;
11993 }
11994 }
11995
11996 /* Don't let the cursor end in the scroll margins. */
11997 {
11998 int this_scroll_margin, cursor_height;
11999
12000 this_scroll_margin = max (0, scroll_margin);
12001 this_scroll_margin = min (this_scroll_margin,
12002 XFASTINT (w->height) / 4);
12003 this_scroll_margin *= CANON_Y_UNIT (it.f);
12004 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
12005
12006 if ((w->cursor.y < this_scroll_margin
12007 && CHARPOS (start) > BEGV)
12008 /* Don't take scroll margin into account at the bottom because
12009 old redisplay didn't do it either. */
12010 || w->cursor.y + cursor_height > it.last_visible_y)
12011 {
12012 w->cursor.vpos = -1;
12013 clear_glyph_matrix (w->desired_matrix);
12014 return -1;
12015 }
12016 }
12017
12018 /* Scroll the display. Do it before changing the current matrix so
12019 that xterm.c doesn't get confused about where the cursor glyph is
12020 found. */
12021 if (dy && run.height)
12022 {
12023 update_begin (f);
12024
12025 if (FRAME_WINDOW_P (f))
12026 {
12027 rif->update_window_begin_hook (w);
12028 rif->clear_mouse_face (w);
12029 rif->scroll_run_hook (w, &run);
12030 rif->update_window_end_hook (w, 0, 0);
12031 }
12032 else
12033 {
12034 /* Terminal frame. In this case, dvpos gives the number of
12035 lines to scroll by; dvpos < 0 means scroll up. */
12036 int first_unchanged_at_end_vpos
12037 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
12038 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
12039 int end = (XFASTINT (w->top)
12040 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
12041 + window_internal_height (w));
12042
12043 /* Perform the operation on the screen. */
12044 if (dvpos > 0)
12045 {
12046 /* Scroll last_unchanged_at_beg_row to the end of the
12047 window down dvpos lines. */
12048 set_terminal_window (end);
12049
12050 /* On dumb terminals delete dvpos lines at the end
12051 before inserting dvpos empty lines. */
12052 if (!scroll_region_ok)
12053 ins_del_lines (end - dvpos, -dvpos);
12054
12055 /* Insert dvpos empty lines in front of
12056 last_unchanged_at_beg_row. */
12057 ins_del_lines (from, dvpos);
12058 }
12059 else if (dvpos < 0)
12060 {
12061 /* Scroll up last_unchanged_at_beg_vpos to the end of
12062 the window to last_unchanged_at_beg_vpos - |dvpos|. */
12063 set_terminal_window (end);
12064
12065 /* Delete dvpos lines in front of
12066 last_unchanged_at_beg_vpos. ins_del_lines will set
12067 the cursor to the given vpos and emit |dvpos| delete
12068 line sequences. */
12069 ins_del_lines (from + dvpos, dvpos);
12070
12071 /* On a dumb terminal insert dvpos empty lines at the
12072 end. */
12073 if (!scroll_region_ok)
12074 ins_del_lines (end + dvpos, -dvpos);
12075 }
12076
12077 set_terminal_window (0);
12078 }
12079
12080 update_end (f);
12081 }
12082
12083 /* Shift reused rows of the current matrix to the right position.
12084 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
12085 text. */
12086 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
12087 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
12088 if (dvpos < 0)
12089 {
12090 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
12091 bottom_vpos, dvpos);
12092 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
12093 bottom_vpos, 0);
12094 }
12095 else if (dvpos > 0)
12096 {
12097 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
12098 bottom_vpos, dvpos);
12099 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
12100 first_unchanged_at_end_vpos + dvpos, 0);
12101 }
12102
12103 /* For frame-based redisplay, make sure that current frame and window
12104 matrix are in sync with respect to glyph memory. */
12105 if (!FRAME_WINDOW_P (f))
12106 sync_frame_with_window_matrix_rows (w);
12107
12108 /* Adjust buffer positions in reused rows. */
12109 if (delta)
12110 increment_matrix_positions (current_matrix,
12111 first_unchanged_at_end_vpos + dvpos,
12112 bottom_vpos, delta, delta_bytes);
12113
12114 /* Adjust Y positions. */
12115 if (dy)
12116 shift_glyph_matrix (w, current_matrix,
12117 first_unchanged_at_end_vpos + dvpos,
12118 bottom_vpos, dy);
12119
12120 if (first_unchanged_at_end_row)
12121 first_unchanged_at_end_row += dvpos;
12122
12123 /* If scrolling up, there may be some lines to display at the end of
12124 the window. */
12125 last_text_row_at_end = NULL;
12126 if (dy < 0)
12127 {
12128 /* Scrolling up can leave for example a partially visible line
12129 at the end of the window to be redisplayed. */
12130 /* Set last_row to the glyph row in the current matrix where the
12131 window end line is found. It has been moved up or down in
12132 the matrix by dvpos. */
12133 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
12134 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
12135
12136 /* If last_row is the window end line, it should display text. */
12137 xassert (last_row->displays_text_p);
12138
12139 /* If window end line was partially visible before, begin
12140 displaying at that line. Otherwise begin displaying with the
12141 line following it. */
12142 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
12143 {
12144 init_to_row_start (&it, w, last_row);
12145 it.vpos = last_vpos;
12146 it.current_y = last_row->y;
12147 }
12148 else
12149 {
12150 init_to_row_end (&it, w, last_row);
12151 it.vpos = 1 + last_vpos;
12152 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
12153 ++last_row;
12154 }
12155
12156 /* We may start in a continuation line. If so, we have to
12157 get the right continuation_lines_width and current_x. */
12158 it.continuation_lines_width = last_row->continuation_lines_width;
12159 it.hpos = it.current_x = 0;
12160
12161 /* Display the rest of the lines at the window end. */
12162 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
12163 while (it.current_y < it.last_visible_y
12164 && !fonts_changed_p)
12165 {
12166 /* Is it always sure that the display agrees with lines in
12167 the current matrix? I don't think so, so we mark rows
12168 displayed invalid in the current matrix by setting their
12169 enabled_p flag to zero. */
12170 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
12171 if (display_line (&it))
12172 last_text_row_at_end = it.glyph_row - 1;
12173 }
12174 }
12175
12176 /* Update window_end_pos and window_end_vpos. */
12177 if (first_unchanged_at_end_row
12178 && first_unchanged_at_end_row->y < it.last_visible_y
12179 && !last_text_row_at_end)
12180 {
12181 /* Window end line if one of the preserved rows from the current
12182 matrix. Set row to the last row displaying text in current
12183 matrix starting at first_unchanged_at_end_row, after
12184 scrolling. */
12185 xassert (first_unchanged_at_end_row->displays_text_p);
12186 row = find_last_row_displaying_text (w->current_matrix, &it,
12187 first_unchanged_at_end_row);
12188 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
12189
12190 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
12191 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
12192 w->window_end_vpos
12193 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
12194 xassert (w->window_end_bytepos >= 0);
12195 IF_DEBUG (debug_method_add (w, "A"));
12196 }
12197 else if (last_text_row_at_end)
12198 {
12199 w->window_end_pos
12200 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
12201 w->window_end_bytepos
12202 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
12203 w->window_end_vpos
12204 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
12205 xassert (w->window_end_bytepos >= 0);
12206 IF_DEBUG (debug_method_add (w, "B"));
12207 }
12208 else if (last_text_row)
12209 {
12210 /* We have displayed either to the end of the window or at the
12211 end of the window, i.e. the last row with text is to be found
12212 in the desired matrix. */
12213 w->window_end_pos
12214 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12215 w->window_end_bytepos
12216 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12217 w->window_end_vpos
12218 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
12219 xassert (w->window_end_bytepos >= 0);
12220 }
12221 else if (first_unchanged_at_end_row == NULL
12222 && last_text_row == NULL
12223 && last_text_row_at_end == NULL)
12224 {
12225 /* Displayed to end of window, but no line containing text was
12226 displayed. Lines were deleted at the end of the window. */
12227 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
12228 int vpos = XFASTINT (w->window_end_vpos);
12229 struct glyph_row *current_row = current_matrix->rows + vpos;
12230 struct glyph_row *desired_row = desired_matrix->rows + vpos;
12231
12232 for (row = NULL;
12233 row == NULL && vpos >= first_vpos;
12234 --vpos, --current_row, --desired_row)
12235 {
12236 if (desired_row->enabled_p)
12237 {
12238 if (desired_row->displays_text_p)
12239 row = desired_row;
12240 }
12241 else if (current_row->displays_text_p)
12242 row = current_row;
12243 }
12244
12245 xassert (row != NULL);
12246 w->window_end_vpos = make_number (vpos + 1);
12247 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
12248 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
12249 xassert (w->window_end_bytepos >= 0);
12250 IF_DEBUG (debug_method_add (w, "C"));
12251 }
12252 else
12253 abort ();
12254
12255 #if 0 /* This leads to problems, for instance when the cursor is
12256 at ZV, and the cursor line displays no text. */
12257 /* Disable rows below what's displayed in the window. This makes
12258 debugging easier. */
12259 enable_glyph_matrix_rows (current_matrix,
12260 XFASTINT (w->window_end_vpos) + 1,
12261 bottom_vpos, 0);
12262 #endif
12263
12264 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
12265 debug_end_vpos = XFASTINT (w->window_end_vpos));
12266
12267 /* Record that display has not been completed. */
12268 w->window_end_valid = Qnil;
12269 w->desired_matrix->no_scrolling_p = 1;
12270 return 3;
12271
12272 #undef GIVE_UP
12273 }
12274
12275
12276 \f
12277 /***********************************************************************
12278 More debugging support
12279 ***********************************************************************/
12280
12281 #if GLYPH_DEBUG
12282
12283 void dump_glyph_row P_ ((struct glyph_row *, int, int));
12284 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
12285 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
12286
12287
12288 /* Dump the contents of glyph matrix MATRIX on stderr.
12289
12290 GLYPHS 0 means don't show glyph contents.
12291 GLYPHS 1 means show glyphs in short form
12292 GLYPHS > 1 means show glyphs in long form. */
12293
12294 void
12295 dump_glyph_matrix (matrix, glyphs)
12296 struct glyph_matrix *matrix;
12297 int glyphs;
12298 {
12299 int i;
12300 for (i = 0; i < matrix->nrows; ++i)
12301 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
12302 }
12303
12304
12305 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
12306 the glyph row and area where the glyph comes from. */
12307
12308 void
12309 dump_glyph (row, glyph, area)
12310 struct glyph_row *row;
12311 struct glyph *glyph;
12312 int area;
12313 {
12314 if (glyph->type == CHAR_GLYPH)
12315 {
12316 fprintf (stderr,
12317 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12318 glyph - row->glyphs[TEXT_AREA],
12319 'C',
12320 glyph->charpos,
12321 (BUFFERP (glyph->object)
12322 ? 'B'
12323 : (STRINGP (glyph->object)
12324 ? 'S'
12325 : '-')),
12326 glyph->pixel_width,
12327 glyph->u.ch,
12328 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
12329 ? glyph->u.ch
12330 : '.'),
12331 glyph->face_id,
12332 glyph->left_box_line_p,
12333 glyph->right_box_line_p);
12334 }
12335 else if (glyph->type == STRETCH_GLYPH)
12336 {
12337 fprintf (stderr,
12338 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12339 glyph - row->glyphs[TEXT_AREA],
12340 'S',
12341 glyph->charpos,
12342 (BUFFERP (glyph->object)
12343 ? 'B'
12344 : (STRINGP (glyph->object)
12345 ? 'S'
12346 : '-')),
12347 glyph->pixel_width,
12348 0,
12349 '.',
12350 glyph->face_id,
12351 glyph->left_box_line_p,
12352 glyph->right_box_line_p);
12353 }
12354 else if (glyph->type == IMAGE_GLYPH)
12355 {
12356 fprintf (stderr,
12357 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12358 glyph - row->glyphs[TEXT_AREA],
12359 'I',
12360 glyph->charpos,
12361 (BUFFERP (glyph->object)
12362 ? 'B'
12363 : (STRINGP (glyph->object)
12364 ? 'S'
12365 : '-')),
12366 glyph->pixel_width,
12367 glyph->u.img_id,
12368 '.',
12369 glyph->face_id,
12370 glyph->left_box_line_p,
12371 glyph->right_box_line_p);
12372 }
12373 }
12374
12375
12376 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
12377 GLYPHS 0 means don't show glyph contents.
12378 GLYPHS 1 means show glyphs in short form
12379 GLYPHS > 1 means show glyphs in long form. */
12380
12381 void
12382 dump_glyph_row (row, vpos, glyphs)
12383 struct glyph_row *row;
12384 int vpos, glyphs;
12385 {
12386 if (glyphs != 1)
12387 {
12388 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
12389 fprintf (stderr, "=======================================================================\n");
12390
12391 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d\
12392 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
12393 vpos,
12394 MATRIX_ROW_START_CHARPOS (row),
12395 MATRIX_ROW_END_CHARPOS (row),
12396 row->used[TEXT_AREA],
12397 row->contains_overlapping_glyphs_p,
12398 row->enabled_p,
12399 row->truncated_on_left_p,
12400 row->truncated_on_right_p,
12401 row->overlay_arrow_p,
12402 row->continued_p,
12403 MATRIX_ROW_CONTINUATION_LINE_P (row),
12404 row->displays_text_p,
12405 row->ends_at_zv_p,
12406 row->fill_line_p,
12407 row->ends_in_middle_of_char_p,
12408 row->starts_in_middle_of_char_p,
12409 row->mouse_face_p,
12410 row->x,
12411 row->y,
12412 row->pixel_width,
12413 row->height,
12414 row->visible_height,
12415 row->ascent,
12416 row->phys_ascent);
12417 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
12418 row->end.overlay_string_index,
12419 row->continuation_lines_width);
12420 fprintf (stderr, "%9d %5d\n",
12421 CHARPOS (row->start.string_pos),
12422 CHARPOS (row->end.string_pos));
12423 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
12424 row->end.dpvec_index);
12425 }
12426
12427 if (glyphs > 1)
12428 {
12429 int area;
12430
12431 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12432 {
12433 struct glyph *glyph = row->glyphs[area];
12434 struct glyph *glyph_end = glyph + row->used[area];
12435
12436 /* Glyph for a line end in text. */
12437 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
12438 ++glyph_end;
12439
12440 if (glyph < glyph_end)
12441 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
12442
12443 for (; glyph < glyph_end; ++glyph)
12444 dump_glyph (row, glyph, area);
12445 }
12446 }
12447 else if (glyphs == 1)
12448 {
12449 int area;
12450
12451 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12452 {
12453 char *s = (char *) alloca (row->used[area] + 1);
12454 int i;
12455
12456 for (i = 0; i < row->used[area]; ++i)
12457 {
12458 struct glyph *glyph = row->glyphs[area] + i;
12459 if (glyph->type == CHAR_GLYPH
12460 && glyph->u.ch < 0x80
12461 && glyph->u.ch >= ' ')
12462 s[i] = glyph->u.ch;
12463 else
12464 s[i] = '.';
12465 }
12466
12467 s[i] = '\0';
12468 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
12469 }
12470 }
12471 }
12472
12473
12474 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
12475 Sdump_glyph_matrix, 0, 1, "p",
12476 doc: /* Dump the current matrix of the selected window to stderr.
12477 Shows contents of glyph row structures. With non-nil
12478 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
12479 glyphs in short form, otherwise show glyphs in long form. */)
12480 (glyphs)
12481 Lisp_Object glyphs;
12482 {
12483 struct window *w = XWINDOW (selected_window);
12484 struct buffer *buffer = XBUFFER (w->buffer);
12485
12486 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
12487 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
12488 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
12489 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
12490 fprintf (stderr, "=============================================\n");
12491 dump_glyph_matrix (w->current_matrix,
12492 NILP (glyphs) ? 0 : XINT (glyphs));
12493 return Qnil;
12494 }
12495
12496
12497 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
12498 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
12499 ()
12500 {
12501 struct frame *f = XFRAME (selected_frame);
12502 dump_glyph_matrix (f->current_matrix, 1);
12503 return Qnil;
12504 }
12505
12506
12507 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
12508 doc: /* Dump glyph row ROW to stderr.
12509 GLYPH 0 means don't dump glyphs.
12510 GLYPH 1 means dump glyphs in short form.
12511 GLYPH > 1 or omitted means dump glyphs in long form. */)
12512 (row, glyphs)
12513 Lisp_Object row, glyphs;
12514 {
12515 struct glyph_matrix *matrix;
12516 int vpos;
12517
12518 CHECK_NUMBER (row);
12519 matrix = XWINDOW (selected_window)->current_matrix;
12520 vpos = XINT (row);
12521 if (vpos >= 0 && vpos < matrix->nrows)
12522 dump_glyph_row (MATRIX_ROW (matrix, vpos),
12523 vpos,
12524 INTEGERP (glyphs) ? XINT (glyphs) : 2);
12525 return Qnil;
12526 }
12527
12528
12529 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
12530 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
12531 GLYPH 0 means don't dump glyphs.
12532 GLYPH 1 means dump glyphs in short form.
12533 GLYPH > 1 or omitted means dump glyphs in long form. */)
12534 (row, glyphs)
12535 Lisp_Object row, glyphs;
12536 {
12537 struct frame *sf = SELECTED_FRAME ();
12538 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
12539 int vpos;
12540
12541 CHECK_NUMBER (row);
12542 vpos = XINT (row);
12543 if (vpos >= 0 && vpos < m->nrows)
12544 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
12545 INTEGERP (glyphs) ? XINT (glyphs) : 2);
12546 return Qnil;
12547 }
12548
12549
12550 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
12551 doc: /* Toggle tracing of redisplay.
12552 With ARG, turn tracing on if and only if ARG is positive. */)
12553 (arg)
12554 Lisp_Object arg;
12555 {
12556 if (NILP (arg))
12557 trace_redisplay_p = !trace_redisplay_p;
12558 else
12559 {
12560 arg = Fprefix_numeric_value (arg);
12561 trace_redisplay_p = XINT (arg) > 0;
12562 }
12563
12564 return Qnil;
12565 }
12566
12567
12568 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
12569 doc: /* Like `format', but print result to stderr.
12570 usage: (trace-to-stderr STRING &rest OBJECTS) */)
12571 (nargs, args)
12572 int nargs;
12573 Lisp_Object *args;
12574 {
12575 Lisp_Object s = Fformat (nargs, args);
12576 fprintf (stderr, "%s", SDATA (s));
12577 return Qnil;
12578 }
12579
12580 #endif /* GLYPH_DEBUG */
12581
12582
12583 \f
12584 /***********************************************************************
12585 Building Desired Matrix Rows
12586 ***********************************************************************/
12587
12588 /* Return a temporary glyph row holding the glyphs of an overlay
12589 arrow. Only used for non-window-redisplay windows. */
12590
12591 static struct glyph_row *
12592 get_overlay_arrow_glyph_row (w)
12593 struct window *w;
12594 {
12595 struct frame *f = XFRAME (WINDOW_FRAME (w));
12596 struct buffer *buffer = XBUFFER (w->buffer);
12597 struct buffer *old = current_buffer;
12598 const unsigned char *arrow_string = SDATA (Voverlay_arrow_string);
12599 int arrow_len = SCHARS (Voverlay_arrow_string);
12600 const unsigned char *arrow_end = arrow_string + arrow_len;
12601 const unsigned char *p;
12602 struct it it;
12603 int multibyte_p;
12604 int n_glyphs_before;
12605
12606 set_buffer_temp (buffer);
12607 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
12608 it.glyph_row->used[TEXT_AREA] = 0;
12609 SET_TEXT_POS (it.position, 0, 0);
12610
12611 multibyte_p = !NILP (buffer->enable_multibyte_characters);
12612 p = arrow_string;
12613 while (p < arrow_end)
12614 {
12615 Lisp_Object face, ilisp;
12616
12617 /* Get the next character. */
12618 if (multibyte_p)
12619 it.c = string_char_and_length (p, arrow_len, &it.len);
12620 else
12621 it.c = *p, it.len = 1;
12622 p += it.len;
12623
12624 /* Get its face. */
12625 ilisp = make_number (p - arrow_string);
12626 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
12627 it.face_id = compute_char_face (f, it.c, face);
12628
12629 /* Compute its width, get its glyphs. */
12630 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
12631 SET_TEXT_POS (it.position, -1, -1);
12632 PRODUCE_GLYPHS (&it);
12633
12634 /* If this character doesn't fit any more in the line, we have
12635 to remove some glyphs. */
12636 if (it.current_x > it.last_visible_x)
12637 {
12638 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
12639 break;
12640 }
12641 }
12642
12643 set_buffer_temp (old);
12644 return it.glyph_row;
12645 }
12646
12647
12648 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
12649 glyphs are only inserted for terminal frames since we can't really
12650 win with truncation glyphs when partially visible glyphs are
12651 involved. Which glyphs to insert is determined by
12652 produce_special_glyphs. */
12653
12654 static void
12655 insert_left_trunc_glyphs (it)
12656 struct it *it;
12657 {
12658 struct it truncate_it;
12659 struct glyph *from, *end, *to, *toend;
12660
12661 xassert (!FRAME_WINDOW_P (it->f));
12662
12663 /* Get the truncation glyphs. */
12664 truncate_it = *it;
12665 truncate_it.current_x = 0;
12666 truncate_it.face_id = DEFAULT_FACE_ID;
12667 truncate_it.glyph_row = &scratch_glyph_row;
12668 truncate_it.glyph_row->used[TEXT_AREA] = 0;
12669 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
12670 truncate_it.object = make_number (0);
12671 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
12672
12673 /* Overwrite glyphs from IT with truncation glyphs. */
12674 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
12675 end = from + truncate_it.glyph_row->used[TEXT_AREA];
12676 to = it->glyph_row->glyphs[TEXT_AREA];
12677 toend = to + it->glyph_row->used[TEXT_AREA];
12678
12679 while (from < end)
12680 *to++ = *from++;
12681
12682 /* There may be padding glyphs left over. Overwrite them too. */
12683 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
12684 {
12685 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
12686 while (from < end)
12687 *to++ = *from++;
12688 }
12689
12690 if (to > toend)
12691 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
12692 }
12693
12694
12695 /* Compute the pixel height and width of IT->glyph_row.
12696
12697 Most of the time, ascent and height of a display line will be equal
12698 to the max_ascent and max_height values of the display iterator
12699 structure. This is not the case if
12700
12701 1. We hit ZV without displaying anything. In this case, max_ascent
12702 and max_height will be zero.
12703
12704 2. We have some glyphs that don't contribute to the line height.
12705 (The glyph row flag contributes_to_line_height_p is for future
12706 pixmap extensions).
12707
12708 The first case is easily covered by using default values because in
12709 these cases, the line height does not really matter, except that it
12710 must not be zero. */
12711
12712 static void
12713 compute_line_metrics (it)
12714 struct it *it;
12715 {
12716 struct glyph_row *row = it->glyph_row;
12717 int area, i;
12718
12719 if (FRAME_WINDOW_P (it->f))
12720 {
12721 int i, min_y, max_y;
12722
12723 /* The line may consist of one space only, that was added to
12724 place the cursor on it. If so, the row's height hasn't been
12725 computed yet. */
12726 if (row->height == 0)
12727 {
12728 if (it->max_ascent + it->max_descent == 0)
12729 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
12730 row->ascent = it->max_ascent;
12731 row->height = it->max_ascent + it->max_descent;
12732 row->phys_ascent = it->max_phys_ascent;
12733 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
12734 }
12735
12736 /* Compute the width of this line. */
12737 row->pixel_width = row->x;
12738 for (i = 0; i < row->used[TEXT_AREA]; ++i)
12739 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
12740
12741 xassert (row->pixel_width >= 0);
12742 xassert (row->ascent >= 0 && row->height > 0);
12743
12744 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
12745 || MATRIX_ROW_OVERLAPS_PRED_P (row));
12746
12747 /* If first line's physical ascent is larger than its logical
12748 ascent, use the physical ascent, and make the row taller.
12749 This makes accented characters fully visible. */
12750 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
12751 && row->phys_ascent > row->ascent)
12752 {
12753 row->height += row->phys_ascent - row->ascent;
12754 row->ascent = row->phys_ascent;
12755 }
12756
12757 /* Compute how much of the line is visible. */
12758 row->visible_height = row->height;
12759
12760 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (it->w);
12761 max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
12762
12763 if (row->y < min_y)
12764 row->visible_height -= min_y - row->y;
12765 if (row->y + row->height > max_y)
12766 row->visible_height -= row->y + row->height - max_y;
12767 }
12768 else
12769 {
12770 row->pixel_width = row->used[TEXT_AREA];
12771 if (row->continued_p)
12772 row->pixel_width -= it->continuation_pixel_width;
12773 else if (row->truncated_on_right_p)
12774 row->pixel_width -= it->truncation_pixel_width;
12775 row->ascent = row->phys_ascent = 0;
12776 row->height = row->phys_height = row->visible_height = 1;
12777 }
12778
12779 /* Compute a hash code for this row. */
12780 row->hash = 0;
12781 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12782 for (i = 0; i < row->used[area]; ++i)
12783 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
12784 + row->glyphs[area][i].u.val
12785 + row->glyphs[area][i].face_id
12786 + row->glyphs[area][i].padding_p
12787 + (row->glyphs[area][i].type << 2));
12788
12789 it->max_ascent = it->max_descent = 0;
12790 it->max_phys_ascent = it->max_phys_descent = 0;
12791 }
12792
12793
12794 /* Append one space to the glyph row of iterator IT if doing a
12795 window-based redisplay. DEFAULT_FACE_P non-zero means let the
12796 space have the default face, otherwise let it have the same face as
12797 IT->face_id. Value is non-zero if a space was added.
12798
12799 This function is called to make sure that there is always one glyph
12800 at the end of a glyph row that the cursor can be set on under
12801 window-systems. (If there weren't such a glyph we would not know
12802 how wide and tall a box cursor should be displayed).
12803
12804 At the same time this space let's a nicely handle clearing to the
12805 end of the line if the row ends in italic text. */
12806
12807 static int
12808 append_space (it, default_face_p)
12809 struct it *it;
12810 int default_face_p;
12811 {
12812 if (FRAME_WINDOW_P (it->f))
12813 {
12814 int n = it->glyph_row->used[TEXT_AREA];
12815
12816 if (it->glyph_row->glyphs[TEXT_AREA] + n
12817 < it->glyph_row->glyphs[1 + TEXT_AREA])
12818 {
12819 /* Save some values that must not be changed.
12820 Must save IT->c and IT->len because otherwise
12821 ITERATOR_AT_END_P wouldn't work anymore after
12822 append_space has been called. */
12823 enum display_element_type saved_what = it->what;
12824 int saved_c = it->c, saved_len = it->len;
12825 int saved_x = it->current_x;
12826 int saved_face_id = it->face_id;
12827 struct text_pos saved_pos;
12828 Lisp_Object saved_object;
12829 struct face *face;
12830
12831 saved_object = it->object;
12832 saved_pos = it->position;
12833
12834 it->what = IT_CHARACTER;
12835 bzero (&it->position, sizeof it->position);
12836 it->object = make_number (0);
12837 it->c = ' ';
12838 it->len = 1;
12839
12840 if (default_face_p)
12841 it->face_id = DEFAULT_FACE_ID;
12842 else if (it->face_before_selective_p)
12843 it->face_id = it->saved_face_id;
12844 face = FACE_FROM_ID (it->f, it->face_id);
12845 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
12846
12847 PRODUCE_GLYPHS (it);
12848
12849 it->current_x = saved_x;
12850 it->object = saved_object;
12851 it->position = saved_pos;
12852 it->what = saved_what;
12853 it->face_id = saved_face_id;
12854 it->len = saved_len;
12855 it->c = saved_c;
12856 return 1;
12857 }
12858 }
12859
12860 return 0;
12861 }
12862
12863
12864 /* Extend the face of the last glyph in the text area of IT->glyph_row
12865 to the end of the display line. Called from display_line.
12866 If the glyph row is empty, add a space glyph to it so that we
12867 know the face to draw. Set the glyph row flag fill_line_p. */
12868
12869 static void
12870 extend_face_to_end_of_line (it)
12871 struct it *it;
12872 {
12873 struct face *face;
12874 struct frame *f = it->f;
12875
12876 /* If line is already filled, do nothing. */
12877 if (it->current_x >= it->last_visible_x)
12878 return;
12879
12880 /* Face extension extends the background and box of IT->face_id
12881 to the end of the line. If the background equals the background
12882 of the frame, we don't have to do anything. */
12883 if (it->face_before_selective_p)
12884 face = FACE_FROM_ID (it->f, it->saved_face_id);
12885 else
12886 face = FACE_FROM_ID (f, it->face_id);
12887
12888 if (FRAME_WINDOW_P (f)
12889 && face->box == FACE_NO_BOX
12890 && face->background == FRAME_BACKGROUND_PIXEL (f)
12891 && !face->stipple)
12892 return;
12893
12894 /* Set the glyph row flag indicating that the face of the last glyph
12895 in the text area has to be drawn to the end of the text area. */
12896 it->glyph_row->fill_line_p = 1;
12897
12898 /* If current character of IT is not ASCII, make sure we have the
12899 ASCII face. This will be automatically undone the next time
12900 get_next_display_element returns a multibyte character. Note
12901 that the character will always be single byte in unibyte text. */
12902 if (!SINGLE_BYTE_CHAR_P (it->c))
12903 {
12904 it->face_id = FACE_FOR_CHAR (f, face, 0);
12905 }
12906
12907 if (FRAME_WINDOW_P (f))
12908 {
12909 /* If the row is empty, add a space with the current face of IT,
12910 so that we know which face to draw. */
12911 if (it->glyph_row->used[TEXT_AREA] == 0)
12912 {
12913 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
12914 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
12915 it->glyph_row->used[TEXT_AREA] = 1;
12916 }
12917 }
12918 else
12919 {
12920 /* Save some values that must not be changed. */
12921 int saved_x = it->current_x;
12922 struct text_pos saved_pos;
12923 Lisp_Object saved_object;
12924 enum display_element_type saved_what = it->what;
12925 int saved_face_id = it->face_id;
12926
12927 saved_object = it->object;
12928 saved_pos = it->position;
12929
12930 it->what = IT_CHARACTER;
12931 bzero (&it->position, sizeof it->position);
12932 it->object = make_number (0);
12933 it->c = ' ';
12934 it->len = 1;
12935 it->face_id = face->id;
12936
12937 PRODUCE_GLYPHS (it);
12938
12939 while (it->current_x <= it->last_visible_x)
12940 PRODUCE_GLYPHS (it);
12941
12942 /* Don't count these blanks really. It would let us insert a left
12943 truncation glyph below and make us set the cursor on them, maybe. */
12944 it->current_x = saved_x;
12945 it->object = saved_object;
12946 it->position = saved_pos;
12947 it->what = saved_what;
12948 it->face_id = saved_face_id;
12949 }
12950 }
12951
12952
12953 /* Value is non-zero if text starting at CHARPOS in current_buffer is
12954 trailing whitespace. */
12955
12956 static int
12957 trailing_whitespace_p (charpos)
12958 int charpos;
12959 {
12960 int bytepos = CHAR_TO_BYTE (charpos);
12961 int c = 0;
12962
12963 while (bytepos < ZV_BYTE
12964 && (c = FETCH_CHAR (bytepos),
12965 c == ' ' || c == '\t'))
12966 ++bytepos;
12967
12968 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
12969 {
12970 if (bytepos != PT_BYTE)
12971 return 1;
12972 }
12973 return 0;
12974 }
12975
12976
12977 /* Highlight trailing whitespace, if any, in ROW. */
12978
12979 void
12980 highlight_trailing_whitespace (f, row)
12981 struct frame *f;
12982 struct glyph_row *row;
12983 {
12984 int used = row->used[TEXT_AREA];
12985
12986 if (used)
12987 {
12988 struct glyph *start = row->glyphs[TEXT_AREA];
12989 struct glyph *glyph = start + used - 1;
12990
12991 /* Skip over glyphs inserted to display the cursor at the
12992 end of a line, for extending the face of the last glyph
12993 to the end of the line on terminals, and for truncation
12994 and continuation glyphs. */
12995 while (glyph >= start
12996 && glyph->type == CHAR_GLYPH
12997 && INTEGERP (glyph->object))
12998 --glyph;
12999
13000 /* If last glyph is a space or stretch, and it's trailing
13001 whitespace, set the face of all trailing whitespace glyphs in
13002 IT->glyph_row to `trailing-whitespace'. */
13003 if (glyph >= start
13004 && BUFFERP (glyph->object)
13005 && (glyph->type == STRETCH_GLYPH
13006 || (glyph->type == CHAR_GLYPH
13007 && glyph->u.ch == ' '))
13008 && trailing_whitespace_p (glyph->charpos))
13009 {
13010 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
13011
13012 while (glyph >= start
13013 && BUFFERP (glyph->object)
13014 && (glyph->type == STRETCH_GLYPH
13015 || (glyph->type == CHAR_GLYPH
13016 && glyph->u.ch == ' ')))
13017 (glyph--)->face_id = face_id;
13018 }
13019 }
13020 }
13021
13022
13023 /* Value is non-zero if glyph row ROW in window W should be
13024 used to hold the cursor. */
13025
13026 static int
13027 cursor_row_p (w, row)
13028 struct window *w;
13029 struct glyph_row *row;
13030 {
13031 int cursor_row_p = 1;
13032
13033 if (PT == MATRIX_ROW_END_CHARPOS (row))
13034 {
13035 /* If the row ends with a newline from a string, we don't want
13036 the cursor there (if the row is continued it doesn't end in a
13037 newline). */
13038 if (CHARPOS (row->end.string_pos) >= 0
13039 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
13040 cursor_row_p = row->continued_p;
13041
13042 /* If the row ends at ZV, display the cursor at the end of that
13043 row instead of at the start of the row below. */
13044 else if (row->ends_at_zv_p)
13045 cursor_row_p = 1;
13046 else
13047 cursor_row_p = 0;
13048 }
13049
13050 return cursor_row_p;
13051 }
13052
13053
13054 /* Construct the glyph row IT->glyph_row in the desired matrix of
13055 IT->w from text at the current position of IT. See dispextern.h
13056 for an overview of struct it. Value is non-zero if
13057 IT->glyph_row displays text, as opposed to a line displaying ZV
13058 only. */
13059
13060 static int
13061 display_line (it)
13062 struct it *it;
13063 {
13064 struct glyph_row *row = it->glyph_row;
13065
13066 /* We always start displaying at hpos zero even if hscrolled. */
13067 xassert (it->hpos == 0 && it->current_x == 0);
13068
13069 /* We must not display in a row that's not a text row. */
13070 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
13071 < it->w->desired_matrix->nrows);
13072
13073 /* Is IT->w showing the region? */
13074 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
13075
13076 /* Clear the result glyph row and enable it. */
13077 prepare_desired_row (row);
13078
13079 row->y = it->current_y;
13080 row->start = it->current;
13081 row->continuation_lines_width = it->continuation_lines_width;
13082 row->displays_text_p = 1;
13083 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
13084 it->starts_in_middle_of_char_p = 0;
13085
13086 /* Arrange the overlays nicely for our purposes. Usually, we call
13087 display_line on only one line at a time, in which case this
13088 can't really hurt too much, or we call it on lines which appear
13089 one after another in the buffer, in which case all calls to
13090 recenter_overlay_lists but the first will be pretty cheap. */
13091 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
13092
13093 /* Move over display elements that are not visible because we are
13094 hscrolled. This may stop at an x-position < IT->first_visible_x
13095 if the first glyph is partially visible or if we hit a line end. */
13096 if (it->current_x < it->first_visible_x)
13097 move_it_in_display_line_to (it, ZV, it->first_visible_x,
13098 MOVE_TO_POS | MOVE_TO_X);
13099
13100 /* Get the initial row height. This is either the height of the
13101 text hscrolled, if there is any, or zero. */
13102 row->ascent = it->max_ascent;
13103 row->height = it->max_ascent + it->max_descent;
13104 row->phys_ascent = it->max_phys_ascent;
13105 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
13106
13107 /* Loop generating characters. The loop is left with IT on the next
13108 character to display. */
13109 while (1)
13110 {
13111 int n_glyphs_before, hpos_before, x_before;
13112 int x, i, nglyphs;
13113 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
13114
13115 /* Retrieve the next thing to display. Value is zero if end of
13116 buffer reached. */
13117 if (!get_next_display_element (it))
13118 {
13119 /* Maybe add a space at the end of this line that is used to
13120 display the cursor there under X. Set the charpos of the
13121 first glyph of blank lines not corresponding to any text
13122 to -1. */
13123 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
13124 || row->used[TEXT_AREA] == 0)
13125 {
13126 row->glyphs[TEXT_AREA]->charpos = -1;
13127 row->displays_text_p = 0;
13128
13129 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
13130 && (!MINI_WINDOW_P (it->w)
13131 || (minibuf_level && EQ (it->window, minibuf_window))))
13132 row->indicate_empty_line_p = 1;
13133 }
13134
13135 it->continuation_lines_width = 0;
13136 row->ends_at_zv_p = 1;
13137 break;
13138 }
13139
13140 /* Now, get the metrics of what we want to display. This also
13141 generates glyphs in `row' (which is IT->glyph_row). */
13142 n_glyphs_before = row->used[TEXT_AREA];
13143 x = it->current_x;
13144
13145 /* Remember the line height so far in case the next element doesn't
13146 fit on the line. */
13147 if (!it->truncate_lines_p)
13148 {
13149 ascent = it->max_ascent;
13150 descent = it->max_descent;
13151 phys_ascent = it->max_phys_ascent;
13152 phys_descent = it->max_phys_descent;
13153 }
13154
13155 PRODUCE_GLYPHS (it);
13156
13157 /* If this display element was in marginal areas, continue with
13158 the next one. */
13159 if (it->area != TEXT_AREA)
13160 {
13161 row->ascent = max (row->ascent, it->max_ascent);
13162 row->height = max (row->height, it->max_ascent + it->max_descent);
13163 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13164 row->phys_height = max (row->phys_height,
13165 it->max_phys_ascent + it->max_phys_descent);
13166 set_iterator_to_next (it, 1);
13167 continue;
13168 }
13169
13170 /* Does the display element fit on the line? If we truncate
13171 lines, we should draw past the right edge of the window. If
13172 we don't truncate, we want to stop so that we can display the
13173 continuation glyph before the right margin. If lines are
13174 continued, there are two possible strategies for characters
13175 resulting in more than 1 glyph (e.g. tabs): Display as many
13176 glyphs as possible in this line and leave the rest for the
13177 continuation line, or display the whole element in the next
13178 line. Original redisplay did the former, so we do it also. */
13179 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
13180 hpos_before = it->hpos;
13181 x_before = x;
13182
13183 if (/* Not a newline. */
13184 nglyphs > 0
13185 /* Glyphs produced fit entirely in the line. */
13186 && it->current_x < it->last_visible_x)
13187 {
13188 it->hpos += nglyphs;
13189 row->ascent = max (row->ascent, it->max_ascent);
13190 row->height = max (row->height, it->max_ascent + it->max_descent);
13191 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13192 row->phys_height = max (row->phys_height,
13193 it->max_phys_ascent + it->max_phys_descent);
13194 if (it->current_x - it->pixel_width < it->first_visible_x)
13195 row->x = x - it->first_visible_x;
13196 }
13197 else
13198 {
13199 int new_x;
13200 struct glyph *glyph;
13201
13202 for (i = 0; i < nglyphs; ++i, x = new_x)
13203 {
13204 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
13205 new_x = x + glyph->pixel_width;
13206
13207 if (/* Lines are continued. */
13208 !it->truncate_lines_p
13209 && (/* Glyph doesn't fit on the line. */
13210 new_x > it->last_visible_x
13211 /* Or it fits exactly on a window system frame. */
13212 || (new_x == it->last_visible_x
13213 && FRAME_WINDOW_P (it->f))))
13214 {
13215 /* End of a continued line. */
13216
13217 if (it->hpos == 0
13218 || (new_x == it->last_visible_x
13219 && FRAME_WINDOW_P (it->f)))
13220 {
13221 /* Current glyph is the only one on the line or
13222 fits exactly on the line. We must continue
13223 the line because we can't draw the cursor
13224 after the glyph. */
13225 row->continued_p = 1;
13226 it->current_x = new_x;
13227 it->continuation_lines_width += new_x;
13228 ++it->hpos;
13229 if (i == nglyphs - 1)
13230 set_iterator_to_next (it, 1);
13231 }
13232 else if (CHAR_GLYPH_PADDING_P (*glyph)
13233 && !FRAME_WINDOW_P (it->f))
13234 {
13235 /* A padding glyph that doesn't fit on this line.
13236 This means the whole character doesn't fit
13237 on the line. */
13238 row->used[TEXT_AREA] = n_glyphs_before;
13239
13240 /* Fill the rest of the row with continuation
13241 glyphs like in 20.x. */
13242 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
13243 < row->glyphs[1 + TEXT_AREA])
13244 produce_special_glyphs (it, IT_CONTINUATION);
13245
13246 row->continued_p = 1;
13247 it->current_x = x_before;
13248 it->continuation_lines_width += x_before;
13249
13250 /* Restore the height to what it was before the
13251 element not fitting on the line. */
13252 it->max_ascent = ascent;
13253 it->max_descent = descent;
13254 it->max_phys_ascent = phys_ascent;
13255 it->max_phys_descent = phys_descent;
13256 }
13257 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
13258 {
13259 /* A TAB that extends past the right edge of the
13260 window. This produces a single glyph on
13261 window system frames. We leave the glyph in
13262 this row and let it fill the row, but don't
13263 consume the TAB. */
13264 it->continuation_lines_width += it->last_visible_x;
13265 row->ends_in_middle_of_char_p = 1;
13266 row->continued_p = 1;
13267 glyph->pixel_width = it->last_visible_x - x;
13268 it->starts_in_middle_of_char_p = 1;
13269 }
13270 else
13271 {
13272 /* Something other than a TAB that draws past
13273 the right edge of the window. Restore
13274 positions to values before the element. */
13275 row->used[TEXT_AREA] = n_glyphs_before + i;
13276
13277 /* Display continuation glyphs. */
13278 if (!FRAME_WINDOW_P (it->f))
13279 produce_special_glyphs (it, IT_CONTINUATION);
13280 row->continued_p = 1;
13281
13282 it->continuation_lines_width += x;
13283
13284 if (nglyphs > 1 && i > 0)
13285 {
13286 row->ends_in_middle_of_char_p = 1;
13287 it->starts_in_middle_of_char_p = 1;
13288 }
13289
13290 /* Restore the height to what it was before the
13291 element not fitting on the line. */
13292 it->max_ascent = ascent;
13293 it->max_descent = descent;
13294 it->max_phys_ascent = phys_ascent;
13295 it->max_phys_descent = phys_descent;
13296 }
13297
13298 break;
13299 }
13300 else if (new_x > it->first_visible_x)
13301 {
13302 /* Increment number of glyphs actually displayed. */
13303 ++it->hpos;
13304
13305 if (x < it->first_visible_x)
13306 /* Glyph is partially visible, i.e. row starts at
13307 negative X position. */
13308 row->x = x - it->first_visible_x;
13309 }
13310 else
13311 {
13312 /* Glyph is completely off the left margin of the
13313 window. This should not happen because of the
13314 move_it_in_display_line at the start of this
13315 function, unless the text display area of the
13316 window is empty. */
13317 xassert (it->first_visible_x <= it->last_visible_x);
13318 }
13319 }
13320
13321 row->ascent = max (row->ascent, it->max_ascent);
13322 row->height = max (row->height, it->max_ascent + it->max_descent);
13323 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13324 row->phys_height = max (row->phys_height,
13325 it->max_phys_ascent + it->max_phys_descent);
13326
13327 /* End of this display line if row is continued. */
13328 if (row->continued_p)
13329 break;
13330 }
13331
13332 /* Is this a line end? If yes, we're also done, after making
13333 sure that a non-default face is extended up to the right
13334 margin of the window. */
13335 if (ITERATOR_AT_END_OF_LINE_P (it))
13336 {
13337 int used_before = row->used[TEXT_AREA];
13338
13339 row->ends_in_newline_from_string_p = STRINGP (it->object);
13340
13341 /* Add a space at the end of the line that is used to
13342 display the cursor there. */
13343 append_space (it, 0);
13344
13345 /* Extend the face to the end of the line. */
13346 extend_face_to_end_of_line (it);
13347
13348 /* Make sure we have the position. */
13349 if (used_before == 0)
13350 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
13351
13352 /* Consume the line end. This skips over invisible lines. */
13353 set_iterator_to_next (it, 1);
13354 it->continuation_lines_width = 0;
13355 break;
13356 }
13357
13358 /* Proceed with next display element. Note that this skips
13359 over lines invisible because of selective display. */
13360 set_iterator_to_next (it, 1);
13361
13362 /* If we truncate lines, we are done when the last displayed
13363 glyphs reach past the right margin of the window. */
13364 if (it->truncate_lines_p
13365 && (FRAME_WINDOW_P (it->f)
13366 ? (it->current_x >= it->last_visible_x)
13367 : (it->current_x > it->last_visible_x)))
13368 {
13369 /* Maybe add truncation glyphs. */
13370 if (!FRAME_WINDOW_P (it->f))
13371 {
13372 int i, n;
13373
13374 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
13375 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
13376 break;
13377
13378 for (n = row->used[TEXT_AREA]; i < n; ++i)
13379 {
13380 row->used[TEXT_AREA] = i;
13381 produce_special_glyphs (it, IT_TRUNCATION);
13382 }
13383 }
13384
13385 row->truncated_on_right_p = 1;
13386 it->continuation_lines_width = 0;
13387 reseat_at_next_visible_line_start (it, 0);
13388 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
13389 it->hpos = hpos_before;
13390 it->current_x = x_before;
13391 break;
13392 }
13393 }
13394
13395 /* If line is not empty and hscrolled, maybe insert truncation glyphs
13396 at the left window margin. */
13397 if (it->first_visible_x
13398 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
13399 {
13400 if (!FRAME_WINDOW_P (it->f))
13401 insert_left_trunc_glyphs (it);
13402 row->truncated_on_left_p = 1;
13403 }
13404
13405 /* If the start of this line is the overlay arrow-position, then
13406 mark this glyph row as the one containing the overlay arrow.
13407 This is clearly a mess with variable size fonts. It would be
13408 better to let it be displayed like cursors under X. */
13409 if (MARKERP (Voverlay_arrow_position)
13410 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
13411 && (MATRIX_ROW_START_CHARPOS (row)
13412 == marker_position (Voverlay_arrow_position))
13413 && STRINGP (Voverlay_arrow_string)
13414 && ! overlay_arrow_seen)
13415 {
13416 /* Overlay arrow in window redisplay is a fringe bitmap. */
13417 if (!FRAME_WINDOW_P (it->f))
13418 {
13419 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
13420 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
13421 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
13422 struct glyph *p = row->glyphs[TEXT_AREA];
13423 struct glyph *p2, *end;
13424
13425 /* Copy the arrow glyphs. */
13426 while (glyph < arrow_end)
13427 *p++ = *glyph++;
13428
13429 /* Throw away padding glyphs. */
13430 p2 = p;
13431 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
13432 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
13433 ++p2;
13434 if (p2 > p)
13435 {
13436 while (p2 < end)
13437 *p++ = *p2++;
13438 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
13439 }
13440 }
13441
13442 overlay_arrow_seen = 1;
13443 row->overlay_arrow_p = 1;
13444 }
13445
13446 /* Compute pixel dimensions of this line. */
13447 compute_line_metrics (it);
13448
13449 /* Remember the position at which this line ends. */
13450 row->end = it->current;
13451
13452 /* Maybe set the cursor. */
13453 if (it->w->cursor.vpos < 0
13454 && PT >= MATRIX_ROW_START_CHARPOS (row)
13455 && PT <= MATRIX_ROW_END_CHARPOS (row)
13456 && cursor_row_p (it->w, row))
13457 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
13458
13459 /* Highlight trailing whitespace. */
13460 if (!NILP (Vshow_trailing_whitespace))
13461 highlight_trailing_whitespace (it->f, it->glyph_row);
13462
13463 /* Prepare for the next line. This line starts horizontally at (X
13464 HPOS) = (0 0). Vertical positions are incremented. As a
13465 convenience for the caller, IT->glyph_row is set to the next
13466 row to be used. */
13467 it->current_x = it->hpos = 0;
13468 it->current_y += row->height;
13469 ++it->vpos;
13470 ++it->glyph_row;
13471 return row->displays_text_p;
13472 }
13473
13474
13475 \f
13476 /***********************************************************************
13477 Menu Bar
13478 ***********************************************************************/
13479
13480 /* Redisplay the menu bar in the frame for window W.
13481
13482 The menu bar of X frames that don't have X toolkit support is
13483 displayed in a special window W->frame->menu_bar_window.
13484
13485 The menu bar of terminal frames is treated specially as far as
13486 glyph matrices are concerned. Menu bar lines are not part of
13487 windows, so the update is done directly on the frame matrix rows
13488 for the menu bar. */
13489
13490 static void
13491 display_menu_bar (w)
13492 struct window *w;
13493 {
13494 struct frame *f = XFRAME (WINDOW_FRAME (w));
13495 struct it it;
13496 Lisp_Object items;
13497 int i;
13498
13499 /* Don't do all this for graphical frames. */
13500 #ifdef HAVE_NTGUI
13501 if (!NILP (Vwindow_system))
13502 return;
13503 #endif
13504 #ifdef USE_X_TOOLKIT
13505 if (FRAME_X_P (f))
13506 return;
13507 #endif
13508 #ifdef MAC_OS
13509 if (FRAME_MAC_P (f))
13510 return;
13511 #endif
13512
13513 #ifdef USE_X_TOOLKIT
13514 xassert (!FRAME_WINDOW_P (f));
13515 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
13516 it.first_visible_x = 0;
13517 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
13518 #else /* not USE_X_TOOLKIT */
13519 if (FRAME_WINDOW_P (f))
13520 {
13521 /* Menu bar lines are displayed in the desired matrix of the
13522 dummy window menu_bar_window. */
13523 struct window *menu_w;
13524 xassert (WINDOWP (f->menu_bar_window));
13525 menu_w = XWINDOW (f->menu_bar_window);
13526 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
13527 MENU_FACE_ID);
13528 it.first_visible_x = 0;
13529 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
13530 }
13531 else
13532 {
13533 /* This is a TTY frame, i.e. character hpos/vpos are used as
13534 pixel x/y. */
13535 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
13536 MENU_FACE_ID);
13537 it.first_visible_x = 0;
13538 it.last_visible_x = FRAME_WIDTH (f);
13539 }
13540 #endif /* not USE_X_TOOLKIT */
13541
13542 if (! mode_line_inverse_video)
13543 /* Force the menu-bar to be displayed in the default face. */
13544 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
13545
13546 /* Clear all rows of the menu bar. */
13547 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
13548 {
13549 struct glyph_row *row = it.glyph_row + i;
13550 clear_glyph_row (row);
13551 row->enabled_p = 1;
13552 row->full_width_p = 1;
13553 }
13554
13555 /* Display all items of the menu bar. */
13556 items = FRAME_MENU_BAR_ITEMS (it.f);
13557 for (i = 0; i < XVECTOR (items)->size; i += 4)
13558 {
13559 Lisp_Object string;
13560
13561 /* Stop at nil string. */
13562 string = AREF (items, i + 1);
13563 if (NILP (string))
13564 break;
13565
13566 /* Remember where item was displayed. */
13567 AREF (items, i + 3) = make_number (it.hpos);
13568
13569 /* Display the item, pad with one space. */
13570 if (it.current_x < it.last_visible_x)
13571 display_string (NULL, string, Qnil, 0, 0, &it,
13572 SCHARS (string) + 1, 0, 0, -1);
13573 }
13574
13575 /* Fill out the line with spaces. */
13576 if (it.current_x < it.last_visible_x)
13577 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
13578
13579 /* Compute the total height of the lines. */
13580 compute_line_metrics (&it);
13581 }
13582
13583
13584 \f
13585 /***********************************************************************
13586 Mode Line
13587 ***********************************************************************/
13588
13589 /* Redisplay mode lines in the window tree whose root is WINDOW. If
13590 FORCE is non-zero, redisplay mode lines unconditionally.
13591 Otherwise, redisplay only mode lines that are garbaged. Value is
13592 the number of windows whose mode lines were redisplayed. */
13593
13594 static int
13595 redisplay_mode_lines (window, force)
13596 Lisp_Object window;
13597 int force;
13598 {
13599 int nwindows = 0;
13600
13601 while (!NILP (window))
13602 {
13603 struct window *w = XWINDOW (window);
13604
13605 if (WINDOWP (w->hchild))
13606 nwindows += redisplay_mode_lines (w->hchild, force);
13607 else if (WINDOWP (w->vchild))
13608 nwindows += redisplay_mode_lines (w->vchild, force);
13609 else if (force
13610 || FRAME_GARBAGED_P (XFRAME (w->frame))
13611 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
13612 {
13613 struct text_pos lpoint;
13614 struct buffer *old = current_buffer;
13615
13616 /* Set the window's buffer for the mode line display. */
13617 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13618 set_buffer_internal_1 (XBUFFER (w->buffer));
13619
13620 /* Point refers normally to the selected window. For any
13621 other window, set up appropriate value. */
13622 if (!EQ (window, selected_window))
13623 {
13624 struct text_pos pt;
13625
13626 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
13627 if (CHARPOS (pt) < BEGV)
13628 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
13629 else if (CHARPOS (pt) > (ZV - 1))
13630 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
13631 else
13632 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
13633 }
13634
13635 /* Display mode lines. */
13636 clear_glyph_matrix (w->desired_matrix);
13637 if (display_mode_lines (w))
13638 {
13639 ++nwindows;
13640 w->must_be_updated_p = 1;
13641 }
13642
13643 /* Restore old settings. */
13644 set_buffer_internal_1 (old);
13645 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13646 }
13647
13648 window = w->next;
13649 }
13650
13651 return nwindows;
13652 }
13653
13654
13655 /* Display the mode and/or top line of window W. Value is the number
13656 of mode lines displayed. */
13657
13658 static int
13659 display_mode_lines (w)
13660 struct window *w;
13661 {
13662 Lisp_Object old_selected_window, old_selected_frame;
13663 int n = 0;
13664
13665 old_selected_frame = selected_frame;
13666 selected_frame = w->frame;
13667 old_selected_window = selected_window;
13668 XSETWINDOW (selected_window, w);
13669
13670 /* These will be set while the mode line specs are processed. */
13671 line_number_displayed = 0;
13672 w->column_number_displayed = Qnil;
13673
13674 if (WINDOW_WANTS_MODELINE_P (w))
13675 {
13676 struct window *sel_w = XWINDOW (old_selected_window);
13677
13678 /* Select mode line face based on the real selected window. */
13679 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
13680 current_buffer->mode_line_format);
13681 ++n;
13682 }
13683
13684 if (WINDOW_WANTS_HEADER_LINE_P (w))
13685 {
13686 display_mode_line (w, HEADER_LINE_FACE_ID,
13687 current_buffer->header_line_format);
13688 ++n;
13689 }
13690
13691 selected_frame = old_selected_frame;
13692 selected_window = old_selected_window;
13693 return n;
13694 }
13695
13696
13697 /* Display mode or top line of window W. FACE_ID specifies which line
13698 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
13699 FORMAT is the mode line format to display. Value is the pixel
13700 height of the mode line displayed. */
13701
13702 static int
13703 display_mode_line (w, face_id, format)
13704 struct window *w;
13705 enum face_id face_id;
13706 Lisp_Object format;
13707 {
13708 struct it it;
13709 struct face *face;
13710
13711 init_iterator (&it, w, -1, -1, NULL, face_id);
13712 prepare_desired_row (it.glyph_row);
13713
13714 if (! mode_line_inverse_video)
13715 /* Force the mode-line to be displayed in the default face. */
13716 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
13717
13718 /* Temporarily make frame's keyboard the current kboard so that
13719 kboard-local variables in the mode_line_format will get the right
13720 values. */
13721 push_frame_kboard (it.f);
13722 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
13723 pop_frame_kboard ();
13724
13725 /* Fill up with spaces. */
13726 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
13727
13728 compute_line_metrics (&it);
13729 it.glyph_row->full_width_p = 1;
13730 it.glyph_row->mode_line_p = 1;
13731 it.glyph_row->continued_p = 0;
13732 it.glyph_row->truncated_on_left_p = 0;
13733 it.glyph_row->truncated_on_right_p = 0;
13734
13735 /* Make a 3D mode-line have a shadow at its right end. */
13736 face = FACE_FROM_ID (it.f, face_id);
13737 extend_face_to_end_of_line (&it);
13738 if (face->box != FACE_NO_BOX)
13739 {
13740 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
13741 + it.glyph_row->used[TEXT_AREA] - 1);
13742 last->right_box_line_p = 1;
13743 }
13744
13745 return it.glyph_row->height;
13746 }
13747
13748 /* Alist that caches the results of :propertize.
13749 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
13750 Lisp_Object mode_line_proptrans_alist;
13751
13752 /* List of strings making up the mode-line. */
13753 Lisp_Object mode_line_string_list;
13754
13755 /* Base face property when building propertized mode line string. */
13756 static Lisp_Object mode_line_string_face;
13757 static Lisp_Object mode_line_string_face_prop;
13758
13759
13760 /* Contribute ELT to the mode line for window IT->w. How it
13761 translates into text depends on its data type.
13762
13763 IT describes the display environment in which we display, as usual.
13764
13765 DEPTH is the depth in recursion. It is used to prevent
13766 infinite recursion here.
13767
13768 FIELD_WIDTH is the number of characters the display of ELT should
13769 occupy in the mode line, and PRECISION is the maximum number of
13770 characters to display from ELT's representation. See
13771 display_string for details.
13772
13773 Returns the hpos of the end of the text generated by ELT.
13774
13775 PROPS is a property list to add to any string we encounter.
13776
13777 If RISKY is nonzero, remove (disregard) any properties in any string
13778 we encounter, and ignore :eval and :propertize.
13779
13780 If the global variable `frame_title_ptr' is non-NULL, then the output
13781 is passed to `store_frame_title' instead of `display_string'. */
13782
13783 static int
13784 display_mode_element (it, depth, field_width, precision, elt, props, risky)
13785 struct it *it;
13786 int depth;
13787 int field_width, precision;
13788 Lisp_Object elt, props;
13789 int risky;
13790 {
13791 int n = 0, field, prec;
13792 int literal = 0;
13793
13794 tail_recurse:
13795 if (depth > 10)
13796 goto invalid;
13797
13798 depth++;
13799
13800 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
13801 {
13802 case Lisp_String:
13803 {
13804 /* A string: output it and check for %-constructs within it. */
13805 unsigned char c;
13806 const unsigned char *this, *lisp_string;
13807
13808 if (!NILP (props) || risky)
13809 {
13810 Lisp_Object oprops, aelt;
13811 oprops = Ftext_properties_at (make_number (0), elt);
13812
13813 if (NILP (Fequal (props, oprops)) || risky)
13814 {
13815 /* If the starting string has properties,
13816 merge the specified ones onto the existing ones. */
13817 if (! NILP (oprops) && !risky)
13818 {
13819 Lisp_Object tem;
13820
13821 oprops = Fcopy_sequence (oprops);
13822 tem = props;
13823 while (CONSP (tem))
13824 {
13825 oprops = Fplist_put (oprops, XCAR (tem),
13826 XCAR (XCDR (tem)));
13827 tem = XCDR (XCDR (tem));
13828 }
13829 props = oprops;
13830 }
13831
13832 aelt = Fassoc (elt, mode_line_proptrans_alist);
13833 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
13834 {
13835 mode_line_proptrans_alist
13836 = Fcons (aelt, Fdelq (aelt, mode_line_proptrans_alist));
13837 elt = XCAR (aelt);
13838 }
13839 else
13840 {
13841 Lisp_Object tem;
13842
13843 elt = Fcopy_sequence (elt);
13844 Fset_text_properties (make_number (0), Flength (elt),
13845 props, elt);
13846 /* Add this item to mode_line_proptrans_alist. */
13847 mode_line_proptrans_alist
13848 = Fcons (Fcons (elt, props),
13849 mode_line_proptrans_alist);
13850 /* Truncate mode_line_proptrans_alist
13851 to at most 50 elements. */
13852 tem = Fnthcdr (make_number (50),
13853 mode_line_proptrans_alist);
13854 if (! NILP (tem))
13855 XSETCDR (tem, Qnil);
13856 }
13857 }
13858 }
13859
13860 this = SDATA (elt);
13861 lisp_string = this;
13862
13863 if (literal)
13864 {
13865 prec = precision - n;
13866 if (frame_title_ptr)
13867 n += store_frame_title (SDATA (elt), -1, prec);
13868 else if (!NILP (mode_line_string_list))
13869 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
13870 else
13871 n += display_string (NULL, elt, Qnil, 0, 0, it,
13872 0, prec, 0, STRING_MULTIBYTE (elt));
13873
13874 break;
13875 }
13876
13877 while ((precision <= 0 || n < precision)
13878 && *this
13879 && (frame_title_ptr
13880 || !NILP (mode_line_string_list)
13881 || it->current_x < it->last_visible_x))
13882 {
13883 const unsigned char *last = this;
13884
13885 /* Advance to end of string or next format specifier. */
13886 while ((c = *this++) != '\0' && c != '%')
13887 ;
13888
13889 if (this - 1 != last)
13890 {
13891 /* Output to end of string or up to '%'. Field width
13892 is length of string. Don't output more than
13893 PRECISION allows us. */
13894 --this;
13895
13896 prec = chars_in_text (last, this - last);
13897 if (precision > 0 && prec > precision - n)
13898 prec = precision - n;
13899
13900 if (frame_title_ptr)
13901 n += store_frame_title (last, 0, prec);
13902 else if (!NILP (mode_line_string_list))
13903 {
13904 int bytepos = last - lisp_string;
13905 int charpos = string_byte_to_char (elt, bytepos);
13906 n += store_mode_line_string (NULL,
13907 Fsubstring (elt, make_number (charpos),
13908 make_number (charpos + prec)),
13909 0, 0, 0, Qnil);
13910 }
13911 else
13912 {
13913 int bytepos = last - lisp_string;
13914 int charpos = string_byte_to_char (elt, bytepos);
13915 n += display_string (NULL, elt, Qnil, 0, charpos,
13916 it, 0, prec, 0,
13917 STRING_MULTIBYTE (elt));
13918 }
13919 }
13920 else /* c == '%' */
13921 {
13922 const unsigned char *percent_position = this;
13923
13924 /* Get the specified minimum width. Zero means
13925 don't pad. */
13926 field = 0;
13927 while ((c = *this++) >= '0' && c <= '9')
13928 field = field * 10 + c - '0';
13929
13930 /* Don't pad beyond the total padding allowed. */
13931 if (field_width - n > 0 && field > field_width - n)
13932 field = field_width - n;
13933
13934 /* Note that either PRECISION <= 0 or N < PRECISION. */
13935 prec = precision - n;
13936
13937 if (c == 'M')
13938 n += display_mode_element (it, depth, field, prec,
13939 Vglobal_mode_string, props,
13940 risky);
13941 else if (c != 0)
13942 {
13943 int multibyte;
13944 int bytepos, charpos;
13945 unsigned char *spec;
13946
13947 bytepos = percent_position - lisp_string;
13948 charpos = (STRING_MULTIBYTE (elt)
13949 ? string_byte_to_char (elt, bytepos)
13950 : bytepos);
13951
13952 spec
13953 = decode_mode_spec (it->w, c, field, prec, &multibyte);
13954
13955 if (frame_title_ptr)
13956 n += store_frame_title (spec, field, prec);
13957 else if (!NILP (mode_line_string_list))
13958 {
13959 int len = strlen (spec);
13960 Lisp_Object tem = make_string (spec, len);
13961 props = Ftext_properties_at (make_number (charpos), elt);
13962 /* Should only keep face property in props */
13963 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
13964 }
13965 else
13966 {
13967 int nglyphs_before, nwritten;
13968
13969 nglyphs_before = it->glyph_row->used[TEXT_AREA];
13970 nwritten = display_string (spec, Qnil, elt,
13971 charpos, 0, it,
13972 field, prec, 0,
13973 multibyte);
13974
13975 /* Assign to the glyphs written above the
13976 string where the `%x' came from, position
13977 of the `%'. */
13978 if (nwritten > 0)
13979 {
13980 struct glyph *glyph
13981 = (it->glyph_row->glyphs[TEXT_AREA]
13982 + nglyphs_before);
13983 int i;
13984
13985 for (i = 0; i < nwritten; ++i)
13986 {
13987 glyph[i].object = elt;
13988 glyph[i].charpos = charpos;
13989 }
13990
13991 n += nwritten;
13992 }
13993 }
13994 }
13995 else /* c == 0 */
13996 break;
13997 }
13998 }
13999 }
14000 break;
14001
14002 case Lisp_Symbol:
14003 /* A symbol: process the value of the symbol recursively
14004 as if it appeared here directly. Avoid error if symbol void.
14005 Special case: if value of symbol is a string, output the string
14006 literally. */
14007 {
14008 register Lisp_Object tem;
14009
14010 /* If the variable is not marked as risky to set
14011 then its contents are risky to use. */
14012 if (NILP (Fget (elt, Qrisky_local_variable)))
14013 risky = 1;
14014
14015 tem = Fboundp (elt);
14016 if (!NILP (tem))
14017 {
14018 tem = Fsymbol_value (elt);
14019 /* If value is a string, output that string literally:
14020 don't check for % within it. */
14021 if (STRINGP (tem))
14022 literal = 1;
14023
14024 if (!EQ (tem, elt))
14025 {
14026 /* Give up right away for nil or t. */
14027 elt = tem;
14028 goto tail_recurse;
14029 }
14030 }
14031 }
14032 break;
14033
14034 case Lisp_Cons:
14035 {
14036 register Lisp_Object car, tem;
14037
14038 /* A cons cell: five distinct cases.
14039 If first element is :eval or :propertize, do something special.
14040 If first element is a string or a cons, process all the elements
14041 and effectively concatenate them.
14042 If first element is a negative number, truncate displaying cdr to
14043 at most that many characters. If positive, pad (with spaces)
14044 to at least that many characters.
14045 If first element is a symbol, process the cadr or caddr recursively
14046 according to whether the symbol's value is non-nil or nil. */
14047 car = XCAR (elt);
14048 if (EQ (car, QCeval))
14049 {
14050 /* An element of the form (:eval FORM) means evaluate FORM
14051 and use the result as mode line elements. */
14052
14053 if (risky)
14054 break;
14055
14056 if (CONSP (XCDR (elt)))
14057 {
14058 Lisp_Object spec;
14059 spec = safe_eval (XCAR (XCDR (elt)));
14060 n += display_mode_element (it, depth, field_width - n,
14061 precision - n, spec, props,
14062 risky);
14063 }
14064 }
14065 else if (EQ (car, QCpropertize))
14066 {
14067 /* An element of the form (:propertize ELT PROPS...)
14068 means display ELT but applying properties PROPS. */
14069
14070 if (risky)
14071 break;
14072
14073 if (CONSP (XCDR (elt)))
14074 n += display_mode_element (it, depth, field_width - n,
14075 precision - n, XCAR (XCDR (elt)),
14076 XCDR (XCDR (elt)), risky);
14077 }
14078 else if (SYMBOLP (car))
14079 {
14080 tem = Fboundp (car);
14081 elt = XCDR (elt);
14082 if (!CONSP (elt))
14083 goto invalid;
14084 /* elt is now the cdr, and we know it is a cons cell.
14085 Use its car if CAR has a non-nil value. */
14086 if (!NILP (tem))
14087 {
14088 tem = Fsymbol_value (car);
14089 if (!NILP (tem))
14090 {
14091 elt = XCAR (elt);
14092 goto tail_recurse;
14093 }
14094 }
14095 /* Symbol's value is nil (or symbol is unbound)
14096 Get the cddr of the original list
14097 and if possible find the caddr and use that. */
14098 elt = XCDR (elt);
14099 if (NILP (elt))
14100 break;
14101 else if (!CONSP (elt))
14102 goto invalid;
14103 elt = XCAR (elt);
14104 goto tail_recurse;
14105 }
14106 else if (INTEGERP (car))
14107 {
14108 register int lim = XINT (car);
14109 elt = XCDR (elt);
14110 if (lim < 0)
14111 {
14112 /* Negative int means reduce maximum width. */
14113 if (precision <= 0)
14114 precision = -lim;
14115 else
14116 precision = min (precision, -lim);
14117 }
14118 else if (lim > 0)
14119 {
14120 /* Padding specified. Don't let it be more than
14121 current maximum. */
14122 if (precision > 0)
14123 lim = min (precision, lim);
14124
14125 /* If that's more padding than already wanted, queue it.
14126 But don't reduce padding already specified even if
14127 that is beyond the current truncation point. */
14128 field_width = max (lim, field_width);
14129 }
14130 goto tail_recurse;
14131 }
14132 else if (STRINGP (car) || CONSP (car))
14133 {
14134 register int limit = 50;
14135 /* Limit is to protect against circular lists. */
14136 while (CONSP (elt)
14137 && --limit > 0
14138 && (precision <= 0 || n < precision))
14139 {
14140 n += display_mode_element (it, depth, field_width - n,
14141 precision - n, XCAR (elt),
14142 props, risky);
14143 elt = XCDR (elt);
14144 }
14145 }
14146 }
14147 break;
14148
14149 default:
14150 invalid:
14151 if (frame_title_ptr)
14152 n += store_frame_title ("*invalid*", 0, precision - n);
14153 else if (!NILP (mode_line_string_list))
14154 n += store_mode_line_string ("*invalid*", Qnil, 0, 0, precision - n, Qnil);
14155 else
14156 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
14157 precision - n, 0, 0);
14158 return n;
14159 }
14160
14161 /* Pad to FIELD_WIDTH. */
14162 if (field_width > 0 && n < field_width)
14163 {
14164 if (frame_title_ptr)
14165 n += store_frame_title ("", field_width - n, 0);
14166 else if (!NILP (mode_line_string_list))
14167 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
14168 else
14169 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
14170 0, 0, 0);
14171 }
14172
14173 return n;
14174 }
14175
14176 /* Store a mode-line string element in mode_line_string_list.
14177
14178 If STRING is non-null, display that C string. Otherwise, the Lisp
14179 string LISP_STRING is displayed.
14180
14181 FIELD_WIDTH is the minimum number of output glyphs to produce.
14182 If STRING has fewer characters than FIELD_WIDTH, pad to the right
14183 with spaces. FIELD_WIDTH <= 0 means don't pad.
14184
14185 PRECISION is the maximum number of characters to output from
14186 STRING. PRECISION <= 0 means don't truncate the string.
14187
14188 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
14189 properties to the string.
14190
14191 PROPS are the properties to add to the string.
14192 The mode_line_string_face face property is always added to the string.
14193 */
14194
14195 static int store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
14196 char *string;
14197 Lisp_Object lisp_string;
14198 int copy_string;
14199 int field_width;
14200 int precision;
14201 Lisp_Object props;
14202 {
14203 int len;
14204 int n = 0;
14205
14206 if (string != NULL)
14207 {
14208 len = strlen (string);
14209 if (precision > 0 && len > precision)
14210 len = precision;
14211 lisp_string = make_string (string, len);
14212 if (NILP (props))
14213 props = mode_line_string_face_prop;
14214 else if (!NILP (mode_line_string_face))
14215 {
14216 Lisp_Object face = Fplist_get (props, Qface);
14217 props = Fcopy_sequence (props);
14218 if (NILP (face))
14219 face = mode_line_string_face;
14220 else
14221 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
14222 props = Fplist_put (props, Qface, face);
14223 }
14224 Fadd_text_properties (make_number (0), make_number (len),
14225 props, lisp_string);
14226 }
14227 else
14228 {
14229 len = XFASTINT (Flength (lisp_string));
14230 if (precision > 0 && len > precision)
14231 {
14232 len = precision;
14233 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
14234 precision = -1;
14235 }
14236 if (!NILP (mode_line_string_face))
14237 {
14238 Lisp_Object face;
14239 if (NILP (props))
14240 props = Ftext_properties_at (make_number (0), lisp_string);
14241 face = Fplist_get (props, Qface);
14242 if (NILP (face))
14243 face = mode_line_string_face;
14244 else
14245 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
14246 props = Fcons (Qface, Fcons (face, Qnil));
14247 if (copy_string)
14248 lisp_string = Fcopy_sequence (lisp_string);
14249 }
14250 if (!NILP (props))
14251 Fadd_text_properties (make_number (0), make_number (len),
14252 props, lisp_string);
14253 }
14254
14255 if (len > 0)
14256 {
14257 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
14258 n += len;
14259 }
14260
14261 if (field_width > len)
14262 {
14263 field_width -= len;
14264 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
14265 if (!NILP (props))
14266 Fadd_text_properties (make_number (0), make_number (field_width),
14267 props, lisp_string);
14268 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
14269 n += field_width;
14270 }
14271
14272 return n;
14273 }
14274
14275
14276 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
14277 0, 3, 0,
14278 doc: /* Return the mode-line of selected window as a string.
14279 First optional arg FORMAT specifies a different format string (see
14280 `mode-line-format' for details) to use. If FORMAT is t, return
14281 the buffer's header-line. Second optional arg WINDOW specifies a
14282 different window to use as the context for the formatting.
14283 If third optional arg NO-PROPS is non-nil, string is not propertized. */)
14284 (format, window, no_props)
14285 Lisp_Object format, window, no_props;
14286 {
14287 struct it it;
14288 int len;
14289 struct window *w;
14290 struct buffer *old_buffer = NULL;
14291 enum face_id face_id = DEFAULT_FACE_ID;
14292
14293 if (NILP (window))
14294 window = selected_window;
14295 CHECK_WINDOW (window);
14296 w = XWINDOW (window);
14297 CHECK_BUFFER (w->buffer);
14298
14299 if (XBUFFER (w->buffer) != current_buffer)
14300 {
14301 old_buffer = current_buffer;
14302 set_buffer_internal_1 (XBUFFER (w->buffer));
14303 }
14304
14305 if (NILP (format) || EQ (format, Qt))
14306 {
14307 face_id = NILP (format)
14308 ? CURRENT_MODE_LINE_FACE_ID (w) :
14309 HEADER_LINE_FACE_ID;
14310 format = NILP (format)
14311 ? current_buffer->mode_line_format
14312 : current_buffer->header_line_format;
14313 }
14314
14315 init_iterator (&it, w, -1, -1, NULL, face_id);
14316
14317 if (NILP (no_props))
14318 {
14319 mode_line_string_face =
14320 (face_id == MODE_LINE_FACE_ID ? Qmode_line :
14321 face_id == MODE_LINE_INACTIVE_FACE_ID ? Qmode_line_inactive :
14322 face_id == HEADER_LINE_FACE_ID ? Qheader_line : Qnil);
14323
14324 mode_line_string_face_prop =
14325 NILP (mode_line_string_face) ? Qnil :
14326 Fcons (Qface, Fcons (mode_line_string_face, Qnil));
14327
14328 /* We need a dummy last element in mode_line_string_list to
14329 indicate we are building the propertized mode-line string.
14330 Using mode_line_string_face_prop here GC protects it. */
14331 mode_line_string_list =
14332 Fcons (mode_line_string_face_prop, Qnil);
14333 frame_title_ptr = NULL;
14334 }
14335 else
14336 {
14337 mode_line_string_face_prop = Qnil;
14338 mode_line_string_list = Qnil;
14339 frame_title_ptr = frame_title_buf;
14340 }
14341
14342 push_frame_kboard (it.f);
14343 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
14344 pop_frame_kboard ();
14345
14346 if (old_buffer)
14347 set_buffer_internal_1 (old_buffer);
14348
14349 if (NILP (no_props))
14350 {
14351 Lisp_Object str;
14352 mode_line_string_list = Fnreverse (mode_line_string_list);
14353 str = Fmapconcat (intern ("identity"), XCDR (mode_line_string_list),
14354 make_string ("", 0));
14355 mode_line_string_face_prop = Qnil;
14356 mode_line_string_list = Qnil;
14357 return str;
14358 }
14359
14360 len = frame_title_ptr - frame_title_buf;
14361 if (len > 0 && frame_title_ptr[-1] == '-')
14362 {
14363 /* Mode lines typically ends with numerous dashes; reduce to two dashes. */
14364 while (frame_title_ptr > frame_title_buf && *--frame_title_ptr == '-')
14365 ;
14366 frame_title_ptr += 3; /* restore last non-dash + two dashes */
14367 if (len > frame_title_ptr - frame_title_buf)
14368 len = frame_title_ptr - frame_title_buf;
14369 }
14370
14371 frame_title_ptr = NULL;
14372 return make_string (frame_title_buf, len);
14373 }
14374
14375 /* Write a null-terminated, right justified decimal representation of
14376 the positive integer D to BUF using a minimal field width WIDTH. */
14377
14378 static void
14379 pint2str (buf, width, d)
14380 register char *buf;
14381 register int width;
14382 register int d;
14383 {
14384 register char *p = buf;
14385
14386 if (d <= 0)
14387 *p++ = '0';
14388 else
14389 {
14390 while (d > 0)
14391 {
14392 *p++ = d % 10 + '0';
14393 d /= 10;
14394 }
14395 }
14396
14397 for (width -= (int) (p - buf); width > 0; --width)
14398 *p++ = ' ';
14399 *p-- = '\0';
14400 while (p > buf)
14401 {
14402 d = *buf;
14403 *buf++ = *p;
14404 *p-- = d;
14405 }
14406 }
14407
14408 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
14409 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
14410 type of CODING_SYSTEM. Return updated pointer into BUF. */
14411
14412 static unsigned char invalid_eol_type[] = "(*invalid*)";
14413
14414 static char *
14415 decode_mode_spec_coding (coding_system, buf, eol_flag)
14416 Lisp_Object coding_system;
14417 register char *buf;
14418 int eol_flag;
14419 {
14420 Lisp_Object val;
14421 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
14422 const unsigned char *eol_str;
14423 int eol_str_len;
14424 /* The EOL conversion we are using. */
14425 Lisp_Object eoltype;
14426
14427 val = Fget (coding_system, Qcoding_system);
14428 eoltype = Qnil;
14429
14430 if (!VECTORP (val)) /* Not yet decided. */
14431 {
14432 if (multibyte)
14433 *buf++ = '-';
14434 if (eol_flag)
14435 eoltype = eol_mnemonic_undecided;
14436 /* Don't mention EOL conversion if it isn't decided. */
14437 }
14438 else
14439 {
14440 Lisp_Object eolvalue;
14441
14442 eolvalue = Fget (coding_system, Qeol_type);
14443
14444 if (multibyte)
14445 *buf++ = XFASTINT (AREF (val, 1));
14446
14447 if (eol_flag)
14448 {
14449 /* The EOL conversion that is normal on this system. */
14450
14451 if (NILP (eolvalue)) /* Not yet decided. */
14452 eoltype = eol_mnemonic_undecided;
14453 else if (VECTORP (eolvalue)) /* Not yet decided. */
14454 eoltype = eol_mnemonic_undecided;
14455 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
14456 eoltype = (XFASTINT (eolvalue) == 0
14457 ? eol_mnemonic_unix
14458 : (XFASTINT (eolvalue) == 1
14459 ? eol_mnemonic_dos : eol_mnemonic_mac));
14460 }
14461 }
14462
14463 if (eol_flag)
14464 {
14465 /* Mention the EOL conversion if it is not the usual one. */
14466 if (STRINGP (eoltype))
14467 {
14468 eol_str = SDATA (eoltype);
14469 eol_str_len = SBYTES (eoltype);
14470 }
14471 else if (INTEGERP (eoltype)
14472 && CHAR_VALID_P (XINT (eoltype), 0))
14473 {
14474 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
14475 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
14476 eol_str = tmp;
14477 }
14478 else
14479 {
14480 eol_str = invalid_eol_type;
14481 eol_str_len = sizeof (invalid_eol_type) - 1;
14482 }
14483 bcopy (eol_str, buf, eol_str_len);
14484 buf += eol_str_len;
14485 }
14486
14487 return buf;
14488 }
14489
14490 /* Return a string for the output of a mode line %-spec for window W,
14491 generated by character C. PRECISION >= 0 means don't return a
14492 string longer than that value. FIELD_WIDTH > 0 means pad the
14493 string returned with spaces to that value. Return 1 in *MULTIBYTE
14494 if the result is multibyte text. */
14495
14496 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
14497
14498 static char *
14499 decode_mode_spec (w, c, field_width, precision, multibyte)
14500 struct window *w;
14501 register int c;
14502 int field_width, precision;
14503 int *multibyte;
14504 {
14505 Lisp_Object obj;
14506 struct frame *f = XFRAME (WINDOW_FRAME (w));
14507 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
14508 struct buffer *b = XBUFFER (w->buffer);
14509
14510 obj = Qnil;
14511 *multibyte = 0;
14512
14513 switch (c)
14514 {
14515 case '*':
14516 if (!NILP (b->read_only))
14517 return "%";
14518 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
14519 return "*";
14520 return "-";
14521
14522 case '+':
14523 /* This differs from %* only for a modified read-only buffer. */
14524 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
14525 return "*";
14526 if (!NILP (b->read_only))
14527 return "%";
14528 return "-";
14529
14530 case '&':
14531 /* This differs from %* in ignoring read-only-ness. */
14532 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
14533 return "*";
14534 return "-";
14535
14536 case '%':
14537 return "%";
14538
14539 case '[':
14540 {
14541 int i;
14542 char *p;
14543
14544 if (command_loop_level > 5)
14545 return "[[[... ";
14546 p = decode_mode_spec_buf;
14547 for (i = 0; i < command_loop_level; i++)
14548 *p++ = '[';
14549 *p = 0;
14550 return decode_mode_spec_buf;
14551 }
14552
14553 case ']':
14554 {
14555 int i;
14556 char *p;
14557
14558 if (command_loop_level > 5)
14559 return " ...]]]";
14560 p = decode_mode_spec_buf;
14561 for (i = 0; i < command_loop_level; i++)
14562 *p++ = ']';
14563 *p = 0;
14564 return decode_mode_spec_buf;
14565 }
14566
14567 case '-':
14568 {
14569 register int i;
14570
14571 /* Let lots_of_dashes be a string of infinite length. */
14572 if (!NILP (mode_line_string_list))
14573 return "--";
14574 if (field_width <= 0
14575 || field_width > sizeof (lots_of_dashes))
14576 {
14577 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
14578 decode_mode_spec_buf[i] = '-';
14579 decode_mode_spec_buf[i] = '\0';
14580 return decode_mode_spec_buf;
14581 }
14582 else
14583 return lots_of_dashes;
14584 }
14585
14586 case 'b':
14587 obj = b->name;
14588 break;
14589
14590 case 'c':
14591 {
14592 int col = (int) current_column (); /* iftc */
14593 w->column_number_displayed = make_number (col);
14594 pint2str (decode_mode_spec_buf, field_width, col);
14595 return decode_mode_spec_buf;
14596 }
14597
14598 case 'F':
14599 /* %F displays the frame name. */
14600 if (!NILP (f->title))
14601 return (char *) SDATA (f->title);
14602 if (f->explicit_name || ! FRAME_WINDOW_P (f))
14603 return (char *) SDATA (f->name);
14604 return "Emacs";
14605
14606 case 'f':
14607 obj = b->filename;
14608 break;
14609
14610 case 'l':
14611 {
14612 int startpos = XMARKER (w->start)->charpos;
14613 int startpos_byte = marker_byte_position (w->start);
14614 int line, linepos, linepos_byte, topline;
14615 int nlines, junk;
14616 int height = XFASTINT (w->height);
14617
14618 /* If we decided that this buffer isn't suitable for line numbers,
14619 don't forget that too fast. */
14620 if (EQ (w->base_line_pos, w->buffer))
14621 goto no_value;
14622 /* But do forget it, if the window shows a different buffer now. */
14623 else if (BUFFERP (w->base_line_pos))
14624 w->base_line_pos = Qnil;
14625
14626 /* If the buffer is very big, don't waste time. */
14627 if (INTEGERP (Vline_number_display_limit)
14628 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
14629 {
14630 w->base_line_pos = Qnil;
14631 w->base_line_number = Qnil;
14632 goto no_value;
14633 }
14634
14635 if (!NILP (w->base_line_number)
14636 && !NILP (w->base_line_pos)
14637 && XFASTINT (w->base_line_pos) <= startpos)
14638 {
14639 line = XFASTINT (w->base_line_number);
14640 linepos = XFASTINT (w->base_line_pos);
14641 linepos_byte = buf_charpos_to_bytepos (b, linepos);
14642 }
14643 else
14644 {
14645 line = 1;
14646 linepos = BUF_BEGV (b);
14647 linepos_byte = BUF_BEGV_BYTE (b);
14648 }
14649
14650 /* Count lines from base line to window start position. */
14651 nlines = display_count_lines (linepos, linepos_byte,
14652 startpos_byte,
14653 startpos, &junk);
14654
14655 topline = nlines + line;
14656
14657 /* Determine a new base line, if the old one is too close
14658 or too far away, or if we did not have one.
14659 "Too close" means it's plausible a scroll-down would
14660 go back past it. */
14661 if (startpos == BUF_BEGV (b))
14662 {
14663 w->base_line_number = make_number (topline);
14664 w->base_line_pos = make_number (BUF_BEGV (b));
14665 }
14666 else if (nlines < height + 25 || nlines > height * 3 + 50
14667 || linepos == BUF_BEGV (b))
14668 {
14669 int limit = BUF_BEGV (b);
14670 int limit_byte = BUF_BEGV_BYTE (b);
14671 int position;
14672 int distance = (height * 2 + 30) * line_number_display_limit_width;
14673
14674 if (startpos - distance > limit)
14675 {
14676 limit = startpos - distance;
14677 limit_byte = CHAR_TO_BYTE (limit);
14678 }
14679
14680 nlines = display_count_lines (startpos, startpos_byte,
14681 limit_byte,
14682 - (height * 2 + 30),
14683 &position);
14684 /* If we couldn't find the lines we wanted within
14685 line_number_display_limit_width chars per line,
14686 give up on line numbers for this window. */
14687 if (position == limit_byte && limit == startpos - distance)
14688 {
14689 w->base_line_pos = w->buffer;
14690 w->base_line_number = Qnil;
14691 goto no_value;
14692 }
14693
14694 w->base_line_number = make_number (topline - nlines);
14695 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
14696 }
14697
14698 /* Now count lines from the start pos to point. */
14699 nlines = display_count_lines (startpos, startpos_byte,
14700 PT_BYTE, PT, &junk);
14701
14702 /* Record that we did display the line number. */
14703 line_number_displayed = 1;
14704
14705 /* Make the string to show. */
14706 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
14707 return decode_mode_spec_buf;
14708 no_value:
14709 {
14710 char* p = decode_mode_spec_buf;
14711 int pad = field_width - 2;
14712 while (pad-- > 0)
14713 *p++ = ' ';
14714 *p++ = '?';
14715 *p++ = '?';
14716 *p = '\0';
14717 return decode_mode_spec_buf;
14718 }
14719 }
14720 break;
14721
14722 case 'm':
14723 obj = b->mode_name;
14724 break;
14725
14726 case 'n':
14727 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
14728 return " Narrow";
14729 break;
14730
14731 case 'p':
14732 {
14733 int pos = marker_position (w->start);
14734 int total = BUF_ZV (b) - BUF_BEGV (b);
14735
14736 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
14737 {
14738 if (pos <= BUF_BEGV (b))
14739 return "All";
14740 else
14741 return "Bottom";
14742 }
14743 else if (pos <= BUF_BEGV (b))
14744 return "Top";
14745 else
14746 {
14747 if (total > 1000000)
14748 /* Do it differently for a large value, to avoid overflow. */
14749 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
14750 else
14751 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
14752 /* We can't normally display a 3-digit number,
14753 so get us a 2-digit number that is close. */
14754 if (total == 100)
14755 total = 99;
14756 sprintf (decode_mode_spec_buf, "%2d%%", total);
14757 return decode_mode_spec_buf;
14758 }
14759 }
14760
14761 /* Display percentage of size above the bottom of the screen. */
14762 case 'P':
14763 {
14764 int toppos = marker_position (w->start);
14765 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
14766 int total = BUF_ZV (b) - BUF_BEGV (b);
14767
14768 if (botpos >= BUF_ZV (b))
14769 {
14770 if (toppos <= BUF_BEGV (b))
14771 return "All";
14772 else
14773 return "Bottom";
14774 }
14775 else
14776 {
14777 if (total > 1000000)
14778 /* Do it differently for a large value, to avoid overflow. */
14779 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
14780 else
14781 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
14782 /* We can't normally display a 3-digit number,
14783 so get us a 2-digit number that is close. */
14784 if (total == 100)
14785 total = 99;
14786 if (toppos <= BUF_BEGV (b))
14787 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
14788 else
14789 sprintf (decode_mode_spec_buf, "%2d%%", total);
14790 return decode_mode_spec_buf;
14791 }
14792 }
14793
14794 case 's':
14795 /* status of process */
14796 obj = Fget_buffer_process (w->buffer);
14797 if (NILP (obj))
14798 return "no process";
14799 #ifdef subprocesses
14800 obj = Fsymbol_name (Fprocess_status (obj));
14801 #endif
14802 break;
14803
14804 case 't': /* indicate TEXT or BINARY */
14805 #ifdef MODE_LINE_BINARY_TEXT
14806 return MODE_LINE_BINARY_TEXT (b);
14807 #else
14808 return "T";
14809 #endif
14810
14811 case 'z':
14812 /* coding-system (not including end-of-line format) */
14813 case 'Z':
14814 /* coding-system (including end-of-line type) */
14815 {
14816 int eol_flag = (c == 'Z');
14817 char *p = decode_mode_spec_buf;
14818
14819 if (! FRAME_WINDOW_P (f))
14820 {
14821 /* No need to mention EOL here--the terminal never needs
14822 to do EOL conversion. */
14823 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
14824 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
14825 }
14826 p = decode_mode_spec_coding (b->buffer_file_coding_system,
14827 p, eol_flag);
14828
14829 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
14830 #ifdef subprocesses
14831 obj = Fget_buffer_process (Fcurrent_buffer ());
14832 if (PROCESSP (obj))
14833 {
14834 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
14835 p, eol_flag);
14836 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
14837 p, eol_flag);
14838 }
14839 #endif /* subprocesses */
14840 #endif /* 0 */
14841 *p = 0;
14842 return decode_mode_spec_buf;
14843 }
14844 }
14845
14846 if (STRINGP (obj))
14847 {
14848 *multibyte = STRING_MULTIBYTE (obj);
14849 return (char *) SDATA (obj);
14850 }
14851 else
14852 return "";
14853 }
14854
14855
14856 /* Count up to COUNT lines starting from START / START_BYTE.
14857 But don't go beyond LIMIT_BYTE.
14858 Return the number of lines thus found (always nonnegative).
14859
14860 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
14861
14862 static int
14863 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
14864 int start, start_byte, limit_byte, count;
14865 int *byte_pos_ptr;
14866 {
14867 register unsigned char *cursor;
14868 unsigned char *base;
14869
14870 register int ceiling;
14871 register unsigned char *ceiling_addr;
14872 int orig_count = count;
14873
14874 /* If we are not in selective display mode,
14875 check only for newlines. */
14876 int selective_display = (!NILP (current_buffer->selective_display)
14877 && !INTEGERP (current_buffer->selective_display));
14878
14879 if (count > 0)
14880 {
14881 while (start_byte < limit_byte)
14882 {
14883 ceiling = BUFFER_CEILING_OF (start_byte);
14884 ceiling = min (limit_byte - 1, ceiling);
14885 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
14886 base = (cursor = BYTE_POS_ADDR (start_byte));
14887 while (1)
14888 {
14889 if (selective_display)
14890 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
14891 ;
14892 else
14893 while (*cursor != '\n' && ++cursor != ceiling_addr)
14894 ;
14895
14896 if (cursor != ceiling_addr)
14897 {
14898 if (--count == 0)
14899 {
14900 start_byte += cursor - base + 1;
14901 *byte_pos_ptr = start_byte;
14902 return orig_count;
14903 }
14904 else
14905 if (++cursor == ceiling_addr)
14906 break;
14907 }
14908 else
14909 break;
14910 }
14911 start_byte += cursor - base;
14912 }
14913 }
14914 else
14915 {
14916 while (start_byte > limit_byte)
14917 {
14918 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
14919 ceiling = max (limit_byte, ceiling);
14920 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
14921 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
14922 while (1)
14923 {
14924 if (selective_display)
14925 while (--cursor != ceiling_addr
14926 && *cursor != '\n' && *cursor != 015)
14927 ;
14928 else
14929 while (--cursor != ceiling_addr && *cursor != '\n')
14930 ;
14931
14932 if (cursor != ceiling_addr)
14933 {
14934 if (++count == 0)
14935 {
14936 start_byte += cursor - base + 1;
14937 *byte_pos_ptr = start_byte;
14938 /* When scanning backwards, we should
14939 not count the newline posterior to which we stop. */
14940 return - orig_count - 1;
14941 }
14942 }
14943 else
14944 break;
14945 }
14946 /* Here we add 1 to compensate for the last decrement
14947 of CURSOR, which took it past the valid range. */
14948 start_byte += cursor - base + 1;
14949 }
14950 }
14951
14952 *byte_pos_ptr = limit_byte;
14953
14954 if (count < 0)
14955 return - orig_count + count;
14956 return orig_count - count;
14957
14958 }
14959
14960
14961 \f
14962 /***********************************************************************
14963 Displaying strings
14964 ***********************************************************************/
14965
14966 /* Display a NUL-terminated string, starting with index START.
14967
14968 If STRING is non-null, display that C string. Otherwise, the Lisp
14969 string LISP_STRING is displayed.
14970
14971 If FACE_STRING is not nil, FACE_STRING_POS is a position in
14972 FACE_STRING. Display STRING or LISP_STRING with the face at
14973 FACE_STRING_POS in FACE_STRING:
14974
14975 Display the string in the environment given by IT, but use the
14976 standard display table, temporarily.
14977
14978 FIELD_WIDTH is the minimum number of output glyphs to produce.
14979 If STRING has fewer characters than FIELD_WIDTH, pad to the right
14980 with spaces. If STRING has more characters, more than FIELD_WIDTH
14981 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
14982
14983 PRECISION is the maximum number of characters to output from
14984 STRING. PRECISION < 0 means don't truncate the string.
14985
14986 This is roughly equivalent to printf format specifiers:
14987
14988 FIELD_WIDTH PRECISION PRINTF
14989 ----------------------------------------
14990 -1 -1 %s
14991 -1 10 %.10s
14992 10 -1 %10s
14993 20 10 %20.10s
14994
14995 MULTIBYTE zero means do not display multibyte chars, > 0 means do
14996 display them, and < 0 means obey the current buffer's value of
14997 enable_multibyte_characters.
14998
14999 Value is the number of glyphs produced. */
15000
15001 static int
15002 display_string (string, lisp_string, face_string, face_string_pos,
15003 start, it, field_width, precision, max_x, multibyte)
15004 unsigned char *string;
15005 Lisp_Object lisp_string;
15006 Lisp_Object face_string;
15007 int face_string_pos;
15008 int start;
15009 struct it *it;
15010 int field_width, precision, max_x;
15011 int multibyte;
15012 {
15013 int hpos_at_start = it->hpos;
15014 int saved_face_id = it->face_id;
15015 struct glyph_row *row = it->glyph_row;
15016
15017 /* Initialize the iterator IT for iteration over STRING beginning
15018 with index START. */
15019 reseat_to_string (it, string, lisp_string, start,
15020 precision, field_width, multibyte);
15021
15022 /* If displaying STRING, set up the face of the iterator
15023 from LISP_STRING, if that's given. */
15024 if (STRINGP (face_string))
15025 {
15026 int endptr;
15027 struct face *face;
15028
15029 it->face_id
15030 = face_at_string_position (it->w, face_string, face_string_pos,
15031 0, it->region_beg_charpos,
15032 it->region_end_charpos,
15033 &endptr, it->base_face_id, 0);
15034 face = FACE_FROM_ID (it->f, it->face_id);
15035 it->face_box_p = face->box != FACE_NO_BOX;
15036 }
15037
15038 /* Set max_x to the maximum allowed X position. Don't let it go
15039 beyond the right edge of the window. */
15040 if (max_x <= 0)
15041 max_x = it->last_visible_x;
15042 else
15043 max_x = min (max_x, it->last_visible_x);
15044
15045 /* Skip over display elements that are not visible. because IT->w is
15046 hscrolled. */
15047 if (it->current_x < it->first_visible_x)
15048 move_it_in_display_line_to (it, 100000, it->first_visible_x,
15049 MOVE_TO_POS | MOVE_TO_X);
15050
15051 row->ascent = it->max_ascent;
15052 row->height = it->max_ascent + it->max_descent;
15053 row->phys_ascent = it->max_phys_ascent;
15054 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15055
15056 /* This condition is for the case that we are called with current_x
15057 past last_visible_x. */
15058 while (it->current_x < max_x)
15059 {
15060 int x_before, x, n_glyphs_before, i, nglyphs;
15061
15062 /* Get the next display element. */
15063 if (!get_next_display_element (it))
15064 break;
15065
15066 /* Produce glyphs. */
15067 x_before = it->current_x;
15068 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
15069 PRODUCE_GLYPHS (it);
15070
15071 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
15072 i = 0;
15073 x = x_before;
15074 while (i < nglyphs)
15075 {
15076 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
15077
15078 if (!it->truncate_lines_p
15079 && x + glyph->pixel_width > max_x)
15080 {
15081 /* End of continued line or max_x reached. */
15082 if (CHAR_GLYPH_PADDING_P (*glyph))
15083 {
15084 /* A wide character is unbreakable. */
15085 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
15086 it->current_x = x_before;
15087 }
15088 else
15089 {
15090 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
15091 it->current_x = x;
15092 }
15093 break;
15094 }
15095 else if (x + glyph->pixel_width > it->first_visible_x)
15096 {
15097 /* Glyph is at least partially visible. */
15098 ++it->hpos;
15099 if (x < it->first_visible_x)
15100 it->glyph_row->x = x - it->first_visible_x;
15101 }
15102 else
15103 {
15104 /* Glyph is off the left margin of the display area.
15105 Should not happen. */
15106 abort ();
15107 }
15108
15109 row->ascent = max (row->ascent, it->max_ascent);
15110 row->height = max (row->height, it->max_ascent + it->max_descent);
15111 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15112 row->phys_height = max (row->phys_height,
15113 it->max_phys_ascent + it->max_phys_descent);
15114 x += glyph->pixel_width;
15115 ++i;
15116 }
15117
15118 /* Stop if max_x reached. */
15119 if (i < nglyphs)
15120 break;
15121
15122 /* Stop at line ends. */
15123 if (ITERATOR_AT_END_OF_LINE_P (it))
15124 {
15125 it->continuation_lines_width = 0;
15126 break;
15127 }
15128
15129 set_iterator_to_next (it, 1);
15130
15131 /* Stop if truncating at the right edge. */
15132 if (it->truncate_lines_p
15133 && it->current_x >= it->last_visible_x)
15134 {
15135 /* Add truncation mark, but don't do it if the line is
15136 truncated at a padding space. */
15137 if (IT_CHARPOS (*it) < it->string_nchars)
15138 {
15139 if (!FRAME_WINDOW_P (it->f))
15140 {
15141 int i, n;
15142
15143 if (it->current_x > it->last_visible_x)
15144 {
15145 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
15146 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
15147 break;
15148 for (n = row->used[TEXT_AREA]; i < n; ++i)
15149 {
15150 row->used[TEXT_AREA] = i;
15151 produce_special_glyphs (it, IT_TRUNCATION);
15152 }
15153 }
15154 produce_special_glyphs (it, IT_TRUNCATION);
15155 }
15156 it->glyph_row->truncated_on_right_p = 1;
15157 }
15158 break;
15159 }
15160 }
15161
15162 /* Maybe insert a truncation at the left. */
15163 if (it->first_visible_x
15164 && IT_CHARPOS (*it) > 0)
15165 {
15166 if (!FRAME_WINDOW_P (it->f))
15167 insert_left_trunc_glyphs (it);
15168 it->glyph_row->truncated_on_left_p = 1;
15169 }
15170
15171 it->face_id = saved_face_id;
15172
15173 /* Value is number of columns displayed. */
15174 return it->hpos - hpos_at_start;
15175 }
15176
15177
15178 \f
15179 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
15180 appears as an element of LIST or as the car of an element of LIST.
15181 If PROPVAL is a list, compare each element against LIST in that
15182 way, and return 1/2 if any element of PROPVAL is found in LIST.
15183 Otherwise return 0. This function cannot quit.
15184 The return value is 2 if the text is invisible but with an ellipsis
15185 and 1 if it's invisible and without an ellipsis. */
15186
15187 int
15188 invisible_p (propval, list)
15189 register Lisp_Object propval;
15190 Lisp_Object list;
15191 {
15192 register Lisp_Object tail, proptail;
15193
15194 for (tail = list; CONSP (tail); tail = XCDR (tail))
15195 {
15196 register Lisp_Object tem;
15197 tem = XCAR (tail);
15198 if (EQ (propval, tem))
15199 return 1;
15200 if (CONSP (tem) && EQ (propval, XCAR (tem)))
15201 return NILP (XCDR (tem)) ? 1 : 2;
15202 }
15203
15204 if (CONSP (propval))
15205 {
15206 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
15207 {
15208 Lisp_Object propelt;
15209 propelt = XCAR (proptail);
15210 for (tail = list; CONSP (tail); tail = XCDR (tail))
15211 {
15212 register Lisp_Object tem;
15213 tem = XCAR (tail);
15214 if (EQ (propelt, tem))
15215 return 1;
15216 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
15217 return NILP (XCDR (tem)) ? 1 : 2;
15218 }
15219 }
15220 }
15221
15222 return 0;
15223 }
15224
15225 \f
15226 /***********************************************************************
15227 Cursor types
15228 ***********************************************************************/
15229
15230 /* Value is the internal representation of the specified cursor type
15231 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
15232 of the bar cursor. */
15233
15234 enum text_cursor_kinds
15235 get_specified_cursor_type (arg, width)
15236 Lisp_Object arg;
15237 int *width;
15238 {
15239 enum text_cursor_kinds type;
15240
15241 if (NILP (arg))
15242 return NO_CURSOR;
15243
15244 if (EQ (arg, Qbox))
15245 return FILLED_BOX_CURSOR;
15246
15247 if (EQ (arg, Qhollow))
15248 return HOLLOW_BOX_CURSOR;
15249
15250 if (EQ (arg, Qbar))
15251 {
15252 *width = 2;
15253 return BAR_CURSOR;
15254 }
15255
15256 if (CONSP (arg)
15257 && EQ (XCAR (arg), Qbar)
15258 && INTEGERP (XCDR (arg))
15259 && XINT (XCDR (arg)) >= 0)
15260 {
15261 *width = XINT (XCDR (arg));
15262 return BAR_CURSOR;
15263 }
15264
15265 if (EQ (arg, Qhbar))
15266 {
15267 *width = 2;
15268 return HBAR_CURSOR;
15269 }
15270
15271 if (CONSP (arg)
15272 && EQ (XCAR (arg), Qhbar)
15273 && INTEGERP (XCDR (arg))
15274 && XINT (XCDR (arg)) >= 0)
15275 {
15276 *width = XINT (XCDR (arg));
15277 return HBAR_CURSOR;
15278 }
15279
15280 /* Treat anything unknown as "hollow box cursor".
15281 It was bad to signal an error; people have trouble fixing
15282 .Xdefaults with Emacs, when it has something bad in it. */
15283 type = HOLLOW_BOX_CURSOR;
15284
15285 return type;
15286 }
15287
15288 /* Set the default cursor types for specified frame. */
15289 void
15290 set_frame_cursor_types (f, arg)
15291 struct frame *f;
15292 Lisp_Object arg;
15293 {
15294 int width;
15295 Lisp_Object tem;
15296
15297 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
15298 FRAME_CURSOR_WIDTH (f) = width;
15299
15300 /* By default, set up the blink-off state depending on the on-state. */
15301
15302 tem = Fassoc (arg, Vblink_cursor_alist);
15303 if (!NILP (tem))
15304 {
15305 FRAME_BLINK_OFF_CURSOR (f)
15306 = get_specified_cursor_type (XCDR (tem), &width);
15307 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
15308 }
15309 else
15310 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
15311 }
15312
15313
15314 /* Return the cursor we want to be displayed in window W. Return
15315 width of bar/hbar cursor through WIDTH arg. Return with
15316 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
15317 (i.e. if the `system caret' should track this cursor).
15318
15319 In a mini-buffer window, we want the cursor only to appear if we
15320 are reading input from this window. For the selected window, we
15321 want the cursor type given by the frame parameter or buffer local
15322 setting of cursor-type. If explicitly marked off, draw no cursor.
15323 In all other cases, we want a hollow box cursor. */
15324
15325 enum text_cursor_kinds
15326 get_window_cursor_type (w, width, active_cursor)
15327 struct window *w;
15328 int *width;
15329 int *active_cursor;
15330 {
15331 struct frame *f = XFRAME (w->frame);
15332 struct buffer *b = XBUFFER (w->buffer);
15333 int cursor_type = DEFAULT_CURSOR;
15334 Lisp_Object alt_cursor;
15335 int non_selected = 0;
15336
15337 *active_cursor = 1;
15338
15339 /* Echo area */
15340 if (cursor_in_echo_area
15341 && FRAME_HAS_MINIBUF_P (f)
15342 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
15343 {
15344 if (w == XWINDOW (echo_area_window))
15345 {
15346 *width = FRAME_CURSOR_WIDTH (f);
15347 return FRAME_DESIRED_CURSOR (f);
15348 }
15349
15350 *active_cursor = 0;
15351 non_selected = 1;
15352 }
15353
15354 /* Nonselected window or nonselected frame. */
15355 else if (w != XWINDOW (f->selected_window)
15356 #ifdef HAVE_WINDOW_SYSTEM
15357 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
15358 #endif
15359 )
15360 {
15361 *active_cursor = 0;
15362
15363 if (MINI_WINDOW_P (w) && minibuf_level == 0)
15364 return NO_CURSOR;
15365
15366 non_selected = 1;
15367 }
15368
15369 /* Never display a cursor in a window in which cursor-type is nil. */
15370 if (NILP (b->cursor_type))
15371 return NO_CURSOR;
15372
15373 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
15374 if (non_selected)
15375 {
15376 alt_cursor = Fbuffer_local_value (Qcursor_in_non_selected_windows, w->buffer);
15377 return get_specified_cursor_type (alt_cursor, width);
15378 }
15379
15380 /* Get the normal cursor type for this window. */
15381 if (EQ (b->cursor_type, Qt))
15382 {
15383 cursor_type = FRAME_DESIRED_CURSOR (f);
15384 *width = FRAME_CURSOR_WIDTH (f);
15385 }
15386 else
15387 cursor_type = get_specified_cursor_type (b->cursor_type, width);
15388
15389 /* Use normal cursor if not blinked off. */
15390 if (!w->cursor_off_p)
15391 return cursor_type;
15392
15393 /* Cursor is blinked off, so determine how to "toggle" it. */
15394
15395 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
15396 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
15397 return get_specified_cursor_type (XCDR (alt_cursor), width);
15398
15399 /* Then see if frame has specified a specific blink off cursor type. */
15400 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
15401 {
15402 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
15403 return FRAME_BLINK_OFF_CURSOR (f);
15404 }
15405
15406 /* Finally perform built-in cursor blinking:
15407 filled box <-> hollow box
15408 wide [h]bar <-> narrow [h]bar
15409 narrow [h]bar <-> no cursor
15410 other type <-> no cursor */
15411
15412 if (cursor_type == FILLED_BOX_CURSOR)
15413 return HOLLOW_BOX_CURSOR;
15414
15415 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
15416 {
15417 *width = 1;
15418 return cursor_type;
15419 }
15420
15421 return NO_CURSOR;
15422 }
15423
15424 \f
15425 /***********************************************************************
15426 Initialization
15427 ***********************************************************************/
15428
15429 void
15430 syms_of_xdisp ()
15431 {
15432 Vwith_echo_area_save_vector = Qnil;
15433 staticpro (&Vwith_echo_area_save_vector);
15434
15435 Vmessage_stack = Qnil;
15436 staticpro (&Vmessage_stack);
15437
15438 Qinhibit_redisplay = intern ("inhibit-redisplay");
15439 staticpro (&Qinhibit_redisplay);
15440
15441 message_dolog_marker1 = Fmake_marker ();
15442 staticpro (&message_dolog_marker1);
15443 message_dolog_marker2 = Fmake_marker ();
15444 staticpro (&message_dolog_marker2);
15445 message_dolog_marker3 = Fmake_marker ();
15446 staticpro (&message_dolog_marker3);
15447
15448 #if GLYPH_DEBUG
15449 defsubr (&Sdump_frame_glyph_matrix);
15450 defsubr (&Sdump_glyph_matrix);
15451 defsubr (&Sdump_glyph_row);
15452 defsubr (&Sdump_tool_bar_row);
15453 defsubr (&Strace_redisplay);
15454 defsubr (&Strace_to_stderr);
15455 #endif
15456 #ifdef HAVE_WINDOW_SYSTEM
15457 defsubr (&Stool_bar_lines_needed);
15458 #endif
15459 defsubr (&Sformat_mode_line);
15460
15461 staticpro (&Qmenu_bar_update_hook);
15462 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
15463
15464 staticpro (&Qoverriding_terminal_local_map);
15465 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
15466
15467 staticpro (&Qoverriding_local_map);
15468 Qoverriding_local_map = intern ("overriding-local-map");
15469
15470 staticpro (&Qwindow_scroll_functions);
15471 Qwindow_scroll_functions = intern ("window-scroll-functions");
15472
15473 staticpro (&Qredisplay_end_trigger_functions);
15474 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
15475
15476 staticpro (&Qinhibit_point_motion_hooks);
15477 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
15478
15479 QCdata = intern (":data");
15480 staticpro (&QCdata);
15481 Qdisplay = intern ("display");
15482 staticpro (&Qdisplay);
15483 Qspace_width = intern ("space-width");
15484 staticpro (&Qspace_width);
15485 Qraise = intern ("raise");
15486 staticpro (&Qraise);
15487 Qspace = intern ("space");
15488 staticpro (&Qspace);
15489 Qmargin = intern ("margin");
15490 staticpro (&Qmargin);
15491 Qleft_margin = intern ("left-margin");
15492 staticpro (&Qleft_margin);
15493 Qright_margin = intern ("right-margin");
15494 staticpro (&Qright_margin);
15495 Qalign_to = intern ("align-to");
15496 staticpro (&Qalign_to);
15497 QCalign_to = intern (":align-to");
15498 staticpro (&QCalign_to);
15499 Qrelative_width = intern ("relative-width");
15500 staticpro (&Qrelative_width);
15501 QCrelative_width = intern (":relative-width");
15502 staticpro (&QCrelative_width);
15503 QCrelative_height = intern (":relative-height");
15504 staticpro (&QCrelative_height);
15505 QCeval = intern (":eval");
15506 staticpro (&QCeval);
15507 QCpropertize = intern (":propertize");
15508 staticpro (&QCpropertize);
15509 Qwhen = intern ("when");
15510 staticpro (&Qwhen);
15511 QCfile = intern (":file");
15512 staticpro (&QCfile);
15513 Qfontified = intern ("fontified");
15514 staticpro (&Qfontified);
15515 Qfontification_functions = intern ("fontification-functions");
15516 staticpro (&Qfontification_functions);
15517 Qtrailing_whitespace = intern ("trailing-whitespace");
15518 staticpro (&Qtrailing_whitespace);
15519 Qimage = intern ("image");
15520 staticpro (&Qimage);
15521 Qmessage_truncate_lines = intern ("message-truncate-lines");
15522 staticpro (&Qmessage_truncate_lines);
15523 Qcursor_in_non_selected_windows = intern ("cursor-in-non-selected-windows");
15524 staticpro (&Qcursor_in_non_selected_windows);
15525 Qgrow_only = intern ("grow-only");
15526 staticpro (&Qgrow_only);
15527 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
15528 staticpro (&Qinhibit_menubar_update);
15529 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
15530 staticpro (&Qinhibit_eval_during_redisplay);
15531 Qposition = intern ("position");
15532 staticpro (&Qposition);
15533 Qbuffer_position = intern ("buffer-position");
15534 staticpro (&Qbuffer_position);
15535 Qobject = intern ("object");
15536 staticpro (&Qobject);
15537 Qbar = intern ("bar");
15538 staticpro (&Qbar);
15539 Qhbar = intern ("hbar");
15540 staticpro (&Qhbar);
15541 Qbox = intern ("box");
15542 staticpro (&Qbox);
15543 Qhollow = intern ("hollow");
15544 staticpro (&Qhollow);
15545 Qrisky_local_variable = intern ("risky-local-variable");
15546 staticpro (&Qrisky_local_variable);
15547 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
15548 staticpro (&Qinhibit_free_realized_faces);
15549
15550 list_of_error = Fcons (intern ("error"), Qnil);
15551 staticpro (&list_of_error);
15552
15553 last_arrow_position = Qnil;
15554 last_arrow_string = Qnil;
15555 staticpro (&last_arrow_position);
15556 staticpro (&last_arrow_string);
15557
15558 echo_buffer[0] = echo_buffer[1] = Qnil;
15559 staticpro (&echo_buffer[0]);
15560 staticpro (&echo_buffer[1]);
15561
15562 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
15563 staticpro (&echo_area_buffer[0]);
15564 staticpro (&echo_area_buffer[1]);
15565
15566 Vmessages_buffer_name = build_string ("*Messages*");
15567 staticpro (&Vmessages_buffer_name);
15568
15569 mode_line_proptrans_alist = Qnil;
15570 staticpro (&mode_line_proptrans_alist);
15571
15572 mode_line_string_list = Qnil;
15573 staticpro (&mode_line_string_list);
15574
15575 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
15576 doc: /* Non-nil means highlight trailing whitespace.
15577 The face used for trailing whitespace is `trailing-whitespace'. */);
15578 Vshow_trailing_whitespace = Qnil;
15579
15580 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
15581 doc: /* Non-nil means don't actually do any redisplay.
15582 This is used for internal purposes. */);
15583 Vinhibit_redisplay = Qnil;
15584
15585 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
15586 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
15587 Vglobal_mode_string = Qnil;
15588
15589 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
15590 doc: /* Marker for where to display an arrow on top of the buffer text.
15591 This must be the beginning of a line in order to work.
15592 See also `overlay-arrow-string'. */);
15593 Voverlay_arrow_position = Qnil;
15594
15595 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
15596 doc: /* String to display as an arrow. See also `overlay-arrow-position'. */);
15597 Voverlay_arrow_string = Qnil;
15598
15599 DEFVAR_INT ("scroll-step", &scroll_step,
15600 doc: /* *The number of lines to try scrolling a window by when point moves out.
15601 If that fails to bring point back on frame, point is centered instead.
15602 If this is zero, point is always centered after it moves off frame.
15603 If you want scrolling to always be a line at a time, you should set
15604 `scroll-conservatively' to a large value rather than set this to 1. */);
15605
15606 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
15607 doc: /* *Scroll up to this many lines, to bring point back on screen.
15608 A value of zero means to scroll the text to center point vertically
15609 in the window. */);
15610 scroll_conservatively = 0;
15611
15612 DEFVAR_INT ("scroll-margin", &scroll_margin,
15613 doc: /* *Number of lines of margin at the top and bottom of a window.
15614 Recenter the window whenever point gets within this many lines
15615 of the top or bottom of the window. */);
15616 scroll_margin = 0;
15617
15618 #if GLYPH_DEBUG
15619 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
15620 #endif
15621
15622 DEFVAR_BOOL ("truncate-partial-width-windows",
15623 &truncate_partial_width_windows,
15624 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
15625 truncate_partial_width_windows = 1;
15626
15627 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
15628 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
15629 Any other value means to use the appropriate face, `mode-line',
15630 `header-line', or `menu' respectively. */);
15631 mode_line_inverse_video = 1;
15632
15633 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
15634 doc: /* *Maximum buffer size for which line number should be displayed.
15635 If the buffer is bigger than this, the line number does not appear
15636 in the mode line. A value of nil means no limit. */);
15637 Vline_number_display_limit = Qnil;
15638
15639 DEFVAR_INT ("line-number-display-limit-width",
15640 &line_number_display_limit_width,
15641 doc: /* *Maximum line width (in characters) for line number display.
15642 If the average length of the lines near point is bigger than this, then the
15643 line number may be omitted from the mode line. */);
15644 line_number_display_limit_width = 200;
15645
15646 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
15647 doc: /* *Non-nil means highlight region even in nonselected windows. */);
15648 highlight_nonselected_windows = 0;
15649
15650 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
15651 doc: /* Non-nil if more than one frame is visible on this display.
15652 Minibuffer-only frames don't count, but iconified frames do.
15653 This variable is not guaranteed to be accurate except while processing
15654 `frame-title-format' and `icon-title-format'. */);
15655
15656 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
15657 doc: /* Template for displaying the title bar of visible frames.
15658 \(Assuming the window manager supports this feature.)
15659 This variable has the same structure as `mode-line-format' (which see),
15660 and is used only on frames for which no explicit name has been set
15661 \(see `modify-frame-parameters'). */);
15662 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
15663 doc: /* Template for displaying the title bar of an iconified frame.
15664 \(Assuming the window manager supports this feature.)
15665 This variable has the same structure as `mode-line-format' (which see),
15666 and is used only on frames for which no explicit name has been set
15667 \(see `modify-frame-parameters'). */);
15668 Vicon_title_format
15669 = Vframe_title_format
15670 = Fcons (intern ("multiple-frames"),
15671 Fcons (build_string ("%b"),
15672 Fcons (Fcons (empty_string,
15673 Fcons (intern ("invocation-name"),
15674 Fcons (build_string ("@"),
15675 Fcons (intern ("system-name"),
15676 Qnil)))),
15677 Qnil)));
15678
15679 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
15680 doc: /* Maximum number of lines to keep in the message log buffer.
15681 If nil, disable message logging. If t, log messages but don't truncate
15682 the buffer when it becomes large. */);
15683 Vmessage_log_max = make_number (50);
15684
15685 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
15686 doc: /* Functions called before redisplay, if window sizes have changed.
15687 The value should be a list of functions that take one argument.
15688 Just before redisplay, for each frame, if any of its windows have changed
15689 size since the last redisplay, or have been split or deleted,
15690 all the functions in the list are called, with the frame as argument. */);
15691 Vwindow_size_change_functions = Qnil;
15692
15693 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
15694 doc: /* List of Functions to call before redisplaying a window with scrolling.
15695 Each function is called with two arguments, the window
15696 and its new display-start position. Note that the value of `window-end'
15697 is not valid when these functions are called. */);
15698 Vwindow_scroll_functions = Qnil;
15699
15700 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
15701 doc: /* *Non-nil means automatically resize tool-bars.
15702 This increases a tool-bar's height if not all tool-bar items are visible.
15703 It decreases a tool-bar's height when it would display blank lines
15704 otherwise. */);
15705 auto_resize_tool_bars_p = 1;
15706
15707 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
15708 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
15709 auto_raise_tool_bar_buttons_p = 1;
15710
15711 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
15712 doc: /* *Margin around tool-bar buttons in pixels.
15713 If an integer, use that for both horizontal and vertical margins.
15714 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
15715 HORZ specifying the horizontal margin, and VERT specifying the
15716 vertical margin. */);
15717 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
15718
15719 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
15720 doc: /* *Relief thickness of tool-bar buttons. */);
15721 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
15722
15723 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
15724 doc: /* List of functions to call to fontify regions of text.
15725 Each function is called with one argument POS. Functions must
15726 fontify a region starting at POS in the current buffer, and give
15727 fontified regions the property `fontified'. */);
15728 Vfontification_functions = Qnil;
15729 Fmake_variable_buffer_local (Qfontification_functions);
15730
15731 DEFVAR_BOOL ("unibyte-display-via-language-environment",
15732 &unibyte_display_via_language_environment,
15733 doc: /* *Non-nil means display unibyte text according to language environment.
15734 Specifically this means that unibyte non-ASCII characters
15735 are displayed by converting them to the equivalent multibyte characters
15736 according to the current language environment. As a result, they are
15737 displayed according to the current fontset. */);
15738 unibyte_display_via_language_environment = 0;
15739
15740 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
15741 doc: /* *Maximum height for resizing mini-windows.
15742 If a float, it specifies a fraction of the mini-window frame's height.
15743 If an integer, it specifies a number of lines. */);
15744 Vmax_mini_window_height = make_float (0.25);
15745
15746 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
15747 doc: /* *How to resize mini-windows.
15748 A value of nil means don't automatically resize mini-windows.
15749 A value of t means resize them to fit the text displayed in them.
15750 A value of `grow-only', the default, means let mini-windows grow
15751 only, until their display becomes empty, at which point the windows
15752 go back to their normal size. */);
15753 Vresize_mini_windows = Qgrow_only;
15754
15755 DEFVAR_LISP ("cursor-in-non-selected-windows",
15756 &Vcursor_in_non_selected_windows,
15757 doc: /* *Cursor type to display in non-selected windows.
15758 t means to use hollow box cursor. See `cursor-type' for other values. */);
15759 Vcursor_in_non_selected_windows = Qt;
15760
15761 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
15762 doc: /* Alist specifying how to blink the cursor off.
15763 Each element has the form (ON-STATE . OFF-STATE). Whenever the
15764 `cursor-type' frame-parameter or variable equals ON-STATE,
15765 comparing using `equal', Emacs uses OFF-STATE to specify
15766 how to blink it off. */);
15767 Vblink_cursor_alist = Qnil;
15768
15769 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
15770 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
15771 automatic_hscrolling_p = 1;
15772
15773 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
15774 doc: /* *How many columns away from the window edge point is allowed to get
15775 before automatic hscrolling will horizontally scroll the window. */);
15776 hscroll_margin = 5;
15777
15778 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
15779 doc: /* *How many columns to scroll the window when point gets too close to the edge.
15780 When point is less than `automatic-hscroll-margin' columns from the window
15781 edge, automatic hscrolling will scroll the window by the amount of columns
15782 determined by this variable. If its value is a positive integer, scroll that
15783 many columns. If it's a positive floating-point number, it specifies the
15784 fraction of the window's width to scroll. If it's nil or zero, point will be
15785 centered horizontally after the scroll. Any other value, including negative
15786 numbers, are treated as if the value were zero.
15787
15788 Automatic hscrolling always moves point outside the scroll margin, so if
15789 point was more than scroll step columns inside the margin, the window will
15790 scroll more than the value given by the scroll step.
15791
15792 Note that the lower bound for automatic hscrolling specified by `scroll-left'
15793 and `scroll-right' overrides this variable's effect. */);
15794 Vhscroll_step = make_number (0);
15795
15796 DEFVAR_LISP ("image-types", &Vimage_types,
15797 doc: /* List of supported image types.
15798 Each element of the list is a symbol for a supported image type. */);
15799 Vimage_types = Qnil;
15800
15801 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
15802 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
15803 Bind this around calls to `message' to let it take effect. */);
15804 message_truncate_lines = 0;
15805
15806 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
15807 doc: /* Normal hook run for clicks on menu bar, before displaying a submenu.
15808 Can be used to update submenus whose contents should vary. */);
15809 Vmenu_bar_update_hook = Qnil;
15810
15811 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
15812 doc: /* Non-nil means don't update menu bars. Internal use only. */);
15813 inhibit_menubar_update = 0;
15814
15815 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
15816 doc: /* Non-nil means don't eval Lisp during redisplay. */);
15817 inhibit_eval_during_redisplay = 0;
15818
15819 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
15820 doc: /* Non-nil means don't free realized faces. Internal use only. */);
15821 inhibit_free_realized_faces = 0;
15822
15823 #if GLYPH_DEBUG
15824 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
15825 doc: /* Inhibit try_window_id display optimization. */);
15826 inhibit_try_window_id = 0;
15827
15828 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
15829 doc: /* Inhibit try_window_reusing display optimization. */);
15830 inhibit_try_window_reusing = 0;
15831
15832 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
15833 doc: /* Inhibit try_cursor_movement display optimization. */);
15834 inhibit_try_cursor_movement = 0;
15835 #endif /* GLYPH_DEBUG */
15836 }
15837
15838
15839 /* Initialize this module when Emacs starts. */
15840
15841 void
15842 init_xdisp ()
15843 {
15844 Lisp_Object root_window;
15845 struct window *mini_w;
15846
15847 current_header_line_height = current_mode_line_height = -1;
15848
15849 CHARPOS (this_line_start_pos) = 0;
15850
15851 mini_w = XWINDOW (minibuf_window);
15852 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
15853
15854 if (!noninteractive)
15855 {
15856 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
15857 int i;
15858
15859 XWINDOW (root_window)->top = make_number (FRAME_TOP_MARGIN (f));
15860 set_window_height (root_window,
15861 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
15862 0);
15863 mini_w->top = make_number (FRAME_HEIGHT (f) - 1);
15864 set_window_height (minibuf_window, 1, 0);
15865
15866 XWINDOW (root_window)->width = make_number (FRAME_WIDTH (f));
15867 mini_w->width = make_number (FRAME_WIDTH (f));
15868
15869 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
15870 scratch_glyph_row.glyphs[TEXT_AREA + 1]
15871 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
15872
15873 /* The default ellipsis glyphs `...'. */
15874 for (i = 0; i < 3; ++i)
15875 default_invis_vector[i] = make_number ('.');
15876 }
15877
15878 {
15879 /* Allocate the buffer for frame titles.
15880 Also used for `format-mode-line'. */
15881 int size = 100;
15882 frame_title_buf = (char *) xmalloc (size);
15883 frame_title_buf_end = frame_title_buf + size;
15884 frame_title_ptr = NULL;
15885 }
15886
15887 help_echo_showing_p = 0;
15888 }
15889
15890